From 77e2c6f0b2479d286406c2dc8802ba93c79ca5fb Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 30 Nov 2021 13:25:12 +0000 Subject: [PATCH] sem: Add Behavior to Statement and Expression Nothing currently does the analysis to calculate these. Change-Id: Ia2103102fbf36109f357aebf32cdfda24d6d8155 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/68407 Kokoro: Kokoro Reviewed-by: David Neto Commit-Queue: Ben Clayton --- src/BUILD.gn | 3 +++ src/CMakeLists.txt | 2 ++ src/sem/behavior.cc | 39 +++++++++++++++++++++++++++++++++++++ src/sem/behavior.h | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/sem/expression.h | 8 ++++++++ src/sem/statement.cc | 2 ++ src/sem/statement.h | 12 ++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 src/sem/behavior.cc create mode 100644 src/sem/behavior.h diff --git a/src/BUILD.gn b/src/BUILD.gn index 6ccac4ac4c..9dab4d3327 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -380,6 +380,7 @@ libtint_source_set("libtint_core_all_src") { "scope_stack.h", "sem/array.h", "sem/atomic_type.h", + "sem/behavior.h", "sem/binding_point.h", "sem/bool_type.h", "sem/call.h", @@ -520,6 +521,8 @@ libtint_source_set("libtint_sem_src") { "sem/array.h", "sem/atomic_type.cc", "sem/atomic_type.h", + "sem/behavior.cc", + "sem/behavior.h", "sem/binding_point.h", "sem/block_statement.cc", "sem/bool_type.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fc5d3f2fcf..e587fb551a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -249,6 +249,8 @@ set(TINT_LIB_SRCS sem/array.h sem/atomic_type.cc sem/atomic_type.h + sem/behavior.cc + sem/behavior.h sem/binding_point.h sem/block_statement.cc sem/block_statement.h diff --git a/src/sem/behavior.cc b/src/sem/behavior.cc new file mode 100644 index 0000000000..2d6f15cb18 --- /dev/null +++ b/src/sem/behavior.cc @@ -0,0 +1,39 @@ +// 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/sem/behavior.h" + +namespace tint { +namespace sem { + +std::ostream& operator<<(std::ostream& out, Behavior behavior) { + switch (behavior) { + case Behavior::kReturn: + return out << "Return"; + case Behavior::kDiscard: + return out << "Discard"; + case Behavior::kBreak: + return out << "Break"; + case Behavior::kContinue: + return out << "Continue"; + case Behavior::kFallthrough: + return out << "Fallthrough"; + case Behavior::kNext: + return out << "Next"; + } + return out << ""; +} + +} // namespace sem +} // namespace tint diff --git a/src/sem/behavior.h b/src/sem/behavior.h new file mode 100644 index 0000000000..1e318c1ce8 --- /dev/null +++ b/src/sem/behavior.h @@ -0,0 +1,46 @@ +// 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_SEM_BEHAVIOR_H_ +#define SRC_SEM_BEHAVIOR_H_ + +#include "src/utils/enum_set.h" + +namespace tint { +namespace sem { + +/// Behavior enumerates the possible behaviors of an expression or statement. +/// @see https://www.w3.org/TR/WGSL/#behaviors +enum class Behavior { + kReturn, + kDiscard, + kBreak, + kContinue, + kFallthrough, + kNext, +}; + +/// Behaviors is a set of Behavior +using Behaviors = utils::EnumSet; + +/// Writes the Behavior to the std::ostream. +/// @param out the std::ostream to write to +/// @param behavior the Behavior to write +/// @returns out so calls can be chained +std::ostream& operator<<(std::ostream& out, Behavior behavior); + +} // namespace sem +} // namespace tint + +#endif // SRC_SEM_BEHAVIOR_H_ diff --git a/src/sem/expression.h b/src/sem/expression.h index 4fc6e82fa1..06ae10b87c 100644 --- a/src/sem/expression.h +++ b/src/sem/expression.h @@ -16,6 +16,7 @@ #define SRC_SEM_EXPRESSION_H_ #include "src/ast/expression.h" +#include "src/sem/behavior.h" #include "src/sem/constant.h" #include "src/sem/node.h" @@ -53,6 +54,12 @@ class Expression : public Castable { /// @return the constant value of this expression const Constant& ConstantValue() const { return constant_; } + /// @return the behaviors of this statement + const sem::Behaviors& Behaviors() const { return behaviors_; } + + /// @return the behaviors of this statement + sem::Behaviors& Behaviors() { return behaviors_; } + protected: /// The AST expression node for this semantic expression const ast::Expression* const declaration_; @@ -61,6 +68,7 @@ class Expression : public Castable { const sem::Type* const type_; const Statement* const statement_; const Constant constant_; + sem::Behaviors behaviors_; }; } // namespace sem diff --git a/src/sem/statement.cc b/src/sem/statement.cc index 098fe14e5d..cc60668482 100644 --- a/src/sem/statement.cc +++ b/src/sem/statement.cc @@ -31,6 +31,8 @@ Statement::Statement(const ast::Statement* declaration, const sem::Function* function) : declaration_(declaration), parent_(parent), function_(function) {} +Statement::~Statement() = default; + const BlockStatement* Statement::Block() const { return FindFirstParent(); } diff --git a/src/sem/statement.h b/src/sem/statement.h index e5297dc674..e1c5160602 100644 --- a/src/sem/statement.h +++ b/src/sem/statement.h @@ -15,6 +15,7 @@ #ifndef SRC_SEM_STATEMENT_H_ #define SRC_SEM_STATEMENT_H_ +#include "src/sem/behavior.h" #include "src/sem/node.h" // Forward declarations @@ -70,6 +71,9 @@ class Statement : public Castable { const CompoundStatement* parent, const sem::Function* function); + /// Destructor + ~Statement() override; + /// @return the AST node for this statement const ast::Statement* Declaration() const { return declaration_; } @@ -96,10 +100,18 @@ class Statement : public Castable { /// @returns the function that owns this statement const sem::Function* Function() const { return function_; } + /// @return the behaviors of this statement + const sem::Behaviors& Behaviors() const { return behaviors_; } + + /// @return the behaviors of this statement + sem::Behaviors& Behaviors() { return behaviors_; } + private: const ast::Statement* const declaration_; const CompoundStatement* const parent_; const sem::Function* const function_; + + sem::Behaviors behaviors_; }; /// CompoundStatement is the base class of statements that can hold other