mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-08 21:17:45 +00:00
Create a semantic class for block statements
Semantic information about block statements the resolver would temporarily create while resolving is now exposed in a sem::BlockStatement class. In the process, semantic information about statements in general is overhauled so that a statement has a reference to its parent statement, regardless of whether this is a block. Bug: tint:799 Bug: tint:800 Change-Id: I8771511c5274ea74741b8c86f0f55cbc39810888 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50904 Commit-Queue: Alastair Donaldson <allydonaldson@googlemail.com> Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Commit Bot service account
parent
43f50eaf86
commit
ac90829e1c
51
src/sem/block_statement.cc
Normal file
51
src/sem/block_statement.cc
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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/block_statement.h"
|
||||
|
||||
#include "src/ast/block_statement.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::BlockStatement);
|
||||
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
BlockStatement::BlockStatement(const ast::BlockStatement* declaration,
|
||||
const Statement* parent,
|
||||
Type type)
|
||||
: Base(declaration, parent), type_(type) {}
|
||||
|
||||
BlockStatement::~BlockStatement() = default;
|
||||
|
||||
const BlockStatement* BlockStatement::FindFirstParent(
|
||||
BlockStatement::Type ty) const {
|
||||
return FindFirstParent(
|
||||
[ty](auto* block_info) { return block_info->type_ == ty; });
|
||||
}
|
||||
|
||||
const ast::BlockStatement* BlockStatement::Declaration() const {
|
||||
return Base::Declaration()->As<ast::BlockStatement>();
|
||||
}
|
||||
|
||||
void BlockStatement::SetFirstContinue(size_t first_continue) {
|
||||
TINT_ASSERT(type_ == Type::kLoop);
|
||||
first_continue_ = first_continue;
|
||||
}
|
||||
|
||||
void BlockStatement::AddDecl(ast::Variable* var) {
|
||||
decls_.push_back(var);
|
||||
}
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
105
src/sem/block_statement.h
Normal file
105
src/sem/block_statement.h
Normal file
@@ -0,0 +1,105 @@
|
||||
// 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_BLOCK_STATEMENT_H_
|
||||
#define SRC_SEM_BLOCK_STATEMENT_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "src/debug.h"
|
||||
#include "src/sem/statement.h"
|
||||
|
||||
namespace tint {
|
||||
|
||||
// Forward declarations
|
||||
namespace ast {
|
||||
class BlockStatement;
|
||||
class Variable;
|
||||
} // namespace ast
|
||||
|
||||
namespace sem {
|
||||
|
||||
/// Holds semantic information about a block, such as parent block and variables
|
||||
/// declared in the block.
|
||||
class BlockStatement : public Castable<BlockStatement, Statement> {
|
||||
public:
|
||||
enum class Type { kGeneric, kLoop, kLoopContinuing, kSwitchCase };
|
||||
|
||||
/// Constructor
|
||||
/// @param declaration the AST node for this block statement
|
||||
/// @param parent the owning statement
|
||||
/// @param type the type of block this is
|
||||
BlockStatement(const ast::BlockStatement* declaration,
|
||||
const Statement* parent,
|
||||
Type type);
|
||||
|
||||
/// Destructor
|
||||
~BlockStatement() override;
|
||||
|
||||
/// @returns the AST block statement associated with this semantic block
|
||||
/// statement
|
||||
const ast::BlockStatement* Declaration() const;
|
||||
|
||||
/// @returns the closest enclosing block that satisfies the given predicate,
|
||||
/// which may be the block itself, or nullptr if no match is found
|
||||
/// @param pred a predicate that the resulting block must satisfy
|
||||
template <typename Pred>
|
||||
const BlockStatement* FindFirstParent(Pred&& pred) const {
|
||||
const BlockStatement* curr = this;
|
||||
while (curr && !pred(curr)) {
|
||||
curr = curr->Block();
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
|
||||
/// @returns the closest enclosing block that matches the given type, which
|
||||
/// may be the block itself, or nullptr if no match is found
|
||||
/// @param ty the type of block to be searched for
|
||||
const BlockStatement* FindFirstParent(BlockStatement::Type ty) const;
|
||||
|
||||
/// @returns the declarations associated with this block
|
||||
const std::vector<const ast::Variable*>& Decls() const { return decls_; }
|
||||
|
||||
/// Requires that this is a loop block.
|
||||
/// @returns the index of the first variable declared after the first continue
|
||||
/// statement
|
||||
size_t FirstContinue() const {
|
||||
TINT_ASSERT(type_ == Type::kLoop);
|
||||
return first_continue_;
|
||||
}
|
||||
|
||||
/// Requires that this is a loop block.
|
||||
/// Allows the resolver to set the index of the first variable declared after
|
||||
/// the first continue statement.
|
||||
/// @param first_continue index of the relevant variable
|
||||
void SetFirstContinue(size_t first_continue);
|
||||
|
||||
/// Allows the resolver to associate a declaration with this block.
|
||||
/// @param var a variable declaration to be added to the block
|
||||
void AddDecl(ast::Variable* var);
|
||||
|
||||
private:
|
||||
Type const type_;
|
||||
std::vector<const ast::Variable*> decls_;
|
||||
|
||||
// first_continue is set to the index of the first variable in decls
|
||||
// declared after the first continue statement in a loop block, if any.
|
||||
constexpr static size_t kNoContinue = size_t(~0);
|
||||
size_t first_continue_ = kNoContinue;
|
||||
};
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_SEM_BLOCK_STATEMENT_H_
|
||||
@@ -15,7 +15,10 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "src/ast/block_statement.h"
|
||||
#include "src/ast/loop_statement.h"
|
||||
#include "src/ast/statement.h"
|
||||
#include "src/debug.h"
|
||||
#include "src/sem/block_statement.h"
|
||||
#include "src/sem/statement.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::Statement);
|
||||
@@ -23,17 +26,44 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Statement);
|
||||
namespace tint {
|
||||
namespace sem {
|
||||
|
||||
Statement::Statement(const ast::Statement* declaration,
|
||||
const ast::BlockStatement* block)
|
||||
: declaration_(declaration), block_(block) {
|
||||
Statement::Statement(const ast::Statement* declaration, const Statement* parent)
|
||||
: declaration_(declaration), parent_(parent) {
|
||||
#ifndef NDEBUG
|
||||
if (block) {
|
||||
auto& stmts = block->statements();
|
||||
TINT_ASSERT(std::find(stmts.begin(), stmts.end(), declaration) !=
|
||||
stmts.end());
|
||||
if (parent_) {
|
||||
auto* block = Block();
|
||||
if (parent_ == block) {
|
||||
// The parent of this statement is a block. We thus expect the statement
|
||||
// to be an element of the block. There is one exception: a loop's
|
||||
// continuing block has the loop's body as its parent, but the continuing
|
||||
// block is not a statement in the body, so we rule out that case.
|
||||
auto& stmts = block->Declaration()->statements();
|
||||
if (std::find(stmts.begin(), stmts.end(), declaration) == stmts.end()) {
|
||||
bool statement_is_continuing_for_loop = false;
|
||||
if (parent_->parent_ != nullptr) {
|
||||
if (auto* loop =
|
||||
parent_->parent_->Declaration()->As<ast::LoopStatement>()) {
|
||||
if (loop->has_continuing() && Declaration() == loop->continuing()) {
|
||||
statement_is_continuing_for_loop = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
TINT_ASSERT(statement_is_continuing_for_loop);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // NDEBUG
|
||||
}
|
||||
|
||||
const BlockStatement* Statement::Block() const {
|
||||
auto* stmt = parent_;
|
||||
while (stmt != nullptr) {
|
||||
if (auto* block_stmt = stmt->As<BlockStatement>()) {
|
||||
return block_stmt;
|
||||
}
|
||||
stmt = stmt->parent_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
|
||||
@@ -21,30 +21,33 @@ namespace tint {
|
||||
|
||||
// Forward declarations
|
||||
namespace ast {
|
||||
class BlockStatement;
|
||||
class Statement;
|
||||
} // namespace ast
|
||||
|
||||
namespace sem {
|
||||
|
||||
class BlockStatement;
|
||||
|
||||
/// Statement holds the semantic information for a statement.
|
||||
class Statement : public Castable<Statement, Node> {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param declaration the AST node for this statement
|
||||
/// @param block the owning AST block statement
|
||||
Statement(const ast::Statement* declaration,
|
||||
const ast::BlockStatement* block);
|
||||
/// @param parent the owning statement
|
||||
Statement(const ast::Statement* declaration, const Statement* parent);
|
||||
|
||||
/// @return the AST node for this statement
|
||||
const ast::Statement* Declaration() const { return declaration_; }
|
||||
|
||||
/// @return the owning AST block statement for this statement
|
||||
const ast::BlockStatement* Block() const { return block_; }
|
||||
/// @return the statement that encloses this statement
|
||||
const Statement* Parent() const { return parent_; }
|
||||
|
||||
/// @return the closest enclosing block for this statement
|
||||
const BlockStatement* Block() const;
|
||||
|
||||
private:
|
||||
ast::Statement const* const declaration_;
|
||||
ast::BlockStatement const* const block_;
|
||||
Statement const* const parent_;
|
||||
};
|
||||
|
||||
} // namespace sem
|
||||
|
||||
Reference in New Issue
Block a user