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
@@ -11,8 +11,6 @@ import { ParseTreeWalker } from "antlr4ng";
11
11
  import { CNextListener } from "../parser/grammar/CNextListener";
12
12
  import * as Parser from "../parser/grammar/CNextParser";
13
13
  import SymbolTable from "../symbols/SymbolTable";
14
- import ESourceLanguage from "../../../utils/types/ESourceLanguage";
15
- import ESymbolKind from "../../../utils/types/ESymbolKind";
16
14
  import IFunctionCallError from "./types/IFunctionCallError";
17
15
  import ParserUtils from "../../../utils/ParserUtils";
18
16
 
@@ -26,195 +24,178 @@ const CNEXT_BUILTINS: Set<string> = new Set([
26
24
  ]);
27
25
 
28
26
  /**
29
- * Standard library functions from common C headers
30
- * These are considered "external" and don't need to be defined in C-Next
27
+ * Standard library functions mapped to their header files.
28
+ * Flat structure: function name header file.
29
+ * These are considered "external" and don't need to be defined in C-Next.
31
30
  */
32
- const STDLIB_FUNCTIONS: Map<string, Set<string>> = new Map([
33
- [
34
- "stdio.h",
35
- new Set([
36
- "printf",
37
- "fprintf",
38
- "sprintf",
39
- "snprintf",
40
- "scanf",
41
- "fscanf",
42
- "sscanf",
43
- "fopen",
44
- "fclose",
45
- "fread",
46
- "fwrite",
47
- "fgets",
48
- "fputs",
49
- "fgetc",
50
- "fputc",
51
- "puts",
52
- "putchar",
53
- "getchar",
54
- "gets",
55
- "perror",
56
- "fflush",
57
- "fseek",
58
- "ftell",
59
- "rewind",
60
- "feof",
61
- "ferror",
62
- "clearerr",
63
- "remove",
64
- "rename",
65
- "tmpfile",
66
- "tmpnam",
67
- "setbuf",
68
- "setvbuf",
69
- ]),
70
- ],
71
- [
72
- "stdlib.h",
73
- new Set([
74
- "malloc",
75
- "calloc",
76
- "realloc",
77
- "free",
78
- "atoi",
79
- "atof",
80
- "atol",
81
- "atoll",
82
- "strtol",
83
- "strtoul",
84
- "strtoll",
85
- "strtoull",
86
- "strtof",
87
- "strtod",
88
- "strtold",
89
- "rand",
90
- "srand",
91
- "exit",
92
- "abort",
93
- "atexit",
94
- "system",
95
- "getenv",
96
- "abs",
97
- "labs",
98
- "llabs",
99
- "div",
100
- "ldiv",
101
- "lldiv",
102
- "qsort",
103
- "bsearch",
104
- ]),
105
- ],
106
- [
107
- "string.h",
108
- new Set([
109
- "strlen",
110
- "strcpy",
111
- "strncpy",
112
- "strcat",
113
- "strncat",
114
- "strcmp",
115
- "strncmp",
116
- "strchr",
117
- "strrchr",
118
- "strstr",
119
- "strtok",
120
- "memcpy",
121
- "memmove",
122
- "memset",
123
- "memcmp",
124
- "memchr",
125
- ]),
126
- ],
127
- [
128
- "math.h",
129
- new Set([
130
- "sin",
131
- "cos",
132
- "tan",
133
- "asin",
134
- "acos",
135
- "atan",
136
- "atan2",
137
- "sinh",
138
- "cosh",
139
- "tanh",
140
- "exp",
141
- "log",
142
- "log10",
143
- "log2",
144
- "pow",
145
- "sqrt",
146
- "cbrt",
147
- "ceil",
148
- "floor",
149
- "round",
150
- "trunc",
151
- "fabs",
152
- "fmod",
153
- "remainder",
154
- "fmax",
155
- "fmin",
156
- "hypot",
157
- "ldexp",
158
- "frexp",
159
- "modf",
160
- ]),
161
- ],
162
- [
163
- "ctype.h",
164
- new Set([
165
- "isalnum",
166
- "isalpha",
167
- "isdigit",
168
- "isxdigit",
169
- "islower",
170
- "isupper",
171
- "isspace",
172
- "ispunct",
173
- "isprint",
174
- "isgraph",
175
- "iscntrl",
176
- "tolower",
177
- "toupper",
178
- ]),
179
- ],
180
- [
181
- "time.h",
182
- new Set([
183
- "time",
184
- "clock",
185
- "difftime",
186
- "mktime",
187
- "strftime",
188
- "localtime",
189
- "gmtime",
190
- "asctime",
191
- "ctime",
192
- ]),
193
- ],
194
- ["assert.h", new Set(["assert"])],
31
+ const STDLIB_FUNCTION_HEADERS: Record<string, string> = {
32
+ // stdio.h
33
+ printf: "stdio.h",
34
+ fprintf: "stdio.h",
35
+ sprintf: "stdio.h",
36
+ snprintf: "stdio.h",
37
+ scanf: "stdio.h",
38
+ fscanf: "stdio.h",
39
+ sscanf: "stdio.h",
40
+ fopen: "stdio.h",
41
+ fclose: "stdio.h",
42
+ fread: "stdio.h",
43
+ fwrite: "stdio.h",
44
+ fgets: "stdio.h",
45
+ fputs: "stdio.h",
46
+ fgetc: "stdio.h",
47
+ fputc: "stdio.h",
48
+ puts: "stdio.h",
49
+ putchar: "stdio.h",
50
+ getchar: "stdio.h",
51
+ gets: "stdio.h",
52
+ perror: "stdio.h",
53
+ fflush: "stdio.h",
54
+ fseek: "stdio.h",
55
+ ftell: "stdio.h",
56
+ rewind: "stdio.h",
57
+ feof: "stdio.h",
58
+ ferror: "stdio.h",
59
+ clearerr: "stdio.h",
60
+ remove: "stdio.h",
61
+ rename: "stdio.h",
62
+ tmpfile: "stdio.h",
63
+ tmpnam: "stdio.h",
64
+ setbuf: "stdio.h",
65
+ setvbuf: "stdio.h",
66
+ // stdlib.h
67
+ malloc: "stdlib.h",
68
+ calloc: "stdlib.h",
69
+ realloc: "stdlib.h",
70
+ free: "stdlib.h",
71
+ atoi: "stdlib.h",
72
+ atof: "stdlib.h",
73
+ atol: "stdlib.h",
74
+ atoll: "stdlib.h",
75
+ strtol: "stdlib.h",
76
+ strtoul: "stdlib.h",
77
+ strtoll: "stdlib.h",
78
+ strtoull: "stdlib.h",
79
+ strtof: "stdlib.h",
80
+ strtod: "stdlib.h",
81
+ strtold: "stdlib.h",
82
+ rand: "stdlib.h",
83
+ srand: "stdlib.h",
84
+ exit: "stdlib.h",
85
+ abort: "stdlib.h",
86
+ atexit: "stdlib.h",
87
+ system: "stdlib.h",
88
+ getenv: "stdlib.h",
89
+ abs: "stdlib.h",
90
+ labs: "stdlib.h",
91
+ llabs: "stdlib.h",
92
+ div: "stdlib.h",
93
+ ldiv: "stdlib.h",
94
+ lldiv: "stdlib.h",
95
+ qsort: "stdlib.h",
96
+ bsearch: "stdlib.h",
97
+ // string.h
98
+ strlen: "string.h",
99
+ strcpy: "string.h",
100
+ strncpy: "string.h",
101
+ strcat: "string.h",
102
+ strncat: "string.h",
103
+ strcmp: "string.h",
104
+ strncmp: "string.h",
105
+ strchr: "string.h",
106
+ strrchr: "string.h",
107
+ strstr: "string.h",
108
+ strtok: "string.h",
109
+ memcpy: "string.h",
110
+ memmove: "string.h",
111
+ memset: "string.h",
112
+ memcmp: "string.h",
113
+ memchr: "string.h",
114
+ // math.h
115
+ sin: "math.h",
116
+ cos: "math.h",
117
+ tan: "math.h",
118
+ asin: "math.h",
119
+ acos: "math.h",
120
+ atan: "math.h",
121
+ atan2: "math.h",
122
+ sinh: "math.h",
123
+ cosh: "math.h",
124
+ tanh: "math.h",
125
+ exp: "math.h",
126
+ log: "math.h",
127
+ log10: "math.h",
128
+ log2: "math.h",
129
+ pow: "math.h",
130
+ sqrt: "math.h",
131
+ cbrt: "math.h",
132
+ ceil: "math.h",
133
+ floor: "math.h",
134
+ round: "math.h",
135
+ trunc: "math.h",
136
+ fabs: "math.h",
137
+ fmod: "math.h",
138
+ remainder: "math.h",
139
+ fmax: "math.h",
140
+ fmin: "math.h",
141
+ hypot: "math.h",
142
+ ldexp: "math.h",
143
+ frexp: "math.h",
144
+ modf: "math.h",
145
+ // C99 classification macros (also functions in C++)
146
+ isnan: "math.h",
147
+ isinf: "math.h",
148
+ isfinite: "math.h",
149
+ isnormal: "math.h",
150
+ signbit: "math.h",
151
+ fpclassify: "math.h",
152
+ nan: "math.h",
153
+ nanf: "math.h",
154
+ nanl: "math.h",
155
+ // ctype.h
156
+ isalnum: "ctype.h",
157
+ isalpha: "ctype.h",
158
+ isdigit: "ctype.h",
159
+ isxdigit: "ctype.h",
160
+ islower: "ctype.h",
161
+ isupper: "ctype.h",
162
+ isspace: "ctype.h",
163
+ ispunct: "ctype.h",
164
+ isprint: "ctype.h",
165
+ isgraph: "ctype.h",
166
+ iscntrl: "ctype.h",
167
+ tolower: "ctype.h",
168
+ toupper: "ctype.h",
169
+ // time.h
170
+ time: "time.h",
171
+ clock: "time.h",
172
+ difftime: "time.h",
173
+ mktime: "time.h",
174
+ strftime: "time.h",
175
+ localtime: "time.h",
176
+ gmtime: "time.h",
177
+ asctime: "time.h",
178
+ ctime: "time.h",
179
+ // assert.h
180
+ assert: "assert.h",
195
181
  // Arduino framework
196
- [
197
- "Arduino.h",
198
- new Set([
199
- "pinMode",
200
- "digitalWrite",
201
- "digitalRead",
202
- "analogRead",
203
- "analogWrite",
204
- "delay",
205
- "delayMicroseconds",
206
- "millis",
207
- "micros",
208
- "attachInterrupt",
209
- "detachInterrupt",
210
- "noInterrupts",
211
- "interrupts",
212
- "Serial",
213
- "Wire",
214
- "SPI",
215
- ]),
216
- ],
217
- ]);
182
+ pinMode: "Arduino.h",
183
+ digitalWrite: "Arduino.h",
184
+ digitalRead: "Arduino.h",
185
+ analogRead: "Arduino.h",
186
+ analogWrite: "Arduino.h",
187
+ delay: "Arduino.h",
188
+ delayMicroseconds: "Arduino.h",
189
+ millis: "Arduino.h",
190
+ micros: "Arduino.h",
191
+ attachInterrupt: "Arduino.h",
192
+ detachInterrupt: "Arduino.h",
193
+ noInterrupts: "Arduino.h",
194
+ interrupts: "Arduino.h",
195
+ Serial: "Arduino.h",
196
+ Wire: "Arduino.h",
197
+ SPI: "Arduino.h",
198
+ };
218
199
 
