Blender V5.3
struct.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 <unordered_set>
10
11#include "intermediate.hh"
12#include "metadata.hh"
13#include "processor.hh"
14
15namespace blender::gpu::shader {
16using namespace std;
17using namespace shader::parser;
18using namespace shader::parser::ast;
19using namespace metadata;
20
21/* `class` -> `struct` */
22void SourceProcessor::lower_classes(Parser &parser)
23{
24 parser().foreach_token(Class, [&](const Token &token) {
25 if (token.prev() != Enum) {
26 parser.replace(token, "struct ");
27 }
28 });
29}
30
31/* Search for constructor definition in active code. These are not supported. */
32void SourceProcessor::lint_constructors(Parser &parser)
33{
34 parser().foreach_struct([&](Token, Scope, Token struct_name, Scope struct_scope) {
35 struct_scope.foreach_match("A(..)", [&](const Tokens &t) {
36 if (t[0].scope() != struct_scope) {
37 return;
38 }
39 if (t[0].str() == struct_name.str()) {
40 report_error(t[0], "Constructors are not supported.");
41 }
42 });
43 });
44}
45
46/* Forward declaration of types are not supported and makes no sense in a shader program where
47 * there is no pointers. */
48void SourceProcessor::lint_forward_declared_structs(Parser &parser)
49{
50 parser().foreach_match("sA;", [&](const Tokens &t) {
51 if (t[0].scope().type() == ScopeType::Global) {
52 report_error(t[0], "Forward declaration of types are not supported.");
53 }
54 });
55}
56
57/* Create default initializer (empty brace) for all classes. */
58void SourceProcessor::lower_default_constructors(Parser &parser)
59{
60 unordered_set<string> builtin_types = {
61 "bool32_t", "float2", "packed_float2", "float3", "packed_float3", "float4",
62 "packed_float4", "float2x2", "float2x3", "float2x4", "float3x2", "float3x3",
63 "float3x4", "float4x2", "float4x3", "float4x4", "float2x2", "float3x3",
64 "float4x4", "int2", "int3", "packed_int3", "int4", "uint2",
65 "uint3", "packed_uint3", "uint4", "bool2", "bool3", "bool4",
66 };
67
68 parser().foreach_struct([&](Token, Scope attributes, Token name, Scope body) {
69 /* Don't do host shared structures. */
70 if (attributes.is_valid()) {
71 return;
72 }
73
74 int decl_count = 0;
75 string decl;
76 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope array, Token) {
77 auto default_value = [&](const string_view type) -> string {
78 if (type == "float") {
79 return "0.0f";
80 }
81 if (type == "uint" || type == "uchar") {
82 return "0u";
83 }
84 if (type == "int" || type == "char") {
85 return "0";
86 }
87 if (type == "bool") {
88 return "false";
89 }
90 if (builtin_types.contains(string(type))) {
91 return string(type) + "(0)";
92 }
93 return string(type) + "{}";
94 };
95
96 if (array.is_valid()) {
97 int array_len = static_array_size(array, 0);
98 if (array_len == 0) {
99 decl += "for(int i=0;i < " + string(array.str_exclusive()) + ";i++){";
100 decl += "r." + string(name.str()) + "[i]=" + default_value(type.str()) + ";";
101 decl += "}";
102 }
103 else {
104 for (int i = 0; i < array_len; i++) {
105 decl += "r." + string(name.str()) + "[" + to_string(i) + "]";
106 decl += "=" + default_value(type.str()) + ";";
107 }
108 }
109 }
110 else {
111 /* Assigning members one by one as the foreach decl iterator can be out of order. */
112 decl += "r." + string(name.str()) + "=" + default_value(type.str()) + ";";
113 }
114 decl_count++;
115 });
116
117 if (decl_count == 0) {
118 /* Empty struct will have a padding int. */
119 decl += "r._pad=0;";
120 }
121
122 decl = "static " + string(name.str()) + " ctor_() {" + string(name.str()) + " r;" + decl +
123 "return r;}";
124
125 parser.insert_after(body.front().str_index_last_no_whitespace(), decl);
126 });
127}
128
129/* Make all members of a class to be referenced using `this->`. */
130void SourceProcessor::lower_implicit_member(Parser &parser)
131{
132 parser().foreach_struct([&](Token, Scope, Token, Scope body) {
133 vector<Token> members_tokens;
134 vector<Token> methods_tokens;
135
136 auto is_class_token = [&](const vector<Token> &members, const string_view token) {
137 for (const Token &member : members) {
138 if (token == member.str()) {
139 return true;
140 }
141 }
142 return false;
143 };
144
145 auto check_shadowing = [&](const Tokens &toks) {
146 if (is_class_token(members_tokens, toks[1].str())) {
147 report_error(toks[1], "Class member shadowing.");
148 }
149 };
150
151 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope, Token) {
152 /* Do not match legacy infos in order to allow resource getter to work. */
153 if (name.scope() == body && type.str() != "ShaderCreateInfo") {
154 members_tokens.emplace_back(name);
155 }
156 });
157
158 body.foreach_function(
159 [&](bool is_static, Token, Token fn_name, Scope fn_args, bool, Scope fn_body) {
160 if (is_static) {
161 return;
162 }
163 fn_args.foreach_match("AA", check_shadowing);
164 fn_args.foreach_match("&A", check_shadowing);
165 fn_body.foreach_match("AA", check_shadowing);
166 fn_body.foreach_match("&A", check_shadowing);
167 methods_tokens.emplace_back(fn_name);
168 });
169
170 body.foreach_function([&](bool is_static, Token, Token, Scope, bool, Scope fn_body) {
171 if (is_static) {
172 return;
173 }
174 fn_body.foreach_token(Word, [&](Token tok) {
175 if (!(tok.prev().prev() == '-' && tok.prev() == '>') && tok.prev() != Dot &&
176 /* Reject namespace qualified symbols. */
177 (tok.prev() != Colon || tok.prev().prev() != Colon))
178 {
179 bool is_method = tok.next() == '(';
180 if (tok.next() == '<' && !tok.next().followed_by_whitespace()) {
181 /* Might be templated method call. */
182 is_method = tok.next().scope().back().next() == '(';
183 }
184
185 if (!is_class_token(is_method ? methods_tokens : members_tokens, tok.str())) {
186 return;
187 }
188 parser.insert_before(tok, "this->");
189 }
190 });
191 });
192 });
193
194 parser.apply_mutations();
195}
196
197void SourceProcessor::lower_this_keyword(Parser &parser)
198{
199 /* NOTE: We need to avoid the case of `a * this->b` being replaced as 2 dereferences. */
200
201 /* `(*this)` -> `(this_)` */
202 parser().foreach_match("*T)", [&](const Tokens &t) { parser.replace(t[0], t[1], "this_"); });
203 /* `return *this;` -> `return this_;` */
204 parser().foreach_match("*T;", [&](const Tokens &t) { parser.replace(t[0], t[1], "this_"); });
205 /* `this->` -> `this_.` */
206 parser().foreach_match("T->", [&](const Tokens &t) { parser.replace(t[0], t[2], "this_."); });
207}
208
209/* Move all method definition outside of struct definition blocks. */
210void SourceProcessor::lower_method_definitions(Parser &parser)
211{
212 lower_this_keyword(parser);
213 parser.apply_mutations();
214
215 parser().foreach_match("sA:", [&](const Tokens &toks) {
216 if (toks[2] == ':') {
217 report_error(toks[2], "class inheritance is not supported");
218 return;
219 }
220 });
221
222 parser().foreach_match("cAA(..)c?{..}", [&](const Tokens &toks) {
223 if (toks[0].prev() == TokenType::Const) {
224 report_error(toks[0],
225 "function return type is marked `const` but it makes no sense for values "
226 "and returning reference is not supported");
227 return;
228 }
229 });
230
231 /* Add `this` parameter and fold static keywords into function name. */
232 parser().foreach_struct([&](Token struct_tok,
233 Scope,
234 const Token struct_name,
235 const Scope struct_scope) {
236 const Scope attributes = struct_tok.prev().scope();
237 const bool is_resource_table = (attributes.type() == ScopeType::Subscript) &&
238 (attributes.str() == "[[resource_table]]");
239
240 if (is_resource_table) {
241 parser.replace(attributes, "");
242 }
243
244 struct_scope.foreach_function(
245 [&](bool is_static, Token fn_type, Token fn_name, Scope fn_args, bool is_const, Scope) {
246 const Token static_tok = is_static ? fn_type.prev() : Token(parser);
247 const Token const_tok = is_const ? fn_args.back().next() : Token(parser);
248
249 if (fn_name.str()[0] == '_') {
250 report_error(fn_name, "function name starting with an underscore are reserved");
251 }
252
253 if (is_static) {
254 parser.replace(
255 fn_name, string(struct_name.str()) + namespace_separator + string(fn_name.str()));
256 /* WORKAROUND: Erase the static keyword as it conflicts with the wrapper class
257 * member accesses MSL. */
258 parser.erase(static_tok);
259 }
260 else {
261 const bool has_no_args = fn_args.token_count() == 2;
262 const char *suffix = (has_no_args ? "" : ", ");
263 const string prefix = (is_resource_table ? "[[resource_table]] " : "");
264
265 /* Add a prefix to all member functions. */
266 parser.insert_before(fn_name, method_call_prefix);
267
268 parser.erase(const_tok);
269 if (is_const && !is_resource_table) {
270 parser.insert_after(fn_args.front(),
271 prefix + "const " + string(struct_name.str()) + " this_" +
272 suffix);
273 }
274 else {
275 parser.insert_after(fn_args.front(),
276 prefix + string(struct_name.str()) + " &this_" + suffix);
277 }
278
279 if (fn_name.str().length() > 1 &&
280 (fn_name.str().find_first_not_of("xyzw") == string::npos ||
281 fn_name.str().find_first_not_of("rgba") == string::npos))
282 {
283 report_error(fn_name, "Method name matching swizzles accessor are forbidden.");
284 }
285 }
286 });
287 });
288
289 parser.apply_mutations();
290
291 /* Copy method functions outside of struct scope. */
292 parser().foreach_struct([&](Token, Scope, const Token, const Scope struct_scope) {
293 const Token struct_end = struct_scope.back().next();
294
295 int method_len = 0;
296 struct_scope.foreach_function([&](bool, Token, Token, Scope, bool, Scope) { method_len++; });
297 if (method_len == 0) {
298 /* Avoid unnecessary preprocessor directives. */
299 return;
300 }
301
302 /* Add prototypes to allow arbitrary order of definition inside a class.
303 * Can be skipped if there is only one method. */
304 if (method_len > 1) {
305 /* First output prototypes. Not needed on metal because of wrapper class. */
306 parser.insert_after(struct_end, "\n#ifndef GPU_METAL\n");
307 struct_scope.foreach_function(
308 [&](bool is_static, Token fn_type, Token, Scope fn_args, bool, Scope) {
309 const Token fn_start = is_static ? fn_type.prev() : fn_type;
310
311 string proto_str = parser.substr_range_inclusive(fn_start, fn_args.back());
312 proto_str = strip_whitespace(proto_str) + ";\n";
313 Parser proto(proto_str, error_handler);
314
315 parser.insert_after(struct_end, proto.result_get());
316 });
317 parser.insert_after(struct_end, "#endif\n");
318 }
319
320 struct_scope.foreach_function(
321 [&](bool is_static, Token fn_type, Token, Scope, bool, Scope fn_body) {
322 const Token fn_start = is_static ? fn_type.prev() : fn_type;
323
324 string fn_str = parser.substr_range_inclusive(fn_start, fn_body.back());
325 fn_str = string(fn_start.char_number(), ' ') + fn_str + "\n";
326
327 parser.erase(fn_start, fn_body.back());
328 parser.insert_line_number(struct_end, fn_start.line_number());
329 parser.insert_after(struct_end, fn_str);
330 });
331
332 parser.insert_line_number(struct_end, struct_end.line_number(true));
333 });
334
335 parser.apply_mutations();
336}
337
338void SourceProcessor::lower_constructors(Parser &parser)
339{
340 for (FuncCall call : parser.root().descendants_of_type<FuncCall>()) {
341 if (call.identifier().str() != "_ctor") {
342 continue;
343 }
344 Token comma = call.parameters().child_first().back().next();
345 if (comma != ',') {
346 report_error(comma, "Compiler error: expecting comma");
347 }
348 parser.replace(comma, ")");
349 parser.replace(call.back(), " _rotc()");
350 }
351 if (error_handler.err) {
352 throw ParserException();
353 }
354}
355
356/* Transform `a.fn(b)` into `fn(a, b)`. */
357void SourceProcessor::lower_method_calls(Parser &parser, bool with_prefix)
358{
359 do {
360 parser().foreach_scope(ScopeType::Function, [&](Scope scope) {
361 scope.foreach_match(".A(", [&](const vector<Token> &tokens) {
362 const Token dot = tokens[0];
363 const Token func = tokens[1];
364 const Token par_open = tokens[2];
365 const Token end_of_this = dot.prev();
366 Token start_of_this = end_of_this;
367 while (true) {
368 if (start_of_this == ')') {
369 /* Function call. Take argument scope and function name. No recursion. */
370 start_of_this = start_of_this.scope().front().prev();
371 break;
372 }
373 if (start_of_this == ']') {
374 /* Array subscript. Take scope and continue. */
375 start_of_this = start_of_this.scope().front().prev();
376 continue;
377 }
378 if (start_of_this == Word) {
379 /* Member. */
380 if (start_of_this.prev() == '.') {
381 start_of_this = start_of_this.prev().prev();
382 /* Continue until we find root member. */
383 continue;
384 }
385 /* End of chain. */
386 break;
387 }
388 report_error(start_of_this.line_number(),
389 start_of_this.char_number(),
390 start_of_this.line_str(),
391 "lower_method_call parsing error");
392 break;
393 }
394 string this_str = parser.substr_range_inclusive(start_of_this, end_of_this);
395 string func_str = (with_prefix ? method_call_prefix : "") + string(func.str());
396 const bool has_no_arg = par_open.next() == ')';
397 /* `a.fn(b)` -> `_fn(a, b)` */
398 parser.replace_try(
399 start_of_this, par_open, func_str + "(" + this_str + (has_no_arg ? "" : ", "));
400 });
401 });
402 } while (parser.apply_mutations());
403}
404
405void SourceProcessor::lower_structured_bindings(Parser &parser)
406{
407 auto get_function_return_type = [&](string_view fn_name) {
408 string return_type;
409 parser().foreach_function([&](bool, Token type, Token name, Scope, bool, Scope) {
410 if (name.str() != fn_name) {
411 return;
412 }
413 return_type = type.str();
414 });
415 return return_type;
416 };
417
418 auto get_argument_type = [&](Scope args, string_view arg_name) {
419 string return_type;
420 args.foreach_scope(ScopeType::FunctionArg, [&](Scope arg) {
421 const Token name = arg.back();
422 if (name == ']') {
423 /* No array support for now. */
424 return;
425 }
426 if (name.str() == arg_name) {
427 Token type = name.prev() == '&' ? name.prev(2) : name.prev();
428 return_type = type.str();
429 }
430 });
431 return return_type;
432 };
433
434 auto get_local_symbol_type = [&](Scope fn_body, Token symbol) {
435 string return_type;
436 string_view symbol_str = symbol.str();
437 fn_body.foreach_token(Word, [&](Token tok) {
438 if (tok != Word || tok.str() != symbol_str || tok.index_ >= symbol.index_) {
439 return;
440 }
441 /* Check if it is in a visible scope. */
442 Scope tok_scope = tok.scope();
443 if (tok_scope != symbol.scope() && !tok_scope.contains(symbol.scope())) {
444 return;
445 }
446 /* Check if it is a definition. */
447 /* TODO(fclem): Comma declaration. */
448 bool is_reference = tok.prev() == Ampersand;
449 if (is_reference ? (tok.next() != '=' || tok.prev(2) != Word) : (tok.prev() != Word)) {
450 return;
451 }
452 Token type_tok = tok.prev(is_reference ? 2 : 1);
453 return_type = type_tok.str();
454 });
455 return return_type;
456 };
457
458 auto get_struct_members = [&](string_view struct_name) {
459 /* Search in symbol table first. */
460 for (const auto &symbol : metadata_.symbol_table) {
461 if (symbol.is_struct && symbol.identifier == struct_name) {
462 return symbol.members;
463 }
464 }
465
466 /* Search symbols inside this file. This can help find instantiated structs. */
467 vector<pair<string, string>> members;
468 parser().foreach_match("sA{", [&](const Tokens &t) {
469 if (t[1].str() != struct_name) {
470 return;
471 }
472 Scope body = t.back().scope();
473 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token name, Scope, Token) {
474 members.emplace_back(type.str(), name.str());
475 });
476 });
477 return members;
478 };
479
480 parser().foreach_function([&](bool, Token, Token, Scope args, bool, Scope body) {
481 /* Unique index per binding to avoid to mess with scopes. */
482 int index = 0;
483 body.foreach_match("A[..]=", [&](const Tokens &t) {
484 if (t[0].str() != "auto") {
485 return;
486 }
487 Token symbol_name = t.back().next(1);
488 /* For now only support expanding form a single function call. */
489 if (symbol_name != Word) {
490 report_error(symbol_name, "Expected either a function call or a single variable");
491 return;
492 }
493
494 Token after_symbol = Token::invalid(&parser);
495 string struct_type;
496 if (symbol_name.next(1) == '(') {
497 after_symbol = symbol_name.next(1).scope().back().next();
498 if (after_symbol != ';') {
499 report_error(after_symbol, "Expected single function call");
500 return;
501 }
502 struct_type = get_function_return_type(symbol_name.str());
503 if (struct_type.empty()) {
504 report_error(symbol_name,
505 "Couldn't infer function return type. Can be caused by overload.");
506 return;
507 }
508 }
509 else {
510 after_symbol = symbol_name.next(1);
511 if (after_symbol != ';') {
512 report_error(after_symbol, "Expected single symbol to unpack");
513 return;
514 }
515 struct_type = get_local_symbol_type(body, symbol_name);
516
517 if (struct_type.empty()) {
518 struct_type = get_argument_type(args, symbol_name.str());
519 }
520
521 if (struct_type.empty()) {
522 report_error(symbol_name, "Couldn't infer local symbol type.");
523 return;
524 }
525 }
526
527 string struct_var = "_u" + to_string(index);
528
529 parser.replace(t[0], t[4], struct_type + " " + struct_var);
530
531 vector<pair<string, string>> struct_members = get_struct_members(struct_type);
532
533 if (struct_members.empty()) {
534 report_error(symbol_name, "Couldn't find type to unpack.");
535 return;
536 }
537
538 Scope var_list = t[1].scope();
539 string assignments = ";";
540 int member_index = 0;
541 var_list.foreach_token(Word, [&](Token tok) {
542 if (struct_members.size() > member_index) {
543 auto [member_type, member_name] = struct_members[member_index];
544 assignments += member_type + " " + string(tok.str()) + "=" + struct_var + "." +
545 member_name + ";";
546 member_index++;
547 }
548 else {
549 report_error(tok, "Too many parameters in structured binding");
550 }
551 });
552 if (struct_members.size() != member_index) {
553 report_error(t[4], "Missing parameters in structured binding");
554 }
555 /* Drop trailing semicolon. */
556 assignments = assignments.substr(0, assignments.size() - 1);
557
558 parser.insert_after(after_symbol.prev(), assignments);
559 index++;
560 });
561 });
562
563 parser.apply_mutations();
564}
565
566void SourceProcessor::lower_method_forward_declaration(Parser &parser)
567{
568 for (Preprocessor directive : parser.root().children_of_type<Preprocessor>()) {
569 if (!directive.str().starts_with("#pragma member_forward_decl")) {
570 continue;
571 }
572 string_view cls_name = directive.back().str();
573 FuncForwardDecl fwd_decl = directive.next();
574 if (!fwd_decl.is_valid()) {
575 report_error(
576 directive.front(),
577 "Compiler error: expecting forward declaration after 'pragma member_forward_decl'");
578 }
579 /* Search for declaration. */
580 ClassDecl cls_decl = {};
581 for (ClassDecl decl : parser.root().children_of_type<ClassDecl>()) {
582 if (decl.identifier().str() == cls_name) {
583 cls_decl = decl;
584 break;
585 }
586 }
587 if (!cls_decl.is_valid()) {
588 report_error(fwd_decl.front(),
589 "Compiler error: can't locate class instantiation of '" + string(cls_name) +
590 "'");
591 }
592 /* Move the forward declaration. */
593 parser.insert_after(cls_decl.back(),
594 "#ifndef GPU_METAL\n" + string(fwd_decl.str()) + "\n#endif\n");
595 parser.replace(directive.front(), fwd_decl.back(), "");
596 }
597 if (error_handler.err) {
598 throw ParserException();
599 }
600 parser.apply_mutations();
601}
602
603} // namespace blender::gpu::shader
static std::string strip_whitespace(const std::string &str)
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
Definition processor.hh:45
static constexpr const char * namespace_separator
Definition processor.hh:51
std::vector< parser::Token > Tokens
Definition processor.hh:48
static constexpr const char * method_call_prefix
Definition processor.hh:53
#define str(s)
#define index(i)
float dot(VecOp< float, D >, VecOp< float, D >) RET
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
SymEdge< T > * prev(const SymEdge< T > *se)
@ Enum
Definition types.hh:106
@ Ampersand
Definition types.hh:50
@ Dot
Definition types.hh:48
@ Colon
Definition types.hh:65
@ Word
Definition types.hh:43
@ Class
Definition types.hh:118
void foreach_function(Callback callback) const
Definition scope.hh:399
void foreach_token(const TokenType token_type, Callback callback) const
Definition scope.hh:380
Token prev(int i=1) const
Definition lexit.hh:376
std::string_view str() const
Definition lexit.hh:381
static Token invalid(const TokenBuffer *buf)
Definition lexit.hh:349
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337
Token next(int i=1) const
Definition lexit.hh:372