-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_ocoa_generator.h
2247 lines (1872 loc) · 80.4 KB
/
c_ocoa_generator.h
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef C_OCOA_GENERATOR_HEADER
#define C_OCOA_GENERATOR_HEADER
#define C_OCOA_GENERATOR_MAJOR_VERSION 1
#define C_OCOA_GENERATOR_MINOR_VERSION 0
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#define assert_runtime(x) if(!(x)) raise(SIGTRAP)
#define code_path_invalid() assert_runtime(0)
#define hook_breakpoint() asm("nop")
#define restrict_modifier __restrict__
#include <objc/runtime.h>
typedef enum
{
ConvertResult_Success = 0,
ConvertResult_OutOfMemory,
ConvertResult_InvalidFunctionName,
ConvertResult_UnknownReturnType,
ConvertResult_UnknownArgumentType,
ConvertResult_AlreadyKnown,
} c_ocoa_convert_result;
typedef enum
{
MethodType_Class = 0,
MethodType_Instance
} c_ocoa_method_type;
typedef uint8_t boolean8_t;
typedef struct
{
int32_t length;
char* pName;
char* pNameLower;
char* pNameUpper;
} c_ocoa_objc_class_name;
typedef struct
{
const char* pOriginalType;
const char* pResolvedType;
int32_t resolvedTypeLength;
int32_t originalTypeLength;
uint32_t typeSizeInBits;
boolean8_t isReference;
boolean8_t isBaseType;
boolean8_t isFloatingType;
boolean8_t isConst;
} c_ocoa_objc_type_resolve_result;
typedef struct
{
const char* pResolvedReturnType;
const char* pResolvedArgumentTypes[32];
const c_ocoa_objc_class_name* pClassName;
char* pResolvedFunctionName;
char* pOriginalFunctionName;
char* pOriginalReturnType;
char* pOriginalArgumentTypes;
c_ocoa_method_type methodType;
uint32_t returnValueSizeInBits;
uint8_t argumentCount;
boolean8_t isVoidFunction : 1;
boolean8_t isAllocFunction : 1;
boolean8_t hasStructReturnValue : 1;
boolean8_t hasFloatReturnValue : 1;
} c_ocoa_objc_function_resolve_result;
typedef FILE*(*fopen_fn)(const char* pPath, const char* pMode);
typedef int(*fclose_fn)(FILE* pStream);
typedef int(*fflush_fn)(FILE* pStream);
typedef struct
{
boolean8_t exportTypes;
const char* pOutputPath;
const char* pPrefix;
const char* pClassNameFilter;
fopen_fn fopen;
fclose_fn fclose;
fflush_fn fflush;
} c_ocoa_code_generator_parameter;
typedef struct
{
void* pNext;
char hashValue[128];
char* pHashValue; //FK: dynamically allocated in case the hash value is > 128
c_ocoa_objc_type_resolve_result resolveResult;
uint32_t index;
boolean8_t declarationWasWritten;
} c_ocoa_objc_type_dictionary_entry;
typedef struct
{
void* pNext;
c_ocoa_objc_type_dictionary_entry* pEntries;
uint32_t entryCount;
uint32_t entryCapacity;
} c_ocoa_objc_type_dictionary_entry_block;
typedef struct
{
c_ocoa_objc_type_dictionary_entry_block* pFreeEntries;
c_ocoa_objc_type_dictionary_entry** ppTypeEntries;
uint32_t typeEntryCapacity;
uint32_t typeEntryCount;
} c_ocoa_objc_type_dictionary;
typedef struct
{
const char* pTestFilePath;
} c_ocoa_command_line_parse_result;
typedef struct
{
FILE* pHeaderFileHandle;
FILE* pSourceFileHandle;
const char* pHeaderFileName;
const char* pSourceFileName;
} c_ocoa_objc_conversion_arguments;
typedef struct
{
char* pReturnType;
char* pFunctionName;
char* pArguments;
int32_t returnTypeLength;
int32_t functionNameLength;
int32_t argumentLength;
c_ocoa_method_type c_ocoa_method_type;
} c_ocoa_parse_result;
typedef struct
{
char* pBufferStart;
uint32_t bufferCapacity;
uint32_t bufferSize;
} c_ocoa_string_allocator;
typedef struct
{
int32_t functionNameLength;
const char* pFunctionName;
} c_ocoa_objc_function_collection_entry;
typedef struct
{
uint32_t capacity;
uint32_t size;
c_ocoa_objc_function_collection_entry* pEntries;
} c_ocoa_objc_function_collection;
typedef struct
{
Method* ppClassMethods;
Method* ppInstanceMethods;
uint32_t classMethodCount;
uint32_t instanceMethodCount;
} c_ocoa_class_method_collection;
typedef struct
{
c_ocoa_objc_function_collection* pFunctionCollection;
c_ocoa_objc_type_dictionary* pTypeDict;
const c_ocoa_objc_class_name* pClassName;
c_ocoa_string_allocator* pStringAllocator;
FILE* pHeaderFileHandle;
FILE* pSourceFileHandle;
} c_ocoa_source_code_generator_input;
typedef struct
{
c_ocoa_objc_type_dictionary typeDict;
c_ocoa_objc_function_collection functionCollection;
c_ocoa_string_allocator stringAllocator;
} c_ocoa_code_gen_context;
//FK: Some helpful macros
#define printf_stderr(x, ...) fprintf( stderr, x, ## __VA_ARGS__ )
#define array_count(x) (sizeof(x)/sizeof(x[0]))
#define get_max(a,b) (a)>(b)?(a):(b)
#define unused_argument(x) (void)x
void c_ocoa_create_source_code_for_objc_methods( c_ocoa_method_type c_ocoa_method_type, Method* ppMethods, const uint32_t methodCount, c_ocoa_source_code_generator_input* pCodeGenInput );
c_ocoa_convert_result objc_parse_result_convert_to_function_definition( c_ocoa_string_allocator* pStringAllocator, c_ocoa_objc_function_resolve_result* pOutFunctionResolveResult, c_ocoa_objc_function_collection* pFunctionCollection, c_ocoa_objc_type_dictionary* pDict, c_ocoa_parse_result* pParseResult, const c_ocoa_objc_class_name* pClassName );
void file_write_c_function_implementation( FILE* pSourceFileHandle, const c_ocoa_objc_function_resolve_result* pFunctionResolveResult, const c_ocoa_objc_class_name* pClassName );
void cfunction_write_declaration( FILE* pResultFileHandle, const c_ocoa_objc_function_resolve_result* pFunctionDefinition );
boolean8_t objc_create_class_name( c_ocoa_objc_class_name* pOutClassName, const char* pClassNameStart, const int32_t classNameLength );
void objc_type_dict_resolve_struct_types( c_ocoa_objc_type_dictionary* pTypeDict, FILE* pTypesFileHandle, boolean8_t exportTypes );
void string_allocator_reset( c_ocoa_string_allocator* pStringAllocator )
{
pStringAllocator->bufferSize = 0u;
}
char* string_allocator_get_current_base( c_ocoa_string_allocator* pStringAllocator )
{
return pStringAllocator->pBufferStart + pStringAllocator->bufferSize;
}
void string_allocator_decrement_capacity( c_ocoa_string_allocator* pStringAllocator, uint32_t sizeInBytes )
{
assert_runtime( pStringAllocator->bufferSize + sizeInBytes < pStringAllocator->bufferCapacity );
pStringAllocator->bufferSize += sizeInBytes;
}
uint32_t string_allocator_get_remaining_capacity( c_ocoa_string_allocator* pStringAllocator )
{
return pStringAllocator->bufferCapacity - pStringAllocator->bufferSize;
}
void memory_copy_non_overlapping( void* pDestination, const void* pSource, const size_t sizeInBytes )
{
memcpy(pDestination, pSource, sizeInBytes);
}
static inline int32_t cast_size_to_int32( size_t val )
{
assert_runtime( val < INT32_MAX );
return (int32_t)val;
}
//FK: Use custom string.h alternatives since we're not concerned with locale
static inline boolean8_t char_is_white_space( const char character )
{
return character == ' ' ||
character == '\n' ||
character == '\t' ||
character == '\v' ||
character == '\f' ||
character == '\r';
}
static inline boolean8_t char_is_alpha( const char character )
{
return ( character >= 'A' && character <= 'Z' ) || ( character >= 'a' && character <= 'z' );
}
static inline char convertCharacterToLower( const char character )
{
if( !char_is_alpha( character ) )
{
return character;
}
return character <= 'Z' ? character + 32 : character;
}
static inline char char_convert_to_upper( const char character )
{
if( !char_is_alpha( character ) )
{
return character;
}
return character >= 'a' ? character - 32 : character;
}
static inline boolean8_t string_is_equal( const char* pStringA, const char* pStringB, const int32_t stringLength )
{
for( int32_t charIndex = 0u; charIndex < stringLength; ++charIndex )
{
if( pStringA[ charIndex ] != pStringB[ charIndex ] )
{
return 0u;
}
}
return 1u;
}
static inline int32_t string_get_length_excl_null_terminator( const char* pString )
{
const char* pStringStart = pString;
while( *pString )
{
if( *pString == 0 )
{
break;
}
++pString;
}
return cast_size_to_int32( pString - pStringStart );
}
static inline int32_t string_get_length_incl_null_terminator( const char* pString )
{
const char* pStringStart = pString;
while( *pString++ );
return cast_size_to_int32( pString - pStringStart );
}
static inline char* string_copy_and_add_null_terminator( char* pDestination, const char* pSource, const int32_t stringLength )
{
for( int32_t charIndex = 0; charIndex < stringLength; ++charIndex )
{
pDestination[ charIndex ] = pSource[ charIndex ];
}
pDestination[ stringLength ] = 0;
return pDestination;
}
static inline char* string_convert_to_lower_inplace( char* pString, const int32_t stringLength )
{
for( int32_t charIndex = 0u; charIndex < stringLength; ++charIndex )
{
pString[ charIndex ] = convertCharacterToLower( pString[ charIndex ] );
}
return pString;
}
static inline char* string_convert_to_upper_inplace( char* pString, const int32_t stringLength )
{
for( int32_t charIndex = 0u; charIndex < stringLength; ++charIndex )
{
pString[ charIndex ] = char_convert_to_upper( pString[ charIndex ] );
}
return pString;
}
static inline char* string_allocate_copy( const char* pString, const int32_t stringLength )
{
char* pStringCopyMemory = (char*)malloc( stringLength + 1 );
if( pStringCopyMemory == NULL )
{
//FK: TODO: Handle out of memory
return NULL;
}
return string_copy_and_add_null_terminator( pStringCopyMemory, pString, stringLength );
}
static inline char* string_allocate_copy_lower( const char* pString, const int32_t stringLength )
{
char* pStringCopyMemory = (char*)malloc( stringLength + 1 );
if( pStringCopyMemory == NULL )
{
//FK: TODO: Handle out of memory
return NULL;
}
string_copy_and_add_null_terminator( pStringCopyMemory, pString, stringLength );
return string_convert_to_lower_inplace( pStringCopyMemory, stringLength );
}
static inline char* string_allocator_copy_upper( const char* pString, const int32_t stringLength )
{
char* pStringCopyMemory = (char*)malloc( stringLength + 1 );
if( pStringCopyMemory == NULL )
{
//FK: TODO: Handle out of memory
return NULL;
}
string_copy_and_add_null_terminator( pStringCopyMemory, pString, stringLength );
return string_convert_to_upper_inplace( pStringCopyMemory, stringLength );
}
static inline char* string_allocator_allocate( c_ocoa_string_allocator* pStringAllocator, const uint32_t length )
{
char* pReturnString = string_allocator_get_current_base( pStringAllocator );
string_allocator_decrement_capacity( pStringAllocator, length );
return pReturnString;
}
static inline char* string_allocate_copy_with_allocator( c_ocoa_string_allocator* pAllocator, const char* pString, const int32_t stringLength )
{
char* pStringCopyMemory = string_allocator_allocate(pAllocator, stringLength + 1);
return string_copy_and_add_null_terminator( pStringCopyMemory, pString, stringLength );
}
static inline const char* string_find_last_position( const char* pString, char character )
{
const char* pLastOccurance = NULL;
while( *pString )
{
if( *pString == character )
{
pLastOccurance = pString;
}
++pString;
}
return pLastOccurance;
}
static inline const char* string_find_next_char_position( const char* pString, char character )
{
while( *pString )
{
if( *pString == character )
{
return pString;
}
++pString;
}
return NULL;
}
c_ocoa_command_line_parse_result command_line_arguments_parse( const int argc, const char** argv )
{
typedef enum
{
NextArgument,
ParseTestArg
} command_line_parse_state;
command_line_parse_state state = NextArgument;
c_ocoa_command_line_parse_result result = {};
for( int argIndex = 0u; argIndex < argc; ++argIndex )
{
switch( state )
{
case NextArgument:
{
if( string_is_equal( argv[argIndex], "--test", 6u ) )
{
state = ParseTestArg;
continue;
}
break;
}
case ParseTestArg:
{
result.pTestFilePath = argv[argIndex];
state = NextArgument;
break;
}
default:
{
code_path_invalid();
break;
}
}
}
return result;
}
boolean8_t string_name_matches_filter( const char* restrict_modifier pName, const char* restrict_modifier pNameFilter)
{
boolean8_t wildcard = 0u;
while( 1 )
{
if( *pName == 0u )
{
return pNameFilter == NULL || *pNameFilter == 0;
}
else if( *pName != 0u && *pNameFilter == 0u && !wildcard )
{
return 0u;
}
if( *pNameFilter != '*' )
{
if( wildcard )
{
if( *pNameFilter == 0u )
{
//FK: early-out - there are only wildcards left
return 1u;
}
else
{
if( *pNameFilter == *pName )
{
wildcard = 0u;
++pName;
++pNameFilter;
}
else
{
++pName;
}
}
}
else
{
if( *pName == *pNameFilter )
{
++pName;
++pNameFilter;
continue;
}
else
{
return 0u;
}
}
}
else
{
wildcard = 1u;
++pNameFilter;
}
}
return 1u;
}
void print_help(void)
{
printf("C OCOA CODE GENERATOR VERSION %d.%d\n", C_OCOA_GENERATOR_MAJOR_VERSION, C_OCOA_GENERATOR_MINOR_VERSION);
printf("Usage: c_ocoa_generator [OPTIONS] [FILTER]\n\n");
printf("OPTIONS are:\n");
printf("-o {path} set output path (output directory)\n");
printf("-p {prefix} set prefix for output source files\n");
printf("-h print this help text\n\n");
printf("-t export all objc types (write all objc structs into c_ocoa_types.h)\n\n");
printf("FILTER is:\n");
printf("A filter for the class name to be exported. Supports wildcard.\n\n");
printf("eg: 'c_ocoa_generator NS*' - export all classes that start with 'NS'.\n");
}
void evaluate_code_generator_argv_arguments( int argc, const char** argv, c_ocoa_code_generator_parameter* pOutArguments )
{
for( int i = 1; i < argc; ++i )
{
const char* pArg = argv[i];
if( pArg[0] == '-' )
{
switch( pArg[1] )
{
case 'o':
pOutArguments->pOutputPath = argv[i+1];
++i;
break;
case 'p':
pOutArguments->pPrefix = argv[i+1];
++i;
break;
case 'h':
print_help();
exit(0);
break;
case 't':
pOutArguments->exportTypes = true;
++i;
break;
}
}
else
{
//FK: it is assumed that the class name filter is the last argument
pOutArguments->pClassNameFilter = pArg;
break;
}
}
}
boolean8_t c_ocoa_objc_type_dictionary_create_entry_block( c_ocoa_objc_type_dictionary_entry_block** pOutEntryBlock, uint32_t entryCapacityPerBlock )
{
const uint32_t entrySizeInBytes = sizeof(c_ocoa_objc_type_dictionary_entry) * entryCapacityPerBlock;
c_ocoa_objc_type_dictionary_entry* pEntries = (c_ocoa_objc_type_dictionary_entry*)malloc(entrySizeInBytes);
if( pEntries == NULL )
{
return 0;
}
c_ocoa_objc_type_dictionary_entry_block* pBlock = (c_ocoa_objc_type_dictionary_entry_block*)malloc(sizeof(c_ocoa_objc_type_dictionary_entry));
if( pBlock == NULL )
{
free(pEntries);
return 0;
}
memset(pEntries, 0, entrySizeInBytes);
pBlock->pEntries = pEntries;
pBlock->entryCapacity = entryCapacityPerBlock;
pBlock->entryCount = 0;
pBlock->pNext = NULL;
*pOutEntryBlock = pBlock;
return 1;
}
boolean8_t objc_type_dict_create( c_ocoa_objc_type_dictionary* pOutTypeDict )
{
const size_t typeEntryCount = 4096u;
const size_t freeEntryBlockCount = 512u;
const size_t typeEntrySizeInBytes = typeEntryCount * sizeof( void* );
c_ocoa_objc_type_dictionary_entry** ppEntries = ( c_ocoa_objc_type_dictionary_entry** )malloc(typeEntrySizeInBytes);
if( ppEntries == NULL )
{
return 0;
}
if( !c_ocoa_objc_type_dictionary_create_entry_block( &pOutTypeDict->pFreeEntries, freeEntryBlockCount ) )
{
free(ppEntries);
return 0;
}
memset(ppEntries, 0, typeEntrySizeInBytes);
pOutTypeDict->ppTypeEntries = ppEntries;
pOutTypeDict->typeEntryCapacity = typeEntryCount;
pOutTypeDict->typeEntryCount = 0u;
return 1;
}
boolean8_t objc_function_collection_create( c_ocoa_objc_function_collection* pOutFunctionCollection )
{
pOutFunctionCollection->capacity = 64u;
pOutFunctionCollection->size = 0u;
pOutFunctionCollection->pEntries = ( c_ocoa_objc_function_collection_entry* )malloc( sizeof( c_ocoa_objc_function_collection_entry ) * 64u );
if( pOutFunctionCollection->pEntries == NULL )
{
return 0;
}
return 1;
}
boolean8_t strings_are_equal( const char* pStringA, const char* pStringB, const int32_t stringLength )
{
for( int32_t charIndex = 0u; charIndex < stringLength; ++charIndex )
{
if( pStringA[ charIndex ] != pStringB[ charIndex ] )
{
return 0;
}
}
return 1;
}
uint32_t djb2_hash( const char* pString, const int32_t stringLength )
{
uint32_t hash = 5381;
for( int32_t charIndex = 0u; charIndex < stringLength; ++charIndex )
{
const char c = pString[ charIndex ];
hash = ((hash << 5) + hash) + c;
}
return hash;
}
c_ocoa_objc_type_dictionary_entry* objc_type_dict_find_entry( c_ocoa_objc_type_dictionary* pDict, const char* pTypeName, const int32_t typeNameLength )
{
const uint32_t hashValue = djb2_hash( pTypeName, typeNameLength );
const uint32_t entryIndex = hashValue % pDict->typeEntryCapacity;
c_ocoa_objc_type_dictionary_entry* pEntry = pDict->ppTypeEntries[entryIndex];
while( pEntry != NULL && strcmp( pEntry->pHashValue, pTypeName ) != 0 )
{
pEntry = (c_ocoa_objc_type_dictionary_entry*)pEntry->pNext;
}
return pEntry;
}
c_ocoa_objc_type_dictionary_entry* c_ocoa_type_dict_get_free_entry_from_block( c_ocoa_objc_type_dictionary_entry_block* pBlock )
{
c_ocoa_objc_type_dictionary_entry_block* pCurrentBlock = pBlock;
c_ocoa_objc_type_dictionary_entry_block* pPrevBlock = NULL;
while( pCurrentBlock != NULL && pCurrentBlock->entryCount == pCurrentBlock->entryCapacity )
{
pPrevBlock = pCurrentBlock;
pCurrentBlock = (c_ocoa_objc_type_dictionary_entry_block*)pCurrentBlock->pNext;
}
if( pCurrentBlock == NULL )
{
if( !c_ocoa_objc_type_dictionary_create_entry_block((c_ocoa_objc_type_dictionary_entry_block**)&pPrevBlock->pNext, pPrevBlock->entryCapacity ))
{
return NULL;
}
pCurrentBlock = (c_ocoa_objc_type_dictionary_entry_block*)pPrevBlock->pNext;
}
const uint32_t entryIndex = pCurrentBlock->entryCount++;
return pCurrentBlock->pEntries + entryIndex;
}
c_ocoa_objc_type_dictionary_entry* objc_type_dict_insert_or_get_entry( c_ocoa_objc_type_dictionary* restrict_modifier pDict, const char* restrict_modifier pTypeName, const int32_t typeNameLength, boolean8_t* restrict_modifier pOutIsNew )
{
//FK: TODO: Check hash distribution
const uint32_t hashValue = djb2_hash( pTypeName, typeNameLength );
const uint32_t entryIndex = hashValue % pDict->typeEntryCapacity;
c_ocoa_objc_type_dictionary_entry* pEntry = pDict->ppTypeEntries[entryIndex];
c_ocoa_objc_type_dictionary_entry* pPrev = NULL;
const boolean8_t isFirstEntry = pEntry == NULL;
while( pEntry != NULL )
{
if( strcmp( pEntry->pHashValue, pTypeName ) == 0 )
{
*pOutIsNew = 0;
return pEntry;
}
pPrev = pEntry;
pEntry = (c_ocoa_objc_type_dictionary_entry*)pEntry->pNext;
}
pEntry = c_ocoa_type_dict_get_free_entry_from_block(pDict->pFreeEntries);
if( pEntry == NULL )
{
return NULL;
}
if( typeNameLength >= sizeof(pEntry->hashValue) )
{
char* pHashValue = string_allocate_copy( pTypeName, typeNameLength );
if( pHashValue == NULL )
{
return NULL;
}
pEntry->pHashValue = pHashValue;
}
else
{
pEntry->pHashValue = pEntry->hashValue;
}
string_copy_and_add_null_terminator(pEntry->pHashValue, pTypeName, typeNameLength);
*pOutIsNew = 1;
pEntry->index = entryIndex;
if(isFirstEntry)
{
pDict->ppTypeEntries[entryIndex] = pEntry;
++pDict->typeEntryCount;
assert_runtime(pDict->typeEntryCount < pDict->typeEntryCapacity);
}
if( pPrev != NULL )
{
pPrev->pNext = pEntry;
}
return pEntry;
}
int32_t typename_get_reference_position( const char* pTypeName, const int32_t typenameLength )
{
for( int32_t charIndex = 0u; charIndex < typenameLength; ++charIndex )
{
if( pTypeName[ charIndex ] == '*' || pTypeName[ charIndex ] == '^' || pTypeName[ charIndex ] == '?' )
{
return charIndex;
}
}
return INT32_MAX;
}
boolean8_t objc_is_struct_type( const char* pTypeName )
{
return *pTypeName == '{';
}
boolean8_t objc_is_reference_type( const char* pTypeName, const int32_t typeNameLength )
{
const int32_t referencePosition = typename_get_reference_position( pTypeName, typeNameLength );
return referencePosition != INT32_MAX;
}
boolean8_t base_type_resolve( c_ocoa_objc_type_resolve_result* pOutResult, const char* pObjectiveCTypeName )
{
//FK: types are defined in objc/runtime.h
// TODO: Double check with static assert that these still map to what I think they map to
const char* pResolvedBaseType = NULL;
boolean8_t isFloatingType = 0;
uint32_t typeSizeInBits = 0;
switch( *pObjectiveCTypeName )
{
case '@':
pResolvedBaseType = "nsobject_t";
typeSizeInBits = 64u;
break;
case ':':
pResolvedBaseType = "nsselector_t";
typeSizeInBits = 64u;
break;
case '#':
pResolvedBaseType = "nsclass_t";
typeSizeInBits = 64u;
break;
case 'q':
pResolvedBaseType = "long long";
typeSizeInBits = 64u;
break;
case 'Q':
pResolvedBaseType = "unsigned long long";
typeSizeInBits = 64u;
break;
case 'l':
pResolvedBaseType = "long";
typeSizeInBits = 32u;
break;
case 'L':
pResolvedBaseType = "unsigned long";
typeSizeInBits = 32u;
break;
case 'i':
pResolvedBaseType = "int";
typeSizeInBits = 32u;
break;
case 'I':
pResolvedBaseType = "unsigned int";
typeSizeInBits = 32u;
break;
case 's':
pResolvedBaseType = "short";
typeSizeInBits = 16u;
break;
case 'S':
pResolvedBaseType = "unsigned short";
typeSizeInBits = 16u;
break;
case 'c':
pResolvedBaseType = "char";
typeSizeInBits = 8u;
break;
case 'C':
pResolvedBaseType = "unsigned char";
typeSizeInBits = 8u;
break;
case 'r':
pResolvedBaseType = "const char";
typeSizeInBits = 8u;
break;
case '*':
pResolvedBaseType = "char *";
typeSizeInBits = 64u;
break;
case 'v':
case 'V':
pResolvedBaseType = "void";
break;
case 'd':
case 'D':
pResolvedBaseType = "double";
typeSizeInBits = 64u;
isFloatingType = 1u;
break;
case 'f':
case 'F':
pResolvedBaseType = "float";
typeSizeInBits = 32u;
isFloatingType = 1u;
break;
case 'B':
pResolvedBaseType = "bool";
typeSizeInBits = 8u;
break;
default:
return 0;
}
const int32_t resolvedTypeLength = string_get_length_excl_null_terminator( pResolvedBaseType );
pOutResult->isReference = ( *pObjectiveCTypeName == '*' );
pOutResult->isBaseType = 1;
pOutResult->isFloatingType = isFloatingType;
pOutResult->originalTypeLength = 1;
pOutResult->pResolvedType = string_allocate_copy( pResolvedBaseType, resolvedTypeLength );
pOutResult->pOriginalType = string_allocate_copy( pObjectiveCTypeName, 1 );
pOutResult->resolvedTypeLength = resolvedTypeLength;
pOutResult->typeSizeInBits = typeSizeInBits;
return 1;
}
void objc_type_dict_remove_entry( c_ocoa_objc_type_dictionary* pDict, c_ocoa_objc_type_dictionary_entry* pEntryToRemove )
{
const uint32_t entryIndex = pEntryToRemove->index;
c_ocoa_objc_type_dictionary_entry* pEntry = pDict->ppTypeEntries[entryIndex];
c_ocoa_objc_type_dictionary_entry* pPrev = NULL;
while( strcmp( pEntry->pHashValue, pEntryToRemove->pHashValue ) != 0 )
{
pPrev = pEntry;
pEntry = ( c_ocoa_objc_type_dictionary_entry* )pEntry->pNext;
}
assert_runtime( pEntry != NULL );
//FK: is first entry in linked list?
if( pPrev == NULL )
{
pDict->ppTypeEntries[entryIndex] = NULL;
assert_runtime( pDict->typeEntryCount > 0 );
--pDict->typeEntryCount;
}
else
{
pPrev->pNext = pEntry->pNext;
}
}
boolean8_t objc_type_dict_resolve_base_type( c_ocoa_objc_type_resolve_result* pOutResult, c_ocoa_objc_type_dictionary* pDict, const char* pObjectiveCTypeName )
{
boolean8_t isNewDictEntry = 0;
c_ocoa_objc_type_dictionary_entry* pDictEntry = objc_type_dict_insert_or_get_entry( pDict, pObjectiveCTypeName, 1, &isNewDictEntry );
if( pDictEntry == NULL )
{
printf_stderr( "[error] Could not resolve type name '%s' because we ran out of memory.", pObjectiveCTypeName );
return 0;
}
if( !isNewDictEntry )
{
*pOutResult = pDictEntry->resolveResult;
return 1;
}
const boolean8_t resolvedSuccessful = base_type_resolve( pOutResult, pObjectiveCTypeName );
if( !resolvedSuccessful )
{
objc_type_dict_remove_entry( pDict, pDictEntry );
return 0;
}
pDictEntry->resolveResult = *pOutResult;
return 1;
}
boolean8_t objc_type_dict_resolve_struct_type( c_ocoa_objc_type_resolve_result* pOutResult, c_ocoa_objc_type_dictionary* pDict, const char* pTypeName, int32_t typeNameLength );
boolean8_t struct_type_resolve( c_ocoa_objc_type_resolve_result* pOutResult, c_ocoa_objc_type_dictionary* pDict, const char* pTypeName, int32_t typeNameLength )
{
const char* pTypeDefinitionStart = pTypeName;
const char* pTypeNameStart = pTypeName + 1; //FK: +1 to skip initial '{' of struct definition
const char* pTypeNameEnd = string_find_next_char_position( pTypeDefinitionStart, '=' );
const int32_t resolvedTypeNameLength = cast_size_to_int32( pTypeNameEnd - pTypeNameStart );
const char* pStructDefinition = pTypeNameEnd;
uint32_t structSizeInBits = 0u;
uint8_t scopeCount = 1u; //FK: start at one to include first leading '{'
++pStructDefinition;
while( scopeCount > 0u )
{
if( *pStructDefinition == '{' )
{
++scopeCount;
const char* pEmbeddedTypeNameStart = pStructDefinition;
const char* pEmbeddedTypeNameEnd = string_find_next_char_position( pStructDefinition, '}' );
const int32_t embeddedTypeNameLength = cast_size_to_int32( pEmbeddedTypeNameEnd - pEmbeddedTypeNameStart );
c_ocoa_objc_type_resolve_result resolveResult = {};
objc_type_dict_resolve_struct_type( &resolveResult, pDict, pEmbeddedTypeNameStart, embeddedTypeNameLength );
structSizeInBits += resolveResult.typeSizeInBits;
pStructDefinition += embeddedTypeNameLength;
}
else if( *pStructDefinition == '}' )
{
--scopeCount;
++pStructDefinition;
}