Blender V5.3
symbol_function.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_function.hh"
10#include "symbol_table.hh"
11
12namespace bsl {
13
14using namespace blender::gpu::shader::parser::ast;
15
17 const SymbolScope &scope,
19{
20 std::optional<AstNodeException> error;
21 vector<SymbolClass *> arg_types;
22 for (Expr expr : list.children_of_type<Expr>()) {
23 arg_types.emplace_back(table.expr_type_analysis(scope, expr.child_first()).unwrap(error).type);
24 }
25 return {arg_types, error};
26}
27
29 const SymbolScope &scope,
31{
32 vector<SymbolClass *> arg_types;
33 for (FuncArg arg : list.children_of_type<FuncArg>()) {
34 auto [cls, _] = scope.lookup_class(table, arg.type().identifier());
35 arg_types.emplace_back(cls);
36 }
37 return {arg_types, std::nullopt};
38}
39
40static string to_str(const vector<SymbolClass *> &types)
41{
42 string parameters = types.empty() ? ", " : "";
43 for (SymbolClass *cls : types) {
44 parameters += ", " + cls->identifier + "";
45 }
46 return "(" + parameters.substr(2) + ")";
47}
48
49optional<vector<MatchRank>> SymbolFunction::get_match_ranks(
50 const SymbolTable &table, const vector<SymbolClass *> &param_types) const
51{
52 /* Early out if there are more arguments than parameters. */
53 if (param_types.size() > arg_types.size()) {
54 return nullopt; /* Not a viable candidate. */
55 }
56
57 vector<MatchRank> ranks;
58 for (size_t i = 0; i < param_types.size(); ++i) {
59 MatchRank rank = table.get_conversion_rank(
60 param_types[i], arg_types[i], allow_vector_promotion);
61 if (rank == MatchRank::None) {
62 return {}; /* Not a viable candidate. */
63 }
64 ranks.push_back(rank);
65 }
66
67 /* Check if remaining parameters are optional. Defaulted parameters are not ranked, so that
68 * every viable candidate returns one rank per supplied argument and stays comparable. */
69 for (size_t i = param_types.size(); i < arg_types.size(); ++i) {
70 if (!arg_defaults[i].is_valid()) {
71 return {}; /* Missing required argument. */
72 }
73 }
74
75 return ranks;
76}
77
78static bool is_strictly_better(const std::vector<MatchRank> &candidate,
79 const std::vector<MatchRank> &best)
80{
81 bool has_strictly_better_arg = false;
82
83 for (size_t i = 0; i < candidate.size(); ++i) {
84 if (candidate[i] < best[i]) {
85 return false; /* Worse in at least one argument, so it's not strictly better overall. */
86 }
87 if (candidate[i] > best[i]) {
88 has_strictly_better_arg = true;
89 }
90 }
91
92 return has_strictly_better_arg;
93}
94
96 const SymbolTable &table,
97 const vector<SymbolClass *> &arg_types)
98{
99 SymbolFunction *best_fn = nullptr;
100 vector<MatchRank> best_ranks;
101 bool is_ambiguous = false;
102
103 for (SymbolFunction *fn = this; fn; fn = fn->overload_next) {
104 if (!fn->is_complete) {
105 continue;
106 }
107
108 auto current_ranks_opt = fn->get_match_ranks(table, arg_types);
109 if (!current_ranks_opt) {
110 continue; /* Skip non-viable candidates. */
111 }
112
113 const vector<MatchRank> &current_ranks = *current_ranks_opt;
114
115 if (!best_fn) {
116 best_fn = fn;
117 best_ranks = std::move(current_ranks);
118 }
119 else {
120 bool current_is_better = is_strictly_better(current_ranks, best_ranks);
121 bool best_is_better = is_strictly_better(best_ranks, current_ranks);
122
123 if (current_is_better) {
124 best_fn = fn;
125 best_ranks = std::move(current_ranks);
126 is_ambiguous = false;
127 }
128 else if (best_is_better) {
129 /* best_fn remains the best. Do nothing. */
130 }
131 else {
132 /* Tie-Breaker: Neither is strictly better based on argument match ranks. */
133
134 /* C++ Rule: Prefer non-template functions over template functions. */
135 bool current_is_template = fn->is_specialization;
136 bool best_is_template = best_fn->is_specialization;
137
138 if (!current_is_template && best_is_template) {
139 /* Current is non-template, best is template. Current wins the tie. */
140 best_fn = fn;
141 best_ranks = std::move(current_ranks);
142 is_ambiguous = false;
143 }
144 else if (current_is_template && !best_is_template) {
145 /* Best is non-template, current is template. Best maintains its win, but keep any
146 * ambiguity with an earlier non-template candidate. */
147 }
148 else {
149 /* Both are templates, or both are non-templates. It is truly ambiguous. */
150 is_ambiguous = true;
151 }
152 }
153 }
154 }
155
156 if (best_fn && !is_ambiguous) {
157 return {best_fn, {}};
158 }
159 /* List all candidates inside the error message. */
160 string candidates;
161 for (const SymbolFunction *fn = this; fn; fn = fn->overload_next) {
162 if (fn->is_complete) {
163 candidates += "\ncandidate: " + fn->return_type->original + " " + fn->original +
164 to_str(fn->arg_types);
165 }
166 }
167
168 Diag error_type = is_ambiguous ? Diag::OverloadAmbiguous : Diag::OverloadNotFound;
170 AstNodeException(id, error_type, original, to_str(arg_types), candidates)};
171}
172
174{
175 fn->overload_next = this->overload_next;
176 this->overload_next = fn;
177}
178
180{
181 this->arg_const.reserve(n);
182 this->arg_types.reserve(n);
183 this->arg_defaults.reserve(n);
184}
185
186void SymbolFunction::add_argument(bool is_const, SymbolClass *type, Expr default_value)
187{
188 this->arg_const.emplace_back(is_const);
189 this->arg_types.emplace_back(type);
190 this->arg_defaults.emplace_back(default_value);
191}
192
193} // namespace bsl
Definition codegen.cc:18
static bool is_strictly_better(const std::vector< MatchRank > &candidate, const std::vector< MatchRank > &best)
@ OverloadAmbiguous
static string to_str(const vector< SymbolClass * > &types)
AstNodeException(ast::Node node, Diag diag, const string &param1="", const string &param2="", const string &param3="")
void reserve_arguments(int n)
vector< ast::Expr > arg_defaults
void add_argument(bool is_const, SymbolClass *type, ast::Expr default_value={})
Result< SymbolFunction * > lookup_overload(ast::IdQualified id, const SymbolTable &table, const vector< SymbolClass * > &arg_types)
void add_overload(SymbolFunction *fn)
vector< SymbolClass * > arg_types
SymbolFunction * overload_next
SymbolFunction(SymbolScope *parent, SymbolClass *return_type, AstNodeT decl, const string &suffix="")
vector< bool > arg_const
static Result< vector< SymbolClass * > > to_arg_types(const SymbolTable &table, const SymbolScope &scope, ast::FuncParamList list)
enum bsl::SymbolScope::Type type
SymbolScope(ast::LocalScope decl)
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< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const