15using namespace shader::parser;
20 struct ArgParseResult {
23 auto parse_for_args = [&](
const Scope loop_args) -> ArgParseResult {
25 loop_args.foreach_scope(ScopeType::LoopArg, [&](
const Scope arg) {
26 if (arg.front().prev() ==
'(' && arg.back().next() ==
';') {
29 else if (arg.front().prev() ==
';' && arg.back().next() ==
';') {
32 else if (arg.front().prev() ==
';' && arg.back().next() ==
')') {
36 report_error(arg.front(),
"Invalid loop declaration.");
42 auto process_loop = [&](
const Token loop_start,
46 const bool condition_is_trivial,
47 const bool iteration_is_trivial,
52 const string body_prefix =
"",
53 const string body_suffix =
"") {
58 if (token.scope().first_scope_of_type(ScopeType::LoopBody) == body) {
59 report_error(token,
"Unrolled loop cannot contain \"continue\" statement.");
64 body.foreach_token(
Break, [&](
const Token token) {
65 if (token.scope().first_scope_of_type(ScopeType::LoopBody) == body) {
67 if (switch_scope.is_invalid() || !body.contains(switch_scope)) {
68 report_error(token,
"Unrolled loop cannot contain \"break\" statement.");
77 if (!parser.replace_try(loop_start, body.back(),
"",
true)) {
82 string indent_init, indent_cond, indent_iter;
83 if (
init.is_valid()) {
84 indent_init = string(
init.front().char_number() - 1,
' ');
86 if (cond.is_valid()) {
87 indent_cond = string(cond.front().char_number() - 3,
' ');
89 if (iter.is_valid()) {
90 indent_iter = string(iter.front().char_number(),
' ');
92 string indent_body = string(body.front().char_number(),
' ');
93 string indent_end = string(body.back().char_number(),
' ');
96 auto replace_index = [&](
const string &
str,
int loop_index) {
97 if (iter.is_invalid() || !iteration_is_trivial ||
str.empty()) {
101 str_parser().foreach_token(
Word, [&](
const Token tok) {
102 if (tok.str() == iter[0].str()) {
103 str_parser.replace(tok,
to_string(loop_index),
true);
106 return str_parser.result_get();
109 parser.insert_after(body.back(),
"\n");
110 if (
init.is_valid() && !iteration_is_trivial) {
111 parser.insert_line_number(body.back(),
init.front().line_number());
112 parser.insert_after(body.back(),
113 indent_init +
"{" +
string(
init.str_with_whitespace()) +
";\n");
116 parser.insert_after(body.back(),
"{\n");
118 for (
int64_t i = 0, value = iter_init; i < iter_count; i++, value += iter_incr) {
119 if (cond.is_valid() && !condition_is_trivial) {
120 parser.insert_line_number(body.back(), cond.front().line_number());
121 parser.insert_after(body.back(),
122 indent_cond +
"if(" +
string(cond.str_with_whitespace()) +
")\n");
124 parser.insert_after(body.back(), replace_index(body_prefix, value));
125 parser.insert_line_number(body.back(), body.front().line_number());
126 parser.insert_after(body.back(),
127 indent_body + replace_index(
string(body.str_with_whitespace()), value) +
129 parser.insert_after(body.back(), body_suffix);
130 if (iter.is_valid() && !iteration_is_trivial) {
131 parser.insert_line_number(body.back(), iter.front().line_number());
132 parser.insert_after(body.back(), indent_iter +
string(iter.str_with_whitespace()) +
";\n");
135 parser.insert_line_number(body.back(), body.back().line_number());
136 parser.insert_after(body.back(), indent_end +
string(body.back().str_with_whitespace()));
143 parser().foreach_match(
"f(..)[[..]]{..}", [&](
const vector<Token> tokens) {
144 tokens[11].scope().set_type(ScopeType::LoopBody);
146 parser().foreach_match(
"f(..){..}", [&](
const vector<Token> tokens) {
147 tokens[5].scope().set_type(ScopeType::LoopBody);
149 parser().foreach_match(
"h(..){..}", [&](
const vector<Token> tokens) {
150 tokens[5].scope().set_type(ScopeType::SwitchBody);
154 parser().foreach_match(
"f(..)[[A]]{..}", [&](
const vector<Token> tokens) {
155 if (tokens[6].scope().str_with_whitespace() !=
"[unroll]") {
158 const Token for_tok = tokens[0];
159 const Scope loop_args = tokens[1].scope();
160 const Scope loop_body = tokens[10].scope();
162 auto [
init, cond, iter] = parse_for_args(loop_args);
168 if (var_type.str() !=
"int" && var_type.str() !=
"uint") {
169 report_error(var_init,
"Can only unroll integer based loop.");
172 if (var_init !=
'=') {
173 report_error(var_init,
"Expecting assignment here.");
177 report_error(
init[3],
"Expecting integer literal here.");
183 const Token cond_var = cond[t++];
184 const Token cond_type = cond[t++];
185 const Token cond_sign = (cond[t] ==
'+' || cond[t] ==
'-') ? cond[t++] :
Token(parser);
186 const Token cond_end = cond[t];
187 if (cond_var.str() != var_name.str()) {
188 report_error(cond_var,
"Non matching loop counter variable.");
192 report_error(cond_end,
"Expecting integer literal here.");
197 const Token iter_var = iter[0];
198 const Token iter_type = iter[1];
199 const Token iter_end = iter[1];
201 if (iter_var.str() != var_name.str()) {
202 report_error(iter_var,
"Non matching loop counter variable.");
208 report_error(for_tok,
"Unsupported condition in unrolled loop.");
215 report_error(for_tok,
"Unsupported condition in unrolled loop.");
220 report_error(iter_type,
"Unsupported loop expression. Expecting ++ or --.");
225 parser.substr_range_inclusive(var_init.next(), var_init.scope().back()));
227 parser.substr_range_inclusive(cond_sign.is_valid() ? cond_sign : cond_end, cond_end));
229 int iter_count =
abs(end_value - init_value);
234 bool condition_is_trivial = (cond_end == cond.back());
235 bool iteration_is_trivial = (iter_end == iter.back());
237 process_loop(tokens[0],
241 condition_is_trivial,
242 iteration_is_trivial,
250 parser().foreach_match(
"f(..)[[A(1)]]{..}", [&](
const vector<Token> tokens) {
251 if (tokens[7].
str() !=
"unroll_n") {
254 const Scope loop_args = tokens[1].scope();
255 const Scope loop_body = tokens[13].scope();
257 auto [
init, cond, iter] = parse_for_args(loop_args);
259 int iter_count = stol(
string(tokens[9].
str()));
261 process_loop(tokens[0], iter_count, 0, 0,
false,
false,
init, cond, iter, loop_body);
263 }
while (parser.apply_mutations());
266 parser().foreach_match(
"[[A", [&](
const vector<Token> tokens) {
267 if (tokens[2].
str().
find(
"unroll") != string::npos) {
268 report_error(tokens[0],
"Incompatible loop format for [[unroll]].");
273void SourceProcessor::lower_static_branch(
Parser &
parser)
278 parser().foreach_match(
"iC(..)", [&](
const vector<Token> &tokens) {
279 parser.erase(tokens[1]);
280 parser.insert_after(tokens[5],
"[[static_branch]]");
282 }
while (parser.apply_mutations());
285 parser().foreach_match(
"i(..)[[A]]{..}", [&](
const vector<Token> &tokens) {
286 Token if_tok = tokens[0];
289 Scope body = tokens[10].scope();
291 if (
attribute.str() !=
"static_branch") {
295 if (
condition.str().find(
"&&") != string::npos ||
condition.str().find(
"||") != string::npos)
297 report_error(
condition[0],
"Expecting single condition.");
301 const int i =
condition[1].str() ==
"!" ? 2 : 1;
303 const bool is_constexpr =
condition[i].str() ==
"true" ||
condition[i].str() ==
"false";
304 const bool constexpr_val = is_constexpr ? (
condition[i].str() ==
"true") ^ (i == 2) :
false;
306 if (
condition[i].
str() !=
"srt_access" && !is_constexpr) {
308 "Expecting compilation or specialization constant. Make sure SRT arguments "
309 "have the [[resource_table]] attribute.");
315 string test = is_constexpr ? string(constexpr_val ?
"1" :
"0") :
316 string(i == 2 ?
"!" :
"") +
"SRT_CONSTANT_" +
319 test += parser.substr_range_inclusive(condition[is_constexpr ? 2 : 7],
320 condition.back().prev());
322 string directive = (if_tok.prev() ==
Else ?
"#elif " :
"#if ");
324 parser.insert_directive(before_body, directive + test);
325 parser.erase(if_tok, before_body);
327 if (body.back().next() ==
Else) {
328 Token else_tok = body.back().next();
329 parser.erase(else_tok);
330 if (else_tok.next() == If) {
332 Token next_if = else_tok.next();
334 Scope attributes = next_if.next().scope().back().next().scope();
335 if (attributes.type() != ScopeType::Subscript ||
336 attributes.front().next().scope().str_exclusive() !=
"static_branch")
338 report_error(next_if,
"Expecting next if statement to also be a static branch.");
343 body = else_tok.next().scope();
345 parser.insert_directive(else_tok,
"#else");
347 parser.insert_directive(body.back(),
"#endif");
349 }
while (
parser.apply_mutations());
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
const char * to_string(ShaderStage stage)
const std::any * find(const AnyDictionary &dictionary, const std::string &key)
Scope first_scope_of_type(const ScopeType type) const
Token prev(int i=1) const