#pragma once

#include <memory>
#include <string_view>

#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Value.h>

#include "Ast.hh"

class CodeGenVisitor
{
public:
  explicit CodeGenVisitor (std::string_view moduleName = "CMinusModule");

  // Primary code generation entry point.
  void
  codeGen (const AstNodePtr& root);

  // Outputs the generated LLVM IR.
  void
  printIR (std::ostream& out = std::cout) const;

private:
  llvm::Value*
  visit (const AstNodePtr& node);

  // Node-specific codegen helpers.
  llvm::Value*
  codeGenNode (const ProgramNode& n);

  // ...

  llvm::Value*
  codeGenLValue (const VarExprNode& node);

private:
  // LLVM context infrastructure.
  std::unique_ptr<llvm::LLVMContext> context_;
  std::unique_ptr<llvm::Module> module_;
  std::unique_ptr<llvm::IRBuilder<>> builder_;

  // Code generation state tracking.
  llvm::Function* currentFunction_ { nullptr };
};
