Understand macro MP_DEFINE_CONST_OBJ_TYPE in-depth #17182
Replies: 3 comments 3 replies
-
I do not know for sure, and the below is an explanation by GH Copilot an AI grounded on this repo. So to be sure it also needs human validation. Let me know if it is incorrect, then I can delete it The macro Initial MacroMP_DEFINE_CONST_OBJ_TYPE(
mp_type_type,
MP_QSTR_type,
MP_TYPE_FLAG_NONE,
make_new, type_make_new,
print, type_print,
call, type_call,
attr, type_attr
); Macro DefinitionThe macro #define MP_DEFINE_CONST_OBJ_TYPE(...) \
MP_DEFINE_CONST_OBJ_TYPE_EXPAND( \
MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__)) Key Components
PurposeThis macro system achieves two goals:
Simplified ViewGiven the complexity, the macro essentially:
|
Beta Was this translation helpful? Give feedback.
-
@Josverl , it's great explanation. But I'm still stuck here, It's like the function call with parameter And macro, At the end, is it called with the macro as you said, is it right?
|
Beta Was this translation helpful? Give feedback.
-
According to my understanding, it will define like this. But I'm still confused about mp_type_type.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand some inner of MicroPython. One of it is
The macro definition MP_DEFINE_CONST_OBJ_TYPE defined as:
#define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__))
With macro:
#define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x
, it will be:MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__)
I could understand
__VA_ARGS__
but not _INV, 12, _INV, 11,...and this macroMP_DEFINE_CONST_OBJ_TYPE_NARGS(_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, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N(mp_obj_type_t, __VA_ARGS__)
This is so complex to my understanding of C, so could someone help me to explain it?
Thank you so much.
Beta Was this translation helpful? Give feedback.
All reactions