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: 44.1% 79 / 0 / 179

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 45808 [[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
15/32
✓ Branch 9 → 10 taken 370 times.
✗ Branch 9 → 81 not taken.
✓ Branch 13 → 14 taken 735 times.
✗ Branch 13 → 41 not taken.
✓ Branch 26 → 27 taken 409 times.
✗ Branch 26 → 67 not taken.
✓ Branch 27 → 28 taken 143 times.
✗ Branch 27 → 76 not taken.
✓ Branch 30 → 31 taken 16 times.
✗ Branch 30 → 62 not taken.
✓ Branch 31 → 32 taken 70 times.
✗ Branch 31 → 65 not taken.
✓ Branch 36 → 37 taken 479 times.
✗ Branch 36 → 63 not taken.
✗ Branch 36 → 73 not taken.
✓ Branch 40 → 41 taken 370 times.
✗ Branch 40 → 70 not taken.
✓ Branch 47 → 48 taken 107 times.
✗ Branch 47 → 81 not taken.
✓ Branch 55 → 56 taken 4055 times.
✗ Branch 55 → 92 not taken.
✗ Branch 55 → 118 not taken.
✓ Branch 60 → 61 taken 3495 times.
✗ Branch 60 → 117 not taken.
✓ Branch 64 → 65 taken 8233 times.
✗ Branch 64 → 124 not taken.
✓ Branch 73 → 74 taken 4259 times.
✗ Branch 73 → 125 not taken.
✓ Branch 76 → 77 taken 8233 times.
✗ Branch 76 → 132 not taken.
✓ Branch 169 → 170 taken 650 times.
✗ Branch 169 → 327 not taken.
58420 return symbolTable.insert(name, declNode);
110 }
111
5/10
✓ Branch 5 → 6 taken 10158 times.
✗ Branch 5 → 66 not taken.
✓ Branch 16 → 17 taken 17006 times.
✗ Branch 16 → 522 not taken.
✓ Branch 21 → 22 taken 59496 times.
✗ Branch 21 → 219 not taken.
✓ Branch 29 → 30 taken 17677 times.
✗ Branch 29 → 502 not taken.
✓ Branch 36 → 37 taken 70 times.
✗ Branch 36 → 79 not taken.
104437 ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); }
112
33/83
✓ Branch 2 → 3 taken 39109 times.
✗ Branch 2 → 41 not taken.
✗ Branch 2 → 65 not taken.
✗ Branch 2 → 165 not taken.
✓ Branch 6 → 7 taken 1843 times.
✗ Branch 6 → 16 not taken.
✗ Branch 6 → 83 not taken.
✗ Branch 6 → 94 not taken.
✓ Branch 7 → 8 taken 1989 times.
✗ Branch 7 → 45 not taken.
✗ Branch 7 → 48 not taken.
✗ Branch 7 → 71 not taken.
✗ Branch 7 → 128 not taken.
✗ Branch 7 → 139 not taken.
✓ Branch 9 → 10 taken 410 times.
✗ Branch 9 → 52 not taken.
✓ Branch 17 → 18 taken 575 times.
✗ Branch 17 → 125 not taken.
✗ Branch 17 → 165 not taken.
✓ Branch 18 → 19 taken 1923 times.
✗ Branch 18 → 28 not taken.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 124 not taken.
✗ Branch 18 → 138 not taken.
✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 139 not taken.
✓ Branch 20 → 21 taken 29 times.
✗ Branch 20 → 142 not taken.
✗ Branch 20 → 172 not taken.
✓ Branch 28 → 29 taken 7760 times.
✗ Branch 28 → 55 not taken.
✓ Branch 33 → 34 taken 393 times.
✗ Branch 33 → 59 not taken.
✓ Branch 35 → 36 taken 730 times.
✗ Branch 35 → 74 not taken.
✓ Branch 37 → 38 taken 464 times.
✗ Branch 37 → 69 not taken.
✗ Branch 37 → 267 not taken.
✓ Branch 39 → 40 taken 850 times.
✗ Branch 39 → 395 not taken.
✓ Branch 41 → 42 taken 3395 times.
✗ Branch 41 → 220 not taken.
✓ Branch 42 → 43 taken 14 times.
✗ Branch 42 → 132 not taken.
✓ Branch 43 → 44 taken 2795 times.
✗ Branch 43 → 174 not taken.
✓ Branch 51 → 52 taken 3492 times.
✗ Branch 51 → 219 not taken.
✓ Branch 56 → 57 taken 3319 times.
✗ Branch 56 → 282 not taken.
✓ Branch 58 → 59 taken 8885 times.
✗ Branch 58 → 233 not taken.
✓ Branch 60 → 61 taken 5154 times.
✗ Branch 60 → 187 not taken.
✓ Branch 64 → 65 taken 2713 times.
✗ Branch 64 → 137 not taken.
✗ Branch 64 → 190 not taken.
✗ Branch 64 → 196 not taken.
✓ Branch 68 → 69 taken 12 times.
✗ Branch 68 → 186 not taken.
✓ Branch 72 → 73 taken 3492 times.
✗ Branch 72 → 213 not taken.
✓ Branch 76 → 77 taken 254 times.
✗ Branch 76 → 158 not taken.
✓ Branch 77 → 78 taken 3461 times.
✗ Branch 77 → 227 not taken.
✗ Branch 77 → 276 not taken.
✓ Branch 80 → 81 taken 3016 times.
✗ Branch 80 → 172 not taken.
✓ Branch 113 → 114 taken 1887 times.
✗ Branch 113 → 163 not taken.
✓ Branch 123 → 124 taken 1559 times.
✗ Branch 123 → 254 not taken.
✓ Branch 126 → 127 taken 4253 times.
✗ Branch 126 → 242 not taken.
✓ Branch 145 → 146 taken 7438 times.
✗ Branch 145 → 264 not taken.
✓ Branch 157 → 158 taken 8224 times.
✗ Branch 157 → 322 not taken.
✓ Branch 158 → 159 taken 652 times.
✗ Branch 158 → 327 not taken.
✓ Branch 174 → 175 taken 116 times.
✗ Branch 174 → 343 not taken.
149718 ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); }
113 ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) {
114
15/30
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3865 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 334 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 4830 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 19657 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1384 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 382 times.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 360 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 77 times.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 1219 times.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 3375 times.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 63 times.
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 1095 times.
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 91 taken 76 times.
✗ Branch 96 → 97 not taken.
✓ Branch 96 → 98 taken 4652 times.
✗ Branch 149 → 150 not taken.
✓ Branch 149 → 151 taken 413 times.
41782 assert(type == ScopeType::STRUCT);
115
11/24
✓ Branch 7 → 8 taken 334 times.
✗ Branch 7 → 27 not taken.
✓ Branch 8 → 9 taken 4830 times.
✗ Branch 8 → 59 not taken.
✗ Branch 8 → 70 not taken.
✗ Branch 8 → 86 not taken.
✓ Branch 18 → 19 taken 1384 times.
✗ Branch 18 → 120 not taken.
✓ Branch 25 → 26 taken 360 times.
✗ Branch 25 → 111 not taken.
✓ Branch 32 → 33 taken 1219 times.
✗ Branch 32 → 137 not taken.
✓ Branch 49 → 50 taken 3375 times.
✗ Branch 49 → 184 not taken.
✓ Branch 85 → 86 taken 63 times.
✗ Branch 85 → 154 not taken.
✓ Branch 88 → 89 taken 1095 times.
✗ Branch 88 → 181 not taken.
✓ Branch 91 → 92 taken 76 times.
✗ Branch 91 → 201 not taken.
✓ Branch 98 → 99 taken 4652 times.
✗ Branch 98 → 185 not taken.
✓ Branch 151 → 152 taken 413 times.
✗ Branch 151 → 253 not taken.
41782 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