ast: Add PhonyExpression

Represents the `_` in a phony assignment `_ = <expr>`

Bug: tint:1213
Change-Id: Ifb070277b7872aa5ae405e467e28191e625c4c5a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/67062
Kokoro: Kokoro <noreply+kokoro@google.com>
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-10-21 18:33:14 +00:00 committed by Tint LUCI CQ
parent 042bd02747
commit 8fca34546b
7 changed files with 141 additions and 0 deletions

View File

@ -274,6 +274,8 @@ libtint_source_set("libtint_core_all_src") {
"ast/node.h",
"ast/override_decoration.cc",
"ast/override_decoration.h",
"ast/phony_expression.cc",
"ast/phony_expression.h",
"ast/pipeline_stage.cc",
"ast/pipeline_stage.h",
"ast/pointer.cc",

View File

@ -138,6 +138,8 @@ set(TINT_LIB_SRCS
ast/node.h
ast/override_decoration.cc
ast/override_decoration.h
ast/phony_expression.cc
ast/phony_expression.h
ast/pipeline_stage.cc
ast/pipeline_stage.h
ast/pointer.cc
@ -619,6 +621,7 @@ if(${TINT_BUILD_TESTS})
ast/module_test.cc
ast/multisampled_texture_test.cc
ast/override_decoration_test.cc
ast/phony_expression_test.cc
ast/pointer_test.cc
ast/return_statement_test.cc
ast/sampled_texture_test.cc

View File

@ -0,0 +1,38 @@
// Copyright 2021 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/phony_expression.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::ast::PhonyExpression);
namespace tint {
namespace ast {
PhonyExpression::PhonyExpression(ProgramID pid, const Source& src)
: Base(pid, src) {}
PhonyExpression::PhonyExpression(PhonyExpression&&) = default;
PhonyExpression::~PhonyExpression() = default;
const PhonyExpression* PhonyExpression::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source);
return ctx->dst->create<PhonyExpression>(src);
}
} // namespace ast
} // namespace tint

View File

@ -0,0 +1,45 @@
// Copyright 2021 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_PHONY_EXPRESSION_H_
#define SRC_AST_PHONY_EXPRESSION_H_
#include "src/ast/expression.h"
namespace tint {
namespace ast {
/// Represents the `_` of a phony assignment `_ = <expr>`
/// @see https://www.w3.org/TR/WGSL/#phony-assignment-section
class PhonyExpression : public Castable<PhonyExpression, Expression> {
public:
/// Constructor
/// @param pid the identifier of the program that owns this node
/// @param src the source of this node
PhonyExpression(ProgramID pid, const Source& src);
/// Move constructor
PhonyExpression(PhonyExpression&&);
~PhonyExpression() override;
/// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`.
/// @param ctx the clone context
/// @return the newly cloned node
const PhonyExpression* Clone(CloneContext* ctx) const override;
};
} // namespace ast
} // namespace tint
#endif // SRC_AST_PHONY_EXPRESSION_H_

View File

@ -0,0 +1,42 @@
// Copyright 2021 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/test_helper.h"
namespace tint {
namespace ast {
namespace {
using IdentifierExpressionTest = TestHelper;
TEST_F(IdentifierExpressionTest, Creation) {
EXPECT_NE(Phony(), nullptr);
}
TEST_F(IdentifierExpressionTest, Creation_WithSource) {
auto* p = Phony(Source{{20, 2}});
auto src = p->source;
EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u);
}
TEST_F(IdentifierExpressionTest, IsPhony) {
auto* p = Phony();
EXPECT_TRUE(p->Is<PhonyExpression>());
}
} // namespace
} // namespace ast
} // namespace tint

View File

@ -49,6 +49,7 @@
#include "src/ast/module.h"
#include "src/ast/multisampled_texture.h"
#include "src/ast/override_decoration.h"
#include "src/ast/phony_expression.h"
#include "src/ast/pointer.h"
#include "src/ast/return_statement.h"
#include "src/ast/sampled_texture.h"
@ -1618,6 +1619,15 @@ class ProgramBuilder {
ExprList(std::forward<ARGS>(args)...));
}
/// @param source the source information
/// @returns a `ast::PhonyExpression`
const ast::PhonyExpression* Phony(const Source& source) {
return create<ast::PhonyExpression>(source);
}
/// @returns a `ast::PhonyExpression`
const ast::PhonyExpression* Phony() { return create<ast::PhonyExpression>(); }
/// @param expr the expression to ignore
/// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
/// passed the single `expr` argument

View File

@ -189,6 +189,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
"../src/ast/module_test.cc",
"../src/ast/multisampled_texture_test.cc",
"../src/ast/override_decoration_test.cc",
"../src/ast/phony_expression_test.cc",
"../src/ast/pointer_test.cc",
"../src/ast/return_statement_test.cc",
"../src/ast/sampled_texture_test.cc",