GCC Code Coverage Report


Directory: ../
File: src/symboltablebuilder/SymbolTableEntry.cpp
Date: 2025-12-19 06:54:40
Coverage Exec Excl Total
Lines: 100.0% 40 4 44
Functions: 100.0% 10 0 10
Branches: 59.4% 38 20 84

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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 1782048 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 79630 void SymbolTableEntry::updateType(const QualType &newType, [[maybe_unused]] bool overwriteExistingType) {
26
4/6
✓ Branch 2 → 3 taken 36347 times.
✓ Branch 2 → 6 taken 43283 times.
✓ Branch 3 → 4 taken 36347 times.
✗ Branch 3 → 7 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 36347 times.
79630 assert(overwriteExistingType || qualType.isOneOf({TY_INVALID, TY_DYN}));
27 79630 qualType = newType;
28 79630 }
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 112347 void SymbolTableEntry::updateState(const LifecycleState &newState, const ASTNode *node) {
39 112347 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 112347 times.
✗ Branch 21 → 41 not taken.
112347 lifecycle.addEvent({newState, node});
45 112347 }
46
47 /**
48 * Retrieve the code location where the symbol was declared
49 *
50 * @return Declaration code location
51 */
52 125 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 73187 times.
73193 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 57034 void SymbolTableEntry::updateAddress(llvm::Value *address) {
67
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 57034 times.
57034 assert(address != nullptr);
68
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 57034 times.
57034 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 50949 times.
✓ Branch 8 → 11 taken 6085 times.
✓ Branch 9 → 10 taken 50949 times.
✗ Branch 9 → 11 not taken.
✓ Branch 11 → 12 taken 6085 times.
✗ Branch 11 → 20 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6085 times.
57034 assert((scope->type != ScopeType::STRUCT && scope->type != ScopeType::INTERFACE) ||
71 qualType.isOneOf({TY_FUNCTION, TY_PROCEDURE}));
72
2/2
✓ Branch 15 → 16 taken 46840 times.
✓ Branch 15 → 17 taken 10194 times.
57034 if (memAddress.empty())
73 46840 memAddress.push(address);
74 else
75 10194 memAddress.top() = address;
76 57034 }
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 17272 bool SymbolTableEntry::isField() const {
102
5/6
✓ Branch 2 → 3 taken 14847 times.
✓ Branch 2 → 7 taken 2425 times.
✓ Branch 4 → 5 taken 14806 times.
✓ Branch 4 → 7 taken 41 times.
✓ Branch 5 → 6 taken 14806 times.
✗ Branch 5 → 7 not taken.
17272 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 180574 nlohmann::ordered_json SymbolTableEntry::toJSON() const {
125 180574 nlohmann::json result;
126
2/4
✓ Branch 3 → 4 taken 180574 times.
✗ Branch 3 → 43 not taken.
✓ Branch 4 → 5 taken 180574 times.
✗ Branch 4 → 41 not taken.
180574 result["name"] = name;
127
3/6
✓ Branch 7 → 8 taken 180574 times.
✗ Branch 7 → 48 not taken.
✓ Branch 8 → 9 taken 180574 times.
✗ Branch 8 → 46 not taken.
✓ Branch 9 → 10 taken 180574 times.
✗ Branch 9 → 44 not taken.
180574 result["type"] = qualType.getName(true);
128
3/6
✓ Branch 13 → 14 taken 180574 times.
✗ Branch 13 → 54 not taken.
✓ Branch 14 → 15 taken 180574 times.
✗ Branch 14 → 52 not taken.
✓ Branch 15 → 16 taken 180574 times.
✗ Branch 15 → 50 not taken.
180574 result["codeLoc"] = declNode->codeLoc.toString();
129
1/2
✓ Branch 20 → 21 taken 180574 times.
✗ Branch 20 → 56 not taken.
180574 result["orderIndex"] = orderIndex;
130
3/6
✓ Branch 23 → 24 taken 180574 times.
✗ Branch 23 → 61 not taken.
✓ Branch 24 → 25 taken 180574 times.
✗ Branch 24 → 61 not taken.
✓ Branch 25 → 26 taken 180574 times.
✗ Branch 25 → 59 not taken.
180574 result["state"] = lifecycle.getCurrentStateName();
131
1/2
✓ Branch 29 → 30 taken 180574 times.
✗ Branch 29 → 63 not taken.
180574 result["isGlobal"] = global;
132
1/2
✓ Branch 33 → 34 taken 180574 times.
✗ Branch 33 → 66 not taken.
180574 result["isVolatile"] = isVolatile;
133
1/2
✓ Branch 36 → 37 taken 180574 times.
✗ Branch 36 → 69 not taken.
361148 return result;
134 180574 }
135
136 } // namespace spice::compiler
137