forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbol_table.hpp
62 lines (52 loc) · 1.5 KB
/
symbol_table.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#pragma once
#include <stack>
#include <unordered_map>
#include <string>
namespace reshadefx
{
#pragma region Forward Declarations
namespace nodes
{
struct declaration_node;
struct call_expression_node;
}
#pragma endregion
/// <summary>
/// A scope encapsulating a list of symbols.
/// </summary>
struct scope
{
std::string name;
unsigned int level, namespace_level;
};
/// <summary>
/// A single symbol in the symbol table.
/// </summary>
using symbol = nodes::declaration_node *;
/// <summary>
/// A symbol table managing a list of scopes and symbols.
/// </summary>
class symbol_table
{
public:
symbol_table();
void enter_scope(symbol parent = nullptr);
void enter_namespace(const std::string &name);
void leave_scope();
void leave_namespace();
symbol current_parent() const { return _parent_stack.empty() ? nullptr : _parent_stack.top(); }
const scope ¤t_scope() const { return _current_scope; }
bool insert(symbol symbol, bool global = false);
symbol find(const std::string &name) const;
symbol find(const std::string &name, const scope &scope, bool exclusive) const;
bool resolve_call(nodes::call_expression_node *call, const scope &scope, bool &intrinsic, bool &ambiguous) const;
private:
scope _current_scope;
std::stack<symbol> _parent_stack;
std::unordered_map<std::string, std::vector<std::pair<scope, symbol>>> _symbol_stack;
};
}