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:
parent
19d3205e15
commit
22d891c6c4
|
@ -545,6 +545,7 @@ if(${TINT_BUILD_TESTS})
|
|||
intrinsic_table_test.cc
|
||||
program_test.cc
|
||||
resolver/assignment_validation_test.cc
|
||||
resolver/block_test.cc
|
||||
resolver/builtins_validation_test.cc
|
||||
resolver/control_block_validation_test.cc
|
||||
resolver/decoration_validation_test.cc
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
// 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/resolver/resolver.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "src/resolver/resolver_test_helper.h"
|
||||
#include "src/sem/block_statement.h"
|
||||
|
||||
namespace tint {
|
||||
namespace resolver {
|
||||
namespace {
|
||||
|
||||
using ResolverBlockTest = ResolverTest;
|
||||
|
||||
TEST_F(ResolverBlockTest, FunctionBlock) {
|
||||
// fn F() {
|
||||
// var x : 32;
|
||||
// }
|
||||
auto* stmt = Decl(Var("x", ty.i32()));
|
||||
auto* f = Func("F", {}, ty.void_(), {stmt});
|
||||
|
||||
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
||||
|
||||
auto* s = Sem().Get(stmt);
|
||||
ASSERT_NE(s, nullptr);
|
||||
ASSERT_NE(s->Block(), nullptr);
|
||||
ASSERT_TRUE(s->Block()->Is<sem::FunctionBlockStatement>());
|
||||
EXPECT_EQ(s->Block(), s->Block()->FindFirstParent<sem::BlockStatement>());
|
||||
EXPECT_EQ(s->Block(),
|
||||
s->Block()->FindFirstParent<sem::FunctionBlockStatement>());
|
||||
EXPECT_EQ(s->Block()->As<sem::FunctionBlockStatement>()->Function(), f);
|
||||
EXPECT_EQ(s->Block()->Parent(), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(ResolverBlockTest, Block) {
|
||||
// fn F() {
|
||||
// {
|
||||
// var x : 32;
|
||||
// }
|
||||
// }
|
||||
auto* stmt = Decl(Var("x", ty.i32()));
|
||||
auto* block = Block(stmt);
|
||||
auto* f = Func("F", {}, ty.void_(), {block});
|
||||
|
||||
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
||||
|
||||
auto* s = Sem().Get(stmt);
|
||||
ASSERT_NE(s, nullptr);
|
||||
ASSERT_NE(s->Block(), nullptr);
|
||||
ASSERT_NE(s->Block()->Parent(), nullptr);
|
||||
EXPECT_EQ(s->Block(), s->Block()->FindFirstParent<sem::BlockStatement>());
|
||||
EXPECT_EQ(s->Block()->Parent(),
|
||||
s->Block()->FindFirstParent<sem::FunctionBlockStatement>());
|
||||
ASSERT_TRUE(s->Block()->Parent()->Is<sem::FunctionBlockStatement>());
|
||||
EXPECT_EQ(s->Block()->Parent()->As<sem::FunctionBlockStatement>()->Function(),
|
||||
f);
|
||||
EXPECT_EQ(s->Block()->Parent()->Parent(), nullptr);
|
||||
}
|
||||
|
||||
// TODO(bclayton): Add tests for other block types (LoopBlockStatement,
|
||||
// LoopContinuingBlockStatement, SwitchCaseBlockStatement)
|
||||
|
||||
} // namespace
|
||||
} // namespace resolver
|
||||
} // namespace tint
|
|
@ -1228,8 +1228,7 @@ bool Resolver::Function(ast::Function* func) {
|
|||
<< "Resolver::Function() called with a current statement";
|
||||
return false;
|
||||
}
|
||||
auto* sem_block =
|
||||
builder_->create<sem::BlockStatement>(func->body(), nullptr);
|
||||
auto* sem_block = builder_->create<sem::FunctionBlockStatement>(func);
|
||||
builder_->Sem().Add(func->body(), sem_block);
|
||||
TINT_SCOPED_ASSIGNMENT(current_statement_, sem_block);
|
||||
if (!BlockScope(func->body(),
|
||||
|
|
|
@ -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) {}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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_;
|
||||
|
|
|
@ -248,6 +248,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
|
|||
"../src/program_builder_test.cc",
|
||||
"../src/program_test.cc",
|
||||
"../src/resolver/assignment_validation_test.cc",
|
||||
"../src/resolver/block_test.cc",
|
||||
"../src/resolver/builtins_validation_test.cc",
|
||||
"../src/resolver/control_block_validation_test.cc",
|
||||
"../src/resolver/decoration_validation_test.cc",
|
||||
|
|
Loading…
Reference in New Issue