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 int count_array_initializer(struct symbol
*t
, struct expression
*expr
)
316 * Arrays of character types are special; they can be initialized by
317 * string literal _or_ by string literal in braces. The latter means
318 * that with T x[] = {<string literal>} number of elements in x depends
319 * on T - if it's a character type, we get the length of string literal
320 * (including NUL), otherwise we have one element here.
322 if (t
->ctype
.base_type
== &int_type
&& t
->rank
== -2)
325 switch (expr
->type
) {
326 case EXPR_INITIALIZER
: {
327 struct expression
*entry
;
330 FOR_EACH_PTR(expr
->expr_list
, entry
) {
332 switch (entry
->type
) {
334 if (entry
->idx_to
>= nr
)
335 nr
= entry
->idx_to
+1;
338 struct expression
*e
= entry
;
340 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
342 if (e
&& e
->type
== EXPR_STRING
) {
346 str_len
= entry
->string
->length
;
355 } END_FOR_EACH_PTR(entry
);
356 if (count
== 1 && str_len
)
362 struct expression
*e
= expr
;
363 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
365 if (e
&& e
->type
== EXPR_STRING
) {
369 nr
= expr
->string
->length
;
379 static struct expression
*get_symbol_initializer(struct symbol
*sym
)
382 if (sym
->initializer
)
383 return sym
->initializer
;
384 } while ((sym
= sym
->same_symbol
) != NULL
);
388 static unsigned int implicit_array_size(struct symbol
*node
, unsigned int count
)
390 struct symbol
*arr_ori
= node
->ctype
.base_type
;
391 struct symbol
*arr_new
= alloc_symbol(node
->pos
, SYM_ARRAY
);
392 struct symbol
*elem_type
= arr_ori
->ctype
.base_type
;
393 struct expression
*size
= alloc_const_expression(node
->pos
, count
);
394 unsigned int bit_size
= array_element_offset(elem_type
->bit_size
, count
);
397 arr_new
->bit_size
= bit_size
;
398 arr_new
->array_size
= size
;
399 node
->array_size
= size
;
400 node
->ctype
.base_type
= arr_new
;
405 static struct symbol
* examine_node_type(struct symbol
*sym
)
407 struct symbol
*base_type
= examine_base_type(sym
);
409 unsigned long alignment
;
411 /* SYM_NODE - figure out what the type of the node was.. */
417 bit_size
= base_type
->bit_size
;
418 alignment
= base_type
->ctype
.alignment
;
420 /* Pick up signedness information into the node */
421 sym
->ctype
.modifiers
|= (MOD_SIGNEDNESS
& base_type
->ctype
.modifiers
);
423 if (!sym
->ctype
.alignment
)
424 sym
->ctype
.alignment
= alignment
;
426 /* Unsized array? The size might come from the initializer.. */
427 if (bit_size
< 0 && base_type
->type
== SYM_ARRAY
) {
428 struct expression
*initializer
= get_symbol_initializer(sym
);
430 struct symbol
*node_type
= base_type
->ctype
.base_type
;
431 int count
= count_array_initializer(node_type
, initializer
);
433 if (node_type
&& node_type
->bit_size
>= 0)
434 bit_size
= implicit_array_size(sym
, count
);
438 sym
->bit_size
= bit_size
;
439 sym
->rank
= base_type
->rank
;
443 static struct symbol
*examine_enum_type(struct symbol
*sym
)
445 struct symbol
*base_type
= examine_base_type(sym
);
447 sym
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_SIGNEDNESS
);
448 sym
->bit_size
= bits_in_enum
;
449 if (base_type
->bit_size
> sym
->bit_size
)
450 sym
->bit_size
= base_type
->bit_size
;
451 sym
->ctype
.alignment
= enum_alignment
;
452 if (base_type
->ctype
.alignment
> sym
->ctype
.alignment
)
453 sym
->ctype
.alignment
= base_type
->ctype
.alignment
;
457 static struct symbol
*examine_pointer_type(struct symbol
*sym
)
460 * Since pointers to incomplete types can be used,
461 * for example in a struct-declaration-list,
462 * the base type must *not* be examined here.
463 * It thus means that it needs to be done later,
464 * when the base type of the pointer is looked at.
467 sym
->bit_size
= bits_in_pointer
;
468 if (!sym
->ctype
.alignment
)
469 sym
->ctype
.alignment
= pointer_alignment
;
473 static struct symbol
*examine_typeof(struct symbol
*sym
)
475 struct symbol
*base
= evaluate_expression(sym
->initializer
);
476 unsigned long mod
= 0;
480 if (base
->type
== SYM_NODE
) {
481 mod
|= base
->ctype
.modifiers
& MOD_TYPEOF
;
482 base
= base
->ctype
.base_type
;
484 if (base
->type
== SYM_BITFIELD
)
485 warning(base
->pos
, "typeof applied to bitfield type");
486 sym
->type
= SYM_NODE
;
487 sym
->ctype
.modifiers
= mod
;
488 sym
->ctype
.base_type
= base
;
489 return examine_node_type(sym
);
493 * Fill in type size and alignment information for
494 * regular SYM_TYPE things.
496 struct symbol
*examine_symbol_type(struct symbol
* sym
)
509 return examine_node_type(sym
);
511 return examine_array_type(sym
);
513 return examine_struct_union_type(sym
, 1);
515 return examine_struct_union_type(sym
, 0);
517 return examine_pointer_type(sym
);
519 return examine_enum_type(sym
);
521 return examine_bitfield_type(sym
);
523 /* Size and alignment had better already be set up */
526 return examine_typeof(sym
);
527 case SYM_PREPROCESSOR
:
528 sparse_error(sym
->pos
, "ctype on preprocessor command? (%s)", show_ident(sym
->ident
));
530 case SYM_UNINITIALIZED
:
531 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
534 examine_base_type(sym
);
537 examine_base_type(sym
);
540 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
546 const char* get_type_name(enum type type
)
548 const char *type_lookup
[] = {
549 [SYM_UNINITIALIZED
] = "uninitialized",
550 [SYM_PREPROCESSOR
] = "preprocessor",
551 [SYM_BASETYPE
] = "basetype",
553 [SYM_PTR
] = "pointer",
554 [SYM_FN
] = "function",
555 [SYM_ARRAY
] = "array",
556 [SYM_STRUCT
] = "struct",
557 [SYM_UNION
] = "union",
559 [SYM_TYPEOF
] = "typeof",
560 [SYM_BITFIELD
] = "bitfield",
561 [SYM_LABEL
] = "label",
562 [SYM_RESTRICT
] = "restrict",
563 [SYM_FOULED
] = "fouled",
564 [SYM_KEYWORD
] = "keyword",
568 return type_lookup
[type
];
573 struct symbol
*examine_pointer_target(struct symbol
*sym
)
575 return examine_base_type(sym
);
578 static struct symbol_list
*restr
, *fouled
;
580 void create_fouled(struct symbol
*type
)
582 if (type
->bit_size
< bits_in_int
) {
583 struct symbol
*new = alloc_symbol(type
->pos
, type
->type
);
585 new->bit_size
= bits_in_int
;
587 new->type
= SYM_FOULED
;
588 new->ctype
.base_type
= type
;
589 add_symbol(&restr
, type
);
590 add_symbol(&fouled
, new);
594 struct symbol
*befoul(struct symbol
*type
)
596 struct symbol
*t1
, *t2
;
597 while (type
->type
== SYM_NODE
)
598 type
= type
->ctype
.base_type
;
599 PREPARE_PTR_LIST(restr
, t1
);
600 PREPARE_PTR_LIST(fouled
, t2
);
614 static void inherit_declaration(struct symbol
*sym
, struct symbol
*prev
)
616 unsigned long mods
= prev
->ctype
.modifiers
;
618 // inherit function attributes
619 sym
->ctype
.modifiers
|= mods
& MOD_FUN_ATTR
;
622 void check_declaration(struct symbol
*sym
)
625 struct symbol
*next
= sym
;
627 while ((next
= next
->next_id
) != NULL
) {
628 if (next
->namespace != sym
->namespace)
630 if (sym
->scope
== next
->scope
) {
631 sym
->same_symbol
= next
;
632 inherit_declaration(sym
, next
);
635 /* Extern in block level matches a TOPLEVEL non-static symbol */
636 if (sym
->ctype
.modifiers
& MOD_EXTERN
) {
637 if ((next
->ctype
.modifiers
& (MOD_TOPLEVEL
|MOD_STATIC
)) == MOD_TOPLEVEL
) {
638 sym
->same_symbol
= next
;
643 if (!Wshadow
|| warned
)
645 if (get_sym_type(next
) == SYM_FN
)
648 warning(sym
->pos
, "symbol '%s' shadows an earlier one", show_ident(sym
->ident
));
649 info(next
->pos
, "originally declared here");
653 static void inherit_static(struct symbol
*sym
)
657 // only 'plain' symbols are concerned
658 if (sym
->ctype
.modifiers
& (MOD_STATIC
|MOD_EXTERN
))
661 for (prev
= sym
->next_id
; prev
; prev
= prev
->next_id
) {
662 if (prev
->namespace != NS_SYMBOL
)
664 if (prev
->scope
!= file_scope
)
667 sym
->ctype
.modifiers
|= prev
->ctype
.modifiers
& MOD_STATIC
;
669 // previous declarations are already converted
674 void bind_symbol_with_scope(struct symbol
*sym
, struct ident
*ident
, enum namespace ns
, struct scope
*scope
)
677 sparse_error(sym
->pos
, "internal error: symbol type already bound");
680 if (ident
->reserved
&& (ns
& (NS_TYPEDEF
| NS_STRUCT
| NS_LABEL
| NS_SYMBOL
))) {
681 sparse_error(sym
->pos
, "Trying to use reserved word '%s' as identifier", show_ident(ident
));
685 sym
->next_id
= ident
->symbols
;
686 ident
->symbols
= sym
;
687 if (sym
->ident
&& sym
->ident
!= ident
)
688 warning(sym
->pos
, "Symbol '%s' already bound", show_ident(sym
->ident
));
692 if (ns
== NS_SYMBOL
&& toplevel(scope
)) {
693 unsigned mod
= MOD_ADDRESSABLE
| MOD_TOPLEVEL
;
697 scope
= global_scope
;
698 if (sym
->ctype
.modifiers
& MOD_STATIC
||
699 is_extern_inline(sym
)) {
703 sym
->ctype
.modifiers
|= mod
;
705 bind_scope(sym
, scope
);
708 void bind_symbol(struct symbol
*sym
, struct ident
*ident
, enum namespace ns
)
710 struct scope
*scope
= block_scope
;;
715 scope
= function_scope
;
716 bind_symbol_with_scope(sym
, ident
, ns
, scope
);
719 struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace)
721 struct ident
*ident
= built_in_ident(name
);
722 struct symbol
*sym
= lookup_symbol(ident
, namespace);
724 if (sym
&& sym
->type
!= type
)
725 die("symbol %s created with different types: %d old %d", name
,
729 struct token
*token
= built_in_token(stream
, ident
);
731 sym
= alloc_symbol(token
->pos
, type
);
732 bind_symbol(sym
, token
->ident
, namespace);
741 struct symbol int_type
,
745 * C types (i.e. actual instances that the abstract types
748 struct symbol bool_ctype
, void_ctype
, type_ctype
,
749 char_ctype
, schar_ctype
, uchar_ctype
,
750 short_ctype
, sshort_ctype
, ushort_ctype
,
751 int_ctype
, sint_ctype
, uint_ctype
,
752 long_ctype
, slong_ctype
, ulong_ctype
,
753 llong_ctype
, sllong_ctype
, ullong_ctype
,
754 int128_ctype
, sint128_ctype
, uint128_ctype
,
755 float_ctype
, double_ctype
, ldouble_ctype
,
756 string_ctype
, ptr_ctype
, lazy_ptr_ctype
,
757 incomplete_ctype
, label_ctype
, bad_ctype
,
759 struct symbol autotype_ctype
;
760 struct symbol int_ptr_ctype
, uint_ptr_ctype
;
761 struct symbol long_ptr_ctype
, ulong_ptr_ctype
;
762 struct symbol llong_ptr_ctype
, ullong_ptr_ctype
;
763 struct symbol float32_ctype
, float32x_ctype
;
764 struct symbol float64_ctype
, float64x_ctype
;
765 struct symbol float128_ctype
;
766 struct symbol const_void_ctype
, const_char_ctype
;
767 struct symbol const_ptr_ctype
, const_string_ctype
;
769 struct symbol zero_int
;
771 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
772 #define __IDENT(n,str,res) \
773 struct ident n = __INIT_IDENT(str,res)
775 #include "ident-list.h"
777 void init_symbols(void)
779 int stream
= init_stream("builtin", -1, includepath
);
781 #define __IDENT(n,str,res) \
783 #include "ident-list.h"
788 #ifdef __CHAR_UNSIGNED__
789 #define CHAR_SIGNEDNESS MOD_UNSIGNED
791 #define CHAR_SIGNEDNESS MOD_SIGNED
793 // For fix-sized types
794 static int bits_in_type32
= 32;
795 static int bits_in_type64
= 64;
796 static int bits_in_type128
= 128;
798 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
799 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
800 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
801 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
802 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
803 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
804 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
805 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
806 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
807 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
809 static const struct ctype_declare
{
812 unsigned long modifiers
;
816 struct symbol
*base_type
;
817 } ctype_declaration
[] = {
818 { &bool_ctype
, T_INT(-3, bool, MOD_UNSIGNED
) },
819 { &void_ctype
, T_BASETYPE
},
820 { &type_ctype
, T_BASETYPE
},
821 { &incomplete_ctype
, T_BASETYPE
},
822 { &autotype_ctype
, T_BASETYPE
},
823 { &bad_ctype
, T_BASETYPE
},
825 { &char_ctype
, T_INT(-2, char, CHAR_SIGNEDNESS
) },
826 { &schar_ctype
, T_SINT(-2, char) },
827 { &uchar_ctype
, T_UINT(-2, char) },
828 { &short_ctype
, T__INT(-1, short) },
829 { &sshort_ctype
, T_SINT(-1, short) },
830 { &ushort_ctype
, T_UINT(-1, short) },
831 { &int_ctype
, T__INT( 0, int) },
832 { &sint_ctype
, T_SINT( 0, int) },
833 { &uint_ctype
, T_UINT( 0, int) },
834 { &long_ctype
, T__INT( 1, long) },
835 { &slong_ctype
, T_SINT( 1, long) },
836 { &ulong_ctype
, T_UINT( 1, long) },
837 { &llong_ctype
, T__INT( 2, longlong
) },
838 { &sllong_ctype
, T_SINT( 2, longlong
) },
839 { &ullong_ctype
, T_UINT( 2, longlong
) },
840 { &int128_ctype
, T__INT( 3, type128
) },
841 { &sint128_ctype
, T_SINT( 3, type128
) },
842 { &uint128_ctype
, T_UINT( 3, type128
) },
844 { &float_ctype
, T_FLOAT(-1, float) },
845 { &double_ctype
, T_FLOAT( 0, double) },
846 { &ldouble_ctype
, T_FLOAT( 1, longdouble
) },
848 { &float32_ctype
, T_FLOAT(-1, type32
) },
849 { &float32x_ctype
, T_FLOAT(-1, double) },
850 { &float64_ctype
, T_FLOAT( 0, type64
) },
851 { &float64x_ctype
, T_FLOAT( 1, longdouble
) },
852 { &float128_ctype
, T_FLOAT_(2, type128
, &max_alignment
) },
854 { &string_ctype
, T_PTR(&char_ctype
) },
855 { &ptr_ctype
, T_PTR(&void_ctype
) },
856 { &null_ctype
, T_PTR(&void_ctype
) },
857 { &label_ctype
, T_PTR(&void_ctype
) },
858 { &lazy_ptr_ctype
, T_PTR(&void_ctype
) },
859 { &int_ptr_ctype
, T_PTR(&int_ctype
) },
860 { &uint_ptr_ctype
, T_PTR(&uint_ctype
) },
861 { &long_ptr_ctype
, T_PTR(&long_ctype
) },
862 { &ulong_ptr_ctype
, T_PTR(&ulong_ctype
) },
863 { &llong_ptr_ctype
, T_PTR(&llong_ctype
) },
864 { &ullong_ptr_ctype
, T_PTR(&ullong_ctype
) },
865 { &const_ptr_ctype
, T_PTR(&const_void_ctype
) },
866 { &const_string_ctype
, T_PTR(&const_char_ctype
) },
868 { &const_void_ctype
, T_CONST(&void_ctype
, NULL
, NULL
) },
869 { &const_char_ctype
, T_CONST(&char_ctype
, &bits_in_char
, &max_int_alignment
)},
873 void init_ctype(void)
875 const struct ctype_declare
*ctype
;
877 for (ctype
= ctype_declaration
; ctype
->ptr
; ctype
++) {
878 struct symbol
*sym
= ctype
->ptr
;
879 unsigned long bit_size
= ctype
->bit_size
? *ctype
->bit_size
: -1;
880 unsigned long maxalign
= ctype
->maxalign
? *ctype
->maxalign
: 0;
881 unsigned long alignment
= bits_to_bytes(bit_size
);
883 if (alignment
> maxalign
)
884 alignment
= maxalign
;
885 sym
->type
= ctype
->type
;
886 sym
->rank
= ctype
->rank
;
887 sym
->bit_size
= bit_size
;
888 sym
->ctype
.alignment
= alignment
;
889 sym
->ctype
.base_type
= ctype
->base_type
;
890 sym
->ctype
.modifiers
= ctype
->modifiers
;
892 if (sym
->type
== SYM_NODE
) {
893 struct symbol
*base
= sym
->ctype
.base_type
;
894 sym
->rank
= base
->rank
;
895 if (!ctype
->bit_size
)
896 sym
->bit_size
= base
->bit_size
;
897 if (!ctype
->maxalign
)
898 sym
->ctype
.alignment
= base
->ctype
.alignment
;
902 // and now some adjustments
903 if (funsigned_char
) {
904 char_ctype
.ctype
.modifiers
|= MOD_UNSIGNED
;
905 char_ctype
.ctype
.modifiers
&= ~MOD_SIGNED
;