// 将语法树翻译为 IR。
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。

#pragma once

#include <any>
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include "SysYBaseVisitor.h"
#include "SysYParser.h"
#include "ir/IR.h"
#include "sem/Sema.h"

namespace ir {
class Module;
class Function;
class IRBuilder;
class Value;
class BasicBlock;
}

class IRGenImpl final : public SysYBaseVisitor {
 public:
  IRGenImpl(ir::Module& module, const SemanticContext& sema);

  // Top-level rules
  std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override;
  std::any visitDecl(SysYParser::DeclContext* ctx) override;
  std::any visitConstDecl(SysYParser::ConstDeclContext* ctx) override;
  std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override;
  std::any visitConstDef(SysYParser::ConstDefContext* ctx) override;
  std::any visitVarDef(SysYParser::VarDefContext* ctx) override;
  std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override;
  std::any visitFuncFParam(SysYParser::FuncFParamContext* ctx) override;

  // Statement rules
  std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx) override;
  std::any visitBlockItem(SysYParser::BlockItemContext* ctx) override;
  std::any visitStmt(SysYParser::StmtContext* ctx) override;
  std::any visitAssignStmt(SysYParser::AssignStmtContext* ctx) override;
  std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override;
  std::any visitIfStmt(SysYParser::IfStmtContext* ctx) override;
  std::any visitWhileStmt(SysYParser::WhileStmtContext* ctx) override;
  std::any visitBreakStmt(SysYParser::BreakStmtContext* ctx) override;
  std::any visitContinueStmt(SysYParser::ContinueStmtContext* ctx) override;
  std::any visitExpStmt(SysYParser::ExpStmtContext* ctx) override;

  // Expression rules
  std::any visitParenExp(SysYParser::ParenExpContext* ctx) override;
  std::any visitLValueExp(SysYParser::LValueExpContext* ctx) override;
  std::any visitNumberExp(SysYParser::NumberExpContext* ctx) override;
  std::any visitFuncCallExp(SysYParser::FuncCallExpContext* ctx) override;
  std::any visitNotExp(SysYParser::NotExpContext* ctx) override;
  std::any visitUnaryAddExp(SysYParser::UnaryAddExpContext* ctx) override;
  std::any visitUnarySubExp(SysYParser::UnarySubExpContext* ctx) override;
  std::any visitMulDivModExp(SysYParser::MulDivModExpContext* ctx) override;
  std::any visitAddSubExp(SysYParser::AddSubExpContext* ctx) override;
  std::any visitRelExp(SysYParser::RelExpContext* ctx) override;
  std::any visitEqNeExp(SysYParser::EqNeExpContext* ctx) override;
  std::any visitAndExp(SysYParser::AndExpContext* ctx) override;
  std::any visitOrExp(SysYParser::OrExpContext* ctx) override;

 private:
  enum class BlockFlow {
    Continue,
    Terminated,
  };

  ir::Value* EvalExpr(SysYParser::ExpContext& expr);
  ir::ConstantValue* EvalConstExpr(SysYParser::ExpContext& expr);
  ir::Value* GetLValuePtr(SysYParser::LValueContext* ctx);
  ir::Value* DecayArrayPtr(SysYParser::LValueContext* ctx);
  bool IsArrayLikeDef(antlr4::ParserRuleContext* def) const;
  size_t GetArrayRank(antlr4::ParserRuleContext* def) const;
  std::shared_ptr<ir::Type> GetDefType(antlr4::ParserRuleContext* def) const;
  void ZeroInitializeLocal(ir::Value* ptr, std::shared_ptr<ir::Type> ty);
  void EmitLocalInitValue(ir::Value* ptr, std::shared_ptr<ir::Type> ty,
                          SysYParser::InitValueContext* init);

  ir::Module& module_;
  const SemanticContext& sema_;
  ir::Function* func_;
  ir::IRBuilder builder_;
  
  // Maps a definition (VarDef, ConstDef, FuncFParam) to its IR value (Alloca or GlobalValue)
  std::unordered_map<antlr4::ParserRuleContext*, ir::Value*> storage_map_;
  
  // For global scope tracking
  bool is_global_scope_ = true;
  
  // For loop control
  std::stack<ir::BasicBlock*> break_stack_;
  std::stack<ir::BasicBlock*> continue_stack_;
  
  // Helper to handle short-circuiting and comparison results
  ir::Value* ToI1(ir::Value* v);
  ir::Value* ToI32(ir::Value* v);
};

std::unique_ptr<ir::Module> GenerateIR(SysYParser::CompUnitContext& tree,
                                       const SemanticContext& sema);
