c-next 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/index.js +561 -78
  2. package/dist/index.js.map +3 -3
  3. package/package.json +3 -1
  4. package/src/transpiler/Transpiler.ts +1 -1
  5. package/src/transpiler/logic/analysis/FunctionCallAnalyzer.ts +194 -8
  6. package/src/transpiler/logic/analysis/__tests__/FunctionCallAnalyzer.test.ts +140 -0
  7. package/src/transpiler/logic/symbols/c/__tests__/CResolver.integration.test.ts +41 -0
  8. package/src/transpiler/logic/symbols/c/collectors/FunctionCollector.ts +11 -5
  9. package/src/transpiler/output/codegen/CodeGenerator.ts +195 -17
  10. package/src/transpiler/output/codegen/TypeResolver.ts +1 -1
  11. package/src/transpiler/output/codegen/__tests__/CodeGenerator.coverage.test.ts +129 -0
  12. package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +30 -5
  13. package/src/transpiler/output/codegen/assignment/handlers/AccessPatternHandlers.ts +2 -2
  14. package/src/transpiler/output/codegen/assignment/handlers/BitAccessHandlers.ts +2 -2
  15. package/src/transpiler/output/codegen/assignment/handlers/BitmapHandlers.ts +2 -2
  16. package/src/transpiler/output/codegen/assignment/handlers/RegisterHandlers.ts +4 -4
  17. package/src/transpiler/output/codegen/assignment/handlers/__tests__/AccessPatternHandlers.test.ts +4 -4
  18. package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitAccessHandlers.test.ts +8 -8
  19. package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitmapHandlers.test.ts +5 -5
  20. package/src/transpiler/output/codegen/assignment/handlers/__tests__/RegisterHandlers.test.ts +4 -4
  21. package/src/transpiler/output/codegen/generators/expressions/CallExprGenerator.ts +8 -1
  22. package/src/transpiler/output/codegen/generators/expressions/PostfixExpressionGenerator.ts +15 -3
  23. package/src/transpiler/output/codegen/helpers/ArgumentGenerator.ts +5 -0
  24. package/src/transpiler/output/codegen/helpers/FunctionContextManager.ts +63 -10
  25. package/src/transpiler/output/codegen/helpers/MemberSeparatorResolver.ts +28 -6
  26. package/src/transpiler/output/codegen/helpers/ParameterDereferenceResolver.ts +12 -0
  27. package/src/transpiler/output/codegen/helpers/ParameterInputAdapter.ts +30 -2
  28. package/src/transpiler/output/codegen/helpers/ParameterSignatureBuilder.ts +15 -7
  29. package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +8 -1
  30. package/src/transpiler/output/codegen/helpers/StringOperationsHelper.ts +1 -1
  31. package/src/transpiler/output/codegen/helpers/TypedefParamParser.ts +220 -0
  32. package/src/transpiler/output/codegen/helpers/VariableDeclHelper.ts +11 -0
  33. package/src/transpiler/output/codegen/helpers/VariableModifierBuilder.ts +16 -1
  34. package/src/transpiler/output/codegen/helpers/__tests__/FunctionContextManager.test.ts +5 -5
  35. package/src/transpiler/output/codegen/helpers/__tests__/MemberSeparatorResolver.test.ts +48 -36
  36. package/src/transpiler/output/codegen/helpers/__tests__/ParameterInputAdapter.test.ts +37 -0
  37. package/src/transpiler/output/codegen/helpers/__tests__/ParameterSignatureBuilder.test.ts +63 -0
  38. package/src/transpiler/output/codegen/helpers/__tests__/TypedefParamParser.test.ts +209 -0
  39. package/src/transpiler/output/codegen/helpers/__tests__/VariableModifierBuilder.test.ts +34 -2
  40. package/src/transpiler/output/codegen/resolution/EnumTypeResolver.ts +1 -1
  41. package/src/transpiler/output/codegen/resolution/SizeofResolver.ts +1 -1
  42. package/src/transpiler/output/codegen/types/IParameterInput.ts +13 -0
  43. package/src/transpiler/output/codegen/types/ISeparatorContext.ts +7 -0
  44. package/src/transpiler/output/codegen/types/TParameterInfo.ts +12 -0
  45. package/src/transpiler/output/codegen/types/TTypeInfo.ts +1 -0
  46. package/src/transpiler/output/codegen/utils/CodegenParserUtils.ts +1 -1
  47. package/src/transpiler/state/CodeGenState.ts +21 -2
  48. package/src/utils/BitUtils.ts +17 -13
  49. package/src/{transpiler/output/codegen/utils → utils}/ExpressionUnwrapper.ts +1 -1
  50. package/src/utils/__tests__/BitUtils.test.ts +56 -56
  51. package/src/{transpiler/output/codegen/utils → utils}/__tests__/ExpressionUnwrapper.test.ts +2 -2
