13#include <unordered_set>
17using namespace shader::parser;
18using namespace shader::parser::ast;
21void SourceProcessor::lower_noop_attributes(
Parser &
parser)
24 if (par_open.next() !=
'[') {
29 if (attr.str() ==
"maybe_unused" || attr.str() ==
"fallthrough") {
30 if (attr.next() ==
',') {
31 parser.erase(attr, attr.next());
33 else if (attr.prev() ==
',') {
34 parser.erase(attr.prev(), attr);
36 else if (attr.next() ==
']') {
37 parser.erase(attributes.scope());
43 parser.apply_mutations();
49 if (par_open.next() !=
'[') {
55 string attr_str = string(attr.str());
56 if (attr_str ==
"base_instance" || attr_str ==
"clip_distance" ||
57 attr_str ==
"force_inline" || attr_str ==
"compilation_constant" ||
58 attr_str ==
"compute" || attr_str ==
"shared" || attr_str ==
"early_fragment_tests" ||
59 attr_str ==
"flat" || attr_str ==
"frag_coord" || attr_str ==
"frag_stencil_ref" ||
60 attr_str ==
"fragment" || attr_str ==
"front_facing" || attr_str ==
"bary_coord" ||
61 attr_str ==
"global_invocation_id" || attr_str ==
"in" || attr_str ==
"instance_id" ||
62 attr_str ==
"instance_index" || attr_str ==
"layer" ||
63 attr_str ==
"local_invocation_id" || attr_str ==
"local_invocation_index" ||
64 attr_str ==
"no_perspective" || attr_str ==
"num_work_groups" || attr_str ==
"out" ||
65 attr_str ==
"subpass_in" || attr_str ==
"point_coord" || attr_str ==
"point_size" ||
66 attr_str ==
"position" || attr_str ==
"push_constant" || attr_str ==
"resource_table" ||
67 attr_str ==
"smooth" || attr_str ==
"vertex_id" || attr_str ==
"legacy_info" ||
68 attr_str ==
"legacy_iface" || attr_str ==
"vertex" || attr_str ==
"viewport_index" ||
69 attr_str ==
"work_group_id" || attr_str ==
"maybe_unused" || attr_str ==
"fallthrough" ||
70 attr_str ==
"nodiscard" || attr_str ==
"node" || attr_str ==
"clip_control" ||
71 attr_str ==
"texture_atomic")
73 if (attr_scope.is_valid()) {
74 report_error(attr,
"This attribute requires no argument");
78 else if (attr_str ==
"acceleration_structure" || attr_str ==
"attribute" ||
79 attr_str ==
"index" || attr_str ==
"frag_color" || attr_str ==
"frag_depth" ||
80 attr_str ==
"uniform" || attr_str ==
"condition" ||
81 attr_str ==
"raster_order_group" || attr_str ==
"frequency" ||
82 attr_str ==
"sampler" || attr_str ==
"specialization_constant")
84 if (attr_scope.is_invalid()) {
85 report_error(attr,
"This attribute requires 1 argument");
89 else if (attr_str ==
"storage" || attr_str ==
"subpass_input") {
90 if (attr_scope.is_invalid()) {
91 report_error(attr,
"This attribute requires 2 arguments");
95 else if (attr_str ==
"image") {
96 if (attr_scope.is_invalid()) {
97 report_error(attr,
"This attribute requires 3 arguments");
101 else if (attr_str ==
"local_size") {
102 if (attr_scope.is_invalid()) {
103 report_error(attr,
"This attribute requires at least 1 argument");
107 else if (attr_str ==
"metal_max_total_threads_per_threadgroup") {
108 if (attr_scope.is_invalid()) {
109 report_error(attr,
"This attribute requires at least 1 argument");
113 else if (attr_str ==
"host_shared") {
114 if (attributes.front().prev().prev() !=
Struct && attributes.front().prev().prev() !=
Enum)
117 attr,
"host_shared attributes must be placed after a struct or an enum definition");
123 else if (attr_str ==
"unroll" || attr_str ==
"unroll_n") {
124 if (attributes.front().prev().prev().scope().front().prev() !=
For) {
125 report_error(attr,
"[[unroll]] attribute must be declared after a 'for' statement");
131 else if (attr_str ==
"static_branch") {
132 if (attributes.front().prev().prev().scope().front().prev() !=
If) {
134 "[[static_branch]] attribute must be declared after a 'if' condition");
141 report_error(attr,
"Unrecognized attribute");
147 if (attr_str ==
"fallthrough") {
153 if (prev_tok ==
'(' || prev_tok ==
'{' || prev_tok ==
';' || prev_tok ==
',' ||
154 prev_tok ==
'}' || prev_tok ==
')' || prev_tok ==
'\n' || prev_tok ==
' ' ||
155 prev_tok ==
'>' || prev_tok.is_invalid() ||
156 prev_tok.scope().type() == ScopeType::Preprocessor)
161 report_error(attr,
"attribute must be declared at a start of a declaration");
167 parser.erase(attributes.scope());
170 parser.apply_mutations();
173void SourceProcessor::lint_attributes_ast(
Parser &
parser)
180 return start +
size - 1;
184 unordered_map<string, ArgCount> attr_map = {
186 {
"base_instance", ArgCount(0, 0)},
187 {
"clip_control", ArgCount(0, 0)},
188 {
"clip_distance", ArgCount(0, 0)},
189 {
"compilation_constant", ArgCount(0, 0)},
190 {
"compute", ArgCount(0, 0)},
191 {
"early_fragment_tests", ArgCount(0, 0)},
192 {
"fallthrough", ArgCount(0, 0)},
193 {
"flat", ArgCount(0, 0)},
194 {
"frag_coord", ArgCount(0, 0)},
195 {
"frag_stencil_ref", ArgCount(0, 0)},
196 {
"fragment", ArgCount(0, 0)},
197 {
"front_facing", ArgCount(0, 0)},
198 {
"bary_coord", ArgCount(0, 0)},
199 {
"global_invocation_id", ArgCount(0, 0)},
200 {
"host_shared", ArgCount(0, 0)},
201 {
"in", ArgCount(0, 0)},
202 {
"instance_id", ArgCount(0, 0)},
203 {
"instance_index", ArgCount(0, 0)},
204 {
"layer", ArgCount(0, 0)},
205 {
"legacy_info", ArgCount(0, 0)},
206 {
"legacy_iface", ArgCount(0, 0)},
207 {
"local_invocation_id", ArgCount(0, 0)},
208 {
"local_invocation_index", ArgCount(0, 0)},
209 {
"maybe_unused", ArgCount(0, 0)},
210 {
"no_perspective", ArgCount(0, 0)},
211 {
"node", ArgCount(0, 0)},
212 {
"nodiscard", ArgCount(0, 0)},
213 {
"num_work_groups", ArgCount(0, 0)},
214 {
"out", ArgCount(0, 0)},
215 {
"point_coord", ArgCount(0, 0)},
216 {
"point_size", ArgCount(0, 0)},
217 {
"position", ArgCount(0, 0)},
218 {
"push_constant", ArgCount(0, 0)},
219 {
"resource_table", ArgCount(0, 0)},
220 {
"shared", ArgCount(0, 0)},
221 {
"smooth", ArgCount(0, 0)},
222 {
"static_branch", ArgCount(0, 0)},
223 {
"subpass_in", ArgCount(0, 0)},
224 {
"texture_atomic", ArgCount(0, 0)},
225 {
"unroll", ArgCount(0, 0)},
226 {
"vertex", ArgCount(0, 0)},
227 {
"vertex_id", ArgCount(0, 0)},
228 {
"viewport_index", ArgCount(0, 0)},
229 {
"work_group_id", ArgCount(0, 0)},
231 {
"attribute", ArgCount(1, 1)},
232 {
"condition", ArgCount(1, 1)},
233 {
"frag_color", ArgCount(1, 1)},
234 {
"frag_depth", ArgCount(1, 1)},
235 {
"frequency", ArgCount(1, 1)},
236 {
"unroll_n", ArgCount(1, 1)},
237 {
"index", ArgCount(1, 1)},
238 {
"metal_max_total_threads_per_threadgroup", ArgCount(1, 1)},
239 {
"raster_order_group", ArgCount(1, 1)},
240 {
"sampler", ArgCount(1, 1)},
241 {
"specialization_constant", ArgCount(1, 1)},
242 {
"uniform", ArgCount(1, 1)},
243 {
"acceleration_structure", ArgCount(1, 1)},
245 {
"storage", ArgCount(2, 1)},
246 {
"subpass_input", ArgCount(2, 1)},
248 {
"image", ArgCount(3, 1)},
250 {
"local_size", ArgCount(1, 3)},
253 for (Attr attr : parser.root().children_of_type<Attr>()) {
255 string attr_str(attr.identifier().str());
256 if (!attr_map.contains(attr_str)) {
257 report_error(attr,
"Unrecognized attribute");
261 const auto ¶m_range = attr_map[string(attr.identifier().str())];
262 if (param_range.size == 0 && param.is_valid()) {
263 report_error(param,
"This attribute requires no argument");
267 int arg_count = param.child_count();
268 if (param_range.size == 1 && arg_count != param_range.start) {
270 "This attribute requires " +
to_string(param_range.start) +
" argument(s)");
273 if (param_range.size != 1 && arg_count < param_range.start && arg_count > param_range.last()) {
275 "This attribute requires between " +
to_string(param_range.start) +
" and " +
276 to_string(param_range.last()) +
" argument(s)");
280 NodeType parent_type = attr.parent().type();
282 if (attr_str ==
"host_shared" && parent_type != NodeType::ClassDecl) {
283 report_error(attr,
"host_shared attribute must be inside a struct or an enum definition");
287 NodeType parent_parent_type = attr.parent().parent().type();
289 if ((attr_str ==
"unroll" || attr_str ==
"unroll_n") &&
290 ((parent_type != NodeType::Condition) || (parent_parent_type != NodeType::ForLoop)))
292 report_error(attr,
"unroll attribute must be declared after a for loop statement");
296 if (attr_str ==
"static_branch" &&
297 (parent_type != NodeType::Condition || parent_parent_type != NodeType::IfStmt ||
298 parent_parent_type != NodeType::ElseIfStmt))
300 report_error(attr,
"static_branch attribute must be declared after a condition");
309void SourceProcessor::lower_attribute_sequences(
Parser &
parser)
312 parser().foreach_match(
"[[..]][[..]]", [&](vector<Token> toks) {
313 parser.insert_before(toks[4],
",");
314 parser.erase(toks[4], toks[7]);
316 }
while (parser.apply_mutations());
319void SourceProcessor::lower_attribute_sequences_ast(
Parser &
parser)
321 for (AttrList list : parser.root().descendants_of_type<AttrList>()) {
322 if (list.parent().type() == NodeType::AttrList) {
324 parser.replace(front,
",");
325 parser.erase(front.next(1), front.next(3));
int size(const AssetLibraryReference *library_reference)
const char * to_string(ShaderStage stage)
void foreach_attribute(CallbackFn callback) const
Token prev(int i=1) const