tint: Add sem::IndexAccessorExpression

Also store expression object in MemberAccessorExpression, as well as the
struct on sem::StructMember.

These are used to implement spir-v reader atomics in a follow-up CL.

Bug: tint:1441
Change-Id: Iea49cfb7f9d2e7898d89d2dac6a16a14022c546f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94523
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/sem/index_accessor_expression.cc b/src/tint/sem/index_accessor_expression.cc
new file mode 100644
index 0000000..fc11b4c
--- /dev/null
+++ b/src/tint/sem/index_accessor_expression.cc
@@ -0,0 +1,37 @@
+// 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/tint/sem/index_accessor_expression.h"
+
+#include <utility>
+
+TINT_INSTANTIATE_TYPEINFO(tint::sem::IndexAccessorExpression);
+
+namespace tint::sem {
+
+IndexAccessorExpression::IndexAccessorExpression(const ast::IndexAccessorExpression* declaration,
+                                                 const sem::Type* type,
+                                                 const Expression* object,
+                                                 const Expression* index,
+                                                 const Statement* statement,
+                                                 Constant constant,
+                                                 bool has_side_effects,
+                                                 const Variable* source_var /* = nullptr */)
+    : Base(declaration, type, statement, constant, has_side_effects, source_var),
+      object_(object),
+      index_(index) {}
+
+IndexAccessorExpression::~IndexAccessorExpression() = default;
+
+}  // namespace tint::sem
diff --git a/src/tint/sem/index_accessor_expression.h b/src/tint/sem/index_accessor_expression.h
new file mode 100644
index 0000000..c77f55d
--- /dev/null
+++ b/src/tint/sem/index_accessor_expression.h
@@ -0,0 +1,66 @@
+// Copyright 2022 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_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_
+#define SRC_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_
+
+#include <vector>
+
+#include "src/tint/sem/expression.h"
+
+// Forward declarations
+namespace tint::ast {
+class IndexAccessorExpression;
+}  // namespace tint::ast
+
+namespace tint::sem {
+
+/// IndexAccessorExpression holds the semantic information for a ast::IndexAccessorExpression node.
+class IndexAccessorExpression final : public Castable<IndexAccessorExpression, Expression> {
+  public:
+    /// Constructor
+    /// @param declaration the AST node
+    /// @param type the resolved type of the expression
+    /// @param object the object expression that is being indexed
+    /// @param index the index expression
+    /// @param statement the statement that owns this expression
+    /// @param constant the constant value of the expression. May be invalid
+    /// @param has_side_effects whether this expression may have side effects
+    /// @param source_var the (optional) source variable for this expression
+    IndexAccessorExpression(const ast::IndexAccessorExpression* declaration,
+                            const sem::Type* type,
+                            const Expression* object,
+                            const Expression* index,
+                            const Statement* statement,
+                            Constant constant,
+                            bool has_side_effects,
+                            const Variable* source_var = nullptr);
+
+    /// Destructor
+    ~IndexAccessorExpression() override;
+
+    /// @returns the object expression that is being indexed
+    Expression const* Object() const { return object_; }
+
+    /// @returns the index expression
+    Expression const* Index() const { return index_; }
+
+  private:
+    Expression const* const object_;
+    Expression const* const index_;
+};
+
+}  // namespace tint::sem
+
+#endif  // SRC_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_
diff --git a/src/tint/sem/member_accessor_expression.cc b/src/tint/sem/member_accessor_expression.cc
index 9dcca76..bd706a5 100644
--- a/src/tint/sem/member_accessor_expression.cc
+++ b/src/tint/sem/member_accessor_expression.cc
@@ -26,29 +26,33 @@
 MemberAccessorExpression::MemberAccessorExpression(const ast::MemberAccessorExpression* declaration,
                                                    const sem::Type* type,
                                                    const Statement* statement,
+                                                   const Expression* object,
                                                    bool has_side_effects,
                                                    const Variable* source_var /* = nullptr */)
-    : Base(declaration, type, statement, Constant{}, has_side_effects, source_var) {}
+    : Base(declaration, type, statement, Constant{}, has_side_effects, source_var),
+      object_(object) {}
 
 MemberAccessorExpression::~MemberAccessorExpression() = default;
 
 StructMemberAccess::StructMemberAccess(const ast::MemberAccessorExpression* declaration,
                                        const sem::Type* type,
                                        const Statement* statement,
+                                       const Expression* object,
                                        const StructMember* member,
                                        bool has_side_effects,
                                        const Variable* source_var /* = nullptr */)
-    : Base(declaration, type, statement, has_side_effects, source_var), member_(member) {}
+    : Base(declaration, type, statement, object, has_side_effects, source_var), member_(member) {}
 
 StructMemberAccess::~StructMemberAccess() = default;
 
 Swizzle::Swizzle(const ast::MemberAccessorExpression* declaration,
                  const sem::Type* type,
                  const Statement* statement,
+                 const Expression* object,
                  std::vector<uint32_t> indices,
                  bool has_side_effects,
                  const Variable* source_var /* = nullptr */)
