forked from libfirm/cparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.c
1698 lines (1491 loc) · 42.7 KB
/
type.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
/*
* This file is part of cparser.
* Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include "type_t.h"
#include "types.h"
#include "entity_t.h"
#include "symbol_t.h"
#include "type_hash.h"
#include "adt/error.h"
#include "adt/util.h"
#include "lang_features.h"
#include "warning.h"
#include "diagnostic.h"
#include "printer.h"
/** The default calling convention. */
cc_kind_t default_calling_convention = CC_CDECL;
static struct obstack type_obst;
static bool print_implicit_array_size = false;
static void intern_print_type_pre(const type_t *type);
static void intern_print_type_post(const type_t *type);
/**
* Returns the size of a type node.
*
* @param kind the type kind
*/
static size_t get_type_struct_size(type_kind_t kind)
{
static const size_t sizes[] = {
[TYPE_ATOMIC] = sizeof(atomic_type_t),
[TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
[TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
[TYPE_ENUM] = sizeof(enum_type_t),
[TYPE_FUNCTION] = sizeof(function_type_t),
[TYPE_POINTER] = sizeof(pointer_type_t),
[TYPE_REFERENCE] = sizeof(reference_type_t),
[TYPE_ARRAY] = sizeof(array_type_t),
[TYPE_TYPEDEF] = sizeof(typedef_type_t),
[TYPE_TYPEOF] = sizeof(typeof_type_t),
};
assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
assert(kind <= TYPE_TYPEOF);
assert(sizes[kind] != 0);
return sizes[kind];
}
type_t *allocate_type_zero(type_kind_t kind)
{
size_t const size = get_type_struct_size(kind);
type_t *const res = obstack_alloc(&type_obst, size);
memset(res, 0, size);
res->base.kind = kind;
return res;
}
/**
* Properties of atomic types.
*/
atomic_type_properties_t atomic_type_properties[ATOMIC_TYPE_LAST+1] = {
[ATOMIC_TYPE_VOID] = {
.size = 1,
.alignment = 1,
.flags = ATOMIC_TYPE_FLAG_NONE,
.rank = 0,
},
[ATOMIC_TYPE_BOOL] = {
.size = 1,
.alignment = 1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 1,
},
[ATOMIC_TYPE_CHAR] = {
.size = 1,
.alignment = 1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 2,
},
[ATOMIC_TYPE_SCHAR] = {
.size = 1,
.alignment = 1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 2,
},
[ATOMIC_TYPE_UCHAR] = {
.size = 1,
.alignment = 1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 2,
},
[ATOMIC_TYPE_SHORT] = {
.size = 2,
.alignment = 2,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 3,
},
[ATOMIC_TYPE_USHORT] = {
.size = 2,
.alignment = 2,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 3,
},
[ATOMIC_TYPE_INT] = {
.size = (unsigned) -1,
.alignment = (unsigned) -1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 4,
},
[ATOMIC_TYPE_UINT] = {
.size = (unsigned) -1,
.alignment = (unsigned) -1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 4,
},
[ATOMIC_TYPE_LONG] = {
.size = (unsigned) -1,
.alignment = (unsigned) -1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 5,
},
[ATOMIC_TYPE_ULONG] = {
.size = (unsigned) -1,
.alignment = (unsigned) -1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 5,
},
[ATOMIC_TYPE_LONGLONG] = {
.size = 8,
.alignment = 8,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 6,
},
[ATOMIC_TYPE_ULONGLONG] = {
.size = 8,
.alignment = 8,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = 6,
},
[ATOMIC_TYPE_FLOAT] = {
.size = 4,
.alignment = 4,
.flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 0,
},
[ATOMIC_TYPE_DOUBLE] = {
.size = 8,
.alignment = 8,
.flags = ATOMIC_TYPE_FLAG_FLOAT | ATOMIC_TYPE_FLAG_ARITHMETIC
| ATOMIC_TYPE_FLAG_SIGNED,
.rank = 0,
},
[ATOMIC_TYPE_WCHAR_T] = {
.size = (unsigned)-1,
.alignment = (unsigned)-1,
.flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC,
.rank = (unsigned)-1,
},
};
atomic_type_properties_t pointer_properties = {
.size = 4,
.alignment = 4,
.flags = ATOMIC_TYPE_FLAG_NONE,
};
static inline bool is_po2(unsigned x)
{
return (x & (x-1)) == 0;
}
void init_types(unsigned machine_size)
{
obstack_init(&type_obst);
atomic_type_properties_t *props = atomic_type_properties;
/* atempt to set some sane defaults based on machine size */
unsigned int_size = machine_size < 32 ? 2 : 4;
unsigned long_size = machine_size < 64 ? 4 : 8;
props[ATOMIC_TYPE_INT].size = int_size;
props[ATOMIC_TYPE_INT].alignment = int_size;
props[ATOMIC_TYPE_UINT].size = int_size;
props[ATOMIC_TYPE_UINT].alignment = int_size;
props[ATOMIC_TYPE_LONG].size = long_size;
props[ATOMIC_TYPE_LONG].alignment = long_size;
props[ATOMIC_TYPE_ULONG].size = long_size;
props[ATOMIC_TYPE_ULONG].alignment = long_size;
pointer_properties.size = long_size;
pointer_properties.alignment = long_size;
pointer_properties.struct_alignment = long_size;
props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
props[ATOMIC_TYPE_WCHAR_T] = props[ATOMIC_TYPE_INT];
/* set struct alignments to the same value as alignment */
for (size_t i = 0; i != lengthof(atomic_type_properties); ++i) {
props[i].struct_alignment = props[i].alignment;
}
}
void exit_types(void)
{
obstack_free(&type_obst, NULL);
}
void print_type_qualifiers(type_qualifiers_t const qualifiers, QualifierSeparators const q)
{
size_t sep = q & QUAL_SEP_START ? 0 : 1;
if (qualifiers & TYPE_QUALIFIER_CONST) {
print_string(" const" + sep);
sep = 0;
}
if (qualifiers & TYPE_QUALIFIER_VOLATILE) {
print_string(" volatile" + sep);
sep = 0;
}
if (qualifiers & TYPE_QUALIFIER_RESTRICT) {
print_string(" restrict" + sep);
sep = 0;
}
if (sep == 0 && q & QUAL_SEP_END)
print_char(' ');
}
const char *get_atomic_kind_name(atomic_type_kind_t kind)
{
switch(kind) {
case ATOMIC_TYPE_INVALID: break;
case ATOMIC_TYPE_VOID: return "void";
case ATOMIC_TYPE_WCHAR_T: return "wchar_t";
case ATOMIC_TYPE_BOOL: return c_mode & _CXX ? "bool" : "_Bool";
case ATOMIC_TYPE_CHAR: return "char";
case ATOMIC_TYPE_SCHAR: return "signed char";
case ATOMIC_TYPE_UCHAR: return "unsigned char";
case ATOMIC_TYPE_INT: return "int";
case ATOMIC_TYPE_UINT: return "unsigned int";
case ATOMIC_TYPE_SHORT: return "short";
case ATOMIC_TYPE_USHORT: return "unsigned short";
case ATOMIC_TYPE_LONG: return "long";
case ATOMIC_TYPE_ULONG: return "unsigned long";
case ATOMIC_TYPE_LONGLONG: return "long long";
case ATOMIC_TYPE_ULONGLONG: return "unsigned long long";
case ATOMIC_TYPE_LONG_DOUBLE: return "long double";
case ATOMIC_TYPE_FLOAT: return "float";
case ATOMIC_TYPE_DOUBLE: return "double";
}
return "INVALIDATOMIC";
}
/**
* Prints the name of an atomic type kinds.
*
* @param kind The type kind.
*/
static void print_atomic_kinds(atomic_type_kind_t kind)
{
const char *s = get_atomic_kind_name(kind);
print_string(s);
}
/**
* Prints the name of an atomic type.
*
* @param type The type.
*/
static void print_atomic_type(const atomic_type_t *type)
{
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
print_atomic_kinds(type->akind);
}
/**
* Prints the name of a complex type.
*
* @param type The type.
*/
static void print_complex_type(const atomic_type_t *type)
{
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
print_string("_Complex");
print_atomic_kinds(type->akind);
}
/**
* Prints the name of an imaginary type.
*
* @param type The type.
*/
static void print_imaginary_type(const atomic_type_t *type)
{
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
print_string("_Imaginary ");
print_atomic_kinds(type->akind);
}
/**
* Print the first part (the prefix) of a type.
*
* @param type The type to print.
*/
static void print_function_type_pre(const function_type_t *type)
{
switch (type->linkage) {
case LINKAGE_C:
if (c_mode & _CXX)
print_string("extern \"C\" ");
break;
case LINKAGE_CXX:
if (!(c_mode & _CXX))
print_string("extern \"C++\" ");
break;
}
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
intern_print_type_pre(type->return_type);
cc_kind_t cc = type->calling_convention;
restart:
switch (cc) {
case CC_CDECL: print_string(" __cdecl"); break;
case CC_STDCALL: print_string(" __stdcall"); break;
case CC_FASTCALL: print_string(" __fastcall"); break;
case CC_THISCALL: print_string(" __thiscall"); break;
case CC_DEFAULT:
if (default_calling_convention != CC_CDECL) {
/* show the default calling convention if its not cdecl */
cc = default_calling_convention;
goto restart;
}
break;
}
}
/**
* Print the second part (the postfix) of a type.
*
* @param type The type to print.
*/
static void print_function_type_post(const function_type_t *type,
const scope_t *parameters)
{
print_string("(");
bool first = true;
if (parameters == NULL) {
function_parameter_t *parameter = type->parameters;
for( ; parameter != NULL; parameter = parameter->next) {
if (first) {
first = false;
} else {
print_string(", ");
}
print_type(parameter->type);
}
} else {
entity_t *parameter = parameters->entities;
for (; parameter != NULL; parameter = parameter->base.next) {
if (parameter->kind != ENTITY_PARAMETER)
continue;
if (first) {
first = false;
} else {
print_string(", ");
}
const type_t *const param_type = parameter->declaration.type;
if (param_type == NULL) {
print_string(parameter->base.symbol->string);
} else {
print_type_ext(param_type, parameter->base.symbol, NULL);
}
}
}
if (type->variadic) {
if (first) {
first = false;
} else {
print_string(", ");
}
print_string("...");
}
if (first && !type->unspecified_parameters) {
print_string("void");
}
print_string(")");
intern_print_type_post(type->return_type);
}
/**
* Prints the prefix part of a pointer type.
*
* @param type The pointer type.
*/
static void print_pointer_type_pre(const pointer_type_t *type)
{
type_t const *const points_to = type->points_to;
intern_print_type_pre(points_to);
if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
print_string(" (");
variable_t *const variable = type->base_variable;
if (variable != NULL) {
print_string(" __based(");
print_string(variable->base.base.symbol->string);
print_string(") ");
}
print_string("*");
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_START);
}
/**
* Prints the postfix part of a pointer type.
*
* @param type The pointer type.
*/
static void print_pointer_type_post(const pointer_type_t *type)
{
type_t const *const points_to = type->points_to;
if (points_to->kind == TYPE_ARRAY || points_to->kind == TYPE_FUNCTION)
print_string(")");
intern_print_type_post(points_to);
}
/**
* Prints the prefix part of a reference type.
*
* @param type The reference type.
*/
static void print_reference_type_pre(const reference_type_t *type)
{
type_t const *const refers_to = type->refers_to;
intern_print_type_pre(refers_to);
if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
print_string(" (");
print_string("&");
}
/**
* Prints the postfix part of a reference type.
*
* @param type The reference type.
*/
static void print_reference_type_post(const reference_type_t *type)
{
type_t const *const refers_to = type->refers_to;
if (refers_to->kind == TYPE_ARRAY || refers_to->kind == TYPE_FUNCTION)
print_string(")");
intern_print_type_post(refers_to);
}
/**
* Prints the prefix part of an array type.
*
* @param type The array type.
*/
static void print_array_type_pre(const array_type_t *type)
{
intern_print_type_pre(type->element_type);
}
/**
* Prints the postfix part of an array type.
*
* @param type The array type.
*/
static void print_array_type_post(const array_type_t *type)
{
print_string("[");
if (type->is_static) {
print_string("static ");
}
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
if (type->size_expression != NULL
&& (print_implicit_array_size || !type->has_implicit_size)) {
print_expression(type->size_expression);
}
print_string("]");
intern_print_type_post(type->element_type);
}
void print_enum_definition(const enum_t *enume)
{
print_string("{\n");
change_indent(1);
entity_t *entry = enume->base.next;
for( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
entry = entry->base.next) {
print_indent();
print_string(entry->base.symbol->string);
if (entry->enum_value.value != NULL) {
print_string(" = ");
print_expression(entry->enum_value.value);
}
print_string(",\n");
}
change_indent(-1);
print_indent();
print_string("}");
}
/**
* Prints an enum type.
*
* @param type The enum type.
*/
static void print_type_enum(const enum_type_t *type)
{
print_type_qualifiers(type->base.base.qualifiers, QUAL_SEP_END);
print_string("enum ");
enum_t *enume = type->enume;
symbol_t *symbol = enume->base.symbol;
if (symbol != NULL) {
print_string(symbol->string);
} else {
print_enum_definition(enume);
}
}
void print_compound_definition(const compound_t *compound)
{
print_string("{\n");
change_indent(1);
entity_t *entity = compound->members.entities;
for( ; entity != NULL; entity = entity->base.next) {
if (entity->kind != ENTITY_COMPOUND_MEMBER)
continue;
print_indent();
print_entity(entity);
print_string("\n");
}
change_indent(-1);
print_indent();
print_string("}");
if (compound->modifiers & DM_TRANSPARENT_UNION) {
print_string("__attribute__((__transparent_union__))");
}
}
/**
* Prints a compound type.
*
* @param type The compound type.
*/
static void print_compound_type(const compound_type_t *type)
{
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
if (type->base.kind == TYPE_COMPOUND_STRUCT) {
print_string("struct ");
} else {
assert(type->base.kind == TYPE_COMPOUND_UNION);
print_string("union ");
}
compound_t *compound = type->compound;
symbol_t *symbol = compound->base.symbol;
if (symbol != NULL) {
print_string(symbol->string);
} else {
print_compound_definition(compound);
}
}
/**
* Prints the prefix part of a typedef type.
*
* @param type The typedef type.
*/
static void print_typedef_type_pre(const typedef_type_t *const type)
{
print_type_qualifiers(type->base.qualifiers, QUAL_SEP_END);
print_string(type->typedefe->base.symbol->string);
}
/**
* Prints the prefix part of a typeof type.
*
* @param type The typeof type.
*/
static void print_typeof_type_pre(const typeof_type_t *const type)
{
print_string("typeof(");
if (type->expression != NULL) {
print_expression(type->expression);
} else {
print_type(type->typeof_type);
}
print_string(")");
}
/**
* Prints the prefix part of a type.
*
* @param type The type.
*/
static void intern_print_type_pre(const type_t *const type)
{
switch(type->kind) {
case TYPE_ERROR:
print_string("<error>");
return;
case TYPE_ENUM:
print_type_enum(&type->enumt);
return;
case TYPE_ATOMIC:
print_atomic_type(&type->atomic);
return;
case TYPE_COMPLEX:
print_complex_type(&type->atomic);
return;
case TYPE_IMAGINARY:
print_imaginary_type(&type->atomic);
return;
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION:
print_compound_type(&type->compound);
return;
case TYPE_FUNCTION:
print_function_type_pre(&type->function);
return;
case TYPE_POINTER:
print_pointer_type_pre(&type->pointer);
return;
case TYPE_REFERENCE:
print_reference_type_pre(&type->reference);
return;
case TYPE_ARRAY:
print_array_type_pre(&type->array);
return;
case TYPE_TYPEDEF:
print_typedef_type_pre(&type->typedeft);
return;
case TYPE_TYPEOF:
print_typeof_type_pre(&type->typeoft);
return;
}
print_string("unknown");
}
/**
* Prints the postfix part of a type.
*
* @param type The type.
*/
static void intern_print_type_post(const type_t *const type)
{
switch(type->kind) {
case TYPE_FUNCTION:
print_function_type_post(&type->function, NULL);
return;
case TYPE_POINTER:
print_pointer_type_post(&type->pointer);
return;
case TYPE_REFERENCE:
print_reference_type_post(&type->reference);
return;
case TYPE_ARRAY:
print_array_type_post(&type->array);
return;
case TYPE_ERROR:
case TYPE_ATOMIC:
case TYPE_COMPLEX:
case TYPE_IMAGINARY:
case TYPE_ENUM:
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION:
case TYPE_TYPEOF:
case TYPE_TYPEDEF:
break;
}
}
void print_type(const type_t *const type)
{
print_type_ext(type, NULL, NULL);
}
void print_type_ext(const type_t *const type, const symbol_t *symbol,
const scope_t *parameters)
{
intern_print_type_pre(type);
if (symbol != NULL) {
print_string(" ");
print_string(symbol->string);
}
if (type->kind == TYPE_FUNCTION) {
print_function_type_post(&type->function, parameters);
} else {
intern_print_type_post(type);
}
}
type_t *duplicate_type(const type_t *type)
{
size_t size = get_type_struct_size(type->kind);
type_t *const copy = obstack_alloc(&type_obst, size);
memcpy(copy, type, size);
copy->base.firm_type = NULL;
return copy;
}
type_t *get_unqualified_type(type_t *type)
{
assert(!is_typeref(type));
if (type->base.qualifiers == TYPE_QUALIFIER_NONE)
return type;
type_t *unqualified_type = duplicate_type(type);
unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
return identify_new_type(unqualified_type);
}
type_t *get_qualified_type(type_t *orig_type, type_qualifiers_t const qual)
{
type_t *type = skip_typeref(orig_type);
type_t *copy;
if (is_type_array(type)) {
/* For array types the element type has to be adjusted */
type_t *element_type = type->array.element_type;
type_t *qual_element_type = get_qualified_type(element_type, qual);
if (qual_element_type == element_type)
return orig_type;
copy = duplicate_type(type);
copy->array.element_type = qual_element_type;
} else if (is_type_valid(type)) {
if ((type->base.qualifiers & qual) == (int)qual)
return orig_type;
copy = duplicate_type(type);
copy->base.qualifiers |= qual;
} else {
return type;
}
return identify_new_type(copy);
}
static bool test_atomic_type_flag(atomic_type_kind_t kind,
atomic_type_flag_t flag)
{
assert(kind <= ATOMIC_TYPE_LAST);
return (atomic_type_properties[kind].flags & flag) != 0;
}
bool is_type_integer(const type_t *type)
{
assert(!is_typeref(type));
if (type->kind == TYPE_ENUM)
return true;
if (type->kind != TYPE_ATOMIC)
return false;
return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_INTEGER);
}
bool is_type_enum(const type_t *type)
{
assert(!is_typeref(type));
return type->kind == TYPE_ENUM;
}
bool is_type_float(const type_t *type)
{
assert(!is_typeref(type));
if (type->kind != TYPE_ATOMIC)
return false;
return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_FLOAT);
}
bool is_type_complex(const type_t *type)
{
assert(!is_typeref(type));
if (type->kind != TYPE_ATOMIC)
return false;
return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_COMPLEX);
}
bool is_type_signed(const type_t *type)
{
assert(!is_typeref(type));
/* enum types are int for now */
if (type->kind == TYPE_ENUM)
return true;
if (type->kind != TYPE_ATOMIC)
return false;
return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_SIGNED);
}
bool is_type_arithmetic(const type_t *type)
{
assert(!is_typeref(type));
switch(type->kind) {
case TYPE_ENUM:
return true;
case TYPE_ATOMIC:
case TYPE_COMPLEX:
case TYPE_IMAGINARY:
return test_atomic_type_flag(type->atomic.akind, ATOMIC_TYPE_FLAG_ARITHMETIC);
default:
return false;
}
}
bool is_type_real(const type_t *type)
{
/* 6.2.5 (17) */
return is_type_integer(type) || is_type_float(type);
}
bool is_type_scalar(const type_t *type)
{
assert(!is_typeref(type));
if (type->kind == TYPE_POINTER)
return true;
return is_type_arithmetic(type);
}
bool is_type_incomplete(const type_t *type)
{
assert(!is_typeref(type));
switch(type->kind) {
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION: {
const compound_type_t *compound_type = &type->compound;
return !compound_type->compound->complete;
}
case TYPE_ENUM:
return false;
case TYPE_ARRAY:
return type->array.size_expression == NULL
&& !type->array.size_constant;
case TYPE_ATOMIC:
case TYPE_IMAGINARY:
case TYPE_COMPLEX:
return type->atomic.akind == ATOMIC_TYPE_VOID;
case TYPE_FUNCTION:
case TYPE_POINTER:
case TYPE_REFERENCE:
case TYPE_ERROR:
return false;
case TYPE_TYPEDEF:
case TYPE_TYPEOF:
panic("is_type_incomplete called without typerefs skipped");
}
panic("invalid type found");
}
bool is_type_object(const type_t *type)
{
return !is_type_function(type) && !is_type_incomplete(type);
}
/**
* Check if two function types are compatible.
*/
static bool function_types_compatible(const function_type_t *func1,
const function_type_t *func2)
{
const type_t* const ret1 = skip_typeref(func1->return_type);
const type_t* const ret2 = skip_typeref(func2->return_type);
if (!types_compatible(ret1, ret2))
return false;
if (func1->linkage != func2->linkage)
return false;
cc_kind_t cc1 = func1->calling_convention;
if (cc1 == CC_DEFAULT)
cc1 = default_calling_convention;
cc_kind_t cc2 = func2->calling_convention;
if (cc2 == CC_DEFAULT)
cc2 = default_calling_convention;
if (cc1 != cc2)
return false;
if (func1->variadic != func2->variadic)
return false;
/* can parameters be compared? */
if ((func1->unspecified_parameters && !func1->kr_style_parameters)
|| (func2->unspecified_parameters && !func2->kr_style_parameters))
return true;
/* TODO: handling of unspecified parameters not correct yet */
/* all argument types must be compatible */
function_parameter_t *parameter1 = func1->parameters;
function_parameter_t *parameter2 = func2->parameters;
for ( ; parameter1 != NULL && parameter2 != NULL;
parameter1 = parameter1->next, parameter2 = parameter2->next) {
type_t *parameter1_type = skip_typeref(parameter1->type);
type_t *parameter2_type = skip_typeref(parameter2->type);
parameter1_type = get_unqualified_type(parameter1_type);
parameter2_type = get_unqualified_type(parameter2_type);
if (!types_compatible(parameter1_type, parameter2_type))
return false;
}
/* same number of arguments? */
if (parameter1 != NULL || parameter2 != NULL)
return false;
return true;
}
/**
* Check if two array types are compatible.
*/
static bool array_types_compatible(const array_type_t *array1,
const array_type_t *array2)
{
type_t *element_type1 = skip_typeref(array1->element_type);
type_t *element_type2 = skip_typeref(array2->element_type);
if (!types_compatible(element_type1, element_type2))
return false;
if (!array1->size_constant || !array2->size_constant)
return true;
return array1->size == array2->size;
}
bool types_compatible(const type_t *type1, const type_t *type2)
{
assert(!is_typeref(type1));
assert(!is_typeref(type2));
/* shortcut: the same type is always compatible */
if (type1 == type2)
return true;
if (!is_type_valid(type1) || !is_type_valid(type2))
return true;