afaik, you need to bake this information in its binary format. just create a function that returns sizeof and alignof in the structure, supports the types you should support, then call this function (or class method) for information.
The program below shows that many of the primitives are just one character. Thus, the main part of the implementation of the function can be a switch.
static void test(SEL sel) { Method method = class_getInstanceMethod([NSString class], sel); const char* const type = method_copyReturnType(method); printf("%s : %s\n", NSStringFromSelector(sel).UTF8String, type); free((void*)type); } int main(int argc, char *argv[]) { @autoreleasepool { test(@selector(init)); test(@selector(superclass)); test(@selector(isEqual:)); test(@selector(length)); return 0; } }
and you can use this as a starting point:
typedef struct t_pair_alignof_sizeof { size_t align; size_t size; } t_pair_alignof_sizeof; static t_pair_alignof_sizeof MakeAlignOfSizeOf(size_t align, size_t size) { t_pair_alignof_sizeof ret = {align, size}; return ret; } static t_pair_alignof_sizeof test2(SEL sel) { Method method = class_getInstanceMethod([NSString class], sel); const char* const type = method_copyReturnType(method); const size_t length = strlen(type); if (1U == length) { switch (type[0]) { case '@' : return MakeAlignOfSizeOf(__alignof__(id), sizeof(id)); case '#' : return MakeAlignOfSizeOf(__alignof__(Class), sizeof(Class)); case 'c' : return MakeAlignOfSizeOf(__alignof__(signed char), sizeof(signed char)); ...
source share