re PR testsuite/40567 (Revision 149002 caused many failures)
[official-gcc.git] / gcc / c-typeck.c
blobdf1e1718405e160f63aab878094f6fb6355ddca0
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 /* Nonzero if we've already printed a "missing braces around initializer"
74 message within this initializer. */
75 static int missing_braces_mentioned;
77 static int require_constant_value;
78 static int require_constant_elements;
80 static bool null_pointer_constant_p (const_tree);
81 static tree qualify_type (tree, tree);
82 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *);
83 static int comp_target_types (location_t, tree, tree);
84 static int function_types_compatible_p (const_tree, const_tree, bool *);
85 static int type_lists_compatible_p (const_tree, const_tree, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 tree);
89 static tree pointer_diff (tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool);
100 static void output_pending_init_elements (int);
101 static int set_designator (int);
102 static void push_range_stack (tree);
103 static void add_pending_init (tree, tree, tree, bool);
104 static void set_nonincremental_init (void);
105 static void set_nonincremental_init_from_string (tree);
106 static tree find_init_member (tree);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return a variant of TYPE which has all the type qualifiers of LIKE
287 as well as those of TYPE. */
289 static tree
290 qualify_type (tree type, tree like)
292 return c_build_qualified_type (type,
293 TYPE_QUALS (type) | TYPE_QUALS (like));
296 /* Return true iff the given tree T is a variable length array. */
298 bool
299 c_vla_type_p (const_tree t)
301 if (TREE_CODE (t) == ARRAY_TYPE
302 && C_TYPE_VARIABLE_SIZE (t))
303 return true;
304 return false;
307 /* Return the composite type of two compatible types.
309 We assume that comptypes has already been done and returned
310 nonzero; if that isn't so, this may crash. In particular, we
311 assume that qualifiers match. */
313 tree
314 composite_type (tree t1, tree t2)
316 enum tree_code code1;
317 enum tree_code code2;
318 tree attributes;
320 /* Save time if the two types are the same. */
322 if (t1 == t2) return t1;
324 /* If one type is nonsense, use the other. */
325 if (t1 == error_mark_node)
326 return t2;
327 if (t2 == error_mark_node)
328 return t1;
330 code1 = TREE_CODE (t1);
331 code2 = TREE_CODE (t2);
333 /* Merge the attributes. */
334 attributes = targetm.merge_type_attributes (t1, t2);
336 /* If one is an enumerated type and the other is the compatible
337 integer type, the composite type might be either of the two
338 (DR#013 question 3). For consistency, use the enumerated type as
339 the composite type. */
341 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
342 return t1;
343 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
344 return t2;
346 gcc_assert (code1 == code2);
348 switch (code1)
350 case POINTER_TYPE:
351 /* For two pointers, do this recursively on the target type. */
353 tree pointed_to_1 = TREE_TYPE (t1);
354 tree pointed_to_2 = TREE_TYPE (t2);
355 tree target = composite_type (pointed_to_1, pointed_to_2);
356 t1 = build_pointer_type (target);
357 t1 = build_type_attribute_variant (t1, attributes);
358 return qualify_type (t1, t2);
361 case ARRAY_TYPE:
363 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
364 int quals;
365 tree unqual_elt;
366 tree d1 = TYPE_DOMAIN (t1);
367 tree d2 = TYPE_DOMAIN (t2);
368 bool d1_variable, d2_variable;
369 bool d1_zero, d2_zero;
370 bool t1_complete, t2_complete;
372 /* We should not have any type quals on arrays at all. */
373 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
375 t1_complete = COMPLETE_TYPE_P (t1);
376 t2_complete = COMPLETE_TYPE_P (t2);
378 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
379 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
381 d1_variable = (!d1_zero
382 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
383 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
384 d2_variable = (!d2_zero
385 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
386 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
387 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
388 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
390 /* Save space: see if the result is identical to one of the args. */
391 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
392 && (d2_variable || d2_zero || !d1_variable))
393 return build_type_attribute_variant (t1, attributes);
394 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
395 && (d1_variable || d1_zero || !d2_variable))
396 return build_type_attribute_variant (t2, attributes);
398 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
399 return build_type_attribute_variant (t1, attributes);
400 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
401 return build_type_attribute_variant (t2, attributes);
403 /* Merge the element types, and have a size if either arg has
404 one. We may have qualifiers on the element types. To set
405 up TYPE_MAIN_VARIANT correctly, we need to form the
406 composite of the unqualified types and add the qualifiers
407 back at the end. */
408 quals = TYPE_QUALS (strip_array_types (elt));
409 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
410 t1 = build_array_type (unqual_elt,
411 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
412 && (d2_variable
413 || d2_zero
414 || !d1_variable))
415 ? t1
416 : t2));
417 /* Ensure a composite type involving a zero-length array type
418 is a zero-length type not an incomplete type. */
419 if (d1_zero && d2_zero
420 && (t1_complete || t2_complete)
421 && !COMPLETE_TYPE_P (t1))
423 TYPE_SIZE (t1) = bitsize_zero_node;
424 TYPE_SIZE_UNIT (t1) = size_zero_node;
426 t1 = c_build_qualified_type (t1, quals);
427 return build_type_attribute_variant (t1, attributes);
430 case ENUMERAL_TYPE:
431 case RECORD_TYPE:
432 case UNION_TYPE:
433 if (attributes != NULL)
435 /* Try harder not to create a new aggregate type. */
436 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
437 return t1;
438 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
439 return t2;
441 return build_type_attribute_variant (t1, attributes);
443 case FUNCTION_TYPE:
444 /* Function types: prefer the one that specified arg types.
445 If both do, merge the arg types. Also merge the return types. */
447 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
448 tree p1 = TYPE_ARG_TYPES (t1);
449 tree p2 = TYPE_ARG_TYPES (t2);
450 int len;
451 tree newargs, n;
452 int i;
454 /* Save space: see if the result is identical to one of the args. */
455 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
456 return build_type_attribute_variant (t1, attributes);
457 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
458 return build_type_attribute_variant (t2, attributes);
460 /* Simple way if one arg fails to specify argument types. */
461 if (TYPE_ARG_TYPES (t1) == 0)
463 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
464 t1 = build_type_attribute_variant (t1, attributes);
465 return qualify_type (t1, t2);
467 if (TYPE_ARG_TYPES (t2) == 0)
469 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
470 t1 = build_type_attribute_variant (t1, attributes);
471 return qualify_type (t1, t2);
474 /* If both args specify argument types, we must merge the two
475 lists, argument by argument. */
476 /* Tell global_bindings_p to return false so that variable_size
477 doesn't die on VLAs in parameter types. */
478 c_override_global_bindings_to_false = true;
480 len = list_length (p1);
481 newargs = 0;
483 for (i = 0; i < len; i++)
484 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
486 n = newargs;
488 for (; p1;
489 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
491 /* A null type means arg type is not specified.
492 Take whatever the other function type has. */
493 if (TREE_VALUE (p1) == 0)
495 TREE_VALUE (n) = TREE_VALUE (p2);
496 goto parm_done;
498 if (TREE_VALUE (p2) == 0)
500 TREE_VALUE (n) = TREE_VALUE (p1);
501 goto parm_done;
504 /* Given wait (union {union wait *u; int *i} *)
505 and wait (union wait *),
506 prefer union wait * as type of parm. */
507 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
508 && TREE_VALUE (p1) != TREE_VALUE (p2))
510 tree memb;
511 tree mv2 = TREE_VALUE (p2);
512 if (mv2 && mv2 != error_mark_node
513 && TREE_CODE (mv2) != ARRAY_TYPE)
514 mv2 = TYPE_MAIN_VARIANT (mv2);
515 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
516 memb; memb = TREE_CHAIN (memb))
518 tree mv3 = TREE_TYPE (memb);
519 if (mv3 && mv3 != error_mark_node
520 && TREE_CODE (mv3) != ARRAY_TYPE)
521 mv3 = TYPE_MAIN_VARIANT (mv3);
522 if (comptypes (mv3, mv2))
524 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
525 TREE_VALUE (p2));
526 pedwarn (input_location, OPT_pedantic,
527 "function types not truly compatible in ISO C");
528 goto parm_done;
532 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
533 && TREE_VALUE (p2) != TREE_VALUE (p1))
535 tree memb;
536 tree mv1 = TREE_VALUE (p1);
537 if (mv1 && mv1 != error_mark_node
538 && TREE_CODE (mv1) != ARRAY_TYPE)
539 mv1 = TYPE_MAIN_VARIANT (mv1);
540 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
541 memb; memb = TREE_CHAIN (memb))
543 tree mv3 = TREE_TYPE (memb);
544 if (mv3 && mv3 != error_mark_node
545 && TREE_CODE (mv3) != ARRAY_TYPE)
546 mv3 = TYPE_MAIN_VARIANT (mv3);
547 if (comptypes (mv3, mv1))
549 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
550 TREE_VALUE (p1));
551 pedwarn (input_location, OPT_pedantic,
552 "function types not truly compatible in ISO C");
553 goto parm_done;
557 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
558 parm_done: ;
561 c_override_global_bindings_to_false = false;
562 t1 = build_function_type (valtype, newargs);
563 t1 = qualify_type (t1, t2);
564 /* ... falls through ... */
567 default:
568 return build_type_attribute_variant (t1, attributes);
573 /* Return the type of a conditional expression between pointers to
574 possibly differently qualified versions of compatible types.
576 We assume that comp_target_types has already been done and returned
577 nonzero; if that isn't so, this may crash. */
579 static tree
580 common_pointer_type (tree t1, tree t2)
582 tree attributes;
583 tree pointed_to_1, mv1;
584 tree pointed_to_2, mv2;
585 tree target;
586 unsigned target_quals;
588 /* Save time if the two types are the same. */
590 if (t1 == t2) return t1;
592 /* If one type is nonsense, use the other. */
593 if (t1 == error_mark_node)
594 return t2;
595 if (t2 == error_mark_node)
596 return t1;
598 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
599 && TREE_CODE (t2) == POINTER_TYPE);
601 /* Merge the attributes. */
602 attributes = targetm.merge_type_attributes (t1, t2);
604 /* Find the composite type of the target types, and combine the
605 qualifiers of the two types' targets. Do not lose qualifiers on
606 array element types by taking the TYPE_MAIN_VARIANT. */
607 mv1 = pointed_to_1 = TREE_TYPE (t1);
608 mv2 = pointed_to_2 = TREE_TYPE (t2);
609 if (TREE_CODE (mv1) != ARRAY_TYPE)
610 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
611 if (TREE_CODE (mv2) != ARRAY_TYPE)
612 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
613 target = composite_type (mv1, mv2);
615 /* For function types do not merge const qualifiers, but drop them
616 if used inconsistently. The middle-end uses these to mark const
617 and noreturn functions. */
618 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
619 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
620 else
621 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
622 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
623 return build_type_attribute_variant (t1, attributes);
626 /* Return the common type for two arithmetic types under the usual
627 arithmetic conversions. The default conversions have already been
628 applied, and enumerated types converted to their compatible integer
629 types. The resulting type is unqualified and has no attributes.
631 This is the type for the result of most arithmetic operations
632 if the operands have the given two types. */
634 static tree
635 c_common_type (tree t1, tree t2)
637 enum tree_code code1;
638 enum tree_code code2;
640 /* If one type is nonsense, use the other. */
641 if (t1 == error_mark_node)
642 return t2;
643 if (t2 == error_mark_node)
644 return t1;
646 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
647 t1 = TYPE_MAIN_VARIANT (t1);
649 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
650 t2 = TYPE_MAIN_VARIANT (t2);
652 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
653 t1 = build_type_attribute_variant (t1, NULL_TREE);
655 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
656 t2 = build_type_attribute_variant (t2, NULL_TREE);
658 /* Save time if the two types are the same. */
660 if (t1 == t2) return t1;
662 code1 = TREE_CODE (t1);
663 code2 = TREE_CODE (t2);
665 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
666 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
667 || code1 == INTEGER_TYPE);
668 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
669 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
670 || code2 == INTEGER_TYPE);
672 /* When one operand is a decimal float type, the other operand cannot be
673 a generic float type or a complex type. We also disallow vector types
674 here. */
675 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
676 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
678 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
680 error ("can%'t mix operands of decimal float and vector types");
681 return error_mark_node;
683 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
685 error ("can%'t mix operands of decimal float and complex types");
686 return error_mark_node;
688 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
690 error ("can%'t mix operands of decimal float and other float types");
691 return error_mark_node;
695 /* If one type is a vector type, return that type. (How the usual
696 arithmetic conversions apply to the vector types extension is not
697 precisely specified.) */
698 if (code1 == VECTOR_TYPE)
699 return t1;
701 if (code2 == VECTOR_TYPE)
702 return t2;
704 /* If one type is complex, form the common type of the non-complex
705 components, then make that complex. Use T1 or T2 if it is the
706 required type. */
707 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
709 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
710 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
711 tree subtype = c_common_type (subtype1, subtype2);
713 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
714 return t1;
715 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
716 return t2;
717 else
718 return build_complex_type (subtype);
721 /* If only one is real, use it as the result. */
723 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
724 return t1;
726 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
727 return t2;
729 /* If both are real and either are decimal floating point types, use
730 the decimal floating point type with the greater precision. */
732 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
734 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
735 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
736 return dfloat128_type_node;
737 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
738 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
739 return dfloat64_type_node;
740 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
741 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
742 return dfloat32_type_node;
745 /* Deal with fixed-point types. */
746 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
748 unsigned int unsignedp = 0, satp = 0;
749 enum machine_mode m1, m2;
750 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
752 m1 = TYPE_MODE (t1);
753 m2 = TYPE_MODE (t2);
755 /* If one input type is saturating, the result type is saturating. */
756 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
757 satp = 1;
759 /* If both fixed-point types are unsigned, the result type is unsigned.
760 When mixing fixed-point and integer types, follow the sign of the
761 fixed-point type.
762 Otherwise, the result type is signed. */
763 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
764 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
765 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
766 && TYPE_UNSIGNED (t1))
767 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
768 && TYPE_UNSIGNED (t2)))
769 unsignedp = 1;
771 /* The result type is signed. */
772 if (unsignedp == 0)
774 /* If the input type is unsigned, we need to convert to the
775 signed type. */
776 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
778 enum mode_class mclass = (enum mode_class) 0;
779 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
780 mclass = MODE_FRACT;
781 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
782 mclass = MODE_ACCUM;
783 else
784 gcc_unreachable ();
785 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
787 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
789 enum mode_class mclass = (enum mode_class) 0;
790 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
791 mclass = MODE_FRACT;
792 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
793 mclass = MODE_ACCUM;
794 else
795 gcc_unreachable ();
796 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
800 if (code1 == FIXED_POINT_TYPE)
802 fbit1 = GET_MODE_FBIT (m1);
803 ibit1 = GET_MODE_IBIT (m1);
805 else
807 fbit1 = 0;
808 /* Signed integers need to subtract one sign bit. */
809 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
812 if (code2 == FIXED_POINT_TYPE)
814 fbit2 = GET_MODE_FBIT (m2);
815 ibit2 = GET_MODE_IBIT (m2);
817 else
819 fbit2 = 0;
820 /* Signed integers need to subtract one sign bit. */
821 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
824 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
825 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
826 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
827 satp);
830 /* Both real or both integers; use the one with greater precision. */
832 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
833 return t1;
834 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
835 return t2;
837 /* Same precision. Prefer long longs to longs to ints when the
838 same precision, following the C99 rules on integer type rank
839 (which are equivalent to the C90 rules for C90 types). */
841 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
842 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
843 return long_long_unsigned_type_node;
845 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
846 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
848 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
849 return long_long_unsigned_type_node;
850 else
851 return long_long_integer_type_node;
854 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
855 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
856 return long_unsigned_type_node;
858 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
859 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
861 /* But preserve unsignedness from the other type,
862 since long cannot hold all the values of an unsigned int. */
863 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
864 return long_unsigned_type_node;
865 else
866 return long_integer_type_node;
869 /* Likewise, prefer long double to double even if same size. */
870 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
871 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
872 return long_double_type_node;
874 /* Otherwise prefer the unsigned one. */
876 if (TYPE_UNSIGNED (t1))
877 return t1;
878 else
879 return t2;
882 /* Wrapper around c_common_type that is used by c-common.c and other
883 front end optimizations that remove promotions. ENUMERAL_TYPEs
884 are allowed here and are converted to their compatible integer types.
885 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
886 preferably a non-Boolean type as the common type. */
887 tree
888 common_type (tree t1, tree t2)
890 if (TREE_CODE (t1) == ENUMERAL_TYPE)
891 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
892 if (TREE_CODE (t2) == ENUMERAL_TYPE)
893 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
895 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
896 if (TREE_CODE (t1) == BOOLEAN_TYPE
897 && TREE_CODE (t2) == BOOLEAN_TYPE)
898 return boolean_type_node;
900 /* If either type is BOOLEAN_TYPE, then return the other. */
901 if (TREE_CODE (t1) == BOOLEAN_TYPE)
902 return t2;
903 if (TREE_CODE (t2) == BOOLEAN_TYPE)
904 return t1;
906 return c_common_type (t1, t2);
909 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
910 or various other operations. Return 2 if they are compatible
911 but a warning may be needed if you use them together. */
914 comptypes (tree type1, tree type2)
916 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
917 int val;
919 val = comptypes_internal (type1, type2, NULL);
920 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
922 return val;
925 /* Like comptypes, but if it returns non-zero because enum and int are
926 compatible, it sets *ENUM_AND_INT_P to true. */
928 static int
929 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
931 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
932 int val;
934 val = comptypes_internal (type1, type2, enum_and_int_p);
935 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
937 return val;
940 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
941 or various other operations. Return 2 if they are compatible
942 but a warning may be needed if you use them together. If
943 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
944 compatible integer type, then this sets *ENUM_AND_INT_P to true;
945 *ENUM_AND_INT_P is never set to false. This differs from
946 comptypes, in that we don't free the seen types. */
948 static int
949 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p)
951 const_tree t1 = type1;
952 const_tree t2 = type2;
953 int attrval, val;
955 /* Suppress errors caused by previously reported errors. */
957 if (t1 == t2 || !t1 || !t2
958 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
959 return 1;
961 /* If either type is the internal version of sizetype, return the
962 language version. */
963 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
964 && TYPE_ORIG_SIZE_TYPE (t1))
965 t1 = TYPE_ORIG_SIZE_TYPE (t1);
967 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
968 && TYPE_ORIG_SIZE_TYPE (t2))
969 t2 = TYPE_ORIG_SIZE_TYPE (t2);
972 /* Enumerated types are compatible with integer types, but this is
973 not transitive: two enumerated types in the same translation unit
974 are compatible with each other only if they are the same type. */
976 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
978 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
979 if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE)
980 *enum_and_int_p = true;
982 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
984 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
985 if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE)
986 *enum_and_int_p = true;
989 if (t1 == t2)
990 return 1;
992 /* Different classes of types can't be compatible. */
994 if (TREE_CODE (t1) != TREE_CODE (t2))
995 return 0;
997 /* Qualifiers must match. C99 6.7.3p9 */
999 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1000 return 0;
1002 /* Allow for two different type nodes which have essentially the same
1003 definition. Note that we already checked for equality of the type
1004 qualifiers (just above). */
1006 if (TREE_CODE (t1) != ARRAY_TYPE
1007 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1008 return 1;
1010 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1011 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1012 return 0;
1014 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1015 val = 0;
1017 switch (TREE_CODE (t1))
1019 case POINTER_TYPE:
1020 /* Do not remove mode or aliasing information. */
1021 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1022 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1023 break;
1024 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1025 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1026 enum_and_int_p));
1027 break;
1029 case FUNCTION_TYPE:
1030 val = function_types_compatible_p (t1, t2, enum_and_int_p);
1031 break;
1033 case ARRAY_TYPE:
1035 tree d1 = TYPE_DOMAIN (t1);
1036 tree d2 = TYPE_DOMAIN (t2);
1037 bool d1_variable, d2_variable;
1038 bool d1_zero, d2_zero;
1039 val = 1;
1041 /* Target types must match incl. qualifiers. */
1042 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1043 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1044 enum_and_int_p)))
1045 return 0;
1047 /* Sizes must match unless one is missing or variable. */
1048 if (d1 == 0 || d2 == 0 || d1 == d2)
1049 break;
1051 d1_zero = !TYPE_MAX_VALUE (d1);
1052 d2_zero = !TYPE_MAX_VALUE (d2);
1054 d1_variable = (!d1_zero
1055 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1056 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1057 d2_variable = (!d2_zero
1058 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1059 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1060 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1061 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1063 if (d1_variable || d2_variable)
1064 break;
1065 if (d1_zero && d2_zero)
1066 break;
1067 if (d1_zero || d2_zero
1068 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1069 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1070 val = 0;
1072 break;
1075 case ENUMERAL_TYPE:
1076 case RECORD_TYPE:
1077 case UNION_TYPE:
1078 if (val != 1 && !same_translation_unit_p (t1, t2))
1080 tree a1 = TYPE_ATTRIBUTES (t1);
1081 tree a2 = TYPE_ATTRIBUTES (t2);
1083 if (! attribute_list_contained (a1, a2)
1084 && ! attribute_list_contained (a2, a1))
1085 break;
1087 if (attrval != 2)
1088 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1089 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1091 break;
1093 case VECTOR_TYPE:
1094 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1095 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1096 enum_and_int_p));
1097 break;
1099 default:
1100 break;
1102 return attrval == 2 && val == 1 ? 2 : val;
1105 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1106 ignoring their qualifiers. */
1108 static int
1109 comp_target_types (location_t location, tree ttl, tree ttr)
1111 int val;
1112 tree mvl, mvr;
1113 bool enum_and_int_p;
1115 /* Do not lose qualifiers on element types of array types that are
1116 pointer targets by taking their TYPE_MAIN_VARIANT. */
1117 mvl = TREE_TYPE (ttl);
1118 mvr = TREE_TYPE (ttr);
1119 if (TREE_CODE (mvl) != ARRAY_TYPE)
1120 mvl = TYPE_MAIN_VARIANT (mvl);
1121 if (TREE_CODE (mvr) != ARRAY_TYPE)
1122 mvr = TYPE_MAIN_VARIANT (mvr);
1123 enum_and_int_p = false;
1124 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1126 if (val == 2)
1127 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1129 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1130 warning_at (location, OPT_Wc___compat,
1131 "pointer target types incompatible in C++");
1133 return val;
1136 /* Subroutines of `comptypes'. */
1138 /* Determine whether two trees derive from the same translation unit.
1139 If the CONTEXT chain ends in a null, that tree's context is still
1140 being parsed, so if two trees have context chains ending in null,
1141 they're in the same translation unit. */
1143 same_translation_unit_p (const_tree t1, const_tree t2)
1145 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1146 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1148 case tcc_declaration:
1149 t1 = DECL_CONTEXT (t1); break;
1150 case tcc_type:
1151 t1 = TYPE_CONTEXT (t1); break;
1152 case tcc_exceptional:
1153 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1154 default: gcc_unreachable ();
1157 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1158 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1160 case tcc_declaration:
1161 t2 = DECL_CONTEXT (t2); break;
1162 case tcc_type:
1163 t2 = TYPE_CONTEXT (t2); break;
1164 case tcc_exceptional:
1165 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1166 default: gcc_unreachable ();
1169 return t1 == t2;
1172 /* Allocate the seen two types, assuming that they are compatible. */
1174 static struct tagged_tu_seen_cache *
1175 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1177 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1178 tu->next = tagged_tu_seen_base;
1179 tu->t1 = t1;
1180 tu->t2 = t2;
1182 tagged_tu_seen_base = tu;
1184 /* The C standard says that two structures in different translation
1185 units are compatible with each other only if the types of their
1186 fields are compatible (among other things). We assume that they
1187 are compatible until proven otherwise when building the cache.
1188 An example where this can occur is:
1189 struct a
1191 struct a *next;
1193 If we are comparing this against a similar struct in another TU,
1194 and did not assume they were compatible, we end up with an infinite
1195 loop. */
1196 tu->val = 1;
1197 return tu;
1200 /* Free the seen types until we get to TU_TIL. */
1202 static void
1203 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1205 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1206 while (tu != tu_til)
1208 const struct tagged_tu_seen_cache *const tu1
1209 = (const struct tagged_tu_seen_cache *) tu;
1210 tu = tu1->next;
1211 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1213 tagged_tu_seen_base = tu_til;
1216 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1217 compatible. If the two types are not the same (which has been
1218 checked earlier), this can only happen when multiple translation
1219 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1220 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1222 static int
1223 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1224 bool *enum_and_int_p)
1226 tree s1, s2;
1227 bool needs_warning = false;
1229 /* We have to verify that the tags of the types are the same. This
1230 is harder than it looks because this may be a typedef, so we have
1231 to go look at the original type. It may even be a typedef of a
1232 typedef...
1233 In the case of compiler-created builtin structs the TYPE_DECL
1234 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1235 while (TYPE_NAME (t1)
1236 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1237 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1238 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1240 while (TYPE_NAME (t2)
1241 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1242 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1243 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1245 /* C90 didn't have the requirement that the two tags be the same. */
1246 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1247 return 0;
1249 /* C90 didn't say what happened if one or both of the types were
1250 incomplete; we choose to follow C99 rules here, which is that they
1251 are compatible. */
1252 if (TYPE_SIZE (t1) == NULL
1253 || TYPE_SIZE (t2) == NULL)
1254 return 1;
1257 const struct tagged_tu_seen_cache * tts_i;
1258 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1259 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1260 return tts_i->val;
1263 switch (TREE_CODE (t1))
1265 case ENUMERAL_TYPE:
1267 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1268 /* Speed up the case where the type values are in the same order. */
1269 tree tv1 = TYPE_VALUES (t1);
1270 tree tv2 = TYPE_VALUES (t2);
1272 if (tv1 == tv2)
1274 return 1;
1277 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1279 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1280 break;
1281 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1283 tu->val = 0;
1284 return 0;
1288 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1290 return 1;
1292 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1294 tu->val = 0;
1295 return 0;
1298 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1300 tu->val = 0;
1301 return 0;
1304 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1306 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1307 if (s2 == NULL
1308 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1310 tu->val = 0;
1311 return 0;
1314 return 1;
1317 case UNION_TYPE:
1319 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1320 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1322 tu->val = 0;
1323 return 0;
1326 /* Speed up the common case where the fields are in the same order. */
1327 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1328 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1330 int result;
1332 if (DECL_NAME (s1) != DECL_NAME (s2))
1333 break;
1334 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1335 enum_and_int_p);
1337 if (result != 1 && !DECL_NAME (s1))
1338 break;
1339 if (result == 0)
1341 tu->val = 0;
1342 return 0;
1344 if (result == 2)
1345 needs_warning = true;
1347 if (TREE_CODE (s1) == FIELD_DECL
1348 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1349 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1351 tu->val = 0;
1352 return 0;
1355 if (!s1 && !s2)
1357 tu->val = needs_warning ? 2 : 1;
1358 return tu->val;
1361 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1363 bool ok = false;
1365 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1366 if (DECL_NAME (s1) == DECL_NAME (s2))
1368 int result;
1370 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1371 enum_and_int_p);
1373 if (result != 1 && !DECL_NAME (s1))
1374 continue;
1375 if (result == 0)
1377 tu->val = 0;
1378 return 0;
1380 if (result == 2)
1381 needs_warning = true;
1383 if (TREE_CODE (s1) == FIELD_DECL
1384 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1385 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1386 break;
1388 ok = true;
1389 break;
1391 if (!ok)
1393 tu->val = 0;
1394 return 0;
1397 tu->val = needs_warning ? 2 : 10;
1398 return tu->val;
1401 case RECORD_TYPE:
1403 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1405 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1406 s1 && s2;
1407 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1409 int result;
1410 if (TREE_CODE (s1) != TREE_CODE (s2)
1411 || DECL_NAME (s1) != DECL_NAME (s2))
1412 break;
1413 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1414 enum_and_int_p);
1415 if (result == 0)
1416 break;
1417 if (result == 2)
1418 needs_warning = true;
1420 if (TREE_CODE (s1) == FIELD_DECL
1421 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1422 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1423 break;
1425 if (s1 && s2)
1426 tu->val = 0;
1427 else
1428 tu->val = needs_warning ? 2 : 1;
1429 return tu->val;
1432 default:
1433 gcc_unreachable ();
1437 /* Return 1 if two function types F1 and F2 are compatible.
1438 If either type specifies no argument types,
1439 the other must specify a fixed number of self-promoting arg types.
1440 Otherwise, if one type specifies only the number of arguments,
1441 the other must specify that number of self-promoting arg types.
1442 Otherwise, the argument types must match.
1443 ENUM_AND_INT_P is as in comptypes_internal. */
1445 static int
1446 function_types_compatible_p (const_tree f1, const_tree f2,
1447 bool *enum_and_int_p)
1449 tree args1, args2;
1450 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1451 int val = 1;
1452 int val1;
1453 tree ret1, ret2;
1455 ret1 = TREE_TYPE (f1);
1456 ret2 = TREE_TYPE (f2);
1458 /* 'volatile' qualifiers on a function's return type used to mean
1459 the function is noreturn. */
1460 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1461 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1462 if (TYPE_VOLATILE (ret1))
1463 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1464 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1465 if (TYPE_VOLATILE (ret2))
1466 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1467 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1468 val = comptypes_internal (ret1, ret2, enum_and_int_p);
1469 if (val == 0)
1470 return 0;
1472 args1 = TYPE_ARG_TYPES (f1);
1473 args2 = TYPE_ARG_TYPES (f2);
1475 /* An unspecified parmlist matches any specified parmlist
1476 whose argument types don't need default promotions. */
1478 if (args1 == 0)
1480 if (!self_promoting_args_p (args2))
1481 return 0;
1482 /* If one of these types comes from a non-prototype fn definition,
1483 compare that with the other type's arglist.
1484 If they don't match, ask for a warning (but no error). */
1485 if (TYPE_ACTUAL_ARG_TYPES (f1)
1486 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1487 enum_and_int_p))
1488 val = 2;
1489 return val;
1491 if (args2 == 0)
1493 if (!self_promoting_args_p (args1))
1494 return 0;
1495 if (TYPE_ACTUAL_ARG_TYPES (f2)
1496 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1497 enum_and_int_p))
1498 val = 2;
1499 return val;
1502 /* Both types have argument lists: compare them and propagate results. */
1503 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p);
1504 return val1 != 1 ? val1 : val;
1507 /* Check two lists of types for compatibility, returning 0 for
1508 incompatible, 1 for compatible, or 2 for compatible with
1509 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1511 static int
1512 type_lists_compatible_p (const_tree args1, const_tree args2,
1513 bool *enum_and_int_p)
1515 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1516 int val = 1;
1517 int newval = 0;
1519 while (1)
1521 tree a1, mv1, a2, mv2;
1522 if (args1 == 0 && args2 == 0)
1523 return val;
1524 /* If one list is shorter than the other,
1525 they fail to match. */
1526 if (args1 == 0 || args2 == 0)
1527 return 0;
1528 mv1 = a1 = TREE_VALUE (args1);
1529 mv2 = a2 = TREE_VALUE (args2);
1530 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1531 mv1 = TYPE_MAIN_VARIANT (mv1);
1532 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1533 mv2 = TYPE_MAIN_VARIANT (mv2);
1534 /* A null pointer instead of a type
1535 means there is supposed to be an argument
1536 but nothing is specified about what type it has.
1537 So match anything that self-promotes. */
1538 if (a1 == 0)
1540 if (c_type_promotes_to (a2) != a2)
1541 return 0;
1543 else if (a2 == 0)
1545 if (c_type_promotes_to (a1) != a1)
1546 return 0;
1548 /* If one of the lists has an error marker, ignore this arg. */
1549 else if (TREE_CODE (a1) == ERROR_MARK
1550 || TREE_CODE (a2) == ERROR_MARK)
1552 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p)))
1554 /* Allow wait (union {union wait *u; int *i} *)
1555 and wait (union wait *) to be compatible. */
1556 if (TREE_CODE (a1) == UNION_TYPE
1557 && (TYPE_NAME (a1) == 0
1558 || TYPE_TRANSPARENT_UNION (a1))
1559 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1560 && tree_int_cst_equal (TYPE_SIZE (a1),
1561 TYPE_SIZE (a2)))
1563 tree memb;
1564 for (memb = TYPE_FIELDS (a1);
1565 memb; memb = TREE_CHAIN (memb))
1567 tree mv3 = TREE_TYPE (memb);
1568 if (mv3 && mv3 != error_mark_node
1569 && TREE_CODE (mv3) != ARRAY_TYPE)
1570 mv3 = TYPE_MAIN_VARIANT (mv3);
1571 if (comptypes_internal (mv3, mv2, enum_and_int_p))
1572 break;
1574 if (memb == 0)
1575 return 0;
1577 else if (TREE_CODE (a2) == UNION_TYPE
1578 && (TYPE_NAME (a2) == 0
1579 || TYPE_TRANSPARENT_UNION (a2))
1580 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1581 && tree_int_cst_equal (TYPE_SIZE (a2),
1582 TYPE_SIZE (a1)))
1584 tree memb;
1585 for (memb = TYPE_FIELDS (a2);
1586 memb; memb = TREE_CHAIN (memb))
1588 tree mv3 = TREE_TYPE (memb);
1589 if (mv3 && mv3 != error_mark_node
1590 && TREE_CODE (mv3) != ARRAY_TYPE)
1591 mv3 = TYPE_MAIN_VARIANT (mv3);
1592 if (comptypes_internal (mv3, mv1, enum_and_int_p))
1593 break;
1595 if (memb == 0)
1596 return 0;
1598 else
1599 return 0;
1602 /* comptypes said ok, but record if it said to warn. */
1603 if (newval > val)
1604 val = newval;
1606 args1 = TREE_CHAIN (args1);
1607 args2 = TREE_CHAIN (args2);
1611 /* Compute the size to increment a pointer by. */
1613 static tree
1614 c_size_in_bytes (const_tree type)
1616 enum tree_code code = TREE_CODE (type);
1618 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1619 return size_one_node;
1621 if (!COMPLETE_OR_VOID_TYPE_P (type))
1623 error ("arithmetic on pointer to an incomplete type");
1624 return size_one_node;
1627 /* Convert in case a char is more than one unit. */
1628 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1629 size_int (TYPE_PRECISION (char_type_node)
1630 / BITS_PER_UNIT));
1633 /* Return either DECL or its known constant value (if it has one). */
1635 tree
1636 decl_constant_value (tree decl)
1638 if (/* Don't change a variable array bound or initial value to a constant
1639 in a place where a variable is invalid. Note that DECL_INITIAL
1640 isn't valid for a PARM_DECL. */
1641 current_function_decl != 0
1642 && TREE_CODE (decl) != PARM_DECL
1643 && !TREE_THIS_VOLATILE (decl)
1644 && TREE_READONLY (decl)
1645 && DECL_INITIAL (decl) != 0
1646 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1647 /* This is invalid if initial value is not constant.
1648 If it has either a function call, a memory reference,
1649 or a variable, then re-evaluating it could give different results. */
1650 && TREE_CONSTANT (DECL_INITIAL (decl))
1651 /* Check for cases where this is sub-optimal, even though valid. */
1652 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1653 return DECL_INITIAL (decl);
1654 return decl;
1657 /* Convert the array expression EXP to a pointer. */
1658 static tree
1659 array_to_pointer_conversion (location_t loc, tree exp)
1661 tree orig_exp = exp;
1662 tree type = TREE_TYPE (exp);
1663 tree adr;
1664 tree restype = TREE_TYPE (type);
1665 tree ptrtype;
1667 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1669 STRIP_TYPE_NOPS (exp);
1671 if (TREE_NO_WARNING (orig_exp))
1672 TREE_NO_WARNING (exp) = 1;
1674 ptrtype = build_pointer_type (restype);
1676 if (TREE_CODE (exp) == INDIRECT_REF)
1677 return convert (ptrtype, TREE_OPERAND (exp, 0));
1679 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1680 return convert (ptrtype, adr);
1683 /* Convert the function expression EXP to a pointer. */
1684 static tree
1685 function_to_pointer_conversion (location_t loc, tree exp)
1687 tree orig_exp = exp;
1689 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1691 STRIP_TYPE_NOPS (exp);
1693 if (TREE_NO_WARNING (orig_exp))
1694 TREE_NO_WARNING (exp) = 1;
1696 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1699 /* Perform the default conversion of arrays and functions to pointers.
1700 Return the result of converting EXP. For any other expression, just
1701 return EXP.
1703 LOC is the location of the expression. */
1705 struct c_expr
1706 default_function_array_conversion (location_t loc, struct c_expr exp)
1708 tree orig_exp = exp.value;
1709 tree type = TREE_TYPE (exp.value);
1710 enum tree_code code = TREE_CODE (type);
1712 switch (code)
1714 case ARRAY_TYPE:
1716 bool not_lvalue = false;
1717 bool lvalue_array_p;
1719 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1720 || CONVERT_EXPR_P (exp.value))
1721 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1723 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1724 not_lvalue = true;
1725 exp.value = TREE_OPERAND (exp.value, 0);
1728 if (TREE_NO_WARNING (orig_exp))
1729 TREE_NO_WARNING (exp.value) = 1;
1731 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1732 if (!flag_isoc99 && !lvalue_array_p)
1734 /* Before C99, non-lvalue arrays do not decay to pointers.
1735 Normally, using such an array would be invalid; but it can
1736 be used correctly inside sizeof or as a statement expression.
1737 Thus, do not give an error here; an error will result later. */
1738 return exp;
1741 exp.value = array_to_pointer_conversion (loc, exp.value);
1743 break;
1744 case FUNCTION_TYPE:
1745 exp.value = function_to_pointer_conversion (loc, exp.value);
1746 break;
1747 default:
1748 break;
1751 return exp;
1755 /* EXP is an expression of integer type. Apply the integer promotions
1756 to it and return the promoted value. */
1758 tree
1759 perform_integral_promotions (tree exp)
1761 tree type = TREE_TYPE (exp);
1762 enum tree_code code = TREE_CODE (type);
1764 gcc_assert (INTEGRAL_TYPE_P (type));
1766 /* Normally convert enums to int,
1767 but convert wide enums to something wider. */
1768 if (code == ENUMERAL_TYPE)
1770 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1771 TYPE_PRECISION (integer_type_node)),
1772 ((TYPE_PRECISION (type)
1773 >= TYPE_PRECISION (integer_type_node))
1774 && TYPE_UNSIGNED (type)));
1776 return convert (type, exp);
1779 /* ??? This should no longer be needed now bit-fields have their
1780 proper types. */
1781 if (TREE_CODE (exp) == COMPONENT_REF
1782 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1783 /* If it's thinner than an int, promote it like a
1784 c_promoting_integer_type_p, otherwise leave it alone. */
1785 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1786 TYPE_PRECISION (integer_type_node)))
1787 return convert (integer_type_node, exp);
1789 if (c_promoting_integer_type_p (type))
1791 /* Preserve unsignedness if not really getting any wider. */
1792 if (TYPE_UNSIGNED (type)
1793 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1794 return convert (unsigned_type_node, exp);
1796 return convert (integer_type_node, exp);
1799 return exp;
1803 /* Perform default promotions for C data used in expressions.
1804 Enumeral types or short or char are converted to int.
1805 In addition, manifest constants symbols are replaced by their values. */
1807 tree
1808 default_conversion (tree exp)
1810 tree orig_exp;
1811 tree type = TREE_TYPE (exp);
1812 enum tree_code code = TREE_CODE (type);
1813 tree promoted_type;
1815 /* Functions and arrays have been converted during parsing. */
1816 gcc_assert (code != FUNCTION_TYPE);
1817 if (code == ARRAY_TYPE)
1818 return exp;
1820 /* Constants can be used directly unless they're not loadable. */
1821 if (TREE_CODE (exp) == CONST_DECL)
1822 exp = DECL_INITIAL (exp);
1824 /* Strip no-op conversions. */
1825 orig_exp = exp;
1826 STRIP_TYPE_NOPS (exp);
1828 if (TREE_NO_WARNING (orig_exp))
1829 TREE_NO_WARNING (exp) = 1;
1831 if (code == VOID_TYPE)
1833 error ("void value not ignored as it ought to be");
1834 return error_mark_node;
1837 exp = require_complete_type (exp);
1838 if (exp == error_mark_node)
1839 return error_mark_node;
1841 promoted_type = targetm.promoted_type (type);
1842 if (promoted_type)
1843 return convert (promoted_type, exp);
1845 if (INTEGRAL_TYPE_P (type))
1846 return perform_integral_promotions (exp);
1848 return exp;
1851 /* Look up COMPONENT in a structure or union DECL.
1853 If the component name is not found, returns NULL_TREE. Otherwise,
1854 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1855 stepping down the chain to the component, which is in the last
1856 TREE_VALUE of the list. Normally the list is of length one, but if
1857 the component is embedded within (nested) anonymous structures or
1858 unions, the list steps down the chain to the component. */
1860 static tree
1861 lookup_field (tree decl, tree component)
1863 tree type = TREE_TYPE (decl);
1864 tree field;
1866 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1867 to the field elements. Use a binary search on this array to quickly
1868 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1869 will always be set for structures which have many elements. */
1871 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1873 int bot, top, half;
1874 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1876 field = TYPE_FIELDS (type);
1877 bot = 0;
1878 top = TYPE_LANG_SPECIFIC (type)->s->len;
1879 while (top - bot > 1)
1881 half = (top - bot + 1) >> 1;
1882 field = field_array[bot+half];
1884 if (DECL_NAME (field) == NULL_TREE)
1886 /* Step through all anon unions in linear fashion. */
1887 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1889 field = field_array[bot++];
1890 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1891 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1893 tree anon = lookup_field (field, component);
1895 if (anon)
1896 return tree_cons (NULL_TREE, field, anon);
1900 /* Entire record is only anon unions. */
1901 if (bot > top)
1902 return NULL_TREE;
1904 /* Restart the binary search, with new lower bound. */
1905 continue;
1908 if (DECL_NAME (field) == component)
1909 break;
1910 if (DECL_NAME (field) < component)
1911 bot += half;
1912 else
1913 top = bot + half;
1916 if (DECL_NAME (field_array[bot]) == component)
1917 field = field_array[bot];
1918 else if (DECL_NAME (field) != component)
1919 return NULL_TREE;
1921 else
1923 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1925 if (DECL_NAME (field) == NULL_TREE
1926 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1927 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1929 tree anon = lookup_field (field, component);
1931 if (anon)
1932 return tree_cons (NULL_TREE, field, anon);
1935 if (DECL_NAME (field) == component)
1936 break;
1939 if (field == NULL_TREE)
1940 return NULL_TREE;
1943 return tree_cons (NULL_TREE, field, NULL_TREE);
1946 /* Make an expression to refer to the COMPONENT field of structure or
1947 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
1948 location of the COMPONENT_REF. */
1950 tree
1951 build_component_ref (location_t loc, tree datum, tree component)
1953 tree type = TREE_TYPE (datum);
1954 enum tree_code code = TREE_CODE (type);
1955 tree field = NULL;
1956 tree ref;
1957 bool datum_lvalue = lvalue_p (datum);
1959 if (!objc_is_public (datum, component))
1960 return error_mark_node;
1962 /* See if there is a field or component with name COMPONENT. */
1964 if (code == RECORD_TYPE || code == UNION_TYPE)
1966 if (!COMPLETE_TYPE_P (type))
1968 c_incomplete_type_error (NULL_TREE, type);
1969 return error_mark_node;
1972 field = lookup_field (datum, component);
1974 if (!field)
1976 error_at (loc, "%qT has no member named %qE", type, component);
1977 return error_mark_node;
1980 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1981 This might be better solved in future the way the C++ front
1982 end does it - by giving the anonymous entities each a
1983 separate name and type, and then have build_component_ref
1984 recursively call itself. We can't do that here. */
1987 tree subdatum = TREE_VALUE (field);
1988 int quals;
1989 tree subtype;
1990 bool use_datum_quals;
1992 if (TREE_TYPE (subdatum) == error_mark_node)
1993 return error_mark_node;
1995 /* If this is an rvalue, it does not have qualifiers in C
1996 standard terms and we must avoid propagating such
1997 qualifiers down to a non-lvalue array that is then
1998 converted to a pointer. */
1999 use_datum_quals = (datum_lvalue
2000 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2002 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2003 if (use_datum_quals)
2004 quals |= TYPE_QUALS (TREE_TYPE (datum));
2005 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2007 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2008 NULL_TREE);
2009 SET_EXPR_LOCATION (ref, loc);
2010 if (TREE_READONLY (subdatum)
2011 || (use_datum_quals && TREE_READONLY (datum)))
2012 TREE_READONLY (ref) = 1;
2013 if (TREE_THIS_VOLATILE (subdatum)
2014 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2015 TREE_THIS_VOLATILE (ref) = 1;
2017 if (TREE_DEPRECATED (subdatum))
2018 warn_deprecated_use (subdatum, NULL_TREE);
2020 datum = ref;
2022 field = TREE_CHAIN (field);
2024 while (field);
2026 return ref;
2028 else if (code != ERROR_MARK)
2029 error_at (loc,
2030 "request for member %qE in something not a structure or union",
2031 component);
2033 return error_mark_node;
2036 /* Given an expression PTR for a pointer, return an expression
2037 for the value pointed to.
2038 ERRORSTRING is the name of the operator to appear in error messages.
2040 LOC is the location to use for the generated tree. */
2042 tree
2043 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
2045 tree pointer = default_conversion (ptr);
2046 tree type = TREE_TYPE (pointer);
2047 tree ref;
2049 if (TREE_CODE (type) == POINTER_TYPE)
2051 if (CONVERT_EXPR_P (pointer)
2052 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2054 /* If a warning is issued, mark it to avoid duplicates from
2055 the backend. This only needs to be done at
2056 warn_strict_aliasing > 2. */
2057 if (warn_strict_aliasing > 2)
2058 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2059 type, TREE_OPERAND (pointer, 0)))
2060 TREE_NO_WARNING (pointer) = 1;
2063 if (TREE_CODE (pointer) == ADDR_EXPR
2064 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2065 == TREE_TYPE (type)))
2067 ref = TREE_OPERAND (pointer, 0);
2068 protected_set_expr_location (ref, loc);
2069 return ref;
2071 else
2073 tree t = TREE_TYPE (type);
2075 ref = build1 (INDIRECT_REF, t, pointer);
2077 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2079 error_at (loc, "dereferencing pointer to incomplete type");
2080 return error_mark_node;
2082 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2083 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2085 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2086 so that we get the proper error message if the result is used
2087 to assign to. Also, &* is supposed to be a no-op.
2088 And ANSI C seems to specify that the type of the result
2089 should be the const type. */
2090 /* A de-reference of a pointer to const is not a const. It is valid
2091 to change it via some other pointer. */
2092 TREE_READONLY (ref) = TYPE_READONLY (t);
2093 TREE_SIDE_EFFECTS (ref)
2094 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2095 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2096 protected_set_expr_location (ref, loc);
2097 return ref;
2100 else if (TREE_CODE (pointer) != ERROR_MARK)
2101 error_at (loc,
2102 "invalid type argument of %qs (have %qT)", errorstring, type);
2103 return error_mark_node;
2106 /* This handles expressions of the form "a[i]", which denotes
2107 an array reference.
2109 This is logically equivalent in C to *(a+i), but we may do it differently.
2110 If A is a variable or a member, we generate a primitive ARRAY_REF.
2111 This avoids forcing the array out of registers, and can work on
2112 arrays that are not lvalues (for example, members of structures returned
2113 by functions).
2115 LOC is the location to use for the returned expression. */
2117 tree
2118 build_array_ref (location_t loc, tree array, tree index)
2120 tree ret;
2121 bool swapped = false;
2122 if (TREE_TYPE (array) == error_mark_node
2123 || TREE_TYPE (index) == error_mark_node)
2124 return error_mark_node;
2126 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2127 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2129 tree temp;
2130 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2131 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2133 error_at (loc, "subscripted value is neither array nor pointer");
2134 return error_mark_node;
2136 temp = array;
2137 array = index;
2138 index = temp;
2139 swapped = true;
2142 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2144 error_at (loc, "array subscript is not an integer");
2145 return error_mark_node;
2148 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2150 error_at (loc, "subscripted value is pointer to function");
2151 return error_mark_node;
2154 /* ??? Existing practice has been to warn only when the char
2155 index is syntactically the index, not for char[array]. */
2156 if (!swapped)
2157 warn_array_subscript_with_type_char (index);
2159 /* Apply default promotions *after* noticing character types. */
2160 index = default_conversion (index);
2162 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2164 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2166 tree rval, type;
2168 /* An array that is indexed by a non-constant
2169 cannot be stored in a register; we must be able to do
2170 address arithmetic on its address.
2171 Likewise an array of elements of variable size. */
2172 if (TREE_CODE (index) != INTEGER_CST
2173 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2174 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2176 if (!c_mark_addressable (array))
2177 return error_mark_node;
2179 /* An array that is indexed by a constant value which is not within
2180 the array bounds cannot be stored in a register either; because we
2181 would get a crash in store_bit_field/extract_bit_field when trying
2182 to access a non-existent part of the register. */
2183 if (TREE_CODE (index) == INTEGER_CST
2184 && TYPE_DOMAIN (TREE_TYPE (array))
2185 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2187 if (!c_mark_addressable (array))
2188 return error_mark_node;
2191 if (pedantic)
2193 tree foo = array;
2194 while (TREE_CODE (foo) == COMPONENT_REF)
2195 foo = TREE_OPERAND (foo, 0);
2196 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2197 pedwarn (loc, OPT_pedantic,
2198 "ISO C forbids subscripting %<register%> array");
2199 else if (!flag_isoc99 && !lvalue_p (foo))
2200 pedwarn (loc, OPT_pedantic,
2201 "ISO C90 forbids subscripting non-lvalue array");
2204 type = TREE_TYPE (TREE_TYPE (array));
2205 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2206 /* Array ref is const/volatile if the array elements are
2207 or if the array is. */
2208 TREE_READONLY (rval)
2209 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2210 | TREE_READONLY (array));
2211 TREE_SIDE_EFFECTS (rval)
2212 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2213 | TREE_SIDE_EFFECTS (array));
2214 TREE_THIS_VOLATILE (rval)
2215 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2216 /* This was added by rms on 16 Nov 91.
2217 It fixes vol struct foo *a; a->elts[1]
2218 in an inline function.
2219 Hope it doesn't break something else. */
2220 | TREE_THIS_VOLATILE (array));
2221 ret = require_complete_type (rval);
2222 protected_set_expr_location (ret, loc);
2223 return ret;
2225 else
2227 tree ar = default_conversion (array);
2229 if (ar == error_mark_node)
2230 return ar;
2232 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2233 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2235 return build_indirect_ref
2236 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2237 "array indexing");
2241 /* Build an external reference to identifier ID. FUN indicates
2242 whether this will be used for a function call. LOC is the source
2243 location of the identifier. This sets *TYPE to the type of the
2244 identifier, which is not the same as the type of the returned value
2245 for CONST_DECLs defined as enum constants. If the type of the
2246 identifier is not available, *TYPE is set to NULL. */
2247 tree
2248 build_external_ref (location_t loc, tree id, int fun, tree *type)
2250 tree ref;
2251 tree decl = lookup_name (id);
2253 /* In Objective-C, an instance variable (ivar) may be preferred to
2254 whatever lookup_name() found. */
2255 decl = objc_lookup_ivar (decl, id);
2257 *type = NULL;
2258 if (decl && decl != error_mark_node)
2260 ref = decl;
2261 *type = TREE_TYPE (ref);
2263 else if (fun)
2264 /* Implicit function declaration. */
2265 ref = implicitly_declare (loc, id);
2266 else if (decl == error_mark_node)
2267 /* Don't complain about something that's already been
2268 complained about. */
2269 return error_mark_node;
2270 else
2272 undeclared_variable (loc, id);
2273 return error_mark_node;
2276 if (TREE_TYPE (ref) == error_mark_node)
2277 return error_mark_node;
2279 if (TREE_DEPRECATED (ref))
2280 warn_deprecated_use (ref, NULL_TREE);
2282 /* Recursive call does not count as usage. */
2283 if (ref != current_function_decl)
2285 TREE_USED (ref) = 1;
2288 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2290 if (!in_sizeof && !in_typeof)
2291 C_DECL_USED (ref) = 1;
2292 else if (DECL_INITIAL (ref) == 0
2293 && DECL_EXTERNAL (ref)
2294 && !TREE_PUBLIC (ref))
2295 record_maybe_used_decl (ref);
2298 if (TREE_CODE (ref) == CONST_DECL)
2300 used_types_insert (TREE_TYPE (ref));
2302 if (warn_cxx_compat
2303 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2304 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2306 warning_at (loc, OPT_Wc___compat,
2307 ("enum constant defined in struct or union "
2308 "is not visible in C++"));
2309 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2312 ref = DECL_INITIAL (ref);
2313 TREE_CONSTANT (ref) = 1;
2315 else if (current_function_decl != 0
2316 && !DECL_FILE_SCOPE_P (current_function_decl)
2317 && (TREE_CODE (ref) == VAR_DECL
2318 || TREE_CODE (ref) == PARM_DECL
2319 || TREE_CODE (ref) == FUNCTION_DECL))
2321 tree context = decl_function_context (ref);
2323 if (context != 0 && context != current_function_decl)
2324 DECL_NONLOCAL (ref) = 1;
2326 /* C99 6.7.4p3: An inline definition of a function with external
2327 linkage ... shall not contain a reference to an identifier with
2328 internal linkage. */
2329 else if (current_function_decl != 0
2330 && DECL_DECLARED_INLINE_P (current_function_decl)
2331 && DECL_EXTERNAL (current_function_decl)
2332 && VAR_OR_FUNCTION_DECL_P (ref)
2333 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2334 && ! TREE_PUBLIC (ref)
2335 && DECL_CONTEXT (ref) != current_function_decl)
2336 record_inline_static (loc, current_function_decl, ref,
2337 csi_internal);
2339 return ref;
2342 /* Record details of decls possibly used inside sizeof or typeof. */
2343 struct maybe_used_decl
2345 /* The decl. */
2346 tree decl;
2347 /* The level seen at (in_sizeof + in_typeof). */
2348 int level;
2349 /* The next one at this level or above, or NULL. */
2350 struct maybe_used_decl *next;
2353 static struct maybe_used_decl *maybe_used_decls;
2355 /* Record that DECL, an undefined static function reference seen
2356 inside sizeof or typeof, might be used if the operand of sizeof is
2357 a VLA type or the operand of typeof is a variably modified
2358 type. */
2360 static void
2361 record_maybe_used_decl (tree decl)
2363 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2364 t->decl = decl;
2365 t->level = in_sizeof + in_typeof;
2366 t->next = maybe_used_decls;
2367 maybe_used_decls = t;
2370 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2371 USED is false, just discard them. If it is true, mark them used
2372 (if no longer inside sizeof or typeof) or move them to the next
2373 level up (if still inside sizeof or typeof). */
2375 void
2376 pop_maybe_used (bool used)
2378 struct maybe_used_decl *p = maybe_used_decls;
2379 int cur_level = in_sizeof + in_typeof;
2380 while (p && p->level > cur_level)
2382 if (used)
2384 if (cur_level == 0)
2385 C_DECL_USED (p->decl) = 1;
2386 else
2387 p->level = cur_level;
2389 p = p->next;
2391 if (!used || cur_level == 0)
2392 maybe_used_decls = p;
2395 /* Return the result of sizeof applied to EXPR. */
2397 struct c_expr
2398 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2400 struct c_expr ret;
2401 if (expr.value == error_mark_node)
2403 ret.value = error_mark_node;
2404 ret.original_code = ERROR_MARK;
2405 ret.original_type = NULL;
2406 pop_maybe_used (false);
2408 else
2410 bool expr_const_operands = true;
2411 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2412 &expr_const_operands);
2413 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2414 ret.original_code = ERROR_MARK;
2415 ret.original_type = NULL;
2416 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2418 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2419 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2420 folded_expr, ret.value);
2421 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2422 SET_EXPR_LOCATION (ret.value, loc);
2424 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2426 return ret;
2429 /* Return the result of sizeof applied to T, a structure for the type
2430 name passed to sizeof (rather than the type itself). LOC is the
2431 location of the original expression. */
2433 struct c_expr
2434 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2436 tree type;
2437 struct c_expr ret;
2438 tree type_expr = NULL_TREE;
2439 bool type_expr_const = true;
2440 type = groktypename (t, &type_expr, &type_expr_const);
2441 ret.value = c_sizeof (loc, type);
2442 ret.original_code = ERROR_MARK;
2443 ret.original_type = NULL;
2444 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2445 && c_vla_type_p (type))
2447 /* If the type is a [*] array, it is a VLA but is represented as
2448 having a size of zero. In such a case we must ensure that
2449 the result of sizeof does not get folded to a constant by
2450 c_fully_fold, because if the size is evaluated the result is
2451 not constant and so constraints on zero or negative size
2452 arrays must not be applied when this sizeof call is inside
2453 another array declarator. */
2454 if (!type_expr)
2455 type_expr = integer_zero_node;
2456 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2457 type_expr, ret.value);
2458 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2460 pop_maybe_used (type != error_mark_node
2461 ? C_TYPE_VARIABLE_SIZE (type) : false);
2462 return ret;
2465 /* Build a function call to function FUNCTION with parameters PARAMS.
2466 The function call is at LOC.
2467 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2468 TREE_VALUE of each node is a parameter-expression.
2469 FUNCTION's data type may be a function type or a pointer-to-function. */
2471 tree
2472 build_function_call (location_t loc, tree function, tree params)
2474 VEC(tree,gc) *vec;
2475 tree ret;
2477 vec = VEC_alloc (tree, gc, list_length (params));
2478 for (; params; params = TREE_CHAIN (params))
2479 VEC_quick_push (tree, vec, TREE_VALUE (params));
2480 ret = build_function_call_vec (loc, function, vec, NULL);
2481 VEC_free (tree, gc, vec);
2482 return ret;
2485 /* Build a function call to function FUNCTION with parameters PARAMS.
2486 ORIGTYPES, if not NULL, is a vector of types; each element is
2487 either NULL or the original type of the corresponding element in
2488 PARAMS. The original type may differ from TREE_TYPE of the
2489 parameter for enums. FUNCTION's data type may be a function type
2490 or pointer-to-function. This function changes the elements of
2491 PARAMS. */
2493 tree
2494 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2495 VEC(tree,gc) *origtypes)
2497 tree fntype, fundecl = 0;
2498 tree name = NULL_TREE, result;
2499 tree tem;
2500 int nargs;
2501 tree *argarray;
2504 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2505 STRIP_TYPE_NOPS (function);
2507 /* Convert anything with function type to a pointer-to-function. */
2508 if (TREE_CODE (function) == FUNCTION_DECL)
2510 /* Implement type-directed function overloading for builtins.
2511 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2512 handle all the type checking. The result is a complete expression
2513 that implements this function call. */
2514 tem = resolve_overloaded_builtin (loc, function, params);
2515 if (tem)
2516 return tem;
2518 name = DECL_NAME (function);
2519 fundecl = function;
2521 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2522 function = function_to_pointer_conversion (loc, function);
2524 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2525 expressions, like those used for ObjC messenger dispatches. */
2526 if (!VEC_empty (tree, params))
2527 function = objc_rewrite_function_call (function,
2528 VEC_index (tree, params, 0));
2530 function = c_fully_fold (function, false, NULL);
2532 fntype = TREE_TYPE (function);
2534 if (TREE_CODE (fntype) == ERROR_MARK)
2535 return error_mark_node;
2537 if (!(TREE_CODE (fntype) == POINTER_TYPE
2538 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2540 error_at (loc, "called object %qE is not a function", function);
2541 return error_mark_node;
2544 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2545 current_function_returns_abnormally = 1;
2547 /* fntype now gets the type of function pointed to. */
2548 fntype = TREE_TYPE (fntype);
2550 /* Convert the parameters to the types declared in the
2551 function prototype, or apply default promotions. */
2553 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2554 function, fundecl);
2555 if (nargs < 0)
2556 return error_mark_node;
2558 /* Check that the function is called through a compatible prototype.
2559 If it is not, replace the call by a trap, wrapped up in a compound
2560 expression if necessary. This has the nice side-effect to prevent
2561 the tree-inliner from generating invalid assignment trees which may
2562 blow up in the RTL expander later. */
2563 if (CONVERT_EXPR_P (function)
2564 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2565 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2566 && !comptypes (fntype, TREE_TYPE (tem)))
2568 tree return_type = TREE_TYPE (fntype);
2569 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2570 NULL_TREE);
2571 int i;
2573 /* This situation leads to run-time undefined behavior. We can't,
2574 therefore, simply error unless we can prove that all possible
2575 executions of the program must execute the code. */
2576 if (warning_at (loc, 0, "function called through a non-compatible type"))
2577 /* We can, however, treat "undefined" any way we please.
2578 Call abort to encourage the user to fix the program. */
2579 inform (loc, "if this code is reached, the program will abort");
2580 /* Before the abort, allow the function arguments to exit or
2581 call longjmp. */
2582 for (i = 0; i < nargs; i++)
2583 trap = build2 (COMPOUND_EXPR, void_type_node,
2584 VEC_index (tree, params, i), trap);
2586 if (VOID_TYPE_P (return_type))
2588 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2589 pedwarn (input_location, 0,
2590 "function with qualified void return type called");
2591 return trap;
2593 else
2595 tree rhs;
2597 if (AGGREGATE_TYPE_P (return_type))
2598 rhs = build_compound_literal (loc, return_type,
2599 build_constructor (return_type, 0),
2600 false);
2601 else
2602 rhs = fold_convert (return_type, integer_zero_node);
2604 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2605 trap, rhs));
2609 argarray = VEC_address (tree, params);
2611 /* Check that arguments to builtin functions match the expectations. */
2612 if (fundecl
2613 && DECL_BUILT_IN (fundecl)
2614 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2615 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2616 return error_mark_node;
2618 /* Check that the arguments to the function are valid. */
2619 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2620 TYPE_ARG_TYPES (fntype));
2622 if (name != NULL_TREE
2623 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2625 if (require_constant_value)
2626 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2627 function, nargs, argarray);
2628 else
2629 result = fold_build_call_array (TREE_TYPE (fntype),
2630 function, nargs, argarray);
2631 if (TREE_CODE (result) == NOP_EXPR
2632 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2633 STRIP_TYPE_NOPS (result);
2635 else
2636 result = build_call_array (TREE_TYPE (fntype),
2637 function, nargs, argarray);
2639 if (VOID_TYPE_P (TREE_TYPE (result)))
2641 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2642 pedwarn (input_location, 0,
2643 "function with qualified void return type called");
2644 return result;
2646 return require_complete_type (result);
2649 /* Convert the argument expressions in the vector VALUES
2650 to the types in the list TYPELIST.
2652 If TYPELIST is exhausted, or when an element has NULL as its type,
2653 perform the default conversions.
2655 ORIGTYPES is the original types of the expressions in VALUES. This
2656 holds the type of enum values which have been converted to integral
2657 types. It may be NULL.
2659 FUNCTION is a tree for the called function. It is used only for
2660 error messages, where it is formatted with %qE.
2662 This is also where warnings about wrong number of args are generated.
2664 Returns the actual number of arguments processed (which may be less
2665 than the length of VALUES in some error situations), or -1 on
2666 failure. */
2668 static int
2669 convert_arguments (tree typelist, VEC(tree,gc) *values,
2670 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2672 tree typetail, val;
2673 unsigned int parmnum;
2674 const bool type_generic = fundecl
2675 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2676 bool type_generic_remove_excess_precision = false;
2677 tree selector;
2679 /* Change pointer to function to the function itself for
2680 diagnostics. */
2681 if (TREE_CODE (function) == ADDR_EXPR
2682 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2683 function = TREE_OPERAND (function, 0);
2685 /* Handle an ObjC selector specially for diagnostics. */
2686 selector = objc_message_selector ();
2688 /* For type-generic built-in functions, determine whether excess
2689 precision should be removed (classification) or not
2690 (comparison). */
2691 if (type_generic
2692 && DECL_BUILT_IN (fundecl)
2693 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2695 switch (DECL_FUNCTION_CODE (fundecl))
2697 case BUILT_IN_ISFINITE:
2698 case BUILT_IN_ISINF:
2699 case BUILT_IN_ISINF_SIGN:
2700 case BUILT_IN_ISNAN:
2701 case BUILT_IN_ISNORMAL:
2702 case BUILT_IN_FPCLASSIFY:
2703 type_generic_remove_excess_precision = true;
2704 break;
2706 default:
2707 type_generic_remove_excess_precision = false;
2708 break;
2712 /* Scan the given expressions and types, producing individual
2713 converted arguments. */
2715 for (typetail = typelist, parmnum = 0;
2716 VEC_iterate (tree, values, parmnum, val);
2717 ++parmnum)
2719 tree type = typetail ? TREE_VALUE (typetail) : 0;
2720 tree valtype = TREE_TYPE (val);
2721 tree rname = function;
2722 int argnum = parmnum + 1;
2723 const char *invalid_func_diag;
2724 bool excess_precision = false;
2725 bool npc;
2726 tree parmval;
2728 if (type == void_type_node)
2730 error ("too many arguments to function %qE", function);
2731 return parmnum;
2734 if (selector && argnum > 2)
2736 rname = selector;
2737 argnum -= 2;
2740 npc = null_pointer_constant_p (val);
2742 /* If there is excess precision and a prototype, convert once to
2743 the required type rather than converting via the semantic
2744 type. Likewise without a prototype a float value represented
2745 as long double should be converted once to double. But for
2746 type-generic classification functions excess precision must
2747 be removed here. */
2748 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2749 && (type || !type_generic || !type_generic_remove_excess_precision))
2751 val = TREE_OPERAND (val, 0);
2752 excess_precision = true;
2754 val = c_fully_fold (val, false, NULL);
2755 STRIP_TYPE_NOPS (val);
2757 val = require_complete_type (val);
2759 if (type != 0)
2761 /* Formal parm type is specified by a function prototype. */
2763 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2765 error ("type of formal parameter %d is incomplete", parmnum + 1);
2766 parmval = val;
2768 else
2770 tree origtype;
2772 /* Optionally warn about conversions that
2773 differ from the default conversions. */
2774 if (warn_traditional_conversion || warn_traditional)
2776 unsigned int formal_prec = TYPE_PRECISION (type);
2778 if (INTEGRAL_TYPE_P (type)
2779 && TREE_CODE (valtype) == REAL_TYPE)
2780 warning (0, "passing argument %d of %qE as integer "
2781 "rather than floating due to prototype",
2782 argnum, rname);
2783 if (INTEGRAL_TYPE_P (type)
2784 && TREE_CODE (valtype) == COMPLEX_TYPE)
2785 warning (0, "passing argument %d of %qE as integer "
2786 "rather than complex due to prototype",
2787 argnum, rname);
2788 else if (TREE_CODE (type) == COMPLEX_TYPE
2789 && TREE_CODE (valtype) == REAL_TYPE)
2790 warning (0, "passing argument %d of %qE as complex "
2791 "rather than floating due to prototype",
2792 argnum, rname);
2793 else if (TREE_CODE (type) == REAL_TYPE
2794 && INTEGRAL_TYPE_P (valtype))
2795 warning (0, "passing argument %d of %qE as floating "
2796 "rather than integer due to prototype",
2797 argnum, rname);
2798 else if (TREE_CODE (type) == COMPLEX_TYPE
2799 && INTEGRAL_TYPE_P (valtype))
2800 warning (0, "passing argument %d of %qE as complex "
2801 "rather than integer due to prototype",
2802 argnum, rname);
2803 else if (TREE_CODE (type) == REAL_TYPE
2804 && TREE_CODE (valtype) == COMPLEX_TYPE)
2805 warning (0, "passing argument %d of %qE as floating "
2806 "rather than complex due to prototype",
2807 argnum, rname);
2808 /* ??? At some point, messages should be written about
2809 conversions between complex types, but that's too messy
2810 to do now. */
2811 else if (TREE_CODE (type) == REAL_TYPE
2812 && TREE_CODE (valtype) == REAL_TYPE)
2814 /* Warn if any argument is passed as `float',
2815 since without a prototype it would be `double'. */
2816 if (formal_prec == TYPE_PRECISION (float_type_node)
2817 && type != dfloat32_type_node)
2818 warning (0, "passing argument %d of %qE as %<float%> "
2819 "rather than %<double%> due to prototype",
2820 argnum, rname);
2822 /* Warn if mismatch between argument and prototype
2823 for decimal float types. Warn of conversions with
2824 binary float types and of precision narrowing due to
2825 prototype. */
2826 else if (type != valtype
2827 && (type == dfloat32_type_node
2828 || type == dfloat64_type_node
2829 || type == dfloat128_type_node
2830 || valtype == dfloat32_type_node
2831 || valtype == dfloat64_type_node
2832 || valtype == dfloat128_type_node)
2833 && (formal_prec
2834 <= TYPE_PRECISION (valtype)
2835 || (type == dfloat128_type_node
2836 && (valtype
2837 != dfloat64_type_node
2838 && (valtype
2839 != dfloat32_type_node)))
2840 || (type == dfloat64_type_node
2841 && (valtype
2842 != dfloat32_type_node))))
2843 warning (0, "passing argument %d of %qE as %qT "
2844 "rather than %qT due to prototype",
2845 argnum, rname, type, valtype);
2848 /* Detect integer changing in width or signedness.
2849 These warnings are only activated with
2850 -Wtraditional-conversion, not with -Wtraditional. */
2851 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2852 && INTEGRAL_TYPE_P (valtype))
2854 tree would_have_been = default_conversion (val);
2855 tree type1 = TREE_TYPE (would_have_been);
2857 if (TREE_CODE (type) == ENUMERAL_TYPE
2858 && (TYPE_MAIN_VARIANT (type)
2859 == TYPE_MAIN_VARIANT (valtype)))
2860 /* No warning if function asks for enum
2861 and the actual arg is that enum type. */
2863 else if (formal_prec != TYPE_PRECISION (type1))
2864 warning (OPT_Wtraditional_conversion,
2865 "passing argument %d of %qE "
2866 "with different width due to prototype",
2867 argnum, rname);
2868 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2870 /* Don't complain if the formal parameter type
2871 is an enum, because we can't tell now whether
2872 the value was an enum--even the same enum. */
2873 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2875 else if (TREE_CODE (val) == INTEGER_CST
2876 && int_fits_type_p (val, type))
2877 /* Change in signedness doesn't matter
2878 if a constant value is unaffected. */
2880 /* If the value is extended from a narrower
2881 unsigned type, it doesn't matter whether we
2882 pass it as signed or unsigned; the value
2883 certainly is the same either way. */
2884 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2885 && TYPE_UNSIGNED (valtype))
2887 else if (TYPE_UNSIGNED (type))
2888 warning (OPT_Wtraditional_conversion,
2889 "passing argument %d of %qE "
2890 "as unsigned due to prototype",
2891 argnum, rname);
2892 else
2893 warning (OPT_Wtraditional_conversion,
2894 "passing argument %d of %qE "
2895 "as signed due to prototype", argnum, rname);
2899 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2900 sake of better warnings from convert_and_check. */
2901 if (excess_precision)
2902 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2903 origtype = (origtypes == NULL
2904 ? NULL_TREE
2905 : VEC_index (tree, origtypes, parmnum));
2906 parmval = convert_for_assignment (input_location, type, val,
2907 origtype, ic_argpass, npc,
2908 fundecl, function,
2909 parmnum + 1);
2911 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2912 && INTEGRAL_TYPE_P (type)
2913 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2914 parmval = default_conversion (parmval);
2917 else if (TREE_CODE (valtype) == REAL_TYPE
2918 && (TYPE_PRECISION (valtype)
2919 < TYPE_PRECISION (double_type_node))
2920 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2922 if (type_generic)
2923 parmval = val;
2924 else
2925 /* Convert `float' to `double'. */
2926 parmval = convert (double_type_node, val);
2928 else if (excess_precision && !type_generic)
2929 /* A "double" argument with excess precision being passed
2930 without a prototype or in variable arguments. */
2931 parmval = convert (valtype, val);
2932 else if ((invalid_func_diag =
2933 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2935 error (invalid_func_diag);
2936 return -1;
2938 else
2939 /* Convert `short' and `char' to full-size `int'. */
2940 parmval = default_conversion (val);
2942 VEC_replace (tree, values, parmnum, parmval);
2944 if (typetail)
2945 typetail = TREE_CHAIN (typetail);
2948 gcc_assert (parmnum == VEC_length (tree, values));
2950 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2952 error ("too few arguments to function %qE", function);
2953 return -1;
2956 return parmnum;
2959 /* This is the entry point used by the parser to build unary operators
2960 in the input. CODE, a tree_code, specifies the unary operator, and
2961 ARG is the operand. For unary plus, the C parser currently uses
2962 CONVERT_EXPR for code.
2964 LOC is the location to use for the tree generated.
2967 struct c_expr
2968 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
2970 struct c_expr result;
2972 result.value = build_unary_op (loc, code, arg.value, 0);
2973 result.original_code = code;
2974 result.original_type = NULL;
2976 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2977 overflow_warning (loc, result.value);
2979 return result;
2982 /* This is the entry point used by the parser to build binary operators
2983 in the input. CODE, a tree_code, specifies the binary operator, and
2984 ARG1 and ARG2 are the operands. In addition to constructing the
2985 expression, we check for operands that were written with other binary
2986 operators in a way that is likely to confuse the user.
2988 LOCATION is the location of the binary operator. */
2990 struct c_expr
2991 parser_build_binary_op (location_t location, enum tree_code code,
2992 struct c_expr arg1, struct c_expr arg2)
2994 struct c_expr result;
2996 enum tree_code code1 = arg1.original_code;
2997 enum tree_code code2 = arg2.original_code;
2998 tree type1 = (arg1.original_type
2999 ? arg1.original_type
3000 : TREE_TYPE (arg1.value));
3001 tree type2 = (arg2.original_type
3002 ? arg2.original_type
3003 : TREE_TYPE (arg2.value));
3005 result.value = build_binary_op (location, code,
3006 arg1.value, arg2.value, 1);
3007 result.original_code = code;
3008 result.original_type = NULL;
3010 if (TREE_CODE (result.value) == ERROR_MARK)
3011 return result;
3013 if (location != UNKNOWN_LOCATION)
3014 protected_set_expr_location (result.value, location);
3016 /* Check for cases such as x+y<<z which users are likely
3017 to misinterpret. */
3018 if (warn_parentheses)
3019 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3021 if (warn_logical_op)
3022 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3023 code1, arg1.value, code2, arg2.value);
3025 /* Warn about comparisons against string literals, with the exception
3026 of testing for equality or inequality of a string literal with NULL. */
3027 if (code == EQ_EXPR || code == NE_EXPR)
3029 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3030 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3031 warning_at (location, OPT_Waddress,
3032 "comparison with string literal results in unspecified behavior");
3034 else if (TREE_CODE_CLASS (code) == tcc_comparison
3035 && (code1 == STRING_CST || code2 == STRING_CST))
3036 warning_at (location, OPT_Waddress,
3037 "comparison with string literal results in unspecified behavior");
3039 if (TREE_OVERFLOW_P (result.value)
3040 && !TREE_OVERFLOW_P (arg1.value)
3041 && !TREE_OVERFLOW_P (arg2.value))
3042 overflow_warning (location, result.value);
3044 /* Warn about comparisons of different enum types. */
3045 if (warn_enum_compare
3046 && TREE_CODE_CLASS (code) == tcc_comparison
3047 && TREE_CODE (type1) == ENUMERAL_TYPE
3048 && TREE_CODE (type2) == ENUMERAL_TYPE
3049 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3050 warning_at (location, OPT_Wenum_compare,
3051 "comparison between %qT and %qT",
3052 type1, type2);
3054 return result;
3057 /* Return a tree for the difference of pointers OP0 and OP1.
3058 The resulting tree has type int. */
3060 static tree
3061 pointer_diff (tree op0, tree op1)
3063 tree restype = ptrdiff_type_node;
3065 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3066 tree con0, con1, lit0, lit1;
3067 tree orig_op1 = op1;
3069 if (TREE_CODE (target_type) == VOID_TYPE)
3070 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3071 "pointer of type %<void *%> used in subtraction");
3072 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3073 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3074 "pointer to a function used in subtraction");
3076 /* If the conversion to ptrdiff_type does anything like widening or
3077 converting a partial to an integral mode, we get a convert_expression
3078 that is in the way to do any simplifications.
3079 (fold-const.c doesn't know that the extra bits won't be needed.
3080 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3081 different mode in place.)
3082 So first try to find a common term here 'by hand'; we want to cover
3083 at least the cases that occur in legal static initializers. */
3084 if (CONVERT_EXPR_P (op0)
3085 && (TYPE_PRECISION (TREE_TYPE (op0))
3086 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3087 con0 = TREE_OPERAND (op0, 0);
3088 else
3089 con0 = op0;
3090 if (CONVERT_EXPR_P (op1)
3091 && (TYPE_PRECISION (TREE_TYPE (op1))
3092 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3093 con1 = TREE_OPERAND (op1, 0);
3094 else
3095 con1 = op1;
3097 if (TREE_CODE (con0) == PLUS_EXPR)
3099 lit0 = TREE_OPERAND (con0, 1);
3100 con0 = TREE_OPERAND (con0, 0);
3102 else
3103 lit0 = integer_zero_node;
3105 if (TREE_CODE (con1) == PLUS_EXPR)
3107 lit1 = TREE_OPERAND (con1, 1);
3108 con1 = TREE_OPERAND (con1, 0);
3110 else
3111 lit1 = integer_zero_node;
3113 if (operand_equal_p (con0, con1, 0))
3115 op0 = lit0;
3116 op1 = lit1;
3120 /* First do the subtraction as integers;
3121 then drop through to build the divide operator.
3122 Do not do default conversions on the minus operator
3123 in case restype is a short type. */
3125 op0 = build_binary_op (input_location,
3126 MINUS_EXPR, convert (restype, op0),
3127 convert (restype, op1), 0);
3128 /* This generates an error if op1 is pointer to incomplete type. */
3129 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3130 error ("arithmetic on pointer to an incomplete type");
3132 /* This generates an error if op0 is pointer to incomplete type. */
3133 op1 = c_size_in_bytes (target_type);
3135 /* Divide by the size, in easiest possible way. */
3136 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3139 /* Construct and perhaps optimize a tree representation
3140 for a unary operation. CODE, a tree_code, specifies the operation
3141 and XARG is the operand.
3142 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3143 the default promotions (such as from short to int).
3144 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3145 allows non-lvalues; this is only used to handle conversion of non-lvalue
3146 arrays to pointers in C99.
3148 LOCATION is the location of the operator. */
3150 tree
3151 build_unary_op (location_t location,
3152 enum tree_code code, tree xarg, int flag)
3154 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3155 tree arg = xarg;
3156 tree argtype = 0;
3157 enum tree_code typecode;
3158 tree val;
3159 tree ret = error_mark_node;
3160 tree eptype = NULL_TREE;
3161 int noconvert = flag;
3162 const char *invalid_op_diag;
3163 bool int_operands;
3165 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3166 if (int_operands)
3167 arg = remove_c_maybe_const_expr (arg);
3169 if (code != ADDR_EXPR)
3170 arg = require_complete_type (arg);
3172 typecode = TREE_CODE (TREE_TYPE (arg));
3173 if (typecode == ERROR_MARK)
3174 return error_mark_node;
3175 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3176 typecode = INTEGER_TYPE;
3178 if ((invalid_op_diag
3179 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3181 error_at (location, invalid_op_diag);
3182 return error_mark_node;
3185 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3187 eptype = TREE_TYPE (arg);
3188 arg = TREE_OPERAND (arg, 0);
3191 switch (code)
3193 case CONVERT_EXPR:
3194 /* This is used for unary plus, because a CONVERT_EXPR
3195 is enough to prevent anybody from looking inside for
3196 associativity, but won't generate any code. */
3197 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3198 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3199 || typecode == VECTOR_TYPE))
3201 error_at (location, "wrong type argument to unary plus");
3202 return error_mark_node;
3204 else if (!noconvert)
3205 arg = default_conversion (arg);
3206 arg = non_lvalue (arg);
3207 break;
3209 case NEGATE_EXPR:
3210 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3211 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3212 || typecode == VECTOR_TYPE))
3214 error_at (location, "wrong type argument to unary minus");
3215 return error_mark_node;
3217 else if (!noconvert)
3218 arg = default_conversion (arg);
3219 break;
3221 case BIT_NOT_EXPR:
3222 /* ~ works on integer types and non float vectors. */
3223 if (typecode == INTEGER_TYPE
3224 || (typecode == VECTOR_TYPE
3225 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3227 if (!noconvert)
3228 arg = default_conversion (arg);
3230 else if (typecode == COMPLEX_TYPE)
3232 code = CONJ_EXPR;
3233 pedwarn (location, OPT_pedantic,
3234 "ISO C does not support %<~%> for complex conjugation");
3235 if (!noconvert)
3236 arg = default_conversion (arg);
3238 else
3240 error_at (location, "wrong type argument to bit-complement");
3241 return error_mark_node;
3243 break;
3245 case ABS_EXPR:
3246 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3248 error_at (location, "wrong type argument to abs");
3249 return error_mark_node;
3251 else if (!noconvert)
3252 arg = default_conversion (arg);
3253 break;
3255 case CONJ_EXPR:
3256 /* Conjugating a real value is a no-op, but allow it anyway. */
3257 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3258 || typecode == COMPLEX_TYPE))
3260 error_at (location, "wrong type argument to conjugation");
3261 return error_mark_node;
3263 else if (!noconvert)
3264 arg = default_conversion (arg);
3265 break;
3267 case TRUTH_NOT_EXPR:
3268 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3269 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3270 && typecode != COMPLEX_TYPE)
3272 error_at (location,
3273 "wrong type argument to unary exclamation mark");
3274 return error_mark_node;
3276 arg = c_objc_common_truthvalue_conversion (location, arg);
3277 ret = invert_truthvalue (arg);
3278 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3279 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3280 location = EXPR_LOCATION (ret);
3281 goto return_build_unary_op;
3283 case REALPART_EXPR:
3284 if (TREE_CODE (arg) == COMPLEX_CST)
3285 ret = TREE_REALPART (arg);
3286 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3287 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3288 else
3289 ret = arg;
3290 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3291 eptype = TREE_TYPE (eptype);
3292 goto return_build_unary_op;
3294 case IMAGPART_EXPR:
3295 if (TREE_CODE (arg) == COMPLEX_CST)
3296 ret = TREE_IMAGPART (arg);
3297 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3298 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3299 else
3300 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3301 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3302 eptype = TREE_TYPE (eptype);
3303 goto return_build_unary_op;
3305 case PREINCREMENT_EXPR:
3306 case POSTINCREMENT_EXPR:
3307 case PREDECREMENT_EXPR:
3308 case POSTDECREMENT_EXPR:
3310 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3312 tree inner = build_unary_op (location, code,
3313 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3314 if (inner == error_mark_node)
3315 return error_mark_node;
3316 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3317 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3318 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3319 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3320 goto return_build_unary_op;
3323 /* Complain about anything that is not a true lvalue. */
3324 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3325 || code == POSTINCREMENT_EXPR)
3326 ? lv_increment
3327 : lv_decrement)))
3328 return error_mark_node;
3330 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3332 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3333 warning_at (location, OPT_Wc___compat,
3334 "increment of enumeration value is invalid in C++");
3335 else
3336 warning_at (location, OPT_Wc___compat,
3337 "decrement of enumeration value is invalid in C++");
3340 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3341 arg = c_fully_fold (arg, false, NULL);
3343 /* Increment or decrement the real part of the value,
3344 and don't change the imaginary part. */
3345 if (typecode == COMPLEX_TYPE)
3347 tree real, imag;
3349 pedwarn (location, OPT_pedantic,
3350 "ISO C does not support %<++%> and %<--%> on complex types");
3352 arg = stabilize_reference (arg);
3353 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3354 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3355 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3356 if (real == error_mark_node || imag == error_mark_node)
3357 return error_mark_node;
3358 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3359 real, imag);
3360 goto return_build_unary_op;
3363 /* Report invalid types. */
3365 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3366 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3368 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3369 error_at (location, "wrong type argument to increment");
3370 else
3371 error_at (location, "wrong type argument to decrement");
3373 return error_mark_node;
3377 tree inc;
3379 argtype = TREE_TYPE (arg);
3381 /* Compute the increment. */
3383 if (typecode == POINTER_TYPE)
3385 /* If pointer target is an undefined struct,
3386 we just cannot know how to do the arithmetic. */
3387 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3389 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3390 error_at (location,
3391 "increment of pointer to unknown structure");
3392 else
3393 error_at (location,
3394 "decrement of pointer to unknown structure");
3396 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3397 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3399 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3400 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3401 "wrong type argument to increment");
3402 else
3403 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3404 "wrong type argument to decrement");
3407 inc = c_size_in_bytes (TREE_TYPE (argtype));
3408 inc = fold_convert (sizetype, inc);
3410 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3412 /* For signed fract types, we invert ++ to -- or
3413 -- to ++, and change inc from 1 to -1, because
3414 it is not possible to represent 1 in signed fract constants.
3415 For unsigned fract types, the result always overflows and
3416 we get an undefined (original) or the maximum value. */
3417 if (code == PREINCREMENT_EXPR)
3418 code = PREDECREMENT_EXPR;
3419 else if (code == PREDECREMENT_EXPR)
3420 code = PREINCREMENT_EXPR;
3421 else if (code == POSTINCREMENT_EXPR)
3422 code = POSTDECREMENT_EXPR;
3423 else /* code == POSTDECREMENT_EXPR */
3424 code = POSTINCREMENT_EXPR;
3426 inc = integer_minus_one_node;
3427 inc = convert (argtype, inc);
3429 else
3431 inc = integer_one_node;
3432 inc = convert (argtype, inc);
3435 /* Report a read-only lvalue. */
3436 if (TYPE_READONLY (argtype))
3438 readonly_error (arg,
3439 ((code == PREINCREMENT_EXPR
3440 || code == POSTINCREMENT_EXPR)
3441 ? lv_increment : lv_decrement));
3442 return error_mark_node;
3444 else if (TREE_READONLY (arg))
3445 readonly_warning (arg,
3446 ((code == PREINCREMENT_EXPR
3447 || code == POSTINCREMENT_EXPR)
3448 ? lv_increment : lv_decrement));
3450 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3451 val = boolean_increment (code, arg);
3452 else
3453 val = build2 (code, TREE_TYPE (arg), arg, inc);
3454 TREE_SIDE_EFFECTS (val) = 1;
3455 if (TREE_CODE (val) != code)
3456 TREE_NO_WARNING (val) = 1;
3457 ret = val;
3458 goto return_build_unary_op;
3461 case ADDR_EXPR:
3462 /* Note that this operation never does default_conversion. */
3464 /* The operand of unary '&' must be an lvalue (which excludes
3465 expressions of type void), or, in C99, the result of a [] or
3466 unary '*' operator. */
3467 if (VOID_TYPE_P (TREE_TYPE (arg))
3468 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3469 && (TREE_CODE (arg) != INDIRECT_REF
3470 || !flag_isoc99))
3471 pedwarn (location, 0, "taking address of expression of type %<void%>");
3473 /* Let &* cancel out to simplify resulting code. */
3474 if (TREE_CODE (arg) == INDIRECT_REF)
3476 /* Don't let this be an lvalue. */
3477 if (lvalue_p (TREE_OPERAND (arg, 0)))
3478 return non_lvalue (TREE_OPERAND (arg, 0));
3479 ret = TREE_OPERAND (arg, 0);
3480 goto return_build_unary_op;
3483 /* For &x[y], return x+y */
3484 if (TREE_CODE (arg) == ARRAY_REF)
3486 tree op0 = TREE_OPERAND (arg, 0);
3487 if (!c_mark_addressable (op0))
3488 return error_mark_node;
3489 return build_binary_op (location, PLUS_EXPR,
3490 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3491 ? array_to_pointer_conversion (location,
3492 op0)
3493 : op0),
3494 TREE_OPERAND (arg, 1), 1);
3497 /* Anything not already handled and not a true memory reference
3498 or a non-lvalue array is an error. */
3499 else if (typecode != FUNCTION_TYPE && !flag
3500 && !lvalue_or_else (arg, lv_addressof))
3501 return error_mark_node;
3503 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3504 folding later. */
3505 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3507 tree inner = build_unary_op (location, code,
3508 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3509 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3510 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3511 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3512 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3513 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3514 goto return_build_unary_op;
3517 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3518 argtype = TREE_TYPE (arg);
3520 /* If the lvalue is const or volatile, merge that into the type
3521 to which the address will point. Note that you can't get a
3522 restricted pointer by taking the address of something, so we
3523 only have to deal with `const' and `volatile' here. */
3524 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3525 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3526 argtype = c_build_type_variant (argtype,
3527 TREE_READONLY (arg),
3528 TREE_THIS_VOLATILE (arg));
3530 if (!c_mark_addressable (arg))
3531 return error_mark_node;
3533 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3534 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3536 argtype = build_pointer_type (argtype);
3538 /* ??? Cope with user tricks that amount to offsetof. Delete this
3539 when we have proper support for integer constant expressions. */
3540 val = get_base_address (arg);
3541 if (val && TREE_CODE (val) == INDIRECT_REF
3542 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3544 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3546 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3547 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3548 goto return_build_unary_op;
3551 val = build1 (ADDR_EXPR, argtype, arg);
3553 ret = val;
3554 goto return_build_unary_op;
3556 default:
3557 gcc_unreachable ();
3560 if (argtype == 0)
3561 argtype = TREE_TYPE (arg);
3562 if (TREE_CODE (arg) == INTEGER_CST)
3563 ret = (require_constant_value
3564 ? fold_build1_initializer (code, argtype, arg)
3565 : fold_build1 (code, argtype, arg));
3566 else
3567 ret = build1 (code, argtype, arg);
3568 return_build_unary_op:
3569 gcc_assert (ret != error_mark_node);
3570 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3571 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3572 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3573 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3574 ret = note_integer_operands (ret);
3575 if (eptype)
3576 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3577 protected_set_expr_location (ret, location);
3578 return ret;
3581 /* Return nonzero if REF is an lvalue valid for this language.
3582 Lvalues can be assigned, unless their type has TYPE_READONLY.
3583 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3585 bool
3586 lvalue_p (const_tree ref)
3588 const enum tree_code code = TREE_CODE (ref);
3590 switch (code)
3592 case REALPART_EXPR:
3593 case IMAGPART_EXPR:
3594 case COMPONENT_REF:
3595 return lvalue_p (TREE_OPERAND (ref, 0));
3597 case C_MAYBE_CONST_EXPR:
3598 return lvalue_p (TREE_OPERAND (ref, 1));
3600 case COMPOUND_LITERAL_EXPR:
3601 case STRING_CST:
3602 return 1;
3604 case INDIRECT_REF:
3605 case ARRAY_REF:
3606 case VAR_DECL:
3607 case PARM_DECL:
3608 case RESULT_DECL:
3609 case ERROR_MARK:
3610 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3611 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3613 case BIND_EXPR:
3614 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3616 default:
3617 return 0;
3621 /* Give an error for storing in something that is 'const'. */
3623 static void
3624 readonly_error (tree arg, enum lvalue_use use)
3626 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3627 || use == lv_asm);
3628 /* Using this macro rather than (for example) arrays of messages
3629 ensures that all the format strings are checked at compile
3630 time. */
3631 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3632 : (use == lv_increment ? (I) \
3633 : (use == lv_decrement ? (D) : (AS))))
3634 if (TREE_CODE (arg) == COMPONENT_REF)
3636 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3637 readonly_error (TREE_OPERAND (arg, 0), use);
3638 else
3639 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3640 G_("increment of read-only member %qD"),
3641 G_("decrement of read-only member %qD"),
3642 G_("read-only member %qD used as %<asm%> output")),
3643 TREE_OPERAND (arg, 1));
3645 else if (TREE_CODE (arg) == VAR_DECL)
3646 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3647 G_("increment of read-only variable %qD"),
3648 G_("decrement of read-only variable %qD"),
3649 G_("read-only variable %qD used as %<asm%> output")),
3650 arg);
3651 else
3652 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3653 G_("increment of read-only location %qE"),
3654 G_("decrement of read-only location %qE"),
3655 G_("read-only location %qE used as %<asm%> output")),
3656 arg);
3659 /* Give a warning for storing in something that is read-only in GCC
3660 terms but not const in ISO C terms. */
3662 static void
3663 readonly_warning (tree arg, enum lvalue_use use)
3665 switch (use)
3667 case lv_assign:
3668 warning (0, "assignment of read-only location %qE", arg);
3669 break;
3670 case lv_increment:
3671 warning (0, "increment of read-only location %qE", arg);
3672 break;
3673 case lv_decrement:
3674 warning (0, "decrement of read-only location %qE", arg);
3675 break;
3676 default:
3677 gcc_unreachable ();
3679 return;
3683 /* Return nonzero if REF is an lvalue valid for this language;
3684 otherwise, print an error message and return zero. USE says
3685 how the lvalue is being used and so selects the error message. */
3687 static int
3688 lvalue_or_else (const_tree ref, enum lvalue_use use)
3690 int win = lvalue_p (ref);
3692 if (!win)
3693 lvalue_error (use);
3695 return win;
3698 /* Mark EXP saying that we need to be able to take the
3699 address of it; it should not be allocated in a register.
3700 Returns true if successful. */
3702 bool
3703 c_mark_addressable (tree exp)
3705 tree x = exp;
3707 while (1)
3708 switch (TREE_CODE (x))
3710 case COMPONENT_REF:
3711 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3713 error
3714 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3715 return false;
3718 /* ... fall through ... */
3720 case ADDR_EXPR:
3721 case ARRAY_REF:
3722 case REALPART_EXPR:
3723 case IMAGPART_EXPR:
3724 x = TREE_OPERAND (x, 0);
3725 break;
3727 case COMPOUND_LITERAL_EXPR:
3728 case CONSTRUCTOR:
3729 TREE_ADDRESSABLE (x) = 1;
3730 return true;
3732 case VAR_DECL:
3733 case CONST_DECL:
3734 case PARM_DECL:
3735 case RESULT_DECL:
3736 if (C_DECL_REGISTER (x)
3737 && DECL_NONLOCAL (x))
3739 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3741 error
3742 ("global register variable %qD used in nested function", x);
3743 return false;
3745 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3747 else if (C_DECL_REGISTER (x))
3749 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3750 error ("address of global register variable %qD requested", x);
3751 else
3752 error ("address of register variable %qD requested", x);
3753 return false;
3756 /* drops in */
3757 case FUNCTION_DECL:
3758 TREE_ADDRESSABLE (x) = 1;
3759 /* drops out */
3760 default:
3761 return true;
3765 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3766 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3767 if folded to an integer constant then the unselected half may
3768 contain arbitrary operations not normally permitted in constant
3769 expressions. Set the location of the expression to LOC. */
3771 tree
3772 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
3773 tree op1, tree op1_original_type, tree op2,
3774 tree op2_original_type)
3776 tree type1;
3777 tree type2;
3778 enum tree_code code1;
3779 enum tree_code code2;
3780 tree result_type = NULL;
3781 tree ep_result_type = NULL;
3782 tree orig_op1 = op1, orig_op2 = op2;
3783 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3784 bool ifexp_int_operands;
3785 tree ret;
3786 bool objc_ok;
3788 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3789 if (op1_int_operands)
3790 op1 = remove_c_maybe_const_expr (op1);
3791 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3792 if (op2_int_operands)
3793 op2 = remove_c_maybe_const_expr (op2);
3794 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3795 if (ifexp_int_operands)
3796 ifexp = remove_c_maybe_const_expr (ifexp);
3798 /* Promote both alternatives. */
3800 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3801 op1 = default_conversion (op1);
3802 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3803 op2 = default_conversion (op2);
3805 if (TREE_CODE (ifexp) == ERROR_MARK
3806 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3807 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3808 return error_mark_node;
3810 type1 = TREE_TYPE (op1);
3811 code1 = TREE_CODE (type1);
3812 type2 = TREE_TYPE (op2);
3813 code2 = TREE_CODE (type2);
3815 /* C90 does not permit non-lvalue arrays in conditional expressions.
3816 In C99 they will be pointers by now. */
3817 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3819 error_at (colon_loc, "non-lvalue array in conditional expression");
3820 return error_mark_node;
3823 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3825 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3826 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3827 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3828 || code1 == COMPLEX_TYPE)
3829 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3830 || code2 == COMPLEX_TYPE))
3832 ep_result_type = c_common_type (type1, type2);
3833 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3835 op1 = TREE_OPERAND (op1, 0);
3836 type1 = TREE_TYPE (op1);
3837 gcc_assert (TREE_CODE (type1) == code1);
3839 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3841 op2 = TREE_OPERAND (op2, 0);
3842 type2 = TREE_TYPE (op2);
3843 gcc_assert (TREE_CODE (type2) == code2);
3847 if (warn_cxx_compat)
3849 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
3850 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
3852 if (TREE_CODE (t1) == ENUMERAL_TYPE
3853 && TREE_CODE (t2) == ENUMERAL_TYPE
3854 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
3855 warning_at (colon_loc, OPT_Wc___compat,
3856 ("different enum types in conditional is "
3857 "invalid in C++: %qT vs %qT"),
3858 t1, t2);
3861 /* Quickly detect the usual case where op1 and op2 have the same type
3862 after promotion. */
3863 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3865 if (type1 == type2)
3866 result_type = type1;
3867 else
3868 result_type = TYPE_MAIN_VARIANT (type1);
3870 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3871 || code1 == COMPLEX_TYPE)
3872 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3873 || code2 == COMPLEX_TYPE))
3875 result_type = c_common_type (type1, type2);
3877 /* If -Wsign-compare, warn here if type1 and type2 have
3878 different signedness. We'll promote the signed to unsigned
3879 and later code won't know it used to be different.
3880 Do this check on the original types, so that explicit casts
3881 will be considered, but default promotions won't. */
3882 if (c_inhibit_evaluation_warnings == 0)
3884 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3885 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3887 if (unsigned_op1 ^ unsigned_op2)
3889 bool ovf;
3891 /* Do not warn if the result type is signed, since the
3892 signed type will only be chosen if it can represent
3893 all the values of the unsigned type. */
3894 if (!TYPE_UNSIGNED (result_type))
3895 /* OK */;
3896 else
3898 bool op1_maybe_const = true;
3899 bool op2_maybe_const = true;
3901 /* Do not warn if the signed quantity is an
3902 unsuffixed integer literal (or some static
3903 constant expression involving such literals) and
3904 it is non-negative. This warning requires the
3905 operands to be folded for best results, so do
3906 that folding in this case even without
3907 warn_sign_compare to avoid warning options
3908 possibly affecting code generation. */
3909 op1 = c_fully_fold (op1, require_constant_value,
3910 &op1_maybe_const);
3911 op2 = c_fully_fold (op2, require_constant_value,
3912 &op2_maybe_const);
3914 if (warn_sign_compare)
3916 if ((unsigned_op2
3917 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3918 || (unsigned_op1
3919 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3920 /* OK */;
3921 else
3922 warning_at (colon_loc, OPT_Wsign_compare,
3923 ("signed and unsigned type in "
3924 "conditional expression"));
3926 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3928 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3929 NULL, op1);
3930 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3932 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3934 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3935 NULL, op2);
3936 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3942 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3944 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3945 pedwarn (colon_loc, OPT_pedantic,
3946 "ISO C forbids conditional expr with only one void side");
3947 result_type = void_type_node;
3949 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3951 if (comp_target_types (colon_loc, type1, type2))
3952 result_type = common_pointer_type (type1, type2);
3953 else if (null_pointer_constant_p (orig_op1))
3954 result_type = qualify_type (type2, type1);
3955 else if (null_pointer_constant_p (orig_op2))
3956 result_type = qualify_type (type1, type2);
3957 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3959 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3960 pedwarn (colon_loc, OPT_pedantic,
3961 "ISO C forbids conditional expr between "
3962 "%<void *%> and function pointer");
3963 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3964 TREE_TYPE (type2)));
3966 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3968 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3969 pedwarn (colon_loc, OPT_pedantic,
3970 "ISO C forbids conditional expr between "
3971 "%<void *%> and function pointer");
3972 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3973 TREE_TYPE (type1)));
3975 else
3977 if (!objc_ok)
3978 pedwarn (colon_loc, 0,
3979 "pointer type mismatch in conditional expression");
3980 result_type = build_pointer_type (void_type_node);
3983 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3985 if (!null_pointer_constant_p (orig_op2))
3986 pedwarn (colon_loc, 0,
3987 "pointer/integer type mismatch in conditional expression");
3988 else
3990 op2 = null_pointer_node;
3992 result_type = type1;
3994 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3996 if (!null_pointer_constant_p (orig_op1))
3997 pedwarn (colon_loc, 0,
3998 "pointer/integer type mismatch in conditional expression");
3999 else
4001 op1 = null_pointer_node;
4003 result_type = type2;
4006 if (!result_type)
4008 if (flag_cond_mismatch)
4009 result_type = void_type_node;
4010 else
4012 error_at (colon_loc, "type mismatch in conditional expression");
4013 return error_mark_node;
4017 /* Merge const and volatile flags of the incoming types. */
4018 result_type
4019 = build_type_variant (result_type,
4020 TREE_READONLY (op1) || TREE_READONLY (op2),
4021 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4023 if (result_type != TREE_TYPE (op1))
4024 op1 = convert_and_check (result_type, op1);
4025 if (result_type != TREE_TYPE (op2))
4026 op2 = convert_and_check (result_type, op2);
4028 if (ifexp_bcp && ifexp == truthvalue_true_node)
4030 op2_int_operands = true;
4031 op1 = c_fully_fold (op1, require_constant_value, NULL);
4033 if (ifexp_bcp && ifexp == truthvalue_false_node)
4035 op1_int_operands = true;
4036 op2 = c_fully_fold (op2, require_constant_value, NULL);
4038 int_const = int_operands = (ifexp_int_operands
4039 && op1_int_operands
4040 && op2_int_operands);
4041 if (int_operands)
4043 int_const = ((ifexp == truthvalue_true_node
4044 && TREE_CODE (orig_op1) == INTEGER_CST
4045 && !TREE_OVERFLOW (orig_op1))
4046 || (ifexp == truthvalue_false_node
4047 && TREE_CODE (orig_op2) == INTEGER_CST
4048 && !TREE_OVERFLOW (orig_op2)));
4050 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4051 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
4052 else
4054 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4055 if (int_operands)
4056 ret = note_integer_operands (ret);
4058 if (ep_result_type)
4059 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
4061 protected_set_expr_location (ret, colon_loc);
4062 return ret;
4065 /* Return a compound expression that performs two expressions and
4066 returns the value of the second of them.
4068 LOC is the location of the COMPOUND_EXPR. */
4070 tree
4071 build_compound_expr (location_t loc, tree expr1, tree expr2)
4073 bool expr1_int_operands, expr2_int_operands;
4074 tree eptype = NULL_TREE;
4075 tree ret;
4077 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4078 if (expr1_int_operands)
4079 expr1 = remove_c_maybe_const_expr (expr1);
4080 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4081 if (expr2_int_operands)
4082 expr2 = remove_c_maybe_const_expr (expr2);
4084 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4085 expr1 = TREE_OPERAND (expr1, 0);
4086 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4088 eptype = TREE_TYPE (expr2);
4089 expr2 = TREE_OPERAND (expr2, 0);
4092 if (!TREE_SIDE_EFFECTS (expr1))
4094 /* The left-hand operand of a comma expression is like an expression
4095 statement: with -Wunused, we should warn if it doesn't have
4096 any side-effects, unless it was explicitly cast to (void). */
4097 if (warn_unused_value)
4099 if (VOID_TYPE_P (TREE_TYPE (expr1))
4100 && CONVERT_EXPR_P (expr1))
4101 ; /* (void) a, b */
4102 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4103 && TREE_CODE (expr1) == COMPOUND_EXPR
4104 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4105 ; /* (void) a, (void) b, c */
4106 else
4107 warning_at (loc, OPT_Wunused_value,
4108 "left-hand operand of comma expression has no effect");
4112 /* With -Wunused, we should also warn if the left-hand operand does have
4113 side-effects, but computes a value which is not used. For example, in
4114 `foo() + bar(), baz()' the result of the `+' operator is not used,
4115 so we should issue a warning. */
4116 else if (warn_unused_value)
4117 warn_if_unused_value (expr1, loc);
4119 if (expr2 == error_mark_node)
4120 return error_mark_node;
4122 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4124 if (flag_isoc99
4125 && expr1_int_operands
4126 && expr2_int_operands)
4127 ret = note_integer_operands (ret);
4129 if (eptype)
4130 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4132 protected_set_expr_location (ret, loc);
4133 return ret;
4136 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4137 which we are casting. OTYPE is the type of the expression being
4138 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4139 on the command line. */
4141 static void
4142 handle_warn_cast_qual (tree type, tree otype)
4144 tree in_type = type;
4145 tree in_otype = otype;
4146 int added = 0;
4147 int discarded = 0;
4148 bool is_const;
4150 /* Check that the qualifiers on IN_TYPE are a superset of the
4151 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4152 nodes is uninteresting and we stop as soon as we hit a
4153 non-POINTER_TYPE node on either type. */
4156 in_otype = TREE_TYPE (in_otype);
4157 in_type = TREE_TYPE (in_type);
4159 /* GNU C allows cv-qualified function types. 'const' means the
4160 function is very pure, 'volatile' means it can't return. We
4161 need to warn when such qualifiers are added, not when they're
4162 taken away. */
4163 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4164 && TREE_CODE (in_type) == FUNCTION_TYPE)
4165 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4166 else
4167 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4169 while (TREE_CODE (in_type) == POINTER_TYPE
4170 && TREE_CODE (in_otype) == POINTER_TYPE);
4172 if (added)
4173 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4175 if (discarded)
4176 /* There are qualifiers present in IN_OTYPE that are not present
4177 in IN_TYPE. */
4178 warning (OPT_Wcast_qual,
4179 "cast discards qualifiers from pointer target type");
4181 if (added || discarded)
4182 return;
4184 /* A cast from **T to const **T is unsafe, because it can cause a
4185 const value to be changed with no additional warning. We only
4186 issue this warning if T is the same on both sides, and we only
4187 issue the warning if there are the same number of pointers on
4188 both sides, as otherwise the cast is clearly unsafe anyhow. A
4189 cast is unsafe when a qualifier is added at one level and const
4190 is not present at all outer levels.
4192 To issue this warning, we check at each level whether the cast
4193 adds new qualifiers not already seen. We don't need to special
4194 case function types, as they won't have the same
4195 TYPE_MAIN_VARIANT. */
4197 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4198 return;
4199 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4200 return;
4202 in_type = type;
4203 in_otype = otype;
4204 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4207 in_type = TREE_TYPE (in_type);
4208 in_otype = TREE_TYPE (in_otype);
4209 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4210 && !is_const)
4212 warning (OPT_Wcast_qual,
4213 ("new qualifiers in middle of multi-level non-const cast "
4214 "are unsafe"));
4215 break;
4217 if (is_const)
4218 is_const = TYPE_READONLY (in_type);
4220 while (TREE_CODE (in_type) == POINTER_TYPE);
4223 /* Build an expression representing a cast to type TYPE of expression EXPR.
4224 LOC is the location of the cast-- typically the open paren of the cast. */
4226 tree
4227 build_c_cast (location_t loc, tree type, tree expr)
4229 tree value;
4231 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4232 expr = TREE_OPERAND (expr, 0);
4234 value = expr;
4236 if (type == error_mark_node || expr == error_mark_node)
4237 return error_mark_node;
4239 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4240 only in <protocol> qualifications. But when constructing cast expressions,
4241 the protocols do matter and must be kept around. */
4242 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4243 return build1 (NOP_EXPR, type, expr);
4245 type = TYPE_MAIN_VARIANT (type);
4247 if (TREE_CODE (type) == ARRAY_TYPE)
4249 error_at (loc, "cast specifies array type");
4250 return error_mark_node;
4253 if (TREE_CODE (type) == FUNCTION_TYPE)
4255 error_at (loc, "cast specifies function type");
4256 return error_mark_node;
4259 if (!VOID_TYPE_P (type))
4261 value = require_complete_type (value);
4262 if (value == error_mark_node)
4263 return error_mark_node;
4266 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4268 if (TREE_CODE (type) == RECORD_TYPE
4269 || TREE_CODE (type) == UNION_TYPE)
4270 pedwarn (loc, OPT_pedantic,
4271 "ISO C forbids casting nonscalar to the same type");
4273 else if (TREE_CODE (type) == UNION_TYPE)
4275 tree field;
4277 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4278 if (TREE_TYPE (field) != error_mark_node
4279 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4280 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4281 break;
4283 if (field)
4285 tree t;
4287 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4288 t = digest_init (loc, type,
4289 build_constructor_single (type, field, value),
4290 NULL_TREE, false, true, 0);
4291 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4292 return t;
4294 error_at (loc, "cast to union type from type not present in union");
4295 return error_mark_node;
4297 else
4299 tree otype, ovalue;
4301 if (type == void_type_node)
4303 tree t = build1 (CONVERT_EXPR, type, value);
4304 SET_EXPR_LOCATION (t, loc);
4305 return t;
4308 otype = TREE_TYPE (value);
4310 /* Optionally warn about potentially worrisome casts. */
4311 if (warn_cast_qual
4312 && TREE_CODE (type) == POINTER_TYPE
4313 && TREE_CODE (otype) == POINTER_TYPE)
4314 handle_warn_cast_qual (type, otype);
4316 /* Warn about possible alignment problems. */
4317 if (STRICT_ALIGNMENT
4318 && TREE_CODE (type) == POINTER_TYPE
4319 && TREE_CODE (otype) == POINTER_TYPE
4320 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4321 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4322 /* Don't warn about opaque types, where the actual alignment
4323 restriction is unknown. */
4324 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4325 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4326 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4327 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4328 warning_at (loc, OPT_Wcast_align,
4329 "cast increases required alignment of target type");
4331 if (TREE_CODE (type) == INTEGER_TYPE
4332 && TREE_CODE (otype) == POINTER_TYPE
4333 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4334 /* Unlike conversion of integers to pointers, where the
4335 warning is disabled for converting constants because
4336 of cases such as SIG_*, warn about converting constant
4337 pointers to integers. In some cases it may cause unwanted
4338 sign extension, and a warning is appropriate. */
4339 warning_at (loc, OPT_Wpointer_to_int_cast,
4340 "cast from pointer to integer of different size");
4342 if (TREE_CODE (value) == CALL_EXPR
4343 && TREE_CODE (type) != TREE_CODE (otype))
4344 warning_at (loc, OPT_Wbad_function_cast,
4345 "cast from function call of type %qT "
4346 "to non-matching type %qT", otype, type);
4348 if (TREE_CODE (type) == POINTER_TYPE
4349 && TREE_CODE (otype) == INTEGER_TYPE
4350 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4351 /* Don't warn about converting any constant. */
4352 && !TREE_CONSTANT (value))
4353 warning_at (loc,
4354 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4355 "of different size");
4357 if (warn_strict_aliasing <= 2)
4358 strict_aliasing_warning (otype, type, expr);
4360 /* If pedantic, warn for conversions between function and object
4361 pointer types, except for converting a null pointer constant
4362 to function pointer type. */
4363 if (pedantic
4364 && TREE_CODE (type) == POINTER_TYPE
4365 && TREE_CODE (otype) == POINTER_TYPE
4366 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4367 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4368 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4369 "conversion of function pointer to object pointer type");
4371 if (pedantic
4372 && TREE_CODE (type) == POINTER_TYPE
4373 && TREE_CODE (otype) == POINTER_TYPE
4374 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4375 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4376 && !null_pointer_constant_p (value))
4377 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4378 "conversion of object pointer to function pointer type");
4380 ovalue = value;
4381 value = convert (type, value);
4383 /* Ignore any integer overflow caused by the cast. */
4384 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4386 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4388 if (!TREE_OVERFLOW (value))
4390 /* Avoid clobbering a shared constant. */
4391 value = copy_node (value);
4392 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4395 else if (TREE_OVERFLOW (value))
4396 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4397 value = build_int_cst_wide (TREE_TYPE (value),
4398 TREE_INT_CST_LOW (value),
4399 TREE_INT_CST_HIGH (value));
4403 /* Don't let a cast be an lvalue. */
4404 if (value == expr)
4405 value = non_lvalue (value);
4407 /* Don't allow the results of casting to floating-point or complex
4408 types be confused with actual constants, or casts involving
4409 integer and pointer types other than direct integer-to-integer
4410 and integer-to-pointer be confused with integer constant
4411 expressions and null pointer constants. */
4412 if (TREE_CODE (value) == REAL_CST
4413 || TREE_CODE (value) == COMPLEX_CST
4414 || (TREE_CODE (value) == INTEGER_CST
4415 && !((TREE_CODE (expr) == INTEGER_CST
4416 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4417 || TREE_CODE (expr) == REAL_CST
4418 || TREE_CODE (expr) == COMPLEX_CST)))
4419 value = build1 (NOP_EXPR, type, value);
4421 if (CAN_HAVE_LOCATION_P (value))
4422 SET_EXPR_LOCATION (value, loc);
4423 return value;
4426 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4427 location of the open paren of the cast, or the position of the cast
4428 expr. */
4429 tree
4430 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4432 tree type;
4433 tree type_expr = NULL_TREE;
4434 bool type_expr_const = true;
4435 tree ret;
4436 int saved_wsp = warn_strict_prototypes;
4438 /* This avoids warnings about unprototyped casts on
4439 integers. E.g. "#define SIG_DFL (void(*)())0". */
4440 if (TREE_CODE (expr) == INTEGER_CST)
4441 warn_strict_prototypes = 0;
4442 type = groktypename (type_name, &type_expr, &type_expr_const);
4443 warn_strict_prototypes = saved_wsp;
4445 ret = build_c_cast (loc, type, expr);
4446 if (type_expr)
4448 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4449 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4450 SET_EXPR_LOCATION (ret, loc);
4453 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4454 SET_EXPR_LOCATION (ret, loc);
4456 /* C++ does not permits types to be defined in a cast. */
4457 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4458 warning_at (loc, OPT_Wc___compat,
4459 "defining a type in a cast is invalid in C++");
4461 return ret;
4464 /* Build an assignment expression of lvalue LHS from value RHS.
4465 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4466 may differ from TREE_TYPE (LHS) for an enum bitfield.
4467 MODIFYCODE is the code for a binary operator that we use
4468 to combine the old value of LHS with RHS to get the new value.
4469 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4470 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4471 which may differ from TREE_TYPE (RHS) for an enum value.
4473 LOCATION is the location of the MODIFYCODE operator.
4474 RHS_LOC is the location of the RHS. */
4476 tree
4477 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4478 enum tree_code modifycode,
4479 location_t rhs_loc, tree rhs, tree rhs_origtype)
4481 tree result;
4482 tree newrhs;
4483 tree rhs_semantic_type = NULL_TREE;
4484 tree lhstype = TREE_TYPE (lhs);
4485 tree olhstype = lhstype;
4486 bool npc;
4488 /* Types that aren't fully specified cannot be used in assignments. */
4489 lhs = require_complete_type (lhs);
4491 /* Avoid duplicate error messages from operands that had errors. */
4492 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4493 return error_mark_node;
4495 if (!lvalue_or_else (lhs, lv_assign))
4496 return error_mark_node;
4498 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4500 rhs_semantic_type = TREE_TYPE (rhs);
4501 rhs = TREE_OPERAND (rhs, 0);
4504 newrhs = rhs;
4506 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4508 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4509 lhs_origtype, modifycode, rhs_loc, rhs,
4510 rhs_origtype);
4511 if (inner == error_mark_node)
4512 return error_mark_node;
4513 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4514 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4515 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4516 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4517 protected_set_expr_location (result, location);
4518 return result;
4521 /* If a binary op has been requested, combine the old LHS value with the RHS
4522 producing the value we should actually store into the LHS. */
4524 if (modifycode != NOP_EXPR)
4526 lhs = c_fully_fold (lhs, false, NULL);
4527 lhs = stabilize_reference (lhs);
4528 newrhs = build_binary_op (location,
4529 modifycode, lhs, rhs, 1);
4531 /* The original type of the right hand side is no longer
4532 meaningful. */
4533 rhs_origtype = NULL_TREE;
4536 /* Give an error for storing in something that is 'const'. */
4538 if (TYPE_READONLY (lhstype)
4539 || ((TREE_CODE (lhstype) == RECORD_TYPE
4540 || TREE_CODE (lhstype) == UNION_TYPE)
4541 && C_TYPE_FIELDS_READONLY (lhstype)))
4543 readonly_error (lhs, lv_assign);
4544 return error_mark_node;
4546 else if (TREE_READONLY (lhs))
4547 readonly_warning (lhs, lv_assign);
4549 /* If storing into a structure or union member,
4550 it has probably been given type `int'.
4551 Compute the type that would go with
4552 the actual amount of storage the member occupies. */
4554 if (TREE_CODE (lhs) == COMPONENT_REF
4555 && (TREE_CODE (lhstype) == INTEGER_TYPE
4556 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4557 || TREE_CODE (lhstype) == REAL_TYPE
4558 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4559 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4561 /* If storing in a field that is in actuality a short or narrower than one,
4562 we must store in the field in its actual type. */
4564 if (lhstype != TREE_TYPE (lhs))
4566 lhs = copy_node (lhs);
4567 TREE_TYPE (lhs) = lhstype;
4570 /* Issue -Wc++-compat warnings about an assignment to an enum type
4571 when LHS does not have its original type. This happens for,
4572 e.g., an enum bitfield in a struct. */
4573 if (warn_cxx_compat
4574 && lhs_origtype != NULL_TREE
4575 && lhs_origtype != lhstype
4576 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4578 tree checktype = (rhs_origtype != NULL_TREE
4579 ? rhs_origtype
4580 : TREE_TYPE (rhs));
4581 if (checktype != error_mark_node
4582 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4583 warning_at (location, OPT_Wc___compat,
4584 "enum conversion in assignment is invalid in C++");
4587 /* Convert new value to destination type. Fold it first, then
4588 restore any excess precision information, for the sake of
4589 conversion warnings. */
4591 npc = null_pointer_constant_p (newrhs);
4592 newrhs = c_fully_fold (newrhs, false, NULL);
4593 if (rhs_semantic_type)
4594 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4595 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4596 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4597 if (TREE_CODE (newrhs) == ERROR_MARK)
4598 return error_mark_node;
4600 /* Emit ObjC write barrier, if necessary. */
4601 if (c_dialect_objc () && flag_objc_gc)
4603 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4604 if (result)
4606 protected_set_expr_location (result, location);
4607 return result;
4611 /* Scan operands. */
4613 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4614 TREE_SIDE_EFFECTS (result) = 1;
4615 protected_set_expr_location (result, location);
4617 /* If we got the LHS in a different type for storing in,
4618 convert the result back to the nominal type of LHS
4619 so that the value we return always has the same type
4620 as the LHS argument. */
4622 if (olhstype == TREE_TYPE (result))
4623 return result;
4625 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4626 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4627 protected_set_expr_location (result, location);
4628 return result;
4631 /* Convert value RHS to type TYPE as preparation for an assignment to
4632 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4633 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4634 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4635 constant before any folding.
4636 The real work of conversion is done by `convert'.
4637 The purpose of this function is to generate error messages
4638 for assignments that are not allowed in C.
4639 ERRTYPE says whether it is argument passing, assignment,
4640 initialization or return.
4642 LOCATION is the location of the RHS.
4643 FUNCTION is a tree for the function being called.
4644 PARMNUM is the number of the argument, for printing in error messages. */
4646 static tree
4647 convert_for_assignment (location_t location, tree type, tree rhs,
4648 tree origtype, enum impl_conv errtype,
4649 bool null_pointer_constant, tree fundecl,
4650 tree function, int parmnum)
4652 enum tree_code codel = TREE_CODE (type);
4653 tree orig_rhs = rhs;
4654 tree rhstype;
4655 enum tree_code coder;
4656 tree rname = NULL_TREE;
4657 bool objc_ok = false;
4659 if (errtype == ic_argpass)
4661 tree selector;
4662 /* Change pointer to function to the function itself for
4663 diagnostics. */
4664 if (TREE_CODE (function) == ADDR_EXPR
4665 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4666 function = TREE_OPERAND (function, 0);
4668 /* Handle an ObjC selector specially for diagnostics. */
4669 selector = objc_message_selector ();
4670 rname = function;
4671 if (selector && parmnum > 2)
4673 rname = selector;
4674 parmnum -= 2;
4678 /* This macro is used to emit diagnostics to ensure that all format
4679 strings are complete sentences, visible to gettext and checked at
4680 compile time. */
4681 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4682 do { \
4683 switch (errtype) \
4685 case ic_argpass: \
4686 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4687 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4688 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4689 "expected %qT but argument is of type %qT", \
4690 type, rhstype); \
4691 break; \
4692 case ic_assign: \
4693 pedwarn (LOCATION, OPT, AS); \
4694 break; \
4695 case ic_init: \
4696 pedwarn (LOCATION, OPT, IN); \
4697 break; \
4698 case ic_return: \
4699 pedwarn (LOCATION, OPT, RE); \
4700 break; \
4701 default: \
4702 gcc_unreachable (); \
4704 } while (0)
4706 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4707 rhs = TREE_OPERAND (rhs, 0);
4709 rhstype = TREE_TYPE (rhs);
4710 coder = TREE_CODE (rhstype);
4712 if (coder == ERROR_MARK)
4713 return error_mark_node;
4715 if (c_dialect_objc ())
4717 int parmno;
4719 switch (errtype)
4721 case ic_return:
4722 parmno = 0;
4723 break;
4725 case ic_assign:
4726 parmno = -1;
4727 break;
4729 case ic_init:
4730 parmno = -2;
4731 break;
4733 default:
4734 parmno = parmnum;
4735 break;
4738 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4741 if (warn_cxx_compat)
4743 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4744 if (checktype != error_mark_node
4745 && TREE_CODE (type) == ENUMERAL_TYPE
4746 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4748 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4749 G_("enum conversion when passing argument "
4750 "%d of %qE is invalid in C++"),
4751 G_("enum conversion in assignment is "
4752 "invalid in C++"),
4753 G_("enum conversion in initialization is "
4754 "invalid in C++"),
4755 G_("enum conversion in return is "
4756 "invalid in C++"));
4760 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4761 return rhs;
4763 if (coder == VOID_TYPE)
4765 /* Except for passing an argument to an unprototyped function,
4766 this is a constraint violation. When passing an argument to
4767 an unprototyped function, it is compile-time undefined;
4768 making it a constraint in that case was rejected in
4769 DR#252. */
4770 error_at (location, "void value not ignored as it ought to be");
4771 return error_mark_node;
4773 rhs = require_complete_type (rhs);
4774 if (rhs == error_mark_node)
4775 return error_mark_node;
4776 /* A type converts to a reference to it.
4777 This code doesn't fully support references, it's just for the
4778 special case of va_start and va_copy. */
4779 if (codel == REFERENCE_TYPE
4780 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4782 if (!lvalue_p (rhs))
4784 error_at (location, "cannot pass rvalue to reference parameter");
4785 return error_mark_node;
4787 if (!c_mark_addressable (rhs))
4788 return error_mark_node;
4789 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4790 SET_EXPR_LOCATION (rhs, location);
4792 /* We already know that these two types are compatible, but they
4793 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4794 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4795 likely to be va_list, a typedef to __builtin_va_list, which
4796 is different enough that it will cause problems later. */
4797 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4799 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4800 SET_EXPR_LOCATION (rhs, location);
4803 rhs = build1 (NOP_EXPR, type, rhs);
4804 SET_EXPR_LOCATION (rhs, location);
4805 return rhs;
4807 /* Some types can interconvert without explicit casts. */
4808 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4809 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4810 return convert (type, rhs);
4811 /* Arithmetic types all interconvert, and enum is treated like int. */
4812 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4813 || codel == FIXED_POINT_TYPE
4814 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4815 || codel == BOOLEAN_TYPE)
4816 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4817 || coder == FIXED_POINT_TYPE
4818 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4819 || coder == BOOLEAN_TYPE))
4821 tree ret;
4822 bool save = in_late_binary_op;
4823 if (codel == BOOLEAN_TYPE)
4824 in_late_binary_op = true;
4825 ret = convert_and_check (type, orig_rhs);
4826 if (codel == BOOLEAN_TYPE)
4827 in_late_binary_op = save;
4828 return ret;
4831 /* Aggregates in different TUs might need conversion. */
4832 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4833 && codel == coder
4834 && comptypes (type, rhstype))
4835 return convert_and_check (type, rhs);
4837 /* Conversion to a transparent union from its member types.
4838 This applies only to function arguments. */
4839 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4840 && errtype == ic_argpass)
4842 tree memb, marginal_memb = NULL_TREE;
4844 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4846 tree memb_type = TREE_TYPE (memb);
4848 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4849 TYPE_MAIN_VARIANT (rhstype)))
4850 break;
4852 if (TREE_CODE (memb_type) != POINTER_TYPE)
4853 continue;
4855 if (coder == POINTER_TYPE)
4857 tree ttl = TREE_TYPE (memb_type);
4858 tree ttr = TREE_TYPE (rhstype);
4860 /* Any non-function converts to a [const][volatile] void *
4861 and vice versa; otherwise, targets must be the same.
4862 Meanwhile, the lhs target must have all the qualifiers of
4863 the rhs. */
4864 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4865 || comp_target_types (location, memb_type, rhstype))
4867 /* If this type won't generate any warnings, use it. */
4868 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4869 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4870 && TREE_CODE (ttl) == FUNCTION_TYPE)
4871 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4872 == TYPE_QUALS (ttr))
4873 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4874 == TYPE_QUALS (ttl))))
4875 break;
4877 /* Keep looking for a better type, but remember this one. */
4878 if (!marginal_memb)
4879 marginal_memb = memb;
4883 /* Can convert integer zero to any pointer type. */
4884 if (null_pointer_constant)
4886 rhs = null_pointer_node;
4887 break;
4891 if (memb || marginal_memb)
4893 if (!memb)
4895 /* We have only a marginally acceptable member type;
4896 it needs a warning. */
4897 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4898 tree ttr = TREE_TYPE (rhstype);
4900 /* Const and volatile mean something different for function
4901 types, so the usual warnings are not appropriate. */
4902 if (TREE_CODE (ttr) == FUNCTION_TYPE
4903 && TREE_CODE (ttl) == FUNCTION_TYPE)
4905 /* Because const and volatile on functions are
4906 restrictions that say the function will not do
4907 certain things, it is okay to use a const or volatile
4908 function where an ordinary one is wanted, but not
4909 vice-versa. */
4910 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4911 WARN_FOR_ASSIGNMENT (location, 0,
4912 G_("passing argument %d of %qE "
4913 "makes qualified function "
4914 "pointer from unqualified"),
4915 G_("assignment makes qualified "
4916 "function pointer from "
4917 "unqualified"),
4918 G_("initialization makes qualified "
4919 "function pointer from "
4920 "unqualified"),
4921 G_("return makes qualified function "
4922 "pointer from unqualified"));
4924 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4925 WARN_FOR_ASSIGNMENT (location, 0,
4926 G_("passing argument %d of %qE discards "
4927 "qualifiers from pointer target type"),
4928 G_("assignment discards qualifiers "
4929 "from pointer target type"),
4930 G_("initialization discards qualifiers "
4931 "from pointer target type"),
4932 G_("return discards qualifiers from "
4933 "pointer target type"));
4935 memb = marginal_memb;
4938 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4939 pedwarn (location, OPT_pedantic,
4940 "ISO C prohibits argument conversion to union type");
4942 rhs = fold_convert (TREE_TYPE (memb), rhs);
4943 return build_constructor_single (type, memb, rhs);
4947 /* Conversions among pointers */
4948 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4949 && (coder == codel))
4951 tree ttl = TREE_TYPE (type);
4952 tree ttr = TREE_TYPE (rhstype);
4953 tree mvl = ttl;
4954 tree mvr = ttr;
4955 bool is_opaque_pointer;
4956 int target_cmp = 0; /* Cache comp_target_types () result. */
4958 if (TREE_CODE (mvl) != ARRAY_TYPE)
4959 mvl = TYPE_MAIN_VARIANT (mvl);
4960 if (TREE_CODE (mvr) != ARRAY_TYPE)
4961 mvr = TYPE_MAIN_VARIANT (mvr);
4962 /* Opaque pointers are treated like void pointers. */
4963 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4965 /* C++ does not allow the implicit conversion void* -> T*. However,
4966 for the purpose of reducing the number of false positives, we
4967 tolerate the special case of
4969 int *p = NULL;
4971 where NULL is typically defined in C to be '(void *) 0'. */
4972 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4973 warning_at (location, OPT_Wc___compat,
4974 "request for implicit conversion "
4975 "from %qT to %qT not permitted in C++", rhstype, type);
4977 /* Check if the right-hand side has a format attribute but the
4978 left-hand side doesn't. */
4979 if (warn_missing_format_attribute
4980 && check_missing_format_attribute (type, rhstype))
4982 switch (errtype)
4984 case ic_argpass:
4985 warning_at (location, OPT_Wmissing_format_attribute,
4986 "argument %d of %qE might be "
4987 "a candidate for a format attribute",
4988 parmnum, rname);
4989 break;
4990 case ic_assign:
4991 warning_at (location, OPT_Wmissing_format_attribute,
4992 "assignment left-hand side might be "
4993 "a candidate for a format attribute");
4994 break;
4995 case ic_init:
4996 warning_at (location, OPT_Wmissing_format_attribute,
4997 "initialization left-hand side might be "
4998 "a candidate for a format attribute");
4999 break;
5000 case ic_return:
5001 warning_at (location, OPT_Wmissing_format_attribute,
5002 "return type might be "
5003 "a candidate for a format attribute");
5004 break;
5005 default:
5006 gcc_unreachable ();
5010 /* Any non-function converts to a [const][volatile] void *
5011 and vice versa; otherwise, targets must be the same.
5012 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5013 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5014 || (target_cmp = comp_target_types (location, type, rhstype))
5015 || is_opaque_pointer
5016 || (c_common_unsigned_type (mvl)
5017 == c_common_unsigned_type (mvr)))
5019 if (pedantic
5020 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5022 (VOID_TYPE_P (ttr)
5023 && !null_pointer_constant
5024 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5025 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5026 G_("ISO C forbids passing argument %d of "
5027 "%qE between function pointer "
5028 "and %<void *%>"),
5029 G_("ISO C forbids assignment between "
5030 "function pointer and %<void *%>"),
5031 G_("ISO C forbids initialization between "
5032 "function pointer and %<void *%>"),
5033 G_("ISO C forbids return between function "
5034 "pointer and %<void *%>"));
5035 /* Const and volatile mean something different for function types,
5036 so the usual warnings are not appropriate. */
5037 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5038 && TREE_CODE (ttl) != FUNCTION_TYPE)
5040 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
5042 /* Types differing only by the presence of the 'volatile'
5043 qualifier are acceptable if the 'volatile' has been added
5044 in by the Objective-C EH machinery. */
5045 if (!objc_type_quals_match (ttl, ttr))
5046 WARN_FOR_ASSIGNMENT (location, 0,
5047 G_("passing argument %d of %qE discards "
5048 "qualifiers from pointer target type"),
5049 G_("assignment discards qualifiers "
5050 "from pointer target type"),
5051 G_("initialization discards qualifiers "
5052 "from pointer target type"),
5053 G_("return discards qualifiers from "
5054 "pointer target type"));
5056 /* If this is not a case of ignoring a mismatch in signedness,
5057 no warning. */
5058 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5059 || target_cmp)
5061 /* If there is a mismatch, do warn. */
5062 else if (warn_pointer_sign)
5063 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5064 G_("pointer targets in passing argument "
5065 "%d of %qE differ in signedness"),
5066 G_("pointer targets in assignment "
5067 "differ in signedness"),
5068 G_("pointer targets in initialization "
5069 "differ in signedness"),
5070 G_("pointer targets in return differ "
5071 "in signedness"));
5073 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5074 && TREE_CODE (ttr) == FUNCTION_TYPE)
5076 /* Because const and volatile on functions are restrictions
5077 that say the function will not do certain things,
5078 it is okay to use a const or volatile function
5079 where an ordinary one is wanted, but not vice-versa. */
5080 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
5081 WARN_FOR_ASSIGNMENT (location, 0,
5082 G_("passing argument %d of %qE makes "
5083 "qualified function pointer "
5084 "from unqualified"),
5085 G_("assignment makes qualified function "
5086 "pointer from unqualified"),
5087 G_("initialization makes qualified "
5088 "function pointer from unqualified"),
5089 G_("return makes qualified function "
5090 "pointer from unqualified"));
5093 else
5094 /* Avoid warning about the volatile ObjC EH puts on decls. */
5095 if (!objc_ok)
5096 WARN_FOR_ASSIGNMENT (location, 0,
5097 G_("passing argument %d of %qE from "
5098 "incompatible pointer type"),
5099 G_("assignment from incompatible pointer type"),
5100 G_("initialization from incompatible "
5101 "pointer type"),
5102 G_("return from incompatible pointer type"));
5104 return convert (type, rhs);
5106 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5108 /* ??? This should not be an error when inlining calls to
5109 unprototyped functions. */
5110 error_at (location, "invalid use of non-lvalue array");
5111 return error_mark_node;
5113 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5115 /* An explicit constant 0 can convert to a pointer,
5116 or one that results from arithmetic, even including
5117 a cast to integer type. */
5118 if (!null_pointer_constant)
5119 WARN_FOR_ASSIGNMENT (location, 0,
5120 G_("passing argument %d of %qE makes "
5121 "pointer from integer without a cast"),
5122 G_("assignment makes pointer from integer "
5123 "without a cast"),
5124 G_("initialization makes pointer from "
5125 "integer without a cast"),
5126 G_("return makes pointer from integer "
5127 "without a cast"));
5129 return convert (type, rhs);
5131 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5133 WARN_FOR_ASSIGNMENT (location, 0,
5134 G_("passing argument %d of %qE makes integer "
5135 "from pointer without a cast"),
5136 G_("assignment makes integer from pointer "
5137 "without a cast"),
5138 G_("initialization makes integer from pointer "
5139 "without a cast"),
5140 G_("return makes integer from pointer "
5141 "without a cast"));
5142 return convert (type, rhs);
5144 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5146 tree ret;
5147 bool save = in_late_binary_op;
5148 in_late_binary_op = true;
5149 ret = convert (type, rhs);
5150 in_late_binary_op = save;
5151 return ret;
5154 switch (errtype)
5156 case ic_argpass:
5157 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5158 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5159 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5160 "expected %qT but argument is of type %qT", type, rhstype);
5161 break;
5162 case ic_assign:
5163 error_at (location, "incompatible types when assigning to type %qT from "
5164 "type %qT", type, rhstype);
5165 break;
5166 case ic_init:
5167 error_at (location,
5168 "incompatible types when initializing type %qT using type %qT",
5169 type, rhstype);
5170 break;
5171 case ic_return:
5172 error_at (location,
5173 "incompatible types when returning type %qT but %qT was "
5174 "expected", rhstype, type);
5175 break;
5176 default:
5177 gcc_unreachable ();
5180 return error_mark_node;
5183 /* If VALUE is a compound expr all of whose expressions are constant, then
5184 return its value. Otherwise, return error_mark_node.
5186 This is for handling COMPOUND_EXPRs as initializer elements
5187 which is allowed with a warning when -pedantic is specified. */
5189 static tree
5190 valid_compound_expr_initializer (tree value, tree endtype)
5192 if (TREE_CODE (value) == COMPOUND_EXPR)
5194 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5195 == error_mark_node)
5196 return error_mark_node;
5197 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5198 endtype);
5200 else if (!initializer_constant_valid_p (value, endtype))
5201 return error_mark_node;
5202 else
5203 return value;
5206 /* Perform appropriate conversions on the initial value of a variable,
5207 store it in the declaration DECL,
5208 and print any error messages that are appropriate.
5209 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5210 If the init is invalid, store an ERROR_MARK.
5212 INIT_LOC is the location of the initial value. */
5214 void
5215 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5217 tree value, type;
5218 bool npc = false;
5220 /* If variable's type was invalidly declared, just ignore it. */
5222 type = TREE_TYPE (decl);
5223 if (TREE_CODE (type) == ERROR_MARK)
5224 return;
5226 /* Digest the specified initializer into an expression. */
5228 if (init)
5229 npc = null_pointer_constant_p (init);
5230 value = digest_init (init_loc, type, init, origtype, npc,
5231 true, TREE_STATIC (decl));
5233 /* Store the expression if valid; else report error. */
5235 if (!in_system_header
5236 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5237 warning (OPT_Wtraditional, "traditional C rejects automatic "
5238 "aggregate initialization");
5240 DECL_INITIAL (decl) = value;
5242 /* ANSI wants warnings about out-of-range constant initializers. */
5243 STRIP_TYPE_NOPS (value);
5244 if (TREE_STATIC (decl))
5245 constant_expression_warning (value);
5247 /* Check if we need to set array size from compound literal size. */
5248 if (TREE_CODE (type) == ARRAY_TYPE
5249 && TYPE_DOMAIN (type) == 0
5250 && value != error_mark_node)
5252 tree inside_init = init;
5254 STRIP_TYPE_NOPS (inside_init);
5255 inside_init = fold (inside_init);
5257 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5259 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5261 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5263 /* For int foo[] = (int [3]){1}; we need to set array size
5264 now since later on array initializer will be just the
5265 brace enclosed list of the compound literal. */
5266 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5267 TREE_TYPE (decl) = type;
5268 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5269 layout_type (type);
5270 layout_decl (cldecl, 0);
5276 /* Methods for storing and printing names for error messages. */
5278 /* Implement a spelling stack that allows components of a name to be pushed
5279 and popped. Each element on the stack is this structure. */
5281 struct spelling
5283 int kind;
5284 union
5286 unsigned HOST_WIDE_INT i;
5287 const char *s;
5288 } u;
5291 #define SPELLING_STRING 1
5292 #define SPELLING_MEMBER 2
5293 #define SPELLING_BOUNDS 3
5295 static struct spelling *spelling; /* Next stack element (unused). */
5296 static struct spelling *spelling_base; /* Spelling stack base. */
5297 static int spelling_size; /* Size of the spelling stack. */
5299 /* Macros to save and restore the spelling stack around push_... functions.
5300 Alternative to SAVE_SPELLING_STACK. */
5302 #define SPELLING_DEPTH() (spelling - spelling_base)
5303 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5305 /* Push an element on the spelling stack with type KIND and assign VALUE
5306 to MEMBER. */
5308 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5310 int depth = SPELLING_DEPTH (); \
5312 if (depth >= spelling_size) \
5314 spelling_size += 10; \
5315 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5316 spelling_size); \
5317 RESTORE_SPELLING_DEPTH (depth); \
5320 spelling->kind = (KIND); \
5321 spelling->MEMBER = (VALUE); \
5322 spelling++; \
5325 /* Push STRING on the stack. Printed literally. */
5327 static void
5328 push_string (const char *string)
5330 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5333 /* Push a member name on the stack. Printed as '.' STRING. */
5335 static void
5336 push_member_name (tree decl)
5338 const char *const string
5339 = (DECL_NAME (decl)
5340 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5341 : _("<anonymous>"));
5342 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5345 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5347 static void
5348 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5350 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5353 /* Compute the maximum size in bytes of the printed spelling. */
5355 static int
5356 spelling_length (void)
5358 int size = 0;
5359 struct spelling *p;
5361 for (p = spelling_base; p < spelling; p++)
5363 if (p->kind == SPELLING_BOUNDS)
5364 size += 25;
5365 else
5366 size += strlen (p->u.s) + 1;
5369 return size;
5372 /* Print the spelling to BUFFER and return it. */
5374 static char *
5375 print_spelling (char *buffer)
5377 char *d = buffer;
5378 struct spelling *p;
5380 for (p = spelling_base; p < spelling; p++)
5381 if (p->kind == SPELLING_BOUNDS)
5383 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5384 d += strlen (d);
5386 else
5388 const char *s;
5389 if (p->kind == SPELLING_MEMBER)
5390 *d++ = '.';
5391 for (s = p->u.s; (*d = *s++); d++)
5394 *d++ = '\0';
5395 return buffer;
5398 /* Issue an error message for a bad initializer component.
5399 MSGID identifies the message.
5400 The component name is taken from the spelling stack. */
5402 void
5403 error_init (const char *msgid)
5405 char *ofwhat;
5407 error ("%s", _(msgid));
5408 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5409 if (*ofwhat)
5410 error ("(near initialization for %qs)", ofwhat);
5413 /* Issue a pedantic warning for a bad initializer component. OPT is
5414 the option OPT_* (from options.h) controlling this warning or 0 if
5415 it is unconditionally given. MSGID identifies the message. The
5416 component name is taken from the spelling stack. */
5418 void
5419 pedwarn_init (location_t location, int opt, const char *msgid)
5421 char *ofwhat;
5423 pedwarn (location, opt, "%s", _(msgid));
5424 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5425 if (*ofwhat)
5426 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5429 /* Issue a warning for a bad initializer component.
5431 OPT is the OPT_W* value corresponding to the warning option that
5432 controls this warning. MSGID identifies the message. The
5433 component name is taken from the spelling stack. */
5435 static void
5436 warning_init (int opt, const char *msgid)
5438 char *ofwhat;
5440 warning (opt, "%s", _(msgid));
5441 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5442 if (*ofwhat)
5443 warning (opt, "(near initialization for %qs)", ofwhat);
5446 /* If TYPE is an array type and EXPR is a parenthesized string
5447 constant, warn if pedantic that EXPR is being used to initialize an
5448 object of type TYPE. */
5450 void
5451 maybe_warn_string_init (tree type, struct c_expr expr)
5453 if (pedantic
5454 && TREE_CODE (type) == ARRAY_TYPE
5455 && TREE_CODE (expr.value) == STRING_CST
5456 && expr.original_code != STRING_CST)
5457 pedwarn_init (input_location, OPT_pedantic,
5458 "array initialized from parenthesized string constant");
5461 /* Digest the parser output INIT as an initializer for type TYPE.
5462 Return a C expression of type TYPE to represent the initial value.
5464 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5466 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5468 If INIT is a string constant, STRICT_STRING is true if it is
5469 unparenthesized or we should not warn here for it being parenthesized.
5470 For other types of INIT, STRICT_STRING is not used.
5472 INIT_LOC is the location of the INIT.
5474 REQUIRE_CONSTANT requests an error if non-constant initializers or
5475 elements are seen. */
5477 static tree
5478 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5479 bool null_pointer_constant, bool strict_string,
5480 int require_constant)
5482 enum tree_code code = TREE_CODE (type);
5483 tree inside_init = init;
5484 tree semantic_type = NULL_TREE;
5485 bool maybe_const = true;
5487 if (type == error_mark_node
5488 || !init
5489 || init == error_mark_node
5490 || TREE_TYPE (init) == error_mark_node)
5491 return error_mark_node;
5493 STRIP_TYPE_NOPS (inside_init);
5495 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5497 semantic_type = TREE_TYPE (inside_init);
5498 inside_init = TREE_OPERAND (inside_init, 0);
5500 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5501 inside_init = decl_constant_value_for_optimization (inside_init);
5503 /* Initialization of an array of chars from a string constant
5504 optionally enclosed in braces. */
5506 if (code == ARRAY_TYPE && inside_init
5507 && TREE_CODE (inside_init) == STRING_CST)
5509 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5510 /* Note that an array could be both an array of character type
5511 and an array of wchar_t if wchar_t is signed char or unsigned
5512 char. */
5513 bool char_array = (typ1 == char_type_node
5514 || typ1 == signed_char_type_node
5515 || typ1 == unsigned_char_type_node);
5516 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5517 bool char16_array = !!comptypes (typ1, char16_type_node);
5518 bool char32_array = !!comptypes (typ1, char32_type_node);
5520 if (char_array || wchar_array || char16_array || char32_array)
5522 struct c_expr expr;
5523 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5524 expr.value = inside_init;
5525 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5526 expr.original_type = NULL;
5527 maybe_warn_string_init (type, expr);
5529 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5530 pedwarn_init (init_loc, OPT_pedantic,
5531 "initialization of a flexible array member");
5533 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5534 TYPE_MAIN_VARIANT (type)))
5535 return inside_init;
5537 if (char_array)
5539 if (typ2 != char_type_node)
5541 error_init ("char-array initialized from wide string");
5542 return error_mark_node;
5545 else
5547 if (typ2 == char_type_node)
5549 error_init ("wide character array initialized from non-wide "
5550 "string");
5551 return error_mark_node;
5553 else if (!comptypes(typ1, typ2))
5555 error_init ("wide character array initialized from "
5556 "incompatible wide string");
5557 return error_mark_node;
5561 TREE_TYPE (inside_init) = type;
5562 if (TYPE_DOMAIN (type) != 0
5563 && TYPE_SIZE (type) != 0
5564 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5566 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5568 /* Subtract the size of a single (possibly wide) character
5569 because it's ok to ignore the terminating null char
5570 that is counted in the length of the constant. */
5571 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5572 (len
5573 - (TYPE_PRECISION (typ1)
5574 / BITS_PER_UNIT))))
5575 pedwarn_init (init_loc, 0,
5576 ("initializer-string for array of chars "
5577 "is too long"));
5578 else if (warn_cxx_compat
5579 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5580 warning_at (init_loc, OPT_Wc___compat,
5581 ("initializer-string for array chars "
5582 "is too long for C++"));
5585 return inside_init;
5587 else if (INTEGRAL_TYPE_P (typ1))
5589 error_init ("array of inappropriate type initialized "
5590 "from string constant");
5591 return error_mark_node;
5595 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5596 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5597 below and handle as a constructor. */
5598 if (code == VECTOR_TYPE
5599 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5600 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5601 && TREE_CONSTANT (inside_init))
5603 if (TREE_CODE (inside_init) == VECTOR_CST
5604 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5605 TYPE_MAIN_VARIANT (type)))
5606 return inside_init;
5608 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5610 unsigned HOST_WIDE_INT ix;
5611 tree value;
5612 bool constant_p = true;
5614 /* Iterate through elements and check if all constructor
5615 elements are *_CSTs. */
5616 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5617 if (!CONSTANT_CLASS_P (value))
5619 constant_p = false;
5620 break;
5623 if (constant_p)
5624 return build_vector_from_ctor (type,
5625 CONSTRUCTOR_ELTS (inside_init));
5629 if (warn_sequence_point)
5630 verify_sequence_points (inside_init);
5632 /* Any type can be initialized
5633 from an expression of the same type, optionally with braces. */
5635 if (inside_init && TREE_TYPE (inside_init) != 0
5636 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5637 TYPE_MAIN_VARIANT (type))
5638 || (code == ARRAY_TYPE
5639 && comptypes (TREE_TYPE (inside_init), type))
5640 || (code == VECTOR_TYPE
5641 && comptypes (TREE_TYPE (inside_init), type))
5642 || (code == POINTER_TYPE
5643 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5644 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5645 TREE_TYPE (type)))))
5647 if (code == POINTER_TYPE)
5649 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5651 if (TREE_CODE (inside_init) == STRING_CST
5652 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5653 inside_init = array_to_pointer_conversion
5654 (init_loc, inside_init);
5655 else
5657 error_init ("invalid use of non-lvalue array");
5658 return error_mark_node;
5663 if (code == VECTOR_TYPE)
5664 /* Although the types are compatible, we may require a
5665 conversion. */
5666 inside_init = convert (type, inside_init);
5668 if (require_constant
5669 && (code == VECTOR_TYPE || !flag_isoc99)
5670 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5672 /* As an extension, allow initializing objects with static storage
5673 duration with compound literals (which are then treated just as
5674 the brace enclosed list they contain). Also allow this for
5675 vectors, as we can only assign them with compound literals. */
5676 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5677 inside_init = DECL_INITIAL (decl);
5680 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5681 && TREE_CODE (inside_init) != CONSTRUCTOR)
5683 error_init ("array initialized from non-constant array expression");
5684 return error_mark_node;
5687 /* Compound expressions can only occur here if -pedantic or
5688 -pedantic-errors is specified. In the later case, we always want
5689 an error. In the former case, we simply want a warning. */
5690 if (require_constant && pedantic
5691 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5693 inside_init
5694 = valid_compound_expr_initializer (inside_init,
5695 TREE_TYPE (inside_init));
5696 if (inside_init == error_mark_node)
5697 error_init ("initializer element is not constant");
5698 else
5699 pedwarn_init (init_loc, OPT_pedantic,
5700 "initializer element is not constant");
5701 if (flag_pedantic_errors)
5702 inside_init = error_mark_node;
5704 else if (require_constant
5705 && !initializer_constant_valid_p (inside_init,
5706 TREE_TYPE (inside_init)))
5708 error_init ("initializer element is not constant");
5709 inside_init = error_mark_node;
5711 else if (require_constant && !maybe_const)
5712 pedwarn_init (init_loc, 0,
5713 "initializer element is not a constant expression");
5715 /* Added to enable additional -Wmissing-format-attribute warnings. */
5716 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5717 inside_init = convert_for_assignment (init_loc, type, inside_init,
5718 origtype,
5719 ic_init, null_pointer_constant,
5720 NULL_TREE, NULL_TREE, 0);
5721 return inside_init;
5724 /* Handle scalar types, including conversions. */
5726 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5727 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5728 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5730 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5731 && (TREE_CODE (init) == STRING_CST
5732 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5733 inside_init = init = array_to_pointer_conversion (init_loc, init);
5734 if (semantic_type)
5735 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5736 inside_init);
5737 inside_init
5738 = convert_for_assignment (init_loc, type, inside_init, origtype,
5739 ic_init, null_pointer_constant,
5740 NULL_TREE, NULL_TREE, 0);
5742 /* Check to see if we have already given an error message. */
5743 if (inside_init == error_mark_node)
5745 else if (require_constant && !TREE_CONSTANT (inside_init))
5747 error_init ("initializer element is not constant");
5748 inside_init = error_mark_node;
5750 else if (require_constant
5751 && !initializer_constant_valid_p (inside_init,
5752 TREE_TYPE (inside_init)))
5754 error_init ("initializer element is not computable at load time");
5755 inside_init = error_mark_node;
5757 else if (require_constant && !maybe_const)
5758 pedwarn_init (init_loc, 0,
5759 "initializer element is not a constant expression");
5761 return inside_init;
5764 /* Come here only for records and arrays. */
5766 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5768 error_init ("variable-sized object may not be initialized");
5769 return error_mark_node;
5772 error_init ("invalid initializer");
5773 return error_mark_node;
5776 /* Handle initializers that use braces. */
5778 /* Type of object we are accumulating a constructor for.
5779 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5780 static tree constructor_type;
5782 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5783 left to fill. */
5784 static tree constructor_fields;
5786 /* For an ARRAY_TYPE, this is the specified index
5787 at which to store the next element we get. */
5788 static tree constructor_index;
5790 /* For an ARRAY_TYPE, this is the maximum index. */
5791 static tree constructor_max_index;
5793 /* For a RECORD_TYPE, this is the first field not yet written out. */
5794 static tree constructor_unfilled_fields;
5796 /* For an ARRAY_TYPE, this is the index of the first element
5797 not yet written out. */
5798 static tree constructor_unfilled_index;
5800 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5801 This is so we can generate gaps between fields, when appropriate. */
5802 static tree constructor_bit_index;
5804 /* If we are saving up the elements rather than allocating them,
5805 this is the list of elements so far (in reverse order,
5806 most recent first). */
5807 static VEC(constructor_elt,gc) *constructor_elements;
5809 /* 1 if constructor should be incrementally stored into a constructor chain,
5810 0 if all the elements should be kept in AVL tree. */
5811 static int constructor_incremental;
5813 /* 1 if so far this constructor's elements are all compile-time constants. */
5814 static int constructor_constant;
5816 /* 1 if so far this constructor's elements are all valid address constants. */
5817 static int constructor_simple;
5819 /* 1 if this constructor has an element that cannot be part of a
5820 constant expression. */
5821 static int constructor_nonconst;
5823 /* 1 if this constructor is erroneous so far. */
5824 static int constructor_erroneous;
5826 /* Structure for managing pending initializer elements, organized as an
5827 AVL tree. */
5829 struct init_node
5831 struct init_node *left, *right;
5832 struct init_node *parent;
5833 int balance;
5834 tree purpose;
5835 tree value;
5836 tree origtype;
5839 /* Tree of pending elements at this constructor level.
5840 These are elements encountered out of order
5841 which belong at places we haven't reached yet in actually
5842 writing the output.
5843 Will never hold tree nodes across GC runs. */
5844 static struct init_node *constructor_pending_elts;
5846 /* The SPELLING_DEPTH of this constructor. */
5847 static int constructor_depth;
5849 /* DECL node for which an initializer is being read.
5850 0 means we are reading a constructor expression
5851 such as (struct foo) {...}. */
5852 static tree constructor_decl;
5854 /* Nonzero if this is an initializer for a top-level decl. */
5855 static int constructor_top_level;
5857 /* Nonzero if there were any member designators in this initializer. */
5858 static int constructor_designated;
5860 /* Nesting depth of designator list. */
5861 static int designator_depth;
5863 /* Nonzero if there were diagnosed errors in this designator list. */
5864 static int designator_erroneous;
5867 /* This stack has a level for each implicit or explicit level of
5868 structuring in the initializer, including the outermost one. It
5869 saves the values of most of the variables above. */
5871 struct constructor_range_stack;
5873 struct constructor_stack
5875 struct constructor_stack *next;
5876 tree type;
5877 tree fields;
5878 tree index;
5879 tree max_index;
5880 tree unfilled_index;
5881 tree unfilled_fields;
5882 tree bit_index;
5883 VEC(constructor_elt,gc) *elements;
5884 struct init_node *pending_elts;
5885 int offset;
5886 int depth;
5887 /* If value nonzero, this value should replace the entire
5888 constructor at this level. */
5889 struct c_expr replacement_value;
5890 struct constructor_range_stack *range_stack;
5891 char constant;
5892 char simple;
5893 char nonconst;
5894 char implicit;
5895 char erroneous;
5896 char outer;
5897 char incremental;
5898 char designated;
5901 static struct constructor_stack *constructor_stack;
5903 /* This stack represents designators from some range designator up to
5904 the last designator in the list. */
5906 struct constructor_range_stack
5908 struct constructor_range_stack *next, *prev;
5909 struct constructor_stack *stack;
5910 tree range_start;
5911 tree index;
5912 tree range_end;
5913 tree fields;
5916 static struct constructor_range_stack *constructor_range_stack;
5918 /* This stack records separate initializers that are nested.
5919 Nested initializers can't happen in ANSI C, but GNU C allows them
5920 in cases like { ... (struct foo) { ... } ... }. */
5922 struct initializer_stack
5924 struct initializer_stack *next;
5925 tree decl;
5926 struct constructor_stack *constructor_stack;
5927 struct constructor_range_stack *constructor_range_stack;
5928 VEC(constructor_elt,gc) *elements;
5929 struct spelling *spelling;
5930 struct spelling *spelling_base;
5931 int spelling_size;
5932 char top_level;
5933 char require_constant_value;
5934 char require_constant_elements;
5937 static struct initializer_stack *initializer_stack;
5939 /* Prepare to parse and output the initializer for variable DECL. */
5941 void
5942 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5944 const char *locus;
5945 struct initializer_stack *p = XNEW (struct initializer_stack);
5947 p->decl = constructor_decl;
5948 p->require_constant_value = require_constant_value;
5949 p->require_constant_elements = require_constant_elements;
5950 p->constructor_stack = constructor_stack;
5951 p->constructor_range_stack = constructor_range_stack;
5952 p->elements = constructor_elements;
5953 p->spelling = spelling;
5954 p->spelling_base = spelling_base;
5955 p->spelling_size = spelling_size;
5956 p->top_level = constructor_top_level;
5957 p->next = initializer_stack;
5958 initializer_stack = p;
5960 constructor_decl = decl;
5961 constructor_designated = 0;
5962 constructor_top_level = top_level;
5964 if (decl != 0 && decl != error_mark_node)
5966 require_constant_value = TREE_STATIC (decl);
5967 require_constant_elements
5968 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5969 /* For a scalar, you can always use any value to initialize,
5970 even within braces. */
5971 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5972 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5973 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5974 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5975 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
5977 else
5979 require_constant_value = 0;
5980 require_constant_elements = 0;
5981 locus = _("(anonymous)");
5984 constructor_stack = 0;
5985 constructor_range_stack = 0;
5987 missing_braces_mentioned = 0;
5989 spelling_base = 0;
5990 spelling_size = 0;
5991 RESTORE_SPELLING_DEPTH (0);
5993 if (locus)
5994 push_string (locus);
5997 void
5998 finish_init (void)
6000 struct initializer_stack *p = initializer_stack;
6002 /* Free the whole constructor stack of this initializer. */
6003 while (constructor_stack)
6005 struct constructor_stack *q = constructor_stack;
6006 constructor_stack = q->next;
6007 free (q);
6010 gcc_assert (!constructor_range_stack);
6012 /* Pop back to the data of the outer initializer (if any). */
6013 free (spelling_base);
6015 constructor_decl = p->decl;
6016 require_constant_value = p->require_constant_value;
6017 require_constant_elements = p->require_constant_elements;
6018 constructor_stack = p->constructor_stack;
6019 constructor_range_stack = p->constructor_range_stack;
6020 constructor_elements = p->elements;
6021 spelling = p->spelling;
6022 spelling_base = p->spelling_base;
6023 spelling_size = p->spelling_size;
6024 constructor_top_level = p->top_level;
6025 initializer_stack = p->next;
6026 free (p);
6029 /* Call here when we see the initializer is surrounded by braces.
6030 This is instead of a call to push_init_level;
6031 it is matched by a call to pop_init_level.
6033 TYPE is the type to initialize, for a constructor expression.
6034 For an initializer for a decl, TYPE is zero. */
6036 void
6037 really_start_incremental_init (tree type)
6039 struct constructor_stack *p = XNEW (struct constructor_stack);
6041 if (type == 0)
6042 type = TREE_TYPE (constructor_decl);
6044 if (TREE_CODE (type) == VECTOR_TYPE
6045 && TYPE_VECTOR_OPAQUE (type))
6046 error ("opaque vector types cannot be initialized");
6048 p->type = constructor_type;
6049 p->fields = constructor_fields;
6050 p->index = constructor_index;
6051 p->max_index = constructor_max_index;
6052 p->unfilled_index = constructor_unfilled_index;
6053 p->unfilled_fields = constructor_unfilled_fields;
6054 p->bit_index = constructor_bit_index;
6055 p->elements = constructor_elements;
6056 p->constant = constructor_constant;
6057 p->simple = constructor_simple;
6058 p->nonconst = constructor_nonconst;
6059 p->erroneous = constructor_erroneous;
6060 p->pending_elts = constructor_pending_elts;
6061 p->depth = constructor_depth;
6062 p->replacement_value.value = 0;
6063 p->replacement_value.original_code = ERROR_MARK;
6064 p->replacement_value.original_type = NULL;
6065 p->implicit = 0;
6066 p->range_stack = 0;
6067 p->outer = 0;
6068 p->incremental = constructor_incremental;
6069 p->designated = constructor_designated;
6070 p->next = 0;
6071 constructor_stack = p;
6073 constructor_constant = 1;
6074 constructor_simple = 1;
6075 constructor_nonconst = 0;
6076 constructor_depth = SPELLING_DEPTH ();
6077 constructor_elements = 0;
6078 constructor_pending_elts = 0;
6079 constructor_type = type;
6080 constructor_incremental = 1;
6081 constructor_designated = 0;
6082 designator_depth = 0;
6083 designator_erroneous = 0;
6085 if (TREE_CODE (constructor_type) == RECORD_TYPE
6086 || TREE_CODE (constructor_type) == UNION_TYPE)
6088 constructor_fields = TYPE_FIELDS (constructor_type);
6089 /* Skip any nameless bit fields at the beginning. */
6090 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6091 && DECL_NAME (constructor_fields) == 0)
6092 constructor_fields = TREE_CHAIN (constructor_fields);
6094 constructor_unfilled_fields = constructor_fields;
6095 constructor_bit_index = bitsize_zero_node;
6097 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6099 if (TYPE_DOMAIN (constructor_type))
6101 constructor_max_index
6102 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6104 /* Detect non-empty initializations of zero-length arrays. */
6105 if (constructor_max_index == NULL_TREE
6106 && TYPE_SIZE (constructor_type))
6107 constructor_max_index = build_int_cst (NULL_TREE, -1);
6109 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6110 to initialize VLAs will cause a proper error; avoid tree
6111 checking errors as well by setting a safe value. */
6112 if (constructor_max_index
6113 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6114 constructor_max_index = build_int_cst (NULL_TREE, -1);
6116 constructor_index
6117 = convert (bitsizetype,
6118 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6120 else
6122 constructor_index = bitsize_zero_node;
6123 constructor_max_index = NULL_TREE;
6126 constructor_unfilled_index = constructor_index;
6128 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6130 /* Vectors are like simple fixed-size arrays. */
6131 constructor_max_index =
6132 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6133 constructor_index = bitsize_zero_node;
6134 constructor_unfilled_index = constructor_index;
6136 else
6138 /* Handle the case of int x = {5}; */
6139 constructor_fields = constructor_type;
6140 constructor_unfilled_fields = constructor_type;
6144 /* Push down into a subobject, for initialization.
6145 If this is for an explicit set of braces, IMPLICIT is 0.
6146 If it is because the next element belongs at a lower level,
6147 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6149 void
6150 push_init_level (int implicit)
6152 struct constructor_stack *p;
6153 tree value = NULL_TREE;
6155 /* If we've exhausted any levels that didn't have braces,
6156 pop them now. If implicit == 1, this will have been done in
6157 process_init_element; do not repeat it here because in the case
6158 of excess initializers for an empty aggregate this leads to an
6159 infinite cycle of popping a level and immediately recreating
6160 it. */
6161 if (implicit != 1)
6163 while (constructor_stack->implicit)
6165 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6166 || TREE_CODE (constructor_type) == UNION_TYPE)
6167 && constructor_fields == 0)
6168 process_init_element (pop_init_level (1), true);
6169 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6170 && constructor_max_index
6171 && tree_int_cst_lt (constructor_max_index,
6172 constructor_index))
6173 process_init_element (pop_init_level (1), true);
6174 else
6175 break;
6179 /* Unless this is an explicit brace, we need to preserve previous
6180 content if any. */
6181 if (implicit)
6183 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6184 || TREE_CODE (constructor_type) == UNION_TYPE)
6185 && constructor_fields)
6186 value = find_init_member (constructor_fields);
6187 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6188 value = find_init_member (constructor_index);
6191 p = XNEW (struct constructor_stack);
6192 p->type = constructor_type;
6193 p->fields = constructor_fields;
6194 p->index = constructor_index;
6195 p->max_index = constructor_max_index;
6196 p->unfilled_index = constructor_unfilled_index;
6197 p->unfilled_fields = constructor_unfilled_fields;
6198 p->bit_index = constructor_bit_index;
6199 p->elements = constructor_elements;
6200 p->constant = constructor_constant;
6201 p->simple = constructor_simple;
6202 p->nonconst = constructor_nonconst;
6203 p->erroneous = constructor_erroneous;
6204 p->pending_elts = constructor_pending_elts;
6205 p->depth = constructor_depth;
6206 p->replacement_value.value = 0;
6207 p->replacement_value.original_code = ERROR_MARK;
6208 p->replacement_value.original_type = NULL;
6209 p->implicit = implicit;
6210 p->outer = 0;
6211 p->incremental = constructor_incremental;
6212 p->designated = constructor_designated;
6213 p->next = constructor_stack;
6214 p->range_stack = 0;
6215 constructor_stack = p;
6217 constructor_constant = 1;
6218 constructor_simple = 1;
6219 constructor_nonconst = 0;
6220 constructor_depth = SPELLING_DEPTH ();
6221 constructor_elements = 0;
6222 constructor_incremental = 1;
6223 constructor_designated = 0;
6224 constructor_pending_elts = 0;
6225 if (!implicit)
6227 p->range_stack = constructor_range_stack;
6228 constructor_range_stack = 0;
6229 designator_depth = 0;
6230 designator_erroneous = 0;
6233 /* Don't die if an entire brace-pair level is superfluous
6234 in the containing level. */
6235 if (constructor_type == 0)
6237 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6238 || TREE_CODE (constructor_type) == UNION_TYPE)
6240 /* Don't die if there are extra init elts at the end. */
6241 if (constructor_fields == 0)
6242 constructor_type = 0;
6243 else
6245 constructor_type = TREE_TYPE (constructor_fields);
6246 push_member_name (constructor_fields);
6247 constructor_depth++;
6250 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6252 constructor_type = TREE_TYPE (constructor_type);
6253 push_array_bounds (tree_low_cst (constructor_index, 1));
6254 constructor_depth++;
6257 if (constructor_type == 0)
6259 error_init ("extra brace group at end of initializer");
6260 constructor_fields = 0;
6261 constructor_unfilled_fields = 0;
6262 return;
6265 if (value && TREE_CODE (value) == CONSTRUCTOR)
6267 constructor_constant = TREE_CONSTANT (value);
6268 constructor_simple = TREE_STATIC (value);
6269 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6270 constructor_elements = CONSTRUCTOR_ELTS (value);
6271 if (!VEC_empty (constructor_elt, constructor_elements)
6272 && (TREE_CODE (constructor_type) == RECORD_TYPE
6273 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6274 set_nonincremental_init ();
6277 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6279 missing_braces_mentioned = 1;
6280 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6283 if (TREE_CODE (constructor_type) == RECORD_TYPE
6284 || TREE_CODE (constructor_type) == UNION_TYPE)
6286 constructor_fields = TYPE_FIELDS (constructor_type);
6287 /* Skip any nameless bit fields at the beginning. */
6288 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6289 && DECL_NAME (constructor_fields) == 0)
6290 constructor_fields = TREE_CHAIN (constructor_fields);
6292 constructor_unfilled_fields = constructor_fields;
6293 constructor_bit_index = bitsize_zero_node;
6295 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6297 /* Vectors are like simple fixed-size arrays. */
6298 constructor_max_index =
6299 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6300 constructor_index = convert (bitsizetype, integer_zero_node);
6301 constructor_unfilled_index = constructor_index;
6303 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6305 if (TYPE_DOMAIN (constructor_type))
6307 constructor_max_index
6308 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6310 /* Detect non-empty initializations of zero-length arrays. */
6311 if (constructor_max_index == NULL_TREE
6312 && TYPE_SIZE (constructor_type))
6313 constructor_max_index = build_int_cst (NULL_TREE, -1);
6315 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6316 to initialize VLAs will cause a proper error; avoid tree
6317 checking errors as well by setting a safe value. */
6318 if (constructor_max_index
6319 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6320 constructor_max_index = build_int_cst (NULL_TREE, -1);
6322 constructor_index
6323 = convert (bitsizetype,
6324 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6326 else
6327 constructor_index = bitsize_zero_node;
6329 constructor_unfilled_index = constructor_index;
6330 if (value && TREE_CODE (value) == STRING_CST)
6332 /* We need to split the char/wchar array into individual
6333 characters, so that we don't have to special case it
6334 everywhere. */
6335 set_nonincremental_init_from_string (value);
6338 else
6340 if (constructor_type != error_mark_node)
6341 warning_init (0, "braces around scalar initializer");
6342 constructor_fields = constructor_type;
6343 constructor_unfilled_fields = constructor_type;
6347 /* At the end of an implicit or explicit brace level,
6348 finish up that level of constructor. If a single expression
6349 with redundant braces initialized that level, return the
6350 c_expr structure for that expression. Otherwise, the original_code
6351 element is set to ERROR_MARK.
6352 If we were outputting the elements as they are read, return 0 as the value
6353 from inner levels (process_init_element ignores that),
6354 but return error_mark_node as the value from the outermost level
6355 (that's what we want to put in DECL_INITIAL).
6356 Otherwise, return a CONSTRUCTOR expression as the value. */
6358 struct c_expr
6359 pop_init_level (int implicit)
6361 struct constructor_stack *p;
6362 struct c_expr ret;
6363 ret.value = 0;
6364 ret.original_code = ERROR_MARK;
6365 ret.original_type = NULL;
6367 if (implicit == 0)
6369 /* When we come to an explicit close brace,
6370 pop any inner levels that didn't have explicit braces. */
6371 while (constructor_stack->implicit)
6372 process_init_element (pop_init_level (1), true);
6374 gcc_assert (!constructor_range_stack);
6377 /* Now output all pending elements. */
6378 constructor_incremental = 1;
6379 output_pending_init_elements (1);
6381 p = constructor_stack;
6383 /* Error for initializing a flexible array member, or a zero-length
6384 array member in an inappropriate context. */
6385 if (constructor_type && constructor_fields
6386 && TREE_CODE (constructor_type) == ARRAY_TYPE
6387 && TYPE_DOMAIN (constructor_type)
6388 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6390 /* Silently discard empty initializations. The parser will
6391 already have pedwarned for empty brackets. */
6392 if (integer_zerop (constructor_unfilled_index))
6393 constructor_type = NULL_TREE;
6394 else
6396 gcc_assert (!TYPE_SIZE (constructor_type));
6398 if (constructor_depth > 2)
6399 error_init ("initialization of flexible array member in a nested context");
6400 else
6401 pedwarn_init (input_location, OPT_pedantic,
6402 "initialization of a flexible array member");
6404 /* We have already issued an error message for the existence
6405 of a flexible array member not at the end of the structure.
6406 Discard the initializer so that we do not die later. */
6407 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6408 constructor_type = NULL_TREE;
6412 /* Warn when some struct elements are implicitly initialized to zero. */
6413 if (warn_missing_field_initializers
6414 && constructor_type
6415 && TREE_CODE (constructor_type) == RECORD_TYPE
6416 && constructor_unfilled_fields)
6418 /* Do not warn for flexible array members or zero-length arrays. */
6419 while (constructor_unfilled_fields
6420 && (!DECL_SIZE (constructor_unfilled_fields)
6421 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6422 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6424 /* Do not warn if this level of the initializer uses member
6425 designators; it is likely to be deliberate. */
6426 if (constructor_unfilled_fields && !constructor_designated)
6428 push_member_name (constructor_unfilled_fields);
6429 warning_init (OPT_Wmissing_field_initializers,
6430 "missing initializer");
6431 RESTORE_SPELLING_DEPTH (constructor_depth);
6435 /* Pad out the end of the structure. */
6436 if (p->replacement_value.value)
6437 /* If this closes a superfluous brace pair,
6438 just pass out the element between them. */
6439 ret = p->replacement_value;
6440 else if (constructor_type == 0)
6442 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6443 && TREE_CODE (constructor_type) != UNION_TYPE
6444 && TREE_CODE (constructor_type) != ARRAY_TYPE
6445 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6447 /* A nonincremental scalar initializer--just return
6448 the element, after verifying there is just one. */
6449 if (VEC_empty (constructor_elt,constructor_elements))
6451 if (!constructor_erroneous)
6452 error_init ("empty scalar initializer");
6453 ret.value = error_mark_node;
6455 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6457 error_init ("extra elements in scalar initializer");
6458 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6460 else
6461 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6463 else
6465 if (constructor_erroneous)
6466 ret.value = error_mark_node;
6467 else
6469 ret.value = build_constructor (constructor_type,
6470 constructor_elements);
6471 if (constructor_constant)
6472 TREE_CONSTANT (ret.value) = 1;
6473 if (constructor_constant && constructor_simple)
6474 TREE_STATIC (ret.value) = 1;
6475 if (constructor_nonconst)
6476 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6480 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6482 if (constructor_nonconst)
6483 ret.original_code = C_MAYBE_CONST_EXPR;
6484 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6485 ret.original_code = ERROR_MARK;
6488 constructor_type = p->type;
6489 constructor_fields = p->fields;
6490 constructor_index = p->index;
6491 constructor_max_index = p->max_index;
6492 constructor_unfilled_index = p->unfilled_index;
6493 constructor_unfilled_fields = p->unfilled_fields;
6494 constructor_bit_index = p->bit_index;
6495 constructor_elements = p->elements;
6496 constructor_constant = p->constant;
6497 constructor_simple = p->simple;
6498 constructor_nonconst = p->nonconst;
6499 constructor_erroneous = p->erroneous;
6500 constructor_incremental = p->incremental;
6501 constructor_designated = p->designated;
6502 constructor_pending_elts = p->pending_elts;
6503 constructor_depth = p->depth;
6504 if (!p->implicit)
6505 constructor_range_stack = p->range_stack;
6506 RESTORE_SPELLING_DEPTH (constructor_depth);
6508 constructor_stack = p->next;
6509 free (p);
6511 if (ret.value == 0 && constructor_stack == 0)
6512 ret.value = error_mark_node;
6513 return ret;
6516 /* Common handling for both array range and field name designators.
6517 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6519 static int
6520 set_designator (int array)
6522 tree subtype;
6523 enum tree_code subcode;
6525 /* Don't die if an entire brace-pair level is superfluous
6526 in the containing level. */
6527 if (constructor_type == 0)
6528 return 1;
6530 /* If there were errors in this designator list already, bail out
6531 silently. */
6532 if (designator_erroneous)
6533 return 1;
6535 if (!designator_depth)
6537 gcc_assert (!constructor_range_stack);
6539 /* Designator list starts at the level of closest explicit
6540 braces. */
6541 while (constructor_stack->implicit)
6542 process_init_element (pop_init_level (1), true);
6543 constructor_designated = 1;
6544 return 0;
6547 switch (TREE_CODE (constructor_type))
6549 case RECORD_TYPE:
6550 case UNION_TYPE:
6551 subtype = TREE_TYPE (constructor_fields);
6552 if (subtype != error_mark_node)
6553 subtype = TYPE_MAIN_VARIANT (subtype);
6554 break;
6555 case ARRAY_TYPE:
6556 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6557 break;
6558 default:
6559 gcc_unreachable ();
6562 subcode = TREE_CODE (subtype);
6563 if (array && subcode != ARRAY_TYPE)
6565 error_init ("array index in non-array initializer");
6566 return 1;
6568 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6570 error_init ("field name not in record or union initializer");
6571 return 1;
6574 constructor_designated = 1;
6575 push_init_level (2);
6576 return 0;
6579 /* If there are range designators in designator list, push a new designator
6580 to constructor_range_stack. RANGE_END is end of such stack range or
6581 NULL_TREE if there is no range designator at this level. */
6583 static void
6584 push_range_stack (tree range_end)
6586 struct constructor_range_stack *p;
6588 p = GGC_NEW (struct constructor_range_stack);
6589 p->prev = constructor_range_stack;
6590 p->next = 0;
6591 p->fields = constructor_fields;
6592 p->range_start = constructor_index;
6593 p->index = constructor_index;
6594 p->stack = constructor_stack;
6595 p->range_end = range_end;
6596 if (constructor_range_stack)
6597 constructor_range_stack->next = p;
6598 constructor_range_stack = p;
6601 /* Within an array initializer, specify the next index to be initialized.
6602 FIRST is that index. If LAST is nonzero, then initialize a range
6603 of indices, running from FIRST through LAST. */
6605 void
6606 set_init_index (tree first, tree last)
6608 if (set_designator (1))
6609 return;
6611 designator_erroneous = 1;
6613 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6614 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6616 error_init ("array index in initializer not of integer type");
6617 return;
6620 if (TREE_CODE (first) != INTEGER_CST)
6622 first = c_fully_fold (first, false, NULL);
6623 if (TREE_CODE (first) == INTEGER_CST)
6624 pedwarn_init (input_location, OPT_pedantic,
6625 "array index in initializer is not "
6626 "an integer constant expression");
6629 if (last && TREE_CODE (last) != INTEGER_CST)
6631 last = c_fully_fold (last, false, NULL);
6632 if (TREE_CODE (last) == INTEGER_CST)
6633 pedwarn_init (input_location, OPT_pedantic,
6634 "array index in initializer is not "
6635 "an integer constant expression");
6638 if (TREE_CODE (first) != INTEGER_CST)
6639 error_init ("nonconstant array index in initializer");
6640 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6641 error_init ("nonconstant array index in initializer");
6642 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6643 error_init ("array index in non-array initializer");
6644 else if (tree_int_cst_sgn (first) == -1)
6645 error_init ("array index in initializer exceeds array bounds");
6646 else if (constructor_max_index
6647 && tree_int_cst_lt (constructor_max_index, first))
6648 error_init ("array index in initializer exceeds array bounds");
6649 else
6651 constant_expression_warning (first);
6652 if (last)
6653 constant_expression_warning (last);
6654 constructor_index = convert (bitsizetype, first);
6656 if (last)
6658 if (tree_int_cst_equal (first, last))
6659 last = 0;
6660 else if (tree_int_cst_lt (last, first))
6662 error_init ("empty index range in initializer");
6663 last = 0;
6665 else
6667 last = convert (bitsizetype, last);
6668 if (constructor_max_index != 0
6669 && tree_int_cst_lt (constructor_max_index, last))
6671 error_init ("array index range in initializer exceeds array bounds");
6672 last = 0;
6677 designator_depth++;
6678 designator_erroneous = 0;
6679 if (constructor_range_stack || last)
6680 push_range_stack (last);
6684 /* Within a struct initializer, specify the next field to be initialized. */
6686 void
6687 set_init_label (tree fieldname)
6689 tree tail;
6691 if (set_designator (0))
6692 return;
6694 designator_erroneous = 1;
6696 if (TREE_CODE (constructor_type) != RECORD_TYPE
6697 && TREE_CODE (constructor_type) != UNION_TYPE)
6699 error_init ("field name not in record or union initializer");
6700 return;
6703 for (tail = TYPE_FIELDS (constructor_type); tail;
6704 tail = TREE_CHAIN (tail))
6706 if (DECL_NAME (tail) == fieldname)
6707 break;
6710 if (tail == 0)
6711 error ("unknown field %qE specified in initializer", fieldname);
6712 else
6714 constructor_fields = tail;
6715 designator_depth++;
6716 designator_erroneous = 0;
6717 if (constructor_range_stack)
6718 push_range_stack (NULL_TREE);
6722 /* Add a new initializer to the tree of pending initializers. PURPOSE
6723 identifies the initializer, either array index or field in a structure.
6724 VALUE is the value of that index or field. If ORIGTYPE is not
6725 NULL_TREE, it is the original type of VALUE.
6727 IMPLICIT is true if value comes from pop_init_level (1),
6728 the new initializer has been merged with the existing one
6729 and thus no warnings should be emitted about overriding an
6730 existing initializer. */
6732 static void
6733 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6735 struct init_node *p, **q, *r;
6737 q = &constructor_pending_elts;
6738 p = 0;
6740 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6742 while (*q != 0)
6744 p = *q;
6745 if (tree_int_cst_lt (purpose, p->purpose))
6746 q = &p->left;
6747 else if (tree_int_cst_lt (p->purpose, purpose))
6748 q = &p->right;
6749 else
6751 if (!implicit)
6753 if (TREE_SIDE_EFFECTS (p->value))
6754 warning_init (0, "initialized field with side-effects overwritten");
6755 else if (warn_override_init)
6756 warning_init (OPT_Woverride_init, "initialized field overwritten");
6758 p->value = value;
6759 p->origtype = origtype;
6760 return;
6764 else
6766 tree bitpos;
6768 bitpos = bit_position (purpose);
6769 while (*q != NULL)
6771 p = *q;
6772 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6773 q = &p->left;
6774 else if (p->purpose != purpose)
6775 q = &p->right;
6776 else
6778 if (!implicit)
6780 if (TREE_SIDE_EFFECTS (p->value))
6781 warning_init (0, "initialized field with side-effects overwritten");
6782 else if (warn_override_init)
6783 warning_init (OPT_Woverride_init, "initialized field overwritten");
6785 p->value = value;
6786 p->origtype = origtype;
6787 return;
6792 r = GGC_NEW (struct init_node);
6793 r->purpose = purpose;
6794 r->value = value;
6795 r->origtype = origtype;
6797 *q = r;
6798 r->parent = p;
6799 r->left = 0;
6800 r->right = 0;
6801 r->balance = 0;
6803 while (p)
6805 struct init_node *s;
6807 if (r == p->left)
6809 if (p->balance == 0)
6810 p->balance = -1;
6811 else if (p->balance < 0)
6813 if (r->balance < 0)
6815 /* L rotation. */
6816 p->left = r->right;
6817 if (p->left)
6818 p->left->parent = p;
6819 r->right = p;
6821 p->balance = 0;
6822 r->balance = 0;
6824 s = p->parent;
6825 p->parent = r;
6826 r->parent = s;
6827 if (s)
6829 if (s->left == p)
6830 s->left = r;
6831 else
6832 s->right = r;
6834 else
6835 constructor_pending_elts = r;
6837 else
6839 /* LR rotation. */
6840 struct init_node *t = r->right;
6842 r->right = t->left;
6843 if (r->right)
6844 r->right->parent = r;
6845 t->left = r;
6847 p->left = t->right;
6848 if (p->left)
6849 p->left->parent = p;
6850 t->right = p;
6852 p->balance = t->balance < 0;
6853 r->balance = -(t->balance > 0);
6854 t->balance = 0;
6856 s = p->parent;
6857 p->parent = t;
6858 r->parent = t;
6859 t->parent = s;
6860 if (s)
6862 if (s->left == p)
6863 s->left = t;
6864 else
6865 s->right = t;
6867 else
6868 constructor_pending_elts = t;
6870 break;
6872 else
6874 /* p->balance == +1; growth of left side balances the node. */
6875 p->balance = 0;
6876 break;
6879 else /* r == p->right */
6881 if (p->balance == 0)
6882 /* Growth propagation from right side. */
6883 p->balance++;
6884 else if (p->balance > 0)
6886 if (r->balance > 0)
6888 /* R rotation. */
6889 p->right = r->left;
6890 if (p->right)
6891 p->right->parent = p;
6892 r->left = p;
6894 p->balance = 0;
6895 r->balance = 0;
6897 s = p->parent;
6898 p->parent = r;
6899 r->parent = s;
6900 if (s)
6902 if (s->left == p)
6903 s->left = r;
6904 else
6905 s->right = r;
6907 else
6908 constructor_pending_elts = r;
6910 else /* r->balance == -1 */
6912 /* RL rotation */
6913 struct init_node *t = r->left;
6915 r->left = t->right;
6916 if (r->left)
6917 r->left->parent = r;
6918 t->right = r;
6920 p->right = t->left;
6921 if (p->right)
6922 p->right->parent = p;
6923 t->left = p;
6925 r->balance = (t->balance < 0);
6926 p->balance = -(t->balance > 0);
6927 t->balance = 0;
6929 s = p->parent;
6930 p->parent = t;
6931 r->parent = t;
6932 t->parent = s;
6933 if (s)
6935 if (s->left == p)
6936 s->left = t;
6937 else
6938 s->right = t;
6940 else
6941 constructor_pending_elts = t;
6943 break;
6945 else
6947 /* p->balance == -1; growth of right side balances the node. */
6948 p->balance = 0;
6949 break;
6953 r = p;
6954 p = p->parent;
6958 /* Build AVL tree from a sorted chain. */
6960 static void
6961 set_nonincremental_init (void)
6963 unsigned HOST_WIDE_INT ix;
6964 tree index, value;
6966 if (TREE_CODE (constructor_type) != RECORD_TYPE
6967 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6968 return;
6970 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6971 add_pending_init (index, value, NULL_TREE, false);
6972 constructor_elements = 0;
6973 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6975 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6976 /* Skip any nameless bit fields at the beginning. */
6977 while (constructor_unfilled_fields != 0
6978 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6979 && DECL_NAME (constructor_unfilled_fields) == 0)
6980 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6983 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6985 if (TYPE_DOMAIN (constructor_type))
6986 constructor_unfilled_index
6987 = convert (bitsizetype,
6988 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6989 else
6990 constructor_unfilled_index = bitsize_zero_node;
6992 constructor_incremental = 0;
6995 /* Build AVL tree from a string constant. */
6997 static void
6998 set_nonincremental_init_from_string (tree str)
7000 tree value, purpose, type;
7001 HOST_WIDE_INT val[2];
7002 const char *p, *end;
7003 int byte, wchar_bytes, charwidth, bitpos;
7005 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7007 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7008 charwidth = TYPE_PRECISION (char_type_node);
7009 type = TREE_TYPE (constructor_type);
7010 p = TREE_STRING_POINTER (str);
7011 end = p + TREE_STRING_LENGTH (str);
7013 for (purpose = bitsize_zero_node;
7014 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7015 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7017 if (wchar_bytes == 1)
7019 val[1] = (unsigned char) *p++;
7020 val[0] = 0;
7022 else
7024 val[0] = 0;
7025 val[1] = 0;
7026 for (byte = 0; byte < wchar_bytes; byte++)
7028 if (BYTES_BIG_ENDIAN)
7029 bitpos = (wchar_bytes - byte - 1) * charwidth;
7030 else
7031 bitpos = byte * charwidth;
7032 val[bitpos < HOST_BITS_PER_WIDE_INT]
7033 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7034 << (bitpos % HOST_BITS_PER_WIDE_INT);
7038 if (!TYPE_UNSIGNED (type))
7040 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7041 if (bitpos < HOST_BITS_PER_WIDE_INT)
7043 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7045 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7046 val[0] = -1;
7049 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7051 if (val[1] < 0)
7052 val[0] = -1;
7054 else if (val[0] & (((HOST_WIDE_INT) 1)
7055 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7056 val[0] |= ((HOST_WIDE_INT) -1)
7057 << (bitpos - HOST_BITS_PER_WIDE_INT);
7060 value = build_int_cst_wide (type, val[1], val[0]);
7061 add_pending_init (purpose, value, NULL_TREE, false);
7064 constructor_incremental = 0;
7067 /* Return value of FIELD in pending initializer or zero if the field was
7068 not initialized yet. */
7070 static tree
7071 find_init_member (tree field)
7073 struct init_node *p;
7075 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7077 if (constructor_incremental
7078 && tree_int_cst_lt (field, constructor_unfilled_index))
7079 set_nonincremental_init ();
7081 p = constructor_pending_elts;
7082 while (p)
7084 if (tree_int_cst_lt (field, p->purpose))
7085 p = p->left;
7086 else if (tree_int_cst_lt (p->purpose, field))
7087 p = p->right;
7088 else
7089 return p->value;
7092 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7094 tree bitpos = bit_position (field);
7096 if (constructor_incremental
7097 && (!constructor_unfilled_fields
7098 || tree_int_cst_lt (bitpos,
7099 bit_position (constructor_unfilled_fields))))
7100 set_nonincremental_init ();
7102 p = constructor_pending_elts;
7103 while (p)
7105 if (field == p->purpose)
7106 return p->value;
7107 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7108 p = p->left;
7109 else
7110 p = p->right;
7113 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7115 if (!VEC_empty (constructor_elt, constructor_elements)
7116 && (VEC_last (constructor_elt, constructor_elements)->index
7117 == field))
7118 return VEC_last (constructor_elt, constructor_elements)->value;
7120 return 0;
7123 /* "Output" the next constructor element.
7124 At top level, really output it to assembler code now.
7125 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7126 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7127 TYPE is the data type that the containing data type wants here.
7128 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7129 If VALUE is a string constant, STRICT_STRING is true if it is
7130 unparenthesized or we should not warn here for it being parenthesized.
7131 For other types of VALUE, STRICT_STRING is not used.
7133 PENDING if non-nil means output pending elements that belong
7134 right after this element. (PENDING is normally 1;
7135 it is 0 while outputting pending elements, to avoid recursion.)
7137 IMPLICIT is true if value comes from pop_init_level (1),
7138 the new initializer has been merged with the existing one
7139 and thus no warnings should be emitted about overriding an
7140 existing initializer. */
7142 static void
7143 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7144 tree field, int pending, bool implicit)
7146 tree semantic_type = NULL_TREE;
7147 constructor_elt *celt;
7148 bool maybe_const = true;
7149 bool npc;
7151 if (type == error_mark_node || value == error_mark_node)
7153 constructor_erroneous = 1;
7154 return;
7156 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7157 && (TREE_CODE (value) == STRING_CST
7158 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7159 && !(TREE_CODE (value) == STRING_CST
7160 && TREE_CODE (type) == ARRAY_TYPE
7161 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7162 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7163 TYPE_MAIN_VARIANT (type)))
7164 value = array_to_pointer_conversion (input_location, value);
7166 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7167 && require_constant_value && !flag_isoc99 && pending)
7169 /* As an extension, allow initializing objects with static storage
7170 duration with compound literals (which are then treated just as
7171 the brace enclosed list they contain). */
7172 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7173 value = DECL_INITIAL (decl);
7176 npc = null_pointer_constant_p (value);
7177 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7179 semantic_type = TREE_TYPE (value);
7180 value = TREE_OPERAND (value, 0);
7182 value = c_fully_fold (value, require_constant_value, &maybe_const);
7184 if (value == error_mark_node)
7185 constructor_erroneous = 1;
7186 else if (!TREE_CONSTANT (value))
7187 constructor_constant = 0;
7188 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7189 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7190 || TREE_CODE (constructor_type) == UNION_TYPE)
7191 && DECL_C_BIT_FIELD (field)
7192 && TREE_CODE (value) != INTEGER_CST))
7193 constructor_simple = 0;
7194 if (!maybe_const)
7195 constructor_nonconst = 1;
7197 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7199 if (require_constant_value)
7201 error_init ("initializer element is not constant");
7202 value = error_mark_node;
7204 else if (require_constant_elements)
7205 pedwarn (input_location, 0,
7206 "initializer element is not computable at load time");
7208 else if (!maybe_const
7209 && (require_constant_value || require_constant_elements))
7210 pedwarn_init (input_location, 0,
7211 "initializer element is not a constant expression");
7213 /* Issue -Wc++-compat warnings about initializing a bitfield with
7214 enum type. */
7215 if (warn_cxx_compat
7216 && field != NULL_TREE
7217 && TREE_CODE (field) == FIELD_DECL
7218 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7219 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7220 != TYPE_MAIN_VARIANT (type))
7221 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7223 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7224 if (checktype != error_mark_node
7225 && (TYPE_MAIN_VARIANT (checktype)
7226 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7227 warning_init (OPT_Wc___compat,
7228 "enum conversion in initialization is invalid in C++");
7231 /* If this field is empty (and not at the end of structure),
7232 don't do anything other than checking the initializer. */
7233 if (field
7234 && (TREE_TYPE (field) == error_mark_node
7235 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7236 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7237 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7238 || TREE_CHAIN (field)))))
7239 return;
7241 if (semantic_type)
7242 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7243 value = digest_init (input_location, type, value, origtype, npc,
7244 strict_string, require_constant_value);
7245 if (value == error_mark_node)
7247 constructor_erroneous = 1;
7248 return;
7250 if (require_constant_value || require_constant_elements)
7251 constant_expression_warning (value);
7253 /* If this element doesn't come next in sequence,
7254 put it on constructor_pending_elts. */
7255 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7256 && (!constructor_incremental
7257 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7259 if (constructor_incremental
7260 && tree_int_cst_lt (field, constructor_unfilled_index))
7261 set_nonincremental_init ();
7263 add_pending_init (field, value, origtype, implicit);
7264 return;
7266 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7267 && (!constructor_incremental
7268 || field != constructor_unfilled_fields))
7270 /* We do this for records but not for unions. In a union,
7271 no matter which field is specified, it can be initialized
7272 right away since it starts at the beginning of the union. */
7273 if (constructor_incremental)
7275 if (!constructor_unfilled_fields)
7276 set_nonincremental_init ();
7277 else
7279 tree bitpos, unfillpos;
7281 bitpos = bit_position (field);
7282 unfillpos = bit_position (constructor_unfilled_fields);
7284 if (tree_int_cst_lt (bitpos, unfillpos))
7285 set_nonincremental_init ();
7289 add_pending_init (field, value, origtype, implicit);
7290 return;
7292 else if (TREE_CODE (constructor_type) == UNION_TYPE
7293 && !VEC_empty (constructor_elt, constructor_elements))
7295 if (!implicit)
7297 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7298 constructor_elements)->value))
7299 warning_init (0,
7300 "initialized field with side-effects overwritten");
7301 else if (warn_override_init)
7302 warning_init (OPT_Woverride_init, "initialized field overwritten");
7305 /* We can have just one union field set. */
7306 constructor_elements = 0;
7309 /* Otherwise, output this element either to
7310 constructor_elements or to the assembler file. */
7312 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7313 celt->index = field;
7314 celt->value = value;
7316 /* Advance the variable that indicates sequential elements output. */
7317 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7318 constructor_unfilled_index
7319 = size_binop (PLUS_EXPR, constructor_unfilled_index,
7320 bitsize_one_node);
7321 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7323 constructor_unfilled_fields
7324 = TREE_CHAIN (constructor_unfilled_fields);
7326 /* Skip any nameless bit fields. */
7327 while (constructor_unfilled_fields != 0
7328 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7329 && DECL_NAME (constructor_unfilled_fields) == 0)
7330 constructor_unfilled_fields =
7331 TREE_CHAIN (constructor_unfilled_fields);
7333 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7334 constructor_unfilled_fields = 0;
7336 /* Now output any pending elements which have become next. */
7337 if (pending)
7338 output_pending_init_elements (0);
7341 /* Output any pending elements which have become next.
7342 As we output elements, constructor_unfilled_{fields,index}
7343 advances, which may cause other elements to become next;
7344 if so, they too are output.
7346 If ALL is 0, we return when there are
7347 no more pending elements to output now.
7349 If ALL is 1, we output space as necessary so that
7350 we can output all the pending elements. */
7352 static void
7353 output_pending_init_elements (int all)
7355 struct init_node *elt = constructor_pending_elts;
7356 tree next;
7358 retry:
7360 /* Look through the whole pending tree.
7361 If we find an element that should be output now,
7362 output it. Otherwise, set NEXT to the element
7363 that comes first among those still pending. */
7365 next = 0;
7366 while (elt)
7368 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7370 if (tree_int_cst_equal (elt->purpose,
7371 constructor_unfilled_index))
7372 output_init_element (elt->value, elt->origtype, true,
7373 TREE_TYPE (constructor_type),
7374 constructor_unfilled_index, 0, false);
7375 else if (tree_int_cst_lt (constructor_unfilled_index,
7376 elt->purpose))
7378 /* Advance to the next smaller node. */
7379 if (elt->left)
7380 elt = elt->left;
7381 else
7383 /* We have reached the smallest node bigger than the
7384 current unfilled index. Fill the space first. */
7385 next = elt->purpose;
7386 break;
7389 else
7391 /* Advance to the next bigger node. */
7392 if (elt->right)
7393 elt = elt->right;
7394 else
7396 /* We have reached the biggest node in a subtree. Find
7397 the parent of it, which is the next bigger node. */
7398 while (elt->parent && elt->parent->right == elt)
7399 elt = elt->parent;
7400 elt = elt->parent;
7401 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7402 elt->purpose))
7404 next = elt->purpose;
7405 break;
7410 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7411 || TREE_CODE (constructor_type) == UNION_TYPE)
7413 tree ctor_unfilled_bitpos, elt_bitpos;
7415 /* If the current record is complete we are done. */
7416 if (constructor_unfilled_fields == 0)
7417 break;
7419 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7420 elt_bitpos = bit_position (elt->purpose);
7421 /* We can't compare fields here because there might be empty
7422 fields in between. */
7423 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7425 constructor_unfilled_fields = elt->purpose;
7426 output_init_element (elt->value, elt->origtype, true,
7427 TREE_TYPE (elt->purpose),
7428 elt->purpose, 0, false);
7430 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7432 /* Advance to the next smaller node. */
7433 if (elt->left)
7434 elt = elt->left;
7435 else
7437 /* We have reached the smallest node bigger than the
7438 current unfilled field. Fill the space first. */
7439 next = elt->purpose;
7440 break;
7443 else
7445 /* Advance to the next bigger node. */
7446 if (elt->right)
7447 elt = elt->right;
7448 else
7450 /* We have reached the biggest node in a subtree. Find
7451 the parent of it, which is the next bigger node. */
7452 while (elt->parent && elt->parent->right == elt)
7453 elt = elt->parent;
7454 elt = elt->parent;
7455 if (elt
7456 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7457 bit_position (elt->purpose))))
7459 next = elt->purpose;
7460 break;
7467 /* Ordinarily return, but not if we want to output all
7468 and there are elements left. */
7469 if (!(all && next != 0))
7470 return;
7472 /* If it's not incremental, just skip over the gap, so that after
7473 jumping to retry we will output the next successive element. */
7474 if (TREE_CODE (constructor_type) == RECORD_TYPE
7475 || TREE_CODE (constructor_type) == UNION_TYPE)
7476 constructor_unfilled_fields = next;
7477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7478 constructor_unfilled_index = next;
7480 /* ELT now points to the node in the pending tree with the next
7481 initializer to output. */
7482 goto retry;
7485 /* Add one non-braced element to the current constructor level.
7486 This adjusts the current position within the constructor's type.
7487 This may also start or terminate implicit levels
7488 to handle a partly-braced initializer.
7490 Once this has found the correct level for the new element,
7491 it calls output_init_element.
7493 IMPLICIT is true if value comes from pop_init_level (1),
7494 the new initializer has been merged with the existing one
7495 and thus no warnings should be emitted about overriding an
7496 existing initializer. */
7498 void
7499 process_init_element (struct c_expr value, bool implicit)
7501 tree orig_value = value.value;
7502 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7503 bool strict_string = value.original_code == STRING_CST;
7505 designator_depth = 0;
7506 designator_erroneous = 0;
7508 /* Handle superfluous braces around string cst as in
7509 char x[] = {"foo"}; */
7510 if (string_flag
7511 && constructor_type
7512 && TREE_CODE (constructor_type) == ARRAY_TYPE
7513 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7514 && integer_zerop (constructor_unfilled_index))
7516 if (constructor_stack->replacement_value.value)
7517 error_init ("excess elements in char array initializer");
7518 constructor_stack->replacement_value = value;
7519 return;
7522 if (constructor_stack->replacement_value.value != 0)
7524 error_init ("excess elements in struct initializer");
7525 return;
7528 /* Ignore elements of a brace group if it is entirely superfluous
7529 and has already been diagnosed. */
7530 if (constructor_type == 0)
7531 return;
7533 /* If we've exhausted any levels that didn't have braces,
7534 pop them now. */
7535 while (constructor_stack->implicit)
7537 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7538 || TREE_CODE (constructor_type) == UNION_TYPE)
7539 && constructor_fields == 0)
7540 process_init_element (pop_init_level (1), true);
7541 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7542 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7543 && (constructor_max_index == 0
7544 || tree_int_cst_lt (constructor_max_index,
7545 constructor_index)))
7546 process_init_element (pop_init_level (1), true);
7547 else
7548 break;
7551 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7552 if (constructor_range_stack)
7554 /* If value is a compound literal and we'll be just using its
7555 content, don't put it into a SAVE_EXPR. */
7556 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7557 || !require_constant_value
7558 || flag_isoc99)
7560 tree semantic_type = NULL_TREE;
7561 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7563 semantic_type = TREE_TYPE (value.value);
7564 value.value = TREE_OPERAND (value.value, 0);
7566 value.value = c_save_expr (value.value);
7567 if (semantic_type)
7568 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7569 value.value);
7573 while (1)
7575 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7577 tree fieldtype;
7578 enum tree_code fieldcode;
7580 if (constructor_fields == 0)
7582 pedwarn_init (input_location, 0,
7583 "excess elements in struct initializer");
7584 break;
7587 fieldtype = TREE_TYPE (constructor_fields);
7588 if (fieldtype != error_mark_node)
7589 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7590 fieldcode = TREE_CODE (fieldtype);
7592 /* Error for non-static initialization of a flexible array member. */
7593 if (fieldcode == ARRAY_TYPE
7594 && !require_constant_value
7595 && TYPE_SIZE (fieldtype) == NULL_TREE
7596 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7598 error_init ("non-static initialization of a flexible array member");
7599 break;
7602 /* Accept a string constant to initialize a subarray. */
7603 if (value.value != 0
7604 && fieldcode == ARRAY_TYPE
7605 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7606 && string_flag)
7607 value.value = orig_value;
7608 /* Otherwise, if we have come to a subaggregate,
7609 and we don't have an element of its type, push into it. */
7610 else if (value.value != 0
7611 && value.value != error_mark_node
7612 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7613 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7614 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7616 push_init_level (1);
7617 continue;
7620 if (value.value)
7622 push_member_name (constructor_fields);
7623 output_init_element (value.value, value.original_type,
7624 strict_string, fieldtype,
7625 constructor_fields, 1, implicit);
7626 RESTORE_SPELLING_DEPTH (constructor_depth);
7628 else
7629 /* Do the bookkeeping for an element that was
7630 directly output as a constructor. */
7632 /* For a record, keep track of end position of last field. */
7633 if (DECL_SIZE (constructor_fields))
7634 constructor_bit_index
7635 = size_binop (PLUS_EXPR,
7636 bit_position (constructor_fields),
7637 DECL_SIZE (constructor_fields));
7639 /* If the current field was the first one not yet written out,
7640 it isn't now, so update. */
7641 if (constructor_unfilled_fields == constructor_fields)
7643 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7644 /* Skip any nameless bit fields. */
7645 while (constructor_unfilled_fields != 0
7646 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7647 && DECL_NAME (constructor_unfilled_fields) == 0)
7648 constructor_unfilled_fields =
7649 TREE_CHAIN (constructor_unfilled_fields);
7653 constructor_fields = TREE_CHAIN (constructor_fields);
7654 /* Skip any nameless bit fields at the beginning. */
7655 while (constructor_fields != 0
7656 && DECL_C_BIT_FIELD (constructor_fields)
7657 && DECL_NAME (constructor_fields) == 0)
7658 constructor_fields = TREE_CHAIN (constructor_fields);
7660 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7662 tree fieldtype;
7663 enum tree_code fieldcode;
7665 if (constructor_fields == 0)
7667 pedwarn_init (input_location, 0,
7668 "excess elements in union initializer");
7669 break;
7672 fieldtype = TREE_TYPE (constructor_fields);
7673 if (fieldtype != error_mark_node)
7674 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7675 fieldcode = TREE_CODE (fieldtype);
7677 /* Warn that traditional C rejects initialization of unions.
7678 We skip the warning if the value is zero. This is done
7679 under the assumption that the zero initializer in user
7680 code appears conditioned on e.g. __STDC__ to avoid
7681 "missing initializer" warnings and relies on default
7682 initialization to zero in the traditional C case.
7683 We also skip the warning if the initializer is designated,
7684 again on the assumption that this must be conditional on
7685 __STDC__ anyway (and we've already complained about the
7686 member-designator already). */
7687 if (!in_system_header && !constructor_designated
7688 && !(value.value && (integer_zerop (value.value)
7689 || real_zerop (value.value))))
7690 warning (OPT_Wtraditional, "traditional C rejects initialization "
7691 "of unions");
7693 /* Accept a string constant to initialize a subarray. */
7694 if (value.value != 0
7695 && fieldcode == ARRAY_TYPE
7696 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7697 && string_flag)
7698 value.value = orig_value;
7699 /* Otherwise, if we have come to a subaggregate,
7700 and we don't have an element of its type, push into it. */
7701 else if (value.value != 0
7702 && value.value != error_mark_node
7703 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7704 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7705 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7707 push_init_level (1);
7708 continue;
7711 if (value.value)
7713 push_member_name (constructor_fields);
7714 output_init_element (value.value, value.original_type,
7715 strict_string, fieldtype,
7716 constructor_fields, 1, implicit);
7717 RESTORE_SPELLING_DEPTH (constructor_depth);
7719 else
7720 /* Do the bookkeeping for an element that was
7721 directly output as a constructor. */
7723 constructor_bit_index = DECL_SIZE (constructor_fields);
7724 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7727 constructor_fields = 0;
7729 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7731 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7732 enum tree_code eltcode = TREE_CODE (elttype);
7734 /* Accept a string constant to initialize a subarray. */
7735 if (value.value != 0
7736 && eltcode == ARRAY_TYPE
7737 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7738 && string_flag)
7739 value.value = orig_value;
7740 /* Otherwise, if we have come to a subaggregate,
7741 and we don't have an element of its type, push into it. */
7742 else if (value.value != 0
7743 && value.value != error_mark_node
7744 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7745 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7746 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7748 push_init_level (1);
7749 continue;
7752 if (constructor_max_index != 0
7753 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7754 || integer_all_onesp (constructor_max_index)))
7756 pedwarn_init (input_location, 0,
7757 "excess elements in array initializer");
7758 break;
7761 /* Now output the actual element. */
7762 if (value.value)
7764 push_array_bounds (tree_low_cst (constructor_index, 1));
7765 output_init_element (value.value, value.original_type,
7766 strict_string, elttype,
7767 constructor_index, 1, implicit);
7768 RESTORE_SPELLING_DEPTH (constructor_depth);
7771 constructor_index
7772 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7774 if (!value.value)
7775 /* If we are doing the bookkeeping for an element that was
7776 directly output as a constructor, we must update
7777 constructor_unfilled_index. */
7778 constructor_unfilled_index = constructor_index;
7780 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7782 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7784 /* Do a basic check of initializer size. Note that vectors
7785 always have a fixed size derived from their type. */
7786 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7788 pedwarn_init (input_location, 0,
7789 "excess elements in vector initializer");
7790 break;
7793 /* Now output the actual element. */
7794 if (value.value)
7796 if (TREE_CODE (value.value) == VECTOR_CST)
7797 elttype = TYPE_MAIN_VARIANT (constructor_type);
7798 output_init_element (value.value, value.original_type,
7799 strict_string, elttype,
7800 constructor_index, 1, implicit);
7803 constructor_index
7804 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7806 if (!value.value)
7807 /* If we are doing the bookkeeping for an element that was
7808 directly output as a constructor, we must update
7809 constructor_unfilled_index. */
7810 constructor_unfilled_index = constructor_index;
7813 /* Handle the sole element allowed in a braced initializer
7814 for a scalar variable. */
7815 else if (constructor_type != error_mark_node
7816 && constructor_fields == 0)
7818 pedwarn_init (input_location, 0,
7819 "excess elements in scalar initializer");
7820 break;
7822 else
7824 if (value.value)
7825 output_init_element (value.value, value.original_type,
7826 strict_string, constructor_type,
7827 NULL_TREE, 1, implicit);
7828 constructor_fields = 0;
7831 /* Handle range initializers either at this level or anywhere higher
7832 in the designator stack. */
7833 if (constructor_range_stack)
7835 struct constructor_range_stack *p, *range_stack;
7836 int finish = 0;
7838 range_stack = constructor_range_stack;
7839 constructor_range_stack = 0;
7840 while (constructor_stack != range_stack->stack)
7842 gcc_assert (constructor_stack->implicit);
7843 process_init_element (pop_init_level (1), true);
7845 for (p = range_stack;
7846 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7847 p = p->prev)
7849 gcc_assert (constructor_stack->implicit);
7850 process_init_element (pop_init_level (1), true);
7853 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7854 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7855 finish = 1;
7857 while (1)
7859 constructor_index = p->index;
7860 constructor_fields = p->fields;
7861 if (finish && p->range_end && p->index == p->range_start)
7863 finish = 0;
7864 p->prev = 0;
7866 p = p->next;
7867 if (!p)
7868 break;
7869 push_init_level (2);
7870 p->stack = constructor_stack;
7871 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7872 p->index = p->range_start;
7875 if (!finish)
7876 constructor_range_stack = range_stack;
7877 continue;
7880 break;
7883 constructor_range_stack = 0;
7886 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7887 (guaranteed to be 'volatile' or null) and ARGS (represented using
7888 an ASM_EXPR node). */
7889 tree
7890 build_asm_stmt (tree cv_qualifier, tree args)
7892 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7893 ASM_VOLATILE_P (args) = 1;
7894 return add_stmt (args);
7897 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7898 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7899 SIMPLE indicates whether there was anything at all after the
7900 string in the asm expression -- asm("blah") and asm("blah" : )
7901 are subtly different. We use a ASM_EXPR node to represent this. */
7902 tree
7903 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
7904 tree clobbers, bool simple)
7906 tree tail;
7907 tree args;
7908 int i;
7909 const char *constraint;
7910 const char **oconstraints;
7911 bool allows_mem, allows_reg, is_inout;
7912 int ninputs, noutputs;
7914 ninputs = list_length (inputs);
7915 noutputs = list_length (outputs);
7916 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7918 string = resolve_asm_operand_names (string, outputs, inputs);
7920 /* Remove output conversions that change the type but not the mode. */
7921 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7923 tree output = TREE_VALUE (tail);
7925 /* ??? Really, this should not be here. Users should be using a
7926 proper lvalue, dammit. But there's a long history of using casts
7927 in the output operands. In cases like longlong.h, this becomes a
7928 primitive form of typechecking -- if the cast can be removed, then
7929 the output operand had a type of the proper width; otherwise we'll
7930 get an error. Gross, but ... */
7931 STRIP_NOPS (output);
7933 if (!lvalue_or_else (output, lv_asm))
7934 output = error_mark_node;
7936 if (output != error_mark_node
7937 && (TREE_READONLY (output)
7938 || TYPE_READONLY (TREE_TYPE (output))
7939 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7940 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7941 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7942 readonly_error (output, lv_asm);
7944 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7945 oconstraints[i] = constraint;
7947 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7948 &allows_mem, &allows_reg, &is_inout))
7950 /* If the operand is going to end up in memory,
7951 mark it addressable. */
7952 if (!allows_reg && !c_mark_addressable (output))
7953 output = error_mark_node;
7955 else
7956 output = error_mark_node;
7958 TREE_VALUE (tail) = output;
7961 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7963 tree input;
7965 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7966 input = TREE_VALUE (tail);
7968 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7969 oconstraints, &allows_mem, &allows_reg))
7971 /* If the operand is going to end up in memory,
7972 mark it addressable. */
7973 if (!allows_reg && allows_mem)
7975 /* Strip the nops as we allow this case. FIXME, this really
7976 should be rejected or made deprecated. */
7977 STRIP_NOPS (input);
7978 if (!c_mark_addressable (input))
7979 input = error_mark_node;
7982 else
7983 input = error_mark_node;
7985 TREE_VALUE (tail) = input;
7988 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers);
7990 /* asm statements without outputs, including simple ones, are treated
7991 as volatile. */
7992 ASM_INPUT_P (args) = simple;
7993 ASM_VOLATILE_P (args) = (noutputs == 0);
7995 return args;
7998 /* Generate a goto statement to LABEL. LOC is the location of the
7999 GOTO. */
8001 tree
8002 c_finish_goto_label (location_t loc, tree label)
8004 tree decl = lookup_label_for_goto (loc, label);
8005 if (!decl)
8006 return NULL_TREE;
8007 TREE_USED (decl) = 1;
8009 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8010 SET_EXPR_LOCATION (t, loc);
8011 return add_stmt (t);
8015 /* Generate a computed goto statement to EXPR. LOC is the location of
8016 the GOTO. */
8018 tree
8019 c_finish_goto_ptr (location_t loc, tree expr)
8021 tree t;
8022 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8023 expr = c_fully_fold (expr, false, NULL);
8024 expr = convert (ptr_type_node, expr);
8025 t = build1 (GOTO_EXPR, void_type_node, expr);
8026 SET_EXPR_LOCATION (t, loc);
8027 return add_stmt (t);
8030 /* Generate a C `return' statement. RETVAL is the expression for what
8031 to return, or a null pointer for `return;' with no value. LOC is
8032 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8033 is the original type of RETVAL. */
8035 tree
8036 c_finish_return (location_t loc, tree retval, tree origtype)
8038 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8039 bool no_warning = false;
8040 bool npc = false;
8042 if (TREE_THIS_VOLATILE (current_function_decl))
8043 warning_at (loc, 0,
8044 "function declared %<noreturn%> has a %<return%> statement");
8046 if (retval)
8048 tree semantic_type = NULL_TREE;
8049 npc = null_pointer_constant_p (retval);
8050 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8052 semantic_type = TREE_TYPE (retval);
8053 retval = TREE_OPERAND (retval, 0);
8055 retval = c_fully_fold (retval, false, NULL);
8056 if (semantic_type)
8057 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8060 if (!retval)
8062 current_function_returns_null = 1;
8063 if ((warn_return_type || flag_isoc99)
8064 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8066 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8067 "%<return%> with no value, in "
8068 "function returning non-void");
8069 no_warning = true;
8072 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8074 current_function_returns_null = 1;
8075 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8076 pedwarn (loc, 0,
8077 "%<return%> with a value, in function returning void");
8078 else
8079 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8080 "%<return%> with expression, in function returning void");
8082 else
8084 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8085 ic_return,
8086 npc, NULL_TREE, NULL_TREE, 0);
8087 tree res = DECL_RESULT (current_function_decl);
8088 tree inner;
8090 current_function_returns_value = 1;
8091 if (t == error_mark_node)
8092 return NULL_TREE;
8094 inner = t = convert (TREE_TYPE (res), t);
8096 /* Strip any conversions, additions, and subtractions, and see if
8097 we are returning the address of a local variable. Warn if so. */
8098 while (1)
8100 switch (TREE_CODE (inner))
8102 CASE_CONVERT:
8103 case NON_LVALUE_EXPR:
8104 case PLUS_EXPR:
8105 case POINTER_PLUS_EXPR:
8106 inner = TREE_OPERAND (inner, 0);
8107 continue;
8109 case MINUS_EXPR:
8110 /* If the second operand of the MINUS_EXPR has a pointer
8111 type (or is converted from it), this may be valid, so
8112 don't give a warning. */
8114 tree op1 = TREE_OPERAND (inner, 1);
8116 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8117 && (CONVERT_EXPR_P (op1)
8118 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8119 op1 = TREE_OPERAND (op1, 0);
8121 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8122 break;
8124 inner = TREE_OPERAND (inner, 0);
8125 continue;
8128 case ADDR_EXPR:
8129 inner = TREE_OPERAND (inner, 0);
8131 while (REFERENCE_CLASS_P (inner)
8132 && TREE_CODE (inner) != INDIRECT_REF)
8133 inner = TREE_OPERAND (inner, 0);
8135 if (DECL_P (inner)
8136 && !DECL_EXTERNAL (inner)
8137 && !TREE_STATIC (inner)
8138 && DECL_CONTEXT (inner) == current_function_decl)
8139 warning_at (loc,
8140 0, "function returns address of local variable");
8141 break;
8143 default:
8144 break;
8147 break;
8150 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8151 SET_EXPR_LOCATION (retval, loc);
8153 if (warn_sequence_point)
8154 verify_sequence_points (retval);
8157 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8158 TREE_NO_WARNING (ret_stmt) |= no_warning;
8159 return add_stmt (ret_stmt);
8162 struct c_switch {
8163 /* The SWITCH_EXPR being built. */
8164 tree switch_expr;
8166 /* The original type of the testing expression, i.e. before the
8167 default conversion is applied. */
8168 tree orig_type;
8170 /* A splay-tree mapping the low element of a case range to the high
8171 element, or NULL_TREE if there is no high element. Used to
8172 determine whether or not a new case label duplicates an old case
8173 label. We need a tree, rather than simply a hash table, because
8174 of the GNU case range extension. */
8175 splay_tree cases;
8177 /* The bindings at the point of the switch. This is used for
8178 warnings crossing decls when branching to a case label. */
8179 struct c_spot_bindings *bindings;
8181 /* The next node on the stack. */
8182 struct c_switch *next;
8185 /* A stack of the currently active switch statements. The innermost
8186 switch statement is on the top of the stack. There is no need to
8187 mark the stack for garbage collection because it is only active
8188 during the processing of the body of a function, and we never
8189 collect at that point. */
8191 struct c_switch *c_switch_stack;
8193 /* Start a C switch statement, testing expression EXP. Return the new
8194 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8195 SWITCH_COND_LOC is the location of the switch's condition. */
8197 tree
8198 c_start_case (location_t switch_loc,
8199 location_t switch_cond_loc,
8200 tree exp)
8202 tree orig_type = error_mark_node;
8203 struct c_switch *cs;
8205 if (exp != error_mark_node)
8207 orig_type = TREE_TYPE (exp);
8209 if (!INTEGRAL_TYPE_P (orig_type))
8211 if (orig_type != error_mark_node)
8213 error_at (switch_cond_loc, "switch quantity not an integer");
8214 orig_type = error_mark_node;
8216 exp = integer_zero_node;
8218 else
8220 tree type = TYPE_MAIN_VARIANT (orig_type);
8222 if (!in_system_header
8223 && (type == long_integer_type_node
8224 || type == long_unsigned_type_node))
8225 warning_at (switch_cond_loc,
8226 OPT_Wtraditional, "%<long%> switch expression not "
8227 "converted to %<int%> in ISO C");
8229 exp = c_fully_fold (exp, false, NULL);
8230 exp = default_conversion (exp);
8232 if (warn_sequence_point)
8233 verify_sequence_points (exp);
8237 /* Add this new SWITCH_EXPR to the stack. */
8238 cs = XNEW (struct c_switch);
8239 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8240 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8241 cs->orig_type = orig_type;
8242 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8243 cs->bindings = c_get_switch_bindings ();
8244 cs->next = c_switch_stack;
8245 c_switch_stack = cs;
8247 return add_stmt (cs->switch_expr);
8250 /* Process a case label at location LOC. */
8252 tree
8253 do_case (location_t loc, tree low_value, tree high_value)
8255 tree label = NULL_TREE;
8257 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8259 low_value = c_fully_fold (low_value, false, NULL);
8260 if (TREE_CODE (low_value) == INTEGER_CST)
8261 pedwarn (input_location, OPT_pedantic,
8262 "case label is not an integer constant expression");
8265 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8267 high_value = c_fully_fold (high_value, false, NULL);
8268 if (TREE_CODE (high_value) == INTEGER_CST)
8269 pedwarn (input_location, OPT_pedantic,
8270 "case label is not an integer constant expression");
8273 if (c_switch_stack == NULL)
8275 if (low_value)
8276 error_at (loc, "case label not within a switch statement");
8277 else
8278 error_at (loc, "%<default%> label not within a switch statement");
8279 return NULL_TREE;
8282 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8283 EXPR_LOCATION (c_switch_stack->switch_expr),
8284 loc))
8285 return NULL_TREE;
8287 label = c_add_case_label (loc, c_switch_stack->cases,
8288 SWITCH_COND (c_switch_stack->switch_expr),
8289 c_switch_stack->orig_type,
8290 low_value, high_value);
8291 if (label == error_mark_node)
8292 label = NULL_TREE;
8293 return label;
8296 /* Finish the switch statement. */
8298 void
8299 c_finish_case (tree body)
8301 struct c_switch *cs = c_switch_stack;
8302 location_t switch_location;
8304 SWITCH_BODY (cs->switch_expr) = body;
8306 /* Emit warnings as needed. */
8307 switch_location = EXPR_LOCATION (cs->switch_expr);
8308 c_do_switch_warnings (cs->cases, switch_location,
8309 TREE_TYPE (cs->switch_expr),
8310 SWITCH_COND (cs->switch_expr));
8312 /* Pop the stack. */
8313 c_switch_stack = cs->next;
8314 splay_tree_delete (cs->cases);
8315 c_release_switch_bindings (cs->bindings);
8316 XDELETE (cs);
8319 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8320 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8321 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8322 statement, and was not surrounded with parenthesis. */
8324 void
8325 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8326 tree else_block, bool nested_if)
8328 tree stmt;
8330 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8331 if (warn_parentheses && nested_if && else_block == NULL)
8333 tree inner_if = then_block;
8335 /* We know from the grammar productions that there is an IF nested
8336 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8337 it might not be exactly THEN_BLOCK, but should be the last
8338 non-container statement within. */
8339 while (1)
8340 switch (TREE_CODE (inner_if))
8342 case COND_EXPR:
8343 goto found;
8344 case BIND_EXPR:
8345 inner_if = BIND_EXPR_BODY (inner_if);
8346 break;
8347 case STATEMENT_LIST:
8348 inner_if = expr_last (then_block);
8349 break;
8350 case TRY_FINALLY_EXPR:
8351 case TRY_CATCH_EXPR:
8352 inner_if = TREE_OPERAND (inner_if, 0);
8353 break;
8354 default:
8355 gcc_unreachable ();
8357 found:
8359 if (COND_EXPR_ELSE (inner_if))
8360 warning (OPT_Wparentheses,
8361 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8362 &if_locus);
8365 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8366 SET_EXPR_LOCATION (stmt, if_locus);
8367 add_stmt (stmt);
8370 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8371 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8372 is false for DO loops. INCR is the FOR increment expression. BODY is
8373 the statement controlled by the loop. BLAB is the break label. CLAB is
8374 the continue label. Everything is allowed to be NULL. */
8376 void
8377 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8378 tree blab, tree clab, bool cond_is_first)
8380 tree entry = NULL, exit = NULL, t;
8382 /* If the condition is zero don't generate a loop construct. */
8383 if (cond && integer_zerop (cond))
8385 if (cond_is_first)
8387 t = build_and_jump (&blab);
8388 SET_EXPR_LOCATION (t, start_locus);
8389 add_stmt (t);
8392 else
8394 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8396 /* If we have an exit condition, then we build an IF with gotos either
8397 out of the loop, or to the top of it. If there's no exit condition,
8398 then we just build a jump back to the top. */
8399 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8401 if (cond && !integer_nonzerop (cond))
8403 /* Canonicalize the loop condition to the end. This means
8404 generating a branch to the loop condition. Reuse the
8405 continue label, if possible. */
8406 if (cond_is_first)
8408 if (incr || !clab)
8410 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8411 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8413 else
8414 t = build1 (GOTO_EXPR, void_type_node, clab);
8415 SET_EXPR_LOCATION (t, start_locus);
8416 add_stmt (t);
8419 t = build_and_jump (&blab);
8420 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8421 if (cond_is_first)
8422 SET_EXPR_LOCATION (exit, start_locus);
8423 else
8424 SET_EXPR_LOCATION (exit, input_location);
8427 add_stmt (top);
8430 if (body)
8431 add_stmt (body);
8432 if (clab)
8433 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8434 if (incr)
8435 add_stmt (incr);
8436 if (entry)
8437 add_stmt (entry);
8438 if (exit)
8439 add_stmt (exit);
8440 if (blab)
8441 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8444 tree
8445 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8447 bool skip;
8448 tree label = *label_p;
8450 /* In switch statements break is sometimes stylistically used after
8451 a return statement. This can lead to spurious warnings about
8452 control reaching the end of a non-void function when it is
8453 inlined. Note that we are calling block_may_fallthru with
8454 language specific tree nodes; this works because
8455 block_may_fallthru returns true when given something it does not
8456 understand. */
8457 skip = !block_may_fallthru (cur_stmt_list);
8459 if (!label)
8461 if (!skip)
8462 *label_p = label = create_artificial_label (loc);
8464 else if (TREE_CODE (label) == LABEL_DECL)
8466 else switch (TREE_INT_CST_LOW (label))
8468 case 0:
8469 if (is_break)
8470 error_at (loc, "break statement not within loop or switch");
8471 else
8472 error_at (loc, "continue statement not within a loop");
8473 return NULL_TREE;
8475 case 1:
8476 gcc_assert (is_break);
8477 error_at (loc, "break statement used with OpenMP for loop");
8478 return NULL_TREE;
8480 default:
8481 gcc_unreachable ();
8484 if (skip)
8485 return NULL_TREE;
8487 if (!is_break)
8488 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8490 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8493 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8495 static void
8496 emit_side_effect_warnings (location_t loc, tree expr)
8498 if (expr == error_mark_node)
8500 else if (!TREE_SIDE_EFFECTS (expr))
8502 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8503 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8505 else
8506 warn_if_unused_value (expr, loc);
8509 /* Process an expression as if it were a complete statement. Emit
8510 diagnostics, but do not call ADD_STMT. LOC is the location of the
8511 statement. */
8513 tree
8514 c_process_expr_stmt (location_t loc, tree expr)
8516 if (!expr)
8517 return NULL_TREE;
8519 expr = c_fully_fold (expr, false, NULL);
8521 if (warn_sequence_point)
8522 verify_sequence_points (expr);
8524 if (TREE_TYPE (expr) != error_mark_node
8525 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8526 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8527 error_at (loc, "expression statement has incomplete type");
8529 /* If we're not processing a statement expression, warn about unused values.
8530 Warnings for statement expressions will be emitted later, once we figure
8531 out which is the result. */
8532 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8533 && warn_unused_value)
8534 emit_side_effect_warnings (loc, expr);
8536 /* If the expression is not of a type to which we cannot assign a line
8537 number, wrap the thing in a no-op NOP_EXPR. */
8538 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8540 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8541 SET_EXPR_LOCATION (expr, loc);
8544 return expr;
8547 /* Emit an expression as a statement. LOC is the location of the
8548 expression. */
8550 tree
8551 c_finish_expr_stmt (location_t loc, tree expr)
8553 if (expr)
8554 return add_stmt (c_process_expr_stmt (loc, expr));
8555 else
8556 return NULL;
8559 /* Do the opposite and emit a statement as an expression. To begin,
8560 create a new binding level and return it. */
8562 tree
8563 c_begin_stmt_expr (void)
8565 tree ret;
8567 /* We must force a BLOCK for this level so that, if it is not expanded
8568 later, there is a way to turn off the entire subtree of blocks that
8569 are contained in it. */
8570 keep_next_level ();
8571 ret = c_begin_compound_stmt (true);
8573 c_bindings_start_stmt_expr (c_switch_stack == NULL
8574 ? NULL
8575 : c_switch_stack->bindings);
8577 /* Mark the current statement list as belonging to a statement list. */
8578 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8580 return ret;
8583 /* LOC is the location of the compound statement to which this body
8584 belongs. */
8586 tree
8587 c_finish_stmt_expr (location_t loc, tree body)
8589 tree last, type, tmp, val;
8590 tree *last_p;
8592 body = c_end_compound_stmt (loc, body, true);
8594 c_bindings_end_stmt_expr (c_switch_stack == NULL
8595 ? NULL
8596 : c_switch_stack->bindings);
8598 /* Locate the last statement in BODY. See c_end_compound_stmt
8599 about always returning a BIND_EXPR. */
8600 last_p = &BIND_EXPR_BODY (body);
8601 last = BIND_EXPR_BODY (body);
8603 continue_searching:
8604 if (TREE_CODE (last) == STATEMENT_LIST)
8606 tree_stmt_iterator i;
8608 /* This can happen with degenerate cases like ({ }). No value. */
8609 if (!TREE_SIDE_EFFECTS (last))
8610 return body;
8612 /* If we're supposed to generate side effects warnings, process
8613 all of the statements except the last. */
8614 if (warn_unused_value)
8616 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8618 location_t tloc;
8619 tree t = tsi_stmt (i);
8621 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
8622 emit_side_effect_warnings (tloc, t);
8625 else
8626 i = tsi_last (last);
8627 last_p = tsi_stmt_ptr (i);
8628 last = *last_p;
8631 /* If the end of the list is exception related, then the list was split
8632 by a call to push_cleanup. Continue searching. */
8633 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8634 || TREE_CODE (last) == TRY_CATCH_EXPR)
8636 last_p = &TREE_OPERAND (last, 0);
8637 last = *last_p;
8638 goto continue_searching;
8641 /* In the case that the BIND_EXPR is not necessary, return the
8642 expression out from inside it. */
8643 if (last == error_mark_node
8644 || (last == BIND_EXPR_BODY (body)
8645 && BIND_EXPR_VARS (body) == NULL))
8647 /* Even if this looks constant, do not allow it in a constant
8648 expression. */
8649 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8650 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8651 /* Do not warn if the return value of a statement expression is
8652 unused. */
8653 TREE_NO_WARNING (last) = 1;
8654 return last;
8657 /* Extract the type of said expression. */
8658 type = TREE_TYPE (last);
8660 /* If we're not returning a value at all, then the BIND_EXPR that
8661 we already have is a fine expression to return. */
8662 if (!type || VOID_TYPE_P (type))
8663 return body;
8665 /* Now that we've located the expression containing the value, it seems
8666 silly to make voidify_wrapper_expr repeat the process. Create a
8667 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8668 tmp = create_tmp_var_raw (type, NULL);
8670 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8671 tree_expr_nonnegative_p giving up immediately. */
8672 val = last;
8673 if (TREE_CODE (val) == NOP_EXPR
8674 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8675 val = TREE_OPERAND (val, 0);
8677 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8678 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8681 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8682 SET_EXPR_LOCATION (t, loc);
8683 return t;
8687 /* Begin and end compound statements. This is as simple as pushing
8688 and popping new statement lists from the tree. */
8690 tree
8691 c_begin_compound_stmt (bool do_scope)
8693 tree stmt = push_stmt_list ();
8694 if (do_scope)
8695 push_scope ();
8696 return stmt;
8699 /* End a compound statement. STMT is the statement. LOC is the
8700 location of the compound statement-- this is usually the location
8701 of the opening brace. */
8703 tree
8704 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
8706 tree block = NULL;
8708 if (do_scope)
8710 if (c_dialect_objc ())
8711 objc_clear_super_receiver ();
8712 block = pop_scope ();
8715 stmt = pop_stmt_list (stmt);
8716 stmt = c_build_bind_expr (loc, block, stmt);
8718 /* If this compound statement is nested immediately inside a statement
8719 expression, then force a BIND_EXPR to be created. Otherwise we'll
8720 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8721 STATEMENT_LISTs merge, and thus we can lose track of what statement
8722 was really last. */
8723 if (cur_stmt_list
8724 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8725 && TREE_CODE (stmt) != BIND_EXPR)
8727 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8728 TREE_SIDE_EFFECTS (stmt) = 1;
8729 SET_EXPR_LOCATION (stmt, loc);
8732 return stmt;
8735 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8736 when the current scope is exited. EH_ONLY is true when this is not
8737 meant to apply to normal control flow transfer. */
8739 void
8740 push_cleanup (tree decl, tree cleanup, bool eh_only)
8742 enum tree_code code;
8743 tree stmt, list;
8744 bool stmt_expr;
8746 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8747 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
8748 add_stmt (stmt);
8749 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8750 list = push_stmt_list ();
8751 TREE_OPERAND (stmt, 0) = list;
8752 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8755 /* Build a binary-operation expression without default conversions.
8756 CODE is the kind of expression to build.
8757 LOCATION is the operator's location.
8758 This function differs from `build' in several ways:
8759 the data type of the result is computed and recorded in it,
8760 warnings are generated if arg data types are invalid,
8761 special handling for addition and subtraction of pointers is known,
8762 and some optimization is done (operations on narrow ints
8763 are done in the narrower type when that gives the same result).
8764 Constant folding is also done before the result is returned.
8766 Note that the operands will never have enumeral types, or function
8767 or array types, because either they will have the default conversions
8768 performed or they have both just been converted to some other type in which
8769 the arithmetic is to be done. */
8771 tree
8772 build_binary_op (location_t location, enum tree_code code,
8773 tree orig_op0, tree orig_op1, int convert_p)
8775 tree type0, type1, orig_type0, orig_type1;
8776 tree eptype;
8777 enum tree_code code0, code1;
8778 tree op0, op1;
8779 tree ret = error_mark_node;
8780 const char *invalid_op_diag;
8781 bool op0_int_operands, op1_int_operands;
8782 bool int_const, int_const_or_overflow, int_operands;
8784 /* Expression code to give to the expression when it is built.
8785 Normally this is CODE, which is what the caller asked for,
8786 but in some special cases we change it. */
8787 enum tree_code resultcode = code;
8789 /* Data type in which the computation is to be performed.
8790 In the simplest cases this is the common type of the arguments. */
8791 tree result_type = NULL;
8793 /* When the computation is in excess precision, the type of the
8794 final EXCESS_PRECISION_EXPR. */
8795 tree real_result_type = NULL;
8797 /* Nonzero means operands have already been type-converted
8798 in whatever way is necessary.
8799 Zero means they need to be converted to RESULT_TYPE. */
8800 int converted = 0;
8802 /* Nonzero means create the expression with this type, rather than
8803 RESULT_TYPE. */
8804 tree build_type = 0;
8806 /* Nonzero means after finally constructing the expression
8807 convert it to this type. */
8808 tree final_type = 0;
8810 /* Nonzero if this is an operation like MIN or MAX which can
8811 safely be computed in short if both args are promoted shorts.
8812 Also implies COMMON.
8813 -1 indicates a bitwise operation; this makes a difference
8814 in the exact conditions for when it is safe to do the operation
8815 in a narrower mode. */
8816 int shorten = 0;
8818 /* Nonzero if this is a comparison operation;
8819 if both args are promoted shorts, compare the original shorts.
8820 Also implies COMMON. */
8821 int short_compare = 0;
8823 /* Nonzero if this is a right-shift operation, which can be computed on the
8824 original short and then promoted if the operand is a promoted short. */
8825 int short_shift = 0;
8827 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8828 int common = 0;
8830 /* True means types are compatible as far as ObjC is concerned. */
8831 bool objc_ok;
8833 /* True means this is an arithmetic operation that may need excess
8834 precision. */
8835 bool may_need_excess_precision;
8837 if (location == UNKNOWN_LOCATION)
8838 location = input_location;
8840 op0 = orig_op0;
8841 op1 = orig_op1;
8843 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8844 if (op0_int_operands)
8845 op0 = remove_c_maybe_const_expr (op0);
8846 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8847 if (op1_int_operands)
8848 op1 = remove_c_maybe_const_expr (op1);
8849 int_operands = (op0_int_operands && op1_int_operands);
8850 if (int_operands)
8852 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8853 && TREE_CODE (orig_op1) == INTEGER_CST);
8854 int_const = (int_const_or_overflow
8855 && !TREE_OVERFLOW (orig_op0)
8856 && !TREE_OVERFLOW (orig_op1));
8858 else
8859 int_const = int_const_or_overflow = false;
8861 if (convert_p)
8863 op0 = default_conversion (op0);
8864 op1 = default_conversion (op1);
8867 orig_type0 = type0 = TREE_TYPE (op0);
8868 orig_type1 = type1 = TREE_TYPE (op1);
8870 /* The expression codes of the data types of the arguments tell us
8871 whether the arguments are integers, floating, pointers, etc. */
8872 code0 = TREE_CODE (type0);
8873 code1 = TREE_CODE (type1);
8875 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8876 STRIP_TYPE_NOPS (op0);
8877 STRIP_TYPE_NOPS (op1);
8879 /* If an error was already reported for one of the arguments,
8880 avoid reporting another error. */
8882 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8883 return error_mark_node;
8885 if ((invalid_op_diag
8886 = targetm.invalid_binary_op (code, type0, type1)))
8888 error_at (location, invalid_op_diag);
8889 return error_mark_node;
8892 switch (code)
8894 case PLUS_EXPR:
8895 case MINUS_EXPR:
8896 case MULT_EXPR:
8897 case TRUNC_DIV_EXPR:
8898 case CEIL_DIV_EXPR:
8899 case FLOOR_DIV_EXPR:
8900 case ROUND_DIV_EXPR:
8901 case EXACT_DIV_EXPR:
8902 may_need_excess_precision = true;
8903 break;
8904 default:
8905 may_need_excess_precision = false;
8906 break;
8908 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8910 op0 = TREE_OPERAND (op0, 0);
8911 type0 = TREE_TYPE (op0);
8913 else if (may_need_excess_precision
8914 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8916 type0 = eptype;
8917 op0 = convert (eptype, op0);
8919 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8921 op1 = TREE_OPERAND (op1, 0);
8922 type1 = TREE_TYPE (op1);
8924 else if (may_need_excess_precision
8925 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8927 type1 = eptype;
8928 op1 = convert (eptype, op1);
8931 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8933 switch (code)
8935 case PLUS_EXPR:
8936 /* Handle the pointer + int case. */
8937 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8939 ret = pointer_int_sum (PLUS_EXPR, op0, op1);
8940 goto return_build_binary_op;
8942 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8944 ret = pointer_int_sum (PLUS_EXPR, op1, op0);
8945 goto return_build_binary_op;
8947 else
8948 common = 1;
8949 break;
8951 case MINUS_EXPR:
8952 /* Subtraction of two similar pointers.
8953 We must subtract them as integers, then divide by object size. */
8954 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8955 && comp_target_types (location, type0, type1))
8957 ret = pointer_diff (op0, op1);
8958 goto return_build_binary_op;
8960 /* Handle pointer minus int. Just like pointer plus int. */
8961 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8963 ret = pointer_int_sum (MINUS_EXPR, op0, op1);
8964 goto return_build_binary_op;
8966 else
8967 common = 1;
8968 break;
8970 case MULT_EXPR:
8971 common = 1;
8972 break;
8974 case TRUNC_DIV_EXPR:
8975 case CEIL_DIV_EXPR:
8976 case FLOOR_DIV_EXPR:
8977 case ROUND_DIV_EXPR:
8978 case EXACT_DIV_EXPR:
8979 warn_for_div_by_zero (location, op1);
8981 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8982 || code0 == FIXED_POINT_TYPE
8983 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8984 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8985 || code1 == FIXED_POINT_TYPE
8986 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8988 enum tree_code tcode0 = code0, tcode1 = code1;
8990 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8991 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8992 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8993 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8995 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8996 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8997 resultcode = RDIV_EXPR;
8998 else
8999 /* Although it would be tempting to shorten always here, that
9000 loses on some targets, since the modulo instruction is
9001 undefined if the quotient can't be represented in the
9002 computation mode. We shorten only if unsigned or if
9003 dividing by something we know != -1. */
9004 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9005 || (TREE_CODE (op1) == INTEGER_CST
9006 && !integer_all_onesp (op1)));
9007 common = 1;
9009 break;
9011 case BIT_AND_EXPR:
9012 case BIT_IOR_EXPR:
9013 case BIT_XOR_EXPR:
9014 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9015 shorten = -1;
9016 /* Allow vector types which are not floating point types. */
9017 else if (code0 == VECTOR_TYPE
9018 && code1 == VECTOR_TYPE
9019 && !VECTOR_FLOAT_TYPE_P (type0)
9020 && !VECTOR_FLOAT_TYPE_P (type1))
9021 common = 1;
9022 break;
9024 case TRUNC_MOD_EXPR:
9025 case FLOOR_MOD_EXPR:
9026 warn_for_div_by_zero (location, op1);
9028 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9029 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9030 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9031 common = 1;
9032 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9034 /* Although it would be tempting to shorten always here, that loses
9035 on some targets, since the modulo instruction is undefined if the
9036 quotient can't be represented in the computation mode. We shorten
9037 only if unsigned or if dividing by something we know != -1. */
9038 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9039 || (TREE_CODE (op1) == INTEGER_CST
9040 && !integer_all_onesp (op1)));
9041 common = 1;
9043 break;
9045 case TRUTH_ANDIF_EXPR:
9046 case TRUTH_ORIF_EXPR:
9047 case TRUTH_AND_EXPR:
9048 case TRUTH_OR_EXPR:
9049 case TRUTH_XOR_EXPR:
9050 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9051 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9052 || code0 == FIXED_POINT_TYPE)
9053 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9054 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9055 || code1 == FIXED_POINT_TYPE))
9057 /* Result of these operations is always an int,
9058 but that does not mean the operands should be
9059 converted to ints! */
9060 result_type = integer_type_node;
9061 op0 = c_common_truthvalue_conversion (location, op0);
9062 op1 = c_common_truthvalue_conversion (location, op1);
9063 converted = 1;
9065 if (code == TRUTH_ANDIF_EXPR)
9067 int_const_or_overflow = (int_operands
9068 && TREE_CODE (orig_op0) == INTEGER_CST
9069 && (op0 == truthvalue_false_node
9070 || TREE_CODE (orig_op1) == INTEGER_CST));
9071 int_const = (int_const_or_overflow
9072 && !TREE_OVERFLOW (orig_op0)
9073 && (op0 == truthvalue_false_node
9074 || !TREE_OVERFLOW (orig_op1)));
9076 else if (code == TRUTH_ORIF_EXPR)
9078 int_const_or_overflow = (int_operands
9079 && TREE_CODE (orig_op0) == INTEGER_CST
9080 && (op0 == truthvalue_true_node
9081 || TREE_CODE (orig_op1) == INTEGER_CST));
9082 int_const = (int_const_or_overflow
9083 && !TREE_OVERFLOW (orig_op0)
9084 && (op0 == truthvalue_true_node
9085 || !TREE_OVERFLOW (orig_op1)));
9087 break;
9089 /* Shift operations: result has same type as first operand;
9090 always convert second operand to int.
9091 Also set SHORT_SHIFT if shifting rightward. */
9093 case RSHIFT_EXPR:
9094 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9095 && code1 == INTEGER_TYPE)
9097 if (TREE_CODE (op1) == INTEGER_CST)
9099 if (tree_int_cst_sgn (op1) < 0)
9101 int_const = false;
9102 if (c_inhibit_evaluation_warnings == 0)
9103 warning (0, "right shift count is negative");
9105 else
9107 if (!integer_zerop (op1))
9108 short_shift = 1;
9110 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9112 int_const = false;
9113 if (c_inhibit_evaluation_warnings == 0)
9114 warning (0, "right shift count >= width of type");
9119 /* Use the type of the value to be shifted. */
9120 result_type = type0;
9121 /* Convert the shift-count to an integer, regardless of size
9122 of value being shifted. */
9123 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9124 op1 = convert (integer_type_node, op1);
9125 /* Avoid converting op1 to result_type later. */
9126 converted = 1;
9128 break;
9130 case LSHIFT_EXPR:
9131 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9132 && code1 == INTEGER_TYPE)
9134 if (TREE_CODE (op1) == INTEGER_CST)
9136 if (tree_int_cst_sgn (op1) < 0)
9138 int_const = false;
9139 if (c_inhibit_evaluation_warnings == 0)
9140 warning (0, "left shift count is negative");
9143 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9145 int_const = false;
9146 if (c_inhibit_evaluation_warnings == 0)
9147 warning (0, "left shift count >= width of type");
9151 /* Use the type of the value to be shifted. */
9152 result_type = type0;
9153 /* Convert the shift-count to an integer, regardless of size
9154 of value being shifted. */
9155 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9156 op1 = convert (integer_type_node, op1);
9157 /* Avoid converting op1 to result_type later. */
9158 converted = 1;
9160 break;
9162 case EQ_EXPR:
9163 case NE_EXPR:
9164 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9165 warning_at (location,
9166 OPT_Wfloat_equal,
9167 "comparing floating point with == or != is unsafe");
9168 /* Result of comparison is always int,
9169 but don't convert the args to int! */
9170 build_type = integer_type_node;
9171 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9172 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9173 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9174 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9175 short_compare = 1;
9176 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9178 tree tt0 = TREE_TYPE (type0);
9179 tree tt1 = TREE_TYPE (type1);
9180 /* Anything compares with void *. void * compares with anything.
9181 Otherwise, the targets must be compatible
9182 and both must be object or both incomplete. */
9183 if (comp_target_types (location, type0, type1))
9184 result_type = common_pointer_type (type0, type1);
9185 else if (VOID_TYPE_P (tt0))
9187 /* op0 != orig_op0 detects the case of something
9188 whose value is 0 but which isn't a valid null ptr const. */
9189 if (pedantic && !null_pointer_constant_p (orig_op0)
9190 && TREE_CODE (tt1) == FUNCTION_TYPE)
9191 pedwarn (location, OPT_pedantic, "ISO C forbids "
9192 "comparison of %<void *%> with function pointer");
9194 else if (VOID_TYPE_P (tt1))
9196 if (pedantic && !null_pointer_constant_p (orig_op1)
9197 && TREE_CODE (tt0) == FUNCTION_TYPE)
9198 pedwarn (location, OPT_pedantic, "ISO C forbids "
9199 "comparison of %<void *%> with function pointer");
9201 else
9202 /* Avoid warning about the volatile ObjC EH puts on decls. */
9203 if (!objc_ok)
9204 pedwarn (location, 0,
9205 "comparison of distinct pointer types lacks a cast");
9207 if (result_type == NULL_TREE)
9208 result_type = ptr_type_node;
9210 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9212 if (TREE_CODE (op0) == ADDR_EXPR
9213 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9214 warning_at (location,
9215 OPT_Waddress, "the address of %qD will never be NULL",
9216 TREE_OPERAND (op0, 0));
9217 result_type = type0;
9219 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9221 if (TREE_CODE (op1) == ADDR_EXPR
9222 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9223 warning_at (location,
9224 OPT_Waddress, "the address of %qD will never be NULL",
9225 TREE_OPERAND (op1, 0));
9226 result_type = type1;
9228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9230 result_type = type0;
9231 pedwarn (location, 0, "comparison between pointer and integer");
9233 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9235 result_type = type1;
9236 pedwarn (location, 0, "comparison between pointer and integer");
9238 break;
9240 case LE_EXPR:
9241 case GE_EXPR:
9242 case LT_EXPR:
9243 case GT_EXPR:
9244 build_type = integer_type_node;
9245 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9246 || code0 == FIXED_POINT_TYPE)
9247 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9248 || code1 == FIXED_POINT_TYPE))
9249 short_compare = 1;
9250 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9252 if (comp_target_types (location, type0, type1))
9254 result_type = common_pointer_type (type0, type1);
9255 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9256 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9257 pedwarn (location, 0,
9258 "comparison of complete and incomplete pointers");
9259 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9260 pedwarn (location, OPT_pedantic, "ISO C forbids "
9261 "ordered comparisons of pointers to functions");
9263 else
9265 result_type = ptr_type_node;
9266 pedwarn (location, 0,
9267 "comparison of distinct pointer types lacks a cast");
9270 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9272 result_type = type0;
9273 if (pedantic)
9274 pedwarn (location, OPT_pedantic,
9275 "ordered comparison of pointer with integer zero");
9276 else if (extra_warnings)
9277 warning_at (location, OPT_Wextra,
9278 "ordered comparison of pointer with integer zero");
9280 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9282 result_type = type1;
9283 pedwarn (location, OPT_pedantic,
9284 "ordered comparison of pointer with integer zero");
9286 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9288 result_type = type0;
9289 pedwarn (location, 0, "comparison between pointer and integer");
9291 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9293 result_type = type1;
9294 pedwarn (location, 0, "comparison between pointer and integer");
9296 break;
9298 default:
9299 gcc_unreachable ();
9302 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9303 return error_mark_node;
9305 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9306 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9307 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9308 TREE_TYPE (type1))))
9310 binary_op_error (location, code, type0, type1);
9311 return error_mark_node;
9314 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9315 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9317 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9318 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9320 bool first_complex = (code0 == COMPLEX_TYPE);
9321 bool second_complex = (code1 == COMPLEX_TYPE);
9322 int none_complex = (!first_complex && !second_complex);
9324 if (shorten || common || short_compare)
9326 result_type = c_common_type (type0, type1);
9327 if (result_type == error_mark_node)
9328 return error_mark_node;
9331 if (first_complex != second_complex
9332 && (code == PLUS_EXPR
9333 || code == MINUS_EXPR
9334 || code == MULT_EXPR
9335 || (code == TRUNC_DIV_EXPR && first_complex))
9336 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9337 && flag_signed_zeros)
9339 /* An operation on mixed real/complex operands must be
9340 handled specially, but the language-independent code can
9341 more easily optimize the plain complex arithmetic if
9342 -fno-signed-zeros. */
9343 tree real_type = TREE_TYPE (result_type);
9344 tree real, imag;
9345 if (type0 != orig_type0 || type1 != orig_type1)
9347 gcc_assert (may_need_excess_precision && common);
9348 real_result_type = c_common_type (orig_type0, orig_type1);
9350 if (first_complex)
9352 if (TREE_TYPE (op0) != result_type)
9353 op0 = convert_and_check (result_type, op0);
9354 if (TREE_TYPE (op1) != real_type)
9355 op1 = convert_and_check (real_type, op1);
9357 else
9359 if (TREE_TYPE (op0) != real_type)
9360 op0 = convert_and_check (real_type, op0);
9361 if (TREE_TYPE (op1) != result_type)
9362 op1 = convert_and_check (result_type, op1);
9364 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9365 return error_mark_node;
9366 if (first_complex)
9368 op0 = c_save_expr (op0);
9369 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9370 op0, 1);
9371 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9372 op0, 1);
9373 switch (code)
9375 case MULT_EXPR:
9376 case TRUNC_DIV_EXPR:
9377 imag = build2 (resultcode, real_type, imag, op1);
9378 /* Fall through. */
9379 case PLUS_EXPR:
9380 case MINUS_EXPR:
9381 real = build2 (resultcode, real_type, real, op1);
9382 break;
9383 default:
9384 gcc_unreachable();
9387 else
9389 op1 = c_save_expr (op1);
9390 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9391 op1, 1);
9392 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9393 op1, 1);
9394 switch (code)
9396 case MULT_EXPR:
9397 imag = build2 (resultcode, real_type, op0, imag);
9398 /* Fall through. */
9399 case PLUS_EXPR:
9400 real = build2 (resultcode, real_type, op0, real);
9401 break;
9402 case MINUS_EXPR:
9403 real = build2 (resultcode, real_type, op0, real);
9404 imag = build1 (NEGATE_EXPR, real_type, imag);
9405 break;
9406 default:
9407 gcc_unreachable();
9410 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9411 goto return_build_binary_op;
9414 /* For certain operations (which identify themselves by shorten != 0)
9415 if both args were extended from the same smaller type,
9416 do the arithmetic in that type and then extend.
9418 shorten !=0 and !=1 indicates a bitwise operation.
9419 For them, this optimization is safe only if
9420 both args are zero-extended or both are sign-extended.
9421 Otherwise, we might change the result.
9422 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9423 but calculated in (unsigned short) it would be (unsigned short)-1. */
9425 if (shorten && none_complex)
9427 final_type = result_type;
9428 result_type = shorten_binary_op (result_type, op0, op1,
9429 shorten == -1);
9432 /* Shifts can be shortened if shifting right. */
9434 if (short_shift)
9436 int unsigned_arg;
9437 tree arg0 = get_narrower (op0, &unsigned_arg);
9439 final_type = result_type;
9441 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9442 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9444 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9445 /* We can shorten only if the shift count is less than the
9446 number of bits in the smaller type size. */
9447 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9448 /* We cannot drop an unsigned shift after sign-extension. */
9449 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9451 /* Do an unsigned shift if the operand was zero-extended. */
9452 result_type
9453 = c_common_signed_or_unsigned_type (unsigned_arg,
9454 TREE_TYPE (arg0));
9455 /* Convert value-to-be-shifted to that type. */
9456 if (TREE_TYPE (op0) != result_type)
9457 op0 = convert (result_type, op0);
9458 converted = 1;
9462 /* Comparison operations are shortened too but differently.
9463 They identify themselves by setting short_compare = 1. */
9465 if (short_compare)
9467 /* Don't write &op0, etc., because that would prevent op0
9468 from being kept in a register.
9469 Instead, make copies of the our local variables and
9470 pass the copies by reference, then copy them back afterward. */
9471 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9472 enum tree_code xresultcode = resultcode;
9473 tree val
9474 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9476 if (val != 0)
9478 ret = val;
9479 goto return_build_binary_op;
9482 op0 = xop0, op1 = xop1;
9483 converted = 1;
9484 resultcode = xresultcode;
9486 if (c_inhibit_evaluation_warnings == 0)
9488 bool op0_maybe_const = true;
9489 bool op1_maybe_const = true;
9490 tree orig_op0_folded, orig_op1_folded;
9492 if (in_late_binary_op)
9494 orig_op0_folded = orig_op0;
9495 orig_op1_folded = orig_op1;
9497 else
9499 /* Fold for the sake of possible warnings, as in
9500 build_conditional_expr. This requires the
9501 "original" values to be folded, not just op0 and
9502 op1. */
9503 op0 = c_fully_fold (op0, require_constant_value,
9504 &op0_maybe_const);
9505 op1 = c_fully_fold (op1, require_constant_value,
9506 &op1_maybe_const);
9507 orig_op0_folded = c_fully_fold (orig_op0,
9508 require_constant_value,
9509 NULL);
9510 orig_op1_folded = c_fully_fold (orig_op1,
9511 require_constant_value,
9512 NULL);
9515 if (warn_sign_compare)
9516 warn_for_sign_compare (location, orig_op0_folded,
9517 orig_op1_folded, op0, op1,
9518 result_type, resultcode);
9519 if (!in_late_binary_op)
9521 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9523 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9524 NULL, op0);
9525 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9527 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9529 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9530 NULL, op1);
9531 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9538 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9539 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9540 Then the expression will be built.
9541 It will be given type FINAL_TYPE if that is nonzero;
9542 otherwise, it will be given type RESULT_TYPE. */
9544 if (!result_type)
9546 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9547 return error_mark_node;
9550 if (!converted)
9552 if (TREE_TYPE (op0) != result_type)
9553 op0 = convert_and_check (result_type, op0);
9554 if (TREE_TYPE (op1) != result_type)
9555 op1 = convert_and_check (result_type, op1);
9557 /* This can happen if one operand has a vector type, and the other
9558 has a different type. */
9559 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9560 return error_mark_node;
9563 if (build_type == NULL_TREE)
9565 build_type = result_type;
9566 if (type0 != orig_type0 || type1 != orig_type1)
9568 gcc_assert (may_need_excess_precision && common);
9569 real_result_type = c_common_type (orig_type0, orig_type1);
9573 /* Treat expressions in initializers specially as they can't trap. */
9574 if (int_const_or_overflow)
9575 ret = (require_constant_value
9576 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9577 : fold_build2 (resultcode, build_type, op0, op1));
9578 else
9579 ret = build2 (resultcode, build_type, op0, op1);
9580 if (final_type != 0)
9581 ret = convert (final_type, ret);
9583 return_build_binary_op:
9584 gcc_assert (ret != error_mark_node);
9585 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9586 ret = (int_operands
9587 ? note_integer_operands (ret)
9588 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9589 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9590 && !in_late_binary_op)
9591 ret = note_integer_operands (ret);
9592 if (real_result_type)
9593 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9594 protected_set_expr_location (ret, location);
9595 return ret;
9599 /* Convert EXPR to be a truth-value, validating its type for this
9600 purpose. LOCATION is the source location for the expression. */
9602 tree
9603 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9605 bool int_const, int_operands;
9607 switch (TREE_CODE (TREE_TYPE (expr)))
9609 case ARRAY_TYPE:
9610 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9611 return error_mark_node;
9613 case RECORD_TYPE:
9614 error_at (location, "used struct type value where scalar is required");
9615 return error_mark_node;
9617 case UNION_TYPE:
9618 error_at (location, "used union type value where scalar is required");
9619 return error_mark_node;
9621 case FUNCTION_TYPE:
9622 gcc_unreachable ();
9624 default:
9625 break;
9628 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9629 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9630 if (int_operands)
9631 expr = remove_c_maybe_const_expr (expr);
9633 /* ??? Should we also give an error for void and vectors rather than
9634 leaving those to give errors later? */
9635 expr = c_common_truthvalue_conversion (location, expr);
9637 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9639 if (TREE_OVERFLOW (expr))
9640 return expr;
9641 else
9642 return note_integer_operands (expr);
9644 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9645 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9646 return expr;
9650 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9651 required. */
9653 tree
9654 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9656 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9658 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9659 /* Executing a compound literal inside a function reinitializes
9660 it. */
9661 if (!TREE_STATIC (decl))
9662 *se = true;
9663 return decl;
9665 else
9666 return expr;
9669 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9671 tree
9672 c_begin_omp_parallel (void)
9674 tree block;
9676 keep_next_level ();
9677 block = c_begin_compound_stmt (true);
9679 return block;
9682 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9683 statement. LOC is the location of the OMP_PARALLEL. */
9685 tree
9686 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
9688 tree stmt;
9690 block = c_end_compound_stmt (loc, block, true);
9692 stmt = make_node (OMP_PARALLEL);
9693 TREE_TYPE (stmt) = void_type_node;
9694 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9695 OMP_PARALLEL_BODY (stmt) = block;
9696 SET_EXPR_LOCATION (stmt, loc);
9698 return add_stmt (stmt);
9701 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9703 tree
9704 c_begin_omp_task (void)
9706 tree block;
9708 keep_next_level ();
9709 block = c_begin_compound_stmt (true);
9711 return block;
9714 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9715 statement. LOC is the location of the #pragma. */
9717 tree
9718 c_finish_omp_task (location_t loc, tree clauses, tree block)
9720 tree stmt;
9722 block = c_end_compound_stmt (loc, block, true);
9724 stmt = make_node (OMP_TASK);
9725 TREE_TYPE (stmt) = void_type_node;
9726 OMP_TASK_CLAUSES (stmt) = clauses;
9727 OMP_TASK_BODY (stmt) = block;
9728 SET_EXPR_LOCATION (stmt, loc);
9730 return add_stmt (stmt);
9733 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9734 Remove any elements from the list that are invalid. */
9736 tree
9737 c_finish_omp_clauses (tree clauses)
9739 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9740 tree c, t, *pc = &clauses;
9741 const char *name;
9743 bitmap_obstack_initialize (NULL);
9744 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9745 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9746 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9748 for (pc = &clauses, c = clauses; c ; c = *pc)
9750 bool remove = false;
9751 bool need_complete = false;
9752 bool need_implicitly_determined = false;
9754 switch (OMP_CLAUSE_CODE (c))
9756 case OMP_CLAUSE_SHARED:
9757 name = "shared";
9758 need_implicitly_determined = true;
9759 goto check_dup_generic;
9761 case OMP_CLAUSE_PRIVATE:
9762 name = "private";
9763 need_complete = true;
9764 need_implicitly_determined = true;
9765 goto check_dup_generic;
9767 case OMP_CLAUSE_REDUCTION:
9768 name = "reduction";
9769 need_implicitly_determined = true;
9770 t = OMP_CLAUSE_DECL (c);
9771 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9772 || POINTER_TYPE_P (TREE_TYPE (t)))
9774 error_at (OMP_CLAUSE_LOCATION (c),
9775 "%qE has invalid type for %<reduction%>", t);
9776 remove = true;
9778 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9780 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9781 const char *r_name = NULL;
9783 switch (r_code)
9785 case PLUS_EXPR:
9786 case MULT_EXPR:
9787 case MINUS_EXPR:
9788 break;
9789 case BIT_AND_EXPR:
9790 r_name = "&";
9791 break;
9792 case BIT_XOR_EXPR:
9793 r_name = "^";
9794 break;
9795 case BIT_IOR_EXPR:
9796 r_name = "|";
9797 break;
9798 case TRUTH_ANDIF_EXPR:
9799 r_name = "&&";
9800 break;
9801 case TRUTH_ORIF_EXPR:
9802 r_name = "||";
9803 break;
9804 default:
9805 gcc_unreachable ();
9807 if (r_name)
9809 error_at (OMP_CLAUSE_LOCATION (c),
9810 "%qE has invalid type for %<reduction(%s)%>",
9811 t, r_name);
9812 remove = true;
9815 goto check_dup_generic;
9817 case OMP_CLAUSE_COPYPRIVATE:
9818 name = "copyprivate";
9819 goto check_dup_generic;
9821 case OMP_CLAUSE_COPYIN:
9822 name = "copyin";
9823 t = OMP_CLAUSE_DECL (c);
9824 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9826 error_at (OMP_CLAUSE_LOCATION (c),
9827 "%qE must be %<threadprivate%> for %<copyin%>", t);
9828 remove = true;
9830 goto check_dup_generic;
9832 check_dup_generic:
9833 t = OMP_CLAUSE_DECL (c);
9834 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9836 error_at (OMP_CLAUSE_LOCATION (c),
9837 "%qE is not a variable in clause %qs", t, name);
9838 remove = true;
9840 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9841 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9842 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9844 error_at (OMP_CLAUSE_LOCATION (c),
9845 "%qE appears more than once in data clauses", t);
9846 remove = true;
9848 else
9849 bitmap_set_bit (&generic_head, DECL_UID (t));
9850 break;
9852 case OMP_CLAUSE_FIRSTPRIVATE:
9853 name = "firstprivate";
9854 t = OMP_CLAUSE_DECL (c);
9855 need_complete = true;
9856 need_implicitly_determined = true;
9857 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9859 error_at (OMP_CLAUSE_LOCATION (c),
9860 "%qE is not a variable in clause %<firstprivate%>", t);
9861 remove = true;
9863 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9864 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9866 error_at (OMP_CLAUSE_LOCATION (c),
9867 "%qE appears more than once in data clauses", t);
9868 remove = true;
9870 else
9871 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9872 break;
9874 case OMP_CLAUSE_LASTPRIVATE:
9875 name = "lastprivate";
9876 t = OMP_CLAUSE_DECL (c);
9877 need_complete = true;
9878 need_implicitly_determined = true;
9879 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9881 error_at (OMP_CLAUSE_LOCATION (c),
9882 "%qE is not a variable in clause %<lastprivate%>", t);
9883 remove = true;
9885 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9886 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9888 error_at (OMP_CLAUSE_LOCATION (c),
9889 "%qE appears more than once in data clauses", t);
9890 remove = true;
9892 else
9893 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9894 break;
9896 case OMP_CLAUSE_IF:
9897 case OMP_CLAUSE_NUM_THREADS:
9898 case OMP_CLAUSE_SCHEDULE:
9899 case OMP_CLAUSE_NOWAIT:
9900 case OMP_CLAUSE_ORDERED:
9901 case OMP_CLAUSE_DEFAULT:
9902 case OMP_CLAUSE_UNTIED:
9903 case OMP_CLAUSE_COLLAPSE:
9904 pc = &OMP_CLAUSE_CHAIN (c);
9905 continue;
9907 default:
9908 gcc_unreachable ();
9911 if (!remove)
9913 t = OMP_CLAUSE_DECL (c);
9915 if (need_complete)
9917 t = require_complete_type (t);
9918 if (t == error_mark_node)
9919 remove = true;
9922 if (need_implicitly_determined)
9924 const char *share_name = NULL;
9926 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9927 share_name = "threadprivate";
9928 else switch (c_omp_predetermined_sharing (t))
9930 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9931 break;
9932 case OMP_CLAUSE_DEFAULT_SHARED:
9933 share_name = "shared";
9934 break;
9935 case OMP_CLAUSE_DEFAULT_PRIVATE:
9936 share_name = "private";
9937 break;
9938 default:
9939 gcc_unreachable ();
9941 if (share_name)
9943 error_at (OMP_CLAUSE_LOCATION (c),
9944 "%qE is predetermined %qs for %qs",
9945 t, share_name, name);
9946 remove = true;
9951 if (remove)
9952 *pc = OMP_CLAUSE_CHAIN (c);
9953 else
9954 pc = &OMP_CLAUSE_CHAIN (c);
9957 bitmap_obstack_release (NULL);
9958 return clauses;
9961 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9962 down to the element type of an array. */
9964 tree
9965 c_build_qualified_type (tree type, int type_quals)
9967 if (type == error_mark_node)
9968 return type;
9970 if (TREE_CODE (type) == ARRAY_TYPE)
9972 tree t;
9973 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9974 type_quals);
9976 /* See if we already have an identically qualified type. */
9977 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9979 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9980 && TYPE_NAME (t) == TYPE_NAME (type)
9981 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9982 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9983 TYPE_ATTRIBUTES (type)))
9984 break;
9986 if (!t)
9988 tree domain = TYPE_DOMAIN (type);
9990 t = build_variant_type_copy (type);
9991 TREE_TYPE (t) = element_type;
9993 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9994 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
9995 SET_TYPE_STRUCTURAL_EQUALITY (t);
9996 else if (TYPE_CANONICAL (element_type) != element_type
9997 || (domain && TYPE_CANONICAL (domain) != domain))
9999 tree unqualified_canon
10000 = build_array_type (TYPE_CANONICAL (element_type),
10001 domain? TYPE_CANONICAL (domain)
10002 : NULL_TREE);
10003 TYPE_CANONICAL (t)
10004 = c_build_qualified_type (unqualified_canon, type_quals);
10006 else
10007 TYPE_CANONICAL (t) = t;
10009 return t;
10012 /* A restrict-qualified pointer type must be a pointer to object or
10013 incomplete type. Note that the use of POINTER_TYPE_P also allows
10014 REFERENCE_TYPEs, which is appropriate for C++. */
10015 if ((type_quals & TYPE_QUAL_RESTRICT)
10016 && (!POINTER_TYPE_P (type)
10017 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10019 error ("invalid use of %<restrict%>");
10020 type_quals &= ~TYPE_QUAL_RESTRICT;
10023 return build_qualified_type (type, type_quals);
10026 /* Build a VA_ARG_EXPR for the C parser. */
10028 tree
10029 c_build_va_arg (location_t loc, tree expr, tree type)
10031 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10032 warning_at (loc, OPT_Wc___compat,
10033 "C++ requires promoted type, not enum type, in %<va_arg%>");
10034 return build_va_arg (loc, expr, type);