1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SRC_AST_ARRAY_ACCESSOR_EXPRESSION_H_
16 #define SRC_AST_ARRAY_ACCESSOR_EXPRESSION_H_
17 
18 #include <memory>
19 #include <utility>
20 
21 #include "src/ast/expression.h"
22 #include "src/ast/literal.h"
23 
24 namespace tint {
25 namespace ast {
26 
27 /// An array accessor expression
28 class ArrayAccessorExpression : public Expression {
29  public:
30   /// Constructor
31   ArrayAccessorExpression();
32   /// Constructor
33   /// @param array the array
34   /// @param idx_expr the index expression
35   ArrayAccessorExpression(std::unique_ptr<Expression> array,
36                           std::unique_ptr<Expression> idx_expr);
37   /// Constructor
38   /// @param source the array accessor source
39   /// @param array the array
40   /// @param idx_expr the index expression
41   ArrayAccessorExpression(const Source& source,
42                           std::unique_ptr<Expression> array,
43                           std::unique_ptr<Expression> idx_expr);
44   /// Move constructor
45   ArrayAccessorExpression(ArrayAccessorExpression&&);
46   ~ArrayAccessorExpression() override;
47 
48   /// Sets the array
49   /// @param array the array
set_array(std::unique_ptr<Expression> array)50   void set_array(std::unique_ptr<Expression> array) {
51     array_ = std::move(array);
52   }
53   /// @returns the array
array()54   Expression* array() const { return array_.get(); }
55 
56   /// Sets the index expression
57   /// @param idx_expr the index expression
set_idx_expr(std::unique_ptr<Expression> idx_expr)58   void set_idx_expr(std::unique_ptr<Expression> idx_expr) {
59     idx_expr_ = std::move(idx_expr);
60   }
61   /// @returns the index expression
idx_expr()62   Expression* idx_expr() const { return idx_expr_.get(); }
63   /// Removes the index expression from the array accessor
64   /// @returns the unique pointer to the index expression
take_idx_expr()65   std::unique_ptr<Expression> take_idx_expr() { return std::move(idx_expr_); }
66 
67   /// @returns true if this is an array accessor expression
68   bool IsArrayAccessor() const override;
69 
70   /// @returns true if the node is valid
71   bool IsValid() const override;
72 
73   /// Writes a representation of the node to the output stream
74   /// @param out the stream to write to
75   /// @param indent number of spaces to indent the node when writing
76   void to_str(std::ostream& out, size_t indent) const override;
77 
78  private:
79   ArrayAccessorExpression(const ArrayAccessorExpression&) = delete;
80 
81   std::unique_ptr<Expression> array_;
82   std::unique_ptr<Expression> idx_expr_;
83 };
84 
85 }  // namespace ast
86 }  // namespace tint
87 
88 #endif  // SRC_AST_ARRAY_ACCESSOR_EXPRESSION_H_
89