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 <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2021-11-30 13:25:12 +00:00 committed by Tint LUCI CQ
parent 76fc6d61e9
commit 77e2c6f0b2
7 changed files with 112 additions and 0 deletions

View File

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

View File

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

39
src/sem/behavior.cc Normal file
View File

@ -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 << "<unknown>";
}
} // namespace sem
} // namespace tint

46
src/sem/behavior.h Normal file
View File

@ -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<Behavior>;
/// 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_

View File

@ -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<Expression, Node> {
/// @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<Expression, Node> {
const sem::Type* const type_;
const Statement* const statement_;
const Constant constant_;
sem::Behaviors behaviors_;
};
} // namespace sem

View File

@ -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<BlockStatement>();
}

View File

@ -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<Statement, Node> {
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<Statement, Node> {
/// @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