@@ -425,6 +425,10 @@ pub trait TypeReflection<'gcc, 'tcx> {
425
425
fn is_u128 ( & self , cx : & CodegenCx < ' gcc , ' tcx > ) -> bool ;
426
426
427
427
fn is_vector ( & self ) -> bool ;
428
+ /// Checks if 2 types are "known to be equal". Returns Some(true) if types are equal(e.g. bool and bool),
429
+ /// Some(false) if they can't possibly be equal(e.g. a struct and a float), and None if there is no way
430
+ /// to check for their equality(struct and struct)
431
+ fn known_eq ( & self , rhs : & Self , cx : & CodegenCx < ' gcc , ' tcx > ) -> Option < bool > ;
428
432
}
429
433
430
434
impl < ' gcc , ' tcx > TypeReflection < ' gcc , ' tcx > for Type < ' gcc > {
@@ -524,4 +528,58 @@ impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
524
528
525
529
false
526
530
}
531
+ fn known_eq ( & self , other : & Self , cx : & CodegenCx < ' gcc , ' tcx > ) -> Option < bool > {
532
+ // "Happy" path: types represented using the same pointer
533
+ if self == other {
534
+ return Some ( true ) ;
535
+ }
536
+ if self . is_bool ( ) {
537
+ return Some ( other. is_bool ( ) ) ;
538
+ } else if self . is_integral ( ) {
539
+ if !other. is_integral ( ) {
540
+ return Some ( false ) ;
541
+ }
542
+ return Some (
543
+ ( self . get_size ( ) == other. get_size ( ) )
544
+ && ( self . is_signed ( cx) == other. is_signed ( cx) ) ,
545
+ ) ;
546
+ } else if self . is_vector ( ) {
547
+ if !other. is_vector ( ) {
548
+ return Some ( false ) ;
549
+ }
550
+ return None ;
551
+ } else if self . is_struct ( ) . is_some ( ) {
552
+ if other. is_struct ( ) . is_none ( ) {
553
+ return Some ( false ) ;
554
+ }
555
+ return None ;
556
+ } else if let Some ( s_ptr) = self . get_pointee ( ) {
557
+ let Some ( other_ptr) = other. get_pointee ( ) else {
558
+ return Some ( false ) ;
559
+ } ;
560
+ return s_ptr. known_eq ( & other_ptr, cx) ;
561
+ }
562
+ #[ cfg( feature = "master" ) ]
563
+ if self . is_floating_point ( ) {
564
+ if !other. is_floating_point ( ) {
565
+ return Some ( false ) ;
566
+ }
567
+ return Some ( self . get_size ( ) == other. get_size ( ) ) ;
568
+ }
569
+ #[ cfg( not( feature = "master" ) ) ]
570
+ {
571
+ fn is_floating_point < ' gcc > ( ty : & Type < ' gcc > , cx : & CodegenCx < ' gcc , ' _ > ) -> bool {
572
+ ty. is_compatible_with ( cx. context . new_type :: < f32 > ( ) )
573
+ || ty. is_compatible_with ( cx. context . new_type :: < f64 > ( ) )
574
+ }
575
+ if is_floating_point ( self , cx) {
576
+ if !is_floating_point ( self , cx) {
577
+ return Some ( false ) ;
578
+ }
579
+ return Some ( self . get_size ( ) == other. get_size ( ) ) ;
580
+ }
581
+ }
582
+ // Unknown type...
583
+ None
584
+ }
527
585
}
0 commit comments