tl-lang 0.3.9

A differentiable programming language with tensor support for machine learning
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
use super::CodeGenerator;
use crate::compiler::ast::{Type, mangle_wrap_args, mangle_base_name, mangle_has_args, mangle_extract_args, parse_mangled_type_strs};
use crate::compiler::mangler::MANGLER;
use crate::compiler::ast_subst::TypeSubstitutor;
use inkwell::types::{BasicTypeEnum, StructType};
use inkwell::AddressSpace;
use std::collections::HashMap;

impl<'ctx> CodeGenerator<'ctx> {
    /// On-demand monomorphization of a method for a generic struct.
    /// On-demand monomorphization of a method for a generic struct.
    pub fn monomorphize_method(
        &mut self,
        struct_name: &str,
        method_name: &str,
        generic_args: &[Type],
    ) -> Result<String, String> {

        let impls = self.generic_impls.get(struct_name)
             .ok_or_else(|| format!("No generic impls found for struct {}", struct_name))?;

        // Find method in impls
        let mut target_method = None;
        let mut target_impl = None;
        
        for imp in impls {
            for method in &imp.methods {
                 if method.name == method_name {
                     target_method = Some(method);
                     target_impl = Some(imp);
                     break;
                 }
            }
            if target_method.is_some() { break; }
        }
        
        let method = target_method.ok_or_else(|| format!("Method {} not found in generic impls of {}", method_name, struct_name))?;
        let imp = target_impl.unwrap();

        // Check and fix generic count - pad with default type if insufficient
        let mut final_generic_args = generic_args.to_vec();
        if imp.generics.len() > generic_args.len() {
            // Pad with I64 as default type for missing generics
            for _ in 0..(imp.generics.len() - generic_args.len()) {
                final_generic_args.push(Type::I64);
            }
        } else if imp.generics.len() < generic_args.len() {
            // Too many generics - truncate to expected count
            final_generic_args.truncate(imp.generics.len());
        }
        let generic_args = &final_generic_args;

        // Build substitution map
        let mut subst_map = HashMap::new();
        for (param, arg) in imp.generics.iter().zip(generic_args) {
             subst_map.insert(param.clone(), arg.clone());
        }

        // Use standard mangling resolution
        let mangled_name = crate::compiler::codegen::builtin_types::resolver::resolve_static_method_name(struct_name, method_name, generic_args);
        
        if self.module.get_function(&mangled_name).is_some() {

            return Ok(mangled_name);
        }

        // Instantiate
        let substitutor = TypeSubstitutor::new(subst_map);
        let mut new_method = method.clone();
        new_method.name = mangled_name.clone(); 
        new_method.generics = vec![]; // Concrete
        
        let concrete_self = if self.enum_defs.contains_key(struct_name) {
            Type::Enum(struct_name.to_string(), generic_args.to_vec())
        } else {
            Type::Struct(struct_name.to_string(), generic_args.to_vec())
        };
        let mut full_map = substitutor.subst.clone();
        full_map.insert("Self".to_string(), concrete_self);
        let full_substitutor = TypeSubstitutor::new(full_map);

        // Substitute
        for (_, ty) in &mut new_method.args {
            *ty = full_substitutor.substitute_type(ty);
            *ty = self.normalize_type(ty);
        }
        new_method.return_type = full_substitutor.substitute_type(&new_method.return_type);
        new_method.return_type = self.normalize_type(&new_method.return_type);
        new_method.body = new_method.body.iter().map(|s| full_substitutor.substitute_stmt(s)).collect();
        
        // Transform StaticMethodCall to EnumInit for enum variant constructors

        self.transform_method_body_enum_inits(&mut new_method.body);
        
        // Compile
        // Save current builder position
        let previous_block = self.builder.get_insert_block();

        self.compile_fn_proto(&new_method)?;
        
        // NEW: If extern, we are done (declaration only). Otherwise compile body.
        if !new_method.is_extern {
            self.pending_functions.push(new_method);
        }
        
        // Restore builder position
        if let Some(block) = previous_block {
            self.builder.position_at_end(block);
        }
        
        Ok(mangled_name)
    }

