Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/reshadefx/include/effect_symbol_table.hpp
4246 views
1
/*
2
* Copyright (C) 2014 Patrick Mours
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#pragma once
7
8
#include "effect_module.hpp"
9
#include <cstdint>
10
#include <unordered_map> // Used for symbol lookup table
11
12
namespace reshadefx
13
{
14
/// <summary>
15
/// A scope encapsulating symbols.
16
/// </summary>
17
struct scope
18
{
19
std::string name;
20
uint32_t level, namespace_level;
21
};
22
23
/// <summary>
24
/// Enumeration of all possible symbol types.
25
/// </summary>
26
enum class symbol_type
27
{
28
invalid,
29
variable,
30
constant,
31
function,
32
intrinsic,
33
structure,
34
};
35
36
/// <summary>
37
/// A single symbol in the symbol table.
38
/// </summary>
39
struct symbol
40
{
41
symbol_type op = symbol_type::invalid;
42
uint32_t id = 0;
43
reshadefx::type type = {};
44
reshadefx::constant constant = {};
45
const reshadefx::function *function = nullptr;
46
};
47
struct scoped_symbol : symbol
48
{
49
struct scope scope; // Store scope together with symbol data
50
};
51
52
/// <summary>
53
/// A symbol table managing a list of scopes and symbols.
54
/// </summary>
55
class symbol_table
56
{
57
public:
58
symbol_table();
59
60
/// <summary>
61
/// Enters a new scope as child of the current one.
62
/// </summary>
63
void enter_scope();
64
/// <summary>
65
/// Enters a new namespace as child of the current one.
66
/// </summary>
67
void enter_namespace(const std::string &name);
68
/// <summary>
69
/// Leaves the current scope and enter the parent one.
70
/// </summary>
71
void leave_scope();
72
/// <summary>
73
/// Leaves the current namespace and enter the parent one.
74
/// </summary>
75
void leave_namespace();
76
77
/// <summary>
78
/// Gets the current scope the symbol table operates in.
79
/// </summary>
80
const scope &current_scope() const { return _current_scope; }
81
82
/// <summary>
83
/// Inserts an new symbol in the symbol table.
84
/// Returns <see langword="false"/> if a symbol by that name and type already exists.
85
/// </summary>
86
bool insert_symbol(const std::string &name, const symbol &symbol, bool global = false);
87
88
/// <summary>
89
/// Looks for an existing symbol with the specified <paramref name="name"/>.
90
/// </summary>
91
scoped_symbol find_symbol(const std::string &name) const;
92
scoped_symbol find_symbol(const std::string &name, const scope &scope, bool exclusive) const;
93
94
/// <summary>
95
/// Searches for the best function or intrinsic overload matching the argument list.
96
/// </summary>
97
bool resolve_function_call(const std::string &name, const std::vector<expression> &args, const scope &scope, symbol &data, bool &ambiguous) const;
98
99
private:
100
scope _current_scope;
101
// Lookup table from name to matching symbols
102
std::unordered_map<std::string, std::vector<scoped_symbol>> _symbol_stack;
103
};
104
}
105
106