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
|
intrinsic_table_test.cc
|
||||||
program_test.cc
|
program_test.cc
|
||||||
resolver/assignment_validation_test.cc
|
resolver/assignment_validation_test.cc
|
||||||
|
resolver/block_test.cc
|
||||||
resolver/builtins_validation_test.cc
|
resolver/builtins_validation_test.cc
|
||||||
resolver/control_block_validation_test.cc
|
resolver/control_block_validation_test.cc
|
||||||
resolver/decoration_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";
|
<< "Resolver::Function() called with a current statement";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto* sem_block =
|
auto* sem_block = builder_->create<sem::FunctionBlockStatement>(func);
|
||||||
builder_->create<sem::BlockStatement>(func->body(), nullptr);
|
|
||||||
builder_->Sem().Add(func->body(), sem_block);
|
builder_->Sem().Add(func->body(), sem_block);
|
||||||
TINT_SCOPED_ASSIGNMENT(current_statement_, sem_block);
|
TINT_SCOPED_ASSIGNMENT(current_statement_, sem_block);
|
||||||
if (!BlockScope(func->body(),
|
if (!BlockScope(func->body(),
|
||||||
|
|
|
@ -15,8 +15,11 @@
|
||||||
#include "src/sem/block_statement.h"
|
#include "src/sem/block_statement.h"
|
||||||
|
|
||||||
#include "src/ast/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::BlockStatement);
|
||||||
|
TINT_INSTANTIATE_TYPEINFO(tint::sem::FunctionBlockStatement);
|
||||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopBlockStatement);
|
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopBlockStatement);
|
||||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopContinuingBlockStatement);
|
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopContinuingBlockStatement);
|
||||||
TINT_INSTANTIATE_TYPEINFO(tint::sem::SwitchCaseBlockStatement);
|
TINT_INSTANTIATE_TYPEINFO(tint::sem::SwitchCaseBlockStatement);
|
||||||
|
@ -38,6 +41,11 @@ void BlockStatement::AddDecl(ast::Variable* var) {
|
||||||
decls_.push_back(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,
|
LoopBlockStatement::LoopBlockStatement(const ast::BlockStatement* declaration,
|
||||||
const Statement* parent)
|
const Statement* parent)
|
||||||
: Base(declaration, parent) {}
|
: Base(declaration, parent) {}
|
||||||
|
|
|
@ -19,14 +19,16 @@
|
||||||
|
|
||||||
#include "src/sem/statement.h"
|
#include "src/sem/statement.h"
|
||||||
|
|
||||||
namespace tint {
|
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
class BlockStatement;
|
class BlockStatement;
|
||||||
|
class Function;
|
||||||
class Variable;
|
class Variable;
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
} // namespace tint
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
namespace sem {
|
namespace sem {
|
||||||
|
|
||||||
/// Holds semantic information about a block, such as parent block and variables
|
/// 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_;
|
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
|
/// Holds semantic information about a loop block
|
||||||
class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
|
class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -65,5 +65,14 @@ const BlockStatement* Statement::Block() const {
|
||||||
return nullptr;
|
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 sem
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -17,16 +17,19 @@
|
||||||
|
|
||||||
#include "src/sem/node.h"
|
#include "src/sem/node.h"
|
||||||
|
|
||||||
namespace tint {
|
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
class Function;
|
||||||
class Statement;
|
class Statement;
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
|
||||||
namespace sem {
|
namespace sem {
|
||||||
|
|
||||||
class BlockStatement;
|
class BlockStatement;
|
||||||
|
} // namespace sem
|
||||||
|
} // namespace tint
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace sem {
|
||||||
|
|
||||||
/// Statement holds the semantic information for a statement.
|
/// Statement holds the semantic information for a statement.
|
||||||
class Statement : public Castable<Statement, Node> {
|
class Statement : public Castable<Statement, Node> {
|
||||||
|
@ -45,6 +48,9 @@ class Statement : public Castable<Statement, Node> {
|
||||||
/// @return the closest enclosing block for this statement
|
/// @return the closest enclosing block for this statement
|
||||||
const BlockStatement* Block() const;
|
const BlockStatement* Block() const;
|
||||||
|
|
||||||
|
/// @returns the function that owns this statement
|
||||||
|
const ast::Function* Function() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ast::Statement const* const declaration_;
|
ast::Statement const* const declaration_;
|
||||||
Statement const* const parent_;
|
Statement const* const parent_;
|
||||||
|
|
|
@ -248,6 +248,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
|
||||||
"../src/program_builder_test.cc",
|
"../src/program_builder_test.cc",
|
||||||
"../src/program_test.cc",
|
"../src/program_test.cc",
|
||||||
"../src/resolver/assignment_validation_test.cc",
|
"../src/resolver/assignment_validation_test.cc",
|
||||||
|
"../src/resolver/block_test.cc",
|
||||||
"../src/resolver/builtins_validation_test.cc",
|
"../src/resolver/builtins_validation_test.cc",
|
||||||
"../src/resolver/control_block_validation_test.cc",
|
"../src/resolver/control_block_validation_test.cc",
|
||||||
"../src/resolver/decoration_validation_test.cc",
|
"../src/resolver/decoration_validation_test.cc",
|
||||||
|
|
Loading…
Reference in New Issue