Add semantic::Variable, use it.

Pull the mutable semantic field from ast::Variable and into a new semantic::Variable node.
Have the TypeDeterminer create these semantic::Variable nodes.

Bug: tint:390
Change-Id: Ia13f5e7b065941ed66ea5a86c6ccb288071feff3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40063
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-02-03 17:51:09 +00:00 committed by Commit Bot service account
parent 401b96b9bb
commit b17aea159c
26 changed files with 485 additions and 295 deletions

View File

@ -385,6 +385,7 @@ source_set("libtint_core_src") {
"src/semantic/sem_function.cc", "src/semantic/sem_function.cc",
"src/semantic/sem_info.cc", "src/semantic/sem_info.cc",
"src/semantic/sem_node.cc", "src/semantic/sem_node.cc",
"src/semantic/sem_variable.cc",
"src/semantic/type_mappings.h", "src/semantic/type_mappings.h",
"src/source.cc", "src/source.cc",
"src/source.h", "src/source.h",

View File

@ -199,6 +199,7 @@ set(TINT_LIB_SRCS
semantic/sem_function.cc semantic/sem_function.cc
semantic/sem_info.cc semantic/sem_info.cc
semantic/sem_node.cc semantic/sem_node.cc
semantic/sem_variable.cc
semantic/type_mappings.h semantic/type_mappings.h
source.cc source.cc
source.h source.h

View File

@ -19,6 +19,7 @@
#include "src/ast/constant_id_decoration.h" #include "src/ast/constant_id_decoration.h"
#include "src/clone_context.h" #include "src/clone_context.h"
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/semantic/variable.h"
TINT_INSTANTIATE_CLASS_ID(tint::ast::Variable); TINT_INSTANTIATE_CLASS_ID(tint::ast::Variable);
@ -38,7 +39,7 @@ Variable::Variable(const Source& source,
is_const_(is_const), is_const_(is_const),
constructor_(constructor), constructor_(constructor),
decorations_(std::move(decorations)), decorations_(std::move(decorations)),
storage_class_(sc) {} declared_storage_class_(sc) {}
Variable::Variable(Variable&&) = default; Variable::Variable(Variable&&) = default;
@ -91,10 +92,10 @@ uint32_t Variable::constant_id() const {
} }
Variable* Variable::Clone(CloneContext* ctx) const { Variable* Variable::Clone(CloneContext* ctx) const {
return ctx->dst->create<Variable>(ctx->Clone(source()), ctx->Clone(symbol_), return ctx->dst->create<Variable>(
storage_class(), ctx->Clone(type()), ctx->Clone(source()), ctx->Clone(symbol_), declared_storage_class(),
is_const_, ctx->Clone(constructor()), ctx->Clone(type()), is_const_, ctx->Clone(constructor()),
ctx->Clone(decorations_)); ctx->Clone(decorations_));
} }
bool Variable::IsValid() const { bool Variable::IsValid() const {
@ -110,13 +111,15 @@ bool Variable::IsValid() const {
return true; return true;
} }
void Variable::info_to_str(const semantic::Info&, void Variable::info_to_str(const semantic::Info& sem,
std::ostream& out, std::ostream& out,
size_t indent) const { size_t indent) const {
auto* var_sem = sem.Get(this);
make_indent(out, indent); make_indent(out, indent);
out << symbol_.to_str() << std::endl; out << symbol_.to_str() << std::endl;
make_indent(out, indent); make_indent(out, indent);
out << storage_class_ << std::endl; out << (var_sem ? var_sem->StorageClass() : declared_storage_class())
<< std::endl;
make_indent(out, indent); make_indent(out, indent);
out << type_->type_name() << std::endl; out << type_->type_name() << std::endl;
} }

View File

@ -84,7 +84,7 @@ class Variable : public Castable<Variable, Node> {
/// Create a variable /// Create a variable
/// @param source the variable source /// @param source the variable source
/// @param sym the variable symbol /// @param sym the variable symbol
/// @param sc the variable storage class /// @param sc the declared storage class
/// @param type the value type /// @param type the value type
/// @param is_const true if the variable is const /// @param is_const true if the variable is const
/// @param constructor the constructor expression /// @param constructor the constructor expression
@ -107,12 +107,10 @@ class Variable : public Castable<Variable, Node> {
/// @returns the variable's type. /// @returns the variable's type.
type::Type* type() const { return type_; } type::Type* type() const { return type_; }
/// Sets the storage class /// @returns the declared storage class
/// @param sc the storage class StorageClass declared_storage_class() const {
void set_storage_class(StorageClass sc) { storage_class_ = sc; } return declared_storage_class_;
/// @returns the storage class }
StorageClass storage_class() const { return storage_class_; }
/// @returns the constructor expression or nullptr if none set /// @returns the constructor expression or nullptr if none set
Expression* constructor() const { return constructor_; } Expression* constructor() const { return constructor_; }
/// @returns true if the variable has an constructor /// @returns true if the variable has an constructor
@ -126,7 +124,7 @@ class Variable : public Castable<Variable, Node> {
/// @returns true if the decorations include a LocationDecoration /// @returns true if the decorations include a LocationDecoration
bool HasLocationDecoration() const; bool HasLocationDecoration() const;
/// @returns true if the deocrations include a BuiltinDecoration /// @returns true if the decorations include a BuiltinDecoration
bool HasBuiltinDecoration() const; bool HasBuiltinDecoration() const;
/// @returns true if the decorations include a ConstantIdDecoration /// @returns true if the decorations include a ConstantIdDecoration
bool HasConstantIdDecoration() const; bool HasConstantIdDecoration() const;
@ -182,8 +180,7 @@ class Variable : public Castable<Variable, Node> {
bool const is_const_; bool const is_const_;
Expression* const constructor_; Expression* const constructor_;
VariableDecorationList const decorations_; VariableDecorationList const decorations_;
StorageClass const declared_storage_class_;
StorageClass storage_class_ = StorageClass::kNone; // Semantic info
}; };
/// A list of variables /// A list of variables

View File

@ -30,7 +30,7 @@ TEST_F(VariableTest, Creation) {
auto* v = Var("my_var", StorageClass::kFunction, ty.i32()); auto* v = Var("my_var", StorageClass::kFunction, ty.i32());
EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->storage_class(), StorageClass::kFunction); EXPECT_EQ(v->declared_storage_class(), StorageClass::kFunction);
EXPECT_EQ(v->type(), ty.i32()); EXPECT_EQ(v->type(), ty.i32());
EXPECT_EQ(v->source().range.begin.line, 0u); EXPECT_EQ(v->source().range.begin.line, 0u);
EXPECT_EQ(v->source().range.begin.column, 0u); EXPECT_EQ(v->source().range.begin.column, 0u);
@ -44,7 +44,7 @@ TEST_F(VariableTest, CreationWithSource) {
"i", StorageClass::kPrivate, ty.f32(), nullptr, VariableDecorationList{}); "i", StorageClass::kPrivate, ty.f32(), nullptr, VariableDecorationList{});
EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->storage_class(), StorageClass::kPrivate); EXPECT_EQ(v->declared_storage_class(), StorageClass::kPrivate);
EXPECT_EQ(v->type(), ty.f32()); EXPECT_EQ(v->type(), ty.f32());
EXPECT_EQ(v->source().range.begin.line, 27u); EXPECT_EQ(v->source().range.begin.line, 27u);
EXPECT_EQ(v->source().range.begin.column, 4u); EXPECT_EQ(v->source().range.begin.column, 4u);
@ -59,7 +59,7 @@ TEST_F(VariableTest, CreationEmpty) {
VariableDecorationList{}); VariableDecorationList{});
EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->storage_class(), StorageClass::kWorkgroup); EXPECT_EQ(v->declared_storage_class(), StorageClass::kWorkgroup);
EXPECT_EQ(v->type(), ty.i32()); EXPECT_EQ(v->type(), ty.i32());
EXPECT_EQ(v->source().range.begin.line, 27u); EXPECT_EQ(v->source().range.begin.line, 27u);
EXPECT_EQ(v->source().range.begin.column, 4u); EXPECT_EQ(v->source().range.begin.column, 4u);

View File

@ -30,6 +30,7 @@
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/program.h" #include "src/program.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
#include "src/type/f32_type.h" #include "src/type/f32_type.h"
@ -66,14 +67,16 @@ std::vector<EntryPoint> Inspector::GetEntryPoints() {
entry_point.workgroup_size_z) = func->workgroup_size(); entry_point.workgroup_size_z) = func->workgroup_size();
for (auto* var : program_->Sem().Get(func)->ReferencedModuleVariables()) { for (auto* var : program_->Sem().Get(func)->ReferencedModuleVariables()) {
auto name = program_->Symbols().NameFor(var->symbol()); auto* decl = var->Declaration();
if (var->HasBuiltinDecoration()) {
auto name = program_->Symbols().NameFor(decl->symbol());
if (decl->HasBuiltinDecoration()) {
continue; continue;
} }
StageVariable stage_variable; StageVariable stage_variable;
stage_variable.name = name; stage_variable.name = name;
auto* location_decoration = var->GetLocationDecoration(); auto* location_decoration = decl->GetLocationDecoration();
if (location_decoration) { if (location_decoration) {
stage_variable.has_location_decoration = true; stage_variable.has_location_decoration = true;
stage_variable.location_decoration = location_decoration->value(); stage_variable.location_decoration = location_decoration->value();
@ -81,9 +84,9 @@ std::vector<EntryPoint> Inspector::GetEntryPoints() {
stage_variable.has_location_decoration = false; stage_variable.has_location_decoration = false;
} }
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
entry_point.input_variables.push_back(stage_variable); entry_point.input_variables.push_back(stage_variable);
} else if (var->storage_class() == ast::StorageClass::kOutput) { } else if (var->StorageClass() == ast::StorageClass::kOutput) {
entry_point.output_variables.push_back(stage_variable); entry_point.output_variables.push_back(stage_variable);
} }
} }
@ -188,14 +191,14 @@ std::vector<ResourceBinding> Inspector::GetUniformBufferResourceBindings(
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
for (auto& ruv : func_sem->ReferencedUniformVariables()) { for (auto& ruv : func_sem->ReferencedUniformVariables()) {
ResourceBinding entry; auto* var = ruv.first;
ast::Variable* var = nullptr; auto* decl = var->Declaration();
semantic::Function::BindingInfo binding_info; auto binding_info = ruv.second;
std::tie(var, binding_info) = ruv;
if (!var->type()->Is<type::AccessControl>()) { if (!decl->type()->Is<type::AccessControl>()) {
continue; continue;
} }
auto* unwrapped_type = var->type()->UnwrapIfNeeded(); auto* unwrapped_type = decl->type()->UnwrapIfNeeded();
auto* str = unwrapped_type->As<type::Struct>(); auto* str = unwrapped_type->As<type::Struct>();
if (str == nullptr) { if (str == nullptr) {
@ -206,10 +209,11 @@ std::vector<ResourceBinding> Inspector::GetUniformBufferResourceBindings(
continue; continue;
} }
ResourceBinding entry;
entry.bind_group = binding_info.group->value(); entry.bind_group = binding_info.group->value();
entry.binding = binding_info.binding->value(); entry.binding = binding_info.binding->value();
entry.min_buffer_binding_size = entry.min_buffer_binding_size =
var->type()->MinBufferBindingSize(type::MemoryLayout::kUniformBuffer); decl->type()->MinBufferBindingSize(type::MemoryLayout::kUniformBuffer);
result.push_back(entry); result.push_back(entry);
} }
@ -239,11 +243,9 @@ std::vector<ResourceBinding> Inspector::GetSamplerResourceBindings(
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
for (auto& rs : func_sem->ReferencedSamplerVariables()) { for (auto& rs : func_sem->ReferencedSamplerVariables()) {
ResourceBinding entry; auto binding_info = rs.second;
ast::Variable* var = nullptr;
semantic::Function::BindingInfo binding_info;
std::tie(var, binding_info) = rs;
ResourceBinding entry;
entry.bind_group = binding_info.group->value(); entry.bind_group = binding_info.group->value();
entry.binding = binding_info.binding->value(); entry.binding = binding_info.binding->value();
@ -264,11 +266,9 @@ std::vector<ResourceBinding> Inspector::GetComparisonSamplerResourceBindings(
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
for (auto& rcs : func_sem->ReferencedComparisonSamplerVariables()) { for (auto& rcs : func_sem->ReferencedComparisonSamplerVariables()) {
ResourceBinding entry; auto binding_info = rcs.second;
ast::Variable* var = nullptr;
semantic::Function::BindingInfo binding_info;
std::tie(var, binding_info) = rcs;
ResourceBinding entry;
entry.bind_group = binding_info.group->value(); entry.bind_group = binding_info.group->value();
entry.binding = binding_info.binding->value(); entry.binding = binding_info.binding->value();
@ -314,12 +314,11 @@ std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindingsImpl(
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
std::vector<ResourceBinding> result; std::vector<ResourceBinding> result;
for (auto& rsv : func_sem->ReferencedStoragebufferVariables()) { for (auto& rsv : func_sem->ReferencedStoragebufferVariables()) {
ResourceBinding entry; auto* var = rsv.first;
ast::Variable* var = nullptr; auto* decl = var->Declaration();
semantic::Function::BindingInfo binding_info; auto binding_info = rsv.second;
std::tie(var, binding_info) = rsv;
auto* ac_type = var->type()->As<type::AccessControl>(); auto* ac_type = decl->type()->As<type::AccessControl>();
if (ac_type == nullptr) { if (ac_type == nullptr) {
continue; continue;
} }
@ -328,14 +327,15 @@ std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindingsImpl(
continue; continue;
} }
if (!var->type()->UnwrapIfNeeded()->Is<type::Struct>()) { if (!decl->type()->UnwrapIfNeeded()->Is<type::Struct>()) {
continue; continue;
} }
ResourceBinding entry;
entry.bind_group = binding_info.group->value(); entry.bind_group = binding_info.group->value();
entry.binding = binding_info.binding->value(); entry.binding = binding_info.binding->value();
entry.min_buffer_binding_size = entry.min_buffer_binding_size =
var->type()->MinBufferBindingSize(type::MemoryLayout::kStorageBuffer); decl->type()->MinBufferBindingSize(type::MemoryLayout::kStorageBuffer);
result.push_back(entry); result.push_back(entry);
} }
@ -357,15 +357,15 @@ std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindingsImpl(
multisampled_only ? func_sem->ReferencedMultisampledTextureVariables() multisampled_only ? func_sem->ReferencedMultisampledTextureVariables()
: func_sem->ReferencedSampledTextureVariables(); : func_sem->ReferencedSampledTextureVariables();
for (auto& ref : referenced_variables) { for (auto& ref : referenced_variables) {
ResourceBinding entry; auto* var = ref.first;
ast::Variable* var = nullptr; auto* decl = var->Declaration();
semantic::Function::BindingInfo binding_info; auto binding_info = ref.second;
std::tie(var, binding_info) = ref;
ResourceBinding entry;
entry.bind_group = binding_info.group->value(); entry.bind_group = binding_info.group->value();
entry.binding = binding_info.binding->value(); entry.binding = binding_info.binding->value();
auto* texture_type = var->type()->UnwrapIfNeeded()->As<type::Texture>(); auto* texture_type = decl->type()->UnwrapIfNeeded()->As<type::Texture>();
switch (texture_type->dim()) { switch (texture_type->dim()) {
case type::TextureDimension::k1d: case type::TextureDimension::k1d:
entry.dim = ResourceBinding::TextureDimension::k1d; entry.dim = ResourceBinding::TextureDimension::k1d;

View File

@ -38,7 +38,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithoutConstructor) {
EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a")); EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a"));
EXPECT_TRUE(e->type()->Is<type::F32>()); EXPECT_TRUE(e->type()->Is<type::F32>());
EXPECT_EQ(e->storage_class(), ast::StorageClass::kOutput); EXPECT_EQ(e->declared_storage_class(), ast::StorageClass::kOutput);
EXPECT_EQ(e->source().range.begin.line, 1u); EXPECT_EQ(e->source().range.begin.line, 1u);
EXPECT_EQ(e->source().range.begin.column, 10u); EXPECT_EQ(e->source().range.begin.column, 10u);
@ -61,7 +61,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithConstructor) {
EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a")); EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a"));
EXPECT_TRUE(e->type()->Is<type::F32>()); EXPECT_TRUE(e->type()->Is<type::F32>());
EXPECT_EQ(e->storage_class(), ast::StorageClass::kOutput); EXPECT_EQ(e->declared_storage_class(), ast::StorageClass::kOutput);
EXPECT_EQ(e->source().range.begin.line, 1u); EXPECT_EQ(e->source().range.begin.line, 1u);
EXPECT_EQ(e->source().range.begin.column, 10u); EXPECT_EQ(e->source().range.begin.column, 10u);
@ -87,7 +87,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) {
EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a")); EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a"));
ASSERT_NE(e->type(), nullptr); ASSERT_NE(e->type(), nullptr);
EXPECT_TRUE(e->type()->Is<type::F32>()); EXPECT_TRUE(e->type()->Is<type::F32>());
EXPECT_EQ(e->storage_class(), ast::StorageClass::kOutput); EXPECT_EQ(e->declared_storage_class(), ast::StorageClass::kOutput);
EXPECT_EQ(e->source().range.begin.line, 1u); EXPECT_EQ(e->source().range.begin.line, 1u);
EXPECT_EQ(e->source().range.begin.column, 35u); EXPECT_EQ(e->source().range.begin.column, 35u);
@ -117,7 +117,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration_MulitpleGroups) {
EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a")); EXPECT_EQ(e->symbol(), p->builder().Symbols().Get("a"));
ASSERT_NE(e->type(), nullptr); ASSERT_NE(e->type(), nullptr);
EXPECT_TRUE(e->type()->Is<type::F32>()); EXPECT_TRUE(e->type()->Is<type::F32>());
EXPECT_EQ(e->storage_class(), ast::StorageClass::kOutput); EXPECT_EQ(e->declared_storage_class(), ast::StorageClass::kOutput);
EXPECT_EQ(e->source().range.begin.line, 1u); EXPECT_EQ(e->source().range.begin.line, 1u);
EXPECT_EQ(e->source().range.begin.column, 38u); EXPECT_EQ(e->source().range.begin.column, 38u);

View File

@ -28,7 +28,6 @@ namespace tint {
namespace ast { namespace ast {
class BindingDecoration; class BindingDecoration;
class GroupDecoration; class GroupDecoration;
class Variable;
class LocationDecoration; class LocationDecoration;
class BuiltinDecoration; class BuiltinDecoration;
} // namespace ast } // namespace ast
@ -38,6 +37,8 @@ class Type;
namespace semantic { namespace semantic {
class Variable;
/// Function holds the semantic information for function nodes. /// Function holds the semantic information for function nodes.
class Function : public Castable<Function, Node> { class Function : public Castable<Function, Node> {
public: public:
@ -54,8 +55,8 @@ class Function : public Castable<Function, Node> {
/// @param local_referenced_module_vars the locally referenced module /// @param local_referenced_module_vars the locally referenced module
/// variables /// variables
/// @param ancestor_entry_points the ancestor entry points /// @param ancestor_entry_points the ancestor entry points
explicit Function(std::vector<ast::Variable*> referenced_module_vars, explicit Function(std::vector<const Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars, std::vector<const Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points); std::vector<Symbol> ancestor_entry_points);
/// Destructor /// Destructor
@ -64,11 +65,11 @@ class Function : public Castable<Function, Node> {
/// Note: If this function calls other functions, the return will also include /// Note: If this function calls other functions, the return will also include
/// all of the referenced variables from the callees. /// all of the referenced variables from the callees.
/// @returns the referenced module variables /// @returns the referenced module variables
const std::vector<ast::Variable*>& ReferencedModuleVariables() const { const std::vector<const Variable*>& ReferencedModuleVariables() const {
return referenced_module_vars_; return referenced_module_vars_;
} }
/// @returns the locally referenced module variables /// @returns the locally referenced module variables
const std::vector<ast::Variable*>& LocalReferencedModuleVariables() const { const std::vector<const Variable*>& LocalReferencedModuleVariables() const {
return local_referenced_module_vars_; return local_referenced_module_vars_;
} }
/// @returns the ancestor entry points /// @returns the ancestor entry points
@ -77,53 +78,53 @@ class Function : public Castable<Function, Node> {
} }
/// Retrieves any referenced location variables /// Retrieves any referenced location variables
/// @returns the <variable, decoration> pair. /// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>> const std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
ReferencedLocationVariables() const; ReferencedLocationVariables() const;
/// Retrieves any referenced builtin variables /// Retrieves any referenced builtin variables
/// @returns the <variable, decoration> pair. /// @returns the <variable, decoration> pair.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
ReferencedBuiltinVariables() const; ReferencedBuiltinVariables() const;
/// Retrieves any referenced uniform variables. Note, the variables must be /// Retrieves any referenced uniform variables. Note, the variables must be
/// decorated with both binding and group decorations. /// decorated with both binding and group decorations.
/// @returns the referenced uniforms /// @returns the referenced uniforms
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedUniformVariables() const; ReferencedUniformVariables() const;
/// Retrieves any referenced storagebuffer variables. Note, the variables /// Retrieves any referenced storagebuffer variables. Note, the variables
/// must be decorated with both binding and group decorations. /// must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers /// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedStoragebufferVariables() const; ReferencedStoragebufferVariables() const;
/// Retrieves any referenced regular Sampler variables. Note, the /// Retrieves any referenced regular Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations. /// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers /// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSamplerVariables() const; ReferencedSamplerVariables() const;
/// Retrieves any referenced comparison Sampler variables. Note, the /// Retrieves any referenced comparison Sampler variables. Note, the
/// variables must be decorated with both binding and group decorations. /// variables must be decorated with both binding and group decorations.
/// @returns the referenced storagebuffers /// @returns the referenced storagebuffers
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedComparisonSamplerVariables() const; ReferencedComparisonSamplerVariables() const;
/// Retrieves any referenced sampled textures variables. Note, the /// Retrieves any referenced sampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations. /// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures /// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSampledTextureVariables() const; ReferencedSampledTextureVariables() const;
/// Retrieves any referenced multisampled textures variables. Note, the /// Retrieves any referenced multisampled textures variables. Note, the
/// variables must be decorated with both binding and group decorations. /// variables must be decorated with both binding and group decorations.
/// @returns the referenced sampled textures /// @returns the referenced sampled textures
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedMultisampledTextureVariables() const; ReferencedMultisampledTextureVariables() const;
/// Retrieves any locally referenced builtin variables /// Retrieves any locally referenced builtin variables
/// @returns the <variable, decoration> pairs. /// @returns the <variable, decoration> pairs.
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
LocalReferencedBuiltinVariables() const; LocalReferencedBuiltinVariables() const;
/// Checks if the given entry point is an ancestor /// Checks if the given entry point is an ancestor
@ -132,13 +133,13 @@ class Function : public Castable<Function, Node> {
bool HasAncestorEntryPoint(Symbol sym) const; bool HasAncestorEntryPoint(Symbol sym) const;
private: private:
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSamplerVariablesImpl(type::SamplerKind kind) const; ReferencedSamplerVariablesImpl(type::SamplerKind kind) const;
const std::vector<std::pair<ast::Variable*, BindingInfo>> const std::vector<std::pair<const Variable*, BindingInfo>>
ReferencedSampledTextureVariablesImpl(bool multisampled) const; ReferencedSampledTextureVariablesImpl(bool multisampled) const;
std::vector<ast::Variable*> const referenced_module_vars_; std::vector<const Variable*> const referenced_module_vars_;
std::vector<ast::Variable*> const local_referenced_module_vars_; std::vector<const Variable*> const local_referenced_module_vars_;
std::vector<Symbol> const ancestor_entry_points_; std::vector<Symbol> const ancestor_entry_points_;
}; };

View File

@ -20,6 +20,7 @@
#include "src/ast/location_decoration.h" #include "src/ast/location_decoration.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/ast/variable_decoration.h" #include "src/ast/variable_decoration.h"
#include "src/semantic/variable.h"
#include "src/type/multisampled_texture_type.h" #include "src/type/multisampled_texture_type.h"
#include "src/type/sampled_texture_type.h" #include "src/type/sampled_texture_type.h"
#include "src/type/texture_type.h" #include "src/type/texture_type.h"
@ -29,8 +30,8 @@ TINT_INSTANTIATE_CLASS_ID(tint::semantic::Function);
namespace tint { namespace tint {
namespace semantic { namespace semantic {
Function::Function(std::vector<ast::Variable*> referenced_module_vars, Function::Function(std::vector<const Variable*> referenced_module_vars,
std::vector<ast::Variable*> local_referenced_module_vars, std::vector<const Variable*> local_referenced_module_vars,
std::vector<Symbol> ancestor_entry_points) std::vector<Symbol> ancestor_entry_points)
: referenced_module_vars_(std::move(referenced_module_vars)), : referenced_module_vars_(std::move(referenced_module_vars)),
local_referenced_module_vars_(std::move(local_referenced_module_vars)), local_referenced_module_vars_(std::move(local_referenced_module_vars)),
@ -38,12 +39,12 @@ Function::Function(std::vector<ast::Variable*> referenced_module_vars,
Function::~Function() = default; Function::~Function() = default;
const std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>> const std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
Function::ReferencedLocationVariables() const { Function::ReferencedLocationVariables() const {
std::vector<std::pair<ast::Variable*, ast::LocationDecoration*>> ret; std::vector<std::pair<const Variable*, ast::LocationDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* location = deco->As<ast::LocationDecoration>()) { if (auto* location = deco->As<ast::LocationDecoration>()) {
ret.push_back({var, location}); ret.push_back({var, location});
break; break;
@ -53,18 +54,18 @@ Function::ReferencedLocationVariables() const {
return ret; return ret;
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedUniformVariables() const { Function::ReferencedUniformVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret; std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kUniform) { if (var->StorageClass() != ast::StorageClass::kUniform) {
continue; continue;
} }
ast::BindingDecoration* binding = nullptr; ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr; ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) { if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b; binding = b;
} else if (auto* g = deco->As<ast::GroupDecoration>()) { } else if (auto* g = deco->As<ast::GroupDecoration>()) {
@ -80,18 +81,18 @@ Function::ReferencedUniformVariables() const {
return ret; return ret;
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedStoragebufferVariables() const { Function::ReferencedStoragebufferVariables() const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret; std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
if (var->storage_class() != ast::StorageClass::kStorage) { if (var->StorageClass() != ast::StorageClass::kStorage) {
continue; continue;
} }
ast::BindingDecoration* binding = nullptr; ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr; ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) { if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b; binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) { } else if (auto* s = deco->As<ast::GroupDecoration>()) {
@ -107,12 +108,12 @@ Function::ReferencedStoragebufferVariables() const {
return ret; return ret;
} }
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Function::ReferencedBuiltinVariables() const { Function::ReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret; std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin}); ret.push_back({var, builtin});
break; break;
@ -122,32 +123,32 @@ Function::ReferencedBuiltinVariables() const {
return ret; return ret;
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariables() const { Function::ReferencedSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler); return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler);
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedComparisonSamplerVariables() const { Function::ReferencedComparisonSamplerVariables() const {
return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler); return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler);
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariables() const { Function::ReferencedSampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(false); return ReferencedSampledTextureVariablesImpl(false);
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedMultisampledTextureVariables() const { Function::ReferencedMultisampledTextureVariables() const {
return ReferencedSampledTextureVariablesImpl(true); return ReferencedSampledTextureVariablesImpl(true);
} }
const std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> const std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
Function::LocalReferencedBuiltinVariables() const { Function::LocalReferencedBuiltinVariables() const {
std::vector<std::pair<ast::Variable*, ast::BuiltinDecoration*>> ret; std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> ret;
for (auto* var : LocalReferencedModuleVariables()) { for (auto* var : LocalReferencedModuleVariables()) {
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) { if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
ret.push_back({var, builtin}); ret.push_back({var, builtin});
break; break;
@ -166,12 +167,12 @@ bool Function::HasAncestorEntryPoint(Symbol symbol) const {
return false; return false;
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const { Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret; std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded(); auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* sampler = unwrapped_type->As<type::Sampler>(); auto* sampler = unwrapped_type->As<type::Sampler>();
if (sampler == nullptr || sampler->kind() != kind) { if (sampler == nullptr || sampler->kind() != kind) {
continue; continue;
@ -179,7 +180,7 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
ast::BindingDecoration* binding = nullptr; ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr; ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) { if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b; binding = b;
} }
@ -196,12 +197,12 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const {
return ret; return ret;
} }
const std::vector<std::pair<ast::Variable*, Function::BindingInfo>> const std::vector<std::pair<const Variable*, Function::BindingInfo>>
Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const { Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
std::vector<std::pair<ast::Variable*, Function::BindingInfo>> ret; std::vector<std::pair<const Variable*, Function::BindingInfo>> ret;
for (auto* var : ReferencedModuleVariables()) { for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->type()->UnwrapIfNeeded(); auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded();
auto* texture = unwrapped_type->As<type::Texture>(); auto* texture = unwrapped_type->As<type::Texture>();
if (texture == nullptr) { if (texture == nullptr) {
continue; continue;
@ -216,7 +217,7 @@ Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
ast::BindingDecoration* binding = nullptr; ast::BindingDecoration* binding = nullptr;
ast::GroupDecoration* group = nullptr; ast::GroupDecoration* group = nullptr;
for (auto* deco : var->decorations()) { for (auto* deco : var->Declaration()->decorations()) {
if (auto* b = deco->As<ast::BindingDecoration>()) { if (auto* b = deco->As<ast::BindingDecoration>()) {
binding = b; binding = b;
} else if (auto* s = deco->As<ast::GroupDecoration>()) { } else if (auto* s = deco->As<ast::GroupDecoration>()) {

View File

@ -0,0 +1,28 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/semantic/variable.h"
TINT_INSTANTIATE_CLASS_ID(tint::semantic::Variable);
namespace tint {
namespace semantic {
Variable::Variable(ast::Variable* declaration, ast::StorageClass storage_class)
: declaration_(declaration), storage_class_(storage_class) {}
Variable::~Variable() = default;
} // namespace semantic
} // namespace tint

View File

@ -24,6 +24,7 @@ namespace ast {
class Expression; class Expression;
class Function; class Function;
class Variable;
} // namespace ast } // namespace ast
@ -31,6 +32,7 @@ namespace semantic {
class Expression; class Expression;
class Function; class Function;
class Variable;
/// TypeMappings is a struct that holds dummy `operator()` methods that's used /// TypeMappings is a struct that holds dummy `operator()` methods that's used
/// by SemanticNodeTypeFor to map AST node types to their corresponding semantic /// by SemanticNodeTypeFor to map AST node types to their corresponding semantic
@ -41,6 +43,7 @@ struct TypeMappings {
//! @cond Doxygen_Suppress //! @cond Doxygen_Suppress
semantic::Expression* operator()(ast::Expression*); semantic::Expression* operator()(ast::Expression*);
semantic::Function* operator()(ast::Function*); semantic::Function* operator()(ast::Function*);
semantic::Variable* operator()(ast::Variable*);
//! @endcond //! @endcond
}; };

64
src/semantic/variable.h Normal file
View File

@ -0,0 +1,64 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_SEMANTIC_VARIABLE_H_
#define SRC_SEMANTIC_VARIABLE_H_
#include <utility>
#include <vector>
#include "src/ast/storage_class.h"
#include "src/semantic/node.h"
#include "src/type/sampler_type.h"
namespace tint {
// Forward declarations
namespace ast {
class Variable;
} // namespace ast
namespace type {
class Type;
} // namespace type
namespace semantic {
/// Variable holds the semantic information for variables.
class Variable : public Castable<Variable, Node> {
public:
/// Constructor
/// @param declaration the AST declaration node
/// @param storage_class the variable storage class
explicit Variable(ast::Variable* declaration,
ast::StorageClass storage_class);
/// Destructor
~Variable() override;
/// @returns the AST declaration node
ast::Variable* Declaration() const { return declaration_; }
/// @returns the storage class for the variable
ast::StorageClass StorageClass() const { return storage_class_; }
private:
ast::Variable* const declaration_;
ast::StorageClass const storage_class_;
};
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_VARIABLE_H_

View File

@ -58,7 +58,6 @@ Transform::Output EmitVertexPointSize::Run(const Program* in) {
false, // is_const false, // is_const
nullptr, // constructor nullptr, // constructor
ast::VariableDecorationList{ ast::VariableDecorationList{
// decorations
out.create<ast::BuiltinDecoration>(Source{}, out.create<ast::BuiltinDecoration>(Source{},
ast::Builtin::kPointSize), ast::Builtin::kPointSize),
}); });

View File

@ -70,7 +70,7 @@ ast::Variable* clone_variable_with_new_name(CloneContext* ctx,
return ctx->dst->create<ast::Variable>( return ctx->dst->create<ast::Variable>(
ctx->Clone(in->source()), // source ctx->Clone(in->source()), // source
ctx->dst->Symbols().Register(new_name), // symbol ctx->dst->Symbols().Register(new_name), // symbol
in->storage_class(), // storage_class in->declared_storage_class(), // declared_storage_class
ctx->Clone(in->type()), // type ctx->Clone(in->type()), // type
in->is_const(), // is_const in->is_const(), // is_const
ctx->Clone(in->constructor()), // constructor ctx->Clone(in->constructor()), // constructor
@ -226,7 +226,7 @@ ast::Variable* FirstIndexOffset::AddUniformBuffer(ProgramBuilder* dst) {
ast::VariableDecorationList{ ast::VariableDecorationList{
dst->create<ast::BindingDecoration>(Source{}, binding_), dst->create<ast::BindingDecoration>(Source{}, binding_),
dst->create<ast::GroupDecoration>(Source{}, group_), dst->create<ast::GroupDecoration>(Source{}, group_),
}); // decorations });
dst->AST().AddGlobalVariable(idx_var); dst->AST().AddGlobalVariable(idx_var);

View File

@ -35,6 +35,7 @@
#include "src/clone_context.h" #include "src/clone_context.h"
#include "src/program.h" #include "src/program.h"
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/semantic/variable.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
#include "src/type/f32_type.h" #include "src/type/f32_type.h"
#include "src/type/i32_type.h" #include "src/type/i32_type.h"
@ -150,7 +151,8 @@ void VertexPulling::State::FindOrInsertVertexIndexIfUsed() {
// Look for an existing vertex index builtin // Look for an existing vertex index builtin
for (auto* v : ctx.src->AST().GlobalVariables()) { for (auto* v : ctx.src->AST().GlobalVariables()) {
if (v->storage_class() != ast::StorageClass::kInput) { auto* sem = ctx.src->Sem().Get(v);
if (sem->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }
@ -196,7 +198,8 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() {
// Look for an existing instance index builtin // Look for an existing instance index builtin
for (auto* v : ctx.src->AST().GlobalVariables()) { for (auto* v : ctx.src->AST().GlobalVariables()) {
if (v->storage_class() != ast::StorageClass::kInput) { auto* sem = ctx.src->Sem().Get(v);
if (sem->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }
@ -229,7 +232,8 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() {
void VertexPulling::State::ConvertVertexInputVariablesToPrivate() { void VertexPulling::State::ConvertVertexInputVariablesToPrivate() {
for (auto* v : ctx.src->AST().GlobalVariables()) { for (auto* v : ctx.src->AST().GlobalVariables()) {
if (v->storage_class() != ast::StorageClass::kInput) { auto* sem = ctx.src->Sem().Get(v);
if (sem->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }

View File

@ -45,6 +45,7 @@
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
#include "src/type/bool_type.h" #include "src/type/bool_type.h"
#include "src/type/depth_texture_type.h" #include "src/type/depth_texture_type.h"
@ -88,13 +89,13 @@ void TypeDeterminer::set_error(const Source& src, const std::string& msg) {
error_ += msg; error_ += msg;
} }
void TypeDeterminer::set_referenced_from_function_if_needed(ast::Variable* var, void TypeDeterminer::set_referenced_from_function_if_needed(VariableInfo* var,
bool local) { bool local) {
if (current_function_ == nullptr) { if (current_function_ == nullptr) {
return; return;
} }
if (var->storage_class() == ast::StorageClass::kNone || if (var->storage_class == ast::StorageClass::kNone ||
var->storage_class() == ast::StorageClass::kFunction) { var->storage_class == ast::StorageClass::kFunction) {
return; return;
} }
@ -105,7 +106,18 @@ void TypeDeterminer::set_referenced_from_function_if_needed(ast::Variable* var,
} }
bool TypeDeterminer::Determine() { bool TypeDeterminer::Determine() {
bool result = DetermineInternal();
// Even if resolving failed, create all the semantic nodes for information we
// did generate.
CreateSemanticNodes();
return result;
}
bool TypeDeterminer::DetermineInternal() {
std::vector<type::StorageTexture*> storage_textures; std::vector<type::StorageTexture*> storage_textures;
for (auto& it : builder_->Types().types()) { for (auto& it : builder_->Types().types()) {
if (auto* storage = if (auto* storage =
it.second->UnwrapIfNeeded()->As<type::StorageTexture>()) { it.second->UnwrapIfNeeded()->As<type::StorageTexture>()) {
@ -122,7 +134,7 @@ bool TypeDeterminer::Determine() {
} }
for (auto* var : builder_->AST().GlobalVariables()) { for (auto* var : builder_->AST().GlobalVariables()) {
variable_stack_.set_global(var->symbol(), var); variable_stack_.set_global(var->symbol(), CreateVariableInfo(var));
if (var->has_constructor()) { if (var->has_constructor()) {
if (!DetermineResultType(var->constructor())) { if (!DetermineResultType(var->constructor())) {
@ -135,8 +147,8 @@ bool TypeDeterminer::Determine() {
return false; return false;
} }
// Walk over the caller to callee information and update functions with which // Walk over the caller to callee information and update functions with
// entry points call those functions. // which entry points call those functions.
for (auto* func : builder_->AST().Functions()) { for (auto* func : builder_->AST().Functions()) {
if (!func->IsEntryPoint()) { if (!func->IsEntryPoint()) {
continue; continue;
@ -146,8 +158,6 @@ bool TypeDeterminer::Determine() {
} }
} }
CreateSemanticFunctions();
return true; return true;
} }
@ -170,15 +180,13 @@ bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) {
} }
bool TypeDeterminer::DetermineFunction(ast::Function* func) { bool TypeDeterminer::DetermineFunction(ast::Function* func) {
auto* info = function_infos_.Create<FunctionInfo>(func); current_function_ = function_infos_.Create<FunctionInfo>(func);
symbol_to_function_[func->symbol()] = info; symbol_to_function_[func->symbol()] = current_function_;
function_to_info_.emplace(func, info); function_to_info_.emplace(func, current_function_);
current_function_ = info;
variable_stack_.push_scope(); variable_stack_.push_scope();
for (auto* param : func->params()) { for (auto* param : func->params()) {
variable_stack_.set(param->symbol(), param); variable_stack_.set(param->symbol(), CreateVariableInfo(param));
} }
if (!DetermineStatements(func->body())) { if (!DetermineStatements(func->body())) {
@ -211,22 +219,26 @@ bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
} }
auto* var = var_decl->variable(); auto* var = var_decl->variable();
auto* info = CreateVariableInfo(var);
variable_to_info_.emplace(var, info);
// Nothing to do for const // Nothing to do for const
if (var->is_const()) { if (var->is_const()) {
return true; return true;
} }
if (var->storage_class() == ast::StorageClass::kFunction) { if (info->storage_class == ast::StorageClass::kFunction) {
return true; return true;
} }
if (var->storage_class() != ast::StorageClass::kNone) { if (info->storage_class != ast::StorageClass::kNone) {
set_error(stmt->source(), set_error(stmt->source(),
"function variable has a non-function storage class"); "function variable has a non-function storage class");
return false; return false;
} }
var->set_storage_class(ast::StorageClass::kFunction); info->storage_class = ast::StorageClass::kFunction;
return true; return true;
} }
@ -291,7 +303,8 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
return true; return true;
} }
if (auto* v = stmt->As<ast::VariableDeclStatement>()) { if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
variable_stack_.set(v->variable()->symbol(), v->variable()); variable_stack_.set(v->variable()->symbol(),
variable_to_info_.at(v->variable()));
return DetermineResultType(v->variable()->constructor()); return DetermineResultType(v->variable()->constructor());
} }
@ -816,17 +829,17 @@ bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) { bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) {
auto symbol = expr->symbol(); auto symbol = expr->symbol();
ast::Variable* var; VariableInfo* var;
if (variable_stack_.get(symbol, &var)) { if (variable_stack_.get(symbol, &var)) {
// A constant is the type, but a variable is always a pointer so synthesize // A constant is the type, but a variable is always a pointer so synthesize
// the pointer around the variable type. // the pointer around the variable type.
if (var->is_const()) { if (var->declaration->is_const()) {
SetType(expr, var->type()); SetType(expr, var->declaration->type());
} else if (var->type()->Is<type::Pointer>()) { } else if (var->declaration->type()->Is<type::Pointer>()) {
SetType(expr, var->type()); SetType(expr, var->declaration->type());
} else { } else {
SetType(expr, builder_->create<type::Pointer>(var->type(), SetType(expr, builder_->create<type::Pointer>(var->declaration->type(),
var->storage_class())); var->storage_class));
} }
set_referenced_from_function_if_needed(var, true); set_referenced_from_function_if_needed(var, true);
@ -1146,6 +1159,13 @@ bool TypeDeterminer::DetermineUnaryOp(ast::UnaryOpExpression* expr) {
return true; return true;
} }
TypeDeterminer::VariableInfo* TypeDeterminer::CreateVariableInfo(
ast::Variable* var) {
auto* info = variable_infos_.Create(var);
variable_to_info_.emplace(var, info);
return info;
}
bool TypeDeterminer::DetermineStorageTextureSubtype(type::StorageTexture* tex) { bool TypeDeterminer::DetermineStorageTextureSubtype(type::StorageTexture* tex) {
if (tex->type() != nullptr) { if (tex->type() != nullptr) {
return true; return true;
@ -1211,22 +1231,40 @@ void TypeDeterminer::SetType(ast::Expression* expr, type::Type* type) const {
builder_->create<semantic::Expression>(type)); builder_->create<semantic::Expression>(type));
} }
void TypeDeterminer::CreateSemanticFunctions() const { void TypeDeterminer::CreateSemanticNodes() const {
auto& sem = builder_->Sem();
for (auto it : variable_to_info_) {
auto* var = it.first;
auto* info = it.second;
sem.Add(var,
builder_->create<semantic::Variable>(var, info->storage_class));
}
auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) {
std::vector<const semantic::Variable*> out;
out.reserve(in.size());
for (auto* info : in) {
out.emplace_back(sem.Get(info->declaration));
}
return out;
};
for (auto it : function_to_info_) { for (auto it : function_to_info_) {
auto* func = it.first; auto* func = it.first;
auto* info = it.second; auto* info = it.second;
if (builder_->Sem().Get(func)) { sem.Add(func, builder_->create<semantic::Function>(
// ast::Function already has a semantic::Function node. remap_vars(info->referenced_module_vars),
// This is likely via explicit call to DetermineXXX() in test. remap_vars(info->local_referenced_module_vars),
continue; info->ancestor_entry_points));
}
builder_->Sem().Add(func, builder_->create<semantic::Function>(
info->referenced_module_vars,
info->local_referenced_module_vars,
info->ancestor_entry_points));
} }
} }
TypeDeterminer::VariableInfo::VariableInfo(ast::Variable* decl)
: declaration(decl), storage_class(decl->declared_storage_class()) {}
TypeDeterminer::VariableInfo::~VariableInfo() = default;
TypeDeterminer::FunctionInfo::FunctionInfo(ast::Function* decl) TypeDeterminer::FunctionInfo::FunctionInfo(ast::Function* decl)
: declaration(decl) {} : declaration(decl) {}

View File

@ -66,23 +66,6 @@ class TypeDeterminer {
/// @returns true if the type determiner was successful /// @returns true if the type determiner was successful
bool Determine(); bool Determine();
/// Creates the semantic::Function nodes and adds them to the semantic::Info
/// of the ProgramBuilder.
void CreateSemanticFunctions() const;
/// Retrieves information for the requested import.
/// @param src the source of the import
/// @param path the import path
/// @param name the method name to get information on
/// @param params the parameters to the method call
/// @param id out parameter for the external call ID. Must not be a nullptr.
/// @returns the return type of `name` in `path` or nullptr on error.
type::Type* GetImportData(const Source& src,
const std::string& path,
const std::string& name,
const ast::ExpressionList& params,
uint32_t* id);
/// Sets the intrinsic data information for the identifier if needed /// Sets the intrinsic data information for the identifier if needed
/// @param ident the identifier expression /// @param ident the identifier expression
/// @returns true if an intrinsic was set /// @returns true if an intrinsic was set
@ -99,6 +82,7 @@ class TypeDeterminer {
set.emplace(val); set.emplace(val);
} }
} }
size_t size() const { return vector.size(); }
ConstIterator begin() const { return vector.begin(); } ConstIterator begin() const { return vector.begin(); }
ConstIterator end() const { return vector.end(); } ConstIterator end() const { return vector.end(); }
operator const std::vector<T> &() const { return vector; } operator const std::vector<T> &() const { return vector; }
@ -108,6 +92,16 @@ class TypeDeterminer {
std::unordered_set<T> set; std::unordered_set<T> set;
}; };
/// Structure holding semantic information about a function.
/// Used to build the semantic::Function nodes at the end of resolving.
struct VariableInfo {
explicit VariableInfo(ast::Variable* decl);
~VariableInfo();
ast::Variable* const declaration;
ast::StorageClass storage_class;
};
/// Structure holding semantic information about a function. /// Structure holding semantic information about a function.
/// Used to build the semantic::Function nodes at the end of resolving. /// Used to build the semantic::Function nodes at the end of resolving.
struct FunctionInfo { struct FunctionInfo {
@ -115,11 +109,16 @@ class TypeDeterminer {
~FunctionInfo(); ~FunctionInfo();
ast::Function* const declaration; ast::Function* const declaration;
UniqueVector<ast::Variable*> referenced_module_vars; UniqueVector<VariableInfo*> referenced_module_vars;
UniqueVector<ast::Variable*> local_referenced_module_vars; UniqueVector<VariableInfo*> local_referenced_module_vars;
UniqueVector<Symbol> ancestor_entry_points; UniqueVector<Symbol> ancestor_entry_points;
}; };
/// Determines type information for the program, without creating final the
/// semantic nodes.
/// @returns true if the determination was successful
bool DetermineInternal();
/// Determines type information for functions /// Determines type information for functions
/// @param funcs the functions to check /// @param funcs the functions to check
/// @returns true if the determination was successful /// @returns true if the determination was successful
@ -154,8 +153,25 @@ class TypeDeterminer {
/// @returns false on error /// @returns false on error
bool DetermineStorageTextureSubtype(type::StorageTexture* tex); bool DetermineStorageTextureSubtype(type::StorageTexture* tex);
/// Creates the nodes and adds them to the semantic::Info mappings of the
/// ProgramBuilder.
void CreateSemanticNodes() const;
/// Retrieves information for the requested import.
/// @param src the source of the import
/// @param path the import path
/// @param name the method name to get information on
/// @param params the parameters to the method call
/// @param id out parameter for the external call ID. Must not be a nullptr.
/// @returns the return type of `name` in `path` or nullptr on error.
type::Type* GetImportData(const Source& src,
const std::string& path,
const std::string& name,
const ast::ExpressionList& params,
uint32_t* id);
void set_error(const Source& src, const std::string& msg); void set_error(const Source& src, const std::string& msg);
void set_referenced_from_function_if_needed(ast::Variable* var, bool local); void set_referenced_from_function_if_needed(VariableInfo* var, bool local);
void set_entry_points(const Symbol& fn_sym, Symbol ep_sym); void set_entry_points(const Symbol& fn_sym, Symbol ep_sym);
bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr); bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr);
@ -169,6 +185,8 @@ class TypeDeterminer {
bool DetermineMemberAccessor(ast::MemberAccessorExpression* expr); bool DetermineMemberAccessor(ast::MemberAccessorExpression* expr);
bool DetermineUnaryOp(ast::UnaryOpExpression* expr); bool DetermineUnaryOp(ast::UnaryOpExpression* expr);
VariableInfo* CreateVariableInfo(ast::Variable*);
/// @returns the resolved type of the ast::Expression `expr` /// @returns the resolved type of the ast::Expression `expr`
/// @param expr the expression /// @param expr the expression
type::Type* TypeOf(ast::Expression* expr) const { type::Type* TypeOf(ast::Expression* expr) const {
@ -183,10 +201,12 @@ class TypeDeterminer {
ProgramBuilder* builder_; ProgramBuilder* builder_;
std::string error_; std::string error_;
ScopeStack<ast::Variable*> variable_stack_; ScopeStack<VariableInfo*> variable_stack_;
std::unordered_map<Symbol, FunctionInfo*> symbol_to_function_; std::unordered_map<Symbol, FunctionInfo*> symbol_to_function_;
std::unordered_map<ast::Function*, FunctionInfo*> function_to_info_; std::unordered_map<ast::Function*, FunctionInfo*> function_to_info_;
std::unordered_map<ast::Variable*, VariableInfo*> variable_to_info_;
FunctionInfo* current_function_ = nullptr; FunctionInfo* current_function_ = nullptr;
BlockAllocator<VariableInfo> variable_infos_;
BlockAllocator<FunctionInfo> function_infos_; BlockAllocator<FunctionInfo> function_infos_;
// Map from caller functions to callee functions. // Map from caller functions to callee functions.

View File

@ -53,6 +53,7 @@
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
#include "src/type/bool_type.h" #include "src/type/bool_type.h"
@ -689,11 +690,11 @@ TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables) {
const auto& vars = func_sem->ReferencedModuleVariables(); const auto& vars = func_sem->ReferencedModuleVariables();
ASSERT_EQ(vars.size(), 5u); ASSERT_EQ(vars.size(), 5u);
EXPECT_EQ(vars[0], out_var); EXPECT_EQ(vars[0]->Declaration(), out_var);
EXPECT_EQ(vars[1], in_var); EXPECT_EQ(vars[1]->Declaration(), in_var);
EXPECT_EQ(vars[2], wg_var); EXPECT_EQ(vars[2]->Declaration(), wg_var);
EXPECT_EQ(vars[3], sb_var); EXPECT_EQ(vars[3]->Declaration(), sb_var);
EXPECT_EQ(vars[4], priv_var); EXPECT_EQ(vars[4]->Declaration(), priv_var);
} }
TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables_SubFunction) { TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables_SubFunction) {
@ -726,11 +727,11 @@ TEST_F(TypeDeterminerTest, Function_RegisterInputOutputVariables_SubFunction) {
const auto& vars = func2_sem->ReferencedModuleVariables(); const auto& vars = func2_sem->ReferencedModuleVariables();
ASSERT_EQ(vars.size(), 5u); ASSERT_EQ(vars.size(), 5u);
EXPECT_EQ(vars[0], out_var); EXPECT_EQ(vars[0]->Declaration(), out_var);
EXPECT_EQ(vars[1], in_var); EXPECT_EQ(vars[1]->Declaration(), in_var);
EXPECT_EQ(vars[2], wg_var); EXPECT_EQ(vars[2]->Declaration(), wg_var);
EXPECT_EQ(vars[3], sb_var); EXPECT_EQ(vars[3]->Declaration(), sb_var);
EXPECT_EQ(vars[4], priv_var); EXPECT_EQ(vars[4]->Declaration(), priv_var);
} }
TEST_F(TypeDeterminerTest, Function_NotRegisterFunctionVariable) { TEST_F(TypeDeterminerTest, Function_NotRegisterFunctionVariable) {
@ -1490,7 +1491,7 @@ TEST_F(TypeDeterminerTest, StorageClass_SetsIfMissing) {
EXPECT_TRUE(td()->Determine()) << td()->error(); EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_EQ(var->storage_class(), ast::StorageClass::kFunction); EXPECT_EQ(Sem().Get(var)->StorageClass(), ast::StorageClass::kFunction);
} }
TEST_F(TypeDeterminerTest, StorageClass_DoesNotSetOnConst) { TEST_F(TypeDeterminerTest, StorageClass_DoesNotSetOnConst) {
@ -1501,7 +1502,7 @@ TEST_F(TypeDeterminerTest, StorageClass_DoesNotSetOnConst) {
EXPECT_TRUE(td()->Determine()) << td()->error(); EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_EQ(var->storage_class(), ast::StorageClass::kNone); EXPECT_EQ(Sem().Get(var)->StorageClass(), ast::StorageClass::kNone);
} }
TEST_F(TypeDeterminerTest, StorageClass_NonFunctionClassError) { TEST_F(TypeDeterminerTest, StorageClass_NonFunctionClassError) {

View File

@ -31,6 +31,7 @@
#include "src/ast/uint_literal.h" #include "src/ast/uint_literal.h"
#include "src/ast/variable_decl_statement.h" #include "src/ast/variable_decl_statement.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/variable.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
#include "src/type/bool_type.h" #include "src/type/bool_type.h"
@ -336,19 +337,26 @@ bool ValidatorImpl::ValidateConstructedTypes(
bool ValidatorImpl::ValidateGlobalVariables( bool ValidatorImpl::ValidateGlobalVariables(
const ast::VariableList& global_vars) { const ast::VariableList& global_vars) {
for (auto* var : global_vars) { for (auto* var : global_vars) {
auto* sem = program_->Sem().Get(var);
if (!sem) {
add_error(var->source(), "no semantic information for variable '" +
program_->Symbols().NameFor(var->symbol()) +
"'");
return false;
}
if (variable_stack_.has(var->symbol())) { if (variable_stack_.has(var->symbol())) {
add_error(var->source(), "v-0011", add_error(var->source(), "v-0011",
"redeclared global identifier '" + "redeclared global identifier '" +
program_->Symbols().NameFor(var->symbol()) + "'"); program_->Symbols().NameFor(var->symbol()) + "'");
return false; return false;
} }
if (!var->is_const() && var->storage_class() == ast::StorageClass::kNone) { if (!var->is_const() && sem->StorageClass() == ast::StorageClass::kNone) {
add_error(var->source(), "v-0022", add_error(var->source(), "v-0022",
"global variables must have a storage class"); "global variables must have a storage class");
return false; return false;
} }
if (var->is_const() && if (var->is_const() && !(sem->StorageClass() == ast::StorageClass::kNone)) {
!(var->storage_class() == ast::StorageClass::kNone)) {
add_error(var->source(), "v-global01", add_error(var->source(), "v-global01",
"global constants shouldn't have a storage class"); "global constants shouldn't have a storage class");
return false; return false;

View File

@ -47,6 +47,7 @@
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
@ -194,7 +195,8 @@ bool GeneratorImpl::Generate(std::ostream& out) {
} }
void GeneratorImpl::register_global(ast::Variable* global) { void GeneratorImpl::register_global(ast::Variable* global) {
global_variables_.set(global->symbol(), global); auto* sem = builder_.Sem().Get(global);
global_variables_.set(global->symbol(), sem);
} }
std::string GeneratorImpl::generate_name(const std::string& prefix) { std::string GeneratorImpl::generate_name(const std::string& prefix) {
@ -1134,10 +1136,11 @@ bool GeneratorImpl::EmitExpression(std::ostream& pre,
return false; return false;
} }
bool GeneratorImpl::global_is_in_struct(ast::Variable* var) const { bool GeneratorImpl::global_is_in_struct(const semantic::Variable* var) const {
if (var->HasLocationDecoration() || var->HasBuiltinDecoration()) { if (var->Declaration()->HasLocationDecoration() ||
return var->storage_class() == ast::StorageClass::kInput || var->Declaration()->HasBuiltinDecoration()) {
var->storage_class() == ast::StorageClass::kOutput; return var->StorageClass() == ast::StorageClass::kInput ||
var->StorageClass() == ast::StorageClass::kOutput;
} }
return false; return false;
} }
@ -1146,10 +1149,10 @@ bool GeneratorImpl::EmitIdentifier(std::ostream&,
std::ostream& out, std::ostream& out,
ast::IdentifierExpression* expr) { ast::IdentifierExpression* expr) {
auto* ident = expr->As<ast::IdentifierExpression>(); auto* ident = expr->As<ast::IdentifierExpression>();
ast::Variable* var = nullptr; const semantic::Variable* var = nullptr;
if (global_variables_.get(ident->symbol(), &var)) { if (global_variables_.get(ident->symbol(), &var)) {
if (global_is_in_struct(var)) { if (global_is_in_struct(var)) {
auto var_type = var->storage_class() == ast::StorageClass::kInput auto var_type = var->StorageClass() == ast::StorageClass::kInput
? VarType::kIn ? VarType::kIn
: VarType::kOut; : VarType::kOut;
auto name = current_ep_var_name(var_type); auto name = current_ep_var_name(var_type);
@ -1230,14 +1233,14 @@ bool GeneratorImpl::has_referenced_in_var_needing_struct(
const semantic::Function* func) { const semantic::Function* func) {
for (auto data : func->ReferencedLocationVariables()) { for (auto data : func->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
return true; return true;
} }
} }
for (auto data : func->ReferencedBuiltinVariables()) { for (auto data : func->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
return true; return true;
} }
} }
@ -1248,14 +1251,14 @@ bool GeneratorImpl::has_referenced_out_var_needing_struct(
const semantic::Function* func) { const semantic::Function* func) {
for (auto data : func->ReferencedLocationVariables()) { for (auto data : func->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput) { if (var->StorageClass() == ast::StorageClass::kOutput) {
return true; return true;
} }
} }
for (auto data : func->ReferencedBuiltinVariables()) { for (auto data : func->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput) { if (var->StorageClass() == ast::StorageClass::kOutput) {
return true; return true;
} }
} }
@ -1266,16 +1269,16 @@ bool GeneratorImpl::has_referenced_var_needing_struct(
const semantic::Function* func) { const semantic::Function* func) {
for (auto data : func->ReferencedLocationVariables()) { for (auto data : func->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput || if (var->StorageClass() == ast::StorageClass::kOutput ||
var->storage_class() == ast::StorageClass::kInput) { var->StorageClass() == ast::StorageClass::kInput) {
return true; return true;
} }
} }
for (auto data : func->ReferencedBuiltinVariables()) { for (auto data : func->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput || if (var->StorageClass() == ast::StorageClass::kOutput ||
var->storage_class() == ast::StorageClass::kInput) { var->StorageClass() == ast::StorageClass::kInput) {
return true; return true;
} }
} }
@ -1407,51 +1410,54 @@ bool GeneratorImpl::EmitEntryPointData(
for (auto data : func_sem->ReferencedLocationVariables()) { for (auto data : func_sem->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
auto* decl = var->Declaration();
auto* deco = data.second; auto* deco = data.second;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
in_variables.push_back({var, deco}); in_variables.push_back({decl, deco});
} else if (var->storage_class() == ast::StorageClass::kOutput) { } else if (var->StorageClass() == ast::StorageClass::kOutput) {
outvariables.push_back({var, deco}); outvariables.push_back({decl, deco});
} }
} }
for (auto data : func_sem->ReferencedBuiltinVariables()) { for (auto data : func_sem->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
auto* decl = var->Declaration();
auto* deco = data.second; auto* deco = data.second;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
in_variables.push_back({var, deco}); in_variables.push_back({decl, deco});
} else if (var->storage_class() == ast::StorageClass::kOutput) { } else if (var->StorageClass() == ast::StorageClass::kOutput) {
outvariables.push_back({var, deco}); outvariables.push_back({decl, deco});
} }
} }
bool emitted_uniform = false; bool emitted_uniform = false;
for (auto data : func_sem->ReferencedUniformVariables()) { for (auto data : func_sem->ReferencedUniformVariables()) {
auto* var = data.first; auto* var = data.first;
auto* decl = var->Declaration();
// TODO(dsinclair): We're using the binding to make up the buffer number but // TODO(dsinclair): We're using the binding to make up the buffer number but
// we should instead be using a provided mapping that uses both buffer and // we should instead be using a provided mapping that uses both buffer and
// set. https://bugs.chromium.org/p/tint/issues/detail?id=104 // set. https://bugs.chromium.org/p/tint/issues/detail?id=104
auto* binding = data.second.binding; auto* binding = data.second.binding;
if (binding == nullptr) { if (binding == nullptr) {
error_ = "unable to find binding information for uniform: " + error_ = "unable to find binding information for uniform: " +
builder_.Symbols().NameFor(var->symbol()); builder_.Symbols().NameFor(decl->symbol());
return false; return false;
} }
// auto* set = data.second.set; // auto* set = data.second.set;
// If the global has already been emitted we skip it, it's been emitted by // If the global has already been emitted we skip it, it's been emitted by
// a previous entry point. // a previous entry point.
if (emitted_globals.count(var->symbol()) != 0) { if (emitted_globals.count(decl->symbol()) != 0) {
continue; continue;
} }
emitted_globals.insert(var->symbol()); emitted_globals.insert(decl->symbol());
auto* type = var->type()->UnwrapIfNeeded(); auto* type = decl->type()->UnwrapIfNeeded();
if (auto* strct = type->As<type::Struct>()) { if (auto* strct = type->As<type::Struct>()) {
out << "ConstantBuffer<" << builder_.Symbols().NameFor(strct->symbol()) out << "ConstantBuffer<" << builder_.Symbols().NameFor(strct->symbol())
<< "> " << builder_.Symbols().NameFor(var->symbol()) << "> " << builder_.Symbols().NameFor(decl->symbol())
<< " : register(b" << binding->value() << ");" << std::endl; << " : register(b" << binding->value() << ");" << std::endl;
} else { } else {
// TODO(dsinclair): There is outstanding spec work to require all uniform // TODO(dsinclair): There is outstanding spec work to require all uniform
@ -1460,7 +1466,7 @@ bool GeneratorImpl::EmitEntryPointData(
// is not a block. // is not a block.
// Relevant: https://github.com/gpuweb/gpuweb/issues/1004 // Relevant: https://github.com/gpuweb/gpuweb/issues/1004
// https://github.com/gpuweb/gpuweb/issues/1008 // https://github.com/gpuweb/gpuweb/issues/1008
auto name = "cbuffer_" + builder_.Symbols().NameFor(var->symbol()); auto name = "cbuffer_" + builder_.Symbols().NameFor(decl->symbol());
out << "cbuffer " << name << " : register(b" << binding->value() << ") {" out << "cbuffer " << name << " : register(b" << binding->value() << ") {"
<< std::endl; << std::endl;
@ -1469,7 +1475,7 @@ bool GeneratorImpl::EmitEntryPointData(
if (!EmitType(out, type, "")) { if (!EmitType(out, type, "")) {
return false; return false;
} }
out << " " << builder_.Symbols().NameFor(var->symbol()) << ";" out << " " << builder_.Symbols().NameFor(decl->symbol()) << ";"
<< std::endl; << std::endl;
decrement_indent(); decrement_indent();
out << "};" << std::endl; out << "};" << std::endl;
@ -1484,16 +1490,17 @@ bool GeneratorImpl::EmitEntryPointData(
bool emitted_storagebuffer = false; bool emitted_storagebuffer = false;
for (auto data : func_sem->ReferencedStoragebufferVariables()) { for (auto data : func_sem->ReferencedStoragebufferVariables()) {
auto* var = data.first; auto* var = data.first;
auto* decl = var->Declaration();
auto* binding = data.second.binding; auto* binding = data.second.binding;
// If the global has already been emitted we skip it, it's been emitted by // If the global has already been emitted we skip it, it's been emitted by
// a previous entry point. // a previous entry point.
if (emitted_globals.count(var->symbol()) != 0) { if (emitted_globals.count(decl->symbol()) != 0) {
continue; continue;
} }
emitted_globals.insert(var->symbol()); emitted_globals.insert(decl->symbol());
auto* ac = var->type()->As<type::AccessControl>(); auto* ac = decl->type()->As<type::AccessControl>();
if (ac == nullptr) { if (ac == nullptr) {
error_ = "access control type required for storage buffer"; error_ = "access control type required for storage buffer";
return false; return false;
@ -1502,7 +1509,7 @@ bool GeneratorImpl::EmitEntryPointData(
if (ac->IsReadWrite()) { if (ac->IsReadWrite()) {
out << "RW"; out << "RW";
} }
out << "ByteAddressBuffer " << builder_.Symbols().NameFor(var->symbol()) out << "ByteAddressBuffer " << builder_.Symbols().NameFor(decl->symbol())
<< " : register(u" << binding->value() << ");" << std::endl; << " : register(u" << binding->value() << ");" << std::endl;
emitted_storagebuffer = true; emitted_storagebuffer = true;
} }
@ -2069,11 +2076,11 @@ bool GeneratorImpl::is_storage_buffer_access(
// Check if this is a storage buffer variable // Check if this is a storage buffer variable
if (auto* ident = expr->structure()->As<ast::IdentifierExpression>()) { if (auto* ident = expr->structure()->As<ast::IdentifierExpression>()) {
ast::Variable* var = nullptr; const semantic::Variable* var = nullptr;
if (!global_variables_.get(ident->symbol(), &var)) { if (!global_variables_.get(ident->symbol(), &var)) {
return false; return false;
} }
return var->storage_class() == ast::StorageClass::kStorage; return var->StorageClass() == ast::StorageClass::kStorage;
} else if (auto* member = structure->As<ast::MemberAccessorExpression>()) { } else if (auto* member = structure->As<ast::MemberAccessorExpression>()) {
return is_storage_buffer_access(member); return is_storage_buffer_access(member);
} else if (auto* array = structure->As<ast::ArrayAccessorExpression>()) { } else if (auto* array = structure->As<ast::ArrayAccessorExpression>()) {

View File

@ -338,7 +338,7 @@ class GeneratorImpl {
/// Checks if the global variable is in an input or output struct /// Checks if the global variable is in an input or output struct
/// @param var the variable to check /// @param var the variable to check
/// @returns true if the global is in an input or output struct /// @returns true if the global is in an input or output struct
bool global_is_in_struct(ast::Variable* var) const; bool global_is_in_struct(const semantic::Variable* var) const;
/// Creates a text string representing the index into a storage buffer /// Creates a text string representing the index into a storage buffer
/// @param pre the pre stream /// @param pre the pre stream
/// @param expr the expression to use as the index /// @param expr the expression to use as the index
@ -400,7 +400,7 @@ class GeneratorImpl {
Symbol current_ep_sym_; Symbol current_ep_sym_;
bool generating_entry_point_ = false; bool generating_entry_point_ = false;
uint32_t loop_emission_counter_ = 0; uint32_t loop_emission_counter_ = 0;
ScopeStack<ast::Variable*> global_variables_; ScopeStack<const semantic::Variable*> global_variables_;
std::unordered_map<Symbol, EntryPointData> ep_sym_to_in_data_; std::unordered_map<Symbol, EntryPointData> ep_sym_to_in_data_;
std::unordered_map<Symbol, EntryPointData> ep_sym_to_out_data_; std::unordered_map<Symbol, EntryPointData> ep_sym_to_out_data_;

View File

@ -52,6 +52,7 @@
#include "src/program.h" #include "src/program.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
@ -120,7 +121,8 @@ bool GeneratorImpl::Generate() {
out_ << "#include <metal_stdlib>" << std::endl << std::endl; out_ << "#include <metal_stdlib>" << std::endl << std::endl;
for (auto* global : program_->AST().GlobalVariables()) { for (auto* global : program_->AST().GlobalVariables()) {
global_variables_.set(global->symbol(), global); auto* sem = program_->Sem().Get(global);
global_variables_.set(global->symbol(), sem);
} }
for (auto* const ty : program_->AST().ConstructedTypes()) { for (auto* const ty : program_->AST().ConstructedTypes()) {
@ -508,14 +510,14 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
for (const auto& data : func_sem->ReferencedBuiltinVariables()) { for (const auto& data : func_sem->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() != ast::StorageClass::kInput) { if (var->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }
if (!first) { if (!first) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << program_->Symbols().NameFor(var->symbol()); out_ << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
for (const auto& data : func_sem->ReferencedUniformVariables()) { for (const auto& data : func_sem->ReferencedUniformVariables()) {
@ -524,7 +526,7 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << program_->Symbols().NameFor(var->symbol()); out_ << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
for (const auto& data : func_sem->ReferencedStoragebufferVariables()) { for (const auto& data : func_sem->ReferencedStoragebufferVariables()) {
@ -533,7 +535,7 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << program_->Symbols().NameFor(var->symbol()); out_ << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
const auto& params = expr->params(); const auto& params = expr->params();
@ -1033,10 +1035,10 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
auto* var = data.first; auto* var = data.first;
auto* deco = data.second; auto* deco = data.second;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
in_locations.push_back({var, deco->value()}); in_locations.push_back({var->Declaration(), deco->value()});
} else if (var->storage_class() == ast::StorageClass::kOutput) { } else if (var->StorageClass() == ast::StorageClass::kOutput) {
out_variables.push_back({var, deco}); out_variables.push_back({var->Declaration(), deco});
} }
} }
@ -1044,8 +1046,8 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
auto* var = data.first; auto* var = data.first;
auto* deco = data.second; auto* deco = data.second;
if (var->storage_class() == ast::StorageClass::kOutput) { if (var->StorageClass() == ast::StorageClass::kOutput) {
out_variables.push_back({var, deco}); out_variables.push_back({var->Declaration(), deco});
} }
} }
@ -1191,7 +1193,7 @@ bool GeneratorImpl::has_referenced_in_var_needing_struct(ast::Function* func) {
auto* func_sem = program_->Sem().Get(func); auto* func_sem = program_->Sem().Get(func);
for (auto data : func_sem->ReferencedLocationVariables()) { for (auto data : func_sem->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kInput) { if (var->StorageClass() == ast::StorageClass::kInput) {
return true; return true;
} }
} }
@ -1203,14 +1205,14 @@ bool GeneratorImpl::has_referenced_out_var_needing_struct(ast::Function* func) {
for (auto data : func_sem->ReferencedLocationVariables()) { for (auto data : func_sem->ReferencedLocationVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput) { if (var->StorageClass() == ast::StorageClass::kOutput) {
return true; return true;
} }
} }
for (auto data : func_sem->ReferencedBuiltinVariables()) { for (auto data : func_sem->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() == ast::StorageClass::kOutput) { if (var->StorageClass() == ast::StorageClass::kOutput) {
return true; return true;
} }
} }
@ -1308,7 +1310,7 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
for (const auto& data : func_sem->ReferencedBuiltinVariables()) { for (const auto& data : func_sem->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() != ast::StorageClass::kInput) { if (var->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }
if (!first) { if (!first) {
@ -1317,10 +1319,10 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
first = false; first = false;
out_ << "thread "; out_ << "thread ";
if (!EmitType(var->type(), "")) { if (!EmitType(var->Declaration()->type(), "")) {
return false; return false;
} }
out_ << "& " << program_->Symbols().NameFor(var->symbol()); out_ << "& " << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
for (const auto& data : func_sem->ReferencedUniformVariables()) { for (const auto& data : func_sem->ReferencedUniformVariables()) {
@ -1332,10 +1334,10 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
out_ << "constant "; out_ << "constant ";
// TODO(dsinclair): Can arrays be uniform? If so, fix this ... // TODO(dsinclair): Can arrays be uniform? If so, fix this ...
if (!EmitType(var->type(), "")) { if (!EmitType(var->Declaration()->type(), "")) {
return false; return false;
} }
out_ << "& " << program_->Symbols().NameFor(var->symbol()); out_ << "& " << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
for (const auto& data : func_sem->ReferencedStoragebufferVariables()) { for (const auto& data : func_sem->ReferencedStoragebufferVariables()) {
@ -1345,7 +1347,7 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
} }
first = false; first = false;
auto* ac = var->type()->As<type::AccessControl>(); auto* ac = var->Declaration()->type()->As<type::AccessControl>();
if (ac == nullptr) { if (ac == nullptr) {
error_ = "invalid type for storage buffer, expected access control"; error_ = "invalid type for storage buffer, expected access control";
return false; return false;
@ -1358,7 +1360,7 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
if (!EmitType(ac->type(), "")) { if (!EmitType(ac->type(), "")) {
return false; return false;
} }
out_ << "& " << program_->Symbols().NameFor(var->symbol()); out_ << "& " << program_->Symbols().NameFor(var->Declaration()->symbol());
} }
for (auto* v : func->params()) { for (auto* v : func->params()) {
@ -1447,7 +1449,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
for (auto data : func_sem->ReferencedBuiltinVariables()) { for (auto data : func_sem->ReferencedBuiltinVariables()) {
auto* var = data.first; auto* var = data.first;
if (var->storage_class() != ast::StorageClass::kInput) { if (var->StorageClass() != ast::StorageClass::kInput) {
continue; continue;
} }
@ -1458,7 +1460,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
auto* builtin = data.second; auto* builtin = data.second;
if (!EmitType(var->type(), "")) { if (!EmitType(var->Declaration()->type(), "")) {
return false; return false;
} }
@ -1467,8 +1469,8 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
error_ = "unknown builtin"; error_ = "unknown builtin";
return false; return false;
} }
out_ << " " << program_->Symbols().NameFor(var->symbol()) << " [[" << attr out_ << " " << program_->Symbols().NameFor(var->Declaration()->symbol())
<< "]]"; << " [[" << attr << "]]";
} }
for (auto data : func_sem->ReferencedUniformVariables()) { for (auto data : func_sem->ReferencedUniformVariables()) {
@ -1484,7 +1486,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
auto* binding = data.second.binding; auto* binding = data.second.binding;
if (binding == nullptr) { if (binding == nullptr) {
error_ = "unable to find binding information for uniform: " + error_ = "unable to find binding information for uniform: " +
program_->Symbols().NameFor(var->symbol()); program_->Symbols().NameFor(var->Declaration()->symbol());
return false; return false;
} }
// auto* set = data.second.set; // auto* set = data.second.set;
@ -1492,11 +1494,11 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
out_ << "constant "; out_ << "constant ";
// TODO(dsinclair): Can you have a uniform array? If so, this needs to be // TODO(dsinclair): Can you have a uniform array? If so, this needs to be
// updated to handle arrays property. // updated to handle arrays property.
if (!EmitType(var->type(), "")) { if (!EmitType(var->Declaration()->type(), "")) {
return false; return false;
} }
out_ << "& " << program_->Symbols().NameFor(var->symbol()) << " [[buffer(" out_ << "& " << program_->Symbols().NameFor(var->Declaration()->symbol())
<< binding->value() << ")]]"; << " [[buffer(" << binding->value() << ")]]";
} }
for (auto data : func_sem->ReferencedStoragebufferVariables()) { for (auto data : func_sem->ReferencedStoragebufferVariables()) {
@ -1512,7 +1514,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
auto* binding = data.second.binding; auto* binding = data.second.binding;
// auto* set = data.second.set; // auto* set = data.second.set;
auto* ac = var->type()->As<type::AccessControl>(); auto* ac = var->Declaration()->type()->As<type::AccessControl>();
if (ac == nullptr) { if (ac == nullptr) {
error_ = "invalid type for storage buffer, expected access control"; error_ = "invalid type for storage buffer, expected access control";
return false; return false;
@ -1525,8 +1527,8 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
if (!EmitType(ac->type(), "")) { if (!EmitType(ac->type(), "")) {
return false; return false;
} }
out_ << "& " << program_->Symbols().NameFor(var->symbol()) << " [[buffer(" out_ << "& " << program_->Symbols().NameFor(var->Declaration()->symbol())
<< binding->value() << ")]]"; << " [[buffer(" << binding->value() << ")]]";
} }
out_ << ") {" << std::endl; out_ << ") {" << std::endl;
@ -1563,23 +1565,23 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
return true; return true;
} }
bool GeneratorImpl::global_is_in_struct(ast::Variable* var) const { bool GeneratorImpl::global_is_in_struct(const semantic::Variable* var) const {
bool in_or_out_struct_has_location = bool in_or_out_struct_has_location =
var != nullptr && var->HasLocationDecoration() && var != nullptr && var->Declaration()->HasLocationDecoration() &&
(var->storage_class() == ast::StorageClass::kInput || (var->StorageClass() == ast::StorageClass::kInput ||
var->storage_class() == ast::StorageClass::kOutput); var->StorageClass() == ast::StorageClass::kOutput);
bool in_struct_has_builtin = bool in_struct_has_builtin =
var != nullptr && var->HasBuiltinDecoration() && var != nullptr && var->Declaration()->HasBuiltinDecoration() &&
var->storage_class() == ast::StorageClass::kOutput; var->StorageClass() == ast::StorageClass::kOutput;
return in_or_out_struct_has_location || in_struct_has_builtin; return in_or_out_struct_has_location || in_struct_has_builtin;
} }
bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
auto* ident = expr->As<ast::IdentifierExpression>(); auto* ident = expr->As<ast::IdentifierExpression>();
ast::Variable* var = nullptr; const semantic::Variable* var = nullptr;
if (global_variables_.get(ident->symbol(), &var)) { if (global_variables_.get(ident->symbol(), &var)) {
if (global_is_in_struct(var)) { if (global_is_in_struct(var)) {
auto var_type = var->storage_class() == ast::StorageClass::kInput auto var_type = var->StorageClass() == ast::StorageClass::kInput
? VarType::kIn ? VarType::kIn
: VarType::kOut; : VarType::kOut;
auto name = current_ep_var_name(var_type); auto name = current_ep_var_name(var_type);
@ -1623,7 +1625,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
// will be turned into assignments. // will be turned into assignments.
for (auto* s : *(stmt->body())) { for (auto* s : *(stmt->body())) {
if (auto* decl = s->As<ast::VariableDeclStatement>()) { if (auto* decl = s->As<ast::VariableDeclStatement>()) {
if (!EmitVariable(decl->variable(), true)) { if (!EmitVariable(program_->Sem().Get(decl->variable()), true)) {
return false; return false;
} }
} }
@ -1839,7 +1841,8 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
return EmitSwitch(s); return EmitSwitch(s);
} }
if (auto* v = stmt->As<ast::VariableDeclStatement>()) { if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
return EmitVariable(v->variable(), false); auto* var = program_->Sem().Get(v->variable());
return EmitVariable(var, false);
} }
error_ = "unknown statement type: " + program_->str(stmt); error_ = "unknown statement type: " + program_->str(stmt);
@ -2078,35 +2081,38 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
return true; return true;
} }
bool GeneratorImpl::EmitVariable(ast::Variable* var, bool skip_constructor) { bool GeneratorImpl::EmitVariable(const semantic::Variable* var,
bool skip_constructor) {
make_indent(); make_indent();
auto* decl = var->Declaration();
// TODO(dsinclair): Handle variable decorations // TODO(dsinclair): Handle variable decorations
if (!var->decorations().empty()) { if (!decl->decorations().empty()) {
error_ = "Variable decorations are not handled yet"; error_ = "Variable decorations are not handled yet";
return false; return false;
} }
if (var->is_const()) { if (decl->is_const()) {
out_ << "const "; out_ << "const ";
} }
if (!EmitType(var->type(), program_->Symbols().NameFor(var->symbol()))) { if (!EmitType(decl->type(), program_->Symbols().NameFor(decl->symbol()))) {
return false; return false;
} }
if (!var->type()->Is<type::Array>()) { if (!decl->type()->Is<type::Array>()) {
out_ << " " << program_->Symbols().NameFor(var->symbol()); out_ << " " << program_->Symbols().NameFor(decl->symbol());
} }
if (!skip_constructor) { if (!skip_constructor) {
out_ << " = "; out_ << " = ";
if (var->constructor() != nullptr) { if (decl->constructor() != nullptr) {
if (!EmitExpression(var->constructor())) { if (!EmitExpression(decl->constructor())) {
return false; return false;
} }
} else if (var->storage_class() == ast::StorageClass::kPrivate || } else if (var->StorageClass() == ast::StorageClass::kPrivate ||
var->storage_class() == ast::StorageClass::kFunction || var->StorageClass() == ast::StorageClass::kFunction ||
var->storage_class() == ast::StorageClass::kNone || var->StorageClass() == ast::StorageClass::kNone ||
var->storage_class() == ast::StorageClass::kOutput) { var->StorageClass() == ast::StorageClass::kOutput) {
if (!EmitZeroValue(var->type())) { if (!EmitZeroValue(decl->type())) {
return false; return false;
} }
} }

View File

@ -221,7 +221,7 @@ class GeneratorImpl : public TextGenerator {
/// @param var the variable to generate /// @param var the variable to generate
/// @param skip_constructor set true if the constructor should be skipped /// @param skip_constructor set true if the constructor should be skipped
/// @returns true if the variable was emitted /// @returns true if the variable was emitted
bool EmitVariable(ast::Variable* var, bool skip_constructor); bool EmitVariable(const semantic::Variable* var, bool skip_constructor);
/// Handles generating a program scope constant variable /// Handles generating a program scope constant variable
/// @param var the variable to emit /// @param var the variable to emit
/// @returns true if the variable was emitted /// @returns true if the variable was emitted
@ -256,7 +256,7 @@ class GeneratorImpl : public TextGenerator {
/// Checks if the global variable is in an input or output struct /// Checks if the global variable is in an input or output struct
/// @param var the variable to check /// @param var the variable to check
/// @returns true if the global is in an input or output struct /// @returns true if the global is in an input or output struct
bool global_is_in_struct(ast::Variable* var) const; bool global_is_in_struct(const semantic::Variable* var) const;
/// Converts a builtin to an attribute name /// Converts a builtin to an attribute name
/// @param builtin the builtin to convert /// @param builtin the builtin to convert
@ -283,7 +283,7 @@ class GeneratorImpl : public TextGenerator {
} }
Namer namer_; Namer namer_;
ScopeStack<ast::Variable*> global_variables_; ScopeStack<const semantic::Variable*> global_variables_;
Symbol current_ep_sym_; Symbol current_ep_sym_;
bool generating_entry_point_ = false; bool generating_entry_point_ = false;
const Program* program_ = nullptr; const Program* program_ = nullptr;

View File

@ -61,6 +61,7 @@
#include "src/program.h" #include "src/program.h"
#include "src/semantic/expression.h" #include "src/semantic/expression.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
@ -462,15 +463,15 @@ bool Builder::GenerateEntryPoint(ast::Function* func, uint32_t id) {
for (const auto* var : func_sem->ReferencedModuleVariables()) { for (const auto* var : func_sem->ReferencedModuleVariables()) {
// For SPIR-V 1.3 we only output Input/output variables. If we update to // For SPIR-V 1.3 we only output Input/output variables. If we update to
// SPIR-V 1.4 or later this should be all variables. // SPIR-V 1.4 or later this should be all variables.
if (var->storage_class() != ast::StorageClass::kInput && if (var->StorageClass() != ast::StorageClass::kInput &&
var->storage_class() != ast::StorageClass::kOutput) { var->StorageClass() != ast::StorageClass::kOutput) {
continue; continue;
} }
uint32_t var_id; uint32_t var_id;
if (!scope_stack_.get(var->symbol(), &var_id)) { if (!scope_stack_.get(var->Declaration()->symbol(), &var_id)) {
error_ = "unable to find ID for global variable: " + error_ = "unable to find ID for global variable: " +
builder_.Symbols().NameFor(var->symbol()); builder_.Symbols().NameFor(var->Declaration()->symbol());
return false; return false;
} }
@ -700,6 +701,8 @@ bool Builder::GenerateStore(uint32_t to, uint32_t from) {
} }
bool Builder::GenerateGlobalVariable(ast::Variable* var) { bool Builder::GenerateGlobalVariable(ast::Variable* var) {
auto* sem = builder_.Sem().Get(var);
uint32_t init_id = 0; uint32_t init_id = 0;
if (var->has_constructor()) { if (var->has_constructor()) {
if (!var->constructor()->Is<ast::ConstructorExpression>()) { if (!var->constructor()->Is<ast::ConstructorExpression>()) {
@ -731,9 +734,9 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
auto result = result_op(); auto result = result_op();
auto var_id = result.to_i(); auto var_id = result.to_i();
auto sc = var->storage_class() == ast::StorageClass::kNone auto sc = sem->StorageClass() == ast::StorageClass::kNone
? ast::StorageClass::kPrivate ? ast::StorageClass::kPrivate
: var->storage_class(); : sem->StorageClass();
type::Pointer pt(var->type(), sc); type::Pointer pt(var->type(), sc);
auto type_id = GenerateTypeIfNeeded(&pt); auto type_id = GenerateTypeIfNeeded(&pt);
@ -799,9 +802,9 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
return 0; return 0;
} }
ops.push_back(Operand::Int(init_id)); ops.push_back(Operand::Int(init_id));
} else if (var->storage_class() == ast::StorageClass::kPrivate || } else if (sem->StorageClass() == ast::StorageClass::kPrivate ||
var->storage_class() == ast::StorageClass::kNone || sem->StorageClass() == ast::StorageClass::kNone ||
var->storage_class() == ast::StorageClass::kOutput) { sem->StorageClass() == ast::StorageClass::kOutput) {
ast::NullLiteral nl(Source{}, type); ast::NullLiteral nl(Source{}, type);
init_id = GenerateLiteralIfNeeded(var, &nl); init_id = GenerateLiteralIfNeeded(var, &nl);
if (init_id == 0) { if (init_id == 0) {

View File

@ -59,6 +59,7 @@
#include "src/ast/workgroup_decoration.h" #include "src/ast/workgroup_decoration.h"
#include "src/program.h" #include "src/program.h"
#include "src/semantic/function.h" #include "src/semantic/function.h"
#include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/type/alias_type.h" #include "src/type/alias_type.h"
#include "src/type/array_type.h" #include "src/type/array_type.h"
@ -148,7 +149,7 @@ bool GeneratorImpl::GenerateEntryPoint(ast::PipelineStage stage,
bool found_func_variable = false; bool found_func_variable = false;
for (auto* var : program_->Sem().Get(func)->ReferencedModuleVariables()) { for (auto* var : program_->Sem().Get(func)->ReferencedModuleVariables()) {
if (!EmitVariable(var)) { if (!EmitVariable(var->Declaration())) {
return false; return false;
} }
found_func_variable = true; found_func_variable = true;
@ -583,9 +584,11 @@ bool GeneratorImpl::EmitStructType(const type::Struct* str) {
} }
bool GeneratorImpl::EmitVariable(ast::Variable* var) { bool GeneratorImpl::EmitVariable(ast::Variable* var) {
auto* sem = program_->Sem().Get(var);
make_indent(); make_indent();
if (!var->decorations().empty() && !EmitVariableDecorations(var)) { if (!var->decorations().empty() && !EmitVariableDecorations(sem)) {
return false; return false;
} }
@ -593,9 +596,9 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
out_ << "const"; out_ << "const";
} else { } else {
out_ << "var"; out_ << "var";
if (var->storage_class() != ast::StorageClass::kNone && if (sem->StorageClass() != ast::StorageClass::kNone &&
var->storage_class() != ast::StorageClass::kFunction) { sem->StorageClass() != ast::StorageClass::kFunction) {
out_ << "<" << var->storage_class() << ">"; out_ << "<" << sem->StorageClass() << ">";
} }
} }
@ -615,10 +618,12 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
return true; return true;
} }
bool GeneratorImpl::EmitVariableDecorations(ast::Variable* var) { bool GeneratorImpl::EmitVariableDecorations(const semantic::Variable* var) {
auto* decl = var->Declaration();
out_ << "[["; out_ << "[[";
bool first = true; bool first = true;
for (auto* deco : var->decorations()) { for (auto* deco : decl->decorations()) {
if (!first) { if (!first) {
out_ << ", "; out_ << ", ";
} }

View File

@ -202,7 +202,7 @@ class GeneratorImpl : public TextGenerator {
/// Handles generating variable decorations /// Handles generating variable decorations
/// @param var the decorated variable /// @param var the decorated variable
/// @returns true if the variable decoration was emitted /// @returns true if the variable decoration was emitted
bool EmitVariableDecorations(ast::Variable* var); bool EmitVariableDecorations(const semantic::Variable* var);
private: private:
Program const* const program_; Program const* const program_;