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 <utility>
|
|
|
|
|
|
|
|
#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
|
2021-04-13 23:27:27 +00:00
|
|
|
/// @param program_id the identifier of the program that owns this node
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @param source the block statement source
|
2020-12-14 20:25:27 +00:00
|
|
|
/// @param statements the statements
|
2021-04-13 23:27:27 +00:00
|
|
|
BlockStatement(ProgramID program_id,
|
|
|
|
const Source& source,
|
|
|
|
const StatementList& statements);
|
2020-07-27 15:25:00 +00:00
|
|
|
/// Move constructor
|
|
|
|
BlockStatement(BlockStatement&&);
|
|
|
|
~BlockStatement() override;
|
|
|
|
|
2021-03-09 10:26:57 +00:00
|
|
|
/// @returns the StatementList
|
|
|
|
const StatementList& list() const { return statements_; }
|
|
|
|
|
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-12-02 15:31:08 +00:00
|
|
|
/// Retrieves the statement at `idx`
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @param idx the index. The index is not bounds checked.
|
2020-12-02 15:31:08 +00:00
|
|
|
/// @returns the statement at `idx`
|
2020-12-14 20:31:17 +00:00
|
|
|
Statement* get(size_t idx) const { return statements_[idx]; }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
2020-12-02 15:31:08 +00:00
|
|
|
/// Retrieves the statement at `idx`
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @param idx the index. The index is not bounds checked.
|
2020-12-02 15:31:08 +00:00
|
|
|
/// @returns the statement at `idx`
|
2020-12-14 20:31:17 +00:00
|
|
|
Statement* operator[](size_t idx) const { return statements_[idx]; }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
|
|
|
/// @returns the beginning iterator
|
2020-12-14 20:25:27 +00:00
|
|
|
StatementList::const_iterator begin() const { return statements_.begin(); }
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @returns the ending iterator
|
2020-12-14 20:25:27 +00:00
|
|
|
StatementList::const_iterator end() const { return statements_.end(); }
|
2020-07-27 15:25:00 +00:00
|
|
|
|
2021-03-31 20:35:46 +00:00
|
|
|
/// @returns the statement list
|
|
|
|
const StatementList& statements() const { return statements_; }
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// Clones this node and all transitive child nodes using the `CloneContext`
|
|
|
|
/// `ctx`.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @return the newly cloned node
|
|
|
|
BlockStatement* Clone(CloneContext* ctx) const override;
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
/// Writes a representation of the node to the output stream
|
2021-01-29 10:55:40 +00:00
|
|
|
/// @param sem the semantic info for the program
|
2020-07-27 15:25:00 +00:00
|
|
|
/// @param out the stream to write to
|
|
|
|
/// @param indent number of spaces to indent the node when writing
|
2021-04-16 19:07:51 +00:00
|
|
|
void to_str(const sem::Info& sem,
|
2021-01-29 10:55:40 +00:00
|
|
|
std::ostream& out,
|
|
|
|
size_t indent) const override;
|
2020-07-27 15:25:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
BlockStatement(const BlockStatement&) = delete;
|
|
|
|
|
2020-12-15 14:52:38 +00:00
|
|
|
StatementList const statements_;
|
2020-07-27 15:25:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_AST_BLOCK_STATEMENT_H_
|