GCC Code Coverage Report


Directory: ../
File: src/model/Function.cpp
Date: 2025-08-26 18:26:32
Exec Total Coverage
Lines: 74 76 97.4%
Functions: 15 15 100.0%
Branches: 104 172 60.5%

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 47253 QualTypeList Function::getParamTypes() const {
19 47253 QualTypeList newParamTypes;
20
2/2
✓ Branch 0 (8→4) taken 70255 times.
✓ Branch 1 (8→9) taken 47253 times.
117508 for (const auto &[qualType, isOptional] : paramList)
21
1/2
✓ Branch 0 (5→6) taken 70255 times.
✗ Branch 1 (5→11) not taken.
70255 newParamTypes.push_back(qualType);
22 47253 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 41939 std::string Function::getSignature(bool withThisType /*=true*/, bool ignorePublic /*=false*/) const {
36 41939 QualTypeList concreteTemplateTypes;
37
1/2
✓ Branch 0 (3→4) taken 41939 times.
✗ Branch 1 (3→31) not taken.
41939 concreteTemplateTypes.reserve(templateTypes.size());
38
2/2
✓ Branch 0 (24→6) taken 19238 times.
✓ Branch 1 (24→25) taken 41939 times.
61177 for (const GenericType &genericType : templateTypes) {
39
6/8
✓ Branch 0 (7→8) taken 19238 times.
✗ Branch 1 (7→30) not taken.
✓ Branch 2 (8→9) taken 19238 times.
✗ Branch 3 (8→12) not taken.
✓ Branch 4 (10→11) taken 8718 times.
✓ Branch 5 (10→12) taken 10520 times.
✓ Branch 6 (13→14) taken 8718 times.
✓ Branch 7 (13→21) taken 10520 times.
19238 if (genericType.is(TY_GENERIC) && !typeMapping.empty()) {
40
3/6
✓ Branch 0 (14→15) taken 8718 times.
✗ Branch 1 (14→30) not taken.
✓ Branch 2 (15→16) taken 8718 times.
✗ Branch 3 (15→30) not taken.
✗ Branch 4 (16→17) not taken.
✓ Branch 5 (16→18) taken 8718 times.
8718 assert(typeMapping.contains(genericType.getSubType()));
41
3/6
✓ Branch 0 (18→19) taken 8718 times.
✗ Branch 1 (18→30) not taken.
✓ Branch 2 (19→20) taken 8718 times.
✗ Branch 3 (19→30) not taken.
✓ Branch 4 (20→22) taken 8718 times.
✗ Branch 5 (20→30) not taken.
8718 concreteTemplateTypes.push_back(typeMapping.at(genericType.getSubType()));
42 } else {
43
1/2
✓ Branch 0 (21→22) taken 10520 times.
✗ Branch 1 (21→30) not taken.
10520 concreteTemplateTypes.push_back(genericType);
44 }
45 }
46
47
1/2
✓ Branch 0 (25→26) taken 41939 times.
✗ Branch 1 (25→31) not taken.
83878 return Function::getSignature(name, thisType, returnType, paramList, concreteTemplateTypes, withThisType, ignorePublic);
48 41939 }
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 41951 std::string Function::getSignature(const std::string &name, const QualType &thisType, const QualType &returnType,
64 const ParamList &paramList, const QualTypeList &concreteTemplateTypes,
65 bool withReturnType /*=true*/, bool withThisType /*=true*/, bool ignorePublic /*=false*/) {
66
1/2
✓ Branch 0 (2→3) taken 41951 times.
✗ Branch 1 (2→81) not taken.
41951 std::stringstream signature;
67
68 // Build return type string
69
2/2
✓ Branch 0 (3→4) taken 16570 times.
✓ Branch 1 (3→10) taken 25381 times.
41951 if (withReturnType) {
70
3/4
✓ Branch 0 (4→5) taken 16570 times.
✗ Branch 1 (4→79) not taken.
✓ Branch 2 (5→6) taken 6026 times.
✓ Branch 3 (5→7) taken 10544 times.
16570 if (returnType.is(TY_DYN)) {
71
1/2
✓ Branch 0 (6→10) taken 6026 times.
✗ Branch 1 (6→79) not taken.
6026 signature << "p ";
72 } else {
73
1/2
✓ Branch 0 (7→8) taken 10544 times.
✗ Branch 1 (7→79) not taken.
10544 signature << "f<";
74
1/2
✓ Branch 0 (8→9) taken 10544 times.
✗ Branch 1 (8→79) not taken.
10544 returnType.getName(signature, false, ignorePublic);
75
1/2
✓ Branch 0 (9→10) taken 10544 times.
✗ Branch 1 (9→79) not taken.
10544 signature << "> ";
76 }
77 }
78
79 // Build this type string
80
6/8
✓ Branch 0 (10→11) taken 41951 times.
✗ Branch 1 (10→14) not taken.
✓ Branch 2 (11→12) taken 41951 times.
✗ Branch 3 (11→79) not taken.
✓ Branch 4 (12→13) taken 24910 times.
✓ Branch 5 (12→14) taken 17041 times.
✓ Branch 6 (15→16) taken 24910 times.
✓ Branch 7 (15→35) taken 17041 times.
41951 if (withThisType && !thisType.is(TY_DYN)) {
81
3/6
✓ Branch 0 (16→17) taken 24910 times.
✗ Branch 1 (16→69) not taken.
✓ Branch 2 (17→18) taken 24910 times.
✗ Branch 3 (17→69) not taken.
✓ Branch 4 (18→19) taken 24910 times.
✗ Branch 5 (18→69) not taken.
24910 signature << thisType.getBase().getSubType();
82
1/2
✓ Branch 0 (19→20) taken 24910 times.
✗ Branch 1 (19→79) not taken.
24910 const QualTypeList &thisTemplateTypes = thisType.getTemplateTypes();
83
2/2
✓ Branch 0 (21→22) taken 9780 times.
✓ Branch 1 (21→34) taken 15130 times.
24910 if (!thisTemplateTypes.empty()) {
84
1/2
✓ Branch 0 (22→23) taken 9780 times.
✗ Branch 1 (22→79) not taken.
9780 signature << "<";
85
2/2
✓ Branch 0 (32→24) taken 12294 times.
✓ Branch 1 (32→33) taken 9780 times.
22074 for (size_t i = 0; i < thisTemplateTypes.size(); i++) {
86
2/2
✓ Branch 0 (24→25) taken 2514 times.
✓ Branch 1 (24→26) taken 9780 times.
12294 if (i > 0)
87
1/2
✓ Branch 0 (25→26) taken 2514 times.
✗ Branch 1 (25→79) not taken.
2514 signature << ",";
88
3/6
✓ Branch 0 (26→27) taken 12294 times.
✗ Branch 1 (26→72) not taken.
✓ Branch 2 (27→28) taken 12294 times.
✗ Branch 3 (27→72) not taken.
✓ Branch 4 (28→29) taken 12294 times.
✗ Branch 5 (28→70) not taken.
12294 signature << thisTemplateTypes.at(i).getName(false, ignorePublic);
89 }
90
1/2
✓ Branch 0 (33→34) taken 9780 times.
✗ Branch 1 (33→79) not taken.
9780 signature << ">";
91 }
92
1/2
✓ Branch 0 (34→35) taken 24910 times.
✗ Branch 1 (34→79) not taken.
24910 signature << MEMBER_ACCESS_TOKEN;
93 }
94
95 // Name
96
1/2
✓ Branch 0 (35→36) taken 41951 times.
✗ Branch 1 (35→79) not taken.
41951 signature << name;
97
98 // Build template type string
99
2/2
✓ Branch 0 (37→38) taken 16039 times.
✓ Branch 1 (37→50) taken 25912 times.
41951 if (!concreteTemplateTypes.empty()) {
100
1/2
✓ Branch 0 (38→39) taken 16039 times.
✗ Branch 1 (38→79) not taken.
16039 signature << "<";
101
2/2
✓ Branch 0 (48→40) taken 19238 times.
✓ Branch 1 (48→49) taken 16039 times.
35277 for (size_t i = 0; i < concreteTemplateTypes.size(); i++) {
102
2/2
✓ Branch 0 (40→41) taken 3199 times.
✓ Branch 1 (40→42) taken 16039 times.
19238 if (i > 0)
103
1/2
✓ Branch 0 (41→42) taken 3199 times.
✗ Branch 1 (41→79) not taken.
3199 signature << ",";
104
3/6
✓ Branch 0 (42→43) taken 19238 times.
✗ Branch 1 (42→75) not taken.
✓ Branch 2 (43→44) taken 19238 times.
✗ Branch 3 (43→75) not taken.
✓ Branch 4 (44→45) taken 19238 times.
✗ Branch 5 (44→73) not taken.
19238 signature << concreteTemplateTypes.at(i).getName(false, ignorePublic);
105 }
106
1/2
✓ Branch 0 (49→50) taken 16039 times.
✗ Branch 1 (49→79) not taken.
16039 signature << ">";
107 }
108
109 // Parameter type string
110
1/2
✓ Branch 0 (50→51) taken 41951 times.
✗ Branch 1 (50→79) not taken.
41951 signature << "(";
111
2/2
✓ Branch 0 (62→52) taken 46650 times.
✓ Branch 1 (62→63) taken 41951 times.
88601 for (size_t i = 0; i < paramList.size(); i++) {
112
1/2
✓ Branch 0 (52→53) taken 46650 times.
✗ Branch 1 (52→79) not taken.
46650 const auto &[qualType, isOptional] = paramList.at(i);
113
2/2
✓ Branch 0 (53→54) taken 15566 times.
✓ Branch 1 (53→55) taken 31084 times.
46650 if (i > 0)
114
1/2
✓ Branch 0 (54→55) taken 15566 times.
✗ Branch 1 (54→79) not taken.
15566 signature << ",";
115
2/4
✓ Branch 0 (55→56) taken 46650 times.
✗ Branch 1 (55→78) not taken.
✓ Branch 2 (56→57) taken 46650 times.
✗ Branch 3 (56→76) not taken.
46650 signature << qualType.getName(false, ignorePublic);
116
1/2
✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→60) taken 46650 times.
46650 if (isOptional)
117 signature << "?";
118 }
119
1/2
✓ Branch 0 (63→64) taken 41951 times.
✗ Branch 1 (63→79) not taken.
41951 signature << ")";
120
121
1/2
✓ Branch 0 (64→65) taken 41951 times.
✗ Branch 1 (64→79) not taken.
83902 return signature.str();
122 41951 }
123
124 27518 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 27517 times.
27518 if (!predefinedMangledName.empty())
127 1 return predefinedMangledName;
128 // Use function name if mangling is disabled
129
2/2
✓ Branch 0 (5→6) taken 2928 times.
✓ Branch 1 (5→7) taken 24589 times.
27517 if (!mangleFunctionName)
130 2928 return name;
131 // Use normal name mangling
132 24589 return NameMangling::mangleFunction(*this);
133 }
134
135 20825 std::string Function::getSymbolTableEntryName(const std::string &functionName, const CodeLoc &codeLoc) {
136
3/6
✓ Branch 0 (2→3) taken 20825 times.
✗ Branch 1 (2→15) not taken.
✓ Branch 2 (3→4) taken 20825 times.
✗ Branch 3 (3→12) not taken.
✓ Branch 4 (4→5) taken 20825 times.
✗ Branch 5 (4→10) not taken.
20825 return functionName + ":" + codeLoc.toString();
137 }
138
139 47 std::string Function::getSymbolTableEntryNameDefaultCtor(const CodeLoc &structCodeLoc) {
140
5/10
✓ Branch 0 (2→3) taken 47 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 47 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 47 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 47 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 47 times.
✗ Branch 9 (8→17) not taken.
141 return "default_" + std::string(CTOR_FUNCTION_NAME) + ":" + structCodeLoc.toString();
141 }
142
143 172 std::string Function::getSymbolTableEntryNameDefaultCopyCtor(const CodeLoc &structCodeLoc) {
144
5/10
✓ Branch 0 (2→3) taken 172 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 172 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 172 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 172 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 172 times.
✗ Branch 9 (8→17) not taken.
516 return "default_copy" + std::string(CTOR_FUNCTION_NAME) + ":" + structCodeLoc.toString();
145 }
146
147 83 std::string Function::getSymbolTableEntryNameDefaultDtor(const CodeLoc &structCodeLoc) {
148
5/10
✓ Branch 0 (2→3) taken 83 times.
✗ Branch 1 (2→31) not taken.
✓ Branch 2 (5→6) taken 83 times.
✗ Branch 3 (5→23) not taken.
✓ Branch 4 (6→7) taken 83 times.
✗ Branch 5 (6→21) not taken.
✓ Branch 6 (7→8) taken 83 times.
✗ Branch 7 (7→19) not taken.
✓ Branch 8 (8→9) taken 83 times.
✗ Branch 9 (8→17) not taken.
249 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 920242 bool Function::hasSubstantiatedParams() const {
158 2252248 return std::ranges::none_of(paramList, [](const Param &param) { 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 106218 bool Function::hasSubstantiatedGenerics() const {
168 48606 const auto predicate = [this](const GenericType &genericType) { return typeMapping.contains(genericType.getSubType()); };
169
1/2
✓ Branch 0 (2→3) taken 106218 times.
✗ Branch 1 (2→6) not taken.
212436 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 106218 times.
✗ Branch 1 (3→7) not taken.
✓ Branch 2 (5→6) taken 74330 times.
✓ Branch 3 (5→7) taken 31888 times.
106218 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 732886 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 3360 const CodeLoc &Function::getDeclCodeLoc() const { return declNode->codeLoc; }
193
194 } // namespace spice::compiler
195