    /// On-demand monomorphization of a user-defined generic function.
    pub fn monomorphize_generic_function(
        &mut self,
        func_name: &str,
        arg_types: &[Type],
    ) -> Result<String, String> {
        // 1. Check if the function exists in generic registry
        if !self.generic_fn_defs.contains_key(func_name) {
             // Not a generic function, or not found.
             return Ok(func_name.to_string());
        }
        
        // 2. Retrieve definition
        let func_def = self.generic_fn_defs.get(func_name).cloned().unwrap();
        
        // 3. Unify argument types to infer type parameters
        // func_def.generics vs func_def.args vs arg_types
        if func_def.args.len() != arg_types.len() {
             return Err(format!("Argument count mismatch for generic function {}: expected {}, got {}", 
                 func_name, func_def.args.len(), arg_types.len()));
        }

        let mut subst_map: HashMap<String, Type> = HashMap::new();
        for ((_, expected_ty), actual_ty) in func_def.args.iter().zip(arg_types) {
            self.unify_types(expected_ty, actual_ty, &mut subst_map)?;
        }
        
        // Ensure all generics are inferred
        for param in &func_def.generics {
            if !subst_map.contains_key(param) {
                 return Err(format!("Could not infer type parameter {} for function {}", param, func_name));
            }
        }
        
        // 4. Mangle name based on concrete types
        // Create a list of type args in the order of declaration
        let type_args: Vec<Type> = func_def.generics.iter().map(|g| subst_map[g].clone()).collect();
        let args_str: Vec<String> = type_args.iter().map(|t| self.type_to_suffix(t)).collect();
        let mangled_name = mangle_wrap_args(func_name, &args_str);
             
        if self.module.get_function(&mangled_name).is_some() {
            return Ok(mangled_name);
        }
        
        // 6. Instantiate
        let substitutor = TypeSubstitutor::new(subst_map);
        
        let mut new_func = func_def.clone();
        new_func.name = mangled_name.clone();
        new_func.generics = vec![]; // Concrete now
        
        // Substitute args
        for (_, ty) in &mut new_func.args {
            *ty = substitutor.substitute_type(ty);
        }
        // Substitute return type
        new_func.return_type = substitutor.substitute_type(&new_func.return_type);
        // Substitute body
        new_func.body = new_func.body.iter().map(|s| substitutor.substitute_stmt(s)).collect();
        
        // 7. Compile
        // Need to register proto first (for recursion support etc)
        self.compile_fn_proto(&new_func)?;
        self.pending_functions.push(new_func);
        
        Ok(mangled_name)
    }

    /// On-demand monomorphization of a generic enum.
    pub fn monomorphize_enum(
        &mut self,
        enum_name: &str,
        generic_args: &[Type],
    ) -> Result<String, String> {
        // Early return: if enum_name is already a mangled name that exists in enum_types, done.
        if self.enum_types.contains_key(enum_name) {
            return Ok(enum_name.to_string());
        }
        // Also check if we can mangle from base + args and find it
        if !generic_args.is_empty() {
            let base = mangle_base_name(enum_name);
            let candidate = self.mangle_type_name(base, generic_args);
            if self.enum_types.contains_key(&candidate) {
                return Ok(candidate);
            }
        }

        // 1. Check if the enum exists in generic registry
        // First, try direct lookup
        let enum_def = if let Some(def) = self.enum_defs.get(enum_name) {
            def.clone()
        } else if mangle_has_args(enum_name) && !enum_name.contains('<') {
            // Try extracting base name from mangled name (e.g., "Option[i64]" -> "Option")
            let base_name = mangle_base_name(enum_name);
            if let Some(def) = self.enum_defs.get(base_name) {
                // If generic_args is empty but the def needs generics, try to parse from name
                if generic_args.is_empty() && !def.generics.is_empty() {
                    // This is already monomorphized with a different naming scheme
                    // Extract type args from bracket notation
                    let arg_strs = mangle_extract_args(enum_name);
                    let inferred_generics = parse_mangled_type_strs(&arg_strs);
                    // Try to find or create the angle-bracket version
                    let angle_name = self.mangle_type_name(base_name, &inferred_generics);
                    if let Some(_existing) = self.enum_types.get(&angle_name) {
                        // Already exists
                        return Ok(angle_name);
                    }
                    // Recursively monomorphize with inferred generics
                    return self.monomorphize_enum(base_name, &inferred_generics);
                }
                def.clone()
            } else {
                return Err(format!("Enum {} not found (tried base {})", enum_name, base_name));
            }
        } else {
            return Err(format!("Enum {} not found", enum_name));
        };

        // 2. Check generics
        if enum_def.generics.len() != generic_args.len() {
             return Err(format!("Generic count mismatch for enum {}: expected {}, got {}", 
                 enum_name, enum_def.generics.len(), generic_args.len()));
        }

        // 3. Mangle - use base name to avoid double-mangling (e.g. Entry[i64][i64] -> Entry[i64][i64][i64][i64])
        let base = mangle_base_name(enum_name);
        let mangled_name = self.mangle_type_name(base, generic_args);
        
        // 4. Check if already instantiated
        if self.enum_types.contains_key(&mangled_name) {
             return Ok(mangled_name);
        }
        
        // Register this specialization
        self.specialization_registry.register(enum_name, generic_args);

        // 5. Instantiate
        // Build substitution map
        let mut subst_map = HashMap::new();
        for (param, arg) in enum_def.generics.iter().zip(generic_args) {
             subst_map.insert(param.clone(), arg.clone());
        }
        let substitutor = TypeSubstitutor::new(subst_map);

        let mut new_def = enum_def.clone();
        new_def.name = mangled_name.clone();
        new_def.generics = vec![];

        // Substitute variants and convert generic types to UnifiedType
        for variant in &mut new_def.variants {
             match &mut variant.kind {
                 crate::compiler::ast::VariantKind::Unit => {},
                 crate::compiler::ast::VariantKind::Tuple(types) => {
                     for t in types.iter_mut() {
                         *t = substitutor.substitute_type(t);
                         *t = self.to_unified_type_if_generic(t.clone());
                     }
                 }
                 crate::compiler::ast::VariantKind::Struct(fields) => {
                     for (_, t) in fields.iter_mut() {
                         *t = substitutor.substitute_type(t);
                         *t = self.to_unified_type_if_generic(t.clone());
                     }
                 }
                 crate::compiler::ast::VariantKind::Array(ty, _size) => {
                     *ty = substitutor.substitute_type(ty);
                     *ty = self.to_unified_type_if_generic(ty.clone());
                 }

             }
        }


        // 6. Pre-monomorphize nested generic types in variant payloads
        // e.g., for Option<Pair<i64>>, ensure Pair_i64 is monomorphized before compiling the enum
        for variant in &new_def.variants {
            let types_to_check: Vec<&Type> = match &variant.kind {
                crate::compiler::ast::VariantKind::Unit => vec![],
                crate::compiler::ast::VariantKind::Tuple(types) => types.iter().collect(),
                crate::compiler::ast::VariantKind::Struct(fields) => fields.iter().map(|(_, t)| t).collect(),
                crate::compiler::ast::VariantKind::Array(ty, size) => vec![ty; *size],
            };
            for ty in types_to_check {
                let _ = self.get_or_monomorphize_type(ty)?;
            }
        }

        // 7. Compile/Register
        // We can reuse compile_enum_defs. It handles LLVM struct creation and map insertion.
        self.compile_enum_defs(&[new_def.clone()])
            .map_err(|e| e.to_string())?;

        // Note: compile_enum_defs adds it to `enum_defs` and `enum_types`
        
        Ok(mangled_name)
    }

