Add sem::Reference

Bug: tint:727
Change-Id: I904d33b6c0678f478b4687fcc08589dc3cfd379d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50541
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 5d0ae2a..fc6835f 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -471,6 +471,8 @@
     "sem/node.h",
     "sem/pointer_type.cc",
     "sem/pointer_type.h",
+    "sem/reference_type.cc",
+    "sem/reference_type.h",
     "sem/sampled_texture_type.cc",
     "sem/sampled_texture_type.h",
     "sem/sampler_type.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dc55ff0..70b691d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -303,6 +303,8 @@
   sem/multisampled_texture_type.h
   sem/pointer_type.cc
   sem/pointer_type.h
+  sem/reference_type.cc
+  sem/reference_type.h
   sem/sampled_texture_type.cc
   sem/sampled_texture_type.h
   sem/sampler_type.cc
@@ -573,6 +575,7 @@
     sem/matrix_type_test.cc
     sem/multisampled_texture_type_test.cc
     sem/pointer_type_test.cc
+    sem/reference_type_test.cc
     sem/sampled_texture_type_test.cc
     sem/sampler_type_test.cc
     sem/sem_array_test.cc
diff --git a/src/sem/pointer_type_test.cc b/src/sem/pointer_type_test.cc
index 6c75fa2..afa3a0a 100644
--- a/src/sem/pointer_type_test.cc
+++ b/src/sem/pointer_type_test.cc
@@ -23,27 +23,24 @@
 using PointerTest = TestHelper;
 
 TEST_F(PointerTest, Creation) {
-  I32 i32;
-  Pointer p{&i32, ast::StorageClass::kStorage};
-  EXPECT_EQ(p.type(), &i32);
-  EXPECT_EQ(p.storage_class(), ast::StorageClass::kStorage);
+  auto* r = create<Pointer>(create<I32>(), ast::StorageClass::kStorage);
+  EXPECT_TRUE(r->type()->Is<sem::I32>());
+  EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage);
 }
 
-
 TEST_F(PointerTest, TypeName) {
-  I32 i32;
-  Pointer p{&i32, ast::StorageClass::kWorkgroup};
-  EXPECT_EQ(p.type_name(), "__ptr_workgroup__i32");
+  auto* r = create<Pointer>(create<I32>(), ast::StorageClass::kWorkgroup);
+  EXPECT_EQ(r->type_name(), "__ptr_workgroup__i32");
 }
 
 TEST_F(PointerTest, FriendlyNameWithStorageClass) {
-  Pointer p{ty.i32(), ast::StorageClass::kWorkgroup};
-  EXPECT_EQ(p.FriendlyName(Symbols()), "ptr<workgroup, i32>");
+  auto* r = create<Pointer>(ty.i32(), ast::StorageClass::kWorkgroup);
+  EXPECT_EQ(r->FriendlyName(Symbols()), "ptr<workgroup, i32>");
 }
 
 TEST_F(PointerTest, FriendlyNameWithoutStorageClass) {
-  Pointer p{ty.i32(), ast::StorageClass::kNone};
-  EXPECT_EQ(p.FriendlyName(Symbols()), "ptr<i32>");
+  auto* r = create<Pointer>(ty.i32(), ast::StorageClass::kNone);
+  EXPECT_EQ(r->FriendlyName(Symbols()), "ptr<i32>");
 }
 
 }  // namespace
