Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2021-2024 ChilliBits. All rights reserved. |
2 |
|
|
|
3 |
|
|
#pragma once |
4 |
|
|
|
5 |
|
|
#include <queue> |
6 |
|
|
#include <string> |
7 |
|
|
#include <unordered_map> |
8 |
|
|
#include <vector> |
9 |
|
|
|
10 |
|
|
#include <model/GenericType.h> |
11 |
|
|
#include <symboltablebuilder/Capture.h> |
12 |
|
|
#include <symboltablebuilder/SymbolTableEntry.h> |
13 |
|
|
#include <util/CompilerWarning.h> |
14 |
|
|
|
15 |
|
|
#include "../../lib/json/json.hpp" |
16 |
|
|
|
17 |
|
|
namespace spice::compiler { |
18 |
|
|
|
19 |
|
|
// Forward declarations |
20 |
|
|
class Scope; |
21 |
|
|
class Type; |
22 |
|
|
struct CodeLoc; |
23 |
|
|
|
24 |
|
|
using CaptureMap = std::unordered_map<std::string /*name*/, Capture /*capture*/>; |
25 |
|
|
using SymbolMap = std::unordered_map<std::string /*name*/, SymbolTableEntry /*entry*/>; |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Class for storing information about symbols of the program. |
29 |
|
|
* Symbol tables are arranged in a tree structure, so that you can navigate with the .parent property and getChild() method up |
30 |
|
|
* and down the tree. |
31 |
|
|
*/ |
32 |
|
|
class SymbolTable { |
33 |
|
|
public: |
34 |
|
|
// Constructors |
35 |
|
16234 |
SymbolTable(SymbolTable *parent, Scope *scope) : parent(parent), scope(scope){}; |
36 |
|
|
|
37 |
|
|
// Friend classes |
38 |
|
|
friend class Scope; |
39 |
|
|
|
40 |
|
|
// Public methods |
41 |
|
|
SymbolTableEntry *insert(const std::string &name, ASTNode *declNode, bool isAnonymousSymbol = false); |
42 |
|
|
SymbolTableEntry *insertAnonymous(const QualType &qualType, ASTNode *declNode, size_t numericSuffix = 0); |
43 |
|
|
SymbolTableEntry *copySymbol(const std::string &originalName, const std::string &newName); |
44 |
|
|
SymbolTableEntry *lookup(const std::string &name); |
45 |
|
|
SymbolTableEntry *lookupStrict(const std::string &symbolName); |
46 |
|
|
SymbolTableEntry *lookupInComposedFields(const std::string &name, std::vector<size_t> &indexPath); |
47 |
|
|
SymbolTableEntry *lookupStrictByIndex(unsigned int orderIndex); |
48 |
|
|
SymbolTableEntry *lookupAnonymous(const CodeLoc &codeLoc, size_t numericSuffix = 0); |
49 |
|
|
Capture *lookupCapture(const std::string &name); |
50 |
|
|
Capture *lookupCaptureStrict(const std::string &name); |
51 |
|
|
void setCapturingRequired(); |
52 |
|
|
void deleteAnonymous(const std::string &name); |
53 |
|
|
[[nodiscard]] nlohmann::json toJSON() const; |
54 |
|
|
|
55 |
|
|
// Public members |
56 |
|
|
SymbolTable *parent; |
57 |
|
|
Scope *scope; |
58 |
|
|
SymbolMap symbols; |
59 |
|
|
CaptureMap captures; |
60 |
|
|
bool capturingRequired = false; |
61 |
|
|
}; |
62 |
|
|
|
63 |
|
|
} // namespace spice::compiler |
64 |
|
|
|