Blender V5.3
enum.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "intermediate.hh"
10#include "metadata.hh"
11#include "processor.hh"
12
13namespace blender::gpu::shader {
14using namespace std;
15using namespace shader::parser;
16using namespace shader::parser::ast;
17using namespace metadata;
18
19void SourceProcessor::lower_enums(Parser &parser)
20{
48
49 auto missing_underlying_type = [&](vector<Token> tokens) {
50 report_error(tokens[0], "enum declaration must explicitly use an underlying type");
51 };
52
53 parser().foreach_match("MA{", missing_underlying_type);
54 parser().foreach_match("MSA{", missing_underlying_type);
55
56 const string placeholder_value = "=__auto__";
57
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" : "");
61
62 auto insert = [&](Token name, const string &replacement) {
63 if (name.next() == ',' || name.next() == '}') {
64 parser.insert_after(name, replacement);
65 }
66 };
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); });
69 };
70
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()); });
75
76 parser.apply_mutations();
77
78 auto process_enum = [&](Token enum_tok,
79 Token class_tok,
80 Token enum_name,
81 Token enum_type,
82 Scope enum_scope,
83 const bool is_host_shared) {
84 const string type_str(enum_type.str());
85 const string enum_name_str(enum_name.str());
86
87 string previous_value = "error_invalid_first_value";
88 enum_scope.foreach_scope(ScopeType::Assignment, [&](Scope scope) {
89 Token name_tok = scope.front().prev();
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" : "");
94 }
95 if (class_tok.is_valid()) {
96 name = enum_name_str + "::" + name;
97 }
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);
101
102 previous_value = name;
103 });
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");
110 return;
111 }
112
113 string define = "#define ";
114 define += enum_name_str + linted_struct_suffix + " " + enum_name_str + "\n";
115 parser.insert_directive(enum_tok.prev(), define);
116 }
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());
121 };
122
123 parser().foreach_match("MSA:A{", [&](vector<Token> tokens) {
124 process_enum(tokens[0], tokens[1], tokens[2], tokens[4], tokens[5].scope(), false);
125 });
126 parser().foreach_match("MA:A{", [&](vector<Token> tokens) {
127 process_enum(tokens[0], Token(parser), tokens[1], tokens[3], tokens[4].scope(), false);
128 });
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);
131 });
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);
134 });
135
136 parser.apply_mutations();
137
138 parser().foreach_token(Enum, [&](Token tok) {
139 report_error(tok, "invalid enum declaration, likely missing underlying type");
140 });
141}
142
143void SourceProcessor::lower_enums_ast(Parser &parser)
144{
172
173 for (ClassDecl cl : parser.root().descendants_of_type<ClassDecl>()) {
174 if (cl.front() != Enum) {
175 return;
176 }
177 bool is_enum_class = (cl.front().next() == Class);
178 string enum_name(cl.identifier().str());
179 string underlying_type(cl.parent_class().str());
180
181 bool is_unsigned = underlying_type == "uchar" || underlying_type == "ushort" ||
182 underlying_type == "uint" || underlying_type == "uint32_t";
183 string suffix = is_unsigned ? "u" : "";
184 string class_prefix = is_enum_class ? enum_name + namespace_separator : "";
185
186 Id last_id;
187 for (EnumValue val : cl.body().children_of_type<EnumValue>()) {
188 Id id = val.identifier();
189
190 /* Convert to static constant. */
191 parser.insert_before(id.front(), "static constexpr " + underlying_type + " " + class_prefix);
192
193 /* Insert value if it doesn't exists. */
194 AssignStmt assign = val.value();
195 if (!assign.is_valid()) {
196 int anchor = id.back().str_index_last_no_whitespace();
197 if (!last_id.is_valid()) {
198 /* First of list. */
199 parser.insert_after(anchor, "= 0" + suffix);
200 }
201 else {
202 parser.insert_after(anchor, "= " + string(last_id.str()) + " + 1" + suffix);
203 }
204 }
205
206 /* Convert or insert trailing semicolon. */
207 if (val.back() == ',') {
208 parser.replace(val.back(), ";", true);
209 }
210 else {
211 parser.insert_after(val.back().str_index_last_no_whitespace(), ";");
212 }
213
214 last_id = id;
215 }
216
217 Token enum_tok = cl.front();
218
219 if (!enum_name.empty()) {
220 /* Check host shared and create alias. */
221 if (cl.attributes().contains_attr("host_shared")) {
222 if (underlying_type != "uint32_t" && underlying_type != "int32_t") {
223 report_error(
224 cl.parent_class().front(),
225 "Host shared enum declaration must use uint32_t or int32_t underlying type");
226 return;
227 }
228 string define = "#define " + enum_name + linted_struct_suffix + " " + enum_name + "\n";
229 parser.insert_directive(enum_tok.prev(), define);
230 }
231
232 /* Insert constructor. */
233 string ctor = enum_name + " " + enum_name + "_ctor_() { return " + enum_name + "(0); }";
234 parser.insert_directive(enum_tok.prev(), ctor);
235
236 /* Insert type alias. */
237 string alias = "#define " + enum_name + " " + underlying_type + "\n";
238 parser.insert_directive(enum_tok.prev(), alias);
239 }
240
241 /* Erase original class declaration. */
242 parser.erase(enum_tok, cl.body().front());
243 parser.erase(cl.body().back(), cl.back());
244 }
245
246 parser.apply_mutations();
247}
248
249} // namespace blender::gpu::shader
__forceinline const float4 insert(const float4 &a, const float b)
Definition binning.cpp:31
static constexpr const char * namespace_separator
Definition processor.hh:51
std::vector< parser::Token > Tokens
Definition processor.hh:48
static constexpr const char * linted_struct_suffix
Definition processor.hh:54
@ Enum
Definition types.hh:106
@ Class
Definition types.hh:118
Token prev(int i=1) const
Definition lexit.hh:376
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337