From e009c2058df07ebc2984bc81d987943017682023 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 2 Jun 2020 20:11:54 +0000 Subject: [PATCH] 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 --- BUILD.gn | 3 +++ src/CMakeLists.txt | 3 +++ src/ast/bool_literal_test.cc | 1 + src/ast/float_literal_test.cc | 1 + src/ast/int_literal.cc | 29 +++++++++++++++++++++++ src/ast/int_literal.h | 42 ++++++++++++++++++++++++++++++++++ src/ast/int_literal_test.cc | 43 +++++++++++++++++++++++++++++++++++ src/ast/literal.cc | 4 ++++ src/ast/literal.h | 2 ++ src/ast/null_literal_test.cc | 1 + src/ast/sint_literal.cc | 2 +- src/ast/sint_literal.h | 4 ++-- src/ast/sint_literal_test.cc | 1 + src/ast/uint_literal.cc | 2 +- src/ast/uint_literal.h | 4 ++-- 15 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 src/ast/int_literal.cc create mode 100644 src/ast/int_literal.h create mode 100644 src/ast/int_literal_test.cc diff --git a/BUILD.gn b/BUILD.gn index 9570e28ac1..28606b16cb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -248,6 +248,8 @@ source_set("libtint_core_src") { "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 @@ source_set("tint_unittests_core_src") { "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 5875054d6c..7f2f27435b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -85,6 +85,8 @@ set(TINT_LIB_SRCS 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 @@ set(TINT_TEST_SRCS 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 3c37042b7b..502b12d138 100644 --- a/src/ast/bool_literal_test.cc +++ b/src/ast/bool_literal_test.cc @@ -46,6 +46,7 @@ TEST_F(BoolLiteralTest, Is) { 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 028b623898..5c5f23f3d0 100644 --- a/src/ast/float_literal_test.cc +++ b/src/ast/float_literal_test.cc @@ -35,6 +35,7 @@ TEST_F(FloatLiteralTest, Is) { 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 0000000000..c641ac28bf --- /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 0000000000..8f092990d1 --- /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 + +#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 0000000000..defdda4ea5 --- /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 6dcf084515..d8923f2b3f 100644 --- a/src/ast/literal.cc +++ b/src/ast/literal.cc @@ -37,6 +37,10 @@ bool Literal::IsFloat() const { 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 16957a9d3a..89ba4d22a4 100644 --- a/src/ast/literal.h +++ b/src/ast/literal.h @@ -37,6 +37,8 @@ class Literal { 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 62bd62efda..5e48f61922 100644 --- a/src/ast/null_literal_test.cc +++ b/src/ast/null_literal_test.cc @@ -30,6 +30,7 @@ TEST_F(NullLiteralTest, Is) { 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 4f9f79c71d..93aab83c78 100644 --- a/src/ast/sint_literal.cc +++ b/src/ast/sint_literal.cc @@ -18,7 +18,7 @@ namespace tint { 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 6c62aad5f0..9d23cf763b 100644 --- a/src/ast/sint_literal.h +++ b/src/ast/sint_literal.h @@ -17,13 +17,13 @@ #include -#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 ad4ccb9dbd..907c5b3ab1 100644 --- a/src/ast/sint_literal_test.cc +++ b/src/ast/sint_literal_test.cc @@ -59,6 +59,7 @@ TEST_F(SintLiteralTest, Name_U32) { 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 5f8fc93c8d..078066bc33 100644 --- a/src/ast/uint_literal.cc +++ b/src/ast/uint_literal.cc @@ -18,7 +18,7 @@ namespace tint { 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 eafbab509e..7eb1311295 100644 --- a/src/ast/uint_literal.h +++ b/src/ast/uint_literal.h @@ -17,13 +17,13 @@ #include -#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