c-next 0.1.69 → 0.1.71

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 (247) hide show
  1. package/package.json +1 -1
  2. package/src/lib/__tests__/parseCHeader.mocked.test.ts +69 -54
  3. package/src/lib/parseCHeader.ts +56 -23
  4. package/src/lib/parseWithSymbols.ts +195 -53
  5. package/src/transpiler/Transpiler.ts +173 -60
  6. package/src/transpiler/logic/analysis/FunctionCallAnalyzer.ts +240 -205
  7. package/src/transpiler/logic/analysis/InitializationAnalyzer.ts +1 -2
  8. package/src/transpiler/logic/analysis/PassByValueAnalyzer.ts +742 -0
  9. package/src/transpiler/logic/analysis/__tests__/FunctionCallAnalyzer.test.ts +102 -15
  10. package/src/transpiler/logic/analysis/__tests__/InitializationAnalyzer.test.ts +9 -9
  11. package/src/transpiler/logic/analysis/__tests__/runAnalyzers.test.ts +5 -5
  12. package/src/transpiler/{output/codegen → logic/analysis}/helpers/AssignmentTargetExtractor.ts +1 -1
  13. package/src/transpiler/{output/codegen → logic/analysis}/helpers/ChildStatementCollector.ts +1 -1
  14. package/src/transpiler/{output/codegen → logic/analysis}/helpers/StatementExpressionCollector.ts +1 -1
  15. package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/AssignmentTargetExtractor.test.ts +2 -2
  16. package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/ChildStatementCollector.test.ts +2 -2
  17. package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/StatementExpressionCollector.test.ts +2 -2
  18. package/src/transpiler/logic/symbols/SymbolTable.ts +676 -258
  19. package/src/transpiler/logic/symbols/SymbolUtils.ts +2 -2
  20. package/src/transpiler/logic/symbols/__tests__/SymbolTable.test.ts +290 -782
  21. package/src/transpiler/logic/symbols/c/__tests__/CResolver.integration.test.ts +573 -0
  22. package/src/transpiler/logic/symbols/c/__tests__/testHelpers.ts +20 -0
  23. package/src/transpiler/logic/symbols/c/collectors/EnumCollector.ts +82 -0
  24. package/src/transpiler/logic/symbols/c/collectors/FunctionCollector.ts +106 -0
  25. package/src/transpiler/logic/symbols/c/collectors/StructCollector.ts +173 -0
  26. package/src/transpiler/logic/symbols/c/collectors/TypedefCollector.ts +35 -0
  27. package/src/transpiler/logic/symbols/c/collectors/VariableCollector.ts +80 -0
  28. package/src/transpiler/logic/symbols/c/index.ts +333 -0
  29. package/src/transpiler/logic/symbols/c/utils/DeclaratorUtils.ts +269 -0
  30. package/src/transpiler/logic/symbols/cnext/__tests__/BitmapCollector.test.ts +50 -11
  31. package/src/transpiler/logic/symbols/cnext/__tests__/CNextResolver.integration.test.ts +45 -34
  32. package/src/transpiler/logic/symbols/cnext/__tests__/EnumCollector.test.ts +30 -13
  33. package/src/transpiler/logic/symbols/cnext/__tests__/FunctionCollector.test.ts +279 -64
  34. package/src/transpiler/logic/symbols/cnext/__tests__/RegisterCollector.test.ts +60 -13
  35. package/src/transpiler/logic/symbols/cnext/__tests__/ScopeCollector.test.ts +40 -37
  36. package/src/transpiler/logic/symbols/cnext/__tests__/StructCollector.test.ts +131 -45
  37. package/src/transpiler/logic/symbols/cnext/__tests__/TSymbolInfoAdapter.test.ts +223 -139
  38. package/src/transpiler/logic/symbols/cnext/__tests__/VariableCollector.test.ts +79 -25
  39. package/src/transpiler/logic/symbols/cnext/__tests__/testUtils.ts +53 -0
  40. package/src/transpiler/logic/symbols/cnext/adapters/TSymbolInfoAdapter.ts +83 -43
  41. package/src/transpiler/logic/symbols/cnext/collectors/BitmapCollector.ts +14 -13
  42. package/src/transpiler/logic/symbols/cnext/collectors/EnumCollector.ts +11 -10
  43. package/src/transpiler/logic/symbols/cnext/collectors/FunctionCollector.ts +83 -34
  44. package/src/transpiler/logic/symbols/cnext/collectors/RegisterCollector.ts +22 -18
  45. package/src/transpiler/logic/symbols/cnext/collectors/ScopeCollector.ts +53 -35
  46. package/src/transpiler/logic/symbols/cnext/collectors/StructCollector.ts +30 -23
  47. package/src/transpiler/logic/symbols/cnext/collectors/VariableCollector.ts +18 -19
  48. package/src/transpiler/logic/symbols/cnext/index.ts +36 -14
  49. package/src/transpiler/logic/symbols/cnext/types/IScopeCollectorResult.ts +2 -2
  50. package/src/transpiler/logic/symbols/cnext/utils/SymbolNameUtils.ts +27 -0
  51. package/src/transpiler/logic/symbols/cpp/__tests__/CppResolver.integration.test.ts +270 -0
  52. package/src/transpiler/logic/symbols/cpp/__tests__/testHelpers.ts +20 -0
  53. package/src/transpiler/logic/symbols/cpp/collectors/ClassCollector.ts +317 -0
  54. package/src/transpiler/logic/symbols/cpp/collectors/EnumCollector.ts +71 -0
  55. package/src/transpiler/logic/symbols/cpp/collectors/FunctionCollector.ts +155 -0
  56. package/src/transpiler/logic/symbols/cpp/collectors/NamespaceCollector.ts +65 -0
  57. package/src/transpiler/logic/symbols/cpp/collectors/TypeAliasCollector.ts +46 -0
  58. package/src/transpiler/logic/symbols/cpp/collectors/VariableCollector.ts +54 -0
  59. package/src/transpiler/logic/symbols/cpp/index.ts +366 -0
  60. package/src/transpiler/logic/symbols/cpp/utils/DeclaratorUtils.ts +248 -0
  61. package/src/transpiler/logic/symbols/shared/IExtractedParameter.ts +18 -0
  62. package/src/transpiler/logic/symbols/shared/ParameterExtractorUtils.ts +73 -0
  63. package/src/transpiler/output/codegen/CodeGenerator.ts +310 -2288
  64. package/src/transpiler/output/codegen/TypeRegistrationUtils.ts +4 -6
  65. package/src/transpiler/output/codegen/TypeResolver.ts +2 -2
  66. package/src/transpiler/output/codegen/TypeValidator.ts +5 -5
  67. package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +7 -1
  68. package/src/transpiler/output/codegen/__tests__/TypeRegistrationUtils.test.ts +36 -51
  69. package/src/transpiler/output/codegen/__tests__/TypeResolver.test.ts +20 -17
  70. package/src/transpiler/output/codegen/__tests__/TypeValidator.resolution.test.ts +3 -3
  71. package/src/transpiler/output/codegen/__tests__/TypeValidator.test.ts +1 -1
  72. package/src/transpiler/output/codegen/analysis/MemberChainAnalyzer.ts +1 -1
  73. package/src/transpiler/output/codegen/analysis/StringLengthCounter.ts +1 -1
  74. package/src/transpiler/output/codegen/analysis/__tests__/MemberChainAnalyzer.test.ts +9 -9
  75. package/src/transpiler/output/codegen/analysis/__tests__/StringLengthCounter.test.ts +12 -12
  76. package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +13 -12
  77. package/src/transpiler/output/codegen/assignment/__tests__/AssignmentClassifier.test.ts +23 -17
  78. package/src/transpiler/output/codegen/assignment/handlers/ArrayHandlers.ts +2 -2
  79. package/src/transpiler/output/codegen/assignment/handlers/AssignmentHandlerUtils.ts +7 -1
  80. package/src/transpiler/output/codegen/assignment/handlers/BitAccessHandlers.ts +3 -3
  81. package/src/transpiler/output/codegen/assignment/handlers/BitmapHandlers.ts +9 -5
  82. package/src/transpiler/output/codegen/assignment/handlers/RegisterHandlers.ts +2 -1
  83. package/src/transpiler/output/codegen/assignment/handlers/SpecialHandlers.ts +4 -4
  84. package/src/transpiler/output/codegen/assignment/handlers/StringHandlers.ts +5 -5
  85. package/src/transpiler/output/codegen/assignment/handlers/__tests__/ArrayHandlers.test.ts +23 -25
  86. package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitAccessHandlers.test.ts +20 -36
  87. package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitmapHandlers.test.ts +18 -18
  88. package/src/transpiler/output/codegen/assignment/handlers/__tests__/SpecialHandlers.test.ts +42 -32
  89. package/src/transpiler/output/codegen/assignment/handlers/__tests__/handlerTestUtils.ts +5 -4
  90. package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +21 -8
  91. package/src/transpiler/output/codegen/generators/declarationGenerators/ScopedRegisterGenerator.ts +3 -2
  92. package/src/transpiler/output/codegen/generators/expressions/CallExprGenerator.ts +14 -6
  93. package/src/transpiler/output/codegen/generators/expressions/CallExprUtils.ts +9 -3
  94. package/src/transpiler/output/codegen/generators/expressions/PostfixExpressionGenerator.ts +19 -16
  95. package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprGenerator.test.ts +24 -8
  96. package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprUtils.test.ts +4 -8
  97. package/src/transpiler/output/codegen/generators/expressions/__tests__/PostfixExpressionGenerator.test.ts +15 -2
  98. package/src/transpiler/output/codegen/helpers/ArgumentGenerator.ts +236 -0
  99. package/src/transpiler/output/codegen/helpers/ArrayInitHelper.ts +2 -1
  100. package/src/transpiler/output/codegen/helpers/AssignmentExpectedTypeResolver.ts +2 -2
  101. package/src/transpiler/output/codegen/helpers/AssignmentValidator.ts +3 -3
  102. package/src/transpiler/output/codegen/helpers/CppConstructorHelper.ts +3 -3
  103. package/src/transpiler/output/codegen/helpers/EnumAssignmentValidator.ts +1 -1
  104. package/src/transpiler/output/codegen/helpers/FunctionContextManager.ts +435 -0
  105. package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +2 -2
  106. package/src/transpiler/output/codegen/helpers/StringOperationsHelper.ts +203 -0
  107. package/src/transpiler/output/codegen/helpers/SymbolLookupHelper.ts +8 -12
  108. package/src/transpiler/output/codegen/helpers/TypeRegistrationEngine.ts +520 -0
  109. package/src/transpiler/output/codegen/helpers/VariableDeclHelper.ts +735 -0
  110. package/src/transpiler/output/codegen/helpers/VariableDeclarationFormatter.ts +1 -1
  111. package/src/transpiler/output/codegen/helpers/__tests__/ArgumentGenerator.test.ts +521 -0
  112. package/src/transpiler/output/codegen/helpers/__tests__/ArrayInitHelper.test.ts +1 -1
  113. package/src/transpiler/output/codegen/helpers/__tests__/AssignmentExpectedTypeResolver.test.ts +7 -7
  114. package/src/transpiler/output/codegen/helpers/__tests__/AssignmentValidator.test.ts +7 -7
  115. package/src/transpiler/output/codegen/helpers/__tests__/CppConstructorHelper.test.ts +4 -5
  116. package/src/transpiler/output/codegen/helpers/__tests__/EnumAssignmentValidator.test.ts +2 -2
  117. package/src/transpiler/output/codegen/helpers/__tests__/FunctionContextManager.test.ts +983 -0
  118. package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +4 -4
  119. package/src/transpiler/output/codegen/helpers/__tests__/StringOperationsHelper.test.ts +269 -0
  120. package/src/transpiler/output/codegen/helpers/__tests__/SymbolLookupHelper.test.ts +31 -32
  121. package/src/transpiler/output/codegen/helpers/__tests__/TypeRegistrationEngine.test.ts +186 -0
  122. package/src/transpiler/output/codegen/helpers/__tests__/VariableDeclHelper.test.ts +460 -0
  123. package/src/transpiler/output/codegen/helpers/types/IArgumentGeneratorCallbacks.ts +32 -0
  124. package/src/transpiler/output/codegen/resolution/EnumTypeResolver.ts +7 -3
  125. package/src/transpiler/output/codegen/resolution/__tests__/EnumTypeResolver.test.ts +5 -5
  126. package/src/transpiler/output/codegen/types/IFunctionContextCallbacks.ts +12 -0
  127. package/src/transpiler/output/codegen/types/IVariableFormatInput.ts +1 -1
  128. package/src/transpiler/output/codegen/utils/QualifiedNameGenerator.ts +114 -0
  129. package/src/transpiler/output/codegen/utils/__tests__/QualifiedNameGenerator.test.ts +183 -0
  130. package/src/transpiler/output/headers/BaseHeaderGenerator.ts +4 -4
  131. package/src/transpiler/output/headers/ExternalTypeHeaderBuilder.ts +7 -7
  132. package/src/transpiler/output/headers/HeaderGenerator.ts +9 -7
  133. package/src/transpiler/output/headers/HeaderGeneratorUtils.ts +19 -20
  134. package/src/transpiler/output/headers/__tests__/BaseHeaderGenerator.test.ts +15 -18
  135. package/src/transpiler/output/headers/__tests__/CHeaderGenerator.test.ts +63 -64
  136. package/src/transpiler/output/headers/__tests__/CppHeaderGenerator.test.ts +36 -32
  137. package/src/transpiler/output/headers/__tests__/ExternalTypeHeaderBuilder.test.ts +26 -26
  138. package/src/transpiler/output/headers/__tests__/HeaderGenerator.test.ts +87 -59
  139. package/src/transpiler/output/headers/__tests__/HeaderGeneratorUtils.test.ts +57 -58
  140. package/src/transpiler/output/headers/adapters/HeaderSymbolAdapter.ts +222 -0
  141. package/src/transpiler/output/headers/adapters/__tests__/HeaderSymbolAdapter.test.ts +538 -0
  142. package/src/transpiler/output/headers/types/IGroupedSymbols.ts +8 -8
  143. package/src/transpiler/output/headers/types/IHeaderSymbol.ts +62 -0
  144. package/src/transpiler/state/CodeGenState.ts +109 -4
  145. package/src/transpiler/state/SymbolRegistry.ts +181 -0
  146. package/src/transpiler/{types → state}/TranspilerState.ts +1 -1
  147. package/src/transpiler/state/__tests__/CodeGenState.test.ts +277 -1
  148. package/src/transpiler/state/__tests__/SymbolRegistry.test.ts +249 -0
  149. package/src/transpiler/{types → state}/__tests__/TranspilerState.test.ts +1 -1
  150. package/src/transpiler/types/ICachedFileEntry.ts +1 -1
  151. package/src/transpiler/types/IConflict.ts +14 -0
  152. package/src/transpiler/types/ISerializedSymbol.ts +11 -0
  153. package/src/transpiler/types/TPrimitiveKind.ts +20 -0
  154. package/src/transpiler/types/TType.ts +103 -0
  155. package/src/transpiler/types/TVisibility.ts +6 -0
  156. package/src/transpiler/types/symbol-kinds/TSymbolKind.ts +10 -0
  157. package/src/transpiler/types/symbol-kinds/TSymbolKindC.ts +12 -0
  158. package/src/transpiler/types/symbol-kinds/TSymbolKindCNext.ts +16 -0
  159. package/src/transpiler/types/symbol-kinds/TSymbolKindCpp.ts +14 -0
  160. package/src/transpiler/types/symbols/IBaseSymbol.ts +31 -0
  161. package/src/transpiler/{logic/symbols/types → types/symbols}/IBitmapFieldInfo.ts +2 -2
  162. package/src/transpiler/types/symbols/IBitmapSymbol.ts +21 -0
  163. package/src/transpiler/{logic/symbols/types → types/symbols}/IEnumSymbol.ts +5 -6
  164. package/src/transpiler/types/symbols/IFieldInfo.ts +26 -0
  165. package/src/transpiler/types/symbols/IFunctionSymbol.ts +30 -0
  166. package/src/transpiler/types/symbols/IParameterInfo.ts +26 -0
  167. package/src/transpiler/{logic/symbols/types → types/symbols}/IRegisterMemberInfo.ts +4 -4
  168. package/src/transpiler/types/symbols/IRegisterSymbol.ts +18 -0
  169. package/src/transpiler/types/symbols/IScopeSymbol.ts +32 -0
  170. package/src/transpiler/{logic/symbols/types → types/symbols}/IStructFieldInfo.ts +2 -1
  171. package/src/transpiler/types/symbols/IStructSymbol.ts +15 -0
  172. package/src/transpiler/types/symbols/IVariableSymbol.ts +30 -0
  173. package/src/transpiler/types/symbols/SymbolGuards.ts +43 -0
  174. package/src/transpiler/types/symbols/TAnySymbol.ts +22 -0
  175. package/src/transpiler/types/symbols/TSymbol.ts +32 -0
  176. package/src/transpiler/types/symbols/__tests__/IBaseSymbol.test.ts +56 -0
  177. package/src/transpiler/types/symbols/__tests__/SymbolGuards.test.ts +57 -0
  178. package/src/transpiler/types/symbols/c/ICBaseSymbol.ts +28 -0
  179. package/src/transpiler/types/symbols/c/ICEnumMemberSymbol.ts +17 -0
  180. package/src/transpiler/types/symbols/c/ICEnumSymbol.ts +17 -0
  181. package/src/transpiler/types/symbols/c/ICFieldInfo.ts +16 -0
  182. package/src/transpiler/types/symbols/c/ICFunctionSymbol.ts +21 -0
  183. package/src/transpiler/types/symbols/c/ICParameterInfo.ts +19 -0
  184. package/src/transpiler/types/symbols/c/ICStructSymbol.ts +21 -0
  185. package/src/transpiler/types/symbols/c/ICTypedefSymbol.ts +14 -0
  186. package/src/transpiler/types/symbols/c/ICVariableSymbol.ts +26 -0
  187. package/src/transpiler/types/symbols/c/TCSymbol.ts +26 -0
  188. package/src/transpiler/types/symbols/cpp/ICppBaseSymbol.ts +31 -0
  189. package/src/transpiler/types/symbols/cpp/ICppClassSymbol.ts +15 -0
  190. package/src/transpiler/types/symbols/cpp/ICppEnumMemberSymbol.ts +14 -0
  191. package/src/transpiler/types/symbols/cpp/ICppEnumSymbol.ts +14 -0
  192. package/src/transpiler/types/symbols/cpp/ICppFieldInfo.ts +16 -0
  193. package/src/transpiler/types/symbols/cpp/ICppFunctionSymbol.ts +21 -0
  194. package/src/transpiler/types/symbols/cpp/ICppNamespaceSymbol.ts +11 -0
  195. package/src/transpiler/types/symbols/cpp/ICppParameterInfo.ts +19 -0
  196. package/src/transpiler/types/symbols/cpp/ICppStructSymbol.ts +16 -0
  197. package/src/transpiler/types/symbols/cpp/ICppTypeAliasSymbol.ts +14 -0
  198. package/src/transpiler/types/symbols/cpp/ICppVariableSymbol.ts +23 -0
  199. package/src/transpiler/types/symbols/cpp/TCppSymbol.ts +30 -0
  200. package/src/utils/CppNamespaceUtils.ts +3 -4
  201. package/src/utils/FunctionUtils.ts +92 -0
  202. package/src/utils/ParameterUtils.ts +55 -0
  203. package/src/utils/PrimitiveKindUtils.ts +33 -0
  204. package/src/utils/ScopeUtils.ts +105 -0
  205. package/src/utils/TTypeUtils.ts +159 -0
  206. package/src/utils/TypeResolver.ts +132 -0
  207. package/src/utils/__tests__/CppNamespaceUtils.test.ts +92 -99
  208. package/src/utils/__tests__/FunctionUtils.test.ts +284 -0
  209. package/src/utils/__tests__/ParameterUtils.test.ts +174 -0
  210. package/src/utils/__tests__/PrimitiveKindUtils.test.ts +59 -0
  211. package/src/utils/__tests__/ScopeUtils.test.ts +53 -0
  212. package/src/utils/__tests__/TTypeUtils.test.ts +245 -0
  213. package/src/utils/__tests__/TypeResolver.test.ts +332 -0
  214. package/src/utils/cache/CacheManager.ts +91 -50
  215. package/src/utils/cache/__tests__/CacheManager.test.ts +180 -114
  216. package/src/transpiler/logic/symbols/AutoConstUpdater.ts +0 -93
  217. package/src/transpiler/logic/symbols/CSymbolCollector.ts +0 -648
  218. package/src/transpiler/logic/symbols/CppSymbolCollector.ts +0 -874
  219. package/src/transpiler/logic/symbols/SymbolCollectorContext.ts +0 -68
  220. package/src/transpiler/logic/symbols/__tests__/AutoConstUpdater.test.ts +0 -418
  221. package/src/transpiler/logic/symbols/__tests__/CSymbolCollector.test.ts +0 -685
  222. package/src/transpiler/logic/symbols/__tests__/CppSymbolCollector.test.ts +0 -1146
  223. package/src/transpiler/logic/symbols/__tests__/SymbolCollectorContext.test.ts +0 -290
  224. package/src/transpiler/logic/symbols/__tests__/cTestHelpers.ts +0 -43
  225. package/src/transpiler/logic/symbols/__tests__/cppTestHelpers.ts +0 -40
  226. package/src/transpiler/logic/symbols/cnext/__tests__/TSymbolAdapter.test.ts +0 -595
  227. package/src/transpiler/logic/symbols/cnext/adapters/TSymbolAdapter.ts +0 -345
  228. package/src/transpiler/logic/symbols/types/IBaseSymbol.ts +0 -27
  229. package/src/transpiler/logic/symbols/types/IBitmapSymbol.ts +0 -23
  230. package/src/transpiler/logic/symbols/types/ICollectorContext.ts +0 -19
  231. package/src/transpiler/logic/symbols/types/IConflict.ts +0 -20
  232. package/src/transpiler/logic/symbols/types/IFieldInfo.ts +0 -18
  233. package/src/transpiler/logic/symbols/types/IFunctionSymbol.ts +0 -25
  234. package/src/transpiler/logic/symbols/types/IParameterInfo.ts +0 -24
  235. package/src/transpiler/logic/symbols/types/IRegisterSymbol.ts +0 -20
  236. package/src/transpiler/logic/symbols/types/IScopeSymbol.ts +0 -19
  237. package/src/transpiler/logic/symbols/types/IStructSymbol.ts +0 -16
  238. package/src/transpiler/logic/symbols/types/IVariableSymbol.ts +0 -30
  239. package/src/transpiler/logic/symbols/types/TSymbol.ts +0 -36
  240. package/src/transpiler/logic/symbols/types/__tests__/SymbolGuards.test.ts +0 -244
  241. package/src/transpiler/logic/symbols/types/typeGuards.ts +0 -44
  242. package/src/utils/types/ESymbolKind.ts +0 -19
  243. package/src/utils/types/ISymbol.ts +0 -64
  244. /package/src/transpiler/{types → constants}/BITMAP_BACKING_TYPE.ts +0 -0
  245. /package/src/transpiler/{types → constants}/BITMAP_SIZE.ts +0 -0
  246. /package/src/transpiler/{output/codegen → logic/analysis}/helpers/TransitiveModificationPropagator.ts +0 -0
  247. /package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/TransitiveModificationPropagator.test.ts +0 -0
