Remove a few more warnings.
[suif.git] / src / basesuif / snoot / stypes.cc
blob6ad1ce45f83ad2a6bb33c4900f415391794a2a7c
1 /* file "stypes.cc" */
3 /* Copyright (c) 1994 Stanford University
5 All rights reserved.
7 This software is provided under the terms described in
8 the "suif_copyright.h" include file. */
10 #include <suif_copyright.h>
12 /* snoot type handling */
14 #include "c.h"
16 /* Basic types: */
18 type_node *chartype; /* char */
19 type_node *doubletype; /* double */
20 type_node *floattype; /* float */
21 type_node *inttype; /* signed int */
22 type_node *longdouble; /* long double */
23 type_node *longtype; /* long */
24 type_node *longlong; /* long long */
25 type_node *shorttype; /* signed short int */
26 type_node *signedchar; /* signed char */
27 type_node *unsignedchar; /* unsigned char */
28 type_node *unsignedlong; /* unsigned long int */
29 type_node *unsignedlonglong; /* unsigned long long int */
30 type_node *unsignedshort; /* unsigned short int */
31 type_node *unsignedtype; /* unsigned int */
32 type_node *voidptype; /* void* */
33 type_node *voidtype; /* void */
34 type_node *booleantype; /* for internal use */
35 type_node *size_t_type; /* whatever size_t is on the target
36 machine, one of unsignedchar,
37 unsignedshort, unsignedtype,
38 unsignedlong, or unsignedlonglong
42 /* Symbol table entries of basic types: */
44 Symbol chartype_sym; /* char */
45 Symbol doubletype_sym; /* double */
46 Symbol floattype_sym; /* float */
47 Symbol inttype_sym; /* signed int */
48 Symbol longdouble_sym; /* long double */
49 Symbol longtype_sym; /* long */
50 Symbol longlong_sym; /* long long */
51 Symbol shorttype_sym; /* signed short int */
52 Symbol signedchar_sym; /* signed char */
53 Symbol unsignedchar_sym; /* unsigned char */
54 Symbol unsignedlong_sym; /* unsigned long int */
55 Symbol unsignedlonglong_sym; /* unsigned long long int */
56 Symbol unsignedshort_sym; /* unsigned short int */
57 Symbol unsignedtype_sym; /* unsigned int */
58 Symbol voidtype_sym; /* void */
60 replacements *global_replacements;
62 static base_symtab *type_symtab; /* table to put types when there is nothing
63 else to go by */
65 static void init_base_type(type_node **the_type_node, char *name,
66 type_ops the_op, boolean is_signed, int size,
67 int alignment, Symbol *the_symbol);
68 static boolean identically_qualified(type_node *type1, type_node *type2);
69 static boolean bit_field_annotations_same(type_node *field1,
70 type_node *field2);
71 static void use_field(struct_type *the_struct, char *name);
72 static Coordinate *get_source(type_node *the_type);
73 static void set_source(type_node *the_type, Coordinate source);
75 /* typeInit - initialize basic types */
76 void typeInit(void)
78 int c_type;
80 target.is_big_endian = this_target->is_big_endian;
81 target.addressable_size = this_target->addressable_size;
82 target.char_is_signed = this_target->char_is_signed;
84 for (c_type = 0; c_type < num_C_types; ++c_type)
86 target.size[c_type] = this_target->size[c_type];
87 target.align[c_type] = this_target->align[c_type];
90 target.array_align = this_target->array_align;
91 target.struct_align = this_target->struct_align;
93 target.ptr_diff_type = this_target->ptr_diff_type;
96 * Sanity check on alignments
98 int c_type_1;
99 for (c_type_1 = C_char; c_type_1 <= C_longlong; ++c_type_1)
101 for (int c_type_2 = C_char; c_type_2 <= C_longlong; ++c_type_2)
103 if ((target.size[c_type_1] == target.size[c_type_2]) &&
104 (target.align[c_type_1] != target.align[c_type_2]))
106 error_line(0, NULL,
107 "integral types with size %d bits have "
108 "conflicting", target.size[c_type_1]);
109 error_line(1, NULL,
110 " alignment requirements of %d and %d bits",
111 target.align[c_type_1], target.align[c_type_2]);
115 for (c_type_1 = C_float; c_type_1 <= C_longdouble; ++c_type_1)
117 for (int c_type_2 = C_float; c_type_2 <= C_longdouble; ++c_type_2)
119 if ((target.size[c_type_1] == target.size[c_type_2]) &&
120 (target.align[c_type_1] != target.align[c_type_2]))
122 error_line(0, NULL,
123 "floating point types with size %d bits have "
124 "conflicting", target.size[c_type_1]);
125 error_line(1, NULL,
126 " alignment requirements of %d and %d bits",
127 target.align[c_type_1], target.align[c_type_2]);
132 fileset->globals()->predefine_types();
133 type_symtab = type_signed->parent();
135 global_replacements = new replacements;
137 init_base_type(&chartype, "char", TYPE_INT,
138 target.char_is_signed, target.size[C_char],
139 target.align[C_char], &chartype_sym);
140 init_base_type(&doubletype, "double", TYPE_FLOAT,
141 TRUE, target.size[C_double],
142 target.align[C_double], &doubletype_sym);
143 init_base_type(&floattype, "float", TYPE_FLOAT,
144 TRUE, target.size[C_float],
145 target.align[C_float], &floattype_sym);
146 init_base_type(&inttype, "int", TYPE_INT,
147 TRUE, target.size[C_int],
148 target.align[C_int], &inttype_sym);
149 init_base_type(&longdouble, "long double", TYPE_FLOAT,
150 TRUE, target.size[C_longdouble],
151 target.align[C_longdouble], &longdouble_sym);
152 init_base_type(&longtype, "long int", TYPE_INT,
153 TRUE, target.size[C_long],
154 target.align[C_long], &longtype_sym);
155 init_base_type(&longlong, "long long int", TYPE_INT,
156 TRUE, target.size[C_longlong],
157 target.align[C_longlong], &longlong_sym);
158 init_base_type(&shorttype, "short", TYPE_INT,
159 TRUE, target.size[C_short],
160 target.align[C_short], &shorttype_sym);
161 init_base_type(&signedchar, "signed char", TYPE_INT,
162 TRUE, target.size[C_char],
163 target.align[C_char], &signedchar_sym);
164 init_base_type(&unsignedchar, "unsigned char", TYPE_INT,
165 FALSE, target.size[C_char],
166 target.align[C_char], &unsignedchar_sym);
167 init_base_type(&unsignedlong, "unsigned long int", TYPE_INT,
168 FALSE, target.size[C_long],
169 target.align[C_long], &unsignedlong_sym);
170 init_base_type(&unsignedlonglong, "unsigned long long int", TYPE_INT,
171 FALSE, target.size[C_longlong],
172 target.align[C_longlong], &unsignedlonglong_sym);
173 init_base_type(&unsignedshort, "unsigned short int", TYPE_INT,
174 FALSE, target.size[C_short],
175 target.align[C_short], &unsignedshort_sym);
176 init_base_type(&unsignedtype, "unsigned int", TYPE_INT,
177 FALSE, target.size[C_int],
178 target.align[C_int], &unsignedtype_sym);
179 init_base_type(&voidtype, "void", TYPE_VOID,
180 FALSE, 0,
181 0, &voidtype_sym);
183 /* In some places, type_ptr_diff is used as an expression result
184 * type, but other pieces assume that expression result types that
185 * are integers will be exactly one of the standard C types. So
186 * we can't just rely on what predefine_types() to set
187 * type_ptr_diff; we have to set it here to one of the standard C
188 * integer types we just created. */
189 switch (target.ptr_diff_type)
191 case C_char:
192 type_ptr_diff = signedchar;
193 break;
194 case C_short:
195 type_ptr_diff = shorttype;
196 break;
197 case C_int:
198 type_ptr_diff = inttype;
199 break;
200 case C_long:
201 type_ptr_diff = longtype;
202 break;
203 case C_longlong:
204 type_ptr_diff = longlong;
205 break;
206 default:
207 assert(FALSE);
210 voidptype = voidtype->ptr_to();
212 booleantype = new base_type(TYPE_INT, type_signed->size(), TRUE);
213 immed_list *new_immed_list = new immed_list;
214 new_immed_list->append(immed("boolean"));
215 booleantype->append_annote(k_type_name, new_immed_list);
216 booleantype = type_symtab->install_type(booleantype);
219 * We assume that size_t is the unsigned version of ptrdiff_t.
220 * This isn't technically required by the ANSI standard, but it's
221 * likely to be true on almost any real back-end C compiler, and
222 * even if it's not, it is very unlikely that the difference will
223 * ever be noticed.
225 switch (target.ptr_diff_type)
227 case C_char:
228 size_t_type = unsignedchar;
229 break;
230 case C_short:
231 size_t_type = unsignedshort;
232 break;
233 case C_int:
234 size_t_type = unsignedtype;
235 break;
236 case C_long:
237 size_t_type = unsignedlong;
238 break;
239 case C_longlong:
240 size_t_type = unsignedlonglong;
241 break;
242 default:
243 assert(FALSE);
247 * BUILTIN in config.h only seems to handle builin expressions
248 * and this is a built-in type.
250 Coordinate c = { "", 0, 0 };
251 deftype(string("__builtin_va_list"), chartype->ptr_to(), &c);
255 static void init_base_type(type_node **the_type_node, char *name,
256 type_ops the_op, boolean is_signed, int size,
257 int alignment, Symbol *the_symbol)
259 *the_symbol = install(string(name), &types, 1);
260 *the_type_node = new base_type(the_op, size, is_signed);
261 immed_list *new_immed_list = new immed_list;
262 new_immed_list->append(immed(name));
263 (*the_type_node)->append_annote(k_type_name, new_immed_list);
264 *the_type_node = type_symtab->install_type(*the_type_node);
265 (*the_symbol)->type = *the_type_node;
266 assert((alignment == 0) || (size % alignment == 0));
268 type_node *merged_version = new base_type(the_op, size, is_signed);
269 merged_version = type_symtab->install_type(merged_version);
270 global_replacements->oldtypes.append(*the_type_node);
271 global_replacements->newtypes.append(merged_version);
274 /* array - construct the type `array 0..n-1 of ty' */
275 extern type_node *build_array(type_node *ty, int n)
277 assert(ty != NULL);
279 if (isfunc(ty))
281 error("illegal type `array of %t'\n", ty);
282 return build_array(inttype, n);
285 if ((level > GLOBAL) && isarray(ty) && (ty->size() == 0))
286 error("missing array size\n");
287 if (ty->size() == 0)
289 if (ty->unqual() == voidtype)
290 error("illegal type `array of %t'\n", ty);
291 else if (Aflag >= 2)
292 warning("declaring type `array of %t' is undefined\n", ty);
294 else if (n > INT_MAX / ty->size())
296 error("size of `array of %t' exceeds %d bytes\n", ty, INT_MAX);
297 n = 1;
300 base_symtab *the_symtab = ty->parent();
301 assert(the_symtab != NULL);
302 array_bound upper_bound = unknown_bound;
303 if (n > 0)
304 upper_bound = array_bound(n - 1);
305 type_node *new_type = new array_type(ty, array_bound(0), upper_bound);
306 return the_symtab->install_type(new_type);
309 /* atop - convert ty from `array of ty' to `pointer to ty' */
310 extern type_node *atop(type_node *ty)
312 if (isarray(ty))
313 return base_from_array(ty)->ptr_to();
314 error("type error: %s\n", "array expected");
315 return ty->ptr_to();
318 /* composite - return the composite type of ty1 & ty2, or NULL if ty1 & ty2 are
319 incompatible */
320 extern type_node *composite(type_node *ty1, type_node *ty2)
322 if ((ty1 == NULL) || (ty2 == NULL))
323 return NULL;
324 if (ty1 == ty2)
325 return ty1;
326 if ((ty1->op() != ty2->op()) &&
327 ((!ty1->is_modifier()) || (!ty2->is_modifier())))
329 return NULL;
331 switch (ty1->op())
333 case TYPE_CONST:
334 case TYPE_VOLATILE:
336 if (!identically_qualified(ty1, ty2))
337 return NULL;
338 type_node *new_type = composite(ty1->unqual(), ty2->unqual());
339 if (new_type == NULL)
340 return NULL;
341 if (ty1->is_const())
342 new_type = qual(CONST, new_type);
343 if (ty1->is_volatile())
344 new_type = qual(VOLATILE, new_type);
345 return new_type;
347 case TYPE_PTR:
349 type_node *new_type =
350 composite(base_from_pointer(ty1), base_from_pointer(ty2));
351 if (new_type == NULL)
352 return NULL;
353 return new_type->ptr_to();
355 case TYPE_ARRAY:
357 type_node *new_type =
358 composite(base_from_array(ty1), base_from_array(ty2));
359 if (new_type == NULL)
360 return NULL;
362 array_type *old_array1 = (array_type *)ty1;
363 array_type *old_array2 = (array_type *)ty2;
364 int upper_bound = 0;
365 if (!old_array1->are_bounds_unknown())
366 upper_bound = old_array1->upper_bound().constant() + 1;
367 if (!old_array2->are_bounds_unknown())
369 if ((upper_bound != 0) &&
370 (upper_bound != old_array2->upper_bound().constant() + 1))
372 return NULL;
374 upper_bound = old_array2->upper_bound().constant() + 1;
376 return build_array(new_type, upper_bound);
378 case TYPE_FUNC:
380 func_type *old_func1 = (func_type *)ty1;
381 func_type *old_func2 = (func_type *)ty2;
382 type_node *new_type =
383 composite(old_func1->return_type(),
384 old_func2->return_type());
385 if (new_type == NULL)
386 return NULL;
388 unsigned num_args = 0;
389 boolean args_unknown = TRUE;
390 boolean varargs = FALSE;
391 if (old_func1->args_known() && old_func2->args_known())
393 num_args = old_func1->num_args();
394 if (num_args != old_func2->num_args())
395 return NULL;
396 args_unknown = FALSE;
397 varargs = old_func1->has_varargs();
398 if (varargs != old_func2->has_varargs())
399 return NULL;
401 else if (old_func1->args_known() && !old_func2->args_known())
403 return old_func1;
405 else if (old_func2->args_known() && !old_func1->args_known())
407 return old_func2;
410 base_symtab *the_symtab = fileset->globals();
412 boolean vis = the_symtab->make_type_visible(new_type);
413 assert(vis);
414 func_type *new_func = new func_type(new_type, num_args, varargs);
416 if (args_unknown)
418 new_func->set_args_unknown();
420 else
422 new_func->set_args_known();
423 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
425 type_node *arg1_type = old_func1->arg_type(arg_num);
426 type_node *arg2_type = old_func2->arg_type(arg_num);
427 type_node *new_arg = composite(arg1_type->unqual(),
428 arg2_type->unqual());
429 if (new_arg == NULL)
431 delete new_func;
432 return NULL;
434 if (isconst(arg1_type) || isconst(arg2_type))
435 new_arg = qual(CONST, new_arg);
436 if (isvolatile(arg1_type) || isvolatile(arg2_type))
437 new_arg = qual(VOLATILE, new_arg);
439 vis = the_symtab->make_type_visible(new_arg);
440 assert(vis);
441 new_func->set_arg_type(arg_num, new_arg);
445 return the_symtab->install_type(new_func);
447 default:
448 break;
450 return NULL;
453 /* deftype - define name to be equivalent to type ty */
454 extern Symbol deftype(char *name, type_node *ty, Coordinate *pos)
456 Symbol p = lookup(name, identifiers);
458 if ((p != NULL) && (p->scope == level))
459 error("redeclaration of `%s'\n", name);
460 p = install(name, &identifiers, level < LOCAL);
461 p->type = ty;
462 p->sclass = TYPEDEF;
463 p->src = *pos;
464 if (option_keep_typedef_info)
466 base_symtab *current_symtab = get_current_symtab();
467 var_sym *typedef_var = current_symtab->new_var(type_signed, name);
468 if (current_symtab->is_file())
470 current_symtab->define_var(typedef_var,
471 get_alignment(type_signed));
473 typedef_var->append_annote(k_typedef_name, new immed_list(ty));
475 return p;
478 /* deref - dereference ty, type *ty */
479 extern type_node *deref(type_node *ty)
481 if (isptr(ty))
482 return base_from_pointer(ty);
483 else
484 error("type error: %s\n", "pointer expected");
485 return ty;
488 /* eqtype - is ty1==ty2? handles arrays & functions; return ret if ty1==ty2,
489 but one is incomplete */
490 extern boolean eqtype(type_node *ty1, type_node *ty2, boolean ret)
492 if (ty1 == ty2)
493 return TRUE;
494 if ((ty1 == NULL) || (ty2 == NULL))
495 return FALSE;
497 if ((ty1->op() != ty2->op()) &&
498 ((!ty1->is_modifier()) || (!ty2->is_modifier())))
500 return FALSE;
502 switch (ty1->op())
504 case TYPE_CONST:
505 case TYPE_VOLATILE:
506 if (!identically_qualified(ty1, ty2))
507 return FALSE;
508 return eqtype(ty1->unqual(), ty2->unqual(), ret);
509 case TYPE_PTR:
510 return eqtype(base_from_pointer(ty1), base_from_pointer(ty2), ret);
511 case TYPE_ARRAY:
513 array_type *old_array1 = (array_type *)ty1;
514 array_type *old_array2 = (array_type *)ty2;
516 if (!eqtype(old_array1->elem_type(), old_array2->elem_type(), ret))
517 return FALSE;
518 if (old_array1->upper_bound() == old_array2->upper_bound())
519 return TRUE;
520 if (old_array1->are_bounds_unknown() !=
521 old_array2->are_bounds_unknown())
523 return ret;
525 break;
527 case TYPE_FUNC:
529 func_type *old_func1 = (func_type *)ty1;
530 func_type *old_func2 = (func_type *)ty2;
531 if (!eqtype(old_func1->return_type(), old_func2->return_type(),
532 ret))
534 return FALSE;
537 if (old_func1->args_known() && old_func2->args_known())
539 unsigned num_args = old_func1->num_args();
540 if (num_args != old_func2->num_args())
541 return FALSE;
542 if (old_func1->has_varargs() != old_func2->has_varargs())
543 return FALSE;
544 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
546 if (!eqtype(old_func1->arg_type(arg_num)->unqual(),
547 old_func2->arg_type(arg_num)->unqual(), ret))
549 return FALSE;
552 return TRUE;
554 else if (old_func1->args_known() && !old_func2->args_known())
556 func_type *temp_func = old_func1;
557 old_func1 = old_func2;
558 old_func2 = temp_func;
560 if (!old_func2->args_known())
561 return TRUE;
563 if (old_func2->has_varargs())
564 return (old_func2->num_args() == 0);
566 unsigned num_args = old_func2->num_args();
567 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
569 type_node *arg_type = old_func2->arg_type(arg_num)->unqual();
570 if ((promote(arg_type) != arg_type) || (arg_type == floattype))
571 return FALSE;
574 return TRUE;
576 default:
577 break;
579 return FALSE;
582 /* fieldref - find field name of type ty, return entry */
583 Field fieldref(char *name, type_node *ty)
585 static struct field the_field;
587 assert(ty != NULL);
588 assert(ty->is_struct());
589 struct_type *the_struct = (struct_type *)ty;
590 unsigned num_fields = the_struct->num_fields();
591 for (unsigned field_num = 0; field_num < num_fields; ++field_num)
593 if (field_is_bit_fields(the_struct, field_num))
595 struct field *bit_fields = bit_field_list(the_struct, field_num);
596 while (bit_fields != NULL)
598 if (strcmp(bit_fields->name, name) == 0)
600 the_field = *bit_fields;
601 the_field.link = NULL;
602 while (bit_fields != NULL)
604 struct field *old_field = bit_fields;
605 bit_fields = bit_fields->link;
606 delete old_field;
608 the_field.offset = the_struct->offset(field_num);
609 the_field.block_name = the_struct->field_name(field_num);
610 the_field.block_type = the_struct->field_type(field_num);
611 use_field(the_struct, name);
612 return &the_field;
615 struct field *old_field = bit_fields;
616 bit_fields = bit_fields->link;
617 delete old_field;
620 else
622 if (strcmp(the_struct->field_name(field_num), name) == 0)
624 the_field.name = the_struct->field_name(field_num);
625 the_field.type = the_struct->field_type(field_num);
626 the_field.offset = the_struct->offset(field_num);
627 the_field.from = 0;
628 the_field.to = 0;
629 the_field.block_name = the_struct->field_name(field_num);
630 the_field.block_type = the_struct->field_type(field_num);
631 the_field.link = NULL;
632 use_field(the_struct, name);
633 return &the_field;
637 return NULL;
640 /* freturn - for `function returning ty', return ty */
641 extern type_node *freturn(type_node *ty)
643 assert(ty != NULL);
644 if (isfunc(ty))
646 func_type *the_func = (func_type *)ty;
647 return the_func->return_type();
649 error("type error: %s\n", "function expected");
650 return inttype;
653 /* func - construct the type `function (void) returning ty' */
654 extern func_type *func(type_node *ty)
656 assert(ty != NULL);
657 if (isarray(ty) || isfunc(ty))
658 error("illegal return type `%t'\n", ty);
660 base_symtab *the_symtab = ty->parent();
661 assert(the_symtab != NULL);
662 func_type *new_type = new func_type(ty);
663 return (func_type *)(the_symtab->install_type(new_type));
666 /* hasproto - true iff ty has no function types or they all have prototypes */
667 extern boolean hasproto(type_node *ty)
669 assert(ty != NULL);
670 switch (ty->op())
672 case TYPE_CONST:
673 case TYPE_VOLATILE:
675 modifier_type *the_modifier = (modifier_type *)ty;
676 return hasproto(the_modifier->base());
678 case TYPE_PTR:
680 ptr_type *the_pointer = (ptr_type *)ty;
681 return hasproto(the_pointer->ref_type());
683 case TYPE_ARRAY:
685 array_type *the_array = (array_type *)ty;
686 return hasproto(the_array->elem_type());
688 case TYPE_FUNC:
690 func_type *the_func = (func_type *)ty;
691 return (hasproto(the_func->return_type()) &&
692 the_func->args_known());
694 default:
695 return TRUE;
699 /* newfield - install a new field in ty with type fty */
700 extern Field newfield(const char *name, struct field **field_list,
701 type_node *fty, struct_type *to_fill)
703 Field p, *q = field_list;
705 if (name == NULL)
706 name = gen_internal_name();
707 for (p = *q; p != NULL; q = &p->link, p = *q)
709 if (strcmp(p->name, name) == 0)
710 error("duplicate field name `%s' in `%t'\n", name, to_fill);
712 #ifndef DEAL_WITH_GCC_BRAIN_DAMAGE
713 *q = p = new struct field;
714 #else
716 // gcc version 2.6.3 gets a parse error on the code above. The code
717 // looks like it should be perfectly legal, and both gcc 2.5.8 and
718 // the IRIX 5.3 C++ compiler have no problem with it, so I'm inclined
719 // to think the problem is caused by brain damage in the g++
720 // front-end. A work-around follows.
722 *q = p = (struct field *)operator new(sizeof(struct field));
723 #endif
724 p->name = name;
725 p->type = fty;
726 p->offset = 0;
727 p->from = 0;
728 p->to = 0;
729 p->block_name = NULL;
730 p->block_type = NULL;
731 p->link = NULL;
732 if (xref)
734 assert(to_fill != NULL);
735 if (get_field_table(to_fill) == NULL)
736 set_field_table(to_fill, table(0, level));
737 Table field_table = get_field_table(to_fill);
738 install(name, &field_table, 1)->src = src;
739 set_field_table(to_fill, field_table);
741 return p;
744 /* newstruct - install a new structure/union/enum depending on op and return
745 its symbol table entry */
746 Symbol newstruct(int op, const char *tag)
748 assert((op == ENUM) || (op == STRUCT) || (op == UNION));
749 if ((tag == NULL) || (*tag == '\0')) /* anonymous structure/union/enum */
750 tag = gen_internal_name();
751 Symbol p = lookup(tag, types);
752 if ((p != NULL) &&
753 ((p->scope == level) || ((p->scope == PARAM) && (level == PARAM+1))))
755 if ((((p->type->op() == TYPE_ENUM) && (op == ENUM)) ||
756 ((p->type->op() == TYPE_STRUCT) && (op == STRUCT)) ||
757 ((p->type->op() == TYPE_UNION) && (op == UNION))) && !p->defined)
759 return p;
761 error("redeclaration of `%s'\n", tag);
763 p = install(tag, &types, 1);
764 if (op == ENUM)
766 p->type = new enum_type(tag, inttype->size(), TRUE, 0);
768 else
770 p->type = new struct_type((op == STRUCT) ? TYPE_STRUCT : TYPE_UNION,
771 0, tag, 0);
773 set_source(p->type, src);
775 base_symtab *the_symtab = get_current_symtab();
776 if (the_symtab->is_file())
777 the_symtab = the_symtab->parent();
778 p->type = the_symtab->install_type(p->type);
780 p->src = src;
781 return p;
784 /* outtype - output type ty */
785 extern void outtype(void (*s_printer)(const char *to_print, void *data),
786 void (*c_printer)(int to_print, void *data), void *data,
787 type_node *ty)
789 if (ty == chartype)
790 (*s_printer)(chartype_sym->name, data);
791 else if (ty == doubletype)
792 (*s_printer)(doubletype_sym->name, data);
793 else if (ty == floattype)
794 (*s_printer)(floattype_sym->name, data);
795 else if (ty == inttype)
796 (*s_printer)(inttype_sym->name, data);
797 else if (ty == longdouble)
798 (*s_printer)(longdouble_sym->name, data);
799 else if (ty == longtype)
800 (*s_printer)(longtype_sym->name, data);
801 else if (ty == longlong)
802 (*s_printer)(longlong_sym->name, data);
803 else if (ty == shorttype)
804 (*s_printer)(shorttype_sym->name, data);
805 else if (ty == signedchar)
806 (*s_printer)(signedchar_sym->name, data);
807 else if (ty == unsignedchar)
808 (*s_printer)(unsignedchar_sym->name, data);
809 else if (ty == unsignedlong)
810 (*s_printer)(unsignedlong_sym->name, data);
811 else if (ty == unsignedlonglong)
812 (*s_printer)(unsignedlonglong_sym->name, data);
813 else if (ty == unsignedshort)
814 (*s_printer)(unsignedshort_sym->name, data);
815 else if (ty == unsignedtype)
816 (*s_printer)(unsignedtype_sym->name, data);
817 else if (ty == voidtype)
818 (*s_printer)(voidtype_sym->name, data);
819 else
821 switch (ty->op())
823 case TYPE_VOID:
824 (*s_printer)(voidtype_sym->name, data);
825 break;
826 case TYPE_CONST:
827 case TYPE_VOLATILE:
828 if (ty->is_const())
829 gprint(s_printer, c_printer, data, "%k ", CONST);
830 if (ty->is_volatile())
831 gprint(s_printer, c_printer, data, "%k ", VOLATILE);
832 gprint(s_printer, c_printer, data, "%t", ty->unqual());
833 break;
834 case TYPE_STRUCT:
835 case TYPE_UNION:
836 case TYPE_ENUM:
838 const char *ty_name;
839 int op;
840 if (ty->op() == TYPE_ENUM)
842 enum_type *the_enum = (enum_type *)ty;
843 ty_name = the_enum->name();
844 op = ENUM;
846 else
848 struct_type *the_struct = (struct_type *)ty;
849 ty_name = the_struct->name();
850 op = (ty->op() == TYPE_STRUCT) ? STRUCT : UNION;
852 if (ty->size() == 0)
853 (*s_printer)("incomplete ", data);
854 assert(ty_name != NULL);
855 if ((*ty_name >= '1') && (*ty_name <= '9'))
857 Symbol p = findtype(ty);
858 if (p == NULL)
860 gprint(s_printer, c_printer, data, "%k", op);
861 Coordinate *location = get_source(ty);
862 if (location != NULL)
864 gprint(s_printer, c_printer, data,
865 " defined at %w", location);
868 else
870 (*s_printer)(p->name, data);
873 else
875 gprint(s_printer, c_printer, data, "%k %s", op, ty_name);
876 if (ty->size() == 0)
878 Coordinate *location = get_source(ty);
879 if (location != NULL)
881 gprint(s_printer, c_printer, data,
882 " defined at %w", location);
886 break;
888 case TYPE_PTR:
889 gprint(s_printer, c_printer, data, "pointer to %t",
890 base_from_pointer(ty));
891 break;
892 case TYPE_FUNC:
894 func_type *the_func = (func_type *)ty;
895 gprint(s_printer, c_printer, data, "%t function",
896 the_func->return_type());
897 if (the_func->args_known())
899 if (the_func->num_args() == 0)
901 if (the_func->has_varargs())
902 gprint(s_printer, c_printer, data, "(...)");
903 else
904 gprint(s_printer, c_printer, data, "(void)");
906 else
908 gprint(s_printer, c_printer, data, "(%t",
909 the_func->arg_type(0));
910 unsigned num_args = the_func->num_args();
911 for (unsigned arg_num = 1; arg_num < num_args;
912 ++arg_num)
914 gprint(s_printer, c_printer, data, ", %t",
915 the_func->arg_type(arg_num));
917 if (the_func->has_varargs())
918 gprint(s_printer, c_printer, data, ", ...");
919 gprint(s_printer, c_printer, data, ")");
922 break;
924 case TYPE_ARRAY:
926 array_type *the_array = (array_type *)ty;
927 if (!the_array->are_bounds_unknown())
929 gprint(s_printer, c_printer, data, "array %d",
930 the_array->upper_bound().constant() + 1);
931 while ((the_array->elem_type() != NULL) &&
932 isarray(the_array->elem_type()) &&
933 (!((array_type *)(the_array->elem_type()))
934 ->are_bounds_unknown()))
936 the_array = (array_type *)(the_array->elem_type());
937 gprint(s_printer, c_printer, data, ", %d",
938 the_array->upper_bound().constant() + 1);
941 else
943 (*s_printer)("incomplete array", data);
945 if (the_array->elem_type() != NULL)
947 gprint(s_printer, c_printer, data, " of %t",
948 the_array->elem_type());
950 break;
952 default:
953 assert(FALSE);
958 /* printdecl - output a C declaration for symbol p of type ty */
959 void printdecl(Symbol p, type_node *ty)
961 switch (p->sclass)
963 case AUTO:
964 fprint(stderr, "%s;\n", typestring(ty, p->name));
965 break;
966 case STATIC:
967 case EXTERN:
968 fprint(stderr, "%k %s;\n", p->sclass, typestring(ty, p->name));
969 case TYPEDEF:
970 case ENUM:
971 break;
972 default:
973 assert(FALSE);
977 /* printproto - output a prototype declaration for function p */
978 void printproto(Symbol p, Symbol callee[])
980 assert(p != NULL);
981 type_node *p_type = p->type;
982 assert(p_type->is_func());
983 func_type *the_func = (func_type *)p_type;
984 if (the_func->args_known())
986 printdecl(p, the_func);
988 else
990 unsigned num_args;
991 for (num_args = 0; callee[num_args] != NULL; ++num_args)
993 func_type *temp_func =
994 new func_type(freturn(the_func), num_args, FALSE);
995 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
996 temp_func->set_arg_type(arg_num, callee[arg_num]->type);
997 printdecl(p, temp_func);
998 delete temp_func;
1002 /* qual - construct the type `op ty' where op is CONST or VOLATILE */
1003 extern type_node *qual(int op, type_node *ty)
1005 assert(ty != NULL);
1006 base_symtab *the_symtab = ty->parent();
1007 assert(the_symtab != NULL);
1009 type_ops suif_type_op;
1010 if (op == CONST)
1011 suif_type_op = TYPE_CONST;
1012 else if (op == VOLATILE)
1013 suif_type_op = TYPE_VOLATILE;
1014 else
1015 assert(FALSE);
1017 type_node *new_type;
1018 if (isarray(ty))
1020 assert(ty == ty->unqual());
1021 assert(ty->is_array());
1022 array_type *old_array = (array_type *)ty;
1023 new_type = new array_type(qual(op, old_array->elem_type()),
1024 old_array->lower_bound(),
1025 old_array->upper_bound());
1027 else if (isfunc(ty))
1029 warning("qualified function type ignored\n");
1030 return ty;
1032 else if ((isconst(ty) && (op == CONST)) ||
1033 (isvolatile(ty) && (op == VOLATILE)))
1035 error("illegal type `%k %t'\n", op, ty);
1036 return ty;
1038 else
1040 new_type = new modifier_type(suif_type_op, ty);
1043 return the_symtab->install_type(new_type);
1046 /* typestring - return ty as C declaration for str, which may be "" */
1047 extern const char *typestring(type_node *ty, const char *str)
1049 assert(ty != NULL);
1050 const char *name = NULL;
1051 if (ty == chartype)
1052 name = chartype_sym->name;
1053 else if (ty == doubletype)
1054 name = doubletype_sym->name;
1055 else if (ty == floattype)
1056 name = floattype_sym->name;
1057 else if (ty == inttype)
1058 name = inttype_sym->name;
1059 else if (ty == longdouble)
1060 name = longdouble_sym->name;
1061 else if (ty == longtype)
1062 name = longtype_sym->name;
1063 else if (ty == longlong)
1064 name = longlong_sym->name;
1065 else if (ty == shorttype)
1066 name = shorttype_sym->name;
1067 else if (ty == signedchar)
1068 name = signedchar_sym->name;
1069 else if (ty == unsignedchar)
1070 name = unsignedchar_sym->name;
1071 else if (ty == unsignedlong)
1072 name = unsignedlong_sym->name;
1073 else if (ty == unsignedlonglong)
1074 name = unsignedlonglong_sym->name;
1075 else if (ty == unsignedshort)
1076 name = unsignedshort_sym->name;
1077 else if (ty == unsignedtype)
1078 name = unsignedtype_sym->name;
1079 else if (ty == voidtype)
1080 name = voidtype_sym->name;
1082 if (name != NULL)
1083 return ((*str != 0) ? stringf("%s %s", name, str) : name);
1085 switch (ty->op())
1087 case TYPE_CONST:
1088 case TYPE_VOLATILE:
1090 type_node *unqualled = ty->unqual();
1091 char *quals = "";
1092 if (ty->is_volatile())
1093 quals = stringf("%k %s", VOLATILE, quals);
1094 if (ty->is_const())
1095 quals = stringf("%k %s", CONST, quals);
1096 if (isptr(unqualled))
1097 return typestring(unqualled, stringf("%s%s", quals, str));
1098 else
1099 return stringf("%s%s", quals, typestring(unqualled, str));
1101 case TYPE_STRUCT:
1102 case TYPE_UNION:
1103 case TYPE_ENUM:
1105 Symbol p = findtype(ty);
1106 if (p != NULL)
1108 return ((*str != 0) ?
1109 stringf("%s %s", p->name, str) : p->name);
1112 const char *name;
1113 int op;
1114 if (ty->op() == TYPE_ENUM)
1116 enum_type *the_enum = (enum_type *)ty;
1117 name = the_enum->name();
1118 op = ENUM;
1120 else
1122 struct_type *the_struct = (struct_type *)ty;
1123 name = the_struct->name();
1124 op = (ty->op() == TYPE_STRUCT) ? STRUCT : UNION;
1126 assert(name != NULL);
1127 if ((*name >= '1') && (*name <= '9'))
1128 warning("unnamed %k in prototype\n", op);
1130 if (*str != 0)
1131 return stringf("%k %s %s", op, name, str);
1132 else
1133 return stringf("%k %s", op, name);
1135 case TYPE_PTR:
1137 Symbol p;
1138 if ((base_from_pointer(ty)->unqual()->size() !=
1139 target.size[C_char]) &&
1140 ((p = findtype(ty)) != NULL))
1142 return ((*str != 0) ?
1143 stringf("%s %s", p->name, str) : p->name);
1145 type_node *base_type = base_from_pointer(ty);
1146 str = stringf((isarray(base_type) || isfunc(base_type)) ?
1147 "(*%s)" : "*%s", str);
1148 return typestring(base_type, str);
1150 case TYPE_FUNC:
1152 Symbol p = findtype(ty);
1153 if (p != NULL)
1155 return ((*str != 0) ?
1156 stringf("%s %s", p->name, str) : p->name);
1159 func_type *the_func = (func_type *)ty;
1160 if (!the_func->args_known())
1162 str = stringf("%s()", str);
1163 return typestring(the_func->return_type(), str);
1165 else
1167 if (the_func->num_args() == 0)
1169 if (the_func->has_varargs())
1170 str = stringf("%s(...)", str);
1171 else
1172 str = stringf("%s(void)", str);
1174 else
1176 str = stringf("%s(%s", str,
1177 typestring(the_func->arg_type(0), ""));
1178 unsigned num_args = the_func->num_args();
1179 for (unsigned arg_num = 1; arg_num < num_args; ++arg_num)
1181 str = stringf("%s, %s", str,
1182 typestring(the_func->arg_type(arg_num),
1183 ""));
1185 if (the_func->has_varargs())
1186 str = stringf("%s, ...", str);
1187 str = stringf("%s)", str);
1189 return typestring(the_func->return_type(), str);
1192 case TYPE_ARRAY:
1194 Symbol p = findtype(ty);
1195 if (p != NULL)
1197 return ((*str != 0) ?
1198 stringf("%s %s", p->name, str) : p->name);
1201 array_type *the_array = (array_type *)ty;
1202 if (!the_array->are_bounds_unknown())
1204 str = stringf("%s[%d]", str,
1205 the_array->upper_bound().constant() + 1);
1206 return typestring(the_array->elem_type(), str);
1208 else
1210 str = stringf("%s[]", str);
1211 return typestring(the_array->elem_type(), str);
1214 default:
1215 assert(FALSE);
1218 assert(FALSE);
1219 return NULL;
1222 extern type_node *base_from_enum(type_node *the_type)
1224 assert(the_type != NULL);
1225 type_node *unqualled = the_type->unqual();
1226 assert(unqualled != NULL);
1227 assert(unqualled->is_enum());
1228 enum_type *the_enum = (enum_type *)unqualled;
1229 boolean is_signed = the_enum->is_signed();
1230 int the_size = the_enum->size();
1231 if (is_signed)
1233 if (the_size == inttype->size())
1234 return inttype;
1235 if (the_size == signedchar->size())
1236 return signedchar;
1237 if (the_size == shorttype->size())
1238 return shorttype;
1239 if (the_size == longtype->size())
1240 return longtype;
1241 if (the_size == longlong->size())
1242 return longlong;
1244 else
1246 if (the_size == unsignedtype->size())
1247 return unsignedtype;
1248 if (the_size == unsignedchar->size())
1249 return unsignedchar;
1250 if (the_size == unsignedshort->size())
1251 return unsignedshort;
1252 if (the_size == unsignedlong->size())
1253 return unsignedlong;
1254 if (the_size == unsignedlonglong->size())
1255 return unsignedlonglong;
1258 base_symtab *the_symtab = the_type->parent();
1259 assert(the_symtab != NULL);
1260 type_node *new_type = new base_type(TYPE_INT, the_size, is_signed);
1261 return the_symtab->install_type(new_type);
1264 extern type_node *base_from_pointer(type_node *the_type)
1266 assert(the_type != NULL);
1267 type_node *unqualled = the_type->unqual();
1268 assert(unqualled != NULL);
1269 assert(unqualled->is_ptr());
1270 ptr_type *the_pointer = (ptr_type *)unqualled;
1271 return the_pointer->ref_type();
1274 extern type_node *base_from_array(type_node *the_type)
1276 assert(the_type != NULL);
1277 type_node *unqualled = the_type->unqual();
1278 assert(unqualled != NULL);
1279 assert(unqualled->is_array());
1280 array_type *the_array = (array_type *)unqualled;
1281 return the_array->elem_type();
1286 * See ANSI/ISO 9899-1990, sections 6.1.2.6, 6.5.2, 6.5.3, and 6.5.4
1289 extern boolean compatible_types(type_node *type1, type_node *type2)
1291 if (!identically_qualified(type1, type2))
1292 return FALSE;
1294 type_node *t1 = type1->unqual();
1295 type_node *t2 = type2->unqual();
1297 if (t1 == t2)
1298 return TRUE;
1301 * ANSI/ISO 9899-1990, section 6.5.4.1, ``Semantics'', paragraph 2
1303 if (t2->op() == TYPE_ENUM)
1305 type_node *temp_type = t2;
1306 t2 = t1;
1307 t1 = temp_type;
1310 switch (t1->op())
1312 case TYPE_INT:
1313 case TYPE_FLOAT:
1314 case TYPE_VOID:
1315 return FALSE;
1316 case TYPE_PTR:
1318 if (t2->op() != TYPE_PTR)
1319 return FALSE;
1322 * ANSI/ISO 9899-1990, section 6.5.4.1, ``Semantics'',
1323 * paragraph 2
1325 return (compatible_types(base_from_pointer(t1),
1326 base_from_pointer(t2)));
1328 case TYPE_ARRAY:
1330 if (t2->op() != TYPE_ARRAY)
1331 return FALSE;
1334 * ANSI/ISO 9899-1990, section 6.5.4.2, ``Semantics'',
1335 * paragraph 2
1337 array_type *array1 = (array_type *)t1;
1338 array_type *array2 = (array_type *)t2;
1339 if ((!array1->are_bounds_unknown()) &&
1340 (!array2->are_bounds_unknown()))
1342 array_bound upper1 = array1->upper_bound();
1343 array_bound upper2 = array2->upper_bound();
1344 if (upper1 != upper2)
1345 return FALSE;
1347 return compatible_types(array1->elem_type(), array2->elem_type());
1349 case TYPE_FUNC:
1351 if (t2->op() != TYPE_FUNC)
1352 return FALSE;
1355 * ANSI/ISO 9899-1990, sectifon 6.5.4.3, ``Semantics'',
1356 * paragraph 6
1358 func_type *function1 = (func_type *)t1;
1359 func_type *function2 = (func_type *)t2;
1361 if (!compatible_types(function1->return_type(),
1362 function2->return_type()))
1364 return FALSE;
1367 if (!(function2->args_known()))
1369 func_type *temp_function = function2;
1370 function2 = function1;
1371 function1 = temp_function;
1374 if (function1->args_known())
1376 if (function1->has_varargs() != function2->has_varargs())
1377 return FALSE;
1379 unsigned num_args = function1->num_args();
1380 if (function2->num_args() != num_args)
1381 return FALSE;
1383 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
1385 type_node *arg1 = function1->arg_type(arg_num);
1386 type_node *arg2 = function2->arg_type(arg_num);
1387 if (!compatible_types(arg1->unqual(), arg2->unqual()))
1388 return FALSE;
1391 return TRUE;
1393 else if (function2->args_known())
1395 if (function2->has_varargs())
1396 return FALSE;
1398 unsigned num_args = function2->num_args();
1400 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
1402 type_node *arg = function2->arg_type(arg_num);
1403 if (!compatible_types(arg->unqual(),
1404 promote(arg->unqual())))
1406 return FALSE;
1410 return TRUE;
1412 else
1414 return TRUE;
1417 case TYPE_STRUCT:
1418 case TYPE_UNION:
1420 if (t1->op() != t2->op())
1421 return FALSE;
1423 boolean is_union = (t1->op() == TYPE_UNION);
1425 struct_type *struct1 = (struct_type *)t1;
1426 struct_type *struct2 = (struct_type *)t2;
1428 unsigned num_fields = struct1->num_fields();
1429 if (struct2->num_fields() != num_fields)
1430 return FALSE;
1432 for (unsigned field_num = 0; field_num < num_fields; ++field_num)
1434 const char *name1 = struct1->field_name(field_num);
1435 type_node *field1 = struct1->field_type(field_num);
1437 unsigned field_2_num;
1438 if (is_union)
1440 field_2_num = struct2->find_field_by_name(name1);
1441 if (field_2_num == (unsigned)-1)
1442 return FALSE;
1444 else
1446 field_2_num = field_num;
1449 if (strcmp(struct2->field_name(field_2_num), name1) != 0)
1450 return FALSE;
1452 if (field_is_bit_fields(struct1, field_num))
1454 if (!field_is_bit_fields(struct2, field_num))
1455 return FALSE;
1456 if (!bit_field_annotations_same(
1457 struct2->field_type(field_2_num), field1))
1459 return FALSE;
1462 else
1464 if (field_is_bit_fields(struct2, field_num))
1465 return FALSE;
1466 if (!compatible_types(struct2->field_type(field_2_num),
1467 field1))
1469 return FALSE;
1474 return TRUE;
1476 case TYPE_ENUM:
1479 * ANSI/ISO 9899-1990, section 6.5.4.1, ``Semantics'',
1480 * paragraph 2
1482 if (t2->op() != TYPE_ENUM)
1484 if (compatible_types(base_from_enum(t1), t2))
1486 if (Aflag >= 1)
1488 warning("compatibility between `%t' and `%t' is "
1489 "compiler-dependent\n", t1, t2);
1491 return TRUE;
1493 return FALSE;
1496 enum_type *enum1 = (enum_type *)t1;
1497 enum_type *enum2 = (enum_type *)t2;
1499 unsigned num_values = enum1->num_values();
1500 if (enum2->num_values() != num_values)
1501 return FALSE;
1503 for (unsigned value_num = 0; value_num < num_values; ++value_num)
1505 const char *value_name = enum1->member(value_num);
1506 int value_value = enum1->value(value_num);
1507 int value_2_num = enum2->find_member_by_value(value_value);
1508 if (value_2_num == -1)
1509 return FALSE;
1510 if (strcmp(enum2->member(value_2_num), value_name) != 0)
1511 return FALSE;
1512 if (enum2->value(value_2_num) != value_value)
1513 return FALSE;
1515 return TRUE;
1517 default:
1518 assert(FALSE);
1520 return FALSE;
1523 static boolean identically_qualified(type_node *type1, type_node *type2)
1525 return ((type1->is_const() == type2->is_const()) &&
1526 (type1->is_volatile() == type2->is_volatile()));
1529 static boolean bit_field_annotations_same(type_node *field1,
1530 type_node *field2)
1532 immed_list *list1 = (immed_list *)(field1->peek_annote(k_bit_field_info));
1533 immed_list *list2 = (immed_list *)(field2->peek_annote(k_bit_field_info));
1534 if ((list1 == NULL) && (list2 == NULL))
1535 return TRUE;
1536 if ((list1 == NULL) || (list2 == NULL))
1537 return FALSE;
1538 immed_list_iter iter1(list1);
1539 immed_list_iter iter2(list2);
1540 while ((!iter1.is_empty()) && (!iter2.is_empty()))
1542 immed value1 = iter1.step();
1543 immed value2 = iter2.step();
1544 if (value1 != value2)
1545 return FALSE;
1547 return (iter1.is_empty() == iter2.is_empty());
1550 extern array_type *make_array(type_node *element_type)
1552 assert(element_type != NULL);
1553 base_symtab *the_symtab = element_type->parent();
1554 assert(the_symtab != NULL);
1555 array_type *new_type = new array_type(element_type, array_bound(0));
1556 return (array_type *)(the_symtab->install_type(new_type));
1559 extern boolean has_constant_in_fields(type_node *the_type)
1561 if (the_type->is_const())
1562 return TRUE;
1564 type_node *unqualled = the_type->unqual();
1565 if (!unqualled->is_struct())
1566 return FALSE;
1568 struct_type *the_struct = (struct_type *)unqualled;
1569 int num_fields = the_struct->num_fields();
1570 for (int field_num = 0; field_num < num_fields; ++field_num)
1572 if (has_constant_in_fields(the_struct->field_type(field_num)))
1573 return TRUE;
1575 return FALSE;
1578 extern type_node *create_function_type(type_node *return_type,
1579 type_node **arguments)
1581 assert(return_type != NULL);
1583 func_type *result = new func_type(return_type);
1584 if (arguments == NULL)
1586 result->set_args_unknown();
1588 else
1590 result->set_args_known();
1591 unsigned num_args = 0;
1592 type_node **current_arg = arguments;
1593 while (*current_arg != NULL)
1595 if (*current_arg == voidtype)
1597 if (num_args > 0)
1598 result->set_varargs(TRUE);
1599 break;
1601 ++current_arg;
1602 ++num_args;
1604 result->set_num_args(num_args);
1605 for (unsigned arg_num = 0; arg_num < num_args; ++arg_num)
1606 result->set_arg_type(arg_num, arguments[arg_num]);
1607 delete arguments;
1609 return result;
1612 extern boolean field_is_bit_fields(struct_type *the_struct,
1613 unsigned field_num)
1615 assert(the_struct != NULL);
1616 assert(the_struct->num_fields() > field_num);
1617 type_node *field_type = the_struct->field_type(field_num);
1618 assert(field_type != NULL);
1619 return (field_type->peek_annote(k_bit_field_info) != NULL);
1622 extern struct field *bit_field_list(struct_type *the_struct,
1623 unsigned field_num)
1625 assert(the_struct != NULL);
1626 assert(the_struct->num_fields() > field_num);
1627 type_node *field_type = the_struct->field_type(field_num);
1628 assert(field_type != NULL);
1630 immed_list *descriptor =
1631 (immed_list *)(field_type->peek_annote(k_bit_field_info));
1632 if (descriptor == NULL)
1633 return NULL;
1635 struct field *result = NULL;
1636 struct field **next_position = &result;
1637 immed_list_iter the_iter(descriptor);
1638 while (!the_iter.is_empty())
1640 #ifndef DEAL_WITH_GCC_BRAIN_DAMAGE
1641 *next_position = new struct field;
1642 #else
1644 // gcc version 2.6.3 gets a parse error on the code above. The code
1645 // looks like it should be perfectly legal, and both gcc 2.5.8 and
1646 // the IRIX 5.3 C++ compiler have no problem with it, so I'm inclined
1647 // to think the problem is caused by brain damage in the g++
1648 // front-end. A work-around follows.
1650 *next_position = (struct field *)operator new(sizeof(struct field));
1651 #endif
1653 immed type_value = the_iter.step();
1654 assert(type_value.is_string());
1655 const char *type_string = type_value.string();
1656 boolean is_volatile = FALSE;
1657 boolean is_constant = FALSE;
1659 if (strcmp(type_string, "volatile") == 0)
1661 is_volatile = TRUE;
1662 assert(!the_iter.is_empty());
1663 type_value = the_iter.step();
1664 assert(type_value.is_string());
1665 type_string = type_value.string();
1668 if (strcmp(type_string, "const") == 0)
1670 is_constant = TRUE;
1671 assert(!the_iter.is_empty());
1672 type_value = the_iter.step();
1673 assert(type_value.is_string());
1674 type_string = type_value.string();
1677 if (strcmp(type_string, "int") == 0)
1678 (*next_position)->type = inttype;
1679 else if (strcmp(type_string, "unsigned") == 0)
1680 (*next_position)->type = unsignedtype;
1681 else
1682 assert(FALSE);
1684 assert(!the_iter.is_empty());
1685 immed name_value = the_iter.step();
1686 assert(name_value.is_string());
1687 (*next_position)->name = name_value.string();
1689 assert(!the_iter.is_empty());
1690 immed from_value = the_iter.step();
1691 assert(from_value.is_integer());
1692 (*next_position)->from = from_value.integer();
1694 assert(!the_iter.is_empty());
1695 immed to_value = the_iter.step();
1696 assert(to_value.is_integer());
1697 (*next_position)->to = to_value.integer();
1699 (*next_position)->offset = 0;
1700 (*next_position)->link = NULL;
1702 next_position = &((*next_position)->link);
1704 return result;
1707 extern Table get_field_table(struct_type *the_struct)
1709 assert(the_struct != NULL);
1710 return (Table)(the_struct->peek_annote(k_field_table));
1713 extern void set_field_table(struct_type *the_struct, Table new_table)
1715 assert(the_struct != NULL);
1716 the_struct->append_annote(k_field_table, new_table);
1720 * This function expects an integer or pointer type and returns an unsigned
1721 * integer type of the same size.
1723 extern type_node *unsigned_int_version(type_node *original_type)
1725 if (original_type->op() == TYPE_INT)
1727 base_type *the_base = (base_type *)original_type;
1728 if (!the_base->is_signed())
1729 return original_type;
1731 else if (original_type->op() != TYPE_PTR)
1733 return original_type;
1735 type_node *new_type = new base_type(TYPE_INT,
1736 original_type->size(), FALSE);
1737 return original_type->parent()->install_type(new_type);
1740 static void use_field(struct_type *the_struct, char *name)
1742 if (xref)
1744 Table field_table = get_field_table(the_struct);
1745 assert(field_table != NULL);
1746 Symbol q = lookup(name, field_table);
1747 assert(q != NULL);
1748 use(q, src);
1752 static Coordinate *get_source(type_node *the_type)
1754 assert(the_type != NULL);
1755 immed_list *immeds =
1756 (immed_list *)(the_type->peek_annote(k_type_source_coordinates));
1757 return immeds_to_coordinate(immeds);
1760 static void set_source(type_node *the_type, Coordinate source)
1762 assert(the_type != NULL);
1763 immed_list *immeds = coordinate_to_immeds(source);
1764 the_type->append_annote(k_type_source_coordinates, immeds);