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>
This commit is contained in:
dan sinclair 2020-06-02 20:11:54 +00:00 committed by dan sinclair
parent c6f2947ceb
commit e009c2058d
15 changed files with 136 additions and 6 deletions

View File

@ -248,6 +248,8 @@ source_set("libtint_core_src") {
"src/ast/if_statement.h", "src/ast/if_statement.h",
"src/ast/import.cc", "src/ast/import.cc",
"src/ast/import.h", "src/ast/import.h",
"src/ast/int_literal.cc",
"src/ast/int_literal.h",
"src/ast/kill_statement.cc", "src/ast/kill_statement.cc",
"src/ast/kill_statement.h", "src/ast/kill_statement.h",
"src/ast/literal.cc", "src/ast/literal.cc",
@ -587,6 +589,7 @@ source_set("tint_unittests_core_src") {
"src/ast/identifier_expression_test.cc", "src/ast/identifier_expression_test.cc",
"src/ast/if_statement_test.cc", "src/ast/if_statement_test.cc",
"src/ast/import_test.cc", "src/ast/import_test.cc",
"src/ast/int_literal_test.cc",
"src/ast/kill_statement_test.cc", "src/ast/kill_statement_test.cc",
"src/ast/location_decoration_test.cc", "src/ast/location_decoration_test.cc",
"src/ast/loop_statement_test.cc", "src/ast/loop_statement_test.cc",

View File

@ -85,6 +85,8 @@ set(TINT_LIB_SRCS
ast/if_statement.h ast/if_statement.h
ast/import.cc ast/import.cc
ast/import.h ast/import.h
ast/int_literal.cc
ast/int_literal.h
ast/kill_statement.cc ast/kill_statement.cc
ast/kill_statement.h ast/kill_statement.h
ast/literal.h ast/literal.h
@ -266,6 +268,7 @@ set(TINT_TEST_SRCS
ast/identifier_expression_test.cc ast/identifier_expression_test.cc
ast/if_statement_test.cc ast/if_statement_test.cc
ast/import_test.cc ast/import_test.cc
ast/int_literal_test.cc
ast/kill_statement_test.cc ast/kill_statement_test.cc
ast/location_decoration_test.cc ast/location_decoration_test.cc
ast/loop_statement_test.cc ast/loop_statement_test.cc

View File

@ -46,6 +46,7 @@ TEST_F(BoolLiteralTest, Is) {
EXPECT_FALSE(b.IsSint()); EXPECT_FALSE(b.IsSint());
EXPECT_FALSE(b.IsFloat()); EXPECT_FALSE(b.IsFloat());
EXPECT_FALSE(b.IsUint()); EXPECT_FALSE(b.IsUint());
EXPECT_FALSE(b.IsInt());
EXPECT_FALSE(b.IsNull()); EXPECT_FALSE(b.IsNull());
} }

View File

@ -35,6 +35,7 @@ TEST_F(FloatLiteralTest, Is) {
FloatLiteral f{&f32, 42.f}; FloatLiteral f{&f32, 42.f};
EXPECT_FALSE(f.IsBool()); EXPECT_FALSE(f.IsBool());
EXPECT_FALSE(f.IsSint()); EXPECT_FALSE(f.IsSint());
EXPECT_FALSE(f.IsInt());
EXPECT_TRUE(f.IsFloat()); EXPECT_TRUE(f.IsFloat());
EXPECT_FALSE(f.IsUint()); EXPECT_FALSE(f.IsUint());
EXPECT_FALSE(f.IsNull()); EXPECT_FALSE(f.IsNull());

29
src/ast/int_literal.cc Normal file
View File

@ -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

42
src/ast/int_literal.h Normal file
View File

@ -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_

View File

@ -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

View File

@ -37,6 +37,10 @@ bool Literal::IsFloat() const {
return false; return false;
} }
bool Literal::IsInt() const {
return false;
}
bool Literal::IsSint() const { bool Literal::IsSint() const {
return false; return false;
} }

View File

@ -37,6 +37,8 @@ class Literal {
virtual bool IsBool() const; virtual bool IsBool() const;
/// @returns true if this is a float literal /// @returns true if this is a float literal
virtual bool IsFloat() const; 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 /// @returns true if this is a signed int literal
virtual bool IsSint() const; virtual bool IsSint() const;
/// @returns true if this is a null literal /// @returns true if this is a null literal

View File

@ -30,6 +30,7 @@ TEST_F(NullLiteralTest, Is) {
EXPECT_FALSE(i.IsSint()); EXPECT_FALSE(i.IsSint());
EXPECT_FALSE(i.IsFloat()); EXPECT_FALSE(i.IsFloat());
EXPECT_FALSE(i.IsUint()); EXPECT_FALSE(i.IsUint());
EXPECT_FALSE(i.IsInt());
EXPECT_TRUE(i.IsNull()); EXPECT_TRUE(i.IsNull());
} }

View File

@ -18,7 +18,7 @@ namespace tint {
namespace ast { namespace ast {
SintLiteral::SintLiteral(ast::type::Type* type, int32_t value) SintLiteral::SintLiteral(ast::type::Type* type, int32_t value)
: Literal(type), value_(value) {} : IntLiteral(type), value_(value) {}
SintLiteral::~SintLiteral() = default; SintLiteral::~SintLiteral() = default;

View File

@ -17,13 +17,13 @@
#include <string> #include <string>
#include "src/ast/literal.h" #include "src/ast/int_literal.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
/// A signed int literal /// A signed int literal
class SintLiteral : public Literal { class SintLiteral : public IntLiteral {
public: public:
/// Constructor /// Constructor
/// @param type the type /// @param type the type

View File

@ -59,6 +59,7 @@ TEST_F(SintLiteralTest, Name_U32) {
SintLiteral i{&u32, 2}; SintLiteral i{&u32, 2};
EXPECT_EQ("__sint__u32_2", i.name()); EXPECT_EQ("__sint__u32_2", i.name());
} }
} // namespace } // namespace
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -18,7 +18,7 @@ namespace tint {
namespace ast { namespace ast {
UintLiteral::UintLiteral(ast::type::Type* type, uint32_t value) UintLiteral::UintLiteral(ast::type::Type* type, uint32_t value)
: Literal(type), value_(value) {} : IntLiteral(type), value_(value) {}
UintLiteral::~UintLiteral() = default; UintLiteral::~UintLiteral() = default;

View File

@ -17,13 +17,13 @@
#include <string> #include <string>
#include "src/ast/literal.h" #include "src/ast/int_literal.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
/// A uint literal /// A uint literal
class UintLiteral : public Literal { class UintLiteral : public IntLiteral {
public: public:
/// Constructor /// Constructor
/// @param type the type of the literal /// @param type the type of the literal