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 <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-05-13 13:36:32 +00:00 committed by Commit Bot service account
parent 688fe4477d
commit 52b06fb4c5
9 changed files with 54 additions and 18 deletions

View File

@ -26,7 +26,7 @@ namespace ast {
/// A pointer type.
class Pointer : public Castable<Pointer, Type> {
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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,7 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
using HlslUnaryOpTest = TestParamHelper<UnaryOpData>;
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,
INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest_UnaryOp,
HlslUnaryOpTest,
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-",
ast::UnaryOp::kNegation}));
testing::Values(UnaryOpData{"", ast::UnaryOp::kAddressOf},
UnaryOpData{"", ast::UnaryOp::kDereference},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
} // namespace
} // namespace hlsl

View File

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

View File

@ -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,
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,
MslUnaryOpTest,
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-",
ast::UnaryOp::kNegation}));
testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf},
UnaryOpData{"*", ast::UnaryOp::kDereference},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
} // namespace
} // namespace msl

View File

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

View File

@ -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,
INSTANTIATE_TEST_SUITE_P(
WgslGeneratorImplTest,
WgslUnaryOpTest,
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-",
ast::UnaryOp::kNegation}));
testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf},
UnaryOpData{"*", ast::UnaryOp::kDereference},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
} // namespace
} // namespace wgsl