2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include "expression.h"
41 * Secondary symbol list for stuff that needs to be output because it
44 struct symbol_list
*translation_unit_used_list
= NULL
;
47 * If the symbol is an inline symbol, add it to the list of symbols to parse
49 void access_symbol(struct symbol
*sym
)
51 if (sym
->ctype
.modifiers
& MOD_INLINE
) {
53 add_symbol(&translation_unit_used_list
, sym
);
59 struct symbol
*lookup_symbol(struct ident
*ident
, enum namespace ns
)
63 for (sym
= ident
->symbols
; sym
; sym
= sym
->next_id
) {
64 if (sym
->namespace & ns
) {
72 struct context
*alloc_context(void)
74 return __alloc_context(0);
77 struct symbol
*alloc_symbol(struct position pos
, int type
)
79 struct symbol
*sym
= __alloc_symbol(0);
86 struct struct_union_info
{
87 unsigned long max_align
;
88 unsigned long bit_size
;
93 * Unions are fairly easy to lay out ;)
95 static void lay_out_union(struct symbol
*sym
, struct struct_union_info
*info
)
97 examine_symbol_type(sym
);
99 // Unnamed bitfields do not affect alignment.
100 if (sym
->ident
|| !is_bitfield_type(sym
)) {
101 if (sym
->ctype
.alignment
> info
->max_align
)
102 info
->max_align
= sym
->ctype
.alignment
;
105 if (sym
->bit_size
> info
->bit_size
)
106 info
->bit_size
= sym
->bit_size
;
111 static int bitfield_base_size(struct symbol
*sym
)
113 if (sym
->type
== SYM_NODE
)
114 sym
= sym
->ctype
.base_type
;
115 if (sym
->type
== SYM_BITFIELD
)
116 sym
= sym
->ctype
.base_type
;
117 return sym
->bit_size
;
121 * Structures are a bit more interesting to lay out
123 static void lay_out_struct(struct symbol
*sym
, struct struct_union_info
*info
)
125 unsigned long bit_size
, align_bit_mask
;
128 examine_symbol_type(sym
);
130 // Unnamed bitfields do not affect alignment.
131 if (sym
->ident
|| !is_bitfield_type(sym
)) {
132 if (sym
->ctype
.alignment
> info
->max_align
)
133 info
->max_align
= sym
->ctype
.alignment
;
136 bit_size
= info
->bit_size
;
137 base_size
= sym
->bit_size
;
140 * Unsized arrays cause us to not align the resulting
144 info
->align_size
= 0;
148 align_bit_mask
= bytes_to_bits(sym
->ctype
.alignment
) - 1;
151 * Bitfields have some very special rules..
153 if (is_bitfield_type (sym
)) {
154 unsigned long bit_offset
= bit_size
& align_bit_mask
;
155 int room
= bitfield_base_size(sym
) - bit_offset
;
156 // Zero-width fields just fill up the unit.
157 int width
= base_size
? : (bit_offset
? room
: 0);
160 bit_size
= (bit_size
+ align_bit_mask
) & ~align_bit_mask
;
163 sym
->offset
= bits_to_bytes(bit_size
- bit_offset
);
164 sym
->bit_offset
= bit_offset
;
165 sym
->ctype
.base_type
->bit_offset
= bit_offset
;
166 info
->bit_size
= bit_size
+ width
;
167 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
173 * Otherwise, just align it right and add it up..
175 bit_size
= (bit_size
+ align_bit_mask
) & ~align_bit_mask
;
176 sym
->offset
= bits_to_bytes(bit_size
);
178 info
->bit_size
= bit_size
+ base_size
;
179 // warning (sym->pos, "regular: offset=%d", sym->offset);
182 static struct symbol
* examine_struct_union_type(struct symbol
*sym
, int advance
)
184 struct struct_union_info info
= {
189 unsigned long bit_size
, bit_align
;
190 void (*fn
)(struct symbol
*, struct struct_union_info
*);
191 struct symbol
*member
;
193 fn
= advance
? lay_out_struct
: lay_out_union
;
194 FOR_EACH_PTR(sym
->symbol_list
, member
) {
195 if (member
->ctype
.base_type
== &autotype_ctype
) {
196 sparse_error(member
->pos
, "member '%s' has __auto_type", show_ident(member
->ident
));
197 member
->ctype
.base_type
= &incomplete_ctype
;
200 } END_FOR_EACH_PTR(member
);
202 if (!sym
->ctype
.alignment
)
203 sym
->ctype
.alignment
= info
.max_align
;
204 bit_size
= info
.bit_size
;
205 if (info
.align_size
) {
206 bit_align
= bytes_to_bits(sym
->ctype
.alignment
)-1;
207 bit_size
= (bit_size
+ bit_align
) & ~bit_align
;
209 sym
->bit_size
= bit_size
;
213 static struct symbol
*examine_base_type(struct symbol
*sym
)
215 struct symbol
*base_type
;
217 if (sym
->ctype
.base_type
== &autotype_ctype
) {
218 struct symbol
*type
= evaluate_expression(sym
->initializer
);
221 if (is_bitfield_type(type
)) {
222 warning(sym
->pos
, "__auto_type on bitfield");
223 if (type
->type
== SYM_NODE
)
224 type
= type
->ctype
.base_type
;
225 type
= type
->ctype
.base_type
;
227 sym
->ctype
.base_type
= type
;
230 /* Check the base type */
231 base_type
= examine_symbol_type(sym
->ctype
.base_type
);
232 if (!base_type
|| base_type
->type
== SYM_PTR
)
234 combine_address_space(sym
->pos
, &sym
->ctype
.as
, base_type
->ctype
.as
);
235 sym
->ctype
.modifiers
|= base_type
->ctype
.modifiers
& MOD_PTRINHERIT
;
236 concat_ptr_list((struct ptr_list
*)base_type
->ctype
.contexts
,
237 (struct ptr_list
**)&sym
->ctype
.contexts
);
238 if (base_type
->type
== SYM_NODE
) {
239 base_type
= base_type
->ctype
.base_type
;
240 sym
->ctype
.base_type
= base_type
;
241 sym
->rank
= base_type
->rank
;
246 static struct symbol
* examine_array_type(struct symbol
*sym
)
248 struct symbol
*base_type
= examine_base_type(sym
);
249 unsigned long bit_size
= -1, alignment
;
250 struct expression
*array_size
= sym
->array_size
;
256 bit_size
= array_element_offset(base_type
->bit_size
,
257 get_expression_value_silent(array_size
));
258 if (array_size
->type
!= EXPR_VALUE
) {
260 warning(array_size
->pos
, "Variable length array is used.");
264 alignment
= base_type
->ctype
.alignment
;
265 if (!sym
->ctype
.alignment
)
266 sym
->ctype
.alignment
= alignment
;
267 sym
->bit_size
= bit_size
;
271 static struct symbol
*examine_bitfield_type(struct symbol
*sym
)
273 struct symbol
*base_type
= examine_base_type(sym
);
274 unsigned long alignment
, modifiers
;
278 if (sym
->bit_size
> base_type
->bit_size
) {
279 sparse_error(sym
->pos
, "bitfield '%s' is wider (%d) than its type (%s)",
280 show_ident(sym
->ident
), sym
->bit_size
, show_typename(base_type
));
284 alignment
= base_type
->ctype
.alignment
;
285 if (!sym
->ctype
.alignment
)
286 sym
->ctype
.alignment
= alignment
;
287 modifiers
= base_type
->ctype
.modifiers
;
289 /* Bitfields are unsigned, unless the base type was explicitly signed */
290 if (!(modifiers
& MOD_EXPLICITLY_SIGNED
))
291 modifiers
= (modifiers
& ~MOD_SIGNED
) | MOD_UNSIGNED
;
292 sym
->ctype
.modifiers
|= modifiers
& MOD_SIGNEDNESS
;
297 * "typeof" will have to merge the types together
299 void merge_type(struct symbol
*sym
, struct symbol
*base_type
)
301 combine_address_space(sym
->pos
, &sym
->ctype
.as
, base_type
->ctype
.as
);
302 sym
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& ~MOD_STORAGE
);
303 concat_ptr_list((struct ptr_list
*)base_type
->ctype
.contexts
,
304 (struct ptr_list
**)&sym
->ctype
.contexts
);
305 sym
->ctype
.base_type
= base_type
->ctype
.base_type
;
306 if (sym
->ctype
.base_type
->type
== SYM_NODE
)
307 merge_type(sym
, sym
->ctype
.base_type
);
310 static bool is_wstring_expr(struct expression
*expr
)
313 switch (expr
->type
) {
316 case EXPR_INITIALIZER
:
317 if (expression_list_size(expr
->expr_list
) != 1)
319 expr
= first_expression(expr
->expr_list
);
322 if (expr
->op
== '(') {
333 static int count_array_initializer(struct symbol
*t
, struct expression
*expr
)
339 * Arrays of character types are special; they can be initialized by
340 * string literal _or_ by string literal in braces. The latter means
341 * that with T x[] = {<string literal>} number of elements in x depends
342 * on T - if it's a character type, we get the length of string literal
343 * (including NUL), otherwise we have one element here.
345 if (t
->ctype
.base_type
== &int_type
&& t
->rank
== -2)
347 else if (t
== wchar_ctype
&& is_wstring_expr(expr
))
350 switch (expr
->type
) {
351 case EXPR_INITIALIZER
: {
352 struct expression
*entry
;
355 FOR_EACH_PTR(expr
->expr_list
, entry
) {
357 switch (entry
->type
) {
359 if (entry
->idx_to
>= nr
)
360 nr
= entry
->idx_to
+1;
363 struct expression
*e
= entry
;
365 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
367 if (e
&& e
->type
== EXPR_STRING
) {
371 str_len
= entry
->string
->length
;
380 } END_FOR_EACH_PTR(entry
);
381 if (count
== 1 && str_len
)
387 struct expression
*e
= expr
;
388 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
390 if (e
&& e
->type
== EXPR_STRING
) {
394 nr
= expr
->string
->length
;
404 static struct expression
*get_symbol_initializer(struct symbol
*sym
)
407 if (sym
->initializer
)
408 return sym
->initializer
;
409 } while ((sym
= sym
->same_symbol
) != NULL
);
413 static unsigned int implicit_array_size(struct symbol
*node
, unsigned int count
)
415 struct symbol
*arr_ori
= node
->ctype
.base_type
;
416 struct symbol
*arr_new
= alloc_symbol(node
->pos
, SYM_ARRAY
);
417 struct symbol
*elem_type
= arr_ori
->ctype
.base_type
;
418 struct expression
*size
= alloc_const_expression(node
->pos
, count
);
419 unsigned int bit_size
= array_element_offset(elem_type
->bit_size
, count
);
422 arr_new
->bit_size
= bit_size
;
423 arr_new
->array_size
= size
;
424 node
->array_size
= size
;
425 node
->ctype
.base_type
= arr_new
;
430 static struct symbol
* examine_node_type(struct symbol
*sym
)
432 struct symbol
*base_type
= examine_base_type(sym
);
434 unsigned long alignment
;
436 /* SYM_NODE - figure out what the type of the node was.. */
442 bit_size
= base_type
->bit_size
;
443 alignment
= base_type
->ctype
.alignment
;
445 /* Pick up signedness information into the node */
446 sym
->ctype
.modifiers
|= (MOD_SIGNEDNESS
& base_type
->ctype
.modifiers
);
448 if (!sym
->ctype
.alignment
)
449 sym
->ctype
.alignment
= alignment
;
451 /* Unsized array? The size might come from the initializer.. */
452 if (bit_size
< 0 && base_type
->type
== SYM_ARRAY
) {
453 struct expression
*initializer
= get_symbol_initializer(sym
);
455 struct symbol
*node_type
= base_type
->ctype
.base_type
;
456 int count
= count_array_initializer(node_type
, initializer
);
458 if (node_type
&& node_type
->bit_size
>= 0)
459 bit_size
= implicit_array_size(sym
, count
);
463 sym
->bit_size
= bit_size
;
464 sym
->rank
= base_type
->rank
;
468 static struct symbol
*examine_enum_type(struct symbol
*sym
)
470 struct symbol
*base_type
= examine_base_type(sym
);
472 sym
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_SIGNEDNESS
);
473 sym
->bit_size
= bits_in_enum
;
474 if (base_type
->bit_size
> sym
->bit_size
)
475 sym
->bit_size
= base_type
->bit_size
;
476 sym
->ctype
.alignment
= enum_alignment
;
477 if (base_type
->ctype
.alignment
> sym
->ctype
.alignment
)
478 sym
->ctype
.alignment
= base_type
->ctype
.alignment
;
482 static struct symbol
*examine_pointer_type(struct symbol
*sym
)
485 * Since pointers to incomplete types can be used,
486 * for example in a struct-declaration-list,
487 * the base type must *not* be examined here.
488 * It thus means that it needs to be done later,
489 * when the base type of the pointer is looked at.
492 sym
->bit_size
= bits_in_pointer
;
493 if (!sym
->ctype
.alignment
)
494 sym
->ctype
.alignment
= pointer_alignment
;
498 static struct symbol
*examine_typeof(struct symbol
*sym
)
500 struct symbol
*base
= evaluate_expression(sym
->initializer
);
501 unsigned long mod
= 0;
505 if (base
->type
== SYM_NODE
) {
506 mod
|= base
->ctype
.modifiers
& MOD_TYPEOF
;
507 base
= base
->ctype
.base_type
;
509 if (base
->type
== SYM_BITFIELD
)
510 warning(base
->pos
, "typeof applied to bitfield type");
511 sym
->type
= SYM_NODE
;
512 sym
->ctype
.modifiers
= mod
;
513 sym
->ctype
.base_type
= base
;
514 return examine_node_type(sym
);
518 * Fill in type size and alignment information for
519 * regular SYM_TYPE things.
521 struct symbol
*examine_symbol_type(struct symbol
* sym
)
534 return examine_node_type(sym
);
536 return examine_array_type(sym
);
538 return examine_struct_union_type(sym
, 1);
540 return examine_struct_union_type(sym
, 0);
542 return examine_pointer_type(sym
);
544 return examine_enum_type(sym
);
546 return examine_bitfield_type(sym
);
548 /* Size and alignment had better already be set up */
551 return examine_typeof(sym
);
552 case SYM_PREPROCESSOR
:
553 sparse_error(sym
->pos
, "ctype on preprocessor command? (%s)", show_ident(sym
->ident
));
555 case SYM_UNINITIALIZED
:
556 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
559 examine_base_type(sym
);
562 examine_base_type(sym
);
565 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
571 const char* get_type_name(enum type type
)
573 const char *type_lookup
[] = {
574 [SYM_UNINITIALIZED
] = "uninitialized",
575 [SYM_PREPROCESSOR
] = "preprocessor",
576 [SYM_BASETYPE
] = "basetype",
578 [SYM_PTR
] = "pointer",
579 [SYM_FN
] = "function",
580 [SYM_ARRAY
] = "array",
581 [SYM_STRUCT
] = "struct",
582 [SYM_UNION
] = "union",
584 [SYM_TYPEOF
] = "typeof",
585 [SYM_BITFIELD
] = "bitfield",
586 [SYM_LABEL
] = "label",
587 [SYM_RESTRICT
] = "restrict",
588 [SYM_FOULED
] = "fouled",
589 [SYM_KEYWORD
] = "keyword",
593 return type_lookup
[type
];
598 struct symbol
*examine_pointer_target(struct symbol
*sym
)
600 return examine_base_type(sym
);
603 static struct symbol_list
*restr
, *fouled
;
605 void create_fouled(struct symbol
*type
)
607 if (type
->bit_size
< bits_in_int
) {
608 struct symbol
*new = alloc_symbol(type
->pos
, type
->type
);
610 new->bit_size
= bits_in_int
;
612 new->type
= SYM_FOULED
;
613 new->ctype
.base_type
= type
;
614 add_symbol(&restr
, type
);
615 add_symbol(&fouled
, new);
619 struct symbol
*befoul(struct symbol
*type
)
621 struct symbol
*t1
, *t2
;
622 while (type
->type
== SYM_NODE
)
623 type
= type
->ctype
.base_type
;
624 PREPARE_PTR_LIST(restr
, t1
);
625 PREPARE_PTR_LIST(fouled
, t2
);
639 static void inherit_declaration(struct symbol
*sym
, struct symbol
*prev
)
641 unsigned long mods
= prev
->ctype
.modifiers
;
643 // inherit function attributes
644 sym
->ctype
.modifiers
|= mods
& MOD_FUN_ATTR
;
647 void check_declaration(struct symbol
*sym
)
650 struct symbol
*next
= sym
;
652 while ((next
= next
->next_id
) != NULL
) {
653 if (next
->namespace != sym
->namespace)
655 if (sym
->scope
== next
->scope
) {
656 sym
->same_symbol
= next
;
657 inherit_declaration(sym
, next
);
660 /* Extern in block level matches a TOPLEVEL non-static symbol */
661 if (sym
->ctype
.modifiers
& MOD_EXTERN
) {
662 if ((next
->ctype
.modifiers
& (MOD_TOPLEVEL
|MOD_STATIC
)) == MOD_TOPLEVEL
) {
663 sym
->same_symbol
= next
;
668 if (!Wshadow
|| warned
)
670 if (get_sym_type(next
) == SYM_FN
)
673 warning(sym
->pos
, "symbol '%s' shadows an earlier one", show_ident(sym
->ident
));
674 info(next
->pos
, "originally declared here");
678 static void inherit_static(struct symbol
*sym
)
682 // only 'plain' symbols are concerned
683 if (sym
->ctype
.modifiers
& (MOD_STATIC
|MOD_EXTERN
))
686 for (prev
= sym
->next_id
; prev
; prev
= prev
->next_id
) {
687 if (prev
->namespace != NS_SYMBOL
)
689 if (prev
->scope
!= file_scope
)
692 sym
->ctype
.modifiers
|= prev
->ctype
.modifiers
& MOD_STATIC
;
694 // previous declarations are already converted
699 void bind_symbol_with_scope(struct symbol
*sym
, struct ident
*ident
, enum namespace ns
, struct scope
*scope
)
702 sparse_error(sym
->pos
, "internal error: symbol type already bound");
705 if (ident
->reserved
&& (ns
& (NS_TYPEDEF
| NS_STRUCT
| NS_LABEL
| NS_SYMBOL
))) {
706 sparse_error(sym
->pos
, "Trying to use reserved word '%s' as identifier", show_ident(ident
));
710 sym
->next_id
= ident
->symbols
;
711 ident
->symbols
= sym
;
712 if (sym
->ident
&& sym
->ident
!= ident
)
713 warning(sym
->pos
, "Symbol '%s' already bound", show_ident(sym
->ident
));
717 if (ns
== NS_SYMBOL
&& toplevel(scope
)) {
718 unsigned mod
= MOD_ADDRESSABLE
| MOD_TOPLEVEL
;
722 scope
= global_scope
;
723 if (sym
->ctype
.modifiers
& MOD_STATIC
||
724 is_extern_inline(sym
)) {
728 sym
->ctype
.modifiers
|= mod
;
730 bind_scope(sym
, scope
);
733 void bind_symbol(struct symbol
*sym
, struct ident
*ident
, enum namespace ns
)
735 struct scope
*scope
= block_scope
;;
740 scope
= function_scope
;
741 bind_symbol_with_scope(sym
, ident
, ns
, scope
);
744 struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace)
746 struct ident
*ident
= built_in_ident(name
);
747 struct symbol
*sym
= lookup_symbol(ident
, namespace);
749 if (sym
&& sym
->type
!= type
)
750 die("symbol %s created with different types: %d old %d", name
,
754 struct token
*token
= built_in_token(stream
, ident
);
756 sym
= alloc_symbol(token
->pos
, type
);
757 bind_symbol(sym
, token
->ident
, namespace);
766 struct symbol int_type
,
770 * C types (i.e. actual instances that the abstract types
773 struct symbol bool_ctype
, void_ctype
, type_ctype
,
774 char_ctype
, schar_ctype
, uchar_ctype
,
775 short_ctype
, sshort_ctype
, ushort_ctype
,
776 int_ctype
, sint_ctype
, uint_ctype
,
777 long_ctype
, slong_ctype
, ulong_ctype
,
778 llong_ctype
, sllong_ctype
, ullong_ctype
,
779 int128_ctype
, sint128_ctype
, uint128_ctype
,
780 float_ctype
, double_ctype
, ldouble_ctype
,
781 string_ctype
, ptr_ctype
, lazy_ptr_ctype
,
782 incomplete_ctype
, label_ctype
, bad_ctype
,
784 struct symbol autotype_ctype
;
785 struct symbol int_ptr_ctype
, uint_ptr_ctype
;
786 struct symbol long_ptr_ctype
, ulong_ptr_ctype
;
787 struct symbol llong_ptr_ctype
, ullong_ptr_ctype
;
788 struct symbol float32_ctype
, float32x_ctype
;
789 struct symbol float64_ctype
, float64x_ctype
;
790 struct symbol float128_ctype
;
791 struct symbol const_void_ctype
, const_char_ctype
;
792 struct symbol const_ptr_ctype
, const_string_ctype
;
794 struct symbol zero_int
;
796 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
797 #define __IDENT(n,str,res) \
798 struct ident n = __INIT_IDENT(str,res)
800 #include "ident-list.h"
802 void init_symbols(void)
804 int stream
= init_stream(NULL
, "builtin", -1, includepath
);
806 #define __IDENT(n,str,res) \
808 #include "ident-list.h"
813 #ifdef __CHAR_UNSIGNED__
814 #define CHAR_SIGNEDNESS MOD_UNSIGNED
816 #define CHAR_SIGNEDNESS MOD_SIGNED
818 // For fix-sized types
819 static int bits_in_type32
= 32;
820 static int bits_in_type64
= 64;
821 static int bits_in_type128
= 128;
823 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
824 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
825 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
826 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
827 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
828 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
829 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
830 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
831 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
832 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
834 static const struct ctype_declare
{
837 unsigned long modifiers
;
841 struct symbol
*base_type
;
842 } ctype_declaration
[] = {
843 { &bool_ctype
, T_INT(-3, bool, MOD_UNSIGNED
) },
844 { &void_ctype
, T_BASETYPE
},
845 { &type_ctype
, T_BASETYPE
},
846 { &incomplete_ctype
, T_BASETYPE
},
847 { &autotype_ctype
, T_BASETYPE
},
848 { &bad_ctype
, T_BASETYPE
},
850 { &char_ctype
, T_INT(-2, char, CHAR_SIGNEDNESS
) },
851 { &schar_ctype
, T_SINT(-2, char) },
852 { &uchar_ctype
, T_UINT(-2, char) },
853 { &short_ctype
, T__INT(-1, short) },
854 { &sshort_ctype
, T_SINT(-1, short) },
855 { &ushort_ctype
, T_UINT(-1, short) },
856 { &int_ctype
, T__INT( 0, int) },
857 { &sint_ctype
, T_SINT( 0, int) },
858 { &uint_ctype
, T_UINT( 0, int) },
859 { &long_ctype
, T__INT( 1, long) },
860 { &slong_ctype
, T_SINT( 1, long) },
861 { &ulong_ctype
, T_UINT( 1, long) },
862 { &llong_ctype
, T__INT( 2, longlong
) },
863 { &sllong_ctype
, T_SINT( 2, longlong
) },
864 { &ullong_ctype
, T_UINT( 2, longlong
) },
865 { &int128_ctype
, T__INT( 3, type128
) },
866 { &sint128_ctype
, T_SINT( 3, type128
) },
867 { &uint128_ctype
, T_UINT( 3, type128
) },
869 { &float_ctype
, T_FLOAT(-1, float) },
870 { &double_ctype
, T_FLOAT( 0, double) },
871 { &ldouble_ctype
, T_FLOAT( 1, longdouble
) },
873 { &float32_ctype
, T_FLOAT(-1, type32
) },
874 { &float32x_ctype
, T_FLOAT(-1, double) },
875 { &float64_ctype
, T_FLOAT( 0, type64
) },
876 { &float64x_ctype
, T_FLOAT( 1, longdouble
) },
877 { &float128_ctype
, T_FLOAT_(2, type128
, &max_alignment
) },
879 { &string_ctype
, T_PTR(&char_ctype
) },
880 { &ptr_ctype
, T_PTR(&void_ctype
) },
881 { &null_ctype
, T_PTR(&void_ctype
) },
882 { &label_ctype
, T_PTR(&void_ctype
) },
883 { &lazy_ptr_ctype
, T_PTR(&void_ctype
) },
884 { &int_ptr_ctype
, T_PTR(&int_ctype
) },
885 { &uint_ptr_ctype
, T_PTR(&uint_ctype
) },
886 { &long_ptr_ctype
, T_PTR(&long_ctype
) },
887 { &ulong_ptr_ctype
, T_PTR(&ulong_ctype
) },
888 { &llong_ptr_ctype
, T_PTR(&llong_ctype
) },
889 { &ullong_ptr_ctype
, T_PTR(&ullong_ctype
) },
890 { &const_ptr_ctype
, T_PTR(&const_void_ctype
) },
891 { &const_string_ctype
, T_PTR(&const_char_ctype
) },
893 { &const_void_ctype
, T_CONST(&void_ctype
, NULL
, NULL
) },
894 { &const_char_ctype
, T_CONST(&char_ctype
, &bits_in_char
, &max_int_alignment
)},
898 void init_ctype(void)
900 const struct ctype_declare
*ctype
;
902 for (ctype
= ctype_declaration
; ctype
->ptr
; ctype
++) {
903 struct symbol
*sym
= ctype
->ptr
;
904 unsigned long bit_size
= ctype
->bit_size
? *ctype
->bit_size
: -1;
905 unsigned long maxalign
= ctype
->maxalign
? *ctype
->maxalign
: 0;
906 unsigned long alignment
= bits_to_bytes(bit_size
);
908 if (alignment
> maxalign
)
909 alignment
= maxalign
;
910 sym
->type
= ctype
->type
;
911 sym
->rank
= ctype
->rank
;
912 sym
->bit_size
= bit_size
;
913 sym
->ctype
.alignment
= alignment
;
914 sym
->ctype
.base_type
= ctype
->base_type
;
915 sym
->ctype
.modifiers
= ctype
->modifiers
;
917 if (sym
->type
== SYM_NODE
) {
918 struct symbol
*base
= sym
->ctype
.base_type
;
919 sym
->rank
= base
->rank
;
920 if (!ctype
->bit_size
)
921 sym
->bit_size
= base
->bit_size
;
922 if (!ctype
->maxalign
)
923 sym
->ctype
.alignment
= base
->ctype
.alignment
;
927 // and now some adjustments
928 if (funsigned_char
) {
929 char_ctype
.ctype
.modifiers
|= MOD_UNSIGNED
;
930 char_ctype
.ctype
.modifiers
&= ~MOD_SIGNED
;
934 ptrdiff_ctype
= ssize_t_ctype
;
936 intptr_ctype
= ssize_t_ctype
;
938 uintptr_ctype
= size_t_ctype
;