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.9% 102 / 0 / 262

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 587741 [[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 2296 times.
✗ Branch 9 → 97 not taken.
✓ Branch 13 → 14 taken 3354 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 1251 times.
✗ Branch 26 → 68 not taken.
✓ Branch 28 → 29 taken 564 times.
✗ Branch 28 → 92 not taken.
✓ Branch 30 → 31 taken 110 times.
✗ Branch 30 → 63 not taken.
✓ Branch 33 → 34 taken 948 times.
✗ Branch 33 → 51 not taken.
✓ Branch 36 → 37 taken 1251 times.
✗ Branch 36 → 74 not taken.
✓ Branch 38 → 39 taken 297 times.
✗ Branch 38 → 73 not taken.
✓ Branch 43 → 44 taken 297 times.
✗ Branch 43 → 71 not taken.
✓ Branch 48 → 49 taken 34 times.
✗ Branch 48 → 82 not taken.
✓ Branch 55 → 56 taken 2296 times.
✗ Branch 55 → 86 not taken.
✓ Branch 62 → 63 taken 415 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 16759 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 18347 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 4104 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 36361 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 23091 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 36361 times.
✗ Branch 84 → 141 not taken.
✓ Branch 225 → 226 taken 3416 times.
✗ Branch 225 → 415 not taken.
287590 return symbolTable.insert(name, declNode);
121 }
122
6/12
✓ Branch 5 → 6 taken 56025 times.
✗ Branch 5 → 100 not taken.
✓ Branch 18 → 19 taken 321974 times.
✗ Branch 18 → 231 not taken.
✓ Branch 23 → 24 taken 92917 times.
✗ Branch 23 → 632 not taken.
✓ Branch 34 → 35 taken 5610 times.
✗ Branch 34 → 78 not taken.
✓ Branch 90 → 91 taken 94523 times.
✗ Branch 90 → 561 not taken.
✓ Branch 244 → 245 taken 134 times.
✗ Branch 244 → 499 not taken.
571187 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
123
39/97
✓ Branch 2 → 3 taken 190347 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 178 not taken.
✓ Branch 6 → 7 taken 10052 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 11134 times.
✗ Branch 7 → 48 not taken.
✗ Branch 7 → 54 not taken.
✗ Branch 7 → 72 not taken.
✗ Branch 7 → 117 not taken.
✗ Branch 7 → 131 not taken.
✗ Branch 7 → 140 not taken.
✓ Branch 9 → 10 taken 2576 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 → 142 not taken.
✓ Branch 18 → 19 taken 16881 times.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 127 not taken.
✗ Branch 18 → 145 not taken.
✗ Branch 18 → 182 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 8677 times.
✗ Branch 24 → 222 not taken.
✓ Branch 27 → 28 taken 35824 times.
✗ Branch 27 → 77 not taken.
✓ Branch 37 → 38 taken 591 times.
✗ Branch 37 → 275 not taken.
✓ Branch 39 → 40 taken 38 times.
✗ Branch 39 → 76 not taken.
✓ Branch 41 → 42 taken 1207 times.
✗ Branch 41 → 68 not taken.
✓ Branch 42 → 43 taken 17682 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 233 not taken.
✓ Branch 44 → 45 taken 18128 times.
✗ Branch 44 → 180 not taken.
✓ Branch 45 → 46 taken 3358 times.
✗ Branch 45 → 77 not taken.
✓ Branch 50 → 51 taken 8 times.
✗ Branch 50 → 89 not taken.
✓ Branch 51 → 52 taken 3344 times.
✗ Branch 51 → 103 not taken.
✓ Branch 59 → 60 taken 37991 times.
✗ Branch 59 → 244 not taken.
✓ Branch 60 → 61 taken 18341 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 28101 times.
✗ Branch 61 → 191 not taken.
✓ Branch 65 → 66 taken 13068 times.
✗ Branch 65 → 252 not taken.
✓ Branch 67 → 68 taken 16755 times.
✗ Branch 67 → 315 not taken.
✓ Branch 69 → 70 taken 4000 times.
✗ Branch 69 → 139 not taken.
✗ Branch 69 → 174 not taken.
✗ Branch 69 → 205 not taken.
✓ Branch 71 → 72 taken 98 times.
✗ Branch 71 → 198 not taken.
✓ Branch 72 → 73 taken 930 times.
✗ Branch 72 → 165 not taken.
✓ Branch 82 → 83 taken 18966 times.
✗ Branch 82 → 199 not taken.
✓ Branch 87 → 88 taken 562 times.
✗ Branch 87 → 253 not taken.
✓ Branch 89 → 90 taken 18341 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 16755 times.
✗ Branch 96 → 309 not taken.
✓ Branch 113 → 114 taken 64 times.
✗ Branch 113 → 205 not taken.
✓ Branch 117 → 118 taken 13442 times.
✗ Branch 117 → 190 not taken.
✓ Branch 128 → 129 taken 35022 times.
✗ Branch 128 → 269 not taken.
✓ Branch 140 → 141 taken 10114 times.
✗ Branch 140 → 280 not taken.
✓ Branch 153 → 154 taken 23079 times.
✗ Branch 153 → 270 not taken.
✓ Branch 159 → 160 taken 22 times.
✗ Branch 159 → 265 not taken.
✓ Branch 186 → 187 taken 616 times.
✗ Branch 186 → 361 not taken.
✓ Branch 189 → 190 taken 36335 times.
✗ Branch 189 → 355 not taken.
✓ Branch 214 → 215 taken 3420 times.
✗ Branch 214 → 415 not taken.
764094 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
124 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
125
24/80
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 4995 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✓ Branch 4 → 7 taken 30536 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✓ Branch 5 → 8 taken 1646 times.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✓ Branch 6 → 9 taken 30569 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 139015 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 25428 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✓ Branch 22 → 25 taken 2145 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 890 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 5985 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 4383 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 2 times.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 20395 times.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 65 taken 6 times.
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 65 not taken.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 81 taken 2364 times.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
✓ Branch 91 → 92 taken 12 times.
✓ Branch 91 → 94 taken 7303 times.
✗ Branch 92 → 93 not taken.
✓ Branch 92 → 94 taken 12 times.
✗ Branch 97 → 98 not taken.
✓ Branch 97 → 100 taken 2273 times.
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 100 not taken.
✗ Branch 100 → 101 not taken.
✓ Branch 100 → 103 taken 33896 times.
✗ Branch 101 → 102 not taken.
✗ Branch 101 → 103 not taken.
✓ Branch 118 → 119 taken 40 times.
✗ Branch 118 → 121 not taken.
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 121 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 5049 times.
✗ Branch 182 → 183 not taken.
✗ Branch 182 → 184 not taken.
✗ Branch 246 → 247 not taken.
✗ Branch 246 → 249 not taken.
✗ Branch 247 → 248 not taken.
✗ Branch 247 → 249 not taken.
316976 assert(type == ScopeType::STRUCT || type == ScopeType::UNION);
126
14/35
✓ Branch 8 → 9 taken 1646 times.
✗ Branch 8 → 36 not taken.
✓ Branch 9 → 10 taken 30569 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 25428 times.
✗ Branch 24 → 96 not taken.
✗ Branch 24 → 116 not taken.
✓ Branch 30 → 31 taken 5985 times.
✗ Branch 30 → 79 not taken.
✓ Branch 33 → 34 taken 4383 times.
✗ Branch 33 → 128 not taken.
✓ Branch 44 → 45 taken 2 times.
✗ Branch 44 → 101 not taken.
✓ Branch 50 → 51 taken 20395 times.
✗ Branch 50 → 187 not taken.
✓ Branch 65 → 66 taken 6 times.
✗ Branch 65 → 101 not taken.
✓ Branch 81 → 82 taken 2364 times.
✗ Branch 81 → 194 not taken.
✓ Branch 94 → 95 taken 7315 times.
✗ Branch 94 → 169 not taken.
✗ Branch 94 → 196 not taken.
✓ Branch 100 → 101 taken 2273 times.
✗ Branch 100 → 329 not taken.
✓ Branch 103 → 104 taken 33896 times.
✗ Branch 103 → 248 not taken.
✓ Branch 177 → 178 taken 44 times.
✗ Branch 177 → 257 not taken.
✓ Branch 184 → 185 taken 5049 times.
✗ Branch 184 → 307 not taken.
✗ Branch 249 → 250 not taken.
✗ Branch 249 → 359 not taken.
316976 return symbolTable.lookupStrictByIndex(n);
127 }
128
129 // Public members
130 Scope *parent;
131 SourceFile *sourceFile;
132 std::map<std::string, std::shared_ptr<Scope>> children;
133 SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
134 const CodeLoc *codeLoc = nullptr;
135 const ScopeType type;
136 bool isGenericScope = false;
137 bool isAsyncScope = false;
138 bool isDtorScope = false;
139 // Only for expression scopes: dtor calls for the temporaries of the expression, in the order they are to be generated
140 std::vector<std::pair<SymbolTableEntry *, Function *>> temporaryDtorsToCall;
141
142 private:
143 // Private members
144 FunctionRegistry functions;
145 StructRegistry structs;
146 InterfaceRegistry interfaces;
147 UnionRegistry unions;
148 std::map<std::string, GenericType> genericTypes;
149 };
150
151 } // namespace spice::compiler
152