2003-09-04 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / c-typeck.c
blob23e73e8798253fbc51a3f163d9bb5aa47bb5044c
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 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 /* Treat an enum type as the integer type of the same width and
478 signedness. */
480 if (TREE_CODE (t1) == ENUMERAL_TYPE)
481 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482 if (TREE_CODE (t2) == ENUMERAL_TYPE)
483 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
485 if (t1 == t2)
486 return 1;
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
495 return 0;
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
502 return 1;
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
506 return 0;
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 val = 0;
511 switch (TREE_CODE (t1))
513 case POINTER_TYPE:
514 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
515 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
516 break;
518 case FUNCTION_TYPE:
519 val = function_types_compatible_p (t1, t2, flags);
520 break;
522 case ARRAY_TYPE:
524 tree d1 = TYPE_DOMAIN (t1);
525 tree d2 = TYPE_DOMAIN (t2);
526 bool d1_variable, d2_variable;
527 bool d1_zero, d2_zero;
528 val = 1;
530 /* Target types must match incl. qualifiers. */
531 if (TREE_TYPE (t1) != TREE_TYPE (t2)
532 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
533 flags)))
534 return 0;
536 /* Sizes must match unless one is missing or variable. */
537 if (d1 == 0 || d2 == 0 || d1 == d2)
538 break;
540 d1_zero = ! TYPE_MAX_VALUE (d1);
541 d2_zero = ! TYPE_MAX_VALUE (d2);
543 d1_variable = (! d1_zero
544 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
545 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
546 d2_variable = (! d2_zero
547 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
548 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
550 if (d1_variable || d2_variable)
551 break;
552 if (d1_zero && d2_zero)
553 break;
554 if (d1_zero || d2_zero
555 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
556 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
557 val = 0;
559 break;
562 case RECORD_TYPE:
563 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
564 val = 1;
566 case ENUMERAL_TYPE:
567 case UNION_TYPE:
568 if (val != 1 && !same_translation_unit_p (t1, t2))
569 val = tagged_types_tu_compatible_p (t1, t2, flags);
570 break;
572 case VECTOR_TYPE:
573 /* The target might allow certain vector types to be compatible. */
574 val = (*targetm.vector_opaque_p) (t1)
575 || (*targetm.vector_opaque_p) (t2);
576 break;
578 default:
579 break;
581 return attrval == 2 && val == 1 ? 2 : val;
584 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
585 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
586 to 1 or 0 depending if the check of the pointer types is meant to
587 be reflexive or not (typically, assignments are not reflexive,
588 while comparisons are reflexive).
591 static int
592 comp_target_types (tree ttl, tree ttr, int reflexive)
594 int val;
596 /* Give objc_comptypes a crack at letting these types through. */
597 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
598 return val;
600 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
601 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
603 if (val == 2 && pedantic)
604 pedwarn ("types are not quite compatible");
605 return val;
608 /* Subroutines of `comptypes'. */
610 /* Determine whether two types derive from the same translation unit.
611 If the CONTEXT chain ends in a null, that type's context is still
612 being parsed, so if two types have context chains ending in null,
613 they're in the same translation unit. */
614 static int
615 same_translation_unit_p (tree t1, tree t2)
617 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
618 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
620 case 'd': t1 = DECL_CONTEXT (t1); break;
621 case 't': t1 = TYPE_CONTEXT (t1); break;
622 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
623 default: abort ();
626 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
627 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
629 case 'd': t2 = DECL_CONTEXT (t1); break;
630 case 't': t2 = TYPE_CONTEXT (t2); break;
631 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
632 default: abort ();
635 return t1 == t2;
638 /* The C standard says that two structures in different translation
639 units are compatible with each other only if the types of their
640 fields are compatible (among other things). So, consider two copies
641 of this structure: */
643 struct tagged_tu_seen {
644 const struct tagged_tu_seen * next;
645 tree t1;
646 tree t2;
649 /* Can they be compatible with each other? We choose to break the
650 recursion by allowing those types to be compatible. */
652 static const struct tagged_tu_seen * tagged_tu_seen_base;
654 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
655 compatible. If the two types are not the same (which has been
656 checked earlier), this can only happen when multiple translation
657 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
658 rules. */
660 static int
661 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
663 tree s1, s2;
664 bool needs_warning = false;
666 /* We have to verify that the tags of the types are the same. This
667 is harder than it looks because this may be a typedef, so we have
668 to go look at the original type. It may even be a typedef of a
669 typedef... */
670 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
671 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
673 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
674 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
676 /* C90 didn't have the requirement that the two tags be the same. */
677 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
678 return 0;
680 /* C90 didn't say what happened if one or both of the types were
681 incomplete; we choose to follow C99 rules here, which is that they
682 are compatible. */
683 if (TYPE_SIZE (t1) == NULL
684 || TYPE_SIZE (t2) == NULL)
685 return 1;
688 const struct tagged_tu_seen * tts_i;
689 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
690 if (tts_i->t1 == t1 && tts_i->t2 == t2)
691 return 1;
694 switch (TREE_CODE (t1))
696 case ENUMERAL_TYPE:
698 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
699 return 0;
701 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
703 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
704 if (s2 == NULL
705 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
706 return 0;
708 return 1;
711 case UNION_TYPE:
713 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
714 return 0;
716 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
718 bool ok = false;
719 struct tagged_tu_seen tts;
721 tts.next = tagged_tu_seen_base;
722 tts.t1 = t1;
723 tts.t2 = t2;
724 tagged_tu_seen_base = &tts;
726 if (DECL_NAME (s1) != NULL)
727 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
728 if (DECL_NAME (s1) == DECL_NAME (s2))
730 int result;
731 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
732 if (result == 0)
733 break;
734 if (result == 2)
735 needs_warning = true;
737 if (TREE_CODE (s1) == FIELD_DECL
738 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
739 DECL_FIELD_BIT_OFFSET (s2)) != 1)
740 break;
742 ok = true;
743 break;
745 tagged_tu_seen_base = tts.next;
746 if (! ok)
747 return 0;
749 return needs_warning ? 2 : 1;
752 case RECORD_TYPE:
754 struct tagged_tu_seen tts;
756 tts.next = tagged_tu_seen_base;
757 tts.t1 = t1;
758 tts.t2 = t2;
759 tagged_tu_seen_base = &tts;
761 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
762 s1 && s2;
763 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
765 int result;
766 if (TREE_CODE (s1) != TREE_CODE (s2)
767 || DECL_NAME (s1) != DECL_NAME (s2))
768 break;
769 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
770 if (result == 0)
771 break;
772 if (result == 2)
773 needs_warning = true;
775 if (TREE_CODE (s1) == FIELD_DECL
776 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
777 DECL_FIELD_BIT_OFFSET (s2)) != 1)
778 break;
780 tagged_tu_seen_base = tts.next;
781 if (s1 && s2)
782 return 0;
783 return needs_warning ? 2 : 1;
786 default:
787 abort ();
791 /* Return 1 if two function types F1 and F2 are compatible.
792 If either type specifies no argument types,
793 the other must specify a fixed number of self-promoting arg types.
794 Otherwise, if one type specifies only the number of arguments,
795 the other must specify that number of self-promoting arg types.
796 Otherwise, the argument types must match. */
798 static int
799 function_types_compatible_p (tree f1, tree f2, int flags)
801 tree args1, args2;
802 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
803 int val = 1;
804 int val1;
805 tree ret1, ret2;
807 ret1 = TREE_TYPE (f1);
808 ret2 = TREE_TYPE (f2);
810 /* 'volatile' qualifiers on a function's return type mean the function
811 is noreturn. */
812 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
813 pedwarn ("function return types not compatible due to `volatile'");
814 if (TYPE_VOLATILE (ret1))
815 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
816 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
817 if (TYPE_VOLATILE (ret2))
818 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
819 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
820 val = comptypes (ret1, ret2, flags);
821 if (val == 0)
822 return 0;
824 args1 = TYPE_ARG_TYPES (f1);
825 args2 = TYPE_ARG_TYPES (f2);
827 /* An unspecified parmlist matches any specified parmlist
828 whose argument types don't need default promotions. */
830 if (args1 == 0)
832 if (!self_promoting_args_p (args2))
833 return 0;
834 /* If one of these types comes from a non-prototype fn definition,
835 compare that with the other type's arglist.
836 If they don't match, ask for a warning (but no error). */
837 if (TYPE_ACTUAL_ARG_TYPES (f1)
838 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
839 flags))
840 val = 2;
841 return val;
843 if (args2 == 0)
845 if (!self_promoting_args_p (args1))
846 return 0;
847 if (TYPE_ACTUAL_ARG_TYPES (f2)
848 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
849 flags))
850 val = 2;
851 return val;
854 /* Both types have argument lists: compare them and propagate results. */
855 val1 = type_lists_compatible_p (args1, args2, flags);
856 return val1 != 1 ? val1 : val;
859 /* Check two lists of types for compatibility,
860 returning 0 for incompatible, 1 for compatible,
861 or 2 for compatible with warning. */
863 static int
864 type_lists_compatible_p (tree args1, tree args2, int flags)
866 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
867 int val = 1;
868 int newval = 0;
870 while (1)
872 if (args1 == 0 && args2 == 0)
873 return val;
874 /* If one list is shorter than the other,
875 they fail to match. */
876 if (args1 == 0 || args2 == 0)
877 return 0;
878 /* A null pointer instead of a type
879 means there is supposed to be an argument
880 but nothing is specified about what type it has.
881 So match anything that self-promotes. */
882 if (TREE_VALUE (args1) == 0)
884 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
885 return 0;
887 else if (TREE_VALUE (args2) == 0)
889 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
890 return 0;
892 /* If one of the lists has an error marker, ignore this arg. */
893 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
894 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
896 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
897 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
898 flags)))
900 /* Allow wait (union {union wait *u; int *i} *)
901 and wait (union wait *) to be compatible. */
902 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
903 && (TYPE_NAME (TREE_VALUE (args1)) == 0
904 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
905 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
906 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
907 TYPE_SIZE (TREE_VALUE (args2))))
909 tree memb;
910 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
911 memb; memb = TREE_CHAIN (memb))
912 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
913 flags))
914 break;
915 if (memb == 0)
916 return 0;
918 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
919 && (TYPE_NAME (TREE_VALUE (args2)) == 0
920 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
921 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
922 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
923 TYPE_SIZE (TREE_VALUE (args1))))
925 tree memb;
926 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
927 memb; memb = TREE_CHAIN (memb))
928 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
929 flags))
930 break;
931 if (memb == 0)
932 return 0;
934 else
935 return 0;
938 /* comptypes said ok, but record if it said to warn. */
939 if (newval > val)
940 val = newval;
942 args1 = TREE_CHAIN (args1);
943 args2 = TREE_CHAIN (args2);
947 /* Compute the size to increment a pointer by. */
949 tree
950 c_size_in_bytes (tree type)
952 enum tree_code code = TREE_CODE (type);
954 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
955 return size_one_node;
957 if (!COMPLETE_OR_VOID_TYPE_P (type))
959 error ("arithmetic on pointer to an incomplete type");
960 return size_one_node;
963 /* Convert in case a char is more than one unit. */
964 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
965 size_int (TYPE_PRECISION (char_type_node)
966 / BITS_PER_UNIT));
969 /* Return either DECL or its known constant value (if it has one). */
971 tree
972 decl_constant_value (tree decl)
974 if (/* Don't change a variable array bound or initial value to a constant
975 in a place where a variable is invalid. */
976 current_function_decl != 0
977 && ! TREE_THIS_VOLATILE (decl)
978 && TREE_READONLY (decl)
979 && DECL_INITIAL (decl) != 0
980 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
981 /* This is invalid if initial value is not constant.
982 If it has either a function call, a memory reference,
983 or a variable, then re-evaluating it could give different results. */
984 && TREE_CONSTANT (DECL_INITIAL (decl))
985 /* Check for cases where this is sub-optimal, even though valid. */
986 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
987 return DECL_INITIAL (decl);
988 return decl;
991 /* Return either DECL or its known constant value (if it has one), but
992 return DECL if pedantic or DECL has mode BLKmode. This is for
993 bug-compatibility with the old behavior of decl_constant_value
994 (before GCC 3.0); every use of this function is a bug and it should
995 be removed before GCC 3.1. It is not appropriate to use pedantic
996 in a way that affects optimization, and BLKmode is probably not the
997 right test for avoiding misoptimizations either. */
999 static tree
1000 decl_constant_value_for_broken_optimization (tree decl)
1002 if (pedantic || DECL_MODE (decl) == BLKmode)
1003 return decl;
1004 else
1005 return decl_constant_value (decl);
1009 /* Perform the default conversion of arrays and functions to pointers.
1010 Return the result of converting EXP. For any other expression, just
1011 return EXP. */
1013 static tree
1014 default_function_array_conversion (tree exp)
1016 tree orig_exp;
1017 tree type = TREE_TYPE (exp);
1018 enum tree_code code = TREE_CODE (type);
1019 int not_lvalue = 0;
1021 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1022 an lvalue.
1024 Do not use STRIP_NOPS here! It will remove conversions from pointer
1025 to integer and cause infinite recursion. */
1026 orig_exp = exp;
1027 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1028 || (TREE_CODE (exp) == NOP_EXPR
1029 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1031 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1032 not_lvalue = 1;
1033 exp = TREE_OPERAND (exp, 0);
1036 /* Preserve the original expression code. */
1037 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1038 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1040 if (code == FUNCTION_TYPE)
1042 return build_unary_op (ADDR_EXPR, exp, 0);
1044 if (code == ARRAY_TYPE)
1046 tree adr;
1047 tree restype = TREE_TYPE (type);
1048 tree ptrtype;
1049 int constp = 0;
1050 int volatilep = 0;
1051 int lvalue_array_p;
1053 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1055 constp = TREE_READONLY (exp);
1056 volatilep = TREE_THIS_VOLATILE (exp);
1059 if (TYPE_QUALS (type) || constp || volatilep)
1060 restype
1061 = c_build_qualified_type (restype,
1062 TYPE_QUALS (type)
1063 | (constp * TYPE_QUAL_CONST)
1064 | (volatilep * TYPE_QUAL_VOLATILE));
1066 if (TREE_CODE (exp) == INDIRECT_REF)
1067 return convert (TYPE_POINTER_TO (restype),
1068 TREE_OPERAND (exp, 0));
1070 if (TREE_CODE (exp) == COMPOUND_EXPR)
1072 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1073 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1074 TREE_OPERAND (exp, 0), op1);
1077 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1078 if (!flag_isoc99 && !lvalue_array_p)
1080 /* Before C99, non-lvalue arrays do not decay to pointers.
1081 Normally, using such an array would be invalid; but it can
1082 be used correctly inside sizeof or as a statement expression.
1083 Thus, do not give an error here; an error will result later. */
1084 return exp;
1087 ptrtype = build_pointer_type (restype);
1089 if (TREE_CODE (exp) == VAR_DECL)
1091 /* ??? This is not really quite correct
1092 in that the type of the operand of ADDR_EXPR
1093 is not the target type of the type of the ADDR_EXPR itself.
1094 Question is, can this lossage be avoided? */
1095 adr = build1 (ADDR_EXPR, ptrtype, exp);
1096 if (!c_mark_addressable (exp))
1097 return error_mark_node;
1098 TREE_CONSTANT (adr) = staticp (exp);
1099 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1100 return adr;
1102 /* This way is better for a COMPONENT_REF since it can
1103 simplify the offset for a component. */
1104 adr = build_unary_op (ADDR_EXPR, exp, 1);
1105 return convert (ptrtype, adr);
1107 return exp;
1110 /* Perform default promotions for C data used in expressions.
1111 Arrays and functions are converted to pointers;
1112 enumeral types or short or char, to int.
1113 In addition, manifest constants symbols are replaced by their values. */
1115 tree
1116 default_conversion (tree exp)
1118 tree orig_exp;
1119 tree type = TREE_TYPE (exp);
1120 enum tree_code code = TREE_CODE (type);
1122 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1123 return default_function_array_conversion (exp);
1125 /* Constants can be used directly unless they're not loadable. */
1126 if (TREE_CODE (exp) == CONST_DECL)
1127 exp = DECL_INITIAL (exp);
1129 /* Replace a nonvolatile const static variable with its value unless
1130 it is an array, in which case we must be sure that taking the
1131 address of the array produces consistent results. */
1132 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1134 exp = decl_constant_value_for_broken_optimization (exp);
1135 type = TREE_TYPE (exp);
1138 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1139 an lvalue.
1141 Do not use STRIP_NOPS here! It will remove conversions from pointer
1142 to integer and cause infinite recursion. */
1143 orig_exp = exp;
1144 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1145 || (TREE_CODE (exp) == NOP_EXPR
1146 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1147 exp = TREE_OPERAND (exp, 0);
1149 /* Preserve the original expression code. */
1150 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1151 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1153 /* Normally convert enums to int,
1154 but convert wide enums to something wider. */
1155 if (code == ENUMERAL_TYPE)
1157 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1158 TYPE_PRECISION (integer_type_node)),
1159 ((TYPE_PRECISION (type)
1160 >= TYPE_PRECISION (integer_type_node))
1161 && TREE_UNSIGNED (type)));
1163 return convert (type, exp);
1166 if (TREE_CODE (exp) == COMPONENT_REF
1167 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1168 /* If it's thinner than an int, promote it like a
1169 c_promoting_integer_type_p, otherwise leave it alone. */
1170 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1171 TYPE_PRECISION (integer_type_node)))
1172 return convert (integer_type_node, exp);
1174 if (c_promoting_integer_type_p (type))
1176 /* Preserve unsignedness if not really getting any wider. */
1177 if (TREE_UNSIGNED (type)
1178 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1179 return convert (unsigned_type_node, exp);
1181 return convert (integer_type_node, exp);
1184 if (code == VOID_TYPE)
1186 error ("void value not ignored as it ought to be");
1187 return error_mark_node;
1189 return exp;
1192 /* Look up COMPONENT in a structure or union DECL.
1194 If the component name is not found, returns NULL_TREE. Otherwise,
1195 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1196 stepping down the chain to the component, which is in the last
1197 TREE_VALUE of the list. Normally the list is of length one, but if
1198 the component is embedded within (nested) anonymous structures or
1199 unions, the list steps down the chain to the component. */
1201 static tree
1202 lookup_field (tree decl, tree component)
1204 tree type = TREE_TYPE (decl);
1205 tree field;
1207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1208 to the field elements. Use a binary search on this array to quickly
1209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1210 will always be set for structures which have many elements. */
1212 if (TYPE_LANG_SPECIFIC (type))
1214 int bot, top, half;
1215 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1217 field = TYPE_FIELDS (type);
1218 bot = 0;
1219 top = TYPE_LANG_SPECIFIC (type)->s->len;
1220 while (top - bot > 1)
1222 half = (top - bot + 1) >> 1;
1223 field = field_array[bot+half];
1225 if (DECL_NAME (field) == NULL_TREE)
1227 /* Step through all anon unions in linear fashion. */
1228 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1230 field = field_array[bot++];
1231 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1232 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1234 tree anon = lookup_field (field, component);
1236 if (anon)
1237 return tree_cons (NULL_TREE, field, anon);
1241 /* Entire record is only anon unions. */
1242 if (bot > top)
1243 return NULL_TREE;
1245 /* Restart the binary search, with new lower bound. */
1246 continue;
1249 if (DECL_NAME (field) == component)
1250 break;
1251 if (DECL_NAME (field) < component)
1252 bot += half;
1253 else
1254 top = bot + half;
1257 if (DECL_NAME (field_array[bot]) == component)
1258 field = field_array[bot];
1259 else if (DECL_NAME (field) != component)
1260 return NULL_TREE;
1262 else
1264 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1266 if (DECL_NAME (field) == NULL_TREE
1267 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1268 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1270 tree anon = lookup_field (field, component);
1272 if (anon)
1273 return tree_cons (NULL_TREE, field, anon);
1276 if (DECL_NAME (field) == component)
1277 break;
1280 if (field == NULL_TREE)
1281 return NULL_TREE;
1284 return tree_cons (NULL_TREE, field, NULL_TREE);
1287 /* Make an expression to refer to the COMPONENT field of
1288 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1290 tree
1291 build_component_ref (tree datum, tree component)
1293 tree type = TREE_TYPE (datum);
1294 enum tree_code code = TREE_CODE (type);
1295 tree field = NULL;
1296 tree ref;
1298 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1299 If pedantic ensure that the arguments are not lvalues; otherwise,
1300 if the component is an array, it would wrongly decay to a pointer in
1301 C89 mode.
1302 We cannot do this with a COND_EXPR, because in a conditional expression
1303 the default promotions are applied to both sides, and this would yield
1304 the wrong type of the result; for example, if the components have
1305 type "char". */
1306 switch (TREE_CODE (datum))
1308 case COMPOUND_EXPR:
1310 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1311 return build (COMPOUND_EXPR, TREE_TYPE (value),
1312 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1314 default:
1315 break;
1318 /* See if there is a field or component with name COMPONENT. */
1320 if (code == RECORD_TYPE || code == UNION_TYPE)
1322 if (!COMPLETE_TYPE_P (type))
1324 c_incomplete_type_error (NULL_TREE, type);
1325 return error_mark_node;
1328 field = lookup_field (datum, component);
1330 if (!field)
1332 error ("%s has no member named `%s'",
1333 code == RECORD_TYPE ? "structure" : "union",
1334 IDENTIFIER_POINTER (component));
1335 return error_mark_node;
1338 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1339 This might be better solved in future the way the C++ front
1340 end does it - by giving the anonymous entities each a
1341 separate name and type, and then have build_component_ref
1342 recursively call itself. We can't do that here. */
1345 tree subdatum = TREE_VALUE (field);
1347 if (TREE_TYPE (subdatum) == error_mark_node)
1348 return error_mark_node;
1350 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1351 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1352 TREE_READONLY (ref) = 1;
1353 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1354 TREE_THIS_VOLATILE (ref) = 1;
1356 if (TREE_DEPRECATED (subdatum))
1357 warn_deprecated_use (subdatum);
1359 datum = ref;
1361 field = TREE_CHAIN (field);
1363 while (field);
1365 return ref;
1367 else if (code != ERROR_MARK)
1368 error ("request for member `%s' in something not a structure or union",
1369 IDENTIFIER_POINTER (component));
1371 return error_mark_node;
1374 /* Given an expression PTR for a pointer, return an expression
1375 for the value pointed to.
1376 ERRORSTRING is the name of the operator to appear in error messages. */
1378 tree
1379 build_indirect_ref (tree ptr, const char *errorstring)
1381 tree pointer = default_conversion (ptr);
1382 tree type = TREE_TYPE (pointer);
1384 if (TREE_CODE (type) == POINTER_TYPE)
1386 if (TREE_CODE (pointer) == ADDR_EXPR
1387 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1388 == TREE_TYPE (type)))
1389 return TREE_OPERAND (pointer, 0);
1390 else
1392 tree t = TREE_TYPE (type);
1393 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1395 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1397 error ("dereferencing pointer to incomplete type");
1398 return error_mark_node;
1400 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1401 warning ("dereferencing `void *' pointer");
1403 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1404 so that we get the proper error message if the result is used
1405 to assign to. Also, &* is supposed to be a no-op.
1406 And ANSI C seems to specify that the type of the result
1407 should be the const type. */
1408 /* A de-reference of a pointer to const is not a const. It is valid
1409 to change it via some other pointer. */
1410 TREE_READONLY (ref) = TYPE_READONLY (t);
1411 TREE_SIDE_EFFECTS (ref)
1412 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1413 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1414 return ref;
1417 else if (TREE_CODE (pointer) != ERROR_MARK)
1418 error ("invalid type argument of `%s'", errorstring);
1419 return error_mark_node;
1422 /* This handles expressions of the form "a[i]", which denotes
1423 an array reference.
1425 This is logically equivalent in C to *(a+i), but we may do it differently.
1426 If A is a variable or a member, we generate a primitive ARRAY_REF.
1427 This avoids forcing the array out of registers, and can work on
1428 arrays that are not lvalues (for example, members of structures returned
1429 by functions). */
1431 tree
1432 build_array_ref (tree array, tree index)
1434 if (index == 0)
1436 error ("subscript missing in array reference");
1437 return error_mark_node;
1440 if (TREE_TYPE (array) == error_mark_node
1441 || TREE_TYPE (index) == error_mark_node)
1442 return error_mark_node;
1444 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1445 && TREE_CODE (array) != INDIRECT_REF)
1447 tree rval, type;
1449 /* Subscripting with type char is likely to lose
1450 on a machine where chars are signed.
1451 So warn on any machine, but optionally.
1452 Don't warn for unsigned char since that type is safe.
1453 Don't warn for signed char because anyone who uses that
1454 must have done so deliberately. */
1455 if (warn_char_subscripts
1456 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1457 warning ("array subscript has type `char'");
1459 /* Apply default promotions *after* noticing character types. */
1460 index = default_conversion (index);
1462 /* Require integer *after* promotion, for sake of enums. */
1463 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1465 error ("array subscript is not an integer");
1466 return error_mark_node;
1469 /* An array that is indexed by a non-constant
1470 cannot be stored in a register; we must be able to do
1471 address arithmetic on its address.
1472 Likewise an array of elements of variable size. */
1473 if (TREE_CODE (index) != INTEGER_CST
1474 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1475 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1477 if (!c_mark_addressable (array))
1478 return error_mark_node;
1480 /* An array that is indexed by a constant value which is not within
1481 the array bounds cannot be stored in a register either; because we
1482 would get a crash in store_bit_field/extract_bit_field when trying
1483 to access a non-existent part of the register. */
1484 if (TREE_CODE (index) == INTEGER_CST
1485 && TYPE_VALUES (TREE_TYPE (array))
1486 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1488 if (!c_mark_addressable (array))
1489 return error_mark_node;
1492 if (pedantic)
1494 tree foo = array;
1495 while (TREE_CODE (foo) == COMPONENT_REF)
1496 foo = TREE_OPERAND (foo, 0);
1497 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1498 pedwarn ("ISO C forbids subscripting `register' array");
1499 else if (! flag_isoc99 && ! lvalue_p (foo))
1500 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1503 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1504 rval = build (ARRAY_REF, type, array, index);
1505 /* Array ref is const/volatile if the array elements are
1506 or if the array is. */
1507 TREE_READONLY (rval)
1508 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1509 | TREE_READONLY (array));
1510 TREE_SIDE_EFFECTS (rval)
1511 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1512 | TREE_SIDE_EFFECTS (array));
1513 TREE_THIS_VOLATILE (rval)
1514 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1515 /* This was added by rms on 16 Nov 91.
1516 It fixes vol struct foo *a; a->elts[1]
1517 in an inline function.
1518 Hope it doesn't break something else. */
1519 | TREE_THIS_VOLATILE (array));
1520 return require_complete_type (fold (rval));
1524 tree ar = default_conversion (array);
1525 tree ind = default_conversion (index);
1527 /* Do the same warning check as above, but only on the part that's
1528 syntactically the index and only if it is also semantically
1529 the index. */
1530 if (warn_char_subscripts
1531 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1532 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1533 warning ("subscript has type `char'");
1535 /* Put the integer in IND to simplify error checking. */
1536 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1538 tree temp = ar;
1539 ar = ind;
1540 ind = temp;
1543 if (ar == error_mark_node)
1544 return ar;
1546 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1547 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1549 error ("subscripted value is neither array nor pointer");
1550 return error_mark_node;
1552 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1554 error ("array subscript is not an integer");
1555 return error_mark_node;
1558 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1559 "array indexing");
1563 /* Build an external reference to identifier ID. FUN indicates
1564 whether this will be used for a function call. */
1565 tree
1566 build_external_ref (tree id, int fun)
1568 tree ref;
1569 tree decl = lookup_name (id);
1570 tree objc_ivar = lookup_objc_ivar (id);
1572 if (decl && decl != error_mark_node)
1574 /* Properly declared variable or function reference. */
1575 if (!objc_ivar)
1576 ref = decl;
1577 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1579 warning ("local declaration of `%s' hides instance variable",
1580 IDENTIFIER_POINTER (id));
1581 ref = decl;
1583 else
1584 ref = objc_ivar;
1586 else if (objc_ivar)
1587 ref = objc_ivar;
1588 else if (fun)
1589 /* Implicit function declaration. */
1590 ref = implicitly_declare (id);
1591 else if (decl == error_mark_node)
1592 /* Don't complain about something that's already been
1593 complained about. */
1594 return error_mark_node;
1595 else
1597 undeclared_variable (id);
1598 return error_mark_node;
1601 if (TREE_TYPE (ref) == error_mark_node)
1602 return error_mark_node;
1604 if (TREE_DEPRECATED (ref))
1605 warn_deprecated_use (ref);
1607 if (!skip_evaluation)
1608 assemble_external (ref);
1609 TREE_USED (ref) = 1;
1611 if (TREE_CODE (ref) == CONST_DECL)
1613 ref = DECL_INITIAL (ref);
1614 TREE_CONSTANT (ref) = 1;
1616 else if (current_function_decl != 0
1617 && !DECL_FILE_SCOPE_P (current_function_decl)
1618 && (TREE_CODE (ref) == VAR_DECL
1619 || TREE_CODE (ref) == PARM_DECL
1620 || TREE_CODE (ref) == FUNCTION_DECL))
1622 tree context = decl_function_context (ref);
1624 if (context != 0 && context != current_function_decl)
1625 DECL_NONLOCAL (ref) = 1;
1628 return ref;
1631 /* Build a function call to function FUNCTION with parameters PARAMS.
1632 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1633 TREE_VALUE of each node is a parameter-expression.
1634 FUNCTION's data type may be a function type or a pointer-to-function. */
1636 tree
1637 build_function_call (tree function, tree params)
1639 tree fntype, fundecl = 0;
1640 tree coerced_params;
1641 tree name = NULL_TREE, result;
1643 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1644 STRIP_TYPE_NOPS (function);
1646 /* Convert anything with function type to a pointer-to-function. */
1647 if (TREE_CODE (function) == FUNCTION_DECL)
1649 name = DECL_NAME (function);
1651 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1652 (because calling an inline function does not mean the function
1653 needs to be separately compiled). */
1654 fntype = build_type_variant (TREE_TYPE (function),
1655 TREE_READONLY (function),
1656 TREE_THIS_VOLATILE (function));
1657 fundecl = function;
1658 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1660 else
1661 function = default_conversion (function);
1663 fntype = TREE_TYPE (function);
1665 if (TREE_CODE (fntype) == ERROR_MARK)
1666 return error_mark_node;
1668 if (!(TREE_CODE (fntype) == POINTER_TYPE
1669 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1671 error ("called object is not a function");
1672 return error_mark_node;
1675 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1676 current_function_returns_abnormally = 1;
1678 /* fntype now gets the type of function pointed to. */
1679 fntype = TREE_TYPE (fntype);
1681 /* Convert the parameters to the types declared in the
1682 function prototype, or apply default promotions. */
1684 coerced_params
1685 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1687 /* Check that the arguments to the function are valid. */
1689 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1691 /* Recognize certain built-in functions so we can make tree-codes
1692 other than CALL_EXPR. We do this when it enables fold-const.c
1693 to do something useful. */
1695 if (TREE_CODE (function) == ADDR_EXPR
1696 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1697 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1699 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1700 params, coerced_params);
1701 if (result)
1702 return result;
1705 result = build (CALL_EXPR, TREE_TYPE (fntype),
1706 function, coerced_params, NULL_TREE);
1707 TREE_SIDE_EFFECTS (result) = 1;
1708 result = fold (result);
1710 if (VOID_TYPE_P (TREE_TYPE (result)))
1711 return result;
1712 return require_complete_type (result);
1715 /* Convert the argument expressions in the list VALUES
1716 to the types in the list TYPELIST. The result is a list of converted
1717 argument expressions.
1719 If TYPELIST is exhausted, or when an element has NULL as its type,
1720 perform the default conversions.
1722 PARMLIST is the chain of parm decls for the function being called.
1723 It may be 0, if that info is not available.
1724 It is used only for generating error messages.
1726 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1728 This is also where warnings about wrong number of args are generated.
1730 Both VALUES and the returned value are chains of TREE_LIST nodes
1731 with the elements of the list in the TREE_VALUE slots of those nodes. */
1733 static tree
1734 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1736 tree typetail, valtail;
1737 tree result = NULL;
1738 int parmnum;
1740 /* Scan the given expressions and types, producing individual
1741 converted arguments and pushing them on RESULT in reverse order. */
1743 for (valtail = values, typetail = typelist, parmnum = 0;
1744 valtail;
1745 valtail = TREE_CHAIN (valtail), parmnum++)
1747 tree type = typetail ? TREE_VALUE (typetail) : 0;
1748 tree val = TREE_VALUE (valtail);
1750 if (type == void_type_node)
1752 if (name)
1753 error ("too many arguments to function `%s'",
1754 IDENTIFIER_POINTER (name));
1755 else
1756 error ("too many arguments to function");
1757 break;
1760 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1761 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1762 to convert automatically to a pointer. */
1763 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1764 val = TREE_OPERAND (val, 0);
1766 val = default_function_array_conversion (val);
1768 val = require_complete_type (val);
1770 if (type != 0)
1772 /* Formal parm type is specified by a function prototype. */
1773 tree parmval;
1775 if (!COMPLETE_TYPE_P (type))
1777 error ("type of formal parameter %d is incomplete", parmnum + 1);
1778 parmval = val;
1780 else
1782 /* Optionally warn about conversions that
1783 differ from the default conversions. */
1784 if (warn_conversion || warn_traditional)
1786 int formal_prec = TYPE_PRECISION (type);
1788 if (INTEGRAL_TYPE_P (type)
1789 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1790 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1791 if (INTEGRAL_TYPE_P (type)
1792 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1793 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1794 else if (TREE_CODE (type) == COMPLEX_TYPE
1795 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1796 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1797 else if (TREE_CODE (type) == REAL_TYPE
1798 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1799 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1800 else if (TREE_CODE (type) == COMPLEX_TYPE
1801 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1802 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1803 else if (TREE_CODE (type) == REAL_TYPE
1804 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1805 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1806 /* ??? At some point, messages should be written about
1807 conversions between complex types, but that's too messy
1808 to do now. */
1809 else if (TREE_CODE (type) == REAL_TYPE
1810 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1812 /* Warn if any argument is passed as `float',
1813 since without a prototype it would be `double'. */
1814 if (formal_prec == TYPE_PRECISION (float_type_node))
1815 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1817 /* Detect integer changing in width or signedness.
1818 These warnings are only activated with
1819 -Wconversion, not with -Wtraditional. */
1820 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1821 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1823 tree would_have_been = default_conversion (val);
1824 tree type1 = TREE_TYPE (would_have_been);
1826 if (TREE_CODE (type) == ENUMERAL_TYPE
1827 && (TYPE_MAIN_VARIANT (type)
1828 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1829 /* No warning if function asks for enum
1830 and the actual arg is that enum type. */
1832 else if (formal_prec != TYPE_PRECISION (type1))
1833 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1834 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1836 /* Don't complain if the formal parameter type
1837 is an enum, because we can't tell now whether
1838 the value was an enum--even the same enum. */
1839 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1841 else if (TREE_CODE (val) == INTEGER_CST
1842 && int_fits_type_p (val, type))
1843 /* Change in signedness doesn't matter
1844 if a constant value is unaffected. */
1846 /* Likewise for a constant in a NOP_EXPR. */
1847 else if (TREE_CODE (val) == NOP_EXPR
1848 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1849 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1851 /* If the value is extended from a narrower
1852 unsigned type, it doesn't matter whether we
1853 pass it as signed or unsigned; the value
1854 certainly is the same either way. */
1855 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1856 && TREE_UNSIGNED (TREE_TYPE (val)))
1858 else if (TREE_UNSIGNED (type))
1859 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1860 else
1861 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1865 parmval = convert_for_assignment (type, val,
1866 (char *) 0, /* arg passing */
1867 fundecl, name, parmnum + 1);
1869 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1870 && INTEGRAL_TYPE_P (type)
1871 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1872 parmval = default_conversion (parmval);
1874 result = tree_cons (NULL_TREE, parmval, result);
1876 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1877 && (TYPE_PRECISION (TREE_TYPE (val))
1878 < TYPE_PRECISION (double_type_node)))
1879 /* Convert `float' to `double'. */
1880 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1881 else
1882 /* Convert `short' and `char' to full-size `int'. */
1883 result = tree_cons (NULL_TREE, default_conversion (val), result);
1885 if (typetail)
1886 typetail = TREE_CHAIN (typetail);
1889 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1891 if (name)
1892 error ("too few arguments to function `%s'",
1893 IDENTIFIER_POINTER (name));
1894 else
1895 error ("too few arguments to function");
1898 return nreverse (result);
1901 /* This is the entry point used by the parser
1902 for binary operators in the input.
1903 In addition to constructing the expression,
1904 we check for operands that were written with other binary operators
1905 in a way that is likely to confuse the user. */
1907 tree
1908 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1910 tree result = build_binary_op (code, arg1, arg2, 1);
1912 char class;
1913 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1914 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1915 enum tree_code code1 = ERROR_MARK;
1916 enum tree_code code2 = ERROR_MARK;
1918 if (TREE_CODE (result) == ERROR_MARK)
1919 return error_mark_node;
1921 if (IS_EXPR_CODE_CLASS (class1))
1922 code1 = C_EXP_ORIGINAL_CODE (arg1);
1923 if (IS_EXPR_CODE_CLASS (class2))
1924 code2 = C_EXP_ORIGINAL_CODE (arg2);
1926 /* Check for cases such as x+y<<z which users are likely
1927 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1928 is cleared to prevent these warnings. */
1929 if (warn_parentheses)
1931 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1933 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1934 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1935 warning ("suggest parentheses around + or - inside shift");
1938 if (code == TRUTH_ORIF_EXPR)
1940 if (code1 == TRUTH_ANDIF_EXPR
1941 || code2 == TRUTH_ANDIF_EXPR)
1942 warning ("suggest parentheses around && within ||");
1945 if (code == BIT_IOR_EXPR)
1947 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1948 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1949 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1950 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1951 warning ("suggest parentheses around arithmetic in operand of |");
1952 /* Check cases like x|y==z */
1953 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1954 warning ("suggest parentheses around comparison in operand of |");
1957 if (code == BIT_XOR_EXPR)
1959 if (code1 == BIT_AND_EXPR
1960 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1961 || code2 == BIT_AND_EXPR
1962 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1963 warning ("suggest parentheses around arithmetic in operand of ^");
1964 /* Check cases like x^y==z */
1965 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1966 warning ("suggest parentheses around comparison in operand of ^");
1969 if (code == BIT_AND_EXPR)
1971 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1972 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1973 warning ("suggest parentheses around + or - in operand of &");
1974 /* Check cases like x&y==z */
1975 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1976 warning ("suggest parentheses around comparison in operand of &");
1980 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1981 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1982 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1983 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1985 unsigned_conversion_warning (result, arg1);
1986 unsigned_conversion_warning (result, arg2);
1987 overflow_warning (result);
1989 class = TREE_CODE_CLASS (TREE_CODE (result));
1991 /* Record the code that was specified in the source,
1992 for the sake of warnings about confusing nesting. */
1993 if (IS_EXPR_CODE_CLASS (class))
1994 C_SET_EXP_ORIGINAL_CODE (result, code);
1995 else
1997 int flag = TREE_CONSTANT (result);
1998 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1999 so that convert_for_assignment wouldn't strip it.
2000 That way, we got warnings for things like p = (1 - 1).
2001 But it turns out we should not get those warnings. */
2002 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2003 C_SET_EXP_ORIGINAL_CODE (result, code);
2004 TREE_CONSTANT (result) = flag;
2007 return result;
2011 /* Return true if `t' is known to be non-negative. */
2014 c_tree_expr_nonnegative_p (tree t)
2016 if (TREE_CODE (t) == STMT_EXPR)
2018 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2020 /* Find the last statement in the chain, ignoring the final
2021 * scope statement */
2022 while (TREE_CHAIN (t) != NULL_TREE
2023 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2024 t = TREE_CHAIN (t);
2025 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2027 return tree_expr_nonnegative_p (t);
2030 /* Return a tree for the difference of pointers OP0 and OP1.
2031 The resulting tree has type int. */
2033 static tree
2034 pointer_diff (tree op0, tree op1)
2036 tree result, folded;
2037 tree restype = ptrdiff_type_node;
2039 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2040 tree con0, con1, lit0, lit1;
2041 tree orig_op1 = op1;
2043 if (pedantic || warn_pointer_arith)
2045 if (TREE_CODE (target_type) == VOID_TYPE)
2046 pedwarn ("pointer of type `void *' used in subtraction");
2047 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2048 pedwarn ("pointer to a function used in subtraction");
2051 /* If the conversion to ptrdiff_type does anything like widening or
2052 converting a partial to an integral mode, we get a convert_expression
2053 that is in the way to do any simplifications.
2054 (fold-const.c doesn't know that the extra bits won't be needed.
2055 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2056 different mode in place.)
2057 So first try to find a common term here 'by hand'; we want to cover
2058 at least the cases that occur in legal static initializers. */
2059 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2060 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2062 if (TREE_CODE (con0) == PLUS_EXPR)
2064 lit0 = TREE_OPERAND (con0, 1);
2065 con0 = TREE_OPERAND (con0, 0);
2067 else
2068 lit0 = integer_zero_node;
2070 if (TREE_CODE (con1) == PLUS_EXPR)
2072 lit1 = TREE_OPERAND (con1, 1);
2073 con1 = TREE_OPERAND (con1, 0);
2075 else
2076 lit1 = integer_zero_node;
2078 if (operand_equal_p (con0, con1, 0))
2080 op0 = lit0;
2081 op1 = lit1;
2085 /* First do the subtraction as integers;
2086 then drop through to build the divide operator.
2087 Do not do default conversions on the minus operator
2088 in case restype is a short type. */
2090 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2091 convert (restype, op1), 0);
2092 /* This generates an error if op1 is pointer to incomplete type. */
2093 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2094 error ("arithmetic on pointer to an incomplete type");
2096 /* This generates an error if op0 is pointer to incomplete type. */
2097 op1 = c_size_in_bytes (target_type);
2099 /* Divide by the size, in easiest possible way. */
2101 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2103 folded = fold (result);
2104 if (folded == result)
2105 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2106 return folded;
2109 /* Construct and perhaps optimize a tree representation
2110 for a unary operation. CODE, a tree_code, specifies the operation
2111 and XARG is the operand.
2112 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2113 the default promotions (such as from short to int).
2114 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2115 allows non-lvalues; this is only used to handle conversion of non-lvalue
2116 arrays to pointers in C99. */
2118 tree
2119 build_unary_op (enum tree_code code, tree xarg, int flag)
2121 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2122 tree arg = xarg;
2123 tree argtype = 0;
2124 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2125 tree val;
2126 int noconvert = flag;
2128 if (typecode == ERROR_MARK)
2129 return error_mark_node;
2130 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2131 typecode = INTEGER_TYPE;
2133 switch (code)
2135 case CONVERT_EXPR:
2136 /* This is used for unary plus, because a CONVERT_EXPR
2137 is enough to prevent anybody from looking inside for
2138 associativity, but won't generate any code. */
2139 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2140 || typecode == COMPLEX_TYPE))
2142 error ("wrong type argument to unary plus");
2143 return error_mark_node;
2145 else if (!noconvert)
2146 arg = default_conversion (arg);
2147 arg = non_lvalue (arg);
2148 break;
2150 case NEGATE_EXPR:
2151 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2152 || typecode == COMPLEX_TYPE
2153 || typecode == VECTOR_TYPE))
2155 error ("wrong type argument to unary minus");
2156 return error_mark_node;
2158 else if (!noconvert)
2159 arg = default_conversion (arg);
2160 break;
2162 case BIT_NOT_EXPR:
2163 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2165 if (!noconvert)
2166 arg = default_conversion (arg);
2168 else if (typecode == COMPLEX_TYPE)
2170 code = CONJ_EXPR;
2171 if (pedantic)
2172 pedwarn ("ISO C does not support `~' for complex conjugation");
2173 if (!noconvert)
2174 arg = default_conversion (arg);
2176 else
2178 error ("wrong type argument to bit-complement");
2179 return error_mark_node;
2181 break;
2183 case ABS_EXPR:
2184 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2186 error ("wrong type argument to abs");
2187 return error_mark_node;
2189 else if (!noconvert)
2190 arg = default_conversion (arg);
2191 break;
2193 case CONJ_EXPR:
2194 /* Conjugating a real value is a no-op, but allow it anyway. */
2195 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2196 || typecode == COMPLEX_TYPE))
2198 error ("wrong type argument to conjugation");
2199 return error_mark_node;
2201 else if (!noconvert)
2202 arg = default_conversion (arg);
2203 break;
2205 case TRUTH_NOT_EXPR:
2206 if (typecode != INTEGER_TYPE
2207 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2208 && typecode != COMPLEX_TYPE
2209 /* These will convert to a pointer. */
2210 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2212 error ("wrong type argument to unary exclamation mark");
2213 return error_mark_node;
2215 arg = c_common_truthvalue_conversion (arg);
2216 return invert_truthvalue (arg);
2218 case NOP_EXPR:
2219 break;
2221 case REALPART_EXPR:
2222 if (TREE_CODE (arg) == COMPLEX_CST)
2223 return TREE_REALPART (arg);
2224 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2225 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2226 else
2227 return arg;
2229 case IMAGPART_EXPR:
2230 if (TREE_CODE (arg) == COMPLEX_CST)
2231 return TREE_IMAGPART (arg);
2232 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2233 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2234 else
2235 return convert (TREE_TYPE (arg), integer_zero_node);
2237 case PREINCREMENT_EXPR:
2238 case POSTINCREMENT_EXPR:
2239 case PREDECREMENT_EXPR:
2240 case POSTDECREMENT_EXPR:
2241 /* Handle complex lvalues (when permitted)
2242 by reduction to simpler cases. */
2244 val = unary_complex_lvalue (code, arg, 0);
2245 if (val != 0)
2246 return val;
2248 /* Increment or decrement the real part of the value,
2249 and don't change the imaginary part. */
2250 if (typecode == COMPLEX_TYPE)
2252 tree real, imag;
2254 if (pedantic)
2255 pedwarn ("ISO C does not support `++' and `--' on complex types");
2257 arg = stabilize_reference (arg);
2258 real = build_unary_op (REALPART_EXPR, arg, 1);
2259 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2260 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2261 build_unary_op (code, real, 1), imag);
2264 /* Report invalid types. */
2266 if (typecode != POINTER_TYPE
2267 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2269 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2270 error ("wrong type argument to increment");
2271 else
2272 error ("wrong type argument to decrement");
2274 return error_mark_node;
2278 tree inc;
2279 tree result_type = TREE_TYPE (arg);
2281 arg = get_unwidened (arg, 0);
2282 argtype = TREE_TYPE (arg);
2284 /* Compute the increment. */
2286 if (typecode == POINTER_TYPE)
2288 /* If pointer target is an undefined struct,
2289 we just cannot know how to do the arithmetic. */
2290 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2292 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2293 error ("increment of pointer to unknown structure");
2294 else
2295 error ("decrement of pointer to unknown structure");
2297 else if ((pedantic || warn_pointer_arith)
2298 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2299 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2301 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2302 pedwarn ("wrong type argument to increment");
2303 else
2304 pedwarn ("wrong type argument to decrement");
2307 inc = c_size_in_bytes (TREE_TYPE (result_type));
2309 else
2310 inc = integer_one_node;
2312 inc = convert (argtype, inc);
2314 /* Handle incrementing a cast-expression. */
2316 while (1)
2317 switch (TREE_CODE (arg))
2319 case NOP_EXPR:
2320 case CONVERT_EXPR:
2321 case FLOAT_EXPR:
2322 case FIX_TRUNC_EXPR:
2323 case FIX_FLOOR_EXPR:
2324 case FIX_ROUND_EXPR:
2325 case FIX_CEIL_EXPR:
2326 pedantic_lvalue_warning (CONVERT_EXPR);
2327 /* If the real type has the same machine representation
2328 as the type it is cast to, we can make better output
2329 by adding directly to the inside of the cast. */
2330 if ((TREE_CODE (TREE_TYPE (arg))
2331 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2332 && (TYPE_MODE (TREE_TYPE (arg))
2333 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2334 arg = TREE_OPERAND (arg, 0);
2335 else
2337 tree incremented, modify, value;
2338 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2339 value = boolean_increment (code, arg);
2340 else
2342 arg = stabilize_reference (arg);
2343 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2344 value = arg;
2345 else
2346 value = save_expr (arg);
2347 incremented = build (((code == PREINCREMENT_EXPR
2348 || code == POSTINCREMENT_EXPR)
2349 ? PLUS_EXPR : MINUS_EXPR),
2350 argtype, value, inc);
2351 TREE_SIDE_EFFECTS (incremented) = 1;
2352 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2353 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2355 TREE_USED (value) = 1;
2356 return value;
2358 break;
2360 default:
2361 goto give_up;
2363 give_up:
2365 /* Complain about anything else that is not a true lvalue. */
2366 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2367 || code == POSTINCREMENT_EXPR)
2368 ? "invalid lvalue in increment"
2369 : "invalid lvalue in decrement")))
2370 return error_mark_node;
2372 /* Report a read-only lvalue. */
2373 if (TREE_READONLY (arg))
2374 readonly_warning (arg,
2375 ((code == PREINCREMENT_EXPR
2376 || code == POSTINCREMENT_EXPR)
2377 ? "increment" : "decrement"));
2379 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2380 val = boolean_increment (code, arg);
2381 else
2382 val = build (code, TREE_TYPE (arg), arg, inc);
2383 TREE_SIDE_EFFECTS (val) = 1;
2384 val = convert (result_type, val);
2385 if (TREE_CODE (val) != code)
2386 TREE_NO_UNUSED_WARNING (val) = 1;
2387 return val;
2390 case ADDR_EXPR:
2391 /* Note that this operation never does default_conversion. */
2393 /* Let &* cancel out to simplify resulting code. */
2394 if (TREE_CODE (arg) == INDIRECT_REF)
2396 /* Don't let this be an lvalue. */
2397 if (lvalue_p (TREE_OPERAND (arg, 0)))
2398 return non_lvalue (TREE_OPERAND (arg, 0));
2399 return TREE_OPERAND (arg, 0);
2402 /* For &x[y], return x+y */
2403 if (TREE_CODE (arg) == ARRAY_REF)
2405 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2406 return error_mark_node;
2407 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2408 TREE_OPERAND (arg, 1), 1);
2411 /* Handle complex lvalues (when permitted)
2412 by reduction to simpler cases. */
2413 val = unary_complex_lvalue (code, arg, flag);
2414 if (val != 0)
2415 return val;
2417 /* Anything not already handled and not a true memory reference
2418 or a non-lvalue array is an error. */
2419 else if (typecode != FUNCTION_TYPE && !flag
2420 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2421 return error_mark_node;
2423 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2424 argtype = TREE_TYPE (arg);
2426 /* If the lvalue is const or volatile, merge that into the type
2427 to which the address will point. Note that you can't get a
2428 restricted pointer by taking the address of something, so we
2429 only have to deal with `const' and `volatile' here. */
2430 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2431 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2432 argtype = c_build_type_variant (argtype,
2433 TREE_READONLY (arg),
2434 TREE_THIS_VOLATILE (arg));
2436 argtype = build_pointer_type (argtype);
2438 if (!c_mark_addressable (arg))
2439 return error_mark_node;
2442 tree addr;
2444 if (TREE_CODE (arg) == COMPONENT_REF)
2446 tree field = TREE_OPERAND (arg, 1);
2448 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2450 if (DECL_C_BIT_FIELD (field))
2452 error ("attempt to take address of bit-field structure member `%s'",
2453 IDENTIFIER_POINTER (DECL_NAME (field)));
2454 return error_mark_node;
2457 addr = fold (build (PLUS_EXPR, argtype,
2458 convert (argtype, addr),
2459 convert (argtype, byte_position (field))));
2461 else
2462 addr = build1 (code, argtype, arg);
2464 /* Address of a static or external variable or
2465 file-scope function counts as a constant. */
2466 if (staticp (arg)
2467 && ! (TREE_CODE (arg) == FUNCTION_DECL
2468 && !DECL_FILE_SCOPE_P (arg)))
2469 TREE_CONSTANT (addr) = 1;
2470 return addr;
2473 default:
2474 break;
2477 if (argtype == 0)
2478 argtype = TREE_TYPE (arg);
2479 return fold (build1 (code, argtype, arg));
2482 /* Return nonzero if REF is an lvalue valid for this language.
2483 Lvalues can be assigned, unless their type has TYPE_READONLY.
2484 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2487 lvalue_p (tree ref)
2489 enum tree_code code = TREE_CODE (ref);
2491 switch (code)
2493 case REALPART_EXPR:
2494 case IMAGPART_EXPR:
2495 case COMPONENT_REF:
2496 return lvalue_p (TREE_OPERAND (ref, 0));
2498 case COMPOUND_LITERAL_EXPR:
2499 case STRING_CST:
2500 return 1;
2502 case INDIRECT_REF:
2503 case ARRAY_REF:
2504 case VAR_DECL:
2505 case PARM_DECL:
2506 case RESULT_DECL:
2507 case ERROR_MARK:
2508 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2509 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2511 case BIND_EXPR:
2512 case RTL_EXPR:
2513 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2515 default:
2516 return 0;
2520 /* Return nonzero if REF is an lvalue valid for this language;
2521 otherwise, print an error message and return zero. */
2524 lvalue_or_else (tree ref, const char *msgid)
2526 int win = lvalue_p (ref);
2528 if (! win)
2529 error ("%s", msgid);
2531 return win;
2534 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2535 for certain kinds of expressions which are not really lvalues
2536 but which we can accept as lvalues. If FLAG is nonzero, then
2537 non-lvalues are OK since we may be converting a non-lvalue array to
2538 a pointer in C99.
2540 If ARG is not a kind of expression we can handle, return zero. */
2542 static tree
2543 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2545 /* Handle (a, b) used as an "lvalue". */
2546 if (TREE_CODE (arg) == COMPOUND_EXPR)
2548 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2550 /* If this returns a function type, it isn't really being used as
2551 an lvalue, so don't issue a warning about it. */
2552 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2553 pedantic_lvalue_warning (COMPOUND_EXPR);
2555 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2556 TREE_OPERAND (arg, 0), real_result);
2559 /* Handle (a ? b : c) used as an "lvalue". */
2560 if (TREE_CODE (arg) == COND_EXPR)
2562 if (!flag)
2563 pedantic_lvalue_warning (COND_EXPR);
2564 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2565 pedantic_lvalue_warning (COMPOUND_EXPR);
2567 return (build_conditional_expr
2568 (TREE_OPERAND (arg, 0),
2569 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2570 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2573 return 0;
2576 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2577 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2579 static void
2580 pedantic_lvalue_warning (enum tree_code code)
2582 if (pedantic)
2583 switch (code)
2585 case COND_EXPR:
2586 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
2587 break;
2588 case COMPOUND_EXPR:
2589 pedwarn ("ISO C forbids use of compound expressions as lvalues");
2590 break;
2591 default:
2592 pedwarn ("ISO C forbids use of cast expressions as lvalues");
2593 break;
2597 /* Warn about storing in something that is `const'. */
2599 void
2600 readonly_warning (tree arg, const char *msgid)
2602 if (TREE_CODE (arg) == COMPONENT_REF)
2604 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2605 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2606 else
2607 pedwarn ("%s of read-only member `%s'", _(msgid),
2608 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2610 else if (TREE_CODE (arg) == VAR_DECL)
2611 pedwarn ("%s of read-only variable `%s'", _(msgid),
2612 IDENTIFIER_POINTER (DECL_NAME (arg)));
2613 else
2614 pedwarn ("%s of read-only location", _(msgid));
2617 /* Mark EXP saying that we need to be able to take the
2618 address of it; it should not be allocated in a register.
2619 Returns true if successful. */
2621 bool
2622 c_mark_addressable (tree exp)
2624 tree x = exp;
2626 while (1)
2627 switch (TREE_CODE (x))
2629 case COMPONENT_REF:
2630 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2632 error ("cannot take address of bit-field `%s'",
2633 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2634 return false;
2637 /* ... fall through ... */
2639 case ADDR_EXPR:
2640 case ARRAY_REF:
2641 case REALPART_EXPR:
2642 case IMAGPART_EXPR:
2643 x = TREE_OPERAND (x, 0);
2644 break;
2646 case COMPOUND_LITERAL_EXPR:
2647 case CONSTRUCTOR:
2648 TREE_ADDRESSABLE (x) = 1;
2649 return true;
2651 case VAR_DECL:
2652 case CONST_DECL:
2653 case PARM_DECL:
2654 case RESULT_DECL:
2655 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2656 && DECL_NONLOCAL (x))
2658 if (TREE_PUBLIC (x))
2660 error ("global register variable `%s' used in nested function",
2661 IDENTIFIER_POINTER (DECL_NAME (x)));
2662 return false;
2664 pedwarn ("register variable `%s' used in nested function",
2665 IDENTIFIER_POINTER (DECL_NAME (x)));
2667 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2669 if (TREE_PUBLIC (x))
2671 error ("address of global register variable `%s' requested",
2672 IDENTIFIER_POINTER (DECL_NAME (x)));
2673 return false;
2676 /* If we are making this addressable due to its having
2677 volatile components, give a different error message. Also
2678 handle the case of an unnamed parameter by not trying
2679 to give the name. */
2681 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2683 error ("cannot put object with volatile field into register");
2684 return false;
2687 pedwarn ("address of register variable `%s' requested",
2688 IDENTIFIER_POINTER (DECL_NAME (x)));
2690 put_var_into_stack (x, /*rescan=*/true);
2692 /* drops in */
2693 case FUNCTION_DECL:
2694 TREE_ADDRESSABLE (x) = 1;
2695 /* drops out */
2696 default:
2697 return true;
2701 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2703 tree
2704 build_conditional_expr (tree ifexp, tree op1, tree op2)
2706 tree type1;
2707 tree type2;
2708 enum tree_code code1;
2709 enum tree_code code2;
2710 tree result_type = NULL;
2711 tree orig_op1 = op1, orig_op2 = op2;
2713 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2715 /* Promote both alternatives. */
2717 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2718 op1 = default_conversion (op1);
2719 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2720 op2 = default_conversion (op2);
2722 if (TREE_CODE (ifexp) == ERROR_MARK
2723 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2724 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2725 return error_mark_node;
2727 type1 = TREE_TYPE (op1);
2728 code1 = TREE_CODE (type1);
2729 type2 = TREE_TYPE (op2);
2730 code2 = TREE_CODE (type2);
2732 /* Quickly detect the usual case where op1 and op2 have the same type
2733 after promotion. */
2734 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2736 if (type1 == type2)
2737 result_type = type1;
2738 else
2739 result_type = TYPE_MAIN_VARIANT (type1);
2741 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2742 || code1 == COMPLEX_TYPE)
2743 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2744 || code2 == COMPLEX_TYPE))
2746 result_type = common_type (type1, type2);
2748 /* If -Wsign-compare, warn here if type1 and type2 have
2749 different signedness. We'll promote the signed to unsigned
2750 and later code won't know it used to be different.
2751 Do this check on the original types, so that explicit casts
2752 will be considered, but default promotions won't. */
2753 if (warn_sign_compare && !skip_evaluation)
2755 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2756 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2758 if (unsigned_op1 ^ unsigned_op2)
2760 /* Do not warn if the result type is signed, since the
2761 signed type will only be chosen if it can represent
2762 all the values of the unsigned type. */
2763 if (! TREE_UNSIGNED (result_type))
2764 /* OK */;
2765 /* Do not warn if the signed quantity is an unsuffixed
2766 integer literal (or some static constant expression
2767 involving such literals) and it is non-negative. */
2768 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2769 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2770 /* OK */;
2771 else
2772 warning ("signed and unsigned type in conditional expression");
2776 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2778 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2779 pedwarn ("ISO C forbids conditional expr with only one void side");
2780 result_type = void_type_node;
2782 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2784 if (comp_target_types (type1, type2, 1))
2785 result_type = common_type (type1, type2);
2786 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2787 && TREE_CODE (orig_op1) != NOP_EXPR)
2788 result_type = qualify_type (type2, type1);
2789 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2790 && TREE_CODE (orig_op2) != NOP_EXPR)
2791 result_type = qualify_type (type1, type2);
2792 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2794 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2795 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2796 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2797 TREE_TYPE (type2)));
2799 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2801 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2802 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2803 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2804 TREE_TYPE (type1)));
2806 else
2808 pedwarn ("pointer type mismatch in conditional expression");
2809 result_type = build_pointer_type (void_type_node);
2812 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2814 if (! integer_zerop (op2))
2815 pedwarn ("pointer/integer type mismatch in conditional expression");
2816 else
2818 op2 = null_pointer_node;
2820 result_type = type1;
2822 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2824 if (!integer_zerop (op1))
2825 pedwarn ("pointer/integer type mismatch in conditional expression");
2826 else
2828 op1 = null_pointer_node;
2830 result_type = type2;
2833 if (!result_type)
2835 if (flag_cond_mismatch)
2836 result_type = void_type_node;
2837 else
2839 error ("type mismatch in conditional expression");
2840 return error_mark_node;
2844 /* Merge const and volatile flags of the incoming types. */
2845 result_type
2846 = build_type_variant (result_type,
2847 TREE_READONLY (op1) || TREE_READONLY (op2),
2848 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2850 if (result_type != TREE_TYPE (op1))
2851 op1 = convert_and_check (result_type, op1);
2852 if (result_type != TREE_TYPE (op2))
2853 op2 = convert_and_check (result_type, op2);
2855 if (TREE_CODE (ifexp) == INTEGER_CST)
2856 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2858 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2861 /* Given a list of expressions, return a compound expression
2862 that performs them all and returns the value of the last of them. */
2864 tree
2865 build_compound_expr (tree list)
2867 return internal_build_compound_expr (list, TRUE);
2870 static tree
2871 internal_build_compound_expr (tree list, int first_p)
2873 tree rest;
2875 if (TREE_CHAIN (list) == 0)
2877 /* Convert arrays and functions to pointers when there
2878 really is a comma operator. */
2879 if (!first_p)
2880 TREE_VALUE (list)
2881 = default_function_array_conversion (TREE_VALUE (list));
2883 /* Don't let (0, 0) be null pointer constant. */
2884 if (!first_p && integer_zerop (TREE_VALUE (list)))
2885 return non_lvalue (TREE_VALUE (list));
2886 return TREE_VALUE (list);
2889 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2891 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2893 /* The left-hand operand of a comma expression is like an expression
2894 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2895 any side-effects, unless it was explicitly cast to (void). */
2896 if (warn_unused_value
2897 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2898 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2899 warning ("left-hand operand of comma expression has no effect");
2901 /* When pedantic, a compound expression can be neither an lvalue
2902 nor an integer constant expression. */
2903 if (! pedantic)
2904 return rest;
2907 /* With -Wunused, we should also warn if the left-hand operand does have
2908 side-effects, but computes a value which is not used. For example, in
2909 `foo() + bar(), baz()' the result of the `+' operator is not used,
2910 so we should issue a warning. */
2911 else if (warn_unused_value)
2912 warn_if_unused_value (TREE_VALUE (list));
2914 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2917 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2919 tree
2920 build_c_cast (tree type, tree expr)
2922 tree value = expr;
2924 if (type == error_mark_node || expr == error_mark_node)
2925 return error_mark_node;
2927 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2928 only in <protocol> qualifications. But when constructing cast expressions,
2929 the protocols do matter and must be kept around. */
2930 if (!c_dialect_objc () || !objc_is_id (type))
2931 type = TYPE_MAIN_VARIANT (type);
2933 if (TREE_CODE (type) == ARRAY_TYPE)
2935 error ("cast specifies array type");
2936 return error_mark_node;
2939 if (TREE_CODE (type) == FUNCTION_TYPE)
2941 error ("cast specifies function type");
2942 return error_mark_node;
2945 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2947 if (pedantic)
2949 if (TREE_CODE (type) == RECORD_TYPE
2950 || TREE_CODE (type) == UNION_TYPE)
2951 pedwarn ("ISO C forbids casting nonscalar to the same type");
2954 else if (TREE_CODE (type) == UNION_TYPE)
2956 tree field;
2957 value = default_function_array_conversion (value);
2959 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2960 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2961 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2962 break;
2964 if (field)
2966 tree t;
2968 if (pedantic)
2969 pedwarn ("ISO C forbids casts to union type");
2970 t = digest_init (type,
2971 build_constructor (type,
2972 build_tree_list (field, value)),
2974 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2975 return t;
2977 error ("cast to union type from type not present in union");
2978 return error_mark_node;
2980 else
2982 tree otype, ovalue;
2984 /* If casting to void, avoid the error that would come
2985 from default_conversion in the case of a non-lvalue array. */
2986 if (type == void_type_node)
2987 return build1 (CONVERT_EXPR, type, value);
2989 /* Convert functions and arrays to pointers,
2990 but don't convert any other types. */
2991 value = default_function_array_conversion (value);
2992 otype = TREE_TYPE (value);
2994 /* Optionally warn about potentially worrisome casts. */
2996 if (warn_cast_qual
2997 && TREE_CODE (type) == POINTER_TYPE
2998 && TREE_CODE (otype) == POINTER_TYPE)
3000 tree in_type = type;
3001 tree in_otype = otype;
3002 int added = 0;
3003 int discarded = 0;
3005 /* Check that the qualifiers on IN_TYPE are a superset of
3006 the qualifiers of IN_OTYPE. The outermost level of
3007 POINTER_TYPE nodes is uninteresting and we stop as soon
3008 as we hit a non-POINTER_TYPE node on either type. */
3011 in_otype = TREE_TYPE (in_otype);
3012 in_type = TREE_TYPE (in_type);
3014 /* GNU C allows cv-qualified function types. 'const'
3015 means the function is very pure, 'volatile' means it
3016 can't return. We need to warn when such qualifiers
3017 are added, not when they're taken away. */
3018 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3019 && TREE_CODE (in_type) == FUNCTION_TYPE)
3020 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3021 else
3022 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3024 while (TREE_CODE (in_type) == POINTER_TYPE
3025 && TREE_CODE (in_otype) == POINTER_TYPE);
3027 if (added)
3028 warning ("cast adds new qualifiers to function type");
3030 if (discarded)
3031 /* There are qualifiers present in IN_OTYPE that are not
3032 present in IN_TYPE. */
3033 warning ("cast discards qualifiers from pointer target type");
3036 /* Warn about possible alignment problems. */
3037 if (STRICT_ALIGNMENT && warn_cast_align
3038 && TREE_CODE (type) == POINTER_TYPE
3039 && TREE_CODE (otype) == POINTER_TYPE
3040 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3041 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3042 /* Don't warn about opaque types, where the actual alignment
3043 restriction is unknown. */
3044 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3045 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3046 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3047 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3048 warning ("cast increases required alignment of target type");
3050 if (TREE_CODE (type) == INTEGER_TYPE
3051 && TREE_CODE (otype) == POINTER_TYPE
3052 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3053 && !TREE_CONSTANT (value))
3054 warning ("cast from pointer to integer of different size");
3056 if (warn_bad_function_cast
3057 && TREE_CODE (value) == CALL_EXPR
3058 && TREE_CODE (type) != TREE_CODE (otype))
3059 warning ("cast does not match function type");
3061 if (TREE_CODE (type) == POINTER_TYPE
3062 && TREE_CODE (otype) == INTEGER_TYPE
3063 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3064 /* Don't warn about converting any constant. */
3065 && !TREE_CONSTANT (value))
3066 warning ("cast to pointer from integer of different size");
3068 if (TREE_CODE (type) == POINTER_TYPE
3069 && TREE_CODE (otype) == POINTER_TYPE
3070 && TREE_CODE (expr) == ADDR_EXPR
3071 && DECL_P (TREE_OPERAND (expr, 0))
3072 && flag_strict_aliasing && warn_strict_aliasing
3073 && !VOID_TYPE_P (TREE_TYPE (type)))
3075 /* Casting the address of a decl to non void pointer. Warn
3076 if the cast breaks type based aliasing. */
3077 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3078 warning ("type-punning to incomplete type might break strict-aliasing rules");
3079 else if (!alias_sets_conflict_p
3080 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3081 get_alias_set (TREE_TYPE (type))))
3082 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3085 ovalue = value;
3086 /* Replace a nonvolatile const static variable with its value. */
3087 if (optimize && TREE_CODE (value) == VAR_DECL)
3088 value = decl_constant_value (value);
3089 value = convert (type, value);
3091 /* Ignore any integer overflow caused by the cast. */
3092 if (TREE_CODE (value) == INTEGER_CST)
3094 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3095 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3099 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3100 if (pedantic && TREE_CODE (value) == INTEGER_CST
3101 && TREE_CODE (expr) == INTEGER_CST
3102 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3103 value = non_lvalue (value);
3105 /* If pedantic, don't let a cast be an lvalue. */
3106 if (value == expr && pedantic)
3107 value = non_lvalue (value);
3109 return value;
3112 /* Interpret a cast of expression EXPR to type TYPE. */
3113 tree
3114 c_cast_expr (tree type, tree expr)
3116 int saved_wsp = warn_strict_prototypes;
3118 /* This avoids warnings about unprototyped casts on
3119 integers. E.g. "#define SIG_DFL (void(*)())0". */
3120 if (TREE_CODE (expr) == INTEGER_CST)
3121 warn_strict_prototypes = 0;
3122 type = groktypename (type);
3123 warn_strict_prototypes = saved_wsp;
3125 return build_c_cast (type, expr);
3129 /* Build an assignment expression of lvalue LHS from value RHS.
3130 MODIFYCODE is the code for a binary operator that we use
3131 to combine the old value of LHS with RHS to get the new value.
3132 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3134 tree
3135 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3137 tree result;
3138 tree newrhs;
3139 tree lhstype = TREE_TYPE (lhs);
3140 tree olhstype = lhstype;
3142 /* Types that aren't fully specified cannot be used in assignments. */
3143 lhs = require_complete_type (lhs);
3145 /* Avoid duplicate error messages from operands that had errors. */
3146 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3147 return error_mark_node;
3149 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3150 /* Do not use STRIP_NOPS here. We do not want an enumerator
3151 whose value is 0 to count as a null pointer constant. */
3152 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3153 rhs = TREE_OPERAND (rhs, 0);
3155 newrhs = rhs;
3157 /* Handle control structure constructs used as "lvalues". */
3159 switch (TREE_CODE (lhs))
3161 /* Handle (a, b) used as an "lvalue". */
3162 case COMPOUND_EXPR:
3163 pedantic_lvalue_warning (COMPOUND_EXPR);
3164 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3165 if (TREE_CODE (newrhs) == ERROR_MARK)
3166 return error_mark_node;
3167 return build (COMPOUND_EXPR, lhstype,
3168 TREE_OPERAND (lhs, 0), newrhs);
3170 /* Handle (a ? b : c) used as an "lvalue". */
3171 case COND_EXPR:
3172 pedantic_lvalue_warning (COND_EXPR);
3173 rhs = save_expr (rhs);
3175 /* Produce (a ? (b = rhs) : (c = rhs))
3176 except that the RHS goes through a save-expr
3177 so the code to compute it is only emitted once. */
3178 tree cond
3179 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3180 build_modify_expr (TREE_OPERAND (lhs, 1),
3181 modifycode, rhs),
3182 build_modify_expr (TREE_OPERAND (lhs, 2),
3183 modifycode, rhs));
3184 if (TREE_CODE (cond) == ERROR_MARK)
3185 return cond;
3186 /* Make sure the code to compute the rhs comes out
3187 before the split. */
3188 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3189 /* But cast it to void to avoid an "unused" error. */
3190 convert (void_type_node, rhs), cond);
3192 default:
3193 break;
3196 /* If a binary op has been requested, combine the old LHS value with the RHS
3197 producing the value we should actually store into the LHS. */
3199 if (modifycode != NOP_EXPR)
3201 lhs = stabilize_reference (lhs);
3202 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3205 /* Handle a cast used as an "lvalue".
3206 We have already performed any binary operator using the value as cast.
3207 Now convert the result to the cast type of the lhs,
3208 and then true type of the lhs and store it there;
3209 then convert result back to the cast type to be the value
3210 of the assignment. */
3212 switch (TREE_CODE (lhs))
3214 case NOP_EXPR:
3215 case CONVERT_EXPR:
3216 case FLOAT_EXPR:
3217 case FIX_TRUNC_EXPR:
3218 case FIX_FLOOR_EXPR:
3219 case FIX_ROUND_EXPR:
3220 case FIX_CEIL_EXPR:
3221 newrhs = default_function_array_conversion (newrhs);
3223 tree inner_lhs = TREE_OPERAND (lhs, 0);
3224 tree result;
3225 result = build_modify_expr (inner_lhs, NOP_EXPR,
3226 convert (TREE_TYPE (inner_lhs),
3227 convert (lhstype, newrhs)));
3228 if (TREE_CODE (result) == ERROR_MARK)
3229 return result;
3230 pedantic_lvalue_warning (CONVERT_EXPR);
3231 return convert (TREE_TYPE (lhs), result);
3234 default:
3235 break;
3238 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3239 Reject anything strange now. */
3241 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3242 return error_mark_node;
3244 /* Warn about storing in something that is `const'. */
3246 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3247 || ((TREE_CODE (lhstype) == RECORD_TYPE
3248 || TREE_CODE (lhstype) == UNION_TYPE)
3249 && C_TYPE_FIELDS_READONLY (lhstype)))
3250 readonly_warning (lhs, "assignment");
3252 /* If storing into a structure or union member,
3253 it has probably been given type `int'.
3254 Compute the type that would go with
3255 the actual amount of storage the member occupies. */
3257 if (TREE_CODE (lhs) == COMPONENT_REF
3258 && (TREE_CODE (lhstype) == INTEGER_TYPE
3259 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3260 || TREE_CODE (lhstype) == REAL_TYPE
3261 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3262 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3264 /* If storing in a field that is in actuality a short or narrower than one,
3265 we must store in the field in its actual type. */
3267 if (lhstype != TREE_TYPE (lhs))
3269 lhs = copy_node (lhs);
3270 TREE_TYPE (lhs) = lhstype;
3273 /* Convert new value to destination type. */
3275 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3276 NULL_TREE, NULL_TREE, 0);
3277 if (TREE_CODE (newrhs) == ERROR_MARK)
3278 return error_mark_node;
3280 /* Scan operands */
3282 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3283 TREE_SIDE_EFFECTS (result) = 1;
3285 /* If we got the LHS in a different type for storing in,
3286 convert the result back to the nominal type of LHS
3287 so that the value we return always has the same type
3288 as the LHS argument. */
3290 if (olhstype == TREE_TYPE (result))
3291 return result;
3292 return convert_for_assignment (olhstype, result, _("assignment"),
3293 NULL_TREE, NULL_TREE, 0);
3296 /* Convert value RHS to type TYPE as preparation for an assignment
3297 to an lvalue of type TYPE.
3298 The real work of conversion is done by `convert'.
3299 The purpose of this function is to generate error messages
3300 for assignments that are not allowed in C.
3301 ERRTYPE is a string to use in error messages:
3302 "assignment", "return", etc. If it is null, this is parameter passing
3303 for a function call (and different error messages are output).
3305 FUNNAME is the name of the function being called,
3306 as an IDENTIFIER_NODE, or null.
3307 PARMNUM is the number of the argument, for printing in error messages. */
3309 static tree
3310 convert_for_assignment (tree type, tree rhs, const char *errtype,
3311 tree fundecl, tree funname, int parmnum)
3313 enum tree_code codel = TREE_CODE (type);
3314 tree rhstype;
3315 enum tree_code coder;
3317 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3318 /* Do not use STRIP_NOPS here. We do not want an enumerator
3319 whose value is 0 to count as a null pointer constant. */
3320 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3321 rhs = TREE_OPERAND (rhs, 0);
3323 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3324 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3325 rhs = default_conversion (rhs);
3326 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3327 rhs = decl_constant_value_for_broken_optimization (rhs);
3329 rhstype = TREE_TYPE (rhs);
3330 coder = TREE_CODE (rhstype);
3332 if (coder == ERROR_MARK)
3333 return error_mark_node;
3335 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3337 overflow_warning (rhs);
3338 /* Check for Objective-C protocols. This will automatically
3339 issue a warning if there are protocol violations. No need to
3340 use the return value. */
3341 if (c_dialect_objc ())
3342 objc_comptypes (type, rhstype, 0);
3343 return rhs;
3346 if (coder == VOID_TYPE)
3348 error ("void value not ignored as it ought to be");
3349 return error_mark_node;
3351 /* A type converts to a reference to it.
3352 This code doesn't fully support references, it's just for the
3353 special case of va_start and va_copy. */
3354 if (codel == REFERENCE_TYPE
3355 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3357 if (!lvalue_p (rhs))
3359 error ("cannot pass rvalue to reference parameter");
3360 return error_mark_node;
3362 if (!c_mark_addressable (rhs))
3363 return error_mark_node;
3364 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3366 /* We already know that these two types are compatible, but they
3367 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3368 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3369 likely to be va_list, a typedef to __builtin_va_list, which
3370 is different enough that it will cause problems later. */
3371 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3372 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3374 rhs = build1 (NOP_EXPR, type, rhs);
3375 return rhs;
3377 /* Some types can interconvert without explicit casts. */
3378 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3379 && ((*targetm.vector_opaque_p) (type)
3380 || (*targetm.vector_opaque_p) (rhstype)))
3381 return convert (type, rhs);
3382 /* Arithmetic types all interconvert, and enum is treated like int. */
3383 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3384 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3385 || codel == BOOLEAN_TYPE)
3386 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3387 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3388 || coder == BOOLEAN_TYPE))
3389 return convert_and_check (type, rhs);
3391 /* Conversion to a transparent union from its member types.
3392 This applies only to function arguments. */
3393 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3395 tree memb_types;
3396 tree marginal_memb_type = 0;
3398 for (memb_types = TYPE_FIELDS (type); memb_types;
3399 memb_types = TREE_CHAIN (memb_types))
3401 tree memb_type = TREE_TYPE (memb_types);
3403 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3404 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3405 break;
3407 if (TREE_CODE (memb_type) != POINTER_TYPE)
3408 continue;
3410 if (coder == POINTER_TYPE)
3412 tree ttl = TREE_TYPE (memb_type);
3413 tree ttr = TREE_TYPE (rhstype);
3415 /* Any non-function converts to a [const][volatile] void *
3416 and vice versa; otherwise, targets must be the same.
3417 Meanwhile, the lhs target must have all the qualifiers of
3418 the rhs. */
3419 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3420 || comp_target_types (memb_type, rhstype, 0))
3422 /* If this type won't generate any warnings, use it. */
3423 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3424 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3425 && TREE_CODE (ttl) == FUNCTION_TYPE)
3426 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3427 == TYPE_QUALS (ttr))
3428 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3429 == TYPE_QUALS (ttl))))
3430 break;
3432 /* Keep looking for a better type, but remember this one. */
3433 if (! marginal_memb_type)
3434 marginal_memb_type = memb_type;
3438 /* Can convert integer zero to any pointer type. */
3439 if (integer_zerop (rhs)
3440 || (TREE_CODE (rhs) == NOP_EXPR
3441 && integer_zerop (TREE_OPERAND (rhs, 0))))
3443 rhs = null_pointer_node;
3444 break;
3448 if (memb_types || marginal_memb_type)
3450 if (! memb_types)
3452 /* We have only a marginally acceptable member type;
3453 it needs a warning. */
3454 tree ttl = TREE_TYPE (marginal_memb_type);
3455 tree ttr = TREE_TYPE (rhstype);
3457 /* Const and volatile mean something different for function
3458 types, so the usual warnings are not appropriate. */
3459 if (TREE_CODE (ttr) == FUNCTION_TYPE
3460 && TREE_CODE (ttl) == FUNCTION_TYPE)
3462 /* Because const and volatile on functions are
3463 restrictions that say the function will not do
3464 certain things, it is okay to use a const or volatile
3465 function where an ordinary one is wanted, but not
3466 vice-versa. */
3467 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3468 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3469 errtype, funname, parmnum);
3471 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3472 warn_for_assignment ("%s discards qualifiers from pointer target type",
3473 errtype, funname,
3474 parmnum);
3477 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3478 pedwarn ("ISO C prohibits argument conversion to union type");
3480 return build1 (NOP_EXPR, type, rhs);
3484 /* Conversions among pointers */
3485 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3486 && (coder == codel))
3488 tree ttl = TREE_TYPE (type);
3489 tree ttr = TREE_TYPE (rhstype);
3490 bool is_opaque_pointer;
3492 /* Opaque pointers are treated like void pointers. */
3493 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3494 || (*targetm.vector_opaque_p) (rhstype))
3495 && TREE_CODE (ttl) == VECTOR_TYPE
3496 && TREE_CODE (ttr) == VECTOR_TYPE;
3498 /* Any non-function converts to a [const][volatile] void *
3499 and vice versa; otherwise, targets must be the same.
3500 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3501 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3502 || comp_target_types (type, rhstype, 0)
3503 || is_opaque_pointer
3504 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3505 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3507 if (pedantic
3508 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3510 (VOID_TYPE_P (ttr)
3511 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3512 which are not ANSI null ptr constants. */
3513 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3514 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3515 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3516 errtype, funname, parmnum);
3517 /* Const and volatile mean something different for function types,
3518 so the usual warnings are not appropriate. */
3519 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3520 && TREE_CODE (ttl) != FUNCTION_TYPE)
3522 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3523 warn_for_assignment ("%s discards qualifiers from pointer target type",
3524 errtype, funname, parmnum);
3525 /* If this is not a case of ignoring a mismatch in signedness,
3526 no warning. */
3527 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3528 || comp_target_types (type, rhstype, 0))
3530 /* If there is a mismatch, do warn. */
3531 else if (pedantic)
3532 warn_for_assignment ("pointer targets in %s differ in signedness",
3533 errtype, funname, parmnum);
3535 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3536 && TREE_CODE (ttr) == FUNCTION_TYPE)
3538 /* Because const and volatile on functions are restrictions
3539 that say the function will not do certain things,
3540 it is okay to use a const or volatile function
3541 where an ordinary one is wanted, but not vice-versa. */
3542 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3543 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3544 errtype, funname, parmnum);
3547 else
3548 warn_for_assignment ("%s from incompatible pointer type",
3549 errtype, funname, parmnum);
3550 return convert (type, rhs);
3552 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3554 /* An explicit constant 0 can convert to a pointer,
3555 or one that results from arithmetic, even including
3556 a cast to integer type. */
3557 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3559 ! (TREE_CODE (rhs) == NOP_EXPR
3560 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3561 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3562 && integer_zerop (TREE_OPERAND (rhs, 0))))
3564 warn_for_assignment ("%s makes pointer from integer without a cast",
3565 errtype, funname, parmnum);
3566 return convert (type, rhs);
3568 return null_pointer_node;
3570 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3572 warn_for_assignment ("%s makes integer from pointer without a cast",
3573 errtype, funname, parmnum);
3574 return convert (type, rhs);
3576 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3577 return convert (type, rhs);
3579 if (!errtype)
3581 if (funname)
3583 tree selector = objc_message_selector ();
3585 if (selector && parmnum > 2)
3586 error ("incompatible type for argument %d of `%s'",
3587 parmnum - 2, IDENTIFIER_POINTER (selector));
3588 else
3589 error ("incompatible type for argument %d of `%s'",
3590 parmnum, IDENTIFIER_POINTER (funname));
3592 else
3593 error ("incompatible type for argument %d of indirect function call",
3594 parmnum);
3596 else
3597 error ("incompatible types in %s", errtype);
3599 return error_mark_node;
3602 /* Convert VALUE for assignment into inlined parameter PARM. */
3604 tree
3605 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
3607 tree ret, type;
3609 /* If FN was prototyped, the value has been converted already
3610 in convert_arguments. */
3611 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3612 return value;
3614 type = TREE_TYPE (parm);
3615 ret = convert_for_assignment (type, value,
3616 (char *) 0 /* arg passing */, fn,
3617 DECL_NAME (fn), 0);
3618 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3619 && INTEGRAL_TYPE_P (type)
3620 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3621 ret = default_conversion (ret);
3622 return ret;
3625 /* Print a warning using MSGID.
3626 It gets OPNAME as its one parameter.
3627 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3628 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3629 FUNCTION and ARGNUM are handled specially if we are building an
3630 Objective-C selector. */
3632 static void
3633 warn_for_assignment (const char *msgid, const char *opname, tree function,
3634 int argnum)
3636 if (opname == 0)
3638 tree selector = objc_message_selector ();
3639 char * new_opname;
3641 if (selector && argnum > 2)
3643 function = selector;
3644 argnum -= 2;
3646 if (argnum == 0)
3648 if (function)
3650 /* Function name is known; supply it. */
3651 const char *const argstring = _("passing arg of `%s'");
3652 new_opname = alloca (IDENTIFIER_LENGTH (function)
3653 + strlen (argstring) + 1 + 1);
3654 sprintf (new_opname, argstring,
3655 IDENTIFIER_POINTER (function));
3657 else
3659 /* Function name unknown (call through ptr). */
3660 const char *const argnofun = _("passing arg of pointer to function");
3661 new_opname = alloca (strlen (argnofun) + 1 + 1);
3662 sprintf (new_opname, argnofun);
3665 else if (function)
3667 /* Function name is known; supply it. */
3668 const char *const argstring = _("passing arg %d of `%s'");
3669 new_opname = alloca (IDENTIFIER_LENGTH (function)
3670 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3671 sprintf (new_opname, argstring, argnum,
3672 IDENTIFIER_POINTER (function));
3674 else
3676 /* Function name unknown (call through ptr); just give arg number. */
3677 const char *const argnofun = _("passing arg %d of pointer to function");
3678 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3679 sprintf (new_opname, argnofun, argnum);
3681 opname = new_opname;
3683 pedwarn (msgid, opname);
3686 /* If VALUE is a compound expr all of whose expressions are constant, then
3687 return its value. Otherwise, return error_mark_node.
3689 This is for handling COMPOUND_EXPRs as initializer elements
3690 which is allowed with a warning when -pedantic is specified. */
3692 static tree
3693 valid_compound_expr_initializer (tree value, tree endtype)
3695 if (TREE_CODE (value) == COMPOUND_EXPR)
3697 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3698 == error_mark_node)
3699 return error_mark_node;
3700 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3701 endtype);
3703 else if (! TREE_CONSTANT (value)
3704 && ! initializer_constant_valid_p (value, endtype))
3705 return error_mark_node;
3706 else
3707 return value;
3710 /* Perform appropriate conversions on the initial value of a variable,
3711 store it in the declaration DECL,
3712 and print any error messages that are appropriate.
3713 If the init is invalid, store an ERROR_MARK. */
3715 void
3716 store_init_value (tree decl, tree init)
3718 tree value, type;
3720 /* If variable's type was invalidly declared, just ignore it. */
3722 type = TREE_TYPE (decl);
3723 if (TREE_CODE (type) == ERROR_MARK)
3724 return;
3726 /* Digest the specified initializer into an expression. */
3728 value = digest_init (type, init, TREE_STATIC (decl));
3730 /* Store the expression if valid; else report error. */
3732 if (warn_traditional && !in_system_header
3733 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3734 warning ("traditional C rejects automatic aggregate initialization");
3736 DECL_INITIAL (decl) = value;
3738 /* ANSI wants warnings about out-of-range constant initializers. */
3739 STRIP_TYPE_NOPS (value);
3740 constant_expression_warning (value);
3742 /* Check if we need to set array size from compound literal size. */
3743 if (TREE_CODE (type) == ARRAY_TYPE
3744 && TYPE_DOMAIN (type) == 0
3745 && value != error_mark_node)
3747 tree inside_init = init;
3749 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3750 inside_init = TREE_OPERAND (init, 0);
3751 inside_init = fold (inside_init);
3753 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3755 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3757 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3759 /* For int foo[] = (int [3]){1}; we need to set array size
3760 now since later on array initializer will be just the
3761 brace enclosed list of the compound literal. */
3762 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3763 layout_type (type);
3764 layout_decl (decl, 0);
3770 /* Methods for storing and printing names for error messages. */
3772 /* Implement a spelling stack that allows components of a name to be pushed
3773 and popped. Each element on the stack is this structure. */
3775 struct spelling
3777 int kind;
3778 union
3780 int i;
3781 const char *s;
3782 } u;
3785 #define SPELLING_STRING 1
3786 #define SPELLING_MEMBER 2
3787 #define SPELLING_BOUNDS 3
3789 static struct spelling *spelling; /* Next stack element (unused). */
3790 static struct spelling *spelling_base; /* Spelling stack base. */
3791 static int spelling_size; /* Size of the spelling stack. */
3793 /* Macros to save and restore the spelling stack around push_... functions.
3794 Alternative to SAVE_SPELLING_STACK. */
3796 #define SPELLING_DEPTH() (spelling - spelling_base)
3797 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3799 /* Push an element on the spelling stack with type KIND and assign VALUE
3800 to MEMBER. */
3802 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3804 int depth = SPELLING_DEPTH (); \
3806 if (depth >= spelling_size) \
3808 spelling_size += 10; \
3809 if (spelling_base == 0) \
3810 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3811 else \
3812 spelling_base = xrealloc (spelling_base, \
3813 spelling_size * sizeof (struct spelling)); \
3814 RESTORE_SPELLING_DEPTH (depth); \
3817 spelling->kind = (KIND); \
3818 spelling->MEMBER = (VALUE); \
3819 spelling++; \
3822 /* Push STRING on the stack. Printed literally. */
3824 static void
3825 push_string (const char *string)
3827 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3830 /* Push a member name on the stack. Printed as '.' STRING. */
3832 static void
3833 push_member_name (tree decl)
3835 const char *const string
3836 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3837 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3840 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3842 static void
3843 push_array_bounds (int bounds)
3845 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3848 /* Compute the maximum size in bytes of the printed spelling. */
3850 static int
3851 spelling_length (void)
3853 int size = 0;
3854 struct spelling *p;
3856 for (p = spelling_base; p < spelling; p++)
3858 if (p->kind == SPELLING_BOUNDS)
3859 size += 25;
3860 else
3861 size += strlen (p->u.s) + 1;
3864 return size;
3867 /* Print the spelling to BUFFER and return it. */
3869 static char *
3870 print_spelling (char *buffer)
3872 char *d = buffer;
3873 struct spelling *p;
3875 for (p = spelling_base; p < spelling; p++)
3876 if (p->kind == SPELLING_BOUNDS)
3878 sprintf (d, "[%d]", p->u.i);
3879 d += strlen (d);
3881 else
3883 const char *s;
3884 if (p->kind == SPELLING_MEMBER)
3885 *d++ = '.';
3886 for (s = p->u.s; (*d = *s++); d++)
3889 *d++ = '\0';
3890 return buffer;
3893 /* Issue an error message for a bad initializer component.
3894 MSGID identifies the message.
3895 The component name is taken from the spelling stack. */
3897 void
3898 error_init (const char *msgid)
3900 char *ofwhat;
3902 error ("%s", _(msgid));
3903 ofwhat = print_spelling (alloca (spelling_length () + 1));
3904 if (*ofwhat)
3905 error ("(near initialization for `%s')", ofwhat);
3908 /* Issue a pedantic warning for a bad initializer component.
3909 MSGID identifies the message.
3910 The component name is taken from the spelling stack. */
3912 void
3913 pedwarn_init (const char *msgid)
3915 char *ofwhat;
3917 pedwarn ("%s", _(msgid));
3918 ofwhat = print_spelling (alloca (spelling_length () + 1));
3919 if (*ofwhat)
3920 pedwarn ("(near initialization for `%s')", ofwhat);
3923 /* Issue a warning for a bad initializer component.
3924 MSGID identifies the message.
3925 The component name is taken from the spelling stack. */
3927 static void
3928 warning_init (const char *msgid)
3930 char *ofwhat;
3932 warning ("%s", _(msgid));
3933 ofwhat = print_spelling (alloca (spelling_length () + 1));
3934 if (*ofwhat)
3935 warning ("(near initialization for `%s')", ofwhat);
3938 /* Digest the parser output INIT as an initializer for type TYPE.
3939 Return a C expression of type TYPE to represent the initial value.
3941 REQUIRE_CONSTANT requests an error if non-constant initializers or
3942 elements are seen. */
3944 static tree
3945 digest_init (tree type, tree init, int require_constant)
3947 enum tree_code code = TREE_CODE (type);
3948 tree inside_init = init;
3950 if (type == error_mark_node
3951 || init == error_mark_node
3952 || TREE_TYPE (init) == error_mark_node)
3953 return error_mark_node;
3955 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3956 /* Do not use STRIP_NOPS here. We do not want an enumerator
3957 whose value is 0 to count as a null pointer constant. */
3958 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3959 inside_init = TREE_OPERAND (init, 0);
3961 inside_init = fold (inside_init);
3963 /* Initialization of an array of chars from a string constant
3964 optionally enclosed in braces. */
3966 if (code == ARRAY_TYPE)
3968 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3969 if ((typ1 == char_type_node
3970 || typ1 == signed_char_type_node
3971 || typ1 == unsigned_char_type_node
3972 || typ1 == unsigned_wchar_type_node
3973 || typ1 == signed_wchar_type_node)
3974 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3976 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3977 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3978 return inside_init;
3980 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3981 != char_type_node)
3982 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3984 error_init ("char-array initialized from wide string");
3985 return error_mark_node;
3987 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3988 == char_type_node)
3989 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3991 error_init ("int-array initialized from non-wide string");
3992 return error_mark_node;
3995 TREE_TYPE (inside_init) = type;
3996 if (TYPE_DOMAIN (type) != 0
3997 && TYPE_SIZE (type) != 0
3998 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3999 /* Subtract 1 (or sizeof (wchar_t))
4000 because it's ok to ignore the terminating null char
4001 that is counted in the length of the constant. */
4002 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4003 TREE_STRING_LENGTH (inside_init)
4004 - ((TYPE_PRECISION (typ1)
4005 != TYPE_PRECISION (char_type_node))
4006 ? (TYPE_PRECISION (wchar_type_node)
4007 / BITS_PER_UNIT)
4008 : 1)))
4009 pedwarn_init ("initializer-string for array of chars is too long");
4011 return inside_init;
4015 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4016 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4017 below and handle as a constructor. */
4018 if (code == VECTOR_TYPE
4019 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
4020 && TREE_CONSTANT (inside_init))
4022 if (TREE_CODE (inside_init) == VECTOR_CST
4023 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4024 TYPE_MAIN_VARIANT (type),
4025 COMPARE_STRICT))
4026 return inside_init;
4027 else
4028 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4031 /* Any type can be initialized
4032 from an expression of the same type, optionally with braces. */
4034 if (inside_init && TREE_TYPE (inside_init) != 0
4035 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4036 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4037 || (code == ARRAY_TYPE
4038 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4039 || (code == VECTOR_TYPE
4040 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4041 || (code == POINTER_TYPE
4042 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4043 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4044 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4045 TREE_TYPE (type), COMPARE_STRICT))))
4047 if (code == POINTER_TYPE)
4048 inside_init = default_function_array_conversion (inside_init);
4050 if (code == VECTOR_TYPE)
4051 /* Although the types are compatible, we may require a
4052 conversion. */
4053 inside_init = convert (type, inside_init);
4055 if (require_constant && !flag_isoc99
4056 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4058 /* As an extension, allow initializing objects with static storage
4059 duration with compound literals (which are then treated just as
4060 the brace enclosed list they contain). */
4061 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4062 inside_init = DECL_INITIAL (decl);
4065 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4066 && TREE_CODE (inside_init) != CONSTRUCTOR)
4068 error_init ("array initialized from non-constant array expression");
4069 return error_mark_node;
4072 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4073 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4075 /* Compound expressions can only occur here if -pedantic or
4076 -pedantic-errors is specified. In the later case, we always want
4077 an error. In the former case, we simply want a warning. */
4078 if (require_constant && pedantic
4079 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4081 inside_init
4082 = valid_compound_expr_initializer (inside_init,
4083 TREE_TYPE (inside_init));
4084 if (inside_init == error_mark_node)
4085 error_init ("initializer element is not constant");
4086 else
4087 pedwarn_init ("initializer element is not constant");
4088 if (flag_pedantic_errors)
4089 inside_init = error_mark_node;
4091 else if (require_constant
4092 && (!TREE_CONSTANT (inside_init)
4093 /* This test catches things like `7 / 0' which
4094 result in an expression for which TREE_CONSTANT
4095 is true, but which is not actually something
4096 that is a legal constant. We really should not
4097 be using this function, because it is a part of
4098 the back-end. Instead, the expression should
4099 already have been turned into ERROR_MARK_NODE. */
4100 || !initializer_constant_valid_p (inside_init,
4101 TREE_TYPE (inside_init))))
4103 error_init ("initializer element is not constant");
4104 inside_init = error_mark_node;
4107 return inside_init;
4110 /* Handle scalar types, including conversions. */
4112 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4113 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4115 /* Note that convert_for_assignment calls default_conversion
4116 for arrays and functions. We must not call it in the
4117 case where inside_init is a null pointer constant. */
4118 inside_init
4119 = convert_for_assignment (type, init, _("initialization"),
4120 NULL_TREE, NULL_TREE, 0);
4122 if (require_constant && ! TREE_CONSTANT (inside_init))
4124 error_init ("initializer element is not constant");
4125 inside_init = error_mark_node;
4127 else if (require_constant
4128 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4130 error_init ("initializer element is not computable at load time");
4131 inside_init = error_mark_node;
4134 return inside_init;
4137 /* Come here only for records and arrays. */
4139 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4141 error_init ("variable-sized object may not be initialized");
4142 return error_mark_node;
4145 error_init ("invalid initializer");
4146 return error_mark_node;
4149 /* Handle initializers that use braces. */
4151 /* Type of object we are accumulating a constructor for.
4152 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4153 static tree constructor_type;
4155 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4156 left to fill. */
4157 static tree constructor_fields;
4159 /* For an ARRAY_TYPE, this is the specified index
4160 at which to store the next element we get. */
4161 static tree constructor_index;
4163 /* For an ARRAY_TYPE, this is the maximum index. */
4164 static tree constructor_max_index;
4166 /* For a RECORD_TYPE, this is the first field not yet written out. */
4167 static tree constructor_unfilled_fields;
4169 /* For an ARRAY_TYPE, this is the index of the first element
4170 not yet written out. */
4171 static tree constructor_unfilled_index;
4173 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4174 This is so we can generate gaps between fields, when appropriate. */
4175 static tree constructor_bit_index;
4177 /* If we are saving up the elements rather than allocating them,
4178 this is the list of elements so far (in reverse order,
4179 most recent first). */
4180 static tree constructor_elements;
4182 /* 1 if constructor should be incrementally stored into a constructor chain,
4183 0 if all the elements should be kept in AVL tree. */
4184 static int constructor_incremental;
4186 /* 1 if so far this constructor's elements are all compile-time constants. */
4187 static int constructor_constant;
4189 /* 1 if so far this constructor's elements are all valid address constants. */
4190 static int constructor_simple;
4192 /* 1 if this constructor is erroneous so far. */
4193 static int constructor_erroneous;
4195 /* Structure for managing pending initializer elements, organized as an
4196 AVL tree. */
4198 struct init_node
4200 struct init_node *left, *right;
4201 struct init_node *parent;
4202 int balance;
4203 tree purpose;
4204 tree value;
4207 /* Tree of pending elements at this constructor level.
4208 These are elements encountered out of order
4209 which belong at places we haven't reached yet in actually
4210 writing the output.
4211 Will never hold tree nodes across GC runs. */
4212 static struct init_node *constructor_pending_elts;
4214 /* The SPELLING_DEPTH of this constructor. */
4215 static int constructor_depth;
4217 /* 0 if implicitly pushing constructor levels is allowed. */
4218 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4220 static int require_constant_value;
4221 static int require_constant_elements;
4223 /* DECL node for which an initializer is being read.
4224 0 means we are reading a constructor expression
4225 such as (struct foo) {...}. */
4226 static tree constructor_decl;
4228 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4229 static const char *constructor_asmspec;
4231 /* Nonzero if this is an initializer for a top-level decl. */
4232 static int constructor_top_level;
4234 /* Nonzero if there were any member designators in this initializer. */
4235 static int constructor_designated;
4237 /* Nesting depth of designator list. */
4238 static int designator_depth;
4240 /* Nonzero if there were diagnosed errors in this designator list. */
4241 static int designator_errorneous;
4244 /* This stack has a level for each implicit or explicit level of
4245 structuring in the initializer, including the outermost one. It
4246 saves the values of most of the variables above. */
4248 struct constructor_range_stack;
4250 struct constructor_stack
4252 struct constructor_stack *next;
4253 tree type;
4254 tree fields;
4255 tree index;
4256 tree max_index;
4257 tree unfilled_index;
4258 tree unfilled_fields;
4259 tree bit_index;
4260 tree elements;
4261 struct init_node *pending_elts;
4262 int offset;
4263 int depth;
4264 /* If nonzero, this value should replace the entire
4265 constructor at this level. */
4266 tree replacement_value;
4267 struct constructor_range_stack *range_stack;
4268 char constant;
4269 char simple;
4270 char implicit;
4271 char erroneous;
4272 char outer;
4273 char incremental;
4274 char designated;
4277 struct constructor_stack *constructor_stack;
4279 /* This stack represents designators from some range designator up to
4280 the last designator in the list. */
4282 struct constructor_range_stack
4284 struct constructor_range_stack *next, *prev;
4285 struct constructor_stack *stack;
4286 tree range_start;
4287 tree index;
4288 tree range_end;
4289 tree fields;
4292 struct constructor_range_stack *constructor_range_stack;
4294 /* This stack records separate initializers that are nested.
4295 Nested initializers can't happen in ANSI C, but GNU C allows them
4296 in cases like { ... (struct foo) { ... } ... }. */
4298 struct initializer_stack
4300 struct initializer_stack *next;
4301 tree decl;
4302 const char *asmspec;
4303 struct constructor_stack *constructor_stack;
4304 struct constructor_range_stack *constructor_range_stack;
4305 tree elements;
4306 struct spelling *spelling;
4307 struct spelling *spelling_base;
4308 int spelling_size;
4309 char top_level;
4310 char require_constant_value;
4311 char require_constant_elements;
4314 struct initializer_stack *initializer_stack;
4316 /* Prepare to parse and output the initializer for variable DECL. */
4318 void
4319 start_init (tree decl, tree asmspec_tree, int top_level)
4321 const char *locus;
4322 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4323 const char *asmspec = 0;
4325 if (asmspec_tree)
4326 asmspec = TREE_STRING_POINTER (asmspec_tree);
4328 p->decl = constructor_decl;
4329 p->asmspec = constructor_asmspec;
4330 p->require_constant_value = require_constant_value;
4331 p->require_constant_elements = require_constant_elements;
4332 p->constructor_stack = constructor_stack;
4333 p->constructor_range_stack = constructor_range_stack;
4334 p->elements = constructor_elements;
4335 p->spelling = spelling;
4336 p->spelling_base = spelling_base;
4337 p->spelling_size = spelling_size;
4338 p->top_level = constructor_top_level;
4339 p->next = initializer_stack;
4340 initializer_stack = p;
4342 constructor_decl = decl;
4343 constructor_asmspec = asmspec;
4344 constructor_designated = 0;
4345 constructor_top_level = top_level;
4347 if (decl != 0)
4349 require_constant_value = TREE_STATIC (decl);
4350 require_constant_elements
4351 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4352 /* For a scalar, you can always use any value to initialize,
4353 even within braces. */
4354 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4355 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4356 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4357 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4358 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4360 else
4362 require_constant_value = 0;
4363 require_constant_elements = 0;
4364 locus = "(anonymous)";
4367 constructor_stack = 0;
4368 constructor_range_stack = 0;
4370 missing_braces_mentioned = 0;
4372 spelling_base = 0;
4373 spelling_size = 0;
4374 RESTORE_SPELLING_DEPTH (0);
4376 if (locus)
4377 push_string (locus);
4380 void
4381 finish_init (void)
4383 struct initializer_stack *p = initializer_stack;
4385 /* Free the whole constructor stack of this initializer. */
4386 while (constructor_stack)
4388 struct constructor_stack *q = constructor_stack;
4389 constructor_stack = q->next;
4390 free (q);
4393 if (constructor_range_stack)
4394 abort ();
4396 /* Pop back to the data of the outer initializer (if any). */
4397 constructor_decl = p->decl;
4398 constructor_asmspec = p->asmspec;
4399 require_constant_value = p->require_constant_value;
4400 require_constant_elements = p->require_constant_elements;
4401 constructor_stack = p->constructor_stack;
4402 constructor_range_stack = p->constructor_range_stack;
4403 constructor_elements = p->elements;
4404 spelling = p->spelling;
4405 spelling_base = p->spelling_base;
4406 spelling_size = p->spelling_size;
4407 constructor_top_level = p->top_level;
4408 initializer_stack = p->next;
4409 free (p);
4412 /* Call here when we see the initializer is surrounded by braces.
4413 This is instead of a call to push_init_level;
4414 it is matched by a call to pop_init_level.
4416 TYPE is the type to initialize, for a constructor expression.
4417 For an initializer for a decl, TYPE is zero. */
4419 void
4420 really_start_incremental_init (tree type)
4422 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4424 if (type == 0)
4425 type = TREE_TYPE (constructor_decl);
4427 if ((*targetm.vector_opaque_p) (type))
4428 error ("opaque vector types cannot be initialized");
4430 p->type = constructor_type;
4431 p->fields = constructor_fields;
4432 p->index = constructor_index;
4433 p->max_index = constructor_max_index;
4434 p->unfilled_index = constructor_unfilled_index;
4435 p->unfilled_fields = constructor_unfilled_fields;
4436 p->bit_index = constructor_bit_index;
4437 p->elements = constructor_elements;
4438 p->constant = constructor_constant;
4439 p->simple = constructor_simple;
4440 p->erroneous = constructor_erroneous;
4441 p->pending_elts = constructor_pending_elts;
4442 p->depth = constructor_depth;
4443 p->replacement_value = 0;
4444 p->implicit = 0;
4445 p->range_stack = 0;
4446 p->outer = 0;
4447 p->incremental = constructor_incremental;
4448 p->designated = constructor_designated;
4449 p->next = 0;
4450 constructor_stack = p;
4452 constructor_constant = 1;
4453 constructor_simple = 1;
4454 constructor_depth = SPELLING_DEPTH ();
4455 constructor_elements = 0;
4456 constructor_pending_elts = 0;
4457 constructor_type = type;
4458 constructor_incremental = 1;
4459 constructor_designated = 0;
4460 designator_depth = 0;
4461 designator_errorneous = 0;
4463 if (TREE_CODE (constructor_type) == RECORD_TYPE
4464 || TREE_CODE (constructor_type) == UNION_TYPE)
4466 constructor_fields = TYPE_FIELDS (constructor_type);
4467 /* Skip any nameless bit fields at the beginning. */
4468 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4469 && DECL_NAME (constructor_fields) == 0)
4470 constructor_fields = TREE_CHAIN (constructor_fields);
4472 constructor_unfilled_fields = constructor_fields;
4473 constructor_bit_index = bitsize_zero_node;
4475 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4477 if (TYPE_DOMAIN (constructor_type))
4479 constructor_max_index
4480 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4482 /* Detect non-empty initializations of zero-length arrays. */
4483 if (constructor_max_index == NULL_TREE
4484 && TYPE_SIZE (constructor_type))
4485 constructor_max_index = build_int_2 (-1, -1);
4487 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4488 to initialize VLAs will cause a proper error; avoid tree
4489 checking errors as well by setting a safe value. */
4490 if (constructor_max_index
4491 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4492 constructor_max_index = build_int_2 (-1, -1);
4494 constructor_index
4495 = convert (bitsizetype,
4496 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4498 else
4499 constructor_index = bitsize_zero_node;
4501 constructor_unfilled_index = constructor_index;
4503 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4505 /* Vectors are like simple fixed-size arrays. */
4506 constructor_max_index =
4507 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4508 constructor_index = convert (bitsizetype, bitsize_zero_node);
4509 constructor_unfilled_index = constructor_index;
4511 else
4513 /* Handle the case of int x = {5}; */
4514 constructor_fields = constructor_type;
4515 constructor_unfilled_fields = constructor_type;
4519 /* Push down into a subobject, for initialization.
4520 If this is for an explicit set of braces, IMPLICIT is 0.
4521 If it is because the next element belongs at a lower level,
4522 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4524 void
4525 push_init_level (int implicit)
4527 struct constructor_stack *p;
4528 tree value = NULL_TREE;
4530 /* If we've exhausted any levels that didn't have braces,
4531 pop them now. */
4532 while (constructor_stack->implicit)
4534 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4535 || TREE_CODE (constructor_type) == UNION_TYPE)
4536 && constructor_fields == 0)
4537 process_init_element (pop_init_level (1));
4538 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4539 && constructor_max_index
4540 && tree_int_cst_lt (constructor_max_index, constructor_index))
4541 process_init_element (pop_init_level (1));
4542 else
4543 break;
4546 /* Unless this is an explicit brace, we need to preserve previous
4547 content if any. */
4548 if (implicit)
4550 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4551 || TREE_CODE (constructor_type) == UNION_TYPE)
4552 && constructor_fields)
4553 value = find_init_member (constructor_fields);
4554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4555 value = find_init_member (constructor_index);
4558 p = xmalloc (sizeof (struct constructor_stack));
4559 p->type = constructor_type;
4560 p->fields = constructor_fields;
4561 p->index = constructor_index;
4562 p->max_index = constructor_max_index;
4563 p->unfilled_index = constructor_unfilled_index;
4564 p->unfilled_fields = constructor_unfilled_fields;
4565 p->bit_index = constructor_bit_index;
4566 p->elements = constructor_elements;
4567 p->constant = constructor_constant;
4568 p->simple = constructor_simple;
4569 p->erroneous = constructor_erroneous;
4570 p->pending_elts = constructor_pending_elts;
4571 p->depth = constructor_depth;
4572 p->replacement_value = 0;
4573 p->implicit = implicit;
4574 p->outer = 0;
4575 p->incremental = constructor_incremental;
4576 p->designated = constructor_designated;
4577 p->next = constructor_stack;
4578 p->range_stack = 0;
4579 constructor_stack = p;
4581 constructor_constant = 1;
4582 constructor_simple = 1;
4583 constructor_depth = SPELLING_DEPTH ();
4584 constructor_elements = 0;
4585 constructor_incremental = 1;
4586 constructor_designated = 0;
4587 constructor_pending_elts = 0;
4588 if (!implicit)
4590 p->range_stack = constructor_range_stack;
4591 constructor_range_stack = 0;
4592 designator_depth = 0;
4593 designator_errorneous = 0;
4596 /* Don't die if an entire brace-pair level is superfluous
4597 in the containing level. */
4598 if (constructor_type == 0)
4600 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4601 || TREE_CODE (constructor_type) == UNION_TYPE)
4603 /* Don't die if there are extra init elts at the end. */
4604 if (constructor_fields == 0)
4605 constructor_type = 0;
4606 else
4608 constructor_type = TREE_TYPE (constructor_fields);
4609 push_member_name (constructor_fields);
4610 constructor_depth++;
4613 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4615 constructor_type = TREE_TYPE (constructor_type);
4616 push_array_bounds (tree_low_cst (constructor_index, 0));
4617 constructor_depth++;
4620 if (constructor_type == 0)
4622 error_init ("extra brace group at end of initializer");
4623 constructor_fields = 0;
4624 constructor_unfilled_fields = 0;
4625 return;
4628 if (value && TREE_CODE (value) == CONSTRUCTOR)
4630 constructor_constant = TREE_CONSTANT (value);
4631 constructor_simple = TREE_STATIC (value);
4632 constructor_elements = CONSTRUCTOR_ELTS (value);
4633 if (constructor_elements
4634 && (TREE_CODE (constructor_type) == RECORD_TYPE
4635 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4636 set_nonincremental_init ();
4639 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4641 missing_braces_mentioned = 1;
4642 warning_init ("missing braces around initializer");
4645 if (TREE_CODE (constructor_type) == RECORD_TYPE
4646 || TREE_CODE (constructor_type) == UNION_TYPE)
4648 constructor_fields = TYPE_FIELDS (constructor_type);
4649 /* Skip any nameless bit fields at the beginning. */
4650 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4651 && DECL_NAME (constructor_fields) == 0)
4652 constructor_fields = TREE_CHAIN (constructor_fields);
4654 constructor_unfilled_fields = constructor_fields;
4655 constructor_bit_index = bitsize_zero_node;
4657 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4659 /* Vectors are like simple fixed-size arrays. */
4660 constructor_max_index =
4661 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4662 constructor_index = convert (bitsizetype, integer_zero_node);
4663 constructor_unfilled_index = constructor_index;
4665 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4667 if (TYPE_DOMAIN (constructor_type))
4669 constructor_max_index
4670 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4672 /* Detect non-empty initializations of zero-length arrays. */
4673 if (constructor_max_index == NULL_TREE
4674 && TYPE_SIZE (constructor_type))
4675 constructor_max_index = build_int_2 (-1, -1);
4677 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4678 to initialize VLAs will cause a proper error; avoid tree
4679 checking errors as well by setting a safe value. */
4680 if (constructor_max_index
4681 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4682 constructor_max_index = build_int_2 (-1, -1);
4684 constructor_index
4685 = convert (bitsizetype,
4686 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4688 else
4689 constructor_index = bitsize_zero_node;
4691 constructor_unfilled_index = constructor_index;
4692 if (value && TREE_CODE (value) == STRING_CST)
4694 /* We need to split the char/wchar array into individual
4695 characters, so that we don't have to special case it
4696 everywhere. */
4697 set_nonincremental_init_from_string (value);
4700 else
4702 warning_init ("braces around scalar initializer");
4703 constructor_fields = constructor_type;
4704 constructor_unfilled_fields = constructor_type;
4708 /* At the end of an implicit or explicit brace level,
4709 finish up that level of constructor.
4710 If we were outputting the elements as they are read, return 0
4711 from inner levels (process_init_element ignores that),
4712 but return error_mark_node from the outermost level
4713 (that's what we want to put in DECL_INITIAL).
4714 Otherwise, return a CONSTRUCTOR expression. */
4716 tree
4717 pop_init_level (int implicit)
4719 struct constructor_stack *p;
4720 tree constructor = 0;
4722 if (implicit == 0)
4724 /* When we come to an explicit close brace,
4725 pop any inner levels that didn't have explicit braces. */
4726 while (constructor_stack->implicit)
4727 process_init_element (pop_init_level (1));
4729 if (constructor_range_stack)
4730 abort ();
4733 p = constructor_stack;
4735 /* Error for initializing a flexible array member, or a zero-length
4736 array member in an inappropriate context. */
4737 if (constructor_type && constructor_fields
4738 && TREE_CODE (constructor_type) == ARRAY_TYPE
4739 && TYPE_DOMAIN (constructor_type)
4740 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4742 /* Silently discard empty initializations. The parser will
4743 already have pedwarned for empty brackets. */
4744 if (integer_zerop (constructor_unfilled_index))
4745 constructor_type = NULL_TREE;
4746 else if (! TYPE_SIZE (constructor_type))
4748 if (constructor_depth > 2)
4749 error_init ("initialization of flexible array member in a nested context");
4750 else if (pedantic)
4751 pedwarn_init ("initialization of a flexible array member");
4753 /* We have already issued an error message for the existence
4754 of a flexible array member not at the end of the structure.
4755 Discard the initializer so that we do not abort later. */
4756 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4757 constructor_type = NULL_TREE;
4759 else
4760 /* Zero-length arrays are no longer special, so we should no longer
4761 get here. */
4762 abort ();
4765 /* Warn when some struct elements are implicitly initialized to zero. */
4766 if (extra_warnings
4767 && constructor_type
4768 && TREE_CODE (constructor_type) == RECORD_TYPE
4769 && constructor_unfilled_fields)
4771 /* Do not warn for flexible array members or zero-length arrays. */
4772 while (constructor_unfilled_fields
4773 && (! DECL_SIZE (constructor_unfilled_fields)
4774 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4775 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4777 /* Do not warn if this level of the initializer uses member
4778 designators; it is likely to be deliberate. */
4779 if (constructor_unfilled_fields && !constructor_designated)
4781 push_member_name (constructor_unfilled_fields);
4782 warning_init ("missing initializer");
4783 RESTORE_SPELLING_DEPTH (constructor_depth);
4787 /* Now output all pending elements. */
4788 constructor_incremental = 1;
4789 output_pending_init_elements (1);
4791 /* Pad out the end of the structure. */
4792 if (p->replacement_value)
4793 /* If this closes a superfluous brace pair,
4794 just pass out the element between them. */
4795 constructor = p->replacement_value;
4796 else if (constructor_type == 0)
4798 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4799 && TREE_CODE (constructor_type) != UNION_TYPE
4800 && TREE_CODE (constructor_type) != ARRAY_TYPE
4801 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4803 /* A nonincremental scalar initializer--just return
4804 the element, after verifying there is just one. */
4805 if (constructor_elements == 0)
4807 if (!constructor_erroneous)
4808 error_init ("empty scalar initializer");
4809 constructor = error_mark_node;
4811 else if (TREE_CHAIN (constructor_elements) != 0)
4813 error_init ("extra elements in scalar initializer");
4814 constructor = TREE_VALUE (constructor_elements);
4816 else
4817 constructor = TREE_VALUE (constructor_elements);
4819 else
4821 if (constructor_erroneous)
4822 constructor = error_mark_node;
4823 else
4825 constructor = build_constructor (constructor_type,
4826 nreverse (constructor_elements));
4827 if (constructor_constant)
4828 TREE_CONSTANT (constructor) = 1;
4829 if (constructor_constant && constructor_simple)
4830 TREE_STATIC (constructor) = 1;
4834 constructor_type = p->type;
4835 constructor_fields = p->fields;
4836 constructor_index = p->index;
4837 constructor_max_index = p->max_index;
4838 constructor_unfilled_index = p->unfilled_index;
4839 constructor_unfilled_fields = p->unfilled_fields;
4840 constructor_bit_index = p->bit_index;
4841 constructor_elements = p->elements;
4842 constructor_constant = p->constant;
4843 constructor_simple = p->simple;
4844 constructor_erroneous = p->erroneous;
4845 constructor_incremental = p->incremental;
4846 constructor_designated = p->designated;
4847 constructor_pending_elts = p->pending_elts;
4848 constructor_depth = p->depth;
4849 if (!p->implicit)
4850 constructor_range_stack = p->range_stack;
4851 RESTORE_SPELLING_DEPTH (constructor_depth);
4853 constructor_stack = p->next;
4854 free (p);
4856 if (constructor == 0)
4858 if (constructor_stack == 0)
4859 return error_mark_node;
4860 return NULL_TREE;
4862 return constructor;
4865 /* Common handling for both array range and field name designators.
4866 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4868 static int
4869 set_designator (int array)
4871 tree subtype;
4872 enum tree_code subcode;
4874 /* Don't die if an entire brace-pair level is superfluous
4875 in the containing level. */
4876 if (constructor_type == 0)
4877 return 1;
4879 /* If there were errors in this designator list already, bail out silently. */
4880 if (designator_errorneous)
4881 return 1;
4883 if (!designator_depth)
4885 if (constructor_range_stack)
4886 abort ();
4888 /* Designator list starts at the level of closest explicit
4889 braces. */
4890 while (constructor_stack->implicit)
4891 process_init_element (pop_init_level (1));
4892 constructor_designated = 1;
4893 return 0;
4896 if (constructor_no_implicit)
4898 error_init ("initialization designators may not nest");
4899 return 1;
4902 if (TREE_CODE (constructor_type) == RECORD_TYPE
4903 || TREE_CODE (constructor_type) == UNION_TYPE)
4905 subtype = TREE_TYPE (constructor_fields);
4906 if (subtype != error_mark_node)
4907 subtype = TYPE_MAIN_VARIANT (subtype);
4909 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4911 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4913 else
4914 abort ();
4916 subcode = TREE_CODE (subtype);
4917 if (array && subcode != ARRAY_TYPE)
4919 error_init ("array index in non-array initializer");
4920 return 1;
4922 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4924 error_init ("field name not in record or union initializer");
4925 return 1;
4928 constructor_designated = 1;
4929 push_init_level (2);
4930 return 0;
4933 /* If there are range designators in designator list, push a new designator
4934 to constructor_range_stack. RANGE_END is end of such stack range or
4935 NULL_TREE if there is no range designator at this level. */
4937 static void
4938 push_range_stack (tree range_end)
4940 struct constructor_range_stack *p;
4942 p = ggc_alloc (sizeof (struct constructor_range_stack));
4943 p->prev = constructor_range_stack;
4944 p->next = 0;
4945 p->fields = constructor_fields;
4946 p->range_start = constructor_index;
4947 p->index = constructor_index;
4948 p->stack = constructor_stack;
4949 p->range_end = range_end;
4950 if (constructor_range_stack)
4951 constructor_range_stack->next = p;
4952 constructor_range_stack = p;
4955 /* Within an array initializer, specify the next index to be initialized.
4956 FIRST is that index. If LAST is nonzero, then initialize a range
4957 of indices, running from FIRST through LAST. */
4959 void
4960 set_init_index (tree first, tree last)
4962 if (set_designator (1))
4963 return;
4965 designator_errorneous = 1;
4967 while ((TREE_CODE (first) == NOP_EXPR
4968 || TREE_CODE (first) == CONVERT_EXPR
4969 || TREE_CODE (first) == NON_LVALUE_EXPR)
4970 && (TYPE_MODE (TREE_TYPE (first))
4971 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4972 first = TREE_OPERAND (first, 0);
4974 if (last)
4975 while ((TREE_CODE (last) == NOP_EXPR
4976 || TREE_CODE (last) == CONVERT_EXPR
4977 || TREE_CODE (last) == NON_LVALUE_EXPR)
4978 && (TYPE_MODE (TREE_TYPE (last))
4979 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4980 last = TREE_OPERAND (last, 0);
4982 if (TREE_CODE (first) != INTEGER_CST)
4983 error_init ("nonconstant array index in initializer");
4984 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4985 error_init ("nonconstant array index in initializer");
4986 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4987 error_init ("array index in non-array initializer");
4988 else if (tree_int_cst_sgn (first) == -1)
4989 error_init ("array index in initializer exceeds array bounds");
4990 else if (constructor_max_index
4991 && tree_int_cst_lt (constructor_max_index, first))
4992 error_init ("array index in initializer exceeds array bounds");
4993 else
4995 constructor_index = convert (bitsizetype, first);
4997 if (last)
4999 if (tree_int_cst_equal (first, last))
5000 last = 0;
5001 else if (tree_int_cst_lt (last, first))
5003 error_init ("empty index range in initializer");
5004 last = 0;
5006 else
5008 last = convert (bitsizetype, last);
5009 if (constructor_max_index != 0
5010 && tree_int_cst_lt (constructor_max_index, last))
5012 error_init ("array index range in initializer exceeds array bounds");
5013 last = 0;
5018 designator_depth++;
5019 designator_errorneous = 0;
5020 if (constructor_range_stack || last)
5021 push_range_stack (last);
5025 /* Within a struct initializer, specify the next field to be initialized. */
5027 void
5028 set_init_label (tree fieldname)
5030 tree tail;
5032 if (set_designator (0))
5033 return;
5035 designator_errorneous = 1;
5037 if (TREE_CODE (constructor_type) != RECORD_TYPE
5038 && TREE_CODE (constructor_type) != UNION_TYPE)
5040 error_init ("field name not in record or union initializer");
5041 return;
5044 for (tail = TYPE_FIELDS (constructor_type); tail;
5045 tail = TREE_CHAIN (tail))
5047 if (DECL_NAME (tail) == fieldname)
5048 break;
5051 if (tail == 0)
5052 error ("unknown field `%s' specified in initializer",
5053 IDENTIFIER_POINTER (fieldname));
5054 else
5056 constructor_fields = tail;
5057 designator_depth++;
5058 designator_errorneous = 0;
5059 if (constructor_range_stack)
5060 push_range_stack (NULL_TREE);
5064 /* Add a new initializer to the tree of pending initializers. PURPOSE
5065 identifies the initializer, either array index or field in a structure.
5066 VALUE is the value of that index or field. */
5068 static void
5069 add_pending_init (tree purpose, tree value)
5071 struct init_node *p, **q, *r;
5073 q = &constructor_pending_elts;
5074 p = 0;
5076 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5078 while (*q != 0)
5080 p = *q;
5081 if (tree_int_cst_lt (purpose, p->purpose))
5082 q = &p->left;
5083 else if (tree_int_cst_lt (p->purpose, purpose))
5084 q = &p->right;
5085 else
5087 if (TREE_SIDE_EFFECTS (p->value))
5088 warning_init ("initialized field with side-effects overwritten");
5089 p->value = value;
5090 return;
5094 else
5096 tree bitpos;
5098 bitpos = bit_position (purpose);
5099 while (*q != NULL)
5101 p = *q;
5102 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5103 q = &p->left;
5104 else if (p->purpose != purpose)
5105 q = &p->right;
5106 else
5108 if (TREE_SIDE_EFFECTS (p->value))
5109 warning_init ("initialized field with side-effects overwritten");
5110 p->value = value;
5111 return;
5116 r = ggc_alloc (sizeof (struct init_node));
5117 r->purpose = purpose;
5118 r->value = value;
5120 *q = r;
5121 r->parent = p;
5122 r->left = 0;
5123 r->right = 0;
5124 r->balance = 0;
5126 while (p)
5128 struct init_node *s;
5130 if (r == p->left)
5132 if (p->balance == 0)
5133 p->balance = -1;
5134 else if (p->balance < 0)
5136 if (r->balance < 0)
5138 /* L rotation. */
5139 p->left = r->right;
5140 if (p->left)
5141 p->left->parent = p;
5142 r->right = p;
5144 p->balance = 0;
5145 r->balance = 0;
5147 s = p->parent;
5148 p->parent = r;
5149 r->parent = s;
5150 if (s)
5152 if (s->left == p)
5153 s->left = r;
5154 else
5155 s->right = r;
5157 else
5158 constructor_pending_elts = r;
5160 else
5162 /* LR rotation. */
5163 struct init_node *t = r->right;
5165 r->right = t->left;
5166 if (r->right)
5167 r->right->parent = r;
5168 t->left = r;
5170 p->left = t->right;
5171 if (p->left)
5172 p->left->parent = p;
5173 t->right = p;
5175 p->balance = t->balance < 0;
5176 r->balance = -(t->balance > 0);
5177 t->balance = 0;
5179 s = p->parent;
5180 p->parent = t;
5181 r->parent = t;
5182 t->parent = s;
5183 if (s)
5185 if (s->left == p)
5186 s->left = t;
5187 else
5188 s->right = t;
5190 else
5191 constructor_pending_elts = t;
5193 break;
5195 else
5197 /* p->balance == +1; growth of left side balances the node. */
5198 p->balance = 0;
5199 break;
5202 else /* r == p->right */
5204 if (p->balance == 0)
5205 /* Growth propagation from right side. */
5206 p->balance++;
5207 else if (p->balance > 0)
5209 if (r->balance > 0)
5211 /* R rotation. */
5212 p->right = r->left;
5213 if (p->right)
5214 p->right->parent = p;
5215 r->left = p;
5217 p->balance = 0;
5218 r->balance = 0;
5220 s = p->parent;
5221 p->parent = r;
5222 r->parent = s;
5223 if (s)
5225 if (s->left == p)
5226 s->left = r;
5227 else
5228 s->right = r;
5230 else
5231 constructor_pending_elts = r;
5233 else /* r->balance == -1 */
5235 /* RL rotation */
5236 struct init_node *t = r->left;
5238 r->left = t->right;
5239 if (r->left)
5240 r->left->parent = r;
5241 t->right = r;
5243 p->right = t->left;
5244 if (p->right)
5245 p->right->parent = p;
5246 t->left = p;
5248 r->balance = (t->balance < 0);
5249 p->balance = -(t->balance > 0);
5250 t->balance = 0;
5252 s = p->parent;
5253 p->parent = t;
5254 r->parent = t;
5255 t->parent = s;
5256 if (s)
5258 if (s->left == p)
5259 s->left = t;
5260 else
5261 s->right = t;
5263 else
5264 constructor_pending_elts = t;
5266 break;
5268 else
5270 /* p->balance == -1; growth of right side balances the node. */
5271 p->balance = 0;
5272 break;
5276 r = p;
5277 p = p->parent;
5281 /* Build AVL tree from a sorted chain. */
5283 static void
5284 set_nonincremental_init (void)
5286 tree chain;
5288 if (TREE_CODE (constructor_type) != RECORD_TYPE
5289 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5290 return;
5292 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5293 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5294 constructor_elements = 0;
5295 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5297 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5298 /* Skip any nameless bit fields at the beginning. */
5299 while (constructor_unfilled_fields != 0
5300 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5301 && DECL_NAME (constructor_unfilled_fields) == 0)
5302 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5305 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5307 if (TYPE_DOMAIN (constructor_type))
5308 constructor_unfilled_index
5309 = convert (bitsizetype,
5310 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5311 else
5312 constructor_unfilled_index = bitsize_zero_node;
5314 constructor_incremental = 0;
5317 /* Build AVL tree from a string constant. */
5319 static void
5320 set_nonincremental_init_from_string (tree str)
5322 tree value, purpose, type;
5323 HOST_WIDE_INT val[2];
5324 const char *p, *end;
5325 int byte, wchar_bytes, charwidth, bitpos;
5327 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5328 abort ();
5330 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5331 == TYPE_PRECISION (char_type_node))
5332 wchar_bytes = 1;
5333 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5334 == TYPE_PRECISION (wchar_type_node))
5335 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5336 else
5337 abort ();
5339 charwidth = TYPE_PRECISION (char_type_node);
5340 type = TREE_TYPE (constructor_type);
5341 p = TREE_STRING_POINTER (str);
5342 end = p + TREE_STRING_LENGTH (str);
5344 for (purpose = bitsize_zero_node;
5345 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5346 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5348 if (wchar_bytes == 1)
5350 val[1] = (unsigned char) *p++;
5351 val[0] = 0;
5353 else
5355 val[0] = 0;
5356 val[1] = 0;
5357 for (byte = 0; byte < wchar_bytes; byte++)
5359 if (BYTES_BIG_ENDIAN)
5360 bitpos = (wchar_bytes - byte - 1) * charwidth;
5361 else
5362 bitpos = byte * charwidth;
5363 val[bitpos < HOST_BITS_PER_WIDE_INT]
5364 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5365 << (bitpos % HOST_BITS_PER_WIDE_INT);
5369 if (!TREE_UNSIGNED (type))
5371 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5372 if (bitpos < HOST_BITS_PER_WIDE_INT)
5374 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5376 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5377 val[0] = -1;
5380 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5382 if (val[1] < 0)
5383 val[0] = -1;
5385 else if (val[0] & (((HOST_WIDE_INT) 1)
5386 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5387 val[0] |= ((HOST_WIDE_INT) -1)
5388 << (bitpos - HOST_BITS_PER_WIDE_INT);
5391 value = build_int_2 (val[1], val[0]);
5392 TREE_TYPE (value) = type;
5393 add_pending_init (purpose, value);
5396 constructor_incremental = 0;
5399 /* Return value of FIELD in pending initializer or zero if the field was
5400 not initialized yet. */
5402 static tree
5403 find_init_member (tree field)
5405 struct init_node *p;
5407 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5409 if (constructor_incremental
5410 && tree_int_cst_lt (field, constructor_unfilled_index))
5411 set_nonincremental_init ();
5413 p = constructor_pending_elts;
5414 while (p)
5416 if (tree_int_cst_lt (field, p->purpose))
5417 p = p->left;
5418 else if (tree_int_cst_lt (p->purpose, field))
5419 p = p->right;
5420 else
5421 return p->value;
5424 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5426 tree bitpos = bit_position (field);
5428 if (constructor_incremental
5429 && (!constructor_unfilled_fields
5430 || tree_int_cst_lt (bitpos,
5431 bit_position (constructor_unfilled_fields))))
5432 set_nonincremental_init ();
5434 p = constructor_pending_elts;
5435 while (p)
5437 if (field == p->purpose)
5438 return p->value;
5439 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5440 p = p->left;
5441 else
5442 p = p->right;
5445 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5447 if (constructor_elements
5448 && TREE_PURPOSE (constructor_elements) == field)
5449 return TREE_VALUE (constructor_elements);
5451 return 0;
5454 /* "Output" the next constructor element.
5455 At top level, really output it to assembler code now.
5456 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5457 TYPE is the data type that the containing data type wants here.
5458 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5460 PENDING if non-nil means output pending elements that belong
5461 right after this element. (PENDING is normally 1;
5462 it is 0 while outputting pending elements, to avoid recursion.) */
5464 static void
5465 output_init_element (tree value, tree type, tree field, int pending)
5467 if (type == error_mark_node)
5469 constructor_erroneous = 1;
5470 return;
5472 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5473 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5474 && !(TREE_CODE (value) == STRING_CST
5475 && TREE_CODE (type) == ARRAY_TYPE
5476 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5477 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5478 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5479 value = default_conversion (value);
5481 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5482 && require_constant_value && !flag_isoc99 && pending)
5484 /* As an extension, allow initializing objects with static storage
5485 duration with compound literals (which are then treated just as
5486 the brace enclosed list they contain). */
5487 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5488 value = DECL_INITIAL (decl);
5491 if (value == error_mark_node)
5492 constructor_erroneous = 1;
5493 else if (!TREE_CONSTANT (value))
5494 constructor_constant = 0;
5495 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5496 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5497 || TREE_CODE (constructor_type) == UNION_TYPE)
5498 && DECL_C_BIT_FIELD (field)
5499 && TREE_CODE (value) != INTEGER_CST))
5500 constructor_simple = 0;
5502 if (require_constant_value && ! TREE_CONSTANT (value))
5504 error_init ("initializer element is not constant");
5505 value = error_mark_node;
5507 else if (require_constant_elements
5508 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5509 pedwarn ("initializer element is not computable at load time");
5511 /* If this field is empty (and not at the end of structure),
5512 don't do anything other than checking the initializer. */
5513 if (field
5514 && (TREE_TYPE (field) == error_mark_node
5515 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5516 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5517 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5518 || TREE_CHAIN (field)))))
5519 return;
5521 value = digest_init (type, value, require_constant_value);
5522 if (value == error_mark_node)
5524 constructor_erroneous = 1;
5525 return;
5528 /* If this element doesn't come next in sequence,
5529 put it on constructor_pending_elts. */
5530 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5531 && (!constructor_incremental
5532 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5534 if (constructor_incremental
5535 && tree_int_cst_lt (field, constructor_unfilled_index))
5536 set_nonincremental_init ();
5538 add_pending_init (field, value);
5539 return;
5541 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5542 && (!constructor_incremental
5543 || field != constructor_unfilled_fields))
5545 /* We do this for records but not for unions. In a union,
5546 no matter which field is specified, it can be initialized
5547 right away since it starts at the beginning of the union. */
5548 if (constructor_incremental)
5550 if (!constructor_unfilled_fields)
5551 set_nonincremental_init ();
5552 else
5554 tree bitpos, unfillpos;
5556 bitpos = bit_position (field);
5557 unfillpos = bit_position (constructor_unfilled_fields);
5559 if (tree_int_cst_lt (bitpos, unfillpos))
5560 set_nonincremental_init ();
5564 add_pending_init (field, value);
5565 return;
5567 else if (TREE_CODE (constructor_type) == UNION_TYPE
5568 && constructor_elements)
5570 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5571 warning_init ("initialized field with side-effects overwritten");
5573 /* We can have just one union field set. */
5574 constructor_elements = 0;
5577 /* Otherwise, output this element either to
5578 constructor_elements or to the assembler file. */
5580 if (field && TREE_CODE (field) == INTEGER_CST)
5581 field = copy_node (field);
5582 constructor_elements
5583 = tree_cons (field, value, constructor_elements);
5585 /* Advance the variable that indicates sequential elements output. */
5586 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5587 constructor_unfilled_index
5588 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5589 bitsize_one_node);
5590 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5592 constructor_unfilled_fields
5593 = TREE_CHAIN (constructor_unfilled_fields);
5595 /* Skip any nameless bit fields. */
5596 while (constructor_unfilled_fields != 0
5597 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5598 && DECL_NAME (constructor_unfilled_fields) == 0)
5599 constructor_unfilled_fields =
5600 TREE_CHAIN (constructor_unfilled_fields);
5602 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5603 constructor_unfilled_fields = 0;
5605 /* Now output any pending elements which have become next. */
5606 if (pending)
5607 output_pending_init_elements (0);
5610 /* Output any pending elements which have become next.
5611 As we output elements, constructor_unfilled_{fields,index}
5612 advances, which may cause other elements to become next;
5613 if so, they too are output.
5615 If ALL is 0, we return when there are
5616 no more pending elements to output now.
5618 If ALL is 1, we output space as necessary so that
5619 we can output all the pending elements. */
5621 static void
5622 output_pending_init_elements (int all)
5624 struct init_node *elt = constructor_pending_elts;
5625 tree next;
5627 retry:
5629 /* Look thru the whole pending tree.
5630 If we find an element that should be output now,
5631 output it. Otherwise, set NEXT to the element
5632 that comes first among those still pending. */
5634 next = 0;
5635 while (elt)
5637 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5639 if (tree_int_cst_equal (elt->purpose,
5640 constructor_unfilled_index))
5641 output_init_element (elt->value,
5642 TREE_TYPE (constructor_type),
5643 constructor_unfilled_index, 0);
5644 else if (tree_int_cst_lt (constructor_unfilled_index,
5645 elt->purpose))
5647 /* Advance to the next smaller node. */
5648 if (elt->left)
5649 elt = elt->left;
5650 else
5652 /* We have reached the smallest node bigger than the
5653 current unfilled index. Fill the space first. */
5654 next = elt->purpose;
5655 break;
5658 else
5660 /* Advance to the next bigger node. */
5661 if (elt->right)
5662 elt = elt->right;
5663 else
5665 /* We have reached the biggest node in a subtree. Find
5666 the parent of it, which is the next bigger node. */
5667 while (elt->parent && elt->parent->right == elt)
5668 elt = elt->parent;
5669 elt = elt->parent;
5670 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5671 elt->purpose))
5673 next = elt->purpose;
5674 break;
5679 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5680 || TREE_CODE (constructor_type) == UNION_TYPE)
5682 tree ctor_unfilled_bitpos, elt_bitpos;
5684 /* If the current record is complete we are done. */
5685 if (constructor_unfilled_fields == 0)
5686 break;
5688 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5689 elt_bitpos = bit_position (elt->purpose);
5690 /* We can't compare fields here because there might be empty
5691 fields in between. */
5692 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5694 constructor_unfilled_fields = elt->purpose;
5695 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5696 elt->purpose, 0);
5698 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5700 /* Advance to the next smaller node. */
5701 if (elt->left)
5702 elt = elt->left;
5703 else
5705 /* We have reached the smallest node bigger than the
5706 current unfilled field. Fill the space first. */
5707 next = elt->purpose;
5708 break;
5711 else
5713 /* Advance to the next bigger node. */
5714 if (elt->right)
5715 elt = elt->right;
5716 else
5718 /* We have reached the biggest node in a subtree. Find
5719 the parent of it, which is the next bigger node. */
5720 while (elt->parent && elt->parent->right == elt)
5721 elt = elt->parent;
5722 elt = elt->parent;
5723 if (elt
5724 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5725 bit_position (elt->purpose))))
5727 next = elt->purpose;
5728 break;
5735 /* Ordinarily return, but not if we want to output all
5736 and there are elements left. */
5737 if (! (all && next != 0))
5738 return;
5740 /* If it's not incremental, just skip over the gap, so that after
5741 jumping to retry we will output the next successive element. */
5742 if (TREE_CODE (constructor_type) == RECORD_TYPE
5743 || TREE_CODE (constructor_type) == UNION_TYPE)
5744 constructor_unfilled_fields = next;
5745 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5746 constructor_unfilled_index = next;
5748 /* ELT now points to the node in the pending tree with the next
5749 initializer to output. */
5750 goto retry;
5753 /* Add one non-braced element to the current constructor level.
5754 This adjusts the current position within the constructor's type.
5755 This may also start or terminate implicit levels
5756 to handle a partly-braced initializer.
5758 Once this has found the correct level for the new element,
5759 it calls output_init_element. */
5761 void
5762 process_init_element (tree value)
5764 tree orig_value = value;
5765 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5767 designator_depth = 0;
5768 designator_errorneous = 0;
5770 /* Handle superfluous braces around string cst as in
5771 char x[] = {"foo"}; */
5772 if (string_flag
5773 && constructor_type
5774 && TREE_CODE (constructor_type) == ARRAY_TYPE
5775 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5776 && integer_zerop (constructor_unfilled_index))
5778 if (constructor_stack->replacement_value)
5779 error_init ("excess elements in char array initializer");
5780 constructor_stack->replacement_value = value;
5781 return;
5784 if (constructor_stack->replacement_value != 0)
5786 error_init ("excess elements in struct initializer");
5787 return;
5790 /* Ignore elements of a brace group if it is entirely superfluous
5791 and has already been diagnosed. */
5792 if (constructor_type == 0)
5793 return;
5795 /* If we've exhausted any levels that didn't have braces,
5796 pop them now. */
5797 while (constructor_stack->implicit)
5799 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5800 || TREE_CODE (constructor_type) == UNION_TYPE)
5801 && constructor_fields == 0)
5802 process_init_element (pop_init_level (1));
5803 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5804 && (constructor_max_index == 0
5805 || tree_int_cst_lt (constructor_max_index,
5806 constructor_index)))
5807 process_init_element (pop_init_level (1));
5808 else
5809 break;
5812 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5813 if (constructor_range_stack)
5815 /* If value is a compound literal and we'll be just using its
5816 content, don't put it into a SAVE_EXPR. */
5817 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5818 || !require_constant_value
5819 || flag_isoc99)
5820 value = save_expr (value);
5823 while (1)
5825 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5827 tree fieldtype;
5828 enum tree_code fieldcode;
5830 if (constructor_fields == 0)
5832 pedwarn_init ("excess elements in struct initializer");
5833 break;
5836 fieldtype = TREE_TYPE (constructor_fields);
5837 if (fieldtype != error_mark_node)
5838 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5839 fieldcode = TREE_CODE (fieldtype);
5841 /* Error for non-static initialization of a flexible array member. */
5842 if (fieldcode == ARRAY_TYPE
5843 && !require_constant_value
5844 && TYPE_SIZE (fieldtype) == NULL_TREE
5845 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5847 error_init ("non-static initialization of a flexible array member");
5848 break;
5851 /* Accept a string constant to initialize a subarray. */
5852 if (value != 0
5853 && fieldcode == ARRAY_TYPE
5854 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5855 && string_flag)
5856 value = orig_value;
5857 /* Otherwise, if we have come to a subaggregate,
5858 and we don't have an element of its type, push into it. */
5859 else if (value != 0 && !constructor_no_implicit
5860 && value != error_mark_node
5861 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5862 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5863 || fieldcode == UNION_TYPE))
5865 push_init_level (1);
5866 continue;
5869 if (value)
5871 push_member_name (constructor_fields);
5872 output_init_element (value, fieldtype, constructor_fields, 1);
5873 RESTORE_SPELLING_DEPTH (constructor_depth);
5875 else
5876 /* Do the bookkeeping for an element that was
5877 directly output as a constructor. */
5879 /* For a record, keep track of end position of last field. */
5880 if (DECL_SIZE (constructor_fields))
5881 constructor_bit_index
5882 = size_binop (PLUS_EXPR,
5883 bit_position (constructor_fields),
5884 DECL_SIZE (constructor_fields));
5886 /* If the current field was the first one not yet written out,
5887 it isn't now, so update. */
5888 if (constructor_unfilled_fields == constructor_fields)
5890 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5891 /* Skip any nameless bit fields. */
5892 while (constructor_unfilled_fields != 0
5893 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5894 && DECL_NAME (constructor_unfilled_fields) == 0)
5895 constructor_unfilled_fields =
5896 TREE_CHAIN (constructor_unfilled_fields);
5900 constructor_fields = TREE_CHAIN (constructor_fields);
5901 /* Skip any nameless bit fields at the beginning. */
5902 while (constructor_fields != 0
5903 && DECL_C_BIT_FIELD (constructor_fields)
5904 && DECL_NAME (constructor_fields) == 0)
5905 constructor_fields = TREE_CHAIN (constructor_fields);
5907 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5909 tree fieldtype;
5910 enum tree_code fieldcode;
5912 if (constructor_fields == 0)
5914 pedwarn_init ("excess elements in union initializer");
5915 break;
5918 fieldtype = TREE_TYPE (constructor_fields);
5919 if (fieldtype != error_mark_node)
5920 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5921 fieldcode = TREE_CODE (fieldtype);
5923 /* Warn that traditional C rejects initialization of unions.
5924 We skip the warning if the value is zero. This is done
5925 under the assumption that the zero initializer in user
5926 code appears conditioned on e.g. __STDC__ to avoid
5927 "missing initializer" warnings and relies on default
5928 initialization to zero in the traditional C case.
5929 We also skip the warning if the initializer is designated,
5930 again on the assumption that this must be conditional on
5931 __STDC__ anyway (and we've already complained about the
5932 member-designator already). */
5933 if (warn_traditional && !in_system_header && !constructor_designated
5934 && !(value && (integer_zerop (value) || real_zerop (value))))
5935 warning ("traditional C rejects initialization of unions");
5937 /* Accept a string constant to initialize a subarray. */
5938 if (value != 0
5939 && fieldcode == ARRAY_TYPE
5940 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5941 && string_flag)
5942 value = orig_value;
5943 /* Otherwise, if we have come to a subaggregate,
5944 and we don't have an element of its type, push into it. */
5945 else if (value != 0 && !constructor_no_implicit
5946 && value != error_mark_node
5947 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5948 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5949 || fieldcode == UNION_TYPE))
5951 push_init_level (1);
5952 continue;
5955 if (value)
5957 push_member_name (constructor_fields);
5958 output_init_element (value, fieldtype, constructor_fields, 1);
5959 RESTORE_SPELLING_DEPTH (constructor_depth);
5961 else
5962 /* Do the bookkeeping for an element that was
5963 directly output as a constructor. */
5965 constructor_bit_index = DECL_SIZE (constructor_fields);
5966 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5969 constructor_fields = 0;
5971 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5973 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5974 enum tree_code eltcode = TREE_CODE (elttype);
5976 /* Accept a string constant to initialize a subarray. */
5977 if (value != 0
5978 && eltcode == ARRAY_TYPE
5979 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5980 && string_flag)
5981 value = orig_value;
5982 /* Otherwise, if we have come to a subaggregate,
5983 and we don't have an element of its type, push into it. */
5984 else if (value != 0 && !constructor_no_implicit
5985 && value != error_mark_node
5986 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5987 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5988 || eltcode == UNION_TYPE))
5990 push_init_level (1);
5991 continue;
5994 if (constructor_max_index != 0
5995 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5996 || integer_all_onesp (constructor_max_index)))
5998 pedwarn_init ("excess elements in array initializer");
5999 break;
6002 /* Now output the actual element. */
6003 if (value)
6005 push_array_bounds (tree_low_cst (constructor_index, 0));
6006 output_init_element (value, elttype, constructor_index, 1);
6007 RESTORE_SPELLING_DEPTH (constructor_depth);
6010 constructor_index
6011 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6013 if (! value)
6014 /* If we are doing the bookkeeping for an element that was
6015 directly output as a constructor, we must update
6016 constructor_unfilled_index. */
6017 constructor_unfilled_index = constructor_index;
6019 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6021 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6023 /* Do a basic check of initializer size. Note that vectors
6024 always have a fixed size derived from their type. */
6025 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6027 pedwarn_init ("excess elements in vector initializer");
6028 break;
6031 /* Now output the actual element. */
6032 if (value)
6033 output_init_element (value, elttype, constructor_index, 1);
6035 constructor_index
6036 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6038 if (! value)
6039 /* If we are doing the bookkeeping for an element that was
6040 directly output as a constructor, we must update
6041 constructor_unfilled_index. */
6042 constructor_unfilled_index = constructor_index;
6045 /* Handle the sole element allowed in a braced initializer
6046 for a scalar variable. */
6047 else if (constructor_fields == 0)
6049 pedwarn_init ("excess elements in scalar initializer");
6050 break;
6052 else
6054 if (value)
6055 output_init_element (value, constructor_type, NULL_TREE, 1);
6056 constructor_fields = 0;
6059 /* Handle range initializers either at this level or anywhere higher
6060 in the designator stack. */
6061 if (constructor_range_stack)
6063 struct constructor_range_stack *p, *range_stack;
6064 int finish = 0;
6066 range_stack = constructor_range_stack;
6067 constructor_range_stack = 0;
6068 while (constructor_stack != range_stack->stack)
6070 if (!constructor_stack->implicit)
6071 abort ();
6072 process_init_element (pop_init_level (1));
6074 for (p = range_stack;
6075 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6076 p = p->prev)
6078 if (!constructor_stack->implicit)
6079 abort ();
6080 process_init_element (pop_init_level (1));
6083 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6084 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6085 finish = 1;
6087 while (1)
6089 constructor_index = p->index;
6090 constructor_fields = p->fields;
6091 if (finish && p->range_end && p->index == p->range_start)
6093 finish = 0;
6094 p->prev = 0;
6096 p = p->next;
6097 if (!p)
6098 break;
6099 push_init_level (2);
6100 p->stack = constructor_stack;
6101 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6102 p->index = p->range_start;
6105 if (!finish)
6106 constructor_range_stack = range_stack;
6107 continue;
6110 break;
6113 constructor_range_stack = 0;
6116 /* Build a simple asm-statement, from one string literal. */
6117 tree
6118 simple_asm_stmt (tree expr)
6120 STRIP_NOPS (expr);
6122 if (TREE_CODE (expr) == ADDR_EXPR)
6123 expr = TREE_OPERAND (expr, 0);
6125 if (TREE_CODE (expr) == STRING_CST)
6127 tree stmt;
6129 /* Simple asm statements are treated as volatile. */
6130 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6131 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6132 ASM_INPUT_P (stmt) = 1;
6133 return stmt;
6136 error ("argument of `asm' is not a constant string");
6137 return NULL_TREE;
6140 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6141 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6143 tree
6144 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6145 tree clobbers)
6147 tree tail;
6149 if (TREE_CODE (string) != STRING_CST)
6151 error ("asm template is not a string constant");
6152 return NULL_TREE;
6155 if (cv_qualifier != NULL_TREE
6156 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6158 warning ("%s qualifier ignored on asm",
6159 IDENTIFIER_POINTER (cv_qualifier));
6160 cv_qualifier = NULL_TREE;
6163 /* We can remove output conversions that change the type,
6164 but not the mode. */
6165 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6167 tree output = TREE_VALUE (tail);
6169 STRIP_NOPS (output);
6170 TREE_VALUE (tail) = output;
6172 /* Allow conversions as LHS here. build_modify_expr as called below
6173 will do the right thing with them. */
6174 while (TREE_CODE (output) == NOP_EXPR
6175 || TREE_CODE (output) == CONVERT_EXPR
6176 || TREE_CODE (output) == FLOAT_EXPR
6177 || TREE_CODE (output) == FIX_TRUNC_EXPR
6178 || TREE_CODE (output) == FIX_FLOOR_EXPR
6179 || TREE_CODE (output) == FIX_ROUND_EXPR
6180 || TREE_CODE (output) == FIX_CEIL_EXPR)
6181 output = TREE_OPERAND (output, 0);
6183 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6186 /* Remove output conversions that change the type but not the mode. */
6187 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6189 tree output = TREE_VALUE (tail);
6190 STRIP_NOPS (output);
6191 TREE_VALUE (tail) = output;
6194 /* Perform default conversions on array and function inputs.
6195 Don't do this for other types as it would screw up operands
6196 expected to be in memory. */
6197 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6198 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6200 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6201 outputs, inputs, clobbers));
6204 /* Expand an ASM statement with operands, handling output operands
6205 that are not variables or INDIRECT_REFS by transforming such
6206 cases into cases that expand_asm_operands can handle.
6208 Arguments are same as for expand_asm_operands. */
6210 void
6211 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6212 tree clobbers, int vol, const char *filename,
6213 int line)
6215 int noutputs = list_length (outputs);
6216 int i;
6217 /* o[I] is the place that output number I should be written. */
6218 tree *o = alloca (noutputs * sizeof (tree));
6219 tree tail;
6221 /* Record the contents of OUTPUTS before it is modified. */
6222 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6224 o[i] = TREE_VALUE (tail);
6225 if (o[i] == error_mark_node)
6226 return;
6229 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6230 OUTPUTS some trees for where the values were actually stored. */
6231 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6233 /* Copy all the intermediate outputs into the specified outputs. */
6234 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6236 if (o[i] != TREE_VALUE (tail))
6238 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6239 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6240 free_temp_slots ();
6242 /* Restore the original value so that it's correct the next
6243 time we expand this function. */
6244 TREE_VALUE (tail) = o[i];
6246 /* Detect modification of read-only values.
6247 (Otherwise done by build_modify_expr.) */
6248 else
6250 tree type = TREE_TYPE (o[i]);
6251 if (TREE_READONLY (o[i])
6252 || TYPE_READONLY (type)
6253 || ((TREE_CODE (type) == RECORD_TYPE
6254 || TREE_CODE (type) == UNION_TYPE)
6255 && C_TYPE_FIELDS_READONLY (type)))
6256 readonly_warning (o[i], "modification by `asm'");
6260 /* Those MODIFY_EXPRs could do autoincrements. */
6261 emit_queue ();
6264 /* Expand a C `return' statement.
6265 RETVAL is the expression for what to return,
6266 or a null pointer for `return;' with no value. */
6268 tree
6269 c_expand_return (tree retval)
6271 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6273 if (TREE_THIS_VOLATILE (current_function_decl))
6274 warning ("function declared `noreturn' has a `return' statement");
6276 if (!retval)
6278 current_function_returns_null = 1;
6279 if ((warn_return_type || flag_isoc99)
6280 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6281 pedwarn_c99 ("`return' with no value, in function returning non-void");
6283 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6285 current_function_returns_null = 1;
6286 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6287 pedwarn ("`return' with a value, in function returning void");
6289 else
6291 tree t = convert_for_assignment (valtype, retval, _("return"),
6292 NULL_TREE, NULL_TREE, 0);
6293 tree res = DECL_RESULT (current_function_decl);
6294 tree inner;
6296 current_function_returns_value = 1;
6297 if (t == error_mark_node)
6298 return NULL_TREE;
6300 inner = t = convert (TREE_TYPE (res), t);
6302 /* Strip any conversions, additions, and subtractions, and see if
6303 we are returning the address of a local variable. Warn if so. */
6304 while (1)
6306 switch (TREE_CODE (inner))
6308 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6309 case PLUS_EXPR:
6310 inner = TREE_OPERAND (inner, 0);
6311 continue;
6313 case MINUS_EXPR:
6314 /* If the second operand of the MINUS_EXPR has a pointer
6315 type (or is converted from it), this may be valid, so
6316 don't give a warning. */
6318 tree op1 = TREE_OPERAND (inner, 1);
6320 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6321 && (TREE_CODE (op1) == NOP_EXPR
6322 || TREE_CODE (op1) == NON_LVALUE_EXPR
6323 || TREE_CODE (op1) == CONVERT_EXPR))
6324 op1 = TREE_OPERAND (op1, 0);
6326 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6327 break;
6329 inner = TREE_OPERAND (inner, 0);
6330 continue;
6333 case ADDR_EXPR:
6334 inner = TREE_OPERAND (inner, 0);
6336 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6337 inner = TREE_OPERAND (inner, 0);
6339 if (TREE_CODE (inner) == VAR_DECL
6340 && ! DECL_EXTERNAL (inner)
6341 && ! TREE_STATIC (inner)
6342 && DECL_CONTEXT (inner) == current_function_decl)
6343 warning ("function returns address of local variable");
6344 break;
6346 default:
6347 break;
6350 break;
6353 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6356 return add_stmt (build_return_stmt (retval));
6359 struct c_switch {
6360 /* The SWITCH_STMT being built. */
6361 tree switch_stmt;
6362 /* A splay-tree mapping the low element of a case range to the high
6363 element, or NULL_TREE if there is no high element. Used to
6364 determine whether or not a new case label duplicates an old case
6365 label. We need a tree, rather than simply a hash table, because
6366 of the GNU case range extension. */
6367 splay_tree cases;
6368 /* The next node on the stack. */
6369 struct c_switch *next;
6372 /* A stack of the currently active switch statements. The innermost
6373 switch statement is on the top of the stack. There is no need to
6374 mark the stack for garbage collection because it is only active
6375 during the processing of the body of a function, and we never
6376 collect at that point. */
6378 static struct c_switch *switch_stack;
6380 /* Start a C switch statement, testing expression EXP. Return the new
6381 SWITCH_STMT. */
6383 tree
6384 c_start_case (tree exp)
6386 enum tree_code code;
6387 tree type, orig_type = error_mark_node;
6388 struct c_switch *cs;
6390 if (exp != error_mark_node)
6392 code = TREE_CODE (TREE_TYPE (exp));
6393 orig_type = TREE_TYPE (exp);
6395 if (! INTEGRAL_TYPE_P (orig_type)
6396 && code != ERROR_MARK)
6398 error ("switch quantity not an integer");
6399 exp = integer_zero_node;
6401 else
6403 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6405 if (warn_traditional && !in_system_header
6406 && (type == long_integer_type_node
6407 || type == long_unsigned_type_node))
6408 warning ("`long' switch expression not converted to `int' in ISO C");
6410 exp = default_conversion (exp);
6411 type = TREE_TYPE (exp);
6415 /* Add this new SWITCH_STMT to the stack. */
6416 cs = xmalloc (sizeof (*cs));
6417 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6418 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6419 cs->next = switch_stack;
6420 switch_stack = cs;
6422 return add_stmt (switch_stack->switch_stmt);
6425 /* Process a case label. */
6427 tree
6428 do_case (tree low_value, tree high_value)
6430 tree label = NULL_TREE;
6432 if (switch_stack)
6434 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6436 label = c_add_case_label (switch_stack->cases,
6437 SWITCH_COND (switch_stack->switch_stmt),
6438 low_value, high_value);
6439 if (label == error_mark_node)
6440 label = NULL_TREE;
6441 else if (switch_was_empty_p)
6443 /* Attach the first case label to the SWITCH_BODY. */
6444 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6445 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6448 else if (low_value)
6449 error ("case label not within a switch statement");
6450 else
6451 error ("`default' label not within a switch statement");
6453 return label;
6456 /* Finish the switch statement. */
6458 void
6459 c_finish_case (void)
6461 struct c_switch *cs = switch_stack;
6463 /* Rechain the next statements to the SWITCH_STMT. */
6464 last_tree = cs->switch_stmt;
6466 /* Pop the stack. */
6467 switch_stack = switch_stack->next;
6468 splay_tree_delete (cs->cases);
6469 free (cs);
6472 /* Build a binary-operation expression without default conversions.
6473 CODE is the kind of expression to build.
6474 This function differs from `build' in several ways:
6475 the data type of the result is computed and recorded in it,
6476 warnings are generated if arg data types are invalid,
6477 special handling for addition and subtraction of pointers is known,
6478 and some optimization is done (operations on narrow ints
6479 are done in the narrower type when that gives the same result).
6480 Constant folding is also done before the result is returned.
6482 Note that the operands will never have enumeral types, or function
6483 or array types, because either they will have the default conversions
6484 performed or they have both just been converted to some other type in which
6485 the arithmetic is to be done. */
6487 tree
6488 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6489 int convert_p)
6491 tree type0, type1;
6492 enum tree_code code0, code1;
6493 tree op0, op1;
6495 /* Expression code to give to the expression when it is built.
6496 Normally this is CODE, which is what the caller asked for,
6497 but in some special cases we change it. */
6498 enum tree_code resultcode = code;
6500 /* Data type in which the computation is to be performed.
6501 In the simplest cases this is the common type of the arguments. */
6502 tree result_type = NULL;
6504 /* Nonzero means operands have already been type-converted
6505 in whatever way is necessary.
6506 Zero means they need to be converted to RESULT_TYPE. */
6507 int converted = 0;
6509 /* Nonzero means create the expression with this type, rather than
6510 RESULT_TYPE. */
6511 tree build_type = 0;
6513 /* Nonzero means after finally constructing the expression
6514 convert it to this type. */
6515 tree final_type = 0;
6517 /* Nonzero if this is an operation like MIN or MAX which can
6518 safely be computed in short if both args are promoted shorts.
6519 Also implies COMMON.
6520 -1 indicates a bitwise operation; this makes a difference
6521 in the exact conditions for when it is safe to do the operation
6522 in a narrower mode. */
6523 int shorten = 0;
6525 /* Nonzero if this is a comparison operation;
6526 if both args are promoted shorts, compare the original shorts.
6527 Also implies COMMON. */
6528 int short_compare = 0;
6530 /* Nonzero if this is a right-shift operation, which can be computed on the
6531 original short and then promoted if the operand is a promoted short. */
6532 int short_shift = 0;
6534 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6535 int common = 0;
6537 if (convert_p)
6539 op0 = default_conversion (orig_op0);
6540 op1 = default_conversion (orig_op1);
6542 else
6544 op0 = orig_op0;
6545 op1 = orig_op1;
6548 type0 = TREE_TYPE (op0);
6549 type1 = TREE_TYPE (op1);
6551 /* The expression codes of the data types of the arguments tell us
6552 whether the arguments are integers, floating, pointers, etc. */
6553 code0 = TREE_CODE (type0);
6554 code1 = TREE_CODE (type1);
6556 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6557 STRIP_TYPE_NOPS (op0);
6558 STRIP_TYPE_NOPS (op1);
6560 /* If an error was already reported for one of the arguments,
6561 avoid reporting another error. */
6563 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6564 return error_mark_node;
6566 switch (code)
6568 case PLUS_EXPR:
6569 /* Handle the pointer + int case. */
6570 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6571 return pointer_int_sum (PLUS_EXPR, op0, op1);
6572 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6573 return pointer_int_sum (PLUS_EXPR, op1, op0);
6574 else
6575 common = 1;
6576 break;
6578 case MINUS_EXPR:
6579 /* Subtraction of two similar pointers.
6580 We must subtract them as integers, then divide by object size. */
6581 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6582 && comp_target_types (type0, type1, 1))
6583 return pointer_diff (op0, op1);
6584 /* Handle pointer minus int. Just like pointer plus int. */
6585 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6586 return pointer_int_sum (MINUS_EXPR, op0, op1);
6587 else
6588 common = 1;
6589 break;
6591 case MULT_EXPR:
6592 common = 1;
6593 break;
6595 case TRUNC_DIV_EXPR:
6596 case CEIL_DIV_EXPR:
6597 case FLOOR_DIV_EXPR:
6598 case ROUND_DIV_EXPR:
6599 case EXACT_DIV_EXPR:
6600 /* Floating point division by zero is a legitimate way to obtain
6601 infinities and NaNs. */
6602 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6603 warning ("division by zero");
6605 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6606 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6607 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6608 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6610 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6611 resultcode = RDIV_EXPR;
6612 else
6613 /* Although it would be tempting to shorten always here, that
6614 loses on some targets, since the modulo instruction is
6615 undefined if the quotient can't be represented in the
6616 computation mode. We shorten only if unsigned or if
6617 dividing by something we know != -1. */
6618 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6619 || (TREE_CODE (op1) == INTEGER_CST
6620 && ! integer_all_onesp (op1)));
6621 common = 1;
6623 break;
6625 case BIT_AND_EXPR:
6626 case BIT_IOR_EXPR:
6627 case BIT_XOR_EXPR:
6628 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6629 shorten = -1;
6630 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6631 common = 1;
6632 break;
6634 case TRUNC_MOD_EXPR:
6635 case FLOOR_MOD_EXPR:
6636 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6637 warning ("division by zero");
6639 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6641 /* Although it would be tempting to shorten always here, that loses
6642 on some targets, since the modulo instruction is undefined if the
6643 quotient can't be represented in the computation mode. We shorten
6644 only if unsigned or if dividing by something we know != -1. */
6645 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6646 || (TREE_CODE (op1) == INTEGER_CST
6647 && ! integer_all_onesp (op1)));
6648 common = 1;
6650 break;
6652 case TRUTH_ANDIF_EXPR:
6653 case TRUTH_ORIF_EXPR:
6654 case TRUTH_AND_EXPR:
6655 case TRUTH_OR_EXPR:
6656 case TRUTH_XOR_EXPR:
6657 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6658 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6659 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6660 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6662 /* Result of these operations is always an int,
6663 but that does not mean the operands should be
6664 converted to ints! */
6665 result_type = integer_type_node;
6666 op0 = c_common_truthvalue_conversion (op0);
6667 op1 = c_common_truthvalue_conversion (op1);
6668 converted = 1;
6670 break;
6672 /* Shift operations: result has same type as first operand;
6673 always convert second operand to int.
6674 Also set SHORT_SHIFT if shifting rightward. */
6676 case RSHIFT_EXPR:
6677 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6679 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6681 if (tree_int_cst_sgn (op1) < 0)
6682 warning ("right shift count is negative");
6683 else
6685 if (! integer_zerop (op1))
6686 short_shift = 1;
6688 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6689 warning ("right shift count >= width of type");
6693 /* Use the type of the value to be shifted. */
6694 result_type = type0;
6695 /* Convert the shift-count to an integer, regardless of size
6696 of value being shifted. */
6697 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6698 op1 = convert (integer_type_node, op1);
6699 /* Avoid converting op1 to result_type later. */
6700 converted = 1;
6702 break;
6704 case LSHIFT_EXPR:
6705 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6707 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6709 if (tree_int_cst_sgn (op1) < 0)
6710 warning ("left shift count is negative");
6712 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6713 warning ("left shift count >= width of type");
6716 /* Use the type of the value to be shifted. */
6717 result_type = type0;
6718 /* Convert the shift-count to an integer, regardless of size
6719 of value being shifted. */
6720 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6721 op1 = convert (integer_type_node, op1);
6722 /* Avoid converting op1 to result_type later. */
6723 converted = 1;
6725 break;
6727 case RROTATE_EXPR:
6728 case LROTATE_EXPR:
6729 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6731 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6733 if (tree_int_cst_sgn (op1) < 0)
6734 warning ("shift count is negative");
6735 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6736 warning ("shift count >= width of type");
6739 /* Use the type of the value to be shifted. */
6740 result_type = type0;
6741 /* Convert the shift-count to an integer, regardless of size
6742 of value being shifted. */
6743 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6744 op1 = convert (integer_type_node, op1);
6745 /* Avoid converting op1 to result_type later. */
6746 converted = 1;
6748 break;
6750 case EQ_EXPR:
6751 case NE_EXPR:
6752 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6753 warning ("comparing floating point with == or != is unsafe");
6754 /* Result of comparison is always int,
6755 but don't convert the args to int! */
6756 build_type = integer_type_node;
6757 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6758 || code0 == COMPLEX_TYPE
6759 || code0 == VECTOR_TYPE)
6760 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6761 || code1 == COMPLEX_TYPE
6762 || code1 == VECTOR_TYPE))
6763 short_compare = 1;
6764 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6766 tree tt0 = TREE_TYPE (type0);
6767 tree tt1 = TREE_TYPE (type1);
6768 /* Anything compares with void *. void * compares with anything.
6769 Otherwise, the targets must be compatible
6770 and both must be object or both incomplete. */
6771 if (comp_target_types (type0, type1, 1))
6772 result_type = common_type (type0, type1);
6773 else if (VOID_TYPE_P (tt0))
6775 /* op0 != orig_op0 detects the case of something
6776 whose value is 0 but which isn't a valid null ptr const. */
6777 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6778 && TREE_CODE (tt1) == FUNCTION_TYPE)
6779 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6781 else if (VOID_TYPE_P (tt1))
6783 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6784 && TREE_CODE (tt0) == FUNCTION_TYPE)
6785 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6787 else
6788 pedwarn ("comparison of distinct pointer types lacks a cast");
6790 if (result_type == NULL_TREE)
6791 result_type = ptr_type_node;
6793 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6794 && integer_zerop (op1))
6795 result_type = type0;
6796 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6797 && integer_zerop (op0))
6798 result_type = type1;
6799 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6801 result_type = type0;
6802 pedwarn ("comparison between pointer and integer");
6804 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6806 result_type = type1;
6807 pedwarn ("comparison between pointer and integer");
6809 break;
6811 case MAX_EXPR:
6812 case MIN_EXPR:
6813 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6814 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6815 shorten = 1;
6816 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6818 if (comp_target_types (type0, type1, 1))
6820 result_type = common_type (type0, type1);
6821 if (pedantic
6822 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6823 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6825 else
6827 result_type = ptr_type_node;
6828 pedwarn ("comparison of distinct pointer types lacks a cast");
6831 break;
6833 case LE_EXPR:
6834 case GE_EXPR:
6835 case LT_EXPR:
6836 case GT_EXPR:
6837 build_type = integer_type_node;
6838 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6839 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6840 short_compare = 1;
6841 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6843 if (comp_target_types (type0, type1, 1))
6845 result_type = common_type (type0, type1);
6846 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6847 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6848 pedwarn ("comparison of complete and incomplete pointers");
6849 else if (pedantic
6850 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6851 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6853 else
6855 result_type = ptr_type_node;
6856 pedwarn ("comparison of distinct pointer types lacks a cast");
6859 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6860 && integer_zerop (op1))
6862 result_type = type0;
6863 if (pedantic || extra_warnings)
6864 pedwarn ("ordered comparison of pointer with integer zero");
6866 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6867 && integer_zerop (op0))
6869 result_type = type1;
6870 if (pedantic)
6871 pedwarn ("ordered comparison of pointer with integer zero");
6873 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6875 result_type = type0;
6876 pedwarn ("comparison between pointer and integer");
6878 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6880 result_type = type1;
6881 pedwarn ("comparison between pointer and integer");
6883 break;
6885 case UNORDERED_EXPR:
6886 case ORDERED_EXPR:
6887 case UNLT_EXPR:
6888 case UNLE_EXPR:
6889 case UNGT_EXPR:
6890 case UNGE_EXPR:
6891 case UNEQ_EXPR:
6892 build_type = integer_type_node;
6893 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6895 error ("unordered comparison on non-floating point argument");
6896 return error_mark_node;
6898 common = 1;
6899 break;
6901 default:
6902 break;
6905 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6906 || code0 == VECTOR_TYPE)
6908 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6909 || code1 == VECTOR_TYPE))
6911 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6913 if (shorten || common || short_compare)
6914 result_type = common_type (type0, type1);
6916 /* For certain operations (which identify themselves by shorten != 0)
6917 if both args were extended from the same smaller type,
6918 do the arithmetic in that type and then extend.
6920 shorten !=0 and !=1 indicates a bitwise operation.
6921 For them, this optimization is safe only if
6922 both args are zero-extended or both are sign-extended.
6923 Otherwise, we might change the result.
6924 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6925 but calculated in (unsigned short) it would be (unsigned short)-1. */
6927 if (shorten && none_complex)
6929 int unsigned0, unsigned1;
6930 tree arg0 = get_narrower (op0, &unsigned0);
6931 tree arg1 = get_narrower (op1, &unsigned1);
6932 /* UNS is 1 if the operation to be done is an unsigned one. */
6933 int uns = TREE_UNSIGNED (result_type);
6934 tree type;
6936 final_type = result_type;
6938 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6939 but it *requires* conversion to FINAL_TYPE. */
6941 if ((TYPE_PRECISION (TREE_TYPE (op0))
6942 == TYPE_PRECISION (TREE_TYPE (arg0)))
6943 && TREE_TYPE (op0) != final_type)
6944 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6945 if ((TYPE_PRECISION (TREE_TYPE (op1))
6946 == TYPE_PRECISION (TREE_TYPE (arg1)))
6947 && TREE_TYPE (op1) != final_type)
6948 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6950 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6952 /* For bitwise operations, signedness of nominal type
6953 does not matter. Consider only how operands were extended. */
6954 if (shorten == -1)
6955 uns = unsigned0;
6957 /* Note that in all three cases below we refrain from optimizing
6958 an unsigned operation on sign-extended args.
6959 That would not be valid. */
6961 /* Both args variable: if both extended in same way
6962 from same width, do it in that width.
6963 Do it unsigned if args were zero-extended. */
6964 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6965 < TYPE_PRECISION (result_type))
6966 && (TYPE_PRECISION (TREE_TYPE (arg1))
6967 == TYPE_PRECISION (TREE_TYPE (arg0)))
6968 && unsigned0 == unsigned1
6969 && (unsigned0 || !uns))
6970 result_type
6971 = c_common_signed_or_unsigned_type
6972 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6973 else if (TREE_CODE (arg0) == INTEGER_CST
6974 && (unsigned1 || !uns)
6975 && (TYPE_PRECISION (TREE_TYPE (arg1))
6976 < TYPE_PRECISION (result_type))
6977 && (type
6978 = c_common_signed_or_unsigned_type (unsigned1,
6979 TREE_TYPE (arg1)),
6980 int_fits_type_p (arg0, type)))
6981 result_type = type;
6982 else if (TREE_CODE (arg1) == INTEGER_CST
6983 && (unsigned0 || !uns)
6984 && (TYPE_PRECISION (TREE_TYPE (arg0))
6985 < TYPE_PRECISION (result_type))
6986 && (type
6987 = c_common_signed_or_unsigned_type (unsigned0,
6988 TREE_TYPE (arg0)),
6989 int_fits_type_p (arg1, type)))
6990 result_type = type;
6993 /* Shifts can be shortened if shifting right. */
6995 if (short_shift)
6997 int unsigned_arg;
6998 tree arg0 = get_narrower (op0, &unsigned_arg);
7000 final_type = result_type;
7002 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7003 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
7005 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7006 /* We can shorten only if the shift count is less than the
7007 number of bits in the smaller type size. */
7008 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7009 /* We cannot drop an unsigned shift after sign-extension. */
7010 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
7012 /* Do an unsigned shift if the operand was zero-extended. */
7013 result_type
7014 = c_common_signed_or_unsigned_type (unsigned_arg,
7015 TREE_TYPE (arg0));
7016 /* Convert value-to-be-shifted to that type. */
7017 if (TREE_TYPE (op0) != result_type)
7018 op0 = convert (result_type, op0);
7019 converted = 1;
7023 /* Comparison operations are shortened too but differently.
7024 They identify themselves by setting short_compare = 1. */
7026 if (short_compare)
7028 /* Don't write &op0, etc., because that would prevent op0
7029 from being kept in a register.
7030 Instead, make copies of the our local variables and
7031 pass the copies by reference, then copy them back afterward. */
7032 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7033 enum tree_code xresultcode = resultcode;
7034 tree val
7035 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7037 if (val != 0)
7038 return val;
7040 op0 = xop0, op1 = xop1;
7041 converted = 1;
7042 resultcode = xresultcode;
7044 if (warn_sign_compare && skip_evaluation == 0)
7046 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7047 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7048 int unsignedp0, unsignedp1;
7049 tree primop0 = get_narrower (op0, &unsignedp0);
7050 tree primop1 = get_narrower (op1, &unsignedp1);
7052 xop0 = orig_op0;
7053 xop1 = orig_op1;
7054 STRIP_TYPE_NOPS (xop0);
7055 STRIP_TYPE_NOPS (xop1);
7057 /* Give warnings for comparisons between signed and unsigned
7058 quantities that may fail.
7060 Do the checking based on the original operand trees, so that
7061 casts will be considered, but default promotions won't be.
7063 Do not warn if the comparison is being done in a signed type,
7064 since the signed type will only be chosen if it can represent
7065 all the values of the unsigned type. */
7066 if (! TREE_UNSIGNED (result_type))
7067 /* OK */;
7068 /* Do not warn if both operands are the same signedness. */
7069 else if (op0_signed == op1_signed)
7070 /* OK */;
7071 else
7073 tree sop, uop;
7075 if (op0_signed)
7076 sop = xop0, uop = xop1;
7077 else
7078 sop = xop1, uop = xop0;
7080 /* Do not warn if the signed quantity is an
7081 unsuffixed integer literal (or some static
7082 constant expression involving such literals or a
7083 conditional expression involving such literals)
7084 and it is non-negative. */
7085 if (c_tree_expr_nonnegative_p (sop))
7086 /* OK */;
7087 /* Do not warn if the comparison is an equality operation,
7088 the unsigned quantity is an integral constant, and it
7089 would fit in the result if the result were signed. */
7090 else if (TREE_CODE (uop) == INTEGER_CST
7091 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7092 && int_fits_type_p
7093 (uop, c_common_signed_type (result_type)))
7094 /* OK */;
7095 /* Do not warn if the unsigned quantity is an enumeration
7096 constant and its maximum value would fit in the result
7097 if the result were signed. */
7098 else if (TREE_CODE (uop) == INTEGER_CST
7099 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7100 && int_fits_type_p
7101 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7102 c_common_signed_type (result_type)))
7103 /* OK */;
7104 else
7105 warning ("comparison between signed and unsigned");
7108 /* Warn if two unsigned values are being compared in a size
7109 larger than their original size, and one (and only one) is the
7110 result of a `~' operator. This comparison will always fail.
7112 Also warn if one operand is a constant, and the constant
7113 does not have all bits set that are set in the ~ operand
7114 when it is extended. */
7116 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7117 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7119 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7120 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7121 &unsignedp0);
7122 else
7123 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7124 &unsignedp1);
7126 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7128 tree primop;
7129 HOST_WIDE_INT constant, mask;
7130 int unsignedp, bits;
7132 if (host_integerp (primop0, 0))
7134 primop = primop1;
7135 unsignedp = unsignedp1;
7136 constant = tree_low_cst (primop0, 0);
7138 else
7140 primop = primop0;
7141 unsignedp = unsignedp0;
7142 constant = tree_low_cst (primop1, 0);
7145 bits = TYPE_PRECISION (TREE_TYPE (primop));
7146 if (bits < TYPE_PRECISION (result_type)
7147 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7149 mask = (~ (HOST_WIDE_INT) 0) << bits;
7150 if ((mask & constant) != mask)
7151 warning ("comparison of promoted ~unsigned with constant");
7154 else if (unsignedp0 && unsignedp1
7155 && (TYPE_PRECISION (TREE_TYPE (primop0))
7156 < TYPE_PRECISION (result_type))
7157 && (TYPE_PRECISION (TREE_TYPE (primop1))
7158 < TYPE_PRECISION (result_type)))
7159 warning ("comparison of promoted ~unsigned with unsigned");
7165 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7166 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7167 Then the expression will be built.
7168 It will be given type FINAL_TYPE if that is nonzero;
7169 otherwise, it will be given type RESULT_TYPE. */
7171 if (!result_type)
7173 binary_op_error (code);
7174 return error_mark_node;
7177 if (! converted)
7179 if (TREE_TYPE (op0) != result_type)
7180 op0 = convert (result_type, op0);
7181 if (TREE_TYPE (op1) != result_type)
7182 op1 = convert (result_type, op1);
7185 if (build_type == NULL_TREE)
7186 build_type = result_type;
7189 tree result = build (resultcode, build_type, op0, op1);
7190 tree folded;
7192 /* Treat expressions in initializers specially as they can't trap. */
7193 folded = initializer_stack ? fold_initializer (result)
7194 : fold (result);
7195 if (folded == result)
7196 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7197 if (final_type != 0)
7198 return convert (final_type, folded);
7199 return folded;