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: 45.0% 81 / 0 / 180

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 <symboltablebuilder/SymbolTable.h>
14 #include <util/GlobalDefinitions.h>
15
16 namespace spice::compiler {
17
18 // Forward declarations
19 class FctDefNode;
20 class ProcDefNode;
21 class SourceFile;
22 class SymbolTableEntry;
23 class GenericType;
24 class CompilerWarning;
25 class Function;
26 class Struct;
27 class ASTNode;
28 using FunctionManifestationList = std::map</*mangledName=*/std::string, Function>;
29 using FunctionRegistry = std::map</*fctId=*/std::string, /*manifestationList=*/FunctionManifestationList>;
30 using StructManifestationList = std::map</*mangledName=*/std::string, Struct>;
31 using StructRegistry = std::map</*structId=*/std::string, /*manifestationList=*/StructManifestationList>;
32 using InterfaceManifestationList = std::map</*mangledName=*/std::string, Interface>;
33 using InterfaceRegistry = std::map<CodeLoc, InterfaceManifestationList>;
34
35 enum class ScopeType : uint8_t {
36 GLOBAL,
37 FUNC_PROC_BODY,
38 LAMBDA_BODY,
39 STRUCT,
40 INTERFACE,
41 ENUM,
42 IF_ELSE_BODY,
43 WHILE_BODY,
44 FOR_BODY,
45 FOREACH_BODY,
46 CASE_BODY,
47 DEFAULT_BODY,
48 UNSAFE_BODY,
49 ANONYMOUS_BLOCK_BODY
50 };
51
52 /**
53 * Represents an enclosed group of instructions, which are semantically separated from other scopes.
54 * In the source code, scopes usually are written as curly braces.
55 *
56 * Following language structures use scopes:
57 * - global scope
58 * - functions/procedures
59 * - structs
60 * - enums
61 * - interfaces
62 * - thread blocks
63 * - unsafe blocks
64 * - for loops
65 * - foreach loops
66 * - while loops
67 * - if statements
68 * - anonymous scopes
69 */
70 class Scope {
71 public:
72 // Constructors
73 Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc);
74
75 // Friend classes
76 friend class FunctionManager;
77 friend class StructManager;
78 friend class InterfaceManager;
79
80 // Public methods
81 // Scope management
82 Scope *createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc);
83 void renameChildScope(const std::string &oldName, const std::string &newName);
84 Scope *copyChildScope(const std::string &oldName, const std::string &newName);
85 std::shared_ptr<Scope> deepCopyScope();
86 [[nodiscard]] Scope *getChildScope(const std::string &scopeName) const;
87 [[nodiscard]] std::vector<SymbolTableEntry *> getVarsGoingOutOfScope();
88
89 // Generic types
90 void insertGenericType(const std::string &typeName, const GenericType &genericType);
91 GenericType *lookupGenericTypeStrict(const std::string &typeName);
92
93 // Util
94 void collectWarnings(std::vector<CompilerWarning> &warnings) const;
95 void ensureSuccessfulTypeInference() const;
96 [[nodiscard]] size_t getFieldCount() const;
97 [[nodiscard]] std::vector<Function *> getVirtualMethods();
98 [[nodiscard]] std::vector<const Struct *> getAllStructManifestationsInDeclarationOrder() const;
99 [[nodiscard]] unsigned int getLoopNestingDepth() const;
100 [[nodiscard]] bool isInCaseBranch() const;
101 [[nodiscard]] bool isInAsyncScope() const;
102 [[nodiscard]] bool doesAllowUnsafeOperations() const;
103 [[nodiscard]] bool isImportedBy(const Scope *askingScope) const;
104 [[nodiscard]] nlohmann::json getSymbolTableJSON() const;
105 49286 [[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; }
106
107 // Wrapper methods for symbol table
108 ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) {
109
17/34
✓ Branch 9 → 10 taken 414 times.
✗ Branch 9 → 89 not taken.
✓ Branch 13 → 14 taken 1182 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 430 times.
✗ Branch 26 → 68 not taken.
✓ Branch 28 → 29 taken 154 times.
✗ Branch 28 → 92 not taken.
✓ Branch 30 → 31 taken 16 times.
✗ Branch 30 → 63 not taken.
✓ Branch 36 → 37 taken 430 times.
✗ Branch 36 → 74 not taken.
✓ Branch 38 → 39 taken 110 times.
✗ Branch 38 → 73 not taken.
✓ Branch 43 → 44 taken 110 times.
✗ Branch 43 → 71 not taken.
✓ Branch 48 → 49 taken 414 times.
✗ Branch 48 → 78 not taken.
✓ Branch 62 → 63 taken 109 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 3556 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 3787 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 807 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 8772 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 4730 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 8772 times.
✗ Branch 84 → 141 not taken.
✓ Branch 169 → 170 taken 682 times.
✗ Branch 169 → 328 not taken.
63569 return symbolTable.insert(name, declNode);
110 }
111
5/10
✓ Branch 5 → 6 taken 10937 times.
✗ Branch 5 → 68 not taken.
✓ Branch 18 → 19 taken 64679 times.
✗ Branch 18 → 216 not taken.
✓ Branch 24 → 25 taken 18535 times.
✗ Branch 24 → 538 not taken.
✓ Branch 35 → 36 taken 82 times.
✗ Branch 35 → 79 not taken.
✓ Branch 90 → 91 taken 19253 times.
✗ Branch 90 → 524 not taken.
113516 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
112
35/85
✓ Branch 2 → 3 taken 42909 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 165 not taken.
✓ Branch 6 → 7 taken 2119 times.
✗ Branch 6 → 15 not taken.
✗ Branch 6 → 99 not taken.
✗ Branch 6 → 110 not taken.
✓ Branch 7 → 8 taken 2184 times.
✗ Branch 7 → 48 not taken.
✗ Branch 7 → 54 not taken.
✗ Branch 7 → 71 not taken.
✗ Branch 7 → 128 not taken.
✗ Branch 7 → 139 not taken.
✓ Branch 9 → 10 taken 431 times.
✗ Branch 9 → 53 not taken.
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 26 not taken.
✗ Branch 17 → 130 not taken.
✓ Branch 18 → 19 taken 2130 times.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 129 not taken.
✓ Branch 19 → 20 taken 721 times.
✗ Branch 19 → 141 not taken.
✗ Branch 19 → 209 not taken.
✓ Branch 20 → 21 taken 29 times.
✗ Branch 20 → 144 not taken.
✗ Branch 20 → 174 not taken.
✓ Branch 21 → 22 taken 12 times.
✗ Branch 21 → 153 not taken.
✓ Branch 29 → 30 taken 8408 times.
✗ Branch 29 → 64 not taken.
✓ Branch 37 → 38 taken 35 times.
✗ Branch 37 → 275 not taken.
✓ Branch 41 → 42 taken 414 times.
✗ Branch 41 → 68 not taken.
✓ Branch 42 → 43 taken 3681 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 235 not taken.
✓ Branch 44 → 45 taken 3098 times.
✗ Branch 44 → 187 not taken.
✓ Branch 45 → 46 taken 479 times.
✗ Branch 45 → 77 not taken.
✓ Branch 47 → 48 taken 979 times.
✗ Branch 47 → 411 not taken.
✓ Branch 49 → 50 taken 1177 times.
✗ Branch 49 → 101 not taken.
✓ Branch 59 → 60 taken 9595 times.
✗ Branch 59 → 248 not taken.
✓ Branch 60 → 61 taken 3784 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 5647 times.
✗ Branch 61 → 200 not taken.
✓ Branch 65 → 66 taken 2323 times.
✗ Branch 65 → 195 not taken.
✓ Branch 67 → 68 taken 3554 times.
✗ Branch 67 → 315 not taken.
✓ Branch 68 → 69 taken 725 times.
✗ Branch 68 → 137 not taken.
✗ Branch 68 → 188 not taken.
✗ Branch 68 → 199 not taken.
✓ Branch 83 → 84 taken 3309 times.
✗ Branch 83 → 198 not taken.
✓ Branch 86 → 87 taken 271 times.
✗ Branch 86 → 173 not taken.
✓ Branch 87 → 88 taken 153 times.
✗ Branch 87 → 254 not taken.
✓ Branch 89 → 90 taken 3784 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 3554 times.
✗ Branch 96 → 309 not taken.
✓ Branch 116 → 117 taken 2121 times.
✗ Branch 116 → 189 not taken.
✓ Branch 142 → 143 taken 1688 times.
✗ Branch 142 → 281 not taken.
✓ Branch 147 → 148 taken 8073 times.
✗ Branch 147 → 279 not taken.
✓ Branch 153 → 154 taken 4724 times.
✗ Branch 153 → 270 not taken.
✓ Branch 158 → 159 taken 684 times.
✗ Branch 158 → 328 not taken.
✓ Branch 186 → 187 taken 131 times.
✗ Branch 186 → 361 not taken.
✓ Branch 189 → 190 taken 8763 times.
✗ Branch 189 → 355 not taken.
163742 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
113 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
114
14/28
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4452 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 377 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 5427 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 22065 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1498 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 406 times.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 386 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 87 times.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 1350 times.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 3717 times.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 63 times.
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 92 taken 1295 times.
✗ Branch 98 → 99 not taken.
✓ Branch 98 → 100 taken 5158 times.
✗ Branch 170 → 171 not taken.
✓ Branch 170 → 172 taken 396 times.
46677 assert(type == ScopeType::STRUCT);
115
10/23
✓ Branch 7 → 8 taken 377 times.
✗ Branch 7 → 35 not taken.
✓ Branch 8 → 9 taken 5427 times.
✗ Branch 8 → 60 not taken.
✗ Branch 8 → 70 not taken.
✗ Branch 8 → 86 not taken.
✓ Branch 18 → 19 taken 1498 times.
✗ Branch 18 → 120 not taken.
✓ Branch 25 → 26 taken 386 times.
✗ Branch 25 → 111 not taken.
✓ Branch 32 → 33 taken 1350 times.
✗ Branch 32 → 137 not taken.
✓ Branch 49 → 50 taken 3717 times.
✗ Branch 49 → 184 not taken.
✓ Branch 85 → 86 taken 63 times.
✗ Branch 85 → 154 not taken.
✓ Branch 92 → 93 taken 1295 times.
✗ Branch 92 → 190 not taken.
✗ Branch 92 → 200 not taken.
✓ Branch 100 → 101 taken 5158 times.
✗ Branch 100 → 191 not taken.
✓ Branch 172 → 173 taken 396 times.
✗ Branch 172 → 275 not taken.
46677 return symbolTable.lookupStrictByIndex(n);
116 }
117
118 // Public members
119 Scope *parent;
120 SourceFile *sourceFile;
121 std::map<std::string, std::shared_ptr<Scope>> children;
122 SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
123 const CodeLoc *codeLoc = nullptr;
124 const ScopeType type;
125 bool isGenericScope = false;
126 bool isAsyncScope = false;
127 bool isDtorScope = false;
128
129 private:
130 // Private members
131 FunctionRegistry functions;
132 StructRegistry structs;
133 InterfaceRegistry interfaces;
134 std::map<std::string, GenericType> genericTypes;
135 };
136
137 } // namespace spice::compiler
138