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

@@ -32,7 +32,8 @@ ParameterList GetParameters(ast::Function* ast) {
ParameterList parameters;
parameters.reserve(ast->params().size());
for (auto* param : ast->params()) {
parameters.emplace_back(Parameter{param->type(), Parameter::Usage::kNone});
parameters.emplace_back(
Parameter{param->declared_type(), Parameter::Usage::kNone});
}
return parameters;
}
@@ -160,7 +161,8 @@ Function::VariableBindings Function::ReferencedStorageTextureVariables() const {
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* unwrapped_type =
var->Declaration()->declared_type()->UnwrapIfNeeded();
auto* storage_texture = unwrapped_type->As<type::StorageTexture>();
if (storage_texture == nullptr) {
continue;
@@ -182,7 +184,8 @@ Function::VariableBindings Function::ReferencedDepthTextureVariables() const {
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* unwrapped_type =
var->Declaration()->declared_type()->UnwrapIfNeeded();
auto* storage_texture = unwrapped_type->As<type::DepthTexture>();
if (storage_texture == nullptr) {
continue;
@@ -229,7 +232,8 @@ Function::VariableBindings Function::ReferencedSamplerVariablesImpl(
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* unwrapped_type =
var->Declaration()->declared_type()->UnwrapIfNeeded();
auto* sampler = unwrapped_type->As<type::Sampler>();
if (sampler == nullptr || sampler->kind() != kind) {
continue;
@@ -252,7 +256,8 @@ Function::VariableBindings Function::ReferencedSampledTextureVariablesImpl(
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* unwrapped_type =
var->Declaration()->declared_type()->UnwrapIfNeeded();
auto* texture = unwrapped_type->As<type::Texture>();
if (texture == nullptr) {
continue;

View File

@@ -19,10 +19,12 @@ TINT_INSTANTIATE_TYPEINFO(tint::semantic::Variable);
namespace tint {
namespace semantic {
Variable::Variable(ast::Variable* declaration,
Variable::Variable(const ast::Variable* declaration,
type::Type* type,
ast::StorageClass storage_class,
std::vector<const Expression*> users)
: declaration_(declaration),
type_(type),
storage_class_(storage_class),
users_(std::move(users)) {}

View File

@@ -37,9 +37,11 @@ class Variable : public Castable<Variable, Node> {
public:
/// Constructor
/// @param declaration the AST declaration node
/// @param type the variable type
/// @param storage_class the variable storage class
/// @param users the expressions that use the variable
explicit Variable(ast::Variable* declaration,
explicit Variable(const ast::Variable* declaration,
type::Type* type,
ast::StorageClass storage_class,
std::vector<const Expression*> users);
@@ -47,7 +49,10 @@ class Variable : public Castable<Variable, Node> {
~Variable() override;
/// @returns the AST declaration node
ast::Variable* Declaration() const { return declaration_; }
const ast::Variable* Declaration() const { return declaration_; }
/// @returns the type for the variable
type::Type* Type() const { return type_; }
/// @returns the storage class for the variable
ast::StorageClass StorageClass() const { return storage_class_; }
@@ -56,7 +61,8 @@ class Variable : public Castable<Variable, Node> {
const std::vector<const Expression*>& Users() const { return users_; }
private:
ast::Variable* const declaration_;
const ast::Variable* const declaration_;
type::Type* const type_;
ast::StorageClass const storage_class_;
std::vector<const Expression*> const users_;
};