@@ -5,28 +5,28 @@ import BitUtils from "../BitUtils";
5
5
  // boolToInt
6
6
  // ========================================================================
7
7
  describe("BitUtils.boolToInt", () => {
8
- it("converts literal true to 1", () => {
9
- expect(BitUtils.boolToInt("true")).toBe("1");
8
+ it("converts literal true to 1U (MISRA 10.1 compliance)", () => {
9
+ expect(BitUtils.boolToInt("true")).toBe("1U");
10
10
  });
11
11
 
12
- it("converts literal false to 0", () => {
13
- expect(BitUtils.boolToInt("false")).toBe("0");
12
+ it("converts literal false to 0U (MISRA 10.1 compliance)", () => {
13
+ expect(BitUtils.boolToInt("false")).toBe("0U");
14
14
  });
15
15
 
16
- it("wraps comparison expressions in ternary", () => {
17
- expect(BitUtils.boolToInt("x > 5")).toBe("(x > 5 ? 1 : 0)");
16
+ it("wraps comparison expressions in ternary with unsigned values", () => {
17
+ expect(BitUtils.boolToInt("x > 5")).toBe("(x > 5 ? 1U : 0U)");
18
18
  });
19
19
 
20
- it("wraps variable expressions in ternary", () => {
21
- expect(BitUtils.boolToInt("isEnabled")).toBe("(isEnabled ? 1 : 0)");
20
+ it("wraps variable expressions in ternary with unsigned values", () => {
21
+ expect(BitUtils.boolToInt("isEnabled")).toBe("(isEnabled ? 1U : 0U)");
22
22
  });
23
23
 
24
- it("wraps complex expressions in ternary", () => {
25
- expect(BitUtils.boolToInt("a && b")).toBe("(a && b ? 1 : 0)");
24
+ it("wraps complex expressions in ternary with unsigned values", () => {
25
+ expect(BitUtils.boolToInt("a && b")).toBe("(a && b ? 1U : 0U)");
26
26
  });
27
27
 
28
- it("wraps function calls in ternary", () => {
29
- expect(BitUtils.boolToInt("isReady()")).toBe("(isReady() ? 1 : 0)");
28
+ it("wraps function calls in ternary with unsigned values", () => {
29
+ expect(BitUtils.boolToInt("isReady()")).toBe("(isReady() ? 1U : 0U)");
30
30
  });
31
31
  });
32
32
 
@@ -96,20 +96,20 @@ describe("BitUtils.oneForType", () => {
96
96
  expect(BitUtils.oneForType("i64")).toBe("1ULL");
97
97
  });
98
98
 
99
- it("returns 1 for u32", () => {
100
- expect(BitUtils.oneForType("u32")).toBe("1");
99
+ it("returns 1U for u32 (MISRA 10.1 compliance)", () => {
100
+ expect(BitUtils.oneForType("u32")).toBe("1U");
101
101
  });
102
102
 
103
- it("returns 1 for i32", () => {
104
- expect(BitUtils.oneForType("i32")).toBe("1");
103
+ it("returns 1U for i32 (MISRA 10.1 compliance)", () => {
104
+ expect(BitUtils.oneForType("i32")).toBe("1U");
105
105
  });
106
106
 
107
- it("returns 1 for u8", () => {
108
- expect(BitUtils.oneForType("u8")).toBe("1");
107
+ it("returns 1U for u8 (MISRA 10.1 compliance)", () => {
108
+ expect(BitUtils.oneForType("u8")).toBe("1U");
109
109
  });
110
110
 
111
- it("returns 1 for unknown types", () => {
112
- expect(BitUtils.oneForType("custom")).toBe("1");
111
+ it("returns 1U for unknown types (MISRA 10.1 compliance)", () => {
112
+ expect(BitUtils.oneForType("custom")).toBe("1U");
113
113
  });
114
114
  });
115
115
 
@@ -117,28 +117,28 @@ describe("BitUtils.oneForType", () => {
117
117
  // formatHex
118
118
  // ========================================================================
119
119
  describe("BitUtils.formatHex", () => {
120
- it("formats 255 as 0xFF", () => {
121
- expect(BitUtils.formatHex(255)).toBe("0xFF");
120
+ it("formats 255 as 0xFFU (MISRA 10.1 - unsigned)", () => {
121
+ expect(BitUtils.formatHex(255)).toBe("0xFFU");
122
122
  });
123
123
 
124
- it("formats 31 as 0x1F", () => {
125
- expect(BitUtils.formatHex(31)).toBe("0x1F");
124
+ it("formats 31 as 0x1FU (MISRA 10.1 - unsigned)", () => {
125
+ expect(BitUtils.formatHex(31)).toBe("0x1FU");
126
126
  });
127
127
 
128
- it("formats 0 as 0x0", () => {
129
- expect(BitUtils.formatHex(0)).toBe("0x0");
128
+ it("formats 0 as 0x0U (MISRA 10.1 - unsigned)", () => {
129
+ expect(BitUtils.formatHex(0)).toBe("0x0U");
130
130
  });
131
131
 
132
- it("formats 65535 as 0xFFFF", () => {
133
- expect(BitUtils.formatHex(65535)).toBe("0xFFFF");
132
+ it("formats 65535 as 0xFFFFU (MISRA 10.1 - unsigned)", () => {
133
+ expect(BitUtils.formatHex(65535)).toBe("0xFFFFU");
134
134
  });
135
135
 
136
- it("formats single digit as 0xN", () => {
137
- expect(BitUtils.formatHex(10)).toBe("0xA");
136
+ it("formats single digit as 0xNU (MISRA 10.1 - unsigned)", () => {
137
+ expect(BitUtils.formatHex(10)).toBe("0xAU");
138
138
  });
139
139
 
140
- it("formats large values correctly", () => {
141
- expect(BitUtils.formatHex(0xdeadbeef)).toBe("0xDEADBEEF");
140
+ it("formats large values correctly with U suffix", () => {
141
+ expect(BitUtils.formatHex(0xdeadbeef)).toBe("0xDEADBEEFU");
142
142
  });
143
143
  });
144
144
 
@@ -286,58 +286,58 @@ describe("BitUtils.bitRangeRead", () => {
286
286
  // singleBitWrite
287
287
  // ========================================================================
288
288
  describe("BitUtils.singleBitWrite", () => {
289
- it("generates RMW for literal true", () => {
289
+ it("generates RMW for literal true (MISRA 10.1 - uses 1U)", () => {
290
290
  expect(BitUtils.singleBitWrite("flags", 0, "true")).toBe(
291
- "flags = (flags & ~(1 << 0)) | (1 << 0);",
291
+ "flags = (flags & ~(1U << 0)) | (1U << 0);",
292
292
  );
293
293
  });
294
294
 
295
- it("generates RMW for literal false", () => {
295
+ it("generates RMW for literal false (MISRA 10.1 - uses 1U)", () => {
296
296
  expect(BitUtils.singleBitWrite("flags", 3, "false")).toBe(
297
- "flags = (flags & ~(1 << 3)) | (0 << 3);",
297
+ "flags = (flags & ~(1U << 3)) | (0U << 3);",
298
298
  );
299
299
  });
300
300
 
301
- it("generates RMW with ternary for expression value", () => {
301
+ it("generates RMW with ternary for expression value (MISRA 10.1)", () => {
302
302
  expect(BitUtils.singleBitWrite("reg", 7, "isEnabled")).toBe(
303
- "reg = (reg & ~(1 << 7)) | ((isEnabled ? 1 : 0) << 7);",
303
+ "reg = (reg & ~(1U << 7)) | ((isEnabled ? 1U : 0U) << 7);",
304
304
  );
305
305
  });
306
306
 
307
- it("generates RMW with dynamic offset", () => {
307
+ it("generates RMW with dynamic offset (MISRA 10.1 - uses 1U)", () => {
308
308
  expect(BitUtils.singleBitWrite("byte", "n", "true")).toBe(
309
- "byte = (byte & ~(1 << n)) | (1 << n);",
309
+ "byte = (byte & ~(1U << n)) | (1U << n);",
310
310
  );
311
311
  });
312
312
 
313
- it("handles comparison expression as value", () => {
313
+ it("handles comparison expression as value (MISRA 10.1)", () => {
314
314
  expect(BitUtils.singleBitWrite("status", 0, "x > 5")).toBe(
315
- "status = (status & ~(1 << 0)) | ((x > 5 ? 1 : 0) << 0);",
315
+ "status = (status & ~(1U << 0)) | ((x > 5 ? 1U : 0U) << 0);",
316
316
  );
317
317
  });
318
318
 
319
319
  // 64-bit target tests (targetType parameter)
320
320
  it("generates 64-bit RMW for u64 target at high position", () => {
321
321
  expect(BitUtils.singleBitWrite("val64", 32, "true", "u64")).toBe(
322
- "val64 = (val64 & ~(1ULL << 32)) | ((uint64_t)1 << 32);",
322
+ "val64 = (val64 & ~(1ULL << 32)) | ((uint64_t)1U << 32);",
323
323
  );
324
324
  });
325
325
 
326
326
  it("generates 64-bit RMW for u64 target at position 63", () => {
327
327
  expect(BitUtils.singleBitWrite("val64", 63, "true", "u64")).toBe(
328
- "val64 = (val64 & ~(1ULL << 63)) | ((uint64_t)1 << 63);",
328
+ "val64 = (val64 & ~(1ULL << 63)) | ((uint64_t)1U << 63);",
329
329
  );
330
330
  });
331
331
 
332
332
  it("generates 64-bit RMW with expression value for u64", () => {
333
333
  expect(BitUtils.singleBitWrite("flags", 48, "isSet", "u64")).toBe(
334
- "flags = (flags & ~(1ULL << 48)) | ((uint64_t)(isSet ? 1 : 0) << 48);",
334
+ "flags = (flags & ~(1ULL << 48)) | ((uint64_t)(isSet ? 1U : 0U) << 48);",
335
335
  );
336
336
  });
337
337
 
338
338
  it("generates 64-bit RMW for i64 target", () => {
339
339
  expect(BitUtils.singleBitWrite("val64", 40, "false", "i64")).toBe(
340
- "val64 = (val64 & ~(1ULL << 40)) | ((uint64_t)0 << 40);",
340
+ "val64 = (val64 & ~(1ULL << 40)) | ((uint64_t)0U << 40);",
341
341
  );
342
342
  });
343
343
  });
@@ -406,33 +406,33 @@ describe("BitUtils.multiBitWrite", () => {
406
406
  // writeOnlySingleBit
407
407
  // ========================================================================
408
408
  describe("BitUtils.writeOnlySingleBit", () => {
409
- it("generates write-only for literal true", () => {
409
+ it("generates write-only for literal true (MISRA 10.1 - uses 1U)", () => {
410
410
  expect(BitUtils.writeOnlySingleBit("SET_REG", 0, "true")).toBe(
411
- "SET_REG = (1 << 0);",
411
+ "SET_REG = (1U << 0);",
412
412
  );
413
413
  });
414
414
 
415
- it("generates write-only for literal false", () => {
415
+ it("generates write-only for literal false (MISRA 10.1 - uses 0U)", () => {
416
416
  expect(BitUtils.writeOnlySingleBit("CLR_REG", 3, "false")).toBe(
417
- "CLR_REG = (0 << 3);",
417
+ "CLR_REG = (0U << 3);",
418
418
  );
419
419
  });
420
420
 
421
- it("generates write-only with ternary for expression", () => {
421
+ it("generates write-only with ternary for expression (MISRA 10.1)", () => {
422
422
  expect(BitUtils.writeOnlySingleBit("CTRL", 7, "isActive")).toBe(
423
- "CTRL = ((isActive ? 1 : 0) << 7);",
423
+ "CTRL = ((isActive ? 1U : 0U) << 7);",
424
424
  );
425
425
  });
426
426
 
427
- it("handles dynamic offset", () => {
427
+ it("handles dynamic offset (MISRA 10.1 - uses 1U)", () => {
428
428
  expect(BitUtils.writeOnlySingleBit("PORT", "bit", "true")).toBe(
429
- "PORT = (1 << bit);",
429
+ "PORT = (1U << bit);",
430
430
  );
431
431
  });
432
432
 
433
- it("handles expression offset", () => {
433
+ it("handles expression offset (MISRA 10.1 - uses 0U)", () => {
434
434
  expect(BitUtils.writeOnlySingleBit("REG", "i + 1", "false")).toBe(
435
- "REG = (0 << i + 1);",
435
+ "REG = (0U << i + 1);",
436
436
  );
437
437
  });
438
438
  });
@@ -4,8 +4,8 @@
4
4
 
5
5
  import { describe, it, expect } from "vitest";
6
6
  import ExpressionUnwrapper from "../ExpressionUnwrapper";
7
- import CNextSourceParser from "../../../../logic/parser/CNextSourceParser";
8
- import * as Parser from "../../../../logic/parser/grammar/CNextParser";
7
+ import CNextSourceParser from "../../transpiler/logic/parser/CNextSourceParser";
8
+ import * as Parser from "../../transpiler/logic/parser/grammar/CNextParser";
9
9
 
10
10
  /**
11
11
  * Helper to parse a simple expression and get its ExpressionContext