sem: Add FunctionBlockStatement

BlockStatement for the root block of a function

Add some basic tests for this lot.

Bug: tint:812
Change-Id: I26b65717798cbff576a44bd78fbcb5c8f0d013e6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51368
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Alastair Donaldson <allydonaldson@googlemail.com>
This commit is contained in:
Ben Clayton
2021-05-20 15:17:08 +00:00
committed by Tint LUCI CQ
parent 19d3205e15
commit 22d891c6c4
8 changed files with 129 additions and 8 deletions

View File

@@ -15,8 +15,11 @@
#include "src/sem/block_statement.h"
#include "src/ast/block_statement.h"
#include "src/ast/function.h"
#include "src/sem/function.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::BlockStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::FunctionBlockStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopBlockStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopContinuingBlockStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::SwitchCaseBlockStatement);
@@ -38,6 +41,11 @@ void BlockStatement::AddDecl(ast::Variable* var) {
decls_.push_back(var);
}
FunctionBlockStatement::FunctionBlockStatement(const ast::Function* function)
: Base(function->body(), nullptr), function_(function) {}
FunctionBlockStatement::~FunctionBlockStatement() = default;
LoopBlockStatement::LoopBlockStatement(const ast::BlockStatement* declaration,
const Statement* parent)
: Base(declaration, parent) {}

View File

@@ -19,14 +19,16 @@
#include "src/sem/statement.h"
namespace tint {
// Forward declarations
namespace tint {
namespace ast {
class BlockStatement;
class Function;
class Variable;
} // namespace ast
} // namespace tint
namespace tint {
namespace sem {
/// Holds semantic information about a block, such as parent block and variables
@@ -84,6 +86,24 @@ class BlockStatement : public Castable<BlockStatement, Statement> {
std::vector<const ast::Variable*> decls_;
};
/// The root block statement for a function
class FunctionBlockStatement
: public Castable<FunctionBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param function the owning function
explicit FunctionBlockStatement(const ast::Function* function);
/// Destructor
~FunctionBlockStatement() override;
/// @returns the function owning this block
const ast::Function* Function() const { return function_; }
private:
ast::Function const* const function_;
};
/// Holds semantic information about a loop block
class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
public:

View File

@@ -65,5 +65,14 @@ const BlockStatement* Statement::Block() const {
return nullptr;
}
const ast::Function* Statement::Function() const {
if (auto* block = Block()) {
if (auto* fbs = block->FindFirstParent<FunctionBlockStatement>()) {
return fbs->Function();
}
}
return nullptr;
}
} // namespace sem
} // namespace tint

View File

@@ -17,16 +17,19 @@
#include "src/sem/node.h"
namespace tint {
// Forward declarations
namespace tint {
namespace ast {
class Function;
class Statement;
} // namespace ast
namespace sem {
class BlockStatement;
} // namespace sem
} // namespace tint
namespace tint {
namespace sem {
/// Statement holds the semantic information for a statement.
class Statement : public Castable<Statement, Node> {
@@ -45,6 +48,9 @@ class Statement : public Castable<Statement, Node> {
/// @return the closest enclosing block for this statement
const BlockStatement* Block() const;
/// @returns the function that owns this statement
const ast::Function* Function() const;
private:
ast::Statement const* const declaration_;
Statement const* const parent_;