@@ -26,7 +26,7 @@ use crate::{
2626 protocol:: { PyIterReturn , PyNumberMethods } ,
2727 types:: {
2828 AsNumber , Callable , Constructor , GetAttr , Initializer , PyTypeFlags , PyTypeSlots ,
29- Representable , SetAttr , TypeDataRef , TypeDataRefMut , TypeDataSlot ,
29+ Representable , SLOT_DEFS , SetAttr , TypeDataRef , TypeDataRefMut , TypeDataSlot ,
3030 } ,
3131} ;
3232use indexmap:: { IndexMap , map:: Entry } ;
@@ -464,173 +464,11 @@ impl PyType {
464464
465465 /// Inherit slots from base type. inherit_slots
466466 pub ( crate ) fn inherit_slots ( & self , base : & Self ) {
467- macro_rules! copyslot {
468- ( $slot: ident) => {
469- if self . slots. $slot. load( ) . is_none( ) {
470- if let Some ( base_val) = base. slots. $slot. load( ) {
471- self . slots. $slot. store( Some ( base_val) ) ;
472- }
473- }
474- } ;
475- }
476-
477- // Copy init slot only if base actually defines it (not just inherited)
478- // This is needed for multiple inheritance where a later base might
479- // have a more specific init slot
480- macro_rules! copyslot_defined {
481- ( $slot: ident) => {
482- if self . slots. $slot. load( ) . is_none( ) {
483- if let Some ( base_val) = base. slots. $slot. load( ) {
484- // SLOTDEFINED: base->SLOT && (basebase == NULL || base->SLOT != basebase->SLOT)
485- let basebase = base. base. as_ref( ) ;
486- let slot_defined = match basebase {
487- None => true ,
488- Some ( bb) => bb. slots. $slot. load( ) . map( |v| v as usize )
489- != Some ( base_val as usize ) ,
490- } ;
491- if slot_defined {
492- self . slots. $slot. store( Some ( base_val) ) ;
493- }
494- }
495- }
496- } ;
497- }
498-
499- // Core slots
500- copyslot ! ( hash) ;
501- copyslot ! ( call) ;
502- copyslot ! ( str ) ;
503- copyslot ! ( repr) ;
504- copyslot ! ( getattro) ;
505- copyslot ! ( setattro) ;
506- copyslot ! ( richcompare) ;
507- copyslot ! ( iter) ;
508- copyslot ! ( iternext) ;
509- copyslot ! ( descr_get) ;
510- copyslot ! ( descr_set) ;
511- // init uses SLOTDEFINED check for multiple inheritance support
512- copyslot_defined ! ( init) ;
513- copyslot ! ( del) ;
514- // new is handled by set_new()
515- // as_buffer is inherited at type creation time (not AtomicCell)
516-
517- // Sub-slots (number, sequence, mapping)
518- self . inherit_number_slots ( base) ;
519- self . inherit_sequence_slots ( base) ;
520- self . inherit_mapping_slots ( base) ;
521- }
522-
523- /// Inherit number sub-slots from base type
524- fn inherit_number_slots ( & self , base : & Self ) {
525- macro_rules! copy_num_slot {
526- ( $slot: ident) => {
527- if self . slots. as_number. $slot. load( ) . is_none( ) {
528- if let Some ( base_val) = base. slots. as_number. $slot. load( ) {
529- self . slots. as_number. $slot. store( Some ( base_val) ) ;
530- }
531- }
532- } ;
533- }
534-
535- // Binary operations
536- copy_num_slot ! ( add) ;
537- copy_num_slot ! ( right_add) ;
538- copy_num_slot ! ( inplace_add) ;
539- copy_num_slot ! ( subtract) ;
540- copy_num_slot ! ( right_subtract) ;
541- copy_num_slot ! ( inplace_subtract) ;
542- copy_num_slot ! ( multiply) ;
543- copy_num_slot ! ( right_multiply) ;
544- copy_num_slot ! ( inplace_multiply) ;
545- copy_num_slot ! ( remainder) ;
546- copy_num_slot ! ( right_remainder) ;
547- copy_num_slot ! ( inplace_remainder) ;
548- copy_num_slot ! ( divmod) ;
549- copy_num_slot ! ( right_divmod) ;
550- copy_num_slot ! ( power) ;
551- copy_num_slot ! ( right_power) ;
552- copy_num_slot ! ( inplace_power) ;
553-
554- // Bitwise operations
555- copy_num_slot ! ( lshift) ;
556- copy_num_slot ! ( right_lshift) ;
557- copy_num_slot ! ( inplace_lshift) ;
558- copy_num_slot ! ( rshift) ;
559- copy_num_slot ! ( right_rshift) ;
560- copy_num_slot ! ( inplace_rshift) ;
561- copy_num_slot ! ( and) ;
562- copy_num_slot ! ( right_and) ;
563- copy_num_slot ! ( inplace_and) ;
564- copy_num_slot ! ( xor) ;
565- copy_num_slot ! ( right_xor) ;
566- copy_num_slot ! ( inplace_xor) ;
567- copy_num_slot ! ( or) ;
568- copy_num_slot ! ( right_or) ;
569- copy_num_slot ! ( inplace_or) ;
570-
571- // Division operations
572- copy_num_slot ! ( floor_divide) ;
573- copy_num_slot ! ( right_floor_divide) ;
574- copy_num_slot ! ( inplace_floor_divide) ;
575- copy_num_slot ! ( true_divide) ;
576- copy_num_slot ! ( right_true_divide) ;
577- copy_num_slot ! ( inplace_true_divide) ;
578-
579- // Matrix multiplication
580- copy_num_slot ! ( matrix_multiply) ;
581- copy_num_slot ! ( right_matrix_multiply) ;
582- copy_num_slot ! ( inplace_matrix_multiply) ;
583-
584- // Unary operations
585- copy_num_slot ! ( negative) ;
586- copy_num_slot ! ( positive) ;
587- copy_num_slot ! ( absolute) ;
588- copy_num_slot ! ( boolean) ;
589- copy_num_slot ! ( invert) ;
590-
591- // Conversion
592- copy_num_slot ! ( int) ;
593- copy_num_slot ! ( float) ;
594- copy_num_slot ! ( index) ;
595- }
596-
597- /// Inherit sequence sub-slots from base type
598- fn inherit_sequence_slots ( & self , base : & Self ) {
599- macro_rules! copy_seq_slot {
600- ( $slot: ident) => {
601- if self . slots. as_sequence. $slot. load( ) . is_none( ) {
602- if let Some ( base_val) = base. slots. as_sequence. $slot. load( ) {
603- self . slots. as_sequence. $slot. store( Some ( base_val) ) ;
604- }
605- }
606- } ;
467+ // Use SLOT_DEFS to iterate all slots
468+ // Note: as_buffer is handled in inherit_static_slots (not AtomicCell)
469+ for def in SLOT_DEFS {
470+ def. accessor . copyslot_if_none ( self , base) ;
607471 }
608-
609- copy_seq_slot ! ( length) ;
610- copy_seq_slot ! ( concat) ;
611- copy_seq_slot ! ( repeat) ;
612- copy_seq_slot ! ( item) ;
613- copy_seq_slot ! ( ass_item) ;
614- copy_seq_slot ! ( contains) ;
615- copy_seq_slot ! ( inplace_concat) ;
616- copy_seq_slot ! ( inplace_repeat) ;
617- }
618-
619- /// Inherit mapping sub-slots from base type
620- fn inherit_mapping_slots ( & self , base : & Self ) {
621- macro_rules! copy_map_slot {
622- ( $slot: ident) => {
623- if self . slots. as_mapping. $slot. load( ) . is_none( ) {
624- if let Some ( base_val) = base. slots. as_mapping. $slot. load( ) {
625- self . slots. as_mapping. $slot. store( Some ( base_val) ) ;
626- }
627- }
628- } ;
629- }
630-
631- copy_map_slot ! ( length) ;
632- copy_map_slot ! ( subscript) ;
633- copy_map_slot ! ( ass_subscript) ;
634472 }
635473
636474 // This is used for class initialization where the vm is not yet available.
0 commit comments