sem: Add CompoundStatement

This change introduces sem::CompoundStatement, a new base class for
statements that can hold other statements.

sem::BlockStatements now derives from sem::CompoundStatement, and
this change introduces the following new CompoundStatements:
* `sem::IfStatement`
* `sem::ElseStatement`
* `sem::ForLoopStatement`
* `sem::LoopStatement`
* `sem::SwitchStatement`.
These new CompoundStatements are now inserted into the semantic
tree as now documented in `docs/compound_statements.md`.

The `sem::BlockStatement::FindFirstParent()` methods have been
moved down to `sem::Statement`.

The `Resolver::BlockScope()` method has been replaced with
`Resolver::Scope()` which now maintains the `current_statement_`,
`current_compound_statement_ ` and `current_block_`. This
simplifies statement nesting.

The most significant change in behavior is that statements now
always have a parent, so calling Block() on the initializer or
continuing of a for-loop statement will now return the
BlockStatement that holds the for-loop. Before this would
return nullptr.

Fixed: tint:979
Change-Id: I90e38fd719da2a281ed9210e975ab96171cb6842
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57707
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton
2021-07-14 09:44:41 +00:00
committed by Tint LUCI CQ
parent 3e27a20f31
commit 6e459fecb7
20 changed files with 1229 additions and 462 deletions

View File

@@ -21,14 +21,12 @@
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);
namespace tint {
namespace sem {
BlockStatement::BlockStatement(const ast::BlockStatement* declaration,
const Statement* parent)
const CompoundStatement* parent)
: Base(declaration, parent) {}
BlockStatement::~BlockStatement() = default;
@@ -47,25 +45,15 @@ FunctionBlockStatement::FunctionBlockStatement(const ast::Function* function)
FunctionBlockStatement::~FunctionBlockStatement() = default;
LoopBlockStatement::LoopBlockStatement(const ast::BlockStatement* declaration,
const Statement* parent)
: Base(declaration, parent) {}
const CompoundStatement* parent)
: Base(declaration, parent) {
TINT_ASSERT(Semantic, parent);
}
LoopBlockStatement::~LoopBlockStatement() = default;
void LoopBlockStatement::SetFirstContinue(size_t first_continue) {
first_continue_ = first_continue;
}
LoopContinuingBlockStatement::LoopContinuingBlockStatement(
const ast::BlockStatement* declaration,
const Statement* parent)
: Base(declaration, parent) {}
LoopContinuingBlockStatement::~LoopContinuingBlockStatement() = default;
SwitchCaseBlockStatement::SwitchCaseBlockStatement(
const ast::BlockStatement* declaration,
const Statement* parent)
: Base(declaration, parent) {}
SwitchCaseBlockStatement::~SwitchCaseBlockStatement() = default;
} // namespace sem
} // namespace tint

View File

