src/symboltablebuilder/SymbolTableEntry.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "SymbolTableEntry.h" | ||
| 4 | |||
| 5 | #include <ast/ASTNodes.h> | ||
| 6 | #include <exception/SemanticError.h> | ||
| 7 | #include <symboltablebuilder/Scope.h> | ||
| 8 | #include <util/CodeLoc.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Retrieve the qualified type of this symbol | ||
| 14 | * | ||
| 15 | * @return Qualified type of this symbol | ||
| 16 | */ | ||
| 17 | 20604408 | const QualType &SymbolTableEntry::getQualType() const { return qualType; } | |
| 18 | |||
| 19 | /** | ||
| 20 | * Update the type of this symbol. | ||
| 21 | * | ||
| 22 | * @param newType New type of the current symbol | ||
| 23 | * @param overwriteExistingType Overwrites the existing type without throwing an error | ||
| 24 | */ | ||
| 25 | 237480 | void SymbolTableEntry::updateType(const QualType &newType, [[maybe_unused]] bool overwriteExistingType) { | |
| 26 |
4/6✓ Branch 2 → 3 taken 109612 times.
✓ Branch 2 → 6 taken 127868 times.
✓ Branch 3 → 4 taken 109612 times.
✗ Branch 3 → 7 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 109612 times.
|
237480 | assert(overwriteExistingType || qualType.isOneOf({TY_INVALID, TY_DYN})); |
| 27 | 237480 | qualType = newType; | |
| 28 | 237480 | } | |
| 29 | |||
| 30 | /** | ||
| 31 | * Update the state of the current symbol | ||
| 32 | * | ||
| 33 | * @throws CompilerError When the state of the symbol is set to initialized before a concrete type was set | ||
| 34 | * | ||
| 35 | * @param newState New state of the current symbol | ||
| 36 | * @param node AST node where the update takes place | ||
| 37 | */ | ||
| 38 | 347543 | void SymbolTableEntry::updateState(const LifecycleState &newState, const ASTNode *node) { | |
| 39 | 347543 | const LifecycleState oldState = lifecycle.getCurrentState(); | |
| 40 | − | if (newState == DEAD && oldState == DECLARED) // GCOV_EXCL_LINE | |
| 41 | − | throw CompilerError(INTERNAL_ERROR, "Cannot destroy uninitialized variable '" + name + "'"); // GCOV_EXCL_LINE | |
| 42 | − | if (newState == DEAD && oldState == DEAD) // GCOV_EXCL_LINE | |
| 43 | − | throw CompilerError(INTERNAL_ERROR, "Cannot destroy already destroyed variable '" + name + "'"); // GCOV_EXCL_LINE | |
| 44 |
1/2✓ Branch 21 → 22 taken 347543 times.
✗ Branch 21 → 41 not taken.
|
347543 | lifecycle.addEvent({newState, node}); |
| 45 | 347543 | } | |
| 46 | |||
| 47 | /** | ||
| 48 | * Retrieve the code location where the symbol was declared | ||
| 49 | * | ||
| 50 | * @return Declaration code location | ||
| 51 | */ | ||
| 52 | 121 | const CodeLoc &SymbolTableEntry::getDeclCodeLoc() const { return declNode->codeLoc; } | |
| 53 | |||
| 54 | /** | ||
| 55 | * Check if this symbol is a struct field | ||
| 56 | * | ||
| 57 | * @return Struct field or not | ||
| 58 | */ | ||
| 59 | 89366 | bool SymbolTableEntry::isField() const { | |
| 60 |
5/6✓ Branch 2 → 3 taken 77237 times.
✓ Branch 2 → 7 taken 12129 times.
✓ Branch 4 → 5 taken 77198 times.
✓ Branch 4 → 7 taken 39 times.
✓ Branch 5 → 6 taken 77198 times.
✗ Branch 5 → 7 not taken.
|
89366 | return scope->type == ScopeType::STRUCT && orderIndex < scope->getFieldCount() && !anonymous; |
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Stringify the current symbol to a human-readable form. Used to dump whole symbol tables with their contents. | ||
| 65 | * | ||
| 66 | * Example: | ||
| 67 | * { | ||
| 68 | * "name": "testIden", | ||
| 69 | * "type": "int[]*", | ||
| 70 | * "orderIndex": 4, | ||
| 71 | * "state": "initialized", | ||
| 72 | * "qualifiers: [ | ||
| 73 | * "const": true, | ||
| 74 | * "signed": false | ||
| 75 | * ], | ||
| 76 | * "isGlobal": false, | ||
| 77 | * "isVolatile": false | ||
| 78 | * } | ||
| 79 | * | ||
| 80 | * @return Symbol table entry as a JSON object | ||
| 81 | */ | ||
| 82 | 934417 | nlohmann::ordered_json SymbolTableEntry::toJSON() const { | |
| 83 | 934417 | nlohmann::json result; | |
| 84 |
2/4✓ Branch 3 → 4 taken 934417 times.
✗ Branch 3 → 43 not taken.
✓ Branch 4 → 5 taken 934417 times.
✗ Branch 4 → 41 not taken.
|
934417 | result["name"] = name; |
| 85 |
3/6✓ Branch 7 → 8 taken 934417 times.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 934417 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 934417 times.
✗ Branch 9 → 44 not taken.
|
934417 | result["type"] = qualType.getName(true); |
| 86 |
3/6✓ Branch 13 → 14 taken 934417 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 934417 times.
✗ Branch 14 → 52 not taken.
✓ Branch 15 → 16 taken 934417 times.
✗ Branch 15 → 50 not taken.
|
934417 | result["codeLoc"] = declNode->codeLoc.toString(); |
| 87 |
1/2✓ Branch 20 → 21 taken 934417 times.
✗ Branch 20 → 56 not taken.
|
934417 | result["orderIndex"] = orderIndex; |
| 88 |
3/6✓ Branch 23 → 24 taken 934417 times.
✗ Branch 23 → 61 not taken.
✓ Branch 24 → 25 taken 934417 times.
✗ Branch 24 → 61 not taken.
✓ Branch 25 → 26 taken 934417 times.
✗ Branch 25 → 59 not taken.
|
934417 | result["state"] = lifecycle.getCurrentStateName(); |
| 89 |
1/2✓ Branch 29 → 30 taken 934417 times.
✗ Branch 29 → 63 not taken.
|
934417 | result["isGlobal"] = global; |
| 90 |
1/2✓ Branch 33 → 34 taken 934417 times.
✗ Branch 33 → 66 not taken.
|
934417 | result["isVolatile"] = isVolatile; |
| 91 |
1/2✓ Branch 36 → 37 taken 934417 times.
✗ Branch 36 → 69 not taken.
|
1868834 | return result; |
| 92 | 934417 | } | |
| 93 | |||
| 94 | } // namespace spice::compiler | ||
| 95 |