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 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 650644 [[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 8176 times.
✗ Branch 9 → 97 not taken.
✓ Branch 13 → 14 taken 11122 times.
✗ Branch 13 → 42 not taken.
✓ Branch 26 → 27 taken 1252 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 1252 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 8176 times.
✗ Branch 55 → 86 not taken.
✓ Branch 62 → 63 taken 819 times.
✗ Branch 62 → 97 not taken.
✓ Branch 63 → 64 taken 25681 times.
✗ Branch 63 → 127 not taken.
✓ Branch 68 → 69 taken 24744 times.
✗ Branch 68 → 126 not taken.
✓ Branch 70 → 71 taken 7665 times.
✗ Branch 70 → 108 not taken.
✓ Branch 72 → 73 taken 50388 times.
✗ Branch 72 → 133 not taken.
✓ Branch 81 → 82 taken 30344 times.
✗ Branch 81 → 134 not taken.
✓ Branch 84 → 85 taken 50388 times.
✗ Branch 84 → 141 not taken.
✓ Branch 225 → 226 taken 6076 times.
✗ Branch 225 → 415 not taken.
411196 return symbolTable.insert(name, declNode);
121 }
122
6/12
✓ Branch 5 → 6 taken 78792 times.
✗ Branch 5 → 98 not taken.
✓ Branch 18 → 19 taken 425160 times.
✗ Branch 18 → 218 not taken.
✓ Branch 23 → 24 taken 135521 times.
✗ Branch 23 → 632 not taken.
✓ Branch 34 → 35 taken 7003 times.
✗ Branch 34 → 78 not taken.
✓ Branch 90 → 91 taken 137258 times.
✗ Branch 90 → 561 not taken.
✓ Branch 244 → 245 taken 152 times.
✗ Branch 244 → 499 not taken.
784394 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
123
38/96
✓ Branch 2 → 3 taken 264712 times.
✗ Branch 2 → 42 not taken.
✗ Branch 2 → 73 not taken.
✗ Branch 2 → 178 not taken.
✓ Branch 6 → 7 taken 21307 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 18448 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 2691 times.
✗ Branch 9 → 23 not taken.
✗ Branch 9 → 53 not taken.
✓ Branch 17 → 18 taken 56 times.
✗ Branch 17 → 26 not taken.
✗ Branch 17 → 129 not taken.
✗ Branch 17 → 142 not taken.
✓ Branch 18 → 19 taken 25129 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 15239 times.
✗ Branch 24 → 222 not taken.
✓ Branch 29 → 30 taken 54079 times.
✗ Branch 29 → 87 not taken.
✓ Branch 37 → 38 taken 643 times.
✗ Branch 37 → 275 not taken.
✓ Branch 41 → 42 taken 1244 times.
✗ Branch 41 → 68 not taken.
✗ Branch 41 → 86 not taken.
✓ Branch 42 → 43 taken 30581 times.
✗ Branch 42 → 140 not taken.
✗ Branch 42 → 233 not taken.
✓ Branch 44 → 45 taken 26758 times.
✗ Branch 44 → 180 not taken.
✓ Branch 45 → 46 taken 5640 times.
✗ Branch 45 → 77 not taken.
✓ Branch 51 → 52 taken 11112 times.
✗ Branch 51 → 103 not taken.
✓ Branch 52 → 53 taken 6 times.
✗ Branch 52 → 99 not taken.
✓ Branch 59 → 60 taken 54447 times.
✗ Branch 59 → 244 not taken.
✓ Branch 60 → 61 taken 24738 times.
✗ Branch 60 → 247 not taken.
✓ Branch 61 → 62 taken 37514 times.
✗ Branch 61 → 191 not taken.
✓ Branch 65 → 66 taken 24343 times.
✗ Branch 65 → 252 not taken.
✓ Branch 67 → 68 taken 25677 times.
✗ Branch 67 → 315 not taken.
✓ Branch 69 → 70 taken 6645 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 951 times.
✗ Branch 72 → 165 not taken.
✓ Branch 82 → 83 taken 27628 times.
✗ Branch 82 → 199 not taken.
✓ Branch 87 → 88 taken 772 times.
✗ Branch 87 → 253 not taken.
✓ Branch 89 → 90 taken 24738 times.
✗ Branch 89 → 241 not taken.
✓ Branch 96 → 97 taken 25677 times.
✗ Branch 96 → 309 not taken.
✓ Branch 113 → 114 taken 304 times.
✗ Branch 113 → 205 not taken.
✓ Branch 117 → 118 taken 20952 times.
✗ Branch 117 → 190 not taken.
✓ Branch 128 → 129 taken 53262 times.
✗ Branch 128 → 269 not taken.
✓ Branch 140 → 141 taken 15605 times.
✗ Branch 140 → 280 not taken.
✓ Branch 153 → 154 taken 30332 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 50370 times.
✗ Branch 189 → 355 not taken.
✓ Branch 214 → 215 taken 6080 times.
✗ Branch 214 → 415 not taken.
1103117 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 13274 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✓ Branch 4 → 7 taken 50368 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 58232 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 191961 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 39741 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✓ Branch 22 → 25 taken 3241 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1448 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 10469 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 5247 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 32097 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 11107 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 57165 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 7436 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.
493573 assert(type == ScopeType::STRUCT || type == ScopeType::UNION);
126
14/35
✓ Branch 8 → 9 taken 2854 times.
✗ Branch 8 → 36 not taken.
✓ Branch 9 → 10 taken 58232 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 39741 times.
✗ Branch 24 → 96 not taken.
✗ Branch 24 → 116 not taken.
✓ Branch 30 → 31 taken 10469 times.
✗ Branch 30 → 79 not taken.
✓ Branch 33 → 34 taken 5247 times.
✗ Branch 33 → 128 not taken.
✓ Branch 44 → 45 taken 14 times.
✗ Branch 44 → 101 not taken.
✓ Branch 50 → 51 taken 32097 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 11119 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 57165 times.
✗ Branch 103 → 248 not taken.
✓ Branch 175 → 176 taken 7436 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.
493573 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