Blender V5.3
namespace.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <set>
10#include <unordered_set>
11
12#include "intermediate.hh"
13#include "metadata.hh"
14#include "processor.hh"
15
16namespace blender::gpu::shader {
17using namespace std;
18using namespace shader::parser;
19using namespace shader::parser::ast;
20using namespace metadata;
21
22static string get_prefix(Scope ns_scope)
23{
24 string prefix;
25 while (ns_scope.type() == ScopeType::Namespace || ns_scope.type() == ScopeType::Struct) {
26 prefix = ns_scope.front().prev().full_symbol_name() + "::" + prefix;
27 ns_scope = ns_scope.scope();
28 }
29 return prefix;
30}
31
32TemplateDefinition SourceProcessor::parse_template_definition(SourceProcessor::Parser &parser,
33 Token template_tok,
34 bool is_method,
35 Scope ns_scope,
36 const std::string &filepath)
37{
38 Token def_start = template_tok;
39 Scope template_args = def_start.next().scope();
40 /* Skip arguments. */
41 Token tok_type = template_args.back().next();
42
43 Token body_start = template_tok.find_next(BracketOpen);
44 Token def_end = body_start.scope().back();
45
46 TemplateDefinition symbol;
47 symbol.filepath = filepath;
48 symbol.definition_line = tok_type.line_number();
49 symbol.is_method = is_method;
50 symbol.is_static = tok_type == Static;
51 symbol.is_struct = tok_type == Struct || tok_type == Class;
52 symbol.name_space = get_prefix(ns_scope);
53
54 if (symbol.is_struct) {
55 Token name = body_start.prev();
56 symbol.identifier = string(name.str());
57 }
58 else {
59 Token fn_args = body_start.prev() == TokenType::Const ? body_start.prev(2) : body_start.prev();
60 Token fn_name = fn_args.scope().front().prev();
61 symbol.identifier = string(fn_name.str());
62 }
63
64 /* Capture end semicolon for structs. */
65 def_end = (symbol.is_struct) ? def_end.next() : def_end;
66 symbol.definition = parser.substr(def_start, def_end);
67 return symbol;
68}
69
70void SourceProcessor::parse_namespace_symbols(SourceProcessor::Parser &parser,
71 Scope ns,
73 const std::string &filepath)
74{
75 ns.foreach_scope(ScopeType::Namespace, [&](const Scope &ns) {
76 parse_namespace_symbols(parser, ns, metadata, filepath);
77 });
78
79 auto process_symbol = [&](Scope ns_scope,
80 Token name,
81 string_view identifier,
82 size_t line,
83 bool is_method,
84 bool is_static,
85 bool is_struct,
86 std::vector<std::pair<std::string, std::string>> members = {}) {
87 if (name.scope() != ns_scope) {
88 return;
89 }
90 metadata::Symbol symbol;
91 symbol.name_space = get_prefix(ns_scope);
92 symbol.identifier = identifier;
93 symbol.definition_line = line;
94 symbol.is_method = is_method;
95 symbol.is_static = is_static;
96 symbol.is_struct = is_struct;
97 symbol.members = members;
98 metadata.symbol_table.emplace_back(symbol);
99 };
100
101 auto process_templates = [&](Scope ns_scope, Token t, bool is_method) {
102 if (t.scope() != ns_scope) {
103 return;
104 }
105
106 if (t.next() == '<') {
107 if (t.next(2) == '>') {
108 /* Template specialization. */
109 return;
110 }
111 TemplateDefinition symbol = SourceProcessor::parse_template_definition(
112 parser, t, is_method, ns_scope, filepath);
113 metadata.template_definitions.emplace_back(symbol);
114 return;
115 }
116 /* Line number of the instantiation should be the one of the definition.
117 * But it is very hard at this point to search for the definition.
118 * Instead we consider the instantiation to be at the top of the file.
119 * It is unlikely we will have name collision with an instantiated template. */
120 size_t line = 0;
121 if (t.next() == Struct || t.next() == Class) {
122 /* Struct. */
123 Token name = t.next().next();
124 Scope template_args = name.next().scope();
125 string resolved_name = string(name.str()) +
127 process_symbol(ns_scope, name, resolved_name, line, false, false, true, {});
128 }
129 else {
130 /* Function. */
131 Token end = t.find_next(SemiColon);
132 Scope template_args = end.prev().scope().front().prev().scope();
133 Token name = template_args.front().prev();
134 string resolved_name = string(name.str()) +
136 process_symbol(ns_scope, name, resolved_name, line, is_method, false, false);
137 }
138 };
139
140 ns.foreach_struct([&](Token, Scope, Token struct_name, Scope body) {
141 /* Parse member. */
142 std::vector<std::pair<std::string, std::string>> members;
143 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope, Token) {
144 /* For methods, the declaration line is the top of the struct. */
145 members.emplace_back(type.str(), name.str());
146 });
147 process_symbol(ns,
148 struct_name,
149 struct_name.str(),
150 struct_name.line_number(),
151 false,
152 false,
153 true,
154 members);
155 /* Methods. */
156 body.foreach_function([&](bool is_static, Token, Token name, Scope, bool, Scope) {
157 /* For methods, the declaration line is the top of the struct. */
158 process_symbol(body, name, name.str(), struct_name.line_number(), true, is_static, false);
159 });
160 /* Parse template instantiations. */
161 body.foreach_token(Template, [&](Token t) { process_templates(body, t, true); });
162 });
163
164 ns.foreach_function([&](bool, Token, Token name, Scope, bool, Scope) {
165 process_symbol(ns, name, name.str(), name.line_number(), false, false, false);
166 });
167 /* Parse template instantiations. */
168 ns.foreach_token(Template, [&](Token t) { process_templates(ns, t, false); });
169}
170
171void SourceProcessor::parse_local_symbols(Parser &parser)
172{
173 parse_namespace_symbols(parser, parser(), metadata_, filepath_);
174}
175
176static void lower_namespace(string ns_prefix,
177 const Scope &scope,
179 ErrorHandler &error_handler,
180 const set<metadata::Symbol> &symbols_set)
181{
182 string ns_name(scope.front().prev().str());
183 ns_prefix += ns_name + "::";
184
185 bool has_nested_scope = false;
186 scope.foreach_scope(ScopeType::Namespace, [&](const Scope &scope) {
187 lower_namespace(ns_prefix, scope, parser, error_handler, symbols_set);
188 has_nested_scope = true;
189 });
190
191 if (has_nested_scope) {
192 /* Process iteratively. */
193 return;
194 }
195
196 scope.foreach_token(Word, [&](const Token &token) {
197 /* Reject method calls. */
198 if (token.prev() == '.') {
199 return;
200 }
201 Token next = token.next();
202 /* Only process the end token of a namespace qualified identifier. */
203 if (next == ':') {
204 return;
205 }
206
207 bool is_pipeline_arg = false;
208 if (token.scope().type() == ScopeType::FunctionArg) {
209 std::string_view type = token.scope().scope().front().prev(2).str();
210 is_pipeline_arg = (type == "PipelineGraphic" || type == "PipelineCompute");
211 }
212
213 if (is_pipeline_arg) {
214 /* Special case for pipelines. We need to match the function names but they are arguments. */
215 }
216 /* Only process function calls or types. */
217 else if (next != '<' && /* Templated type or function. */
218 next != '{' && /* Type definition. */
219 next != '&' && /* Reference definition. */
220 next != Word && /* Variable definition. */
221 token.scope().type() != parser::ScopeType::TemplateArg && /* Template argument. */
222 next != '(' /* Function call or reference definition. */
223 )
224 {
225 return;
226 }
227
228 const bool is_fn = (token.next() == '(') ||
229 (token.next() == '<' && token.next().scope().back().next() == '(');
230 /* Reject method definition. */
231 if (is_fn && token.scope().type() == ScopeType::Struct) {
232 return;
233 }
234
235 string struct_name;
236 if (is_fn) {
237 /* If this is function call inside a struct, this could reference a method.
238 * In this case we need to add the struct name during the fully qualified name lookup. */
239 const Scope struct_scope = token.scope().first_scope_of_type(ScopeType::Struct);
240 if (struct_scope.is_valid()) {
241 struct_name = struct_scope.front().prev().full_symbol_name();
242 }
243 }
244
245 const int token_line = token.line_number();
246
247 for (const auto &symbol : symbols_set) {
248 if (token.str() != symbol.identifier) {
249 continue;
250 }
251 /* Only expand symbols that are visible inside this namespace. */
252 if (!symbol.name_space.starts_with(ns_prefix)) {
253 continue;
254 }
255 /* Reject symbols declared after the identifier.
256 * Note that static method have their definition line at the top of the struct. */
257 if (token_line < symbol.definition_line) {
258 continue;
259 }
260 /* Symbol as it could be specified from this namespace. */
261 string symbol_visible = symbol.name_space.substr(ns_prefix.size()) + symbol.identifier;
262
263 bool append_struct_ns = false;
264 string specified_symbol = token.full_symbol_name();
265 /* First try to match methods. */
266 if (symbol.is_method && !struct_name.empty()) {
267 if (!symbol.is_static) {
268 continue;
269 }
270
271 bool is_prev_ns_specifier = token.prev() == ':' && token.prev(2) == ':';
272 if (!is_prev_ns_specifier) {
273 /* For unspecified symbol, we append the struct namespace to try to match the method
274 * visible symbol. */
275 specified_symbol = struct_name + "::" + specified_symbol;
276 append_struct_ns = true;
277 }
278
279 if (specified_symbol != symbol_visible) {
280 continue;
281 }
282 /* Matched a static method. */
283 }
284 else {
285 /* Other symbols. */
286 if (specified_symbol != symbol_visible) {
287 continue;
288 }
289 }
290
291 /* Only for non-definition. */
292 if (token.prev() != Word) {
293 /* WORKAROUND: Since we do not have overload argument type infos, we cannot resolve
294 * them like C++ does. Instead, we error on ambiguity and let the user resolve it. */
295 for (const auto &overload : symbols_set) {
296 /* Searching for overload in other namespaces. */
297 if (overload.name_space == symbol.name_space || overload.identifier != symbol.identifier)
298 {
299 continue;
300 }
301 /* Reject symbols declared after the identifier.
302 * Note that static method have their definition line at the top of the struct. */
303 if (token_line < overload.definition_line) {
304 continue;
305 }
306 /* Only expand symbols that are visible inside this namespace. */
307 if (!ns_prefix.starts_with(overload.name_space)) {
308 continue;
309 }
310 if (specified_symbol != overload.identifier) {
311 continue;
312 }
313 error_handler.report(
314 token, "Call to function is ambiguous. Specify namespace to remove ambiguity.");
315 break;
316 }
317 }
318
319 /* Append current namespace. */
320 parser.insert_before(token.namespace_start(), ns_name + "::");
321 if (append_struct_ns) {
322 /* Append struct namespace for static methods. */
323 parser.insert_before(token.namespace_start(), struct_name + "::");
324 }
325 /* Only match a symbol once. */
326 break;
327 }
328 });
329
330 /* Pipeline declarations.
331 * Manually handle them. They are the only use-case of variable defined in global scope. */
332 scope.foreach_match("AA(A", [&](vector<Token> toks) {
333 if (toks[0].scope().type() != ScopeType::Namespace || !toks[0].str().starts_with("Pipeline")) {
334 return;
335 }
336 parser.insert_before(toks[1], ns_name + SourceProcessor::namespace_separator);
337 });
338
339 Token namespace_tok = scope.front().prev().namespace_start().prev();
340 if (namespace_tok == TokenType::Namespace) {
341 parser.erase(namespace_tok, scope.front());
342 parser.erase(scope.back());
343 }
344 else {
345 error_handler.report(namespace_tok, "Expected namespace token.");
346 }
347}
348
349/* Lower namespaces by adding namespace prefix to all the contained structs and functions. */
350void SourceProcessor::lower_namespaces(Parser &parser)
351{
352 using namespace metadata;
353
354 /* Expand compound namespaces. Simplify lowering.
355 * Example: `namespace A::B {}` > `namespace A { namespace B {} }` */
356 parser().foreach_token(TokenType::Namespace, [&](Token t) {
357 int nesting = 0;
358 Token name = t.next();
359 while (name.next() == ':') {
360 parser.replace(name.next(), name.next().next(), " { namespace ");
361 name = name.next().next().next();
362 nesting++;
363 }
364 Scope scope = name.next().scope();
365 for (int i = 0; i < nesting; i++) {
366 parser.insert_before(scope.back(), "}");
367 }
368 });
369
370 parser.apply_mutations();
371
372 /* Using an ordered set ordered by namespace make homonym symbols are resolve
373 * properly (closest from current namespace). */
374 set<metadata::Symbol> symbols_set;
375 {
376 /* Deduplicate symbols. Done this way because we want to keep line definition ordering
377 * inside the symbols_set. */
378 unordered_set<string> unique_symbols;
379 for (const auto &symbol : metadata_.symbol_table) {
380 auto [_, inserted] = unique_symbols.insert(symbol.name_space + symbol.identifier);
381 if (inserted) {
382 symbols_set.emplace(symbol);
383 }
384 }
385 }
386
387 do {
388 /* Parse each namespace declaration.
389 * Do it iteratively from the deepest namespace to the shallowest. */
390 parser().foreach_scope(ScopeType::Namespace, [&](const Scope &scope) {
391 lower_namespace("", scope, parser, error_handler, symbols_set);
392 });
393 } while (parser.apply_mutations());
394}
395
396void SourceProcessor::lower_scope_resolution_operators(Parser &parser)
397{
398 parser().foreach_match<true>("::", [&](const vector<Token> &tokens) {
399 if (tokens[0].scope().type() == ScopeType::Attribute) {
400 return;
401 }
402 Token prev = tokens[0].prev();
403 if (prev != Word && !(prev == '>' && prev.followed_by_whitespace() == false)) {
404 /* Global namespace reference. */
405 parser.erase(tokens.front(), tokens.back());
406 }
407 else {
408 /* Specific namespace reference. */
409 parser.replace(tokens.front(), tokens.back(), namespace_separator);
410 }
411 });
412 parser.apply_mutations();
413}
414
415} // namespace blender::gpu::shader
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
Definition processor.hh:45
static constexpr const char * namespace_separator
Definition processor.hh:51
static std::string template_arguments_mangle(const Scope template_args)
Definition template.cc:20
#define str(s)
static void lower_namespace(string ns_prefix, const Scope &scope, SourceProcessor::Parser &parser, ErrorHandler &error_handler, const set< metadata::Symbol > &symbols_set)
Definition namespace.cc:176
static string get_prefix(Scope ns_scope)
Definition namespace.cc:22
SymEdge< T > * prev(const SymEdge< T > *se)
@ BracketOpen
Definition types.hh:55
@ Static
Definition types.hh:105
@ Struct
Definition types.hh:117
@ SemiColon
Definition types.hh:62
@ Word
Definition types.hh:43
@ Template
Definition types.hh:119
@ Class
Definition types.hh:118
void report(Token tok, std::string_view message)
void foreach_match(const std::string &pattern, CallbackFn callback) const
Definition scope.hh:255
void foreach_scope(ScopeType type, CallbackFn callback) const
Definition scope.hh:339
void foreach_token(const TokenType token_type, Callback callback) const
Definition scope.hh:380
std::string_view substr(const Token &start, const Token &end, const bool with_trailing_whitespaces=false) const
Return the substring between the start and end tokens (included).
Definition lexit.hh:224
Token prev(int i=1) const
Definition lexit.hh:376
std::string_view str() const
Definition lexit.hh:381
const TokenType & type() const
Definition lexit.hh:354
Token next(int i=1) const
Definition lexit.hh:372