Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 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_IDENTIFIER_EXPRESSION_H_ |
| 16 | #define SRC_AST_IDENTIFIER_EXPRESSION_H_ |
| 17 | |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 18 | #include <memory> |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 19 | #include <utility> |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 20 | |
| 21 | #include "src/ast/expression.h" |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 22 | #include "src/ast/intrinsic.h" |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 23 | #include "src/symbol.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 24 | |
| 25 | namespace tint { |
| 26 | namespace ast { |
| 27 | |
| 28 | /// An identifier expression |
Ben Clayton | e319d7f | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 29 | class IdentifierExpression : public Castable<IdentifierExpression, Expression> { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 30 | public: |
| 31 | /// Constructor |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 32 | /// @param source the source |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 33 | /// @param sym the symbol for the identifier |
dan sinclair | c8c3156 | 2021-01-11 16:24:32 +0000 | [diff] [blame] | 34 | IdentifierExpression(const Source& source, Symbol sym); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 35 | /// Move constructor |
Ryan Harrison | 4d32be4 | 2020-04-09 18:52:06 +0000 | [diff] [blame] | 36 | IdentifierExpression(IdentifierExpression&&); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 37 | ~IdentifierExpression() override; |
| 38 | |
dan sinclair | 6b59bf4 | 2020-12-11 19:16:13 +0000 | [diff] [blame] | 39 | /// @returns the symbol for the identifier |
| 40 | Symbol symbol() const { return sym_; } |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 41 | |
| 42 | /// Sets the intrinsic for this identifier |
| 43 | /// @param i the intrinsic to set |
| 44 | void set_intrinsic(Intrinsic i) { intrinsic_ = i; } |
| 45 | /// @returns the intrinsic this identifier represents |
| 46 | Intrinsic intrinsic() const { return intrinsic_; } |
Ben Clayton | 3ea3c99 | 2020-11-18 21:19:22 +0000 | [diff] [blame] | 47 | |
| 48 | /// Sets the intrinsic signature |
| 49 | /// @param s the intrinsic signature to set |
| 50 | void set_intrinsic_signature(std::unique_ptr<intrinsic::Signature> s) { |
| 51 | intrinsic_sig_ = std::move(s); |
| 52 | } |
| 53 | /// @returns the intrinsic signature for this identifier. |
| 54 | const intrinsic::Signature* intrinsic_signature() const { |
| 55 | return intrinsic_sig_.get(); |
| 56 | } |
| 57 | |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 58 | /// @returns true if this identifier is for an intrinsic |
| 59 | bool IsIntrinsic() const { return intrinsic_ != Intrinsic::kNone; } |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 60 | |
Ben Clayton | ed2b978 | 2020-12-01 18:04:17 +0000 | [diff] [blame] | 61 | /// Clones this node and all transitive child nodes using the `CloneContext` |
| 62 | /// `ctx`. |
| 63 | /// @note Semantic information such as resolved expression type and intrinsic |
| 64 | /// information is not cloned. |
| 65 | /// @param ctx the clone context |
| 66 | /// @return the newly cloned node |
| 67 | IdentifierExpression* Clone(CloneContext* ctx) const override; |
| 68 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 69 | /// @returns true if the node is valid |
| 70 | bool IsValid() const override; |
| 71 | |
| 72 | /// Writes a representation of the node to the output stream |
| 73 | /// @param out the stream to write to |
| 74 | /// @param indent number of spaces to indent the node when writing |
| 75 | void to_str(std::ostream& out, size_t indent) const override; |
| 76 | |
| 77 | private: |
| 78 | IdentifierExpression(const IdentifierExpression&) = delete; |
| 79 | |
Ben Clayton | be96376 | 2020-12-15 14:52:38 +0000 | [diff] [blame] | 80 | Symbol const sym_; |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 81 | |
Ben Clayton | be96376 | 2020-12-15 14:52:38 +0000 | [diff] [blame] | 82 | Intrinsic intrinsic_ = Intrinsic::kNone; // Semantic info |
| 83 | std::unique_ptr<intrinsic::Signature> intrinsic_sig_; // Semantic info |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace ast |
| 87 | } // namespace tint |
| 88 | |
| 89 | #endif // SRC_AST_IDENTIFIER_EXPRESSION_H_ |