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,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeResolver - Converts string type representations to TType.
|
|
3
|
+
*
|
|
4
|
+
* This utility handles the conversion from the old string-based type system
|
|
5
|
+
* (e.g., "u32", "string<32>", "u8[10]") to the new TType discriminated union.
|
|
6
|
+
*/
|
|
7
|
+
import type TType from "../transpiler/types/TType";
|
|
8
|
+
import TTypeUtils from "./TTypeUtils";
|
|
9
|
+
import PrimitiveKindUtils from "./PrimitiveKindUtils";
|
|
10
|
+
|
|
11
|
+
// Regex patterns for type parsing
|
|
12
|
+
const STRING_TYPE_PATTERN = /^string\s*<\s*(\d+)\s*>$/;
|
|
13
|
+
const ARRAY_TYPE_PATTERN = /^(.+?)(\[\s*[^\]]+\s*\])+$/;
|
|
14
|
+
const ARRAY_DIMENSION_PATTERN = /\[\s*([^\]]+)\s*\]/g;
|
|
15
|
+
const EXTERNAL_TYPE_PATTERN = /[:<>]/;
|
|
16
|
+
const ENUM_PREFIX_PATTERN = /^E[A-Z]/;
|
|
17
|
+
|
|
18
|
+
class TypeResolver {
|
|
19
|
+
/**
|
|
20
|
+
* Parse a string type representation and return the corresponding TType.
|
|
21
|
+
*
|
|
22
|
+
* @param typeString - String representation of a type (e.g., "u32", "string<32>", "u8[10]")
|
|
23
|
+
* @returns The corresponding TType object
|
|
24
|
+
* @throws Error if the type string is empty or invalid
|
|
25
|
+
*/
|
|
26
|
+
static resolve(typeString: string): TType {
|
|
27
|
+
const trimmed = typeString.trim();
|
|
28
|
+
|
|
29
|
+
if (trimmed === "") {
|
|
30
|
+
throw new Error("Cannot resolve empty type string");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check for string type: string<N>
|
|
34
|
+
const stringMatch = STRING_TYPE_PATTERN.exec(trimmed);
|
|
35
|
+
if (stringMatch) {
|
|
36
|
+
const capacity = Number.parseInt(stringMatch[1], 10);
|
|
37
|
+
return TTypeUtils.createString(capacity);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check for array type: baseType[dim1][dim2]...
|
|
41
|
+
const arrayMatch = ARRAY_TYPE_PATTERN.exec(trimmed);
|
|
42
|
+
if (arrayMatch) {
|
|
43
|
+
return TypeResolver.parseArrayType(trimmed, arrayMatch[1]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Check for external type (contains :: or < > for templates)
|
|
47
|
+
if (EXTERNAL_TYPE_PATTERN.test(trimmed)) {
|
|
48
|
+
return TTypeUtils.createExternal(trimmed);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Check for primitive type
|
|
52
|
+
if (PrimitiveKindUtils.isPrimitive(trimmed)) {
|
|
53
|
+
return TTypeUtils.createPrimitive(trimmed);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Check for enum (E prefix convention)
|
|
57
|
+
// LIMITATION: This heuristic assumes enums start with 'E' followed by uppercase.
|
|
58
|
+
// It may misclassify: enums without 'E' prefix, or structs named like 'EFoo'.
|
|
59
|
+
// For accurate resolution, use SymbolRegistry to check against known types.
|
|
60
|
+
if (ENUM_PREFIX_PATTERN.test(trimmed)) {
|
|
61
|
+
return TTypeUtils.createEnum(trimmed);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Default to struct type
|
|
65
|
+
return TTypeUtils.createStruct(trimmed);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Parse an array type and return the TType.
|
|
70
|
+
*/
|
|
71
|
+
private static parseArrayType(fullType: string, baseTypeStr: string): TType {
|
|
72
|
+
// Extract all dimensions from the full type string
|
|
73
|
+
const dimensions: (number | string)[] = [];
|
|
74
|
+
let match: RegExpExecArray | null = null;
|
|
75
|
+
|
|
76
|
+
// Reset lastIndex for global regex
|
|
77
|
+
ARRAY_DIMENSION_PATTERN.lastIndex = 0;
|
|
78
|
+
|
|
79
|
+
while ((match = ARRAY_DIMENSION_PATTERN.exec(fullType)) !== null) {
|
|
80
|
+
const dimStr = match[1].trim();
|
|
81
|
+
const dimNum = Number.parseInt(dimStr, 10);
|
|
82
|
+
|
|
83
|
+
if (Number.isNaN(dimNum)) {
|
|
84
|
+
// String dimension (C macro)
|
|
85
|
+
dimensions.push(dimStr);
|
|
86
|
+
} else {
|
|
87
|
+
dimensions.push(dimNum);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Resolve the base type (recursively handles string<N> as base type)
|
|
92
|
+
const elementType = TypeResolver.resolve(baseTypeStr.trim());
|
|
93
|
+
|
|
94
|
+
return TTypeUtils.createArray(elementType, dimensions);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Convert a TType back to its string representation.
|
|
99
|
+
*
|
|
100
|
+
* This is useful for round-trip compatibility and debugging.
|
|
101
|
+
*
|
|
102
|
+
* @param type - The TType object to convert
|
|
103
|
+
* @returns String representation of the type
|
|
104
|
+
*/
|
|
105
|
+
static getTypeName(type: TType): string {
|
|
106
|
+
switch (type.kind) {
|
|
107
|
+
case "primitive":
|
|
108
|
+
return type.primitive;
|
|
109
|
+
case "string":
|
|
110
|
+
return `string<${type.capacity}>`;
|
|
111
|
+
case "struct":
|
|
112
|
+
return type.name;
|
|
113
|
+
case "enum":
|
|
114
|
+
return type.name;
|
|
115
|
+
case "bitmap":
|
|
116
|
+
return type.name;
|
|
117
|
+
case "callback":
|
|
118
|
+
return type.name;
|
|
119
|
+
case "register":
|
|
120
|
+
return type.name;
|
|
121
|
+
case "external":
|
|
122
|
+
return type.name;
|
|
123
|
+
case "array": {
|
|
124
|
+
const elementName = TypeResolver.getTypeName(type.elementType);
|
|
125
|
+
const dims = type.dimensions.map((d) => `[${d}]`).join("");
|
|
126
|
+
return `${elementName}${dims}`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export default TypeResolver;
|
|
@@ -6,29 +6,99 @@ import { describe, it, expect, beforeEach } from "vitest";
|
|
|
6
6
|
import CppNamespaceUtils from "../CppNamespaceUtils";
|
|
7
7
|
import SymbolTable from "../../transpiler/logic/symbols/SymbolTable";
|
|
8
8
|
import ESourceLanguage from "../types/ESourceLanguage";
|
|
9
|
-
import
|
|
10
|
-
import
|
|
9
|
+
import type TCppSymbol from "../../transpiler/types/symbols/cpp/TCppSymbol";
|
|
10
|
+
import type TCSymbol from "../../transpiler/types/symbols/c/TCSymbol";
|
|
11
|
+
import type IScopeSymbol from "../../transpiler/types/symbols/IScopeSymbol";
|
|
11
12
|
|
|
12
13
|
describe("CppNamespaceUtils", () => {
|
|
13
14
|
let symbolTable: SymbolTable;
|
|
14
15
|
|
|
15
|
-
// Helper to create a
|
|
16
|
-
function
|
|
17
|
-
name: string,
|
|
18
|
-
kind: ESymbolKind,
|
|
19
|
-
sourceLanguage: ESourceLanguage,
|
|
20
|
-
sourceFile: string,
|
|
21
|
-
): ISymbol {
|
|
16
|
+
// Helper to create a C++ namespace symbol
|
|
17
|
+
function makeCppNamespace(name: string, sourceFile: string): TCppSymbol {
|
|
22
18
|
return {
|
|
19
|
+
kind: "namespace",
|
|
23
20
|
name,
|
|
24
|
-
kind,
|
|
25
|
-
sourceLanguage,
|
|
26
21
|
sourceFile,
|
|
27
22
|
sourceLine: 1,
|
|
23
|
+
sourceLanguage: ESourceLanguage.Cpp,
|
|
28
24
|
isExported: false,
|
|
29
25
|
};
|
|
30
26
|
}
|
|
31
27
|
|
|
28
|
+
// Helper to create a C++ class symbol
|
|
29
|
+
function makeCppClass(name: string, sourceFile: string): TCppSymbol {
|
|
30
|
+
return {
|
|
31
|
+
kind: "class",
|
|
32
|
+
name,
|
|
33
|
+
sourceFile,
|
|
34
|
+
sourceLine: 1,
|
|
35
|
+
sourceLanguage: ESourceLanguage.Cpp,
|
|
36
|
+
isExported: false,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Helper to create a C++ enum symbol
|
|
41
|
+
function makeCppEnum(name: string, sourceFile: string): TCppSymbol {
|
|
42
|
+
return {
|
|
43
|
+
kind: "enum",
|
|
44
|
+
name,
|
|
45
|
+
sourceFile,
|
|
46
|
+
sourceLine: 1,
|
|
47
|
+
sourceLanguage: ESourceLanguage.Cpp,
|
|
48
|
+
isExported: false,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Helper to create a C++ function symbol
|
|
53
|
+
function makeCppFunction(name: string, sourceFile: string): TCppSymbol {
|
|
54
|
+
return {
|
|
55
|
+
kind: "function",
|
|
56
|
+
name,
|
|
57
|
+
sourceFile,
|
|
58
|
+
sourceLine: 1,
|
|
59
|
+
sourceLanguage: ESourceLanguage.Cpp,
|
|
60
|
+
isExported: false,
|
|
61
|
+
type: "void",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Helper to create a C struct symbol
|
|
66
|
+
function makeCStruct(name: string, sourceFile: string): TCSymbol {
|
|
67
|
+
return {
|
|
68
|
+
kind: "struct",
|
|
69
|
+
name,
|
|
70
|
+
sourceFile,
|
|
71
|
+
sourceLine: 1,
|
|
72
|
+
sourceLanguage: ESourceLanguage.C,
|
|
73
|
+
isExported: false,
|
|
74
|
+
isUnion: false,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Helper to create a C-Next scope symbol (minimal)
|
|
79
|
+
function makeCNextScope(name: string, sourceFile: string): IScopeSymbol {
|
|
80
|
+
const scope: IScopeSymbol = {
|
|
81
|
+
kind: "scope",
|
|
82
|
+
name,
|
|
83
|
+
sourceFile,
|
|
84
|
+
sourceLine: 1,
|
|
85
|
+
sourceLanguage: ESourceLanguage.CNext,
|
|
86
|
+
isExported: false,
|
|
87
|
+
members: [],
|
|
88
|
+
functions: [],
|
|
89
|
+
variables: [],
|
|
90
|
+
memberVisibility: new Map(),
|
|
91
|
+
// Self-referencing for global scope
|
|
92
|
+
get parent(): IScopeSymbol {
|
|
93
|
+
return scope;
|
|
94
|
+
},
|
|
95
|
+
get scope(): IScopeSymbol {
|
|
96
|
+
return scope;
|
|
97
|
+
},
|
|
98
|
+
} as IScopeSymbol;
|
|
99
|
+
return scope;
|
|
100
|
+
}
|
|
101
|
+
|
|
32
102
|
beforeEach(() => {
|
|
33
103
|
symbolTable = new SymbolTable();
|
|
34
104
|
});
|
|
@@ -51,84 +121,42 @@ describe("CppNamespaceUtils", () => {
|
|
|
51
121
|
});
|
|
52
122
|
|
|
53
123
|
it("should return true for C++ namespace", () => {
|
|
54
|
-
symbolTable.
|
|
55
|
-
makeSymbol(
|
|
56
|
-
"SeaDash",
|
|
57
|
-
ESymbolKind.Namespace,
|
|
58
|
-
ESourceLanguage.Cpp,
|
|
59
|
-
"SeaDash.hpp",
|
|
60
|
-
),
|
|
61
|
-
);
|
|
124
|
+
symbolTable.addCppSymbol(makeCppNamespace("SeaDash", "SeaDash.hpp"));
|
|
62
125
|
expect(CppNamespaceUtils.isCppNamespace("SeaDash", symbolTable)).toBe(
|
|
63
126
|
true,
|
|
64
127
|
);
|
|
65
128
|
});
|
|
66
129
|
|
|
67
130
|
it("should return true for C++ class", () => {
|
|
68
|
-
symbolTable.
|
|
69
|
-
makeSymbol(
|
|
70
|
-
"MyClass",
|
|
71
|
-
ESymbolKind.Class,
|
|
72
|
-
ESourceLanguage.Cpp,
|
|
73
|
-
"MyClass.hpp",
|
|
74
|
-
),
|
|
75
|
-
);
|
|
131
|
+
symbolTable.addCppSymbol(makeCppClass("MyClass", "MyClass.hpp"));
|
|
76
132
|
expect(CppNamespaceUtils.isCppNamespace("MyClass", symbolTable)).toBe(
|
|
77
133
|
true,
|
|
78
134
|
);
|
|
79
135
|
});
|
|
80
136
|
|
|
81
137
|
it("should return true for C++ enum (scoped enum)", () => {
|
|
82
|
-
symbolTable.
|
|
83
|
-
makeSymbol(
|
|
84
|
-
"MyEnum",
|
|
85
|
-
ESymbolKind.Enum,
|
|
86
|
-
ESourceLanguage.Cpp,
|
|
87
|
-
"MyEnum.hpp",
|
|
88
|
-
),
|
|
89
|
-
);
|
|
138
|
+
symbolTable.addCppSymbol(makeCppEnum("MyEnum", "MyEnum.hpp"));
|
|
90
139
|
expect(CppNamespaceUtils.isCppNamespace("MyEnum", symbolTable)).toBe(
|
|
91
140
|
true,
|
|
92
141
|
);
|
|
93
142
|
});
|
|
94
143
|
|
|
95
144
|
it("should return false for C-Next namespace (scope)", () => {
|
|
96
|
-
symbolTable.
|
|
97
|
-
makeSymbol(
|
|
98
|
-
"MyScope",
|
|
99
|
-
ESymbolKind.Namespace,
|
|
100
|
-
ESourceLanguage.CNext,
|
|
101
|
-
"MyScope.cnx",
|
|
102
|
-
),
|
|
103
|
-
);
|
|
145
|
+
symbolTable.addTSymbol(makeCNextScope("MyScope", "MyScope.cnx"));
|
|
104
146
|
expect(CppNamespaceUtils.isCppNamespace("MyScope", symbolTable)).toBe(
|
|
105
147
|
false,
|
|
106
148
|
);
|
|
107
149
|
});
|
|
108
150
|
|
|
109
151
|
it("should return false for C struct", () => {
|
|
110
|
-
symbolTable.
|
|
111
|
-
makeSymbol(
|
|
112
|
-
"MyStruct",
|
|
113
|
-
ESymbolKind.Struct,
|
|
114
|
-
ESourceLanguage.C,
|
|
115
|
-
"MyStruct.h",
|
|
116
|
-
),
|
|
117
|
-
);
|
|
152
|
+
symbolTable.addCSymbol(makeCStruct("MyStruct", "MyStruct.h"));
|
|
118
153
|
expect(CppNamespaceUtils.isCppNamespace("MyStruct", symbolTable)).toBe(
|
|
119
154
|
false,
|
|
120
155
|
);
|
|
121
156
|
});
|
|
122
157
|
|
|
123
158
|
it("should return false for C++ function", () => {
|
|
124
|
-
symbolTable.
|
|
125
|
-
makeSymbol(
|
|
126
|
-
"myFunction",
|
|
127
|
-
ESymbolKind.Function,
|
|
128
|
-
ESourceLanguage.Cpp,
|
|
129
|
-
"funcs.hpp",
|
|
130
|
-
),
|
|
131
|
-
);
|
|
159
|
+
symbolTable.addCppSymbol(makeCppFunction("myFunction", "funcs.hpp"));
|
|
132
160
|
expect(CppNamespaceUtils.isCppNamespace("myFunction", symbolTable)).toBe(
|
|
133
161
|
false,
|
|
134
162
|
);
|
|
@@ -162,14 +190,7 @@ describe("CppNamespaceUtils", () => {
|
|
|
162
190
|
});
|
|
163
191
|
|
|
164
192
|
it("should return true for underscore type with C++ namespace prefix", () => {
|
|
165
|
-
symbolTable.
|
|
166
|
-
makeSymbol(
|
|
167
|
-
"SeaDash",
|
|
168
|
-
ESymbolKind.Namespace,
|
|
169
|
-
ESourceLanguage.Cpp,
|
|
170
|
-
"SeaDash.hpp",
|
|
171
|
-
),
|
|
172
|
-
);
|
|
193
|
+
symbolTable.addCppSymbol(makeCppNamespace("SeaDash", "SeaDash.hpp"));
|
|
173
194
|
expect(
|
|
174
195
|
CppNamespaceUtils.isCppNamespaceType(
|
|
175
196
|
"SeaDash_Parse_ParseResult",
|
|
@@ -186,14 +207,7 @@ describe("CppNamespaceUtils", () => {
|
|
|
186
207
|
});
|
|
187
208
|
|
|
188
209
|
it("should return false for C-Next scope underscore type", () => {
|
|
189
|
-
symbolTable.
|
|
190
|
-
makeSymbol(
|
|
191
|
-
"MyScope",
|
|
192
|
-
ESymbolKind.Namespace,
|
|
193
|
-
ESourceLanguage.CNext,
|
|
194
|
-
"MyScope.cnx",
|
|
195
|
-
),
|
|
196
|
-
);
|
|
210
|
+
symbolTable.addTSymbol(makeCNextScope("MyScope", "MyScope.cnx"));
|
|
197
211
|
expect(
|
|
198
212
|
CppNamespaceUtils.isCppNamespaceType("MyScope_MyType", symbolTable),
|
|
199
213
|
).toBe(false);
|
|
@@ -230,14 +244,7 @@ describe("CppNamespaceUtils", () => {
|
|
|
230
244
|
});
|
|
231
245
|
|
|
232
246
|
it("should convert underscore type with C++ namespace prefix", () => {
|
|
233
|
-
symbolTable.
|
|
234
|
-
makeSymbol(
|
|
235
|
-
"SeaDash",
|
|
236
|
-
ESymbolKind.Namespace,
|
|
237
|
-
ESourceLanguage.Cpp,
|
|
238
|
-
"SeaDash.hpp",
|
|
239
|
-
),
|
|
240
|
-
);
|
|
247
|
+
symbolTable.addCppSymbol(makeCppNamespace("SeaDash", "SeaDash.hpp"));
|
|
241
248
|
expect(
|
|
242
249
|
CppNamespaceUtils.convertToCppNamespace(
|
|
243
250
|
"SeaDash_Parse_ParseResult",
|
|
@@ -254,28 +261,14 @@ describe("CppNamespaceUtils", () => {
|
|
|
254
261
|
});
|
|
255
262
|
|
|
256
263
|
it("should NOT convert C-Next scope underscore type", () => {
|
|
257
|
-
symbolTable.
|
|
258
|
-
makeSymbol(
|
|
259
|
-
"MyScope",
|
|
260
|
-
ESymbolKind.Namespace,
|
|
261
|
-
ESourceLanguage.CNext,
|
|
262
|
-
"MyScope.cnx",
|
|
263
|
-
),
|
|
264
|
-
);
|
|
264
|
+
symbolTable.addTSymbol(makeCNextScope("MyScope", "MyScope.cnx"));
|
|
265
265
|
expect(
|
|
266
266
|
CppNamespaceUtils.convertToCppNamespace("MyScope_MyType", symbolTable),
|
|
267
267
|
).toBe("MyScope_MyType");
|
|
268
268
|
});
|
|
269
269
|
|
|
270
270
|
it("should handle deeply nested namespace", () => {
|
|
271
|
-
symbolTable.
|
|
272
|
-
makeSymbol(
|
|
273
|
-
"Lib",
|
|
274
|
-
ESymbolKind.Namespace,
|
|
275
|
-
ESourceLanguage.Cpp,
|
|
276
|
-
"Lib.hpp",
|
|
277
|
-
),
|
|
278
|
-
);
|
|
271
|
+
symbolTable.addCppSymbol(makeCppNamespace("Lib", "Lib.hpp"));
|
|
279
272
|
expect(
|
|
280
273
|
CppNamespaceUtils.convertToCppNamespace(
|
|
281
274
|
"Lib_Sub_Deep_Type",
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for IFunctionSymbol - C-Next function symbol representation
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import FunctionUtils from "../FunctionUtils";
|
|
6
|
+
import ScopeUtils from "../ScopeUtils";
|
|
7
|
+
import ParameterUtils from "../ParameterUtils";
|
|
8
|
+
import TTypeUtils from "../TTypeUtils";
|
|
9
|
+
|
|
10
|
+
describe("IFunctionSymbol", () => {
|
|
11
|
+
describe("FunctionUtils.create", () => {
|
|
12
|
+
it("creates function with bare name and scope reference", () => {
|
|
13
|
+
const global = ScopeUtils.createGlobalScope();
|
|
14
|
+
const testScope = ScopeUtils.createScope("Test", global);
|
|
15
|
+
|
|
16
|
+
const func = FunctionUtils.create({
|
|
17
|
+
name: "fillData", // Bare name, NOT "Test_fillData"
|
|
18
|
+
scope: testScope,
|
|
19
|
+
parameters: [
|
|
20
|
+
ParameterUtils.create({
|
|
21
|
+
name: "d",
|
|
22
|
+
type: TTypeUtils.createPrimitive("u32"),
|
|
23
|
+
isConst: false,
|
|
24
|
+
isArray: false,
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
28
|
+
visibility: "private",
|
|
29
|
+
body: null,
|
|
30
|
+
sourceFile: "test.cnx",
|
|
31
|
+
sourceLine: 10,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(func.kind).toBe("function");
|
|
35
|
+
expect(func.name).toBe("fillData");
|
|
36
|
+
expect(func.scope).toBe(testScope);
|
|
37
|
+
expect(func.parameters).toHaveLength(1);
|
|
38
|
+
expect(func.parameters[0].name).toBe("d");
|
|
39
|
+
expect(func.returnType.kind).toBe("primitive");
|
|
40
|
+
expect(func.visibility).toBe("private");
|
|
41
|
+
expect(func.sourceFile).toBe("test.cnx");
|
|
42
|
+
expect(func.sourceLine).toBe(10);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("creates public function in global scope", () => {
|
|
46
|
+
const global = ScopeUtils.createGlobalScope();
|
|
47
|
+
|
|
48
|
+
const func = FunctionUtils.create({
|
|
49
|
+
name: "main",
|
|
50
|
+
scope: global,
|
|
51
|
+
parameters: [],
|
|
52
|
+
returnType: TTypeUtils.createPrimitive("i32"),
|
|
53
|
+
visibility: "public",
|
|
54
|
+
body: null,
|
|
55
|
+
sourceFile: "main.cnx",
|
|
56
|
+
sourceLine: 1,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(func.scope).toBe(global);
|
|
60
|
+
expect(func.visibility).toBe("public");
|
|
61
|
+
expect(func.name).toBe("main");
|
|
62
|
+
expect(ScopeUtils.isGlobalScope(func.scope)).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("creates function with multiple parameters", () => {
|
|
66
|
+
const global = ScopeUtils.createGlobalScope();
|
|
67
|
+
|
|
68
|
+
const func = FunctionUtils.create({
|
|
69
|
+
name: "calculate",
|
|
70
|
+
scope: global,
|
|
71
|
+
parameters: [
|
|
72
|
+
ParameterUtils.create({
|
|
73
|
+
name: "a",
|
|
74
|
+
type: TTypeUtils.createPrimitive("i32"),
|
|
75
|
+
isConst: true,
|
|
76
|
+
isArray: false,
|
|
77
|
+
}),
|
|
78
|
+
ParameterUtils.create({
|
|
79
|
+
name: "b",
|
|
80
|
+
type: TTypeUtils.createPrimitive("i32"),
|
|
81
|
+
isConst: true,
|
|
82
|
+
isArray: false,
|
|
83
|
+
}),
|
|
84
|
+
ParameterUtils.create({
|
|
85
|
+
name: "result",
|
|
86
|
+
type: TTypeUtils.createPrimitive("i32"),
|
|
87
|
+
isConst: false,
|
|
88
|
+
isArray: false,
|
|
89
|
+
}),
|
|
90
|
+
],
|
|
91
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
92
|
+
visibility: "public",
|
|
93
|
+
body: null,
|
|
94
|
+
sourceFile: "calc.cnx",
|
|
95
|
+
sourceLine: 5,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(func.parameters).toHaveLength(3);
|
|
99
|
+
expect(func.parameters[0].isConst).toBe(true);
|
|
100
|
+
expect(func.parameters[1].isConst).toBe(true);
|
|
101
|
+
expect(func.parameters[2].isConst).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("creates function with struct return type", () => {
|
|
105
|
+
const global = ScopeUtils.createGlobalScope();
|
|
106
|
+
|
|
107
|
+
const func = FunctionUtils.create({
|
|
108
|
+
name: "createPoint",
|
|
109
|
+
scope: global,
|
|
110
|
+
parameters: [
|
|
111
|
+
ParameterUtils.create({
|
|
112
|
+
name: "x",
|
|
113
|
+
type: TTypeUtils.createPrimitive("i32"),
|
|
114
|
+
isConst: false,
|
|
115
|
+
isArray: false,
|
|
116
|
+
}),
|
|
117
|
+
ParameterUtils.create({
|
|
118
|
+
name: "y",
|
|
119
|
+
type: TTypeUtils.createPrimitive("i32"),
|
|
120
|
+
isConst: false,
|
|
121
|
+
isArray: false,
|
|
122
|
+
}),
|
|
123
|
+
],
|
|
124
|
+
returnType: TTypeUtils.createStruct("Point"),
|
|
125
|
+
visibility: "public",
|
|
126
|
+
body: null,
|
|
127
|
+
sourceFile: "point.cnx",
|
|
128
|
+
sourceLine: 10,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(func.returnType.kind).toBe("struct");
|
|
132
|
+
if (func.returnType.kind === "struct") {
|
|
133
|
+
expect(func.returnType.name).toBe("Point");
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("creates function with body reference", () => {
|
|
138
|
+
const global = ScopeUtils.createGlobalScope();
|
|
139
|
+
const mockBody = { type: "block", statements: [] };
|
|
140
|
+
|
|
141
|
+
const func = FunctionUtils.create({
|
|
142
|
+
name: "doSomething",
|
|
143
|
+
scope: global,
|
|
144
|
+
parameters: [],
|
|
145
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
146
|
+
visibility: "public",
|
|
147
|
+
body: mockBody,
|
|
148
|
+
sourceFile: "example.cnx",
|
|
149
|
+
sourceLine: 15,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(func.body).toBe(mockBody);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("FunctionUtils.getCMangledName", () => {
|
|
157
|
+
it("returns bare name for global scope function", () => {
|
|
158
|
+
const global = ScopeUtils.createGlobalScope();
|
|
159
|
+
|
|
160
|
+
const func = FunctionUtils.create({
|
|
161
|
+
name: "main",
|
|
162
|
+
scope: global,
|
|
163
|
+
parameters: [],
|
|
164
|
+
returnType: TTypeUtils.createPrimitive("i32"),
|
|
165
|
+
visibility: "public",
|
|
166
|
+
body: null,
|
|
167
|
+
sourceFile: "main.cnx",
|
|
168
|
+
sourceLine: 1,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
expect(FunctionUtils.getCMangledName(func)).toBe("main");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("returns scope-prefixed name for scoped function", () => {
|
|
175
|
+
const global = ScopeUtils.createGlobalScope();
|
|
176
|
+
const testScope = ScopeUtils.createScope("Test", global);
|
|
177
|
+
|
|
178
|
+
const func = FunctionUtils.create({
|
|
179
|
+
name: "fillData",
|
|
180
|
+
scope: testScope,
|
|
181
|
+
parameters: [],
|
|
182
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
183
|
+
visibility: "public",
|
|
184
|
+
body: null,
|
|
185
|
+
sourceFile: "test.cnx",
|
|
186
|
+
sourceLine: 10,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
expect(FunctionUtils.getCMangledName(func)).toBe("Test_fillData");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("returns nested scope-prefixed name for nested scope function", () => {
|
|
193
|
+
const global = ScopeUtils.createGlobalScope();
|
|
194
|
+
const outerScope = ScopeUtils.createScope("Outer", global);
|
|
195
|
+
const innerScope = ScopeUtils.createScope("Inner", outerScope);
|
|
196
|
+
|
|
197
|
+
const func = FunctionUtils.create({
|
|
198
|
+
name: "process",
|
|
199
|
+
scope: innerScope,
|
|
200
|
+
parameters: [],
|
|
201
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
202
|
+
visibility: "public",
|
|
203
|
+
body: null,
|
|
204
|
+
sourceFile: "nested.cnx",
|
|
205
|
+
sourceLine: 20,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
expect(FunctionUtils.getCMangledName(func)).toBe("Outer_Inner_process");
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("FunctionUtils.isPublic", () => {
|
|
213
|
+
it("returns true for public function", () => {
|
|
214
|
+
const global = ScopeUtils.createGlobalScope();
|
|
215
|
+
|
|
216
|
+
const func = FunctionUtils.create({
|
|
217
|
+
name: "publicFunc",
|
|
218
|
+
scope: global,
|
|
219
|
+
parameters: [],
|
|
220
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
221
|
+
visibility: "public",
|
|
222
|
+
body: null,
|
|
223
|
+
sourceFile: "test.cnx",
|
|
224
|
+
sourceLine: 1,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(FunctionUtils.isPublic(func)).toBe(true);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("returns false for private function", () => {
|
|
231
|
+
const global = ScopeUtils.createGlobalScope();
|
|
232
|
+
|
|
233
|
+
const func = FunctionUtils.create({
|
|
234
|
+
name: "privateFunc",
|
|
235
|
+
scope: global,
|
|
236
|
+
parameters: [],
|
|
237
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
238
|
+
visibility: "private",
|
|
239
|
+
body: null,
|
|
240
|
+
sourceFile: "test.cnx",
|
|
241
|
+
sourceLine: 1,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
expect(FunctionUtils.isPublic(func)).toBe(false);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe("FunctionUtils.isInGlobalScope", () => {
|
|
249
|
+
it("returns true for function in global scope", () => {
|
|
250
|
+
const global = ScopeUtils.createGlobalScope();
|
|
251
|
+
|
|
252
|
+
const func = FunctionUtils.create({
|
|
253
|
+
name: "main",
|
|
254
|
+
scope: global,
|
|
255
|
+
parameters: [],
|
|
256
|
+
returnType: TTypeUtils.createPrimitive("i32"),
|
|
257
|
+
visibility: "public",
|
|
258
|
+
body: null,
|
|
259
|
+
sourceFile: "main.cnx",
|
|
260
|
+
sourceLine: 1,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
expect(FunctionUtils.isInGlobalScope(func)).toBe(true);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("returns false for function in named scope", () => {
|
|
267
|
+
const global = ScopeUtils.createGlobalScope();
|
|
268
|
+
const testScope = ScopeUtils.createScope("Test", global);
|
|
269
|
+
|
|
270
|
+
const func = FunctionUtils.create({
|
|
271
|
+
name: "helper",
|
|
272
|
+
scope: testScope,
|
|
273
|
+
parameters: [],
|
|
274
|
+
returnType: TTypeUtils.createPrimitive("void"),
|
|
275
|
+
visibility: "private",
|
|
276
|
+
body: null,
|
|
277
|
+
sourceFile: "test.cnx",
|
|
278
|
+
sourceLine: 5,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
expect(FunctionUtils.isInGlobalScope(func)).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
});
|