ast::Module: Move ConstructedTypes() to typ::Type

And add a few additional helper methods.
Stepping stone to having the module only reference AST nodes.

Bug: tint:724
Change-Id: Ib321dadce5f739afe4f71cbafde9dd2d1c6431bb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49743
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2021-05-04 18:03:11 +00:00 committed by Commit Bot service account
parent 0f88b31503
commit 49a545c919
7 changed files with 42 additions and 12 deletions

View File

@ -16,6 +16,7 @@
#include <utility>
#include "src/ast/named_type.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::ast::Module);
@ -50,6 +51,17 @@ Module::Module(ProgramID program_id,
Module::~Module() = default;
const ast::NamedType* Module::LookupType(Symbol name) const {
for (auto ct : ConstructedTypes()) {
if (auto* ty = ct.ast->As<ast::NamedType>()) {
if (ty->name() == name) {
return ty;
}
}
}
return nullptr;
}
Module* Module::Clone(CloneContext* ctx) const {
auto* out = ctx->dst->create<Module>();
out->Copy(ctx, this);
@ -80,7 +92,7 @@ void Module::to_str(const sem::Info& sem,
make_indent(out, indent);
out << "Module{" << std::endl;
indent += 2;
for (auto* const ty : constructed_types_) {
for (auto const ty : constructed_types_) {
make_indent(out, indent);
if (auto* alias = ty->As<sem::Alias>()) {
out << alias->symbol().to_str() << " -> " << alias->type()->type_name()

View File

@ -19,10 +19,13 @@
#include <vector>
#include "src/ast/function.h"
#include "src/ast/type.h"
namespace tint {
namespace ast {
class NamedType;
/// Module holds the top-level AST types, functions and global variables used by
/// a Program.
class Module : public Castable<Module, Node> {
@ -58,6 +61,17 @@ class Module : public Castable<Module, Node> {
global_declarations_.push_back(var);
}
/// @returns true if the module has the global declaration `decl`
/// @param decl the declaration to check
bool HasGlobalDeclaration(const Cloneable* decl) const {
for (auto* d : global_declarations_) {
if (d == decl) {
return true;
}
}
return false;
}
/// @returns the global variables for the translation unit
const VariableList& GlobalVariables() const { return global_variables_; }
@ -67,14 +81,18 @@ class Module : public Castable<Module, Node> {
/// Adds a constructed type to the Builder.
/// The type must be an alias or a struct.
/// @param type the constructed type to add
void AddConstructedType(sem::Type* type) {
void AddConstructedType(typ::Type type) {
TINT_ASSERT(type);
constructed_types_.push_back(type);
global_declarations_.push_back(type);
global_declarations_.push_back(const_cast<sem::Type*>(type.sem));
}
/// @returns the NamedType registered as a ConstructedType()
/// @param name the name of the type to search for
const ast::NamedType* LookupType(Symbol name) const;
/// @returns the constructed types in the translation unit
const std::vector<sem::Type*>& ConstructedTypes() const {
const std::vector<typ::Type>& ConstructedTypes() const {
return constructed_types_;
}
@ -115,7 +133,7 @@ class Module : public Castable<Module, Node> {
private:
std::vector<Cloneable*> global_declarations_;
std::vector<sem::Type*> constructed_types_;
std::vector<typ::Type> constructed_types_;
FunctionList functions_;
VariableList global_variables_;
};

View File

@ -163,7 +163,7 @@ TEST_F(ParserImplTest, GlobalDecl_ParsesStruct) {
auto program = p->program();
ASSERT_EQ(program.AST().ConstructedTypes().size(), 1u);
auto* t = program.AST().ConstructedTypes()[0];
auto t = program.AST().ConstructedTypes()[0];
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->Is<sem::StructType>());
@ -181,7 +181,7 @@ TEST_F(ParserImplTest, GlobalDecl_Struct_WithStride) {
auto program = p->program();
ASSERT_EQ(program.AST().ConstructedTypes().size(), 1u);
auto* t = program.AST().ConstructedTypes()[0];
auto t = program.AST().ConstructedTypes()[0];
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->Is<sem::StructType>());
@ -208,7 +208,7 @@ TEST_F(ParserImplTest, GlobalDecl_Struct_WithDecoration) {
auto program = p->program();
ASSERT_EQ(program.AST().ConstructedTypes().size(), 1u);
auto* t = program.AST().ConstructedTypes()[0];
auto t = program.AST().ConstructedTypes()[0];
ASSERT_NE(t, nullptr);
ASSERT_TRUE(t->Is<sem::StructType>());

View File

@ -65,7 +65,7 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
// Strip entry point IO decorations from struct declarations.
// TODO(jrprice): This code is duplicated with the SPIR-V transform.
for (auto* ty : ctx.src->AST().ConstructedTypes()) {
for (auto ty : ctx.src->AST().ConstructedTypes()) {
if (auto* struct_ty = ty->As<sem::StructType>()) {
// Build new list of struct members without entry point IO decorations.
ast::StructMemberList new_struct_members;

View File

@ -110,7 +110,7 @@ void Spirv::HandleEntryPointIOTypes(CloneContext& ctx) const {
// ```
// Strip entry point IO decorations from struct declarations.
for (auto* ty : ctx.src->AST().ConstructedTypes()) {
for (auto ty : ctx.src->AST().ConstructedTypes()) {
if (auto* struct_ty = ty->As<sem::StructType>()) {
// Build new list of struct members without entry point IO decorations.
ast::StructMemberList new_struct_members;

View File

@ -119,7 +119,7 @@ bool GeneratorImpl::Generate(std::ostream& out) {
register_global(global);
}
for (auto* const ty : builder_.AST().ConstructedTypes()) {
for (auto const ty : builder_.AST().ConstructedTypes()) {
if (!EmitConstructedType(out, ty)) {
return false;
}

View File

@ -86,7 +86,7 @@ bool GeneratorImpl::Generate() {
global_variables_.set(global->symbol(), sem);
}
for (auto* const ty : program_->AST().ConstructedTypes()) {
for (auto const ty : program_->AST().ConstructedTypes()) {
if (!EmitConstructedType(ty)) {
return false;
}