15using namespace shader::parser;
16using namespace shader::parser::ast;
49 auto missing_underlying_type = [&](vector<Token> tokens) {
50 report_error(tokens[0],
"enum declaration must explicitly use an underlying type");
53 parser().foreach_match(
"MA{", missing_underlying_type);
54 parser().foreach_match(
"MSA{", missing_underlying_type);
56 const string placeholder_value =
"=__auto__";
58 auto placeholder = [&](
Scope enum_scope) {
59 const string &value = placeholder_value;
60 const string start =
" = 0" + string(enum_scope.front().prev().str()[0] ==
'u' ?
"u" :
"");
62 auto insert = [&](
Token name,
const string &replacement) {
63 if (name.next() ==
',' || name.next() ==
'}') {
64 parser.insert_after(name, replacement);
67 enum_scope.foreach_match(
"{A", [&](
const Tokens &t) {
insert(t[1], start); });
68 enum_scope.foreach_match(
",A", [&](
const Tokens &t) {
insert(t[1], value); });
71 parser().foreach_match(
"MSA:A{", [&](
const Tokens &t) { placeholder(t[5].scope()); });
72 parser().foreach_match(
"MA:A{", [&](
const Tokens &t) { placeholder(t[4].scope()); });
73 parser().foreach_match(
"MS[[A]]A:A{", [&](
const Tokens &t) { placeholder(t[10].scope()); });
74 parser().foreach_match(
"M[[A]]A:A{", [&](
const Tokens &t) { placeholder(t[9].scope()); });
76 parser.apply_mutations();
78 auto process_enum = [&](
Token enum_tok,
83 const bool is_host_shared) {
84 const string type_str(enum_type.str());
85 const string enum_name_str(enum_name.str());
87 string previous_value =
"error_invalid_first_value";
88 enum_scope.foreach_scope(ScopeType::Assignment, [&](
Scope scope) {
90 string name(name_tok.str());
91 string value(scope.str());
92 if (value == placeholder_value) {
93 value =
"= " + previous_value +
" + 1" + (enum_type.str()[0] ==
'u' ?
"u" :
"");
95 if (class_tok.is_valid()) {
96 name = enum_name_str +
"::" + name;
98 string decl =
"static constexpr " + type_str +
" " + name +
" " + value +
";\n";
99 parser.insert_line_number(enum_tok.prev(), name_tok.line_number());
100 parser.insert_after(enum_tok.prev(), decl);
102 previous_value = name;
104 parser.insert_directive(enum_tok.prev(),
105 "#define " + enum_name_str +
" " +
string(enum_type.str()) +
"\n");
106 if (is_host_shared) {
107 if (type_str !=
"uint32_t" && type_str !=
"int32_t") {
108 report_error(enum_type,
109 "Host shared enum declaration must use uint32_t or int32_t underlying type");
113 string define =
"#define ";
115 parser.insert_directive(enum_tok.prev(), define);
117 const string ctor_decl = enum_name_str +
" " + enum_name_str +
"_ctor_() { return " +
118 enum_name_str +
"(0); }";
119 parser.insert_directive(enum_tok.prev(), ctor_decl);
120 parser.erase(enum_tok, enum_scope.back().next());
123 parser().foreach_match(
"MSA:A{", [&](vector<Token> tokens) {
124 process_enum(tokens[0], tokens[1], tokens[2], tokens[4], tokens[5].scope(),
false);
126 parser().foreach_match(
"MA:A{", [&](vector<Token> tokens) {
127 process_enum(tokens[0],
Token(parser), tokens[1], tokens[3], tokens[4].scope(),
false);
129 parser().foreach_match(
"MS[[A]]A:A{", [&](vector<Token> tokens) {
130 process_enum(tokens[0], tokens[1], tokens[7], tokens[9], tokens[10].scope(),
true);
132 parser().foreach_match(
"M[[A]]A:A{", [&](vector<Token> tokens) {
133 process_enum(tokens[0],
Token(parser), tokens[6], tokens[8], tokens[9].scope(),
true);
136 parser.apply_mutations();
138 parser().foreach_token(
Enum, [&](
Token tok) {
139 report_error(tok,
"invalid enum declaration, likely missing underlying type");
173 for (ClassDecl cl : parser.root().descendants_of_type<ClassDecl>()) {
174 if (cl.front() !=
Enum) {
177 bool is_enum_class = (cl.front().next() ==
Class);
178 string enum_name(cl.identifier().str());
179 string underlying_type(cl.parent_class().str());
181 bool is_unsigned = underlying_type ==
"uchar" || underlying_type ==
"ushort" ||
182 underlying_type ==
"uint" || underlying_type ==
"uint32_t";
183 string suffix = is_unsigned ?
"u" :
"";
187 for (EnumValue val : cl.body().children_of_type<EnumValue>()) {
188 Id id = val.identifier();
191 parser.insert_before(
id.front(),
"static constexpr " + underlying_type +
" " + class_prefix);
195 if (!assign.is_valid()) {
196 int anchor =
id.back().str_index_last_no_whitespace();
197 if (!last_id.is_valid()) {
199 parser.insert_after(anchor,
"= 0" + suffix);
202 parser.insert_after(anchor,
"= " +
string(last_id.str()) +
" + 1" + suffix);
207 if (val.back() ==
',') {
208 parser.replace(val.back(),
";",
true);
211 parser.insert_after(val.back().str_index_last_no_whitespace(),
";");
217 Token enum_tok = cl.front();
219 if (!enum_name.empty()) {
221 if (cl.attributes().contains_attr(
"host_shared")) {
222 if (underlying_type !=
"uint32_t" && underlying_type !=
"int32_t") {
224 cl.parent_class().front(),
225 "Host shared enum declaration must use uint32_t or int32_t underlying type");
229 parser.insert_directive(enum_tok.prev(), define);
233 string ctor = enum_name +
" " + enum_name +
"_ctor_() { return " + enum_name +
"(0); }";
234 parser.insert_directive(enum_tok.prev(), ctor);
237 string alias =
"#define " + enum_name +
" " + underlying_type +
"\n";
238 parser.insert_directive(enum_tok.prev(), alias);
242 parser.erase(enum_tok, cl.body().front());
243 parser.erase(cl.body().back(), cl.back());
246 parser.apply_mutations();
__forceinline const float4 insert(const float4 &a, const float b)
static constexpr const char * namespace_separator
std::vector< parser::Token > Tokens
static constexpr const char * linted_struct_suffix
Token prev(int i=1) const
Token(const TokenBuffer *buf, int32_t index)