    fn unify_types(
        &self,
        expected: &Type,
        actual: &Type,
        map: &mut HashMap<String, Type>,
    ) -> Result<(), String> {
         match (expected, actual) {
             (Type::Struct(name, args), _) => {
                 // If It's a Type Parameter (no args), infer it.
                 // Need to know if 'name' is a generic param.
                 // For now assume if it's in the generic list it is.
                 // But here we don't have scope.
                 // However, UserDefined type params usually have empty args.
                 if args.is_empty() {
                     // Check if already mapped
                     if let Some(existing) = map.get(name) {
                         if existing != actual {
                              return Err(format!("Type mismatch for generic {}: expected {:?}, got {:?}", name, existing, actual));
                         }
                     } else {
                         // Map it!
                         // But wait, what if 'name' is a concrete type? We shouldn't map it to actual.
                         // We should only map if 'name' is in Fn generics.
                         // But unification helper doesn't know the list.
                         // We can assume valid AST would prevent Shadowing of Types by generic params.
                         // So if we see UserDefined("T"), we map it.
                         // Ideally we should pass the set of generic params to safe guard.
                         map.insert(name.clone(), actual.clone());
                     }
                     return Ok(());
                 }
                 // If recursive generic (e.g. MyStruct<T>)
                 if let Type::Struct(act_name, act_args) = actual {
                     if name != act_name || args.len() != act_args.len() {
                          return Err("Type mismatch or arity mismatch".into());
                     }
                     for (e, a) in args.iter().zip(act_args) {
                         self.unify_types(e, a, map)?;
                     }
                     return Ok(());
                 }
                 // If structural match fails
                 // Might handle Struct vs UserDefined aliases?
                 // Check if it's a generic struct instance matching a concrete struct
                 // This logic might be redundant if the above arm handles it, 
                 // but kept for "Any" matching or partial unification if needed.
                 // For now, if we are UNIFYING, we usually want exact structure.
                 // If `expected` is generic T and `actual` is Struct, handled by (Type::Generic, _)
                 
                 // If we are here, we have Struct vs something else (not Struct).
                 // e.g. Struct vs Tensor? Error.
                 return Err(format!("Type mismatch: Expected Struct {}, found {:?}", name, actual));
             }
             (Type::Tensor(e, r), Type::Tensor(a, ar)) => {
                 if r != ar { return Err("Rank mismatch".into()); }
                 self.unify_types(e, a, map)?;
             }
             (Type::Array(e_inner, e_size), Type::Array(a_inner, a_size)) => {
                 if e_size != a_size { return Err(format!("Array size mismatch: expected {}, got {}", e_size, a_size)); }
                 self.unify_types(e_inner, a_inner, map)?;
             }
             // Add other structural recursions as needed
             _ => {
                 if expected != actual {
                     // If they are different concrete types, error.
                     // But wait, what if expected is concrete? (e.g. Fn(i64, T))
                     return Err(format!("Type mismatch: expected {:?}, got {:?}", expected, actual));
                 }
             }
         }
         Ok(())
    }

    /// Generate a mangled name for a monomorphized type.
    /// Example: `Vec` + `[i64]` -> `Vec[i64]`
    pub fn mangle_type_name(&self, base_name: &str, type_args: &[Type]) -> String {
        if type_args.is_empty() {
            base_name.to_string()
        } else {
            let args_str: Vec<String> = type_args.iter().map(|t| self.type_to_suffix(t)).collect();
            mangle_wrap_args(base_name, &args_str)
        }
    }
    
