blob 68ebd2ec (3858B) - Raw
1 // zcu.h — ported from src/Zcu.zig + src/Zcu/PerThread.zig. 2 // Holds all module-level state for a compilation unit. 3 #ifndef _ZIG0_ZCU_H__ 4 #define _ZIG0_ZCU_H__ 5 6 #include "ast.h" 7 #include "compilation.h" 8 #include "intern_pool.h" 9 #include "zir.h" 10 #include <stdbool.h> 11 #include <stdint.h> 12 13 // --- ZcuFile (matches Zcu.File) --- 14 // Per-source-file state. 15 16 #define ZCU_MAX_FILES 512 17 18 typedef struct { 19 char path[1024]; // canonical file path 20 char source_dir[1024]; // directory containing this file 21 Zir zir; // parsed ZIR (inst_len>0 if loaded) 22 Ast ast; // AST (kept alive to own source memory) 23 bool has_zir; // true if zir/ast are populated 24 bool analyzed; // true if createFileRootStructC has been called 25 } ZcuFile; 26 27 // --- ZcuNamespace (matches Zcu.Namespace) --- 28 29 #define ZCU_NS_MAX_NAVS 1024 30 #define ZCU_NS_MAX_COMPTIME 64 31 #define ZCU_MAX_NAMESPACES 512 32 33 typedef struct { 34 InternPoolIndex owner_type; // type_struct IP index 35 uint32_t file_idx; // index into files[] 36 uint32_t pub_navs[ZCU_NS_MAX_NAVS]; 37 uint32_t pub_nav_count; 38 uint32_t priv_navs[ZCU_NS_MAX_NAVS]; 39 uint32_t priv_nav_count; 40 uint32_t comptime_decls[ZCU_NS_MAX_COMPTIME]; // ZIR inst indices 41 uint32_t comptime_decl_count; 42 } ZcuNamespace; 43 44 // --- Zcu struct (matches Zcu.zig) --- 45 // Heap-allocated (use zcuInit/zcuDeinit); too large for stack. 46 47 #define NUM_BUILTIN_DECL_MAIN 36 48 49 typedef struct Zcu { 50 InternPool ip; // owns IP; matches Zcu.intern_pool 51 52 // --- File management (matches Zcu.import_table / Zcu.File) --- 53 ZcuFile files[ZCU_MAX_FILES]; // matches Zcu.files 54 uint32_t num_files; 55 uint32_t root_file_idx; 56 uint32_t std_file_idx; 57 uint32_t builtin_file_idx; 58 59 // --- Builtin namespace/nav --- 60 uint32_t cg_builtin_ns_idx; // compiler-generated builtin namespace 61 uint32_t cg_builtin_nav; 62 63 // --- Struct type hashing (C-specific) --- 64 uint32_t next_struct_hash; // unique hash counter for struct types 65 66 // --- Analysis state --- 67 bool in_main_analysis; 68 uint32_t enum_force_intern_threshold; 69 70 // --- Namespaces (matches Zcu.Namespace storage) --- 71 ZcuNamespace namespaces[ZCU_MAX_NAMESPACES]; 72 uint32_t num_namespaces; 73 InternPoolIndex file_root_types[ZCU_MAX_FILES]; 74 uint32_t file_namespaces[ZCU_MAX_FILES]; 75 76 // --- Type/layout resolution tracking --- 77 bool struct_layout_resolved[4096]; 78 bool struct_fully_resolved[4096]; 79 bool union_fully_resolved[4096]; 80 InternPoolIndex union_tag_enums[256]; 81 InternPoolIndex union_tag_types[256]; 82 uint32_t num_union_tag_enums; 83 84 // --- Memoized builtin state (matches Zcu.builtin_decl_values) --- 85 bool memoized_main_resolved; 86 bool cc_memoized_resolved; // builtins 0-14 resolved (CC phase) 87 bool full_memoized_resolved; // builtins 15-35 resolved (Type phase) 88 InternPoolIndex builtin_decl_values[NUM_BUILTIN_DECL_MAIN]; 89 90 // --- Preamble IP range tracking (C-specific shard simulation) --- 91 uint32_t preamble_memoized_start; 92 uint32_t preamble_memoized_end; 93 uint32_t preamble_cc_start; 94 uint32_t preamble_cc_end; 95 uint32_t preamble_builtin_navs[5]; 96 uint32_t preamble_builtin_nav_count; 97 // When true, resolveEnumDeclFromZir and resolveUnionDeclFromZir skip 98 // creating ptr_nav entries. Set during analyzeMemoizedStateC so that 99 // AS ptr_nav and CC ptr_nav are deferred to main analysis (matching 100 // Zig's sharded IP where preamble and main shards use different nav IDs). 101 bool preamble_skip_ptr_nav; 102 103 // --- Compilation config --- 104 Compilation* comp; // back-pointer; matches Zcu.comp in Zig 105 } Zcu; 106 107 // --- Function declarations --- 108 109 // Allocate and initialize a Zcu on the heap (too large for stack). 110 Zcu* zcuInit(Compilation* comp); 111 112 // Free all resources including the struct itself (matches zcuInit). 113 void zcuDeinit(Zcu* zcu); 114 115 #endif