Add semantic::Variable::Type() and use it instead of ast::Variable::type()

In anticipation of adding support for type inference, no longer use
ast::Variable::type() everywhere, as it will eventually return nullptr
for type-inferred variables. Instead, the Resolver now stores the final
resolved type into the semantic::Variable, and nearly all code now makes
use of that.

ast::Variable::type() has been renamed to ast::Variable::declared_type()
to help make its usage clear, and to distinguish it from
semantic::Variable::Type().

Fixed tests that failed after this change because variables were missing
VariableDeclStatements, so there was no path to the variables during
resolving, and thus no semantic info generated for them.

Bug: tint:672
Change-Id: I0125e2f555839a4892248dc6739a72e9c7f51b1e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/46100
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano
2021-03-26 12:47:58 +00:00
committed by Commit Bot service account
parent d7f23f5c75
commit 9ef17472e8
28 changed files with 216 additions and 171 deletions

View File

@@ -39,7 +39,7 @@ ast::Variable* clone_variable_with_new_name(CloneContext* ctx,
// Clone arguments outside of create() call to have deterministic ordering
auto source = ctx->Clone(in->source());
auto symbol = ctx->dst->Symbols().Register(new_name);
auto* type = ctx->Clone(in->type());
auto* type = ctx->Clone(in->declared_type());
auto* constructor = ctx->Clone(in->constructor());
auto decorations = ctx->Clone(in->decorations());
return ctx->dst->create<ast::Variable>(

View File

@@ -145,7 +145,8 @@ void Hlsl::HandleEntryPointIOTypes(CloneContext& ctx) const {
// Build a new structure to hold the non-struct input parameters.
ast::StructMemberList struct_members;
for (auto* param : func->params()) {
if (param->type()->Is<type::Struct>()) {
auto* type = ctx.src->Sem().Get(param)->Type();
if (type->Is<type::Struct>()) {
// Already a struct, nothing to do.
continue;
}
@@ -159,14 +160,12 @@ void Hlsl::HandleEntryPointIOTypes(CloneContext& ctx) const {
auto* deco = param->decorations()[0];
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
// Create a struct member with the builtin decoration.
struct_members.push_back(
ctx.dst->Member(name, ctx.Clone(param->type()),
ast::DecorationList{ctx.Clone(builtin)}));
struct_members.push_back(ctx.dst->Member(
name, ctx.Clone(type), ast::DecorationList{ctx.Clone(builtin)}));
} else if (auto* loc = deco->As<ast::LocationDecoration>()) {
// Create a struct member with the location decoration.
struct_members.push_back(
ctx.dst->Member(name, ctx.Clone(param->type()),
ast::DecorationList{ctx.Clone(loc)}));
struct_members.push_back(ctx.dst->Member(
name, ctx.Clone(type), ast::DecorationList{ctx.Clone(loc)}));
} else {
TINT_ICE(ctx.dst->Diagnostics())
<< "Unsupported entry point parameter decoration";
@@ -195,7 +194,8 @@ void Hlsl::HandleEntryPointIOTypes(CloneContext& ctx) const {
// Replace the original parameters with function-scope constants.
for (auto* param : func->params()) {
if (param->type()->Is<type::Struct>()) {
auto* type = ctx.src->Sem().Get(param)->Type();
if (type->Is<type::Struct>()) {
// Keep struct parameters unchanged.
new_parameters.push_back(ctx.Clone(param));
continue;
@@ -207,7 +207,7 @@ void Hlsl::HandleEntryPointIOTypes(CloneContext& ctx) const {
// Initialize it with the value extracted from the struct parameter.
auto func_const_symbol = ctx.dst->Symbols().Register(name);
auto* func_const =
ctx.dst->Const(func_const_symbol, ctx.Clone(param->type()),
ctx.dst->Const(func_const_symbol, ctx.Clone(type),
ctx.dst->MemberAccessor(struct_param_symbol, name));
new_body.push_back(ctx.dst->WrapInStatement(func_const));

View File

@@ -14,6 +14,7 @@
#include "src/transform/msl.h"
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
@@ -326,9 +327,10 @@ void Msl::HandleEntryPointIOTypes(CloneContext& ctx) const {
continue;
} else if (auto* loc = deco->As<ast::LocationDecoration>()) {
// Create a struct member with the location decoration.
struct_members.push_back(ctx.dst->Member(
ctx.src->Symbols().NameFor(param->symbol()),
ctx.Clone(param->type()), ast::DecorationList{ctx.Clone(loc)}));
std::string name = ctx.src->Symbols().NameFor(param->symbol());
auto* type = ctx.Clone(ctx.src->Sem().Get(param)->Type());
struct_members.push_back(
ctx.dst->Member(name, type, ast::DecorationList{ctx.Clone(loc)}));
} else {
TINT_ICE(ctx.dst->Diagnostics())
<< "Unsupported entry point parameter decoration";
@@ -368,9 +370,9 @@ void Msl::HandleEntryPointIOTypes(CloneContext& ctx) const {
// Create a function-scope const to replace the parameter.
// Initialize it with the value extracted from the struct parameter.
auto func_const_symbol = ctx.dst->Symbols().Register(name);
auto* func_const =
ctx.dst->Const(func_const_symbol, ctx.Clone(param->type()),
ctx.dst->MemberAccessor(struct_param_symbol, name));
auto* type = ctx.Clone(ctx.src->Sem().Get(param)->Type());
auto* constructor = ctx.dst->MemberAccessor(struct_param_symbol, name);
auto* func_const = ctx.dst->Const(func_const_symbol, type, constructor);
new_body.push_back(ctx.dst->WrapInStatement(func_const));

View File

@@ -137,8 +137,8 @@ void Spirv::HandleEntryPointIOTypes(CloneContext& ctx) const {
}
for (auto* param : func->params()) {
Symbol new_var =
HoistToInputVariables(ctx, func, param->type(), param->decorations());
Symbol new_var = HoistToInputVariables(
ctx, func, ctx.src->Sem().Get(param)->Type(), param->decorations());
// Replace all uses of the function parameter with the new variable.
for (auto* user : ctx.src->Sem().Get(param)->Users()) {

View File

@@ -207,7 +207,7 @@ void VertexPulling::State::ConvertVertexInputVariablesToPrivate() {
Source{}, // source
ctx.dst->Symbols().Register(name), // symbol
ast::StorageClass::kPrivate, // storage_class
ctx.Clone(v->type()), // type
ctx.Clone(v->declared_type()), // type
false, // is_const
nullptr, // constructor
ast::DecorationList{}); // decorations