-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
2037 lines (1989 loc) · 111 KB
/
parser.c
File metadata and controls
2037 lines (1989 loc) · 111 KB
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 "dynamics.h"
#include "jitc.h"
#include "jitc_internal.h"
#include "cleanups.h"
#include "compares.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <dlfcn.h>
#define NEXT_TOKEN (&queue_peek(tokens))
#define throw_impl(...) jitc_error_set(context, jitc_error_parser(__VA_ARGS__))
// passed as "min_prec" into jitc_parse_expression
// commas have precedence of 1
#define EXPR_NO_COMMAS 2
#define EXPR_WITH_COMMAS 1
typedef enum {
Spec_Int = (1 << 0),
Spec_Short = (1 << 1),
Spec_Long1 = (1 << 2),
Spec_Long2 = (1 << 3),
Spec_Char = (1 << 4),
Spec_Void = (1 << 5),
Spec_Float = (1 << 6),
Spec_Double = (1 << 7),
} jitc_specifiers_t;
static struct {
jitc_type_kind_t type;
jitc_specifiers_t specifiers;
} types[] = {
{ Type_Void, Spec_Void },
{ Type_Int8, Spec_Char },
{ Type_Int16, Spec_Short | Spec_Int },
{ Type_Int16, Spec_Short },
{ Type_Int32, Spec_Int },
{ Type_Int64, Spec_Long1 },
{ Type_Int64, Spec_Long1 | Spec_Int },
{ Type_Int64, Spec_Long1 | Spec_Long2 },
{ Type_Int64, Spec_Long1 | Spec_Long2 | Spec_Int },
{ Type_Float32, Spec_Float },
{ Type_Float64, Spec_Double },
{ Type_Float64, Spec_Long1 | Spec_Double },
};
static jitc_ast_t* root_node = NULL;
static jitc_ast_t* func_body_node = NULL;
static jitc_ast_t* mknode(jitc_ast_type_t type, jitc_token_t* token) {
jitc_ast_t* ast = calloc(sizeof(jitc_ast_t), 1);
ast->node_type = type;
ast->token = token;
if (type == AST_List || type == AST_Scope) ast->list.inner = list_new(jitc_ast_t*);
return ast;
}
static bool is_integer(jitc_type_t* type) {
return type->kind == Type_Int8 || type->kind == Type_Int16 || type->kind == Type_Int32 || type->kind == Type_Int64 || type->kind == Type_Enum;
}
static bool is_floating(jitc_type_t* type) {
return type->kind == Type_Float32 || type->kind == Type_Float64;
}
static bool is_pointer(jitc_type_t* type) {
return type->kind == Type_Pointer || type->kind == Type_Array;
}
static bool is_number(jitc_type_t* type) {
return is_integer(type) || is_floating(type);
}
static bool is_scalar(jitc_type_t* type) {
return is_number(type) || is_pointer(type);
}
static bool is_struct(jitc_type_t* type) {
return type->kind == Type_Struct || type->kind == Type_Union;
}
static bool is_function(jitc_type_t* type) {
return type->kind == Type_Pointer && (type->ptr.prev == Type_Function || type->ptr.base->kind == Type_Function);
}
static bool is_decayed_pointer(jitc_type_t* type) {
return type->kind == Type_Pointer && type->ptr.prev != Type_Pointer;
}
static bool is_constant(jitc_ast_t* ast) {
return ast->node_type == AST_Integer || ast->node_type == AST_Floating || ast->node_type == AST_StringLit;
}
static bool is_lvalue(jitc_ast_t* ast) {
return ast->node_type == AST_Variable || (ast->node_type == AST_Unary && ast->unary.operation == Unary_Dereference) || ast->node_type == AST_WalkStruct || ast->node_type == AST_Initializer;
}
bool jitc_peek_type(jitc_context_t* context, queue_t* _tokens) {
queue(jitc_token_t)* tokens = _tokens;
switch (NEXT_TOKEN->type) {
case TOKEN_extern:
case TOKEN_static:
case TOKEN_typedef:
case TOKEN_const:
case TOKEN_unsigned:
case TOKEN_char:
case TOKEN_short:
case TOKEN_int:
case TOKEN_float:
case TOKEN_double:
case TOKEN_void:
case TOKEN_long:
case TOKEN_bool:
case TOKEN_volatile:
case TOKEN_register:
case TOKEN_restrict:
case TOKEN_inline:
case TOKEN_typeof:
case TOKEN_struct:
case TOKEN_union:
case TOKEN_enum:
case TOKEN_preserve:
case TOKEN_hotswap:
return true;
case TOKEN_IDENTIFIER: {
jitc_variable_t* var = jitc_get_variable(context, NEXT_TOKEN->value.string);
if (var == NULL) return false;
return var->decltype == Decltype_Typedef;
}
default: return false;
}
}
bool jitc_parse_type_declarations(jitc_context_t* context, queue_t* _tokens, jitc_type_t** type) {
queue(jitc_token_t)* tokens = _tokens;
typedef struct {
enum {
DeclID_InnerDeclaration,
DeclID_Function,
DeclID_Array
} id;
jitc_token_t* starting_token;
union {
size_t array_size;
queue(jitc_token_t)* tokens;
};
} declstack_t;
if (!*type) return NULL;
if (queue_size(tokens) == 1) return true;
bool lhs_flag = true;
jitc_token_t* token = NULL;
smartptr(stack(declstack_t)) declstack = stack_new(declstack_t);
const char* name = NULL;
while (true) {
if ((token = jitc_token_expect(tokens, TOKEN_ASTERISK))) {
if (!lhs_flag) throw(token, "Pointer declaration on right-hand-side of type");
*type = jitc_typecache_pointer(context, *type);
}
else if (jitc_token_expect(tokens, TOKEN_const)) {
if (!lhs_flag) throw(token, "'const' declaration on right-hand-side of type");
*type = jitc_typecache_const(context, *type);
}
else if ((token = jitc_token_expect(tokens, TOKEN_IDENTIFIER))) {
name = token->value.string;
lhs_flag = false;
}
else if ((token = jitc_token_expect(tokens, TOKEN_BRACKET_OPEN))) {
lhs_flag = false;
size_t size = -1;
jitc_token_t* starting_token = token;
if (!jitc_token_expect(tokens, TOKEN_BRACKET_CLOSE)) {
token = NEXT_TOKEN;
smartptr(jitc_ast_t) ast = try(jitc_parse_expression(context, tokens, EXPR_NO_COMMAS, NULL));
if (ast->node_type != AST_Integer) throw(token, "Expected integer constant");
size = ast->integer.value;
if (!jitc_token_expect(tokens, TOKEN_BRACKET_CLOSE)) throw(NEXT_TOKEN, "Expected ']'");
}
stack_push(declstack) = (declstack_t){
.id = DeclID_Array,
.starting_token = starting_token,
.array_size = size
};
}
else if ((token = jitc_token_expect(tokens, TOKEN_PARENTHESIS_OPEN))) {
int depth = 0;
jitc_token_t* starting_token = token;
smartptr(queue(jitc_token_t)) func = queue_new(jitc_token_t);
while (true) {
token = &queue_pop(tokens);
queue_push(func) = *token;
if (token->type == TOKEN_END_OF_FILE) throw(token, "Unexpected EOF");
if (token->type == TOKEN_PARENTHESIS_OPEN) depth++;
if (token->type == TOKEN_PARENTHESIS_CLOSE) {
depth--;
if (depth < 0) break;
}
}
stack_push(declstack) = (declstack_t){
.id = lhs_flag ? DeclID_InnerDeclaration : DeclID_Function,
.starting_token = starting_token,
.tokens = (void*)move(func)
};
lhs_flag = false;
}
else break;
}
while (stack_size(declstack) > 0) {
declstack_t* item = &stack_pop(declstack);
switch (item->id) {
case DeclID_Array: {
if (!jitc_validate_type(*type, TypePolicy_NoFunction)) throw(item->starting_token, "Array cannot contain a function");
if (!jitc_validate_type(*type, TypePolicy_NoUndefTags)) *type = jitc_get_tagged_type(context, *type) ?: *type;
if (!jitc_validate_type(*type, TypePolicy_NoIncomplete)) throw(item->starting_token, "Array with incomplete type");
*type = jitc_typecache_array(context, *type, item->array_size);
} break;
case DeclID_Function: {
jitc_token_t* comma = NULL;
smartptr(list(jitc_type_t*)) list = list_new(jitc_type_t*);
smartptr(queue(jitc_token_t)) queue = (void*)item->tokens;
if (!jitc_validate_type(*type, TypePolicy_NoDerived)) throw(item->starting_token, "Function cannot return an array or function");
if (!jitc_validate_type(*type, TypePolicy_NoUndefTags)) *type = jitc_get_tagged_type(context, *type) ?: *type;
if (!jitc_validate_type(*type, TypePolicy_NoIncomplete & ~TypePolicy_NoVoid)) throw(item->starting_token, "Function returns an incomplete type");
while (queue_size(queue) > 1) {
comma = NULL;
token = &queue_peek(queue);
if (token->type == TOKEN_TRIPLE_DOT) {
queue_pop(queue);
list_add(list) = jitc_typecache_primitive(context, Type_Varargs);
if (!jitc_token_expect(queue, TOKEN_PARENTHESIS_CLOSE)) throw(&queue_pop(queue), "Expected ')'");
break;
}
jitc_type_t* param_type = jitc_typecache_decay(context, try(jitc_parse_type(context, queue, NULL, NULL)));
if (param_type->kind == Type_Void) {
if (param_type->name) throw(token, "'void' cannot have a name");
if (list_size(list) > 0) throw(token, "'void' must be the only parameter");
break;
}
if (!jitc_validate_type(param_type, TypePolicy_NoUndefTags)) param_type = jitc_get_tagged_type(context, param_type) ?: param_type;
if (!jitc_validate_type(param_type, TypePolicy_NoIncomplete)) throw(token, "Function parameter '%s' has incomplete type", param_type->name);
list_add(list) = param_type;
comma = jitc_token_expect(queue, TOKEN_COMMA);
}
if (queue_size(queue) > 1) throw(&queue_peek(queue), "Expected ')'");
if (comma) throw(comma, "Expected type");
*type = jitc_typecache_function(context, *type, list);
} break;
case DeclID_InnerDeclaration: {
smartptr(queue(jitc_token_t)) inner_tokens = (void*)item->tokens;
if (queue_size(inner_tokens) == 1) throw(&queue_pop(inner_tokens), "Unexpected ')'");
if (!jitc_parse_type_declarations(context, inner_tokens, type)) return false;
} break;
}
}
if (name) *type = jitc_typecache_named(context, *type, name);
return true;
}
jitc_type_t* jitc_parse_base_type(jitc_context_t* context, queue_t* _tokens, jitc_decltype_t* decltype, const char** extern_symbol, jitc_preserve_t* preserve_policy) {
queue(jitc_token_t)* tokens = _tokens;
bool is_const = false, is_unsigned = false;
jitc_specifiers_t specs = 0;
jitc_token_t* token = NULL;
jitc_token_t* unsigned_token = NULL, *first_token = NULL;
jitc_type_t* type = NULL;
size_t align = -1;
bool had_identifier = false;
while (true) {
jitc_decltype_t decl = Decltype_None;
jitc_preserve_t prsv = Preserve_IfConst;
jitc_specifiers_t new_specs = 0;
if ((token = jitc_token_expect(tokens, TOKEN_extern))) {
decl = Decltype_Extern;
if (jitc_token_expect(tokens, TOKEN_PARENTHESIS_OPEN)) {
jitc_token_t* str_token = jitc_token_expect(tokens, TOKEN_STRING);
if (!str_token) throw(NEXT_TOKEN, "Expected string literal");
if (!jitc_token_expect(tokens, TOKEN_PARENTHESIS_CLOSE)) throw(NEXT_TOKEN, "Expected ')'");
if (extern_symbol) *extern_symbol = str_token->value.string;
}
}
else if ((token = jitc_token_expect(tokens, TOKEN_static))) decl = Decltype_Static;
else if ((token = jitc_token_expect(tokens, TOKEN_typedef))) decl = Decltype_Typedef;
else if ((token = jitc_token_expect(tokens, TOKEN_preserve))) prsv = Preserve_Always;
else if ((token = jitc_token_expect(tokens, TOKEN_hotswap))) prsv = Preserve_Never;
else if ((token = jitc_token_expect(tokens, TOKEN_const))) is_const = true;
else if ((token = jitc_token_expect(tokens, TOKEN_unsigned))) is_unsigned = true;
else if ((token = jitc_token_expect(tokens, TOKEN_char))) new_specs |= Spec_Char;
else if ((token = jitc_token_expect(tokens, TOKEN_short))) new_specs |= Spec_Short;
else if ((token = jitc_token_expect(tokens, TOKEN_int))) new_specs |= Spec_Int;
else if ((token = jitc_token_expect(tokens, TOKEN_float))) new_specs |= Spec_Float;
else if ((token = jitc_token_expect(tokens, TOKEN_double))) new_specs |= Spec_Double;
else if ((token = jitc_token_expect(tokens, TOKEN_void))) new_specs |= Spec_Void;
else if ((token = jitc_token_expect(tokens, TOKEN_long))) new_specs |= specs & Spec_Long1 ? Spec_Long2 : Spec_Long1;
else if (
jitc_token_expect(tokens, TOKEN_volatile) ||
jitc_token_expect(tokens, TOKEN_register) ||
jitc_token_expect(tokens, TOKEN_restrict) ||
jitc_token_expect(tokens, TOKEN_inline)
) (void)0; // no-op, maybe implement later?
else if ((token = jitc_token_expect(tokens, TOKEN_bool))) {
new_specs |= Spec_Char;
is_unsigned = true;
}
else if ((token = NEXT_TOKEN)->type == TOKEN_IDENTIFIER && !had_identifier) {
had_identifier = true;
jitc_variable_t* variable = jitc_get_variable(context, token->value.string);
if (specs != 0 || !variable || variable->decltype != Decltype_Typedef) {
if (first_token) break;
throw(token, "Undefined type '%s'", token->value.string);
}
type = jitc_typecache_named(context, variable->type, NULL);
queue_pop(tokens);
}
else if ((token = jitc_token_expect(tokens, TOKEN_typeof))) {
if (!jitc_token_expect(tokens, TOKEN_PARENTHESIS_OPEN)) throw(NEXT_TOKEN, "Expected '('");
if (jitc_peek_type(context, tokens)) type = try(jitc_parse_type(context, tokens, NULL, NULL));
else jitc_destroy_ast(try(jitc_parse_expression(context, tokens, EXPR_WITH_COMMAS, &type)));
if (!jitc_token_expect(tokens, TOKEN_PARENTHESIS_CLOSE)) throw(NEXT_TOKEN, "Expected ')'");
}
else if ((token = jitc_token_expect(tokens, TOKEN_struct)) || (token = jitc_token_expect(tokens, TOKEN_union))) {
smartptr(list(char*)) template_names = NULL;
jitc_token_t* template_start = NULL;
if ((template_start = jitc_token_expect(tokens, TOKEN_LESS_THAN))) {
template_names = list_new(char*);
if (!jitc_token_expect(tokens, TOKEN_GREATER_THAN)) while (true) {
jitc_token_t* template_name = jitc_token_expect(tokens, TOKEN_IDENTIFIER);
if (!template_name) throw(NEXT_TOKEN, "Expected identifier");
for (int i = 0; i < list_size(template_names); i++) {
if (strcmp(list_get(template_names, i), template_name->value.string) == 0)
throw(template_name, "Duplicate template parameter");
}
list_add(template_names) = template_name->value.string;
if (jitc_token_expect(tokens, TOKEN_COMMA)) continue;
if (jitc_token_expect(tokens, TOKEN_GREATER_THAN)) break;
throw(NEXT_TOKEN, "Expected ',' or '>'");
}
}
jitc_token_t* name_token = jitc_token_expect(tokens, TOKEN_IDENTIFIER);
if (jitc_token_expect(tokens, TOKEN_BRACE_OPEN)) {
smartptr(list(jitc_type_t*)) fields = list_new(jitc_type_t*);
jitc_push_scope(context);
if (template_names) for (int i = 0; i < list_size(template_names); i++) {
jitc_type_t* placeholder = jitc_typecache_placeholder(context, list_get(template_names, i));
placeholder = jitc_typecache_named(context, placeholder, list_get(template_names, i));
jitc_declare_variable(context, placeholder, Decltype_Typedef, NULL, 0, 0);
}
while (!jitc_token_expect(tokens, TOKEN_BRACE_CLOSE)) {
while (NEXT_TOKEN->type == TOKEN_SEMICOLON) queue_pop(tokens);
jitc_type_t* field_type = try(jitc_parse_base_type(context, tokens, NULL, NULL, NULL));
while (true) {
field_type = jitc_typecache_named(context, field_type, NULL);
try(jitc_parse_type_declarations(context, tokens, &field_type));
const char* field_name = field_type->name;
if (!jitc_validate_type(field_type, TypePolicy_NoUndefTags)) {
jitc_type_t* resolved = jitc_get_tagged_type(context, field_type);
if (!resolved) throw(NEXT_TOKEN, "Cannot declare an incomplete struct");
field_type = jitc_typecache_named(context, resolved, field_name);
}
if (!jitc_validate_type(field_type, TypePolicy_NoIncomplete)) throw(NEXT_TOKEN, "Field '%s' has incomplete type", field_type->name);
if (field_type->name) if (jitc_struct_field_exists_list(fields, field_type->name))
throw(NEXT_TOKEN, "Duplicate field '%s'", field_type->name);
list_add(fields) = field_type;
if (jitc_token_expect(tokens, TOKEN_COMMA)) continue;
if (jitc_token_expect(tokens, TOKEN_SEMICOLON)) break;
throw(NEXT_TOKEN, "Expected ';' or ','");
}
}
type = (token->type == TOKEN_struct ? jitc_typecache_struct : jitc_typecache_union)(context, fields, token);
if (template_names) type = jitc_typecache_template(context, type, template_names, template_start);
if (name_token) {
const char* name = token->type == TOKEN_struct ? "Struct" : "Union";
jitc_pop_scope(context);
jitc_declare_tagged_type(context, type, name_token->value.string);
}
else jitc_pop_scope(context);
}
else if (!name_token) throw(NEXT_TOKEN, "Expected identifier or '{'");
else if (template_names) throw(NEXT_TOKEN, "Expected '{'");
else {
type = jitc_get_tagged_type_notype(context, token->type ? Type_Struct : Type_Union, name_token->value.string);
smartptr(map(char*, jitc_type_t*)) template_map = NULL;
smartptr(list(jitc_type_t*)) template_list = NULL;
jitc_token_t* template_start = NULL;
if ((template_start = jitc_token_expect(tokens, TOKEN_LESS_THAN))) {
if (type && type->kind != Type_Template) throw(template_start, "Unexpected template parameter list");
template_list = list_new(jitc_type_t*);
if (!jitc_token_expect(tokens, TOKEN_GREATER_THAN)) while (true) {
list_add(template_list) = try(jitc_parse_type(context, tokens, NULL, NULL));
if (jitc_token_expect(tokens, TOKEN_COMMA)) continue;
if (jitc_token_expect(tokens, TOKEN_GREATER_THAN)) break;
throw(NEXT_TOKEN, "Expected ',' or '>'");
}
if (type) {
if (list_size(template_list) != type->templ.num_names)
throw(template_start, "Expected %d types in template parameter list, got %d", type->templ.num_names, list_size(template_map));
template_map = map_new(compare_string, char*, jitc_type_t*);
for (int i = 0; i < type->templ.num_names; i++) {
map_add(template_map) = (char*)type->templ.names[i];
map_commit(template_map);
map_get_value(template_map) = list_get(template_list, i);
}
type = jitc_typecache_fill_template(context, type, template_map);
}
}
if (!type) type = (token->type == TOKEN_struct ? jitc_typecache_structref : jitc_typecache_unionref)(context, name_token->value.string, template_list);
}
}
else if ((token = jitc_token_expect(tokens, TOKEN_enum))) {
jitc_token_t* name_token = jitc_token_expect(tokens, TOKEN_IDENTIFIER);
jitc_type_kind_t kind = Type_Int32;
bool is_unsigned = false;
bool force_definition = false;
if (jitc_token_expect(tokens, TOKEN_COLON)) {
force_definition = true;
jitc_token_t* type_token = NEXT_TOKEN;
jitc_type_t* type = try(jitc_parse_base_type(context, tokens, NULL, NULL, NULL));
if (!is_integer(type)) throw(type_token, "Enum base type must be an integer");
kind = type->kind;
}
jitc_type_t* value_type = jitc_typecache_primitive(context, kind);
if (is_unsigned) value_type = jitc_typecache_unsigned(context, value_type);
if (jitc_token_expect(tokens, TOKEN_BRACE_OPEN)) {
uint64_t prev_value = -1;
while (!jitc_token_expect(tokens, TOKEN_BRACE_CLOSE)) {
jitc_token_t* id = NULL;
if (!(id = jitc_token_expect(tokens, TOKEN_IDENTIFIER))) throw(NEXT_TOKEN, "Expected identifier");
const char* name = id->value.string;
uint64_t value = prev_value + 1;
if ((jitc_token_expect(tokens, TOKEN_EQUALS))) {
jitc_ast_t* ast = try(jitc_parse_expression(context, tokens, EXPR_NO_COMMAS, NULL));
if (ast->node_type != AST_Integer) throw(ast->token, "Value must be an integer");
value = ast->integer.value;
}
prev_value = value;
if (!jitc_declare_variable(context, jitc_typecache_named(context, value_type, name), Decltype_EnumItem, NULL, Preserve_IfConst, value))
throw(id, "Symbol '%s' already defined", name);
if (jitc_token_expect(tokens, TOKEN_COMMA)) continue;
if (jitc_token_expect(tokens, TOKEN_BRACE_CLOSE)) break;
throw(NEXT_TOKEN, "Expected ',' or '}'");
}
type = jitc_typecache_enum(context, value_type);
if (name_token) jitc_declare_tagged_type(context, type, name_token->value.string);
}
else {
if (force_definition) throw(NEXT_TOKEN, "Expected '{'");
if (!name_token) throw(NEXT_TOKEN, "Expected identifier or '{'");
type = jitc_get_tagged_type_notype(context, Type_Enum, name_token->value.string);
if (!type) type = jitc_typecache_enumref(context, name_token->value.string);
else type = type->ptr.base;
}
}
else {
if (!first_token) throw(NEXT_TOKEN, "Expected type");
break;
}
if ((specs & new_specs) != 0) throw(token, "Duplicate specifier");
specs |= new_specs;
if (decl != Decltype_None) {
if (!decltype) throw(token, "Declaration type illegal here");
if (*decltype != Decltype_None) throw(token, "Duplicate declaration type");
*decltype = decl;
}
if (prsv != Preserve_IfConst) {
if (!preserve_policy) throw(token, "Preservation policy illegal here");
if (*preserve_policy != Preserve_IfConst) throw(token, "Duplicate preservation policy");
*preserve_policy = prsv;
}
if (token->type == TOKEN_unsigned && !unsigned_token) unsigned_token = token;
if (!first_token) first_token = token;
}
if (!type) {
int i = 0;
jitc_type_kind_t kind;
if (specs == 0 && is_unsigned) specs |= Spec_Int;
for (; i < sizeof(types) / sizeof(*types); i++) {
if (types[i].specifiers == specs) {
kind = types[i].type;
break;
}
}
if (i == sizeof(types) / sizeof(*types)) throw(first_token, "Invalid specifier combination");
if (is_unsigned && !(
kind == Type_Int8 || kind == Type_Int16 ||
kind == Type_Int32 || kind == Type_Int64
)) throw(unsigned_token, "Unsigned non-integer type");
type = jitc_typecache_primitive(context, kind);
}
if (type->kind == Type_Template) {
if (jitc_token_expect(tokens, TOKEN_LESS_THAN)) {
smartptr(map(char*, jitc_type_t*)) template_map = map_new(compare_string, char*, jitc_type_t*);
for (int i = 0; i < type->templ.num_names; i++) {
if (i != 0 && !jitc_token_expect(tokens, TOKEN_COMMA)) throw(NEXT_TOKEN, "Expected ','");
map_add(template_map) = (char*)type->templ.names[i];
map_commit(template_map);
map_get_value(template_map) = try(jitc_parse_type(context, tokens, NULL, NULL));
}
if (!jitc_token_expect(tokens, TOKEN_GREATER_THAN)) throw(NEXT_TOKEN, "Expected '>'");
type = jitc_typecache_fill_template(context, type, template_map);
}
else if (((decltype && *decltype != Decltype_Typedef) || !decltype) && NEXT_TOKEN->type != TOKEN_SEMICOLON) throw(NEXT_TOKEN, "Expected '<'");
}
if (is_const) type = jitc_typecache_const(context, type);
if (is_unsigned) type = jitc_typecache_unsigned(context, type);
if (align != -1) type = jitc_typecache_align(context, type, align);
return type;
}
jitc_type_t* jitc_parse_type(jitc_context_t* context, queue_t* tokens, jitc_decltype_t* decltype, jitc_preserve_t* preserve_policy) {
jitc_type_t* type = NULL;
if (!(type = jitc_parse_base_type(context, tokens, decltype, NULL, preserve_policy))) return NULL;
if (!jitc_parse_type_declarations(context, tokens, &type)) return NULL;
return type;
}
jitc_type_t* jitc_type_promotion(jitc_context_t* context, jitc_type_t* left, jitc_type_t* right, bool min_int32) {
if (!is_number(left)) return left;
if (!is_number(right)) return right;
jitc_type_kind_t kind = left->kind > right->kind ? left->kind : right->kind;
bool is_unsigned = left->is_unsigned || right->is_unsigned;
if (min_int32 && kind < Type_Int32) kind = Type_Int32;
if (kind == Type_Float32 || kind == Type_Float64) is_unsigned = false;
jitc_type_t* type = jitc_typecache_primitive(context, kind);
if (is_unsigned) type = jitc_typecache_unsigned(context, type);
return type;
}
bool jitc_can_cast(jitc_type_t* from, jitc_type_t* to, bool explicit, bool is_zero) {
if (from->kind == Type_Void) return false;
if (to->kind == Type_Void) return true;
if (from == to) return true;
if (
from->kind == Type_Struct ||
from->kind == Type_Union ||
to->kind == Type_Struct ||
to->kind == Type_Union
) return false;
if (is_floating(from) && is_pointer(to)) return false;
if (is_floating(to) && is_pointer(from)) return false;
return true;
}
jitc_ast_t* jitc_cast(jitc_context_t* context, jitc_ast_t* node, jitc_type_t* type, bool explicit, jitc_token_t* cast_token) {
if (jitc_typecmp(context, node->exprtype, type)) return node;
bool is_zero = node->node_type == AST_Integer && node->integer.value == 0;
if (!jitc_can_cast(node->exprtype, type, explicit, is_zero)) throw(cast_token, "Unable to perform cast");
switch (node->node_type) {
case AST_Integer:
if (is_floating(type)) {
if (type->is_unsigned) node->floating.value = node->integer.value;
else node->floating.value = (int64_t)node->integer.value;
node->floating.is_single_precision = type->kind == Type_Float32;
node->node_type = AST_Floating;
node->exprtype = jitc_typecache_primitive(context,
node->floating.is_single_precision
? Type_Float32
: Type_Float64
);
}
else if (!is_pointer(type)) {
bool is_negative = !node->integer.is_unsigned && ((node->integer.value >> 63) & 1);
node->integer.type_kind = is_pointer(type) ? Type_Int64 : type->kind;
node->integer.is_unsigned = is_pointer(type) ? true : type->is_unsigned;
memset((char*)&node->integer.value + type->size, is_negative ? 0xFF : 0x00, 8 - type->size);
node->exprtype = jitc_typecache_primitive(context, node->integer.type_kind);
if (node->integer.is_unsigned) node->exprtype = jitc_typecache_unsigned(context, node->exprtype);
}
else goto fallback;
break;
case AST_Floating:
if (is_floating(type)) {
node->floating.is_single_precision = type->kind == Type_Float32;
node->exprtype = jitc_typecache_primitive(context,
node->floating.is_single_precision
? Type_Float32
: Type_Float64
);
}
else {
node->node_type = AST_Integer;
node->integer.value = node->floating.value;
node->integer.type_kind = type->kind;
node->integer.is_unsigned = type->is_unsigned;
node->exprtype = jitc_typecache_primitive(context, node->integer.type_kind);
if (node->integer.is_unsigned) node->exprtype = jitc_typecache_unsigned(context, node->exprtype);
}
break;
case AST_StringLit:
node->node_type = AST_Integer;
node->integer.type_kind = is_pointer(type) ? Type_Int64 : type->kind;
node->integer.is_unsigned = is_pointer(type) ? true : type->is_unsigned;
node->exprtype = jitc_typecache_primitive(context, Type_Int8);
node->exprtype = jitc_typecache_const(context, node->exprtype);
node->exprtype = jitc_typecache_pointer(context, node->exprtype);
break;
default: fallback: {
jitc_ast_t* cast = mknode(AST_Binary, node->token);
jitc_ast_t* type_node = mknode(AST_Type, node->token);
type_node->type.type = type;
cast->exprtype = type;
cast->binary.operation = Binary_Cast;
cast->binary.left = node;
cast->binary.right = type_node;
node = cast;
} break;
}
return node;
}
jitc_ast_t* jitc_process_ast(jitc_context_t* context, jitc_ast_t* ast, jitc_type_t** exprtype) {
jitc_ast_t* node = ast;
switch (node->node_type) {
case AST_Integer: if (!node->exprtype) {
node->exprtype = jitc_typecache_primitive(context, node->integer.type_kind);
if (node->integer.is_unsigned) node->exprtype = jitc_typecache_unsigned(context, node->exprtype);
} break;
case AST_Floating: if (!node->exprtype) {
node->exprtype = jitc_typecache_primitive(context,
node->floating.is_single_precision
? Type_Float32
: Type_Float64
);
} break;
case AST_StringLit: if (!node->exprtype) {
node->exprtype = jitc_typecache_primitive(context, Type_Int8);
node->exprtype = jitc_typecache_const(context, node->exprtype);
node->exprtype = jitc_typecache_pointer(context, node->exprtype);
} break;
case AST_Variable: if (!node->exprtype) {
jitc_variable_t* variable = jitc_get_variable(context, node->variable.name);
if (variable->decltype == Decltype_EnumItem) {
node->node_type = AST_Integer;
node->integer.type_kind = variable->type->kind;
node->integer.is_unsigned = variable->type->is_unsigned;
node->integer.value = variable->enum_value;
node->exprtype = variable->type;
}
else if (node->variable.templ_map) {
node->exprtype = jitc_typecache_fill_template(context, variable->type, node->variable.templ_map);
node->exprtype = jitc_mangle_template(context, node->exprtype, node->variable.templ_map);
node->variable.name = node->exprtype->name;
queue_push(context->instantiation_requests) = (jitc_instantiation_request_t){
.tokens = variable->ptr,
.preserve_policy = variable->preserve_policy,
.decltype = variable->decltype,
.target_type = node->exprtype,
.template_map = node->variable.templ_map
};
}
else node->exprtype = variable->type;
node->exprtype = jitc_typecache_decay(context, node->exprtype);
} break;
case AST_WalkStruct: if (!node->exprtype) {
jitc_type_t* type;
node->walk_struct.struct_ptr = try(jitc_process_ast(context, node->walk_struct.struct_ptr, &type));
while (is_pointer(type)) {
jitc_ast_t* deref = mknode(AST_Unary, node->token);
deref->unary.operation = Unary_Dereference;
deref->unary.inner = node->walk_struct.struct_ptr;
deref->exprtype = type = type->ptr.base;
node->walk_struct.struct_ptr = deref;
}
if (!jitc_validate_type(type, TypePolicy_NoUndefTags)) {
jitc_type_t* resolved = jitc_get_tagged_type(context, type) ?: type;
type = node->walk_struct.struct_ptr->exprtype = resolved;
}
if (is_struct(type) && jitc_walk_struct(type, node->walk_struct.field_name, &node->exprtype, &node->walk_struct.offset)) {
if (node->walk_struct.templ_list) throw(node->token, "Template parameter list on a struct field");
break;
}
map_t* template_map = NULL;
jitc_variable_t* method = jitc_get_method(context, type, node->walk_struct.field_name, node->walk_struct.templ_list, &template_map);
if (!method) throw(node->token,
node->walk_struct.templ_list ? "No %s matching '%s<%d>' found" : "No %s matching '%s' found",
is_struct(type) ? "field or method" : "method",
node->walk_struct.field_name,
node->walk_struct.templ_list ? list_size(node->walk_struct.templ_list) : 0
);
node->node_type = AST_Variable;
node->variable.this_ptr = node->walk_struct.struct_ptr;
node->variable.name = method->type->name;
node->variable.templ_map = template_map;
node->variable.write_dest = false;
node->exprtype = NULL;
node = try(jitc_process_ast(context, node, NULL));
} break;
case AST_Initializer:
node->init.store_to = try(jitc_process_ast(context, node->init.store_to, NULL));
node->exprtype = node->init.type;
break;
case AST_Unary: switch (node->unary.operation) {
case Unary_SuffixIncrement:
case Unary_SuffixDecrement:
case Unary_PrefixIncrement:
case Unary_PrefixDecrement:
case Unary_AddressOf:
node->unary.inner = try(jitc_process_ast(context, node->unary.inner, &node->exprtype));
if (node->unary.operation == Unary_AddressOf) {
if (node->unary.inner->node_type == AST_Unary && node->unary.inner->unary.operation == Unary_Dereference) {
jitc_ast_t* tmp = node->unary.inner->unary.inner;
replace(node->unary.inner) = tmp;
replace(node) = tmp;
break;
}
else node->exprtype = jitc_typecache_pointer(context, node->exprtype);
}
else {
if (node->exprtype->is_const) throw(node->token, "Assigning to a const");
if (!is_scalar(node->exprtype)) throw(node->token, "Operand must be a scalar type");
if (is_pointer(node->exprtype)) node->unary.operation += Unary_PtrSuffixIncrement - Unary_SuffixIncrement;
}
if (!is_lvalue(node->unary.inner)) throw(node->token, "Operand must be an lvalue");
break;
case Unary_Dereference:
node->unary.inner = try(jitc_process_ast(context, node->unary.inner, &node->exprtype));
if (node->unary.inner->node_type == AST_Unary && node->unary.inner->unary.operation == Unary_AddressOf) {
jitc_ast_t* tmp = node->unary.inner->unary.inner;
replace(node->unary.inner) = tmp;
replace(node) = tmp;
}
else {
if (!is_pointer(node->exprtype)) throw(node->token, "Operand must be a pointer type");
if (is_function(node->exprtype)) throw(node->token, "Cannot dereference a function");
if (node->exprtype->ptr.base->kind == Type_Void) throw(node->token, "Dereferencing void pointer");
node->exprtype = node->exprtype->ptr.base;
}
break;
case Unary_ArithPlus:
case Unary_ArithNegate:
node->unary.inner = try(jitc_process_ast(context, node->unary.inner, &node->exprtype));
if (!is_number(node->exprtype)) throw(node->token, "Operand must be a numeric type");
if (node->unary.operation == Unary_ArithPlus) {
jitc_ast_t* inner = node->unary.inner;
replace(node) = inner;
}
else {
if (node->unary.inner->node_type == AST_Integer) {
jitc_ast_t* inner = node->unary.inner;
inner->integer.value = -inner->integer.value;
replace(node) = inner;
}
else if (node->unary.inner->node_type == AST_Floating) {
jitc_ast_t* inner = node->unary.inner;
inner->floating.value = -inner->floating.value;
replace(node) = inner;
}
else if (node->unary.inner->node_type == AST_Unary && node->unary.inner->unary.operation == Unary_ArithNegate) {
jitc_ast_t* tmp = node->unary.inner->unary.inner;
replace(node->unary.inner) = tmp;
replace(node) = tmp;
}
}
break;
case Unary_BinaryNegate:
node->unary.inner = try(jitc_process_ast(context, node->unary.inner, &node->exprtype));
if (!is_integer(node->exprtype)) throw(node->token, "Operand must be an integer type");
if (is_constant(node->unary.inner)) {
jitc_ast_t* inner = node->unary.inner;
inner->integer.value = ~inner->integer.value;
replace(node) = inner;
}
else if (node->unary.inner->node_type == AST_Unary && node->unary.inner->unary.operation == Unary_BinaryNegate) {
jitc_ast_t* tmp = node->unary.inner->unary.inner;
replace(node->unary.inner) = tmp;
replace(node) = tmp;
}
break;
case Unary_LogicNegate:
node->unary.inner = try(jitc_process_ast(context, node->unary.inner, &node->exprtype));
if (is_struct(node->exprtype)) throw(node->token, "Negating a non-scalar type");
if (node->unary.inner->node_type == AST_Unary && node->unary.inner->unary.operation == Unary_LogicNegate) {
jitc_ast_t* inner = node->unary.inner;
if (inner->unary.inner->node_type == AST_Unary && inner->unary.inner->unary.operation == Unary_LogicNegate) {
jitc_ast_t* tmp = inner->unary.inner->unary.inner;
replace(inner->unary.inner) = tmp;
replace(node->unary.inner) = tmp;
replace(node) = tmp;
}
}
if (is_constant(node->unary.inner)) {
jitc_ast_t* inner = node->unary.inner;
node->node_type = AST_Integer;
node->integer.value = !(is_decayed_pointer(node->exprtype) ? 1 : node->unary.inner->node_type == AST_Floating
? inner->floating.value
: inner->integer.value
);
node->integer.type_kind = Type_Int8;
node->integer.is_unsigned = true;
jitc_destroy_ast(inner);
}
node->exprtype = jitc_typecache_primitive(context, Type_Int8);
node->exprtype = jitc_typecache_unsigned(context, node->exprtype);
break;
default: break;
} break;
case AST_Binary: switch (node->binary.operation) {
case Binary_Cast: {
jitc_ast_t* left = node->binary.left;
jitc_ast_t* right = node->binary.right;
replace(node) = try(jitc_cast(context,
try(jitc_process_ast(context, left, NULL)),
right->type.type, true, right->token
));
} break;
case Binary_FunctionCall: {
node->binary.left = try(jitc_process_ast(context, node->binary.left, &node->exprtype));
if (!is_function(node->exprtype)) throw(node->token, "Calling a non-function");
jitc_type_t* func = node->exprtype->ptr.base;
list(jitc_ast_t*)* list = (void*)node->binary.right->list.inner;
size_t num_fixed_args = func->func.num_params;
bool has_varargs = func->func.num_params != 0 && func->func.params[func->func.num_params - 1]->kind == Type_Varargs;
node->exprtype = func->func.ret;
if (has_varargs) num_fixed_args--;
if (node->binary.left->node_type == AST_Variable && node->binary.left->variable.this_ptr) {
smartptr(list(jitc_ast_t*)) old_list = (void*)list;
smartptr(list(jitc_ast_t*)) new_list = list_new(jitc_ast_t*);
jitc_ast_t* addrof = mknode(AST_Unary, node->token);
addrof->unary.operation = Unary_AddressOf;
addrof->unary.inner = move(node->binary.left->variable.this_ptr);
list_add(new_list) = addrof;
for (size_t i = 0; i < list_size(list); i++) list_add(new_list) = list_get(old_list, i);
list = (void*)move(new_list);
node->binary.right->list.inner = (void*)list;
}
if (list_size(list) < num_fixed_args || (!has_varargs && list_size(list) != num_fixed_args)) throw(node->token,
"Incorrect number of arguments (got %d, %s %d)",
list_size(list), has_varargs ? "minimum" : "expected", func->func.num_params
);
for (size_t i = 0; i < list_size(list); i++) {
jitc_ast_t** param = &list_get(list, i);
*param = try(jitc_process_ast(context, *param, NULL));
if (i < num_fixed_args)
*param = try(jitc_cast(context, *param, func->func.params[i], false, (*param)->token));
else if ((*param)->exprtype->kind == Type_Float32)
*param = try(jitc_cast(context, *param, jitc_typecache_primitive(context, Type_Float64), false, (*param)->token));
}
} break;
#define ARITHMETIC(op, can_be_ptr) \
node->binary.left = try(jitc_process_ast(context, node->binary.left, NULL)); \
node->binary.right = try(jitc_process_ast(context, node->binary.right, NULL)); \
node->exprtype = jitc_type_promotion(context, node->binary.left->exprtype, node->binary.right->exprtype, true); \
if (can_be_ptr && is_pointer(node->exprtype)) { \
if (is_pointer(node->binary.right->exprtype)) { \
if (node->binary.operation == Binary_Subtraction) { \
if (is_pointer(node->binary.left->exprtype)) { \
if (node->binary.left->exprtype->ptr.base->size != node->binary.right->exprtype->ptr.base->size) \
throw(node->token, "Pointed-to objects have different sizes"); \
node->binary.operation = Binary_PtrDiff; \
node->exprtype = jitc_typecache_unsigned(context, jitc_typecache_primitive(context, Type_Int64)); \
break; \
} \
else throw(node->token, "Pointer subtraction with pointer on RHS of expression"); \
} \
jitc_ast_t* tmp = node->binary.left; \
node->binary.left = node->binary.right; \
node->binary.right = tmp; \
} \
if (is_pointer(node->binary.left->exprtype) && !is_integer(node->binary.right->exprtype)) \
throw(node->token, "Pointer arithmetic on a non-integer type"); \
node->binary.operation += Binary_PtrAddition - Binary_Addition; \
break; \
} \
if (!is_number(node->exprtype)) throw(node->token, "Arithmetic operation on a non-%s", can_be_ptr ? "scalar" : "number"); \
node->binary.left = try(jitc_cast(context, node->binary.left, node->exprtype, false, node->token)); \
node->binary.right = try(jitc_cast(context, node->binary.right, node->exprtype, false, node->token)); \
if (is_constant(node->binary.left) && is_constant(node->binary.right)) { \
smartptr(jitc_ast_t) left = node->binary.left; \
smartptr(jitc_ast_t) right = node->binary.right; \
if (is_floating(node->exprtype)) { \
node->node_type = AST_Floating; \
node->floating.is_single_precision = node->exprtype->kind == Type_Float32; \
node->floating.value = left->floating.value op right->floating.value; \
} \
else { \
node->node_type = AST_Integer; \
node->integer.type_kind = node->exprtype->kind; \
node->integer.is_unsigned = node->exprtype->is_unsigned; \
node->integer.value = left->integer.value op right->integer.value; \
} \
} \
break
#define BINARY(op) \
node->binary.left = try(jitc_process_ast(context, node->binary.left, NULL)); \
node->binary.right = try(jitc_process_ast(context, node->binary.right, NULL)); \
node->exprtype = jitc_type_promotion(context, node->binary.left->exprtype, node->binary.right->exprtype, true); \
if (!is_integer(node->exprtype)) throw(node->token, "Bitwise operation on a non-integer"); \
node->binary.left = try(jitc_cast(context, node->binary.left, node->exprtype, false, node->token)); \
node->binary.right = try(jitc_cast(context, node->binary.right, node->exprtype, false, node->token)); \
if (is_constant(node->binary.left) && is_constant(node->binary.right)) { \
smartptr(jitc_ast_t) left = node->binary.left; \
smartptr(jitc_ast_t) right = node->binary.right; \
node->node_type = AST_Integer; \
node->integer.type_kind = node->exprtype->kind; \
node->integer.is_unsigned = node->exprtype->is_unsigned; \
node->integer.value = left->integer.value op right->integer.value; \
} \
break
#define COMPARE(op) { \
node->binary.left = try(jitc_process_ast(context, node->binary.left, NULL)); \
node->binary.right = try(jitc_process_ast(context, node->binary.right, NULL)); \
jitc_type_t* type = jitc_type_promotion(context, node->binary.left->exprtype, node->binary.right->exprtype, true); \
node->binary.left = try(jitc_cast(context, node->binary.left, type, false, node->token)); \
node->binary.right = try(jitc_cast(context, node->binary.right, type, false, node->token)); \
if (is_constant(node->binary.left) && is_constant(node->binary.right)) { \
smartptr(jitc_ast_t) left = node->binary.left; \
smartptr(jitc_ast_t) right = node->binary.right; \
node->node_type = AST_Integer; \
node->integer.type_kind = Type_Int8; \
node->integer.is_unsigned = true; \
if (type->kind == Type_Float32 || type->kind == Type_Float64) \
node->integer.value = left->floating.value op node->binary.right->floating.value; \
else { \
bool lneg = !left ->integer.is_unsigned && ((left ->integer.value >> 63) & 1); \
bool rneg = !right->integer.is_unsigned && ((right->integer.value >> 63) & 1); \
if (lneg != rneg) node->integer.value = rneg op lneg; \
else node->integer.value = left->integer.value op right->integer.value; \
} \
} \
node->exprtype = jitc_typecache_primitive(context, Type_Int8); \
node->exprtype = jitc_typecache_unsigned(context, node->exprtype); \
} break
#define LOGIC(op, shortcircuit) { \
node->binary.left = try(jitc_process_ast(context, node->binary.left, NULL)); \
node->binary.right = try(jitc_process_ast(context, node->binary.right, NULL)); \
bool lval = false, lconst = is_constant(node->binary.left); \
bool rval = false, rconst = is_constant(node->binary.right); \
if (is_struct(node->binary.left->exprtype) || is_struct(node->binary.right->exprtype)) \
throw(node->token, "Performing logic on a non-scalar type"); \
if (is_decayed_pointer(node->binary.right->exprtype)) lconst = lval = true; \
else if (lconst) lval = !(node->node_type == AST_Floating \
? !node->binary.left->floating.value \
: !node->binary.left->integer.value \
); \
if (is_decayed_pointer(node->binary.right->exprtype)) rconst = rval = true; \
else if (rconst) rval = !(node->node_type == AST_Floating \
? !node->binary.right->floating.value \
: !node->binary.right->integer.value \
); \
if ((lval == shortcircuit && lconst) || (rval == shortcircuit && rconst) || (lconst && rconst)) { \
smartptr(jitc_ast_t) left = node->binary.left; \
smartptr(jitc_ast_t) right = node->binary.right; \
node->node_type = AST_Integer; \
node->integer.value = lval op rval; \
node->integer.type_kind = Type_Int8; \
node->integer.is_unsigned = true; \
} \
node->exprtype = jitc_typecache_primitive(context, Type_Int8); \
node->exprtype = jitc_typecache_unsigned(context, node->exprtype); \
} break
#define ASSIGNMENT(allow_const, check, errmsg) \
node->binary.left = try(jitc_process_ast(context, node->binary.left, &node->exprtype)); \
node->binary.right = try(jitc_process_ast(context, node->binary.right, NULL)); \
if (node->exprtype->is_const && !allow_const) throw(node->token, "Assigning to const"); \
if (!is_lvalue(node->binary.left)) throw(node->token, "Assigning to an rvalue"); \
if (is_decayed_pointer(node->unary.inner->exprtype)) throw(node->token, "Assigning to an object"); \
if (!(check)) throw(node->token, errmsg); \
if (is_pointer(node->binary.left->exprtype) && node->binary.operation != Binary_Assignment && node->binary.operation != Binary_AssignConst) { \
if (is_pointer(node->binary.right->exprtype) && node->binary.operation == Binary_AssignSubtraction) { \
if (node->binary.left->exprtype->ptr.base->size != node->binary.right->exprtype->ptr.base->size) \
throw(node->token, "Pointed-to objects have different sizes"); \
node->binary.operation = Binary_AssignPtrDiff; \
node->exprtype = jitc_typecache_unsigned(context, jitc_typecache_primitive(context, Type_Int64)); \
} \
else { \
if (!is_integer(node->binary.right->exprtype)) \
throw(node->token, "Pointer arithmetic with a non-integer type"); \
node->binary.operation += Binary_AssignPtrAddition - Binary_AssignAddition; \
} \
} \
else node->binary.right = try(jitc_cast(context, node->binary.right, node->exprtype, false, node->token)); \
break
case Binary_Addition: ARITHMETIC(+, true);
case Binary_Subtraction: ARITHMETIC(-, true);
case Binary_Multiplication: ARITHMETIC(*, false);
case Binary_Division: ARITHMETIC(/, false);
case Binary_Modulo: BINARY(%);
case Binary_BitshiftLeft: BINARY(<<);
case Binary_BitshiftRight: BINARY(>>);
case Binary_And: BINARY(&);
case Binary_Or: BINARY(|);
case Binary_Xor: BINARY(^);
case Binary_LessThan: COMPARE(<);
case Binary_LessThanOrEqualTo: COMPARE(<=);
case Binary_GreaterThan: COMPARE(>);
case Binary_GreaterThanOrEqualTo: COMPARE(>=);
case Binary_Equals: COMPARE(==);
case Binary_NotEquals: COMPARE(!=);
case Binary_LogicAnd: LOGIC(&&, false);
case Binary_LogicOr: LOGIC(||, true);
case Binary_AssignConst: ASSIGNMENT(true, true, "");
case Binary_Assignment: ASSIGNMENT(false, true, "");
case Binary_AssignAddition:
case Binary_AssignSubtraction: ASSIGNMENT(false, is_number(node->exprtype) || node->exprtype->kind == Type_Pointer, "Arithmetic on a non-scalar type");
case Binary_AssignMultiplication:
case Binary_AssignDivision: ASSIGNMENT(false, is_number(node->exprtype), "Arithmetic on a non-numeric type");
case Binary_AssignModulo:
case Binary_AssignBitshiftLeft: