Path: blob/master/dep/reshadefx/include/effect_symbol_table.hpp
4246 views
/*1* Copyright (C) 2014 Patrick Mours2* SPDX-License-Identifier: BSD-3-Clause3*/45#pragma once67#include "effect_module.hpp"8#include <cstdint>9#include <unordered_map> // Used for symbol lookup table1011namespace reshadefx12{13/// <summary>14/// A scope encapsulating symbols.15/// </summary>16struct scope17{18std::string name;19uint32_t level, namespace_level;20};2122/// <summary>23/// Enumeration of all possible symbol types.24/// </summary>25enum class symbol_type26{27invalid,28variable,29constant,30function,31intrinsic,32structure,33};3435/// <summary>36/// A single symbol in the symbol table.37/// </summary>38struct symbol39{40symbol_type op = symbol_type::invalid;41uint32_t id = 0;42reshadefx::type type = {};43reshadefx::constant constant = {};44const reshadefx::function *function = nullptr;45};46struct scoped_symbol : symbol47{48struct scope scope; // Store scope together with symbol data49};5051/// <summary>52/// A symbol table managing a list of scopes and symbols.53/// </summary>54class symbol_table55{56public:57symbol_table();5859/// <summary>60/// Enters a new scope as child of the current one.61/// </summary>62void enter_scope();63/// <summary>64/// Enters a new namespace as child of the current one.65/// </summary>66void enter_namespace(const std::string &name);67/// <summary>68/// Leaves the current scope and enter the parent one.69/// </summary>70void leave_scope();71/// <summary>72/// Leaves the current namespace and enter the parent one.73/// </summary>74void leave_namespace();7576/// <summary>77/// Gets the current scope the symbol table operates in.78/// </summary>79const scope ¤t_scope() const { return _current_scope; }8081/// <summary>82/// Inserts an new symbol in the symbol table.83/// Returns <see langword="false"/> if a symbol by that name and type already exists.84/// </summary>85bool insert_symbol(const std::string &name, const symbol &symbol, bool global = false);8687/// <summary>88/// Looks for an existing symbol with the specified <paramref name="name"/>.89/// </summary>90scoped_symbol find_symbol(const std::string &name) const;91scoped_symbol find_symbol(const std::string &name, const scope &scope, bool exclusive) const;9293/// <summary>94/// Searches for the best function or intrinsic overload matching the argument list.95/// </summary>96bool resolve_function_call(const std::string &name, const std::vector<expression> &args, const scope &scope, symbol &data, bool &ambiguous) const;9798private:99scope _current_scope;100// Lookup table from name to matching symbols101std::unordered_map<std::string, std::vector<scoped_symbol>> _symbol_stack;102};103}104105106