@@ -578,17 +578,22 @@ struct _mp_obj_type_t {
578
578
// Corresponds to __new__ and __init__ special methods, to make an instance of the type.
579
579
mp_make_new_fun_t make_new ;
580
580
581
- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
581
+ // Slots: For the rest of the fields, the slot index points to the
582
+ // relevant function in the variable-length "slots" field. Ideally these
583
+ // would be only 4 bits, but the extra overhead of accessing them adds
584
+ // more code, and we also need to be able to take the address of them for
585
+ // mp_obj_class_lookup.
586
+
582
587
// Corresponds to __repr__ and __str__ special methods.
583
- mp_print_fun_t print ;
588
+ uint8_t slot_index_print ;
584
589
585
590
// Corresponds to __call__ special method, ie T(...).
586
- mp_call_fun_t call ;
591
+ uint8_t slot_index_call ;
587
592
588
593
// Implements unary and binary operations.
589
594
// Can return MP_OBJ_NULL if the operation is not supported.
590
- mp_unary_op_fun_t unary_op ;
591
- mp_binary_op_fun_t binary_op ;
595
+ uint8_t slot_index_unary_op ;
596
+ uint8_t slot_index_binary_op ;
592
597
593
598
// Implements load, store and delete attribute.
594
599
//
@@ -602,70 +607,46 @@ struct _mp_obj_type_t {
602
607
// dest[0,1] = {MP_OBJ_SENTINEL, object} means store
603
608
// return: for fail, do nothing
604
609
// for success set dest[0] = MP_OBJ_NULL
605
- mp_attr_fun_t attr ;
610
+ uint8_t slot_index_attr ;
606
611
607
612
// Implements load, store and delete subscripting:
608
613
// - value = MP_OBJ_SENTINEL means load
609
614
// - value = MP_OBJ_NULL means delete
610
615
// - all other values mean store the value
611
616
// Can return MP_OBJ_NULL if operation not supported.
612
- mp_subscr_fun_t subscr ;
617
+ uint8_t slot_index_subscr ;
613
618
614
619
// Corresponds to __iter__ special method.
615
620
// Can use the given mp_obj_iter_buf_t to store iterator object,
616
621
// otherwise can return a pointer to an object on the heap.
617
- mp_getiter_fun_t getiter ;
622
+ uint8_t slot_index_getiter ;
618
623
619
624
// Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
620
625
// as an optimisation instead of raising StopIteration() with no args.
621
- mp_fun_1_t iternext ;
626
+ uint8_t slot_index_iternext ;
622
627
623
628
// Implements the buffer protocol if supported by this type.
624
- mp_buffer_fun_t buffer ;
629
+ uint8_t slot_index_buffer ;
625
630
626
631
// One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
627
- const void * protocol ;
632
+ uint8_t slot_index_protocol ;
628
633
629
634
// A pointer to the parents of this type:
630
635
// - 0 parents: pointer is NULL (object is implicitly the single parent)
631
636
// - 1 parent: a pointer to the type of that parent
632
637
// - 2 or more parents: pointer to a tuple object containing the parent types
633
- const void * parent ;
638
+ uint8_t slot_index_parent ;
634
639
635
640
// A dict mapping qstrs to objects local methods/constants/etc.
636
- struct _mp_obj_dict_t * locals_dict ;
637
-
638
- #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
639
-
640
- // Ideally these would be only 4 bits, but the extra overhead of
641
- // accessing them adds more code, and we also need to be able to
642
- // take the address of them for mp_obj_class_lookup.
643
- uint8_t slot_index_print ;
644
- uint8_t slot_index_call ;
645
- uint8_t slot_index_unary_op ;
646
- uint8_t slot_index_binary_op ;
647
- uint8_t slot_index_attr ;
648
- uint8_t slot_index_subscr ;
649
- uint8_t slot_index_getiter ;
650
- uint8_t slot_index_iternext ;
651
- uint8_t slot_index_buffer ;
652
- uint8_t slot_index_protocol ;
653
- uint8_t slot_index_parent ;
654
641
uint8_t slot_index_locals_dict ;
655
642
656
643
const void * slots [];
657
-
658
- #endif
659
644
};
660
645
661
646
// Non-variable sized versions of mp_obj_type_t to be used as a member
662
647
// in other structs or for dynamic allocation. The fields are exactly
663
648
// as in mp_obj_type_t, but with a fixed size for the flexible array
664
649
// members.
665
- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
666
- typedef mp_obj_type_t mp_obj_empty_type_t ;
667
- typedef mp_obj_type_t mp_obj_full_type_t ;
668
- #else
669
650
typedef struct _mp_obj_empty_type_t {
670
651
mp_obj_base_t base ;
671
652
uint16_t flags ;
@@ -710,38 +691,9 @@ typedef struct _mp_obj_full_type_t {
710
691
// Explicitly add 12 slots.
711
692
const void * slots [12 ];
712
693
} mp_obj_full_type_t ;
713
- #endif
714
694
715
695
#define MP_TYPE_NULL_MAKE_NEW (NULL)
716
696
717
- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
718
-
719
- #define _MP_DEFINE_CONST_OBJ_TYPE_0 (_struct_type , _typename , _name , _flags , _make_new ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new }
720
- #define _MP_DEFINE_CONST_OBJ_TYPE_1 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1 }
721
- #define _MP_DEFINE_CONST_OBJ_TYPE_2 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2 }
722
- #define _MP_DEFINE_CONST_OBJ_TYPE_3 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3 }
723
- #define _MP_DEFINE_CONST_OBJ_TYPE_4 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4 }
724
- #define _MP_DEFINE_CONST_OBJ_TYPE_5 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5 }
725
- #define _MP_DEFINE_CONST_OBJ_TYPE_6 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6 }
726
- #define _MP_DEFINE_CONST_OBJ_TYPE_7 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7 }
727
- #define _MP_DEFINE_CONST_OBJ_TYPE_8 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 , f8 , v8 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8 }
728
- #define _MP_DEFINE_CONST_OBJ_TYPE_9 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 , f8 , v8 , f9 , v9 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9 }
729
- #define _MP_DEFINE_CONST_OBJ_TYPE_10 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 , f8 , v8 , f9 , v9 , f10 , v10 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10 }
730
- #define _MP_DEFINE_CONST_OBJ_TYPE_11 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 , f8 , v8 , f9 , v9 , f10 , v10 , f11 , v11 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11 }
731
- #define _MP_DEFINE_CONST_OBJ_TYPE_12 (_struct_type , _typename , _name , _flags , _make_new , f1 , v1 , f2 , v2 , f3 , v3 , f4 , v4 , f5 , v5 , f6 , v6 , f7 , v7 , f8 , v8 , f9 , v9 , f10 , v10 , f11 , v11 , f12 , v12 ) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11, .f12 = v12 }
732
-
733
- // Always safe, checks if the type can and does have this slot.
734
- #define MP_OBJ_TYPE_HAS_SLOT (t , f ) ((t)->f)
735
- // Requires you know that this type can have this slot.
736
- #define MP_OBJ_TYPE_GET_SLOT (t , f ) ((t)->f)
737
- // Always safe, returns NULL if the type cannot have this slot.
738
- #define MP_OBJ_TYPE_GET_SLOT_OR_NULL (t , f ) ((t)->f)
739
- #define MP_OBJ_TYPE_SET_SLOT (t , f , v , n ) ((t)->f = v)
740
- #define MP_OBJ_TYPE_OFFSETOF_SLOT (f ) (offsetof(mp_obj_type_t, f))
741
- #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET (t , offset ) (*(void **)((char *)(t) + (offset)) != NULL)
742
-
743
- #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
744
-
745
697
#define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
746
698
#define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
747
699
#define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
@@ -779,8 +731,6 @@ typedef struct _mp_obj_full_type_t {
779
731
// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer.
780
732
#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET (t , offset ) (*(uint8_t *)((char *)(t) + (offset)) != 0 || (offset == offsetof(mp_obj_type_t, make_new) && t->make_new))
781
733
782
- #endif
783
-
784
734
// Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
785
735
#define _MP_DEFINE_CONST_OBJ_TYPE_EXPAND (x ) x
786
736
0 commit comments