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 <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 8c321d6..4bb3d81 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -342,6 +342,8 @@
     "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 11b363b..aff37fc 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -124,6 +124,8 @@
   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 a3b6a2cd..be02bb2 100644
--- a/src/ast/alias.cc
+++ b/src/ast/alias.cc
@@ -23,12 +23,11 @@
 
 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 @@
   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<Alias>(src, sym, ty);
 }
diff --git a/src/ast/alias.h b/src/ast/alias.h
index 5f94ec6..dc2dd8b 100644
--- a/src/ast/alias.h
+++ b/src/ast/alias.h
@@ -17,48 +17,43 @@
 
 #include <string>
 
-#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<Alias, Type> {
+class Alias : public Castable<Alias, NamedType> {
  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 0000000..52d7125
--- /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 0000000..c0c157b
--- /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 <string>
+
+#include "src/ast/type.h"
+
+namespace tint {
+namespace ast {
+
+/// The base class for user declared, named types.
+class NamedType : public Castable<NamedType, Type> {
+ 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 1ebf174..8e569a5 100644
--- a/src/ast/struct.cc
+++ b/src/ast/struct.cc
@@ -29,8 +29,7 @@
                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 @@
   auto n = ctx->Clone(name());
   auto mem = ctx->Clone(members());
   auto decos = ctx->Clone(decorations());
-  return ctx->dst->create<Struct>(src, n, mem, decos);
+  auto* out = ctx->dst->create<Struct>(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 @@
   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 770fb3e..d9fe744 100644
--- a/src/ast/struct.h
+++ b/src/ast/struct.h
@@ -19,14 +19,14 @@
 #include <utility>
 
 #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<Struct, Type> {
+class Struct : public Castable<Struct, NamedType> {
  public:
   /// Create a new struct statement
   /// @param program_id the identifier of the program that owns this node
@@ -44,9 +44,6 @@
 
   ~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 @@
   /// @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 6ac527c..904bdc5 100644
--- a/src/ast/struct_test.cc
+++ b/src/ast/struct_test.cc
@@ -107,7 +107,7 @@
       {
         ProgramBuilder b1;
         ProgramBuilder b2;
-        b1.create<Struct>(b2.Sym("S"),
+        b1.create<Struct>(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 ad3e45e..d7f7203 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_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();