From 1737485a058b673c38f60046eade9697525c0239 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 30 Apr 2021 19:58:49 +0000 Subject: [PATCH] ast: Add ast::NamedType Have ast::Struct and ast::Alias derive from it. NamedType currently derives from ast::Type, but this might change in the future. Bug: tint:724 Change-Id: I46ab7751c89176f4dec34d63247d2459d02ab6bb Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49525 Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: Antonio Maiorano --- src/BUILD.gn | 2 + src/CMakeLists.txt | 2 + src/ast/alias.cc | 13 ++--- src/ast/alias.h | 17 ++---- src/ast/named_type.cc | 38 +++++++++++++ src/ast/named_type.h | 56 +++++++++++++++++++ src/ast/struct.cc | 21 ++++--- src/ast/struct.h | 13 +---- src/ast/struct_test.cc | 2 +- .../wgsl/parser_impl_type_alias_test.cc | 2 +- 10 files changed, 126 insertions(+), 40 deletions(-) create mode 100644 src/ast/named_type.cc create mode 100644 src/ast/named_type.h diff --git a/src/BUILD.gn b/src/BUILD.gn index 8c321d60a0..4bb3d8165d 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -342,6 +342,8 @@ libtint_source_set("libtint_core_all_src") { "ast/module.h", "ast/multisampled_texture.cc", "ast/multisampled_texture.h", + "ast/named_type.cc", + "ast/named_type.h", "ast/node.cc", "ast/node.h", "ast/override_decoration.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 11b363b13f..aff37fcf79 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -124,6 +124,8 @@ set(TINT_LIB_SRCS ast/module.h ast/multisampled_texture.cc ast/multisampled_texture.h + ast/named_type.cc + ast/named_type.h ast/node.cc ast/node.h ast/override_decoration.cc diff --git a/src/ast/alias.cc b/src/ast/alias.cc index a3b6a2cd15..be02bb2151 100644 --- a/src/ast/alias.cc +++ b/src/ast/alias.cc @@ -23,12 +23,11 @@ namespace ast { Alias::Alias(ProgramID program_id, const Source& source, - const Symbol& sym, + const Symbol& name, Type* subtype) - : Base(program_id, source), - symbol_(sym), + : Base(program_id, source, name), subtype_(subtype), - type_name_("__alias_" + sym.to_str() + subtype->type_name()) { + type_name_("__alias_" + name.to_str() + subtype->type_name()) { TINT_ASSERT(subtype_); } @@ -40,14 +39,10 @@ std::string Alias::type_name() const { return type_name_; } -std::string Alias::FriendlyName(const SymbolTable& symbols) const { - return symbols.NameFor(symbol_); -} - Alias* Alias::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto sym = ctx->Clone(symbol()); + auto sym = ctx->Clone(name()); auto* ty = ctx->Clone(type()); return ctx->dst->create(src, sym, ty); } diff --git a/src/ast/alias.h b/src/ast/alias.h index 5f94ec693a..dc2dd8b76e 100644 --- a/src/ast/alias.h +++ b/src/ast/alias.h @@ -17,48 +17,43 @@ #include -#include "src/ast/type.h" +#include "src/ast/named_type.h" namespace tint { namespace ast { /// A type alias type. Holds a name and pointer to another type. -class Alias : public Castable { +class Alias : public Castable { public: /// Constructor /// @param program_id the identifier of the program that owns this node /// @param source the source of this node - /// @param sym the symbol for the alias + /// @param name the symbol for the alias /// @param subtype the alias'd type Alias(ProgramID program_id, const Source& source, - const Symbol& sym, + const Symbol& name, Type* subtype); /// Move constructor Alias(Alias&&); /// Destructor ~Alias() override; + /// [DEPRECATED] use name() /// @returns the alias symbol - Symbol symbol() const { return symbol_; } + Symbol symbol() const { return name(); } /// @returns the alias type Type* type() const { return subtype_; } /// @returns the type_name for this type std::string type_name() const override; - /// @param symbols the program's symbol table - /// @returns the name for this type that closely resembles how it would be - /// declared in WGSL. - std::string FriendlyName(const SymbolTable& symbols) const override; - /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type Alias* Clone(CloneContext* ctx) const override; private: - Symbol const symbol_; Type* const subtype_; std::string const type_name_; }; diff --git a/src/ast/named_type.cc b/src/ast/named_type.cc new file mode 100644 index 0000000000..52d7125371 --- /dev/null +++ b/src/ast/named_type.cc @@ -0,0 +1,38 @@ +// 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/ast/named_type.h" + +#include "src/program_builder.h" + +TINT_INSTANTIATE_TYPEINFO(tint::ast::NamedType); + +namespace tint { +namespace ast { + +NamedType::NamedType(ProgramID program_id, const Source& source, Symbol name) + : Base(program_id, source), name_(name) { + TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(name, program_id); +} + +NamedType::NamedType(NamedType&&) = default; + +NamedType::~NamedType() = default; + +std::string NamedType::FriendlyName(const SymbolTable& symbols) const { + return symbols.NameFor(name()); +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/named_type.h b/src/ast/named_type.h new file mode 100644 index 0000000000..c0c157b4ea --- /dev/null +++ b/src/ast/named_type.h @@ -0,0 +1,56 @@ +// 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_AST_NAMED_TYPE_H_ +#define SRC_AST_NAMED_TYPE_H_ + +#include + +#include "src/ast/type.h" + +namespace tint { +namespace ast { + +/// The base class for user declared, named types. +class NamedType : public Castable { + public: + /// Create a new struct statement + /// @param program_id the identifier of the program that owns this node + /// @param source The input source for the import statement + /// @param name The name of the structure + NamedType(ProgramID program_id, const Source& source, Symbol name); + /// Move constructor + NamedType(NamedType&&); + + ~NamedType() override; + + /// @returns the name of the structure + Symbol name() const { return name_; } + + /// @param symbols the program's symbol table + /// @returns the name for this type that closely resembles how it would be + /// declared in WGSL. + std::string FriendlyName(const SymbolTable& symbols) const override; + + private: + NamedType(const NamedType&) = delete; + NamedType& operator=(const NamedType&) = delete; + + Symbol const name_; +}; + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_NAMED_TYPE_H_ diff --git a/src/ast/struct.cc b/src/ast/struct.cc index 1ebf174dba..8e569a503f 100644 --- a/src/ast/struct.cc +++ b/src/ast/struct.cc @@ -29,8 +29,7 @@ Struct::Struct(ProgramID program_id, Symbol name, StructMemberList members, DecorationList decorations) - : Base(program_id, source), - name_(name), + : Base(program_id, source, name), members_(std::move(members)), decorations_(std::move(decorations)) { for (auto* mem : members_) { @@ -66,7 +65,19 @@ Struct* Struct::Clone(CloneContext* ctx) const { auto n = ctx->Clone(name()); auto mem = ctx->Clone(members()); auto decos = ctx->Clone(decorations()); - return ctx->dst->create(src, n, mem, decos); + auto* out = ctx->dst->create(src, n, mem, decos); + + // HACK(crbug.com/tint/724): AST nodes do not derive from ShareableCloneable, + // and so each call to Clone() produces a new copy of the node. However, + // during the type system migration of tint:724, we have the situation where + // we have diamonds pointers to the ast::Struct - one from the ast::Module and + // one from the sem::StructType. This is a hack to make ast::Struct act like + // it derives from ShareableCloneable. + // This should be removed (possibly along with ShareableCloneable) once + // tint:724 is complete. + ctx->Replace(this, out); + + return out; } void Struct::to_str(const sem::Info& sem, @@ -90,9 +101,5 @@ std::string Struct::type_name() const { return "__struct_" + name().to_str(); } -std::string Struct::FriendlyName(const SymbolTable& symbols) const { - return symbols.NameFor(name()); -} - } // namespace ast } // namespace tint diff --git a/src/ast/struct.h b/src/ast/struct.h index 770fb3ebc6..d9fe744a8e 100644 --- a/src/ast/struct.h +++ b/src/ast/struct.h @@ -19,14 +19,14 @@ #include #include "src/ast/decoration.h" +#include "src/ast/named_type.h" #include "src/ast/struct_member.h" -#include "src/ast/type.h" namespace tint { namespace ast { /// A struct statement. -class Struct : public Castable { +class Struct : public Castable { public: /// Create a new struct statement /// @param program_id the identifier of the program that owns this node @@ -44,9 +44,6 @@ class Struct : public Castable { ~Struct() override; - /// @returns the name of the structure - Symbol name() const { return name_; } - /// @returns the struct decorations const DecorationList& decorations() const { return decorations_; } @@ -78,15 +75,9 @@ class Struct : public Castable { /// @returns the name for the type std::string type_name() const override; - /// @param symbols the program's symbol table - /// @returns the name for this type that closely resembles how it would be - /// declared in WGSL. - std::string FriendlyName(const SymbolTable& symbols) const override; - private: Struct(const Struct&) = delete; - Symbol const name_; StructMemberList const members_; DecorationList const decorations_; }; diff --git a/src/ast/struct_test.cc b/src/ast/struct_test.cc index 6ac527c5c5..904bdc5c46 100644 --- a/src/ast/struct_test.cc +++ b/src/ast/struct_test.cc @@ -107,7 +107,7 @@ TEST_F(AstStructTest, Assert_DifferentProgramID_StructMember) { { ProgramBuilder b1; ProgramBuilder b2; - b1.create(b2.Sym("S"), + b1.create(b1.Sym("S"), StructMemberList{b2.Member("a", b2.ty.i32())}, DecorationList{}); }, diff --git a/src/reader/wgsl/parser_impl_type_alias_test.cc b/src/reader/wgsl/parser_impl_type_alias_test.cc index ad3e45e41b..d7f72034b5 100644 --- a/src/reader/wgsl/parser_impl_type_alias_test.cc +++ b/src/reader/wgsl/parser_impl_type_alias_test.cc @@ -40,7 +40,7 @@ TEST_F(ParserImplTest, TypeDecl_ParsesType) { TEST_F(ParserImplTest, TypeDecl_ParsesStruct_Ident) { auto p = parser("type a = B"); - auto str = Structure(p->builder().Symbols().Register("B"), {}); + auto str = p->builder().Structure(p->builder().Symbols().Register("B"), {}); p->register_constructed("B", str); auto t = p->type_alias();