From 52b06fb4c515892aa011886a81abe9cc9e94891f Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 13 May 2021 13:36:32 +0000 Subject: [PATCH] ast: Add address-of and dereference unary ops Nothing currently generates these. Resolver currently doesn't know how to handle these. Backends currently stubbed enough to build without warnings. Bug: tint:727 Change-Id: I4b9863ae098b903b51a63c36c10bc6e390efbbb3 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50746 Commit-Queue: Ben Clayton Reviewed-by: David Neto Reviewed-by: James Price --- src/ast/pointer.h | 2 +- src/ast/unary_op.cc | 8 ++++++++ src/ast/unary_op.h | 7 ++++++- src/writer/hlsl/generator_impl.cc | 4 ++++ src/writer/hlsl/generator_impl_unary_op_test.cc | 15 +++++++++------ src/writer/msl/generator_impl.cc | 6 ++++++ src/writer/msl/generator_impl_unary_op_test.cc | 12 +++++++----- src/writer/wgsl/generator_impl.cc | 6 ++++++ src/writer/wgsl/generator_impl_unary_op_test.cc | 12 +++++++----- 9 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/ast/pointer.h b/src/ast/pointer.h index a3364f62c8..18b5a34eb9 100644 --- a/src/ast/pointer.h +++ b/src/ast/pointer.h @@ -26,7 +26,7 @@ namespace ast { /// A pointer type. class Pointer : public Castable { public: - /// Construtor + /// Constructor /// @param program_id the identifier of the program that owns this node /// @param source the source of this node /// @param subtype the pointee type diff --git a/src/ast/unary_op.cc b/src/ast/unary_op.cc index 5c8bba1e64..1b60c5c667 100644 --- a/src/ast/unary_op.cc +++ b/src/ast/unary_op.cc @@ -19,6 +19,14 @@ namespace ast { std::ostream& operator<<(std::ostream& out, UnaryOp mod) { switch (mod) { + case UnaryOp::kAddressOf: { + out << "address-of"; + break; + } + case UnaryOp::kDereference: { + out << "dereference"; + break; + } case UnaryOp::kNegation: { out << "negation"; break; diff --git a/src/ast/unary_op.h b/src/ast/unary_op.h index 04d754d1fe..f4352e690a 100644 --- a/src/ast/unary_op.h +++ b/src/ast/unary_op.h @@ -21,7 +21,12 @@ namespace tint { namespace ast { /// The unary op -enum class UnaryOp { kNegation = 0, kNot }; +enum class UnaryOp { + kAddressOf, // &EXPR + kDereference, // *EXPR + kNegation, // -EXPR + kNot, // !EXPR +}; /// @param out the std::ostream to write to /// @param mod the UnaryOp diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 30edeb409a..c7a3f26763 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -2601,6 +2601,10 @@ bool GeneratorImpl::EmitUnaryOp(std::ostream& pre, std::ostream& out, ast::UnaryOpExpression* expr) { switch (expr->op()) { + case ast::UnaryOp::kDereference: + case ast::UnaryOp::kAddressOf: + // TODO(crbug.com/tint/183) - support pointers + return EmitExpression(pre, out, expr->expr()); case ast::UnaryOp::kNot: out << "!"; break; diff --git a/src/writer/hlsl/generator_impl_unary_op_test.cc b/src/writer/hlsl/generator_impl_unary_op_test.cc index c1d973b2d8..97ea2a4885 100644 --- a/src/writer/hlsl/generator_impl_unary_op_test.cc +++ b/src/writer/hlsl/generator_impl_unary_op_test.cc @@ -30,6 +30,7 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { using HlslUnaryOpTest = TestParamHelper; TEST_P(HlslUnaryOpTest, Emit) { auto params = GetParam(); + auto name = std::string(params.name); Global("expr", ty.i32(), ast::StorageClass::kPrivate); @@ -39,13 +40,15 @@ TEST_P(HlslUnaryOpTest, Emit) { GeneratorImpl& gen = Build(); ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error(); - EXPECT_EQ(result(), std::string(params.name) + "(expr)"); + EXPECT_EQ(result(), name.empty() ? "expr" : name + "(expr)"); } -INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_UnaryOp, - HlslUnaryOpTest, - testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, - UnaryOpData{"-", - ast::UnaryOp::kNegation})); +INSTANTIATE_TEST_SUITE_P( + HlslGeneratorImplTest_UnaryOp, + HlslUnaryOpTest, + testing::Values(UnaryOpData{"", ast::UnaryOp::kAddressOf}, + UnaryOpData{"", ast::UnaryOp::kDereference}, + UnaryOpData{"!", ast::UnaryOp::kNot}, + UnaryOpData{"-", ast::UnaryOp::kNegation})); } // namespace } // namespace hlsl diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index ffc6c12819..afb3c5de1e 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -2169,6 +2169,12 @@ bool GeneratorImpl::EmitStructType(const sem::Struct* str) { bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { switch (expr->op()) { + case ast::UnaryOp::kDereference: + out_ << "*"; + break; + case ast::UnaryOp::kAddressOf: + out_ << "&"; + break; case ast::UnaryOp::kNot: out_ << "!"; break; diff --git a/src/writer/msl/generator_impl_unary_op_test.cc b/src/writer/msl/generator_impl_unary_op_test.cc index b94d2205a0..69e5ff480b 100644 --- a/src/writer/msl/generator_impl_unary_op_test.cc +++ b/src/writer/msl/generator_impl_unary_op_test.cc @@ -40,11 +40,13 @@ TEST_P(MslUnaryOpTest, Emit) { ASSERT_TRUE(gen.EmitExpression(op)) << gen.error(); EXPECT_EQ(gen.result(), std::string(params.name) + "(expr)"); } -INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest, - MslUnaryOpTest, - testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, - UnaryOpData{"-", - ast::UnaryOp::kNegation})); +INSTANTIATE_TEST_SUITE_P( + MslGeneratorImplTest, + MslUnaryOpTest, + testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf}, + UnaryOpData{"*", ast::UnaryOp::kDereference}, + UnaryOpData{"!", ast::UnaryOp::kNot}, + UnaryOpData{"-", ast::UnaryOp::kNegation})); } // namespace } // namespace msl diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index f488a63c56..a5be99dcb5 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -706,6 +706,12 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { switch (expr->op()) { + case ast::UnaryOp::kDereference: + out_ << "*"; + break; + case ast::UnaryOp::kAddressOf: + out_ << "&"; + break; case ast::UnaryOp::kNot: out_ << "!"; break; diff --git a/src/writer/wgsl/generator_impl_unary_op_test.cc b/src/writer/wgsl/generator_impl_unary_op_test.cc index 6865234f46..42d3a9dff4 100644 --- a/src/writer/wgsl/generator_impl_unary_op_test.cc +++ b/src/writer/wgsl/generator_impl_unary_op_test.cc @@ -44,11 +44,13 @@ TEST_P(WgslUnaryOpTest, Emit) { ASSERT_TRUE(gen.EmitExpression(op)) << gen.error(); EXPECT_EQ(gen.result(), std::string(params.name) + "(expr)"); } -INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest, - WgslUnaryOpTest, - testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, - UnaryOpData{"-", - ast::UnaryOp::kNegation})); +INSTANTIATE_TEST_SUITE_P( + WgslGeneratorImplTest, + WgslUnaryOpTest, + testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf}, + UnaryOpData{"*", ast::UnaryOp::kDereference}, + UnaryOpData{"!", ast::UnaryOp::kNot}, + UnaryOpData{"-", ast::UnaryOp::kNegation})); } // namespace } // namespace wgsl