#include "AST/StmtNode.h" #include "AST/DeclNode.h" #include "Sema/ASTVisitor.h" ExprStmtNode::ExprStmtNode(const SharedPtr &expr) : expr(expr) {} void ExprStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void ExprStmtNode::bindChildrenInversely() { expr->parent = shared_from_this(); } CompoundStmtNode::CompoundStmtNode(const SharedPtrVector &childStmts) : childStmts(childStmts) {} bool CompoundStmtNode::isEmpty() const { return childStmts.empty(); } void CompoundStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void CompoundStmtNode::bindChildrenInversely() { auto self = shared_from_this(); for (const SharedPtr &stmt: childStmts) { stmt->parent = self; } } void VarDeclStmtNode::pushVarDecl(const SharedPtr &varDecl) { childVarDecls.push_back(varDecl); } void VarDeclStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void VarDeclStmtNode::bindChildrenInversely() { auto self = shared_from_this(); for (const SharedPtr &varDecl: childVarDecls) { varDecl->parent = self; } } FunctionDeclStmtNode::FunctionDeclStmtNode( const SharedPtr &childFunctionDecl ) : childFunctionDecl(childFunctionDecl) {} void FunctionDeclStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void FunctionDeclStmtNode::bindChildrenInversely() { childFunctionDecl->parent = shared_from_this(); } IfStmtNode::IfStmtNode( const SharedPtr &condition, const SharedPtr &thenStmt, const SharedPtr &elseStmt ) : condition(condition), thenBody(thenStmt), elseBody(elseStmt) {} void IfStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void IfStmtNode::bindChildrenInversely() { auto self = shared_from_this(); condition->parent = thenBody->parent = self; if (elseBody) { elseBody->parent = self; } } WhileStmtNode::WhileStmtNode( const SharedPtr &condition, const SharedPtr &body ) : condition(condition), body(body) {} void WhileStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void WhileStmtNode::bindChildrenInversely() { condition->parent = body->parent = shared_from_this(); } ForStmtNode::ForStmtNode( const SharedPtr &forInitVarDecls, const SharedPtrVector &forInitExprList, const SharedPtr &forCondition, const SharedPtrVector &forUpdate, const SharedPtr &body ) : initVarStmt(forInitVarDecls), initExprs(forInitExprList), condition(forCondition), updates(forUpdate), body(body) {} void ForStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void ForStmtNode::bindChildrenInversely() { auto self = shared_from_this(); if (initVarStmt) { initVarStmt->parent = self; } for (const SharedPtr &forInitExpr: initExprs) { forInitExpr->parent = self; } if (condition) { condition->parent = self; } for (const SharedPtr &forUpdateItem: updates) { forUpdateItem->parent = self; } body->parent = self; } void ContinueStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void BreakStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } ReturnStmtNode::ReturnStmtNode(const SharedPtr &argument) : returnExpr(argument) {} void ReturnStmtNode::accept(const SharedPtr &visitor) { visitor->visit(staticPtrCast(shared_from_this())); } void ReturnStmtNode::bindChildrenInversely() { if (returnExpr) { returnExpr->parent = shared_from_this(); } }