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.
- package/package.json +1 -1
- package/src/lib/__tests__/parseCHeader.mocked.test.ts +69 -54
- package/src/lib/parseCHeader.ts +56 -23
- package/src/lib/parseWithSymbols.ts +195 -53
- package/src/transpiler/Transpiler.ts +173 -60
- package/src/transpiler/logic/analysis/FunctionCallAnalyzer.ts +240 -205
- package/src/transpiler/logic/analysis/InitializationAnalyzer.ts +1 -2
- package/src/transpiler/logic/analysis/PassByValueAnalyzer.ts +742 -0
- package/src/transpiler/logic/analysis/__tests__/FunctionCallAnalyzer.test.ts +102 -15
- package/src/transpiler/logic/analysis/__tests__/InitializationAnalyzer.test.ts +9 -9
- package/src/transpiler/logic/analysis/__tests__/runAnalyzers.test.ts +5 -5
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/AssignmentTargetExtractor.ts +1 -1
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/ChildStatementCollector.ts +1 -1
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/StatementExpressionCollector.ts +1 -1
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/AssignmentTargetExtractor.test.ts +2 -2
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/ChildStatementCollector.test.ts +2 -2
- package/src/transpiler/{output/codegen → logic/analysis}/helpers/__tests__/StatementExpressionCollector.test.ts +2 -2
- package/src/transpiler/logic/symbols/SymbolTable.ts +676 -258
- package/src/transpiler/logic/symbols/SymbolUtils.ts +2 -2
- package/src/transpiler/logic/symbols/__tests__/SymbolTable.test.ts +290 -782
- package/src/transpiler/logic/symbols/c/__tests__/CResolver.integration.test.ts +573 -0
- package/src/transpiler/logic/symbols/c/__tests__/testHelpers.ts +20 -0
- package/src/transpiler/logic/symbols/c/collectors/EnumCollector.ts +82 -0
- package/src/transpiler/logic/symbols/c/collectors/FunctionCollector.ts +106 -0
- package/src/transpiler/logic/symbols/c/collectors/StructCollector.ts +173 -0
- package/src/transpiler/logic/symbols/c/collectors/TypedefCollector.ts +35 -0
- package/src/transpiler/logic/symbols/c/collectors/VariableCollector.ts +80 -0
- package/src/transpiler/logic/symbols/c/index.ts +333 -0
- package/src/transpiler/logic/symbols/c/utils/DeclaratorUtils.ts +269 -0
- package/src/transpiler/logic/symbols/cnext/__tests__/BitmapCollector.test.ts +50 -11
- package/src/transpiler/logic/symbols/cnext/__tests__/CNextResolver.integration.test.ts +45 -34
- package/src/transpiler/logic/symbols/cnext/__tests__/EnumCollector.test.ts +30 -13
- package/src/transpiler/logic/symbols/cnext/__tests__/FunctionCollector.test.ts +279 -64
- package/src/transpiler/logic/symbols/cnext/__tests__/RegisterCollector.test.ts +60 -13
- package/src/transpiler/logic/symbols/cnext/__tests__/ScopeCollector.test.ts +40 -37
- package/src/transpiler/logic/symbols/cnext/__tests__/StructCollector.test.ts +131 -45
- package/src/transpiler/logic/symbols/cnext/__tests__/TSymbolInfoAdapter.test.ts +223 -139
- package/src/transpiler/logic/symbols/cnext/__tests__/VariableCollector.test.ts +79 -25
- package/src/transpiler/logic/symbols/cnext/__tests__/testUtils.ts +53 -0
- package/src/transpiler/logic/symbols/cnext/adapters/TSymbolInfoAdapter.ts +83 -43
- package/src/transpiler/logic/symbols/cnext/collectors/BitmapCollector.ts +14 -13
- package/src/transpiler/logic/symbols/cnext/collectors/EnumCollector.ts +11 -10
- package/src/transpiler/logic/symbols/cnext/collectors/FunctionCollector.ts +83 -34
- package/src/transpiler/logic/symbols/cnext/collectors/RegisterCollector.ts +22 -18
- package/src/transpiler/logic/symbols/cnext/collectors/ScopeCollector.ts +53 -35
- package/src/transpiler/logic/symbols/cnext/collectors/StructCollector.ts +30 -23
- package/src/transpiler/logic/symbols/cnext/collectors/VariableCollector.ts +18 -19
- package/src/transpiler/logic/symbols/cnext/index.ts +36 -14
- package/src/transpiler/logic/symbols/cnext/types/IScopeCollectorResult.ts +2 -2
- package/src/transpiler/logic/symbols/cnext/utils/SymbolNameUtils.ts +27 -0
- package/src/transpiler/logic/symbols/cpp/__tests__/CppResolver.integration.test.ts +270 -0
- package/src/transpiler/logic/symbols/cpp/__tests__/testHelpers.ts +20 -0
- package/src/transpiler/logic/symbols/cpp/collectors/ClassCollector.ts +317 -0
- package/src/transpiler/logic/symbols/cpp/collectors/EnumCollector.ts +71 -0
- package/src/transpiler/logic/symbols/cpp/collectors/FunctionCollector.ts +155 -0
- package/src/transpiler/logic/symbols/cpp/collectors/NamespaceCollector.ts +65 -0
- package/src/transpiler/logic/symbols/cpp/collectors/TypeAliasCollector.ts +46 -0
- package/src/transpiler/logic/symbols/cpp/collectors/VariableCollector.ts +54 -0
- package/src/transpiler/logic/symbols/cpp/index.ts +366 -0
- package/src/transpiler/logic/symbols/cpp/utils/DeclaratorUtils.ts +248 -0
- package/src/transpiler/logic/symbols/shared/IExtractedParameter.ts +18 -0
- package/src/transpiler/logic/symbols/shared/ParameterExtractorUtils.ts +73 -0
- package/src/transpiler/output/codegen/CodeGenerator.ts +310 -2288
- package/src/transpiler/output/codegen/TypeRegistrationUtils.ts +4 -6
- package/src/transpiler/output/codegen/TypeResolver.ts +2 -2
- package/src/transpiler/output/codegen/TypeValidator.ts +5 -5
- package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +7 -1
- package/src/transpiler/output/codegen/__tests__/TypeRegistrationUtils.test.ts +36 -51
- package/src/transpiler/output/codegen/__tests__/TypeResolver.test.ts +20 -17
- package/src/transpiler/output/codegen/__tests__/TypeValidator.resolution.test.ts +3 -3
- package/src/transpiler/output/codegen/__tests__/TypeValidator.test.ts +1 -1
- package/src/transpiler/output/codegen/analysis/MemberChainAnalyzer.ts +1 -1
- package/src/transpiler/output/codegen/analysis/StringLengthCounter.ts +1 -1
- package/src/transpiler/output/codegen/analysis/__tests__/MemberChainAnalyzer.test.ts +9 -9
- package/src/transpiler/output/codegen/analysis/__tests__/StringLengthCounter.test.ts +12 -12
- package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +13 -12
- package/src/transpiler/output/codegen/assignment/__tests__/AssignmentClassifier.test.ts +23 -17
- package/src/transpiler/output/codegen/assignment/handlers/ArrayHandlers.ts +2 -2
- package/src/transpiler/output/codegen/assignment/handlers/AssignmentHandlerUtils.ts +7 -1
- package/src/transpiler/output/codegen/assignment/handlers/BitAccessHandlers.ts +3 -3
- package/src/transpiler/output/codegen/assignment/handlers/BitmapHandlers.ts +9 -5
- package/src/transpiler/output/codegen/assignment/handlers/RegisterHandlers.ts +2 -1
- package/src/transpiler/output/codegen/assignment/handlers/SpecialHandlers.ts +4 -4
- package/src/transpiler/output/codegen/assignment/handlers/StringHandlers.ts +5 -5
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/ArrayHandlers.test.ts +23 -25
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitAccessHandlers.test.ts +20 -36
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitmapHandlers.test.ts +18 -18
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/SpecialHandlers.test.ts +42 -32
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/handlerTestUtils.ts +5 -4
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +21 -8
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopedRegisterGenerator.ts +3 -2
- package/src/transpiler/output/codegen/generators/expressions/CallExprGenerator.ts +14 -6
- package/src/transpiler/output/codegen/generators/expressions/CallExprUtils.ts +9 -3
- package/src/transpiler/output/codegen/generators/expressions/PostfixExpressionGenerator.ts +19 -16
- package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprGenerator.test.ts +24 -8
- package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprUtils.test.ts +4 -8
- package/src/transpiler/output/codegen/generators/expressions/__tests__/PostfixExpressionGenerator.test.ts +15 -2
- package/src/transpiler/output/codegen/helpers/ArgumentGenerator.ts +236 -0
- package/src/transpiler/output/codegen/helpers/ArrayInitHelper.ts +2 -1
- package/src/transpiler/output/codegen/helpers/AssignmentExpectedTypeResolver.ts +2 -2
- package/src/transpiler/output/codegen/helpers/AssignmentValidator.ts +3 -3
- package/src/transpiler/output/codegen/helpers/CppConstructorHelper.ts +3 -3
- package/src/transpiler/output/codegen/helpers/EnumAssignmentValidator.ts +1 -1
- package/src/transpiler/output/codegen/helpers/FunctionContextManager.ts +435 -0
- package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +2 -2
- package/src/transpiler/output/codegen/helpers/StringOperationsHelper.ts +203 -0
- package/src/transpiler/output/codegen/helpers/SymbolLookupHelper.ts +8 -12
- package/src/transpiler/output/codegen/helpers/TypeRegistrationEngine.ts +520 -0
- package/src/transpiler/output/codegen/helpers/VariableDeclHelper.ts +735 -0
- package/src/transpiler/output/codegen/helpers/VariableDeclarationFormatter.ts +1 -1
- package/src/transpiler/output/codegen/helpers/__tests__/ArgumentGenerator.test.ts +521 -0
- package/src/transpiler/output/codegen/helpers/__tests__/ArrayInitHelper.test.ts +1 -1
- package/src/transpiler/output/codegen/helpers/__tests__/AssignmentExpectedTypeResolver.test.ts +7 -7
- package/src/transpiler/output/codegen/helpers/__tests__/AssignmentValidator.test.ts +7 -7
- package/src/transpiler/output/codegen/helpers/__tests__/CppConstructorHelper.test.ts +4 -5
- package/src/transpiler/output/codegen/helpers/__tests__/EnumAssignmentValidator.test.ts +2 -2
- package/src/transpiler/output/codegen/helpers/__tests__/FunctionContextManager.test.ts +983 -0
- package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +4 -4
- package/src/transpiler/output/codegen/helpers/__tests__/StringOperationsHelper.test.ts +269 -0
- package/src/transpiler/output/codegen/helpers/__tests__/SymbolLookupHelper.test.ts +31 -32
- package/src/transpiler/output/codegen/helpers/__tests__/TypeRegistrationEngine.test.ts +186 -0
- package/src/transpiler/output/codegen/helpers/__tests__/VariableDeclHelper.test.ts +460 -0
- package/src/transpiler/output/codegen/helpers/types/IArgumentGeneratorCallbacks.ts +32 -0
- package/src/transpiler/output/codegen/resolution/EnumTypeResolver.ts +7 -3
- package/src/transpiler/output/codegen/resolution/__tests__/EnumTypeResolver.test.ts +5 -5
- package/src/transpiler/output/codegen/types/IFunctionContextCallbacks.ts +12 -0
- package/src/transpiler/output/codegen/types/IVariableFormatInput.ts +1 -1
- package/src/transpiler/output/codegen/utils/QualifiedNameGenerator.ts +114 -0
- package/src/transpiler/output/codegen/utils/__tests__/QualifiedNameGenerator.test.ts +183 -0
- package/src/transpiler/output/headers/BaseHeaderGenerator.ts +4 -4
- package/src/transpiler/output/headers/ExternalTypeHeaderBuilder.ts +7 -7
- package/src/transpiler/output/headers/HeaderGenerator.ts +9 -7
- package/src/transpiler/output/headers/HeaderGeneratorUtils.ts +19 -20
- package/src/transpiler/output/headers/__tests__/BaseHeaderGenerator.test.ts +15 -18
- package/src/transpiler/output/headers/__tests__/CHeaderGenerator.test.ts +63 -64
- package/src/transpiler/output/headers/__tests__/CppHeaderGenerator.test.ts +36 -32
- package/src/transpiler/output/headers/__tests__/ExternalTypeHeaderBuilder.test.ts +26 -26
- package/src/transpiler/output/headers/__tests__/HeaderGenerator.test.ts +87 -59
- package/src/transpiler/output/headers/__tests__/HeaderGeneratorUtils.test.ts +57 -58
- package/src/transpiler/output/headers/adapters/HeaderSymbolAdapter.ts +222 -0
- package/src/transpiler/output/headers/adapters/__tests__/HeaderSymbolAdapter.test.ts +538 -0
- package/src/transpiler/output/headers/types/IGroupedSymbols.ts +8 -8
- package/src/transpiler/output/headers/types/IHeaderSymbol.ts +62 -0
- package/src/transpiler/state/CodeGenState.ts +109 -4
- package/src/transpiler/state/SymbolRegistry.ts +181 -0
- package/src/transpiler/{types → state}/TranspilerState.ts +1 -1
- package/src/transpiler/state/__tests__/CodeGenState.test.ts +277 -1
- package/src/transpiler/state/__tests__/SymbolRegistry.test.ts +249 -0
- package/src/transpiler/{types → state}/__tests__/TranspilerState.test.ts +1 -1
- package/src/transpiler/types/ICachedFileEntry.ts +1 -1
- package/src/transpiler/types/IConflict.ts +14 -0
- package/src/transpiler/types/ISerializedSymbol.ts +11 -0
- package/src/transpiler/types/TPrimitiveKind.ts +20 -0
- package/src/transpiler/types/TType.ts +103 -0
- package/src/transpiler/types/TVisibility.ts +6 -0
- package/src/transpiler/types/symbol-kinds/TSymbolKind.ts +10 -0
- package/src/transpiler/types/symbol-kinds/TSymbolKindC.ts +12 -0
- package/src/transpiler/types/symbol-kinds/TSymbolKindCNext.ts +16 -0
- package/src/transpiler/types/symbol-kinds/TSymbolKindCpp.ts +14 -0
- package/src/transpiler/types/symbols/IBaseSymbol.ts +31 -0
- package/src/transpiler/{logic/symbols/types → types/symbols}/IBitmapFieldInfo.ts +2 -2
- package/src/transpiler/types/symbols/IBitmapSymbol.ts +21 -0
- package/src/transpiler/{logic/symbols/types → types/symbols}/IEnumSymbol.ts +5 -6
- package/src/transpiler/types/symbols/IFieldInfo.ts +26 -0
- package/src/transpiler/types/symbols/IFunctionSymbol.ts +30 -0
- package/src/transpiler/types/symbols/IParameterInfo.ts +26 -0
- package/src/transpiler/{logic/symbols/types → types/symbols}/IRegisterMemberInfo.ts +4 -4
- package/src/transpiler/types/symbols/IRegisterSymbol.ts +18 -0
- package/src/transpiler/types/symbols/IScopeSymbol.ts +32 -0
- package/src/transpiler/{logic/symbols/types → types/symbols}/IStructFieldInfo.ts +2 -1
- package/src/transpiler/types/symbols/IStructSymbol.ts +15 -0
- package/src/transpiler/types/symbols/IVariableSymbol.ts +30 -0
- package/src/transpiler/types/symbols/SymbolGuards.ts +43 -0
- package/src/transpiler/types/symbols/TAnySymbol.ts +22 -0
- package/src/transpiler/types/symbols/TSymbol.ts +32 -0
- package/src/transpiler/types/symbols/__tests__/IBaseSymbol.test.ts +56 -0
- package/src/transpiler/types/symbols/__tests__/SymbolGuards.test.ts +57 -0
- package/src/transpiler/types/symbols/c/ICBaseSymbol.ts +28 -0
- package/src/transpiler/types/symbols/c/ICEnumMemberSymbol.ts +17 -0
- package/src/transpiler/types/symbols/c/ICEnumSymbol.ts +17 -0
- package/src/transpiler/types/symbols/c/ICFieldInfo.ts +16 -0
- package/src/transpiler/types/symbols/c/ICFunctionSymbol.ts +21 -0
- package/src/transpiler/types/symbols/c/ICParameterInfo.ts +19 -0
- package/src/transpiler/types/symbols/c/ICStructSymbol.ts +21 -0
- package/src/transpiler/types/symbols/c/ICTypedefSymbol.ts +14 -0
- package/src/transpiler/types/symbols/c/ICVariableSymbol.ts +26 -0
- package/src/transpiler/types/symbols/c/TCSymbol.ts +26 -0
- package/src/transpiler/types/symbols/cpp/ICppBaseSymbol.ts +31 -0
- package/src/transpiler/types/symbols/cpp/ICppClassSymbol.ts +15 -0
- package/src/transpiler/types/symbols/cpp/ICppEnumMemberSymbol.ts +14 -0
- package/src/transpiler/types/symbols/cpp/ICppEnumSymbol.ts +14 -0
- package/src/transpiler/types/symbols/cpp/ICppFieldInfo.ts +16 -0
- package/src/transpiler/types/symbols/cpp/ICppFunctionSymbol.ts +21 -0
- package/src/transpiler/types/symbols/cpp/ICppNamespaceSymbol.ts +11 -0
- package/src/transpiler/types/symbols/cpp/ICppParameterInfo.ts +19 -0
- package/src/transpiler/types/symbols/cpp/ICppStructSymbol.ts +16 -0
- package/src/transpiler/types/symbols/cpp/ICppTypeAliasSymbol.ts +14 -0
- package/src/transpiler/types/symbols/cpp/ICppVariableSymbol.ts +23 -0
- package/src/transpiler/types/symbols/cpp/TCppSymbol.ts +30 -0
- package/src/utils/CppNamespaceUtils.ts +3 -4
- package/src/utils/FunctionUtils.ts +92 -0
- package/src/utils/ParameterUtils.ts +55 -0
- package/src/utils/PrimitiveKindUtils.ts +33 -0
- package/src/utils/ScopeUtils.ts +105 -0
- package/src/utils/TTypeUtils.ts +159 -0
- package/src/utils/TypeResolver.ts +132 -0
- package/src/utils/__tests__/CppNamespaceUtils.test.ts +92 -99
- package/src/utils/__tests__/FunctionUtils.test.ts +284 -0
- package/src/utils/__tests__/ParameterUtils.test.ts +174 -0
- package/src/utils/__tests__/PrimitiveKindUtils.test.ts +59 -0
- package/src/utils/__tests__/ScopeUtils.test.ts +53 -0
- package/src/utils/__tests__/TTypeUtils.test.ts +245 -0
- package/src/utils/__tests__/TypeResolver.test.ts +332 -0
- package/src/utils/cache/CacheManager.ts +91 -50
- package/src/utils/cache/__tests__/CacheManager.test.ts +180 -114
- package/src/transpiler/logic/symbols/AutoConstUpdater.ts +0 -93
- package/src/transpiler/logic/symbols/CSymbolCollector.ts +0 -648
- package/src/transpiler/logic/symbols/CppSymbolCollector.ts +0 -874
- package/src/transpiler/logic/symbols/SymbolCollectorContext.ts +0 -68
- package/src/transpiler/logic/symbols/__tests__/AutoConstUpdater.test.ts +0 -418
- package/src/transpiler/logic/symbols/__tests__/CSymbolCollector.test.ts +0 -685
- package/src/transpiler/logic/symbols/__tests__/CppSymbolCollector.test.ts +0 -1146
- package/src/transpiler/logic/symbols/__tests__/SymbolCollectorContext.test.ts +0 -290
- package/src/transpiler/logic/symbols/__tests__/cTestHelpers.ts +0 -43
- package/src/transpiler/logic/symbols/__tests__/cppTestHelpers.ts +0 -40
- package/src/transpiler/logic/symbols/cnext/__tests__/TSymbolAdapter.test.ts +0 -595
- package/src/transpiler/logic/symbols/cnext/adapters/TSymbolAdapter.ts +0 -345
- package/src/transpiler/logic/symbols/types/IBaseSymbol.ts +0 -27
- package/src/transpiler/logic/symbols/types/IBitmapSymbol.ts +0 -23
- package/src/transpiler/logic/symbols/types/ICollectorContext.ts +0 -19
- package/src/transpiler/logic/symbols/types/IConflict.ts +0 -20
- package/src/transpiler/logic/symbols/types/IFieldInfo.ts +0 -18
- package/src/transpiler/logic/symbols/types/IFunctionSymbol.ts +0 -25
- package/src/transpiler/logic/symbols/types/IParameterInfo.ts +0 -24
- package/src/transpiler/logic/symbols/types/IRegisterSymbol.ts +0 -20
- package/src/transpiler/logic/symbols/types/IScopeSymbol.ts +0 -19
- package/src/transpiler/logic/symbols/types/IStructSymbol.ts +0 -16
- package/src/transpiler/logic/symbols/types/IVariableSymbol.ts +0 -30
- package/src/transpiler/logic/symbols/types/TSymbol.ts +0 -36
- package/src/transpiler/logic/symbols/types/__tests__/SymbolGuards.test.ts +0 -244
- package/src/transpiler/logic/symbols/types/typeGuards.ts +0 -44
- package/src/utils/types/ESymbolKind.ts +0 -19
- package/src/utils/types/ISymbol.ts +0 -64
- /package/src/transpiler/{types → constants}/BITMAP_BACKING_TYPE.ts +0 -0
- /package/src/transpiler/{types → constants}/BITMAP_SIZE.ts +0 -0
- /package/src/transpiler/{output/codegen → logic/analysis}/helpers/TransitiveModificationPropagator.ts +0 -0
- /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
|
|
30
|
-
*
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
|
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
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
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
|
|
661
|
+
message,
|
|
637
662
|
});
|
|
638
663
|
}
|
|
639
664
|
|
|
640
665
|
/**
|
|
641
|
-
* Check if a function is defined externally (
|
|
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
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
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 ===
|
|
461
|
+
if (sym.kind === "struct" || sym.kind === "class") {
|
|
463
462
|
return true;
|
|
464
463
|
}
|
|
465
464
|
}
|