    /// Convert a Type to a string representation for mangling/display.
    pub fn type_to_suffix(&self, ty: &Type) -> String {
        match ty {
            Type::I64 => "i64".to_string(),
            Type::I32 => "i32".to_string(),
            Type::U8 => "u8".to_string(),
            Type::F32 => "f32".to_string(),
            Type::F64 => "f64".to_string(),
            Type::Bool => "bool".to_string(),
            Type::Usize => "usize".to_string(),
            Type::Void => "void".to_string(),
            Type::String(_) => "String".to_string(),
            Type::Char(_) => "Char".to_string(),
            Type::Struct(name, args) => {
                if args.is_empty() || mangle_has_args(name) {
                    // Already mangled or no args: use name directly
                    name.clone()
                } else {
                    self.mangle_type_name(name, args)
                }
            }
            Type::Enum(name, args) => {
                if args.is_empty() || mangle_has_args(name) {
                    name.clone()
                } else {
                    self.mangle_type_name(name, args)
                }
            }
            Type::UnifiedType { mangled_name, .. } => {
                mangled_name.clone()
            }

            Type::Tensor(inner, rank) => {
                let args = vec![self.type_to_suffix(inner), rank.to_string()];
                mangle_wrap_args("Tensor", &args)
            }
            Type::Tuple(types) => {
                let parts: Vec<String> = types.iter().map(|t| self.type_to_suffix(t)).collect();
                mangle_wrap_args("Tuple", &parts)
            }
            Type::Path(path, args) => {
                // Path types represent generic parameters or type references
                // Use the last segment of the path
                if let Some(name) = path.last() {
                    if args.is_empty() {
                        name.clone()
                    } else {
                        self.mangle_type_name(name, args)
                    }
                } else {
                    "unknown_path".to_string()
                }
            }
            Type::Undefined(id) => format!("undefined{}", MANGLER.wrap_single(&id.to_string())),
            Type::Ptr(inner) => format!("ptr{}", MANGLER.wrap_single(&self.type_to_suffix(inner))),
            Type::Array(inner, size) => {
                let args = vec![self.type_to_suffix(inner), size.to_string()];
                mangle_wrap_args("Array", &args)
            }
            Type::Entity => "entity".to_string(),
            _ => "unknown".to_string(),
        }
    }

    /// Mangle a method name for a generic type.
    /// Example: mangle_generic_method("Container", [U8], "process") -> "tl_container_u8_process"
    pub fn mangle_generic_method(
        &self,
        base_type: &str,
        type_args: &[Type],
        method: &str,
    ) -> String {
        let suffix = if type_args.is_empty() {
            String::new()
        } else {
            type_args.iter()
                .map(|t| MANGLER.wrap_single(&self.type_to_suffix(t).to_lowercase()))
                .collect::<String>()
        };
        format!("tl_{}{}_{}", base_type.to_lowercase(), suffix, method)
    }