@@ -0,0 +1,435 @@
1
+ /**
2
+ * FunctionContextManager - Manages function context lifecycle and parameter processing
3
+ *
4
+ * Issue #793: Extracted from CodeGenerator to reduce file size.
5
+ *
6
+ * Handles:
7
+ * - Function context setup/cleanup lifecycle
8
+ * - Parameter type resolution and registration
9
+ * - Return type resolution (including main() special case)
10
+ * - Function body enter/exit coordination
11
+ */
12
+
13
+ import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
14
+ import CodeGenState from "../../../state/CodeGenState.js";
15
+ import TYPE_WIDTH from "../types/TYPE_WIDTH.js";
16
+ import ArrayDimensionParser from "./ArrayDimensionParser.js";
17
+ import IFunctionContextCallbacks from "../types/IFunctionContextCallbacks.js";
18
+
19
+ /**
20
+ * Result from resolving parameter type information.
21
+ */
22
+ interface IParameterTypeInfo {
23
+ typeName: string;
24
+ isStruct: boolean;
25
+ isCallback: boolean;
26
+ isString: boolean;
27
+ }
28
+
29
+ /**
30
+ * Result from resolving return type and params for a function.
31
+ */
32
+ interface IReturnTypeAndParams {
33
+ actualReturnType: string;
34
+ initialParams: string;
35
+ }
36
+
37
+ /**
38
+ * Manages function context lifecycle and parameter processing.
39
+ */
40
+ class FunctionContextManager {
41
+ /**
42
+ * Set up context for function generation.
43
+ * - Sets current function name (with scope prefix if in a scope)
44
+ * - Sets return type for enum inference
45
+ * - Processes parameters for ADR-006 pointer semantics
46
+ * - Clears local variables and marks in function body
47
+ */
48
+ static setupFunctionContext(
49
+ name: string,
50
+ ctx: Parser.FunctionDeclarationContext,
51
+ callbacks: IFunctionContextCallbacks,
52
+ ): void {
53
+ // Issue #269: Set current function name for pass-by-value lookup
54
+ const fullFuncName = CodeGenState.currentScope
55
+ ? `${CodeGenState.currentScope}_${name}`
56
+ : name;
57
+ CodeGenState.currentFunctionName = fullFuncName;
58
+
59
+ // Issue #477: Set return type for enum inference in return statements
60
+ CodeGenState.currentFunctionReturnType = ctx.type().getText();
61
+
62
+ // Track parameters for ADR-006 pointer semantics
63
+ FunctionContextManager.processParameterList(
64
+ ctx.parameterList() ?? null,
65
+ callbacks,
66
+ );
67
+
68
+ // ADR-016: Clear local variables and mark that we're in a function body
69
+ CodeGenState.localVariables.clear();
70
+ CodeGenState.floatBitShadows.clear();
71
+ CodeGenState.floatShadowCurrent.clear();
72
+ CodeGenState.inFunctionBody = true;
73
+ }
74
+
75
+ /**
76
+ * Clean up context after function generation.
77
+ * Resets all function-related state.
78
+ */
79
+ static cleanupFunctionContext(): void {
80
+ CodeGenState.inFunctionBody = false;
81
+ CodeGenState.localVariables.clear();
82
+ CodeGenState.floatBitShadows.clear();
83
+ CodeGenState.floatShadowCurrent.clear();
84
+ CodeGenState.mainArgsName = null;
85
+ CodeGenState.currentFunctionName = null;
86
+ CodeGenState.currentFunctionReturnType = null;
87
+ FunctionContextManager.clearParameters();
88
+ }
89
+
90
+ /**
91
+ * Resolve return type and initial params for function.
92
+ * Handles main() special cases:
93
+ * - main(u8 args[][]) -> int main(int argc, char *argv[])
94
+ * - main() -> int main() (for C++ compatibility)
95
+ */
96
+ static resolveReturnTypeAndParams(
97
+ name: string,
98
+ returnType: string,
99
+ isMainWithArgs: boolean,
100
+ ctx: Parser.FunctionDeclarationContext,
101
+ ): IReturnTypeAndParams {
102
+ if (isMainWithArgs) {
103
+ // Special case: main(u8 args[][]) -> int main(int argc, char *argv[])
104
+ const argsParam = ctx.parameterList()!.parameter()[0];
105
+ CodeGenState.mainArgsName = argsParam.IDENTIFIER().getText();
106
+ return {
107
+ actualReturnType: "int",
108
+ initialParams: "int argc, char *argv[]",
109
+ };
110
+ }
111
+
112
+ // For main() without args, always use int return type for C++ compatibility
113
+ const actualReturnType = name === "main" ? "int" : returnType;
114
+ return { actualReturnType, initialParams: "" };
115
+ }
116
+
117
+ /**
118
+ * Process parameter list and register parameters in state.
119
+ */
120
+ static processParameterList(
121
+ params: Parser.ParameterListContext | null,
122
+ callbacks: IFunctionContextCallbacks,
123
+ ): void {
124
+ CodeGenState.currentParameters.clear();
125
+ if (!params) return;
126
+
127
+ for (const param of params.parameter()) {
128
+ FunctionContextManager.processParameter(param, callbacks);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Process a single parameter declaration.
134
+ */
135
+ static processParameter(
136
+ param: Parser.ParameterContext,
137
+ callbacks: IFunctionContextCallbacks,
138
+ ): void {
139
+ const name = param.IDENTIFIER().getText();
140
+ // Check both C-Next style (u8[8] param) and legacy style (u8 param[8])
141
+ const isArray =
142
+ param.arrayDimension().length > 0 || param.type().arrayType() !== null;
143
+ const isConst = param.constModifier() !== null;
144
+ const typeCtx = param.type();
145
+
146
+ // Resolve type information
147
+ const typeInfo = FunctionContextManager.resolveParameterTypeInfo(
148
+ typeCtx,
149
+ callbacks,
150
+ );
151
+
152
+ // Register in currentParameters
153
+ const paramInfo = {
154
+ name,
155
+ baseType: typeInfo.typeName,
156
+ isArray,
157
+ isStruct: typeInfo.isStruct,
158
+ isConst,
159
+ isCallback: typeInfo.isCallback,
160
+ isString: typeInfo.isString,
161
+ };
162
+ CodeGenState.currentParameters.set(name, paramInfo);
163
+
164
+ // Register in typeRegistry
165
+ FunctionContextManager.registerParameterType(
166
+ name,
167
+ typeInfo,
168
+ param,
169
+ isArray,
170
+ isConst,
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Resolve type name and flags from a type context.
176
+ */
177
+ static resolveParameterTypeInfo(
178
+ typeCtx: Parser.TypeContext,
179
+ callbacks: IFunctionContextCallbacks,
180
+ ): IParameterTypeInfo {
181
+ if (typeCtx.primitiveType()) {
182
+ return {
183
+ typeName: typeCtx.primitiveType()!.getText(),
184
+ isStruct: false,
185
+ isCallback: false,
186
+ isString: false,
187
+ };
188
+ }
189
+
190
+ if (typeCtx.userType()) {
191
+ const typeName = typeCtx.userType()!.getText();
192
+ return {
193
+ typeName,
194
+ isStruct: callbacks.isStructType(typeName),
195
+ isCallback: CodeGenState.callbackTypes.has(typeName),
196
+ isString: false,
197
+ };
198
+ }
199
+
200
+ if (typeCtx.qualifiedType()) {
201
+ const identifierNames = typeCtx
202
+ .qualifiedType()!
203
+ .IDENTIFIER()
204
+ .map((id) => id.getText());
205
+ const typeName = callbacks.resolveQualifiedType(identifierNames);
206
+ return {
207
+ typeName,
208
+ isStruct: callbacks.isStructType(typeName),
209
+ isCallback: false,
210
+ isString: false,
211
+ };
212
+ }
213
+
214
+ if (typeCtx.scopedType()) {
215
+ const localTypeName = typeCtx.scopedType()!.IDENTIFIER().getText();
216
+ const typeName = CodeGenState.currentScope
217
+ ? `${CodeGenState.currentScope}_${localTypeName}`
218
+ : localTypeName;
219
+ return {
220
+ typeName,
221
+ isStruct: callbacks.isStructType(typeName),
222
+ isCallback: false,
223
+ isString: false,
224
+ };
225
+ }
226
+
227
+ if (typeCtx.globalType()) {
228
+ const typeName = typeCtx.globalType()!.IDENTIFIER().getText();
229
+ return {
230
+ typeName,
231
+ isStruct: callbacks.isStructType(typeName),
232
+ isCallback: false,
233
+ isString: false,
234
+ };
235
+ }
236
+
237
+ if (typeCtx.stringType()) {
238
+ return {
239
+ typeName: "string",
240
+ isStruct: false,
241
+ isCallback: false,
242
+ isString: true,
243
+ };
244
+ }
245
+
246
+ // Handle C-Next style array type (u8[8] param) - extract base type
247
+ if (typeCtx.arrayType()) {
248
+ const arrayTypeCtx = typeCtx.arrayType()!;
249
+ if (arrayTypeCtx.primitiveType()) {
250
+ return {
251
+ typeName: arrayTypeCtx.primitiveType()!.getText(),
252
+ isStruct: false,
253
+ isCallback: false,
254
+ isString: false,
255
+ };
256
+ }
257
+ if (arrayTypeCtx.userType()) {
258
+ const typeName = arrayTypeCtx.userType()!.getText();
259
+ return {
260
+ typeName,
261
+ isStruct: callbacks.isStructType(typeName),
262
+ isCallback: CodeGenState.callbackTypes.has(typeName),
263
+ isString: false,
264
+ };
265
+ }
266
+ // Handle string array type (string<32>[5] param)
267
+ if (arrayTypeCtx.stringType()) {
268
+ const stringCtx = arrayTypeCtx.stringType()!;
269
+ return {
270
+ typeName: stringCtx.getText(), // "string<32>"
271
+ isStruct: false,
272
+ isCallback: false,
273
+ isString: true,
274
+ };
275
+ }
276
+ }
277
+
278
+ // Fallback
279
+ return {
280
+ typeName: typeCtx.getText(),
281
+ isStruct: false,
282
+ isCallback: false,
283
+ isString: false,
284
+ };
285
+ }
286
+
287
+ /**
288
+ * Register a parameter in the type registry.
289
+ */
290
+ static registerParameterType(
291
+ name: string,
292
+ typeInfo: IParameterTypeInfo,
293
+ param: Parser.ParameterContext,
294
+ isArray: boolean,
295
+ isConst: boolean,
296
+ ): void {
297
+ const { typeName, isString } = typeInfo;
298
+ const typeCtx = param.type();
299
+
300
+ const isEnum = CodeGenState.symbols!.knownEnums.has(typeName);
301
+ const isBitmap = CodeGenState.symbols!.knownBitmaps.has(typeName);
302
+
303
+ // Extract array dimensions
304
+ const arrayDimensions = FunctionContextManager.extractParamArrayDimensions(
305
+ param,
306
+ typeCtx,
307
+ isArray,
308
+ );
309
+
310
+ // Add string capacity dimension if applicable
311
+ const stringCapacity = FunctionContextManager.getStringCapacity(
312
+ typeCtx,
313
+ isString,
314
+ );
315
+ if (isArray && stringCapacity !== undefined) {
316
+ arrayDimensions.push(stringCapacity + 1);
317
+ }
318
+
319
+ const registeredType = {
320
+ baseType: typeName,
321
+ bitWidth: isBitmap
322
+ ? CodeGenState.symbols!.bitmapBitWidth.get(typeName) || 0
323
+ : TYPE_WIDTH[typeName] || 0,
324
+ isArray,
325
+ arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
326
+ isConst,
327
+ isEnum,
328
+ enumTypeName: isEnum ? typeName : undefined,
329
+ isBitmap,
330
+ bitmapTypeName: isBitmap ? typeName : undefined,
331
+ isString,
332
+ stringCapacity,
333
+ isParameter: true,
334
+ };
335
+ CodeGenState.setVariableTypeInfo(name, registeredType);
336
+ }
337
+
338
+ /**
339
+ * Extract array dimensions from parameter (C-style or C-Next style).
340
+ */
341
+ static extractParamArrayDimensions(
342
+ param: Parser.ParameterContext,
343
+ typeCtx: Parser.TypeContext,
344
+ isArray: boolean,
345
+ ): number[] {
346
+ if (!isArray) return [];
347
+
348
+ // Try C-style first (param.arrayDimension())
349
+ if (param.arrayDimension().length > 0) {
350
+ return ArrayDimensionParser.parseForParameters(param.arrayDimension());
351
+ }
352
+
353
+ // C-Next style: get dimensions from arrayType
354
+ const arrayTypeCtx = typeCtx.arrayType();
355
+ if (!arrayTypeCtx) return [];
356
+
357
+ const dimensions: number[] = [];
358
+ for (const dim of arrayTypeCtx.arrayTypeDimension()) {
359
+ const expr = dim.expression();
360
+ if (!expr) continue;
361
+ const size = Number.parseInt(expr.getText(), 10);
362
+ if (!Number.isNaN(size)) {
363
+ dimensions.push(size);
364
+ }
365
+ }
366
+ return dimensions;
367
+ }
368
+
369
+ /**
370
+ * Extract string capacity from a string type context.
371
+ */
372
+ static getStringCapacity(
373
+ typeCtx: Parser.TypeContext,
374
+ isString: boolean,
375
+ ): number | undefined {
376
+ if (!isString) return undefined;
377
+
378
+ // Check direct stringType (e.g., string<32> param)
379
+ if (typeCtx.stringType()) {
380
+ const intLiteral = typeCtx.stringType()!.INTEGER_LITERAL();
381
+ if (intLiteral) {
382
+ return Number.parseInt(intLiteral.getText(), 10);
383
+ }
384
+ }
385
+
386
+ // Check arrayType with stringType (e.g., string<32>[5] param)
387
+ if (typeCtx.arrayType()?.stringType()) {
388
+ const intLiteral = typeCtx.arrayType()!.stringType()!.INTEGER_LITERAL();
389
+ if (intLiteral) {
390
+ return Number.parseInt(intLiteral.getText(), 10);
391
+ }
392
+ }
393
+
394
+ return undefined;
395
+ }
396
+
397
+ /**
398
+ * Clear parameter tracking when leaving a function.
399
+ */
400
+ static clearParameters(): void {
401
+ // ADR-025: Remove parameter types from typeRegistry
402
+ for (const name of CodeGenState.currentParameters.keys()) {
403
+ CodeGenState.deleteVariableTypeInfo(name);
404
+ }
405
+ CodeGenState.currentParameters.clear();
406
+ CodeGenState.localArrays.clear();
407
+ }
408
+
409
+ /**
410
+ * Enter function body - clears local variables and sets inFunctionBody flag.
411
+ * This is a simpler version used when only body lifecycle is needed.
412
+ */
413
+ static enterFunctionBody(): void {
414
+ CodeGenState.localVariables.clear();
415
+ CodeGenState.floatBitShadows.clear();
416
+ CodeGenState.floatShadowCurrent.clear();
417
+ CodeGenState.inFunctionBody = true;
418
+ CodeGenState.enterFunctionBody();
419
+ }
420
+
421
+ /**
422
+ * Exit function body - clears local variables and inFunctionBody flag.
423
+ * This is a simpler version used when only body lifecycle is needed.
424
+ */
425
+ static exitFunctionBody(): void {
426
+ CodeGenState.inFunctionBody = false;
427
+ CodeGenState.localVariables.clear();
428
+ CodeGenState.floatBitShadows.clear();
429
+ CodeGenState.floatShadowCurrent.clear();
430
+ CodeGenState.mainArgsName = null;
431
+ CodeGenState.exitFunctionBody();
432
+ }
433
+ }
434
+
435
+ export default FunctionContextManager;
@@ -325,7 +325,7 @@ class StringDeclHelper {
325
325
  const arraySize = CodeGenState.lastArrayInitCount;
326
326
 
327
327
  // Update type registry with inferred size
328
- CodeGenState.typeRegistry.set(name, {
328
+ CodeGenState.setVariableTypeInfo(name, {
329
329
  baseType: "char",
330
330
  bitWidth: 8,
331
331
  isArray: true,
@@ -531,7 +531,7 @@ class StringDeclHelper {
531
531
  callbacks.requireStringInclude();
532
532
 
533
533
  // Register in type registry with inferred capacity
534
- CodeGenState.typeRegistry.set(name, {
534
+ CodeGenState.setVariableTypeInfo(name, {
535
535
  baseType: "char",
536
536
  bitWidth: 8,
537
537
  isArray: true,
@@ -0,0 +1,203 @@
1
+ /**
2
+ * StringOperationsHelper - String operation detection and extraction
3
+ *
4
+ * Extracted from CodeGenerator to reduce file size.
5
+ * Handles detection of string concatenation and substring patterns.
6
+ *
7
+ * ADR-045: String type support
8
+ * Issue #707: Uses ExpressionUnwrapper for tree navigation
9
+ */
10
+
11
+ import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
12
+ import CodeGenState from "../../../state/CodeGenState.js";
13
+ import StringUtils from "../../../../utils/StringUtils.js";
14
+ import ExpressionUnwrapper from "../utils/ExpressionUnwrapper.js";
15
+
16
+ /** Regex for identifying valid C/C++ identifiers */
17
+ const IDENTIFIER_REGEX = /^[a-zA-Z_]\w*$/;
18
+
19
+ /**
20
+ * String concatenation operands extracted from expression.
21
+ */
22
+ interface IStringConcatOps {
23
+ left: string;
24
+ right: string;
25
+ leftCapacity: number;
26
+ rightCapacity: number;
27
+ }
28
+
29
+ /**
30
+ * Substring extraction operands extracted from expression.
31
+ */
32
+ interface ISubstringOps {
33
+ source: string;
34
+ start: string;
35
+ length: string;
36
+ sourceCapacity: number;
37
+ }
38
+
39
+ /**
40
+ * Callbacks for substring operand extraction.
41
+ */
42
+ interface ISubstringCallbacks {
43
+ /** Generate expression code */
44
+ generateExpression: (ctx: Parser.ExpressionContext) => string;
45
+ }
46
+
47
+ /**
48
+ * Helper for string operation detection and extraction.
49
+ * All methods are static - uses CodeGenState for shared state.
50
+ */
51
+ class StringOperationsHelper {
52
+ // ========================================================================
53
+ // Tier 1: Pure Utilities (no callbacks needed)
54
+ // ========================================================================
55
+
56
+ /**
57
+ * Get the capacity of a string expression.
58
+ * For string literals, capacity equals content length.
59
+ * For string variables, capacity is from the type registry.
60
+ *
61
+ * ADR-045: String capacity resolution for concatenation and bounds checking.
62
+ *
63
+ * @param exprCode - Expression code text (e.g., "hello" or varName)
64
+ * @returns Capacity in characters, or null if not a string
65
+ */
66
+ static getStringExprCapacity(exprCode: string): number | null {
67
+ // String literal - capacity equals content length
68
+ if (exprCode.startsWith('"') && exprCode.endsWith('"')) {
69
+ return StringUtils.literalLength(exprCode);
70
+ }
71
+
72
+ // Variable - check type registry
73
+ if (IDENTIFIER_REGEX.test(exprCode)) {
74
+ const typeInfo = CodeGenState.getVariableTypeInfo(exprCode);
75
+ if (typeInfo?.isString && typeInfo.stringCapacity !== undefined) {
76
+ return typeInfo.stringCapacity;
77
+ }
78
+ }
79
+
80
+ return null;
81
+ }
82
+
83
+ /**
84
+ * Check if an expression is a string concatenation (contains + with string operands).
85
+ * Returns the operand expressions and capacities if it is, null otherwise.
86
+ *
87
+ * ADR-045: String concatenation detection for strncpy/strncat generation.
88
+ * Issue #707: Uses ExpressionUnwrapper for tree navigation.
89
+ *
90
+ * @param ctx - Expression context to check
91
+ * @returns Concatenation operands or null if not a string concat
92
+ */
93
+ static getStringConcatOperands(
94
+ ctx: Parser.ExpressionContext,
95
+ ): IStringConcatOps | null {
96
+ // Navigate to the additive expression level using ExpressionUnwrapper
97
+ const add = ExpressionUnwrapper.getAdditiveExpression(ctx);
98
+ if (!add) return null;
99
+ const multExprs = add.multiplicativeExpression();
100
+
101
+ // Need exactly 2 operands for simple concatenation
102
+ if (multExprs.length !== 2) return null;
103
+
104
+ // Check if this is addition (not subtraction)
105
+ // Use MINUS() token check instead of text.includes("-") to avoid
106
+ // false positives from identifiers/literals containing hyphens
107
+ if (add.MINUS().length > 0) return null;
108
+
109
+ // Get the operand texts
110
+ const leftText = multExprs[0].getText();
111
+ const rightText = multExprs[1].getText();
112
+
113
+ // Check if at least one operand is a string
114
+ const leftCapacity = StringOperationsHelper.getStringExprCapacity(leftText);
115
+ const rightCapacity =
116
+ StringOperationsHelper.getStringExprCapacity(rightText);
117
+
118
+ if (leftCapacity === null && rightCapacity === null) {
119
+ return null; // Neither is a string
120
+ }
121
+
122
+ // If one is null, it's not a valid string concatenation
123
+ if (leftCapacity === null || rightCapacity === null) {
124
+ return null;
125
+ }
126
+
127
+ return {
128
+ left: leftText,
129
+ right: rightText,
130
+ leftCapacity,
131
+ rightCapacity,
132
+ };
133
+ }
134
+
135
+ // ========================================================================
136
+ // Tier 2: Operations with Callbacks
137
+ // ========================================================================
138
+
139
+ /**
140
+ * Check if an expression is a substring extraction (string[start, length]).
141
+ * Returns the source string, start, length, and source capacity if it is.
142
+ *
143
+ * ADR-045: Substring extraction detection for safe string slicing.
144
+ * Issue #707: Uses ExpressionUnwrapper for tree navigation.
145
+ * Issue #140: Handles both [start, length] and single-char [index] patterns.
146
+ *
147
+ * @param ctx - Expression context to check
148
+ * @param callbacks - Callbacks for expression generation
149
+ * @returns Substring operands or null if not a substring extraction
150
+ */
151
+ static getSubstringOperands(
152
+ ctx: Parser.ExpressionContext,
153
+ callbacks: ISubstringCallbacks,
154
+ ): ISubstringOps | null {
155
+ // Navigate to the postfix expression level using shared utility
156
+ const postfix = ExpressionUnwrapper.getPostfixExpression(ctx);
157
+ if (!postfix) return null;
158
+
159
+ const primary = postfix.primaryExpression();
160
+ const ops = postfix.postfixOp();
161
+
162
+ // Need exactly one postfix operation (the [start, length])
163
+ if (ops.length !== 1) return null;
164
+
165
+ const op = ops[0];
166
+ const exprs = op.expression();
167
+
168
+ // Get the source variable name first
169
+ const sourceId = primary.IDENTIFIER();
170
+ if (!sourceId) return null;
171
+
172
+ const sourceName = sourceId.getText();
173
+
174
+ // Check if source is a string type
175
+ const typeInfo = CodeGenState.getVariableTypeInfo(sourceName);
176
+ if (!typeInfo?.isString || typeInfo.stringCapacity === undefined) {
177
+ return null;
178
+ }
179
+
180
+ // Issue #140: Handle both [start, length] pattern (2 expressions)
181
+ // and single-character access [index] pattern (1 expression, treated as [index, 1])
182
+ if (exprs.length === 2) {
183
+ return {
184
+ source: sourceName,
185
+ start: callbacks.generateExpression(exprs[0]),
186
+ length: callbacks.generateExpression(exprs[1]),
187
+ sourceCapacity: typeInfo.stringCapacity,
188
+ };
189
+ } else if (exprs.length === 1) {
190
+ // Single-character access: source[i] is sugar for source[i, 1]
191
+ return {
192
+ source: sourceName,
193
+ start: callbacks.generateExpression(exprs[0]),
194
+ length: "1",
195
+ sourceCapacity: typeInfo.stringCapacity,
196
+ };
197
+ }
198
+
199
+ return null;
200
+ }
201
+ }
202
+
203
+ export default StringOperationsHelper;