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% 101 / 0 / 261

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 };
56
57 /**
58 * Represents an enclosed group of instructions, which are semantically separated from other scopes.
59 * In the source code, scopes usually are written as curly braces.
60 *
61 * Following language structures use scopes:
62 * - global scope
63 * - functions/procedures
64 * - structs
65 * - enums
66 * - interfaces
67 * - thread blocks
68 * - unsafe blocks
69 * - for loops
70 * - foreach loops
71 * - while loops
72 * - if statements
73 * - anonymous scopes
74 */
75 class Scope {
76 public:
77 // Constructors
78 Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc);
79
80 // Friend classes
81 friend class FunctionManager;
82 friend class StructManager;
83 friend class InterfaceManager;
84 friend class UnionManager;
85
86 // Public methods
87 // Scope management
88 Scope *createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc);
89 void renameChildScope(const std::string &oldName, const std::string &newName);
90 Scope *copyChildScope(const std::string &oldName, const std::string &newName);
91 std::shared_ptr<Scope> deepCopyScope();
92 [[nodiscard]] Scope *getChildScope(const std::string &scopeName) const;
93 [[nodiscard]] std::vector<SymbolTableEntry *> getVarsGoingOutOfScope();
94
95 // Generic types
96 void insertGenericType(const std::string &typeName, const GenericType &genericType);
97 GenericType *lookupGenericTypeStrict(const std::string &typeName);
98
99 // Util
100 void collectWarnings(std::vector<CompilerWarning> &warnings) const;
101 void ensureSuccessfulTypeInference() const;
102 [[nodiscard]] size_t getFieldCount() const;
103 [[nodiscard]] std::vector<const Function *> getVirtualMethods();
104 [[nodiscard]] std::vector<Struct *> getAllStructManifestationsInDeclarationOrder();
105 [[nodiscard]] std::vector<Union *> getAllUnionManifestationsInDeclarationOrder();
106 void collectManifestationFingerprint(std::stringstream &fingerprint) const;
107 [[nodiscard]] unsigned int getLoopNestingDepth() const;
108 [[nodiscard]] Scope *getFunctionScope();
109 [[nodiscard]] bool isInCaseBranch() const;
110 [[nodiscard]] bool isInAsyncScope() const;
111 [[nodiscard]] bool doesAllowUnsafeOperations() const;
112 [[nodiscard]] bool isImportedBy(const Scope *askingScope) const;
113 [[nodiscard]] nlohmann::json getSymbolTableJSON() const;
114 406111 [[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; }
115
116 // Wrapper methods for symbol table
117 ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) {
118
19/38
✓ Branch 9 → 10 taken 8166 times.
✗ Branch 9 → 97 not taken.
✓ Branch 13 → 14 taken 11078 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 1244 times.
✗ Branch 26 → 68 not taken.
✓ Branch 28 → 29 taken 774 times.
✗ Branch 28 → 92 not taken.
✓ Branch 30 → 31 taken 109 times.
✗ Branch 30 → 63 not taken.
✓ Branch 33 → 34 taken 4907 times.
✗ Branch 33 → 51 not taken.
✓ Branch 36 → 37 taken 1244 times.
✗ Branch 36 → 74 not taken.
✓ Branch 38 → 39 taken 709 times.
✗ Branch 38 → 73 not taken.
✓ Branch 43 → 44 taken 709 times.
✗ Branch 43 → 71 not taken.
✓ Branch 48 → 49 taken 64 times.
✗ Branch 48 → 82 not taken.
✓ Branch 55 → 56 taken 8166 times.
✗ Branch 55 → 86 not taken.
✓ Branch 62 → 63 taken 819 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 25617 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 24652 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 7637 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 50188 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 30228 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 50188 times.
✗ Branch 84 → 141 not taken.
✓ Branch 225 → 226 taken 6070 times.
✗ Branch 225 → 415 not taken.
409710 return symbolTable.insert(name, declNode);
119 }
120
6/12
✓ Branch 5 → 6 taken 78456 times.
✗ Branch 5 → 98 not taken.
✓ Branch 18 → 19 taken 423554 times.
✗ Branch 18 → 218 not taken.
✓ Branch 23 → 24 taken 134955 times.
✗ Branch 23 → 632 not taken.
✓ Branch 34 → 35 taken 6983 times.
✗ Branch 34 → 78 not taken.
✓ Branch 90 → 91 taken 136692 times.
✗ Branch 90 → 561 not taken.
✓ Branch 244 → 245 taken 128 times.
✗ Branch 244 → 499 not taken.
781276 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
121
38/96
✓ Branch 2 → 3 taken 263684 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 178 not taken.
✓ Branch 6 → 7 taken 21279 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 18394 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 2671 times.
✗ Branch 9 → 23 not taken.
✗ Branch 9 → 53 not taken.
✓ Branch 17 → 18 taken 56 times.
✗ Branch 17 → 26 not taken.
✗ Branch 17 → 123 not taken.
✗ Branch 17 → 142 not taken.
✓ Branch 18 → 19 taken 25031 times.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 121 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 15221 times.
✗ Branch 24 → 222 not taken.
✓ Branch 29 → 30 taken 53885 times.
✗ Branch 29 → 87 not taken.
✓ Branch 37 → 38 taken 639 times.
✗ Branch 37 → 275 not taken.
✓ Branch 41 → 42 taken 1236 times.
✗ Branch 41 → 68 not taken.
✗ Branch 41 → 86 not taken.
✓ Branch 42 → 43 taken 30515 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 233 not taken.
✓ Branch 44 → 45 taken 26672 times.
✗ Branch 44 → 180 not taken.
✓ Branch 45 → 46 taken 5626 times.
✗ Branch 45 → 77 not taken.
✓ Branch 51 → 52 taken 11068 times.
✗ Branch 51 → 103 not taken.
✓ Branch 52 → 53 taken 6 times.
✗ Branch 52 → 99 not taken.
✓ Branch 59 → 60 taken 54209 times.
✗ Branch 59 → 244 not taken.
✓ Branch 60 → 61 taken 24646 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 37388 times.
✗ Branch 61 → 191 not taken.
✓ Branch 65 → 66 taken 24303 times.
✗ Branch 65 → 252 not taken.
✓ Branch 67 → 68 taken 25613 times.
✗ Branch 67 → 315 not taken.
✓ Branch 69 → 70 taken 6639 times.
✗ Branch 69 → 139 not taken.
✗ Branch 69 → 174 not taken.
✗ Branch 69 → 205 not taken.
✓ Branch 71 → 72 taken 97 times.
✗ Branch 71 → 198 not taken.
✓ Branch 72 → 73 taken 943 times.
✗ Branch 72 → 165 not taken.
✓ Branch 82 → 83 taken 27592 times.
✗ Branch 82 → 199 not taken.
✓ Branch 87 → 88 taken 772 times.
✗ Branch 87 → 253 not taken.
✓ Branch 89 → 90 taken 24646 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 25613 times.
✗ Branch 96 → 309 not taken.
✓ Branch 113 → 114 taken 304 times.
✗ Branch 113 → 205 not taken.
✓ Branch 117 → 118 taken 20930 times.
✗ Branch 117 → 190 not taken.
✓ Branch 128 → 129 taken 53068 times.
✗ Branch 128 → 269 not taken.
✓ Branch 140 → 141 taken 15547 times.
✗ Branch 140 → 280 not taken.
✓ Branch 153 → 154 taken 30216 times.
✗ Branch 153 → 270 not taken.
✓ Branch 159 → 160 taken 64 times.
✗ Branch 159 → 265 not taken.
✓ Branch 186 → 187 taken 990 times.
✗ Branch 186 → 361 not taken.
✓ Branch 189 → 190 taken 50170 times.
✗ Branch 189 → 355 not taken.
✓ Branch 214 → 215 taken 6074 times.
✗ Branch 214 → 415 not taken.
1099375 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
122 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
123
24/80
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 13234 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✓ Branch 4 → 7 taken 50242 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✓ Branch 5 → 8 taken 2854 times.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✓ Branch 6 → 9 taken 58112 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 191841 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 39629 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✓ Branch 22 → 25 taken 3237 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1444 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 10425 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 5215 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 14 times.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 32025 times.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 65 taken 1646 times.
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 65 not taken.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 81 taken 4328 times.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
✓ Branch 91 → 92 taken 12 times.
✓ Branch 91 → 94 taken 11095 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 57081 times.
✗ Branch 101 → 102 not taken.
✗ Branch 101 → 103 not taken.
✓ Branch 118 → 119 taken 280 times.
✗ Branch 118 → 121 not taken.
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 121 taken 280 times.
✗ Branch 172 → 173 not taken.
✓ Branch 172 → 175 taken 7394 times.
✗ Branch 173 → 174 not taken.
✗ Branch 173 → 175 not taken.
✓ Branch 174 → 175 taken 380 times.
✗ Branch 174 → 177 not taken.
✗ Branch 175 → 176 not taken.
✓ Branch 175 → 177 taken 380 times.
✗ Branch 246 → 247 not taken.
✗ Branch 246 → 249 not taken.
✗ Branch 247 → 248 not taken.
✗ Branch 247 → 249 not taken.
492761 assert(type == ScopeType::STRUCT || type == ScopeType::UNION);
124
14/35
✓ Branch 8 → 9 taken 2854 times.
✗ Branch 8 → 36 not taken.
✓ Branch 9 → 10 taken 58112 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 39629 times.
✗ Branch 24 → 96 not taken.
✗ Branch 24 → 116 not taken.
✓ Branch 30 → 31 taken 10425 times.
✗ Branch 30 → 79 not taken.
✓ Branch 33 → 34 taken 5215 times.
✗ Branch 33 → 128 not taken.
✓ Branch 44 → 45 taken 14 times.
✗ Branch 44 → 101 not taken.
✓ Branch 50 → 51 taken 32025 times.
✗ Branch 50 → 187 not taken.
✓ Branch 65 → 66 taken 1646 times.
✗ Branch 65 → 101 not taken.
✓ Branch 81 → 82 taken 4328 times.
✗ Branch 81 → 194 not taken.
✓ Branch 94 → 95 taken 11107 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 57081 times.
✗ Branch 103 → 248 not taken.
✓ Branch 175 → 176 taken 7394 times.
✗ Branch 175 → 294 not taken.
✓ Branch 177 → 178 taken 380 times.
✗ Branch 177 → 257 not taken.
✗ Branch 249 → 250 not taken.
✗ Branch 249 → 359 not taken.
492761 return symbolTable.lookupStrictByIndex(n);
125 }
126
127 // Public members
128 Scope *parent;
129 SourceFile *sourceFile;
130 std::map<std::string, std::shared_ptr<Scope>> children;
131 SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
132 const CodeLoc *codeLoc = nullptr;
133 const ScopeType type;
134 bool isGenericScope = false;
135 bool isAsyncScope = false;
136 bool isDtorScope = false;
137
138 private:
139 // Private members
140 FunctionRegistry functions;
141 StructRegistry structs;
142 InterfaceRegistry interfaces;
143 UnionRegistry unions;
144 std::map<std::string, GenericType> genericTypes;
145 };
146
147 } // namespace spice::compiler
148