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: 39.8% 104 / 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 <string>
8 #include <vector>
9
10 #include <model/Function.h>
11 #include <model/Interface.h>
12 #include <model/Struct.h>
13 #include <model/Union.h>
14 #include <symboltablebuilder/SymbolTable.h>
15 #include <util/GlobalDefinitions.h>
16
17 namespace spice::compiler {
18
19 // Forward declarations
20 class FctDefNode;
21 class ProcDefNode;
22 class SourceFile;
23 class SymbolTableEntry;
24 class GenericType;
25 class CompilerWarning;
26 class Function;
27 class Struct;
28 class ASTNode;
29 using FunctionManifestationList = std::map</*mangledName=*/std::string, Function>;
30 using FunctionRegistry = std::map</*fctId=*/std::string, /*manifestationList=*/FunctionManifestationList>;
31 using StructManifestationList = std::map</*mangledName=*/std::string, Struct>;
32 using StructRegistry = std::map</*structId=*/std::string, /*manifestationList=*/StructManifestationList>;
33 using InterfaceManifestationList = std::map</*mangledName=*/std::string, Interface>;
34 using InterfaceRegistry = std::map<CodeLoc, InterfaceManifestationList>;
35 using UnionManifestationList = std::map</*mangledName=*/std::string, Union>;
36 using UnionRegistry = std::map</*unionId=*/std::string, /*manifestationList=*/UnionManifestationList>;
37
38 enum class ScopeType : uint8_t {
39 GLOBAL,
40 FUNC_PROC_BODY,
41 LAMBDA_BODY,
42 STRUCT,
43 INTERFACE,
44 UNION,
45 ENUM,
46 IF_ELSE_BODY,
47 WHILE_BODY,
48 FOR_BODY,
49 FOREACH_BODY,
50 CASE_BODY,
51 DEFAULT_BODY,
52 UNSAFE_BODY,
53 ANONYMOUS_BLOCK_BODY
54 };
55
56 /**
57 * Represents an enclosed group of instructions, which are semantically separated from other scopes.
58 * In the source code, scopes usually are written as curly braces.
59 *
60 * Following language structures use scopes:
61 * - global scope
62 * - functions/procedures
63 * - structs
64 * - enums
65 * - interfaces
66 * - thread blocks
67 * - unsafe blocks
68 * - for loops
69 * - foreach loops
70 * - while loops
71 * - if statements
72 * - anonymous scopes
73 */
74 class Scope {
75 public:
76 // Constructors
77 Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc);
78
79 // Friend classes
80 friend class FunctionManager;
81 friend class StructManager;
82 friend class InterfaceManager;
83 friend class UnionManager;
84
85 // Public methods
86 // Scope management
87 Scope *createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc);
88 void renameChildScope(const std::string &oldName, const std::string &newName);
89 Scope *copyChildScope(const std::string &oldName, const std::string &newName);
90 std::shared_ptr<Scope> deepCopyScope();
91 [[nodiscard]] Scope *getChildScope(const std::string &scopeName) const;
92 [[nodiscard]] std::vector<SymbolTableEntry *> getVarsGoingOutOfScope();
93
94 // Generic types
95 void insertGenericType(const std::string &typeName, const GenericType &genericType);
96 GenericType *lookupGenericTypeStrict(const std::string &typeName);
97
98 // Util
99 void collectWarnings(std::vector<CompilerWarning> &warnings) const;
100 void ensureSuccessfulTypeInference() const;
101 [[nodiscard]] size_t getFieldCount() const;
102 [[nodiscard]] std::vector<const Function *> getVirtualMethods();
103 [[nodiscard]] std::vector<Struct *> getAllStructManifestationsInDeclarationOrder();
104 [[nodiscard]] std::vector<Union *> getAllUnionManifestationsInDeclarationOrder();
105 [[nodiscard]] unsigned int getLoopNestingDepth() const;
106 [[nodiscard]] Scope *getFunctionScope();
107 [[nodiscard]] bool isInCaseBranch() const;
108 [[nodiscard]] bool isInAsyncScope() const;
109 [[nodiscard]] bool doesAllowUnsafeOperations() const;
110 [[nodiscard]] bool isImportedBy(const Scope *askingScope) const;
111 [[nodiscard]] nlohmann::json getSymbolTableJSON() const;
112 391379 [[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; }
113
114 // Wrapper methods for symbol table
115 ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) {
116
19/38
✓ Branch 9 → 10 taken 8056 times.
✗ Branch 9 → 97 not taken.
✓ Branch 13 → 14 taken 11046 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 1212 times.
✗ Branch 26 → 68 not taken.
✓ Branch 28 → 29 taken 742 times.
✗ Branch 28 → 92 not taken.
✓ Branch 30 → 31 taken 109 times.
✗ Branch 30 → 63 not taken.
✓ Branch 33 → 34 taken 4867 times.
✗ Branch 33 → 51 not taken.
✓ Branch 36 → 37 taken 1212 times.
✗ Branch 36 → 74 not taken.
✓ Branch 38 → 39 taken 705 times.
✗ Branch 38 → 73 not taken.
✓ Branch 43 → 44 taken 705 times.
✗ Branch 43 → 71 not taken.
✓ Branch 48 → 49 taken 64 times.
✗ Branch 48 → 82 not taken.
✓ Branch 55 → 56 taken 8056 times.
✗ Branch 55 → 86 not taken.
✓ Branch 62 → 63 taken 803 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 24905 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 23938 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 7459 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 48728 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 29252 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 48728 times.
✗ Branch 84 → 141 not taken.
✓ Branch 169 → 170 taken 5900 times.
✗ Branch 169 → 323 not taken.
397468 return symbolTable.insert(name, declNode);
117 }
118
6/12
✓ Branch 5 → 6 taken 75904 times.
✗ Branch 5 → 98 not taken.
✓ Branch 18 → 19 taken 409302 times.
✗ Branch 18 → 218 not taken.
✓ Branch 23 → 24 taken 130829 times.
✗ Branch 23 → 632 not taken.
✓ Branch 34 → 35 taken 6695 times.
✗ Branch 34 → 78 not taken.
✓ Branch 90 → 91 taken 132558 times.
✗ Branch 90 → 561 not taken.
✓ Branch 236 → 237 taken 122 times.
✗ Branch 236 → 491 not taken.
755918 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
119
38/96
✓ Branch 2 → 3 taken 256148 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 178 not taken.
✓ Branch 6 → 7 taken 21041 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 18038 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 2591 times.
✗ Branch 9 → 23 not taken.
✗ Branch 9 → 53 not taken.
✓ Branch 17 → 18 taken 52 times.
✗ Branch 17 → 26 not taken.
✗ Branch 17 → 123 not taken.
✓ Branch 18 → 19 taken 23982 times.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 121 not taken.
✓ Branch 20 → 21 taken 4 times.
✗ Branch 20 → 143 not taken.
✓ Branch 21 → 22 taken 213 times.
✗ Branch 21 → 146 not taken.
✗ Branch 21 → 150 not taken.
✗ Branch 21 → 183 not taken.
✓ Branch 23 → 24 taken 8 times.
✗ Branch 23 → 62 not taken.
✓ Branch 24 → 25 taken 15005 times.
✗ Branch 24 → 222 not taken.
✓ Branch 29 → 30 taken 52523 times.
✗ Branch 29 → 87 not taken.
✓ Branch 37 → 38 taken 575 times.
✗ Branch 37 → 275 not taken.
✓ Branch 41 → 42 taken 1204 times.
✗ Branch 41 → 68 not taken.
✗ Branch 41 → 86 not taken.
✓ Branch 42 → 43 taken 29803 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 233 not taken.
✓ Branch 44 → 45 taken 26000 times.
✗ Branch 44 → 180 not taken.
✓ Branch 45 → 46 taken 5522 times.
✗ Branch 45 → 77 not taken.
✓ Branch 51 → 52 taken 11036 times.
✗ Branch 51 → 103 not taken.
✓ Branch 52 → 53 taken 6 times.
✗ Branch 52 → 99 not taken.
✓ Branch 59 → 60 taken 52731 times.
✗ Branch 59 → 244 not taken.
✓ Branch 60 → 61 taken 23932 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 36398 times.
✗ Branch 61 → 191 not taken.
✓ Branch 65 → 66 taken 25958 times.
✗ Branch 65 → 252 not taken.
✓ Branch 67 → 68 taken 24901 times.
✗ Branch 67 → 315 not taken.
✓ Branch 69 → 70 taken 6537 times.
✗ Branch 69 → 139 not taken.
✗ Branch 69 → 174 not taken.
✗ Branch 69 → 205 not taken.
✓ Branch 70 → 71 taken 1018 times.
✗ Branch 70 → 163 not taken.
✗ Branch 70 → 197 not taken.
✓ Branch 82 → 83 taken 27062 times.
✗ Branch 82 → 199 not taken.
✓ Branch 87 → 88 taken 740 times.
✗ Branch 87 → 253 not taken.
✓ Branch 89 → 90 taken 23932 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 24901 times.
✗ Branch 96 → 309 not taken.
✓ Branch 113 → 114 taken 304 times.
✗ Branch 113 → 205 not taken.
✓ Branch 117 → 118 taken 20582 times.
✗ Branch 117 → 190 not taken.
✓ Branch 128 → 129 taken 51712 times.
✗ Branch 128 → 269 not taken.
✓ Branch 140 → 141 taken 15017 times.
✗ Branch 140 → 280 not taken.
✓ Branch 153 → 154 taken 29240 times.
✗ Branch 153 → 270 not taken.
✓ Branch 158 → 159 taken 5904 times.
✗ Branch 158 → 323 not taken.
✓ Branch 159 → 160 taken 64 times.
✗ Branch 159 → 265 not taken.
✓ Branch 186 → 187 taken 958 times.
✗ Branch 186 → 361 not taken.
✓ Branch 189 → 190 taken 48710 times.
✗ Branch 189 → 355 not taken.
1175542 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
120 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
121
26/80
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 12902 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✓ Branch 4 → 7 taken 49158 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✓ Branch 5 → 8 taken 2790 times.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✓ Branch 6 → 9 taken 56902 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 189369 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 38417 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✓ Branch 22 → 25 taken 3155 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1396 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 10005 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 4903 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 31363 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 4360 times.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
✓ Branch 91 → 92 taken 12 times.
✓ Branch 91 → 94 taken 10925 times.
✗ Branch 92 → 93 not taken.
✓ Branch 92 → 94 taken 12 times.
✗ Branch 97 → 98 not taken.
✓ Branch 97 → 100 taken 46785 times.
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 100 not taken.
✗ Branch 100 → 101 not taken.
✓ Branch 100 → 103 taken 61944 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 7152 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 taken 190 times.
✗ Branch 246 → 249 not taken.
✗ Branch 247 → 248 not taken.
✓ Branch 247 → 249 taken 190 times.
534048 assert(type == ScopeType::STRUCT || type == ScopeType::UNION);
122
15/35
✓ Branch 8 → 9 taken 2790 times.
✗ Branch 8 → 36 not taken.
✓ Branch 9 → 10 taken 56902 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 38417 times.
✗ Branch 24 → 96 not taken.
✗ Branch 24 → 116 not taken.
✓ Branch 30 → 31 taken 10005 times.
✗ Branch 30 → 79 not taken.
✓ Branch 33 → 34 taken 4903 times.
✗ Branch 33 → 128 not taken.
✓ Branch 44 → 45 taken 14 times.
✗ Branch 44 → 101 not taken.
✓ Branch 50 → 51 taken 31363 times.
✗ Branch 50 → 187 not taken.
✓ Branch 65 → 66 taken 1646 times.
✗ Branch 65 → 101 not taken.
✓ Branch 81 → 82 taken 4360 times.
✗ Branch 81 → 194 not taken.
✓ Branch 94 → 95 taken 10937 times.
✗ Branch 94 → 169 not taken.
✗ Branch 94 → 196 not taken.
✓ Branch 100 → 101 taken 46785 times.
✗ Branch 100 → 329 not taken.
✓ Branch 103 → 104 taken 61944 times.
✗ Branch 103 → 248 not taken.
✓ Branch 175 → 176 taken 7152 times.
✗ Branch 175 → 294 not taken.
✓ Branch 177 → 178 taken 380 times.
✗ Branch 177 → 257 not taken.
✓ Branch 249 → 250 taken 190 times.
✗ Branch 249 → 359 not taken.
534048 return symbolTable.lookupStrictByIndex(n);
123 }
124
125 // Public members
126 Scope *parent;
127 SourceFile *sourceFile;
128 std::map<std::string, std::shared_ptr<Scope>> children;
129 SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
130 const CodeLoc *codeLoc = nullptr;
131 const ScopeType type;
132 bool isGenericScope = false;
133 bool isAsyncScope = false;
134 bool isDtorScope = false;
135
136 private:
137 // Private members
138 FunctionRegistry functions;
139 StructRegistry structs;
140 InterfaceRegistry interfaces;
141 UnionRegistry unions;
142 std::map<std::string, GenericType> genericTypes;
143 };
144
145 } // namespace spice::compiler
146