Add files that I missed when importing NaCl changes earlier
[gcc/nacl-gcc.git] / gcc / c-typeck.c
blobe1673336e4dc7bb22049c2de266ba8784055f795
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, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (tree, tree);
80 static int comp_target_types (tree, tree);
81 static int function_types_compatible_p (tree, tree);
82 static int type_lists_compatible_p (tree, tree);
83 static tree decl_constant_value_for_broken_optimization (tree);
84 static tree lookup_field (tree, tree);
85 static tree convert_arguments (tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (const char *);
95 static tree digest_init (tree, tree, bool, int);
96 static void output_init_element (tree, bool, tree, tree, int);
97 static void output_pending_init_elements (int);
98 static int set_designator (int);
99 static void push_range_stack (tree);
100 static void add_pending_init (tree, tree);
101 static void set_nonincremental_init (void);
102 static void set_nonincremental_init_from_string (tree);
103 static tree find_init_member (tree);
104 static void readonly_error (tree, enum lvalue_use);
105 static int lvalue_or_else (tree, enum lvalue_use);
106 static int lvalue_p (tree);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (tree, tree);
110 /* Return true if EXP is a null pointer constant, false otherwise. */
112 static bool
113 null_pointer_constant_p (tree expr)
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
119 && !TREE_CONSTANT_OVERFLOW (expr)
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
126 \f/* This is a cache to hold if two types are compatible or not. */
128 struct tagged_tu_seen_cache {
129 const struct tagged_tu_seen_cache * next;
130 tree t1;
131 tree t2;
132 /* The return value of tagged_types_tu_compatible_p if we had seen
133 these two types already. */
134 int val;
137 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
138 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
140 /* Do `exp = require_complete_type (exp);' to make sure exp
141 does not have an incomplete type. (That includes void types.) */
143 tree
144 require_complete_type (tree value)
146 tree type = TREE_TYPE (value);
148 if (value == error_mark_node || type == error_mark_node)
149 return error_mark_node;
151 /* First, detect a valid value with a complete type. */
152 if (COMPLETE_TYPE_P (type))
153 return value;
155 c_incomplete_type_error (value, type);
156 return error_mark_node;
159 /* Print an error message for invalid use of an incomplete type.
160 VALUE is the expression that was used (or 0 if that isn't known)
161 and TYPE is the type that was invalid. */
163 void
164 c_incomplete_type_error (tree value, tree type)
166 const char *type_code_string;
168 /* Avoid duplicate error message. */
169 if (TREE_CODE (type) == ERROR_MARK)
170 return;
172 if (value != 0 && (TREE_CODE (value) == VAR_DECL
173 || TREE_CODE (value) == PARM_DECL))
174 error ("%qD has an incomplete type", value);
175 else
177 retry:
178 /* We must print an error message. Be clever about what it says. */
180 switch (TREE_CODE (type))
182 case RECORD_TYPE:
183 type_code_string = "struct";
184 break;
186 case UNION_TYPE:
187 type_code_string = "union";
188 break;
190 case ENUMERAL_TYPE:
191 type_code_string = "enum";
192 break;
194 case VOID_TYPE:
195 error ("invalid use of void expression");
196 return;
198 case ARRAY_TYPE:
199 if (TYPE_DOMAIN (type))
201 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
203 error ("invalid use of flexible array member");
204 return;
206 type = TREE_TYPE (type);
207 goto retry;
209 error ("invalid use of array with unspecified bounds");
210 return;
212 default:
213 gcc_unreachable ();
216 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
217 error ("invalid use of undefined type %<%s %E%>",
218 type_code_string, TYPE_NAME (type));
219 else
220 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
221 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
225 /* Given a type, apply default promotions wrt unnamed function
226 arguments and return the new type. */
228 tree
229 c_type_promotes_to (tree type)
231 if (TYPE_MAIN_VARIANT (type) == float_type_node)
232 return double_type_node;
234 if (c_promoting_integer_type_p (type))
236 /* Preserve unsignedness if not really getting any wider. */
237 if (TYPE_UNSIGNED (type)
238 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
239 return unsigned_type_node;
240 return integer_type_node;
243 return type;
246 /* Return a variant of TYPE which has all the type qualifiers of LIKE
247 as well as those of TYPE. */
249 static tree
250 qualify_type (tree type, tree like)
252 return c_build_qualified_type (type,
253 TYPE_QUALS (type) | TYPE_QUALS (like));
256 /* Return true iff the given tree T is a variable length array. */
258 bool
259 c_vla_type_p (tree t)
261 if (TREE_CODE (t) == ARRAY_TYPE
262 && C_TYPE_VARIABLE_SIZE (t))
263 return true;
264 return false;
267 /* Return the composite type of two compatible types.
269 We assume that comptypes has already been done and returned
270 nonzero; if that isn't so, this may crash. In particular, we
271 assume that qualifiers match. */
273 tree
274 composite_type (tree t1, tree t2)
276 enum tree_code code1;
277 enum tree_code code2;
278 tree attributes;
280 /* Save time if the two types are the same. */
282 if (t1 == t2) return t1;
284 /* If one type is nonsense, use the other. */
285 if (t1 == error_mark_node)
286 return t2;
287 if (t2 == error_mark_node)
288 return t1;
290 code1 = TREE_CODE (t1);
291 code2 = TREE_CODE (t2);
293 /* Merge the attributes. */
294 attributes = targetm.merge_type_attributes (t1, t2);
296 /* If one is an enumerated type and the other is the compatible
297 integer type, the composite type might be either of the two
298 (DR#013 question 3). For consistency, use the enumerated type as
299 the composite type. */
301 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
302 return t1;
303 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
304 return t2;
306 gcc_assert (code1 == code2);
308 switch (code1)
310 case POINTER_TYPE:
311 /* For two pointers, do this recursively on the target type. */
313 tree pointed_to_1 = TREE_TYPE (t1);
314 tree pointed_to_2 = TREE_TYPE (t2);
315 tree target = composite_type (pointed_to_1, pointed_to_2);
316 t1 = build_pointer_type (target);
317 t1 = build_type_attribute_variant (t1, attributes);
318 return qualify_type (t1, t2);
321 case ARRAY_TYPE:
323 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
324 int quals;
325 tree unqual_elt;
326 tree d1 = TYPE_DOMAIN (t1);
327 tree d2 = TYPE_DOMAIN (t2);
328 bool d1_variable, d2_variable;
329 bool d1_zero, d2_zero;
331 /* We should not have any type quals on arrays at all. */
332 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
334 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
335 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
337 d1_variable = (!d1_zero
338 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
339 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
340 d2_variable = (!d2_zero
341 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
342 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
343 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
344 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
346 /* Save space: see if the result is identical to one of the args. */
347 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
348 && (d2_variable || d2_zero || !d1_variable))
349 return build_type_attribute_variant (t1, attributes);
350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
351 && (d1_variable || d1_zero || !d2_variable))
352 return build_type_attribute_variant (t2, attributes);
354 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
355 return build_type_attribute_variant (t1, attributes);
356 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Merge the element types, and have a size if either arg has
360 one. We may have qualifiers on the element types. To set
361 up TYPE_MAIN_VARIANT correctly, we need to form the
362 composite of the unqualified types and add the qualifiers
363 back at the end. */
364 quals = TYPE_QUALS (strip_array_types (elt));
365 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
366 t1 = build_array_type (unqual_elt,
367 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
368 && (d2_variable
369 || d2_zero
370 || !d1_variable))
371 ? t1
372 : t2));
373 t1 = c_build_qualified_type (t1, quals);
374 return build_type_attribute_variant (t1, attributes);
377 case ENUMERAL_TYPE:
378 case RECORD_TYPE:
379 case UNION_TYPE:
380 if (attributes != NULL)
382 /* Try harder not to create a new aggregate type. */
383 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
384 return t1;
385 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
386 return t2;
388 return build_type_attribute_variant (t1, attributes);
390 case FUNCTION_TYPE:
391 /* Function types: prefer the one that specified arg types.
392 If both do, merge the arg types. Also merge the return types. */
394 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
395 tree p1 = TYPE_ARG_TYPES (t1);
396 tree p2 = TYPE_ARG_TYPES (t2);
397 int len;
398 tree newargs, n;
399 int i;
401 /* Save space: see if the result is identical to one of the args. */
402 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
403 return build_type_attribute_variant (t1, attributes);
404 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
405 return build_type_attribute_variant (t2, attributes);
407 /* Simple way if one arg fails to specify argument types. */
408 if (TYPE_ARG_TYPES (t1) == 0)
410 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
414 if (TYPE_ARG_TYPES (t2) == 0)
416 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
417 t1 = build_type_attribute_variant (t1, attributes);
418 return qualify_type (t1, t2);
421 /* If both args specify argument types, we must merge the two
422 lists, argument by argument. */
423 /* Tell global_bindings_p to return false so that variable_size
424 doesn't die on VLAs in parameter types. */
425 c_override_global_bindings_to_false = true;
427 len = list_length (p1);
428 newargs = 0;
430 for (i = 0; i < len; i++)
431 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
433 n = newargs;
435 for (; p1;
436 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
438 /* A null type means arg type is not specified.
439 Take whatever the other function type has. */
440 if (TREE_VALUE (p1) == 0)
442 TREE_VALUE (n) = TREE_VALUE (p2);
443 goto parm_done;
445 if (TREE_VALUE (p2) == 0)
447 TREE_VALUE (n) = TREE_VALUE (p1);
448 goto parm_done;
451 /* Given wait (union {union wait *u; int *i} *)
452 and wait (union wait *),
453 prefer union wait * as type of parm. */
454 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
455 && TREE_VALUE (p1) != TREE_VALUE (p2))
457 tree memb;
458 tree mv2 = TREE_VALUE (p2);
459 if (mv2 && mv2 != error_mark_node
460 && TREE_CODE (mv2) != ARRAY_TYPE)
461 mv2 = TYPE_MAIN_VARIANT (mv2);
462 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
463 memb; memb = TREE_CHAIN (memb))
465 tree mv3 = TREE_TYPE (memb);
466 if (mv3 && mv3 != error_mark_node
467 && TREE_CODE (mv3) != ARRAY_TYPE)
468 mv3 = TYPE_MAIN_VARIANT (mv3);
469 if (comptypes (mv3, mv2))
471 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
472 TREE_VALUE (p2));
473 if (pedantic)
474 pedwarn ("function types not truly compatible in ISO C");
475 goto parm_done;
479 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
480 && TREE_VALUE (p2) != TREE_VALUE (p1))
482 tree memb;
483 tree mv1 = TREE_VALUE (p1);
484 if (mv1 && mv1 != error_mark_node
485 && TREE_CODE (mv1) != ARRAY_TYPE)
486 mv1 = TYPE_MAIN_VARIANT (mv1);
487 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
488 memb; memb = TREE_CHAIN (memb))
490 tree mv3 = TREE_TYPE (memb);
491 if (mv3 && mv3 != error_mark_node
492 && TREE_CODE (mv3) != ARRAY_TYPE)
493 mv3 = TYPE_MAIN_VARIANT (mv3);
494 if (comptypes (mv3, mv1))
496 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
497 TREE_VALUE (p1));
498 if (pedantic)
499 pedwarn ("function types not truly compatible in ISO C");
500 goto parm_done;
504 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
505 parm_done: ;
508 c_override_global_bindings_to_false = false;
509 t1 = build_function_type (valtype, newargs);
510 t1 = qualify_type (t1, t2);
511 /* ... falls through ... */
514 default:
515 return build_type_attribute_variant (t1, attributes);
520 /* Return the type of a conditional expression between pointers to
521 possibly differently qualified versions of compatible types.
523 We assume that comp_target_types has already been done and returned
524 nonzero; if that isn't so, this may crash. */
526 static tree
527 common_pointer_type (tree t1, tree t2)
529 tree attributes;
530 tree pointed_to_1, mv1;
531 tree pointed_to_2, mv2;
532 tree target;
534 /* Save time if the two types are the same. */
536 if (t1 == t2) return t1;
538 /* If one type is nonsense, use the other. */
539 if (t1 == error_mark_node)
540 return t2;
541 if (t2 == error_mark_node)
542 return t1;
544 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
545 && TREE_CODE (t2) == POINTER_TYPE);
547 /* Merge the attributes. */
548 attributes = targetm.merge_type_attributes (t1, t2);
550 /* Find the composite type of the target types, and combine the
551 qualifiers of the two types' targets. Do not lose qualifiers on
552 array element types by taking the TYPE_MAIN_VARIANT. */
553 mv1 = pointed_to_1 = TREE_TYPE (t1);
554 mv2 = pointed_to_2 = TREE_TYPE (t2);
555 if (TREE_CODE (mv1) != ARRAY_TYPE)
556 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
557 if (TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
559 target = composite_type (mv1, mv2);
560 t1 = build_pointer_type (c_build_qualified_type
561 (target,
562 TYPE_QUALS (pointed_to_1) |
563 TYPE_QUALS (pointed_to_2)));
564 return build_type_attribute_variant (t1, attributes);
567 /* Return the common type for two arithmetic types under the usual
568 arithmetic conversions. The default conversions have already been
569 applied, and enumerated types converted to their compatible integer
570 types. The resulting type is unqualified and has no attributes.
572 This is the type for the result of most arithmetic operations
573 if the operands have the given two types. */
575 static tree
576 c_common_type (tree t1, tree t2)
578 enum tree_code code1;
579 enum tree_code code2;
581 /* If one type is nonsense, use the other. */
582 if (t1 == error_mark_node)
583 return t2;
584 if (t2 == error_mark_node)
585 return t1;
587 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
588 t1 = TYPE_MAIN_VARIANT (t1);
590 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
591 t2 = TYPE_MAIN_VARIANT (t2);
593 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
594 t1 = build_type_attribute_variant (t1, NULL_TREE);
596 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
597 t2 = build_type_attribute_variant (t2, NULL_TREE);
599 /* Save time if the two types are the same. */
601 if (t1 == t2) return t1;
603 code1 = TREE_CODE (t1);
604 code2 = TREE_CODE (t2);
606 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
607 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
608 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
609 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
611 /* When one operand is a decimal float type, the other operand cannot be
612 a generic float type or a complex type. We also disallow vector types
613 here. */
614 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
615 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
617 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
619 error ("can%'t mix operands of decimal float and vector types");
620 return error_mark_node;
622 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
624 error ("can%'t mix operands of decimal float and complex types");
625 return error_mark_node;
627 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
629 error ("can%'t mix operands of decimal float and other float types");
630 return error_mark_node;
634 /* If one type is a vector type, return that type. (How the usual
635 arithmetic conversions apply to the vector types extension is not
636 precisely specified.) */
637 if (code1 == VECTOR_TYPE)
638 return t1;
640 if (code2 == VECTOR_TYPE)
641 return t2;
643 /* If one type is complex, form the common type of the non-complex
644 components, then make that complex. Use T1 or T2 if it is the
645 required type. */
646 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
648 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
649 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
650 tree subtype = c_common_type (subtype1, subtype2);
652 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
653 return t1;
654 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
655 return t2;
656 else
657 return build_complex_type (subtype);
660 /* If only one is real, use it as the result. */
662 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
663 return t1;
665 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
666 return t2;
668 /* If both are real and either are decimal floating point types, use
669 the decimal floating point type with the greater precision. */
671 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
673 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
674 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
675 return dfloat128_type_node;
676 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
677 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
678 return dfloat64_type_node;
679 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
680 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
681 return dfloat32_type_node;
684 /* Both real or both integers; use the one with greater precision. */
686 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
687 return t1;
688 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
689 return t2;
691 /* Same precision. Prefer long longs to longs to ints when the
692 same precision, following the C99 rules on integer type rank
693 (which are equivalent to the C90 rules for C90 types). */
695 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
696 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
697 return long_long_unsigned_type_node;
699 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
700 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
702 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
703 return long_long_unsigned_type_node;
704 else
705 return long_long_integer_type_node;
708 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
709 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
710 return long_unsigned_type_node;
712 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
713 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
715 /* But preserve unsignedness from the other type,
716 since long cannot hold all the values of an unsigned int. */
717 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
718 return long_unsigned_type_node;
719 else
720 return long_integer_type_node;
723 /* Likewise, prefer long double to double even if same size. */
724 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
725 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
726 return long_double_type_node;
728 /* Otherwise prefer the unsigned one. */
730 if (TYPE_UNSIGNED (t1))
731 return t1;
732 else
733 return t2;
736 /* Wrapper around c_common_type that is used by c-common.c and other
737 front end optimizations that remove promotions. ENUMERAL_TYPEs
738 are allowed here and are converted to their compatible integer types.
739 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
740 preferably a non-Boolean type as the common type. */
741 tree
742 common_type (tree t1, tree t2)
744 if (TREE_CODE (t1) == ENUMERAL_TYPE)
745 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
746 if (TREE_CODE (t2) == ENUMERAL_TYPE)
747 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
749 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
750 if (TREE_CODE (t1) == BOOLEAN_TYPE
751 && TREE_CODE (t2) == BOOLEAN_TYPE)
752 return boolean_type_node;
754 /* If either type is BOOLEAN_TYPE, then return the other. */
755 if (TREE_CODE (t1) == BOOLEAN_TYPE)
756 return t2;
757 if (TREE_CODE (t2) == BOOLEAN_TYPE)
758 return t1;
760 return c_common_type (t1, t2);
763 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
764 or various other operations. Return 2 if they are compatible
765 but a warning may be needed if you use them together. */
768 comptypes (tree type1, tree type2)
770 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
771 int val;
773 val = comptypes_internal (type1, type2);
774 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
776 return val;
779 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
780 or various other operations. Return 2 if they are compatible
781 but a warning may be needed if you use them together. This
782 differs from comptypes, in that we don't free the seen types. */
784 static int
785 comptypes_internal (tree type1, tree type2)
787 tree t1 = type1;
788 tree t2 = type2;
789 int attrval, val;
791 /* Suppress errors caused by previously reported errors. */
793 if (t1 == t2 || !t1 || !t2
794 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
795 return 1;
797 /* If either type is the internal version of sizetype, return the
798 language version. */
799 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
800 && TYPE_ORIG_SIZE_TYPE (t1))
801 t1 = TYPE_ORIG_SIZE_TYPE (t1);
803 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
804 && TYPE_ORIG_SIZE_TYPE (t2))
805 t2 = TYPE_ORIG_SIZE_TYPE (t2);
808 /* Enumerated types are compatible with integer types, but this is
809 not transitive: two enumerated types in the same translation unit
810 are compatible with each other only if they are the same type. */
812 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
813 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
814 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
815 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
817 if (t1 == t2)
818 return 1;
820 /* Different classes of types can't be compatible. */
822 if (TREE_CODE (t1) != TREE_CODE (t2))
823 return 0;
825 /* Qualifiers must match. C99 6.7.3p9 */
827 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
828 return 0;
830 /* Allow for two different type nodes which have essentially the same
831 definition. Note that we already checked for equality of the type
832 qualifiers (just above). */
834 if (TREE_CODE (t1) != ARRAY_TYPE
835 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
836 return 1;
838 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
839 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
840 return 0;
842 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
843 val = 0;
845 switch (TREE_CODE (t1))
847 case POINTER_TYPE:
848 /* Do not remove mode or aliasing information. */
849 if (TYPE_MODE (t1) != TYPE_MODE (t2)
850 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
851 break;
852 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
853 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
854 break;
856 case FUNCTION_TYPE:
857 val = function_types_compatible_p (t1, t2);
858 break;
860 case ARRAY_TYPE:
862 tree d1 = TYPE_DOMAIN (t1);
863 tree d2 = TYPE_DOMAIN (t2);
864 bool d1_variable, d2_variable;
865 bool d1_zero, d2_zero;
866 val = 1;
868 /* Target types must match incl. qualifiers. */
869 if (TREE_TYPE (t1) != TREE_TYPE (t2)
870 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
871 return 0;
873 /* Sizes must match unless one is missing or variable. */
874 if (d1 == 0 || d2 == 0 || d1 == d2)
875 break;
877 d1_zero = !TYPE_MAX_VALUE (d1);
878 d2_zero = !TYPE_MAX_VALUE (d2);
880 d1_variable = (!d1_zero
881 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
882 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
883 d2_variable = (!d2_zero
884 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
885 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
886 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
887 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
889 if (d1_variable || d2_variable)
890 break;
891 if (d1_zero && d2_zero)
892 break;
893 if (d1_zero || d2_zero
894 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
895 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
896 val = 0;
898 break;
901 case ENUMERAL_TYPE:
902 case RECORD_TYPE:
903 case UNION_TYPE:
904 if (val != 1 && !same_translation_unit_p (t1, t2))
906 tree a1 = TYPE_ATTRIBUTES (t1);
907 tree a2 = TYPE_ATTRIBUTES (t2);
909 if (! attribute_list_contained (a1, a2)
910 && ! attribute_list_contained (a2, a1))
911 break;
913 if (attrval != 2)
914 return tagged_types_tu_compatible_p (t1, t2);
915 val = tagged_types_tu_compatible_p (t1, t2);
917 break;
919 case VECTOR_TYPE:
920 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
921 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
922 break;
924 default:
925 break;
927 return attrval == 2 && val == 1 ? 2 : val;
930 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
931 ignoring their qualifiers. */
933 static int
934 comp_target_types (tree ttl, tree ttr)
936 int val;
937 tree mvl, mvr;
939 /* Do not lose qualifiers on element types of array types that are
940 pointer targets by taking their TYPE_MAIN_VARIANT. */
941 mvl = TREE_TYPE (ttl);
942 mvr = TREE_TYPE (ttr);
943 if (TREE_CODE (mvl) != ARRAY_TYPE)
944 mvl = TYPE_MAIN_VARIANT (mvl);
945 if (TREE_CODE (mvr) != ARRAY_TYPE)
946 mvr = TYPE_MAIN_VARIANT (mvr);
947 val = comptypes (mvl, mvr);
949 if (val == 2 && pedantic)
950 pedwarn ("types are not quite compatible");
951 return val;
954 /* Subroutines of `comptypes'. */
956 /* Determine whether two trees derive from the same translation unit.
957 If the CONTEXT chain ends in a null, that tree's context is still
958 being parsed, so if two trees have context chains ending in null,
959 they're in the same translation unit. */
961 same_translation_unit_p (tree t1, tree t2)
963 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
964 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
966 case tcc_declaration:
967 t1 = DECL_CONTEXT (t1); break;
968 case tcc_type:
969 t1 = TYPE_CONTEXT (t1); break;
970 case tcc_exceptional:
971 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
972 default: gcc_unreachable ();
975 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
976 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
978 case tcc_declaration:
979 t2 = DECL_CONTEXT (t2); break;
980 case tcc_type:
981 t2 = TYPE_CONTEXT (t2); break;
982 case tcc_exceptional:
983 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
984 default: gcc_unreachable ();
987 return t1 == t2;
990 /* Allocate the seen two types, assuming that they are compatible. */
992 static struct tagged_tu_seen_cache *
993 alloc_tagged_tu_seen_cache (tree t1, tree t2)
995 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
996 tu->next = tagged_tu_seen_base;
997 tu->t1 = t1;
998 tu->t2 = t2;
1000 tagged_tu_seen_base = tu;
1002 /* The C standard says that two structures in different translation
1003 units are compatible with each other only if the types of their
1004 fields are compatible (among other things). We assume that they
1005 are compatible until proven otherwise when building the cache.
1006 An example where this can occur is:
1007 struct a
1009 struct a *next;
1011 If we are comparing this against a similar struct in another TU,
1012 and did not assume they were compatible, we end up with an infinite
1013 loop. */
1014 tu->val = 1;
1015 return tu;
1018 /* Free the seen types until we get to TU_TIL. */
1020 static void
1021 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1023 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1024 while (tu != tu_til)
1026 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
1027 tu = tu1->next;
1028 free (tu1);
1030 tagged_tu_seen_base = tu_til;
1033 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1034 compatible. If the two types are not the same (which has been
1035 checked earlier), this can only happen when multiple translation
1036 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1037 rules. */
1039 static int
1040 tagged_types_tu_compatible_p (tree t1, tree t2)
1042 tree s1, s2;
1043 bool needs_warning = false;
1045 /* We have to verify that the tags of the types are the same. This
1046 is harder than it looks because this may be a typedef, so we have
1047 to go look at the original type. It may even be a typedef of a
1048 typedef...
1049 In the case of compiler-created builtin structs the TYPE_DECL
1050 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1051 while (TYPE_NAME (t1)
1052 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1053 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1054 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1056 while (TYPE_NAME (t2)
1057 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1058 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1059 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1061 /* C90 didn't have the requirement that the two tags be the same. */
1062 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1063 return 0;
1065 /* C90 didn't say what happened if one or both of the types were
1066 incomplete; we choose to follow C99 rules here, which is that they
1067 are compatible. */
1068 if (TYPE_SIZE (t1) == NULL
1069 || TYPE_SIZE (t2) == NULL)
1070 return 1;
1073 const struct tagged_tu_seen_cache * tts_i;
1074 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1075 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1076 return tts_i->val;
1079 switch (TREE_CODE (t1))
1081 case ENUMERAL_TYPE:
1083 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1084 /* Speed up the case where the type values are in the same order. */
1085 tree tv1 = TYPE_VALUES (t1);
1086 tree tv2 = TYPE_VALUES (t2);
1088 if (tv1 == tv2)
1090 return 1;
1093 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1095 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1096 break;
1097 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1099 tu->val = 0;
1100 return 0;
1104 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1106 return 1;
1108 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1110 tu->val = 0;
1111 return 0;
1114 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1116 tu->val = 0;
1117 return 0;
1120 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1122 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1123 if (s2 == NULL
1124 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1126 tu->val = 0;
1127 return 0;
1130 return 1;
1133 case UNION_TYPE:
1135 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1136 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1138 tu->val = 0;
1139 return 0;
1142 /* Speed up the common case where the fields are in the same order. */
1143 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1144 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1146 int result;
1149 if (DECL_NAME (s1) == NULL
1150 || DECL_NAME (s1) != DECL_NAME (s2))
1151 break;
1152 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1153 if (result == 0)
1155 tu->val = 0;
1156 return 0;
1158 if (result == 2)
1159 needs_warning = true;
1161 if (TREE_CODE (s1) == FIELD_DECL
1162 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1163 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1165 tu->val = 0;
1166 return 0;
1169 if (!s1 && !s2)
1171 tu->val = needs_warning ? 2 : 1;
1172 return tu->val;
1175 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1177 bool ok = false;
1179 if (DECL_NAME (s1) != NULL)
1180 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1181 if (DECL_NAME (s1) == DECL_NAME (s2))
1183 int result;
1184 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1185 if (result == 0)
1187 tu->val = 0;
1188 return 0;
1190 if (result == 2)
1191 needs_warning = true;
1193 if (TREE_CODE (s1) == FIELD_DECL
1194 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1195 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1196 break;
1198 ok = true;
1199 break;
1201 if (!ok)
1203 tu->val = 0;
1204 return 0;
1207 tu->val = needs_warning ? 2 : 10;
1208 return tu->val;
1211 case RECORD_TYPE:
1213 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1215 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1216 s1 && s2;
1217 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1219 int result;
1220 if (TREE_CODE (s1) != TREE_CODE (s2)
1221 || DECL_NAME (s1) != DECL_NAME (s2))
1222 break;
1223 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1224 if (result == 0)
1225 break;
1226 if (result == 2)
1227 needs_warning = true;
1229 if (TREE_CODE (s1) == FIELD_DECL
1230 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1231 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1232 break;
1234 if (s1 && s2)
1235 tu->val = 0;
1236 else
1237 tu->val = needs_warning ? 2 : 1;
1238 return tu->val;
1241 default:
1242 gcc_unreachable ();
1246 /* Return 1 if two function types F1 and F2 are compatible.
1247 If either type specifies no argument types,
1248 the other must specify a fixed number of self-promoting arg types.
1249 Otherwise, if one type specifies only the number of arguments,
1250 the other must specify that number of self-promoting arg types.
1251 Otherwise, the argument types must match. */
1253 static int
1254 function_types_compatible_p (tree f1, tree f2)
1256 tree args1, args2;
1257 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1258 int val = 1;
1259 int val1;
1260 tree ret1, ret2;
1262 ret1 = TREE_TYPE (f1);
1263 ret2 = TREE_TYPE (f2);
1265 /* 'volatile' qualifiers on a function's return type used to mean
1266 the function is noreturn. */
1267 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1268 pedwarn ("function return types not compatible due to %<volatile%>");
1269 if (TYPE_VOLATILE (ret1))
1270 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1271 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1272 if (TYPE_VOLATILE (ret2))
1273 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1274 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1275 val = comptypes_internal (ret1, ret2);
1276 if (val == 0)
1277 return 0;
1279 args1 = TYPE_ARG_TYPES (f1);
1280 args2 = TYPE_ARG_TYPES (f2);
1282 /* An unspecified parmlist matches any specified parmlist
1283 whose argument types don't need default promotions. */
1285 if (args1 == 0)
1287 if (!self_promoting_args_p (args2))
1288 return 0;
1289 /* If one of these types comes from a non-prototype fn definition,
1290 compare that with the other type's arglist.
1291 If they don't match, ask for a warning (but no error). */
1292 if (TYPE_ACTUAL_ARG_TYPES (f1)
1293 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1294 val = 2;
1295 return val;
1297 if (args2 == 0)
1299 if (!self_promoting_args_p (args1))
1300 return 0;
1301 if (TYPE_ACTUAL_ARG_TYPES (f2)
1302 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1303 val = 2;
1304 return val;
1307 /* Both types have argument lists: compare them and propagate results. */
1308 val1 = type_lists_compatible_p (args1, args2);
1309 return val1 != 1 ? val1 : val;
1312 /* Check two lists of types for compatibility,
1313 returning 0 for incompatible, 1 for compatible,
1314 or 2 for compatible with warning. */
1316 static int
1317 type_lists_compatible_p (tree args1, tree args2)
1319 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1320 int val = 1;
1321 int newval = 0;
1323 while (1)
1325 tree a1, mv1, a2, mv2;
1326 if (args1 == 0 && args2 == 0)
1327 return val;
1328 /* If one list is shorter than the other,
1329 they fail to match. */
1330 if (args1 == 0 || args2 == 0)
1331 return 0;
1332 mv1 = a1 = TREE_VALUE (args1);
1333 mv2 = a2 = TREE_VALUE (args2);
1334 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1335 mv1 = TYPE_MAIN_VARIANT (mv1);
1336 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1337 mv2 = TYPE_MAIN_VARIANT (mv2);
1338 /* A null pointer instead of a type
1339 means there is supposed to be an argument
1340 but nothing is specified about what type it has.
1341 So match anything that self-promotes. */
1342 if (a1 == 0)
1344 if (c_type_promotes_to (a2) != a2)
1345 return 0;
1347 else if (a2 == 0)
1349 if (c_type_promotes_to (a1) != a1)
1350 return 0;
1352 /* If one of the lists has an error marker, ignore this arg. */
1353 else if (TREE_CODE (a1) == ERROR_MARK
1354 || TREE_CODE (a2) == ERROR_MARK)
1356 else if (!(newval = comptypes_internal (mv1, mv2)))
1358 /* Allow wait (union {union wait *u; int *i} *)
1359 and wait (union wait *) to be compatible. */
1360 if (TREE_CODE (a1) == UNION_TYPE
1361 && (TYPE_NAME (a1) == 0
1362 || TYPE_TRANSPARENT_UNION (a1))
1363 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1364 && tree_int_cst_equal (TYPE_SIZE (a1),
1365 TYPE_SIZE (a2)))
1367 tree memb;
1368 for (memb = TYPE_FIELDS (a1);
1369 memb; memb = TREE_CHAIN (memb))
1371 tree mv3 = TREE_TYPE (memb);
1372 if (mv3 && mv3 != error_mark_node
1373 && TREE_CODE (mv3) != ARRAY_TYPE)
1374 mv3 = TYPE_MAIN_VARIANT (mv3);
1375 if (comptypes_internal (mv3, mv2))
1376 break;
1378 if (memb == 0)
1379 return 0;
1381 else if (TREE_CODE (a2) == UNION_TYPE
1382 && (TYPE_NAME (a2) == 0
1383 || TYPE_TRANSPARENT_UNION (a2))
1384 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1385 && tree_int_cst_equal (TYPE_SIZE (a2),
1386 TYPE_SIZE (a1)))
1388 tree memb;
1389 for (memb = TYPE_FIELDS (a2);
1390 memb; memb = TREE_CHAIN (memb))
1392 tree mv3 = TREE_TYPE (memb);
1393 if (mv3 && mv3 != error_mark_node
1394 && TREE_CODE (mv3) != ARRAY_TYPE)
1395 mv3 = TYPE_MAIN_VARIANT (mv3);
1396 if (comptypes_internal (mv3, mv1))
1397 break;
1399 if (memb == 0)
1400 return 0;
1402 else
1403 return 0;
1406 /* comptypes said ok, but record if it said to warn. */
1407 if (newval > val)
1408 val = newval;
1410 args1 = TREE_CHAIN (args1);
1411 args2 = TREE_CHAIN (args2);
1415 /* Compute the size to increment a pointer by. */
1417 static tree
1418 c_size_in_bytes (tree type)
1420 enum tree_code code = TREE_CODE (type);
1422 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1423 return size_one_node;
1425 if (!COMPLETE_OR_VOID_TYPE_P (type))
1427 error ("arithmetic on pointer to an incomplete type");
1428 return size_one_node;
1431 /* Convert in case a char is more than one unit. */
1432 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1433 size_int (TYPE_PRECISION (char_type_node)
1434 / BITS_PER_UNIT));
1437 /* Return either DECL or its known constant value (if it has one). */
1439 tree
1440 decl_constant_value (tree decl)
1442 if (/* Don't change a variable array bound or initial value to a constant
1443 in a place where a variable is invalid. Note that DECL_INITIAL
1444 isn't valid for a PARM_DECL. */
1445 current_function_decl != 0
1446 && TREE_CODE (decl) != PARM_DECL
1447 && !TREE_THIS_VOLATILE (decl)
1448 && TREE_READONLY (decl)
1449 && DECL_INITIAL (decl) != 0
1450 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1451 /* This is invalid if initial value is not constant.
1452 If it has either a function call, a memory reference,
1453 or a variable, then re-evaluating it could give different results. */
1454 && TREE_CONSTANT (DECL_INITIAL (decl))
1455 /* Check for cases where this is sub-optimal, even though valid. */
1456 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1457 return DECL_INITIAL (decl);
1458 return decl;
1461 /* Return either DECL or its known constant value (if it has one), but
1462 return DECL if pedantic or DECL has mode BLKmode. This is for
1463 bug-compatibility with the old behavior of decl_constant_value
1464 (before GCC 3.0); every use of this function is a bug and it should
1465 be removed before GCC 3.1. It is not appropriate to use pedantic
1466 in a way that affects optimization, and BLKmode is probably not the
1467 right test for avoiding misoptimizations either. */
1469 static tree
1470 decl_constant_value_for_broken_optimization (tree decl)
1472 tree ret;
1474 if (pedantic || DECL_MODE (decl) == BLKmode)
1475 return decl;
1477 ret = decl_constant_value (decl);
1478 /* Avoid unwanted tree sharing between the initializer and current
1479 function's body where the tree can be modified e.g. by the
1480 gimplifier. */
1481 if (ret != decl && TREE_STATIC (decl))
1482 ret = unshare_expr (ret);
1483 return ret;
1486 /* Convert the array expression EXP to a pointer. */
1487 static tree
1488 array_to_pointer_conversion (tree exp)
1490 tree orig_exp = exp;
1491 tree type = TREE_TYPE (exp);
1492 tree adr;
1493 tree restype = TREE_TYPE (type);
1494 tree ptrtype;
1496 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1498 STRIP_TYPE_NOPS (exp);
1500 if (TREE_NO_WARNING (orig_exp))
1501 TREE_NO_WARNING (exp) = 1;
1503 ptrtype = build_pointer_type (restype);
1505 if (TREE_CODE (exp) == INDIRECT_REF)
1506 return convert (ptrtype, TREE_OPERAND (exp, 0));
1508 if (TREE_CODE (exp) == VAR_DECL)
1510 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1511 ADDR_EXPR because it's the best way of representing what
1512 happens in C when we take the address of an array and place
1513 it in a pointer to the element type. */
1514 adr = build1 (ADDR_EXPR, ptrtype, exp);
1515 if (!c_mark_addressable (exp))
1516 return error_mark_node;
1517 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1518 return adr;
1521 /* This way is better for a COMPONENT_REF since it can
1522 simplify the offset for a component. */
1523 adr = build_unary_op (ADDR_EXPR, exp, 1);
1524 return convert (ptrtype, adr);
1527 /* Convert the function expression EXP to a pointer. */
1528 static tree
1529 function_to_pointer_conversion (tree exp)
1531 tree orig_exp = exp;
1533 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1535 STRIP_TYPE_NOPS (exp);
1537 if (TREE_NO_WARNING (orig_exp))
1538 TREE_NO_WARNING (exp) = 1;
1540 return build_unary_op (ADDR_EXPR, exp, 0);
1543 /* Perform the default conversion of arrays and functions to pointers.
1544 Return the result of converting EXP. For any other expression, just
1545 return EXP after removing NOPs. */
1547 struct c_expr
1548 default_function_array_conversion (struct c_expr exp)
1550 tree orig_exp = exp.value;
1551 tree type = TREE_TYPE (exp.value);
1552 enum tree_code code = TREE_CODE (type);
1554 switch (code)
1556 case ARRAY_TYPE:
1558 bool not_lvalue = false;
1559 bool lvalue_array_p;
1561 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1562 || TREE_CODE (exp.value) == NOP_EXPR
1563 || TREE_CODE (exp.value) == CONVERT_EXPR)
1564 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1566 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1567 not_lvalue = true;
1568 exp.value = TREE_OPERAND (exp.value, 0);
1571 if (TREE_NO_WARNING (orig_exp))
1572 TREE_NO_WARNING (exp.value) = 1;
1574 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1575 if (!flag_isoc99 && !lvalue_array_p)
1577 /* Before C99, non-lvalue arrays do not decay to pointers.
1578 Normally, using such an array would be invalid; but it can
1579 be used correctly inside sizeof or as a statement expression.
1580 Thus, do not give an error here; an error will result later. */
1581 return exp;
1584 exp.value = array_to_pointer_conversion (exp.value);
1586 break;
1587 case FUNCTION_TYPE:
1588 exp.value = function_to_pointer_conversion (exp.value);
1589 break;
1590 default:
1591 STRIP_TYPE_NOPS (exp.value);
1592 if (TREE_NO_WARNING (orig_exp))
1593 TREE_NO_WARNING (exp.value) = 1;
1594 break;
1597 return exp;
1601 /* EXP is an expression of integer type. Apply the integer promotions
1602 to it and return the promoted value. */
1604 tree
1605 perform_integral_promotions (tree exp)
1607 tree type = TREE_TYPE (exp);
1608 enum tree_code code = TREE_CODE (type);
1610 gcc_assert (INTEGRAL_TYPE_P (type));
1612 /* Normally convert enums to int,
1613 but convert wide enums to something wider. */
1614 if (code == ENUMERAL_TYPE)
1616 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1617 TYPE_PRECISION (integer_type_node)),
1618 ((TYPE_PRECISION (type)
1619 >= TYPE_PRECISION (integer_type_node))
1620 && TYPE_UNSIGNED (type)));
1622 return convert (type, exp);
1625 /* ??? This should no longer be needed now bit-fields have their
1626 proper types. */
1627 if (TREE_CODE (exp) == COMPONENT_REF
1628 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1629 /* If it's thinner than an int, promote it like a
1630 c_promoting_integer_type_p, otherwise leave it alone. */
1631 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1632 TYPE_PRECISION (integer_type_node)))
1633 return convert (integer_type_node, exp);
1635 if (c_promoting_integer_type_p (type))
1637 /* Preserve unsignedness if not really getting any wider. */
1638 if (TYPE_UNSIGNED (type)
1639 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1640 return convert (unsigned_type_node, exp);
1642 return convert (integer_type_node, exp);
1645 return exp;
1649 /* Perform default promotions for C data used in expressions.
1650 Enumeral types or short or char are converted to int.
1651 In addition, manifest constants symbols are replaced by their values. */
1653 tree
1654 default_conversion (tree exp)
1656 tree orig_exp;
1657 tree type = TREE_TYPE (exp);
1658 enum tree_code code = TREE_CODE (type);
1660 /* Functions and arrays have been converted during parsing. */
1661 gcc_assert (code != FUNCTION_TYPE);
1662 if (code == ARRAY_TYPE)
1663 return exp;
1665 /* Constants can be used directly unless they're not loadable. */
1666 if (TREE_CODE (exp) == CONST_DECL)
1667 exp = DECL_INITIAL (exp);
1669 /* Replace a nonvolatile const static variable with its value unless
1670 it is an array, in which case we must be sure that taking the
1671 address of the array produces consistent results. */
1672 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1674 exp = decl_constant_value_for_broken_optimization (exp);
1675 type = TREE_TYPE (exp);
1678 /* Strip no-op conversions. */
1679 orig_exp = exp;
1680 STRIP_TYPE_NOPS (exp);
1682 if (TREE_NO_WARNING (orig_exp))
1683 TREE_NO_WARNING (exp) = 1;
1685 if (INTEGRAL_TYPE_P (type))
1686 return perform_integral_promotions (exp);
1688 if (code == VOID_TYPE)
1690 error ("void value not ignored as it ought to be");
1691 return error_mark_node;
1693 return exp;
1696 /* Look up COMPONENT in a structure or union DECL.
1698 If the component name is not found, returns NULL_TREE. Otherwise,
1699 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1700 stepping down the chain to the component, which is in the last
1701 TREE_VALUE of the list. Normally the list is of length one, but if
1702 the component is embedded within (nested) anonymous structures or
1703 unions, the list steps down the chain to the component. */
1705 static tree
1706 lookup_field (tree decl, tree component)
1708 tree type = TREE_TYPE (decl);
1709 tree field;
1711 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1712 to the field elements. Use a binary search on this array to quickly
1713 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1714 will always be set for structures which have many elements. */
1716 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1718 int bot, top, half;
1719 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1721 field = TYPE_FIELDS (type);
1722 bot = 0;
1723 top = TYPE_LANG_SPECIFIC (type)->s->len;
1724 while (top - bot > 1)
1726 half = (top - bot + 1) >> 1;
1727 field = field_array[bot+half];
1729 if (DECL_NAME (field) == NULL_TREE)
1731 /* Step through all anon unions in linear fashion. */
1732 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1734 field = field_array[bot++];
1735 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1736 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1738 tree anon = lookup_field (field, component);
1740 if (anon)
1741 return tree_cons (NULL_TREE, field, anon);
1745 /* Entire record is only anon unions. */
1746 if (bot > top)
1747 return NULL_TREE;
1749 /* Restart the binary search, with new lower bound. */
1750 continue;
1753 if (DECL_NAME (field) == component)
1754 break;
1755 if (DECL_NAME (field) < component)
1756 bot += half;
1757 else
1758 top = bot + half;
1761 if (DECL_NAME (field_array[bot]) == component)
1762 field = field_array[bot];
1763 else if (DECL_NAME (field) != component)
1764 return NULL_TREE;
1766 else
1768 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1770 if (DECL_NAME (field) == NULL_TREE
1771 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1772 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1774 tree anon = lookup_field (field, component);
1776 if (anon)
1777 return tree_cons (NULL_TREE, field, anon);
1780 if (DECL_NAME (field) == component)
1781 break;
1784 if (field == NULL_TREE)
1785 return NULL_TREE;
1788 return tree_cons (NULL_TREE, field, NULL_TREE);
1791 /* Make an expression to refer to the COMPONENT field of
1792 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1794 tree
1795 build_component_ref (tree datum, tree component)
1797 tree type = TREE_TYPE (datum);
1798 enum tree_code code = TREE_CODE (type);
1799 tree field = NULL;
1800 tree ref;
1802 if (!objc_is_public (datum, component))
1803 return error_mark_node;
1805 /* See if there is a field or component with name COMPONENT. */
1807 if (code == RECORD_TYPE || code == UNION_TYPE)
1809 if (!COMPLETE_TYPE_P (type))
1811 c_incomplete_type_error (NULL_TREE, type);
1812 return error_mark_node;
1815 field = lookup_field (datum, component);
1817 if (!field)
1819 error ("%qT has no member named %qE", type, component);
1820 return error_mark_node;
1823 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1824 This might be better solved in future the way the C++ front
1825 end does it - by giving the anonymous entities each a
1826 separate name and type, and then have build_component_ref
1827 recursively call itself. We can't do that here. */
1830 tree subdatum = TREE_VALUE (field);
1831 int quals;
1832 tree subtype;
1834 if (TREE_TYPE (subdatum) == error_mark_node)
1835 return error_mark_node;
1837 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1838 quals |= TYPE_QUALS (TREE_TYPE (datum));
1839 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1841 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1842 NULL_TREE);
1843 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1844 TREE_READONLY (ref) = 1;
1845 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1846 TREE_THIS_VOLATILE (ref) = 1;
1848 if (TREE_DEPRECATED (subdatum))
1849 warn_deprecated_use (subdatum);
1851 datum = ref;
1853 field = TREE_CHAIN (field);
1855 while (field);
1857 return ref;
1859 else if (code != ERROR_MARK)
1860 error ("request for member %qE in something not a structure or union",
1861 component);
1863 return error_mark_node;
1866 /* Given an expression PTR for a pointer, return an expression
1867 for the value pointed to.
1868 ERRORSTRING is the name of the operator to appear in error messages. */
1870 tree
1871 build_indirect_ref (tree ptr, const char *errorstring)
1873 tree pointer = default_conversion (ptr);
1874 tree type = TREE_TYPE (pointer);
1876 if (TREE_CODE (type) == POINTER_TYPE)
1878 if (TREE_CODE (pointer) == ADDR_EXPR
1879 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1880 == TREE_TYPE (type)))
1881 return TREE_OPERAND (pointer, 0);
1882 else
1884 tree t = TREE_TYPE (type);
1885 tree ref;
1887 ref = build1 (INDIRECT_REF, t, pointer);
1889 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1891 error ("dereferencing pointer to incomplete type");
1892 return error_mark_node;
1894 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1895 warning (0, "dereferencing %<void *%> pointer");
1897 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1898 so that we get the proper error message if the result is used
1899 to assign to. Also, &* is supposed to be a no-op.
1900 And ANSI C seems to specify that the type of the result
1901 should be the const type. */
1902 /* A de-reference of a pointer to const is not a const. It is valid
1903 to change it via some other pointer. */
1904 TREE_READONLY (ref) = TYPE_READONLY (t);
1905 TREE_SIDE_EFFECTS (ref)
1906 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1907 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1908 return ref;
1911 else if (TREE_CODE (pointer) != ERROR_MARK)
1912 error ("invalid type argument of %qs", errorstring);
1913 return error_mark_node;
1916 /* This handles expressions of the form "a[i]", which denotes
1917 an array reference.
1919 This is logically equivalent in C to *(a+i), but we may do it differently.
1920 If A is a variable or a member, we generate a primitive ARRAY_REF.
1921 This avoids forcing the array out of registers, and can work on
1922 arrays that are not lvalues (for example, members of structures returned
1923 by functions). */
1925 tree
1926 build_array_ref (tree array, tree index)
1928 bool swapped = false;
1929 if (TREE_TYPE (array) == error_mark_node
1930 || TREE_TYPE (index) == error_mark_node)
1931 return error_mark_node;
1933 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1934 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1936 tree temp;
1937 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1938 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1940 error ("subscripted value is neither array nor pointer");
1941 return error_mark_node;
1943 temp = array;
1944 array = index;
1945 index = temp;
1946 swapped = true;
1949 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1951 error ("array subscript is not an integer");
1952 return error_mark_node;
1955 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1957 error ("subscripted value is pointer to function");
1958 return error_mark_node;
1961 /* ??? Existing practice has been to warn only when the char
1962 index is syntactically the index, not for char[array]. */
1963 if (!swapped)
1964 warn_array_subscript_with_type_char (index);
1966 /* Apply default promotions *after* noticing character types. */
1967 index = default_conversion (index);
1969 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1971 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1973 tree rval, type;
1975 /* An array that is indexed by a non-constant
1976 cannot be stored in a register; we must be able to do
1977 address arithmetic on its address.
1978 Likewise an array of elements of variable size. */
1979 if (TREE_CODE (index) != INTEGER_CST
1980 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1981 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1983 if (!c_mark_addressable (array))
1984 return error_mark_node;
1986 /* An array that is indexed by a constant value which is not within
1987 the array bounds cannot be stored in a register either; because we
1988 would get a crash in store_bit_field/extract_bit_field when trying
1989 to access a non-existent part of the register. */
1990 if (TREE_CODE (index) == INTEGER_CST
1991 && TYPE_DOMAIN (TREE_TYPE (array))
1992 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1994 if (!c_mark_addressable (array))
1995 return error_mark_node;
1998 if (pedantic)
2000 tree foo = array;
2001 while (TREE_CODE (foo) == COMPONENT_REF)
2002 foo = TREE_OPERAND (foo, 0);
2003 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2004 pedwarn ("ISO C forbids subscripting %<register%> array");
2005 else if (!flag_isoc99 && !lvalue_p (foo))
2006 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
2009 type = TREE_TYPE (TREE_TYPE (array));
2010 if (TREE_CODE (type) != ARRAY_TYPE)
2011 type = TYPE_MAIN_VARIANT (type);
2012 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2013 /* Array ref is const/volatile if the array elements are
2014 or if the array is. */
2015 TREE_READONLY (rval)
2016 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2017 | TREE_READONLY (array));
2018 TREE_SIDE_EFFECTS (rval)
2019 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2020 | TREE_SIDE_EFFECTS (array));
2021 TREE_THIS_VOLATILE (rval)
2022 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2023 /* This was added by rms on 16 Nov 91.
2024 It fixes vol struct foo *a; a->elts[1]
2025 in an inline function.
2026 Hope it doesn't break something else. */
2027 | TREE_THIS_VOLATILE (array));
2028 return require_complete_type (fold (rval));
2030 else
2032 tree ar = default_conversion (array);
2034 if (ar == error_mark_node)
2035 return ar;
2037 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2038 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2040 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2041 "array indexing");
2045 /* Build an external reference to identifier ID. FUN indicates
2046 whether this will be used for a function call. LOC is the source
2047 location of the identifier. */
2048 tree
2049 build_external_ref (tree id, int fun, location_t loc)
2051 tree ref;
2052 tree decl = lookup_name (id);
2054 /* In Objective-C, an instance variable (ivar) may be preferred to
2055 whatever lookup_name() found. */
2056 decl = objc_lookup_ivar (decl, id);
2058 if (decl && decl != error_mark_node)
2059 ref = decl;
2060 else if (fun)
2061 /* Implicit function declaration. */
2062 ref = implicitly_declare (id);
2063 else if (decl == error_mark_node)
2064 /* Don't complain about something that's already been
2065 complained about. */
2066 return error_mark_node;
2067 else
2069 undeclared_variable (id, loc);
2070 return error_mark_node;
2073 if (TREE_TYPE (ref) == error_mark_node)
2074 return error_mark_node;
2076 if (TREE_DEPRECATED (ref))
2077 warn_deprecated_use (ref);
2079 if (!skip_evaluation)
2080 assemble_external (ref);
2081 TREE_USED (ref) = 1;
2083 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2085 if (!in_sizeof && !in_typeof)
2086 C_DECL_USED (ref) = 1;
2087 else if (DECL_INITIAL (ref) == 0
2088 && DECL_EXTERNAL (ref)
2089 && !TREE_PUBLIC (ref))
2090 record_maybe_used_decl (ref);
2093 if (TREE_CODE (ref) == CONST_DECL)
2095 used_types_insert (TREE_TYPE (ref));
2096 ref = DECL_INITIAL (ref);
2097 TREE_CONSTANT (ref) = 1;
2098 TREE_INVARIANT (ref) = 1;
2100 else if (current_function_decl != 0
2101 && !DECL_FILE_SCOPE_P (current_function_decl)
2102 && (TREE_CODE (ref) == VAR_DECL
2103 || TREE_CODE (ref) == PARM_DECL
2104 || TREE_CODE (ref) == FUNCTION_DECL))
2106 tree context = decl_function_context (ref);
2108 if (context != 0 && context != current_function_decl)
2109 DECL_NONLOCAL (ref) = 1;
2112 return ref;
2115 /* Record details of decls possibly used inside sizeof or typeof. */
2116 struct maybe_used_decl
2118 /* The decl. */
2119 tree decl;
2120 /* The level seen at (in_sizeof + in_typeof). */
2121 int level;
2122 /* The next one at this level or above, or NULL. */
2123 struct maybe_used_decl *next;
2126 static struct maybe_used_decl *maybe_used_decls;
2128 /* Record that DECL, an undefined static function reference seen
2129 inside sizeof or typeof, might be used if the operand of sizeof is
2130 a VLA type or the operand of typeof is a variably modified
2131 type. */
2133 static void
2134 record_maybe_used_decl (tree decl)
2136 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2137 t->decl = decl;
2138 t->level = in_sizeof + in_typeof;
2139 t->next = maybe_used_decls;
2140 maybe_used_decls = t;
2143 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2144 USED is false, just discard them. If it is true, mark them used
2145 (if no longer inside sizeof or typeof) or move them to the next
2146 level up (if still inside sizeof or typeof). */
2148 void
2149 pop_maybe_used (bool used)
2151 struct maybe_used_decl *p = maybe_used_decls;
2152 int cur_level = in_sizeof + in_typeof;
2153 while (p && p->level > cur_level)
2155 if (used)
2157 if (cur_level == 0)
2158 C_DECL_USED (p->decl) = 1;
2159 else
2160 p->level = cur_level;
2162 p = p->next;
2164 if (!used || cur_level == 0)
2165 maybe_used_decls = p;
2168 /* Return the result of sizeof applied to EXPR. */
2170 struct c_expr
2171 c_expr_sizeof_expr (struct c_expr expr)
2173 struct c_expr ret;
2174 if (expr.value == error_mark_node)
2176 ret.value = error_mark_node;
2177 ret.original_code = ERROR_MARK;
2178 pop_maybe_used (false);
2180 else
2182 ret.value = c_sizeof (TREE_TYPE (expr.value));
2183 ret.original_code = ERROR_MARK;
2184 if (c_vla_type_p (TREE_TYPE (expr.value)))
2186 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2187 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2189 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2191 return ret;
2194 /* Return the result of sizeof applied to T, a structure for the type
2195 name passed to sizeof (rather than the type itself). */
2197 struct c_expr
2198 c_expr_sizeof_type (struct c_type_name *t)
2200 tree type;
2201 struct c_expr ret;
2202 type = groktypename (t);
2203 ret.value = c_sizeof (type);
2204 ret.original_code = ERROR_MARK;
2205 pop_maybe_used (type != error_mark_node
2206 ? C_TYPE_VARIABLE_SIZE (type) : false);
2207 return ret;
2210 /* Build a function call to function FUNCTION with parameters PARAMS.
2211 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2212 TREE_VALUE of each node is a parameter-expression.
2213 FUNCTION's data type may be a function type or a pointer-to-function. */
2215 tree
2216 build_function_call (tree function, tree params)
2218 tree fntype, fundecl = 0;
2219 tree coerced_params;
2220 tree name = NULL_TREE, result;
2221 tree tem;
2223 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2224 STRIP_TYPE_NOPS (function);
2226 /* Convert anything with function type to a pointer-to-function. */
2227 if (TREE_CODE (function) == FUNCTION_DECL)
2229 /* Implement type-directed function overloading for builtins.
2230 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2231 handle all the type checking. The result is a complete expression
2232 that implements this function call. */
2233 tem = resolve_overloaded_builtin (function, params);
2234 if (tem)
2235 return tem;
2237 name = DECL_NAME (function);
2238 fundecl = function;
2240 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2241 function = function_to_pointer_conversion (function);
2243 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2244 expressions, like those used for ObjC messenger dispatches. */
2245 function = objc_rewrite_function_call (function, params);
2247 fntype = TREE_TYPE (function);
2249 if (TREE_CODE (fntype) == ERROR_MARK)
2250 return error_mark_node;
2252 if (!(TREE_CODE (fntype) == POINTER_TYPE
2253 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2255 error ("called object %qE is not a function", function);
2256 return error_mark_node;
2259 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2260 current_function_returns_abnormally = 1;
2262 /* fntype now gets the type of function pointed to. */
2263 fntype = TREE_TYPE (fntype);
2265 /* Check that the function is called through a compatible prototype.
2266 If it is not, replace the call by a trap, wrapped up in a compound
2267 expression if necessary. This has the nice side-effect to prevent
2268 the tree-inliner from generating invalid assignment trees which may
2269 blow up in the RTL expander later. */
2270 if ((TREE_CODE (function) == NOP_EXPR
2271 || TREE_CODE (function) == CONVERT_EXPR)
2272 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2273 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2274 && !comptypes (fntype, TREE_TYPE (tem)))
2276 tree return_type = TREE_TYPE (fntype);
2277 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2278 NULL_TREE);
2280 /* This situation leads to run-time undefined behavior. We can't,
2281 therefore, simply error unless we can prove that all possible
2282 executions of the program must execute the code. */
2283 warning (0, "function called through a non-compatible type");
2285 /* We can, however, treat "undefined" any way we please.
2286 Call abort to encourage the user to fix the program. */
2287 inform ("if this code is reached, the program will abort");
2289 if (VOID_TYPE_P (return_type))
2290 return trap;
2291 else
2293 tree rhs;
2295 if (AGGREGATE_TYPE_P (return_type))
2296 rhs = build_compound_literal (return_type,
2297 build_constructor (return_type, 0));
2298 else
2299 rhs = fold_convert (return_type, integer_zero_node);
2301 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2305 /* Convert the parameters to the types declared in the
2306 function prototype, or apply default promotions. */
2308 coerced_params
2309 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2311 if (coerced_params == error_mark_node)
2312 return error_mark_node;
2314 /* Check that the arguments to the function are valid. */
2316 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2317 TYPE_ARG_TYPES (fntype));
2319 if (require_constant_value)
2321 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2322 function, coerced_params, NULL_TREE);
2324 if (TREE_CONSTANT (result)
2325 && (name == NULL_TREE
2326 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2327 pedwarn_init ("initializer element is not constant");
2329 else
2330 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2331 function, coerced_params, NULL_TREE);
2333 if (VOID_TYPE_P (TREE_TYPE (result)))
2334 return result;
2335 return require_complete_type (result);
2338 /* Convert the argument expressions in the list VALUES
2339 to the types in the list TYPELIST. The result is a list of converted
2340 argument expressions, unless there are too few arguments in which
2341 case it is error_mark_node.
2343 If TYPELIST is exhausted, or when an element has NULL as its type,
2344 perform the default conversions.
2346 PARMLIST is the chain of parm decls for the function being called.
2347 It may be 0, if that info is not available.
2348 It is used only for generating error messages.
2350 FUNCTION is a tree for the called function. It is used only for
2351 error messages, where it is formatted with %qE.
2353 This is also where warnings about wrong number of args are generated.
2355 Both VALUES and the returned value are chains of TREE_LIST nodes
2356 with the elements of the list in the TREE_VALUE slots of those nodes. */
2358 static tree
2359 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2361 tree typetail, valtail;
2362 tree result = NULL;
2363 int parmnum;
2364 tree selector;
2366 /* Change pointer to function to the function itself for
2367 diagnostics. */
2368 if (TREE_CODE (function) == ADDR_EXPR
2369 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2370 function = TREE_OPERAND (function, 0);
2372 /* Handle an ObjC selector specially for diagnostics. */
2373 selector = objc_message_selector ();
2375 /* Scan the given expressions and types, producing individual
2376 converted arguments and pushing them on RESULT in reverse order. */
2378 for (valtail = values, typetail = typelist, parmnum = 0;
2379 valtail;
2380 valtail = TREE_CHAIN (valtail), parmnum++)
2382 tree type = typetail ? TREE_VALUE (typetail) : 0;
2383 tree val = TREE_VALUE (valtail);
2384 tree rname = function;
2385 int argnum = parmnum + 1;
2386 const char *invalid_func_diag;
2388 if (type == void_type_node)
2390 error ("too many arguments to function %qE", function);
2391 break;
2394 if (selector && argnum > 2)
2396 rname = selector;
2397 argnum -= 2;
2400 STRIP_TYPE_NOPS (val);
2402 val = require_complete_type (val);
2404 if (type != 0)
2406 /* Formal parm type is specified by a function prototype. */
2407 tree parmval;
2409 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2411 error ("type of formal parameter %d is incomplete", parmnum + 1);
2412 parmval = val;
2414 else
2416 /* Optionally warn about conversions that
2417 differ from the default conversions. */
2418 if (warn_conversion || warn_traditional)
2420 unsigned int formal_prec = TYPE_PRECISION (type);
2422 if (INTEGRAL_TYPE_P (type)
2423 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2424 warning (0, "passing argument %d of %qE as integer "
2425 "rather than floating due to prototype",
2426 argnum, rname);
2427 if (INTEGRAL_TYPE_P (type)
2428 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2429 warning (0, "passing argument %d of %qE as integer "
2430 "rather than complex due to prototype",
2431 argnum, rname);
2432 else if (TREE_CODE (type) == COMPLEX_TYPE
2433 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2434 warning (0, "passing argument %d of %qE as complex "
2435 "rather than floating due to prototype",
2436 argnum, rname);
2437 else if (TREE_CODE (type) == REAL_TYPE
2438 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2439 warning (0, "passing argument %d of %qE as floating "
2440 "rather than integer due to prototype",
2441 argnum, rname);
2442 else if (TREE_CODE (type) == COMPLEX_TYPE
2443 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2444 warning (0, "passing argument %d of %qE as complex "
2445 "rather than integer due to prototype",
2446 argnum, rname);
2447 else if (TREE_CODE (type) == REAL_TYPE
2448 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2449 warning (0, "passing argument %d of %qE as floating "
2450 "rather than complex due to prototype",
2451 argnum, rname);
2452 /* ??? At some point, messages should be written about
2453 conversions between complex types, but that's too messy
2454 to do now. */
2455 else if (TREE_CODE (type) == REAL_TYPE
2456 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2458 /* Warn if any argument is passed as `float',
2459 since without a prototype it would be `double'. */
2460 if (formal_prec == TYPE_PRECISION (float_type_node)
2461 && type != dfloat32_type_node)
2462 warning (0, "passing argument %d of %qE as %<float%> "
2463 "rather than %<double%> due to prototype",
2464 argnum, rname);
2466 /* Warn if mismatch between argument and prototype
2467 for decimal float types. Warn of conversions with
2468 binary float types and of precision narrowing due to
2469 prototype. */
2470 else if (type != TREE_TYPE (val)
2471 && (type == dfloat32_type_node
2472 || type == dfloat64_type_node
2473 || type == dfloat128_type_node
2474 || TREE_TYPE (val) == dfloat32_type_node
2475 || TREE_TYPE (val) == dfloat64_type_node
2476 || TREE_TYPE (val) == dfloat128_type_node)
2477 && (formal_prec
2478 <= TYPE_PRECISION (TREE_TYPE (val))
2479 || (type == dfloat128_type_node
2480 && (TREE_TYPE (val)
2481 != dfloat64_type_node
2482 && (TREE_TYPE (val)
2483 != dfloat32_type_node)))
2484 || (type == dfloat64_type_node
2485 && (TREE_TYPE (val)
2486 != dfloat32_type_node))))
2487 warning (0, "passing argument %d of %qE as %qT "
2488 "rather than %qT due to prototype",
2489 argnum, rname, type, TREE_TYPE (val));
2492 /* Detect integer changing in width or signedness.
2493 These warnings are only activated with
2494 -Wconversion, not with -Wtraditional. */
2495 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2496 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2498 tree would_have_been = default_conversion (val);
2499 tree type1 = TREE_TYPE (would_have_been);
2501 if (TREE_CODE (type) == ENUMERAL_TYPE
2502 && (TYPE_MAIN_VARIANT (type)
2503 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2504 /* No warning if function asks for enum
2505 and the actual arg is that enum type. */
2507 else if (formal_prec != TYPE_PRECISION (type1))
2508 warning (OPT_Wconversion, "passing argument %d of %qE "
2509 "with different width due to prototype",
2510 argnum, rname);
2511 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2513 /* Don't complain if the formal parameter type
2514 is an enum, because we can't tell now whether
2515 the value was an enum--even the same enum. */
2516 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2518 else if (TREE_CODE (val) == INTEGER_CST
2519 && int_fits_type_p (val, type))
2520 /* Change in signedness doesn't matter
2521 if a constant value is unaffected. */
2523 /* If the value is extended from a narrower
2524 unsigned type, it doesn't matter whether we
2525 pass it as signed or unsigned; the value
2526 certainly is the same either way. */
2527 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2528 && TYPE_UNSIGNED (TREE_TYPE (val)))
2530 else if (TYPE_UNSIGNED (type))
2531 warning (OPT_Wconversion, "passing argument %d of %qE "
2532 "as unsigned due to prototype",
2533 argnum, rname);
2534 else
2535 warning (OPT_Wconversion, "passing argument %d of %qE "
2536 "as signed due to prototype", argnum, rname);
2540 parmval = convert_for_assignment (type, val, ic_argpass,
2541 fundecl, function,
2542 parmnum + 1);
2544 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2545 && INTEGRAL_TYPE_P (type)
2546 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2547 parmval = default_conversion (parmval);
2549 result = tree_cons (NULL_TREE, parmval, result);
2551 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2552 && (TYPE_PRECISION (TREE_TYPE (val))
2553 < TYPE_PRECISION (double_type_node))
2554 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2555 /* Convert `float' to `double'. */
2556 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2557 else if ((invalid_func_diag =
2558 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2560 error (invalid_func_diag);
2561 return error_mark_node;
2563 else
2564 /* Convert `short' and `char' to full-size `int'. */
2565 result = tree_cons (NULL_TREE, default_conversion (val), result);
2567 if (typetail)
2568 typetail = TREE_CHAIN (typetail);
2571 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2573 error ("too few arguments to function %qE", function);
2574 return error_mark_node;
2577 return nreverse (result);
2580 /* This is the entry point used by the parser to build unary operators
2581 in the input. CODE, a tree_code, specifies the unary operator, and
2582 ARG is the operand. For unary plus, the C parser currently uses
2583 CONVERT_EXPR for code. */
2585 struct c_expr
2586 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2588 struct c_expr result;
2590 result.original_code = ERROR_MARK;
2591 result.value = build_unary_op (code, arg.value, 0);
2592 overflow_warning (result.value);
2593 return result;
2596 /* This is the entry point used by the parser to build binary operators
2597 in the input. CODE, a tree_code, specifies the binary operator, and
2598 ARG1 and ARG2 are the operands. In addition to constructing the
2599 expression, we check for operands that were written with other binary
2600 operators in a way that is likely to confuse the user. */
2602 struct c_expr
2603 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2604 struct c_expr arg2)
2606 struct c_expr result;
2608 enum tree_code code1 = arg1.original_code;
2609 enum tree_code code2 = arg2.original_code;
2611 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2612 result.original_code = code;
2614 if (TREE_CODE (result.value) == ERROR_MARK)
2615 return result;
2617 /* Check for cases such as x+y<<z which users are likely
2618 to misinterpret. */
2619 if (warn_parentheses)
2621 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2623 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2624 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2625 warning (OPT_Wparentheses,
2626 "suggest parentheses around + or - inside shift");
2629 if (code == TRUTH_ORIF_EXPR)
2631 if (code1 == TRUTH_ANDIF_EXPR
2632 || code2 == TRUTH_ANDIF_EXPR)
2633 warning (OPT_Wparentheses,
2634 "suggest parentheses around && within ||");
2637 if (code == BIT_IOR_EXPR)
2639 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2640 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2641 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2642 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2643 warning (OPT_Wparentheses,
2644 "suggest parentheses around arithmetic in operand of |");
2645 /* Check cases like x|y==z */
2646 if (TREE_CODE_CLASS (code1) == tcc_comparison
2647 || TREE_CODE_CLASS (code2) == tcc_comparison)
2648 warning (OPT_Wparentheses,
2649 "suggest parentheses around comparison in operand of |");
2652 if (code == BIT_XOR_EXPR)
2654 if (code1 == BIT_AND_EXPR
2655 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2656 || code2 == BIT_AND_EXPR
2657 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2658 warning (OPT_Wparentheses,
2659 "suggest parentheses around arithmetic in operand of ^");
2660 /* Check cases like x^y==z */
2661 if (TREE_CODE_CLASS (code1) == tcc_comparison
2662 || TREE_CODE_CLASS (code2) == tcc_comparison)
2663 warning (OPT_Wparentheses,
2664 "suggest parentheses around comparison in operand of ^");
2667 if (code == BIT_AND_EXPR)
2669 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2670 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2671 warning (OPT_Wparentheses,
2672 "suggest parentheses around + or - in operand of &");
2673 /* Check cases like x&y==z */
2674 if (TREE_CODE_CLASS (code1) == tcc_comparison
2675 || TREE_CODE_CLASS (code2) == tcc_comparison)
2676 warning (OPT_Wparentheses,
2677 "suggest parentheses around comparison in operand of &");
2679 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2680 if (TREE_CODE_CLASS (code) == tcc_comparison
2681 && (TREE_CODE_CLASS (code1) == tcc_comparison
2682 || TREE_CODE_CLASS (code2) == tcc_comparison))
2683 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2684 "have their mathematical meaning");
2688 /* Warn about comparisons against string literals, with the exception
2689 of testing for equality or inequality of a string literal with NULL. */
2690 if (code == EQ_EXPR || code == NE_EXPR)
2692 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2693 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2694 warning (OPT_Waddress,
2695 "comparison with string literal results in unspecified behaviour");
2697 else if (TREE_CODE_CLASS (code) == tcc_comparison
2698 && (code1 == STRING_CST || code2 == STRING_CST))
2699 warning (OPT_Waddress,
2700 "comparison with string literal results in unspecified behaviour");
2702 overflow_warning (result.value);
2704 return result;
2707 /* Return a tree for the difference of pointers OP0 and OP1.
2708 The resulting tree has type int. */
2710 static tree
2711 pointer_diff (tree op0, tree op1)
2713 tree restype = ptrdiff_type_node;
2715 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2716 tree con0, con1, lit0, lit1;
2717 tree orig_op1 = op1;
2719 if (pedantic || warn_pointer_arith)
2721 if (TREE_CODE (target_type) == VOID_TYPE)
2722 pedwarn ("pointer of type %<void *%> used in subtraction");
2723 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2724 pedwarn ("pointer to a function used in subtraction");
2727 /* If the conversion to ptrdiff_type does anything like widening or
2728 converting a partial to an integral mode, we get a convert_expression
2729 that is in the way to do any simplifications.
2730 (fold-const.c doesn't know that the extra bits won't be needed.
2731 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2732 different mode in place.)
2733 So first try to find a common term here 'by hand'; we want to cover
2734 at least the cases that occur in legal static initializers. */
2735 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2736 && (TYPE_PRECISION (TREE_TYPE (op0))
2737 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2738 con0 = TREE_OPERAND (op0, 0);
2739 else
2740 con0 = op0;
2741 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2742 && (TYPE_PRECISION (TREE_TYPE (op1))
2743 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2744 con1 = TREE_OPERAND (op1, 0);
2745 else
2746 con1 = op1;
2748 if (TREE_CODE (con0) == PLUS_EXPR)
2750 lit0 = TREE_OPERAND (con0, 1);
2751 con0 = TREE_OPERAND (con0, 0);
2753 else
2754 lit0 = integer_zero_node;
2756 if (TREE_CODE (con1) == PLUS_EXPR)
2758 lit1 = TREE_OPERAND (con1, 1);
2759 con1 = TREE_OPERAND (con1, 0);
2761 else
2762 lit1 = integer_zero_node;
2764 if (operand_equal_p (con0, con1, 0))
2766 op0 = lit0;
2767 op1 = lit1;
2771 /* First do the subtraction as integers;
2772 then drop through to build the divide operator.
2773 Do not do default conversions on the minus operator
2774 in case restype is a short type. */
2776 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2777 convert (restype, op1), 0);
2778 /* This generates an error if op1 is pointer to incomplete type. */
2779 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2780 error ("arithmetic on pointer to an incomplete type");
2782 /* This generates an error if op0 is pointer to incomplete type. */
2783 op1 = c_size_in_bytes (target_type);
2785 /* Divide by the size, in easiest possible way. */
2786 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2789 /* Construct and perhaps optimize a tree representation
2790 for a unary operation. CODE, a tree_code, specifies the operation
2791 and XARG is the operand.
2792 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2793 the default promotions (such as from short to int).
2794 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2795 allows non-lvalues; this is only used to handle conversion of non-lvalue
2796 arrays to pointers in C99. */
2798 tree
2799 build_unary_op (enum tree_code code, tree xarg, int flag)
2801 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2802 tree arg = xarg;
2803 tree argtype = 0;
2804 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2805 tree val;
2806 int noconvert = flag;
2807 const char *invalid_op_diag;
2809 if (typecode == ERROR_MARK)
2810 return error_mark_node;
2811 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2812 typecode = INTEGER_TYPE;
2814 if ((invalid_op_diag
2815 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2817 error (invalid_op_diag);
2818 return error_mark_node;
2821 switch (code)
2823 case CONVERT_EXPR:
2824 /* This is used for unary plus, because a CONVERT_EXPR
2825 is enough to prevent anybody from looking inside for
2826 associativity, but won't generate any code. */
2827 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2828 || typecode == COMPLEX_TYPE
2829 || typecode == VECTOR_TYPE))
2831 error ("wrong type argument to unary plus");
2832 return error_mark_node;
2834 else if (!noconvert)
2835 arg = default_conversion (arg);
2836 arg = non_lvalue (arg);
2837 break;
2839 case NEGATE_EXPR:
2840 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2841 || typecode == COMPLEX_TYPE
2842 || typecode == VECTOR_TYPE))
2844 error ("wrong type argument to unary minus");
2845 return error_mark_node;
2847 else if (!noconvert)
2848 arg = default_conversion (arg);
2849 break;
2851 case BIT_NOT_EXPR:
2852 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2854 if (!noconvert)
2855 arg = default_conversion (arg);
2857 else if (typecode == COMPLEX_TYPE)
2859 code = CONJ_EXPR;
2860 if (pedantic)
2861 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2862 if (!noconvert)
2863 arg = default_conversion (arg);
2865 else
2867 error ("wrong type argument to bit-complement");
2868 return error_mark_node;
2870 break;
2872 case ABS_EXPR:
2873 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2875 error ("wrong type argument to abs");
2876 return error_mark_node;
2878 else if (!noconvert)
2879 arg = default_conversion (arg);
2880 break;
2882 case CONJ_EXPR:
2883 /* Conjugating a real value is a no-op, but allow it anyway. */
2884 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2885 || typecode == COMPLEX_TYPE))
2887 error ("wrong type argument to conjugation");
2888 return error_mark_node;
2890 else if (!noconvert)
2891 arg = default_conversion (arg);
2892 break;
2894 case TRUTH_NOT_EXPR:
2895 if (typecode != INTEGER_TYPE
2896 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2897 && typecode != COMPLEX_TYPE)
2899 error ("wrong type argument to unary exclamation mark");
2900 return error_mark_node;
2902 arg = c_objc_common_truthvalue_conversion (arg);
2903 return invert_truthvalue (arg);
2905 case REALPART_EXPR:
2906 if (TREE_CODE (arg) == COMPLEX_CST)
2907 return TREE_REALPART (arg);
2908 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2909 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2910 else
2911 return arg;
2913 case IMAGPART_EXPR:
2914 if (TREE_CODE (arg) == COMPLEX_CST)
2915 return TREE_IMAGPART (arg);
2916 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2917 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2918 else
2919 return convert (TREE_TYPE (arg), integer_zero_node);
2921 case PREINCREMENT_EXPR:
2922 case POSTINCREMENT_EXPR:
2923 case PREDECREMENT_EXPR:
2924 case POSTDECREMENT_EXPR:
2926 /* Increment or decrement the real part of the value,
2927 and don't change the imaginary part. */
2928 if (typecode == COMPLEX_TYPE)
2930 tree real, imag;
2932 if (pedantic)
2933 pedwarn ("ISO C does not support %<++%> and %<--%>"
2934 " on complex types");
2936 arg = stabilize_reference (arg);
2937 real = build_unary_op (REALPART_EXPR, arg, 1);
2938 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2939 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2940 build_unary_op (code, real, 1), imag);
2943 /* Report invalid types. */
2945 if (typecode != POINTER_TYPE
2946 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2948 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2949 error ("wrong type argument to increment");
2950 else
2951 error ("wrong type argument to decrement");
2953 return error_mark_node;
2957 tree inc;
2958 tree result_type = TREE_TYPE (arg);
2960 arg = get_unwidened (arg, 0);
2961 argtype = TREE_TYPE (arg);
2963 /* Compute the increment. */
2965 if (typecode == POINTER_TYPE)
2967 /* If pointer target is an undefined struct,
2968 we just cannot know how to do the arithmetic. */
2969 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2971 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2972 error ("increment of pointer to unknown structure");
2973 else
2974 error ("decrement of pointer to unknown structure");
2976 else if ((pedantic || warn_pointer_arith)
2977 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2978 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2980 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2981 pedwarn ("wrong type argument to increment");
2982 else
2983 pedwarn ("wrong type argument to decrement");
2986 inc = c_size_in_bytes (TREE_TYPE (result_type));
2988 else
2989 inc = integer_one_node;
2991 inc = convert (argtype, inc);
2993 /* Complain about anything else that is not a true lvalue. */
2994 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2995 || code == POSTINCREMENT_EXPR)
2996 ? lv_increment
2997 : lv_decrement)))
2998 return error_mark_node;
3000 /* Report a read-only lvalue. */
3001 if (TREE_READONLY (arg))
3003 readonly_error (arg,
3004 ((code == PREINCREMENT_EXPR
3005 || code == POSTINCREMENT_EXPR)
3006 ? lv_increment : lv_decrement));
3007 return error_mark_node;
3010 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3011 val = boolean_increment (code, arg);
3012 else
3013 val = build2 (code, TREE_TYPE (arg), arg, inc);
3014 TREE_SIDE_EFFECTS (val) = 1;
3015 val = convert (result_type, val);
3016 if (TREE_CODE (val) != code)
3017 TREE_NO_WARNING (val) = 1;
3018 return val;
3021 case ADDR_EXPR:
3022 /* Note that this operation never does default_conversion. */
3024 /* Let &* cancel out to simplify resulting code. */
3025 if (TREE_CODE (arg) == INDIRECT_REF)
3027 /* Don't let this be an lvalue. */
3028 if (lvalue_p (TREE_OPERAND (arg, 0)))
3029 return non_lvalue (TREE_OPERAND (arg, 0));
3030 return TREE_OPERAND (arg, 0);
3033 /* For &x[y], return x+y */
3034 if (TREE_CODE (arg) == ARRAY_REF)
3036 tree op0 = TREE_OPERAND (arg, 0);
3037 if (!c_mark_addressable (op0))
3038 return error_mark_node;
3039 return build_binary_op (PLUS_EXPR,
3040 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3041 ? array_to_pointer_conversion (op0)
3042 : op0),
3043 TREE_OPERAND (arg, 1), 1);
3046 /* Anything not already handled and not a true memory reference
3047 or a non-lvalue array is an error. */
3048 else if (typecode != FUNCTION_TYPE && !flag
3049 && !lvalue_or_else (arg, lv_addressof))
3050 return error_mark_node;
3052 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3053 argtype = TREE_TYPE (arg);
3055 /* If the lvalue is const or volatile, merge that into the type
3056 to which the address will point. Note that you can't get a
3057 restricted pointer by taking the address of something, so we
3058 only have to deal with `const' and `volatile' here. */
3059 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3060 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3061 argtype = c_build_type_variant (argtype,
3062 TREE_READONLY (arg),
3063 TREE_THIS_VOLATILE (arg));
3065 if (!c_mark_addressable (arg))
3066 return error_mark_node;
3068 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3069 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3071 argtype = build_pointer_type (argtype);
3073 /* ??? Cope with user tricks that amount to offsetof. Delete this
3074 when we have proper support for integer constant expressions. */
3075 val = get_base_address (arg);
3076 if (val && TREE_CODE (val) == INDIRECT_REF
3077 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3079 tree op0 = fold_convert (argtype, fold_offsetof (arg, val)), op1;
3081 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3082 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3085 val = build1 (ADDR_EXPR, argtype, arg);
3087 return val;
3089 default:
3090 gcc_unreachable ();
3093 if (argtype == 0)
3094 argtype = TREE_TYPE (arg);
3095 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3096 : fold_build1 (code, argtype, arg);
3099 /* Return nonzero if REF is an lvalue valid for this language.
3100 Lvalues can be assigned, unless their type has TYPE_READONLY.
3101 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3103 static int
3104 lvalue_p (tree ref)
3106 enum tree_code code = TREE_CODE (ref);
3108 switch (code)
3110 case REALPART_EXPR:
3111 case IMAGPART_EXPR:
3112 case COMPONENT_REF:
3113 return lvalue_p (TREE_OPERAND (ref, 0));
3115 case COMPOUND_LITERAL_EXPR:
3116 case STRING_CST:
3117 return 1;
3119 case INDIRECT_REF:
3120 case ARRAY_REF:
3121 case VAR_DECL:
3122 case PARM_DECL:
3123 case RESULT_DECL:
3124 case ERROR_MARK:
3125 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3126 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3128 case BIND_EXPR:
3129 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3131 default:
3132 return 0;
3136 /* Give an error for storing in something that is 'const'. */
3138 static void
3139 readonly_error (tree arg, enum lvalue_use use)
3141 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3142 || use == lv_asm);
3143 /* Using this macro rather than (for example) arrays of messages
3144 ensures that all the format strings are checked at compile
3145 time. */
3146 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3147 : (use == lv_increment ? (I) \
3148 : (use == lv_decrement ? (D) : (AS))))
3149 if (TREE_CODE (arg) == COMPONENT_REF)
3151 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3152 readonly_error (TREE_OPERAND (arg, 0), use);
3153 else
3154 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3155 G_("increment of read-only member %qD"),
3156 G_("decrement of read-only member %qD"),
3157 G_("read-only member %qD used as %<asm%> output")),
3158 TREE_OPERAND (arg, 1));
3160 else if (TREE_CODE (arg) == VAR_DECL)
3161 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3162 G_("increment of read-only variable %qD"),
3163 G_("decrement of read-only variable %qD"),
3164 G_("read-only variable %qD used as %<asm%> output")),
3165 arg);
3166 else
3167 error (READONLY_MSG (G_("assignment of read-only location"),
3168 G_("increment of read-only location"),
3169 G_("decrement of read-only location"),
3170 G_("read-only location used as %<asm%> output")));
3174 /* Return nonzero if REF is an lvalue valid for this language;
3175 otherwise, print an error message and return zero. USE says
3176 how the lvalue is being used and so selects the error message. */
3178 static int
3179 lvalue_or_else (tree ref, enum lvalue_use use)
3181 int win = lvalue_p (ref);
3183 if (!win)
3184 lvalue_error (use);
3186 return win;
3189 /* Mark EXP saying that we need to be able to take the
3190 address of it; it should not be allocated in a register.
3191 Returns true if successful. */
3193 bool
3194 c_mark_addressable (tree exp)
3196 tree x = exp;
3198 while (1)
3199 switch (TREE_CODE (x))
3201 case COMPONENT_REF:
3202 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3204 error
3205 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3206 return false;
3209 /* ... fall through ... */
3211 case ADDR_EXPR:
3212 case ARRAY_REF:
3213 case REALPART_EXPR:
3214 case IMAGPART_EXPR:
3215 x = TREE_OPERAND (x, 0);
3216 break;
3218 case COMPOUND_LITERAL_EXPR:
3219 case CONSTRUCTOR:
3220 TREE_ADDRESSABLE (x) = 1;
3221 return true;
3223 case VAR_DECL:
3224 case CONST_DECL:
3225 case PARM_DECL:
3226 case RESULT_DECL:
3227 if (C_DECL_REGISTER (x)
3228 && DECL_NONLOCAL (x))
3230 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3232 error
3233 ("global register variable %qD used in nested function", x);
3234 return false;
3236 pedwarn ("register variable %qD used in nested function", x);
3238 else if (C_DECL_REGISTER (x))
3240 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3241 error ("address of global register variable %qD requested", x);
3242 else
3243 error ("address of register variable %qD requested", x);
3244 return false;
3247 /* drops in */
3248 case FUNCTION_DECL:
3249 TREE_ADDRESSABLE (x) = 1;
3250 /* drops out */
3251 default:
3252 return true;
3256 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3258 tree
3259 build_conditional_expr (tree ifexp, tree op1, tree op2)
3261 tree type1;
3262 tree type2;
3263 enum tree_code code1;
3264 enum tree_code code2;
3265 tree result_type = NULL;
3266 tree orig_op1 = op1, orig_op2 = op2;
3268 /* Promote both alternatives. */
3270 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3271 op1 = default_conversion (op1);
3272 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3273 op2 = default_conversion (op2);
3275 if (TREE_CODE (ifexp) == ERROR_MARK
3276 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3277 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3278 return error_mark_node;
3280 type1 = TREE_TYPE (op1);
3281 code1 = TREE_CODE (type1);
3282 type2 = TREE_TYPE (op2);
3283 code2 = TREE_CODE (type2);
3285 /* C90 does not permit non-lvalue arrays in conditional expressions.
3286 In C99 they will be pointers by now. */
3287 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3289 error ("non-lvalue array in conditional expression");
3290 return error_mark_node;
3293 /* Quickly detect the usual case where op1 and op2 have the same type
3294 after promotion. */
3295 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3297 if (type1 == type2)
3298 result_type = type1;
3299 else
3300 result_type = TYPE_MAIN_VARIANT (type1);
3302 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3303 || code1 == COMPLEX_TYPE)
3304 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3305 || code2 == COMPLEX_TYPE))
3307 result_type = c_common_type (type1, type2);
3309 /* If -Wsign-compare, warn here if type1 and type2 have
3310 different signedness. We'll promote the signed to unsigned
3311 and later code won't know it used to be different.
3312 Do this check on the original types, so that explicit casts
3313 will be considered, but default promotions won't. */
3314 if (warn_sign_compare && !skip_evaluation)
3316 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3317 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3319 if (unsigned_op1 ^ unsigned_op2)
3321 bool ovf;
3323 /* Do not warn if the result type is signed, since the
3324 signed type will only be chosen if it can represent
3325 all the values of the unsigned type. */
3326 if (!TYPE_UNSIGNED (result_type))
3327 /* OK */;
3328 /* Do not warn if the signed quantity is an unsuffixed
3329 integer literal (or some static constant expression
3330 involving such literals) and it is non-negative. */
3331 else if ((unsigned_op2
3332 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3333 || (unsigned_op1
3334 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3335 /* OK */;
3336 else
3337 warning (0, "signed and unsigned type in conditional expression");
3341 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3343 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3344 pedwarn ("ISO C forbids conditional expr with only one void side");
3345 result_type = void_type_node;
3347 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3349 if (comp_target_types (type1, type2))
3350 result_type = common_pointer_type (type1, type2);
3351 else if (null_pointer_constant_p (orig_op1))
3352 result_type = qualify_type (type2, type1);
3353 else if (null_pointer_constant_p (orig_op2))
3354 result_type = qualify_type (type1, type2);
3355 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3357 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3358 pedwarn ("ISO C forbids conditional expr between "
3359 "%<void *%> and function pointer");
3360 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3361 TREE_TYPE (type2)));
3363 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3365 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3366 pedwarn ("ISO C forbids conditional expr between "
3367 "%<void *%> and function pointer");
3368 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3369 TREE_TYPE (type1)));
3371 else
3373 pedwarn ("pointer type mismatch in conditional expression");
3374 result_type = build_pointer_type (void_type_node);
3377 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3379 if (!null_pointer_constant_p (orig_op2))
3380 pedwarn ("pointer/integer type mismatch in conditional expression");
3381 else
3383 op2 = null_pointer_node;
3385 result_type = type1;
3387 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3389 if (!null_pointer_constant_p (orig_op1))
3390 pedwarn ("pointer/integer type mismatch in conditional expression");
3391 else
3393 op1 = null_pointer_node;
3395 result_type = type2;
3398 if (!result_type)
3400 if (flag_cond_mismatch)
3401 result_type = void_type_node;
3402 else
3404 error ("type mismatch in conditional expression");
3405 return error_mark_node;
3409 /* Merge const and volatile flags of the incoming types. */
3410 result_type
3411 = build_type_variant (result_type,
3412 TREE_READONLY (op1) || TREE_READONLY (op2),
3413 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3415 if (result_type != TREE_TYPE (op1))
3416 op1 = convert_and_check (result_type, op1);
3417 if (result_type != TREE_TYPE (op2))
3418 op2 = convert_and_check (result_type, op2);
3420 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3423 /* Return a compound expression that performs two expressions and
3424 returns the value of the second of them. */
3426 tree
3427 build_compound_expr (tree expr1, tree expr2)
3429 if (!TREE_SIDE_EFFECTS (expr1))
3431 /* The left-hand operand of a comma expression is like an expression
3432 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3433 any side-effects, unless it was explicitly cast to (void). */
3434 if (warn_unused_value)
3436 if (VOID_TYPE_P (TREE_TYPE (expr1))
3437 && (TREE_CODE (expr1) == NOP_EXPR
3438 || TREE_CODE (expr1) == CONVERT_EXPR))
3439 ; /* (void) a, b */
3440 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3441 && TREE_CODE (expr1) == COMPOUND_EXPR
3442 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3443 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3444 ; /* (void) a, (void) b, c */
3445 else
3446 warning (0, "left-hand operand of comma expression has no effect");
3450 /* With -Wunused, we should also warn if the left-hand operand does have
3451 side-effects, but computes a value which is not used. For example, in
3452 `foo() + bar(), baz()' the result of the `+' operator is not used,
3453 so we should issue a warning. */
3454 else if (warn_unused_value)
3455 warn_if_unused_value (expr1, input_location);
3457 if (expr2 == error_mark_node)
3458 return error_mark_node;
3460 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3463 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3465 tree
3466 build_c_cast (tree type, tree expr)
3468 tree value = expr;
3470 if (type == error_mark_node || expr == error_mark_node)
3471 return error_mark_node;
3473 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3474 only in <protocol> qualifications. But when constructing cast expressions,
3475 the protocols do matter and must be kept around. */
3476 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3477 return build1 (NOP_EXPR, type, expr);
3479 type = TYPE_MAIN_VARIANT (type);
3481 if (TREE_CODE (type) == ARRAY_TYPE)
3483 error ("cast specifies array type");
3484 return error_mark_node;
3487 if (TREE_CODE (type) == FUNCTION_TYPE)
3489 error ("cast specifies function type");
3490 return error_mark_node;
3493 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3495 if (pedantic)
3497 if (TREE_CODE (type) == RECORD_TYPE
3498 || TREE_CODE (type) == UNION_TYPE)
3499 pedwarn ("ISO C forbids casting nonscalar to the same type");
3502 else if (TREE_CODE (type) == UNION_TYPE)
3504 tree field;
3506 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3507 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3508 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3509 break;
3511 if (field)
3513 tree t;
3515 if (pedantic)
3516 pedwarn ("ISO C forbids casts to union type");
3517 t = digest_init (type,
3518 build_constructor_single (type, field, value),
3519 true, 0);
3520 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3521 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3522 return t;
3524 error ("cast to union type from type not present in union");
3525 return error_mark_node;
3527 else
3529 tree otype, ovalue;
3531 if (type == void_type_node)
3532 return build1 (CONVERT_EXPR, type, value);
3534 otype = TREE_TYPE (value);
3536 /* Optionally warn about potentially worrisome casts. */
3538 if (warn_cast_qual
3539 && TREE_CODE (type) == POINTER_TYPE
3540 && TREE_CODE (otype) == POINTER_TYPE)
3542 tree in_type = type;
3543 tree in_otype = otype;
3544 int added = 0;
3545 int discarded = 0;
3547 /* Check that the qualifiers on IN_TYPE are a superset of
3548 the qualifiers of IN_OTYPE. The outermost level of
3549 POINTER_TYPE nodes is uninteresting and we stop as soon
3550 as we hit a non-POINTER_TYPE node on either type. */
3553 in_otype = TREE_TYPE (in_otype);
3554 in_type = TREE_TYPE (in_type);
3556 /* GNU C allows cv-qualified function types. 'const'
3557 means the function is very pure, 'volatile' means it
3558 can't return. We need to warn when such qualifiers
3559 are added, not when they're taken away. */
3560 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3561 && TREE_CODE (in_type) == FUNCTION_TYPE)
3562 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3563 else
3564 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3566 while (TREE_CODE (in_type) == POINTER_TYPE
3567 && TREE_CODE (in_otype) == POINTER_TYPE);
3569 if (added)
3570 warning (0, "cast adds new qualifiers to function type");
3572 if (discarded)
3573 /* There are qualifiers present in IN_OTYPE that are not
3574 present in IN_TYPE. */
3575 warning (0, "cast discards qualifiers from pointer target type");
3578 /* Warn about possible alignment problems. */
3579 if (STRICT_ALIGNMENT
3580 && TREE_CODE (type) == POINTER_TYPE
3581 && TREE_CODE (otype) == POINTER_TYPE
3582 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3583 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3584 /* Don't warn about opaque types, where the actual alignment
3585 restriction is unknown. */
3586 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3587 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3588 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3589 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3590 warning (OPT_Wcast_align,
3591 "cast increases required alignment of target type");
3593 if (TREE_CODE (type) == INTEGER_TYPE
3594 && TREE_CODE (otype) == POINTER_TYPE
3595 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3596 /* Unlike conversion of integers to pointers, where the
3597 warning is disabled for converting constants because
3598 of cases such as SIG_*, warn about converting constant
3599 pointers to integers. In some cases it may cause unwanted
3600 sign extension, and a warning is appropriate. */
3601 warning (OPT_Wpointer_to_int_cast,
3602 "cast from pointer to integer of different size");
3604 if (TREE_CODE (value) == CALL_EXPR
3605 && TREE_CODE (type) != TREE_CODE (otype))
3606 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3607 "to non-matching type %qT", otype, type);
3609 if (TREE_CODE (type) == POINTER_TYPE
3610 && TREE_CODE (otype) == INTEGER_TYPE
3611 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3612 /* Don't warn about converting any constant. */
3613 && !TREE_CONSTANT (value))
3614 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3615 "of different size");
3617 strict_aliasing_warning (otype, type, expr);
3619 /* If pedantic, warn for conversions between function and object
3620 pointer types, except for converting a null pointer constant
3621 to function pointer type. */
3622 if (pedantic
3623 && TREE_CODE (type) == POINTER_TYPE
3624 && TREE_CODE (otype) == POINTER_TYPE
3625 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3626 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3627 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3629 if (pedantic
3630 && TREE_CODE (type) == POINTER_TYPE
3631 && TREE_CODE (otype) == POINTER_TYPE
3632 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3633 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3634 && !null_pointer_constant_p (value))
3635 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3637 ovalue = value;
3638 value = convert (type, value);
3640 /* Ignore any integer overflow caused by the cast. */
3641 if (TREE_CODE (value) == INTEGER_CST)
3643 if (CONSTANT_CLASS_P (ovalue)
3644 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3646 /* Avoid clobbering a shared constant. */
3647 value = copy_node (value);
3648 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3649 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3651 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3652 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3653 value = build_int_cst_wide (TREE_TYPE (value),
3654 TREE_INT_CST_LOW (value),
3655 TREE_INT_CST_HIGH (value));
3659 /* Don't let a cast be an lvalue. */
3660 if (value == expr)
3661 value = non_lvalue (value);
3663 return value;
3666 /* Interpret a cast of expression EXPR to type TYPE. */
3667 tree
3668 c_cast_expr (struct c_type_name *type_name, tree expr)
3670 tree type;
3671 int saved_wsp = warn_strict_prototypes;
3673 /* This avoids warnings about unprototyped casts on
3674 integers. E.g. "#define SIG_DFL (void(*)())0". */
3675 if (TREE_CODE (expr) == INTEGER_CST)
3676 warn_strict_prototypes = 0;
3677 type = groktypename (type_name);
3678 warn_strict_prototypes = saved_wsp;
3680 return build_c_cast (type, expr);
3683 /* Build an assignment expression of lvalue LHS from value RHS.
3684 MODIFYCODE is the code for a binary operator that we use
3685 to combine the old value of LHS with RHS to get the new value.
3686 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3688 tree
3689 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3691 tree result;
3692 tree newrhs;
3693 tree lhstype = TREE_TYPE (lhs);
3694 tree olhstype = lhstype;
3696 /* Types that aren't fully specified cannot be used in assignments. */
3697 lhs = require_complete_type (lhs);
3699 /* Avoid duplicate error messages from operands that had errors. */
3700 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3701 return error_mark_node;
3703 if (!lvalue_or_else (lhs, lv_assign))
3704 return error_mark_node;
3706 STRIP_TYPE_NOPS (rhs);
3708 newrhs = rhs;
3710 /* If a binary op has been requested, combine the old LHS value with the RHS
3711 producing the value we should actually store into the LHS. */
3713 if (modifycode != NOP_EXPR)
3715 lhs = stabilize_reference (lhs);
3716 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3719 /* Give an error for storing in something that is 'const'. */
3721 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3722 || ((TREE_CODE (lhstype) == RECORD_TYPE
3723 || TREE_CODE (lhstype) == UNION_TYPE)
3724 && C_TYPE_FIELDS_READONLY (lhstype)))
3726 readonly_error (lhs, lv_assign);
3727 return error_mark_node;
3730 /* If storing into a structure or union member,
3731 it has probably been given type `int'.
3732 Compute the type that would go with
3733 the actual amount of storage the member occupies. */
3735 if (TREE_CODE (lhs) == COMPONENT_REF
3736 && (TREE_CODE (lhstype) == INTEGER_TYPE
3737 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3738 || TREE_CODE (lhstype) == REAL_TYPE
3739 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3740 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3742 /* If storing in a field that is in actuality a short or narrower than one,
3743 we must store in the field in its actual type. */
3745 if (lhstype != TREE_TYPE (lhs))
3747 lhs = copy_node (lhs);
3748 TREE_TYPE (lhs) = lhstype;
3751 /* Convert new value to destination type. */
3753 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3754 NULL_TREE, NULL_TREE, 0);
3755 if (TREE_CODE (newrhs) == ERROR_MARK)
3756 return error_mark_node;
3758 /* Emit ObjC write barrier, if necessary. */
3759 if (c_dialect_objc () && flag_objc_gc)
3761 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3762 if (result)
3763 return result;
3766 /* Scan operands. */
3768 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3769 TREE_SIDE_EFFECTS (result) = 1;
3771 /* If we got the LHS in a different type for storing in,
3772 convert the result back to the nominal type of LHS
3773 so that the value we return always has the same type
3774 as the LHS argument. */
3776 if (olhstype == TREE_TYPE (result))
3777 return result;
3778 return convert_for_assignment (olhstype, result, ic_assign,
3779 NULL_TREE, NULL_TREE, 0);
3782 /* Convert value RHS to type TYPE as preparation for an assignment
3783 to an lvalue of type TYPE.
3784 The real work of conversion is done by `convert'.
3785 The purpose of this function is to generate error messages
3786 for assignments that are not allowed in C.
3787 ERRTYPE says whether it is argument passing, assignment,
3788 initialization or return.
3790 FUNCTION is a tree for the function being called.
3791 PARMNUM is the number of the argument, for printing in error messages. */
3793 static tree
3794 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3795 tree fundecl, tree function, int parmnum)
3797 enum tree_code codel = TREE_CODE (type);
3798 tree rhstype;
3799 enum tree_code coder;
3800 tree rname = NULL_TREE;
3801 bool objc_ok = false;
3803 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3805 tree selector;
3806 /* Change pointer to function to the function itself for
3807 diagnostics. */
3808 if (TREE_CODE (function) == ADDR_EXPR
3809 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3810 function = TREE_OPERAND (function, 0);
3812 /* Handle an ObjC selector specially for diagnostics. */
3813 selector = objc_message_selector ();
3814 rname = function;
3815 if (selector && parmnum > 2)
3817 rname = selector;
3818 parmnum -= 2;
3822 /* This macro is used to emit diagnostics to ensure that all format
3823 strings are complete sentences, visible to gettext and checked at
3824 compile time. */
3825 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3826 do { \
3827 switch (errtype) \
3829 case ic_argpass: \
3830 pedwarn (AR, parmnum, rname); \
3831 break; \
3832 case ic_argpass_nonproto: \
3833 warning (0, AR, parmnum, rname); \
3834 break; \
3835 case ic_assign: \
3836 pedwarn (AS); \
3837 break; \
3838 case ic_init: \
3839 pedwarn (IN); \
3840 break; \
3841 case ic_return: \
3842 pedwarn (RE); \
3843 break; \
3844 default: \
3845 gcc_unreachable (); \
3847 } while (0)
3849 STRIP_TYPE_NOPS (rhs);
3851 if (optimize && TREE_CODE (rhs) == VAR_DECL
3852 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3853 rhs = decl_constant_value_for_broken_optimization (rhs);
3855 rhstype = TREE_TYPE (rhs);
3856 coder = TREE_CODE (rhstype);
3858 if (coder == ERROR_MARK)
3859 return error_mark_node;
3861 if (c_dialect_objc ())
3863 int parmno;
3865 switch (errtype)
3867 case ic_return:
3868 parmno = 0;
3869 break;
3871 case ic_assign:
3872 parmno = -1;
3873 break;
3875 case ic_init:
3876 parmno = -2;
3877 break;
3879 default:
3880 parmno = parmnum;
3881 break;
3884 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3887 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3889 overflow_warning (rhs);
3890 return rhs;
3893 if (coder == VOID_TYPE)
3895 /* Except for passing an argument to an unprototyped function,
3896 this is a constraint violation. When passing an argument to
3897 an unprototyped function, it is compile-time undefined;
3898 making it a constraint in that case was rejected in
3899 DR#252. */
3900 error ("void value not ignored as it ought to be");
3901 return error_mark_node;
3903 /* A type converts to a reference to it.
3904 This code doesn't fully support references, it's just for the
3905 special case of va_start and va_copy. */
3906 if (codel == REFERENCE_TYPE
3907 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3909 if (!lvalue_p (rhs))
3911 error ("cannot pass rvalue to reference parameter");
3912 return error_mark_node;
3914 if (!c_mark_addressable (rhs))
3915 return error_mark_node;
3916 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3918 /* We already know that these two types are compatible, but they
3919 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3920 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3921 likely to be va_list, a typedef to __builtin_va_list, which
3922 is different enough that it will cause problems later. */
3923 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3924 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3926 rhs = build1 (NOP_EXPR, type, rhs);
3927 return rhs;
3929 /* Some types can interconvert without explicit casts. */
3930 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3931 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3932 return convert (type, rhs);
3933 /* Arithmetic types all interconvert, and enum is treated like int. */
3934 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3935 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3936 || codel == BOOLEAN_TYPE)
3937 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3938 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3939 || coder == BOOLEAN_TYPE))
3940 return convert_and_check (type, rhs);
3942 /* Aggregates in different TUs might need conversion. */
3943 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
3944 && codel == coder
3945 && comptypes (type, rhstype))
3946 return convert_and_check (type, rhs);
3948 /* Conversion to a transparent union from its member types.
3949 This applies only to function arguments. */
3950 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3951 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3953 tree memb, marginal_memb = NULL_TREE;
3955 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3957 tree memb_type = TREE_TYPE (memb);
3959 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3960 TYPE_MAIN_VARIANT (rhstype)))
3961 break;
3963 if (TREE_CODE (memb_type) != POINTER_TYPE)
3964 continue;
3966 if (coder == POINTER_TYPE)
3968 tree ttl = TREE_TYPE (memb_type);
3969 tree ttr = TREE_TYPE (rhstype);
3971 /* Any non-function converts to a [const][volatile] void *
3972 and vice versa; otherwise, targets must be the same.
3973 Meanwhile, the lhs target must have all the qualifiers of
3974 the rhs. */
3975 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3976 || comp_target_types (memb_type, rhstype))
3978 /* If this type won't generate any warnings, use it. */
3979 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3980 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3981 && TREE_CODE (ttl) == FUNCTION_TYPE)
3982 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3983 == TYPE_QUALS (ttr))
3984 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3985 == TYPE_QUALS (ttl))))
3986 break;
3988 /* Keep looking for a better type, but remember this one. */
3989 if (!marginal_memb)
3990 marginal_memb = memb;
3994 /* Can convert integer zero to any pointer type. */
3995 if (null_pointer_constant_p (rhs))
3997 rhs = null_pointer_node;
3998 break;
4002 if (memb || marginal_memb)
4004 if (!memb)
4006 /* We have only a marginally acceptable member type;
4007 it needs a warning. */
4008 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4009 tree ttr = TREE_TYPE (rhstype);
4011 /* Const and volatile mean something different for function
4012 types, so the usual warnings are not appropriate. */
4013 if (TREE_CODE (ttr) == FUNCTION_TYPE
4014 && TREE_CODE (ttl) == FUNCTION_TYPE)
4016 /* Because const and volatile on functions are
4017 restrictions that say the function will not do
4018 certain things, it is okay to use a const or volatile
4019 function where an ordinary one is wanted, but not
4020 vice-versa. */
4021 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4022 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
4023 "makes qualified function "
4024 "pointer from unqualified"),
4025 G_("assignment makes qualified "
4026 "function pointer from "
4027 "unqualified"),
4028 G_("initialization makes qualified "
4029 "function pointer from "
4030 "unqualified"),
4031 G_("return makes qualified function "
4032 "pointer from unqualified"));
4034 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4035 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4036 "qualifiers from pointer target type"),
4037 G_("assignment discards qualifiers "
4038 "from pointer target type"),
4039 G_("initialization discards qualifiers "
4040 "from pointer target type"),
4041 G_("return discards qualifiers from "
4042 "pointer target type"));
4044 memb = marginal_memb;
4047 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4048 pedwarn ("ISO C prohibits argument conversion to union type");
4050 return build_constructor_single (type, memb, rhs);
4054 /* Conversions among pointers */
4055 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4056 && (coder == codel))
4058 tree ttl = TREE_TYPE (type);
4059 tree ttr = TREE_TYPE (rhstype);
4060 tree mvl = ttl;
4061 tree mvr = ttr;
4062 bool is_opaque_pointer;
4063 int target_cmp = 0; /* Cache comp_target_types () result. */
4065 if (TREE_CODE (mvl) != ARRAY_TYPE)
4066 mvl = TYPE_MAIN_VARIANT (mvl);
4067 if (TREE_CODE (mvr) != ARRAY_TYPE)
4068 mvr = TYPE_MAIN_VARIANT (mvr);
4069 /* Opaque pointers are treated like void pointers. */
4070 is_opaque_pointer = (targetm.vector_opaque_p (type)
4071 || targetm.vector_opaque_p (rhstype))
4072 && TREE_CODE (ttl) == VECTOR_TYPE
4073 && TREE_CODE (ttr) == VECTOR_TYPE;
4075 /* C++ does not allow the implicit conversion void* -> T*. However,
4076 for the purpose of reducing the number of false positives, we
4077 tolerate the special case of
4079 int *p = NULL;
4081 where NULL is typically defined in C to be '(void *) 0'. */
4082 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4083 warning (OPT_Wc___compat, "request for implicit conversion from "
4084 "%qT to %qT not permitted in C++", rhstype, type);
4086 /* Check if the right-hand side has a format attribute but the
4087 left-hand side doesn't. */
4088 if (warn_missing_format_attribute
4089 && check_missing_format_attribute (type, rhstype))
4091 switch (errtype)
4093 case ic_argpass:
4094 case ic_argpass_nonproto:
4095 warning (OPT_Wmissing_format_attribute,
4096 "argument %d of %qE might be "
4097 "a candidate for a format attribute",
4098 parmnum, rname);
4099 break;
4100 case ic_assign:
4101 warning (OPT_Wmissing_format_attribute,
4102 "assignment left-hand side might be "
4103 "a candidate for a format attribute");
4104 break;
4105 case ic_init:
4106 warning (OPT_Wmissing_format_attribute,
4107 "initialization left-hand side might be "
4108 "a candidate for a format attribute");
4109 break;
4110 case ic_return:
4111 warning (OPT_Wmissing_format_attribute,
4112 "return type might be "
4113 "a candidate for a format attribute");
4114 break;
4115 default:
4116 gcc_unreachable ();
4120 /* Any non-function converts to a [const][volatile] void *
4121 and vice versa; otherwise, targets must be the same.
4122 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4123 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4124 || (target_cmp = comp_target_types (type, rhstype))
4125 || is_opaque_pointer
4126 || (c_common_unsigned_type (mvl)
4127 == c_common_unsigned_type (mvr)))
4129 if (pedantic
4130 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4132 (VOID_TYPE_P (ttr)
4133 && !null_pointer_constant_p (rhs)
4134 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4135 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4136 "%qE between function pointer "
4137 "and %<void *%>"),
4138 G_("ISO C forbids assignment between "
4139 "function pointer and %<void *%>"),
4140 G_("ISO C forbids initialization between "
4141 "function pointer and %<void *%>"),
4142 G_("ISO C forbids return between function "
4143 "pointer and %<void *%>"));
4144 /* Const and volatile mean something different for function types,
4145 so the usual warnings are not appropriate. */
4146 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4147 && TREE_CODE (ttl) != FUNCTION_TYPE)
4149 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4151 /* Types differing only by the presence of the 'volatile'
4152 qualifier are acceptable if the 'volatile' has been added
4153 in by the Objective-C EH machinery. */
4154 if (!objc_type_quals_match (ttl, ttr))
4155 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4156 "qualifiers from pointer target type"),
4157 G_("assignment discards qualifiers "
4158 "from pointer target type"),
4159 G_("initialization discards qualifiers "
4160 "from pointer target type"),
4161 G_("return discards qualifiers from "
4162 "pointer target type"));
4164 /* If this is not a case of ignoring a mismatch in signedness,
4165 no warning. */
4166 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4167 || target_cmp)
4169 /* If there is a mismatch, do warn. */
4170 else if (warn_pointer_sign)
4171 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4172 "%d of %qE differ in signedness"),
4173 G_("pointer targets in assignment "
4174 "differ in signedness"),
4175 G_("pointer targets in initialization "
4176 "differ in signedness"),
4177 G_("pointer targets in return differ "
4178 "in signedness"));
4180 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4181 && TREE_CODE (ttr) == FUNCTION_TYPE)
4183 /* Because const and volatile on functions are restrictions
4184 that say the function will not do certain things,
4185 it is okay to use a const or volatile function
4186 where an ordinary one is wanted, but not vice-versa. */
4187 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4188 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4189 "qualified function pointer "
4190 "from unqualified"),
4191 G_("assignment makes qualified function "
4192 "pointer from unqualified"),
4193 G_("initialization makes qualified "
4194 "function pointer from unqualified"),
4195 G_("return makes qualified function "
4196 "pointer from unqualified"));
4199 else
4200 /* Avoid warning about the volatile ObjC EH puts on decls. */
4201 if (!objc_ok)
4202 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4203 "incompatible pointer type"),
4204 G_("assignment from incompatible pointer type"),
4205 G_("initialization from incompatible "
4206 "pointer type"),
4207 G_("return from incompatible pointer type"));
4209 return convert (type, rhs);
4211 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4213 /* ??? This should not be an error when inlining calls to
4214 unprototyped functions. */
4215 error ("invalid use of non-lvalue array");
4216 return error_mark_node;
4218 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4220 /* An explicit constant 0 can convert to a pointer,
4221 or one that results from arithmetic, even including
4222 a cast to integer type. */
4223 if (!null_pointer_constant_p (rhs))
4224 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4225 "pointer from integer without a cast"),
4226 G_("assignment makes pointer from integer "
4227 "without a cast"),
4228 G_("initialization makes pointer from "
4229 "integer without a cast"),
4230 G_("return makes pointer from integer "
4231 "without a cast"));
4233 return convert (type, rhs);
4235 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4237 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4238 "from pointer without a cast"),
4239 G_("assignment makes integer from pointer "
4240 "without a cast"),
4241 G_("initialization makes integer from pointer "
4242 "without a cast"),
4243 G_("return makes integer from pointer "
4244 "without a cast"));
4245 return convert (type, rhs);
4247 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4248 return convert (type, rhs);
4250 switch (errtype)
4252 case ic_argpass:
4253 case ic_argpass_nonproto:
4254 /* ??? This should not be an error when inlining calls to
4255 unprototyped functions. */
4256 error ("incompatible type for argument %d of %qE", parmnum, rname);
4257 break;
4258 case ic_assign:
4259 error ("incompatible types in assignment");
4260 break;
4261 case ic_init:
4262 error ("incompatible types in initialization");
4263 break;
4264 case ic_return:
4265 error ("incompatible types in return");
4266 break;
4267 default:
4268 gcc_unreachable ();
4271 return error_mark_node;
4274 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4275 is used for error and warning reporting and indicates which argument
4276 is being processed. */
4278 tree
4279 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4281 tree ret, type;
4283 /* If FN was prototyped at the call site, the value has been converted
4284 already in convert_arguments.
4285 However, we might see a prototype now that was not in place when
4286 the function call was seen, so check that the VALUE actually matches
4287 PARM before taking an early exit. */
4288 if (!value
4289 || (TYPE_ARG_TYPES (TREE_TYPE (fn))
4290 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
4291 == TYPE_MAIN_VARIANT (TREE_TYPE (value)))))
4292 return value;
4294 type = TREE_TYPE (parm);
4295 ret = convert_for_assignment (type, value,
4296 ic_argpass_nonproto, fn,
4297 fn, argnum);
4298 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4299 && INTEGRAL_TYPE_P (type)
4300 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4301 ret = default_conversion (ret);
4302 return ret;
4305 /* If VALUE is a compound expr all of whose expressions are constant, then
4306 return its value. Otherwise, return error_mark_node.
4308 This is for handling COMPOUND_EXPRs as initializer elements
4309 which is allowed with a warning when -pedantic is specified. */
4311 static tree
4312 valid_compound_expr_initializer (tree value, tree endtype)
4314 if (TREE_CODE (value) == COMPOUND_EXPR)
4316 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4317 == error_mark_node)
4318 return error_mark_node;
4319 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4320 endtype);
4322 else if (!initializer_constant_valid_p (value, endtype))
4323 return error_mark_node;
4324 else
4325 return value;
4328 /* Perform appropriate conversions on the initial value of a variable,
4329 store it in the declaration DECL,
4330 and print any error messages that are appropriate.
4331 If the init is invalid, store an ERROR_MARK. */
4333 void
4334 store_init_value (tree decl, tree init)
4336 tree value, type;
4338 /* If variable's type was invalidly declared, just ignore it. */
4340 type = TREE_TYPE (decl);
4341 if (TREE_CODE (type) == ERROR_MARK)
4342 return;
4344 /* Digest the specified initializer into an expression. */
4346 value = digest_init (type, init, true, TREE_STATIC (decl));
4348 /* Store the expression if valid; else report error. */
4350 if (!in_system_header
4351 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4352 warning (OPT_Wtraditional, "traditional C rejects automatic "
4353 "aggregate initialization");
4355 DECL_INITIAL (decl) = value;
4357 /* ANSI wants warnings about out-of-range constant initializers. */
4358 STRIP_TYPE_NOPS (value);
4359 constant_expression_warning (value);
4361 /* Check if we need to set array size from compound literal size. */
4362 if (TREE_CODE (type) == ARRAY_TYPE
4363 && TYPE_DOMAIN (type) == 0
4364 && value != error_mark_node)
4366 tree inside_init = init;
4368 STRIP_TYPE_NOPS (inside_init);
4369 inside_init = fold (inside_init);
4371 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4373 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4375 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4377 /* For int foo[] = (int [3]){1}; we need to set array size
4378 now since later on array initializer will be just the
4379 brace enclosed list of the compound literal. */
4380 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4381 TREE_TYPE (decl) = type;
4382 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4383 layout_type (type);
4384 layout_decl (cldecl, 0);
4390 /* Methods for storing and printing names for error messages. */
4392 /* Implement a spelling stack that allows components of a name to be pushed
4393 and popped. Each element on the stack is this structure. */
4395 struct spelling
4397 int kind;
4398 union
4400 unsigned HOST_WIDE_INT i;
4401 const char *s;
4402 } u;
4405 #define SPELLING_STRING 1
4406 #define SPELLING_MEMBER 2
4407 #define SPELLING_BOUNDS 3
4409 static struct spelling *spelling; /* Next stack element (unused). */
4410 static struct spelling *spelling_base; /* Spelling stack base. */
4411 static int spelling_size; /* Size of the spelling stack. */
4413 /* Macros to save and restore the spelling stack around push_... functions.
4414 Alternative to SAVE_SPELLING_STACK. */
4416 #define SPELLING_DEPTH() (spelling - spelling_base)
4417 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4419 /* Push an element on the spelling stack with type KIND and assign VALUE
4420 to MEMBER. */
4422 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4424 int depth = SPELLING_DEPTH (); \
4426 if (depth >= spelling_size) \
4428 spelling_size += 10; \
4429 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4430 spelling_size); \
4431 RESTORE_SPELLING_DEPTH (depth); \
4434 spelling->kind = (KIND); \
4435 spelling->MEMBER = (VALUE); \
4436 spelling++; \
4439 /* Push STRING on the stack. Printed literally. */
4441 static void
4442 push_string (const char *string)
4444 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4447 /* Push a member name on the stack. Printed as '.' STRING. */
4449 static void
4450 push_member_name (tree decl)
4452 const char *const string
4453 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4454 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4457 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4459 static void
4460 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4462 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4465 /* Compute the maximum size in bytes of the printed spelling. */
4467 static int
4468 spelling_length (void)
4470 int size = 0;
4471 struct spelling *p;
4473 for (p = spelling_base; p < spelling; p++)
4475 if (p->kind == SPELLING_BOUNDS)
4476 size += 25;
4477 else
4478 size += strlen (p->u.s) + 1;
4481 return size;
4484 /* Print the spelling to BUFFER and return it. */
4486 static char *
4487 print_spelling (char *buffer)
4489 char *d = buffer;
4490 struct spelling *p;
4492 for (p = spelling_base; p < spelling; p++)
4493 if (p->kind == SPELLING_BOUNDS)
4495 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4496 d += strlen (d);
4498 else
4500 const char *s;
4501 if (p->kind == SPELLING_MEMBER)
4502 *d++ = '.';
4503 for (s = p->u.s; (*d = *s++); d++)
4506 *d++ = '\0';
4507 return buffer;
4510 /* Issue an error message for a bad initializer component.
4511 MSGID identifies the message.
4512 The component name is taken from the spelling stack. */
4514 void
4515 error_init (const char *msgid)
4517 char *ofwhat;
4519 error ("%s", _(msgid));
4520 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4521 if (*ofwhat)
4522 error ("(near initialization for %qs)", ofwhat);
4525 /* Issue a pedantic warning for a bad initializer component.
4526 MSGID identifies the message.
4527 The component name is taken from the spelling stack. */
4529 void
4530 pedwarn_init (const char *msgid)
4532 char *ofwhat;
4534 pedwarn ("%s", _(msgid));
4535 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4536 if (*ofwhat)
4537 pedwarn ("(near initialization for %qs)", ofwhat);
4540 /* Issue a warning for a bad initializer component.
4541 MSGID identifies the message.
4542 The component name is taken from the spelling stack. */
4544 static void
4545 warning_init (const char *msgid)
4547 char *ofwhat;
4549 warning (0, "%s", _(msgid));
4550 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4551 if (*ofwhat)
4552 warning (0, "(near initialization for %qs)", ofwhat);
4555 /* If TYPE is an array type and EXPR is a parenthesized string
4556 constant, warn if pedantic that EXPR is being used to initialize an
4557 object of type TYPE. */
4559 void
4560 maybe_warn_string_init (tree type, struct c_expr expr)
4562 if (pedantic
4563 && TREE_CODE (type) == ARRAY_TYPE
4564 && TREE_CODE (expr.value) == STRING_CST
4565 && expr.original_code != STRING_CST)
4566 pedwarn_init ("array initialized from parenthesized string constant");
4569 /* Digest the parser output INIT as an initializer for type TYPE.
4570 Return a C expression of type TYPE to represent the initial value.
4572 If INIT is a string constant, STRICT_STRING is true if it is
4573 unparenthesized or we should not warn here for it being parenthesized.
4574 For other types of INIT, STRICT_STRING is not used.
4576 REQUIRE_CONSTANT requests an error if non-constant initializers or
4577 elements are seen. */
4579 static tree
4580 digest_init (tree type, tree init, bool strict_string, int require_constant)
4582 enum tree_code code = TREE_CODE (type);
4583 tree inside_init = init;
4585 if (type == error_mark_node
4586 || !init
4587 || init == error_mark_node
4588 || TREE_TYPE (init) == error_mark_node)
4589 return error_mark_node;
4591 STRIP_TYPE_NOPS (inside_init);
4593 inside_init = fold (inside_init);
4595 /* Initialization of an array of chars from a string constant
4596 optionally enclosed in braces. */
4598 if (code == ARRAY_TYPE && inside_init
4599 && TREE_CODE (inside_init) == STRING_CST)
4601 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4602 /* Note that an array could be both an array of character type
4603 and an array of wchar_t if wchar_t is signed char or unsigned
4604 char. */
4605 bool char_array = (typ1 == char_type_node
4606 || typ1 == signed_char_type_node
4607 || typ1 == unsigned_char_type_node);
4608 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4609 if (char_array || wchar_array)
4611 struct c_expr expr;
4612 bool char_string;
4613 expr.value = inside_init;
4614 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4615 maybe_warn_string_init (type, expr);
4617 char_string
4618 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4619 == char_type_node);
4621 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4622 TYPE_MAIN_VARIANT (type)))
4623 return inside_init;
4625 if (!wchar_array && !char_string)
4627 error_init ("char-array initialized from wide string");
4628 return error_mark_node;
4630 if (char_string && !char_array)
4632 error_init ("wchar_t-array initialized from non-wide string");
4633 return error_mark_node;
4636 TREE_TYPE (inside_init) = type;
4637 if (TYPE_DOMAIN (type) != 0
4638 && TYPE_SIZE (type) != 0
4639 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4640 /* Subtract 1 (or sizeof (wchar_t))
4641 because it's ok to ignore the terminating null char
4642 that is counted in the length of the constant. */
4643 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4644 TREE_STRING_LENGTH (inside_init)
4645 - ((TYPE_PRECISION (typ1)
4646 != TYPE_PRECISION (char_type_node))
4647 ? (TYPE_PRECISION (wchar_type_node)
4648 / BITS_PER_UNIT)
4649 : 1)))
4650 pedwarn_init ("initializer-string for array of chars is too long");
4652 return inside_init;
4654 else if (INTEGRAL_TYPE_P (typ1))
4656 error_init ("array of inappropriate type initialized "
4657 "from string constant");
4658 return error_mark_node;
4662 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4663 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4664 below and handle as a constructor. */
4665 if (code == VECTOR_TYPE
4666 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4667 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4668 && TREE_CONSTANT (inside_init))
4670 if (TREE_CODE (inside_init) == VECTOR_CST
4671 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4672 TYPE_MAIN_VARIANT (type)))
4673 return inside_init;
4675 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4677 unsigned HOST_WIDE_INT ix;
4678 tree value;
4679 bool constant_p = true;
4681 /* Iterate through elements and check if all constructor
4682 elements are *_CSTs. */
4683 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4684 if (!CONSTANT_CLASS_P (value))
4686 constant_p = false;
4687 break;
4690 if (constant_p)
4691 return build_vector_from_ctor (type,
4692 CONSTRUCTOR_ELTS (inside_init));
4696 /* Any type can be initialized
4697 from an expression of the same type, optionally with braces. */
4699 if (inside_init && TREE_TYPE (inside_init) != 0
4700 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4701 TYPE_MAIN_VARIANT (type))
4702 || (code == ARRAY_TYPE
4703 && comptypes (TREE_TYPE (inside_init), type))
4704 || (code == VECTOR_TYPE
4705 && comptypes (TREE_TYPE (inside_init), type))
4706 || (code == POINTER_TYPE
4707 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4708 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4709 TREE_TYPE (type)))))
4711 if (code == POINTER_TYPE)
4713 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4715 if (TREE_CODE (inside_init) == STRING_CST
4716 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4717 inside_init = array_to_pointer_conversion (inside_init);
4718 else
4720 error_init ("invalid use of non-lvalue array");
4721 return error_mark_node;
4726 if (code == VECTOR_TYPE)
4727 /* Although the types are compatible, we may require a
4728 conversion. */
4729 inside_init = convert (type, inside_init);
4731 if (require_constant
4732 && (code == VECTOR_TYPE || !flag_isoc99)
4733 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4735 /* As an extension, allow initializing objects with static storage
4736 duration with compound literals (which are then treated just as
4737 the brace enclosed list they contain). Also allow this for
4738 vectors, as we can only assign them with compound literals. */
4739 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4740 inside_init = DECL_INITIAL (decl);
4743 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4744 && TREE_CODE (inside_init) != CONSTRUCTOR)
4746 error_init ("array initialized from non-constant array expression");
4747 return error_mark_node;
4750 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4751 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4753 /* Compound expressions can only occur here if -pedantic or
4754 -pedantic-errors is specified. In the later case, we always want
4755 an error. In the former case, we simply want a warning. */
4756 if (require_constant && pedantic
4757 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4759 inside_init
4760 = valid_compound_expr_initializer (inside_init,
4761 TREE_TYPE (inside_init));
4762 if (inside_init == error_mark_node)
4763 error_init ("initializer element is not constant");
4764 else
4765 pedwarn_init ("initializer element is not constant");
4766 if (flag_pedantic_errors)
4767 inside_init = error_mark_node;
4769 else if (require_constant
4770 && !initializer_constant_valid_p (inside_init,
4771 TREE_TYPE (inside_init)))
4773 error_init ("initializer element is not constant");
4774 inside_init = error_mark_node;
4777 /* Added to enable additional -Wmissing-format-attribute warnings. */
4778 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4779 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4780 NULL_TREE, 0);
4781 return inside_init;
4784 /* Handle scalar types, including conversions. */
4786 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4787 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4788 || code == VECTOR_TYPE)
4790 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4791 && (TREE_CODE (init) == STRING_CST
4792 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4793 init = array_to_pointer_conversion (init);
4794 inside_init
4795 = convert_for_assignment (type, init, ic_init,
4796 NULL_TREE, NULL_TREE, 0);
4798 /* Check to see if we have already given an error message. */
4799 if (inside_init == error_mark_node)
4801 else if (require_constant && !TREE_CONSTANT (inside_init))
4803 error_init ("initializer element is not constant");
4804 inside_init = error_mark_node;
4806 else if (require_constant
4807 && !initializer_constant_valid_p (inside_init,
4808 TREE_TYPE (inside_init)))
4810 error_init ("initializer element is not computable at load time");
4811 inside_init = error_mark_node;
4814 return inside_init;
4817 /* Come here only for records and arrays. */
4819 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4821 error_init ("variable-sized object may not be initialized");
4822 return error_mark_node;
4825 error_init ("invalid initializer");
4826 return error_mark_node;
4829 /* Handle initializers that use braces. */
4831 /* Type of object we are accumulating a constructor for.
4832 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4833 static tree constructor_type;
4835 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4836 left to fill. */
4837 static tree constructor_fields;
4839 /* For an ARRAY_TYPE, this is the specified index
4840 at which to store the next element we get. */
4841 static tree constructor_index;
4843 /* For an ARRAY_TYPE, this is the maximum index. */
4844 static tree constructor_max_index;
4846 /* For a RECORD_TYPE, this is the first field not yet written out. */
4847 static tree constructor_unfilled_fields;
4849 /* For an ARRAY_TYPE, this is the index of the first element
4850 not yet written out. */
4851 static tree constructor_unfilled_index;
4853 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4854 This is so we can generate gaps between fields, when appropriate. */
4855 static tree constructor_bit_index;
4857 /* If we are saving up the elements rather than allocating them,
4858 this is the list of elements so far (in reverse order,
4859 most recent first). */
4860 static VEC(constructor_elt,gc) *constructor_elements;
4862 /* 1 if constructor should be incrementally stored into a constructor chain,
4863 0 if all the elements should be kept in AVL tree. */
4864 static int constructor_incremental;
4866 /* 1 if so far this constructor's elements are all compile-time constants. */
4867 static int constructor_constant;
4869 /* 1 if so far this constructor's elements are all valid address constants. */
4870 static int constructor_simple;
4872 /* 1 if this constructor is erroneous so far. */
4873 static int constructor_erroneous;
4875 /* Structure for managing pending initializer elements, organized as an
4876 AVL tree. */
4878 struct init_node
4880 struct init_node *left, *right;
4881 struct init_node *parent;
4882 int balance;
4883 tree purpose;
4884 tree value;
4887 /* Tree of pending elements at this constructor level.
4888 These are elements encountered out of order
4889 which belong at places we haven't reached yet in actually
4890 writing the output.
4891 Will never hold tree nodes across GC runs. */
4892 static struct init_node *constructor_pending_elts;
4894 /* The SPELLING_DEPTH of this constructor. */
4895 static int constructor_depth;
4897 /* DECL node for which an initializer is being read.
4898 0 means we are reading a constructor expression
4899 such as (struct foo) {...}. */
4900 static tree constructor_decl;
4902 /* Nonzero if this is an initializer for a top-level decl. */
4903 static int constructor_top_level;
4905 /* Nonzero if there were any member designators in this initializer. */
4906 static int constructor_designated;
4908 /* Nesting depth of designator list. */
4909 static int designator_depth;
4911 /* Nonzero if there were diagnosed errors in this designator list. */
4912 static int designator_erroneous;
4915 /* This stack has a level for each implicit or explicit level of
4916 structuring in the initializer, including the outermost one. It
4917 saves the values of most of the variables above. */
4919 struct constructor_range_stack;
4921 struct constructor_stack
4923 struct constructor_stack *next;
4924 tree type;
4925 tree fields;
4926 tree index;
4927 tree max_index;
4928 tree unfilled_index;
4929 tree unfilled_fields;
4930 tree bit_index;
4931 VEC(constructor_elt,gc) *elements;
4932 struct init_node *pending_elts;
4933 int offset;
4934 int depth;
4935 /* If value nonzero, this value should replace the entire
4936 constructor at this level. */
4937 struct c_expr replacement_value;
4938 struct constructor_range_stack *range_stack;
4939 char constant;
4940 char simple;
4941 char implicit;
4942 char erroneous;
4943 char outer;
4944 char incremental;
4945 char designated;
4948 static struct constructor_stack *constructor_stack;
4950 /* This stack represents designators from some range designator up to
4951 the last designator in the list. */
4953 struct constructor_range_stack
4955 struct constructor_range_stack *next, *prev;
4956 struct constructor_stack *stack;
4957 tree range_start;
4958 tree index;
4959 tree range_end;
4960 tree fields;
4963 static struct constructor_range_stack *constructor_range_stack;
4965 /* This stack records separate initializers that are nested.
4966 Nested initializers can't happen in ANSI C, but GNU C allows them
4967 in cases like { ... (struct foo) { ... } ... }. */
4969 struct initializer_stack
4971 struct initializer_stack *next;
4972 tree decl;
4973 struct constructor_stack *constructor_stack;
4974 struct constructor_range_stack *constructor_range_stack;
4975 VEC(constructor_elt,gc) *elements;
4976 struct spelling *spelling;
4977 struct spelling *spelling_base;
4978 int spelling_size;
4979 char top_level;
4980 char require_constant_value;
4981 char require_constant_elements;
4984 static struct initializer_stack *initializer_stack;
4986 /* Prepare to parse and output the initializer for variable DECL. */
4988 void
4989 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4991 const char *locus;
4992 struct initializer_stack *p = XNEW (struct initializer_stack);
4994 p->decl = constructor_decl;
4995 p->require_constant_value = require_constant_value;
4996 p->require_constant_elements = require_constant_elements;
4997 p->constructor_stack = constructor_stack;
4998 p->constructor_range_stack = constructor_range_stack;
4999 p->elements = constructor_elements;
5000 p->spelling = spelling;
5001 p->spelling_base = spelling_base;
5002 p->spelling_size = spelling_size;
5003 p->top_level = constructor_top_level;
5004 p->next = initializer_stack;
5005 initializer_stack = p;
5007 constructor_decl = decl;
5008 constructor_designated = 0;
5009 constructor_top_level = top_level;
5011 if (decl != 0 && decl != error_mark_node)
5013 require_constant_value = TREE_STATIC (decl);
5014 require_constant_elements
5015 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5016 /* For a scalar, you can always use any value to initialize,
5017 even within braces. */
5018 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5019 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5020 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5021 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5022 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5024 else
5026 require_constant_value = 0;
5027 require_constant_elements = 0;
5028 locus = "(anonymous)";
5031 constructor_stack = 0;
5032 constructor_range_stack = 0;
5034 missing_braces_mentioned = 0;
5036 spelling_base = 0;
5037 spelling_size = 0;
5038 RESTORE_SPELLING_DEPTH (0);
5040 if (locus)
5041 push_string (locus);
5044 void
5045 finish_init (void)
5047 struct initializer_stack *p = initializer_stack;
5049 /* Free the whole constructor stack of this initializer. */
5050 while (constructor_stack)
5052 struct constructor_stack *q = constructor_stack;
5053 constructor_stack = q->next;
5054 free (q);
5057 gcc_assert (!constructor_range_stack);
5059 /* Pop back to the data of the outer initializer (if any). */
5060 free (spelling_base);
5062 constructor_decl = p->decl;
5063 require_constant_value = p->require_constant_value;
5064 require_constant_elements = p->require_constant_elements;
5065 constructor_stack = p->constructor_stack;
5066 constructor_range_stack = p->constructor_range_stack;
5067 constructor_elements = p->elements;
5068 spelling = p->spelling;
5069 spelling_base = p->spelling_base;
5070 spelling_size = p->spelling_size;
5071 constructor_top_level = p->top_level;
5072 initializer_stack = p->next;
5073 free (p);
5076 /* Call here when we see the initializer is surrounded by braces.
5077 This is instead of a call to push_init_level;
5078 it is matched by a call to pop_init_level.
5080 TYPE is the type to initialize, for a constructor expression.
5081 For an initializer for a decl, TYPE is zero. */
5083 void
5084 really_start_incremental_init (tree type)
5086 struct constructor_stack *p = XNEW (struct constructor_stack);
5088 if (type == 0)
5089 type = TREE_TYPE (constructor_decl);
5091 if (targetm.vector_opaque_p (type))
5092 error ("opaque vector types cannot be initialized");
5094 p->type = constructor_type;
5095 p->fields = constructor_fields;
5096 p->index = constructor_index;
5097 p->max_index = constructor_max_index;
5098 p->unfilled_index = constructor_unfilled_index;
5099 p->unfilled_fields = constructor_unfilled_fields;
5100 p->bit_index = constructor_bit_index;
5101 p->elements = constructor_elements;
5102 p->constant = constructor_constant;
5103 p->simple = constructor_simple;
5104 p->erroneous = constructor_erroneous;
5105 p->pending_elts = constructor_pending_elts;
5106 p->depth = constructor_depth;
5107 p->replacement_value.value = 0;
5108 p->replacement_value.original_code = ERROR_MARK;
5109 p->implicit = 0;
5110 p->range_stack = 0;
5111 p->outer = 0;
5112 p->incremental = constructor_incremental;
5113 p->designated = constructor_designated;
5114 p->next = 0;
5115 constructor_stack = p;
5117 constructor_constant = 1;
5118 constructor_simple = 1;
5119 constructor_depth = SPELLING_DEPTH ();
5120 constructor_elements = 0;
5121 constructor_pending_elts = 0;
5122 constructor_type = type;
5123 constructor_incremental = 1;
5124 constructor_designated = 0;
5125 designator_depth = 0;
5126 designator_erroneous = 0;
5128 if (TREE_CODE (constructor_type) == RECORD_TYPE
5129 || TREE_CODE (constructor_type) == UNION_TYPE)
5131 constructor_fields = TYPE_FIELDS (constructor_type);
5132 /* Skip any nameless bit fields at the beginning. */
5133 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5134 && DECL_NAME (constructor_fields) == 0)
5135 constructor_fields = TREE_CHAIN (constructor_fields);
5137 constructor_unfilled_fields = constructor_fields;
5138 constructor_bit_index = bitsize_zero_node;
5140 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5142 if (TYPE_DOMAIN (constructor_type))
5144 constructor_max_index
5145 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5147 /* Detect non-empty initializations of zero-length arrays. */
5148 if (constructor_max_index == NULL_TREE
5149 && TYPE_SIZE (constructor_type))
5150 constructor_max_index = build_int_cst (NULL_TREE, -1);
5152 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5153 to initialize VLAs will cause a proper error; avoid tree
5154 checking errors as well by setting a safe value. */
5155 if (constructor_max_index
5156 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5157 constructor_max_index = build_int_cst (NULL_TREE, -1);
5159 constructor_index
5160 = convert (bitsizetype,
5161 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5163 else
5165 constructor_index = bitsize_zero_node;
5166 constructor_max_index = NULL_TREE;
5169 constructor_unfilled_index = constructor_index;
5171 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5173 /* Vectors are like simple fixed-size arrays. */
5174 constructor_max_index =
5175 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5176 constructor_index = bitsize_zero_node;
5177 constructor_unfilled_index = constructor_index;
5179 else
5181 /* Handle the case of int x = {5}; */
5182 constructor_fields = constructor_type;
5183 constructor_unfilled_fields = constructor_type;
5187 /* Push down into a subobject, for initialization.
5188 If this is for an explicit set of braces, IMPLICIT is 0.
5189 If it is because the next element belongs at a lower level,
5190 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5192 void
5193 push_init_level (int implicit)
5195 struct constructor_stack *p;
5196 tree value = NULL_TREE;
5198 /* If we've exhausted any levels that didn't have braces,
5199 pop them now. If implicit == 1, this will have been done in
5200 process_init_element; do not repeat it here because in the case
5201 of excess initializers for an empty aggregate this leads to an
5202 infinite cycle of popping a level and immediately recreating
5203 it. */
5204 if (implicit != 1)
5206 while (constructor_stack->implicit)
5208 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5209 || TREE_CODE (constructor_type) == UNION_TYPE)
5210 && constructor_fields == 0)
5211 process_init_element (pop_init_level (1));
5212 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5213 && constructor_max_index
5214 && tree_int_cst_lt (constructor_max_index,
5215 constructor_index))
5216 process_init_element (pop_init_level (1));
5217 else
5218 break;
5222 /* Unless this is an explicit brace, we need to preserve previous
5223 content if any. */
5224 if (implicit)
5226 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5227 || TREE_CODE (constructor_type) == UNION_TYPE)
5228 && constructor_fields)
5229 value = find_init_member (constructor_fields);
5230 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5231 value = find_init_member (constructor_index);
5234 p = XNEW (struct constructor_stack);
5235 p->type = constructor_type;
5236 p->fields = constructor_fields;
5237 p->index = constructor_index;
5238 p->max_index = constructor_max_index;
5239 p->unfilled_index = constructor_unfilled_index;
5240 p->unfilled_fields = constructor_unfilled_fields;
5241 p->bit_index = constructor_bit_index;
5242 p->elements = constructor_elements;
5243 p->constant = constructor_constant;
5244 p->simple = constructor_simple;
5245 p->erroneous = constructor_erroneous;
5246 p->pending_elts = constructor_pending_elts;
5247 p->depth = constructor_depth;
5248 p->replacement_value.value = 0;
5249 p->replacement_value.original_code = ERROR_MARK;
5250 p->implicit = implicit;
5251 p->outer = 0;
5252 p->incremental = constructor_incremental;
5253 p->designated = constructor_designated;
5254 p->next = constructor_stack;
5255 p->range_stack = 0;
5256 constructor_stack = p;
5258 constructor_constant = 1;
5259 constructor_simple = 1;
5260 constructor_depth = SPELLING_DEPTH ();
5261 constructor_elements = 0;
5262 constructor_incremental = 1;
5263 constructor_designated = 0;
5264 constructor_pending_elts = 0;
5265 if (!implicit)
5267 p->range_stack = constructor_range_stack;
5268 constructor_range_stack = 0;
5269 designator_depth = 0;
5270 designator_erroneous = 0;
5273 /* Don't die if an entire brace-pair level is superfluous
5274 in the containing level. */
5275 if (constructor_type == 0)
5277 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5278 || TREE_CODE (constructor_type) == UNION_TYPE)
5280 /* Don't die if there are extra init elts at the end. */
5281 if (constructor_fields == 0)
5282 constructor_type = 0;
5283 else
5285 constructor_type = TREE_TYPE (constructor_fields);
5286 push_member_name (constructor_fields);
5287 constructor_depth++;
5290 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5292 constructor_type = TREE_TYPE (constructor_type);
5293 push_array_bounds (tree_low_cst (constructor_index, 1));
5294 constructor_depth++;
5297 if (constructor_type == 0)
5299 error_init ("extra brace group at end of initializer");
5300 constructor_fields = 0;
5301 constructor_unfilled_fields = 0;
5302 return;
5305 if (value && TREE_CODE (value) == CONSTRUCTOR)
5307 constructor_constant = TREE_CONSTANT (value);
5308 constructor_simple = TREE_STATIC (value);
5309 constructor_elements = CONSTRUCTOR_ELTS (value);
5310 if (!VEC_empty (constructor_elt, constructor_elements)
5311 && (TREE_CODE (constructor_type) == RECORD_TYPE
5312 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5313 set_nonincremental_init ();
5316 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5318 missing_braces_mentioned = 1;
5319 warning_init ("missing braces around initializer");
5322 if (TREE_CODE (constructor_type) == RECORD_TYPE
5323 || TREE_CODE (constructor_type) == UNION_TYPE)
5325 constructor_fields = TYPE_FIELDS (constructor_type);
5326 /* Skip any nameless bit fields at the beginning. */
5327 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5328 && DECL_NAME (constructor_fields) == 0)
5329 constructor_fields = TREE_CHAIN (constructor_fields);
5331 constructor_unfilled_fields = constructor_fields;
5332 constructor_bit_index = bitsize_zero_node;
5334 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5336 /* Vectors are like simple fixed-size arrays. */
5337 constructor_max_index =
5338 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5339 constructor_index = convert (bitsizetype, integer_zero_node);
5340 constructor_unfilled_index = constructor_index;
5342 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5344 if (TYPE_DOMAIN (constructor_type))
5346 constructor_max_index
5347 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5349 /* Detect non-empty initializations of zero-length arrays. */
5350 if (constructor_max_index == NULL_TREE
5351 && TYPE_SIZE (constructor_type))
5352 constructor_max_index = build_int_cst (NULL_TREE, -1);
5354 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5355 to initialize VLAs will cause a proper error; avoid tree
5356 checking errors as well by setting a safe value. */
5357 if (constructor_max_index
5358 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5359 constructor_max_index = build_int_cst (NULL_TREE, -1);
5361 constructor_index
5362 = convert (bitsizetype,
5363 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5365 else
5366 constructor_index = bitsize_zero_node;
5368 constructor_unfilled_index = constructor_index;
5369 if (value && TREE_CODE (value) == STRING_CST)
5371 /* We need to split the char/wchar array into individual
5372 characters, so that we don't have to special case it
5373 everywhere. */
5374 set_nonincremental_init_from_string (value);
5377 else
5379 if (constructor_type != error_mark_node)
5380 warning_init ("braces around scalar initializer");
5381 constructor_fields = constructor_type;
5382 constructor_unfilled_fields = constructor_type;
5386 /* At the end of an implicit or explicit brace level,
5387 finish up that level of constructor. If a single expression
5388 with redundant braces initialized that level, return the
5389 c_expr structure for that expression. Otherwise, the original_code
5390 element is set to ERROR_MARK.
5391 If we were outputting the elements as they are read, return 0 as the value
5392 from inner levels (process_init_element ignores that),
5393 but return error_mark_node as the value from the outermost level
5394 (that's what we want to put in DECL_INITIAL).
5395 Otherwise, return a CONSTRUCTOR expression as the value. */
5397 struct c_expr
5398 pop_init_level (int implicit)
5400 struct constructor_stack *p;
5401 struct c_expr ret;
5402 ret.value = 0;
5403 ret.original_code = ERROR_MARK;
5405 if (implicit == 0)
5407 /* When we come to an explicit close brace,
5408 pop any inner levels that didn't have explicit braces. */
5409 while (constructor_stack->implicit)
5410 process_init_element (pop_init_level (1));
5412 gcc_assert (!constructor_range_stack);
5415 /* Now output all pending elements. */
5416 constructor_incremental = 1;
5417 output_pending_init_elements (1);
5419 p = constructor_stack;
5421 /* Error for initializing a flexible array member, or a zero-length
5422 array member in an inappropriate context. */
5423 if (constructor_type && constructor_fields
5424 && TREE_CODE (constructor_type) == ARRAY_TYPE
5425 && TYPE_DOMAIN (constructor_type)
5426 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5428 /* Silently discard empty initializations. The parser will
5429 already have pedwarned for empty brackets. */
5430 if (integer_zerop (constructor_unfilled_index))
5431 constructor_type = NULL_TREE;
5432 else
5434 gcc_assert (!TYPE_SIZE (constructor_type));
5436 if (constructor_depth > 2)
5437 error_init ("initialization of flexible array member in a nested context");
5438 else if (pedantic)
5439 pedwarn_init ("initialization of a flexible array member");
5441 /* We have already issued an error message for the existence
5442 of a flexible array member not at the end of the structure.
5443 Discard the initializer so that we do not die later. */
5444 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5445 constructor_type = NULL_TREE;
5449 /* Warn when some struct elements are implicitly initialized to zero. */
5450 if (warn_missing_field_initializers
5451 && constructor_type
5452 && TREE_CODE (constructor_type) == RECORD_TYPE
5453 && constructor_unfilled_fields)
5455 /* Do not warn for flexible array members or zero-length arrays. */
5456 while (constructor_unfilled_fields
5457 && (!DECL_SIZE (constructor_unfilled_fields)
5458 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5459 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5461 /* Do not warn if this level of the initializer uses member
5462 designators; it is likely to be deliberate. */
5463 if (constructor_unfilled_fields && !constructor_designated)
5465 push_member_name (constructor_unfilled_fields);
5466 warning_init ("missing initializer");
5467 RESTORE_SPELLING_DEPTH (constructor_depth);
5471 /* Pad out the end of the structure. */
5472 if (p->replacement_value.value)
5473 /* If this closes a superfluous brace pair,
5474 just pass out the element between them. */
5475 ret = p->replacement_value;
5476 else if (constructor_type == 0)
5478 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5479 && TREE_CODE (constructor_type) != UNION_TYPE
5480 && TREE_CODE (constructor_type) != ARRAY_TYPE
5481 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5483 /* A nonincremental scalar initializer--just return
5484 the element, after verifying there is just one. */
5485 if (VEC_empty (constructor_elt,constructor_elements))
5487 if (!constructor_erroneous)
5488 error_init ("empty scalar initializer");
5489 ret.value = error_mark_node;
5491 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5493 error_init ("extra elements in scalar initializer");
5494 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5496 else
5497 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5499 else
5501 if (constructor_erroneous)
5502 ret.value = error_mark_node;
5503 else
5505 ret.value = build_constructor (constructor_type,
5506 constructor_elements);
5507 if (constructor_constant)
5508 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5509 if (constructor_constant && constructor_simple)
5510 TREE_STATIC (ret.value) = 1;
5514 constructor_type = p->type;
5515 constructor_fields = p->fields;
5516 constructor_index = p->index;
5517 constructor_max_index = p->max_index;
5518 constructor_unfilled_index = p->unfilled_index;
5519 constructor_unfilled_fields = p->unfilled_fields;
5520 constructor_bit_index = p->bit_index;
5521 constructor_elements = p->elements;
5522 constructor_constant = p->constant;
5523 constructor_simple = p->simple;
5524 constructor_erroneous = p->erroneous;
5525 constructor_incremental = p->incremental;
5526 constructor_designated = p->designated;
5527 constructor_pending_elts = p->pending_elts;
5528 constructor_depth = p->depth;
5529 if (!p->implicit)
5530 constructor_range_stack = p->range_stack;
5531 RESTORE_SPELLING_DEPTH (constructor_depth);
5533 constructor_stack = p->next;
5534 free (p);
5536 if (ret.value == 0 && constructor_stack == 0)
5537 ret.value = error_mark_node;
5538 return ret;
5541 /* Common handling for both array range and field name designators.
5542 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5544 static int
5545 set_designator (int array)
5547 tree subtype;
5548 enum tree_code subcode;
5550 /* Don't die if an entire brace-pair level is superfluous
5551 in the containing level. */
5552 if (constructor_type == 0)
5553 return 1;
5555 /* If there were errors in this designator list already, bail out
5556 silently. */
5557 if (designator_erroneous)
5558 return 1;
5560 if (!designator_depth)
5562 gcc_assert (!constructor_range_stack);
5564 /* Designator list starts at the level of closest explicit
5565 braces. */
5566 while (constructor_stack->implicit)
5567 process_init_element (pop_init_level (1));
5568 constructor_designated = 1;
5569 return 0;
5572 switch (TREE_CODE (constructor_type))
5574 case RECORD_TYPE:
5575 case UNION_TYPE:
5576 subtype = TREE_TYPE (constructor_fields);
5577 if (subtype != error_mark_node)
5578 subtype = TYPE_MAIN_VARIANT (subtype);
5579 break;
5580 case ARRAY_TYPE:
5581 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5582 break;
5583 default:
5584 gcc_unreachable ();
5587 subcode = TREE_CODE (subtype);
5588 if (array && subcode != ARRAY_TYPE)
5590 error_init ("array index in non-array initializer");
5591 return 1;
5593 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5595 error_init ("field name not in record or union initializer");
5596 return 1;
5599 constructor_designated = 1;
5600 push_init_level (2);
5601 return 0;
5604 /* If there are range designators in designator list, push a new designator
5605 to constructor_range_stack. RANGE_END is end of such stack range or
5606 NULL_TREE if there is no range designator at this level. */
5608 static void
5609 push_range_stack (tree range_end)
5611 struct constructor_range_stack *p;
5613 p = GGC_NEW (struct constructor_range_stack);
5614 p->prev = constructor_range_stack;
5615 p->next = 0;
5616 p->fields = constructor_fields;
5617 p->range_start = constructor_index;
5618 p->index = constructor_index;
5619 p->stack = constructor_stack;
5620 p->range_end = range_end;
5621 if (constructor_range_stack)
5622 constructor_range_stack->next = p;
5623 constructor_range_stack = p;
5626 /* Within an array initializer, specify the next index to be initialized.
5627 FIRST is that index. If LAST is nonzero, then initialize a range
5628 of indices, running from FIRST through LAST. */
5630 void
5631 set_init_index (tree first, tree last)
5633 if (set_designator (1))
5634 return;
5636 designator_erroneous = 1;
5638 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5639 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5641 error_init ("array index in initializer not of integer type");
5642 return;
5645 if (TREE_CODE (first) != INTEGER_CST)
5646 error_init ("nonconstant array index in initializer");
5647 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5648 error_init ("nonconstant array index in initializer");
5649 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5650 error_init ("array index in non-array initializer");
5651 else if (tree_int_cst_sgn (first) == -1)
5652 error_init ("array index in initializer exceeds array bounds");
5653 else if (constructor_max_index
5654 && tree_int_cst_lt (constructor_max_index, first))
5655 error_init ("array index in initializer exceeds array bounds");
5656 else
5658 constructor_index = convert (bitsizetype, first);
5660 if (last)
5662 if (tree_int_cst_equal (first, last))
5663 last = 0;
5664 else if (tree_int_cst_lt (last, first))
5666 error_init ("empty index range in initializer");
5667 last = 0;
5669 else
5671 last = convert (bitsizetype, last);
5672 if (constructor_max_index != 0
5673 && tree_int_cst_lt (constructor_max_index, last))
5675 error_init ("array index range in initializer exceeds array bounds");
5676 last = 0;
5681 designator_depth++;
5682 designator_erroneous = 0;
5683 if (constructor_range_stack || last)
5684 push_range_stack (last);
5688 /* Within a struct initializer, specify the next field to be initialized. */
5690 void
5691 set_init_label (tree fieldname)
5693 tree tail;
5695 if (set_designator (0))
5696 return;
5698 designator_erroneous = 1;
5700 if (TREE_CODE (constructor_type) != RECORD_TYPE
5701 && TREE_CODE (constructor_type) != UNION_TYPE)
5703 error_init ("field name not in record or union initializer");
5704 return;
5707 for (tail = TYPE_FIELDS (constructor_type); tail;
5708 tail = TREE_CHAIN (tail))
5710 if (DECL_NAME (tail) == fieldname)
5711 break;
5714 if (tail == 0)
5715 error ("unknown field %qE specified in initializer", fieldname);
5716 else
5718 constructor_fields = tail;
5719 designator_depth++;
5720 designator_erroneous = 0;
5721 if (constructor_range_stack)
5722 push_range_stack (NULL_TREE);
5726 /* Add a new initializer to the tree of pending initializers. PURPOSE
5727 identifies the initializer, either array index or field in a structure.
5728 VALUE is the value of that index or field. */
5730 static void
5731 add_pending_init (tree purpose, tree value)
5733 struct init_node *p, **q, *r;
5735 q = &constructor_pending_elts;
5736 p = 0;
5738 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5740 while (*q != 0)
5742 p = *q;
5743 if (tree_int_cst_lt (purpose, p->purpose))
5744 q = &p->left;
5745 else if (tree_int_cst_lt (p->purpose, purpose))
5746 q = &p->right;
5747 else
5749 if (TREE_SIDE_EFFECTS (p->value))
5750 warning_init ("initialized field with side-effects overwritten");
5751 else if (warn_override_init)
5752 warning_init ("initialized field overwritten");
5753 p->value = value;
5754 return;
5758 else
5760 tree bitpos;
5762 bitpos = bit_position (purpose);
5763 while (*q != NULL)
5765 p = *q;
5766 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5767 q = &p->left;
5768 else if (p->purpose != purpose)
5769 q = &p->right;
5770 else
5772 if (TREE_SIDE_EFFECTS (p->value))
5773 warning_init ("initialized field with side-effects overwritten");
5774 else if (warn_override_init)
5775 warning_init ("initialized field overwritten");
5776 p->value = value;
5777 return;
5782 r = GGC_NEW (struct init_node);
5783 r->purpose = purpose;
5784 r->value = value;
5786 *q = r;
5787 r->parent = p;
5788 r->left = 0;
5789 r->right = 0;
5790 r->balance = 0;
5792 while (p)
5794 struct init_node *s;
5796 if (r == p->left)
5798 if (p->balance == 0)
5799 p->balance = -1;
5800 else if (p->balance < 0)
5802 if (r->balance < 0)
5804 /* L rotation. */
5805 p->left = r->right;
5806 if (p->left)
5807 p->left->parent = p;
5808 r->right = p;
5810 p->balance = 0;
5811 r->balance = 0;
5813 s = p->parent;
5814 p->parent = r;
5815 r->parent = s;
5816 if (s)
5818 if (s->left == p)
5819 s->left = r;
5820 else
5821 s->right = r;
5823 else
5824 constructor_pending_elts = r;
5826 else
5828 /* LR rotation. */
5829 struct init_node *t = r->right;
5831 r->right = t->left;
5832 if (r->right)
5833 r->right->parent = r;
5834 t->left = r;
5836 p->left = t->right;
5837 if (p->left)
5838 p->left->parent = p;
5839 t->right = p;
5841 p->balance = t->balance < 0;
5842 r->balance = -(t->balance > 0);
5843 t->balance = 0;
5845 s = p->parent;
5846 p->parent = t;
5847 r->parent = t;
5848 t->parent = s;
5849 if (s)
5851 if (s->left == p)
5852 s->left = t;
5853 else
5854 s->right = t;
5856 else
5857 constructor_pending_elts = t;
5859 break;
5861 else
5863 /* p->balance == +1; growth of left side balances the node. */
5864 p->balance = 0;
5865 break;
5868 else /* r == p->right */
5870 if (p->balance == 0)
5871 /* Growth propagation from right side. */
5872 p->balance++;
5873 else if (p->balance > 0)
5875 if (r->balance > 0)
5877 /* R rotation. */
5878 p->right = r->left;
5879 if (p->right)
5880 p->right->parent = p;
5881 r->left = p;
5883 p->balance = 0;
5884 r->balance = 0;
5886 s = p->parent;
5887 p->parent = r;
5888 r->parent = s;
5889 if (s)
5891 if (s->left == p)
5892 s->left = r;
5893 else
5894 s->right = r;
5896 else
5897 constructor_pending_elts = r;
5899 else /* r->balance == -1 */
5901 /* RL rotation */
5902 struct init_node *t = r->left;
5904 r->left = t->right;
5905 if (r->left)
5906 r->left->parent = r;
5907 t->right = r;
5909 p->right = t->left;
5910 if (p->right)
5911 p->right->parent = p;
5912 t->left = p;
5914 r->balance = (t->balance < 0);
5915 p->balance = -(t->balance > 0);
5916 t->balance = 0;
5918 s = p->parent;
5919 p->parent = t;
5920 r->parent = t;
5921 t->parent = s;
5922 if (s)
5924 if (s->left == p)
5925 s->left = t;
5926 else
5927 s->right = t;
5929 else
5930 constructor_pending_elts = t;
5932 break;
5934 else
5936 /* p->balance == -1; growth of right side balances the node. */
5937 p->balance = 0;
5938 break;
5942 r = p;
5943 p = p->parent;
5947 /* Build AVL tree from a sorted chain. */
5949 static void
5950 set_nonincremental_init (void)
5952 unsigned HOST_WIDE_INT ix;
5953 tree index, value;
5955 if (TREE_CODE (constructor_type) != RECORD_TYPE
5956 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5957 return;
5959 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5960 add_pending_init (index, value);
5961 constructor_elements = 0;
5962 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5964 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5965 /* Skip any nameless bit fields at the beginning. */
5966 while (constructor_unfilled_fields != 0
5967 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5968 && DECL_NAME (constructor_unfilled_fields) == 0)
5969 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5972 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5974 if (TYPE_DOMAIN (constructor_type))
5975 constructor_unfilled_index
5976 = convert (bitsizetype,
5977 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5978 else
5979 constructor_unfilled_index = bitsize_zero_node;
5981 constructor_incremental = 0;
5984 /* Build AVL tree from a string constant. */
5986 static void
5987 set_nonincremental_init_from_string (tree str)
5989 tree value, purpose, type;
5990 HOST_WIDE_INT val[2];
5991 const char *p, *end;
5992 int byte, wchar_bytes, charwidth, bitpos;
5994 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5996 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5997 == TYPE_PRECISION (char_type_node))
5998 wchar_bytes = 1;
5999 else
6001 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6002 == TYPE_PRECISION (wchar_type_node));
6003 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6005 charwidth = TYPE_PRECISION (char_type_node);
6006 type = TREE_TYPE (constructor_type);
6007 p = TREE_STRING_POINTER (str);
6008 end = p + TREE_STRING_LENGTH (str);
6010 for (purpose = bitsize_zero_node;
6011 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6012 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6014 if (wchar_bytes == 1)
6016 val[1] = (unsigned char) *p++;
6017 val[0] = 0;
6019 else
6021 val[0] = 0;
6022 val[1] = 0;
6023 for (byte = 0; byte < wchar_bytes; byte++)
6025 if (BYTES_BIG_ENDIAN)
6026 bitpos = (wchar_bytes - byte - 1) * charwidth;
6027 else
6028 bitpos = byte * charwidth;
6029 val[bitpos < HOST_BITS_PER_WIDE_INT]
6030 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6031 << (bitpos % HOST_BITS_PER_WIDE_INT);
6035 if (!TYPE_UNSIGNED (type))
6037 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6038 if (bitpos < HOST_BITS_PER_WIDE_INT)
6040 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6042 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6043 val[0] = -1;
6046 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6048 if (val[1] < 0)
6049 val[0] = -1;
6051 else if (val[0] & (((HOST_WIDE_INT) 1)
6052 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6053 val[0] |= ((HOST_WIDE_INT) -1)
6054 << (bitpos - HOST_BITS_PER_WIDE_INT);
6057 value = build_int_cst_wide (type, val[1], val[0]);
6058 add_pending_init (purpose, value);
6061 constructor_incremental = 0;
6064 /* Return value of FIELD in pending initializer or zero if the field was
6065 not initialized yet. */
6067 static tree
6068 find_init_member (tree field)
6070 struct init_node *p;
6072 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6074 if (constructor_incremental
6075 && tree_int_cst_lt (field, constructor_unfilled_index))
6076 set_nonincremental_init ();
6078 p = constructor_pending_elts;
6079 while (p)
6081 if (tree_int_cst_lt (field, p->purpose))
6082 p = p->left;
6083 else if (tree_int_cst_lt (p->purpose, field))
6084 p = p->right;
6085 else
6086 return p->value;
6089 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6091 tree bitpos = bit_position (field);
6093 if (constructor_incremental
6094 && (!constructor_unfilled_fields
6095 || tree_int_cst_lt (bitpos,
6096 bit_position (constructor_unfilled_fields))))
6097 set_nonincremental_init ();
6099 p = constructor_pending_elts;
6100 while (p)
6102 if (field == p->purpose)
6103 return p->value;
6104 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6105 p = p->left;
6106 else
6107 p = p->right;
6110 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6112 if (!VEC_empty (constructor_elt, constructor_elements)
6113 && (VEC_last (constructor_elt, constructor_elements)->index
6114 == field))
6115 return VEC_last (constructor_elt, constructor_elements)->value;
6117 return 0;
6120 /* "Output" the next constructor element.
6121 At top level, really output it to assembler code now.
6122 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6123 TYPE is the data type that the containing data type wants here.
6124 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6125 If VALUE is a string constant, STRICT_STRING is true if it is
6126 unparenthesized or we should not warn here for it being parenthesized.
6127 For other types of VALUE, STRICT_STRING is not used.
6129 PENDING if non-nil means output pending elements that belong
6130 right after this element. (PENDING is normally 1;
6131 it is 0 while outputting pending elements, to avoid recursion.) */
6133 static void
6134 output_init_element (tree value, bool strict_string, tree type, tree field,
6135 int pending)
6137 constructor_elt *celt;
6139 if (type == error_mark_node || value == error_mark_node)
6141 constructor_erroneous = 1;
6142 return;
6144 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6145 && (TREE_CODE (value) == STRING_CST
6146 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6147 && !(TREE_CODE (value) == STRING_CST
6148 && TREE_CODE (type) == ARRAY_TYPE
6149 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6150 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6151 TYPE_MAIN_VARIANT (type)))
6152 value = array_to_pointer_conversion (value);
6154 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6155 && require_constant_value && !flag_isoc99 && pending)
6157 /* As an extension, allow initializing objects with static storage
6158 duration with compound literals (which are then treated just as
6159 the brace enclosed list they contain). */
6160 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6161 value = DECL_INITIAL (decl);
6164 if (value == error_mark_node)
6165 constructor_erroneous = 1;
6166 else if (!TREE_CONSTANT (value))
6167 constructor_constant = 0;
6168 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6169 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6170 || TREE_CODE (constructor_type) == UNION_TYPE)
6171 && DECL_C_BIT_FIELD (field)
6172 && TREE_CODE (value) != INTEGER_CST))
6173 constructor_simple = 0;
6175 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6177 if (require_constant_value)
6179 error_init ("initializer element is not constant");
6180 value = error_mark_node;
6182 else if (require_constant_elements)
6183 pedwarn ("initializer element is not computable at load time");
6186 /* If this field is empty (and not at the end of structure),
6187 don't do anything other than checking the initializer. */
6188 if (field
6189 && (TREE_TYPE (field) == error_mark_node
6190 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6191 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6192 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6193 || TREE_CHAIN (field)))))
6194 return;
6196 value = digest_init (type, value, strict_string, require_constant_value);
6197 if (value == error_mark_node)
6199 constructor_erroneous = 1;
6200 return;
6203 /* If this element doesn't come next in sequence,
6204 put it on constructor_pending_elts. */
6205 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6206 && (!constructor_incremental
6207 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6209 if (constructor_incremental
6210 && tree_int_cst_lt (field, constructor_unfilled_index))
6211 set_nonincremental_init ();
6213 add_pending_init (field, value);
6214 return;
6216 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6217 && (!constructor_incremental
6218 || field != constructor_unfilled_fields))
6220 /* We do this for records but not for unions. In a union,
6221 no matter which field is specified, it can be initialized
6222 right away since it starts at the beginning of the union. */
6223 if (constructor_incremental)
6225 if (!constructor_unfilled_fields)
6226 set_nonincremental_init ();
6227 else
6229 tree bitpos, unfillpos;
6231 bitpos = bit_position (field);
6232 unfillpos = bit_position (constructor_unfilled_fields);
6234 if (tree_int_cst_lt (bitpos, unfillpos))
6235 set_nonincremental_init ();
6239 add_pending_init (field, value);
6240 return;
6242 else if (TREE_CODE (constructor_type) == UNION_TYPE
6243 && !VEC_empty (constructor_elt, constructor_elements))
6245 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6246 constructor_elements)->value))
6247 warning_init ("initialized field with side-effects overwritten");
6248 else if (warn_override_init)
6249 warning_init ("initialized field overwritten");
6251 /* We can have just one union field set. */
6252 constructor_elements = 0;
6255 /* Otherwise, output this element either to
6256 constructor_elements or to the assembler file. */
6258 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6259 celt->index = field;
6260 celt->value = value;
6262 /* Advance the variable that indicates sequential elements output. */
6263 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6264 constructor_unfilled_index
6265 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6266 bitsize_one_node);
6267 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6269 constructor_unfilled_fields
6270 = TREE_CHAIN (constructor_unfilled_fields);
6272 /* Skip any nameless bit fields. */
6273 while (constructor_unfilled_fields != 0
6274 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6275 && DECL_NAME (constructor_unfilled_fields) == 0)
6276 constructor_unfilled_fields =
6277 TREE_CHAIN (constructor_unfilled_fields);
6279 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6280 constructor_unfilled_fields = 0;
6282 /* Now output any pending elements which have become next. */
6283 if (pending)
6284 output_pending_init_elements (0);
6287 /* Output any pending elements which have become next.
6288 As we output elements, constructor_unfilled_{fields,index}
6289 advances, which may cause other elements to become next;
6290 if so, they too are output.
6292 If ALL is 0, we return when there are
6293 no more pending elements to output now.
6295 If ALL is 1, we output space as necessary so that
6296 we can output all the pending elements. */
6298 static void
6299 output_pending_init_elements (int all)
6301 struct init_node *elt = constructor_pending_elts;
6302 tree next;
6304 retry:
6306 /* Look through the whole pending tree.
6307 If we find an element that should be output now,
6308 output it. Otherwise, set NEXT to the element
6309 that comes first among those still pending. */
6311 next = 0;
6312 while (elt)
6314 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6316 if (tree_int_cst_equal (elt->purpose,
6317 constructor_unfilled_index))
6318 output_init_element (elt->value, true,
6319 TREE_TYPE (constructor_type),
6320 constructor_unfilled_index, 0);
6321 else if (tree_int_cst_lt (constructor_unfilled_index,
6322 elt->purpose))
6324 /* Advance to the next smaller node. */
6325 if (elt->left)
6326 elt = elt->left;
6327 else
6329 /* We have reached the smallest node bigger than the
6330 current unfilled index. Fill the space first. */
6331 next = elt->purpose;
6332 break;
6335 else
6337 /* Advance to the next bigger node. */
6338 if (elt->right)
6339 elt = elt->right;
6340 else
6342 /* We have reached the biggest node in a subtree. Find
6343 the parent of it, which is the next bigger node. */
6344 while (elt->parent && elt->parent->right == elt)
6345 elt = elt->parent;
6346 elt = elt->parent;
6347 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6348 elt->purpose))
6350 next = elt->purpose;
6351 break;
6356 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6357 || TREE_CODE (constructor_type) == UNION_TYPE)
6359 tree ctor_unfilled_bitpos, elt_bitpos;
6361 /* If the current record is complete we are done. */
6362 if (constructor_unfilled_fields == 0)
6363 break;
6365 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6366 elt_bitpos = bit_position (elt->purpose);
6367 /* We can't compare fields here because there might be empty
6368 fields in between. */
6369 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6371 constructor_unfilled_fields = elt->purpose;
6372 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6373 elt->purpose, 0);
6375 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6377 /* Advance to the next smaller node. */
6378 if (elt->left)
6379 elt = elt->left;
6380 else
6382 /* We have reached the smallest node bigger than the
6383 current unfilled field. Fill the space first. */
6384 next = elt->purpose;
6385 break;
6388 else
6390 /* Advance to the next bigger node. */
6391 if (elt->right)
6392 elt = elt->right;
6393 else
6395 /* We have reached the biggest node in a subtree. Find
6396 the parent of it, which is the next bigger node. */
6397 while (elt->parent && elt->parent->right == elt)
6398 elt = elt->parent;
6399 elt = elt->parent;
6400 if (elt
6401 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6402 bit_position (elt->purpose))))
6404 next = elt->purpose;
6405 break;
6412 /* Ordinarily return, but not if we want to output all
6413 and there are elements left. */
6414 if (!(all && next != 0))
6415 return;
6417 /* If it's not incremental, just skip over the gap, so that after
6418 jumping to retry we will output the next successive element. */
6419 if (TREE_CODE (constructor_type) == RECORD_TYPE
6420 || TREE_CODE (constructor_type) == UNION_TYPE)
6421 constructor_unfilled_fields = next;
6422 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6423 constructor_unfilled_index = next;
6425 /* ELT now points to the node in the pending tree with the next
6426 initializer to output. */
6427 goto retry;
6430 /* Add one non-braced element to the current constructor level.
6431 This adjusts the current position within the constructor's type.
6432 This may also start or terminate implicit levels
6433 to handle a partly-braced initializer.
6435 Once this has found the correct level for the new element,
6436 it calls output_init_element. */
6438 void
6439 process_init_element (struct c_expr value)
6441 tree orig_value = value.value;
6442 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6443 bool strict_string = value.original_code == STRING_CST;
6445 designator_depth = 0;
6446 designator_erroneous = 0;
6448 /* Handle superfluous braces around string cst as in
6449 char x[] = {"foo"}; */
6450 if (string_flag
6451 && constructor_type
6452 && TREE_CODE (constructor_type) == ARRAY_TYPE
6453 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6454 && integer_zerop (constructor_unfilled_index))
6456 if (constructor_stack->replacement_value.value)
6457 error_init ("excess elements in char array initializer");
6458 constructor_stack->replacement_value = value;
6459 return;
6462 if (constructor_stack->replacement_value.value != 0)
6464 error_init ("excess elements in struct initializer");
6465 return;
6468 /* Ignore elements of a brace group if it is entirely superfluous
6469 and has already been diagnosed. */
6470 if (constructor_type == 0)
6471 return;
6473 /* If we've exhausted any levels that didn't have braces,
6474 pop them now. */
6475 while (constructor_stack->implicit)
6477 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6478 || TREE_CODE (constructor_type) == UNION_TYPE)
6479 && constructor_fields == 0)
6480 process_init_element (pop_init_level (1));
6481 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6482 && (constructor_max_index == 0
6483 || tree_int_cst_lt (constructor_max_index,
6484 constructor_index)))
6485 process_init_element (pop_init_level (1));
6486 else
6487 break;
6490 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6491 if (constructor_range_stack)
6493 /* If value is a compound literal and we'll be just using its
6494 content, don't put it into a SAVE_EXPR. */
6495 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6496 || !require_constant_value
6497 || flag_isoc99)
6498 value.value = save_expr (value.value);
6501 while (1)
6503 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6505 tree fieldtype;
6506 enum tree_code fieldcode;
6508 if (constructor_fields == 0)
6510 pedwarn_init ("excess elements in struct initializer");
6511 break;
6514 fieldtype = TREE_TYPE (constructor_fields);
6515 if (fieldtype != error_mark_node)
6516 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6517 fieldcode = TREE_CODE (fieldtype);
6519 /* Error for non-static initialization of a flexible array member. */
6520 if (fieldcode == ARRAY_TYPE
6521 && !require_constant_value
6522 && TYPE_SIZE (fieldtype) == NULL_TREE
6523 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6525 error_init ("non-static initialization of a flexible array member");
6526 break;
6529 /* Accept a string constant to initialize a subarray. */
6530 if (value.value != 0
6531 && fieldcode == ARRAY_TYPE
6532 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6533 && string_flag)
6534 value.value = orig_value;
6535 /* Otherwise, if we have come to a subaggregate,
6536 and we don't have an element of its type, push into it. */
6537 else if (value.value != 0
6538 && value.value != error_mark_node
6539 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6540 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6541 || fieldcode == UNION_TYPE))
6543 push_init_level (1);
6544 continue;
6547 if (value.value)
6549 push_member_name (constructor_fields);
6550 output_init_element (value.value, strict_string,
6551 fieldtype, constructor_fields, 1);
6552 RESTORE_SPELLING_DEPTH (constructor_depth);
6554 else
6555 /* Do the bookkeeping for an element that was
6556 directly output as a constructor. */
6558 /* For a record, keep track of end position of last field. */
6559 if (DECL_SIZE (constructor_fields))
6560 constructor_bit_index
6561 = size_binop (PLUS_EXPR,
6562 bit_position (constructor_fields),
6563 DECL_SIZE (constructor_fields));
6565 /* If the current field was the first one not yet written out,
6566 it isn't now, so update. */
6567 if (constructor_unfilled_fields == constructor_fields)
6569 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6570 /* Skip any nameless bit fields. */
6571 while (constructor_unfilled_fields != 0
6572 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6573 && DECL_NAME (constructor_unfilled_fields) == 0)
6574 constructor_unfilled_fields =
6575 TREE_CHAIN (constructor_unfilled_fields);
6579 constructor_fields = TREE_CHAIN (constructor_fields);
6580 /* Skip any nameless bit fields at the beginning. */
6581 while (constructor_fields != 0
6582 && DECL_C_BIT_FIELD (constructor_fields)
6583 && DECL_NAME (constructor_fields) == 0)
6584 constructor_fields = TREE_CHAIN (constructor_fields);
6586 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6588 tree fieldtype;
6589 enum tree_code fieldcode;
6591 if (constructor_fields == 0)
6593 pedwarn_init ("excess elements in union initializer");
6594 break;
6597 fieldtype = TREE_TYPE (constructor_fields);
6598 if (fieldtype != error_mark_node)
6599 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6600 fieldcode = TREE_CODE (fieldtype);
6602 /* Warn that traditional C rejects initialization of unions.
6603 We skip the warning if the value is zero. This is done
6604 under the assumption that the zero initializer in user
6605 code appears conditioned on e.g. __STDC__ to avoid
6606 "missing initializer" warnings and relies on default
6607 initialization to zero in the traditional C case.
6608 We also skip the warning if the initializer is designated,
6609 again on the assumption that this must be conditional on
6610 __STDC__ anyway (and we've already complained about the
6611 member-designator already). */
6612 if (!in_system_header && !constructor_designated
6613 && !(value.value && (integer_zerop (value.value)
6614 || real_zerop (value.value))))
6615 warning (OPT_Wtraditional, "traditional C rejects initialization "
6616 "of unions");
6618 /* Accept a string constant to initialize a subarray. */
6619 if (value.value != 0
6620 && fieldcode == ARRAY_TYPE
6621 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6622 && string_flag)
6623 value.value = orig_value;
6624 /* Otherwise, if we have come to a subaggregate,
6625 and we don't have an element of its type, push into it. */
6626 else if (value.value != 0
6627 && value.value != error_mark_node
6628 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6629 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6630 || fieldcode == UNION_TYPE))
6632 push_init_level (1);
6633 continue;
6636 if (value.value)
6638 push_member_name (constructor_fields);
6639 output_init_element (value.value, strict_string,
6640 fieldtype, constructor_fields, 1);
6641 RESTORE_SPELLING_DEPTH (constructor_depth);
6643 else
6644 /* Do the bookkeeping for an element that was
6645 directly output as a constructor. */
6647 constructor_bit_index = DECL_SIZE (constructor_fields);
6648 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6651 constructor_fields = 0;
6653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6655 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6656 enum tree_code eltcode = TREE_CODE (elttype);
6658 /* Accept a string constant to initialize a subarray. */
6659 if (value.value != 0
6660 && eltcode == ARRAY_TYPE
6661 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6662 && string_flag)
6663 value.value = orig_value;
6664 /* Otherwise, if we have come to a subaggregate,
6665 and we don't have an element of its type, push into it. */
6666 else if (value.value != 0
6667 && value.value != error_mark_node
6668 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6669 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6670 || eltcode == UNION_TYPE))
6672 push_init_level (1);
6673 continue;
6676 if (constructor_max_index != 0
6677 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6678 || integer_all_onesp (constructor_max_index)))
6680 pedwarn_init ("excess elements in array initializer");
6681 break;
6684 /* Now output the actual element. */
6685 if (value.value)
6687 push_array_bounds (tree_low_cst (constructor_index, 1));
6688 output_init_element (value.value, strict_string,
6689 elttype, constructor_index, 1);
6690 RESTORE_SPELLING_DEPTH (constructor_depth);
6693 constructor_index
6694 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6696 if (!value.value)
6697 /* If we are doing the bookkeeping for an element that was
6698 directly output as a constructor, we must update
6699 constructor_unfilled_index. */
6700 constructor_unfilled_index = constructor_index;
6702 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6704 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6706 /* Do a basic check of initializer size. Note that vectors
6707 always have a fixed size derived from their type. */
6708 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6710 pedwarn_init ("excess elements in vector initializer");
6711 break;
6714 /* Now output the actual element. */
6715 if (value.value)
6716 output_init_element (value.value, strict_string,
6717 elttype, constructor_index, 1);
6719 constructor_index
6720 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6722 if (!value.value)
6723 /* If we are doing the bookkeeping for an element that was
6724 directly output as a constructor, we must update
6725 constructor_unfilled_index. */
6726 constructor_unfilled_index = constructor_index;
6729 /* Handle the sole element allowed in a braced initializer
6730 for a scalar variable. */
6731 else if (constructor_type != error_mark_node
6732 && constructor_fields == 0)
6734 pedwarn_init ("excess elements in scalar initializer");
6735 break;
6737 else
6739 if (value.value)
6740 output_init_element (value.value, strict_string,
6741 constructor_type, NULL_TREE, 1);
6742 constructor_fields = 0;
6745 /* Handle range initializers either at this level or anywhere higher
6746 in the designator stack. */
6747 if (constructor_range_stack)
6749 struct constructor_range_stack *p, *range_stack;
6750 int finish = 0;
6752 range_stack = constructor_range_stack;
6753 constructor_range_stack = 0;
6754 while (constructor_stack != range_stack->stack)
6756 gcc_assert (constructor_stack->implicit);
6757 process_init_element (pop_init_level (1));
6759 for (p = range_stack;
6760 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6761 p = p->prev)
6763 gcc_assert (constructor_stack->implicit);
6764 process_init_element (pop_init_level (1));
6767 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6768 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6769 finish = 1;
6771 while (1)
6773 constructor_index = p->index;
6774 constructor_fields = p->fields;
6775 if (finish && p->range_end && p->index == p->range_start)
6777 finish = 0;
6778 p->prev = 0;
6780 p = p->next;
6781 if (!p)
6782 break;
6783 push_init_level (2);
6784 p->stack = constructor_stack;
6785 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6786 p->index = p->range_start;
6789 if (!finish)
6790 constructor_range_stack = range_stack;
6791 continue;
6794 break;
6797 constructor_range_stack = 0;
6800 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6801 (guaranteed to be 'volatile' or null) and ARGS (represented using
6802 an ASM_EXPR node). */
6803 tree
6804 build_asm_stmt (tree cv_qualifier, tree args)
6806 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6807 ASM_VOLATILE_P (args) = 1;
6808 return add_stmt (args);
6811 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6812 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6813 SIMPLE indicates whether there was anything at all after the
6814 string in the asm expression -- asm("blah") and asm("blah" : )
6815 are subtly different. We use a ASM_EXPR node to represent this. */
6816 tree
6817 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6818 bool simple)
6820 tree tail;
6821 tree args;
6822 int i;
6823 const char *constraint;
6824 const char **oconstraints;
6825 bool allows_mem, allows_reg, is_inout;
6826 int ninputs, noutputs;
6828 ninputs = list_length (inputs);
6829 noutputs = list_length (outputs);
6830 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6832 string = resolve_asm_operand_names (string, outputs, inputs);
6834 /* Remove output conversions that change the type but not the mode. */
6835 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6837 tree output = TREE_VALUE (tail);
6839 /* ??? Really, this should not be here. Users should be using a
6840 proper lvalue, dammit. But there's a long history of using casts
6841 in the output operands. In cases like longlong.h, this becomes a
6842 primitive form of typechecking -- if the cast can be removed, then
6843 the output operand had a type of the proper width; otherwise we'll
6844 get an error. Gross, but ... */
6845 STRIP_NOPS (output);
6847 if (!lvalue_or_else (output, lv_asm))
6848 output = error_mark_node;
6850 if (output != error_mark_node
6851 && (TREE_READONLY (output)
6852 || TYPE_READONLY (TREE_TYPE (output))
6853 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6854 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6855 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6856 readonly_error (output, lv_asm);
6858 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6859 oconstraints[i] = constraint;
6861 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6862 &allows_mem, &allows_reg, &is_inout))
6864 /* If the operand is going to end up in memory,
6865 mark it addressable. */
6866 if (!allows_reg && !c_mark_addressable (output))
6867 output = error_mark_node;
6869 else
6870 output = error_mark_node;
6872 TREE_VALUE (tail) = output;
6875 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6877 tree input;
6879 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6880 input = TREE_VALUE (tail);
6882 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6883 oconstraints, &allows_mem, &allows_reg))
6885 /* If the operand is going to end up in memory,
6886 mark it addressable. */
6887 if (!allows_reg && allows_mem)
6889 /* Strip the nops as we allow this case. FIXME, this really
6890 should be rejected or made deprecated. */
6891 STRIP_NOPS (input);
6892 if (!c_mark_addressable (input))
6893 input = error_mark_node;
6896 else
6897 input = error_mark_node;
6899 TREE_VALUE (tail) = input;
6902 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6904 /* asm statements without outputs, including simple ones, are treated
6905 as volatile. */
6906 ASM_INPUT_P (args) = simple;
6907 ASM_VOLATILE_P (args) = (noutputs == 0);
6909 return args;
6912 /* Generate a goto statement to LABEL. */
6914 tree
6915 c_finish_goto_label (tree label)
6917 tree decl = lookup_label (label);
6918 if (!decl)
6919 return NULL_TREE;
6921 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6923 error ("jump into statement expression");
6924 return NULL_TREE;
6927 if (C_DECL_UNJUMPABLE_VM (decl))
6929 error ("jump into scope of identifier with variably modified type");
6930 return NULL_TREE;
6933 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6935 /* No jump from outside this statement expression context, so
6936 record that there is a jump from within this context. */
6937 struct c_label_list *nlist;
6938 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6939 nlist->next = label_context_stack_se->labels_used;
6940 nlist->label = decl;
6941 label_context_stack_se->labels_used = nlist;
6944 if (!C_DECL_UNDEFINABLE_VM (decl))
6946 /* No jump from outside this context context of identifiers with
6947 variably modified type, so record that there is a jump from
6948 within this context. */
6949 struct c_label_list *nlist;
6950 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6951 nlist->next = label_context_stack_vm->labels_used;
6952 nlist->label = decl;
6953 label_context_stack_vm->labels_used = nlist;
6956 TREE_USED (decl) = 1;
6957 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6960 /* Generate a computed goto statement to EXPR. */
6962 tree
6963 c_finish_goto_ptr (tree expr)
6965 if (pedantic)
6966 pedwarn ("ISO C forbids %<goto *expr;%>");
6967 expr = convert (ptr_type_node, expr);
6968 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6971 /* Generate a C `return' statement. RETVAL is the expression for what
6972 to return, or a null pointer for `return;' with no value. */
6974 tree
6975 c_finish_return (tree retval)
6977 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6978 bool no_warning = false;
6980 if (TREE_THIS_VOLATILE (current_function_decl))
6981 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6983 if (!retval)
6985 current_function_returns_null = 1;
6986 if ((warn_return_type || flag_isoc99)
6987 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6989 pedwarn_c99 ("%<return%> with no value, in "
6990 "function returning non-void");
6991 no_warning = true;
6994 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6996 current_function_returns_null = 1;
6997 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6998 pedwarn ("%<return%> with a value, in function returning void");
7000 else
7002 tree t = convert_for_assignment (valtype, retval, ic_return,
7003 NULL_TREE, NULL_TREE, 0);
7004 tree res = DECL_RESULT (current_function_decl);
7005 tree inner;
7007 current_function_returns_value = 1;
7008 if (t == error_mark_node)
7009 return NULL_TREE;
7011 inner = t = convert (TREE_TYPE (res), t);
7013 /* Strip any conversions, additions, and subtractions, and see if
7014 we are returning the address of a local variable. Warn if so. */
7015 while (1)
7017 switch (TREE_CODE (inner))
7019 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7020 case PLUS_EXPR:
7021 inner = TREE_OPERAND (inner, 0);
7022 continue;
7024 case MINUS_EXPR:
7025 /* If the second operand of the MINUS_EXPR has a pointer
7026 type (or is converted from it), this may be valid, so
7027 don't give a warning. */
7029 tree op1 = TREE_OPERAND (inner, 1);
7031 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7032 && (TREE_CODE (op1) == NOP_EXPR
7033 || TREE_CODE (op1) == NON_LVALUE_EXPR
7034 || TREE_CODE (op1) == CONVERT_EXPR))
7035 op1 = TREE_OPERAND (op1, 0);
7037 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7038 break;
7040 inner = TREE_OPERAND (inner, 0);
7041 continue;
7044 case ADDR_EXPR:
7045 inner = TREE_OPERAND (inner, 0);
7047 while (REFERENCE_CLASS_P (inner)
7048 && TREE_CODE (inner) != INDIRECT_REF)
7049 inner = TREE_OPERAND (inner, 0);
7051 if (DECL_P (inner)
7052 && !DECL_EXTERNAL (inner)
7053 && !TREE_STATIC (inner)
7054 && DECL_CONTEXT (inner) == current_function_decl)
7055 warning (0, "function returns address of local variable");
7056 break;
7058 default:
7059 break;
7062 break;
7065 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7068 ret_stmt = build_stmt (RETURN_EXPR, retval);
7069 TREE_NO_WARNING (ret_stmt) |= no_warning;
7070 return add_stmt (ret_stmt);
7073 struct c_switch {
7074 /* The SWITCH_EXPR being built. */
7075 tree switch_expr;
7077 /* The original type of the testing expression, i.e. before the
7078 default conversion is applied. */
7079 tree orig_type;
7081 /* A splay-tree mapping the low element of a case range to the high
7082 element, or NULL_TREE if there is no high element. Used to
7083 determine whether or not a new case label duplicates an old case
7084 label. We need a tree, rather than simply a hash table, because
7085 of the GNU case range extension. */
7086 splay_tree cases;
7088 /* Number of nested statement expressions within this switch
7089 statement; if nonzero, case and default labels may not
7090 appear. */
7091 unsigned int blocked_stmt_expr;
7093 /* Scope of outermost declarations of identifiers with variably
7094 modified type within this switch statement; if nonzero, case and
7095 default labels may not appear. */
7096 unsigned int blocked_vm;
7098 /* The next node on the stack. */
7099 struct c_switch *next;
7102 /* A stack of the currently active switch statements. The innermost
7103 switch statement is on the top of the stack. There is no need to
7104 mark the stack for garbage collection because it is only active
7105 during the processing of the body of a function, and we never
7106 collect at that point. */
7108 struct c_switch *c_switch_stack;
7110 /* Start a C switch statement, testing expression EXP. Return the new
7111 SWITCH_EXPR. */
7113 tree
7114 c_start_case (tree exp)
7116 tree orig_type = error_mark_node;
7117 struct c_switch *cs;
7119 if (exp != error_mark_node)
7121 orig_type = TREE_TYPE (exp);
7123 if (!INTEGRAL_TYPE_P (orig_type))
7125 if (orig_type != error_mark_node)
7127 error ("switch quantity not an integer");
7128 orig_type = error_mark_node;
7130 exp = integer_zero_node;
7132 else
7134 tree type = TYPE_MAIN_VARIANT (orig_type);
7136 if (!in_system_header
7137 && (type == long_integer_type_node
7138 || type == long_unsigned_type_node))
7139 warning (OPT_Wtraditional, "%<long%> switch expression not "
7140 "converted to %<int%> in ISO C");
7142 exp = default_conversion (exp);
7146 /* Add this new SWITCH_EXPR to the stack. */
7147 cs = XNEW (struct c_switch);
7148 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7149 cs->orig_type = orig_type;
7150 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7151 cs->blocked_stmt_expr = 0;
7152 cs->blocked_vm = 0;
7153 cs->next = c_switch_stack;
7154 c_switch_stack = cs;
7156 return add_stmt (cs->switch_expr);
7159 /* Process a case label. */
7161 tree
7162 do_case (tree low_value, tree high_value)
7164 tree label = NULL_TREE;
7166 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7167 && !c_switch_stack->blocked_vm)
7169 label = c_add_case_label (c_switch_stack->cases,
7170 SWITCH_COND (c_switch_stack->switch_expr),
7171 c_switch_stack->orig_type,
7172 low_value, high_value);
7173 if (label == error_mark_node)
7174 label = NULL_TREE;
7176 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7178 if (low_value)
7179 error ("case label in statement expression not containing "
7180 "enclosing switch statement");
7181 else
7182 error ("%<default%> label in statement expression not containing "
7183 "enclosing switch statement");
7185 else if (c_switch_stack && c_switch_stack->blocked_vm)
7187 if (low_value)
7188 error ("case label in scope of identifier with variably modified "
7189 "type not containing enclosing switch statement");
7190 else
7191 error ("%<default%> label in scope of identifier with variably "
7192 "modified type not containing enclosing switch statement");
7194 else if (low_value)
7195 error ("case label not within a switch statement");
7196 else
7197 error ("%<default%> label not within a switch statement");
7199 return label;
7202 /* Finish the switch statement. */
7204 void
7205 c_finish_case (tree body)
7207 struct c_switch *cs = c_switch_stack;
7208 location_t switch_location;
7210 SWITCH_BODY (cs->switch_expr) = body;
7212 /* We must not be within a statement expression nested in the switch
7213 at this point; we might, however, be within the scope of an
7214 identifier with variably modified type nested in the switch. */
7215 gcc_assert (!cs->blocked_stmt_expr);
7217 /* Emit warnings as needed. */
7218 if (EXPR_HAS_LOCATION (cs->switch_expr))
7219 switch_location = EXPR_LOCATION (cs->switch_expr);
7220 else
7221 switch_location = input_location;
7222 c_do_switch_warnings (cs->cases, switch_location,
7223 TREE_TYPE (cs->switch_expr),
7224 SWITCH_COND (cs->switch_expr));
7226 /* Pop the stack. */
7227 c_switch_stack = cs->next;
7228 splay_tree_delete (cs->cases);
7229 XDELETE (cs);
7232 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7233 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7234 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7235 statement, and was not surrounded with parenthesis. */
7237 void
7238 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7239 tree else_block, bool nested_if)
7241 tree stmt;
7243 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7244 if (warn_parentheses && nested_if && else_block == NULL)
7246 tree inner_if = then_block;
7248 /* We know from the grammar productions that there is an IF nested
7249 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7250 it might not be exactly THEN_BLOCK, but should be the last
7251 non-container statement within. */
7252 while (1)
7253 switch (TREE_CODE (inner_if))
7255 case COND_EXPR:
7256 goto found;
7257 case BIND_EXPR:
7258 inner_if = BIND_EXPR_BODY (inner_if);
7259 break;
7260 case STATEMENT_LIST:
7261 inner_if = expr_last (then_block);
7262 break;
7263 case TRY_FINALLY_EXPR:
7264 case TRY_CATCH_EXPR:
7265 inner_if = TREE_OPERAND (inner_if, 0);
7266 break;
7267 default:
7268 gcc_unreachable ();
7270 found:
7272 if (COND_EXPR_ELSE (inner_if))
7273 warning (OPT_Wparentheses,
7274 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7275 &if_locus);
7278 empty_body_warning (then_block, else_block);
7280 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7281 SET_EXPR_LOCATION (stmt, if_locus);
7282 add_stmt (stmt);
7285 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7286 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7287 is false for DO loops. INCR is the FOR increment expression. BODY is
7288 the statement controlled by the loop. BLAB is the break label. CLAB is
7289 the continue label. Everything is allowed to be NULL. */
7291 void
7292 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7293 tree blab, tree clab, bool cond_is_first)
7295 tree entry = NULL, exit = NULL, t;
7297 /* If the condition is zero don't generate a loop construct. */
7298 if (cond && integer_zerop (cond))
7300 if (cond_is_first)
7302 t = build_and_jump (&blab);
7303 SET_EXPR_LOCATION (t, start_locus);
7304 add_stmt (t);
7307 else
7309 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7311 /* If we have an exit condition, then we build an IF with gotos either
7312 out of the loop, or to the top of it. If there's no exit condition,
7313 then we just build a jump back to the top. */
7314 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7316 if (cond && !integer_nonzerop (cond))
7318 /* Canonicalize the loop condition to the end. This means
7319 generating a branch to the loop condition. Reuse the
7320 continue label, if possible. */
7321 if (cond_is_first)
7323 if (incr || !clab)
7325 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7326 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7328 else
7329 t = build1 (GOTO_EXPR, void_type_node, clab);
7330 SET_EXPR_LOCATION (t, start_locus);
7331 add_stmt (t);
7334 t = build_and_jump (&blab);
7335 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7336 if (cond_is_first)
7337 SET_EXPR_LOCATION (exit, start_locus);
7338 else
7339 SET_EXPR_LOCATION (exit, input_location);
7342 add_stmt (top);
7345 if (body)
7346 add_stmt (body);
7347 if (clab)
7348 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7349 if (incr)
7350 add_stmt (incr);
7351 if (entry)
7352 add_stmt (entry);
7353 if (exit)
7354 add_stmt (exit);
7355 if (blab)
7356 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7359 tree
7360 c_finish_bc_stmt (tree *label_p, bool is_break)
7362 bool skip;
7363 tree label = *label_p;
7365 /* In switch statements break is sometimes stylistically used after
7366 a return statement. This can lead to spurious warnings about
7367 control reaching the end of a non-void function when it is
7368 inlined. Note that we are calling block_may_fallthru with
7369 language specific tree nodes; this works because
7370 block_may_fallthru returns true when given something it does not
7371 understand. */
7372 skip = !block_may_fallthru (cur_stmt_list);
7374 if (!label)
7376 if (!skip)
7377 *label_p = label = create_artificial_label ();
7379 else if (TREE_CODE (label) == LABEL_DECL)
7381 else switch (TREE_INT_CST_LOW (label))
7383 case 0:
7384 if (is_break)
7385 error ("break statement not within loop or switch");
7386 else
7387 error ("continue statement not within a loop");
7388 return NULL_TREE;
7390 case 1:
7391 gcc_assert (is_break);
7392 error ("break statement used with OpenMP for loop");
7393 return NULL_TREE;
7395 default:
7396 gcc_unreachable ();
7399 if (skip)
7400 return NULL_TREE;
7402 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7405 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7407 static void
7408 emit_side_effect_warnings (tree expr)
7410 if (expr == error_mark_node)
7412 else if (!TREE_SIDE_EFFECTS (expr))
7414 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7415 warning (0, "%Hstatement with no effect",
7416 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7418 else if (warn_unused_value)
7419 warn_if_unused_value (expr, input_location);
7422 /* Process an expression as if it were a complete statement. Emit
7423 diagnostics, but do not call ADD_STMT. */
7425 tree
7426 c_process_expr_stmt (tree expr)
7428 if (!expr)
7429 return NULL_TREE;
7431 if (warn_sequence_point)
7432 verify_sequence_points (expr);
7434 if (TREE_TYPE (expr) != error_mark_node
7435 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7436 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7437 error ("expression statement has incomplete type");
7439 /* If we're not processing a statement expression, warn about unused values.
7440 Warnings for statement expressions will be emitted later, once we figure
7441 out which is the result. */
7442 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7443 && (extra_warnings || warn_unused_value))
7444 emit_side_effect_warnings (expr);
7446 /* If the expression is not of a type to which we cannot assign a line
7447 number, wrap the thing in a no-op NOP_EXPR. */
7448 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7449 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7451 if (EXPR_P (expr))
7452 SET_EXPR_LOCATION (expr, input_location);
7454 return expr;
7457 /* Emit an expression as a statement. */
7459 tree
7460 c_finish_expr_stmt (tree expr)
7462 if (expr)
7463 return add_stmt (c_process_expr_stmt (expr));
7464 else
7465 return NULL;
7468 /* Do the opposite and emit a statement as an expression. To begin,
7469 create a new binding level and return it. */
7471 tree
7472 c_begin_stmt_expr (void)
7474 tree ret;
7475 struct c_label_context_se *nstack;
7476 struct c_label_list *glist;
7478 /* We must force a BLOCK for this level so that, if it is not expanded
7479 later, there is a way to turn off the entire subtree of blocks that
7480 are contained in it. */
7481 keep_next_level ();
7482 ret = c_begin_compound_stmt (true);
7483 if (c_switch_stack)
7485 c_switch_stack->blocked_stmt_expr++;
7486 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7488 for (glist = label_context_stack_se->labels_used;
7489 glist != NULL;
7490 glist = glist->next)
7492 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7494 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7495 nstack->labels_def = NULL;
7496 nstack->labels_used = NULL;
7497 nstack->next = label_context_stack_se;
7498 label_context_stack_se = nstack;
7500 /* Mark the current statement list as belonging to a statement list. */
7501 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7503 return ret;
7506 tree
7507 c_finish_stmt_expr (tree body)
7509 tree last, type, tmp, val;
7510 tree *last_p;
7511 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7513 body = c_end_compound_stmt (body, true);
7514 if (c_switch_stack)
7516 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7517 c_switch_stack->blocked_stmt_expr--;
7519 /* It is no longer possible to jump to labels defined within this
7520 statement expression. */
7521 for (dlist = label_context_stack_se->labels_def;
7522 dlist != NULL;
7523 dlist = dlist->next)
7525 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7527 /* It is again possible to define labels with a goto just outside
7528 this statement expression. */
7529 for (glist = label_context_stack_se->next->labels_used;
7530 glist != NULL;
7531 glist = glist->next)
7533 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7534 glist_prev = glist;
7536 if (glist_prev != NULL)
7537 glist_prev->next = label_context_stack_se->labels_used;
7538 else
7539 label_context_stack_se->next->labels_used
7540 = label_context_stack_se->labels_used;
7541 label_context_stack_se = label_context_stack_se->next;
7543 /* Locate the last statement in BODY. See c_end_compound_stmt
7544 about always returning a BIND_EXPR. */
7545 last_p = &BIND_EXPR_BODY (body);
7546 last = BIND_EXPR_BODY (body);
7548 continue_searching:
7549 if (TREE_CODE (last) == STATEMENT_LIST)
7551 tree_stmt_iterator i;
7553 /* This can happen with degenerate cases like ({ }). No value. */
7554 if (!TREE_SIDE_EFFECTS (last))
7555 return body;
7557 /* If we're supposed to generate side effects warnings, process
7558 all of the statements except the last. */
7559 if (extra_warnings || warn_unused_value)
7561 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7562 emit_side_effect_warnings (tsi_stmt (i));
7564 else
7565 i = tsi_last (last);
7566 last_p = tsi_stmt_ptr (i);
7567 last = *last_p;
7570 /* If the end of the list is exception related, then the list was split
7571 by a call to push_cleanup. Continue searching. */
7572 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7573 || TREE_CODE (last) == TRY_CATCH_EXPR)
7575 last_p = &TREE_OPERAND (last, 0);
7576 last = *last_p;
7577 goto continue_searching;
7580 /* In the case that the BIND_EXPR is not necessary, return the
7581 expression out from inside it. */
7582 if (last == error_mark_node
7583 || (last == BIND_EXPR_BODY (body)
7584 && BIND_EXPR_VARS (body) == NULL))
7586 /* Do not warn if the return value of a statement expression is
7587 unused. */
7588 if (EXPR_P (last))
7589 TREE_NO_WARNING (last) = 1;
7590 return last;
7593 /* Extract the type of said expression. */
7594 type = TREE_TYPE (last);
7596 /* If we're not returning a value at all, then the BIND_EXPR that
7597 we already have is a fine expression to return. */
7598 if (!type || VOID_TYPE_P (type))
7599 return body;
7601 /* Now that we've located the expression containing the value, it seems
7602 silly to make voidify_wrapper_expr repeat the process. Create a
7603 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7604 tmp = create_tmp_var_raw (type, NULL);
7606 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7607 tree_expr_nonnegative_p giving up immediately. */
7608 val = last;
7609 if (TREE_CODE (val) == NOP_EXPR
7610 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7611 val = TREE_OPERAND (val, 0);
7613 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7614 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7616 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7619 /* Begin the scope of an identifier of variably modified type, scope
7620 number SCOPE. Jumping from outside this scope to inside it is not
7621 permitted. */
7623 void
7624 c_begin_vm_scope (unsigned int scope)
7626 struct c_label_context_vm *nstack;
7627 struct c_label_list *glist;
7629 gcc_assert (scope > 0);
7631 /* At file_scope, we don't have to do any processing. */
7632 if (label_context_stack_vm == NULL)
7633 return;
7635 if (c_switch_stack && !c_switch_stack->blocked_vm)
7636 c_switch_stack->blocked_vm = scope;
7637 for (glist = label_context_stack_vm->labels_used;
7638 glist != NULL;
7639 glist = glist->next)
7641 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7643 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7644 nstack->labels_def = NULL;
7645 nstack->labels_used = NULL;
7646 nstack->scope = scope;
7647 nstack->next = label_context_stack_vm;
7648 label_context_stack_vm = nstack;
7651 /* End a scope which may contain identifiers of variably modified
7652 type, scope number SCOPE. */
7654 void
7655 c_end_vm_scope (unsigned int scope)
7657 if (label_context_stack_vm == NULL)
7658 return;
7659 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7660 c_switch_stack->blocked_vm = 0;
7661 /* We may have a number of nested scopes of identifiers with
7662 variably modified type, all at this depth. Pop each in turn. */
7663 while (label_context_stack_vm->scope == scope)
7665 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7667 /* It is no longer possible to jump to labels defined within this
7668 scope. */
7669 for (dlist = label_context_stack_vm->labels_def;
7670 dlist != NULL;
7671 dlist = dlist->next)
7673 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7675 /* It is again possible to define labels with a goto just outside
7676 this scope. */
7677 for (glist = label_context_stack_vm->next->labels_used;
7678 glist != NULL;
7679 glist = glist->next)
7681 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7682 glist_prev = glist;
7684 if (glist_prev != NULL)
7685 glist_prev->next = label_context_stack_vm->labels_used;
7686 else
7687 label_context_stack_vm->next->labels_used
7688 = label_context_stack_vm->labels_used;
7689 label_context_stack_vm = label_context_stack_vm->next;
7693 /* Begin and end compound statements. This is as simple as pushing
7694 and popping new statement lists from the tree. */
7696 tree
7697 c_begin_compound_stmt (bool do_scope)
7699 tree stmt = push_stmt_list ();
7700 if (do_scope)
7701 push_scope ();
7702 return stmt;
7705 tree
7706 c_end_compound_stmt (tree stmt, bool do_scope)
7708 tree block = NULL;
7710 if (do_scope)
7712 if (c_dialect_objc ())
7713 objc_clear_super_receiver ();
7714 block = pop_scope ();
7717 stmt = pop_stmt_list (stmt);
7718 stmt = c_build_bind_expr (block, stmt);
7720 /* If this compound statement is nested immediately inside a statement
7721 expression, then force a BIND_EXPR to be created. Otherwise we'll
7722 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7723 STATEMENT_LISTs merge, and thus we can lose track of what statement
7724 was really last. */
7725 if (cur_stmt_list
7726 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7727 && TREE_CODE (stmt) != BIND_EXPR)
7729 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7730 TREE_SIDE_EFFECTS (stmt) = 1;
7733 return stmt;
7736 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7737 when the current scope is exited. EH_ONLY is true when this is not
7738 meant to apply to normal control flow transfer. */
7740 void
7741 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7743 enum tree_code code;
7744 tree stmt, list;
7745 bool stmt_expr;
7747 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7748 stmt = build_stmt (code, NULL, cleanup);
7749 add_stmt (stmt);
7750 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7751 list = push_stmt_list ();
7752 TREE_OPERAND (stmt, 0) = list;
7753 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7756 /* Build a binary-operation expression without default conversions.
7757 CODE is the kind of expression to build.
7758 This function differs from `build' in several ways:
7759 the data type of the result is computed and recorded in it,
7760 warnings are generated if arg data types are invalid,
7761 special handling for addition and subtraction of pointers is known,
7762 and some optimization is done (operations on narrow ints
7763 are done in the narrower type when that gives the same result).
7764 Constant folding is also done before the result is returned.
7766 Note that the operands will never have enumeral types, or function
7767 or array types, because either they will have the default conversions
7768 performed or they have both just been converted to some other type in which
7769 the arithmetic is to be done. */
7771 tree
7772 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7773 int convert_p)
7775 tree type0, type1;
7776 enum tree_code code0, code1;
7777 tree op0, op1;
7778 const char *invalid_op_diag;
7780 /* Expression code to give to the expression when it is built.
7781 Normally this is CODE, which is what the caller asked for,
7782 but in some special cases we change it. */
7783 enum tree_code resultcode = code;
7785 /* Data type in which the computation is to be performed.
7786 In the simplest cases this is the common type of the arguments. */
7787 tree result_type = NULL;
7789 /* Nonzero means operands have already been type-converted
7790 in whatever way is necessary.
7791 Zero means they need to be converted to RESULT_TYPE. */
7792 int converted = 0;
7794 /* Nonzero means create the expression with this type, rather than
7795 RESULT_TYPE. */
7796 tree build_type = 0;
7798 /* Nonzero means after finally constructing the expression
7799 convert it to this type. */
7800 tree final_type = 0;
7802 /* Nonzero if this is an operation like MIN or MAX which can
7803 safely be computed in short if both args are promoted shorts.
7804 Also implies COMMON.
7805 -1 indicates a bitwise operation; this makes a difference
7806 in the exact conditions for when it is safe to do the operation
7807 in a narrower mode. */
7808 int shorten = 0;
7810 /* Nonzero if this is a comparison operation;
7811 if both args are promoted shorts, compare the original shorts.
7812 Also implies COMMON. */
7813 int short_compare = 0;
7815 /* Nonzero if this is a right-shift operation, which can be computed on the
7816 original short and then promoted if the operand is a promoted short. */
7817 int short_shift = 0;
7819 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7820 int common = 0;
7822 /* True means types are compatible as far as ObjC is concerned. */
7823 bool objc_ok;
7825 if (convert_p)
7827 op0 = default_conversion (orig_op0);
7828 op1 = default_conversion (orig_op1);
7830 else
7832 op0 = orig_op0;
7833 op1 = orig_op1;
7836 type0 = TREE_TYPE (op0);
7837 type1 = TREE_TYPE (op1);
7839 /* The expression codes of the data types of the arguments tell us
7840 whether the arguments are integers, floating, pointers, etc. */
7841 code0 = TREE_CODE (type0);
7842 code1 = TREE_CODE (type1);
7844 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7845 STRIP_TYPE_NOPS (op0);
7846 STRIP_TYPE_NOPS (op1);
7848 /* If an error was already reported for one of the arguments,
7849 avoid reporting another error. */
7851 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7852 return error_mark_node;
7854 if ((invalid_op_diag
7855 = targetm.invalid_binary_op (code, type0, type1)))
7857 error (invalid_op_diag);
7858 return error_mark_node;
7861 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7863 switch (code)
7865 case PLUS_EXPR:
7866 /* Handle the pointer + int case. */
7867 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7868 return pointer_int_sum (PLUS_EXPR, op0, op1);
7869 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7870 return pointer_int_sum (PLUS_EXPR, op1, op0);
7871 else
7872 common = 1;
7873 break;
7875 case MINUS_EXPR:
7876 /* Subtraction of two similar pointers.
7877 We must subtract them as integers, then divide by object size. */
7878 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7879 && comp_target_types (type0, type1))
7880 return pointer_diff (op0, op1);
7881 /* Handle pointer minus int. Just like pointer plus int. */
7882 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7883 return pointer_int_sum (MINUS_EXPR, op0, op1);
7884 else
7885 common = 1;
7886 break;
7888 case MULT_EXPR:
7889 common = 1;
7890 break;
7892 case TRUNC_DIV_EXPR:
7893 case CEIL_DIV_EXPR:
7894 case FLOOR_DIV_EXPR:
7895 case ROUND_DIV_EXPR:
7896 case EXACT_DIV_EXPR:
7897 /* Floating point division by zero is a legitimate way to obtain
7898 infinities and NaNs. */
7899 if (skip_evaluation == 0 && integer_zerop (op1))
7900 warning (OPT_Wdiv_by_zero, "division by zero");
7902 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7903 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7904 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7905 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7907 enum tree_code tcode0 = code0, tcode1 = code1;
7909 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7910 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7911 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7912 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7914 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7915 resultcode = RDIV_EXPR;
7916 else
7917 /* Although it would be tempting to shorten always here, that
7918 loses on some targets, since the modulo instruction is
7919 undefined if the quotient can't be represented in the
7920 computation mode. We shorten only if unsigned or if
7921 dividing by something we know != -1. */
7922 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7923 || (TREE_CODE (op1) == INTEGER_CST
7924 && !integer_all_onesp (op1)));
7925 common = 1;
7927 break;
7929 case BIT_AND_EXPR:
7930 case BIT_IOR_EXPR:
7931 case BIT_XOR_EXPR:
7932 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7933 shorten = -1;
7934 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7935 common = 1;
7936 break;
7938 case TRUNC_MOD_EXPR:
7939 case FLOOR_MOD_EXPR:
7940 if (skip_evaluation == 0 && integer_zerop (op1))
7941 warning (OPT_Wdiv_by_zero, "division by zero");
7943 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7945 /* Although it would be tempting to shorten always here, that loses
7946 on some targets, since the modulo instruction is undefined if the
7947 quotient can't be represented in the computation mode. We shorten
7948 only if unsigned or if dividing by something we know != -1. */
7949 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7950 || (TREE_CODE (op1) == INTEGER_CST
7951 && !integer_all_onesp (op1)));
7952 common = 1;
7954 break;
7956 case TRUTH_ANDIF_EXPR:
7957 case TRUTH_ORIF_EXPR:
7958 case TRUTH_AND_EXPR:
7959 case TRUTH_OR_EXPR:
7960 case TRUTH_XOR_EXPR:
7961 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7962 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7963 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7964 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7966 /* Result of these operations is always an int,
7967 but that does not mean the operands should be
7968 converted to ints! */
7969 result_type = integer_type_node;
7970 op0 = c_common_truthvalue_conversion (op0);
7971 op1 = c_common_truthvalue_conversion (op1);
7972 converted = 1;
7974 break;
7976 /* Shift operations: result has same type as first operand;
7977 always convert second operand to int.
7978 Also set SHORT_SHIFT if shifting rightward. */
7980 case RSHIFT_EXPR:
7981 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7983 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7985 if (tree_int_cst_sgn (op1) < 0)
7986 warning (0, "right shift count is negative");
7987 else
7989 if (!integer_zerop (op1))
7990 short_shift = 1;
7992 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7993 warning (0, "right shift count >= width of type");
7997 /* Use the type of the value to be shifted. */
7998 result_type = type0;
7999 /* Convert the shift-count to an integer, regardless of size
8000 of value being shifted. */
8001 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8002 op1 = convert (integer_type_node, op1);
8003 /* Avoid converting op1 to result_type later. */
8004 converted = 1;
8006 break;
8008 case LSHIFT_EXPR:
8009 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8011 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8013 if (tree_int_cst_sgn (op1) < 0)
8014 warning (0, "left shift count is negative");
8016 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8017 warning (0, "left shift count >= width of type");
8020 /* Use the type of the value to be shifted. */
8021 result_type = type0;
8022 /* Convert the shift-count to an integer, regardless of size
8023 of value being shifted. */
8024 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8025 op1 = convert (integer_type_node, op1);
8026 /* Avoid converting op1 to result_type later. */
8027 converted = 1;
8029 break;
8031 case EQ_EXPR:
8032 case NE_EXPR:
8033 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8034 warning (OPT_Wfloat_equal,
8035 "comparing floating point with == or != is unsafe");
8036 /* Result of comparison is always int,
8037 but don't convert the args to int! */
8038 build_type = integer_type_node;
8039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8040 || code0 == COMPLEX_TYPE)
8041 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8042 || code1 == COMPLEX_TYPE))
8043 short_compare = 1;
8044 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8046 tree tt0 = TREE_TYPE (type0);
8047 tree tt1 = TREE_TYPE (type1);
8048 /* Anything compares with void *. void * compares with anything.
8049 Otherwise, the targets must be compatible
8050 and both must be object or both incomplete. */
8051 if (comp_target_types (type0, type1))
8052 result_type = common_pointer_type (type0, type1);
8053 else if (VOID_TYPE_P (tt0))
8055 /* op0 != orig_op0 detects the case of something
8056 whose value is 0 but which isn't a valid null ptr const. */
8057 if (pedantic && !null_pointer_constant_p (orig_op0)
8058 && TREE_CODE (tt1) == FUNCTION_TYPE)
8059 pedwarn ("ISO C forbids comparison of %<void *%>"
8060 " with function pointer");
8062 else if (VOID_TYPE_P (tt1))
8064 if (pedantic && !null_pointer_constant_p (orig_op1)
8065 && TREE_CODE (tt0) == FUNCTION_TYPE)
8066 pedwarn ("ISO C forbids comparison of %<void *%>"
8067 " with function pointer");
8069 else
8070 /* Avoid warning about the volatile ObjC EH puts on decls. */
8071 if (!objc_ok)
8072 pedwarn ("comparison of distinct pointer types lacks a cast");
8074 if (result_type == NULL_TREE)
8075 result_type = ptr_type_node;
8077 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8079 if (TREE_CODE (op0) == ADDR_EXPR
8080 && DECL_P (TREE_OPERAND (op0, 0))
8081 && (TREE_CODE (TREE_OPERAND (op0, 0)) == PARM_DECL
8082 || TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
8083 || !DECL_WEAK (TREE_OPERAND (op0, 0))))
8084 warning (OPT_Waddress, "the address of %qD will never be NULL",
8085 TREE_OPERAND (op0, 0));
8086 result_type = type0;
8088 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8090 if (TREE_CODE (op1) == ADDR_EXPR
8091 && DECL_P (TREE_OPERAND (op1, 0))
8092 && (TREE_CODE (TREE_OPERAND (op1, 0)) == PARM_DECL
8093 || TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL
8094 || !DECL_WEAK (TREE_OPERAND (op1, 0))))
8095 warning (OPT_Waddress, "the address of %qD will never be NULL",
8096 TREE_OPERAND (op1, 0));
8097 result_type = type1;
8099 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8101 result_type = type0;
8102 pedwarn ("comparison between pointer and integer");
8104 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8106 result_type = type1;
8107 pedwarn ("comparison between pointer and integer");
8109 break;
8111 case LE_EXPR:
8112 case GE_EXPR:
8113 case LT_EXPR:
8114 case GT_EXPR:
8115 build_type = integer_type_node;
8116 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
8117 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
8118 short_compare = 1;
8119 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8121 if (comp_target_types (type0, type1))
8123 result_type = common_pointer_type (type0, type1);
8124 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8125 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8126 pedwarn ("comparison of complete and incomplete pointers");
8127 else if (pedantic
8128 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8129 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8131 else
8133 result_type = ptr_type_node;
8134 pedwarn ("comparison of distinct pointer types lacks a cast");
8137 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8139 result_type = type0;
8140 if (pedantic || extra_warnings)
8141 pedwarn ("ordered comparison of pointer with integer zero");
8143 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8145 result_type = type1;
8146 if (pedantic)
8147 pedwarn ("ordered comparison of pointer with integer zero");
8149 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8151 result_type = type0;
8152 pedwarn ("comparison between pointer and integer");
8154 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8156 result_type = type1;
8157 pedwarn ("comparison between pointer and integer");
8159 break;
8161 default:
8162 gcc_unreachable ();
8165 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8166 return error_mark_node;
8168 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8169 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8170 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8171 TREE_TYPE (type1))))
8173 binary_op_error (code);
8174 return error_mark_node;
8177 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8178 || code0 == VECTOR_TYPE)
8180 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8181 || code1 == VECTOR_TYPE))
8183 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8185 if (shorten || common || short_compare)
8186 result_type = c_common_type (type0, type1);
8188 /* For certain operations (which identify themselves by shorten != 0)
8189 if both args were extended from the same smaller type,
8190 do the arithmetic in that type and then extend.
8192 shorten !=0 and !=1 indicates a bitwise operation.
8193 For them, this optimization is safe only if
8194 both args are zero-extended or both are sign-extended.
8195 Otherwise, we might change the result.
8196 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8197 but calculated in (unsigned short) it would be (unsigned short)-1. */
8199 if (shorten && none_complex)
8201 int unsigned0, unsigned1;
8202 tree arg0, arg1;
8203 int uns;
8204 tree type;
8206 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8207 excessive narrowing when we call get_narrower below. For
8208 example, suppose that OP0 is of unsigned int extended
8209 from signed char and that RESULT_TYPE is long long int.
8210 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8211 like
8213 (long long int) (unsigned int) signed_char
8215 which get_narrower would narrow down to
8217 (unsigned int) signed char
8219 If we do not cast OP0 first, get_narrower would return
8220 signed_char, which is inconsistent with the case of the
8221 explicit cast. */
8222 op0 = convert (result_type, op0);
8223 op1 = convert (result_type, op1);
8225 arg0 = get_narrower (op0, &unsigned0);
8226 arg1 = get_narrower (op1, &unsigned1);
8228 /* UNS is 1 if the operation to be done is an unsigned one. */
8229 uns = TYPE_UNSIGNED (result_type);
8231 final_type = result_type;
8233 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8234 but it *requires* conversion to FINAL_TYPE. */
8236 if ((TYPE_PRECISION (TREE_TYPE (op0))
8237 == TYPE_PRECISION (TREE_TYPE (arg0)))
8238 && TREE_TYPE (op0) != final_type)
8239 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8240 if ((TYPE_PRECISION (TREE_TYPE (op1))
8241 == TYPE_PRECISION (TREE_TYPE (arg1)))
8242 && TREE_TYPE (op1) != final_type)
8243 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8245 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8247 /* For bitwise operations, signedness of nominal type
8248 does not matter. Consider only how operands were extended. */
8249 if (shorten == -1)
8250 uns = unsigned0;
8252 /* Note that in all three cases below we refrain from optimizing
8253 an unsigned operation on sign-extended args.
8254 That would not be valid. */
8256 /* Both args variable: if both extended in same way
8257 from same width, do it in that width.
8258 Do it unsigned if args were zero-extended. */
8259 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8260 < TYPE_PRECISION (result_type))
8261 && (TYPE_PRECISION (TREE_TYPE (arg1))
8262 == TYPE_PRECISION (TREE_TYPE (arg0)))
8263 && unsigned0 == unsigned1
8264 && (unsigned0 || !uns))
8265 result_type
8266 = c_common_signed_or_unsigned_type
8267 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8268 else if (TREE_CODE (arg0) == INTEGER_CST
8269 && (unsigned1 || !uns)
8270 && (TYPE_PRECISION (TREE_TYPE (arg1))
8271 < TYPE_PRECISION (result_type))
8272 && (type
8273 = c_common_signed_or_unsigned_type (unsigned1,
8274 TREE_TYPE (arg1)),
8275 int_fits_type_p (arg0, type)))
8276 result_type = type;
8277 else if (TREE_CODE (arg1) == INTEGER_CST
8278 && (unsigned0 || !uns)
8279 && (TYPE_PRECISION (TREE_TYPE (arg0))
8280 < TYPE_PRECISION (result_type))
8281 && (type
8282 = c_common_signed_or_unsigned_type (unsigned0,
8283 TREE_TYPE (arg0)),
8284 int_fits_type_p (arg1, type)))
8285 result_type = type;
8288 /* Shifts can be shortened if shifting right. */
8290 if (short_shift)
8292 int unsigned_arg;
8293 tree arg0 = get_narrower (op0, &unsigned_arg);
8295 final_type = result_type;
8297 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8298 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8300 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8301 /* We can shorten only if the shift count is less than the
8302 number of bits in the smaller type size. */
8303 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8304 /* We cannot drop an unsigned shift after sign-extension. */
8305 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8307 /* Do an unsigned shift if the operand was zero-extended. */
8308 result_type
8309 = c_common_signed_or_unsigned_type (unsigned_arg,
8310 TREE_TYPE (arg0));
8311 /* Convert value-to-be-shifted to that type. */
8312 if (TREE_TYPE (op0) != result_type)
8313 op0 = convert (result_type, op0);
8314 converted = 1;
8318 /* Comparison operations are shortened too but differently.
8319 They identify themselves by setting short_compare = 1. */
8321 if (short_compare)
8323 /* Don't write &op0, etc., because that would prevent op0
8324 from being kept in a register.
8325 Instead, make copies of the our local variables and
8326 pass the copies by reference, then copy them back afterward. */
8327 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8328 enum tree_code xresultcode = resultcode;
8329 tree val
8330 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8332 if (val != 0)
8333 return val;
8335 op0 = xop0, op1 = xop1;
8336 converted = 1;
8337 resultcode = xresultcode;
8339 if (warn_sign_compare && skip_evaluation == 0)
8341 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8342 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8343 int unsignedp0, unsignedp1;
8344 tree primop0 = get_narrower (op0, &unsignedp0);
8345 tree primop1 = get_narrower (op1, &unsignedp1);
8347 xop0 = orig_op0;
8348 xop1 = orig_op1;
8349 STRIP_TYPE_NOPS (xop0);
8350 STRIP_TYPE_NOPS (xop1);
8352 /* Give warnings for comparisons between signed and unsigned
8353 quantities that may fail.
8355 Do the checking based on the original operand trees, so that
8356 casts will be considered, but default promotions won't be.
8358 Do not warn if the comparison is being done in a signed type,
8359 since the signed type will only be chosen if it can represent
8360 all the values of the unsigned type. */
8361 if (!TYPE_UNSIGNED (result_type))
8362 /* OK */;
8363 /* Do not warn if both operands are the same signedness. */
8364 else if (op0_signed == op1_signed)
8365 /* OK */;
8366 else
8368 tree sop, uop;
8369 bool ovf;
8371 if (op0_signed)
8372 sop = xop0, uop = xop1;
8373 else
8374 sop = xop1, uop = xop0;
8376 /* Do not warn if the signed quantity is an
8377 unsuffixed integer literal (or some static
8378 constant expression involving such literals or a
8379 conditional expression involving such literals)
8380 and it is non-negative. */
8381 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
8382 /* OK */;
8383 /* Do not warn if the comparison is an equality operation,
8384 the unsigned quantity is an integral constant, and it
8385 would fit in the result if the result were signed. */
8386 else if (TREE_CODE (uop) == INTEGER_CST
8387 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8388 && int_fits_type_p
8389 (uop, c_common_signed_type (result_type)))
8390 /* OK */;
8391 /* Do not warn if the unsigned quantity is an enumeration
8392 constant and its maximum value would fit in the result
8393 if the result were signed. */
8394 else if (TREE_CODE (uop) == INTEGER_CST
8395 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8396 && int_fits_type_p
8397 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8398 c_common_signed_type (result_type)))
8399 /* OK */;
8400 else
8401 warning (0, "comparison between signed and unsigned");
8404 /* Warn if two unsigned values are being compared in a size
8405 larger than their original size, and one (and only one) is the
8406 result of a `~' operator. This comparison will always fail.
8408 Also warn if one operand is a constant, and the constant
8409 does not have all bits set that are set in the ~ operand
8410 when it is extended. */
8412 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8413 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8415 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8416 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8417 &unsignedp0);
8418 else
8419 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8420 &unsignedp1);
8422 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8424 tree primop;
8425 HOST_WIDE_INT constant, mask;
8426 int unsignedp, bits;
8428 if (host_integerp (primop0, 0))
8430 primop = primop1;
8431 unsignedp = unsignedp1;
8432 constant = tree_low_cst (primop0, 0);
8434 else
8436 primop = primop0;
8437 unsignedp = unsignedp0;
8438 constant = tree_low_cst (primop1, 0);
8441 bits = TYPE_PRECISION (TREE_TYPE (primop));
8442 if (bits < TYPE_PRECISION (result_type)
8443 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8445 mask = (~(HOST_WIDE_INT) 0) << bits;
8446 if ((mask & constant) != mask)
8447 warning (0, "comparison of promoted ~unsigned with constant");
8450 else if (unsignedp0 && unsignedp1
8451 && (TYPE_PRECISION (TREE_TYPE (primop0))
8452 < TYPE_PRECISION (result_type))
8453 && (TYPE_PRECISION (TREE_TYPE (primop1))
8454 < TYPE_PRECISION (result_type)))
8455 warning (0, "comparison of promoted ~unsigned with unsigned");
8461 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8462 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8463 Then the expression will be built.
8464 It will be given type FINAL_TYPE if that is nonzero;
8465 otherwise, it will be given type RESULT_TYPE. */
8467 if (!result_type)
8469 binary_op_error (code);
8470 return error_mark_node;
8473 if (!converted)
8475 if (TREE_TYPE (op0) != result_type)
8476 op0 = convert_and_check (result_type, op0);
8477 if (TREE_TYPE (op1) != result_type)
8478 op1 = convert_and_check (result_type, op1);
8480 /* This can happen if one operand has a vector type, and the other
8481 has a different type. */
8482 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8483 return error_mark_node;
8486 if (build_type == NULL_TREE)
8487 build_type = result_type;
8490 /* Treat expressions in initializers specially as they can't trap. */
8491 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8492 build_type,
8493 op0, op1)
8494 : fold_build2 (resultcode, build_type,
8495 op0, op1);
8497 if (final_type != 0)
8498 result = convert (final_type, result);
8499 return result;
8504 /* Convert EXPR to be a truth-value, validating its type for this
8505 purpose. */
8507 tree
8508 c_objc_common_truthvalue_conversion (tree expr)
8510 switch (TREE_CODE (TREE_TYPE (expr)))
8512 case ARRAY_TYPE:
8513 error ("used array that cannot be converted to pointer where scalar is required");
8514 return error_mark_node;
8516 case RECORD_TYPE:
8517 error ("used struct type value where scalar is required");
8518 return error_mark_node;
8520 case UNION_TYPE:
8521 error ("used union type value where scalar is required");
8522 return error_mark_node;
8524 case FUNCTION_TYPE:
8525 gcc_unreachable ();
8527 default:
8528 break;
8531 /* ??? Should we also give an error for void and vectors rather than
8532 leaving those to give errors later? */
8533 return c_common_truthvalue_conversion (expr);
8537 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8538 required. */
8540 tree
8541 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8542 bool *ti ATTRIBUTE_UNUSED, bool *se)
8544 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8546 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8547 /* Executing a compound literal inside a function reinitializes
8548 it. */
8549 if (!TREE_STATIC (decl))
8550 *se = true;
8551 return decl;
8553 else
8554 return expr;
8557 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8559 tree
8560 c_begin_omp_parallel (void)
8562 tree block;
8564 keep_next_level ();
8565 block = c_begin_compound_stmt (true);
8567 return block;
8570 tree
8571 c_finish_omp_parallel (tree clauses, tree block)
8573 tree stmt;
8575 block = c_end_compound_stmt (block, true);
8577 stmt = make_node (OMP_PARALLEL);
8578 TREE_TYPE (stmt) = void_type_node;
8579 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8580 OMP_PARALLEL_BODY (stmt) = block;
8582 return add_stmt (stmt);
8585 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
8586 Remove any elements from the list that are invalid. */
8588 tree
8589 c_finish_omp_clauses (tree clauses)
8591 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8592 tree c, t, *pc = &clauses;
8593 const char *name;
8595 bitmap_obstack_initialize (NULL);
8596 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8597 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8598 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8600 for (pc = &clauses, c = clauses; c ; c = *pc)
8602 bool remove = false;
8603 bool need_complete = false;
8604 bool need_implicitly_determined = false;
8606 switch (OMP_CLAUSE_CODE (c))
8608 case OMP_CLAUSE_SHARED:
8609 name = "shared";
8610 need_implicitly_determined = true;
8611 goto check_dup_generic;
8613 case OMP_CLAUSE_PRIVATE:
8614 name = "private";
8615 need_complete = true;
8616 need_implicitly_determined = true;
8617 goto check_dup_generic;
8619 case OMP_CLAUSE_REDUCTION:
8620 name = "reduction";
8621 need_implicitly_determined = true;
8622 t = OMP_CLAUSE_DECL (c);
8623 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8624 || POINTER_TYPE_P (TREE_TYPE (t)))
8626 error ("%qE has invalid type for %<reduction%>", t);
8627 remove = true;
8629 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8631 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8632 const char *r_name = NULL;
8634 switch (r_code)
8636 case PLUS_EXPR:
8637 case MULT_EXPR:
8638 case MINUS_EXPR:
8639 break;
8640 case BIT_AND_EXPR:
8641 r_name = "&";
8642 break;
8643 case BIT_XOR_EXPR:
8644 r_name = "^";
8645 break;
8646 case BIT_IOR_EXPR:
8647 r_name = "|";
8648 break;
8649 case TRUTH_ANDIF_EXPR:
8650 r_name = "&&";
8651 break;
8652 case TRUTH_ORIF_EXPR:
8653 r_name = "||";
8654 break;
8655 default:
8656 gcc_unreachable ();
8658 if (r_name)
8660 error ("%qE has invalid type for %<reduction(%s)%>",
8661 t, r_name);
8662 remove = true;
8665 goto check_dup_generic;
8667 case OMP_CLAUSE_COPYPRIVATE:
8668 name = "copyprivate";
8669 goto check_dup_generic;
8671 case OMP_CLAUSE_COPYIN:
8672 name = "copyin";
8673 t = OMP_CLAUSE_DECL (c);
8674 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8676 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8677 remove = true;
8679 goto check_dup_generic;
8681 check_dup_generic:
8682 t = OMP_CLAUSE_DECL (c);
8683 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8685 error ("%qE is not a variable in clause %qs", t, name);
8686 remove = true;
8688 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8689 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8690 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8692 error ("%qE appears more than once in data clauses", t);
8693 remove = true;
8695 else
8696 bitmap_set_bit (&generic_head, DECL_UID (t));
8697 break;
8699 case OMP_CLAUSE_FIRSTPRIVATE:
8700 name = "firstprivate";
8701 t = OMP_CLAUSE_DECL (c);
8702 need_complete = true;
8703 need_implicitly_determined = true;
8704 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8706 error ("%qE is not a variable in clause %<firstprivate%>", t);
8707 remove = true;
8709 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8710 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8712 error ("%qE appears more than once in data clauses", t);
8713 remove = true;
8715 else
8716 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8717 break;
8719 case OMP_CLAUSE_LASTPRIVATE:
8720 name = "lastprivate";
8721 t = OMP_CLAUSE_DECL (c);
8722 need_complete = true;
8723 need_implicitly_determined = true;
8724 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8726 error ("%qE is not a variable in clause %<lastprivate%>", t);
8727 remove = true;
8729 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8730 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8732 error ("%qE appears more than once in data clauses", t);
8733 remove = true;
8735 else
8736 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8737 break;
8739 case OMP_CLAUSE_IF:
8740 case OMP_CLAUSE_NUM_THREADS:
8741 case OMP_CLAUSE_SCHEDULE:
8742 case OMP_CLAUSE_NOWAIT:
8743 case OMP_CLAUSE_ORDERED:
8744 case OMP_CLAUSE_DEFAULT:
8745 pc = &OMP_CLAUSE_CHAIN (c);
8746 continue;
8748 default:
8749 gcc_unreachable ();
8752 if (!remove)
8754 t = OMP_CLAUSE_DECL (c);
8756 if (need_complete)
8758 t = require_complete_type (t);
8759 if (t == error_mark_node)
8760 remove = true;
8763 if (need_implicitly_determined)
8765 const char *share_name = NULL;
8767 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8768 share_name = "threadprivate";
8769 else switch (c_omp_predetermined_sharing (t))
8771 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8772 break;
8773 case OMP_CLAUSE_DEFAULT_SHARED:
8774 share_name = "shared";
8775 break;
8776 case OMP_CLAUSE_DEFAULT_PRIVATE:
8777 share_name = "private";
8778 break;
8779 default:
8780 gcc_unreachable ();
8782 if (share_name)
8784 error ("%qE is predetermined %qs for %qs",
8785 t, share_name, name);
8786 remove = true;
8791 if (remove)
8792 *pc = OMP_CLAUSE_CHAIN (c);
8793 else
8794 pc = &OMP_CLAUSE_CHAIN (c);
8797 bitmap_obstack_release (NULL);
8798 return clauses;