Add ast::ExternalTexture

Also create it in wgsl parser. With this, we now create all the AST type
nodes in the wgsl parser.

Bug: tint:724
Change-Id: I575c9eb0ffbd60c3e7aca0b00227d9833ef65d58
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/48962
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 9d033c6..c808dee 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -308,6 +308,8 @@
     "ast/else_statement.h",
     "ast/expression.cc",
     "ast/expression.h",
+    "ast/external_texture.cc",
+    "ast/external_texture.h",
     "ast/f32.cc",
     "ast/f32.h",
     "ast/fallthrough_statement.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8b8b1ca..4ed9729 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -90,6 +90,8 @@
   ast/else_statement.h
   ast/expression.cc
   ast/expression.h
+  ast/external_texture.cc
+  ast/external_texture.h
   ast/f32.cc
   ast/f32.h
   ast/fallthrough_statement.cc
@@ -479,6 +481,7 @@
     ast/depth_texture_test.cc
     ast/discard_statement_test.cc
     ast/else_statement_test.cc
+    ast/external_texture_test.cc
     ast/f32_test.cc
     ast/fallthrough_statement_test.cc
     ast/float_literal_test.cc
diff --git a/src/ast/external_texture.cc b/src/ast/external_texture.cc
new file mode 100644
index 0000000..bc50f53
--- /dev/null
+++ b/src/ast/external_texture.cc
@@ -0,0 +1,45 @@
+// 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/external_texture.h"
+
+#include "src/program_builder.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::ast::ExternalTexture);
+
+namespace tint {
+namespace ast {
+
+// ExternalTexture::ExternalTexture() : Base(ast::TextureDimension::k2d) {}
+ExternalTexture::ExternalTexture(ProgramID program_id, const Source& source)
+    : Base(program_id, source, ast::TextureDimension::k2d) {}
+
+ExternalTexture::ExternalTexture(ExternalTexture&&) = default;
+
+ExternalTexture::~ExternalTexture() = default;
+
+std::string ExternalTexture::type_name() const {
+  return "__external_texture";
+}
+
+std::string ExternalTexture::FriendlyName(const SymbolTable&) const {
+  return "texture_external";
+}
+
+ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const {
+  return ctx->dst->create<ExternalTexture>();
+}
+
+}  // namespace ast
+}  // namespace tint
diff --git a/src/ast/external_texture.h b/src/ast/external_texture.h
new file mode 100644
index 0000000..a7bbea4
--- /dev/null
+++ b/src/ast/external_texture.h
@@ -0,0 +1,54 @@
+// 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_EXTERNAL_TEXTURE_H_
+#define SRC_AST_EXTERNAL_TEXTURE_H_
+
+#include <string>
+
+#include "src/ast/texture.h"
+
+namespace tint {
+namespace ast {
+
+/// An external texture type
+class ExternalTexture : public Castable<ExternalTexture, Texture> {
+ public:
+  /// Constructor
+  /// @param program_id the identifier of the program that owns this node
+  /// @param source the source of this node
+  ExternalTexture(ProgramID program_id, const Source& source);
+
+  /// Move constructor
+  ExternalTexture(ExternalTexture&&);
+  ~ExternalTexture() override;
+
+  /// @returns the 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
+  ExternalTexture* Clone(CloneContext* ctx) const override;
+};
+
+}  // namespace ast
+}  // namespace tint
+
+#endif  // SRC_AST_EXTERNAL_TEXTURE_H_
diff --git a/src/ast/external_texture_test.cc b/src/ast/external_texture_test.cc
new file mode 100644
index 0000000..803e626
--- /dev/null
+++ b/src/ast/external_texture_test.cc
@@ -0,0 +1,83 @@
+// 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/external_texture.h"
+
+#include "src/ast/access_control.h"
+#include "src/ast/alias.h"
+#include "src/ast/array.h"
+#include "src/ast/bool.h"
+#include "src/ast/depth_texture.h"
+#include "src/ast/f32.h"
+#include "src/ast/i32.h"
+#include "src/ast/matrix.h"
+#include "src/ast/pointer.h"
+#include "src/ast/sampler.h"
+#include "src/ast/storage_texture.h"
+#include "src/ast/struct.h"
+#include "src/ast/test_helper.h"
+#include "src/ast/texture.h"
+#include "src/ast/u32.h"
+#include "src/ast/vector.h"
+
+namespace tint {
+namespace ast {
+namespace {
+
+using AstExternalTextureTest = TestHelper;
+
+TEST_F(AstExternalTextureTest, Is) {
+  Type* ty = create<ExternalTexture>();
+  EXPECT_FALSE(ty->Is<AccessControl>());
+  EXPECT_FALSE(ty->Is<Alias>());
+  EXPECT_FALSE(ty->Is<Array>());
+  EXPECT_FALSE(ty->Is<Bool>());
+  EXPECT_FALSE(ty->Is<F32>());
+  EXPECT_FALSE(ty->Is<I32>());
+  EXPECT_FALSE(ty->Is<Matrix>());
+  EXPECT_FALSE(ty->Is<Pointer>());
+  EXPECT_FALSE(ty->Is<Sampler>());
+  EXPECT_FALSE(ty->Is<Struct>());
+  EXPECT_TRUE(ty->Is<Texture>());
+  EXPECT_FALSE(ty->Is<U32>());
+  EXPECT_FALSE(ty->Is<Vector>());
+}
+
+TEST_F(AstExternalTextureTest, IsTexture) {
+  Texture* ty = create<ExternalTexture>();
+  EXPECT_FALSE(ty->Is<DepthTexture>());
+  EXPECT_TRUE(ty->Is<ExternalTexture>());
+  EXPECT_FALSE(ty->Is<MultisampledTexture>());
+  EXPECT_FALSE(ty->Is<SampledTexture>());
+  EXPECT_FALSE(ty->Is<StorageTexture>());
+}
+
+TEST_F(AstExternalTextureTest, Dim) {
+  auto* ty = create<ExternalTexture>();
+  EXPECT_EQ(ty->dim(), ast::TextureDimension::k2d);
+}
+
+TEST_F(AstExternalTextureTest, TypeName) {
+  auto* ty = create<ExternalTexture>();
+  EXPECT_EQ(ty->type_name(), "__external_texture");
+}
+
+TEST_F(AstExternalTextureTest, FriendlyName) {
+  auto* ty = create<ExternalTexture>();
+  EXPECT_EQ(ty->FriendlyName(Symbols()), "texture_external");
+}
+
+}  // namespace
+}  // namespace ast
+}  // namespace tint
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc
index 3c07f7b..7536cc4 100644
--- a/src/reader/wgsl/parser_impl.cc
+++ b/src/reader/wgsl/parser_impl.cc
@@ -23,6 +23,7 @@
 #include "src/ast/constant_id_decoration.h"
 #include "src/ast/continue_statement.h"
 #include "src/ast/discard_statement.h"
