Blender V5.3
symbol_template.cc
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#include "symbol_template.hh"
10#include "symbol_table.hh"
11
12namespace bsl {
13
14using namespace blender::gpu::shader::parser::ast;
15
16template<typename T>
18 const SymbolScope &scope,
19 const SymbolTable &table) const
20{
21 auto [mangled, err] = table.mangle_identifier(decl.arguments(), list, scope);
22 auto [args, args_debug] = mangled;
23 auto it = instances.find(args);
24 string full_id = string(id().str()) + "<" + args_debug.substr(2) + ">";
25
26 if (it == instances.end() && !err) {
28 }
29
30 if (err) {
31 if constexpr (is_same_v<T, SymbolFunction>) {
32 return {table.root->lookup_function(SymbolTable::err_symbol), full_id, err};
33 }
34 else {
35 return {table.root->lookup_class(SymbolTable::err_symbol), full_id, err};
36 }
37 }
38 return {it->second, full_id, err};
39}
40
42{
43 if (temp.is_class()) {
44 return;
45 }
46 TemplateArgList tmp_args(temp.arguments());
47
48 int tmp_arg_count = tmp_args.child_count();
49 if (tmp_arg_count == 0) {
50 return;
51 }
52
53 FuncDecl decl(temp.decl());
54 for (TemplateArg tmp_arg : tmp_args.children_of_type<TemplateArg>()) {
55 int arg_id = -1;
56 int id = 0;
58 if (tmp_arg.identifier().str() == arg.type().identifier().str()) {
59 arg_id = id;
60 break;
61 }
62 ++id;
63 }
64 if (arg_id != -1) {
65 temp_arg_index_in_fn_arg.emplace_back(arg_id);
66 }
67 }
68
69 if (temp_arg_index_in_fn_arg.size() != tmp_arg_count) {
70 /* ADL not possible. */
72 }
73}
74
77 const SymbolScope &scope) const
78{
79 vector<SymbolClass *> arg_cls;
80 for (Expr expr : list.children_of_type<Expr>()) {
81 auto [result, _] = expr_type_analysis(scope, expr.child_first());
82 arg_cls.emplace_back(result.type);
83 }
84
86 for (int i : tmp.temp_arg_index_in_fn_arg) {
87 if (i < arg_cls.size()) {
88 result.str += "T" + arg_cls[i]->identifier;
89 result.str_debug += ", " + arg_cls[i]->identifier;
90 }
91 else {
92 result.str += "T" + string(err_symbol);
93 result.str_debug += ", " + string(err_symbol);
94 }
95 }
96 return {result, {}};
97}
98
99template<typename T>
102 const SymbolScope &scope) const
103{
104 if constexpr (is_same_v<T, SymbolClass>) {
105 return {symbols.root->lookup_class(SymbolTable::err_symbol),
107 }
108 else {
109 if (!is_adl_possible()) {
112 }
113 auto [mangled, err] = symbols.mangle_identifier(*this, list, scope);
114 auto [args, args_debug] = mangled;
115 auto it = instances.find(args);
116 if (it == instances.end() && !err) {
117 err = {list,
119 string(id().str()) + "<" + args_debug.substr(2) + ">'"};
120 }
121
122 if (err) {
123 return {symbols.root->lookup_function(SymbolTable::err_symbol), err};
124 }
125 return {it->second, err};
126 }
127}
128
129template struct SymbolTemplate<SymbolClass>;
130template struct SymbolTemplate<SymbolFunction>;
131
132} // namespace bsl
#define str(s)
if(state< num_states)
Definition codegen.cc:18
SymbolTemplate< SymbolFunction > SymbolFunctionTemplate
@ CompilerErrorADLOnTypesNotAllowed
Definition diagnostic.hh:97
@ TemplateMissingExplicitArguments
@ TemplateMissingInstantiation
TypedChildRange< NodeT > children_of_type() const
Definition ast.hh:379
AstNodeException(ast::Node node, Diag diag, const string &param1="", const string &param2="", const string &param3="")
Result< SymbolFunction * > lookup_function(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
static constexpr const char * err_symbol
SymbolScope * root
Result< StringPair > mangle_identifier(ast::TemplateArgList args, ast::TemplateParamList list, const SymbolScope &scope) const
Result< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const
vector< int > temp_arg_index_in_fn_arg
Result< T * > lookup_adl(const SymbolTable &symbols, ast::FuncParamList list, const SymbolScope &scope) const
bool is_adl_possible() const
SymbolTemplate(ast::TemplateDecl temp)
unordered_map< string, T * > instances
ast::TemplateDecl decl
ResultPair< T *, string > lookup_inst(ast::TemplateParamList list, const SymbolScope &scope, const SymbolTable &table) const