-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.upfront.cpp
2098 lines (1865 loc) · 55.7 KB
/
interface.upfront.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
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "interface.cpp"
#endif
#pragma region Upfront
enum OpValidateResult : u32
{
OpValResult_Fail,
OpValResult_Global,
OpValResult_Member
};
internal neverinline
OpValidateResult operator__validate( Operator op, CodeParams params_code, CodeTypename ret_type, CodeSpecifiers specifier )
{
if ( op == Op_Invalid )
{
log_failure("gen::def_operator: op cannot be invalid");
return OpValResult_Fail;
}
#pragma region Helper Macros
# define check_params() \
if ( ! params_code ) \
{ \
log_failure("gen::def_operator: params is null and operator %S requires it", operator_to_str(op)); \
return OpValResult_Fail; \
} \
if ( params_code->Type != CT_Parameters ) \
{ \
log_failure("gen::def_operator: params is not of Parameters type - %S", code_debug_str( cast(Code, params_code))); \
return OpValResult_Fail; \
}
# define check_param_eq_ret() \
if ( ! is_member_symbol && ! code_is_equal(cast(Code, params_code->ValueType), cast(Code, ret_type)) ) \
{ \
log_failure("gen::def_operator: operator %S requires first parameter to equal return type\n" \
"param types: %S\n" \
"return type: %S", \
operator_to_str(op), \
code_debug_str(cast(Code, params_code)), \
code_debug_str(cast(Code, ret_type)) \
); \
return OpValResult_Fail; \
}
#pragma endregion Helper Macros
if ( ! ret_type )
{
log_failure("gen::def_operator: ret_type is null but is required by operator %S", operator_to_str(op));
}
if ( ret_type->Type != CT_Typename )
{
log_failure("gen::def_operator: operator %S - ret_type is not of typename type - %S",
operator_to_str(op),
code_debug_str(cast(Code, ret_type))
);
return OpValResult_Fail;
}
bool is_member_symbol = false;
switch ( op )
{
# define specs( ... ) num_args( __VA_ARGS__ ), __VA_ARGS__
case Op_Assign:
check_params();
if ( params_code->NumEntries > 1 )
{
log_failure("gen::def_operator: "
"operator %S does not support non-member definition (more than one parameter provided) - %S",
operator_to_str(op),
code_debug_str(cast(Code, params_code))
);
return OpValResult_Fail;
}
is_member_symbol = true;
break;
case Op_Assign_Add:
case Op_Assign_Subtract:
case Op_Assign_Multiply:
case Op_Assign_Divide:
case Op_Assign_Modulo:
case Op_Assign_BAnd:
case Op_Assign_BOr:
case Op_Assign_BXOr:
case Op_Assign_LShift:
case Op_Assign_RShift:
check_params();
if ( params_code->NumEntries == 1 )
is_member_symbol = true;
else
check_param_eq_ret();
if (params_code->NumEntries > 2 )
{
log_failure("gen::def_operator: operator %S may not be defined with more than two parametes - param count; %d\n%S"
, operator_to_str(op)
, params_code->NumEntries
, code_debug_str(cast(Code, params_code))
);
return OpValResult_Fail;
}
break;
case Op_Increment:
case Op_Decrement:
// If its not set, it just means its a prefix member op.
if ( params_code )
{
if ( params_code->Type != CT_Parameters )
{
log_failure("gen::def_operator: operator %S params code provided is not of Parameters type - %S"
, operator_to_str(op)
, code_debug_str(cast(Code, params_code))
);
return OpValResult_Fail;
}
switch ( params_code->NumEntries )
{
case 1:
if ( code_is_equal((Code)params_code->ValueType, (Code)t_int ) )
is_member_symbol = true;
else
check_param_eq_ret();
break;
case 2:
check_param_eq_ret();
if ( ! code_is_equal((Code)params_get(params_code, 1), (Code)t_int ) )
{
log_failure("gen::def_operator: "
"operator %S requires second parameter of non-member definition to be int for post-decrement",
operator_to_str(op)
);
return OpValResult_Fail;
}
break;
default:
log_failure("gen::def_operator: operator %S recieved unexpected number of parameters recived %d instead of 0-2"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
}
break;
case Op_Unary_Plus:
case Op_Unary_Minus:
if ( ! params_code )
is_member_symbol = true;
else
{
if ( params_code->Type != CT_Parameters )
{
log_failure("gen::def_operator: params is not of Parameters type - %S", code_debug_str((Code)params_code));
return OpValResult_Fail;
}
if ( code_is_equal((Code)params_code->ValueType, (Code)ret_type ) )
{
log_failure("gen::def_operator: "
"operator %S is non-member symbol yet first paramter does not equal return type\n"
"param type: %S\n"
"return type: %S\n"
, code_debug_str((Code)params_code)
, code_debug_str((Code)ret_type)
);
return OpValResult_Fail;
}
if ( params_code->NumEntries > 1 )
{
log_failure("gen::def_operator: operator %S may not have more than one parameter - param count: %d"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
}
break;
case Op_BNot:
{
// Some compilers let you do this...
#if 0
if ( ! ret_type.is_equal( t_bool) )
{
log_failure( "gen::def_operator: operator %S return type is not a boolean - %S", operator_to_str(op) code_debug_str(params_code) );
return OpValidateResult::Fail;
}
#endif
if ( ! params_code )
is_member_symbol = true;
else
{
if ( params_code->Type != CT_Parameters )
{
log_failure( "gen::def_operator: operator %S - params is not of Parameters type - %S", operator_to_str(op), code_debug_str((Code)params_code) );
return OpValResult_Fail;
}
if ( params_code->NumEntries > 1 )
{
log_failure(
"gen::def_operator: operator %S may not have more than one parameter - param count: %d",
operator_to_str( op ),
params_code->NumEntries
);
return OpValResult_Fail;
}
}
break;
}
case Op_Add:
case Op_Subtract:
case Op_Multiply:
case Op_Divide:
case Op_Modulo:
case Op_BAnd:
case Op_BOr:
case Op_BXOr:
case Op_LShift:
case Op_RShift:
check_params();
switch ( params_code->NumEntries )
{
case 1:
is_member_symbol = true;
break;
case 2:
// This is allowed for arithemtic operators
// if ( ! code_is_equal((Code)params_code->ValueType, (Code)ret_type ) )
// {
// log_failure("gen::def_operator: "
// "operator %S is non-member symbol yet first paramter does not equal return type\n"
// "param type: %S\n"
// "return type: %S\n"
// , code_debug_str((Code)params_code)
// , code_debug_str((Code)ret_type)
// );
// return OpValResult_Fail;
// }
break;
default:
log_failure("gen::def_operator: operator %S recieved unexpected number of paramters recived %d instead of 0-2"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
break;
case Op_UnaryNot:
if ( ! params_code )
is_member_symbol = true;
else
{
if ( params_code->Type != CT_Parameters )
{
log_failure("gen::def_operator: operator %S - params is not of Parameters type - %S", operator_to_str(op), code_debug_str((Code)params_code));
return OpValResult_Fail;
}
if ( params_code->NumEntries != 1 )
{
log_failure("gen::def_operator: operator %S recieved unexpected number of paramters recived %d instead of 0-1"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
}
if ( ! code_is_equal((Code)ret_type, (Code)t_bool ))
{
log_failure("gen::def_operator: operator %S return type must be of type bool - %S"
, operator_to_str(op)
, code_debug_str((Code)ret_type)
);
return OpValResult_Fail;
}
break;
case Op_LAnd:
case Op_LOr:
case Op_LEqual:
case Op_LNot:
case Op_Lesser:
case Op_Greater:
case Op_LesserEqual:
case Op_GreaterEqual:
check_params();
switch ( params_code->NumEntries )
{
case 1:
is_member_symbol = true;
break;
case 2:
break;
default:
log_failure("gen::def_operator: operator %S recieved unexpected number of paramters recived %d instead of 1-2"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
break;
case Op_Indirection:
case Op_AddressOf:
case Op_MemberOfPointer:
if ( params_code && params_code->NumEntries > 1)
{
log_failure("gen::def_operator: operator %S recieved unexpected number of paramters recived %d instead of 0-1"
, operator_to_str(op)
, params_code->NumEntries
);
return OpValResult_Fail;
}
else
{
is_member_symbol = true;
}
break;
case Op_PtrToMemOfPtr:
if ( params_code )
{
log_failure("gen::def_operator: operator %S expects no paramters - %S", operator_to_str(op), code_debug_str((Code)params_code));
return OpValResult_Fail;
}
break;
case Op_Subscript:
case Op_FunctionCall:
case Op_Comma:
check_params();
break;
case Op_New:
case Op_Delete:
// This library doesn't support validating new and delete yet.
break;
# undef specs
}
return is_member_symbol ? OpValResult_Member : OpValResult_Global;
# undef check_params
# undef check_ret_type
# undef check_param_eq_ret
}
forceinline
bool name__check( char const* context, Str name )
{
if ( name.Len <= 0 ) {
log_failure( "gen::%s: Invalid name length provided - %d", name.Len );
return false;
}
if ( name.Ptr == nullptr ) {
log_failure( "gen::%s: name is null" );
return false;
}
return true;
}
#define name_check( context, name ) name__check( #context, name )
forceinline
bool null__check( char const* context, char const* code_id, Code code ) {
if ( code == nullptr ) {
log_failure( "gen::%s: %s provided is null", context, code_id );
return false;
}
return true;
}
#define null_check( context, code ) null__check( #context, #code, cast(Code, code) )
/*
The implementation of the upfront constructors involves doing three things:
* Validate the arguments given to construct the intended type of AST is valid.
* Construct said AST type.
* Lock the AST (set to readonly) and return the valid object.
If any of the validation fails, it triggers a call to log_failure with as much info the give the user so that they can hopefully
identify the issue without having to debug too much (at least they can debug though...)
The largest of the functions is related to operator overload definitions.
The library validates a good protion of their form and thus the argument processing for is quite a bit.
*/
CodeAttributes def_attributes( Str content )
{
if ( content.Len <= 0 || content.Ptr == nullptr ) {
log_failure( "gen::def_attributes: Invalid attributes provided" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
Code
result = make_code();
result->Type = CT_PlatformAttributes;
result->Name = cache_str( content );
result->Content = result->Name;
return (CodeAttributes) result;
}
CodeComment def_comment( Str content )
{
if ( content.Len <= 0 || content.Ptr == nullptr )
{
log_failure( "gen::def_comment: Invalid comment provided:" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
StrBuilder cmt_formatted = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
char const* end = content.Ptr + content.Len;
char const* scanner = content.Ptr;
s32 curr = 0;
do
{
char const* next = scanner;
s32 length = 0;
while ( next != end && scanner[ length ] != '\n' )
{
next = scanner + length;
length++;
}
length++;
strbuilder_append_fmt(& cmt_formatted, "//%.*s", length, scanner );
scanner += length;
}
while ( scanner <= end );
if ( * strbuilder_back(cmt_formatted) != '\n' )
strbuilder_append_str( & cmt_formatted, txt("\n") );
Str name = strbuilder_to_str(cmt_formatted);
Code
result = make_code();
result->Type = CT_Comment;
result->Name = cache_str( name );
result->Content = result->Name;
strbuilder_free(& cmt_formatted);
return (CodeComment) result;
}
CodeConstructor def_constructor( Opts_def_constructor p )
{
if ( p.params && p.params->Type != CT_Parameters ) {
log_failure("gen::def_constructor: params must be of Parameters type - %s", code_debug_str((Code)p.params));
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeConstructor result = (CodeConstructor) make_code();
if ( p.params ) {
result->Params = p.params;
}
if ( p.initializer_list ) {
result->InitializerList = p.initializer_list;
}
if ( p.body )
{
switch ( p.body->Type ) {
case CT_Function_Body:
case CT_Untyped:
break;
default:
log_failure("gen::def_constructor: body must be either of Function_Body or Untyped type - %s", code_debug_str(p.body));
return InvalidCode;
}
result->Type = CT_Constructor;
result->Body = p.body;
}
else
{
result->Type = CT_Constructor_Fwd;
}
return result;
}
CodeClass def_class( Str name, Opts_def_struct p )
{
if ( ! name_check( def_class, name ) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", code_debug_str(p.attributes) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.parent && ( p.parent->Type != CT_Class && p.parent->Type != CT_Struct && p.parent->Type != CT_Typename && p.parent->Type != CT_Untyped ) ) {
log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", code_debug_str(p.parent) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeClass
result = (CodeClass) make_code();
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
result->Attributes = p.attributes;
result->ParentAccess = p.parent_access;
result->ParentType = p.parent;
if ( p.body )
{
switch ( p.body->Type )
{
case CT_Class_Body:
case CT_Untyped:
break;
default:
log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", code_debug_str(p.body));
return InvalidCode;
}
result->Type = CT_Class;
result->Body = p.body;
result->Body->Parent = cast(Code, result);
}
else {
result->Type = CT_Class_Fwd;
}
for (s32 idx = 0; idx < p.num_interfaces; idx++ ) {
class_add_interface(result, p.interfaces[idx] );
}
return result;
}
CodeDefine def_define( Str name, MacroType type, Opts_def_define p )
{
if ( ! name_check( def_define, name ) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeDefine
result = (CodeDefine) make_code();
result->Type = CT_Preprocess_Define;
result->Name = cache_str( name );
result->Params = p.params;
if ( p.content.Len <= 0 || p.content.Ptr == nullptr )
result->Body = untyped_str( txt("\n") );
else
result->Body = untyped_str( strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S\n", p.content)) );
b32 register_define = ! p.dont_register_to_preprocess_macros;
if ( register_define ) {
Macro macro_entry = { result->Name, type, p.flags };
register_macro(macro_entry);
}
return result;
}
CodeDestructor def_destructor( Opts_def_destructor p )
{
if ( p.specifiers && p.specifiers->Type != CT_Specifiers ) {
log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", code_debug_str(p.specifiers) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeDestructor
result = (CodeDestructor) make_code();
result->Specs = p.specifiers;
if ( p.body )
{
switch ( p.body->Type )
{
case CT_Function_Body:
case CT_Untyped:
break;
default:
log_failure("gen::def_destructor: body must be either of Function_Body or Untyped type - %s", code_debug_str(p.body));
return InvalidCode;
}
result->Type = CT_Destructor;
result->Body = p.body;
}
else
{
result->Type = CT_Destructor_Fwd;
}
return result;
}
CodeEnum def_enum( Str name, Opts_def_enum p )
{
if ( ! name_check( def_enum, name ) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.type && p.type->Type != CT_Typename ) {
log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", code_debug_str(p.type) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", code_debug_str(p.attributes) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeEnum
result = (CodeEnum) make_code();
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
if ( p.body )
{
switch ( p.body->Type )
{
case CT_Enum_Body:
case CT_Untyped:
break;
default:
log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", code_debug_str(p.body));
return InvalidCode;
}
result->Type = p.specifier == EnumDecl_Class ?
CT_Enum_Class : CT_Enum;
result->Body = p.body;
}
else
{
result->Type = p.specifier == EnumDecl_Class ?
CT_Enum_Class_Fwd : CT_Enum_Fwd;
}
result->Attributes = p.attributes;
if ( p.type ) {
result->UnderlyingType = p.type;
}
else if ( p.type_macro ) {
result->UnderlyingTypeMacro = p.type_macro;
}
else if ( result->Type != CT_Enum_Class_Fwd && result->Type != CT_Enum_Fwd )
{
log_failure( "gen::def_enum: enum forward declaration must have an underlying type" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
return result;
}
CodeExec def_execution( Str content )
{
if ( content.Len <= 0 || content.Ptr == nullptr ) {
log_failure( "gen::def_execution: Invalid execution provided" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeExec
result = (CodeExec) make_code();
result->Type = CT_Execution;
result->Content = cache_str( content );
return result;
}
CodeExtern def_extern_link( Str name, CodeBody body )
{
if ( ! name_check(def_extern_link, name) || ! null_check(def_extern_link, body) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( body->Type != CT_Extern_Linkage_Body && body->Type != CT_Untyped ) {
log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", code_debug_str(body));
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeExtern
result = (CodeExtern)make_code();
result->Type = CT_Extern_Linkage;
result->Name = cache_str( name );
result->Body = body;
return result;
}
CodeFriend def_friend( Code declaration )
{
if ( ! null_check( def_friend, declaration ) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
switch ( declaration->Type )
{
case CT_Class_Fwd:
case CT_Function_Fwd:
case CT_Operator_Fwd:
case CT_Struct_Fwd:
case CT_Class:
case CT_Function:
case CT_Operator:
case CT_Struct:
break;
default:
log_failure("gen::def_friend: requires declartion to have class, function, operator, or struct - %s", code_debug_str(declaration));
return InvalidCode;
}
CodeFriend
result = (CodeFriend) make_code();
result->Type = CT_Friend;
result->Declaration = declaration;
return result;
}
CodeFn def_function( Str name, Opts_def_function p )
{
if ( ! name_check( def_function, name )) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.params && p.params->Type != CT_Parameters ) {
log_failure( "gen::def_function: params was not a `Parameters` type: %s", code_debug_str(p.params) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.ret_type && p.ret_type->Type != CT_Typename ) {
log_failure( "gen::def_function: ret_type was not a Typename: %s", code_debug_str(p.ret_type) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.specs && p.specs-> Type != CT_Specifiers ) {
log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", code_debug_str(p.specs) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.attrs && p.attrs->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", code_debug_str(p.attrs) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeFn
result = (CodeFn) make_code();
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
if ( p.body )
{
switch ( p.body->Type )
{
case CT_Function_Body:
case CT_Execution:
case CT_Untyped:
break;
default:
{
log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", code_debug_str(p.body));
return InvalidCode;
}
}
result->Type = CT_Function;
result->Body = p.body;
}
else
{
result->Type = CT_Function_Fwd;
}
result->Attributes = p.attrs;
result->Specs = p.specs;
result->Params = p.params;
result->ReturnType = p.ret_type ? p.ret_type : t_void;
return result;
}
CodeInclude def_include( Str path, Opts_def_include p )
{
if ( path.Len <= 0 || path.Ptr == nullptr ) {
log_failure( "gen::def_include: Invalid path provided - %d" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
StrBuilder content = p.foreign ?
strbuilder_fmt_buf( _ctx->Allocator_Temp, "<%.*s>", path.Len, path.Ptr )
: strbuilder_fmt_buf( _ctx->Allocator_Temp, "\"%.*s\"", path.Len, path.Ptr );
CodeInclude
result = (CodeInclude) make_code();
result->Type = CT_Preprocess_Include;
result->Name = cache_str( strbuilder_to_str(content) );
result->Content = result->Name;
return result;
}
CodeModule def_module( Str name, Opts_def_module p )
{
if ( ! name_check( def_module, name )) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeModule
result = (CodeModule) make_code();
result->Type = CT_Module;
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
return result;
}
CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace p )
{
if ( ! name_check( def_namespace, name )) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( ! null_check( def_namespace, body)) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( body && body->Type != CT_Namespace_Body && body->Type != CT_Untyped ) {
log_failure("gen::def_namespace: body is not of namespace or untyped type %s", code_debug_str(body));
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeNS
result = (CodeNS) make_code();
result->Type = CT_Namespace;
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
result->Body = body;
return result;
}
CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator p )
{
if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", code_debug_str(p.attributes) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( p.specifiers && p.specifiers->Type != CT_Specifiers ) {
log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", code_debug_str(p.specifiers) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
OpValidateResult check_result = operator__validate( op, p.params, p.ret_type, p.specifiers );
if ( check_result == OpValResult_Fail ) {
return InvalidCode;
}
char const* name = nullptr;
Str op_str = operator_to_str( op );
if ( nspace.Len > 0 )
name = c_str_fmt_buf( "%.*soperator %.*s", nspace.Len, nspace.Ptr, op_str.Len, op_str.Ptr );
else
name = c_str_fmt_buf( "operator %.*s", op_str.Len, op_str.Ptr );
Str name_resolved = { name, c_str_len(name) };
CodeOperator
result = (CodeOperator) make_code();
result->Name = cache_str( name_resolved );
result->ModuleFlags = p.mflags;
result->Op = op;
if ( p.body )
{
switch ( p.body->Type )
{
case CT_Function_Body:
case CT_Execution:
case CT_Untyped:
break;
default:
{
log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", code_debug_str(p.body));
GEN_DEBUG_TRAP();
return InvalidCode;
}
}
result->Type = check_result == OpValResult_Global ?
CT_Operator : CT_Operator_Member;
result->Body = p.body;
}
else
{
result->Type = check_result == OpValResult_Global ?
CT_Operator_Fwd : CT_Operator_Member_Fwd;
}
result->Attributes = p.attributes;
result->Specs = p.specifiers;
result->ReturnType = p.ret_type;
result->Params = p.params;
return result;
}
CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast p )
{
if ( ! null_check( def_operator_cast, type )) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( type->Type != CT_Typename ) {
log_failure( "gen::def_operator_cast: type is not a typename - %s", code_debug_str(type) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodeOpCast result = (CodeOpCast) make_code();
if (p.body)
{
result->Type = CT_Operator_Cast;
if ( p.body->Type != CT_Function_Body && p.body->Type != CT_Execution ) {
log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", code_debug_str(p.body) );
GEN_DEBUG_TRAP();
return InvalidCode;
}
result->Body = p.body;
}
else
{
result->Type = CT_Operator_Cast_Fwd;
}
result->Specs = p.specs;
result->ValueType = type;
return result;
}
CodeParams def_param( CodeTypename type, Str name, Opts_def_param p )
{
if ( ! name_check( def_param, name ) || ! null_check( def_param, type ) ) {
GEN_DEBUG_TRAP();
return InvalidCode;
}
if ( type->Type != CT_Typename ) {
log_failure( "gen::def_param: type is not a typename - %s", code_debug_str(type) );
return InvalidCode;
}
if ( p.value && p.value->Type != CT_Untyped ) {
log_failure( "gen::def_param: value is not untyped - %s", code_debug_str(p.value) );
return InvalidCode;
}
CodeParams
result = (CodeParams) make_code();
result->Type = CT_Parameters;
result->Name = cache_str( name );
result->ValueType = type;
result->Value = p.value;
result->NumEntries++;
return result;
}
CodePragma def_pragma( Str directive )
{
if ( directive.Len <= 0 || directive.Ptr == nullptr ) {
log_failure( "gen::def_comment: Invalid comment provided:" );
GEN_DEBUG_TRAP();
return InvalidCode;
}
CodePragma
result = (CodePragma) make_code();
result->Type = CT_Preprocess_Pragma;
result->Content = cache_str( directive );
return result;
}
CodePreprocessCond def_preprocess_cond( EPreprocessCond type, Str expr )
{