Blender V5.3
processor.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 <cctype>
10#include <cstdint>
11#include <filesystem>
12#include <fstream>
13#include <iostream>
14#include <string>
15#include <unordered_map>
16#include <unordered_set>
17#include <vector>
18
19#include "intermediate.hh"
20#include "metadata.hh"
21#include "processor.hh"
22
23#include "bsl/symbol_table.hh"
24
25namespace blender::gpu::shader {
26using namespace std;
27using namespace shader::parser;
28using namespace metadata;
29using namespace shader::parser::ast;
30
31SourceProcessor::Result SourceProcessor::convert_glsl()
32{
33 metadata_ = {};
34
35 string str = this->source_;
36
38
39 IntermediateForm<SimpleLexer, DummyParser> parser(error_handler);
40 try {
41 parser.language = Language::GLSL;
42 parser.set_str(str);
43 /* Remove trailing white space as they make the subsequent transformation much slower. */
44 cleanup_whitespace(parser);
45 str = parser.result_get();
46 str = threadgroup_variables_parse_and_remove(str);
47 }
48 catch (ParserException & /*e*/) {
49 /* Output the current source state for inspection. */
50 return {parser.result_get(), metadata_, error_handler.err};
51 }
52
53 parse_builtins(str, filename, true);
54#ifdef __APPLE__ /* Limiting to Apple hardware since GLSL compilers might have issues. */
55 str = matrix_constructor_mutation(str);
56#endif
57 str = argument_decorator_macro_injection(str);
58 str = array_constructor_macro_injection(str);
59 str = line_directive_prefix(filename) + str;
60 return {str, metadata_, error_handler.err};
61}
62
63SourceProcessor::Result SourceProcessor::convert_msl()
64{
65 metadata_ = {};
66
67 string str = this->source_;
68
70
71 {
72 IntermediateForm<SimpleLexer, DummyParser> parser(str, error_handler);
73 /* Remove trailing white space as they make the subsequent regex much slower. */
74 cleanup_whitespace(parser);
75 str = parser.result_get();
76 }
77
78 str = threadgroup_variables_parse_and_remove(str);
79
80 Parser parser(error_handler);
81 try {
82 parser.language = Language::MSL;
83 parser.set_str(str);
84 parse_pragma_runtime_generated(parser);
85 parse_includes(parser);
86 lower_preprocessor(parser);
87 str = parser.result_get();
88 }
89 catch (ParserException & /*e*/) {
90 /* Output the current source state for inspection. */
91 return {parser.result_get(), metadata_, error_handler.err};
92 }
93
94 str = argument_decorator_macro_injection(str);
95 str = array_constructor_macro_injection(str);
96 str = line_directive_prefix(filename) + str;
97 return {str, metadata_, error_handler.err};
98}
99
100SourceProcessor::Result SourceProcessor::convert_bsl_legacy(
101 metadata::Source external_sources_symbols)
102{
103 metadata_ = {};
104
105 /* Only use symbols and templates from external sources. */
106 metadata_.symbol_table.insert(metadata_.symbol_table.end(),
107 external_sources_symbols.symbol_table.begin(),
108 external_sources_symbols.symbol_table.end());
109 metadata_.template_definitions.insert(metadata_.template_definitions.end(),
110 external_sources_symbols.template_definitions.begin(),
111 external_sources_symbols.template_definitions.end());
112
113 /* Set line number for each symbol to 0 as they are defined outside of the target file. */
114 for (auto &symbol : metadata_.symbol_table) {
115 symbol.definition_line = 0;
116 }
117
118 string str = remove_comments(this->source_);
119
120 Parser parser(error_handler);
121 try {
122 parser.set_str(str);
123
124 disabled_code_mutation(parser);
125 /* Legacy GLSL compat. */
126 threadgroup_variables_parse_and_remove(parser);
127 parse_builtins(parser, filename);
128 /* Preprocessor directive parsing & linting. */
129 lint_pragma_once(parser, filename);
130 parse_pragma_runtime_generated(parser);
131 parse_includes(parser);
132 parse_defines(parser);
133 parse_library_functions(parser);
134
135 lower_preprocessor(parser);
136
137 parser.apply_mutations();
138
139 /* Early out for certain files. */
140 if (parser.str().find("\n#pragma no_processing") != string::npos) {
141 cleanup_whitespace(parser);
142 return {line_directive_prefix(filename) + parser.result_get(), metadata_, error_handler.err};
143 }
144
145 /* Lower high level parsing complexity.
146 * Merge tokens that can be combined together,
147 * remove the token that are unsupported or that are noop.
148 * All these steps should be independent. */
149 lower_namesless_parameters(parser);
150 lower_attribute_sequences(parser);
151 lower_strings_sequences(parser);
152 lower_swizzle_methods(parser);
153 lower_binary_literals(parser);
154 lower_classes(parser);
155 lower_noop_keywords(parser);
156 lower_trailing_comma_in_list(parser);
157 lower_comma_separated_declarations(parser);
158 lower_assert(parser, filename);
159 /* Lower implicit members before we remove SRT member from their struct. */
160 lower_implicit_member(parser);
161
162 parser.apply_mutations();
163
164 parse_local_symbols(parser);
165
166 /* Linting phase. Detect valid syntax with invalid usage. */
167 lint_unbraced_statements(parser);
168 lint_reserved_tokens(parser);
169 lint_attributes(parser);
170 lint_global_scope_constants(parser);
171 lint_constructors(parser);
172 lint_forward_declared_structs(parser);
173
174 /* All mutations that needs to also be applied on template definitions. */
175 lower_pre_template(parser);
176 /* Lower templates. */
177 lower_templates(parser);
178 /* Lower unions and then lint shared structures. */
179 lower_unions(parser);
180 lower_host_shared_structures(parser);
181 /* Lower enums. */
182 lower_enums(parser);
183 /* Lower SRT and Interfaces. */
184 lower_entry_points(parser);
185 lower_pipeline_definition(parser, filename);
186 lower_resource_table(parser);
187 lower_resource_access_functions(parser);
188 /* Lower class methods. */
189 lower_default_constructors(parser);
190 lower_function_default_arguments(parser);
191 lower_method_definitions(parser);
192 lower_method_calls(parser);
193 lower_empty_struct(parser);
194 /* Lower SRT accesses. */
195 lower_srt_member_access(parser);
196 lower_srt_arguments(parser);
197 lower_entry_points_signature(parser);
198 lower_stage_function(parser);
199 /* Lower string, assert, printf. */
200 lower_strings(parser);
201 lower_printf(parser);
202 /* Lower other C++ constructs. */
203 lower_implicit_return_types(parser);
204 lower_initializer_implicit_types(parser);
205 lower_designated_initializers(parser);
206 lower_aggregate_initializers(parser);
207 lower_array_initializations(parser);
208 lower_scope_resolution_operators(parser);
209 lower_structured_bindings(parser);
210 lower_tests(parser);
211 /* Lower references. */
212 lower_reference_arguments(parser);
213 lower_reference_variables(parser);
214 /* Lower control flow. */
215 lower_static_branch(parser);
216 /* Unroll last to avoid processing more tokens in other phases. */
217 lower_loop_unroll(parser);
218
219 /* GLSL syntax compatibility.
220 * TODO(fclem): Remove. */
221 lower_argument_qualifiers(parser);
222 lower_gather_component(parser);
223
224 /* Cleanup to make output more human readable and smaller for runtime. */
225 cleanup_whitespace(parser);
226 cleanup_empty_lines(parser);
227 cleanup_line_directives(parser);
228
229 str = parser.result_get();
230 }
231 catch (ParserException & /*e*/) {
232 /* Output the current source state for inspection. */
233 return {parser.result_get(), metadata_, error_handler.err};
234 }
235
236 str = line_directive_prefix(filename) + str;
237 return {str, metadata_, error_handler.err};
238}
239
240SourceProcessor::Result SourceProcessor::convert_bsl()
241{
242 metadata_ = {};
243
244 string str = remove_comments(this->source_);
245 /* Add source file line directive first so that error lines are correct. */
246 str = line_directive_prefix(filename) + str;
247 /* Define `BSL_530` macro for this file only. Needed for compatibility with old BSL version? */
248 str = "#define BSL_530\n" + str + "\n#undef BSL_530\n";
249
250 SourceManager sources;
251
252 /* Init builtin parser to add builtin symbols. */
253 Parser &builtin_parser = sources.new_source(error_handler);
254 builtin_parser.language = Language::BSL;
255 builtin_parser.set_str("#line 1 \"builtin\"\n#error This should not be emitted\n");
256 builtin_parser.include_id = sources.include_id_get();
257 /* Init symbol table and add builtin symbols. */
258 bsl::SymbolTable symbols(builtin_parser);
259
260 Parser &parser = sources.new_source(error_handler);
261 try {
262 /* Allow CPP grammar until we remove #ifndef GPU_SHADER blocks. */
263 parser.language = Language::CPP;
264 parser.set_str(str);
265
266 disabled_code_mutation(parser, true);
267 /* Preprocessor directive parsing & linting. */
268 lint_pragma_once(parser, filename);
269 parse_pragma_runtime_generated(parser);
270 parse_includes(parser);
271 parse_defines(parser);
272 lower_tests(parser, "srt.");
273
274 parser.only_apply_mutations();
275
276 scan_external_symbols(sources, symbols, metadata_.visited_files);
277
278 parser.include_id = sources.include_id_get();
279
280 parser.language = Language::BSL;
281 parser.parse(error_handler);
282
283 parse_draw_debug(parser, filename);
284 parse_library_functions_ast(parser);
285 lower_preprocessor_ast(parser);
286
287 /* Lower high level parsing complexity.
288 * Merge tokens that can be combined together,
289 * remove the token that are unsupported or that are noop.
290 * All these steps should be independent. */
291 lower_namesless_parameters_ast(parser);
292 lower_attribute_sequences_ast(parser);
293 lower_strings_sequences(parser);
294 lower_swizzle_methods_ast(parser);
295 lower_binary_literals(parser);
296 lower_noop_keywords_ast(parser);
297 lower_trailing_comma_in_list_ast(parser);
298 lower_assert_ast(parser, filename);
299 lower_this_keyword(parser);
300 parser.apply_mutations();
301
302 /* Lower string, assert, printf. */
303 lower_strings(parser);
304 lower_printf(parser);
305
306 /* Linting phase. Detect valid syntax with invalid usage. */
307 lint_reserved_tokens(parser);
308 lint_attributes_ast(parser);
309
310 lower_srt_accessor_templates_ast(parser); /* Legacy. To remove. */
311 lower_union_accessor_templates_ast(parser); /* Legacy. To remove. */
312
313 symbols.parse(parser.root(), error_handler);
314
315 lower_bsl_to_il(parser, symbols);
316 /* Lower SRT and Interfaces. */
317 lower_pipeline_definition(parser, filename);
318 /* Lower class methods. */
319 lower_method_forward_declaration(parser);
320 lower_union_setters(parser);
321 lower_bitfield_setters(parser);
322 lower_method_calls(parser, false);
323 /* Needs to be last. */
324 lower_resource_macro_placeholder_ast(parser);
325 lower_constructors(parser);
326
327 parser.language = Language::IL;
328 parser.apply_mutations();
329
330 /* GLSL syntax compatibility. */
331 lower_reference_arguments(parser);
332 lower_argument_qualifiers(parser);
333 lower_gather_component(parser);
334
335 /* Cleanup to make output more human readable and smaller for runtime. */
336 cleanup_whitespace(parser, true);
337 cleanup_empty_lines(parser);
338 cleanup_line_directives(parser);
339
340 str = parser.result_get();
341 }
342 catch (ParserException & /*e*/) {
343 /* Output the current source state for inspection. */
344 return {parser.result_get(), metadata_, error_handler.err};
345 }
346 return {str, metadata_, error_handler.err};
347}
348
349SourceProcessor::Result SourceProcessor::convert_info()
350{
351 metadata_ = {};
352
353 string str = remove_comments(this->source_);
354
355 Parser parser(error_handler);
356 try {
357 parser.set_str(str);
358
359 disabled_code_mutation(parser);
360 /* Legacy GLSL compat. */
361 threadgroup_variables_parse_and_remove(parser);
362 parse_builtins(parser, filename);
363 /* Preprocessor directive parsing & linting. */
364 lint_pragma_once(parser, filename);
365 parse_pragma_runtime_generated(parser);
366 parse_includes(parser);
367 parse_defines(parser);
368 parse_legacy_create_info(parser);
369
370 lower_preprocessor(parser);
371
372 /* Cleanup to make output more human readable and smaller for runtime. */
373 cleanup_whitespace(parser);
374 cleanup_empty_lines(parser);
375 cleanup_line_directives(parser);
376
377 str = parser.result_get();
378 }
379 catch (ParserException & /*e*/) {
380 /* Output the current source state for inspection. */
381 return {parser.result_get(), metadata_, error_handler.err};
382 }
383
384 return {str, metadata_, error_handler.err};
385}
386
388{
389 switch (language_) {
390 case Language::INFO:
391 return convert_info();
392 case Language::CPP:
393 /* Should become BSL, but until the new compiler is fully working, fallback
394 * to the legacy path. */
395 return (filename.starts_with("eevee_") || filename.starts_with("draw_")) ?
396 convert_bsl() :
397 convert_bsl_legacy(external_sources_symbols);
398 case Language::BSL:
399 return convert_bsl(); /* WIP */
401 return convert_bsl_legacy(external_sources_symbols);
402 case Language::MSL:
403 return convert_msl();
404 case Language::GLSL:
405 return convert_glsl();
407 default:
408 break;
409 }
410
411 metadata_ = {};
412 report_error(0, 0, "", "Unknown file type");
413 return {"", metadata_, error_handler.err};
414}
415
417{
418 metadata_ = {};
419
420 string str = remove_comments(this->source_);
421
422 Parser parser(error_handler);
423 try {
424 parser.set_str(str);
425 disabled_code_mutation(parser);
426 parse_pragma_runtime_generated(parser);
427 parse_includes(parser);
428
429 parser.apply_mutations();
430
431 lower_preprocessor(parser);
432
433 parser.apply_mutations();
434
435 /* Lower high level parsing complexity.
436 * Merge tokens that can be combined together,
437 * remove the token that are unsupported or that are noop.
438 * All these steps should be independent. */
439 lower_namesless_parameters(parser);
440 lower_attribute_sequences(parser);
441 lower_strings_sequences(parser);
442 lower_swizzle_methods(parser);
443 lower_classes(parser);
444 lower_noop_keywords(parser);
445 lower_trailing_comma_in_list(parser);
446 lower_comma_separated_declarations(parser);
447 lower_assert(parser, filename);
448 /* Lower implicit members before we remove SRT member from their struct. */
449 lower_implicit_member(parser);
450
451 parser.apply_mutations();
452
453 parse_local_symbols(parser);
454 }
455 catch (ParserException & /*e*/) {
456 /* Expect that the parsing will generate error when the file itself is compiled. */
457 return {};
458 }
459
460 return metadata_;
461}
462
463void SourceProcessor::scan_external_symbols(SourceManager &sources,
464 bsl::SymbolTable &symbols,
465 vector<string> &visited_files)
466{
467 for (const auto &dep : metadata_.dependencies) {
468 string file;
469 for (const auto &filename : file_list_) {
470 if (std::filesystem::path(filename).filename() == dep) {
471 file = filename;
472 break;
473 }
474 }
475
476 if (file.empty()) {
477 report_error(0, 0, "", "Error: Included file not found " + dep);
478 throw ParserException();
479 }
480
481 if (ranges::find(visited_files, file) == visited_files.end()) {
482 visited_files.emplace_back(file);
483
484 ifstream input_file(file);
485 if (!input_file) {
486 report_error(0, 0, "", "Error: Could not open file " + file);
487 throw ParserException();
488 }
489
490 stringstream buffer;
491 buffer << input_file.rdbuf();
492
493 Language language = language_from_filename(file);
494 SourceProcessor processor(buffer.str(), file, language, file_list_);
495 /* Recursive. */
496 processor.parse_include_and_symbols(sources, symbols, visited_files);
497
498 /* If an error occur, cancel everything and let the error bubble up. */
499 if (processor.error_handler.err) {
500 this->error_handler.err = processor.error_handler.err;
501 throw ParserException();
502 }
503 }
504 }
505}
506
508 bsl::SymbolTable &symbols,
509 vector<string> &visited_files)
510{
511 string str = remove_comments(this->source_);
512 /* Add source file line directive first so that error lines are correct. */
513 str = line_directive_prefix(filename) + str;
514
515 Parser &parser = sources.new_source(error_handler);
516 try {
517 parser.set_str(str);
518 disabled_code_mutation(parser, true);
519 parse_pragma_runtime_generated(parser);
520 parse_includes(parser);
521
522 if (language_ == Language::INFO) {
523 return metadata_;
524 }
525
526 if (language_ == Language::GLSL || language_ == Language::BLENDER_GLSL) {
527 parser().foreach_match("A(A)", [&](Tokens toks) {
528 string_view fn_name = toks[0].str();
529 if (fn_name == "SHADER_LIBRARY_CREATE_INFO" || fn_name == "VERTEX_SHADER_CREATE_INFO" ||
530 fn_name == "FRAGMENT_SHADER_CREATE_INFO" || fn_name == "COMPUTE_SHADER_CREATE_INFO")
531 {
532 parser.erase(toks.front(), toks.back().next() == ';' ? toks.back().next() : toks.back());
533 }
534 });
535 }
536
537 parser.apply_mutations();
538
539 lower_preprocessor(parser);
540
541 parser.language = Language::BSL;
542 parser.only_apply_mutations();
543 parser.parse(error_handler);
544
545 lower_namesless_parameters_ast(parser);
546 lower_attribute_sequences_ast(parser);
547
548 parser.apply_mutations();
549
550 scan_external_symbols(sources, symbols, visited_files);
551
552 parser.include_id = sources.include_id_get();
553
554 lower_srt_accessor_templates_ast(parser); /* Legacy. To remove. */
555 lower_union_accessor_templates_ast(parser); /* Legacy. To remove. */
556
557 symbols.parse(parser.root(), error_handler);
558 }
559 catch (ParserException & /*e*/) {
560 /* Expect that the parsing will generate error when the file itself is compiled. */
561 return {};
562 }
563
564 return metadata_;
565}
566
567string SourceProcessor::remove_comments(const string &str)
568{
569 string out_str = str;
570 {
571 /* Multi-line comments. */
572 size_t start, end = 0;
573 while ((start = out_str.find("/*", end)) != string::npos) {
574 end = out_str.find("*/", start + 2);
575 if (end == string::npos) {
576 break;
577 }
578 for (size_t i = start; i < end + 2; ++i) {
579 if (out_str[i] != '\n') {
580 out_str[i] = ' ';
581 }
582 }
583 }
584
585 if (end == string::npos) {
586 report_error(line_number(out_str, start),
587 char_number(out_str, start),
588 line_str(out_str, start),
589 "Malformed multi-line comment.");
590 return out_str;
591 }
592 }
593 {
594 /* Single-line comments. */
595 size_t start, end = 0;
596 while ((start = out_str.find("//", end)) != string::npos) {
597 end = out_str.find('\n', start + 2);
598 if (end == string::npos) {
599 end = out_str.size();
600 }
601 for (size_t i = start; i < end; ++i) {
602 out_str[i] = ' ';
603 }
604 }
605 }
606 return out_str;
607}
608
610{
611 parser().foreach_token(TokenType::Comment, [&](Token tok) { parser.erase(tok); });
612 parser.apply_mutations();
613}
614
615/* Remove trailing white spaces. */
616template<typename ParserT>
617void SourceProcessor::cleanup_whitespace(ParserT &parser, bool do_leading)
618{
619 const string &str = parser.str();
620
621 if (do_leading) {
622 /* Cleanup leading white-spaces at the start of the file.
623 * Only to be done if there is a line directive at the top of the file. */
624 size_t first_char = str.find_first_not_of(" \n");
625 if (first_char != 0 && first_char != string::npos) {
626 parser.replace(0, first_char - 1, "");
627 }
628 }
629
630 size_t last_whitespace = -1;
631 while ((last_whitespace = str.find(" \n", last_whitespace + 1)) != string::npos) {
632 size_t first_not_whitespace = str.find_last_not_of(" ", last_whitespace);
633 if (first_not_whitespace == string::npos) {
634 first_not_whitespace = 0;
635 }
636 parser.replace(first_not_whitespace + 1, last_whitespace, "");
637 }
638 parser.apply_mutations();
639}
640
641/* Parse defines in order to output them with the create infos.
642 * This allow the create infos to use shared defines values. */
643void SourceProcessor::parse_defines(Parser &parser)
644{
645 parser().foreach_match<true>("#A", [&](const vector<Token> &tokens) {
646 if (tokens[1].str() == "define") {
647 if (tokens[1].next().str().starts_with("gather_")) {
648 /* WORKAROUND: Avoid warning caused by EEVEE macro setup. */
649 return;
650 }
651
652 metadata_.create_infos_defines.emplace_back(tokens[1].next().scope().str_with_whitespace());
653 }
654 if (tokens[1].str() == "undef") {
655 metadata_.create_infos_defines.emplace_back(tokens[1].next().scope().str_with_whitespace());
656 }
657 });
658}
659
661{
662 string placeholder;
663 placeholder += "#ifdef CREATE_INFO_RES_PASS_" + name + "\n";
664 placeholder += "CREATE_INFO_RES_PASS_" + name + "\n";
665 placeholder += "#endif\n";
666 placeholder += "#ifdef CREATE_INFO_RES_BATCH_" + name + "\n";
667 placeholder += "CREATE_INFO_RES_BATCH_" + name + "\n";
668 placeholder += "#endif\n";
669 placeholder += "#ifdef CREATE_INFO_RES_GEOMETRY_" + name + "\n";
670 placeholder += "CREATE_INFO_RES_GEOMETRY_" + name + "\n";
671 placeholder += "#endif\n";
672 placeholder += "#ifdef CREATE_INFO_RES_SHARED_VARS_" + name + "\n";
673 placeholder += "CREATE_INFO_RES_SHARED_VARS_" + name + "\n";
674 placeholder += "#endif\n";
675 return placeholder;
676};
677
678/* Legacy create info parsing and removing. */
679void SourceProcessor::parse_legacy_create_info(Parser &parser)
680{
681 parser().foreach_scope(ScopeType::Attributes, [&](const Scope attrs) {
682 if (attrs.str_with_whitespace() != "[resource_table]") {
683 return;
684 }
685 Token type = attrs.scope().back().next();
686 Token struct_keyword = attrs.scope().front().prev();
687 if (type != Word || struct_keyword != Struct) {
688 return;
689 }
690 parser.insert_before(struct_keyword, get_create_info_placeholder(string(type.str())));
691 parser.insert_line_number(struct_keyword.str_index_start() - 1, struct_keyword.line_number());
692 });
693
694 parser().foreach_match("A(..)", [&](const vector<Token> &tokens) {
695 if (tokens[0].str() == "CREATE_INFO_VARIANT") {
696 const string variant_name(tokens[1].scope().front().next().str());
697 metadata_.create_infos.emplace_back(variant_name);
698
699 const string variant_decl = parser.substr_range_inclusive(tokens.front(), tokens.back());
700 metadata_.create_infos_declarations.emplace_back(variant_decl);
701
702 parser.replace(tokens.front(), tokens.back(), get_create_info_placeholder(variant_name));
703 return;
704 }
705 if (tokens[0].str() == "GPU_SHADER_CREATE_INFO") {
706 const string variant_name(tokens[1].scope().front().next().str());
707 metadata_.create_infos.emplace_back(variant_name);
708
709 const size_t start_end = tokens.back().str_index_last();
710 const string end_tok = "GPU_SHADER_CREATE_END()";
711 const size_t end_pos = parser.str().find(end_tok, start_end);
712 if (end_pos == string::npos) {
713 report_error(tokens[0], "Missing create info end.");
714 return;
715 }
716
717 const string variant_decl = parser.substr_range_inclusive(tokens.front().str_index_start(),
718 end_pos + end_tok.size());
719 metadata_.create_infos_declarations.emplace_back(variant_decl);
720
721 parser.replace(tokens.front().str_index_start(),
722 end_pos + end_tok.size(),
723 get_create_info_placeholder(variant_name));
724 return;
725 }
726 if (tokens[0].str() == "GPU_SHADER_NAMED_INTERFACE_INFO") {
727 const size_t start_end = tokens.back().str_index_last();
728 const string end_str = "GPU_SHADER_NAMED_INTERFACE_END(";
729 size_t end_pos = parser.str().find(end_str, start_end);
730 if (end_pos == string::npos) {
731 report_error(tokens[0], "Missing create info end.");
732 return;
733 }
734
735 end_pos = parser.str().find(')', end_pos);
736 if (end_pos == string::npos) {
737 report_error(tokens[0], "Missing parenthesis at info end.");
738 return;
739 }
740
741 const string variant_decl = parser.substr_range_inclusive(tokens.front().str_index_start(),
742 end_pos);
743 metadata_.create_infos_declarations.emplace_back(variant_decl);
744
745 parser.erase(tokens.front().str_index_start(), end_pos);
746 return;
747 }
748 if (tokens[0].str() == "GPU_SHADER_INTERFACE_INFO") {
749 const size_t start_end = tokens.back().str_index_last();
750 const string end_str = "GPU_SHADER_INTERFACE_END()";
751 size_t end_pos = parser.str().find(end_str, start_end);
752 if (end_pos == string::npos) {
753 report_error(tokens[0], "Missing create info end.");
754 return;
755 }
756 const string variant_decl = parser.substr_range_inclusive(tokens.front().str_index_start(),
757 end_pos + end_str.size());
758 metadata_.create_infos_declarations.emplace_back(variant_decl);
759
760 parser.erase(tokens.front().str_index_start(), end_pos + end_str.size());
761 return;
762 }
763 });
764
765 parser.apply_mutations();
766}
767
768/* Return the content without the first and last characters. */
769static std::string_view str_view_exclusive(Token tok)
770{
771 std::string_view str = tok.str();
772 if (str.length() < 2) {
773 return "";
774 }
775 return str.substr(1, str.length() - 2);
776}
777
778void SourceProcessor::parse_includes(Parser &parser)
779{
780 parser().foreach_match<true>("#A\"", [&](const vector<Token> &tokens) {
781 if (tokens[1].str() != "include") {
782 return;
783 }
784 string_view dependency_name = str_view_exclusive(tokens[2]);
785
786 if (dependency_name.find("defines.hh") != string::npos ||
787 /* WORKAROUND(fclem): Only needed in EEVEE and overlays for now.
788 * Needs the file to be in the same folder. */
789 (dependency_name.ends_with(".bsl.hh") && filename.ends_with(".bsl.hh") &&
790 ((dependency_name.starts_with("eevee_") && filename.starts_with("eevee_")) ||
791 (dependency_name.starts_with("overlay_") && filename.starts_with("overlay_")))))
792 {
793 /* Dependencies between create infos are not needed for reflections.
794 * Only the dependencies on the defines are needed. */
795 if (dependency_name.ends_with(".bsl.hh")) {
796 metadata_.create_infos_dependencies.emplace_back(string(dependency_name) + ".info");
797 }
798 else {
799 metadata_.create_infos_dependencies.emplace_back(dependency_name);
800 }
801 }
802
803 if (dependency_name == "BLI_utildefines_variadic.hh") {
804 /* Skip GLSL-C++ stubs. They are only for IDE linting. */
805 parser.erase(tokens.front(), tokens.back());
806 return;
807 }
808 if (dependency_name == "gpu_shader_compat.hh") {
809 /* Skip GLSL-C++ stubs. They are only for IDE linting. */
810 parser.erase(tokens.front(), tokens.back());
811 return;
812 }
813 if (dependency_name.find("gpu_shader_create_info.hh") != string::npos) {
814 /* Skip info files. They are only for IDE linting. */
815 parser.erase(tokens.front(), tokens.back());
816 return;
817 }
818
819 if (dependency_name.find("infos/") != string::npos) {
820 dependency_name = dependency_name.substr(6);
821 }
822
823 if (dependency_name == filename) {
824 report_error(tokens[2], "Recursive include");
825 }
826 metadata_.dependencies.emplace_back(dependency_name);
827 });
828 parser.apply_mutations();
829}
830
831bool SourceProcessor::has_pragma(Parser &parser, string_view pragma_str)
832{
833 bool has_pragma = false;
834 /* Can't use foreach_match because it skips preprocessor scopes. */
835 parser().foreach_token(Hash, [&](Token tok) {
836 if (tok.scope().type() == ScopeType::Preprocessor && tok.next(1).str() == "pragma" &&
837 tok.next(2).str() == pragma_str)
838 {
839 has_pragma = true;
840 }
841 });
842 return has_pragma;
843}
844
845void SourceProcessor::parse_pragma_runtime_generated(Parser &parser)
846{
847 if (has_pragma(parser, "runtime_generated")) {
848 metadata_.builtins.emplace_back(metadata::Builtin::runtime_generated);
849 }
850}
851
852void SourceProcessor::lint_pragma_once(Parser &parser, const string &filename)
853{
854 if (filename.find("_lib.") == string::npos && filename.find(".hh") == string::npos) {
855 return;
856 }
857 if (!has_pragma(parser, "once")) {
858 report_error(parser[0], "Header files must contain #pragma once directive.");
859 }
860}
861
862void SourceProcessor::lower_namesless_parameters(Parser &parser)
863{
864 parser().foreach_token(ParOpen, [&](Token tok) {
865 if (tok.scope().type() != ScopeType::FunctionArgs) {
866 return;
867 }
868 if (tok.prev(2).str().starts_with("Pipeline")) {
869 return;
870 }
871 /* Make sure we matched a function definition and not a macro call. */
872 if (tok.prev() != '>' && tok.prev(2) != Word && tok.prev(2) != '>') {
873 return;
874 }
875 int i = 0;
876 tok.scope().foreach_scope(ScopeType::FunctionArg, [&](Scope arg) {
877 /* Skip array declarator, e.g. `float[4]`. */
878 Token back = arg.back();
879 while (back == ']') {
880 back = back.scope().front().prev();
881 }
882 const bool is_single_token = back.str_index_start() == arg.front().str_index_start();
883 if (is_single_token || back.prev() == TokenType::Const || back == '&' || back == '>') {
884 /* Append a name for nameless argument. */
885 parser.replace(back.str_index_last_no_whitespace() + 1,
886 back.str_index_last(),
887 " _" + std::to_string(i++));
888 }
889 });
890 });
891}
892
893void SourceProcessor::lower_namesless_parameters_ast(Parser &parser)
894{
895 for (FuncDecl fn : parser.root().descendants_of_type<FuncDecl>()) {
896 int i = 0;
897 for (FuncArg arg : fn.arguments().children_of_type<FuncArg>()) {
898 if (!arg.identifier().is_valid()) {
899 bool is_ref = arg.is_reference();
900 ast::ArrayDecl arr = arg.array();
901 Token arg_back(is_ref ? arg.declarator().reference().back() :
902 (arr.is_valid() ? arr.front().prev() : arg.back()));
903 /* Append a name for nameless argument. */
904 parser.replace(arg_back.str_index_last_no_whitespace() + 1,
905 arg_back.str_index_last(),
906 " _" + std::to_string(i++));
907 }
908 }
909 }
910}
911
912void SourceProcessor::disabled_code_mutation(Parser &parser, bool new_bsl_compiler)
913{
914 auto process_disabled_scope = [&](Token start_tok) {
915 /* Search for endif with the same indentation. Assume formatted input. */
916 string end_str = string(start_tok.str_with_whitespace()) + "endif";
917 size_t scope_end = parser.str().find(end_str, start_tok.str_index_start());
918 if (scope_end == string::npos) {
919 report_error(start_tok, "Couldn't find end of disabled scope.");
920 return;
921 }
922 /* Search for else/elif with the same indentation. Assume formatted input. */
923 string else_str = string(start_tok.str_with_whitespace()) + "el";
924 size_t scope_else = parser.str().find(else_str, start_tok.str_index_start());
925 if (scope_else != string::npos && scope_else < scope_end) {
926 /* Only erase the content and keep the preprocessor directives. */
927 parser.erase(start_tok.line_end() + 1, scope_else - 1);
928 }
929 else {
930 /* Erase the content and the preprocessor directives. */
931 parser.erase(start_tok.str_index_start(), scope_end + end_str.size());
932 }
933 };
934
935 auto process_enabled_scope = [&](Token start_tok) {
936 /* Search for endif with the same indentation. Assume formatted input. */
937 string end_str = string(start_tok.str_with_whitespace()) + "endif";
938 size_t scope_end = parser.str().find(end_str, start_tok.str_index_start());
939 if (scope_end == string::npos) {
940 report_error(start_tok, "Couldn't find end of enabled scope.");
941 return;
942 }
943
944 /* Find where the #if 1 line ends to start keeping content */
945 size_t code_start = start_tok.line_end() + 1;
946
947 /* Search for else/elif with the same indentation. */
948 string else_str = string(start_tok.str_with_whitespace()) + "el";
949 size_t scope_else = parser.str().find(else_str, start_tok.str_index_start());
950
951 /* Erase the initial #if 1 directive line */
952 parser.erase(start_tok.str_index_start(), code_start - 1);
953
954 if (scope_else != string::npos && scope_else < scope_end) {
955 /* Erase the disabled #else/#elif block up to the end of #endif */
956 parser.erase(scope_else, scope_end + end_str.size());
957 }
958 else {
959 /* If there's no #else branch, erase just the #endif directive line */
960 parser.erase(scope_end, scope_end + end_str.size());
961 }
962 };
963
964 parser().foreach_match<true>("#AA", [&](const vector<Token> &tokens) {
965 if (tokens[1].str() != "ifndef") {
966 return;
967 }
968 if (tokens[2].str() == "GPU_SHADER" ||
969 (new_bsl_compiler ? tokens[2].str() == "BSL_530" : false))
970 {
971 process_disabled_scope(tokens[0]);
972 }
973 });
974 parser().foreach_match<true>("#i!A(A)", [&](const vector<Token> &tokens) {
975 if (tokens[1].str() != "if" || tokens[3].str() != "defined") {
976 return;
977 }
978 if (tokens[5].str() == "GPU_SHADER" ||
979 (new_bsl_compiler ? tokens[5].str() == "BSL_530" : false))
980 {
981 process_disabled_scope(tokens[0]);
982 }
983 });
984 parser().foreach_match<true>("#i1", [&](const vector<Token> &tokens) {
985 if (tokens[1].str() == "if") {
986 if (tokens[2].str() == "0") {
987 process_disabled_scope(tokens[0]);
988 }
989 else if (tokens[2].str() == "1") {
990 process_enabled_scope(tokens[0]);
991 }
992 }
993 });
994
995 parser.apply_mutations();
996}
997
998string SourceProcessor::disabled_code_mutation(const string &str)
999{
1000 Parser parser(str, error_handler);
1001 disabled_code_mutation(parser);
1002 return parser.result_get();
1003}
1004
1005void SourceProcessor::lower_preprocessor(Parser &parser)
1006{
1007 /* Remove unsupported directives. */
1008
1009 parser().foreach_match<true>("#A", [&](const vector<Token> &tokens) {
1010 if (tokens[1].str() == "pragma") {
1011 Token next = tokens[1].next();
1012 if (next.str() == "once") {
1013 parser.erase(tokens.front(), next);
1014 }
1015 else if (next.str() == "runtime_generated") {
1016 parser.erase(tokens.front(), next);
1017 }
1018 }
1019 else if (tokens[1].str() == "include" && tokens[1].next() == String) {
1020 parser.erase(tokens.front(), tokens[1].next());
1021 }
1022 });
1023 parser.apply_mutations();
1024}
1025
1026void SourceProcessor::lower_preprocessor_ast(Parser &parser)
1027{
1028 /* Remove unsupported directives. */
1029 for (Preprocessor directive : parser.root().descendants_of_type<Preprocessor>()) {
1030 Token type = directive.front().next();
1031 if (type.str() == "pragma") {
1032 Token pragma = type.next();
1033 if (pragma.str() == "once") {
1034 parser.erase(directive);
1035 }
1036 else if (pragma.str() == "runtime_generated") {
1037 parser.erase(directive);
1038 }
1039 }
1040 else if (type.str() == "include" && type.next() == String) {
1041 parser.erase(directive);
1042 }
1043 }
1044 parser.apply_mutations();
1045}
1046
1047/* Support for BLI swizzle syntax. */
1048void SourceProcessor::lower_swizzle_methods(Parser &parser)
1049{
1050 /* Change C++ swizzle functions into plain swizzle. */
1052 parser().foreach_match(".A()", [&](const vector<Token> &tokens) {
1053 string_view method_name(tokens[1].str());
1054 if (method_name.length() > 1 && method_name.length() <= 4 &&
1055 (method_name.find_first_not_of("xyzw") == string::npos ||
1056 method_name.find_first_not_of("rgba") == string::npos))
1057 {
1058 /* `.xyz()` -> `.xyz` */
1059 /* Keep character count the same. Replace parenthesis by spaces. */
1060 parser.erase(tokens[2], tokens[3]);
1061 }
1062 });
1063}
1064
1065void SourceProcessor::lower_swizzle_methods_ast(Parser &parser)
1066{
1067 /* Change C++ swizzle functions into plain swizzle. */
1069 for (FuncCall call : parser.root().descendants_of_type<FuncCall>()) {
1070 ast::FuncParamList params = call.parameters();
1071 if (call.front().prev() != Dot || !params.is_empty()) {
1072 continue;
1073 }
1074
1075 string_view method_name = call.identifier().str();
1076 if (method_name.length() > 1 && method_name.length() <= 4 &&
1077 (method_name.find_first_not_of("xyzw") == string::npos ||
1078 method_name.find_first_not_of("rgba") == string::npos))
1079 {
1080 /* `.xyz()` -> `.xyz ` */
1081 parser.erase(params);
1082 }
1083 }
1084}
1085
1086/* Support for C++ binary literal syntax for integers. */
1087void SourceProcessor::lower_binary_literals(Parser &parser)
1088{
1089 parser().foreach_token(Number, [&](const Token tok) {
1090 string_view str = tok.str();
1091 if (str.starts_with("0b") || str.starts_with("0B")) {
1092 int64_t value = std::stoll(string(str.substr(2)), nullptr, 2);
1093 parser.replace(tok.str_index_start(),
1094 tok.str_index_last_no_whitespace(),
1095 std::to_string(value) + (str.ends_with("u") ? "u" : ""));
1096 }
1097 });
1098}
1099
1100void SourceProcessor::threadgroup_variables_parse_and_remove(Parser &parser)
1101{
1102 auto process_shared_var = [&](Token shared_tok, Token type, Token name, Token decl_end) {
1103 if (shared_tok.str() == "shared") {
1104 metadata_.shared_variables.push_back(
1105 {string(type.str()), parser.substr_range_inclusive(name, decl_end.prev())});
1106
1107 parser.erase(shared_tok, decl_end);
1108 }
1109 };
1110 parser().foreach_match("AAA", [&](const vector<Token> &tokens) {
1111 Token end = tokens[2].find_next(lexit::SemiColon);
1112 process_shared_var(tokens[0], tokens[1], tokens[2], end);
1113 });
1114 parser.apply_mutations();
1115}
1116
1117string SourceProcessor::threadgroup_variables_parse_and_remove(const string &str)
1118{
1119 IntermediateForm<FullLexer, DummyParser> parser(str, error_handler);
1120 auto process_shared_var = [&](Token shared_tok, Token type, Token name, Token decl_end) {
1121 if (shared_tok.str() == "shared") {
1122 metadata_.shared_variables.push_back(
1123 {string(type.str()), parser.substr_range_inclusive(name, decl_end.prev())});
1124
1125 parser.erase(shared_tok, decl_end);
1126 }
1127 };
1128 parser().foreach_match("AAA", [&](const vector<Token> &tokens) {
1129 process_shared_var(tokens[0], tokens[1], tokens[2], tokens[2].find_next(lexit::SemiColon));
1130 });
1131 parser.apply_mutations();
1132 return parser.result_get();
1133}
1134
1135void SourceProcessor::parse_library_functions(Parser &parser)
1136{
1137 using namespace metadata;
1138
1139 parser().foreach_function(
1140 [&](bool is_static, Token fn_type, Token fn_name, Scope fn_args, bool, Scope) {
1141 Token first_tok = is_static ? fn_type.prev() : fn_type;
1142 Scope attributes = first_tok.attribute_before();
1143 if (!attributes.contains("node")) {
1144 return;
1145 }
1146 if (fn_type.str() != "void") {
1147 report_error(fn_type, "Expected void return type for node function");
1148 return;
1149 }
1150 if (fn_args.token_count() <= 3) {
1151 report_error(fn_type, "Expected at least one argument for node function");
1152 return;
1153 }
1154 FunctionFormat fn;
1155 fn.name = fn_name.str();
1156
1157 fn_args.foreach_scope(ScopeType::FunctionArg, [&](Scope arg) {
1158 /* Note: There is no array support. */
1159 Token curr = arg.front();
1160 /* Skip attribute. */
1161 if (curr == '[') {
1162 curr = curr.scope().back().next();
1163 }
1164 /* Skip const. */
1165 if (curr.str() == "const") {
1166 curr = curr.next();
1167 }
1168 /* Parse qualifier. */
1169 string qualifier = "in";
1170 if (curr.str() == "in") {
1171 qualifier = "in";
1172 curr = curr.next();
1173 }
1174 else if (curr.str() == "out") {
1175 qualifier = "out";
1176 curr = curr.next();
1177 }
1178 /* Parse the type */
1179 Token type_tok = curr;
1180 string type = string(curr.str());
1181 curr = curr.next();
1182 /* Skip optional parenthesis. */
1183 if (curr == '(') {
1184 curr = curr.next();
1185 }
1186 /* Reference. */
1187 if (curr == '&') {
1188 qualifier = "out";
1189 }
1190
1191 if (type == "ShadingData" || type == "KernelGlobals") {
1192 /* They are technically inout, but we declare them at the end of the input list. */
1193 qualifier = "in";
1194 }
1195
1196 metadata::Qualifier qualifier_enum = metadata::Qualifier(hash(qualifier));
1197 metadata::Type type_enum = metadata::Type(hash(type));
1198
1199 switch (qualifier_enum) {
1203 break;
1204 default:
1205 report_error(arg.front(), "Unknown qualifier '" + qualifier + "'");
1206 break;
1207 }
1208
1209 switch (type_enum) {
1228 break;
1229 default:
1230 report_error(type_tok, "Invalid type for node function '" + type + "'");
1231 break;
1232 }
1233
1234 fn.arguments.emplace_back(ArgumentFormat{qualifier_enum, type_enum});
1235 });
1236
1237 metadata_.functions.emplace_back(fn);
1238 });
1239
1240 if (error_handler.err.has_value()) {
1241 throw ParserException();
1242 }
1243}
1244
1245void SourceProcessor::parse_library_functions_ast(Parser &parser)
1246{
1247 using namespace metadata;
1248 for (FuncDecl func : parser.root().children_of_type<FuncDecl>()) {
1249 if (!func.attributes().contains_attr("node")) {
1250 continue;
1251 }
1252 if (func.return_type().str() != "void") {
1253 report_error(func.return_type(), "Expected void return type for node function");
1254 continue;
1255 }
1256 if (func.arguments().is_empty()) {
1257 report_error(func.identifier(), "Expected at least one argument for node function");
1258 continue;
1259 }
1260
1261 FunctionFormat fn;
1262 fn.name = func.identifier().str();
1263
1264 for (FuncArg arg : func.arguments().children_of_type<FuncArg>()) {
1265 if (arg.declarator().array().is_valid()) {
1266 report_error(arg.declarator().array(),
1267 "Array arguments are not supported in node functions.");
1268 }
1269
1270 Type type = Type(hash(string(arg.type().identifier().str())));
1271 Qualifier qualifier;
1272 if (arg.is_reference() && !arg.is_const()) {
1273 qualifier = Qualifier::out;
1274 }
1275 else {
1276 qualifier = Qualifier::in;
1277 }
1278
1279 if (type == Type::KernelGlobals || type == Type::ShadingData) {
1280 /* They are technically inout, but we declare them at the end of the input list. */
1281 qualifier = Qualifier::in;
1282 }
1283
1284 [&](Type type) {
1285 switch (type) {
1286 case Type::float1:
1287 case Type::float2:
1288 case Type::float3:
1289 case Type::float4:
1290 case Type::float3x3:
1291 case Type::float4x4:
1292 case Type::int1:
1293 case Type::int2:
1294 case Type::int3:
1295 case Type::int4:
1296 case Type::bool1:
1297 case Type::sampler1DArray:
1298 case Type::sampler2DArray:
1299 case Type::sampler2D:
1300 case Type::sampler3D:
1301 case Type::Closure:
1302 case Type::KernelGlobals:
1303 case Type::ShadingData:
1304 return;
1305 }
1306 report_error(arg.type().identifier(),
1307 "Invalid type for node function '" + string(arg.type().identifier().str()) +
1308 "'");
1309 }(type);
1310
1311 fn.arguments.emplace_back(qualifier, type);
1312 }
1313 metadata_.functions.emplace_back(fn);
1314 }
1315
1316 if (error_handler.err.has_value()) {
1317 throw ParserException();
1318 }
1319}
1320
1321void SourceProcessor::parse_draw_debug(Parser &parser, const string &filename)
1322{
1323 const bool skip_drw_debug = filename == "draw_debug_draw.bsl.hh" ||
1324 filename == "draw_debug_infos.hh" ||
1325 filename == "draw_debug_draw_display.bsl.hh" ||
1326 filename == "draw_shader_shared.hh";
1327
1328 if (skip_drw_debug) {
1329 return;
1330 }
1331
1332 for (auto fn : parser.root().descendants_of_type<FuncCall>()) {
1333 if (fn.identifier().str().starts_with("drw_debug_")) {
1334 metadata_.builtins.emplace_back(Builtin::drw_debug);
1335 break;
1336 }
1337 }
1338}
1339
1340void SourceProcessor::parse_builtins(const string &str, const string &filename, bool pure_glsl)
1341{
1342 const bool skip_drw_debug = filename == "draw_debug_draw.bsl.hh" ||
1343 filename == "draw_debug_infos.hh" ||
1344 filename == "draw_debug_draw_display.bsl.hh" ||
1345 filename == "draw_shader_shared.hh";
1346 using namespace metadata;
1347 /* TODO: This can trigger false positive caused by disabled #if blocks. */
1348 vector<string> tokens = {
1349 "gl_FragCoord",
1350 "gl_FragStencilRefARB",
1351 "gl_FrontFacing",
1352 "gl_GlobalInvocationID",
1353 "gpu_InstanceIndex",
1354 "gpu_BaseInstance",
1355 "gl_InstanceID",
1356 "gl_LocalInvocationID",
1357 "gl_LocalInvocationIndex",
1358 "gl_NumWorkGroup",
1359 "gl_PointCoord",
1360 "gl_PointSize",
1361 "gl_PrimitiveID",
1362 "gl_VertexID",
1363 "gl_WorkGroupID",
1364 "gl_WorkGroupSize",
1365 };
1366
1367 if (pure_glsl) {
1368 /* Only parsed for Python GLSL sources as false positive of this are costly. */
1369 tokens.emplace_back("gl_ClipDistance");
1370 }
1371 else {
1372 /* Assume blender GLSL or BSL. */
1373 tokens.emplace_back("drw_debug_");
1374#ifdef WITH_GPU_SHADER_ASSERT
1375 tokens.emplace_back("assert");
1376#endif
1377 }
1378
1379 for (auto &token : tokens) {
1380 if (skip_drw_debug && token == "drw_debug_") {
1381 continue;
1382 }
1383 if (str.find(token) != string::npos) {
1384 metadata_.builtins.emplace_back(Builtin(hash(token)));
1385 }
1386 }
1387}
1388
1389void SourceProcessor::parse_builtins(Parser &parser, const std::string &filename)
1390{
1391 parser.apply_mutations();
1392 parse_builtins(parser.str(), filename);
1393}
1394
1395/* Add padding member to empty structs.
1396 * Empty structs are useful for templating. */
1397void SourceProcessor::lower_empty_struct(Parser &parser)
1398{
1399 parser().foreach_struct([&](Token, Scope, Token, Scope body) {
1400 int decl_count = 0;
1401 body.foreach_declaration(
1402 [&](Scope, Token, Token, Scope, Token, Scope, Token) { decl_count += 1; });
1403
1404 if (decl_count == 0) {
1405 parser.insert_before(body.back(), "int _pad;");
1406 }
1407 });
1408
1409 parser.apply_mutations();
1410}
1411
1412/* Parse, convert to create infos, and erase declaration. */
1413void SourceProcessor::lower_pipeline_definition(Parser &parser, const string &filename)
1414{
1415 using namespace metadata;
1416
1417 auto process_compilation_constants = [&](Token tok) {
1418 string create_info_decl;
1419
1420 while (tok == ',') {
1421 Token struct_name = tok.next();
1422 Scope scope = struct_name.next().scope();
1423 if (scope.token_count() == 2) {
1424 report_error(struct_name,
1425 "Empty brace constructor is an error in Pipeline declaration. "
1426 "Either remove it or add compilation constant values to it.");
1427 }
1428 auto process_constant = [&](const vector<Token> &toks) {
1429 create_info_decl += "COMPILATION_CONSTANT(";
1430 create_info_decl += (toks[3] == Number) ?
1431 ((toks[3].str().back() == 'u') ? "uint" : "int") :
1432 "bool";
1433 create_info_decl += ", " + string(toks[1].str());
1434 create_info_decl += ", " + string(toks[3].str());
1435 create_info_decl += ")\n";
1436 };
1437 scope.foreach_match(".A=A", process_constant);
1438 scope.foreach_match(".A=1", process_constant);
1439 tok = scope.back().next();
1440 }
1441
1442 return create_info_decl;
1443 };
1444
1445 auto validate_fn_name = [&](Token fn_name) {
1446 if (fn_name == '&') {
1447 report_error(fn_name, "Double function reference, remove '&'");
1448 }
1449 else if (fn_name != Word) {
1450 report_error(fn_name, "Expected function name");
1451 }
1452 return fn_name;
1453 };
1454
1455 auto process_graphic_pipeline = [&](Token pipeline_name, Scope params) {
1456 Token vertex_fn = validate_fn_name(params[1]);
1457 Token fragment_fn = validate_fn_name(params[3]);
1458 /* For now, just emit good old create info macros. */
1459 string create_info_decl;
1460 create_info_decl += "GPU_SHADER_CREATE_INFO(" + string(pipeline_name.str()) + ")\n";
1461 create_info_decl += "GRAPHIC_SOURCE(\"" + filename + "\")\n";
1462 create_info_decl += "VERTEX_FUNCTION(\"" + string(vertex_fn.str()) + "\")\n";
1463 create_info_decl += "FRAGMENT_FUNCTION(\"" + string(fragment_fn.str()) + "\")\n";
1464 create_info_decl += "ADDITIONAL_INFO(" + string(vertex_fn.str()) + "_infos_)\n";
1465 create_info_decl += "ADDITIONAL_INFO(" + string(fragment_fn.str()) + "_infos_)\n";
1466 create_info_decl += process_compilation_constants(params[4]);
1467 create_info_decl += "DO_STATIC_COMPILATION()\n";
1468 create_info_decl += "GPU_SHADER_CREATE_END()\n";
1469
1470 metadata_.create_infos_declarations.emplace_back(create_info_decl);
1471 };
1472
1473 auto process_compute_pipeline = [&](Token pipeline_name, Scope params) {
1474 Token compute_fn = validate_fn_name(params[1]);
1475 /* For now, just emit good old create info macros. */
1476 string create_info_decl;
1477 create_info_decl += "GPU_SHADER_CREATE_INFO(" + string(pipeline_name.str()) + ")\n";
1478 create_info_decl += "COMPUTE_SOURCE(\"" + filename + "\")\n";
1479 create_info_decl += "COMPUTE_FUNCTION(\"" + string(compute_fn.str()) + "\")\n";
1480 create_info_decl += "ADDITIONAL_INFO(" + string(compute_fn.str()) + "_infos_)\n";
1481 create_info_decl += process_compilation_constants(params[2]);
1482 create_info_decl += "DO_STATIC_COMPILATION()\n";
1483 create_info_decl += "GPU_SHADER_CREATE_END()\n";
1484
1485 metadata_.create_infos_declarations.emplace_back(create_info_decl);
1486 };
1487
1488 parser().foreach_match("AA(", [&](const vector<Token> &tokens) {
1489 Scope parameters = tokens[2].scope();
1490 if (tokens[0].str() == "PipelineGraphic") {
1491 process_graphic_pipeline(tokens[1], parameters);
1492 parser.erase(tokens.front(), parameters.back().next());
1493 }
1494 else if (tokens[0].str() == "PipelineCompute") {
1495 process_compute_pipeline(tokens[1], parameters);
1496 parser.erase(tokens.front(), parameters.back().next());
1497 }
1498 });
1499}
1500
1501void SourceProcessor::lower_stage_function(Parser &parser)
1502{
1503 parser().foreach_function([&](bool is_static, Token fn_type, Token, Scope, bool, Scope fn_body) {
1504 Token attr_tok = (is_static) ? fn_type.prev().prev() : fn_type.prev();
1505 if (attr_tok.is_invalid() || attr_tok != ']' || attr_tok.prev() != ']') {
1506 return;
1507 }
1508 Scope attributes = attr_tok.prev().scope();
1509 if (attributes.type() != ScopeType::Attributes) {
1510 return;
1511 }
1512
1513 parser.erase(attributes.scope());
1514
1515 string condition;
1516 attributes.foreach_attribute([&](Token attr_tok, Scope) {
1517 const string_view attr = attr_tok.str();
1518 if (attr == "vertex") {
1519 condition += "GPU_VERTEX_SHADER";
1520 }
1521 else if (attr == "fragment") {
1522 condition += "GPU_FRAGMENT_SHADER";
1523 }
1524 else if (attr == "compute") {
1525 condition += "GPU_COMPUTE_SHADER";
1526 }
1527 });
1528 if (condition.empty()) {
1529 return;
1530 }
1531 condition = "defined(" + condition + ")";
1532
1533 guarded_scope_mutation(parser, fn_body, condition, Token(parser));
1534 });
1535 parser.apply_mutations();
1536}
1537
1539 Scope scope,
1540 const string &condition,
1541 Token fn_type)
1542{
1543 string line_start = "#line " + to_string(scope.front().next().line_number()) + "\n";
1544
1545 string guard_start = "#if " + condition;
1546 string guard_else;
1547 if (fn_type.is_valid() && fn_type.str() != "void") {
1548 string type(fn_type.str());
1549 bool is_trivial = false;
1550 if (type == "float" || type == "float2" || type == "float3" || type == "float4" ||
1551 /**/
1552 type == "int" || type == "int2" || type == "int3" || type == "int4" ||
1553 /**/
1554 type == "uint" || type == "uint2" || type == "uint3" || type == "uint4" ||
1555 /**/
1556 type == "float2x2" || type == "float2x3" || type == "float2x4" ||
1557 /**/
1558 type == "float3x2" || type == "float3x3" || type == "float3x4" ||
1559 /**/
1560 type == "float4x2" || type == "float4x3" || type == "float4x4" || type == "bool")
1561 {
1562 is_trivial = true;
1563 }
1564 guard_else += "#else\n";
1565 guard_else += line_start;
1566 guard_else += " return " + type + (is_trivial ? "(0)" : "{}") + ";\n";
1567 }
1568 string guard_end = "#endif";
1569
1570 parser.insert_directive(scope.front(), guard_start);
1571 parser.insert_directive(scope.back().prev(), guard_else + guard_end);
1572};
1573
1574/* Lint host shared structure for padding and alignment.
1575 * Remove the [[host_shared]] attribute. */
1576void SourceProcessor::lower_host_shared_structures(Parser &parser)
1577{
1578 parser().foreach_struct([&](Token struct_keyword,
1579 Scope attributes,
1580 Token struct_name,
1581 Scope body) {
1582 if (attributes.is_invalid()) {
1583 return;
1584 }
1585 parser.erase(attributes.scope());
1586 bool is_shared = false;
1587 attributes.foreach_attribute([&](Token attr, Scope) {
1588 if (attr.str() == "host_shared") {
1589 is_shared = true;
1590 }
1591 });
1592 if (!is_shared) {
1593 return;
1594 }
1595
1596 Token comma = body.find_token(',');
1597 if (comma.is_valid() && comma.scope() == body) {
1598 report_error(
1599 comma,
1600 "comma declaration is not supported in shared struct, expand to multiple definition");
1601 return;
1602 }
1603
1604 bool is_std140_compatible = true;
1605 bool has_vec3 = false;
1606
1607 struct Type {
1608 size_t size;
1609 size_t alignment;
1610 };
1611 unordered_map<string, Type> sizeof_types = {
1612 {"float", {4, 4}},
1613 {"float2", {8, 8}},
1614 {"float4", {16, 16}},
1615 {"float2x4", {16 * 2, 16}},
1616 {"float3x4", {16 * 3, 16}},
1617 {"float4x4", {16 * 4, 16}},
1618 {"bool32_t", {4, 4}},
1619 {"int", {4, 4}},
1620 {"int2", {8, 8}},
1621 {"int4", {16, 16}},
1622 {"uint", {4, 4}},
1623 {"uint2", {8, 8}},
1624 {"uint4", {16, 16}},
1625 {"string_t", {4, 4}},
1626 {"packed_float3", {12, 16}},
1627 {"packed_int3", {12, 16}},
1628 {"packed_uint3", {12, 16}},
1629 };
1630
1631 size_t offset = 0;
1632 body.foreach_declaration([&](Scope, Token, Token type, Scope, Token, Scope array, Token) {
1633 string_view type_str(type.str());
1634
1635 if (type_str.find("char") != string::npos || type_str.find("short") != string::npos ||
1636 type_str.find("half") != string::npos)
1637 {
1638 report_error(type, "Small types are forbidden in shader interfaces.");
1639 }
1640 else if (type_str == "float3") {
1641 report_error(type, "use packed_float3 instead of float3 in shared structure");
1642 }
1643 else if (type_str == "uint3") {
1644 report_error(type, "use packed_uint3 instead of uint3 in shared structure");
1645 }
1646 else if (type_str == "int3") {
1647 report_error(type, "use packed_int3 instead of int3 in shared structure");
1648 }
1649 else if (type_str == "bool") {
1650 report_error(type, "bool is not allowed in shared structure, use bool32_t");
1651 }
1652 else if (type_str == "float4x3") {
1653 report_error(type, "float4x3 is not allowed in shared structure");
1654 }
1655 else if (type_str == "float3x3") {
1656 report_error(type, "float3x3 is not allowed in shared structure");
1657 }
1658 else if (type_str == "float2x3") {
1659 report_error(type, "float2x3 is not allowed in shared structure");
1660 }
1661 else if (type_str == "float4x2") {
1662 report_error(type, "float4x2 is not allowed in shared structure");
1663 }
1664 else if (type_str == "float3x2") {
1665 report_error(type, "float3x2 is not allowed in shared structure");
1666 }
1667 else if (type_str == "float2x2") {
1668 report_error(type, "float2x2 is not allowed in shared structure");
1669 }
1670
1671 auto sz = sizeof_types.find(string(type_str));
1672
1673 Type type_info{16, 16};
1674 if (sz != sizeof_types.end()) {
1675 type_info = sz->second;
1676 }
1677 else if (type.prev() == Enum) {
1678 /* Only 4 bytes enums are allowed. */
1679 type_info = {4, 4};
1680 parser.erase(type.prev());
1681 /* Make sure that linted structs only contain other linted structs. */
1682 /* TODO(fclem): Conflicts with default ctor. */
1683 // parser.replace(type, type.str() + linted_struct_suffix + " ");
1684 }
1685 else if (type.prev() == Struct) {
1686 /* Only 4 bytes enums are allowed. */
1687 type_info = {16, 16};
1688 /* Erase redundant struct keyword. */
1689 parser.erase(type.prev());
1690 /* Make sure that linted structs only contain other linted structs. */
1691 /* TODO(fclem): Conflicts with default ctor. */
1692 // parser.replace(type, type.str() + linted_struct_suffix + " ");
1693 }
1694 else {
1695 report_error(type, "Unknown type, add 'enum' or 'struct' keyword before the type name");
1696 return;
1697 }
1698
1699 if (type_info.size == 12) {
1700 has_vec3 = true;
1701 }
1702
1703 size_t align = type_info.alignment - (offset % type_info.alignment);
1704 if (align != type_info.alignment) {
1705 string err = "Misaligned member, missing " + to_string(align) + " padding bytes";
1706 report_error(type, err);
1707 }
1708
1709 size_t array_size = 1;
1710 if (array.is_valid()) {
1711 if (array_size > 1 && type_info.size < 16) {
1712 /* Arrays of non-vec4 are padded and should not be used inside std140. */
1713 is_std140_compatible = false;
1714 }
1715
1716 /* For macro or expression assume value is multiple of 4. */
1717 array_size = static_array_size(array, 4);
1718 }
1719
1720 offset += type_info.size * array_size;
1721 });
1722
1723 /* Only check for std140 padding for bigger structs. Otherwise consider the struct to be for
1724 * storage buffers. Eventually we could add an attribute for that usage. */
1725 if (offset < 32) {
1726 is_std140_compatible = ((offset % 16) == 0);
1727 }
1728 else if (offset % 16 != 0) {
1729 string err = "Alignment issue, missing " + to_string(16 - (offset % 16)) + " padding bytes";
1730 report_error(struct_name, err);
1731 }
1732 /* Insert an alias to the type that will get referenced for shaders that enforce usage of
1733 * linted types. */
1734 string directive = "#define " + string(struct_name.str()) + linted_struct_suffix + " " +
1735 string(struct_name.str()) + "\n";
1736 if (is_std140_compatible) {
1737 directive += "#define " + string(struct_name.str()) + linted_struct_suffix +
1738 uniform_struct_suffix + " " + string(struct_name.str()) + "\n";
1739 }
1740 parser.insert_directive(struct_keyword.prev(), directive);
1741 });
1742 parser.apply_mutations();
1743}
1744
1745void SourceProcessor::lint_unbraced_statements(Parser &parser)
1746{
1747 auto check_statement = [&](const Tokens &toks) {
1748 Token end_tok = toks.back();
1749 if (end_tok.next() == If || end_tok.scope().type() == ScopeType::Preprocessor) {
1750 return;
1751 }
1752 if (end_tok.next() == '[' && end_tok.next().next() == '[') {
1753 end_tok = end_tok.next().scope().back();
1754 }
1755 if (end_tok.next() != '{') {
1756 report_error(end_tok, "Missing curly braces after flow control statement.");
1757 }
1758 };
1759
1760 parser().foreach_match("i(..)", check_statement);
1761 parser().foreach_match("I", check_statement);
1762 parser().foreach_match("f(..)", check_statement);
1763 parser().foreach_match("F(..)", check_statement);
1764}
1765
1766void SourceProcessor::lint_reserved_tokens(Parser &parser)
1767{
1768 unordered_set<string> reserved_symbols = {
1769 "vec2", "vec3", "vec4", "mat2x2", "mat2x3", "mat2x4", "mat3x2",
1770 "mat3x3", "mat3x4", "mat4x2", "mat4x3", "mat4x4", "mat2", "mat3",
1771 "mat4", "ivec2", "ivec3", "ivec4", "uvec2", "uvec3", "uvec4",
1772 "bvec2", "bvec3", "bvec4", "common", "partition", "active", "typedef",
1773 "packed", "resource", "goto", "noinline", "extern", "external", "interface",
1774 "long", "fixed", "unsigned", "superp", "input", "output", "hvec2",
1775 "hvec3", "hvec4", "fvec2", "fvec3", "fvec4", "sample", "sampler3DRect",
1776 "filter", "cast", "row_major", "inout",
1777 };
1778
1779 parser().foreach_token(Word, [&](Token tok) {
1780 if (reserved_symbols.contains(string(tok.str()))) {
1781 string err = string(tok.str()) + " is a reserved token";
1782 report_error(tok, err);
1783 }
1784 });
1785}
1786
1787void SourceProcessor::lower_tests(Parser &parser, const string &prefix)
1788{
1789 parser().foreach_function([&](bool, Token type, Token, Scope, bool, Scope fn_body) {
1790 if (type.str() != "void") {
1791 return;
1792 }
1793 /* Note: Assume any function containing tests are entry points. */
1794 int test_id = 0;
1795 fn_body.foreach_match("A(A,A){..}", [&](Tokens toks) {
1796 if (toks[0].str() != "TEST") {
1797 return;
1798 }
1799 Scope test_body = toks[6].scope();
1800 parser.erase(toks[0], toks[5]);
1801 test_body.foreach_match("A(..)", [&](Tokens toks) {
1802 if (toks[0].str().starts_with("EXPECT_")) {
1803 int id = test_id;
1804 parser.insert_before(toks[0], prefix + "out_test[" + to_string(id) + "] = ");
1805 parser.insert_after(toks[4],
1806 "; " + prefix + "out_test[" + to_string(id) +
1807 "].line = " + to_string(toks[0].line_number()));
1808 test_id++;
1809 }
1810 });
1811 });
1812 });
1813
1814 parser.apply_mutations();
1815}
1816
1817void SourceProcessor::lower_noop_keywords(Parser &parser)
1818{
1819 /* inline has no equivalent in GLSL and is making parsing more complicated. */
1820 parser().foreach_token(Inline, [&](Token tok) { parser.erase(tok); });
1821 /* static have no meaning for the shading language when not inside a struct.
1822 * Removing to make parsing easier. */
1823 parser().foreach_token(Static, [&](Token tok) {
1824 ScopeType scope_type = tok.scope().type();
1825 if (scope_type != ScopeType::Struct && scope_type != ScopeType::Preprocessor) {
1826 if (tok.next() != Constexpr) {
1827 parser.erase(tok);
1828 }
1829 }
1830 });
1831
1832 /* Erase `public:` and `private:` keywords. Access is checked by C++ compilation. */
1833 auto process_access = [&](Token tok) {
1834 if (tok.next() == ':') {
1835 parser.erase(tok, tok.next());
1836 }
1837 else {
1838 report_error(tok, "Expecting colon ':' after access specifier");
1839 }
1840 };
1841 parser().foreach_token(Private, process_access);
1842 parser().foreach_token(Public, process_access);
1843
1844 lower_template_dependent_names(parser);
1845}
1846
1847void SourceProcessor::lower_noop_keywords_ast(Parser &parser)
1848{
1849 /* inline has no equivalent in GLSL and is making parsing more complicated. */
1850 parser().foreach_token(Inline, [&](Token tok) { parser.erase(tok); });
1851 /* Erase `public:` and `private:` keywords. Access is checked by C++ compilation. */
1852 for (AccessSpecifier node : parser.root().descendants_of_type<AccessSpecifier>()) {
1853 parser.erase(node);
1854 }
1855 /* Given our code-style, we don't need the disambiguation. */
1856 for (TemplateExplicit node : parser.root().descendants_of_type<TemplateExplicit>()) {
1857 parser.erase(node.front());
1858 }
1859 /* Remove `struct`, `class`, `enum`, `union` from type declaration. */
1860 for (IdType type : parser.root().descendants_of_type<IdType>()) {
1861 Token tok = type.identifier().front().prev();
1862 if (tok == Struct || tok == Class || tok == Enum || tok == Union) {
1863 parser.erase(tok);
1864 }
1865 }
1866}
1867
1868void SourceProcessor::lower_trailing_comma_in_list(Parser &parser)
1869{
1870 parser().foreach_match(",}", [&](const Tokens &t) { parser.erase(t[0]); });
1871}
1872
1873void SourceProcessor::lower_trailing_comma_in_list_ast(Parser &parser)
1874{
1875 for (InitializerList decl : parser.root().descendants_of_type<InitializerList>()) {
1876 if (decl.back().prev() == ',') {
1877 parser.erase(decl.back().prev());
1878 }
1879 }
1880}
1881
1882/* Allow easier parsing of struct member declaration.
1883 * Example: `int a, b;` > `int a; int b;` */
1884void SourceProcessor::lower_comma_separated_declarations(Parser &parser)
1885{
1886 auto process_decl = [&](const Tokens &t) {
1887 if (t[0].scope().type() != ScopeType::Struct) {
1888 return;
1889 }
1890 string type(t[0].str());
1891 Token comma = t[2];
1892 while (comma == ',' || comma == '[') {
1893 if (comma == '[') {
1894 comma = comma.scope().back().next();
1895 continue;
1896 }
1897 parser.replace(comma, ";" + type, true);
1898 comma = comma.next().next();
1899 }
1900 };
1901
1902 parser().foreach_match("AA,", [&](const Tokens &t) { process_decl(t); });
1903 parser().foreach_match("AA[..],", [&](const Tokens &t) { process_decl(t); });
1904}
1905
1906void SourceProcessor::lower_implicit_return_types(Parser &parser)
1907{
1908 parser().foreach_function([&](bool, Token type, Token, Scope, bool, Scope fn_body) {
1909 fn_body.foreach_match("rA?{..};", [&](Tokens toks) {
1910 Scope list = toks[3].scope();
1911 if (list.front().next() == '.') {
1912 /* `return {1, 2};` > `T tmp = T{1, 2}; return tmp;`
1913 * This syntax allow to support designated initializer. */
1914 parser.insert_before(toks[0],
1915 "{" + string(type.str()) + " _tmp = " + string(type.str()) +
1916 string(list.str()) + "; ");
1917 const Token start = toks[1].is_valid() ? toks[1] : list.front();
1918 parser.replace(start, list.back(), "_tmp;}");
1919 }
1920 else if (toks[1].is_invalid()) {
1921 /* Regular initializer list. Keep it simple. */
1922 parser.insert_after(toks[0], string(type.str()));
1923 }
1924 });
1925 });
1926}
1927
1928void SourceProcessor::lower_implicit_return_types_ast(Parser &parser)
1929{
1930 for (FuncDecl func : parser.root().descendants_of_type<FuncDecl>()) {
1931 for (ReturnStmt stmt : func.body().descendants_of_type<ReturnStmt>()) {
1932 Expr expr = stmt.expression();
1933 if (!expr.is_valid()) {
1934 return;
1935 }
1936 InitializerList list;
1937 Node node = expr.child_first();
1938 if (node == NodeType::InitializerList) {
1939 list = node;
1940 }
1941 else if (node == NodeType::Constructor) {
1942 list = node.child_first();
1943 }
1944 else {
1945 return;
1946 }
1947
1948 const string type_str(func.return_type().str());
1949 if (list.child_first() == NodeType::DesignatedInitializer) {
1950 /* `return {1, 2};` > `T tmp = T{1, 2}; return tmp;`
1951 * This syntax allow to support designated initializer. */
1952 parser.replace(
1953 stmt, "{" + type_str + " _tmp" + string(list.str()) + "; return _tmp;}", true);
1954 }
1955 else {
1956 /* Regular initializer list. Keep it simple. */
1957 parser.insert_before(list.front(), type_str);
1958 }
1959 }
1960 }
1961}
1962
1963void SourceProcessor::lower_initializer_implicit_types(Parser &parser)
1964{
1965 auto process_scope = [&](Scope s) {
1966 /* Auto insert equal. */
1967 s.foreach_match("AA{..}",
1968 [&](Tokens t) { parser.insert_before(t[2], " = " + string(t[0].str())); });
1969 /* Auto insert type. */
1970 s.foreach_match("AA={..}", [&](Tokens t) { parser.insert_before(t[3], string(t[0].str())); });
1971 };
1972
1973 parser().foreach_scope(ScopeType::FunctionArg, process_scope);
1974 parser().foreach_scope(ScopeType::Function, process_scope);
1975 parser.apply_mutations();
1976}
1977
1978void SourceProcessor::lower_initializer_implicit_types_ast(Parser &parser)
1979{
1980 for (VarDecl decl : parser.root().descendants_of_type<VarDecl>()) {
1981 for (Declarator var : decl.children_of_type<Declarator>()) {
1982 InitializerList init_list = var.initializer_list();
1983 if (init_list.is_valid()) {
1984 /* Insert assignment. */
1985 parser.insert_before(init_list.front(), " = " + string(decl.type().str()));
1986 return;
1987 }
1988
1989 AssignStmt assign = var.initial_value();
1990 if (assign.is_valid()) {
1991 InitializerList init_list = assign.initializer_list();
1992 if (init_list.is_valid()) {
1993 /* Insert type. */
1994 parser.insert_before(init_list.front(), string(decl.type().str()));
1995 return;
1996 }
1997 }
1998 }
1999 }
2000
2001 parser.apply_mutations();
2002}
2003
2004void SourceProcessor::lower_designated_initializers(Parser &parser)
2005{
2006 /* Transform to compatibility macro. */
2007 parser().foreach_match("A{.A=", [&](Tokens t) {
2008 if (t[0].prev() != '=' || t[0].prev().prev() != Word) {
2009 report_error(t[0], "Designated initializers are only supported in assignments");
2010 return;
2011 }
2012 /* Lint for nested aggregates. */
2013 Token nested_aggregate_end = t[0].scope().find_token(BracketClose);
2014 if (nested_aggregate_end != t[3]) {
2015 Token nested_aggregate_start = nested_aggregate_end.scope().front();
2016 if (nested_aggregate_start.prev() != Word) {
2017 report_error(nested_aggregate_start, "Nested anonymous aggregate is not supported");
2018 return;
2019 }
2020 }
2021 Token assign_tok = t[0].prev();
2022 Token var = t[0].prev().prev();
2023 Scope aggregate = t[2].scope();
2024
2025 parser.insert_before(assign_tok, ";");
2026 parser.erase(assign_tok, t[1]);
2027 aggregate.foreach_match(".A=", [&](Tokens t) {
2028 if (t[0].scope() != aggregate) {
2029 report_error(t[0], "Nested initializer lists are not supported");
2030 return;
2031 }
2032 parser.insert_before(t[0], string(var.str()));
2033 Token value_end = t[2].scope().back();
2034 parser.insert_after(value_end, ";");
2035 if (value_end.next() == ',') {
2036 parser.erase(value_end.next());
2037 }
2038 });
2039 parser.erase(aggregate.back(), aggregate.back().next());
2040
2041 /* TODO: Lint for vector/matrix type (unsafe aggregate). */
2042 });
2043
2044 parser.apply_mutations();
2045}
2046
2047/* Support for **full** aggregate initialization.
2048 * They are converted to default constructor for GLSL. */
2049void SourceProcessor::lower_aggregate_initializers(Parser &parser)
2050{
2051 unordered_set<string> builtin_types = {
2052 "float2", "float3", "float4", "float2x2", "float2x3", "float2x4",
2053 "float3x2", "float3x3", "float3x4", "float4x2", "float4x3", "float4x4",
2054 "float2x2", "float3x3", "float4x4", "int2", "int3", "int4",
2055 "uint2", "uint3", "uint4", "bool2", "bool3", "bool4",
2056 };
2057
2058 do {
2059 /* Transform to compatibility macro. */
2060 parser().foreach_match("A{..}", [&](Tokens t) {
2061 if (t[0].prev() == Struct) {
2062 return;
2063 }
2064 if (builtin_types.contains(string(t[0].str()))) {
2065 report_error(t[0],
2066 "Aggregate is error prone for built-in vector and matrix types, use "
2067 "constructors instead");
2068 }
2069 if (t[1].scope().token_count() == 2) {
2070 /* Call generated default ctor. */
2071 parser.insert_after(t.front(), "_ctor_");
2072 parser.replace(t[1], t[4], "()");
2073 return;
2074 }
2075 /* Lint for nested aggregates. */
2076 Token nested_aggregate_end = t[1].scope().find_token(BracketClose);
2077 if (nested_aggregate_end != t[4]) {
2078 Token nested_aggregate_start = nested_aggregate_end.scope().front();
2079 if (nested_aggregate_start.prev() != Word) {
2080 report_error(nested_aggregate_start, "Nested anonymous aggregate is not supported");
2081 }
2082 }
2083 parser.insert_before(t[0], "_ctor(");
2084 parser.insert_before(t[1], ")");
2085 parser.erase(t[1]);
2086 if (t[4].prev() == ',') {
2087 parser.erase(t[4].prev());
2088 }
2089 parser.insert_before(t[4], " _rotc()");
2090 parser.erase(t[4]);
2091
2092 /* TODO: Lint for vector/matrix type (unsafe aggregate). */
2093 });
2094 } while (parser.apply_mutations());
2095}
2096
2097/* Support for **full** aggregate initialization.
2098 * They are converted to default constructor for GLSL. */
2099void SourceProcessor::lower_aggregate_initializers_ast(Parser &parser)
2100{
2101 unordered_set<string> builtin_types = {
2102 "float2", "float3", "float4", "float2x2", "float2x3", "float2x4",
2103 "float3x2", "float3x3", "float3x4", "float4x2", "float4x3", "float4x4",
2104 "float2x2", "float3x3", "float4x4", "int2", "int3", "int4",
2105 "uint2", "uint3", "uint4", "bool2", "bool3", "bool4",
2106 };
2107
2108 /* Transform aggregate to compatibility macro. */
2109 for (InitializerList list : parser.root().descendants_of_type<InitializerList>()) {
2110 IdType type(list.prev());
2111 if (!type.is_valid()) {
2112 return;
2113 }
2114 /* Lint unsafe use with vector types. */
2115 if (builtin_types.contains(string(type.str()))) {
2116 report_error(type.front(),
2117 "Aggregate is error prone for built-in vector and matrix types, use "
2118 "constructors instead");
2119 }
2120 /* Call generated default ctor for empty bracket initializer. */
2121 if (list.is_empty()) {
2122 parser.insert_after(type.back(), "_ctor_");
2123 parser.replace(list, "()", true);
2124 return;
2125 }
2126 /* Lint for nested aggregates. */
2127 for (InitializerList nested_list : list.descendants_of_type<InitializerList>()) {
2128 if (!IdType(nested_list.prev()).is_valid()) {
2129 report_error(nested_list.front(), "Nested anonymous aggregate is not supported");
2130 }
2131 }
2132 /* `A{1,}` -> `_agg(A,1)` */
2133 parser.insert_before(type.front(), "_ctor(");
2134 parser.insert_after(type.back(), ",");
2135 parser.erase(list.front());
2136 if (list.back().prev() == ',') {
2137 parser.erase(list.back().prev());
2138 }
2139 parser.insert_before(list.back(), " _rotc()");
2140 parser.erase(list.back());
2141 }
2142
2143 parser.apply_mutations();
2144}
2145
2146/* Auto detect array length, and lower to GLSL compatible syntax.
2147 * TODO(fclem): GLSL 4.3 already supports initializer list. So port the old GLSL syntax to
2148 * initializer list instead. */
2149void SourceProcessor::lower_array_initializations(Parser &parser)
2150{
2151 parser().foreach_match("AA[..]={..};", [&](vector<Token> toks) {
2152 const Token type_tok = toks[0];
2153 const Token name_tok = toks[1];
2154 const Scope array_scope = toks[2].scope();
2155 const Scope list_scope = toks[7].scope();
2156
2157 /* Auto array size. */
2158 int array_scope_tok_len = array_scope.token_count();
2159 if (array_scope_tok_len == 2) {
2160 int comma_count = 0;
2161 list_scope.foreach_token(Comma, [&](Token t) {
2162 if (t.scope() == list_scope) {
2163 comma_count++;
2164 }
2165 });
2166 const int list_len = (comma_count > 0) ? comma_count + 1 : 0;
2167 if (list_len == 0) {
2168 report_error(name_tok, "Array size must be greater than zero.");
2169 }
2170 parser.insert_after(array_scope[0], to_string(list_len));
2171 }
2172 else if (array_scope_tok_len == 3 && array_scope[1] == Number) {
2173 if (stol(string(array_scope[1].str())) == 0) {
2174 report_error(name_tok, "Array size must be greater than zero.");
2175 }
2176 }
2177
2178 /* Lint nested initializer list. */
2179 list_scope.foreach_token(BracketOpen, [&](Token tok) {
2180 if (tok != list_scope.front()) {
2181 report_error(name_tok, "Nested initializer list is not supported.");
2182 }
2183 });
2184
2185 /* Mutation to compatible syntax. */
2186 parser.insert_before(list_scope.front(), "ARRAY_T(" + string(type_tok.str()) + ") ARRAY_V(");
2187 parser.insert_after(list_scope.back(), ")");
2188 parser.erase(list_scope.front());
2189 parser.erase(list_scope.back());
2190 if (list_scope.back().prev() == ',') {
2191 parser.erase(list_scope.back().prev());
2192 }
2193 });
2194 parser.apply_mutations();
2195}
2196
2198{
2199 return str.substr(0, str.find_last_not_of(" \n") + 1);
2200}
2201
2206void SourceProcessor::lower_function_default_arguments(Parser &parser)
2207{
2208 parser().foreach_function([&](const bool is_static,
2209 Token fn_type,
2210 Token fn_name,
2211 Scope fn_args,
2212 const bool fn_const,
2213 Scope fn_body) {
2214 if (!fn_args.contains_token('=')) {
2215 return;
2216 }
2217
2218 const bool has_non_void_return_type = fn_type.str() != "void";
2219 const bool is_method = fn_type.scope().type() == ScopeType::Struct;
2220
2221 string args_decl;
2222 string args_names;
2223 string struct_name;
2224
2225 if (is_method) {
2226 struct_name = fn_type.scope().front().prev().str();
2227 }
2228
2229 vector<string> fn_overloads;
2230
2231 fn_args.foreach_scope(ScopeType::FunctionArg, [&](Scope arg) {
2232 Token equal = arg.find_token('=');
2233 const char *comma = (args_decl.empty() ? "" : ", ");
2234 if (equal.is_invalid()) {
2235 args_decl += comma + string(arg.str_with_whitespace());
2236 args_names += comma + string(arg.back().str());
2237 }
2238 else {
2239 string arg_name(equal.prev().str());
2240 string value = parser.substr_range_inclusive(equal.next(), arg.back());
2241 string decl = parser.substr_range_inclusive(arg.front(), equal.prev());
2242
2243 string fn_call = string(fn_name.str()) + '(' + args_names + comma + value + ");";
2244 if (is_method) {
2245 if (is_static) {
2246 fn_call = struct_name + namespace_separator + fn_call;
2247 }
2248 else {
2249 fn_call = "this->" + fn_call;
2250 }
2251 }
2252 if (has_non_void_return_type) {
2253 fn_call = "return " + fn_call;
2254 }
2255 string overload;
2256 overload += string(fn_type.str()) + " ";
2257 overload += string(fn_name.str()) + '(' + args_decl + ")" +
2258 string(fn_const ? " const" : "") + "\n";
2259 overload += "{\n";
2260 overload += "#line " + to_string(fn_type.line_number()) + "\n";
2261 overload += " " + fn_call + "\n}\n";
2262 fn_overloads.emplace_back(overload);
2263
2264 args_decl += comma + strip_whitespace(decl);
2265 args_names += comma + arg_name;
2266 /* Erase the value assignment and keep the declaration. */
2267 parser.erase(equal.scope());
2268 }
2269 });
2270 size_t end_of_fn_char = fn_body.back().line_end() + 1;
2271 /* Have to reverse the declaration order. */
2272 for (auto it = fn_overloads.rbegin(); it != fn_overloads.rend(); ++it) {
2273 parser.insert_line_number(end_of_fn_char, fn_type.line_number());
2274 parser.insert_after(end_of_fn_char, *it);
2275 }
2276 parser.insert_line_number(end_of_fn_char, fn_body.back().line_number() + 1);
2277 });
2278
2279 parser.apply_mutations();
2280}
2281
2282/* Successive mutations can introduce a lot of unneeded line directives. */
2283void SourceProcessor::cleanup_line_directives(Parser &parser)
2284{
2285 parser().foreach_match<true>("#A1", [&](vector<Token> toks) {
2286 if (toks[1].str() != "line") {
2287 return;
2288 }
2289 if (toks[2].next() == String) {
2290 /* Do not process directives with filenames. */
2291 return;
2292 }
2293 /* Workaround the foreach_match not matching overlapping patterns. */
2294 if (toks.back().next() == '#' && toks.back().next().next() == Word &&
2295 toks.back().next().next().next() == Number)
2296 {
2297 parser.replace(toks[0].line_start(), toks[0].line_end() + 1, "");
2298 }
2299 });
2300 parser.apply_mutations();
2301
2302 parser().foreach_match<true>("#A1#A", [&](vector<Token> toks) {
2303 if (toks[1].str() != "line") {
2304 return;
2305 }
2306 /* Workaround the foreach_match not matching overlapping patterns. */
2307 if (toks.back().next() == '#' && toks.back().next().next() == Word &&
2308 toks.back().next().next().next() == Number)
2309 {
2310 parser.replace(toks[0].line_start(), toks[0].line_end() + 1, "");
2311 }
2312 });
2313 parser.apply_mutations();
2314
2315 parser().foreach_match<true>("#A1", [&](vector<Token> toks) {
2316 if (toks[1].str() != "line") {
2317 return;
2318 }
2319 if (toks[2].next() == String) {
2320 /* Do not process directives with filenames. */
2321 return;
2322 }
2323 int line = toks[0].line_number();
2324 int value = stol(string(toks[2].str()));
2325
2326 Token prev = toks[0].prev();
2327 Token next = toks[2].next();
2328 /* True if the directive splits a logical line and the parts do not overlap. */
2329 if (prev.line_number() == value) {
2330 /* Backtrack to find the first token of the previous line. */
2331 Token first_on_prev_line = prev;
2332 Token peek = first_on_prev_line.prev();
2333 while (peek.is_valid() && peek.str_with_whitespace().find_first_of('\n') == string::npos) {
2334 first_on_prev_line = peek;
2335 peek = first_on_prev_line.prev();
2336 }
2337
2338 /* Check if the previous line is a preprocessor directive. */
2339 bool is_prev_directive = (first_on_prev_line.is_valid() && first_on_prev_line == '#');
2340
2341 /* Only merge if the previous line is NOT a preprocessor directive. */
2342 if (!is_prev_directive) {
2343 int prev_end_col = prev.char_number() + prev.str().length();
2344 int next_start_col = next.char_number();
2345
2346 if (prev_end_col < next_start_col) {
2347 int spaces_needed = next_start_col - prev_end_col;
2348 parser.replace(prev.str_index_last_no_whitespace() + 1,
2349 next.str_index_start() - 1,
2350 std::string(spaces_needed, ' '));
2351 return;
2352 }
2353 }
2354 }
2355 /* True if directive is noop. */
2356 if (line == value) {
2357 parser.replace(toks[0].line_start(), toks[0].line_end() + 1, "");
2358 }
2359 /* True if directive is not better than 1 newline. */
2360 if (line == value - 1) {
2361 parser.replace(toks[0].line_start(), toks[0].line_end(), "");
2362 }
2363 /* True if directive is not better than 2 newline. */
2364 if (line == value - 2) {
2365 parser.replace(toks[0].line_start(), toks[0].line_end(), "\n");
2366 }
2367 });
2368 parser.apply_mutations();
2369}
2370
2371/* Successive mutations can introduce a lot of unneeded blank lines. */
2372void SourceProcessor::cleanup_empty_lines(Parser &parser)
2373{
2374 const string &str = parser.str();
2375
2376 {
2377 size_t sequence_start = 0;
2378 size_t sequence_end = -1;
2379 while ((sequence_start = str.find("\n\n\n", sequence_end + 1)) != string::npos) {
2380 sequence_end = str.find_first_not_of("\n", sequence_start);
2381 if (sequence_end == string::npos) {
2382 break;
2383 }
2384 size_t line = line_number(str, sequence_end);
2385 parser.replace(sequence_start + 2, sequence_end - 1, "#line " + to_string(line) + "\n");
2386 }
2387 parser.apply_mutations();
2388 }
2389 {
2390 size_t sequence_start = 0;
2391 size_t sequence_end = -1;
2392 while ((sequence_end = str.find("\n\n#line ", sequence_end + 1)) != string::npos) {
2393 sequence_start = str.find_last_not_of("\n", sequence_end) + 1;
2394 if (sequence_start == string::npos) {
2395 continue;
2396 }
2397 parser.replace(sequence_start, sequence_end, "");
2398 }
2399 parser.apply_mutations();
2400 }
2401}
2402
2403/* Used to make GLSL matrix constructor compatible with MSL in pyGPU shaders.
2404 * This syntax is not supported in blender's own shaders. */
2405string SourceProcessor::matrix_constructor_mutation(const string &str)
2406{
2407 if (str.find("mat") == string::npos) {
2408 return str;
2409 }
2410
2411 IntermediateForm<FullLexer, DummyParser> parser(str, error_handler);
2412 parser().foreach_token(ParOpen, [&](const Token t) {
2413 if (t.prev() == Word) {
2414 Token fn_name = t.prev();
2415 string_view fn_name_str = fn_name.str();
2416 if (fn_name_str.size() == 4) {
2417 /* Example: `mat2(x)` > `__mat2x2(x)` */
2418 if (fn_name_str == "mat2") {
2419 parser.replace(fn_name, "__mat2x2", true);
2420 }
2421 else if (fn_name_str == "mat3") {
2422 parser.replace(fn_name, "__mat3x3", true);
2423 }
2424 else if (fn_name_str == "mat4") {
2425 parser.replace(fn_name, "__mat4x4", true);
2426 }
2427 }
2428 else if (fn_name_str.size() == 6) {
2429 if (fn_name_str == "mat2x2" || fn_name_str == "mat3x3" || fn_name_str == "mat4x4") {
2430 /* Only process square matrices since this is the only types we overload the
2431 * constructors. */
2432 /* Example: `mat2x2(x)` > `__mat2x2(x)` */
2433 parser.insert_before(fn_name, "__");
2434 }
2435 }
2436 }
2437 });
2438 return parser.result_get();
2439}
2440
2441/* To be run before `argument_decorator_macro_injection()`. */
2442void SourceProcessor::lower_reference_arguments(Parser &parser)
2443{
2444 auto add_mutation = [&](Token type, Token arg_name, Token last_tok) {
2445 if (type.prev() == TokenType::Const) {
2446 parser.replace(type.prev(), last_tok, string(type.str()) + " " + string(arg_name.str()));
2447 }
2448 else {
2449 parser.replace(type, last_tok, "inout " + string(type.str()) + " " + string(arg_name.str()));
2450 }
2451 };
2452
2453 parser().foreach_scope(ScopeType::FunctionArgs, [&](const Scope scope) {
2454 scope.foreach_match(
2455 "A(&A)", [&](const vector<Token> toks) { add_mutation(toks[0], toks[3], toks[4]); });
2456 scope.foreach_match(
2457 "A&A", [&](const vector<Token> toks) { add_mutation(toks[0], toks[2], toks[2]); });
2458 scope.foreach_match(
2459 "A&T", [&](const vector<Token> toks) { add_mutation(toks[0], toks[2], toks[2]); });
2460 });
2461 parser.apply_mutations();
2462}
2463
2464/* To be run after `lower_reference_arguments()`. */
2465void SourceProcessor::lower_reference_variables(Parser &parser)
2466{
2467 parser().foreach_function([&](bool, Token, Token, Scope fn_args, bool, Scope fn_scope) {
2468 fn_scope.foreach_match("c?A&A=", [&](const vector<Token> &tokens) {
2469 const Token name = tokens[4];
2470 const Scope assignment = tokens[5].scope();
2471
2472 Token decl_start = tokens[0].is_valid() ? tokens[0] : tokens[2];
2473 /* Take attribute into account. */
2474 decl_start = (decl_start.prev() == ']') ? decl_start.prev().scope().front() : decl_start;
2475 /* Take ending ; into account. */
2476 const Token decl_end = assignment.back().next();
2477
2478 /* Assert definition doesn't contain any side effect. */
2479 assignment.foreach_token(Increment, [&](const Token token) {
2480 report_error(token, "Reference definitions cannot have side effects.");
2481 });
2482 assignment.foreach_token(Decrement, [&](const Token token) {
2483 report_error(token, "Reference definitions cannot have side effects.");
2484 });
2485 assignment.foreach_token(ParOpen, [&](const Token token) {
2486 string_view fn_name = token.prev().str();
2487 if ((fn_name != "specialization_constant_get") && (fn_name != "push_constant_get") &&
2488 (fn_name != "interface_get") && (fn_name != "resource_table_get") &&
2489 (fn_name != "attribute_get") && (fn_name != "buffer_get") &&
2490 (fn_name != "srt_access") && (fn_name != "sampler_get") && (fn_name != "image_get"))
2491 {
2492 report_error(token, "Reference definitions cannot contain function calls.");
2493 }
2494 });
2495 assignment.foreach_scope(ScopeType::Subscript, [&](const Scope subscript) {
2496 if (subscript.token_count() != 3) {
2497 report_error(subscript.front(),
2498 "Array subscript inside reference declaration must be a single variable or "
2499 "a constant, not an expression.");
2500 return;
2501 }
2502
2503 const Token index_var = subscript[1];
2504
2505 if (index_var == Number) {
2506 /* Literals are fine. */
2507 return;
2508 }
2509
2510 /* Search if index variable definition qualifies it as `const`. */
2511 bool is_const = false;
2512 bool is_ref = false;
2513 bool is_found = false;
2514
2515 auto process_decl = [&](const vector<Token> &tokens) {
2516 if (tokens[5].str_index_start() < index_var.str_index_start() &&
2517 tokens[5].str() == index_var.str())
2518 {
2519 is_const = tokens[0].is_valid();
2520 is_ref = tokens[3].is_valid();
2521 is_found = true;
2522 }
2523 };
2524 fn_args.foreach_match("c?A&?A", [&](const vector<Token> &toks) { process_decl(toks); });
2525 fn_scope.foreach_match("c?A&?A", [&](const vector<Token> &toks) { process_decl(toks); });
2526
2527 if (!is_found) {
2528 report_error(index_var,
2529 "Cannot locate array subscript variable declaration. "
2530 "If it is a global variable, assign it to a temporary const variable for "
2531 "indexing inside the reference.");
2532 return;
2533 }
2534 if (!is_const) {
2535 report_error(index_var, "Array subscript variable must be declared as const qualified.");
2536 return;
2537 }
2538 if (is_ref) {
2539 report_error(index_var, "Array subscript variable must not be declared as reference.");
2540 return;
2541 }
2542 });
2543
2544 string definition = parser.substr_range_inclusive(assignment[1], assignment.back());
2545
2546 bool error = false;
2547 /* Replace declaration. */
2548 parser.erase(decl_start, decl_end);
2549 /* Replace all occurrences with definition. */
2550 name.scope().foreach_token(Word, [&](const Token token) {
2551 /* Do not match member access or function calls. */
2552 if (error || token.prev() == '.' || token.next() == '(') {
2553 return;
2554 }
2555 if (token.str_index_start() > decl_end.str_index_last() && token.str() == name.str()) {
2556 if (token.prev() == '&' && token.next() == '=') {
2557 report_error(token, "Local reference shadowing is not allowed.");
2558 error = true;
2559 }
2560 else {
2561 parser.replace(token, definition);
2562 }
2563 }
2564 });
2565 });
2566 });
2567 parser.apply_mutations();
2568
2569 parser().foreach_match("c?A&A=", [&](const vector<Token> &tokens) {
2570 report_error(tokens[4], "Reference is defined inside a global or unterminated scope.");
2571 });
2572}
2573
2574void SourceProcessor::lower_argument_qualifiers(Parser &parser)
2575{
2576 parser().foreach_match("AAA", [&](const Tokens &toks) {
2577 if (toks[0].scope().type() == ScopeType::Preprocessor) {
2578 /* Don't mutate the actual implementation. */
2579 return;
2580 }
2581 if (toks[0].str() == "inout" || toks[0].str() == "out") {
2582 parser.replace(toks[0], "_ref(");
2583 parser.insert_after(toks[1], ",");
2584 parser.insert_after(toks[2], ")");
2585 }
2586 });
2587 parser.apply_mutations();
2588}
2589
2590void SourceProcessor::lower_gather_component(Parser &parser)
2591{
2592 parser().foreach_match("A(..)", [&](const Tokens &toks) {
2593 if (toks[0].scope().type() == ScopeType::Preprocessor) {
2594 /* Don't mutate the actual implementation. */
2595 return;
2596 }
2597 Token component = toks.back().prev();
2598 /* Assume that if there is a number at the end of argument list, it is the component id. */
2599 if (toks[0].str() == "textureGather" && component == Number && component.prev() == Comma) {
2600 parser.insert_after(toks[0], string(component.str()));
2601 parser.erase(component.prev(), component);
2602 }
2603 });
2604 parser.apply_mutations();
2605}
2606
2607string SourceProcessor::argument_decorator_macro_injection(const string &str)
2608{
2609 IntermediateForm<FullLexer, DummyParser> parser(str, error_handler);
2610 /* Example: `out float foo` > `out float _out_sta foo _out_end` */
2611 parser().foreach_match("AAA", [&](const Tokens &t) {
2612 string_view qualifier = t[0].str();
2613 if (qualifier == "out" || qualifier == "inout" || qualifier == "in" || qualifier == "shared") {
2614 parser.insert_after(t[1], " _" + string(qualifier) + "_sta ");
2615 parser.insert_after(t[2], " _" + string(qualifier) + "_end ");
2616 }
2617 });
2618 return parser.result_get();
2619}
2620
2621string SourceProcessor::array_constructor_macro_injection(const string &str)
2622{
2623 IntermediateForm<FullLexer, DummyParser> parser(str, error_handler);
2624 parser().foreach_match("=A[", [&](const Tokens toks) {
2625 Token array_len_start = toks.back();
2626 Token array_len_end = array_len_start.find_next(SquareClose);
2627 if (array_len_end.is_valid()) {
2628 Token type = toks[1];
2629 Token array_start = array_len_end.next();
2630 if (array_start == '(') {
2631 parser.insert_before(type, " ARRAY_T(");
2632 parser.replace(array_len_start, array_len_end, ") ");
2633 parser.insert_before(array_start, "ARRAY_V");
2634 }
2635 }
2636 });
2637 return parser.result_get();
2638}
2639
2640/* Assume formatted source with our code style. Cannot be applied to python shaders. */
2641void SourceProcessor::lint_global_scope_constants(Parser &parser)
2642{
2643 /* Example: `const uint global_var = 1u;`. */
2644 parser().foreach_match("cAA=", [&](const vector<Token> &tokens) {
2645 if (tokens[0].scope().type() == ScopeType::Global) {
2646 report_error(
2647 tokens[2],
2648 "Global scope constant expression found. These get allocated per-thread in MSL. "
2649 "Use Macro's or uniforms instead.");
2650 }
2651 });
2652}
2653
2654void SourceProcessor::lint_global_scope_constants_ast(Parser &parser)
2655{
2656 /* Example: `const uint global_var = 1u;`. */
2657 for (VarDecl decl : parser.root().children_of_type<VarDecl>()) {
2658 if (decl.is_const()) {
2659 report_error(
2660 decl,
2661 "Global scope constant expression found. These get allocated per-thread in MSL. "
2662 "Use Macro's or uniforms instead.");
2663 }
2664 }
2665}
2666
2667int SourceProcessor::static_array_size(const Scope &array, int fallback_value)
2668{
2669 if (array.token_count() == 3 && array[1] == Number) {
2670 try {
2671 return stol(string(array[1].str()));
2672 }
2673 catch (invalid_argument const & /*ex*/) {
2674 report_error(array.front(), "Invalid array size, expecting integer literal");
2675 }
2676 }
2677 return fallback_value;
2678}
2679
2680string SourceProcessor::line_directive_prefix(const string &filename)
2681{
2682 /* NOTE: This is not supported by GLSL. All line directives are muted at runtime and the
2683 * sources are scanned after error reporting for the locating the muted line. */
2684 return "#line 1 \"" + filename + "\"\n";
2685}
2686
2687} // namespace blender::gpu::shader
static std::string strip_whitespace(const std::string &str)
static bool has_pragma(Parser &parser, std::string_view pragma_str)
Definition processor.cc:831
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
Definition processor.hh:45
SourceProcessor(const std::string &source, const std::string &filepath, Language language, const std::vector< std::string > &file_list)
Definition processor.hh:105
metadata::Source parse_include_and_symbols()
Definition processor.cc:416
static constexpr const char * namespace_separator
Definition processor.hh:51
std::vector< parser::Token > Tokens
Definition processor.hh:48
static constexpr const char * linted_struct_suffix
Definition processor.hh:54
static std::string get_create_info_placeholder(const std::string &name)
Definition processor.cc:660
static std::string line_directive_prefix(const std::string &filename)
static constexpr const char * uniform_struct_suffix
Definition processor.hh:55
Result convert(metadata::Source external_sources_symbols={})
Definition processor.cc:387
static void guarded_scope_mutation(Parser &parser, Scope scope, const std::string &condition, Token fn_type)
#define str(s)
#define condition(cond)
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
int size(const AssetLibraryReference *library_reference)
size_t line_number(const std::string_view &str, size_t pos)
std::string filename(const std::string_view &str, size_t pos)
size_t char_number(const std::string_view &str, size_t pos)
std::string line_str(const std::string_view &str, size_t pos)
static std::string_view str_view_exclusive(Token tok)
Definition processor.cc:769
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
SymEdge< T > * prev(const SymEdge< T > *se)
__int64 int64_t
Definition BKE_text.h:90
@ ParOpen
Definition types.hh:53
@ Private
Definition types.hh:123
@ BracketOpen
Definition types.hh:55
@ Hash
Definition types.hh:49
@ Static
Definition types.hh:105
@ Enum
Definition types.hh:106
@ Struct
Definition types.hh:117
@ Union
Definition types.hh:109
@ Dot
Definition types.hh:48
@ SquareClose
Definition types.hh:58
@ Decrement
Definition types.hh:88
@ Increment
Definition types.hh:112
@ Constexpr
Definition types.hh:86
@ String
Definition types.hh:154
@ If
Definition types.hh:97
@ Number
Definition types.hh:44
@ SemiColon
Definition types.hh:62
@ Inline
Definition types.hh:103
@ Word
Definition types.hh:43
@ Public
Definition types.hh:124
@ BracketClose
Definition types.hh:56
@ Class
Definition types.hh:118
@ Comma
Definition types.hh:66
#define hash
Definition noise_c.cc:156
std::vector< std::string > create_infos
Definition metadata.hh:278
std::vector< std::string > dependencies
Definition metadata.hh:274
std::vector< std::string > create_infos_declarations
Definition metadata.hh:279
std::vector< Builtin > builtins
Definition metadata.hh:272
std::vector< std::string > create_infos_dependencies
Definition metadata.hh:280
bool apply_mutations(const bool all_mutation_ordered=false)
bool contains_token(const char token_type) const
Definition scope.hh:221
std::string_view str_with_whitespace() const
Definition scope.hh:186
void foreach_declaration(Callback callback) const
Definition scope.hh:467
Token find_token(const char token_type) const
Definition scope.hh:212
std::string_view str() const
Definition scope.hh:194
void foreach_match(const std::string &pattern, CallbackFn callback) const
Definition scope.hh:255
void foreach_attribute(CallbackFn callback) const
Definition scope.hh:367
void foreach_scope(ScopeType type, CallbackFn callback) const
Definition scope.hh:339
void parse(ast::LocalScope node, ErrorHandler &err_handler)
Definition symbol.cc:1328
Token prev(int i=1) const
Definition lexit.hh:376
std::string_view str() const
Definition lexit.hh:381
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337
const TokenType & type() const
Definition lexit.hh:354
bool is_valid() const
Definition lexit.hh:65
Token next(int i=1) const
Definition lexit.hh:372