PR c++/3478
[official-gcc.git] / gcc / c-typeck.c
blobec00801a824df32ca17210e8d75a4b3f24ea2771
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static tree qualify_type (tree, tree);
53 static int same_translation_unit_p (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree unary_complex_lvalue (enum tree_code, tree, int);
64 static void pedantic_lvalue_warning (enum tree_code);
65 static tree internal_build_compound_expr (tree, int);
66 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
67 int);
68 static void warn_for_assignment (const char *, const char *, tree, int);
69 static tree valid_compound_expr_initializer (tree, tree);
70 static void push_string (const char *);
71 static void push_member_name (tree);
72 static void push_array_bounds (int);
73 static int spelling_length (void);
74 static char *print_spelling (char *);
75 static void warning_init (const char *);
76 static tree digest_init (tree, tree, int);
77 static void output_init_element (tree, tree, tree, int);
78 static void output_pending_init_elements (int);
79 static int set_designator (int);
80 static void push_range_stack (tree);
81 static void add_pending_init (tree, tree);
82 static void set_nonincremental_init (void);
83 static void set_nonincremental_init_from_string (tree);
84 static tree find_init_member (tree);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
89 tree
90 require_complete_type (tree value)
92 tree type = TREE_TYPE (value);
94 if (value == error_mark_node || type == error_mark_node)
95 return error_mark_node;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type))
99 return value;
101 c_incomplete_type_error (value, type);
102 return error_mark_node;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
109 void
110 c_incomplete_type_error (tree value, tree type)
112 const char *type_code_string;
114 /* Avoid duplicate error message. */
115 if (TREE_CODE (type) == ERROR_MARK)
116 return;
118 if (value != 0 && (TREE_CODE (value) == VAR_DECL
119 || TREE_CODE (value) == PARM_DECL))
120 error ("`%s' has an incomplete type",
121 IDENTIFIER_POINTER (DECL_NAME (value)));
122 else
124 retry:
125 /* We must print an error message. Be clever about what it says. */
127 switch (TREE_CODE (type))
129 case RECORD_TYPE:
130 type_code_string = "struct";
131 break;
133 case UNION_TYPE:
134 type_code_string = "union";
135 break;
137 case ENUMERAL_TYPE:
138 type_code_string = "enum";
139 break;
141 case VOID_TYPE:
142 error ("invalid use of void expression");
143 return;
145 case ARRAY_TYPE:
146 if (TYPE_DOMAIN (type))
148 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
150 error ("invalid use of flexible array member");
151 return;
153 type = TREE_TYPE (type);
154 goto retry;
156 error ("invalid use of array with unspecified bounds");
157 return;
159 default:
160 abort ();
163 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
164 error ("invalid use of undefined type `%s %s'",
165 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
166 else
167 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
168 error ("invalid use of incomplete typedef `%s'",
169 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
173 /* Given a type, apply default promotions wrt unnamed function
174 arguments and return the new type. */
176 tree
177 c_type_promotes_to (tree type)
179 if (TYPE_MAIN_VARIANT (type) == float_type_node)
180 return double_type_node;
182 if (c_promoting_integer_type_p (type))
184 /* Preserve unsignedness if not really getting any wider. */
185 if (TREE_UNSIGNED (type)
186 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
187 return unsigned_type_node;
188 return integer_type_node;
191 return type;
194 /* Return a variant of TYPE which has all the type qualifiers of LIKE
195 as well as those of TYPE. */
197 static tree
198 qualify_type (tree type, tree like)
200 return c_build_qualified_type (type,
201 TYPE_QUALS (type) | TYPE_QUALS (like));
204 /* Return the common type of two types.
205 We assume that comptypes has already been done and returned 1;
206 if that isn't so, this may crash. In particular, we assume that qualifiers
207 match.
209 This is the type for the result of most arithmetic operations
210 if the operands have the given two types. */
212 tree
213 common_type (tree t1, tree t2)
215 enum tree_code code1;
216 enum tree_code code2;
217 tree attributes;
219 /* Save time if the two types are the same. */
221 if (t1 == t2) return t1;
223 /* If one type is nonsense, use the other. */
224 if (t1 == error_mark_node)
225 return t2;
226 if (t2 == error_mark_node)
227 return t1;
229 /* Merge the attributes. */
230 attributes = (*targetm.merge_type_attributes) (t1, t2);
232 /* Treat an enum type as the unsigned integer type of the same width. */
234 if (TREE_CODE (t1) == ENUMERAL_TYPE)
235 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
236 if (TREE_CODE (t2) == ENUMERAL_TYPE)
237 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
239 code1 = TREE_CODE (t1);
240 code2 = TREE_CODE (t2);
242 /* If one type is complex, form the common type of the non-complex
243 components, then make that complex. Use T1 or T2 if it is the
244 required type. */
245 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
247 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
248 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
249 tree subtype = common_type (subtype1, subtype2);
251 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
252 return build_type_attribute_variant (t1, attributes);
253 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
254 return build_type_attribute_variant (t2, attributes);
255 else
256 return build_type_attribute_variant (build_complex_type (subtype),
257 attributes);
260 switch (code1)
262 case INTEGER_TYPE:
263 case REAL_TYPE:
264 /* If only one is real, use it as the result. */
266 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
267 return build_type_attribute_variant (t1, attributes);
269 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
270 return build_type_attribute_variant (t2, attributes);
272 /* Both real or both integers; use the one with greater precision. */
274 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
275 return build_type_attribute_variant (t1, attributes);
276 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
277 return build_type_attribute_variant (t2, attributes);
279 /* Same precision. Prefer longs to ints even when same size. */
281 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
283 return build_type_attribute_variant (long_unsigned_type_node,
284 attributes);
286 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
289 /* But preserve unsignedness from the other type,
290 since long cannot hold all the values of an unsigned int. */
291 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
292 t1 = long_unsigned_type_node;
293 else
294 t1 = long_integer_type_node;
295 return build_type_attribute_variant (t1, attributes);
298 /* Likewise, prefer long double to double even if same size. */
299 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
300 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
301 return build_type_attribute_variant (long_double_type_node,
302 attributes);
304 /* Otherwise prefer the unsigned one. */
306 if (TREE_UNSIGNED (t1))
307 return build_type_attribute_variant (t1, attributes);
308 else
309 return build_type_attribute_variant (t2, attributes);
311 case POINTER_TYPE:
312 /* For two pointers, do this recursively on the target type,
313 and combine the qualifiers of the two types' targets. */
314 /* This code was turned off; I don't know why.
315 But ANSI C specifies doing this with the qualifiers.
316 So I turned it on again. */
318 tree pointed_to_1 = TREE_TYPE (t1);
319 tree pointed_to_2 = TREE_TYPE (t2);
320 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
321 TYPE_MAIN_VARIANT (pointed_to_2));
322 t1 = build_pointer_type (c_build_qualified_type
323 (target,
324 TYPE_QUALS (pointed_to_1) |
325 TYPE_QUALS (pointed_to_2)));
326 return build_type_attribute_variant (t1, attributes);
329 case ARRAY_TYPE:
331 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
332 /* Save space: see if the result is identical to one of the args. */
333 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
334 return build_type_attribute_variant (t1, attributes);
335 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
336 return build_type_attribute_variant (t2, attributes);
337 /* Merge the element types, and have a size if either arg has one. */
338 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
339 return build_type_attribute_variant (t1, attributes);
342 case FUNCTION_TYPE:
343 /* Function types: prefer the one that specified arg types.
344 If both do, merge the arg types. Also merge the return types. */
346 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
347 tree p1 = TYPE_ARG_TYPES (t1);
348 tree p2 = TYPE_ARG_TYPES (t2);
349 int len;
350 tree newargs, n;
351 int i;
353 /* Save space: see if the result is identical to one of the args. */
354 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
355 return build_type_attribute_variant (t1, attributes);
356 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Simple way if one arg fails to specify argument types. */
360 if (TYPE_ARG_TYPES (t1) == 0)
362 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
363 return build_type_attribute_variant (t1, attributes);
365 if (TYPE_ARG_TYPES (t2) == 0)
367 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
368 return build_type_attribute_variant (t1, attributes);
371 /* If both args specify argument types, we must merge the two
372 lists, argument by argument. */
374 pushlevel (0);
375 declare_parm_level ();
377 len = list_length (p1);
378 newargs = 0;
380 for (i = 0; i < len; i++)
381 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
383 n = newargs;
385 for (; p1;
386 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
388 /* A null type means arg type is not specified.
389 Take whatever the other function type has. */
390 if (TREE_VALUE (p1) == 0)
392 TREE_VALUE (n) = TREE_VALUE (p2);
393 goto parm_done;
395 if (TREE_VALUE (p2) == 0)
397 TREE_VALUE (n) = TREE_VALUE (p1);
398 goto parm_done;
401 /* Given wait (union {union wait *u; int *i} *)
402 and wait (union wait *),
403 prefer union wait * as type of parm. */
404 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
405 && TREE_VALUE (p1) != TREE_VALUE (p2))
407 tree memb;
408 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
409 memb; memb = TREE_CHAIN (memb))
410 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
411 COMPARE_STRICT))
413 TREE_VALUE (n) = TREE_VALUE (p2);
414 if (pedantic)
415 pedwarn ("function types not truly compatible in ISO C");
416 goto parm_done;
419 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
420 && TREE_VALUE (p2) != TREE_VALUE (p1))
422 tree memb;
423 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
424 memb; memb = TREE_CHAIN (memb))
425 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
426 COMPARE_STRICT))
428 TREE_VALUE (n) = TREE_VALUE (p1);
429 if (pedantic)
430 pedwarn ("function types not truly compatible in ISO C");
431 goto parm_done;
434 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
435 parm_done: ;
438 poplevel (0, 0, 0);
440 t1 = build_function_type (valtype, newargs);
441 /* ... falls through ... */
444 default:
445 return build_type_attribute_variant (t1, attributes);
450 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
451 or various other operations. Return 2 if they are compatible
452 but a warning may be needed if you use them together. */
455 comptypes (tree type1, tree type2, int flags)
457 tree t1 = type1;
458 tree t2 = type2;
459 int attrval, val;
461 /* Suppress errors caused by previously reported errors. */
463 if (t1 == t2 || !t1 || !t2
464 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
465 return 1;
467 /* If either type is the internal version of sizetype, return the
468 language version. */
469 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
470 && TYPE_DOMAIN (t1) != 0)
471 t1 = TYPE_DOMAIN (t1);
473 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
474 && TYPE_DOMAIN (t2) != 0)
475 t2 = TYPE_DOMAIN (t2);
477 /* Enumerated types are compatible with integer types, but this is
478 not transitive: two enumerated types in the same translation unit
479 are compatible with each other only if they are the same type. */
481 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
482 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
483 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
484 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
486 if (t1 == t2)
487 return 1;
489 /* Different classes of types can't be compatible. */
491 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
493 /* Qualifiers must match. */
495 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
496 return 0;
498 /* Allow for two different type nodes which have essentially the same
499 definition. Note that we already checked for equality of the type
500 qualifiers (just above). */
502 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 return 1;
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
506 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
507 return 0;
509 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
510 val = 0;
512 switch (TREE_CODE (t1))
514 case POINTER_TYPE:
515 /* We must give ObjC the first crack at comparing pointers, since
516 protocol qualifiers may be involved. */
517 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
518 break;
519 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
520 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
521 break;
523 case FUNCTION_TYPE:
524 val = function_types_compatible_p (t1, t2, flags);
525 break;
527 case ARRAY_TYPE:
529 tree d1 = TYPE_DOMAIN (t1);
530 tree d2 = TYPE_DOMAIN (t2);
531 bool d1_variable, d2_variable;
532 bool d1_zero, d2_zero;
533 val = 1;
535 /* Target types must match incl. qualifiers. */
536 if (TREE_TYPE (t1) != TREE_TYPE (t2)
537 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
538 flags)))
539 return 0;
541 /* Sizes must match unless one is missing or variable. */
542 if (d1 == 0 || d2 == 0 || d1 == d2)
543 break;
545 d1_zero = ! TYPE_MAX_VALUE (d1);
546 d2_zero = ! TYPE_MAX_VALUE (d2);
548 d1_variable = (! d1_zero
549 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
550 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
551 d2_variable = (! d2_zero
552 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
553 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
555 if (d1_variable || d2_variable)
556 break;
557 if (d1_zero && d2_zero)
558 break;
559 if (d1_zero || d2_zero
560 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
561 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
562 val = 0;
564 break;
567 case RECORD_TYPE:
568 /* We are dealing with two distinct structs. In assorted Objective-C
569 corner cases, however, these can still be deemed equivalent. */
570 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
571 val = 1;
573 case ENUMERAL_TYPE:
574 case UNION_TYPE:
575 if (val != 1 && !same_translation_unit_p (t1, t2))
576 val = tagged_types_tu_compatible_p (t1, t2, flags);
577 break;
579 case VECTOR_TYPE:
580 /* The target might allow certain vector types to be compatible. */
581 val = (*targetm.vector_opaque_p) (t1)
582 || (*targetm.vector_opaque_p) (t2);
583 break;
585 default:
586 break;
588 return attrval == 2 && val == 1 ? 2 : val;
591 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
592 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
593 to 1 or 0 depending if the check of the pointer types is meant to
594 be reflexive or not (typically, assignments are not reflexive,
595 while comparisons are reflexive).
598 static int
599 comp_target_types (tree ttl, tree ttr, int reflexive)
601 int val;
603 /* Give objc_comptypes a crack at letting these types through. */
604 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
605 return val;
607 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
608 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
610 if (val == 2 && pedantic)
611 pedwarn ("types are not quite compatible");
612 return val;
615 /* Subroutines of `comptypes'. */
617 /* Determine whether two types derive from the same translation unit.
618 If the CONTEXT chain ends in a null, that type's context is still
619 being parsed, so if two types have context chains ending in null,
620 they're in the same translation unit. */
621 static int
622 same_translation_unit_p (tree t1, tree t2)
624 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
625 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
627 case 'd': t1 = DECL_CONTEXT (t1); break;
628 case 't': t1 = TYPE_CONTEXT (t1); break;
629 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
630 default: abort ();
633 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
634 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
636 case 'd': t2 = DECL_CONTEXT (t1); break;
637 case 't': t2 = TYPE_CONTEXT (t2); break;
638 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
639 default: abort ();
642 return t1 == t2;
645 /* The C standard says that two structures in different translation
646 units are compatible with each other only if the types of their
647 fields are compatible (among other things). So, consider two copies
648 of this structure: */
650 struct tagged_tu_seen {
651 const struct tagged_tu_seen * next;
652 tree t1;
653 tree t2;
656 /* Can they be compatible with each other? We choose to break the
657 recursion by allowing those types to be compatible. */
659 static const struct tagged_tu_seen * tagged_tu_seen_base;
661 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
662 compatible. If the two types are not the same (which has been
663 checked earlier), this can only happen when multiple translation
664 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
665 rules. */
667 static int
668 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
670 tree s1, s2;
671 bool needs_warning = false;
673 /* We have to verify that the tags of the types are the same. This
674 is harder than it looks because this may be a typedef, so we have
675 to go look at the original type. It may even be a typedef of a
676 typedef... */
677 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
678 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
680 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
681 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
683 /* C90 didn't have the requirement that the two tags be the same. */
684 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
685 return 0;
687 /* C90 didn't say what happened if one or both of the types were
688 incomplete; we choose to follow C99 rules here, which is that they
689 are compatible. */
690 if (TYPE_SIZE (t1) == NULL
691 || TYPE_SIZE (t2) == NULL)
692 return 1;
695 const struct tagged_tu_seen * tts_i;
696 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
697 if (tts_i->t1 == t1 && tts_i->t2 == t2)
698 return 1;
701 switch (TREE_CODE (t1))
703 case ENUMERAL_TYPE:
705 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
706 return 0;
708 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
710 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
711 if (s2 == NULL
712 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
713 return 0;
715 return 1;
718 case UNION_TYPE:
720 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
721 return 0;
723 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
725 bool ok = false;
726 struct tagged_tu_seen tts;
728 tts.next = tagged_tu_seen_base;
729 tts.t1 = t1;
730 tts.t2 = t2;
731 tagged_tu_seen_base = &tts;
733 if (DECL_NAME (s1) != NULL)
734 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
735 if (DECL_NAME (s1) == DECL_NAME (s2))
737 int result;
738 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
739 if (result == 0)
740 break;
741 if (result == 2)
742 needs_warning = true;
744 if (TREE_CODE (s1) == FIELD_DECL
745 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
746 DECL_FIELD_BIT_OFFSET (s2)) != 1)
747 break;
749 ok = true;
750 break;
752 tagged_tu_seen_base = tts.next;
753 if (! ok)
754 return 0;
756 return needs_warning ? 2 : 1;
759 case RECORD_TYPE:
761 struct tagged_tu_seen tts;
763 tts.next = tagged_tu_seen_base;
764 tts.t1 = t1;
765 tts.t2 = t2;
766 tagged_tu_seen_base = &tts;
768 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
769 s1 && s2;
770 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
772 int result;
773 if (TREE_CODE (s1) != TREE_CODE (s2)
774 || DECL_NAME (s1) != DECL_NAME (s2))
775 break;
776 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
777 if (result == 0)
778 break;
779 if (result == 2)
780 needs_warning = true;
782 if (TREE_CODE (s1) == FIELD_DECL
783 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
784 DECL_FIELD_BIT_OFFSET (s2)) != 1)
785 break;
787 tagged_tu_seen_base = tts.next;
788 if (s1 && s2)
789 return 0;
790 return needs_warning ? 2 : 1;
793 default:
794 abort ();
798 /* Return 1 if two function types F1 and F2 are compatible.
799 If either type specifies no argument types,
800 the other must specify a fixed number of self-promoting arg types.
801 Otherwise, if one type specifies only the number of arguments,
802 the other must specify that number of self-promoting arg types.
803 Otherwise, the argument types must match. */
805 static int
806 function_types_compatible_p (tree f1, tree f2, int flags)
808 tree args1, args2;
809 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
810 int val = 1;
811 int val1;
812 tree ret1, ret2;
814 ret1 = TREE_TYPE (f1);
815 ret2 = TREE_TYPE (f2);
817 /* 'volatile' qualifiers on a function's return type mean the function
818 is noreturn. */
819 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
820 pedwarn ("function return types not compatible due to `volatile'");
821 if (TYPE_VOLATILE (ret1))
822 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
823 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
824 if (TYPE_VOLATILE (ret2))
825 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
826 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
827 val = comptypes (ret1, ret2, flags);
828 if (val == 0)
829 return 0;
831 args1 = TYPE_ARG_TYPES (f1);
832 args2 = TYPE_ARG_TYPES (f2);
834 /* An unspecified parmlist matches any specified parmlist
835 whose argument types don't need default promotions. */
837 if (args1 == 0)
839 if (!self_promoting_args_p (args2))
840 return 0;
841 /* If one of these types comes from a non-prototype fn definition,
842 compare that with the other type's arglist.
843 If they don't match, ask for a warning (but no error). */
844 if (TYPE_ACTUAL_ARG_TYPES (f1)
845 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
846 flags))
847 val = 2;
848 return val;
850 if (args2 == 0)
852 if (!self_promoting_args_p (args1))
853 return 0;
854 if (TYPE_ACTUAL_ARG_TYPES (f2)
855 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
856 flags))
857 val = 2;
858 return val;
861 /* Both types have argument lists: compare them and propagate results. */
862 val1 = type_lists_compatible_p (args1, args2, flags);
863 return val1 != 1 ? val1 : val;
866 /* Check two lists of types for compatibility,
867 returning 0 for incompatible, 1 for compatible,
868 or 2 for compatible with warning. */
870 static int
871 type_lists_compatible_p (tree args1, tree args2, int flags)
873 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
874 int val = 1;
875 int newval = 0;
877 while (1)
879 if (args1 == 0 && args2 == 0)
880 return val;
881 /* If one list is shorter than the other,
882 they fail to match. */
883 if (args1 == 0 || args2 == 0)
884 return 0;
885 /* A null pointer instead of a type
886 means there is supposed to be an argument
887 but nothing is specified about what type it has.
888 So match anything that self-promotes. */
889 if (TREE_VALUE (args1) == 0)
891 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
892 return 0;
894 else if (TREE_VALUE (args2) == 0)
896 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
897 return 0;
899 /* If one of the lists has an error marker, ignore this arg. */
900 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
901 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
903 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
904 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
905 flags)))
907 /* Allow wait (union {union wait *u; int *i} *)
908 and wait (union wait *) to be compatible. */
909 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
910 && (TYPE_NAME (TREE_VALUE (args1)) == 0
911 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
912 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
913 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
914 TYPE_SIZE (TREE_VALUE (args2))))
916 tree memb;
917 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
918 memb; memb = TREE_CHAIN (memb))
919 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
920 flags))
921 break;
922 if (memb == 0)
923 return 0;
925 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
926 && (TYPE_NAME (TREE_VALUE (args2)) == 0
927 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
928 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
929 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
930 TYPE_SIZE (TREE_VALUE (args1))))
932 tree memb;
933 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
934 memb; memb = TREE_CHAIN (memb))
935 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
936 flags))
937 break;
938 if (memb == 0)
939 return 0;
941 else
942 return 0;
945 /* comptypes said ok, but record if it said to warn. */
946 if (newval > val)
947 val = newval;
949 args1 = TREE_CHAIN (args1);
950 args2 = TREE_CHAIN (args2);
954 /* Compute the size to increment a pointer by. */
956 tree
957 c_size_in_bytes (tree type)
959 enum tree_code code = TREE_CODE (type);
961 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
962 return size_one_node;
964 if (!COMPLETE_OR_VOID_TYPE_P (type))
966 error ("arithmetic on pointer to an incomplete type");
967 return size_one_node;
970 /* Convert in case a char is more than one unit. */
971 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
972 size_int (TYPE_PRECISION (char_type_node)
973 / BITS_PER_UNIT));
976 /* Return either DECL or its known constant value (if it has one). */
978 tree
979 decl_constant_value (tree decl)
981 if (/* Don't change a variable array bound or initial value to a constant
982 in a place where a variable is invalid. */
983 current_function_decl != 0
984 && ! TREE_THIS_VOLATILE (decl)
985 && TREE_READONLY (decl)
986 && DECL_INITIAL (decl) != 0
987 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
988 /* This is invalid if initial value is not constant.
989 If it has either a function call, a memory reference,
990 or a variable, then re-evaluating it could give different results. */
991 && TREE_CONSTANT (DECL_INITIAL (decl))
992 /* Check for cases where this is sub-optimal, even though valid. */
993 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
994 return DECL_INITIAL (decl);
995 return decl;
998 /* Return either DECL or its known constant value (if it has one), but
999 return DECL if pedantic or DECL has mode BLKmode. This is for
1000 bug-compatibility with the old behavior of decl_constant_value
1001 (before GCC 3.0); every use of this function is a bug and it should
1002 be removed before GCC 3.1. It is not appropriate to use pedantic
1003 in a way that affects optimization, and BLKmode is probably not the
1004 right test for avoiding misoptimizations either. */
1006 static tree
1007 decl_constant_value_for_broken_optimization (tree decl)
1009 if (pedantic || DECL_MODE (decl) == BLKmode)
1010 return decl;
1011 else
1012 return decl_constant_value (decl);
1016 /* Perform the default conversion of arrays and functions to pointers.
1017 Return the result of converting EXP. For any other expression, just
1018 return EXP. */
1020 static tree
1021 default_function_array_conversion (tree exp)
1023 tree orig_exp;
1024 tree type = TREE_TYPE (exp);
1025 enum tree_code code = TREE_CODE (type);
1026 int not_lvalue = 0;
1028 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1029 an lvalue.
1031 Do not use STRIP_NOPS here! It will remove conversions from pointer
1032 to integer and cause infinite recursion. */
1033 orig_exp = exp;
1034 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1035 || (TREE_CODE (exp) == NOP_EXPR
1036 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1038 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1039 not_lvalue = 1;
1040 exp = TREE_OPERAND (exp, 0);
1043 /* Preserve the original expression code. */
1044 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1045 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1047 if (code == FUNCTION_TYPE)
1049 return build_unary_op (ADDR_EXPR, exp, 0);
1051 if (code == ARRAY_TYPE)
1053 tree adr;
1054 tree restype = TREE_TYPE (type);
1055 tree ptrtype;
1056 int constp = 0;
1057 int volatilep = 0;
1058 int lvalue_array_p;
1060 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1062 constp = TREE_READONLY (exp);
1063 volatilep = TREE_THIS_VOLATILE (exp);
1066 if (TYPE_QUALS (type) || constp || volatilep)
1067 restype
1068 = c_build_qualified_type (restype,
1069 TYPE_QUALS (type)
1070 | (constp * TYPE_QUAL_CONST)
1071 | (volatilep * TYPE_QUAL_VOLATILE));
1073 if (TREE_CODE (exp) == INDIRECT_REF)
1074 return convert (TYPE_POINTER_TO (restype),
1075 TREE_OPERAND (exp, 0));
1077 if (TREE_CODE (exp) == COMPOUND_EXPR)
1079 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1080 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1081 TREE_OPERAND (exp, 0), op1);
1084 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1085 if (!flag_isoc99 && !lvalue_array_p)
1087 /* Before C99, non-lvalue arrays do not decay to pointers.
1088 Normally, using such an array would be invalid; but it can
1089 be used correctly inside sizeof or as a statement expression.
1090 Thus, do not give an error here; an error will result later. */
1091 return exp;
1094 ptrtype = build_pointer_type (restype);
1096 if (TREE_CODE (exp) == VAR_DECL)
1098 /* ??? This is not really quite correct
1099 in that the type of the operand of ADDR_EXPR
1100 is not the target type of the type of the ADDR_EXPR itself.
1101 Question is, can this lossage be avoided? */
1102 adr = build1 (ADDR_EXPR, ptrtype, exp);
1103 if (!c_mark_addressable (exp))
1104 return error_mark_node;
1105 TREE_CONSTANT (adr) = staticp (exp);
1106 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1107 return adr;
1109 /* This way is better for a COMPONENT_REF since it can
1110 simplify the offset for a component. */
1111 adr = build_unary_op (ADDR_EXPR, exp, 1);
1112 return convert (ptrtype, adr);
1114 return exp;
1117 /* Perform default promotions for C data used in expressions.
1118 Arrays and functions are converted to pointers;
1119 enumeral types or short or char, to int.
1120 In addition, manifest constants symbols are replaced by their values. */
1122 tree
1123 default_conversion (tree exp)
1125 tree orig_exp;
1126 tree type = TREE_TYPE (exp);
1127 enum tree_code code = TREE_CODE (type);
1129 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1130 return default_function_array_conversion (exp);
1132 /* Constants can be used directly unless they're not loadable. */
1133 if (TREE_CODE (exp) == CONST_DECL)
1134 exp = DECL_INITIAL (exp);
1136 /* Replace a nonvolatile const static variable with its value unless
1137 it is an array, in which case we must be sure that taking the
1138 address of the array produces consistent results. */
1139 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1141 exp = decl_constant_value_for_broken_optimization (exp);
1142 type = TREE_TYPE (exp);
1145 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1146 an lvalue.
1148 Do not use STRIP_NOPS here! It will remove conversions from pointer
1149 to integer and cause infinite recursion. */
1150 orig_exp = exp;
1151 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1152 || (TREE_CODE (exp) == NOP_EXPR
1153 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1154 exp = TREE_OPERAND (exp, 0);
1156 /* Preserve the original expression code. */
1157 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1158 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1160 /* Normally convert enums to int,
1161 but convert wide enums to something wider. */
1162 if (code == ENUMERAL_TYPE)
1164 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1165 TYPE_PRECISION (integer_type_node)),
1166 ((TYPE_PRECISION (type)
1167 >= TYPE_PRECISION (integer_type_node))
1168 && TREE_UNSIGNED (type)));
1170 return convert (type, exp);
1173 if (TREE_CODE (exp) == COMPONENT_REF
1174 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1175 /* If it's thinner than an int, promote it like a
1176 c_promoting_integer_type_p, otherwise leave it alone. */
1177 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1178 TYPE_PRECISION (integer_type_node)))
1179 return convert (integer_type_node, exp);
1181 if (c_promoting_integer_type_p (type))
1183 /* Preserve unsignedness if not really getting any wider. */
1184 if (TREE_UNSIGNED (type)
1185 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1186 return convert (unsigned_type_node, exp);
1188 return convert (integer_type_node, exp);
1191 if (code == VOID_TYPE)
1193 error ("void value not ignored as it ought to be");
1194 return error_mark_node;
1196 return exp;
1199 /* Look up COMPONENT in a structure or union DECL.
1201 If the component name is not found, returns NULL_TREE. Otherwise,
1202 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1203 stepping down the chain to the component, which is in the last
1204 TREE_VALUE of the list. Normally the list is of length one, but if
1205 the component is embedded within (nested) anonymous structures or
1206 unions, the list steps down the chain to the component. */
1208 static tree
1209 lookup_field (tree decl, tree component)
1211 tree type = TREE_TYPE (decl);
1212 tree field;
1214 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1215 to the field elements. Use a binary search on this array to quickly
1216 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1217 will always be set for structures which have many elements. */
1219 if (TYPE_LANG_SPECIFIC (type))
1221 int bot, top, half;
1222 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1224 field = TYPE_FIELDS (type);
1225 bot = 0;
1226 top = TYPE_LANG_SPECIFIC (type)->s->len;
1227 while (top - bot > 1)
1229 half = (top - bot + 1) >> 1;
1230 field = field_array[bot+half];
1232 if (DECL_NAME (field) == NULL_TREE)
1234 /* Step through all anon unions in linear fashion. */
1235 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1237 field = field_array[bot++];
1238 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1241 tree anon = lookup_field (field, component);
1243 if (anon)
1244 return tree_cons (NULL_TREE, field, anon);
1248 /* Entire record is only anon unions. */
1249 if (bot > top)
1250 return NULL_TREE;
1252 /* Restart the binary search, with new lower bound. */
1253 continue;
1256 if (DECL_NAME (field) == component)
1257 break;
1258 if (DECL_NAME (field) < component)
1259 bot += half;
1260 else
1261 top = bot + half;
1264 if (DECL_NAME (field_array[bot]) == component)
1265 field = field_array[bot];
1266 else if (DECL_NAME (field) != component)
1267 return NULL_TREE;
1269 else
1271 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1273 if (DECL_NAME (field) == NULL_TREE
1274 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1275 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1277 tree anon = lookup_field (field, component);
1279 if (anon)
1280 return tree_cons (NULL_TREE, field, anon);
1283 if (DECL_NAME (field) == component)
1284 break;
1287 if (field == NULL_TREE)
1288 return NULL_TREE;
1291 return tree_cons (NULL_TREE, field, NULL_TREE);
1294 /* Make an expression to refer to the COMPONENT field of
1295 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1297 tree
1298 build_component_ref (tree datum, tree component)
1300 tree type = TREE_TYPE (datum);
1301 enum tree_code code = TREE_CODE (type);
1302 tree field = NULL;
1303 tree ref;
1305 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1306 If pedantic ensure that the arguments are not lvalues; otherwise,
1307 if the component is an array, it would wrongly decay to a pointer in
1308 C89 mode.
1309 We cannot do this with a COND_EXPR, because in a conditional expression
1310 the default promotions are applied to both sides, and this would yield
1311 the wrong type of the result; for example, if the components have
1312 type "char". */
1313 switch (TREE_CODE (datum))
1315 case COMPOUND_EXPR:
1317 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1318 return build (COMPOUND_EXPR, TREE_TYPE (value),
1319 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1321 default:
1322 break;
1325 /* See if there is a field or component with name COMPONENT. */
1327 if (code == RECORD_TYPE || code == UNION_TYPE)
1329 if (!COMPLETE_TYPE_P (type))
1331 c_incomplete_type_error (NULL_TREE, type);
1332 return error_mark_node;
1335 field = lookup_field (datum, component);
1337 if (!field)
1339 error ("%s has no member named `%s'",
1340 code == RECORD_TYPE ? "structure" : "union",
1341 IDENTIFIER_POINTER (component));
1342 return error_mark_node;
1345 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1346 This might be better solved in future the way the C++ front
1347 end does it - by giving the anonymous entities each a
1348 separate name and type, and then have build_component_ref
1349 recursively call itself. We can't do that here. */
1352 tree subdatum = TREE_VALUE (field);
1354 if (TREE_TYPE (subdatum) == error_mark_node)
1355 return error_mark_node;
1357 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1358 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1359 TREE_READONLY (ref) = 1;
1360 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1361 TREE_THIS_VOLATILE (ref) = 1;
1363 if (TREE_DEPRECATED (subdatum))
1364 warn_deprecated_use (subdatum);
1366 datum = ref;
1368 field = TREE_CHAIN (field);
1370 while (field);
1372 return ref;
1374 else if (code != ERROR_MARK)
1375 error ("request for member `%s' in something not a structure or union",
1376 IDENTIFIER_POINTER (component));
1378 return error_mark_node;
1381 /* Given an expression PTR for a pointer, return an expression
1382 for the value pointed to.
1383 ERRORSTRING is the name of the operator to appear in error messages. */
1385 tree
1386 build_indirect_ref (tree ptr, const char *errorstring)
1388 tree pointer = default_conversion (ptr);
1389 tree type = TREE_TYPE (pointer);
1391 if (TREE_CODE (type) == POINTER_TYPE)
1393 if (TREE_CODE (pointer) == ADDR_EXPR
1394 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1395 == TREE_TYPE (type)))
1396 return TREE_OPERAND (pointer, 0);
1397 else
1399 tree t = TREE_TYPE (type);
1400 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1402 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1404 error ("dereferencing pointer to incomplete type");
1405 return error_mark_node;
1407 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1408 warning ("dereferencing `void *' pointer");
1410 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1411 so that we get the proper error message if the result is used
1412 to assign to. Also, &* is supposed to be a no-op.
1413 And ANSI C seems to specify that the type of the result
1414 should be the const type. */
1415 /* A de-reference of a pointer to const is not a const. It is valid
1416 to change it via some other pointer. */
1417 TREE_READONLY (ref) = TYPE_READONLY (t);
1418 TREE_SIDE_EFFECTS (ref)
1419 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1420 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1421 return ref;
1424 else if (TREE_CODE (pointer) != ERROR_MARK)
1425 error ("invalid type argument of `%s'", errorstring);
1426 return error_mark_node;
1429 /* This handles expressions of the form "a[i]", which denotes
1430 an array reference.
1432 This is logically equivalent in C to *(a+i), but we may do it differently.
1433 If A is a variable or a member, we generate a primitive ARRAY_REF.
1434 This avoids forcing the array out of registers, and can work on
1435 arrays that are not lvalues (for example, members of structures returned
1436 by functions). */
1438 tree
1439 build_array_ref (tree array, tree index)
1441 if (index == 0)
1443 error ("subscript missing in array reference");
1444 return error_mark_node;
1447 if (TREE_TYPE (array) == error_mark_node
1448 || TREE_TYPE (index) == error_mark_node)
1449 return error_mark_node;
1451 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1452 && TREE_CODE (array) != INDIRECT_REF)
1454 tree rval, type;
1456 /* Subscripting with type char is likely to lose
1457 on a machine where chars are signed.
1458 So warn on any machine, but optionally.
1459 Don't warn for unsigned char since that type is safe.
1460 Don't warn for signed char because anyone who uses that
1461 must have done so deliberately. */
1462 if (warn_char_subscripts
1463 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1464 warning ("array subscript has type `char'");
1466 /* Apply default promotions *after* noticing character types. */
1467 index = default_conversion (index);
1469 /* Require integer *after* promotion, for sake of enums. */
1470 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1472 error ("array subscript is not an integer");
1473 return error_mark_node;
1476 /* An array that is indexed by a non-constant
1477 cannot be stored in a register; we must be able to do
1478 address arithmetic on its address.
1479 Likewise an array of elements of variable size. */
1480 if (TREE_CODE (index) != INTEGER_CST
1481 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1482 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1484 if (!c_mark_addressable (array))
1485 return error_mark_node;
1487 /* An array that is indexed by a constant value which is not within
1488 the array bounds cannot be stored in a register either; because we
1489 would get a crash in store_bit_field/extract_bit_field when trying
1490 to access a non-existent part of the register. */
1491 if (TREE_CODE (index) == INTEGER_CST
1492 && TYPE_VALUES (TREE_TYPE (array))
1493 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1495 if (!c_mark_addressable (array))
1496 return error_mark_node;
1499 if (pedantic)
1501 tree foo = array;
1502 while (TREE_CODE (foo) == COMPONENT_REF)
1503 foo = TREE_OPERAND (foo, 0);
1504 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1505 pedwarn ("ISO C forbids subscripting `register' array");
1506 else if (! flag_isoc99 && ! lvalue_p (foo))
1507 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1510 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1511 rval = build (ARRAY_REF, type, array, index);
1512 /* Array ref is const/volatile if the array elements are
1513 or if the array is. */
1514 TREE_READONLY (rval)
1515 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1516 | TREE_READONLY (array));
1517 TREE_SIDE_EFFECTS (rval)
1518 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1519 | TREE_SIDE_EFFECTS (array));
1520 TREE_THIS_VOLATILE (rval)
1521 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1522 /* This was added by rms on 16 Nov 91.
1523 It fixes vol struct foo *a; a->elts[1]
1524 in an inline function.
1525 Hope it doesn't break something else. */
1526 | TREE_THIS_VOLATILE (array));
1527 return require_complete_type (fold (rval));
1531 tree ar = default_conversion (array);
1532 tree ind = default_conversion (index);
1534 /* Do the same warning check as above, but only on the part that's
1535 syntactically the index and only if it is also semantically
1536 the index. */
1537 if (warn_char_subscripts
1538 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1539 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1540 warning ("subscript has type `char'");
1542 /* Put the integer in IND to simplify error checking. */
1543 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1545 tree temp = ar;
1546 ar = ind;
1547 ind = temp;
1550 if (ar == error_mark_node)
1551 return ar;
1553 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1554 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1556 error ("subscripted value is neither array nor pointer");
1557 return error_mark_node;
1559 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1561 error ("array subscript is not an integer");
1562 return error_mark_node;
1565 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1566 "array indexing");
1570 /* Build an external reference to identifier ID. FUN indicates
1571 whether this will be used for a function call. */
1572 tree
1573 build_external_ref (tree id, int fun)
1575 tree ref;
1576 tree decl = lookup_name (id);
1577 tree objc_ivar = lookup_objc_ivar (id);
1579 if (decl && decl != error_mark_node)
1581 /* Properly declared variable or function reference. */
1582 if (!objc_ivar)
1583 ref = decl;
1584 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1586 warning ("local declaration of `%s' hides instance variable",
1587 IDENTIFIER_POINTER (id));
1588 ref = decl;
1590 else
1591 ref = objc_ivar;
1593 else if (objc_ivar)
1594 ref = objc_ivar;
1595 else if (fun)
1596 /* Implicit function declaration. */
1597 ref = implicitly_declare (id);
1598 else if (decl == error_mark_node)
1599 /* Don't complain about something that's already been
1600 complained about. */
1601 return error_mark_node;
1602 else
1604 undeclared_variable (id);
1605 return error_mark_node;
1608 if (TREE_TYPE (ref) == error_mark_node)
1609 return error_mark_node;
1611 if (TREE_DEPRECATED (ref))
1612 warn_deprecated_use (ref);
1614 if (!skip_evaluation)
1615 assemble_external (ref);
1616 TREE_USED (ref) = 1;
1618 if (TREE_CODE (ref) == CONST_DECL)
1620 ref = DECL_INITIAL (ref);
1621 TREE_CONSTANT (ref) = 1;
1623 else if (current_function_decl != 0
1624 && !DECL_FILE_SCOPE_P (current_function_decl)
1625 && (TREE_CODE (ref) == VAR_DECL
1626 || TREE_CODE (ref) == PARM_DECL
1627 || TREE_CODE (ref) == FUNCTION_DECL))
1629 tree context = decl_function_context (ref);
1631 if (context != 0 && context != current_function_decl)
1632 DECL_NONLOCAL (ref) = 1;
1635 return ref;
1638 /* Build a function call to function FUNCTION with parameters PARAMS.
1639 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1640 TREE_VALUE of each node is a parameter-expression.
1641 FUNCTION's data type may be a function type or a pointer-to-function. */
1643 tree
1644 build_function_call (tree function, tree params)
1646 tree fntype, fundecl = 0;
1647 tree coerced_params;
1648 tree name = NULL_TREE, result;
1649 tree tem;
1651 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1652 STRIP_TYPE_NOPS (function);
1654 /* Convert anything with function type to a pointer-to-function. */
1655 if (TREE_CODE (function) == FUNCTION_DECL)
1657 name = DECL_NAME (function);
1659 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1660 (because calling an inline function does not mean the function
1661 needs to be separately compiled). */
1662 fntype = build_type_variant (TREE_TYPE (function),
1663 TREE_READONLY (function),
1664 TREE_THIS_VOLATILE (function));
1665 fundecl = function;
1666 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1668 else
1669 function = default_conversion (function);
1671 fntype = TREE_TYPE (function);
1673 if (TREE_CODE (fntype) == ERROR_MARK)
1674 return error_mark_node;
1676 if (!(TREE_CODE (fntype) == POINTER_TYPE
1677 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1679 error ("called object is not a function");
1680 return error_mark_node;
1683 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1684 current_function_returns_abnormally = 1;
1686 /* fntype now gets the type of function pointed to. */
1687 fntype = TREE_TYPE (fntype);
1689 /* Check that the function is called through a compatible prototype.
1690 If it is not, replace the call by a trap, wrapped up in a compound
1691 expression if necessary. This has the nice side-effect to prevent
1692 the tree-inliner from generating invalid assignment trees which may
1693 blow up in the RTL expander later.
1695 ??? This doesn't work for Objective-C because objc_comptypes
1696 refuses to compare function prototypes, yet the compiler appears
1697 to build calls that are flagged as invalid by C's comptypes. */
1698 if (! c_dialect_objc ()
1699 && TREE_CODE (function) == NOP_EXPR
1700 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1701 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1702 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1704 tree return_type = TREE_TYPE (fntype);
1705 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1706 NULL_TREE);
1708 /* This situation leads to run-time undefined behavior. We can't,
1709 therefore, simply error unless we can prove that all possible
1710 executions of the program must execute the code. */
1711 warning ("function called through a non-compatible type");
1713 if (VOID_TYPE_P (return_type))
1714 return trap;
1715 else
1717 tree rhs;
1719 if (AGGREGATE_TYPE_P (return_type))
1720 rhs = build_compound_literal (return_type,
1721 build_constructor (return_type,
1722 NULL_TREE));
1723 else
1724 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1726 return build (COMPOUND_EXPR, return_type, trap, rhs);
1730 /* Convert the parameters to the types declared in the
1731 function prototype, or apply default promotions. */
1733 coerced_params
1734 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1736 /* Check that the arguments to the function are valid. */
1738 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1740 /* Recognize certain built-in functions so we can make tree-codes
1741 other than CALL_EXPR. We do this when it enables fold-const.c
1742 to do something useful. */
1744 if (TREE_CODE (function) == ADDR_EXPR
1745 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1746 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1748 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1749 params, coerced_params);
1750 if (result)
1751 return result;
1754 result = build (CALL_EXPR, TREE_TYPE (fntype),
1755 function, coerced_params, NULL_TREE);
1756 TREE_SIDE_EFFECTS (result) = 1;
1757 result = fold (result);
1759 if (VOID_TYPE_P (TREE_TYPE (result)))
1760 return result;
1761 return require_complete_type (result);
1764 /* Convert the argument expressions in the list VALUES
1765 to the types in the list TYPELIST. The result is a list of converted
1766 argument expressions.
1768 If TYPELIST is exhausted, or when an element has NULL as its type,
1769 perform the default conversions.
1771 PARMLIST is the chain of parm decls for the function being called.
1772 It may be 0, if that info is not available.
1773 It is used only for generating error messages.
1775 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1777 This is also where warnings about wrong number of args are generated.
1779 Both VALUES and the returned value are chains of TREE_LIST nodes
1780 with the elements of the list in the TREE_VALUE slots of those nodes. */
1782 static tree
1783 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1785 tree typetail, valtail;
1786 tree result = NULL;
1787 int parmnum;
1789 /* Scan the given expressions and types, producing individual
1790 converted arguments and pushing them on RESULT in reverse order. */
1792 for (valtail = values, typetail = typelist, parmnum = 0;
1793 valtail;
1794 valtail = TREE_CHAIN (valtail), parmnum++)
1796 tree type = typetail ? TREE_VALUE (typetail) : 0;
1797 tree val = TREE_VALUE (valtail);
1799 if (type == void_type_node)
1801 if (name)
1802 error ("too many arguments to function `%s'",
1803 IDENTIFIER_POINTER (name));
1804 else
1805 error ("too many arguments to function");
1806 break;
1809 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1810 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1811 to convert automatically to a pointer. */
1812 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1813 val = TREE_OPERAND (val, 0);
1815 val = default_function_array_conversion (val);
1817 val = require_complete_type (val);
1819 if (type != 0)
1821 /* Formal parm type is specified by a function prototype. */
1822 tree parmval;
1824 if (!COMPLETE_TYPE_P (type))
1826 error ("type of formal parameter %d is incomplete", parmnum + 1);
1827 parmval = val;
1829 else
1831 /* Optionally warn about conversions that
1832 differ from the default conversions. */
1833 if (warn_conversion || warn_traditional)
1835 int formal_prec = TYPE_PRECISION (type);
1837 if (INTEGRAL_TYPE_P (type)
1838 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1839 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1840 if (INTEGRAL_TYPE_P (type)
1841 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1842 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1843 else if (TREE_CODE (type) == COMPLEX_TYPE
1844 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1845 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1846 else if (TREE_CODE (type) == REAL_TYPE
1847 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1848 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1849 else if (TREE_CODE (type) == COMPLEX_TYPE
1850 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1851 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1852 else if (TREE_CODE (type) == REAL_TYPE
1853 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1854 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1855 /* ??? At some point, messages should be written about
1856 conversions between complex types, but that's too messy
1857 to do now. */
1858 else if (TREE_CODE (type) == REAL_TYPE
1859 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1861 /* Warn if any argument is passed as `float',
1862 since without a prototype it would be `double'. */
1863 if (formal_prec == TYPE_PRECISION (float_type_node))
1864 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1866 /* Detect integer changing in width or signedness.
1867 These warnings are only activated with
1868 -Wconversion, not with -Wtraditional. */
1869 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1870 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1872 tree would_have_been = default_conversion (val);
1873 tree type1 = TREE_TYPE (would_have_been);
1875 if (TREE_CODE (type) == ENUMERAL_TYPE
1876 && (TYPE_MAIN_VARIANT (type)
1877 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1878 /* No warning if function asks for enum
1879 and the actual arg is that enum type. */
1881 else if (formal_prec != TYPE_PRECISION (type1))
1882 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1883 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1885 /* Don't complain if the formal parameter type
1886 is an enum, because we can't tell now whether
1887 the value was an enum--even the same enum. */
1888 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1890 else if (TREE_CODE (val) == INTEGER_CST
1891 && int_fits_type_p (val, type))
1892 /* Change in signedness doesn't matter
1893 if a constant value is unaffected. */
1895 /* Likewise for a constant in a NOP_EXPR. */
1896 else if (TREE_CODE (val) == NOP_EXPR
1897 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1898 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1900 /* If the value is extended from a narrower
1901 unsigned type, it doesn't matter whether we
1902 pass it as signed or unsigned; the value
1903 certainly is the same either way. */
1904 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1905 && TREE_UNSIGNED (TREE_TYPE (val)))
1907 else if (TREE_UNSIGNED (type))
1908 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1909 else
1910 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1914 parmval = convert_for_assignment (type, val,
1915 (char *) 0, /* arg passing */
1916 fundecl, name, parmnum + 1);
1918 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1919 && INTEGRAL_TYPE_P (type)
1920 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1921 parmval = default_conversion (parmval);
1923 result = tree_cons (NULL_TREE, parmval, result);
1925 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1926 && (TYPE_PRECISION (TREE_TYPE (val))
1927 < TYPE_PRECISION (double_type_node)))
1928 /* Convert `float' to `double'. */
1929 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1930 else
1931 /* Convert `short' and `char' to full-size `int'. */
1932 result = tree_cons (NULL_TREE, default_conversion (val), result);
1934 if (typetail)
1935 typetail = TREE_CHAIN (typetail);
1938 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1940 if (name)
1941 error ("too few arguments to function `%s'",
1942 IDENTIFIER_POINTER (name));
1943 else
1944 error ("too few arguments to function");
1947 return nreverse (result);
1950 /* This is the entry point used by the parser
1951 for binary operators in the input.
1952 In addition to constructing the expression,
1953 we check for operands that were written with other binary operators
1954 in a way that is likely to confuse the user. */
1956 tree
1957 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1959 tree result = build_binary_op (code, arg1, arg2, 1);
1961 char class;
1962 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1963 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1964 enum tree_code code1 = ERROR_MARK;
1965 enum tree_code code2 = ERROR_MARK;
1967 if (TREE_CODE (result) == ERROR_MARK)
1968 return error_mark_node;
1970 if (IS_EXPR_CODE_CLASS (class1))
1971 code1 = C_EXP_ORIGINAL_CODE (arg1);
1972 if (IS_EXPR_CODE_CLASS (class2))
1973 code2 = C_EXP_ORIGINAL_CODE (arg2);
1975 /* Check for cases such as x+y<<z which users are likely
1976 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1977 is cleared to prevent these warnings. */
1978 if (warn_parentheses)
1980 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1982 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1983 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1984 warning ("suggest parentheses around + or - inside shift");
1987 if (code == TRUTH_ORIF_EXPR)
1989 if (code1 == TRUTH_ANDIF_EXPR
1990 || code2 == TRUTH_ANDIF_EXPR)
1991 warning ("suggest parentheses around && within ||");
1994 if (code == BIT_IOR_EXPR)
1996 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1997 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1998 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1999 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2000 warning ("suggest parentheses around arithmetic in operand of |");
2001 /* Check cases like x|y==z */
2002 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2003 warning ("suggest parentheses around comparison in operand of |");
2006 if (code == BIT_XOR_EXPR)
2008 if (code1 == BIT_AND_EXPR
2009 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2010 || code2 == BIT_AND_EXPR
2011 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2012 warning ("suggest parentheses around arithmetic in operand of ^");
2013 /* Check cases like x^y==z */
2014 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2015 warning ("suggest parentheses around comparison in operand of ^");
2018 if (code == BIT_AND_EXPR)
2020 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2021 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2022 warning ("suggest parentheses around + or - in operand of &");
2023 /* Check cases like x&y==z */
2024 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2025 warning ("suggest parentheses around comparison in operand of &");
2029 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2030 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2031 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2032 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2034 unsigned_conversion_warning (result, arg1);
2035 unsigned_conversion_warning (result, arg2);
2036 overflow_warning (result);
2038 class = TREE_CODE_CLASS (TREE_CODE (result));
2040 /* Record the code that was specified in the source,
2041 for the sake of warnings about confusing nesting. */
2042 if (IS_EXPR_CODE_CLASS (class))
2043 C_SET_EXP_ORIGINAL_CODE (result, code);
2044 else
2046 int flag = TREE_CONSTANT (result);
2047 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2048 so that convert_for_assignment wouldn't strip it.
2049 That way, we got warnings for things like p = (1 - 1).
2050 But it turns out we should not get those warnings. */
2051 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2052 C_SET_EXP_ORIGINAL_CODE (result, code);
2053 TREE_CONSTANT (result) = flag;
2056 return result;
2060 /* Return true if `t' is known to be non-negative. */
2063 c_tree_expr_nonnegative_p (tree t)
2065 if (TREE_CODE (t) == STMT_EXPR)
2067 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2069 /* Find the last statement in the chain, ignoring the final
2070 * scope statement */
2071 while (TREE_CHAIN (t) != NULL_TREE
2072 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2073 t = TREE_CHAIN (t);
2074 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2076 return tree_expr_nonnegative_p (t);
2079 /* Return a tree for the difference of pointers OP0 and OP1.
2080 The resulting tree has type int. */
2082 static tree
2083 pointer_diff (tree op0, tree op1)
2085 tree result, folded;
2086 tree restype = ptrdiff_type_node;
2088 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2089 tree con0, con1, lit0, lit1;
2090 tree orig_op1 = op1;
2092 if (pedantic || warn_pointer_arith)
2094 if (TREE_CODE (target_type) == VOID_TYPE)
2095 pedwarn ("pointer of type `void *' used in subtraction");
2096 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2097 pedwarn ("pointer to a function used in subtraction");
2100 /* If the conversion to ptrdiff_type does anything like widening or
2101 converting a partial to an integral mode, we get a convert_expression
2102 that is in the way to do any simplifications.
2103 (fold-const.c doesn't know that the extra bits won't be needed.
2104 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2105 different mode in place.)
2106 So first try to find a common term here 'by hand'; we want to cover
2107 at least the cases that occur in legal static initializers. */
2108 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2109 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2111 if (TREE_CODE (con0) == PLUS_EXPR)
2113 lit0 = TREE_OPERAND (con0, 1);
2114 con0 = TREE_OPERAND (con0, 0);
2116 else
2117 lit0 = integer_zero_node;
2119 if (TREE_CODE (con1) == PLUS_EXPR)
2121 lit1 = TREE_OPERAND (con1, 1);
2122 con1 = TREE_OPERAND (con1, 0);
2124 else
2125 lit1 = integer_zero_node;
2127 if (operand_equal_p (con0, con1, 0))
2129 op0 = lit0;
2130 op1 = lit1;
2134 /* First do the subtraction as integers;
2135 then drop through to build the divide operator.
2136 Do not do default conversions on the minus operator
2137 in case restype is a short type. */
2139 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2140 convert (restype, op1), 0);
2141 /* This generates an error if op1 is pointer to incomplete type. */
2142 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2143 error ("arithmetic on pointer to an incomplete type");
2145 /* This generates an error if op0 is pointer to incomplete type. */
2146 op1 = c_size_in_bytes (target_type);
2148 /* Divide by the size, in easiest possible way. */
2150 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2152 folded = fold (result);
2153 if (folded == result)
2154 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2155 return folded;
2158 /* Construct and perhaps optimize a tree representation
2159 for a unary operation. CODE, a tree_code, specifies the operation
2160 and XARG is the operand.
2161 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2162 the default promotions (such as from short to int).
2163 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2164 allows non-lvalues; this is only used to handle conversion of non-lvalue
2165 arrays to pointers in C99. */
2167 tree
2168 build_unary_op (enum tree_code code, tree xarg, int flag)
2170 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2171 tree arg = xarg;
2172 tree argtype = 0;
2173 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2174 tree val;
2175 int noconvert = flag;
2177 if (typecode == ERROR_MARK)
2178 return error_mark_node;
2179 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2180 typecode = INTEGER_TYPE;
2182 switch (code)
2184 case CONVERT_EXPR:
2185 /* This is used for unary plus, because a CONVERT_EXPR
2186 is enough to prevent anybody from looking inside for
2187 associativity, but won't generate any code. */
2188 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2189 || typecode == COMPLEX_TYPE))
2191 error ("wrong type argument to unary plus");
2192 return error_mark_node;
2194 else if (!noconvert)
2195 arg = default_conversion (arg);
2196 arg = non_lvalue (arg);
2197 break;
2199 case NEGATE_EXPR:
2200 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2201 || typecode == COMPLEX_TYPE
2202 || typecode == VECTOR_TYPE))
2204 error ("wrong type argument to unary minus");
2205 return error_mark_node;
2207 else if (!noconvert)
2208 arg = default_conversion (arg);
2209 break;
2211 case BIT_NOT_EXPR:
2212 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2214 if (!noconvert)
2215 arg = default_conversion (arg);
2217 else if (typecode == COMPLEX_TYPE)
2219 code = CONJ_EXPR;
2220 if (pedantic)
2221 pedwarn ("ISO C does not support `~' for complex conjugation");
2222 if (!noconvert)
2223 arg = default_conversion (arg);
2225 else
2227 error ("wrong type argument to bit-complement");
2228 return error_mark_node;
2230 break;
2232 case ABS_EXPR:
2233 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2235 error ("wrong type argument to abs");
2236 return error_mark_node;
2238 else if (!noconvert)
2239 arg = default_conversion (arg);
2240 break;
2242 case CONJ_EXPR:
2243 /* Conjugating a real value is a no-op, but allow it anyway. */
2244 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2245 || typecode == COMPLEX_TYPE))
2247 error ("wrong type argument to conjugation");
2248 return error_mark_node;
2250 else if (!noconvert)
2251 arg = default_conversion (arg);
2252 break;
2254 case TRUTH_NOT_EXPR:
2255 if (typecode != INTEGER_TYPE
2256 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2257 && typecode != COMPLEX_TYPE
2258 /* These will convert to a pointer. */
2259 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2261 error ("wrong type argument to unary exclamation mark");
2262 return error_mark_node;
2264 arg = c_common_truthvalue_conversion (arg);
2265 return invert_truthvalue (arg);
2267 case NOP_EXPR:
2268 break;
2270 case REALPART_EXPR:
2271 if (TREE_CODE (arg) == COMPLEX_CST)
2272 return TREE_REALPART (arg);
2273 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2274 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2275 else
2276 return arg;
2278 case IMAGPART_EXPR:
2279 if (TREE_CODE (arg) == COMPLEX_CST)
2280 return TREE_IMAGPART (arg);
2281 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2282 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2283 else
2284 return convert (TREE_TYPE (arg), integer_zero_node);
2286 case PREINCREMENT_EXPR:
2287 case POSTINCREMENT_EXPR:
2288 case PREDECREMENT_EXPR:
2289 case POSTDECREMENT_EXPR:
2290 /* Handle complex lvalues (when permitted)
2291 by reduction to simpler cases. */
2293 val = unary_complex_lvalue (code, arg, 0);
2294 if (val != 0)
2295 return val;
2297 /* Increment or decrement the real part of the value,
2298 and don't change the imaginary part. */
2299 if (typecode == COMPLEX_TYPE)
2301 tree real, imag;
2303 if (pedantic)
2304 pedwarn ("ISO C does not support `++' and `--' on complex types");
2306 arg = stabilize_reference (arg);
2307 real = build_unary_op (REALPART_EXPR, arg, 1);
2308 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2309 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2310 build_unary_op (code, real, 1), imag);
2313 /* Report invalid types. */
2315 if (typecode != POINTER_TYPE
2316 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2318 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2319 error ("wrong type argument to increment");
2320 else
2321 error ("wrong type argument to decrement");
2323 return error_mark_node;
2327 tree inc;
2328 tree result_type = TREE_TYPE (arg);
2330 arg = get_unwidened (arg, 0);
2331 argtype = TREE_TYPE (arg);
2333 /* Compute the increment. */
2335 if (typecode == POINTER_TYPE)
2337 /* If pointer target is an undefined struct,
2338 we just cannot know how to do the arithmetic. */
2339 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2341 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2342 error ("increment of pointer to unknown structure");
2343 else
2344 error ("decrement of pointer to unknown structure");
2346 else if ((pedantic || warn_pointer_arith)
2347 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2348 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2350 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2351 pedwarn ("wrong type argument to increment");
2352 else
2353 pedwarn ("wrong type argument to decrement");
2356 inc = c_size_in_bytes (TREE_TYPE (result_type));
2358 else
2359 inc = integer_one_node;
2361 inc = convert (argtype, inc);
2363 /* Handle incrementing a cast-expression. */
2365 while (1)
2366 switch (TREE_CODE (arg))
2368 case NOP_EXPR:
2369 case CONVERT_EXPR:
2370 case FLOAT_EXPR:
2371 case FIX_TRUNC_EXPR:
2372 case FIX_FLOOR_EXPR:
2373 case FIX_ROUND_EXPR:
2374 case FIX_CEIL_EXPR:
2375 pedantic_lvalue_warning (CONVERT_EXPR);
2376 /* If the real type has the same machine representation
2377 as the type it is cast to, we can make better output
2378 by adding directly to the inside of the cast. */
2379 if ((TREE_CODE (TREE_TYPE (arg))
2380 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2381 && (TYPE_MODE (TREE_TYPE (arg))
2382 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2383 arg = TREE_OPERAND (arg, 0);
2384 else
2386 tree incremented, modify, value;
2387 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2388 value = boolean_increment (code, arg);
2389 else
2391 arg = stabilize_reference (arg);
2392 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2393 value = arg;
2394 else
2395 value = save_expr (arg);
2396 incremented = build (((code == PREINCREMENT_EXPR
2397 || code == POSTINCREMENT_EXPR)
2398 ? PLUS_EXPR : MINUS_EXPR),
2399 argtype, value, inc);
2400 TREE_SIDE_EFFECTS (incremented) = 1;
2401 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2402 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2404 TREE_USED (value) = 1;
2405 return value;
2407 break;
2409 default:
2410 goto give_up;
2412 give_up:
2414 /* Complain about anything else that is not a true lvalue. */
2415 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2416 || code == POSTINCREMENT_EXPR)
2417 ? "invalid lvalue in increment"
2418 : "invalid lvalue in decrement")))
2419 return error_mark_node;
2421 /* Report a read-only lvalue. */
2422 if (TREE_READONLY (arg))
2423 readonly_warning (arg,
2424 ((code == PREINCREMENT_EXPR
2425 || code == POSTINCREMENT_EXPR)
2426 ? "increment" : "decrement"));
2428 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2429 val = boolean_increment (code, arg);
2430 else
2431 val = build (code, TREE_TYPE (arg), arg, inc);
2432 TREE_SIDE_EFFECTS (val) = 1;
2433 val = convert (result_type, val);
2434 if (TREE_CODE (val) != code)
2435 TREE_NO_UNUSED_WARNING (val) = 1;
2436 return val;
2439 case ADDR_EXPR:
2440 /* Note that this operation never does default_conversion. */
2442 /* Let &* cancel out to simplify resulting code. */
2443 if (TREE_CODE (arg) == INDIRECT_REF)
2445 /* Don't let this be an lvalue. */
2446 if (lvalue_p (TREE_OPERAND (arg, 0)))
2447 return non_lvalue (TREE_OPERAND (arg, 0));
2448 return TREE_OPERAND (arg, 0);
2451 /* For &x[y], return x+y */
2452 if (TREE_CODE (arg) == ARRAY_REF)
2454 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2455 return error_mark_node;
2456 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2457 TREE_OPERAND (arg, 1), 1);
2460 /* Handle complex lvalues (when permitted)
2461 by reduction to simpler cases. */
2462 val = unary_complex_lvalue (code, arg, flag);
2463 if (val != 0)
2464 return val;
2466 /* Anything not already handled and not a true memory reference
2467 or a non-lvalue array is an error. */
2468 else if (typecode != FUNCTION_TYPE && !flag
2469 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2470 return error_mark_node;
2472 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2473 argtype = TREE_TYPE (arg);
2475 /* If the lvalue is const or volatile, merge that into the type
2476 to which the address will point. Note that you can't get a
2477 restricted pointer by taking the address of something, so we
2478 only have to deal with `const' and `volatile' here. */
2479 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2480 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2481 argtype = c_build_type_variant (argtype,
2482 TREE_READONLY (arg),
2483 TREE_THIS_VOLATILE (arg));
2485 argtype = build_pointer_type (argtype);
2487 if (!c_mark_addressable (arg))
2488 return error_mark_node;
2491 tree addr;
2493 if (TREE_CODE (arg) == COMPONENT_REF)
2495 tree field = TREE_OPERAND (arg, 1);
2497 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2499 if (DECL_C_BIT_FIELD (field))
2501 error ("attempt to take address of bit-field structure member `%s'",
2502 IDENTIFIER_POINTER (DECL_NAME (field)));
2503 return error_mark_node;
2506 addr = fold (build (PLUS_EXPR, argtype,
2507 convert (argtype, addr),
2508 convert (argtype, byte_position (field))));
2510 else
2511 addr = build1 (code, argtype, arg);
2513 /* Address of a static or external variable or
2514 file-scope function counts as a constant. */
2515 if (staticp (arg)
2516 && ! (TREE_CODE (arg) == FUNCTION_DECL
2517 && !DECL_FILE_SCOPE_P (arg)))
2518 TREE_CONSTANT (addr) = 1;
2519 return addr;
2522 default:
2523 break;
2526 if (argtype == 0)
2527 argtype = TREE_TYPE (arg);
2528 return fold (build1 (code, argtype, arg));
2531 /* Return nonzero if REF is an lvalue valid for this language.
2532 Lvalues can be assigned, unless their type has TYPE_READONLY.
2533 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2536 lvalue_p (tree ref)
2538 enum tree_code code = TREE_CODE (ref);
2540 switch (code)
2542 case REALPART_EXPR:
2543 case IMAGPART_EXPR:
2544 case COMPONENT_REF:
2545 return lvalue_p (TREE_OPERAND (ref, 0));
2547 case COMPOUND_LITERAL_EXPR:
2548 case STRING_CST:
2549 return 1;
2551 case INDIRECT_REF:
2552 case ARRAY_REF:
2553 case VAR_DECL:
2554 case PARM_DECL:
2555 case RESULT_DECL:
2556 case ERROR_MARK:
2557 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2558 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2560 case BIND_EXPR:
2561 case RTL_EXPR:
2562 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2564 default:
2565 return 0;
2569 /* Return nonzero if REF is an lvalue valid for this language;
2570 otherwise, print an error message and return zero. */
2573 lvalue_or_else (tree ref, const char *msgid)
2575 int win = lvalue_p (ref);
2577 if (! win)
2578 error ("%s", msgid);
2580 return win;
2583 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2584 for certain kinds of expressions which are not really lvalues
2585 but which we can accept as lvalues. If FLAG is nonzero, then
2586 non-lvalues are OK since we may be converting a non-lvalue array to
2587 a pointer in C99.
2589 If ARG is not a kind of expression we can handle, return zero. */
2591 static tree
2592 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2594 /* Handle (a, b) used as an "lvalue". */
2595 if (TREE_CODE (arg) == COMPOUND_EXPR)
2597 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2599 /* If this returns a function type, it isn't really being used as
2600 an lvalue, so don't issue a warning about it. */
2601 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2602 pedantic_lvalue_warning (COMPOUND_EXPR);
2604 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2605 TREE_OPERAND (arg, 0), real_result);
2608 /* Handle (a ? b : c) used as an "lvalue". */
2609 if (TREE_CODE (arg) == COND_EXPR)
2611 if (!flag)
2612 pedantic_lvalue_warning (COND_EXPR);
2613 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2614 pedantic_lvalue_warning (COMPOUND_EXPR);
2616 return (build_conditional_expr
2617 (TREE_OPERAND (arg, 0),
2618 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2619 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2622 return 0;
2625 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2626 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2628 static void
2629 pedantic_lvalue_warning (enum tree_code code)
2631 switch (code)
2633 case COND_EXPR:
2634 pedwarn ("use of conditional expressions as lvalues is deprecated");
2635 break;
2636 case COMPOUND_EXPR:
2637 pedwarn ("use of compound expressions as lvalues is deprecated");
2638 break;
2639 default:
2640 pedwarn ("use of cast expressions as lvalues is deprecated");
2641 break;
2645 /* Warn about storing in something that is `const'. */
2647 void
2648 readonly_warning (tree arg, const char *msgid)
2650 if (TREE_CODE (arg) == COMPONENT_REF)
2652 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2653 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2654 else
2655 pedwarn ("%s of read-only member `%s'", _(msgid),
2656 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2658 else if (TREE_CODE (arg) == VAR_DECL)
2659 pedwarn ("%s of read-only variable `%s'", _(msgid),
2660 IDENTIFIER_POINTER (DECL_NAME (arg)));
2661 else
2662 pedwarn ("%s of read-only location", _(msgid));
2665 /* Mark EXP saying that we need to be able to take the
2666 address of it; it should not be allocated in a register.
2667 Returns true if successful. */
2669 bool
2670 c_mark_addressable (tree exp)
2672 tree x = exp;
2674 while (1)
2675 switch (TREE_CODE (x))
2677 case COMPONENT_REF:
2678 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2680 error ("cannot take address of bit-field `%s'",
2681 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2682 return false;
2685 /* ... fall through ... */
2687 case ADDR_EXPR:
2688 case ARRAY_REF:
2689 case REALPART_EXPR:
2690 case IMAGPART_EXPR:
2691 x = TREE_OPERAND (x, 0);
2692 break;
2694 case COMPOUND_LITERAL_EXPR:
2695 case CONSTRUCTOR:
2696 TREE_ADDRESSABLE (x) = 1;
2697 return true;
2699 case VAR_DECL:
2700 case CONST_DECL:
2701 case PARM_DECL:
2702 case RESULT_DECL:
2703 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2704 && DECL_NONLOCAL (x))
2706 if (TREE_PUBLIC (x))
2708 error ("global register variable `%s' used in nested function",
2709 IDENTIFIER_POINTER (DECL_NAME (x)));
2710 return false;
2712 pedwarn ("register variable `%s' used in nested function",
2713 IDENTIFIER_POINTER (DECL_NAME (x)));
2715 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2717 if (TREE_PUBLIC (x))
2719 error ("address of global register variable `%s' requested",
2720 IDENTIFIER_POINTER (DECL_NAME (x)));
2721 return false;
2724 /* If we are making this addressable due to its having
2725 volatile components, give a different error message. Also
2726 handle the case of an unnamed parameter by not trying
2727 to give the name. */
2729 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2731 error ("cannot put object with volatile field into register");
2732 return false;
2735 pedwarn ("address of register variable `%s' requested",
2736 IDENTIFIER_POINTER (DECL_NAME (x)));
2738 put_var_into_stack (x, /*rescan=*/true);
2740 /* drops in */
2741 case FUNCTION_DECL:
2742 TREE_ADDRESSABLE (x) = 1;
2743 /* drops out */
2744 default:
2745 return true;
2749 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2751 tree
2752 build_conditional_expr (tree ifexp, tree op1, tree op2)
2754 tree type1;
2755 tree type2;
2756 enum tree_code code1;
2757 enum tree_code code2;
2758 tree result_type = NULL;
2759 tree orig_op1 = op1, orig_op2 = op2;
2761 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2763 /* Promote both alternatives. */
2765 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2766 op1 = default_conversion (op1);
2767 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2768 op2 = default_conversion (op2);
2770 if (TREE_CODE (ifexp) == ERROR_MARK
2771 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2772 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2773 return error_mark_node;
2775 type1 = TREE_TYPE (op1);
2776 code1 = TREE_CODE (type1);
2777 type2 = TREE_TYPE (op2);
2778 code2 = TREE_CODE (type2);
2780 /* Quickly detect the usual case where op1 and op2 have the same type
2781 after promotion. */
2782 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2784 if (type1 == type2)
2785 result_type = type1;
2786 else
2787 result_type = TYPE_MAIN_VARIANT (type1);
2789 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2790 || code1 == COMPLEX_TYPE)
2791 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2792 || code2 == COMPLEX_TYPE))
2794 result_type = common_type (type1, type2);
2796 /* If -Wsign-compare, warn here if type1 and type2 have
2797 different signedness. We'll promote the signed to unsigned
2798 and later code won't know it used to be different.
2799 Do this check on the original types, so that explicit casts
2800 will be considered, but default promotions won't. */
2801 if (warn_sign_compare && !skip_evaluation)
2803 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2804 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2806 if (unsigned_op1 ^ unsigned_op2)
2808 /* Do not warn if the result type is signed, since the
2809 signed type will only be chosen if it can represent
2810 all the values of the unsigned type. */
2811 if (! TREE_UNSIGNED (result_type))
2812 /* OK */;
2813 /* Do not warn if the signed quantity is an unsuffixed
2814 integer literal (or some static constant expression
2815 involving such literals) and it is non-negative. */
2816 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2817 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2818 /* OK */;
2819 else
2820 warning ("signed and unsigned type in conditional expression");
2824 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2826 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2827 pedwarn ("ISO C forbids conditional expr with only one void side");
2828 result_type = void_type_node;
2830 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2832 if (comp_target_types (type1, type2, 1))
2833 result_type = common_type (type1, type2);
2834 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2835 && TREE_CODE (orig_op1) != NOP_EXPR)
2836 result_type = qualify_type (type2, type1);
2837 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2838 && TREE_CODE (orig_op2) != NOP_EXPR)
2839 result_type = qualify_type (type1, type2);
2840 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2842 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2843 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2844 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2845 TREE_TYPE (type2)));
2847 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2849 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2850 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2851 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2852 TREE_TYPE (type1)));
2854 else
2856 pedwarn ("pointer type mismatch in conditional expression");
2857 result_type = build_pointer_type (void_type_node);
2860 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2862 if (! integer_zerop (op2))
2863 pedwarn ("pointer/integer type mismatch in conditional expression");
2864 else
2866 op2 = null_pointer_node;
2868 result_type = type1;
2870 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2872 if (!integer_zerop (op1))
2873 pedwarn ("pointer/integer type mismatch in conditional expression");
2874 else
2876 op1 = null_pointer_node;
2878 result_type = type2;
2881 if (!result_type)
2883 if (flag_cond_mismatch)
2884 result_type = void_type_node;
2885 else
2887 error ("type mismatch in conditional expression");
2888 return error_mark_node;
2892 /* Merge const and volatile flags of the incoming types. */
2893 result_type
2894 = build_type_variant (result_type,
2895 TREE_READONLY (op1) || TREE_READONLY (op2),
2896 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2898 if (result_type != TREE_TYPE (op1))
2899 op1 = convert_and_check (result_type, op1);
2900 if (result_type != TREE_TYPE (op2))
2901 op2 = convert_and_check (result_type, op2);
2903 if (TREE_CODE (ifexp) == INTEGER_CST)
2904 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2906 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2909 /* Given a list of expressions, return a compound expression
2910 that performs them all and returns the value of the last of them. */
2912 tree
2913 build_compound_expr (tree list)
2915 return internal_build_compound_expr (list, TRUE);
2918 static tree
2919 internal_build_compound_expr (tree list, int first_p)
2921 tree rest;
2923 if (TREE_CHAIN (list) == 0)
2925 /* Convert arrays and functions to pointers when there
2926 really is a comma operator. */
2927 if (!first_p)
2928 TREE_VALUE (list)
2929 = default_function_array_conversion (TREE_VALUE (list));
2931 /* Don't let (0, 0) be null pointer constant. */
2932 if (!first_p && integer_zerop (TREE_VALUE (list)))
2933 return non_lvalue (TREE_VALUE (list));
2934 return TREE_VALUE (list);
2937 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2939 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2941 /* The left-hand operand of a comma expression is like an expression
2942 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2943 any side-effects, unless it was explicitly cast to (void). */
2944 if (warn_unused_value
2945 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2946 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2947 warning ("left-hand operand of comma expression has no effect");
2950 /* With -Wunused, we should also warn if the left-hand operand does have
2951 side-effects, but computes a value which is not used. For example, in
2952 `foo() + bar(), baz()' the result of the `+' operator is not used,
2953 so we should issue a warning. */
2954 else if (warn_unused_value)
2955 warn_if_unused_value (TREE_VALUE (list));
2957 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2960 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2962 tree
2963 build_c_cast (tree type, tree expr)
2965 tree value = expr;
2967 if (type == error_mark_node || expr == error_mark_node)
2968 return error_mark_node;
2970 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2971 only in <protocol> qualifications. But when constructing cast expressions,
2972 the protocols do matter and must be kept around. */
2973 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2974 type = TYPE_MAIN_VARIANT (type);
2976 if (TREE_CODE (type) == ARRAY_TYPE)
2978 error ("cast specifies array type");
2979 return error_mark_node;
2982 if (TREE_CODE (type) == FUNCTION_TYPE)
2984 error ("cast specifies function type");
2985 return error_mark_node;
2988 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2990 if (pedantic)
2992 if (TREE_CODE (type) == RECORD_TYPE
2993 || TREE_CODE (type) == UNION_TYPE)
2994 pedwarn ("ISO C forbids casting nonscalar to the same type");
2997 else if (TREE_CODE (type) == UNION_TYPE)
2999 tree field;
3000 value = default_function_array_conversion (value);
3002 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3003 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3004 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3005 break;
3007 if (field)
3009 tree t;
3011 if (pedantic)
3012 pedwarn ("ISO C forbids casts to union type");
3013 t = digest_init (type,
3014 build_constructor (type,
3015 build_tree_list (field, value)),
3017 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3018 return t;
3020 error ("cast to union type from type not present in union");
3021 return error_mark_node;
3023 else
3025 tree otype, ovalue;
3027 /* If casting to void, avoid the error that would come
3028 from default_conversion in the case of a non-lvalue array. */
3029 if (type == void_type_node)
3030 return build1 (CONVERT_EXPR, type, value);
3032 /* Convert functions and arrays to pointers,
3033 but don't convert any other types. */
3034 value = default_function_array_conversion (value);
3035 otype = TREE_TYPE (value);
3037 /* Optionally warn about potentially worrisome casts. */
3039 if (warn_cast_qual
3040 && TREE_CODE (type) == POINTER_TYPE
3041 && TREE_CODE (otype) == POINTER_TYPE)
3043 tree in_type = type;
3044 tree in_otype = otype;
3045 int added = 0;
3046 int discarded = 0;
3048 /* Check that the qualifiers on IN_TYPE are a superset of
3049 the qualifiers of IN_OTYPE. The outermost level of
3050 POINTER_TYPE nodes is uninteresting and we stop as soon
3051 as we hit a non-POINTER_TYPE node on either type. */
3054 in_otype = TREE_TYPE (in_otype);
3055 in_type = TREE_TYPE (in_type);
3057 /* GNU C allows cv-qualified function types. 'const'
3058 means the function is very pure, 'volatile' means it
3059 can't return. We need to warn when such qualifiers
3060 are added, not when they're taken away. */
3061 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3062 && TREE_CODE (in_type) == FUNCTION_TYPE)
3063 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3064 else
3065 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3067 while (TREE_CODE (in_type) == POINTER_TYPE
3068 && TREE_CODE (in_otype) == POINTER_TYPE);
3070 if (added)
3071 warning ("cast adds new qualifiers to function type");
3073 if (discarded)
3074 /* There are qualifiers present in IN_OTYPE that are not
3075 present in IN_TYPE. */
3076 warning ("cast discards qualifiers from pointer target type");
3079 /* Warn about possible alignment problems. */
3080 if (STRICT_ALIGNMENT && warn_cast_align
3081 && TREE_CODE (type) == POINTER_TYPE
3082 && TREE_CODE (otype) == POINTER_TYPE
3083 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3084 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3085 /* Don't warn about opaque types, where the actual alignment
3086 restriction is unknown. */
3087 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3088 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3089 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3090 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3091 warning ("cast increases required alignment of target type");
3093 if (TREE_CODE (type) == INTEGER_TYPE
3094 && TREE_CODE (otype) == POINTER_TYPE
3095 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3096 && !TREE_CONSTANT (value))
3097 warning ("cast from pointer to integer of different size");
3099 if (warn_bad_function_cast
3100 && TREE_CODE (value) == CALL_EXPR
3101 && TREE_CODE (type) != TREE_CODE (otype))
3102 warning ("cast does not match function type");
3104 if (TREE_CODE (type) == POINTER_TYPE
3105 && TREE_CODE (otype) == INTEGER_TYPE
3106 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3107 /* Don't warn about converting any constant. */
3108 && !TREE_CONSTANT (value))
3109 warning ("cast to pointer from integer of different size");
3111 if (TREE_CODE (type) == POINTER_TYPE
3112 && TREE_CODE (otype) == POINTER_TYPE
3113 && TREE_CODE (expr) == ADDR_EXPR
3114 && DECL_P (TREE_OPERAND (expr, 0))
3115 && flag_strict_aliasing && warn_strict_aliasing
3116 && !VOID_TYPE_P (TREE_TYPE (type)))
3118 /* Casting the address of a decl to non void pointer. Warn
3119 if the cast breaks type based aliasing. */
3120 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3121 warning ("type-punning to incomplete type might break strict-aliasing rules");
3122 else if (!alias_sets_conflict_p
3123 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3124 get_alias_set (TREE_TYPE (type))))
3125 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3128 /* If pedantic, warn for conversions between function and object
3129 pointer types, except for converting a null pointer constant
3130 to function pointer type. */
3131 if (pedantic
3132 && TREE_CODE (type) == POINTER_TYPE
3133 && TREE_CODE (otype) == POINTER_TYPE
3134 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3135 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3136 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3138 if (pedantic
3139 && TREE_CODE (type) == POINTER_TYPE
3140 && TREE_CODE (otype) == POINTER_TYPE
3141 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3142 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3143 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3144 && TREE_CODE (expr) != NOP_EXPR))
3145 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3147 ovalue = value;
3148 /* Replace a nonvolatile const static variable with its value. */
3149 if (optimize && TREE_CODE (value) == VAR_DECL)
3150 value = decl_constant_value (value);
3151 value = convert (type, value);
3153 /* Ignore any integer overflow caused by the cast. */
3154 if (TREE_CODE (value) == INTEGER_CST)
3156 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3157 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3161 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3162 if (pedantic && TREE_CODE (value) == INTEGER_CST
3163 && TREE_CODE (expr) == INTEGER_CST
3164 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3165 value = non_lvalue (value);
3167 /* If pedantic, don't let a cast be an lvalue. */
3168 if (value == expr && pedantic)
3169 value = non_lvalue (value);
3171 return value;
3174 /* Interpret a cast of expression EXPR to type TYPE. */
3175 tree
3176 c_cast_expr (tree type, tree expr)
3178 int saved_wsp = warn_strict_prototypes;
3180 /* This avoids warnings about unprototyped casts on
3181 integers. E.g. "#define SIG_DFL (void(*)())0". */
3182 if (TREE_CODE (expr) == INTEGER_CST)
3183 warn_strict_prototypes = 0;
3184 type = groktypename (type);
3185 warn_strict_prototypes = saved_wsp;
3187 return build_c_cast (type, expr);
3191 /* Build an assignment expression of lvalue LHS from value RHS.
3192 MODIFYCODE is the code for a binary operator that we use
3193 to combine the old value of LHS with RHS to get the new value.
3194 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3196 tree
3197 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3199 tree result;
3200 tree newrhs;
3201 tree lhstype = TREE_TYPE (lhs);
3202 tree olhstype = lhstype;
3204 /* Types that aren't fully specified cannot be used in assignments. */
3205 lhs = require_complete_type (lhs);
3207 /* Avoid duplicate error messages from operands that had errors. */
3208 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3209 return error_mark_node;
3211 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3212 /* Do not use STRIP_NOPS here. We do not want an enumerator
3213 whose value is 0 to count as a null pointer constant. */
3214 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3215 rhs = TREE_OPERAND (rhs, 0);
3217 newrhs = rhs;
3219 /* Handle control structure constructs used as "lvalues". */
3221 switch (TREE_CODE (lhs))
3223 /* Handle (a, b) used as an "lvalue". */
3224 case COMPOUND_EXPR:
3225 pedantic_lvalue_warning (COMPOUND_EXPR);
3226 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3227 if (TREE_CODE (newrhs) == ERROR_MARK)
3228 return error_mark_node;
3229 return build (COMPOUND_EXPR, lhstype,
3230 TREE_OPERAND (lhs, 0), newrhs);
3232 /* Handle (a ? b : c) used as an "lvalue". */
3233 case COND_EXPR:
3234 pedantic_lvalue_warning (COND_EXPR);
3235 rhs = save_expr (rhs);
3237 /* Produce (a ? (b = rhs) : (c = rhs))
3238 except that the RHS goes through a save-expr
3239 so the code to compute it is only emitted once. */
3240 tree cond
3241 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3242 build_modify_expr (TREE_OPERAND (lhs, 1),
3243 modifycode, rhs),
3244 build_modify_expr (TREE_OPERAND (lhs, 2),
3245 modifycode, rhs));
3246 if (TREE_CODE (cond) == ERROR_MARK)
3247 return cond;
3248 /* Make sure the code to compute the rhs comes out
3249 before the split. */
3250 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3251 /* But cast it to void to avoid an "unused" error. */
3252 convert (void_type_node, rhs), cond);
3254 default:
3255 break;
3258 /* If a binary op has been requested, combine the old LHS value with the RHS
3259 producing the value we should actually store into the LHS. */
3261 if (modifycode != NOP_EXPR)
3263 lhs = stabilize_reference (lhs);
3264 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3267 /* Handle a cast used as an "lvalue".
3268 We have already performed any binary operator using the value as cast.
3269 Now convert the result to the cast type of the lhs,
3270 and then true type of the lhs and store it there;
3271 then convert result back to the cast type to be the value
3272 of the assignment. */
3274 switch (TREE_CODE (lhs))
3276 case NOP_EXPR:
3277 case CONVERT_EXPR:
3278 case FLOAT_EXPR:
3279 case FIX_TRUNC_EXPR:
3280 case FIX_FLOOR_EXPR:
3281 case FIX_ROUND_EXPR:
3282 case FIX_CEIL_EXPR:
3283 newrhs = default_function_array_conversion (newrhs);
3285 tree inner_lhs = TREE_OPERAND (lhs, 0);
3286 tree result;
3287 result = build_modify_expr (inner_lhs, NOP_EXPR,
3288 convert (TREE_TYPE (inner_lhs),
3289 convert (lhstype, newrhs)));
3290 if (TREE_CODE (result) == ERROR_MARK)
3291 return result;
3292 pedantic_lvalue_warning (CONVERT_EXPR);
3293 return convert (TREE_TYPE (lhs), result);
3296 default:
3297 break;
3300 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3301 Reject anything strange now. */
3303 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3304 return error_mark_node;
3306 /* Warn about storing in something that is `const'. */
3308 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3309 || ((TREE_CODE (lhstype) == RECORD_TYPE
3310 || TREE_CODE (lhstype) == UNION_TYPE)
3311 && C_TYPE_FIELDS_READONLY (lhstype)))
3312 readonly_warning (lhs, "assignment");
3314 /* If storing into a structure or union member,
3315 it has probably been given type `int'.
3316 Compute the type that would go with
3317 the actual amount of storage the member occupies. */
3319 if (TREE_CODE (lhs) == COMPONENT_REF
3320 && (TREE_CODE (lhstype) == INTEGER_TYPE
3321 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3322 || TREE_CODE (lhstype) == REAL_TYPE
3323 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3324 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3326 /* If storing in a field that is in actuality a short or narrower than one,
3327 we must store in the field in its actual type. */
3329 if (lhstype != TREE_TYPE (lhs))
3331 lhs = copy_node (lhs);
3332 TREE_TYPE (lhs) = lhstype;
3335 /* Convert new value to destination type. */
3337 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3338 NULL_TREE, NULL_TREE, 0);
3339 if (TREE_CODE (newrhs) == ERROR_MARK)
3340 return error_mark_node;
3342 /* Scan operands */
3344 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3345 TREE_SIDE_EFFECTS (result) = 1;
3347 /* If we got the LHS in a different type for storing in,
3348 convert the result back to the nominal type of LHS
3349 so that the value we return always has the same type
3350 as the LHS argument. */
3352 if (olhstype == TREE_TYPE (result))
3353 return result;
3354 return convert_for_assignment (olhstype, result, _("assignment"),
3355 NULL_TREE, NULL_TREE, 0);
3358 /* Convert value RHS to type TYPE as preparation for an assignment
3359 to an lvalue of type TYPE.
3360 The real work of conversion is done by `convert'.
3361 The purpose of this function is to generate error messages
3362 for assignments that are not allowed in C.
3363 ERRTYPE is a string to use in error messages:
3364 "assignment", "return", etc. If it is null, this is parameter passing
3365 for a function call (and different error messages are output).
3367 FUNNAME is the name of the function being called,
3368 as an IDENTIFIER_NODE, or null.
3369 PARMNUM is the number of the argument, for printing in error messages. */
3371 static tree
3372 convert_for_assignment (tree type, tree rhs, const char *errtype,
3373 tree fundecl, tree funname, int parmnum)
3375 enum tree_code codel = TREE_CODE (type);
3376 tree rhstype;
3377 enum tree_code coder;
3379 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3380 /* Do not use STRIP_NOPS here. We do not want an enumerator
3381 whose value is 0 to count as a null pointer constant. */
3382 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3383 rhs = TREE_OPERAND (rhs, 0);
3385 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3386 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3387 rhs = default_conversion (rhs);
3388 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3389 rhs = decl_constant_value_for_broken_optimization (rhs);
3391 rhstype = TREE_TYPE (rhs);
3392 coder = TREE_CODE (rhstype);
3394 if (coder == ERROR_MARK)
3395 return error_mark_node;
3397 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3399 overflow_warning (rhs);
3400 /* Check for Objective-C protocols. This will automatically
3401 issue a warning if there are protocol violations. No need to
3402 use the return value. */
3403 if (c_dialect_objc ())
3404 objc_comptypes (type, rhstype, 0);
3405 return rhs;
3408 if (coder == VOID_TYPE)
3410 error ("void value not ignored as it ought to be");
3411 return error_mark_node;
3413 /* A type converts to a reference to it.
3414 This code doesn't fully support references, it's just for the
3415 special case of va_start and va_copy. */
3416 if (codel == REFERENCE_TYPE
3417 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3419 if (!lvalue_p (rhs))
3421 error ("cannot pass rvalue to reference parameter");
3422 return error_mark_node;
3424 if (!c_mark_addressable (rhs))
3425 return error_mark_node;
3426 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3428 /* We already know that these two types are compatible, but they
3429 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3430 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3431 likely to be va_list, a typedef to __builtin_va_list, which
3432 is different enough that it will cause problems later. */
3433 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3434 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3436 rhs = build1 (NOP_EXPR, type, rhs);
3437 return rhs;
3439 /* Some types can interconvert without explicit casts. */
3440 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3441 && ((*targetm.vector_opaque_p) (type)
3442 || (*targetm.vector_opaque_p) (rhstype)))
3443 return convert (type, rhs);
3444 /* Arithmetic types all interconvert, and enum is treated like int. */
3445 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3446 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3447 || codel == BOOLEAN_TYPE)
3448 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3449 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3450 || coder == BOOLEAN_TYPE))
3451 return convert_and_check (type, rhs);
3453 /* Conversion to a transparent union from its member types.
3454 This applies only to function arguments. */
3455 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3457 tree memb_types;
3458 tree marginal_memb_type = 0;
3460 for (memb_types = TYPE_FIELDS (type); memb_types;
3461 memb_types = TREE_CHAIN (memb_types))
3463 tree memb_type = TREE_TYPE (memb_types);
3465 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3466 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3467 break;
3469 if (TREE_CODE (memb_type) != POINTER_TYPE)
3470 continue;
3472 if (coder == POINTER_TYPE)
3474 tree ttl = TREE_TYPE (memb_type);
3475 tree ttr = TREE_TYPE (rhstype);
3477 /* Any non-function converts to a [const][volatile] void *
3478 and vice versa; otherwise, targets must be the same.
3479 Meanwhile, the lhs target must have all the qualifiers of
3480 the rhs. */
3481 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3482 || comp_target_types (memb_type, rhstype, 0))
3484 /* If this type won't generate any warnings, use it. */
3485 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3486 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3487 && TREE_CODE (ttl) == FUNCTION_TYPE)
3488 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3489 == TYPE_QUALS (ttr))
3490 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3491 == TYPE_QUALS (ttl))))
3492 break;
3494 /* Keep looking for a better type, but remember this one. */
3495 if (! marginal_memb_type)
3496 marginal_memb_type = memb_type;
3500 /* Can convert integer zero to any pointer type. */
3501 if (integer_zerop (rhs)
3502 || (TREE_CODE (rhs) == NOP_EXPR
3503 && integer_zerop (TREE_OPERAND (rhs, 0))))
3505 rhs = null_pointer_node;
3506 break;
3510 if (memb_types || marginal_memb_type)
3512 if (! memb_types)
3514 /* We have only a marginally acceptable member type;
3515 it needs a warning. */
3516 tree ttl = TREE_TYPE (marginal_memb_type);
3517 tree ttr = TREE_TYPE (rhstype);
3519 /* Const and volatile mean something different for function
3520 types, so the usual warnings are not appropriate. */
3521 if (TREE_CODE (ttr) == FUNCTION_TYPE
3522 && TREE_CODE (ttl) == FUNCTION_TYPE)
3524 /* Because const and volatile on functions are
3525 restrictions that say the function will not do
3526 certain things, it is okay to use a const or volatile
3527 function where an ordinary one is wanted, but not
3528 vice-versa. */
3529 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3530 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3531 errtype, funname, parmnum);
3533 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3534 warn_for_assignment ("%s discards qualifiers from pointer target type",
3535 errtype, funname,
3536 parmnum);
3539 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3540 pedwarn ("ISO C prohibits argument conversion to union type");
3542 return build1 (NOP_EXPR, type, rhs);
3546 /* Conversions among pointers */
3547 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3548 && (coder == codel))
3550 tree ttl = TREE_TYPE (type);
3551 tree ttr = TREE_TYPE (rhstype);
3552 bool is_opaque_pointer;
3553 int target_cmp = 0; /* Cache comp_target_types () result. */
3555 /* Opaque pointers are treated like void pointers. */
3556 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3557 || (*targetm.vector_opaque_p) (rhstype))
3558 && TREE_CODE (ttl) == VECTOR_TYPE
3559 && TREE_CODE (ttr) == VECTOR_TYPE;
3561 /* Any non-function converts to a [const][volatile] void *
3562 and vice versa; otherwise, targets must be the same.
3563 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3564 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3565 || (target_cmp = comp_target_types (type, rhstype, 0))
3566 || is_opaque_pointer
3567 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3568 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3570 if (pedantic
3571 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3573 (VOID_TYPE_P (ttr)
3574 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3575 which are not ANSI null ptr constants. */
3576 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3577 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3578 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3579 errtype, funname, parmnum);
3580 /* Const and volatile mean something different for function types,
3581 so the usual warnings are not appropriate. */
3582 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3583 && TREE_CODE (ttl) != FUNCTION_TYPE)
3585 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3586 warn_for_assignment ("%s discards qualifiers from pointer target type",
3587 errtype, funname, parmnum);
3588 /* If this is not a case of ignoring a mismatch in signedness,
3589 no warning. */
3590 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3591 || target_cmp)
3593 /* If there is a mismatch, do warn. */
3594 else if (pedantic)
3595 warn_for_assignment ("pointer targets in %s differ in signedness",
3596 errtype, funname, parmnum);
3598 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3599 && TREE_CODE (ttr) == FUNCTION_TYPE)
3601 /* Because const and volatile on functions are restrictions
3602 that say the function will not do certain things,
3603 it is okay to use a const or volatile function
3604 where an ordinary one is wanted, but not vice-versa. */
3605 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3606 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3607 errtype, funname, parmnum);
3610 else
3611 warn_for_assignment ("%s from incompatible pointer type",
3612 errtype, funname, parmnum);
3613 return convert (type, rhs);
3615 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3617 error ("invalid use of non-lvalue array");
3618 return error_mark_node;
3620 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3622 /* An explicit constant 0 can convert to a pointer,
3623 or one that results from arithmetic, even including
3624 a cast to integer type. */
3625 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3627 ! (TREE_CODE (rhs) == NOP_EXPR
3628 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3629 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3630 && integer_zerop (TREE_OPERAND (rhs, 0))))
3631 warn_for_assignment ("%s makes pointer from integer without a cast",
3632 errtype, funname, parmnum);
3634 return convert (type, rhs);
3636 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3638 warn_for_assignment ("%s makes integer from pointer without a cast",
3639 errtype, funname, parmnum);
3640 return convert (type, rhs);
3642 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3643 return convert (type, rhs);
3645 if (!errtype)
3647 if (funname)
3649 tree selector = objc_message_selector ();
3651 if (selector && parmnum > 2)
3652 error ("incompatible type for argument %d of `%s'",
3653 parmnum - 2, IDENTIFIER_POINTER (selector));
3654 else
3655 error ("incompatible type for argument %d of `%s'",
3656 parmnum, IDENTIFIER_POINTER (funname));
3658 else
3659 error ("incompatible type for argument %d of indirect function call",
3660 parmnum);
3662 else
3663 error ("incompatible types in %s", errtype);
3665 return error_mark_node;
3668 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3669 is used for error and waring reporting and indicates which argument
3670 is being processed. */
3672 tree
3673 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3675 tree ret, type;
3677 /* If FN was prototyped, the value has been converted already
3678 in convert_arguments. */
3679 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3680 return value;
3682 type = TREE_TYPE (parm);
3683 ret = convert_for_assignment (type, value,
3684 (char *) 0 /* arg passing */, fn,
3685 DECL_NAME (fn), argnum);
3686 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3687 && INTEGRAL_TYPE_P (type)
3688 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3689 ret = default_conversion (ret);
3690 return ret;
3693 /* Print a warning using MSGID.
3694 It gets OPNAME as its one parameter.
3695 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3696 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3697 FUNCTION and ARGNUM are handled specially if we are building an
3698 Objective-C selector. */
3700 static void
3701 warn_for_assignment (const char *msgid, const char *opname, tree function,
3702 int argnum)
3704 if (opname == 0)
3706 tree selector = objc_message_selector ();
3707 char * new_opname;
3709 if (selector && argnum > 2)
3711 function = selector;
3712 argnum -= 2;
3714 if (argnum == 0)
3716 if (function)
3718 /* Function name is known; supply it. */
3719 const char *const argstring = _("passing arg of `%s'");
3720 new_opname = alloca (IDENTIFIER_LENGTH (function)
3721 + strlen (argstring) + 1 + 1);
3722 sprintf (new_opname, argstring,
3723 IDENTIFIER_POINTER (function));
3725 else
3727 /* Function name unknown (call through ptr). */
3728 const char *const argnofun = _("passing arg of pointer to function");
3729 new_opname = alloca (strlen (argnofun) + 1 + 1);
3730 sprintf (new_opname, argnofun);
3733 else if (function)
3735 /* Function name is known; supply it. */
3736 const char *const argstring = _("passing arg %d of `%s'");
3737 new_opname = alloca (IDENTIFIER_LENGTH (function)
3738 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3739 sprintf (new_opname, argstring, argnum,
3740 IDENTIFIER_POINTER (function));
3742 else
3744 /* Function name unknown (call through ptr); just give arg number. */
3745 const char *const argnofun = _("passing arg %d of pointer to function");
3746 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3747 sprintf (new_opname, argnofun, argnum);
3749 opname = new_opname;
3751 pedwarn (msgid, opname);
3754 /* If VALUE is a compound expr all of whose expressions are constant, then
3755 return its value. Otherwise, return error_mark_node.
3757 This is for handling COMPOUND_EXPRs as initializer elements
3758 which is allowed with a warning when -pedantic is specified. */
3760 static tree
3761 valid_compound_expr_initializer (tree value, tree endtype)
3763 if (TREE_CODE (value) == COMPOUND_EXPR)
3765 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3766 == error_mark_node)
3767 return error_mark_node;
3768 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3769 endtype);
3771 else if (! TREE_CONSTANT (value)
3772 && ! initializer_constant_valid_p (value, endtype))
3773 return error_mark_node;
3774 else
3775 return value;
3778 /* Perform appropriate conversions on the initial value of a variable,
3779 store it in the declaration DECL,
3780 and print any error messages that are appropriate.
3781 If the init is invalid, store an ERROR_MARK. */
3783 void
3784 store_init_value (tree decl, tree init)
3786 tree value, type;
3788 /* If variable's type was invalidly declared, just ignore it. */
3790 type = TREE_TYPE (decl);
3791 if (TREE_CODE (type) == ERROR_MARK)
3792 return;
3794 /* Digest the specified initializer into an expression. */
3796 value = digest_init (type, init, TREE_STATIC (decl));
3798 /* Store the expression if valid; else report error. */
3800 if (warn_traditional && !in_system_header
3801 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3802 warning ("traditional C rejects automatic aggregate initialization");
3804 DECL_INITIAL (decl) = value;
3806 /* ANSI wants warnings about out-of-range constant initializers. */
3807 STRIP_TYPE_NOPS (value);
3808 constant_expression_warning (value);
3810 /* Check if we need to set array size from compound literal size. */
3811 if (TREE_CODE (type) == ARRAY_TYPE
3812 && TYPE_DOMAIN (type) == 0
3813 && value != error_mark_node)
3815 tree inside_init = init;
3817 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3818 inside_init = TREE_OPERAND (init, 0);
3819 inside_init = fold (inside_init);
3821 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3823 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3825 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3827 /* For int foo[] = (int [3]){1}; we need to set array size
3828 now since later on array initializer will be just the
3829 brace enclosed list of the compound literal. */
3830 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3831 layout_type (type);
3832 layout_decl (decl, 0);
3838 /* Methods for storing and printing names for error messages. */
3840 /* Implement a spelling stack that allows components of a name to be pushed
3841 and popped. Each element on the stack is this structure. */
3843 struct spelling
3845 int kind;
3846 union
3848 int i;
3849 const char *s;
3850 } u;
3853 #define SPELLING_STRING 1
3854 #define SPELLING_MEMBER 2
3855 #define SPELLING_BOUNDS 3
3857 static struct spelling *spelling; /* Next stack element (unused). */
3858 static struct spelling *spelling_base; /* Spelling stack base. */
3859 static int spelling_size; /* Size of the spelling stack. */
3861 /* Macros to save and restore the spelling stack around push_... functions.
3862 Alternative to SAVE_SPELLING_STACK. */
3864 #define SPELLING_DEPTH() (spelling - spelling_base)
3865 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3867 /* Push an element on the spelling stack with type KIND and assign VALUE
3868 to MEMBER. */
3870 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3872 int depth = SPELLING_DEPTH (); \
3874 if (depth >= spelling_size) \
3876 spelling_size += 10; \
3877 if (spelling_base == 0) \
3878 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3879 else \
3880 spelling_base = xrealloc (spelling_base, \
3881 spelling_size * sizeof (struct spelling)); \
3882 RESTORE_SPELLING_DEPTH (depth); \
3885 spelling->kind = (KIND); \
3886 spelling->MEMBER = (VALUE); \
3887 spelling++; \
3890 /* Push STRING on the stack. Printed literally. */
3892 static void
3893 push_string (const char *string)
3895 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3898 /* Push a member name on the stack. Printed as '.' STRING. */
3900 static void
3901 push_member_name (tree decl)
3903 const char *const string
3904 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3905 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3908 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3910 static void
3911 push_array_bounds (int bounds)
3913 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3916 /* Compute the maximum size in bytes of the printed spelling. */
3918 static int
3919 spelling_length (void)
3921 int size = 0;
3922 struct spelling *p;
3924 for (p = spelling_base; p < spelling; p++)
3926 if (p->kind == SPELLING_BOUNDS)
3927 size += 25;
3928 else
3929 size += strlen (p->u.s) + 1;
3932 return size;
3935 /* Print the spelling to BUFFER and return it. */
3937 static char *
3938 print_spelling (char *buffer)
3940 char *d = buffer;
3941 struct spelling *p;
3943 for (p = spelling_base; p < spelling; p++)
3944 if (p->kind == SPELLING_BOUNDS)
3946 sprintf (d, "[%d]", p->u.i);
3947 d += strlen (d);
3949 else
3951 const char *s;
3952 if (p->kind == SPELLING_MEMBER)
3953 *d++ = '.';
3954 for (s = p->u.s; (*d = *s++); d++)
3957 *d++ = '\0';
3958 return buffer;
3961 /* Issue an error message for a bad initializer component.
3962 MSGID identifies the message.
3963 The component name is taken from the spelling stack. */
3965 void
3966 error_init (const char *msgid)
3968 char *ofwhat;
3970 error ("%s", _(msgid));
3971 ofwhat = print_spelling (alloca (spelling_length () + 1));
3972 if (*ofwhat)
3973 error ("(near initialization for `%s')", ofwhat);
3976 /* Issue a pedantic warning for a bad initializer component.
3977 MSGID identifies the message.
3978 The component name is taken from the spelling stack. */
3980 void
3981 pedwarn_init (const char *msgid)
3983 char *ofwhat;
3985 pedwarn ("%s", _(msgid));
3986 ofwhat = print_spelling (alloca (spelling_length () + 1));
3987 if (*ofwhat)
3988 pedwarn ("(near initialization for `%s')", ofwhat);
3991 /* Issue a warning for a bad initializer component.
3992 MSGID identifies the message.
3993 The component name is taken from the spelling stack. */
3995 static void
3996 warning_init (const char *msgid)
3998 char *ofwhat;
4000 warning ("%s", _(msgid));
4001 ofwhat = print_spelling (alloca (spelling_length () + 1));
4002 if (*ofwhat)
4003 warning ("(near initialization for `%s')", ofwhat);
4006 /* Digest the parser output INIT as an initializer for type TYPE.
4007 Return a C expression of type TYPE to represent the initial value.
4009 REQUIRE_CONSTANT requests an error if non-constant initializers or
4010 elements are seen. */
4012 static tree
4013 digest_init (tree type, tree init, int require_constant)
4015 enum tree_code code = TREE_CODE (type);
4016 tree inside_init = init;
4018 if (type == error_mark_node
4019 || init == error_mark_node
4020 || TREE_TYPE (init) == error_mark_node)
4021 return error_mark_node;
4023 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4024 /* Do not use STRIP_NOPS here. We do not want an enumerator
4025 whose value is 0 to count as a null pointer constant. */
4026 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4027 inside_init = TREE_OPERAND (init, 0);
4029 inside_init = fold (inside_init);
4031 /* Initialization of an array of chars from a string constant
4032 optionally enclosed in braces. */
4034 if (code == ARRAY_TYPE)
4036 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4037 if ((typ1 == char_type_node
4038 || typ1 == signed_char_type_node
4039 || typ1 == unsigned_char_type_node
4040 || typ1 == unsigned_wchar_type_node
4041 || typ1 == signed_wchar_type_node)
4042 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4044 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4045 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
4046 return inside_init;
4048 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4049 != char_type_node)
4050 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4052 error_init ("char-array initialized from wide string");
4053 return error_mark_node;
4055 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4056 == char_type_node)
4057 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4059 error_init ("int-array initialized from non-wide string");
4060 return error_mark_node;
4063 TREE_TYPE (inside_init) = type;
4064 if (TYPE_DOMAIN (type) != 0
4065 && TYPE_SIZE (type) != 0
4066 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4067 /* Subtract 1 (or sizeof (wchar_t))
4068 because it's ok to ignore the terminating null char
4069 that is counted in the length of the constant. */
4070 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4071 TREE_STRING_LENGTH (inside_init)
4072 - ((TYPE_PRECISION (typ1)
4073 != TYPE_PRECISION (char_type_node))
4074 ? (TYPE_PRECISION (wchar_type_node)
4075 / BITS_PER_UNIT)
4076 : 1)))
4077 pedwarn_init ("initializer-string for array of chars is too long");
4079 return inside_init;
4083 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4084 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4085 below and handle as a constructor. */
4086 if (code == VECTOR_TYPE
4087 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
4088 && TREE_CONSTANT (inside_init))
4090 if (TREE_CODE (inside_init) == VECTOR_CST
4091 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4092 TYPE_MAIN_VARIANT (type),
4093 COMPARE_STRICT))
4094 return inside_init;
4095 else
4096 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4099 /* Any type can be initialized
4100 from an expression of the same type, optionally with braces. */
4102 if (inside_init && TREE_TYPE (inside_init) != 0
4103 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4104 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4105 || (code == ARRAY_TYPE
4106 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4107 || (code == VECTOR_TYPE
4108 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4109 || (code == POINTER_TYPE
4110 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4111 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4112 TREE_TYPE (type), COMPARE_STRICT))
4113 || (code == POINTER_TYPE
4114 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4115 && comptypes (TREE_TYPE (inside_init),
4116 TREE_TYPE (type), COMPARE_STRICT))))
4118 if (code == POINTER_TYPE)
4120 inside_init = default_function_array_conversion (inside_init);
4122 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4124 error_init ("invalid use of non-lvalue array");
4125 return error_mark_node;
4129 if (code == VECTOR_TYPE)
4130 /* Although the types are compatible, we may require a
4131 conversion. */
4132 inside_init = convert (type, inside_init);
4134 if (require_constant && !flag_isoc99
4135 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4137 /* As an extension, allow initializing objects with static storage
4138 duration with compound literals (which are then treated just as
4139 the brace enclosed list they contain). */
4140 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4141 inside_init = DECL_INITIAL (decl);
4144 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4145 && TREE_CODE (inside_init) != CONSTRUCTOR)
4147 error_init ("array initialized from non-constant array expression");
4148 return error_mark_node;
4151 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4152 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4154 /* Compound expressions can only occur here if -pedantic or
4155 -pedantic-errors is specified. In the later case, we always want
4156 an error. In the former case, we simply want a warning. */
4157 if (require_constant && pedantic
4158 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4160 inside_init
4161 = valid_compound_expr_initializer (inside_init,
4162 TREE_TYPE (inside_init));
4163 if (inside_init == error_mark_node)
4164 error_init ("initializer element is not constant");
4165 else
4166 pedwarn_init ("initializer element is not constant");
4167 if (flag_pedantic_errors)
4168 inside_init = error_mark_node;
4170 else if (require_constant
4171 && (!TREE_CONSTANT (inside_init)
4172 /* This test catches things like `7 / 0' which
4173 result in an expression for which TREE_CONSTANT
4174 is true, but which is not actually something
4175 that is a legal constant. We really should not
4176 be using this function, because it is a part of
4177 the back-end. Instead, the expression should
4178 already have been turned into ERROR_MARK_NODE. */
4179 || !initializer_constant_valid_p (inside_init,
4180 TREE_TYPE (inside_init))))
4182 error_init ("initializer element is not constant");
4183 inside_init = error_mark_node;
4186 return inside_init;
4189 /* Handle scalar types, including conversions. */
4191 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4192 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4194 /* Note that convert_for_assignment calls default_conversion
4195 for arrays and functions. We must not call it in the
4196 case where inside_init is a null pointer constant. */
4197 inside_init
4198 = convert_for_assignment (type, init, _("initialization"),
4199 NULL_TREE, NULL_TREE, 0);
4201 if (require_constant && ! TREE_CONSTANT (inside_init))
4203 error_init ("initializer element is not constant");
4204 inside_init = error_mark_node;
4206 else if (require_constant
4207 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4209 error_init ("initializer element is not computable at load time");
4210 inside_init = error_mark_node;
4213 return inside_init;
4216 /* Come here only for records and arrays. */
4218 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4220 error_init ("variable-sized object may not be initialized");
4221 return error_mark_node;
4224 error_init ("invalid initializer");
4225 return error_mark_node;
4228 /* Handle initializers that use braces. */
4230 /* Type of object we are accumulating a constructor for.
4231 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4232 static tree constructor_type;
4234 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4235 left to fill. */
4236 static tree constructor_fields;
4238 /* For an ARRAY_TYPE, this is the specified index
4239 at which to store the next element we get. */
4240 static tree constructor_index;
4242 /* For an ARRAY_TYPE, this is the maximum index. */
4243 static tree constructor_max_index;
4245 /* For a RECORD_TYPE, this is the first field not yet written out. */
4246 static tree constructor_unfilled_fields;
4248 /* For an ARRAY_TYPE, this is the index of the first element
4249 not yet written out. */
4250 static tree constructor_unfilled_index;
4252 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4253 This is so we can generate gaps between fields, when appropriate. */
4254 static tree constructor_bit_index;
4256 /* If we are saving up the elements rather than allocating them,
4257 this is the list of elements so far (in reverse order,
4258 most recent first). */
4259 static tree constructor_elements;
4261 /* 1 if constructor should be incrementally stored into a constructor chain,
4262 0 if all the elements should be kept in AVL tree. */
4263 static int constructor_incremental;
4265 /* 1 if so far this constructor's elements are all compile-time constants. */
4266 static int constructor_constant;
4268 /* 1 if so far this constructor's elements are all valid address constants. */
4269 static int constructor_simple;
4271 /* 1 if this constructor is erroneous so far. */
4272 static int constructor_erroneous;
4274 /* Structure for managing pending initializer elements, organized as an
4275 AVL tree. */
4277 struct init_node
4279 struct init_node *left, *right;
4280 struct init_node *parent;
4281 int balance;
4282 tree purpose;
4283 tree value;
4286 /* Tree of pending elements at this constructor level.
4287 These are elements encountered out of order
4288 which belong at places we haven't reached yet in actually
4289 writing the output.
4290 Will never hold tree nodes across GC runs. */
4291 static struct init_node *constructor_pending_elts;
4293 /* The SPELLING_DEPTH of this constructor. */
4294 static int constructor_depth;
4296 /* 0 if implicitly pushing constructor levels is allowed. */
4297 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4299 static int require_constant_value;
4300 static int require_constant_elements;
4302 /* DECL node for which an initializer is being read.
4303 0 means we are reading a constructor expression
4304 such as (struct foo) {...}. */
4305 static tree constructor_decl;
4307 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4308 static const char *constructor_asmspec;
4310 /* Nonzero if this is an initializer for a top-level decl. */
4311 static int constructor_top_level;
4313 /* Nonzero if there were any member designators in this initializer. */
4314 static int constructor_designated;
4316 /* Nesting depth of designator list. */
4317 static int designator_depth;
4319 /* Nonzero if there were diagnosed errors in this designator list. */
4320 static int designator_errorneous;
4323 /* This stack has a level for each implicit or explicit level of
4324 structuring in the initializer, including the outermost one. It
4325 saves the values of most of the variables above. */
4327 struct constructor_range_stack;
4329 struct constructor_stack
4331 struct constructor_stack *next;
4332 tree type;
4333 tree fields;
4334 tree index;
4335 tree max_index;
4336 tree unfilled_index;
4337 tree unfilled_fields;
4338 tree bit_index;
4339 tree elements;
4340 struct init_node *pending_elts;
4341 int offset;
4342 int depth;
4343 /* If nonzero, this value should replace the entire
4344 constructor at this level. */
4345 tree replacement_value;
4346 struct constructor_range_stack *range_stack;
4347 char constant;
4348 char simple;
4349 char implicit;
4350 char erroneous;
4351 char outer;
4352 char incremental;
4353 char designated;
4356 struct constructor_stack *constructor_stack;
4358 /* This stack represents designators from some range designator up to
4359 the last designator in the list. */
4361 struct constructor_range_stack
4363 struct constructor_range_stack *next, *prev;
4364 struct constructor_stack *stack;
4365 tree range_start;
4366 tree index;
4367 tree range_end;
4368 tree fields;
4371 struct constructor_range_stack *constructor_range_stack;
4373 /* This stack records separate initializers that are nested.
4374 Nested initializers can't happen in ANSI C, but GNU C allows them
4375 in cases like { ... (struct foo) { ... } ... }. */
4377 struct initializer_stack
4379 struct initializer_stack *next;
4380 tree decl;
4381 const char *asmspec;
4382 struct constructor_stack *constructor_stack;
4383 struct constructor_range_stack *constructor_range_stack;
4384 tree elements;
4385 struct spelling *spelling;
4386 struct spelling *spelling_base;
4387 int spelling_size;
4388 char top_level;
4389 char require_constant_value;
4390 char require_constant_elements;
4393 struct initializer_stack *initializer_stack;
4395 /* Prepare to parse and output the initializer for variable DECL. */
4397 void
4398 start_init (tree decl, tree asmspec_tree, int top_level)
4400 const char *locus;
4401 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4402 const char *asmspec = 0;
4404 if (asmspec_tree)
4405 asmspec = TREE_STRING_POINTER (asmspec_tree);
4407 p->decl = constructor_decl;
4408 p->asmspec = constructor_asmspec;
4409 p->require_constant_value = require_constant_value;
4410 p->require_constant_elements = require_constant_elements;
4411 p->constructor_stack = constructor_stack;
4412 p->constructor_range_stack = constructor_range_stack;
4413 p->elements = constructor_elements;
4414 p->spelling = spelling;
4415 p->spelling_base = spelling_base;
4416 p->spelling_size = spelling_size;
4417 p->top_level = constructor_top_level;
4418 p->next = initializer_stack;
4419 initializer_stack = p;
4421 constructor_decl = decl;
4422 constructor_asmspec = asmspec;
4423 constructor_designated = 0;
4424 constructor_top_level = top_level;
4426 if (decl != 0)
4428 require_constant_value = TREE_STATIC (decl);
4429 require_constant_elements
4430 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4431 /* For a scalar, you can always use any value to initialize,
4432 even within braces. */
4433 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4434 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4435 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4436 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4437 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4439 else
4441 require_constant_value = 0;
4442 require_constant_elements = 0;
4443 locus = "(anonymous)";
4446 constructor_stack = 0;
4447 constructor_range_stack = 0;
4449 missing_braces_mentioned = 0;
4451 spelling_base = 0;
4452 spelling_size = 0;
4453 RESTORE_SPELLING_DEPTH (0);
4455 if (locus)
4456 push_string (locus);
4459 void
4460 finish_init (void)
4462 struct initializer_stack *p = initializer_stack;
4464 /* Free the whole constructor stack of this initializer. */
4465 while (constructor_stack)
4467 struct constructor_stack *q = constructor_stack;
4468 constructor_stack = q->next;
4469 free (q);
4472 if (constructor_range_stack)
4473 abort ();
4475 /* Pop back to the data of the outer initializer (if any). */
4476 free (spelling_base);
4478 constructor_decl = p->decl;
4479 constructor_asmspec = p->asmspec;
4480 require_constant_value = p->require_constant_value;
4481 require_constant_elements = p->require_constant_elements;
4482 constructor_stack = p->constructor_stack;
4483 constructor_range_stack = p->constructor_range_stack;
4484 constructor_elements = p->elements;
4485 spelling = p->spelling;
4486 spelling_base = p->spelling_base;
4487 spelling_size = p->spelling_size;
4488 constructor_top_level = p->top_level;
4489 initializer_stack = p->next;
4490 free (p);
4493 /* Call here when we see the initializer is surrounded by braces.
4494 This is instead of a call to push_init_level;
4495 it is matched by a call to pop_init_level.
4497 TYPE is the type to initialize, for a constructor expression.
4498 For an initializer for a decl, TYPE is zero. */
4500 void
4501 really_start_incremental_init (tree type)
4503 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4505 if (type == 0)
4506 type = TREE_TYPE (constructor_decl);
4508 if ((*targetm.vector_opaque_p) (type))
4509 error ("opaque vector types cannot be initialized");
4511 p->type = constructor_type;
4512 p->fields = constructor_fields;
4513 p->index = constructor_index;
4514 p->max_index = constructor_max_index;
4515 p->unfilled_index = constructor_unfilled_index;
4516 p->unfilled_fields = constructor_unfilled_fields;
4517 p->bit_index = constructor_bit_index;
4518 p->elements = constructor_elements;
4519 p->constant = constructor_constant;
4520 p->simple = constructor_simple;
4521 p->erroneous = constructor_erroneous;
4522 p->pending_elts = constructor_pending_elts;
4523 p->depth = constructor_depth;
4524 p->replacement_value = 0;
4525 p->implicit = 0;
4526 p->range_stack = 0;
4527 p->outer = 0;
4528 p->incremental = constructor_incremental;
4529 p->designated = constructor_designated;
4530 p->next = 0;
4531 constructor_stack = p;
4533 constructor_constant = 1;
4534 constructor_simple = 1;
4535 constructor_depth = SPELLING_DEPTH ();
4536 constructor_elements = 0;
4537 constructor_pending_elts = 0;
4538 constructor_type = type;
4539 constructor_incremental = 1;
4540 constructor_designated = 0;
4541 designator_depth = 0;
4542 designator_errorneous = 0;
4544 if (TREE_CODE (constructor_type) == RECORD_TYPE
4545 || TREE_CODE (constructor_type) == UNION_TYPE)
4547 constructor_fields = TYPE_FIELDS (constructor_type);
4548 /* Skip any nameless bit fields at the beginning. */
4549 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4550 && DECL_NAME (constructor_fields) == 0)
4551 constructor_fields = TREE_CHAIN (constructor_fields);
4553 constructor_unfilled_fields = constructor_fields;
4554 constructor_bit_index = bitsize_zero_node;
4556 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4558 if (TYPE_DOMAIN (constructor_type))
4560 constructor_max_index
4561 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4563 /* Detect non-empty initializations of zero-length arrays. */
4564 if (constructor_max_index == NULL_TREE
4565 && TYPE_SIZE (constructor_type))
4566 constructor_max_index = build_int_2 (-1, -1);
4568 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4569 to initialize VLAs will cause a proper error; avoid tree
4570 checking errors as well by setting a safe value. */
4571 if (constructor_max_index
4572 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4573 constructor_max_index = build_int_2 (-1, -1);
4575 constructor_index
4576 = convert (bitsizetype,
4577 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4579 else
4580 constructor_index = bitsize_zero_node;
4582 constructor_unfilled_index = constructor_index;
4584 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4586 /* Vectors are like simple fixed-size arrays. */
4587 constructor_max_index =
4588 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4589 constructor_index = convert (bitsizetype, bitsize_zero_node);
4590 constructor_unfilled_index = constructor_index;
4592 else
4594 /* Handle the case of int x = {5}; */
4595 constructor_fields = constructor_type;
4596 constructor_unfilled_fields = constructor_type;
4600 /* Push down into a subobject, for initialization.
4601 If this is for an explicit set of braces, IMPLICIT is 0.
4602 If it is because the next element belongs at a lower level,
4603 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4605 void
4606 push_init_level (int implicit)
4608 struct constructor_stack *p;
4609 tree value = NULL_TREE;
4611 /* If we've exhausted any levels that didn't have braces,
4612 pop them now. */
4613 while (constructor_stack->implicit)
4615 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4616 || TREE_CODE (constructor_type) == UNION_TYPE)
4617 && constructor_fields == 0)
4618 process_init_element (pop_init_level (1));
4619 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4620 && constructor_max_index
4621 && tree_int_cst_lt (constructor_max_index, constructor_index))
4622 process_init_element (pop_init_level (1));
4623 else
4624 break;
4627 /* Unless this is an explicit brace, we need to preserve previous
4628 content if any. */
4629 if (implicit)
4631 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4632 || TREE_CODE (constructor_type) == UNION_TYPE)
4633 && constructor_fields)
4634 value = find_init_member (constructor_fields);
4635 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4636 value = find_init_member (constructor_index);
4639 p = xmalloc (sizeof (struct constructor_stack));
4640 p->type = constructor_type;
4641 p->fields = constructor_fields;
4642 p->index = constructor_index;
4643 p->max_index = constructor_max_index;
4644 p->unfilled_index = constructor_unfilled_index;
4645 p->unfilled_fields = constructor_unfilled_fields;
4646 p->bit_index = constructor_bit_index;
4647 p->elements = constructor_elements;
4648 p->constant = constructor_constant;
4649 p->simple = constructor_simple;
4650 p->erroneous = constructor_erroneous;
4651 p->pending_elts = constructor_pending_elts;
4652 p->depth = constructor_depth;
4653 p->replacement_value = 0;
4654 p->implicit = implicit;
4655 p->outer = 0;
4656 p->incremental = constructor_incremental;
4657 p->designated = constructor_designated;
4658 p->next = constructor_stack;
4659 p->range_stack = 0;
4660 constructor_stack = p;
4662 constructor_constant = 1;
4663 constructor_simple = 1;
4664 constructor_depth = SPELLING_DEPTH ();
4665 constructor_elements = 0;
4666 constructor_incremental = 1;
4667 constructor_designated = 0;
4668 constructor_pending_elts = 0;
4669 if (!implicit)
4671 p->range_stack = constructor_range_stack;
4672 constructor_range_stack = 0;
4673 designator_depth = 0;
4674 designator_errorneous = 0;
4677 /* Don't die if an entire brace-pair level is superfluous
4678 in the containing level. */
4679 if (constructor_type == 0)
4681 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4682 || TREE_CODE (constructor_type) == UNION_TYPE)
4684 /* Don't die if there are extra init elts at the end. */
4685 if (constructor_fields == 0)
4686 constructor_type = 0;
4687 else
4689 constructor_type = TREE_TYPE (constructor_fields);
4690 push_member_name (constructor_fields);
4691 constructor_depth++;
4694 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4696 constructor_type = TREE_TYPE (constructor_type);
4697 push_array_bounds (tree_low_cst (constructor_index, 0));
4698 constructor_depth++;
4701 if (constructor_type == 0)
4703 error_init ("extra brace group at end of initializer");
4704 constructor_fields = 0;
4705 constructor_unfilled_fields = 0;
4706 return;
4709 if (value && TREE_CODE (value) == CONSTRUCTOR)
4711 constructor_constant = TREE_CONSTANT (value);
4712 constructor_simple = TREE_STATIC (value);
4713 constructor_elements = CONSTRUCTOR_ELTS (value);
4714 if (constructor_elements
4715 && (TREE_CODE (constructor_type) == RECORD_TYPE
4716 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4717 set_nonincremental_init ();
4720 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4722 missing_braces_mentioned = 1;
4723 warning_init ("missing braces around initializer");
4726 if (TREE_CODE (constructor_type) == RECORD_TYPE
4727 || TREE_CODE (constructor_type) == UNION_TYPE)
4729 constructor_fields = TYPE_FIELDS (constructor_type);
4730 /* Skip any nameless bit fields at the beginning. */
4731 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4732 && DECL_NAME (constructor_fields) == 0)
4733 constructor_fields = TREE_CHAIN (constructor_fields);
4735 constructor_unfilled_fields = constructor_fields;
4736 constructor_bit_index = bitsize_zero_node;
4738 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4740 /* Vectors are like simple fixed-size arrays. */
4741 constructor_max_index =
4742 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4743 constructor_index = convert (bitsizetype, integer_zero_node);
4744 constructor_unfilled_index = constructor_index;
4746 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4748 if (TYPE_DOMAIN (constructor_type))
4750 constructor_max_index
4751 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4753 /* Detect non-empty initializations of zero-length arrays. */
4754 if (constructor_max_index == NULL_TREE
4755 && TYPE_SIZE (constructor_type))
4756 constructor_max_index = build_int_2 (-1, -1);
4758 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4759 to initialize VLAs will cause a proper error; avoid tree
4760 checking errors as well by setting a safe value. */
4761 if (constructor_max_index
4762 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4763 constructor_max_index = build_int_2 (-1, -1);
4765 constructor_index
4766 = convert (bitsizetype,
4767 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4769 else
4770 constructor_index = bitsize_zero_node;
4772 constructor_unfilled_index = constructor_index;
4773 if (value && TREE_CODE (value) == STRING_CST)
4775 /* We need to split the char/wchar array into individual
4776 characters, so that we don't have to special case it
4777 everywhere. */
4778 set_nonincremental_init_from_string (value);
4781 else
4783 warning_init ("braces around scalar initializer");
4784 constructor_fields = constructor_type;
4785 constructor_unfilled_fields = constructor_type;
4789 /* At the end of an implicit or explicit brace level,
4790 finish up that level of constructor.
4791 If we were outputting the elements as they are read, return 0
4792 from inner levels (process_init_element ignores that),
4793 but return error_mark_node from the outermost level
4794 (that's what we want to put in DECL_INITIAL).
4795 Otherwise, return a CONSTRUCTOR expression. */
4797 tree
4798 pop_init_level (int implicit)
4800 struct constructor_stack *p;
4801 tree constructor = 0;
4803 if (implicit == 0)
4805 /* When we come to an explicit close brace,
4806 pop any inner levels that didn't have explicit braces. */
4807 while (constructor_stack->implicit)
4808 process_init_element (pop_init_level (1));
4810 if (constructor_range_stack)
4811 abort ();
4814 p = constructor_stack;
4816 /* Error for initializing a flexible array member, or a zero-length
4817 array member in an inappropriate context. */
4818 if (constructor_type && constructor_fields
4819 && TREE_CODE (constructor_type) == ARRAY_TYPE
4820 && TYPE_DOMAIN (constructor_type)
4821 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4823 /* Silently discard empty initializations. The parser will
4824 already have pedwarned for empty brackets. */
4825 if (integer_zerop (constructor_unfilled_index))
4826 constructor_type = NULL_TREE;
4827 else if (! TYPE_SIZE (constructor_type))
4829 if (constructor_depth > 2)
4830 error_init ("initialization of flexible array member in a nested context");
4831 else if (pedantic)
4832 pedwarn_init ("initialization of a flexible array member");
4834 /* We have already issued an error message for the existence
4835 of a flexible array member not at the end of the structure.
4836 Discard the initializer so that we do not abort later. */
4837 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4838 constructor_type = NULL_TREE;
4840 else
4841 /* Zero-length arrays are no longer special, so we should no longer
4842 get here. */
4843 abort ();
4846 /* Warn when some struct elements are implicitly initialized to zero. */
4847 if (extra_warnings
4848 && constructor_type
4849 && TREE_CODE (constructor_type) == RECORD_TYPE
4850 && constructor_unfilled_fields)
4852 /* Do not warn for flexible array members or zero-length arrays. */
4853 while (constructor_unfilled_fields
4854 && (! DECL_SIZE (constructor_unfilled_fields)
4855 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4856 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4858 /* Do not warn if this level of the initializer uses member
4859 designators; it is likely to be deliberate. */
4860 if (constructor_unfilled_fields && !constructor_designated)
4862 push_member_name (constructor_unfilled_fields);
4863 warning_init ("missing initializer");
4864 RESTORE_SPELLING_DEPTH (constructor_depth);
4868 /* Now output all pending elements. */
4869 constructor_incremental = 1;
4870 output_pending_init_elements (1);
4872 /* Pad out the end of the structure. */
4873 if (p->replacement_value)
4874 /* If this closes a superfluous brace pair,
4875 just pass out the element between them. */
4876 constructor = p->replacement_value;
4877 else if (constructor_type == 0)
4879 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4880 && TREE_CODE (constructor_type) != UNION_TYPE
4881 && TREE_CODE (constructor_type) != ARRAY_TYPE
4882 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4884 /* A nonincremental scalar initializer--just return
4885 the element, after verifying there is just one. */
4886 if (constructor_elements == 0)
4888 if (!constructor_erroneous)
4889 error_init ("empty scalar initializer");
4890 constructor = error_mark_node;
4892 else if (TREE_CHAIN (constructor_elements) != 0)
4894 error_init ("extra elements in scalar initializer");
4895 constructor = TREE_VALUE (constructor_elements);
4897 else
4898 constructor = TREE_VALUE (constructor_elements);
4900 else
4902 if (constructor_erroneous)
4903 constructor = error_mark_node;
4904 else
4906 constructor = build_constructor (constructor_type,
4907 nreverse (constructor_elements));
4908 if (constructor_constant)
4909 TREE_CONSTANT (constructor) = 1;
4910 if (constructor_constant && constructor_simple)
4911 TREE_STATIC (constructor) = 1;
4915 constructor_type = p->type;
4916 constructor_fields = p->fields;
4917 constructor_index = p->index;
4918 constructor_max_index = p->max_index;
4919 constructor_unfilled_index = p->unfilled_index;
4920 constructor_unfilled_fields = p->unfilled_fields;
4921 constructor_bit_index = p->bit_index;
4922 constructor_elements = p->elements;
4923 constructor_constant = p->constant;
4924 constructor_simple = p->simple;
4925 constructor_erroneous = p->erroneous;
4926 constructor_incremental = p->incremental;
4927 constructor_designated = p->designated;
4928 constructor_pending_elts = p->pending_elts;
4929 constructor_depth = p->depth;
4930 if (!p->implicit)
4931 constructor_range_stack = p->range_stack;
4932 RESTORE_SPELLING_DEPTH (constructor_depth);
4934 constructor_stack = p->next;
4935 free (p);
4937 if (constructor == 0)
4939 if (constructor_stack == 0)
4940 return error_mark_node;
4941 return NULL_TREE;
4943 return constructor;
4946 /* Common handling for both array range and field name designators.
4947 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4949 static int
4950 set_designator (int array)
4952 tree subtype;
4953 enum tree_code subcode;
4955 /* Don't die if an entire brace-pair level is superfluous
4956 in the containing level. */
4957 if (constructor_type == 0)
4958 return 1;
4960 /* If there were errors in this designator list already, bail out silently. */
4961 if (designator_errorneous)
4962 return 1;
4964 if (!designator_depth)
4966 if (constructor_range_stack)
4967 abort ();
4969 /* Designator list starts at the level of closest explicit
4970 braces. */
4971 while (constructor_stack->implicit)
4972 process_init_element (pop_init_level (1));
4973 constructor_designated = 1;
4974 return 0;
4977 if (constructor_no_implicit)
4979 error_init ("initialization designators may not nest");
4980 return 1;
4983 if (TREE_CODE (constructor_type) == RECORD_TYPE
4984 || TREE_CODE (constructor_type) == UNION_TYPE)
4986 subtype = TREE_TYPE (constructor_fields);
4987 if (subtype != error_mark_node)
4988 subtype = TYPE_MAIN_VARIANT (subtype);
4990 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4992 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4994 else
4995 abort ();
4997 subcode = TREE_CODE (subtype);
4998 if (array && subcode != ARRAY_TYPE)
5000 error_init ("array index in non-array initializer");
5001 return 1;
5003 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5005 error_init ("field name not in record or union initializer");
5006 return 1;
5009 constructor_designated = 1;
5010 push_init_level (2);
5011 return 0;
5014 /* If there are range designators in designator list, push a new designator
5015 to constructor_range_stack. RANGE_END is end of such stack range or
5016 NULL_TREE if there is no range designator at this level. */
5018 static void
5019 push_range_stack (tree range_end)
5021 struct constructor_range_stack *p;
5023 p = ggc_alloc (sizeof (struct constructor_range_stack));
5024 p->prev = constructor_range_stack;
5025 p->next = 0;
5026 p->fields = constructor_fields;
5027 p->range_start = constructor_index;
5028 p->index = constructor_index;
5029 p->stack = constructor_stack;
5030 p->range_end = range_end;
5031 if (constructor_range_stack)
5032 constructor_range_stack->next = p;
5033 constructor_range_stack = p;
5036 /* Within an array initializer, specify the next index to be initialized.
5037 FIRST is that index. If LAST is nonzero, then initialize a range
5038 of indices, running from FIRST through LAST. */
5040 void
5041 set_init_index (tree first, tree last)
5043 if (set_designator (1))
5044 return;
5046 designator_errorneous = 1;
5048 while ((TREE_CODE (first) == NOP_EXPR
5049 || TREE_CODE (first) == CONVERT_EXPR
5050 || TREE_CODE (first) == NON_LVALUE_EXPR)
5051 && (TYPE_MODE (TREE_TYPE (first))
5052 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5053 first = TREE_OPERAND (first, 0);
5055 if (last)
5056 while ((TREE_CODE (last) == NOP_EXPR
5057 || TREE_CODE (last) == CONVERT_EXPR
5058 || TREE_CODE (last) == NON_LVALUE_EXPR)
5059 && (TYPE_MODE (TREE_TYPE (last))
5060 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5061 last = TREE_OPERAND (last, 0);
5063 if (TREE_CODE (first) != INTEGER_CST)
5064 error_init ("nonconstant array index in initializer");
5065 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5066 error_init ("nonconstant array index in initializer");
5067 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5068 error_init ("array index in non-array initializer");
5069 else if (tree_int_cst_sgn (first) == -1)
5070 error_init ("array index in initializer exceeds array bounds");
5071 else if (constructor_max_index
5072 && tree_int_cst_lt (constructor_max_index, first))
5073 error_init ("array index in initializer exceeds array bounds");
5074 else
5076 constructor_index = convert (bitsizetype, first);
5078 if (last)
5080 if (tree_int_cst_equal (first, last))
5081 last = 0;
5082 else if (tree_int_cst_lt (last, first))
5084 error_init ("empty index range in initializer");
5085 last = 0;
5087 else
5089 last = convert (bitsizetype, last);
5090 if (constructor_max_index != 0
5091 && tree_int_cst_lt (constructor_max_index, last))
5093 error_init ("array index range in initializer exceeds array bounds");
5094 last = 0;
5099 designator_depth++;
5100 designator_errorneous = 0;
5101 if (constructor_range_stack || last)
5102 push_range_stack (last);
5106 /* Within a struct initializer, specify the next field to be initialized. */
5108 void
5109 set_init_label (tree fieldname)
5111 tree tail;
5113 if (set_designator (0))
5114 return;
5116 designator_errorneous = 1;
5118 if (TREE_CODE (constructor_type) != RECORD_TYPE
5119 && TREE_CODE (constructor_type) != UNION_TYPE)
5121 error_init ("field name not in record or union initializer");
5122 return;
5125 for (tail = TYPE_FIELDS (constructor_type); tail;
5126 tail = TREE_CHAIN (tail))
5128 if (DECL_NAME (tail) == fieldname)
5129 break;
5132 if (tail == 0)
5133 error ("unknown field `%s' specified in initializer",
5134 IDENTIFIER_POINTER (fieldname));
5135 else
5137 constructor_fields = tail;
5138 designator_depth++;
5139 designator_errorneous = 0;
5140 if (constructor_range_stack)
5141 push_range_stack (NULL_TREE);
5145 /* Add a new initializer to the tree of pending initializers. PURPOSE
5146 identifies the initializer, either array index or field in a structure.
5147 VALUE is the value of that index or field. */
5149 static void
5150 add_pending_init (tree purpose, tree value)
5152 struct init_node *p, **q, *r;
5154 q = &constructor_pending_elts;
5155 p = 0;
5157 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5159 while (*q != 0)
5161 p = *q;
5162 if (tree_int_cst_lt (purpose, p->purpose))
5163 q = &p->left;
5164 else if (tree_int_cst_lt (p->purpose, purpose))
5165 q = &p->right;
5166 else
5168 if (TREE_SIDE_EFFECTS (p->value))
5169 warning_init ("initialized field with side-effects overwritten");
5170 p->value = value;
5171 return;
5175 else
5177 tree bitpos;
5179 bitpos = bit_position (purpose);
5180 while (*q != NULL)
5182 p = *q;
5183 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5184 q = &p->left;
5185 else if (p->purpose != purpose)
5186 q = &p->right;
5187 else
5189 if (TREE_SIDE_EFFECTS (p->value))
5190 warning_init ("initialized field with side-effects overwritten");
5191 p->value = value;
5192 return;
5197 r = ggc_alloc (sizeof (struct init_node));
5198 r->purpose = purpose;
5199 r->value = value;
5201 *q = r;
5202 r->parent = p;
5203 r->left = 0;
5204 r->right = 0;
5205 r->balance = 0;
5207 while (p)
5209 struct init_node *s;
5211 if (r == p->left)
5213 if (p->balance == 0)
5214 p->balance = -1;
5215 else if (p->balance < 0)
5217 if (r->balance < 0)
5219 /* L rotation. */
5220 p->left = r->right;
5221 if (p->left)
5222 p->left->parent = p;
5223 r->right = p;
5225 p->balance = 0;
5226 r->balance = 0;
5228 s = p->parent;
5229 p->parent = r;
5230 r->parent = s;
5231 if (s)
5233 if (s->left == p)
5234 s->left = r;
5235 else
5236 s->right = r;
5238 else
5239 constructor_pending_elts = r;
5241 else
5243 /* LR rotation. */
5244 struct init_node *t = r->right;
5246 r->right = t->left;
5247 if (r->right)
5248 r->right->parent = r;
5249 t->left = r;
5251 p->left = t->right;
5252 if (p->left)
5253 p->left->parent = p;
5254 t->right = p;
5256 p->balance = t->balance < 0;
5257 r->balance = -(t->balance > 0);
5258 t->balance = 0;
5260 s = p->parent;
5261 p->parent = t;
5262 r->parent = t;
5263 t->parent = s;
5264 if (s)
5266 if (s->left == p)
5267 s->left = t;
5268 else
5269 s->right = t;
5271 else
5272 constructor_pending_elts = t;
5274 break;
5276 else
5278 /* p->balance == +1; growth of left side balances the node. */
5279 p->balance = 0;
5280 break;
5283 else /* r == p->right */
5285 if (p->balance == 0)
5286 /* Growth propagation from right side. */
5287 p->balance++;
5288 else if (p->balance > 0)
5290 if (r->balance > 0)
5292 /* R rotation. */
5293 p->right = r->left;
5294 if (p->right)
5295 p->right->parent = p;
5296 r->left = p;
5298 p->balance = 0;
5299 r->balance = 0;
5301 s = p->parent;
5302 p->parent = r;
5303 r->parent = s;
5304 if (s)
5306 if (s->left == p)
5307 s->left = r;
5308 else
5309 s->right = r;
5311 else
5312 constructor_pending_elts = r;
5314 else /* r->balance == -1 */
5316 /* RL rotation */
5317 struct init_node *t = r->left;
5319 r->left = t->right;
5320 if (r->left)
5321 r->left->parent = r;
5322 t->right = r;
5324 p->right = t->left;
5325 if (p->right)
5326 p->right->parent = p;
5327 t->left = p;
5329 r->balance = (t->balance < 0);
5330 p->balance = -(t->balance > 0);
5331 t->balance = 0;
5333 s = p->parent;
5334 p->parent = t;
5335 r->parent = t;
5336 t->parent = s;
5337 if (s)
5339 if (s->left == p)
5340 s->left = t;
5341 else
5342 s->right = t;
5344 else
5345 constructor_pending_elts = t;
5347 break;
5349 else
5351 /* p->balance == -1; growth of right side balances the node. */
5352 p->balance = 0;
5353 break;
5357 r = p;
5358 p = p->parent;
5362 /* Build AVL tree from a sorted chain. */
5364 static void
5365 set_nonincremental_init (void)
5367 tree chain;
5369 if (TREE_CODE (constructor_type) != RECORD_TYPE
5370 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5371 return;
5373 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5374 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5375 constructor_elements = 0;
5376 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5378 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5379 /* Skip any nameless bit fields at the beginning. */
5380 while (constructor_unfilled_fields != 0
5381 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5382 && DECL_NAME (constructor_unfilled_fields) == 0)
5383 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5386 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5388 if (TYPE_DOMAIN (constructor_type))
5389 constructor_unfilled_index
5390 = convert (bitsizetype,
5391 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5392 else
5393 constructor_unfilled_index = bitsize_zero_node;
5395 constructor_incremental = 0;
5398 /* Build AVL tree from a string constant. */
5400 static void
5401 set_nonincremental_init_from_string (tree str)
5403 tree value, purpose, type;
5404 HOST_WIDE_INT val[2];
5405 const char *p, *end;
5406 int byte, wchar_bytes, charwidth, bitpos;
5408 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5409 abort ();
5411 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5412 == TYPE_PRECISION (char_type_node))
5413 wchar_bytes = 1;
5414 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5415 == TYPE_PRECISION (wchar_type_node))
5416 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5417 else
5418 abort ();
5420 charwidth = TYPE_PRECISION (char_type_node);
5421 type = TREE_TYPE (constructor_type);
5422 p = TREE_STRING_POINTER (str);
5423 end = p + TREE_STRING_LENGTH (str);
5425 for (purpose = bitsize_zero_node;
5426 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5427 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5429 if (wchar_bytes == 1)
5431 val[1] = (unsigned char) *p++;
5432 val[0] = 0;
5434 else
5436 val[0] = 0;
5437 val[1] = 0;
5438 for (byte = 0; byte < wchar_bytes; byte++)
5440 if (BYTES_BIG_ENDIAN)
5441 bitpos = (wchar_bytes - byte - 1) * charwidth;
5442 else
5443 bitpos = byte * charwidth;
5444 val[bitpos < HOST_BITS_PER_WIDE_INT]
5445 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5446 << (bitpos % HOST_BITS_PER_WIDE_INT);
5450 if (!TREE_UNSIGNED (type))
5452 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5453 if (bitpos < HOST_BITS_PER_WIDE_INT)
5455 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5457 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5458 val[0] = -1;
5461 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5463 if (val[1] < 0)
5464 val[0] = -1;
5466 else if (val[0] & (((HOST_WIDE_INT) 1)
5467 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5468 val[0] |= ((HOST_WIDE_INT) -1)
5469 << (bitpos - HOST_BITS_PER_WIDE_INT);
5472 value = build_int_2 (val[1], val[0]);
5473 TREE_TYPE (value) = type;
5474 add_pending_init (purpose, value);
5477 constructor_incremental = 0;
5480 /* Return value of FIELD in pending initializer or zero if the field was
5481 not initialized yet. */
5483 static tree
5484 find_init_member (tree field)
5486 struct init_node *p;
5488 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5490 if (constructor_incremental
5491 && tree_int_cst_lt (field, constructor_unfilled_index))
5492 set_nonincremental_init ();
5494 p = constructor_pending_elts;
5495 while (p)
5497 if (tree_int_cst_lt (field, p->purpose))
5498 p = p->left;
5499 else if (tree_int_cst_lt (p->purpose, field))
5500 p = p->right;
5501 else
5502 return p->value;
5505 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5507 tree bitpos = bit_position (field);
5509 if (constructor_incremental
5510 && (!constructor_unfilled_fields
5511 || tree_int_cst_lt (bitpos,
5512 bit_position (constructor_unfilled_fields))))
5513 set_nonincremental_init ();
5515 p = constructor_pending_elts;
5516 while (p)
5518 if (field == p->purpose)
5519 return p->value;
5520 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5521 p = p->left;
5522 else
5523 p = p->right;
5526 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5528 if (constructor_elements
5529 && TREE_PURPOSE (constructor_elements) == field)
5530 return TREE_VALUE (constructor_elements);
5532 return 0;
5535 /* "Output" the next constructor element.
5536 At top level, really output it to assembler code now.
5537 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5538 TYPE is the data type that the containing data type wants here.
5539 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5541 PENDING if non-nil means output pending elements that belong
5542 right after this element. (PENDING is normally 1;
5543 it is 0 while outputting pending elements, to avoid recursion.) */
5545 static void
5546 output_init_element (tree value, tree type, tree field, int pending)
5548 if (type == error_mark_node)
5550 constructor_erroneous = 1;
5551 return;
5553 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5554 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5555 && !(TREE_CODE (value) == STRING_CST
5556 && TREE_CODE (type) == ARRAY_TYPE
5557 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5558 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5559 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5560 value = default_conversion (value);
5562 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5563 && require_constant_value && !flag_isoc99 && pending)
5565 /* As an extension, allow initializing objects with static storage
5566 duration with compound literals (which are then treated just as
5567 the brace enclosed list they contain). */
5568 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5569 value = DECL_INITIAL (decl);
5572 if (value == error_mark_node)
5573 constructor_erroneous = 1;
5574 else if (!TREE_CONSTANT (value))
5575 constructor_constant = 0;
5576 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5577 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5578 || TREE_CODE (constructor_type) == UNION_TYPE)
5579 && DECL_C_BIT_FIELD (field)
5580 && TREE_CODE (value) != INTEGER_CST))
5581 constructor_simple = 0;
5583 if (require_constant_value && ! TREE_CONSTANT (value))
5585 error_init ("initializer element is not constant");
5586 value = error_mark_node;
5588 else if (require_constant_elements
5589 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5590 pedwarn ("initializer element is not computable at load time");
5592 /* If this field is empty (and not at the end of structure),
5593 don't do anything other than checking the initializer. */
5594 if (field
5595 && (TREE_TYPE (field) == error_mark_node
5596 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5597 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5598 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5599 || TREE_CHAIN (field)))))
5600 return;
5602 value = digest_init (type, value, require_constant_value);
5603 if (value == error_mark_node)
5605 constructor_erroneous = 1;
5606 return;
5609 /* If this element doesn't come next in sequence,
5610 put it on constructor_pending_elts. */
5611 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5612 && (!constructor_incremental
5613 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5615 if (constructor_incremental
5616 && tree_int_cst_lt (field, constructor_unfilled_index))
5617 set_nonincremental_init ();
5619 add_pending_init (field, value);
5620 return;
5622 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5623 && (!constructor_incremental
5624 || field != constructor_unfilled_fields))
5626 /* We do this for records but not for unions. In a union,
5627 no matter which field is specified, it can be initialized
5628 right away since it starts at the beginning of the union. */
5629 if (constructor_incremental)
5631 if (!constructor_unfilled_fields)
5632 set_nonincremental_init ();
5633 else
5635 tree bitpos, unfillpos;
5637 bitpos = bit_position (field);
5638 unfillpos = bit_position (constructor_unfilled_fields);
5640 if (tree_int_cst_lt (bitpos, unfillpos))
5641 set_nonincremental_init ();
5645 add_pending_init (field, value);
5646 return;
5648 else if (TREE_CODE (constructor_type) == UNION_TYPE
5649 && constructor_elements)
5651 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5652 warning_init ("initialized field with side-effects overwritten");
5654 /* We can have just one union field set. */
5655 constructor_elements = 0;
5658 /* Otherwise, output this element either to
5659 constructor_elements or to the assembler file. */
5661 if (field && TREE_CODE (field) == INTEGER_CST)
5662 field = copy_node (field);
5663 constructor_elements
5664 = tree_cons (field, value, constructor_elements);
5666 /* Advance the variable that indicates sequential elements output. */
5667 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5668 constructor_unfilled_index
5669 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5670 bitsize_one_node);
5671 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5673 constructor_unfilled_fields
5674 = TREE_CHAIN (constructor_unfilled_fields);
5676 /* Skip any nameless bit fields. */
5677 while (constructor_unfilled_fields != 0
5678 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5679 && DECL_NAME (constructor_unfilled_fields) == 0)
5680 constructor_unfilled_fields =
5681 TREE_CHAIN (constructor_unfilled_fields);
5683 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5684 constructor_unfilled_fields = 0;
5686 /* Now output any pending elements which have become next. */
5687 if (pending)
5688 output_pending_init_elements (0);
5691 /* Output any pending elements which have become next.
5692 As we output elements, constructor_unfilled_{fields,index}
5693 advances, which may cause other elements to become next;
5694 if so, they too are output.
5696 If ALL is 0, we return when there are
5697 no more pending elements to output now.
5699 If ALL is 1, we output space as necessary so that
5700 we can output all the pending elements. */
5702 static void
5703 output_pending_init_elements (int all)
5705 struct init_node *elt = constructor_pending_elts;
5706 tree next;
5708 retry:
5710 /* Look through the whole pending tree.
5711 If we find an element that should be output now,
5712 output it. Otherwise, set NEXT to the element
5713 that comes first among those still pending. */
5715 next = 0;
5716 while (elt)
5718 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5720 if (tree_int_cst_equal (elt->purpose,
5721 constructor_unfilled_index))
5722 output_init_element (elt->value,
5723 TREE_TYPE (constructor_type),
5724 constructor_unfilled_index, 0);
5725 else if (tree_int_cst_lt (constructor_unfilled_index,
5726 elt->purpose))
5728 /* Advance to the next smaller node. */
5729 if (elt->left)
5730 elt = elt->left;
5731 else
5733 /* We have reached the smallest node bigger than the
5734 current unfilled index. Fill the space first. */
5735 next = elt->purpose;
5736 break;
5739 else
5741 /* Advance to the next bigger node. */
5742 if (elt->right)
5743 elt = elt->right;
5744 else
5746 /* We have reached the biggest node in a subtree. Find
5747 the parent of it, which is the next bigger node. */
5748 while (elt->parent && elt->parent->right == elt)
5749 elt = elt->parent;
5750 elt = elt->parent;
5751 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5752 elt->purpose))
5754 next = elt->purpose;
5755 break;
5760 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5761 || TREE_CODE (constructor_type) == UNION_TYPE)
5763 tree ctor_unfilled_bitpos, elt_bitpos;
5765 /* If the current record is complete we are done. */
5766 if (constructor_unfilled_fields == 0)
5767 break;
5769 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5770 elt_bitpos = bit_position (elt->purpose);
5771 /* We can't compare fields here because there might be empty
5772 fields in between. */
5773 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5775 constructor_unfilled_fields = elt->purpose;
5776 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5777 elt->purpose, 0);
5779 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5781 /* Advance to the next smaller node. */
5782 if (elt->left)
5783 elt = elt->left;
5784 else
5786 /* We have reached the smallest node bigger than the
5787 current unfilled field. Fill the space first. */
5788 next = elt->purpose;
5789 break;
5792 else
5794 /* Advance to the next bigger node. */
5795 if (elt->right)
5796 elt = elt->right;
5797 else
5799 /* We have reached the biggest node in a subtree. Find
5800 the parent of it, which is the next bigger node. */
5801 while (elt->parent && elt->parent->right == elt)
5802 elt = elt->parent;
5803 elt = elt->parent;
5804 if (elt
5805 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5806 bit_position (elt->purpose))))
5808 next = elt->purpose;
5809 break;
5816 /* Ordinarily return, but not if we want to output all
5817 and there are elements left. */
5818 if (! (all && next != 0))
5819 return;
5821 /* If it's not incremental, just skip over the gap, so that after
5822 jumping to retry we will output the next successive element. */
5823 if (TREE_CODE (constructor_type) == RECORD_TYPE
5824 || TREE_CODE (constructor_type) == UNION_TYPE)
5825 constructor_unfilled_fields = next;
5826 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5827 constructor_unfilled_index = next;
5829 /* ELT now points to the node in the pending tree with the next
5830 initializer to output. */
5831 goto retry;
5834 /* Add one non-braced element to the current constructor level.
5835 This adjusts the current position within the constructor's type.
5836 This may also start or terminate implicit levels
5837 to handle a partly-braced initializer.
5839 Once this has found the correct level for the new element,
5840 it calls output_init_element. */
5842 void
5843 process_init_element (tree value)
5845 tree orig_value = value;
5846 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5848 designator_depth = 0;
5849 designator_errorneous = 0;
5851 /* Handle superfluous braces around string cst as in
5852 char x[] = {"foo"}; */
5853 if (string_flag
5854 && constructor_type
5855 && TREE_CODE (constructor_type) == ARRAY_TYPE
5856 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5857 && integer_zerop (constructor_unfilled_index))
5859 if (constructor_stack->replacement_value)
5860 error_init ("excess elements in char array initializer");
5861 constructor_stack->replacement_value = value;
5862 return;
5865 if (constructor_stack->replacement_value != 0)
5867 error_init ("excess elements in struct initializer");
5868 return;
5871 /* Ignore elements of a brace group if it is entirely superfluous
5872 and has already been diagnosed. */
5873 if (constructor_type == 0)
5874 return;
5876 /* If we've exhausted any levels that didn't have braces,
5877 pop them now. */
5878 while (constructor_stack->implicit)
5880 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5881 || TREE_CODE (constructor_type) == UNION_TYPE)
5882 && constructor_fields == 0)
5883 process_init_element (pop_init_level (1));
5884 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5885 && (constructor_max_index == 0
5886 || tree_int_cst_lt (constructor_max_index,
5887 constructor_index)))
5888 process_init_element (pop_init_level (1));
5889 else
5890 break;
5893 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5894 if (constructor_range_stack)
5896 /* If value is a compound literal and we'll be just using its
5897 content, don't put it into a SAVE_EXPR. */
5898 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5899 || !require_constant_value
5900 || flag_isoc99)
5901 value = save_expr (value);
5904 while (1)
5906 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5908 tree fieldtype;
5909 enum tree_code fieldcode;
5911 if (constructor_fields == 0)
5913 pedwarn_init ("excess elements in struct initializer");
5914 break;
5917 fieldtype = TREE_TYPE (constructor_fields);
5918 if (fieldtype != error_mark_node)
5919 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5920 fieldcode = TREE_CODE (fieldtype);
5922 /* Error for non-static initialization of a flexible array member. */
5923 if (fieldcode == ARRAY_TYPE
5924 && !require_constant_value
5925 && TYPE_SIZE (fieldtype) == NULL_TREE
5926 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5928 error_init ("non-static initialization of a flexible array member");
5929 break;
5932 /* Accept a string constant to initialize a subarray. */
5933 if (value != 0
5934 && fieldcode == ARRAY_TYPE
5935 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5936 && string_flag)
5937 value = orig_value;
5938 /* Otherwise, if we have come to a subaggregate,
5939 and we don't have an element of its type, push into it. */
5940 else if (value != 0 && !constructor_no_implicit
5941 && value != error_mark_node
5942 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5943 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5944 || fieldcode == UNION_TYPE))
5946 push_init_level (1);
5947 continue;
5950 if (value)
5952 push_member_name (constructor_fields);
5953 output_init_element (value, fieldtype, constructor_fields, 1);
5954 RESTORE_SPELLING_DEPTH (constructor_depth);
5956 else
5957 /* Do the bookkeeping for an element that was
5958 directly output as a constructor. */
5960 /* For a record, keep track of end position of last field. */
5961 if (DECL_SIZE (constructor_fields))
5962 constructor_bit_index
5963 = size_binop (PLUS_EXPR,
5964 bit_position (constructor_fields),
5965 DECL_SIZE (constructor_fields));
5967 /* If the current field was the first one not yet written out,
5968 it isn't now, so update. */
5969 if (constructor_unfilled_fields == constructor_fields)
5971 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5972 /* Skip any nameless bit fields. */
5973 while (constructor_unfilled_fields != 0
5974 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5975 && DECL_NAME (constructor_unfilled_fields) == 0)
5976 constructor_unfilled_fields =
5977 TREE_CHAIN (constructor_unfilled_fields);
5981 constructor_fields = TREE_CHAIN (constructor_fields);
5982 /* Skip any nameless bit fields at the beginning. */
5983 while (constructor_fields != 0
5984 && DECL_C_BIT_FIELD (constructor_fields)
5985 && DECL_NAME (constructor_fields) == 0)
5986 constructor_fields = TREE_CHAIN (constructor_fields);
5988 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5990 tree fieldtype;
5991 enum tree_code fieldcode;
5993 if (constructor_fields == 0)
5995 pedwarn_init ("excess elements in union initializer");
5996 break;
5999 fieldtype = TREE_TYPE (constructor_fields);
6000 if (fieldtype != error_mark_node)
6001 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6002 fieldcode = TREE_CODE (fieldtype);
6004 /* Warn that traditional C rejects initialization of unions.
6005 We skip the warning if the value is zero. This is done
6006 under the assumption that the zero initializer in user
6007 code appears conditioned on e.g. __STDC__ to avoid
6008 "missing initializer" warnings and relies on default
6009 initialization to zero in the traditional C case.
6010 We also skip the warning if the initializer is designated,
6011 again on the assumption that this must be conditional on
6012 __STDC__ anyway (and we've already complained about the
6013 member-designator already). */
6014 if (warn_traditional && !in_system_header && !constructor_designated
6015 && !(value && (integer_zerop (value) || real_zerop (value))))
6016 warning ("traditional C rejects initialization of unions");
6018 /* Accept a string constant to initialize a subarray. */
6019 if (value != 0
6020 && fieldcode == ARRAY_TYPE
6021 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6022 && string_flag)
6023 value = orig_value;
6024 /* Otherwise, if we have come to a subaggregate,
6025 and we don't have an element of its type, push into it. */
6026 else if (value != 0 && !constructor_no_implicit
6027 && value != error_mark_node
6028 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6029 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6030 || fieldcode == UNION_TYPE))
6032 push_init_level (1);
6033 continue;
6036 if (value)
6038 push_member_name (constructor_fields);
6039 output_init_element (value, fieldtype, constructor_fields, 1);
6040 RESTORE_SPELLING_DEPTH (constructor_depth);
6042 else
6043 /* Do the bookkeeping for an element that was
6044 directly output as a constructor. */
6046 constructor_bit_index = DECL_SIZE (constructor_fields);
6047 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6050 constructor_fields = 0;
6052 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6054 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6055 enum tree_code eltcode = TREE_CODE (elttype);
6057 /* Accept a string constant to initialize a subarray. */
6058 if (value != 0
6059 && eltcode == ARRAY_TYPE
6060 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6061 && string_flag)
6062 value = orig_value;
6063 /* Otherwise, if we have come to a subaggregate,
6064 and we don't have an element of its type, push into it. */
6065 else if (value != 0 && !constructor_no_implicit
6066 && value != error_mark_node
6067 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6068 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6069 || eltcode == UNION_TYPE))
6071 push_init_level (1);
6072 continue;
6075 if (constructor_max_index != 0
6076 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6077 || integer_all_onesp (constructor_max_index)))
6079 pedwarn_init ("excess elements in array initializer");
6080 break;
6083 /* Now output the actual element. */
6084 if (value)
6086 push_array_bounds (tree_low_cst (constructor_index, 0));
6087 output_init_element (value, elttype, constructor_index, 1);
6088 RESTORE_SPELLING_DEPTH (constructor_depth);
6091 constructor_index
6092 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6094 if (! value)
6095 /* If we are doing the bookkeeping for an element that was
6096 directly output as a constructor, we must update
6097 constructor_unfilled_index. */
6098 constructor_unfilled_index = constructor_index;
6100 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6102 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6104 /* Do a basic check of initializer size. Note that vectors
6105 always have a fixed size derived from their type. */
6106 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6108 pedwarn_init ("excess elements in vector initializer");
6109 break;
6112 /* Now output the actual element. */
6113 if (value)
6114 output_init_element (value, elttype, constructor_index, 1);
6116 constructor_index
6117 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6119 if (! value)
6120 /* If we are doing the bookkeeping for an element that was
6121 directly output as a constructor, we must update
6122 constructor_unfilled_index. */
6123 constructor_unfilled_index = constructor_index;
6126 /* Handle the sole element allowed in a braced initializer
6127 for a scalar variable. */
6128 else if (constructor_fields == 0)
6130 pedwarn_init ("excess elements in scalar initializer");
6131 break;
6133 else
6135 if (value)
6136 output_init_element (value, constructor_type, NULL_TREE, 1);
6137 constructor_fields = 0;
6140 /* Handle range initializers either at this level or anywhere higher
6141 in the designator stack. */
6142 if (constructor_range_stack)
6144 struct constructor_range_stack *p, *range_stack;
6145 int finish = 0;
6147 range_stack = constructor_range_stack;
6148 constructor_range_stack = 0;
6149 while (constructor_stack != range_stack->stack)
6151 if (!constructor_stack->implicit)
6152 abort ();
6153 process_init_element (pop_init_level (1));
6155 for (p = range_stack;
6156 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6157 p = p->prev)
6159 if (!constructor_stack->implicit)
6160 abort ();
6161 process_init_element (pop_init_level (1));
6164 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6165 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6166 finish = 1;
6168 while (1)
6170 constructor_index = p->index;
6171 constructor_fields = p->fields;
6172 if (finish && p->range_end && p->index == p->range_start)
6174 finish = 0;
6175 p->prev = 0;
6177 p = p->next;
6178 if (!p)
6179 break;
6180 push_init_level (2);
6181 p->stack = constructor_stack;
6182 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6183 p->index = p->range_start;
6186 if (!finish)
6187 constructor_range_stack = range_stack;
6188 continue;
6191 break;
6194 constructor_range_stack = 0;
6197 /* Build a simple asm-statement, from one string literal. */
6198 tree
6199 simple_asm_stmt (tree expr)
6201 STRIP_NOPS (expr);
6203 if (TREE_CODE (expr) == ADDR_EXPR)
6204 expr = TREE_OPERAND (expr, 0);
6206 if (TREE_CODE (expr) == STRING_CST)
6208 tree stmt;
6210 /* Simple asm statements are treated as volatile. */
6211 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6212 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6213 ASM_INPUT_P (stmt) = 1;
6214 return stmt;
6217 error ("argument of `asm' is not a constant string");
6218 return NULL_TREE;
6221 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6222 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6224 tree
6225 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6226 tree clobbers)
6228 tree tail;
6230 if (TREE_CODE (string) != STRING_CST)
6232 error ("asm template is not a string constant");
6233 return NULL_TREE;
6236 if (cv_qualifier != NULL_TREE
6237 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6239 warning ("%s qualifier ignored on asm",
6240 IDENTIFIER_POINTER (cv_qualifier));
6241 cv_qualifier = NULL_TREE;
6244 /* We can remove output conversions that change the type,
6245 but not the mode. */
6246 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6248 tree output = TREE_VALUE (tail);
6250 STRIP_NOPS (output);
6251 TREE_VALUE (tail) = output;
6253 /* Allow conversions as LHS here. build_modify_expr as called below
6254 will do the right thing with them. */
6255 while (TREE_CODE (output) == NOP_EXPR
6256 || TREE_CODE (output) == CONVERT_EXPR
6257 || TREE_CODE (output) == FLOAT_EXPR
6258 || TREE_CODE (output) == FIX_TRUNC_EXPR
6259 || TREE_CODE (output) == FIX_FLOOR_EXPR
6260 || TREE_CODE (output) == FIX_ROUND_EXPR
6261 || TREE_CODE (output) == FIX_CEIL_EXPR)
6262 output = TREE_OPERAND (output, 0);
6264 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6267 /* Remove output conversions that change the type but not the mode. */
6268 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6270 tree output = TREE_VALUE (tail);
6271 STRIP_NOPS (output);
6272 TREE_VALUE (tail) = output;
6275 /* Perform default conversions on array and function inputs.
6276 Don't do this for other types as it would screw up operands
6277 expected to be in memory. */
6278 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6279 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6281 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6282 outputs, inputs, clobbers));
6285 /* Expand an ASM statement with operands, handling output operands
6286 that are not variables or INDIRECT_REFS by transforming such
6287 cases into cases that expand_asm_operands can handle.
6289 Arguments are same as for expand_asm_operands. */
6291 void
6292 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6293 tree clobbers, int vol, location_t locus)
6295 int noutputs = list_length (outputs);
6296 int i;
6297 /* o[I] is the place that output number I should be written. */
6298 tree *o = alloca (noutputs * sizeof (tree));
6299 tree tail;
6301 /* Record the contents of OUTPUTS before it is modified. */
6302 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6304 o[i] = TREE_VALUE (tail);
6305 if (o[i] == error_mark_node)
6306 return;
6309 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6310 OUTPUTS some trees for where the values were actually stored. */
6311 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6313 /* Copy all the intermediate outputs into the specified outputs. */
6314 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6316 if (o[i] != TREE_VALUE (tail))
6318 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6319 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6320 free_temp_slots ();
6322 /* Restore the original value so that it's correct the next
6323 time we expand this function. */
6324 TREE_VALUE (tail) = o[i];
6326 /* Detect modification of read-only values.
6327 (Otherwise done by build_modify_expr.) */
6328 else
6330 tree type = TREE_TYPE (o[i]);
6331 if (TREE_READONLY (o[i])
6332 || TYPE_READONLY (type)
6333 || ((TREE_CODE (type) == RECORD_TYPE
6334 || TREE_CODE (type) == UNION_TYPE)
6335 && C_TYPE_FIELDS_READONLY (type)))
6336 readonly_warning (o[i], "modification by `asm'");
6340 /* Those MODIFY_EXPRs could do autoincrements. */
6341 emit_queue ();
6344 /* Expand a C `return' statement.
6345 RETVAL is the expression for what to return,
6346 or a null pointer for `return;' with no value. */
6348 tree
6349 c_expand_return (tree retval)
6351 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6353 if (TREE_THIS_VOLATILE (current_function_decl))
6354 warning ("function declared `noreturn' has a `return' statement");
6356 if (!retval)
6358 current_function_returns_null = 1;
6359 if ((warn_return_type || flag_isoc99)
6360 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6361 pedwarn_c99 ("`return' with no value, in function returning non-void");
6363 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6365 current_function_returns_null = 1;
6366 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6367 pedwarn ("`return' with a value, in function returning void");
6369 else
6371 tree t = convert_for_assignment (valtype, retval, _("return"),
6372 NULL_TREE, NULL_TREE, 0);
6373 tree res = DECL_RESULT (current_function_decl);
6374 tree inner;
6376 current_function_returns_value = 1;
6377 if (t == error_mark_node)
6378 return NULL_TREE;
6380 inner = t = convert (TREE_TYPE (res), t);
6382 /* Strip any conversions, additions, and subtractions, and see if
6383 we are returning the address of a local variable. Warn if so. */
6384 while (1)
6386 switch (TREE_CODE (inner))
6388 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6389 case PLUS_EXPR:
6390 inner = TREE_OPERAND (inner, 0);
6391 continue;
6393 case MINUS_EXPR:
6394 /* If the second operand of the MINUS_EXPR has a pointer
6395 type (or is converted from it), this may be valid, so
6396 don't give a warning. */
6398 tree op1 = TREE_OPERAND (inner, 1);
6400 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6401 && (TREE_CODE (op1) == NOP_EXPR
6402 || TREE_CODE (op1) == NON_LVALUE_EXPR
6403 || TREE_CODE (op1) == CONVERT_EXPR))
6404 op1 = TREE_OPERAND (op1, 0);
6406 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6407 break;
6409 inner = TREE_OPERAND (inner, 0);
6410 continue;
6413 case ADDR_EXPR:
6414 inner = TREE_OPERAND (inner, 0);
6416 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6417 inner = TREE_OPERAND (inner, 0);
6419 if (TREE_CODE (inner) == VAR_DECL
6420 && ! DECL_EXTERNAL (inner)
6421 && ! TREE_STATIC (inner)
6422 && DECL_CONTEXT (inner) == current_function_decl)
6423 warning ("function returns address of local variable");
6424 break;
6426 default:
6427 break;
6430 break;
6433 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6436 return add_stmt (build_return_stmt (retval));
6439 struct c_switch {
6440 /* The SWITCH_STMT being built. */
6441 tree switch_stmt;
6442 /* A splay-tree mapping the low element of a case range to the high
6443 element, or NULL_TREE if there is no high element. Used to
6444 determine whether or not a new case label duplicates an old case
6445 label. We need a tree, rather than simply a hash table, because
6446 of the GNU case range extension. */
6447 splay_tree cases;
6448 /* The next node on the stack. */
6449 struct c_switch *next;
6452 /* A stack of the currently active switch statements. The innermost
6453 switch statement is on the top of the stack. There is no need to
6454 mark the stack for garbage collection because it is only active
6455 during the processing of the body of a function, and we never
6456 collect at that point. */
6458 static struct c_switch *switch_stack;
6460 /* Start a C switch statement, testing expression EXP. Return the new
6461 SWITCH_STMT. */
6463 tree
6464 c_start_case (tree exp)
6466 enum tree_code code;
6467 tree type, orig_type = error_mark_node;
6468 struct c_switch *cs;
6470 if (exp != error_mark_node)
6472 code = TREE_CODE (TREE_TYPE (exp));
6473 orig_type = TREE_TYPE (exp);
6475 if (! INTEGRAL_TYPE_P (orig_type)
6476 && code != ERROR_MARK)
6478 error ("switch quantity not an integer");
6479 exp = integer_zero_node;
6481 else
6483 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6485 if (warn_traditional && !in_system_header
6486 && (type == long_integer_type_node
6487 || type == long_unsigned_type_node))
6488 warning ("`long' switch expression not converted to `int' in ISO C");
6490 exp = default_conversion (exp);
6491 type = TREE_TYPE (exp);
6495 /* Add this new SWITCH_STMT to the stack. */
6496 cs = xmalloc (sizeof (*cs));
6497 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6498 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6499 cs->next = switch_stack;
6500 switch_stack = cs;
6502 return add_stmt (switch_stack->switch_stmt);
6505 /* Process a case label. */
6507 tree
6508 do_case (tree low_value, tree high_value)
6510 tree label = NULL_TREE;
6512 if (switch_stack)
6514 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6516 label = c_add_case_label (switch_stack->cases,
6517 SWITCH_COND (switch_stack->switch_stmt),
6518 low_value, high_value);
6519 if (label == error_mark_node)
6520 label = NULL_TREE;
6521 else if (switch_was_empty_p)
6523 /* Attach the first case label to the SWITCH_BODY. */
6524 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6525 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6528 else if (low_value)
6529 error ("case label not within a switch statement");
6530 else
6531 error ("`default' label not within a switch statement");
6533 return label;
6536 /* Finish the switch statement. */
6538 void
6539 c_finish_case (void)
6541 struct c_switch *cs = switch_stack;
6543 /* Rechain the next statements to the SWITCH_STMT. */
6544 last_tree = cs->switch_stmt;
6546 /* Pop the stack. */
6547 switch_stack = switch_stack->next;
6548 splay_tree_delete (cs->cases);
6549 free (cs);
6552 /* Build a binary-operation expression without default conversions.
6553 CODE is the kind of expression to build.
6554 This function differs from `build' in several ways:
6555 the data type of the result is computed and recorded in it,
6556 warnings are generated if arg data types are invalid,
6557 special handling for addition and subtraction of pointers is known,
6558 and some optimization is done (operations on narrow ints
6559 are done in the narrower type when that gives the same result).
6560 Constant folding is also done before the result is returned.
6562 Note that the operands will never have enumeral types, or function
6563 or array types, because either they will have the default conversions
6564 performed or they have both just been converted to some other type in which
6565 the arithmetic is to be done. */
6567 tree
6568 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6569 int convert_p)
6571 tree type0, type1;
6572 enum tree_code code0, code1;
6573 tree op0, op1;
6575 /* Expression code to give to the expression when it is built.
6576 Normally this is CODE, which is what the caller asked for,
6577 but in some special cases we change it. */
6578 enum tree_code resultcode = code;
6580 /* Data type in which the computation is to be performed.
6581 In the simplest cases this is the common type of the arguments. */
6582 tree result_type = NULL;
6584 /* Nonzero means operands have already been type-converted
6585 in whatever way is necessary.
6586 Zero means they need to be converted to RESULT_TYPE. */
6587 int converted = 0;
6589 /* Nonzero means create the expression with this type, rather than
6590 RESULT_TYPE. */
6591 tree build_type = 0;
6593 /* Nonzero means after finally constructing the expression
6594 convert it to this type. */
6595 tree final_type = 0;
6597 /* Nonzero if this is an operation like MIN or MAX which can
6598 safely be computed in short if both args are promoted shorts.
6599 Also implies COMMON.
6600 -1 indicates a bitwise operation; this makes a difference
6601 in the exact conditions for when it is safe to do the operation
6602 in a narrower mode. */
6603 int shorten = 0;
6605 /* Nonzero if this is a comparison operation;
6606 if both args are promoted shorts, compare the original shorts.
6607 Also implies COMMON. */
6608 int short_compare = 0;
6610 /* Nonzero if this is a right-shift operation, which can be computed on the
6611 original short and then promoted if the operand is a promoted short. */
6612 int short_shift = 0;
6614 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6615 int common = 0;
6617 if (convert_p)
6619 op0 = default_conversion (orig_op0);
6620 op1 = default_conversion (orig_op1);
6622 else
6624 op0 = orig_op0;
6625 op1 = orig_op1;
6628 type0 = TREE_TYPE (op0);
6629 type1 = TREE_TYPE (op1);
6631 /* The expression codes of the data types of the arguments tell us
6632 whether the arguments are integers, floating, pointers, etc. */
6633 code0 = TREE_CODE (type0);
6634 code1 = TREE_CODE (type1);
6636 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6637 STRIP_TYPE_NOPS (op0);
6638 STRIP_TYPE_NOPS (op1);
6640 /* If an error was already reported for one of the arguments,
6641 avoid reporting another error. */
6643 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6644 return error_mark_node;
6646 switch (code)
6648 case PLUS_EXPR:
6649 /* Handle the pointer + int case. */
6650 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6651 return pointer_int_sum (PLUS_EXPR, op0, op1);
6652 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6653 return pointer_int_sum (PLUS_EXPR, op1, op0);
6654 else
6655 common = 1;
6656 break;
6658 case MINUS_EXPR:
6659 /* Subtraction of two similar pointers.
6660 We must subtract them as integers, then divide by object size. */
6661 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6662 && comp_target_types (type0, type1, 1))
6663 return pointer_diff (op0, op1);
6664 /* Handle pointer minus int. Just like pointer plus int. */
6665 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6666 return pointer_int_sum (MINUS_EXPR, op0, op1);
6667 else
6668 common = 1;
6669 break;
6671 case MULT_EXPR:
6672 common = 1;
6673 break;
6675 case TRUNC_DIV_EXPR:
6676 case CEIL_DIV_EXPR:
6677 case FLOOR_DIV_EXPR:
6678 case ROUND_DIV_EXPR:
6679 case EXACT_DIV_EXPR:
6680 /* Floating point division by zero is a legitimate way to obtain
6681 infinities and NaNs. */
6682 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6683 warning ("division by zero");
6685 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6686 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6687 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6688 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6690 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6691 resultcode = RDIV_EXPR;
6692 else
6693 /* Although it would be tempting to shorten always here, that
6694 loses on some targets, since the modulo instruction is
6695 undefined if the quotient can't be represented in the
6696 computation mode. We shorten only if unsigned or if
6697 dividing by something we know != -1. */
6698 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6699 || (TREE_CODE (op1) == INTEGER_CST
6700 && ! integer_all_onesp (op1)));
6701 common = 1;
6703 break;
6705 case BIT_AND_EXPR:
6706 case BIT_IOR_EXPR:
6707 case BIT_XOR_EXPR:
6708 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6709 shorten = -1;
6710 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6711 common = 1;
6712 break;
6714 case TRUNC_MOD_EXPR:
6715 case FLOOR_MOD_EXPR:
6716 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6717 warning ("division by zero");
6719 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6721 /* Although it would be tempting to shorten always here, that loses
6722 on some targets, since the modulo instruction is undefined if the
6723 quotient can't be represented in the computation mode. We shorten
6724 only if unsigned or if dividing by something we know != -1. */
6725 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6726 || (TREE_CODE (op1) == INTEGER_CST
6727 && ! integer_all_onesp (op1)));
6728 common = 1;
6730 break;
6732 case TRUTH_ANDIF_EXPR:
6733 case TRUTH_ORIF_EXPR:
6734 case TRUTH_AND_EXPR:
6735 case TRUTH_OR_EXPR:
6736 case TRUTH_XOR_EXPR:
6737 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6738 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6739 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6740 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6742 /* Result of these operations is always an int,
6743 but that does not mean the operands should be
6744 converted to ints! */
6745 result_type = integer_type_node;
6746 op0 = c_common_truthvalue_conversion (op0);
6747 op1 = c_common_truthvalue_conversion (op1);
6748 converted = 1;
6750 break;
6752 /* Shift operations: result has same type as first operand;
6753 always convert second operand to int.
6754 Also set SHORT_SHIFT if shifting rightward. */
6756 case RSHIFT_EXPR:
6757 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6759 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6761 if (tree_int_cst_sgn (op1) < 0)
6762 warning ("right shift count is negative");
6763 else
6765 if (! integer_zerop (op1))
6766 short_shift = 1;
6768 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6769 warning ("right shift count >= width of type");
6773 /* Use the type of the value to be shifted. */
6774 result_type = type0;
6775 /* Convert the shift-count to an integer, regardless of size
6776 of value being shifted. */
6777 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6778 op1 = convert (integer_type_node, op1);
6779 /* Avoid converting op1 to result_type later. */
6780 converted = 1;
6782 break;
6784 case LSHIFT_EXPR:
6785 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6787 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6789 if (tree_int_cst_sgn (op1) < 0)
6790 warning ("left shift count is negative");
6792 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6793 warning ("left shift count >= width of type");
6796 /* Use the type of the value to be shifted. */
6797 result_type = type0;
6798 /* Convert the shift-count to an integer, regardless of size
6799 of value being shifted. */
6800 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6801 op1 = convert (integer_type_node, op1);
6802 /* Avoid converting op1 to result_type later. */
6803 converted = 1;
6805 break;
6807 case RROTATE_EXPR:
6808 case LROTATE_EXPR:
6809 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6811 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6813 if (tree_int_cst_sgn (op1) < 0)
6814 warning ("shift count is negative");
6815 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6816 warning ("shift count >= width of type");
6819 /* Use the type of the value to be shifted. */
6820 result_type = type0;
6821 /* Convert the shift-count to an integer, regardless of size
6822 of value being shifted. */
6823 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6824 op1 = convert (integer_type_node, op1);
6825 /* Avoid converting op1 to result_type later. */
6826 converted = 1;
6828 break;
6830 case EQ_EXPR:
6831 case NE_EXPR:
6832 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6833 warning ("comparing floating point with == or != is unsafe");
6834 /* Result of comparison is always int,
6835 but don't convert the args to int! */
6836 build_type = integer_type_node;
6837 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6838 || code0 == COMPLEX_TYPE
6839 || code0 == VECTOR_TYPE)
6840 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6841 || code1 == COMPLEX_TYPE
6842 || code1 == VECTOR_TYPE))
6843 short_compare = 1;
6844 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6846 tree tt0 = TREE_TYPE (type0);
6847 tree tt1 = TREE_TYPE (type1);
6848 /* Anything compares with void *. void * compares with anything.
6849 Otherwise, the targets must be compatible
6850 and both must be object or both incomplete. */
6851 if (comp_target_types (type0, type1, 1))
6852 result_type = common_type (type0, type1);
6853 else if (VOID_TYPE_P (tt0))
6855 /* op0 != orig_op0 detects the case of something
6856 whose value is 0 but which isn't a valid null ptr const. */
6857 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6858 && TREE_CODE (tt1) == FUNCTION_TYPE)
6859 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6861 else if (VOID_TYPE_P (tt1))
6863 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6864 && TREE_CODE (tt0) == FUNCTION_TYPE)
6865 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6867 else
6868 pedwarn ("comparison of distinct pointer types lacks a cast");
6870 if (result_type == NULL_TREE)
6871 result_type = ptr_type_node;
6873 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6874 && integer_zerop (op1))
6875 result_type = type0;
6876 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6877 && integer_zerop (op0))
6878 result_type = type1;
6879 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6881 result_type = type0;
6882 pedwarn ("comparison between pointer and integer");
6884 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6886 result_type = type1;
6887 pedwarn ("comparison between pointer and integer");
6889 break;
6891 case MAX_EXPR:
6892 case MIN_EXPR:
6893 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6894 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6895 shorten = 1;
6896 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6898 if (comp_target_types (type0, type1, 1))
6900 result_type = common_type (type0, type1);
6901 if (pedantic
6902 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6903 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6905 else
6907 result_type = ptr_type_node;
6908 pedwarn ("comparison of distinct pointer types lacks a cast");
6911 break;
6913 case LE_EXPR:
6914 case GE_EXPR:
6915 case LT_EXPR:
6916 case GT_EXPR:
6917 build_type = integer_type_node;
6918 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6919 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6920 short_compare = 1;
6921 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6923 if (comp_target_types (type0, type1, 1))
6925 result_type = common_type (type0, type1);
6926 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6927 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6928 pedwarn ("comparison of complete and incomplete pointers");
6929 else if (pedantic
6930 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6931 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6933 else
6935 result_type = ptr_type_node;
6936 pedwarn ("comparison of distinct pointer types lacks a cast");
6939 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6940 && integer_zerop (op1))
6942 result_type = type0;
6943 if (pedantic || extra_warnings)
6944 pedwarn ("ordered comparison of pointer with integer zero");
6946 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6947 && integer_zerop (op0))
6949 result_type = type1;
6950 if (pedantic)
6951 pedwarn ("ordered comparison of pointer with integer zero");
6953 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6955 result_type = type0;
6956 pedwarn ("comparison between pointer and integer");
6958 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6960 result_type = type1;
6961 pedwarn ("comparison between pointer and integer");
6963 break;
6965 case UNORDERED_EXPR:
6966 case ORDERED_EXPR:
6967 case UNLT_EXPR:
6968 case UNLE_EXPR:
6969 case UNGT_EXPR:
6970 case UNGE_EXPR:
6971 case UNEQ_EXPR:
6972 build_type = integer_type_node;
6973 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6975 error ("unordered comparison on non-floating point argument");
6976 return error_mark_node;
6978 common = 1;
6979 break;
6981 default:
6982 break;
6985 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6986 || code0 == VECTOR_TYPE)
6988 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6989 || code1 == VECTOR_TYPE))
6991 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6993 if (shorten || common || short_compare)
6994 result_type = common_type (type0, type1);
6996 /* For certain operations (which identify themselves by shorten != 0)
6997 if both args were extended from the same smaller type,
6998 do the arithmetic in that type and then extend.
7000 shorten !=0 and !=1 indicates a bitwise operation.
7001 For them, this optimization is safe only if
7002 both args are zero-extended or both are sign-extended.
7003 Otherwise, we might change the result.
7004 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7005 but calculated in (unsigned short) it would be (unsigned short)-1. */
7007 if (shorten && none_complex)
7009 int unsigned0, unsigned1;
7010 tree arg0 = get_narrower (op0, &unsigned0);
7011 tree arg1 = get_narrower (op1, &unsigned1);
7012 /* UNS is 1 if the operation to be done is an unsigned one. */
7013 int uns = TREE_UNSIGNED (result_type);
7014 tree type;
7016 final_type = result_type;
7018 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7019 but it *requires* conversion to FINAL_TYPE. */
7021 if ((TYPE_PRECISION (TREE_TYPE (op0))
7022 == TYPE_PRECISION (TREE_TYPE (arg0)))
7023 && TREE_TYPE (op0) != final_type)
7024 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
7025 if ((TYPE_PRECISION (TREE_TYPE (op1))
7026 == TYPE_PRECISION (TREE_TYPE (arg1)))
7027 && TREE_TYPE (op1) != final_type)
7028 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
7030 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7032 /* For bitwise operations, signedness of nominal type
7033 does not matter. Consider only how operands were extended. */
7034 if (shorten == -1)
7035 uns = unsigned0;
7037 /* Note that in all three cases below we refrain from optimizing
7038 an unsigned operation on sign-extended args.
7039 That would not be valid. */
7041 /* Both args variable: if both extended in same way
7042 from same width, do it in that width.
7043 Do it unsigned if args were zero-extended. */
7044 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7045 < TYPE_PRECISION (result_type))
7046 && (TYPE_PRECISION (TREE_TYPE (arg1))
7047 == TYPE_PRECISION (TREE_TYPE (arg0)))
7048 && unsigned0 == unsigned1
7049 && (unsigned0 || !uns))
7050 result_type
7051 = c_common_signed_or_unsigned_type
7052 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7053 else if (TREE_CODE (arg0) == INTEGER_CST
7054 && (unsigned1 || !uns)
7055 && (TYPE_PRECISION (TREE_TYPE (arg1))
7056 < TYPE_PRECISION (result_type))
7057 && (type
7058 = c_common_signed_or_unsigned_type (unsigned1,
7059 TREE_TYPE (arg1)),
7060 int_fits_type_p (arg0, type)))
7061 result_type = type;
7062 else if (TREE_CODE (arg1) == INTEGER_CST
7063 && (unsigned0 || !uns)
7064 && (TYPE_PRECISION (TREE_TYPE (arg0))
7065 < TYPE_PRECISION (result_type))
7066 && (type
7067 = c_common_signed_or_unsigned_type (unsigned0,
7068 TREE_TYPE (arg0)),
7069 int_fits_type_p (arg1, type)))
7070 result_type = type;
7073 /* Shifts can be shortened if shifting right. */
7075 if (short_shift)
7077 int unsigned_arg;
7078 tree arg0 = get_narrower (op0, &unsigned_arg);
7080 final_type = result_type;
7082 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7083 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
7085 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7086 /* We can shorten only if the shift count is less than the
7087 number of bits in the smaller type size. */
7088 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7089 /* We cannot drop an unsigned shift after sign-extension. */
7090 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
7092 /* Do an unsigned shift if the operand was zero-extended. */
7093 result_type
7094 = c_common_signed_or_unsigned_type (unsigned_arg,
7095 TREE_TYPE (arg0));
7096 /* Convert value-to-be-shifted to that type. */
7097 if (TREE_TYPE (op0) != result_type)
7098 op0 = convert (result_type, op0);
7099 converted = 1;
7103 /* Comparison operations are shortened too but differently.
7104 They identify themselves by setting short_compare = 1. */
7106 if (short_compare)
7108 /* Don't write &op0, etc., because that would prevent op0
7109 from being kept in a register.
7110 Instead, make copies of the our local variables and
7111 pass the copies by reference, then copy them back afterward. */
7112 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7113 enum tree_code xresultcode = resultcode;
7114 tree val
7115 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7117 if (val != 0)
7118 return val;
7120 op0 = xop0, op1 = xop1;
7121 converted = 1;
7122 resultcode = xresultcode;
7124 if (warn_sign_compare && skip_evaluation == 0)
7126 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7127 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7128 int unsignedp0, unsignedp1;
7129 tree primop0 = get_narrower (op0, &unsignedp0);
7130 tree primop1 = get_narrower (op1, &unsignedp1);
7132 xop0 = orig_op0;
7133 xop1 = orig_op1;
7134 STRIP_TYPE_NOPS (xop0);
7135 STRIP_TYPE_NOPS (xop1);
7137 /* Give warnings for comparisons between signed and unsigned
7138 quantities that may fail.
7140 Do the checking based on the original operand trees, so that
7141 casts will be considered, but default promotions won't be.
7143 Do not warn if the comparison is being done in a signed type,
7144 since the signed type will only be chosen if it can represent
7145 all the values of the unsigned type. */
7146 if (! TREE_UNSIGNED (result_type))
7147 /* OK */;
7148 /* Do not warn if both operands are the same signedness. */
7149 else if (op0_signed == op1_signed)
7150 /* OK */;
7151 else
7153 tree sop, uop;
7155 if (op0_signed)
7156 sop = xop0, uop = xop1;
7157 else
7158 sop = xop1, uop = xop0;
7160 /* Do not warn if the signed quantity is an
7161 unsuffixed integer literal (or some static
7162 constant expression involving such literals or a
7163 conditional expression involving such literals)
7164 and it is non-negative. */
7165 if (c_tree_expr_nonnegative_p (sop))
7166 /* OK */;
7167 /* Do not warn if the comparison is an equality operation,
7168 the unsigned quantity is an integral constant, and it
7169 would fit in the result if the result were signed. */
7170 else if (TREE_CODE (uop) == INTEGER_CST
7171 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7172 && int_fits_type_p
7173 (uop, c_common_signed_type (result_type)))
7174 /* OK */;
7175 /* Do not warn if the unsigned quantity is an enumeration
7176 constant and its maximum value would fit in the result
7177 if the result were signed. */
7178 else if (TREE_CODE (uop) == INTEGER_CST
7179 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7180 && int_fits_type_p
7181 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7182 c_common_signed_type (result_type)))
7183 /* OK */;
7184 else
7185 warning ("comparison between signed and unsigned");
7188 /* Warn if two unsigned values are being compared in a size
7189 larger than their original size, and one (and only one) is the
7190 result of a `~' operator. This comparison will always fail.
7192 Also warn if one operand is a constant, and the constant
7193 does not have all bits set that are set in the ~ operand
7194 when it is extended. */
7196 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7197 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7199 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7200 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7201 &unsignedp0);
7202 else
7203 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7204 &unsignedp1);
7206 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7208 tree primop;
7209 HOST_WIDE_INT constant, mask;
7210 int unsignedp, bits;
7212 if (host_integerp (primop0, 0))
7214 primop = primop1;
7215 unsignedp = unsignedp1;
7216 constant = tree_low_cst (primop0, 0);
7218 else
7220 primop = primop0;
7221 unsignedp = unsignedp0;
7222 constant = tree_low_cst (primop1, 0);
7225 bits = TYPE_PRECISION (TREE_TYPE (primop));
7226 if (bits < TYPE_PRECISION (result_type)
7227 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7229 mask = (~ (HOST_WIDE_INT) 0) << bits;
7230 if ((mask & constant) != mask)
7231 warning ("comparison of promoted ~unsigned with constant");
7234 else if (unsignedp0 && unsignedp1
7235 && (TYPE_PRECISION (TREE_TYPE (primop0))
7236 < TYPE_PRECISION (result_type))
7237 && (TYPE_PRECISION (TREE_TYPE (primop1))
7238 < TYPE_PRECISION (result_type)))
7239 warning ("comparison of promoted ~unsigned with unsigned");
7245 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7246 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7247 Then the expression will be built.
7248 It will be given type FINAL_TYPE if that is nonzero;
7249 otherwise, it will be given type RESULT_TYPE. */
7251 if (!result_type)
7253 binary_op_error (code);
7254 return error_mark_node;
7257 if (! converted)
7259 if (TREE_TYPE (op0) != result_type)
7260 op0 = convert (result_type, op0);
7261 if (TREE_TYPE (op1) != result_type)
7262 op1 = convert (result_type, op1);
7265 if (build_type == NULL_TREE)
7266 build_type = result_type;
7269 tree result = build (resultcode, build_type, op0, op1);
7270 tree folded;
7272 /* Treat expressions in initializers specially as they can't trap. */
7273 folded = initializer_stack ? fold_initializer (result)
7274 : fold (result);
7275 if (folded == result)
7276 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7277 if (final_type != 0)
7278 return convert (final_type, folded);
7279 return folded;