    /// Get or create the LLVM type for the given AST Type.
    /// This is the single source of truth for Type -> BasicTypeEnum conversion.
    /// Note: This version uses &self and does NOT perform on-demand monomorphization.
    /// For monomorphization, use `get_or_monomorphize_type` which takes &mut self.
    pub fn get_llvm_type(&self, ty: &Type) -> Result<BasicTypeEnum<'ctx>, String> {
        match ty {
            Type::Ptr(_) => Ok(self.context.ptr_type(AddressSpace::default()).into()),
            Type::I64 | Type::Entity => Ok(self.context.i64_type().into()),
            Type::I32 => Ok(self.context.i32_type().into()),
            Type::F32 => Ok(self.context.f32_type().into()),
            Type::F64 => Ok(self.context.f64_type().into()),
            Type::Bool => Ok(self.context.bool_type().into()),
            Type::U8 => Ok(self.context.i8_type().into()), // Added U8 support
            Type::Usize => Ok(self.context.i64_type().into()), // usize as i64
            Type::Void => panic!("Void type encountered in get_llvm_type"),
            
            Type::Tensor(_, _) | Type::TensorShaped(_, _) => {
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            
            Type::String(_) => {
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            Type::Char(_) => {
                Ok(self.context.i32_type().into())
            }

            Type::Struct(name, _args) => {
                // Compatibility: Handle primitives parsed as UserDefined
                match name.as_str() {
                    "bool" => return Ok(self.context.bool_type().into()),
                    "i64" => return Ok(self.context.i64_type().into()),
                    "i32" => return Ok(self.context.i32_type().into()),
                    "f32" => return Ok(self.context.f32_type().into()),
                    "f64" => return Ok(self.context.f64_type().into()),
                    "usize" => return Ok(self.context.i64_type().into()),
                    "u8" => return Ok(self.context.i8_type().into()), // Added u8 support
                    "String" => return Ok(self.context.ptr_type(inkwell::AddressSpace::default()).into()),
                    _ => {}
                }

                // Handle special types
                if name == "File" || name == "Path" || name == "Env" || name == "Http" {
                    return Ok(self.context.ptr_type(AddressSpace::default()).into());
                }

                // Check for ZSTs
                let simple_name = name.as_str();

                if let Some(def) = self.struct_defs.get(simple_name) {
                    if def.fields.is_empty() {
                        // ZST = Pointer (NULL)
                        return Ok(self.context.ptr_type(AddressSpace::default()).into());
                    }
                }
                
                if name.contains("PhantomData") {
                }

                // All other structs are pointer types (Reference Semantics)
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            
            Type::Enum(_name, _args) => {
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }

            Type::Array(inner, size) => {
                let elem_ty = self.get_llvm_type(inner)?;
                // Build LLVM array type from element type
                match elem_ty {
                    BasicTypeEnum::IntType(t) => Ok(t.array_type(*size as u32).into()),
                    BasicTypeEnum::FloatType(t) => Ok(t.array_type(*size as u32).into()),
                    BasicTypeEnum::PointerType(t) => Ok(t.array_type(*size as u32).into()),
                    BasicTypeEnum::StructType(t) => Ok(t.array_type(*size as u32).into()),
                    BasicTypeEnum::ArrayType(t) => Ok(t.array_type(*size as u32).into()),
                    BasicTypeEnum::VectorType(t) => Ok(t.array_type(*size as u32).into()),
                    _ => Err(format!("Unsupported array element type: {:?}", elem_ty)),
                }
            }
            
            Type::Tuple(_) => {
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }

            // Type::Ref(_) => { // REMOVED - Ref not in spec
            //     Ok(self.context.ptr_type(AddressSpace::default()).into())
            // }

            Type::Path(_segments, _) => {
                 // Assume Path resolves to Struct/Enum which are pointers (in this phase)
                 Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            
            _ => {
                // Default to i64 for unknown types
                Ok(self.context.i64_type().into())
            }
        }
    }
    
    /// Get or create the LLVM type, performing on-demand monomorphization if needed.
    /// This version takes &mut self and can create new struct definitions.
    pub fn get_or_monomorphize_type(&mut self, ty: &Type) -> Result<BasicTypeEnum<'ctx>, String> {
        match ty {
            Type::Struct(name, args) if !args.is_empty() => {
                // Generic struct: monomorphize
                let _ = self.monomorphize_struct(name, args)?;
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            Type::Enum(name, args) if !args.is_empty() => {
                // Generic enum: monomorphize
                let _ = self.monomorphize_enum(name, args)?;
                Ok(self.context.ptr_type(AddressSpace::default()).into())
            }
            _ => self.get_llvm_type(ty),
        }
    }

    /// Monomorphize a generic struct definition with concrete type arguments.
    /// Returns the LLVM StructType for the specialized struct.
    pub fn monomorphize_struct(
        &mut self,
        base_name: &str,
        type_args: &[Type],
    ) -> Result<StructType<'ctx>, String> {
        let mangled_name = self.mangle_type_name(base_name, type_args);
        
        // Check if already monomorphized
        if let Some(existing) = self.struct_types.get(&mangled_name) {
            return Ok(*existing);
        }
        
        // Get the generic struct definition
        let struct_def = self.struct_defs.get(base_name).cloned()
            .ok_or_else(|| format!("Generic struct definition not found: {}", base_name))?;
        
        // Register this specialization
        self.specialization_registry.register(base_name, type_args);
        
        // Build type parameter substitution map
        let mut subst: HashMap<String, Type> = HashMap::new();
        for (i, param_name) in struct_def.generics.iter().enumerate() {
            if let Some(arg) = type_args.get(i) {
                subst.insert(param_name.clone(), arg.clone());
            }
        }
        
        // Create opaque struct
        let opaque_struct = self.context.opaque_struct_type(&mangled_name);
        self.struct_types.insert(mangled_name.clone(), opaque_struct);
        
        // Build field types with substitution
        let mut field_llvm_types = Vec::new();
        for (field_name, field_ty) in &struct_def.fields {
            let substituted_ty = self.substitute_type(field_ty, &subst);
            let llvm_ty = self.get_llvm_type(&substituted_ty).map_err(|e| format!("Error compiling field {} of {}: {}", field_name, mangled_name, e))?;
            field_llvm_types.push(llvm_ty);
        }
        
        // Set struct body
        opaque_struct.set_body(&field_llvm_types, false);
        
        // Store the specialized struct def for later use
        let mut specialized_def = struct_def.clone();
        specialized_def.name = mangled_name.clone();
        specialized_def.generics = vec![]; // No longer generic
        // Substitute field types and convert generic types to UnifiedType
        specialized_def.fields = struct_def.fields.iter().map(|(name, ty)| {
            let substituted = self.substitute_type(ty, &subst);
            let unified = self.to_unified_type_if_generic(substituted);
            (name.clone(), unified)
        }).collect();
        
        self.struct_defs.insert(mangled_name.clone(), specialized_def);
        
        Ok(opaque_struct)
    }
    
    /// Check if a list of type arguments contains unresolved generic parameters.
    /// Generic parameters appear as Struct("K", []) or Struct("V", []) etc.
    #[allow(dead_code)]
    fn contains_unresolved_generics(args: &[Type]) -> bool {
        args.iter().any(|arg| Self::is_unresolved_generic(arg))
    }
    
    /// Check if a single type is (or contains) an unresolved generic parameter.
    #[allow(dead_code)]
    fn is_unresolved_generic(ty: &Type) -> bool {
        match ty {
            Type::Struct(name, inner_args) => {
                // A generic parameter is typically a short uppercase name with no args
                // e.g., Struct("K", []), Struct("V", []), Struct("T", [])
                if inner_args.is_empty() && name.len() <= 2 && name.chars().all(|c| c.is_ascii_uppercase()) {
                    return true;
                }
                // Check nested args recursively
                inner_args.iter().any(|a| Self::is_unresolved_generic(a))
            }
            Type::Enum(_, inner_args) => inner_args.iter().any(|a| Self::is_unresolved_generic(a)),
            Type::UnifiedType { type_args, .. } => type_args.iter().any(|a| Self::is_unresolved_generic(a)),
            Type::Undefined(_) => true,
            _ => false,
        }
    }

    /// Convert a Type to UnifiedType if it has generic arguments.
    /// Also corrects Struct→Enum misclassification using enum_defs.
    /// Non-generic types (e.g. I64, Struct("File", [])) are returned as-is.
    pub fn to_unified_type_if_generic(&self, ty: Type) -> Type {
        match &ty {
            Type::Struct(name, args) if !args.is_empty() => {
                let is_enum = self.enum_defs.contains_key(name)
                    || self.enum_defs.contains_key(mangle_base_name(name));
                // Avoid double-mangling: if name already contains brackets, use it directly
                let (base, mangled) = if mangle_has_args(name) {
                    (mangle_base_name(name).to_string(), name.clone())
                } else {
                    (name.clone(), self.mangle_type_name(name, args))
                };
                // Recursively convert inner type_args too
                let unified_args: Vec<Type> = args.iter()
                    .map(|a| self.to_unified_type_if_generic(a.clone()))
                    .collect();
                Type::UnifiedType {
                    base_name: base,
                    type_args: unified_args,
                    mangled_name: mangled,
                    is_enum,
                }
            }
            Type::Struct(name, args) if args.is_empty() => {
                // Correct Struct→Enum for non-generic types
                if self.enum_defs.contains_key(name) {
                    Type::Enum(name.clone(), vec![])
                } else {
                    ty
                }
            }
            Type::Enum(name, args) if !args.is_empty() => {
                // Avoid double-mangling
                let (base, mangled) = if mangle_has_args(name) {
                    (mangle_base_name(name).to_string(), name.clone())
                } else {
                    (name.clone(), self.mangle_type_name(name, args))
                };
                let unified_args: Vec<Type> = args.iter()
                    .map(|a| self.to_unified_type_if_generic(a.clone()))
                    .collect();
                Type::UnifiedType {
                    base_name: base,
                    type_args: unified_args,
                    mangled_name: mangled,
                    is_enum: true,
                }
            }
            _ => ty,
        }
    }
    
    /// Substitute type parameters in a type using the given substitution map.
    pub fn substitute_type(&self, ty: &Type, subst: &HashMap<String, Type>) -> Type {
        match ty {
            Type::Struct(name, args) => {
                // Check if this is a type parameter
                if args.is_empty() {
                    if let Some(replacement) = subst.get(name) {
                        return replacement.clone();
                    }
                }
                let new_args: Vec<Type> = args.iter().map(|a| self.substitute_type(a, subst)).collect();
                
                // Fix: Check if this is actually an Enum (exact match only)
                if self.enum_defs.contains_key(name) {
                    Type::Enum(name.clone(), new_args)
                } else {
                    Type::Struct(name.clone(), new_args)
                }
            }

            Type::Enum(name, args) => {
                // Check if this is a type parameter (e.g. Enum("K", []))
                if args.is_empty() {
                    if let Some(replacement) = subst.get(name) {
                        return replacement.clone();
                    }
                }
                let new_args: Vec<Type> = args.iter().map(|a| self.substitute_type(a, subst)).collect();
                Type::Enum(name.clone(), new_args)
            }

            Type::Tensor(inner, rank) => Type::Tensor(Box::new(self.substitute_type(inner, subst)), *rank),
            Type::Tuple(types) => Type::Tuple(types.iter().map(|t| self.substitute_type(t, subst)).collect()),
             Type::Path(segments, args) => {
                 if segments.len() == 1 {
                     let name = &segments[0];
                     // Check if this is a type parameter being substituted
                     if let Some(replacement) = subst.get(name) {
                         return replacement.clone();
                     }
                     // Otherwise, convert to Struct or Enum based on definitions
                     let new_args: Vec<Type> = args.iter().map(|a| self.substitute_type(a, subst)).collect();
                     if self.enum_defs.contains_key(name) {
                         return Type::Enum(name.clone(), new_args);
                     } else {
                         return Type::Struct(name.clone(), new_args);
                     }
                 }
                 let new_args: Vec<Type> = args.iter().map(|a| self.substitute_type(a, subst)).collect();
                 Type::Path(segments.clone(), new_args)
            },
            Type::Ptr(inner) => Type::Ptr(Box::new(self.substitute_type(inner, subst))),
            Type::Array(inner, size) => Type::Array(Box::new(self.substitute_type(inner, subst)), *size),
            Type::UnifiedType { base_name, type_args, mangled_name, is_enum } => {
                let new_args: Vec<Type> = type_args.iter().map(|a| self.substitute_type(a, subst)).collect();
                Type::UnifiedType {
                    base_name: base_name.clone(),
                    type_args: new_args,
                    mangled_name: mangled_name.clone(),
                    is_enum: *is_enum,
                }
            }
            _ => ty.clone(),
        }
    }

    /// Normalize Type::Path to Type::Struct or Type::Enum based on definitions.
    /// This is called after TypeSubstitutor.substitute_type which doesn't have access to enum_defs.
    pub fn normalize_type(&self, ty: &Type) -> Type {
        match ty {
            Type::Path(segments, args) => {
                if segments.len() == 1 {
                    let name = &segments[0];
                    let normalized_args: Vec<Type> = args.iter().map(|a| self.normalize_type(a)).collect();
                    // Check if it's an enum and generic counts match
                    if let Some(enum_def) = self.enum_defs.get(name) {
                        if enum_def.generics.len() == normalized_args.len() || enum_def.generics.is_empty() {
                            return Type::Enum(name.clone(), normalized_args);
                        }
                        // Generic count mismatch - return as-is (don't convert)
                        return ty.clone();
                    }
                    // Not an enum, treat as Struct
                    Type::Struct(name.clone(), normalized_args)
                } else {
                    let normalized_args: Vec<Type> = args.iter().map(|a| self.normalize_type(a)).collect();
                    Type::Path(segments.clone(), normalized_args)
                }
            }
            Type::Struct(name, args) => {
                let normalized_args: Vec<Type> = args.iter().map(|a| self.normalize_type(a)).collect();
                // Only convert Struct to Enum if it's actually an enum AND generic counts match
                if let Some(enum_def) = self.enum_defs.get(name) {
                    if enum_def.generics.len() == normalized_args.len() || enum_def.generics.is_empty() {
                        return Type::Enum(name.clone(), normalized_args);
                    }
                    // Generic count mismatch - keep as Struct
                }
                Type::Struct(name.clone(), normalized_args)
            }
            Type::Enum(name, args) => {
                let normalized_args: Vec<Type> = args.iter().map(|a| self.normalize_type(a)).collect();
                Type::Enum(name.clone(), normalized_args)
            }
            Type::UnifiedType { base_name: _, type_args, mangled_name, is_enum } => {
                // Flatten UnifiedType to Struct/Enum for legacy pattern matching compatibility.
                // Use mangled_name as the name and preserve type_args.
                let normalized_args: Vec<Type> = type_args.iter().map(|a| self.normalize_type(a)).collect();
                if *is_enum {
                    Type::Enum(mangled_name.clone(), normalized_args)
                } else {
                    Type::Struct(mangled_name.clone(), normalized_args)
                }
            }
            Type::Tensor(inner, rank) => Type::Tensor(Box::new(self.normalize_type(inner)), *rank),
            Type::Tuple(types) => Type::Tuple(types.iter().map(|t| self.normalize_type(t)).collect()),
            Type::Ptr(inner) => Type::Ptr(Box::new(self.normalize_type(inner))),
            Type::Array(inner, size) => Type::Array(Box::new(self.normalize_type(inner)), *size),
            _ => ty.clone(),
        }
    }

    // ========== AST Transformation for Enum Variant Constructors ==========
    
    fn transform_method_body_enum_inits(&self, stmts: &mut Vec<crate::compiler::ast::Stmt>) {
        for stmt in stmts.iter_mut() {
            self.transform_stmt_enum_inits(stmt);
        }
    }
    
    fn transform_stmt_enum_inits(&self, stmt: &mut crate::compiler::ast::Stmt) {
        use crate::compiler::ast::StmtKind;
        match &mut stmt.inner {
            StmtKind::Let { value, .. } => self.transform_expr_enum_inits(value),
            StmtKind::Expr(e) => self.transform_expr_enum_inits(e),
            StmtKind::Return(Some(e)) => self.transform_expr_enum_inits(e),
            StmtKind::While { cond, body } => {
                self.transform_expr_enum_inits(cond);
                self.transform_method_body_enum_inits(body);
            }
            StmtKind::For { iterator, body, .. } => {
                self.transform_expr_enum_inits(iterator);
                self.transform_method_body_enum_inits(body);
            }
            StmtKind::Loop { body } => {
                self.transform_method_body_enum_inits(body);
            }
            StmtKind::Assign { value, .. } => {
                self.transform_expr_enum_inits(value);
            }
            _ => {}
        }
    }
    
    fn transform_expr_enum_inits(&self, expr: &mut crate::compiler::ast::Expr) {
        use crate::compiler::ast::{ExprKind, EnumVariantInit};
        
        // First, recurse into children
        match &mut expr.inner {
            ExprKind::BinOp(l, _, r) => {
                self.transform_expr_enum_inits(l);
                self.transform_expr_enum_inits(r);
            }
            ExprKind::UnOp(_, e) => {
                self.transform_expr_enum_inits(e);
            }
            ExprKind::MethodCall(obj, _, args) => {
                self.transform_expr_enum_inits(obj);
                for arg in args.iter_mut() {
                    self.transform_expr_enum_inits(arg);
                }
            }
            ExprKind::FnCall(_, args) => {
                for arg in args.iter_mut() {
                    self.transform_expr_enum_inits(arg);
                }
            }
            ExprKind::IndexAccess(e, indices) => {
                self.transform_expr_enum_inits(e);
                for idx in indices.iter_mut() {
                    self.transform_expr_enum_inits(idx);
                }
            }
            ExprKind::FieldAccess(obj, _) => {
                self.transform_expr_enum_inits(obj);
            }
            ExprKind::Match { expr: subject, arms } => {
                self.transform_expr_enum_inits(subject);
                for (_, arm_expr) in arms.iter_mut() {
                    self.transform_expr_enum_inits(arm_expr);
                }
            }
            ExprKind::Block(stmts) => {
                self.transform_method_body_enum_inits(stmts);
            }
            ExprKind::IfExpr(cond, then_block, else_block) => {
                self.transform_expr_enum_inits(cond);
                self.transform_method_body_enum_inits(then_block);
                if let Some(else_stmts) = else_block {
                    self.transform_method_body_enum_inits(else_stmts);
                }
            }
            ExprKind::Tuple(exprs) => {
                for e in exprs.iter_mut() {
                    self.transform_expr_enum_inits(e);
                }
            }
            ExprKind::StructInit(_, fields) => {
                for (_, e) in fields.iter_mut() {
                    self.transform_expr_enum_inits(e);
                }
            }
            ExprKind::EnumInit { payload, .. } => {
                match payload {
                    EnumVariantInit::Tuple(exprs) => {
                        for e in exprs.iter_mut() {
                            self.transform_expr_enum_inits(e);
                        }
                    }
                    EnumVariantInit::Struct(fields) => {
                        for (_, e) in fields.iter_mut() {
                            self.transform_expr_enum_inits(e);
                        }
                    }
                    EnumVariantInit::Unit => {}
                }
            }
            ExprKind::StaticMethodCall(ty, method, args) => {
                // Transform args first
                for arg in args.iter_mut() {
                    self.transform_expr_enum_inits(arg);
                }
                
                // Check if this is an enum variant constructor
                let enum_name = match ty {
                    Type::Struct(name, _) | Type::Enum(name, _) => name.clone(),
                    Type::Path(segments, _) => segments.last().cloned().unwrap_or_default(),
                    _ => String::new(),
                };
                
                if let Some(enum_def) = self.enum_defs.get(&enum_name) {
                    if let Some(variant) = enum_def.variants.iter().find(|v| &v.name == method) {
                        use crate::compiler::ast::VariantKind;
                        
                        // Build payload
                        let payload = match &variant.kind {
                            VariantKind::Unit => EnumVariantInit::Unit,
                            VariantKind::Tuple(_) => EnumVariantInit::Tuple(std::mem::take(args)),
                            VariantKind::Struct(_) => EnumVariantInit::Unit, // TODO: struct variant
                            VariantKind::Array(_, _) => EnumVariantInit::Tuple(std::mem::take(args)),
                        };
                        
                        // Extract generics from type
                        let generics = match ty {
                            Type::Struct(_, g) | Type::Enum(_, g) | Type::Path(_, g) => g.clone(),
                            _ => vec![],
                        };
                        
                        // Replace with EnumInit
                        expr.inner = ExprKind::EnumInit {
                            enum_name,
                            variant_name: method.clone(),
                            generics,
                            payload,
                        };
                        return;
                    }
                }
            }
            _ => {}
        }
    }

    /// Generate all methods for a specialized type.
    /// This is called at the end of compilation to ensure all methods are generated.
    pub fn generate_methods_for_specialized_type(
        &mut self,
        base_name: &str,
        type_args: &[Type],
    ) -> Result<(), String> {
        // Get impl blocks for this base type
        let impls = match self.generic_impls.get(base_name) {
            Some(impls) => impls.clone(),
            None => return Ok(()), // No impl blocks for this type
        };
        
        // Generate each method
        for imp in &impls {
            for method in &imp.methods {
                // Skip if already generated
                let mangled_name = crate::compiler::codegen::builtin_types::resolver::resolve_static_method_name(
                    base_name, &method.name, type_args
                );
                if self.module.get_function(&mangled_name).is_some() {
                    continue;
                }
                
                // Generate this method
                match self.monomorphize_method(base_name, &method.name, type_args) {
                    Ok(_) => {}
                    Err(e) => {
                        // Log but continue - some methods may not be needed
                        log::debug!("Could not generate method {}.{}: {}", 
                            base_name, method.name, e);
                    }
                }
            }
        }
        
        Ok(())
    }

}