blob: 39b826c10b07ed31306ba0db54e03493726937ef [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001// 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 Clayton6f585462020-11-13 21:43:58 +000020#include "src/ast/node.h"
dan sinclair113fb072020-03-27 00:45:34 +000021
Dan Sinclair6e581892020-03-02 15:47:43 -050022namespace tint {
23namespace ast {
24
Dan Sinclair6e581892020-03-02 15:47:43 -050025/// Base class for a literal value
Ben Claytone319d7f2020-11-30 23:30:58 +000026class Literal : public Castable<Literal, Node> {
Dan Sinclair6e581892020-03-02 15:47:43 -050027 public:
Ben Clayton6f585462020-11-13 21:43:58 +000028 ~Literal() override;
Dan Sinclair6e581892020-03-02 15:47:43 -050029
dan sinclair113fb072020-03-27 00:45:34 +000030 /// @returns the type of the literal
Ben Clayton03ae9a32020-11-30 23:30:58 +000031 type::Type* type() const { return type_; }
dan sinclair113fb072020-03-27 00:45:34 +000032
Ben Clayton6f585462020-11-13 21:43:58 +000033 /// Writes a representation of the node to the output stream
Ben Claytondd1b6fc2021-01-29 10:55:40 +000034 /// @param sem the semantic info for the program
Ben Clayton6f585462020-11-13 21:43:58 +000035 /// @param out the stream to write to
36 /// @param indent number of spaces to indent the node when writing
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000037 void to_str(const sem::Info& sem,
Ben Claytondd1b6fc2021-01-29 10:55:40 +000038 std::ostream& out,
39 size_t indent) const override;
Ben Clayton6f585462020-11-13 21:43:58 +000040
Ben Claytondd1b6fc2021-01-29 10:55:40 +000041 /// @param sem the semantic info for the program
Dan Sinclair6e581892020-03-02 15:47:43 -050042 /// @returns the literal as a string
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000043 virtual std::string to_str(const sem::Info& sem) const = 0;
Dan Sinclair6e581892020-03-02 15:47:43 -050044
dan sinclair113fb072020-03-27 00:45:34 +000045 /// @returns the name for this literal. This name is unique to this value.
46 virtual std::string name() const = 0;
47
Dan Sinclair6e581892020-03-02 15:47:43 -050048 protected:
49 /// Constructor
Ben Claytone6995de2021-04-13 23:27:27 +000050 /// @param program_id the identifier of the program that owns this node
Ben Clayton5ed161b2020-12-12 01:35:43 +000051 /// @param source the input source
dan sinclaira085c7b2020-03-31 21:15:40 +000052 /// @param type the type of the literal
Ben Claytone6995de2021-04-13 23:27:27 +000053 explicit Literal(ProgramID program_id,
54 const Source& source,
55 type::Type* type);
dan sinclair113fb072020-03-27 00:45:34 +000056
57 private:
Ben Claytonbe963762020-12-15 14:52:38 +000058 type::Type* const type_;
Dan Sinclair6e581892020-03-02 15:47:43 -050059};
60
61} // namespace ast
62} // namespace tint
63
64#endif // SRC_AST_LITERAL_H_