GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 6 / 0 / 6
Functions: -% 0 / 0 / 0
Branches: 38.7% 105 / 0 / 271

src/symboltablebuilder/Scope.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <map>
6 #include <memory>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10
11 #include <model/Function.h>
12 #include <model/Interface.h>
13 #include <model/Struct.h>
14 #include <model/Union.h>
15 #include <symboltablebuilder/SymbolTable.h>
16 #include <util/GlobalDefinitions.h>
17
18 namespace spice::compiler {
19
20 // Forward declarations
21 class FctDefNode;
22 class ProcDefNode;
23 class SourceFile;
24 class SymbolTableEntry;
25 class GenericType;
26 class CompilerWarning;
27 class Function;
28 class Struct;
29 class ASTNode;
30 using FunctionManifestationList = std::map</*mangledName=*/std::string, Function>;
31 using FunctionRegistry = std::map</*fctId=*/std::string, /*manifestationList=*/FunctionManifestationList>;
32 using StructManifestationList = std::map</*mangledName=*/std::string, Struct>;
33 using StructRegistry = std::map</*structId=*/std::string, /*manifestationList=*/StructManifestationList>;
34 using InterfaceManifestationList = std::map</*mangledName=*/std::string, Interface>;
35 using InterfaceRegistry = std::map<CodeLoc, InterfaceManifestationList>;
36 using UnionManifestationList = std::map</*mangledName=*/std::string, Union>;
37 using UnionRegistry = std::map</*unionId=*/std::string, /*manifestationList=*/UnionManifestationList>;
38
39 enum class ScopeType : uint8_t {
40 GLOBAL,
41 FUNC_PROC_BODY,
42 LAMBDA_BODY,
43 STRUCT,
44 INTERFACE,
45 UNION,
46 ENUM,
47 IF_ELSE_BODY,
48 WHILE_BODY,
49 FOR_BODY,
50 FOREACH_BODY,
51 CASE_BODY,
52 DEFAULT_BODY,
53 UNSAFE_BODY,
54 ANONYMOUS_BLOCK_BODY,
55 EXPR_BODY
56 };
57
58 /**
59 * Represents an enclosed group of instructions, which are semantically separated from other scopes.
60 * In the source code, scopes usually are written as curly braces.
61 *
62 * Following language structures use scopes:
63 * - global scope
64 * - functions/procedures
65 * - structs
66 * - enums
67 * - interfaces
68 * - thread blocks
69 * - unsafe blocks
70 * - for loops
71 * - foreach loops
72 * - while loops
73 * - if statements
74 * - anonymous scopes
75 * - conditions, and expressions that are only evaluated conditionally (e.g. the right side of '&&' and '||')
76 */
77 class Scope {
78 public:
79 // Constructors
80 Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc);
81
82 // Friend classes
83 friend class FunctionManager;
84 friend class StructManager;
85 friend class InterfaceManager;
86 friend class UnionManager;
87
88 // Public methods
89 // Scope management
90 Scope *createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc);
91 void renameChildScope(const std::string &oldName, const std::string &newName);
92 Scope *copyChildScope(const std::string &oldName, const std::string &newName);
93 std::shared_ptr<Scope> deepCopyScope();
94 [[nodiscard]] Scope *getChildScope(const std::string &scopeName) const;
95 [[nodiscard]] std::vector<SymbolTableEntry *> getVarsGoingOutOfScope();
96
97 // Generic types
98 void insertGenericType(const std::string &typeName, const GenericType &genericType);
99 GenericType *lookupGenericTypeStrict(const std::string &typeName);
100
101 // Util
102 void collectWarnings(std::vector<CompilerWarning> &warnings) const;
103 void ensureSuccessfulTypeInference() const;
104 [[nodiscard]] size_t getFieldCount() const;
105 [[nodiscard]] std::vector<const Function *> getVirtualMethods();
106 [[nodiscard]] std::vector<Struct *> getAllStructManifestationsInDeclarationOrder();
107 [[nodiscard]] std::vector<Union *> getAllUnionManifestationsInDeclarationOrder();
108 void collectManifestationFingerprint(std::stringstream &fingerprint) const;
109 [[nodiscard]] unsigned int getLoopNestingDepth() const;
110 [[nodiscard]] Scope *getFunctionScope();
111 [[nodiscard]] bool isInCaseBranch() const;
112 [[nodiscard]] bool isInAsyncScope() const;
113 [[nodiscard]] bool doesAllowUnsafeOperations() const;
114 [[nodiscard]] bool isImportedBy(const Scope *askingScope) const;
115 [[nodiscard]] nlohmann::json getSymbolTableJSON() const;
116 637804 [[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; }
117
118 // Wrapper methods for symbol table
119 ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) {
120
19/38
✓ Branch 9 → 10 taken 2502 times.
✗ Branch 9 → 97 not taken.
✓ Branch 13 → 14 taken 3970 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 1298 times.
✗ Branch 26 → 68 not taken.
✓ Branch 28 → 29 taken 606 times.
✗ Branch 28 → 92 not taken.
✓ Branch 30 → 31 taken 113 times.
✗ Branch 30 → 63 not taken.
✓ Branch 33 → 34 taken 1042 times.
✗ Branch 33 → 51 not taken.
✓ Branch 36 → 37 taken 1298 times.
✗ Branch 36 → 74 not taken.
✓ Branch 38 → 39 taken 365 times.
✗ Branch 38 → 73 not taken.
✓ Branch 43 → 44 taken 365 times.
✗ Branch 43 → 71 not taken.
✓ Branch 48 → 49 taken 34 times.
✗ Branch 48 → 82 not taken.
✓ Branch 55 → 56 taken 2502 times.
✗ Branch 55 → 86 not taken.
✓ Branch 62 → 63 taken 455 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 18532 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 20029 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 4566 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 40260 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 25183 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 40260 times.
✗ Branch 84 → 141 not taken.
✓ Branch 225 → 226 taken 3776 times.
✗ Branch 225 → 415 not taken.
317306 return symbolTable.insert(name, declNode);
121 }
122
6/12
✓ Branch 5 → 6 taken 61549 times.
✗ Branch 5 → 102 not taken.
✓ Branch 18 → 19 taken 350112 times.
✗ Branch 18 → 231 not taken.
✓ Branch 23 → 24 taken 103386 times.
✗ Branch 23 → 626 not taken.
✓ Branch 44 → 45 taken 6022 times.
✗ Branch 44 → 91 not taken.
✓ Branch 90 → 91 taken 104984 times.
✗ Branch 90 → 561 not taken.
✓ Branch 241 → 242 taken 158 times.
✗ Branch 241 → 496 not taken.
626215 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
123
40/99
✓ Branch 2 → 3 taken 209366 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 178 not taken.
✓ Branch 5 → 6 taken 152766 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 12750 times.
✗ Branch 6 → 15 not taken.
✗ Branch 6 → 84 not taken.
✗ Branch 6 → 99 not taken.
✗ Branch 6 → 110 not taken.
✓ Branch 7 → 8 taken 12073 times.
✗ Branch 7 → 52 not taken.
✗ Branch 7 → 54 not taken.
✗ Branch 7 → 76 not taken.
✗ Branch 7 → 133 not taken.
✗ Branch 7 → 139 not taken.
✗ Branch 7 → 165 not taken.
✓ Branch 9 → 10 taken 2701 times.
✗ Branch 9 → 23 not taken.
✗ Branch 9 → 53 not taken.
✓ Branch 17 → 18 taken 8 times.
✗ Branch 17 → 26 not taken.
✗ Branch 17 → 129 not taken.
✗ Branch 17 → 149 not taken.
✓ Branch 18 → 19 taken 19437 times.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 127 not taken.
✗ Branch 18 → 145 not taken.
✗ Branch 18 → 200 not taken.
✓ Branch 21 → 22 taken 28 times.
✗ Branch 21 → 152 not taken.
✓ Branch 23 → 24 taken 8 times.
✗ Branch 23 → 62 not taken.
✓ Branch 24 → 25 taken 9351 times.
✗ Branch 24 → 225 not taken.
✓ Branch 27 → 28 taken 39899 times.
✗ Branch 27 → 77 not taken.
✓ Branch 37 → 38 taken 621 times.
✗ Branch 37 → 275 not taken.
✓ Branch 39 → 40 taken 50 times.
✗ Branch 39 → 76 not taken.
✓ Branch 41 → 42 taken 1254 times.
✗ Branch 41 → 68 not taken.
✓ Branch 42 → 43 taken 19610 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 246 not taken.
✓ Branch 43 → 44 taken 3752 times.
✗ Branch 43 → 75 not taken.
✓ Branch 44 → 45 taken 19906 times.
✗ Branch 44 → 180 not taken.
✓ Branch 50 → 51 taken 8 times.
✗ Branch 50 → 89 not taken.
✓ Branch 51 → 52 taken 3960 times.
✗ Branch 51 → 103 not taken.
✓ Branch 59 → 60 taken 42684 times.
✗ Branch 59 → 257 not taken.
✓ Branch 60 → 61 taken 20023 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 30115 times.
✗ Branch 61 → 191 not taken.
✓ Branch 65 → 66 taken 14914 times.
✗ Branch 65 → 252 not taken.
✓ Branch 67 → 68 taken 18528 times.
✗ Branch 67 → 315 not taken.
✓ Branch 69 → 70 taken 4341 times.
✗ Branch 69 → 139 not taken.
✗ Branch 69 → 174 not taken.
✗ Branch 69 → 205 not taken.
✓ Branch 72 → 73 taken 983 times.
✗ Branch 72 → 165 not taken.
✓ Branch 75 → 76 taken 101 times.
✗ Branch 75 → 216 not taken.
✓ Branch 82 → 83 taken 20641 times.
✗ Branch 82 → 199 not taken.
✓ Branch 87 → 88 taken 604 times.
✗ Branch 87 → 253 not taken.
✓ Branch 89 → 90 taken 20023 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 18528 times.
✗ Branch 96 → 309 not taken.
✓ Branch 113 → 114 taken 64 times.
✗ Branch 113 → 205 not taken.
✓ Branch 117 → 118 taken 14644 times.
✗ Branch 117 → 190 not taken.
✓ Branch 128 → 129 taken 39111 times.
✗ Branch 128 → 281 not taken.
✓ Branch 140 → 141 taken 11043 times.
✗ Branch 140 → 280 not taken.
✓ Branch 153 → 154 taken 25171 times.
✗ Branch 153 → 270 not taken.
✓ Branch 159 → 160 taken 22 times.
✗ Branch 159 → 265 not taken.
✓ Branch 186 → 187 taken 644 times.
✗ Branch 186 → 361 not taken.
✓ Branch 189 → 190 taken 40234 times.
✗ Branch 189 → 355 not taken.
✓ Branch 214 → 215 taken 3780 times.
✗ Branch 214 → 415 not taken.
1246373 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
124 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
125
24/82
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✓ Branch 4 → 7 taken 32970 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✓ Branch 5 → 8 taken 1760 times.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✓ Branch 6 → 9 taken 32925 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 148417 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✓ Branch 10 → 13 taken 240 times.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 27630 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 2185 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 6594 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 4828 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 512 times.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 942 times.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 22189 times.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 66 taken 6 times.
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 89 taken 2564 times.
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 89 not taken.
✓ Branch 91 → 92 taken 12 times.
✓ Branch 91 → 94 taken 7891 times.
✗ Branch 92 → 93 not taken.
✓ Branch 92 → 94 taken 12 times.
✗ Branch 93 → 94 not taken.
✓ Branch 93 → 96 taken 2279 times.
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 96 not taken.
✗ Branch 100 → 101 not taken.
✓ Branch 100 → 103 taken 37013 times.
✗ Branch 101 → 102 not taken.
✗ Branch 101 → 103 not taken.
✓ Branch 126 → 127 taken 40 times.
✗ Branch 126 → 129 not taken.
✗ Branch 127 → 128 not taken.
✓ Branch 127 → 129 taken 40 times.
✓ Branch 174 → 175 taken 44 times.
✗ Branch 174 → 177 not taken.
✗ Branch 175 → 176 not taken.
✓ Branch 175 → 177 taken 44 times.
✗ Branch 181 → 182 not taken.
✓ Branch 181 → 184 taken 6040 times.
✗ Branch 182 → 183 not taken.
✗ Branch 182 → 184 not taken.
✗ Branch 243 → 244 not taken.
✗ Branch 243 → 246 not taken.
✗ Branch 244 → 245 not taken.
✗ Branch 244 → 246 not taken.
337081 assert(type == ScopeType::STRUCT || type == ScopeType::UNION);
126
16/40
✓ Branch 8 → 9 taken 1760 times.
✗ Branch 8 → 36 not taken.
✓ Branch 9 → 10 taken 32925 times.
✗ Branch 9 → 42 not taken.
✗ Branch 9 → 76 not taken.
✗ Branch 9 → 89 not taken.
✗ Branch 9 → 98 not taken.
✓ Branch 24 → 25 taken 27630 times.
✗ Branch 24 → 96 not taken.
✗ Branch 24 → 116 not taken.
✓ Branch 28 → 29 taken 2185 times.
✗ Branch 28 → 106 not taken.
✓ Branch 30 → 31 taken 6594 times.
✗ Branch 30 → 79 not taken.
✓ Branch 33 → 34 taken 4828 times.
✗ Branch 33 → 128 not taken.
✓ Branch 44 → 45 taken 512 times.
✗ Branch 44 → 103 not taken.
✓ Branch 48 → 49 taken 942 times.
✗ Branch 48 → 210 not taken.
✗ Branch 48 → 248 not taken.
✓ Branch 50 → 51 taken 22189 times.
✗ Branch 50 → 189 not taken.
✓ Branch 66 → 67 taken 6 times.
✗ Branch 66 → 103 not taken.
✓ Branch 89 → 90 taken 2564 times.
✗ Branch 89 → 203 not taken.
✓ Branch 94 → 95 taken 7903 times.
✗ Branch 94 → 169 not taken.
✗ Branch 94 → 196 not taken.
✓ Branch 96 → 97 taken 2279 times.
✗ Branch 96 → 326 not taken.
✓ Branch 103 → 104 taken 37012 times.
✗ Branch 103 → 248 not taken.
✓ Branch 177 → 178 taken 44 times.
✗ Branch 177 → 257 not taken.
✓ Branch 184 → 185 taken 6040 times.
✗ Branch 184 → 307 not taken.
✗ Branch 246 → 247 not taken.
✗ Branch 246 → 356 not taken.
342552 return symbolTable.lookupStrictByIndex(n);
127 }
128
129 // Public members
130 Scope *parent;
131 SourceFile *sourceFile;
132 uint64_t scopeId;
133 std::map<std::string, std::shared_ptr<Scope>> children;
134 SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
135 const CodeLoc *codeLoc = nullptr;
136 const ScopeType type;
137 bool isGenericScope = false;
138 bool isAsyncScope = false;
139 bool isDtorScope = false;
140 // Only for expression scopes: dtor calls for the temporaries of the expression, in the order they are to be generated
141 std::vector<std::pair<SymbolTableEntry *, Function *>> temporaryDtorsToCall;
142
143 private:
144 // Private members
145 FunctionRegistry functions;
146 StructRegistry structs;
147 InterfaceRegistry interfaces;
148 UnionRegistry unions;
149 std::map<std::string, GenericType> genericTypes;
150 };
151
152 } // namespace spice::compiler
153