From 761e6b139c4a3dda12a6df765e66dbd1153f6d0f Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 28 Apr 2021 09:29:13 +0000 Subject: [PATCH] ast: Migrate all nodes over to typ::* (except for ast::Module) CloneContext::Clone(typ::Type) now only clones the sem::Type. Attempting to clone both the AST and SEM type will cause the cloned AST to be disjoint. Another change will switch this over to cloning the AST. Bug: tint:724 Change-Id: I2baf5491365d7dc25e6b25d02bfbb46bf90fd0d9 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49341 Commit-Queue: Ben Clayton Kokoro: Kokoro Reviewed-by: Antonio Maiorano --- src/ast/bitcast_expression.cc | 6 ++--- src/ast/bitcast_expression.h | 6 ++--- src/ast/bool_literal.cc | 4 +-- src/ast/bool_literal.h | 2 +- src/ast/bool_literal_test.cc | 14 ++++------ src/ast/float_literal.cc | 4 +-- src/ast/float_literal.h | 2 +- src/ast/int_literal.cc | 2 +- src/ast/int_literal.h | 2 +- src/ast/literal.cc | 4 +-- src/ast/literal.h | 8 +++--- src/ast/sint_literal.cc | 4 +-- src/ast/sint_literal.h | 2 +- src/ast/sint_literal_test.cc | 3 +-- src/ast/type_constructor_expression.cc | 6 ++--- src/ast/type_constructor_expression.h | 6 ++--- src/ast/type_constructor_expression_test.cc | 3 +-- src/ast/uint_literal.cc | 4 +-- src/ast/uint_literal.h | 2 +- src/clone_context.h | 3 +-- src/reader/spirv/namer_test.cc | 6 ++--- src/reader/wgsl/parser_impl_type_decl_test.cc | 27 +++++++++---------- src/typepair.h | 3 ++- 23 files changed, 55 insertions(+), 68 deletions(-) diff --git a/src/ast/bitcast_expression.cc b/src/ast/bitcast_expression.cc index 5a11e5c35b..b2baa53604 100644 --- a/src/ast/bitcast_expression.cc +++ b/src/ast/bitcast_expression.cc @@ -23,10 +23,10 @@ namespace ast { BitcastExpression::BitcastExpression(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, Expression* expr) : Base(program_id, source), type_(type), expr_(expr) { - TINT_ASSERT(type_); + TINT_ASSERT(type_.sem); TINT_ASSERT(expr_); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(expr, program_id); } @@ -37,7 +37,7 @@ BitcastExpression::~BitcastExpression() = default; BitcastExpression* BitcastExpression::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); auto* e = ctx->Clone(expr_); return ctx->dst->create(src, ty, e); } diff --git a/src/ast/bitcast_expression.h b/src/ast/bitcast_expression.h index 75a5a7aea9..5b8bacad83 100644 --- a/src/ast/bitcast_expression.h +++ b/src/ast/bitcast_expression.h @@ -30,14 +30,14 @@ class BitcastExpression : public Castable { /// @param expr the expr BitcastExpression(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, Expression* expr); /// Move constructor BitcastExpression(BitcastExpression&&); ~BitcastExpression() override; /// @returns the left side expression - sem::Type* type() const { return const_cast(type_); } + typ::Type type() const { return type_; } /// @returns the expression Expression* expr() const { return expr_; } @@ -58,7 +58,7 @@ class BitcastExpression : public Castable { private: BitcastExpression(const BitcastExpression&) = delete; - const sem::Type* const type_; + typ::Type const type_; Expression* const expr_; }; diff --git a/src/ast/bool_literal.cc b/src/ast/bool_literal.cc index 097ea15a2b..c4fd8c3152 100644 --- a/src/ast/bool_literal.cc +++ b/src/ast/bool_literal.cc @@ -23,7 +23,7 @@ namespace ast { BoolLiteral::BoolLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, bool value) : Base(program_id, source, type), value_(value) {} @@ -40,7 +40,7 @@ std::string BoolLiteral::name() const { BoolLiteral* BoolLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); return ctx->dst->create(src, ty, value_); } diff --git a/src/ast/bool_literal.h b/src/ast/bool_literal.h index 72c44fed0d..faa303c66e 100644 --- a/src/ast/bool_literal.h +++ b/src/ast/bool_literal.h @@ -32,7 +32,7 @@ class BoolLiteral : public Castable { /// @param value the bool literals value BoolLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, bool value); ~BoolLiteral() override; diff --git a/src/ast/bool_literal_test.cc b/src/ast/bool_literal_test.cc index f7a8005794..a5a0535641 100644 --- a/src/ast/bool_literal_test.cc +++ b/src/ast/bool_literal_test.cc @@ -21,24 +21,21 @@ namespace { using BoolLiteralTest = TestHelper; TEST_F(BoolLiteralTest, True) { - sem::Bool bool_type; - auto* b = create(&bool_type, true); + auto* b = create(ty.bool_(), true); ASSERT_TRUE(b->Is()); ASSERT_TRUE(b->IsTrue()); ASSERT_FALSE(b->IsFalse()); } TEST_F(BoolLiteralTest, False) { - sem::Bool bool_type; - auto* b = create(&bool_type, false); + auto* b = create(ty.bool_(), false); ASSERT_TRUE(b->Is()); ASSERT_FALSE(b->IsTrue()); ASSERT_TRUE(b->IsFalse()); } TEST_F(BoolLiteralTest, Is) { - sem::Bool bool_type; - ast::Literal* l = create(&bool_type, false); + ast::Literal* l = create(ty.bool_(), false); EXPECT_TRUE(l->Is()); EXPECT_FALSE(l->Is()); EXPECT_FALSE(l->Is()); @@ -47,9 +44,8 @@ TEST_F(BoolLiteralTest, Is) { } TEST_F(BoolLiteralTest, ToStr) { - sem::Bool bool_type; - auto* t = create(&bool_type, true); - auto* f = create(&bool_type, false); + auto* t = create(ty.bool_(), true); + auto* f = create(ty.bool_(), false); EXPECT_EQ(str(t), "true"); EXPECT_EQ(str(f), "false"); diff --git a/src/ast/float_literal.cc b/src/ast/float_literal.cc index 908928936c..cba68c6d1c 100644 --- a/src/ast/float_literal.cc +++ b/src/ast/float_literal.cc @@ -25,7 +25,7 @@ namespace ast { FloatLiteral::FloatLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, float value) : Base(program_id, source, type), value_(value) {} @@ -46,7 +46,7 @@ std::string FloatLiteral::name() const { FloatLiteral* FloatLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); return ctx->dst->create(src, ty, value_); } diff --git a/src/ast/float_literal.h b/src/ast/float_literal.h index 05ddbfc1e4..ea3cee41d0 100644 --- a/src/ast/float_literal.h +++ b/src/ast/float_literal.h @@ -32,7 +32,7 @@ class FloatLiteral : public Castable { /// @param value the float literals value FloatLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, float value); ~FloatLiteral() override; diff --git a/src/ast/int_literal.cc b/src/ast/int_literal.cc index c35b92326f..7177787984 100644 --- a/src/ast/int_literal.cc +++ b/src/ast/int_literal.cc @@ -21,7 +21,7 @@ namespace ast { IntLiteral::IntLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, uint32_t value) : Base(program_id, source, type), value_(value) {} diff --git a/src/ast/int_literal.h b/src/ast/int_literal.h index 3a23a7c272..eed01c7b06 100644 --- a/src/ast/int_literal.h +++ b/src/ast/int_literal.h @@ -36,7 +36,7 @@ class IntLiteral : public Castable { /// @param value value of the literal IntLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, uint32_t value); private: diff --git a/src/ast/literal.cc b/src/ast/literal.cc index dd632bdd80..db71189e0e 100644 --- a/src/ast/literal.cc +++ b/src/ast/literal.cc @@ -19,9 +19,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::Literal); namespace tint { namespace ast { -Literal::Literal(ProgramID program_id, - const Source& source, - const sem::Type* type) +Literal::Literal(ProgramID program_id, const Source& source, typ::Type type) : Base(program_id, source), type_(type) {} Literal::~Literal() = default; diff --git a/src/ast/literal.h b/src/ast/literal.h index 1351f007bc..abe5e12fb9 100644 --- a/src/ast/literal.h +++ b/src/ast/literal.h @@ -28,7 +28,7 @@ class Literal : public Castable { ~Literal() override; /// @returns the type of the literal - sem::Type* type() const { return const_cast(type_); } + typ::Type type() const { return type_; } /// Writes a representation of the node to the output stream /// @param sem the semantic info for the program @@ -50,12 +50,10 @@ class Literal : public Castable { /// @param program_id the identifier of the program that owns this node /// @param source the input source /// @param type the type of the literal - explicit Literal(ProgramID program_id, - const Source& source, - const sem::Type* type); + explicit Literal(ProgramID program_id, const Source& source, typ::Type type); private: - const sem::Type* const type_; + typ::Type const type_; }; } // namespace ast diff --git a/src/ast/sint_literal.cc b/src/ast/sint_literal.cc index c3e0a169f0..804caa1300 100644 --- a/src/ast/sint_literal.cc +++ b/src/ast/sint_literal.cc @@ -23,7 +23,7 @@ namespace ast { SintLiteral::SintLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, int32_t value) : Base(program_id, source, type, static_cast(value)) {} @@ -40,7 +40,7 @@ std::string SintLiteral::name() const { SintLiteral* SintLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); return ctx->dst->create(src, ty, value()); } diff --git a/src/ast/sint_literal.h b/src/ast/sint_literal.h index 874ceb52f5..a20410e0ed 100644 --- a/src/ast/sint_literal.h +++ b/src/ast/sint_literal.h @@ -32,7 +32,7 @@ class SintLiteral : public Castable { /// @param value the signed int literals value SintLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, int32_t value); ~SintLiteral() override; diff --git a/src/ast/sint_literal_test.cc b/src/ast/sint_literal_test.cc index da4121b6f8..0d6815a13b 100644 --- a/src/ast/sint_literal_test.cc +++ b/src/ast/sint_literal_test.cc @@ -45,8 +45,7 @@ TEST_F(SintLiteralTest, Name_I32) { } TEST_F(SintLiteralTest, Name_U32) { - sem::U32 u32; - auto* i = create(&u32, 2); + auto* i = create(ty.u32(), 2); EXPECT_EQ("__sint__u32_2", i->name()); } diff --git a/src/ast/type_constructor_expression.cc b/src/ast/type_constructor_expression.cc index ecfa60c0cf..01c0c739c9 100644 --- a/src/ast/type_constructor_expression.cc +++ b/src/ast/type_constructor_expression.cc @@ -23,10 +23,10 @@ namespace ast { TypeConstructorExpression::TypeConstructorExpression(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, ExpressionList values) : Base(program_id, source), type_(type), values_(std::move(values)) { - TINT_ASSERT(type_); + TINT_ASSERT(type_.sem); for (auto* val : values_) { TINT_ASSERT(val); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(val, program_id); @@ -42,7 +42,7 @@ TypeConstructorExpression* TypeConstructorExpression::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); auto vals = ctx->Clone(values()); return ctx->dst->create(src, ty, vals); } diff --git a/src/ast/type_constructor_expression.h b/src/ast/type_constructor_expression.h index 5b63f4386b..6b89a6bf87 100644 --- a/src/ast/type_constructor_expression.h +++ b/src/ast/type_constructor_expression.h @@ -33,14 +33,14 @@ class TypeConstructorExpression /// @param values the constructor values TypeConstructorExpression(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, ExpressionList values); /// Move constructor TypeConstructorExpression(TypeConstructorExpression&&); ~TypeConstructorExpression() override; /// @returns the type - sem::Type* type() const { return const_cast(type_); } + typ::Type type() const { return type_; } /// @returns the values const ExpressionList& values() const { return values_; } @@ -62,7 +62,7 @@ class TypeConstructorExpression private: TypeConstructorExpression(const TypeConstructorExpression&) = delete; - const sem::Type* const type_; + typ::Type const type_; ExpressionList const values_; }; diff --git a/src/ast/type_constructor_expression_test.cc b/src/ast/type_constructor_expression_test.cc index e9e8acec77..73c61ea2b3 100644 --- a/src/ast/type_constructor_expression_test.cc +++ b/src/ast/type_constructor_expression_test.cc @@ -81,13 +81,12 @@ TEST_F(TypeConstructorExpressionTest, Assert_DifferentProgramID_Value) { } TEST_F(TypeConstructorExpressionTest, ToStr) { - sem::Vector vec(ty.f32(), 3); ExpressionList expr; expr.push_back(Expr("expr_1")); expr.push_back(Expr("expr_2")); expr.push_back(Expr("expr_3")); - auto* t = create(&vec, expr); + auto* t = create(ty.vec3(), expr); EXPECT_EQ(str(t), R"(TypeConstructor[not set]{ __vec_3__f32 Identifier[not set]{expr_1} diff --git a/src/ast/uint_literal.cc b/src/ast/uint_literal.cc index 4280855d86..6b6b2cde0f 100644 --- a/src/ast/uint_literal.cc +++ b/src/ast/uint_literal.cc @@ -23,7 +23,7 @@ namespace ast { UintLiteral::UintLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, uint32_t value) : Base(program_id, source, type, value) {} @@ -40,7 +40,7 @@ std::string UintLiteral::name() const { UintLiteral* UintLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source()); - auto* ty = ctx->Clone(type()); + auto ty = ctx->Clone(type()); return ctx->dst->create(src, ty, value()); } diff --git a/src/ast/uint_literal.h b/src/ast/uint_literal.h index 6015415ea2..9b58eb3c7a 100644 --- a/src/ast/uint_literal.h +++ b/src/ast/uint_literal.h @@ -32,7 +32,7 @@ class UintLiteral : public Castable { /// @param value the uint literals value UintLiteral(ProgramID program_id, const Source& source, - const sem::Type* type, + typ::Type type, uint32_t value); ~UintLiteral() override; diff --git a/src/clone_context.h b/src/clone_context.h index 080c27b25b..ad02692fe8 100644 --- a/src/clone_context.h +++ b/src/clone_context.h @@ -185,8 +185,7 @@ class CloneContext { /// @return the cloned type pair template typ::TypePair Clone(const typ::TypePair& tp) { - return {Clone(const_cast(tp.ast)), - Clone(const_cast(tp.sem))}; + return Clone(const_cast(tp.sem)); } /// Clones the Source `s` into #dst diff --git a/src/reader/spirv/namer_test.cc b/src/reader/spirv/namer_test.cc index 79f271b4d4..2ef9943c9c 100644 --- a/src/reader/spirv/namer_test.cc +++ b/src/reader/spirv/namer_test.cc @@ -160,19 +160,19 @@ TEST_F(SpvNamerTest, RegisterWithoutId_Twice) { // Fails on second attempt. EXPECT_FALSE(namer.RegisterWithoutId(n)); EXPECT_FALSE(success_); - EXPECT_EQ(error(),"internal error: name already registered: abbey"); + EXPECT_EQ(error(), "internal error: name already registered: abbey"); } TEST_F(SpvNamerTest, RegisterWithoutId_ConflictsWithIdRegisteredName) { Namer namer(fail_stream_); const std::string n("abbey"); - EXPECT_TRUE(namer.Register(1,n)); + EXPECT_TRUE(namer.Register(1, n)); EXPECT_TRUE(namer.IsRegistered(n)); // Fails on attempt to register without ID. EXPECT_FALSE(namer.RegisterWithoutId(n)); EXPECT_FALSE(success_); - EXPECT_EQ(error(),"internal error: name already registered: abbey"); + EXPECT_EQ(error(), "internal error: name already registered: abbey"); } TEST_F(SpvNamerTest, Register_Once) { diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index 8d7ebb1000..b97f189fbb 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -151,11 +151,9 @@ TEST_P(VecTest, Parse) { INSTANTIATE_TEST_SUITE_P( ParserImplTest, VecTest, - testing::Values(VecData{"vec2", 2, Source::Range{{1u, 1u}, {1u, 10u}}}, - VecData{"vec3", 3, Source::Range{{1u, 1u}, {1u, 10u}}}, - VecData{"vec4", 4, Source::Range{{1u, 1u}, {1u, 10u}}} - - )); + testing::Values(VecData{"vec2", 2, {{1u, 1u}, {1u, 10u}}}, + VecData{"vec3", 3, {{1u, 1u}, {1u, 10u}}}, + VecData{"vec4", 4, {{1u, 1u}, {1u, 10u}}})); class VecMissingGreaterThanTest : public ParserImplTestWithParam {}; @@ -657,16 +655,15 @@ TEST_P(MatrixTest, Parse) { INSTANTIATE_TEST_SUITE_P( ParserImplTest, MatrixTest, - testing::Values( - MatrixData{"mat2x2", 2, 2, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat2x3", 2, 3, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat2x4", 2, 4, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat3x2", 3, 2, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat3x3", 3, 3, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat3x4", 3, 4, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat4x2", 4, 2, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat4x3", 4, 3, Source::Range{{1u, 1u}, {1u, 12u}}}, - MatrixData{"mat4x4", 4, 4, Source::Range{{1u, 1u}, {1u, 12u}}})); + testing::Values(MatrixData{"mat2x2", 2, 2, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat2x3", 2, 3, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat2x4", 2, 4, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat3x2", 3, 2, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat3x3", 3, 3, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat3x4", 3, 4, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat4x2", 4, 2, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat4x3", 4, 3, {{1u, 1u}, {1u, 12u}}}, + MatrixData{"mat4x4", 4, 4, {{1u, 1u}, {1u, 12u}}})); class MatrixMissingGreaterThanTest : public ParserImplTestWithParam {}; diff --git a/src/typepair.h b/src/typepair.h index 7e538002e9..c3fa0f5e42 100644 --- a/src/typepair.h +++ b/src/typepair.h @@ -101,7 +101,6 @@ struct TypePair { sem(static_cast(other.sem)) {} /// Constructor /// @param a the ast::Type pointer - template TypePair(const AST* a) : ast(a) {} // NOLINT: explicit /// Constructor /// @param s the sem::Type pointer @@ -110,6 +109,8 @@ struct TypePair { /// @param a the ast::Type pointer /// @param s the sem::Type pointer TypePair(const AST* a, const SEM* s) : ast(a), sem(s) {} + /// Constructor + TypePair(std::nullptr_t) {} // NOLINT: explicit /// @returns the ast::Type pointer operator AST*() const { return const_cast(ast); }