15using namespace shader::parser;
16using namespace shader::parser::ast;
19void SourceProcessor::lower_strings_sequences(
Parser &
parser)
22 parser().foreach_match(
"\"\"", [&](
const vector<Token> &tokens) {
23 string first(tokens[0].
str());
24 string second(tokens[1].
str());
25 string between = parser.substr_range_inclusive(tokens[0].str_index_last_no_whitespace() + 1,
26 tokens[1].str_index_start() - 1);
27 string trailing = parser.substr_range_inclusive(tokens[1].str_index_last_no_whitespace() + 1,
28 tokens[1].str_index_last());
29 string merged = first.substr(0, first.length() - 1) + second.substr(1) + between + trailing;
30 parser.replace_try(tokens[0], tokens[1], merged);
32 }
while (parser.apply_mutations());
39 parser().foreach_match(
"A(..)", [&](
const vector<Token> &tokens) {
40 if (tokens[0].
str() !=
"assert") {
44#ifdef WITH_GPU_SHADER_ASSERT
47 auto escape = [](
string s) {
66 replacement +=
"if (!" +
condition +
") ";
68 replacement +=
" printf(\"";
69 replacement +=
"Assertion failed: " + escape(
condition) +
", ";
70 replacement +=
"file " + filename +
", ";
72 replacement +=
"thread (%u,%u,%u).\\n";
74 replacement +=
", GPU_THREAD.x, GPU_THREAD.y, GPU_THREAD.z); ";
77 parser.replace(tokens[0], tokens[4], replacement);
79 parser.apply_mutations();
86 for (FuncCall call : parser.root().descendants_of_type<FuncCall>()) {
87 if (call.identifier().str() !=
"assert") {
92#ifdef WITH_GPU_SHADER_ASSERT
93 string condition = string(call.parameters().str());
95 auto escape = [](
string s) {
101 else if (c ==
'\\') {
104 else if (c ==
'\"') {
114 replacement +=
"if (!" +
condition +
") ";
116 replacement +=
" printf(\"";
117 replacement +=
"Assertion failed: " + escape(
condition) +
", ";
118 replacement +=
"file " + filename +
", ";
119 replacement +=
"line " +
to_string(call.parameters().front().line_number()) +
", ";
120 replacement +=
"thread (%u,%u,%u).\\n";
122 replacement +=
", GPU_THREAD.x, GPU_THREAD.y, GPU_THREAD.z); ";
125 parser.replace(call, replacement);
128 parser.apply_mutations();
134 parser().foreach_token(
String, [&](
const Token &token) {
135 if (token.scope().type() == ScopeType::Preprocessor) {
139 metadata::PrintfFormat
format = {
hash, string(token.str())};
140 metadata_.printf_formats.emplace_back(format);
141 parser.replace(token,
"string_t(" +
to_string(
hash) +
"u)",
true);
143 parser.apply_mutations();
150 bool has_printf =
false;
151 parser().foreach_match(
"A(..)", [&](
const vector<Token> &tokens) {
152 if (tokens[0].
str() !=
"printf") {
158 tokens[1].scope().foreach_scope(ScopeType::FunctionParam, [&](
const Scope &) { arg_count++; });
160 string unrolled =
"print_start(" +
to_string(arg_count) +
"u)";
161 tokens[1].scope().foreach_scope(ScopeType::FunctionParam, [&](
const Scope &
attribute) {
162 unrolled =
"print_data(" + unrolled +
", " + string(
attribute.str()) +
")";
165 parser.replace(tokens.front(), tokens.back(), unrolled);
167 parser.apply_mutations();
170 metadata_.builtins.emplace_back(Builtin::printf);
static uint32_t hash_string(const std::string &str)
size_t line_number(const std::string_view &str, size_t pos)
std::string filename(const std::string_view &str, size_t pos)
const char * to_string(ShaderStage stage)
std::string format(const std::string &fmt, Args &&...args)