Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "Function.h" | ||
4 | |||
5 | #include "symboltablebuilder/SymbolTableBuilder.h" | ||
6 | |||
7 | #include <ast/ASTBuilder.h> | ||
8 | #include <ast/ASTNodes.h> | ||
9 | #include <irgenerator/NameMangling.h> | ||
10 | |||
11 | namespace spice::compiler { | ||
12 | |||
13 | /** | ||
14 | * Retrieve the parameter types of the current function | ||
15 | * | ||
16 | * @return Vector of parameter types | ||
17 | */ | ||
18 | 43408 | QualTypeList Function::getParamTypes() const { | |
19 | 43408 | QualTypeList newParamTypes; | |
20 |
2/2✓ Branch 0 (8→4) taken 64115 times.
✓ Branch 1 (8→9) taken 43408 times.
|
107523 | for (const auto &[qualType, isOptional] : paramList) |
21 |
1/2✓ Branch 0 (5→6) taken 64115 times.
✗ Branch 1 (5→11) not taken.
|
64115 | newParamTypes.push_back(qualType); |
22 | 43408 | return newParamTypes; | |
23 | ✗ | } | |
24 | |||
25 | /** | ||
26 | * Get a string representation of the current function. | ||
27 | * | ||
28 | * Example: | ||
29 | * string Vector<double>.setData<double>(double) | ||
30 | * | ||
31 | * @param withThisType Include 'this' type in signature | ||
32 | * @param ignorePublic Not include public modifiers in signature | ||
33 | * @return String representation as function signature | ||
34 | */ | ||
35 | 36886 | std::string Function::getSignature(bool withThisType /*=true*/, bool ignorePublic /*=false*/) const { | |
36 | 36886 | QualTypeList concreteTemplateTypes; | |
37 |
1/2✓ Branch 0 (3→4) taken 36886 times.
✗ Branch 1 (3→31) not taken.
|
36886 | concreteTemplateTypes.reserve(templateTypes.size()); |
38 |
2/2✓ Branch 0 (24→6) taken 17311 times.
✓ Branch 1 (24→25) taken 36886 times.
|
54197 | for (const GenericType &genericType : templateTypes) { |
39 |
6/8✓ Branch 0 (7→8) taken 17311 times.
✗ Branch 1 (7→30) not taken.
✓ Branch 2 (8→9) taken 17311 times.
✗ Branch 3 (8→12) not taken.
✓ Branch 4 (10→11) taken 8169 times.
✓ Branch 5 (10→12) taken 9142 times.
✓ Branch 6 (13→14) taken 8169 times.
✓ Branch 7 (13→21) taken 9142 times.
|
17311 | if (genericType.is(TY_GENERIC) && !typeMapping.empty()) { |
40 |
3/6✓ Branch 0 (14→15) taken 8169 times.
✗ Branch 1 (14→30) not taken.
✓ Branch 2 (15→16) taken 8169 times.
✗ Branch 3 (15→30) not taken.
✗ Branch 4 (16→17) not taken.
✓ Branch 5 (16→18) taken 8169 times.
|
8169 | assert(typeMapping.contains(genericType.getSubType())); |
41 |
3/6✓ Branch 0 (18→19) taken 8169 times.
✗ Branch 1 (18→30) not taken.
✓ Branch 2 (19→20) taken 8169 times.
✗ Branch 3 (19→30) not taken.
✓ Branch 4 (20→22) taken 8169 times.
✗ Branch 5 (20→30) not taken.
|
8169 | concreteTemplateTypes.push_back(typeMapping.at(genericType.getSubType())); |
42 | } else { | ||
43 |
1/2✓ Branch 0 (21→22) taken 9142 times.
✗ Branch 1 (21→30) not taken.
|
9142 | concreteTemplateTypes.push_back(genericType); |
44 | } | ||
45 | } | ||
46 | |||
47 |
1/2✓ Branch 0 (25→26) taken 36886 times.
✗ Branch 1 (25→31) not taken.
|
73772 | return Function::getSignature(name, thisType, returnType, paramList, concreteTemplateTypes, withThisType, ignorePublic); |
48 | 36886 | } | |
49 | |||
50 | /** | ||
51 | * Get a string representation of the current function. | ||
52 | * | ||
53 | * @param name Function name | ||
54 | * @param thisType This symbol type | ||
55 | * @param returnType Return symbol type | ||
56 | * @param paramList Param symbol types | ||
57 | * @param concreteTemplateTypes Concrete template symbol types | ||
58 | * @param withReturnType Include return type in signature | ||
59 | * @param withThisType Include 'this' type in signature | ||
60 | * @param ignorePublic Not include public modifiers in signature | ||
61 | * @return Function signature | ||
62 | */ | ||
63 | 36898 | std::string Function::getSignature(const std::string &name, const QualType &thisType, const QualType &returnType, | |
64 | const ParamList ¶mList, const QualTypeList &concreteTemplateTypes, | ||
65 | bool withReturnType /*=true*/, bool withThisType /*=true*/, bool ignorePublic /*=false*/) { | ||
66 |
1/2✓ Branch 0 (2→3) taken 36898 times.
✗ Branch 1 (2→81) not taken.
|
36898 | std::stringstream signature; |
67 | |||
68 | // Build return type string | ||
69 |
2/2✓ Branch 0 (3→4) taken 14601 times.
✓ Branch 1 (3→10) taken 22297 times.
|
36898 | if (withReturnType) { |
70 |
3/4✓ Branch 0 (4→5) taken 14601 times.
✗ Branch 1 (4→79) not taken.
✓ Branch 2 (5→6) taken 5442 times.
✓ Branch 3 (5→7) taken 9159 times.
|
14601 | if (returnType.is(TY_DYN)) { |
71 |
1/2✓ Branch 0 (6→10) taken 5442 times.
✗ Branch 1 (6→79) not taken.
|
5442 | signature << "p "; |
72 | } else { | ||
73 |
1/2✓ Branch 0 (7→8) taken 9159 times.
✗ Branch 1 (7→79) not taken.
|
9159 | signature << "f<"; |
74 |
1/2✓ Branch 0 (8→9) taken 9159 times.
✗ Branch 1 (8→79) not taken.
|
9159 | returnType.getName(signature, false, ignorePublic); |
75 |
1/2✓ Branch 0 (9→10) taken 9159 times.
✗ Branch 1 (9→79) not taken.
|
9159 | signature << "> "; |
76 | } | ||
77 | } | ||
78 | |||
79 | // Build this type string | ||
80 |
6/8✓ Branch 0 (10→11) taken 36898 times.
✗ Branch 1 (10→14) not taken.
✓ Branch 2 (11→12) taken 36898 times.
✗ Branch 3 (11→79) not taken.
✓ Branch 4 (12→13) taken 23340 times.
✓ Branch 5 (12→14) taken 13558 times.
✓ Branch 6 (15→16) taken 23340 times.
✓ Branch 7 (15→35) taken 13558 times.
|
36898 | if (withThisType && !thisType.is(TY_DYN)) { |
81 |
3/6✓ Branch 0 (16→17) taken 23340 times.
✗ Branch 1 (16→69) not taken.
✓ Branch 2 (17→18) taken 23340 times.
✗ Branch 3 (17→69) not taken.
✓ Branch 4 (18→19) taken 23340 times.
✗ Branch 5 (18→69) not taken.
|
23340 | signature << thisType.getBase().getSubType(); |
82 |
1/2✓ Branch 0 (19→20) taken 23340 times.
✗ Branch 1 (19→79) not taken.
|
23340 | const QualTypeList &thisTemplateTypes = thisType.getTemplateTypes(); |
83 |
2/2✓ Branch 0 (21→22) taken 9369 times.
✓ Branch 1 (21→34) taken 13971 times.
|
23340 | if (!thisTemplateTypes.empty()) { |
84 |
1/2✓ Branch 0 (22→23) taken 9369 times.
✗ Branch 1 (22→79) not taken.
|
9369 | signature << "<"; |
85 |
2/2✓ Branch 0 (32→24) taken 11578 times.
✓ Branch 1 (32→33) taken 9369 times.
|
20947 | for (size_t i = 0; i < thisTemplateTypes.size(); i++) { |
86 |
2/2✓ Branch 0 (24→25) taken 2209 times.
✓ Branch 1 (24→26) taken 9369 times.
|
11578 | if (i > 0) |
87 |
1/2✓ Branch 0 (25→26) taken 2209 times.
✗ Branch 1 (25→79) not taken.
|
2209 | signature << ","; |
88 |
3/6✓ Branch 0 (26→27) taken 11578 times.
✗ Branch 1 (26→72) not taken.
✓ Branch 2 (27→28) taken 11578 times.
✗ Branch 3 (27→72) not taken.
✓ Branch 4 (28→29) taken 11578 times.
✗ Branch 5 (28→70) not taken.
|
11578 | signature << thisTemplateTypes.at(i).getName(false, ignorePublic); |
89 | } | ||
90 |
1/2✓ Branch 0 (33→34) taken 9369 times.
✗ Branch 1 (33→79) not taken.
|
9369 | signature << ">"; |
91 | } | ||
92 |
1/2✓ Branch 0 (34→35) taken 23340 times.
✗ Branch 1 (34→79) not taken.
|
23340 | signature << MEMBER_ACCESS_TOKEN; |
93 | } | ||
94 | |||
95 | // Name | ||
96 |
1/2✓ Branch 0 (35→36) taken 36898 times.
✗ Branch 1 (35→79) not taken.
|
36898 | signature << name; |
97 | |||
98 | // Build template type string | ||
99 |
2/2✓ Branch 0 (37→38) taken 14500 times.
✓ Branch 1 (37→50) taken 22398 times.
|
36898 | if (!concreteTemplateTypes.empty()) { |
100 |
1/2✓ Branch 0 (38→39) taken 14500 times.
✗ Branch 1 (38→79) not taken.
|
14500 | signature << "<"; |
101 |
2/2✓ Branch 0 (48→40) taken 17311 times.
✓ Branch 1 (48→49) taken 14500 times.
|
31811 | for (size_t i = 0; i < concreteTemplateTypes.size(); i++) { |
102 |
2/2✓ Branch 0 (40→41) taken 2811 times.
✓ Branch 1 (40→42) taken 14500 times.
|
17311 | if (i > 0) |
103 |
1/2✓ Branch 0 (41→42) taken 2811 times.
✗ Branch 1 (41→79) not taken.
|
2811 | signature << ","; |
104 |
3/6✓ Branch 0 (42→43) taken 17311 times.
✗ Branch 1 (42→75) not taken.
✓ Branch 2 (43→44) taken 17311 times.
✗ Branch 3 (43→75) not taken.
✓ Branch 4 (44→45) taken 17311 times.
✗ Branch 5 (44→73) not taken.
|
17311 | signature << concreteTemplateTypes.at(i).getName(false, ignorePublic); |
105 | } | ||
106 |
1/2✓ Branch 0 (49→50) taken 14500 times.
✗ Branch 1 (49→79) not taken.
|
14500 | signature << ">"; |
107 | } | ||
108 | |||
109 | // Parameter type string | ||
110 |
1/2✓ Branch 0 (50→51) taken 36898 times.
✗ Branch 1 (50→79) not taken.
|
36898 | signature << "("; |
111 |
2/2✓ Branch 0 (62→52) taken 40074 times.
✓ Branch 1 (62→63) taken 36898 times.
|
76972 | for (size_t i = 0; i < paramList.size(); i++) { |
112 |
1/2✓ Branch 0 (52→53) taken 40074 times.
✗ Branch 1 (52→79) not taken.
|
40074 | const auto &[qualType, isOptional] = paramList.at(i); |
113 |
2/2✓ Branch 0 (53→54) taken 13534 times.
✓ Branch 1 (53→55) taken 26540 times.
|
40074 | if (i > 0) |
114 |
1/2✓ Branch 0 (54→55) taken 13534 times.
✗ Branch 1 (54→79) not taken.
|
13534 | signature << ","; |
115 |
2/4✓ Branch 0 (55→56) taken 40074 times.
✗ Branch 1 (55→78) not taken.
✓ Branch 2 (56→57) taken 40074 times.
✗ Branch 3 (56→76) not taken.
|
40074 | signature << qualType.getName(false, ignorePublic); |
116 |
1/2✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→60) taken 40074 times.
|
40074 | if (isOptional) |
117 | ✗ | signature << "?"; | |
118 | } | ||
119 |
1/2✓ Branch 0 (63→64) taken 36898 times.
✗ Branch 1 (63→79) not taken.
|
36898 | signature << ")"; |
120 | |||
121 |
1/2✓ Branch 0 (64→65) taken 36898 times.
✗ Branch 1 (64→79) not taken.
|
73796 | return signature.str(); |
122 | 36898 | } | |
123 | |||
124 | 23811 | std::string Function::getMangledName() const { | |
125 | // Use predefined mangled name if available | ||
126 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 23810 times.
|
23811 | if (!predefinedMangledName.empty()) |
127 | 1 | return predefinedMangledName; | |
128 | // Use function name if mangling is disabled | ||
129 |
2/2✓ Branch 0 (5→6) taken 2567 times.
✓ Branch 1 (5→7) taken 21243 times.
|
23810 | if (!mangleFunctionName) |
130 | 2567 | return name; | |
131 | // Use normal name mangling | ||
132 | 21243 | return NameMangling::mangleFunction(*this); | |
133 | } | ||
134 | |||
135 | 17904 | std::string Function::getSymbolTableEntryName(const std::string &functionName, const CodeLoc &codeLoc) { | |
136 |
3/6✓ Branch 0 (2→3) taken 17904 times.
✗ Branch 1 (2→15) not taken.
✓ Branch 2 (3→4) taken 17904 times.
✗ Branch 3 (3→12) not taken.
✓ Branch 4 (4→5) taken 17904 times.
✗ Branch 5 (4→10) not taken.
|
17904 | return functionName + ":" + codeLoc.toString(); |
137 | } | ||
138 | |||
139 | 44 | std::string Function::getSymbolTableEntryNameDefaultCtor(const CodeLoc &structCodeLoc) { | |
140 |
5/10✓ Branch 0 (2→3) taken 44 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 44 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 44 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 44 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 44 times.
✗ Branch 9 (8→17) not taken.
|
132 | return "default_" + std::string(CTOR_FUNCTION_NAME) + ":" + structCodeLoc.toString(); |
141 | } | ||
142 | |||
143 | 161 | std::string Function::getSymbolTableEntryNameDefaultCopyCtor(const CodeLoc &structCodeLoc) { | |
144 |
5/10✓ Branch 0 (2→3) taken 161 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 161 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 161 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 161 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 161 times.
✗ Branch 9 (8→17) not taken.
|
483 | return "default_copy" + std::string(CTOR_FUNCTION_NAME) + ":" + structCodeLoc.toString(); |
145 | } | ||
146 | |||
147 | 75 | std::string Function::getSymbolTableEntryNameDefaultDtor(const CodeLoc &structCodeLoc) { | |
148 |
5/10✓ Branch 0 (2→3) taken 75 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 75 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 75 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 75 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 75 times.
✗ Branch 9 (8→17) not taken.
|
225 | return "default_" + std::string(DTOR_FUNCTION_NAME) + ":" + structCodeLoc.toString(); |
149 | } | ||
150 | |||
151 | /** | ||
152 | * Checks if a function contains optional parameters. | ||
153 | * This would imply that the function is not substantiated by its optional parameters yet. | ||
154 | * | ||
155 | * @return Substantiated params or not | ||
156 | */ | ||
157 | 673800 | bool Function::hasSubstantiatedParams() const { | |
158 | 1688898 | return std::ranges::none_of(paramList, [](const Param ¶m) { return param.isOptional; }); | |
159 | } | ||
160 | |||
161 | /** | ||
162 | * Checks if a function contains template types. | ||
163 | * This would imply that the function is not substantiated by its generic types yet. | ||
164 | * | ||
165 | * @return Substantiated generics or not | ||
166 | */ | ||
167 | 75328 | bool Function::hasSubstantiatedGenerics() const { | |
168 | 33669 | const auto predicate = [this](const GenericType &genericType) { return typeMapping.contains(genericType.getSubType()); }; | |
169 |
1/2✓ Branch 0 (2→3) taken 75328 times.
✗ Branch 1 (2→6) not taken.
|
150656 | return std::ranges::all_of(templateTypes, predicate); |
170 | } | ||
171 | |||
172 | /** | ||
173 | * Checks if a function contains optional parameters or has generic types present. | ||
174 | * This would imply that the function is not fully substantiated yet. | ||
175 | * | ||
176 | * @return Fully substantiated or not | ||
177 | */ | ||
178 |
3/4✓ Branch 0 (3→4) taken 75328 times.
✗ Branch 1 (3→7) not taken.
✓ Branch 2 (5→6) taken 55206 times.
✓ Branch 3 (5→7) taken 20122 times.
|
75328 | bool Function::isFullySubstantiated() const { return hasSubstantiatedParams() && hasSubstantiatedGenerics(); } |
179 | |||
180 | /** | ||
181 | * Returns, if this function is a substantiation of a generic one. | ||
182 | * | ||
183 | * @return Generic substantiation or not | ||
184 | */ | ||
185 | 544765 | bool Function::isGenericSubstantiation() const { return genericPreset != nullptr; } | |
186 | |||
187 | /** | ||
188 | * Retrieve the declaration code location of this function | ||
189 | * | ||
190 | * @return Declaration code location | ||
191 | */ | ||
192 | 3168 | const CodeLoc &Function::getDeclCodeLoc() const { return declNode->codeLoc; } | |
193 | |||
194 | } // namespace spice::compiler | ||
195 |