+#include "src/ast/external_texture.h"
 #include "src/ast/fallthrough_statement.h"
 #include "src/ast/if_statement.h"
 #include "src/ast/loop_statement.h"
@@ -657,8 +658,8 @@
 //  : TEXTURE_EXTERNAL
 Maybe<typ::Type> ParserImpl::external_texture_type() {
   if (match(Token::Type::kTextureExternal)) {
-    // TODO(crbug.com/tint/724): builder_.create<ast::ExternalTexture>()
-    return typ::Type{nullptr, builder_.create<sem::ExternalTexture>()};
+    return typ::Type{builder_.create<ast::ExternalTexture>(),
+                     builder_.create<sem::ExternalTexture>()};
   }
 
   return Failure::kNoMatch;
diff --git a/src/sem/external_texture_type.cc b/src/sem/external_texture_type.cc
index 7f475e2..c101245 100644
--- a/src/sem/external_texture_type.cc
+++ b/src/sem/external_texture_type.cc
@@ -28,15 +28,11 @@
 ExternalTexture::~ExternalTexture() = default;
 
 std::string ExternalTexture::type_name() const {
-  std::ostringstream out;
-  out << "__external_texture";
-  return out.str();
+  return "__external_texture";
 }
 
 std::string ExternalTexture::FriendlyName(const SymbolTable&) const {
-  std::ostringstream out;
-  out << "texture_external";
-  return out.str();
+  return "texture_external";
 }
 
 ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const {
diff --git a/test/BUILD.gn b/test/BUILD.gn
index ce361dd..ab2ae1a 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -192,6 +192,7 @@
     "../src/ast/depth_texture_test.cc",
     "../src/ast/discard_statement_test.cc",
     "../src/ast/else_statement_test.cc",
+    "../src/ast/external_texture_test.cc",
     "../src/ast/f32_test.cc",
     "../src/ast/fallthrough_statement_test.cc",
     "../src/ast/float_literal_test.cc",