diff --git a/src/sem/reference_type.cc b/src/sem/reference_type.cc
new file mode 100644
index 0000000..8e4c405
--- /dev/null
+++ b/src/sem/reference_type.cc
@@ -0,0 +1,48 @@
+// 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/sem/reference_type.h"
+
+#include "src/program_builder.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::sem::Reference);
+
+namespace tint {
+namespace sem {
+
+Reference::Reference(const Type* subtype, ast::StorageClass storage_class)
+    : subtype_(subtype), storage_class_(storage_class) {}
+
+std::string Reference::type_name() const {
+  std::ostringstream out;
+  out << "__ref_" << storage_class_ << subtype_->type_name();
+  return out.str();
+}
+
+std::string Reference::FriendlyName(const SymbolTable& symbols) const {
+  std::ostringstream out;
+  out << "ref<";
+  if (storage_class_ != ast::StorageClass::kNone) {
+    out << storage_class_ << ", ";
+  }
+  out << subtype_->FriendlyName(symbols) << ">";
+  return out.str();
+}
+
+Reference::Reference(Reference&&) = default;
+
+Reference::~Reference() = default;
+
+}  // namespace sem
+}  // namespace tint
diff --git a/src/sem/reference_type.h b/src/sem/reference_type.h
new file mode 100644
index 0000000..b86112a
--- /dev/null
+++ b/src/sem/reference_type.h
@@ -0,0 +1,58 @@
+// 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_SEM_REFERENCE_TYPE_H_
+#define SRC_SEM_REFERENCE_TYPE_H_
+
+#include <string>
+
+#include "src/ast/storage_class.h"
+#include "src/sem/type.h"
+
+namespace tint {
+namespace sem {
+
+/// A reference type.
+class Reference : public Castable<Reference, Type> {
+ public:
+  /// Constructor
+  /// @param subtype the pointee type
+  /// @param storage_class the storage class of the reference
+  Reference(const Type* subtype, ast::StorageClass storage_class);
+  /// Move constructor
+  Reference(Reference&&);
+  ~Reference() override;
+
+  /// @returns the pointee type
+  const Type* type() const { return subtype_; }
+  /// @returns the storage class of the reference
+  ast::StorageClass storage_class() const { return storage_class_; }
+
+  /// @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;
+
+ private:
+  Type const* const subtype_;
+  ast::StorageClass const storage_class_;
+};
+
+}  // namespace sem
+}  // namespace tint
+
+#endif  // SRC_SEM_REFERENCE_TYPE_H_
diff --git a/src/sem/reference_type_test.cc b/src/sem/reference_type_test.cc
new file mode 100644
index 0000000..bc890fa
--- /dev/null
+++ b/src/sem/reference_type_test.cc
@@ -0,0 +1,47 @@
+// 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/sem/reference_type.h"
+#include "src/sem/test_helper.h"
+
+namespace tint {
+namespace sem {
+namespace {
+
+using ReferenceTest = TestHelper;
+
+TEST_F(ReferenceTest, Creation) {
+  auto* r = create<Reference>(create<I32>(), ast::StorageClass::kStorage);
+  EXPECT_TRUE(r->type()->Is<sem::I32>());
+  EXPECT_EQ(r->storage_class(), ast::StorageClass::kStorage);
+}
+
+TEST_F(ReferenceTest, TypeName) {
+  auto* r = create<Reference>(create<I32>(), ast::StorageClass::kWorkgroup);
+  EXPECT_EQ(r->type_name(), "__ref_workgroup__i32");
+}
+
+TEST_F(ReferenceTest, FriendlyNameWithStorageClass) {
+  auto* r = create<Reference>(ty.i32(), ast::StorageClass::kWorkgroup);
+  EXPECT_EQ(r->FriendlyName(Symbols()), "ref<workgroup, i32>");
+}
+
+TEST_F(ReferenceTest, FriendlyNameWithoutStorageClass) {
+  auto* r = create<Reference>(ty.i32(), ast::StorageClass::kNone);
+  EXPECT_EQ(r->FriendlyName(Symbols()), "ref<i32>");
+}
+
+}  // namespace
+}  // namespace sem
+}  // namespace tint
diff --git a/test/BUILD.gn b/test/BUILD.gn
index c5cd555..ca6b446 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -277,6 +277,7 @@
     "../src/sem/matrix_type_test.cc",
     "../src/sem/multisampled_texture_type_test.cc",
     "../src/sem/pointer_type_test.cc",
+    "../src/sem/reference_type_test.cc",
     "../src/sem/sampled_texture_type_test.cc",
     "../src/sem/sampler_type_test.cc",
     "../src/sem/sem_array_test.cc",