-    : Base(declaration, type, statement, has_side_effects, source_var),
+    : Base(declaration, type, statement, object, has_side_effects, source_var),
       indices_(std::move(indices)) {}
 
 Swizzle::~Swizzle() = default;
diff --git a/src/tint/sem/member_accessor_expression.h b/src/tint/sem/member_accessor_expression.h
index 0233541..3d60816 100644
--- a/src/tint/sem/member_accessor_expression.h
+++ b/src/tint/sem/member_accessor_expression.h
@@ -38,16 +38,24 @@
     /// @param declaration the AST node
     /// @param type the resolved type of the expression
     /// @param statement the statement that owns this expression
+    /// @param object the object that holds the member being accessed
     /// @param has_side_effects whether this expression may have side effects
     /// @param source_var the (optional) source variable for this expression
     MemberAccessorExpression(const ast::MemberAccessorExpression* declaration,
                              const sem::Type* type,
                              const Statement* statement,
+                             const Expression* object,
                              bool has_side_effects,
                              const Variable* source_var = nullptr);
 
     /// Destructor
     ~MemberAccessorExpression() override;
+
+    /// @returns the object that holds the member being accessed
+    const Expression* Object() const { return object_; }
+
+  private:
+    Expression const* const object_;
 };
 
 /// StructMemberAccess holds the semantic information for a
@@ -59,12 +67,14 @@
     /// @param declaration the AST node
     /// @param type the resolved type of the expression
     /// @param statement the statement that owns this expression
+    /// @param object the object that holds the member being accessed
     /// @param member the structure member
     /// @param has_side_effects whether this expression may have side effects
     /// @param source_var the (optional) source variable for this expression
     StructMemberAccess(const ast::MemberAccessorExpression* declaration,
                        const sem::Type* type,
                        const Statement* statement,
+                       const Expression* object,
                        const StructMember* member,
                        bool has_side_effects,
                        const Variable* source_var = nullptr);
@@ -87,12 +97,14 @@
     /// @param declaration the AST node
     /// @param type the resolved type of the expression
     /// @param statement the statement that owns this expression
+    /// @param object the object that holds the member being accessed
     /// @param indices the swizzle indices
     /// @param has_side_effects whether this expression may have side effects
     /// @param source_var the (optional) source variable for this expression
     Swizzle(const ast::MemberAccessorExpression* declaration,
             const sem::Type* type,
             const Statement* statement,
+            const Expression* object,
             std::vector<uint32_t> indices,
             bool has_side_effects,
             const Variable* source_var = nullptr);
diff --git a/src/tint/sem/struct.h b/src/tint/sem/struct.h
index fe9169d..e026b11 100644
--- a/src/tint/sem/struct.h
+++ b/src/tint/sem/struct.h
@@ -194,9 +194,16 @@
     /// @returns the AST declaration node
     const ast::StructMember* Declaration() const { return declaration_; }
 
-    /// @returns the name of the structure
+    /// @returns the name of the structure member
     Symbol Name() const { return name_; }
 
+    /// Sets the owning structure to `s`
+    /// @param s the new structure owner
+    void SetStruct(const sem::Struct* s) { struct_ = s; }
+
+    /// @returns the structure that owns this member
+    const sem::Struct* Struct() const { return struct_; }
+
     /// @returns the type of the member
     sem::Type* Type() const { return type_; }
 
@@ -215,6 +222,7 @@
   private:
     const ast::StructMember* const declaration_;
     const Symbol name_;
+    const sem::Struct* struct_;
     sem::Type* const type_;
     const uint32_t index_;
     const uint32_t offset_;
diff --git a/src/tint/sem/type_mappings.h b/src/tint/sem/type_mappings.h
index 9041bdb..7dc0ade 100644
--- a/src/tint/sem/type_mappings.h
+++ b/src/tint/sem/type_mappings.h
@@ -25,6 +25,7 @@
 class ForLoopStatement;
 class Function;
 class IfStatement;
+class IndexAccessorExpression;
 class MemberAccessorExpression;
 class Node;
 class Override;
@@ -44,6 +45,7 @@
 class ForLoopStatement;
 class Function;
 class IfStatement;
+class IndexAccessorExpression;
 class MemberAccessorExpression;
 class Node;
 class GlobalVariable;
@@ -69,6 +71,7 @@
     ForLoopStatement* operator()(ast::ForLoopStatement*);
     Function* operator()(ast::Function*);
     IfStatement* operator()(ast::IfStatement*);
+    IndexAccessorExpression* operator()(ast::IndexAccessorExpression*);
     MemberAccessorExpression* operator()(ast::MemberAccessorExpression*);
     Node* operator()(ast::Node*);
     GlobalVariable* operator()(ast::Override*);