2020-07-27 15:25:00 +00:00
|
|
|
// Copyright 2020 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_AST_BLOCK_STATEMENT_H_
|
|
|
|
#define SRC_AST_BLOCK_STATEMENT_H_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "src/ast/statement.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
/// A block statement
|
2020-11-30 23:30:58 +00:00
|
|
|
class BlockStatement : public Castable<BlockStatement, Statement> {
|
2020-07-27 15:25:00 +00:00
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
BlockStatement();
|
|
|
|
/// Constructor
|
|
|
|
/// @param source the block statement source
|
|
|
|
explicit BlockStatement(const Source& source);
|
|
|
|
/// Move constructor
|
|
|
|
BlockStatement(BlockStatement&&);
|
|
|
|
~BlockStatement() override;
|
|
|
|
|
|
|
|
/// Appends a statement to the block
|
|
|
|
/// @param stmt the statement to append
|
2020-11-30 23:30:58 +00:00
|
|
|
void append(Statement* stmt) { statements_.push_back(stmt); }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
2020-07-30 13:59:00 +00:00
|
|
|
/// Insert a statement to the block
|
|
|
|
/// @param index the index to insert at
|
|
|
|
/// @param stmt the statement to insert
|
2020-11-30 23:30:58 +00:00
|
|
|
void insert(size_t index, Statement* stmt) {
|
2020-11-03 17:46:49 +00:00
|
|
|
auto offset = static_cast<decltype(statements_)::difference_type>(index);
|
2020-11-16 16:31:07 +00:00
|
|
|
statements_.insert(statements_.begin() + offset, stmt);
|
2020-07-30 13:59:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns true if the block is empty
|
|
|
|
bool empty() const { return statements_.empty(); }
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns the number of statements directly in the block
|
|
|
|
size_t size() const { return statements_.size(); }
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns the last statement in the block or nullptr if block empty
|
2020-11-30 23:30:58 +00:00
|
|
|
const Statement* last() const {
|
2020-11-16 16:31:07 +00:00
|
|
|
return statements_.empty() ? nullptr : statements_.back();
|
2020-07-27 15:25:00 +00:00
|
|
|
}
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns the last statement in the block or nullptr if block empty
|
2020-11-30 23:30:58 +00:00
|
|
|
Statement* last() {
|
2020-11-16 16:31:07 +00:00
|
|
|
return statements_.empty() ? nullptr : statements_.back();
|
2020-07-27 15:25:00 +00:00
|
|
|
}
|
2020-07-27 15:25:00 +00:00
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// Retrieves the statement at |idx|
|
|
|
|
/// @param idx the index. The index is not bounds checked.
|
|
|
|
/// @returns the statement at |idx|
|
2020-11-30 23:30:58 +00:00
|
|
|
const Statement* get(size_t idx) const { return statements_[idx]; }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// Retrieves the statement at |idx|
|
|
|
|
/// @param idx the index. The index is not bounds checked.
|
|
|
|
/// @returns the statement at |idx|
|
2020-11-30 23:30:58 +00:00
|
|
|
Statement* operator[](size_t idx) { return statements_[idx]; }
|
2020-07-27 15:25:00 +00:00
|
|
|
/// Retrieves the statement at |idx|
|
|
|
|
/// @param idx the index. The index is not bounds checked.
|
|
|
|
/// @returns the statement at |idx|
|
2020-11-30 23:30:58 +00:00
|
|
|
const Statement* operator[](size_t idx) const { return statements_[idx]; }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
|
|
|
/// @returns the beginning iterator
|
2020-11-30 23:30:58 +00:00
|
|
|
std::vector<Statement*>::const_iterator begin() const {
|
2020-07-27 15:25:00 +00:00
|
|
|
return statements_.begin();
|
|
|
|
}
|
|
|
|
/// @returns the ending iterator
|
2020-11-30 23:30:58 +00:00
|
|
|
std::vector<Statement*>::const_iterator end() const {
|
2020-07-27 15:25:00 +00:00
|
|
|
return statements_.end();
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// Clones this node and all transitive child nodes using the `CloneContext`
|
|
|
|
/// `ctx`.
|
|
|
|
/// @note Semantic information such as resolved expression type and intrinsic
|
|
|
|
/// information is not cloned.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @return the newly cloned node
|
|
|
|
BlockStatement* Clone(CloneContext* ctx) const override;
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns true if the node is valid
|
|
|
|
bool IsValid() const override;
|
|
|
|
|
|
|
|
/// Writes a representation of the node to the output stream
|
|
|
|
/// @param out the stream to write to
|
|
|
|
/// @param indent number of spaces to indent the node when writing
|
|
|
|
void to_str(std::ostream& out, size_t indent) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
BlockStatement(const BlockStatement&) = delete;
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
std::vector<Statement*> statements_;
|
2020-07-27 15:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_BLOCK_STATEMENT_H_
|