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
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CppResolver - Orchestrates symbol collection from C++ parse trees.
|
|
3
|
+
*
|
|
4
|
+
* Uses composable collectors to extract symbols from different C++ constructs.
|
|
5
|
+
* Produces TCppSymbol instances (discriminated union of C++ symbol types).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
import { CPP14Parser } from "../../parser/cpp/grammar/CPP14Parser";
|
|
11
|
+
import TCppSymbol from "../../../types/symbols/cpp/TCppSymbol";
|
|
12
|
+
import SymbolTable from "../SymbolTable";
|
|
13
|
+
import NamespaceCollector from "./collectors/NamespaceCollector";
|
|
14
|
+
import EnumCollector from "./collectors/EnumCollector";
|
|
15
|
+
import TypeAliasCollector from "./collectors/TypeAliasCollector";
|
|
16
|
+
import FunctionCollector from "./collectors/FunctionCollector";
|
|
17
|
+
import ClassCollector from "./collectors/ClassCollector";
|
|
18
|
+
import VariableCollector from "./collectors/VariableCollector";
|
|
19
|
+
import DeclaratorUtils from "./utils/DeclaratorUtils";
|
|
20
|
+
|
|
21
|
+
// Import context types
|
|
22
|
+
type TranslationUnitContext = ReturnType<CPP14Parser["translationUnit"]>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Result of resolving C++ symbols.
|
|
26
|
+
*/
|
|
27
|
+
interface ICppResolverResult {
|
|
28
|
+
symbols: TCppSymbol[];
|
|
29
|
+
warnings: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Internal context for declaration processing.
|
|
34
|
+
*/
|
|
35
|
+
interface ICppDeclarationContext {
|
|
36
|
+
readonly sourceFile: string;
|
|
37
|
+
readonly currentNamespace: string | undefined;
|
|
38
|
+
readonly symbolTable: SymbolTable | undefined;
|
|
39
|
+
readonly symbols: TCppSymbol[];
|
|
40
|
+
readonly warnings: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class CppResolver {
|
|
44
|
+
/**
|
|
45
|
+
* Resolve all symbols from a C++ translation unit.
|
|
46
|
+
*
|
|
47
|
+
* @param tree The translation unit context from the parser
|
|
48
|
+
* @param sourceFile Source file path
|
|
49
|
+
* @param symbolTable Optional symbol table for storing struct fields, enum bit widths
|
|
50
|
+
* @returns Result containing symbols and warnings
|
|
51
|
+
*/
|
|
52
|
+
static resolve(
|
|
53
|
+
tree: TranslationUnitContext,
|
|
54
|
+
sourceFile: string,
|
|
55
|
+
symbolTable?: SymbolTable,
|
|
56
|
+
): ICppResolverResult {
|
|
57
|
+
const symbols: TCppSymbol[] = [];
|
|
58
|
+
const warnings: string[] = [];
|
|
59
|
+
|
|
60
|
+
if (!tree) {
|
|
61
|
+
return { symbols, warnings };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const declSeq = tree.declarationseq?.();
|
|
65
|
+
if (!declSeq) {
|
|
66
|
+
return { symbols, warnings };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const ctx: ICppDeclarationContext = {
|
|
70
|
+
sourceFile,
|
|
71
|
+
currentNamespace: undefined,
|
|
72
|
+
symbolTable,
|
|
73
|
+
symbols,
|
|
74
|
+
warnings,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
for (const decl of declSeq.declaration()) {
|
|
78
|
+
CppResolver._collectDeclaration(decl, ctx);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { symbols, warnings };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Collect symbols from a single declaration.
|
|
86
|
+
*/
|
|
87
|
+
private static _collectDeclaration(
|
|
88
|
+
decl: any,
|
|
89
|
+
ctx: ICppDeclarationContext,
|
|
90
|
+
): void {
|
|
91
|
+
const line = decl.start?.line ?? 0;
|
|
92
|
+
|
|
93
|
+
// Function definition
|
|
94
|
+
const funcDef = decl.functionDefinition?.();
|
|
95
|
+
if (funcDef) {
|
|
96
|
+
const symbol = FunctionCollector.collectDefinition(
|
|
97
|
+
funcDef,
|
|
98
|
+
ctx.sourceFile,
|
|
99
|
+
line,
|
|
100
|
+
ctx.currentNamespace,
|
|
101
|
+
);
|
|
102
|
+
if (symbol) {
|
|
103
|
+
ctx.symbols.push(symbol);
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Namespace definition
|
|
109
|
+
const nsDef = decl.namespaceDefinition?.();
|
|
110
|
+
if (nsDef) {
|
|
111
|
+
CppResolver._collectNamespaceDefinition(nsDef, line, ctx);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Template declaration - skip for now (complex to handle)
|
|
116
|
+
const templDecl = decl.templateDeclaration?.();
|
|
117
|
+
if (templDecl) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Block declaration (simpleDeclaration, etc.)
|
|
122
|
+
const blockDecl = decl.blockDeclaration?.();
|
|
123
|
+
if (blockDecl) {
|
|
124
|
+
CppResolver._collectBlockDeclaration(blockDecl, line, ctx);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Collect symbols from a namespace definition.
|
|
130
|
+
*/
|
|
131
|
+
private static _collectNamespaceDefinition(
|
|
132
|
+
nsDef: any,
|
|
133
|
+
line: number,
|
|
134
|
+
ctx: ICppDeclarationContext,
|
|
135
|
+
): void {
|
|
136
|
+
const nsSymbol = NamespaceCollector.collect(
|
|
137
|
+
nsDef,
|
|
138
|
+
ctx.sourceFile,
|
|
139
|
+
line,
|
|
140
|
+
ctx.currentNamespace,
|
|
141
|
+
);
|
|
142
|
+
if (nsSymbol) {
|
|
143
|
+
ctx.symbols.push(nsSymbol);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Process namespace body with updated namespace
|
|
147
|
+
const newNamespace = NamespaceCollector.getFullNamespaceName(
|
|
148
|
+
nsDef,
|
|
149
|
+
ctx.currentNamespace,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const body = nsDef.declarationseq?.();
|
|
153
|
+
if (body) {
|
|
154
|
+
const nestedCtx: ICppDeclarationContext = {
|
|
155
|
+
...ctx,
|
|
156
|
+
currentNamespace: newNamespace,
|
|
157
|
+
};
|
|
158
|
+
for (const decl of body.declaration()) {
|
|
159
|
+
CppResolver._collectDeclaration(decl, nestedCtx);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Collect symbols from a block declaration.
|
|
166
|
+
*/
|
|
167
|
+
private static _collectBlockDeclaration(
|
|
168
|
+
blockDecl: any,
|
|
169
|
+
line: number,
|
|
170
|
+
ctx: ICppDeclarationContext,
|
|
171
|
+
): void {
|
|
172
|
+
// Simple declaration (variables, typedefs, class declarations)
|
|
173
|
+
const simpleDecl = blockDecl.simpleDeclaration?.();
|
|
174
|
+
if (simpleDecl) {
|
|
175
|
+
CppResolver._collectSimpleDeclaration(simpleDecl, line, ctx);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Alias declaration (using X = Y)
|
|
179
|
+
const aliasDecl = blockDecl.aliasDeclaration?.();
|
|
180
|
+
if (aliasDecl) {
|
|
181
|
+
const symbol = TypeAliasCollector.collect(
|
|
182
|
+
aliasDecl,
|
|
183
|
+
ctx.sourceFile,
|
|
184
|
+
line,
|
|
185
|
+
ctx.currentNamespace,
|
|
186
|
+
);
|
|
187
|
+
if (symbol) {
|
|
188
|
+
ctx.symbols.push(symbol);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Collect symbols from a simple declaration.
|
|
195
|
+
*/
|
|
196
|
+
private static _collectSimpleDeclaration(
|
|
197
|
+
simpleDecl: any,
|
|
198
|
+
line: number,
|
|
199
|
+
ctx: ICppDeclarationContext,
|
|
200
|
+
): void {
|
|
201
|
+
const declSpecSeq = simpleDecl.declSpecifierSeq?.();
|
|
202
|
+
if (!declSpecSeq) return;
|
|
203
|
+
|
|
204
|
+
const baseType = DeclaratorUtils.extractTypeFromDeclSpecSeq(declSpecSeq);
|
|
205
|
+
|
|
206
|
+
// Process type specifiers (classes, enums)
|
|
207
|
+
const anonymousClassSpec = CppResolver._processTypeSpecifiers(
|
|
208
|
+
declSpecSeq,
|
|
209
|
+
line,
|
|
210
|
+
ctx,
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
// Collect declarators (variables, function prototypes)
|
|
214
|
+
const initDeclList = simpleDecl.initDeclaratorList?.();
|
|
215
|
+
if (!initDeclList) return;
|
|
216
|
+
|
|
217
|
+
for (const initDecl of initDeclList.initDeclarator()) {
|
|
218
|
+
const declarator = initDecl.declarator?.();
|
|
219
|
+
if (declarator) {
|
|
220
|
+
CppResolver._collectDeclarator(
|
|
221
|
+
declarator,
|
|
222
|
+
baseType,
|
|
223
|
+
line,
|
|
224
|
+
ctx,
|
|
225
|
+
anonymousClassSpec,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Process type specifiers in a declaration, collecting classes and enums.
|
|
233
|
+
* Returns anonymous class specifier if found (for typedef handling).
|
|
234
|
+
*/
|
|
235
|
+
private static _processTypeSpecifiers(
|
|
236
|
+
declSpecSeq: any,
|
|
237
|
+
line: number,
|
|
238
|
+
ctx: ICppDeclarationContext,
|
|
239
|
+
): any {
|
|
240
|
+
let anonymousClassSpec: any = null;
|
|
241
|
+
|
|
242
|
+
for (const spec of declSpecSeq.declSpecifier?.() ?? []) {
|
|
243
|
+
const typeSpec = spec.typeSpecifier?.();
|
|
244
|
+
if (!typeSpec) continue;
|
|
245
|
+
|
|
246
|
+
const classSpec = typeSpec.classSpecifier?.();
|
|
247
|
+
if (classSpec) {
|
|
248
|
+
const result = CppResolver._handleClassSpecInDecl(classSpec, line, ctx);
|
|
249
|
+
if (result) {
|
|
250
|
+
anonymousClassSpec = result;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const enumSpec = typeSpec.enumSpecifier?.();
|
|
255
|
+
if (enumSpec) {
|
|
256
|
+
const enumSymbol = EnumCollector.collect(
|
|
257
|
+
enumSpec,
|
|
258
|
+
ctx.sourceFile,
|
|
259
|
+
line,
|
|
260
|
+
ctx.currentNamespace,
|
|
261
|
+
ctx.symbolTable,
|
|
262
|
+
);
|
|
263
|
+
if (enumSymbol) {
|
|
264
|
+
ctx.symbols.push(enumSymbol);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return anonymousClassSpec;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Handle a class specifier in a declaration.
|
|
274
|
+
* Returns the class spec if it's anonymous (for typedef handling).
|
|
275
|
+
*/
|
|
276
|
+
private static _handleClassSpecInDecl(
|
|
277
|
+
classSpec: any,
|
|
278
|
+
line: number,
|
|
279
|
+
ctx: ICppDeclarationContext,
|
|
280
|
+
): any {
|
|
281
|
+
const classHead = classSpec.classHead?.();
|
|
282
|
+
const classHeadName = classHead?.classHeadName?.();
|
|
283
|
+
const className = classHeadName?.className?.();
|
|
284
|
+
const identifier = className?.Identifier?.();
|
|
285
|
+
|
|
286
|
+
if (identifier?.getText()) {
|
|
287
|
+
// Named struct - collect normally
|
|
288
|
+
const result = ClassCollector.collect(
|
|
289
|
+
classSpec,
|
|
290
|
+
ctx.sourceFile,
|
|
291
|
+
line,
|
|
292
|
+
ctx.currentNamespace,
|
|
293
|
+
ctx.symbolTable,
|
|
294
|
+
);
|
|
295
|
+
if (result) {
|
|
296
|
+
ctx.symbols.push(result.classSymbol, ...result.memberFunctions);
|
|
297
|
+
ctx.warnings.push(...result.warnings);
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Anonymous struct - return for typedef handling
|
|
303
|
+
return classSpec;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Process a single declarator (variable or function).
|
|
308
|
+
*/
|
|
309
|
+
private static _collectDeclarator(
|
|
310
|
+
declarator: any,
|
|
311
|
+
baseType: string,
|
|
312
|
+
line: number,
|
|
313
|
+
ctx: ICppDeclarationContext,
|
|
314
|
+
anonymousClassSpec: any,
|
|
315
|
+
): void {
|
|
316
|
+
const name = DeclaratorUtils.extractDeclaratorName(declarator);
|
|
317
|
+
if (!name) return;
|
|
318
|
+
|
|
319
|
+
const isFunction = DeclaratorUtils.declaratorIsFunction(declarator);
|
|
320
|
+
const fullName = ctx.currentNamespace
|
|
321
|
+
? `${ctx.currentNamespace}::${name}`
|
|
322
|
+
: name;
|
|
323
|
+
|
|
324
|
+
// Handle anonymous struct typedef
|
|
325
|
+
if (anonymousClassSpec && ctx.symbolTable) {
|
|
326
|
+
const result = ClassCollector.collectAnonymousTypedef(
|
|
327
|
+
anonymousClassSpec,
|
|
328
|
+
fullName,
|
|
329
|
+
ctx.sourceFile,
|
|
330
|
+
line,
|
|
331
|
+
ctx.symbolTable,
|
|
332
|
+
);
|
|
333
|
+
if (result) {
|
|
334
|
+
ctx.symbols.push(result.classSymbol, ...result.memberFunctions);
|
|
335
|
+
ctx.warnings.push(...result.warnings);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (isFunction) {
|
|
341
|
+
const funcSymbol = FunctionCollector.collectDeclaration(
|
|
342
|
+
declarator,
|
|
343
|
+
baseType,
|
|
344
|
+
ctx.sourceFile,
|
|
345
|
+
line,
|
|
346
|
+
ctx.currentNamespace,
|
|
347
|
+
);
|
|
348
|
+
if (funcSymbol) {
|
|
349
|
+
ctx.symbols.push(funcSymbol);
|
|
350
|
+
}
|
|
351
|
+
} else {
|
|
352
|
+
const varSymbol = VariableCollector.collect(
|
|
353
|
+
declarator,
|
|
354
|
+
baseType,
|
|
355
|
+
ctx.sourceFile,
|
|
356
|
+
line,
|
|
357
|
+
ctx.currentNamespace,
|
|
358
|
+
);
|
|
359
|
+
if (varSymbol) {
|
|
360
|
+
ctx.symbols.push(varSymbol);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export default CppResolver;
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeclaratorUtils - Shared utilities for extracting information from C++ declarators.
|
|
3
|
+
*
|
|
4
|
+
* Provides methods for extracting names, types, parameters, and array dimensions
|
|
5
|
+
* from C++ parse tree declarator contexts.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
import SymbolUtils from "../../SymbolUtils";
|
|
11
|
+
import IExtractedParameter from "../../shared/IExtractedParameter";
|
|
12
|
+
import ParameterExtractorUtils from "../../shared/ParameterExtractorUtils";
|
|
13
|
+
|
|
14
|
+
class DeclaratorUtils {
|
|
15
|
+
/**
|
|
16
|
+
* Extract name from a declarator context.
|
|
17
|
+
*/
|
|
18
|
+
static extractDeclaratorName(declarator: any): string | null {
|
|
19
|
+
// Pointer declarator -> noPointerDeclarator
|
|
20
|
+
const ptrDecl = declarator.pointerDeclarator?.();
|
|
21
|
+
if (ptrDecl) {
|
|
22
|
+
const noPtr = ptrDecl.noPointerDeclarator?.();
|
|
23
|
+
if (noPtr) {
|
|
24
|
+
return DeclaratorUtils.extractNoPointerDeclaratorName(noPtr);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// No pointer declarator
|
|
29
|
+
const noPtr = declarator.noPointerDeclarator?.();
|
|
30
|
+
if (noPtr) {
|
|
31
|
+
return DeclaratorUtils.extractNoPointerDeclaratorName(noPtr);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Extract name from a noPointerDeclarator context.
|
|
39
|
+
*/
|
|
40
|
+
static extractNoPointerDeclaratorName(noPtr: any): string | null {
|
|
41
|
+
const declId = noPtr.declaratorid?.();
|
|
42
|
+
if (declId) {
|
|
43
|
+
const idExpr = declId.idExpression?.();
|
|
44
|
+
if (idExpr) {
|
|
45
|
+
const unqualId = idExpr.unqualifiedId?.();
|
|
46
|
+
if (unqualId) {
|
|
47
|
+
const identifier = unqualId.Identifier?.();
|
|
48
|
+
if (identifier) {
|
|
49
|
+
return identifier.getText();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Recursive case
|
|
56
|
+
const innerNoPtr = noPtr.noPointerDeclarator?.();
|
|
57
|
+
if (innerNoPtr) {
|
|
58
|
+
return DeclaratorUtils.extractNoPointerDeclaratorName(innerNoPtr);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check if a declarator represents a function.
|
|
66
|
+
*/
|
|
67
|
+
static declaratorIsFunction(declarator: any): boolean {
|
|
68
|
+
const ptrDecl = declarator.pointerDeclarator?.();
|
|
69
|
+
if (ptrDecl) {
|
|
70
|
+
const noPtr = ptrDecl.noPointerDeclarator?.();
|
|
71
|
+
if (noPtr?.parametersAndQualifiers?.()) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const noPtr = declarator.noPointerDeclarator?.();
|
|
77
|
+
if (noPtr?.parametersAndQualifiers?.()) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Extract function parameters from a declarator.
|
|
86
|
+
*/
|
|
87
|
+
static extractFunctionParameters(declarator: any): IExtractedParameter[] {
|
|
88
|
+
const params: IExtractedParameter[] = [];
|
|
89
|
+
|
|
90
|
+
// Find parametersAndQualifiers from the declarator
|
|
91
|
+
let paramsAndQuals: any = null;
|
|
92
|
+
const ptrDecl = declarator.pointerDeclarator?.();
|
|
93
|
+
if (ptrDecl) {
|
|
94
|
+
const noPtr = ptrDecl.noPointerDeclarator?.();
|
|
95
|
+
paramsAndQuals = noPtr?.parametersAndQualifiers?.();
|
|
96
|
+
}
|
|
97
|
+
if (!paramsAndQuals) {
|
|
98
|
+
const noPtr = declarator.noPointerDeclarator?.();
|
|
99
|
+
paramsAndQuals = noPtr?.parametersAndQualifiers?.();
|
|
100
|
+
}
|
|
101
|
+
if (!paramsAndQuals) {
|
|
102
|
+
return params;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Get parameterDeclarationClause
|
|
106
|
+
const paramClause = paramsAndQuals.parameterDeclarationClause?.();
|
|
107
|
+
if (!paramClause) {
|
|
108
|
+
return params;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Get parameterDeclarationList
|
|
112
|
+
const paramList = paramClause.parameterDeclarationList?.();
|
|
113
|
+
if (!paramList) {
|
|
114
|
+
return params;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return ParameterExtractorUtils.processParameterList(
|
|
118
|
+
paramList,
|
|
119
|
+
DeclaratorUtils.extractParameterInfo,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Extract parameter info from a single parameter declaration.
|
|
125
|
+
*/
|
|
126
|
+
static extractParameterInfo(paramDecl: any): IExtractedParameter | null {
|
|
127
|
+
// Get the type from declSpecifierSeq
|
|
128
|
+
const declSpecSeq = paramDecl.declSpecifierSeq?.();
|
|
129
|
+
if (!declSpecSeq) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const baseType = DeclaratorUtils.extractTypeFromDeclSpecSeq(declSpecSeq);
|
|
134
|
+
const isConst = declSpecSeq.getText().includes("const");
|
|
135
|
+
|
|
136
|
+
// Check for pointer in declarator or abstractDeclarator
|
|
137
|
+
const declarator = paramDecl.declarator?.();
|
|
138
|
+
const abstractDecl = paramDecl.abstractDeclarator?.();
|
|
139
|
+
|
|
140
|
+
const isPointer =
|
|
141
|
+
(declarator && DeclaratorUtils.declaratorHasPointer(declarator)) ||
|
|
142
|
+
(abstractDecl &&
|
|
143
|
+
DeclaratorUtils.abstractDeclaratorHasPointer(abstractDecl));
|
|
144
|
+
|
|
145
|
+
return ParameterExtractorUtils.buildParameterInfo(
|
|
146
|
+
declarator,
|
|
147
|
+
baseType,
|
|
148
|
+
isConst,
|
|
149
|
+
isPointer ?? false,
|
|
150
|
+
false, // isArray - could be enhanced
|
|
151
|
+
DeclaratorUtils.extractDeclaratorName,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Check if a declarator contains a pointer operator.
|
|
157
|
+
*/
|
|
158
|
+
static declaratorHasPointer(declarator: any): boolean {
|
|
159
|
+
const ptrDecl = declarator.pointerDeclarator?.();
|
|
160
|
+
if (ptrDecl) {
|
|
161
|
+
// Check for pointerOperator children
|
|
162
|
+
const ptrOps = ptrDecl.pointerOperator?.();
|
|
163
|
+
if (ptrOps && ptrOps.length > 0) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
// Also check getText for *
|
|
167
|
+
if (ptrDecl.getText().includes("*")) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Check if an abstract declarator contains a pointer.
|
|
176
|
+
*/
|
|
177
|
+
static abstractDeclaratorHasPointer(abstractDecl: any): boolean {
|
|
178
|
+
// Check for pointerAbstractDeclarator
|
|
179
|
+
const ptrAbstract = abstractDecl.pointerAbstractDeclarator?.();
|
|
180
|
+
if (ptrAbstract) {
|
|
181
|
+
const ptrOps = ptrAbstract.pointerOperator?.();
|
|
182
|
+
if (ptrOps && ptrOps.length > 0) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
if (ptrAbstract.getText().includes("*")) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// Simple check for * in the text
|
|
190
|
+
if (abstractDecl.getText().includes("*")) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Extract type string from a declSpecifierSeq context.
|
|
198
|
+
*/
|
|
199
|
+
static extractTypeFromDeclSpecSeq(declSpecSeq: any): string {
|
|
200
|
+
const parts: string[] = [];
|
|
201
|
+
|
|
202
|
+
for (const spec of declSpecSeq.declSpecifier?.() ?? []) {
|
|
203
|
+
const typeSpec = spec.typeSpecifier?.();
|
|
204
|
+
if (typeSpec) {
|
|
205
|
+
const trailingType = typeSpec.trailingTypeSpecifier?.();
|
|
206
|
+
if (trailingType) {
|
|
207
|
+
const simpleType = trailingType.simpleTypeSpecifier?.();
|
|
208
|
+
if (simpleType) {
|
|
209
|
+
parts.push(simpleType.getText());
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return parts.join(" ") || "int";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Extract array dimensions from a declarator.
|
|
220
|
+
*/
|
|
221
|
+
static extractArrayDimensions(declarator: any): number[] {
|
|
222
|
+
// For C++, we need to check both pointer and no-pointer declarators
|
|
223
|
+
const ptrDecl = declarator.pointerDeclarator?.();
|
|
224
|
+
if (ptrDecl) {
|
|
225
|
+
const noPtr = ptrDecl.noPointerDeclarator?.();
|
|
226
|
+
if (noPtr) {
|
|
227
|
+
return DeclaratorUtils.extractArrayDimensionsFromNoPtr(noPtr);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const noPtr = declarator.noPointerDeclarator?.();
|
|
232
|
+
if (noPtr) {
|
|
233
|
+
return DeclaratorUtils.extractArrayDimensionsFromNoPtr(noPtr);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return [];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Extract array dimensions from a noPointerDeclarator.
|
|
241
|
+
*/
|
|
242
|
+
static extractArrayDimensionsFromNoPtr(noPtr: any): number[] {
|
|
243
|
+
// Use shared utility for regex-based extraction
|
|
244
|
+
return SymbolUtils.parseArrayDimensions(noPtr.getText());
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export default DeclaratorUtils;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Information extracted from a C/C++ parameter declaration.
|
|
3
|
+
*/
|
|
4
|
+
interface IExtractedParameter {
|
|
5
|
+
/** Parameter name (may be empty for abstract declarators) */
|
|
6
|
+
readonly name: string;
|
|
7
|
+
|
|
8
|
+
/** Parameter type as string */
|
|
9
|
+
readonly type: string;
|
|
10
|
+
|
|
11
|
+
/** Whether the parameter is const-qualified */
|
|
12
|
+
readonly isConst: boolean;
|
|
13
|
+
|
|
14
|
+
/** Whether the parameter is an array */
|
|
15
|
+
readonly isArray: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default IExtractedParameter;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ParameterExtractorUtils - Shared utilities for parameter extraction in C/C++.
|
|
3
|
+
*
|
|
4
|
+
* Contains common patterns for building parameter info from extracted values.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
+
|
|
9
|
+
import IExtractedParameter from "./IExtractedParameter";
|
|
10
|
+
|
|
11
|
+
class ParameterExtractorUtils {
|
|
12
|
+
/**
|
|
13
|
+
* Process a list of parameter declarations and collect parameter info.
|
|
14
|
+
*
|
|
15
|
+
* @param paramList The parameter list context (from either C or C++ grammar)
|
|
16
|
+
* @param extractInfo Function to extract parameter info from a single declaration
|
|
17
|
+
* @returns Array of extracted parameters
|
|
18
|
+
*/
|
|
19
|
+
static processParameterList(
|
|
20
|
+
paramList: any,
|
|
21
|
+
extractInfo: (paramDecl: any) => IExtractedParameter | null,
|
|
22
|
+
): IExtractedParameter[] {
|
|
23
|
+
const params: IExtractedParameter[] = [];
|
|
24
|
+
for (const paramDecl of paramList.parameterDeclaration?.() ?? []) {
|
|
25
|
+
const paramInfo = extractInfo(paramDecl);
|
|
26
|
+
if (paramInfo) {
|
|
27
|
+
params.push(paramInfo);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return params;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Build extracted parameter info from intermediate values.
|
|
35
|
+
*
|
|
36
|
+
* @param declarator The declarator context
|
|
37
|
+
* @param baseType The base type string
|
|
38
|
+
* @param isConst Whether the parameter is const
|
|
39
|
+
* @param isPointer Whether the parameter is a pointer
|
|
40
|
+
* @param isArray Whether the parameter is an array
|
|
41
|
+
* @param extractName Function to extract name from declarator
|
|
42
|
+
* @returns The extracted parameter info
|
|
43
|
+
*/
|
|
44
|
+
static buildParameterInfo(
|
|
45
|
+
declarator: any,
|
|
46
|
+
baseType: string,
|
|
47
|
+
isConst: boolean,
|
|
48
|
+
isPointer: boolean,
|
|
49
|
+
isArray: boolean,
|
|
50
|
+
extractName: (decl: any) => string | null,
|
|
51
|
+
): IExtractedParameter {
|
|
52
|
+
// Append * to type if pointer
|
|
53
|
+
const finalType = isPointer ? baseType + "*" : baseType;
|
|
54
|
+
|
|
55
|
+
// Get parameter name (may be empty for abstract declarators)
|
|
56
|
+
let paramName = "";
|
|
57
|
+
if (declarator) {
|
|
58
|
+
const name = extractName(declarator);
|
|
59
|
+
if (name) {
|
|
60
|
+
paramName = name;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
name: paramName,
|
|
66
|
+
type: finalType,
|
|
67
|
+
isConst,
|
|
68
|
+
isArray,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default ParameterExtractorUtils;
|