Mark ChangeLog
[official-gcc.git] / gcc / c-typeck.c
blob9646bd6e4360c14fb239ba8c88b2f6b829ebf7e1
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;
533 unsigned target_quals;
535 /* Save time if the two types are the same. */
537 if (t1 == t2) return t1;
539 /* If one type is nonsense, use the other. */
540 if (t1 == error_mark_node)
541 return t2;
542 if (t2 == error_mark_node)
543 return t1;
545 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
546 && TREE_CODE (t2) == POINTER_TYPE);
548 /* Merge the attributes. */
549 attributes = targetm.merge_type_attributes (t1, t2);
551 /* Find the composite type of the target types, and combine the
552 qualifiers of the two types' targets. Do not lose qualifiers on
553 array element types by taking the TYPE_MAIN_VARIANT. */
554 mv1 = pointed_to_1 = TREE_TYPE (t1);
555 mv2 = pointed_to_2 = TREE_TYPE (t2);
556 if (TREE_CODE (mv1) != ARRAY_TYPE)
557 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
558 if (TREE_CODE (mv2) != ARRAY_TYPE)
559 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
560 target = composite_type (mv1, mv2);
562 /* For function types do not merge const qualifiers, but drop them
563 if used inconsistently. The middle-end uses these to mark const
564 and noreturn functions. */
565 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
566 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
567 else
568 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
569 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
570 return build_type_attribute_variant (t1, attributes);
573 /* Return the common type for two arithmetic types under the usual
574 arithmetic conversions. The default conversions have already been
575 applied, and enumerated types converted to their compatible integer
576 types. The resulting type is unqualified and has no attributes.
578 This is the type for the result of most arithmetic operations
579 if the operands have the given two types. */
581 static tree
582 c_common_type (tree t1, tree t2)
584 enum tree_code code1;
585 enum tree_code code2;
587 /* If one type is nonsense, use the other. */
588 if (t1 == error_mark_node)
589 return t2;
590 if (t2 == error_mark_node)
591 return t1;
593 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
594 t1 = TYPE_MAIN_VARIANT (t1);
596 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
597 t2 = TYPE_MAIN_VARIANT (t2);
599 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
600 t1 = build_type_attribute_variant (t1, NULL_TREE);
602 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
603 t2 = build_type_attribute_variant (t2, NULL_TREE);
605 /* Save time if the two types are the same. */
607 if (t1 == t2) return t1;
609 code1 = TREE_CODE (t1);
610 code2 = TREE_CODE (t2);
612 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
613 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
614 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
615 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
617 /* When one operand is a decimal float type, the other operand cannot be
618 a generic float type or a complex type. We also disallow vector types
619 here. */
620 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
621 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
623 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
625 error ("can%'t mix operands of decimal float and vector types");
626 return error_mark_node;
628 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
630 error ("can%'t mix operands of decimal float and complex types");
631 return error_mark_node;
633 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
635 error ("can%'t mix operands of decimal float and other float types");
636 return error_mark_node;
640 /* If one type is a vector type, return that type. (How the usual
641 arithmetic conversions apply to the vector types extension is not
642 precisely specified.) */
643 if (code1 == VECTOR_TYPE)
644 return t1;
646 if (code2 == VECTOR_TYPE)
647 return t2;
649 /* If one type is complex, form the common type of the non-complex
650 components, then make that complex. Use T1 or T2 if it is the
651 required type. */
652 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
654 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
655 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
656 tree subtype = c_common_type (subtype1, subtype2);
658 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
659 return t1;
660 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
661 return t2;
662 else
663 return build_complex_type (subtype);
666 /* If only one is real, use it as the result. */
668 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
669 return t1;
671 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
672 return t2;
674 /* If both are real and either are decimal floating point types, use
675 the decimal floating point type with the greater precision. */
677 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
679 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
680 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
681 return dfloat128_type_node;
682 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
683 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
684 return dfloat64_type_node;
685 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
686 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
687 return dfloat32_type_node;
690 /* Both real or both integers; use the one with greater precision. */
692 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
693 return t1;
694 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
695 return t2;
697 /* Same precision. Prefer long longs to longs to ints when the
698 same precision, following the C99 rules on integer type rank
699 (which are equivalent to the C90 rules for C90 types). */
701 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
702 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
703 return long_long_unsigned_type_node;
705 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
706 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
708 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
709 return long_long_unsigned_type_node;
710 else
711 return long_long_integer_type_node;
714 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
715 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
716 return long_unsigned_type_node;
718 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
719 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
721 /* But preserve unsignedness from the other type,
722 since long cannot hold all the values of an unsigned int. */
723 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
724 return long_unsigned_type_node;
725 else
726 return long_integer_type_node;
729 /* Likewise, prefer long double to double even if same size. */
730 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
731 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
732 return long_double_type_node;
734 /* Otherwise prefer the unsigned one. */
736 if (TYPE_UNSIGNED (t1))
737 return t1;
738 else
739 return t2;
742 /* Wrapper around c_common_type that is used by c-common.c and other
743 front end optimizations that remove promotions. ENUMERAL_TYPEs
744 are allowed here and are converted to their compatible integer types.
745 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
746 preferably a non-Boolean type as the common type. */
747 tree
748 common_type (tree t1, tree t2)
750 if (TREE_CODE (t1) == ENUMERAL_TYPE)
751 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
752 if (TREE_CODE (t2) == ENUMERAL_TYPE)
753 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
755 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
756 if (TREE_CODE (t1) == BOOLEAN_TYPE
757 && TREE_CODE (t2) == BOOLEAN_TYPE)
758 return boolean_type_node;
760 /* If either type is BOOLEAN_TYPE, then return the other. */
761 if (TREE_CODE (t1) == BOOLEAN_TYPE)
762 return t2;
763 if (TREE_CODE (t2) == BOOLEAN_TYPE)
764 return t1;
766 return c_common_type (t1, t2);
769 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
770 or various other operations. Return 2 if they are compatible
771 but a warning may be needed if you use them together. */
774 comptypes (tree type1, tree type2)
776 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
777 int val;
779 val = comptypes_internal (type1, type2);
780 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
782 return val;
785 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
786 or various other operations. Return 2 if they are compatible
787 but a warning may be needed if you use them together. This
788 differs from comptypes, in that we don't free the seen types. */
790 static int
791 comptypes_internal (tree type1, tree type2)
793 tree t1 = type1;
794 tree t2 = type2;
795 int attrval, val;
797 /* Suppress errors caused by previously reported errors. */
799 if (t1 == t2 || !t1 || !t2
800 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
801 return 1;
803 /* If either type is the internal version of sizetype, return the
804 language version. */
805 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
806 && TYPE_ORIG_SIZE_TYPE (t1))
807 t1 = TYPE_ORIG_SIZE_TYPE (t1);
809 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
810 && TYPE_ORIG_SIZE_TYPE (t2))
811 t2 = TYPE_ORIG_SIZE_TYPE (t2);
814 /* Enumerated types are compatible with integer types, but this is
815 not transitive: two enumerated types in the same translation unit
816 are compatible with each other only if they are the same type. */
818 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
819 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
820 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
821 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
823 if (t1 == t2)
824 return 1;
826 /* Different classes of types can't be compatible. */
828 if (TREE_CODE (t1) != TREE_CODE (t2))
829 return 0;
831 /* Qualifiers must match. C99 6.7.3p9 */
833 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
834 return 0;
836 /* Allow for two different type nodes which have essentially the same
837 definition. Note that we already checked for equality of the type
838 qualifiers (just above). */
840 if (TREE_CODE (t1) != ARRAY_TYPE
841 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
842 return 1;
844 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
845 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
846 return 0;
848 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
849 val = 0;
851 switch (TREE_CODE (t1))
853 case POINTER_TYPE:
854 /* Do not remove mode or aliasing information. */
855 if (TYPE_MODE (t1) != TYPE_MODE (t2)
856 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
857 break;
858 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
859 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
860 break;
862 case FUNCTION_TYPE:
863 val = function_types_compatible_p (t1, t2);
864 break;
866 case ARRAY_TYPE:
868 tree d1 = TYPE_DOMAIN (t1);
869 tree d2 = TYPE_DOMAIN (t2);
870 bool d1_variable, d2_variable;
871 bool d1_zero, d2_zero;
872 val = 1;
874 /* Target types must match incl. qualifiers. */
875 if (TREE_TYPE (t1) != TREE_TYPE (t2)
876 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
877 return 0;
879 /* Sizes must match unless one is missing or variable. */
880 if (d1 == 0 || d2 == 0 || d1 == d2)
881 break;
883 d1_zero = !TYPE_MAX_VALUE (d1);
884 d2_zero = !TYPE_MAX_VALUE (d2);
886 d1_variable = (!d1_zero
887 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
888 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
889 d2_variable = (!d2_zero
890 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
891 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
892 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
893 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
895 if (d1_variable || d2_variable)
896 break;
897 if (d1_zero && d2_zero)
898 break;
899 if (d1_zero || d2_zero
900 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
901 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
902 val = 0;
904 break;
907 case ENUMERAL_TYPE:
908 case RECORD_TYPE:
909 case UNION_TYPE:
910 if (val != 1 && !same_translation_unit_p (t1, t2))
912 tree a1 = TYPE_ATTRIBUTES (t1);
913 tree a2 = TYPE_ATTRIBUTES (t2);
915 if (! attribute_list_contained (a1, a2)
916 && ! attribute_list_contained (a2, a1))
917 break;
919 if (attrval != 2)
920 return tagged_types_tu_compatible_p (t1, t2);
921 val = tagged_types_tu_compatible_p (t1, t2);
923 break;
925 case VECTOR_TYPE:
926 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
927 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
928 break;
930 default:
931 break;
933 return attrval == 2 && val == 1 ? 2 : val;
936 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
937 ignoring their qualifiers. */
939 static int
940 comp_target_types (tree ttl, tree ttr)
942 int val;
943 tree mvl, mvr;
945 /* Do not lose qualifiers on element types of array types that are
946 pointer targets by taking their TYPE_MAIN_VARIANT. */
947 mvl = TREE_TYPE (ttl);
948 mvr = TREE_TYPE (ttr);
949 if (TREE_CODE (mvl) != ARRAY_TYPE)
950 mvl = TYPE_MAIN_VARIANT (mvl);
951 if (TREE_CODE (mvr) != ARRAY_TYPE)
952 mvr = TYPE_MAIN_VARIANT (mvr);
953 val = comptypes (mvl, mvr);
955 if (val == 2 && pedantic)
956 pedwarn ("types are not quite compatible");
957 return val;
960 /* Subroutines of `comptypes'. */
962 /* Determine whether two trees derive from the same translation unit.
963 If the CONTEXT chain ends in a null, that tree's context is still
964 being parsed, so if two trees have context chains ending in null,
965 they're in the same translation unit. */
967 same_translation_unit_p (tree t1, tree t2)
969 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
970 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
972 case tcc_declaration:
973 t1 = DECL_CONTEXT (t1); break;
974 case tcc_type:
975 t1 = TYPE_CONTEXT (t1); break;
976 case tcc_exceptional:
977 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
978 default: gcc_unreachable ();
981 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
982 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
984 case tcc_declaration:
985 t2 = DECL_CONTEXT (t2); break;
986 case tcc_type:
987 t2 = TYPE_CONTEXT (t2); break;
988 case tcc_exceptional:
989 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
990 default: gcc_unreachable ();
993 return t1 == t2;
996 /* Allocate the seen two types, assuming that they are compatible. */
998 static struct tagged_tu_seen_cache *
999 alloc_tagged_tu_seen_cache (tree t1, tree t2)
1001 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1002 tu->next = tagged_tu_seen_base;
1003 tu->t1 = t1;
1004 tu->t2 = t2;
1006 tagged_tu_seen_base = tu;
1008 /* The C standard says that two structures in different translation
1009 units are compatible with each other only if the types of their
1010 fields are compatible (among other things). We assume that they
1011 are compatible until proven otherwise when building the cache.
1012 An example where this can occur is:
1013 struct a
1015 struct a *next;
1017 If we are comparing this against a similar struct in another TU,
1018 and did not assume they were compatible, we end up with an infinite
1019 loop. */
1020 tu->val = 1;
1021 return tu;
1024 /* Free the seen types until we get to TU_TIL. */
1026 static void
1027 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1029 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1030 while (tu != tu_til)
1032 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
1033 tu = tu1->next;
1034 free (tu1);
1036 tagged_tu_seen_base = tu_til;
1039 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1040 compatible. If the two types are not the same (which has been
1041 checked earlier), this can only happen when multiple translation
1042 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1043 rules. */
1045 static int
1046 tagged_types_tu_compatible_p (tree t1, tree t2)
1048 tree s1, s2;
1049 bool needs_warning = false;
1051 /* We have to verify that the tags of the types are the same. This
1052 is harder than it looks because this may be a typedef, so we have
1053 to go look at the original type. It may even be a typedef of a
1054 typedef...
1055 In the case of compiler-created builtin structs the TYPE_DECL
1056 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1057 while (TYPE_NAME (t1)
1058 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1059 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1060 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1062 while (TYPE_NAME (t2)
1063 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1064 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1065 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1067 /* C90 didn't have the requirement that the two tags be the same. */
1068 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1069 return 0;
1071 /* C90 didn't say what happened if one or both of the types were
1072 incomplete; we choose to follow C99 rules here, which is that they
1073 are compatible. */
1074 if (TYPE_SIZE (t1) == NULL
1075 || TYPE_SIZE (t2) == NULL)
1076 return 1;
1079 const struct tagged_tu_seen_cache * tts_i;
1080 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1081 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1082 return tts_i->val;
1085 switch (TREE_CODE (t1))
1087 case ENUMERAL_TYPE:
1089 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1090 /* Speed up the case where the type values are in the same order. */
1091 tree tv1 = TYPE_VALUES (t1);
1092 tree tv2 = TYPE_VALUES (t2);
1094 if (tv1 == tv2)
1096 return 1;
1099 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1101 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1102 break;
1103 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1105 tu->val = 0;
1106 return 0;
1110 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1112 return 1;
1114 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1116 tu->val = 0;
1117 return 0;
1120 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1122 tu->val = 0;
1123 return 0;
1126 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1128 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1129 if (s2 == NULL
1130 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1132 tu->val = 0;
1133 return 0;
1136 return 1;
1139 case UNION_TYPE:
1141 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1142 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1144 tu->val = 0;
1145 return 0;
1148 /* Speed up the common case where the fields are in the same order. */
1149 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1150 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1152 int result;
1155 if (DECL_NAME (s1) == NULL
1156 || DECL_NAME (s1) != DECL_NAME (s2))
1157 break;
1158 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1159 if (result == 0)
1161 tu->val = 0;
1162 return 0;
1164 if (result == 2)
1165 needs_warning = true;
1167 if (TREE_CODE (s1) == FIELD_DECL
1168 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1169 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1171 tu->val = 0;
1172 return 0;
1175 if (!s1 && !s2)
1177 tu->val = needs_warning ? 2 : 1;
1178 return tu->val;
1181 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1183 bool ok = false;
1185 if (DECL_NAME (s1) != NULL)
1186 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1187 if (DECL_NAME (s1) == DECL_NAME (s2))
1189 int result;
1190 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1191 if (result == 0)
1193 tu->val = 0;
1194 return 0;
1196 if (result == 2)
1197 needs_warning = true;
1199 if (TREE_CODE (s1) == FIELD_DECL
1200 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1201 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1202 break;
1204 ok = true;
1205 break;
1207 if (!ok)
1209 tu->val = 0;
1210 return 0;
1213 tu->val = needs_warning ? 2 : 10;
1214 return tu->val;
1217 case RECORD_TYPE:
1219 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1221 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1222 s1 && s2;
1223 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1225 int result;
1226 if (TREE_CODE (s1) != TREE_CODE (s2)
1227 || DECL_NAME (s1) != DECL_NAME (s2))
1228 break;
1229 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1230 if (result == 0)
1231 break;
1232 if (result == 2)
1233 needs_warning = true;
1235 if (TREE_CODE (s1) == FIELD_DECL
1236 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1237 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1238 break;
1240 if (s1 && s2)
1241 tu->val = 0;
1242 else
1243 tu->val = needs_warning ? 2 : 1;
1244 return tu->val;
1247 default:
1248 gcc_unreachable ();
1252 /* Return 1 if two function types F1 and F2 are compatible.
1253 If either type specifies no argument types,
1254 the other must specify a fixed number of self-promoting arg types.
1255 Otherwise, if one type specifies only the number of arguments,
1256 the other must specify that number of self-promoting arg types.
1257 Otherwise, the argument types must match. */
1259 static int
1260 function_types_compatible_p (tree f1, tree f2)
1262 tree args1, args2;
1263 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1264 int val = 1;
1265 int val1;
1266 tree ret1, ret2;
1268 ret1 = TREE_TYPE (f1);
1269 ret2 = TREE_TYPE (f2);
1271 /* 'volatile' qualifiers on a function's return type used to mean
1272 the function is noreturn. */
1273 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1274 pedwarn ("function return types not compatible due to %<volatile%>");
1275 if (TYPE_VOLATILE (ret1))
1276 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1277 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1278 if (TYPE_VOLATILE (ret2))
1279 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1280 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1281 val = comptypes_internal (ret1, ret2);
1282 if (val == 0)
1283 return 0;
1285 args1 = TYPE_ARG_TYPES (f1);
1286 args2 = TYPE_ARG_TYPES (f2);
1288 /* An unspecified parmlist matches any specified parmlist
1289 whose argument types don't need default promotions. */
1291 if (args1 == 0)
1293 if (!self_promoting_args_p (args2))
1294 return 0;
1295 /* If one of these types comes from a non-prototype fn definition,
1296 compare that with the other type's arglist.
1297 If they don't match, ask for a warning (but no error). */
1298 if (TYPE_ACTUAL_ARG_TYPES (f1)
1299 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1300 val = 2;
1301 return val;
1303 if (args2 == 0)
1305 if (!self_promoting_args_p (args1))
1306 return 0;
1307 if (TYPE_ACTUAL_ARG_TYPES (f2)
1308 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1309 val = 2;
1310 return val;
1313 /* Both types have argument lists: compare them and propagate results. */
1314 val1 = type_lists_compatible_p (args1, args2);
1315 return val1 != 1 ? val1 : val;
1318 /* Check two lists of types for compatibility,
1319 returning 0 for incompatible, 1 for compatible,
1320 or 2 for compatible with warning. */
1322 static int
1323 type_lists_compatible_p (tree args1, tree args2)
1325 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1326 int val = 1;
1327 int newval = 0;
1329 while (1)
1331 tree a1, mv1, a2, mv2;
1332 if (args1 == 0 && args2 == 0)
1333 return val;
1334 /* If one list is shorter than the other,
1335 they fail to match. */
1336 if (args1 == 0 || args2 == 0)
1337 return 0;
1338 mv1 = a1 = TREE_VALUE (args1);
1339 mv2 = a2 = TREE_VALUE (args2);
1340 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1341 mv1 = TYPE_MAIN_VARIANT (mv1);
1342 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1343 mv2 = TYPE_MAIN_VARIANT (mv2);
1344 /* A null pointer instead of a type
1345 means there is supposed to be an argument
1346 but nothing is specified about what type it has.
1347 So match anything that self-promotes. */
1348 if (a1 == 0)
1350 if (c_type_promotes_to (a2) != a2)
1351 return 0;
1353 else if (a2 == 0)
1355 if (c_type_promotes_to (a1) != a1)
1356 return 0;
1358 /* If one of the lists has an error marker, ignore this arg. */
1359 else if (TREE_CODE (a1) == ERROR_MARK
1360 || TREE_CODE (a2) == ERROR_MARK)
1362 else if (!(newval = comptypes_internal (mv1, mv2)))
1364 /* Allow wait (union {union wait *u; int *i} *)
1365 and wait (union wait *) to be compatible. */
1366 if (TREE_CODE (a1) == UNION_TYPE
1367 && (TYPE_NAME (a1) == 0
1368 || TYPE_TRANSPARENT_UNION (a1))
1369 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1370 && tree_int_cst_equal (TYPE_SIZE (a1),
1371 TYPE_SIZE (a2)))
1373 tree memb;
1374 for (memb = TYPE_FIELDS (a1);
1375 memb; memb = TREE_CHAIN (memb))
1377 tree mv3 = TREE_TYPE (memb);
1378 if (mv3 && mv3 != error_mark_node
1379 && TREE_CODE (mv3) != ARRAY_TYPE)
1380 mv3 = TYPE_MAIN_VARIANT (mv3);
1381 if (comptypes_internal (mv3, mv2))
1382 break;
1384 if (memb == 0)
1385 return 0;
1387 else if (TREE_CODE (a2) == UNION_TYPE
1388 && (TYPE_NAME (a2) == 0
1389 || TYPE_TRANSPARENT_UNION (a2))
1390 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1391 && tree_int_cst_equal (TYPE_SIZE (a2),
1392 TYPE_SIZE (a1)))
1394 tree memb;
1395 for (memb = TYPE_FIELDS (a2);
1396 memb; memb = TREE_CHAIN (memb))
1398 tree mv3 = TREE_TYPE (memb);
1399 if (mv3 && mv3 != error_mark_node
1400 && TREE_CODE (mv3) != ARRAY_TYPE)
1401 mv3 = TYPE_MAIN_VARIANT (mv3);
1402 if (comptypes_internal (mv3, mv1))
1403 break;
1405 if (memb == 0)
1406 return 0;
1408 else
1409 return 0;
1412 /* comptypes said ok, but record if it said to warn. */
1413 if (newval > val)
1414 val = newval;
1416 args1 = TREE_CHAIN (args1);
1417 args2 = TREE_CHAIN (args2);
1421 /* Compute the size to increment a pointer by. */
1423 static tree
1424 c_size_in_bytes (tree type)
1426 enum tree_code code = TREE_CODE (type);
1428 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1429 return size_one_node;
1431 if (!COMPLETE_OR_VOID_TYPE_P (type))
1433 error ("arithmetic on pointer to an incomplete type");
1434 return size_one_node;
1437 /* Convert in case a char is more than one unit. */
1438 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1439 size_int (TYPE_PRECISION (char_type_node)
1440 / BITS_PER_UNIT));
1443 /* Return either DECL or its known constant value (if it has one). */
1445 tree
1446 decl_constant_value (tree decl)
1448 if (/* Don't change a variable array bound or initial value to a constant
1449 in a place where a variable is invalid. Note that DECL_INITIAL
1450 isn't valid for a PARM_DECL. */
1451 current_function_decl != 0
1452 && TREE_CODE (decl) != PARM_DECL
1453 && !TREE_THIS_VOLATILE (decl)
1454 && TREE_READONLY (decl)
1455 && DECL_INITIAL (decl) != 0
1456 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1457 /* This is invalid if initial value is not constant.
1458 If it has either a function call, a memory reference,
1459 or a variable, then re-evaluating it could give different results. */
1460 && TREE_CONSTANT (DECL_INITIAL (decl))
1461 /* Check for cases where this is sub-optimal, even though valid. */
1462 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1463 return DECL_INITIAL (decl);
1464 return decl;
1467 /* Return either DECL or its known constant value (if it has one), but
1468 return DECL if pedantic or DECL has mode BLKmode. This is for
1469 bug-compatibility with the old behavior of decl_constant_value
1470 (before GCC 3.0); every use of this function is a bug and it should
1471 be removed before GCC 3.1. It is not appropriate to use pedantic
1472 in a way that affects optimization, and BLKmode is probably not the
1473 right test for avoiding misoptimizations either. */
1475 static tree
1476 decl_constant_value_for_broken_optimization (tree decl)
1478 tree ret;
1480 if (pedantic || DECL_MODE (decl) == BLKmode)
1481 return decl;
1483 ret = decl_constant_value (decl);
1484 /* Avoid unwanted tree sharing between the initializer and current
1485 function's body where the tree can be modified e.g. by the
1486 gimplifier. */
1487 if (ret != decl && TREE_STATIC (decl))
1488 ret = unshare_expr (ret);
1489 return ret;
1492 /* Convert the array expression EXP to a pointer. */
1493 static tree
1494 array_to_pointer_conversion (tree exp)
1496 tree orig_exp = exp;
1497 tree type = TREE_TYPE (exp);
1498 tree adr;
1499 tree restype = TREE_TYPE (type);
1500 tree ptrtype;
1502 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1504 STRIP_TYPE_NOPS (exp);
1506 if (TREE_NO_WARNING (orig_exp))
1507 TREE_NO_WARNING (exp) = 1;
1509 ptrtype = build_pointer_type (restype);
1511 if (TREE_CODE (exp) == INDIRECT_REF)
1512 return convert (ptrtype, TREE_OPERAND (exp, 0));
1514 if (TREE_CODE (exp) == VAR_DECL)
1516 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1517 ADDR_EXPR because it's the best way of representing what
1518 happens in C when we take the address of an array and place
1519 it in a pointer to the element type. */
1520 adr = build1 (ADDR_EXPR, ptrtype, exp);
1521 if (!c_mark_addressable (exp))
1522 return error_mark_node;
1523 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1524 return adr;
1527 /* This way is better for a COMPONENT_REF since it can
1528 simplify the offset for a component. */
1529 adr = build_unary_op (ADDR_EXPR, exp, 1);
1530 return convert (ptrtype, adr);
1533 /* Convert the function expression EXP to a pointer. */
1534 static tree
1535 function_to_pointer_conversion (tree exp)
1537 tree orig_exp = exp;
1539 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1541 STRIP_TYPE_NOPS (exp);
1543 if (TREE_NO_WARNING (orig_exp))
1544 TREE_NO_WARNING (exp) = 1;
1546 return build_unary_op (ADDR_EXPR, exp, 0);
1549 /* Perform the default conversion of arrays and functions to pointers.
1550 Return the result of converting EXP. For any other expression, just
1551 return EXP after removing NOPs. */
1553 struct c_expr
1554 default_function_array_conversion (struct c_expr exp)
1556 tree orig_exp = exp.value;
1557 tree type = TREE_TYPE (exp.value);
1558 enum tree_code code = TREE_CODE (type);
1560 switch (code)
1562 case ARRAY_TYPE:
1564 bool not_lvalue = false;
1565 bool lvalue_array_p;
1567 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1568 || TREE_CODE (exp.value) == NOP_EXPR
1569 || TREE_CODE (exp.value) == CONVERT_EXPR)
1570 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1572 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1573 not_lvalue = true;
1574 exp.value = TREE_OPERAND (exp.value, 0);
1577 if (TREE_NO_WARNING (orig_exp))
1578 TREE_NO_WARNING (exp.value) = 1;
1580 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1581 if (!flag_isoc99 && !lvalue_array_p)
1583 /* Before C99, non-lvalue arrays do not decay to pointers.
1584 Normally, using such an array would be invalid; but it can
1585 be used correctly inside sizeof or as a statement expression.
1586 Thus, do not give an error here; an error will result later. */
1587 return exp;
1590 exp.value = array_to_pointer_conversion (exp.value);
1592 break;
1593 case FUNCTION_TYPE:
1594 exp.value = function_to_pointer_conversion (exp.value);
1595 break;
1596 default:
1597 STRIP_TYPE_NOPS (exp.value);
1598 if (TREE_NO_WARNING (orig_exp))
1599 TREE_NO_WARNING (exp.value) = 1;
1600 break;
1603 return exp;
1607 /* EXP is an expression of integer type. Apply the integer promotions
1608 to it and return the promoted value. */
1610 tree
1611 perform_integral_promotions (tree exp)
1613 tree type = TREE_TYPE (exp);
1614 enum tree_code code = TREE_CODE (type);
1616 gcc_assert (INTEGRAL_TYPE_P (type));
1618 /* Normally convert enums to int,
1619 but convert wide enums to something wider. */
1620 if (code == ENUMERAL_TYPE)
1622 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1623 TYPE_PRECISION (integer_type_node)),
1624 ((TYPE_PRECISION (type)
1625 >= TYPE_PRECISION (integer_type_node))
1626 && TYPE_UNSIGNED (type)));
1628 return convert (type, exp);
1631 /* ??? This should no longer be needed now bit-fields have their
1632 proper types. */
1633 if (TREE_CODE (exp) == COMPONENT_REF
1634 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1635 /* If it's thinner than an int, promote it like a
1636 c_promoting_integer_type_p, otherwise leave it alone. */
1637 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1638 TYPE_PRECISION (integer_type_node)))
1639 return convert (integer_type_node, exp);
1641 if (c_promoting_integer_type_p (type))
1643 /* Preserve unsignedness if not really getting any wider. */
1644 if (TYPE_UNSIGNED (type)
1645 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1646 return convert (unsigned_type_node, exp);
1648 return convert (integer_type_node, exp);
1651 return exp;
1655 /* Perform default promotions for C data used in expressions.
1656 Enumeral types or short or char are converted to int.
1657 In addition, manifest constants symbols are replaced by their values. */
1659 tree
1660 default_conversion (tree exp)
1662 tree orig_exp;
1663 tree type = TREE_TYPE (exp);
1664 enum tree_code code = TREE_CODE (type);
1666 /* Functions and arrays have been converted during parsing. */
1667 gcc_assert (code != FUNCTION_TYPE);
1668 if (code == ARRAY_TYPE)
1669 return exp;
1671 /* Constants can be used directly unless they're not loadable. */
1672 if (TREE_CODE (exp) == CONST_DECL)
1673 exp = DECL_INITIAL (exp);
1675 /* Replace a nonvolatile const static variable with its value unless
1676 it is an array, in which case we must be sure that taking the
1677 address of the array produces consistent results. */
1678 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1680 exp = decl_constant_value_for_broken_optimization (exp);
1681 type = TREE_TYPE (exp);
1684 /* Strip no-op conversions. */
1685 orig_exp = exp;
1686 STRIP_TYPE_NOPS (exp);
1688 if (TREE_NO_WARNING (orig_exp))
1689 TREE_NO_WARNING (exp) = 1;
1691 if (code == VOID_TYPE)
1693 error ("void value not ignored as it ought to be");
1694 return error_mark_node;
1697 exp = require_complete_type (exp);
1698 if (exp == error_mark_node)
1699 return error_mark_node;
1701 if (INTEGRAL_TYPE_P (type))
1702 return perform_integral_promotions (exp);
1704 return exp;
1707 /* Look up COMPONENT in a structure or union DECL.
1709 If the component name is not found, returns NULL_TREE. Otherwise,
1710 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1711 stepping down the chain to the component, which is in the last
1712 TREE_VALUE of the list. Normally the list is of length one, but if
1713 the component is embedded within (nested) anonymous structures or
1714 unions, the list steps down the chain to the component. */
1716 static tree
1717 lookup_field (tree decl, tree component)
1719 tree type = TREE_TYPE (decl);
1720 tree field;
1722 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1723 to the field elements. Use a binary search on this array to quickly
1724 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1725 will always be set for structures which have many elements. */
1727 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1729 int bot, top, half;
1730 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1732 field = TYPE_FIELDS (type);
1733 bot = 0;
1734 top = TYPE_LANG_SPECIFIC (type)->s->len;
1735 while (top - bot > 1)
1737 half = (top - bot + 1) >> 1;
1738 field = field_array[bot+half];
1740 if (DECL_NAME (field) == NULL_TREE)
1742 /* Step through all anon unions in linear fashion. */
1743 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1745 field = field_array[bot++];
1746 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1747 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1749 tree anon = lookup_field (field, component);
1751 if (anon)
1752 return tree_cons (NULL_TREE, field, anon);
1756 /* Entire record is only anon unions. */
1757 if (bot > top)
1758 return NULL_TREE;
1760 /* Restart the binary search, with new lower bound. */
1761 continue;
1764 if (DECL_NAME (field) == component)
1765 break;
1766 if (DECL_NAME (field) < component)
1767 bot += half;
1768 else
1769 top = bot + half;
1772 if (DECL_NAME (field_array[bot]) == component)
1773 field = field_array[bot];
1774 else if (DECL_NAME (field) != component)
1775 return NULL_TREE;
1777 else
1779 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1781 if (DECL_NAME (field) == NULL_TREE
1782 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1783 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1785 tree anon = lookup_field (field, component);
1787 if (anon)
1788 return tree_cons (NULL_TREE, field, anon);
1791 if (DECL_NAME (field) == component)
1792 break;
1795 if (field == NULL_TREE)
1796 return NULL_TREE;
1799 return tree_cons (NULL_TREE, field, NULL_TREE);
1802 /* Make an expression to refer to the COMPONENT field of
1803 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1805 tree
1806 build_component_ref (tree datum, tree component)
1808 tree type = TREE_TYPE (datum);
1809 enum tree_code code = TREE_CODE (type);
1810 tree field = NULL;
1811 tree ref;
1813 if (!objc_is_public (datum, component))
1814 return error_mark_node;
1816 /* See if there is a field or component with name COMPONENT. */
1818 if (code == RECORD_TYPE || code == UNION_TYPE)
1820 if (!COMPLETE_TYPE_P (type))
1822 c_incomplete_type_error (NULL_TREE, type);
1823 return error_mark_node;
1826 field = lookup_field (datum, component);
1828 if (!field)
1830 error ("%qT has no member named %qE", type, component);
1831 return error_mark_node;
1834 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1835 This might be better solved in future the way the C++ front
1836 end does it - by giving the anonymous entities each a
1837 separate name and type, and then have build_component_ref
1838 recursively call itself. We can't do that here. */
1841 tree subdatum = TREE_VALUE (field);
1842 int quals;
1843 tree subtype;
1845 if (TREE_TYPE (subdatum) == error_mark_node)
1846 return error_mark_node;
1848 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1849 quals |= TYPE_QUALS (TREE_TYPE (datum));
1850 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1852 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1853 NULL_TREE);
1854 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1855 TREE_READONLY (ref) = 1;
1856 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1857 TREE_THIS_VOLATILE (ref) = 1;
1859 if (TREE_DEPRECATED (subdatum))
1860 warn_deprecated_use (subdatum);
1862 datum = ref;
1864 field = TREE_CHAIN (field);
1866 while (field);
1868 return ref;
1870 else if (code != ERROR_MARK)
1871 error ("request for member %qE in something not a structure or union",
1872 component);
1874 return error_mark_node;
1877 /* Given an expression PTR for a pointer, return an expression
1878 for the value pointed to.
1879 ERRORSTRING is the name of the operator to appear in error messages. */
1881 tree
1882 build_indirect_ref (tree ptr, const char *errorstring)
1884 tree pointer = default_conversion (ptr);
1885 tree type = TREE_TYPE (pointer);
1887 if (TREE_CODE (type) == POINTER_TYPE)
1889 if (TREE_CODE (pointer) == ADDR_EXPR
1890 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1891 == TREE_TYPE (type)))
1892 return TREE_OPERAND (pointer, 0);
1893 else
1895 tree t = TREE_TYPE (type);
1896 tree ref;
1898 ref = build1 (INDIRECT_REF, t, pointer);
1900 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1902 error ("dereferencing pointer to incomplete type");
1903 return error_mark_node;
1905 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1906 warning (0, "dereferencing %<void *%> pointer");
1908 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1909 so that we get the proper error message if the result is used
1910 to assign to. Also, &* is supposed to be a no-op.
1911 And ANSI C seems to specify that the type of the result
1912 should be the const type. */
1913 /* A de-reference of a pointer to const is not a const. It is valid
1914 to change it via some other pointer. */
1915 TREE_READONLY (ref) = TYPE_READONLY (t);
1916 TREE_SIDE_EFFECTS (ref)
1917 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1918 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1919 return ref;
1922 else if (TREE_CODE (pointer) != ERROR_MARK)
1923 error ("invalid type argument of %qs", errorstring);
1924 return error_mark_node;
1927 /* This handles expressions of the form "a[i]", which denotes
1928 an array reference.
1930 This is logically equivalent in C to *(a+i), but we may do it differently.
1931 If A is a variable or a member, we generate a primitive ARRAY_REF.
1932 This avoids forcing the array out of registers, and can work on
1933 arrays that are not lvalues (for example, members of structures returned
1934 by functions). */
1936 tree
1937 build_array_ref (tree array, tree index)
1939 bool swapped = false;
1940 if (TREE_TYPE (array) == error_mark_node
1941 || TREE_TYPE (index) == error_mark_node)
1942 return error_mark_node;
1944 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1945 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1947 tree temp;
1948 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1949 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1951 error ("subscripted value is neither array nor pointer");
1952 return error_mark_node;
1954 temp = array;
1955 array = index;
1956 index = temp;
1957 swapped = true;
1960 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1962 error ("array subscript is not an integer");
1963 return error_mark_node;
1966 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1968 error ("subscripted value is pointer to function");
1969 return error_mark_node;
1972 /* ??? Existing practice has been to warn only when the char
1973 index is syntactically the index, not for char[array]. */
1974 if (!swapped)
1975 warn_array_subscript_with_type_char (index);
1977 /* Apply default promotions *after* noticing character types. */
1978 index = default_conversion (index);
1980 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1982 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1984 tree rval, type;
1986 /* An array that is indexed by a non-constant
1987 cannot be stored in a register; we must be able to do
1988 address arithmetic on its address.
1989 Likewise an array of elements of variable size. */
1990 if (TREE_CODE (index) != INTEGER_CST
1991 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1992 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1994 if (!c_mark_addressable (array))
1995 return error_mark_node;
1997 /* An array that is indexed by a constant value which is not within
1998 the array bounds cannot be stored in a register either; because we
1999 would get a crash in store_bit_field/extract_bit_field when trying
2000 to access a non-existent part of the register. */
2001 if (TREE_CODE (index) == INTEGER_CST
2002 && TYPE_DOMAIN (TREE_TYPE (array))
2003 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2005 if (!c_mark_addressable (array))
2006 return error_mark_node;
2009 if (pedantic)
2011 tree foo = array;
2012 while (TREE_CODE (foo) == COMPONENT_REF)
2013 foo = TREE_OPERAND (foo, 0);
2014 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2015 pedwarn ("ISO C forbids subscripting %<register%> array");
2016 else if (!flag_isoc99 && !lvalue_p (foo))
2017 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
2020 type = TREE_TYPE (TREE_TYPE (array));
2021 if (TREE_CODE (type) != ARRAY_TYPE)
2022 type = TYPE_MAIN_VARIANT (type);
2023 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2024 /* Array ref is const/volatile if the array elements are
2025 or if the array is. */
2026 TREE_READONLY (rval)
2027 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2028 | TREE_READONLY (array));
2029 TREE_SIDE_EFFECTS (rval)
2030 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2031 | TREE_SIDE_EFFECTS (array));
2032 TREE_THIS_VOLATILE (rval)
2033 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2034 /* This was added by rms on 16 Nov 91.
2035 It fixes vol struct foo *a; a->elts[1]
2036 in an inline function.
2037 Hope it doesn't break something else. */
2038 | TREE_THIS_VOLATILE (array));
2039 return require_complete_type (fold (rval));
2041 else
2043 tree ar = default_conversion (array);
2045 if (ar == error_mark_node)
2046 return ar;
2048 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2049 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2051 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2052 "array indexing");
2056 /* Build an external reference to identifier ID. FUN indicates
2057 whether this will be used for a function call. LOC is the source
2058 location of the identifier. */
2059 tree
2060 build_external_ref (tree id, int fun, location_t loc)
2062 tree ref;
2063 tree decl = lookup_name (id);
2065 /* In Objective-C, an instance variable (ivar) may be preferred to
2066 whatever lookup_name() found. */
2067 decl = objc_lookup_ivar (decl, id);
2069 if (decl && decl != error_mark_node)
2070 ref = decl;
2071 else if (fun)
2072 /* Implicit function declaration. */
2073 ref = implicitly_declare (id);
2074 else if (decl == error_mark_node)
2075 /* Don't complain about something that's already been
2076 complained about. */
2077 return error_mark_node;
2078 else
2080 undeclared_variable (id, loc);
2081 return error_mark_node;
2084 if (TREE_TYPE (ref) == error_mark_node)
2085 return error_mark_node;
2087 if (TREE_DEPRECATED (ref))
2088 warn_deprecated_use (ref);
2090 if (!skip_evaluation)
2091 assemble_external (ref);
2092 TREE_USED (ref) = 1;
2094 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2096 if (!in_sizeof && !in_typeof)
2097 C_DECL_USED (ref) = 1;
2098 else if (DECL_INITIAL (ref) == 0
2099 && DECL_EXTERNAL (ref)
2100 && !TREE_PUBLIC (ref))
2101 record_maybe_used_decl (ref);
2104 if (TREE_CODE (ref) == CONST_DECL)
2106 used_types_insert (TREE_TYPE (ref));
2107 ref = DECL_INITIAL (ref);
2108 TREE_CONSTANT (ref) = 1;
2109 TREE_INVARIANT (ref) = 1;
2111 else if (current_function_decl != 0
2112 && !DECL_FILE_SCOPE_P (current_function_decl)
2113 && (TREE_CODE (ref) == VAR_DECL
2114 || TREE_CODE (ref) == PARM_DECL
2115 || TREE_CODE (ref) == FUNCTION_DECL))
2117 tree context = decl_function_context (ref);
2119 if (context != 0 && context != current_function_decl)
2120 DECL_NONLOCAL (ref) = 1;
2123 return ref;
2126 /* Record details of decls possibly used inside sizeof or typeof. */
2127 struct maybe_used_decl
2129 /* The decl. */
2130 tree decl;
2131 /* The level seen at (in_sizeof + in_typeof). */
2132 int level;
2133 /* The next one at this level or above, or NULL. */
2134 struct maybe_used_decl *next;
2137 static struct maybe_used_decl *maybe_used_decls;
2139 /* Record that DECL, an undefined static function reference seen
2140 inside sizeof or typeof, might be used if the operand of sizeof is
2141 a VLA type or the operand of typeof is a variably modified
2142 type. */
2144 static void
2145 record_maybe_used_decl (tree decl)
2147 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2148 t->decl = decl;
2149 t->level = in_sizeof + in_typeof;
2150 t->next = maybe_used_decls;
2151 maybe_used_decls = t;
2154 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2155 USED is false, just discard them. If it is true, mark them used
2156 (if no longer inside sizeof or typeof) or move them to the next
2157 level up (if still inside sizeof or typeof). */
2159 void
2160 pop_maybe_used (bool used)
2162 struct maybe_used_decl *p = maybe_used_decls;
2163 int cur_level = in_sizeof + in_typeof;
2164 while (p && p->level > cur_level)
2166 if (used)
2168 if (cur_level == 0)
2169 C_DECL_USED (p->decl) = 1;
2170 else
2171 p->level = cur_level;
2173 p = p->next;
2175 if (!used || cur_level == 0)
2176 maybe_used_decls = p;
2179 /* Return the result of sizeof applied to EXPR. */
2181 struct c_expr
2182 c_expr_sizeof_expr (struct c_expr expr)
2184 struct c_expr ret;
2185 if (expr.value == error_mark_node)
2187 ret.value = error_mark_node;
2188 ret.original_code = ERROR_MARK;
2189 pop_maybe_used (false);
2191 else
2193 ret.value = c_sizeof (TREE_TYPE (expr.value));
2194 ret.original_code = ERROR_MARK;
2195 if (c_vla_type_p (TREE_TYPE (expr.value)))
2197 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2198 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2200 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2202 return ret;
2205 /* Return the result of sizeof applied to T, a structure for the type
2206 name passed to sizeof (rather than the type itself). */
2208 struct c_expr
2209 c_expr_sizeof_type (struct c_type_name *t)
2211 tree type;
2212 struct c_expr ret;
2213 type = groktypename (t);
2214 ret.value = c_sizeof (type);
2215 ret.original_code = ERROR_MARK;
2216 pop_maybe_used (type != error_mark_node
2217 ? C_TYPE_VARIABLE_SIZE (type) : false);
2218 return ret;
2221 /* Build a function call to function FUNCTION with parameters PARAMS.
2222 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2223 TREE_VALUE of each node is a parameter-expression.
2224 FUNCTION's data type may be a function type or a pointer-to-function. */
2226 tree
2227 build_function_call (tree function, tree params)
2229 tree fntype, fundecl = 0;
2230 tree coerced_params;
2231 tree name = NULL_TREE, result;
2232 tree tem;
2234 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2235 STRIP_TYPE_NOPS (function);
2237 /* Convert anything with function type to a pointer-to-function. */
2238 if (TREE_CODE (function) == FUNCTION_DECL)
2240 /* Implement type-directed function overloading for builtins.
2241 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2242 handle all the type checking. The result is a complete expression
2243 that implements this function call. */
2244 tem = resolve_overloaded_builtin (function, params);
2245 if (tem)
2246 return tem;
2248 name = DECL_NAME (function);
2249 fundecl = function;
2251 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2252 function = function_to_pointer_conversion (function);
2254 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2255 expressions, like those used for ObjC messenger dispatches. */
2256 function = objc_rewrite_function_call (function, params);
2258 fntype = TREE_TYPE (function);
2260 if (TREE_CODE (fntype) == ERROR_MARK)
2261 return error_mark_node;
2263 if (!(TREE_CODE (fntype) == POINTER_TYPE
2264 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2266 error ("called object %qE is not a function", function);
2267 return error_mark_node;
2270 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2271 current_function_returns_abnormally = 1;
2273 /* fntype now gets the type of function pointed to. */
2274 fntype = TREE_TYPE (fntype);
2276 /* Check that the function is called through a compatible prototype.
2277 If it is not, replace the call by a trap, wrapped up in a compound
2278 expression if necessary. This has the nice side-effect to prevent
2279 the tree-inliner from generating invalid assignment trees which may
2280 blow up in the RTL expander later. */
2281 if ((TREE_CODE (function) == NOP_EXPR
2282 || TREE_CODE (function) == CONVERT_EXPR)
2283 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2284 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2285 && !comptypes (fntype, TREE_TYPE (tem)))
2287 tree return_type = TREE_TYPE (fntype);
2288 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2289 NULL_TREE);
2291 /* This situation leads to run-time undefined behavior. We can't,
2292 therefore, simply error unless we can prove that all possible
2293 executions of the program must execute the code. */
2294 warning (0, "function called through a non-compatible type");
2296 /* We can, however, treat "undefined" any way we please.
2297 Call abort to encourage the user to fix the program. */
2298 inform ("if this code is reached, the program will abort");
2300 if (VOID_TYPE_P (return_type))
2301 return trap;
2302 else
2304 tree rhs;
2306 if (AGGREGATE_TYPE_P (return_type))
2307 rhs = build_compound_literal (return_type,
2308 build_constructor (return_type, 0));
2309 else
2310 rhs = fold_convert (return_type, integer_zero_node);
2312 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2316 /* Convert the parameters to the types declared in the
2317 function prototype, or apply default promotions. */
2319 coerced_params
2320 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2322 if (coerced_params == error_mark_node)
2323 return error_mark_node;
2325 /* Check that the arguments to the function are valid. */
2327 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2328 TYPE_ARG_TYPES (fntype));
2330 if (require_constant_value)
2332 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2333 function, coerced_params, NULL_TREE);
2335 if (TREE_CONSTANT (result)
2336 && (name == NULL_TREE
2337 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2338 pedwarn_init ("initializer element is not constant");
2340 else
2341 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2342 function, coerced_params, NULL_TREE);
2344 if (VOID_TYPE_P (TREE_TYPE (result)))
2345 return result;
2346 return require_complete_type (result);
2349 /* Convert the argument expressions in the list VALUES
2350 to the types in the list TYPELIST. The result is a list of converted
2351 argument expressions, unless there are too few arguments in which
2352 case it is error_mark_node.
2354 If TYPELIST is exhausted, or when an element has NULL as its type,
2355 perform the default conversions.
2357 PARMLIST is the chain of parm decls for the function being called.
2358 It may be 0, if that info is not available.
2359 It is used only for generating error messages.
2361 FUNCTION is a tree for the called function. It is used only for
2362 error messages, where it is formatted with %qE.
2364 This is also where warnings about wrong number of args are generated.
2366 Both VALUES and the returned value are chains of TREE_LIST nodes
2367 with the elements of the list in the TREE_VALUE slots of those nodes. */
2369 static tree
2370 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2372 tree typetail, valtail;
2373 tree result = NULL;
2374 int parmnum;
2375 tree selector;
2377 /* Change pointer to function to the function itself for
2378 diagnostics. */
2379 if (TREE_CODE (function) == ADDR_EXPR
2380 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2381 function = TREE_OPERAND (function, 0);
2383 /* Handle an ObjC selector specially for diagnostics. */
2384 selector = objc_message_selector ();
2386 /* Scan the given expressions and types, producing individual
2387 converted arguments and pushing them on RESULT in reverse order. */
2389 for (valtail = values, typetail = typelist, parmnum = 0;
2390 valtail;
2391 valtail = TREE_CHAIN (valtail), parmnum++)
2393 tree type = typetail ? TREE_VALUE (typetail) : 0;
2394 tree val = TREE_VALUE (valtail);
2395 tree rname = function;
2396 int argnum = parmnum + 1;
2397 const char *invalid_func_diag;
2399 if (type == void_type_node)
2401 error ("too many arguments to function %qE", function);
2402 break;
2405 if (selector && argnum > 2)
2407 rname = selector;
2408 argnum -= 2;
2411 STRIP_TYPE_NOPS (val);
2413 val = require_complete_type (val);
2415 if (type != 0)
2417 /* Formal parm type is specified by a function prototype. */
2418 tree parmval;
2420 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2422 error ("type of formal parameter %d is incomplete", parmnum + 1);
2423 parmval = val;
2425 else
2427 /* Optionally warn about conversions that
2428 differ from the default conversions. */
2429 if (warn_conversion || warn_traditional)
2431 unsigned int formal_prec = TYPE_PRECISION (type);
2433 if (INTEGRAL_TYPE_P (type)
2434 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2435 warning (0, "passing argument %d of %qE as integer "
2436 "rather than floating due to prototype",
2437 argnum, rname);
2438 if (INTEGRAL_TYPE_P (type)
2439 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2440 warning (0, "passing argument %d of %qE as integer "
2441 "rather than complex due to prototype",
2442 argnum, rname);
2443 else if (TREE_CODE (type) == COMPLEX_TYPE
2444 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2445 warning (0, "passing argument %d of %qE as complex "
2446 "rather than floating due to prototype",
2447 argnum, rname);
2448 else if (TREE_CODE (type) == REAL_TYPE
2449 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2450 warning (0, "passing argument %d of %qE as floating "
2451 "rather than integer due to prototype",
2452 argnum, rname);
2453 else if (TREE_CODE (type) == COMPLEX_TYPE
2454 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2455 warning (0, "passing argument %d of %qE as complex "
2456 "rather than integer due to prototype",
2457 argnum, rname);
2458 else if (TREE_CODE (type) == REAL_TYPE
2459 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2460 warning (0, "passing argument %d of %qE as floating "
2461 "rather than complex due to prototype",
2462 argnum, rname);
2463 /* ??? At some point, messages should be written about
2464 conversions between complex types, but that's too messy
2465 to do now. */
2466 else if (TREE_CODE (type) == REAL_TYPE
2467 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2469 /* Warn if any argument is passed as `float',
2470 since without a prototype it would be `double'. */
2471 if (formal_prec == TYPE_PRECISION (float_type_node)
2472 && type != dfloat32_type_node)
2473 warning (0, "passing argument %d of %qE as %<float%> "
2474 "rather than %<double%> due to prototype",
2475 argnum, rname);
2477 /* Warn if mismatch between argument and prototype
2478 for decimal float types. Warn of conversions with
2479 binary float types and of precision narrowing due to
2480 prototype. */
2481 else if (type != TREE_TYPE (val)
2482 && (type == dfloat32_type_node
2483 || type == dfloat64_type_node
2484 || type == dfloat128_type_node
2485 || TREE_TYPE (val) == dfloat32_type_node
2486 || TREE_TYPE (val) == dfloat64_type_node
2487 || TREE_TYPE (val) == dfloat128_type_node)
2488 && (formal_prec
2489 <= TYPE_PRECISION (TREE_TYPE (val))
2490 || (type == dfloat128_type_node
2491 && (TREE_TYPE (val)
2492 != dfloat64_type_node
2493 && (TREE_TYPE (val)
2494 != dfloat32_type_node)))
2495 || (type == dfloat64_type_node
2496 && (TREE_TYPE (val)
2497 != dfloat32_type_node))))
2498 warning (0, "passing argument %d of %qE as %qT "
2499 "rather than %qT due to prototype",
2500 argnum, rname, type, TREE_TYPE (val));
2503 /* Detect integer changing in width or signedness.
2504 These warnings are only activated with
2505 -Wconversion, not with -Wtraditional. */
2506 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2507 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2509 tree would_have_been = default_conversion (val);
2510 tree type1 = TREE_TYPE (would_have_been);
2512 if (TREE_CODE (type) == ENUMERAL_TYPE
2513 && (TYPE_MAIN_VARIANT (type)
2514 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2515 /* No warning if function asks for enum
2516 and the actual arg is that enum type. */
2518 else if (formal_prec != TYPE_PRECISION (type1))
2519 warning (OPT_Wconversion, "passing argument %d of %qE "
2520 "with different width due to prototype",
2521 argnum, rname);
2522 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2524 /* Don't complain if the formal parameter type
2525 is an enum, because we can't tell now whether
2526 the value was an enum--even the same enum. */
2527 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2529 else if (TREE_CODE (val) == INTEGER_CST
2530 && int_fits_type_p (val, type))
2531 /* Change in signedness doesn't matter
2532 if a constant value is unaffected. */
2534 /* If the value is extended from a narrower
2535 unsigned type, it doesn't matter whether we
2536 pass it as signed or unsigned; the value
2537 certainly is the same either way. */
2538 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2539 && TYPE_UNSIGNED (TREE_TYPE (val)))
2541 else if (TYPE_UNSIGNED (type))
2542 warning (OPT_Wconversion, "passing argument %d of %qE "
2543 "as unsigned due to prototype",
2544 argnum, rname);
2545 else
2546 warning (OPT_Wconversion, "passing argument %d of %qE "
2547 "as signed due to prototype", argnum, rname);
2551 parmval = convert_for_assignment (type, val, ic_argpass,
2552 fundecl, function,
2553 parmnum + 1);
2555 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2556 && INTEGRAL_TYPE_P (type)
2557 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2558 parmval = default_conversion (parmval);
2560 result = tree_cons (NULL_TREE, parmval, result);
2562 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2563 && (TYPE_PRECISION (TREE_TYPE (val))
2564 < TYPE_PRECISION (double_type_node))
2565 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2566 /* Convert `float' to `double'. */
2567 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2568 else if ((invalid_func_diag =
2569 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2571 error (invalid_func_diag);
2572 return error_mark_node;
2574 else
2575 /* Convert `short' and `char' to full-size `int'. */
2576 result = tree_cons (NULL_TREE, default_conversion (val), result);
2578 if (typetail)
2579 typetail = TREE_CHAIN (typetail);
2582 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2584 error ("too few arguments to function %qE", function);
2585 return error_mark_node;
2588 return nreverse (result);
2591 /* This is the entry point used by the parser to build unary operators
2592 in the input. CODE, a tree_code, specifies the unary operator, and
2593 ARG is the operand. For unary plus, the C parser currently uses
2594 CONVERT_EXPR for code. */
2596 struct c_expr
2597 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2599 struct c_expr result;
2601 result.original_code = ERROR_MARK;
2602 result.value = build_unary_op (code, arg.value, 0);
2603 overflow_warning (result.value);
2604 return result;
2607 /* This is the entry point used by the parser to build binary operators
2608 in the input. CODE, a tree_code, specifies the binary operator, and
2609 ARG1 and ARG2 are the operands. In addition to constructing the
2610 expression, we check for operands that were written with other binary
2611 operators in a way that is likely to confuse the user. */
2613 struct c_expr
2614 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2615 struct c_expr arg2)
2617 struct c_expr result;
2619 enum tree_code code1 = arg1.original_code;
2620 enum tree_code code2 = arg2.original_code;
2622 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2623 result.original_code = code;
2625 if (TREE_CODE (result.value) == ERROR_MARK)
2626 return result;
2628 /* Check for cases such as x+y<<z which users are likely
2629 to misinterpret. */
2630 if (warn_parentheses)
2632 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2634 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2635 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2636 warning (OPT_Wparentheses,
2637 "suggest parentheses around + or - inside shift");
2640 if (code == TRUTH_ORIF_EXPR)
2642 if (code1 == TRUTH_ANDIF_EXPR
2643 || code2 == TRUTH_ANDIF_EXPR)
2644 warning (OPT_Wparentheses,
2645 "suggest parentheses around && within ||");
2648 if (code == BIT_IOR_EXPR)
2650 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2651 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2652 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2653 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2654 warning (OPT_Wparentheses,
2655 "suggest parentheses around arithmetic in operand of |");
2656 /* Check cases like x|y==z */
2657 if (TREE_CODE_CLASS (code1) == tcc_comparison
2658 || TREE_CODE_CLASS (code2) == tcc_comparison)
2659 warning (OPT_Wparentheses,
2660 "suggest parentheses around comparison in operand of |");
2663 if (code == BIT_XOR_EXPR)
2665 if (code1 == BIT_AND_EXPR
2666 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2667 || code2 == BIT_AND_EXPR
2668 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2669 warning (OPT_Wparentheses,
2670 "suggest parentheses around arithmetic in operand of ^");
2671 /* Check cases like x^y==z */
2672 if (TREE_CODE_CLASS (code1) == tcc_comparison
2673 || TREE_CODE_CLASS (code2) == tcc_comparison)
2674 warning (OPT_Wparentheses,
2675 "suggest parentheses around comparison in operand of ^");
2678 if (code == BIT_AND_EXPR)
2680 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2681 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2682 warning (OPT_Wparentheses,
2683 "suggest parentheses around + or - in operand of &");
2684 /* Check cases like x&y==z */
2685 if (TREE_CODE_CLASS (code1) == tcc_comparison
2686 || TREE_CODE_CLASS (code2) == tcc_comparison)
2687 warning (OPT_Wparentheses,
2688 "suggest parentheses around comparison in operand of &");
2690 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2691 if (TREE_CODE_CLASS (code) == tcc_comparison
2692 && (TREE_CODE_CLASS (code1) == tcc_comparison
2693 || TREE_CODE_CLASS (code2) == tcc_comparison))
2694 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2695 "have their mathematical meaning");
2699 /* Warn about comparisons against string literals, with the exception
2700 of testing for equality or inequality of a string literal with NULL. */
2701 if (code == EQ_EXPR || code == NE_EXPR)
2703 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2704 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2705 warning (OPT_Waddress,
2706 "comparison with string literal results in unspecified behaviour");
2708 else if (TREE_CODE_CLASS (code) == tcc_comparison
2709 && (code1 == STRING_CST || code2 == STRING_CST))
2710 warning (OPT_Waddress,
2711 "comparison with string literal results in unspecified behaviour");
2713 overflow_warning (result.value);
2715 return result;
2718 /* Return a tree for the difference of pointers OP0 and OP1.
2719 The resulting tree has type int. */
2721 static tree
2722 pointer_diff (tree op0, tree op1)
2724 tree restype = ptrdiff_type_node;
2726 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2727 tree con0, con1, lit0, lit1;
2728 tree orig_op1 = op1;
2730 if (pedantic || warn_pointer_arith)
2732 if (TREE_CODE (target_type) == VOID_TYPE)
2733 pedwarn ("pointer of type %<void *%> used in subtraction");
2734 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2735 pedwarn ("pointer to a function used in subtraction");
2738 /* If the conversion to ptrdiff_type does anything like widening or
2739 converting a partial to an integral mode, we get a convert_expression
2740 that is in the way to do any simplifications.
2741 (fold-const.c doesn't know that the extra bits won't be needed.
2742 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2743 different mode in place.)
2744 So first try to find a common term here 'by hand'; we want to cover
2745 at least the cases that occur in legal static initializers. */
2746 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2747 && (TYPE_PRECISION (TREE_TYPE (op0))
2748 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2749 con0 = TREE_OPERAND (op0, 0);
2750 else
2751 con0 = op0;
2752 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2753 && (TYPE_PRECISION (TREE_TYPE (op1))
2754 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2755 con1 = TREE_OPERAND (op1, 0);
2756 else
2757 con1 = op1;
2759 if (TREE_CODE (con0) == PLUS_EXPR)
2761 lit0 = TREE_OPERAND (con0, 1);
2762 con0 = TREE_OPERAND (con0, 0);
2764 else
2765 lit0 = integer_zero_node;
2767 if (TREE_CODE (con1) == PLUS_EXPR)
2769 lit1 = TREE_OPERAND (con1, 1);
2770 con1 = TREE_OPERAND (con1, 0);
2772 else
2773 lit1 = integer_zero_node;
2775 if (operand_equal_p (con0, con1, 0))
2777 op0 = lit0;
2778 op1 = lit1;
2782 /* First do the subtraction as integers;
2783 then drop through to build the divide operator.
2784 Do not do default conversions on the minus operator
2785 in case restype is a short type. */
2787 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2788 convert (restype, op1), 0);
2789 /* This generates an error if op1 is pointer to incomplete type. */
2790 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2791 error ("arithmetic on pointer to an incomplete type");
2793 /* This generates an error if op0 is pointer to incomplete type. */
2794 op1 = c_size_in_bytes (target_type);
2796 /* Divide by the size, in easiest possible way. */
2797 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2800 /* Construct and perhaps optimize a tree representation
2801 for a unary operation. CODE, a tree_code, specifies the operation
2802 and XARG is the operand.
2803 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2804 the default promotions (such as from short to int).
2805 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2806 allows non-lvalues; this is only used to handle conversion of non-lvalue
2807 arrays to pointers in C99. */
2809 tree
2810 build_unary_op (enum tree_code code, tree xarg, int flag)
2812 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2813 tree arg = xarg;
2814 tree argtype = 0;
2815 enum tree_code typecode;
2816 tree val;
2817 int noconvert = flag;
2818 const char *invalid_op_diag;
2820 if (code != ADDR_EXPR)
2821 arg = require_complete_type (arg);
2823 typecode = TREE_CODE (TREE_TYPE (arg));
2824 if (typecode == ERROR_MARK)
2825 return error_mark_node;
2826 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2827 typecode = INTEGER_TYPE;
2829 if ((invalid_op_diag
2830 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2832 error (invalid_op_diag);
2833 return error_mark_node;
2836 switch (code)
2838 case CONVERT_EXPR:
2839 /* This is used for unary plus, because a CONVERT_EXPR
2840 is enough to prevent anybody from looking inside for
2841 associativity, but won't generate any code. */
2842 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2843 || typecode == COMPLEX_TYPE
2844 || typecode == VECTOR_TYPE))
2846 error ("wrong type argument to unary plus");
2847 return error_mark_node;
2849 else if (!noconvert)
2850 arg = default_conversion (arg);
2851 arg = non_lvalue (arg);
2852 break;
2854 case NEGATE_EXPR:
2855 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2856 || typecode == COMPLEX_TYPE
2857 || typecode == VECTOR_TYPE))
2859 error ("wrong type argument to unary minus");
2860 return error_mark_node;
2862 else if (!noconvert)
2863 arg = default_conversion (arg);
2864 break;
2866 case BIT_NOT_EXPR:
2867 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2869 if (!noconvert)
2870 arg = default_conversion (arg);
2872 else if (typecode == COMPLEX_TYPE)
2874 code = CONJ_EXPR;
2875 if (pedantic)
2876 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2877 if (!noconvert)
2878 arg = default_conversion (arg);
2880 else
2882 error ("wrong type argument to bit-complement");
2883 return error_mark_node;
2885 break;
2887 case ABS_EXPR:
2888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2890 error ("wrong type argument to abs");
2891 return error_mark_node;
2893 else if (!noconvert)
2894 arg = default_conversion (arg);
2895 break;
2897 case CONJ_EXPR:
2898 /* Conjugating a real value is a no-op, but allow it anyway. */
2899 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2900 || typecode == COMPLEX_TYPE))
2902 error ("wrong type argument to conjugation");
2903 return error_mark_node;
2905 else if (!noconvert)
2906 arg = default_conversion (arg);
2907 break;
2909 case TRUTH_NOT_EXPR:
2910 if (typecode != INTEGER_TYPE
2911 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2912 && typecode != COMPLEX_TYPE)
2914 error ("wrong type argument to unary exclamation mark");
2915 return error_mark_node;
2917 arg = c_objc_common_truthvalue_conversion (arg);
2918 return invert_truthvalue (arg);
2920 case REALPART_EXPR:
2921 if (TREE_CODE (arg) == COMPLEX_CST)
2922 return TREE_REALPART (arg);
2923 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2924 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2925 else
2926 return arg;
2928 case IMAGPART_EXPR:
2929 if (TREE_CODE (arg) == COMPLEX_CST)
2930 return TREE_IMAGPART (arg);
2931 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2932 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2933 else
2934 return convert (TREE_TYPE (arg), integer_zero_node);
2936 case PREINCREMENT_EXPR:
2937 case POSTINCREMENT_EXPR:
2938 case PREDECREMENT_EXPR:
2939 case POSTDECREMENT_EXPR:
2941 /* Increment or decrement the real part of the value,
2942 and don't change the imaginary part. */
2943 if (typecode == COMPLEX_TYPE)
2945 tree real, imag;
2947 if (pedantic)
2948 pedwarn ("ISO C does not support %<++%> and %<--%>"
2949 " on complex types");
2951 arg = stabilize_reference (arg);
2952 real = build_unary_op (REALPART_EXPR, arg, 1);
2953 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2954 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2955 build_unary_op (code, real, 1), imag);
2958 /* Report invalid types. */
2960 if (typecode != POINTER_TYPE
2961 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2963 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2964 error ("wrong type argument to increment");
2965 else
2966 error ("wrong type argument to decrement");
2968 return error_mark_node;
2972 tree inc;
2973 tree result_type = TREE_TYPE (arg);
2975 arg = get_unwidened (arg, 0);
2976 argtype = TREE_TYPE (arg);
2978 /* Compute the increment. */
2980 if (typecode == POINTER_TYPE)
2982 /* If pointer target is an undefined struct,
2983 we just cannot know how to do the arithmetic. */
2984 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2986 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2987 error ("increment of pointer to unknown structure");
2988 else
2989 error ("decrement of pointer to unknown structure");
2991 else if ((pedantic || warn_pointer_arith)
2992 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2993 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2995 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2996 pedwarn ("wrong type argument to increment");
2997 else
2998 pedwarn ("wrong type argument to decrement");
3001 inc = c_size_in_bytes (TREE_TYPE (result_type));
3003 else
3004 inc = integer_one_node;
3006 inc = convert (argtype, inc);
3008 /* Complain about anything else that is not a true lvalue. */
3009 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3010 || code == POSTINCREMENT_EXPR)
3011 ? lv_increment
3012 : lv_decrement)))
3013 return error_mark_node;
3015 /* Report a read-only lvalue. */
3016 if (TREE_READONLY (arg))
3018 readonly_error (arg,
3019 ((code == PREINCREMENT_EXPR
3020 || code == POSTINCREMENT_EXPR)
3021 ? lv_increment : lv_decrement));
3022 return error_mark_node;
3025 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3026 val = boolean_increment (code, arg);
3027 else
3028 val = build2 (code, TREE_TYPE (arg), arg, inc);
3029 TREE_SIDE_EFFECTS (val) = 1;
3030 val = convert (result_type, val);
3031 if (TREE_CODE (val) != code)
3032 TREE_NO_WARNING (val) = 1;
3033 return val;
3036 case ADDR_EXPR:
3037 /* Note that this operation never does default_conversion. */
3039 /* Let &* cancel out to simplify resulting code. */
3040 if (TREE_CODE (arg) == INDIRECT_REF)
3042 /* Don't let this be an lvalue. */
3043 if (lvalue_p (TREE_OPERAND (arg, 0)))
3044 return non_lvalue (TREE_OPERAND (arg, 0));
3045 return TREE_OPERAND (arg, 0);
3048 /* For &x[y], return x+y */
3049 if (TREE_CODE (arg) == ARRAY_REF)
3051 tree op0 = TREE_OPERAND (arg, 0);
3052 if (!c_mark_addressable (op0))
3053 return error_mark_node;
3054 return build_binary_op (PLUS_EXPR,
3055 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3056 ? array_to_pointer_conversion (op0)
3057 : op0),
3058 TREE_OPERAND (arg, 1), 1);
3061 /* Anything not already handled and not a true memory reference
3062 or a non-lvalue array is an error. */
3063 else if (typecode != FUNCTION_TYPE && !flag
3064 && !lvalue_or_else (arg, lv_addressof))
3065 return error_mark_node;
3067 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3068 argtype = TREE_TYPE (arg);
3070 /* If the lvalue is const or volatile, merge that into the type
3071 to which the address will point. Note that you can't get a
3072 restricted pointer by taking the address of something, so we
3073 only have to deal with `const' and `volatile' here. */
3074 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3075 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3076 argtype = c_build_type_variant (argtype,
3077 TREE_READONLY (arg),
3078 TREE_THIS_VOLATILE (arg));
3080 if (!c_mark_addressable (arg))
3081 return error_mark_node;
3083 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3084 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3086 argtype = build_pointer_type (argtype);
3088 /* ??? Cope with user tricks that amount to offsetof. Delete this
3089 when we have proper support for integer constant expressions. */
3090 val = get_base_address (arg);
3091 if (val && TREE_CODE (val) == INDIRECT_REF
3092 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3094 tree op0 = fold_convert (argtype, fold_offsetof (arg, val)), op1;
3096 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3097 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3100 val = build1 (ADDR_EXPR, argtype, arg);
3102 return val;
3104 default:
3105 gcc_unreachable ();
3108 if (argtype == 0)
3109 argtype = TREE_TYPE (arg);
3110 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3111 : fold_build1 (code, argtype, arg);
3114 /* Return nonzero if REF is an lvalue valid for this language.
3115 Lvalues can be assigned, unless their type has TYPE_READONLY.
3116 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3118 static int
3119 lvalue_p (tree ref)
3121 enum tree_code code = TREE_CODE (ref);
3123 switch (code)
3125 case REALPART_EXPR:
3126 case IMAGPART_EXPR:
3127 case COMPONENT_REF:
3128 return lvalue_p (TREE_OPERAND (ref, 0));
3130 case COMPOUND_LITERAL_EXPR:
3131 case STRING_CST:
3132 return 1;
3134 case INDIRECT_REF:
3135 case ARRAY_REF:
3136 case VAR_DECL:
3137 case PARM_DECL:
3138 case RESULT_DECL:
3139 case ERROR_MARK:
3140 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3141 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3143 case BIND_EXPR:
3144 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3146 default:
3147 return 0;
3151 /* Give an error for storing in something that is 'const'. */
3153 static void
3154 readonly_error (tree arg, enum lvalue_use use)
3156 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3157 || use == lv_asm);
3158 /* Using this macro rather than (for example) arrays of messages
3159 ensures that all the format strings are checked at compile
3160 time. */
3161 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3162 : (use == lv_increment ? (I) \
3163 : (use == lv_decrement ? (D) : (AS))))
3164 if (TREE_CODE (arg) == COMPONENT_REF)
3166 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3167 readonly_error (TREE_OPERAND (arg, 0), use);
3168 else
3169 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3170 G_("increment of read-only member %qD"),
3171 G_("decrement of read-only member %qD"),
3172 G_("read-only member %qD used as %<asm%> output")),
3173 TREE_OPERAND (arg, 1));
3175 else if (TREE_CODE (arg) == VAR_DECL)
3176 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3177 G_("increment of read-only variable %qD"),
3178 G_("decrement of read-only variable %qD"),
3179 G_("read-only variable %qD used as %<asm%> output")),
3180 arg);
3181 else
3182 error (READONLY_MSG (G_("assignment of read-only location"),
3183 G_("increment of read-only location"),
3184 G_("decrement of read-only location"),
3185 G_("read-only location used as %<asm%> output")));
3189 /* Return nonzero if REF is an lvalue valid for this language;
3190 otherwise, print an error message and return zero. USE says
3191 how the lvalue is being used and so selects the error message. */
3193 static int
3194 lvalue_or_else (tree ref, enum lvalue_use use)
3196 int win = lvalue_p (ref);
3198 if (!win)
3199 lvalue_error (use);
3201 return win;
3204 /* Mark EXP saying that we need to be able to take the
3205 address of it; it should not be allocated in a register.
3206 Returns true if successful. */
3208 bool
3209 c_mark_addressable (tree exp)
3211 tree x = exp;
3213 while (1)
3214 switch (TREE_CODE (x))
3216 case COMPONENT_REF:
3217 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3219 error
3220 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3221 return false;
3224 /* ... fall through ... */
3226 case ADDR_EXPR:
3227 case ARRAY_REF:
3228 case REALPART_EXPR:
3229 case IMAGPART_EXPR:
3230 x = TREE_OPERAND (x, 0);
3231 break;
3233 case COMPOUND_LITERAL_EXPR:
3234 case CONSTRUCTOR:
3235 TREE_ADDRESSABLE (x) = 1;
3236 return true;
3238 case VAR_DECL:
3239 case CONST_DECL:
3240 case PARM_DECL:
3241 case RESULT_DECL:
3242 if (C_DECL_REGISTER (x)
3243 && DECL_NONLOCAL (x))
3245 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3247 error
3248 ("global register variable %qD used in nested function", x);
3249 return false;
3251 pedwarn ("register variable %qD used in nested function", x);
3253 else if (C_DECL_REGISTER (x))
3255 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3256 error ("address of global register variable %qD requested", x);
3257 else
3258 error ("address of register variable %qD requested", x);
3259 return false;
3262 /* drops in */
3263 case FUNCTION_DECL:
3264 TREE_ADDRESSABLE (x) = 1;
3265 /* drops out */
3266 default:
3267 return true;
3271 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3273 tree
3274 build_conditional_expr (tree ifexp, tree op1, tree op2)
3276 tree type1;
3277 tree type2;
3278 enum tree_code code1;
3279 enum tree_code code2;
3280 tree result_type = NULL;
3281 tree orig_op1 = op1, orig_op2 = op2;
3283 /* Promote both alternatives. */
3285 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3286 op1 = default_conversion (op1);
3287 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3288 op2 = default_conversion (op2);
3290 if (TREE_CODE (ifexp) == ERROR_MARK
3291 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3292 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3293 return error_mark_node;
3295 type1 = TREE_TYPE (op1);
3296 code1 = TREE_CODE (type1);
3297 type2 = TREE_TYPE (op2);
3298 code2 = TREE_CODE (type2);
3300 /* C90 does not permit non-lvalue arrays in conditional expressions.
3301 In C99 they will be pointers by now. */
3302 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3304 error ("non-lvalue array in conditional expression");
3305 return error_mark_node;
3308 /* Quickly detect the usual case where op1 and op2 have the same type
3309 after promotion. */
3310 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3312 if (type1 == type2)
3313 result_type = type1;
3314 else
3315 result_type = TYPE_MAIN_VARIANT (type1);
3317 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3318 || code1 == COMPLEX_TYPE)
3319 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3320 || code2 == COMPLEX_TYPE))
3322 result_type = c_common_type (type1, type2);
3324 /* If -Wsign-compare, warn here if type1 and type2 have
3325 different signedness. We'll promote the signed to unsigned
3326 and later code won't know it used to be different.
3327 Do this check on the original types, so that explicit casts
3328 will be considered, but default promotions won't. */
3329 if (warn_sign_compare && !skip_evaluation)
3331 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3332 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3334 if (unsigned_op1 ^ unsigned_op2)
3336 bool ovf;
3338 /* Do not warn if the result type is signed, since the
3339 signed type will only be chosen if it can represent
3340 all the values of the unsigned type. */
3341 if (!TYPE_UNSIGNED (result_type))
3342 /* OK */;
3343 /* Do not warn if the signed quantity is an unsuffixed
3344 integer literal (or some static constant expression
3345 involving such literals) and it is non-negative. */
3346 else if ((unsigned_op2
3347 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3348 || (unsigned_op1
3349 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3350 /* OK */;
3351 else
3352 warning (0, "signed and unsigned type in conditional expression");
3356 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3358 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3359 pedwarn ("ISO C forbids conditional expr with only one void side");
3360 result_type = void_type_node;
3362 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3364 if (comp_target_types (type1, type2))
3365 result_type = common_pointer_type (type1, type2);
3366 else if (null_pointer_constant_p (orig_op1))
3367 result_type = qualify_type (type2, type1);
3368 else if (null_pointer_constant_p (orig_op2))
3369 result_type = qualify_type (type1, type2);
3370 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3372 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3373 pedwarn ("ISO C forbids conditional expr between "
3374 "%<void *%> and function pointer");
3375 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3376 TREE_TYPE (type2)));
3378 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3380 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3381 pedwarn ("ISO C forbids conditional expr between "
3382 "%<void *%> and function pointer");
3383 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3384 TREE_TYPE (type1)));
3386 else
3388 pedwarn ("pointer type mismatch in conditional expression");
3389 result_type = build_pointer_type (void_type_node);
3392 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3394 if (!null_pointer_constant_p (orig_op2))
3395 pedwarn ("pointer/integer type mismatch in conditional expression");
3396 else
3398 op2 = null_pointer_node;
3400 result_type = type1;
3402 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3404 if (!null_pointer_constant_p (orig_op1))
3405 pedwarn ("pointer/integer type mismatch in conditional expression");
3406 else
3408 op1 = null_pointer_node;
3410 result_type = type2;
3413 if (!result_type)
3415 if (flag_cond_mismatch)
3416 result_type = void_type_node;
3417 else
3419 error ("type mismatch in conditional expression");
3420 return error_mark_node;
3424 /* Merge const and volatile flags of the incoming types. */
3425 result_type
3426 = build_type_variant (result_type,
3427 TREE_READONLY (op1) || TREE_READONLY (op2),
3428 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3430 if (result_type != TREE_TYPE (op1))
3431 op1 = convert_and_check (result_type, op1);
3432 if (result_type != TREE_TYPE (op2))
3433 op2 = convert_and_check (result_type, op2);
3435 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3438 /* Return a compound expression that performs two expressions and
3439 returns the value of the second of them. */
3441 tree
3442 build_compound_expr (tree expr1, tree expr2)
3444 if (!TREE_SIDE_EFFECTS (expr1))
3446 /* The left-hand operand of a comma expression is like an expression
3447 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3448 any side-effects, unless it was explicitly cast to (void). */
3449 if (warn_unused_value)
3451 if (VOID_TYPE_P (TREE_TYPE (expr1))
3452 && (TREE_CODE (expr1) == NOP_EXPR
3453 || TREE_CODE (expr1) == CONVERT_EXPR))
3454 ; /* (void) a, b */
3455 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3456 && TREE_CODE (expr1) == COMPOUND_EXPR
3457 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3458 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3459 ; /* (void) a, (void) b, c */
3460 else
3461 warning (0, "left-hand operand of comma expression has no effect");
3465 /* With -Wunused, we should also warn if the left-hand operand does have
3466 side-effects, but computes a value which is not used. For example, in
3467 `foo() + bar(), baz()' the result of the `+' operator is not used,
3468 so we should issue a warning. */
3469 else if (warn_unused_value)
3470 warn_if_unused_value (expr1, input_location);
3472 if (expr2 == error_mark_node)
3473 return error_mark_node;
3475 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3478 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3480 tree
3481 build_c_cast (tree type, tree expr)
3483 tree value = expr;
3485 if (type == error_mark_node || expr == error_mark_node)
3486 return error_mark_node;
3488 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3489 only in <protocol> qualifications. But when constructing cast expressions,
3490 the protocols do matter and must be kept around. */
3491 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3492 return build1 (NOP_EXPR, type, expr);
3494 type = TYPE_MAIN_VARIANT (type);
3496 if (TREE_CODE (type) == ARRAY_TYPE)
3498 error ("cast specifies array type");
3499 return error_mark_node;
3502 if (TREE_CODE (type) == FUNCTION_TYPE)
3504 error ("cast specifies function type");
3505 return error_mark_node;
3508 if (!VOID_TYPE_P (type))
3510 value = require_complete_type (value);
3511 if (value == error_mark_node)
3512 return error_mark_node;
3515 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3517 if (pedantic)
3519 if (TREE_CODE (type) == RECORD_TYPE
3520 || TREE_CODE (type) == UNION_TYPE)
3521 pedwarn ("ISO C forbids casting nonscalar to the same type");
3524 else if (TREE_CODE (type) == UNION_TYPE)
3526 tree field;
3528 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3529 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3530 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3531 break;
3533 if (field)
3535 tree t;
3537 if (pedantic)
3538 pedwarn ("ISO C forbids casts to union type");
3539 t = digest_init (type,
3540 build_constructor_single (type, field, value),
3541 true, 0);
3542 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3543 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3544 return t;
3546 error ("cast to union type from type not present in union");
3547 return error_mark_node;
3549 else
3551 tree otype, ovalue;
3553 if (type == void_type_node)
3554 return build1 (CONVERT_EXPR, type, value);
3556 otype = TREE_TYPE (value);
3558 /* Optionally warn about potentially worrisome casts. */
3560 if (warn_cast_qual
3561 && TREE_CODE (type) == POINTER_TYPE
3562 && TREE_CODE (otype) == POINTER_TYPE)
3564 tree in_type = type;
3565 tree in_otype = otype;
3566 int added = 0;
3567 int discarded = 0;
3569 /* Check that the qualifiers on IN_TYPE are a superset of
3570 the qualifiers of IN_OTYPE. The outermost level of
3571 POINTER_TYPE nodes is uninteresting and we stop as soon
3572 as we hit a non-POINTER_TYPE node on either type. */
3575 in_otype = TREE_TYPE (in_otype);
3576 in_type = TREE_TYPE (in_type);
3578 /* GNU C allows cv-qualified function types. 'const'
3579 means the function is very pure, 'volatile' means it
3580 can't return. We need to warn when such qualifiers
3581 are added, not when they're taken away. */
3582 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3583 && TREE_CODE (in_type) == FUNCTION_TYPE)
3584 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3585 else
3586 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3588 while (TREE_CODE (in_type) == POINTER_TYPE
3589 && TREE_CODE (in_otype) == POINTER_TYPE);
3591 if (added)
3592 warning (0, "cast adds new qualifiers to function type");
3594 if (discarded)
3595 /* There are qualifiers present in IN_OTYPE that are not
3596 present in IN_TYPE. */
3597 warning (0, "cast discards qualifiers from pointer target type");
3600 /* Warn about possible alignment problems. */
3601 if (STRICT_ALIGNMENT
3602 && TREE_CODE (type) == POINTER_TYPE
3603 && TREE_CODE (otype) == POINTER_TYPE
3604 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3605 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3606 /* Don't warn about opaque types, where the actual alignment
3607 restriction is unknown. */
3608 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3609 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3610 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3611 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3612 warning (OPT_Wcast_align,
3613 "cast increases required alignment of target type");
3615 if (TREE_CODE (type) == INTEGER_TYPE
3616 && TREE_CODE (otype) == POINTER_TYPE
3617 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3618 /* Unlike conversion of integers to pointers, where the
3619 warning is disabled for converting constants because
3620 of cases such as SIG_*, warn about converting constant
3621 pointers to integers. In some cases it may cause unwanted
3622 sign extension, and a warning is appropriate. */
3623 warning (OPT_Wpointer_to_int_cast,
3624 "cast from pointer to integer of different size");
3626 if (TREE_CODE (value) == CALL_EXPR
3627 && TREE_CODE (type) != TREE_CODE (otype))
3628 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3629 "to non-matching type %qT", otype, type);
3631 if (TREE_CODE (type) == POINTER_TYPE
3632 && TREE_CODE (otype) == INTEGER_TYPE
3633 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3634 /* Don't warn about converting any constant. */
3635 && !TREE_CONSTANT (value))
3636 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3637 "of different size");
3639 strict_aliasing_warning (otype, type, expr);
3641 /* If pedantic, warn for conversions between function and object
3642 pointer types, except for converting a null pointer constant
3643 to function pointer type. */
3644 if (pedantic
3645 && TREE_CODE (type) == POINTER_TYPE
3646 && TREE_CODE (otype) == POINTER_TYPE
3647 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3648 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3649 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3651 if (pedantic
3652 && TREE_CODE (type) == POINTER_TYPE
3653 && TREE_CODE (otype) == POINTER_TYPE
3654 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3655 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3656 && !null_pointer_constant_p (value))
3657 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3659 ovalue = value;
3660 value = convert (type, value);
3662 /* Ignore any integer overflow caused by the cast. */
3663 if (TREE_CODE (value) == INTEGER_CST)
3665 if (CONSTANT_CLASS_P (ovalue)
3666 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3668 /* Avoid clobbering a shared constant. */
3669 value = copy_node (value);
3670 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3671 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3673 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3674 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3675 value = build_int_cst_wide (TREE_TYPE (value),
3676 TREE_INT_CST_LOW (value),
3677 TREE_INT_CST_HIGH (value));
3681 /* Don't let a cast be an lvalue. */
3682 if (value == expr)
3683 value = non_lvalue (value);
3685 return value;
3688 /* Interpret a cast of expression EXPR to type TYPE. */
3689 tree
3690 c_cast_expr (struct c_type_name *type_name, tree expr)
3692 tree type;
3693 int saved_wsp = warn_strict_prototypes;
3695 /* This avoids warnings about unprototyped casts on
3696 integers. E.g. "#define SIG_DFL (void(*)())0". */
3697 if (TREE_CODE (expr) == INTEGER_CST)
3698 warn_strict_prototypes = 0;
3699 type = groktypename (type_name);
3700 warn_strict_prototypes = saved_wsp;
3702 return build_c_cast (type, expr);
3705 /* Build an assignment expression of lvalue LHS from value RHS.
3706 MODIFYCODE is the code for a binary operator that we use
3707 to combine the old value of LHS with RHS to get the new value.
3708 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3710 tree
3711 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3713 tree result;
3714 tree newrhs;
3715 tree lhstype = TREE_TYPE (lhs);
3716 tree olhstype = lhstype;
3718 /* Types that aren't fully specified cannot be used in assignments. */
3719 lhs = require_complete_type (lhs);
3721 /* Avoid duplicate error messages from operands that had errors. */
3722 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3723 return error_mark_node;
3725 if (!lvalue_or_else (lhs, lv_assign))
3726 return error_mark_node;
3728 STRIP_TYPE_NOPS (rhs);
3730 newrhs = rhs;
3732 /* If a binary op has been requested, combine the old LHS value with the RHS
3733 producing the value we should actually store into the LHS. */
3735 if (modifycode != NOP_EXPR)
3737 lhs = stabilize_reference (lhs);
3738 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3741 /* Give an error for storing in something that is 'const'. */
3743 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3744 || ((TREE_CODE (lhstype) == RECORD_TYPE
3745 || TREE_CODE (lhstype) == UNION_TYPE)
3746 && C_TYPE_FIELDS_READONLY (lhstype)))
3748 readonly_error (lhs, lv_assign);
3749 return error_mark_node;
3752 /* If storing into a structure or union member,
3753 it has probably been given type `int'.
3754 Compute the type that would go with
3755 the actual amount of storage the member occupies. */
3757 if (TREE_CODE (lhs) == COMPONENT_REF
3758 && (TREE_CODE (lhstype) == INTEGER_TYPE
3759 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3760 || TREE_CODE (lhstype) == REAL_TYPE
3761 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3762 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3764 /* If storing in a field that is in actuality a short or narrower than one,
3765 we must store in the field in its actual type. */
3767 if (lhstype != TREE_TYPE (lhs))
3769 lhs = copy_node (lhs);
3770 TREE_TYPE (lhs) = lhstype;
3773 /* Convert new value to destination type. */
3775 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3776 NULL_TREE, NULL_TREE, 0);
3777 if (TREE_CODE (newrhs) == ERROR_MARK)
3778 return error_mark_node;
3780 /* Emit ObjC write barrier, if necessary. */
3781 if (c_dialect_objc () && flag_objc_gc)
3783 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3784 if (result)
3785 return result;
3788 /* Scan operands. */
3790 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3791 TREE_SIDE_EFFECTS (result) = 1;
3793 /* If we got the LHS in a different type for storing in,
3794 convert the result back to the nominal type of LHS
3795 so that the value we return always has the same type
3796 as the LHS argument. */
3798 if (olhstype == TREE_TYPE (result))
3799 return result;
3800 return convert_for_assignment (olhstype, result, ic_assign,
3801 NULL_TREE, NULL_TREE, 0);
3804 /* Convert value RHS to type TYPE as preparation for an assignment
3805 to an lvalue of type TYPE.
3806 The real work of conversion is done by `convert'.
3807 The purpose of this function is to generate error messages
3808 for assignments that are not allowed in C.
3809 ERRTYPE says whether it is argument passing, assignment,
3810 initialization or return.
3812 FUNCTION is a tree for the function being called.
3813 PARMNUM is the number of the argument, for printing in error messages. */
3815 static tree
3816 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3817 tree fundecl, tree function, int parmnum)
3819 enum tree_code codel = TREE_CODE (type);
3820 tree rhstype;
3821 enum tree_code coder;
3822 tree rname = NULL_TREE;
3823 bool objc_ok = false;
3825 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3827 tree selector;
3828 /* Change pointer to function to the function itself for
3829 diagnostics. */
3830 if (TREE_CODE (function) == ADDR_EXPR
3831 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3832 function = TREE_OPERAND (function, 0);
3834 /* Handle an ObjC selector specially for diagnostics. */
3835 selector = objc_message_selector ();
3836 rname = function;
3837 if (selector && parmnum > 2)
3839 rname = selector;
3840 parmnum -= 2;
3844 /* This macro is used to emit diagnostics to ensure that all format
3845 strings are complete sentences, visible to gettext and checked at
3846 compile time. */
3847 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3848 do { \
3849 switch (errtype) \
3851 case ic_argpass: \
3852 pedwarn (AR, parmnum, rname); \
3853 break; \
3854 case ic_argpass_nonproto: \
3855 warning (0, AR, parmnum, rname); \
3856 break; \
3857 case ic_assign: \
3858 pedwarn (AS); \
3859 break; \
3860 case ic_init: \
3861 pedwarn (IN); \
3862 break; \
3863 case ic_return: \
3864 pedwarn (RE); \
3865 break; \
3866 default: \
3867 gcc_unreachable (); \
3869 } while (0)
3871 STRIP_TYPE_NOPS (rhs);
3873 if (optimize && TREE_CODE (rhs) == VAR_DECL
3874 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3875 rhs = decl_constant_value_for_broken_optimization (rhs);
3877 rhstype = TREE_TYPE (rhs);
3878 coder = TREE_CODE (rhstype);
3880 if (coder == ERROR_MARK)
3881 return error_mark_node;
3883 if (c_dialect_objc ())
3885 int parmno;
3887 switch (errtype)
3889 case ic_return:
3890 parmno = 0;
3891 break;
3893 case ic_assign:
3894 parmno = -1;
3895 break;
3897 case ic_init:
3898 parmno = -2;
3899 break;
3901 default:
3902 parmno = parmnum;
3903 break;
3906 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3909 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3911 overflow_warning (rhs);
3912 return rhs;
3915 if (coder == VOID_TYPE)
3917 /* Except for passing an argument to an unprototyped function,
3918 this is a constraint violation. When passing an argument to
3919 an unprototyped function, it is compile-time undefined;
3920 making it a constraint in that case was rejected in
3921 DR#252. */
3922 error ("void value not ignored as it ought to be");
3923 return error_mark_node;
3925 rhs = require_complete_type (rhs);
3926 if (rhs == error_mark_node)
3927 return error_mark_node;
3928 /* A type converts to a reference to it.
3929 This code doesn't fully support references, it's just for the
3930 special case of va_start and va_copy. */
3931 if (codel == REFERENCE_TYPE
3932 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3934 if (!lvalue_p (rhs))
3936 error ("cannot pass rvalue to reference parameter");
3937 return error_mark_node;
3939 if (!c_mark_addressable (rhs))
3940 return error_mark_node;
3941 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3943 /* We already know that these two types are compatible, but they
3944 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3945 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3946 likely to be va_list, a typedef to __builtin_va_list, which
3947 is different enough that it will cause problems later. */
3948 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3949 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3951 rhs = build1 (NOP_EXPR, type, rhs);
3952 return rhs;
3954 /* Some types can interconvert without explicit casts. */
3955 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3956 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3957 return convert (type, rhs);
3958 /* Arithmetic types all interconvert, and enum is treated like int. */
3959 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3960 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3961 || codel == BOOLEAN_TYPE)
3962 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3963 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3964 || coder == BOOLEAN_TYPE))
3965 return convert_and_check (type, rhs);
3967 /* Aggregates in different TUs might need conversion. */
3968 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
3969 && codel == coder
3970 && comptypes (type, rhstype))
3971 return convert_and_check (type, rhs);
3973 /* Conversion to a transparent union from its member types.
3974 This applies only to function arguments. */
3975 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3976 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3978 tree memb, marginal_memb = NULL_TREE;
3980 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3982 tree memb_type = TREE_TYPE (memb);
3984 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3985 TYPE_MAIN_VARIANT (rhstype)))
3986 break;
3988 if (TREE_CODE (memb_type) != POINTER_TYPE)
3989 continue;
3991 if (coder == POINTER_TYPE)
3993 tree ttl = TREE_TYPE (memb_type);
3994 tree ttr = TREE_TYPE (rhstype);
3996 /* Any non-function converts to a [const][volatile] void *
3997 and vice versa; otherwise, targets must be the same.
3998 Meanwhile, the lhs target must have all the qualifiers of
3999 the rhs. */
4000 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4001 || comp_target_types (memb_type, rhstype))
4003 /* If this type won't generate any warnings, use it. */
4004 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4005 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4006 && TREE_CODE (ttl) == FUNCTION_TYPE)
4007 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4008 == TYPE_QUALS (ttr))
4009 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4010 == TYPE_QUALS (ttl))))
4011 break;
4013 /* Keep looking for a better type, but remember this one. */
4014 if (!marginal_memb)
4015 marginal_memb = memb;
4019 /* Can convert integer zero to any pointer type. */
4020 if (null_pointer_constant_p (rhs))
4022 rhs = null_pointer_node;
4023 break;
4027 if (memb || marginal_memb)
4029 if (!memb)
4031 /* We have only a marginally acceptable member type;
4032 it needs a warning. */
4033 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4034 tree ttr = TREE_TYPE (rhstype);
4036 /* Const and volatile mean something different for function
4037 types, so the usual warnings are not appropriate. */
4038 if (TREE_CODE (ttr) == FUNCTION_TYPE
4039 && TREE_CODE (ttl) == FUNCTION_TYPE)
4041 /* Because const and volatile on functions are
4042 restrictions that say the function will not do
4043 certain things, it is okay to use a const or volatile
4044 function where an ordinary one is wanted, but not
4045 vice-versa. */
4046 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4047 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
4048 "makes qualified function "
4049 "pointer from unqualified"),
4050 G_("assignment makes qualified "
4051 "function pointer from "
4052 "unqualified"),
4053 G_("initialization makes qualified "
4054 "function pointer from "
4055 "unqualified"),
4056 G_("return makes qualified function "
4057 "pointer from unqualified"));
4059 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4060 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4061 "qualifiers from pointer target type"),
4062 G_("assignment discards qualifiers "
4063 "from pointer target type"),
4064 G_("initialization discards qualifiers "
4065 "from pointer target type"),
4066 G_("return discards qualifiers from "
4067 "pointer target type"));
4069 memb = marginal_memb;
4072 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4073 pedwarn ("ISO C prohibits argument conversion to union type");
4075 return build_constructor_single (type, memb, rhs);
4079 /* Conversions among pointers */
4080 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4081 && (coder == codel))
4083 tree ttl = TREE_TYPE (type);
4084 tree ttr = TREE_TYPE (rhstype);
4085 tree mvl = ttl;
4086 tree mvr = ttr;
4087 bool is_opaque_pointer;
4088 int target_cmp = 0; /* Cache comp_target_types () result. */
4090 if (TREE_CODE (mvl) != ARRAY_TYPE)
4091 mvl = TYPE_MAIN_VARIANT (mvl);
4092 if (TREE_CODE (mvr) != ARRAY_TYPE)
4093 mvr = TYPE_MAIN_VARIANT (mvr);
4094 /* Opaque pointers are treated like void pointers. */
4095 is_opaque_pointer = (targetm.vector_opaque_p (type)
4096 || targetm.vector_opaque_p (rhstype))
4097 && TREE_CODE (ttl) == VECTOR_TYPE
4098 && TREE_CODE (ttr) == VECTOR_TYPE;
4100 /* C++ does not allow the implicit conversion void* -> T*. However,
4101 for the purpose of reducing the number of false positives, we
4102 tolerate the special case of
4104 int *p = NULL;
4106 where NULL is typically defined in C to be '(void *) 0'. */
4107 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4108 warning (OPT_Wc___compat, "request for implicit conversion from "
4109 "%qT to %qT not permitted in C++", rhstype, type);
4111 /* Check if the right-hand side has a format attribute but the
4112 left-hand side doesn't. */
4113 if (warn_missing_format_attribute
4114 && check_missing_format_attribute (type, rhstype))
4116 switch (errtype)
4118 case ic_argpass:
4119 case ic_argpass_nonproto:
4120 warning (OPT_Wmissing_format_attribute,
4121 "argument %d of %qE might be "
4122 "a candidate for a format attribute",
4123 parmnum, rname);
4124 break;
4125 case ic_assign:
4126 warning (OPT_Wmissing_format_attribute,
4127 "assignment left-hand side might be "
4128 "a candidate for a format attribute");
4129 break;
4130 case ic_init:
4131 warning (OPT_Wmissing_format_attribute,
4132 "initialization left-hand side might be "
4133 "a candidate for a format attribute");
4134 break;
4135 case ic_return:
4136 warning (OPT_Wmissing_format_attribute,
4137 "return type might be "
4138 "a candidate for a format attribute");
4139 break;
4140 default:
4141 gcc_unreachable ();
4145 /* Any non-function converts to a [const][volatile] void *
4146 and vice versa; otherwise, targets must be the same.
4147 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4148 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4149 || (target_cmp = comp_target_types (type, rhstype))
4150 || is_opaque_pointer
4151 || (c_common_unsigned_type (mvl)
4152 == c_common_unsigned_type (mvr)))
4154 if (pedantic
4155 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4157 (VOID_TYPE_P (ttr)
4158 && !null_pointer_constant_p (rhs)
4159 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4160 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4161 "%qE between function pointer "
4162 "and %<void *%>"),
4163 G_("ISO C forbids assignment between "
4164 "function pointer and %<void *%>"),
4165 G_("ISO C forbids initialization between "
4166 "function pointer and %<void *%>"),
4167 G_("ISO C forbids return between function "
4168 "pointer and %<void *%>"));
4169 /* Const and volatile mean something different for function types,
4170 so the usual warnings are not appropriate. */
4171 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4172 && TREE_CODE (ttl) != FUNCTION_TYPE)
4174 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4176 /* Types differing only by the presence of the 'volatile'
4177 qualifier are acceptable if the 'volatile' has been added
4178 in by the Objective-C EH machinery. */
4179 if (!objc_type_quals_match (ttl, ttr))
4180 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4181 "qualifiers from pointer target type"),
4182 G_("assignment discards qualifiers "
4183 "from pointer target type"),
4184 G_("initialization discards qualifiers "
4185 "from pointer target type"),
4186 G_("return discards qualifiers from "
4187 "pointer target type"));
4189 /* If this is not a case of ignoring a mismatch in signedness,
4190 no warning. */
4191 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4192 || target_cmp)
4194 /* If there is a mismatch, do warn. */
4195 else if (warn_pointer_sign)
4196 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4197 "%d of %qE differ in signedness"),
4198 G_("pointer targets in assignment "
4199 "differ in signedness"),
4200 G_("pointer targets in initialization "
4201 "differ in signedness"),
4202 G_("pointer targets in return differ "
4203 "in signedness"));
4205 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4206 && TREE_CODE (ttr) == FUNCTION_TYPE)
4208 /* Because const and volatile on functions are restrictions
4209 that say the function will not do certain things,
4210 it is okay to use a const or volatile function
4211 where an ordinary one is wanted, but not vice-versa. */
4212 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4213 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4214 "qualified function pointer "
4215 "from unqualified"),
4216 G_("assignment makes qualified function "
4217 "pointer from unqualified"),
4218 G_("initialization makes qualified "
4219 "function pointer from unqualified"),
4220 G_("return makes qualified function "
4221 "pointer from unqualified"));
4224 else
4225 /* Avoid warning about the volatile ObjC EH puts on decls. */
4226 if (!objc_ok)
4227 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4228 "incompatible pointer type"),
4229 G_("assignment from incompatible pointer type"),
4230 G_("initialization from incompatible "
4231 "pointer type"),
4232 G_("return from incompatible pointer type"));
4234 return convert (type, rhs);
4236 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4238 /* ??? This should not be an error when inlining calls to
4239 unprototyped functions. */
4240 error ("invalid use of non-lvalue array");
4241 return error_mark_node;
4243 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4245 /* An explicit constant 0 can convert to a pointer,
4246 or one that results from arithmetic, even including
4247 a cast to integer type. */
4248 if (!null_pointer_constant_p (rhs))
4249 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4250 "pointer from integer without a cast"),
4251 G_("assignment makes pointer from integer "
4252 "without a cast"),
4253 G_("initialization makes pointer from "
4254 "integer without a cast"),
4255 G_("return makes pointer from integer "
4256 "without a cast"));
4258 return convert (type, rhs);
4260 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4262 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4263 "from pointer without a cast"),
4264 G_("assignment makes integer from pointer "
4265 "without a cast"),
4266 G_("initialization makes integer from pointer "
4267 "without a cast"),
4268 G_("return makes integer from pointer "
4269 "without a cast"));
4270 return convert (type, rhs);
4272 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4273 return convert (type, rhs);
4275 switch (errtype)
4277 case ic_argpass:
4278 case ic_argpass_nonproto:
4279 /* ??? This should not be an error when inlining calls to
4280 unprototyped functions. */
4281 error ("incompatible type for argument %d of %qE", parmnum, rname);
4282 break;
4283 case ic_assign:
4284 error ("incompatible types in assignment");
4285 break;
4286 case ic_init:
4287 error ("incompatible types in initialization");
4288 break;
4289 case ic_return:
4290 error ("incompatible types in return");
4291 break;
4292 default:
4293 gcc_unreachable ();
4296 return error_mark_node;
4299 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4300 is used for error and warning reporting and indicates which argument
4301 is being processed. */
4303 tree
4304 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4306 tree ret, type;
4308 /* If FN was prototyped at the call site, the value has been converted
4309 already in convert_arguments.
4310 However, we might see a prototype now that was not in place when
4311 the function call was seen, so check that the VALUE actually matches
4312 PARM before taking an early exit. */
4313 if (!value
4314 || (TYPE_ARG_TYPES (TREE_TYPE (fn))
4315 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
4316 == TYPE_MAIN_VARIANT (TREE_TYPE (value)))))
4317 return value;
4319 type = TREE_TYPE (parm);
4320 ret = convert_for_assignment (type, value,
4321 ic_argpass_nonproto, fn,
4322 fn, argnum);
4323 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4324 && INTEGRAL_TYPE_P (type)
4325 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4326 ret = default_conversion (ret);
4327 return ret;
4330 /* If VALUE is a compound expr all of whose expressions are constant, then
4331 return its value. Otherwise, return error_mark_node.
4333 This is for handling COMPOUND_EXPRs as initializer elements
4334 which is allowed with a warning when -pedantic is specified. */
4336 static tree
4337 valid_compound_expr_initializer (tree value, tree endtype)
4339 if (TREE_CODE (value) == COMPOUND_EXPR)
4341 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4342 == error_mark_node)
4343 return error_mark_node;
4344 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4345 endtype);
4347 else if (!initializer_constant_valid_p (value, endtype))
4348 return error_mark_node;
4349 else
4350 return value;
4353 /* Perform appropriate conversions on the initial value of a variable,
4354 store it in the declaration DECL,
4355 and print any error messages that are appropriate.
4356 If the init is invalid, store an ERROR_MARK. */
4358 void
4359 store_init_value (tree decl, tree init)
4361 tree value, type;
4363 /* If variable's type was invalidly declared, just ignore it. */
4365 type = TREE_TYPE (decl);
4366 if (TREE_CODE (type) == ERROR_MARK)
4367 return;
4369 /* Digest the specified initializer into an expression. */
4371 value = digest_init (type, init, true, TREE_STATIC (decl));
4373 /* Store the expression if valid; else report error. */
4375 if (!in_system_header
4376 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4377 warning (OPT_Wtraditional, "traditional C rejects automatic "
4378 "aggregate initialization");
4380 DECL_INITIAL (decl) = value;
4382 /* ANSI wants warnings about out-of-range constant initializers. */
4383 STRIP_TYPE_NOPS (value);
4384 constant_expression_warning (value);
4386 /* Check if we need to set array size from compound literal size. */
4387 if (TREE_CODE (type) == ARRAY_TYPE
4388 && TYPE_DOMAIN (type) == 0
4389 && value != error_mark_node)
4391 tree inside_init = init;
4393 STRIP_TYPE_NOPS (inside_init);
4394 inside_init = fold (inside_init);
4396 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4398 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4400 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4402 /* For int foo[] = (int [3]){1}; we need to set array size
4403 now since later on array initializer will be just the
4404 brace enclosed list of the compound literal. */
4405 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4406 TREE_TYPE (decl) = type;
4407 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4408 layout_type (type);
4409 layout_decl (cldecl, 0);
4415 /* Methods for storing and printing names for error messages. */
4417 /* Implement a spelling stack that allows components of a name to be pushed
4418 and popped. Each element on the stack is this structure. */
4420 struct spelling
4422 int kind;
4423 union
4425 unsigned HOST_WIDE_INT i;
4426 const char *s;
4427 } u;
4430 #define SPELLING_STRING 1
4431 #define SPELLING_MEMBER 2
4432 #define SPELLING_BOUNDS 3
4434 static struct spelling *spelling; /* Next stack element (unused). */
4435 static struct spelling *spelling_base; /* Spelling stack base. */
4436 static int spelling_size; /* Size of the spelling stack. */
4438 /* Macros to save and restore the spelling stack around push_... functions.
4439 Alternative to SAVE_SPELLING_STACK. */
4441 #define SPELLING_DEPTH() (spelling - spelling_base)
4442 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4444 /* Push an element on the spelling stack with type KIND and assign VALUE
4445 to MEMBER. */
4447 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4449 int depth = SPELLING_DEPTH (); \
4451 if (depth >= spelling_size) \
4453 spelling_size += 10; \
4454 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4455 spelling_size); \
4456 RESTORE_SPELLING_DEPTH (depth); \
4459 spelling->kind = (KIND); \
4460 spelling->MEMBER = (VALUE); \
4461 spelling++; \
4464 /* Push STRING on the stack. Printed literally. */
4466 static void
4467 push_string (const char *string)
4469 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4472 /* Push a member name on the stack. Printed as '.' STRING. */
4474 static void
4475 push_member_name (tree decl)
4477 const char *const string
4478 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4479 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4482 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4484 static void
4485 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4487 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4490 /* Compute the maximum size in bytes of the printed spelling. */
4492 static int
4493 spelling_length (void)
4495 int size = 0;
4496 struct spelling *p;
4498 for (p = spelling_base; p < spelling; p++)
4500 if (p->kind == SPELLING_BOUNDS)
4501 size += 25;
4502 else
4503 size += strlen (p->u.s) + 1;
4506 return size;
4509 /* Print the spelling to BUFFER and return it. */
4511 static char *
4512 print_spelling (char *buffer)
4514 char *d = buffer;
4515 struct spelling *p;
4517 for (p = spelling_base; p < spelling; p++)
4518 if (p->kind == SPELLING_BOUNDS)
4520 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4521 d += strlen (d);
4523 else
4525 const char *s;
4526 if (p->kind == SPELLING_MEMBER)
4527 *d++ = '.';
4528 for (s = p->u.s; (*d = *s++); d++)
4531 *d++ = '\0';
4532 return buffer;
4535 /* Issue an error message for a bad initializer component.
4536 MSGID identifies the message.
4537 The component name is taken from the spelling stack. */
4539 void
4540 error_init (const char *msgid)
4542 char *ofwhat;
4544 error ("%s", _(msgid));
4545 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4546 if (*ofwhat)
4547 error ("(near initialization for %qs)", ofwhat);
4550 /* Issue a pedantic warning for a bad initializer component.
4551 MSGID identifies the message.
4552 The component name is taken from the spelling stack. */
4554 void
4555 pedwarn_init (const char *msgid)
4557 char *ofwhat;
4559 pedwarn ("%s", _(msgid));
4560 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4561 if (*ofwhat)
4562 pedwarn ("(near initialization for %qs)", ofwhat);
4565 /* Issue a warning for a bad initializer component.
4566 MSGID identifies the message.
4567 The component name is taken from the spelling stack. */
4569 static void
4570 warning_init (const char *msgid)
4572 char *ofwhat;
4574 warning (0, "%s", _(msgid));
4575 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4576 if (*ofwhat)
4577 warning (0, "(near initialization for %qs)", ofwhat);
4580 /* If TYPE is an array type and EXPR is a parenthesized string
4581 constant, warn if pedantic that EXPR is being used to initialize an
4582 object of type TYPE. */
4584 void
4585 maybe_warn_string_init (tree type, struct c_expr expr)
4587 if (pedantic
4588 && TREE_CODE (type) == ARRAY_TYPE
4589 && TREE_CODE (expr.value) == STRING_CST
4590 && expr.original_code != STRING_CST)
4591 pedwarn_init ("array initialized from parenthesized string constant");
4594 /* Digest the parser output INIT as an initializer for type TYPE.
4595 Return a C expression of type TYPE to represent the initial value.
4597 If INIT is a string constant, STRICT_STRING is true if it is
4598 unparenthesized or we should not warn here for it being parenthesized.
4599 For other types of INIT, STRICT_STRING is not used.
4601 REQUIRE_CONSTANT requests an error if non-constant initializers or
4602 elements are seen. */
4604 static tree
4605 digest_init (tree type, tree init, bool strict_string, int require_constant)
4607 enum tree_code code = TREE_CODE (type);
4608 tree inside_init = init;
4610 if (type == error_mark_node
4611 || !init
4612 || init == error_mark_node
4613 || TREE_TYPE (init) == error_mark_node)
4614 return error_mark_node;
4616 STRIP_TYPE_NOPS (inside_init);
4618 inside_init = fold (inside_init);
4620 /* Initialization of an array of chars from a string constant
4621 optionally enclosed in braces. */
4623 if (code == ARRAY_TYPE && inside_init
4624 && TREE_CODE (inside_init) == STRING_CST)
4626 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4627 /* Note that an array could be both an array of character type
4628 and an array of wchar_t if wchar_t is signed char or unsigned
4629 char. */
4630 bool char_array = (typ1 == char_type_node
4631 || typ1 == signed_char_type_node
4632 || typ1 == unsigned_char_type_node);
4633 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4634 if (char_array || wchar_array)
4636 struct c_expr expr;
4637 bool char_string;
4638 expr.value = inside_init;
4639 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4640 maybe_warn_string_init (type, expr);
4642 char_string
4643 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4644 == char_type_node);
4646 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4647 TYPE_MAIN_VARIANT (type)))
4648 return inside_init;
4650 if (!wchar_array && !char_string)
4652 error_init ("char-array initialized from wide string");
4653 return error_mark_node;
4655 if (char_string && !char_array)
4657 error_init ("wchar_t-array initialized from non-wide string");
4658 return error_mark_node;
4661 TREE_TYPE (inside_init) = type;
4662 if (TYPE_DOMAIN (type) != 0
4663 && TYPE_SIZE (type) != 0
4664 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4665 /* Subtract 1 (or sizeof (wchar_t))
4666 because it's ok to ignore the terminating null char
4667 that is counted in the length of the constant. */
4668 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4669 TREE_STRING_LENGTH (inside_init)
4670 - ((TYPE_PRECISION (typ1)
4671 != TYPE_PRECISION (char_type_node))
4672 ? (TYPE_PRECISION (wchar_type_node)
4673 / BITS_PER_UNIT)
4674 : 1)))
4675 pedwarn_init ("initializer-string for array of chars is too long");
4677 return inside_init;
4679 else if (INTEGRAL_TYPE_P (typ1))
4681 error_init ("array of inappropriate type initialized "
4682 "from string constant");
4683 return error_mark_node;
4687 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4688 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4689 below and handle as a constructor. */
4690 if (code == VECTOR_TYPE
4691 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4692 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4693 && TREE_CONSTANT (inside_init))
4695 if (TREE_CODE (inside_init) == VECTOR_CST
4696 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4697 TYPE_MAIN_VARIANT (type)))
4698 return inside_init;
4700 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4702 unsigned HOST_WIDE_INT ix;
4703 tree value;
4704 bool constant_p = true;
4706 /* Iterate through elements and check if all constructor
4707 elements are *_CSTs. */
4708 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4709 if (!CONSTANT_CLASS_P (value))
4711 constant_p = false;
4712 break;
4715 if (constant_p)
4716 return build_vector_from_ctor (type,
4717 CONSTRUCTOR_ELTS (inside_init));
4721 /* Any type can be initialized
4722 from an expression of the same type, optionally with braces. */
4724 if (inside_init && TREE_TYPE (inside_init) != 0
4725 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4726 TYPE_MAIN_VARIANT (type))
4727 || (code == ARRAY_TYPE
4728 && comptypes (TREE_TYPE (inside_init), type))
4729 || (code == VECTOR_TYPE
4730 && comptypes (TREE_TYPE (inside_init), type))
4731 || (code == POINTER_TYPE
4732 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4733 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4734 TREE_TYPE (type)))))
4736 if (code == POINTER_TYPE)
4738 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4740 if (TREE_CODE (inside_init) == STRING_CST
4741 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4742 inside_init = array_to_pointer_conversion (inside_init);
4743 else
4745 error_init ("invalid use of non-lvalue array");
4746 return error_mark_node;
4751 if (code == VECTOR_TYPE)
4752 /* Although the types are compatible, we may require a
4753 conversion. */
4754 inside_init = convert (type, inside_init);
4756 if (require_constant
4757 && (code == VECTOR_TYPE || !flag_isoc99)
4758 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4760 /* As an extension, allow initializing objects with static storage
4761 duration with compound literals (which are then treated just as
4762 the brace enclosed list they contain). Also allow this for
4763 vectors, as we can only assign them with compound literals. */
4764 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4765 inside_init = DECL_INITIAL (decl);
4768 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4769 && TREE_CODE (inside_init) != CONSTRUCTOR)
4771 error_init ("array initialized from non-constant array expression");
4772 return error_mark_node;
4775 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4776 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4778 /* Compound expressions can only occur here if -pedantic or
4779 -pedantic-errors is specified. In the later case, we always want
4780 an error. In the former case, we simply want a warning. */
4781 if (require_constant && pedantic
4782 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4784 inside_init
4785 = valid_compound_expr_initializer (inside_init,
4786 TREE_TYPE (inside_init));
4787 if (inside_init == error_mark_node)
4788 error_init ("initializer element is not constant");
4789 else
4790 pedwarn_init ("initializer element is not constant");
4791 if (flag_pedantic_errors)
4792 inside_init = error_mark_node;
4794 else if (require_constant
4795 && !initializer_constant_valid_p (inside_init,
4796 TREE_TYPE (inside_init)))
4798 error_init ("initializer element is not constant");
4799 inside_init = error_mark_node;
4802 /* Added to enable additional -Wmissing-format-attribute warnings. */
4803 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4804 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4805 NULL_TREE, 0);
4806 return inside_init;
4809 /* Handle scalar types, including conversions. */
4811 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4812 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4813 || code == VECTOR_TYPE)
4815 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4816 && (TREE_CODE (init) == STRING_CST
4817 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4818 init = array_to_pointer_conversion (init);
4819 inside_init
4820 = convert_for_assignment (type, init, ic_init,
4821 NULL_TREE, NULL_TREE, 0);
4823 /* Check to see if we have already given an error message. */
4824 if (inside_init == error_mark_node)
4826 else if (require_constant && !TREE_CONSTANT (inside_init))
4828 error_init ("initializer element is not constant");
4829 inside_init = error_mark_node;
4831 else if (require_constant
4832 && !initializer_constant_valid_p (inside_init,
4833 TREE_TYPE (inside_init)))
4835 error_init ("initializer element is not computable at load time");
4836 inside_init = error_mark_node;
4839 return inside_init;
4842 /* Come here only for records and arrays. */
4844 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4846 error_init ("variable-sized object may not be initialized");
4847 return error_mark_node;
4850 error_init ("invalid initializer");
4851 return error_mark_node;
4854 /* Handle initializers that use braces. */
4856 /* Type of object we are accumulating a constructor for.
4857 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4858 static tree constructor_type;
4860 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4861 left to fill. */
4862 static tree constructor_fields;
4864 /* For an ARRAY_TYPE, this is the specified index
4865 at which to store the next element we get. */
4866 static tree constructor_index;
4868 /* For an ARRAY_TYPE, this is the maximum index. */
4869 static tree constructor_max_index;
4871 /* For a RECORD_TYPE, this is the first field not yet written out. */
4872 static tree constructor_unfilled_fields;
4874 /* For an ARRAY_TYPE, this is the index of the first element
4875 not yet written out. */
4876 static tree constructor_unfilled_index;
4878 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4879 This is so we can generate gaps between fields, when appropriate. */
4880 static tree constructor_bit_index;
4882 /* If we are saving up the elements rather than allocating them,
4883 this is the list of elements so far (in reverse order,
4884 most recent first). */
4885 static VEC(constructor_elt,gc) *constructor_elements;
4887 /* 1 if constructor should be incrementally stored into a constructor chain,
4888 0 if all the elements should be kept in AVL tree. */
4889 static int constructor_incremental;
4891 /* 1 if so far this constructor's elements are all compile-time constants. */
4892 static int constructor_constant;
4894 /* 1 if so far this constructor's elements are all valid address constants. */
4895 static int constructor_simple;
4897 /* 1 if this constructor is erroneous so far. */
4898 static int constructor_erroneous;
4900 /* Structure for managing pending initializer elements, organized as an
4901 AVL tree. */
4903 struct init_node
4905 struct init_node *left, *right;
4906 struct init_node *parent;
4907 int balance;
4908 tree purpose;
4909 tree value;
4912 /* Tree of pending elements at this constructor level.
4913 These are elements encountered out of order
4914 which belong at places we haven't reached yet in actually
4915 writing the output.
4916 Will never hold tree nodes across GC runs. */
4917 static struct init_node *constructor_pending_elts;
4919 /* The SPELLING_DEPTH of this constructor. */
4920 static int constructor_depth;
4922 /* DECL node for which an initializer is being read.
4923 0 means we are reading a constructor expression
4924 such as (struct foo) {...}. */
4925 static tree constructor_decl;
4927 /* Nonzero if this is an initializer for a top-level decl. */
4928 static int constructor_top_level;
4930 /* Nonzero if there were any member designators in this initializer. */
4931 static int constructor_designated;
4933 /* Nesting depth of designator list. */
4934 static int designator_depth;
4936 /* Nonzero if there were diagnosed errors in this designator list. */
4937 static int designator_erroneous;
4940 /* This stack has a level for each implicit or explicit level of
4941 structuring in the initializer, including the outermost one. It
4942 saves the values of most of the variables above. */
4944 struct constructor_range_stack;
4946 struct constructor_stack
4948 struct constructor_stack *next;
4949 tree type;
4950 tree fields;
4951 tree index;
4952 tree max_index;
4953 tree unfilled_index;
4954 tree unfilled_fields;
4955 tree bit_index;
4956 VEC(constructor_elt,gc) *elements;
4957 struct init_node *pending_elts;
4958 int offset;
4959 int depth;
4960 /* If value nonzero, this value should replace the entire
4961 constructor at this level. */
4962 struct c_expr replacement_value;
4963 struct constructor_range_stack *range_stack;
4964 char constant;
4965 char simple;
4966 char implicit;
4967 char erroneous;
4968 char outer;
4969 char incremental;
4970 char designated;
4973 static struct constructor_stack *constructor_stack;
4975 /* This stack represents designators from some range designator up to
4976 the last designator in the list. */
4978 struct constructor_range_stack
4980 struct constructor_range_stack *next, *prev;
4981 struct constructor_stack *stack;
4982 tree range_start;
4983 tree index;
4984 tree range_end;
4985 tree fields;
4988 static struct constructor_range_stack *constructor_range_stack;
4990 /* This stack records separate initializers that are nested.
4991 Nested initializers can't happen in ANSI C, but GNU C allows them
4992 in cases like { ... (struct foo) { ... } ... }. */
4994 struct initializer_stack
4996 struct initializer_stack *next;
4997 tree decl;
4998 struct constructor_stack *constructor_stack;
4999 struct constructor_range_stack *constructor_range_stack;
5000 VEC(constructor_elt,gc) *elements;
5001 struct spelling *spelling;
5002 struct spelling *spelling_base;
5003 int spelling_size;
5004 char top_level;
5005 char require_constant_value;
5006 char require_constant_elements;
5009 static struct initializer_stack *initializer_stack;
5011 /* Prepare to parse and output the initializer for variable DECL. */
5013 void
5014 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5016 const char *locus;
5017 struct initializer_stack *p = XNEW (struct initializer_stack);
5019 p->decl = constructor_decl;
5020 p->require_constant_value = require_constant_value;
5021 p->require_constant_elements = require_constant_elements;
5022 p->constructor_stack = constructor_stack;
5023 p->constructor_range_stack = constructor_range_stack;
5024 p->elements = constructor_elements;
5025 p->spelling = spelling;
5026 p->spelling_base = spelling_base;
5027 p->spelling_size = spelling_size;
5028 p->top_level = constructor_top_level;
5029 p->next = initializer_stack;
5030 initializer_stack = p;
5032 constructor_decl = decl;
5033 constructor_designated = 0;
5034 constructor_top_level = top_level;
5036 if (decl != 0 && decl != error_mark_node)
5038 require_constant_value = TREE_STATIC (decl);
5039 require_constant_elements
5040 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5041 /* For a scalar, you can always use any value to initialize,
5042 even within braces. */
5043 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5044 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5045 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5046 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5047 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5049 else
5051 require_constant_value = 0;
5052 require_constant_elements = 0;
5053 locus = "(anonymous)";
5056 constructor_stack = 0;
5057 constructor_range_stack = 0;
5059 missing_braces_mentioned = 0;
5061 spelling_base = 0;
5062 spelling_size = 0;
5063 RESTORE_SPELLING_DEPTH (0);
5065 if (locus)
5066 push_string (locus);
5069 void
5070 finish_init (void)
5072 struct initializer_stack *p = initializer_stack;
5074 /* Free the whole constructor stack of this initializer. */
5075 while (constructor_stack)
5077 struct constructor_stack *q = constructor_stack;
5078 constructor_stack = q->next;
5079 free (q);
5082 gcc_assert (!constructor_range_stack);
5084 /* Pop back to the data of the outer initializer (if any). */
5085 free (spelling_base);
5087 constructor_decl = p->decl;
5088 require_constant_value = p->require_constant_value;
5089 require_constant_elements = p->require_constant_elements;
5090 constructor_stack = p->constructor_stack;
5091 constructor_range_stack = p->constructor_range_stack;
5092 constructor_elements = p->elements;
5093 spelling = p->spelling;
5094 spelling_base = p->spelling_base;
5095 spelling_size = p->spelling_size;
5096 constructor_top_level = p->top_level;
5097 initializer_stack = p->next;
5098 free (p);
5101 /* Call here when we see the initializer is surrounded by braces.
5102 This is instead of a call to push_init_level;
5103 it is matched by a call to pop_init_level.
5105 TYPE is the type to initialize, for a constructor expression.
5106 For an initializer for a decl, TYPE is zero. */
5108 void
5109 really_start_incremental_init (tree type)
5111 struct constructor_stack *p = XNEW (struct constructor_stack);
5113 if (type == 0)
5114 type = TREE_TYPE (constructor_decl);
5116 if (targetm.vector_opaque_p (type))
5117 error ("opaque vector types cannot be initialized");
5119 p->type = constructor_type;
5120 p->fields = constructor_fields;
5121 p->index = constructor_index;
5122 p->max_index = constructor_max_index;
5123 p->unfilled_index = constructor_unfilled_index;
5124 p->unfilled_fields = constructor_unfilled_fields;
5125 p->bit_index = constructor_bit_index;
5126 p->elements = constructor_elements;
5127 p->constant = constructor_constant;
5128 p->simple = constructor_simple;
5129 p->erroneous = constructor_erroneous;
5130 p->pending_elts = constructor_pending_elts;
5131 p->depth = constructor_depth;
5132 p->replacement_value.value = 0;
5133 p->replacement_value.original_code = ERROR_MARK;
5134 p->implicit = 0;
5135 p->range_stack = 0;
5136 p->outer = 0;
5137 p->incremental = constructor_incremental;
5138 p->designated = constructor_designated;
5139 p->next = 0;
5140 constructor_stack = p;
5142 constructor_constant = 1;
5143 constructor_simple = 1;
5144 constructor_depth = SPELLING_DEPTH ();
5145 constructor_elements = 0;
5146 constructor_pending_elts = 0;
5147 constructor_type = type;
5148 constructor_incremental = 1;
5149 constructor_designated = 0;
5150 designator_depth = 0;
5151 designator_erroneous = 0;
5153 if (TREE_CODE (constructor_type) == RECORD_TYPE
5154 || TREE_CODE (constructor_type) == UNION_TYPE)
5156 constructor_fields = TYPE_FIELDS (constructor_type);
5157 /* Skip any nameless bit fields at the beginning. */
5158 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5159 && DECL_NAME (constructor_fields) == 0)
5160 constructor_fields = TREE_CHAIN (constructor_fields);
5162 constructor_unfilled_fields = constructor_fields;
5163 constructor_bit_index = bitsize_zero_node;
5165 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5167 if (TYPE_DOMAIN (constructor_type))
5169 constructor_max_index
5170 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5172 /* Detect non-empty initializations of zero-length arrays. */
5173 if (constructor_max_index == NULL_TREE
5174 && TYPE_SIZE (constructor_type))
5175 constructor_max_index = build_int_cst (NULL_TREE, -1);
5177 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5178 to initialize VLAs will cause a proper error; avoid tree
5179 checking errors as well by setting a safe value. */
5180 if (constructor_max_index
5181 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5182 constructor_max_index = build_int_cst (NULL_TREE, -1);
5184 constructor_index
5185 = convert (bitsizetype,
5186 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5188 else
5190 constructor_index = bitsize_zero_node;
5191 constructor_max_index = NULL_TREE;
5194 constructor_unfilled_index = constructor_index;
5196 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5198 /* Vectors are like simple fixed-size arrays. */
5199 constructor_max_index =
5200 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5201 constructor_index = bitsize_zero_node;
5202 constructor_unfilled_index = constructor_index;
5204 else
5206 /* Handle the case of int x = {5}; */
5207 constructor_fields = constructor_type;
5208 constructor_unfilled_fields = constructor_type;
5212 /* Push down into a subobject, for initialization.
5213 If this is for an explicit set of braces, IMPLICIT is 0.
5214 If it is because the next element belongs at a lower level,
5215 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5217 void
5218 push_init_level (int implicit)
5220 struct constructor_stack *p;
5221 tree value = NULL_TREE;
5223 /* If we've exhausted any levels that didn't have braces,
5224 pop them now. If implicit == 1, this will have been done in
5225 process_init_element; do not repeat it here because in the case
5226 of excess initializers for an empty aggregate this leads to an
5227 infinite cycle of popping a level and immediately recreating
5228 it. */
5229 if (implicit != 1)
5231 while (constructor_stack->implicit)
5233 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5234 || TREE_CODE (constructor_type) == UNION_TYPE)
5235 && constructor_fields == 0)
5236 process_init_element (pop_init_level (1));
5237 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5238 && constructor_max_index
5239 && tree_int_cst_lt (constructor_max_index,
5240 constructor_index))
5241 process_init_element (pop_init_level (1));
5242 else
5243 break;
5247 /* Unless this is an explicit brace, we need to preserve previous
5248 content if any. */
5249 if (implicit)
5251 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5252 || TREE_CODE (constructor_type) == UNION_TYPE)
5253 && constructor_fields)
5254 value = find_init_member (constructor_fields);
5255 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5256 value = find_init_member (constructor_index);
5259 p = XNEW (struct constructor_stack);
5260 p->type = constructor_type;
5261 p->fields = constructor_fields;
5262 p->index = constructor_index;
5263 p->max_index = constructor_max_index;
5264 p->unfilled_index = constructor_unfilled_index;
5265 p->unfilled_fields = constructor_unfilled_fields;
5266 p->bit_index = constructor_bit_index;
5267 p->elements = constructor_elements;
5268 p->constant = constructor_constant;
5269 p->simple = constructor_simple;
5270 p->erroneous = constructor_erroneous;
5271 p->pending_elts = constructor_pending_elts;
5272 p->depth = constructor_depth;
5273 p->replacement_value.value = 0;
5274 p->replacement_value.original_code = ERROR_MARK;
5275 p->implicit = implicit;
5276 p->outer = 0;
5277 p->incremental = constructor_incremental;
5278 p->designated = constructor_designated;
5279 p->next = constructor_stack;
5280 p->range_stack = 0;
5281 constructor_stack = p;
5283 constructor_constant = 1;
5284 constructor_simple = 1;
5285 constructor_depth = SPELLING_DEPTH ();
5286 constructor_elements = 0;
5287 constructor_incremental = 1;
5288 constructor_designated = 0;
5289 constructor_pending_elts = 0;
5290 if (!implicit)
5292 p->range_stack = constructor_range_stack;
5293 constructor_range_stack = 0;
5294 designator_depth = 0;
5295 designator_erroneous = 0;
5298 /* Don't die if an entire brace-pair level is superfluous
5299 in the containing level. */
5300 if (constructor_type == 0)
5302 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5303 || TREE_CODE (constructor_type) == UNION_TYPE)
5305 /* Don't die if there are extra init elts at the end. */
5306 if (constructor_fields == 0)
5307 constructor_type = 0;
5308 else
5310 constructor_type = TREE_TYPE (constructor_fields);
5311 push_member_name (constructor_fields);
5312 constructor_depth++;
5315 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5317 constructor_type = TREE_TYPE (constructor_type);
5318 push_array_bounds (tree_low_cst (constructor_index, 1));
5319 constructor_depth++;
5322 if (constructor_type == 0)
5324 error_init ("extra brace group at end of initializer");
5325 constructor_fields = 0;
5326 constructor_unfilled_fields = 0;
5327 return;
5330 if (value && TREE_CODE (value) == CONSTRUCTOR)
5332 constructor_constant = TREE_CONSTANT (value);
5333 constructor_simple = TREE_STATIC (value);
5334 constructor_elements = CONSTRUCTOR_ELTS (value);
5335 if (!VEC_empty (constructor_elt, constructor_elements)
5336 && (TREE_CODE (constructor_type) == RECORD_TYPE
5337 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5338 set_nonincremental_init ();
5341 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5343 missing_braces_mentioned = 1;
5344 warning_init ("missing braces around initializer");
5347 if (TREE_CODE (constructor_type) == RECORD_TYPE
5348 || TREE_CODE (constructor_type) == UNION_TYPE)
5350 constructor_fields = TYPE_FIELDS (constructor_type);
5351 /* Skip any nameless bit fields at the beginning. */
5352 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5353 && DECL_NAME (constructor_fields) == 0)
5354 constructor_fields = TREE_CHAIN (constructor_fields);
5356 constructor_unfilled_fields = constructor_fields;
5357 constructor_bit_index = bitsize_zero_node;
5359 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5361 /* Vectors are like simple fixed-size arrays. */
5362 constructor_max_index =
5363 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5364 constructor_index = convert (bitsizetype, integer_zero_node);
5365 constructor_unfilled_index = constructor_index;
5367 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5369 if (TYPE_DOMAIN (constructor_type))
5371 constructor_max_index
5372 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5374 /* Detect non-empty initializations of zero-length arrays. */
5375 if (constructor_max_index == NULL_TREE
5376 && TYPE_SIZE (constructor_type))
5377 constructor_max_index = build_int_cst (NULL_TREE, -1);
5379 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5380 to initialize VLAs will cause a proper error; avoid tree
5381 checking errors as well by setting a safe value. */
5382 if (constructor_max_index
5383 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5384 constructor_max_index = build_int_cst (NULL_TREE, -1);
5386 constructor_index
5387 = convert (bitsizetype,
5388 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5390 else
5391 constructor_index = bitsize_zero_node;
5393 constructor_unfilled_index = constructor_index;
5394 if (value && TREE_CODE (value) == STRING_CST)
5396 /* We need to split the char/wchar array into individual
5397 characters, so that we don't have to special case it
5398 everywhere. */
5399 set_nonincremental_init_from_string (value);
5402 else
5404 if (constructor_type != error_mark_node)
5405 warning_init ("braces around scalar initializer");
5406 constructor_fields = constructor_type;
5407 constructor_unfilled_fields = constructor_type;
5411 /* At the end of an implicit or explicit brace level,
5412 finish up that level of constructor. If a single expression
5413 with redundant braces initialized that level, return the
5414 c_expr structure for that expression. Otherwise, the original_code
5415 element is set to ERROR_MARK.
5416 If we were outputting the elements as they are read, return 0 as the value
5417 from inner levels (process_init_element ignores that),
5418 but return error_mark_node as the value from the outermost level
5419 (that's what we want to put in DECL_INITIAL).
5420 Otherwise, return a CONSTRUCTOR expression as the value. */
5422 struct c_expr
5423 pop_init_level (int implicit)
5425 struct constructor_stack *p;
5426 struct c_expr ret;
5427 ret.value = 0;
5428 ret.original_code = ERROR_MARK;
5430 if (implicit == 0)
5432 /* When we come to an explicit close brace,
5433 pop any inner levels that didn't have explicit braces. */
5434 while (constructor_stack->implicit)
5435 process_init_element (pop_init_level (1));
5437 gcc_assert (!constructor_range_stack);
5440 /* Now output all pending elements. */
5441 constructor_incremental = 1;
5442 output_pending_init_elements (1);
5444 p = constructor_stack;
5446 /* Error for initializing a flexible array member, or a zero-length
5447 array member in an inappropriate context. */
5448 if (constructor_type && constructor_fields
5449 && TREE_CODE (constructor_type) == ARRAY_TYPE
5450 && TYPE_DOMAIN (constructor_type)
5451 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5453 /* Silently discard empty initializations. The parser will
5454 already have pedwarned for empty brackets. */
5455 if (integer_zerop (constructor_unfilled_index))
5456 constructor_type = NULL_TREE;
5457 else
5459 gcc_assert (!TYPE_SIZE (constructor_type));
5461 if (constructor_depth > 2)
5462 error_init ("initialization of flexible array member in a nested context");
5463 else if (pedantic)
5464 pedwarn_init ("initialization of a flexible array member");
5466 /* We have already issued an error message for the existence
5467 of a flexible array member not at the end of the structure.
5468 Discard the initializer so that we do not die later. */
5469 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5470 constructor_type = NULL_TREE;
5474 /* Warn when some struct elements are implicitly initialized to zero. */
5475 if (warn_missing_field_initializers
5476 && constructor_type
5477 && TREE_CODE (constructor_type) == RECORD_TYPE
5478 && constructor_unfilled_fields)
5480 /* Do not warn for flexible array members or zero-length arrays. */
5481 while (constructor_unfilled_fields
5482 && (!DECL_SIZE (constructor_unfilled_fields)
5483 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5484 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5486 /* Do not warn if this level of the initializer uses member
5487 designators; it is likely to be deliberate. */
5488 if (constructor_unfilled_fields && !constructor_designated)
5490 push_member_name (constructor_unfilled_fields);
5491 warning_init ("missing initializer");
5492 RESTORE_SPELLING_DEPTH (constructor_depth);
5496 /* Pad out the end of the structure. */
5497 if (p->replacement_value.value)
5498 /* If this closes a superfluous brace pair,
5499 just pass out the element between them. */
5500 ret = p->replacement_value;
5501 else if (constructor_type == 0)
5503 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5504 && TREE_CODE (constructor_type) != UNION_TYPE
5505 && TREE_CODE (constructor_type) != ARRAY_TYPE
5506 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5508 /* A nonincremental scalar initializer--just return
5509 the element, after verifying there is just one. */
5510 if (VEC_empty (constructor_elt,constructor_elements))
5512 if (!constructor_erroneous)
5513 error_init ("empty scalar initializer");
5514 ret.value = error_mark_node;
5516 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5518 error_init ("extra elements in scalar initializer");
5519 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5521 else
5522 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5524 else
5526 if (constructor_erroneous)
5527 ret.value = error_mark_node;
5528 else
5530 ret.value = build_constructor (constructor_type,
5531 constructor_elements);
5532 if (constructor_constant)
5533 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5534 if (constructor_constant && constructor_simple)
5535 TREE_STATIC (ret.value) = 1;
5539 constructor_type = p->type;
5540 constructor_fields = p->fields;
5541 constructor_index = p->index;
5542 constructor_max_index = p->max_index;
5543 constructor_unfilled_index = p->unfilled_index;
5544 constructor_unfilled_fields = p->unfilled_fields;
5545 constructor_bit_index = p->bit_index;
5546 constructor_elements = p->elements;
5547 constructor_constant = p->constant;
5548 constructor_simple = p->simple;
5549 constructor_erroneous = p->erroneous;
5550 constructor_incremental = p->incremental;
5551 constructor_designated = p->designated;
5552 constructor_pending_elts = p->pending_elts;
5553 constructor_depth = p->depth;
5554 if (!p->implicit)
5555 constructor_range_stack = p->range_stack;
5556 RESTORE_SPELLING_DEPTH (constructor_depth);
5558 constructor_stack = p->next;
5559 free (p);
5561 if (ret.value == 0 && constructor_stack == 0)
5562 ret.value = error_mark_node;
5563 return ret;
5566 /* Common handling for both array range and field name designators.
5567 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5569 static int
5570 set_designator (int array)
5572 tree subtype;
5573 enum tree_code subcode;
5575 /* Don't die if an entire brace-pair level is superfluous
5576 in the containing level. */
5577 if (constructor_type == 0)
5578 return 1;
5580 /* If there were errors in this designator list already, bail out
5581 silently. */
5582 if (designator_erroneous)
5583 return 1;
5585 if (!designator_depth)
5587 gcc_assert (!constructor_range_stack);
5589 /* Designator list starts at the level of closest explicit
5590 braces. */
5591 while (constructor_stack->implicit)
5592 process_init_element (pop_init_level (1));
5593 constructor_designated = 1;
5594 return 0;
5597 switch (TREE_CODE (constructor_type))
5599 case RECORD_TYPE:
5600 case UNION_TYPE:
5601 subtype = TREE_TYPE (constructor_fields);
5602 if (subtype != error_mark_node)
5603 subtype = TYPE_MAIN_VARIANT (subtype);
5604 break;
5605 case ARRAY_TYPE:
5606 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5607 break;
5608 default:
5609 gcc_unreachable ();
5612 subcode = TREE_CODE (subtype);
5613 if (array && subcode != ARRAY_TYPE)
5615 error_init ("array index in non-array initializer");
5616 return 1;
5618 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5620 error_init ("field name not in record or union initializer");
5621 return 1;
5624 constructor_designated = 1;
5625 push_init_level (2);
5626 return 0;
5629 /* If there are range designators in designator list, push a new designator
5630 to constructor_range_stack. RANGE_END is end of such stack range or
5631 NULL_TREE if there is no range designator at this level. */
5633 static void
5634 push_range_stack (tree range_end)
5636 struct constructor_range_stack *p;
5638 p = GGC_NEW (struct constructor_range_stack);
5639 p->prev = constructor_range_stack;
5640 p->next = 0;
5641 p->fields = constructor_fields;
5642 p->range_start = constructor_index;
5643 p->index = constructor_index;
5644 p->stack = constructor_stack;
5645 p->range_end = range_end;
5646 if (constructor_range_stack)
5647 constructor_range_stack->next = p;
5648 constructor_range_stack = p;
5651 /* Within an array initializer, specify the next index to be initialized.
5652 FIRST is that index. If LAST is nonzero, then initialize a range
5653 of indices, running from FIRST through LAST. */
5655 void
5656 set_init_index (tree first, tree last)
5658 if (set_designator (1))
5659 return;
5661 designator_erroneous = 1;
5663 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5664 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5666 error_init ("array index in initializer not of integer type");
5667 return;
5670 if (TREE_CODE (first) != INTEGER_CST)
5671 error_init ("nonconstant array index in initializer");
5672 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5673 error_init ("nonconstant array index in initializer");
5674 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5675 error_init ("array index in non-array initializer");
5676 else if (tree_int_cst_sgn (first) == -1)
5677 error_init ("array index in initializer exceeds array bounds");
5678 else if (constructor_max_index
5679 && tree_int_cst_lt (constructor_max_index, first))
5680 error_init ("array index in initializer exceeds array bounds");
5681 else
5683 constructor_index = convert (bitsizetype, first);
5685 if (last)
5687 if (tree_int_cst_equal (first, last))
5688 last = 0;
5689 else if (tree_int_cst_lt (last, first))
5691 error_init ("empty index range in initializer");
5692 last = 0;
5694 else
5696 last = convert (bitsizetype, last);
5697 if (constructor_max_index != 0
5698 && tree_int_cst_lt (constructor_max_index, last))
5700 error_init ("array index range in initializer exceeds array bounds");
5701 last = 0;
5706 designator_depth++;
5707 designator_erroneous = 0;
5708 if (constructor_range_stack || last)
5709 push_range_stack (last);
5713 /* Within a struct initializer, specify the next field to be initialized. */
5715 void
5716 set_init_label (tree fieldname)
5718 tree tail;
5720 if (set_designator (0))
5721 return;
5723 designator_erroneous = 1;
5725 if (TREE_CODE (constructor_type) != RECORD_TYPE
5726 && TREE_CODE (constructor_type) != UNION_TYPE)
5728 error_init ("field name not in record or union initializer");
5729 return;
5732 for (tail = TYPE_FIELDS (constructor_type); tail;
5733 tail = TREE_CHAIN (tail))
5735 if (DECL_NAME (tail) == fieldname)
5736 break;
5739 if (tail == 0)
5740 error ("unknown field %qE specified in initializer", fieldname);
5741 else
5743 constructor_fields = tail;
5744 designator_depth++;
5745 designator_erroneous = 0;
5746 if (constructor_range_stack)
5747 push_range_stack (NULL_TREE);
5751 /* Add a new initializer to the tree of pending initializers. PURPOSE
5752 identifies the initializer, either array index or field in a structure.
5753 VALUE is the value of that index or field. */
5755 static void
5756 add_pending_init (tree purpose, tree value)
5758 struct init_node *p, **q, *r;
5760 q = &constructor_pending_elts;
5761 p = 0;
5763 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5765 while (*q != 0)
5767 p = *q;
5768 if (tree_int_cst_lt (purpose, p->purpose))
5769 q = &p->left;
5770 else if (tree_int_cst_lt (p->purpose, purpose))
5771 q = &p->right;
5772 else
5774 if (TREE_SIDE_EFFECTS (p->value))
5775 warning_init ("initialized field with side-effects overwritten");
5776 else if (warn_override_init)
5777 warning_init ("initialized field overwritten");
5778 p->value = value;
5779 return;
5783 else
5785 tree bitpos;
5787 bitpos = bit_position (purpose);
5788 while (*q != NULL)
5790 p = *q;
5791 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5792 q = &p->left;
5793 else if (p->purpose != purpose)
5794 q = &p->right;
5795 else
5797 if (TREE_SIDE_EFFECTS (p->value))
5798 warning_init ("initialized field with side-effects overwritten");
5799 else if (warn_override_init)
5800 warning_init ("initialized field overwritten");
5801 p->value = value;
5802 return;
5807 r = GGC_NEW (struct init_node);
5808 r->purpose = purpose;
5809 r->value = value;
5811 *q = r;
5812 r->parent = p;
5813 r->left = 0;
5814 r->right = 0;
5815 r->balance = 0;
5817 while (p)
5819 struct init_node *s;
5821 if (r == p->left)
5823 if (p->balance == 0)
5824 p->balance = -1;
5825 else if (p->balance < 0)
5827 if (r->balance < 0)
5829 /* L rotation. */
5830 p->left = r->right;
5831 if (p->left)
5832 p->left->parent = p;
5833 r->right = p;
5835 p->balance = 0;
5836 r->balance = 0;
5838 s = p->parent;
5839 p->parent = r;
5840 r->parent = s;
5841 if (s)
5843 if (s->left == p)
5844 s->left = r;
5845 else
5846 s->right = r;
5848 else
5849 constructor_pending_elts = r;
5851 else
5853 /* LR rotation. */
5854 struct init_node *t = r->right;
5856 r->right = t->left;
5857 if (r->right)
5858 r->right->parent = r;
5859 t->left = r;
5861 p->left = t->right;
5862 if (p->left)
5863 p->left->parent = p;
5864 t->right = p;
5866 p->balance = t->balance < 0;
5867 r->balance = -(t->balance > 0);
5868 t->balance = 0;
5870 s = p->parent;
5871 p->parent = t;
5872 r->parent = t;
5873 t->parent = s;
5874 if (s)
5876 if (s->left == p)
5877 s->left = t;
5878 else
5879 s->right = t;
5881 else
5882 constructor_pending_elts = t;
5884 break;
5886 else
5888 /* p->balance == +1; growth of left side balances the node. */
5889 p->balance = 0;
5890 break;
5893 else /* r == p->right */
5895 if (p->balance == 0)
5896 /* Growth propagation from right side. */
5897 p->balance++;
5898 else if (p->balance > 0)
5900 if (r->balance > 0)
5902 /* R rotation. */
5903 p->right = r->left;
5904 if (p->right)
5905 p->right->parent = p;
5906 r->left = p;
5908 p->balance = 0;
5909 r->balance = 0;
5911 s = p->parent;
5912 p->parent = r;
5913 r->parent = s;
5914 if (s)
5916 if (s->left == p)
5917 s->left = r;
5918 else
5919 s->right = r;
5921 else
5922 constructor_pending_elts = r;
5924 else /* r->balance == -1 */
5926 /* RL rotation */
5927 struct init_node *t = r->left;
5929 r->left = t->right;
5930 if (r->left)
5931 r->left->parent = r;
5932 t->right = r;
5934 p->right = t->left;
5935 if (p->right)
5936 p->right->parent = p;
5937 t->left = p;
5939 r->balance = (t->balance < 0);
5940 p->balance = -(t->balance > 0);
5941 t->balance = 0;
5943 s = p->parent;
5944 p->parent = t;
5945 r->parent = t;
5946 t->parent = s;
5947 if (s)
5949 if (s->left == p)
5950 s->left = t;
5951 else
5952 s->right = t;
5954 else
5955 constructor_pending_elts = t;
5957 break;
5959 else
5961 /* p->balance == -1; growth of right side balances the node. */
5962 p->balance = 0;
5963 break;
5967 r = p;
5968 p = p->parent;
5972 /* Build AVL tree from a sorted chain. */
5974 static void
5975 set_nonincremental_init (void)
5977 unsigned HOST_WIDE_INT ix;
5978 tree index, value;
5980 if (TREE_CODE (constructor_type) != RECORD_TYPE
5981 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5982 return;
5984 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5985 add_pending_init (index, value);
5986 constructor_elements = 0;
5987 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5989 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5990 /* Skip any nameless bit fields at the beginning. */
5991 while (constructor_unfilled_fields != 0
5992 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5993 && DECL_NAME (constructor_unfilled_fields) == 0)
5994 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5997 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5999 if (TYPE_DOMAIN (constructor_type))
6000 constructor_unfilled_index
6001 = convert (bitsizetype,
6002 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6003 else
6004 constructor_unfilled_index = bitsize_zero_node;
6006 constructor_incremental = 0;
6009 /* Build AVL tree from a string constant. */
6011 static void
6012 set_nonincremental_init_from_string (tree str)
6014 tree value, purpose, type;
6015 HOST_WIDE_INT val[2];
6016 const char *p, *end;
6017 int byte, wchar_bytes, charwidth, bitpos;
6019 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6021 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6022 == TYPE_PRECISION (char_type_node))
6023 wchar_bytes = 1;
6024 else
6026 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6027 == TYPE_PRECISION (wchar_type_node));
6028 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6030 charwidth = TYPE_PRECISION (char_type_node);
6031 type = TREE_TYPE (constructor_type);
6032 p = TREE_STRING_POINTER (str);
6033 end = p + TREE_STRING_LENGTH (str);
6035 for (purpose = bitsize_zero_node;
6036 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6037 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6039 if (wchar_bytes == 1)
6041 val[1] = (unsigned char) *p++;
6042 val[0] = 0;
6044 else
6046 val[0] = 0;
6047 val[1] = 0;
6048 for (byte = 0; byte < wchar_bytes; byte++)
6050 if (BYTES_BIG_ENDIAN)
6051 bitpos = (wchar_bytes - byte - 1) * charwidth;
6052 else
6053 bitpos = byte * charwidth;
6054 val[bitpos < HOST_BITS_PER_WIDE_INT]
6055 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6056 << (bitpos % HOST_BITS_PER_WIDE_INT);
6060 if (!TYPE_UNSIGNED (type))
6062 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6063 if (bitpos < HOST_BITS_PER_WIDE_INT)
6065 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6067 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6068 val[0] = -1;
6071 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6073 if (val[1] < 0)
6074 val[0] = -1;
6076 else if (val[0] & (((HOST_WIDE_INT) 1)
6077 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6078 val[0] |= ((HOST_WIDE_INT) -1)
6079 << (bitpos - HOST_BITS_PER_WIDE_INT);
6082 value = build_int_cst_wide (type, val[1], val[0]);
6083 add_pending_init (purpose, value);
6086 constructor_incremental = 0;
6089 /* Return value of FIELD in pending initializer or zero if the field was
6090 not initialized yet. */
6092 static tree
6093 find_init_member (tree field)
6095 struct init_node *p;
6097 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6099 if (constructor_incremental
6100 && tree_int_cst_lt (field, constructor_unfilled_index))
6101 set_nonincremental_init ();
6103 p = constructor_pending_elts;
6104 while (p)
6106 if (tree_int_cst_lt (field, p->purpose))
6107 p = p->left;
6108 else if (tree_int_cst_lt (p->purpose, field))
6109 p = p->right;
6110 else
6111 return p->value;
6114 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6116 tree bitpos = bit_position (field);
6118 if (constructor_incremental
6119 && (!constructor_unfilled_fields
6120 || tree_int_cst_lt (bitpos,
6121 bit_position (constructor_unfilled_fields))))
6122 set_nonincremental_init ();
6124 p = constructor_pending_elts;
6125 while (p)
6127 if (field == p->purpose)
6128 return p->value;
6129 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6130 p = p->left;
6131 else
6132 p = p->right;
6135 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6137 if (!VEC_empty (constructor_elt, constructor_elements)
6138 && (VEC_last (constructor_elt, constructor_elements)->index
6139 == field))
6140 return VEC_last (constructor_elt, constructor_elements)->value;
6142 return 0;
6145 /* "Output" the next constructor element.
6146 At top level, really output it to assembler code now.
6147 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6148 TYPE is the data type that the containing data type wants here.
6149 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6150 If VALUE is a string constant, STRICT_STRING is true if it is
6151 unparenthesized or we should not warn here for it being parenthesized.
6152 For other types of VALUE, STRICT_STRING is not used.
6154 PENDING if non-nil means output pending elements that belong
6155 right after this element. (PENDING is normally 1;
6156 it is 0 while outputting pending elements, to avoid recursion.) */
6158 static void
6159 output_init_element (tree value, bool strict_string, tree type, tree field,
6160 int pending)
6162 constructor_elt *celt;
6164 if (type == error_mark_node || value == error_mark_node)
6166 constructor_erroneous = 1;
6167 return;
6169 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6170 && (TREE_CODE (value) == STRING_CST
6171 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6172 && !(TREE_CODE (value) == STRING_CST
6173 && TREE_CODE (type) == ARRAY_TYPE
6174 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6175 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6176 TYPE_MAIN_VARIANT (type)))
6177 value = array_to_pointer_conversion (value);
6179 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6180 && require_constant_value && !flag_isoc99 && pending)
6182 /* As an extension, allow initializing objects with static storage
6183 duration with compound literals (which are then treated just as
6184 the brace enclosed list they contain). */
6185 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6186 value = DECL_INITIAL (decl);
6189 if (value == error_mark_node)
6190 constructor_erroneous = 1;
6191 else if (!TREE_CONSTANT (value))
6192 constructor_constant = 0;
6193 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6194 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6195 || TREE_CODE (constructor_type) == UNION_TYPE)
6196 && DECL_C_BIT_FIELD (field)
6197 && TREE_CODE (value) != INTEGER_CST))
6198 constructor_simple = 0;
6200 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6202 if (require_constant_value)
6204 error_init ("initializer element is not constant");
6205 value = error_mark_node;
6207 else if (require_constant_elements)
6208 pedwarn ("initializer element is not computable at load time");
6211 /* If this field is empty (and not at the end of structure),
6212 don't do anything other than checking the initializer. */
6213 if (field
6214 && (TREE_TYPE (field) == error_mark_node
6215 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6216 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6217 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6218 || TREE_CHAIN (field)))))
6219 return;
6221 value = digest_init (type, value, strict_string, require_constant_value);
6222 if (value == error_mark_node)
6224 constructor_erroneous = 1;
6225 return;
6228 /* If this element doesn't come next in sequence,
6229 put it on constructor_pending_elts. */
6230 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6231 && (!constructor_incremental
6232 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6234 if (constructor_incremental
6235 && tree_int_cst_lt (field, constructor_unfilled_index))
6236 set_nonincremental_init ();
6238 add_pending_init (field, value);
6239 return;
6241 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6242 && (!constructor_incremental
6243 || field != constructor_unfilled_fields))
6245 /* We do this for records but not for unions. In a union,
6246 no matter which field is specified, it can be initialized
6247 right away since it starts at the beginning of the union. */
6248 if (constructor_incremental)
6250 if (!constructor_unfilled_fields)
6251 set_nonincremental_init ();
6252 else
6254 tree bitpos, unfillpos;
6256 bitpos = bit_position (field);
6257 unfillpos = bit_position (constructor_unfilled_fields);
6259 if (tree_int_cst_lt (bitpos, unfillpos))
6260 set_nonincremental_init ();
6264 add_pending_init (field, value);
6265 return;
6267 else if (TREE_CODE (constructor_type) == UNION_TYPE
6268 && !VEC_empty (constructor_elt, constructor_elements))
6270 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6271 constructor_elements)->value))
6272 warning_init ("initialized field with side-effects overwritten");
6273 else if (warn_override_init)
6274 warning_init ("initialized field overwritten");
6276 /* We can have just one union field set. */
6277 constructor_elements = 0;
6280 /* Otherwise, output this element either to
6281 constructor_elements or to the assembler file. */
6283 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6284 celt->index = field;
6285 celt->value = value;
6287 /* Advance the variable that indicates sequential elements output. */
6288 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6289 constructor_unfilled_index
6290 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6291 bitsize_one_node);
6292 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6294 constructor_unfilled_fields
6295 = TREE_CHAIN (constructor_unfilled_fields);
6297 /* Skip any nameless bit fields. */
6298 while (constructor_unfilled_fields != 0
6299 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6300 && DECL_NAME (constructor_unfilled_fields) == 0)
6301 constructor_unfilled_fields =
6302 TREE_CHAIN (constructor_unfilled_fields);
6304 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6305 constructor_unfilled_fields = 0;
6307 /* Now output any pending elements which have become next. */
6308 if (pending)
6309 output_pending_init_elements (0);
6312 /* Output any pending elements which have become next.
6313 As we output elements, constructor_unfilled_{fields,index}
6314 advances, which may cause other elements to become next;
6315 if so, they too are output.
6317 If ALL is 0, we return when there are
6318 no more pending elements to output now.
6320 If ALL is 1, we output space as necessary so that
6321 we can output all the pending elements. */
6323 static void
6324 output_pending_init_elements (int all)
6326 struct init_node *elt = constructor_pending_elts;
6327 tree next;
6329 retry:
6331 /* Look through the whole pending tree.
6332 If we find an element that should be output now,
6333 output it. Otherwise, set NEXT to the element
6334 that comes first among those still pending. */
6336 next = 0;
6337 while (elt)
6339 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6341 if (tree_int_cst_equal (elt->purpose,
6342 constructor_unfilled_index))
6343 output_init_element (elt->value, true,
6344 TREE_TYPE (constructor_type),
6345 constructor_unfilled_index, 0);
6346 else if (tree_int_cst_lt (constructor_unfilled_index,
6347 elt->purpose))
6349 /* Advance to the next smaller node. */
6350 if (elt->left)
6351 elt = elt->left;
6352 else
6354 /* We have reached the smallest node bigger than the
6355 current unfilled index. Fill the space first. */
6356 next = elt->purpose;
6357 break;
6360 else
6362 /* Advance to the next bigger node. */
6363 if (elt->right)
6364 elt = elt->right;
6365 else
6367 /* We have reached the biggest node in a subtree. Find
6368 the parent of it, which is the next bigger node. */
6369 while (elt->parent && elt->parent->right == elt)
6370 elt = elt->parent;
6371 elt = elt->parent;
6372 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6373 elt->purpose))
6375 next = elt->purpose;
6376 break;
6381 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6382 || TREE_CODE (constructor_type) == UNION_TYPE)
6384 tree ctor_unfilled_bitpos, elt_bitpos;
6386 /* If the current record is complete we are done. */
6387 if (constructor_unfilled_fields == 0)
6388 break;
6390 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6391 elt_bitpos = bit_position (elt->purpose);
6392 /* We can't compare fields here because there might be empty
6393 fields in between. */
6394 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6396 constructor_unfilled_fields = elt->purpose;
6397 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6398 elt->purpose, 0);
6400 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6402 /* Advance to the next smaller node. */
6403 if (elt->left)
6404 elt = elt->left;
6405 else
6407 /* We have reached the smallest node bigger than the
6408 current unfilled field. Fill the space first. */
6409 next = elt->purpose;
6410 break;
6413 else
6415 /* Advance to the next bigger node. */
6416 if (elt->right)
6417 elt = elt->right;
6418 else
6420 /* We have reached the biggest node in a subtree. Find
6421 the parent of it, which is the next bigger node. */
6422 while (elt->parent && elt->parent->right == elt)
6423 elt = elt->parent;
6424 elt = elt->parent;
6425 if (elt
6426 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6427 bit_position (elt->purpose))))
6429 next = elt->purpose;
6430 break;
6437 /* Ordinarily return, but not if we want to output all
6438 and there are elements left. */
6439 if (!(all && next != 0))
6440 return;
6442 /* If it's not incremental, just skip over the gap, so that after
6443 jumping to retry we will output the next successive element. */
6444 if (TREE_CODE (constructor_type) == RECORD_TYPE
6445 || TREE_CODE (constructor_type) == UNION_TYPE)
6446 constructor_unfilled_fields = next;
6447 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6448 constructor_unfilled_index = next;
6450 /* ELT now points to the node in the pending tree with the next
6451 initializer to output. */
6452 goto retry;
6455 /* Add one non-braced element to the current constructor level.
6456 This adjusts the current position within the constructor's type.
6457 This may also start or terminate implicit levels
6458 to handle a partly-braced initializer.
6460 Once this has found the correct level for the new element,
6461 it calls output_init_element. */
6463 void
6464 process_init_element (struct c_expr value)
6466 tree orig_value = value.value;
6467 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6468 bool strict_string = value.original_code == STRING_CST;
6470 designator_depth = 0;
6471 designator_erroneous = 0;
6473 /* Handle superfluous braces around string cst as in
6474 char x[] = {"foo"}; */
6475 if (string_flag
6476 && constructor_type
6477 && TREE_CODE (constructor_type) == ARRAY_TYPE
6478 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6479 && integer_zerop (constructor_unfilled_index))
6481 if (constructor_stack->replacement_value.value)
6482 error_init ("excess elements in char array initializer");
6483 constructor_stack->replacement_value = value;
6484 return;
6487 if (constructor_stack->replacement_value.value != 0)
6489 error_init ("excess elements in struct initializer");
6490 return;
6493 /* Ignore elements of a brace group if it is entirely superfluous
6494 and has already been diagnosed. */
6495 if (constructor_type == 0)
6496 return;
6498 /* If we've exhausted any levels that didn't have braces,
6499 pop them now. */
6500 while (constructor_stack->implicit)
6502 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6503 || TREE_CODE (constructor_type) == UNION_TYPE)
6504 && constructor_fields == 0)
6505 process_init_element (pop_init_level (1));
6506 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6507 && (constructor_max_index == 0
6508 || tree_int_cst_lt (constructor_max_index,
6509 constructor_index)))
6510 process_init_element (pop_init_level (1));
6511 else
6512 break;
6515 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6516 if (constructor_range_stack)
6518 /* If value is a compound literal and we'll be just using its
6519 content, don't put it into a SAVE_EXPR. */
6520 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6521 || !require_constant_value
6522 || flag_isoc99)
6523 value.value = save_expr (value.value);
6526 while (1)
6528 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6530 tree fieldtype;
6531 enum tree_code fieldcode;
6533 if (constructor_fields == 0)
6535 pedwarn_init ("excess elements in struct initializer");
6536 break;
6539 fieldtype = TREE_TYPE (constructor_fields);
6540 if (fieldtype != error_mark_node)
6541 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6542 fieldcode = TREE_CODE (fieldtype);
6544 /* Error for non-static initialization of a flexible array member. */
6545 if (fieldcode == ARRAY_TYPE
6546 && !require_constant_value
6547 && TYPE_SIZE (fieldtype) == NULL_TREE
6548 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6550 error_init ("non-static initialization of a flexible array member");
6551 break;
6554 /* Accept a string constant to initialize a subarray. */
6555 if (value.value != 0
6556 && fieldcode == ARRAY_TYPE
6557 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6558 && string_flag)
6559 value.value = orig_value;
6560 /* Otherwise, if we have come to a subaggregate,
6561 and we don't have an element of its type, push into it. */
6562 else if (value.value != 0
6563 && value.value != error_mark_node
6564 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6565 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6566 || fieldcode == UNION_TYPE))
6568 push_init_level (1);
6569 continue;
6572 if (value.value)
6574 push_member_name (constructor_fields);
6575 output_init_element (value.value, strict_string,
6576 fieldtype, constructor_fields, 1);
6577 RESTORE_SPELLING_DEPTH (constructor_depth);
6579 else
6580 /* Do the bookkeeping for an element that was
6581 directly output as a constructor. */
6583 /* For a record, keep track of end position of last field. */
6584 if (DECL_SIZE (constructor_fields))
6585 constructor_bit_index
6586 = size_binop (PLUS_EXPR,
6587 bit_position (constructor_fields),
6588 DECL_SIZE (constructor_fields));
6590 /* If the current field was the first one not yet written out,
6591 it isn't now, so update. */
6592 if (constructor_unfilled_fields == constructor_fields)
6594 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6595 /* Skip any nameless bit fields. */
6596 while (constructor_unfilled_fields != 0
6597 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6598 && DECL_NAME (constructor_unfilled_fields) == 0)
6599 constructor_unfilled_fields =
6600 TREE_CHAIN (constructor_unfilled_fields);
6604 constructor_fields = TREE_CHAIN (constructor_fields);
6605 /* Skip any nameless bit fields at the beginning. */
6606 while (constructor_fields != 0
6607 && DECL_C_BIT_FIELD (constructor_fields)
6608 && DECL_NAME (constructor_fields) == 0)
6609 constructor_fields = TREE_CHAIN (constructor_fields);
6611 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6613 tree fieldtype;
6614 enum tree_code fieldcode;
6616 if (constructor_fields == 0)
6618 pedwarn_init ("excess elements in union initializer");
6619 break;
6622 fieldtype = TREE_TYPE (constructor_fields);
6623 if (fieldtype != error_mark_node)
6624 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6625 fieldcode = TREE_CODE (fieldtype);
6627 /* Warn that traditional C rejects initialization of unions.
6628 We skip the warning if the value is zero. This is done
6629 under the assumption that the zero initializer in user
6630 code appears conditioned on e.g. __STDC__ to avoid
6631 "missing initializer" warnings and relies on default
6632 initialization to zero in the traditional C case.
6633 We also skip the warning if the initializer is designated,
6634 again on the assumption that this must be conditional on
6635 __STDC__ anyway (and we've already complained about the
6636 member-designator already). */
6637 if (!in_system_header && !constructor_designated
6638 && !(value.value && (integer_zerop (value.value)
6639 || real_zerop (value.value))))
6640 warning (OPT_Wtraditional, "traditional C rejects initialization "
6641 "of unions");
6643 /* Accept a string constant to initialize a subarray. */
6644 if (value.value != 0
6645 && fieldcode == ARRAY_TYPE
6646 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6647 && string_flag)
6648 value.value = orig_value;
6649 /* Otherwise, if we have come to a subaggregate,
6650 and we don't have an element of its type, push into it. */
6651 else if (value.value != 0
6652 && value.value != error_mark_node
6653 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6654 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6655 || fieldcode == UNION_TYPE))
6657 push_init_level (1);
6658 continue;
6661 if (value.value)
6663 push_member_name (constructor_fields);
6664 output_init_element (value.value, strict_string,
6665 fieldtype, constructor_fields, 1);
6666 RESTORE_SPELLING_DEPTH (constructor_depth);
6668 else
6669 /* Do the bookkeeping for an element that was
6670 directly output as a constructor. */
6672 constructor_bit_index = DECL_SIZE (constructor_fields);
6673 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6676 constructor_fields = 0;
6678 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6680 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6681 enum tree_code eltcode = TREE_CODE (elttype);
6683 /* Accept a string constant to initialize a subarray. */
6684 if (value.value != 0
6685 && eltcode == ARRAY_TYPE
6686 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6687 && string_flag)
6688 value.value = orig_value;
6689 /* Otherwise, if we have come to a subaggregate,
6690 and we don't have an element of its type, push into it. */
6691 else if (value.value != 0
6692 && value.value != error_mark_node
6693 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6694 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6695 || eltcode == UNION_TYPE))
6697 push_init_level (1);
6698 continue;
6701 if (constructor_max_index != 0
6702 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6703 || integer_all_onesp (constructor_max_index)))
6705 pedwarn_init ("excess elements in array initializer");
6706 break;
6709 /* Now output the actual element. */
6710 if (value.value)
6712 push_array_bounds (tree_low_cst (constructor_index, 1));
6713 output_init_element (value.value, strict_string,
6714 elttype, constructor_index, 1);
6715 RESTORE_SPELLING_DEPTH (constructor_depth);
6718 constructor_index
6719 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6721 if (!value.value)
6722 /* If we are doing the bookkeeping for an element that was
6723 directly output as a constructor, we must update
6724 constructor_unfilled_index. */
6725 constructor_unfilled_index = constructor_index;
6727 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6729 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6731 /* Do a basic check of initializer size. Note that vectors
6732 always have a fixed size derived from their type. */
6733 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6735 pedwarn_init ("excess elements in vector initializer");
6736 break;
6739 /* Now output the actual element. */
6740 if (value.value)
6741 output_init_element (value.value, strict_string,
6742 elttype, constructor_index, 1);
6744 constructor_index
6745 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6747 if (!value.value)
6748 /* If we are doing the bookkeeping for an element that was
6749 directly output as a constructor, we must update
6750 constructor_unfilled_index. */
6751 constructor_unfilled_index = constructor_index;
6754 /* Handle the sole element allowed in a braced initializer
6755 for a scalar variable. */
6756 else if (constructor_type != error_mark_node
6757 && constructor_fields == 0)
6759 pedwarn_init ("excess elements in scalar initializer");
6760 break;
6762 else
6764 if (value.value)
6765 output_init_element (value.value, strict_string,
6766 constructor_type, NULL_TREE, 1);
6767 constructor_fields = 0;
6770 /* Handle range initializers either at this level or anywhere higher
6771 in the designator stack. */
6772 if (constructor_range_stack)
6774 struct constructor_range_stack *p, *range_stack;
6775 int finish = 0;
6777 range_stack = constructor_range_stack;
6778 constructor_range_stack = 0;
6779 while (constructor_stack != range_stack->stack)
6781 gcc_assert (constructor_stack->implicit);
6782 process_init_element (pop_init_level (1));
6784 for (p = range_stack;
6785 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6786 p = p->prev)
6788 gcc_assert (constructor_stack->implicit);
6789 process_init_element (pop_init_level (1));
6792 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6793 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6794 finish = 1;
6796 while (1)
6798 constructor_index = p->index;
6799 constructor_fields = p->fields;
6800 if (finish && p->range_end && p->index == p->range_start)
6802 finish = 0;
6803 p->prev = 0;
6805 p = p->next;
6806 if (!p)
6807 break;
6808 push_init_level (2);
6809 p->stack = constructor_stack;
6810 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6811 p->index = p->range_start;
6814 if (!finish)
6815 constructor_range_stack = range_stack;
6816 continue;
6819 break;
6822 constructor_range_stack = 0;
6825 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6826 (guaranteed to be 'volatile' or null) and ARGS (represented using
6827 an ASM_EXPR node). */
6828 tree
6829 build_asm_stmt (tree cv_qualifier, tree args)
6831 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6832 ASM_VOLATILE_P (args) = 1;
6833 return add_stmt (args);
6836 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6837 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6838 SIMPLE indicates whether there was anything at all after the
6839 string in the asm expression -- asm("blah") and asm("blah" : )
6840 are subtly different. We use a ASM_EXPR node to represent this. */
6841 tree
6842 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6843 bool simple)
6845 tree tail;
6846 tree args;
6847 int i;
6848 const char *constraint;
6849 const char **oconstraints;
6850 bool allows_mem, allows_reg, is_inout;
6851 int ninputs, noutputs;
6853 ninputs = list_length (inputs);
6854 noutputs = list_length (outputs);
6855 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6857 string = resolve_asm_operand_names (string, outputs, inputs);
6859 /* Remove output conversions that change the type but not the mode. */
6860 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6862 tree output = TREE_VALUE (tail);
6864 /* ??? Really, this should not be here. Users should be using a
6865 proper lvalue, dammit. But there's a long history of using casts
6866 in the output operands. In cases like longlong.h, this becomes a
6867 primitive form of typechecking -- if the cast can be removed, then
6868 the output operand had a type of the proper width; otherwise we'll
6869 get an error. Gross, but ... */
6870 STRIP_NOPS (output);
6872 if (!lvalue_or_else (output, lv_asm))
6873 output = error_mark_node;
6875 if (output != error_mark_node
6876 && (TREE_READONLY (output)
6877 || TYPE_READONLY (TREE_TYPE (output))
6878 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6879 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6880 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6881 readonly_error (output, lv_asm);
6883 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6884 oconstraints[i] = constraint;
6886 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6887 &allows_mem, &allows_reg, &is_inout))
6889 /* If the operand is going to end up in memory,
6890 mark it addressable. */
6891 if (!allows_reg && !c_mark_addressable (output))
6892 output = error_mark_node;
6894 else
6895 output = error_mark_node;
6897 TREE_VALUE (tail) = output;
6900 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6902 tree input;
6904 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6905 input = TREE_VALUE (tail);
6907 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6908 oconstraints, &allows_mem, &allows_reg))
6910 /* If the operand is going to end up in memory,
6911 mark it addressable. */
6912 if (!allows_reg && allows_mem)
6914 /* Strip the nops as we allow this case. FIXME, this really
6915 should be rejected or made deprecated. */
6916 STRIP_NOPS (input);
6917 if (!c_mark_addressable (input))
6918 input = error_mark_node;
6921 else
6922 input = error_mark_node;
6924 TREE_VALUE (tail) = input;
6927 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6929 /* asm statements without outputs, including simple ones, are treated
6930 as volatile. */
6931 ASM_INPUT_P (args) = simple;
6932 ASM_VOLATILE_P (args) = (noutputs == 0);
6934 return args;
6937 /* Generate a goto statement to LABEL. */
6939 tree
6940 c_finish_goto_label (tree label)
6942 tree decl = lookup_label (label);
6943 if (!decl)
6944 return NULL_TREE;
6946 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6948 error ("jump into statement expression");
6949 return NULL_TREE;
6952 if (C_DECL_UNJUMPABLE_VM (decl))
6954 error ("jump into scope of identifier with variably modified type");
6955 return NULL_TREE;
6958 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6960 /* No jump from outside this statement expression context, so
6961 record that there is a jump from within this context. */
6962 struct c_label_list *nlist;
6963 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6964 nlist->next = label_context_stack_se->labels_used;
6965 nlist->label = decl;
6966 label_context_stack_se->labels_used = nlist;
6969 if (!C_DECL_UNDEFINABLE_VM (decl))
6971 /* No jump from outside this context context of identifiers with
6972 variably modified type, so record that there is a jump from
6973 within this context. */
6974 struct c_label_list *nlist;
6975 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6976 nlist->next = label_context_stack_vm->labels_used;
6977 nlist->label = decl;
6978 label_context_stack_vm->labels_used = nlist;
6981 TREE_USED (decl) = 1;
6982 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6985 /* Generate a computed goto statement to EXPR. */
6987 tree
6988 c_finish_goto_ptr (tree expr)
6990 if (pedantic)
6991 pedwarn ("ISO C forbids %<goto *expr;%>");
6992 expr = convert (ptr_type_node, expr);
6993 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6996 /* Generate a C `return' statement. RETVAL is the expression for what
6997 to return, or a null pointer for `return;' with no value. */
6999 tree
7000 c_finish_return (tree retval)
7002 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7003 bool no_warning = false;
7005 if (TREE_THIS_VOLATILE (current_function_decl))
7006 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7008 if (!retval)
7010 current_function_returns_null = 1;
7011 if ((warn_return_type || flag_isoc99)
7012 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7014 pedwarn_c99 ("%<return%> with no value, in "
7015 "function returning non-void");
7016 no_warning = true;
7019 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7021 current_function_returns_null = 1;
7022 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7023 pedwarn ("%<return%> with a value, in function returning void");
7025 else
7027 tree t = convert_for_assignment (valtype, retval, ic_return,
7028 NULL_TREE, NULL_TREE, 0);
7029 tree res = DECL_RESULT (current_function_decl);
7030 tree inner;
7032 current_function_returns_value = 1;
7033 if (t == error_mark_node)
7034 return NULL_TREE;
7036 inner = t = convert (TREE_TYPE (res), t);
7038 /* Strip any conversions, additions, and subtractions, and see if
7039 we are returning the address of a local variable. Warn if so. */
7040 while (1)
7042 switch (TREE_CODE (inner))
7044 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7045 case PLUS_EXPR:
7046 inner = TREE_OPERAND (inner, 0);
7047 continue;
7049 case MINUS_EXPR:
7050 /* If the second operand of the MINUS_EXPR has a pointer
7051 type (or is converted from it), this may be valid, so
7052 don't give a warning. */
7054 tree op1 = TREE_OPERAND (inner, 1);
7056 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7057 && (TREE_CODE (op1) == NOP_EXPR
7058 || TREE_CODE (op1) == NON_LVALUE_EXPR
7059 || TREE_CODE (op1) == CONVERT_EXPR))
7060 op1 = TREE_OPERAND (op1, 0);
7062 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7063 break;
7065 inner = TREE_OPERAND (inner, 0);
7066 continue;
7069 case ADDR_EXPR:
7070 inner = TREE_OPERAND (inner, 0);
7072 while (REFERENCE_CLASS_P (inner)
7073 && TREE_CODE (inner) != INDIRECT_REF)
7074 inner = TREE_OPERAND (inner, 0);
7076 if (DECL_P (inner)
7077 && !DECL_EXTERNAL (inner)
7078 && !TREE_STATIC (inner)
7079 && DECL_CONTEXT (inner) == current_function_decl)
7080 warning (0, "function returns address of local variable");
7081 break;
7083 default:
7084 break;
7087 break;
7090 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7093 ret_stmt = build_stmt (RETURN_EXPR, retval);
7094 TREE_NO_WARNING (ret_stmt) |= no_warning;
7095 return add_stmt (ret_stmt);
7098 struct c_switch {
7099 /* The SWITCH_EXPR being built. */
7100 tree switch_expr;
7102 /* The original type of the testing expression, i.e. before the
7103 default conversion is applied. */
7104 tree orig_type;
7106 /* A splay-tree mapping the low element of a case range to the high
7107 element, or NULL_TREE if there is no high element. Used to
7108 determine whether or not a new case label duplicates an old case
7109 label. We need a tree, rather than simply a hash table, because
7110 of the GNU case range extension. */
7111 splay_tree cases;
7113 /* Number of nested statement expressions within this switch
7114 statement; if nonzero, case and default labels may not
7115 appear. */
7116 unsigned int blocked_stmt_expr;
7118 /* Scope of outermost declarations of identifiers with variably
7119 modified type within this switch statement; if nonzero, case and
7120 default labels may not appear. */
7121 unsigned int blocked_vm;
7123 /* The next node on the stack. */
7124 struct c_switch *next;
7127 /* A stack of the currently active switch statements. The innermost
7128 switch statement is on the top of the stack. There is no need to
7129 mark the stack for garbage collection because it is only active
7130 during the processing of the body of a function, and we never
7131 collect at that point. */
7133 struct c_switch *c_switch_stack;
7135 /* Start a C switch statement, testing expression EXP. Return the new
7136 SWITCH_EXPR. */
7138 tree
7139 c_start_case (tree exp)
7141 tree orig_type = error_mark_node;
7142 struct c_switch *cs;
7144 if (exp != error_mark_node)
7146 orig_type = TREE_TYPE (exp);
7148 if (!INTEGRAL_TYPE_P (orig_type))
7150 if (orig_type != error_mark_node)
7152 error ("switch quantity not an integer");
7153 orig_type = error_mark_node;
7155 exp = integer_zero_node;
7157 else
7159 tree type = TYPE_MAIN_VARIANT (orig_type);
7161 if (!in_system_header
7162 && (type == long_integer_type_node
7163 || type == long_unsigned_type_node))
7164 warning (OPT_Wtraditional, "%<long%> switch expression not "
7165 "converted to %<int%> in ISO C");
7167 exp = default_conversion (exp);
7171 /* Add this new SWITCH_EXPR to the stack. */
7172 cs = XNEW (struct c_switch);
7173 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7174 cs->orig_type = orig_type;
7175 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7176 cs->blocked_stmt_expr = 0;
7177 cs->blocked_vm = 0;
7178 cs->next = c_switch_stack;
7179 c_switch_stack = cs;
7181 return add_stmt (cs->switch_expr);
7184 /* Process a case label. */
7186 tree
7187 do_case (tree low_value, tree high_value)
7189 tree label = NULL_TREE;
7191 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7192 && !c_switch_stack->blocked_vm)
7194 label = c_add_case_label (c_switch_stack->cases,
7195 SWITCH_COND (c_switch_stack->switch_expr),
7196 c_switch_stack->orig_type,
7197 low_value, high_value);
7198 if (label == error_mark_node)
7199 label = NULL_TREE;
7201 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7203 if (low_value)
7204 error ("case label in statement expression not containing "
7205 "enclosing switch statement");
7206 else
7207 error ("%<default%> label in statement expression not containing "
7208 "enclosing switch statement");
7210 else if (c_switch_stack && c_switch_stack->blocked_vm)
7212 if (low_value)
7213 error ("case label in scope of identifier with variably modified "
7214 "type not containing enclosing switch statement");
7215 else
7216 error ("%<default%> label in scope of identifier with variably "
7217 "modified type not containing enclosing switch statement");
7219 else if (low_value)
7220 error ("case label not within a switch statement");
7221 else
7222 error ("%<default%> label not within a switch statement");
7224 return label;
7227 /* Finish the switch statement. */
7229 void
7230 c_finish_case (tree body)
7232 struct c_switch *cs = c_switch_stack;
7233 location_t switch_location;
7235 SWITCH_BODY (cs->switch_expr) = body;
7237 /* We must not be within a statement expression nested in the switch
7238 at this point; we might, however, be within the scope of an
7239 identifier with variably modified type nested in the switch. */
7240 gcc_assert (!cs->blocked_stmt_expr);
7242 /* Emit warnings as needed. */
7243 if (EXPR_HAS_LOCATION (cs->switch_expr))
7244 switch_location = EXPR_LOCATION (cs->switch_expr);
7245 else
7246 switch_location = input_location;
7247 c_do_switch_warnings (cs->cases, switch_location,
7248 TREE_TYPE (cs->switch_expr),
7249 SWITCH_COND (cs->switch_expr));
7251 /* Pop the stack. */
7252 c_switch_stack = cs->next;
7253 splay_tree_delete (cs->cases);
7254 XDELETE (cs);
7257 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7258 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7259 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7260 statement, and was not surrounded with parenthesis. */
7262 void
7263 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7264 tree else_block, bool nested_if)
7266 tree stmt;
7268 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7269 if (warn_parentheses && nested_if && else_block == NULL)
7271 tree inner_if = then_block;
7273 /* We know from the grammar productions that there is an IF nested
7274 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7275 it might not be exactly THEN_BLOCK, but should be the last
7276 non-container statement within. */
7277 while (1)
7278 switch (TREE_CODE (inner_if))
7280 case COND_EXPR:
7281 goto found;
7282 case BIND_EXPR:
7283 inner_if = BIND_EXPR_BODY (inner_if);
7284 break;
7285 case STATEMENT_LIST:
7286 inner_if = expr_last (then_block);
7287 break;
7288 case TRY_FINALLY_EXPR:
7289 case TRY_CATCH_EXPR:
7290 inner_if = TREE_OPERAND (inner_if, 0);
7291 break;
7292 default:
7293 gcc_unreachable ();
7295 found:
7297 if (COND_EXPR_ELSE (inner_if))
7298 warning (OPT_Wparentheses,
7299 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7300 &if_locus);
7303 empty_body_warning (then_block, else_block);
7305 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7306 SET_EXPR_LOCATION (stmt, if_locus);
7307 add_stmt (stmt);
7310 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7311 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7312 is false for DO loops. INCR is the FOR increment expression. BODY is
7313 the statement controlled by the loop. BLAB is the break label. CLAB is
7314 the continue label. Everything is allowed to be NULL. */
7316 void
7317 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7318 tree blab, tree clab, bool cond_is_first)
7320 tree entry = NULL, exit = NULL, t;
7322 /* If the condition is zero don't generate a loop construct. */
7323 if (cond && integer_zerop (cond))
7325 if (cond_is_first)
7327 t = build_and_jump (&blab);
7328 SET_EXPR_LOCATION (t, start_locus);
7329 add_stmt (t);
7332 else
7334 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7336 /* If we have an exit condition, then we build an IF with gotos either
7337 out of the loop, or to the top of it. If there's no exit condition,
7338 then we just build a jump back to the top. */
7339 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7341 if (cond && !integer_nonzerop (cond))
7343 /* Canonicalize the loop condition to the end. This means
7344 generating a branch to the loop condition. Reuse the
7345 continue label, if possible. */
7346 if (cond_is_first)
7348 if (incr || !clab)
7350 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7351 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7353 else
7354 t = build1 (GOTO_EXPR, void_type_node, clab);
7355 SET_EXPR_LOCATION (t, start_locus);
7356 add_stmt (t);
7359 t = build_and_jump (&blab);
7360 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7361 if (cond_is_first)
7362 SET_EXPR_LOCATION (exit, start_locus);
7363 else
7364 SET_EXPR_LOCATION (exit, input_location);
7367 add_stmt (top);
7370 if (body)
7371 add_stmt (body);
7372 if (clab)
7373 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7374 if (incr)
7375 add_stmt (incr);
7376 if (entry)
7377 add_stmt (entry);
7378 if (exit)
7379 add_stmt (exit);
7380 if (blab)
7381 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7384 tree
7385 c_finish_bc_stmt (tree *label_p, bool is_break)
7387 bool skip;
7388 tree label = *label_p;
7390 /* In switch statements break is sometimes stylistically used after
7391 a return statement. This can lead to spurious warnings about
7392 control reaching the end of a non-void function when it is
7393 inlined. Note that we are calling block_may_fallthru with
7394 language specific tree nodes; this works because
7395 block_may_fallthru returns true when given something it does not
7396 understand. */
7397 skip = !block_may_fallthru (cur_stmt_list);
7399 if (!label)
7401 if (!skip)
7402 *label_p = label = create_artificial_label ();
7404 else if (TREE_CODE (label) == LABEL_DECL)
7406 else switch (TREE_INT_CST_LOW (label))
7408 case 0:
7409 if (is_break)
7410 error ("break statement not within loop or switch");
7411 else
7412 error ("continue statement not within a loop");
7413 return NULL_TREE;
7415 case 1:
7416 gcc_assert (is_break);
7417 error ("break statement used with OpenMP for loop");
7418 return NULL_TREE;
7420 default:
7421 gcc_unreachable ();
7424 if (skip)
7425 return NULL_TREE;
7427 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7430 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7432 static void
7433 emit_side_effect_warnings (tree expr)
7435 if (expr == error_mark_node)
7437 else if (!TREE_SIDE_EFFECTS (expr))
7439 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7440 warning (0, "%Hstatement with no effect",
7441 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7443 else if (warn_unused_value)
7444 warn_if_unused_value (expr, input_location);
7447 /* Process an expression as if it were a complete statement. Emit
7448 diagnostics, but do not call ADD_STMT. */
7450 tree
7451 c_process_expr_stmt (tree expr)
7453 if (!expr)
7454 return NULL_TREE;
7456 if (warn_sequence_point)
7457 verify_sequence_points (expr);
7459 if (TREE_TYPE (expr) != error_mark_node
7460 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7461 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7462 error ("expression statement has incomplete type");
7464 /* If we're not processing a statement expression, warn about unused values.
7465 Warnings for statement expressions will be emitted later, once we figure
7466 out which is the result. */
7467 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7468 && (extra_warnings || warn_unused_value))
7469 emit_side_effect_warnings (expr);
7471 /* If the expression is not of a type to which we cannot assign a line
7472 number, wrap the thing in a no-op NOP_EXPR. */
7473 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7474 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7476 if (EXPR_P (expr))
7477 SET_EXPR_LOCATION (expr, input_location);
7479 return expr;
7482 /* Emit an expression as a statement. */
7484 tree
7485 c_finish_expr_stmt (tree expr)
7487 if (expr)
7488 return add_stmt (c_process_expr_stmt (expr));
7489 else
7490 return NULL;
7493 /* Do the opposite and emit a statement as an expression. To begin,
7494 create a new binding level and return it. */
7496 tree
7497 c_begin_stmt_expr (void)
7499 tree ret;
7500 struct c_label_context_se *nstack;
7501 struct c_label_list *glist;
7503 /* We must force a BLOCK for this level so that, if it is not expanded
7504 later, there is a way to turn off the entire subtree of blocks that
7505 are contained in it. */
7506 keep_next_level ();
7507 ret = c_begin_compound_stmt (true);
7508 if (c_switch_stack)
7510 c_switch_stack->blocked_stmt_expr++;
7511 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7513 for (glist = label_context_stack_se->labels_used;
7514 glist != NULL;
7515 glist = glist->next)
7517 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7519 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7520 nstack->labels_def = NULL;
7521 nstack->labels_used = NULL;
7522 nstack->next = label_context_stack_se;
7523 label_context_stack_se = nstack;
7525 /* Mark the current statement list as belonging to a statement list. */
7526 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7528 return ret;
7531 tree
7532 c_finish_stmt_expr (tree body)
7534 tree last, type, tmp, val;
7535 tree *last_p;
7536 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7538 body = c_end_compound_stmt (body, true);
7539 if (c_switch_stack)
7541 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7542 c_switch_stack->blocked_stmt_expr--;
7544 /* It is no longer possible to jump to labels defined within this
7545 statement expression. */
7546 for (dlist = label_context_stack_se->labels_def;
7547 dlist != NULL;
7548 dlist = dlist->next)
7550 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7552 /* It is again possible to define labels with a goto just outside
7553 this statement expression. */
7554 for (glist = label_context_stack_se->next->labels_used;
7555 glist != NULL;
7556 glist = glist->next)
7558 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7559 glist_prev = glist;
7561 if (glist_prev != NULL)
7562 glist_prev->next = label_context_stack_se->labels_used;
7563 else
7564 label_context_stack_se->next->labels_used
7565 = label_context_stack_se->labels_used;
7566 label_context_stack_se = label_context_stack_se->next;
7568 /* Locate the last statement in BODY. See c_end_compound_stmt
7569 about always returning a BIND_EXPR. */
7570 last_p = &BIND_EXPR_BODY (body);
7571 last = BIND_EXPR_BODY (body);
7573 continue_searching:
7574 if (TREE_CODE (last) == STATEMENT_LIST)
7576 tree_stmt_iterator i;
7578 /* This can happen with degenerate cases like ({ }). No value. */
7579 if (!TREE_SIDE_EFFECTS (last))
7580 return body;
7582 /* If we're supposed to generate side effects warnings, process
7583 all of the statements except the last. */
7584 if (extra_warnings || warn_unused_value)
7586 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7587 emit_side_effect_warnings (tsi_stmt (i));
7589 else
7590 i = tsi_last (last);
7591 last_p = tsi_stmt_ptr (i);
7592 last = *last_p;
7595 /* If the end of the list is exception related, then the list was split
7596 by a call to push_cleanup. Continue searching. */
7597 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7598 || TREE_CODE (last) == TRY_CATCH_EXPR)
7600 last_p = &TREE_OPERAND (last, 0);
7601 last = *last_p;
7602 goto continue_searching;
7605 /* In the case that the BIND_EXPR is not necessary, return the
7606 expression out from inside it. */
7607 if (last == error_mark_node
7608 || (last == BIND_EXPR_BODY (body)
7609 && BIND_EXPR_VARS (body) == NULL))
7611 /* Do not warn if the return value of a statement expression is
7612 unused. */
7613 if (EXPR_P (last))
7614 TREE_NO_WARNING (last) = 1;
7615 return last;
7618 /* Extract the type of said expression. */
7619 type = TREE_TYPE (last);
7621 /* If we're not returning a value at all, then the BIND_EXPR that
7622 we already have is a fine expression to return. */
7623 if (!type || VOID_TYPE_P (type))
7624 return body;
7626 /* Now that we've located the expression containing the value, it seems
7627 silly to make voidify_wrapper_expr repeat the process. Create a
7628 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7629 tmp = create_tmp_var_raw (type, NULL);
7631 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7632 tree_expr_nonnegative_p giving up immediately. */
7633 val = last;
7634 if (TREE_CODE (val) == NOP_EXPR
7635 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7636 val = TREE_OPERAND (val, 0);
7638 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7639 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7641 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7644 /* Begin the scope of an identifier of variably modified type, scope
7645 number SCOPE. Jumping from outside this scope to inside it is not
7646 permitted. */
7648 void
7649 c_begin_vm_scope (unsigned int scope)
7651 struct c_label_context_vm *nstack;
7652 struct c_label_list *glist;
7654 gcc_assert (scope > 0);
7656 /* At file_scope, we don't have to do any processing. */
7657 if (label_context_stack_vm == NULL)
7658 return;
7660 if (c_switch_stack && !c_switch_stack->blocked_vm)
7661 c_switch_stack->blocked_vm = scope;
7662 for (glist = label_context_stack_vm->labels_used;
7663 glist != NULL;
7664 glist = glist->next)
7666 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7668 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7669 nstack->labels_def = NULL;
7670 nstack->labels_used = NULL;
7671 nstack->scope = scope;
7672 nstack->next = label_context_stack_vm;
7673 label_context_stack_vm = nstack;
7676 /* End a scope which may contain identifiers of variably modified
7677 type, scope number SCOPE. */
7679 void
7680 c_end_vm_scope (unsigned int scope)
7682 if (label_context_stack_vm == NULL)
7683 return;
7684 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7685 c_switch_stack->blocked_vm = 0;
7686 /* We may have a number of nested scopes of identifiers with
7687 variably modified type, all at this depth. Pop each in turn. */
7688 while (label_context_stack_vm->scope == scope)
7690 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7692 /* It is no longer possible to jump to labels defined within this
7693 scope. */
7694 for (dlist = label_context_stack_vm->labels_def;
7695 dlist != NULL;
7696 dlist = dlist->next)
7698 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7700 /* It is again possible to define labels with a goto just outside
7701 this scope. */
7702 for (glist = label_context_stack_vm->next->labels_used;
7703 glist != NULL;
7704 glist = glist->next)
7706 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7707 glist_prev = glist;
7709 if (glist_prev != NULL)
7710 glist_prev->next = label_context_stack_vm->labels_used;
7711 else
7712 label_context_stack_vm->next->labels_used
7713 = label_context_stack_vm->labels_used;
7714 label_context_stack_vm = label_context_stack_vm->next;
7718 /* Begin and end compound statements. This is as simple as pushing
7719 and popping new statement lists from the tree. */
7721 tree
7722 c_begin_compound_stmt (bool do_scope)
7724 tree stmt = push_stmt_list ();
7725 if (do_scope)
7726 push_scope ();
7727 return stmt;
7730 tree
7731 c_end_compound_stmt (tree stmt, bool do_scope)
7733 tree block = NULL;
7735 if (do_scope)
7737 if (c_dialect_objc ())
7738 objc_clear_super_receiver ();
7739 block = pop_scope ();
7742 stmt = pop_stmt_list (stmt);
7743 stmt = c_build_bind_expr (block, stmt);
7745 /* If this compound statement is nested immediately inside a statement
7746 expression, then force a BIND_EXPR to be created. Otherwise we'll
7747 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7748 STATEMENT_LISTs merge, and thus we can lose track of what statement
7749 was really last. */
7750 if (cur_stmt_list
7751 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7752 && TREE_CODE (stmt) != BIND_EXPR)
7754 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7755 TREE_SIDE_EFFECTS (stmt) = 1;
7758 return stmt;
7761 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7762 when the current scope is exited. EH_ONLY is true when this is not
7763 meant to apply to normal control flow transfer. */
7765 void
7766 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7768 enum tree_code code;
7769 tree stmt, list;
7770 bool stmt_expr;
7772 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7773 stmt = build_stmt (code, NULL, cleanup);
7774 add_stmt (stmt);
7775 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7776 list = push_stmt_list ();
7777 TREE_OPERAND (stmt, 0) = list;
7778 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7781 /* Build a binary-operation expression without default conversions.
7782 CODE is the kind of expression to build.
7783 This function differs from `build' in several ways:
7784 the data type of the result is computed and recorded in it,
7785 warnings are generated if arg data types are invalid,
7786 special handling for addition and subtraction of pointers is known,
7787 and some optimization is done (operations on narrow ints
7788 are done in the narrower type when that gives the same result).
7789 Constant folding is also done before the result is returned.
7791 Note that the operands will never have enumeral types, or function
7792 or array types, because either they will have the default conversions
7793 performed or they have both just been converted to some other type in which
7794 the arithmetic is to be done. */
7796 tree
7797 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7798 int convert_p)
7800 tree type0, type1;
7801 enum tree_code code0, code1;
7802 tree op0, op1;
7803 const char *invalid_op_diag;
7805 /* Expression code to give to the expression when it is built.
7806 Normally this is CODE, which is what the caller asked for,
7807 but in some special cases we change it. */
7808 enum tree_code resultcode = code;
7810 /* Data type in which the computation is to be performed.
7811 In the simplest cases this is the common type of the arguments. */
7812 tree result_type = NULL;
7814 /* Nonzero means operands have already been type-converted
7815 in whatever way is necessary.
7816 Zero means they need to be converted to RESULT_TYPE. */
7817 int converted = 0;
7819 /* Nonzero means create the expression with this type, rather than
7820 RESULT_TYPE. */
7821 tree build_type = 0;
7823 /* Nonzero means after finally constructing the expression
7824 convert it to this type. */
7825 tree final_type = 0;
7827 /* Nonzero if this is an operation like MIN or MAX which can
7828 safely be computed in short if both args are promoted shorts.
7829 Also implies COMMON.
7830 -1 indicates a bitwise operation; this makes a difference
7831 in the exact conditions for when it is safe to do the operation
7832 in a narrower mode. */
7833 int shorten = 0;
7835 /* Nonzero if this is a comparison operation;
7836 if both args are promoted shorts, compare the original shorts.
7837 Also implies COMMON. */
7838 int short_compare = 0;
7840 /* Nonzero if this is a right-shift operation, which can be computed on the
7841 original short and then promoted if the operand is a promoted short. */
7842 int short_shift = 0;
7844 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7845 int common = 0;
7847 /* True means types are compatible as far as ObjC is concerned. */
7848 bool objc_ok;
7850 if (convert_p)
7852 op0 = default_conversion (orig_op0);
7853 op1 = default_conversion (orig_op1);
7855 else
7857 op0 = orig_op0;
7858 op1 = orig_op1;
7861 type0 = TREE_TYPE (op0);
7862 type1 = TREE_TYPE (op1);
7864 /* The expression codes of the data types of the arguments tell us
7865 whether the arguments are integers, floating, pointers, etc. */
7866 code0 = TREE_CODE (type0);
7867 code1 = TREE_CODE (type1);
7869 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7870 STRIP_TYPE_NOPS (op0);
7871 STRIP_TYPE_NOPS (op1);
7873 /* If an error was already reported for one of the arguments,
7874 avoid reporting another error. */
7876 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7877 return error_mark_node;
7879 if ((invalid_op_diag
7880 = targetm.invalid_binary_op (code, type0, type1)))
7882 error (invalid_op_diag);
7883 return error_mark_node;
7886 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7888 switch (code)
7890 case PLUS_EXPR:
7891 /* Handle the pointer + int case. */
7892 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7893 return pointer_int_sum (PLUS_EXPR, op0, op1);
7894 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7895 return pointer_int_sum (PLUS_EXPR, op1, op0);
7896 else
7897 common = 1;
7898 break;
7900 case MINUS_EXPR:
7901 /* Subtraction of two similar pointers.
7902 We must subtract them as integers, then divide by object size. */
7903 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7904 && comp_target_types (type0, type1))
7905 return pointer_diff (op0, op1);
7906 /* Handle pointer minus int. Just like pointer plus int. */
7907 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7908 return pointer_int_sum (MINUS_EXPR, op0, op1);
7909 else
7910 common = 1;
7911 break;
7913 case MULT_EXPR:
7914 common = 1;
7915 break;
7917 case TRUNC_DIV_EXPR:
7918 case CEIL_DIV_EXPR:
7919 case FLOOR_DIV_EXPR:
7920 case ROUND_DIV_EXPR:
7921 case EXACT_DIV_EXPR:
7922 /* Floating point division by zero is a legitimate way to obtain
7923 infinities and NaNs. */
7924 if (skip_evaluation == 0 && integer_zerop (op1))
7925 warning (OPT_Wdiv_by_zero, "division by zero");
7927 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7928 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7929 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7930 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7932 enum tree_code tcode0 = code0, tcode1 = code1;
7934 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7935 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7936 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7937 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7939 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7940 resultcode = RDIV_EXPR;
7941 else
7942 /* Although it would be tempting to shorten always here, that
7943 loses on some targets, since the modulo instruction is
7944 undefined if the quotient can't be represented in the
7945 computation mode. We shorten only if unsigned or if
7946 dividing by something we know != -1. */
7947 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7948 || (TREE_CODE (op1) == INTEGER_CST
7949 && !integer_all_onesp (op1)));
7950 common = 1;
7952 break;
7954 case BIT_AND_EXPR:
7955 case BIT_IOR_EXPR:
7956 case BIT_XOR_EXPR:
7957 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7958 shorten = -1;
7959 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7960 common = 1;
7961 break;
7963 case TRUNC_MOD_EXPR:
7964 case FLOOR_MOD_EXPR:
7965 if (skip_evaluation == 0 && integer_zerop (op1))
7966 warning (OPT_Wdiv_by_zero, "division by zero");
7968 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7970 /* Although it would be tempting to shorten always here, that loses
7971 on some targets, since the modulo instruction is undefined if the
7972 quotient can't be represented in the computation mode. We shorten
7973 only if unsigned or if dividing by something we know != -1. */
7974 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7975 || (TREE_CODE (op1) == INTEGER_CST
7976 && !integer_all_onesp (op1)));
7977 common = 1;
7979 break;
7981 case TRUTH_ANDIF_EXPR:
7982 case TRUTH_ORIF_EXPR:
7983 case TRUTH_AND_EXPR:
7984 case TRUTH_OR_EXPR:
7985 case TRUTH_XOR_EXPR:
7986 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7987 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7988 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7989 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7991 /* Result of these operations is always an int,
7992 but that does not mean the operands should be
7993 converted to ints! */
7994 result_type = integer_type_node;
7995 op0 = c_common_truthvalue_conversion (op0);
7996 op1 = c_common_truthvalue_conversion (op1);
7997 converted = 1;
7999 break;
8001 /* Shift operations: result has same type as first operand;
8002 always convert second operand to int.
8003 Also set SHORT_SHIFT if shifting rightward. */
8005 case RSHIFT_EXPR:
8006 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8008 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8010 if (tree_int_cst_sgn (op1) < 0)
8011 warning (0, "right shift count is negative");
8012 else
8014 if (!integer_zerop (op1))
8015 short_shift = 1;
8017 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8018 warning (0, "right shift count >= width of type");
8022 /* Use the type of the value to be shifted. */
8023 result_type = type0;
8024 /* Convert the shift-count to an integer, regardless of size
8025 of value being shifted. */
8026 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8027 op1 = convert (integer_type_node, op1);
8028 /* Avoid converting op1 to result_type later. */
8029 converted = 1;
8031 break;
8033 case LSHIFT_EXPR:
8034 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8036 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8038 if (tree_int_cst_sgn (op1) < 0)
8039 warning (0, "left shift count is negative");
8041 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8042 warning (0, "left shift count >= width of type");
8045 /* Use the type of the value to be shifted. */
8046 result_type = type0;
8047 /* Convert the shift-count to an integer, regardless of size
8048 of value being shifted. */
8049 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8050 op1 = convert (integer_type_node, op1);
8051 /* Avoid converting op1 to result_type later. */
8052 converted = 1;
8054 break;
8056 case EQ_EXPR:
8057 case NE_EXPR:
8058 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8059 warning (OPT_Wfloat_equal,
8060 "comparing floating point with == or != is unsafe");
8061 /* Result of comparison is always int,
8062 but don't convert the args to int! */
8063 build_type = integer_type_node;
8064 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8065 || code0 == COMPLEX_TYPE)
8066 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8067 || code1 == COMPLEX_TYPE))
8068 short_compare = 1;
8069 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8071 tree tt0 = TREE_TYPE (type0);
8072 tree tt1 = TREE_TYPE (type1);
8073 /* Anything compares with void *. void * compares with anything.
8074 Otherwise, the targets must be compatible
8075 and both must be object or both incomplete. */
8076 if (comp_target_types (type0, type1))
8077 result_type = common_pointer_type (type0, type1);
8078 else if (VOID_TYPE_P (tt0))
8080 /* op0 != orig_op0 detects the case of something
8081 whose value is 0 but which isn't a valid null ptr const. */
8082 if (pedantic && !null_pointer_constant_p (orig_op0)
8083 && TREE_CODE (tt1) == FUNCTION_TYPE)
8084 pedwarn ("ISO C forbids comparison of %<void *%>"
8085 " with function pointer");
8087 else if (VOID_TYPE_P (tt1))
8089 if (pedantic && !null_pointer_constant_p (orig_op1)
8090 && TREE_CODE (tt0) == FUNCTION_TYPE)
8091 pedwarn ("ISO C forbids comparison of %<void *%>"
8092 " with function pointer");
8094 else
8095 /* Avoid warning about the volatile ObjC EH puts on decls. */
8096 if (!objc_ok)
8097 pedwarn ("comparison of distinct pointer types lacks a cast");
8099 if (result_type == NULL_TREE)
8100 result_type = ptr_type_node;
8102 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8104 if (TREE_CODE (op0) == ADDR_EXPR
8105 && DECL_P (TREE_OPERAND (op0, 0))
8106 && (TREE_CODE (TREE_OPERAND (op0, 0)) == PARM_DECL
8107 || TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
8108 || !DECL_WEAK (TREE_OPERAND (op0, 0))))
8109 warning (OPT_Waddress, "the address of %qD will never be NULL",
8110 TREE_OPERAND (op0, 0));
8111 result_type = type0;
8113 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8115 if (TREE_CODE (op1) == ADDR_EXPR
8116 && DECL_P (TREE_OPERAND (op1, 0))
8117 && (TREE_CODE (TREE_OPERAND (op1, 0)) == PARM_DECL
8118 || TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL
8119 || !DECL_WEAK (TREE_OPERAND (op1, 0))))
8120 warning (OPT_Waddress, "the address of %qD will never be NULL",
8121 TREE_OPERAND (op1, 0));
8122 result_type = type1;
8124 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8126 result_type = type0;
8127 pedwarn ("comparison between pointer and integer");
8129 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8131 result_type = type1;
8132 pedwarn ("comparison between pointer and integer");
8134 break;
8136 case LE_EXPR:
8137 case GE_EXPR:
8138 case LT_EXPR:
8139 case GT_EXPR:
8140 build_type = integer_type_node;
8141 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
8142 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
8143 short_compare = 1;
8144 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8146 if (comp_target_types (type0, type1))
8148 result_type = common_pointer_type (type0, type1);
8149 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8150 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8151 pedwarn ("comparison of complete and incomplete pointers");
8152 else if (pedantic
8153 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8154 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8156 else
8158 result_type = ptr_type_node;
8159 pedwarn ("comparison of distinct pointer types lacks a cast");
8162 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8164 result_type = type0;
8165 if (pedantic || extra_warnings)
8166 pedwarn ("ordered comparison of pointer with integer zero");
8168 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8170 result_type = type1;
8171 if (pedantic)
8172 pedwarn ("ordered comparison of pointer with integer zero");
8174 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8176 result_type = type0;
8177 pedwarn ("comparison between pointer and integer");
8179 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8181 result_type = type1;
8182 pedwarn ("comparison between pointer and integer");
8184 break;
8186 default:
8187 gcc_unreachable ();
8190 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8191 return error_mark_node;
8193 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8194 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8195 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8196 TREE_TYPE (type1))))
8198 binary_op_error (code);
8199 return error_mark_node;
8202 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8203 || code0 == VECTOR_TYPE)
8205 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8206 || code1 == VECTOR_TYPE))
8208 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8210 if (shorten || common || short_compare)
8211 result_type = c_common_type (type0, type1);
8213 /* For certain operations (which identify themselves by shorten != 0)
8214 if both args were extended from the same smaller type,
8215 do the arithmetic in that type and then extend.
8217 shorten !=0 and !=1 indicates a bitwise operation.
8218 For them, this optimization is safe only if
8219 both args are zero-extended or both are sign-extended.
8220 Otherwise, we might change the result.
8221 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8222 but calculated in (unsigned short) it would be (unsigned short)-1. */
8224 if (shorten && none_complex)
8226 int unsigned0, unsigned1;
8227 tree arg0, arg1;
8228 int uns;
8229 tree type;
8231 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8232 excessive narrowing when we call get_narrower below. For
8233 example, suppose that OP0 is of unsigned int extended
8234 from signed char and that RESULT_TYPE is long long int.
8235 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8236 like
8238 (long long int) (unsigned int) signed_char
8240 which get_narrower would narrow down to
8242 (unsigned int) signed char
8244 If we do not cast OP0 first, get_narrower would return
8245 signed_char, which is inconsistent with the case of the
8246 explicit cast. */
8247 op0 = convert (result_type, op0);
8248 op1 = convert (result_type, op1);
8250 arg0 = get_narrower (op0, &unsigned0);
8251 arg1 = get_narrower (op1, &unsigned1);
8253 /* UNS is 1 if the operation to be done is an unsigned one. */
8254 uns = TYPE_UNSIGNED (result_type);
8256 final_type = result_type;
8258 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8259 but it *requires* conversion to FINAL_TYPE. */
8261 if ((TYPE_PRECISION (TREE_TYPE (op0))
8262 == TYPE_PRECISION (TREE_TYPE (arg0)))
8263 && TREE_TYPE (op0) != final_type)
8264 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8265 if ((TYPE_PRECISION (TREE_TYPE (op1))
8266 == TYPE_PRECISION (TREE_TYPE (arg1)))
8267 && TREE_TYPE (op1) != final_type)
8268 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8270 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8272 /* For bitwise operations, signedness of nominal type
8273 does not matter. Consider only how operands were extended. */
8274 if (shorten == -1)
8275 uns = unsigned0;
8277 /* Note that in all three cases below we refrain from optimizing
8278 an unsigned operation on sign-extended args.
8279 That would not be valid. */
8281 /* Both args variable: if both extended in same way
8282 from same width, do it in that width.
8283 Do it unsigned if args were zero-extended. */
8284 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8285 < TYPE_PRECISION (result_type))
8286 && (TYPE_PRECISION (TREE_TYPE (arg1))
8287 == TYPE_PRECISION (TREE_TYPE (arg0)))
8288 && unsigned0 == unsigned1
8289 && (unsigned0 || !uns))
8290 result_type
8291 = c_common_signed_or_unsigned_type
8292 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8293 else if (TREE_CODE (arg0) == INTEGER_CST
8294 && (unsigned1 || !uns)
8295 && (TYPE_PRECISION (TREE_TYPE (arg1))
8296 < TYPE_PRECISION (result_type))
8297 && (type
8298 = c_common_signed_or_unsigned_type (unsigned1,
8299 TREE_TYPE (arg1)),
8300 int_fits_type_p (arg0, type)))
8301 result_type = type;
8302 else if (TREE_CODE (arg1) == INTEGER_CST
8303 && (unsigned0 || !uns)
8304 && (TYPE_PRECISION (TREE_TYPE (arg0))
8305 < TYPE_PRECISION (result_type))
8306 && (type
8307 = c_common_signed_or_unsigned_type (unsigned0,
8308 TREE_TYPE (arg0)),
8309 int_fits_type_p (arg1, type)))
8310 result_type = type;
8313 /* Shifts can be shortened if shifting right. */
8315 if (short_shift)
8317 int unsigned_arg;
8318 tree arg0 = get_narrower (op0, &unsigned_arg);
8320 final_type = result_type;
8322 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8323 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8325 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8326 /* We can shorten only if the shift count is less than the
8327 number of bits in the smaller type size. */
8328 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8329 /* We cannot drop an unsigned shift after sign-extension. */
8330 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8332 /* Do an unsigned shift if the operand was zero-extended. */
8333 result_type
8334 = c_common_signed_or_unsigned_type (unsigned_arg,
8335 TREE_TYPE (arg0));
8336 /* Convert value-to-be-shifted to that type. */
8337 if (TREE_TYPE (op0) != result_type)
8338 op0 = convert (result_type, op0);
8339 converted = 1;
8343 /* Comparison operations are shortened too but differently.
8344 They identify themselves by setting short_compare = 1. */
8346 if (short_compare)
8348 /* Don't write &op0, etc., because that would prevent op0
8349 from being kept in a register.
8350 Instead, make copies of the our local variables and
8351 pass the copies by reference, then copy them back afterward. */
8352 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8353 enum tree_code xresultcode = resultcode;
8354 tree val
8355 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8357 if (val != 0)
8358 return val;
8360 op0 = xop0, op1 = xop1;
8361 converted = 1;
8362 resultcode = xresultcode;
8364 if (warn_sign_compare && skip_evaluation == 0)
8366 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8367 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8368 int unsignedp0, unsignedp1;
8369 tree primop0 = get_narrower (op0, &unsignedp0);
8370 tree primop1 = get_narrower (op1, &unsignedp1);
8372 xop0 = orig_op0;
8373 xop1 = orig_op1;
8374 STRIP_TYPE_NOPS (xop0);
8375 STRIP_TYPE_NOPS (xop1);
8377 /* Give warnings for comparisons between signed and unsigned
8378 quantities that may fail.
8380 Do the checking based on the original operand trees, so that
8381 casts will be considered, but default promotions won't be.
8383 Do not warn if the comparison is being done in a signed type,
8384 since the signed type will only be chosen if it can represent
8385 all the values of the unsigned type. */
8386 if (!TYPE_UNSIGNED (result_type))
8387 /* OK */;
8388 /* Do not warn if both operands are the same signedness. */
8389 else if (op0_signed == op1_signed)
8390 /* OK */;
8391 else
8393 tree sop, uop;
8394 bool ovf;
8396 if (op0_signed)
8397 sop = xop0, uop = xop1;
8398 else
8399 sop = xop1, uop = xop0;
8401 /* Do not warn if the signed quantity is an
8402 unsuffixed integer literal (or some static
8403 constant expression involving such literals or a
8404 conditional expression involving such literals)
8405 and it is non-negative. */
8406 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
8407 /* OK */;
8408 /* Do not warn if the comparison is an equality operation,
8409 the unsigned quantity is an integral constant, and it
8410 would fit in the result if the result were signed. */
8411 else if (TREE_CODE (uop) == INTEGER_CST
8412 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8413 && int_fits_type_p
8414 (uop, c_common_signed_type (result_type)))
8415 /* OK */;
8416 /* Do not warn if the unsigned quantity is an enumeration
8417 constant and its maximum value would fit in the result
8418 if the result were signed. */
8419 else if (TREE_CODE (uop) == INTEGER_CST
8420 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8421 && int_fits_type_p
8422 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8423 c_common_signed_type (result_type)))
8424 /* OK */;
8425 else
8426 warning (0, "comparison between signed and unsigned");
8429 /* Warn if two unsigned values are being compared in a size
8430 larger than their original size, and one (and only one) is the
8431 result of a `~' operator. This comparison will always fail.
8433 Also warn if one operand is a constant, and the constant
8434 does not have all bits set that are set in the ~ operand
8435 when it is extended. */
8437 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8438 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8440 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8441 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8442 &unsignedp0);
8443 else
8444 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8445 &unsignedp1);
8447 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8449 tree primop;
8450 HOST_WIDE_INT constant, mask;
8451 int unsignedp, bits;
8453 if (host_integerp (primop0, 0))
8455 primop = primop1;
8456 unsignedp = unsignedp1;
8457 constant = tree_low_cst (primop0, 0);
8459 else
8461 primop = primop0;
8462 unsignedp = unsignedp0;
8463 constant = tree_low_cst (primop1, 0);
8466 bits = TYPE_PRECISION (TREE_TYPE (primop));
8467 if (bits < TYPE_PRECISION (result_type)
8468 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8470 mask = (~(HOST_WIDE_INT) 0) << bits;
8471 if ((mask & constant) != mask)
8472 warning (0, "comparison of promoted ~unsigned with constant");
8475 else if (unsignedp0 && unsignedp1
8476 && (TYPE_PRECISION (TREE_TYPE (primop0))
8477 < TYPE_PRECISION (result_type))
8478 && (TYPE_PRECISION (TREE_TYPE (primop1))
8479 < TYPE_PRECISION (result_type)))
8480 warning (0, "comparison of promoted ~unsigned with unsigned");
8486 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8487 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8488 Then the expression will be built.
8489 It will be given type FINAL_TYPE if that is nonzero;
8490 otherwise, it will be given type RESULT_TYPE. */
8492 if (!result_type)
8494 binary_op_error (code);
8495 return error_mark_node;
8498 if (!converted)
8500 if (TREE_TYPE (op0) != result_type)
8501 op0 = convert_and_check (result_type, op0);
8502 if (TREE_TYPE (op1) != result_type)
8503 op1 = convert_and_check (result_type, op1);
8505 /* This can happen if one operand has a vector type, and the other
8506 has a different type. */
8507 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8508 return error_mark_node;
8511 if (build_type == NULL_TREE)
8512 build_type = result_type;
8515 /* Treat expressions in initializers specially as they can't trap. */
8516 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8517 build_type,
8518 op0, op1)
8519 : fold_build2 (resultcode, build_type,
8520 op0, op1);
8522 if (final_type != 0)
8523 result = convert (final_type, result);
8524 return result;
8529 /* Convert EXPR to be a truth-value, validating its type for this
8530 purpose. */
8532 tree
8533 c_objc_common_truthvalue_conversion (tree expr)
8535 switch (TREE_CODE (TREE_TYPE (expr)))
8537 case ARRAY_TYPE:
8538 error ("used array that cannot be converted to pointer where scalar is required");
8539 return error_mark_node;
8541 case RECORD_TYPE:
8542 error ("used struct type value where scalar is required");
8543 return error_mark_node;
8545 case UNION_TYPE:
8546 error ("used union type value where scalar is required");
8547 return error_mark_node;
8549 case FUNCTION_TYPE:
8550 gcc_unreachable ();
8552 default:
8553 break;
8556 /* ??? Should we also give an error for void and vectors rather than
8557 leaving those to give errors later? */
8558 return c_common_truthvalue_conversion (expr);
8562 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8563 required. */
8565 tree
8566 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8567 bool *ti ATTRIBUTE_UNUSED, bool *se)
8569 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8571 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8572 /* Executing a compound literal inside a function reinitializes
8573 it. */
8574 if (!TREE_STATIC (decl))
8575 *se = true;
8576 return decl;
8578 else
8579 return expr;
8582 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8584 tree
8585 c_begin_omp_parallel (void)
8587 tree block;
8589 keep_next_level ();
8590 block = c_begin_compound_stmt (true);
8592 return block;
8595 tree
8596 c_finish_omp_parallel (tree clauses, tree block)
8598 tree stmt;
8600 block = c_end_compound_stmt (block, true);
8602 stmt = make_node (OMP_PARALLEL);
8603 TREE_TYPE (stmt) = void_type_node;
8604 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8605 OMP_PARALLEL_BODY (stmt) = block;
8607 return add_stmt (stmt);
8610 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
8611 Remove any elements from the list that are invalid. */
8613 tree
8614 c_finish_omp_clauses (tree clauses)
8616 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8617 tree c, t, *pc = &clauses;
8618 const char *name;
8620 bitmap_obstack_initialize (NULL);
8621 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8622 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8623 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8625 for (pc = &clauses, c = clauses; c ; c = *pc)
8627 bool remove = false;
8628 bool need_complete = false;
8629 bool need_implicitly_determined = false;
8631 switch (OMP_CLAUSE_CODE (c))
8633 case OMP_CLAUSE_SHARED:
8634 name = "shared";
8635 need_implicitly_determined = true;
8636 goto check_dup_generic;
8638 case OMP_CLAUSE_PRIVATE:
8639 name = "private";
8640 need_complete = true;
8641 need_implicitly_determined = true;
8642 goto check_dup_generic;
8644 case OMP_CLAUSE_REDUCTION:
8645 name = "reduction";
8646 need_implicitly_determined = true;
8647 t = OMP_CLAUSE_DECL (c);
8648 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8649 || POINTER_TYPE_P (TREE_TYPE (t)))
8651 error ("%qE has invalid type for %<reduction%>", t);
8652 remove = true;
8654 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8656 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8657 const char *r_name = NULL;
8659 switch (r_code)
8661 case PLUS_EXPR:
8662 case MULT_EXPR:
8663 case MINUS_EXPR:
8664 break;
8665 case BIT_AND_EXPR:
8666 r_name = "&";
8667 break;
8668 case BIT_XOR_EXPR:
8669 r_name = "^";
8670 break;
8671 case BIT_IOR_EXPR:
8672 r_name = "|";
8673 break;
8674 case TRUTH_ANDIF_EXPR:
8675 r_name = "&&";
8676 break;
8677 case TRUTH_ORIF_EXPR:
8678 r_name = "||";
8679 break;
8680 default:
8681 gcc_unreachable ();
8683 if (r_name)
8685 error ("%qE has invalid type for %<reduction(%s)%>",
8686 t, r_name);
8687 remove = true;
8690 goto check_dup_generic;
8692 case OMP_CLAUSE_COPYPRIVATE:
8693 name = "copyprivate";
8694 goto check_dup_generic;
8696 case OMP_CLAUSE_COPYIN:
8697 name = "copyin";
8698 t = OMP_CLAUSE_DECL (c);
8699 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8701 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8702 remove = true;
8704 goto check_dup_generic;
8706 check_dup_generic:
8707 t = OMP_CLAUSE_DECL (c);
8708 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8710 error ("%qE is not a variable in clause %qs", t, name);
8711 remove = true;
8713 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8714 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8715 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8717 error ("%qE appears more than once in data clauses", t);
8718 remove = true;
8720 else
8721 bitmap_set_bit (&generic_head, DECL_UID (t));
8722 break;
8724 case OMP_CLAUSE_FIRSTPRIVATE:
8725 name = "firstprivate";
8726 t = OMP_CLAUSE_DECL (c);
8727 need_complete = true;
8728 need_implicitly_determined = true;
8729 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8731 error ("%qE is not a variable in clause %<firstprivate%>", t);
8732 remove = true;
8734 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8735 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8737 error ("%qE appears more than once in data clauses", t);
8738 remove = true;
8740 else
8741 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8742 break;
8744 case OMP_CLAUSE_LASTPRIVATE:
8745 name = "lastprivate";
8746 t = OMP_CLAUSE_DECL (c);
8747 need_complete = true;
8748 need_implicitly_determined = true;
8749 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8751 error ("%qE is not a variable in clause %<lastprivate%>", t);
8752 remove = true;
8754 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8755 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8757 error ("%qE appears more than once in data clauses", t);
8758 remove = true;
8760 else
8761 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8762 break;
8764 case OMP_CLAUSE_IF:
8765 case OMP_CLAUSE_NUM_THREADS:
8766 case OMP_CLAUSE_SCHEDULE:
8767 case OMP_CLAUSE_NOWAIT:
8768 case OMP_CLAUSE_ORDERED:
8769 case OMP_CLAUSE_DEFAULT:
8770 pc = &OMP_CLAUSE_CHAIN (c);
8771 continue;
8773 default:
8774 gcc_unreachable ();
8777 if (!remove)
8779 t = OMP_CLAUSE_DECL (c);
8781 if (need_complete)
8783 t = require_complete_type (t);
8784 if (t == error_mark_node)
8785 remove = true;
8788 if (need_implicitly_determined)
8790 const char *share_name = NULL;
8792 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8793 share_name = "threadprivate";
8794 else switch (c_omp_predetermined_sharing (t))
8796 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8797 break;
8798 case OMP_CLAUSE_DEFAULT_SHARED:
8799 share_name = "shared";
8800 break;
8801 case OMP_CLAUSE_DEFAULT_PRIVATE:
8802 share_name = "private";
8803 break;
8804 default:
8805 gcc_unreachable ();
8807 if (share_name)
8809 error ("%qE is predetermined %qs for %qs",
8810 t, share_name, name);
8811 remove = true;
8816 if (remove)
8817 *pc = OMP_CLAUSE_CHAIN (c);
8818 else
8819 pc = &OMP_CLAUSE_CHAIN (c);
8822 bitmap_obstack_release (NULL);
8823 return clauses;