-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc_library.cpp
2017 lines (1763 loc) · 66.5 KB
/
c_library.cpp
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
#include <cstdlib> // for system()
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#include "gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include <stdlib.h>
GEN_NS_BEGIN
#include "helpers/base_codegen.hpp"
#include "helpers/misc.hpp"
GEN_NS_END
#include "components/memory.fixed_arena.hpp"
#include "components/misc.hpp"
#include "components/containers.array.hpp"
#include "components/containers.hashtable.hpp"
using namespace gen;
constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's c_library.cpp "
"(See: https://github.com/Ed94/gencpp)\n\n";
constexpr Str roll_own_dependencies_guard_start = txt(R"(
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file.
// Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl
#ifndef GEN_ROLL_OWN_DEPENDENCIES
)");
constexpr Str roll_own_dependencies_guard_end = txt(R"(
// GEN_ROLL_OWN_DEPENDENCIES
#endif
)");
constexpr Str implementation_guard_start = txt(R"(
#pragma region GENCPP IMPLEMENTATION GUARD
#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED)
# define GEN_IMPLEMENTED
)");
constexpr Str implementation_guard_end = txt(R"(
#endif
#pragma endregion GENCPP IMPLEMENTATION GUARD
)");
#define path_refactor_script "./c_library.refactor"
#define path_format_style "../scripts/.clang-format "
#define scratch_file "gen/scratch.hpp"
#define path_base "../base/"
Code refactor( Code code ) {
return code_refactor_and_format(code, scratch_file, path_refactor_script, nullptr );
}
Code refactor_and_format( Code code ) {
return code_refactor_and_format(code, scratch_file, path_refactor_script, path_format_style );
}
constexpr bool helper_use_c_definition = true;
int gen_main()
{
Context ctx {};
gen::init(& ctx);
register_macros( args(
(Macro { txt("bit"), MT_Expression, MF_Functional }),
(Macro { txt("bitfield_is_set"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }),
(Macro { txt("cast"), MT_Expression, MF_Functional }),
(Macro { txt("ccast"), MT_Expression, MF_Functional }),
(Macro { txt("rcast"), MT_Expression, MF_Functional }),
(Macro { txt("pcast"), MT_Expression, MF_Functional }),
(Macro { txt("scast"), MT_Expression, MF_Functional }),
(Macro { txt("stringize_va"), MT_Expression, MF_Functional }),
(Macro { txt("stringize"), MT_Expression, MF_Functional }),
(Macro { txt("do_once"), MT_Expression, MF_Functional }),
(Macro { txt("do_once_defer"), MT_Expression, MF_Functional }),
(Macro { txt("do_once_start"), MT_Statement, MF_Null }),
(Macro { txt("do_once_end"), MT_Statement, MF_Null }),
(Macro { txt("labeled_scope_start"), MT_Statement, MF_Null }),
(Macro { txt("labeled_scope_end"), MT_Statement, MF_Null }),
(Macro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }),
(Macro { txt("num_args_impl"), MT_Expression, MF_Functional }),
(Macro { txt("num_args"), MT_Expression, MF_Functional }),
(Macro { txt("count_of"), MT_Expression, MF_Functional }),
(Macro { txt("clamp"), MT_Expression, MF_Functional }),
(Macro { txt("is_between"), MT_Expression, MF_Functional }),
(Macro { txt("size_of"), MT_Expression, MF_Functional }),
(Macro { txt("min"), MT_Expression, MF_Functional }),
(Macro { txt("max"), MT_Expression, MF_Functional }),
(Macro { txt("offset_of"), MT_Expression, MF_Functional }),
(Macro { txt("static_assert"), MT_Statement, MF_Functional }),
(Macro { txt("typeof"), MT_Expression, MF_Null }),
(Macro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }),
(Macro { txt("GEN_API_C_END"), MT_Statement, MF_Null }),
(Macro { txt("nullptr"), MT_Expression, MF_Null }),
(Macro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }),
(Macro { txt("struct_init"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }),
(Macro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null }),
(Macro { txt("Array"), MT_Typename, MF_Functional }),
(Macro { txt("HashTable"), MT_Typename, MF_Functional }),
(Macro { txt("Using_Code"), MT_Statement, MF_Functional }),
(Macro { txt("Using_CodeOps"), MT_Statement, MF_Functional }),
(Macro { txt("kilobytes"), MT_Expression, MF_Functional }),
(Macro { txt("megabytes"), MT_Expression, MF_Functional }),
(Macro { txt("gigabytes"), MT_Expression, MF_Functional }),
(Macro { txt("terabytes"), MT_Expression, MF_Functional }),
(Macro { txt("GEN__ONES"), MT_Expression, MF_Null }),
(Macro { txt("GEN__HIGHS"), MT_Expression, MF_Null }),
(Macro { txt("GEN__HAS_ZERO"), MT_Expression, MF_Functional }),
(Macro { txt("zero_item"), MT_Expression, MF_Functional }),
(Macro { txt("zero_array"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_DEFAULT_MEMORY_ALIGNMENT"), MT_Expression, MF_Null }),
(Macro { txt("GEN_DEFAULT_ALLOCATOR_FLAGS"), MT_Expression, MF_Null }),
(Macro { txt("alloc_item"), MT_Expression, MF_Functional }),
(Macro { txt("alloc_array"), MT_Expression, MF_Functional }),
(Macro { txt("malloc"), MT_Expression, MF_Functional }),
(Macro { txt("mfree"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_PRINTF_MAXLEN"), MT_Expression, MF_Null }),
(Macro { txt("cast_to_str"), MT_Expression, MF_Functional }),
(Macro { txt("current"), MT_Expression, MF_Null }),
(Macro { txt("txt"), MT_Expression, MF_Functional }),
(Macro { txt("GEN_FILE_OPEN_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(Macro { txt("GEN_FILE_READ_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(Macro { txt("GEN_FILE_WRITE_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(Macro { txt("GEN_FILE_SEEK_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(Macro { txt("GEN_FILE_CLOSE_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(Macro { txt("log_failure"), MT_Expression, MF_Null }),
(Macro { txt("operator"), MT_Expression, MF_Null }),
(Macro { txt("InvalidCode"), MT_Expression, MF_Null }),
(Macro { txt("NullCode"), MT_Expression, MF_Null }),
(Macro { txt("Verify_POD"), MT_Expression, MF_Functional }),
(Macro { txt("gen_main"), MT_Statement, MF_Null })
));
register_macros( args(
(Macro { txt("name"), MT_Expression, MF_Functional }),
(Macro { txt("code"), MT_Expression, MF_Functional }),
(Macro { txt("args"), MT_Expression, MF_Functional }),
(Macro { txt("code_str"), MT_Expression, MF_Functional }),
(Macro { txt("code_fmt"), MT_Expression, MF_Functional }),
(Macro { txt("parse_fmt"), MT_Expression, MF_Functional }),
(Macro { txt("token_fmt"), MT_Expression, MF_Functional }),
(Macro { txt("check_member_val"), MT_Expression, MF_Functional }),
(Macro { txt("check_member_str"), MT_Expression, MF_Functional }),
(Macro { txt("check_member_content"), MT_Expression, MF_Functional }),
(Macro { txt("check_member_ast"), MT_Expression, MF_Functional }),
(Macro { txt("check_params"), MT_Expression, MF_Functional }),
(Macro { txt("check_param_eq_ret"), MT_Expression, MF_Functional }),
(Macro { txt("specs"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }),
(Macro { txt("name_check"), MT_Expression, MF_Functional }),
(Macro { txt("null_check"), MT_Expression, MF_Functional }),
(Macro { txt("def_body_start"), MT_Expression, MF_Functional }),
(Macro { txt("def_body_code_array_start"), MT_Expression, MF_Functional }),
(Macro { txt("move_forward"), MT_Expression, MF_Functional }),
(Macro { txt("skip_whitespace"), MT_Expression, MF_Functional }),
(Macro { txt("end_line"), MT_Expression, MF_Functional }),
(Macro { txt("check_parse_args"), MT_Expression, MF_Functional }),
(Macro { txt("currtok_noskip"), MT_Expression, MF_Null }),
(Macro { txt("currtok"), MT_Expression, MF_Null }),
(Macro { txt("peektok"), MT_Expression, MF_Null }),
(Macro { txt("prevtok"), MT_Expression, MF_Null }),
(Macro { txt("nexttok"), MT_Expression, MF_Null }),
(Macro { txt("nexttok_noskip"), MT_Expression, MF_Null }),
(Macro { txt("eat"), MT_Expression, MF_Functional }),
(Macro { txt("left"), MT_Expression, MF_Null | MF_Allow_As_Identifier }),
(Macro { txt("def_assign"), MT_Expression, MF_Functional }),
(Macro { txt("CHECK_WAS_DEFINED"), MT_Expression, MF_Null }),
(Macro { txt("check_noskip"), MT_Expression, MF_Functional }),
(Macro { txt("check"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }),
(Macro { txt("push_scope"), MT_Expression, MF_Functional }),
(Macro { txt("cut_length"), MT_Expression, MF_Null }),
(Macro { txt("cut_ptr"), MT_Expression, MF_Null }),
(Macro { txt("pos"), MT_Expression, MF_Null }),
(Macro { txt("move_fwd"), MT_Expression, MF_Functional }),
(Macro { txt("Entry"), MT_Expression, MF_Functional }),
(Macro { txt("CheckEndParams"), MT_Expression, MF_Functional }),
(Macro { txt("UseTemplateCapture"), MT_Expression, MF_Null }),
(Macro { txt("check_current"), MT_Expression, MF_Functional })
));
Code push_ignores = scan_file( path_base "helpers/push_ignores.inline.hpp" );
Code pop_ignores = scan_file( path_base "helpers/pop_ignores.inline.hpp" );
Code c_library_header_start = scan_file( "components/header_start.hpp" );
// Header Content: Reflection and Generation
#pragma region Resolve Dependencies
Code header_platform = scan_file( path_base "dependencies/platform.hpp" );
Code header_macros = scan_file( path_base "dependencies/macros.hpp" );
Code header_generic_macros = scan_file( "components/generic_macros.h" );
Code header_basic_types = scan_file( path_base "dependencies/basic_types.hpp" );
Code header_debug = scan_file( path_base "dependencies/debug.hpp" );
Code header_string_ops = scan_file( path_base "dependencies/string_ops.hpp" );
Code header_hashing = scan_file( path_base "dependencies/hashing.hpp" );
Code header_timing = scan_file( path_base "dependencies/timing.hpp" );
CodeBody parsed_header_memory = parse_file( path_base "dependencies/memory.hpp" );
CodeBody header_memory = def_body(CT_Global_Body);
for ( Code entry = parsed_header_memory.begin(); entry != parsed_header_memory.end(); ++ entry ) switch (entry->Type)
{
case CT_Using:
{
log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name);
CodeUsing using_ver = cast(CodeUsing, entry);
CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType);
header_memory.append(typedef_ver);
}
break;
case CT_Function_Fwd:
{
CodeFn fn = cast(CodeFn, entry);
header_memory.append(fn);
}
break;
case CT_Function:
{
CodeFn fn = cast(CodeFn, entry);
if (fn->Name.is_equal(txt("heap")))
{
Code heap_def = untyped_str(txt("#define heap() (AllocatorInfo){ heap_allocator_proc, nullptr }"));
header_memory.append(heap_def);
continue;
}
if (fn->Specs) {
s32 constexpr_found = fn->Specs.remove( Spec_Constexpr );
if (constexpr_found > -1) {
//log_fmt("Found constexpr: %SB\n", entry.to_strbuilder());
fn->Specs.append(Spec_Inline);
}
}
header_memory.append(fn);
}
break;
case CT_Template:
{
CodeTemplate tmpl = cast(CodeTemplate, entry);
if ( tmpl->Declaration->Name.contains(txt("swap")))
{
register_macro({ txt("swap"), MT_Expression, MF_Functional });
CodeDefine macro_swap = parse_define( txt(R"(
#define swap( a, b ) \
do \
{ \
typeof( a ) temp = ( a ); \
( a ) = ( b ); \
( b ) = temp; \
} while ( 0 )
)"
));
header_memory.append(macro_swap);
}
}
break;
case CT_Enum:
{
convert_cpp_enum_to_c(cast(CodeEnum, entry), header_memory);
}
break;
case CT_Struct_Fwd:
{
CodeTypedef tdef = parse_typedef(token_fmt("name", entry->Name, stringize( typedef struct <name> <name>; )));
header_memory.append(entry);
header_memory.append(tdef);
}
break;
case CT_Class:
case CT_Struct:
{
CodeBody body = cast(CodeBody, entry->Body);
CodeBody new_body = def_body( entry->Body->Type );
for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type)
{
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP"), body_entry, body, new_body );
if (found) break;
new_body.append(body_entry);
}
break;
default:
new_body.append(body_entry);
break;
}
entry->Body = new_body;
CodeTypedef tdef = parse_typedef(token_fmt("name", entry->Name, stringize( typedef struct <name> <name>; )));
header_memory.append(entry);
header_memory.append(tdef);
}
break;
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP"), entry, parsed_header_memory, header_memory );
if (found) break;
header_memory.append(entry);
}
break;
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_memory, header_memory );
if (found) break;
header_memory.append(entry);
}
break;
case CT_Preprocess_Pragma:
{
CodePragma pragma = cast(CodePragma, entry);
b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, header_memory);
if (found) break;
header_memory.append(entry);
}
break;
default: {
header_memory.append(entry);
}
break;
}
CodeBody parsed_header_printing = parse_file( path_base "dependencies/printing.hpp" );
CodeBody header_printing = def_body(CT_Global_Body);
for ( Code entry = parsed_header_printing.begin(); entry != parsed_header_printing.end(); ++ entry ) switch (entry->Type)
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_printing, header_printing );
if (found) break;
header_printing.append(entry);
}
break;
case CT_Variable:
{
if ( str_contains(entry->Name, txt("Msg_Invalid_Value")))
{
Opts_def_define opts = { {}, entry->Value->Content };
CodeDefine define = def_define(entry->Name, MT_Expression, opts );
header_printing.append(define);
continue;
}
header_printing.append(entry);
}
break;
default:
header_printing.append(entry);
break;
}
Code array_ssize = gen_array(txt("gen_ssize"), txt("Array_gen_ssize"));
Code array_string_cached = gen_array(txt("gen_StrCached"), txt("Array_gen_StrCached"));
CodeBody parsed_header_strings = parse_file( path_base "dependencies/strings.hpp" );
CodeBody header_strings = def_body(CT_Global_Body);
for ( Code entry = parsed_header_strings.begin(); entry != parsed_header_strings.end(); ++ entry ) switch (entry->Type)
{
case CT_Preprocess_If:
{
CodePreprocessCond cond = cast(CodePreprocessCond, entry);
if (cond->Content.is_equal(txt("GEN_COMPILER_C")))
{
++ entry; // Remove #if GEN_COMPILER_C
for ( ; entry->Type != CT_Preprocess_Else
&& entry->Type != CT_Preprocess_EndIf; ++ entry ) {
header_strings.append(entry); // Preserve content
}
for ( ; entry->Type != CT_Preprocess_EndIf; ++ entry ) {} // Discard C++
// #endif discarded by for loop
break;
}
bool found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_header_strings, header_strings);
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP"), entry, parsed_header_strings, header_strings);
if (found) break;
header_strings.append(entry);
}
break;
case CT_Preprocess_IfDef:
{
ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_strings, header_strings );
}
break;
case CT_Preprocess_IfNotDef:
{
//log_fmt("\nIFNDEF: %S\n", entry->Content);
header_strings.append(entry);
}
break;
case CT_Struct_Fwd:
{
if ( entry->Name.is_equal(txt("StrBuilder")) )
{
CodeTypedef c_def = parse_typedef(code( typedef char* StrBuilder; ));
header_strings.append(c_def);
header_strings.append(fmt_newline);
++ entry;
continue;
}
else
{
CodeTypedef c_def = parse_typedef(token_fmt("name", entry->Name, stringize( typedef struct <name> <name>; )));
header_strings.append(c_def);
}
header_strings.append(entry);
}
break;
case CT_Struct:
{
CodeBody body = cast(CodeBody, entry->Body);
CodeBody new_body = def_body( entry->Body->Type );
for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type)
{
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), body_entry, body, new_body );
if (found) break;
new_body.append(body_entry);
}
break;
default:
new_body.append(body_entry);
break;
}
entry->Body = new_body;
header_strings.append(entry);
}
break;
case CT_Typedef:
{
Str name_string_table = txt("StringTable");
CodeTypedef td = cast(CodeTypedef, entry);
if (td->Name.contains(name_string_table))
{
CodeBody ht = gen_hashtable(txt("gen_Str"), name_string_table);
header_strings.append(ht);
break;
}
header_strings.append(td);
}
break;
default:
header_strings.append(entry);
break;
}
CodeBody array_u8 = gen_array(txt("gen_u8"), txt("Array_gen_u8"));
CodeBody parsed_header_filesystem = parse_file( path_base "dependencies/filesystem.hpp" );
CodeBody header_filesystem = def_body(CT_Global_Body);
for ( Code entry = parsed_header_filesystem.begin(); entry != parsed_header_filesystem.end(); ++ entry ) switch (entry->Type)
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_filesystem, header_filesystem );
if (found) break;
header_filesystem.append(entry);
}
break;
case CT_Enum:
{
if (entry->Name.is_equal(txt("FileOperations")))
continue;
convert_cpp_enum_to_c(cast(CodeEnum, entry), header_filesystem);
}
break;
case CT_Enum_Fwd:
case CT_Struct_Fwd:
case CT_Struct:
case CT_Union:
case CT_Union_Fwd:
{
Str type_str = codetype_to_keyword_str(entry->Type);
Str formated_tmpl = token_fmt_impl( 3,
"type", type_str
, "name", entry->Name,
stringize(
typedef <type> <name> <name>;
));
CodeTypedef tdef = parse_typedef(formated_tmpl);
header_filesystem.append(entry);
header_filesystem.append(tdef);
}
break;
case CT_Variable:
{
CodeVar var = cast(CodeVar, entry);
if (var->Specs.has(Spec_Constexpr) > -1)
{
Opts_def_define opts = { {}, entry->Value->Content };
CodeDefine define = def_define(entry->Name, MT_Expression, opts);
header_filesystem.append(define);
continue;
}
header_filesystem.append(entry);
}
break;
default:
header_filesystem.append(entry);
break;
}
CodeBody array_adt_node = gen_array(txt("gen_ADT_Node"), txt("Array_gen_ADT_Node"));
CodeBody parsed_header_parsing = parse_file( path_base "dependencies/parsing.hpp" );
CodeBody header_parsing = def_body(CT_Global_Body);
for ( Code entry = parsed_header_parsing.begin(); entry != parsed_header_parsing.end(); ++ entry ) switch (entry->Type)
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_parsing, header_parsing );
if (found) break;
header_parsing.append(entry);
}
break;
case CT_Preprocess_Pragma:
{
if ( entry->Content.contains(txt("ADT")) )
{
header_parsing.append(entry);
header_parsing.append(fmt_newline);
// Add ADT_Node forward and typedef early.
CodeStruct adt_node_fwd = parse_struct(code( struct gen_ADT_Node; ));
CodeTypedef adt_node_typedef = parse_typedef(code( typedef struct gen_ADT_Node gen_ADT_Node; ));
header_parsing.append(adt_node_fwd);
header_parsing.append(adt_node_typedef);
// Skip typedef since we added it
b32 continue_for = true;
for (Code array_entry = array_adt_node.begin(); continue_for && array_entry != array_adt_node.end(); ++ array_entry) switch (array_entry->Type)
{
case CT_Typedef:
{
// pop the array entry
array_adt_node->NumEntries -= 1;
Code next = array_entry->Next;
Code prev = array_entry->Prev;
next->Prev = array_entry->Prev;
prev->Next = next;
if ( array_adt_node->Front == array_entry )
array_adt_node->Front = next;
header_parsing.append(array_entry);
continue_for = false;
}
break;
}
}
}
break;
case CT_Enum:
{
convert_cpp_enum_to_c(cast(CodeEnum, entry), header_parsing);
}
break;
case CT_Struct:
{
CodeStruct struct_def = cast(CodeStruct, entry);
if ( struct_def->Name.is_equal(txt("ADT_Node") ) )
{
header_parsing.append(entry);
// We need to define the array for ADT_Node right here.
header_parsing.append(array_adt_node);
header_parsing.append(fmt_newline);
continue;
}
Str type_str = codetype_to_keyword_str(entry->Type);
Str formated_tmpl = token_fmt_impl( 3,
"type", type_str
, "name", entry->Name,
stringize(
typedef <type> <name> <name>;
));
CodeTypedef tdef = parse_typedef(formated_tmpl);
header_parsing.append(entry);
header_parsing.append(tdef);
}
break;
default:
{
header_parsing.append(entry);
}
break;
}
#pragma endregion Resolve Dependencies
#pragma region Resolve Components
// Only has operator overload definitions that C doesn't need.
// CodeBody ast_inlines = gen_ast_inlines();
CodeBody ecode = gen_ecode ( path_base "enums/ECodeTypes.csv", helper_use_c_definition );
CodeBody eoperator = gen_eoperator ( path_base "enums/EOperator.csv", helper_use_c_definition );
CodeBody especifier = gen_especifier( path_base "enums/ESpecifier.csv", helper_use_c_definition );
CodeBody parsed_types = parse_file( path_base "components/types.hpp" );
CodeBody types = def_body(CT_Global_Body);
for ( Code entry = parsed_types.begin(); entry != parsed_types.end(); ++ entry ) switch(entry->Type)
{
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_types, types );
if (found) break;
types.append(entry);
}
break;
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_types, types );
if (found) break;
types.append(entry);
}
break;
case CT_Using:
{
CodeUsing using_ver = cast(CodeUsing, entry);
if (using_ver->UnderlyingType->ReturnType)
{
CodeTypename type = using_ver->UnderlyingType;
CodeTypedef typedef_ver = parse_typedef(token_fmt(
"ReturnType", to_strbuilder(type->ReturnType).to_str()
, "Name" , using_ver->Name
, "Parameters", to_strbuilder(type->Params).to_str()
, stringize(
typedef <ReturnType>( * <Name>)(<Parameters>);
)));
types.append(typedef_ver);
break;
}
CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType);
types.append(typedef_ver);
}
break;
case CT_Enum:
{
if (entry->Name.is_equal(txt("ETypenameTag")))
{
#pragma push_macro("enum_underlying")
#undef enum_underlying
entry->UnderlyingTypeMacro = untyped_str(token_fmt("type", entry->UnderlyingType->Name, stringize(enum_underlying(<type>))));
entry->UnderlyingType = CodeTypename{nullptr};
types.append(entry);
#pragma pop_macro("enum_underlying")
CodeTypedef entry_td = parse_typedef(code( typedef u16 ETypenameTag; ));
types.append(entry_td);
continue;
}
//log_fmt("Detected ENUM: %SB", entry->Name);
convert_cpp_enum_to_c(cast(CodeEnum, entry), types);
}
break;
default:
types.append(entry);
break;
}
CodeBody array_token = gen_array(txt("gen_Token"), txt("Array_gen_Token"));
CodeBody parsed_parser_types = parse_file( path_base "components/parser_types.hpp" );
CodeBody parser_types = def_body(CT_Global_Body);
for ( Code entry = parsed_parser_types.begin(); entry != parsed_parser_types.end(); ++ entry ) switch(entry->Type)
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_parser_types, parser_types );
if (found) break;
parser_types.append(entry);
}
break;
case CT_Enum:
{
if (entry->Name.Len)
{
convert_cpp_enum_to_c(cast(CodeEnum, entry), parser_types);
break;
}
parser_types.append(entry);
}
break;
case CT_Struct:
{
if ( entry->Name.is_equal(txt("Token")))
{
// Add struct Token forward and typedef early.
CodeStruct token_fwd = parse_struct(code( struct Token; ));
CodeTypedef token_typedef = parse_typedef(code( typedef struct Token Token; ));
parser_types.append(token_fwd);
parser_types.append(token_typedef);
// Skip typedef since we added it
b32 continue_for = true;
for (Code array_entry = array_token.begin(); continue_for && array_entry != array_token.end(); ++ array_entry) switch (array_entry->Type)
{
case CT_Typedef:
{
// pop the array entry
array_token->NumEntries -= 1;
Code next = array_entry->Next;
Code prev = array_entry->Prev;
next->Prev = array_entry->Prev;
prev->Next = next;
if ( array_token->Front == array_entry )
array_token->Front = next;
parser_types.append(array_entry);
continue_for = false;
}
break;
}
// Append the struct
parser_types.append(entry);
// Append the token array
parser_types.append(array_token);
continue;
}
CodeTypedef struct_tdef = parse_typedef(token_fmt("name", entry->Name, stringize( typedef struct <name> <name>; )));
parser_types.append(struct_tdef);
parser_types.append(entry);
}
break;
case CT_Variable:
{
CodeVar var = cast(CodeVar, entry);
if (var->Specs && var->Specs.has(Spec_Constexpr) > -1) {
Code define_ver = untyped_str(token_fmt(
"name", var->Name
, "value", var->Value->Content
, "type", var->ValueType.to_strbuilder().to_str()
, "#define <name> (<type>) <value>\n"
));
parser_types.append(define_ver);
continue;
}
parser_types.append(entry);
}
break;
default:
parser_types.append(entry);
break;
}
// Used to track which functions need generic selectors.
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(_ctx->Allocator_Temp, 16);
CodeBody parsed_ast = parse_file( path_base "components/ast.hpp" );
CodeBody ast = def_body(CT_Global_Body);
for ( Code entry = parsed_ast.begin(); entry != parsed_ast.end(); ++ entry ) switch (entry->Type)
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_ast, ast );
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_EXECUTION_EXPRESSION_SUPPORT"), entry, parsed_ast, ast );
if (found) break;
ast.append(entry);
}
break;
case CT_Preprocess_If:
{
CodePreprocessCond cond = cast(CodePreprocessCond, entry);
if (cond->Content.is_equal(txt("GEN_COMPILER_C")))
{
++ entry; // #if
for ( ; entry != parsed_ast.end() && entry->Type != CT_Preprocess_Else; ++ entry) {
ast.append(entry);
}
for ( ; entry != parsed_ast.end() && entry->Type != CT_Preprocess_EndIf; ++ entry) {}
++ entry; // Consume endif
continue;
}
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_ast, ast);
if (found) break;
ast.append(entry);
}
break;
case CT_Preprocess_Pragma:
{
if ( ! entry->Content.contains(txt("region Code C-Interface"))) {
continue;
}
// Reached the #pragma region Code C-Interface
for (b32 continue_for = true; continue_for; ++ entry) switch(entry->Type)
{
default:
// Pass through everything but function forwards or the end region pragma
ast.append(entry);
break;
case CT_Function_Fwd:
{
// Were going to wrap usage of these procedures into generic selectors in code_types.hpp section,
// so we're changing the namespace to code__<name>
CodeFn fn = cast(CodeFn, entry);
if (fn->Name.starts_with(txt("code_")))
{
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "code__%S", actual_name );
fn->Name = cache_str(new_name);
code_c_interface.append(fn);
}
ast.append(entry);
}
break;
case CT_Preprocess_Pragma:
// Reached the end of the interface, go back to regular ast.hpp iteration.
ast.append(entry);
if ( entry->Content.contains(txt("endregion Code C-Interface"))) {
continue_for = false;
}
break;
}
}
break;
case CT_Struct_Fwd:
{
CodeStruct fwd = cast(CodeStruct, entry);
CodeTypedef tdef = parse_typedef(token_fmt("name", fwd->Name, stringize( typedef struct <name> <name>; )));
ast.append(fwd);
ast.append(tdef);
}
break;
case CT_Struct:
{
CodeStruct struct_def = cast(CodeStruct, entry);
ast.append(struct_def);
if ( ! entry->Name.is_equal(txt("AST")))
{
CodeTypedef tdef = parse_typedef(token_fmt("name", struct_def->Name, stringize( typedef struct <name> <name>; )));
ast.append(tdef);
}
}
break;
case CT_Variable:
{
CodeVar var = cast(CodeVar, entry);
s32 constexpr_found = var->Specs ? var->Specs.remove( Spec_Constexpr ) : - 1;
if (constexpr_found > -1) {
//log_fmt("Found constexpr: %SB\n", entry.to_strbuilder());
if (var->Name.contains(txt("AST_ArrSpecs_Cap")))
{
Code def = untyped_str(txt(
R"(#define AST_ArrSpecs_Cap \
( \
AST_POD_Size \
- sizeof(Code) \
- sizeof(StrCached) \
- sizeof(Code) * 2 \
- sizeof(Token*) \
- sizeof(Code) \
- sizeof(CodeType) \
- sizeof(ModuleFlag) \
- sizeof(u32) \
) \
/ sizeof(Specifier) - 1
)"
));
ast.append(def);
break;
}
Opts_def_define opts = { {}, var->Value.to_strbuilder() };
CodeDefine def = def_define(var->Name, MT_Expression, opts );
ast.append(def);
break;
}
ast.append(var);
}
break;
default:
ast.append(entry);
break;
}
Str code_typenames[] = {
txt("Code"),
txt("CodeBody"),
txt("CodeAttributes"),
txt("CodeComment"),
txt("CodeClass"),
txt("CodeConstructor"),
txt("CodeDefine"),
txt("CodeDefineParams"),
txt("CodeDestructor"),
txt("CodeEnum"),
txt("CodeExec"),
txt("CodeExtern"),
txt("CodeInclude"),
txt("CodeFriend"),
txt("CodeFn"),
txt("CodeModule"),
txt("CodeNS"),
txt("CodeOperator"),
txt("CodeOpCast"),
txt("CodePragma"),
txt("CodeParams"),
txt("CodePreprocessCond"),
txt("CodeSpecifiers"),
txt("CodeStruct"),
txt("CodeTemplate"),
txt("CodeTypename"),
txt("CodeTypedef"),
txt("CodeUnion"),
txt("CodeUsing"),
txt("CodeVar"),
};
CodeBody parsed_code_types = parse_file( path_base "components/code_types.hpp" );
CodeBody code_types = def_body(CT_Global_Body);
for ( Code entry = parsed_code_types.begin(); entry != parsed_code_types.end(); ++ entry ) switch( entry->Type )
{
case CT_Preprocess_If:
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_code_types, code_types );
if (found) {
++ entry; // Skip a newline...
break;
}
found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_code_types, code_types );
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_EXECUTION_EXPRESSION_SUPPORT"), entry, parsed_code_types, code_types);
if (found) break;
code_types.append(entry);
}
break;
case CT_Preprocess_Pragma: if ( entry->Content.is_equal(txt("region Code Type C-Interface")) )
{
code_types.append(entry);
code_types.append(fmt_newline);
/*
This thing makes a:
#define code_<interface_name>( code, ... ) _Generic( (code), \
<slots> of definitions that look like: <typeof(code)>: code__<interface_name>, \
default: gen_generic_selection (Fail case) \
) GEN_RESOLVED_FUNCTION_CALL( code, ... ) \