GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 40 / 4 / 44
Functions: 100.0% 10 / 0 / 10
Branches: 59.4% 38 / 20 / 84

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 1790577 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 80710 void SymbolTableEntry::updateType(const QualType &newType, [[maybe_unused]] bool overwriteExistingType) {
26
4/6
✓ Branch 2 → 3 taken 36871 times.
✓ Branch 2 → 6 taken 43839 times.
✓ Branch 3 → 4 taken 36871 times.
✗ Branch 3 → 7 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 36871 times.
80710 assert(overwriteExistingType || qualType.isOneOf({TY_INVALID, TY_DYN}));
27 80710 qualType = newType;
28 80710 }
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 121573 void SymbolTableEntry::updateState(const LifecycleState &newState, const ASTNode *node) {
39 121573 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 121573 times.
✗ Branch 21 → 41 not taken.
121573 lifecycle.addEvent({newState, node});
45 121573 }
46
47 /**
48 * Retrieve the code location where the symbol was declared
49 *
50 * @return Declaration code location
51 */
52 136 const CodeLoc &SymbolTableEntry::getDeclCodeLoc() const { return declNode->codeLoc; }
53
54 /**
55 * Retrieve the address of the assigned value
56 *
57 * @return Address of the value in memory
58 */
59
2/2
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 5 taken 74312 times.
74318 llvm::Value *SymbolTableEntry::getAddress() const { return memAddress.empty() ? nullptr : memAddress.top(); }
60
61 /**
62 * Update the address of a symbol. This is used to save the allocated address where the symbol lives.
63 *
64 * @param address Address of the symbol in memory
65 */
66 57865 void SymbolTableEntry::updateAddress(llvm::Value *address) {
67
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 57865 times.
57865 assert(address != nullptr);
68
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 57865 times.
57865 assert(address->getType()->isPointerTy());
69 // Ensure that structs fields get no addresses assigned, as the addresses are meant for the struct instances
70
5/8
✓ Branch 8 → 9 taken 51675 times.
✓ Branch 8 → 11 taken 6190 times.
✓ Branch 9 → 10 taken 51675 times.
✗ Branch 9 → 11 not taken.
✓ Branch 11 → 12 taken 6190 times.
✗ Branch 11 → 20 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6190 times.
57865 assert((scope->type != ScopeType::STRUCT && scope->type != ScopeType::INTERFACE) ||
71 qualType.isOneOf({TY_FUNCTION, TY_PROCEDURE}));
72
2/2
✓ Branch 15 → 16 taken 47524 times.
✓ Branch 15 → 17 taken 10341 times.
57865 if (memAddress.empty())
73 47524 memAddress.push(address);
74 else
75 10341 memAddress.top() = address;
76 57865 }
77
78 /**
79 * Push a new address to the stack
80 *
81 * @param address Address to push
82 */
83 24 void SymbolTableEntry::pushAddress(llvm::Value *address) {
84
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 24 times.
24 assert(address != nullptr);
85 24 memAddress.push(address);
86 24 }
87
88 /**
89 * Remove the last address from the stack
90 */
91 24 void SymbolTableEntry::popAddress() {
92
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24 times.
24 assert(!memAddress.empty());
93 24 memAddress.pop();
94 24 }
95
96 /**
97 * Check if this symbol is a struct field
98 *
99 * @return Struct field or not
100 */
101 17498 bool SymbolTableEntry::isField() const {
102
5/6
✓ Branch 2 → 3 taken 15039 times.
✓ Branch 2 → 7 taken 2459 times.
✓ Branch 4 → 5 taken 14996 times.
✓ Branch 4 → 7 taken 43 times.
✓ Branch 5 → 6 taken 14996 times.
✗ Branch 5 → 7 not taken.
17498 return scope->type == ScopeType::STRUCT && orderIndex < scope->getFieldCount() && !anonymous;
103 }
104
105 /**
106 * Stringify the current symbol to a human-readable form. Used to dump whole symbol tables with their contents.
107 *
108 * Example:
109 * {
110 * "name": "testIden",
111 * "type": "int[]*",
112 * "orderIndex": 4,
113 * "state": "initialized",
114 * "qualifiers: [
115 * "const": true,
116 * "signed": false
117 * ],
118 * "isGlobal": false,
119 * "isVolatile": false
120 * }
121 *
122 * @return Symbol table entry as a JSON object
123 */
124 182056 nlohmann::ordered_json SymbolTableEntry::toJSON() const {
125 182056 nlohmann::json result;
126
2/4
✓ Branch 3 → 4 taken 182056 times.
✗ Branch 3 → 43 not taken.
✓ Branch 4 → 5 taken 182056 times.
✗ Branch 4 → 41 not taken.
182056 result["name"] = name;
127
3/6
✓ Branch 7 → 8 taken 182056 times.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 182056 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 182056 times.
✗ Branch 9 → 44 not taken.
182056 result["type"] = qualType.getName(true);
128
3/6
✓ Branch 13 → 14 taken 182056 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 182056 times.
✗ Branch 14 → 52 not taken.
✓ Branch 15 → 16 taken 182056 times.
✗ Branch 15 → 50 not taken.
182056 result["codeLoc"] = declNode->codeLoc.toString();
129
1/2
✓ Branch 20 → 21 taken 182056 times.
✗ Branch 20 → 56 not taken.
182056 result["orderIndex"] = orderIndex;
130
3/6
✓ Branch 23 → 24 taken 182056 times.
✗ Branch 23 → 61 not taken.
✓ Branch 24 → 25 taken 182056 times.
✗ Branch 24 → 61 not taken.
✓ Branch 25 → 26 taken 182056 times.
✗ Branch 25 → 59 not taken.
182056 result["state"] = lifecycle.getCurrentStateName();
131
1/2
✓ Branch 29 → 30 taken 182056 times.
✗ Branch 29 → 63 not taken.
182056 result["isGlobal"] = global;
132
1/2
✓ Branch 33 → 34 taken 182056 times.
✗ Branch 33 → 66 not taken.
182056 result["isVolatile"] = isVolatile;
133
1/2
✓ Branch 36 → 37 taken 182056 times.
✗ Branch 36 → 69 not taken.
364112 return result;
134 182056 }
135
136 } // namespace spice::compiler
137