// zcu.h — ported from src/Zcu.zig + src/Zcu/PerThread.zig.
// Holds all module-level state for a compilation unit.
#ifndef _ZIG0_ZCU_H__
#define _ZIG0_ZCU_H__

#include "ast.h"
#include "compilation.h"
#include "intern_pool.h"
#include "zir.h"
#include <stdbool.h>
#include <stdint.h>

// --- ZcuFile (matches Zcu.File) ---
// Per-source-file state.

#define ZCU_MAX_FILES 512

typedef struct {
    char path[1024]; // canonical file path
    char source_dir[1024]; // directory containing this file
    Zir zir; // parsed ZIR (inst_len>0 if loaded)
    Ast ast; // AST (kept alive to own source memory)
    bool has_zir; // true if zir/ast are populated
    bool analyzed; // true if createFileRootStructC has been called
} ZcuFile;

// --- ZcuNamespace (matches Zcu.Namespace) ---

#define ZCU_NS_MAX_NAVS 1024
#define ZCU_NS_MAX_COMPTIME 64
#define ZCU_MAX_NAMESPACES 512

typedef struct {
    InternPoolIndex owner_type; // type_struct IP index
    uint32_t file_idx; // index into files[]
    uint32_t pub_navs[ZCU_NS_MAX_NAVS];
    uint32_t pub_nav_count;
    uint32_t priv_navs[ZCU_NS_MAX_NAVS];
    uint32_t priv_nav_count;
    uint32_t comptime_decls[ZCU_NS_MAX_COMPTIME]; // ZIR inst indices
    uint32_t comptime_decl_count;
} ZcuNamespace;

// --- Zcu struct (matches Zcu.zig) ---
// Heap-allocated (use zcuInit/zcuDeinit); too large for stack.

#define NUM_BUILTIN_DECL_MAIN 36

typedef struct Zcu {
    InternPool ip; // owns IP; matches Zcu.intern_pool

    // --- File management (matches Zcu.import_table / Zcu.File) ---
    ZcuFile files[ZCU_MAX_FILES]; // matches Zcu.files
    uint32_t num_files;
    uint32_t root_file_idx;
    uint32_t std_file_idx;
    uint32_t builtin_file_idx;

    // --- Builtin namespace/nav ---
    uint32_t cg_builtin_ns_idx; // compiler-generated builtin namespace
    uint32_t cg_builtin_nav;

    // --- Struct type hashing (C-specific) ---
    uint32_t next_struct_hash; // unique hash counter for struct types

    // --- Analysis state ---
    bool in_main_analysis;
    uint32_t enum_force_intern_threshold;

    // --- Namespaces (matches Zcu.Namespace storage) ---
    ZcuNamespace namespaces[ZCU_MAX_NAMESPACES];
    uint32_t num_namespaces;
    InternPoolIndex file_root_types[ZCU_MAX_FILES];
    uint32_t file_namespaces[ZCU_MAX_FILES];

    // --- Type/layout resolution tracking ---
    bool struct_layout_resolved[4096];
    bool struct_fully_resolved[4096];
    bool union_fully_resolved[4096];
    InternPoolIndex union_tag_enums[256];
    InternPoolIndex union_tag_types[256];
    uint32_t num_union_tag_enums;

    // --- Memoized builtin state (matches Zcu.builtin_decl_values) ---
    bool memoized_main_resolved;
    bool cc_memoized_resolved; // builtins 0-14 resolved (CC phase)
    bool full_memoized_resolved; // builtins 15-35 resolved (Type phase)
    InternPoolIndex builtin_decl_values[NUM_BUILTIN_DECL_MAIN];

    // --- Preamble IP range tracking (C-specific shard simulation) ---
    uint32_t preamble_memoized_start;
    uint32_t preamble_memoized_end;
    uint32_t preamble_cc_start;
    uint32_t preamble_cc_end;
    uint32_t preamble_builtin_navs[5];
    uint32_t preamble_builtin_nav_count;
    // When true, resolveEnumDeclFromZir and resolveUnionDeclFromZir skip
    // creating ptr_nav entries.  Set during analyzeMemoizedStateC so that
    // AS ptr_nav and CC ptr_nav are deferred to main analysis (matching
    // Zig's sharded IP where preamble and main shards use different nav IDs).
    bool preamble_skip_ptr_nav;

    // --- Compilation config ---
    Compilation* comp; // back-pointer; matches Zcu.comp in Zig
} Zcu;

// --- Function declarations ---

// Allocate and initialize a Zcu on the heap (too large for stack).
Zcu* zcuInit(Compilation* comp);

// Free all resources including the struct itself (matches zcuInit).
void zcuDeinit(Zcu* zcu);

#endif
