Refuse to label media that is too large to handle a 32 bit disklabel
[dragonfly/vkernel-mp.git] / contrib / gcc-4.1 / gcc / c-typeck.c
blob679417cc00fb1c6b91680f039144f6504d572e78
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 tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree lookup_field (tree, tree);
84 static tree convert_arguments (tree, tree, tree, tree);
85 static tree pointer_diff (tree, tree);
86 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
87 int);
88 static tree valid_compound_expr_initializer (tree, tree);
89 static void push_string (const char *);
90 static void push_member_name (tree);
91 static int spelling_length (void);
92 static char *print_spelling (char *);
93 static void warning_init (const char *);
94 static tree digest_init (tree, tree, bool, int);
95 static void output_init_element (tree, bool, tree, tree, int);
96 static void output_pending_init_elements (int);
97 static int set_designator (int);
98 static void push_range_stack (tree);
99 static void add_pending_init (tree, tree);
100 static void set_nonincremental_init (void);
101 static void set_nonincremental_init_from_string (tree);
102 static tree find_init_member (tree);
103 static void readonly_error (tree, enum lvalue_use);
104 static int lvalue_or_else (tree, enum lvalue_use);
105 static int lvalue_p (tree);
106 static void record_maybe_used_decl (tree);
107 static int comptypes_internal (tree, tree);
108 \f/* This is a cache to hold if two types are compatible or not. */
110 struct tagged_tu_seen_cache {
111 const struct tagged_tu_seen_cache * next;
112 tree t1;
113 tree t2;
114 /* The return value of tagged_types_tu_compatible_p if we had seen
115 these two types already. */
116 int val;
119 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
120 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
122 /* Do `exp = require_complete_type (exp);' to make sure exp
123 does not have an incomplete type. (That includes void types.) */
125 tree
126 require_complete_type (tree value)
128 tree type = TREE_TYPE (value);
130 if (value == error_mark_node || type == error_mark_node)
131 return error_mark_node;
133 /* First, detect a valid value with a complete type. */
134 if (COMPLETE_TYPE_P (type))
135 return value;
137 c_incomplete_type_error (value, type);
138 return error_mark_node;
141 /* Print an error message for invalid use of an incomplete type.
142 VALUE is the expression that was used (or 0 if that isn't known)
143 and TYPE is the type that was invalid. */
145 void
146 c_incomplete_type_error (tree value, tree type)
148 const char *type_code_string;
150 /* Avoid duplicate error message. */
151 if (TREE_CODE (type) == ERROR_MARK)
152 return;
154 if (value != 0 && (TREE_CODE (value) == VAR_DECL
155 || TREE_CODE (value) == PARM_DECL))
156 error ("%qD has an incomplete type", value);
157 else
159 retry:
160 /* We must print an error message. Be clever about what it says. */
162 switch (TREE_CODE (type))
164 case RECORD_TYPE:
165 type_code_string = "struct";
166 break;
168 case UNION_TYPE:
169 type_code_string = "union";
170 break;
172 case ENUMERAL_TYPE:
173 type_code_string = "enum";
174 break;
176 case VOID_TYPE:
177 error ("invalid use of void expression");
178 return;
180 case ARRAY_TYPE:
181 if (TYPE_DOMAIN (type))
183 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
185 error ("invalid use of flexible array member");
186 return;
188 type = TREE_TYPE (type);
189 goto retry;
191 error ("invalid use of array with unspecified bounds");
192 return;
194 default:
195 gcc_unreachable ();
198 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
199 error ("invalid use of undefined type %<%s %E%>",
200 type_code_string, TYPE_NAME (type));
201 else
202 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
203 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
207 /* Given a type, apply default promotions wrt unnamed function
208 arguments and return the new type. */
210 tree
211 c_type_promotes_to (tree type)
213 if (TYPE_MAIN_VARIANT (type) == float_type_node)
214 return double_type_node;
216 if (c_promoting_integer_type_p (type))
218 /* Preserve unsignedness if not really getting any wider. */
219 if (TYPE_UNSIGNED (type)
220 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
221 return unsigned_type_node;
222 return integer_type_node;
225 return type;
228 /* Return a variant of TYPE which has all the type qualifiers of LIKE
229 as well as those of TYPE. */
231 static tree
232 qualify_type (tree type, tree like)
234 return c_build_qualified_type (type,
235 TYPE_QUALS (type) | TYPE_QUALS (like));
238 /* Return the composite type of two compatible types.
240 We assume that comptypes has already been done and returned
241 nonzero; if that isn't so, this may crash. In particular, we
242 assume that qualifiers match. */
244 tree
245 composite_type (tree t1, tree t2)
247 enum tree_code code1;
248 enum tree_code code2;
249 tree attributes;
251 /* Save time if the two types are the same. */
253 if (t1 == t2) return t1;
255 /* If one type is nonsense, use the other. */
256 if (t1 == error_mark_node)
257 return t2;
258 if (t2 == error_mark_node)
259 return t1;
261 code1 = TREE_CODE (t1);
262 code2 = TREE_CODE (t2);
264 /* Merge the attributes. */
265 attributes = targetm.merge_type_attributes (t1, t2);
267 /* If one is an enumerated type and the other is the compatible
268 integer type, the composite type might be either of the two
269 (DR#013 question 3). For consistency, use the enumerated type as
270 the composite type. */
272 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
273 return t1;
274 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
275 return t2;
277 gcc_assert (code1 == code2);
279 switch (code1)
281 case POINTER_TYPE:
282 /* For two pointers, do this recursively on the target type. */
284 tree pointed_to_1 = TREE_TYPE (t1);
285 tree pointed_to_2 = TREE_TYPE (t2);
286 tree target = composite_type (pointed_to_1, pointed_to_2);
287 t1 = build_pointer_type (target);
288 t1 = build_type_attribute_variant (t1, attributes);
289 return qualify_type (t1, t2);
292 case ARRAY_TYPE:
294 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
295 int quals;
296 tree unqual_elt;
297 tree d1 = TYPE_DOMAIN (t1);
298 tree d2 = TYPE_DOMAIN (t2);
299 bool d1_variable, d2_variable;
300 bool d1_zero, d2_zero;
302 /* We should not have any type quals on arrays at all. */
303 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
305 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
306 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
308 d1_variable = (!d1_zero
309 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
310 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
311 d2_variable = (!d2_zero
312 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
313 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
315 /* Save space: see if the result is identical to one of the args. */
316 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
317 && (d2_variable || d2_zero || !d1_variable))
318 return build_type_attribute_variant (t1, attributes);
319 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
320 && (d1_variable || d1_zero || !d2_variable))
321 return build_type_attribute_variant (t2, attributes);
323 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
324 return build_type_attribute_variant (t1, attributes);
325 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
326 return build_type_attribute_variant (t2, attributes);
328 /* Merge the element types, and have a size if either arg has
329 one. We may have qualifiers on the element types. To set
330 up TYPE_MAIN_VARIANT correctly, we need to form the
331 composite of the unqualified types and add the qualifiers
332 back at the end. */
333 quals = TYPE_QUALS (strip_array_types (elt));
334 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
335 t1 = build_array_type (unqual_elt,
336 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
337 && (d2_variable
338 || d2_zero
339 || !d1_variable))
340 ? t1
341 : t2));
342 t1 = c_build_qualified_type (t1, quals);
343 return build_type_attribute_variant (t1, attributes);
346 case FUNCTION_TYPE:
347 /* Function types: prefer the one that specified arg types.
348 If both do, merge the arg types. Also merge the return types. */
350 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
351 tree p1 = TYPE_ARG_TYPES (t1);
352 tree p2 = TYPE_ARG_TYPES (t2);
353 int len;
354 tree newargs, n;
355 int i;
357 /* Save space: see if the result is identical to one of the args. */
358 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
359 return build_type_attribute_variant (t1, attributes);
360 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
361 return build_type_attribute_variant (t2, attributes);
363 /* Simple way if one arg fails to specify argument types. */
364 if (TYPE_ARG_TYPES (t1) == 0)
366 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
367 t1 = build_type_attribute_variant (t1, attributes);
368 return qualify_type (t1, t2);
370 if (TYPE_ARG_TYPES (t2) == 0)
372 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
373 t1 = build_type_attribute_variant (t1, attributes);
374 return qualify_type (t1, t2);
377 /* If both args specify argument types, we must merge the two
378 lists, argument by argument. */
379 /* Tell global_bindings_p to return false so that variable_size
380 doesn't die on VLAs in parameter types. */
381 c_override_global_bindings_to_false = true;
383 len = list_length (p1);
384 newargs = 0;
386 for (i = 0; i < len; i++)
387 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
389 n = newargs;
391 for (; p1;
392 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
394 /* A null type means arg type is not specified.
395 Take whatever the other function type has. */
396 if (TREE_VALUE (p1) == 0)
398 TREE_VALUE (n) = TREE_VALUE (p2);
399 goto parm_done;
401 if (TREE_VALUE (p2) == 0)
403 TREE_VALUE (n) = TREE_VALUE (p1);
404 goto parm_done;
407 /* Given wait (union {union wait *u; int *i} *)
408 and wait (union wait *),
409 prefer union wait * as type of parm. */
410 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
411 && TREE_VALUE (p1) != TREE_VALUE (p2))
413 tree memb;
414 tree mv2 = TREE_VALUE (p2);
415 if (mv2 && mv2 != error_mark_node
416 && TREE_CODE (mv2) != ARRAY_TYPE)
417 mv2 = TYPE_MAIN_VARIANT (mv2);
418 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
419 memb; memb = TREE_CHAIN (memb))
421 tree mv3 = TREE_TYPE (memb);
422 if (mv3 && mv3 != error_mark_node
423 && TREE_CODE (mv3) != ARRAY_TYPE)
424 mv3 = TYPE_MAIN_VARIANT (mv3);
425 if (comptypes (mv3, mv2))
427 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
428 TREE_VALUE (p2));
429 if (pedantic)
430 pedwarn ("function types not truly compatible in ISO C");
431 goto parm_done;
435 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
436 && TREE_VALUE (p2) != TREE_VALUE (p1))
438 tree memb;
439 tree mv1 = TREE_VALUE (p1);
440 if (mv1 && mv1 != error_mark_node
441 && TREE_CODE (mv1) != ARRAY_TYPE)
442 mv1 = TYPE_MAIN_VARIANT (mv1);
443 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
444 memb; memb = TREE_CHAIN (memb))
446 tree mv3 = TREE_TYPE (memb);
447 if (mv3 && mv3 != error_mark_node
448 && TREE_CODE (mv3) != ARRAY_TYPE)
449 mv3 = TYPE_MAIN_VARIANT (mv3);
450 if (comptypes (mv3, mv1))
452 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
453 TREE_VALUE (p1));
454 if (pedantic)
455 pedwarn ("function types not truly compatible in ISO C");
456 goto parm_done;
460 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
461 parm_done: ;
464 c_override_global_bindings_to_false = false;
465 t1 = build_function_type (valtype, newargs);
466 t1 = qualify_type (t1, t2);
467 /* ... falls through ... */
470 default:
471 return build_type_attribute_variant (t1, attributes);
476 /* Return the type of a conditional expression between pointers to
477 possibly differently qualified versions of compatible types.
479 We assume that comp_target_types has already been done and returned
480 nonzero; if that isn't so, this may crash. */
482 static tree
483 common_pointer_type (tree t1, tree t2)
485 tree attributes;
486 tree pointed_to_1, mv1;
487 tree pointed_to_2, mv2;
488 tree target;
490 /* Save time if the two types are the same. */
492 if (t1 == t2) return t1;
494 /* If one type is nonsense, use the other. */
495 if (t1 == error_mark_node)
496 return t2;
497 if (t2 == error_mark_node)
498 return t1;
500 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
501 && TREE_CODE (t2) == POINTER_TYPE);
503 /* Merge the attributes. */
504 attributes = targetm.merge_type_attributes (t1, t2);
506 /* Find the composite type of the target types, and combine the
507 qualifiers of the two types' targets. Do not lose qualifiers on
508 array element types by taking the TYPE_MAIN_VARIANT. */
509 mv1 = pointed_to_1 = TREE_TYPE (t1);
510 mv2 = pointed_to_2 = TREE_TYPE (t2);
511 if (TREE_CODE (mv1) != ARRAY_TYPE)
512 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
513 if (TREE_CODE (mv2) != ARRAY_TYPE)
514 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
515 target = composite_type (mv1, mv2);
516 t1 = build_pointer_type (c_build_qualified_type
517 (target,
518 TYPE_QUALS (pointed_to_1) |
519 TYPE_QUALS (pointed_to_2)));
520 return build_type_attribute_variant (t1, attributes);
523 /* Return the common type for two arithmetic types under the usual
524 arithmetic conversions. The default conversions have already been
525 applied, and enumerated types converted to their compatible integer
526 types. The resulting type is unqualified and has no attributes.
528 This is the type for the result of most arithmetic operations
529 if the operands have the given two types. */
531 static tree
532 c_common_type (tree t1, tree t2)
534 enum tree_code code1;
535 enum tree_code code2;
537 /* If one type is nonsense, use the other. */
538 if (t1 == error_mark_node)
539 return t2;
540 if (t2 == error_mark_node)
541 return t1;
543 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
544 t1 = TYPE_MAIN_VARIANT (t1);
546 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
547 t2 = TYPE_MAIN_VARIANT (t2);
549 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
550 t1 = build_type_attribute_variant (t1, NULL_TREE);
552 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
553 t2 = build_type_attribute_variant (t2, NULL_TREE);
555 /* Save time if the two types are the same. */
557 if (t1 == t2) return t1;
559 code1 = TREE_CODE (t1);
560 code2 = TREE_CODE (t2);
562 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
563 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
564 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
565 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
567 /* If one type is a vector type, return that type. (How the usual
568 arithmetic conversions apply to the vector types extension is not
569 precisely specified.) */
570 if (code1 == VECTOR_TYPE)
571 return t1;
573 if (code2 == VECTOR_TYPE)
574 return t2;
576 /* If one type is complex, form the common type of the non-complex
577 components, then make that complex. Use T1 or T2 if it is the
578 required type. */
579 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
581 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
582 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
583 tree subtype = c_common_type (subtype1, subtype2);
585 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
586 return t1;
587 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
588 return t2;
589 else
590 return build_complex_type (subtype);
593 /* If only one is real, use it as the result. */
595 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
596 return t1;
598 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
599 return t2;
601 /* Both real or both integers; use the one with greater precision. */
603 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
604 return t1;
605 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
606 return t2;
608 /* Same precision. Prefer long longs to longs to ints when the
609 same precision, following the C99 rules on integer type rank
610 (which are equivalent to the C90 rules for C90 types). */
612 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
613 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
614 return long_long_unsigned_type_node;
616 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
617 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
619 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
620 return long_long_unsigned_type_node;
621 else
622 return long_long_integer_type_node;
625 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
626 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
627 return long_unsigned_type_node;
629 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
630 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
632 /* But preserve unsignedness from the other type,
633 since long cannot hold all the values of an unsigned int. */
634 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
635 return long_unsigned_type_node;
636 else
637 return long_integer_type_node;
640 /* Likewise, prefer long double to double even if same size. */
641 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
642 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
643 return long_double_type_node;
645 /* Otherwise prefer the unsigned one. */
647 if (TYPE_UNSIGNED (t1))
648 return t1;
649 else
650 return t2;
653 /* Wrapper around c_common_type that is used by c-common.c and other
654 front end optimizations that remove promotions. ENUMERAL_TYPEs
655 are allowed here and are converted to their compatible integer types.
656 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
657 preferably a non-Boolean type as the common type. */
658 tree
659 common_type (tree t1, tree t2)
661 if (TREE_CODE (t1) == ENUMERAL_TYPE)
662 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
663 if (TREE_CODE (t2) == ENUMERAL_TYPE)
664 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
666 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
667 if (TREE_CODE (t1) == BOOLEAN_TYPE
668 && TREE_CODE (t2) == BOOLEAN_TYPE)
669 return boolean_type_node;
671 /* If either type is BOOLEAN_TYPE, then return the other. */
672 if (TREE_CODE (t1) == BOOLEAN_TYPE)
673 return t2;
674 if (TREE_CODE (t2) == BOOLEAN_TYPE)
675 return t1;
677 return c_common_type (t1, t2);
680 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
681 or various other operations. Return 2 if they are compatible
682 but a warning may be needed if you use them together. */
685 comptypes (tree type1, tree type2)
687 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
688 int val;
690 val = comptypes_internal (type1, type2);
691 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
693 return val;
695 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
696 or various other operations. Return 2 if they are compatible
697 but a warning may be needed if you use them together. This
698 differs from comptypes, in that we don't free the seen types. */
700 static int
701 comptypes_internal (tree type1, tree type2)
703 tree t1 = type1;
704 tree t2 = type2;
705 int attrval, val;
707 /* Suppress errors caused by previously reported errors. */
709 if (t1 == t2 || !t1 || !t2
710 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
711 return 1;
713 /* If either type is the internal version of sizetype, return the
714 language version. */
715 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
716 && TYPE_ORIG_SIZE_TYPE (t1))
717 t1 = TYPE_ORIG_SIZE_TYPE (t1);
719 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
720 && TYPE_ORIG_SIZE_TYPE (t2))
721 t2 = TYPE_ORIG_SIZE_TYPE (t2);
724 /* Enumerated types are compatible with integer types, but this is
725 not transitive: two enumerated types in the same translation unit
726 are compatible with each other only if they are the same type. */
728 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
729 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
730 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
731 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
733 if (t1 == t2)
734 return 1;
736 /* Different classes of types can't be compatible. */
738 if (TREE_CODE (t1) != TREE_CODE (t2))
739 return 0;
741 /* Qualifiers must match. C99 6.7.3p9 */
743 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
744 return 0;
746 /* Allow for two different type nodes which have essentially the same
747 definition. Note that we already checked for equality of the type
748 qualifiers (just above). */
750 if (TREE_CODE (t1) != ARRAY_TYPE
751 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
752 return 1;
754 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
755 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
756 return 0;
758 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
759 val = 0;
761 switch (TREE_CODE (t1))
763 case POINTER_TYPE:
764 /* Do not remove mode or aliasing information. */
765 if (TYPE_MODE (t1) != TYPE_MODE (t2)
766 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
767 break;
768 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
769 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
770 break;
772 case FUNCTION_TYPE:
773 val = function_types_compatible_p (t1, t2);
774 break;
776 case ARRAY_TYPE:
778 tree d1 = TYPE_DOMAIN (t1);
779 tree d2 = TYPE_DOMAIN (t2);
780 bool d1_variable, d2_variable;
781 bool d1_zero, d2_zero;
782 val = 1;
784 /* Target types must match incl. qualifiers. */
785 if (TREE_TYPE (t1) != TREE_TYPE (t2)
786 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
787 return 0;
789 /* Sizes must match unless one is missing or variable. */
790 if (d1 == 0 || d2 == 0 || d1 == d2)
791 break;
793 d1_zero = !TYPE_MAX_VALUE (d1);
794 d2_zero = !TYPE_MAX_VALUE (d2);
796 d1_variable = (!d1_zero
797 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
798 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
799 d2_variable = (!d2_zero
800 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
801 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
803 if (d1_variable || d2_variable)
804 break;
805 if (d1_zero && d2_zero)
806 break;
807 if (d1_zero || d2_zero
808 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
809 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
810 val = 0;
812 break;
815 case ENUMERAL_TYPE:
816 case RECORD_TYPE:
817 case UNION_TYPE:
818 if (val != 1 && !same_translation_unit_p (t1, t2))
820 if (attrval != 2)
821 return tagged_types_tu_compatible_p (t1, t2);
822 val = tagged_types_tu_compatible_p (t1, t2);
824 break;
826 case VECTOR_TYPE:
827 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
828 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
829 break;
831 default:
832 break;
834 return attrval == 2 && val == 1 ? 2 : val;
837 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
838 ignoring their qualifiers. */
840 static int
841 comp_target_types (tree ttl, tree ttr)
843 int val;
844 tree mvl, mvr;
846 /* Do not lose qualifiers on element types of array types that are
847 pointer targets by taking their TYPE_MAIN_VARIANT. */
848 mvl = TREE_TYPE (ttl);
849 mvr = TREE_TYPE (ttr);
850 if (TREE_CODE (mvl) != ARRAY_TYPE)
851 mvl = TYPE_MAIN_VARIANT (mvl);
852 if (TREE_CODE (mvr) != ARRAY_TYPE)
853 mvr = TYPE_MAIN_VARIANT (mvr);
854 val = comptypes (mvl, mvr);
856 if (val == 2 && pedantic)
857 pedwarn ("types are not quite compatible");
858 return val;
861 /* Subroutines of `comptypes'. */
863 /* Determine whether two trees derive from the same translation unit.
864 If the CONTEXT chain ends in a null, that tree's context is still
865 being parsed, so if two trees have context chains ending in null,
866 they're in the same translation unit. */
868 same_translation_unit_p (tree t1, tree t2)
870 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
871 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
873 case tcc_declaration:
874 t1 = DECL_CONTEXT (t1); break;
875 case tcc_type:
876 t1 = TYPE_CONTEXT (t1); break;
877 case tcc_exceptional:
878 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
879 default: gcc_unreachable ();
882 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
883 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
885 case tcc_declaration:
886 t2 = DECL_CONTEXT (t2); break;
887 case tcc_type:
888 t2 = TYPE_CONTEXT (t2); break;
889 case tcc_exceptional:
890 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
891 default: gcc_unreachable ();
894 return t1 == t2;
897 /* Allocate the seen two types, assuming that they are compatible. */
899 static struct tagged_tu_seen_cache *
900 alloc_tagged_tu_seen_cache (tree t1, tree t2)
902 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
903 tu->next = tagged_tu_seen_base;
904 tu->t1 = t1;
905 tu->t2 = t2;
907 tagged_tu_seen_base = tu;
909 /* The C standard says that two structures in different translation
910 units are compatible with each other only if the types of their
911 fields are compatible (among other things). We assume that they
912 are compatible until proven otherwise when building the cache.
913 An example where this can occur is:
914 struct a
916 struct a *next;
918 If we are comparing this against a similar struct in another TU,
919 and did not assume they were compatible, we end up with an infinite
920 loop. */
921 tu->val = 1;
922 return tu;
925 /* Free the seen types until we get to TU_TIL. */
927 static void
928 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
930 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
931 while (tu != tu_til)
933 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
934 tu = tu1->next;
935 free (tu1);
937 tagged_tu_seen_base = tu_til;
940 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
941 compatible. If the two types are not the same (which has been
942 checked earlier), this can only happen when multiple translation
943 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
944 rules. */
946 static int
947 tagged_types_tu_compatible_p (tree t1, tree t2)
949 tree s1, s2;
950 bool needs_warning = false;
952 /* We have to verify that the tags of the types are the same. This
953 is harder than it looks because this may be a typedef, so we have
954 to go look at the original type. It may even be a typedef of a
955 typedef...
956 In the case of compiler-created builtin structs the TYPE_DECL
957 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
958 while (TYPE_NAME (t1)
959 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
960 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
961 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
963 while (TYPE_NAME (t2)
964 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
965 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
966 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
968 /* C90 didn't have the requirement that the two tags be the same. */
969 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
970 return 0;
972 /* C90 didn't say what happened if one or both of the types were
973 incomplete; we choose to follow C99 rules here, which is that they
974 are compatible. */
975 if (TYPE_SIZE (t1) == NULL
976 || TYPE_SIZE (t2) == NULL)
977 return 1;
980 const struct tagged_tu_seen_cache * tts_i;
981 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
982 if (tts_i->t1 == t1 && tts_i->t2 == t2)
983 return tts_i->val;
986 switch (TREE_CODE (t1))
988 case ENUMERAL_TYPE:
990 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
991 /* Speed up the case where the type values are in the same order. */
992 tree tv1 = TYPE_VALUES (t1);
993 tree tv2 = TYPE_VALUES (t2);
995 if (tv1 == tv2)
997 return 1;
1000 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1002 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1003 break;
1004 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1006 tu->val = 0;
1007 return 0;
1011 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1013 return 1;
1015 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1017 tu->val = 0;
1018 return 0;
1021 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1023 tu->val = 0;
1024 return 0;
1027 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1029 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1030 if (s2 == NULL
1031 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1033 tu->val = 0;
1034 return 0;
1037 return 1;
1040 case UNION_TYPE:
1042 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1043 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1045 tu->val = 0;
1046 return 0;
1049 /* Speed up the common case where the fields are in the same order. */
1050 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1051 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1053 int result;
1056 if (DECL_NAME (s1) == NULL
1057 || DECL_NAME (s1) != DECL_NAME (s2))
1058 break;
1059 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1060 if (result == 0)
1062 tu->val = 0;
1063 return 0;
1065 if (result == 2)
1066 needs_warning = true;
1068 if (TREE_CODE (s1) == FIELD_DECL
1069 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1070 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1072 tu->val = 0;
1073 return 0;
1076 if (!s1 && !s2)
1078 tu->val = needs_warning ? 2 : 1;
1079 return tu->val;
1082 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1084 bool ok = false;
1086 if (DECL_NAME (s1) != NULL)
1087 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1088 if (DECL_NAME (s1) == DECL_NAME (s2))
1090 int result;
1091 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1092 if (result == 0)
1094 tu->val = 0;
1095 return 0;
1097 if (result == 2)
1098 needs_warning = true;
1100 if (TREE_CODE (s1) == FIELD_DECL
1101 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1102 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1103 break;
1105 ok = true;
1106 break;
1108 if (!ok)
1110 tu->val = 0;
1111 return 0;
1114 tu->val = needs_warning ? 2 : 10;
1115 return tu->val;
1118 case RECORD_TYPE:
1120 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1122 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1123 s1 && s2;
1124 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1126 int result;
1127 if (TREE_CODE (s1) != TREE_CODE (s2)
1128 || DECL_NAME (s1) != DECL_NAME (s2))
1129 break;
1130 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1131 if (result == 0)
1132 break;
1133 if (result == 2)
1134 needs_warning = true;
1136 if (TREE_CODE (s1) == FIELD_DECL
1137 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1138 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1139 break;
1141 if (s1 && s2)
1142 tu->val = 0;
1143 else
1144 tu->val = needs_warning ? 2 : 1;
1145 return tu->val;
1148 default:
1149 gcc_unreachable ();
1153 /* Return 1 if two function types F1 and F2 are compatible.
1154 If either type specifies no argument types,
1155 the other must specify a fixed number of self-promoting arg types.
1156 Otherwise, if one type specifies only the number of arguments,
1157 the other must specify that number of self-promoting arg types.
1158 Otherwise, the argument types must match. */
1160 static int
1161 function_types_compatible_p (tree f1, tree f2)
1163 tree args1, args2;
1164 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1165 int val = 1;
1166 int val1;
1167 tree ret1, ret2;
1169 ret1 = TREE_TYPE (f1);
1170 ret2 = TREE_TYPE (f2);
1172 /* 'volatile' qualifiers on a function's return type used to mean
1173 the function is noreturn. */
1174 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1175 pedwarn ("function return types not compatible due to %<volatile%>");
1176 if (TYPE_VOLATILE (ret1))
1177 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1178 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1179 if (TYPE_VOLATILE (ret2))
1180 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1181 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1182 val = comptypes_internal (ret1, ret2);
1183 if (val == 0)
1184 return 0;
1186 args1 = TYPE_ARG_TYPES (f1);
1187 args2 = TYPE_ARG_TYPES (f2);
1189 /* An unspecified parmlist matches any specified parmlist
1190 whose argument types don't need default promotions. */
1192 if (args1 == 0)
1194 if (!self_promoting_args_p (args2))
1195 return 0;
1196 /* If one of these types comes from a non-prototype fn definition,
1197 compare that with the other type's arglist.
1198 If they don't match, ask for a warning (but no error). */
1199 if (TYPE_ACTUAL_ARG_TYPES (f1)
1200 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1201 val = 2;
1202 return val;
1204 if (args2 == 0)
1206 if (!self_promoting_args_p (args1))
1207 return 0;
1208 if (TYPE_ACTUAL_ARG_TYPES (f2)
1209 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1210 val = 2;
1211 return val;
1214 /* Both types have argument lists: compare them and propagate results. */
1215 val1 = type_lists_compatible_p (args1, args2);
1216 return val1 != 1 ? val1 : val;
1219 /* Check two lists of types for compatibility,
1220 returning 0 for incompatible, 1 for compatible,
1221 or 2 for compatible with warning. */
1223 static int
1224 type_lists_compatible_p (tree args1, tree args2)
1226 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1227 int val = 1;
1228 int newval = 0;
1230 while (1)
1232 tree a1, mv1, a2, mv2;
1233 if (args1 == 0 && args2 == 0)
1234 return val;
1235 /* If one list is shorter than the other,
1236 they fail to match. */
1237 if (args1 == 0 || args2 == 0)
1238 return 0;
1239 mv1 = a1 = TREE_VALUE (args1);
1240 mv2 = a2 = TREE_VALUE (args2);
1241 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1242 mv1 = TYPE_MAIN_VARIANT (mv1);
1243 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1244 mv2 = TYPE_MAIN_VARIANT (mv2);
1245 /* A null pointer instead of a type
1246 means there is supposed to be an argument
1247 but nothing is specified about what type it has.
1248 So match anything that self-promotes. */
1249 if (a1 == 0)
1251 if (c_type_promotes_to (a2) != a2)
1252 return 0;
1254 else if (a2 == 0)
1256 if (c_type_promotes_to (a1) != a1)
1257 return 0;
1259 /* If one of the lists has an error marker, ignore this arg. */
1260 else if (TREE_CODE (a1) == ERROR_MARK
1261 || TREE_CODE (a2) == ERROR_MARK)
1263 else if (!(newval = comptypes_internal (mv1, mv2)))
1265 /* Allow wait (union {union wait *u; int *i} *)
1266 and wait (union wait *) to be compatible. */
1267 if (TREE_CODE (a1) == UNION_TYPE
1268 && (TYPE_NAME (a1) == 0
1269 || TYPE_TRANSPARENT_UNION (a1))
1270 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1271 && tree_int_cst_equal (TYPE_SIZE (a1),
1272 TYPE_SIZE (a2)))
1274 tree memb;
1275 for (memb = TYPE_FIELDS (a1);
1276 memb; memb = TREE_CHAIN (memb))
1278 tree mv3 = TREE_TYPE (memb);
1279 if (mv3 && mv3 != error_mark_node
1280 && TREE_CODE (mv3) != ARRAY_TYPE)
1281 mv3 = TYPE_MAIN_VARIANT (mv3);
1282 if (comptypes_internal (mv3, mv2))
1283 break;
1285 if (memb == 0)
1286 return 0;
1288 else if (TREE_CODE (a2) == UNION_TYPE
1289 && (TYPE_NAME (a2) == 0
1290 || TYPE_TRANSPARENT_UNION (a2))
1291 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1292 && tree_int_cst_equal (TYPE_SIZE (a2),
1293 TYPE_SIZE (a1)))
1295 tree memb;
1296 for (memb = TYPE_FIELDS (a2);
1297 memb; memb = TREE_CHAIN (memb))
1299 tree mv3 = TREE_TYPE (memb);
1300 if (mv3 && mv3 != error_mark_node
1301 && TREE_CODE (mv3) != ARRAY_TYPE)
1302 mv3 = TYPE_MAIN_VARIANT (mv3);
1303 if (comptypes_internal (mv3, mv1))
1304 break;
1306 if (memb == 0)
1307 return 0;
1309 else
1310 return 0;
1313 /* comptypes said ok, but record if it said to warn. */
1314 if (newval > val)
1315 val = newval;
1317 args1 = TREE_CHAIN (args1);
1318 args2 = TREE_CHAIN (args2);
1322 /* Compute the size to increment a pointer by. */
1324 static tree
1325 c_size_in_bytes (tree type)
1327 enum tree_code code = TREE_CODE (type);
1329 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1330 return size_one_node;
1332 if (!COMPLETE_OR_VOID_TYPE_P (type))
1334 error ("arithmetic on pointer to an incomplete type");
1335 return size_one_node;
1338 /* Convert in case a char is more than one unit. */
1339 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1340 size_int (TYPE_PRECISION (char_type_node)
1341 / BITS_PER_UNIT));
1344 /* Return either DECL or its known constant value (if it has one). */
1346 tree
1347 decl_constant_value (tree decl)
1349 if (/* Don't change a variable array bound or initial value to a constant
1350 in a place where a variable is invalid. Note that DECL_INITIAL
1351 isn't valid for a PARM_DECL. */
1352 current_function_decl != 0
1353 && TREE_CODE (decl) != PARM_DECL
1354 && !TREE_THIS_VOLATILE (decl)
1355 && TREE_READONLY (decl)
1356 && DECL_INITIAL (decl) != 0
1357 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1358 /* This is invalid if initial value is not constant.
1359 If it has either a function call, a memory reference,
1360 or a variable, then re-evaluating it could give different results. */
1361 && TREE_CONSTANT (DECL_INITIAL (decl))
1362 /* Check for cases where this is sub-optimal, even though valid. */
1363 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1364 return DECL_INITIAL (decl);
1365 return decl;
1368 /* Return either DECL or its known constant value (if it has one), but
1369 return DECL if pedantic or DECL has mode BLKmode. This is for
1370 bug-compatibility with the old behavior of decl_constant_value
1371 (before GCC 3.0); every use of this function is a bug and it should
1372 be removed before GCC 3.1. It is not appropriate to use pedantic
1373 in a way that affects optimization, and BLKmode is probably not the
1374 right test for avoiding misoptimizations either. */
1376 static tree
1377 decl_constant_value_for_broken_optimization (tree decl)
1379 tree ret;
1381 if (pedantic || DECL_MODE (decl) == BLKmode)
1382 return decl;
1384 ret = decl_constant_value (decl);
1385 /* Avoid unwanted tree sharing between the initializer and current
1386 function's body where the tree can be modified e.g. by the
1387 gimplifier. */
1388 if (ret != decl && TREE_STATIC (decl))
1389 ret = unshare_expr (ret);
1390 return ret;
1393 /* Convert the array expression EXP to a pointer. */
1394 static tree
1395 array_to_pointer_conversion (tree exp)
1397 tree orig_exp = exp;
1398 tree type = TREE_TYPE (exp);
1399 tree adr;
1400 tree restype = TREE_TYPE (type);
1401 tree ptrtype;
1403 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1405 STRIP_TYPE_NOPS (exp);
1407 if (TREE_NO_WARNING (orig_exp))
1408 TREE_NO_WARNING (exp) = 1;
1410 ptrtype = build_pointer_type (restype);
1412 if (TREE_CODE (exp) == INDIRECT_REF)
1413 return convert (ptrtype, TREE_OPERAND (exp, 0));
1415 if (TREE_CODE (exp) == VAR_DECL)
1417 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1418 ADDR_EXPR because it's the best way of representing what
1419 happens in C when we take the address of an array and place
1420 it in a pointer to the element type. */
1421 adr = build1 (ADDR_EXPR, ptrtype, exp);
1422 if (!c_mark_addressable (exp))
1423 return error_mark_node;
1424 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1425 return adr;
1428 /* This way is better for a COMPONENT_REF since it can
1429 simplify the offset for a component. */
1430 adr = build_unary_op (ADDR_EXPR, exp, 1);
1431 return convert (ptrtype, adr);
1434 /* Convert the function expression EXP to a pointer. */
1435 static tree
1436 function_to_pointer_conversion (tree exp)
1438 tree orig_exp = exp;
1440 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1442 STRIP_TYPE_NOPS (exp);
1444 if (TREE_NO_WARNING (orig_exp))
1445 TREE_NO_WARNING (exp) = 1;
1447 return build_unary_op (ADDR_EXPR, exp, 0);
1450 /* Perform the default conversion of arrays and functions to pointers.
1451 Return the result of converting EXP. For any other expression, just
1452 return EXP after removing NOPs. */
1454 struct c_expr
1455 default_function_array_conversion (struct c_expr exp)
1457 tree orig_exp = exp.value;
1458 tree type = TREE_TYPE (exp.value);
1459 enum tree_code code = TREE_CODE (type);
1461 switch (code)
1463 case ARRAY_TYPE:
1465 bool not_lvalue = false;
1466 bool lvalue_array_p;
1468 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1469 || TREE_CODE (exp.value) == NOP_EXPR)
1470 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1472 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1473 not_lvalue = true;
1474 exp.value = TREE_OPERAND (exp.value, 0);
1477 if (TREE_NO_WARNING (orig_exp))
1478 TREE_NO_WARNING (exp.value) = 1;
1480 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1481 if (!flag_isoc99 && !lvalue_array_p)
1483 /* Before C99, non-lvalue arrays do not decay to pointers.
1484 Normally, using such an array would be invalid; but it can
1485 be used correctly inside sizeof or as a statement expression.
1486 Thus, do not give an error here; an error will result later. */
1487 return exp;
1490 exp.value = array_to_pointer_conversion (exp.value);
1492 break;
1493 case FUNCTION_TYPE:
1494 exp.value = function_to_pointer_conversion (exp.value);
1495 break;
1496 default:
1497 STRIP_TYPE_NOPS (exp.value);
1498 if (TREE_NO_WARNING (orig_exp))
1499 TREE_NO_WARNING (exp.value) = 1;
1500 break;
1503 return exp;
1507 /* EXP is an expression of integer type. Apply the integer promotions
1508 to it and return the promoted value. */
1510 tree
1511 perform_integral_promotions (tree exp)
1513 tree type = TREE_TYPE (exp);
1514 enum tree_code code = TREE_CODE (type);
1516 gcc_assert (INTEGRAL_TYPE_P (type));
1518 /* Normally convert enums to int,
1519 but convert wide enums to something wider. */
1520 if (code == ENUMERAL_TYPE)
1522 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1523 TYPE_PRECISION (integer_type_node)),
1524 ((TYPE_PRECISION (type)
1525 >= TYPE_PRECISION (integer_type_node))
1526 && TYPE_UNSIGNED (type)));
1528 return convert (type, exp);
1531 /* ??? This should no longer be needed now bit-fields have their
1532 proper types. */
1533 if (TREE_CODE (exp) == COMPONENT_REF
1534 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1535 /* If it's thinner than an int, promote it like a
1536 c_promoting_integer_type_p, otherwise leave it alone. */
1537 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1538 TYPE_PRECISION (integer_type_node)))
1539 return convert (integer_type_node, exp);
1541 if (c_promoting_integer_type_p (type))
1543 /* Preserve unsignedness if not really getting any wider. */
1544 if (TYPE_UNSIGNED (type)
1545 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1546 return convert (unsigned_type_node, exp);
1548 return convert (integer_type_node, exp);
1551 return exp;
1555 /* Perform default promotions for C data used in expressions.
1556 Enumeral types or short or char are converted to int.
1557 In addition, manifest constants symbols are replaced by their values. */
1559 tree
1560 default_conversion (tree exp)
1562 tree orig_exp;
1563 tree type = TREE_TYPE (exp);
1564 enum tree_code code = TREE_CODE (type);
1566 /* Functions and arrays have been converted during parsing. */
1567 gcc_assert (code != FUNCTION_TYPE);
1568 if (code == ARRAY_TYPE)
1569 return exp;
1571 /* Constants can be used directly unless they're not loadable. */
1572 if (TREE_CODE (exp) == CONST_DECL)
1573 exp = DECL_INITIAL (exp);
1575 /* Replace a nonvolatile const static variable with its value unless
1576 it is an array, in which case we must be sure that taking the
1577 address of the array produces consistent results. */
1578 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1580 exp = decl_constant_value_for_broken_optimization (exp);
1581 type = TREE_TYPE (exp);
1584 /* Strip no-op conversions. */
1585 orig_exp = exp;
1586 STRIP_TYPE_NOPS (exp);
1588 if (TREE_NO_WARNING (orig_exp))
1589 TREE_NO_WARNING (exp) = 1;
1591 if (INTEGRAL_TYPE_P (type))
1592 return perform_integral_promotions (exp);
1594 if (code == VOID_TYPE)
1596 error ("void value not ignored as it ought to be");
1597 return error_mark_node;
1599 return exp;
1602 /* Look up COMPONENT in a structure or union DECL.
1604 If the component name is not found, returns NULL_TREE. Otherwise,
1605 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1606 stepping down the chain to the component, which is in the last
1607 TREE_VALUE of the list. Normally the list is of length one, but if
1608 the component is embedded within (nested) anonymous structures or
1609 unions, the list steps down the chain to the component. */
1611 static tree
1612 lookup_field (tree decl, tree component)
1614 tree type = TREE_TYPE (decl);
1615 tree field;
1617 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1618 to the field elements. Use a binary search on this array to quickly
1619 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1620 will always be set for structures which have many elements. */
1622 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1624 int bot, top, half;
1625 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1627 field = TYPE_FIELDS (type);
1628 bot = 0;
1629 top = TYPE_LANG_SPECIFIC (type)->s->len;
1630 while (top - bot > 1)
1632 half = (top - bot + 1) >> 1;
1633 field = field_array[bot+half];
1635 if (DECL_NAME (field) == NULL_TREE)
1637 /* Step through all anon unions in linear fashion. */
1638 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1640 field = field_array[bot++];
1641 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1642 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1644 tree anon = lookup_field (field, component);
1646 if (anon)
1647 return tree_cons (NULL_TREE, field, anon);
1651 /* Entire record is only anon unions. */
1652 if (bot > top)
1653 return NULL_TREE;
1655 /* Restart the binary search, with new lower bound. */
1656 continue;
1659 if (DECL_NAME (field) == component)
1660 break;
1661 if (DECL_NAME (field) < component)
1662 bot += half;
1663 else
1664 top = bot + half;
1667 if (DECL_NAME (field_array[bot]) == component)
1668 field = field_array[bot];
1669 else if (DECL_NAME (field) != component)
1670 return NULL_TREE;
1672 else
1674 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1676 if (DECL_NAME (field) == NULL_TREE
1677 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1678 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1680 tree anon = lookup_field (field, component);
1682 if (anon)
1683 return tree_cons (NULL_TREE, field, anon);
1686 if (DECL_NAME (field) == component)
1687 break;
1690 if (field == NULL_TREE)
1691 return NULL_TREE;
1694 return tree_cons (NULL_TREE, field, NULL_TREE);
1697 /* Make an expression to refer to the COMPONENT field of
1698 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1700 tree
1701 build_component_ref (tree datum, tree component)
1703 tree type = TREE_TYPE (datum);
1704 enum tree_code code = TREE_CODE (type);
1705 tree field = NULL;
1706 tree ref;
1708 if (!objc_is_public (datum, component))
1709 return error_mark_node;
1711 /* See if there is a field or component with name COMPONENT. */
1713 if (code == RECORD_TYPE || code == UNION_TYPE)
1715 if (!COMPLETE_TYPE_P (type))
1717 c_incomplete_type_error (NULL_TREE, type);
1718 return error_mark_node;
1721 field = lookup_field (datum, component);
1723 if (!field)
1725 error ("%qT has no member named %qE", type, component);
1726 return error_mark_node;
1729 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1730 This might be better solved in future the way the C++ front
1731 end does it - by giving the anonymous entities each a
1732 separate name and type, and then have build_component_ref
1733 recursively call itself. We can't do that here. */
1736 tree subdatum = TREE_VALUE (field);
1737 int quals;
1738 tree subtype;
1740 if (TREE_TYPE (subdatum) == error_mark_node)
1741 return error_mark_node;
1743 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1744 quals |= TYPE_QUALS (TREE_TYPE (datum));
1745 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1747 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1748 NULL_TREE);
1749 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1750 TREE_READONLY (ref) = 1;
1751 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1752 TREE_THIS_VOLATILE (ref) = 1;
1754 if (TREE_DEPRECATED (subdatum))
1755 warn_deprecated_use (subdatum);
1757 datum = ref;
1759 field = TREE_CHAIN (field);
1761 while (field);
1763 return ref;
1765 else if (code != ERROR_MARK)
1766 error ("request for member %qE in something not a structure or union",
1767 component);
1769 return error_mark_node;
1772 /* Given an expression PTR for a pointer, return an expression
1773 for the value pointed to.
1774 ERRORSTRING is the name of the operator to appear in error messages. */
1776 tree
1777 build_indirect_ref (tree ptr, const char *errorstring)
1779 tree pointer = default_conversion (ptr);
1780 tree type = TREE_TYPE (pointer);
1782 if (TREE_CODE (type) == POINTER_TYPE)
1784 if (TREE_CODE (pointer) == ADDR_EXPR
1785 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1786 == TREE_TYPE (type)))
1787 return TREE_OPERAND (pointer, 0);
1788 else
1790 tree t = TREE_TYPE (type);
1791 tree ref;
1793 ref = build1 (INDIRECT_REF, t, pointer);
1795 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1797 error ("dereferencing pointer to incomplete type");
1798 return error_mark_node;
1800 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1801 warning (0, "dereferencing %<void *%> pointer");
1803 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1804 so that we get the proper error message if the result is used
1805 to assign to. Also, &* is supposed to be a no-op.
1806 And ANSI C seems to specify that the type of the result
1807 should be the const type. */
1808 /* A de-reference of a pointer to const is not a const. It is valid
1809 to change it via some other pointer. */
1810 TREE_READONLY (ref) = TYPE_READONLY (t);
1811 TREE_SIDE_EFFECTS (ref)
1812 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1813 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1814 return ref;
1817 else if (TREE_CODE (pointer) != ERROR_MARK)
1818 error ("invalid type argument of %qs", errorstring);
1819 return error_mark_node;
1822 /* This handles expressions of the form "a[i]", which denotes
1823 an array reference.
1825 This is logically equivalent in C to *(a+i), but we may do it differently.
1826 If A is a variable or a member, we generate a primitive ARRAY_REF.
1827 This avoids forcing the array out of registers, and can work on
1828 arrays that are not lvalues (for example, members of structures returned
1829 by functions). */
1831 tree
1832 build_array_ref (tree array, tree index)
1834 bool swapped = false;
1835 if (TREE_TYPE (array) == error_mark_node
1836 || TREE_TYPE (index) == error_mark_node)
1837 return error_mark_node;
1839 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1840 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1842 tree temp;
1843 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1844 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1846 error ("subscripted value is neither array nor pointer");
1847 return error_mark_node;
1849 temp = array;
1850 array = index;
1851 index = temp;
1852 swapped = true;
1855 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1857 error ("array subscript is not an integer");
1858 return error_mark_node;
1861 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1863 error ("subscripted value is pointer to function");
1864 return error_mark_node;
1867 /* Subscripting with type char is likely to lose on a machine where
1868 chars are signed. So warn on any machine, but optionally. Don't
1869 warn for unsigned char since that type is safe. Don't warn for
1870 signed char because anyone who uses that must have done so
1871 deliberately. ??? Existing practice has also been to warn only
1872 when the char index is syntactically the index, not for
1873 char[array]. */
1874 if (!swapped
1875 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1876 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
1878 /* Apply default promotions *after* noticing character types. */
1879 index = default_conversion (index);
1881 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1883 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1885 tree rval, type;
1887 /* An array that is indexed by a non-constant
1888 cannot be stored in a register; we must be able to do
1889 address arithmetic on its address.
1890 Likewise an array of elements of variable size. */
1891 if (TREE_CODE (index) != INTEGER_CST
1892 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1893 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1895 if (!c_mark_addressable (array))
1896 return error_mark_node;
1898 /* An array that is indexed by a constant value which is not within
1899 the array bounds cannot be stored in a register either; because we
1900 would get a crash in store_bit_field/extract_bit_field when trying
1901 to access a non-existent part of the register. */
1902 if (TREE_CODE (index) == INTEGER_CST
1903 && TYPE_DOMAIN (TREE_TYPE (array))
1904 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1906 if (!c_mark_addressable (array))
1907 return error_mark_node;
1910 if (pedantic)
1912 tree foo = array;
1913 while (TREE_CODE (foo) == COMPONENT_REF)
1914 foo = TREE_OPERAND (foo, 0);
1915 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1916 pedwarn ("ISO C forbids subscripting %<register%> array");
1917 else if (!flag_isoc99 && !lvalue_p (foo))
1918 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1921 type = TREE_TYPE (TREE_TYPE (array));
1922 if (TREE_CODE (type) != ARRAY_TYPE)
1923 type = TYPE_MAIN_VARIANT (type);
1924 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1925 /* Array ref is const/volatile if the array elements are
1926 or if the array is. */
1927 TREE_READONLY (rval)
1928 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1929 | TREE_READONLY (array));
1930 TREE_SIDE_EFFECTS (rval)
1931 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1932 | TREE_SIDE_EFFECTS (array));
1933 TREE_THIS_VOLATILE (rval)
1934 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1935 /* This was added by rms on 16 Nov 91.
1936 It fixes vol struct foo *a; a->elts[1]
1937 in an inline function.
1938 Hope it doesn't break something else. */
1939 | TREE_THIS_VOLATILE (array));
1940 return require_complete_type (fold (rval));
1942 else
1944 tree ar = default_conversion (array);
1946 if (ar == error_mark_node)
1947 return ar;
1949 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1950 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1952 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1953 "array indexing");
1957 /* Build an external reference to identifier ID. FUN indicates
1958 whether this will be used for a function call. LOC is the source
1959 location of the identifier. */
1960 tree
1961 build_external_ref (tree id, int fun, location_t loc)
1963 tree ref;
1964 tree decl = lookup_name (id);
1966 /* In Objective-C, an instance variable (ivar) may be preferred to
1967 whatever lookup_name() found. */
1968 decl = objc_lookup_ivar (decl, id);
1970 if (decl && decl != error_mark_node)
1971 ref = decl;
1972 else if (fun)
1973 /* Implicit function declaration. */
1974 ref = implicitly_declare (id);
1975 else if (decl == error_mark_node)
1976 /* Don't complain about something that's already been
1977 complained about. */
1978 return error_mark_node;
1979 else
1981 undeclared_variable (id, loc);
1982 return error_mark_node;
1985 if (TREE_TYPE (ref) == error_mark_node)
1986 return error_mark_node;
1988 if (TREE_DEPRECATED (ref))
1989 warn_deprecated_use (ref);
1991 if (!skip_evaluation)
1992 assemble_external (ref);
1993 TREE_USED (ref) = 1;
1995 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1997 if (!in_sizeof && !in_typeof)
1998 C_DECL_USED (ref) = 1;
1999 else if (DECL_INITIAL (ref) == 0
2000 && DECL_EXTERNAL (ref)
2001 && !TREE_PUBLIC (ref))
2002 record_maybe_used_decl (ref);
2005 if (TREE_CODE (ref) == CONST_DECL)
2007 ref = DECL_INITIAL (ref);
2008 TREE_CONSTANT (ref) = 1;
2009 TREE_INVARIANT (ref) = 1;
2011 else if (current_function_decl != 0
2012 && !DECL_FILE_SCOPE_P (current_function_decl)
2013 && (TREE_CODE (ref) == VAR_DECL
2014 || TREE_CODE (ref) == PARM_DECL
2015 || TREE_CODE (ref) == FUNCTION_DECL))
2017 tree context = decl_function_context (ref);
2019 if (context != 0 && context != current_function_decl)
2020 DECL_NONLOCAL (ref) = 1;
2023 return ref;
2026 /* Record details of decls possibly used inside sizeof or typeof. */
2027 struct maybe_used_decl
2029 /* The decl. */
2030 tree decl;
2031 /* The level seen at (in_sizeof + in_typeof). */
2032 int level;
2033 /* The next one at this level or above, or NULL. */
2034 struct maybe_used_decl *next;
2037 static struct maybe_used_decl *maybe_used_decls;
2039 /* Record that DECL, an undefined static function reference seen
2040 inside sizeof or typeof, might be used if the operand of sizeof is
2041 a VLA type or the operand of typeof is a variably modified
2042 type. */
2044 static void
2045 record_maybe_used_decl (tree decl)
2047 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2048 t->decl = decl;
2049 t->level = in_sizeof + in_typeof;
2050 t->next = maybe_used_decls;
2051 maybe_used_decls = t;
2054 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2055 USED is false, just discard them. If it is true, mark them used
2056 (if no longer inside sizeof or typeof) or move them to the next
2057 level up (if still inside sizeof or typeof). */
2059 void
2060 pop_maybe_used (bool used)
2062 struct maybe_used_decl *p = maybe_used_decls;
2063 int cur_level = in_sizeof + in_typeof;
2064 while (p && p->level > cur_level)
2066 if (used)
2068 if (cur_level == 0)
2069 C_DECL_USED (p->decl) = 1;
2070 else
2071 p->level = cur_level;
2073 p = p->next;
2075 if (!used || cur_level == 0)
2076 maybe_used_decls = p;
2079 /* Return the result of sizeof applied to EXPR. */
2081 struct c_expr
2082 c_expr_sizeof_expr (struct c_expr expr)
2084 struct c_expr ret;
2085 if (expr.value == error_mark_node)
2087 ret.value = error_mark_node;
2088 ret.original_code = ERROR_MARK;
2089 pop_maybe_used (false);
2091 else
2093 ret.value = c_sizeof (TREE_TYPE (expr.value));
2094 ret.original_code = ERROR_MARK;
2095 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2097 return ret;
2100 /* Return the result of sizeof applied to T, a structure for the type
2101 name passed to sizeof (rather than the type itself). */
2103 struct c_expr
2104 c_expr_sizeof_type (struct c_type_name *t)
2106 tree type;
2107 struct c_expr ret;
2108 type = groktypename (t);
2109 ret.value = c_sizeof (type);
2110 ret.original_code = ERROR_MARK;
2111 pop_maybe_used (type != error_mark_node
2112 ? C_TYPE_VARIABLE_SIZE (type) : false);
2113 return ret;
2116 /* Build a function call to function FUNCTION with parameters PARAMS.
2117 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2118 TREE_VALUE of each node is a parameter-expression.
2119 FUNCTION's data type may be a function type or a pointer-to-function. */
2121 tree
2122 build_function_call (tree function, tree params)
2124 tree fntype, fundecl = 0;
2125 tree coerced_params;
2126 tree name = NULL_TREE, result;
2127 tree tem;
2129 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2130 STRIP_TYPE_NOPS (function);
2132 /* Convert anything with function type to a pointer-to-function. */
2133 if (TREE_CODE (function) == FUNCTION_DECL)
2135 /* Implement type-directed function overloading for builtins.
2136 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2137 handle all the type checking. The result is a complete expression
2138 that implements this function call. */
2139 tem = resolve_overloaded_builtin (function, params);
2140 if (tem)
2141 return tem;
2143 name = DECL_NAME (function);
2144 fundecl = function;
2146 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2147 function = function_to_pointer_conversion (function);
2149 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2150 expressions, like those used for ObjC messenger dispatches. */
2151 function = objc_rewrite_function_call (function, params);
2153 fntype = TREE_TYPE (function);
2155 if (TREE_CODE (fntype) == ERROR_MARK)
2156 return error_mark_node;
2158 if (!(TREE_CODE (fntype) == POINTER_TYPE
2159 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2161 error ("called object %qE is not a function", function);
2162 return error_mark_node;
2165 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2166 current_function_returns_abnormally = 1;
2168 /* fntype now gets the type of function pointed to. */
2169 fntype = TREE_TYPE (fntype);
2171 /* Check that the function is called through a compatible prototype.
2172 If it is not, replace the call by a trap, wrapped up in a compound
2173 expression if necessary. This has the nice side-effect to prevent
2174 the tree-inliner from generating invalid assignment trees which may
2175 blow up in the RTL expander later. */
2176 if (TREE_CODE (function) == NOP_EXPR
2177 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2178 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2179 && !comptypes (fntype, TREE_TYPE (tem)))
2181 tree return_type = TREE_TYPE (fntype);
2182 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2183 NULL_TREE);
2185 /* This situation leads to run-time undefined behavior. We can't,
2186 therefore, simply error unless we can prove that all possible
2187 executions of the program must execute the code. */
2188 warning (0, "function called through a non-compatible type");
2190 /* We can, however, treat "undefined" any way we please.
2191 Call abort to encourage the user to fix the program. */
2192 inform ("if this code is reached, the program will abort");
2194 if (VOID_TYPE_P (return_type))
2195 return trap;
2196 else
2198 tree rhs;
2200 if (AGGREGATE_TYPE_P (return_type))
2201 rhs = build_compound_literal (return_type,
2202 build_constructor (return_type, 0));
2203 else
2204 rhs = fold_build1 (NOP_EXPR, return_type, integer_zero_node);
2206 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2210 /* Convert the parameters to the types declared in the
2211 function prototype, or apply default promotions. */
2213 coerced_params
2214 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2216 if (coerced_params == error_mark_node)
2217 return error_mark_node;
2219 /* Check that the arguments to the function are valid. */
2221 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2222 TYPE_ARG_TYPES (fntype));
2224 if (require_constant_value)
2226 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2227 function, coerced_params, NULL_TREE);
2229 if (TREE_CONSTANT (result)
2230 && (name == NULL_TREE
2231 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2232 pedwarn_init ("initializer element is not constant");
2234 else
2235 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2236 function, coerced_params, NULL_TREE);
2238 if (VOID_TYPE_P (TREE_TYPE (result)))
2239 return result;
2240 return require_complete_type (result);
2243 /* Convert the argument expressions in the list VALUES
2244 to the types in the list TYPELIST. The result is a list of converted
2245 argument expressions, unless there are too few arguments in which
2246 case it is error_mark_node.
2248 If TYPELIST is exhausted, or when an element has NULL as its type,
2249 perform the default conversions.
2251 PARMLIST is the chain of parm decls for the function being called.
2252 It may be 0, if that info is not available.
2253 It is used only for generating error messages.
2255 FUNCTION is a tree for the called function. It is used only for
2256 error messages, where it is formatted with %qE.
2258 This is also where warnings about wrong number of args are generated.
2260 Both VALUES and the returned value are chains of TREE_LIST nodes
2261 with the elements of the list in the TREE_VALUE slots of those nodes. */
2263 static tree
2264 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2266 tree typetail, valtail;
2267 tree result = NULL;
2268 int parmnum;
2269 tree selector;
2271 /* Change pointer to function to the function itself for
2272 diagnostics. */
2273 if (TREE_CODE (function) == ADDR_EXPR
2274 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2275 function = TREE_OPERAND (function, 0);
2277 /* Handle an ObjC selector specially for diagnostics. */
2278 selector = objc_message_selector ();
2280 /* Scan the given expressions and types, producing individual
2281 converted arguments and pushing them on RESULT in reverse order. */
2283 for (valtail = values, typetail = typelist, parmnum = 0;
2284 valtail;
2285 valtail = TREE_CHAIN (valtail), parmnum++)
2287 tree type = typetail ? TREE_VALUE (typetail) : 0;
2288 tree val = TREE_VALUE (valtail);
2289 tree rname = function;
2290 int argnum = parmnum + 1;
2291 const char *invalid_func_diag;
2293 if (type == void_type_node)
2295 error ("too many arguments to function %qE", function);
2296 break;
2299 if (selector && argnum > 2)
2301 rname = selector;
2302 argnum -= 2;
2305 STRIP_TYPE_NOPS (val);
2307 val = require_complete_type (val);
2309 if (type != 0)
2311 /* Formal parm type is specified by a function prototype. */
2312 tree parmval;
2314 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2316 error ("type of formal parameter %d is incomplete", parmnum + 1);
2317 parmval = val;
2319 else
2321 /* Optionally warn about conversions that
2322 differ from the default conversions. */
2323 if (warn_conversion || warn_traditional)
2325 unsigned int formal_prec = TYPE_PRECISION (type);
2327 if (INTEGRAL_TYPE_P (type)
2328 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2329 warning (0, "passing argument %d of %qE as integer "
2330 "rather than floating due to prototype",
2331 argnum, rname);
2332 if (INTEGRAL_TYPE_P (type)
2333 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2334 warning (0, "passing argument %d of %qE as integer "
2335 "rather than complex due to prototype",
2336 argnum, rname);
2337 else if (TREE_CODE (type) == COMPLEX_TYPE
2338 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2339 warning (0, "passing argument %d of %qE as complex "
2340 "rather than floating due to prototype",
2341 argnum, rname);
2342 else if (TREE_CODE (type) == REAL_TYPE
2343 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2344 warning (0, "passing argument %d of %qE as floating "
2345 "rather than integer due to prototype",
2346 argnum, rname);
2347 else if (TREE_CODE (type) == COMPLEX_TYPE
2348 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2349 warning (0, "passing argument %d of %qE as complex "
2350 "rather than integer due to prototype",
2351 argnum, rname);
2352 else if (TREE_CODE (type) == REAL_TYPE
2353 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2354 warning (0, "passing argument %d of %qE as floating "
2355 "rather than complex due to prototype",
2356 argnum, rname);
2357 /* ??? At some point, messages should be written about
2358 conversions between complex types, but that's too messy
2359 to do now. */
2360 else if (TREE_CODE (type) == REAL_TYPE
2361 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2363 /* Warn if any argument is passed as `float',
2364 since without a prototype it would be `double'. */
2365 if (formal_prec == TYPE_PRECISION (float_type_node))
2366 warning (0, "passing argument %d of %qE as %<float%> "
2367 "rather than %<double%> due to prototype",
2368 argnum, rname);
2370 /* Detect integer changing in width or signedness.
2371 These warnings are only activated with
2372 -Wconversion, not with -Wtraditional. */
2373 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2374 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2376 tree would_have_been = default_conversion (val);
2377 tree type1 = TREE_TYPE (would_have_been);
2379 if (TREE_CODE (type) == ENUMERAL_TYPE
2380 && (TYPE_MAIN_VARIANT (type)
2381 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2382 /* No warning if function asks for enum
2383 and the actual arg is that enum type. */
2385 else if (formal_prec != TYPE_PRECISION (type1))
2386 warning (OPT_Wconversion, "passing argument %d of %qE "
2387 "with different width due to prototype",
2388 argnum, rname);
2389 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2391 /* Don't complain if the formal parameter type
2392 is an enum, because we can't tell now whether
2393 the value was an enum--even the same enum. */
2394 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2396 else if (TREE_CODE (val) == INTEGER_CST
2397 && int_fits_type_p (val, type))
2398 /* Change in signedness doesn't matter
2399 if a constant value is unaffected. */
2401 /* If the value is extended from a narrower
2402 unsigned type, it doesn't matter whether we
2403 pass it as signed or unsigned; the value
2404 certainly is the same either way. */
2405 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2406 && TYPE_UNSIGNED (TREE_TYPE (val)))
2408 else if (TYPE_UNSIGNED (type))
2409 warning (OPT_Wconversion, "passing argument %d of %qE "
2410 "as unsigned due to prototype",
2411 argnum, rname);
2412 else
2413 warning (OPT_Wconversion, "passing argument %d of %qE "
2414 "as signed due to prototype", argnum, rname);
2418 parmval = convert_for_assignment (type, val, ic_argpass,
2419 fundecl, function,
2420 parmnum + 1);
2422 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2423 && INTEGRAL_TYPE_P (type)
2424 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2425 parmval = default_conversion (parmval);
2427 result = tree_cons (NULL_TREE, parmval, result);
2429 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2430 && (TYPE_PRECISION (TREE_TYPE (val))
2431 < TYPE_PRECISION (double_type_node)))
2432 /* Convert `float' to `double'. */
2433 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2434 else if ((invalid_func_diag =
2435 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2437 error (invalid_func_diag);
2438 return error_mark_node;
2440 else
2441 /* Convert `short' and `char' to full-size `int'. */
2442 result = tree_cons (NULL_TREE, default_conversion (val), result);
2444 if (typetail)
2445 typetail = TREE_CHAIN (typetail);
2448 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2450 error ("too few arguments to function %qE", function);
2451 return error_mark_node;
2454 return nreverse (result);
2457 /* This is the entry point used by the parser to build unary operators
2458 in the input. CODE, a tree_code, specifies the unary operator, and
2459 ARG is the operand. For unary plus, the C parser currently uses
2460 CONVERT_EXPR for code. */
2462 struct c_expr
2463 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2465 struct c_expr result;
2467 result.original_code = ERROR_MARK;
2468 result.value = build_unary_op (code, arg.value, 0);
2469 overflow_warning (result.value);
2470 return result;
2473 /* This is the entry point used by the parser to build binary operators
2474 in the input. CODE, a tree_code, specifies the binary operator, and
2475 ARG1 and ARG2 are the operands. In addition to constructing the
2476 expression, we check for operands that were written with other binary
2477 operators in a way that is likely to confuse the user. */
2479 struct c_expr
2480 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2481 struct c_expr arg2)
2483 struct c_expr result;
2485 enum tree_code code1 = arg1.original_code;
2486 enum tree_code code2 = arg2.original_code;
2488 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2489 result.original_code = code;
2491 if (TREE_CODE (result.value) == ERROR_MARK)
2492 return result;
2494 /* Check for cases such as x+y<<z which users are likely
2495 to misinterpret. */
2496 if (warn_parentheses)
2498 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2500 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2501 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2502 warning (OPT_Wparentheses,
2503 "suggest parentheses around + or - inside shift");
2506 if (code == TRUTH_ORIF_EXPR)
2508 if (code1 == TRUTH_ANDIF_EXPR
2509 || code2 == TRUTH_ANDIF_EXPR)
2510 warning (OPT_Wparentheses,
2511 "suggest parentheses around && within ||");
2514 if (code == BIT_IOR_EXPR)
2516 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2517 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2518 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2519 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2520 warning (OPT_Wparentheses,
2521 "suggest parentheses around arithmetic in operand of |");
2522 /* Check cases like x|y==z */
2523 if (TREE_CODE_CLASS (code1) == tcc_comparison
2524 || TREE_CODE_CLASS (code2) == tcc_comparison)
2525 warning (OPT_Wparentheses,
2526 "suggest parentheses around comparison in operand of |");
2529 if (code == BIT_XOR_EXPR)
2531 if (code1 == BIT_AND_EXPR
2532 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2533 || code2 == BIT_AND_EXPR
2534 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2535 warning (OPT_Wparentheses,
2536 "suggest parentheses around arithmetic in operand of ^");
2537 /* Check cases like x^y==z */
2538 if (TREE_CODE_CLASS (code1) == tcc_comparison
2539 || TREE_CODE_CLASS (code2) == tcc_comparison)
2540 warning (OPT_Wparentheses,
2541 "suggest parentheses around comparison in operand of ^");
2544 if (code == BIT_AND_EXPR)
2546 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2547 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2548 warning (OPT_Wparentheses,
2549 "suggest parentheses around + or - in operand of &");
2550 /* Check cases like x&y==z */
2551 if (TREE_CODE_CLASS (code1) == tcc_comparison
2552 || TREE_CODE_CLASS (code2) == tcc_comparison)
2553 warning (OPT_Wparentheses,
2554 "suggest parentheses around comparison in operand of &");
2556 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2557 if (TREE_CODE_CLASS (code) == tcc_comparison
2558 && (TREE_CODE_CLASS (code1) == tcc_comparison
2559 || TREE_CODE_CLASS (code2) == tcc_comparison))
2560 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2561 "have their mathematical meaning");
2565 unsigned_conversion_warning (result.value, arg1.value);
2566 unsigned_conversion_warning (result.value, arg2.value);
2567 overflow_warning (result.value);
2569 return result;
2572 /* Return a tree for the difference of pointers OP0 and OP1.
2573 The resulting tree has type int. */
2575 static tree
2576 pointer_diff (tree op0, tree op1)
2578 tree restype = ptrdiff_type_node;
2580 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2581 tree con0, con1, lit0, lit1;
2582 tree orig_op1 = op1;
2584 if (pedantic || warn_pointer_arith)
2586 if (TREE_CODE (target_type) == VOID_TYPE)
2587 pedwarn ("pointer of type %<void *%> used in subtraction");
2588 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2589 pedwarn ("pointer to a function used in subtraction");
2592 /* If the conversion to ptrdiff_type does anything like widening or
2593 converting a partial to an integral mode, we get a convert_expression
2594 that is in the way to do any simplifications.
2595 (fold-const.c doesn't know that the extra bits won't be needed.
2596 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2597 different mode in place.)
2598 So first try to find a common term here 'by hand'; we want to cover
2599 at least the cases that occur in legal static initializers. */
2600 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2601 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2603 if (TREE_CODE (con0) == PLUS_EXPR)
2605 lit0 = TREE_OPERAND (con0, 1);
2606 con0 = TREE_OPERAND (con0, 0);
2608 else
2609 lit0 = integer_zero_node;
2611 if (TREE_CODE (con1) == PLUS_EXPR)
2613 lit1 = TREE_OPERAND (con1, 1);
2614 con1 = TREE_OPERAND (con1, 0);
2616 else
2617 lit1 = integer_zero_node;
2619 if (operand_equal_p (con0, con1, 0))
2621 op0 = lit0;
2622 op1 = lit1;
2626 /* First do the subtraction as integers;
2627 then drop through to build the divide operator.
2628 Do not do default conversions on the minus operator
2629 in case restype is a short type. */
2631 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2632 convert (restype, op1), 0);
2633 /* This generates an error if op1 is pointer to incomplete type. */
2634 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2635 error ("arithmetic on pointer to an incomplete type");
2637 /* This generates an error if op0 is pointer to incomplete type. */
2638 op1 = c_size_in_bytes (target_type);
2640 /* Divide by the size, in easiest possible way. */
2641 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2644 /* Construct and perhaps optimize a tree representation
2645 for a unary operation. CODE, a tree_code, specifies the operation
2646 and XARG is the operand.
2647 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2648 the default promotions (such as from short to int).
2649 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2650 allows non-lvalues; this is only used to handle conversion of non-lvalue
2651 arrays to pointers in C99. */
2653 tree
2654 build_unary_op (enum tree_code code, tree xarg, int flag)
2656 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2657 tree arg = xarg;
2658 tree argtype = 0;
2659 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2660 tree val;
2661 int noconvert = flag;
2662 const char *invalid_op_diag;
2664 if (typecode == ERROR_MARK)
2665 return error_mark_node;
2666 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2667 typecode = INTEGER_TYPE;
2669 if ((invalid_op_diag
2670 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2672 error (invalid_op_diag);
2673 return error_mark_node;
2676 switch (code)
2678 case CONVERT_EXPR:
2679 /* This is used for unary plus, because a CONVERT_EXPR
2680 is enough to prevent anybody from looking inside for
2681 associativity, but won't generate any code. */
2682 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2683 || typecode == COMPLEX_TYPE
2684 || typecode == VECTOR_TYPE))
2686 error ("wrong type argument to unary plus");
2687 return error_mark_node;
2689 else if (!noconvert)
2690 arg = default_conversion (arg);
2691 arg = non_lvalue (arg);
2692 break;
2694 case NEGATE_EXPR:
2695 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2696 || typecode == COMPLEX_TYPE
2697 || typecode == VECTOR_TYPE))
2699 error ("wrong type argument to unary minus");
2700 return error_mark_node;
2702 else if (!noconvert)
2703 arg = default_conversion (arg);
2704 break;
2706 case BIT_NOT_EXPR:
2707 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2709 if (!noconvert)
2710 arg = default_conversion (arg);
2712 else if (typecode == COMPLEX_TYPE)
2714 code = CONJ_EXPR;
2715 if (pedantic)
2716 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2717 if (!noconvert)
2718 arg = default_conversion (arg);
2720 else
2722 error ("wrong type argument to bit-complement");
2723 return error_mark_node;
2725 break;
2727 case ABS_EXPR:
2728 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2730 error ("wrong type argument to abs");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2735 break;
2737 case CONJ_EXPR:
2738 /* Conjugating a real value is a no-op, but allow it anyway. */
2739 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2740 || typecode == COMPLEX_TYPE))
2742 error ("wrong type argument to conjugation");
2743 return error_mark_node;
2745 else if (!noconvert)
2746 arg = default_conversion (arg);
2747 break;
2749 case TRUTH_NOT_EXPR:
2750 if (typecode != INTEGER_TYPE
2751 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2752 && typecode != COMPLEX_TYPE)
2754 error ("wrong type argument to unary exclamation mark");
2755 return error_mark_node;
2757 arg = c_objc_common_truthvalue_conversion (arg);
2758 return invert_truthvalue (arg);
2760 case NOP_EXPR:
2761 break;
2763 case REALPART_EXPR:
2764 if (TREE_CODE (arg) == COMPLEX_CST)
2765 return TREE_REALPART (arg);
2766 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2767 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2768 else
2769 return arg;
2771 case IMAGPART_EXPR:
2772 if (TREE_CODE (arg) == COMPLEX_CST)
2773 return TREE_IMAGPART (arg);
2774 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2775 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2776 else
2777 return convert (TREE_TYPE (arg), integer_zero_node);
2779 case PREINCREMENT_EXPR:
2780 case POSTINCREMENT_EXPR:
2781 case PREDECREMENT_EXPR:
2782 case POSTDECREMENT_EXPR:
2784 /* Increment or decrement the real part of the value,
2785 and don't change the imaginary part. */
2786 if (typecode == COMPLEX_TYPE)
2788 tree real, imag;
2790 if (pedantic)
2791 pedwarn ("ISO C does not support %<++%> and %<--%>"
2792 " on complex types");
2794 arg = stabilize_reference (arg);
2795 real = build_unary_op (REALPART_EXPR, arg, 1);
2796 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2797 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2798 build_unary_op (code, real, 1), imag);
2801 /* Report invalid types. */
2803 if (typecode != POINTER_TYPE
2804 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2806 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2807 error ("wrong type argument to increment");
2808 else
2809 error ("wrong type argument to decrement");
2811 return error_mark_node;
2815 tree inc;
2816 tree result_type = TREE_TYPE (arg);
2818 arg = get_unwidened (arg, 0);
2819 argtype = TREE_TYPE (arg);
2821 /* Compute the increment. */
2823 if (typecode == POINTER_TYPE)
2825 /* If pointer target is an undefined struct,
2826 we just cannot know how to do the arithmetic. */
2827 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2829 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2830 error ("increment of pointer to unknown structure");
2831 else
2832 error ("decrement of pointer to unknown structure");
2834 else if ((pedantic || warn_pointer_arith)
2835 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2836 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2838 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2839 pedwarn ("wrong type argument to increment");
2840 else
2841 pedwarn ("wrong type argument to decrement");
2844 inc = c_size_in_bytes (TREE_TYPE (result_type));
2846 else
2847 inc = integer_one_node;
2849 inc = convert (argtype, inc);
2851 /* Complain about anything else that is not a true lvalue. */
2852 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2853 || code == POSTINCREMENT_EXPR)
2854 ? lv_increment
2855 : lv_decrement)))
2856 return error_mark_node;
2858 /* Report a read-only lvalue. */
2859 if (TREE_READONLY (arg))
2860 readonly_error (arg,
2861 ((code == PREINCREMENT_EXPR
2862 || code == POSTINCREMENT_EXPR)
2863 ? lv_increment : lv_decrement));
2865 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2866 val = boolean_increment (code, arg);
2867 else
2868 val = build2 (code, TREE_TYPE (arg), arg, inc);
2869 TREE_SIDE_EFFECTS (val) = 1;
2870 val = convert (result_type, val);
2871 if (TREE_CODE (val) != code)
2872 TREE_NO_WARNING (val) = 1;
2873 return val;
2876 case ADDR_EXPR:
2877 /* Note that this operation never does default_conversion. */
2879 /* Let &* cancel out to simplify resulting code. */
2880 if (TREE_CODE (arg) == INDIRECT_REF)
2882 /* Don't let this be an lvalue. */
2883 if (lvalue_p (TREE_OPERAND (arg, 0)))
2884 return non_lvalue (TREE_OPERAND (arg, 0));
2885 return TREE_OPERAND (arg, 0);
2888 /* For &x[y], return x+y */
2889 if (TREE_CODE (arg) == ARRAY_REF)
2891 tree op0 = TREE_OPERAND (arg, 0);
2892 if (!c_mark_addressable (op0))
2893 return error_mark_node;
2894 return build_binary_op (PLUS_EXPR,
2895 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2896 ? array_to_pointer_conversion (op0)
2897 : op0),
2898 TREE_OPERAND (arg, 1), 1);
2901 /* Anything not already handled and not a true memory reference
2902 or a non-lvalue array is an error. */
2903 else if (typecode != FUNCTION_TYPE && !flag
2904 && !lvalue_or_else (arg, lv_addressof))
2905 return error_mark_node;
2907 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2908 argtype = TREE_TYPE (arg);
2910 /* If the lvalue is const or volatile, merge that into the type
2911 to which the address will point. Note that you can't get a
2912 restricted pointer by taking the address of something, so we
2913 only have to deal with `const' and `volatile' here. */
2914 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2915 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2916 argtype = c_build_type_variant (argtype,
2917 TREE_READONLY (arg),
2918 TREE_THIS_VOLATILE (arg));
2920 if (!c_mark_addressable (arg))
2921 return error_mark_node;
2923 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2924 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2926 argtype = build_pointer_type (argtype);
2928 /* ??? Cope with user tricks that amount to offsetof. Delete this
2929 when we have proper support for integer constant expressions. */
2930 val = get_base_address (arg);
2931 if (val && TREE_CODE (val) == INDIRECT_REF
2932 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
2934 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
2936 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
2937 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
2940 val = build1 (ADDR_EXPR, argtype, arg);
2942 return val;
2944 default:
2945 break;
2948 if (argtype == 0)
2949 argtype = TREE_TYPE (arg);
2950 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
2951 : fold_build1 (code, argtype, arg);
2954 /* Return nonzero if REF is an lvalue valid for this language.
2955 Lvalues can be assigned, unless their type has TYPE_READONLY.
2956 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2958 static int
2959 lvalue_p (tree ref)
2961 enum tree_code code = TREE_CODE (ref);
2963 switch (code)
2965 case REALPART_EXPR:
2966 case IMAGPART_EXPR:
2967 case COMPONENT_REF:
2968 return lvalue_p (TREE_OPERAND (ref, 0));
2970 case COMPOUND_LITERAL_EXPR:
2971 case STRING_CST:
2972 return 1;
2974 case INDIRECT_REF:
2975 case ARRAY_REF:
2976 case VAR_DECL:
2977 case PARM_DECL:
2978 case RESULT_DECL:
2979 case ERROR_MARK:
2980 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2981 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2983 case BIND_EXPR:
2984 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2986 default:
2987 return 0;
2991 /* Give an error for storing in something that is 'const'. */
2993 static void
2994 readonly_error (tree arg, enum lvalue_use use)
2996 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
2997 || use == lv_asm);
2998 /* Using this macro rather than (for example) arrays of messages
2999 ensures that all the format strings are checked at compile
3000 time. */
3001 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3002 : (use == lv_increment ? (I) \
3003 : (use == lv_decrement ? (D) : (AS))))
3004 if (TREE_CODE (arg) == COMPONENT_REF)
3006 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3007 readonly_error (TREE_OPERAND (arg, 0), use);
3008 else
3009 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3010 G_("increment of read-only member %qD"),
3011 G_("decrement of read-only member %qD"),
3012 G_("read-only member %qD used as %<asm%> output")),
3013 TREE_OPERAND (arg, 1));
3015 else if (TREE_CODE (arg) == VAR_DECL)
3016 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3017 G_("increment of read-only variable %qD"),
3018 G_("decrement of read-only variable %qD"),
3019 G_("read-only variable %qD used as %<asm%> output")),
3020 arg);
3021 else
3022 error (READONLY_MSG (G_("assignment of read-only location"),
3023 G_("increment of read-only location"),
3024 G_("decrement of read-only location"),
3025 G_("read-only location used as %<asm%> output")));
3029 /* Return nonzero if REF is an lvalue valid for this language;
3030 otherwise, print an error message and return zero. USE says
3031 how the lvalue is being used and so selects the error message. */
3033 static int
3034 lvalue_or_else (tree ref, enum lvalue_use use)
3036 int win = lvalue_p (ref);
3038 if (!win)
3039 lvalue_error (use);
3041 return win;
3044 /* Mark EXP saying that we need to be able to take the
3045 address of it; it should not be allocated in a register.
3046 Returns true if successful. */
3048 bool
3049 c_mark_addressable (tree exp)
3051 tree x = exp;
3053 while (1)
3054 switch (TREE_CODE (x))
3056 case COMPONENT_REF:
3057 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3059 error
3060 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3061 return false;
3064 /* ... fall through ... */
3066 case ADDR_EXPR:
3067 case ARRAY_REF:
3068 case REALPART_EXPR:
3069 case IMAGPART_EXPR:
3070 x = TREE_OPERAND (x, 0);
3071 break;
3073 case COMPOUND_LITERAL_EXPR:
3074 case CONSTRUCTOR:
3075 TREE_ADDRESSABLE (x) = 1;
3076 return true;
3078 case VAR_DECL:
3079 case CONST_DECL:
3080 case PARM_DECL:
3081 case RESULT_DECL:
3082 if (C_DECL_REGISTER (x)
3083 && DECL_NONLOCAL (x))
3085 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3087 error
3088 ("global register variable %qD used in nested function", x);
3089 return false;
3091 pedwarn ("register variable %qD used in nested function", x);
3093 else if (C_DECL_REGISTER (x))
3095 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3096 error ("address of global register variable %qD requested", x);
3097 else
3098 error ("address of register variable %qD requested", x);
3099 return false;
3102 /* drops in */
3103 case FUNCTION_DECL:
3104 TREE_ADDRESSABLE (x) = 1;
3105 /* drops out */
3106 default:
3107 return true;
3111 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3113 tree
3114 build_conditional_expr (tree ifexp, tree op1, tree op2)
3116 tree type1;
3117 tree type2;
3118 enum tree_code code1;
3119 enum tree_code code2;
3120 tree result_type = NULL;
3121 tree orig_op1 = op1, orig_op2 = op2;
3123 /* Promote both alternatives. */
3125 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3126 op1 = default_conversion (op1);
3127 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3128 op2 = default_conversion (op2);
3130 if (TREE_CODE (ifexp) == ERROR_MARK
3131 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3132 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3133 return error_mark_node;
3135 type1 = TREE_TYPE (op1);
3136 code1 = TREE_CODE (type1);
3137 type2 = TREE_TYPE (op2);
3138 code2 = TREE_CODE (type2);
3140 /* C90 does not permit non-lvalue arrays in conditional expressions.
3141 In C99 they will be pointers by now. */
3142 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3144 error ("non-lvalue array in conditional expression");
3145 return error_mark_node;
3148 /* Quickly detect the usual case where op1 and op2 have the same type
3149 after promotion. */
3150 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3152 if (type1 == type2)
3153 result_type = type1;
3154 else
3155 result_type = TYPE_MAIN_VARIANT (type1);
3157 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3158 || code1 == COMPLEX_TYPE)
3159 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3160 || code2 == COMPLEX_TYPE))
3162 result_type = c_common_type (type1, type2);
3164 /* If -Wsign-compare, warn here if type1 and type2 have
3165 different signedness. We'll promote the signed to unsigned
3166 and later code won't know it used to be different.
3167 Do this check on the original types, so that explicit casts
3168 will be considered, but default promotions won't. */
3169 if (warn_sign_compare && !skip_evaluation)
3171 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3172 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3174 if (unsigned_op1 ^ unsigned_op2)
3176 /* Do not warn if the result type is signed, since the
3177 signed type will only be chosen if it can represent
3178 all the values of the unsigned type. */
3179 if (!TYPE_UNSIGNED (result_type))
3180 /* OK */;
3181 /* Do not warn if the signed quantity is an unsuffixed
3182 integer literal (or some static constant expression
3183 involving such literals) and it is non-negative. */
3184 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3185 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3186 /* OK */;
3187 else
3188 warning (0, "signed and unsigned type in conditional expression");
3192 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3194 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3195 pedwarn ("ISO C forbids conditional expr with only one void side");
3196 result_type = void_type_node;
3198 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3200 if (comp_target_types (type1, type2))
3201 result_type = common_pointer_type (type1, type2);
3202 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3203 && TREE_CODE (orig_op1) != NOP_EXPR)
3204 result_type = qualify_type (type2, type1);
3205 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3206 && TREE_CODE (orig_op2) != NOP_EXPR)
3207 result_type = qualify_type (type1, type2);
3208 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3210 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3211 pedwarn ("ISO C forbids conditional expr between "
3212 "%<void *%> and function pointer");
3213 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3214 TREE_TYPE (type2)));
3216 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3218 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3219 pedwarn ("ISO C forbids conditional expr between "
3220 "%<void *%> and function pointer");
3221 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3222 TREE_TYPE (type1)));
3224 else
3226 pedwarn ("pointer type mismatch in conditional expression");
3227 result_type = build_pointer_type (void_type_node);
3230 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3232 if (!integer_zerop (op2))
3233 pedwarn ("pointer/integer type mismatch in conditional expression");
3234 else
3236 op2 = null_pointer_node;
3238 result_type = type1;
3240 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3242 if (!integer_zerop (op1))
3243 pedwarn ("pointer/integer type mismatch in conditional expression");
3244 else
3246 op1 = null_pointer_node;
3248 result_type = type2;
3251 if (!result_type)
3253 if (flag_cond_mismatch)
3254 result_type = void_type_node;
3255 else
3257 error ("type mismatch in conditional expression");
3258 return error_mark_node;
3262 /* Merge const and volatile flags of the incoming types. */
3263 result_type
3264 = build_type_variant (result_type,
3265 TREE_READONLY (op1) || TREE_READONLY (op2),
3266 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3268 if (result_type != TREE_TYPE (op1))
3269 op1 = convert_and_check (result_type, op1);
3270 if (result_type != TREE_TYPE (op2))
3271 op2 = convert_and_check (result_type, op2);
3273 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3276 /* Return a compound expression that performs two expressions and
3277 returns the value of the second of them. */
3279 tree
3280 build_compound_expr (tree expr1, tree expr2)
3282 if (!TREE_SIDE_EFFECTS (expr1))
3284 /* The left-hand operand of a comma expression is like an expression
3285 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3286 any side-effects, unless it was explicitly cast to (void). */
3287 if (warn_unused_value)
3289 if (VOID_TYPE_P (TREE_TYPE (expr1))
3290 && TREE_CODE (expr1) == CONVERT_EXPR)
3291 ; /* (void) a, b */
3292 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3293 && TREE_CODE (expr1) == COMPOUND_EXPR
3294 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3295 ; /* (void) a, (void) b, c */
3296 else
3297 warning (0, "left-hand operand of comma expression has no effect");
3301 /* With -Wunused, we should also warn if the left-hand operand does have
3302 side-effects, but computes a value which is not used. For example, in
3303 `foo() + bar(), baz()' the result of the `+' operator is not used,
3304 so we should issue a warning. */
3305 else if (warn_unused_value)
3306 warn_if_unused_value (expr1, input_location);
3308 if (expr2 == error_mark_node)
3309 return error_mark_node;
3311 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3314 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3316 tree
3317 build_c_cast (tree type, tree expr)
3319 tree value = expr;
3321 if (type == error_mark_node || expr == error_mark_node)
3322 return error_mark_node;
3324 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3325 only in <protocol> qualifications. But when constructing cast expressions,
3326 the protocols do matter and must be kept around. */
3327 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3328 return build1 (NOP_EXPR, type, expr);
3330 type = TYPE_MAIN_VARIANT (type);
3332 if (TREE_CODE (type) == ARRAY_TYPE)
3334 error ("cast specifies array type");
3335 return error_mark_node;
3338 if (TREE_CODE (type) == FUNCTION_TYPE)
3340 error ("cast specifies function type");
3341 return error_mark_node;
3344 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3346 if (pedantic)
3348 if (TREE_CODE (type) == RECORD_TYPE
3349 || TREE_CODE (type) == UNION_TYPE)
3350 pedwarn ("ISO C forbids casting nonscalar to the same type");
3353 else if (TREE_CODE (type) == UNION_TYPE)
3355 tree field;
3357 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3358 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3359 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3360 break;
3362 if (field)
3364 tree t;
3366 if (pedantic)
3367 pedwarn ("ISO C forbids casts to union type");
3368 t = digest_init (type,
3369 build_constructor_single (type, field, value),
3370 true, 0);
3371 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3372 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3373 return t;
3375 error ("cast to union type from type not present in union");
3376 return error_mark_node;
3378 else
3380 tree otype, ovalue;
3382 if (type == void_type_node)
3383 return build1 (CONVERT_EXPR, type, value);
3385 otype = TREE_TYPE (value);
3387 /* Optionally warn about potentially worrisome casts. */
3389 if (warn_cast_qual
3390 && TREE_CODE (type) == POINTER_TYPE
3391 && TREE_CODE (otype) == POINTER_TYPE)
3393 tree in_type = type;
3394 tree in_otype = otype;
3395 int added = 0;
3396 int discarded = 0;
3398 /* Check that the qualifiers on IN_TYPE are a superset of
3399 the qualifiers of IN_OTYPE. The outermost level of
3400 POINTER_TYPE nodes is uninteresting and we stop as soon
3401 as we hit a non-POINTER_TYPE node on either type. */
3404 in_otype = TREE_TYPE (in_otype);
3405 in_type = TREE_TYPE (in_type);
3407 /* GNU C allows cv-qualified function types. 'const'
3408 means the function is very pure, 'volatile' means it
3409 can't return. We need to warn when such qualifiers
3410 are added, not when they're taken away. */
3411 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3412 && TREE_CODE (in_type) == FUNCTION_TYPE)
3413 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3414 else
3415 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3417 while (TREE_CODE (in_type) == POINTER_TYPE
3418 && TREE_CODE (in_otype) == POINTER_TYPE);
3420 if (added)
3421 warning (0, "cast adds new qualifiers to function type");
3423 if (discarded)
3424 /* There are qualifiers present in IN_OTYPE that are not
3425 present in IN_TYPE. */
3426 warning (0, "cast discards qualifiers from pointer target type");
3429 /* Warn about possible alignment problems. */
3430 if (STRICT_ALIGNMENT
3431 && TREE_CODE (type) == POINTER_TYPE
3432 && TREE_CODE (otype) == POINTER_TYPE
3433 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3434 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3435 /* Don't warn about opaque types, where the actual alignment
3436 restriction is unknown. */
3437 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3438 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3439 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3440 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3441 warning (OPT_Wcast_align,
3442 "cast increases required alignment of target type");
3444 if (TREE_CODE (type) == INTEGER_TYPE
3445 && TREE_CODE (otype) == POINTER_TYPE
3446 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3447 && !TREE_CONSTANT (value))
3448 warning (OPT_Wpointer_to_int_cast,
3449 "cast from pointer to integer of different size");
3451 if (TREE_CODE (value) == CALL_EXPR
3452 && TREE_CODE (type) != TREE_CODE (otype))
3453 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3454 "to non-matching type %qT", otype, type);
3456 if (TREE_CODE (type) == POINTER_TYPE
3457 && TREE_CODE (otype) == INTEGER_TYPE
3458 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3459 /* Don't warn about converting any constant. */
3460 && !TREE_CONSTANT (value))
3461 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3462 "of different size");
3464 strict_aliasing_warning (otype, type, expr);
3466 /* If pedantic, warn for conversions between function and object
3467 pointer types, except for converting a null pointer constant
3468 to function pointer type. */
3469 if (pedantic
3470 && TREE_CODE (type) == POINTER_TYPE
3471 && TREE_CODE (otype) == POINTER_TYPE
3472 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3473 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3474 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3476 if (pedantic
3477 && TREE_CODE (type) == POINTER_TYPE
3478 && TREE_CODE (otype) == POINTER_TYPE
3479 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3480 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3481 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3482 && TREE_CODE (expr) != NOP_EXPR))
3483 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3485 ovalue = value;
3486 value = convert (type, value);
3488 /* Ignore any integer overflow caused by the cast. */
3489 if (TREE_CODE (value) == INTEGER_CST)
3491 if (CONSTANT_CLASS_P (ovalue)
3492 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3494 /* Avoid clobbering a shared constant. */
3495 value = copy_node (value);
3496 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3497 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3499 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3500 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3501 value = build_int_cst_wide (TREE_TYPE (value),
3502 TREE_INT_CST_LOW (value),
3503 TREE_INT_CST_HIGH (value));
3507 /* Don't let a cast be an lvalue. */
3508 if (value == expr)
3509 value = non_lvalue (value);
3511 return value;
3514 /* Interpret a cast of expression EXPR to type TYPE. */
3515 tree
3516 c_cast_expr (struct c_type_name *type_name, tree expr)
3518 tree type;
3519 int saved_wsp = warn_strict_prototypes;
3521 /* This avoids warnings about unprototyped casts on
3522 integers. E.g. "#define SIG_DFL (void(*)())0". */
3523 if (TREE_CODE (expr) == INTEGER_CST)
3524 warn_strict_prototypes = 0;
3525 type = groktypename (type_name);
3526 warn_strict_prototypes = saved_wsp;
3528 return build_c_cast (type, expr);
3532 /* Build an assignment expression of lvalue LHS from value RHS.
3533 MODIFYCODE is the code for a binary operator that we use
3534 to combine the old value of LHS with RHS to get the new value.
3535 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3537 tree
3538 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3540 tree result;
3541 tree newrhs;
3542 tree lhstype = TREE_TYPE (lhs);
3543 tree olhstype = lhstype;
3545 /* Types that aren't fully specified cannot be used in assignments. */
3546 lhs = require_complete_type (lhs);
3548 /* Avoid duplicate error messages from operands that had errors. */
3549 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3550 return error_mark_node;
3552 if (!lvalue_or_else (lhs, lv_assign))
3553 return error_mark_node;
3555 STRIP_TYPE_NOPS (rhs);
3557 newrhs = rhs;
3559 /* If a binary op has been requested, combine the old LHS value with the RHS
3560 producing the value we should actually store into the LHS. */
3562 if (modifycode != NOP_EXPR)
3564 lhs = stabilize_reference (lhs);
3565 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3568 /* Give an error for storing in something that is 'const'. */
3570 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3571 || ((TREE_CODE (lhstype) == RECORD_TYPE
3572 || TREE_CODE (lhstype) == UNION_TYPE)
3573 && C_TYPE_FIELDS_READONLY (lhstype)))
3574 readonly_error (lhs, lv_assign);
3576 /* If storing into a structure or union member,
3577 it has probably been given type `int'.
3578 Compute the type that would go with
3579 the actual amount of storage the member occupies. */
3581 if (TREE_CODE (lhs) == COMPONENT_REF
3582 && (TREE_CODE (lhstype) == INTEGER_TYPE
3583 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3584 || TREE_CODE (lhstype) == REAL_TYPE
3585 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3586 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3588 /* If storing in a field that is in actuality a short or narrower than one,
3589 we must store in the field in its actual type. */
3591 if (lhstype != TREE_TYPE (lhs))
3593 lhs = copy_node (lhs);
3594 TREE_TYPE (lhs) = lhstype;
3597 /* Convert new value to destination type. */
3599 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3600 NULL_TREE, NULL_TREE, 0);
3601 if (TREE_CODE (newrhs) == ERROR_MARK)
3602 return error_mark_node;
3604 /* Emit ObjC write barrier, if necessary. */
3605 if (c_dialect_objc () && flag_objc_gc)
3607 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3608 if (result)
3609 return result;
3612 /* Scan operands. */
3614 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3615 TREE_SIDE_EFFECTS (result) = 1;
3617 /* If we got the LHS in a different type for storing in,
3618 convert the result back to the nominal type of LHS
3619 so that the value we return always has the same type
3620 as the LHS argument. */
3622 if (olhstype == TREE_TYPE (result))
3623 return result;
3624 return convert_for_assignment (olhstype, result, ic_assign,
3625 NULL_TREE, NULL_TREE, 0);
3628 /* Convert value RHS to type TYPE as preparation for an assignment
3629 to an lvalue of type TYPE.
3630 The real work of conversion is done by `convert'.
3631 The purpose of this function is to generate error messages
3632 for assignments that are not allowed in C.
3633 ERRTYPE says whether it is argument passing, assignment,
3634 initialization or return.
3636 FUNCTION is a tree for the function being called.
3637 PARMNUM is the number of the argument, for printing in error messages. */
3639 static tree
3640 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3641 tree fundecl, tree function, int parmnum)
3643 enum tree_code codel = TREE_CODE (type);
3644 tree rhstype;
3645 enum tree_code coder;
3646 tree rname = NULL_TREE;
3647 bool objc_ok = false;
3649 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3651 tree selector;
3652 /* Change pointer to function to the function itself for
3653 diagnostics. */
3654 if (TREE_CODE (function) == ADDR_EXPR
3655 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3656 function = TREE_OPERAND (function, 0);
3658 /* Handle an ObjC selector specially for diagnostics. */
3659 selector = objc_message_selector ();
3660 rname = function;
3661 if (selector && parmnum > 2)
3663 rname = selector;
3664 parmnum -= 2;
3668 /* This macro is used to emit diagnostics to ensure that all format
3669 strings are complete sentences, visible to gettext and checked at
3670 compile time. */
3671 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3672 do { \
3673 switch (errtype) \
3675 case ic_argpass: \
3676 pedwarn (AR, parmnum, rname); \
3677 break; \
3678 case ic_argpass_nonproto: \
3679 warning (0, AR, parmnum, rname); \
3680 break; \
3681 case ic_assign: \
3682 pedwarn (AS); \
3683 break; \
3684 case ic_init: \
3685 pedwarn (IN); \
3686 break; \
3687 case ic_return: \
3688 pedwarn (RE); \
3689 break; \
3690 default: \
3691 gcc_unreachable (); \
3693 } while (0)
3695 STRIP_TYPE_NOPS (rhs);
3697 if (optimize && TREE_CODE (rhs) == VAR_DECL
3698 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3699 rhs = decl_constant_value_for_broken_optimization (rhs);
3701 rhstype = TREE_TYPE (rhs);
3702 coder = TREE_CODE (rhstype);
3704 if (coder == ERROR_MARK)
3705 return error_mark_node;
3707 if (c_dialect_objc ())
3709 int parmno;
3711 switch (errtype)
3713 case ic_return:
3714 parmno = 0;
3715 break;
3717 case ic_assign:
3718 parmno = -1;
3719 break;
3721 case ic_init:
3722 parmno = -2;
3723 break;
3725 default:
3726 parmno = parmnum;
3727 break;
3730 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3733 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3735 overflow_warning (rhs);
3736 return rhs;
3739 if (coder == VOID_TYPE)
3741 /* Except for passing an argument to an unprototyped function,
3742 this is a constraint violation. When passing an argument to
3743 an unprototyped function, it is compile-time undefined;
3744 making it a constraint in that case was rejected in
3745 DR#252. */
3746 error ("void value not ignored as it ought to be");
3747 return error_mark_node;
3749 /* A type converts to a reference to it.
3750 This code doesn't fully support references, it's just for the
3751 special case of va_start and va_copy. */
3752 if (codel == REFERENCE_TYPE
3753 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3755 if (!lvalue_p (rhs))
3757 error ("cannot pass rvalue to reference parameter");
3758 return error_mark_node;
3760 if (!c_mark_addressable (rhs))
3761 return error_mark_node;
3762 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3764 /* We already know that these two types are compatible, but they
3765 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3766 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3767 likely to be va_list, a typedef to __builtin_va_list, which
3768 is different enough that it will cause problems later. */
3769 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3770 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3772 rhs = build1 (NOP_EXPR, type, rhs);
3773 return rhs;
3775 /* Some types can interconvert without explicit casts. */
3776 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3777 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3778 return convert (type, rhs);
3779 /* Arithmetic types all interconvert, and enum is treated like int. */
3780 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3781 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3782 || codel == BOOLEAN_TYPE)
3783 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3784 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3785 || coder == BOOLEAN_TYPE))
3786 return convert_and_check (type, rhs);
3788 /* Conversion to a transparent union from its member types.
3789 This applies only to function arguments. */
3790 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3791 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3793 tree memb, marginal_memb = NULL_TREE;
3795 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3797 tree memb_type = TREE_TYPE (memb);
3799 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3800 TYPE_MAIN_VARIANT (rhstype)))
3801 break;
3803 if (TREE_CODE (memb_type) != POINTER_TYPE)
3804 continue;
3806 if (coder == POINTER_TYPE)
3808 tree ttl = TREE_TYPE (memb_type);
3809 tree ttr = TREE_TYPE (rhstype);
3811 /* Any non-function converts to a [const][volatile] void *
3812 and vice versa; otherwise, targets must be the same.
3813 Meanwhile, the lhs target must have all the qualifiers of
3814 the rhs. */
3815 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3816 || comp_target_types (memb_type, rhstype))
3818 /* If this type won't generate any warnings, use it. */
3819 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3820 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3821 && TREE_CODE (ttl) == FUNCTION_TYPE)
3822 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3823 == TYPE_QUALS (ttr))
3824 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3825 == TYPE_QUALS (ttl))))
3826 break;
3828 /* Keep looking for a better type, but remember this one. */
3829 if (!marginal_memb)
3830 marginal_memb = memb;
3834 /* Can convert integer zero to any pointer type. */
3835 if (integer_zerop (rhs)
3836 || (TREE_CODE (rhs) == NOP_EXPR
3837 && integer_zerop (TREE_OPERAND (rhs, 0))))
3839 rhs = null_pointer_node;
3840 break;
3844 if (memb || marginal_memb)
3846 if (!memb)
3848 /* We have only a marginally acceptable member type;
3849 it needs a warning. */
3850 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3851 tree ttr = TREE_TYPE (rhstype);
3853 /* Const and volatile mean something different for function
3854 types, so the usual warnings are not appropriate. */
3855 if (TREE_CODE (ttr) == FUNCTION_TYPE
3856 && TREE_CODE (ttl) == FUNCTION_TYPE)
3858 /* Because const and volatile on functions are
3859 restrictions that say the function will not do
3860 certain things, it is okay to use a const or volatile
3861 function where an ordinary one is wanted, but not
3862 vice-versa. */
3863 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3864 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3865 "makes qualified function "
3866 "pointer from unqualified"),
3867 G_("assignment makes qualified "
3868 "function pointer from "
3869 "unqualified"),
3870 G_("initialization makes qualified "
3871 "function pointer from "
3872 "unqualified"),
3873 G_("return makes qualified function "
3874 "pointer from unqualified"));
3876 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3877 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3878 "qualifiers from pointer target type"),
3879 G_("assignment discards qualifiers "
3880 "from pointer target type"),
3881 G_("initialization discards qualifiers "
3882 "from pointer target type"),
3883 G_("return discards qualifiers from "
3884 "pointer target type"));
3886 memb = marginal_memb;
3889 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3890 pedwarn ("ISO C prohibits argument conversion to union type");
3892 return build_constructor_single (type, memb, rhs);
3896 /* Conversions among pointers */
3897 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3898 && (coder == codel))
3900 tree ttl = TREE_TYPE (type);
3901 tree ttr = TREE_TYPE (rhstype);
3902 tree mvl = ttl;
3903 tree mvr = ttr;
3904 bool is_opaque_pointer;
3905 int target_cmp = 0; /* Cache comp_target_types () result. */
3907 if (TREE_CODE (mvl) != ARRAY_TYPE)
3908 mvl = TYPE_MAIN_VARIANT (mvl);
3909 if (TREE_CODE (mvr) != ARRAY_TYPE)
3910 mvr = TYPE_MAIN_VARIANT (mvr);
3911 /* Opaque pointers are treated like void pointers. */
3912 is_opaque_pointer = (targetm.vector_opaque_p (type)
3913 || targetm.vector_opaque_p (rhstype))
3914 && TREE_CODE (ttl) == VECTOR_TYPE
3915 && TREE_CODE (ttr) == VECTOR_TYPE;
3917 /* C++ does not allow the implicit conversion void* -> T*. However,
3918 for the purpose of reducing the number of false positives, we
3919 tolerate the special case of
3921 int *p = NULL;
3923 where NULL is typically defined in C to be '(void *) 0'. */
3924 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
3925 warning (OPT_Wc___compat, "request for implicit conversion from "
3926 "%qT to %qT not permitted in C++", rhstype, type);
3928 /* Check if the right-hand side has a format attribute but the
3929 left-hand side doesn't. */
3930 if (warn_missing_format_attribute
3931 && check_missing_format_attribute (type, rhstype))
3933 switch (errtype)
3935 case ic_argpass:
3936 case ic_argpass_nonproto:
3937 warning (OPT_Wmissing_format_attribute,
3938 "argument %d of %qE might be "
3939 "a candidate for a format attribute",
3940 parmnum, rname);
3941 break;
3942 case ic_assign:
3943 warning (OPT_Wmissing_format_attribute,
3944 "assignment left-hand side might be "
3945 "a candidate for a format attribute");
3946 break;
3947 case ic_init:
3948 warning (OPT_Wmissing_format_attribute,
3949 "initialization left-hand side might be "
3950 "a candidate for a format attribute");
3951 break;
3952 case ic_return:
3953 warning (OPT_Wmissing_format_attribute,
3954 "return type might be "
3955 "a candidate for a format attribute");
3956 break;
3957 default:
3958 gcc_unreachable ();
3962 /* Any non-function converts to a [const][volatile] void *
3963 and vice versa; otherwise, targets must be the same.
3964 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3965 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3966 || (target_cmp = comp_target_types (type, rhstype))
3967 || is_opaque_pointer
3968 || (c_common_unsigned_type (mvl)
3969 == c_common_unsigned_type (mvr)))
3971 if (pedantic
3972 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3974 (VOID_TYPE_P (ttr)
3975 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3976 which are not ANSI null ptr constants. */
3977 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3978 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3979 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3980 "%qE between function pointer "
3981 "and %<void *%>"),
3982 G_("ISO C forbids assignment between "
3983 "function pointer and %<void *%>"),
3984 G_("ISO C forbids initialization between "
3985 "function pointer and %<void *%>"),
3986 G_("ISO C forbids return between function "
3987 "pointer and %<void *%>"));
3988 /* Const and volatile mean something different for function types,
3989 so the usual warnings are not appropriate. */
3990 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3991 && TREE_CODE (ttl) != FUNCTION_TYPE)
3993 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3995 /* Types differing only by the presence of the 'volatile'
3996 qualifier are acceptable if the 'volatile' has been added
3997 in by the Objective-C EH machinery. */
3998 if (!objc_type_quals_match (ttl, ttr))
3999 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4000 "qualifiers from pointer target type"),
4001 G_("assignment discards qualifiers "
4002 "from pointer target type"),
4003 G_("initialization discards qualifiers "
4004 "from pointer target type"),
4005 G_("return discards qualifiers from "
4006 "pointer target type"));
4008 /* If this is not a case of ignoring a mismatch in signedness,
4009 no warning. */
4010 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4011 || target_cmp)
4013 /* If there is a mismatch, do warn. */
4014 else if (warn_pointer_sign)
4015 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4016 "%d of %qE differ in signedness"),
4017 G_("pointer targets in assignment "
4018 "differ in signedness"),
4019 G_("pointer targets in initialization "
4020 "differ in signedness"),
4021 G_("pointer targets in return differ "
4022 "in signedness"));
4024 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4025 && TREE_CODE (ttr) == FUNCTION_TYPE)
4027 /* Because const and volatile on functions are restrictions
4028 that say the function will not do certain things,
4029 it is okay to use a const or volatile function
4030 where an ordinary one is wanted, but not vice-versa. */
4031 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4032 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4033 "qualified function pointer "
4034 "from unqualified"),
4035 G_("assignment makes qualified function "
4036 "pointer from unqualified"),
4037 G_("initialization makes qualified "
4038 "function pointer from unqualified"),
4039 G_("return makes qualified function "
4040 "pointer from unqualified"));
4043 else
4044 /* Avoid warning about the volatile ObjC EH puts on decls. */
4045 if (!objc_ok)
4046 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4047 "incompatible pointer type"),
4048 G_("assignment from incompatible pointer type"),
4049 G_("initialization from incompatible "
4050 "pointer type"),
4051 G_("return from incompatible pointer type"));
4053 return convert (type, rhs);
4055 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4057 /* ??? This should not be an error when inlining calls to
4058 unprototyped functions. */
4059 error ("invalid use of non-lvalue array");
4060 return error_mark_node;
4062 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4064 /* An explicit constant 0 can convert to a pointer,
4065 or one that results from arithmetic, even including
4066 a cast to integer type. */
4067 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4069 !(TREE_CODE (rhs) == NOP_EXPR
4070 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4071 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4072 && integer_zerop (TREE_OPERAND (rhs, 0))))
4073 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4074 "pointer from integer without a cast"),
4075 G_("assignment makes pointer from integer "
4076 "without a cast"),
4077 G_("initialization makes pointer from "
4078 "integer without a cast"),
4079 G_("return makes pointer from integer "
4080 "without a cast"));
4082 return convert (type, rhs);
4084 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4086 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4087 "from pointer without a cast"),
4088 G_("assignment makes integer from pointer "
4089 "without a cast"),
4090 G_("initialization makes integer from pointer "
4091 "without a cast"),
4092 G_("return makes integer from pointer "
4093 "without a cast"));
4094 return convert (type, rhs);
4096 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4097 return convert (type, rhs);
4099 switch (errtype)
4101 case ic_argpass:
4102 case ic_argpass_nonproto:
4103 /* ??? This should not be an error when inlining calls to
4104 unprototyped functions. */
4105 error ("incompatible type for argument %d of %qE", parmnum, rname);
4106 break;
4107 case ic_assign:
4108 error ("incompatible types in assignment");
4109 break;
4110 case ic_init:
4111 error ("incompatible types in initialization");
4112 break;
4113 case ic_return:
4114 error ("incompatible types in return");
4115 break;
4116 default:
4117 gcc_unreachable ();
4120 return error_mark_node;
4123 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4124 is used for error and waring reporting and indicates which argument
4125 is being processed. */
4127 tree
4128 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4130 tree ret, type;
4132 /* If FN was prototyped, the value has been converted already
4133 in convert_arguments. */
4134 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4135 return value;
4137 type = TREE_TYPE (parm);
4138 ret = convert_for_assignment (type, value,
4139 ic_argpass_nonproto, fn,
4140 fn, argnum);
4141 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4142 && INTEGRAL_TYPE_P (type)
4143 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4144 ret = default_conversion (ret);
4145 return ret;
4148 /* If VALUE is a compound expr all of whose expressions are constant, then
4149 return its value. Otherwise, return error_mark_node.
4151 This is for handling COMPOUND_EXPRs as initializer elements
4152 which is allowed with a warning when -pedantic is specified. */
4154 static tree
4155 valid_compound_expr_initializer (tree value, tree endtype)
4157 if (TREE_CODE (value) == COMPOUND_EXPR)
4159 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4160 == error_mark_node)
4161 return error_mark_node;
4162 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4163 endtype);
4165 else if (!initializer_constant_valid_p (value, endtype))
4166 return error_mark_node;
4167 else
4168 return value;
4171 /* Perform appropriate conversions on the initial value of a variable,
4172 store it in the declaration DECL,
4173 and print any error messages that are appropriate.
4174 If the init is invalid, store an ERROR_MARK. */
4176 void
4177 store_init_value (tree decl, tree init)
4179 tree value, type;
4181 /* If variable's type was invalidly declared, just ignore it. */
4183 type = TREE_TYPE (decl);
4184 if (TREE_CODE (type) == ERROR_MARK)
4185 return;
4187 /* Digest the specified initializer into an expression. */
4189 value = digest_init (type, init, true, TREE_STATIC (decl));
4191 /* Store the expression if valid; else report error. */
4193 if (!in_system_header
4194 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4195 warning (OPT_Wtraditional, "traditional C rejects automatic "
4196 "aggregate initialization");
4198 DECL_INITIAL (decl) = value;
4200 /* ANSI wants warnings about out-of-range constant initializers. */
4201 STRIP_TYPE_NOPS (value);
4202 constant_expression_warning (value);
4204 /* Check if we need to set array size from compound literal size. */
4205 if (TREE_CODE (type) == ARRAY_TYPE
4206 && TYPE_DOMAIN (type) == 0
4207 && value != error_mark_node)
4209 tree inside_init = init;
4211 STRIP_TYPE_NOPS (inside_init);
4212 inside_init = fold (inside_init);
4214 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4216 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4218 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4220 /* For int foo[] = (int [3]){1}; we need to set array size
4221 now since later on array initializer will be just the
4222 brace enclosed list of the compound literal. */
4223 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4224 TREE_TYPE (decl) = type;
4225 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4226 layout_type (type);
4227 layout_decl (cldecl, 0);
4233 /* Methods for storing and printing names for error messages. */
4235 /* Implement a spelling stack that allows components of a name to be pushed
4236 and popped. Each element on the stack is this structure. */
4238 struct spelling
4240 int kind;
4241 union
4243 unsigned HOST_WIDE_INT i;
4244 const char *s;
4245 } u;
4248 #define SPELLING_STRING 1
4249 #define SPELLING_MEMBER 2
4250 #define SPELLING_BOUNDS 3
4252 static struct spelling *spelling; /* Next stack element (unused). */
4253 static struct spelling *spelling_base; /* Spelling stack base. */
4254 static int spelling_size; /* Size of the spelling stack. */
4256 /* Macros to save and restore the spelling stack around push_... functions.
4257 Alternative to SAVE_SPELLING_STACK. */
4259 #define SPELLING_DEPTH() (spelling - spelling_base)
4260 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4262 /* Push an element on the spelling stack with type KIND and assign VALUE
4263 to MEMBER. */
4265 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4267 int depth = SPELLING_DEPTH (); \
4269 if (depth >= spelling_size) \
4271 spelling_size += 10; \
4272 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4273 spelling_size); \
4274 RESTORE_SPELLING_DEPTH (depth); \
4277 spelling->kind = (KIND); \
4278 spelling->MEMBER = (VALUE); \
4279 spelling++; \
4282 /* Push STRING on the stack. Printed literally. */
4284 static void
4285 push_string (const char *string)
4287 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4290 /* Push a member name on the stack. Printed as '.' STRING. */
4292 static void
4293 push_member_name (tree decl)
4295 const char *const string
4296 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4297 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4300 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4302 static void
4303 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4305 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4308 /* Compute the maximum size in bytes of the printed spelling. */
4310 static int
4311 spelling_length (void)
4313 int size = 0;
4314 struct spelling *p;
4316 for (p = spelling_base; p < spelling; p++)
4318 if (p->kind == SPELLING_BOUNDS)
4319 size += 25;
4320 else
4321 size += strlen (p->u.s) + 1;
4324 return size;
4327 /* Print the spelling to BUFFER and return it. */
4329 static char *
4330 print_spelling (char *buffer)
4332 char *d = buffer;
4333 struct spelling *p;
4335 for (p = spelling_base; p < spelling; p++)
4336 if (p->kind == SPELLING_BOUNDS)
4338 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4339 d += strlen (d);
4341 else
4343 const char *s;
4344 if (p->kind == SPELLING_MEMBER)
4345 *d++ = '.';
4346 for (s = p->u.s; (*d = *s++); d++)
4349 *d++ = '\0';
4350 return buffer;
4353 /* Issue an error message for a bad initializer component.
4354 MSGID identifies the message.
4355 The component name is taken from the spelling stack. */
4357 void
4358 error_init (const char *msgid)
4360 char *ofwhat;
4362 error ("%s", _(msgid));
4363 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4364 if (*ofwhat)
4365 error ("(near initialization for %qs)", ofwhat);
4368 /* Issue a pedantic warning for a bad initializer component.
4369 MSGID identifies the message.
4370 The component name is taken from the spelling stack. */
4372 void
4373 pedwarn_init (const char *msgid)
4375 char *ofwhat;
4377 pedwarn ("%s", _(msgid));
4378 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4379 if (*ofwhat)
4380 pedwarn ("(near initialization for %qs)", ofwhat);
4383 /* Issue a warning for a bad initializer component.
4384 MSGID identifies the message.
4385 The component name is taken from the spelling stack. */
4387 static void
4388 warning_init (const char *msgid)
4390 char *ofwhat;
4392 warning (0, "%s", _(msgid));
4393 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4394 if (*ofwhat)
4395 warning (0, "(near initialization for %qs)", ofwhat);
4398 /* If TYPE is an array type and EXPR is a parenthesized string
4399 constant, warn if pedantic that EXPR is being used to initialize an
4400 object of type TYPE. */
4402 void
4403 maybe_warn_string_init (tree type, struct c_expr expr)
4405 if (pedantic
4406 && TREE_CODE (type) == ARRAY_TYPE
4407 && TREE_CODE (expr.value) == STRING_CST
4408 && expr.original_code != STRING_CST)
4409 pedwarn_init ("array initialized from parenthesized string constant");
4412 /* Digest the parser output INIT as an initializer for type TYPE.
4413 Return a C expression of type TYPE to represent the initial value.
4415 If INIT is a string constant, STRICT_STRING is true if it is
4416 unparenthesized or we should not warn here for it being parenthesized.
4417 For other types of INIT, STRICT_STRING is not used.
4419 REQUIRE_CONSTANT requests an error if non-constant initializers or
4420 elements are seen. */
4422 static tree
4423 digest_init (tree type, tree init, bool strict_string, int require_constant)
4425 enum tree_code code = TREE_CODE (type);
4426 tree inside_init = init;
4428 if (type == error_mark_node
4429 || !init
4430 || init == error_mark_node
4431 || TREE_TYPE (init) == error_mark_node)
4432 return error_mark_node;
4434 STRIP_TYPE_NOPS (inside_init);
4436 inside_init = fold (inside_init);
4438 /* Initialization of an array of chars from a string constant
4439 optionally enclosed in braces. */
4441 if (code == ARRAY_TYPE && inside_init
4442 && TREE_CODE (inside_init) == STRING_CST)
4444 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4445 /* Note that an array could be both an array of character type
4446 and an array of wchar_t if wchar_t is signed char or unsigned
4447 char. */
4448 bool char_array = (typ1 == char_type_node
4449 || typ1 == signed_char_type_node
4450 || typ1 == unsigned_char_type_node);
4451 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4452 if (char_array || wchar_array)
4454 struct c_expr expr;
4455 bool char_string;
4456 expr.value = inside_init;
4457 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4458 maybe_warn_string_init (type, expr);
4460 char_string
4461 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4462 == char_type_node);
4464 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4465 TYPE_MAIN_VARIANT (type)))
4466 return inside_init;
4468 if (!wchar_array && !char_string)
4470 error_init ("char-array initialized from wide string");
4471 return error_mark_node;
4473 if (char_string && !char_array)
4475 error_init ("wchar_t-array initialized from non-wide string");
4476 return error_mark_node;
4479 TREE_TYPE (inside_init) = type;
4480 if (TYPE_DOMAIN (type) != 0
4481 && TYPE_SIZE (type) != 0
4482 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4483 /* Subtract 1 (or sizeof (wchar_t))
4484 because it's ok to ignore the terminating null char
4485 that is counted in the length of the constant. */
4486 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4487 TREE_STRING_LENGTH (inside_init)
4488 - ((TYPE_PRECISION (typ1)
4489 != TYPE_PRECISION (char_type_node))
4490 ? (TYPE_PRECISION (wchar_type_node)
4491 / BITS_PER_UNIT)
4492 : 1)))
4493 pedwarn_init ("initializer-string for array of chars is too long");
4495 return inside_init;
4497 else if (INTEGRAL_TYPE_P (typ1))
4499 error_init ("array of inappropriate type initialized "
4500 "from string constant");
4501 return error_mark_node;
4505 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4506 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4507 below and handle as a constructor. */
4508 if (code == VECTOR_TYPE
4509 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4510 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4511 && TREE_CONSTANT (inside_init))
4513 if (TREE_CODE (inside_init) == VECTOR_CST
4514 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4515 TYPE_MAIN_VARIANT (type)))
4516 return inside_init;
4518 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4520 unsigned HOST_WIDE_INT ix;
4521 tree value;
4522 bool constant_p = true;
4524 /* Iterate through elements and check if all constructor
4525 elements are *_CSTs. */
4526 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4527 if (!CONSTANT_CLASS_P (value))
4529 constant_p = false;
4530 break;
4533 if (constant_p)
4534 return build_vector_from_ctor (type,
4535 CONSTRUCTOR_ELTS (inside_init));
4539 /* Any type can be initialized
4540 from an expression of the same type, optionally with braces. */
4542 if (inside_init && TREE_TYPE (inside_init) != 0
4543 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4544 TYPE_MAIN_VARIANT (type))
4545 || (code == ARRAY_TYPE
4546 && comptypes (TREE_TYPE (inside_init), type))
4547 || (code == VECTOR_TYPE
4548 && comptypes (TREE_TYPE (inside_init), type))
4549 || (code == POINTER_TYPE
4550 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4551 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4552 TREE_TYPE (type)))))
4554 if (code == POINTER_TYPE)
4556 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4558 if (TREE_CODE (inside_init) == STRING_CST
4559 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4560 inside_init = array_to_pointer_conversion (inside_init);
4561 else
4563 error_init ("invalid use of non-lvalue array");
4564 return error_mark_node;
4569 if (code == VECTOR_TYPE)
4570 /* Although the types are compatible, we may require a
4571 conversion. */
4572 inside_init = convert (type, inside_init);
4574 if (require_constant
4575 && (code == VECTOR_TYPE || !flag_isoc99)
4576 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4578 /* As an extension, allow initializing objects with static storage
4579 duration with compound literals (which are then treated just as
4580 the brace enclosed list they contain). Also allow this for
4581 vectors, as we can only assign them with compound literals. */
4582 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4583 inside_init = DECL_INITIAL (decl);
4586 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4587 && TREE_CODE (inside_init) != CONSTRUCTOR)
4589 error_init ("array initialized from non-constant array expression");
4590 return error_mark_node;
4593 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4594 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4596 /* Compound expressions can only occur here if -pedantic or
4597 -pedantic-errors is specified. In the later case, we always want
4598 an error. In the former case, we simply want a warning. */
4599 if (require_constant && pedantic
4600 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4602 inside_init
4603 = valid_compound_expr_initializer (inside_init,
4604 TREE_TYPE (inside_init));
4605 if (inside_init == error_mark_node)
4606 error_init ("initializer element is not constant");
4607 else
4608 pedwarn_init ("initializer element is not constant");
4609 if (flag_pedantic_errors)
4610 inside_init = error_mark_node;
4612 else if (require_constant
4613 && !initializer_constant_valid_p (inside_init,
4614 TREE_TYPE (inside_init)))
4616 error_init ("initializer element is not constant");
4617 inside_init = error_mark_node;
4620 /* Added to enable additional -Wmissing-format-attribute warnings. */
4621 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4622 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4623 NULL_TREE, 0);
4624 return inside_init;
4627 /* Handle scalar types, including conversions. */
4629 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4630 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4631 || code == VECTOR_TYPE)
4633 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4634 && (TREE_CODE (init) == STRING_CST
4635 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4636 init = array_to_pointer_conversion (init);
4637 inside_init
4638 = convert_for_assignment (type, init, ic_init,
4639 NULL_TREE, NULL_TREE, 0);
4641 /* Check to see if we have already given an error message. */
4642 if (inside_init == error_mark_node)
4644 else if (require_constant && !TREE_CONSTANT (inside_init))
4646 error_init ("initializer element is not constant");
4647 inside_init = error_mark_node;
4649 else if (require_constant
4650 && !initializer_constant_valid_p (inside_init,
4651 TREE_TYPE (inside_init)))
4653 error_init ("initializer element is not computable at load time");
4654 inside_init = error_mark_node;
4657 return inside_init;
4660 /* Come here only for records and arrays. */
4662 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4664 error_init ("variable-sized object may not be initialized");
4665 return error_mark_node;
4668 error_init ("invalid initializer");
4669 return error_mark_node;
4672 /* Handle initializers that use braces. */
4674 /* Type of object we are accumulating a constructor for.
4675 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4676 static tree constructor_type;
4678 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4679 left to fill. */
4680 static tree constructor_fields;
4682 /* For an ARRAY_TYPE, this is the specified index
4683 at which to store the next element we get. */
4684 static tree constructor_index;
4686 /* For an ARRAY_TYPE, this is the maximum index. */
4687 static tree constructor_max_index;
4689 /* For a RECORD_TYPE, this is the first field not yet written out. */
4690 static tree constructor_unfilled_fields;
4692 /* For an ARRAY_TYPE, this is the index of the first element
4693 not yet written out. */
4694 static tree constructor_unfilled_index;
4696 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4697 This is so we can generate gaps between fields, when appropriate. */
4698 static tree constructor_bit_index;
4700 /* If we are saving up the elements rather than allocating them,
4701 this is the list of elements so far (in reverse order,
4702 most recent first). */
4703 static VEC(constructor_elt,gc) *constructor_elements;
4705 /* 1 if constructor should be incrementally stored into a constructor chain,
4706 0 if all the elements should be kept in AVL tree. */
4707 static int constructor_incremental;
4709 /* 1 if so far this constructor's elements are all compile-time constants. */
4710 static int constructor_constant;
4712 /* 1 if so far this constructor's elements are all valid address constants. */
4713 static int constructor_simple;
4715 /* 1 if this constructor is erroneous so far. */
4716 static int constructor_erroneous;
4718 /* Structure for managing pending initializer elements, organized as an
4719 AVL tree. */
4721 struct init_node
4723 struct init_node *left, *right;
4724 struct init_node *parent;
4725 int balance;
4726 tree purpose;
4727 tree value;
4730 /* Tree of pending elements at this constructor level.
4731 These are elements encountered out of order
4732 which belong at places we haven't reached yet in actually
4733 writing the output.
4734 Will never hold tree nodes across GC runs. */
4735 static struct init_node *constructor_pending_elts;
4737 /* The SPELLING_DEPTH of this constructor. */
4738 static int constructor_depth;
4740 /* DECL node for which an initializer is being read.
4741 0 means we are reading a constructor expression
4742 such as (struct foo) {...}. */
4743 static tree constructor_decl;
4745 /* Nonzero if this is an initializer for a top-level decl. */
4746 static int constructor_top_level;
4748 /* Nonzero if there were any member designators in this initializer. */
4749 static int constructor_designated;
4751 /* Nesting depth of designator list. */
4752 static int designator_depth;
4754 /* Nonzero if there were diagnosed errors in this designator list. */
4755 static int designator_erroneous;
4758 /* This stack has a level for each implicit or explicit level of
4759 structuring in the initializer, including the outermost one. It
4760 saves the values of most of the variables above. */
4762 struct constructor_range_stack;
4764 struct constructor_stack
4766 struct constructor_stack *next;
4767 tree type;
4768 tree fields;
4769 tree index;
4770 tree max_index;
4771 tree unfilled_index;
4772 tree unfilled_fields;
4773 tree bit_index;
4774 VEC(constructor_elt,gc) *elements;
4775 struct init_node *pending_elts;
4776 int offset;
4777 int depth;
4778 /* If value nonzero, this value should replace the entire
4779 constructor at this level. */
4780 struct c_expr replacement_value;
4781 struct constructor_range_stack *range_stack;
4782 char constant;
4783 char simple;
4784 char implicit;
4785 char erroneous;
4786 char outer;
4787 char incremental;
4788 char designated;
4791 static struct constructor_stack *constructor_stack;
4793 /* This stack represents designators from some range designator up to
4794 the last designator in the list. */
4796 struct constructor_range_stack
4798 struct constructor_range_stack *next, *prev;
4799 struct constructor_stack *stack;
4800 tree range_start;
4801 tree index;
4802 tree range_end;
4803 tree fields;
4806 static struct constructor_range_stack *constructor_range_stack;
4808 /* This stack records separate initializers that are nested.
4809 Nested initializers can't happen in ANSI C, but GNU C allows them
4810 in cases like { ... (struct foo) { ... } ... }. */
4812 struct initializer_stack
4814 struct initializer_stack *next;
4815 tree decl;
4816 struct constructor_stack *constructor_stack;
4817 struct constructor_range_stack *constructor_range_stack;
4818 VEC(constructor_elt,gc) *elements;
4819 struct spelling *spelling;
4820 struct spelling *spelling_base;
4821 int spelling_size;
4822 char top_level;
4823 char require_constant_value;
4824 char require_constant_elements;
4827 static struct initializer_stack *initializer_stack;
4829 /* Prepare to parse and output the initializer for variable DECL. */
4831 void
4832 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4834 const char *locus;
4835 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4837 p->decl = constructor_decl;
4838 p->require_constant_value = require_constant_value;
4839 p->require_constant_elements = require_constant_elements;
4840 p->constructor_stack = constructor_stack;
4841 p->constructor_range_stack = constructor_range_stack;
4842 p->elements = constructor_elements;
4843 p->spelling = spelling;
4844 p->spelling_base = spelling_base;
4845 p->spelling_size = spelling_size;
4846 p->top_level = constructor_top_level;
4847 p->next = initializer_stack;
4848 initializer_stack = p;
4850 constructor_decl = decl;
4851 constructor_designated = 0;
4852 constructor_top_level = top_level;
4854 if (decl != 0 && decl != error_mark_node)
4856 require_constant_value = TREE_STATIC (decl);
4857 require_constant_elements
4858 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4859 /* For a scalar, you can always use any value to initialize,
4860 even within braces. */
4861 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4862 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4863 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4864 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4865 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4867 else
4869 require_constant_value = 0;
4870 require_constant_elements = 0;
4871 locus = "(anonymous)";
4874 constructor_stack = 0;
4875 constructor_range_stack = 0;
4877 missing_braces_mentioned = 0;
4879 spelling_base = 0;
4880 spelling_size = 0;
4881 RESTORE_SPELLING_DEPTH (0);
4883 if (locus)
4884 push_string (locus);
4887 void
4888 finish_init (void)
4890 struct initializer_stack *p = initializer_stack;
4892 /* Free the whole constructor stack of this initializer. */
4893 while (constructor_stack)
4895 struct constructor_stack *q = constructor_stack;
4896 constructor_stack = q->next;
4897 free (q);
4900 gcc_assert (!constructor_range_stack);
4902 /* Pop back to the data of the outer initializer (if any). */
4903 free (spelling_base);
4905 constructor_decl = p->decl;
4906 require_constant_value = p->require_constant_value;
4907 require_constant_elements = p->require_constant_elements;
4908 constructor_stack = p->constructor_stack;
4909 constructor_range_stack = p->constructor_range_stack;
4910 constructor_elements = p->elements;
4911 spelling = p->spelling;
4912 spelling_base = p->spelling_base;
4913 spelling_size = p->spelling_size;
4914 constructor_top_level = p->top_level;
4915 initializer_stack = p->next;
4916 free (p);
4919 /* Call here when we see the initializer is surrounded by braces.
4920 This is instead of a call to push_init_level;
4921 it is matched by a call to pop_init_level.
4923 TYPE is the type to initialize, for a constructor expression.
4924 For an initializer for a decl, TYPE is zero. */
4926 void
4927 really_start_incremental_init (tree type)
4929 struct constructor_stack *p = XNEW (struct constructor_stack);
4931 if (type == 0)
4932 type = TREE_TYPE (constructor_decl);
4934 if (targetm.vector_opaque_p (type))
4935 error ("opaque vector types cannot be initialized");
4937 p->type = constructor_type;
4938 p->fields = constructor_fields;
4939 p->index = constructor_index;
4940 p->max_index = constructor_max_index;
4941 p->unfilled_index = constructor_unfilled_index;
4942 p->unfilled_fields = constructor_unfilled_fields;
4943 p->bit_index = constructor_bit_index;
4944 p->elements = constructor_elements;
4945 p->constant = constructor_constant;
4946 p->simple = constructor_simple;
4947 p->erroneous = constructor_erroneous;
4948 p->pending_elts = constructor_pending_elts;
4949 p->depth = constructor_depth;
4950 p->replacement_value.value = 0;
4951 p->replacement_value.original_code = ERROR_MARK;
4952 p->implicit = 0;
4953 p->range_stack = 0;
4954 p->outer = 0;
4955 p->incremental = constructor_incremental;
4956 p->designated = constructor_designated;
4957 p->next = 0;
4958 constructor_stack = p;
4960 constructor_constant = 1;
4961 constructor_simple = 1;
4962 constructor_depth = SPELLING_DEPTH ();
4963 constructor_elements = 0;
4964 constructor_pending_elts = 0;
4965 constructor_type = type;
4966 constructor_incremental = 1;
4967 constructor_designated = 0;
4968 designator_depth = 0;
4969 designator_erroneous = 0;
4971 if (TREE_CODE (constructor_type) == RECORD_TYPE
4972 || TREE_CODE (constructor_type) == UNION_TYPE)
4974 constructor_fields = TYPE_FIELDS (constructor_type);
4975 /* Skip any nameless bit fields at the beginning. */
4976 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4977 && DECL_NAME (constructor_fields) == 0)
4978 constructor_fields = TREE_CHAIN (constructor_fields);
4980 constructor_unfilled_fields = constructor_fields;
4981 constructor_bit_index = bitsize_zero_node;
4983 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4985 if (TYPE_DOMAIN (constructor_type))
4987 constructor_max_index
4988 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4990 /* Detect non-empty initializations of zero-length arrays. */
4991 if (constructor_max_index == NULL_TREE
4992 && TYPE_SIZE (constructor_type))
4993 constructor_max_index = build_int_cst (NULL_TREE, -1);
4995 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4996 to initialize VLAs will cause a proper error; avoid tree
4997 checking errors as well by setting a safe value. */
4998 if (constructor_max_index
4999 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5000 constructor_max_index = build_int_cst (NULL_TREE, -1);
5002 constructor_index
5003 = convert (bitsizetype,
5004 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5006 else
5008 constructor_index = bitsize_zero_node;
5009 constructor_max_index = NULL_TREE;
5012 constructor_unfilled_index = constructor_index;
5014 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5016 /* Vectors are like simple fixed-size arrays. */
5017 constructor_max_index =
5018 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5019 constructor_index = bitsize_zero_node;
5020 constructor_unfilled_index = constructor_index;
5022 else
5024 /* Handle the case of int x = {5}; */
5025 constructor_fields = constructor_type;
5026 constructor_unfilled_fields = constructor_type;
5030 /* Push down into a subobject, for initialization.
5031 If this is for an explicit set of braces, IMPLICIT is 0.
5032 If it is because the next element belongs at a lower level,
5033 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5035 void
5036 push_init_level (int implicit)
5038 struct constructor_stack *p;
5039 tree value = NULL_TREE;
5041 /* If we've exhausted any levels that didn't have braces,
5042 pop them now. If implicit == 1, this will have been done in
5043 process_init_element; do not repeat it here because in the case
5044 of excess initializers for an empty aggregate this leads to an
5045 infinite cycle of popping a level and immediately recreating
5046 it. */
5047 if (implicit != 1)
5049 while (constructor_stack->implicit)
5051 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5052 || TREE_CODE (constructor_type) == UNION_TYPE)
5053 && constructor_fields == 0)
5054 process_init_element (pop_init_level (1));
5055 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5056 && constructor_max_index
5057 && tree_int_cst_lt (constructor_max_index,
5058 constructor_index))
5059 process_init_element (pop_init_level (1));
5060 else
5061 break;
5065 /* Unless this is an explicit brace, we need to preserve previous
5066 content if any. */
5067 if (implicit)
5069 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5070 || TREE_CODE (constructor_type) == UNION_TYPE)
5071 && constructor_fields)
5072 value = find_init_member (constructor_fields);
5073 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5074 value = find_init_member (constructor_index);
5077 p = XNEW (struct constructor_stack);
5078 p->type = constructor_type;
5079 p->fields = constructor_fields;
5080 p->index = constructor_index;
5081 p->max_index = constructor_max_index;
5082 p->unfilled_index = constructor_unfilled_index;
5083 p->unfilled_fields = constructor_unfilled_fields;
5084 p->bit_index = constructor_bit_index;
5085 p->elements = constructor_elements;
5086 p->constant = constructor_constant;
5087 p->simple = constructor_simple;
5088 p->erroneous = constructor_erroneous;
5089 p->pending_elts = constructor_pending_elts;
5090 p->depth = constructor_depth;
5091 p->replacement_value.value = 0;
5092 p->replacement_value.original_code = ERROR_MARK;
5093 p->implicit = implicit;
5094 p->outer = 0;
5095 p->incremental = constructor_incremental;
5096 p->designated = constructor_designated;
5097 p->next = constructor_stack;
5098 p->range_stack = 0;
5099 constructor_stack = p;
5101 constructor_constant = 1;
5102 constructor_simple = 1;
5103 constructor_depth = SPELLING_DEPTH ();
5104 constructor_elements = 0;
5105 constructor_incremental = 1;
5106 constructor_designated = 0;
5107 constructor_pending_elts = 0;
5108 if (!implicit)
5110 p->range_stack = constructor_range_stack;
5111 constructor_range_stack = 0;
5112 designator_depth = 0;
5113 designator_erroneous = 0;
5116 /* Don't die if an entire brace-pair level is superfluous
5117 in the containing level. */
5118 if (constructor_type == 0)
5120 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5121 || TREE_CODE (constructor_type) == UNION_TYPE)
5123 /* Don't die if there are extra init elts at the end. */
5124 if (constructor_fields == 0)
5125 constructor_type = 0;
5126 else
5128 constructor_type = TREE_TYPE (constructor_fields);
5129 push_member_name (constructor_fields);
5130 constructor_depth++;
5133 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5135 constructor_type = TREE_TYPE (constructor_type);
5136 push_array_bounds (tree_low_cst (constructor_index, 1));
5137 constructor_depth++;
5140 if (constructor_type == 0)
5142 error_init ("extra brace group at end of initializer");
5143 constructor_fields = 0;
5144 constructor_unfilled_fields = 0;
5145 return;
5148 if (value && TREE_CODE (value) == CONSTRUCTOR)
5150 constructor_constant = TREE_CONSTANT (value);
5151 constructor_simple = TREE_STATIC (value);
5152 constructor_elements = CONSTRUCTOR_ELTS (value);
5153 if (!VEC_empty (constructor_elt, constructor_elements)
5154 && (TREE_CODE (constructor_type) == RECORD_TYPE
5155 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5156 set_nonincremental_init ();
5159 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5161 missing_braces_mentioned = 1;
5162 warning_init ("missing braces around initializer");
5165 if (TREE_CODE (constructor_type) == RECORD_TYPE
5166 || TREE_CODE (constructor_type) == UNION_TYPE)
5168 constructor_fields = TYPE_FIELDS (constructor_type);
5169 /* Skip any nameless bit fields at the beginning. */
5170 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5171 && DECL_NAME (constructor_fields) == 0)
5172 constructor_fields = TREE_CHAIN (constructor_fields);
5174 constructor_unfilled_fields = constructor_fields;
5175 constructor_bit_index = bitsize_zero_node;
5177 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5179 /* Vectors are like simple fixed-size arrays. */
5180 constructor_max_index =
5181 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5182 constructor_index = convert (bitsizetype, integer_zero_node);
5183 constructor_unfilled_index = constructor_index;
5185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5187 if (TYPE_DOMAIN (constructor_type))
5189 constructor_max_index
5190 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5192 /* Detect non-empty initializations of zero-length arrays. */
5193 if (constructor_max_index == NULL_TREE
5194 && TYPE_SIZE (constructor_type))
5195 constructor_max_index = build_int_cst (NULL_TREE, -1);
5197 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5198 to initialize VLAs will cause a proper error; avoid tree
5199 checking errors as well by setting a safe value. */
5200 if (constructor_max_index
5201 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5202 constructor_max_index = build_int_cst (NULL_TREE, -1);
5204 constructor_index
5205 = convert (bitsizetype,
5206 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5208 else
5209 constructor_index = bitsize_zero_node;
5211 constructor_unfilled_index = constructor_index;
5212 if (value && TREE_CODE (value) == STRING_CST)
5214 /* We need to split the char/wchar array into individual
5215 characters, so that we don't have to special case it
5216 everywhere. */
5217 set_nonincremental_init_from_string (value);
5220 else
5222 if (constructor_type != error_mark_node)
5223 warning_init ("braces around scalar initializer");
5224 constructor_fields = constructor_type;
5225 constructor_unfilled_fields = constructor_type;
5229 /* At the end of an implicit or explicit brace level,
5230 finish up that level of constructor. If a single expression
5231 with redundant braces initialized that level, return the
5232 c_expr structure for that expression. Otherwise, the original_code
5233 element is set to ERROR_MARK.
5234 If we were outputting the elements as they are read, return 0 as the value
5235 from inner levels (process_init_element ignores that),
5236 but return error_mark_node as the value from the outermost level
5237 (that's what we want to put in DECL_INITIAL).
5238 Otherwise, return a CONSTRUCTOR expression as the value. */
5240 struct c_expr
5241 pop_init_level (int implicit)
5243 struct constructor_stack *p;
5244 struct c_expr ret;
5245 ret.value = 0;
5246 ret.original_code = ERROR_MARK;
5248 if (implicit == 0)
5250 /* When we come to an explicit close brace,
5251 pop any inner levels that didn't have explicit braces. */
5252 while (constructor_stack->implicit)
5253 process_init_element (pop_init_level (1));
5255 gcc_assert (!constructor_range_stack);
5258 /* Now output all pending elements. */
5259 constructor_incremental = 1;
5260 output_pending_init_elements (1);
5262 p = constructor_stack;
5264 /* Error for initializing a flexible array member, or a zero-length
5265 array member in an inappropriate context. */
5266 if (constructor_type && constructor_fields
5267 && TREE_CODE (constructor_type) == ARRAY_TYPE
5268 && TYPE_DOMAIN (constructor_type)
5269 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5271 /* Silently discard empty initializations. The parser will
5272 already have pedwarned for empty brackets. */
5273 if (integer_zerop (constructor_unfilled_index))
5274 constructor_type = NULL_TREE;
5275 else
5277 gcc_assert (!TYPE_SIZE (constructor_type));
5279 if (constructor_depth > 2)
5280 error_init ("initialization of flexible array member in a nested context");
5281 else if (pedantic)
5282 pedwarn_init ("initialization of a flexible array member");
5284 /* We have already issued an error message for the existence
5285 of a flexible array member not at the end of the structure.
5286 Discard the initializer so that we do not die later. */
5287 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5288 constructor_type = NULL_TREE;
5292 /* Warn when some struct elements are implicitly initialized to zero. */
5293 if (warn_missing_field_initializers
5294 && constructor_type
5295 && TREE_CODE (constructor_type) == RECORD_TYPE
5296 && constructor_unfilled_fields)
5298 /* Do not warn for flexible array members or zero-length arrays. */
5299 while (constructor_unfilled_fields
5300 && (!DECL_SIZE (constructor_unfilled_fields)
5301 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5302 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5304 /* Do not warn if this level of the initializer uses member
5305 designators; it is likely to be deliberate. */
5306 if (constructor_unfilled_fields && !constructor_designated)
5308 push_member_name (constructor_unfilled_fields);
5309 warning_init ("missing initializer");
5310 RESTORE_SPELLING_DEPTH (constructor_depth);
5314 /* Pad out the end of the structure. */
5315 if (p->replacement_value.value)
5316 /* If this closes a superfluous brace pair,
5317 just pass out the element between them. */
5318 ret = p->replacement_value;
5319 else if (constructor_type == 0)
5321 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5322 && TREE_CODE (constructor_type) != UNION_TYPE
5323 && TREE_CODE (constructor_type) != ARRAY_TYPE
5324 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5326 /* A nonincremental scalar initializer--just return
5327 the element, after verifying there is just one. */
5328 if (VEC_empty (constructor_elt,constructor_elements))
5330 if (!constructor_erroneous)
5331 error_init ("empty scalar initializer");
5332 ret.value = error_mark_node;
5334 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5336 error_init ("extra elements in scalar initializer");
5337 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5339 else
5340 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5342 else
5344 if (constructor_erroneous)
5345 ret.value = error_mark_node;
5346 else
5348 ret.value = build_constructor (constructor_type,
5349 constructor_elements);
5350 if (constructor_constant)
5351 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5352 if (constructor_constant && constructor_simple)
5353 TREE_STATIC (ret.value) = 1;
5357 constructor_type = p->type;
5358 constructor_fields = p->fields;
5359 constructor_index = p->index;
5360 constructor_max_index = p->max_index;
5361 constructor_unfilled_index = p->unfilled_index;
5362 constructor_unfilled_fields = p->unfilled_fields;
5363 constructor_bit_index = p->bit_index;
5364 constructor_elements = p->elements;
5365 constructor_constant = p->constant;
5366 constructor_simple = p->simple;
5367 constructor_erroneous = p->erroneous;
5368 constructor_incremental = p->incremental;
5369 constructor_designated = p->designated;
5370 constructor_pending_elts = p->pending_elts;
5371 constructor_depth = p->depth;
5372 if (!p->implicit)
5373 constructor_range_stack = p->range_stack;
5374 RESTORE_SPELLING_DEPTH (constructor_depth);
5376 constructor_stack = p->next;
5377 free (p);
5379 if (ret.value == 0)
5381 if (constructor_stack == 0)
5383 ret.value = error_mark_node;
5384 return ret;
5386 return ret;
5388 return ret;
5391 /* Common handling for both array range and field name designators.
5392 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5394 static int
5395 set_designator (int array)
5397 tree subtype;
5398 enum tree_code subcode;
5400 /* Don't die if an entire brace-pair level is superfluous
5401 in the containing level. */
5402 if (constructor_type == 0)
5403 return 1;
5405 /* If there were errors in this designator list already, bail out
5406 silently. */
5407 if (designator_erroneous)
5408 return 1;
5410 if (!designator_depth)
5412 gcc_assert (!constructor_range_stack);
5414 /* Designator list starts at the level of closest explicit
5415 braces. */
5416 while (constructor_stack->implicit)
5417 process_init_element (pop_init_level (1));
5418 constructor_designated = 1;
5419 return 0;
5422 switch (TREE_CODE (constructor_type))
5424 case RECORD_TYPE:
5425 case UNION_TYPE:
5426 subtype = TREE_TYPE (constructor_fields);
5427 if (subtype != error_mark_node)
5428 subtype = TYPE_MAIN_VARIANT (subtype);
5429 break;
5430 case ARRAY_TYPE:
5431 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5432 break;
5433 default:
5434 gcc_unreachable ();
5437 subcode = TREE_CODE (subtype);
5438 if (array && subcode != ARRAY_TYPE)
5440 error_init ("array index in non-array initializer");
5441 return 1;
5443 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5445 error_init ("field name not in record or union initializer");
5446 return 1;
5449 constructor_designated = 1;
5450 push_init_level (2);
5451 return 0;
5454 /* If there are range designators in designator list, push a new designator
5455 to constructor_range_stack. RANGE_END is end of such stack range or
5456 NULL_TREE if there is no range designator at this level. */
5458 static void
5459 push_range_stack (tree range_end)
5461 struct constructor_range_stack *p;
5463 p = GGC_NEW (struct constructor_range_stack);
5464 p->prev = constructor_range_stack;
5465 p->next = 0;
5466 p->fields = constructor_fields;
5467 p->range_start = constructor_index;
5468 p->index = constructor_index;
5469 p->stack = constructor_stack;
5470 p->range_end = range_end;
5471 if (constructor_range_stack)
5472 constructor_range_stack->next = p;
5473 constructor_range_stack = p;
5476 /* Within an array initializer, specify the next index to be initialized.
5477 FIRST is that index. If LAST is nonzero, then initialize a range
5478 of indices, running from FIRST through LAST. */
5480 void
5481 set_init_index (tree first, tree last)
5483 if (set_designator (1))
5484 return;
5486 designator_erroneous = 1;
5488 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5489 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5491 error_init ("array index in initializer not of integer type");
5492 return;
5495 if (TREE_CODE (first) != INTEGER_CST)
5496 error_init ("nonconstant array index in initializer");
5497 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5498 error_init ("nonconstant array index in initializer");
5499 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5500 error_init ("array index in non-array initializer");
5501 else if (tree_int_cst_sgn (first) == -1)
5502 error_init ("array index in initializer exceeds array bounds");
5503 else if (constructor_max_index
5504 && tree_int_cst_lt (constructor_max_index, first))
5505 error_init ("array index in initializer exceeds array bounds");
5506 else
5508 constructor_index = convert (bitsizetype, first);
5510 if (last)
5512 if (tree_int_cst_equal (first, last))
5513 last = 0;
5514 else if (tree_int_cst_lt (last, first))
5516 error_init ("empty index range in initializer");
5517 last = 0;
5519 else
5521 last = convert (bitsizetype, last);
5522 if (constructor_max_index != 0
5523 && tree_int_cst_lt (constructor_max_index, last))
5525 error_init ("array index range in initializer exceeds array bounds");
5526 last = 0;
5531 designator_depth++;
5532 designator_erroneous = 0;
5533 if (constructor_range_stack || last)
5534 push_range_stack (last);
5538 /* Within a struct initializer, specify the next field to be initialized. */
5540 void
5541 set_init_label (tree fieldname)
5543 tree tail;
5545 if (set_designator (0))
5546 return;
5548 designator_erroneous = 1;
5550 if (TREE_CODE (constructor_type) != RECORD_TYPE
5551 && TREE_CODE (constructor_type) != UNION_TYPE)
5553 error_init ("field name not in record or union initializer");
5554 return;
5557 for (tail = TYPE_FIELDS (constructor_type); tail;
5558 tail = TREE_CHAIN (tail))
5560 if (DECL_NAME (tail) == fieldname)
5561 break;
5564 if (tail == 0)
5565 error ("unknown field %qE specified in initializer", fieldname);
5566 else
5568 constructor_fields = tail;
5569 designator_depth++;
5570 designator_erroneous = 0;
5571 if (constructor_range_stack)
5572 push_range_stack (NULL_TREE);
5576 /* Add a new initializer to the tree of pending initializers. PURPOSE
5577 identifies the initializer, either array index or field in a structure.
5578 VALUE is the value of that index or field. */
5580 static void
5581 add_pending_init (tree purpose, tree value)
5583 struct init_node *p, **q, *r;
5585 q = &constructor_pending_elts;
5586 p = 0;
5588 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5590 while (*q != 0)
5592 p = *q;
5593 if (tree_int_cst_lt (purpose, p->purpose))
5594 q = &p->left;
5595 else if (tree_int_cst_lt (p->purpose, purpose))
5596 q = &p->right;
5597 else
5599 if (TREE_SIDE_EFFECTS (p->value))
5600 warning_init ("initialized field with side-effects overwritten");
5601 p->value = value;
5602 return;
5606 else
5608 tree bitpos;
5610 bitpos = bit_position (purpose);
5611 while (*q != NULL)
5613 p = *q;
5614 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5615 q = &p->left;
5616 else if (p->purpose != purpose)
5617 q = &p->right;
5618 else
5620 if (TREE_SIDE_EFFECTS (p->value))
5621 warning_init ("initialized field with side-effects overwritten");
5622 p->value = value;
5623 return;
5628 r = GGC_NEW (struct init_node);
5629 r->purpose = purpose;
5630 r->value = value;
5632 *q = r;
5633 r->parent = p;
5634 r->left = 0;
5635 r->right = 0;
5636 r->balance = 0;
5638 while (p)
5640 struct init_node *s;
5642 if (r == p->left)
5644 if (p->balance == 0)
5645 p->balance = -1;
5646 else if (p->balance < 0)
5648 if (r->balance < 0)
5650 /* L rotation. */
5651 p->left = r->right;
5652 if (p->left)
5653 p->left->parent = p;
5654 r->right = p;
5656 p->balance = 0;
5657 r->balance = 0;
5659 s = p->parent;
5660 p->parent = r;
5661 r->parent = s;
5662 if (s)
5664 if (s->left == p)
5665 s->left = r;
5666 else
5667 s->right = r;
5669 else
5670 constructor_pending_elts = r;
5672 else
5674 /* LR rotation. */
5675 struct init_node *t = r->right;
5677 r->right = t->left;
5678 if (r->right)
5679 r->right->parent = r;
5680 t->left = r;
5682 p->left = t->right;
5683 if (p->left)
5684 p->left->parent = p;
5685 t->right = p;
5687 p->balance = t->balance < 0;
5688 r->balance = -(t->balance > 0);
5689 t->balance = 0;
5691 s = p->parent;
5692 p->parent = t;
5693 r->parent = t;
5694 t->parent = s;
5695 if (s)
5697 if (s->left == p)
5698 s->left = t;
5699 else
5700 s->right = t;
5702 else
5703 constructor_pending_elts = t;
5705 break;
5707 else
5709 /* p->balance == +1; growth of left side balances the node. */
5710 p->balance = 0;
5711 break;
5714 else /* r == p->right */
5716 if (p->balance == 0)
5717 /* Growth propagation from right side. */
5718 p->balance++;
5719 else if (p->balance > 0)
5721 if (r->balance > 0)
5723 /* R rotation. */
5724 p->right = r->left;
5725 if (p->right)
5726 p->right->parent = p;
5727 r->left = p;
5729 p->balance = 0;
5730 r->balance = 0;
5732 s = p->parent;
5733 p->parent = r;
5734 r->parent = s;
5735 if (s)
5737 if (s->left == p)
5738 s->left = r;
5739 else
5740 s->right = r;
5742 else
5743 constructor_pending_elts = r;
5745 else /* r->balance == -1 */
5747 /* RL rotation */
5748 struct init_node *t = r->left;
5750 r->left = t->right;
5751 if (r->left)
5752 r->left->parent = r;
5753 t->right = r;
5755 p->right = t->left;
5756 if (p->right)
5757 p->right->parent = p;
5758 t->left = p;
5760 r->balance = (t->balance < 0);
5761 p->balance = -(t->balance > 0);
5762 t->balance = 0;
5764 s = p->parent;
5765 p->parent = t;
5766 r->parent = t;
5767 t->parent = s;
5768 if (s)
5770 if (s->left == p)
5771 s->left = t;
5772 else
5773 s->right = t;
5775 else
5776 constructor_pending_elts = t;
5778 break;
5780 else
5782 /* p->balance == -1; growth of right side balances the node. */
5783 p->balance = 0;
5784 break;
5788 r = p;
5789 p = p->parent;
5793 /* Build AVL tree from a sorted chain. */
5795 static void
5796 set_nonincremental_init (void)
5798 unsigned HOST_WIDE_INT ix;
5799 tree index, value;
5801 if (TREE_CODE (constructor_type) != RECORD_TYPE
5802 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5803 return;
5805 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5806 add_pending_init (index, value);
5807 constructor_elements = 0;
5808 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5810 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5811 /* Skip any nameless bit fields at the beginning. */
5812 while (constructor_unfilled_fields != 0
5813 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5814 && DECL_NAME (constructor_unfilled_fields) == 0)
5815 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5818 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5820 if (TYPE_DOMAIN (constructor_type))
5821 constructor_unfilled_index
5822 = convert (bitsizetype,
5823 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5824 else
5825 constructor_unfilled_index = bitsize_zero_node;
5827 constructor_incremental = 0;
5830 /* Build AVL tree from a string constant. */
5832 static void
5833 set_nonincremental_init_from_string (tree str)
5835 tree value, purpose, type;
5836 HOST_WIDE_INT val[2];
5837 const char *p, *end;
5838 int byte, wchar_bytes, charwidth, bitpos;
5840 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5842 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5843 == TYPE_PRECISION (char_type_node))
5844 wchar_bytes = 1;
5845 else
5847 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5848 == TYPE_PRECISION (wchar_type_node));
5849 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5851 charwidth = TYPE_PRECISION (char_type_node);
5852 type = TREE_TYPE (constructor_type);
5853 p = TREE_STRING_POINTER (str);
5854 end = p + TREE_STRING_LENGTH (str);
5856 for (purpose = bitsize_zero_node;
5857 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5858 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5860 if (wchar_bytes == 1)
5862 val[1] = (unsigned char) *p++;
5863 val[0] = 0;
5865 else
5867 val[0] = 0;
5868 val[1] = 0;
5869 for (byte = 0; byte < wchar_bytes; byte++)
5871 if (BYTES_BIG_ENDIAN)
5872 bitpos = (wchar_bytes - byte - 1) * charwidth;
5873 else
5874 bitpos = byte * charwidth;
5875 val[bitpos < HOST_BITS_PER_WIDE_INT]
5876 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5877 << (bitpos % HOST_BITS_PER_WIDE_INT);
5881 if (!TYPE_UNSIGNED (type))
5883 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5884 if (bitpos < HOST_BITS_PER_WIDE_INT)
5886 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5888 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5889 val[0] = -1;
5892 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5894 if (val[1] < 0)
5895 val[0] = -1;
5897 else if (val[0] & (((HOST_WIDE_INT) 1)
5898 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5899 val[0] |= ((HOST_WIDE_INT) -1)
5900 << (bitpos - HOST_BITS_PER_WIDE_INT);
5903 value = build_int_cst_wide (type, val[1], val[0]);
5904 add_pending_init (purpose, value);
5907 constructor_incremental = 0;
5910 /* Return value of FIELD in pending initializer or zero if the field was
5911 not initialized yet. */
5913 static tree
5914 find_init_member (tree field)
5916 struct init_node *p;
5918 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5920 if (constructor_incremental
5921 && tree_int_cst_lt (field, constructor_unfilled_index))
5922 set_nonincremental_init ();
5924 p = constructor_pending_elts;
5925 while (p)
5927 if (tree_int_cst_lt (field, p->purpose))
5928 p = p->left;
5929 else if (tree_int_cst_lt (p->purpose, field))
5930 p = p->right;
5931 else
5932 return p->value;
5935 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5937 tree bitpos = bit_position (field);
5939 if (constructor_incremental
5940 && (!constructor_unfilled_fields
5941 || tree_int_cst_lt (bitpos,
5942 bit_position (constructor_unfilled_fields))))
5943 set_nonincremental_init ();
5945 p = constructor_pending_elts;
5946 while (p)
5948 if (field == p->purpose)
5949 return p->value;
5950 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5951 p = p->left;
5952 else
5953 p = p->right;
5956 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5958 if (!VEC_empty (constructor_elt, constructor_elements)
5959 && (VEC_last (constructor_elt, constructor_elements)->index
5960 == field))
5961 return VEC_last (constructor_elt, constructor_elements)->value;
5963 return 0;
5966 /* "Output" the next constructor element.
5967 At top level, really output it to assembler code now.
5968 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5969 TYPE is the data type that the containing data type wants here.
5970 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5971 If VALUE is a string constant, STRICT_STRING is true if it is
5972 unparenthesized or we should not warn here for it being parenthesized.
5973 For other types of VALUE, STRICT_STRING is not used.
5975 PENDING if non-nil means output pending elements that belong
5976 right after this element. (PENDING is normally 1;
5977 it is 0 while outputting pending elements, to avoid recursion.) */
5979 static void
5980 output_init_element (tree value, bool strict_string, tree type, tree field,
5981 int pending)
5983 constructor_elt *celt;
5985 if (type == error_mark_node || value == error_mark_node)
5987 constructor_erroneous = 1;
5988 return;
5990 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5991 && (TREE_CODE (value) == STRING_CST
5992 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
5993 && !(TREE_CODE (value) == STRING_CST
5994 && TREE_CODE (type) == ARRAY_TYPE
5995 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5996 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5997 TYPE_MAIN_VARIANT (type)))
5998 value = array_to_pointer_conversion (value);
6000 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6001 && require_constant_value && !flag_isoc99 && pending)
6003 /* As an extension, allow initializing objects with static storage
6004 duration with compound literals (which are then treated just as
6005 the brace enclosed list they contain). */
6006 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6007 value = DECL_INITIAL (decl);
6010 if (value == error_mark_node)
6011 constructor_erroneous = 1;
6012 else if (!TREE_CONSTANT (value))
6013 constructor_constant = 0;
6014 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6015 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6016 || TREE_CODE (constructor_type) == UNION_TYPE)
6017 && DECL_C_BIT_FIELD (field)
6018 && TREE_CODE (value) != INTEGER_CST))
6019 constructor_simple = 0;
6021 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6023 if (require_constant_value)
6025 error_init ("initializer element is not constant");
6026 value = error_mark_node;
6028 else if (require_constant_elements)
6029 pedwarn ("initializer element is not computable at load time");
6032 /* If this field is empty (and not at the end of structure),
6033 don't do anything other than checking the initializer. */
6034 if (field
6035 && (TREE_TYPE (field) == error_mark_node
6036 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6037 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6038 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6039 || TREE_CHAIN (field)))))
6040 return;
6042 value = digest_init (type, value, strict_string, require_constant_value);
6043 if (value == error_mark_node)
6045 constructor_erroneous = 1;
6046 return;
6049 /* If this element doesn't come next in sequence,
6050 put it on constructor_pending_elts. */
6051 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6052 && (!constructor_incremental
6053 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6055 if (constructor_incremental
6056 && tree_int_cst_lt (field, constructor_unfilled_index))
6057 set_nonincremental_init ();
6059 add_pending_init (field, value);
6060 return;
6062 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6063 && (!constructor_incremental
6064 || field != constructor_unfilled_fields))
6066 /* We do this for records but not for unions. In a union,
6067 no matter which field is specified, it can be initialized
6068 right away since it starts at the beginning of the union. */
6069 if (constructor_incremental)
6071 if (!constructor_unfilled_fields)
6072 set_nonincremental_init ();
6073 else
6075 tree bitpos, unfillpos;
6077 bitpos = bit_position (field);
6078 unfillpos = bit_position (constructor_unfilled_fields);
6080 if (tree_int_cst_lt (bitpos, unfillpos))
6081 set_nonincremental_init ();
6085 add_pending_init (field, value);
6086 return;
6088 else if (TREE_CODE (constructor_type) == UNION_TYPE
6089 && !VEC_empty (constructor_elt, constructor_elements))
6091 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6092 constructor_elements)->value))
6093 warning_init ("initialized field with side-effects overwritten");
6095 /* We can have just one union field set. */
6096 constructor_elements = 0;
6099 /* Otherwise, output this element either to
6100 constructor_elements or to the assembler file. */
6102 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6103 celt->index = field;
6104 celt->value = value;
6106 /* Advance the variable that indicates sequential elements output. */
6107 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6108 constructor_unfilled_index
6109 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6110 bitsize_one_node);
6111 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6113 constructor_unfilled_fields
6114 = TREE_CHAIN (constructor_unfilled_fields);
6116 /* Skip any nameless bit fields. */
6117 while (constructor_unfilled_fields != 0
6118 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6119 && DECL_NAME (constructor_unfilled_fields) == 0)
6120 constructor_unfilled_fields =
6121 TREE_CHAIN (constructor_unfilled_fields);
6123 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6124 constructor_unfilled_fields = 0;
6126 /* Now output any pending elements which have become next. */
6127 if (pending)
6128 output_pending_init_elements (0);
6131 /* Output any pending elements which have become next.
6132 As we output elements, constructor_unfilled_{fields,index}
6133 advances, which may cause other elements to become next;
6134 if so, they too are output.
6136 If ALL is 0, we return when there are
6137 no more pending elements to output now.
6139 If ALL is 1, we output space as necessary so that
6140 we can output all the pending elements. */
6142 static void
6143 output_pending_init_elements (int all)
6145 struct init_node *elt = constructor_pending_elts;
6146 tree next;
6148 retry:
6150 /* Look through the whole pending tree.
6151 If we find an element that should be output now,
6152 output it. Otherwise, set NEXT to the element
6153 that comes first among those still pending. */
6155 next = 0;
6156 while (elt)
6158 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6160 if (tree_int_cst_equal (elt->purpose,
6161 constructor_unfilled_index))
6162 output_init_element (elt->value, true,
6163 TREE_TYPE (constructor_type),
6164 constructor_unfilled_index, 0);
6165 else if (tree_int_cst_lt (constructor_unfilled_index,
6166 elt->purpose))
6168 /* Advance to the next smaller node. */
6169 if (elt->left)
6170 elt = elt->left;
6171 else
6173 /* We have reached the smallest node bigger than the
6174 current unfilled index. Fill the space first. */
6175 next = elt->purpose;
6176 break;
6179 else
6181 /* Advance to the next bigger node. */
6182 if (elt->right)
6183 elt = elt->right;
6184 else
6186 /* We have reached the biggest node in a subtree. Find
6187 the parent of it, which is the next bigger node. */
6188 while (elt->parent && elt->parent->right == elt)
6189 elt = elt->parent;
6190 elt = elt->parent;
6191 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6192 elt->purpose))
6194 next = elt->purpose;
6195 break;
6200 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6201 || TREE_CODE (constructor_type) == UNION_TYPE)
6203 tree ctor_unfilled_bitpos, elt_bitpos;
6205 /* If the current record is complete we are done. */
6206 if (constructor_unfilled_fields == 0)
6207 break;
6209 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6210 elt_bitpos = bit_position (elt->purpose);
6211 /* We can't compare fields here because there might be empty
6212 fields in between. */
6213 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6215 constructor_unfilled_fields = elt->purpose;
6216 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6217 elt->purpose, 0);
6219 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6221 /* Advance to the next smaller node. */
6222 if (elt->left)
6223 elt = elt->left;
6224 else
6226 /* We have reached the smallest node bigger than the
6227 current unfilled field. Fill the space first. */
6228 next = elt->purpose;
6229 break;
6232 else
6234 /* Advance to the next bigger node. */
6235 if (elt->right)
6236 elt = elt->right;
6237 else
6239 /* We have reached the biggest node in a subtree. Find
6240 the parent of it, which is the next bigger node. */
6241 while (elt->parent && elt->parent->right == elt)
6242 elt = elt->parent;
6243 elt = elt->parent;
6244 if (elt
6245 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6246 bit_position (elt->purpose))))
6248 next = elt->purpose;
6249 break;
6256 /* Ordinarily return, but not if we want to output all
6257 and there are elements left. */
6258 if (!(all && next != 0))
6259 return;
6261 /* If it's not incremental, just skip over the gap, so that after
6262 jumping to retry we will output the next successive element. */
6263 if (TREE_CODE (constructor_type) == RECORD_TYPE
6264 || TREE_CODE (constructor_type) == UNION_TYPE)
6265 constructor_unfilled_fields = next;
6266 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6267 constructor_unfilled_index = next;
6269 /* ELT now points to the node in the pending tree with the next
6270 initializer to output. */
6271 goto retry;
6274 /* Add one non-braced element to the current constructor level.
6275 This adjusts the current position within the constructor's type.
6276 This may also start or terminate implicit levels
6277 to handle a partly-braced initializer.
6279 Once this has found the correct level for the new element,
6280 it calls output_init_element. */
6282 void
6283 process_init_element (struct c_expr value)
6285 tree orig_value = value.value;
6286 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6287 bool strict_string = value.original_code == STRING_CST;
6289 designator_depth = 0;
6290 designator_erroneous = 0;
6292 /* Handle superfluous braces around string cst as in
6293 char x[] = {"foo"}; */
6294 if (string_flag
6295 && constructor_type
6296 && TREE_CODE (constructor_type) == ARRAY_TYPE
6297 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6298 && integer_zerop (constructor_unfilled_index))
6300 if (constructor_stack->replacement_value.value)
6301 error_init ("excess elements in char array initializer");
6302 constructor_stack->replacement_value = value;
6303 return;
6306 if (constructor_stack->replacement_value.value != 0)
6308 error_init ("excess elements in struct initializer");
6309 return;
6312 /* Ignore elements of a brace group if it is entirely superfluous
6313 and has already been diagnosed. */
6314 if (constructor_type == 0)
6315 return;
6317 /* If we've exhausted any levels that didn't have braces,
6318 pop them now. */
6319 while (constructor_stack->implicit)
6321 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6322 || TREE_CODE (constructor_type) == UNION_TYPE)
6323 && constructor_fields == 0)
6324 process_init_element (pop_init_level (1));
6325 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6326 && (constructor_max_index == 0
6327 || tree_int_cst_lt (constructor_max_index,
6328 constructor_index)))
6329 process_init_element (pop_init_level (1));
6330 else
6331 break;
6334 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6335 if (constructor_range_stack)
6337 /* If value is a compound literal and we'll be just using its
6338 content, don't put it into a SAVE_EXPR. */
6339 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6340 || !require_constant_value
6341 || flag_isoc99)
6342 value.value = save_expr (value.value);
6345 while (1)
6347 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6349 tree fieldtype;
6350 enum tree_code fieldcode;
6352 if (constructor_fields == 0)
6354 pedwarn_init ("excess elements in struct initializer");
6355 break;
6358 fieldtype = TREE_TYPE (constructor_fields);
6359 if (fieldtype != error_mark_node)
6360 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6361 fieldcode = TREE_CODE (fieldtype);
6363 /* Error for non-static initialization of a flexible array member. */
6364 if (fieldcode == ARRAY_TYPE
6365 && !require_constant_value
6366 && TYPE_SIZE (fieldtype) == NULL_TREE
6367 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6369 error_init ("non-static initialization of a flexible array member");
6370 break;
6373 /* Accept a string constant to initialize a subarray. */
6374 if (value.value != 0
6375 && fieldcode == ARRAY_TYPE
6376 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6377 && string_flag)
6378 value.value = orig_value;
6379 /* Otherwise, if we have come to a subaggregate,
6380 and we don't have an element of its type, push into it. */
6381 else if (value.value != 0
6382 && value.value != error_mark_node
6383 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6384 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6385 || fieldcode == UNION_TYPE))
6387 push_init_level (1);
6388 continue;
6391 if (value.value)
6393 push_member_name (constructor_fields);
6394 output_init_element (value.value, strict_string,
6395 fieldtype, constructor_fields, 1);
6396 RESTORE_SPELLING_DEPTH (constructor_depth);
6398 else
6399 /* Do the bookkeeping for an element that was
6400 directly output as a constructor. */
6402 /* For a record, keep track of end position of last field. */
6403 if (DECL_SIZE (constructor_fields))
6404 constructor_bit_index
6405 = size_binop (PLUS_EXPR,
6406 bit_position (constructor_fields),
6407 DECL_SIZE (constructor_fields));
6409 /* If the current field was the first one not yet written out,
6410 it isn't now, so update. */
6411 if (constructor_unfilled_fields == constructor_fields)
6413 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6414 /* Skip any nameless bit fields. */
6415 while (constructor_unfilled_fields != 0
6416 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6417 && DECL_NAME (constructor_unfilled_fields) == 0)
6418 constructor_unfilled_fields =
6419 TREE_CHAIN (constructor_unfilled_fields);
6423 constructor_fields = TREE_CHAIN (constructor_fields);
6424 /* Skip any nameless bit fields at the beginning. */
6425 while (constructor_fields != 0
6426 && DECL_C_BIT_FIELD (constructor_fields)
6427 && DECL_NAME (constructor_fields) == 0)
6428 constructor_fields = TREE_CHAIN (constructor_fields);
6430 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6432 tree fieldtype;
6433 enum tree_code fieldcode;
6435 if (constructor_fields == 0)
6437 pedwarn_init ("excess elements in union initializer");
6438 break;
6441 fieldtype = TREE_TYPE (constructor_fields);
6442 if (fieldtype != error_mark_node)
6443 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6444 fieldcode = TREE_CODE (fieldtype);
6446 /* Warn that traditional C rejects initialization of unions.
6447 We skip the warning if the value is zero. This is done
6448 under the assumption that the zero initializer in user
6449 code appears conditioned on e.g. __STDC__ to avoid
6450 "missing initializer" warnings and relies on default
6451 initialization to zero in the traditional C case.
6452 We also skip the warning if the initializer is designated,
6453 again on the assumption that this must be conditional on
6454 __STDC__ anyway (and we've already complained about the
6455 member-designator already). */
6456 if (!in_system_header && !constructor_designated
6457 && !(value.value && (integer_zerop (value.value)
6458 || real_zerop (value.value))))
6459 warning (OPT_Wtraditional, "traditional C rejects initialization "
6460 "of unions");
6462 /* Accept a string constant to initialize a subarray. */
6463 if (value.value != 0
6464 && fieldcode == ARRAY_TYPE
6465 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6466 && string_flag)
6467 value.value = orig_value;
6468 /* Otherwise, if we have come to a subaggregate,
6469 and we don't have an element of its type, push into it. */
6470 else if (value.value != 0
6471 && value.value != error_mark_node
6472 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6473 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6474 || fieldcode == UNION_TYPE))
6476 push_init_level (1);
6477 continue;
6480 if (value.value)
6482 push_member_name (constructor_fields);
6483 output_init_element (value.value, strict_string,
6484 fieldtype, constructor_fields, 1);
6485 RESTORE_SPELLING_DEPTH (constructor_depth);
6487 else
6488 /* Do the bookkeeping for an element that was
6489 directly output as a constructor. */
6491 constructor_bit_index = DECL_SIZE (constructor_fields);
6492 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6495 constructor_fields = 0;
6497 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6499 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6500 enum tree_code eltcode = TREE_CODE (elttype);
6502 /* Accept a string constant to initialize a subarray. */
6503 if (value.value != 0
6504 && eltcode == ARRAY_TYPE
6505 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6506 && string_flag)
6507 value.value = orig_value;
6508 /* Otherwise, if we have come to a subaggregate,
6509 and we don't have an element of its type, push into it. */
6510 else if (value.value != 0
6511 && value.value != error_mark_node
6512 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6513 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6514 || eltcode == UNION_TYPE))
6516 push_init_level (1);
6517 continue;
6520 if (constructor_max_index != 0
6521 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6522 || integer_all_onesp (constructor_max_index)))
6524 pedwarn_init ("excess elements in array initializer");
6525 break;
6528 /* Now output the actual element. */
6529 if (value.value)
6531 push_array_bounds (tree_low_cst (constructor_index, 1));
6532 output_init_element (value.value, strict_string,
6533 elttype, constructor_index, 1);
6534 RESTORE_SPELLING_DEPTH (constructor_depth);
6537 constructor_index
6538 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6540 if (!value.value)
6541 /* If we are doing the bookkeeping for an element that was
6542 directly output as a constructor, we must update
6543 constructor_unfilled_index. */
6544 constructor_unfilled_index = constructor_index;
6546 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6548 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6550 /* Do a basic check of initializer size. Note that vectors
6551 always have a fixed size derived from their type. */
6552 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6554 pedwarn_init ("excess elements in vector initializer");
6555 break;
6558 /* Now output the actual element. */
6559 if (value.value)
6560 output_init_element (value.value, strict_string,
6561 elttype, constructor_index, 1);
6563 constructor_index
6564 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6566 if (!value.value)
6567 /* If we are doing the bookkeeping for an element that was
6568 directly output as a constructor, we must update
6569 constructor_unfilled_index. */
6570 constructor_unfilled_index = constructor_index;
6573 /* Handle the sole element allowed in a braced initializer
6574 for a scalar variable. */
6575 else if (constructor_type != error_mark_node
6576 && constructor_fields == 0)
6578 pedwarn_init ("excess elements in scalar initializer");
6579 break;
6581 else
6583 if (value.value)
6584 output_init_element (value.value, strict_string,
6585 constructor_type, NULL_TREE, 1);
6586 constructor_fields = 0;
6589 /* Handle range initializers either at this level or anywhere higher
6590 in the designator stack. */
6591 if (constructor_range_stack)
6593 struct constructor_range_stack *p, *range_stack;
6594 int finish = 0;
6596 range_stack = constructor_range_stack;
6597 constructor_range_stack = 0;
6598 while (constructor_stack != range_stack->stack)
6600 gcc_assert (constructor_stack->implicit);
6601 process_init_element (pop_init_level (1));
6603 for (p = range_stack;
6604 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6605 p = p->prev)
6607 gcc_assert (constructor_stack->implicit);
6608 process_init_element (pop_init_level (1));
6611 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6612 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6613 finish = 1;
6615 while (1)
6617 constructor_index = p->index;
6618 constructor_fields = p->fields;
6619 if (finish && p->range_end && p->index == p->range_start)
6621 finish = 0;
6622 p->prev = 0;
6624 p = p->next;
6625 if (!p)
6626 break;
6627 push_init_level (2);
6628 p->stack = constructor_stack;
6629 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6630 p->index = p->range_start;
6633 if (!finish)
6634 constructor_range_stack = range_stack;
6635 continue;
6638 break;
6641 constructor_range_stack = 0;
6644 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6645 (guaranteed to be 'volatile' or null) and ARGS (represented using
6646 an ASM_EXPR node). */
6647 tree
6648 build_asm_stmt (tree cv_qualifier, tree args)
6650 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6651 ASM_VOLATILE_P (args) = 1;
6652 return add_stmt (args);
6655 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6656 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6657 SIMPLE indicates whether there was anything at all after the
6658 string in the asm expression -- asm("blah") and asm("blah" : )
6659 are subtly different. We use a ASM_EXPR node to represent this. */
6660 tree
6661 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6662 bool simple)
6664 tree tail;
6665 tree args;
6666 int i;
6667 const char *constraint;
6668 const char **oconstraints;
6669 bool allows_mem, allows_reg, is_inout;
6670 int ninputs, noutputs;
6672 ninputs = list_length (inputs);
6673 noutputs = list_length (outputs);
6674 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6676 string = resolve_asm_operand_names (string, outputs, inputs);
6678 /* Remove output conversions that change the type but not the mode. */
6679 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6681 tree output = TREE_VALUE (tail);
6683 /* ??? Really, this should not be here. Users should be using a
6684 proper lvalue, dammit. But there's a long history of using casts
6685 in the output operands. In cases like longlong.h, this becomes a
6686 primitive form of typechecking -- if the cast can be removed, then
6687 the output operand had a type of the proper width; otherwise we'll
6688 get an error. Gross, but ... */
6689 STRIP_NOPS (output);
6691 if (!lvalue_or_else (output, lv_asm))
6692 output = error_mark_node;
6694 if (output != error_mark_node
6695 && (TREE_READONLY (output)
6696 || TYPE_READONLY (TREE_TYPE (output))
6697 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6698 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6699 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6700 readonly_error (output, lv_asm);
6702 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6703 oconstraints[i] = constraint;
6705 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6706 &allows_mem, &allows_reg, &is_inout))
6708 /* If the operand is going to end up in memory,
6709 mark it addressable. */
6710 if (!allows_reg && !c_mark_addressable (output))
6711 output = error_mark_node;
6713 else
6714 output = error_mark_node;
6716 TREE_VALUE (tail) = output;
6719 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6721 tree input;
6723 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6724 input = TREE_VALUE (tail);
6726 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6727 oconstraints, &allows_mem, &allows_reg))
6729 /* If the operand is going to end up in memory,
6730 mark it addressable. */
6731 if (!allows_reg && allows_mem)
6733 /* Strip the nops as we allow this case. FIXME, this really
6734 should be rejected or made deprecated. */
6735 STRIP_NOPS (input);
6736 if (!c_mark_addressable (input))
6737 input = error_mark_node;
6740 else
6741 input = error_mark_node;
6743 TREE_VALUE (tail) = input;
6746 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6748 /* asm statements without outputs, including simple ones, are treated
6749 as volatile. */
6750 ASM_INPUT_P (args) = simple;
6751 ASM_VOLATILE_P (args) = (noutputs == 0);
6753 return args;
6756 /* Generate a goto statement to LABEL. */
6758 tree
6759 c_finish_goto_label (tree label)
6761 tree decl = lookup_label (label);
6762 if (!decl)
6763 return NULL_TREE;
6765 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6767 error ("jump into statement expression");
6768 return NULL_TREE;
6771 if (C_DECL_UNJUMPABLE_VM (decl))
6773 error ("jump into scope of identifier with variably modified type");
6774 return NULL_TREE;
6777 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6779 /* No jump from outside this statement expression context, so
6780 record that there is a jump from within this context. */
6781 struct c_label_list *nlist;
6782 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6783 nlist->next = label_context_stack_se->labels_used;
6784 nlist->label = decl;
6785 label_context_stack_se->labels_used = nlist;
6788 if (!C_DECL_UNDEFINABLE_VM (decl))
6790 /* No jump from outside this context context of identifiers with
6791 variably modified type, so record that there is a jump from
6792 within this context. */
6793 struct c_label_list *nlist;
6794 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6795 nlist->next = label_context_stack_vm->labels_used;
6796 nlist->label = decl;
6797 label_context_stack_vm->labels_used = nlist;
6800 TREE_USED (decl) = 1;
6801 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6804 /* Generate a computed goto statement to EXPR. */
6806 tree
6807 c_finish_goto_ptr (tree expr)
6809 if (pedantic)
6810 pedwarn ("ISO C forbids %<goto *expr;%>");
6811 expr = convert (ptr_type_node, expr);
6812 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6815 /* Generate a C `return' statement. RETVAL is the expression for what
6816 to return, or a null pointer for `return;' with no value. */
6818 tree
6819 c_finish_return (tree retval)
6821 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6822 bool no_warning = false;
6824 if (TREE_THIS_VOLATILE (current_function_decl))
6825 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6827 if (!retval)
6829 current_function_returns_null = 1;
6830 if ((warn_return_type || flag_isoc99)
6831 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6833 pedwarn_c99 ("%<return%> with no value, in "
6834 "function returning non-void");
6835 no_warning = true;
6838 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6840 current_function_returns_null = 1;
6841 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6842 pedwarn ("%<return%> with a value, in function returning void");
6844 else
6846 tree t = convert_for_assignment (valtype, retval, ic_return,
6847 NULL_TREE, NULL_TREE, 0);
6848 tree res = DECL_RESULT (current_function_decl);
6849 tree inner;
6851 current_function_returns_value = 1;
6852 if (t == error_mark_node)
6853 return NULL_TREE;
6855 inner = t = convert (TREE_TYPE (res), t);
6857 /* Strip any conversions, additions, and subtractions, and see if
6858 we are returning the address of a local variable. Warn if so. */
6859 while (1)
6861 switch (TREE_CODE (inner))
6863 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6864 case PLUS_EXPR:
6865 inner = TREE_OPERAND (inner, 0);
6866 continue;
6868 case MINUS_EXPR:
6869 /* If the second operand of the MINUS_EXPR has a pointer
6870 type (or is converted from it), this may be valid, so
6871 don't give a warning. */
6873 tree op1 = TREE_OPERAND (inner, 1);
6875 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6876 && (TREE_CODE (op1) == NOP_EXPR
6877 || TREE_CODE (op1) == NON_LVALUE_EXPR
6878 || TREE_CODE (op1) == CONVERT_EXPR))
6879 op1 = TREE_OPERAND (op1, 0);
6881 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6882 break;
6884 inner = TREE_OPERAND (inner, 0);
6885 continue;
6888 case ADDR_EXPR:
6889 inner = TREE_OPERAND (inner, 0);
6891 while (REFERENCE_CLASS_P (inner)
6892 && TREE_CODE (inner) != INDIRECT_REF)
6893 inner = TREE_OPERAND (inner, 0);
6895 if (DECL_P (inner)
6896 && !DECL_EXTERNAL (inner)
6897 && !TREE_STATIC (inner)
6898 && DECL_CONTEXT (inner) == current_function_decl)
6899 warning (0, "function returns address of local variable");
6900 break;
6902 default:
6903 break;
6906 break;
6909 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6912 ret_stmt = build_stmt (RETURN_EXPR, retval);
6913 TREE_NO_WARNING (ret_stmt) |= no_warning;
6914 return add_stmt (ret_stmt);
6917 struct c_switch {
6918 /* The SWITCH_EXPR being built. */
6919 tree switch_expr;
6921 /* The original type of the testing expression, i.e. before the
6922 default conversion is applied. */
6923 tree orig_type;
6925 /* A splay-tree mapping the low element of a case range to the high
6926 element, or NULL_TREE if there is no high element. Used to
6927 determine whether or not a new case label duplicates an old case
6928 label. We need a tree, rather than simply a hash table, because
6929 of the GNU case range extension. */
6930 splay_tree cases;
6932 /* Number of nested statement expressions within this switch
6933 statement; if nonzero, case and default labels may not
6934 appear. */
6935 unsigned int blocked_stmt_expr;
6937 /* Scope of outermost declarations of identifiers with variably
6938 modified type within this switch statement; if nonzero, case and
6939 default labels may not appear. */
6940 unsigned int blocked_vm;
6942 /* The next node on the stack. */
6943 struct c_switch *next;
6946 /* A stack of the currently active switch statements. The innermost
6947 switch statement is on the top of the stack. There is no need to
6948 mark the stack for garbage collection because it is only active
6949 during the processing of the body of a function, and we never
6950 collect at that point. */
6952 struct c_switch *c_switch_stack;
6954 /* Start a C switch statement, testing expression EXP. Return the new
6955 SWITCH_EXPR. */
6957 tree
6958 c_start_case (tree exp)
6960 enum tree_code code;
6961 tree type, orig_type = error_mark_node;
6962 struct c_switch *cs;
6964 if (exp != error_mark_node)
6966 code = TREE_CODE (TREE_TYPE (exp));
6967 orig_type = TREE_TYPE (exp);
6969 if (!INTEGRAL_TYPE_P (orig_type)
6970 && code != ERROR_MARK)
6972 error ("switch quantity not an integer");
6973 exp = integer_zero_node;
6974 orig_type = error_mark_node;
6976 else
6978 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6980 if (!in_system_header
6981 && (type == long_integer_type_node
6982 || type == long_unsigned_type_node))
6983 warning (OPT_Wtraditional, "%<long%> switch expression not "
6984 "converted to %<int%> in ISO C");
6986 exp = default_conversion (exp);
6987 type = TREE_TYPE (exp);
6991 /* Add this new SWITCH_EXPR to the stack. */
6992 cs = XNEW (struct c_switch);
6993 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
6994 cs->orig_type = orig_type;
6995 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6996 cs->blocked_stmt_expr = 0;
6997 cs->blocked_vm = 0;
6998 cs->next = c_switch_stack;
6999 c_switch_stack = cs;
7001 return add_stmt (cs->switch_expr);
7004 /* Process a case label. */
7006 tree
7007 do_case (tree low_value, tree high_value)
7009 tree label = NULL_TREE;
7011 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7012 && !c_switch_stack->blocked_vm)
7014 label = c_add_case_label (c_switch_stack->cases,
7015 SWITCH_COND (c_switch_stack->switch_expr),
7016 c_switch_stack->orig_type,
7017 low_value, high_value);
7018 if (label == error_mark_node)
7019 label = NULL_TREE;
7021 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7023 if (low_value)
7024 error ("case label in statement expression not containing "
7025 "enclosing switch statement");
7026 else
7027 error ("%<default%> label in statement expression not containing "
7028 "enclosing switch statement");
7030 else if (c_switch_stack && c_switch_stack->blocked_vm)
7032 if (low_value)
7033 error ("case label in scope of identifier with variably modified "
7034 "type not containing enclosing switch statement");
7035 else
7036 error ("%<default%> label in scope of identifier with variably "
7037 "modified type not containing enclosing switch statement");
7039 else if (low_value)
7040 error ("case label not within a switch statement");
7041 else
7042 error ("%<default%> label not within a switch statement");
7044 return label;
7047 /* Finish the switch statement. */
7049 void
7050 c_finish_case (tree body)
7052 struct c_switch *cs = c_switch_stack;
7053 location_t switch_location;
7055 SWITCH_BODY (cs->switch_expr) = body;
7057 /* We must not be within a statement expression nested in the switch
7058 at this point; we might, however, be within the scope of an
7059 identifier with variably modified type nested in the switch. */
7060 gcc_assert (!cs->blocked_stmt_expr);
7062 /* Emit warnings as needed. */
7063 if (EXPR_HAS_LOCATION (cs->switch_expr))
7064 switch_location = EXPR_LOCATION (cs->switch_expr);
7065 else
7066 switch_location = input_location;
7067 c_do_switch_warnings (cs->cases, switch_location,
7068 TREE_TYPE (cs->switch_expr),
7069 SWITCH_COND (cs->switch_expr));
7071 /* Pop the stack. */
7072 c_switch_stack = cs->next;
7073 splay_tree_delete (cs->cases);
7074 XDELETE (cs);
7077 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7078 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7079 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7080 statement, and was not surrounded with parenthesis. */
7082 void
7083 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7084 tree else_block, bool nested_if)
7086 tree stmt;
7088 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7089 if (warn_parentheses && nested_if && else_block == NULL)
7091 tree inner_if = then_block;
7093 /* We know from the grammar productions that there is an IF nested
7094 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7095 it might not be exactly THEN_BLOCK, but should be the last
7096 non-container statement within. */
7097 while (1)
7098 switch (TREE_CODE (inner_if))
7100 case COND_EXPR:
7101 goto found;
7102 case BIND_EXPR:
7103 inner_if = BIND_EXPR_BODY (inner_if);
7104 break;
7105 case STATEMENT_LIST:
7106 inner_if = expr_last (then_block);
7107 break;
7108 case TRY_FINALLY_EXPR:
7109 case TRY_CATCH_EXPR:
7110 inner_if = TREE_OPERAND (inner_if, 0);
7111 break;
7112 default:
7113 gcc_unreachable ();
7115 found:
7117 if (COND_EXPR_ELSE (inner_if))
7118 warning (OPT_Wparentheses,
7119 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7120 &if_locus);
7123 /* Diagnose ";" via the special empty statement node that we create. */
7124 if (extra_warnings)
7126 tree *inner_then = &then_block, *inner_else = &else_block;
7128 if (TREE_CODE (*inner_then) == STATEMENT_LIST
7129 && STATEMENT_LIST_TAIL (*inner_then))
7130 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
7131 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
7132 && STATEMENT_LIST_TAIL (*inner_else))
7133 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
7135 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
7137 if (!*inner_else)
7138 warning (0, "%Hempty body in an if-statement",
7139 EXPR_LOCUS (*inner_then));
7141 *inner_then = alloc_stmt_list ();
7143 if (*inner_else
7144 && TREE_CODE (*inner_else) == NOP_EXPR
7145 && !TREE_TYPE (*inner_else))
7147 warning (0, "%Hempty body in an else-statement",
7148 EXPR_LOCUS (*inner_else));
7150 *inner_else = alloc_stmt_list ();
7154 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7155 SET_EXPR_LOCATION (stmt, if_locus);
7156 add_stmt (stmt);
7159 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7160 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7161 is false for DO loops. INCR is the FOR increment expression. BODY is
7162 the statement controlled by the loop. BLAB is the break label. CLAB is
7163 the continue label. Everything is allowed to be NULL. */
7165 void
7166 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7167 tree blab, tree clab, bool cond_is_first)
7169 tree entry = NULL, exit = NULL, t;
7171 /* If the condition is zero don't generate a loop construct. */
7172 if (cond && integer_zerop (cond))
7174 if (cond_is_first)
7176 t = build_and_jump (&blab);
7177 SET_EXPR_LOCATION (t, start_locus);
7178 add_stmt (t);
7181 else
7183 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7185 /* If we have an exit condition, then we build an IF with gotos either
7186 out of the loop, or to the top of it. If there's no exit condition,
7187 then we just build a jump back to the top. */
7188 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7190 if (cond && !integer_nonzerop (cond))
7192 /* Canonicalize the loop condition to the end. This means
7193 generating a branch to the loop condition. Reuse the
7194 continue label, if possible. */
7195 if (cond_is_first)
7197 if (incr || !clab)
7199 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7200 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7202 else
7203 t = build1 (GOTO_EXPR, void_type_node, clab);
7204 SET_EXPR_LOCATION (t, start_locus);
7205 add_stmt (t);
7208 t = build_and_jump (&blab);
7209 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7210 if (cond_is_first)
7211 SET_EXPR_LOCATION (exit, start_locus);
7212 else
7213 SET_EXPR_LOCATION (exit, input_location);
7216 add_stmt (top);
7219 if (body)
7220 add_stmt (body);
7221 if (clab)
7222 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7223 if (incr)
7224 add_stmt (incr);
7225 if (entry)
7226 add_stmt (entry);
7227 if (exit)
7228 add_stmt (exit);
7229 if (blab)
7230 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7233 tree
7234 c_finish_bc_stmt (tree *label_p, bool is_break)
7236 bool skip;
7237 tree label = *label_p;
7239 /* In switch statements break is sometimes stylistically used after
7240 a return statement. This can lead to spurious warnings about
7241 control reaching the end of a non-void function when it is
7242 inlined. Note that we are calling block_may_fallthru with
7243 language specific tree nodes; this works because
7244 block_may_fallthru returns true when given something it does not
7245 understand. */
7246 skip = !block_may_fallthru (cur_stmt_list);
7248 if (!label)
7250 if (!skip)
7251 *label_p = label = create_artificial_label ();
7253 else if (TREE_CODE (label) != LABEL_DECL)
7255 if (is_break)
7256 error ("break statement not within loop or switch");
7257 else
7258 error ("continue statement not within a loop");
7259 return NULL_TREE;
7262 if (skip)
7263 return NULL_TREE;
7265 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7268 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7270 static void
7271 emit_side_effect_warnings (tree expr)
7273 if (expr == error_mark_node)
7275 else if (!TREE_SIDE_EFFECTS (expr))
7277 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7278 warning (0, "%Hstatement with no effect",
7279 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7281 else if (warn_unused_value)
7282 warn_if_unused_value (expr, input_location);
7285 /* Process an expression as if it were a complete statement. Emit
7286 diagnostics, but do not call ADD_STMT. */
7288 tree
7289 c_process_expr_stmt (tree expr)
7291 if (!expr)
7292 return NULL_TREE;
7294 if (warn_sequence_point)
7295 verify_sequence_points (expr);
7297 if (TREE_TYPE (expr) != error_mark_node
7298 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7299 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7300 error ("expression statement has incomplete type");
7302 /* If we're not processing a statement expression, warn about unused values.
7303 Warnings for statement expressions will be emitted later, once we figure
7304 out which is the result. */
7305 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7306 && (extra_warnings || warn_unused_value))
7307 emit_side_effect_warnings (expr);
7309 /* If the expression is not of a type to which we cannot assign a line
7310 number, wrap the thing in a no-op NOP_EXPR. */
7311 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7312 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7314 if (EXPR_P (expr))
7315 SET_EXPR_LOCATION (expr, input_location);
7317 return expr;
7320 /* Emit an expression as a statement. */
7322 tree
7323 c_finish_expr_stmt (tree expr)
7325 if (expr)
7326 return add_stmt (c_process_expr_stmt (expr));
7327 else
7328 return NULL;
7331 /* Do the opposite and emit a statement as an expression. To begin,
7332 create a new binding level and return it. */
7334 tree
7335 c_begin_stmt_expr (void)
7337 tree ret;
7338 struct c_label_context_se *nstack;
7339 struct c_label_list *glist;
7341 /* We must force a BLOCK for this level so that, if it is not expanded
7342 later, there is a way to turn off the entire subtree of blocks that
7343 are contained in it. */
7344 keep_next_level ();
7345 ret = c_begin_compound_stmt (true);
7346 if (c_switch_stack)
7348 c_switch_stack->blocked_stmt_expr++;
7349 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7351 for (glist = label_context_stack_se->labels_used;
7352 glist != NULL;
7353 glist = glist->next)
7355 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7357 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7358 nstack->labels_def = NULL;
7359 nstack->labels_used = NULL;
7360 nstack->next = label_context_stack_se;
7361 label_context_stack_se = nstack;
7363 /* Mark the current statement list as belonging to a statement list. */
7364 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7366 return ret;
7369 tree
7370 c_finish_stmt_expr (tree body)
7372 tree last, type, tmp, val;
7373 tree *last_p;
7374 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7376 body = c_end_compound_stmt (body, true);
7377 if (c_switch_stack)
7379 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7380 c_switch_stack->blocked_stmt_expr--;
7382 /* It is no longer possible to jump to labels defined within this
7383 statement expression. */
7384 for (dlist = label_context_stack_se->labels_def;
7385 dlist != NULL;
7386 dlist = dlist->next)
7388 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7390 /* It is again possible to define labels with a goto just outside
7391 this statement expression. */
7392 for (glist = label_context_stack_se->next->labels_used;
7393 glist != NULL;
7394 glist = glist->next)
7396 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7397 glist_prev = glist;
7399 if (glist_prev != NULL)
7400 glist_prev->next = label_context_stack_se->labels_used;
7401 else
7402 label_context_stack_se->next->labels_used
7403 = label_context_stack_se->labels_used;
7404 label_context_stack_se = label_context_stack_se->next;
7406 /* Locate the last statement in BODY. See c_end_compound_stmt
7407 about always returning a BIND_EXPR. */
7408 last_p = &BIND_EXPR_BODY (body);
7409 last = BIND_EXPR_BODY (body);
7411 continue_searching:
7412 if (TREE_CODE (last) == STATEMENT_LIST)
7414 tree_stmt_iterator i;
7416 /* This can happen with degenerate cases like ({ }). No value. */
7417 if (!TREE_SIDE_EFFECTS (last))
7418 return body;
7420 /* If we're supposed to generate side effects warnings, process
7421 all of the statements except the last. */
7422 if (extra_warnings || warn_unused_value)
7424 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7425 emit_side_effect_warnings (tsi_stmt (i));
7427 else
7428 i = tsi_last (last);
7429 last_p = tsi_stmt_ptr (i);
7430 last = *last_p;
7433 /* If the end of the list is exception related, then the list was split
7434 by a call to push_cleanup. Continue searching. */
7435 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7436 || TREE_CODE (last) == TRY_CATCH_EXPR)
7438 last_p = &TREE_OPERAND (last, 0);
7439 last = *last_p;
7440 goto continue_searching;
7443 /* In the case that the BIND_EXPR is not necessary, return the
7444 expression out from inside it. */
7445 if (last == error_mark_node
7446 || (last == BIND_EXPR_BODY (body)
7447 && BIND_EXPR_VARS (body) == NULL))
7449 /* Do not warn if the return value of a statement expression is
7450 unused. */
7451 if (EXPR_P (last))
7452 TREE_NO_WARNING (last) = 1;
7453 return last;
7456 /* Extract the type of said expression. */
7457 type = TREE_TYPE (last);
7459 /* If we're not returning a value at all, then the BIND_EXPR that
7460 we already have is a fine expression to return. */
7461 if (!type || VOID_TYPE_P (type))
7462 return body;
7464 /* Now that we've located the expression containing the value, it seems
7465 silly to make voidify_wrapper_expr repeat the process. Create a
7466 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7467 tmp = create_tmp_var_raw (type, NULL);
7469 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7470 tree_expr_nonnegative_p giving up immediately. */
7471 val = last;
7472 if (TREE_CODE (val) == NOP_EXPR
7473 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7474 val = TREE_OPERAND (val, 0);
7476 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7477 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7479 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7482 /* Begin the scope of an identifier of variably modified type, scope
7483 number SCOPE. Jumping from outside this scope to inside it is not
7484 permitted. */
7486 void
7487 c_begin_vm_scope (unsigned int scope)
7489 struct c_label_context_vm *nstack;
7490 struct c_label_list *glist;
7492 gcc_assert (scope > 0);
7493 if (c_switch_stack && !c_switch_stack->blocked_vm)
7494 c_switch_stack->blocked_vm = scope;
7495 for (glist = label_context_stack_vm->labels_used;
7496 glist != NULL;
7497 glist = glist->next)
7499 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7501 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7502 nstack->labels_def = NULL;
7503 nstack->labels_used = NULL;
7504 nstack->scope = scope;
7505 nstack->next = label_context_stack_vm;
7506 label_context_stack_vm = nstack;
7509 /* End a scope which may contain identifiers of variably modified
7510 type, scope number SCOPE. */
7512 void
7513 c_end_vm_scope (unsigned int scope)
7515 if (label_context_stack_vm == NULL)
7516 return;
7517 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7518 c_switch_stack->blocked_vm = 0;
7519 /* We may have a number of nested scopes of identifiers with
7520 variably modified type, all at this depth. Pop each in turn. */
7521 while (label_context_stack_vm->scope == scope)
7523 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7525 /* It is no longer possible to jump to labels defined within this
7526 scope. */
7527 for (dlist = label_context_stack_vm->labels_def;
7528 dlist != NULL;
7529 dlist = dlist->next)
7531 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7533 /* It is again possible to define labels with a goto just outside
7534 this scope. */
7535 for (glist = label_context_stack_vm->next->labels_used;
7536 glist != NULL;
7537 glist = glist->next)
7539 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7540 glist_prev = glist;
7542 if (glist_prev != NULL)
7543 glist_prev->next = label_context_stack_vm->labels_used;
7544 else
7545 label_context_stack_vm->next->labels_used
7546 = label_context_stack_vm->labels_used;
7547 label_context_stack_vm = label_context_stack_vm->next;
7551 /* Begin and end compound statements. This is as simple as pushing
7552 and popping new statement lists from the tree. */
7554 tree
7555 c_begin_compound_stmt (bool do_scope)
7557 tree stmt = push_stmt_list ();
7558 if (do_scope)
7559 push_scope ();
7560 return stmt;
7563 tree
7564 c_end_compound_stmt (tree stmt, bool do_scope)
7566 tree block = NULL;
7568 if (do_scope)
7570 if (c_dialect_objc ())
7571 objc_clear_super_receiver ();
7572 block = pop_scope ();
7575 stmt = pop_stmt_list (stmt);
7576 stmt = c_build_bind_expr (block, stmt);
7578 /* If this compound statement is nested immediately inside a statement
7579 expression, then force a BIND_EXPR to be created. Otherwise we'll
7580 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7581 STATEMENT_LISTs merge, and thus we can lose track of what statement
7582 was really last. */
7583 if (cur_stmt_list
7584 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7585 && TREE_CODE (stmt) != BIND_EXPR)
7587 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7588 TREE_SIDE_EFFECTS (stmt) = 1;
7591 return stmt;
7594 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7595 when the current scope is exited. EH_ONLY is true when this is not
7596 meant to apply to normal control flow transfer. */
7598 void
7599 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7601 enum tree_code code;
7602 tree stmt, list;
7603 bool stmt_expr;
7605 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7606 stmt = build_stmt (code, NULL, cleanup);
7607 add_stmt (stmt);
7608 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7609 list = push_stmt_list ();
7610 TREE_OPERAND (stmt, 0) = list;
7611 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7614 /* Build a binary-operation expression without default conversions.
7615 CODE is the kind of expression to build.
7616 This function differs from `build' in several ways:
7617 the data type of the result is computed and recorded in it,
7618 warnings are generated if arg data types are invalid,
7619 special handling for addition and subtraction of pointers is known,
7620 and some optimization is done (operations on narrow ints
7621 are done in the narrower type when that gives the same result).
7622 Constant folding is also done before the result is returned.
7624 Note that the operands will never have enumeral types, or function
7625 or array types, because either they will have the default conversions
7626 performed or they have both just been converted to some other type in which
7627 the arithmetic is to be done. */
7629 tree
7630 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7631 int convert_p)
7633 tree type0, type1;
7634 enum tree_code code0, code1;
7635 tree op0, op1;
7636 const char *invalid_op_diag;
7638 /* Expression code to give to the expression when it is built.
7639 Normally this is CODE, which is what the caller asked for,
7640 but in some special cases we change it. */
7641 enum tree_code resultcode = code;
7643 /* Data type in which the computation is to be performed.
7644 In the simplest cases this is the common type of the arguments. */
7645 tree result_type = NULL;
7647 /* Nonzero means operands have already been type-converted
7648 in whatever way is necessary.
7649 Zero means they need to be converted to RESULT_TYPE. */
7650 int converted = 0;
7652 /* Nonzero means create the expression with this type, rather than
7653 RESULT_TYPE. */
7654 tree build_type = 0;
7656 /* Nonzero means after finally constructing the expression
7657 convert it to this type. */
7658 tree final_type = 0;
7660 /* Nonzero if this is an operation like MIN or MAX which can
7661 safely be computed in short if both args are promoted shorts.
7662 Also implies COMMON.
7663 -1 indicates a bitwise operation; this makes a difference
7664 in the exact conditions for when it is safe to do the operation
7665 in a narrower mode. */
7666 int shorten = 0;
7668 /* Nonzero if this is a comparison operation;
7669 if both args are promoted shorts, compare the original shorts.
7670 Also implies COMMON. */
7671 int short_compare = 0;
7673 /* Nonzero if this is a right-shift operation, which can be computed on the
7674 original short and then promoted if the operand is a promoted short. */
7675 int short_shift = 0;
7677 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7678 int common = 0;
7680 /* True means types are compatible as far as ObjC is concerned. */
7681 bool objc_ok;
7683 if (convert_p)
7685 op0 = default_conversion (orig_op0);
7686 op1 = default_conversion (orig_op1);
7688 else
7690 op0 = orig_op0;
7691 op1 = orig_op1;
7694 type0 = TREE_TYPE (op0);
7695 type1 = TREE_TYPE (op1);
7697 /* The expression codes of the data types of the arguments tell us
7698 whether the arguments are integers, floating, pointers, etc. */
7699 code0 = TREE_CODE (type0);
7700 code1 = TREE_CODE (type1);
7702 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7703 STRIP_TYPE_NOPS (op0);
7704 STRIP_TYPE_NOPS (op1);
7706 /* If an error was already reported for one of the arguments,
7707 avoid reporting another error. */
7709 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7710 return error_mark_node;
7712 if ((invalid_op_diag
7713 = targetm.invalid_binary_op (code, type0, type1)))
7715 error (invalid_op_diag);
7716 return error_mark_node;
7719 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7721 switch (code)
7723 case PLUS_EXPR:
7724 /* Handle the pointer + int case. */
7725 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7726 return pointer_int_sum (PLUS_EXPR, op0, op1);
7727 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7728 return pointer_int_sum (PLUS_EXPR, op1, op0);
7729 else
7730 common = 1;
7731 break;
7733 case MINUS_EXPR:
7734 /* Subtraction of two similar pointers.
7735 We must subtract them as integers, then divide by object size. */
7736 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7737 && comp_target_types (type0, type1))
7738 return pointer_diff (op0, op1);
7739 /* Handle pointer minus int. Just like pointer plus int. */
7740 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7741 return pointer_int_sum (MINUS_EXPR, op0, op1);
7742 else
7743 common = 1;
7744 break;
7746 case MULT_EXPR:
7747 common = 1;
7748 break;
7750 case TRUNC_DIV_EXPR:
7751 case CEIL_DIV_EXPR:
7752 case FLOOR_DIV_EXPR:
7753 case ROUND_DIV_EXPR:
7754 case EXACT_DIV_EXPR:
7755 /* Floating point division by zero is a legitimate way to obtain
7756 infinities and NaNs. */
7757 if (skip_evaluation == 0 && integer_zerop (op1))
7758 warning (OPT_Wdiv_by_zero, "division by zero");
7760 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7761 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7762 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7763 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7765 enum tree_code tcode0 = code0, tcode1 = code1;
7767 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7768 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7769 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7770 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7772 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7773 resultcode = RDIV_EXPR;
7774 else
7775 /* Although it would be tempting to shorten always here, that
7776 loses on some targets, since the modulo instruction is
7777 undefined if the quotient can't be represented in the
7778 computation mode. We shorten only if unsigned or if
7779 dividing by something we know != -1. */
7780 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7781 || (TREE_CODE (op1) == INTEGER_CST
7782 && !integer_all_onesp (op1)));
7783 common = 1;
7785 break;
7787 case BIT_AND_EXPR:
7788 case BIT_IOR_EXPR:
7789 case BIT_XOR_EXPR:
7790 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7791 shorten = -1;
7792 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7793 common = 1;
7794 break;
7796 case TRUNC_MOD_EXPR:
7797 case FLOOR_MOD_EXPR:
7798 if (skip_evaluation == 0 && integer_zerop (op1))
7799 warning (OPT_Wdiv_by_zero, "division by zero");
7801 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7803 /* Although it would be tempting to shorten always here, that loses
7804 on some targets, since the modulo instruction is undefined if the
7805 quotient can't be represented in the computation mode. We shorten
7806 only if unsigned or if dividing by something we know != -1. */
7807 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7808 || (TREE_CODE (op1) == INTEGER_CST
7809 && !integer_all_onesp (op1)));
7810 common = 1;
7812 break;
7814 case TRUTH_ANDIF_EXPR:
7815 case TRUTH_ORIF_EXPR:
7816 case TRUTH_AND_EXPR:
7817 case TRUTH_OR_EXPR:
7818 case TRUTH_XOR_EXPR:
7819 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7820 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7821 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7822 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7824 /* Result of these operations is always an int,
7825 but that does not mean the operands should be
7826 converted to ints! */
7827 result_type = integer_type_node;
7828 op0 = c_common_truthvalue_conversion (op0);
7829 op1 = c_common_truthvalue_conversion (op1);
7830 converted = 1;
7832 break;
7834 /* Shift operations: result has same type as first operand;
7835 always convert second operand to int.
7836 Also set SHORT_SHIFT if shifting rightward. */
7838 case RSHIFT_EXPR:
7839 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7841 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7843 if (tree_int_cst_sgn (op1) < 0)
7844 warning (0, "right shift count is negative");
7845 else
7847 if (!integer_zerop (op1))
7848 short_shift = 1;
7850 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7851 warning (0, "right shift count >= width of type");
7855 /* Use the type of the value to be shifted. */
7856 result_type = type0;
7857 /* Convert the shift-count to an integer, regardless of size
7858 of value being shifted. */
7859 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7860 op1 = convert (integer_type_node, op1);
7861 /* Avoid converting op1 to result_type later. */
7862 converted = 1;
7864 break;
7866 case LSHIFT_EXPR:
7867 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7869 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7871 if (tree_int_cst_sgn (op1) < 0)
7872 warning (0, "left shift count is negative");
7874 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7875 warning (0, "left shift count >= width of type");
7878 /* Use the type of the value to be shifted. */
7879 result_type = type0;
7880 /* Convert the shift-count to an integer, regardless of size
7881 of value being shifted. */
7882 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7883 op1 = convert (integer_type_node, op1);
7884 /* Avoid converting op1 to result_type later. */
7885 converted = 1;
7887 break;
7889 case EQ_EXPR:
7890 case NE_EXPR:
7891 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
7892 warning (OPT_Wfloat_equal,
7893 "comparing floating point with == or != is unsafe");
7894 /* Result of comparison is always int,
7895 but don't convert the args to int! */
7896 build_type = integer_type_node;
7897 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7898 || code0 == COMPLEX_TYPE)
7899 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7900 || code1 == COMPLEX_TYPE))
7901 short_compare = 1;
7902 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7904 tree tt0 = TREE_TYPE (type0);
7905 tree tt1 = TREE_TYPE (type1);
7906 /* Anything compares with void *. void * compares with anything.
7907 Otherwise, the targets must be compatible
7908 and both must be object or both incomplete. */
7909 if (comp_target_types (type0, type1))
7910 result_type = common_pointer_type (type0, type1);
7911 else if (VOID_TYPE_P (tt0))
7913 /* op0 != orig_op0 detects the case of something
7914 whose value is 0 but which isn't a valid null ptr const. */
7915 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7916 && TREE_CODE (tt1) == FUNCTION_TYPE)
7917 pedwarn ("ISO C forbids comparison of %<void *%>"
7918 " with function pointer");
7920 else if (VOID_TYPE_P (tt1))
7922 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7923 && TREE_CODE (tt0) == FUNCTION_TYPE)
7924 pedwarn ("ISO C forbids comparison of %<void *%>"
7925 " with function pointer");
7927 else
7928 /* Avoid warning about the volatile ObjC EH puts on decls. */
7929 if (!objc_ok)
7930 pedwarn ("comparison of distinct pointer types lacks a cast");
7932 if (result_type == NULL_TREE)
7933 result_type = ptr_type_node;
7935 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7936 && integer_zerop (op1))
7937 result_type = type0;
7938 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7939 && integer_zerop (op0))
7940 result_type = type1;
7941 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7943 result_type = type0;
7944 pedwarn ("comparison between pointer and integer");
7946 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7948 result_type = type1;
7949 pedwarn ("comparison between pointer and integer");
7951 break;
7953 case LE_EXPR:
7954 case GE_EXPR:
7955 case LT_EXPR:
7956 case GT_EXPR:
7957 build_type = integer_type_node;
7958 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7959 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7960 short_compare = 1;
7961 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7963 if (comp_target_types (type0, type1))
7965 result_type = common_pointer_type (type0, type1);
7966 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7967 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7968 pedwarn ("comparison of complete and incomplete pointers");
7969 else if (pedantic
7970 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7971 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7973 else
7975 result_type = ptr_type_node;
7976 pedwarn ("comparison of distinct pointer types lacks a cast");
7979 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7980 && integer_zerop (op1))
7982 result_type = type0;
7983 if (pedantic || extra_warnings)
7984 pedwarn ("ordered comparison of pointer with integer zero");
7986 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7987 && integer_zerop (op0))
7989 result_type = type1;
7990 if (pedantic)
7991 pedwarn ("ordered comparison of pointer with integer zero");
7993 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7995 result_type = type0;
7996 pedwarn ("comparison between pointer and integer");
7998 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8000 result_type = type1;
8001 pedwarn ("comparison between pointer and integer");
8003 break;
8005 default:
8006 gcc_unreachable ();
8009 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8010 return error_mark_node;
8012 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8013 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8014 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8015 TREE_TYPE (type1))))
8017 binary_op_error (code);
8018 return error_mark_node;
8021 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8022 || code0 == VECTOR_TYPE)
8024 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8025 || code1 == VECTOR_TYPE))
8027 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8029 if (shorten || common || short_compare)
8030 result_type = c_common_type (type0, type1);
8032 /* For certain operations (which identify themselves by shorten != 0)
8033 if both args were extended from the same smaller type,
8034 do the arithmetic in that type and then extend.
8036 shorten !=0 and !=1 indicates a bitwise operation.
8037 For them, this optimization is safe only if
8038 both args are zero-extended or both are sign-extended.
8039 Otherwise, we might change the result.
8040 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8041 but calculated in (unsigned short) it would be (unsigned short)-1. */
8043 if (shorten && none_complex)
8045 int unsigned0, unsigned1;
8046 tree arg0 = get_narrower (op0, &unsigned0);
8047 tree arg1 = get_narrower (op1, &unsigned1);
8048 /* UNS is 1 if the operation to be done is an unsigned one. */
8049 int uns = TYPE_UNSIGNED (result_type);
8050 tree type;
8052 final_type = result_type;
8054 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8055 but it *requires* conversion to FINAL_TYPE. */
8057 if ((TYPE_PRECISION (TREE_TYPE (op0))
8058 == TYPE_PRECISION (TREE_TYPE (arg0)))
8059 && TREE_TYPE (op0) != final_type)
8060 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8061 if ((TYPE_PRECISION (TREE_TYPE (op1))
8062 == TYPE_PRECISION (TREE_TYPE (arg1)))
8063 && TREE_TYPE (op1) != final_type)
8064 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8066 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8068 /* For bitwise operations, signedness of nominal type
8069 does not matter. Consider only how operands were extended. */
8070 if (shorten == -1)
8071 uns = unsigned0;
8073 /* Note that in all three cases below we refrain from optimizing
8074 an unsigned operation on sign-extended args.
8075 That would not be valid. */
8077 /* Both args variable: if both extended in same way
8078 from same width, do it in that width.
8079 Do it unsigned if args were zero-extended. */
8080 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8081 < TYPE_PRECISION (result_type))
8082 && (TYPE_PRECISION (TREE_TYPE (arg1))
8083 == TYPE_PRECISION (TREE_TYPE (arg0)))
8084 && unsigned0 == unsigned1
8085 && (unsigned0 || !uns))
8086 result_type
8087 = c_common_signed_or_unsigned_type
8088 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8089 else if (TREE_CODE (arg0) == INTEGER_CST
8090 && (unsigned1 || !uns)
8091 && (TYPE_PRECISION (TREE_TYPE (arg1))
8092 < TYPE_PRECISION (result_type))
8093 && (type
8094 = c_common_signed_or_unsigned_type (unsigned1,
8095 TREE_TYPE (arg1)),
8096 int_fits_type_p (arg0, type)))
8097 result_type = type;
8098 else if (TREE_CODE (arg1) == INTEGER_CST
8099 && (unsigned0 || !uns)
8100 && (TYPE_PRECISION (TREE_TYPE (arg0))
8101 < TYPE_PRECISION (result_type))
8102 && (type
8103 = c_common_signed_or_unsigned_type (unsigned0,
8104 TREE_TYPE (arg0)),
8105 int_fits_type_p (arg1, type)))
8106 result_type = type;
8109 /* Shifts can be shortened if shifting right. */
8111 if (short_shift)
8113 int unsigned_arg;
8114 tree arg0 = get_narrower (op0, &unsigned_arg);
8116 final_type = result_type;
8118 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8119 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8121 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8122 /* We can shorten only if the shift count is less than the
8123 number of bits in the smaller type size. */
8124 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8125 /* We cannot drop an unsigned shift after sign-extension. */
8126 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8128 /* Do an unsigned shift if the operand was zero-extended. */
8129 result_type
8130 = c_common_signed_or_unsigned_type (unsigned_arg,
8131 TREE_TYPE (arg0));
8132 /* Convert value-to-be-shifted to that type. */
8133 if (TREE_TYPE (op0) != result_type)
8134 op0 = convert (result_type, op0);
8135 converted = 1;
8139 /* Comparison operations are shortened too but differently.
8140 They identify themselves by setting short_compare = 1. */
8142 if (short_compare)
8144 /* Don't write &op0, etc., because that would prevent op0
8145 from being kept in a register.
8146 Instead, make copies of the our local variables and
8147 pass the copies by reference, then copy them back afterward. */
8148 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8149 enum tree_code xresultcode = resultcode;
8150 tree val
8151 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8153 if (val != 0)
8154 return val;
8156 op0 = xop0, op1 = xop1;
8157 converted = 1;
8158 resultcode = xresultcode;
8160 if (warn_sign_compare && skip_evaluation == 0)
8162 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8163 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8164 int unsignedp0, unsignedp1;
8165 tree primop0 = get_narrower (op0, &unsignedp0);
8166 tree primop1 = get_narrower (op1, &unsignedp1);
8168 xop0 = orig_op0;
8169 xop1 = orig_op1;
8170 STRIP_TYPE_NOPS (xop0);
8171 STRIP_TYPE_NOPS (xop1);
8173 /* Give warnings for comparisons between signed and unsigned
8174 quantities that may fail.
8176 Do the checking based on the original operand trees, so that
8177 casts will be considered, but default promotions won't be.
8179 Do not warn if the comparison is being done in a signed type,
8180 since the signed type will only be chosen if it can represent
8181 all the values of the unsigned type. */
8182 if (!TYPE_UNSIGNED (result_type))
8183 /* OK */;
8184 /* Do not warn if both operands are the same signedness. */
8185 else if (op0_signed == op1_signed)
8186 /* OK */;
8187 else
8189 tree sop, uop;
8191 if (op0_signed)
8192 sop = xop0, uop = xop1;
8193 else
8194 sop = xop1, uop = xop0;
8196 /* Do not warn if the signed quantity is an
8197 unsuffixed integer literal (or some static
8198 constant expression involving such literals or a
8199 conditional expression involving such literals)
8200 and it is non-negative. */
8201 if (tree_expr_nonnegative_p (sop))
8202 /* OK */;
8203 /* Do not warn if the comparison is an equality operation,
8204 the unsigned quantity is an integral constant, and it
8205 would fit in the result if the result were signed. */
8206 else if (TREE_CODE (uop) == INTEGER_CST
8207 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8208 && int_fits_type_p
8209 (uop, c_common_signed_type (result_type)))
8210 /* OK */;
8211 /* Do not warn if the unsigned quantity is an enumeration
8212 constant and its maximum value would fit in the result
8213 if the result were signed. */
8214 else if (TREE_CODE (uop) == INTEGER_CST
8215 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8216 && int_fits_type_p
8217 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8218 c_common_signed_type (result_type)))
8219 /* OK */;
8220 else
8221 warning (0, "comparison between signed and unsigned");
8224 /* Warn if two unsigned values are being compared in a size
8225 larger than their original size, and one (and only one) is the
8226 result of a `~' operator. This comparison will always fail.
8228 Also warn if one operand is a constant, and the constant
8229 does not have all bits set that are set in the ~ operand
8230 when it is extended. */
8232 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8233 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8235 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8236 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8237 &unsignedp0);
8238 else
8239 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8240 &unsignedp1);
8242 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8244 tree primop;
8245 HOST_WIDE_INT constant, mask;
8246 int unsignedp, bits;
8248 if (host_integerp (primop0, 0))
8250 primop = primop1;
8251 unsignedp = unsignedp1;
8252 constant = tree_low_cst (primop0, 0);
8254 else
8256 primop = primop0;
8257 unsignedp = unsignedp0;
8258 constant = tree_low_cst (primop1, 0);
8261 bits = TYPE_PRECISION (TREE_TYPE (primop));
8262 if (bits < TYPE_PRECISION (result_type)
8263 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8265 mask = (~(HOST_WIDE_INT) 0) << bits;
8266 if ((mask & constant) != mask)
8267 warning (0, "comparison of promoted ~unsigned with constant");
8270 else if (unsignedp0 && unsignedp1
8271 && (TYPE_PRECISION (TREE_TYPE (primop0))
8272 < TYPE_PRECISION (result_type))
8273 && (TYPE_PRECISION (TREE_TYPE (primop1))
8274 < TYPE_PRECISION (result_type)))
8275 warning (0, "comparison of promoted ~unsigned with unsigned");
8281 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8282 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8283 Then the expression will be built.
8284 It will be given type FINAL_TYPE if that is nonzero;
8285 otherwise, it will be given type RESULT_TYPE. */
8287 if (!result_type)
8289 binary_op_error (code);
8290 return error_mark_node;
8293 if (!converted)
8295 if (TREE_TYPE (op0) != result_type)
8296 op0 = convert (result_type, op0);
8297 if (TREE_TYPE (op1) != result_type)
8298 op1 = convert (result_type, op1);
8300 /* This can happen if one operand has a vector type, and the other
8301 has a different type. */
8302 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8303 return error_mark_node;
8306 if (build_type == NULL_TREE)
8307 build_type = result_type;
8310 /* Treat expressions in initializers specially as they can't trap. */
8311 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8312 build_type,
8313 op0, op1)
8314 : fold_build2 (resultcode, build_type,
8315 op0, op1);
8317 if (final_type != 0)
8318 result = convert (final_type, result);
8319 return result;
8324 /* Convert EXPR to be a truth-value, validating its type for this
8325 purpose. */
8327 tree
8328 c_objc_common_truthvalue_conversion (tree expr)
8330 switch (TREE_CODE (TREE_TYPE (expr)))
8332 case ARRAY_TYPE:
8333 error ("used array that cannot be converted to pointer where scalar is required");
8334 return error_mark_node;
8336 case RECORD_TYPE:
8337 error ("used struct type value where scalar is required");
8338 return error_mark_node;
8340 case UNION_TYPE:
8341 error ("used union type value where scalar is required");
8342 return error_mark_node;
8344 case FUNCTION_TYPE:
8345 gcc_unreachable ();
8347 default:
8348 break;
8351 /* ??? Should we also give an error for void and vectors rather than
8352 leaving those to give errors later? */
8353 return c_common_truthvalue_conversion (expr);
8357 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8358 required. */
8360 tree
8361 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8362 bool *ti ATTRIBUTE_UNUSED, bool *se)
8364 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8366 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8367 /* Executing a compound literal inside a function reinitializes
8368 it. */
8369 if (!TREE_STATIC (decl))
8370 *se = true;
8371 return decl;
8373 else
8374 return expr;