Blender V5.3
symbol_table.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 "symbol_class.hh"
14#include "symbol_function.hh"
15#include "symbol_variable.hh"
16#include "type_checker.hh"
17
18#include <deque>
19#include <unordered_map>
20
21namespace bsl {
22
23struct StringPair {
24 string str;
25 string str_debug;
26};
27
28struct SymbolVariable;
29struct SymbolFunction;
30struct SymbolClass;
31struct SymbolScope;
32
34
35 static constexpr const char *err_symbol = "ERROR_SYMBOL";
36
37 template<typename T> struct Allocator {
38 private:
39 std::deque<T> arena;
40
41 public:
42 template<typename... Args> T *alloc(Args &&...args)
43 {
44 arena.emplace_back(std::forward<Args>(args)...);
45 return &arena.back();
46 }
47 };
48
55
58
59 SymbolClass *err_cls = nullptr;
62 SymbolClass *int_cls = nullptr;
69 SymbolClass *str_cls = nullptr;
111
112 unordered_map<int, string> image_formats;
113
114 static constexpr const char *subscript_operator_id = "arr_op_";
115
116 template<typename SymbolT> SymbolT *alloc(SymbolT &&sym);
117
118 SymbolScope *root = nullptr;
119
120 SymbolTable(ParserBase &builtin_parser);
121
122 void parse(ast::LocalScope node, ErrorHandler &err_handler);
123
126 const SymbolScope &scope) const;
127
130 const SymbolScope &scope) const;
131
133 ast::Node start_node,
134 bool is_reference = false) const;
135
137
139 ast::Node start,
140 int &node_count,
141 bool allow_compilation_constant = false) const;
142
143 SymbolClass *get_literal_type(string_view lit) const;
144
146 const SymbolClass *to,
147 const bool ctor_conversion = false) const;
148
150
151 SymbolClass *make_type(SymbolClass *base, int size) const;
152
153 private:
154 struct BuiltinType {
155 /* "int", "int2", ... */
156 SymbolClass *type;
157 /* "int", "uint", "float", "bool", ... */
158 SymbolClass *base;
159 };
160
161 struct BuiltinOp {
162 SymbolClass *left;
163 TokenType op;
164 SymbolClass *right;
165 SymbolClass *result;
166 };
167
168 struct BuiltinFunc {
169 SymbolClass *return_type;
170 string id;
171 vector<SymbolClass *> arg_types;
172 };
173
174 void register_builtins(ast::LocalScope node);
175 vector<BuiltinType> generate_builtin_types();
176 vector<BuiltinFunc> generate_all_constructors(const vector<BuiltinType> &types);
177 vector<BuiltinFunc> generate_all_builtin_functions();
178 vector<BuiltinFunc> generate_all_builtin_constexpr_functions();
179
180 void add_operator(
181 SourceLocation loc, SymbolClass *left, TokenType op, SymbolClass *right, SymbolClass *ret);
182};
183
184} // namespace bsl
#define T
Definition codegen.cc:18
SymbolTemplate< SymbolFunction > SymbolFunctionTemplate
TokenType
Definition types.hh:41
T * alloc(Args &&...args)
SymbolClass * half4_cls
SymbolClass * float3x2_cls
SymbolClass * float4_cls
void parse(ast::LocalScope node, ErrorHandler &err_handler)
Definition symbol.cc:1328
SymbolClass * to_class(builtin::ClassId id) const
Definition operator.cc:238
SymbolClass * ushort_cls
SymbolClass * void_cls
SymbolClass * bool3_cls
SymbolClass * short4_cls
SymbolClass * float4x3_cls
SymbolClass * uchar2_cls
SymbolClass * uint4_cls
SymbolClass * uint3_cls
static constexpr const char * err_symbol
SymbolClass * float3x3_cls
SymbolScope * root
SymbolClass * uchar3_cls
SymbolClass * int2_cls
SymbolClass * packed_float2_cls
SymbolClass * half3_cls
SymbolClass * short3_cls
SymbolClass * float2_cls
SymbolClass * float2x2_cls
SymbolClass * uchar4_cls
Result< StringPair > mangle_identifier(ast::TemplateArgList args, ast::TemplateParamList list, const SymbolScope &scope) const
SymbolClass * uint_cls
SymbolTable(ParserBase &builtin_parser)
Allocator< SymbolFunction > fun_arena
SymbolT * alloc(SymbolT &&sym)
SymbolClass * uint2_cls
SymbolClass * bool2_cls
SymbolClass * get_literal_type(string_view lit) const
SymbolClass * float2x3_cls
SymbolClass * float_cls
Result< string > expr_to_string(const SymbolScope &scope, ast::Node start, int &node_count, bool allow_compilation_constant=false) const
SymbolClass * bool_cls
Result< SymbolClass * > resolve_auto_type(SymbolScope &scope, ast::Declarator decl) const
SymbolClass * short_cls
SymbolClass * half2_cls
SymbolClass * float2x4_cls
static constexpr const char * subscript_operator_id
SymbolClass * make_type(SymbolClass *base, int size) const
Definition operator.cc:336
Allocator< SymbolClass > cls_arena
SymbolClass * bool32_t_cls
SymbolClass * err_cls
Allocator< SymbolScope > scp_arena
SymbolClass * int3_cls
Allocator< SymbolClassTemplate > tmp_cls_arena
SymbolClass * packed_float3_cls
SymbolClass * float3x4_cls
SymbolClass * float4x2_cls
SymbolClass * str_cls
SymbolClass * float3_cls
SymbolClass * short2_cls
Result< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const
SymbolClass * int4_cls
SymbolClass * char2_cls
SymbolVariable * err_var
SymbolClass * uchar_cls
SymbolClass * int_cls
SymbolClass * ushort2_cls
SymbolClass * packed_float4_cls
SymbolClass * char_cls
Allocator< SymbolVariable > var_arena
SymbolClass * bool4_cls
MatchRank get_conversion_rank(const SymbolClass *from, const SymbolClass *to, const bool ctor_conversion=false) const
SymbolClass * char3_cls
SymbolClass * half_cls
SymbolClass * ushort4_cls
SymbolClass * ushort3_cls
Allocator< SymbolFunctionTemplate > tmp_fun_arena
SymbolClass * float4x4_cls
SymbolClass * char4_cls
SymbolFunction * err_func
unordered_map< int, string > image_formats