// Copyright (c) 2020-now by the Zeek Project. See LICENSE for details. #pragma once #include #include #include #include #include #include namespace hilti::statement { /** AST node for a `while` statement. */ class While : public Statement { public: auto init() const { return child(0); } auto condition() const { return child<::hilti::Expression>(1); } auto body() const { return child(2); } auto else_() const { return child(3); } void setCondition(ASTContext* ctx, hilti::Expression* c) { setChild(ctx, 1, c); } void removeElse(ASTContext* ctx) { setChild(ctx, 3, nullptr); } static auto create(ASTContext* ctx, hilti::Declaration* init, hilti::Expression* cond, Statement* body, Statement* else_ = nullptr, Meta meta = {}) { return ctx->make(ctx, {init, cond, body, else_}, std::move(meta)); } static auto create(ASTContext* ctx, hilti::Expression* cond, Statement* body, Meta meta = {}) { return create(ctx, nullptr, cond, body, nullptr, std::move(meta)); } static auto create(ASTContext* ctx, hilti::Expression* cond, Statement* body, Statement* else_ = nullptr, Meta meta = {}) { return create(ctx, nullptr, cond, body, else_, std::move(meta)); } protected: While(ASTContext* ctx, Nodes children, Meta meta) : Statement(ctx, NodeTags, std::move(children), std::move(meta)) { if ( child(0) && ! child(0)->isA() ) logger().internalError("initialization for 'while' must be a local declaration"); } HILTI_NODE_1(statement::While, Statement, final); }; } // namespace hilti::statement