Blender V5.3
symbol_scope.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "ast.hh"
12#include "diagnostic.hh"
13#include "source_location.hh"
14#include "symbol_variable.hh"
15
16#include <unordered_map>
17
18namespace bsl {
19
20struct SymbolParser;
21struct SymbolTable;
22
23struct SymbolFunction;
24struct SymbolClass;
25
29template<typename SymbolT>
30struct SymbolMap : unordered_map<string, pair<SourceLocation, SymbolT *>> {
31 pair<SymbolT *, bool> emplace(SymbolT *sym)
32 {
33 auto [it, success] = this->try_emplace(sym->identifier,
34 pair<SourceLocation, SymbolT *>(sym->loc, sym));
35 return pair<SymbolT *, bool>(it->second.second, success);
36 }
37
38 pair<SymbolT *, bool> emplace(const string &id, SymbolT *sym)
39 {
40 auto [it, success] = this->try_emplace(id, pair<SourceLocation, SymbolT *>(sym->loc, sym));
41 return pair<SymbolT *, bool>(it->second.second, success);
42 }
43
44 pair<SymbolT *, bool> emplace(const string &id, SymbolT *sym, SourceLocation loc)
45 {
46 auto [it, success] = this->try_emplace(id, pair<SourceLocation, SymbolT *>(loc, sym));
47 return pair<SymbolT *, bool>(it->second.second, success);
48 }
49
50 SymbolT *operator[](const string &k)
51 {
52
53 return this->find(k)->second.second;
54 }
55};
56
64
65 /* Namespaces or nested blocks. */
66 unordered_map<string, SymbolScope *> scopes;
67
71
72 vector<std::pair<ast::FuncForwardDecl, SymbolFunction *>> function_prototypes;
73
74 /* Point of instantiation if this is a template instantiation.
75 *
76 * The point of instantiation allows to correctly resolve symbols external to the templated
77 * symbol itself.
78 *
79 * For this to work, we need to substitute the symbol location with the POI when doing name
80 * lookup outside of the symbol's scope.
81 *
82 * This will effectively teleport the symbol location when trying to match symbol outside of this
83 * scope.
84 *
85 * The tricky part is that member access to outsider classes also need to have their location
86 * substituted.*/
87 optional<SourceLocation> poi;
88
89 /* Function scope act as anonymous scopes but still have identifier. */
91
92 /* Root constructor. */
93 SymbolScope(ast::LocalScope decl) : Symbol("", "", decl.back(), nullptr) {}
94
95 SymbolScope(SymbolScope *parent, Token tok, const string &id, Type type)
96 : Symbol(id, id, tok, parent), type(type)
97 {
98 }
99
101 : SymbolScope(parent, decl.front(), parent->unique_id(), LOCAL)
102 {
103 }
104
106 : SymbolScope(parent, id.front(), string(id.str()), type)
107 {
108 }
109
111
114 optional<SourceLocation> loc = nullopt) const;
115
116 template<typename ArgOrParamList>
119 ArgOrParamList params,
120 const SymbolScope &arguments_scope,
121 optional<SourceLocation> loc = nullopt) const;
122
125 optional<SourceLocation> loc = nullopt) const;
126
128 ast::Id id,
129 ast::Id last,
130 optional<SourceLocation> loc = nullopt) const;
131
132 /* Returns nullptr if not found. */
133 SymbolVariable *lookup_variable(string id) const;
134 SymbolFunction *lookup_function(string id) const;
135 SymbolClass *lookup_class(string id) const;
136
137 /* Doesn't match last template in identifier. */
140
141 std::pair<ast::FuncForwardDecl, SymbolFunction *> lookup_function_forward_decl(
142 ast::FuncDecl decl) const;
143
144 void print() const;
145
146 const SymbolScope *root_scope() const;
148
149 /* Can be null if this is not a class. */
151 /* Can be null if this is not a function. */
153 const SymbolFunction *as_function() const;
154
155 /* Return the function containing this scope or the scope itself if it is a function.
156 * Return nullptr if this scope is not inside a function. */
158 const SymbolFunction *parent_function() const;
159
160 /* Returns true if the function is an overload or redefines the symbol. */
161 bool function_emplace(SymbolFunction *fn, bool builtin = false);
162 /* Returns true if redefined symbol. */
163 bool variable_emplace(string id, SymbolVariable *var);
164 /* Returns true if redefined symbol. */
166
167 vector<SymbolVariable *> non_static_variables_in_declaration_order() const;
168
169 /* Return the N'th anonymous child scope or nullptr if not existing. */
170 SymbolScope *child_scope(int n);
171
172 private:
173 template<typename T>
174 T *lookup_generic(const SymbolTable &table,
176 const SourceLocation &loc) const;
177 template<typename T>
178 T *lookup_generic_nested(const SymbolTable &table,
179 ast::Id id,
180 ast::Id last,
181 const SourceLocation &loc) const;
182
183 int id = 0;
184 /* Create a unique, non-reachable key.
185 * Identifiers cannot start with digits.
186 * This makes a scope impenetrable (can only leave the scope) during traversal. */
187 string unique_id()
188 {
189 return to_string(id++);
190 }
191};
192
193} // namespace bsl
#define str(s)
#define T
Definition codegen.cc:18
string to_string(const char *str)
Definition string.cpp:202
Represents a specific position within a source file or included sub-file.
pair< SymbolT *, bool > emplace(const string &id, SymbolT *sym, SourceLocation loc)
pair< SymbolT *, bool > emplace(SymbolT *sym)
SymbolT * operator[](const string &k)
pair< SymbolT *, bool > emplace(const string &id, SymbolT *sym)
SymbolScope * child_scope(int n)
unordered_map< string, SymbolScope * > scopes
vector< SymbolVariable * > non_static_variables_in_declaration_order() const
SymbolVariable * lookup_variable(const SymbolTable &table, ast::IdQualified id) const
enum bsl::SymbolScope::Type type
SymbolScope(ast::LocalScope decl)
vector< std::pair< ast::FuncForwardDecl, SymbolFunction * > > function_prototypes
bool function_emplace(SymbolFunction *fn, bool builtin=false)
SymbolMap< SymbolFunction > functions
SymbolScope(SymbolScope *parent, Token tok, const string &id, Type type)
optional< SourceLocation > poi
SymbolScope(SymbolScope *parent, ast::Id id, Type type)
SymbolFunction * as_function()
bool variable_emplace(string id, SymbolVariable *var)
SymbolFunction * parent_function()
SymbolMap< SymbolVariable > variables
SymbolFunction * lookup_function_base(const SymbolTable &table, ast::IdQualified id) const
void print() const
SymbolMap< SymbolClass > classes
const SymbolScope * root_scope() const
Result< SymbolFunction * > lookup_function(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolClass * lookup_class_base(const SymbolTable &table, ast::IdQualified id) const
SymbolClass * as_class()
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolScope(SymbolScope *parent, ast::LocalScope decl)
std::pair< ast::FuncForwardDecl, SymbolFunction * > lookup_function_forward_decl(ast::FuncDecl decl) const
SymbolScope * parent
SourceLocation loc