2005-12-13 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / c-typeck.c
blob36c9bc0702c811c8741381a293c7ec55551e2f75
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (tree, tree);
80 static int comp_target_types (tree, tree);
81 static int function_types_compatible_p (tree, tree);
82 static int type_lists_compatible_p (tree, tree);
83 static tree decl_constant_value_for_broken_optimization (tree);
84 static tree lookup_field (tree, tree);
85 static tree convert_arguments (tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static void push_array_bounds (int);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
109 static int comptypes_internal (tree, tree);
111 /* Return true if EXP is a null pointer constant, false otherwise. */
113 static bool
114 null_pointer_constant_p (tree expr)
116 /* This should really operate on c_expr structures, but they aren't
117 yet available everywhere required. */
118 tree type = TREE_TYPE (expr);
119 return (TREE_CODE (expr) == INTEGER_CST
120 && !TREE_CONSTANT_OVERFLOW (expr)
121 && integer_zerop (expr)
122 && (INTEGRAL_TYPE_P (type)
123 || (TREE_CODE (type) == POINTER_TYPE
124 && VOID_TYPE_P (TREE_TYPE (type))
125 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
127 \f/* This is a cache to hold if two types are compatible or not. */
129 struct tagged_tu_seen_cache {
130 const struct tagged_tu_seen_cache * next;
131 tree t1;
132 tree t2;
133 /* The return value of tagged_types_tu_compatible_p if we had seen
134 these two types already. */
135 int val;
138 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
139 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
141 /* Do `exp = require_complete_type (exp);' to make sure exp
142 does not have an incomplete type. (That includes void types.) */
144 tree
145 require_complete_type (tree value)
147 tree type = TREE_TYPE (value);
149 if (value == error_mark_node || type == error_mark_node)
150 return error_mark_node;
152 /* First, detect a valid value with a complete type. */
153 if (COMPLETE_TYPE_P (type))
154 return value;
156 c_incomplete_type_error (value, type);
157 return error_mark_node;
160 /* Print an error message for invalid use of an incomplete type.
161 VALUE is the expression that was used (or 0 if that isn't known)
162 and TYPE is the type that was invalid. */
164 void
165 c_incomplete_type_error (tree value, tree type)
167 const char *type_code_string;
169 /* Avoid duplicate error message. */
170 if (TREE_CODE (type) == ERROR_MARK)
171 return;
173 if (value != 0 && (TREE_CODE (value) == VAR_DECL
174 || TREE_CODE (value) == PARM_DECL))
175 error ("%qD has an incomplete type", value);
176 else
178 retry:
179 /* We must print an error message. Be clever about what it says. */
181 switch (TREE_CODE (type))
183 case RECORD_TYPE:
184 type_code_string = "struct";
185 break;
187 case UNION_TYPE:
188 type_code_string = "union";
189 break;
191 case ENUMERAL_TYPE:
192 type_code_string = "enum";
193 break;
195 case VOID_TYPE:
196 error ("invalid use of void expression");
197 return;
199 case ARRAY_TYPE:
200 if (TYPE_DOMAIN (type))
202 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
204 error ("invalid use of flexible array member");
205 return;
207 type = TREE_TYPE (type);
208 goto retry;
210 error ("invalid use of array with unspecified bounds");
211 return;
213 default:
214 gcc_unreachable ();
217 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
218 error ("invalid use of undefined type %<%s %E%>",
219 type_code_string, TYPE_NAME (type));
220 else
221 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
222 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
226 /* Given a type, apply default promotions wrt unnamed function
227 arguments and return the new type. */
229 tree
230 c_type_promotes_to (tree type)
232 if (TYPE_MAIN_VARIANT (type) == float_type_node)
233 return double_type_node;
235 if (c_promoting_integer_type_p (type))
237 /* Preserve unsignedness if not really getting any wider. */
238 if (TYPE_UNSIGNED (type)
239 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
240 return unsigned_type_node;
241 return integer_type_node;
244 return type;
247 /* Return a variant of TYPE which has all the type qualifiers of LIKE
248 as well as those of TYPE. */
250 static tree
251 qualify_type (tree type, tree like)
253 return c_build_qualified_type (type,
254 TYPE_QUALS (type) | TYPE_QUALS (like));
257 /* Return the composite type of two compatible types.
259 We assume that comptypes has already been done and returned
260 nonzero; if that isn't so, this may crash. In particular, we
261 assume that qualifiers match. */
263 tree
264 composite_type (tree t1, tree t2)
266 enum tree_code code1;
267 enum tree_code code2;
268 tree attributes;
270 /* Save time if the two types are the same. */
272 if (t1 == t2) return t1;
274 /* If one type is nonsense, use the other. */
275 if (t1 == error_mark_node)
276 return t2;
277 if (t2 == error_mark_node)
278 return t1;
280 code1 = TREE_CODE (t1);
281 code2 = TREE_CODE (t2);
283 /* Merge the attributes. */
284 attributes = targetm.merge_type_attributes (t1, t2);
286 /* If one is an enumerated type and the other is the compatible
287 integer type, the composite type might be either of the two
288 (DR#013 question 3). For consistency, use the enumerated type as
289 the composite type. */
291 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
292 return t1;
293 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
294 return t2;
296 gcc_assert (code1 == code2);
298 switch (code1)
300 case POINTER_TYPE:
301 /* For two pointers, do this recursively on the target type. */
303 tree pointed_to_1 = TREE_TYPE (t1);
304 tree pointed_to_2 = TREE_TYPE (t2);
305 tree target = composite_type (pointed_to_1, pointed_to_2);
306 t1 = build_pointer_type (target);
307 t1 = build_type_attribute_variant (t1, attributes);
308 return qualify_type (t1, t2);
311 case ARRAY_TYPE:
313 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
314 int quals;
315 tree unqual_elt;
316 tree d1 = TYPE_DOMAIN (t1);
317 tree d2 = TYPE_DOMAIN (t2);
318 bool d1_variable, d2_variable;
319 bool d1_zero, d2_zero;
321 /* We should not have any type quals on arrays at all. */
322 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
324 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
325 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
327 d1_variable = (!d1_zero
328 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
329 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
330 d2_variable = (!d2_zero
331 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
332 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
334 /* Save space: see if the result is identical to one of the args. */
335 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
336 && (d2_variable || d2_zero || !d1_variable))
337 return build_type_attribute_variant (t1, attributes);
338 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
339 && (d1_variable || d1_zero || !d2_variable))
340 return build_type_attribute_variant (t2, attributes);
342 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
343 return build_type_attribute_variant (t1, attributes);
344 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
345 return build_type_attribute_variant (t2, attributes);
347 /* Merge the element types, and have a size if either arg has
348 one. We may have qualifiers on the element types. To set
349 up TYPE_MAIN_VARIANT correctly, we need to form the
350 composite of the unqualified types and add the qualifiers
351 back at the end. */
352 quals = TYPE_QUALS (strip_array_types (elt));
353 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
354 t1 = build_array_type (unqual_elt,
355 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
356 && (d2_variable
357 || d2_zero
358 || !d1_variable))
359 ? t1
360 : t2));
361 t1 = c_build_qualified_type (t1, quals);
362 return build_type_attribute_variant (t1, attributes);
365 case FUNCTION_TYPE:
366 /* Function types: prefer the one that specified arg types.
367 If both do, merge the arg types. Also merge the return types. */
369 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
370 tree p1 = TYPE_ARG_TYPES (t1);
371 tree p2 = TYPE_ARG_TYPES (t2);
372 int len;
373 tree newargs, n;
374 int i;
376 /* Save space: see if the result is identical to one of the args. */
377 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
378 return build_type_attribute_variant (t1, attributes);
379 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
380 return build_type_attribute_variant (t2, attributes);
382 /* Simple way if one arg fails to specify argument types. */
383 if (TYPE_ARG_TYPES (t1) == 0)
385 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
386 t1 = build_type_attribute_variant (t1, attributes);
387 return qualify_type (t1, t2);
389 if (TYPE_ARG_TYPES (t2) == 0)
391 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
392 t1 = build_type_attribute_variant (t1, attributes);
393 return qualify_type (t1, t2);
396 /* If both args specify argument types, we must merge the two
397 lists, argument by argument. */
398 /* Tell global_bindings_p to return false so that variable_size
399 doesn't die on VLAs in parameter types. */
400 c_override_global_bindings_to_false = true;
402 len = list_length (p1);
403 newargs = 0;
405 for (i = 0; i < len; i++)
406 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
408 n = newargs;
410 for (; p1;
411 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
413 /* A null type means arg type is not specified.
414 Take whatever the other function type has. */
415 if (TREE_VALUE (p1) == 0)
417 TREE_VALUE (n) = TREE_VALUE (p2);
418 goto parm_done;
420 if (TREE_VALUE (p2) == 0)
422 TREE_VALUE (n) = TREE_VALUE (p1);
423 goto parm_done;
426 /* Given wait (union {union wait *u; int *i} *)
427 and wait (union wait *),
428 prefer union wait * as type of parm. */
429 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
430 && TREE_VALUE (p1) != TREE_VALUE (p2))
432 tree memb;
433 tree mv2 = TREE_VALUE (p2);
434 if (mv2 && mv2 != error_mark_node
435 && TREE_CODE (mv2) != ARRAY_TYPE)
436 mv2 = TYPE_MAIN_VARIANT (mv2);
437 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
438 memb; memb = TREE_CHAIN (memb))
440 tree mv3 = TREE_TYPE (memb);
441 if (mv3 && mv3 != error_mark_node
442 && TREE_CODE (mv3) != ARRAY_TYPE)
443 mv3 = TYPE_MAIN_VARIANT (mv3);
444 if (comptypes (mv3, mv2))
446 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
447 TREE_VALUE (p2));
448 if (pedantic)
449 pedwarn ("function types not truly compatible in ISO C");
450 goto parm_done;
454 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
455 && TREE_VALUE (p2) != TREE_VALUE (p1))
457 tree memb;
458 tree mv1 = TREE_VALUE (p1);
459 if (mv1 && mv1 != error_mark_node
460 && TREE_CODE (mv1) != ARRAY_TYPE)
461 mv1 = TYPE_MAIN_VARIANT (mv1);
462 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
463 memb; memb = TREE_CHAIN (memb))
465 tree mv3 = TREE_TYPE (memb);
466 if (mv3 && mv3 != error_mark_node
467 && TREE_CODE (mv3) != ARRAY_TYPE)
468 mv3 = TYPE_MAIN_VARIANT (mv3);
469 if (comptypes (mv3, mv1))
471 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
472 TREE_VALUE (p1));
473 if (pedantic)
474 pedwarn ("function types not truly compatible in ISO C");
475 goto parm_done;
479 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
480 parm_done: ;
483 c_override_global_bindings_to_false = false;
484 t1 = build_function_type (valtype, newargs);
485 t1 = qualify_type (t1, t2);
486 /* ... falls through ... */
489 default:
490 return build_type_attribute_variant (t1, attributes);
495 /* Return the type of a conditional expression between pointers to
496 possibly differently qualified versions of compatible types.
498 We assume that comp_target_types has already been done and returned
499 nonzero; if that isn't so, this may crash. */
501 static tree
502 common_pointer_type (tree t1, tree t2)
504 tree attributes;
505 tree pointed_to_1, mv1;
506 tree pointed_to_2, mv2;
507 tree target;
509 /* Save time if the two types are the same. */
511 if (t1 == t2) return t1;
513 /* If one type is nonsense, use the other. */
514 if (t1 == error_mark_node)
515 return t2;
516 if (t2 == error_mark_node)
517 return t1;
519 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
520 && TREE_CODE (t2) == POINTER_TYPE);
522 /* Merge the attributes. */
523 attributes = targetm.merge_type_attributes (t1, t2);
525 /* Find the composite type of the target types, and combine the
526 qualifiers of the two types' targets. Do not lose qualifiers on
527 array element types by taking the TYPE_MAIN_VARIANT. */
528 mv1 = pointed_to_1 = TREE_TYPE (t1);
529 mv2 = pointed_to_2 = TREE_TYPE (t2);
530 if (TREE_CODE (mv1) != ARRAY_TYPE)
531 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
532 if (TREE_CODE (mv2) != ARRAY_TYPE)
533 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
534 target = composite_type (mv1, mv2);
535 t1 = build_pointer_type (c_build_qualified_type
536 (target,
537 TYPE_QUALS (pointed_to_1) |
538 TYPE_QUALS (pointed_to_2)));
539 return build_type_attribute_variant (t1, attributes);
542 /* Return the common type for two arithmetic types under the usual
543 arithmetic conversions. The default conversions have already been
544 applied, and enumerated types converted to their compatible integer
545 types. The resulting type is unqualified and has no attributes.
547 This is the type for the result of most arithmetic operations
548 if the operands have the given two types. */
550 static tree
551 c_common_type (tree t1, tree t2)
553 enum tree_code code1;
554 enum tree_code code2;
556 /* If one type is nonsense, use the other. */
557 if (t1 == error_mark_node)
558 return t2;
559 if (t2 == error_mark_node)
560 return t1;
562 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
563 t1 = TYPE_MAIN_VARIANT (t1);
565 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
566 t2 = TYPE_MAIN_VARIANT (t2);
568 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
569 t1 = build_type_attribute_variant (t1, NULL_TREE);
571 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
572 t2 = build_type_attribute_variant (t2, NULL_TREE);
574 /* Save time if the two types are the same. */
576 if (t1 == t2) return t1;
578 code1 = TREE_CODE (t1);
579 code2 = TREE_CODE (t2);
581 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
582 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
583 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
584 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
586 /* If one type is a vector type, return that type. (How the usual
587 arithmetic conversions apply to the vector types extension is not
588 precisely specified.) */
589 if (code1 == VECTOR_TYPE)
590 return t1;
592 if (code2 == VECTOR_TYPE)
593 return t2;
595 /* If one type is complex, form the common type of the non-complex
596 components, then make that complex. Use T1 or T2 if it is the
597 required type. */
598 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
600 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
601 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
602 tree subtype = c_common_type (subtype1, subtype2);
604 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
605 return t1;
606 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
607 return t2;
608 else
609 return build_complex_type (subtype);
612 /* If only one is real, use it as the result. */
614 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
615 return t1;
617 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
618 return t2;
620 /* Both real or both integers; use the one with greater precision. */
622 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
623 return t1;
624 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
625 return t2;
627 /* Same precision. Prefer long longs to longs to ints when the
628 same precision, following the C99 rules on integer type rank
629 (which are equivalent to the C90 rules for C90 types). */
631 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
632 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
633 return long_long_unsigned_type_node;
635 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
636 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
638 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
639 return long_long_unsigned_type_node;
640 else
641 return long_long_integer_type_node;
644 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
645 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
646 return long_unsigned_type_node;
648 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
649 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
651 /* But preserve unsignedness from the other type,
652 since long cannot hold all the values of an unsigned int. */
653 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
654 return long_unsigned_type_node;
655 else
656 return long_integer_type_node;
659 /* Likewise, prefer long double to double even if same size. */
660 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
661 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
662 return long_double_type_node;
664 /* Otherwise prefer the unsigned one. */
666 if (TYPE_UNSIGNED (t1))
667 return t1;
668 else
669 return t2;
672 /* Wrapper around c_common_type that is used by c-common.c and other
673 front end optimizations that remove promotions. ENUMERAL_TYPEs
674 are allowed here and are converted to their compatible integer types.
675 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
676 preferably a non-Boolean type as the common type. */
677 tree
678 common_type (tree t1, tree t2)
680 if (TREE_CODE (t1) == ENUMERAL_TYPE)
681 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
682 if (TREE_CODE (t2) == ENUMERAL_TYPE)
683 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
685 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
686 if (TREE_CODE (t1) == BOOLEAN_TYPE
687 && TREE_CODE (t2) == BOOLEAN_TYPE)
688 return boolean_type_node;
690 /* If either type is BOOLEAN_TYPE, then return the other. */
691 if (TREE_CODE (t1) == BOOLEAN_TYPE)
692 return t2;
693 if (TREE_CODE (t2) == BOOLEAN_TYPE)
694 return t1;
696 return c_common_type (t1, t2);
699 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
700 or various other operations. Return 2 if they are compatible
701 but a warning may be needed if you use them together. */
704 comptypes (tree type1, tree type2)
706 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
707 int val;
709 val = comptypes_internal (type1, type2);
710 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
712 return val;
714 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
715 or various other operations. Return 2 if they are compatible
716 but a warning may be needed if you use them together. This
717 differs from comptypes, in that we don't free the seen types. */
719 static int
720 comptypes_internal (tree type1, tree type2)
722 tree t1 = type1;
723 tree t2 = type2;
724 int attrval, val;
726 /* Suppress errors caused by previously reported errors. */
728 if (t1 == t2 || !t1 || !t2
729 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
730 return 1;
732 /* If either type is the internal version of sizetype, return the
733 language version. */
734 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
735 && TYPE_ORIG_SIZE_TYPE (t1))
736 t1 = TYPE_ORIG_SIZE_TYPE (t1);
738 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
739 && TYPE_ORIG_SIZE_TYPE (t2))
740 t2 = TYPE_ORIG_SIZE_TYPE (t2);
743 /* Enumerated types are compatible with integer types, but this is
744 not transitive: two enumerated types in the same translation unit
745 are compatible with each other only if they are the same type. */
747 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
748 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
749 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
750 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
752 if (t1 == t2)
753 return 1;
755 /* Different classes of types can't be compatible. */
757 if (TREE_CODE (t1) != TREE_CODE (t2))
758 return 0;
760 /* Qualifiers must match. C99 6.7.3p9 */
762 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
763 return 0;
765 /* Allow for two different type nodes which have essentially the same
766 definition. Note that we already checked for equality of the type
767 qualifiers (just above). */
769 if (TREE_CODE (t1) != ARRAY_TYPE
770 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
771 return 1;
773 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
774 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
775 return 0;
777 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
778 val = 0;
780 switch (TREE_CODE (t1))
782 case POINTER_TYPE:
783 /* Do not remove mode or aliasing information. */
784 if (TYPE_MODE (t1) != TYPE_MODE (t2)
785 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
786 break;
787 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
788 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
789 break;
791 case FUNCTION_TYPE:
792 val = function_types_compatible_p (t1, t2);
793 break;
795 case ARRAY_TYPE:
797 tree d1 = TYPE_DOMAIN (t1);
798 tree d2 = TYPE_DOMAIN (t2);
799 bool d1_variable, d2_variable;
800 bool d1_zero, d2_zero;
801 val = 1;
803 /* Target types must match incl. qualifiers. */
804 if (TREE_TYPE (t1) != TREE_TYPE (t2)
805 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
806 return 0;
808 /* Sizes must match unless one is missing or variable. */
809 if (d1 == 0 || d2 == 0 || d1 == d2)
810 break;
812 d1_zero = !TYPE_MAX_VALUE (d1);
813 d2_zero = !TYPE_MAX_VALUE (d2);
815 d1_variable = (!d1_zero
816 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
817 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
818 d2_variable = (!d2_zero
819 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
820 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
822 if (d1_variable || d2_variable)
823 break;
824 if (d1_zero && d2_zero)
825 break;
826 if (d1_zero || d2_zero
827 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
828 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
829 val = 0;
831 break;
834 case ENUMERAL_TYPE:
835 case RECORD_TYPE:
836 case UNION_TYPE:
837 if (val != 1 && !same_translation_unit_p (t1, t2))
839 if (attrval != 2)
840 return tagged_types_tu_compatible_p (t1, t2);
841 val = tagged_types_tu_compatible_p (t1, t2);
843 break;
845 case VECTOR_TYPE:
846 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
847 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
848 break;
850 default:
851 break;
853 return attrval == 2 && val == 1 ? 2 : val;
856 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
857 ignoring their qualifiers. */
859 static int
860 comp_target_types (tree ttl, tree ttr)
862 int val;
863 tree mvl, mvr;
865 /* Do not lose qualifiers on element types of array types that are
866 pointer targets by taking their TYPE_MAIN_VARIANT. */
867 mvl = TREE_TYPE (ttl);
868 mvr = TREE_TYPE (ttr);
869 if (TREE_CODE (mvl) != ARRAY_TYPE)
870 mvl = TYPE_MAIN_VARIANT (mvl);
871 if (TREE_CODE (mvr) != ARRAY_TYPE)
872 mvr = TYPE_MAIN_VARIANT (mvr);
873 val = comptypes (mvl, mvr);
875 if (val == 2 && pedantic)
876 pedwarn ("types are not quite compatible");
877 return val;
880 /* Subroutines of `comptypes'. */
882 /* Determine whether two trees derive from the same translation unit.
883 If the CONTEXT chain ends in a null, that tree's context is still
884 being parsed, so if two trees have context chains ending in null,
885 they're in the same translation unit. */
887 same_translation_unit_p (tree t1, tree t2)
889 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
890 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
892 case tcc_declaration:
893 t1 = DECL_CONTEXT (t1); break;
894 case tcc_type:
895 t1 = TYPE_CONTEXT (t1); break;
896 case tcc_exceptional:
897 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
898 default: gcc_unreachable ();
901 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
902 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
904 case tcc_declaration:
905 t2 = DECL_CONTEXT (t2); break;
906 case tcc_type:
907 t2 = TYPE_CONTEXT (t2); break;
908 case tcc_exceptional:
909 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
910 default: gcc_unreachable ();
913 return t1 == t2;
916 /* Allocate the seen two types, assuming that they are compatible. */
918 static struct tagged_tu_seen_cache *
919 alloc_tagged_tu_seen_cache (tree t1, tree t2)
921 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
922 tu->next = tagged_tu_seen_base;
923 tu->t1 = t1;
924 tu->t2 = t2;
926 tagged_tu_seen_base = tu;
928 /* The C standard says that two structures in different translation
929 units are compatible with each other only if the types of their
930 fields are compatible (among other things). We assume that they
931 are compatible until proven otherwise when building the cache.
932 An example where this can occur is:
933 struct a
935 struct a *next;
937 If we are comparing this against a similar struct in another TU,
938 and did not assume they were compatible, we end up with an infinite
939 loop. */
940 tu->val = 1;
941 return tu;
944 /* Free the seen types until we get to TU_TIL. */
946 static void
947 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
949 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
950 while (tu != tu_til)
952 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
953 tu = tu1->next;
954 free (tu1);
956 tagged_tu_seen_base = tu_til;
959 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
960 compatible. If the two types are not the same (which has been
961 checked earlier), this can only happen when multiple translation
962 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
963 rules. */
965 static int
966 tagged_types_tu_compatible_p (tree t1, tree t2)
968 tree s1, s2;
969 bool needs_warning = false;
971 /* We have to verify that the tags of the types are the same. This
972 is harder than it looks because this may be a typedef, so we have
973 to go look at the original type. It may even be a typedef of a
974 typedef...
975 In the case of compiler-created builtin structs the TYPE_DECL
976 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
977 while (TYPE_NAME (t1)
978 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
979 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
980 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
982 while (TYPE_NAME (t2)
983 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
984 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
985 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
987 /* C90 didn't have the requirement that the two tags be the same. */
988 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
989 return 0;
991 /* C90 didn't say what happened if one or both of the types were
992 incomplete; we choose to follow C99 rules here, which is that they
993 are compatible. */
994 if (TYPE_SIZE (t1) == NULL
995 || TYPE_SIZE (t2) == NULL)
996 return 1;
999 const struct tagged_tu_seen_cache * tts_i;
1000 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1001 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1002 return tts_i->val;
1005 switch (TREE_CODE (t1))
1007 case ENUMERAL_TYPE:
1009 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1010 /* Speed up the case where the type values are in the same order. */
1011 tree tv1 = TYPE_VALUES (t1);
1012 tree tv2 = TYPE_VALUES (t2);
1014 if (tv1 == tv2)
1016 return 1;
1019 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1021 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1022 break;
1023 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1025 tu->val = 0;
1026 return 0;
1030 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1032 return 1;
1034 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1036 tu->val = 0;
1037 return 0;
1040 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1042 tu->val = 0;
1043 return 0;
1046 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1048 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1049 if (s2 == NULL
1050 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1052 tu->val = 0;
1053 return 0;
1056 return 1;
1059 case UNION_TYPE:
1061 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1062 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1064 tu->val = 0;
1065 return 0;
1068 /* Speed up the common case where the fields are in the same order. */
1069 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1070 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1072 int result;
1075 if (DECL_NAME (s1) == NULL
1076 || DECL_NAME (s1) != DECL_NAME (s2))
1077 break;
1078 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1079 if (result == 0)
1081 tu->val = 0;
1082 return 0;
1084 if (result == 2)
1085 needs_warning = true;
1087 if (TREE_CODE (s1) == FIELD_DECL
1088 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1089 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1091 tu->val = 0;
1092 return 0;
1095 if (!s1 && !s2)
1097 tu->val = needs_warning ? 2 : 1;
1098 return tu->val;
1101 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1103 bool ok = false;
1105 if (DECL_NAME (s1) != NULL)
1106 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1107 if (DECL_NAME (s1) == DECL_NAME (s2))
1109 int result;
1110 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1111 if (result == 0)
1113 tu->val = 0;
1114 return 0;
1116 if (result == 2)
1117 needs_warning = true;
1119 if (TREE_CODE (s1) == FIELD_DECL
1120 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1121 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1122 break;
1124 ok = true;
1125 break;
1127 if (!ok)
1129 tu->val = 0;
1130 return 0;
1133 tu->val = needs_warning ? 2 : 10;
1134 return tu->val;
1137 case RECORD_TYPE:
1139 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1141 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1142 s1 && s2;
1143 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1145 int result;
1146 if (TREE_CODE (s1) != TREE_CODE (s2)
1147 || DECL_NAME (s1) != DECL_NAME (s2))
1148 break;
1149 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1150 if (result == 0)
1151 break;
1152 if (result == 2)
1153 needs_warning = true;
1155 if (TREE_CODE (s1) == FIELD_DECL
1156 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1157 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1158 break;
1160 if (s1 && s2)
1161 tu->val = 0;
1162 else
1163 tu->val = needs_warning ? 2 : 1;
1164 return tu->val;
1167 default:
1168 gcc_unreachable ();
1172 /* Return 1 if two function types F1 and F2 are compatible.
1173 If either type specifies no argument types,
1174 the other must specify a fixed number of self-promoting arg types.
1175 Otherwise, if one type specifies only the number of arguments,
1176 the other must specify that number of self-promoting arg types.
1177 Otherwise, the argument types must match. */
1179 static int
1180 function_types_compatible_p (tree f1, tree f2)
1182 tree args1, args2;
1183 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1184 int val = 1;
1185 int val1;
1186 tree ret1, ret2;
1188 ret1 = TREE_TYPE (f1);
1189 ret2 = TREE_TYPE (f2);
1191 /* 'volatile' qualifiers on a function's return type used to mean
1192 the function is noreturn. */
1193 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1194 pedwarn ("function return types not compatible due to %<volatile%>");
1195 if (TYPE_VOLATILE (ret1))
1196 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1197 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1198 if (TYPE_VOLATILE (ret2))
1199 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1200 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1201 val = comptypes_internal (ret1, ret2);
1202 if (val == 0)
1203 return 0;
1205 args1 = TYPE_ARG_TYPES (f1);
1206 args2 = TYPE_ARG_TYPES (f2);
1208 /* An unspecified parmlist matches any specified parmlist
1209 whose argument types don't need default promotions. */
1211 if (args1 == 0)
1213 if (!self_promoting_args_p (args2))
1214 return 0;
1215 /* If one of these types comes from a non-prototype fn definition,
1216 compare that with the other type's arglist.
1217 If they don't match, ask for a warning (but no error). */
1218 if (TYPE_ACTUAL_ARG_TYPES (f1)
1219 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1220 val = 2;
1221 return val;
1223 if (args2 == 0)
1225 if (!self_promoting_args_p (args1))
1226 return 0;
1227 if (TYPE_ACTUAL_ARG_TYPES (f2)
1228 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1229 val = 2;
1230 return val;
1233 /* Both types have argument lists: compare them and propagate results. */
1234 val1 = type_lists_compatible_p (args1, args2);
1235 return val1 != 1 ? val1 : val;
1238 /* Check two lists of types for compatibility,
1239 returning 0 for incompatible, 1 for compatible,
1240 or 2 for compatible with warning. */
1242 static int
1243 type_lists_compatible_p (tree args1, tree args2)
1245 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1246 int val = 1;
1247 int newval = 0;
1249 while (1)
1251 tree a1, mv1, a2, mv2;
1252 if (args1 == 0 && args2 == 0)
1253 return val;
1254 /* If one list is shorter than the other,
1255 they fail to match. */
1256 if (args1 == 0 || args2 == 0)
1257 return 0;
1258 mv1 = a1 = TREE_VALUE (args1);
1259 mv2 = a2 = TREE_VALUE (args2);
1260 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1261 mv1 = TYPE_MAIN_VARIANT (mv1);
1262 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1263 mv2 = TYPE_MAIN_VARIANT (mv2);
1264 /* A null pointer instead of a type
1265 means there is supposed to be an argument
1266 but nothing is specified about what type it has.
1267 So match anything that self-promotes. */
1268 if (a1 == 0)
1270 if (c_type_promotes_to (a2) != a2)
1271 return 0;
1273 else if (a2 == 0)
1275 if (c_type_promotes_to (a1) != a1)
1276 return 0;
1278 /* If one of the lists has an error marker, ignore this arg. */
1279 else if (TREE_CODE (a1) == ERROR_MARK
1280 || TREE_CODE (a2) == ERROR_MARK)
1282 else if (!(newval = comptypes_internal (mv1, mv2)))
1284 /* Allow wait (union {union wait *u; int *i} *)
1285 and wait (union wait *) to be compatible. */
1286 if (TREE_CODE (a1) == UNION_TYPE
1287 && (TYPE_NAME (a1) == 0
1288 || TYPE_TRANSPARENT_UNION (a1))
1289 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1290 && tree_int_cst_equal (TYPE_SIZE (a1),
1291 TYPE_SIZE (a2)))
1293 tree memb;
1294 for (memb = TYPE_FIELDS (a1);
1295 memb; memb = TREE_CHAIN (memb))
1297 tree mv3 = TREE_TYPE (memb);
1298 if (mv3 && mv3 != error_mark_node
1299 && TREE_CODE (mv3) != ARRAY_TYPE)
1300 mv3 = TYPE_MAIN_VARIANT (mv3);
1301 if (comptypes_internal (mv3, mv2))
1302 break;
1304 if (memb == 0)
1305 return 0;
1307 else if (TREE_CODE (a2) == UNION_TYPE
1308 && (TYPE_NAME (a2) == 0
1309 || TYPE_TRANSPARENT_UNION (a2))
1310 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1311 && tree_int_cst_equal (TYPE_SIZE (a2),
1312 TYPE_SIZE (a1)))
1314 tree memb;
1315 for (memb = TYPE_FIELDS (a2);
1316 memb; memb = TREE_CHAIN (memb))
1318 tree mv3 = TREE_TYPE (memb);
1319 if (mv3 && mv3 != error_mark_node
1320 && TREE_CODE (mv3) != ARRAY_TYPE)
1321 mv3 = TYPE_MAIN_VARIANT (mv3);
1322 if (comptypes_internal (mv3, mv1))
1323 break;
1325 if (memb == 0)
1326 return 0;
1328 else
1329 return 0;
1332 /* comptypes said ok, but record if it said to warn. */
1333 if (newval > val)
1334 val = newval;
1336 args1 = TREE_CHAIN (args1);
1337 args2 = TREE_CHAIN (args2);
1341 /* Compute the size to increment a pointer by. */
1343 static tree
1344 c_size_in_bytes (tree type)
1346 enum tree_code code = TREE_CODE (type);
1348 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1349 return size_one_node;
1351 if (!COMPLETE_OR_VOID_TYPE_P (type))
1353 error ("arithmetic on pointer to an incomplete type");
1354 return size_one_node;
1357 /* Convert in case a char is more than one unit. */
1358 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1359 size_int (TYPE_PRECISION (char_type_node)
1360 / BITS_PER_UNIT));
1363 /* Return either DECL or its known constant value (if it has one). */
1365 tree
1366 decl_constant_value (tree decl)
1368 if (/* Don't change a variable array bound or initial value to a constant
1369 in a place where a variable is invalid. Note that DECL_INITIAL
1370 isn't valid for a PARM_DECL. */
1371 current_function_decl != 0
1372 && TREE_CODE (decl) != PARM_DECL
1373 && !TREE_THIS_VOLATILE (decl)
1374 && TREE_READONLY (decl)
1375 && DECL_INITIAL (decl) != 0
1376 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1377 /* This is invalid if initial value is not constant.
1378 If it has either a function call, a memory reference,
1379 or a variable, then re-evaluating it could give different results. */
1380 && TREE_CONSTANT (DECL_INITIAL (decl))
1381 /* Check for cases where this is sub-optimal, even though valid. */
1382 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1383 return DECL_INITIAL (decl);
1384 return decl;
1387 /* Return either DECL or its known constant value (if it has one), but
1388 return DECL if pedantic or DECL has mode BLKmode. This is for
1389 bug-compatibility with the old behavior of decl_constant_value
1390 (before GCC 3.0); every use of this function is a bug and it should
1391 be removed before GCC 3.1. It is not appropriate to use pedantic
1392 in a way that affects optimization, and BLKmode is probably not the
1393 right test for avoiding misoptimizations either. */
1395 static tree
1396 decl_constant_value_for_broken_optimization (tree decl)
1398 tree ret;
1400 if (pedantic || DECL_MODE (decl) == BLKmode)
1401 return decl;
1403 ret = decl_constant_value (decl);
1404 /* Avoid unwanted tree sharing between the initializer and current
1405 function's body where the tree can be modified e.g. by the
1406 gimplifier. */
1407 if (ret != decl && TREE_STATIC (decl))
1408 ret = unshare_expr (ret);
1409 return ret;
1412 /* Convert the array expression EXP to a pointer. */
1413 static tree
1414 array_to_pointer_conversion (tree exp)
1416 tree orig_exp = exp;
1417 tree type = TREE_TYPE (exp);
1418 tree adr;
1419 tree restype = TREE_TYPE (type);
1420 tree ptrtype;
1422 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1424 STRIP_TYPE_NOPS (exp);
1426 if (TREE_NO_WARNING (orig_exp))
1427 TREE_NO_WARNING (exp) = 1;
1429 ptrtype = build_pointer_type (restype);
1431 if (TREE_CODE (exp) == INDIRECT_REF)
1432 return convert (ptrtype, TREE_OPERAND (exp, 0));
1434 if (TREE_CODE (exp) == VAR_DECL)
1436 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1437 ADDR_EXPR because it's the best way of representing what
1438 happens in C when we take the address of an array and place
1439 it in a pointer to the element type. */
1440 adr = build1 (ADDR_EXPR, ptrtype, exp);
1441 if (!c_mark_addressable (exp))
1442 return error_mark_node;
1443 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1444 return adr;
1447 /* This way is better for a COMPONENT_REF since it can
1448 simplify the offset for a component. */
1449 adr = build_unary_op (ADDR_EXPR, exp, 1);
1450 return convert (ptrtype, adr);
1453 /* Convert the function expression EXP to a pointer. */
1454 static tree
1455 function_to_pointer_conversion (tree exp)
1457 tree orig_exp = exp;
1459 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1461 STRIP_TYPE_NOPS (exp);
1463 if (TREE_NO_WARNING (orig_exp))
1464 TREE_NO_WARNING (exp) = 1;
1466 return build_unary_op (ADDR_EXPR, exp, 0);
1469 /* Perform the default conversion of arrays and functions to pointers.
1470 Return the result of converting EXP. For any other expression, just
1471 return EXP after removing NOPs. */
1473 struct c_expr
1474 default_function_array_conversion (struct c_expr exp)
1476 tree orig_exp = exp.value;
1477 tree type = TREE_TYPE (exp.value);
1478 enum tree_code code = TREE_CODE (type);
1480 switch (code)
1482 case ARRAY_TYPE:
1484 bool not_lvalue = false;
1485 bool lvalue_array_p;
1487 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1488 || TREE_CODE (exp.value) == NOP_EXPR
1489 || TREE_CODE (exp.value) == CONVERT_EXPR)
1490 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1492 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1493 not_lvalue = true;
1494 exp.value = TREE_OPERAND (exp.value, 0);
1497 if (TREE_NO_WARNING (orig_exp))
1498 TREE_NO_WARNING (exp.value) = 1;
1500 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1501 if (!flag_isoc99 && !lvalue_array_p)
1503 /* Before C99, non-lvalue arrays do not decay to pointers.
1504 Normally, using such an array would be invalid; but it can
1505 be used correctly inside sizeof or as a statement expression.
1506 Thus, do not give an error here; an error will result later. */
1507 return exp;
1510 exp.value = array_to_pointer_conversion (exp.value);
1512 break;
1513 case FUNCTION_TYPE:
1514 exp.value = function_to_pointer_conversion (exp.value);
1515 break;
1516 default:
1517 STRIP_TYPE_NOPS (exp.value);
1518 if (TREE_NO_WARNING (orig_exp))
1519 TREE_NO_WARNING (exp.value) = 1;
1520 break;
1523 return exp;
1527 /* EXP is an expression of integer type. Apply the integer promotions
1528 to it and return the promoted value. */
1530 tree
1531 perform_integral_promotions (tree exp)
1533 tree type = TREE_TYPE (exp);
1534 enum tree_code code = TREE_CODE (type);
1536 gcc_assert (INTEGRAL_TYPE_P (type));
1538 /* Normally convert enums to int,
1539 but convert wide enums to something wider. */
1540 if (code == ENUMERAL_TYPE)
1542 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1543 TYPE_PRECISION (integer_type_node)),
1544 ((TYPE_PRECISION (type)
1545 >= TYPE_PRECISION (integer_type_node))
1546 && TYPE_UNSIGNED (type)));
1548 return convert (type, exp);
1551 /* ??? This should no longer be needed now bit-fields have their
1552 proper types. */
1553 if (TREE_CODE (exp) == COMPONENT_REF
1554 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1555 /* If it's thinner than an int, promote it like a
1556 c_promoting_integer_type_p, otherwise leave it alone. */
1557 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1558 TYPE_PRECISION (integer_type_node)))
1559 return convert (integer_type_node, exp);
1561 if (c_promoting_integer_type_p (type))
1563 /* Preserve unsignedness if not really getting any wider. */
1564 if (TYPE_UNSIGNED (type)
1565 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1566 return convert (unsigned_type_node, exp);
1568 return convert (integer_type_node, exp);
1571 return exp;
1575 /* Perform default promotions for C data used in expressions.
1576 Enumeral types or short or char are converted to int.
1577 In addition, manifest constants symbols are replaced by their values. */
1579 tree
1580 default_conversion (tree exp)
1582 tree orig_exp;
1583 tree type = TREE_TYPE (exp);
1584 enum tree_code code = TREE_CODE (type);
1586 /* Functions and arrays have been converted during parsing. */
1587 gcc_assert (code != FUNCTION_TYPE);
1588 if (code == ARRAY_TYPE)
1589 return exp;
1591 /* Constants can be used directly unless they're not loadable. */
1592 if (TREE_CODE (exp) == CONST_DECL)
1593 exp = DECL_INITIAL (exp);
1595 /* Replace a nonvolatile const static variable with its value unless
1596 it is an array, in which case we must be sure that taking the
1597 address of the array produces consistent results. */
1598 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1600 exp = decl_constant_value_for_broken_optimization (exp);
1601 type = TREE_TYPE (exp);
1604 /* Strip no-op conversions. */
1605 orig_exp = exp;
1606 STRIP_TYPE_NOPS (exp);
1608 if (TREE_NO_WARNING (orig_exp))
1609 TREE_NO_WARNING (exp) = 1;
1611 if (INTEGRAL_TYPE_P (type))
1612 return perform_integral_promotions (exp);
1614 if (code == VOID_TYPE)
1616 error ("void value not ignored as it ought to be");
1617 return error_mark_node;
1619 return exp;
1622 /* Look up COMPONENT in a structure or union DECL.
1624 If the component name is not found, returns NULL_TREE. Otherwise,
1625 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1626 stepping down the chain to the component, which is in the last
1627 TREE_VALUE of the list. Normally the list is of length one, but if
1628 the component is embedded within (nested) anonymous structures or
1629 unions, the list steps down the chain to the component. */
1631 static tree
1632 lookup_field (tree decl, tree component)
1634 tree type = TREE_TYPE (decl);
1635 tree field;
1637 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1638 to the field elements. Use a binary search on this array to quickly
1639 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1640 will always be set for structures which have many elements. */
1642 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1644 int bot, top, half;
1645 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1647 field = TYPE_FIELDS (type);
1648 bot = 0;
1649 top = TYPE_LANG_SPECIFIC (type)->s->len;
1650 while (top - bot > 1)
1652 half = (top - bot + 1) >> 1;
1653 field = field_array[bot+half];
1655 if (DECL_NAME (field) == NULL_TREE)
1657 /* Step through all anon unions in linear fashion. */
1658 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1660 field = field_array[bot++];
1661 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1662 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1664 tree anon = lookup_field (field, component);
1666 if (anon)
1667 return tree_cons (NULL_TREE, field, anon);
1671 /* Entire record is only anon unions. */
1672 if (bot > top)
1673 return NULL_TREE;
1675 /* Restart the binary search, with new lower bound. */
1676 continue;
1679 if (DECL_NAME (field) == component)
1680 break;
1681 if (DECL_NAME (field) < component)
1682 bot += half;
1683 else
1684 top = bot + half;
1687 if (DECL_NAME (field_array[bot]) == component)
1688 field = field_array[bot];
1689 else if (DECL_NAME (field) != component)
1690 return NULL_TREE;
1692 else
1694 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1696 if (DECL_NAME (field) == NULL_TREE
1697 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1698 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1700 tree anon = lookup_field (field, component);
1702 if (anon)
1703 return tree_cons (NULL_TREE, field, anon);
1706 if (DECL_NAME (field) == component)
1707 break;
1710 if (field == NULL_TREE)
1711 return NULL_TREE;
1714 return tree_cons (NULL_TREE, field, NULL_TREE);
1717 /* Make an expression to refer to the COMPONENT field of
1718 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1720 tree
1721 build_component_ref (tree datum, tree component)
1723 tree type = TREE_TYPE (datum);
1724 enum tree_code code = TREE_CODE (type);
1725 tree field = NULL;
1726 tree ref;
1728 if (!objc_is_public (datum, component))
1729 return error_mark_node;
1731 /* See if there is a field or component with name COMPONENT. */
1733 if (code == RECORD_TYPE || code == UNION_TYPE)
1735 if (!COMPLETE_TYPE_P (type))
1737 c_incomplete_type_error (NULL_TREE, type);
1738 return error_mark_node;
1741 field = lookup_field (datum, component);
1743 if (!field)
1745 error ("%qT has no member named %qE", type, component);
1746 return error_mark_node;
1749 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1750 This might be better solved in future the way the C++ front
1751 end does it - by giving the anonymous entities each a
1752 separate name and type, and then have build_component_ref
1753 recursively call itself. We can't do that here. */
1756 tree subdatum = TREE_VALUE (field);
1758 if (TREE_TYPE (subdatum) == error_mark_node)
1759 return error_mark_node;
1761 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1762 NULL_TREE);
1763 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1764 TREE_READONLY (ref) = 1;
1765 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1766 TREE_THIS_VOLATILE (ref) = 1;
1768 if (TREE_DEPRECATED (subdatum))
1769 warn_deprecated_use (subdatum);
1771 datum = ref;
1773 field = TREE_CHAIN (field);
1775 while (field);
1777 return ref;
1779 else if (code != ERROR_MARK)
1780 error ("request for member %qE in something not a structure or union",
1781 component);
1783 return error_mark_node;
1786 /* Given an expression PTR for a pointer, return an expression
1787 for the value pointed to.
1788 ERRORSTRING is the name of the operator to appear in error messages. */
1790 tree
1791 build_indirect_ref (tree ptr, const char *errorstring)
1793 tree pointer = default_conversion (ptr);
1794 tree type = TREE_TYPE (pointer);
1796 if (TREE_CODE (type) == POINTER_TYPE)
1798 if (TREE_CODE (pointer) == ADDR_EXPR
1799 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1800 == TREE_TYPE (type)))
1801 return TREE_OPERAND (pointer, 0);
1802 else
1804 tree t = TREE_TYPE (type);
1805 tree ref;
1807 ref = build1 (INDIRECT_REF, t, pointer);
1809 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1811 error ("dereferencing pointer to incomplete type");
1812 return error_mark_node;
1814 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1815 warning (0, "dereferencing %<void *%> pointer");
1817 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1818 so that we get the proper error message if the result is used
1819 to assign to. Also, &* is supposed to be a no-op.
1820 And ANSI C seems to specify that the type of the result
1821 should be the const type. */
1822 /* A de-reference of a pointer to const is not a const. It is valid
1823 to change it via some other pointer. */
1824 TREE_READONLY (ref) = TYPE_READONLY (t);
1825 TREE_SIDE_EFFECTS (ref)
1826 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1827 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1828 return ref;
1831 else if (TREE_CODE (pointer) != ERROR_MARK)
1832 error ("invalid type argument of %qs", errorstring);
1833 return error_mark_node;
1836 /* This handles expressions of the form "a[i]", which denotes
1837 an array reference.
1839 This is logically equivalent in C to *(a+i), but we may do it differently.
1840 If A is a variable or a member, we generate a primitive ARRAY_REF.
1841 This avoids forcing the array out of registers, and can work on
1842 arrays that are not lvalues (for example, members of structures returned
1843 by functions). */
1845 tree
1846 build_array_ref (tree array, tree index)
1848 bool swapped = false;
1849 if (TREE_TYPE (array) == error_mark_node
1850 || TREE_TYPE (index) == error_mark_node)
1851 return error_mark_node;
1853 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1854 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1856 tree temp;
1857 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1858 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1860 error ("subscripted value is neither array nor pointer");
1861 return error_mark_node;
1863 temp = array;
1864 array = index;
1865 index = temp;
1866 swapped = true;
1869 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1871 error ("array subscript is not an integer");
1872 return error_mark_node;
1875 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1877 error ("subscripted value is pointer to function");
1878 return error_mark_node;
1881 /* ??? Existing practice has been to warn only when the char
1882 index is syntactically the index, not for char[array]. */
1883 if (!swapped)
1884 warn_array_subscript_with_type_char (index);
1886 /* Apply default promotions *after* noticing character types. */
1887 index = default_conversion (index);
1889 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1891 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1893 tree rval, type;
1895 /* An array that is indexed by a non-constant
1896 cannot be stored in a register; we must be able to do
1897 address arithmetic on its address.
1898 Likewise an array of elements of variable size. */
1899 if (TREE_CODE (index) != INTEGER_CST
1900 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1901 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1903 if (!c_mark_addressable (array))
1904 return error_mark_node;
1906 /* An array that is indexed by a constant value which is not within
1907 the array bounds cannot be stored in a register either; because we
1908 would get a crash in store_bit_field/extract_bit_field when trying
1909 to access a non-existent part of the register. */
1910 if (TREE_CODE (index) == INTEGER_CST
1911 && TYPE_DOMAIN (TREE_TYPE (array))
1912 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1914 if (!c_mark_addressable (array))
1915 return error_mark_node;
1918 if (pedantic)
1920 tree foo = array;
1921 while (TREE_CODE (foo) == COMPONENT_REF)
1922 foo = TREE_OPERAND (foo, 0);
1923 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1924 pedwarn ("ISO C forbids subscripting %<register%> array");
1925 else if (!flag_isoc99 && !lvalue_p (foo))
1926 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1929 type = TREE_TYPE (TREE_TYPE (array));
1930 if (TREE_CODE (type) != ARRAY_TYPE)
1931 type = TYPE_MAIN_VARIANT (type);
1932 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1933 /* Array ref is const/volatile if the array elements are
1934 or if the array is. */
1935 TREE_READONLY (rval)
1936 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1937 | TREE_READONLY (array));
1938 TREE_SIDE_EFFECTS (rval)
1939 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1940 | TREE_SIDE_EFFECTS (array));
1941 TREE_THIS_VOLATILE (rval)
1942 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1943 /* This was added by rms on 16 Nov 91.
1944 It fixes vol struct foo *a; a->elts[1]
1945 in an inline function.
1946 Hope it doesn't break something else. */
1947 | TREE_THIS_VOLATILE (array));
1948 return require_complete_type (fold (rval));
1950 else
1952 tree ar = default_conversion (array);
1954 if (ar == error_mark_node)
1955 return ar;
1957 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1958 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1960 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1961 "array indexing");
1965 /* Build an external reference to identifier ID. FUN indicates
1966 whether this will be used for a function call. LOC is the source
1967 location of the identifier. */
1968 tree
1969 build_external_ref (tree id, int fun, location_t loc)
1971 tree ref;
1972 tree decl = lookup_name (id);
1974 /* In Objective-C, an instance variable (ivar) may be preferred to
1975 whatever lookup_name() found. */
1976 decl = objc_lookup_ivar (decl, id);
1978 if (decl && decl != error_mark_node)
1979 ref = decl;
1980 else if (fun)
1981 /* Implicit function declaration. */
1982 ref = implicitly_declare (id);
1983 else if (decl == error_mark_node)
1984 /* Don't complain about something that's already been
1985 complained about. */
1986 return error_mark_node;
1987 else
1989 undeclared_variable (id, loc);
1990 return error_mark_node;
1993 if (TREE_TYPE (ref) == error_mark_node)
1994 return error_mark_node;
1996 if (TREE_DEPRECATED (ref))
1997 warn_deprecated_use (ref);
1999 if (!skip_evaluation)
2000 assemble_external (ref);
2001 TREE_USED (ref) = 1;
2003 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2005 if (!in_sizeof && !in_typeof)
2006 C_DECL_USED (ref) = 1;
2007 else if (DECL_INITIAL (ref) == 0
2008 && DECL_EXTERNAL (ref)
2009 && !TREE_PUBLIC (ref))
2010 record_maybe_used_decl (ref);
2013 if (TREE_CODE (ref) == CONST_DECL)
2015 ref = DECL_INITIAL (ref);
2016 TREE_CONSTANT (ref) = 1;
2017 TREE_INVARIANT (ref) = 1;
2019 else if (current_function_decl != 0
2020 && !DECL_FILE_SCOPE_P (current_function_decl)
2021 && (TREE_CODE (ref) == VAR_DECL
2022 || TREE_CODE (ref) == PARM_DECL
2023 || TREE_CODE (ref) == FUNCTION_DECL))
2025 tree context = decl_function_context (ref);
2027 if (context != 0 && context != current_function_decl)
2028 DECL_NONLOCAL (ref) = 1;
2031 return ref;
2034 /* Record details of decls possibly used inside sizeof or typeof. */
2035 struct maybe_used_decl
2037 /* The decl. */
2038 tree decl;
2039 /* The level seen at (in_sizeof + in_typeof). */
2040 int level;
2041 /* The next one at this level or above, or NULL. */
2042 struct maybe_used_decl *next;
2045 static struct maybe_used_decl *maybe_used_decls;
2047 /* Record that DECL, an undefined static function reference seen
2048 inside sizeof or typeof, might be used if the operand of sizeof is
2049 a VLA type or the operand of typeof is a variably modified
2050 type. */
2052 static void
2053 record_maybe_used_decl (tree decl)
2055 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2056 t->decl = decl;
2057 t->level = in_sizeof + in_typeof;
2058 t->next = maybe_used_decls;
2059 maybe_used_decls = t;
2062 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2063 USED is false, just discard them. If it is true, mark them used
2064 (if no longer inside sizeof or typeof) or move them to the next
2065 level up (if still inside sizeof or typeof). */
2067 void
2068 pop_maybe_used (bool used)
2070 struct maybe_used_decl *p = maybe_used_decls;
2071 int cur_level = in_sizeof + in_typeof;
2072 while (p && p->level > cur_level)
2074 if (used)
2076 if (cur_level == 0)
2077 C_DECL_USED (p->decl) = 1;
2078 else
2079 p->level = cur_level;
2081 p = p->next;
2083 if (!used || cur_level == 0)
2084 maybe_used_decls = p;
2087 /* Return the result of sizeof applied to EXPR. */
2089 struct c_expr
2090 c_expr_sizeof_expr (struct c_expr expr)
2092 struct c_expr ret;
2093 if (expr.value == error_mark_node)
2095 ret.value = error_mark_node;
2096 ret.original_code = ERROR_MARK;
2097 pop_maybe_used (false);
2099 else
2101 ret.value = c_sizeof (TREE_TYPE (expr.value));
2102 ret.original_code = ERROR_MARK;
2103 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2105 return ret;
2108 /* Return the result of sizeof applied to T, a structure for the type
2109 name passed to sizeof (rather than the type itself). */
2111 struct c_expr
2112 c_expr_sizeof_type (struct c_type_name *t)
2114 tree type;
2115 struct c_expr ret;
2116 type = groktypename (t);
2117 ret.value = c_sizeof (type);
2118 ret.original_code = ERROR_MARK;
2119 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
2120 return ret;
2123 /* Build a function call to function FUNCTION with parameters PARAMS.
2124 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2125 TREE_VALUE of each node is a parameter-expression.
2126 FUNCTION's data type may be a function type or a pointer-to-function. */
2128 tree
2129 build_function_call (tree function, tree params)
2131 tree fntype, fundecl = 0;
2132 tree coerced_params;
2133 tree name = NULL_TREE, result;
2134 tree tem;
2136 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2137 STRIP_TYPE_NOPS (function);
2139 /* Convert anything with function type to a pointer-to-function. */
2140 if (TREE_CODE (function) == FUNCTION_DECL)
2142 /* Implement type-directed function overloading for builtins.
2143 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2144 handle all the type checking. The result is a complete expression
2145 that implements this function call. */
2146 tem = resolve_overloaded_builtin (function, params);
2147 if (tem)
2148 return tem;
2150 name = DECL_NAME (function);
2151 fundecl = function;
2153 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2154 function = function_to_pointer_conversion (function);
2156 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2157 expressions, like those used for ObjC messenger dispatches. */
2158 function = objc_rewrite_function_call (function, params);
2160 fntype = TREE_TYPE (function);
2162 if (TREE_CODE (fntype) == ERROR_MARK)
2163 return error_mark_node;
2165 if (!(TREE_CODE (fntype) == POINTER_TYPE
2166 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2168 error ("called object %qE is not a function", function);
2169 return error_mark_node;
2172 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2173 current_function_returns_abnormally = 1;
2175 /* fntype now gets the type of function pointed to. */
2176 fntype = TREE_TYPE (fntype);
2178 /* Check that the function is called through a compatible prototype.
2179 If it is not, replace the call by a trap, wrapped up in a compound
2180 expression if necessary. This has the nice side-effect to prevent
2181 the tree-inliner from generating invalid assignment trees which may
2182 blow up in the RTL expander later. */
2183 if ((TREE_CODE (function) == NOP_EXPR
2184 || TREE_CODE (function) == CONVERT_EXPR)
2185 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2186 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2187 && !comptypes (fntype, TREE_TYPE (tem)))
2189 tree return_type = TREE_TYPE (fntype);
2190 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2191 NULL_TREE);
2193 /* This situation leads to run-time undefined behavior. We can't,
2194 therefore, simply error unless we can prove that all possible
2195 executions of the program must execute the code. */
2196 warning (0, "function called through a non-compatible type");
2198 /* We can, however, treat "undefined" any way we please.
2199 Call abort to encourage the user to fix the program. */
2200 inform ("if this code is reached, the program will abort");
2202 if (VOID_TYPE_P (return_type))
2203 return trap;
2204 else
2206 tree rhs;
2208 if (AGGREGATE_TYPE_P (return_type))
2209 rhs = build_compound_literal (return_type,
2210 build_constructor (return_type, 0));
2211 else
2212 rhs = fold_convert (return_type, integer_zero_node);
2214 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2218 /* Convert the parameters to the types declared in the
2219 function prototype, or apply default promotions. */
2221 coerced_params
2222 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2224 if (coerced_params == error_mark_node)
2225 return error_mark_node;
2227 /* Check that the arguments to the function are valid. */
2229 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2230 TYPE_ARG_TYPES (fntype));
2232 if (require_constant_value)
2234 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2235 function, coerced_params, NULL_TREE);
2237 if (TREE_CONSTANT (result)
2238 && (name == NULL_TREE
2239 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2240 pedwarn_init ("initializer element is not constant");
2242 else
2243 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2244 function, coerced_params, NULL_TREE);
2246 if (VOID_TYPE_P (TREE_TYPE (result)))
2247 return result;
2248 return require_complete_type (result);
2251 /* Convert the argument expressions in the list VALUES
2252 to the types in the list TYPELIST. The result is a list of converted
2253 argument expressions, unless there are too few arguments in which
2254 case it is error_mark_node.
2256 If TYPELIST is exhausted, or when an element has NULL as its type,
2257 perform the default conversions.
2259 PARMLIST is the chain of parm decls for the function being called.
2260 It may be 0, if that info is not available.
2261 It is used only for generating error messages.
2263 FUNCTION is a tree for the called function. It is used only for
2264 error messages, where it is formatted with %qE.
2266 This is also where warnings about wrong number of args are generated.
2268 Both VALUES and the returned value are chains of TREE_LIST nodes
2269 with the elements of the list in the TREE_VALUE slots of those nodes. */
2271 static tree
2272 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2274 tree typetail, valtail;
2275 tree result = NULL;
2276 int parmnum;
2277 tree selector;
2279 /* Change pointer to function to the function itself for
2280 diagnostics. */
2281 if (TREE_CODE (function) == ADDR_EXPR
2282 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2283 function = TREE_OPERAND (function, 0);
2285 /* Handle an ObjC selector specially for diagnostics. */
2286 selector = objc_message_selector ();
2288 /* Scan the given expressions and types, producing individual
2289 converted arguments and pushing them on RESULT in reverse order. */
2291 for (valtail = values, typetail = typelist, parmnum = 0;
2292 valtail;
2293 valtail = TREE_CHAIN (valtail), parmnum++)
2295 tree type = typetail ? TREE_VALUE (typetail) : 0;
2296 tree val = TREE_VALUE (valtail);
2297 tree rname = function;
2298 int argnum = parmnum + 1;
2299 const char *invalid_func_diag;
2301 if (type == void_type_node)
2303 error ("too many arguments to function %qE", function);
2304 break;
2307 if (selector && argnum > 2)
2309 rname = selector;
2310 argnum -= 2;
2313 STRIP_TYPE_NOPS (val);
2315 val = require_complete_type (val);
2317 if (type != 0)
2319 /* Formal parm type is specified by a function prototype. */
2320 tree parmval;
2322 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2324 error ("type of formal parameter %d is incomplete", parmnum + 1);
2325 parmval = val;
2327 else
2329 /* Optionally warn about conversions that
2330 differ from the default conversions. */
2331 if (warn_conversion || warn_traditional)
2333 unsigned int formal_prec = TYPE_PRECISION (type);
2335 if (INTEGRAL_TYPE_P (type)
2336 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2337 warning (0, "passing argument %d of %qE as integer "
2338 "rather than floating due to prototype",
2339 argnum, rname);
2340 if (INTEGRAL_TYPE_P (type)
2341 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2342 warning (0, "passing argument %d of %qE as integer "
2343 "rather than complex due to prototype",
2344 argnum, rname);
2345 else if (TREE_CODE (type) == COMPLEX_TYPE
2346 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2347 warning (0, "passing argument %d of %qE as complex "
2348 "rather than floating due to prototype",
2349 argnum, rname);
2350 else if (TREE_CODE (type) == REAL_TYPE
2351 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2352 warning (0, "passing argument %d of %qE as floating "
2353 "rather than integer due to prototype",
2354 argnum, rname);
2355 else if (TREE_CODE (type) == COMPLEX_TYPE
2356 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2357 warning (0, "passing argument %d of %qE as complex "
2358 "rather than integer due to prototype",
2359 argnum, rname);
2360 else if (TREE_CODE (type) == REAL_TYPE
2361 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2362 warning (0, "passing argument %d of %qE as floating "
2363 "rather than complex due to prototype",
2364 argnum, rname);
2365 /* ??? At some point, messages should be written about
2366 conversions between complex types, but that's too messy
2367 to do now. */
2368 else if (TREE_CODE (type) == REAL_TYPE
2369 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2371 /* Warn if any argument is passed as `float',
2372 since without a prototype it would be `double'. */
2373 if (formal_prec == TYPE_PRECISION (float_type_node))
2374 warning (0, "passing argument %d of %qE as %<float%> "
2375 "rather than %<double%> due to prototype",
2376 argnum, rname);
2378 /* Detect integer changing in width or signedness.
2379 These warnings are only activated with
2380 -Wconversion, not with -Wtraditional. */
2381 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2382 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2384 tree would_have_been = default_conversion (val);
2385 tree type1 = TREE_TYPE (would_have_been);
2387 if (TREE_CODE (type) == ENUMERAL_TYPE
2388 && (TYPE_MAIN_VARIANT (type)
2389 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2390 /* No warning if function asks for enum
2391 and the actual arg is that enum type. */
2393 else if (formal_prec != TYPE_PRECISION (type1))
2394 warning (OPT_Wconversion, "passing argument %d of %qE "
2395 "with different width due to prototype",
2396 argnum, rname);
2397 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2399 /* Don't complain if the formal parameter type
2400 is an enum, because we can't tell now whether
2401 the value was an enum--even the same enum. */
2402 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2404 else if (TREE_CODE (val) == INTEGER_CST
2405 && int_fits_type_p (val, type))
2406 /* Change in signedness doesn't matter
2407 if a constant value is unaffected. */
2409 /* If the value is extended from a narrower
2410 unsigned type, it doesn't matter whether we
2411 pass it as signed or unsigned; the value
2412 certainly is the same either way. */
2413 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2414 && TYPE_UNSIGNED (TREE_TYPE (val)))
2416 else if (TYPE_UNSIGNED (type))
2417 warning (OPT_Wconversion, "passing argument %d of %qE "
2418 "as unsigned due to prototype",
2419 argnum, rname);
2420 else
2421 warning (OPT_Wconversion, "passing argument %d of %qE "
2422 "as signed due to prototype", argnum, rname);
2426 parmval = convert_for_assignment (type, val, ic_argpass,
2427 fundecl, function,
2428 parmnum + 1);
2430 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2431 && INTEGRAL_TYPE_P (type)
2432 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2433 parmval = default_conversion (parmval);
2435 result = tree_cons (NULL_TREE, parmval, result);
2437 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2438 && (TYPE_PRECISION (TREE_TYPE (val))
2439 < TYPE_PRECISION (double_type_node)))
2440 /* Convert `float' to `double'. */
2441 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2442 else if ((invalid_func_diag =
2443 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2445 error (invalid_func_diag);
2446 return error_mark_node;
2448 else
2449 /* Convert `short' and `char' to full-size `int'. */
2450 result = tree_cons (NULL_TREE, default_conversion (val), result);
2452 if (typetail)
2453 typetail = TREE_CHAIN (typetail);
2456 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2458 error ("too few arguments to function %qE", function);
2459 return error_mark_node;
2462 return nreverse (result);
2465 /* This is the entry point used by the parser to build unary operators
2466 in the input. CODE, a tree_code, specifies the unary operator, and
2467 ARG is the operand. For unary plus, the C parser currently uses
2468 CONVERT_EXPR for code. */
2470 struct c_expr
2471 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2473 struct c_expr result;
2475 result.original_code = ERROR_MARK;
2476 result.value = build_unary_op (code, arg.value, 0);
2477 overflow_warning (result.value);
2478 return result;
2481 /* This is the entry point used by the parser to build binary operators
2482 in the input. CODE, a tree_code, specifies the binary operator, and
2483 ARG1 and ARG2 are the operands. In addition to constructing the
2484 expression, we check for operands that were written with other binary
2485 operators in a way that is likely to confuse the user. */
2487 struct c_expr
2488 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2489 struct c_expr arg2)
2491 struct c_expr result;
2493 enum tree_code code1 = arg1.original_code;
2494 enum tree_code code2 = arg2.original_code;
2496 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2497 result.original_code = code;
2499 if (TREE_CODE (result.value) == ERROR_MARK)
2500 return result;
2502 /* Check for cases such as x+y<<z which users are likely
2503 to misinterpret. */
2504 if (warn_parentheses)
2506 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2508 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2509 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2510 warning (OPT_Wparentheses,
2511 "suggest parentheses around + or - inside shift");
2514 if (code == TRUTH_ORIF_EXPR)
2516 if (code1 == TRUTH_ANDIF_EXPR
2517 || code2 == TRUTH_ANDIF_EXPR)
2518 warning (OPT_Wparentheses,
2519 "suggest parentheses around && within ||");
2522 if (code == BIT_IOR_EXPR)
2524 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2525 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2526 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2527 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2528 warning (OPT_Wparentheses,
2529 "suggest parentheses around arithmetic in operand of |");
2530 /* Check cases like x|y==z */
2531 if (TREE_CODE_CLASS (code1) == tcc_comparison
2532 || TREE_CODE_CLASS (code2) == tcc_comparison)
2533 warning (OPT_Wparentheses,
2534 "suggest parentheses around comparison in operand of |");
2537 if (code == BIT_XOR_EXPR)
2539 if (code1 == BIT_AND_EXPR
2540 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2541 || code2 == BIT_AND_EXPR
2542 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2543 warning (OPT_Wparentheses,
2544 "suggest parentheses around arithmetic in operand of ^");
2545 /* Check cases like x^y==z */
2546 if (TREE_CODE_CLASS (code1) == tcc_comparison
2547 || TREE_CODE_CLASS (code2) == tcc_comparison)
2548 warning (OPT_Wparentheses,
2549 "suggest parentheses around comparison in operand of ^");
2552 if (code == BIT_AND_EXPR)
2554 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2555 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2556 warning (OPT_Wparentheses,
2557 "suggest parentheses around + or - in operand of &");
2558 /* Check cases like x&y==z */
2559 if (TREE_CODE_CLASS (code1) == tcc_comparison
2560 || TREE_CODE_CLASS (code2) == tcc_comparison)
2561 warning (OPT_Wparentheses,
2562 "suggest parentheses around comparison in operand of &");
2564 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2565 if (TREE_CODE_CLASS (code) == tcc_comparison
2566 && (TREE_CODE_CLASS (code1) == tcc_comparison
2567 || TREE_CODE_CLASS (code2) == tcc_comparison))
2568 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2569 "have their mathematical meaning");
2573 /* Warn about comparisons against string literals, with the exception
2574 of testing for equality or inequality of a string literal with NULL. */
2575 if (code == EQ_EXPR || code == NE_EXPR)
2577 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2578 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2579 warning (OPT_Wstring_literal_comparison,
2580 "comparison with string literal");
2582 else if (TREE_CODE_CLASS (code) == tcc_comparison
2583 && (code1 == STRING_CST || code2 == STRING_CST))
2584 warning (OPT_Wstring_literal_comparison,
2585 "comparison with string literal");
2587 unsigned_conversion_warning (result.value, arg1.value);
2588 unsigned_conversion_warning (result.value, arg2.value);
2589 overflow_warning (result.value);
2591 return result;
2594 /* Return a tree for the difference of pointers OP0 and OP1.
2595 The resulting tree has type int. */
2597 static tree
2598 pointer_diff (tree op0, tree op1)
2600 tree restype = ptrdiff_type_node;
2602 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2603 tree con0, con1, lit0, lit1;
2604 tree orig_op1 = op1;
2606 if (pedantic || warn_pointer_arith)
2608 if (TREE_CODE (target_type) == VOID_TYPE)
2609 pedwarn ("pointer of type %<void *%> used in subtraction");
2610 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2611 pedwarn ("pointer to a function used in subtraction");
2614 /* If the conversion to ptrdiff_type does anything like widening or
2615 converting a partial to an integral mode, we get a convert_expression
2616 that is in the way to do any simplifications.
2617 (fold-const.c doesn't know that the extra bits won't be needed.
2618 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2619 different mode in place.)
2620 So first try to find a common term here 'by hand'; we want to cover
2621 at least the cases that occur in legal static initializers. */
2622 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2623 && (TYPE_PRECISION (TREE_TYPE (op0))
2624 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2625 con0 = TREE_OPERAND (op0, 0);
2626 else
2627 con0 = op0;
2628 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2629 && (TYPE_PRECISION (TREE_TYPE (op1))
2630 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2631 con1 = TREE_OPERAND (op1, 0);
2632 else
2633 con1 = op1;
2635 if (TREE_CODE (con0) == PLUS_EXPR)
2637 lit0 = TREE_OPERAND (con0, 1);
2638 con0 = TREE_OPERAND (con0, 0);
2640 else
2641 lit0 = integer_zero_node;
2643 if (TREE_CODE (con1) == PLUS_EXPR)
2645 lit1 = TREE_OPERAND (con1, 1);
2646 con1 = TREE_OPERAND (con1, 0);
2648 else
2649 lit1 = integer_zero_node;
2651 if (operand_equal_p (con0, con1, 0))
2653 op0 = lit0;
2654 op1 = lit1;
2658 /* First do the subtraction as integers;
2659 then drop through to build the divide operator.
2660 Do not do default conversions on the minus operator
2661 in case restype is a short type. */
2663 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2664 convert (restype, op1), 0);
2665 /* This generates an error if op1 is pointer to incomplete type. */
2666 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2667 error ("arithmetic on pointer to an incomplete type");
2669 /* This generates an error if op0 is pointer to incomplete type. */
2670 op1 = c_size_in_bytes (target_type);
2672 /* Divide by the size, in easiest possible way. */
2673 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2676 /* Construct and perhaps optimize a tree representation
2677 for a unary operation. CODE, a tree_code, specifies the operation
2678 and XARG is the operand.
2679 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2680 the default promotions (such as from short to int).
2681 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2682 allows non-lvalues; this is only used to handle conversion of non-lvalue
2683 arrays to pointers in C99. */
2685 tree
2686 build_unary_op (enum tree_code code, tree xarg, int flag)
2688 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2689 tree arg = xarg;
2690 tree argtype = 0;
2691 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2692 tree val;
2693 int noconvert = flag;
2694 const char *invalid_op_diag;
2696 if (typecode == ERROR_MARK)
2697 return error_mark_node;
2698 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2699 typecode = INTEGER_TYPE;
2701 if ((invalid_op_diag
2702 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2704 error (invalid_op_diag);
2705 return error_mark_node;
2708 switch (code)
2710 case CONVERT_EXPR:
2711 /* This is used for unary plus, because a CONVERT_EXPR
2712 is enough to prevent anybody from looking inside for
2713 associativity, but won't generate any code. */
2714 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2715 || typecode == COMPLEX_TYPE
2716 || typecode == VECTOR_TYPE))
2718 error ("wrong type argument to unary plus");
2719 return error_mark_node;
2721 else if (!noconvert)
2722 arg = default_conversion (arg);
2723 arg = non_lvalue (arg);
2724 break;
2726 case NEGATE_EXPR:
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE
2729 || typecode == VECTOR_TYPE))
2731 error ("wrong type argument to unary minus");
2732 return error_mark_node;
2734 else if (!noconvert)
2735 arg = default_conversion (arg);
2736 break;
2738 case BIT_NOT_EXPR:
2739 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2741 if (!noconvert)
2742 arg = default_conversion (arg);
2744 else if (typecode == COMPLEX_TYPE)
2746 code = CONJ_EXPR;
2747 if (pedantic)
2748 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2749 if (!noconvert)
2750 arg = default_conversion (arg);
2752 else
2754 error ("wrong type argument to bit-complement");
2755 return error_mark_node;
2757 break;
2759 case ABS_EXPR:
2760 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2762 error ("wrong type argument to abs");
2763 return error_mark_node;
2765 else if (!noconvert)
2766 arg = default_conversion (arg);
2767 break;
2769 case CONJ_EXPR:
2770 /* Conjugating a real value is a no-op, but allow it anyway. */
2771 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2772 || typecode == COMPLEX_TYPE))
2774 error ("wrong type argument to conjugation");
2775 return error_mark_node;
2777 else if (!noconvert)
2778 arg = default_conversion (arg);
2779 break;
2781 case TRUTH_NOT_EXPR:
2782 if (typecode != INTEGER_TYPE
2783 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2784 && typecode != COMPLEX_TYPE)
2786 error ("wrong type argument to unary exclamation mark");
2787 return error_mark_node;
2789 arg = c_objc_common_truthvalue_conversion (arg);
2790 return invert_truthvalue (arg);
2792 case REALPART_EXPR:
2793 if (TREE_CODE (arg) == COMPLEX_CST)
2794 return TREE_REALPART (arg);
2795 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2796 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2797 else
2798 return arg;
2800 case IMAGPART_EXPR:
2801 if (TREE_CODE (arg) == COMPLEX_CST)
2802 return TREE_IMAGPART (arg);
2803 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2804 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2805 else
2806 return convert (TREE_TYPE (arg), integer_zero_node);
2808 case PREINCREMENT_EXPR:
2809 case POSTINCREMENT_EXPR:
2810 case PREDECREMENT_EXPR:
2811 case POSTDECREMENT_EXPR:
2813 /* Increment or decrement the real part of the value,
2814 and don't change the imaginary part. */
2815 if (typecode == COMPLEX_TYPE)
2817 tree real, imag;
2819 if (pedantic)
2820 pedwarn ("ISO C does not support %<++%> and %<--%>"
2821 " on complex types");
2823 arg = stabilize_reference (arg);
2824 real = build_unary_op (REALPART_EXPR, arg, 1);
2825 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2826 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2827 build_unary_op (code, real, 1), imag);
2830 /* Report invalid types. */
2832 if (typecode != POINTER_TYPE
2833 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2835 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2836 error ("wrong type argument to increment");
2837 else
2838 error ("wrong type argument to decrement");
2840 return error_mark_node;
2844 tree inc;
2845 tree result_type = TREE_TYPE (arg);
2847 arg = get_unwidened (arg, 0);
2848 argtype = TREE_TYPE (arg);
2850 /* Compute the increment. */
2852 if (typecode == POINTER_TYPE)
2854 /* If pointer target is an undefined struct,
2855 we just cannot know how to do the arithmetic. */
2856 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2858 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2859 error ("increment of pointer to unknown structure");
2860 else
2861 error ("decrement of pointer to unknown structure");
2863 else if ((pedantic || warn_pointer_arith)
2864 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2865 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2867 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2868 pedwarn ("wrong type argument to increment");
2869 else
2870 pedwarn ("wrong type argument to decrement");
2873 inc = c_size_in_bytes (TREE_TYPE (result_type));
2875 else
2876 inc = integer_one_node;
2878 inc = convert (argtype, inc);
2880 /* Complain about anything else that is not a true lvalue. */
2881 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2882 || code == POSTINCREMENT_EXPR)
2883 ? lv_increment
2884 : lv_decrement)))
2885 return error_mark_node;
2887 /* Report a read-only lvalue. */
2888 if (TREE_READONLY (arg))
2889 readonly_error (arg,
2890 ((code == PREINCREMENT_EXPR
2891 || code == POSTINCREMENT_EXPR)
2892 ? lv_increment : lv_decrement));
2894 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2895 val = boolean_increment (code, arg);
2896 else
2897 val = build2 (code, TREE_TYPE (arg), arg, inc);
2898 TREE_SIDE_EFFECTS (val) = 1;
2899 val = convert (result_type, val);
2900 if (TREE_CODE (val) != code)
2901 TREE_NO_WARNING (val) = 1;
2902 return val;
2905 case ADDR_EXPR:
2906 /* Note that this operation never does default_conversion. */
2908 /* Let &* cancel out to simplify resulting code. */
2909 if (TREE_CODE (arg) == INDIRECT_REF)
2911 /* Don't let this be an lvalue. */
2912 if (lvalue_p (TREE_OPERAND (arg, 0)))
2913 return non_lvalue (TREE_OPERAND (arg, 0));
2914 return TREE_OPERAND (arg, 0);
2917 /* For &x[y], return x+y */
2918 if (TREE_CODE (arg) == ARRAY_REF)
2920 tree op0 = TREE_OPERAND (arg, 0);
2921 if (!c_mark_addressable (op0))
2922 return error_mark_node;
2923 return build_binary_op (PLUS_EXPR,
2924 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2925 ? array_to_pointer_conversion (op0)
2926 : op0),
2927 TREE_OPERAND (arg, 1), 1);
2930 /* Anything not already handled and not a true memory reference
2931 or a non-lvalue array is an error. */
2932 else if (typecode != FUNCTION_TYPE && !flag
2933 && !lvalue_or_else (arg, lv_addressof))
2934 return error_mark_node;
2936 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2937 argtype = TREE_TYPE (arg);
2939 /* If the lvalue is const or volatile, merge that into the type
2940 to which the address will point. Note that you can't get a
2941 restricted pointer by taking the address of something, so we
2942 only have to deal with `const' and `volatile' here. */
2943 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2944 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2945 argtype = c_build_type_variant (argtype,
2946 TREE_READONLY (arg),
2947 TREE_THIS_VOLATILE (arg));
2949 if (!c_mark_addressable (arg))
2950 return error_mark_node;
2952 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2953 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2955 argtype = build_pointer_type (argtype);
2957 /* ??? Cope with user tricks that amount to offsetof. Delete this
2958 when we have proper support for integer constant expressions. */
2959 val = get_base_address (arg);
2960 if (val && TREE_CODE (val) == INDIRECT_REF
2961 && integer_zerop (TREE_OPERAND (val, 0)))
2962 return fold_convert (argtype, fold_offsetof (arg));
2964 val = build1 (ADDR_EXPR, argtype, arg);
2966 return val;
2968 default:
2969 gcc_unreachable ();
2972 if (argtype == 0)
2973 argtype = TREE_TYPE (arg);
2974 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
2975 : fold_build1 (code, argtype, arg);
2978 /* Return nonzero if REF is an lvalue valid for this language.
2979 Lvalues can be assigned, unless their type has TYPE_READONLY.
2980 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2982 static int
2983 lvalue_p (tree ref)
2985 enum tree_code code = TREE_CODE (ref);
2987 switch (code)
2989 case REALPART_EXPR:
2990 case IMAGPART_EXPR:
2991 case COMPONENT_REF:
2992 return lvalue_p (TREE_OPERAND (ref, 0));
2994 case COMPOUND_LITERAL_EXPR:
2995 case STRING_CST:
2996 return 1;
2998 case INDIRECT_REF:
2999 case ARRAY_REF:
3000 case VAR_DECL:
3001 case PARM_DECL:
3002 case RESULT_DECL:
3003 case ERROR_MARK:
3004 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3005 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3007 case BIND_EXPR:
3008 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3010 default:
3011 return 0;
3015 /* Give an error for storing in something that is 'const'. */
3017 static void
3018 readonly_error (tree arg, enum lvalue_use use)
3020 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3021 || use == lv_asm);
3022 /* Using this macro rather than (for example) arrays of messages
3023 ensures that all the format strings are checked at compile
3024 time. */
3025 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3026 : (use == lv_increment ? (I) \
3027 : (use == lv_decrement ? (D) : (AS))))
3028 if (TREE_CODE (arg) == COMPONENT_REF)
3030 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3031 readonly_error (TREE_OPERAND (arg, 0), use);
3032 else
3033 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3034 G_("increment of read-only member %qD"),
3035 G_("decrement of read-only member %qD"),
3036 G_("read-only member %qD used as %<asm%> output")),
3037 TREE_OPERAND (arg, 1));
3039 else if (TREE_CODE (arg) == VAR_DECL)
3040 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3041 G_("increment of read-only variable %qD"),
3042 G_("decrement of read-only variable %qD"),
3043 G_("read-only variable %qD used as %<asm%> output")),
3044 arg);
3045 else
3046 error (READONLY_MSG (G_("assignment of read-only location"),
3047 G_("increment of read-only location"),
3048 G_("decrement of read-only location"),
3049 G_("read-only location used as %<asm%> output")));
3053 /* Return nonzero if REF is an lvalue valid for this language;
3054 otherwise, print an error message and return zero. USE says
3055 how the lvalue is being used and so selects the error message. */
3057 static int
3058 lvalue_or_else (tree ref, enum lvalue_use use)
3060 int win = lvalue_p (ref);
3062 if (!win)
3063 lvalue_error (use);
3065 return win;
3068 /* Mark EXP saying that we need to be able to take the
3069 address of it; it should not be allocated in a register.
3070 Returns true if successful. */
3072 bool
3073 c_mark_addressable (tree exp)
3075 tree x = exp;
3077 while (1)
3078 switch (TREE_CODE (x))
3080 case COMPONENT_REF:
3081 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3083 error
3084 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3085 return false;
3088 /* ... fall through ... */
3090 case ADDR_EXPR:
3091 case ARRAY_REF:
3092 case REALPART_EXPR:
3093 case IMAGPART_EXPR:
3094 x = TREE_OPERAND (x, 0);
3095 break;
3097 case COMPOUND_LITERAL_EXPR:
3098 case CONSTRUCTOR:
3099 TREE_ADDRESSABLE (x) = 1;
3100 return true;
3102 case VAR_DECL:
3103 case CONST_DECL:
3104 case PARM_DECL:
3105 case RESULT_DECL:
3106 if (C_DECL_REGISTER (x)
3107 && DECL_NONLOCAL (x))
3109 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3111 error
3112 ("global register variable %qD used in nested function", x);
3113 return false;
3115 pedwarn ("register variable %qD used in nested function", x);
3117 else if (C_DECL_REGISTER (x))
3119 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3120 error ("address of global register variable %qD requested", x);
3121 else
3122 error ("address of register variable %qD requested", x);
3123 return false;
3126 /* drops in */
3127 case FUNCTION_DECL:
3128 TREE_ADDRESSABLE (x) = 1;
3129 /* drops out */
3130 default:
3131 return true;
3135 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3137 tree
3138 build_conditional_expr (tree ifexp, tree op1, tree op2)
3140 tree type1;
3141 tree type2;
3142 enum tree_code code1;
3143 enum tree_code code2;
3144 tree result_type = NULL;
3145 tree orig_op1 = op1, orig_op2 = op2;
3147 /* Promote both alternatives. */
3149 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3150 op1 = default_conversion (op1);
3151 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3152 op2 = default_conversion (op2);
3154 if (TREE_CODE (ifexp) == ERROR_MARK
3155 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3156 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3157 return error_mark_node;
3159 type1 = TREE_TYPE (op1);
3160 code1 = TREE_CODE (type1);
3161 type2 = TREE_TYPE (op2);
3162 code2 = TREE_CODE (type2);
3164 /* C90 does not permit non-lvalue arrays in conditional expressions.
3165 In C99 they will be pointers by now. */
3166 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3168 error ("non-lvalue array in conditional expression");
3169 return error_mark_node;
3172 /* Quickly detect the usual case where op1 and op2 have the same type
3173 after promotion. */
3174 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3176 if (type1 == type2)
3177 result_type = type1;
3178 else
3179 result_type = TYPE_MAIN_VARIANT (type1);
3181 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3182 || code1 == COMPLEX_TYPE)
3183 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3184 || code2 == COMPLEX_TYPE))
3186 result_type = c_common_type (type1, type2);
3188 /* If -Wsign-compare, warn here if type1 and type2 have
3189 different signedness. We'll promote the signed to unsigned
3190 and later code won't know it used to be different.
3191 Do this check on the original types, so that explicit casts
3192 will be considered, but default promotions won't. */
3193 if (warn_sign_compare && !skip_evaluation)
3195 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3196 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3198 if (unsigned_op1 ^ unsigned_op2)
3200 /* Do not warn if the result type is signed, since the
3201 signed type will only be chosen if it can represent
3202 all the values of the unsigned type. */
3203 if (!TYPE_UNSIGNED (result_type))
3204 /* OK */;
3205 /* Do not warn if the signed quantity is an unsuffixed
3206 integer literal (or some static constant expression
3207 involving such literals) and it is non-negative. */
3208 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3209 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3210 /* OK */;
3211 else
3212 warning (0, "signed and unsigned type in conditional expression");
3216 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3218 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3219 pedwarn ("ISO C forbids conditional expr with only one void side");
3220 result_type = void_type_node;
3222 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3224 if (comp_target_types (type1, type2))
3225 result_type = common_pointer_type (type1, type2);
3226 else if (null_pointer_constant_p (orig_op1))
3227 result_type = qualify_type (type2, type1);
3228 else if (null_pointer_constant_p (orig_op2))
3229 result_type = qualify_type (type1, type2);
3230 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3232 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3233 pedwarn ("ISO C forbids conditional expr between "
3234 "%<void *%> and function pointer");
3235 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3236 TREE_TYPE (type2)));
3238 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3240 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3241 pedwarn ("ISO C forbids conditional expr between "
3242 "%<void *%> and function pointer");
3243 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3244 TREE_TYPE (type1)));
3246 else
3248 pedwarn ("pointer type mismatch in conditional expression");
3249 result_type = build_pointer_type (void_type_node);
3252 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3254 if (!null_pointer_constant_p (orig_op2))
3255 pedwarn ("pointer/integer type mismatch in conditional expression");
3256 else
3258 op2 = null_pointer_node;
3260 result_type = type1;
3262 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3264 if (!null_pointer_constant_p (orig_op1))
3265 pedwarn ("pointer/integer type mismatch in conditional expression");
3266 else
3268 op1 = null_pointer_node;
3270 result_type = type2;
3273 if (!result_type)
3275 if (flag_cond_mismatch)
3276 result_type = void_type_node;
3277 else
3279 error ("type mismatch in conditional expression");
3280 return error_mark_node;
3284 /* Merge const and volatile flags of the incoming types. */
3285 result_type
3286 = build_type_variant (result_type,
3287 TREE_READONLY (op1) || TREE_READONLY (op2),
3288 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3290 if (result_type != TREE_TYPE (op1))
3291 op1 = convert_and_check (result_type, op1);
3292 if (result_type != TREE_TYPE (op2))
3293 op2 = convert_and_check (result_type, op2);
3295 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3298 /* Return a compound expression that performs two expressions and
3299 returns the value of the second of them. */
3301 tree
3302 build_compound_expr (tree expr1, tree expr2)
3304 if (!TREE_SIDE_EFFECTS (expr1))
3306 /* The left-hand operand of a comma expression is like an expression
3307 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3308 any side-effects, unless it was explicitly cast to (void). */
3309 if (warn_unused_value)
3311 if (VOID_TYPE_P (TREE_TYPE (expr1))
3312 && (TREE_CODE (expr1) == NOP_EXPR
3313 || TREE_CODE (expr1) == CONVERT_EXPR))
3314 ; /* (void) a, b */
3315 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3316 && TREE_CODE (expr1) == COMPOUND_EXPR
3317 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3318 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3319 ; /* (void) a, (void) b, c */
3320 else
3321 warning (0, "left-hand operand of comma expression has no effect");
3325 /* With -Wunused, we should also warn if the left-hand operand does have
3326 side-effects, but computes a value which is not used. For example, in
3327 `foo() + bar(), baz()' the result of the `+' operator is not used,
3328 so we should issue a warning. */
3329 else if (warn_unused_value)
3330 warn_if_unused_value (expr1, input_location);
3332 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3335 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3337 tree
3338 build_c_cast (tree type, tree expr)
3340 tree value = expr;
3342 if (type == error_mark_node || expr == error_mark_node)
3343 return error_mark_node;
3345 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3346 only in <protocol> qualifications. But when constructing cast expressions,
3347 the protocols do matter and must be kept around. */
3348 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3349 return build1 (NOP_EXPR, type, expr);
3351 type = TYPE_MAIN_VARIANT (type);
3353 if (TREE_CODE (type) == ARRAY_TYPE)
3355 error ("cast specifies array type");
3356 return error_mark_node;
3359 if (TREE_CODE (type) == FUNCTION_TYPE)
3361 error ("cast specifies function type");
3362 return error_mark_node;
3365 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3367 if (pedantic)
3369 if (TREE_CODE (type) == RECORD_TYPE
3370 || TREE_CODE (type) == UNION_TYPE)
3371 pedwarn ("ISO C forbids casting nonscalar to the same type");
3374 else if (TREE_CODE (type) == UNION_TYPE)
3376 tree field;
3378 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3379 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3380 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3381 break;
3383 if (field)
3385 tree t;
3387 if (pedantic)
3388 pedwarn ("ISO C forbids casts to union type");
3389 t = digest_init (type,
3390 build_constructor_single (type, field, value),
3391 true, 0);
3392 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3393 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3394 return t;
3396 error ("cast to union type from type not present in union");
3397 return error_mark_node;
3399 else
3401 tree otype, ovalue;
3403 if (type == void_type_node)
3404 return build1 (CONVERT_EXPR, type, value);
3406 otype = TREE_TYPE (value);
3408 /* Optionally warn about potentially worrisome casts. */
3410 if (warn_cast_qual
3411 && TREE_CODE (type) == POINTER_TYPE
3412 && TREE_CODE (otype) == POINTER_TYPE)
3414 tree in_type = type;
3415 tree in_otype = otype;
3416 int added = 0;
3417 int discarded = 0;
3419 /* Check that the qualifiers on IN_TYPE are a superset of
3420 the qualifiers of IN_OTYPE. The outermost level of
3421 POINTER_TYPE nodes is uninteresting and we stop as soon
3422 as we hit a non-POINTER_TYPE node on either type. */
3425 in_otype = TREE_TYPE (in_otype);
3426 in_type = TREE_TYPE (in_type);
3428 /* GNU C allows cv-qualified function types. 'const'
3429 means the function is very pure, 'volatile' means it
3430 can't return. We need to warn when such qualifiers
3431 are added, not when they're taken away. */
3432 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3433 && TREE_CODE (in_type) == FUNCTION_TYPE)
3434 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3435 else
3436 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3438 while (TREE_CODE (in_type) == POINTER_TYPE
3439 && TREE_CODE (in_otype) == POINTER_TYPE);
3441 if (added)
3442 warning (0, "cast adds new qualifiers to function type");
3444 if (discarded)
3445 /* There are qualifiers present in IN_OTYPE that are not
3446 present in IN_TYPE. */
3447 warning (0, "cast discards qualifiers from pointer target type");
3450 /* Warn about possible alignment problems. */
3451 if (STRICT_ALIGNMENT
3452 && TREE_CODE (type) == POINTER_TYPE
3453 && TREE_CODE (otype) == POINTER_TYPE
3454 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3455 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3456 /* Don't warn about opaque types, where the actual alignment
3457 restriction is unknown. */
3458 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3459 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3460 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3461 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3462 warning (OPT_Wcast_align,
3463 "cast increases required alignment of target type");
3465 if (TREE_CODE (type) == INTEGER_TYPE
3466 && TREE_CODE (otype) == POINTER_TYPE
3467 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3468 && !TREE_CONSTANT (value))
3469 warning (OPT_Wpointer_to_int_cast,
3470 "cast from pointer to integer of different size");
3472 if (TREE_CODE (value) == CALL_EXPR
3473 && TREE_CODE (type) != TREE_CODE (otype))
3474 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3475 "to non-matching type %qT", otype, type);
3477 if (TREE_CODE (type) == POINTER_TYPE
3478 && TREE_CODE (otype) == INTEGER_TYPE
3479 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3480 /* Don't warn about converting any constant. */
3481 && !TREE_CONSTANT (value))
3482 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3483 "of different size");
3485 strict_aliasing_warning (otype, type, expr);
3487 /* If pedantic, warn for conversions between function and object
3488 pointer types, except for converting a null pointer constant
3489 to function pointer type. */
3490 if (pedantic
3491 && TREE_CODE (type) == POINTER_TYPE
3492 && TREE_CODE (otype) == POINTER_TYPE
3493 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3494 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3495 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3497 if (pedantic
3498 && TREE_CODE (type) == POINTER_TYPE
3499 && TREE_CODE (otype) == POINTER_TYPE
3500 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3501 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3502 && !null_pointer_constant_p (value))
3503 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3505 ovalue = value;
3506 value = convert (type, value);
3508 /* Ignore any integer overflow caused by the cast. */
3509 if (TREE_CODE (value) == INTEGER_CST)
3511 if (CONSTANT_CLASS_P (ovalue)
3512 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3514 /* Avoid clobbering a shared constant. */
3515 value = copy_node (value);
3516 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3517 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3519 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3520 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3521 value = build_int_cst_wide (TREE_TYPE (value),
3522 TREE_INT_CST_LOW (value),
3523 TREE_INT_CST_HIGH (value));
3527 /* Don't let a cast be an lvalue. */
3528 if (value == expr)
3529 value = non_lvalue (value);
3531 return value;
3534 /* Interpret a cast of expression EXPR to type TYPE. */
3535 tree
3536 c_cast_expr (struct c_type_name *type_name, tree expr)
3538 tree type;
3539 int saved_wsp = warn_strict_prototypes;
3541 /* This avoids warnings about unprototyped casts on
3542 integers. E.g. "#define SIG_DFL (void(*)())0". */
3543 if (TREE_CODE (expr) == INTEGER_CST)
3544 warn_strict_prototypes = 0;
3545 type = groktypename (type_name);
3546 warn_strict_prototypes = saved_wsp;
3548 return build_c_cast (type, expr);
3552 /* Build an assignment expression of lvalue LHS from value RHS.
3553 MODIFYCODE is the code for a binary operator that we use
3554 to combine the old value of LHS with RHS to get the new value.
3555 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3557 tree
3558 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3560 tree result;
3561 tree newrhs;
3562 tree lhstype = TREE_TYPE (lhs);
3563 tree olhstype = lhstype;
3565 /* Types that aren't fully specified cannot be used in assignments. */
3566 lhs = require_complete_type (lhs);
3568 /* Avoid duplicate error messages from operands that had errors. */
3569 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3570 return error_mark_node;
3572 STRIP_TYPE_NOPS (rhs);
3574 newrhs = rhs;
3576 /* If a binary op has been requested, combine the old LHS value with the RHS
3577 producing the value we should actually store into the LHS. */
3579 if (modifycode != NOP_EXPR)
3581 lhs = stabilize_reference (lhs);
3582 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3585 if (!lvalue_or_else (lhs, lv_assign))
3586 return error_mark_node;
3588 /* Give an error for storing in something that is 'const'. */
3590 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3591 || ((TREE_CODE (lhstype) == RECORD_TYPE
3592 || TREE_CODE (lhstype) == UNION_TYPE)
3593 && C_TYPE_FIELDS_READONLY (lhstype)))
3594 readonly_error (lhs, lv_assign);
3596 /* If storing into a structure or union member,
3597 it has probably been given type `int'.
3598 Compute the type that would go with
3599 the actual amount of storage the member occupies. */
3601 if (TREE_CODE (lhs) == COMPONENT_REF
3602 && (TREE_CODE (lhstype) == INTEGER_TYPE
3603 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3604 || TREE_CODE (lhstype) == REAL_TYPE
3605 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3606 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3608 /* If storing in a field that is in actuality a short or narrower than one,
3609 we must store in the field in its actual type. */
3611 if (lhstype != TREE_TYPE (lhs))
3613 lhs = copy_node (lhs);
3614 TREE_TYPE (lhs) = lhstype;
3617 /* Convert new value to destination type. */
3619 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3620 NULL_TREE, NULL_TREE, 0);
3621 if (TREE_CODE (newrhs) == ERROR_MARK)
3622 return error_mark_node;
3624 /* Emit ObjC write barrier, if necessary. */
3625 if (c_dialect_objc () && flag_objc_gc)
3627 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3628 if (result)
3629 return result;
3632 /* Scan operands. */
3634 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3635 TREE_SIDE_EFFECTS (result) = 1;
3637 /* If we got the LHS in a different type for storing in,
3638 convert the result back to the nominal type of LHS
3639 so that the value we return always has the same type
3640 as the LHS argument. */
3642 if (olhstype == TREE_TYPE (result))
3643 return result;
3644 return convert_for_assignment (olhstype, result, ic_assign,
3645 NULL_TREE, NULL_TREE, 0);
3648 /* Convert value RHS to type TYPE as preparation for an assignment
3649 to an lvalue of type TYPE.
3650 The real work of conversion is done by `convert'.
3651 The purpose of this function is to generate error messages
3652 for assignments that are not allowed in C.
3653 ERRTYPE says whether it is argument passing, assignment,
3654 initialization or return.
3656 FUNCTION is a tree for the function being called.
3657 PARMNUM is the number of the argument, for printing in error messages. */
3659 static tree
3660 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3661 tree fundecl, tree function, int parmnum)
3663 enum tree_code codel = TREE_CODE (type);
3664 tree rhstype;
3665 enum tree_code coder;
3666 tree rname = NULL_TREE;
3667 bool objc_ok = false;
3669 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3671 tree selector;
3672 /* Change pointer to function to the function itself for
3673 diagnostics. */
3674 if (TREE_CODE (function) == ADDR_EXPR
3675 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3676 function = TREE_OPERAND (function, 0);
3678 /* Handle an ObjC selector specially for diagnostics. */
3679 selector = objc_message_selector ();
3680 rname = function;
3681 if (selector && parmnum > 2)
3683 rname = selector;
3684 parmnum -= 2;
3688 /* This macro is used to emit diagnostics to ensure that all format
3689 strings are complete sentences, visible to gettext and checked at
3690 compile time. */
3691 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3692 do { \
3693 switch (errtype) \
3695 case ic_argpass: \
3696 pedwarn (AR, parmnum, rname); \
3697 break; \
3698 case ic_argpass_nonproto: \
3699 warning (0, AR, parmnum, rname); \
3700 break; \
3701 case ic_assign: \
3702 pedwarn (AS); \
3703 break; \
3704 case ic_init: \
3705 pedwarn (IN); \
3706 break; \
3707 case ic_return: \
3708 pedwarn (RE); \
3709 break; \
3710 default: \
3711 gcc_unreachable (); \
3713 } while (0)
3715 STRIP_TYPE_NOPS (rhs);
3717 if (optimize && TREE_CODE (rhs) == VAR_DECL
3718 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3719 rhs = decl_constant_value_for_broken_optimization (rhs);
3721 rhstype = TREE_TYPE (rhs);
3722 coder = TREE_CODE (rhstype);
3724 if (coder == ERROR_MARK)
3725 return error_mark_node;
3727 if (c_dialect_objc ())
3729 int parmno;
3731 switch (errtype)
3733 case ic_return:
3734 parmno = 0;
3735 break;
3737 case ic_assign:
3738 parmno = -1;
3739 break;
3741 case ic_init:
3742 parmno = -2;
3743 break;
3745 default:
3746 parmno = parmnum;
3747 break;
3750 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3753 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3755 overflow_warning (rhs);
3756 return rhs;
3759 if (coder == VOID_TYPE)
3761 /* Except for passing an argument to an unprototyped function,
3762 this is a constraint violation. When passing an argument to
3763 an unprototyped function, it is compile-time undefined;
3764 making it a constraint in that case was rejected in
3765 DR#252. */
3766 error ("void value not ignored as it ought to be");
3767 return error_mark_node;
3769 /* A type converts to a reference to it.
3770 This code doesn't fully support references, it's just for the
3771 special case of va_start and va_copy. */
3772 if (codel == REFERENCE_TYPE
3773 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3775 if (!lvalue_p (rhs))
3777 error ("cannot pass rvalue to reference parameter");
3778 return error_mark_node;
3780 if (!c_mark_addressable (rhs))
3781 return error_mark_node;
3782 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3784 /* We already know that these two types are compatible, but they
3785 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3786 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3787 likely to be va_list, a typedef to __builtin_va_list, which
3788 is different enough that it will cause problems later. */
3789 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3790 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3792 rhs = build1 (NOP_EXPR, type, rhs);
3793 return rhs;
3795 /* Some types can interconvert without explicit casts. */
3796 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3797 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3798 return convert (type, rhs);
3799 /* Arithmetic types all interconvert, and enum is treated like int. */
3800 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3801 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3802 || codel == BOOLEAN_TYPE)
3803 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3804 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3805 || coder == BOOLEAN_TYPE))
3806 return convert_and_check (type, rhs);
3808 /* Conversion to a transparent union from its member types.
3809 This applies only to function arguments. */
3810 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3811 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3813 tree memb, marginal_memb = NULL_TREE;
3815 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3817 tree memb_type = TREE_TYPE (memb);
3819 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3820 TYPE_MAIN_VARIANT (rhstype)))
3821 break;
3823 if (TREE_CODE (memb_type) != POINTER_TYPE)
3824 continue;
3826 if (coder == POINTER_TYPE)
3828 tree ttl = TREE_TYPE (memb_type);
3829 tree ttr = TREE_TYPE (rhstype);
3831 /* Any non-function converts to a [const][volatile] void *
3832 and vice versa; otherwise, targets must be the same.
3833 Meanwhile, the lhs target must have all the qualifiers of
3834 the rhs. */
3835 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3836 || comp_target_types (memb_type, rhstype))
3838 /* If this type won't generate any warnings, use it. */
3839 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3840 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3841 && TREE_CODE (ttl) == FUNCTION_TYPE)
3842 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3843 == TYPE_QUALS (ttr))
3844 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3845 == TYPE_QUALS (ttl))))
3846 break;
3848 /* Keep looking for a better type, but remember this one. */
3849 if (!marginal_memb)
3850 marginal_memb = memb;
3854 /* Can convert integer zero to any pointer type. */
3855 if (null_pointer_constant_p (rhs))
3857 rhs = null_pointer_node;
3858 break;
3862 if (memb || marginal_memb)
3864 if (!memb)
3866 /* We have only a marginally acceptable member type;
3867 it needs a warning. */
3868 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3869 tree ttr = TREE_TYPE (rhstype);
3871 /* Const and volatile mean something different for function
3872 types, so the usual warnings are not appropriate. */
3873 if (TREE_CODE (ttr) == FUNCTION_TYPE
3874 && TREE_CODE (ttl) == FUNCTION_TYPE)
3876 /* Because const and volatile on functions are
3877 restrictions that say the function will not do
3878 certain things, it is okay to use a const or volatile
3879 function where an ordinary one is wanted, but not
3880 vice-versa. */
3881 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3882 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3883 "makes qualified function "
3884 "pointer from unqualified"),
3885 G_("assignment makes qualified "
3886 "function pointer from "
3887 "unqualified"),
3888 G_("initialization makes qualified "
3889 "function pointer from "
3890 "unqualified"),
3891 G_("return makes qualified function "
3892 "pointer from unqualified"));
3894 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3895 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3896 "qualifiers from pointer target type"),
3897 G_("assignment discards qualifiers "
3898 "from pointer target type"),
3899 G_("initialization discards qualifiers "
3900 "from pointer target type"),
3901 G_("return discards qualifiers from "
3902 "pointer target type"));
3904 memb = marginal_memb;
3907 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3908 pedwarn ("ISO C prohibits argument conversion to union type");
3910 return build_constructor_single (type, memb, rhs);
3914 /* Conversions among pointers */
3915 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3916 && (coder == codel))
3918 tree ttl = TREE_TYPE (type);
3919 tree ttr = TREE_TYPE (rhstype);
3920 tree mvl = ttl;
3921 tree mvr = ttr;
3922 bool is_opaque_pointer;
3923 int target_cmp = 0; /* Cache comp_target_types () result. */
3925 if (TREE_CODE (mvl) != ARRAY_TYPE)
3926 mvl = TYPE_MAIN_VARIANT (mvl);
3927 if (TREE_CODE (mvr) != ARRAY_TYPE)
3928 mvr = TYPE_MAIN_VARIANT (mvr);
3929 /* Opaque pointers are treated like void pointers. */
3930 is_opaque_pointer = (targetm.vector_opaque_p (type)
3931 || targetm.vector_opaque_p (rhstype))
3932 && TREE_CODE (ttl) == VECTOR_TYPE
3933 && TREE_CODE (ttr) == VECTOR_TYPE;
3935 /* C++ does not allow the implicit conversion void* -> T*. However,
3936 for the purpose of reducing the number of false positives, we
3937 tolerate the special case of
3939 int *p = NULL;
3941 where NULL is typically defined in C to be '(void *) 0'. */
3942 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
3943 warning (OPT_Wc___compat, "request for implicit conversion from "
3944 "%qT to %qT not permitted in C++", rhstype, type);
3946 /* Check if the right-hand side has a format attribute but the
3947 left-hand side doesn't. */
3948 if (warn_missing_format_attribute
3949 && check_missing_format_attribute (type, rhstype))
3951 switch (errtype)
3953 case ic_argpass:
3954 case ic_argpass_nonproto:
3955 warning (OPT_Wmissing_format_attribute,
3956 "argument %d of %qE might be "
3957 "a candidate for a format attribute",
3958 parmnum, rname);
3959 break;
3960 case ic_assign:
3961 warning (OPT_Wmissing_format_attribute,
3962 "assignment left-hand side might be "
3963 "a candidate for a format attribute");
3964 break;
3965 case ic_init:
3966 warning (OPT_Wmissing_format_attribute,
3967 "initialization left-hand side might be "
3968 "a candidate for a format attribute");
3969 break;
3970 case ic_return:
3971 warning (OPT_Wmissing_format_attribute,
3972 "return type might be "
3973 "a candidate for a format attribute");
3974 break;
3975 default:
3976 gcc_unreachable ();
3980 /* Any non-function converts to a [const][volatile] void *
3981 and vice versa; otherwise, targets must be the same.
3982 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3983 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3984 || (target_cmp = comp_target_types (type, rhstype))
3985 || is_opaque_pointer
3986 || (c_common_unsigned_type (mvl)
3987 == c_common_unsigned_type (mvr)))
3989 if (pedantic
3990 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3992 (VOID_TYPE_P (ttr)
3993 && !null_pointer_constant_p (rhs)
3994 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3995 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3996 "%qE between function pointer "
3997 "and %<void *%>"),
3998 G_("ISO C forbids assignment between "
3999 "function pointer and %<void *%>"),
4000 G_("ISO C forbids initialization between "
4001 "function pointer and %<void *%>"),
4002 G_("ISO C forbids return between function "
4003 "pointer and %<void *%>"));
4004 /* Const and volatile mean something different for function types,
4005 so the usual warnings are not appropriate. */
4006 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4007 && TREE_CODE (ttl) != FUNCTION_TYPE)
4009 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4011 /* Types differing only by the presence of the 'volatile'
4012 qualifier are acceptable if the 'volatile' has been added
4013 in by the Objective-C EH machinery. */
4014 if (!objc_type_quals_match (ttl, ttr))
4015 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4016 "qualifiers from pointer target type"),
4017 G_("assignment discards qualifiers "
4018 "from pointer target type"),
4019 G_("initialization discards qualifiers "
4020 "from pointer target type"),
4021 G_("return discards qualifiers from "
4022 "pointer target type"));
4024 /* If this is not a case of ignoring a mismatch in signedness,
4025 no warning. */
4026 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4027 || target_cmp)
4029 /* If there is a mismatch, do warn. */
4030 else if (warn_pointer_sign)
4031 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4032 "%d of %qE differ in signedness"),
4033 G_("pointer targets in assignment "
4034 "differ in signedness"),
4035 G_("pointer targets in initialization "
4036 "differ in signedness"),
4037 G_("pointer targets in return differ "
4038 "in signedness"));
4040 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4041 && TREE_CODE (ttr) == FUNCTION_TYPE)
4043 /* Because const and volatile on functions are restrictions
4044 that say the function will not do certain things,
4045 it is okay to use a const or volatile function
4046 where an ordinary one is wanted, but not vice-versa. */
4047 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4048 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4049 "qualified function pointer "
4050 "from unqualified"),
4051 G_("assignment makes qualified function "
4052 "pointer from unqualified"),
4053 G_("initialization makes qualified "
4054 "function pointer from unqualified"),
4055 G_("return makes qualified function "
4056 "pointer from unqualified"));
4059 else
4060 /* Avoid warning about the volatile ObjC EH puts on decls. */
4061 if (!objc_ok)
4062 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4063 "incompatible pointer type"),
4064 G_("assignment from incompatible pointer type"),
4065 G_("initialization from incompatible "
4066 "pointer type"),
4067 G_("return from incompatible pointer type"));
4069 return convert (type, rhs);
4071 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4073 /* ??? This should not be an error when inlining calls to
4074 unprototyped functions. */
4075 error ("invalid use of non-lvalue array");
4076 return error_mark_node;
4078 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4080 /* An explicit constant 0 can convert to a pointer,
4081 or one that results from arithmetic, even including
4082 a cast to integer type. */
4083 if (!null_pointer_constant_p (rhs))
4084 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4085 "pointer from integer without a cast"),
4086 G_("assignment makes pointer from integer "
4087 "without a cast"),
4088 G_("initialization makes pointer from "
4089 "integer without a cast"),
4090 G_("return makes pointer from integer "
4091 "without a cast"));
4093 return convert (type, rhs);
4095 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4097 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4098 "from pointer without a cast"),
4099 G_("assignment makes integer from pointer "
4100 "without a cast"),
4101 G_("initialization makes integer from pointer "
4102 "without a cast"),
4103 G_("return makes integer from pointer "
4104 "without a cast"));
4105 return convert (type, rhs);
4107 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4108 return convert (type, rhs);
4110 switch (errtype)
4112 case ic_argpass:
4113 case ic_argpass_nonproto:
4114 /* ??? This should not be an error when inlining calls to
4115 unprototyped functions. */
4116 error ("incompatible type for argument %d of %qE", parmnum, rname);
4117 break;
4118 case ic_assign:
4119 error ("incompatible types in assignment");
4120 break;
4121 case ic_init:
4122 error ("incompatible types in initialization");
4123 break;
4124 case ic_return:
4125 error ("incompatible types in return");
4126 break;
4127 default:
4128 gcc_unreachable ();
4131 return error_mark_node;
4134 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4135 is used for error and waring reporting and indicates which argument
4136 is being processed. */
4138 tree
4139 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4141 tree ret, type;
4143 /* If FN was prototyped, the value has been converted already
4144 in convert_arguments. */
4145 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4146 return value;
4148 type = TREE_TYPE (parm);
4149 ret = convert_for_assignment (type, value,
4150 ic_argpass_nonproto, fn,
4151 fn, argnum);
4152 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4153 && INTEGRAL_TYPE_P (type)
4154 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4155 ret = default_conversion (ret);
4156 return ret;
4159 /* If VALUE is a compound expr all of whose expressions are constant, then
4160 return its value. Otherwise, return error_mark_node.
4162 This is for handling COMPOUND_EXPRs as initializer elements
4163 which is allowed with a warning when -pedantic is specified. */
4165 static tree
4166 valid_compound_expr_initializer (tree value, tree endtype)
4168 if (TREE_CODE (value) == COMPOUND_EXPR)
4170 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4171 == error_mark_node)
4172 return error_mark_node;
4173 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4174 endtype);
4176 else if (!initializer_constant_valid_p (value, endtype))
4177 return error_mark_node;
4178 else
4179 return value;
4182 /* Perform appropriate conversions on the initial value of a variable,
4183 store it in the declaration DECL,
4184 and print any error messages that are appropriate.
4185 If the init is invalid, store an ERROR_MARK. */
4187 void
4188 store_init_value (tree decl, tree init)
4190 tree value, type;
4192 /* If variable's type was invalidly declared, just ignore it. */
4194 type = TREE_TYPE (decl);
4195 if (TREE_CODE (type) == ERROR_MARK)
4196 return;
4198 /* Digest the specified initializer into an expression. */
4200 value = digest_init (type, init, true, TREE_STATIC (decl));
4202 /* Store the expression if valid; else report error. */
4204 if (!in_system_header
4205 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4206 warning (OPT_Wtraditional, "traditional C rejects automatic "
4207 "aggregate initialization");
4209 DECL_INITIAL (decl) = value;
4211 /* ANSI wants warnings about out-of-range constant initializers. */
4212 STRIP_TYPE_NOPS (value);
4213 constant_expression_warning (value);
4215 /* Check if we need to set array size from compound literal size. */
4216 if (TREE_CODE (type) == ARRAY_TYPE
4217 && TYPE_DOMAIN (type) == 0
4218 && value != error_mark_node)
4220 tree inside_init = init;
4222 STRIP_TYPE_NOPS (inside_init);
4223 inside_init = fold (inside_init);
4225 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4227 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4229 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4231 /* For int foo[] = (int [3]){1}; we need to set array size
4232 now since later on array initializer will be just the
4233 brace enclosed list of the compound literal. */
4234 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4235 layout_type (type);
4236 layout_decl (decl, 0);
4242 /* Methods for storing and printing names for error messages. */
4244 /* Implement a spelling stack that allows components of a name to be pushed
4245 and popped. Each element on the stack is this structure. */
4247 struct spelling
4249 int kind;
4250 union
4252 int i;
4253 const char *s;
4254 } u;
4257 #define SPELLING_STRING 1
4258 #define SPELLING_MEMBER 2
4259 #define SPELLING_BOUNDS 3
4261 static struct spelling *spelling; /* Next stack element (unused). */
4262 static struct spelling *spelling_base; /* Spelling stack base. */
4263 static int spelling_size; /* Size of the spelling stack. */
4265 /* Macros to save and restore the spelling stack around push_... functions.
4266 Alternative to SAVE_SPELLING_STACK. */
4268 #define SPELLING_DEPTH() (spelling - spelling_base)
4269 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4271 /* Push an element on the spelling stack with type KIND and assign VALUE
4272 to MEMBER. */
4274 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4276 int depth = SPELLING_DEPTH (); \
4278 if (depth >= spelling_size) \
4280 spelling_size += 10; \
4281 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4282 spelling_size); \
4283 RESTORE_SPELLING_DEPTH (depth); \
4286 spelling->kind = (KIND); \
4287 spelling->MEMBER = (VALUE); \
4288 spelling++; \
4291 /* Push STRING on the stack. Printed literally. */
4293 static void
4294 push_string (const char *string)
4296 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4299 /* Push a member name on the stack. Printed as '.' STRING. */
4301 static void
4302 push_member_name (tree decl)
4304 const char *const string
4305 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4306 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4309 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4311 static void
4312 push_array_bounds (int bounds)
4314 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4317 /* Compute the maximum size in bytes of the printed spelling. */
4319 static int
4320 spelling_length (void)
4322 int size = 0;
4323 struct spelling *p;
4325 for (p = spelling_base; p < spelling; p++)
4327 if (p->kind == SPELLING_BOUNDS)
4328 size += 25;
4329 else
4330 size += strlen (p->u.s) + 1;
4333 return size;
4336 /* Print the spelling to BUFFER and return it. */
4338 static char *
4339 print_spelling (char *buffer)
4341 char *d = buffer;
4342 struct spelling *p;
4344 for (p = spelling_base; p < spelling; p++)
4345 if (p->kind == SPELLING_BOUNDS)
4347 sprintf (d, "[%d]", p->u.i);
4348 d += strlen (d);
4350 else
4352 const char *s;
4353 if (p->kind == SPELLING_MEMBER)
4354 *d++ = '.';
4355 for (s = p->u.s; (*d = *s++); d++)
4358 *d++ = '\0';
4359 return buffer;
4362 /* Issue an error message for a bad initializer component.
4363 MSGID identifies the message.
4364 The component name is taken from the spelling stack. */
4366 void
4367 error_init (const char *msgid)
4369 char *ofwhat;
4371 error ("%s", _(msgid));
4372 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4373 if (*ofwhat)
4374 error ("(near initialization for %qs)", ofwhat);
4377 /* Issue a pedantic warning for a bad initializer component.
4378 MSGID identifies the message.
4379 The component name is taken from the spelling stack. */
4381 void
4382 pedwarn_init (const char *msgid)
4384 char *ofwhat;
4386 pedwarn ("%s", _(msgid));
4387 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4388 if (*ofwhat)
4389 pedwarn ("(near initialization for %qs)", ofwhat);
4392 /* Issue a warning for a bad initializer component.
4393 MSGID identifies the message.
4394 The component name is taken from the spelling stack. */
4396 static void
4397 warning_init (const char *msgid)
4399 char *ofwhat;
4401 warning (0, "%s", _(msgid));
4402 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4403 if (*ofwhat)
4404 warning (0, "(near initialization for %qs)", ofwhat);
4407 /* If TYPE is an array type and EXPR is a parenthesized string
4408 constant, warn if pedantic that EXPR is being used to initialize an
4409 object of type TYPE. */
4411 void
4412 maybe_warn_string_init (tree type, struct c_expr expr)
4414 if (pedantic
4415 && TREE_CODE (type) == ARRAY_TYPE
4416 && TREE_CODE (expr.value) == STRING_CST
4417 && expr.original_code != STRING_CST)
4418 pedwarn_init ("array initialized from parenthesized string constant");
4421 /* Digest the parser output INIT as an initializer for type TYPE.
4422 Return a C expression of type TYPE to represent the initial value.
4424 If INIT is a string constant, STRICT_STRING is true if it is
4425 unparenthesized or we should not warn here for it being parenthesized.
4426 For other types of INIT, STRICT_STRING is not used.
4428 REQUIRE_CONSTANT requests an error if non-constant initializers or
4429 elements are seen. */
4431 static tree
4432 digest_init (tree type, tree init, bool strict_string, int require_constant)
4434 enum tree_code code = TREE_CODE (type);
4435 tree inside_init = init;
4437 if (type == error_mark_node
4438 || init == error_mark_node
4439 || TREE_TYPE (init) == error_mark_node)
4440 return error_mark_node;
4442 STRIP_TYPE_NOPS (inside_init);
4444 inside_init = fold (inside_init);
4446 /* Initialization of an array of chars from a string constant
4447 optionally enclosed in braces. */
4449 if (code == ARRAY_TYPE && inside_init
4450 && TREE_CODE (inside_init) == STRING_CST)
4452 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4453 /* Note that an array could be both an array of character type
4454 and an array of wchar_t if wchar_t is signed char or unsigned
4455 char. */
4456 bool char_array = (typ1 == char_type_node
4457 || typ1 == signed_char_type_node
4458 || typ1 == unsigned_char_type_node);
4459 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4460 if (char_array || wchar_array)
4462 struct c_expr expr;
4463 bool char_string;
4464 expr.value = inside_init;
4465 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4466 maybe_warn_string_init (type, expr);
4468 char_string
4469 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4470 == char_type_node);
4472 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4473 TYPE_MAIN_VARIANT (type)))
4474 return inside_init;
4476 if (!wchar_array && !char_string)
4478 error_init ("char-array initialized from wide string");
4479 return error_mark_node;
4481 if (char_string && !char_array)
4483 error_init ("wchar_t-array initialized from non-wide string");
4484 return error_mark_node;
4487 TREE_TYPE (inside_init) = type;
4488 if (TYPE_DOMAIN (type) != 0
4489 && TYPE_SIZE (type) != 0
4490 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4491 /* Subtract 1 (or sizeof (wchar_t))
4492 because it's ok to ignore the terminating null char
4493 that is counted in the length of the constant. */
4494 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4495 TREE_STRING_LENGTH (inside_init)
4496 - ((TYPE_PRECISION (typ1)
4497 != TYPE_PRECISION (char_type_node))
4498 ? (TYPE_PRECISION (wchar_type_node)
4499 / BITS_PER_UNIT)
4500 : 1)))
4501 pedwarn_init ("initializer-string for array of chars is too long");
4503 return inside_init;
4505 else if (INTEGRAL_TYPE_P (typ1))
4507 error_init ("array of inappropriate type initialized "
4508 "from string constant");
4509 return error_mark_node;
4513 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4514 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4515 below and handle as a constructor. */
4516 if (code == VECTOR_TYPE
4517 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4518 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4519 && TREE_CONSTANT (inside_init))
4521 if (TREE_CODE (inside_init) == VECTOR_CST
4522 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4523 TYPE_MAIN_VARIANT (type)))
4524 return inside_init;
4526 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4528 unsigned HOST_WIDE_INT ix;
4529 tree value;
4530 bool constant_p = true;
4532 /* Iterate through elements and check if all constructor
4533 elements are *_CSTs. */
4534 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4535 if (!CONSTANT_CLASS_P (value))
4537 constant_p = false;
4538 break;
4541 if (constant_p)
4542 return build_vector_from_ctor (type,
4543 CONSTRUCTOR_ELTS (inside_init));
4547 /* Any type can be initialized
4548 from an expression of the same type, optionally with braces. */
4550 if (inside_init && TREE_TYPE (inside_init) != 0
4551 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4552 TYPE_MAIN_VARIANT (type))
4553 || (code == ARRAY_TYPE
4554 && comptypes (TREE_TYPE (inside_init), type))
4555 || (code == VECTOR_TYPE
4556 && comptypes (TREE_TYPE (inside_init), type))
4557 || (code == POINTER_TYPE
4558 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4559 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4560 TREE_TYPE (type)))))
4562 if (code == POINTER_TYPE)
4564 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4566 if (TREE_CODE (inside_init) == STRING_CST
4567 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4568 inside_init = array_to_pointer_conversion (inside_init);
4569 else
4571 error_init ("invalid use of non-lvalue array");
4572 return error_mark_node;
4577 if (code == VECTOR_TYPE)
4578 /* Although the types are compatible, we may require a
4579 conversion. */
4580 inside_init = convert (type, inside_init);
4582 if (require_constant && !flag_isoc99
4583 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4585 /* As an extension, allow initializing objects with static storage
4586 duration with compound literals (which are then treated just as
4587 the brace enclosed list they contain). */
4588 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4589 inside_init = DECL_INITIAL (decl);
4592 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4593 && TREE_CODE (inside_init) != CONSTRUCTOR)
4595 error_init ("array initialized from non-constant array expression");
4596 return error_mark_node;
4599 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4600 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4602 /* Compound expressions can only occur here if -pedantic or
4603 -pedantic-errors is specified. In the later case, we always want
4604 an error. In the former case, we simply want a warning. */
4605 if (require_constant && pedantic
4606 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4608 inside_init
4609 = valid_compound_expr_initializer (inside_init,
4610 TREE_TYPE (inside_init));
4611 if (inside_init == error_mark_node)
4612 error_init ("initializer element is not constant");
4613 else
4614 pedwarn_init ("initializer element is not constant");
4615 if (flag_pedantic_errors)
4616 inside_init = error_mark_node;
4618 else if (require_constant
4619 && !initializer_constant_valid_p (inside_init,
4620 TREE_TYPE (inside_init)))
4622 error_init ("initializer element is not constant");
4623 inside_init = error_mark_node;
4626 /* Added to enable additional -Wmissing-format-attribute warnings. */
4627 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4628 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4629 NULL_TREE, 0);
4630 return inside_init;
4633 /* Handle scalar types, including conversions. */
4635 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4636 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4637 || code == VECTOR_TYPE)
4639 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4640 && (TREE_CODE (init) == STRING_CST
4641 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4642 init = array_to_pointer_conversion (init);
4643 inside_init
4644 = convert_for_assignment (type, init, ic_init,
4645 NULL_TREE, NULL_TREE, 0);
4647 /* Check to see if we have already given an error message. */
4648 if (inside_init == error_mark_node)
4650 else if (require_constant && !TREE_CONSTANT (inside_init))
4652 error_init ("initializer element is not constant");
4653 inside_init = error_mark_node;
4655 else if (require_constant
4656 && !initializer_constant_valid_p (inside_init,
4657 TREE_TYPE (inside_init)))
4659 error_init ("initializer element is not computable at load time");
4660 inside_init = error_mark_node;
4663 return inside_init;
4666 /* Come here only for records and arrays. */
4668 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4670 error_init ("variable-sized object may not be initialized");
4671 return error_mark_node;
4674 error_init ("invalid initializer");
4675 return error_mark_node;
4678 /* Handle initializers that use braces. */
4680 /* Type of object we are accumulating a constructor for.
4681 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4682 static tree constructor_type;
4684 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4685 left to fill. */
4686 static tree constructor_fields;
4688 /* For an ARRAY_TYPE, this is the specified index
4689 at which to store the next element we get. */
4690 static tree constructor_index;
4692 /* For an ARRAY_TYPE, this is the maximum index. */
4693 static tree constructor_max_index;
4695 /* For a RECORD_TYPE, this is the first field not yet written out. */
4696 static tree constructor_unfilled_fields;
4698 /* For an ARRAY_TYPE, this is the index of the first element
4699 not yet written out. */
4700 static tree constructor_unfilled_index;
4702 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4703 This is so we can generate gaps between fields, when appropriate. */
4704 static tree constructor_bit_index;
4706 /* If we are saving up the elements rather than allocating them,
4707 this is the list of elements so far (in reverse order,
4708 most recent first). */
4709 static VEC(constructor_elt,gc) *constructor_elements;
4711 /* 1 if constructor should be incrementally stored into a constructor chain,
4712 0 if all the elements should be kept in AVL tree. */
4713 static int constructor_incremental;
4715 /* 1 if so far this constructor's elements are all compile-time constants. */
4716 static int constructor_constant;
4718 /* 1 if so far this constructor's elements are all valid address constants. */
4719 static int constructor_simple;
4721 /* 1 if this constructor is erroneous so far. */
4722 static int constructor_erroneous;
4724 /* Structure for managing pending initializer elements, organized as an
4725 AVL tree. */
4727 struct init_node
4729 struct init_node *left, *right;
4730 struct init_node *parent;
4731 int balance;
4732 tree purpose;
4733 tree value;
4736 /* Tree of pending elements at this constructor level.
4737 These are elements encountered out of order
4738 which belong at places we haven't reached yet in actually
4739 writing the output.
4740 Will never hold tree nodes across GC runs. */
4741 static struct init_node *constructor_pending_elts;
4743 /* The SPELLING_DEPTH of this constructor. */
4744 static int constructor_depth;
4746 /* DECL node for which an initializer is being read.
4747 0 means we are reading a constructor expression
4748 such as (struct foo) {...}. */
4749 static tree constructor_decl;
4751 /* Nonzero if this is an initializer for a top-level decl. */
4752 static int constructor_top_level;
4754 /* Nonzero if there were any member designators in this initializer. */
4755 static int constructor_designated;
4757 /* Nesting depth of designator list. */
4758 static int designator_depth;
4760 /* Nonzero if there were diagnosed errors in this designator list. */
4761 static int designator_erroneous;
4764 /* This stack has a level for each implicit or explicit level of
4765 structuring in the initializer, including the outermost one. It
4766 saves the values of most of the variables above. */
4768 struct constructor_range_stack;
4770 struct constructor_stack
4772 struct constructor_stack *next;
4773 tree type;
4774 tree fields;
4775 tree index;
4776 tree max_index;
4777 tree unfilled_index;
4778 tree unfilled_fields;
4779 tree bit_index;
4780 VEC(constructor_elt,gc) *elements;
4781 struct init_node *pending_elts;
4782 int offset;
4783 int depth;
4784 /* If value nonzero, this value should replace the entire
4785 constructor at this level. */
4786 struct c_expr replacement_value;
4787 struct constructor_range_stack *range_stack;
4788 char constant;
4789 char simple;
4790 char implicit;
4791 char erroneous;
4792 char outer;
4793 char incremental;
4794 char designated;
4797 static struct constructor_stack *constructor_stack;
4799 /* This stack represents designators from some range designator up to
4800 the last designator in the list. */
4802 struct constructor_range_stack
4804 struct constructor_range_stack *next, *prev;
4805 struct constructor_stack *stack;
4806 tree range_start;
4807 tree index;
4808 tree range_end;
4809 tree fields;
4812 static struct constructor_range_stack *constructor_range_stack;
4814 /* This stack records separate initializers that are nested.
4815 Nested initializers can't happen in ANSI C, but GNU C allows them
4816 in cases like { ... (struct foo) { ... } ... }. */
4818 struct initializer_stack
4820 struct initializer_stack *next;
4821 tree decl;
4822 struct constructor_stack *constructor_stack;
4823 struct constructor_range_stack *constructor_range_stack;
4824 VEC(constructor_elt,gc) *elements;
4825 struct spelling *spelling;
4826 struct spelling *spelling_base;
4827 int spelling_size;
4828 char top_level;
4829 char require_constant_value;
4830 char require_constant_elements;
4833 static struct initializer_stack *initializer_stack;
4835 /* Prepare to parse and output the initializer for variable DECL. */
4837 void
4838 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4840 const char *locus;
4841 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4843 p->decl = constructor_decl;
4844 p->require_constant_value = require_constant_value;
4845 p->require_constant_elements = require_constant_elements;
4846 p->constructor_stack = constructor_stack;
4847 p->constructor_range_stack = constructor_range_stack;
4848 p->elements = constructor_elements;
4849 p->spelling = spelling;
4850 p->spelling_base = spelling_base;
4851 p->spelling_size = spelling_size;
4852 p->top_level = constructor_top_level;
4853 p->next = initializer_stack;
4854 initializer_stack = p;
4856 constructor_decl = decl;
4857 constructor_designated = 0;
4858 constructor_top_level = top_level;
4860 if (decl != 0 && decl != error_mark_node)
4862 require_constant_value = TREE_STATIC (decl);
4863 require_constant_elements
4864 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4865 /* For a scalar, you can always use any value to initialize,
4866 even within braces. */
4867 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4868 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4869 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4870 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4871 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4873 else
4875 require_constant_value = 0;
4876 require_constant_elements = 0;
4877 locus = "(anonymous)";
4880 constructor_stack = 0;
4881 constructor_range_stack = 0;
4883 missing_braces_mentioned = 0;
4885 spelling_base = 0;
4886 spelling_size = 0;
4887 RESTORE_SPELLING_DEPTH (0);
4889 if (locus)
4890 push_string (locus);
4893 void
4894 finish_init (void)
4896 struct initializer_stack *p = initializer_stack;
4898 /* Free the whole constructor stack of this initializer. */
4899 while (constructor_stack)
4901 struct constructor_stack *q = constructor_stack;
4902 constructor_stack = q->next;
4903 free (q);
4906 gcc_assert (!constructor_range_stack);
4908 /* Pop back to the data of the outer initializer (if any). */
4909 free (spelling_base);
4911 constructor_decl = p->decl;
4912 require_constant_value = p->require_constant_value;
4913 require_constant_elements = p->require_constant_elements;
4914 constructor_stack = p->constructor_stack;
4915 constructor_range_stack = p->constructor_range_stack;
4916 constructor_elements = p->elements;
4917 spelling = p->spelling;
4918 spelling_base = p->spelling_base;
4919 spelling_size = p->spelling_size;
4920 constructor_top_level = p->top_level;
4921 initializer_stack = p->next;
4922 free (p);
4925 /* Call here when we see the initializer is surrounded by braces.
4926 This is instead of a call to push_init_level;
4927 it is matched by a call to pop_init_level.
4929 TYPE is the type to initialize, for a constructor expression.
4930 For an initializer for a decl, TYPE is zero. */
4932 void
4933 really_start_incremental_init (tree type)
4935 struct constructor_stack *p = XNEW (struct constructor_stack);
4937 if (type == 0)
4938 type = TREE_TYPE (constructor_decl);
4940 if (targetm.vector_opaque_p (type))
4941 error ("opaque vector types cannot be initialized");
4943 p->type = constructor_type;
4944 p->fields = constructor_fields;
4945 p->index = constructor_index;
4946 p->max_index = constructor_max_index;
4947 p->unfilled_index = constructor_unfilled_index;
4948 p->unfilled_fields = constructor_unfilled_fields;
4949 p->bit_index = constructor_bit_index;
4950 p->elements = constructor_elements;
4951 p->constant = constructor_constant;
4952 p->simple = constructor_simple;
4953 p->erroneous = constructor_erroneous;
4954 p->pending_elts = constructor_pending_elts;
4955 p->depth = constructor_depth;
4956 p->replacement_value.value = 0;
4957 p->replacement_value.original_code = ERROR_MARK;
4958 p->implicit = 0;
4959 p->range_stack = 0;
4960 p->outer = 0;
4961 p->incremental = constructor_incremental;
4962 p->designated = constructor_designated;
4963 p->next = 0;
4964 constructor_stack = p;
4966 constructor_constant = 1;
4967 constructor_simple = 1;
4968 constructor_depth = SPELLING_DEPTH ();
4969 constructor_elements = 0;
4970 constructor_pending_elts = 0;
4971 constructor_type = type;
4972 constructor_incremental = 1;
4973 constructor_designated = 0;
4974 designator_depth = 0;
4975 designator_erroneous = 0;
4977 if (TREE_CODE (constructor_type) == RECORD_TYPE
4978 || TREE_CODE (constructor_type) == UNION_TYPE)
4980 constructor_fields = TYPE_FIELDS (constructor_type);
4981 /* Skip any nameless bit fields at the beginning. */
4982 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4983 && DECL_NAME (constructor_fields) == 0)
4984 constructor_fields = TREE_CHAIN (constructor_fields);
4986 constructor_unfilled_fields = constructor_fields;
4987 constructor_bit_index = bitsize_zero_node;
4989 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4991 if (TYPE_DOMAIN (constructor_type))
4993 constructor_max_index
4994 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4996 /* Detect non-empty initializations of zero-length arrays. */
4997 if (constructor_max_index == NULL_TREE
4998 && TYPE_SIZE (constructor_type))
4999 constructor_max_index = build_int_cst (NULL_TREE, -1);
5001 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5002 to initialize VLAs will cause a proper error; avoid tree
5003 checking errors as well by setting a safe value. */
5004 if (constructor_max_index
5005 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5006 constructor_max_index = build_int_cst (NULL_TREE, -1);
5008 constructor_index
5009 = convert (bitsizetype,
5010 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5012 else
5014 constructor_index = bitsize_zero_node;
5015 constructor_max_index = NULL_TREE;
5018 constructor_unfilled_index = constructor_index;
5020 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5022 /* Vectors are like simple fixed-size arrays. */
5023 constructor_max_index =
5024 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5025 constructor_index = convert (bitsizetype, bitsize_zero_node);
5026 constructor_unfilled_index = constructor_index;
5028 else
5030 /* Handle the case of int x = {5}; */
5031 constructor_fields = constructor_type;
5032 constructor_unfilled_fields = constructor_type;
5036 /* Push down into a subobject, for initialization.
5037 If this is for an explicit set of braces, IMPLICIT is 0.
5038 If it is because the next element belongs at a lower level,
5039 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5041 void
5042 push_init_level (int implicit)
5044 struct constructor_stack *p;
5045 tree value = NULL_TREE;
5047 /* If we've exhausted any levels that didn't have braces,
5048 pop them now. If implicit == 1, this will have been done in
5049 process_init_element; do not repeat it here because in the case
5050 of excess initializers for an empty aggregate this leads to an
5051 infinite cycle of popping a level and immediately recreating
5052 it. */
5053 if (implicit != 1)
5055 while (constructor_stack->implicit)
5057 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5058 || TREE_CODE (constructor_type) == UNION_TYPE)
5059 && constructor_fields == 0)
5060 process_init_element (pop_init_level (1));
5061 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5062 && constructor_max_index
5063 && tree_int_cst_lt (constructor_max_index,
5064 constructor_index))
5065 process_init_element (pop_init_level (1));
5066 else
5067 break;
5071 /* Unless this is an explicit brace, we need to preserve previous
5072 content if any. */
5073 if (implicit)
5075 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5076 || TREE_CODE (constructor_type) == UNION_TYPE)
5077 && constructor_fields)
5078 value = find_init_member (constructor_fields);
5079 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5080 value = find_init_member (constructor_index);
5083 p = XNEW (struct constructor_stack);
5084 p->type = constructor_type;
5085 p->fields = constructor_fields;
5086 p->index = constructor_index;
5087 p->max_index = constructor_max_index;
5088 p->unfilled_index = constructor_unfilled_index;
5089 p->unfilled_fields = constructor_unfilled_fields;
5090 p->bit_index = constructor_bit_index;
5091 p->elements = constructor_elements;
5092 p->constant = constructor_constant;
5093 p->simple = constructor_simple;
5094 p->erroneous = constructor_erroneous;
5095 p->pending_elts = constructor_pending_elts;
5096 p->depth = constructor_depth;
5097 p->replacement_value.value = 0;
5098 p->replacement_value.original_code = ERROR_MARK;
5099 p->implicit = implicit;
5100 p->outer = 0;
5101 p->incremental = constructor_incremental;
5102 p->designated = constructor_designated;
5103 p->next = constructor_stack;
5104 p->range_stack = 0;
5105 constructor_stack = p;
5107 constructor_constant = 1;
5108 constructor_simple = 1;
5109 constructor_depth = SPELLING_DEPTH ();
5110 constructor_elements = 0;
5111 constructor_incremental = 1;
5112 constructor_designated = 0;
5113 constructor_pending_elts = 0;
5114 if (!implicit)
5116 p->range_stack = constructor_range_stack;
5117 constructor_range_stack = 0;
5118 designator_depth = 0;
5119 designator_erroneous = 0;
5122 /* Don't die if an entire brace-pair level is superfluous
5123 in the containing level. */
5124 if (constructor_type == 0)
5126 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5127 || TREE_CODE (constructor_type) == UNION_TYPE)
5129 /* Don't die if there are extra init elts at the end. */
5130 if (constructor_fields == 0)
5131 constructor_type = 0;
5132 else
5134 constructor_type = TREE_TYPE (constructor_fields);
5135 push_member_name (constructor_fields);
5136 constructor_depth++;
5139 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5141 constructor_type = TREE_TYPE (constructor_type);
5142 push_array_bounds (tree_low_cst (constructor_index, 0));
5143 constructor_depth++;
5146 if (constructor_type == 0)
5148 error_init ("extra brace group at end of initializer");
5149 constructor_fields = 0;
5150 constructor_unfilled_fields = 0;
5151 return;
5154 if (value && TREE_CODE (value) == CONSTRUCTOR)
5156 constructor_constant = TREE_CONSTANT (value);
5157 constructor_simple = TREE_STATIC (value);
5158 constructor_elements = CONSTRUCTOR_ELTS (value);
5159 if (!VEC_empty (constructor_elt, constructor_elements)
5160 && (TREE_CODE (constructor_type) == RECORD_TYPE
5161 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5162 set_nonincremental_init ();
5165 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5167 missing_braces_mentioned = 1;
5168 warning_init ("missing braces around initializer");
5171 if (TREE_CODE (constructor_type) == RECORD_TYPE
5172 || TREE_CODE (constructor_type) == UNION_TYPE)
5174 constructor_fields = TYPE_FIELDS (constructor_type);
5175 /* Skip any nameless bit fields at the beginning. */
5176 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5177 && DECL_NAME (constructor_fields) == 0)
5178 constructor_fields = TREE_CHAIN (constructor_fields);
5180 constructor_unfilled_fields = constructor_fields;
5181 constructor_bit_index = bitsize_zero_node;
5183 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5185 /* Vectors are like simple fixed-size arrays. */
5186 constructor_max_index =
5187 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5188 constructor_index = convert (bitsizetype, integer_zero_node);
5189 constructor_unfilled_index = constructor_index;
5191 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5193 if (TYPE_DOMAIN (constructor_type))
5195 constructor_max_index
5196 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5198 /* Detect non-empty initializations of zero-length arrays. */
5199 if (constructor_max_index == NULL_TREE
5200 && TYPE_SIZE (constructor_type))
5201 constructor_max_index = build_int_cst (NULL_TREE, -1);
5203 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5204 to initialize VLAs will cause a proper error; avoid tree
5205 checking errors as well by setting a safe value. */
5206 if (constructor_max_index
5207 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5208 constructor_max_index = build_int_cst (NULL_TREE, -1);
5210 constructor_index
5211 = convert (bitsizetype,
5212 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5214 else
5215 constructor_index = bitsize_zero_node;
5217 constructor_unfilled_index = constructor_index;
5218 if (value && TREE_CODE (value) == STRING_CST)
5220 /* We need to split the char/wchar array into individual
5221 characters, so that we don't have to special case it
5222 everywhere. */
5223 set_nonincremental_init_from_string (value);
5226 else
5228 if (constructor_type != error_mark_node)
5229 warning_init ("braces around scalar initializer");
5230 constructor_fields = constructor_type;
5231 constructor_unfilled_fields = constructor_type;
5235 /* At the end of an implicit or explicit brace level,
5236 finish up that level of constructor. If a single expression
5237 with redundant braces initialized that level, return the
5238 c_expr structure for that expression. Otherwise, the original_code
5239 element is set to ERROR_MARK.
5240 If we were outputting the elements as they are read, return 0 as the value
5241 from inner levels (process_init_element ignores that),
5242 but return error_mark_node as the value from the outermost level
5243 (that's what we want to put in DECL_INITIAL).
5244 Otherwise, return a CONSTRUCTOR expression as the value. */
5246 struct c_expr
5247 pop_init_level (int implicit)
5249 struct constructor_stack *p;
5250 struct c_expr ret;
5251 ret.value = 0;
5252 ret.original_code = ERROR_MARK;
5254 if (implicit == 0)
5256 /* When we come to an explicit close brace,
5257 pop any inner levels that didn't have explicit braces. */
5258 while (constructor_stack->implicit)
5259 process_init_element (pop_init_level (1));
5261 gcc_assert (!constructor_range_stack);
5264 /* Now output all pending elements. */
5265 constructor_incremental = 1;
5266 output_pending_init_elements (1);
5268 p = constructor_stack;
5270 /* Error for initializing a flexible array member, or a zero-length
5271 array member in an inappropriate context. */
5272 if (constructor_type && constructor_fields
5273 && TREE_CODE (constructor_type) == ARRAY_TYPE
5274 && TYPE_DOMAIN (constructor_type)
5275 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5277 /* Silently discard empty initializations. The parser will
5278 already have pedwarned for empty brackets. */
5279 if (integer_zerop (constructor_unfilled_index))
5280 constructor_type = NULL_TREE;
5281 else
5283 gcc_assert (!TYPE_SIZE (constructor_type));
5285 if (constructor_depth > 2)
5286 error_init ("initialization of flexible array member in a nested context");
5287 else if (pedantic)
5288 pedwarn_init ("initialization of a flexible array member");
5290 /* We have already issued an error message for the existence
5291 of a flexible array member not at the end of the structure.
5292 Discard the initializer so that we do not die later. */
5293 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5294 constructor_type = NULL_TREE;
5298 /* Warn when some struct elements are implicitly initialized to zero. */
5299 if (warn_missing_field_initializers
5300 && constructor_type
5301 && TREE_CODE (constructor_type) == RECORD_TYPE
5302 && constructor_unfilled_fields)
5304 /* Do not warn for flexible array members or zero-length arrays. */
5305 while (constructor_unfilled_fields
5306 && (!DECL_SIZE (constructor_unfilled_fields)
5307 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5308 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5310 /* Do not warn if this level of the initializer uses member
5311 designators; it is likely to be deliberate. */
5312 if (constructor_unfilled_fields && !constructor_designated)
5314 push_member_name (constructor_unfilled_fields);
5315 warning_init ("missing initializer");
5316 RESTORE_SPELLING_DEPTH (constructor_depth);
5320 /* Pad out the end of the structure. */
5321 if (p->replacement_value.value)
5322 /* If this closes a superfluous brace pair,
5323 just pass out the element between them. */
5324 ret = p->replacement_value;
5325 else if (constructor_type == 0)
5327 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5328 && TREE_CODE (constructor_type) != UNION_TYPE
5329 && TREE_CODE (constructor_type) != ARRAY_TYPE
5330 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5332 /* A nonincremental scalar initializer--just return
5333 the element, after verifying there is just one. */
5334 if (VEC_empty (constructor_elt,constructor_elements))
5336 if (!constructor_erroneous)
5337 error_init ("empty scalar initializer");
5338 ret.value = error_mark_node;
5340 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5342 error_init ("extra elements in scalar initializer");
5343 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5345 else
5346 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5348 else
5350 if (constructor_erroneous)
5351 ret.value = error_mark_node;
5352 else
5354 ret.value = build_constructor (constructor_type,
5355 constructor_elements);
5356 if (constructor_constant)
5357 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5358 if (constructor_constant && constructor_simple)
5359 TREE_STATIC (ret.value) = 1;
5363 constructor_type = p->type;
5364 constructor_fields = p->fields;
5365 constructor_index = p->index;
5366 constructor_max_index = p->max_index;
5367 constructor_unfilled_index = p->unfilled_index;
5368 constructor_unfilled_fields = p->unfilled_fields;
5369 constructor_bit_index = p->bit_index;
5370 constructor_elements = p->elements;
5371 constructor_constant = p->constant;
5372 constructor_simple = p->simple;
5373 constructor_erroneous = p->erroneous;
5374 constructor_incremental = p->incremental;
5375 constructor_designated = p->designated;
5376 constructor_pending_elts = p->pending_elts;
5377 constructor_depth = p->depth;
5378 if (!p->implicit)
5379 constructor_range_stack = p->range_stack;
5380 RESTORE_SPELLING_DEPTH (constructor_depth);
5382 constructor_stack = p->next;
5383 free (p);
5385 if (ret.value == 0)
5387 if (constructor_stack == 0)
5389 ret.value = error_mark_node;
5390 return ret;
5392 return ret;
5394 return ret;
5397 /* Common handling for both array range and field name designators.
5398 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5400 static int
5401 set_designator (int array)
5403 tree subtype;
5404 enum tree_code subcode;
5406 /* Don't die if an entire brace-pair level is superfluous
5407 in the containing level. */
5408 if (constructor_type == 0)
5409 return 1;
5411 /* If there were errors in this designator list already, bail out
5412 silently. */
5413 if (designator_erroneous)
5414 return 1;
5416 if (!designator_depth)
5418 gcc_assert (!constructor_range_stack);
5420 /* Designator list starts at the level of closest explicit
5421 braces. */
5422 while (constructor_stack->implicit)
5423 process_init_element (pop_init_level (1));
5424 constructor_designated = 1;
5425 return 0;
5428 switch (TREE_CODE (constructor_type))
5430 case RECORD_TYPE:
5431 case UNION_TYPE:
5432 subtype = TREE_TYPE (constructor_fields);
5433 if (subtype != error_mark_node)
5434 subtype = TYPE_MAIN_VARIANT (subtype);
5435 break;
5436 case ARRAY_TYPE:
5437 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5438 break;
5439 default:
5440 gcc_unreachable ();
5443 subcode = TREE_CODE (subtype);
5444 if (array && subcode != ARRAY_TYPE)
5446 error_init ("array index in non-array initializer");
5447 return 1;
5449 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5451 error_init ("field name not in record or union initializer");
5452 return 1;
5455 constructor_designated = 1;
5456 push_init_level (2);
5457 return 0;
5460 /* If there are range designators in designator list, push a new designator
5461 to constructor_range_stack. RANGE_END is end of such stack range or
5462 NULL_TREE if there is no range designator at this level. */
5464 static void
5465 push_range_stack (tree range_end)
5467 struct constructor_range_stack *p;
5469 p = GGC_NEW (struct constructor_range_stack);
5470 p->prev = constructor_range_stack;
5471 p->next = 0;
5472 p->fields = constructor_fields;
5473 p->range_start = constructor_index;
5474 p->index = constructor_index;
5475 p->stack = constructor_stack;
5476 p->range_end = range_end;
5477 if (constructor_range_stack)
5478 constructor_range_stack->next = p;
5479 constructor_range_stack = p;
5482 /* Within an array initializer, specify the next index to be initialized.
5483 FIRST is that index. If LAST is nonzero, then initialize a range
5484 of indices, running from FIRST through LAST. */
5486 void
5487 set_init_index (tree first, tree last)
5489 if (set_designator (1))
5490 return;
5492 designator_erroneous = 1;
5494 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5495 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5497 error_init ("array index in initializer not of integer type");
5498 return;
5501 if (TREE_CODE (first) != INTEGER_CST)
5502 error_init ("nonconstant array index in initializer");
5503 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5504 error_init ("nonconstant array index in initializer");
5505 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5506 error_init ("array index in non-array initializer");
5507 else if (tree_int_cst_sgn (first) == -1)
5508 error_init ("array index in initializer exceeds array bounds");
5509 else if (constructor_max_index
5510 && tree_int_cst_lt (constructor_max_index, first))
5511 error_init ("array index in initializer exceeds array bounds");
5512 else
5514 constructor_index = convert (bitsizetype, first);
5516 if (last)
5518 if (tree_int_cst_equal (first, last))
5519 last = 0;
5520 else if (tree_int_cst_lt (last, first))
5522 error_init ("empty index range in initializer");
5523 last = 0;
5525 else
5527 last = convert (bitsizetype, last);
5528 if (constructor_max_index != 0
5529 && tree_int_cst_lt (constructor_max_index, last))
5531 error_init ("array index range in initializer exceeds array bounds");
5532 last = 0;
5537 designator_depth++;
5538 designator_erroneous = 0;
5539 if (constructor_range_stack || last)
5540 push_range_stack (last);
5544 /* Within a struct initializer, specify the next field to be initialized. */
5546 void
5547 set_init_label (tree fieldname)
5549 tree tail;
5551 if (set_designator (0))
5552 return;
5554 designator_erroneous = 1;
5556 if (TREE_CODE (constructor_type) != RECORD_TYPE
5557 && TREE_CODE (constructor_type) != UNION_TYPE)
5559 error_init ("field name not in record or union initializer");
5560 return;
5563 for (tail = TYPE_FIELDS (constructor_type); tail;
5564 tail = TREE_CHAIN (tail))
5566 if (DECL_NAME (tail) == fieldname)
5567 break;
5570 if (tail == 0)
5571 error ("unknown field %qE specified in initializer", fieldname);
5572 else
5574 constructor_fields = tail;
5575 designator_depth++;
5576 designator_erroneous = 0;
5577 if (constructor_range_stack)
5578 push_range_stack (NULL_TREE);
5582 /* Add a new initializer to the tree of pending initializers. PURPOSE
5583 identifies the initializer, either array index or field in a structure.
5584 VALUE is the value of that index or field. */
5586 static void
5587 add_pending_init (tree purpose, tree value)
5589 struct init_node *p, **q, *r;
5591 q = &constructor_pending_elts;
5592 p = 0;
5594 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5596 while (*q != 0)
5598 p = *q;
5599 if (tree_int_cst_lt (purpose, p->purpose))
5600 q = &p->left;
5601 else if (tree_int_cst_lt (p->purpose, purpose))
5602 q = &p->right;
5603 else
5605 if (TREE_SIDE_EFFECTS (p->value))
5606 warning_init ("initialized field with side-effects overwritten");
5607 p->value = value;
5608 return;
5612 else
5614 tree bitpos;
5616 bitpos = bit_position (purpose);
5617 while (*q != NULL)
5619 p = *q;
5620 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5621 q = &p->left;
5622 else if (p->purpose != purpose)
5623 q = &p->right;
5624 else
5626 if (TREE_SIDE_EFFECTS (p->value))
5627 warning_init ("initialized field with side-effects overwritten");
5628 p->value = value;
5629 return;
5634 r = GGC_NEW (struct init_node);
5635 r->purpose = purpose;
5636 r->value = value;
5638 *q = r;
5639 r->parent = p;
5640 r->left = 0;
5641 r->right = 0;
5642 r->balance = 0;
5644 while (p)
5646 struct init_node *s;
5648 if (r == p->left)
5650 if (p->balance == 0)
5651 p->balance = -1;
5652 else if (p->balance < 0)
5654 if (r->balance < 0)
5656 /* L rotation. */
5657 p->left = r->right;
5658 if (p->left)
5659 p->left->parent = p;
5660 r->right = p;
5662 p->balance = 0;
5663 r->balance = 0;
5665 s = p->parent;
5666 p->parent = r;
5667 r->parent = s;
5668 if (s)
5670 if (s->left == p)
5671 s->left = r;
5672 else
5673 s->right = r;
5675 else
5676 constructor_pending_elts = r;
5678 else
5680 /* LR rotation. */
5681 struct init_node *t = r->right;
5683 r->right = t->left;
5684 if (r->right)
5685 r->right->parent = r;
5686 t->left = r;
5688 p->left = t->right;
5689 if (p->left)
5690 p->left->parent = p;
5691 t->right = p;
5693 p->balance = t->balance < 0;
5694 r->balance = -(t->balance > 0);
5695 t->balance = 0;
5697 s = p->parent;
5698 p->parent = t;
5699 r->parent = t;
5700 t->parent = s;
5701 if (s)
5703 if (s->left == p)
5704 s->left = t;
5705 else
5706 s->right = t;
5708 else
5709 constructor_pending_elts = t;
5711 break;
5713 else
5715 /* p->balance == +1; growth of left side balances the node. */
5716 p->balance = 0;
5717 break;
5720 else /* r == p->right */
5722 if (p->balance == 0)
5723 /* Growth propagation from right side. */
5724 p->balance++;
5725 else if (p->balance > 0)
5727 if (r->balance > 0)
5729 /* R rotation. */
5730 p->right = r->left;
5731 if (p->right)
5732 p->right->parent = p;
5733 r->left = p;
5735 p->balance = 0;
5736 r->balance = 0;
5738 s = p->parent;
5739 p->parent = r;
5740 r->parent = s;
5741 if (s)
5743 if (s->left == p)
5744 s->left = r;
5745 else
5746 s->right = r;
5748 else
5749 constructor_pending_elts = r;
5751 else /* r->balance == -1 */
5753 /* RL rotation */
5754 struct init_node *t = r->left;
5756 r->left = t->right;
5757 if (r->left)
5758 r->left->parent = r;
5759 t->right = r;
5761 p->right = t->left;
5762 if (p->right)
5763 p->right->parent = p;
5764 t->left = p;
5766 r->balance = (t->balance < 0);
5767 p->balance = -(t->balance > 0);
5768 t->balance = 0;
5770 s = p->parent;
5771 p->parent = t;
5772 r->parent = t;
5773 t->parent = s;
5774 if (s)
5776 if (s->left == p)
5777 s->left = t;
5778 else
5779 s->right = t;
5781 else
5782 constructor_pending_elts = t;
5784 break;
5786 else
5788 /* p->balance == -1; growth of right side balances the node. */
5789 p->balance = 0;
5790 break;
5794 r = p;
5795 p = p->parent;
5799 /* Build AVL tree from a sorted chain. */
5801 static void
5802 set_nonincremental_init (void)
5804 unsigned HOST_WIDE_INT ix;
5805 tree index, value;
5807 if (TREE_CODE (constructor_type) != RECORD_TYPE
5808 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5809 return;
5811 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5812 add_pending_init (index, value);
5813 constructor_elements = 0;
5814 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5816 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5817 /* Skip any nameless bit fields at the beginning. */
5818 while (constructor_unfilled_fields != 0
5819 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5820 && DECL_NAME (constructor_unfilled_fields) == 0)
5821 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5824 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5826 if (TYPE_DOMAIN (constructor_type))
5827 constructor_unfilled_index
5828 = convert (bitsizetype,
5829 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5830 else
5831 constructor_unfilled_index = bitsize_zero_node;
5833 constructor_incremental = 0;
5836 /* Build AVL tree from a string constant. */
5838 static void
5839 set_nonincremental_init_from_string (tree str)
5841 tree value, purpose, type;
5842 HOST_WIDE_INT val[2];
5843 const char *p, *end;
5844 int byte, wchar_bytes, charwidth, bitpos;
5846 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5848 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5849 == TYPE_PRECISION (char_type_node))
5850 wchar_bytes = 1;
5851 else
5853 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5854 == TYPE_PRECISION (wchar_type_node));
5855 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5857 charwidth = TYPE_PRECISION (char_type_node);
5858 type = TREE_TYPE (constructor_type);
5859 p = TREE_STRING_POINTER (str);
5860 end = p + TREE_STRING_LENGTH (str);
5862 for (purpose = bitsize_zero_node;
5863 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5864 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5866 if (wchar_bytes == 1)
5868 val[1] = (unsigned char) *p++;
5869 val[0] = 0;
5871 else
5873 val[0] = 0;
5874 val[1] = 0;
5875 for (byte = 0; byte < wchar_bytes; byte++)
5877 if (BYTES_BIG_ENDIAN)
5878 bitpos = (wchar_bytes - byte - 1) * charwidth;
5879 else
5880 bitpos = byte * charwidth;
5881 val[bitpos < HOST_BITS_PER_WIDE_INT]
5882 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5883 << (bitpos % HOST_BITS_PER_WIDE_INT);
5887 if (!TYPE_UNSIGNED (type))
5889 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5890 if (bitpos < HOST_BITS_PER_WIDE_INT)
5892 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5894 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5895 val[0] = -1;
5898 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5900 if (val[1] < 0)
5901 val[0] = -1;
5903 else if (val[0] & (((HOST_WIDE_INT) 1)
5904 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5905 val[0] |= ((HOST_WIDE_INT) -1)
5906 << (bitpos - HOST_BITS_PER_WIDE_INT);
5909 value = build_int_cst_wide (type, val[1], val[0]);
5910 add_pending_init (purpose, value);
5913 constructor_incremental = 0;
5916 /* Return value of FIELD in pending initializer or zero if the field was
5917 not initialized yet. */
5919 static tree
5920 find_init_member (tree field)
5922 struct init_node *p;
5924 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5926 if (constructor_incremental
5927 && tree_int_cst_lt (field, constructor_unfilled_index))
5928 set_nonincremental_init ();
5930 p = constructor_pending_elts;
5931 while (p)
5933 if (tree_int_cst_lt (field, p->purpose))
5934 p = p->left;
5935 else if (tree_int_cst_lt (p->purpose, field))
5936 p = p->right;
5937 else
5938 return p->value;
5941 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5943 tree bitpos = bit_position (field);
5945 if (constructor_incremental
5946 && (!constructor_unfilled_fields
5947 || tree_int_cst_lt (bitpos,
5948 bit_position (constructor_unfilled_fields))))
5949 set_nonincremental_init ();
5951 p = constructor_pending_elts;
5952 while (p)
5954 if (field == p->purpose)
5955 return p->value;
5956 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5957 p = p->left;
5958 else
5959 p = p->right;
5962 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5964 if (!VEC_empty (constructor_elt, constructor_elements)
5965 && (VEC_last (constructor_elt, constructor_elements)->index
5966 == field))
5967 return VEC_last (constructor_elt, constructor_elements)->value;
5969 return 0;
5972 /* "Output" the next constructor element.
5973 At top level, really output it to assembler code now.
5974 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5975 TYPE is the data type that the containing data type wants here.
5976 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5977 If VALUE is a string constant, STRICT_STRING is true if it is
5978 unparenthesized or we should not warn here for it being parenthesized.
5979 For other types of VALUE, STRICT_STRING is not used.
5981 PENDING if non-nil means output pending elements that belong
5982 right after this element. (PENDING is normally 1;
5983 it is 0 while outputting pending elements, to avoid recursion.) */
5985 static void
5986 output_init_element (tree value, bool strict_string, tree type, tree field,
5987 int pending)
5989 constructor_elt *celt;
5991 if (type == error_mark_node || value == error_mark_node)
5993 constructor_erroneous = 1;
5994 return;
5996 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5997 && (TREE_CODE (value) == STRING_CST
5998 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
5999 && !(TREE_CODE (value) == STRING_CST
6000 && TREE_CODE (type) == ARRAY_TYPE
6001 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6002 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6003 TYPE_MAIN_VARIANT (type)))
6004 value = array_to_pointer_conversion (value);
6006 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6007 && require_constant_value && !flag_isoc99 && pending)
6009 /* As an extension, allow initializing objects with static storage
6010 duration with compound literals (which are then treated just as
6011 the brace enclosed list they contain). */
6012 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6013 value = DECL_INITIAL (decl);
6016 if (value == error_mark_node)
6017 constructor_erroneous = 1;
6018 else if (!TREE_CONSTANT (value))
6019 constructor_constant = 0;
6020 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6021 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6022 || TREE_CODE (constructor_type) == UNION_TYPE)
6023 && DECL_C_BIT_FIELD (field)
6024 && TREE_CODE (value) != INTEGER_CST))
6025 constructor_simple = 0;
6027 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6029 if (require_constant_value)
6031 error_init ("initializer element is not constant");
6032 value = error_mark_node;
6034 else if (require_constant_elements)
6035 pedwarn ("initializer element is not computable at load time");
6038 /* If this field is empty (and not at the end of structure),
6039 don't do anything other than checking the initializer. */
6040 if (field
6041 && (TREE_TYPE (field) == error_mark_node
6042 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6043 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6044 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6045 || TREE_CHAIN (field)))))
6046 return;
6048 value = digest_init (type, value, strict_string, require_constant_value);
6049 if (value == error_mark_node)
6051 constructor_erroneous = 1;
6052 return;
6055 /* If this element doesn't come next in sequence,
6056 put it on constructor_pending_elts. */
6057 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6058 && (!constructor_incremental
6059 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6061 if (constructor_incremental
6062 && tree_int_cst_lt (field, constructor_unfilled_index))
6063 set_nonincremental_init ();
6065 add_pending_init (field, value);
6066 return;
6068 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6069 && (!constructor_incremental
6070 || field != constructor_unfilled_fields))
6072 /* We do this for records but not for unions. In a union,
6073 no matter which field is specified, it can be initialized
6074 right away since it starts at the beginning of the union. */
6075 if (constructor_incremental)
6077 if (!constructor_unfilled_fields)
6078 set_nonincremental_init ();
6079 else
6081 tree bitpos, unfillpos;
6083 bitpos = bit_position (field);
6084 unfillpos = bit_position (constructor_unfilled_fields);
6086 if (tree_int_cst_lt (bitpos, unfillpos))
6087 set_nonincremental_init ();
6091 add_pending_init (field, value);
6092 return;
6094 else if (TREE_CODE (constructor_type) == UNION_TYPE
6095 && !VEC_empty (constructor_elt, constructor_elements))
6097 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6098 constructor_elements)->value))
6099 warning_init ("initialized field with side-effects overwritten");
6101 /* We can have just one union field set. */
6102 constructor_elements = 0;
6105 /* Otherwise, output this element either to
6106 constructor_elements or to the assembler file. */
6108 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6109 celt->index = field;
6110 celt->value = value;
6112 /* Advance the variable that indicates sequential elements output. */
6113 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6114 constructor_unfilled_index
6115 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6116 bitsize_one_node);
6117 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6119 constructor_unfilled_fields
6120 = TREE_CHAIN (constructor_unfilled_fields);
6122 /* Skip any nameless bit fields. */
6123 while (constructor_unfilled_fields != 0
6124 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6125 && DECL_NAME (constructor_unfilled_fields) == 0)
6126 constructor_unfilled_fields =
6127 TREE_CHAIN (constructor_unfilled_fields);
6129 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6130 constructor_unfilled_fields = 0;
6132 /* Now output any pending elements which have become next. */
6133 if (pending)
6134 output_pending_init_elements (0);
6137 /* Output any pending elements which have become next.
6138 As we output elements, constructor_unfilled_{fields,index}
6139 advances, which may cause other elements to become next;
6140 if so, they too are output.
6142 If ALL is 0, we return when there are
6143 no more pending elements to output now.
6145 If ALL is 1, we output space as necessary so that
6146 we can output all the pending elements. */
6148 static void
6149 output_pending_init_elements (int all)
6151 struct init_node *elt = constructor_pending_elts;
6152 tree next;
6154 retry:
6156 /* Look through the whole pending tree.
6157 If we find an element that should be output now,
6158 output it. Otherwise, set NEXT to the element
6159 that comes first among those still pending. */
6161 next = 0;
6162 while (elt)
6164 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6166 if (tree_int_cst_equal (elt->purpose,
6167 constructor_unfilled_index))
6168 output_init_element (elt->value, true,
6169 TREE_TYPE (constructor_type),
6170 constructor_unfilled_index, 0);
6171 else if (tree_int_cst_lt (constructor_unfilled_index,
6172 elt->purpose))
6174 /* Advance to the next smaller node. */
6175 if (elt->left)
6176 elt = elt->left;
6177 else
6179 /* We have reached the smallest node bigger than the
6180 current unfilled index. Fill the space first. */
6181 next = elt->purpose;
6182 break;
6185 else
6187 /* Advance to the next bigger node. */
6188 if (elt->right)
6189 elt = elt->right;
6190 else
6192 /* We have reached the biggest node in a subtree. Find
6193 the parent of it, which is the next bigger node. */
6194 while (elt->parent && elt->parent->right == elt)
6195 elt = elt->parent;
6196 elt = elt->parent;
6197 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6198 elt->purpose))
6200 next = elt->purpose;
6201 break;
6206 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6207 || TREE_CODE (constructor_type) == UNION_TYPE)
6209 tree ctor_unfilled_bitpos, elt_bitpos;
6211 /* If the current record is complete we are done. */
6212 if (constructor_unfilled_fields == 0)
6213 break;
6215 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6216 elt_bitpos = bit_position (elt->purpose);
6217 /* We can't compare fields here because there might be empty
6218 fields in between. */
6219 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6221 constructor_unfilled_fields = elt->purpose;
6222 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6223 elt->purpose, 0);
6225 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6227 /* Advance to the next smaller node. */
6228 if (elt->left)
6229 elt = elt->left;
6230 else
6232 /* We have reached the smallest node bigger than the
6233 current unfilled field. Fill the space first. */
6234 next = elt->purpose;
6235 break;
6238 else
6240 /* Advance to the next bigger node. */
6241 if (elt->right)
6242 elt = elt->right;
6243 else
6245 /* We have reached the biggest node in a subtree. Find
6246 the parent of it, which is the next bigger node. */
6247 while (elt->parent && elt->parent->right == elt)
6248 elt = elt->parent;
6249 elt = elt->parent;
6250 if (elt
6251 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6252 bit_position (elt->purpose))))
6254 next = elt->purpose;
6255 break;
6262 /* Ordinarily return, but not if we want to output all
6263 and there are elements left. */
6264 if (!(all && next != 0))
6265 return;
6267 /* If it's not incremental, just skip over the gap, so that after
6268 jumping to retry we will output the next successive element. */
6269 if (TREE_CODE (constructor_type) == RECORD_TYPE
6270 || TREE_CODE (constructor_type) == UNION_TYPE)
6271 constructor_unfilled_fields = next;
6272 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6273 constructor_unfilled_index = next;
6275 /* ELT now points to the node in the pending tree with the next
6276 initializer to output. */
6277 goto retry;
6280 /* Add one non-braced element to the current constructor level.
6281 This adjusts the current position within the constructor's type.
6282 This may also start or terminate implicit levels
6283 to handle a partly-braced initializer.
6285 Once this has found the correct level for the new element,
6286 it calls output_init_element. */
6288 void
6289 process_init_element (struct c_expr value)
6291 tree orig_value = value.value;
6292 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6293 bool strict_string = value.original_code == STRING_CST;
6295 designator_depth = 0;
6296 designator_erroneous = 0;
6298 /* Handle superfluous braces around string cst as in
6299 char x[] = {"foo"}; */
6300 if (string_flag
6301 && constructor_type
6302 && TREE_CODE (constructor_type) == ARRAY_TYPE
6303 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6304 && integer_zerop (constructor_unfilled_index))
6306 if (constructor_stack->replacement_value.value)
6307 error_init ("excess elements in char array initializer");
6308 constructor_stack->replacement_value = value;
6309 return;
6312 if (constructor_stack->replacement_value.value != 0)
6314 error_init ("excess elements in struct initializer");
6315 return;
6318 /* Ignore elements of a brace group if it is entirely superfluous
6319 and has already been diagnosed. */
6320 if (constructor_type == 0)
6321 return;
6323 /* If we've exhausted any levels that didn't have braces,
6324 pop them now. */
6325 while (constructor_stack->implicit)
6327 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6328 || TREE_CODE (constructor_type) == UNION_TYPE)
6329 && constructor_fields == 0)
6330 process_init_element (pop_init_level (1));
6331 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6332 && (constructor_max_index == 0
6333 || tree_int_cst_lt (constructor_max_index,
6334 constructor_index)))
6335 process_init_element (pop_init_level (1));
6336 else
6337 break;
6340 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6341 if (constructor_range_stack)
6343 /* If value is a compound literal and we'll be just using its
6344 content, don't put it into a SAVE_EXPR. */
6345 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6346 || !require_constant_value
6347 || flag_isoc99)
6348 value.value = save_expr (value.value);
6351 while (1)
6353 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6355 tree fieldtype;
6356 enum tree_code fieldcode;
6358 if (constructor_fields == 0)
6360 pedwarn_init ("excess elements in struct initializer");
6361 break;
6364 fieldtype = TREE_TYPE (constructor_fields);
6365 if (fieldtype != error_mark_node)
6366 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6367 fieldcode = TREE_CODE (fieldtype);
6369 /* Error for non-static initialization of a flexible array member. */
6370 if (fieldcode == ARRAY_TYPE
6371 && !require_constant_value
6372 && TYPE_SIZE (fieldtype) == NULL_TREE
6373 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6375 error_init ("non-static initialization of a flexible array member");
6376 break;
6379 /* Accept a string constant to initialize a subarray. */
6380 if (value.value != 0
6381 && fieldcode == ARRAY_TYPE
6382 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6383 && string_flag)
6384 value.value = orig_value;
6385 /* Otherwise, if we have come to a subaggregate,
6386 and we don't have an element of its type, push into it. */
6387 else if (value.value != 0
6388 && value.value != error_mark_node
6389 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6390 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6391 || fieldcode == UNION_TYPE))
6393 push_init_level (1);
6394 continue;
6397 if (value.value)
6399 push_member_name (constructor_fields);
6400 output_init_element (value.value, strict_string,
6401 fieldtype, constructor_fields, 1);
6402 RESTORE_SPELLING_DEPTH (constructor_depth);
6404 else
6405 /* Do the bookkeeping for an element that was
6406 directly output as a constructor. */
6408 /* For a record, keep track of end position of last field. */
6409 if (DECL_SIZE (constructor_fields))
6410 constructor_bit_index
6411 = size_binop (PLUS_EXPR,
6412 bit_position (constructor_fields),
6413 DECL_SIZE (constructor_fields));
6415 /* If the current field was the first one not yet written out,
6416 it isn't now, so update. */
6417 if (constructor_unfilled_fields == constructor_fields)
6419 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6420 /* Skip any nameless bit fields. */
6421 while (constructor_unfilled_fields != 0
6422 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6423 && DECL_NAME (constructor_unfilled_fields) == 0)
6424 constructor_unfilled_fields =
6425 TREE_CHAIN (constructor_unfilled_fields);
6429 constructor_fields = TREE_CHAIN (constructor_fields);
6430 /* Skip any nameless bit fields at the beginning. */
6431 while (constructor_fields != 0
6432 && DECL_C_BIT_FIELD (constructor_fields)
6433 && DECL_NAME (constructor_fields) == 0)
6434 constructor_fields = TREE_CHAIN (constructor_fields);
6436 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6438 tree fieldtype;
6439 enum tree_code fieldcode;
6441 if (constructor_fields == 0)
6443 pedwarn_init ("excess elements in union initializer");
6444 break;
6447 fieldtype = TREE_TYPE (constructor_fields);
6448 if (fieldtype != error_mark_node)
6449 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6450 fieldcode = TREE_CODE (fieldtype);
6452 /* Warn that traditional C rejects initialization of unions.
6453 We skip the warning if the value is zero. This is done
6454 under the assumption that the zero initializer in user
6455 code appears conditioned on e.g. __STDC__ to avoid
6456 "missing initializer" warnings and relies on default
6457 initialization to zero in the traditional C case.
6458 We also skip the warning if the initializer is designated,
6459 again on the assumption that this must be conditional on
6460 __STDC__ anyway (and we've already complained about the
6461 member-designator already). */
6462 if (!in_system_header && !constructor_designated
6463 && !(value.value && (integer_zerop (value.value)
6464 || real_zerop (value.value))))
6465 warning (OPT_Wtraditional, "traditional C rejects initialization "
6466 "of unions");
6468 /* Accept a string constant to initialize a subarray. */
6469 if (value.value != 0
6470 && fieldcode == ARRAY_TYPE
6471 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6472 && string_flag)
6473 value.value = orig_value;
6474 /* Otherwise, if we have come to a subaggregate,
6475 and we don't have an element of its type, push into it. */
6476 else if (value.value != 0
6477 && value.value != error_mark_node
6478 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6479 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6480 || fieldcode == UNION_TYPE))
6482 push_init_level (1);
6483 continue;
6486 if (value.value)
6488 push_member_name (constructor_fields);
6489 output_init_element (value.value, strict_string,
6490 fieldtype, constructor_fields, 1);
6491 RESTORE_SPELLING_DEPTH (constructor_depth);
6493 else
6494 /* Do the bookkeeping for an element that was
6495 directly output as a constructor. */
6497 constructor_bit_index = DECL_SIZE (constructor_fields);
6498 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6501 constructor_fields = 0;
6503 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6505 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6506 enum tree_code eltcode = TREE_CODE (elttype);
6508 /* Accept a string constant to initialize a subarray. */
6509 if (value.value != 0
6510 && eltcode == ARRAY_TYPE
6511 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6512 && string_flag)
6513 value.value = orig_value;
6514 /* Otherwise, if we have come to a subaggregate,
6515 and we don't have an element of its type, push into it. */
6516 else if (value.value != 0
6517 && value.value != error_mark_node
6518 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6519 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6520 || eltcode == UNION_TYPE))
6522 push_init_level (1);
6523 continue;
6526 if (constructor_max_index != 0
6527 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6528 || integer_all_onesp (constructor_max_index)))
6530 pedwarn_init ("excess elements in array initializer");
6531 break;
6534 /* Now output the actual element. */
6535 if (value.value)
6537 push_array_bounds (tree_low_cst (constructor_index, 0));
6538 output_init_element (value.value, strict_string,
6539 elttype, constructor_index, 1);
6540 RESTORE_SPELLING_DEPTH (constructor_depth);
6543 constructor_index
6544 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6546 if (!value.value)
6547 /* If we are doing the bookkeeping for an element that was
6548 directly output as a constructor, we must update
6549 constructor_unfilled_index. */
6550 constructor_unfilled_index = constructor_index;
6552 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6554 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6556 /* Do a basic check of initializer size. Note that vectors
6557 always have a fixed size derived from their type. */
6558 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6560 pedwarn_init ("excess elements in vector initializer");
6561 break;
6564 /* Now output the actual element. */
6565 if (value.value)
6566 output_init_element (value.value, strict_string,
6567 elttype, constructor_index, 1);
6569 constructor_index
6570 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6572 if (!value.value)
6573 /* If we are doing the bookkeeping for an element that was
6574 directly output as a constructor, we must update
6575 constructor_unfilled_index. */
6576 constructor_unfilled_index = constructor_index;
6579 /* Handle the sole element allowed in a braced initializer
6580 for a scalar variable. */
6581 else if (constructor_type != error_mark_node
6582 && constructor_fields == 0)
6584 pedwarn_init ("excess elements in scalar initializer");
6585 break;
6587 else
6589 if (value.value)
6590 output_init_element (value.value, strict_string,
6591 constructor_type, NULL_TREE, 1);
6592 constructor_fields = 0;
6595 /* Handle range initializers either at this level or anywhere higher
6596 in the designator stack. */
6597 if (constructor_range_stack)
6599 struct constructor_range_stack *p, *range_stack;
6600 int finish = 0;
6602 range_stack = constructor_range_stack;
6603 constructor_range_stack = 0;
6604 while (constructor_stack != range_stack->stack)
6606 gcc_assert (constructor_stack->implicit);
6607 process_init_element (pop_init_level (1));
6609 for (p = range_stack;
6610 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6611 p = p->prev)
6613 gcc_assert (constructor_stack->implicit);
6614 process_init_element (pop_init_level (1));
6617 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6618 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6619 finish = 1;
6621 while (1)
6623 constructor_index = p->index;
6624 constructor_fields = p->fields;
6625 if (finish && p->range_end && p->index == p->range_start)
6627 finish = 0;
6628 p->prev = 0;
6630 p = p->next;
6631 if (!p)
6632 break;
6633 push_init_level (2);
6634 p->stack = constructor_stack;
6635 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6636 p->index = p->range_start;
6639 if (!finish)
6640 constructor_range_stack = range_stack;
6641 continue;
6644 break;
6647 constructor_range_stack = 0;
6650 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6651 (guaranteed to be 'volatile' or null) and ARGS (represented using
6652 an ASM_EXPR node). */
6653 tree
6654 build_asm_stmt (tree cv_qualifier, tree args)
6656 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6657 ASM_VOLATILE_P (args) = 1;
6658 return add_stmt (args);
6661 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6662 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6663 SIMPLE indicates whether there was anything at all after the
6664 string in the asm expression -- asm("blah") and asm("blah" : )
6665 are subtly different. We use a ASM_EXPR node to represent this. */
6666 tree
6667 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6668 bool simple)
6670 tree tail;
6671 tree args;
6672 int i;
6673 const char *constraint;
6674 const char **oconstraints;
6675 bool allows_mem, allows_reg, is_inout;
6676 int ninputs, noutputs;
6678 ninputs = list_length (inputs);
6679 noutputs = list_length (outputs);
6680 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6682 string = resolve_asm_operand_names (string, outputs, inputs);
6684 /* Remove output conversions that change the type but not the mode. */
6685 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6687 tree output = TREE_VALUE (tail);
6689 /* ??? Really, this should not be here. Users should be using a
6690 proper lvalue, dammit. But there's a long history of using casts
6691 in the output operands. In cases like longlong.h, this becomes a
6692 primitive form of typechecking -- if the cast can be removed, then
6693 the output operand had a type of the proper width; otherwise we'll
6694 get an error. Gross, but ... */
6695 STRIP_NOPS (output);
6697 if (!lvalue_or_else (output, lv_asm))
6698 output = error_mark_node;
6700 if (output != error_mark_node
6701 && (TREE_READONLY (output)
6702 || TYPE_READONLY (TREE_TYPE (output))
6703 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6704 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6705 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6706 readonly_error (output, lv_asm);
6708 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6709 oconstraints[i] = constraint;
6711 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6712 &allows_mem, &allows_reg, &is_inout))
6714 /* If the operand is going to end up in memory,
6715 mark it addressable. */
6716 if (!allows_reg && !c_mark_addressable (output))
6717 output = error_mark_node;
6719 else
6720 output = error_mark_node;
6722 TREE_VALUE (tail) = output;
6725 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6727 tree input;
6729 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6730 input = TREE_VALUE (tail);
6732 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6733 oconstraints, &allows_mem, &allows_reg))
6735 /* If the operand is going to end up in memory,
6736 mark it addressable. */
6737 if (!allows_reg && allows_mem)
6739 /* Strip the nops as we allow this case. FIXME, this really
6740 should be rejected or made deprecated. */
6741 STRIP_NOPS (input);
6742 if (!c_mark_addressable (input))
6743 input = error_mark_node;
6746 else
6747 input = error_mark_node;
6749 TREE_VALUE (tail) = input;
6752 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6754 /* asm statements without outputs, including simple ones, are treated
6755 as volatile. */
6756 ASM_INPUT_P (args) = simple;
6757 ASM_VOLATILE_P (args) = (noutputs == 0);
6759 return args;
6762 /* Generate a goto statement to LABEL. */
6764 tree
6765 c_finish_goto_label (tree label)
6767 tree decl = lookup_label (label);
6768 if (!decl)
6769 return NULL_TREE;
6771 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6773 error ("jump into statement expression");
6774 return NULL_TREE;
6777 if (C_DECL_UNJUMPABLE_VM (decl))
6779 error ("jump into scope of identifier with variably modified type");
6780 return NULL_TREE;
6783 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6785 /* No jump from outside this statement expression context, so
6786 record that there is a jump from within this context. */
6787 struct c_label_list *nlist;
6788 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6789 nlist->next = label_context_stack_se->labels_used;
6790 nlist->label = decl;
6791 label_context_stack_se->labels_used = nlist;
6794 if (!C_DECL_UNDEFINABLE_VM (decl))
6796 /* No jump from outside this context context of identifiers with
6797 variably modified type, so record that there is a jump from
6798 within this context. */
6799 struct c_label_list *nlist;
6800 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6801 nlist->next = label_context_stack_vm->labels_used;
6802 nlist->label = decl;
6803 label_context_stack_vm->labels_used = nlist;
6806 TREE_USED (decl) = 1;
6807 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6810 /* Generate a computed goto statement to EXPR. */
6812 tree
6813 c_finish_goto_ptr (tree expr)
6815 if (pedantic)
6816 pedwarn ("ISO C forbids %<goto *expr;%>");
6817 expr = convert (ptr_type_node, expr);
6818 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6821 /* Generate a C `return' statement. RETVAL is the expression for what
6822 to return, or a null pointer for `return;' with no value. */
6824 tree
6825 c_finish_return (tree retval)
6827 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6828 bool no_warning = false;
6830 if (TREE_THIS_VOLATILE (current_function_decl))
6831 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6833 if (!retval)
6835 current_function_returns_null = 1;
6836 if ((warn_return_type || flag_isoc99)
6837 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6839 pedwarn_c99 ("%<return%> with no value, in "
6840 "function returning non-void");
6841 no_warning = true;
6844 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6846 current_function_returns_null = 1;
6847 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6848 pedwarn ("%<return%> with a value, in function returning void");
6850 else
6852 tree t = convert_for_assignment (valtype, retval, ic_return,
6853 NULL_TREE, NULL_TREE, 0);
6854 tree res = DECL_RESULT (current_function_decl);
6855 tree inner;
6857 current_function_returns_value = 1;
6858 if (t == error_mark_node)
6859 return NULL_TREE;
6861 inner = t = convert (TREE_TYPE (res), t);
6863 /* Strip any conversions, additions, and subtractions, and see if
6864 we are returning the address of a local variable. Warn if so. */
6865 while (1)
6867 switch (TREE_CODE (inner))
6869 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6870 case PLUS_EXPR:
6871 inner = TREE_OPERAND (inner, 0);
6872 continue;
6874 case MINUS_EXPR:
6875 /* If the second operand of the MINUS_EXPR has a pointer
6876 type (or is converted from it), this may be valid, so
6877 don't give a warning. */
6879 tree op1 = TREE_OPERAND (inner, 1);
6881 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6882 && (TREE_CODE (op1) == NOP_EXPR
6883 || TREE_CODE (op1) == NON_LVALUE_EXPR
6884 || TREE_CODE (op1) == CONVERT_EXPR))
6885 op1 = TREE_OPERAND (op1, 0);
6887 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6888 break;
6890 inner = TREE_OPERAND (inner, 0);
6891 continue;
6894 case ADDR_EXPR:
6895 inner = TREE_OPERAND (inner, 0);
6897 while (REFERENCE_CLASS_P (inner)
6898 && TREE_CODE (inner) != INDIRECT_REF)
6899 inner = TREE_OPERAND (inner, 0);
6901 if (DECL_P (inner)
6902 && !DECL_EXTERNAL (inner)
6903 && !TREE_STATIC (inner)
6904 && DECL_CONTEXT (inner) == current_function_decl)
6905 warning (0, "function returns address of local variable");
6906 break;
6908 default:
6909 break;
6912 break;
6915 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6918 ret_stmt = build_stmt (RETURN_EXPR, retval);
6919 TREE_NO_WARNING (ret_stmt) |= no_warning;
6920 return add_stmt (ret_stmt);
6923 struct c_switch {
6924 /* The SWITCH_EXPR being built. */
6925 tree switch_expr;
6927 /* The original type of the testing expression, i.e. before the
6928 default conversion is applied. */
6929 tree orig_type;
6931 /* A splay-tree mapping the low element of a case range to the high
6932 element, or NULL_TREE if there is no high element. Used to
6933 determine whether or not a new case label duplicates an old case
6934 label. We need a tree, rather than simply a hash table, because
6935 of the GNU case range extension. */
6936 splay_tree cases;
6938 /* Number of nested statement expressions within this switch
6939 statement; if nonzero, case and default labels may not
6940 appear. */
6941 unsigned int blocked_stmt_expr;
6943 /* Scope of outermost declarations of identifiers with variably
6944 modified type within this switch statement; if nonzero, case and
6945 default labels may not appear. */
6946 unsigned int blocked_vm;
6948 /* The next node on the stack. */
6949 struct c_switch *next;
6952 /* A stack of the currently active switch statements. The innermost
6953 switch statement is on the top of the stack. There is no need to
6954 mark the stack for garbage collection because it is only active
6955 during the processing of the body of a function, and we never
6956 collect at that point. */
6958 struct c_switch *c_switch_stack;
6960 /* Start a C switch statement, testing expression EXP. Return the new
6961 SWITCH_EXPR. */
6963 tree
6964 c_start_case (tree exp)
6966 enum tree_code code;
6967 tree type, orig_type = error_mark_node;
6968 struct c_switch *cs;
6970 if (exp != error_mark_node)
6972 code = TREE_CODE (TREE_TYPE (exp));
6973 orig_type = TREE_TYPE (exp);
6975 if (!INTEGRAL_TYPE_P (orig_type)
6976 && code != ERROR_MARK)
6978 error ("switch quantity not an integer");
6979 exp = integer_zero_node;
6980 orig_type = error_mark_node;
6982 else
6984 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6986 if (!in_system_header
6987 && (type == long_integer_type_node
6988 || type == long_unsigned_type_node))
6989 warning (OPT_Wtraditional, "%<long%> switch expression not "
6990 "converted to %<int%> in ISO C");
6992 exp = default_conversion (exp);
6993 type = TREE_TYPE (exp);
6997 /* Add this new SWITCH_EXPR to the stack. */
6998 cs = XNEW (struct c_switch);
6999 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7000 cs->orig_type = orig_type;
7001 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7002 cs->blocked_stmt_expr = 0;
7003 cs->blocked_vm = 0;
7004 cs->next = c_switch_stack;
7005 c_switch_stack = cs;
7007 return add_stmt (cs->switch_expr);
7010 /* Process a case label. */
7012 tree
7013 do_case (tree low_value, tree high_value)
7015 tree label = NULL_TREE;
7017 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7018 && !c_switch_stack->blocked_vm)
7020 label = c_add_case_label (c_switch_stack->cases,
7021 SWITCH_COND (c_switch_stack->switch_expr),
7022 c_switch_stack->orig_type,
7023 low_value, high_value);
7024 if (label == error_mark_node)
7025 label = NULL_TREE;
7027 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7029 if (low_value)
7030 error ("case label in statement expression not containing "
7031 "enclosing switch statement");
7032 else
7033 error ("%<default%> label in statement expression not containing "
7034 "enclosing switch statement");
7036 else if (c_switch_stack && c_switch_stack->blocked_vm)
7038 if (low_value)
7039 error ("case label in scope of identifier with variably modified "
7040 "type not containing enclosing switch statement");
7041 else
7042 error ("%<default%> label in scope of identifier with variably "
7043 "modified type not containing enclosing switch statement");
7045 else if (low_value)
7046 error ("case label not within a switch statement");
7047 else
7048 error ("%<default%> label not within a switch statement");
7050 return label;
7053 /* Finish the switch statement. */
7055 void
7056 c_finish_case (tree body)
7058 struct c_switch *cs = c_switch_stack;
7059 location_t switch_location;
7061 SWITCH_BODY (cs->switch_expr) = body;
7063 /* We must not be within a statement expression nested in the switch
7064 at this point; we might, however, be within the scope of an
7065 identifier with variably modified type nested in the switch. */
7066 gcc_assert (!cs->blocked_stmt_expr);
7068 /* Emit warnings as needed. */
7069 if (EXPR_HAS_LOCATION (cs->switch_expr))
7070 switch_location = EXPR_LOCATION (cs->switch_expr);
7071 else
7072 switch_location = input_location;
7073 c_do_switch_warnings (cs->cases, switch_location,
7074 TREE_TYPE (cs->switch_expr),
7075 SWITCH_COND (cs->switch_expr));
7077 /* Pop the stack. */
7078 c_switch_stack = cs->next;
7079 splay_tree_delete (cs->cases);
7080 XDELETE (cs);
7083 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7084 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7085 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7086 statement, and was not surrounded with parenthesis. */
7088 void
7089 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7090 tree else_block, bool nested_if)
7092 tree stmt;
7094 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7095 if (warn_parentheses && nested_if && else_block == NULL)
7097 tree inner_if = then_block;
7099 /* We know from the grammar productions that there is an IF nested
7100 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7101 it might not be exactly THEN_BLOCK, but should be the last
7102 non-container statement within. */
7103 while (1)
7104 switch (TREE_CODE (inner_if))
7106 case COND_EXPR:
7107 goto found;
7108 case BIND_EXPR:
7109 inner_if = BIND_EXPR_BODY (inner_if);
7110 break;
7111 case STATEMENT_LIST:
7112 inner_if = expr_last (then_block);
7113 break;
7114 case TRY_FINALLY_EXPR:
7115 case TRY_CATCH_EXPR:
7116 inner_if = TREE_OPERAND (inner_if, 0);
7117 break;
7118 default:
7119 gcc_unreachable ();
7121 found:
7123 if (COND_EXPR_ELSE (inner_if))
7124 warning (OPT_Wparentheses,
7125 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7126 &if_locus);
7129 /* Diagnose ";" via the special empty statement node that we create. */
7130 if (extra_warnings)
7132 tree *inner_then = &then_block, *inner_else = &else_block;
7134 if (TREE_CODE (*inner_then) == STATEMENT_LIST
7135 && STATEMENT_LIST_TAIL (*inner_then))
7136 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
7137 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
7138 && STATEMENT_LIST_TAIL (*inner_else))
7139 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
7141 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
7143 if (!*inner_else)
7144 warning (0, "%Hempty body in an if-statement",
7145 EXPR_LOCUS (*inner_then));
7147 *inner_then = alloc_stmt_list ();
7149 if (*inner_else
7150 && TREE_CODE (*inner_else) == NOP_EXPR
7151 && !TREE_TYPE (*inner_else))
7153 warning (0, "%Hempty body in an else-statement",
7154 EXPR_LOCUS (*inner_else));
7156 *inner_else = alloc_stmt_list ();
7160 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7161 SET_EXPR_LOCATION (stmt, if_locus);
7162 add_stmt (stmt);
7165 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7166 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7167 is false for DO loops. INCR is the FOR increment expression. BODY is
7168 the statement controlled by the loop. BLAB is the break label. CLAB is
7169 the continue label. Everything is allowed to be NULL. */
7171 void
7172 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7173 tree blab, tree clab, bool cond_is_first)
7175 tree entry = NULL, exit = NULL, t;
7177 /* If the condition is zero don't generate a loop construct. */
7178 if (cond && integer_zerop (cond))
7180 if (cond_is_first)
7182 t = build_and_jump (&blab);
7183 SET_EXPR_LOCATION (t, start_locus);
7184 add_stmt (t);
7187 else
7189 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7191 /* If we have an exit condition, then we build an IF with gotos either
7192 out of the loop, or to the top of it. If there's no exit condition,
7193 then we just build a jump back to the top. */
7194 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7196 if (cond && !integer_nonzerop (cond))
7198 /* Canonicalize the loop condition to the end. This means
7199 generating a branch to the loop condition. Reuse the
7200 continue label, if possible. */
7201 if (cond_is_first)
7203 if (incr || !clab)
7205 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7206 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7208 else
7209 t = build1 (GOTO_EXPR, void_type_node, clab);
7210 SET_EXPR_LOCATION (t, start_locus);
7211 add_stmt (t);
7214 t = build_and_jump (&blab);
7215 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7216 if (cond_is_first)
7217 SET_EXPR_LOCATION (exit, start_locus);
7218 else
7219 SET_EXPR_LOCATION (exit, input_location);
7222 add_stmt (top);
7225 if (body)
7226 add_stmt (body);
7227 if (clab)
7228 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7229 if (incr)
7230 add_stmt (incr);
7231 if (entry)
7232 add_stmt (entry);
7233 if (exit)
7234 add_stmt (exit);
7235 if (blab)
7236 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7239 tree
7240 c_finish_bc_stmt (tree *label_p, bool is_break)
7242 bool skip;
7243 tree label = *label_p;
7245 /* In switch statements break is sometimes stylistically used after
7246 a return statement. This can lead to spurious warnings about
7247 control reaching the end of a non-void function when it is
7248 inlined. Note that we are calling block_may_fallthru with
7249 language specific tree nodes; this works because
7250 block_may_fallthru returns true when given something it does not
7251 understand. */
7252 skip = !block_may_fallthru (cur_stmt_list);
7254 if (!label)
7256 if (!skip)
7257 *label_p = label = create_artificial_label ();
7259 else if (TREE_CODE (label) != LABEL_DECL)
7261 if (is_break)
7262 error ("break statement not within loop or switch");
7263 else
7264 error ("continue statement not within a loop");
7265 return NULL_TREE;
7268 if (skip)
7269 return NULL_TREE;
7271 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7274 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7276 static void
7277 emit_side_effect_warnings (tree expr)
7279 if (expr == error_mark_node)
7281 else if (!TREE_SIDE_EFFECTS (expr))
7283 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7284 warning (0, "%Hstatement with no effect",
7285 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7287 else if (warn_unused_value)
7288 warn_if_unused_value (expr, input_location);
7291 /* Process an expression as if it were a complete statement. Emit
7292 diagnostics, but do not call ADD_STMT. */
7294 tree
7295 c_process_expr_stmt (tree expr)
7297 if (!expr)
7298 return NULL_TREE;
7300 if (warn_sequence_point)
7301 verify_sequence_points (expr);
7303 if (TREE_TYPE (expr) != error_mark_node
7304 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7305 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7306 error ("expression statement has incomplete type");
7308 /* If we're not processing a statement expression, warn about unused values.
7309 Warnings for statement expressions will be emitted later, once we figure
7310 out which is the result. */
7311 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7312 && (extra_warnings || warn_unused_value))
7313 emit_side_effect_warnings (expr);
7315 /* If the expression is not of a type to which we cannot assign a line
7316 number, wrap the thing in a no-op NOP_EXPR. */
7317 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7318 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7320 if (EXPR_P (expr))
7321 SET_EXPR_LOCATION (expr, input_location);
7323 return expr;
7326 /* Emit an expression as a statement. */
7328 tree
7329 c_finish_expr_stmt (tree expr)
7331 if (expr)
7332 return add_stmt (c_process_expr_stmt (expr));
7333 else
7334 return NULL;
7337 /* Do the opposite and emit a statement as an expression. To begin,
7338 create a new binding level and return it. */
7340 tree
7341 c_begin_stmt_expr (void)
7343 tree ret;
7344 struct c_label_context_se *nstack;
7345 struct c_label_list *glist;
7347 /* We must force a BLOCK for this level so that, if it is not expanded
7348 later, there is a way to turn off the entire subtree of blocks that
7349 are contained in it. */
7350 keep_next_level ();
7351 ret = c_begin_compound_stmt (true);
7352 if (c_switch_stack)
7354 c_switch_stack->blocked_stmt_expr++;
7355 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7357 for (glist = label_context_stack_se->labels_used;
7358 glist != NULL;
7359 glist = glist->next)
7361 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7363 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7364 nstack->labels_def = NULL;
7365 nstack->labels_used = NULL;
7366 nstack->next = label_context_stack_se;
7367 label_context_stack_se = nstack;
7369 /* Mark the current statement list as belonging to a statement list. */
7370 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7372 return ret;
7375 tree
7376 c_finish_stmt_expr (tree body)
7378 tree last, type, tmp, val;
7379 tree *last_p;
7380 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7382 body = c_end_compound_stmt (body, true);
7383 if (c_switch_stack)
7385 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7386 c_switch_stack->blocked_stmt_expr--;
7388 /* It is no longer possible to jump to labels defined within this
7389 statement expression. */
7390 for (dlist = label_context_stack_se->labels_def;
7391 dlist != NULL;
7392 dlist = dlist->next)
7394 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7396 /* It is again possible to define labels with a goto just outside
7397 this statement expression. */
7398 for (glist = label_context_stack_se->next->labels_used;
7399 glist != NULL;
7400 glist = glist->next)
7402 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7403 glist_prev = glist;
7405 if (glist_prev != NULL)
7406 glist_prev->next = label_context_stack_se->labels_used;
7407 else
7408 label_context_stack_se->next->labels_used
7409 = label_context_stack_se->labels_used;
7410 label_context_stack_se = label_context_stack_se->next;
7412 /* Locate the last statement in BODY. See c_end_compound_stmt
7413 about always returning a BIND_EXPR. */
7414 last_p = &BIND_EXPR_BODY (body);
7415 last = BIND_EXPR_BODY (body);
7417 continue_searching:
7418 if (TREE_CODE (last) == STATEMENT_LIST)
7420 tree_stmt_iterator i;
7422 /* This can happen with degenerate cases like ({ }). No value. */
7423 if (!TREE_SIDE_EFFECTS (last))
7424 return body;
7426 /* If we're supposed to generate side effects warnings, process
7427 all of the statements except the last. */
7428 if (extra_warnings || warn_unused_value)
7430 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7431 emit_side_effect_warnings (tsi_stmt (i));
7433 else
7434 i = tsi_last (last);
7435 last_p = tsi_stmt_ptr (i);
7436 last = *last_p;
7439 /* If the end of the list is exception related, then the list was split
7440 by a call to push_cleanup. Continue searching. */
7441 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7442 || TREE_CODE (last) == TRY_CATCH_EXPR)
7444 last_p = &TREE_OPERAND (last, 0);
7445 last = *last_p;
7446 goto continue_searching;
7449 /* In the case that the BIND_EXPR is not necessary, return the
7450 expression out from inside it. */
7451 if (last == error_mark_node
7452 || (last == BIND_EXPR_BODY (body)
7453 && BIND_EXPR_VARS (body) == NULL))
7455 /* Do not warn if the return value of a statement expression is
7456 unused. */
7457 if (EXPR_P (last))
7458 TREE_NO_WARNING (last) = 1;
7459 return last;
7462 /* Extract the type of said expression. */
7463 type = TREE_TYPE (last);
7465 /* If we're not returning a value at all, then the BIND_EXPR that
7466 we already have is a fine expression to return. */
7467 if (!type || VOID_TYPE_P (type))
7468 return body;
7470 /* Now that we've located the expression containing the value, it seems
7471 silly to make voidify_wrapper_expr repeat the process. Create a
7472 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7473 tmp = create_tmp_var_raw (type, NULL);
7475 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7476 tree_expr_nonnegative_p giving up immediately. */
7477 val = last;
7478 if (TREE_CODE (val) == NOP_EXPR
7479 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7480 val = TREE_OPERAND (val, 0);
7482 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7483 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7485 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7488 /* Begin the scope of an identifier of variably modified type, scope
7489 number SCOPE. Jumping from outside this scope to inside it is not
7490 permitted. */
7492 void
7493 c_begin_vm_scope (unsigned int scope)
7495 struct c_label_context_vm *nstack;
7496 struct c_label_list *glist;
7498 gcc_assert (scope > 0);
7499 if (c_switch_stack && !c_switch_stack->blocked_vm)
7500 c_switch_stack->blocked_vm = scope;
7501 for (glist = label_context_stack_vm->labels_used;
7502 glist != NULL;
7503 glist = glist->next)
7505 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7507 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7508 nstack->labels_def = NULL;
7509 nstack->labels_used = NULL;
7510 nstack->scope = scope;
7511 nstack->next = label_context_stack_vm;
7512 label_context_stack_vm = nstack;
7515 /* End a scope which may contain identifiers of variably modified
7516 type, scope number SCOPE. */
7518 void
7519 c_end_vm_scope (unsigned int scope)
7521 if (label_context_stack_vm == NULL)
7522 return;
7523 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7524 c_switch_stack->blocked_vm = 0;
7525 /* We may have a number of nested scopes of identifiers with
7526 variably modified type, all at this depth. Pop each in turn. */
7527 while (label_context_stack_vm->scope == scope)
7529 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7531 /* It is no longer possible to jump to labels defined within this
7532 scope. */
7533 for (dlist = label_context_stack_vm->labels_def;
7534 dlist != NULL;
7535 dlist = dlist->next)
7537 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7539 /* It is again possible to define labels with a goto just outside
7540 this scope. */
7541 for (glist = label_context_stack_vm->next->labels_used;
7542 glist != NULL;
7543 glist = glist->next)
7545 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7546 glist_prev = glist;
7548 if (glist_prev != NULL)
7549 glist_prev->next = label_context_stack_vm->labels_used;
7550 else
7551 label_context_stack_vm->next->labels_used
7552 = label_context_stack_vm->labels_used;
7553 label_context_stack_vm = label_context_stack_vm->next;
7557 /* Begin and end compound statements. This is as simple as pushing
7558 and popping new statement lists from the tree. */
7560 tree
7561 c_begin_compound_stmt (bool do_scope)
7563 tree stmt = push_stmt_list ();
7564 if (do_scope)
7565 push_scope ();
7566 return stmt;
7569 tree
7570 c_end_compound_stmt (tree stmt, bool do_scope)
7572 tree block = NULL;
7574 if (do_scope)
7576 if (c_dialect_objc ())
7577 objc_clear_super_receiver ();
7578 block = pop_scope ();
7581 stmt = pop_stmt_list (stmt);
7582 stmt = c_build_bind_expr (block, stmt);
7584 /* If this compound statement is nested immediately inside a statement
7585 expression, then force a BIND_EXPR to be created. Otherwise we'll
7586 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7587 STATEMENT_LISTs merge, and thus we can lose track of what statement
7588 was really last. */
7589 if (cur_stmt_list
7590 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7591 && TREE_CODE (stmt) != BIND_EXPR)
7593 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7594 TREE_SIDE_EFFECTS (stmt) = 1;
7597 return stmt;
7600 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7601 when the current scope is exited. EH_ONLY is true when this is not
7602 meant to apply to normal control flow transfer. */
7604 void
7605 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7607 enum tree_code code;
7608 tree stmt, list;
7609 bool stmt_expr;
7611 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7612 stmt = build_stmt (code, NULL, cleanup);
7613 add_stmt (stmt);
7614 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7615 list = push_stmt_list ();
7616 TREE_OPERAND (stmt, 0) = list;
7617 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7620 /* Build a binary-operation expression without default conversions.
7621 CODE is the kind of expression to build.
7622 This function differs from `build' in several ways:
7623 the data type of the result is computed and recorded in it,
7624 warnings are generated if arg data types are invalid,
7625 special handling for addition and subtraction of pointers is known,
7626 and some optimization is done (operations on narrow ints
7627 are done in the narrower type when that gives the same result).
7628 Constant folding is also done before the result is returned.
7630 Note that the operands will never have enumeral types, or function
7631 or array types, because either they will have the default conversions
7632 performed or they have both just been converted to some other type in which
7633 the arithmetic is to be done. */
7635 tree
7636 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7637 int convert_p)
7639 tree type0, type1;
7640 enum tree_code code0, code1;
7641 tree op0, op1;
7642 const char *invalid_op_diag;
7644 /* Expression code to give to the expression when it is built.
7645 Normally this is CODE, which is what the caller asked for,
7646 but in some special cases we change it. */
7647 enum tree_code resultcode = code;
7649 /* Data type in which the computation is to be performed.
7650 In the simplest cases this is the common type of the arguments. */
7651 tree result_type = NULL;
7653 /* Nonzero means operands have already been type-converted
7654 in whatever way is necessary.
7655 Zero means they need to be converted to RESULT_TYPE. */
7656 int converted = 0;
7658 /* Nonzero means create the expression with this type, rather than
7659 RESULT_TYPE. */
7660 tree build_type = 0;
7662 /* Nonzero means after finally constructing the expression
7663 convert it to this type. */
7664 tree final_type = 0;
7666 /* Nonzero if this is an operation like MIN or MAX which can
7667 safely be computed in short if both args are promoted shorts.
7668 Also implies COMMON.
7669 -1 indicates a bitwise operation; this makes a difference
7670 in the exact conditions for when it is safe to do the operation
7671 in a narrower mode. */
7672 int shorten = 0;
7674 /* Nonzero if this is a comparison operation;
7675 if both args are promoted shorts, compare the original shorts.
7676 Also implies COMMON. */
7677 int short_compare = 0;
7679 /* Nonzero if this is a right-shift operation, which can be computed on the
7680 original short and then promoted if the operand is a promoted short. */
7681 int short_shift = 0;
7683 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7684 int common = 0;
7686 /* True means types are compatible as far as ObjC is concerned. */
7687 bool objc_ok;
7689 if (convert_p)
7691 op0 = default_conversion (orig_op0);
7692 op1 = default_conversion (orig_op1);
7694 else
7696 op0 = orig_op0;
7697 op1 = orig_op1;
7700 type0 = TREE_TYPE (op0);
7701 type1 = TREE_TYPE (op1);
7703 /* The expression codes of the data types of the arguments tell us
7704 whether the arguments are integers, floating, pointers, etc. */
7705 code0 = TREE_CODE (type0);
7706 code1 = TREE_CODE (type1);
7708 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7709 STRIP_TYPE_NOPS (op0);
7710 STRIP_TYPE_NOPS (op1);
7712 /* If an error was already reported for one of the arguments,
7713 avoid reporting another error. */
7715 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7716 return error_mark_node;
7718 if ((invalid_op_diag
7719 = targetm.invalid_binary_op (code, type0, type1)))
7721 error (invalid_op_diag);
7722 return error_mark_node;
7725 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7727 switch (code)
7729 case PLUS_EXPR:
7730 /* Handle the pointer + int case. */
7731 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7732 return pointer_int_sum (PLUS_EXPR, op0, op1);
7733 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7734 return pointer_int_sum (PLUS_EXPR, op1, op0);
7735 else
7736 common = 1;
7737 break;
7739 case MINUS_EXPR:
7740 /* Subtraction of two similar pointers.
7741 We must subtract them as integers, then divide by object size. */
7742 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7743 && comp_target_types (type0, type1))
7744 return pointer_diff (op0, op1);
7745 /* Handle pointer minus int. Just like pointer plus int. */
7746 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7747 return pointer_int_sum (MINUS_EXPR, op0, op1);
7748 else
7749 common = 1;
7750 break;
7752 case MULT_EXPR:
7753 common = 1;
7754 break;
7756 case TRUNC_DIV_EXPR:
7757 case CEIL_DIV_EXPR:
7758 case FLOOR_DIV_EXPR:
7759 case ROUND_DIV_EXPR:
7760 case EXACT_DIV_EXPR:
7761 /* Floating point division by zero is a legitimate way to obtain
7762 infinities and NaNs. */
7763 if (skip_evaluation == 0 && integer_zerop (op1))
7764 warning (OPT_Wdiv_by_zero, "division by zero");
7766 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7767 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7768 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7769 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7771 enum tree_code tcode0 = code0, tcode1 = code1;
7773 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7774 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7775 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7776 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7778 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7779 resultcode = RDIV_EXPR;
7780 else
7781 /* Although it would be tempting to shorten always here, that
7782 loses on some targets, since the modulo instruction is
7783 undefined if the quotient can't be represented in the
7784 computation mode. We shorten only if unsigned or if
7785 dividing by something we know != -1. */
7786 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7787 || (TREE_CODE (op1) == INTEGER_CST
7788 && !integer_all_onesp (op1)));
7789 common = 1;
7791 break;
7793 case BIT_AND_EXPR:
7794 case BIT_IOR_EXPR:
7795 case BIT_XOR_EXPR:
7796 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7797 shorten = -1;
7798 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7799 common = 1;
7800 break;
7802 case TRUNC_MOD_EXPR:
7803 case FLOOR_MOD_EXPR:
7804 if (skip_evaluation == 0 && integer_zerop (op1))
7805 warning (OPT_Wdiv_by_zero, "division by zero");
7807 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7809 /* Although it would be tempting to shorten always here, that loses
7810 on some targets, since the modulo instruction is undefined if the
7811 quotient can't be represented in the computation mode. We shorten
7812 only if unsigned or if dividing by something we know != -1. */
7813 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7814 || (TREE_CODE (op1) == INTEGER_CST
7815 && !integer_all_onesp (op1)));
7816 common = 1;
7818 break;
7820 case TRUTH_ANDIF_EXPR:
7821 case TRUTH_ORIF_EXPR:
7822 case TRUTH_AND_EXPR:
7823 case TRUTH_OR_EXPR:
7824 case TRUTH_XOR_EXPR:
7825 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7826 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7827 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7828 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7830 /* Result of these operations is always an int,
7831 but that does not mean the operands should be
7832 converted to ints! */
7833 result_type = integer_type_node;
7834 op0 = c_common_truthvalue_conversion (op0);
7835 op1 = c_common_truthvalue_conversion (op1);
7836 converted = 1;
7838 break;
7840 /* Shift operations: result has same type as first operand;
7841 always convert second operand to int.
7842 Also set SHORT_SHIFT if shifting rightward. */
7844 case RSHIFT_EXPR:
7845 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7847 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7849 if (tree_int_cst_sgn (op1) < 0)
7850 warning (0, "right shift count is negative");
7851 else
7853 if (!integer_zerop (op1))
7854 short_shift = 1;
7856 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7857 warning (0, "right shift count >= width of type");
7861 /* Use the type of the value to be shifted. */
7862 result_type = type0;
7863 /* Convert the shift-count to an integer, regardless of size
7864 of value being shifted. */
7865 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7866 op1 = convert (integer_type_node, op1);
7867 /* Avoid converting op1 to result_type later. */
7868 converted = 1;
7870 break;
7872 case LSHIFT_EXPR:
7873 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7875 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7877 if (tree_int_cst_sgn (op1) < 0)
7878 warning (0, "left shift count is negative");
7880 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7881 warning (0, "left shift count >= width of type");
7884 /* Use the type of the value to be shifted. */
7885 result_type = type0;
7886 /* Convert the shift-count to an integer, regardless of size
7887 of value being shifted. */
7888 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7889 op1 = convert (integer_type_node, op1);
7890 /* Avoid converting op1 to result_type later. */
7891 converted = 1;
7893 break;
7895 case EQ_EXPR:
7896 case NE_EXPR:
7897 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
7898 warning (OPT_Wfloat_equal,
7899 "comparing floating point with == or != is unsafe");
7900 /* Result of comparison is always int,
7901 but don't convert the args to int! */
7902 build_type = integer_type_node;
7903 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7904 || code0 == COMPLEX_TYPE)
7905 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7906 || code1 == COMPLEX_TYPE))
7907 short_compare = 1;
7908 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7910 tree tt0 = TREE_TYPE (type0);
7911 tree tt1 = TREE_TYPE (type1);
7912 /* Anything compares with void *. void * compares with anything.
7913 Otherwise, the targets must be compatible
7914 and both must be object or both incomplete. */
7915 if (comp_target_types (type0, type1))
7916 result_type = common_pointer_type (type0, type1);
7917 else if (VOID_TYPE_P (tt0))
7919 /* op0 != orig_op0 detects the case of something
7920 whose value is 0 but which isn't a valid null ptr const. */
7921 if (pedantic && !null_pointer_constant_p (orig_op0)
7922 && TREE_CODE (tt1) == FUNCTION_TYPE)
7923 pedwarn ("ISO C forbids comparison of %<void *%>"
7924 " with function pointer");
7926 else if (VOID_TYPE_P (tt1))
7928 if (pedantic && !null_pointer_constant_p (orig_op1)
7929 && TREE_CODE (tt0) == FUNCTION_TYPE)
7930 pedwarn ("ISO C forbids comparison of %<void *%>"
7931 " with function pointer");
7933 else
7934 /* Avoid warning about the volatile ObjC EH puts on decls. */
7935 if (!objc_ok)
7936 pedwarn ("comparison of distinct pointer types lacks a cast");
7938 if (result_type == NULL_TREE)
7939 result_type = ptr_type_node;
7941 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
7942 result_type = type0;
7943 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
7944 result_type = type1;
7945 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7947 result_type = type0;
7948 pedwarn ("comparison between pointer and integer");
7950 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7952 result_type = type1;
7953 pedwarn ("comparison between pointer and integer");
7955 break;
7957 case LE_EXPR:
7958 case GE_EXPR:
7959 case LT_EXPR:
7960 case GT_EXPR:
7961 build_type = integer_type_node;
7962 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7963 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7964 short_compare = 1;
7965 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7967 if (comp_target_types (type0, type1))
7969 result_type = common_pointer_type (type0, type1);
7970 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7971 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7972 pedwarn ("comparison of complete and incomplete pointers");
7973 else if (pedantic
7974 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7975 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7977 else
7979 result_type = ptr_type_node;
7980 pedwarn ("comparison of distinct pointer types lacks a cast");
7983 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
7985 result_type = type0;
7986 if (pedantic || extra_warnings)
7987 pedwarn ("ordered comparison of pointer with integer zero");
7989 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
7991 result_type = type1;
7992 if (pedantic)
7993 pedwarn ("ordered comparison of pointer with integer zero");
7995 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7997 result_type = type0;
7998 pedwarn ("comparison between pointer and integer");
8000 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8002 result_type = type1;
8003 pedwarn ("comparison between pointer and integer");
8005 break;
8007 default:
8008 gcc_unreachable ();
8011 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8012 return error_mark_node;
8014 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8015 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8016 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8017 TREE_TYPE (type1))))
8019 binary_op_error (code);
8020 return error_mark_node;
8023 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8024 || code0 == VECTOR_TYPE)
8026 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8027 || code1 == VECTOR_TYPE))
8029 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8031 if (shorten || common || short_compare)
8032 result_type = c_common_type (type0, type1);
8034 /* For certain operations (which identify themselves by shorten != 0)
8035 if both args were extended from the same smaller type,
8036 do the arithmetic in that type and then extend.
8038 shorten !=0 and !=1 indicates a bitwise operation.
8039 For them, this optimization is safe only if
8040 both args are zero-extended or both are sign-extended.
8041 Otherwise, we might change the result.
8042 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8043 but calculated in (unsigned short) it would be (unsigned short)-1. */
8045 if (shorten && none_complex)
8047 int unsigned0, unsigned1;
8048 tree arg0 = get_narrower (op0, &unsigned0);
8049 tree arg1 = get_narrower (op1, &unsigned1);
8050 /* UNS is 1 if the operation to be done is an unsigned one. */
8051 int uns = TYPE_UNSIGNED (result_type);
8052 tree type;
8054 final_type = result_type;
8056 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8057 but it *requires* conversion to FINAL_TYPE. */
8059 if ((TYPE_PRECISION (TREE_TYPE (op0))
8060 == TYPE_PRECISION (TREE_TYPE (arg0)))
8061 && TREE_TYPE (op0) != final_type)
8062 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8063 if ((TYPE_PRECISION (TREE_TYPE (op1))
8064 == TYPE_PRECISION (TREE_TYPE (arg1)))
8065 && TREE_TYPE (op1) != final_type)
8066 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8068 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8070 /* For bitwise operations, signedness of nominal type
8071 does not matter. Consider only how operands were extended. */
8072 if (shorten == -1)
8073 uns = unsigned0;
8075 /* Note that in all three cases below we refrain from optimizing
8076 an unsigned operation on sign-extended args.
8077 That would not be valid. */
8079 /* Both args variable: if both extended in same way
8080 from same width, do it in that width.
8081 Do it unsigned if args were zero-extended. */
8082 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8083 < TYPE_PRECISION (result_type))
8084 && (TYPE_PRECISION (TREE_TYPE (arg1))
8085 == TYPE_PRECISION (TREE_TYPE (arg0)))
8086 && unsigned0 == unsigned1
8087 && (unsigned0 || !uns))
8088 result_type
8089 = c_common_signed_or_unsigned_type
8090 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8091 else if (TREE_CODE (arg0) == INTEGER_CST
8092 && (unsigned1 || !uns)
8093 && (TYPE_PRECISION (TREE_TYPE (arg1))
8094 < TYPE_PRECISION (result_type))
8095 && (type
8096 = c_common_signed_or_unsigned_type (unsigned1,
8097 TREE_TYPE (arg1)),
8098 int_fits_type_p (arg0, type)))
8099 result_type = type;
8100 else if (TREE_CODE (arg1) == INTEGER_CST
8101 && (unsigned0 || !uns)
8102 && (TYPE_PRECISION (TREE_TYPE (arg0))
8103 < TYPE_PRECISION (result_type))
8104 && (type
8105 = c_common_signed_or_unsigned_type (unsigned0,
8106 TREE_TYPE (arg0)),
8107 int_fits_type_p (arg1, type)))
8108 result_type = type;
8111 /* Shifts can be shortened if shifting right. */
8113 if (short_shift)
8115 int unsigned_arg;
8116 tree arg0 = get_narrower (op0, &unsigned_arg);
8118 final_type = result_type;
8120 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8121 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8123 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8124 /* We can shorten only if the shift count is less than the
8125 number of bits in the smaller type size. */
8126 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8127 /* We cannot drop an unsigned shift after sign-extension. */
8128 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8130 /* Do an unsigned shift if the operand was zero-extended. */
8131 result_type
8132 = c_common_signed_or_unsigned_type (unsigned_arg,
8133 TREE_TYPE (arg0));
8134 /* Convert value-to-be-shifted to that type. */
8135 if (TREE_TYPE (op0) != result_type)
8136 op0 = convert (result_type, op0);
8137 converted = 1;
8141 /* Comparison operations are shortened too but differently.
8142 They identify themselves by setting short_compare = 1. */
8144 if (short_compare)
8146 /* Don't write &op0, etc., because that would prevent op0
8147 from being kept in a register.
8148 Instead, make copies of the our local variables and
8149 pass the copies by reference, then copy them back afterward. */
8150 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8151 enum tree_code xresultcode = resultcode;
8152 tree val
8153 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8155 if (val != 0)
8156 return val;
8158 op0 = xop0, op1 = xop1;
8159 converted = 1;
8160 resultcode = xresultcode;
8162 if (warn_sign_compare && skip_evaluation == 0)
8164 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8165 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8166 int unsignedp0, unsignedp1;
8167 tree primop0 = get_narrower (op0, &unsignedp0);
8168 tree primop1 = get_narrower (op1, &unsignedp1);
8170 xop0 = orig_op0;
8171 xop1 = orig_op1;
8172 STRIP_TYPE_NOPS (xop0);
8173 STRIP_TYPE_NOPS (xop1);
8175 /* Give warnings for comparisons between signed and unsigned
8176 quantities that may fail.
8178 Do the checking based on the original operand trees, so that
8179 casts will be considered, but default promotions won't be.
8181 Do not warn if the comparison is being done in a signed type,
8182 since the signed type will only be chosen if it can represent
8183 all the values of the unsigned type. */
8184 if (!TYPE_UNSIGNED (result_type))
8185 /* OK */;
8186 /* Do not warn if both operands are the same signedness. */
8187 else if (op0_signed == op1_signed)
8188 /* OK */;
8189 else
8191 tree sop, uop;
8193 if (op0_signed)
8194 sop = xop0, uop = xop1;
8195 else
8196 sop = xop1, uop = xop0;
8198 /* Do not warn if the signed quantity is an
8199 unsuffixed integer literal (or some static
8200 constant expression involving such literals or a
8201 conditional expression involving such literals)
8202 and it is non-negative. */
8203 if (tree_expr_nonnegative_p (sop))
8204 /* OK */;
8205 /* Do not warn if the comparison is an equality operation,
8206 the unsigned quantity is an integral constant, and it
8207 would fit in the result if the result were signed. */
8208 else if (TREE_CODE (uop) == INTEGER_CST
8209 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8210 && int_fits_type_p
8211 (uop, c_common_signed_type (result_type)))
8212 /* OK */;
8213 /* Do not warn if the unsigned quantity is an enumeration
8214 constant and its maximum value would fit in the result
8215 if the result were signed. */
8216 else if (TREE_CODE (uop) == INTEGER_CST
8217 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8218 && int_fits_type_p
8219 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8220 c_common_signed_type (result_type)))
8221 /* OK */;
8222 else
8223 warning (0, "comparison between signed and unsigned");
8226 /* Warn if two unsigned values are being compared in a size
8227 larger than their original size, and one (and only one) is the
8228 result of a `~' operator. This comparison will always fail.
8230 Also warn if one operand is a constant, and the constant
8231 does not have all bits set that are set in the ~ operand
8232 when it is extended. */
8234 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8235 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8237 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8238 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8239 &unsignedp0);
8240 else
8241 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8242 &unsignedp1);
8244 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8246 tree primop;
8247 HOST_WIDE_INT constant, mask;
8248 int unsignedp, bits;
8250 if (host_integerp (primop0, 0))
8252 primop = primop1;
8253 unsignedp = unsignedp1;
8254 constant = tree_low_cst (primop0, 0);
8256 else
8258 primop = primop0;
8259 unsignedp = unsignedp0;
8260 constant = tree_low_cst (primop1, 0);
8263 bits = TYPE_PRECISION (TREE_TYPE (primop));
8264 if (bits < TYPE_PRECISION (result_type)
8265 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8267 mask = (~(HOST_WIDE_INT) 0) << bits;
8268 if ((mask & constant) != mask)
8269 warning (0, "comparison of promoted ~unsigned with constant");
8272 else if (unsignedp0 && unsignedp1
8273 && (TYPE_PRECISION (TREE_TYPE (primop0))
8274 < TYPE_PRECISION (result_type))
8275 && (TYPE_PRECISION (TREE_TYPE (primop1))
8276 < TYPE_PRECISION (result_type)))
8277 warning (0, "comparison of promoted ~unsigned with unsigned");
8283 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8284 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8285 Then the expression will be built.
8286 It will be given type FINAL_TYPE if that is nonzero;
8287 otherwise, it will be given type RESULT_TYPE. */
8289 if (!result_type)
8291 binary_op_error (code);
8292 return error_mark_node;
8295 if (!converted)
8297 if (TREE_TYPE (op0) != result_type)
8298 op0 = convert (result_type, op0);
8299 if (TREE_TYPE (op1) != result_type)
8300 op1 = convert (result_type, op1);
8302 /* This can happen if one operand has a vector type, and the other
8303 has a different type. */
8304 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8305 return error_mark_node;
8308 if (build_type == NULL_TREE)
8309 build_type = result_type;
8312 /* Treat expressions in initializers specially as they can't trap. */
8313 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8314 build_type,
8315 op0, op1)
8316 : fold_build2 (resultcode, build_type,
8317 op0, op1);
8319 if (final_type != 0)
8320 result = convert (final_type, result);
8321 return result;
8326 /* Convert EXPR to be a truth-value, validating its type for this
8327 purpose. */
8329 tree
8330 c_objc_common_truthvalue_conversion (tree expr)
8332 switch (TREE_CODE (TREE_TYPE (expr)))
8334 case ARRAY_TYPE:
8335 error ("used array that cannot be converted to pointer where scalar is required");
8336 return error_mark_node;
8338 case RECORD_TYPE:
8339 error ("used struct type value where scalar is required");
8340 return error_mark_node;
8342 case UNION_TYPE:
8343 error ("used union type value where scalar is required");
8344 return error_mark_node;
8346 case FUNCTION_TYPE:
8347 gcc_unreachable ();
8349 default:
8350 break;
8353 /* ??? Should we also give an error for void and vectors rather than
8354 leaving those to give errors later? */
8355 return c_common_truthvalue_conversion (expr);
8359 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8360 required. */
8362 tree
8363 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8364 bool *ti ATTRIBUTE_UNUSED, bool *se)
8366 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8368 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8369 /* Executing a compound literal inside a function reinitializes
8370 it. */
8371 if (!TREE_STATIC (decl))
8372 *se = true;
8373 return decl;
8375 else
8376 return expr;