PR 31567
[official-gcc.git] / gcc / c-typeck.c
blob4b0dbbb8be88f47b190351f87101490557769293
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, 2008, 2009
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 "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_assign,
53 ic_init,
54 ic_return
57 /* Whether we are building a boolean conversion inside
58 convert_for_assignment, or some other late binary operation. If
59 build_binary_op is called (from code shared with C++) in this case,
60 then the operands have already been folded and the result will not
61 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
62 bool in_late_binary_op;
64 /* The level of nesting inside "__alignof__". */
65 int in_alignof;
67 /* The level of nesting inside "sizeof". */
68 int in_sizeof;
70 /* The level of nesting inside "typeof". */
71 int in_typeof;
73 struct c_label_context_se *label_context_stack_se;
74 struct c_label_context_vm *label_context_stack_vm;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree);
86 static int comp_target_types (tree, tree);
87 static int function_types_compatible_p (const_tree, const_tree);
88 static int type_lists_compatible_p (const_tree, const_tree);
89 static tree lookup_field (tree, tree);
90 static int convert_arguments (int, tree *, tree, tree, tree, tree);
91 static tree pointer_diff (tree, tree);
92 static tree convert_for_assignment (tree, tree, enum impl_conv, bool,
93 tree, tree, int);
94 static tree valid_compound_expr_initializer (tree, tree);
95 static void push_string (const char *);
96 static void push_member_name (tree);
97 static int spelling_length (void);
98 static char *print_spelling (char *);
99 static void warning_init (int, const char *);
100 static tree digest_init (tree, tree, bool, bool, int);
101 static void output_init_element (tree, bool, tree, tree, int, bool);
102 static void output_pending_init_elements (int);
103 static int set_designator (int);
104 static void push_range_stack (tree);
105 static void add_pending_init (tree, tree, bool);
106 static void set_nonincremental_init (void);
107 static void set_nonincremental_init_from_string (tree);
108 static tree find_init_member (tree);
109 static void readonly_error (tree, enum lvalue_use);
110 static int lvalue_or_else (const_tree, enum lvalue_use);
111 static int lvalue_p (const_tree);
112 static void record_maybe_used_decl (tree);
113 static int comptypes_internal (const_tree, const_tree);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
117 static bool
118 null_pointer_constant_p (const_tree expr)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type = TREE_TYPE (expr);
123 return (TREE_CODE (expr) == INTEGER_CST
124 && !TREE_OVERFLOW (expr)
125 && integer_zerop (expr)
126 && (INTEGRAL_TYPE_P (type)
127 || (TREE_CODE (type) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type))
129 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
137 static tree
138 note_integer_operands (tree expr)
140 tree ret;
141 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
143 ret = copy_node (expr);
144 TREE_OVERFLOW (ret) = 1;
146 else
148 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
151 return ret;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
157 expression. */
159 static inline tree
160 remove_c_maybe_const_expr (tree expr)
162 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
163 return C_MAYBE_CONST_EXPR_EXPR (expr);
164 else
165 return expr;
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache {
171 const struct tagged_tu_seen_cache * next;
172 const_tree t1;
173 const_tree t2;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
176 int val;
179 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
185 tree
186 require_complete_type (tree value)
188 tree type = TREE_TYPE (value);
190 if (value == error_mark_node || type == error_mark_node)
191 return error_mark_node;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type))
195 return value;
197 c_incomplete_type_error (value, type);
198 return error_mark_node;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
205 void
206 c_incomplete_type_error (const_tree value, const_tree type)
208 const char *type_code_string;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type) == ERROR_MARK)
212 return;
214 if (value != 0 && (TREE_CODE (value) == VAR_DECL
215 || TREE_CODE (value) == PARM_DECL))
216 error ("%qD has an incomplete type", value);
217 else
219 retry:
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type))
224 case RECORD_TYPE:
225 type_code_string = "struct";
226 break;
228 case UNION_TYPE:
229 type_code_string = "union";
230 break;
232 case ENUMERAL_TYPE:
233 type_code_string = "enum";
234 break;
236 case VOID_TYPE:
237 error ("invalid use of void expression");
238 return;
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error ("invalid use of flexible array member");
246 return;
248 type = TREE_TYPE (type);
249 goto retry;
251 error ("invalid use of array with unspecified bounds");
252 return;
254 default:
255 gcc_unreachable ();
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string, TYPE_NAME (type));
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
270 tree
271 c_type_promotes_to (tree type)
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 return double_type_node;
276 if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 return unsigned_type_node;
282 return integer_type_node;
285 return type;
288 /* Return a variant of TYPE which has all the type qualifiers of LIKE
289 as well as those of TYPE. */
291 static tree
292 qualify_type (tree type, tree like)
294 return c_build_qualified_type (type,
295 TYPE_QUALS (type) | TYPE_QUALS (like));
298 /* Return true iff the given tree T is a variable length array. */
300 bool
301 c_vla_type_p (const_tree t)
303 if (TREE_CODE (t) == ARRAY_TYPE
304 && C_TYPE_VARIABLE_SIZE (t))
305 return true;
306 return false;
309 /* Return the composite type of two compatible types.
311 We assume that comptypes has already been done and returned
312 nonzero; if that isn't so, this may crash. In particular, we
313 assume that qualifiers match. */
315 tree
316 composite_type (tree t1, tree t2)
318 enum tree_code code1;
319 enum tree_code code2;
320 tree attributes;
322 /* Save time if the two types are the same. */
324 if (t1 == t2) return t1;
326 /* If one type is nonsense, use the other. */
327 if (t1 == error_mark_node)
328 return t2;
329 if (t2 == error_mark_node)
330 return t1;
332 code1 = TREE_CODE (t1);
333 code2 = TREE_CODE (t2);
335 /* Merge the attributes. */
336 attributes = targetm.merge_type_attributes (t1, t2);
338 /* If one is an enumerated type and the other is the compatible
339 integer type, the composite type might be either of the two
340 (DR#013 question 3). For consistency, use the enumerated type as
341 the composite type. */
343 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
344 return t1;
345 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
346 return t2;
348 gcc_assert (code1 == code2);
350 switch (code1)
352 case POINTER_TYPE:
353 /* For two pointers, do this recursively on the target type. */
355 tree pointed_to_1 = TREE_TYPE (t1);
356 tree pointed_to_2 = TREE_TYPE (t2);
357 tree target = composite_type (pointed_to_1, pointed_to_2);
358 t1 = build_pointer_type (target);
359 t1 = build_type_attribute_variant (t1, attributes);
360 return qualify_type (t1, t2);
363 case ARRAY_TYPE:
365 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
366 int quals;
367 tree unqual_elt;
368 tree d1 = TYPE_DOMAIN (t1);
369 tree d2 = TYPE_DOMAIN (t2);
370 bool d1_variable, d2_variable;
371 bool d1_zero, d2_zero;
372 bool t1_complete, t2_complete;
374 /* We should not have any type quals on arrays at all. */
375 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
377 t1_complete = COMPLETE_TYPE_P (t1);
378 t2_complete = COMPLETE_TYPE_P (t2);
380 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
381 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
383 d1_variable = (!d1_zero
384 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
385 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
386 d2_variable = (!d2_zero
387 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
388 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
389 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
390 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
392 /* Save space: see if the result is identical to one of the args. */
393 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
394 && (d2_variable || d2_zero || !d1_variable))
395 return build_type_attribute_variant (t1, attributes);
396 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
397 && (d1_variable || d1_zero || !d2_variable))
398 return build_type_attribute_variant (t2, attributes);
400 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
401 return build_type_attribute_variant (t1, attributes);
402 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
403 return build_type_attribute_variant (t2, attributes);
405 /* Merge the element types, and have a size if either arg has
406 one. We may have qualifiers on the element types. To set
407 up TYPE_MAIN_VARIANT correctly, we need to form the
408 composite of the unqualified types and add the qualifiers
409 back at the end. */
410 quals = TYPE_QUALS (strip_array_types (elt));
411 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
412 t1 = build_array_type (unqual_elt,
413 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
414 && (d2_variable
415 || d2_zero
416 || !d1_variable))
417 ? t1
418 : t2));
419 /* Ensure a composite type involving a zero-length array type
420 is a zero-length type not an incomplete type. */
421 if (d1_zero && d2_zero
422 && (t1_complete || t2_complete)
423 && !COMPLETE_TYPE_P (t1))
425 TYPE_SIZE (t1) = bitsize_zero_node;
426 TYPE_SIZE_UNIT (t1) = size_zero_node;
428 t1 = c_build_qualified_type (t1, quals);
429 return build_type_attribute_variant (t1, attributes);
432 case ENUMERAL_TYPE:
433 case RECORD_TYPE:
434 case UNION_TYPE:
435 if (attributes != NULL)
437 /* Try harder not to create a new aggregate type. */
438 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
439 return t1;
440 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
441 return t2;
443 return build_type_attribute_variant (t1, attributes);
445 case FUNCTION_TYPE:
446 /* Function types: prefer the one that specified arg types.
447 If both do, merge the arg types. Also merge the return types. */
449 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
450 tree p1 = TYPE_ARG_TYPES (t1);
451 tree p2 = TYPE_ARG_TYPES (t2);
452 int len;
453 tree newargs, n;
454 int i;
456 /* Save space: see if the result is identical to one of the args. */
457 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
458 return build_type_attribute_variant (t1, attributes);
459 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
460 return build_type_attribute_variant (t2, attributes);
462 /* Simple way if one arg fails to specify argument types. */
463 if (TYPE_ARG_TYPES (t1) == 0)
465 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
466 t1 = build_type_attribute_variant (t1, attributes);
467 return qualify_type (t1, t2);
469 if (TYPE_ARG_TYPES (t2) == 0)
471 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
472 t1 = build_type_attribute_variant (t1, attributes);
473 return qualify_type (t1, t2);
476 /* If both args specify argument types, we must merge the two
477 lists, argument by argument. */
478 /* Tell global_bindings_p to return false so that variable_size
479 doesn't die on VLAs in parameter types. */
480 c_override_global_bindings_to_false = true;
482 len = list_length (p1);
483 newargs = 0;
485 for (i = 0; i < len; i++)
486 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
488 n = newargs;
490 for (; p1;
491 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
493 /* A null type means arg type is not specified.
494 Take whatever the other function type has. */
495 if (TREE_VALUE (p1) == 0)
497 TREE_VALUE (n) = TREE_VALUE (p2);
498 goto parm_done;
500 if (TREE_VALUE (p2) == 0)
502 TREE_VALUE (n) = TREE_VALUE (p1);
503 goto parm_done;
506 /* Given wait (union {union wait *u; int *i} *)
507 and wait (union wait *),
508 prefer union wait * as type of parm. */
509 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
510 && TREE_VALUE (p1) != TREE_VALUE (p2))
512 tree memb;
513 tree mv2 = TREE_VALUE (p2);
514 if (mv2 && mv2 != error_mark_node
515 && TREE_CODE (mv2) != ARRAY_TYPE)
516 mv2 = TYPE_MAIN_VARIANT (mv2);
517 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
518 memb; memb = TREE_CHAIN (memb))
520 tree mv3 = TREE_TYPE (memb);
521 if (mv3 && mv3 != error_mark_node
522 && TREE_CODE (mv3) != ARRAY_TYPE)
523 mv3 = TYPE_MAIN_VARIANT (mv3);
524 if (comptypes (mv3, mv2))
526 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
527 TREE_VALUE (p2));
528 pedwarn (input_location, OPT_pedantic,
529 "function types not truly compatible in ISO C");
530 goto parm_done;
534 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
535 && TREE_VALUE (p2) != TREE_VALUE (p1))
537 tree memb;
538 tree mv1 = TREE_VALUE (p1);
539 if (mv1 && mv1 != error_mark_node
540 && TREE_CODE (mv1) != ARRAY_TYPE)
541 mv1 = TYPE_MAIN_VARIANT (mv1);
542 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
543 memb; memb = TREE_CHAIN (memb))
545 tree mv3 = TREE_TYPE (memb);
546 if (mv3 && mv3 != error_mark_node
547 && TREE_CODE (mv3) != ARRAY_TYPE)
548 mv3 = TYPE_MAIN_VARIANT (mv3);
549 if (comptypes (mv3, mv1))
551 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
552 TREE_VALUE (p1));
553 pedwarn (input_location, OPT_pedantic,
554 "function types not truly compatible in ISO C");
555 goto parm_done;
559 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
560 parm_done: ;
563 c_override_global_bindings_to_false = false;
564 t1 = build_function_type (valtype, newargs);
565 t1 = qualify_type (t1, t2);
566 /* ... falls through ... */
569 default:
570 return build_type_attribute_variant (t1, attributes);
575 /* Return the type of a conditional expression between pointers to
576 possibly differently qualified versions of compatible types.
578 We assume that comp_target_types has already been done and returned
579 nonzero; if that isn't so, this may crash. */
581 static tree
582 common_pointer_type (tree t1, tree t2)
584 tree attributes;
585 tree pointed_to_1, mv1;
586 tree pointed_to_2, mv2;
587 tree target;
588 unsigned target_quals;
590 /* Save time if the two types are the same. */
592 if (t1 == t2) return t1;
594 /* If one type is nonsense, use the other. */
595 if (t1 == error_mark_node)
596 return t2;
597 if (t2 == error_mark_node)
598 return t1;
600 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
601 && TREE_CODE (t2) == POINTER_TYPE);
603 /* Merge the attributes. */
604 attributes = targetm.merge_type_attributes (t1, t2);
606 /* Find the composite type of the target types, and combine the
607 qualifiers of the two types' targets. Do not lose qualifiers on
608 array element types by taking the TYPE_MAIN_VARIANT. */
609 mv1 = pointed_to_1 = TREE_TYPE (t1);
610 mv2 = pointed_to_2 = TREE_TYPE (t2);
611 if (TREE_CODE (mv1) != ARRAY_TYPE)
612 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
613 if (TREE_CODE (mv2) != ARRAY_TYPE)
614 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
615 target = composite_type (mv1, mv2);
617 /* For function types do not merge const qualifiers, but drop them
618 if used inconsistently. The middle-end uses these to mark const
619 and noreturn functions. */
620 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
621 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
622 else
623 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
624 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
625 return build_type_attribute_variant (t1, attributes);
628 /* Return the common type for two arithmetic types under the usual
629 arithmetic conversions. The default conversions have already been
630 applied, and enumerated types converted to their compatible integer
631 types. The resulting type is unqualified and has no attributes.
633 This is the type for the result of most arithmetic operations
634 if the operands have the given two types. */
636 static tree
637 c_common_type (tree t1, tree t2)
639 enum tree_code code1;
640 enum tree_code code2;
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
648 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
649 t1 = TYPE_MAIN_VARIANT (t1);
651 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
652 t2 = TYPE_MAIN_VARIANT (t2);
654 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
655 t1 = build_type_attribute_variant (t1, NULL_TREE);
657 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
658 t2 = build_type_attribute_variant (t2, NULL_TREE);
660 /* Save time if the two types are the same. */
662 if (t1 == t2) return t1;
664 code1 = TREE_CODE (t1);
665 code2 = TREE_CODE (t2);
667 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
668 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
669 || code1 == INTEGER_TYPE);
670 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
671 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
672 || code2 == INTEGER_TYPE);
674 /* When one operand is a decimal float type, the other operand cannot be
675 a generic float type or a complex type. We also disallow vector types
676 here. */
677 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
678 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
680 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
682 error ("can%'t mix operands of decimal float and vector types");
683 return error_mark_node;
685 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
687 error ("can%'t mix operands of decimal float and complex types");
688 return error_mark_node;
690 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
692 error ("can%'t mix operands of decimal float and other float types");
693 return error_mark_node;
697 /* If one type is a vector type, return that type. (How the usual
698 arithmetic conversions apply to the vector types extension is not
699 precisely specified.) */
700 if (code1 == VECTOR_TYPE)
701 return t1;
703 if (code2 == VECTOR_TYPE)
704 return t2;
706 /* If one type is complex, form the common type of the non-complex
707 components, then make that complex. Use T1 or T2 if it is the
708 required type. */
709 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
711 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
712 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
713 tree subtype = c_common_type (subtype1, subtype2);
715 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
716 return t1;
717 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
718 return t2;
719 else
720 return build_complex_type (subtype);
723 /* If only one is real, use it as the result. */
725 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
726 return t1;
728 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
729 return t2;
731 /* If both are real and either are decimal floating point types, use
732 the decimal floating point type with the greater precision. */
734 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
736 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
737 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
738 return dfloat128_type_node;
739 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
740 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
741 return dfloat64_type_node;
742 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
743 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
744 return dfloat32_type_node;
747 /* Deal with fixed-point types. */
748 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
750 unsigned int unsignedp = 0, satp = 0;
751 enum machine_mode m1, m2;
752 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
754 m1 = TYPE_MODE (t1);
755 m2 = TYPE_MODE (t2);
757 /* If one input type is saturating, the result type is saturating. */
758 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
759 satp = 1;
761 /* If both fixed-point types are unsigned, the result type is unsigned.
762 When mixing fixed-point and integer types, follow the sign of the
763 fixed-point type.
764 Otherwise, the result type is signed. */
765 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
766 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
767 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
768 && TYPE_UNSIGNED (t1))
769 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
770 && TYPE_UNSIGNED (t2)))
771 unsignedp = 1;
773 /* The result type is signed. */
774 if (unsignedp == 0)
776 /* If the input type is unsigned, we need to convert to the
777 signed type. */
778 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
780 enum mode_class mclass = (enum mode_class) 0;
781 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
782 mclass = MODE_FRACT;
783 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
784 mclass = MODE_ACCUM;
785 else
786 gcc_unreachable ();
787 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
789 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
791 enum mode_class mclass = (enum mode_class) 0;
792 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
793 mclass = MODE_FRACT;
794 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
795 mclass = MODE_ACCUM;
796 else
797 gcc_unreachable ();
798 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
802 if (code1 == FIXED_POINT_TYPE)
804 fbit1 = GET_MODE_FBIT (m1);
805 ibit1 = GET_MODE_IBIT (m1);
807 else
809 fbit1 = 0;
810 /* Signed integers need to subtract one sign bit. */
811 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
814 if (code2 == FIXED_POINT_TYPE)
816 fbit2 = GET_MODE_FBIT (m2);
817 ibit2 = GET_MODE_IBIT (m2);
819 else
821 fbit2 = 0;
822 /* Signed integers need to subtract one sign bit. */
823 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
826 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
827 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
828 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
829 satp);
832 /* Both real or both integers; use the one with greater precision. */
834 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
835 return t1;
836 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
837 return t2;
839 /* Same precision. Prefer long longs to longs to ints when the
840 same precision, following the C99 rules on integer type rank
841 (which are equivalent to the C90 rules for C90 types). */
843 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
844 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
845 return long_long_unsigned_type_node;
847 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
848 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
850 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
851 return long_long_unsigned_type_node;
852 else
853 return long_long_integer_type_node;
856 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
857 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
858 return long_unsigned_type_node;
860 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
861 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
863 /* But preserve unsignedness from the other type,
864 since long cannot hold all the values of an unsigned int. */
865 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
866 return long_unsigned_type_node;
867 else
868 return long_integer_type_node;
871 /* Likewise, prefer long double to double even if same size. */
872 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
873 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
874 return long_double_type_node;
876 /* Otherwise prefer the unsigned one. */
878 if (TYPE_UNSIGNED (t1))
879 return t1;
880 else
881 return t2;
884 /* Wrapper around c_common_type that is used by c-common.c and other
885 front end optimizations that remove promotions. ENUMERAL_TYPEs
886 are allowed here and are converted to their compatible integer types.
887 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
888 preferably a non-Boolean type as the common type. */
889 tree
890 common_type (tree t1, tree t2)
892 if (TREE_CODE (t1) == ENUMERAL_TYPE)
893 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
894 if (TREE_CODE (t2) == ENUMERAL_TYPE)
895 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
897 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
898 if (TREE_CODE (t1) == BOOLEAN_TYPE
899 && TREE_CODE (t2) == BOOLEAN_TYPE)
900 return boolean_type_node;
902 /* If either type is BOOLEAN_TYPE, then return the other. */
903 if (TREE_CODE (t1) == BOOLEAN_TYPE)
904 return t2;
905 if (TREE_CODE (t2) == BOOLEAN_TYPE)
906 return t1;
908 return c_common_type (t1, t2);
911 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
912 or various other operations. Return 2 if they are compatible
913 but a warning may be needed if you use them together. */
916 comptypes (tree type1, tree type2)
918 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
919 int val;
921 val = comptypes_internal (type1, type2);
922 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
924 return val;
927 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
928 or various other operations. Return 2 if they are compatible
929 but a warning may be needed if you use them together. This
930 differs from comptypes, in that we don't free the seen types. */
932 static int
933 comptypes_internal (const_tree type1, const_tree type2)
935 const_tree t1 = type1;
936 const_tree t2 = type2;
937 int attrval, val;
939 /* Suppress errors caused by previously reported errors. */
941 if (t1 == t2 || !t1 || !t2
942 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
943 return 1;
945 /* If either type is the internal version of sizetype, return the
946 language version. */
947 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
948 && TYPE_ORIG_SIZE_TYPE (t1))
949 t1 = TYPE_ORIG_SIZE_TYPE (t1);
951 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
952 && TYPE_ORIG_SIZE_TYPE (t2))
953 t2 = TYPE_ORIG_SIZE_TYPE (t2);
956 /* Enumerated types are compatible with integer types, but this is
957 not transitive: two enumerated types in the same translation unit
958 are compatible with each other only if they are the same type. */
960 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
961 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
962 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
963 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
965 if (t1 == t2)
966 return 1;
968 /* Different classes of types can't be compatible. */
970 if (TREE_CODE (t1) != TREE_CODE (t2))
971 return 0;
973 /* Qualifiers must match. C99 6.7.3p9 */
975 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
976 return 0;
978 /* Allow for two different type nodes which have essentially the same
979 definition. Note that we already checked for equality of the type
980 qualifiers (just above). */
982 if (TREE_CODE (t1) != ARRAY_TYPE
983 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
984 return 1;
986 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
987 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
988 return 0;
990 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
991 val = 0;
993 switch (TREE_CODE (t1))
995 case POINTER_TYPE:
996 /* Do not remove mode or aliasing information. */
997 if (TYPE_MODE (t1) != TYPE_MODE (t2)
998 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
999 break;
1000 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1001 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
1002 break;
1004 case FUNCTION_TYPE:
1005 val = function_types_compatible_p (t1, t2);
1006 break;
1008 case ARRAY_TYPE:
1010 tree d1 = TYPE_DOMAIN (t1);
1011 tree d2 = TYPE_DOMAIN (t2);
1012 bool d1_variable, d2_variable;
1013 bool d1_zero, d2_zero;
1014 val = 1;
1016 /* Target types must match incl. qualifiers. */
1017 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1018 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
1019 return 0;
1021 /* Sizes must match unless one is missing or variable. */
1022 if (d1 == 0 || d2 == 0 || d1 == d2)
1023 break;
1025 d1_zero = !TYPE_MAX_VALUE (d1);
1026 d2_zero = !TYPE_MAX_VALUE (d2);
1028 d1_variable = (!d1_zero
1029 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1030 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1031 d2_variable = (!d2_zero
1032 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1033 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1034 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1035 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1037 if (d1_variable || d2_variable)
1038 break;
1039 if (d1_zero && d2_zero)
1040 break;
1041 if (d1_zero || d2_zero
1042 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1043 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1044 val = 0;
1046 break;
1049 case ENUMERAL_TYPE:
1050 case RECORD_TYPE:
1051 case UNION_TYPE:
1052 if (val != 1 && !same_translation_unit_p (t1, t2))
1054 tree a1 = TYPE_ATTRIBUTES (t1);
1055 tree a2 = TYPE_ATTRIBUTES (t2);
1057 if (! attribute_list_contained (a1, a2)
1058 && ! attribute_list_contained (a2, a1))
1059 break;
1061 if (attrval != 2)
1062 return tagged_types_tu_compatible_p (t1, t2);
1063 val = tagged_types_tu_compatible_p (t1, t2);
1065 break;
1067 case VECTOR_TYPE:
1068 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1069 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1070 break;
1072 default:
1073 break;
1075 return attrval == 2 && val == 1 ? 2 : val;
1078 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1079 ignoring their qualifiers. */
1081 static int
1082 comp_target_types (tree ttl, tree ttr)
1084 int val;
1085 tree mvl, mvr;
1087 /* Do not lose qualifiers on element types of array types that are
1088 pointer targets by taking their TYPE_MAIN_VARIANT. */
1089 mvl = TREE_TYPE (ttl);
1090 mvr = TREE_TYPE (ttr);
1091 if (TREE_CODE (mvl) != ARRAY_TYPE)
1092 mvl = TYPE_MAIN_VARIANT (mvl);
1093 if (TREE_CODE (mvr) != ARRAY_TYPE)
1094 mvr = TYPE_MAIN_VARIANT (mvr);
1095 val = comptypes (mvl, mvr);
1097 if (val == 2)
1098 pedwarn (input_location, OPT_pedantic, "types are not quite compatible");
1099 return val;
1102 /* Subroutines of `comptypes'. */
1104 /* Determine whether two trees derive from the same translation unit.
1105 If the CONTEXT chain ends in a null, that tree's context is still
1106 being parsed, so if two trees have context chains ending in null,
1107 they're in the same translation unit. */
1109 same_translation_unit_p (const_tree t1, const_tree t2)
1111 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1112 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1114 case tcc_declaration:
1115 t1 = DECL_CONTEXT (t1); break;
1116 case tcc_type:
1117 t1 = TYPE_CONTEXT (t1); break;
1118 case tcc_exceptional:
1119 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1120 default: gcc_unreachable ();
1123 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1124 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1126 case tcc_declaration:
1127 t2 = DECL_CONTEXT (t2); break;
1128 case tcc_type:
1129 t2 = TYPE_CONTEXT (t2); break;
1130 case tcc_exceptional:
1131 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1132 default: gcc_unreachable ();
1135 return t1 == t2;
1138 /* Allocate the seen two types, assuming that they are compatible. */
1140 static struct tagged_tu_seen_cache *
1141 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1143 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1144 tu->next = tagged_tu_seen_base;
1145 tu->t1 = t1;
1146 tu->t2 = t2;
1148 tagged_tu_seen_base = tu;
1150 /* The C standard says that two structures in different translation
1151 units are compatible with each other only if the types of their
1152 fields are compatible (among other things). We assume that they
1153 are compatible until proven otherwise when building the cache.
1154 An example where this can occur is:
1155 struct a
1157 struct a *next;
1159 If we are comparing this against a similar struct in another TU,
1160 and did not assume they were compatible, we end up with an infinite
1161 loop. */
1162 tu->val = 1;
1163 return tu;
1166 /* Free the seen types until we get to TU_TIL. */
1168 static void
1169 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1171 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1172 while (tu != tu_til)
1174 const struct tagged_tu_seen_cache *const tu1
1175 = (const struct tagged_tu_seen_cache *) tu;
1176 tu = tu1->next;
1177 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1179 tagged_tu_seen_base = tu_til;
1182 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1183 compatible. If the two types are not the same (which has been
1184 checked earlier), this can only happen when multiple translation
1185 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1186 rules. */
1188 static int
1189 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1191 tree s1, s2;
1192 bool needs_warning = false;
1194 /* We have to verify that the tags of the types are the same. This
1195 is harder than it looks because this may be a typedef, so we have
1196 to go look at the original type. It may even be a typedef of a
1197 typedef...
1198 In the case of compiler-created builtin structs the TYPE_DECL
1199 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1200 while (TYPE_NAME (t1)
1201 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1202 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1203 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1205 while (TYPE_NAME (t2)
1206 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1207 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1208 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1210 /* C90 didn't have the requirement that the two tags be the same. */
1211 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1212 return 0;
1214 /* C90 didn't say what happened if one or both of the types were
1215 incomplete; we choose to follow C99 rules here, which is that they
1216 are compatible. */
1217 if (TYPE_SIZE (t1) == NULL
1218 || TYPE_SIZE (t2) == NULL)
1219 return 1;
1222 const struct tagged_tu_seen_cache * tts_i;
1223 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1224 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1225 return tts_i->val;
1228 switch (TREE_CODE (t1))
1230 case ENUMERAL_TYPE:
1232 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1233 /* Speed up the case where the type values are in the same order. */
1234 tree tv1 = TYPE_VALUES (t1);
1235 tree tv2 = TYPE_VALUES (t2);
1237 if (tv1 == tv2)
1239 return 1;
1242 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1244 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1245 break;
1246 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1248 tu->val = 0;
1249 return 0;
1253 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1255 return 1;
1257 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1259 tu->val = 0;
1260 return 0;
1263 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1265 tu->val = 0;
1266 return 0;
1269 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1271 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1272 if (s2 == NULL
1273 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1275 tu->val = 0;
1276 return 0;
1279 return 1;
1282 case UNION_TYPE:
1284 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1285 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1287 tu->val = 0;
1288 return 0;
1291 /* Speed up the common case where the fields are in the same order. */
1292 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1293 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1295 int result;
1297 if (DECL_NAME (s1) != DECL_NAME (s2))
1298 break;
1299 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1301 if (result != 1 && !DECL_NAME (s1))
1302 break;
1303 if (result == 0)
1305 tu->val = 0;
1306 return 0;
1308 if (result == 2)
1309 needs_warning = true;
1311 if (TREE_CODE (s1) == FIELD_DECL
1312 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1313 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1315 tu->val = 0;
1316 return 0;
1319 if (!s1 && !s2)
1321 tu->val = needs_warning ? 2 : 1;
1322 return tu->val;
1325 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1327 bool ok = false;
1329 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1330 if (DECL_NAME (s1) == DECL_NAME (s2))
1332 int result;
1334 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1336 if (result != 1 && !DECL_NAME (s1))
1337 continue;
1338 if (result == 0)
1340 tu->val = 0;
1341 return 0;
1343 if (result == 2)
1344 needs_warning = true;
1346 if (TREE_CODE (s1) == FIELD_DECL
1347 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1348 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1349 break;
1351 ok = true;
1352 break;
1354 if (!ok)
1356 tu->val = 0;
1357 return 0;
1360 tu->val = needs_warning ? 2 : 10;
1361 return tu->val;
1364 case RECORD_TYPE:
1366 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1368 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1369 s1 && s2;
1370 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1372 int result;
1373 if (TREE_CODE (s1) != TREE_CODE (s2)
1374 || DECL_NAME (s1) != DECL_NAME (s2))
1375 break;
1376 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1377 if (result == 0)
1378 break;
1379 if (result == 2)
1380 needs_warning = true;
1382 if (TREE_CODE (s1) == FIELD_DECL
1383 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1384 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1385 break;
1387 if (s1 && s2)
1388 tu->val = 0;
1389 else
1390 tu->val = needs_warning ? 2 : 1;
1391 return tu->val;
1394 default:
1395 gcc_unreachable ();
1399 /* Return 1 if two function types F1 and F2 are compatible.
1400 If either type specifies no argument types,
1401 the other must specify a fixed number of self-promoting arg types.
1402 Otherwise, if one type specifies only the number of arguments,
1403 the other must specify that number of self-promoting arg types.
1404 Otherwise, the argument types must match. */
1406 static int
1407 function_types_compatible_p (const_tree f1, const_tree f2)
1409 tree args1, args2;
1410 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1411 int val = 1;
1412 int val1;
1413 tree ret1, ret2;
1415 ret1 = TREE_TYPE (f1);
1416 ret2 = TREE_TYPE (f2);
1418 /* 'volatile' qualifiers on a function's return type used to mean
1419 the function is noreturn. */
1420 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1421 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1422 if (TYPE_VOLATILE (ret1))
1423 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1424 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1425 if (TYPE_VOLATILE (ret2))
1426 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1427 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1428 val = comptypes_internal (ret1, ret2);
1429 if (val == 0)
1430 return 0;
1432 args1 = TYPE_ARG_TYPES (f1);
1433 args2 = TYPE_ARG_TYPES (f2);
1435 /* An unspecified parmlist matches any specified parmlist
1436 whose argument types don't need default promotions. */
1438 if (args1 == 0)
1440 if (!self_promoting_args_p (args2))
1441 return 0;
1442 /* If one of these types comes from a non-prototype fn definition,
1443 compare that with the other type's arglist.
1444 If they don't match, ask for a warning (but no error). */
1445 if (TYPE_ACTUAL_ARG_TYPES (f1)
1446 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1447 val = 2;
1448 return val;
1450 if (args2 == 0)
1452 if (!self_promoting_args_p (args1))
1453 return 0;
1454 if (TYPE_ACTUAL_ARG_TYPES (f2)
1455 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1456 val = 2;
1457 return val;
1460 /* Both types have argument lists: compare them and propagate results. */
1461 val1 = type_lists_compatible_p (args1, args2);
1462 return val1 != 1 ? val1 : val;
1465 /* Check two lists of types for compatibility,
1466 returning 0 for incompatible, 1 for compatible,
1467 or 2 for compatible with warning. */
1469 static int
1470 type_lists_compatible_p (const_tree args1, const_tree args2)
1472 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1473 int val = 1;
1474 int newval = 0;
1476 while (1)
1478 tree a1, mv1, a2, mv2;
1479 if (args1 == 0 && args2 == 0)
1480 return val;
1481 /* If one list is shorter than the other,
1482 they fail to match. */
1483 if (args1 == 0 || args2 == 0)
1484 return 0;
1485 mv1 = a1 = TREE_VALUE (args1);
1486 mv2 = a2 = TREE_VALUE (args2);
1487 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1488 mv1 = TYPE_MAIN_VARIANT (mv1);
1489 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1490 mv2 = TYPE_MAIN_VARIANT (mv2);
1491 /* A null pointer instead of a type
1492 means there is supposed to be an argument
1493 but nothing is specified about what type it has.
1494 So match anything that self-promotes. */
1495 if (a1 == 0)
1497 if (c_type_promotes_to (a2) != a2)
1498 return 0;
1500 else if (a2 == 0)
1502 if (c_type_promotes_to (a1) != a1)
1503 return 0;
1505 /* If one of the lists has an error marker, ignore this arg. */
1506 else if (TREE_CODE (a1) == ERROR_MARK
1507 || TREE_CODE (a2) == ERROR_MARK)
1509 else if (!(newval = comptypes_internal (mv1, mv2)))
1511 /* Allow wait (union {union wait *u; int *i} *)
1512 and wait (union wait *) to be compatible. */
1513 if (TREE_CODE (a1) == UNION_TYPE
1514 && (TYPE_NAME (a1) == 0
1515 || TYPE_TRANSPARENT_UNION (a1))
1516 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1517 && tree_int_cst_equal (TYPE_SIZE (a1),
1518 TYPE_SIZE (a2)))
1520 tree memb;
1521 for (memb = TYPE_FIELDS (a1);
1522 memb; memb = TREE_CHAIN (memb))
1524 tree mv3 = TREE_TYPE (memb);
1525 if (mv3 && mv3 != error_mark_node
1526 && TREE_CODE (mv3) != ARRAY_TYPE)
1527 mv3 = TYPE_MAIN_VARIANT (mv3);
1528 if (comptypes_internal (mv3, mv2))
1529 break;
1531 if (memb == 0)
1532 return 0;
1534 else if (TREE_CODE (a2) == UNION_TYPE
1535 && (TYPE_NAME (a2) == 0
1536 || TYPE_TRANSPARENT_UNION (a2))
1537 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1538 && tree_int_cst_equal (TYPE_SIZE (a2),
1539 TYPE_SIZE (a1)))
1541 tree memb;
1542 for (memb = TYPE_FIELDS (a2);
1543 memb; memb = TREE_CHAIN (memb))
1545 tree mv3 = TREE_TYPE (memb);
1546 if (mv3 && mv3 != error_mark_node
1547 && TREE_CODE (mv3) != ARRAY_TYPE)
1548 mv3 = TYPE_MAIN_VARIANT (mv3);
1549 if (comptypes_internal (mv3, mv1))
1550 break;
1552 if (memb == 0)
1553 return 0;
1555 else
1556 return 0;
1559 /* comptypes said ok, but record if it said to warn. */
1560 if (newval > val)
1561 val = newval;
1563 args1 = TREE_CHAIN (args1);
1564 args2 = TREE_CHAIN (args2);
1568 /* Compute the size to increment a pointer by. */
1570 static tree
1571 c_size_in_bytes (const_tree type)
1573 enum tree_code code = TREE_CODE (type);
1575 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1576 return size_one_node;
1578 if (!COMPLETE_OR_VOID_TYPE_P (type))
1580 error ("arithmetic on pointer to an incomplete type");
1581 return size_one_node;
1584 /* Convert in case a char is more than one unit. */
1585 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1586 size_int (TYPE_PRECISION (char_type_node)
1587 / BITS_PER_UNIT));
1590 /* Return either DECL or its known constant value (if it has one). */
1592 tree
1593 decl_constant_value (tree decl)
1595 if (/* Don't change a variable array bound or initial value to a constant
1596 in a place where a variable is invalid. Note that DECL_INITIAL
1597 isn't valid for a PARM_DECL. */
1598 current_function_decl != 0
1599 && TREE_CODE (decl) != PARM_DECL
1600 && !TREE_THIS_VOLATILE (decl)
1601 && TREE_READONLY (decl)
1602 && DECL_INITIAL (decl) != 0
1603 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1604 /* This is invalid if initial value is not constant.
1605 If it has either a function call, a memory reference,
1606 or a variable, then re-evaluating it could give different results. */
1607 && TREE_CONSTANT (DECL_INITIAL (decl))
1608 /* Check for cases where this is sub-optimal, even though valid. */
1609 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1610 return DECL_INITIAL (decl);
1611 return decl;
1614 /* Convert the array expression EXP to a pointer. */
1615 static tree
1616 array_to_pointer_conversion (tree exp)
1618 tree orig_exp = exp;
1619 tree type = TREE_TYPE (exp);
1620 tree adr;
1621 tree restype = TREE_TYPE (type);
1622 tree ptrtype;
1624 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1626 STRIP_TYPE_NOPS (exp);
1628 if (TREE_NO_WARNING (orig_exp))
1629 TREE_NO_WARNING (exp) = 1;
1631 ptrtype = build_pointer_type (restype);
1633 if (TREE_CODE (exp) == INDIRECT_REF)
1634 return convert (ptrtype, TREE_OPERAND (exp, 0));
1636 if (TREE_CODE (exp) == VAR_DECL)
1638 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1639 ADDR_EXPR because it's the best way of representing what
1640 happens in C when we take the address of an array and place
1641 it in a pointer to the element type. */
1642 adr = build1 (ADDR_EXPR, ptrtype, exp);
1643 if (!c_mark_addressable (exp))
1644 return error_mark_node;
1645 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1646 return adr;
1649 /* This way is better for a COMPONENT_REF since it can
1650 simplify the offset for a component. */
1651 adr = build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 1);
1652 return convert (ptrtype, adr);
1655 /* Convert the function expression EXP to a pointer. */
1656 static tree
1657 function_to_pointer_conversion (tree exp)
1659 tree orig_exp = exp;
1661 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1663 STRIP_TYPE_NOPS (exp);
1665 if (TREE_NO_WARNING (orig_exp))
1666 TREE_NO_WARNING (exp) = 1;
1668 return build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 0);
1671 /* Perform the default conversion of arrays and functions to pointers.
1672 Return the result of converting EXP. For any other expression, just
1673 return EXP. */
1675 struct c_expr
1676 default_function_array_conversion (struct c_expr exp)
1678 tree orig_exp = exp.value;
1679 tree type = TREE_TYPE (exp.value);
1680 enum tree_code code = TREE_CODE (type);
1682 switch (code)
1684 case ARRAY_TYPE:
1686 bool not_lvalue = false;
1687 bool lvalue_array_p;
1689 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1690 || CONVERT_EXPR_P (exp.value))
1691 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1693 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1694 not_lvalue = true;
1695 exp.value = TREE_OPERAND (exp.value, 0);
1698 if (TREE_NO_WARNING (orig_exp))
1699 TREE_NO_WARNING (exp.value) = 1;
1701 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1702 if (!flag_isoc99 && !lvalue_array_p)
1704 /* Before C99, non-lvalue arrays do not decay to pointers.
1705 Normally, using such an array would be invalid; but it can
1706 be used correctly inside sizeof or as a statement expression.
1707 Thus, do not give an error here; an error will result later. */
1708 return exp;
1711 exp.value = array_to_pointer_conversion (exp.value);
1713 break;
1714 case FUNCTION_TYPE:
1715 exp.value = function_to_pointer_conversion (exp.value);
1716 break;
1717 default:
1718 break;
1721 return exp;
1725 /* EXP is an expression of integer type. Apply the integer promotions
1726 to it and return the promoted value. */
1728 tree
1729 perform_integral_promotions (tree exp)
1731 tree type = TREE_TYPE (exp);
1732 enum tree_code code = TREE_CODE (type);
1734 gcc_assert (INTEGRAL_TYPE_P (type));
1736 /* Normally convert enums to int,
1737 but convert wide enums to something wider. */
1738 if (code == ENUMERAL_TYPE)
1740 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1741 TYPE_PRECISION (integer_type_node)),
1742 ((TYPE_PRECISION (type)
1743 >= TYPE_PRECISION (integer_type_node))
1744 && TYPE_UNSIGNED (type)));
1746 return convert (type, exp);
1749 /* ??? This should no longer be needed now bit-fields have their
1750 proper types. */
1751 if (TREE_CODE (exp) == COMPONENT_REF
1752 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1753 /* If it's thinner than an int, promote it like a
1754 c_promoting_integer_type_p, otherwise leave it alone. */
1755 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1756 TYPE_PRECISION (integer_type_node)))
1757 return convert (integer_type_node, exp);
1759 if (c_promoting_integer_type_p (type))
1761 /* Preserve unsignedness if not really getting any wider. */
1762 if (TYPE_UNSIGNED (type)
1763 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1764 return convert (unsigned_type_node, exp);
1766 return convert (integer_type_node, exp);
1769 return exp;
1773 /* Perform default promotions for C data used in expressions.
1774 Enumeral types or short or char are converted to int.
1775 In addition, manifest constants symbols are replaced by their values. */
1777 tree
1778 default_conversion (tree exp)
1780 tree orig_exp;
1781 tree type = TREE_TYPE (exp);
1782 enum tree_code code = TREE_CODE (type);
1784 /* Functions and arrays have been converted during parsing. */
1785 gcc_assert (code != FUNCTION_TYPE);
1786 if (code == ARRAY_TYPE)
1787 return exp;
1789 /* Constants can be used directly unless they're not loadable. */
1790 if (TREE_CODE (exp) == CONST_DECL)
1791 exp = DECL_INITIAL (exp);
1793 /* Strip no-op conversions. */
1794 orig_exp = exp;
1795 STRIP_TYPE_NOPS (exp);
1797 if (TREE_NO_WARNING (orig_exp))
1798 TREE_NO_WARNING (exp) = 1;
1800 if (code == VOID_TYPE)
1802 error ("void value not ignored as it ought to be");
1803 return error_mark_node;
1806 exp = require_complete_type (exp);
1807 if (exp == error_mark_node)
1808 return error_mark_node;
1810 if (INTEGRAL_TYPE_P (type))
1811 return perform_integral_promotions (exp);
1813 return exp;
1816 /* Look up COMPONENT in a structure or union DECL.
1818 If the component name is not found, returns NULL_TREE. Otherwise,
1819 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1820 stepping down the chain to the component, which is in the last
1821 TREE_VALUE of the list. Normally the list is of length one, but if
1822 the component is embedded within (nested) anonymous structures or
1823 unions, the list steps down the chain to the component. */
1825 static tree
1826 lookup_field (tree decl, tree component)
1828 tree type = TREE_TYPE (decl);
1829 tree field;
1831 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1832 to the field elements. Use a binary search on this array to quickly
1833 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1834 will always be set for structures which have many elements. */
1836 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1838 int bot, top, half;
1839 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1841 field = TYPE_FIELDS (type);
1842 bot = 0;
1843 top = TYPE_LANG_SPECIFIC (type)->s->len;
1844 while (top - bot > 1)
1846 half = (top - bot + 1) >> 1;
1847 field = field_array[bot+half];
1849 if (DECL_NAME (field) == NULL_TREE)
1851 /* Step through all anon unions in linear fashion. */
1852 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1854 field = field_array[bot++];
1855 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1856 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1858 tree anon = lookup_field (field, component);
1860 if (anon)
1861 return tree_cons (NULL_TREE, field, anon);
1865 /* Entire record is only anon unions. */
1866 if (bot > top)
1867 return NULL_TREE;
1869 /* Restart the binary search, with new lower bound. */
1870 continue;
1873 if (DECL_NAME (field) == component)
1874 break;
1875 if (DECL_NAME (field) < component)
1876 bot += half;
1877 else
1878 top = bot + half;
1881 if (DECL_NAME (field_array[bot]) == component)
1882 field = field_array[bot];
1883 else if (DECL_NAME (field) != component)
1884 return NULL_TREE;
1886 else
1888 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1890 if (DECL_NAME (field) == NULL_TREE
1891 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1892 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1894 tree anon = lookup_field (field, component);
1896 if (anon)
1897 return tree_cons (NULL_TREE, field, anon);
1900 if (DECL_NAME (field) == component)
1901 break;
1904 if (field == NULL_TREE)
1905 return NULL_TREE;
1908 return tree_cons (NULL_TREE, field, NULL_TREE);
1911 /* Make an expression to refer to the COMPONENT field of
1912 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1914 tree
1915 build_component_ref (tree datum, tree component)
1917 tree type = TREE_TYPE (datum);
1918 enum tree_code code = TREE_CODE (type);
1919 tree field = NULL;
1920 tree ref;
1921 bool datum_lvalue = lvalue_p (datum);
1923 if (!objc_is_public (datum, component))
1924 return error_mark_node;
1926 /* See if there is a field or component with name COMPONENT. */
1928 if (code == RECORD_TYPE || code == UNION_TYPE)
1930 if (!COMPLETE_TYPE_P (type))
1932 c_incomplete_type_error (NULL_TREE, type);
1933 return error_mark_node;
1936 field = lookup_field (datum, component);
1938 if (!field)
1940 error ("%qT has no member named %qE", type, component);
1941 return error_mark_node;
1944 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1945 This might be better solved in future the way the C++ front
1946 end does it - by giving the anonymous entities each a
1947 separate name and type, and then have build_component_ref
1948 recursively call itself. We can't do that here. */
1951 tree subdatum = TREE_VALUE (field);
1952 int quals;
1953 tree subtype;
1954 bool use_datum_quals;
1956 if (TREE_TYPE (subdatum) == error_mark_node)
1957 return error_mark_node;
1959 /* If this is an rvalue, it does not have qualifiers in C
1960 standard terms and we must avoid propagating such
1961 qualifiers down to a non-lvalue array that is then
1962 converted to a pointer. */
1963 use_datum_quals = (datum_lvalue
1964 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
1966 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1967 if (use_datum_quals)
1968 quals |= TYPE_QUALS (TREE_TYPE (datum));
1969 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1971 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1972 NULL_TREE);
1973 if (TREE_READONLY (subdatum)
1974 || (use_datum_quals && TREE_READONLY (datum)))
1975 TREE_READONLY (ref) = 1;
1976 if (TREE_THIS_VOLATILE (subdatum)
1977 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
1978 TREE_THIS_VOLATILE (ref) = 1;
1980 if (TREE_DEPRECATED (subdatum))
1981 warn_deprecated_use (subdatum);
1983 datum = ref;
1985 field = TREE_CHAIN (field);
1987 while (field);
1989 return ref;
1991 else if (code != ERROR_MARK)
1992 error ("request for member %qE in something not a structure or union",
1993 component);
1995 return error_mark_node;
1998 /* Given an expression PTR for a pointer, return an expression
1999 for the value pointed to.
2000 ERRORSTRING is the name of the operator to appear in error messages.
2002 LOC is the location to use for the generated tree. */
2004 tree
2005 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
2007 tree pointer = default_conversion (ptr);
2008 tree type = TREE_TYPE (pointer);
2009 tree ref;
2011 if (TREE_CODE (type) == POINTER_TYPE)
2013 if (CONVERT_EXPR_P (pointer)
2014 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2016 /* If a warning is issued, mark it to avoid duplicates from
2017 the backend. This only needs to be done at
2018 warn_strict_aliasing > 2. */
2019 if (warn_strict_aliasing > 2)
2020 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2021 type, TREE_OPERAND (pointer, 0)))
2022 TREE_NO_WARNING (pointer) = 1;
2025 if (TREE_CODE (pointer) == ADDR_EXPR
2026 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2027 == TREE_TYPE (type)))
2029 ref = TREE_OPERAND (pointer, 0);
2030 protected_set_expr_location (ref, loc);
2031 return ref;
2033 else
2035 tree t = TREE_TYPE (type);
2037 ref = build1 (INDIRECT_REF, t, pointer);
2039 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2041 error_at (loc, "dereferencing pointer to incomplete type");
2042 return error_mark_node;
2044 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2045 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2047 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2048 so that we get the proper error message if the result is used
2049 to assign to. Also, &* is supposed to be a no-op.
2050 And ANSI C seems to specify that the type of the result
2051 should be the const type. */
2052 /* A de-reference of a pointer to const is not a const. It is valid
2053 to change it via some other pointer. */
2054 TREE_READONLY (ref) = TYPE_READONLY (t);
2055 TREE_SIDE_EFFECTS (ref)
2056 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2057 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2058 protected_set_expr_location (ref, loc);
2059 return ref;
2062 else if (TREE_CODE (pointer) != ERROR_MARK)
2063 error_at (loc,
2064 "invalid type argument of %qs (have %qT)", errorstring, type);
2065 return error_mark_node;
2068 /* This handles expressions of the form "a[i]", which denotes
2069 an array reference.
2071 This is logically equivalent in C to *(a+i), but we may do it differently.
2072 If A is a variable or a member, we generate a primitive ARRAY_REF.
2073 This avoids forcing the array out of registers, and can work on
2074 arrays that are not lvalues (for example, members of structures returned
2075 by functions).
2077 LOC is the location to use for the returned expression. */
2079 tree
2080 build_array_ref (tree array, tree index, location_t loc)
2082 tree ret;
2083 bool swapped = false;
2084 if (TREE_TYPE (array) == error_mark_node
2085 || TREE_TYPE (index) == error_mark_node)
2086 return error_mark_node;
2088 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2089 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2091 tree temp;
2092 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2093 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2095 error_at (loc, "subscripted value is neither array nor pointer");
2096 return error_mark_node;
2098 temp = array;
2099 array = index;
2100 index = temp;
2101 swapped = true;
2104 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2106 error_at (loc, "array subscript is not an integer");
2107 return error_mark_node;
2110 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2112 error_at (loc, "subscripted value is pointer to function");
2113 return error_mark_node;
2116 /* ??? Existing practice has been to warn only when the char
2117 index is syntactically the index, not for char[array]. */
2118 if (!swapped)
2119 warn_array_subscript_with_type_char (index);
2121 /* Apply default promotions *after* noticing character types. */
2122 index = default_conversion (index);
2124 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2126 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2128 tree rval, type;
2130 /* An array that is indexed by a non-constant
2131 cannot be stored in a register; we must be able to do
2132 address arithmetic on its address.
2133 Likewise an array of elements of variable size. */
2134 if (TREE_CODE (index) != INTEGER_CST
2135 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2136 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2138 if (!c_mark_addressable (array))
2139 return error_mark_node;
2141 /* An array that is indexed by a constant value which is not within
2142 the array bounds cannot be stored in a register either; because we
2143 would get a crash in store_bit_field/extract_bit_field when trying
2144 to access a non-existent part of the register. */
2145 if (TREE_CODE (index) == INTEGER_CST
2146 && TYPE_DOMAIN (TREE_TYPE (array))
2147 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2149 if (!c_mark_addressable (array))
2150 return error_mark_node;
2153 if (pedantic)
2155 tree foo = array;
2156 while (TREE_CODE (foo) == COMPONENT_REF)
2157 foo = TREE_OPERAND (foo, 0);
2158 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2159 pedwarn (loc, OPT_pedantic,
2160 "ISO C forbids subscripting %<register%> array");
2161 else if (!flag_isoc99 && !lvalue_p (foo))
2162 pedwarn (loc, OPT_pedantic,
2163 "ISO C90 forbids subscripting non-lvalue array");
2166 type = TREE_TYPE (TREE_TYPE (array));
2167 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2168 /* Array ref is const/volatile if the array elements are
2169 or if the array is. */
2170 TREE_READONLY (rval)
2171 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2172 | TREE_READONLY (array));
2173 TREE_SIDE_EFFECTS (rval)
2174 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2175 | TREE_SIDE_EFFECTS (array));
2176 TREE_THIS_VOLATILE (rval)
2177 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2178 /* This was added by rms on 16 Nov 91.
2179 It fixes vol struct foo *a; a->elts[1]
2180 in an inline function.
2181 Hope it doesn't break something else. */
2182 | TREE_THIS_VOLATILE (array));
2183 ret = require_complete_type (rval);
2184 protected_set_expr_location (ret, loc);
2185 return ret;
2187 else
2189 tree ar = default_conversion (array);
2191 if (ar == error_mark_node)
2192 return ar;
2194 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2195 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2197 return build_indirect_ref
2198 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2199 "array indexing");
2203 /* Build an external reference to identifier ID. FUN indicates
2204 whether this will be used for a function call. LOC is the source
2205 location of the identifier. This sets *TYPE to the type of the
2206 identifier, which is not the same as the type of the returned value
2207 for CONST_DECLs defined as enum constants. If the type of the
2208 identifier is not available, *TYPE is set to NULL. */
2209 tree
2210 build_external_ref (tree id, int fun, location_t loc, tree *type)
2212 tree ref;
2213 tree decl = lookup_name (id);
2215 /* In Objective-C, an instance variable (ivar) may be preferred to
2216 whatever lookup_name() found. */
2217 decl = objc_lookup_ivar (decl, id);
2219 *type = NULL;
2220 if (decl && decl != error_mark_node)
2222 ref = decl;
2223 *type = TREE_TYPE (ref);
2225 else if (fun)
2226 /* Implicit function declaration. */
2227 ref = implicitly_declare (id);
2228 else if (decl == error_mark_node)
2229 /* Don't complain about something that's already been
2230 complained about. */
2231 return error_mark_node;
2232 else
2234 undeclared_variable (id, loc);
2235 return error_mark_node;
2238 if (TREE_TYPE (ref) == error_mark_node)
2239 return error_mark_node;
2241 if (TREE_DEPRECATED (ref))
2242 warn_deprecated_use (ref);
2244 /* Recursive call does not count as usage. */
2245 if (ref != current_function_decl)
2247 TREE_USED (ref) = 1;
2250 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2252 if (!in_sizeof && !in_typeof)
2253 C_DECL_USED (ref) = 1;
2254 else if (DECL_INITIAL (ref) == 0
2255 && DECL_EXTERNAL (ref)
2256 && !TREE_PUBLIC (ref))
2257 record_maybe_used_decl (ref);
2260 if (TREE_CODE (ref) == CONST_DECL)
2262 used_types_insert (TREE_TYPE (ref));
2263 ref = DECL_INITIAL (ref);
2264 TREE_CONSTANT (ref) = 1;
2266 else if (current_function_decl != 0
2267 && !DECL_FILE_SCOPE_P (current_function_decl)
2268 && (TREE_CODE (ref) == VAR_DECL
2269 || TREE_CODE (ref) == PARM_DECL
2270 || TREE_CODE (ref) == FUNCTION_DECL))
2272 tree context = decl_function_context (ref);
2274 if (context != 0 && context != current_function_decl)
2275 DECL_NONLOCAL (ref) = 1;
2277 /* C99 6.7.4p3: An inline definition of a function with external
2278 linkage ... shall not contain a reference to an identifier with
2279 internal linkage. */
2280 else if (current_function_decl != 0
2281 && DECL_DECLARED_INLINE_P (current_function_decl)
2282 && DECL_EXTERNAL (current_function_decl)
2283 && VAR_OR_FUNCTION_DECL_P (ref)
2284 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2285 && ! TREE_PUBLIC (ref)
2286 && DECL_CONTEXT (ref) != current_function_decl)
2287 pedwarn (loc, 0, "%qD is static but used in inline function %qD "
2288 "which is not static", ref, current_function_decl);
2290 return ref;
2293 /* Record details of decls possibly used inside sizeof or typeof. */
2294 struct maybe_used_decl
2296 /* The decl. */
2297 tree decl;
2298 /* The level seen at (in_sizeof + in_typeof). */
2299 int level;
2300 /* The next one at this level or above, or NULL. */
2301 struct maybe_used_decl *next;
2304 static struct maybe_used_decl *maybe_used_decls;
2306 /* Record that DECL, an undefined static function reference seen
2307 inside sizeof or typeof, might be used if the operand of sizeof is
2308 a VLA type or the operand of typeof is a variably modified
2309 type. */
2311 static void
2312 record_maybe_used_decl (tree decl)
2314 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2315 t->decl = decl;
2316 t->level = in_sizeof + in_typeof;
2317 t->next = maybe_used_decls;
2318 maybe_used_decls = t;
2321 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2322 USED is false, just discard them. If it is true, mark them used
2323 (if no longer inside sizeof or typeof) or move them to the next
2324 level up (if still inside sizeof or typeof). */
2326 void
2327 pop_maybe_used (bool used)
2329 struct maybe_used_decl *p = maybe_used_decls;
2330 int cur_level = in_sizeof + in_typeof;
2331 while (p && p->level > cur_level)
2333 if (used)
2335 if (cur_level == 0)
2336 C_DECL_USED (p->decl) = 1;
2337 else
2338 p->level = cur_level;
2340 p = p->next;
2342 if (!used || cur_level == 0)
2343 maybe_used_decls = p;
2346 /* Return the result of sizeof applied to EXPR. */
2348 struct c_expr
2349 c_expr_sizeof_expr (struct c_expr expr)
2351 struct c_expr ret;
2352 if (expr.value == error_mark_node)
2354 ret.value = error_mark_node;
2355 ret.original_code = ERROR_MARK;
2356 ret.original_type = NULL;
2357 pop_maybe_used (false);
2359 else
2361 bool expr_const_operands = true;
2362 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2363 &expr_const_operands);
2364 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2365 ret.original_code = ERROR_MARK;
2366 ret.original_type = NULL;
2367 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2369 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2370 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2371 folded_expr, ret.value);
2372 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2374 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2376 return ret;
2379 /* Return the result of sizeof applied to T, a structure for the type
2380 name passed to sizeof (rather than the type itself). */
2382 struct c_expr
2383 c_expr_sizeof_type (struct c_type_name *t)
2385 tree type;
2386 struct c_expr ret;
2387 tree type_expr = NULL_TREE;
2388 bool type_expr_const = true;
2389 type = groktypename (t, &type_expr, &type_expr_const);
2390 ret.value = c_sizeof (type);
2391 ret.original_code = ERROR_MARK;
2392 ret.original_type = NULL;
2393 if (type_expr && c_vla_type_p (type))
2395 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2396 type_expr, ret.value);
2397 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2399 pop_maybe_used (type != error_mark_node
2400 ? C_TYPE_VARIABLE_SIZE (type) : false);
2401 return ret;
2404 /* Build a function call to function FUNCTION with parameters PARAMS.
2405 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2406 TREE_VALUE of each node is a parameter-expression.
2407 FUNCTION's data type may be a function type or a pointer-to-function. */
2409 tree
2410 build_function_call (tree function, tree params)
2412 tree fntype, fundecl = 0;
2413 tree name = NULL_TREE, result;
2414 tree tem;
2415 int nargs;
2416 tree *argarray;
2419 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2420 STRIP_TYPE_NOPS (function);
2422 /* Convert anything with function type to a pointer-to-function. */
2423 if (TREE_CODE (function) == FUNCTION_DECL)
2425 /* Implement type-directed function overloading for builtins.
2426 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2427 handle all the type checking. The result is a complete expression
2428 that implements this function call. */
2429 tem = resolve_overloaded_builtin (function, params);
2430 if (tem)
2431 return tem;
2433 name = DECL_NAME (function);
2434 fundecl = function;
2436 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2437 function = function_to_pointer_conversion (function);
2439 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2440 expressions, like those used for ObjC messenger dispatches. */
2441 function = objc_rewrite_function_call (function, params);
2443 function = c_fully_fold (function, false, NULL);
2445 fntype = TREE_TYPE (function);
2447 if (TREE_CODE (fntype) == ERROR_MARK)
2448 return error_mark_node;
2450 if (!(TREE_CODE (fntype) == POINTER_TYPE
2451 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2453 error ("called object %qE is not a function", function);
2454 return error_mark_node;
2457 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2458 current_function_returns_abnormally = 1;
2460 /* fntype now gets the type of function pointed to. */
2461 fntype = TREE_TYPE (fntype);
2463 /* Convert the parameters to the types declared in the
2464 function prototype, or apply default promotions. */
2466 nargs = list_length (params);
2467 argarray = (tree *) alloca (nargs * sizeof (tree));
2468 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2469 params, function, fundecl);
2470 if (nargs < 0)
2471 return error_mark_node;
2473 /* Check that the function is called through a compatible prototype.
2474 If it is not, replace the call by a trap, wrapped up in a compound
2475 expression if necessary. This has the nice side-effect to prevent
2476 the tree-inliner from generating invalid assignment trees which may
2477 blow up in the RTL expander later. */
2478 if (CONVERT_EXPR_P (function)
2479 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2480 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2481 && !comptypes (fntype, TREE_TYPE (tem)))
2483 tree return_type = TREE_TYPE (fntype);
2484 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2485 NULL_TREE);
2486 int i;
2488 /* This situation leads to run-time undefined behavior. We can't,
2489 therefore, simply error unless we can prove that all possible
2490 executions of the program must execute the code. */
2491 if (warning (0, "function called through a non-compatible type"))
2492 /* We can, however, treat "undefined" any way we please.
2493 Call abort to encourage the user to fix the program. */
2494 inform (input_location, "if this code is reached, the program will abort");
2495 /* Before the abort, allow the function arguments to exit or
2496 call longjmp. */
2497 for (i = 0; i < nargs; i++)
2498 trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
2500 if (VOID_TYPE_P (return_type))
2501 return trap;
2502 else
2504 tree rhs;
2506 if (AGGREGATE_TYPE_P (return_type))
2507 rhs = build_compound_literal (return_type,
2508 build_constructor (return_type, 0),
2509 false);
2510 else
2511 rhs = fold_convert (return_type, integer_zero_node);
2513 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2517 /* Check that arguments to builtin functions match the expectations. */
2518 if (fundecl
2519 && DECL_BUILT_IN (fundecl)
2520 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2521 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2522 return error_mark_node;
2524 /* Check that the arguments to the function are valid. */
2525 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2526 TYPE_ARG_TYPES (fntype));
2528 if (name != NULL_TREE
2529 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2531 if (require_constant_value)
2532 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2533 function, nargs, argarray);
2534 else
2535 result = fold_build_call_array (TREE_TYPE (fntype),
2536 function, nargs, argarray);
2537 if (TREE_CODE (result) == NOP_EXPR
2538 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2539 STRIP_TYPE_NOPS (result);
2541 else
2542 result = build_call_array (TREE_TYPE (fntype),
2543 function, nargs, argarray);
2545 if (VOID_TYPE_P (TREE_TYPE (result)))
2546 return result;
2547 return require_complete_type (result);
2550 /* Convert the argument expressions in the list VALUES
2551 to the types in the list TYPELIST. The resulting arguments are
2552 stored in the array ARGARRAY which has size NARGS.
2554 If TYPELIST is exhausted, or when an element has NULL as its type,
2555 perform the default conversions.
2557 PARMLIST is the chain of parm decls for the function being called.
2558 It may be 0, if that info is not available.
2559 It is used only for generating error messages.
2561 FUNCTION is a tree for the called function. It is used only for
2562 error messages, where it is formatted with %qE.
2564 This is also where warnings about wrong number of args are generated.
2566 VALUES is a chain of TREE_LIST nodes with the elements of the list
2567 in the TREE_VALUE slots of those nodes.
2569 Returns the actual number of arguments processed (which may be less
2570 than NARGS in some error situations), or -1 on failure. */
2572 static int
2573 convert_arguments (int nargs, tree *argarray,
2574 tree typelist, tree values, tree function, tree fundecl)
2576 tree typetail, valtail;
2577 int parmnum;
2578 const bool type_generic = fundecl
2579 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2580 bool type_generic_remove_excess_precision = false;
2581 tree selector;
2583 /* Change pointer to function to the function itself for
2584 diagnostics. */
2585 if (TREE_CODE (function) == ADDR_EXPR
2586 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2587 function = TREE_OPERAND (function, 0);
2589 /* Handle an ObjC selector specially for diagnostics. */
2590 selector = objc_message_selector ();
2592 /* For type-generic built-in functions, determine whether excess
2593 precision should be removed (classification) or not
2594 (comparison). */
2595 if (type_generic
2596 && DECL_BUILT_IN (fundecl)
2597 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2599 switch (DECL_FUNCTION_CODE (fundecl))
2601 case BUILT_IN_ISFINITE:
2602 case BUILT_IN_ISINF:
2603 case BUILT_IN_ISINF_SIGN:
2604 case BUILT_IN_ISNAN:
2605 case BUILT_IN_ISNORMAL:
2606 case BUILT_IN_FPCLASSIFY:
2607 type_generic_remove_excess_precision = true;
2608 break;
2610 default:
2611 type_generic_remove_excess_precision = false;
2612 break;
2616 /* Scan the given expressions and types, producing individual
2617 converted arguments and storing them in ARGARRAY. */
2619 for (valtail = values, typetail = typelist, parmnum = 0;
2620 valtail;
2621 valtail = TREE_CHAIN (valtail), parmnum++)
2623 tree type = typetail ? TREE_VALUE (typetail) : 0;
2624 tree val = TREE_VALUE (valtail);
2625 tree valtype = TREE_TYPE (val);
2626 tree rname = function;
2627 int argnum = parmnum + 1;
2628 const char *invalid_func_diag;
2629 bool excess_precision = false;
2630 bool npc;
2632 if (type == void_type_node)
2634 error ("too many arguments to function %qE", function);
2635 return parmnum;
2638 if (selector && argnum > 2)
2640 rname = selector;
2641 argnum -= 2;
2644 npc = null_pointer_constant_p (val);
2646 /* If there is excess precision and a prototype, convert once to
2647 the required type rather than converting via the semantic
2648 type. Likewise without a prototype a float value represented
2649 as long double should be converted once to double. But for
2650 type-generic classification functions excess precision must
2651 be removed here. */
2652 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2653 && (type || !type_generic || !type_generic_remove_excess_precision))
2655 val = TREE_OPERAND (val, 0);
2656 excess_precision = true;
2658 val = c_fully_fold (val, false, NULL);
2659 STRIP_TYPE_NOPS (val);
2661 val = require_complete_type (val);
2663 if (type != 0)
2665 /* Formal parm type is specified by a function prototype. */
2666 tree parmval;
2668 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2670 error ("type of formal parameter %d is incomplete", parmnum + 1);
2671 parmval = val;
2673 else
2675 /* Optionally warn about conversions that
2676 differ from the default conversions. */
2677 if (warn_traditional_conversion || warn_traditional)
2679 unsigned int formal_prec = TYPE_PRECISION (type);
2681 if (INTEGRAL_TYPE_P (type)
2682 && TREE_CODE (valtype) == REAL_TYPE)
2683 warning (0, "passing argument %d of %qE as integer "
2684 "rather than floating due to prototype",
2685 argnum, rname);
2686 if (INTEGRAL_TYPE_P (type)
2687 && TREE_CODE (valtype) == COMPLEX_TYPE)
2688 warning (0, "passing argument %d of %qE as integer "
2689 "rather than complex due to prototype",
2690 argnum, rname);
2691 else if (TREE_CODE (type) == COMPLEX_TYPE
2692 && TREE_CODE (valtype) == REAL_TYPE)
2693 warning (0, "passing argument %d of %qE as complex "
2694 "rather than floating due to prototype",
2695 argnum, rname);
2696 else if (TREE_CODE (type) == REAL_TYPE
2697 && INTEGRAL_TYPE_P (valtype))
2698 warning (0, "passing argument %d of %qE as floating "
2699 "rather than integer due to prototype",
2700 argnum, rname);
2701 else if (TREE_CODE (type) == COMPLEX_TYPE
2702 && INTEGRAL_TYPE_P (valtype))
2703 warning (0, "passing argument %d of %qE as complex "
2704 "rather than integer due to prototype",
2705 argnum, rname);
2706 else if (TREE_CODE (type) == REAL_TYPE
2707 && TREE_CODE (valtype) == COMPLEX_TYPE)
2708 warning (0, "passing argument %d of %qE as floating "
2709 "rather than complex due to prototype",
2710 argnum, rname);
2711 /* ??? At some point, messages should be written about
2712 conversions between complex types, but that's too messy
2713 to do now. */
2714 else if (TREE_CODE (type) == REAL_TYPE
2715 && TREE_CODE (valtype) == REAL_TYPE)
2717 /* Warn if any argument is passed as `float',
2718 since without a prototype it would be `double'. */
2719 if (formal_prec == TYPE_PRECISION (float_type_node)
2720 && type != dfloat32_type_node)
2721 warning (0, "passing argument %d of %qE as %<float%> "
2722 "rather than %<double%> due to prototype",
2723 argnum, rname);
2725 /* Warn if mismatch between argument and prototype
2726 for decimal float types. Warn of conversions with
2727 binary float types and of precision narrowing due to
2728 prototype. */
2729 else if (type != valtype
2730 && (type == dfloat32_type_node
2731 || type == dfloat64_type_node
2732 || type == dfloat128_type_node
2733 || valtype == dfloat32_type_node
2734 || valtype == dfloat64_type_node
2735 || valtype == dfloat128_type_node)
2736 && (formal_prec
2737 <= TYPE_PRECISION (valtype)
2738 || (type == dfloat128_type_node
2739 && (valtype
2740 != dfloat64_type_node
2741 && (valtype
2742 != dfloat32_type_node)))
2743 || (type == dfloat64_type_node
2744 && (valtype
2745 != dfloat32_type_node))))
2746 warning (0, "passing argument %d of %qE as %qT "
2747 "rather than %qT due to prototype",
2748 argnum, rname, type, valtype);
2751 /* Detect integer changing in width or signedness.
2752 These warnings are only activated with
2753 -Wtraditional-conversion, not with -Wtraditional. */
2754 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2755 && INTEGRAL_TYPE_P (valtype))
2757 tree would_have_been = default_conversion (val);
2758 tree type1 = TREE_TYPE (would_have_been);
2760 if (TREE_CODE (type) == ENUMERAL_TYPE
2761 && (TYPE_MAIN_VARIANT (type)
2762 == TYPE_MAIN_VARIANT (valtype)))
2763 /* No warning if function asks for enum
2764 and the actual arg is that enum type. */
2766 else if (formal_prec != TYPE_PRECISION (type1))
2767 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2768 "with different width due to prototype",
2769 argnum, rname);
2770 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2772 /* Don't complain if the formal parameter type
2773 is an enum, because we can't tell now whether
2774 the value was an enum--even the same enum. */
2775 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2777 else if (TREE_CODE (val) == INTEGER_CST
2778 && int_fits_type_p (val, type))
2779 /* Change in signedness doesn't matter
2780 if a constant value is unaffected. */
2782 /* If the value is extended from a narrower
2783 unsigned type, it doesn't matter whether we
2784 pass it as signed or unsigned; the value
2785 certainly is the same either way. */
2786 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2787 && TYPE_UNSIGNED (valtype))
2789 else if (TYPE_UNSIGNED (type))
2790 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2791 "as unsigned due to prototype",
2792 argnum, rname);
2793 else
2794 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2795 "as signed due to prototype", argnum, rname);
2799 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2800 sake of better warnings from convert_and_check. */
2801 if (excess_precision)
2802 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2803 parmval = convert_for_assignment (type, val, ic_argpass, npc,
2804 fundecl, function,
2805 parmnum + 1);
2807 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2808 && INTEGRAL_TYPE_P (type)
2809 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2810 parmval = default_conversion (parmval);
2812 argarray[parmnum] = parmval;
2814 else if (TREE_CODE (valtype) == REAL_TYPE
2815 && (TYPE_PRECISION (valtype)
2816 < TYPE_PRECISION (double_type_node))
2817 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2819 if (type_generic)
2820 argarray[parmnum] = val;
2821 else
2822 /* Convert `float' to `double'. */
2823 argarray[parmnum] = convert (double_type_node, val);
2825 else if (excess_precision && !type_generic)
2826 /* A "double" argument with excess precision being passed
2827 without a prototype or in variable arguments. */
2828 argarray[parmnum] = convert (valtype, val);
2829 else if ((invalid_func_diag =
2830 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2832 error (invalid_func_diag);
2833 return -1;
2835 else
2836 /* Convert `short' and `char' to full-size `int'. */
2837 argarray[parmnum] = default_conversion (val);
2839 if (typetail)
2840 typetail = TREE_CHAIN (typetail);
2843 gcc_assert (parmnum == nargs);
2845 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2847 error ("too few arguments to function %qE", function);
2848 return -1;
2851 return parmnum;
2854 /* This is the entry point used by the parser to build unary operators
2855 in the input. CODE, a tree_code, specifies the unary operator, and
2856 ARG is the operand. For unary plus, the C parser currently uses
2857 CONVERT_EXPR for code.
2859 LOC is the location to use for the tree generated.
2862 struct c_expr
2863 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2865 struct c_expr result;
2867 result.value = build_unary_op (loc, code, arg.value, 0);
2868 result.original_code = code;
2869 result.original_type = NULL;
2871 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2872 overflow_warning (result.value);
2874 return result;
2877 /* This is the entry point used by the parser to build binary operators
2878 in the input. CODE, a tree_code, specifies the binary operator, and
2879 ARG1 and ARG2 are the operands. In addition to constructing the
2880 expression, we check for operands that were written with other binary
2881 operators in a way that is likely to confuse the user.
2883 LOCATION is the location of the binary operator. */
2885 struct c_expr
2886 parser_build_binary_op (location_t location, enum tree_code code,
2887 struct c_expr arg1, struct c_expr arg2)
2889 struct c_expr result;
2891 enum tree_code code1 = arg1.original_code;
2892 enum tree_code code2 = arg2.original_code;
2893 tree type1 = (arg1.original_type
2894 ? arg1.original_type
2895 : TREE_TYPE (arg1.value));
2896 tree type2 = (arg2.original_type
2897 ? arg2.original_type
2898 : TREE_TYPE (arg2.value));
2900 result.value = build_binary_op (location, code,
2901 arg1.value, arg2.value, 1);
2902 result.original_code = code;
2903 result.original_type = NULL;
2905 if (TREE_CODE (result.value) == ERROR_MARK)
2906 return result;
2908 if (location != UNKNOWN_LOCATION)
2909 protected_set_expr_location (result.value, location);
2911 /* Check for cases such as x+y<<z which users are likely
2912 to misinterpret. */
2913 if (warn_parentheses)
2914 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2916 if (TREE_CODE_CLASS (code1) != tcc_comparison)
2917 warn_logical_operator (code, arg1.value, arg2.value);
2919 /* Warn about comparisons against string literals, with the exception
2920 of testing for equality or inequality of a string literal with NULL. */
2921 if (code == EQ_EXPR || code == NE_EXPR)
2923 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2924 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2925 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2927 else if (TREE_CODE_CLASS (code) == tcc_comparison
2928 && (code1 == STRING_CST || code2 == STRING_CST))
2929 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2931 if (TREE_OVERFLOW_P (result.value)
2932 && !TREE_OVERFLOW_P (arg1.value)
2933 && !TREE_OVERFLOW_P (arg2.value))
2934 overflow_warning (result.value);
2936 /* Warn about comparisons of different enum types. */
2937 if (warn_enum_compare
2938 && TREE_CODE_CLASS (code) == tcc_comparison
2939 && TREE_CODE (type1) == ENUMERAL_TYPE
2940 && TREE_CODE (type2) == ENUMERAL_TYPE
2941 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
2942 warning_at (location, OPT_Wenum_compare,
2943 "comparison between %qT and %qT",
2944 type1, type2);
2946 return result;
2949 /* Return a tree for the difference of pointers OP0 and OP1.
2950 The resulting tree has type int. */
2952 static tree
2953 pointer_diff (tree op0, tree op1)
2955 tree restype = ptrdiff_type_node;
2957 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2958 tree con0, con1, lit0, lit1;
2959 tree orig_op1 = op1;
2961 if (TREE_CODE (target_type) == VOID_TYPE)
2962 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2963 "pointer of type %<void *%> used in subtraction");
2964 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2965 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2966 "pointer to a function used in subtraction");
2968 /* If the conversion to ptrdiff_type does anything like widening or
2969 converting a partial to an integral mode, we get a convert_expression
2970 that is in the way to do any simplifications.
2971 (fold-const.c doesn't know that the extra bits won't be needed.
2972 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2973 different mode in place.)
2974 So first try to find a common term here 'by hand'; we want to cover
2975 at least the cases that occur in legal static initializers. */
2976 if (CONVERT_EXPR_P (op0)
2977 && (TYPE_PRECISION (TREE_TYPE (op0))
2978 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2979 con0 = TREE_OPERAND (op0, 0);
2980 else
2981 con0 = op0;
2982 if (CONVERT_EXPR_P (op1)
2983 && (TYPE_PRECISION (TREE_TYPE (op1))
2984 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2985 con1 = TREE_OPERAND (op1, 0);
2986 else
2987 con1 = op1;
2989 if (TREE_CODE (con0) == PLUS_EXPR)
2991 lit0 = TREE_OPERAND (con0, 1);
2992 con0 = TREE_OPERAND (con0, 0);
2994 else
2995 lit0 = integer_zero_node;
2997 if (TREE_CODE (con1) == PLUS_EXPR)
2999 lit1 = TREE_OPERAND (con1, 1);
3000 con1 = TREE_OPERAND (con1, 0);
3002 else
3003 lit1 = integer_zero_node;
3005 if (operand_equal_p (con0, con1, 0))
3007 op0 = lit0;
3008 op1 = lit1;
3012 /* First do the subtraction as integers;
3013 then drop through to build the divide operator.
3014 Do not do default conversions on the minus operator
3015 in case restype is a short type. */
3017 op0 = build_binary_op (input_location,
3018 MINUS_EXPR, convert (restype, op0),
3019 convert (restype, op1), 0);
3020 /* This generates an error if op1 is pointer to incomplete type. */
3021 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3022 error ("arithmetic on pointer to an incomplete type");
3024 /* This generates an error if op0 is pointer to incomplete type. */
3025 op1 = c_size_in_bytes (target_type);
3027 /* Divide by the size, in easiest possible way. */
3028 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3031 /* Construct and perhaps optimize a tree representation
3032 for a unary operation. CODE, a tree_code, specifies the operation
3033 and XARG is the operand.
3034 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3035 the default promotions (such as from short to int).
3036 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3037 allows non-lvalues; this is only used to handle conversion of non-lvalue
3038 arrays to pointers in C99.
3040 LOCATION is the location of the operator. */
3042 tree
3043 build_unary_op (location_t location,
3044 enum tree_code code, tree xarg, int flag)
3046 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3047 tree arg = xarg;
3048 tree argtype = 0;
3049 enum tree_code typecode;
3050 tree val;
3051 tree ret = error_mark_node;
3052 tree eptype = NULL_TREE;
3053 int noconvert = flag;
3054 const char *invalid_op_diag;
3055 bool int_operands;
3057 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3058 if (int_operands)
3059 arg = remove_c_maybe_const_expr (arg);
3061 if (code != ADDR_EXPR)
3062 arg = require_complete_type (arg);
3064 typecode = TREE_CODE (TREE_TYPE (arg));
3065 if (typecode == ERROR_MARK)
3066 return error_mark_node;
3067 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3068 typecode = INTEGER_TYPE;
3070 if ((invalid_op_diag
3071 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3073 error_at (location, invalid_op_diag);
3074 return error_mark_node;
3077 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3079 eptype = TREE_TYPE (arg);
3080 arg = TREE_OPERAND (arg, 0);
3083 switch (code)
3085 case CONVERT_EXPR:
3086 /* This is used for unary plus, because a CONVERT_EXPR
3087 is enough to prevent anybody from looking inside for
3088 associativity, but won't generate any code. */
3089 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3090 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3091 || typecode == VECTOR_TYPE))
3093 error_at (location, "wrong type argument to unary plus");
3094 return error_mark_node;
3096 else if (!noconvert)
3097 arg = default_conversion (arg);
3098 arg = non_lvalue (arg);
3099 break;
3101 case NEGATE_EXPR:
3102 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3103 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3104 || typecode == VECTOR_TYPE))
3106 error_at (location, "wrong type argument to unary minus");
3107 return error_mark_node;
3109 else if (!noconvert)
3110 arg = default_conversion (arg);
3111 break;
3113 case BIT_NOT_EXPR:
3114 /* ~ works on integer types and non float vectors. */
3115 if (typecode == INTEGER_TYPE
3116 || (typecode == VECTOR_TYPE
3117 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3119 if (!noconvert)
3120 arg = default_conversion (arg);
3122 else if (typecode == COMPLEX_TYPE)
3124 code = CONJ_EXPR;
3125 pedwarn (location, OPT_pedantic,
3126 "ISO C does not support %<~%> for complex conjugation");
3127 if (!noconvert)
3128 arg = default_conversion (arg);
3130 else
3132 error_at (location, "wrong type argument to bit-complement");
3133 return error_mark_node;
3135 break;
3137 case ABS_EXPR:
3138 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3140 error_at (location, "wrong type argument to abs");
3141 return error_mark_node;
3143 else if (!noconvert)
3144 arg = default_conversion (arg);
3145 break;
3147 case CONJ_EXPR:
3148 /* Conjugating a real value is a no-op, but allow it anyway. */
3149 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3150 || typecode == COMPLEX_TYPE))
3152 error_at (location, "wrong type argument to conjugation");
3153 return error_mark_node;
3155 else if (!noconvert)
3156 arg = default_conversion (arg);
3157 break;
3159 case TRUTH_NOT_EXPR:
3160 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3161 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3162 && typecode != COMPLEX_TYPE)
3164 error_at (location,
3165 "wrong type argument to unary exclamation mark");
3166 return error_mark_node;
3168 arg = c_objc_common_truthvalue_conversion (location, arg);
3169 ret = invert_truthvalue (arg);
3170 goto return_build_unary_op;
3172 case REALPART_EXPR:
3173 if (TREE_CODE (arg) == COMPLEX_CST)
3174 ret = TREE_REALPART (arg);
3175 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3176 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3177 else
3178 ret = arg;
3179 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3180 eptype = TREE_TYPE (eptype);
3181 goto return_build_unary_op;
3183 case IMAGPART_EXPR:
3184 if (TREE_CODE (arg) == COMPLEX_CST)
3185 ret = TREE_IMAGPART (arg);
3186 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3187 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3188 else
3189 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3190 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3191 eptype = TREE_TYPE (eptype);
3192 goto return_build_unary_op;
3194 case PREINCREMENT_EXPR:
3195 case POSTINCREMENT_EXPR:
3196 case PREDECREMENT_EXPR:
3197 case POSTDECREMENT_EXPR:
3199 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3201 tree inner = build_unary_op (location, code,
3202 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3203 if (inner == error_mark_node)
3204 return error_mark_node;
3205 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3206 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3207 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3208 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3209 goto return_build_unary_op;
3212 /* Complain about anything that is not a true lvalue. */
3213 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3214 || code == POSTINCREMENT_EXPR)
3215 ? lv_increment
3216 : lv_decrement)))
3217 return error_mark_node;
3219 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3220 arg = c_fully_fold (arg, false, NULL);
3222 /* Increment or decrement the real part of the value,
3223 and don't change the imaginary part. */
3224 if (typecode == COMPLEX_TYPE)
3226 tree real, imag;
3228 pedwarn (location, OPT_pedantic,
3229 "ISO C does not support %<++%> and %<--%> on complex types");
3231 arg = stabilize_reference (arg);
3232 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3233 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3234 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3235 if (real == error_mark_node || imag == error_mark_node)
3236 return error_mark_node;
3237 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3238 real, imag);
3239 goto return_build_unary_op;
3242 /* Report invalid types. */
3244 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3245 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3247 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3248 error_at (location, "wrong type argument to increment");
3249 else
3250 error_at (location, "wrong type argument to decrement");
3252 return error_mark_node;
3256 tree inc;
3258 argtype = TREE_TYPE (arg);
3260 /* Compute the increment. */
3262 if (typecode == POINTER_TYPE)
3264 /* If pointer target is an undefined struct,
3265 we just cannot know how to do the arithmetic. */
3266 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3268 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3269 error_at (location,
3270 "increment of pointer to unknown structure");
3271 else
3272 error_at (location,
3273 "decrement of pointer to unknown structure");
3275 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3276 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3278 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3279 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3280 "wrong type argument to increment");
3281 else
3282 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3283 "wrong type argument to decrement");
3286 inc = c_size_in_bytes (TREE_TYPE (argtype));
3287 inc = fold_convert (sizetype, inc);
3289 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3291 /* For signed fract types, we invert ++ to -- or
3292 -- to ++, and change inc from 1 to -1, because
3293 it is not possible to represent 1 in signed fract constants.
3294 For unsigned fract types, the result always overflows and
3295 we get an undefined (original) or the maximum value. */
3296 if (code == PREINCREMENT_EXPR)
3297 code = PREDECREMENT_EXPR;
3298 else if (code == PREDECREMENT_EXPR)
3299 code = PREINCREMENT_EXPR;
3300 else if (code == POSTINCREMENT_EXPR)
3301 code = POSTDECREMENT_EXPR;
3302 else /* code == POSTDECREMENT_EXPR */
3303 code = POSTINCREMENT_EXPR;
3305 inc = integer_minus_one_node;
3306 inc = convert (argtype, inc);
3308 else
3310 inc = integer_one_node;
3311 inc = convert (argtype, inc);
3314 /* Report a read-only lvalue. */
3315 if (TREE_READONLY (arg))
3317 readonly_error (arg,
3318 ((code == PREINCREMENT_EXPR
3319 || code == POSTINCREMENT_EXPR)
3320 ? lv_increment : lv_decrement));
3321 return error_mark_node;
3324 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3325 val = boolean_increment (code, arg);
3326 else
3327 val = build2 (code, TREE_TYPE (arg), arg, inc);
3328 TREE_SIDE_EFFECTS (val) = 1;
3329 if (TREE_CODE (val) != code)
3330 TREE_NO_WARNING (val) = 1;
3331 ret = val;
3332 goto return_build_unary_op;
3335 case ADDR_EXPR:
3336 /* Note that this operation never does default_conversion. */
3338 /* Let &* cancel out to simplify resulting code. */
3339 if (TREE_CODE (arg) == INDIRECT_REF)
3341 /* Don't let this be an lvalue. */
3342 if (lvalue_p (TREE_OPERAND (arg, 0)))
3343 return non_lvalue (TREE_OPERAND (arg, 0));
3344 ret = TREE_OPERAND (arg, 0);
3345 goto return_build_unary_op;
3348 /* For &x[y], return x+y */
3349 if (TREE_CODE (arg) == ARRAY_REF)
3351 tree op0 = TREE_OPERAND (arg, 0);
3352 if (!c_mark_addressable (op0))
3353 return error_mark_node;
3354 return build_binary_op (location, PLUS_EXPR,
3355 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3356 ? array_to_pointer_conversion (op0)
3357 : op0),
3358 TREE_OPERAND (arg, 1), 1);
3361 /* Anything not already handled and not a true memory reference
3362 or a non-lvalue array is an error. */
3363 else if (typecode != FUNCTION_TYPE && !flag
3364 && !lvalue_or_else (arg, lv_addressof))
3365 return error_mark_node;
3367 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3368 folding later. */
3369 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3371 tree inner = build_unary_op (location, code,
3372 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3373 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3374 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3375 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3376 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3377 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3378 goto return_build_unary_op;
3381 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3382 argtype = TREE_TYPE (arg);
3384 /* If the lvalue is const or volatile, merge that into the type
3385 to which the address will point. Note that you can't get a
3386 restricted pointer by taking the address of something, so we
3387 only have to deal with `const' and `volatile' here. */
3388 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3389 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3390 argtype = c_build_type_variant (argtype,
3391 TREE_READONLY (arg),
3392 TREE_THIS_VOLATILE (arg));
3394 if (!c_mark_addressable (arg))
3395 return error_mark_node;
3397 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3398 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3400 argtype = build_pointer_type (argtype);
3402 /* ??? Cope with user tricks that amount to offsetof. Delete this
3403 when we have proper support for integer constant expressions. */
3404 val = get_base_address (arg);
3405 if (val && TREE_CODE (val) == INDIRECT_REF
3406 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3408 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3410 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3411 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3412 goto return_build_unary_op;
3415 val = build1 (ADDR_EXPR, argtype, arg);
3417 ret = val;
3418 goto return_build_unary_op;
3420 default:
3421 gcc_unreachable ();
3424 if (argtype == 0)
3425 argtype = TREE_TYPE (arg);
3426 if (TREE_CODE (arg) == INTEGER_CST)
3427 ret = (require_constant_value
3428 ? fold_build1_initializer (code, argtype, arg)
3429 : fold_build1 (code, argtype, arg));
3430 else
3431 ret = build1 (code, argtype, arg);
3432 return_build_unary_op:
3433 gcc_assert (ret != error_mark_node);
3434 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3435 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3436 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3437 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3438 ret = note_integer_operands (ret);
3439 if (eptype)
3440 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3441 protected_set_expr_location (ret, location);
3442 return ret;
3445 /* Return nonzero if REF is an lvalue valid for this language.
3446 Lvalues can be assigned, unless their type has TYPE_READONLY.
3447 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3449 static int
3450 lvalue_p (const_tree ref)
3452 const enum tree_code code = TREE_CODE (ref);
3454 switch (code)
3456 case REALPART_EXPR:
3457 case IMAGPART_EXPR:
3458 case COMPONENT_REF:
3459 return lvalue_p (TREE_OPERAND (ref, 0));
3461 case C_MAYBE_CONST_EXPR:
3462 return lvalue_p (TREE_OPERAND (ref, 1));
3464 case COMPOUND_LITERAL_EXPR:
3465 case STRING_CST:
3466 return 1;
3468 case INDIRECT_REF:
3469 case ARRAY_REF:
3470 case VAR_DECL:
3471 case PARM_DECL:
3472 case RESULT_DECL:
3473 case ERROR_MARK:
3474 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3475 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3477 case BIND_EXPR:
3478 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3480 default:
3481 return 0;
3485 /* Give an error for storing in something that is 'const'. */
3487 static void
3488 readonly_error (tree arg, enum lvalue_use use)
3490 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3491 || use == lv_asm);
3492 /* Using this macro rather than (for example) arrays of messages
3493 ensures that all the format strings are checked at compile
3494 time. */
3495 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3496 : (use == lv_increment ? (I) \
3497 : (use == lv_decrement ? (D) : (AS))))
3498 if (TREE_CODE (arg) == COMPONENT_REF)
3500 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3501 readonly_error (TREE_OPERAND (arg, 0), use);
3502 else
3503 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3504 G_("increment of read-only member %qD"),
3505 G_("decrement of read-only member %qD"),
3506 G_("read-only member %qD used as %<asm%> output")),
3507 TREE_OPERAND (arg, 1));
3509 else if (TREE_CODE (arg) == VAR_DECL)
3510 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3511 G_("increment of read-only variable %qD"),
3512 G_("decrement of read-only variable %qD"),
3513 G_("read-only variable %qD used as %<asm%> output")),
3514 arg);
3515 else
3516 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3517 G_("increment of read-only location %qE"),
3518 G_("decrement of read-only location %qE"),
3519 G_("read-only location %qE used as %<asm%> output")),
3520 arg);
3524 /* Return nonzero if REF is an lvalue valid for this language;
3525 otherwise, print an error message and return zero. USE says
3526 how the lvalue is being used and so selects the error message. */
3528 static int
3529 lvalue_or_else (const_tree ref, enum lvalue_use use)
3531 int win = lvalue_p (ref);
3533 if (!win)
3534 lvalue_error (use);
3536 return win;
3539 /* Mark EXP saying that we need to be able to take the
3540 address of it; it should not be allocated in a register.
3541 Returns true if successful. */
3543 bool
3544 c_mark_addressable (tree exp)
3546 tree x = exp;
3548 while (1)
3549 switch (TREE_CODE (x))
3551 case COMPONENT_REF:
3552 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3554 error
3555 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3556 return false;
3559 /* ... fall through ... */
3561 case ADDR_EXPR:
3562 case ARRAY_REF:
3563 case REALPART_EXPR:
3564 case IMAGPART_EXPR:
3565 x = TREE_OPERAND (x, 0);
3566 break;
3568 case COMPOUND_LITERAL_EXPR:
3569 case CONSTRUCTOR:
3570 TREE_ADDRESSABLE (x) = 1;
3571 return true;
3573 case VAR_DECL:
3574 case CONST_DECL:
3575 case PARM_DECL:
3576 case RESULT_DECL:
3577 if (C_DECL_REGISTER (x)
3578 && DECL_NONLOCAL (x))
3580 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3582 error
3583 ("global register variable %qD used in nested function", x);
3584 return false;
3586 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3588 else if (C_DECL_REGISTER (x))
3590 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3591 error ("address of global register variable %qD requested", x);
3592 else
3593 error ("address of register variable %qD requested", x);
3594 return false;
3597 /* drops in */
3598 case FUNCTION_DECL:
3599 TREE_ADDRESSABLE (x) = 1;
3600 /* drops out */
3601 default:
3602 return true;
3606 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3607 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3608 if folded to an integer constant then the unselected half may
3609 contain arbitrary operations not normally permitted in constant
3610 expressions. */
3612 tree
3613 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3615 tree type1;
3616 tree type2;
3617 enum tree_code code1;
3618 enum tree_code code2;
3619 tree result_type = NULL;
3620 tree ep_result_type = NULL;
3621 tree orig_op1 = op1, orig_op2 = op2;
3622 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3623 bool ifexp_int_operands;
3624 tree ret;
3625 bool objc_ok;
3627 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3628 if (op1_int_operands)
3629 op1 = remove_c_maybe_const_expr (op1);
3630 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3631 if (op2_int_operands)
3632 op2 = remove_c_maybe_const_expr (op2);
3633 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3634 if (ifexp_int_operands)
3635 ifexp = remove_c_maybe_const_expr (ifexp);
3637 /* Promote both alternatives. */
3639 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3640 op1 = default_conversion (op1);
3641 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3642 op2 = default_conversion (op2);
3644 if (TREE_CODE (ifexp) == ERROR_MARK
3645 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3646 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3647 return error_mark_node;
3649 type1 = TREE_TYPE (op1);
3650 code1 = TREE_CODE (type1);
3651 type2 = TREE_TYPE (op2);
3652 code2 = TREE_CODE (type2);
3654 /* C90 does not permit non-lvalue arrays in conditional expressions.
3655 In C99 they will be pointers by now. */
3656 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3658 error ("non-lvalue array in conditional expression");
3659 return error_mark_node;
3662 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3664 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3665 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3666 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3667 || code1 == COMPLEX_TYPE)
3668 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3669 || code2 == COMPLEX_TYPE))
3671 ep_result_type = c_common_type (type1, type2);
3672 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3674 op1 = TREE_OPERAND (op1, 0);
3675 type1 = TREE_TYPE (op1);
3676 gcc_assert (TREE_CODE (type1) == code1);
3678 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3680 op2 = TREE_OPERAND (op2, 0);
3681 type2 = TREE_TYPE (op2);
3682 gcc_assert (TREE_CODE (type2) == code2);
3686 /* Quickly detect the usual case where op1 and op2 have the same type
3687 after promotion. */
3688 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3690 if (type1 == type2)
3691 result_type = type1;
3692 else
3693 result_type = TYPE_MAIN_VARIANT (type1);
3695 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3696 || code1 == COMPLEX_TYPE)
3697 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3698 || code2 == COMPLEX_TYPE))
3700 result_type = c_common_type (type1, type2);
3702 /* If -Wsign-compare, warn here if type1 and type2 have
3703 different signedness. We'll promote the signed to unsigned
3704 and later code won't know it used to be different.
3705 Do this check on the original types, so that explicit casts
3706 will be considered, but default promotions won't. */
3707 if (!skip_evaluation)
3709 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3710 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3712 if (unsigned_op1 ^ unsigned_op2)
3714 bool ovf;
3716 /* Do not warn if the result type is signed, since the
3717 signed type will only be chosen if it can represent
3718 all the values of the unsigned type. */
3719 if (!TYPE_UNSIGNED (result_type))
3720 /* OK */;
3721 else
3723 bool op1_maybe_const = true;
3724 bool op2_maybe_const = true;
3726 /* Do not warn if the signed quantity is an
3727 unsuffixed integer literal (or some static
3728 constant expression involving such literals) and
3729 it is non-negative. This warning requires the
3730 operands to be folded for best results, so do
3731 that folding in this case even without
3732 warn_sign_compare to avoid warning options
3733 possibly affecting code generation. */
3734 op1 = c_fully_fold (op1, require_constant_value,
3735 &op1_maybe_const);
3736 op2 = c_fully_fold (op2, require_constant_value,
3737 &op2_maybe_const);
3739 if (warn_sign_compare)
3741 if ((unsigned_op2
3742 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3743 || (unsigned_op1
3744 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3745 /* OK */;
3746 else
3747 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3749 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3751 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3752 NULL, op1);
3753 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3755 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3757 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3758 NULL, op2);
3759 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3765 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3767 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3768 pedwarn (input_location, OPT_pedantic,
3769 "ISO C forbids conditional expr with only one void side");
3770 result_type = void_type_node;
3772 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3774 if (comp_target_types (type1, type2))
3775 result_type = common_pointer_type (type1, type2);
3776 else if (null_pointer_constant_p (orig_op1))
3777 result_type = qualify_type (type2, type1);
3778 else if (null_pointer_constant_p (orig_op2))
3779 result_type = qualify_type (type1, type2);
3780 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3782 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3783 pedwarn (input_location, OPT_pedantic,
3784 "ISO C forbids conditional expr between "
3785 "%<void *%> and function pointer");
3786 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3787 TREE_TYPE (type2)));
3789 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3791 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3792 pedwarn (input_location, OPT_pedantic,
3793 "ISO C forbids conditional expr between "
3794 "%<void *%> and function pointer");
3795 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3796 TREE_TYPE (type1)));
3798 else
3800 if (!objc_ok)
3801 pedwarn (input_location, 0,
3802 "pointer type mismatch in conditional expression");
3803 result_type = build_pointer_type (void_type_node);
3806 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3808 if (!null_pointer_constant_p (orig_op2))
3809 pedwarn (input_location, 0,
3810 "pointer/integer type mismatch in conditional expression");
3811 else
3813 op2 = null_pointer_node;
3815 result_type = type1;
3817 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3819 if (!null_pointer_constant_p (orig_op1))
3820 pedwarn (input_location, 0,
3821 "pointer/integer type mismatch in conditional expression");
3822 else
3824 op1 = null_pointer_node;
3826 result_type = type2;
3829 if (!result_type)
3831 if (flag_cond_mismatch)
3832 result_type = void_type_node;
3833 else
3835 error ("type mismatch in conditional expression");
3836 return error_mark_node;
3840 /* Merge const and volatile flags of the incoming types. */
3841 result_type
3842 = build_type_variant (result_type,
3843 TREE_READONLY (op1) || TREE_READONLY (op2),
3844 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3846 if (result_type != TREE_TYPE (op1))
3847 op1 = convert_and_check (result_type, op1);
3848 if (result_type != TREE_TYPE (op2))
3849 op2 = convert_and_check (result_type, op2);
3851 if (ifexp_bcp && ifexp == truthvalue_true_node)
3853 op2_int_operands = true;
3854 op1 = c_fully_fold (op1, require_constant_value, NULL);
3856 if (ifexp_bcp && ifexp == truthvalue_false_node)
3858 op1_int_operands = true;
3859 op2 = c_fully_fold (op2, require_constant_value, NULL);
3861 int_const = int_operands = (ifexp_int_operands
3862 && op1_int_operands
3863 && op2_int_operands);
3864 if (int_operands)
3866 int_const = ((ifexp == truthvalue_true_node
3867 && TREE_CODE (orig_op1) == INTEGER_CST
3868 && !TREE_OVERFLOW (orig_op1))
3869 || (ifexp == truthvalue_false_node
3870 && TREE_CODE (orig_op2) == INTEGER_CST
3871 && !TREE_OVERFLOW (orig_op2)));
3873 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3874 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3875 else
3877 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3878 if (int_operands)
3879 ret = note_integer_operands (ret);
3881 if (ep_result_type)
3882 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3884 return ret;
3887 /* Return a compound expression that performs two expressions and
3888 returns the value of the second of them. */
3890 tree
3891 build_compound_expr (tree expr1, tree expr2)
3893 bool expr1_int_operands, expr2_int_operands;
3894 tree eptype = NULL_TREE;
3895 tree ret;
3897 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
3898 if (expr1_int_operands)
3899 expr1 = remove_c_maybe_const_expr (expr1);
3900 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
3901 if (expr2_int_operands)
3902 expr2 = remove_c_maybe_const_expr (expr2);
3904 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
3905 expr1 = TREE_OPERAND (expr1, 0);
3906 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
3908 eptype = TREE_TYPE (expr2);
3909 expr2 = TREE_OPERAND (expr2, 0);
3912 if (!TREE_SIDE_EFFECTS (expr1))
3914 /* The left-hand operand of a comma expression is like an expression
3915 statement: with -Wunused, we should warn if it doesn't have
3916 any side-effects, unless it was explicitly cast to (void). */
3917 if (warn_unused_value)
3919 if (VOID_TYPE_P (TREE_TYPE (expr1))
3920 && CONVERT_EXPR_P (expr1))
3921 ; /* (void) a, b */
3922 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3923 && TREE_CODE (expr1) == COMPOUND_EXPR
3924 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
3925 ; /* (void) a, (void) b, c */
3926 else
3927 warning (OPT_Wunused_value,
3928 "left-hand operand of comma expression has no effect");
3932 /* With -Wunused, we should also warn if the left-hand operand does have
3933 side-effects, but computes a value which is not used. For example, in
3934 `foo() + bar(), baz()' the result of the `+' operator is not used,
3935 so we should issue a warning. */
3936 else if (warn_unused_value)
3937 warn_if_unused_value (expr1, input_location);
3939 if (expr2 == error_mark_node)
3940 return error_mark_node;
3942 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3944 if (flag_isoc99
3945 && expr1_int_operands
3946 && expr2_int_operands)
3947 ret = note_integer_operands (ret);
3949 if (eptype)
3950 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3952 return ret;
3955 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3957 tree
3958 build_c_cast (tree type, tree expr)
3960 tree value;
3962 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3963 expr = TREE_OPERAND (expr, 0);
3965 value = expr;
3967 if (type == error_mark_node || expr == error_mark_node)
3968 return error_mark_node;
3970 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3971 only in <protocol> qualifications. But when constructing cast expressions,
3972 the protocols do matter and must be kept around. */
3973 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3974 return build1 (NOP_EXPR, type, expr);
3976 type = TYPE_MAIN_VARIANT (type);
3978 if (TREE_CODE (type) == ARRAY_TYPE)
3980 error ("cast specifies array type");
3981 return error_mark_node;
3984 if (TREE_CODE (type) == FUNCTION_TYPE)
3986 error ("cast specifies function type");
3987 return error_mark_node;
3990 if (!VOID_TYPE_P (type))
3992 value = require_complete_type (value);
3993 if (value == error_mark_node)
3994 return error_mark_node;
3997 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3999 if (TREE_CODE (type) == RECORD_TYPE
4000 || TREE_CODE (type) == UNION_TYPE)
4001 pedwarn (input_location, OPT_pedantic,
4002 "ISO C forbids casting nonscalar to the same type");
4004 else if (TREE_CODE (type) == UNION_TYPE)
4006 tree field;
4008 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4009 if (TREE_TYPE (field) != error_mark_node
4010 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4011 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4012 break;
4014 if (field)
4016 tree t;
4018 pedwarn (input_location, OPT_pedantic,
4019 "ISO C forbids casts to union type");
4020 t = digest_init (type,
4021 build_constructor_single (type, field, value),
4022 false, true, 0);
4023 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4024 return t;
4026 error ("cast to union type from type not present in union");
4027 return error_mark_node;
4029 else
4031 tree otype, ovalue;
4033 if (type == void_type_node)
4034 return build1 (CONVERT_EXPR, type, value);
4036 otype = TREE_TYPE (value);
4038 /* Optionally warn about potentially worrisome casts. */
4040 if (warn_cast_qual
4041 && TREE_CODE (type) == POINTER_TYPE
4042 && TREE_CODE (otype) == POINTER_TYPE)
4044 tree in_type = type;
4045 tree in_otype = otype;
4046 int added = 0;
4047 int discarded = 0;
4049 /* Check that the qualifiers on IN_TYPE are a superset of
4050 the qualifiers of IN_OTYPE. The outermost level of
4051 POINTER_TYPE nodes is uninteresting and we stop as soon
4052 as we hit a non-POINTER_TYPE node on either type. */
4055 in_otype = TREE_TYPE (in_otype);
4056 in_type = TREE_TYPE (in_type);
4058 /* GNU C allows cv-qualified function types. 'const'
4059 means the function is very pure, 'volatile' means it
4060 can't return. We need to warn when such qualifiers
4061 are added, not when they're taken away. */
4062 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4063 && TREE_CODE (in_type) == FUNCTION_TYPE)
4064 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4065 else
4066 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4068 while (TREE_CODE (in_type) == POINTER_TYPE
4069 && TREE_CODE (in_otype) == POINTER_TYPE);
4071 if (added)
4072 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4074 if (discarded)
4075 /* There are qualifiers present in IN_OTYPE that are not
4076 present in IN_TYPE. */
4077 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
4080 /* Warn about possible alignment problems. */
4081 if (STRICT_ALIGNMENT
4082 && TREE_CODE (type) == POINTER_TYPE
4083 && TREE_CODE (otype) == POINTER_TYPE
4084 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4085 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4086 /* Don't warn about opaque types, where the actual alignment
4087 restriction is unknown. */
4088 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4089 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4090 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4091 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4092 warning (OPT_Wcast_align,
4093 "cast increases required alignment of target type");
4095 if (TREE_CODE (type) == INTEGER_TYPE
4096 && TREE_CODE (otype) == POINTER_TYPE
4097 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4098 /* Unlike conversion of integers to pointers, where the
4099 warning is disabled for converting constants because
4100 of cases such as SIG_*, warn about converting constant
4101 pointers to integers. In some cases it may cause unwanted
4102 sign extension, and a warning is appropriate. */
4103 warning (OPT_Wpointer_to_int_cast,
4104 "cast from pointer to integer of different size");
4106 if (TREE_CODE (value) == CALL_EXPR
4107 && TREE_CODE (type) != TREE_CODE (otype))
4108 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4109 "to non-matching type %qT", otype, type);
4111 if (TREE_CODE (type) == POINTER_TYPE
4112 && TREE_CODE (otype) == INTEGER_TYPE
4113 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4114 /* Don't warn about converting any constant. */
4115 && !TREE_CONSTANT (value))
4116 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4117 "of different size");
4119 if (warn_strict_aliasing <= 2)
4120 strict_aliasing_warning (otype, type, expr);
4122 /* If pedantic, warn for conversions between function and object
4123 pointer types, except for converting a null pointer constant
4124 to function pointer type. */
4125 if (pedantic
4126 && TREE_CODE (type) == POINTER_TYPE
4127 && TREE_CODE (otype) == POINTER_TYPE
4128 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4129 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4130 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4131 "conversion of function pointer to object pointer type");
4133 if (pedantic
4134 && TREE_CODE (type) == POINTER_TYPE
4135 && TREE_CODE (otype) == POINTER_TYPE
4136 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4137 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4138 && !null_pointer_constant_p (value))
4139 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4140 "conversion of object pointer to function pointer type");
4142 ovalue = value;
4143 value = convert (type, value);
4145 /* Ignore any integer overflow caused by the cast. */
4146 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4148 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4150 if (!TREE_OVERFLOW (value))
4152 /* Avoid clobbering a shared constant. */
4153 value = copy_node (value);
4154 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4157 else if (TREE_OVERFLOW (value))
4158 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4159 value = build_int_cst_wide (TREE_TYPE (value),
4160 TREE_INT_CST_LOW (value),
4161 TREE_INT_CST_HIGH (value));
4165 /* Don't let a cast be an lvalue. */
4166 if (value == expr)
4167 value = non_lvalue (value);
4169 /* Don't allow the results of casting to floating-point or complex
4170 types be confused with actual constants, or casts involving
4171 integer and pointer types other than direct integer-to-integer
4172 and integer-to-pointer be confused with integer constant
4173 expressions and null pointer constants. */
4174 if (TREE_CODE (value) == REAL_CST
4175 || TREE_CODE (value) == COMPLEX_CST
4176 || (TREE_CODE (value) == INTEGER_CST
4177 && !((TREE_CODE (expr) == INTEGER_CST
4178 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4179 || TREE_CODE (expr) == REAL_CST
4180 || TREE_CODE (expr) == COMPLEX_CST)))
4181 value = build1 (NOP_EXPR, type, value);
4183 return value;
4186 /* Interpret a cast of expression EXPR to type TYPE. */
4187 tree
4188 c_cast_expr (struct c_type_name *type_name, tree expr)
4190 tree type;
4191 tree type_expr = NULL_TREE;
4192 bool type_expr_const = true;
4193 tree ret;
4194 int saved_wsp = warn_strict_prototypes;
4196 /* This avoids warnings about unprototyped casts on
4197 integers. E.g. "#define SIG_DFL (void(*)())0". */
4198 if (TREE_CODE (expr) == INTEGER_CST)
4199 warn_strict_prototypes = 0;
4200 type = groktypename (type_name, &type_expr, &type_expr_const);
4201 warn_strict_prototypes = saved_wsp;
4203 ret = build_c_cast (type, expr);
4204 if (type_expr)
4206 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4207 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4209 return ret;
4212 /* Build an assignment expression of lvalue LHS from value RHS.
4213 MODIFYCODE is the code for a binary operator that we use
4214 to combine the old value of LHS with RHS to get the new value.
4215 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4217 LOCATION is the location of the MODIFYCODE operator. */
4219 tree
4220 build_modify_expr (location_t location,
4221 tree lhs, enum tree_code modifycode, tree rhs)
4223 tree result;
4224 tree newrhs;
4225 tree rhs_semantic_type = NULL_TREE;
4226 tree lhstype = TREE_TYPE (lhs);
4227 tree olhstype = lhstype;
4228 bool npc;
4230 /* Types that aren't fully specified cannot be used in assignments. */
4231 lhs = require_complete_type (lhs);
4233 /* Avoid duplicate error messages from operands that had errors. */
4234 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4235 return error_mark_node;
4237 if (!lvalue_or_else (lhs, lv_assign))
4238 return error_mark_node;
4240 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4242 rhs_semantic_type = TREE_TYPE (rhs);
4243 rhs = TREE_OPERAND (rhs, 0);
4246 newrhs = rhs;
4248 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4250 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4251 modifycode, rhs);
4252 if (inner == error_mark_node)
4253 return error_mark_node;
4254 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4255 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4256 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4257 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4258 protected_set_expr_location (result, location);
4259 return result;
4262 /* If a binary op has been requested, combine the old LHS value with the RHS
4263 producing the value we should actually store into the LHS. */
4265 if (modifycode != NOP_EXPR)
4267 lhs = c_fully_fold (lhs, false, NULL);
4268 lhs = stabilize_reference (lhs);
4269 newrhs = build_binary_op (location,
4270 modifycode, lhs, rhs, 1);
4273 /* Give an error for storing in something that is 'const'. */
4275 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4276 || ((TREE_CODE (lhstype) == RECORD_TYPE
4277 || TREE_CODE (lhstype) == UNION_TYPE)
4278 && C_TYPE_FIELDS_READONLY (lhstype)))
4280 readonly_error (lhs, lv_assign);
4281 return error_mark_node;
4284 /* If storing into a structure or union member,
4285 it has probably been given type `int'.
4286 Compute the type that would go with
4287 the actual amount of storage the member occupies. */
4289 if (TREE_CODE (lhs) == COMPONENT_REF
4290 && (TREE_CODE (lhstype) == INTEGER_TYPE
4291 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4292 || TREE_CODE (lhstype) == REAL_TYPE
4293 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4294 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4296 /* If storing in a field that is in actuality a short or narrower than one,
4297 we must store in the field in its actual type. */
4299 if (lhstype != TREE_TYPE (lhs))
4301 lhs = copy_node (lhs);
4302 TREE_TYPE (lhs) = lhstype;
4305 /* Convert new value to destination type. Fold it first, then
4306 restore any excess precision information, for the sake of
4307 conversion warnings. */
4309 npc = null_pointer_constant_p (newrhs);
4310 newrhs = c_fully_fold (newrhs, false, NULL);
4311 if (rhs_semantic_type)
4312 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4313 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign, npc,
4314 NULL_TREE, NULL_TREE, 0);
4315 if (TREE_CODE (newrhs) == ERROR_MARK)
4316 return error_mark_node;
4318 /* Emit ObjC write barrier, if necessary. */
4319 if (c_dialect_objc () && flag_objc_gc)
4321 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4322 if (result)
4324 protected_set_expr_location (result, location);
4325 return result;
4329 /* Scan operands. */
4331 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4332 TREE_SIDE_EFFECTS (result) = 1;
4333 protected_set_expr_location (result, location);
4335 /* If we got the LHS in a different type for storing in,
4336 convert the result back to the nominal type of LHS
4337 so that the value we return always has the same type
4338 as the LHS argument. */
4340 if (olhstype == TREE_TYPE (result))
4341 return result;
4343 result = convert_for_assignment (olhstype, result, ic_assign, false,
4344 NULL_TREE, NULL_TREE, 0);
4345 protected_set_expr_location (result, location);
4346 return result;
4349 /* Convert value RHS to type TYPE as preparation for an assignment
4350 to an lvalue of type TYPE. NULL_POINTER_CONSTANT says whether RHS
4351 was a null pointer constant before any folding.
4352 The real work of conversion is done by `convert'.
4353 The purpose of this function is to generate error messages
4354 for assignments that are not allowed in C.
4355 ERRTYPE says whether it is argument passing, assignment,
4356 initialization or return.
4358 FUNCTION is a tree for the function being called.
4359 PARMNUM is the number of the argument, for printing in error messages. */
4361 static tree
4362 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
4363 bool null_pointer_constant,
4364 tree fundecl, tree function, int parmnum)
4366 enum tree_code codel = TREE_CODE (type);
4367 tree orig_rhs = rhs;
4368 tree rhstype;
4369 enum tree_code coder;
4370 tree rname = NULL_TREE;
4371 bool objc_ok = false;
4373 if (errtype == ic_argpass)
4375 tree selector;
4376 /* Change pointer to function to the function itself for
4377 diagnostics. */
4378 if (TREE_CODE (function) == ADDR_EXPR
4379 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4380 function = TREE_OPERAND (function, 0);
4382 /* Handle an ObjC selector specially for diagnostics. */
4383 selector = objc_message_selector ();
4384 rname = function;
4385 if (selector && parmnum > 2)
4387 rname = selector;
4388 parmnum -= 2;
4392 /* This macro is used to emit diagnostics to ensure that all format
4393 strings are complete sentences, visible to gettext and checked at
4394 compile time. */
4395 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4396 do { \
4397 switch (errtype) \
4399 case ic_argpass: \
4400 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4401 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4402 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4403 "expected %qT but argument is of type %qT", \
4404 type, rhstype); \
4405 break; \
4406 case ic_assign: \
4407 pedwarn (LOCATION, OPT, AS); \
4408 break; \
4409 case ic_init: \
4410 pedwarn (LOCATION, OPT, IN); \
4411 break; \
4412 case ic_return: \
4413 pedwarn (LOCATION, OPT, RE); \
4414 break; \
4415 default: \
4416 gcc_unreachable (); \
4418 } while (0)
4420 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4421 rhs = TREE_OPERAND (rhs, 0);
4423 rhstype = TREE_TYPE (rhs);
4424 coder = TREE_CODE (rhstype);
4426 if (coder == ERROR_MARK)
4427 return error_mark_node;
4429 if (c_dialect_objc ())
4431 int parmno;
4433 switch (errtype)
4435 case ic_return:
4436 parmno = 0;
4437 break;
4439 case ic_assign:
4440 parmno = -1;
4441 break;
4443 case ic_init:
4444 parmno = -2;
4445 break;
4447 default:
4448 parmno = parmnum;
4449 break;
4452 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4455 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4456 return rhs;
4458 if (coder == VOID_TYPE)
4460 /* Except for passing an argument to an unprototyped function,
4461 this is a constraint violation. When passing an argument to
4462 an unprototyped function, it is compile-time undefined;
4463 making it a constraint in that case was rejected in
4464 DR#252. */
4465 error ("void value not ignored as it ought to be");
4466 return error_mark_node;
4468 rhs = require_complete_type (rhs);
4469 if (rhs == error_mark_node)
4470 return error_mark_node;
4471 /* A type converts to a reference to it.
4472 This code doesn't fully support references, it's just for the
4473 special case of va_start and va_copy. */
4474 if (codel == REFERENCE_TYPE
4475 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4477 if (!lvalue_p (rhs))
4479 error ("cannot pass rvalue to reference parameter");
4480 return error_mark_node;
4482 if (!c_mark_addressable (rhs))
4483 return error_mark_node;
4484 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4486 /* We already know that these two types are compatible, but they
4487 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4488 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4489 likely to be va_list, a typedef to __builtin_va_list, which
4490 is different enough that it will cause problems later. */
4491 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4492 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4494 rhs = build1 (NOP_EXPR, type, rhs);
4495 return rhs;
4497 /* Some types can interconvert without explicit casts. */
4498 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4499 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4500 return convert (type, rhs);
4501 /* Arithmetic types all interconvert, and enum is treated like int. */
4502 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4503 || codel == FIXED_POINT_TYPE
4504 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4505 || codel == BOOLEAN_TYPE)
4506 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4507 || coder == FIXED_POINT_TYPE
4508 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4509 || coder == BOOLEAN_TYPE))
4511 tree ret;
4512 bool save = in_late_binary_op;
4513 if (codel == BOOLEAN_TYPE)
4514 in_late_binary_op = true;
4515 ret = convert_and_check (type, orig_rhs);
4516 if (codel == BOOLEAN_TYPE)
4517 in_late_binary_op = save;
4518 return ret;
4521 /* Aggregates in different TUs might need conversion. */
4522 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4523 && codel == coder
4524 && comptypes (type, rhstype))
4525 return convert_and_check (type, rhs);
4527 /* Conversion to a transparent union from its member types.
4528 This applies only to function arguments. */
4529 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4530 && errtype == ic_argpass)
4532 tree memb, marginal_memb = NULL_TREE;
4534 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4536 tree memb_type = TREE_TYPE (memb);
4538 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4539 TYPE_MAIN_VARIANT (rhstype)))
4540 break;
4542 if (TREE_CODE (memb_type) != POINTER_TYPE)
4543 continue;
4545 if (coder == POINTER_TYPE)
4547 tree ttl = TREE_TYPE (memb_type);
4548 tree ttr = TREE_TYPE (rhstype);
4550 /* Any non-function converts to a [const][volatile] void *
4551 and vice versa; otherwise, targets must be the same.
4552 Meanwhile, the lhs target must have all the qualifiers of
4553 the rhs. */
4554 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4555 || comp_target_types (memb_type, rhstype))
4557 /* If this type won't generate any warnings, use it. */
4558 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4559 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4560 && TREE_CODE (ttl) == FUNCTION_TYPE)
4561 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4562 == TYPE_QUALS (ttr))
4563 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4564 == TYPE_QUALS (ttl))))
4565 break;
4567 /* Keep looking for a better type, but remember this one. */
4568 if (!marginal_memb)
4569 marginal_memb = memb;
4573 /* Can convert integer zero to any pointer type. */
4574 if (null_pointer_constant)
4576 rhs = null_pointer_node;
4577 break;
4581 if (memb || marginal_memb)
4583 if (!memb)
4585 /* We have only a marginally acceptable member type;
4586 it needs a warning. */
4587 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4588 tree ttr = TREE_TYPE (rhstype);
4590 /* Const and volatile mean something different for function
4591 types, so the usual warnings are not appropriate. */
4592 if (TREE_CODE (ttr) == FUNCTION_TYPE
4593 && TREE_CODE (ttl) == FUNCTION_TYPE)
4595 /* Because const and volatile on functions are
4596 restrictions that say the function will not do
4597 certain things, it is okay to use a const or volatile
4598 function where an ordinary one is wanted, but not
4599 vice-versa. */
4600 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4601 WARN_FOR_ASSIGNMENT (input_location, 0,
4602 G_("passing argument %d of %qE "
4603 "makes qualified function "
4604 "pointer from unqualified"),
4605 G_("assignment makes qualified "
4606 "function pointer from "
4607 "unqualified"),
4608 G_("initialization makes qualified "
4609 "function pointer from "
4610 "unqualified"),
4611 G_("return makes qualified function "
4612 "pointer from unqualified"));
4614 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4615 WARN_FOR_ASSIGNMENT (input_location, 0,
4616 G_("passing argument %d of %qE discards "
4617 "qualifiers from pointer target type"),
4618 G_("assignment discards qualifiers "
4619 "from pointer target type"),
4620 G_("initialization discards qualifiers "
4621 "from pointer target type"),
4622 G_("return discards qualifiers from "
4623 "pointer target type"));
4625 memb = marginal_memb;
4628 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4629 pedwarn (input_location, OPT_pedantic,
4630 "ISO C prohibits argument conversion to union type");
4632 rhs = fold_convert (TREE_TYPE (memb), rhs);
4633 return build_constructor_single (type, memb, rhs);
4637 /* Conversions among pointers */
4638 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4639 && (coder == codel))
4641 tree ttl = TREE_TYPE (type);
4642 tree ttr = TREE_TYPE (rhstype);
4643 tree mvl = ttl;
4644 tree mvr = ttr;
4645 bool is_opaque_pointer;
4646 int target_cmp = 0; /* Cache comp_target_types () result. */
4648 if (TREE_CODE (mvl) != ARRAY_TYPE)
4649 mvl = TYPE_MAIN_VARIANT (mvl);
4650 if (TREE_CODE (mvr) != ARRAY_TYPE)
4651 mvr = TYPE_MAIN_VARIANT (mvr);
4652 /* Opaque pointers are treated like void pointers. */
4653 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4655 /* C++ does not allow the implicit conversion void* -> T*. However,
4656 for the purpose of reducing the number of false positives, we
4657 tolerate the special case of
4659 int *p = NULL;
4661 where NULL is typically defined in C to be '(void *) 0'. */
4662 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4663 warning (OPT_Wc___compat, "request for implicit conversion from "
4664 "%qT to %qT not permitted in C++", rhstype, type);
4666 /* Check if the right-hand side has a format attribute but the
4667 left-hand side doesn't. */
4668 if (warn_missing_format_attribute
4669 && check_missing_format_attribute (type, rhstype))
4671 switch (errtype)
4673 case ic_argpass:
4674 warning (OPT_Wmissing_format_attribute,
4675 "argument %d of %qE might be "
4676 "a candidate for a format attribute",
4677 parmnum, rname);
4678 break;
4679 case ic_assign:
4680 warning (OPT_Wmissing_format_attribute,
4681 "assignment left-hand side might be "
4682 "a candidate for a format attribute");
4683 break;
4684 case ic_init:
4685 warning (OPT_Wmissing_format_attribute,
4686 "initialization left-hand side might be "
4687 "a candidate for a format attribute");
4688 break;
4689 case ic_return:
4690 warning (OPT_Wmissing_format_attribute,
4691 "return type might be "
4692 "a candidate for a format attribute");
4693 break;
4694 default:
4695 gcc_unreachable ();
4699 /* Any non-function converts to a [const][volatile] void *
4700 and vice versa; otherwise, targets must be the same.
4701 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4702 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4703 || (target_cmp = comp_target_types (type, rhstype))
4704 || is_opaque_pointer
4705 || (c_common_unsigned_type (mvl)
4706 == c_common_unsigned_type (mvr)))
4708 if (pedantic
4709 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4711 (VOID_TYPE_P (ttr)
4712 && !null_pointer_constant
4713 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4714 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4715 G_("ISO C forbids passing argument %d of "
4716 "%qE between function pointer "
4717 "and %<void *%>"),
4718 G_("ISO C forbids assignment between "
4719 "function pointer and %<void *%>"),
4720 G_("ISO C forbids initialization between "
4721 "function pointer and %<void *%>"),
4722 G_("ISO C forbids return between function "
4723 "pointer and %<void *%>"));
4724 /* Const and volatile mean something different for function types,
4725 so the usual warnings are not appropriate. */
4726 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4727 && TREE_CODE (ttl) != FUNCTION_TYPE)
4729 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4731 /* Types differing only by the presence of the 'volatile'
4732 qualifier are acceptable if the 'volatile' has been added
4733 in by the Objective-C EH machinery. */
4734 if (!objc_type_quals_match (ttl, ttr))
4735 WARN_FOR_ASSIGNMENT (input_location, 0,
4736 G_("passing argument %d of %qE discards "
4737 "qualifiers from pointer target type"),
4738 G_("assignment discards qualifiers "
4739 "from pointer target type"),
4740 G_("initialization discards qualifiers "
4741 "from pointer target type"),
4742 G_("return discards qualifiers from "
4743 "pointer target type"));
4745 /* If this is not a case of ignoring a mismatch in signedness,
4746 no warning. */
4747 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4748 || target_cmp)
4750 /* If there is a mismatch, do warn. */
4751 else if (warn_pointer_sign)
4752 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4753 G_("pointer targets in passing argument "
4754 "%d of %qE differ in signedness"),
4755 G_("pointer targets in assignment "
4756 "differ in signedness"),
4757 G_("pointer targets in initialization "
4758 "differ in signedness"),
4759 G_("pointer targets in return differ "
4760 "in signedness"));
4762 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4763 && TREE_CODE (ttr) == FUNCTION_TYPE)
4765 /* Because const and volatile on functions are restrictions
4766 that say the function will not do certain things,
4767 it is okay to use a const or volatile function
4768 where an ordinary one is wanted, but not vice-versa. */
4769 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4770 WARN_FOR_ASSIGNMENT (input_location, 0,
4771 G_("passing argument %d of %qE makes "
4772 "qualified function pointer "
4773 "from unqualified"),
4774 G_("assignment makes qualified function "
4775 "pointer from unqualified"),
4776 G_("initialization makes qualified "
4777 "function pointer from unqualified"),
4778 G_("return makes qualified function "
4779 "pointer from unqualified"));
4782 else
4783 /* Avoid warning about the volatile ObjC EH puts on decls. */
4784 if (!objc_ok)
4785 WARN_FOR_ASSIGNMENT (input_location, 0,
4786 G_("passing argument %d of %qE from "
4787 "incompatible pointer type"),
4788 G_("assignment from incompatible pointer type"),
4789 G_("initialization from incompatible "
4790 "pointer type"),
4791 G_("return from incompatible pointer type"));
4793 return convert (type, rhs);
4795 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4797 /* ??? This should not be an error when inlining calls to
4798 unprototyped functions. */
4799 error ("invalid use of non-lvalue array");
4800 return error_mark_node;
4802 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4804 /* An explicit constant 0 can convert to a pointer,
4805 or one that results from arithmetic, even including
4806 a cast to integer type. */
4807 if (!null_pointer_constant)
4808 WARN_FOR_ASSIGNMENT (input_location, 0,
4809 G_("passing argument %d of %qE makes "
4810 "pointer from integer without a cast"),
4811 G_("assignment makes pointer from integer "
4812 "without a cast"),
4813 G_("initialization makes pointer from "
4814 "integer without a cast"),
4815 G_("return makes pointer from integer "
4816 "without a cast"));
4818 return convert (type, rhs);
4820 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4822 WARN_FOR_ASSIGNMENT (input_location, 0,
4823 G_("passing argument %d of %qE makes integer "
4824 "from pointer without a cast"),
4825 G_("assignment makes integer from pointer "
4826 "without a cast"),
4827 G_("initialization makes integer from pointer "
4828 "without a cast"),
4829 G_("return makes integer from pointer "
4830 "without a cast"));
4831 return convert (type, rhs);
4833 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4835 tree ret;
4836 bool save = in_late_binary_op;
4837 in_late_binary_op = true;
4838 ret = convert (type, rhs);
4839 in_late_binary_op = save;
4840 return ret;
4843 switch (errtype)
4845 case ic_argpass:
4846 error ("incompatible type for argument %d of %qE", parmnum, rname);
4847 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
4848 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
4849 "expected %qT but argument is of type %qT", type, rhstype);
4850 break;
4851 case ic_assign:
4852 error ("incompatible types when assigning to type %qT from type %qT",
4853 type, rhstype);
4854 break;
4855 case ic_init:
4856 error ("incompatible types when initializing type %qT using type %qT",
4857 type, rhstype);
4858 break;
4859 case ic_return:
4860 error ("incompatible types when returning type %qT but %qT was expected",
4861 rhstype, type);
4862 break;
4863 default:
4864 gcc_unreachable ();
4867 return error_mark_node;
4870 /* If VALUE is a compound expr all of whose expressions are constant, then
4871 return its value. Otherwise, return error_mark_node.
4873 This is for handling COMPOUND_EXPRs as initializer elements
4874 which is allowed with a warning when -pedantic is specified. */
4876 static tree
4877 valid_compound_expr_initializer (tree value, tree endtype)
4879 if (TREE_CODE (value) == COMPOUND_EXPR)
4881 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4882 == error_mark_node)
4883 return error_mark_node;
4884 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4885 endtype);
4887 else if (!initializer_constant_valid_p (value, endtype))
4888 return error_mark_node;
4889 else
4890 return value;
4893 /* Perform appropriate conversions on the initial value of a variable,
4894 store it in the declaration DECL,
4895 and print any error messages that are appropriate.
4896 If the init is invalid, store an ERROR_MARK. */
4898 void
4899 store_init_value (tree decl, tree init)
4901 tree value, type;
4902 bool npc = false;
4904 /* If variable's type was invalidly declared, just ignore it. */
4906 type = TREE_TYPE (decl);
4907 if (TREE_CODE (type) == ERROR_MARK)
4908 return;
4910 /* Digest the specified initializer into an expression. */
4912 if (init)
4913 npc = null_pointer_constant_p (init);
4914 value = digest_init (type, init, npc, true, TREE_STATIC (decl));
4916 /* Store the expression if valid; else report error. */
4918 if (!in_system_header
4919 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4920 warning (OPT_Wtraditional, "traditional C rejects automatic "
4921 "aggregate initialization");
4923 DECL_INITIAL (decl) = value;
4925 /* ANSI wants warnings about out-of-range constant initializers. */
4926 STRIP_TYPE_NOPS (value);
4927 if (TREE_STATIC (decl))
4928 constant_expression_warning (value);
4930 /* Check if we need to set array size from compound literal size. */
4931 if (TREE_CODE (type) == ARRAY_TYPE
4932 && TYPE_DOMAIN (type) == 0
4933 && value != error_mark_node)
4935 tree inside_init = init;
4937 STRIP_TYPE_NOPS (inside_init);
4938 inside_init = fold (inside_init);
4940 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4942 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4944 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4946 /* For int foo[] = (int [3]){1}; we need to set array size
4947 now since later on array initializer will be just the
4948 brace enclosed list of the compound literal. */
4949 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4950 TREE_TYPE (decl) = type;
4951 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4952 layout_type (type);
4953 layout_decl (cldecl, 0);
4959 /* Methods for storing and printing names for error messages. */
4961 /* Implement a spelling stack that allows components of a name to be pushed
4962 and popped. Each element on the stack is this structure. */
4964 struct spelling
4966 int kind;
4967 union
4969 unsigned HOST_WIDE_INT i;
4970 const char *s;
4971 } u;
4974 #define SPELLING_STRING 1
4975 #define SPELLING_MEMBER 2
4976 #define SPELLING_BOUNDS 3
4978 static struct spelling *spelling; /* Next stack element (unused). */
4979 static struct spelling *spelling_base; /* Spelling stack base. */
4980 static int spelling_size; /* Size of the spelling stack. */
4982 /* Macros to save and restore the spelling stack around push_... functions.
4983 Alternative to SAVE_SPELLING_STACK. */
4985 #define SPELLING_DEPTH() (spelling - spelling_base)
4986 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4988 /* Push an element on the spelling stack with type KIND and assign VALUE
4989 to MEMBER. */
4991 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4993 int depth = SPELLING_DEPTH (); \
4995 if (depth >= spelling_size) \
4997 spelling_size += 10; \
4998 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4999 spelling_size); \
5000 RESTORE_SPELLING_DEPTH (depth); \
5003 spelling->kind = (KIND); \
5004 spelling->MEMBER = (VALUE); \
5005 spelling++; \
5008 /* Push STRING on the stack. Printed literally. */
5010 static void
5011 push_string (const char *string)
5013 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5016 /* Push a member name on the stack. Printed as '.' STRING. */
5018 static void
5019 push_member_name (tree decl)
5021 const char *const string
5022 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
5023 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5026 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5028 static void
5029 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5031 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5034 /* Compute the maximum size in bytes of the printed spelling. */
5036 static int
5037 spelling_length (void)
5039 int size = 0;
5040 struct spelling *p;
5042 for (p = spelling_base; p < spelling; p++)
5044 if (p->kind == SPELLING_BOUNDS)
5045 size += 25;
5046 else
5047 size += strlen (p->u.s) + 1;
5050 return size;
5053 /* Print the spelling to BUFFER and return it. */
5055 static char *
5056 print_spelling (char *buffer)
5058 char *d = buffer;
5059 struct spelling *p;
5061 for (p = spelling_base; p < spelling; p++)
5062 if (p->kind == SPELLING_BOUNDS)
5064 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5065 d += strlen (d);
5067 else
5069 const char *s;
5070 if (p->kind == SPELLING_MEMBER)
5071 *d++ = '.';
5072 for (s = p->u.s; (*d = *s++); d++)
5075 *d++ = '\0';
5076 return buffer;
5079 /* Issue an error message for a bad initializer component.
5080 MSGID identifies the message.
5081 The component name is taken from the spelling stack. */
5083 void
5084 error_init (const char *msgid)
5086 char *ofwhat;
5088 error ("%s", _(msgid));
5089 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5090 if (*ofwhat)
5091 error ("(near initialization for %qs)", ofwhat);
5094 /* Issue a pedantic warning for a bad initializer component. OPT is
5095 the option OPT_* (from options.h) controlling this warning or 0 if
5096 it is unconditionally given. MSGID identifies the message. The
5097 component name is taken from the spelling stack. */
5099 void
5100 pedwarn_init (location_t location, int opt, const char *msgid)
5102 char *ofwhat;
5104 pedwarn (location, opt, "%s", _(msgid));
5105 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5106 if (*ofwhat)
5107 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5110 /* Issue a warning for a bad initializer component.
5112 OPT is the OPT_W* value corresponding to the warning option that
5113 controls this warning. MSGID identifies the message. The
5114 component name is taken from the spelling stack. */
5116 static void
5117 warning_init (int opt, const char *msgid)
5119 char *ofwhat;
5121 warning (opt, "%s", _(msgid));
5122 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5123 if (*ofwhat)
5124 warning (opt, "(near initialization for %qs)", ofwhat);
5127 /* If TYPE is an array type and EXPR is a parenthesized string
5128 constant, warn if pedantic that EXPR is being used to initialize an
5129 object of type TYPE. */
5131 void
5132 maybe_warn_string_init (tree type, struct c_expr expr)
5134 if (pedantic
5135 && TREE_CODE (type) == ARRAY_TYPE
5136 && TREE_CODE (expr.value) == STRING_CST
5137 && expr.original_code != STRING_CST)
5138 pedwarn_init (input_location, OPT_pedantic,
5139 "array initialized from parenthesized string constant");
5142 /* Digest the parser output INIT as an initializer for type TYPE.
5143 Return a C expression of type TYPE to represent the initial value.
5145 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5147 If INIT is a string constant, STRICT_STRING is true if it is
5148 unparenthesized or we should not warn here for it being parenthesized.
5149 For other types of INIT, STRICT_STRING is not used.
5151 REQUIRE_CONSTANT requests an error if non-constant initializers or
5152 elements are seen. */
5154 static tree
5155 digest_init (tree type, tree init, bool null_pointer_constant,
5156 bool strict_string, int require_constant)
5158 enum tree_code code = TREE_CODE (type);
5159 tree inside_init = init;
5160 tree semantic_type = NULL_TREE;
5161 bool maybe_const = true;
5163 if (type == error_mark_node
5164 || !init
5165 || init == error_mark_node
5166 || TREE_TYPE (init) == error_mark_node)
5167 return error_mark_node;
5169 STRIP_TYPE_NOPS (inside_init);
5171 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5173 semantic_type = TREE_TYPE (inside_init);
5174 inside_init = TREE_OPERAND (inside_init, 0);
5176 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5177 inside_init = decl_constant_value_for_optimization (inside_init);
5179 /* Initialization of an array of chars from a string constant
5180 optionally enclosed in braces. */
5182 if (code == ARRAY_TYPE && inside_init
5183 && TREE_CODE (inside_init) == STRING_CST)
5185 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5186 /* Note that an array could be both an array of character type
5187 and an array of wchar_t if wchar_t is signed char or unsigned
5188 char. */
5189 bool char_array = (typ1 == char_type_node
5190 || typ1 == signed_char_type_node
5191 || typ1 == unsigned_char_type_node);
5192 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5193 bool char16_array = !!comptypes (typ1, char16_type_node);
5194 bool char32_array = !!comptypes (typ1, char32_type_node);
5196 if (char_array || wchar_array || char16_array || char32_array)
5198 struct c_expr expr;
5199 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5200 expr.value = inside_init;
5201 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5202 expr.original_type = NULL;
5203 maybe_warn_string_init (type, expr);
5205 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5206 TYPE_MAIN_VARIANT (type)))
5207 return inside_init;
5209 if (char_array)
5211 if (typ2 != char_type_node)
5213 error_init ("char-array initialized from wide string");
5214 return error_mark_node;
5217 else
5219 if (typ2 == char_type_node)
5221 error_init ("wide character array initialized from non-wide "
5222 "string");
5223 return error_mark_node;
5225 else if (!comptypes(typ1, typ2))
5227 error_init ("wide character array initialized from "
5228 "incompatible wide string");
5229 return error_mark_node;
5233 TREE_TYPE (inside_init) = type;
5234 if (TYPE_DOMAIN (type) != 0
5235 && TYPE_SIZE (type) != 0
5236 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5237 /* Subtract the size of a single (possibly wide) character
5238 because it's ok to ignore the terminating null char
5239 that is counted in the length of the constant. */
5240 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5241 TREE_STRING_LENGTH (inside_init)
5242 - (TYPE_PRECISION (typ1)
5243 / BITS_PER_UNIT)))
5244 pedwarn_init (input_location, 0,
5245 "initializer-string for array of chars is too long");
5247 return inside_init;
5249 else if (INTEGRAL_TYPE_P (typ1))
5251 error_init ("array of inappropriate type initialized "
5252 "from string constant");
5253 return error_mark_node;
5257 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5258 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5259 below and handle as a constructor. */
5260 if (code == VECTOR_TYPE
5261 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5262 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5263 && TREE_CONSTANT (inside_init))
5265 if (TREE_CODE (inside_init) == VECTOR_CST
5266 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5267 TYPE_MAIN_VARIANT (type)))
5268 return inside_init;
5270 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5272 unsigned HOST_WIDE_INT ix;
5273 tree value;
5274 bool constant_p = true;
5276 /* Iterate through elements and check if all constructor
5277 elements are *_CSTs. */
5278 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5279 if (!CONSTANT_CLASS_P (value))
5281 constant_p = false;
5282 break;
5285 if (constant_p)
5286 return build_vector_from_ctor (type,
5287 CONSTRUCTOR_ELTS (inside_init));
5291 if (warn_sequence_point)
5292 verify_sequence_points (inside_init);
5294 /* Any type can be initialized
5295 from an expression of the same type, optionally with braces. */
5297 if (inside_init && TREE_TYPE (inside_init) != 0
5298 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5299 TYPE_MAIN_VARIANT (type))
5300 || (code == ARRAY_TYPE
5301 && comptypes (TREE_TYPE (inside_init), type))
5302 || (code == VECTOR_TYPE
5303 && comptypes (TREE_TYPE (inside_init), type))
5304 || (code == POINTER_TYPE
5305 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5306 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5307 TREE_TYPE (type)))))
5309 if (code == POINTER_TYPE)
5311 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5313 if (TREE_CODE (inside_init) == STRING_CST
5314 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5315 inside_init = array_to_pointer_conversion (inside_init);
5316 else
5318 error_init ("invalid use of non-lvalue array");
5319 return error_mark_node;
5324 if (code == VECTOR_TYPE)
5325 /* Although the types are compatible, we may require a
5326 conversion. */
5327 inside_init = convert (type, inside_init);
5329 if (require_constant
5330 && (code == VECTOR_TYPE || !flag_isoc99)
5331 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5333 /* As an extension, allow initializing objects with static storage
5334 duration with compound literals (which are then treated just as
5335 the brace enclosed list they contain). Also allow this for
5336 vectors, as we can only assign them with compound literals. */
5337 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5338 inside_init = DECL_INITIAL (decl);
5341 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5342 && TREE_CODE (inside_init) != CONSTRUCTOR)
5344 error_init ("array initialized from non-constant array expression");
5345 return error_mark_node;
5348 /* Compound expressions can only occur here if -pedantic or
5349 -pedantic-errors is specified. In the later case, we always want
5350 an error. In the former case, we simply want a warning. */
5351 if (require_constant && pedantic
5352 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5354 inside_init
5355 = valid_compound_expr_initializer (inside_init,
5356 TREE_TYPE (inside_init));
5357 if (inside_init == error_mark_node)
5358 error_init ("initializer element is not constant");
5359 else
5360 pedwarn_init (input_location, OPT_pedantic,
5361 "initializer element is not constant");
5362 if (flag_pedantic_errors)
5363 inside_init = error_mark_node;
5365 else if (require_constant
5366 && !initializer_constant_valid_p (inside_init,
5367 TREE_TYPE (inside_init)))
5369 error_init ("initializer element is not constant");
5370 inside_init = error_mark_node;
5372 else if (require_constant && !maybe_const)
5373 pedwarn_init (input_location, 0,
5374 "initializer element is not a constant expression");
5376 /* Added to enable additional -Wmissing-format-attribute warnings. */
5377 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5378 inside_init = convert_for_assignment (type, inside_init, ic_init,
5379 null_pointer_constant,
5380 NULL_TREE, NULL_TREE, 0);
5381 return inside_init;
5384 /* Handle scalar types, including conversions. */
5386 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5387 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5388 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5390 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5391 && (TREE_CODE (init) == STRING_CST
5392 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5393 inside_init = init = array_to_pointer_conversion (init);
5394 if (semantic_type)
5395 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5396 inside_init);
5397 inside_init
5398 = convert_for_assignment (type, inside_init, ic_init,
5399 null_pointer_constant,
5400 NULL_TREE, NULL_TREE, 0);
5402 /* Check to see if we have already given an error message. */
5403 if (inside_init == error_mark_node)
5405 else if (require_constant && !TREE_CONSTANT (inside_init))
5407 error_init ("initializer element is not constant");
5408 inside_init = error_mark_node;
5410 else if (require_constant
5411 && !initializer_constant_valid_p (inside_init,
5412 TREE_TYPE (inside_init)))
5414 error_init ("initializer element is not computable at load time");
5415 inside_init = error_mark_node;
5417 else if (require_constant && !maybe_const)
5418 pedwarn_init (input_location, 0,
5419 "initializer element is not a constant expression");
5421 return inside_init;
5424 /* Come here only for records and arrays. */
5426 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5428 error_init ("variable-sized object may not be initialized");
5429 return error_mark_node;
5432 error_init ("invalid initializer");
5433 return error_mark_node;
5436 /* Handle initializers that use braces. */
5438 /* Type of object we are accumulating a constructor for.
5439 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5440 static tree constructor_type;
5442 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5443 left to fill. */
5444 static tree constructor_fields;
5446 /* For an ARRAY_TYPE, this is the specified index
5447 at which to store the next element we get. */
5448 static tree constructor_index;
5450 /* For an ARRAY_TYPE, this is the maximum index. */
5451 static tree constructor_max_index;
5453 /* For a RECORD_TYPE, this is the first field not yet written out. */
5454 static tree constructor_unfilled_fields;
5456 /* For an ARRAY_TYPE, this is the index of the first element
5457 not yet written out. */
5458 static tree constructor_unfilled_index;
5460 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5461 This is so we can generate gaps between fields, when appropriate. */
5462 static tree constructor_bit_index;
5464 /* If we are saving up the elements rather than allocating them,
5465 this is the list of elements so far (in reverse order,
5466 most recent first). */
5467 static VEC(constructor_elt,gc) *constructor_elements;
5469 /* 1 if constructor should be incrementally stored into a constructor chain,
5470 0 if all the elements should be kept in AVL tree. */
5471 static int constructor_incremental;
5473 /* 1 if so far this constructor's elements are all compile-time constants. */
5474 static int constructor_constant;
5476 /* 1 if so far this constructor's elements are all valid address constants. */
5477 static int constructor_simple;
5479 /* 1 if this constructor has an element that cannot be part of a
5480 constant expression. */
5481 static int constructor_nonconst;
5483 /* 1 if this constructor is erroneous so far. */
5484 static int constructor_erroneous;
5486 /* Structure for managing pending initializer elements, organized as an
5487 AVL tree. */
5489 struct init_node
5491 struct init_node *left, *right;
5492 struct init_node *parent;
5493 int balance;
5494 tree purpose;
5495 tree value;
5498 /* Tree of pending elements at this constructor level.
5499 These are elements encountered out of order
5500 which belong at places we haven't reached yet in actually
5501 writing the output.
5502 Will never hold tree nodes across GC runs. */
5503 static struct init_node *constructor_pending_elts;
5505 /* The SPELLING_DEPTH of this constructor. */
5506 static int constructor_depth;
5508 /* DECL node for which an initializer is being read.
5509 0 means we are reading a constructor expression
5510 such as (struct foo) {...}. */
5511 static tree constructor_decl;
5513 /* Nonzero if this is an initializer for a top-level decl. */
5514 static int constructor_top_level;
5516 /* Nonzero if there were any member designators in this initializer. */
5517 static int constructor_designated;
5519 /* Nesting depth of designator list. */
5520 static int designator_depth;
5522 /* Nonzero if there were diagnosed errors in this designator list. */
5523 static int designator_erroneous;
5526 /* This stack has a level for each implicit or explicit level of
5527 structuring in the initializer, including the outermost one. It
5528 saves the values of most of the variables above. */
5530 struct constructor_range_stack;
5532 struct constructor_stack
5534 struct constructor_stack *next;
5535 tree type;
5536 tree fields;
5537 tree index;
5538 tree max_index;
5539 tree unfilled_index;
5540 tree unfilled_fields;
5541 tree bit_index;
5542 VEC(constructor_elt,gc) *elements;
5543 struct init_node *pending_elts;
5544 int offset;
5545 int depth;
5546 /* If value nonzero, this value should replace the entire
5547 constructor at this level. */
5548 struct c_expr replacement_value;
5549 struct constructor_range_stack *range_stack;
5550 char constant;
5551 char simple;
5552 char nonconst;
5553 char implicit;
5554 char erroneous;
5555 char outer;
5556 char incremental;
5557 char designated;
5560 static struct constructor_stack *constructor_stack;
5562 /* This stack represents designators from some range designator up to
5563 the last designator in the list. */
5565 struct constructor_range_stack
5567 struct constructor_range_stack *next, *prev;
5568 struct constructor_stack *stack;
5569 tree range_start;
5570 tree index;
5571 tree range_end;
5572 tree fields;
5575 static struct constructor_range_stack *constructor_range_stack;
5577 /* This stack records separate initializers that are nested.
5578 Nested initializers can't happen in ANSI C, but GNU C allows them
5579 in cases like { ... (struct foo) { ... } ... }. */
5581 struct initializer_stack
5583 struct initializer_stack *next;
5584 tree decl;
5585 struct constructor_stack *constructor_stack;
5586 struct constructor_range_stack *constructor_range_stack;
5587 VEC(constructor_elt,gc) *elements;
5588 struct spelling *spelling;
5589 struct spelling *spelling_base;
5590 int spelling_size;
5591 char top_level;
5592 char require_constant_value;
5593 char require_constant_elements;
5596 static struct initializer_stack *initializer_stack;
5598 /* Prepare to parse and output the initializer for variable DECL. */
5600 void
5601 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5603 const char *locus;
5604 struct initializer_stack *p = XNEW (struct initializer_stack);
5606 p->decl = constructor_decl;
5607 p->require_constant_value = require_constant_value;
5608 p->require_constant_elements = require_constant_elements;
5609 p->constructor_stack = constructor_stack;
5610 p->constructor_range_stack = constructor_range_stack;
5611 p->elements = constructor_elements;
5612 p->spelling = spelling;
5613 p->spelling_base = spelling_base;
5614 p->spelling_size = spelling_size;
5615 p->top_level = constructor_top_level;
5616 p->next = initializer_stack;
5617 initializer_stack = p;
5619 constructor_decl = decl;
5620 constructor_designated = 0;
5621 constructor_top_level = top_level;
5623 if (decl != 0 && decl != error_mark_node)
5625 require_constant_value = TREE_STATIC (decl);
5626 require_constant_elements
5627 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5628 /* For a scalar, you can always use any value to initialize,
5629 even within braces. */
5630 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5631 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5632 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5633 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5634 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5636 else
5638 require_constant_value = 0;
5639 require_constant_elements = 0;
5640 locus = "(anonymous)";
5643 constructor_stack = 0;
5644 constructor_range_stack = 0;
5646 missing_braces_mentioned = 0;
5648 spelling_base = 0;
5649 spelling_size = 0;
5650 RESTORE_SPELLING_DEPTH (0);
5652 if (locus)
5653 push_string (locus);
5656 void
5657 finish_init (void)
5659 struct initializer_stack *p = initializer_stack;
5661 /* Free the whole constructor stack of this initializer. */
5662 while (constructor_stack)
5664 struct constructor_stack *q = constructor_stack;
5665 constructor_stack = q->next;
5666 free (q);
5669 gcc_assert (!constructor_range_stack);
5671 /* Pop back to the data of the outer initializer (if any). */
5672 free (spelling_base);
5674 constructor_decl = p->decl;
5675 require_constant_value = p->require_constant_value;
5676 require_constant_elements = p->require_constant_elements;
5677 constructor_stack = p->constructor_stack;
5678 constructor_range_stack = p->constructor_range_stack;
5679 constructor_elements = p->elements;
5680 spelling = p->spelling;
5681 spelling_base = p->spelling_base;
5682 spelling_size = p->spelling_size;
5683 constructor_top_level = p->top_level;
5684 initializer_stack = p->next;
5685 free (p);
5688 /* Call here when we see the initializer is surrounded by braces.
5689 This is instead of a call to push_init_level;
5690 it is matched by a call to pop_init_level.
5692 TYPE is the type to initialize, for a constructor expression.
5693 For an initializer for a decl, TYPE is zero. */
5695 void
5696 really_start_incremental_init (tree type)
5698 struct constructor_stack *p = XNEW (struct constructor_stack);
5700 if (type == 0)
5701 type = TREE_TYPE (constructor_decl);
5703 if (TREE_CODE (type) == VECTOR_TYPE
5704 && TYPE_VECTOR_OPAQUE (type))
5705 error ("opaque vector types cannot be initialized");
5707 p->type = constructor_type;
5708 p->fields = constructor_fields;
5709 p->index = constructor_index;
5710 p->max_index = constructor_max_index;
5711 p->unfilled_index = constructor_unfilled_index;
5712 p->unfilled_fields = constructor_unfilled_fields;
5713 p->bit_index = constructor_bit_index;
5714 p->elements = constructor_elements;
5715 p->constant = constructor_constant;
5716 p->simple = constructor_simple;
5717 p->nonconst = constructor_nonconst;
5718 p->erroneous = constructor_erroneous;
5719 p->pending_elts = constructor_pending_elts;
5720 p->depth = constructor_depth;
5721 p->replacement_value.value = 0;
5722 p->replacement_value.original_code = ERROR_MARK;
5723 p->replacement_value.original_type = NULL;
5724 p->implicit = 0;
5725 p->range_stack = 0;
5726 p->outer = 0;
5727 p->incremental = constructor_incremental;
5728 p->designated = constructor_designated;
5729 p->next = 0;
5730 constructor_stack = p;
5732 constructor_constant = 1;
5733 constructor_simple = 1;
5734 constructor_nonconst = 0;
5735 constructor_depth = SPELLING_DEPTH ();
5736 constructor_elements = 0;
5737 constructor_pending_elts = 0;
5738 constructor_type = type;
5739 constructor_incremental = 1;
5740 constructor_designated = 0;
5741 designator_depth = 0;
5742 designator_erroneous = 0;
5744 if (TREE_CODE (constructor_type) == RECORD_TYPE
5745 || TREE_CODE (constructor_type) == UNION_TYPE)
5747 constructor_fields = TYPE_FIELDS (constructor_type);
5748 /* Skip any nameless bit fields at the beginning. */
5749 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5750 && DECL_NAME (constructor_fields) == 0)
5751 constructor_fields = TREE_CHAIN (constructor_fields);
5753 constructor_unfilled_fields = constructor_fields;
5754 constructor_bit_index = bitsize_zero_node;
5756 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5758 if (TYPE_DOMAIN (constructor_type))
5760 constructor_max_index
5761 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5763 /* Detect non-empty initializations of zero-length arrays. */
5764 if (constructor_max_index == NULL_TREE
5765 && TYPE_SIZE (constructor_type))
5766 constructor_max_index = build_int_cst (NULL_TREE, -1);
5768 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5769 to initialize VLAs will cause a proper error; avoid tree
5770 checking errors as well by setting a safe value. */
5771 if (constructor_max_index
5772 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5773 constructor_max_index = build_int_cst (NULL_TREE, -1);
5775 constructor_index
5776 = convert (bitsizetype,
5777 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5779 else
5781 constructor_index = bitsize_zero_node;
5782 constructor_max_index = NULL_TREE;
5785 constructor_unfilled_index = constructor_index;
5787 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5789 /* Vectors are like simple fixed-size arrays. */
5790 constructor_max_index =
5791 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5792 constructor_index = bitsize_zero_node;
5793 constructor_unfilled_index = constructor_index;
5795 else
5797 /* Handle the case of int x = {5}; */
5798 constructor_fields = constructor_type;
5799 constructor_unfilled_fields = constructor_type;
5803 /* Push down into a subobject, for initialization.
5804 If this is for an explicit set of braces, IMPLICIT is 0.
5805 If it is because the next element belongs at a lower level,
5806 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5808 void
5809 push_init_level (int implicit)
5811 struct constructor_stack *p;
5812 tree value = NULL_TREE;
5814 /* If we've exhausted any levels that didn't have braces,
5815 pop them now. If implicit == 1, this will have been done in
5816 process_init_element; do not repeat it here because in the case
5817 of excess initializers for an empty aggregate this leads to an
5818 infinite cycle of popping a level and immediately recreating
5819 it. */
5820 if (implicit != 1)
5822 while (constructor_stack->implicit)
5824 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5825 || TREE_CODE (constructor_type) == UNION_TYPE)
5826 && constructor_fields == 0)
5827 process_init_element (pop_init_level (1), true);
5828 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5829 && constructor_max_index
5830 && tree_int_cst_lt (constructor_max_index,
5831 constructor_index))
5832 process_init_element (pop_init_level (1), true);
5833 else
5834 break;
5838 /* Unless this is an explicit brace, we need to preserve previous
5839 content if any. */
5840 if (implicit)
5842 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5843 || TREE_CODE (constructor_type) == UNION_TYPE)
5844 && constructor_fields)
5845 value = find_init_member (constructor_fields);
5846 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5847 value = find_init_member (constructor_index);
5850 p = XNEW (struct constructor_stack);
5851 p->type = constructor_type;
5852 p->fields = constructor_fields;
5853 p->index = constructor_index;
5854 p->max_index = constructor_max_index;
5855 p->unfilled_index = constructor_unfilled_index;
5856 p->unfilled_fields = constructor_unfilled_fields;
5857 p->bit_index = constructor_bit_index;
5858 p->elements = constructor_elements;
5859 p->constant = constructor_constant;
5860 p->simple = constructor_simple;
5861 p->nonconst = constructor_nonconst;
5862 p->erroneous = constructor_erroneous;
5863 p->pending_elts = constructor_pending_elts;
5864 p->depth = constructor_depth;
5865 p->replacement_value.value = 0;
5866 p->replacement_value.original_code = ERROR_MARK;
5867 p->replacement_value.original_type = NULL;
5868 p->implicit = implicit;
5869 p->outer = 0;
5870 p->incremental = constructor_incremental;
5871 p->designated = constructor_designated;
5872 p->next = constructor_stack;
5873 p->range_stack = 0;
5874 constructor_stack = p;
5876 constructor_constant = 1;
5877 constructor_simple = 1;
5878 constructor_nonconst = 0;
5879 constructor_depth = SPELLING_DEPTH ();
5880 constructor_elements = 0;
5881 constructor_incremental = 1;
5882 constructor_designated = 0;
5883 constructor_pending_elts = 0;
5884 if (!implicit)
5886 p->range_stack = constructor_range_stack;
5887 constructor_range_stack = 0;
5888 designator_depth = 0;
5889 designator_erroneous = 0;
5892 /* Don't die if an entire brace-pair level is superfluous
5893 in the containing level. */
5894 if (constructor_type == 0)
5896 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5897 || TREE_CODE (constructor_type) == UNION_TYPE)
5899 /* Don't die if there are extra init elts at the end. */
5900 if (constructor_fields == 0)
5901 constructor_type = 0;
5902 else
5904 constructor_type = TREE_TYPE (constructor_fields);
5905 push_member_name (constructor_fields);
5906 constructor_depth++;
5909 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5911 constructor_type = TREE_TYPE (constructor_type);
5912 push_array_bounds (tree_low_cst (constructor_index, 1));
5913 constructor_depth++;
5916 if (constructor_type == 0)
5918 error_init ("extra brace group at end of initializer");
5919 constructor_fields = 0;
5920 constructor_unfilled_fields = 0;
5921 return;
5924 if (value && TREE_CODE (value) == CONSTRUCTOR)
5926 constructor_constant = TREE_CONSTANT (value);
5927 constructor_simple = TREE_STATIC (value);
5928 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
5929 constructor_elements = CONSTRUCTOR_ELTS (value);
5930 if (!VEC_empty (constructor_elt, constructor_elements)
5931 && (TREE_CODE (constructor_type) == RECORD_TYPE
5932 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5933 set_nonincremental_init ();
5936 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5938 missing_braces_mentioned = 1;
5939 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
5942 if (TREE_CODE (constructor_type) == RECORD_TYPE
5943 || TREE_CODE (constructor_type) == UNION_TYPE)
5945 constructor_fields = TYPE_FIELDS (constructor_type);
5946 /* Skip any nameless bit fields at the beginning. */
5947 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5948 && DECL_NAME (constructor_fields) == 0)
5949 constructor_fields = TREE_CHAIN (constructor_fields);
5951 constructor_unfilled_fields = constructor_fields;
5952 constructor_bit_index = bitsize_zero_node;
5954 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5956 /* Vectors are like simple fixed-size arrays. */
5957 constructor_max_index =
5958 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5959 constructor_index = convert (bitsizetype, integer_zero_node);
5960 constructor_unfilled_index = constructor_index;
5962 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5964 if (TYPE_DOMAIN (constructor_type))
5966 constructor_max_index
5967 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5969 /* Detect non-empty initializations of zero-length arrays. */
5970 if (constructor_max_index == NULL_TREE
5971 && TYPE_SIZE (constructor_type))
5972 constructor_max_index = build_int_cst (NULL_TREE, -1);
5974 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5975 to initialize VLAs will cause a proper error; avoid tree
5976 checking errors as well by setting a safe value. */
5977 if (constructor_max_index
5978 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5979 constructor_max_index = build_int_cst (NULL_TREE, -1);
5981 constructor_index
5982 = convert (bitsizetype,
5983 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5985 else
5986 constructor_index = bitsize_zero_node;
5988 constructor_unfilled_index = constructor_index;
5989 if (value && TREE_CODE (value) == STRING_CST)
5991 /* We need to split the char/wchar array into individual
5992 characters, so that we don't have to special case it
5993 everywhere. */
5994 set_nonincremental_init_from_string (value);
5997 else
5999 if (constructor_type != error_mark_node)
6000 warning_init (0, "braces around scalar initializer");
6001 constructor_fields = constructor_type;
6002 constructor_unfilled_fields = constructor_type;
6006 /* At the end of an implicit or explicit brace level,
6007 finish up that level of constructor. If a single expression
6008 with redundant braces initialized that level, return the
6009 c_expr structure for that expression. Otherwise, the original_code
6010 element is set to ERROR_MARK.
6011 If we were outputting the elements as they are read, return 0 as the value
6012 from inner levels (process_init_element ignores that),
6013 but return error_mark_node as the value from the outermost level
6014 (that's what we want to put in DECL_INITIAL).
6015 Otherwise, return a CONSTRUCTOR expression as the value. */
6017 struct c_expr
6018 pop_init_level (int implicit)
6020 struct constructor_stack *p;
6021 struct c_expr ret;
6022 ret.value = 0;
6023 ret.original_code = ERROR_MARK;
6024 ret.original_type = NULL;
6026 if (implicit == 0)
6028 /* When we come to an explicit close brace,
6029 pop any inner levels that didn't have explicit braces. */
6030 while (constructor_stack->implicit)
6031 process_init_element (pop_init_level (1), true);
6033 gcc_assert (!constructor_range_stack);
6036 /* Now output all pending elements. */
6037 constructor_incremental = 1;
6038 output_pending_init_elements (1);
6040 p = constructor_stack;
6042 /* Error for initializing a flexible array member, or a zero-length
6043 array member in an inappropriate context. */
6044 if (constructor_type && constructor_fields
6045 && TREE_CODE (constructor_type) == ARRAY_TYPE
6046 && TYPE_DOMAIN (constructor_type)
6047 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6049 /* Silently discard empty initializations. The parser will
6050 already have pedwarned for empty brackets. */
6051 if (integer_zerop (constructor_unfilled_index))
6052 constructor_type = NULL_TREE;
6053 else
6055 gcc_assert (!TYPE_SIZE (constructor_type));
6057 if (constructor_depth > 2)
6058 error_init ("initialization of flexible array member in a nested context");
6059 else
6060 pedwarn_init (input_location, OPT_pedantic,
6061 "initialization of a flexible array member");
6063 /* We have already issued an error message for the existence
6064 of a flexible array member not at the end of the structure.
6065 Discard the initializer so that we do not die later. */
6066 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6067 constructor_type = NULL_TREE;
6071 /* Warn when some struct elements are implicitly initialized to zero. */
6072 if (warn_missing_field_initializers
6073 && constructor_type
6074 && TREE_CODE (constructor_type) == RECORD_TYPE
6075 && constructor_unfilled_fields)
6077 /* Do not warn for flexible array members or zero-length arrays. */
6078 while (constructor_unfilled_fields
6079 && (!DECL_SIZE (constructor_unfilled_fields)
6080 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6081 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6083 /* Do not warn if this level of the initializer uses member
6084 designators; it is likely to be deliberate. */
6085 if (constructor_unfilled_fields && !constructor_designated)
6087 push_member_name (constructor_unfilled_fields);
6088 warning_init (OPT_Wmissing_field_initializers,
6089 "missing initializer");
6090 RESTORE_SPELLING_DEPTH (constructor_depth);
6094 /* Pad out the end of the structure. */
6095 if (p->replacement_value.value)
6096 /* If this closes a superfluous brace pair,
6097 just pass out the element between them. */
6098 ret = p->replacement_value;
6099 else if (constructor_type == 0)
6101 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6102 && TREE_CODE (constructor_type) != UNION_TYPE
6103 && TREE_CODE (constructor_type) != ARRAY_TYPE
6104 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6106 /* A nonincremental scalar initializer--just return
6107 the element, after verifying there is just one. */
6108 if (VEC_empty (constructor_elt,constructor_elements))
6110 if (!constructor_erroneous)
6111 error_init ("empty scalar initializer");
6112 ret.value = error_mark_node;
6114 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6116 error_init ("extra elements in scalar initializer");
6117 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6119 else
6120 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6122 else
6124 if (constructor_erroneous)
6125 ret.value = error_mark_node;
6126 else
6128 ret.value = build_constructor (constructor_type,
6129 constructor_elements);
6130 if (constructor_constant)
6131 TREE_CONSTANT (ret.value) = 1;
6132 if (constructor_constant && constructor_simple)
6133 TREE_STATIC (ret.value) = 1;
6134 if (constructor_nonconst)
6135 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6139 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6141 if (constructor_nonconst)
6142 ret.original_code = C_MAYBE_CONST_EXPR;
6143 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6144 ret.original_code = ERROR_MARK;
6147 constructor_type = p->type;
6148 constructor_fields = p->fields;
6149 constructor_index = p->index;
6150 constructor_max_index = p->max_index;
6151 constructor_unfilled_index = p->unfilled_index;
6152 constructor_unfilled_fields = p->unfilled_fields;
6153 constructor_bit_index = p->bit_index;
6154 constructor_elements = p->elements;
6155 constructor_constant = p->constant;
6156 constructor_simple = p->simple;
6157 constructor_nonconst = p->nonconst;
6158 constructor_erroneous = p->erroneous;
6159 constructor_incremental = p->incremental;
6160 constructor_designated = p->designated;
6161 constructor_pending_elts = p->pending_elts;
6162 constructor_depth = p->depth;
6163 if (!p->implicit)
6164 constructor_range_stack = p->range_stack;
6165 RESTORE_SPELLING_DEPTH (constructor_depth);
6167 constructor_stack = p->next;
6168 free (p);
6170 if (ret.value == 0 && constructor_stack == 0)
6171 ret.value = error_mark_node;
6172 return ret;
6175 /* Common handling for both array range and field name designators.
6176 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6178 static int
6179 set_designator (int array)
6181 tree subtype;
6182 enum tree_code subcode;
6184 /* Don't die if an entire brace-pair level is superfluous
6185 in the containing level. */
6186 if (constructor_type == 0)
6187 return 1;
6189 /* If there were errors in this designator list already, bail out
6190 silently. */
6191 if (designator_erroneous)
6192 return 1;
6194 if (!designator_depth)
6196 gcc_assert (!constructor_range_stack);
6198 /* Designator list starts at the level of closest explicit
6199 braces. */
6200 while (constructor_stack->implicit)
6201 process_init_element (pop_init_level (1), true);
6202 constructor_designated = 1;
6203 return 0;
6206 switch (TREE_CODE (constructor_type))
6208 case RECORD_TYPE:
6209 case UNION_TYPE:
6210 subtype = TREE_TYPE (constructor_fields);
6211 if (subtype != error_mark_node)
6212 subtype = TYPE_MAIN_VARIANT (subtype);
6213 break;
6214 case ARRAY_TYPE:
6215 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6216 break;
6217 default:
6218 gcc_unreachable ();
6221 subcode = TREE_CODE (subtype);
6222 if (array && subcode != ARRAY_TYPE)
6224 error_init ("array index in non-array initializer");
6225 return 1;
6227 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6229 error_init ("field name not in record or union initializer");
6230 return 1;
6233 constructor_designated = 1;
6234 push_init_level (2);
6235 return 0;
6238 /* If there are range designators in designator list, push a new designator
6239 to constructor_range_stack. RANGE_END is end of such stack range or
6240 NULL_TREE if there is no range designator at this level. */
6242 static void
6243 push_range_stack (tree range_end)
6245 struct constructor_range_stack *p;
6247 p = GGC_NEW (struct constructor_range_stack);
6248 p->prev = constructor_range_stack;
6249 p->next = 0;
6250 p->fields = constructor_fields;
6251 p->range_start = constructor_index;
6252 p->index = constructor_index;
6253 p->stack = constructor_stack;
6254 p->range_end = range_end;
6255 if (constructor_range_stack)
6256 constructor_range_stack->next = p;
6257 constructor_range_stack = p;
6260 /* Within an array initializer, specify the next index to be initialized.
6261 FIRST is that index. If LAST is nonzero, then initialize a range
6262 of indices, running from FIRST through LAST. */
6264 void
6265 set_init_index (tree first, tree last)
6267 if (set_designator (1))
6268 return;
6270 designator_erroneous = 1;
6272 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6273 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6275 error_init ("array index in initializer not of integer type");
6276 return;
6279 if (TREE_CODE (first) != INTEGER_CST)
6280 error_init ("nonconstant array index in initializer");
6281 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6282 error_init ("nonconstant array index in initializer");
6283 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6284 error_init ("array index in non-array initializer");
6285 else if (tree_int_cst_sgn (first) == -1)
6286 error_init ("array index in initializer exceeds array bounds");
6287 else if (constructor_max_index
6288 && tree_int_cst_lt (constructor_max_index, first))
6289 error_init ("array index in initializer exceeds array bounds");
6290 else
6292 constant_expression_warning (first);
6293 if (last)
6294 constant_expression_warning (last);
6295 constructor_index = convert (bitsizetype, first);
6297 if (last)
6299 if (tree_int_cst_equal (first, last))
6300 last = 0;
6301 else if (tree_int_cst_lt (last, first))
6303 error_init ("empty index range in initializer");
6304 last = 0;
6306 else
6308 last = convert (bitsizetype, last);
6309 if (constructor_max_index != 0
6310 && tree_int_cst_lt (constructor_max_index, last))
6312 error_init ("array index range in initializer exceeds array bounds");
6313 last = 0;
6318 designator_depth++;
6319 designator_erroneous = 0;
6320 if (constructor_range_stack || last)
6321 push_range_stack (last);
6325 /* Within a struct initializer, specify the next field to be initialized. */
6327 void
6328 set_init_label (tree fieldname)
6330 tree tail;
6332 if (set_designator (0))
6333 return;
6335 designator_erroneous = 1;
6337 if (TREE_CODE (constructor_type) != RECORD_TYPE
6338 && TREE_CODE (constructor_type) != UNION_TYPE)
6340 error_init ("field name not in record or union initializer");
6341 return;
6344 for (tail = TYPE_FIELDS (constructor_type); tail;
6345 tail = TREE_CHAIN (tail))
6347 if (DECL_NAME (tail) == fieldname)
6348 break;
6351 if (tail == 0)
6352 error ("unknown field %qE specified in initializer", fieldname);
6353 else
6355 constructor_fields = tail;
6356 designator_depth++;
6357 designator_erroneous = 0;
6358 if (constructor_range_stack)
6359 push_range_stack (NULL_TREE);
6363 /* Add a new initializer to the tree of pending initializers. PURPOSE
6364 identifies the initializer, either array index or field in a structure.
6365 VALUE is the value of that index or field.
6367 IMPLICIT is true if value comes from pop_init_level (1),
6368 the new initializer has been merged with the existing one
6369 and thus no warnings should be emitted about overriding an
6370 existing initializer. */
6372 static void
6373 add_pending_init (tree purpose, tree value, bool implicit)
6375 struct init_node *p, **q, *r;
6377 q = &constructor_pending_elts;
6378 p = 0;
6380 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6382 while (*q != 0)
6384 p = *q;
6385 if (tree_int_cst_lt (purpose, p->purpose))
6386 q = &p->left;
6387 else if (tree_int_cst_lt (p->purpose, purpose))
6388 q = &p->right;
6389 else
6391 if (!implicit)
6393 if (TREE_SIDE_EFFECTS (p->value))
6394 warning_init (0, "initialized field with side-effects overwritten");
6395 else if (warn_override_init)
6396 warning_init (OPT_Woverride_init, "initialized field overwritten");
6398 p->value = value;
6399 return;
6403 else
6405 tree bitpos;
6407 bitpos = bit_position (purpose);
6408 while (*q != NULL)
6410 p = *q;
6411 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6412 q = &p->left;
6413 else if (p->purpose != purpose)
6414 q = &p->right;
6415 else
6417 if (!implicit)
6419 if (TREE_SIDE_EFFECTS (p->value))
6420 warning_init (0, "initialized field with side-effects overwritten");
6421 else if (warn_override_init)
6422 warning_init (OPT_Woverride_init, "initialized field overwritten");
6424 p->value = value;
6425 return;
6430 r = GGC_NEW (struct init_node);
6431 r->purpose = purpose;
6432 r->value = value;
6434 *q = r;
6435 r->parent = p;
6436 r->left = 0;
6437 r->right = 0;
6438 r->balance = 0;
6440 while (p)
6442 struct init_node *s;
6444 if (r == p->left)
6446 if (p->balance == 0)
6447 p->balance = -1;
6448 else if (p->balance < 0)
6450 if (r->balance < 0)
6452 /* L rotation. */
6453 p->left = r->right;
6454 if (p->left)
6455 p->left->parent = p;
6456 r->right = p;
6458 p->balance = 0;
6459 r->balance = 0;
6461 s = p->parent;
6462 p->parent = r;
6463 r->parent = s;
6464 if (s)
6466 if (s->left == p)
6467 s->left = r;
6468 else
6469 s->right = r;
6471 else
6472 constructor_pending_elts = r;
6474 else
6476 /* LR rotation. */
6477 struct init_node *t = r->right;
6479 r->right = t->left;
6480 if (r->right)
6481 r->right->parent = r;
6482 t->left = r;
6484 p->left = t->right;
6485 if (p->left)
6486 p->left->parent = p;
6487 t->right = p;
6489 p->balance = t->balance < 0;
6490 r->balance = -(t->balance > 0);
6491 t->balance = 0;
6493 s = p->parent;
6494 p->parent = t;
6495 r->parent = t;
6496 t->parent = s;
6497 if (s)
6499 if (s->left == p)
6500 s->left = t;
6501 else
6502 s->right = t;
6504 else
6505 constructor_pending_elts = t;
6507 break;
6509 else
6511 /* p->balance == +1; growth of left side balances the node. */
6512 p->balance = 0;
6513 break;
6516 else /* r == p->right */
6518 if (p->balance == 0)
6519 /* Growth propagation from right side. */
6520 p->balance++;
6521 else if (p->balance > 0)
6523 if (r->balance > 0)
6525 /* R rotation. */
6526 p->right = r->left;
6527 if (p->right)
6528 p->right->parent = p;
6529 r->left = p;
6531 p->balance = 0;
6532 r->balance = 0;
6534 s = p->parent;
6535 p->parent = r;
6536 r->parent = s;
6537 if (s)
6539 if (s->left == p)
6540 s->left = r;
6541 else
6542 s->right = r;
6544 else
6545 constructor_pending_elts = r;
6547 else /* r->balance == -1 */
6549 /* RL rotation */
6550 struct init_node *t = r->left;
6552 r->left = t->right;
6553 if (r->left)
6554 r->left->parent = r;
6555 t->right = r;
6557 p->right = t->left;
6558 if (p->right)
6559 p->right->parent = p;
6560 t->left = p;
6562 r->balance = (t->balance < 0);
6563 p->balance = -(t->balance > 0);
6564 t->balance = 0;
6566 s = p->parent;
6567 p->parent = t;
6568 r->parent = t;
6569 t->parent = s;
6570 if (s)
6572 if (s->left == p)
6573 s->left = t;
6574 else
6575 s->right = t;
6577 else
6578 constructor_pending_elts = t;
6580 break;
6582 else
6584 /* p->balance == -1; growth of right side balances the node. */
6585 p->balance = 0;
6586 break;
6590 r = p;
6591 p = p->parent;
6595 /* Build AVL tree from a sorted chain. */
6597 static void
6598 set_nonincremental_init (void)
6600 unsigned HOST_WIDE_INT ix;
6601 tree index, value;
6603 if (TREE_CODE (constructor_type) != RECORD_TYPE
6604 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6605 return;
6607 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6608 add_pending_init (index, value, false);
6609 constructor_elements = 0;
6610 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6612 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6613 /* Skip any nameless bit fields at the beginning. */
6614 while (constructor_unfilled_fields != 0
6615 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6616 && DECL_NAME (constructor_unfilled_fields) == 0)
6617 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6620 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6622 if (TYPE_DOMAIN (constructor_type))
6623 constructor_unfilled_index
6624 = convert (bitsizetype,
6625 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6626 else
6627 constructor_unfilled_index = bitsize_zero_node;
6629 constructor_incremental = 0;
6632 /* Build AVL tree from a string constant. */
6634 static void
6635 set_nonincremental_init_from_string (tree str)
6637 tree value, purpose, type;
6638 HOST_WIDE_INT val[2];
6639 const char *p, *end;
6640 int byte, wchar_bytes, charwidth, bitpos;
6642 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6644 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6645 charwidth = TYPE_PRECISION (char_type_node);
6646 type = TREE_TYPE (constructor_type);
6647 p = TREE_STRING_POINTER (str);
6648 end = p + TREE_STRING_LENGTH (str);
6650 for (purpose = bitsize_zero_node;
6651 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6652 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6654 if (wchar_bytes == 1)
6656 val[1] = (unsigned char) *p++;
6657 val[0] = 0;
6659 else
6661 val[0] = 0;
6662 val[1] = 0;
6663 for (byte = 0; byte < wchar_bytes; byte++)
6665 if (BYTES_BIG_ENDIAN)
6666 bitpos = (wchar_bytes - byte - 1) * charwidth;
6667 else
6668 bitpos = byte * charwidth;
6669 val[bitpos < HOST_BITS_PER_WIDE_INT]
6670 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6671 << (bitpos % HOST_BITS_PER_WIDE_INT);
6675 if (!TYPE_UNSIGNED (type))
6677 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6678 if (bitpos < HOST_BITS_PER_WIDE_INT)
6680 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6682 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6683 val[0] = -1;
6686 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6688 if (val[1] < 0)
6689 val[0] = -1;
6691 else if (val[0] & (((HOST_WIDE_INT) 1)
6692 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6693 val[0] |= ((HOST_WIDE_INT) -1)
6694 << (bitpos - HOST_BITS_PER_WIDE_INT);
6697 value = build_int_cst_wide (type, val[1], val[0]);
6698 add_pending_init (purpose, value, false);
6701 constructor_incremental = 0;
6704 /* Return value of FIELD in pending initializer or zero if the field was
6705 not initialized yet. */
6707 static tree
6708 find_init_member (tree field)
6710 struct init_node *p;
6712 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6714 if (constructor_incremental
6715 && tree_int_cst_lt (field, constructor_unfilled_index))
6716 set_nonincremental_init ();
6718 p = constructor_pending_elts;
6719 while (p)
6721 if (tree_int_cst_lt (field, p->purpose))
6722 p = p->left;
6723 else if (tree_int_cst_lt (p->purpose, field))
6724 p = p->right;
6725 else
6726 return p->value;
6729 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6731 tree bitpos = bit_position (field);
6733 if (constructor_incremental
6734 && (!constructor_unfilled_fields
6735 || tree_int_cst_lt (bitpos,
6736 bit_position (constructor_unfilled_fields))))
6737 set_nonincremental_init ();
6739 p = constructor_pending_elts;
6740 while (p)
6742 if (field == p->purpose)
6743 return p->value;
6744 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6745 p = p->left;
6746 else
6747 p = p->right;
6750 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6752 if (!VEC_empty (constructor_elt, constructor_elements)
6753 && (VEC_last (constructor_elt, constructor_elements)->index
6754 == field))
6755 return VEC_last (constructor_elt, constructor_elements)->value;
6757 return 0;
6760 /* "Output" the next constructor element.
6761 At top level, really output it to assembler code now.
6762 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6763 TYPE is the data type that the containing data type wants here.
6764 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6765 If VALUE is a string constant, STRICT_STRING is true if it is
6766 unparenthesized or we should not warn here for it being parenthesized.
6767 For other types of VALUE, STRICT_STRING is not used.
6769 PENDING if non-nil means output pending elements that belong
6770 right after this element. (PENDING is normally 1;
6771 it is 0 while outputting pending elements, to avoid recursion.)
6773 IMPLICIT is true if value comes from pop_init_level (1),
6774 the new initializer has been merged with the existing one
6775 and thus no warnings should be emitted about overriding an
6776 existing initializer. */
6778 static void
6779 output_init_element (tree value, bool strict_string, tree type, tree field,
6780 int pending, bool implicit)
6782 tree semantic_type = NULL_TREE;
6783 constructor_elt *celt;
6784 bool maybe_const = true;
6785 bool npc;
6787 if (type == error_mark_node || value == error_mark_node)
6789 constructor_erroneous = 1;
6790 return;
6792 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6793 && (TREE_CODE (value) == STRING_CST
6794 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6795 && !(TREE_CODE (value) == STRING_CST
6796 && TREE_CODE (type) == ARRAY_TYPE
6797 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6798 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6799 TYPE_MAIN_VARIANT (type)))
6800 value = array_to_pointer_conversion (value);
6802 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6803 && require_constant_value && !flag_isoc99 && pending)
6805 /* As an extension, allow initializing objects with static storage
6806 duration with compound literals (which are then treated just as
6807 the brace enclosed list they contain). */
6808 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6809 value = DECL_INITIAL (decl);
6812 npc = null_pointer_constant_p (value);
6813 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
6815 semantic_type = TREE_TYPE (value);
6816 value = TREE_OPERAND (value, 0);
6818 value = c_fully_fold (value, require_constant_value, &maybe_const);
6820 if (value == error_mark_node)
6821 constructor_erroneous = 1;
6822 else if (!TREE_CONSTANT (value))
6823 constructor_constant = 0;
6824 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6825 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6826 || TREE_CODE (constructor_type) == UNION_TYPE)
6827 && DECL_C_BIT_FIELD (field)
6828 && TREE_CODE (value) != INTEGER_CST))
6829 constructor_simple = 0;
6830 if (!maybe_const)
6831 constructor_nonconst = 1;
6833 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6835 if (require_constant_value)
6837 error_init ("initializer element is not constant");
6838 value = error_mark_node;
6840 else if (require_constant_elements)
6841 pedwarn (input_location, 0,
6842 "initializer element is not computable at load time");
6844 else if (!maybe_const
6845 && (require_constant_value || require_constant_elements))
6846 pedwarn_init (input_location, 0,
6847 "initializer element is not a constant expression");
6849 /* If this field is empty (and not at the end of structure),
6850 don't do anything other than checking the initializer. */
6851 if (field
6852 && (TREE_TYPE (field) == error_mark_node
6853 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6854 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6855 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6856 || TREE_CHAIN (field)))))
6857 return;
6859 if (semantic_type)
6860 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
6861 value = digest_init (type, value, npc, strict_string,
6862 require_constant_value);
6863 if (value == error_mark_node)
6865 constructor_erroneous = 1;
6866 return;
6868 if (require_constant_value || require_constant_elements)
6869 constant_expression_warning (value);
6871 /* If this element doesn't come next in sequence,
6872 put it on constructor_pending_elts. */
6873 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6874 && (!constructor_incremental
6875 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6877 if (constructor_incremental
6878 && tree_int_cst_lt (field, constructor_unfilled_index))
6879 set_nonincremental_init ();
6881 add_pending_init (field, value, implicit);
6882 return;
6884 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6885 && (!constructor_incremental
6886 || field != constructor_unfilled_fields))
6888 /* We do this for records but not for unions. In a union,
6889 no matter which field is specified, it can be initialized
6890 right away since it starts at the beginning of the union. */
6891 if (constructor_incremental)
6893 if (!constructor_unfilled_fields)
6894 set_nonincremental_init ();
6895 else
6897 tree bitpos, unfillpos;
6899 bitpos = bit_position (field);
6900 unfillpos = bit_position (constructor_unfilled_fields);
6902 if (tree_int_cst_lt (bitpos, unfillpos))
6903 set_nonincremental_init ();
6907 add_pending_init (field, value, implicit);
6908 return;
6910 else if (TREE_CODE (constructor_type) == UNION_TYPE
6911 && !VEC_empty (constructor_elt, constructor_elements))
6913 if (!implicit)
6915 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6916 constructor_elements)->value))
6917 warning_init (0,
6918 "initialized field with side-effects overwritten");
6919 else if (warn_override_init)
6920 warning_init (OPT_Woverride_init, "initialized field overwritten");
6923 /* We can have just one union field set. */
6924 constructor_elements = 0;
6927 /* Otherwise, output this element either to
6928 constructor_elements or to the assembler file. */
6930 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6931 celt->index = field;
6932 celt->value = value;
6934 /* Advance the variable that indicates sequential elements output. */
6935 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6936 constructor_unfilled_index
6937 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6938 bitsize_one_node);
6939 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6941 constructor_unfilled_fields
6942 = TREE_CHAIN (constructor_unfilled_fields);
6944 /* Skip any nameless bit fields. */
6945 while (constructor_unfilled_fields != 0
6946 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6947 && DECL_NAME (constructor_unfilled_fields) == 0)
6948 constructor_unfilled_fields =
6949 TREE_CHAIN (constructor_unfilled_fields);
6951 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6952 constructor_unfilled_fields = 0;
6954 /* Now output any pending elements which have become next. */
6955 if (pending)
6956 output_pending_init_elements (0);
6959 /* Output any pending elements which have become next.
6960 As we output elements, constructor_unfilled_{fields,index}
6961 advances, which may cause other elements to become next;
6962 if so, they too are output.
6964 If ALL is 0, we return when there are
6965 no more pending elements to output now.
6967 If ALL is 1, we output space as necessary so that
6968 we can output all the pending elements. */
6970 static void
6971 output_pending_init_elements (int all)
6973 struct init_node *elt = constructor_pending_elts;
6974 tree next;
6976 retry:
6978 /* Look through the whole pending tree.
6979 If we find an element that should be output now,
6980 output it. Otherwise, set NEXT to the element
6981 that comes first among those still pending. */
6983 next = 0;
6984 while (elt)
6986 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6988 if (tree_int_cst_equal (elt->purpose,
6989 constructor_unfilled_index))
6990 output_init_element (elt->value, true,
6991 TREE_TYPE (constructor_type),
6992 constructor_unfilled_index, 0, false);
6993 else if (tree_int_cst_lt (constructor_unfilled_index,
6994 elt->purpose))
6996 /* Advance to the next smaller node. */
6997 if (elt->left)
6998 elt = elt->left;
6999 else
7001 /* We have reached the smallest node bigger than the
7002 current unfilled index. Fill the space first. */
7003 next = elt->purpose;
7004 break;
7007 else
7009 /* Advance to the next bigger node. */
7010 if (elt->right)
7011 elt = elt->right;
7012 else
7014 /* We have reached the biggest node in a subtree. Find
7015 the parent of it, which is the next bigger node. */
7016 while (elt->parent && elt->parent->right == elt)
7017 elt = elt->parent;
7018 elt = elt->parent;
7019 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7020 elt->purpose))
7022 next = elt->purpose;
7023 break;
7028 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7029 || TREE_CODE (constructor_type) == UNION_TYPE)
7031 tree ctor_unfilled_bitpos, elt_bitpos;
7033 /* If the current record is complete we are done. */
7034 if (constructor_unfilled_fields == 0)
7035 break;
7037 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7038 elt_bitpos = bit_position (elt->purpose);
7039 /* We can't compare fields here because there might be empty
7040 fields in between. */
7041 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7043 constructor_unfilled_fields = elt->purpose;
7044 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
7045 elt->purpose, 0, false);
7047 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7049 /* Advance to the next smaller node. */
7050 if (elt->left)
7051 elt = elt->left;
7052 else
7054 /* We have reached the smallest node bigger than the
7055 current unfilled field. Fill the space first. */
7056 next = elt->purpose;
7057 break;
7060 else
7062 /* Advance to the next bigger node. */
7063 if (elt->right)
7064 elt = elt->right;
7065 else
7067 /* We have reached the biggest node in a subtree. Find
7068 the parent of it, which is the next bigger node. */
7069 while (elt->parent && elt->parent->right == elt)
7070 elt = elt->parent;
7071 elt = elt->parent;
7072 if (elt
7073 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7074 bit_position (elt->purpose))))
7076 next = elt->purpose;
7077 break;
7084 /* Ordinarily return, but not if we want to output all
7085 and there are elements left. */
7086 if (!(all && next != 0))
7087 return;
7089 /* If it's not incremental, just skip over the gap, so that after
7090 jumping to retry we will output the next successive element. */
7091 if (TREE_CODE (constructor_type) == RECORD_TYPE
7092 || TREE_CODE (constructor_type) == UNION_TYPE)
7093 constructor_unfilled_fields = next;
7094 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7095 constructor_unfilled_index = next;
7097 /* ELT now points to the node in the pending tree with the next
7098 initializer to output. */
7099 goto retry;
7102 /* Add one non-braced element to the current constructor level.
7103 This adjusts the current position within the constructor's type.
7104 This may also start or terminate implicit levels
7105 to handle a partly-braced initializer.
7107 Once this has found the correct level for the new element,
7108 it calls output_init_element.
7110 IMPLICIT is true if value comes from pop_init_level (1),
7111 the new initializer has been merged with the existing one
7112 and thus no warnings should be emitted about overriding an
7113 existing initializer. */
7115 void
7116 process_init_element (struct c_expr value, bool implicit)
7118 tree orig_value = value.value;
7119 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7120 bool strict_string = value.original_code == STRING_CST;
7122 designator_depth = 0;
7123 designator_erroneous = 0;
7125 /* Handle superfluous braces around string cst as in
7126 char x[] = {"foo"}; */
7127 if (string_flag
7128 && constructor_type
7129 && TREE_CODE (constructor_type) == ARRAY_TYPE
7130 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7131 && integer_zerop (constructor_unfilled_index))
7133 if (constructor_stack->replacement_value.value)
7134 error_init ("excess elements in char array initializer");
7135 constructor_stack->replacement_value = value;
7136 return;
7139 if (constructor_stack->replacement_value.value != 0)
7141 error_init ("excess elements in struct initializer");
7142 return;
7145 /* Ignore elements of a brace group if it is entirely superfluous
7146 and has already been diagnosed. */
7147 if (constructor_type == 0)
7148 return;
7150 /* If we've exhausted any levels that didn't have braces,
7151 pop them now. */
7152 while (constructor_stack->implicit)
7154 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7155 || TREE_CODE (constructor_type) == UNION_TYPE)
7156 && constructor_fields == 0)
7157 process_init_element (pop_init_level (1), true);
7158 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7159 && (constructor_max_index == 0
7160 || tree_int_cst_lt (constructor_max_index,
7161 constructor_index)))
7162 process_init_element (pop_init_level (1), true);
7163 else
7164 break;
7167 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7168 if (constructor_range_stack)
7170 /* If value is a compound literal and we'll be just using its
7171 content, don't put it into a SAVE_EXPR. */
7172 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7173 || !require_constant_value
7174 || flag_isoc99)
7176 tree semantic_type = NULL_TREE;
7177 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7179 semantic_type = TREE_TYPE (value.value);
7180 value.value = TREE_OPERAND (value.value, 0);
7182 value.value = c_save_expr (value.value);
7183 if (semantic_type)
7184 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7185 value.value);
7189 while (1)
7191 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7193 tree fieldtype;
7194 enum tree_code fieldcode;
7196 if (constructor_fields == 0)
7198 pedwarn_init (input_location, 0,
7199 "excess elements in struct initializer");
7200 break;
7203 fieldtype = TREE_TYPE (constructor_fields);
7204 if (fieldtype != error_mark_node)
7205 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7206 fieldcode = TREE_CODE (fieldtype);
7208 /* Error for non-static initialization of a flexible array member. */
7209 if (fieldcode == ARRAY_TYPE
7210 && !require_constant_value
7211 && TYPE_SIZE (fieldtype) == NULL_TREE
7212 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7214 error_init ("non-static initialization of a flexible array member");
7215 break;
7218 /* Accept a string constant to initialize a subarray. */
7219 if (value.value != 0
7220 && fieldcode == ARRAY_TYPE
7221 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7222 && string_flag)
7223 value.value = orig_value;
7224 /* Otherwise, if we have come to a subaggregate,
7225 and we don't have an element of its type, push into it. */
7226 else if (value.value != 0
7227 && value.value != error_mark_node
7228 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7229 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7230 || fieldcode == UNION_TYPE))
7232 push_init_level (1);
7233 continue;
7236 if (value.value)
7238 push_member_name (constructor_fields);
7239 output_init_element (value.value, strict_string,
7240 fieldtype, constructor_fields, 1, implicit);
7241 RESTORE_SPELLING_DEPTH (constructor_depth);
7243 else
7244 /* Do the bookkeeping for an element that was
7245 directly output as a constructor. */
7247 /* For a record, keep track of end position of last field. */
7248 if (DECL_SIZE (constructor_fields))
7249 constructor_bit_index
7250 = size_binop (PLUS_EXPR,
7251 bit_position (constructor_fields),
7252 DECL_SIZE (constructor_fields));
7254 /* If the current field was the first one not yet written out,
7255 it isn't now, so update. */
7256 if (constructor_unfilled_fields == constructor_fields)
7258 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7259 /* Skip any nameless bit fields. */
7260 while (constructor_unfilled_fields != 0
7261 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7262 && DECL_NAME (constructor_unfilled_fields) == 0)
7263 constructor_unfilled_fields =
7264 TREE_CHAIN (constructor_unfilled_fields);
7268 constructor_fields = TREE_CHAIN (constructor_fields);
7269 /* Skip any nameless bit fields at the beginning. */
7270 while (constructor_fields != 0
7271 && DECL_C_BIT_FIELD (constructor_fields)
7272 && DECL_NAME (constructor_fields) == 0)
7273 constructor_fields = TREE_CHAIN (constructor_fields);
7275 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7277 tree fieldtype;
7278 enum tree_code fieldcode;
7280 if (constructor_fields == 0)
7282 pedwarn_init (input_location, 0,
7283 "excess elements in union initializer");
7284 break;
7287 fieldtype = TREE_TYPE (constructor_fields);
7288 if (fieldtype != error_mark_node)
7289 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7290 fieldcode = TREE_CODE (fieldtype);
7292 /* Warn that traditional C rejects initialization of unions.
7293 We skip the warning if the value is zero. This is done
7294 under the assumption that the zero initializer in user
7295 code appears conditioned on e.g. __STDC__ to avoid
7296 "missing initializer" warnings and relies on default
7297 initialization to zero in the traditional C case.
7298 We also skip the warning if the initializer is designated,
7299 again on the assumption that this must be conditional on
7300 __STDC__ anyway (and we've already complained about the
7301 member-designator already). */
7302 if (!in_system_header && !constructor_designated
7303 && !(value.value && (integer_zerop (value.value)
7304 || real_zerop (value.value))))
7305 warning (OPT_Wtraditional, "traditional C rejects initialization "
7306 "of unions");
7308 /* Accept a string constant to initialize a subarray. */
7309 if (value.value != 0
7310 && fieldcode == ARRAY_TYPE
7311 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7312 && string_flag)
7313 value.value = orig_value;
7314 /* Otherwise, if we have come to a subaggregate,
7315 and we don't have an element of its type, push into it. */
7316 else if (value.value != 0
7317 && value.value != error_mark_node
7318 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7319 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7320 || fieldcode == UNION_TYPE))
7322 push_init_level (1);
7323 continue;
7326 if (value.value)
7328 push_member_name (constructor_fields);
7329 output_init_element (value.value, strict_string,
7330 fieldtype, constructor_fields, 1, implicit);
7331 RESTORE_SPELLING_DEPTH (constructor_depth);
7333 else
7334 /* Do the bookkeeping for an element that was
7335 directly output as a constructor. */
7337 constructor_bit_index = DECL_SIZE (constructor_fields);
7338 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7341 constructor_fields = 0;
7343 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7345 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7346 enum tree_code eltcode = TREE_CODE (elttype);
7348 /* Accept a string constant to initialize a subarray. */
7349 if (value.value != 0
7350 && eltcode == ARRAY_TYPE
7351 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7352 && string_flag)
7353 value.value = orig_value;
7354 /* Otherwise, if we have come to a subaggregate,
7355 and we don't have an element of its type, push into it. */
7356 else if (value.value != 0
7357 && value.value != error_mark_node
7358 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7359 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7360 || eltcode == UNION_TYPE))
7362 push_init_level (1);
7363 continue;
7366 if (constructor_max_index != 0
7367 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7368 || integer_all_onesp (constructor_max_index)))
7370 pedwarn_init (input_location, 0,
7371 "excess elements in array initializer");
7372 break;
7375 /* Now output the actual element. */
7376 if (value.value)
7378 push_array_bounds (tree_low_cst (constructor_index, 1));
7379 output_init_element (value.value, strict_string,
7380 elttype, constructor_index, 1, implicit);
7381 RESTORE_SPELLING_DEPTH (constructor_depth);
7384 constructor_index
7385 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7387 if (!value.value)
7388 /* If we are doing the bookkeeping for an element that was
7389 directly output as a constructor, we must update
7390 constructor_unfilled_index. */
7391 constructor_unfilled_index = constructor_index;
7393 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7395 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7397 /* Do a basic check of initializer size. Note that vectors
7398 always have a fixed size derived from their type. */
7399 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7401 pedwarn_init (input_location, 0,
7402 "excess elements in vector initializer");
7403 break;
7406 /* Now output the actual element. */
7407 if (value.value)
7408 output_init_element (value.value, strict_string,
7409 elttype, constructor_index, 1, implicit);
7411 constructor_index
7412 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7414 if (!value.value)
7415 /* If we are doing the bookkeeping for an element that was
7416 directly output as a constructor, we must update
7417 constructor_unfilled_index. */
7418 constructor_unfilled_index = constructor_index;
7421 /* Handle the sole element allowed in a braced initializer
7422 for a scalar variable. */
7423 else if (constructor_type != error_mark_node
7424 && constructor_fields == 0)
7426 pedwarn_init (input_location, 0,
7427 "excess elements in scalar initializer");
7428 break;
7430 else
7432 if (value.value)
7433 output_init_element (value.value, strict_string,
7434 constructor_type, NULL_TREE, 1, implicit);
7435 constructor_fields = 0;
7438 /* Handle range initializers either at this level or anywhere higher
7439 in the designator stack. */
7440 if (constructor_range_stack)
7442 struct constructor_range_stack *p, *range_stack;
7443 int finish = 0;
7445 range_stack = constructor_range_stack;
7446 constructor_range_stack = 0;
7447 while (constructor_stack != range_stack->stack)
7449 gcc_assert (constructor_stack->implicit);
7450 process_init_element (pop_init_level (1), true);
7452 for (p = range_stack;
7453 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7454 p = p->prev)
7456 gcc_assert (constructor_stack->implicit);
7457 process_init_element (pop_init_level (1), true);
7460 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7461 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7462 finish = 1;
7464 while (1)
7466 constructor_index = p->index;
7467 constructor_fields = p->fields;
7468 if (finish && p->range_end && p->index == p->range_start)
7470 finish = 0;
7471 p->prev = 0;
7473 p = p->next;
7474 if (!p)
7475 break;
7476 push_init_level (2);
7477 p->stack = constructor_stack;
7478 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7479 p->index = p->range_start;
7482 if (!finish)
7483 constructor_range_stack = range_stack;
7484 continue;
7487 break;
7490 constructor_range_stack = 0;
7493 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7494 (guaranteed to be 'volatile' or null) and ARGS (represented using
7495 an ASM_EXPR node). */
7496 tree
7497 build_asm_stmt (tree cv_qualifier, tree args)
7499 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7500 ASM_VOLATILE_P (args) = 1;
7501 return add_stmt (args);
7504 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7505 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7506 SIMPLE indicates whether there was anything at all after the
7507 string in the asm expression -- asm("blah") and asm("blah" : )
7508 are subtly different. We use a ASM_EXPR node to represent this. */
7509 tree
7510 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7511 bool simple)
7513 tree tail;
7514 tree args;
7515 int i;
7516 const char *constraint;
7517 const char **oconstraints;
7518 bool allows_mem, allows_reg, is_inout;
7519 int ninputs, noutputs;
7521 ninputs = list_length (inputs);
7522 noutputs = list_length (outputs);
7523 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7525 string = resolve_asm_operand_names (string, outputs, inputs);
7527 /* Remove output conversions that change the type but not the mode. */
7528 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7530 tree output = TREE_VALUE (tail);
7532 /* ??? Really, this should not be here. Users should be using a
7533 proper lvalue, dammit. But there's a long history of using casts
7534 in the output operands. In cases like longlong.h, this becomes a
7535 primitive form of typechecking -- if the cast can be removed, then
7536 the output operand had a type of the proper width; otherwise we'll
7537 get an error. Gross, but ... */
7538 STRIP_NOPS (output);
7540 if (!lvalue_or_else (output, lv_asm))
7541 output = error_mark_node;
7543 if (output != error_mark_node
7544 && (TREE_READONLY (output)
7545 || TYPE_READONLY (TREE_TYPE (output))
7546 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7547 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7548 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7549 readonly_error (output, lv_asm);
7551 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7552 oconstraints[i] = constraint;
7554 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7555 &allows_mem, &allows_reg, &is_inout))
7557 /* If the operand is going to end up in memory,
7558 mark it addressable. */
7559 if (!allows_reg && !c_mark_addressable (output))
7560 output = error_mark_node;
7562 else
7563 output = error_mark_node;
7565 TREE_VALUE (tail) = output;
7568 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7570 tree input;
7572 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7573 input = TREE_VALUE (tail);
7575 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7576 oconstraints, &allows_mem, &allows_reg))
7578 /* If the operand is going to end up in memory,
7579 mark it addressable. */
7580 if (!allows_reg && allows_mem)
7582 /* Strip the nops as we allow this case. FIXME, this really
7583 should be rejected or made deprecated. */
7584 STRIP_NOPS (input);
7585 if (!c_mark_addressable (input))
7586 input = error_mark_node;
7589 else
7590 input = error_mark_node;
7592 TREE_VALUE (tail) = input;
7595 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7597 /* asm statements without outputs, including simple ones, are treated
7598 as volatile. */
7599 ASM_INPUT_P (args) = simple;
7600 ASM_VOLATILE_P (args) = (noutputs == 0);
7602 return args;
7605 /* Generate a goto statement to LABEL. */
7607 tree
7608 c_finish_goto_label (tree label)
7610 tree decl = lookup_label (label);
7611 if (!decl)
7612 return NULL_TREE;
7614 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7616 error ("jump into statement expression");
7617 return NULL_TREE;
7620 if (C_DECL_UNJUMPABLE_VM (decl))
7622 error ("jump into scope of identifier with variably modified type");
7623 return NULL_TREE;
7626 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7628 /* No jump from outside this statement expression context, so
7629 record that there is a jump from within this context. */
7630 struct c_label_list *nlist;
7631 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7632 nlist->next = label_context_stack_se->labels_used;
7633 nlist->label = decl;
7634 label_context_stack_se->labels_used = nlist;
7637 if (!C_DECL_UNDEFINABLE_VM (decl))
7639 /* No jump from outside this context context of identifiers with
7640 variably modified type, so record that there is a jump from
7641 within this context. */
7642 struct c_label_list *nlist;
7643 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7644 nlist->next = label_context_stack_vm->labels_used;
7645 nlist->label = decl;
7646 label_context_stack_vm->labels_used = nlist;
7649 TREE_USED (decl) = 1;
7650 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7653 /* Generate a computed goto statement to EXPR. */
7655 tree
7656 c_finish_goto_ptr (tree expr)
7658 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7659 expr = c_fully_fold (expr, false, NULL);
7660 expr = convert (ptr_type_node, expr);
7661 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7664 /* Generate a C `return' statement. RETVAL is the expression for what
7665 to return, or a null pointer for `return;' with no value. */
7667 tree
7668 c_finish_return (tree retval)
7670 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7671 bool no_warning = false;
7672 bool npc = false;
7674 if (TREE_THIS_VOLATILE (current_function_decl))
7675 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7677 if (retval)
7679 tree semantic_type = NULL_TREE;
7680 npc = null_pointer_constant_p (retval);
7681 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7683 semantic_type = TREE_TYPE (retval);
7684 retval = TREE_OPERAND (retval, 0);
7686 retval = c_fully_fold (retval, false, NULL);
7687 if (semantic_type)
7688 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7691 if (!retval)
7693 current_function_returns_null = 1;
7694 if ((warn_return_type || flag_isoc99)
7695 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7697 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7698 "%<return%> with no value, in "
7699 "function returning non-void");
7700 no_warning = true;
7703 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7705 current_function_returns_null = 1;
7706 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7707 pedwarn (input_location, 0,
7708 "%<return%> with a value, in function returning void");
7709 else
7710 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7711 "%<return%> with expression, in function returning void");
7713 else
7715 tree t = convert_for_assignment (valtype, retval, ic_return, npc,
7716 NULL_TREE, NULL_TREE, 0);
7717 tree res = DECL_RESULT (current_function_decl);
7718 tree inner;
7720 current_function_returns_value = 1;
7721 if (t == error_mark_node)
7722 return NULL_TREE;
7724 inner = t = convert (TREE_TYPE (res), t);
7726 /* Strip any conversions, additions, and subtractions, and see if
7727 we are returning the address of a local variable. Warn if so. */
7728 while (1)
7730 switch (TREE_CODE (inner))
7732 CASE_CONVERT:
7733 case NON_LVALUE_EXPR:
7734 case PLUS_EXPR:
7735 case POINTER_PLUS_EXPR:
7736 inner = TREE_OPERAND (inner, 0);
7737 continue;
7739 case MINUS_EXPR:
7740 /* If the second operand of the MINUS_EXPR has a pointer
7741 type (or is converted from it), this may be valid, so
7742 don't give a warning. */
7744 tree op1 = TREE_OPERAND (inner, 1);
7746 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7747 && (CONVERT_EXPR_P (op1)
7748 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7749 op1 = TREE_OPERAND (op1, 0);
7751 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7752 break;
7754 inner = TREE_OPERAND (inner, 0);
7755 continue;
7758 case ADDR_EXPR:
7759 inner = TREE_OPERAND (inner, 0);
7761 while (REFERENCE_CLASS_P (inner)
7762 && TREE_CODE (inner) != INDIRECT_REF)
7763 inner = TREE_OPERAND (inner, 0);
7765 if (DECL_P (inner)
7766 && !DECL_EXTERNAL (inner)
7767 && !TREE_STATIC (inner)
7768 && DECL_CONTEXT (inner) == current_function_decl)
7769 warning (0, "function returns address of local variable");
7770 break;
7772 default:
7773 break;
7776 break;
7779 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7781 if (warn_sequence_point)
7782 verify_sequence_points (retval);
7785 ret_stmt = build_stmt (RETURN_EXPR, retval);
7786 TREE_NO_WARNING (ret_stmt) |= no_warning;
7787 return add_stmt (ret_stmt);
7790 struct c_switch {
7791 /* The SWITCH_EXPR being built. */
7792 tree switch_expr;
7794 /* The original type of the testing expression, i.e. before the
7795 default conversion is applied. */
7796 tree orig_type;
7798 /* A splay-tree mapping the low element of a case range to the high
7799 element, or NULL_TREE if there is no high element. Used to
7800 determine whether or not a new case label duplicates an old case
7801 label. We need a tree, rather than simply a hash table, because
7802 of the GNU case range extension. */
7803 splay_tree cases;
7805 /* Number of nested statement expressions within this switch
7806 statement; if nonzero, case and default labels may not
7807 appear. */
7808 unsigned int blocked_stmt_expr;
7810 /* Scope of outermost declarations of identifiers with variably
7811 modified type within this switch statement; if nonzero, case and
7812 default labels may not appear. */
7813 unsigned int blocked_vm;
7815 /* The next node on the stack. */
7816 struct c_switch *next;
7819 /* A stack of the currently active switch statements. The innermost
7820 switch statement is on the top of the stack. There is no need to
7821 mark the stack for garbage collection because it is only active
7822 during the processing of the body of a function, and we never
7823 collect at that point. */
7825 struct c_switch *c_switch_stack;
7827 /* Start a C switch statement, testing expression EXP. Return the new
7828 SWITCH_EXPR. */
7830 tree
7831 c_start_case (tree exp)
7833 tree orig_type = error_mark_node;
7834 struct c_switch *cs;
7836 if (exp != error_mark_node)
7838 orig_type = TREE_TYPE (exp);
7840 if (!INTEGRAL_TYPE_P (orig_type))
7842 if (orig_type != error_mark_node)
7844 error ("switch quantity not an integer");
7845 orig_type = error_mark_node;
7847 exp = integer_zero_node;
7849 else
7851 tree type = TYPE_MAIN_VARIANT (orig_type);
7853 if (!in_system_header
7854 && (type == long_integer_type_node
7855 || type == long_unsigned_type_node))
7856 warning (OPT_Wtraditional, "%<long%> switch expression not "
7857 "converted to %<int%> in ISO C");
7859 exp = c_fully_fold (exp, false, NULL);
7860 exp = default_conversion (exp);
7862 if (warn_sequence_point)
7863 verify_sequence_points (exp);
7867 /* Add this new SWITCH_EXPR to the stack. */
7868 cs = XNEW (struct c_switch);
7869 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7870 cs->orig_type = orig_type;
7871 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7872 cs->blocked_stmt_expr = 0;
7873 cs->blocked_vm = 0;
7874 cs->next = c_switch_stack;
7875 c_switch_stack = cs;
7877 return add_stmt (cs->switch_expr);
7880 /* Process a case label. */
7882 tree
7883 do_case (tree low_value, tree high_value)
7885 tree label = NULL_TREE;
7887 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
7889 low_value = c_fully_fold (low_value, false, NULL);
7890 if (TREE_CODE (low_value) == INTEGER_CST)
7891 pedwarn (input_location, OPT_pedantic,
7892 "case label is not an integer constant expression");
7895 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
7897 high_value = c_fully_fold (high_value, false, NULL);
7898 if (TREE_CODE (high_value) == INTEGER_CST)
7899 pedwarn (input_location, OPT_pedantic,
7900 "case label is not an integer constant expression");
7903 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7904 && !c_switch_stack->blocked_vm)
7906 label = c_add_case_label (c_switch_stack->cases,
7907 SWITCH_COND (c_switch_stack->switch_expr),
7908 c_switch_stack->orig_type,
7909 low_value, high_value);
7910 if (label == error_mark_node)
7911 label = NULL_TREE;
7913 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7915 if (low_value)
7916 error ("case label in statement expression not containing "
7917 "enclosing switch statement");
7918 else
7919 error ("%<default%> label in statement expression not containing "
7920 "enclosing switch statement");
7922 else if (c_switch_stack && c_switch_stack->blocked_vm)
7924 if (low_value)
7925 error ("case label in scope of identifier with variably modified "
7926 "type not containing enclosing switch statement");
7927 else
7928 error ("%<default%> label in scope of identifier with variably "
7929 "modified type not containing enclosing switch statement");
7931 else if (low_value)
7932 error ("case label not within a switch statement");
7933 else
7934 error ("%<default%> label not within a switch statement");
7936 return label;
7939 /* Finish the switch statement. */
7941 void
7942 c_finish_case (tree body)
7944 struct c_switch *cs = c_switch_stack;
7945 location_t switch_location;
7947 SWITCH_BODY (cs->switch_expr) = body;
7949 /* We must not be within a statement expression nested in the switch
7950 at this point; we might, however, be within the scope of an
7951 identifier with variably modified type nested in the switch. */
7952 gcc_assert (!cs->blocked_stmt_expr);
7954 /* Emit warnings as needed. */
7955 if (EXPR_HAS_LOCATION (cs->switch_expr))
7956 switch_location = EXPR_LOCATION (cs->switch_expr);
7957 else
7958 switch_location = input_location;
7959 c_do_switch_warnings (cs->cases, switch_location,
7960 TREE_TYPE (cs->switch_expr),
7961 SWITCH_COND (cs->switch_expr));
7963 /* Pop the stack. */
7964 c_switch_stack = cs->next;
7965 splay_tree_delete (cs->cases);
7966 XDELETE (cs);
7969 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7970 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7971 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7972 statement, and was not surrounded with parenthesis. */
7974 void
7975 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7976 tree else_block, bool nested_if)
7978 tree stmt;
7980 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7981 if (warn_parentheses && nested_if && else_block == NULL)
7983 tree inner_if = then_block;
7985 /* We know from the grammar productions that there is an IF nested
7986 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7987 it might not be exactly THEN_BLOCK, but should be the last
7988 non-container statement within. */
7989 while (1)
7990 switch (TREE_CODE (inner_if))
7992 case COND_EXPR:
7993 goto found;
7994 case BIND_EXPR:
7995 inner_if = BIND_EXPR_BODY (inner_if);
7996 break;
7997 case STATEMENT_LIST:
7998 inner_if = expr_last (then_block);
7999 break;
8000 case TRY_FINALLY_EXPR:
8001 case TRY_CATCH_EXPR:
8002 inner_if = TREE_OPERAND (inner_if, 0);
8003 break;
8004 default:
8005 gcc_unreachable ();
8007 found:
8009 if (COND_EXPR_ELSE (inner_if))
8010 warning (OPT_Wparentheses,
8011 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8012 &if_locus);
8015 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8016 SET_EXPR_LOCATION (stmt, if_locus);
8017 add_stmt (stmt);
8020 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8021 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8022 is false for DO loops. INCR is the FOR increment expression. BODY is
8023 the statement controlled by the loop. BLAB is the break label. CLAB is
8024 the continue label. Everything is allowed to be NULL. */
8026 void
8027 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8028 tree blab, tree clab, bool cond_is_first)
8030 tree entry = NULL, exit = NULL, t;
8032 /* If the condition is zero don't generate a loop construct. */
8033 if (cond && integer_zerop (cond))
8035 if (cond_is_first)
8037 t = build_and_jump (&blab);
8038 SET_EXPR_LOCATION (t, start_locus);
8039 add_stmt (t);
8042 else
8044 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8046 /* If we have an exit condition, then we build an IF with gotos either
8047 out of the loop, or to the top of it. If there's no exit condition,
8048 then we just build a jump back to the top. */
8049 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8051 if (cond && !integer_nonzerop (cond))
8053 /* Canonicalize the loop condition to the end. This means
8054 generating a branch to the loop condition. Reuse the
8055 continue label, if possible. */
8056 if (cond_is_first)
8058 if (incr || !clab)
8060 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8061 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8063 else
8064 t = build1 (GOTO_EXPR, void_type_node, clab);
8065 SET_EXPR_LOCATION (t, start_locus);
8066 add_stmt (t);
8069 t = build_and_jump (&blab);
8070 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8071 if (cond_is_first)
8072 SET_EXPR_LOCATION (exit, start_locus);
8073 else
8074 SET_EXPR_LOCATION (exit, input_location);
8077 add_stmt (top);
8080 if (body)
8081 add_stmt (body);
8082 if (clab)
8083 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8084 if (incr)
8085 add_stmt (incr);
8086 if (entry)
8087 add_stmt (entry);
8088 if (exit)
8089 add_stmt (exit);
8090 if (blab)
8091 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8094 tree
8095 c_finish_bc_stmt (tree *label_p, bool is_break)
8097 bool skip;
8098 tree label = *label_p;
8100 /* In switch statements break is sometimes stylistically used after
8101 a return statement. This can lead to spurious warnings about
8102 control reaching the end of a non-void function when it is
8103 inlined. Note that we are calling block_may_fallthru with
8104 language specific tree nodes; this works because
8105 block_may_fallthru returns true when given something it does not
8106 understand. */
8107 skip = !block_may_fallthru (cur_stmt_list);
8109 if (!label)
8111 if (!skip)
8112 *label_p = label = create_artificial_label ();
8114 else if (TREE_CODE (label) == LABEL_DECL)
8116 else switch (TREE_INT_CST_LOW (label))
8118 case 0:
8119 if (is_break)
8120 error ("break statement not within loop or switch");
8121 else
8122 error ("continue statement not within a loop");
8123 return NULL_TREE;
8125 case 1:
8126 gcc_assert (is_break);
8127 error ("break statement used with OpenMP for loop");
8128 return NULL_TREE;
8130 default:
8131 gcc_unreachable ();
8134 if (skip)
8135 return NULL_TREE;
8137 if (!is_break)
8138 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8140 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8143 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8145 static void
8146 emit_side_effect_warnings (tree expr)
8148 if (expr == error_mark_node)
8150 else if (!TREE_SIDE_EFFECTS (expr))
8152 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8153 warning (OPT_Wunused_value, "%Hstatement with no effect",
8154 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8156 else
8157 warn_if_unused_value (expr, input_location);
8160 /* Process an expression as if it were a complete statement. Emit
8161 diagnostics, but do not call ADD_STMT. */
8163 tree
8164 c_process_expr_stmt (tree expr)
8166 if (!expr)
8167 return NULL_TREE;
8169 expr = c_fully_fold (expr, false, NULL);
8171 if (warn_sequence_point)
8172 verify_sequence_points (expr);
8174 if (TREE_TYPE (expr) != error_mark_node
8175 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8176 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8177 error ("expression statement has incomplete type");
8179 /* If we're not processing a statement expression, warn about unused values.
8180 Warnings for statement expressions will be emitted later, once we figure
8181 out which is the result. */
8182 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8183 && warn_unused_value)
8184 emit_side_effect_warnings (expr);
8186 /* If the expression is not of a type to which we cannot assign a line
8187 number, wrap the thing in a no-op NOP_EXPR. */
8188 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8189 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8191 if (CAN_HAVE_LOCATION_P (expr))
8192 SET_EXPR_LOCATION (expr, input_location);
8194 return expr;
8197 /* Emit an expression as a statement. */
8199 tree
8200 c_finish_expr_stmt (tree expr)
8202 if (expr)
8203 return add_stmt (c_process_expr_stmt (expr));
8204 else
8205 return NULL;
8208 /* Do the opposite and emit a statement as an expression. To begin,
8209 create a new binding level and return it. */
8211 tree
8212 c_begin_stmt_expr (void)
8214 tree ret;
8215 struct c_label_context_se *nstack;
8216 struct c_label_list *glist;
8218 /* We must force a BLOCK for this level so that, if it is not expanded
8219 later, there is a way to turn off the entire subtree of blocks that
8220 are contained in it. */
8221 keep_next_level ();
8222 ret = c_begin_compound_stmt (true);
8223 if (c_switch_stack)
8225 c_switch_stack->blocked_stmt_expr++;
8226 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8228 for (glist = label_context_stack_se->labels_used;
8229 glist != NULL;
8230 glist = glist->next)
8232 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8234 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8235 nstack->labels_def = NULL;
8236 nstack->labels_used = NULL;
8237 nstack->next = label_context_stack_se;
8238 label_context_stack_se = nstack;
8240 /* Mark the current statement list as belonging to a statement list. */
8241 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8243 return ret;
8246 tree
8247 c_finish_stmt_expr (tree body)
8249 tree last, type, tmp, val;
8250 tree *last_p;
8251 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8253 body = c_end_compound_stmt (body, true);
8254 if (c_switch_stack)
8256 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8257 c_switch_stack->blocked_stmt_expr--;
8259 /* It is no longer possible to jump to labels defined within this
8260 statement expression. */
8261 for (dlist = label_context_stack_se->labels_def;
8262 dlist != NULL;
8263 dlist = dlist->next)
8265 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8267 /* It is again possible to define labels with a goto just outside
8268 this statement expression. */
8269 for (glist = label_context_stack_se->next->labels_used;
8270 glist != NULL;
8271 glist = glist->next)
8273 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8274 glist_prev = glist;
8276 if (glist_prev != NULL)
8277 glist_prev->next = label_context_stack_se->labels_used;
8278 else
8279 label_context_stack_se->next->labels_used
8280 = label_context_stack_se->labels_used;
8281 label_context_stack_se = label_context_stack_se->next;
8283 /* Locate the last statement in BODY. See c_end_compound_stmt
8284 about always returning a BIND_EXPR. */
8285 last_p = &BIND_EXPR_BODY (body);
8286 last = BIND_EXPR_BODY (body);
8288 continue_searching:
8289 if (TREE_CODE (last) == STATEMENT_LIST)
8291 tree_stmt_iterator i;
8293 /* This can happen with degenerate cases like ({ }). No value. */
8294 if (!TREE_SIDE_EFFECTS (last))
8295 return body;
8297 /* If we're supposed to generate side effects warnings, process
8298 all of the statements except the last. */
8299 if (warn_unused_value)
8301 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8302 emit_side_effect_warnings (tsi_stmt (i));
8304 else
8305 i = tsi_last (last);
8306 last_p = tsi_stmt_ptr (i);
8307 last = *last_p;
8310 /* If the end of the list is exception related, then the list was split
8311 by a call to push_cleanup. Continue searching. */
8312 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8313 || TREE_CODE (last) == TRY_CATCH_EXPR)
8315 last_p = &TREE_OPERAND (last, 0);
8316 last = *last_p;
8317 goto continue_searching;
8320 /* In the case that the BIND_EXPR is not necessary, return the
8321 expression out from inside it. */
8322 if (last == error_mark_node
8323 || (last == BIND_EXPR_BODY (body)
8324 && BIND_EXPR_VARS (body) == NULL))
8326 /* Even if this looks constant, do not allow it in a constant
8327 expression. */
8328 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8329 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8330 /* Do not warn if the return value of a statement expression is
8331 unused. */
8332 TREE_NO_WARNING (last) = 1;
8333 return last;
8336 /* Extract the type of said expression. */
8337 type = TREE_TYPE (last);
8339 /* If we're not returning a value at all, then the BIND_EXPR that
8340 we already have is a fine expression to return. */
8341 if (!type || VOID_TYPE_P (type))
8342 return body;
8344 /* Now that we've located the expression containing the value, it seems
8345 silly to make voidify_wrapper_expr repeat the process. Create a
8346 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8347 tmp = create_tmp_var_raw (type, NULL);
8349 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8350 tree_expr_nonnegative_p giving up immediately. */
8351 val = last;
8352 if (TREE_CODE (val) == NOP_EXPR
8353 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8354 val = TREE_OPERAND (val, 0);
8356 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8357 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8359 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8362 /* Begin the scope of an identifier of variably modified type, scope
8363 number SCOPE. Jumping from outside this scope to inside it is not
8364 permitted. */
8366 void
8367 c_begin_vm_scope (unsigned int scope)
8369 struct c_label_context_vm *nstack;
8370 struct c_label_list *glist;
8372 gcc_assert (scope > 0);
8374 /* At file_scope, we don't have to do any processing. */
8375 if (label_context_stack_vm == NULL)
8376 return;
8378 if (c_switch_stack && !c_switch_stack->blocked_vm)
8379 c_switch_stack->blocked_vm = scope;
8380 for (glist = label_context_stack_vm->labels_used;
8381 glist != NULL;
8382 glist = glist->next)
8384 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8386 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8387 nstack->labels_def = NULL;
8388 nstack->labels_used = NULL;
8389 nstack->scope = scope;
8390 nstack->next = label_context_stack_vm;
8391 label_context_stack_vm = nstack;
8394 /* End a scope which may contain identifiers of variably modified
8395 type, scope number SCOPE. */
8397 void
8398 c_end_vm_scope (unsigned int scope)
8400 if (label_context_stack_vm == NULL)
8401 return;
8402 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8403 c_switch_stack->blocked_vm = 0;
8404 /* We may have a number of nested scopes of identifiers with
8405 variably modified type, all at this depth. Pop each in turn. */
8406 while (label_context_stack_vm->scope == scope)
8408 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8410 /* It is no longer possible to jump to labels defined within this
8411 scope. */
8412 for (dlist = label_context_stack_vm->labels_def;
8413 dlist != NULL;
8414 dlist = dlist->next)
8416 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8418 /* It is again possible to define labels with a goto just outside
8419 this scope. */
8420 for (glist = label_context_stack_vm->next->labels_used;
8421 glist != NULL;
8422 glist = glist->next)
8424 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8425 glist_prev = glist;
8427 if (glist_prev != NULL)
8428 glist_prev->next = label_context_stack_vm->labels_used;
8429 else
8430 label_context_stack_vm->next->labels_used
8431 = label_context_stack_vm->labels_used;
8432 label_context_stack_vm = label_context_stack_vm->next;
8436 /* Begin and end compound statements. This is as simple as pushing
8437 and popping new statement lists from the tree. */
8439 tree
8440 c_begin_compound_stmt (bool do_scope)
8442 tree stmt = push_stmt_list ();
8443 if (do_scope)
8444 push_scope ();
8445 return stmt;
8448 tree
8449 c_end_compound_stmt (tree stmt, bool do_scope)
8451 tree block = NULL;
8453 if (do_scope)
8455 if (c_dialect_objc ())
8456 objc_clear_super_receiver ();
8457 block = pop_scope ();
8460 stmt = pop_stmt_list (stmt);
8461 stmt = c_build_bind_expr (block, stmt);
8463 /* If this compound statement is nested immediately inside a statement
8464 expression, then force a BIND_EXPR to be created. Otherwise we'll
8465 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8466 STATEMENT_LISTs merge, and thus we can lose track of what statement
8467 was really last. */
8468 if (cur_stmt_list
8469 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8470 && TREE_CODE (stmt) != BIND_EXPR)
8472 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8473 TREE_SIDE_EFFECTS (stmt) = 1;
8476 return stmt;
8479 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8480 when the current scope is exited. EH_ONLY is true when this is not
8481 meant to apply to normal control flow transfer. */
8483 void
8484 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8486 enum tree_code code;
8487 tree stmt, list;
8488 bool stmt_expr;
8490 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8491 stmt = build_stmt (code, NULL, cleanup);
8492 add_stmt (stmt);
8493 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8494 list = push_stmt_list ();
8495 TREE_OPERAND (stmt, 0) = list;
8496 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8499 /* Build a binary-operation expression without default conversions.
8500 CODE is the kind of expression to build.
8501 LOCATION is the operator's location.
8502 This function differs from `build' in several ways:
8503 the data type of the result is computed and recorded in it,
8504 warnings are generated if arg data types are invalid,
8505 special handling for addition and subtraction of pointers is known,
8506 and some optimization is done (operations on narrow ints
8507 are done in the narrower type when that gives the same result).
8508 Constant folding is also done before the result is returned.
8510 Note that the operands will never have enumeral types, or function
8511 or array types, because either they will have the default conversions
8512 performed or they have both just been converted to some other type in which
8513 the arithmetic is to be done. */
8515 tree
8516 build_binary_op (location_t location, enum tree_code code,
8517 tree orig_op0, tree orig_op1, int convert_p)
8519 tree type0, type1, orig_type0, orig_type1;
8520 tree eptype;
8521 enum tree_code code0, code1;
8522 tree op0, op1;
8523 tree ret = error_mark_node;
8524 const char *invalid_op_diag;
8525 bool op0_int_operands, op1_int_operands;
8526 bool int_const, int_const_or_overflow, int_operands;
8528 /* Expression code to give to the expression when it is built.
8529 Normally this is CODE, which is what the caller asked for,
8530 but in some special cases we change it. */
8531 enum tree_code resultcode = code;
8533 /* Data type in which the computation is to be performed.
8534 In the simplest cases this is the common type of the arguments. */
8535 tree result_type = NULL;
8537 /* When the computation is in excess precision, the type of the
8538 final EXCESS_PRECISION_EXPR. */
8539 tree real_result_type = NULL;
8541 /* Nonzero means operands have already been type-converted
8542 in whatever way is necessary.
8543 Zero means they need to be converted to RESULT_TYPE. */
8544 int converted = 0;
8546 /* Nonzero means create the expression with this type, rather than
8547 RESULT_TYPE. */
8548 tree build_type = 0;
8550 /* Nonzero means after finally constructing the expression
8551 convert it to this type. */
8552 tree final_type = 0;
8554 /* Nonzero if this is an operation like MIN or MAX which can
8555 safely be computed in short if both args are promoted shorts.
8556 Also implies COMMON.
8557 -1 indicates a bitwise operation; this makes a difference
8558 in the exact conditions for when it is safe to do the operation
8559 in a narrower mode. */
8560 int shorten = 0;
8562 /* Nonzero if this is a comparison operation;
8563 if both args are promoted shorts, compare the original shorts.
8564 Also implies COMMON. */
8565 int short_compare = 0;
8567 /* Nonzero if this is a right-shift operation, which can be computed on the
8568 original short and then promoted if the operand is a promoted short. */
8569 int short_shift = 0;
8571 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8572 int common = 0;
8574 /* True means types are compatible as far as ObjC is concerned. */
8575 bool objc_ok;
8577 /* True means this is an arithmetic operation that may need excess
8578 precision. */
8579 bool may_need_excess_precision;
8581 if (location == UNKNOWN_LOCATION)
8582 location = input_location;
8584 op0 = orig_op0;
8585 op1 = orig_op1;
8587 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8588 if (op0_int_operands)
8589 op0 = remove_c_maybe_const_expr (op0);
8590 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8591 if (op1_int_operands)
8592 op1 = remove_c_maybe_const_expr (op1);
8593 int_operands = (op0_int_operands && op1_int_operands);
8594 if (int_operands)
8596 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8597 && TREE_CODE (orig_op1) == INTEGER_CST);
8598 int_const = (int_const_or_overflow
8599 && !TREE_OVERFLOW (orig_op0)
8600 && !TREE_OVERFLOW (orig_op1));
8602 else
8603 int_const = int_const_or_overflow = false;
8605 if (convert_p)
8607 op0 = default_conversion (op0);
8608 op1 = default_conversion (op1);
8611 orig_type0 = type0 = TREE_TYPE (op0);
8612 orig_type1 = type1 = TREE_TYPE (op1);
8614 /* The expression codes of the data types of the arguments tell us
8615 whether the arguments are integers, floating, pointers, etc. */
8616 code0 = TREE_CODE (type0);
8617 code1 = TREE_CODE (type1);
8619 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8620 STRIP_TYPE_NOPS (op0);
8621 STRIP_TYPE_NOPS (op1);
8623 /* If an error was already reported for one of the arguments,
8624 avoid reporting another error. */
8626 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8627 return error_mark_node;
8629 if ((invalid_op_diag
8630 = targetm.invalid_binary_op (code, type0, type1)))
8632 error_at (location, invalid_op_diag);
8633 return error_mark_node;
8636 switch (code)
8638 case PLUS_EXPR:
8639 case MINUS_EXPR:
8640 case MULT_EXPR:
8641 case TRUNC_DIV_EXPR:
8642 case CEIL_DIV_EXPR:
8643 case FLOOR_DIV_EXPR:
8644 case ROUND_DIV_EXPR:
8645 case EXACT_DIV_EXPR:
8646 may_need_excess_precision = true;
8647 break;
8648 default:
8649 may_need_excess_precision = false;
8650 break;
8652 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8654 op0 = TREE_OPERAND (op0, 0);
8655 type0 = TREE_TYPE (op0);
8657 else if (may_need_excess_precision
8658 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8660 type0 = eptype;
8661 op0 = convert (eptype, op0);
8663 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8665 op1 = TREE_OPERAND (op1, 0);
8666 type1 = TREE_TYPE (op1);
8668 else if (may_need_excess_precision
8669 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8671 type1 = eptype;
8672 op1 = convert (eptype, op1);
8675 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8677 switch (code)
8679 case PLUS_EXPR:
8680 /* Handle the pointer + int case. */
8681 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8683 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8684 goto return_build_binary_op;
8686 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8688 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8689 goto return_build_binary_op;
8691 else
8692 common = 1;
8693 break;
8695 case MINUS_EXPR:
8696 /* Subtraction of two similar pointers.
8697 We must subtract them as integers, then divide by object size. */
8698 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8699 && comp_target_types (type0, type1))
8701 ret = pointer_diff (op0, op1);
8702 goto return_build_binary_op;
8704 /* Handle pointer minus int. Just like pointer plus int. */
8705 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8707 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8708 goto return_build_binary_op;
8710 else
8711 common = 1;
8712 break;
8714 case MULT_EXPR:
8715 common = 1;
8716 break;
8718 case TRUNC_DIV_EXPR:
8719 case CEIL_DIV_EXPR:
8720 case FLOOR_DIV_EXPR:
8721 case ROUND_DIV_EXPR:
8722 case EXACT_DIV_EXPR:
8723 warn_for_div_by_zero (location, op1);
8725 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8726 || code0 == FIXED_POINT_TYPE
8727 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8728 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8729 || code1 == FIXED_POINT_TYPE
8730 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8732 enum tree_code tcode0 = code0, tcode1 = code1;
8734 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8735 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8736 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8737 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8739 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8740 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8741 resultcode = RDIV_EXPR;
8742 else
8743 /* Although it would be tempting to shorten always here, that
8744 loses on some targets, since the modulo instruction is
8745 undefined if the quotient can't be represented in the
8746 computation mode. We shorten only if unsigned or if
8747 dividing by something we know != -1. */
8748 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8749 || (TREE_CODE (op1) == INTEGER_CST
8750 && !integer_all_onesp (op1)));
8751 common = 1;
8753 break;
8755 case BIT_AND_EXPR:
8756 case BIT_IOR_EXPR:
8757 case BIT_XOR_EXPR:
8758 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8759 shorten = -1;
8760 /* Allow vector types which are not floating point types. */
8761 else if (code0 == VECTOR_TYPE
8762 && code1 == VECTOR_TYPE
8763 && !VECTOR_FLOAT_TYPE_P (type0)
8764 && !VECTOR_FLOAT_TYPE_P (type1))
8765 common = 1;
8766 break;
8768 case TRUNC_MOD_EXPR:
8769 case FLOOR_MOD_EXPR:
8770 warn_for_div_by_zero (location, op1);
8772 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8774 /* Although it would be tempting to shorten always here, that loses
8775 on some targets, since the modulo instruction is undefined if the
8776 quotient can't be represented in the computation mode. We shorten
8777 only if unsigned or if dividing by something we know != -1. */
8778 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8779 || (TREE_CODE (op1) == INTEGER_CST
8780 && !integer_all_onesp (op1)));
8781 common = 1;
8783 break;
8785 case TRUTH_ANDIF_EXPR:
8786 case TRUTH_ORIF_EXPR:
8787 case TRUTH_AND_EXPR:
8788 case TRUTH_OR_EXPR:
8789 case TRUTH_XOR_EXPR:
8790 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8791 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8792 || code0 == FIXED_POINT_TYPE)
8793 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8794 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8795 || code1 == FIXED_POINT_TYPE))
8797 /* Result of these operations is always an int,
8798 but that does not mean the operands should be
8799 converted to ints! */
8800 result_type = integer_type_node;
8801 op0 = c_common_truthvalue_conversion (location, op0);
8802 op1 = c_common_truthvalue_conversion (location, op1);
8803 converted = 1;
8805 if (code == TRUTH_ANDIF_EXPR)
8807 int_const_or_overflow = (int_operands
8808 && TREE_CODE (orig_op0) == INTEGER_CST
8809 && (op0 == truthvalue_false_node
8810 || TREE_CODE (orig_op1) == INTEGER_CST));
8811 int_const = (int_const_or_overflow
8812 && !TREE_OVERFLOW (orig_op0)
8813 && (op0 == truthvalue_false_node
8814 || !TREE_OVERFLOW (orig_op1)));
8816 else if (code == TRUTH_ORIF_EXPR)
8818 int_const_or_overflow = (int_operands
8819 && TREE_CODE (orig_op0) == INTEGER_CST
8820 && (op0 == truthvalue_true_node
8821 || TREE_CODE (orig_op1) == INTEGER_CST));
8822 int_const = (int_const_or_overflow
8823 && !TREE_OVERFLOW (orig_op0)
8824 && (op0 == truthvalue_true_node
8825 || !TREE_OVERFLOW (orig_op1)));
8827 break;
8829 /* Shift operations: result has same type as first operand;
8830 always convert second operand to int.
8831 Also set SHORT_SHIFT if shifting rightward. */
8833 case RSHIFT_EXPR:
8834 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8835 && code1 == INTEGER_TYPE)
8837 if (TREE_CODE (op1) == INTEGER_CST)
8839 if (tree_int_cst_sgn (op1) < 0)
8841 int_const = false;
8842 if (skip_evaluation == 0)
8843 warning (0, "right shift count is negative");
8845 else
8847 if (!integer_zerop (op1))
8848 short_shift = 1;
8850 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8852 int_const = false;
8853 if (skip_evaluation == 0)
8854 warning (0, "right shift count >= width of type");
8859 /* Use the type of the value to be shifted. */
8860 result_type = type0;
8861 /* Convert the shift-count to an integer, regardless of size
8862 of value being shifted. */
8863 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8864 op1 = convert (integer_type_node, op1);
8865 /* Avoid converting op1 to result_type later. */
8866 converted = 1;
8868 break;
8870 case LSHIFT_EXPR:
8871 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8872 && code1 == INTEGER_TYPE)
8874 if (TREE_CODE (op1) == INTEGER_CST)
8876 if (tree_int_cst_sgn (op1) < 0)
8878 int_const = false;
8879 if (skip_evaluation == 0)
8880 warning (0, "left shift count is negative");
8883 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8885 int_const = false;
8886 if (skip_evaluation == 0)
8887 warning (0, "left shift count >= width of type");
8891 /* Use the type of the value to be shifted. */
8892 result_type = type0;
8893 /* Convert the shift-count to an integer, regardless of size
8894 of value being shifted. */
8895 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8896 op1 = convert (integer_type_node, op1);
8897 /* Avoid converting op1 to result_type later. */
8898 converted = 1;
8900 break;
8902 case EQ_EXPR:
8903 case NE_EXPR:
8904 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8905 warning_at (location,
8906 OPT_Wfloat_equal,
8907 "comparing floating point with == or != is unsafe");
8908 /* Result of comparison is always int,
8909 but don't convert the args to int! */
8910 build_type = integer_type_node;
8911 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8912 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8913 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8914 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8915 short_compare = 1;
8916 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8918 tree tt0 = TREE_TYPE (type0);
8919 tree tt1 = TREE_TYPE (type1);
8920 /* Anything compares with void *. void * compares with anything.
8921 Otherwise, the targets must be compatible
8922 and both must be object or both incomplete. */
8923 if (comp_target_types (type0, type1))
8924 result_type = common_pointer_type (type0, type1);
8925 else if (VOID_TYPE_P (tt0))
8927 /* op0 != orig_op0 detects the case of something
8928 whose value is 0 but which isn't a valid null ptr const. */
8929 if (pedantic && !null_pointer_constant_p (orig_op0)
8930 && TREE_CODE (tt1) == FUNCTION_TYPE)
8931 pedwarn (location, OPT_pedantic, "ISO C forbids "
8932 "comparison of %<void *%> with function pointer");
8934 else if (VOID_TYPE_P (tt1))
8936 if (pedantic && !null_pointer_constant_p (orig_op1)
8937 && TREE_CODE (tt0) == FUNCTION_TYPE)
8938 pedwarn (location, OPT_pedantic, "ISO C forbids "
8939 "comparison of %<void *%> with function pointer");
8941 else
8942 /* Avoid warning about the volatile ObjC EH puts on decls. */
8943 if (!objc_ok)
8944 pedwarn (location, 0,
8945 "comparison of distinct pointer types lacks a cast");
8947 if (result_type == NULL_TREE)
8948 result_type = ptr_type_node;
8950 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8952 if (TREE_CODE (op0) == ADDR_EXPR
8953 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
8954 warning_at (location,
8955 OPT_Waddress, "the address of %qD will never be NULL",
8956 TREE_OPERAND (op0, 0));
8957 result_type = type0;
8959 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8961 if (TREE_CODE (op1) == ADDR_EXPR
8962 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
8963 warning_at (location,
8964 OPT_Waddress, "the address of %qD will never be NULL",
8965 TREE_OPERAND (op1, 0));
8966 result_type = type1;
8968 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8970 result_type = type0;
8971 pedwarn (location, 0, "comparison between pointer and integer");
8973 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8975 result_type = type1;
8976 pedwarn (location, 0, "comparison between pointer and integer");
8978 break;
8980 case LE_EXPR:
8981 case GE_EXPR:
8982 case LT_EXPR:
8983 case GT_EXPR:
8984 build_type = integer_type_node;
8985 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8986 || code0 == FIXED_POINT_TYPE)
8987 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8988 || code1 == FIXED_POINT_TYPE))
8989 short_compare = 1;
8990 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8992 if (comp_target_types (type0, type1))
8994 result_type = common_pointer_type (type0, type1);
8995 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8996 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8997 pedwarn (location, 0,
8998 "comparison of complete and incomplete pointers");
8999 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9000 pedwarn (location, OPT_pedantic, "ISO C forbids "
9001 "ordered comparisons of pointers to functions");
9003 else
9005 result_type = ptr_type_node;
9006 pedwarn (location, 0,
9007 "comparison of distinct pointer types lacks a cast");
9010 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9012 result_type = type0;
9013 if (pedantic)
9014 pedwarn (location, OPT_pedantic,
9015 "ordered comparison of pointer with integer zero");
9016 else if (extra_warnings)
9017 warning_at (location, OPT_Wextra,
9018 "ordered comparison of pointer with integer zero");
9020 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9022 result_type = type1;
9023 pedwarn (location, OPT_pedantic,
9024 "ordered comparison of pointer with integer zero");
9026 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9028 result_type = type0;
9029 pedwarn (location, 0, "comparison between pointer and integer");
9031 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9033 result_type = type1;
9034 pedwarn (location, 0, "comparison between pointer and integer");
9036 break;
9038 default:
9039 gcc_unreachable ();
9042 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9043 return error_mark_node;
9045 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9046 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9047 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9048 TREE_TYPE (type1))))
9050 binary_op_error (location, code, type0, type1);
9051 return error_mark_node;
9054 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9055 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9057 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9058 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9060 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
9062 if (shorten || common || short_compare)
9064 result_type = c_common_type (type0, type1);
9065 if (result_type == error_mark_node)
9066 return error_mark_node;
9069 /* For certain operations (which identify themselves by shorten != 0)
9070 if both args were extended from the same smaller type,
9071 do the arithmetic in that type and then extend.
9073 shorten !=0 and !=1 indicates a bitwise operation.
9074 For them, this optimization is safe only if
9075 both args are zero-extended or both are sign-extended.
9076 Otherwise, we might change the result.
9077 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9078 but calculated in (unsigned short) it would be (unsigned short)-1. */
9080 if (shorten && none_complex)
9082 final_type = result_type;
9083 result_type = shorten_binary_op (result_type, op0, op1,
9084 shorten == -1);
9087 /* Shifts can be shortened if shifting right. */
9089 if (short_shift)
9091 int unsigned_arg;
9092 tree arg0 = get_narrower (op0, &unsigned_arg);
9094 final_type = result_type;
9096 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9097 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9099 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9100 /* We can shorten only if the shift count is less than the
9101 number of bits in the smaller type size. */
9102 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9103 /* We cannot drop an unsigned shift after sign-extension. */
9104 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9106 /* Do an unsigned shift if the operand was zero-extended. */
9107 result_type
9108 = c_common_signed_or_unsigned_type (unsigned_arg,
9109 TREE_TYPE (arg0));
9110 /* Convert value-to-be-shifted to that type. */
9111 if (TREE_TYPE (op0) != result_type)
9112 op0 = convert (result_type, op0);
9113 converted = 1;
9117 /* Comparison operations are shortened too but differently.
9118 They identify themselves by setting short_compare = 1. */
9120 if (short_compare)
9122 /* Don't write &op0, etc., because that would prevent op0
9123 from being kept in a register.
9124 Instead, make copies of the our local variables and
9125 pass the copies by reference, then copy them back afterward. */
9126 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9127 enum tree_code xresultcode = resultcode;
9128 tree val
9129 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9131 if (val != 0)
9133 ret = val;
9134 goto return_build_binary_op;
9137 op0 = xop0, op1 = xop1;
9138 converted = 1;
9139 resultcode = xresultcode;
9141 if (!skip_evaluation)
9143 bool op0_maybe_const = true;
9144 bool op1_maybe_const = true;
9145 tree orig_op0_folded, orig_op1_folded;
9147 if (in_late_binary_op)
9149 orig_op0_folded = orig_op0;
9150 orig_op1_folded = orig_op1;
9152 else
9154 /* Fold for the sake of possible warnings, as in
9155 build_conditional_expr. This requires the
9156 "original" values to be folded, not just op0 and
9157 op1. */
9158 op0 = c_fully_fold (op0, require_constant_value,
9159 &op0_maybe_const);
9160 op1 = c_fully_fold (op1, require_constant_value,
9161 &op1_maybe_const);
9162 orig_op0_folded = c_fully_fold (orig_op0,
9163 require_constant_value,
9164 NULL);
9165 orig_op1_folded = c_fully_fold (orig_op1,
9166 require_constant_value,
9167 NULL);
9170 if (warn_sign_compare)
9171 warn_for_sign_compare (location, orig_op0_folded,
9172 orig_op1_folded, op0, op1,
9173 result_type, resultcode);
9174 if (!in_late_binary_op)
9176 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9178 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9179 NULL, op0);
9180 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9182 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9184 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9185 NULL, op1);
9186 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9193 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9194 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9195 Then the expression will be built.
9196 It will be given type FINAL_TYPE if that is nonzero;
9197 otherwise, it will be given type RESULT_TYPE. */
9199 if (!result_type)
9201 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9202 return error_mark_node;
9205 if (!converted)
9207 if (TREE_TYPE (op0) != result_type)
9208 op0 = convert_and_check (result_type, op0);
9209 if (TREE_TYPE (op1) != result_type)
9210 op1 = convert_and_check (result_type, op1);
9212 /* This can happen if one operand has a vector type, and the other
9213 has a different type. */
9214 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9215 return error_mark_node;
9218 if (build_type == NULL_TREE)
9220 build_type = result_type;
9221 if (type0 != orig_type0 || type1 != orig_type1)
9223 gcc_assert (may_need_excess_precision && common);
9224 real_result_type = c_common_type (orig_type0, orig_type1);
9228 /* Treat expressions in initializers specially as they can't trap. */
9229 if (int_const_or_overflow)
9230 ret = (require_constant_value
9231 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9232 : fold_build2 (resultcode, build_type, op0, op1));
9233 else
9234 ret = build2 (resultcode, build_type, op0, op1);
9235 if (final_type != 0)
9236 ret = convert (final_type, ret);
9238 return_build_binary_op:
9239 gcc_assert (ret != error_mark_node);
9240 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9241 ret = (int_operands
9242 ? note_integer_operands (ret)
9243 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9244 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9245 && !in_late_binary_op)
9246 ret = note_integer_operands (ret);
9247 if (real_result_type)
9248 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9249 protected_set_expr_location (ret, location);
9250 return ret;
9254 /* Convert EXPR to be a truth-value, validating its type for this
9255 purpose. LOCATION is the source location for the expression. */
9257 tree
9258 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9260 bool int_const, int_operands;
9262 switch (TREE_CODE (TREE_TYPE (expr)))
9264 case ARRAY_TYPE:
9265 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9266 return error_mark_node;
9268 case RECORD_TYPE:
9269 error_at (location, "used struct type value where scalar is required");
9270 return error_mark_node;
9272 case UNION_TYPE:
9273 error_at (location, "used union type value where scalar is required");
9274 return error_mark_node;
9276 case FUNCTION_TYPE:
9277 gcc_unreachable ();
9279 default:
9280 break;
9283 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9284 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9285 if (int_operands)
9286 expr = remove_c_maybe_const_expr (expr);
9288 /* ??? Should we also give an error for void and vectors rather than
9289 leaving those to give errors later? */
9290 expr = c_common_truthvalue_conversion (location, expr);
9292 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9294 if (TREE_OVERFLOW (expr))
9295 return expr;
9296 else
9297 return note_integer_operands (expr);
9299 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9300 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9301 return expr;
9305 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9306 required. */
9308 tree
9309 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9311 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9313 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9314 /* Executing a compound literal inside a function reinitializes
9315 it. */
9316 if (!TREE_STATIC (decl))
9317 *se = true;
9318 return decl;
9320 else
9321 return expr;
9324 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9326 tree
9327 c_begin_omp_parallel (void)
9329 tree block;
9331 keep_next_level ();
9332 block = c_begin_compound_stmt (true);
9334 return block;
9337 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9339 tree
9340 c_finish_omp_parallel (tree clauses, tree block)
9342 tree stmt;
9344 block = c_end_compound_stmt (block, true);
9346 stmt = make_node (OMP_PARALLEL);
9347 TREE_TYPE (stmt) = void_type_node;
9348 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9349 OMP_PARALLEL_BODY (stmt) = block;
9351 return add_stmt (stmt);
9354 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9356 tree
9357 c_begin_omp_task (void)
9359 tree block;
9361 keep_next_level ();
9362 block = c_begin_compound_stmt (true);
9364 return block;
9367 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9369 tree
9370 c_finish_omp_task (tree clauses, tree block)
9372 tree stmt;
9374 block = c_end_compound_stmt (block, true);
9376 stmt = make_node (OMP_TASK);
9377 TREE_TYPE (stmt) = void_type_node;
9378 OMP_TASK_CLAUSES (stmt) = clauses;
9379 OMP_TASK_BODY (stmt) = block;
9381 return add_stmt (stmt);
9384 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9385 Remove any elements from the list that are invalid. */
9387 tree
9388 c_finish_omp_clauses (tree clauses)
9390 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9391 tree c, t, *pc = &clauses;
9392 const char *name;
9394 bitmap_obstack_initialize (NULL);
9395 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9396 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9397 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9399 for (pc = &clauses, c = clauses; c ; c = *pc)
9401 bool remove = false;
9402 bool need_complete = false;
9403 bool need_implicitly_determined = false;
9405 switch (OMP_CLAUSE_CODE (c))
9407 case OMP_CLAUSE_SHARED:
9408 name = "shared";
9409 need_implicitly_determined = true;
9410 goto check_dup_generic;
9412 case OMP_CLAUSE_PRIVATE:
9413 name = "private";
9414 need_complete = true;
9415 need_implicitly_determined = true;
9416 goto check_dup_generic;
9418 case OMP_CLAUSE_REDUCTION:
9419 name = "reduction";
9420 need_implicitly_determined = true;
9421 t = OMP_CLAUSE_DECL (c);
9422 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9423 || POINTER_TYPE_P (TREE_TYPE (t)))
9425 error ("%qE has invalid type for %<reduction%>", t);
9426 remove = true;
9428 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9430 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9431 const char *r_name = NULL;
9433 switch (r_code)
9435 case PLUS_EXPR:
9436 case MULT_EXPR:
9437 case MINUS_EXPR:
9438 break;
9439 case BIT_AND_EXPR:
9440 r_name = "&";
9441 break;
9442 case BIT_XOR_EXPR:
9443 r_name = "^";
9444 break;
9445 case BIT_IOR_EXPR:
9446 r_name = "|";
9447 break;
9448 case TRUTH_ANDIF_EXPR:
9449 r_name = "&&";
9450 break;
9451 case TRUTH_ORIF_EXPR:
9452 r_name = "||";
9453 break;
9454 default:
9455 gcc_unreachable ();
9457 if (r_name)
9459 error ("%qE has invalid type for %<reduction(%s)%>",
9460 t, r_name);
9461 remove = true;
9464 goto check_dup_generic;
9466 case OMP_CLAUSE_COPYPRIVATE:
9467 name = "copyprivate";
9468 goto check_dup_generic;
9470 case OMP_CLAUSE_COPYIN:
9471 name = "copyin";
9472 t = OMP_CLAUSE_DECL (c);
9473 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9475 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9476 remove = true;
9478 goto check_dup_generic;
9480 check_dup_generic:
9481 t = OMP_CLAUSE_DECL (c);
9482 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9484 error ("%qE is not a variable in clause %qs", t, name);
9485 remove = true;
9487 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9488 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9489 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9491 error ("%qE appears more than once in data clauses", t);
9492 remove = true;
9494 else
9495 bitmap_set_bit (&generic_head, DECL_UID (t));
9496 break;
9498 case OMP_CLAUSE_FIRSTPRIVATE:
9499 name = "firstprivate";
9500 t = OMP_CLAUSE_DECL (c);
9501 need_complete = true;
9502 need_implicitly_determined = true;
9503 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9505 error ("%qE is not a variable in clause %<firstprivate%>", t);
9506 remove = true;
9508 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9509 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9511 error ("%qE appears more than once in data clauses", t);
9512 remove = true;
9514 else
9515 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9516 break;
9518 case OMP_CLAUSE_LASTPRIVATE:
9519 name = "lastprivate";
9520 t = OMP_CLAUSE_DECL (c);
9521 need_complete = true;
9522 need_implicitly_determined = true;
9523 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9525 error ("%qE is not a variable in clause %<lastprivate%>", t);
9526 remove = true;
9528 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9529 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9531 error ("%qE appears more than once in data clauses", t);
9532 remove = true;
9534 else
9535 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9536 break;
9538 case OMP_CLAUSE_IF:
9539 case OMP_CLAUSE_NUM_THREADS:
9540 case OMP_CLAUSE_SCHEDULE:
9541 case OMP_CLAUSE_NOWAIT:
9542 case OMP_CLAUSE_ORDERED:
9543 case OMP_CLAUSE_DEFAULT:
9544 case OMP_CLAUSE_UNTIED:
9545 case OMP_CLAUSE_COLLAPSE:
9546 pc = &OMP_CLAUSE_CHAIN (c);
9547 continue;
9549 default:
9550 gcc_unreachable ();
9553 if (!remove)
9555 t = OMP_CLAUSE_DECL (c);
9557 if (need_complete)
9559 t = require_complete_type (t);
9560 if (t == error_mark_node)
9561 remove = true;
9564 if (need_implicitly_determined)
9566 const char *share_name = NULL;
9568 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9569 share_name = "threadprivate";
9570 else switch (c_omp_predetermined_sharing (t))
9572 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9573 break;
9574 case OMP_CLAUSE_DEFAULT_SHARED:
9575 share_name = "shared";
9576 break;
9577 case OMP_CLAUSE_DEFAULT_PRIVATE:
9578 share_name = "private";
9579 break;
9580 default:
9581 gcc_unreachable ();
9583 if (share_name)
9585 error ("%qE is predetermined %qs for %qs",
9586 t, share_name, name);
9587 remove = true;
9592 if (remove)
9593 *pc = OMP_CLAUSE_CHAIN (c);
9594 else
9595 pc = &OMP_CLAUSE_CHAIN (c);
9598 bitmap_obstack_release (NULL);
9599 return clauses;
9602 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9603 down to the element type of an array. */
9605 tree
9606 c_build_qualified_type (tree type, int type_quals)
9608 if (type == error_mark_node)
9609 return type;
9611 if (TREE_CODE (type) == ARRAY_TYPE)
9613 tree t;
9614 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9615 type_quals);
9617 /* See if we already have an identically qualified type. */
9618 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9620 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9621 && TYPE_NAME (t) == TYPE_NAME (type)
9622 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9623 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9624 TYPE_ATTRIBUTES (type)))
9625 break;
9627 if (!t)
9629 tree domain = TYPE_DOMAIN (type);
9631 t = build_variant_type_copy (type);
9632 TREE_TYPE (t) = element_type;
9634 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9635 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9636 SET_TYPE_STRUCTURAL_EQUALITY (t);
9637 else if (TYPE_CANONICAL (element_type) != element_type
9638 || (domain && TYPE_CANONICAL (domain) != domain))
9640 tree unqualified_canon
9641 = build_array_type (TYPE_CANONICAL (element_type),
9642 domain? TYPE_CANONICAL (domain)
9643 : NULL_TREE);
9644 TYPE_CANONICAL (t)
9645 = c_build_qualified_type (unqualified_canon, type_quals);
9647 else
9648 TYPE_CANONICAL (t) = t;
9650 return t;
9653 /* A restrict-qualified pointer type must be a pointer to object or
9654 incomplete type. Note that the use of POINTER_TYPE_P also allows
9655 REFERENCE_TYPEs, which is appropriate for C++. */
9656 if ((type_quals & TYPE_QUAL_RESTRICT)
9657 && (!POINTER_TYPE_P (type)
9658 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
9660 error ("invalid use of %<restrict%>");
9661 type_quals &= ~TYPE_QUAL_RESTRICT;
9664 return build_qualified_type (type, type_quals);