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_LITERAL_H_ |
| 16 | #define SRC_AST_LITERAL_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 20 | #include "src/ast/node.h" |
dan sinclair | 113fb07 | 2020-03-27 00:45:34 +0000 | [diff] [blame] | 21 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 22 | namespace tint { |
| 23 | namespace ast { |
| 24 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 25 | /// Base class for a literal value |
Ben Clayton | e319d7f | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 26 | class Literal : public Castable<Literal, Node> { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 27 | public: |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 28 | ~Literal() override; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 29 | |
dan sinclair | 113fb07 | 2020-03-27 00:45:34 +0000 | [diff] [blame] | 30 | /// @returns the type of the literal |
Ben Clayton | 03ae9a3 | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 31 | type::Type* type() const { return type_; } |
dan sinclair | 113fb07 | 2020-03-27 00:45:34 +0000 | [diff] [blame] | 32 | |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 33 | /// Writes a representation of the node to the output stream |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 34 | /// @param sem the semantic info for the program |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 35 | /// @param out the stream to write to |
| 36 | /// @param indent number of spaces to indent the node when writing |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame^] | 37 | void to_str(const sem::Info& sem, |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 38 | std::ostream& out, |
| 39 | size_t indent) const override; |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 40 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 41 | /// @param sem the semantic info for the program |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 42 | /// @returns the literal as a string |
Antonio Maiorano | 5cd71b8 | 2021-04-16 19:07:51 +0000 | [diff] [blame^] | 43 | virtual std::string to_str(const sem::Info& sem) const = 0; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 44 | |
dan sinclair | 113fb07 | 2020-03-27 00:45:34 +0000 | [diff] [blame] | 45 | /// @returns the name for this literal. This name is unique to this value. |
| 46 | virtual std::string name() const = 0; |
| 47 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 48 | protected: |
| 49 | /// Constructor |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 50 | /// @param program_id the identifier of the program that owns this node |
Ben Clayton | 5ed161b | 2020-12-12 01:35:43 +0000 | [diff] [blame] | 51 | /// @param source the input source |
dan sinclair | a085c7b | 2020-03-31 21:15:40 +0000 | [diff] [blame] | 52 | /// @param type the type of the literal |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 53 | explicit Literal(ProgramID program_id, |
| 54 | const Source& source, |
| 55 | type::Type* type); |
dan sinclair | 113fb07 | 2020-03-27 00:45:34 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
Ben Clayton | be96376 | 2020-12-15 14:52:38 +0000 | [diff] [blame] | 58 | type::Type* const type_; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace ast |
| 62 | } // namespace tint |
| 63 | |
| 64 | #endif // SRC_AST_LITERAL_H_ |