//! Pure type-synthesis and IR-shaping helpers.
//!
//! Free functions extracted from `lib.rs` during the 18g tidy split.
//! None of these touch the `Emitter` state — they map AILang `Type`s
//! to LLVM type strings, mangling descriptors, or built-in op
//! signatures. Submodule access to the parent module's private
//! `Result`, `CodegenError`, and `FnSig` works through normal Rust
//! visibility (a submodule sees its parent's private items).

use ailang_core::ast::*;

use super::{CodegenError, FnSig, Result};

pub(crate) fn llvm_type(t: &Type) -> Result<String> {
    match t {
        Type::Con { name, .. } => match name.as_str() {
            "Int" => Ok("i64".into()),
            "Bool" => Ok("i1".into()),
            "Unit" => Ok("i8".into()),
            "Str" => Ok("ptr".into()),
            "Float" => Ok("double".into()),
            // All other type names are treated as ADT (boxed).
            // If the typechecker didn't reject this earlier, it's
            // intentional — otherwise `ptr` would mask a wrong value.
            _ => Ok("ptr".into()),
        },
        // Function values (Iter 7): all fn-pointers are opaque `ptr`
        // at the LLVM level. The actual signature travels via the
        // emitter's `ssa_fn_sigs` sidetable.
        Type::Fn { .. } => Ok("ptr".into()),
        // an unresolved rigid `Type::Var` reaching codegen is
        // a substitution bug. Earlier this silently lowered as `ptr`
        // (via the ADT fallback) and produced garbage IR; failing loudly
        // here surfaces the bug in the test suite.
        Type::Var { name } => Err(CodegenError::UnsupportedType(format!(
            "unresolved type var `{name}` in codegen"
        ))),
        other => Err(CodegenError::UnsupportedType(
            ailang_core::pretty::type_to_string(other),
        )),
    }
}

/// Builds an `FnSig` (LLVM types only) from an AILang `Type::Fn`.
/// Returns `None` for non-function types or if any param/ret type fails
/// to lower (e.g. a residual `Type::Var` or `Forall` that the typechecker
/// would reject before us).
pub(crate) fn fn_sig_from_type(t: &Type) -> Option<FnSig> {
    if let Type::Fn { params, ret, .. } = t {
        let p: Result<Vec<String>> = params.iter().map(llvm_type).collect();
        let r = llvm_type(ret);
        if let (Ok(p), Ok(r)) = (p, r) {
            return Some(FnSig { params: p, ret: r });
        }
    }
    None
}

// mir.1b: `builtin_ail_type` (the codegen-side mirror of a builtin's
// AILang type) and `builtin_effect_op_ret` were read only by the
// codegen-side type re-derivation deleted in this iteration. Codegen
// now reads `MTerm::ty()` off the typed MIR, so the mirror table is
// gone — both definitions removed.

// iter 23.4: `type_descriptor` was the helper that mangled a `Type`
// into an identifier-safe suffix for the codegen-side mono mangling
// (`Int → I`, `Bool → B`, etc.). The unified mono pass produces
// surface-named mono symbols instead (`Int → "Int"`, parameterised
// via 8-hex hash) — see `ailang_check::mono::mono_symbol_n`.

/// Floats iter 4.2: arithmetic / comparison ops are type-dispatched
/// over `{Int, Float}`. Caller (`lower_app`) reads the arg type off
/// `MTerm::ty()` and passes it here. Returns the
/// `(instruction, operand_llvm_type, result_llvm_type)` triple to
/// emit. `%` stays Int-only.
///
/// The triple carries operand and result types separately because
/// comparison ops produce `i1` regardless of operand width
/// (e.g. `icmp slt i64 ..., ... -> i1`); arithmetic produces the
/// same type as its operands. Keeping both in the table localises
/// the comparison-vs-arithmetic distinction here — the caller no
/// longer has to second-guess which arm fired.
///
/// Comparison-op Int arms are kept here in iter 4.2 to preserve
/// `builtin_binop_typed` is now arithmetic-only. Comparators
/// (`==` / `!=` / `<` / `<=` / `>` / `>=`) were removed in iter
/// operator-routing-eq-ord.1 — surface comparison routes through
/// the prelude.Eq / Ord class-method dispatch, with primitive
/// instance bodies emitted via `try_emit_primitive_instance_body`
/// in lib.rs (Eq Int / Bool / Str / Unit → `icmp`; Ord Int / Bool
/// / Str → `icmp` + Ordering ctor; float_eq / float_ne / float_lt
/// / float_le / float_gt / float_ge → `fcmp`).
pub(crate) fn builtin_binop_typed(
    name: &str,
    arg_ty: &Type,
) -> Option<(&'static str, &'static str, &'static str)> {
    let is_int = matches!(arg_ty, Type::Con { name, .. } if name == "Int");
    let is_float = matches!(arg_ty, Type::Con { name, .. } if name == "Float");
    match (name, is_int, is_float) {
        ("+", true, _) => Some(("add", "i64", "i64")),
        ("+", _, true) => Some(("fadd", "double", "double")),
        ("-", true, _) => Some(("sub", "i64", "i64")),
        ("-", _, true) => Some(("fsub", "double", "double")),
        ("*", true, _) => Some(("mul", "i64", "i64")),
        ("*", _, true) => Some(("fmul", "double", "double")),
        ("/", true, _) => Some(("sdiv", "i64", "i64")),
        ("/", _, true) => Some(("fdiv", "double", "double")),
        ("%", true, _) => Some(("srem", "i64", "i64")),
        _ => None,
    }
}

pub(crate) fn c_byte_len(s: &str) -> usize {
    s.len() + 1 // + NUL terminator
}

/// Escapes a string for LLVM IR `c"..."`. All bytes outside
/// 0x20..0x7E are escaped as `\HH`; `"` and `\` likewise. Ends with `\00`.
pub(crate) fn default_triple() -> &'static str {
    // In the MVP we query the compile host. For cross-compilation this
    // would need to be configurable — not needed now.
    if cfg!(target_os = "linux") && cfg!(target_arch = "x86_64") {
        "x86_64-pc-linux-gnu"
    } else if cfg!(target_os = "macos") && cfg!(target_arch = "aarch64") {
        "arm64-apple-darwin"
    } else if cfg!(target_os = "macos") && cfg!(target_arch = "x86_64") {
        "x86_64-apple-darwin"
    } else if cfg!(target_arch = "aarch64") {
        "aarch64-unknown-linux-gnu"
    } else {
        "x86_64-pc-linux-gnu"
    }
}

pub(crate) fn ll_string_literal(s: &str) -> String {
    let mut out = String::new();
    for &b in s.as_bytes() {
        match b {
            b'"' => out.push_str("\\22"),
            b'\\' => out.push_str("\\5C"),
            0x20..=0x7E => out.push(b as char),
            _ => out.push_str(&format!("\\{:02X}", b)),
        }
    }
    out.push_str("\\00");
    out
}
