diff --git a/src/BUILD.gn b/src/BUILD.gn index cc56039878..0214c2cd04 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -286,8 +286,6 @@ libtint_source_set("libtint_core_all_src") { "ast/sampled_texture.h", "ast/sampler.cc", "ast/sampler.h", - "ast/scalar_constructor_expression.cc", - "ast/scalar_constructor_expression.h", "ast/sint_literal.cc", "ast/sint_literal.h", "ast/stage_decoration.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 97e92f6ef3..34027d38d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -150,8 +150,6 @@ set(TINT_LIB_SRCS ast/sampled_texture.h ast/sampler.cc ast/sampler.h - ast/scalar_constructor_expression.cc - ast/scalar_constructor_expression.h ast/sint_literal.cc ast/sint_literal.h ast/stage_decoration.cc @@ -631,7 +629,6 @@ if(${TINT_BUILD_TESTS}) ast/return_statement_test.cc ast/sampled_texture_test.cc ast/sampler_test.cc - ast/scalar_constructor_expression_test.cc ast/sint_literal_test.cc ast/stage_decoration_test.cc ast/storage_texture_test.cc diff --git a/src/ast/array.cc b/src/ast/array.cc index e37de045c1..779a950cd2 100644 --- a/src/ast/array.cc +++ b/src/ast/array.cc @@ -30,11 +30,8 @@ std::string SizeExprToString(const Expression* size, if (auto* ident = size->As()) { return symbols.NameFor(ident->symbol); } - if (auto* scalar = size->As()) { - auto* literal = scalar->literal->As(); - if (literal) { - return std::to_string(literal->ValueAsU32()); - } + if (auto* literal = size->As()) { + return std::to_string(literal->ValueAsU32()); } // This will never be exposed to the user as the Resolver will reject this // expression for array size. diff --git a/src/ast/literal.h b/src/ast/literal.h index df8db02c9d..851b3ced40 100644 --- a/src/ast/literal.h +++ b/src/ast/literal.h @@ -17,13 +17,13 @@ #include -#include "src/ast/node.h" +#include "src/ast/expression.h" namespace tint { namespace ast { -/// Base class for a literal value -class Literal : public Castable { +/// Base class for a literal value expressions +class Literal : public Castable { public: ~Literal() override; diff --git a/src/ast/scalar_constructor_expression.cc b/src/ast/scalar_constructor_expression.cc deleted file mode 100644 index 8085188c91..0000000000 --- a/src/ast/scalar_constructor_expression.cc +++ /dev/null @@ -1,46 +0,0 @@ -// 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/scalar_constructor_expression.h" - -#include "src/program_builder.h" - -TINT_INSTANTIATE_TYPEINFO(tint::ast::ScalarConstructorExpression); - -namespace tint { -namespace ast { - -ScalarConstructorExpression::ScalarConstructorExpression(ProgramID pid, - const Source& src, - const Literal* lit) - : Base(pid, src), literal(lit) { - TINT_ASSERT(AST, literal); - TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, literal, program_id); -} - -ScalarConstructorExpression::ScalarConstructorExpression( - ScalarConstructorExpression&&) = default; - -ScalarConstructorExpression::~ScalarConstructorExpression() = default; - -const ScalarConstructorExpression* ScalarConstructorExpression::Clone( - CloneContext* ctx) const { - // Clone arguments outside of create() call to have deterministic ordering - auto src = ctx->Clone(source); - auto* lit = ctx->Clone(literal); - return ctx->dst->create(src, lit); -} - -} // namespace ast -} // namespace tint diff --git a/src/ast/scalar_constructor_expression.h b/src/ast/scalar_constructor_expression.h deleted file mode 100644 index b80650e2bb..0000000000 --- a/src/ast/scalar_constructor_expression.h +++ /dev/null @@ -1,52 +0,0 @@ -// 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_SCALAR_CONSTRUCTOR_EXPRESSION_H_ -#define SRC_AST_SCALAR_CONSTRUCTOR_EXPRESSION_H_ - -#include "src/ast/constructor_expression.h" -#include "src/ast/literal.h" - -namespace tint { -namespace ast { - -/// A scalar constructor -class ScalarConstructorExpression - : public Castable { - public: - /// Constructor - /// @param pid the identifier of the program that owns this node - /// @param src the source of this node - /// @param literal the const literal - ScalarConstructorExpression(ProgramID pid, - const Source& src, - const Literal* literal); - /// Move constructor - ScalarConstructorExpression(ScalarConstructorExpression&&); - ~ScalarConstructorExpression() override; - - /// Clones this node and all transitive child nodes using the `CloneContext` - /// `ctx`. - /// @param ctx the clone context - /// @return the newly cloned node - const ScalarConstructorExpression* Clone(CloneContext* ctx) const override; - - /// The literal value - const Literal* const literal; -}; - -} // namespace ast -} // namespace tint - -#endif // SRC_AST_SCALAR_CONSTRUCTOR_EXPRESSION_H_ diff --git a/src/ast/scalar_constructor_expression_test.cc b/src/ast/scalar_constructor_expression_test.cc deleted file mode 100644 index 1c184d5f56..0000000000 --- a/src/ast/scalar_constructor_expression_test.cc +++ /dev/null @@ -1,49 +0,0 @@ -// 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 "gtest/gtest-spi.h" -#include "src/ast/test_helper.h" - -namespace tint { -namespace ast { -namespace { - -using ScalarConstructorExpressionTest = TestHelper; - -TEST_F(ScalarConstructorExpressionTest, Creation) { - auto* b = create(true); - auto* c = create(b); - EXPECT_EQ(c->literal, b); -} - -TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) { - SetSource(Source{Source::Location{20, 2}}); - auto src = Expr(true)->source; - EXPECT_EQ(src.range.begin.line, 20u); - EXPECT_EQ(src.range.begin.column, 2u); -} - -TEST_F(ScalarConstructorExpressionTest, Assert_DifferentProgramID_Literal) { - EXPECT_FATAL_FAILURE( - { - ProgramBuilder b1; - ProgramBuilder b2; - b1.create(b2.create(true)); - }, - "internal compiler error"); -} - -} // namespace -} // namespace ast -} // namespace tint diff --git a/src/ast/traverse_expressions.h b/src/ast/traverse_expressions.h index 28df9f28ca..97658f3d01 100644 --- a/src/ast/traverse_expressions.h +++ b/src/ast/traverse_expressions.h @@ -21,9 +21,9 @@ #include "src/ast/binary_expression.h" #include "src/ast/bitcast_expression.h" #include "src/ast/call_expression.h" +#include "src/ast/literal.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/phony_expression.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/type_constructor_expression.h" #include "src/ast/unary_op_expression.h" #include "src/utils/reverse.h" @@ -122,8 +122,7 @@ bool TraverseExpressions(const ast::Expression* root, to_visit.push_back(member->structure); } else if (auto* unary = expr->As()) { to_visit.push_back(unary->expr); - } else if (expr->IsAnyOfIsAnyOf()) { // Leaf expression } else { diff --git a/src/ast/workgroup_decoration_test.cc b/src/ast/workgroup_decoration_test.cc index cbf7ee8aef..68b5b22944 100644 --- a/src/ast/workgroup_decoration_test.cc +++ b/src/ast/workgroup_decoration_test.cc @@ -26,11 +26,9 @@ using WorkgroupDecorationTest = TestHelper; TEST_F(WorkgroupDecorationTest, Creation_1param) { auto* d = WorkgroupSize(2); auto values = d->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_TRUE(x_scalar); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); + + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); EXPECT_EQ(values[1], nullptr); EXPECT_EQ(values[2], nullptr); @@ -38,17 +36,12 @@ TEST_F(WorkgroupDecorationTest, Creation_1param) { TEST_F(WorkgroupDecorationTest, Creation_2param) { auto* d = WorkgroupSize(2, 4); auto values = d->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_TRUE(x_scalar); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_TRUE(y_scalar); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); + + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 4u); EXPECT_EQ(values[2], nullptr); } @@ -56,42 +49,28 @@ TEST_F(WorkgroupDecorationTest, Creation_2param) { TEST_F(WorkgroupDecorationTest, Creation_3param) { auto* d = WorkgroupSize(2, 4, 6); auto values = d->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_TRUE(x_scalar); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_TRUE(y_scalar); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); - ASSERT_NE(values[2], nullptr); - auto* z_scalar = values[2]->As(); - ASSERT_TRUE(z_scalar); - ASSERT_TRUE(z_scalar->literal->Is()); - EXPECT_EQ(z_scalar->literal->As()->ValueAsU32(), 6u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 4u); + + ASSERT_TRUE(values[2]->Is()); + EXPECT_EQ(values[2]->As()->ValueAsU32(), 6u); } TEST_F(WorkgroupDecorationTest, Creation_WithIdentifier) { auto* d = WorkgroupSize(2, 4, "depth"); auto values = d->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_TRUE(x_scalar); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_TRUE(y_scalar); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); - ASSERT_NE(values[2], nullptr); - auto* z_ident = values[2]->As(); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 4u); + + auto* z_ident = As(values[2]); ASSERT_TRUE(z_ident); EXPECT_EQ(Symbols().NameFor(z_ident->symbol), "depth"); } diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index b602c0aa70..0ef287254e 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -24,7 +24,6 @@ #include "src/ast/location_decoration.h" #include "src/ast/module.h" #include "src/ast/override_decoration.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/sint_literal.h" #include "src/ast/uint_literal.h" #include "src/sem/array.h" @@ -264,23 +263,7 @@ std::map Inspector::GetConstantIDs() { continue; } - auto* expression = var->constructor; - auto* constructor = expression->As(); - if (constructor == nullptr) { - // This is invalid WGSL, but handling gracefully. - result[constant_id] = Scalar(); - continue; - } - - auto* scalar_constructor = - constructor->As(); - if (scalar_constructor == nullptr) { - // This is invalid WGSL, but handling gracefully. - result[constant_id] = Scalar(); - continue; - } - - auto* literal = scalar_constructor->literal; + auto* literal = var->constructor->As(); if (!literal) { // This is invalid WGSL, but handling gracefully. result[constant_id] = Scalar(); diff --git a/src/program_builder.cc b/src/program_builder.cc index 7bea10bf00..f20b76a587 100644 --- a/src/program_builder.cc +++ b/src/program_builder.cc @@ -114,10 +114,6 @@ const ast::TypeName* ProgramBuilder::TypesBuilder::Of( ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {} -const ast::Statement* ProgramBuilder::WrapInStatement(const ast::Literal* lit) { - return WrapInStatement(create(lit)); -} - const ast::Statement* ProgramBuilder::WrapInStatement( const ast::Expression* expr) { if (auto* ce = expr->As()) { diff --git a/src/program_builder.h b/src/program_builder.h index abe9a6e99b..2c139809da 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -55,7 +55,6 @@ #include "src/ast/return_statement.h" #include "src/ast/sampled_texture.h" #include "src/ast/sampler.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/sint_literal.h" #include "src/ast/stage_decoration.h" #include "src/ast/storage_texture.h" @@ -1038,57 +1037,53 @@ class ProgramBuilder { /// @param source the source information /// @param value the boolean value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(const Source& source, - bool value) { - return create(source, Literal(value)); + const ast::Literal* Expr(const Source& source, bool value) { + return create(source, value); } /// @param value the boolean value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(bool value) { - return create(Literal(value)); + const ast::BoolLiteral* Expr(bool value) { + return create(value); } /// @param source the source information /// @param value the float value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(const Source& source, - f32 value) { - return create(source, Literal(value)); + const ast::FloatLiteral* Expr(const Source& source, f32 value) { + return create(source, value); } /// @param value the float value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(f32 value) { - return create(Literal(value)); + const ast::FloatLiteral* Expr(f32 value) { + return create(value); } /// @param source the source information /// @param value the integer value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(const Source& source, - i32 value) { - return create(source, Literal(value)); + const ast::Literal* Expr(const Source& source, i32 value) { + return create(source, value); } /// @param value the integer value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(i32 value) { - return create(Literal(value)); + const ast::SintLiteral* Expr(i32 value) { + return create(value); } /// @param source the source information /// @param value the unsigned int value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(const Source& source, - u32 value) { - return create(source, Literal(value)); + const ast::UintLiteral* Expr(const Source& source, u32 value) { + return create(source, value); } /// @param value the unsigned int value /// @return a Scalar constructor for the given value - const ast::ScalarConstructorExpression* Expr(u32 value) { - return create(Literal(value)); + const ast::UintLiteral* Expr(u32 value) { + return create(value); } /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to @@ -2502,12 +2497,6 @@ class ProgramBuilder { /// the type declaration has no resolved type. const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const; - /// Wraps the ast::Literal in a statement. This is used by tests that - /// construct a partial AST and require the Resolver to reach these - /// nodes. - /// @param lit the ast::Literal to be wrapped by an ast::Statement - /// @return the ast::Statement that wraps the ast::Statement - const ast::Statement* WrapInStatement(const ast::Literal* lit); /// Wraps the ast::Expression in a statement. This is used by tests that /// construct a partial AST and require the Resolver to reach these /// nodes. diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index 9bea3afd44..21e51aee3a 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -2548,9 +2548,7 @@ TypedExpression FunctionEmitter::MakeExpression(uint32_t id) { return source_expr; } case SkipReason::kPointSizeBuiltinValue: { - return {ty_.F32(), - create( - Source{}, create(Source{}, 1.0f))}; + return {ty_.F32(), create(Source{}, 1.0f)}; } case SkipReason::kPointSizeBuiltinPointer: Fail() << "unhandled use of a pointer to the PointSize builtin, with ID: " @@ -4477,8 +4475,7 @@ TypedExpression FunctionEmitter::MakeCompositeValueDecomposition( auto current_type_id = composite_type_id; auto make_index = [this](uint32_t literal) { - return create( - Source{}, create(Source{}, literal)); + return create(Source{}, literal); }; // Build up a nested expression for the decomposition by walking down the type @@ -4594,13 +4591,11 @@ TypedExpression FunctionEmitter::MakeCompositeValueDecomposition( } const ast::Expression* FunctionEmitter::MakeTrue(const Source& source) const { - return create( - source, create(source, true)); + return create(source, true); } const ast::Expression* FunctionEmitter::MakeFalse(const Source& source) const { - return create( - source, create(source, false)); + return create(source, false); } TypedExpression FunctionEmitter::MakeVectorShuffle( diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 65a57c9825..6ad62fd88d 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -1353,34 +1353,30 @@ bool ParserImpl::EmitScalarSpecConstants() { for (auto& inst : module_->types_values()) { // These will be populated for a valid scalar spec constant. const Type* ast_type = nullptr; - ast::ScalarConstructorExpression* ast_expr = nullptr; + ast::Literal* ast_expr = nullptr; switch (inst.opcode()) { case SpvOpSpecConstantTrue: case SpvOpSpecConstantFalse: { ast_type = ConvertType(inst.type_id()); - ast_expr = create( - Source{}, create( - Source{}, inst.opcode() == SpvOpSpecConstantTrue)); + ast_expr = create( + Source{}, inst.opcode() == SpvOpSpecConstantTrue); break; } case SpvOpSpecConstant: { ast_type = ConvertType(inst.type_id()); const uint32_t literal_value = inst.GetSingleWordInOperand(0); if (ast_type->Is()) { - ast_expr = create( - Source{}, create( - Source{}, static_cast(literal_value))); + ast_expr = create( + Source{}, static_cast(literal_value)); } else if (ast_type->Is()) { - ast_expr = create( - Source{}, create( - Source{}, static_cast(literal_value))); + ast_expr = create( + Source{}, static_cast(literal_value)); } else if (ast_type->Is()) { float float_value; // Copy the bits so we can read them as a float. std::memcpy(&float_value, &literal_value, sizeof(float_value)); - ast_expr = create( - Source{}, create(Source{}, float_value)); + ast_expr = create(Source{}, float_value); } else { return Fail() << " invalid result type for OpSpecConstant " << inst.PrettyPrint(); @@ -1981,26 +1977,20 @@ TypedExpression ParserImpl::MakeConstantExpressionForScalarSpirvConstant( // Currently "null" is missing from the WGSL parser. // See https://bugs.chromium.org/p/tint/issues/detail?id=34 if (ast_type->Is()) { - return {ty_.U32(), create( - Source{}, create( - source, spirv_const->GetU32()))}; + return {ty_.U32(), create(source, spirv_const->GetU32())}; } if (ast_type->Is()) { - return {ty_.I32(), create( - Source{}, create( - source, spirv_const->GetS32()))}; + return {ty_.I32(), create(source, spirv_const->GetS32())}; } if (ast_type->Is()) { - return {ty_.F32(), create( - Source{}, create( - source, spirv_const->GetFloat()))}; + return {ty_.F32(), + create(source, spirv_const->GetFloat())}; } if (ast_type->Is()) { const bool value = spirv_const->AsNullConstant() ? false : spirv_const->AsBoolConstant()->value(); - return {ty_.Bool(), create( - Source{}, create(source, value))}; + return {ty_.Bool(), create(source, value)}; } Fail() << "expected scalar constant"; return {}; @@ -2021,20 +2011,16 @@ const ast::Expression* ParserImpl::MakeNullValue(const Type* type) { type = type->UnwrapAlias(); if (type->Is()) { - return create( - Source{}, create(Source{}, false)); + return create(Source{}, false); } if (type->Is()) { - return create( - Source{}, create(Source{}, 0u)); + return create(Source{}, 0u); } if (type->Is()) { - return create( - Source{}, create(Source{}, 0)); + return create(Source{}, 0); } if (type->Is()) { - return create( - Source{}, create(Source{}, 0.0f)); + return create(Source{}, 0.0f); } if (type->Is()) { // TODO(amaiorano): No type constructor for TypeName (yet?) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index fdcb027cde..8e904bf52d 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -521,11 +521,12 @@ Maybe ParserImpl::global_constant_decl( if (decl.errored) return Failure::kErrored; - ast::ConstructorExpression* initializer = nullptr; + const ast::Expression* initializer = nullptr; if (match(Token::Type::kEqual)) { auto init = expect_const_expr(); - if (init.errored) + if (init.errored) { return Failure::kErrored; + } initializer = std::move(init.value); } @@ -2175,15 +2176,18 @@ Maybe ParserImpl::primary_expression() { auto source = t.source(); auto lit = const_literal(); - if (lit.errored) + if (lit.errored) { return Failure::kErrored; - if (lit.matched) - return create(source, lit.value); + } + if (lit.matched) { + return lit.value; + } if (t.Is(Token::Type::kParenLeft)) { auto paren = expect_paren_rhs_stmt(); - if (paren.errored) + if (paren.errored) { return Failure::kErrored; + } return paren.value; } @@ -2869,23 +2873,25 @@ Maybe ParserImpl::const_literal() { // const_expr // : type_decl PAREN_LEFT ((const_expr COMMA)? const_expr COMMA?)? PAREN_RIGHT // | const_literal -Expect ParserImpl::expect_const_expr() { +Expect ParserImpl::expect_const_expr() { auto t = peek(); auto source = t.source(); if (t.IsLiteral()) { auto lit = const_literal(); - if (lit.errored) + if (lit.errored) { return Failure::kErrored; - if (!lit.matched) + } + if (!lit.matched) { return add_error(peek(), "unable to parse constant literal"); - - return create(source, lit.value); + } + return lit.value; } else if (!t.IsIdentifier() || get_type(t.to_str())) { if (peek_is(Token::Type::kParenLeft, 1) || peek_is(Token::Type::kLessThan, 1)) { auto type = expect_type("const_expr"); - if (type.errored) + if (type.errored) { return Failure::kErrored; + } auto params = expect_paren_block( "type constructor", [&]() -> Expect { diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index 40335aa0fc..2da91e240e 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -571,7 +571,7 @@ class ParserImpl { Maybe const_literal(); /// Parses a `const_expr` grammar element, erroring on parse failure. /// @returns the parsed constructor expression or nullptr on error - Expect expect_const_expr(); + Expect expect_const_expr(); /// Parses a `primary_expression` grammar element /// @returns the parsed expression or nullptr Maybe primary_expression(); diff --git a/src/reader/wgsl/parser_impl_additive_expression_test.cc b/src/reader/wgsl/parser_impl_additive_expression_test.cc index f6643d4dec..fb66a736dd 100644 --- a/src/reader/wgsl/parser_impl_additive_expression_test.cc +++ b/src/reader/wgsl/parser_impl_additive_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Plus) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) { @@ -58,11 +55,8 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, AdditiveExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_and_expression_test.cc b/src/reader/wgsl/parser_impl_and_expression_test.cc index 64f715ca03..73aa8dea68 100644 --- a/src/reader/wgsl/parser_impl_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_and_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, AndExpression_Parses) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, AndExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_argument_expression_list_test.cc b/src/reader/wgsl/parser_impl_argument_expression_list_test.cc index bb73660bb7..575ca439f4 100644 --- a/src/reader/wgsl/parser_impl_argument_expression_list_test.cc +++ b/src/reader/wgsl/parser_impl_argument_expression_list_test.cc @@ -46,7 +46,7 @@ TEST_F(ParserImplTest, ArgumentExpressionList_ParsesMultiple) { ASSERT_EQ(e.value.size(), 3u); ASSERT_TRUE(e.value[0]->Is()); - ASSERT_TRUE(e.value[1]->Is()); + ASSERT_TRUE(e.value[1]->Is()); ASSERT_TRUE(e.value[2]->Is()); } @@ -58,7 +58,7 @@ TEST_F(ParserImplTest, ArgumentExpressionList_TrailingComma) { ASSERT_EQ(e.value.size(), 2u); ASSERT_TRUE(e.value[0]->Is()); - ASSERT_TRUE(e.value[1]->Is()); + ASSERT_TRUE(e.value[1]->Is()); } TEST_F(ParserImplTest, ArgumentExpressionList_HandlesMissingLeftParen) { diff --git a/src/reader/wgsl/parser_impl_assignment_stmt_test.cc b/src/reader/wgsl/parser_impl_assignment_stmt_test.cc index 7fe7097cac..38cdc7c1c3 100644 --- a/src/reader/wgsl/parser_impl_assignment_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_assignment_stmt_test.cc @@ -35,13 +35,9 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToVariable) { auto* ident = e->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(e->rhs->Is()); - ASSERT_TRUE(e->rhs->Is()); - - auto* init = e->rhs->As(); - ASSERT_NE(init->literal, nullptr); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 123); + ASSERT_NE(e->rhs, nullptr); + ASSERT_TRUE(e->rhs->Is()); + EXPECT_EQ(e->rhs->As()->value, 123); } TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) { @@ -56,12 +52,9 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) { ASSERT_NE(e->lhs, nullptr); ASSERT_NE(e->rhs, nullptr); - ASSERT_TRUE(e->rhs->Is()); - ASSERT_TRUE(e->rhs->Is()); - auto* init = e->rhs->As(); - ASSERT_NE(init->literal, nullptr); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 123); + ASSERT_NE(e->rhs, nullptr); + ASSERT_TRUE(e->rhs->Is()); + EXPECT_EQ(e->rhs->As()->value, 123); ASSERT_TRUE(e->lhs->Is()); auto* mem = e->lhs->As(); @@ -73,12 +66,9 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) { ASSERT_TRUE(mem->structure->Is()); auto* ary = mem->structure->As(); - ASSERT_TRUE(ary->index->Is()); - ASSERT_TRUE(ary->index->Is()); - init = ary->index->As(); - ASSERT_NE(init->literal, nullptr); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 2); + ASSERT_NE(ary->index, nullptr); + ASSERT_TRUE(ary->index->Is()); + EXPECT_EQ(ary->index->As()->value, 2); ASSERT_TRUE(ary->array->Is()); mem = ary->array->As(); @@ -110,12 +100,9 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToPhony) { ASSERT_NE(e->lhs, nullptr); ASSERT_NE(e->rhs, nullptr); - ASSERT_TRUE(e->rhs->Is()); - ASSERT_TRUE(e->rhs->Is()); - auto* init = e->rhs->As(); - ASSERT_NE(init->literal, nullptr); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 123); + ASSERT_NE(e->rhs, nullptr); + ASSERT_TRUE(e->rhs->Is()); + EXPECT_EQ(e->rhs->As()->value, 123); ASSERT_TRUE(e->lhs->Is()); } diff --git a/src/reader/wgsl/parser_impl_call_stmt_test.cc b/src/reader/wgsl/parser_impl_call_stmt_test.cc index e38774f869..edd81424a0 100644 --- a/src/reader/wgsl/parser_impl_call_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_call_stmt_test.cc @@ -55,7 +55,7 @@ TEST_F(ParserImplTest, Statement_Call_WithParams) { EXPECT_EQ(c->func->symbol, p->builder().Symbols().Get("a")); EXPECT_EQ(c->args.size(), 3u); - EXPECT_TRUE(c->args[0]->Is()); + EXPECT_TRUE(c->args[0]->Is()); EXPECT_TRUE(c->args[1]->Is()); EXPECT_TRUE(c->args[2]->Is()); } @@ -74,7 +74,7 @@ TEST_F(ParserImplTest, Statement_Call_WithParams_TrailingComma) { EXPECT_EQ(c->func->symbol, p->builder().Symbols().Get("a")); EXPECT_EQ(c->args.size(), 2u); - EXPECT_TRUE(c->args[0]->Is()); + EXPECT_TRUE(c->args[0]->Is()); EXPECT_TRUE(c->args[1]->Is()); } diff --git a/src/reader/wgsl/parser_impl_const_expr_test.cc b/src/reader/wgsl/parser_impl_const_expr_test.cc index a37f6e0b2e..62e5e3ff96 100644 --- a/src/reader/wgsl/parser_impl_const_expr_test.cc +++ b/src/reader/wgsl/parser_impl_const_expr_test.cc @@ -34,17 +34,11 @@ TEST_F(ParserImplTest, ConstExpr_TypeDecl) { ASSERT_EQ(t->values.size(), 2u); auto& v = t->values; - ASSERT_TRUE(v[0]->Is()); - ASSERT_TRUE(v[0]->Is()); - auto* c = v[0]->As(); - ASSERT_TRUE(c->literal->Is()); - EXPECT_FLOAT_EQ(c->literal->As()->value, 1.); + ASSERT_TRUE(v[0]->Is()); + EXPECT_FLOAT_EQ(v[0]->As()->value, 1.); - ASSERT_TRUE(v[1]->Is()); - ASSERT_TRUE(v[1]->Is()); - c = v[1]->As(); - ASSERT_TRUE(c->literal->Is()); - EXPECT_FLOAT_EQ(c->literal->As()->value, 2.); + ASSERT_TRUE(v[1]->Is()); + EXPECT_FLOAT_EQ(v[1]->As()->value, 2.); } TEST_F(ParserImplTest, ConstExpr_TypeDecl_Empty) { @@ -75,8 +69,8 @@ TEST_F(ParserImplTest, ConstExpr_TypeDecl_TrailingComma) { EXPECT_EQ(t->type->As()->width, 2u); ASSERT_EQ(t->values.size(), 2u); - ASSERT_TRUE(t->values[0]->Is()); - ASSERT_TRUE(t->values[1]->Is()); + ASSERT_TRUE(t->values[0]->Is()); + ASSERT_TRUE(t->values[1]->Is()); } TEST_F(ParserImplTest, ConstExpr_TypeDecl_MissingRightParen) { @@ -121,11 +115,8 @@ TEST_F(ParserImplTest, ConstExpr_ConstLiteral) { ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(e.errored); ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); - ASSERT_TRUE(e->Is()); - auto* c = e->As(); - ASSERT_TRUE(c->literal->Is()); - EXPECT_TRUE(c->literal->As()->value); + ASSERT_TRUE(e.value->Is()); + EXPECT_TRUE(e.value->As()->value); } TEST_F(ParserImplTest, ConstExpr_ConstLiteral_Invalid) { diff --git a/src/reader/wgsl/parser_impl_equality_expression_test.cc b/src/reader/wgsl/parser_impl_equality_expression_test.cc index 8350e96f96..72db3a6f5e 100644 --- a/src/reader/wgsl/parser_impl_equality_expression_test.cc +++ b/src/reader/wgsl/parser_impl_equality_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_Equal) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, EqualityExpression_Parses_NotEqual) { @@ -58,11 +55,8 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_NotEqual) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, EqualityExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc index bfcbf9467e..6fbf186fb6 100644 --- a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, ExclusiveOrExpression_Parses) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, ExclusiveOrExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_function_decl_test.cc b/src/reader/wgsl/parser_impl_function_decl_test.cc index 2ec9a8d7ad..67131b895c 100644 --- a/src/reader/wgsl/parser_impl_function_decl_test.cc +++ b/src/reader/wgsl/parser_impl_function_decl_test.cc @@ -71,23 +71,14 @@ TEST_F(ParserImplTest, FunctionDecl_DecorationList) { auto values = decorations[0]->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_NE(y_scalar, nullptr); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 3u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 3u); - ASSERT_NE(values[2], nullptr); - auto* z_scalar = values[2]->As(); - ASSERT_NE(z_scalar, nullptr); - ASSERT_TRUE(z_scalar->literal->Is()); - EXPECT_EQ(z_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[2]->Is()); + EXPECT_EQ(values[2]->As()->ValueAsU32(), 4u); auto* body = f->body; ASSERT_EQ(body->statements.size(), 1u); @@ -119,23 +110,14 @@ fn main() { return; })"); ASSERT_TRUE(decorations[0]->Is()); auto values = decorations[0]->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_NE(y_scalar, nullptr); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 3u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 3u); - ASSERT_NE(values[2], nullptr); - auto* z_scalar = values[2]->As(); - ASSERT_NE(z_scalar, nullptr); - ASSERT_TRUE(z_scalar->literal->Is()); - EXPECT_EQ(z_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[2]->Is()); + EXPECT_EQ(values[2]->As()->ValueAsU32(), 4u); ASSERT_TRUE(decorations[1]->Is()); EXPECT_EQ(decorations[1]->As()->stage, @@ -172,23 +154,14 @@ fn main() { return; })"); ASSERT_TRUE(decos[0]->Is()); auto values = decos[0]->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 2u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_NE(y_scalar, nullptr); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 3u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 3u); - ASSERT_NE(values[2], nullptr); - auto* z_scalar = values[2]->As(); - ASSERT_NE(z_scalar, nullptr); - ASSERT_TRUE(z_scalar->literal->Is()); - EXPECT_EQ(z_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[2]->Is()); + EXPECT_EQ(values[2]->As()->ValueAsU32(), 4u); ASSERT_TRUE(decos[1]->Is()); EXPECT_EQ(decos[1]->As()->stage, diff --git a/src/reader/wgsl/parser_impl_function_decoration_list_test.cc b/src/reader/wgsl/parser_impl_function_decoration_list_test.cc index 1a5924a2cb..0b681c01ba 100644 --- a/src/reader/wgsl/parser_impl_function_decoration_list_test.cc +++ b/src/reader/wgsl/parser_impl_function_decoration_list_test.cc @@ -36,10 +36,10 @@ TEST_F(ParserImplTest, DecorationList_Parses) { ASSERT_TRUE(deco_0->Is()); const ast::Expression* x = deco_0->As()->x; ASSERT_NE(x, nullptr); - auto* x_scalar = x->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 2u); + auto* x_literal = x->As(); + ASSERT_NE(x_literal, nullptr); + ASSERT_TRUE(x_literal->Is()); + EXPECT_EQ(x_literal->As()->ValueAsU32(), 2u); ASSERT_TRUE(deco_1->Is()); EXPECT_EQ(deco_1->As()->stage, diff --git a/src/reader/wgsl/parser_impl_function_decoration_test.cc b/src/reader/wgsl/parser_impl_function_decoration_test.cc index 2fe6994122..d63d2721e1 100644 --- a/src/reader/wgsl/parser_impl_function_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_function_decoration_test.cc @@ -34,11 +34,8 @@ TEST_F(ParserImplTest, Decoration_Workgroup) { auto values = func_deco->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 4u); EXPECT_EQ(values[1], nullptr); EXPECT_EQ(values[2], nullptr); @@ -57,17 +54,11 @@ TEST_F(ParserImplTest, Decoration_Workgroup_2Param) { auto values = func_deco->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 4u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_NE(y_scalar, nullptr); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 5u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 5u); EXPECT_EQ(values[2], nullptr); } @@ -85,23 +76,14 @@ TEST_F(ParserImplTest, Decoration_Workgroup_3Param) { auto values = func_deco->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 4u); - ASSERT_NE(values[1], nullptr); - auto* y_scalar = values[1]->As(); - ASSERT_NE(y_scalar, nullptr); - ASSERT_TRUE(y_scalar->literal->Is()); - EXPECT_EQ(y_scalar->literal->As()->ValueAsU32(), 5u); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->ValueAsU32(), 5u); - ASSERT_NE(values[2], nullptr); - auto* z_scalar = values[2]->As(); - ASSERT_NE(z_scalar, nullptr); - ASSERT_TRUE(z_scalar->literal->Is()); - EXPECT_EQ(z_scalar->literal->As()->ValueAsU32(), 6u); + ASSERT_TRUE(values[2]->Is()); + EXPECT_EQ(values[2]->As()->ValueAsU32(), 6u); } TEST_F(ParserImplTest, Decoration_Workgroup_WithIdent) { @@ -117,11 +99,8 @@ TEST_F(ParserImplTest, Decoration_Workgroup_WithIdent) { auto values = func_deco->As()->Values(); - ASSERT_NE(values[0], nullptr); - auto* x_scalar = values[0]->As(); - ASSERT_NE(x_scalar, nullptr); - ASSERT_TRUE(x_scalar->literal->Is()); - EXPECT_EQ(x_scalar->literal->As()->ValueAsU32(), 4u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->ValueAsU32(), 4u); ASSERT_NE(values[1], nullptr); auto* y_ident = values[1]->As(); diff --git a/src/reader/wgsl/parser_impl_global_constant_decl_test.cc b/src/reader/wgsl/parser_impl_global_constant_decl_test.cc index 308bd70b6b..29fe7954ad 100644 --- a/src/reader/wgsl/parser_impl_global_constant_decl_test.cc +++ b/src/reader/wgsl/parser_impl_global_constant_decl_test.cc @@ -42,7 +42,7 @@ TEST_F(ParserImplTest, GlobalConstantDecl) { EXPECT_EQ(e->source.range.end.column, 6u); ASSERT_NE(e->constructor, nullptr); - EXPECT_TRUE(e->constructor->Is()); + EXPECT_TRUE(e->constructor->Is()); EXPECT_FALSE( ast::HasDecoration(e.value->decorations)); @@ -69,7 +69,7 @@ TEST_F(ParserImplTest, GlobalConstantDecl_Inferred) { EXPECT_EQ(e->source.range.end.column, 6u); ASSERT_NE(e->constructor, nullptr); - EXPECT_TRUE(e->constructor->Is()); + EXPECT_TRUE(e->constructor->Is()); EXPECT_FALSE( ast::HasDecoration(e.value->decorations)); @@ -137,7 +137,7 @@ TEST_F(ParserImplTest, GlobalConstantDec_Override_WithId) { EXPECT_EQ(e->source.range.end.column, 22u); ASSERT_NE(e->constructor, nullptr); - EXPECT_TRUE(e->constructor->Is()); + EXPECT_TRUE(e->constructor->Is()); auto* override_deco = ast::GetDecoration(e.value->decorations); @@ -169,7 +169,7 @@ TEST_F(ParserImplTest, GlobalConstantDec_Override_WithoutId) { EXPECT_EQ(e->source.range.end.column, 19u); ASSERT_NE(e->constructor, nullptr); - EXPECT_TRUE(e->constructor->Is()); + EXPECT_TRUE(e->constructor->Is()); auto* override_deco = ast::GetDecoration(e.value->decorations); diff --git a/src/reader/wgsl/parser_impl_global_variable_decl_test.cc b/src/reader/wgsl/parser_impl_global_variable_decl_test.cc index c7c563066d..9823c3f555 100644 --- a/src/reader/wgsl/parser_impl_global_variable_decl_test.cc +++ b/src/reader/wgsl/parser_impl_global_variable_decl_test.cc @@ -63,8 +63,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithConstructor) { EXPECT_EQ(e->source.range.end.column, 15u); ASSERT_NE(e->constructor, nullptr); - ASSERT_TRUE(e->constructor->Is()); - ASSERT_TRUE(e->constructor->Is()); + ASSERT_TRUE(e->constructor->Is()); } TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) { diff --git a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc index 15b04aaffd..5f6d5da161 100644 --- a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, InclusiveOrExpression_Parses) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, InclusiveOrExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc index c2bbb2bfc2..b394558538 100644 --- a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, LogicalAndExpression_Parses) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, LogicalAndExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc index 320bad6c17..5b1d8842c7 100644 --- a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, LogicalOrExpression_Parses) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, LogicalOrExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc index ad860475d0..c2ab2f17bd 100644 --- a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc +++ b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Multiply) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Divide) { @@ -58,11 +55,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Divide) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Modulo) { @@ -81,11 +75,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Modulo) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, MultiplicativeExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_primary_expression_test.cc b/src/reader/wgsl/parser_impl_primary_expression_test.cc index 4a23abc78d..1d44f16ae4 100644 --- a/src/reader/wgsl/parser_impl_primary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_primary_expression_test.cc @@ -45,29 +45,17 @@ TEST_F(ParserImplTest, PrimaryExpression_TypeDecl) { ASSERT_EQ(ty->values.size(), 4u); const auto& val = ty->values; - ASSERT_TRUE(val[0]->Is()); - ASSERT_TRUE(val[0]->Is()); - auto* ident = val[0]->As(); - ASSERT_TRUE(ident->literal->Is()); - EXPECT_EQ(ident->literal->As()->value, 1); + ASSERT_TRUE(val[0]->Is()); + EXPECT_EQ(val[0]->As()->value, 1); - ASSERT_TRUE(val[1]->Is()); - ASSERT_TRUE(val[1]->Is()); - ident = val[1]->As(); - ASSERT_TRUE(ident->literal->Is()); - EXPECT_EQ(ident->literal->As()->value, 2); + ASSERT_TRUE(val[1]->Is()); + EXPECT_EQ(val[1]->As()->value, 2); - ASSERT_TRUE(val[2]->Is()); - ASSERT_TRUE(val[2]->Is()); - ident = val[2]->As(); - ASSERT_TRUE(ident->literal->Is()); - EXPECT_EQ(ident->literal->As()->value, 3); + ASSERT_TRUE(val[2]->Is()); + EXPECT_EQ(val[2]->As()->value, 3); - ASSERT_TRUE(val[3]->Is()); - ASSERT_TRUE(val[3]->Is()); - ident = val[3]->As(); - ASSERT_TRUE(ident->literal->Is()); - EXPECT_EQ(ident->literal->As()->value, 4); + ASSERT_TRUE(val[3]->Is()); + EXPECT_EQ(val[3]->As()->value, 4); } TEST_F(ParserImplTest, PrimaryExpression_TypeDecl_ZeroConstructor) { @@ -173,15 +161,11 @@ TEST_F(ParserImplTest, PrimaryExpression_TypeDecl_StructConstructor_NotEmpty) { auto values = constructor->values; ASSERT_EQ(values.size(), 2u); - ASSERT_TRUE(values[0]->Is()); - auto* val0 = values[0]->As(); - ASSERT_TRUE(val0->literal->Is()); - EXPECT_EQ(val0->literal->As()->value, 1u); + ASSERT_TRUE(values[0]->Is()); + EXPECT_EQ(values[0]->As()->value, 1u); - ASSERT_TRUE(values[1]->Is()); - auto* val1 = values[1]->As(); - ASSERT_TRUE(val1->literal->Is()); - EXPECT_EQ(val1->literal->As()->value, 2.f); + ASSERT_TRUE(values[1]->Is()); + EXPECT_EQ(values[1]->As()->value, 2.f); } TEST_F(ParserImplTest, PrimaryExpression_ConstLiteral_True) { @@ -191,11 +175,8 @@ TEST_F(ParserImplTest, PrimaryExpression_ConstLiteral_True) { EXPECT_FALSE(e.errored); EXPECT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); - ASSERT_TRUE(e->Is()); - auto* init = e->As(); - ASSERT_TRUE(init->literal->Is()); - EXPECT_TRUE(init->literal->As()->value); + ASSERT_TRUE(e->Is()); + EXPECT_TRUE(e->As()->value); } TEST_F(ParserImplTest, PrimaryExpression_ParenExpr) { @@ -253,8 +234,7 @@ TEST_F(ParserImplTest, PrimaryExpression_Cast) { ASSERT_TRUE(c->type->Is()); ASSERT_EQ(c->values.size(), 1u); - ASSERT_TRUE(c->values[0]->Is()); - ASSERT_TRUE(c->values[0]->Is()); + ASSERT_TRUE(c->values[0]->Is()); } TEST_F(ParserImplTest, PrimaryExpression_Bitcast) { @@ -269,8 +249,7 @@ TEST_F(ParserImplTest, PrimaryExpression_Bitcast) { auto* c = e->As(); ASSERT_TRUE(c->type->Is()); - ASSERT_TRUE(c->expr->Is()); - ASSERT_TRUE(c->expr->Is()); + ASSERT_TRUE(c->expr->Is()); } TEST_F(ParserImplTest, PrimaryExpression_Bitcast_MissingGreaterThan) { diff --git a/src/reader/wgsl/parser_impl_relational_expression_test.cc b/src/reader/wgsl/parser_impl_relational_expression_test.cc index 2bbfcbfe2b..290fec3b36 100644 --- a/src/reader/wgsl/parser_impl_relational_expression_test.cc +++ b/src/reader/wgsl/parser_impl_relational_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThan) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThan) { @@ -58,11 +55,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThan) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, RelationalExpression_Parses_LessThanEqual) { @@ -81,11 +75,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThanEqual) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThanEqual) { @@ -104,11 +95,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThanEqual) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, RelationalExpression_InvalidLHS) { diff --git a/src/reader/wgsl/parser_impl_shift_expression_test.cc b/src/reader/wgsl/parser_impl_shift_expression_test.cc index 58675d6b0c..1b6e73beba 100644 --- a/src/reader/wgsl/parser_impl_shift_expression_test.cc +++ b/src/reader/wgsl/parser_impl_shift_expression_test.cc @@ -35,11 +35,8 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) { @@ -58,11 +55,8 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) { auto* ident = rel->lhs->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->Is()); - auto* init = rel->rhs->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_TRUE(init->literal->As()->value); + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); } TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceLeft) { diff --git a/src/reader/wgsl/parser_impl_singular_expression_test.cc b/src/reader/wgsl/parser_impl_singular_expression_test.cc index 50afbb07c8..10c0e13a59 100644 --- a/src/reader/wgsl/parser_impl_singular_expression_test.cc +++ b/src/reader/wgsl/parser_impl_singular_expression_test.cc @@ -34,11 +34,8 @@ TEST_F(ParserImplTest, SingularExpression_Array_ConstantIndex) { auto* ident = ary->array->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(ary->index->Is()); - ASSERT_TRUE(ary->index->Is()); - auto* c = ary->index->As(); - ASSERT_TRUE(c->literal->Is()); - EXPECT_EQ(c->literal->As()->value, 1); + ASSERT_TRUE(ary->index->Is()); + EXPECT_EQ(ary->index->As()->value, 1); } TEST_F(ParserImplTest, SingularExpression_Array_ExpressionIndex) { @@ -119,7 +116,7 @@ TEST_F(ParserImplTest, SingularExpression_Call_WithArgs) { EXPECT_EQ(c->func->symbol, p->builder().Symbols().Get("test")); EXPECT_EQ(c->args.size(), 3u); - EXPECT_TRUE(c->args[0]->Is()); + EXPECT_TRUE(c->args[0]->Is()); EXPECT_TRUE(c->args[1]->Is()); EXPECT_TRUE(c->args[2]->Is()); } diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index 2035359233..957ab0793a 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -455,9 +455,7 @@ TEST_F(ParserImplTest, TypeDecl_Array_SintLiteralSize) { EXPECT_EQ(a->decorations.size(), 0u); EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 14u}})); - auto* count_expr = a->count->As(); - ASSERT_NE(count_expr, nullptr); - auto* size = count_expr->literal->As(); + auto* size = a->count->As(); ASSERT_NE(size, nullptr); EXPECT_EQ(size->ValueAsI32(), 5); } @@ -477,9 +475,7 @@ TEST_F(ParserImplTest, TypeDecl_Array_UintLiteralSize) { EXPECT_EQ(a->decorations.size(), 0u); EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 15u}})); - auto* count_expr = a->count->As(); - ASSERT_NE(count_expr, nullptr); - auto* size = count_expr->literal->As(); + auto* size = a->count->As(); ASSERT_NE(size, nullptr); EXPECT_EQ(size->ValueAsU32(), 5u); } @@ -517,9 +513,7 @@ TEST_F(ParserImplTest, TypeDecl_Array_Stride) { ASSERT_FALSE(a->IsRuntimeArray()); ASSERT_TRUE(a->type->Is()); - auto* count_expr = a->count->As(); - ASSERT_NE(count_expr, nullptr); - auto* size = count_expr->literal->As(); + auto* size = a->count->As(); ASSERT_NE(size, nullptr); EXPECT_EQ(size->ValueAsI32(), 5); diff --git a/src/reader/wgsl/parser_impl_unary_expression_test.cc b/src/reader/wgsl/parser_impl_unary_expression_test.cc index 46273661e0..177a97addb 100644 --- a/src/reader/wgsl/parser_impl_unary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_unary_expression_test.cc @@ -34,11 +34,8 @@ TEST_F(ParserImplTest, UnaryExpression_Postix) { auto* ident = ary->array->As(); EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); - ASSERT_TRUE(ary->index->Is()); - ASSERT_TRUE(ary->index->Is()); - auto* init = ary->index->As(); - ASSERT_TRUE(init->literal->Is()); - ASSERT_EQ(init->literal->As()->value, 2); + ASSERT_TRUE(ary->index->Is()); + ASSERT_EQ(ary->index->As()->value, 2); } TEST_F(ParserImplTest, UnaryExpression_Minus) { @@ -53,12 +50,8 @@ TEST_F(ParserImplTest, UnaryExpression_Minus) { auto* u = e->As(); ASSERT_EQ(u->op, ast::UnaryOp::kNegation); - ASSERT_TRUE(u->expr->Is()); - ASSERT_TRUE(u->expr->Is()); - - auto* init = u->expr->As(); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 1); + ASSERT_TRUE(u->expr->Is()); + EXPECT_EQ(u->expr->As()->value, 1); } TEST_F(ParserImplTest, UnaryExpression_AddressOf) { @@ -139,12 +132,8 @@ TEST_F(ParserImplTest, UnaryExpression_Bang) { auto* u = e->As(); ASSERT_EQ(u->op, ast::UnaryOp::kNot); - ASSERT_TRUE(u->expr->Is()); - ASSERT_TRUE(u->expr->Is()); - - auto* init = u->expr->As(); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 1); + ASSERT_TRUE(u->expr->Is()); + EXPECT_EQ(u->expr->As()->value, 1); } TEST_F(ParserImplTest, UnaryExpression_Bang_InvalidRHS) { @@ -169,12 +158,8 @@ TEST_F(ParserImplTest, UnaryExpression_Tilde) { auto* u = e->As(); ASSERT_EQ(u->op, ast::UnaryOp::kComplement); - ASSERT_TRUE(u->expr->Is()); - ASSERT_TRUE(u->expr->Is()); - - auto* init = u->expr->As(); - ASSERT_TRUE(init->literal->Is()); - EXPECT_EQ(init->literal->As()->value, 1); + ASSERT_TRUE(u->expr->Is()); + EXPECT_EQ(u->expr->As()->value, 1); } TEST_F(ParserImplTest, UnaryExpression_PrefixPlusPlus) { diff --git a/src/reader/wgsl/parser_impl_variable_stmt_test.cc b/src/reader/wgsl/parser_impl_variable_stmt_test.cc index d9ec4ad823..39e5e62064 100644 --- a/src/reader/wgsl/parser_impl_variable_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_variable_stmt_test.cc @@ -55,7 +55,7 @@ TEST_F(ParserImplTest, VariableStmt_VariableDecl_WithInit) { ASSERT_EQ(e->source.range.end.column, 6u); ASSERT_NE(e->variable->constructor, nullptr); - EXPECT_TRUE(e->variable->constructor->Is()); + EXPECT_TRUE(e->variable->constructor->Is()); } TEST_F(ParserImplTest, VariableStmt_VariableDecl_Invalid) { diff --git a/src/resolver/function_validation_test.cc b/src/resolver/function_validation_test.cc index ec44227cff..825b9cae0f 100644 --- a/src/resolver/function_validation_test.cc +++ b/src/resolver/function_validation_test.cc @@ -89,8 +89,7 @@ TEST_F(ResolverFunctionValidationTest, // fn func { var a:i32 = 2; } auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2)); - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, - ty.void_(), + Func(Source{{12, 34}}, "func", ast::VariableList{}, ty.void_(), ast::StatementList{ Decl(var), }); @@ -103,11 +102,11 @@ TEST_F(ResolverFunctionValidationTest, // var foo:f32 = 3.14; // fn foo() -> void {} - auto* global_var = Var(Source{Source::Location{56, 78}}, "foo", ty.f32(), + auto* global_var = Var(Source{{56, 78}}, "foo", ty.f32(), ast::StorageClass::kPrivate, Expr(3.14f)); AST().AddGlobalVariable(global_var); - Func(Source{Source::Location{12, 34}}, "foo", ast::VariableList{}, ty.void_(), + Func(Source{{12, 34}}, "foo", ast::VariableList{}, ty.void_(), ast::StatementList{}, ast::DecorationList{}); EXPECT_FALSE(r()->Resolve()) << r()->error(); @@ -121,9 +120,9 @@ TEST_F(ResolverFunctionValidationTest, // fn foo() -> void {} // var foo:f32 = 3.14; - Func(Source{Source::Location{12, 34}}, "foo", ast::VariableList{}, ty.void_(), + Func(Source{{12, 34}}, "foo", ast::VariableList{}, ty.void_(), ast::StatementList{}, ast::DecorationList{}); - auto* global_var = Var(Source{Source::Location{56, 78}}, "foo", ty.f32(), + auto* global_var = Var(Source{{56, 78}}, "foo", ty.f32(), ast::StorageClass::kPrivate, Expr(3.14f)); AST().AddGlobalVariable(global_var); @@ -143,7 +142,7 @@ TEST_F(ResolverFunctionValidationTest, FunctionUsingSameVariableName_Pass) { Func("func", ast::VariableList{}, ty.i32(), ast::StatementList{ Decl(var), - Return(Source{Source::Location{12, 34}}, Expr("func")), + Return(Source{{12, 34}}, Expr("func")), }, ast::DecorationList{}); @@ -162,7 +161,7 @@ TEST_F(ResolverFunctionValidationTest, }, ast::DecorationList{}); - Func(Source{Source::Location{12, 34}}, "b", ast::VariableList{}, ty.i32(), + Func(Source{{12, 34}}, "b", ast::VariableList{}, ty.i32(), ast::StatementList{ Return(2), }, @@ -244,7 +243,7 @@ TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatement_Fail) { auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2)); - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, ty.i32(), + Func(Source{{12, 34}}, "func", ast::VariableList{}, ty.i32(), ast::StatementList{ Decl(var), }, @@ -259,8 +258,8 @@ TEST_F(ResolverFunctionValidationTest, VoidFunctionEndWithoutReturnStatementEmptyBody_Pass) { // fn func {} - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, - ty.void_(), ast::StatementList{}); + Func(Source{{12, 34}}, "func", ast::VariableList{}, ty.void_(), + ast::StatementList{}); EXPECT_TRUE(r()->Resolve()) << r()->error(); } @@ -269,7 +268,7 @@ TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatementEmptyBody_Fail) { // fn func() -> int {} - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, ty.i32(), + Func(Source{{12, 34}}, "func", ast::VariableList{}, ty.i32(), ast::StatementList{}, ast::DecorationList{}); EXPECT_FALSE(r()->Resolve()); @@ -294,7 +293,7 @@ TEST_F(ResolverFunctionValidationTest, // fn func { return 2; } Func("func", ast::VariableList{}, ty.void_(), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, Expr(2)), + Return(Source{{12, 34}}, Expr(2)), }, ast::DecorationList{}); @@ -311,7 +310,7 @@ TEST_F(ResolverFunctionValidationTest, Func("v", {}, ty.void_(), {Return()}); Func("func", {}, ty.void_(), { - Return(Call(Source{Source::Location{12, 34}}, "v")), + Return(Call(Source{{12, 34}}, "v")), }); EXPECT_FALSE(r()->Resolve()); @@ -323,7 +322,7 @@ TEST_F(ResolverFunctionValidationTest, // fn func() -> f32 { return; } Func("func", ast::VariableList{}, ty.f32(), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, nullptr), + Return(Source{{12, 34}}, nullptr), }, ast::DecorationList{}); @@ -338,7 +337,7 @@ TEST_F(ResolverFunctionValidationTest, // fn func() -> f32 { return 2.0; } Func("func", ast::VariableList{}, ty.f32(), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, Expr(2.f)), + Return(Source{{12, 34}}, Expr(2.f)), }, ast::DecorationList{}); @@ -350,7 +349,7 @@ TEST_F(ResolverFunctionValidationTest, // fn func() -> f32 { return 2; } Func("func", ast::VariableList{}, ty.f32(), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, Expr(2)), + Return(Source{{12, 34}}, Expr(2)), }, ast::DecorationList{}); @@ -367,7 +366,7 @@ TEST_F(ResolverFunctionValidationTest, auto* myf32 = Alias("myf32", ty.f32()); Func("func", ast::VariableList{}, ty.Of(myf32), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, Expr(2.f)), + Return(Source{{12, 34}}, Expr(2.f)), }, ast::DecorationList{}); @@ -381,7 +380,7 @@ TEST_F(ResolverFunctionValidationTest, auto* myf32 = Alias("myf32", ty.f32()); Func("func", ast::VariableList{}, ty.Of(myf32), ast::StatementList{ - Return(Source{Source::Location{12, 34}}, Expr(2u)), + Return(Source{{12, 34}}, Expr(2u)), }, ast::DecorationList{}); @@ -413,8 +412,7 @@ TEST_F(ResolverFunctionValidationTest, PipelineStage_MustBeUnique_Fail) { // [[stage(fragment)]] // [[stage(vertex)]] // fn main() { return; } - Func(Source{Source::Location{12, 34}}, "main", ast::VariableList{}, - ty.void_(), + Func(Source{{12, 34}}, "main", ast::VariableList{}, ty.void_(), ast::StatementList{ Return(), }, @@ -537,7 +535,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_TypeMismatch) { GlobalConst("x", ty.u32(), Expr(64u)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(1), Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(1), Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -554,7 +552,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_TypeMismatch2) { GlobalConst("y", ty.i32(), Expr(32)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr("x"), Expr(Source{Source::Location{12, 34}}, "y"))}); + WorkgroupSize(Expr("x"), Expr(Source{{12, 34}}, "y"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -570,8 +568,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Mismatch_ConstU32) { GlobalConst("y", ty.u32(), Expr(8u)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr("x"), Expr("y"), - Expr(Source{Source::Location{12, 34}}, 16))}); + WorkgroupSize(Expr("x"), Expr("y"), Expr(Source{{12, 34}}, 16))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -585,8 +582,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Literal_BadType) { Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(create( - Source{Source::Location{12, 34}}, Literal(64.f)))}); + WorkgroupSize(Literal(Source{{12, 34}}, 64.f))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -600,8 +596,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Literal_Negative) { Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(create( - Source{Source::Location{12, 34}}, Literal(-2)))}); + WorkgroupSize(Literal(Source{{12, 34}}, -2))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -614,8 +609,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Literal_Zero) { Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(create( - Source{Source::Location{12, 34}}, Literal(0)))}); + WorkgroupSize(Literal(Source{{12, 34}}, 0))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -629,7 +623,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_BadType) { GlobalConst("x", ty.f32(), Expr(64.f)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -644,7 +638,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_Negative) { GlobalConst("x", ty.i32(), Expr(-2)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -658,7 +652,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_Zero) { GlobalConst("x", ty.i32(), Expr(0)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -674,7 +668,7 @@ TEST_F(ResolverFunctionValidationTest, Construct(ty.i32(), Construct(ty.i32(), Construct(ty.i32())))); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -688,7 +682,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_NonConst) { Global("x", ty.i32(), ast::StorageClass::kPrivate, Expr(64)); Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize(Expr(Source{Source::Location{12, 34}}, "x"))}); + WorkgroupSize(Expr(Source{{12, 34}}, "x"))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -701,8 +695,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_InvalidExpr) { // fn main() {} Func("main", {}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute), - WorkgroupSize( - Construct(Source{Source::Location{12, 34}}, ty.i32(), 1))}); + WorkgroupSize(Construct(Source{{12, 34}}, ty.i32(), 1))}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 7808b8f9c3..9dea45fe70 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -2015,7 +2015,7 @@ bool Resolver::WorkgroupSizeFor(const ast::Function* func, ws[i].value = 0; continue; } - } else if (!expr->Is()) { + } else if (!expr->Is()) { AddError( "workgroup_size argument must be either a literal or a " "module-scope constant", @@ -2366,6 +2366,8 @@ sem::Expression* Resolver::Expression(const ast::Expression* root) { sem_expr = Constructor(ctor); } else if (auto* ident = expr->As()) { sem_expr = Identifier(ident); + } else if (auto* literal = expr->As()) { + sem_expr = Literal(literal); } else if (auto* member = expr->As()) { sem_expr = MemberAccessor(member); } else if (auto* unary = expr->As()) { @@ -2421,8 +2423,7 @@ sem::Expression* Resolver::ArrayAccessor( if (!parent_raw_ty->Is()) { // TODO(bclayton): expand this to allow any const_expr expression // https://github.com/gpuweb/gpuweb/issues/1272 - auto* scalar = idx->As(); - if (!scalar || !scalar->literal->As()) { + if (!idx->As()) { AddError("index must be signed or unsigned integer literal", idx->source); return nullptr; @@ -2615,8 +2616,7 @@ bool Resolver::ValidateTextureIntrinsicFunction(const sem::Call* call) { bool is_const_expr = true; ast::TraverseExpressions( arg->Declaration(), diagnostics_, [&](const ast::Expression* e) { - if (e->IsAnyOf()) { + if (e->IsAnyOf()) { return ast::TraverseAction::Descend; } is_const_expr = false; @@ -2763,21 +2763,21 @@ sem::Expression* Resolver::Constructor(const ast::ConstructorExpression* expr) { return builder_->create(expr, ty, current_statement_, val); } - if (auto* scalar_ctor = expr->As()) { - Mark(scalar_ctor->literal); - auto* ty = TypeOf(scalar_ctor->literal); - if (!ty) { - return nullptr; - } - - auto val = EvaluateConstantValue(expr, ty); - return builder_->create(expr, ty, current_statement_, val); - } - TINT_ICE(Resolver, diagnostics_) << "unexpected constructor expression type"; return nullptr; } +sem::Expression* Resolver::Literal(const ast::Literal* literal) { + auto* ty = TypeOf(literal); + if (!ty) { + return nullptr; + } + + auto val = EvaluateConstantValue(literal, ty); + return builder_->create(literal, ty, current_statement_, + val); +} + bool Resolver::ValidateStructureConstructor( const ast::TypeConstructorExpression* ctor, const sem::Struct* struct_type) { @@ -3783,7 +3783,7 @@ sem::Array* Resolver::Array(const ast::Array* arr) { } count_expr = var->Declaration()->constructor; - } else if (!count_expr->Is()) { + } else if (!count_expr->Is()) { AddError( "array size expression must be either a literal or a module-scope " "constant", diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index 5730a55356..b75532eba4 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -177,6 +177,7 @@ class Resolver { sem::Call* FunctionCall(const ast::CallExpression*); sem::Expression* Identifier(const ast::IdentifierExpression*); sem::Call* IntrinsicCall(const ast::CallExpression*, sem::IntrinsicType); + sem::Expression* Literal(const ast::Literal*); sem::Expression* MemberAccessor(const ast::MemberAccessorExpression*); sem::Expression* UnaryOp(const ast::UnaryOpExpression*); @@ -375,9 +376,8 @@ class Resolver { sem::Constant EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type); - sem::Constant EvaluateConstantValue( - const ast::ScalarConstructorExpression* scalar_ctor, - const sem::Type* type); + sem::Constant EvaluateConstantValue(const ast::Literal* literal, + const sem::Type* type); sem::Constant EvaluateConstantValue( const ast::TypeConstructorExpression* type_ctor, const sem::Type* type); diff --git a/src/resolver/resolver_constants.cc b/src/resolver/resolver_constants.cc index fb59ff3895..5541d64145 100644 --- a/src/resolver/resolver_constants.cc +++ b/src/resolver/resolver_constants.cc @@ -29,7 +29,7 @@ using f32 = ProgramBuilder::f32; sem::Constant Resolver::EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type) { - if (auto* e = expr->As()) { + if (auto* e = expr->As()) { return EvaluateConstantValue(e, type); } if (auto* e = expr->As()) { @@ -38,10 +38,8 @@ sem::Constant Resolver::EvaluateConstantValue(const ast::Expression* expr, return {}; } -sem::Constant Resolver::EvaluateConstantValue( - const ast::ScalarConstructorExpression* scalar_ctor, - const sem::Type* type) { - auto* literal = scalar_ctor->literal; +sem::Constant Resolver::EvaluateConstantValue(const ast::Literal* literal, + const sem::Type* type) { if (auto* lit = literal->As()) { return {type, {lit->ValueAsI32()}}; } diff --git a/src/resolver/type_constructor_validation_test.cc b/src/resolver/type_constructor_validation_test.cc index c369f0bf6c..764d2ed8b9 100644 --- a/src/resolver/type_constructor_validation_test.cc +++ b/src/resolver/type_constructor_validation_test.cc @@ -424,10 +424,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_type_match) { // array(0u, 10u. 20u); - auto* tc = - array(create(Literal(0u)), - create(Literal(10u)), - create(Literal(20u))); + auto* tc = array(Literal(0u), Literal(10u), Literal(20u)); WrapInFunction(tc); EXPECT_TRUE(r()->Resolve()); @@ -436,10 +433,8 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_type_Mismatch_U32F32) { // array(0u, 1.0f, 20u); - auto* tc = array( - create(Literal(0u)), - create(Source{{12, 34}}, Literal(1.0f)), - create(Literal(20u))); + auto* tc = + array(Literal(0u), Literal(Source{{12, 34}}, 1.0f), Literal(20u)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -451,8 +446,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_ScalarArgumentTypeMismatch_F32I32) { // array(1); - auto* tc = array( - create(Source{{12, 34}}, Literal(1))); + auto* tc = array(Literal(Source{{12, 34}}, 1)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -464,12 +458,8 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_ScalarArgumentTypeMismatch_U32I32) { // array(1, 0u, 0u, 0u, 0u, 0u); - auto* tc = array( - create(Source{{12, 34}}, Literal(1)), - create(Literal(0u)), - create(Literal(0u)), - create(Literal(0u)), - create(Literal(0u))); + auto* tc = array(Literal(Source{{12, 34}}, 1), Literal(0u), + Literal(0u), Literal(0u), Literal(0u)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -481,7 +471,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_ScalarArgumentTypeMismatch_Vec2) { // array(1, vec2()); - auto* tc = array(create(Literal(1)), + auto* tc = array(Literal(1), create( Source{{12, 34}}, ty.vec2(), ExprList())); WrapInFunction(tc); @@ -555,10 +545,7 @@ TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_TooFewElements) { // array(1, 2, 3); SetSource(Source::Location({12, 34})); - auto* tc = - array(create(Literal(1)), - create(Literal(2)), - create(Literal(3))); + auto* tc = array(Literal(1), Literal(2), Literal(3)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -572,11 +559,7 @@ TEST_F(ResolverTypeConstructorValidationTest, // array(1, 2, 3, 4, 5); SetSource(Source::Location({12, 34})); auto* tc = - array(create(Literal(1)), - create(Literal(2)), - create(Literal(3)), - create(Literal(4)), - create(Literal(5))); + array(Literal(1), Literal(2), Literal(3), Literal(4), Literal(5)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -588,9 +571,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_Runtime) { // array(1); - auto* tc = array( - ty.i32(), nullptr, - create(Source{{12, 34}}, Literal(1))); + auto* tc = array(ty.i32(), nullptr, Literal(Source{{12, 34}}, 1)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -613,9 +594,7 @@ namespace VectorConstructor { TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2F32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec2( - create(Source{{12, 34}}, Literal(1)), - 1.0f); + auto* tc = vec2(Literal(Source{{12, 34}}, 1), 1.0f); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -626,8 +605,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2U32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec2(1u, create( - Source{{12, 34}}, Literal(1))); + auto* tc = vec2(1u, Literal(Source{{12, 34}}, 1)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -638,9 +616,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2I32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec2( - create(Source{{12, 34}}, Literal(1u)), - 1); + auto* tc = vec2(Literal(Source{{12, 34}}, 1u), 1); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -651,8 +627,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2Bool_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec2(true, create( - Source{{12, 34}}, Literal(1))); + auto* tc = vec2(true, Literal(Source{{12, 34}}, 1)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -687,11 +662,9 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Error_TooManyArgumentsScalar) { - auto* tc = vec2( - create(Source{{12, 34}}, Literal(1.0f)), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, - Literal(1.0f))); + auto* tc = vec2(Literal(Source{{12, 34}}, 1.0f), + Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -718,8 +691,7 @@ TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Error_TooManyArgumentsVectorAndScalar) { auto* tc = vec2(create( Source{{12, 34}}, ty.vec2(), ExprList()), - create( - Source{{12, 40}}, Literal(1.0f))); + Literal(Source{{12, 40}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -833,9 +805,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3F32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec3( - 1.0f, 1.0f, - create(Source{{12, 34}}, Literal(1))); + auto* tc = vec3(1.0f, 1.0f, Literal(Source{{12, 34}}, 1)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -846,10 +816,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3U32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec3( - 1u, - create(Source{{12, 34}}, Literal(1)), - 1u); + auto* tc = vec3(1u, Literal(Source{{12, 34}}, 1), 1u); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -860,10 +827,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3I32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec3( - 1, - create(Source{{12, 34}}, Literal(1u)), - 1); + auto* tc = vec3(1, Literal(Source{{12, 34}}, 1u), 1); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -874,10 +838,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3Bool_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec3( - true, - create(Source{{12, 34}}, Literal(1)), - false); + auto* tc = vec3(true, Literal(Source{{12, 34}}, 1), false); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -900,10 +861,8 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooFewArgumentsScalar) { - auto* tc = vec3( - create(Source{{12, 34}}, Literal(1.0f)), - create(Source{{12, 40}}, - Literal(1.0f))); + auto* tc = vec3(Literal(Source{{12, 34}}, 1.0f), + Literal(Source{{12, 40}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -915,11 +874,8 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsScalar) { auto* tc = vec3( - create(Source{{12, 34}}, Literal(1.0f)), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, Literal(1.0f)), - create(Source{{12, 52}}, - Literal(1.0f))); + Literal(Source{{12, 34}}, 1.0f), Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f), Literal(Source{{12, 52}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -956,12 +912,10 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsVec2AndScalar) { - auto* tc = vec3( - create(Source{{12, 34}}, ty.vec2(), - ExprList()), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, - Literal(1.0f))); + auto* tc = vec3(create( + Source{{12, 34}}, ty.vec2(), ExprList()), + Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -974,8 +928,7 @@ TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsVec3) { auto* tc = vec3(create( Source{{12, 34}}, ty.vec3(), ExprList()), - create( - Source{{12, 40}}, Literal(1.0f))); + Literal(Source{{12, 40}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1115,10 +1068,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4F32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec4( - 1.0f, 1.0f, - create(Source{{12, 34}}, Literal(1)), - 1.0f); + auto* tc = vec4(1.0f, 1.0f, Literal(Source{{12, 34}}, 1), 1.0f); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1129,10 +1079,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4U32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec4( - 1u, 1u, - create(Source{{12, 34}}, Literal(1)), - 1u); + auto* tc = vec4(1u, 1u, Literal(Source{{12, 34}}, 1), 1u); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1143,10 +1090,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4I32_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec4( - 1, 1, - create(Source{{12, 34}}, Literal(1u)), - 1); + auto* tc = vec4(1, 1, Literal(Source{{12, 34}}, 1u), 1); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1157,10 +1101,7 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4Bool_Error_ScalarArgumentTypeMismatch) { - auto* tc = vec4( - true, false, - create(Source{{12, 34}}, Literal(1)), - true); + auto* tc = vec4(true, false, Literal(Source{{12, 34}}, 1), true); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1171,11 +1112,9 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooFewArgumentsScalar) { - auto* tc = vec4( - create(Source{{12, 34}}, Literal(1.0f)), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, - Literal(1.0f))); + auto* tc = vec4(Literal(Source{{12, 34}}, 1.0f), + Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1187,12 +1126,9 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooManyArgumentsScalar) { auto* tc = vec4( - create(Source{{12, 34}}, Literal(1.0f)), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, Literal(1.0f)), - create(Source{{12, 52}}, Literal(1.0f)), - create(Source{{12, 58}}, - Literal(1.0f))); + Literal(Source{{12, 34}}, 1.0f), Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f), Literal(Source{{12, 52}}, 1.0f), + Literal(Source{{12, 58}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1205,8 +1141,7 @@ TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooFewArgumentsVec2AndScalar) { auto* tc = vec4(create( Source{{12, 34}}, ty.vec2(), ExprList()), - create( - Source{{12, 40}}, Literal(1.0f))); + Literal(Source{{12, 40}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1217,13 +1152,11 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooManyArgumentsVec2AndScalars) { - auto* tc = vec4( - create(Source{{12, 34}}, ty.vec2(), - ExprList()), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, Literal(1.0f)), - create(Source{{12, 52}}, - Literal(1.0f))); + auto* tc = vec4(create( + Source{{12, 34}}, ty.vec2(), ExprList()), + Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f), + Literal(Source{{12, 52}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1238,8 +1171,7 @@ TEST_F(ResolverTypeConstructorValidationTest, Source{{12, 34}}, ty.vec2(), ExprList()), create( Source{{12, 40}}, ty.vec2(), ExprList()), - create( - Source{{12, 46}}, Literal(1.0f))); + Literal(Source{{12, 46}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1278,12 +1210,10 @@ TEST_F(ResolverTypeConstructorValidationTest, TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooManyArgumentsVec3AndScalars) { - auto* tc = vec4( - create(Source{{12, 34}}, ty.vec3(), - ExprList()), - create(Source{{12, 40}}, Literal(1.0f)), - create(Source{{12, 46}}, - Literal(1.0f))); + auto* tc = vec4(create( + Source{{12, 34}}, ty.vec3(), ExprList()), + Literal(Source{{12, 40}}, 1.0f), + Literal(Source{{12, 46}}, 1.0f)); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1576,8 +1506,7 @@ TEST_F(ResolverTypeConstructorValidationTest, auto* vec_type = ty.vec(ty.Of(f32_alias), 2); auto* tc = create( Source{{12, 34}}, vec_type, - ExprList(1.0f, create(Source{{12, 40}}, - Literal(1u)))); + ExprList(1.0f, Literal(Source{{12, 40}}, 1u))); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -1767,8 +1696,7 @@ TEST_P(MatrixConstructorTest, ast::ExpressionList args; for (uint32_t i = 1; i <= param.columns; i++) { - args.push_back( - create(Source{{12, i}}, Literal(1u))); + args.push_back(Literal(Source{{12, i}}, 1u)); } auto* matrix_type = ty.mat(param.columns, param.rows); diff --git a/src/resolver/validation_test.cc b/src/resolver/validation_test.cc index bdad5fef50..01f52d025b 100644 --- a/src/resolver/validation_test.cc +++ b/src/resolver/validation_test.cc @@ -129,9 +129,7 @@ TEST_F(ResolverValidationTest, Stmt_Error_Unknown) { TEST_F(ResolverValidationTest, Stmt_If_NonBool) { // if (1.23f) {} - WrapInFunction(If(create(Source{{12, 34}}, - Literal(1.23f)), - Block())); + WrapInFunction(If(Literal(Source{{12, 34}}, 1.23f), Block())); EXPECT_FALSE(r()->Resolve()); @@ -142,10 +140,8 @@ TEST_F(ResolverValidationTest, Stmt_If_NonBool) { TEST_F(ResolverValidationTest, Stmt_Else_NonBool) { // else (1.23f) {} - WrapInFunction(If(Expr(true), Block(), - Else(create( - Source{{12, 34}}, Literal(1.23f)), - Block()))); + WrapInFunction( + If(Expr(true), Block(), Else(Literal(Source{{12, 34}}, 1.23f), Block()))); EXPECT_FALSE(r()->Resolve()); diff --git a/src/transform/decompose_memory_access.cc b/src/transform/decompose_memory_access.cc index a524ee4f1d..32ff132e97 100644 --- a/src/transform/decompose_memory_access.cc +++ b/src/transform/decompose_memory_access.cc @@ -23,7 +23,6 @@ #include "src/ast/assignment_statement.h" #include "src/ast/call_statement.h" #include "src/ast/disable_validation_decoration.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/type_name.h" #include "src/ast/unary_op.h" #include "src/block_allocator.h" @@ -331,13 +330,11 @@ struct DecomposeMemoryAccess::State { /// @param expr the expression to convert to an Offset /// @returns an Offset for the given ast::Expression const Offset* ToOffset(const ast::Expression* expr) { - if (auto* scalar = expr->As()) { - if (auto* u32 = scalar->literal->As()) { - return offsets_.Create(u32->value); - } else if (auto* i32 = scalar->literal->As()) { - if (i32->value > 0) { - return offsets_.Create(i32->value); - } + if (auto* u32 = expr->As()) { + return offsets_.Create(u32->value); + } else if (auto* i32 = expr->As()) { + if (i32->value > 0) { + return offsets_.Create(i32->value); } } return offsets_.Create(expr); diff --git a/src/transform/fold_constants.cc b/src/transform/fold_constants.cc index 271a930d64..773136f7c3 100644 --- a/src/transform/fold_constants.cc +++ b/src/transform/fold_constants.cc @@ -81,7 +81,8 @@ void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) { } if (ty->is_scalar()) { - return value.WithScalarAt(0, [&](auto&& s) { return ctx.dst->Expr(s); }); + return value.WithScalarAt( + 0, [&](auto&& s) -> const ast::Literal* { return ctx.dst->Expr(s); }); } return nullptr; diff --git a/src/transform/fold_trivial_single_use_lets.cc b/src/transform/fold_trivial_single_use_lets.cc index 0427075b37..c83008c9eb 100644 --- a/src/transform/fold_trivial_single_use_lets.cc +++ b/src/transform/fold_trivial_single_use_lets.cc @@ -37,8 +37,7 @@ const ast::VariableDeclStatement* AsTrivialLetDecl(const ast::Statement* stmt) { return nullptr; } auto* ctor = var->constructor; - if (!IsAnyOf( - ctor)) { + if (!IsAnyOf(ctor)) { return nullptr; } return var_decl; diff --git a/src/transform/inline_pointer_lets.cc b/src/transform/inline_pointer_lets.cc index 196c0d564d..ea61615da0 100644 --- a/src/transform/inline_pointer_lets.cc +++ b/src/transform/inline_pointer_lets.cc @@ -46,7 +46,7 @@ void CollectSavedArrayIndices(const Program* program, if (auto* a = expr->As()) { CollectSavedArrayIndices(program, a->array, cb); - if (!a->index->Is()) { + if (!a->index->Is()) { cb(a->index); } return; diff --git a/src/transform/robustness.cc b/src/transform/robustness.cc index 28cd0f9031..b4465a3afa 100644 --- a/src/transform/robustness.cc +++ b/src/transform/robustness.cc @@ -189,7 +189,9 @@ struct Robustness::State { // Convert idx to an expression, so we can emit the new accessor. if (!idx.expr) { - idx.expr = idx.is_signed ? b.Expr(idx.i32) : b.Expr(idx.u32); + idx.expr = idx.is_signed + ? static_cast(b.Expr(idx.i32)) + : static_cast(b.Expr(idx.u32)); } // Clone arguments outside of create() call to have deterministic ordering diff --git a/src/transform/transform_test.cc b/src/transform/transform_test.cc index 001e54ab73..981f7d2be1 100644 --- a/src/transform/transform_test.cc +++ b/src/transform/transform_test.cc @@ -84,10 +84,7 @@ TEST_F(CreateASTTypeForTest, ArrayImplicitStride) { ASSERT_TRUE(arr->As()->type->Is()); ASSERT_EQ(arr->As()->decorations.size(), 0u); - auto* count_expr = - arr->As()->count->As(); - ASSERT_NE(count_expr, nullptr); - auto* size = count_expr->literal->As(); + auto* size = arr->As()->count->As(); ASSERT_NE(size, nullptr); EXPECT_EQ(size->ValueAsI32(), 2); } @@ -107,10 +104,7 @@ TEST_F(CreateASTTypeForTest, ArrayNonImplicitStride) { ->stride, 64u); - auto* count_expr = - arr->As()->count->As(); - ASSERT_NE(count_expr, nullptr); - auto* size = count_expr->literal->As(); + auto* size = arr->As()->count->As(); ASSERT_NE(size, nullptr); EXPECT_EQ(size->ValueAsI32(), 2); } diff --git a/src/writer/append_vector.cc b/src/writer/append_vector.cc index 18ea6147d8..9520cc9654 100644 --- a/src/writer/append_vector.cc +++ b/src/writer/append_vector.cc @@ -85,7 +85,7 @@ const ast::TypeConstructorExpression* AppendVector( const auto num_supplied = vc->values.size(); if (num_supplied == 0) { // Zero-value vector constructor. Populate with zeros - auto buildZero = [&]() -> const ast::ScalarConstructorExpression* { + auto buildZero = [&]() -> const ast::Literal* { if (packed_el_sem_ty->Is()) { return b->Expr(0); } else if (packed_el_sem_ty->Is()) { diff --git a/src/writer/append_vector_test.cc b/src/writer/append_vector_test.cc index 39d837cecc..26de490f54 100644 --- a/src/writer/append_vector_test.cc +++ b/src/writer/append_vector_test.cc @@ -249,9 +249,7 @@ TEST_F(AppendVectorTest, ZeroVec3i32_i32) { ASSERT_NE(vec_0004, nullptr); ASSERT_EQ(vec_0004->values.size(), 4u); for (size_t i = 0; i < 3; i++) { - auto* ctor = vec_0004->values[i]->As(); - ASSERT_NE(ctor, nullptr); - auto* literal = As(ctor->literal); + auto* literal = As(vec_0004->values[i]); ASSERT_NE(literal, nullptr); EXPECT_EQ(literal->value, 0); } diff --git a/src/writer/glsl/generator_impl.cc b/src/writer/glsl/generator_impl.cc index 6e78944454..16cbf30d95 100644 --- a/src/writer/glsl/generator_impl.cc +++ b/src/writer/glsl/generator_impl.cc @@ -1387,18 +1387,9 @@ bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) { bool GeneratorImpl::EmitConstructor(std::ostream& out, const ast::ConstructorExpression* expr) { - if (auto* scalar = expr->As()) { - return EmitScalarConstructor(out, scalar); - } return EmitTypeConstructor(out, expr->As()); } -bool GeneratorImpl::EmitScalarConstructor( - std::ostream& out, - const ast::ScalarConstructorExpression* expr) { - return EmitLiteral(out, expr->literal); -} - bool GeneratorImpl::EmitTypeConstructor( std::ostream& out, const ast::TypeConstructorExpression* expr) { @@ -1486,6 +1477,9 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, if (auto* i = expr->As()) { return EmitIdentifier(out, i); } + if (auto* l = expr->As()) { + return EmitLiteral(out, l); + } if (auto* m = expr->As()) { return EmitMemberAccessor(out, m); } diff --git a/src/writer/glsl/generator_impl.h b/src/writer/glsl/generator_impl.h index 7539da5de1..9258cae6e3 100644 --- a/src/writer/glsl/generator_impl.h +++ b/src/writer/glsl/generator_impl.h @@ -198,12 +198,6 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the discard statement /// @returns true if the statement was successfully emitted bool EmitDiscard(const ast::DiscardStatement* stmt); - /// Handles generating a scalar constructor - /// @param out the output of the expression stream - /// @param expr the scalar constructor expression - /// @returns true if the scalar constructor is emitted - bool EmitScalarConstructor(std::ostream& out, - const ast::ScalarConstructorExpression* expr); /// Handles emitting a type constructor /// @param out the output of the expression stream /// @param expr the type constructor expression diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 2730c398ee..f01ce15c25 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -2114,18 +2114,9 @@ bool GeneratorImpl::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { bool GeneratorImpl::EmitConstructor(std::ostream& out, const ast::ConstructorExpression* expr) { - if (auto* scalar = expr->As()) { - return EmitScalarConstructor(out, scalar); - } return EmitTypeConstructor(out, expr->As()); } -bool GeneratorImpl::EmitScalarConstructor( - std::ostream& out, - const ast::ScalarConstructorExpression* expr) { - return EmitLiteral(out, expr->literal); -} - bool GeneratorImpl::EmitTypeConstructor( std::ostream& out, const ast::TypeConstructorExpression* expr) { @@ -2218,6 +2209,9 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, if (auto* i = expr->As()) { return EmitIdentifier(out, i); } + if (auto* l = expr->As()) { + return EmitLiteral(out, l); + } if (auto* m = expr->As()) { return EmitMemberAccessor(out, m); } diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 61b895dcff..67a4da51a3 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -227,12 +227,6 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the discard statement /// @returns true if the statement was successfully emitted bool EmitDiscard(const ast::DiscardStatement* stmt); - /// Handles generating a scalar constructor - /// @param out the output of the expression stream - /// @param expr the scalar constructor expression - /// @returns true if the scalar constructor is emitted - bool EmitScalarConstructor(std::ostream& out, - const ast::ScalarConstructorExpression* expr); /// Handles emitting a type constructor /// @param out the output of the expression stream /// @param expr the type constructor expression diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 2d7ad03d40..3f7358664a 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -1293,9 +1293,6 @@ bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) { bool GeneratorImpl::EmitConstructor(std::ostream& out, const ast::ConstructorExpression* expr) { - if (auto* scalar = expr->As()) { - return EmitScalarConstructor(out, scalar); - } return EmitTypeConstructor(out, expr->As()); } @@ -1387,12 +1384,6 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) { return true; } -bool GeneratorImpl::EmitScalarConstructor( - std::ostream& out, - const ast::ScalarConstructorExpression* expr) { - return EmitLiteral(out, expr->literal); -} - bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* l = lit->As()) { out << (l->value ? "true" : "false"); @@ -1445,6 +1436,9 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, if (auto* i = expr->As()) { return EmitIdentifier(out, i); } + if (auto* l = expr->As()) { + return EmitLiteral(out, l); + } if (auto* m = expr->As()) { return EmitMemberAccessor(out, m); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 5c68d318e1..5c593260f9 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -26,12 +26,12 @@ #include "src/ast/break_statement.h" #include "src/ast/continue_statement.h" #include "src/ast/discard_statement.h" +#include "src/ast/expression.h" #include "src/ast/if_statement.h" #include "src/ast/interpolate_decoration.h" #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/switch_statement.h" #include "src/ast/type_constructor_expression.h" #include "src/ast/unary_op_expression.h" @@ -241,12 +241,6 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted bool EmitReturn(const ast::ReturnStatement* stmt); - /// Handles generating a scalar constructor - /// @param out the output of the expression stream - /// @param expr the scalar constructor expression - /// @returns true if the scalar constructor is emitted - bool EmitScalarConstructor(std::ostream& out, - const ast::ScalarConstructorExpression* expr); /// Handles emitting a pipeline stage name /// @param out the output of the expression stream /// @param stage the stage to emit diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index d0340e9ac0..a56e636d59 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -583,6 +583,9 @@ uint32_t Builder::GenerateExpression(const ast::Expression* expr) { if (auto* i = expr->As()) { return GenerateIdentifierExpression(i); } + if (auto* l = expr->As()) { + return GenerateLiteralIfNeeded(nullptr, l); + } if (auto* m = expr->As()) { return GenerateAccessorExpression(m); } @@ -757,13 +760,7 @@ bool Builder::GenerateGlobalVariable(const ast::Variable* var) { uint32_t init_id = 0; if (var->constructor) { - if (!var->constructor->Is()) { - error_ = "scalar constructor expected"; - return false; - } - - init_id = GenerateConstructorExpression( - var, var->constructor->As()); + init_id = GenerateConstructorExpression(var, var->constructor); if (init_id == 0) { return false; } @@ -931,14 +928,7 @@ bool Builder::GenerateArrayAccessor(const ast::ArrayAccessorExpression* expr, auto extract_id = extract.to_i(); // If the index is a literal, we use OpCompositeExtract. - if (auto* scalar = expr->index->As()) { - auto* literal = scalar->literal->As(); - if (!literal) { - TINT_ICE(Writer, builder_.Diagnostics()) - << "bad literal in array accessor"; - return false; - } - + if (auto* literal = expr->index->As()) { if (!push_function_inst(spv::Op::OpCompositeExtract, {Operand::Int(result_type_id), extract, Operand::Int(info->source_id), @@ -1264,11 +1254,10 @@ uint32_t Builder::GetGLSLstd450Import() { return id; } -uint32_t Builder::GenerateConstructorExpression( - const ast::Variable* var, - const ast::ConstructorExpression* expr) { - if (auto* scalar = expr->As()) { - return GenerateLiteralIfNeeded(var, scalar->literal); +uint32_t Builder::GenerateConstructorExpression(const ast::Variable* var, + const ast::Expression* expr) { + if (auto* literal = expr->As()) { + return GenerateLiteralIfNeeded(var, literal); } if (auto* type = expr->As()) { return GenerateTypeConstructorExpression(var, type); @@ -1280,20 +1269,19 @@ uint32_t Builder::GenerateConstructorExpression( bool Builder::is_constructor_const(const ast::Expression* expr, bool is_global_init) { - auto* constructor = expr->As(); - if (constructor == nullptr) { - return false; - } - if (constructor->Is()) { + if (expr->Is()) { return true; } - auto* tc = constructor->As(); + auto* tc = expr->As(); + if (!tc) { + return false; + } auto* result_type = TypeOf(tc)->UnwrapRef(); for (size_t i = 0; i < tc->values.size(); ++i) { auto* e = tc->values[i]; - if (!e->Is()) { + if (!e->IsAnyOf()) { if (is_global_init) { error_ = "constructor must be a constant expression"; return false; @@ -1307,13 +1295,13 @@ bool Builder::is_constructor_const(const ast::Expression* expr, return false; } - auto* sc = e->As(); - if (result_type->Is() && sc == nullptr) { + auto* lit = e->As(); + if (result_type->Is() && lit == nullptr) { return false; } // This should all be handled by |is_constructor_const| call above - if (sc == nullptr) { + if (lit == nullptr) { continue; } @@ -1327,7 +1315,7 @@ bool Builder::is_constructor_const(const ast::Expression* expr, } else if (auto* str = subtype->As()) { subtype = str->Members()[i]->Type(); } - if (subtype != TypeOf(sc)->UnwrapRef()) { + if (subtype != TypeOf(lit)->UnwrapRef()) { return false; } } @@ -1409,8 +1397,7 @@ uint32_t Builder::GenerateTypeConstructorExpression( for (auto* e : values) { uint32_t id = 0; if (constructor_is_const) { - id = GenerateConstructorExpression(nullptr, - e->As()); + id = GenerateConstructorExpression(nullptr, e); } else { id = GenerateExpression(e); id = GenerateLoadIfNeeded(TypeOf(e), id); diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index 77d878f980..4ceefa2df9 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -339,9 +339,8 @@ class Builder { /// @param var the variable generated for, nullptr if no variable associated. /// @param expr the expression to generate /// @returns the ID of the expression or 0 on failure. - uint32_t GenerateConstructorExpression( - const ast::Variable* var, - const ast::ConstructorExpression* expr); + uint32_t GenerateConstructorExpression(const ast::Variable* var, + const ast::Expression* expr); /// Generates a type constructor expression /// @param var the variable generated for, nullptr if no variable associated. /// @param init the expression to generate diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index c97669facd..5d1a77ef53 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -131,6 +131,9 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, if (auto* i = expr->As()) { return EmitIdentifier(out, i); } + if (auto* l = expr->As()) { + return EmitLiteral(out, l); + } if (auto* c = expr->As()) { return EmitConstructor(out, c); } @@ -242,9 +245,6 @@ bool GeneratorImpl::EmitCall(std::ostream& out, bool GeneratorImpl::EmitConstructor(std::ostream& out, const ast::ConstructorExpression* expr) { - if (auto* scalar = expr->As()) { - return EmitScalarConstructor(out, scalar); - } return EmitTypeConstructor(out, expr->As()); } @@ -273,12 +273,6 @@ bool GeneratorImpl::EmitTypeConstructor( return true; } -bool GeneratorImpl::EmitScalarConstructor( - std::ostream& out, - const ast::ScalarConstructorExpression* expr) { - return EmitLiteral(out, expr->literal); -} - bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* bl = lit->As()) { out << (bl->value ? "true" : "false"); diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index c626d76193..ca6a182039 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -30,7 +30,6 @@ #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" -#include "src/ast/scalar_constructor_expression.h" #include "src/ast/switch_statement.h" #include "src/ast/type_constructor_expression.h" #include "src/ast/unary_op_expression.h" @@ -96,12 +95,11 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement /// @returns true if the statment was emitted successfully bool EmitCase(const ast::CaseStatement* stmt); - /// Handles generating a scalar constructor + /// Handles generating a literal expression /// @param out the output of the expression stream - /// @param expr the scalar constructor expression - /// @returns true if the scalar constructor is emitted - bool EmitScalarConstructor(std::ostream& out, - const ast::ScalarConstructorExpression* expr); + /// @param expr the literal expression expression + /// @returns true if the literal expression is emitted + bool EmitLiteral(std::ostream& out, const ast::Literal* expr); /// Handles a continue statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully @@ -138,11 +136,6 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the discard statement /// @returns true if the statement was successfully emitted bool EmitDiscard(const ast::DiscardStatement* stmt); - /// Handles a literal - /// @param out the output of the expression stream - /// @param lit the literal to emit - /// @returns true if the literal was successfully emitted - bool EmitLiteral(std::ostream& out, const ast::Literal* lit); /// Handles a loop statement /// @param stmt the statement to emit /// @returns true if the statement was emtited diff --git a/src/writer/wgsl/generator_impl_literal_test.cc b/src/writer/wgsl/generator_impl_literal_test.cc index e2244b1092..0937fc847c 100644 --- a/src/writer/wgsl/generator_impl_literal_test.cc +++ b/src/writer/wgsl/generator_impl_literal_test.cc @@ -59,7 +59,7 @@ TEST_P(WgslGenerator_FloatLiteralTest, Emit) { GeneratorImpl& gen = Build(); std::stringstream out; - ASSERT_TRUE(gen.EmitScalarConstructor(out, v)) << gen.error(); + ASSERT_TRUE(gen.EmitLiteral(out, v)) << gen.error(); EXPECT_EQ(out.str(), GetParam().expected); } diff --git a/test/BUILD.gn b/test/BUILD.gn index edd66b940e..6c4b740814 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -194,7 +194,6 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/ast/return_statement_test.cc", "../src/ast/sampled_texture_test.cc", "../src/ast/sampler_test.cc", - "../src/ast/scalar_constructor_expression_test.cc", "../src/ast/sint_literal_test.cc", "../src/ast/stage_decoration_test.cc", "../src/ast/storage_texture_test.cc",