15using namespace blender::gpu::shader::parser;
16using namespace blender::gpu::shader::parser::ast;
18using Token = blender::gpu::shader::parser::Token;
31 const std::string &prefix,
44 if (parent.
type() == NodeType::ForLoop) {
49 int bitfield_offset = 0;
50 SymbolVariable *bitfield_base =
nullptr;
53 ConstexprValue enum_last_val(-1);
55 if (
node.type() == NodeType::FuncDecl ||
node.type() == NodeType::ClassDecl ||
56 node.type() == NodeType::LocalScope)
58 for (Node child =
node.child_first(); child.is_valid(); child = child.next()) {
59 switch (child.type()) {
60 case NodeType::Preprocessor:
63 case NodeType::Namespace:
66 case NodeType::UsingStmt:
69 case NodeType::LocalScope:
72 case NodeType::ClassDecl:
75 case NodeType::EnumValue:
78 case NodeType::FuncForwardDecl:
81 case NodeType::FuncDecl:
84 case NodeType::VarDecl:
85 parse_var_decl(scope, child, offset, bitfield_offset, bitfield_base, prefix);
87 case NodeType::StructuredBinding:
90 case NodeType::TemplateDecl:
93 case NodeType::TemplateInst:
96 case NodeType::TemplateSpec:
99 case NodeType::SwitchStmt:
101 for (SwitchCase stmt : child.children_of_type<SwitchCase>()) {
102 for (LocalScope local : stmt.children_of_type<LocalScope>()) {
107 case NodeType::ForLoop:
108 case NodeType::DoWhileLoop:
109 case NodeType::WhileLoop:
110 case NodeType::IfStmt:
111 case NodeType::ElseIfStmt:
112 case NodeType::ElseStmt:
113 case NodeType::ReturnStmt:
115 for (LocalScope local : child.children_of_type<LocalScope>()) {
119 case NodeType::PipelineDecl:
132 scope.
scopes.emplace(sym->identifier, sym);
151 string id(arg_id.
str());
160 scope.
scopes.emplace(
id, resolved);
164 string id(arg.identifier().str());
168 if (!val.is_constexpr()) {
173 var->
value = val.value;
178 param = param.
next();
213 fn_sym->
add_argument(arg.type().is_const(), type, arg.declarator().initial_value().expr());
215 switch (attr.res_type) {
285 switch (entry_point_type) {
291 error(arg.type().identifier(),
299 error(arg.type().identifier(),
311 switch (entry_point_type) {
319 error(arg.type().identifier(),
328 error(arg.type().identifier(),
354 if (!is_entry_point && attr.condition.is_valid()) {
358 if (attr.condition.is_valid()) {
363 if (scope.
parent !=
nullptr) {
366 if (
auto it = fns.find(scope.
identifier); it != fns.end()) {
370 &scope,
fn->parent_class(), arg_list.
front(),
"this_");
386 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
399 if (type.
str() ==
"define") {
401 if (name == directive.
back()) {
406 bool is_func =
false;
410 value = value.
next(
')');
411 if (value == directive.
back()) {
415 value = value.
next();
428 if (directive.
back() == value) {
430 (value ==
Word && (value.
str() ==
"true" || value.
str() ==
"false")))
435 else if (value ==
Word) {
438 if (value.
str() ==
"int") {
441 else if (value.
str() ==
"uint") {
444 else if (value.
str() ==
"float") {
447 else if (value.
str() ==
"bool") {
452 root->
classes.emplace(
string(name.
str()), cls);
473 ns_scope = ns_scope->
scopes.try_emplace(sym->identifier, sym).first->second;
474 ns_prefix = ns_prefix + sym->identifier +
ns_sep;
487 string identifier(name.
name().
str());
509 const string &prefix)
514 sym->is_static =
true;
515 sym->is_constexpr =
true;
519 sym->value =
result.value;
531 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
537 enum_last_val = sym->value;
541 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
551 sym->identifier = prefix + sym->identifier;
559 const std::string &prefix,
560 const std::string &suffix =
"",
561 optional<SourceLocation> poi = nullopt,
566 if (!scope.
classes.emplace(cls).second) {
573 else if (scope.
poi) {
589 cls_template->
instances.emplace(suffix, cls);
592 cls->
classes.emplace(alias_name, cls);
602 cls->
size = underlying_type->size;
603 cls->
align = underlying_type->align;
611 ctor->add_argument(
true, cls);
612 ctor->allow_vector_promotion =
true;
614 ctor->identifier = prefix + ctor->identifier;
628 var->set_offset(parent_class.
is_union(), offset);
639 fn->is_defined =
false;
647 if (is_overload &&
fn->is_entry_point()) {
659 const std::string &prefix,
660 const std::string &suffix =
"",
661 optional<SourceLocation> poi = nullopt,
670 fn->loc = fdecl.front();
671 fn->is_defined =
true;
677 fn->variables.clear();
678 fn->arg_const.clear();
679 fn->arg_types.clear();
680 fn->arg_defaults.clear();
686 if (is_overload &&
fn->is_entry_point()) {
694 else if (scope.
poi) {
728 if (
decl.is_function()) {
733 fn->is_complete =
false;
735 if (
fn->template_data) {
745 if (!scope.
classes.emplace(cls).second) {
752 template<
typename DeclAst,
typename SymbolT>
757 if (sym.template_data ==
nullptr) {
788 Id start =
id.namespace_start();
790 if (
auto *parent_cls = scope.
lookup_class(
table, start, last).unwrap(
this); parent_cls) {
797 if (parent ==
nullptr) {
806 string full_id = string(
id.name().
str()) +
"<" + mangled.str_debug.substr(2) +
">";
809 *parent, temp_decl.decl(), prefix, mangled.str, poi, temp);
814 if (
fn->template_data->is_adl_possible()) {
816 0, fn_inst->
identifier.size() - mangled.str.size());
818 fn->template_data->instances.emplace(mangled.str, fn_inst);
819 fn->add_overload(fn_inst);
833 string full_id = string(
id.name().
str()) +
"<" + mangled.str_debug.substr(2) +
">";
835 int unused_offset = 0;
864 string full_id = string(
decl.identifier().str()) +
"<" + mangled.str_debug.substr(2) +
">";
871 if (
fn->template_data->is_adl_possible()) {
873 spec->
identifier.size() - mangled.str.size());
875 fn->template_data->instances.emplace(mangled.str, spec);
876 fn->add_overload(spec);
890 string full_id = string(
decl.identifier().str()) +
"<" + mangled.str_debug.substr(2) +
">";
892 int unused_offset = 0;
894 scope,
decl,
Node{}, unused_offset, prefix, mangled.
str, poi);
904 sym->is_static =
true;
906 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
912 sym->identifier = prefix + sym->identifier;
930 Id id =
decl.child_first();
942 if (!
id.is_valid()) {
948 sym->identifier =
id.str();
949 sym->parent = &scope;
951 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
957 sym->identifier = tmp->
identifier +
'.' + var->identifier;
970 int &bitfield_offset,
972 const std::string &prefix)
975 string_view type_id_str = type_id.
str();
1018 else if (cls->
srt_type != attr.srt_type) {
1022 to_str(attr.srt_type));
1032 if (attr.condition.is_valid()) {
1041 const bool is_srt_local_ref = type->
is_srt() && cls ==
nullptr;
1047 anon_prefix = prefix.prefix;
1048 named_parent = prefix.non_anonymous_parent;
1053 if (is_ref !=
decl.is_reference()) {
1060 sym->res_type = attr.res_type;
1063 NOTE(scope.
variables.find(sym->identifier)->second.first.tok,
1069 if (is_srt_local_ref && !is_ref) {
1074 if (attr.capacity.is_valid()) {
1075 if (sym->array_dimensions != 1) {
1079 sym->capacity_value = attr.capacity;
1082 if (attr.condition.is_valid()) {
1083 sym->condition = attr.condition;
1087 if (is_ref && cls ==
nullptr) {
1098 else if (type !=
result.type) {
1102 sym->reference_value = stmt.
expr();
1103 sym->array_dimensions =
result.array_dim;
1111 if (sym->is_bitfield) {
1113 sym->array_dimensions != 0)
1123 if (!
result.is_constexpr()) {
1130 else if (
len == 0) {
1139 sym->bit_length =
len;
1140 sym->bit_offset = bitfield_offset;
1141 bitfield_offset +=
len;
1145 sym->bit_length =
len;
1146 sym->bit_offset = 0;
1147 bitfield_offset =
len;
1148 bitfield_base = sym;
1154 bitfield_base =
nullptr;
1157 if (sym->bit_offset != 0 && bitfield_base) {
1159 sym->offset = bitfield_base->
offset;
1162 sym->set_offset(cls->
is_union, offset);
1172 if (bitfield_base) {
1175 sym->identifier = bitfield_base->
original;
1180 string suffix = cls->
is_union ?
"()" :
"";
1181 string prefix = cls->
is_union ?
"_" :
"";
1183 sym->identifier = anon_prefix + prefix + sym->identifier + suffix;
1185 if (sym->is_static) {
1188 decl.identifier().str(),
1190 (cls->
is_union ?
"union" :
"struct"));
1194 if (bitfield_base) {
1196 sym->identifier +=
".bitfieldExtract(" +
to_string(sym->bit_offset) +
", " +
1201 if (sym->is_static) {
1205 sym->identifier = prefix + sym->identifier;
1209 if (sym->is_constexpr) {
1210 if (sym->array_dimensions > 0) {
1216 if (
result.is_constexpr()) {
1217 sym->value =
result.value;
1220 if (!sym->is_static) {
1221 if (sym->parent_class()) {
1224 else if (sym->parent->parent_function()) {
1241 if (
list.is_empty()) {
1244 if (
list.child_count() != 1) {
1286 if (
AssignStmt assign =
decl.initial_value(); assign.is_valid()) {
1293 if (
result.type != cls) {
1296 decl.identifier().str(),
1300 else if (!
result.is_constexpr()) {
1321 prefix = (access ?
"_" :
"") + cls->
original +
"_" + (access ?
"()" :
"") +
"." + prefix;
1324 return {prefix, cls};
1333 if (err_handler.
err.has_value()) {
static constexpr const char * namespace_separator
#define NOTE(node, diag, id)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
SymbolTemplate< SymbolClass > SymbolClassTemplate
static const char * ns_sep
Result< ParsedAttribute > resource_type_from_attributes(AttrList list)
@ TemplateParameterNotConstexpr
@ ConstexprVarMustBeInitializedByConstantExpr
@ ReferenceCannotBindTemporary
@ StructuredBindingTooManyNames
@ ExprNotIntegralConstant
@ AnonymousUnionNotSupportedAtNamespaceScope
@ ConstexprMemberNonStatic
@ ExpectedScalarInitializer
@ ConstexprGlobalNonStatic
@ ResourceOutOfClassDeclaration
@ NoteInStorageBufferDeclaration
@ ExpectedResourceDefinitionOfType
@ RedefinitionOfEntryPointFunction
@ StaticDataMemberNotAllowed
@ ResourcesNotAllowedInAnonymousStruct
@ TemplateParameterCountMismatch
@ ConditionAttributeUnsupported
@ NoteInUniformBufferDeclaration
@ ConstexprVarMustNotBeArray
@ NoteInstantiationClassTemplateRequested
@ ExpectedEntryPointResourceAttribute
@ AttributeNotAllowedInComp
@ AttributeOnlyEntryPointsCanUseOut
@ InvalidResourceTypeForResourceTableClass
@ AttributeOnlyEntryPointsCanUseIn
@ NoteInstantiationFuncTemplateRequested
@ AutoTypeCannotBeDeduced
@ ReferenceVariableRequiresInitializer
@ ResourceAttributesOnlyOnEntryPointArgs
@ TemplateMissingDefinition
@ ConstexprVarMustBeValidType
@ TemplateMissingParameters
@ StructuredBindingNotEnoughNames
@ TemplateMissingParentClassInstantiation
@ ExpectedResourceOfEitherType
@ CapacityArrayDimensionMismatch
@ ConditionAttributeNotOnResource
@ ResourceTableMustBeReference
@ UsingNamespaceNotSupported
@ ConstexprVarMustBeInitializedByCorrectType
string to_string(const char *str)
std::optional< Error > err
InitializerList initializer_list() const
bool contains_attr(std::string_view attr_name) const
IdQualified identifier() const
IdQualified parent_class() const
bool is_enum_class() const
Condition condition() const
IdQualified identifier() const
AttrList attributes() const
IdQualified identifier() const
TemplateParamList template_params() const
IdQualified identifier() const
RecursiveTypedRange< NodeT > descendants_of_type() const
TypedChildRange< NodeT > children_of_type() const
std::string_view str() const
TemplateArgList arguments() const
TemplateParamList parameters() const
TemplateParamList parameters() const
bool is_namespace() const
IdQualified aliased() const
IdQualified identifier() const
AttrList attributes() const
bool is_reference() const
ConstexprValue(int32_t s)
void error(Token tok, const string &msg)
NodeErrorHandler(ErrorHandler &handler)
Represents a specific position within a source file or included sub-file.
bool is_std140_compatible
SymbolClassTemplate * template_data
ResourceTableType srt_type
builtin::ClassId builtin_class
bool is_std430_compatible
void ensure_size_and_align(NodeErrorHandler *err_handler=nullptr, bool check_st430=false)
SymbolFunctionTemplate * template_data
SymbolClass * return_type
void add_argument(bool is_const, SymbolClass *type, ast::Expr default_value={})
enum bsl::SymbolFunction::EntryPointType entry_point_type
bool is_entry_point() const
static constexpr const char * inline_fn_ret_id
enum bsl::SymbolFunction::Type fn_type
SymbolScope * non_anonymous_parent
void parse_local_scope(SymbolScope &scope, LocalScope local_node, const std::string &prefix)
void parse_enum_value(SymbolScope &scope, EnumValue val, ClassDecl cls, ConstexprValue &enum_last_val, const string &prefix)
void parse_template_inst(SymbolScope &scope, TemplateInst temp, const std::string &prefix)
void parse_template_decl(SymbolScope &scope, TemplateDecl decl)
SymbolParser(ErrorHandler &handler, SymbolTable &table)
void parse_var_decl(SymbolScope &scope, VarDecl var, int &offset, int &bitfield_offset, SymbolVariable *&bitfield_base, const std::string &prefix)
void parse_function_arguments(SymbolScope &scope, FuncArgList arg_list)
void parse_template_spec(SymbolScope &scope, TemplateSpec temp, const std::string &prefix)
void parse_template_arguments(SymbolScope &scope, TemplateDecl decl, TemplateInst inst)
void parse_pipeline_decl(SymbolScope &scope, PipelineDecl decl, const std::string &prefix)
ExpressionResult initialize_assign(const SymbolScope &scope, SymbolClass *cls, AssignStmt assign)
ExpressionResult eval_scalar_initializer_list(const SymbolScope &scope, SymbolClass *cls, InitializerList list)
ExpressionResult initialize_constexpr(const SymbolScope &scope, SymbolClass *cls, Declarator decl)
void parse_preprocessor(SymbolScope &, Preprocessor directive)
void parse_using_stmt(SymbolScope &scope, UsingStmt stmt)
static AnonScopePrefix anonymous_scope_prefix(SymbolClass *cls)
void parse_scope(SymbolScope &scope, Node node, const std::string &prefix, TemplateInst template_inst={})
SymbolFunction * parse_func_decl(SymbolScope &scope, FuncDecl func, const std::string &prefix, const std::string &suffix="", optional< SourceLocation > poi=nullopt, TemplateInst temp={})
void parse_func_forward_decl(SymbolScope &scope, FuncForwardDecl decl)
TemplateDecl check_template_instance(TemplateParamList temp_params, DeclAst &decl, const SymbolT &sym)
void parse_namespace(SymbolScope &scope, ast::Namespace decl, string ns_prefix)
void parse_function_return_type(SymbolScope &scope, ast::IdType return_type)
void parse_structured_binding(SymbolScope &scope, StructuredBinding decl)
void parse_loop_arguments(SymbolScope &scope, ForLoop loop)
SymbolClass * parse_class_decl(SymbolScope &scope, ClassDecl decl, ClassDecl parent_class, int &offset, const std::string &prefix, const std::string &suffix="", optional< SourceLocation > poi=nullopt, TemplateInst temp={}, SymbolClassTemplate *cls_template=nullptr)
unordered_map< string, SymbolScope * > scopes
vector< SymbolVariable * > non_static_variables_in_declaration_order() const
vector< std::pair< ast::FuncForwardDecl, SymbolFunction * > > function_prototypes
bool function_emplace(SymbolFunction *fn, bool builtin=false)
SymbolMap< SymbolFunction > functions
optional< SourceLocation > poi
bool variable_emplace(string id, SymbolVariable *var)
SymbolMap< SymbolVariable > variables
SymbolFunction * lookup_function_base(const SymbolTable &table, ast::IdQualified id) const
SymbolMap< SymbolClass > classes
Result< SymbolFunction * > lookup_function(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolClass * lookup_class_base(const SymbolTable &table, ast::IdQualified id) const
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
std::pair< ast::FuncForwardDecl, SymbolFunction * > lookup_function_forward_decl(ast::FuncDecl decl) const
void parse(ast::LocalScope node, ErrorHandler &err_handler)
Result< StringPair > mangle_identifier(ast::TemplateArgList args, ast::TemplateParamList list, const SymbolScope &scope) const
Allocator< SymbolFunction > fun_arena
SymbolClass * get_literal_type(string_view lit) const
Result< SymbolClass * > resolve_auto_type(SymbolScope &scope, ast::Declarator decl) const
Allocator< SymbolClass > cls_arena
Allocator< SymbolScope > scp_arena
Allocator< SymbolClassTemplate > tmp_cls_arena
Result< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const
Allocator< SymbolVariable > var_arena
Allocator< SymbolFunctionTemplate > tmp_fun_arena
unordered_map< string, T * > instances
std::string_view str() const
std::string_view str_with_whitespace() const
Token next(int i=1) const