Add IntLiteral parent for Sint and Uint literals.
This CL adds a parent class for the type types of interger literals for
the cases where we can have either.
Change-Id: I61b540bedd49a6cf5a899e6864e2ea6f140cd2be
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/22541
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 9570e28..28606b1 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -248,6 +248,8 @@
"src/ast/if_statement.h",
"src/ast/import.cc",
"src/ast/import.h",
+ "src/ast/int_literal.cc",
+ "src/ast/int_literal.h",
"src/ast/kill_statement.cc",
"src/ast/kill_statement.h",
"src/ast/literal.cc",
@@ -587,6 +589,7 @@
"src/ast/identifier_expression_test.cc",
"src/ast/if_statement_test.cc",
"src/ast/import_test.cc",
+ "src/ast/int_literal_test.cc",
"src/ast/kill_statement_test.cc",
"src/ast/location_decoration_test.cc",
"src/ast/loop_statement_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 5875054..7f2f274 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -85,6 +85,8 @@
ast/if_statement.h
ast/import.cc
ast/import.h
+ ast/int_literal.cc
+ ast/int_literal.h
ast/kill_statement.cc
ast/kill_statement.h
ast/literal.h
@@ -266,6 +268,7 @@
ast/identifier_expression_test.cc
ast/if_statement_test.cc
ast/import_test.cc
+ ast/int_literal_test.cc
ast/kill_statement_test.cc
ast/location_decoration_test.cc
ast/loop_statement_test.cc
diff --git a/src/ast/bool_literal_test.cc b/src/ast/bool_literal_test.cc
index 3c37042..502b12d 100644
--- a/src/ast/bool_literal_test.cc
+++ b/src/ast/bool_literal_test.cc
@@ -46,6 +46,7 @@
EXPECT_FALSE(b.IsSint());
EXPECT_FALSE(b.IsFloat());
EXPECT_FALSE(b.IsUint());
+ EXPECT_FALSE(b.IsInt());
EXPECT_FALSE(b.IsNull());
}
diff --git a/src/ast/float_literal_test.cc b/src/ast/float_literal_test.cc
index 028b623..5c5f23f 100644
--- a/src/ast/float_literal_test.cc
+++ b/src/ast/float_literal_test.cc
@@ -35,6 +35,7 @@
FloatLiteral f{&f32, 42.f};
EXPECT_FALSE(f.IsBool());
EXPECT_FALSE(f.IsSint());
+ EXPECT_FALSE(f.IsInt());
EXPECT_TRUE(f.IsFloat());
EXPECT_FALSE(f.IsUint());
EXPECT_FALSE(f.IsNull());
diff --git a/src/ast/int_literal.cc b/src/ast/int_literal.cc
new file mode 100644
index 0000000..c641ac2
--- /dev/null
+++ b/src/ast/int_literal.cc
@@ -0,0 +1,29 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/int_literal.h"
+
+namespace tint {
+namespace ast {
+
+IntLiteral::IntLiteral(ast::type::Type* type) : Literal(type) {}
+
+IntLiteral::~IntLiteral() = default;
+
+bool IntLiteral::IsInt() const {
+ return true;
+}
+
+} // namespace ast
+} // namespace tint
diff --git a/src/ast/int_literal.h b/src/ast/int_literal.h
new file mode 100644
index 0000000..8f09299
--- /dev/null
+++ b/src/ast/int_literal.h
@@ -0,0 +1,42 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_AST_INT_LITERAL_H_
+#define SRC_AST_INT_LITERAL_H_
+
+#include <string>
+
+#include "src/ast/literal.h"
+
+namespace tint {
+namespace ast {
+
+/// An integer literal. This could be either signed or unsigned.
+class IntLiteral : public Literal {
+ public:
+ ~IntLiteral() override;
+
+ /// @returns true if this is a signed or unsigned integer.
+ bool IsInt() const override;
+
+ protected:
+ /// Constructor
+ /// @param type the type of the literal
+ explicit IntLiteral(ast::type::Type* type);
+};
+
+} // namespace ast
+} // namespace tint
+
+#endif // SRC_AST_INT_LITERAL_H_
diff --git a/src/ast/int_literal_test.cc b/src/ast/int_literal_test.cc
new file mode 100644
index 0000000..defdda4
--- /dev/null
+++ b/src/ast/int_literal_test.cc
@@ -0,0 +1,43 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/int_literal.h"
+
+#include "gtest/gtest.h"
+#include "src/ast/type/i32_type.h"
+#include "src/ast/type/u32_type.h"
+#include "src/ast/sint_literal.h"
+#include "src/ast/uint_literal.h"
+
+namespace tint {
+namespace ast {
+namespace {
+
+using IntLiteralTest = testing::Test;
+
+TEST_F(IntLiteralTest, Sint_IsInt) {
+ ast::type::I32Type i32;
+ SintLiteral i{&i32, 47};
+ ASSERT_TRUE(i.IsInt());
+}
+
+TEST_F(IntLiteralTest, Uint_IsInt) {
+ ast::type::I32Type i32;
+ UintLiteral i{&i32, 42};
+ EXPECT_TRUE(i.IsInt());
+}
+
+} // namespace
+} // namespace ast
+} // namespace tint
diff --git a/src/ast/literal.cc b/src/ast/literal.cc
index 6dcf084..d8923f2 100644
--- a/src/ast/literal.cc
+++ b/src/ast/literal.cc
@@ -37,6 +37,10 @@
return false;
}
+bool Literal::IsInt() const {
+ return false;
+}
+
bool Literal::IsSint() const {
return false;
}
diff --git a/src/ast/literal.h b/src/ast/literal.h
index 16957a9..89ba4d2 100644
--- a/src/ast/literal.h
+++ b/src/ast/literal.h
@@ -37,6 +37,8 @@
virtual bool IsBool() const;
/// @returns true if this is a float literal
virtual bool IsFloat() const;
+ /// @returns thre if this is an int literal (either sint or uint)
+ virtual bool IsInt() const;
/// @returns true if this is a signed int literal
virtual bool IsSint() const;
/// @returns true if this is a null literal
diff --git a/src/ast/null_literal_test.cc b/src/ast/null_literal_test.cc
index 62bd62e..5e48f61 100644
--- a/src/ast/null_literal_test.cc
+++ b/src/ast/null_literal_test.cc
@@ -30,6 +30,7 @@
EXPECT_FALSE(i.IsSint());
EXPECT_FALSE(i.IsFloat());
EXPECT_FALSE(i.IsUint());
+ EXPECT_FALSE(i.IsInt());
EXPECT_TRUE(i.IsNull());
}
diff --git a/src/ast/sint_literal.cc b/src/ast/sint_literal.cc
index 4f9f79c..93aab83 100644
--- a/src/ast/sint_literal.cc
+++ b/src/ast/sint_literal.cc
@@ -18,7 +18,7 @@
namespace ast {
SintLiteral::SintLiteral(ast::type::Type* type, int32_t value)
- : Literal(type), value_(value) {}
+ : IntLiteral(type), value_(value) {}
SintLiteral::~SintLiteral() = default;
diff --git a/src/ast/sint_literal.h b/src/ast/sint_literal.h
index 6c62aad..9d23cf7 100644
--- a/src/ast/sint_literal.h
+++ b/src/ast/sint_literal.h
@@ -17,13 +17,13 @@
#include <string>
-#include "src/ast/literal.h"
+#include "src/ast/int_literal.h"
namespace tint {
namespace ast {
/// A signed int literal
-class SintLiteral : public Literal {
+class SintLiteral : public IntLiteral {
public:
/// Constructor
/// @param type the type
diff --git a/src/ast/sint_literal_test.cc b/src/ast/sint_literal_test.cc
index ad4ccb9..907c5b3 100644
--- a/src/ast/sint_literal_test.cc
+++ b/src/ast/sint_literal_test.cc
@@ -59,6 +59,7 @@
SintLiteral i{&u32, 2};
EXPECT_EQ("__sint__u32_2", i.name());
}
+
} // namespace
} // namespace ast
} // namespace tint
diff --git a/src/ast/uint_literal.cc b/src/ast/uint_literal.cc
index 5f8fc93..078066b 100644
--- a/src/ast/uint_literal.cc
+++ b/src/ast/uint_literal.cc
@@ -18,7 +18,7 @@
namespace ast {
UintLiteral::UintLiteral(ast::type::Type* type, uint32_t value)
- : Literal(type), value_(value) {}
+ : IntLiteral(type), value_(value) {}
UintLiteral::~UintLiteral() = default;
diff --git a/src/ast/uint_literal.h b/src/ast/uint_literal.h
index eafbab5..7eb1311 100644
--- a/src/ast/uint_literal.h
+++ b/src/ast/uint_literal.h
@@ -17,13 +17,13 @@
#include <string>
-#include "src/ast/literal.h"
+#include "src/ast/int_literal.h"
namespace tint {
namespace ast {
/// A uint literal
-class UintLiteral : public Literal {
+class UintLiteral : public IntLiteral {
public:
/// Constructor
/// @param type the type of the literal