@@ -34,13 +34,13 @@ namespace sem {
/// Holds semantic information about a block, such as parent block and variables
/// declared in the block.
class BlockStatement : public Castable<BlockStatement, Statement> {
class BlockStatement : public Castable<BlockStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
BlockStatement(const ast::BlockStatement* declaration,
const Statement* parent);
const CompoundStatement* parent);
/// Destructor
~BlockStatement() override;
@@ -49,33 +49,6 @@ class BlockStatement : public Castable<BlockStatement, Statement> {
/// 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 statement itself if it matches the template type `T`,
/// otherwise the nearest enclosing block that matches `T`, or nullptr if
/// there is none.
template <typename T>
const T* FindFirstParent() const {
const BlockStatement* curr = this;
while (curr) {
if (auto* block = curr->As<T>()) {
return block;
}
curr = curr->Block();
}
return nullptr;
}
/// @returns the declarations associated with this block
const std::vector<const ast::Variable*>& Decls() const { return decls_; }
@@ -105,14 +78,14 @@ class FunctionBlockStatement
ast::Function const* const function_;
};
/// Holds semantic information about a loop block or a for-loop block
/// Holds semantic information about a loop body block or for-loop body block
class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
LoopBlockStatement(const ast::BlockStatement* declaration,
const Statement* parent);
const CompoundStatement* parent);
/// Destructor
~LoopBlockStatement() override;
@@ -134,34 +107,6 @@ class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
size_t first_continue_ = kNoContinue;
};
/// Holds semantic information about a loop continuing block
class LoopContinuingBlockStatement
: public Castable<LoopContinuingBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
LoopContinuingBlockStatement(const ast::BlockStatement* declaration,
const Statement* parent);
/// Destructor
~LoopContinuingBlockStatement() override;
};
/// Holds semantic information about a switch case block
class SwitchCaseBlockStatement
: public Castable<SwitchCaseBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
SwitchCaseBlockStatement(const ast::BlockStatement* declaration,
const Statement* parent);
/// Destructor
~SwitchCaseBlockStatement() override;
};
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,31 @@
// 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/for_loop_statement.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::ForLoopStatement);
namespace tint {
namespace sem {
ForLoopStatement::ForLoopStatement(const ast::ForLoopStatement* declaration,
CompoundStatement* parent)
: Base(declaration, parent) {}
ForLoopStatement::~ForLoopStatement() = default;
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,45 @@
// 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_FOR_LOOP_STATEMENT_H_
#define SRC_SEM_FOR_LOOP_STATEMENT_H_
#include "src/sem/statement.h"
namespace tint {
namespace ast {
class ForLoopStatement;
} // namespace ast
} // namespace tint
namespace tint {
namespace sem {
/// Holds semantic information about a for-loop statement
class ForLoopStatement : public Castable<ForLoopStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this for-loop statement
/// @param parent the owning statement
ForLoopStatement(const ast::ForLoopStatement* declaration,
CompoundStatement* parent);
/// Destructor
~ForLoopStatement() override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_FOR_LOOP_STATEMENT_H_

38
src/sem/if_statement.cc Normal file
View File

@@ -0,0 +1,38 @@
// 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/if_statement.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::IfStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::ElseStatement);
namespace tint {
namespace sem {
IfStatement::IfStatement(const ast::IfStatement* declaration,
CompoundStatement* parent)
: Base(declaration, parent) {}
IfStatement::~IfStatement() = default;
ElseStatement::ElseStatement(const ast::ElseStatement* declaration,
CompoundStatement* parent)
: Base(declaration, parent) {}
ElseStatement::~ElseStatement() = default;
} // namespace sem
} // namespace tint

59
src/sem/if_statement.h Normal file
View File

@@ -0,0 +1,59 @@
// 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_IF_STATEMENT_H_
#define SRC_SEM_IF_STATEMENT_H_
#include "src/sem/statement.h"
// Forward declarations
namespace tint {
namespace ast {
class IfStatement;
class ElseStatement;
} // namespace ast
} // namespace tint
namespace tint {
namespace sem {
/// Holds semantic information about an if statement
class IfStatement : public Castable<IfStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this if statement
/// @param parent the owning statement
IfStatement(const ast::IfStatement* declaration, CompoundStatement* parent);
/// Destructor
~IfStatement() override;
};
/// Holds semantic information about an else statement
class ElseStatement : public Castable<ElseStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this else statement
/// @param parent the owning statement
ElseStatement(const ast::ElseStatement* declaration,
CompoundStatement* parent);
/// Destructor
~ElseStatement() override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_IF_STATEMENT_H_

40
src/sem/loop_statement.cc Normal file
View File

@@ -0,0 +1,40 @@
// 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/loop_statement.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::LoopContinuingBlockStatement);
namespace tint {
namespace sem {
LoopStatement::LoopStatement(const ast::LoopStatement* declaration,
CompoundStatement* parent)
: Base(declaration, parent) {}
LoopStatement::~LoopStatement() = default;
LoopContinuingBlockStatement::LoopContinuingBlockStatement(
const ast::BlockStatement* declaration,
const CompoundStatement* parent)
: Base(declaration, parent) {
TINT_ASSERT(Semantic, parent);
}
LoopContinuingBlockStatement::~LoopContinuingBlockStatement() = default;
} // namespace sem
} // namespace tint

60
src/sem/loop_statement.h Normal file
View File

@@ -0,0 +1,60 @@
// 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_LOOP_STATEMENT_H_
#define SRC_SEM_LOOP_STATEMENT_H_
#include "src/sem/block_statement.h"
// Forward declarations
namespace tint {
namespace ast {
class LoopStatement;
} // namespace ast
} // namespace tint
namespace tint {
namespace sem {
/// Holds semantic information about a loop statement
class LoopStatement : public Castable<LoopStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this loop statement
/// @param parent the owning statement
LoopStatement(const ast::LoopStatement* declaration,
CompoundStatement* parent);
/// Destructor
~LoopStatement() override;
};
/// Holds semantic information about a loop continuing block
class LoopContinuingBlockStatement
: public Castable<LoopContinuingBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
LoopContinuingBlockStatement(const ast::BlockStatement* declaration,
const CompoundStatement* parent);
/// Destructor
~LoopContinuingBlockStatement() override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_LOOP_STATEMENT_H_

View File

@@ -21,32 +21,31 @@
#include "src/sem/statement.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Statement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::CompoundStatement);
namespace tint {
namespace sem {
Statement::Statement(const ast::Statement* declaration, const Statement* parent)
Statement::Statement(const ast::Statement* declaration,
const CompoundStatement* parent)
: declaration_(declaration), parent_(parent) {}
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;
return FindFirstParent<BlockStatement>();
}
const ast::Function* Statement::Function() const {
if (auto* block = Block()) {
if (auto* fbs = block->FindFirstParent<FunctionBlockStatement>()) {
return fbs->Function();
}
if (auto* fbs = FindFirstParent<FunctionBlockStatement>()) {
return fbs->Function();
}
return nullptr;
}
CompoundStatement::CompoundStatement(const ast::Statement* declaration,
const CompoundStatement* parent)
: Base(declaration, parent) {}
CompoundStatement::~CompoundStatement() = default;
} // namespace sem
} // namespace tint

View File

@@ -31,19 +31,34 @@ class BlockStatement;
namespace tint {
namespace sem {
/// Forward declaration
class CompoundStatement;
/// 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 parent the owning statement
Statement(const ast::Statement* declaration, const Statement* parent);
Statement(const ast::Statement* declaration, const CompoundStatement* parent);
/// @return the AST node for this statement
const ast::Statement* Declaration() const { return declaration_; }
/// @return the statement that encloses this statement
const Statement* Parent() const { return parent_; }
const CompoundStatement* Parent() const { return parent_; }
/// @returns the closest enclosing parent that satisfies the given predicate,
/// which may be the statement itself, or nullptr if no match is found
/// @param pred a predicate that the resulting block must satisfy
template <typename Pred>
const CompoundStatement* FindFirstParent(Pred&& pred) const;
/// @returns the statement itself if it matches the template type `T`,
/// otherwise the nearest enclosing statement that matches `T`, or nullptr if
/// there is none.
template <typename T>
const T* FindFirstParent() const;
/// @return the closest enclosing block for this statement
const BlockStatement* Block() const;
@@ -53,9 +68,52 @@ class Statement : public Castable<Statement, Node> {
private:
ast::Statement const* const declaration_;
Statement const* const parent_;
CompoundStatement const* const parent_;
};
/// CompoundStatement is the base class of statements that can hold other
/// statements.
class CompoundStatement : public Castable<Statement, Statement> {
public:
/// Constructor
/// @param declaration the AST node for this statement
/// @param parent the owning statement
CompoundStatement(const ast::Statement* declaration,
const CompoundStatement* parent);
/// Destructor
~CompoundStatement() override;
};
template <typename Pred>
const CompoundStatement* Statement::FindFirstParent(Pred&& pred) const {
if (auto* self = As<CompoundStatement>()) {
if (pred(self)) {
return self;
}
}
const auto* curr = parent_;
while (curr && !pred(curr)) {
curr = curr->Parent();
}
return curr;
}
template <typename T>
const T* Statement::FindFirstParent() const {
if (auto* p = As<T>()) {
return p;
}
const auto* curr = parent_;
while (curr) {
if (auto* p = curr->As<T>()) {
return p;
}
curr = curr->Parent();
}
return nullptr;
}
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,40 @@
// 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/switch_statement.h"
#include "src/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::SwitchStatement);
TINT_INSTANTIATE_TYPEINFO(tint::sem::SwitchCaseBlockStatement);
namespace tint {
namespace sem {
SwitchStatement::SwitchStatement(const ast::SwitchStatement* declaration,
CompoundStatement* parent)
: Base(declaration, parent) {}
SwitchStatement::~SwitchStatement() = default;
SwitchCaseBlockStatement::SwitchCaseBlockStatement(
const ast::BlockStatement* declaration,
const CompoundStatement* parent)
: Base(declaration, parent) {
TINT_ASSERT(Semantic, parent);
}
SwitchCaseBlockStatement::~SwitchCaseBlockStatement() = default;
} // namespace sem
} // namespace tint

View File

@@ -0,0 +1,60 @@
// 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_SWITCH_STATEMENT_H_
#define SRC_SEM_SWITCH_STATEMENT_H_
#include "src/sem/block_statement.h"
// Forward declarations
namespace tint {
namespace ast {
class SwitchStatement;
} // namespace ast
} // namespace tint
namespace tint {
namespace sem {
/// Holds semantic information about an switch statement
class SwitchStatement : public Castable<SwitchStatement, CompoundStatement> {
public:
/// Constructor
/// @param declaration the AST node for this switch statement
/// @param parent the owning statement
SwitchStatement(const ast::SwitchStatement* declaration,
CompoundStatement* parent);
/// Destructor
~SwitchStatement() override;
};
/// Holds semantic information about a switch case block
class SwitchCaseBlockStatement
: public Castable<SwitchCaseBlockStatement, BlockStatement> {
public:
/// Constructor
/// @param declaration the AST node for this block statement
/// @param parent the owning statement
SwitchCaseBlockStatement(const ast::BlockStatement* declaration,
const CompoundStatement* parent);
/// Destructor
~SwitchCaseBlockStatement() override;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_SWITCH_STATEMENT_H_