9#include <unordered_set>
17using namespace shader::parser;
18using namespace shader::parser::ast;
24 parser().foreach_token(
Class, [&](
const Token &token) {
25 if (token.prev() !=
Enum) {
26 parser.replace(token,
"struct ");
35 struct_scope.foreach_match(
"A(..)", [&](
const Tokens &t) {
36 if (t[0].scope() != struct_scope) {
39 if (t[0].
str() == struct_name.str()) {
40 report_error(t[0],
"Constructors are not supported.");
48void SourceProcessor::lint_forward_declared_structs(
Parser &
parser)
50 parser().foreach_match(
"sA;", [&](
const Tokens &t) {
51 if (t[0].scope().type() == ScopeType::Global) {
52 report_error(t[0],
"Forward declaration of types are not supported.");
58void SourceProcessor::lower_default_constructors(
Parser &
parser)
60 unordered_set<string> builtin_types = {
61 "bool32_t",
"float2",
"packed_float2",
"float3",
"packed_float3",
"float4",
62 "packed_float4",
"float2x2",
"float2x3",
"float2x4",
"float3x2",
"float3x3",
63 "float3x4",
"float4x2",
"float4x3",
"float4x4",
"float2x2",
"float3x3",
64 "float4x4",
"int2",
"int3",
"packed_int3",
"int4",
"uint2",
65 "uint3",
"packed_uint3",
"uint4",
"bool2",
"bool3",
"bool4",
70 if (attributes.is_valid()) {
77 auto default_value = [&](
const string_view type) ->
string {
78 if (type ==
"float") {
81 if (type ==
"uint" || type ==
"uchar") {
84 if (type ==
"int" || type ==
"char") {
90 if (builtin_types.contains(
string(type))) {
91 return string(type) +
"(0)";
93 return string(type) +
"{}";
96 if (array.is_valid()) {
97 int array_len = static_array_size(array, 0);
99 decl +=
"for(int i=0;i < " + string(array.str_exclusive()) +
";i++){";
100 decl +=
"r." + string(name.str()) +
"[i]=" + default_value(type.str()) +
";";
104 for (
int i = 0; i < array_len; i++) {
105 decl +=
"r." + string(name.str()) +
"[" +
to_string(i) +
"]";
106 decl +=
"=" + default_value(type.str()) +
";";
112 decl +=
"r." + string(name.str()) +
"=" + default_value(type.str()) +
";";
117 if (decl_count == 0) {
122 decl =
"static " + string(name.str()) +
" ctor_() {" + string(name.str()) +
" r;" + decl +
125 parser.insert_after(body.front().str_index_last_no_whitespace(), decl);
130void SourceProcessor::lower_implicit_member(
Parser &
parser)
133 vector<Token> members_tokens;
134 vector<Token> methods_tokens;
136 auto is_class_token = [&](
const vector<Token> &members,
const string_view token) {
137 for (
const Token &member : members) {
138 if (token == member.str()) {
145 auto check_shadowing = [&](
const Tokens &toks) {
146 if (is_class_token(members_tokens, toks[1].
str())) {
147 report_error(toks[1],
"Class member shadowing.");
153 if (name.scope() == body && type.str() !=
"ShaderCreateInfo") {
154 members_tokens.emplace_back(name);
158 body.foreach_function(
163 fn_args.foreach_match(
"AA", check_shadowing);
164 fn_args.foreach_match(
"&A", check_shadowing);
165 fn_body.foreach_match(
"AA", check_shadowing);
166 fn_body.foreach_match(
"&A", check_shadowing);
167 methods_tokens.emplace_back(fn_name);
174 fn_body.foreach_token(
Word, [&](
Token tok) {
175 if (!(tok.prev().prev() ==
'-' && tok.prev() ==
'>') && tok.prev() !=
Dot &&
177 (tok.prev() !=
Colon || tok.prev().prev() !=
Colon))
179 bool is_method = tok.next() ==
'(';
180 if (tok.next() ==
'<' && !tok.next().followed_by_whitespace()) {
182 is_method = tok.next().scope().back().next() ==
'(';
185 if (!is_class_token(is_method ? methods_tokens : members_tokens, tok.str())) {
188 parser.insert_before(tok,
"this->");
194 parser.apply_mutations();
202 parser().foreach_match(
"*T)", [&](
const Tokens &t) { parser.replace(t[0], t[1],
"this_"); });
204 parser().foreach_match(
"*T;", [&](
const Tokens &t) { parser.replace(t[0], t[1],
"this_"); });
206 parser().foreach_match(
"T->", [&](
const Tokens &t) { parser.replace(t[0], t[2],
"this_."); });
210void SourceProcessor::lower_method_definitions(
Parser &
parser)
212 lower_this_keyword(parser);
213 parser.apply_mutations();
215 parser().foreach_match(
"sA:", [&](
const Tokens &toks) {
216 if (toks[2] ==
':') {
217 report_error(toks[2],
"class inheritance is not supported");
222 parser().foreach_match(
"cAA(..)c?{..}", [&](
const Tokens &toks) {
223 if (toks[0].
prev() == TokenType::Const) {
224 report_error(toks[0],
225 "function return type is marked `const` but it makes no sense for values "
226 "and returning reference is not supported");
232 parser().foreach_struct([&](
Token struct_tok,
234 const Token struct_name,
235 const Scope struct_scope) {
237 const bool is_resource_table = (attributes.type() == ScopeType::Subscript) &&
238 (attributes.str() ==
"[[resource_table]]");
240 if (is_resource_table) {
241 parser.replace(attributes,
"");
246 const Token static_tok = is_static ? fn_type.
prev() :
Token(parser);
247 const Token const_tok = is_const ? fn_args.back().
next() :
Token(parser);
249 if (fn_name.str()[0] ==
'_') {
250 report_error(fn_name,
"function name starting with an underscore are reserved");
258 parser.erase(static_tok);
261 const bool has_no_args = fn_args.token_count() == 2;
262 const char *suffix = (has_no_args ?
"" :
", ");
263 const string prefix = (is_resource_table ?
"[[resource_table]] " :
"");
268 parser.erase(const_tok);
269 if (is_const && !is_resource_table) {
270 parser.insert_after(fn_args.front(),
271 prefix +
"const " +
string(struct_name.str()) +
" this_" +
275 parser.insert_after(fn_args.front(),
276 prefix +
string(struct_name.str()) +
" &this_" + suffix);
279 if (fn_name.str().length() > 1 &&
280 (fn_name.str().find_first_not_of(
"xyzw") == string::npos ||
281 fn_name.str().find_first_not_of(
"rgba") == string::npos))
283 report_error(fn_name,
"Method name matching swizzles accessor are forbidden.");
289 parser.apply_mutations();
293 const Token struct_end = struct_scope.back().
next();
297 if (method_len == 0) {
304 if (method_len > 1) {
306 parser.insert_after(struct_end,
"\n#ifndef GPU_METAL\n");
307 struct_scope.foreach_function(
309 const Token fn_start = is_static ? fn_type.
prev() : fn_type;
311 string proto_str = parser.substr_range_inclusive(fn_start, fn_args.back());
313 Parser proto(proto_str, error_handler);
315 parser.insert_after(struct_end, proto.result_get());
317 parser.insert_after(struct_end,
"#endif\n");
320 struct_scope.foreach_function(
322 const Token fn_start = is_static ? fn_type.
prev() : fn_type;
324 string fn_str = parser.substr_range_inclusive(fn_start, fn_body.back());
325 fn_str = string(fn_start.char_number(),
' ') + fn_str +
"\n";
327 parser.erase(fn_start, fn_body.back());
328 parser.insert_line_number(struct_end, fn_start.line_number());
329 parser.insert_after(struct_end, fn_str);
332 parser.insert_line_number(struct_end, struct_end.line_number(
true));
335 parser.apply_mutations();
340 for (FuncCall call : parser.root().descendants_of_type<FuncCall>()) {
341 if (call.identifier().str() !=
"_ctor") {
344 Token comma = call.parameters().child_first().back().
next();
346 report_error(comma,
"Compiler error: expecting comma");
348 parser.replace(comma,
")");
349 parser.replace(call.back(),
" _rotc()");
351 if (error_handler.err) {
352 throw ParserException();
357void SourceProcessor::lower_method_calls(
Parser &
parser,
bool with_prefix)
360 parser().foreach_scope(ScopeType::Function, [&](
Scope scope) {
361 scope.foreach_match(
".A(", [&](
const vector<Token> &tokens) {
363 const Token func = tokens[1];
364 const Token par_open = tokens[2];
366 Token start_of_this = end_of_this;
368 if (start_of_this ==
')') {
370 start_of_this = start_of_this.scope().front().prev();
373 if (start_of_this ==
']') {
375 start_of_this = start_of_this.scope().front().prev();
378 if (start_of_this ==
Word) {
380 if (start_of_this.prev() ==
'.') {
381 start_of_this = start_of_this.prev().prev();
388 report_error(start_of_this.line_number(),
389 start_of_this.char_number(),
390 start_of_this.line_str(),
391 "lower_method_call parsing error");
394 string this_str = parser.substr_range_inclusive(start_of_this, end_of_this);
396 const bool has_no_arg = par_open.
next() ==
')';
399 start_of_this, par_open, func_str +
"(" + this_str + (has_no_arg ?
"" :
", "));
402 }
while (parser.apply_mutations());
405void SourceProcessor::lower_structured_bindings(
Parser &
parser)
407 auto get_function_return_type = [&](string_view fn_name) {
410 if (name.str() != fn_name) {
413 return_type = type.str();
418 auto get_argument_type = [&](
Scope args, string_view arg_name) {
420 args.foreach_scope(ScopeType::FunctionArg, [&](
Scope arg) {
421 const Token name = arg.back();
426 if (name.str() == arg_name) {
428 return_type = type.
str();
434 auto get_local_symbol_type = [&](
Scope fn_body,
Token symbol) {
436 string_view symbol_str = symbol.str();
437 fn_body.foreach_token(
Word, [&](
Token tok) {
438 if (tok !=
Word || tok.str() != symbol_str || tok.index_ >= symbol.index_) {
443 if (tok_scope != symbol.scope() && !tok_scope.contains(symbol.scope())) {
449 if (is_reference ? (tok.next() !=
'=' || tok.prev(2) !=
Word) : (tok.prev() !=
Word)) {
452 Token type_tok = tok.
prev(is_reference ? 2 : 1);
453 return_type = type_tok.
str();
458 auto get_struct_members = [&](string_view struct_name) {
460 for (
const auto &symbol : metadata_.symbol_table) {
461 if (symbol.is_struct && symbol.identifier == struct_name) {
462 return symbol.members;
467 vector<pair<string, string>> members;
468 parser().foreach_match(
"sA{", [&](
const Tokens &t) {
469 if (t[1].
str() != struct_name) {
474 members.emplace_back(type.str(), name.str());
483 body.foreach_match(
"A[..]=", [&](
const Tokens &t) {
484 if (t[0].
str() !=
"auto") {
489 if (symbol_name !=
Word) {
490 report_error(symbol_name,
"Expected either a function call or a single variable");
496 if (symbol_name.next(1) ==
'(') {
497 after_symbol = symbol_name.next(1).scope().back().next();
498 if (after_symbol !=
';') {
499 report_error(after_symbol,
"Expected single function call");
502 struct_type = get_function_return_type(symbol_name.str());
503 if (struct_type.empty()) {
504 report_error(symbol_name,
505 "Couldn't infer function return type. Can be caused by overload.");
510 after_symbol = symbol_name.next(1);
511 if (after_symbol !=
';') {
512 report_error(after_symbol,
"Expected single symbol to unpack");
515 struct_type = get_local_symbol_type(body, symbol_name);
517 if (struct_type.empty()) {
518 struct_type = get_argument_type(args, symbol_name.str());
521 if (struct_type.empty()) {
522 report_error(symbol_name,
"Couldn't infer local symbol type.");
527 string struct_var =
"_u" +
to_string(index);
529 parser.replace(t[0], t[4], struct_type +
" " + struct_var);
531 vector<pair<string, string>> struct_members = get_struct_members(struct_type);
533 if (struct_members.empty()) {
534 report_error(symbol_name,
"Couldn't find type to unpack.");
539 string assignments =
";";
540 int member_index = 0;
542 if (struct_members.size() > member_index) {
543 auto [member_type, member_name] = struct_members[member_index];
544 assignments += member_type +
" " + string(tok.str()) +
"=" + struct_var +
"." +
549 report_error(tok,
"Too many parameters in structured binding");
552 if (struct_members.size() != member_index) {
553 report_error(t[4],
"Missing parameters in structured binding");
556 assignments = assignments.substr(0, assignments.size() - 1);
558 parser.insert_after(after_symbol.prev(), assignments);
563 parser.apply_mutations();
566void SourceProcessor::lower_method_forward_declaration(
Parser &
parser)
568 for (Preprocessor directive : parser.root().children_of_type<Preprocessor>()) {
569 if (!directive.str().starts_with(
"#pragma member_forward_decl")) {
572 string_view cls_name = directive.back().str();
574 if (!fwd_decl.is_valid()) {
577 "Compiler error: expecting forward declaration after 'pragma member_forward_decl'");
581 for (ClassDecl decl : parser.root().children_of_type<ClassDecl>()) {
582 if (decl.identifier().str() == cls_name) {
587 if (!cls_decl.is_valid()) {
588 report_error(fwd_decl.front(),
589 "Compiler error: can't locate class instantiation of '" +
string(cls_name) +
593 parser.insert_after(cls_decl.back(),
594 "#ifndef GPU_METAL\n" +
string(fwd_decl.str()) +
"\n#endif\n");
595 parser.replace(directive.front(), fwd_decl.back(),
"");
597 if (error_handler.err) {
598 throw ParserException();
600 parser.apply_mutations();
static std::string strip_whitespace(const std::string &str)
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
static constexpr const char * namespace_separator
std::vector< parser::Token > Tokens
static constexpr const char * method_call_prefix
float dot(VecOp< float, D >, VecOp< float, D >) RET
const char * to_string(ShaderStage stage)
SymEdge< T > * prev(const SymEdge< T > *se)
void foreach_function(Callback callback) const
void foreach_token(const TokenType token_type, Callback callback) const
Token prev(int i=1) const
std::string_view str() const
static Token invalid(const TokenBuffer *buf)
Token(const TokenBuffer *buf, int32_t index)
Token next(int i=1) const