tint/resolver: Make Materialize() handle nullptr expressions

Simplifies common usage

Bug: tint:1504
Change-Id: I901c512388b0793a0b27b2bb0e40169e815342f2
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91843
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-05-27 21:39:56 +00:00 committed by Dawn LUCI CQ
parent 572eaf271e
commit eac0566718
2 changed files with 15 additions and 13 deletions

View File

@ -319,11 +319,7 @@ sem::Variable* Resolver::Variable(const ast::Variable* var,
// Does the variable have a constructor? // Does the variable have a constructor?
if (var->constructor) { if (var->constructor) {
auto* ctor = Expression(var->constructor); rhs = Materialize(Expression(var->constructor), storage_ty);
if (!ctor) {
return nullptr;
}
rhs = Materialize(ctor, storage_ty);
if (!rhs) { if (!rhs) {
return nullptr; return nullptr;
} }
@ -1107,6 +1103,10 @@ sem::Expression* Resolver::Expression(const ast::Expression* root) {
const sem::Expression* Resolver::Materialize(const sem::Expression* expr, const sem::Expression* Resolver::Materialize(const sem::Expression* expr,
const sem::Type* target_type /* = nullptr */) { const sem::Type* target_type /* = nullptr */) {
if (!expr) {
return nullptr; // Allow for Materialize(Expression(blah))
}
// Helper for actually creating the the materialize node, performing the constant cast, updating // Helper for actually creating the the materialize node, performing the constant cast, updating
// the ast -> sem binding, and performing validation. // the ast -> sem binding, and performing validation.
auto materialize = [&](const sem::Type* target_ty) -> sem::Materialize* { auto materialize = [&](const sem::Type* target_ty) -> sem::Materialize* {
@ -1118,15 +1118,20 @@ const sem::Expression* Resolver::Materialize(const sem::Expression* expr,
if (!expr_val->IsValid()) { if (!expr_val->IsValid()) {
TINT_ICE(Resolver, builder_->Diagnostics()) TINT_ICE(Resolver, builder_->Diagnostics())
<< decl->source << decl->source
<< " EvaluateConstantValue() returned invalid value for materialized " << "EvaluateConstantValue() returned invalid value for materialized value of type: "
"value of type: " << builder_->FriendlyName(expr->Type());
<< (expr->Type() ? expr->Type()->FriendlyName(builder_->Symbols()) : "<null>");
return nullptr; return nullptr;
} }
auto materialized_val = ConvertValue(expr_val.Get(), target_ty, decl->source); auto materialized_val = ConvertValue(expr_val.Get(), target_ty, decl->source);
if (!materialized_val) { if (!materialized_val) {
return nullptr; return nullptr;
} }
if (!materialized_val->IsValid()) {
TINT_ICE(Resolver, builder_->Diagnostics())
<< decl->source << "ConvertValue(" << builder_->FriendlyName(expr_val->Type())
<< " -> " << builder_->FriendlyName(target_ty) << ") returned invalid value";
return nullptr;
}
auto* m = auto* m =
builder_->create<sem::Materialize>(expr, current_statement_, materialized_val.Get()); builder_->create<sem::Materialize>(expr, current_statement_, materialized_val.Get());
m->Behaviors() = expr->Behaviors(); m->Behaviors() = expr->Behaviors();
@ -2269,11 +2274,7 @@ sem::Statement* Resolver::ReturnStatement(const ast::ReturnStatement* stmt) {
const sem::Type* value_ty = nullptr; const sem::Type* value_ty = nullptr;
if (auto* value = stmt->value) { if (auto* value = stmt->value) {
const auto* expr = Expression(value); const auto* expr = Materialize(Expression(value), current_function_->ReturnType());
if (!expr) {
return false;
}
expr = Materialize(expr, current_function_->ReturnType());
if (!expr) { if (!expr) {
return false; return false;
} }

View File

@ -211,6 +211,7 @@ class Resolver {
/// if `expr` has a element type of abstract-float. /// if `expr` has a element type of abstract-float.
/// * The sem::Materialize constant value will be the value of `expr` value-converted to the /// * The sem::Materialize constant value will be the value of `expr` value-converted to the
/// materialized type. /// materialized type.
/// If `expr` is nullptr, then Materialize() will also return nullptr.
const sem::Expression* Materialize(const sem::Expression* expr, const sem::Expression* Materialize(const sem::Expression* expr,
const sem::Type* target_type = nullptr); const sem::Type* target_type = nullptr);