[wgsl-writer] Emit access control type.

This CL updates the WGSL-Writer to emit the access control type.

Bug: tint:287
Change-Id: Ifb42a5ab199f18014c33be62960d078e57df8dba
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33360
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Auto-Submit: dan sinclair <dsinclair@chromium.org>
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index d9df273..bd345bd 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -404,18 +404,17 @@
 bool GeneratorImpl::EmitType(ast::type::Type* type) {
   if (type->IsAccessControl()) {
     auto* ac = type->AsAccessControl();
-    // TODO(dsinclair): Access control isn't supported in WGSL yet, so this
-    // is disabled for now.
-    //
-    // out_ << "[[access(";
-    // if (ac->IsReadOnly()) {
-    //   out_ << "read";
-    // } else if (ac->IsWriteOnly()) {
-    //   out_ << "write";
-    // } else {
-    //   out_ << "read_write";
-    // }
-    // out_ << ")]]" << std::endl;
+
+    out_ << "[[access(";
+    if (ac->IsReadOnly()) {
+      out_ << "read";
+    } else if (ac->IsReadWrite()) {
+      out_ << "read_write";
+    } else {
+      error_ = "invalid access control";
+      return false;
+    }
+    out_ << ")]]" << std::endl;
     if (!EmitType(ac->type())) {
       return false;
     }
diff --git a/src/writer/wgsl/generator_impl_function_test.cc b/src/writer/wgsl/generator_impl_function_test.cc
index 6923099..15815c1 100644
--- a/src/writer/wgsl/generator_impl_function_test.cc
+++ b/src/writer/wgsl/generator_impl_function_test.cc
@@ -240,7 +240,8 @@
   d : f32;
 };
 
-[[binding(0), set(0)]] var<storage_buffer> data : Data;
+[[binding(0), set(0)]] var<storage_buffer> data : [[access(read_write)]]
+Data;
 
 [[stage(compute)]]
 fn a() -> void {
diff --git a/src/writer/wgsl/generator_impl_type_test.cc b/src/writer/wgsl/generator_impl_type_test.cc
index 6cc49aa..fd4d81d 100644
--- a/src/writer/wgsl/generator_impl_type_test.cc
+++ b/src/writer/wgsl/generator_impl_type_test.cc
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include "gtest/gtest.h"
+#include "src/ast/access_control.h"
 #include "src/ast/stride_decoration.h"
 #include "src/ast/struct.h"
 #include "src/ast/struct_block_decoration.h"
@@ -20,6 +21,7 @@
 #include "src/ast/struct_member.h"
 #include "src/ast/struct_member_decoration.h"
 #include "src/ast/struct_member_offset_decoration.h"
+#include "src/ast/type/access_control_type.h"
 #include "src/ast/type/array_type.h"
 #include "src/ast/type/bool_type.h"
 #include "src/ast/type/depth_texture_type.h"
@@ -61,6 +63,46 @@
   EXPECT_EQ(gen.result(), "array<bool, 4>");
 }
 
+TEST_F(WgslGeneratorImplTest, EmitType_AccessControl_Read) {
+  ast::type::I32Type i32;
+
+  ast::StructMember mem("a", &i32, ast::StructMemberDecorationList{});
+  ast::StructMemberList members;
+  members.push_back(&mem);
+
+  ast::StructBlockDecoration block_deco(Source{});
+  ast::StructDecorationList decos;
+  decos.push_back(&block_deco);
+
+  ast::Struct str(decos, members);
+  ast::type::StructType s("S", &str);
+
+  ast::type::AccessControlType a(ast::AccessControl::kReadOnly, &s);
+
+  ASSERT_TRUE(gen.EmitType(&a)) << gen.error();
+  EXPECT_EQ(gen.result(), "[[access(read)]]\nS");
+}
+
+TEST_F(WgslGeneratorImplTest, EmitType_AccessControl_ReadWrite) {
+  ast::type::I32Type i32;
+
+  ast::StructMember mem("a", &i32, ast::StructMemberDecorationList{});
+  ast::StructMemberList members;
+  members.push_back(&mem);
+
+  ast::StructBlockDecoration block_deco(Source{});
+  ast::StructDecorationList decos;
+  decos.push_back(&block_deco);
+
+  ast::Struct str(decos, members);
+  ast::type::StructType s("S", &str);
+
+  ast::type::AccessControlType a(ast::AccessControl::kReadWrite, &s);
+
+  ASSERT_TRUE(gen.EmitType(&a)) << gen.error();
+  EXPECT_EQ(gen.result(), "[[access(read_write)]]\nS");
+}
+
 TEST_F(WgslGeneratorImplTest, EmitType_Array_Decoration) {
   ast::type::BoolType b;
   ast::ArrayDecorationList decos;