resolver: Use Switch() for type-dispatch

Bug: tint:1383
Change-Id: I9efbe6b3e7c0314a76f65b5e8969f1f20bcecf93
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/79771
Reviewed-by: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2022-02-14 10:05:16 +00:00 committed by Tint LUCI CQ
parent 6f9ac3524a
commit f95f11417e
2 changed files with 366 additions and 338 deletions

View File

@ -119,25 +119,24 @@ bool Resolver::ResolveInternal() {
// Process all module-scope declarations in dependency order. // Process all module-scope declarations in dependency order.
for (auto* decl : dependencies_.ordered_globals) { for (auto* decl : dependencies_.ordered_globals) {
if (auto* td = decl->As<ast::TypeDecl>()) { Mark(decl);
Mark(td); if (!Switch(
if (!TypeDecl(td)) { decl, //
return false; [&](const ast::TypeDecl* td) { //
} return TypeDecl(td) != nullptr;
} else if (auto* func = decl->As<ast::Function>()) { },
Mark(func); [&](const ast::Function* func) {
if (!Function(func)) { return Function(func) != nullptr;
return false; },
} [&](const ast::Variable* var) {
} else if (auto* var = decl->As<ast::Variable>()) { return GlobalVariable(var) != nullptr;
Mark(var); },
if (!GlobalVariable(var)) { [&](Default) {
return false;
}
} else {
TINT_UNREACHABLE(Resolver, diagnostics_) TINT_UNREACHABLE(Resolver, diagnostics_)
<< "unhandled global declaration: " << decl->TypeInfo().name; << "unhandled global declaration: " << decl->TypeInfo().name;
return false; return false;
})) {
return false;
} }
} }
@ -165,23 +164,24 @@ bool Resolver::ResolveInternal() {
sem::Type* Resolver::Type(const ast::Type* ty) { sem::Type* Resolver::Type(const ast::Type* ty) {
Mark(ty); Mark(ty);
auto* s = [&]() -> sem::Type* { auto* s = Switch(
if (ty->Is<ast::Void>()) { ty,
[&](const ast::Void*) -> sem::Type* {
return builder_->create<sem::Void>(); return builder_->create<sem::Void>();
} },
if (ty->Is<ast::Bool>()) { [&](const ast::Bool*) -> sem::Type* {
return builder_->create<sem::Bool>(); return builder_->create<sem::Bool>();
} },
if (ty->Is<ast::I32>()) { [&](const ast::I32*) -> sem::Type* {
return builder_->create<sem::I32>(); return builder_->create<sem::I32>();
} },
if (ty->Is<ast::U32>()) { [&](const ast::U32*) -> sem::Type* {
return builder_->create<sem::U32>(); return builder_->create<sem::U32>();
} },
if (ty->Is<ast::F32>()) { [&](const ast::F32*) -> sem::Type* {
return builder_->create<sem::F32>(); return builder_->create<sem::F32>();
} },
if (auto* t = ty->As<ast::Vector>()) { [&](const ast::Vector* t) -> sem::Type* {
if (!t->type) { if (!t->type) {
AddError("missing vector element type", t->source.End()); AddError("missing vector element type", t->source.End());
return nullptr; return nullptr;
@ -194,8 +194,8 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
} }
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::Matrix>()) { [&](const ast::Matrix* t) -> sem::Type* {
if (!t->type) { if (!t->type) {
AddError("missing matrix element type", t->source.End()); AddError("missing matrix element type", t->source.End());
return nullptr; return nullptr;
@ -211,11 +211,9 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
} }
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::Array>()) { [&](const ast::Array* t) -> sem::Type* { return Array(t); },
return Array(t); [&](const ast::Atomic* t) -> sem::Type* {
}
if (auto* t = ty->As<ast::Atomic>()) {
if (auto* el = Type(t->type)) { if (auto* el = Type(t->type)) {
auto* a = builder_->create<sem::Atomic>(el); auto* a = builder_->create<sem::Atomic>(el);
if (!ValidateAtomic(t, a)) { if (!ValidateAtomic(t, a)) {
@ -224,8 +222,8 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
return a; return a;
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::Pointer>()) { [&](const ast::Pointer* t) -> sem::Type* {
if (auto* el = Type(t->type)) { if (auto* el = Type(t->type)) {
auto access = t->access; auto access = t->access;
if (access == ast::kUndefined) { if (access == ast::kUndefined) {
@ -234,29 +232,29 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
return builder_->create<sem::Pointer>(el, t->storage_class, access); return builder_->create<sem::Pointer>(el, t->storage_class, access);
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::Sampler>()) { [&](const ast::Sampler* t) -> sem::Type* {
return builder_->create<sem::Sampler>(t->kind); return builder_->create<sem::Sampler>(t->kind);
} },
if (auto* t = ty->As<ast::SampledTexture>()) { [&](const ast::SampledTexture* t) -> sem::Type* {
if (auto* el = Type(t->type)) { if (auto* el = Type(t->type)) {
return builder_->create<sem::SampledTexture>(t->dim, el); return builder_->create<sem::SampledTexture>(t->dim, el);
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::MultisampledTexture>()) { [&](const ast::MultisampledTexture* t) -> sem::Type* {
if (auto* el = Type(t->type)) { if (auto* el = Type(t->type)) {
return builder_->create<sem::MultisampledTexture>(t->dim, el); return builder_->create<sem::MultisampledTexture>(t->dim, el);
} }
return nullptr; return nullptr;
} },
if (auto* t = ty->As<ast::DepthTexture>()) { [&](const ast::DepthTexture* t) -> sem::Type* {
return builder_->create<sem::DepthTexture>(t->dim); return builder_->create<sem::DepthTexture>(t->dim);
} },
if (auto* t = ty->As<ast::DepthMultisampledTexture>()) { [&](const ast::DepthMultisampledTexture* t) -> sem::Type* {
return builder_->create<sem::DepthMultisampledTexture>(t->dim); return builder_->create<sem::DepthMultisampledTexture>(t->dim);
} },
if (auto* t = ty->As<ast::StorageTexture>()) { [&](const ast::StorageTexture* t) -> sem::Type* {
if (auto* el = Type(t->type)) { if (auto* el = Type(t->type)) {
if (!ValidateStorageTexture(t)) { if (!ValidateStorageTexture(t)) {
return nullptr; return nullptr;
@ -265,23 +263,30 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
t->access, el); t->access, el);
} }
return nullptr; return nullptr;
} },
if (ty->As<ast::ExternalTexture>()) { [&](const ast::ExternalTexture*) -> sem::Type* {
return builder_->create<sem::ExternalTexture>(); return builder_->create<sem::ExternalTexture>();
} },
[&](Default) -> sem::Type* {
return Switch( return Switch(
ResolvedSymbol(ty), // ResolvedSymbol(ty), //
[&](sem::Type* type) { return type; }, [&](sem::Type* type) { return type; },
[&](sem::Variable* var) { [&](sem::Variable* var) {
auto name = builder_->Symbols().NameFor(var->Declaration()->symbol); auto name =
AddError("cannot use variable '" + name + "' as type", ty->source); builder_->Symbols().NameFor(var->Declaration()->symbol);
AddNote("'" + name + "' declared here", var->Declaration()->source); AddError("cannot use variable '" + name + "' as type",
ty->source);
AddNote("'" + name + "' declared here",
var->Declaration()->source);
return nullptr; return nullptr;
}, },
[&](sem::Function* func) { [&](sem::Function* func) {
auto name = builder_->Symbols().NameFor(func->Declaration()->symbol); auto name =
AddError("cannot use function '" + name + "' as type", ty->source); builder_->Symbols().NameFor(func->Declaration()->symbol);
AddNote("'" + name + "' declared here", func->Declaration()->source); AddError("cannot use function '" + name + "' as type",
ty->source);
AddNote("'" + name + "' declared here",
func->Declaration()->source);
return nullptr; return nullptr;
}, },
[&](Default) { [&](Default) {
@ -289,7 +294,7 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
<< "Unhandled ast::Type: " << ty->TypeInfo().name; << "Unhandled ast::Type: " << ty->TypeInfo().name;
return nullptr; return nullptr;
}); });
}(); });
if (s) { if (s) {
builder_->Sem().Add(ty, s); builder_->Sem().Add(ty, s);
@ -520,30 +525,27 @@ void Resolver::AllocateOverridableConstantIds() {
void Resolver::SetShadows() { void Resolver::SetShadows() {
for (auto it : dependencies_.shadows) { for (auto it : dependencies_.shadows) {
auto* var = Sem(it.first); Switch(
if (auto* local = var->As<sem::LocalVariable>()) { Sem(it.first), //
local->SetShadows(Sem(it.second)); [&](sem::LocalVariable* local) { local->SetShadows(Sem(it.second)); },
} [&](sem::Parameter* param) { param->SetShadows(Sem(it.second)); });
if (auto* param = var->As<sem::Parameter>()) {
param->SetShadows(Sem(it.second));
}
} }
} // namespace resolver } // namespace resolver
bool Resolver::GlobalVariable(const ast::Variable* var) { sem::GlobalVariable* Resolver::GlobalVariable(const ast::Variable* var) {
auto* sem = Variable(var, VariableKind::kGlobal); auto* sem = Variable(var, VariableKind::kGlobal);
if (!sem) { if (!sem) {
return false; return nullptr;
} }
auto storage_class = sem->StorageClass(); auto storage_class = sem->StorageClass();
if (!var->is_const && storage_class == ast::StorageClass::kNone) { if (!var->is_const && storage_class == ast::StorageClass::kNone) {
AddError("global variables must have a storage class", var->source); AddError("global variables must have a storage class", var->source);
return false; return nullptr;
} }
if (var->is_const && storage_class != ast::StorageClass::kNone) { if (var->is_const && storage_class != ast::StorageClass::kNone) {
AddError("global constants shouldn't have a storage class", var->source); AddError("global constants shouldn't have a storage class", var->source);
return false; return nullptr;
} }
for (auto* attr : var->attributes) { for (auto* attr : var->attributes) {
@ -558,20 +560,20 @@ bool Resolver::GlobalVariable(const ast::Variable* var) {
} }
if (!ValidateNoDuplicateAttributes(var->attributes)) { if (!ValidateNoDuplicateAttributes(var->attributes)) {
return false; return nullptr;
} }
if (!ValidateGlobalVariable(sem)) { if (!ValidateGlobalVariable(sem)) {
return false; return nullptr;
} }
// TODO(bclayton): Call this at the end of resolve on all uniform and storage // TODO(bclayton): Call this at the end of resolve on all uniform and storage
// referenced structs // referenced structs
if (!ValidateStorageClassLayout(sem)) { if (!ValidateStorageClassLayout(sem)) {
return false; return nullptr;
} }
return true; return sem->As<sem::GlobalVariable>();
} }
sem::Function* Resolver::Function(const ast::Function* decl) { sem::Function* Resolver::Function(const ast::Function* decl) {
@ -858,66 +860,71 @@ bool Resolver::Statements(const ast::StatementList& stmts) {
} }
sem::Statement* Resolver::Statement(const ast::Statement* stmt) { sem::Statement* Resolver::Statement(const ast::Statement* stmt) {
if (stmt->Is<ast::CaseStatement>()) { return Switch(
stmt,
// Compound statements. These create their own sem::CompoundStatement
// bindings.
[&](const ast::BlockStatement* b) -> sem::Statement* {
return BlockStatement(b);
},
[&](const ast::ForLoopStatement* l) -> sem::Statement* {
return ForLoopStatement(l);
},
[&](const ast::LoopStatement* l) -> sem::Statement* {
return LoopStatement(l);
},
[&](const ast::IfStatement* i) -> sem::Statement* {
return IfStatement(i);
},
[&](const ast::SwitchStatement* s) -> sem::Statement* {
return SwitchStatement(s);
},
// Non-Compound statements
[&](const ast::AssignmentStatement* a) -> sem::Statement* {
return AssignmentStatement(a);
},
[&](const ast::BreakStatement* b) -> sem::Statement* {
return BreakStatement(b);
},
[&](const ast::CallStatement* c) -> sem::Statement* {
return CallStatement(c);
},
[&](const ast::ContinueStatement* c) -> sem::Statement* {
return ContinueStatement(c);
},
[&](const ast::DiscardStatement* d) -> sem::Statement* {
return DiscardStatement(d);
},
[&](const ast::FallthroughStatement* f) -> sem::Statement* {
return FallthroughStatement(f);
},
[&](const ast::ReturnStatement* r) -> sem::Statement* {
return ReturnStatement(r);
},
[&](const ast::VariableDeclStatement* v) -> sem::Statement* {
return VariableDeclStatement(v);
},
// Error cases
[&](const ast::CaseStatement*) -> sem::Statement* {
AddError("case statement can only be used inside a switch statement", AddError("case statement can only be used inside a switch statement",
stmt->source); stmt->source);
return nullptr; return nullptr;
} },
if (stmt->Is<ast::ElseStatement>()) { [&](const ast::ElseStatement*) -> sem::Statement* {
TINT_ICE(Resolver, diagnostics_) TINT_ICE(Resolver, diagnostics_)
<< "Resolver::Statement() encountered an Else statement. Else " << "Resolver::Statement() encountered an Else statement. Else "
"statements are embedded in If statements, so should never be " "statements are embedded in If statements, so should never be "
"encountered as top-level statements"; "encountered as top-level statements";
return nullptr; return nullptr;
} },
[&](Default) -> sem::Statement* {
// Compound statements. These create their own sem::CompoundStatement AddError(
// bindings. "unknown statement type: " + std::string(stmt->TypeInfo().name),
if (auto* b = stmt->As<ast::BlockStatement>()) {
return BlockStatement(b);
}
if (auto* l = stmt->As<ast::ForLoopStatement>()) {
return ForLoopStatement(l);
}
if (auto* l = stmt->As<ast::LoopStatement>()) {
return LoopStatement(l);
}
if (auto* i = stmt->As<ast::IfStatement>()) {
return IfStatement(i);
}
if (auto* s = stmt->As<ast::SwitchStatement>()) {
return SwitchStatement(s);
}
// Non-Compound statements
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
return AssignmentStatement(a);
}
if (auto* b = stmt->As<ast::BreakStatement>()) {
return BreakStatement(b);
}
if (auto* c = stmt->As<ast::CallStatement>()) {
return CallStatement(c);
}
if (auto* c = stmt->As<ast::ContinueStatement>()) {
return ContinueStatement(c);
}
if (auto* d = stmt->As<ast::DiscardStatement>()) {
return DiscardStatement(d);
}
if (auto* f = stmt->As<ast::FallthroughStatement>()) {
return FallthroughStatement(f);
}
if (auto* r = stmt->As<ast::ReturnStatement>()) {
return ReturnStatement(r);
}
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
return VariableDeclStatement(v);
}
AddError("unknown statement type: " + std::string(stmt->TypeInfo().name),
stmt->source); stmt->source);
return nullptr; return nullptr;
});
} }
sem::CaseStatement* Resolver::CaseStatement(const ast::CaseStatement* stmt) { sem::CaseStatement* Resolver::CaseStatement(const ast::CaseStatement* stmt) {
@ -1137,32 +1144,42 @@ sem::Expression* Resolver::Expression(const ast::Expression* root) {
} }
for (auto* expr : utils::Reverse(sorted)) { for (auto* expr : utils::Reverse(sorted)) {
sem::Expression* sem_expr = nullptr; auto* sem_expr = Switch(
if (auto* array = expr->As<ast::IndexAccessorExpression>()) { expr,
sem_expr = IndexAccessor(array); [&](const ast::IndexAccessorExpression* array) -> sem::Expression* {
} else if (auto* bin_op = expr->As<ast::BinaryExpression>()) { return IndexAccessor(array);
sem_expr = Binary(bin_op); },
} else if (auto* bitcast = expr->As<ast::BitcastExpression>()) { [&](const ast::BinaryExpression* bin_op) -> sem::Expression* {
sem_expr = Bitcast(bitcast); return Binary(bin_op);
} else if (auto* call = expr->As<ast::CallExpression>()) { },
sem_expr = Call(call); [&](const ast::BitcastExpression* bitcast) -> sem::Expression* {
} else if (auto* ident = expr->As<ast::IdentifierExpression>()) { return Bitcast(bitcast);
sem_expr = Identifier(ident); },
} else if (auto* literal = expr->As<ast::LiteralExpression>()) { [&](const ast::CallExpression* call) -> sem::Expression* {
sem_expr = Literal(literal); return Call(call);
} else if (auto* member = expr->As<ast::MemberAccessorExpression>()) { },
sem_expr = MemberAccessor(member); [&](const ast::IdentifierExpression* ident) -> sem::Expression* {
} else if (auto* unary = expr->As<ast::UnaryOpExpression>()) { return Identifier(ident);
sem_expr = UnaryOp(unary); },
} else if (expr->Is<ast::PhonyExpression>()) { [&](const ast::LiteralExpression* literal) -> sem::Expression* {
sem_expr = builder_->create<sem::Expression>( return Literal(literal);
},
[&](const ast::MemberAccessorExpression* member) -> sem::Expression* {
return MemberAccessor(member);
},
[&](const ast::UnaryOpExpression* unary) -> sem::Expression* {
return UnaryOp(unary);
},
[&](const ast::PhonyExpression*) -> sem::Expression* {
return builder_->create<sem::Expression>(
expr, builder_->create<sem::Void>(), current_statement_, expr, builder_->create<sem::Void>(), current_statement_,
sem::Constant{}, /* has_side_effects */ false); sem::Constant{}, /* has_side_effects */ false);
} else { },
[&](Default) {
TINT_ICE(Resolver, diagnostics_) TINT_ICE(Resolver, diagnostics_)
<< "unhandled expression type: " << expr->TypeInfo().name; << "unhandled expression type: " << expr->TypeInfo().name;
return nullptr; return nullptr;
} });
if (!sem_expr) { if (!sem_expr) {
return nullptr; return nullptr;
} }
@ -1183,15 +1200,23 @@ sem::Expression* Resolver::IndexAccessor(
auto* obj = Sem(expr->object); auto* obj = Sem(expr->object);
auto* obj_raw_ty = obj->Type(); auto* obj_raw_ty = obj->Type();
auto* obj_ty = obj_raw_ty->UnwrapRef(); auto* obj_ty = obj_raw_ty->UnwrapRef();
const sem::Type* ty = nullptr; auto* ty = Switch(
if (auto* arr = obj_ty->As<sem::Array>()) { obj_ty, //
ty = arr->ElemType(); [&](const sem::Array* arr) -> const sem::Type* {
} else if (auto* vec = obj_ty->As<sem::Vector>()) { return arr->ElemType();
ty = vec->type(); },
} else if (auto* mat = obj_ty->As<sem::Matrix>()) { [&](const sem::Vector* vec) -> const sem::Type* { //
ty = builder_->create<sem::Vector>(mat->type(), mat->rows()); return vec->type();
} else { },
AddError("cannot index type '" + TypeNameOf(obj_ty) + "'", expr->source); [&](const sem::Matrix* mat) -> const sem::Type* {
return builder_->create<sem::Vector>(mat->type(), mat->rows());
},
[&](Default) -> const sem::Type* {
AddError("cannot index type '" + TypeNameOf(obj_ty) + "'",
expr->source);
return nullptr;
});
if (ty == nullptr) {
return nullptr; return nullptr;
} }
@ -1528,24 +1553,30 @@ sem::Call* Resolver::TypeConversion(const ast::CallExpression* expr,
// Now that the argument types have been determined, make sure that // Now that the argument types have been determined, make sure that
// they obey the conversion rules laid out in // they obey the conversion rules laid out in
// https://gpuweb.github.io/gpuweb/wgsl/#conversion-expr. // https://gpuweb.github.io/gpuweb/wgsl/#conversion-expr.
bool ok = true; bool ok = Switch(
if (auto* vec_type = target->As<sem::Vector>()) { target,
ok = ValidateVectorConstructorOrCast(expr, vec_type); [&](const sem::Vector* vec_type) {
} else if (auto* mat_type = target->As<sem::Matrix>()) { return ValidateVectorConstructorOrCast(expr, vec_type);
},
[&](const sem::Matrix* mat_type) {
// Note: Matrix types currently cannot be converted (the element // Note: Matrix types currently cannot be converted (the element
// type must only be f32). We implement this for the day we support // type must only be f32). We implement this for the day we
// other matrix element types. // support other matrix element types.
ok = ValidateMatrixConstructorOrCast(expr, mat_type); return ValidateMatrixConstructorOrCast(expr, mat_type);
} else if (target->is_scalar()) { },
ok = ValidateScalarConstructorOrCast(expr, target); [&](const sem::Array* arr_type) {
} else if (auto* arr_type = target->As<sem::Array>()) { return ValidateArrayConstructorOrCast(expr, arr_type);
ok = ValidateArrayConstructorOrCast(expr, arr_type); },
} else if (auto* struct_type = target->As<sem::Struct>()) { [&](const sem::Struct* struct_type) {
ok = ValidateStructureConstructorOrCast(expr, struct_type); return ValidateStructureConstructorOrCast(expr, struct_type);
} else { },
AddError("type is not constructible", expr->source); [&](Default) {
return nullptr; if (target->is_scalar()) {
return ValidateScalarConstructorOrCast(expr, target);
} }
AddError("type is not constructible", expr->source);
return false;
});
if (!ok) { if (!ok) {
return nullptr; return nullptr;
} }
@ -1588,21 +1619,27 @@ sem::Call* Resolver::TypeConstructor(
// Now that the argument types have been determined, make sure that // Now that the argument types have been determined, make sure that
// they obey the constructor type rules laid out in // they obey the constructor type rules laid out in
// https://gpuweb.github.io/gpuweb/wgsl/#type-constructor-expr. // https://gpuweb.github.io/gpuweb/wgsl/#type-constructor-expr.
bool ok = true; bool ok = Switch(
if (auto* vec_type = ty->As<sem::Vector>()) { ty,
ok = ValidateVectorConstructorOrCast(expr, vec_type); [&](const sem::Vector* vec_type) {
} else if (auto* mat_type = ty->As<sem::Matrix>()) { return ValidateVectorConstructorOrCast(expr, vec_type);
ok = ValidateMatrixConstructorOrCast(expr, mat_type); },
} else if (ty->is_scalar()) { [&](const sem::Matrix* mat_type) {
ok = ValidateScalarConstructorOrCast(expr, ty); return ValidateMatrixConstructorOrCast(expr, mat_type);
} else if (auto* arr_type = ty->As<sem::Array>()) { },
ok = ValidateArrayConstructorOrCast(expr, arr_type); [&](const sem::Array* arr_type) {
} else if (auto* struct_type = ty->As<sem::Struct>()) { return ValidateArrayConstructorOrCast(expr, arr_type);
ok = ValidateStructureConstructorOrCast(expr, struct_type); },
} else { [&](const sem::Struct* struct_type) {
AddError("type is not constructible", expr->source); return ValidateStructureConstructorOrCast(expr, struct_type);
return nullptr; },
[&](Default) {
if (ty->is_scalar()) {
return ValidateScalarConstructorOrCast(expr, ty);
} }
AddError("type is not constructible", expr->source);
return false;
});
if (!ok) { if (!ok) {
return nullptr; return nullptr;
} }
@ -2155,21 +2192,25 @@ std::string Resolver::RawTypeNameOf(const sem::Type* ty) {
} }
sem::Type* Resolver::TypeOf(const ast::LiteralExpression* lit) { sem::Type* Resolver::TypeOf(const ast::LiteralExpression* lit) {
if (lit->Is<ast::SintLiteralExpression>()) { return Switch(
lit,
[&](const ast::SintLiteralExpression*) -> sem::Type* {
return builder_->create<sem::I32>(); return builder_->create<sem::I32>();
} },
if (lit->Is<ast::UintLiteralExpression>()) { [&](const ast::UintLiteralExpression*) -> sem::Type* {
return builder_->create<sem::U32>(); return builder_->create<sem::U32>();
} },
if (lit->Is<ast::FloatLiteralExpression>()) { [&](const ast::FloatLiteralExpression*) -> sem::Type* {
return builder_->create<sem::F32>(); return builder_->create<sem::F32>();
} },
if (lit->Is<ast::BoolLiteralExpression>()) { [&](const ast::BoolLiteralExpression*) -> sem::Type* {
return builder_->create<sem::Bool>(); return builder_->create<sem::Bool>();
} },
[&](Default) -> sem::Type* {
TINT_UNREACHABLE(Resolver, diagnostics_) TINT_UNREACHABLE(Resolver, diagnostics_)
<< "Unhandled literal type: " << lit->TypeInfo().name; << "Unhandled literal type: " << lit->TypeInfo().name;
return nullptr; return nullptr;
});
} }
sem::Array* Resolver::Array(const ast::Array* arr) { sem::Array* Resolver::Array(const ast::Array* arr) {
@ -2770,30 +2811,23 @@ bool Resolver::IsPlain(const sem::Type* type) const {
// https://gpuweb.github.io/gpuweb/wgsl/#fixed-footprint-types // https://gpuweb.github.io/gpuweb/wgsl/#fixed-footprint-types
bool Resolver::IsFixedFootprint(const sem::Type* type) const { bool Resolver::IsFixedFootprint(const sem::Type* type) const {
if (type->is_scalar()) { return Switch(
return true; type, //
} [&](const sem::Vector*) { return true; }, //
if (type->Is<sem::Vector>()) { [&](const sem::Matrix*) { return true; }, //
return true; [&](const sem::Atomic*) { return true; },
} [&](const sem::Array* arr) {
if (type->Is<sem::Matrix>()) {
return true;
}
if (type->Is<sem::Atomic>()) {
return true;
}
if (auto* arr = type->As<sem::Array>()) {
return !arr->IsRuntimeSized() && IsFixedFootprint(arr->ElemType()); return !arr->IsRuntimeSized() && IsFixedFootprint(arr->ElemType());
} },
if (auto* str = type->As<sem::Struct>()) { [&](const sem::Struct* str) {
for (auto* member : str->Members()) { for (auto* member : str->Members()) {
if (!IsFixedFootprint(member->Type())) { if (!IsFixedFootprint(member->Type())) {
return false; return false;
} }
} }
return true; return true;
} },
return false; [&](Default) { return type->is_scalar(); });
} }
// https://gpuweb.github.io/gpuweb/wgsl.html#storable-types // https://gpuweb.github.io/gpuweb/wgsl.html#storable-types
@ -2806,27 +2840,22 @@ bool Resolver::IsHostShareable(const sem::Type* type) const {
if (type->IsAnyOf<sem::I32, sem::U32, sem::F32>()) { if (type->IsAnyOf<sem::I32, sem::U32, sem::F32>()) {
return true; return true;
} }
if (auto* vec = type->As<sem::Vector>()) { return Switch(
return IsHostShareable(vec->type()); type, //
} [&](const sem::Vector* vec) { return IsHostShareable(vec->type()); },
if (auto* mat = type->As<sem::Matrix>()) { [&](const sem::Matrix* mat) { return IsHostShareable(mat->type()); },
return IsHostShareable(mat->type()); [&](const sem::Array* arr) { return IsHostShareable(arr->ElemType()); },
} [&](const sem::Struct* str) {
if (auto* arr = type->As<sem::Array>()) {
return IsHostShareable(arr->ElemType());
}
if (auto* str = type->As<sem::Struct>()) {
for (auto* member : str->Members()) { for (auto* member : str->Members()) {
if (!IsHostShareable(member->Type())) { if (!IsHostShareable(member->Type())) {
return false; return false;
} }
} }
return true; return true;
} },
if (auto* atomic = type->As<sem::Atomic>()) { [&](const sem::Atomic* atomic) {
return IsHostShareable(atomic->Type()); return IsHostShareable(atomic->Type());
} });
return false;
} }
bool Resolver::IsBuiltin(Symbol symbol) const { bool Resolver::IsBuiltin(Symbol symbol) const {

View File

@ -219,6 +219,7 @@ class Resolver {
sem::ElseStatement* ElseStatement(const ast::ElseStatement*); sem::ElseStatement* ElseStatement(const ast::ElseStatement*);
sem::Statement* FallthroughStatement(const ast::FallthroughStatement*); sem::Statement* FallthroughStatement(const ast::FallthroughStatement*);
sem::ForLoopStatement* ForLoopStatement(const ast::ForLoopStatement*); sem::ForLoopStatement* ForLoopStatement(const ast::ForLoopStatement*);
sem::GlobalVariable* GlobalVariable(const ast::Variable*);
sem::Statement* Parameter(const ast::Variable*); sem::Statement* Parameter(const ast::Variable*);
sem::IfStatement* IfStatement(const ast::IfStatement*); sem::IfStatement* IfStatement(const ast::IfStatement*);
sem::LoopStatement* LoopStatement(const ast::LoopStatement*); sem::LoopStatement* LoopStatement(const ast::LoopStatement*);
@ -228,8 +229,6 @@ class Resolver {
sem::Statement* VariableDeclStatement(const ast::VariableDeclStatement*); sem::Statement* VariableDeclStatement(const ast::VariableDeclStatement*);
bool Statements(const ast::StatementList&); bool Statements(const ast::StatementList&);
bool GlobalVariable(const ast::Variable*);
// AST and Type validation methods // AST and Type validation methods
// Each return true on success, false on failure. // Each return true on success, false on failure.
bool ValidateAlias(const ast::Alias*); bool ValidateAlias(const ast::Alias*);