-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsemantic.c
2585 lines (2205 loc) · 73.3 KB
/
semantic.c
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 <config.h>
#include <stdbool.h>
#include "semantic_t.h"
#include "ast_t.h"
#include "type_t.h"
#include "type_hash.h"
#include "match_type.h"
#include "adt/obst.h"
#include "adt/array.h"
#include "adt/error.h"
//#define DEBUG_TYPEVAR_BINDINGS
//#define ABORT_ON_ERRORS
//#define DEBUG_ENVIRONMENT
typedef struct environment_entry_t environment_entry_t;
struct environment_entry_t {
symbol_t *symbol;
entity_t *up;
const void *up_context;
};
static lower_statement_function *statement_lowerers = NULL;
static lower_expression_function *expression_lowerers = NULL;
static struct obstack symbol_environment_obstack;
static environment_entry_t **symbol_stack;
static bool found_export;
static bool found_errors;
static type_t *type_bool = NULL;
static type_t *type_byte = NULL;
static type_t *type_int = NULL;
static type_t *type_uint = NULL;
static type_t *type_double = NULL;
static type_t *type_byte_ptr = NULL;
static type_t *type_void_ptr = NULL;
static type_t *error_type = NULL;
static function_t *current_function = NULL;
bool last_statement_was_return = false;
static void check_and_push_context(context_t *context);
static void check_function(function_t *function, symbol_t *symbol,
const source_position_t source_position);
static void resolve_function_types(function_t *function);
void print_error_prefix(const source_position_t position)
{
fprintf(stderr, "%s:%d: error: ", position.input_name, position.linenr);
found_errors = true;
#ifdef ABORT_ON_ERRORS
abort();
#endif
}
void print_warning_prefix(const source_position_t position)
{
fprintf(stderr, "%s:%d: warning: ", position.input_name, position.linenr);
}
void error_at(const source_position_t position,
const char *message)
{
print_error_prefix(position);
fprintf(stderr, "%s\n", message);
}
/**
* pushs an environment_entry on the environment stack and links the
* corresponding symbol to the new entry
*/
static void environment_push(entity_t *entity, const void *context)
{
environment_entry_t *entry
= obstack_alloc(&symbol_environment_obstack, sizeof(entry[0]));
memset(entry, 0, sizeof(entry[0]));
int top = ARR_LEN(symbol_stack);
ARR_RESIZE(environment_entry_t*, symbol_stack, top + 1);
symbol_stack[top] = entry;
symbol_t *symbol = entity->base.symbol;
assert(entity != symbol->entity);
if (symbol->context == context) {
assert(symbol->entity != NULL);
print_error_prefix(entity->base.source_position);
fprintf(stderr, "multiple definitions for symbol '%s'.\n",
symbol->string);
print_error_prefix(symbol->entity->base.source_position);
fprintf(stderr, "this is the location of the previous entity.\n");
}
#ifdef DEBUG_ENVIRONMENT
fprintf(stderr, "Push symbol '%s'\n", symbol->string);
#endif
entry->up = symbol->entity;
entry->up_context = symbol->context;
entry->symbol = symbol;
symbol->entity = entity;
symbol->context = context;
}
/**
* pops symbols from the environment stack until @p new_top is the top element
*/
static inline
void environment_pop_to(size_t new_top)
{
environment_entry_t *entry = NULL;
size_t top = ARR_LEN(symbol_stack);
size_t i;
if (new_top == top)
return;
assert(new_top < top);
i = top;
do {
entry = symbol_stack[i - 1];
symbol_t *symbol = entry->symbol;
entity_t *entity = symbol->entity;
if (entity->base.refs == 0 && !entity->base.exported) {
switch (entity->kind) {
/* only warn for functions/variables at the moment, we don't
count refs on types yet */
case ENTITY_FUNCTION:
case ENTITY_VARIABLE:
print_warning_prefix(entity->base.source_position);
fprintf(stderr, "%s '%s' was declared but never read\n",
get_entity_kind_name(entity->kind), symbol->string);
default:
break;
}
}
#ifdef DEBUG_ENVIRONMENT
fprintf(stderr, "Pop symbol '%s'\n", symbol->string);
#endif
symbol->entity = entry->up;
symbol->context = entry->up_context;
--i;
} while (i != new_top);
obstack_free(&symbol_environment_obstack, entry);
ARR_SHRINKLEN(symbol_stack, (int) new_top);
}
/**
* returns the top element of the environment stack
*/
static inline
size_t environment_top(void)
{
return ARR_LEN(symbol_stack);
}
static type_t *normalize_type(type_t *type);
static void normalize_type_arguments(type_argument_t *type_arguments)
{
/* normalize type arguments */
type_argument_t *type_argument = type_arguments;
while (type_argument != NULL) {
type_argument->type = normalize_type(type_argument->type);
type_argument = type_argument->next;
}
}
static type_t *resolve_type_reference(type_reference_t *type_ref)
{
normalize_type_arguments(type_ref->type_arguments);
symbol_t *symbol = type_ref->symbol;
entity_t *entity = symbol->entity;
if (entity == NULL) {
print_error_prefix(type_ref->source_position);
fprintf(stderr, "can't resolve type: symbol '%s' is unknown\n",
symbol->string);
return type_invalid;
}
if (entity->kind == ENTITY_TYPE_VARIABLE) {
type_variable_t *type_variable = &entity->type_variable;
if (type_variable->current_type != NULL) {
/* not sure if this is really a problem... */
fprintf(stderr, "Debug warning: unresolved type var ref found "
"a concrete type...\n");
return type_variable->current_type;
}
type_ref->base.kind = TYPE_REFERENCE_TYPE_VARIABLE;
type_ref->type_variable = type_variable;
return typehash_insert((type_t*) type_ref);
}
if (entity->kind != ENTITY_TYPEALIAS) {
print_error_prefix(type_ref->source_position);
fprintf(stderr, "expected a type alias or type variable, but '%s' is a '%s'\n",
symbol->string, get_entity_kind_name(entity->kind));
return type_invalid;
}
typealias_t *typealias = &entity->typealias;
typealias->type = normalize_type(typealias->type);
type_t *type = typealias->type;
type_variable_t *type_parameters = NULL;
compound_type_t *compound_type = NULL;
if (type->kind == TYPE_COMPOUND_STRUCT || type->kind == TYPE_COMPOUND_UNION) {
compound_type = (compound_type_t*) type;
type_parameters = compound_type->type_parameters;
}
/* check that type arguments match type parameters
* and normalize the type arguments */
type_argument_t *type_arguments = type_ref->type_arguments;
type_variable_t *type_parameter = type_parameters;
type_argument_t *type_argument = type_arguments;
while (type_parameter != NULL) {
if (type_argument == NULL) {
print_error_prefix(type_ref->source_position);
fprintf(stderr, "too few type parameters specified for type ");
print_type(type);
fprintf(stderr, "\n");
break;
}
type_parameter = type_parameter->next;
type_argument = type_argument->next;
}
if (type_argument != NULL) {
print_error_prefix(type_ref->source_position);
if (type_parameters == NULL) {
fprintf(stderr, "type ");
} else {
fprintf(stderr, "too many type parameters specified for ");
}
print_type(type);
fprintf(stderr, " takes no type parameters\n");
}
if (type_parameters != NULL && type_argument == NULL
&& type_argument == NULL) {
bind_typevariables_type_t *bind_typevariables
= obstack_alloc(type_obst, sizeof(bind_typevariables[0]));
memset(bind_typevariables, 0, sizeof(bind_typevariables[0]));
bind_typevariables->base.kind = TYPE_BIND_TYPEVARIABLES;
bind_typevariables->type_arguments = type_arguments;
assert(compound_type != NULL);
bind_typevariables->polymorphic_type = compound_type;
type = (type_t*) bind_typevariables;
}
return type;
}
static type_t *resolve_type_reference_type_var(type_reference_t *type_ref)
{
type_variable_t *type_variable = type_ref->type_variable;
if (type_variable->current_type != NULL) {
return normalize_type(type_variable->current_type);
}
return typehash_insert((type_t*) type_ref);
}
static type_t *normalize_pointer_type(pointer_type_t *type)
{
type->points_to = normalize_type(type->points_to);
return typehash_insert((type_t*) type);
}
static type_t *normalize_array_type(array_type_t *type)
{
type->element_type = normalize_type(type->element_type);
type->size_expression = check_expression(type->size_expression);
return typehash_insert((type_t*) type);
}
static type_t *normalize_function_type(function_type_t *function_type)
{
function_type->result_type = normalize_type(function_type->result_type);
function_parameter_type_t *parameter = function_type->parameter_types;
while (parameter != NULL) {
parameter->type = normalize_type(parameter->type);
parameter = parameter->next;
}
return typehash_insert((type_t*) function_type);
}
static void check_compound_type(compound_type_t *type)
{
int old_top = environment_top();
check_and_push_context(&type->context);
compound_entry_t *entry = type->entries;
while (entry != NULL) {
type_t *type = entry->type;
if (type->kind == TYPE_COMPOUND_STRUCT
|| type->kind == TYPE_COMPOUND_UNION) {
compound_type_t *compound_type = (compound_type_t*) type;
check_compound_type(compound_type);
}
entry->type = normalize_type(type);
entry = entry->next;
}
environment_pop_to(old_top);
}
static type_t *normalize_compound_type(compound_type_t *type)
{
type_t *result = typehash_insert((type_t*) type);
return result;
}
static type_t *normalize_bind_typevariables(bind_typevariables_type_t *type)
{
type_t *polymorphic_type = (type_t*) type->polymorphic_type;
polymorphic_type = normalize_type(polymorphic_type);
assert(polymorphic_type->kind == TYPE_COMPOUND_STRUCT ||
polymorphic_type->kind == TYPE_COMPOUND_UNION);
type->polymorphic_type = (compound_type_t*) polymorphic_type;
type_t *result = typehash_insert((type_t*) type);
return result;
}
static type_t *normalize_type(type_t *type)
{
/* happens sometimes on semantic errors */
if (type == NULL)
return NULL;
switch (type->kind) {
case TYPE_INVALID:
case TYPE_VOID:
case TYPE_ATOMIC:
case TYPE_ERROR:
return type;
case TYPE_TYPEOF: {
typeof_type_t *typeof_type = (typeof_type_t*) type;
typeof_type->expression = check_expression(typeof_type->expression);
return type;
}
case TYPE_REFERENCE:
return resolve_type_reference((type_reference_t*) type);
case TYPE_REFERENCE_TYPE_VARIABLE:
return resolve_type_reference_type_var((type_reference_t*) type);
case TYPE_POINTER:
return normalize_pointer_type((pointer_type_t*) type);
case TYPE_ARRAY:
return normalize_array_type((array_type_t*) type);
case TYPE_FUNCTION:
return normalize_function_type((function_type_t*) type);
case TYPE_COMPOUND_UNION:
case TYPE_COMPOUND_STRUCT:
return normalize_compound_type((compound_type_t*) type);
case TYPE_BIND_TYPEVARIABLES:
return normalize_bind_typevariables((bind_typevariables_type_t*) type);
}
panic("Unknown type found");
}
static type_t *check_reference(entity_t *entity,
const source_position_t source_position)
{
type_t *type;
entity->base.refs++;
switch (entity->kind) {
case ENTITY_VARIABLE:
type = entity->variable.type;
if (type == NULL)
return NULL;
if (type->kind == TYPE_COMPOUND_STRUCT
|| type->kind == TYPE_COMPOUND_UNION
|| type->kind == TYPE_BIND_TYPEVARIABLES
|| type->kind == TYPE_ARRAY) {
entity->variable.needs_entity = true;
}
return type;
case ENTITY_FUNCTION:
return make_pointer_type((type_t*) entity->function.function.type);
case ENTITY_CONSTANT: {
constant_t *constant = &entity->constant;
/* do type inference for the constant if needed */
if (constant->type == NULL) {
constant->expression = check_expression(constant->expression);
constant->type = constant->expression->base.type;
}
return constant->type;
}
case ENTITY_FUNCTION_PARAMETER:
assert(entity->parameter.type != NULL);
return entity->parameter.type;
case ENTITY_CONCEPT_FUNCTION:
return make_pointer_type((type_t*) entity->concept_function.type);
case ENTITY_LABEL:
case ENTITY_TYPEALIAS:
case ENTITY_CONCEPT:
case ENTITY_TYPE_VARIABLE:
print_error_prefix(source_position);
fprintf(stderr, "'%s' (a '%s') can't be used as expression\n",
entity->base.symbol->string,
get_entity_kind_name(entity->kind));
return NULL;
case ENTITY_ERROR:
found_errors = true;
return NULL;
case ENTITY_INVALID:
panic("reference to invalid declaration type encountered");
}
panic("reference to unknown declaration type encountered");
}
static entity_t *create_error_entity(symbol_t *symbol)
{
entity_t *entity = allocate_entity(ENTITY_ERROR);
entity->base.symbol = symbol;
entity->base.exported = true;
return entity;
}
static void check_reference_expression(reference_expression_t *ref)
{
symbol_t *symbol = ref->symbol;
entity_t *entity = symbol->entity;
if (entity == NULL) {
print_error_prefix(ref->base.source_position);
fprintf(stderr, "no known definition for '%s'\n", symbol->string);
entity = create_error_entity(symbol);
}
normalize_type_arguments(ref->type_arguments);
ref->entity = entity;
type_t *type = check_reference(entity, ref->base.source_position);
ref->base.type = type;
}
static bool is_lvalue(const expression_t *expression)
{
switch (expression->kind) {
case EXPR_REFERENCE: {
const reference_expression_t *reference
= (const reference_expression_t*) expression;
const entity_t *entity = reference->entity;
if (entity->kind == ENTITY_VARIABLE) {
return true;
}
break;
}
case EXPR_ARRAY_ACCESS:
return true;
case EXPR_SELECT:
return true;
case EXPR_UNARY_DEREFERENCE:
return true;
default:
break;
}
return false;
}
static void check_assign_expression(binary_expression_t *assign)
{
expression_t *left = assign->left;
expression_t *right = assign->right;
if (!is_lvalue(left)) {
error_at(assign->base.source_position,
"left side of assign is not an lvalue.\n");
return;
}
if (left->kind == EXPR_REFERENCE) {
reference_expression_t *reference = (reference_expression_t*) left;
entity_t *entity = reference->entity;
if (entity->kind == ENTITY_VARIABLE) {
variable_t *variable = (variable_t*) entity;
symbol_t *symbol = variable->base.symbol;
/* do type inference if needed */
if (left->base.type == NULL) {
if (right->base.type == NULL) {
print_error_prefix(assign->base.source_position);
fprintf(stderr, "can't infer type for '%s'\n",
symbol->string);
return;
}
variable->type = right->base.type;
left->base.type = right->base.type;
}
/* the reference expression increased the ref pointer, but
* making an assignment is not reading the value */
variable->base.refs--;
}
}
}
/**
* creates an implicit cast if possible or reports an error
*/
static expression_t *make_cast(expression_t *from,
type_t *dest_type,
const source_position_t source_position,
bool lenient)
{
if (dest_type == NULL || from->base.type == dest_type)
return from;
/* TODO: - test which types can be implicitely casted...
* - improve error reporting (want to know the context of the cast)
* ("can't implicitely cast for argument 2 of function call...")
*/
dest_type = skip_typeref(dest_type);
type_t *from_type = from->base.type;
if (from_type == NULL) {
print_error_prefix(from->base.source_position);
fprintf(stderr, "can't implicitely cast from unknown type to ");
print_type(dest_type);
fprintf(stderr, "\n");
return NULL;
}
from_type = skip_typeref(from_type);
bool implicit_cast_allowed = true;
if (from_type->kind == TYPE_POINTER) {
if (dest_type->kind == TYPE_POINTER) {
pointer_type_t *p1 = (pointer_type_t*) from_type;
pointer_type_t *p2 = (pointer_type_t*) dest_type;
/* you can implicitely cast any pointer to void* and
* it is allowed to cast 'null' to any pointer */
if (p1->points_to == p2->points_to
|| dest_type == type_void_ptr
|| from->kind == EXPR_NULL_POINTER) {
/* fine */
} else if (is_type_array(p1->points_to)) {
array_type_t *array_type = (array_type_t*) p1->points_to;
if (array_type->element_type == p2->points_to) {
/* fine */
} else {
implicit_cast_allowed = false;
}
} else {
implicit_cast_allowed = false;
}
} else {
implicit_cast_allowed = false;
}
} else if (from_type->kind == TYPE_ARRAY) {
array_type_t *array_type = (array_type_t*) from_type;
if (dest_type->kind == TYPE_POINTER) {
pointer_type_t *pointer_type = (pointer_type_t*) dest_type;
/* we can cast to pointer of same type and void* */
if (pointer_type->points_to != array_type->element_type &&
dest_type != type_void_ptr) {
implicit_cast_allowed = false;
}
} else {
implicit_cast_allowed = false;
}
} else if (dest_type->kind == TYPE_POINTER) {
implicit_cast_allowed = false;
} else if (from_type->kind == TYPE_ATOMIC) {
if (dest_type->kind != TYPE_ATOMIC) {
implicit_cast_allowed = false;
} else {
atomic_type_t *from_type_atomic = &from_type->atomic;
atomic_type_kind_t from_akind = from_type_atomic->akind;
atomic_type_t *dest_type_atomic = &dest_type->atomic;
atomic_type_kind_t dest_akind = dest_type_atomic->akind;
switch (from_akind) {
case ATOMIC_TYPE_BOOL:
if (!lenient) {
implicit_cast_allowed = false;
break;
}
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_BYTE) ||
(dest_akind == ATOMIC_TYPE_UBYTE);
case ATOMIC_TYPE_UBYTE:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_USHORT) ||
(dest_akind == ATOMIC_TYPE_SHORT);
case ATOMIC_TYPE_USHORT:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_UINT) ||
(dest_akind == ATOMIC_TYPE_INT);
case ATOMIC_TYPE_UINT:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_ULONG) ||
(dest_akind == ATOMIC_TYPE_LONG);
case ATOMIC_TYPE_ULONG:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_ULONGLONG) ||
(dest_akind == ATOMIC_TYPE_LONGLONG);
break;
case ATOMIC_TYPE_BYTE:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_SHORT);
case ATOMIC_TYPE_SHORT:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_INT);
case ATOMIC_TYPE_INT:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_LONG);
case ATOMIC_TYPE_LONG:
implicit_cast_allowed |=
(dest_akind == ATOMIC_TYPE_LONGLONG);
break;
case ATOMIC_TYPE_FLOAT:
implicit_cast_allowed = (dest_akind == ATOMIC_TYPE_DOUBLE);
break;
case ATOMIC_TYPE_DOUBLE:
case ATOMIC_TYPE_LONGLONG:
case ATOMIC_TYPE_ULONGLONG:
case ATOMIC_TYPE_INVALID:
implicit_cast_allowed = false;
break;
}
}
}
if (!implicit_cast_allowed) {
print_error_prefix(source_position);
fprintf(stderr, "can't implicitely cast ");
print_type(from_type);
fprintf(stderr, " to ");
print_type(dest_type);
fprintf(stderr, "\n");
return NULL;
}
expression_t *cast = allocate_expression(EXPR_UNARY_CAST);
cast->base.source_position = source_position;
cast->base.type = dest_type;
cast->unary.value = from;
return cast;
}
static expression_t *lower_sub_expression(expression_t *expression)
{
binary_expression_t *sub = (binary_expression_t*) expression;
expression_t *left = check_expression(sub->left);
expression_t *right = check_expression(sub->right);
type_t *lefttype = left->base.type;
type_t *righttype = right->base.type;
if (lefttype->kind != TYPE_POINTER && righttype->kind != TYPE_POINTER)
return expression;
sub->base.type = type_uint;
pointer_type_t *p1 = (pointer_type_t*) lefttype;
expression_t *sizeof_expr = allocate_expression(EXPR_SIZEOF);
sizeof_expr->base.type = type_uint;
sizeof_expr->sizeofe.type = p1->points_to;
expression_t *divexpr = allocate_expression(EXPR_BINARY_DIV);
divexpr->base.type = type_uint;
divexpr->binary.left = expression;
divexpr->binary.right = sizeof_expr;
sub->base.lowered = true;
return divexpr;
}
static void check_binary_expression(binary_expression_t *binexpr)
{
binexpr->left = check_expression(binexpr->left);
binexpr->right = check_expression(binexpr->right);
expression_t *left = binexpr->left;
expression_t *right = binexpr->right;
type_t *exprtype;
type_t *lefttype, *righttype;
expression_kind_t kind = binexpr->base.kind;
switch (kind) {
case EXPR_BINARY_ASSIGN:
check_assign_expression(binexpr);
exprtype = left->base.type;
lefttype = exprtype;
righttype = exprtype;
break;
case EXPR_BINARY_ADD:
case EXPR_BINARY_SUB:
exprtype = left->base.type;
lefttype = exprtype;
righttype = right->base.type;
/* implement address arithmetic */
if (lefttype->kind == TYPE_POINTER && is_type_int(righttype)) {
pointer_type_t *pointer_type = (pointer_type_t*) lefttype;
expression_t *sizeof_expr = allocate_expression(EXPR_SIZEOF);
sizeof_expr->base.type = type_uint;
sizeof_expr->sizeofe.type = pointer_type->points_to;
expression_t *mulexpr = allocate_expression(EXPR_BINARY_MUL);
mulexpr->base.type = type_uint;
mulexpr->binary.left = make_cast(right, type_uint,
binexpr->base.source_position,
false);
mulexpr->binary.right = sizeof_expr;
expression_t *cast = allocate_expression(EXPR_UNARY_CAST);
cast->base.source_position = binexpr->base.source_position;
cast->base.type = lefttype;
cast->unary.value = mulexpr;
right = cast;
binexpr->right = cast;
}
if (lefttype->kind == TYPE_POINTER && righttype->kind == TYPE_POINTER) {
pointer_type_t *p1 = (pointer_type_t*) lefttype;
pointer_type_t *p2 = (pointer_type_t*) righttype;
if (p1->points_to != p2->points_to) {
print_error_prefix(binexpr->base.source_position);
fprintf(stderr, "Can only subtract pointers to same type, but have type ");
print_type(lefttype);
fprintf(stderr, " and ");
print_type(righttype);
fprintf(stderr, "\n");
}
exprtype = type_uint;
}
righttype = lefttype;
break;
case EXPR_BINARY_MUL:
case EXPR_BINARY_MOD:
case EXPR_BINARY_DIV:
if (!is_type_numeric(left->base.type)) {
print_error_prefix(binexpr->base.source_position);
fprintf(stderr, "Mul/Mod/Div expressions need a numeric type but "
"type ");
print_type(left->base.type);
fprintf(stderr, "is given\n");
}
exprtype = left->base.type;
lefttype = exprtype;
righttype = lefttype;
break;
case EXPR_BINARY_AND:
case EXPR_BINARY_OR:
case EXPR_BINARY_XOR:
if (!is_type_int(left->base.type)) {
print_error_prefix(binexpr->base.source_position);
fprintf(stderr, "And/Or/Xor expressions need an integer type "
"but type ");
print_type(left->base.type);
fprintf(stderr, "is given\n");
}
exprtype = left->base.type;
lefttype = exprtype;
righttype = left->base.type;
break;
case EXPR_BINARY_SHIFTLEFT:
case EXPR_BINARY_SHIFTRIGHT:
if (!is_type_int(left->base.type)) {
print_error_prefix(binexpr->base.source_position);
fprintf(stderr, "ShiftLeft/ShiftRight expressions need an integer "
"type, but type ");
print_type(left->base.type);
fprintf(stderr, "is given\n");
}
exprtype = left->base.type;
lefttype = exprtype;
righttype = type_uint;
break;
/* comparison operation */
case EXPR_BINARY_EQUAL:
case EXPR_BINARY_NOTEQUAL:
case EXPR_BINARY_LESS:
case EXPR_BINARY_LESSEQUAL:
case EXPR_BINARY_GREATER:
case EXPR_BINARY_GREATEREQUAL:
exprtype = type_bool;
/* TODO find out greatest common type... */
lefttype = left->base.type;
righttype = left->base.type;
break;
case EXPR_BINARY_LAZY_AND:
case EXPR_BINARY_LAZY_OR:
exprtype = type_bool;
lefttype = type_bool;
righttype = type_bool;
break;
default:
panic("invalid type in binexpr");
}
if (left == NULL || right == NULL)
return;
if (left->base.type != lefttype) {
binexpr->left = make_cast(left, lefttype,
binexpr->base.source_position,
false);
}
if (right->base.type != righttype) {
binexpr->right = make_cast(right, righttype,
binexpr->base.source_position,
false);
}
binexpr->base.type = exprtype;
}
/**
* find a concept instance matching the current type_variable configuration
*/
static concept_instance_t *_find_concept_instance(concept_t *concept,
const source_position_t *pos)
{
concept_instance_t *instance;
for ( instance = concept->instances; instance != NULL;
instance = instance->next_in_concept) {
assert(instance->concept == concept);
type_argument_t *argument = instance->type_arguments;
type_variable_t *parameter = concept->type_parameters;
bool match = true;
while (argument != NULL && parameter != NULL) {
if (parameter->current_type == NULL) {
print_error_prefix(*pos);
panic("type variable has no type set while searching "
"concept instance");
}
if (!match_variant_to_concrete_type(
argument->type, parameter->current_type,
concept->base.source_position, false)) {
match = false;
break;
}
argument = argument->next;
parameter = parameter->next;
}
if (match && (argument != NULL || parameter != NULL)) {
print_error_prefix(instance->source_position);
panic("type argument count of concept instance doesn't match "
"type parameter count of concept");
}
if (match)
break;
}
return instance;
}
concept_instance_t *find_concept_instance(concept_t *concept)
{
return _find_concept_instance(concept, NULL);
}
/** tests whether a type variable has a concept as constraint */
static bool type_variable_has_constraint(const type_variable_t *type_variable,
const concept_t *concept)
{
type_constraint_t *constraint = type_variable->constraints;
while (constraint != NULL) {
if (constraint->concept == concept)
return true;
constraint = constraint->next;
}
return false;
}
concept_function_instance_t *get_function_from_concept_instance(
concept_instance_t *instance, concept_function_t *function)
{
concept_function_instance_t *function_instance
= instance->function_instances;
while (function_instance != NULL) {
if (function_instance->concept_function == function) {
return function_instance;
}
function_instance = function_instance->next;
}
return NULL;
}
static void resolve_concept_function_instance(reference_expression_t *reference)
{
entity_t *entity = reference->entity;
assert(entity->kind == ENTITY_CONCEPT_FUNCTION);
concept_function_t *concept_function = &entity->concept_function;
concept_t *concept = concept_function->concept;
/* test whether 1 of the type variables points to another type variable.
* this can happen when concept functions are invoked inside polymorphic
* functions. We can't resolve the function right now, but we have to check
* the constraints of the type variable */
bool cant_resolve = false;
type_variable_t *type_var = concept->type_parameters;
while (type_var != NULL) {
type_t *current_type = type_var->current_type;
if (current_type == NULL)
return;
if (current_type->kind == TYPE_REFERENCE_TYPE_VARIABLE) {
type_reference_t *type_ref = (type_reference_t*) current_type;
type_variable_t *type_variable = type_ref->type_variable;
if (!type_variable_has_constraint(type_variable, concept)) {
print_error_prefix(reference->base.source_position);
fprintf(stderr, "type variable '%s' needs a constraint for "
"concept '%s' when using function '%s'.\n",
type_variable->base.symbol->string,
concept->base.symbol->string,
concept_function->base.symbol->string);
return;
}
cant_resolve = true;
}
type_var = type_var->next;
}
/* we have to defer the resolving for the ast2firm phase */
if (cant_resolve) {
return;
}
/* we assume that all typevars have current_type set */
const source_position_t *pos = &reference->base.source_position;
concept_instance_t *instance = _find_concept_instance(concept, pos);
if (instance == NULL) {
print_error_prefix(reference->base.source_position);
fprintf(stderr, "there's no instance of concept '%s' for type ",
concept->base.symbol->string);
type_variable_t *typevar = concept->type_parameters;
while (typevar != NULL) {
if (typevar->current_type != NULL) {
print_type(typevar->current_type);
fprintf(stderr, " ");
}
typevar = typevar->next;
}
fprintf(stderr, "\n");
return;
}
#if 0
concept_function_instance_t *function_instance
= get_function_from_concept_instance(instance, concept_function);