c-next 0.1.70 → 0.1.72
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 +180 -63
- package/src/transpiler/logic/analysis/FunctionCallAnalyzer.ts +1 -2
- package/src/transpiler/logic/analysis/InitializationAnalyzer.ts +1 -2
- package/src/transpiler/logic/analysis/PassByValueAnalyzer.ts +51 -2
- package/src/transpiler/logic/analysis/__tests__/FunctionCallAnalyzer.test.ts +18 -12
- 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/logic/symbols/SymbolTable.ts +729 -265
- package/src/transpiler/logic/symbols/SymbolUtils.ts +2 -2
- package/src/transpiler/logic/symbols/__tests__/SymbolTable.test.ts +415 -751
- 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 +268 -1674
- package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +7 -1
- package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +2 -1
- package/src/transpiler/output/codegen/assignment/handlers/AssignmentHandlerUtils.ts +7 -1
- package/src/transpiler/output/codegen/assignment/handlers/BitmapHandlers.ts +6 -2
- package/src/transpiler/output/codegen/assignment/handlers/RegisterHandlers.ts +2 -1
- 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/CallExprUtils.ts +9 -3
- package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprGenerator.test.ts +3 -4
- package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprUtils.test.ts +4 -8
- package/src/transpiler/output/codegen/helpers/ArgumentGenerator.ts +236 -0
- package/src/transpiler/output/codegen/helpers/CppConstructorHelper.ts +3 -3
- package/src/transpiler/output/codegen/helpers/FunctionContextManager.ts +435 -0
- 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__/CppConstructorHelper.test.ts +4 -5
- package/src/transpiler/output/codegen/helpers/__tests__/FunctionContextManager.test.ts +983 -0
- 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 +5 -1
- 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 +20 -33
- 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 +67 -59
- 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/IPipelineInput.ts +0 -3
- 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
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FunctionCollector - Collects function symbols from C parse trees.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
|
|
7
|
+
import type { FunctionDefinitionContext } from "../../../parser/c/grammar/CParser";
|
|
8
|
+
import type ICFunctionSymbol from "../../../../types/symbols/c/ICFunctionSymbol";
|
|
9
|
+
import type ICParameterInfo from "../../../../types/symbols/c/ICParameterInfo";
|
|
10
|
+
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
|
|
11
|
+
import DeclaratorUtils from "../utils/DeclaratorUtils";
|
|
12
|
+
import type IExtractedParameter from "../../shared/IExtractedParameter";
|
|
13
|
+
|
|
14
|
+
class FunctionCollector {
|
|
15
|
+
/**
|
|
16
|
+
* Map extracted parameters to ICParameterInfo array.
|
|
17
|
+
*/
|
|
18
|
+
private static _mapParameters(
|
|
19
|
+
extracted: IExtractedParameter[],
|
|
20
|
+
): ICParameterInfo[] {
|
|
21
|
+
return extracted.map((p) => ({
|
|
22
|
+
name: p.name,
|
|
23
|
+
type: p.type,
|
|
24
|
+
isConst: p.isConst,
|
|
25
|
+
isArray: p.isArray,
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Collect a function symbol from a function definition.
|
|
31
|
+
*
|
|
32
|
+
* @param funcDef The function definition context
|
|
33
|
+
* @param sourceFile Source file path
|
|
34
|
+
*/
|
|
35
|
+
static collectFromDefinition(
|
|
36
|
+
funcDef: FunctionDefinitionContext,
|
|
37
|
+
sourceFile: string,
|
|
38
|
+
): ICFunctionSymbol | null {
|
|
39
|
+
const declarator = funcDef.declarator();
|
|
40
|
+
if (!declarator) return null;
|
|
41
|
+
|
|
42
|
+
const name = DeclaratorUtils.extractDeclaratorName(declarator);
|
|
43
|
+
if (!name) return null;
|
|
44
|
+
|
|
45
|
+
const line = funcDef.start?.line ?? 0;
|
|
46
|
+
|
|
47
|
+
// Get return type from declaration specifiers
|
|
48
|
+
const declSpecs = funcDef.declarationSpecifiers();
|
|
49
|
+
const returnType = declSpecs
|
|
50
|
+
? DeclaratorUtils.extractTypeFromDeclSpecs(declSpecs)
|
|
51
|
+
: "int";
|
|
52
|
+
|
|
53
|
+
const parameters = FunctionCollector._mapParameters(
|
|
54
|
+
DeclaratorUtils.extractFunctionParameters(declarator),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
kind: "function",
|
|
59
|
+
name,
|
|
60
|
+
sourceFile,
|
|
61
|
+
sourceLine: line,
|
|
62
|
+
sourceLanguage: ESourceLanguage.C,
|
|
63
|
+
isExported: true,
|
|
64
|
+
type: returnType,
|
|
65
|
+
parameters: parameters.length > 0 ? parameters : undefined,
|
|
66
|
+
isDeclaration: false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Collect a function symbol from a declaration (prototype).
|
|
72
|
+
*
|
|
73
|
+
* @param name Function name
|
|
74
|
+
* @param baseType Return type
|
|
75
|
+
* @param declarator The declarator context
|
|
76
|
+
* @param sourceFile Source file path
|
|
77
|
+
* @param line Source line number
|
|
78
|
+
* @param isExtern Whether the function is extern
|
|
79
|
+
*/
|
|
80
|
+
static collectFromDeclaration(
|
|
81
|
+
name: string,
|
|
82
|
+
baseType: string,
|
|
83
|
+
declarator: any,
|
|
84
|
+
sourceFile: string,
|
|
85
|
+
line: number,
|
|
86
|
+
isExtern: boolean,
|
|
87
|
+
): ICFunctionSymbol {
|
|
88
|
+
const parameters = FunctionCollector._mapParameters(
|
|
89
|
+
DeclaratorUtils.extractFunctionParameters(declarator),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
kind: "function",
|
|
94
|
+
name,
|
|
95
|
+
sourceFile,
|
|
96
|
+
sourceLine: line,
|
|
97
|
+
sourceLanguage: ESourceLanguage.C,
|
|
98
|
+
isExported: !isExtern,
|
|
99
|
+
type: baseType,
|
|
100
|
+
parameters: parameters.length > 0 ? parameters : undefined,
|
|
101
|
+
isDeclaration: true,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default FunctionCollector;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StructCollector - Collects struct and union symbols from C parse trees.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
|
|
7
|
+
import type { StructOrUnionSpecifierContext } from "../../../parser/c/grammar/CParser";
|
|
8
|
+
import type ICStructSymbol from "../../../../types/symbols/c/ICStructSymbol";
|
|
9
|
+
import type ICFieldInfo from "../../../../types/symbols/c/ICFieldInfo";
|
|
10
|
+
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
|
|
11
|
+
import SymbolTable from "../../SymbolTable";
|
|
12
|
+
import SymbolUtils from "../../SymbolUtils";
|
|
13
|
+
import DeclaratorUtils from "../utils/DeclaratorUtils";
|
|
14
|
+
|
|
15
|
+
class StructCollector {
|
|
16
|
+
/**
|
|
17
|
+
* Collect a struct or union symbol from a specifier context.
|
|
18
|
+
*
|
|
19
|
+
* @param structSpec The struct/union specifier context
|
|
20
|
+
* @param sourceFile Source file path
|
|
21
|
+
* @param line Source line number
|
|
22
|
+
* @param symbolTable Optional symbol table for field tracking
|
|
23
|
+
* @param typedefName Optional typedef name for anonymous structs
|
|
24
|
+
* @param isTypedef Whether this is part of a typedef declaration
|
|
25
|
+
* @param warnings Array to collect warnings
|
|
26
|
+
*/
|
|
27
|
+
static collect(
|
|
28
|
+
structSpec: StructOrUnionSpecifierContext,
|
|
29
|
+
sourceFile: string,
|
|
30
|
+
line: number,
|
|
31
|
+
symbolTable: SymbolTable | null,
|
|
32
|
+
typedefName?: string,
|
|
33
|
+
isTypedef?: boolean,
|
|
34
|
+
warnings?: string[],
|
|
35
|
+
): ICStructSymbol | null {
|
|
36
|
+
const identifier = structSpec.Identifier();
|
|
37
|
+
|
|
38
|
+
// Use typedef name for anonymous structs (e.g., typedef struct { ... } AppConfig;)
|
|
39
|
+
const name = identifier?.getText() || typedefName;
|
|
40
|
+
if (!name) return null; // Skip if no name available
|
|
41
|
+
|
|
42
|
+
const isUnion = structSpec.structOrUnion()?.getText() === "union";
|
|
43
|
+
|
|
44
|
+
// Extract fields if struct has a body
|
|
45
|
+
const fields = StructCollector.collectFields(
|
|
46
|
+
structSpec,
|
|
47
|
+
name,
|
|
48
|
+
symbolTable,
|
|
49
|
+
warnings,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Mark named structs that are not typedef'd - they need 'struct' keyword
|
|
53
|
+
// Example: "struct NamedPoint { ... };" -> needs "struct NamedPoint var"
|
|
54
|
+
// But "typedef struct { ... } Rectangle;" -> just "Rectangle var"
|
|
55
|
+
const needsStructKeyword = Boolean(identifier && !isTypedef);
|
|
56
|
+
|
|
57
|
+
if (symbolTable && needsStructKeyword) {
|
|
58
|
+
symbolTable.markNeedsStructKeyword(name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
kind: "struct",
|
|
63
|
+
name,
|
|
64
|
+
sourceFile,
|
|
65
|
+
sourceLine: line,
|
|
66
|
+
sourceLanguage: ESourceLanguage.C,
|
|
67
|
+
isExported: true,
|
|
68
|
+
isUnion,
|
|
69
|
+
needsStructKeyword,
|
|
70
|
+
fields: fields.size > 0 ? fields : undefined,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Collect fields from a struct/union definition.
|
|
76
|
+
*/
|
|
77
|
+
private static collectFields(
|
|
78
|
+
structSpec: StructOrUnionSpecifierContext,
|
|
79
|
+
structName: string,
|
|
80
|
+
symbolTable: SymbolTable | null,
|
|
81
|
+
warnings?: string[],
|
|
82
|
+
): ReadonlyMap<string, ICFieldInfo> {
|
|
83
|
+
const fields = new Map<string, ICFieldInfo>();
|
|
84
|
+
|
|
85
|
+
const declList = structSpec.structDeclarationList();
|
|
86
|
+
if (!declList) return fields;
|
|
87
|
+
|
|
88
|
+
for (const structDecl of declList.structDeclaration()) {
|
|
89
|
+
StructCollector.collectFieldsFromDecl(
|
|
90
|
+
structDecl,
|
|
91
|
+
structName,
|
|
92
|
+
fields,
|
|
93
|
+
symbolTable,
|
|
94
|
+
warnings,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return fields;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Collect fields from a single struct declaration.
|
|
103
|
+
*/
|
|
104
|
+
private static collectFieldsFromDecl(
|
|
105
|
+
structDecl: any,
|
|
106
|
+
structName: string,
|
|
107
|
+
fields: Map<string, ICFieldInfo>,
|
|
108
|
+
symbolTable: SymbolTable | null,
|
|
109
|
+
warnings?: string[],
|
|
110
|
+
): void {
|
|
111
|
+
const specQualList = structDecl.specifierQualifierList?.();
|
|
112
|
+
if (!specQualList) return;
|
|
113
|
+
|
|
114
|
+
const fieldType = DeclaratorUtils.extractTypeFromSpecQualList(specQualList);
|
|
115
|
+
const structDeclList = structDecl.structDeclaratorList?.();
|
|
116
|
+
if (!structDeclList) return;
|
|
117
|
+
|
|
118
|
+
for (const structDeclarator of structDeclList.structDeclarator()) {
|
|
119
|
+
StructCollector.processFieldDeclarator(
|
|
120
|
+
structDeclarator,
|
|
121
|
+
structName,
|
|
122
|
+
fieldType,
|
|
123
|
+
fields,
|
|
124
|
+
symbolTable,
|
|
125
|
+
warnings,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Process a single field declarator and add to fields map.
|
|
132
|
+
*/
|
|
133
|
+
private static processFieldDeclarator(
|
|
134
|
+
structDeclarator: any,
|
|
135
|
+
structName: string,
|
|
136
|
+
fieldType: string,
|
|
137
|
+
fields: Map<string, ICFieldInfo>,
|
|
138
|
+
symbolTable: SymbolTable | null,
|
|
139
|
+
warnings?: string[],
|
|
140
|
+
): void {
|
|
141
|
+
const declarator = structDeclarator.declarator?.();
|
|
142
|
+
if (!declarator) return;
|
|
143
|
+
|
|
144
|
+
const fieldName = DeclaratorUtils.extractDeclaratorName(declarator);
|
|
145
|
+
if (!fieldName) return;
|
|
146
|
+
|
|
147
|
+
if (warnings && SymbolUtils.isReservedFieldName(fieldName)) {
|
|
148
|
+
warnings.push(
|
|
149
|
+
SymbolUtils.getReservedFieldWarning("C", structName, fieldName),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const arrayDimensions = DeclaratorUtils.extractArrayDimensions(declarator);
|
|
154
|
+
const fieldInfo: ICFieldInfo = {
|
|
155
|
+
name: fieldName,
|
|
156
|
+
type: fieldType,
|
|
157
|
+
arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
fields.set(fieldName, fieldInfo);
|
|
161
|
+
|
|
162
|
+
if (symbolTable) {
|
|
163
|
+
symbolTable.addStructField(
|
|
164
|
+
structName,
|
|
165
|
+
fieldName,
|
|
166
|
+
fieldType,
|
|
167
|
+
arrayDimensions.length > 0 ? arrayDimensions : undefined,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export default StructCollector;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypedefCollector - Collects typedef symbols from C parse trees.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type ICTypedefSymbol from "../../../../types/symbols/c/ICTypedefSymbol";
|
|
6
|
+
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
|
|
7
|
+
|
|
8
|
+
class TypedefCollector {
|
|
9
|
+
/**
|
|
10
|
+
* Collect a typedef symbol.
|
|
11
|
+
*
|
|
12
|
+
* @param name Typedef name
|
|
13
|
+
* @param baseType The underlying type
|
|
14
|
+
* @param sourceFile Source file path
|
|
15
|
+
* @param line Source line number
|
|
16
|
+
*/
|
|
17
|
+
static collect(
|
|
18
|
+
name: string,
|
|
19
|
+
baseType: string,
|
|
20
|
+
sourceFile: string,
|
|
21
|
+
line: number,
|
|
22
|
+
): ICTypedefSymbol {
|
|
23
|
+
return {
|
|
24
|
+
kind: "type",
|
|
25
|
+
name,
|
|
26
|
+
sourceFile,
|
|
27
|
+
sourceLine: line,
|
|
28
|
+
sourceLanguage: ESourceLanguage.C,
|
|
29
|
+
isExported: true,
|
|
30
|
+
type: baseType,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default TypedefCollector;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VariableCollector - Collects variable symbols from C parse trees.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
|
|
7
|
+
import type ICVariableSymbol from "../../../../types/symbols/c/ICVariableSymbol";
|
|
8
|
+
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
|
|
9
|
+
import DeclaratorUtils from "../utils/DeclaratorUtils";
|
|
10
|
+
|
|
11
|
+
class VariableCollector {
|
|
12
|
+
/**
|
|
13
|
+
* Collect a variable symbol from a declarator.
|
|
14
|
+
*
|
|
15
|
+
* @param name Variable name
|
|
16
|
+
* @param baseType Variable type
|
|
17
|
+
* @param declarator The declarator context (for array dimensions)
|
|
18
|
+
* @param sourceFile Source file path
|
|
19
|
+
* @param line Source line number
|
|
20
|
+
* @param isExtern Whether the variable is extern
|
|
21
|
+
*/
|
|
22
|
+
static collect(
|
|
23
|
+
name: string,
|
|
24
|
+
baseType: string,
|
|
25
|
+
declarator: any,
|
|
26
|
+
sourceFile: string,
|
|
27
|
+
line: number,
|
|
28
|
+
isExtern: boolean,
|
|
29
|
+
): ICVariableSymbol {
|
|
30
|
+
// Extract array dimensions if present
|
|
31
|
+
const arrayDimensions = declarator
|
|
32
|
+
? DeclaratorUtils.extractArrayDimensions(declarator)
|
|
33
|
+
: [];
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
kind: "variable",
|
|
37
|
+
name,
|
|
38
|
+
sourceFile,
|
|
39
|
+
sourceLine: line,
|
|
40
|
+
sourceLanguage: ESourceLanguage.C,
|
|
41
|
+
isExported: !isExtern,
|
|
42
|
+
type: baseType,
|
|
43
|
+
isArray: arrayDimensions.length > 0,
|
|
44
|
+
arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
|
|
45
|
+
isExtern,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Collect a variable from declaration specifiers (when identifier appears as typedefName).
|
|
51
|
+
* This handles the C grammar ambiguity where variable names can be parsed as typedef names.
|
|
52
|
+
*
|
|
53
|
+
* @param name Variable name
|
|
54
|
+
* @param baseType Variable type
|
|
55
|
+
* @param sourceFile Source file path
|
|
56
|
+
* @param line Source line number
|
|
57
|
+
* @param isExtern Whether the variable is extern
|
|
58
|
+
*/
|
|
59
|
+
static collectFromDeclSpecs(
|
|
60
|
+
name: string,
|
|
61
|
+
baseType: string,
|
|
62
|
+
sourceFile: string,
|
|
63
|
+
line: number,
|
|
64
|
+
isExtern: boolean,
|
|
65
|
+
): ICVariableSymbol {
|
|
66
|
+
return {
|
|
67
|
+
kind: "variable",
|
|
68
|
+
name,
|
|
69
|
+
sourceFile,
|
|
70
|
+
sourceLine: line,
|
|
71
|
+
sourceLanguage: ESourceLanguage.C,
|
|
72
|
+
isExported: !isExtern,
|
|
73
|
+
type: baseType,
|
|
74
|
+
isArray: false,
|
|
75
|
+
isExtern,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export default VariableCollector;
|