| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <model/Interface.h> | ||
| 6 | #include <model/Struct.h> | ||
| 7 | #include <symboltablebuilder/SymbolTable.h> | ||
| 8 | #include <typechecker/FunctionManager.h> | ||
| 9 | #include <typechecker/InterfaceManager.h> | ||
| 10 | #include <typechecker/StructManager.h> | ||
| 11 | #include <util/CompilerWarning.h> | ||
| 12 | |||
| 13 | namespace spice::compiler { | ||
| 14 | |||
| 15 | // Forward declarations | ||
| 16 | class FctDefNode; | ||
| 17 | class ProcDefNode; | ||
| 18 | class SourceFile; | ||
| 19 | |||
| 20 | enum class ScopeType : uint8_t { | ||
| 21 | GLOBAL, | ||
| 22 | FUNC_PROC_BODY, | ||
| 23 | LAMBDA_BODY, | ||
| 24 | STRUCT, | ||
| 25 | INTERFACE, | ||
| 26 | ENUM, | ||
| 27 | IF_ELSE_BODY, | ||
| 28 | WHILE_BODY, | ||
| 29 | FOR_BODY, | ||
| 30 | FOREACH_BODY, | ||
| 31 | CASE_BODY, | ||
| 32 | DEFAULT_BODY, | ||
| 33 | UNSAFE_BODY, | ||
| 34 | ANONYMOUS_BLOCK_BODY | ||
| 35 | }; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Represents an enclosed group of instructions, which are semantically separated from other scopes. | ||
| 39 | * In the source code, scopes usually are written as curly braces. | ||
| 40 | * | ||
| 41 | * Following language structures use scopes: | ||
| 42 | * - global scope | ||
| 43 | * - functions/procedures | ||
| 44 | * - structs | ||
| 45 | * - enums | ||
| 46 | * - interfaces | ||
| 47 | * - thread blocks | ||
| 48 | * - unsafe blocks | ||
| 49 | * - for loops | ||
| 50 | * - foreach loops | ||
| 51 | * - while loops | ||
| 52 | * - if statements | ||
| 53 | * - anonymous scopes | ||
| 54 | */ | ||
| 55 | class Scope { | ||
| 56 | public: | ||
| 57 | // Constructors | ||
| 58 | 24071 | Scope(Scope *parent, SourceFile *sourceFile, ScopeType scopeType, const CodeLoc *codeLoc) | |
| 59 |
2/2✓ Branch 3 → 4 taken 22938 times.
✓ Branch 3 → 5 taken 1133 times.
|
24071 | : parent(parent), sourceFile(sourceFile), codeLoc(codeLoc), type(scopeType) {} |
| 60 | |||
| 61 | // Friend classes | ||
| 62 | friend class FunctionManager; | ||
| 63 | friend class StructManager; | ||
| 64 | friend class InterfaceManager; | ||
| 65 | |||
| 66 | // Public methods | ||
| 67 | // Scope management | ||
| 68 | Scope *createChildScope(const std::string &scopeName, ScopeType scopeType, const CodeLoc *declCodeLoc); | ||
| 69 | void renameChildScope(const std::string &oldName, const std::string &newName); | ||
| 70 | Scope *copyChildScope(const std::string &oldName, const std::string &newName); | ||
| 71 | std::shared_ptr<Scope> deepCopyScope(); | ||
| 72 | [[nodiscard]] Scope *getChildScope(const std::string &scopeName) const; | ||
| 73 | [[nodiscard]] std::vector<SymbolTableEntry *> getVarsGoingOutOfScope(); | ||
| 74 | |||
| 75 | // Generic types | ||
| 76 | void insertGenericType(const std::string &typeName, const GenericType &genericType); | ||
| 77 | GenericType *lookupGenericTypeStrict(const std::string &typeName); | ||
| 78 | |||
| 79 | // Util | ||
| 80 | void collectWarnings(std::vector<CompilerWarning> &warnings) const; | ||
| 81 | void ensureSuccessfulTypeInference() const; | ||
| 82 | [[nodiscard]] size_t getFieldCount() const; | ||
| 83 | [[nodiscard]] std::vector<Function *> getVirtualMethods(); | ||
| 84 | [[nodiscard]] std::vector<const Struct *> getAllStructManifestationsInDeclarationOrder() const; | ||
| 85 | [[nodiscard]] bool hasRefFields(); | ||
| 86 | [[nodiscard]] unsigned int getLoopNestingDepth() const; | ||
| 87 | [[nodiscard]] bool isInCaseBranch() const; | ||
| 88 | [[nodiscard]] bool isInAsyncScope() const; | ||
| 89 | [[nodiscard]] bool doesAllowUnsafeOperations() const; | ||
| 90 | [[nodiscard]] bool isImportedBy(const Scope *askingScope) const; | ||
| 91 | [[nodiscard]] nlohmann::json getSymbolTableJSON() const; | ||
| 92 | 41219 | [[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; } | |
| 93 | |||
| 94 | // Wrapper methods for symbol table | ||
| 95 | ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) { | ||
| 96 |
13/28✓ Branch 9 → 10 taken 359 times.
✗ Branch 9 → 66 not taken.
✓ Branch 13 → 14 taken 742 times.
✗ Branch 13 → 41 not taken.
✓ Branch 26 → 27 taken 518 times.
✗ Branch 26 → 67 not taken.
✗ Branch 26 → 74 not taken.
✓ Branch 29 → 30 taken 359 times.
✗ Branch 29 → 55 not taken.
✓ Branch 30 → 31 taken 12 times.
✗ Branch 30 → 62 not taken.
✓ Branch 31 → 32 taken 70 times.
✗ Branch 31 → 65 not taken.
✓ Branch 36 → 37 taken 458 times.
✗ Branch 36 → 63 not taken.
✗ Branch 36 → 73 not taken.
✓ Branch 52 → 53 taken 3055 times.
✗ Branch 52 → 112 not taken.
✓ Branch 57 → 58 taken 3197 times.
✗ Branch 57 → 111 not taken.
✓ Branch 61 → 62 taken 7595 times.
✗ Branch 61 → 118 not taken.
✓ Branch 70 → 71 taken 3786 times.
✗ Branch 70 → 119 not taken.
✓ Branch 73 → 74 taken 7595 times.
✗ Branch 73 → 126 not taken.
✓ Branch 138 → 139 taken 600 times.
✗ Branch 138 → 274 not taken.
|
53522 | return symbolTable.insert(name, declNode); |
| 97 | } | ||
| 98 |
5/10✓ Branch 5 → 6 taken 9393 times.
✗ Branch 5 → 66 not taken.
✓ Branch 12 → 13 taken 14497 times.
✗ Branch 12 → 535 not taken.
✓ Branch 21 → 22 taken 50498 times.
✗ Branch 21 → 219 not taken.
✓ Branch 29 → 30 taken 15168 times.
✗ Branch 29 → 502 not taken.
✓ Branch 36 → 37 taken 66 times.
✗ Branch 36 → 79 not taken.
|
89652 | ALWAYS_INLINE SymbolTableEntry *lookup(const std::string &symbolName) { return symbolTable.lookup(symbolName); } |
| 99 |
32/80✓ Branch 2 → 3 taken 32990 times.
✗ Branch 2 → 41 not taken.
✗ Branch 2 → 65 not taken.
✗ Branch 2 → 179 not taken.
✓ Branch 6 → 7 taken 999 times.
✗ Branch 6 → 16 not taken.
✓ Branch 7 → 8 taken 1863 times.
✗ Branch 7 → 45 not taken.
✗ Branch 7 → 48 not taken.
✗ Branch 7 → 73 not taken.
✗ Branch 7 → 128 not taken.
✗ Branch 7 → 141 not taken.
✓ Branch 9 → 10 taken 389 times.
✗ Branch 9 → 52 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 117 not taken.
✓ Branch 18 → 19 taken 1810 times.
✗ Branch 18 → 28 not taken.
✗ Branch 18 → 47 not taken.
✗ Branch 18 → 116 not taken.
✗ Branch 18 → 133 not taken.
✓ Branch 19 → 20 taken 2 times.
✗ Branch 19 → 140 not taken.
✓ Branch 20 → 21 taken 13 times.
✗ Branch 20 → 143 not taken.
✗ Branch 20 → 173 not taken.
✓ Branch 30 → 31 taken 7214 times.
✗ Branch 30 → 60 not taken.
✓ Branch 33 → 34 taken 372 times.
✗ Branch 33 → 59 not taken.
✓ Branch 35 → 36 taken 1575 times.
✗ Branch 35 → 74 not taken.
✗ Branch 35 → 407 not taken.
✓ Branch 37 → 38 taken 443 times.
✗ Branch 37 → 69 not taken.
✗ Branch 37 → 267 not taken.
✓ Branch 42 → 43 taken 10 times.
✗ Branch 42 → 132 not taken.
✓ Branch 43 → 44 taken 3132 times.
✗ Branch 43 → 247 not taken.
✓ Branch 45 → 46 taken 2584 times.
✗ Branch 45 → 202 not taken.
✓ Branch 51 → 52 taken 3194 times.
✗ Branch 51 → 219 not taken.
✓ Branch 56 → 57 taken 3053 times.
✗ Branch 56 → 282 not taken.
✓ Branch 60 → 61 taken 8202 times.
✗ Branch 60 → 259 not taken.
✓ Branch 62 → 63 taken 2244 times.
✗ Branch 62 → 214 not taken.
✓ Branch 63 → 64 taken 601 times.
✗ Branch 63 → 137 not taken.
✗ Branch 63 → 189 not taken.
✓ Branch 64 → 65 taken 1943 times.
✗ Branch 64 → 196 not taken.
✓ Branch 69 → 70 taken 8 times.
✗ Branch 69 → 187 not taken.
✓ Branch 70 → 71 taken 238 times.
✗ Branch 70 → 152 not taken.
✓ Branch 72 → 73 taken 3194 times.
✗ Branch 72 → 213 not taken.
✓ Branch 77 → 78 taken 3182 times.
✗ Branch 77 → 227 not taken.
✗ Branch 77 → 276 not taken.
✓ Branch 79 → 80 taken 2212 times.
✗ Branch 79 → 171 not taken.
✓ Branch 112 → 113 taken 1791 times.
✗ Branch 112 → 162 not taken.
✓ Branch 123 → 124 taken 1459 times.
✗ Branch 123 → 254 not taken.
✓ Branch 126 → 127 taken 3780 times.
✗ Branch 126 → 242 not taken.
✓ Branch 127 → 128 taken 602 times.
✗ Branch 127 → 274 not taken.
✓ Branch 157 → 158 taken 7586 times.
✗ Branch 157 → 322 not taken.
✓ Branch 167 → 168 taken 6892 times.
✗ Branch 167 → 292 not taken.
✓ Branch 174 → 175 taken 111 times.
✗ Branch 174 → 344 not taken.
|
131453 | ALWAYS_INLINE SymbolTableEntry *lookupStrict(const std::string &symbolName) { return symbolTable.lookupStrict(symbolName); } |
| 100 | ALWAYS_INLINE SymbolTableEntry *lookupField(unsigned int n) { | ||
| 101 |
14/28✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1227 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 4553 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 17793 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1292 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 366 times.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 352 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 83 times.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 1182 times.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 3134 times.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 66 times.
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 87 taken 1039 times.
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 91 taken 70 times.
✗ Branch 96 → 97 not taken.
✓ Branch 96 → 98 taken 4392 times.
✗ Branch 149 → 150 not taken.
✓ Branch 149 → 151 taken 413 times.
|
35962 | assert(type == ScopeType::STRUCT); |
| 102 |
11/24✓ Branch 7 → 8 taken 313 times.
✗ Branch 7 → 27 not taken.
✓ Branch 8 → 9 taken 4553 times.
✗ Branch 8 → 59 not taken.
✗ Branch 8 → 64 not taken.
✗ Branch 8 → 76 not taken.
✓ Branch 18 → 19 taken 1292 times.
✗ Branch 18 → 141 not taken.
✓ Branch 25 → 26 taken 352 times.
✗ Branch 25 → 114 not taken.
✓ Branch 32 → 33 taken 1182 times.
✗ Branch 32 → 151 not taken.
✓ Branch 49 → 50 taken 3134 times.
✗ Branch 49 → 184 not taken.
✓ Branch 85 → 86 taken 66 times.
✗ Branch 85 → 154 not taken.
✓ Branch 87 → 88 taken 1039 times.
✗ Branch 87 → 180 not taken.
✓ Branch 91 → 92 taken 70 times.
✗ Branch 91 → 201 not taken.
✓ Branch 98 → 99 taken 4392 times.
✗ Branch 98 → 185 not taken.
✓ Branch 151 → 152 taken 413 times.
✗ Branch 151 → 253 not taken.
|
35962 | return symbolTable.lookupStrictByIndex(n); |
| 103 | } | ||
| 104 | |||
| 105 | // Public members | ||
| 106 | Scope *parent; | ||
| 107 | SourceFile *sourceFile; | ||
| 108 | std::unordered_map<std::string, std::shared_ptr<Scope>> children; | ||
| 109 | SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this); | ||
| 110 | const CodeLoc *codeLoc = nullptr; | ||
| 111 | const ScopeType type; | ||
| 112 | bool isGenericScope = false; | ||
| 113 | bool isAsyncScope = false; | ||
| 114 | bool isDtorScope = false; | ||
| 115 | |||
| 116 | private: | ||
| 117 | // Private members | ||
| 118 | FunctionRegistry functions; | ||
| 119 | StructRegistry structs; | ||
| 120 | InterfaceRegistry interfaces; | ||
| 121 | std::map<std::string, GenericType> genericTypes; | ||
| 122 | }; | ||
| 123 | |||
| 124 | } // namespace spice::compiler | ||
| 125 |