219
200
  /**
220
201
  * Listener that walks the parse tree and checks function calls
@@ -424,6 +405,9 @@ class FunctionCallAnalyzer {
424
405
  /** Functions that have been defined (in order of appearance) */
425
406
  private definedFunctions: Set<string> = new Set();
426
407
 
408
+ /** All functions that will be defined in this file (for distinguishing local vs cross-file) */
409
+ private allLocalFunctions: Set<string> = new Set();
410
+
427
411
  /** Known scopes (for Scope.member -> Scope_member resolution) */
428
412
  private knownScopes: Set<string> = new Set();
429
413
 
@@ -454,6 +438,7 @@ class FunctionCallAnalyzer {
454
438
  ): IFunctionCallError[] {
455
439
  this.errors = [];
456
440
  this.definedFunctions = new Set();
441
+ this.allLocalFunctions = new Set();
457
442
  this.knownScopes = new Set();
458
443
  this.includedHeaders = new Set();
459
444
  this.symbolTable = symbolTable ?? null;
@@ -461,10 +446,11 @@ class FunctionCallAnalyzer {
461
446
  this.callableVariables = new Set();
462
447
  this.callbackTypes = new Set();
463
448
 
464
- // First pass: collect scope names, includes, and callback types
449
+ // First pass: collect scope names, includes, callback types, and all local functions
465
450
  this.collectScopes(tree);
466
451
  this.collectIncludes(tree);
467
452
  this.collectCallbackTypes(tree);
453
+ this.collectAllLocalFunctions(tree);
468
454
 
469
455
  // Second pass: walk tree in order, tracking definitions and checking calls
470
456
  const listener = new FunctionCallListener(this);
@@ -503,13 +489,17 @@ class FunctionCallAnalyzer {
503
489
  * Check if a function is from an included standard library header
504
490
  */
505
491
  private isStdlibFunction(name: string): boolean {
506
- for (const header of this.includedHeaders) {
507
- const funcs = STDLIB_FUNCTIONS.get(header);
508
- if (funcs?.has(name)) {
509
- return true;
510
- }
511
- }
512
- return false;
492
+ const header = STDLIB_FUNCTION_HEADERS[name];
493
+ return header !== undefined && this.includedHeaders.has(header);
494
+ }
495
+
496
+ /**
497
+ * Issue #787: Find which header a stdlib function belongs to (if any).
498
+ * Returns the header name if found, or null if not a known stdlib function.
499
+ * Checks ALL stdlib functions, not just from included headers.
500
+ */
501
+ private findStdlibHeader(name: string): string | null {
502
+ return STDLIB_FUNCTION_HEADERS[name] ?? null;
513
503
  }
514
504
 
515
505
  /**
@@ -525,6 +515,35 @@ class FunctionCallAnalyzer {
525
515
  }
526
516
  }
527
517
 
518
+ /**
519
+ * Issue #786: Pre-collect all function names defined in this file.
520
+ * Used to distinguish between local functions (subject to define-before-use)
521
+ * and cross-file functions from includes (allowed without local definition).
522
+ */
523
+ private collectAllLocalFunctions(tree: Parser.ProgramContext): void {
524
+ for (const decl of tree.declaration()) {
525
+ // Standalone functions
526
+ if (decl.functionDeclaration()) {
527
+ const name = decl.functionDeclaration()!.IDENTIFIER().getText();
528
+ this.allLocalFunctions.add(name);
529
+ }
530
+ // Scope member functions
531
+ if (decl.scopeDeclaration()) {
532
+ const scopeDecl = decl.scopeDeclaration()!;
533
+ const scopeName = scopeDecl.IDENTIFIER().getText();
534
+ for (const member of scopeDecl.scopeMember()) {
535
+ if (member.functionDeclaration()) {
536
+ const funcName = member
537
+ .functionDeclaration()!
538
+ .IDENTIFIER()
539
+ .getText();
540
+ this.allLocalFunctions.add(`${scopeName}_${funcName}`);
541
+ }
542
+ }
543
+ }
544
+ }
545
+ }
546
+
528
547
  /**
529
548
  * ADR-029: Check if a type name is a callback type (function-as-type)
530
549
  */
@@ -627,31 +646,47 @@ class FunctionCallAnalyzer {
627
646
  }
628
647
  }
629
648
 
630
- // Not defined - report error
649
+ // Not defined - report error with optional hint
650
+ const header = this.findStdlibHeader(name);
651
+ let message = `function '${name}' called before definition`;
652
+ if (header) {
653
+ message += `; hint: '${name}' is available from ${header} — try global.${name}()`;
654
+ }
655
+
631
656
  this.errors.push({
632
657
  code: "E0422",
633
658
  functionName: name,
634
659
  line,
635
660
  column,
636
- message: `function '${name}' called before definition`,
661
+ message,
637
662
  });
638
663
  }
639
664
 
640
665
  /**
641
- * Check if a function is defined externally (C/C++ interop)
666
+ * Check if a function is defined externally (from included files)
667
+ * This includes C/C++ headers AND C-Next includes.
668
+ *
669
+ * Issue #786: Only considers a function "external" if it's NOT defined
670
+ * in the current file. Functions defined locally are subject to
671
+ * define-before-use checking, even if they exist in the SymbolTable.
642
672
  */
643
673
  private isExternalFunction(name: string): boolean {
674
+ // If the function is defined in this file, it's not external
675
+ // (even if it's also in the SymbolTable from symbol collection)
676
+ if (this.allLocalFunctions.has(name)) {
677
+ return false;
678
+ }
679
+
644
680
  if (!this.symbolTable) {
645
681
  return false;
646
682
  }
647
683
 
648
684
  const symbols = this.symbolTable.getOverloads(name);
649
685
  for (const sym of symbols) {
650
- if (
651
- (sym.sourceLanguage === ESourceLanguage.C ||
652
- sym.sourceLanguage === ESourceLanguage.Cpp) &&
653
- sym.kind === ESymbolKind.Function
654
- ) {
686
+ // Accept functions from any source language:
687
+ // - C/C++ functions from header includes
688
+ // - C-Next functions from .cnx file includes
689
+ if (sym.kind === "function") {
655
690
  return true;
656
691
  }
657
692
  }
@@ -23,7 +23,6 @@ import analyzePostfixOps from "../../../utils/PostfixAnalysisUtils";
23
23
  import SymbolTable from "../symbols/SymbolTable";
24
24
  import CodeGenState from "../../state/CodeGenState";
25
25
  import ESourceLanguage from "../../../utils/types/ESourceLanguage";
26
- import ESymbolKind from "../../../utils/types/ESymbolKind";
27
26
 
28
27
  /**
29
28
  * Tracks the initialization state of a variable
@@ -459,7 +458,7 @@ class InitializationAnalyzer {
459
458
  for (const sym of symbols) {
460
459
  if (sym.sourceLanguage === ESourceLanguage.Cpp) {
461
460
  // C++ classes and structs have default constructors
462
- if (sym.kind === ESymbolKind.Struct || sym.kind === ESymbolKind.Class) {
461
+ if (sym.kind === "struct" || sym.kind === "class") {
463
462
  return true;
464
463
  }
465
464
  }