blob: 10548c1e5bc7a4fb32ebfef7dff89de65aa0959c [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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
dan sinclair99181d82023-07-20 01:14:15 +000015#ifndef SRC_TINT_LANG_WGSL_AST_INT_LITERAL_EXPRESSION_H_
16#define SRC_TINT_LANG_WGSL_AST_INT_LITERAL_EXPRESSION_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000017
dan sinclair99181d82023-07-20 01:14:15 +000018#include "src/tint/lang/wgsl/ast/literal_expression.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000019
dan sinclair34323ac2022-04-07 18:39:35 +000020namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000021
Ben Clayton8822e292022-05-04 22:18:49 +000022/// An integer literal. The literal may have an 'i', 'u' or no suffix.
dan sinclairbae54e72023-07-28 15:01:54 +000023class IntLiteralExpression final : public Castable<IntLiteralExpression, LiteralExpression> {
dan sinclair41e4d9a2022-05-01 14:40:55 +000024 public:
Ben Clayton8822e292022-05-04 22:18:49 +000025 /// Literal suffix
26 enum class Suffix {
27 /// No suffix
28 kNone,
29 /// 'i' suffix (i32)
30 kI,
31 /// 'u' suffix (u32)
32 kU,
33 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
dan sinclair41e4d9a2022-05-01 14:40:55 +000035 /// Constructor
36 /// @param pid the identifier of the program that owns this node
Ben Clayton4a92a3c2022-07-18 20:50:02 +000037 /// @param nid the unique node identifier
dan sinclair41e4d9a2022-05-01 14:40:55 +000038 /// @param src the source of this node
Ben Clayton8822e292022-05-04 22:18:49 +000039 /// @param val the literal value
40 /// @param suf the literal suffix
dan sinclair637a2fe2023-07-24 21:11:41 +000041 IntLiteralExpression(GenerationID pid, NodeID nid, const Source& src, int64_t val, Suffix suf);
Ben Clayton8822e292022-05-04 22:18:49 +000042
43 ~IntLiteralExpression() override;
44
45 /// Clones this node and all transitive child nodes using the `CloneContext`
46 /// `ctx`.
47 /// @param ctx the clone context
48 /// @return the newly cloned node
Ben Claytonae18c412023-07-29 13:00:40 +000049 const IntLiteralExpression* Clone(CloneContext& ctx) const override;
Ben Clayton8822e292022-05-04 22:18:49 +000050
51 /// The literal value
52 const int64_t value;
53
54 /// The literal suffix
55 const Suffix suffix;
56};
57
Ben Clayton68919602023-07-28 22:51:18 +000058/// @param suffix the enum value
59/// @returns the string for the given enum value
60std::string_view ToString(IntLiteralExpression::Suffix suffix);
61
dan sinclair6cc183c2023-03-02 21:28:45 +000062/// Writes the integer literal suffix to the stream.
63/// @param out the stream to write to
Ben Clayton8822e292022-05-04 22:18:49 +000064/// @param suffix the suffix to write
65/// @returns out so calls can be chained
Ben Clayton68919602023-07-28 22:51:18 +000066template <typename STREAM, typename = traits::EnableIfIsOStream<STREAM>>
67auto& operator<<(STREAM& out, IntLiteralExpression::Suffix suffix) {
68 return out << ToString(suffix);
69}
Ryan Harrisondbc13af2022-02-21 15:19:07 +000070
dan sinclair34323ac2022-04-07 18:39:35 +000071} // namespace tint::ast
Ryan Harrisondbc13af2022-02-21 15:19:07 +000072
dan sinclair99181d82023-07-20 01:14:15 +000073#endif // SRC_TINT_LANG_WGSL_AST_INT_LITERAL_EXPRESSION_H_