Fix to PR41012
[official-gcc.git] / gcc / c-typeck.c
blob01cdcd201fe042af249298791ad7060347f0a922
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 (location_t, 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_loc (input_location, 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 (loc, 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_loc (loc, 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 =
2627 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2628 function, nargs, argarray);
2629 else
2630 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2631 function, nargs, argarray);
2632 if (TREE_CODE (result) == NOP_EXPR
2633 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2634 STRIP_TYPE_NOPS (result);
2636 else
2637 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2638 function, nargs, argarray);
2640 if (VOID_TYPE_P (TREE_TYPE (result)))
2642 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2643 pedwarn (loc, 0,
2644 "function with qualified void return type called");
2645 return result;
2647 return require_complete_type (result);
2650 /* Convert the argument expressions in the vector VALUES
2651 to the types in the list TYPELIST.
2653 If TYPELIST is exhausted, or when an element has NULL as its type,
2654 perform the default conversions.
2656 ORIGTYPES is the original types of the expressions in VALUES. This
2657 holds the type of enum values which have been converted to integral
2658 types. It may be NULL.
2660 FUNCTION is a tree for the called function. It is used only for
2661 error messages, where it is formatted with %qE.
2663 This is also where warnings about wrong number of args are generated.
2665 Returns the actual number of arguments processed (which may be less
2666 than the length of VALUES in some error situations), or -1 on
2667 failure. */
2669 static int
2670 convert_arguments (tree typelist, VEC(tree,gc) *values,
2671 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2673 tree typetail, val;
2674 unsigned int parmnum;
2675 const bool type_generic = fundecl
2676 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2677 bool type_generic_remove_excess_precision = false;
2678 tree selector;
2680 /* Change pointer to function to the function itself for
2681 diagnostics. */
2682 if (TREE_CODE (function) == ADDR_EXPR
2683 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2684 function = TREE_OPERAND (function, 0);
2686 /* Handle an ObjC selector specially for diagnostics. */
2687 selector = objc_message_selector ();
2689 /* For type-generic built-in functions, determine whether excess
2690 precision should be removed (classification) or not
2691 (comparison). */
2692 if (type_generic
2693 && DECL_BUILT_IN (fundecl)
2694 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2696 switch (DECL_FUNCTION_CODE (fundecl))
2698 case BUILT_IN_ISFINITE:
2699 case BUILT_IN_ISINF:
2700 case BUILT_IN_ISINF_SIGN:
2701 case BUILT_IN_ISNAN:
2702 case BUILT_IN_ISNORMAL:
2703 case BUILT_IN_FPCLASSIFY:
2704 type_generic_remove_excess_precision = true;
2705 break;
2707 default:
2708 type_generic_remove_excess_precision = false;
2709 break;
2713 /* Scan the given expressions and types, producing individual
2714 converted arguments. */
2716 for (typetail = typelist, parmnum = 0;
2717 VEC_iterate (tree, values, parmnum, val);
2718 ++parmnum)
2720 tree type = typetail ? TREE_VALUE (typetail) : 0;
2721 tree valtype = TREE_TYPE (val);
2722 tree rname = function;
2723 int argnum = parmnum + 1;
2724 const char *invalid_func_diag;
2725 bool excess_precision = false;
2726 bool npc;
2727 tree parmval;
2729 if (type == void_type_node)
2731 error ("too many arguments to function %qE", function);
2732 return parmnum;
2735 if (selector && argnum > 2)
2737 rname = selector;
2738 argnum -= 2;
2741 npc = null_pointer_constant_p (val);
2743 /* If there is excess precision and a prototype, convert once to
2744 the required type rather than converting via the semantic
2745 type. Likewise without a prototype a float value represented
2746 as long double should be converted once to double. But for
2747 type-generic classification functions excess precision must
2748 be removed here. */
2749 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2750 && (type || !type_generic || !type_generic_remove_excess_precision))
2752 val = TREE_OPERAND (val, 0);
2753 excess_precision = true;
2755 val = c_fully_fold (val, false, NULL);
2756 STRIP_TYPE_NOPS (val);
2758 val = require_complete_type (val);
2760 if (type != 0)
2762 /* Formal parm type is specified by a function prototype. */
2764 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2766 error ("type of formal parameter %d is incomplete", parmnum + 1);
2767 parmval = val;
2769 else
2771 tree origtype;
2773 /* Optionally warn about conversions that
2774 differ from the default conversions. */
2775 if (warn_traditional_conversion || warn_traditional)
2777 unsigned int formal_prec = TYPE_PRECISION (type);
2779 if (INTEGRAL_TYPE_P (type)
2780 && TREE_CODE (valtype) == REAL_TYPE)
2781 warning (0, "passing argument %d of %qE as integer "
2782 "rather than floating due to prototype",
2783 argnum, rname);
2784 if (INTEGRAL_TYPE_P (type)
2785 && TREE_CODE (valtype) == COMPLEX_TYPE)
2786 warning (0, "passing argument %d of %qE as integer "
2787 "rather than complex due to prototype",
2788 argnum, rname);
2789 else if (TREE_CODE (type) == COMPLEX_TYPE
2790 && TREE_CODE (valtype) == REAL_TYPE)
2791 warning (0, "passing argument %d of %qE as complex "
2792 "rather than floating due to prototype",
2793 argnum, rname);
2794 else if (TREE_CODE (type) == REAL_TYPE
2795 && INTEGRAL_TYPE_P (valtype))
2796 warning (0, "passing argument %d of %qE as floating "
2797 "rather than integer due to prototype",
2798 argnum, rname);
2799 else if (TREE_CODE (type) == COMPLEX_TYPE
2800 && INTEGRAL_TYPE_P (valtype))
2801 warning (0, "passing argument %d of %qE as complex "
2802 "rather than integer due to prototype",
2803 argnum, rname);
2804 else if (TREE_CODE (type) == REAL_TYPE
2805 && TREE_CODE (valtype) == COMPLEX_TYPE)
2806 warning (0, "passing argument %d of %qE as floating "
2807 "rather than complex due to prototype",
2808 argnum, rname);
2809 /* ??? At some point, messages should be written about
2810 conversions between complex types, but that's too messy
2811 to do now. */
2812 else if (TREE_CODE (type) == REAL_TYPE
2813 && TREE_CODE (valtype) == REAL_TYPE)
2815 /* Warn if any argument is passed as `float',
2816 since without a prototype it would be `double'. */
2817 if (formal_prec == TYPE_PRECISION (float_type_node)
2818 && type != dfloat32_type_node)
2819 warning (0, "passing argument %d of %qE as %<float%> "
2820 "rather than %<double%> due to prototype",
2821 argnum, rname);
2823 /* Warn if mismatch between argument and prototype
2824 for decimal float types. Warn of conversions with
2825 binary float types and of precision narrowing due to
2826 prototype. */
2827 else if (type != valtype
2828 && (type == dfloat32_type_node
2829 || type == dfloat64_type_node
2830 || type == dfloat128_type_node
2831 || valtype == dfloat32_type_node
2832 || valtype == dfloat64_type_node
2833 || valtype == dfloat128_type_node)
2834 && (formal_prec
2835 <= TYPE_PRECISION (valtype)
2836 || (type == dfloat128_type_node
2837 && (valtype
2838 != dfloat64_type_node
2839 && (valtype
2840 != dfloat32_type_node)))
2841 || (type == dfloat64_type_node
2842 && (valtype
2843 != dfloat32_type_node))))
2844 warning (0, "passing argument %d of %qE as %qT "
2845 "rather than %qT due to prototype",
2846 argnum, rname, type, valtype);
2849 /* Detect integer changing in width or signedness.
2850 These warnings are only activated with
2851 -Wtraditional-conversion, not with -Wtraditional. */
2852 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2853 && INTEGRAL_TYPE_P (valtype))
2855 tree would_have_been = default_conversion (val);
2856 tree type1 = TREE_TYPE (would_have_been);
2858 if (TREE_CODE (type) == ENUMERAL_TYPE
2859 && (TYPE_MAIN_VARIANT (type)
2860 == TYPE_MAIN_VARIANT (valtype)))
2861 /* No warning if function asks for enum
2862 and the actual arg is that enum type. */
2864 else if (formal_prec != TYPE_PRECISION (type1))
2865 warning (OPT_Wtraditional_conversion,
2866 "passing argument %d of %qE "
2867 "with different width due to prototype",
2868 argnum, rname);
2869 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2871 /* Don't complain if the formal parameter type
2872 is an enum, because we can't tell now whether
2873 the value was an enum--even the same enum. */
2874 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2876 else if (TREE_CODE (val) == INTEGER_CST
2877 && int_fits_type_p (val, type))
2878 /* Change in signedness doesn't matter
2879 if a constant value is unaffected. */
2881 /* If the value is extended from a narrower
2882 unsigned type, it doesn't matter whether we
2883 pass it as signed or unsigned; the value
2884 certainly is the same either way. */
2885 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2886 && TYPE_UNSIGNED (valtype))
2888 else if (TYPE_UNSIGNED (type))
2889 warning (OPT_Wtraditional_conversion,
2890 "passing argument %d of %qE "
2891 "as unsigned due to prototype",
2892 argnum, rname);
2893 else
2894 warning (OPT_Wtraditional_conversion,
2895 "passing argument %d of %qE "
2896 "as signed due to prototype", argnum, rname);
2900 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2901 sake of better warnings from convert_and_check. */
2902 if (excess_precision)
2903 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2904 origtype = (origtypes == NULL
2905 ? NULL_TREE
2906 : VEC_index (tree, origtypes, parmnum));
2907 parmval = convert_for_assignment (input_location, type, val,
2908 origtype, ic_argpass, npc,
2909 fundecl, function,
2910 parmnum + 1);
2912 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2913 && INTEGRAL_TYPE_P (type)
2914 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2915 parmval = default_conversion (parmval);
2918 else if (TREE_CODE (valtype) == REAL_TYPE
2919 && (TYPE_PRECISION (valtype)
2920 < TYPE_PRECISION (double_type_node))
2921 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2923 if (type_generic)
2924 parmval = val;
2925 else
2926 /* Convert `float' to `double'. */
2927 parmval = convert (double_type_node, val);
2929 else if (excess_precision && !type_generic)
2930 /* A "double" argument with excess precision being passed
2931 without a prototype or in variable arguments. */
2932 parmval = convert (valtype, val);
2933 else if ((invalid_func_diag =
2934 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2936 error (invalid_func_diag);
2937 return -1;
2939 else
2940 /* Convert `short' and `char' to full-size `int'. */
2941 parmval = default_conversion (val);
2943 VEC_replace (tree, values, parmnum, parmval);
2945 if (typetail)
2946 typetail = TREE_CHAIN (typetail);
2949 gcc_assert (parmnum == VEC_length (tree, values));
2951 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2953 error ("too few arguments to function %qE", function);
2954 return -1;
2957 return parmnum;
2960 /* This is the entry point used by the parser to build unary operators
2961 in the input. CODE, a tree_code, specifies the unary operator, and
2962 ARG is the operand. For unary plus, the C parser currently uses
2963 CONVERT_EXPR for code.
2965 LOC is the location to use for the tree generated.
2968 struct c_expr
2969 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
2971 struct c_expr result;
2973 result.value = build_unary_op (loc, code, arg.value, 0);
2974 result.original_code = code;
2975 result.original_type = NULL;
2977 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2978 overflow_warning (loc, result.value);
2980 return result;
2983 /* This is the entry point used by the parser to build binary operators
2984 in the input. CODE, a tree_code, specifies the binary operator, and
2985 ARG1 and ARG2 are the operands. In addition to constructing the
2986 expression, we check for operands that were written with other binary
2987 operators in a way that is likely to confuse the user.
2989 LOCATION is the location of the binary operator. */
2991 struct c_expr
2992 parser_build_binary_op (location_t location, enum tree_code code,
2993 struct c_expr arg1, struct c_expr arg2)
2995 struct c_expr result;
2997 enum tree_code code1 = arg1.original_code;
2998 enum tree_code code2 = arg2.original_code;
2999 tree type1 = (arg1.original_type
3000 ? arg1.original_type
3001 : TREE_TYPE (arg1.value));
3002 tree type2 = (arg2.original_type
3003 ? arg2.original_type
3004 : TREE_TYPE (arg2.value));
3006 result.value = build_binary_op (location, code,
3007 arg1.value, arg2.value, 1);
3008 result.original_code = code;
3009 result.original_type = NULL;
3011 if (TREE_CODE (result.value) == ERROR_MARK)
3012 return result;
3014 if (location != UNKNOWN_LOCATION)
3015 protected_set_expr_location (result.value, location);
3017 /* Check for cases such as x+y<<z which users are likely
3018 to misinterpret. */
3019 if (warn_parentheses)
3020 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3022 if (warn_logical_op)
3023 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3024 code1, arg1.value, code2, arg2.value);
3026 /* Warn about comparisons against string literals, with the exception
3027 of testing for equality or inequality of a string literal with NULL. */
3028 if (code == EQ_EXPR || code == NE_EXPR)
3030 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3031 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3032 warning_at (location, OPT_Waddress,
3033 "comparison with string literal results in unspecified behavior");
3035 else if (TREE_CODE_CLASS (code) == tcc_comparison
3036 && (code1 == STRING_CST || code2 == STRING_CST))
3037 warning_at (location, OPT_Waddress,
3038 "comparison with string literal results in unspecified behavior");
3040 if (TREE_OVERFLOW_P (result.value)
3041 && !TREE_OVERFLOW_P (arg1.value)
3042 && !TREE_OVERFLOW_P (arg2.value))
3043 overflow_warning (location, result.value);
3045 /* Warn about comparisons of different enum types. */
3046 if (warn_enum_compare
3047 && TREE_CODE_CLASS (code) == tcc_comparison
3048 && TREE_CODE (type1) == ENUMERAL_TYPE
3049 && TREE_CODE (type2) == ENUMERAL_TYPE
3050 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3051 warning_at (location, OPT_Wenum_compare,
3052 "comparison between %qT and %qT",
3053 type1, type2);
3055 return result;
3058 /* Return a tree for the difference of pointers OP0 and OP1.
3059 The resulting tree has type int. */
3061 static tree
3062 pointer_diff (location_t loc, tree op0, tree op1)
3064 tree restype = ptrdiff_type_node;
3066 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3067 tree con0, con1, lit0, lit1;
3068 tree orig_op1 = op1;
3070 if (TREE_CODE (target_type) == VOID_TYPE)
3071 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3072 "pointer of type %<void *%> used in subtraction");
3073 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3074 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3075 "pointer to a function used in subtraction");
3077 /* If the conversion to ptrdiff_type does anything like widening or
3078 converting a partial to an integral mode, we get a convert_expression
3079 that is in the way to do any simplifications.
3080 (fold-const.c doesn't know that the extra bits won't be needed.
3081 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3082 different mode in place.)
3083 So first try to find a common term here 'by hand'; we want to cover
3084 at least the cases that occur in legal static initializers. */
3085 if (CONVERT_EXPR_P (op0)
3086 && (TYPE_PRECISION (TREE_TYPE (op0))
3087 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3088 con0 = TREE_OPERAND (op0, 0);
3089 else
3090 con0 = op0;
3091 if (CONVERT_EXPR_P (op1)
3092 && (TYPE_PRECISION (TREE_TYPE (op1))
3093 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3094 con1 = TREE_OPERAND (op1, 0);
3095 else
3096 con1 = op1;
3098 if (TREE_CODE (con0) == PLUS_EXPR)
3100 lit0 = TREE_OPERAND (con0, 1);
3101 con0 = TREE_OPERAND (con0, 0);
3103 else
3104 lit0 = integer_zero_node;
3106 if (TREE_CODE (con1) == PLUS_EXPR)
3108 lit1 = TREE_OPERAND (con1, 1);
3109 con1 = TREE_OPERAND (con1, 0);
3111 else
3112 lit1 = integer_zero_node;
3114 if (operand_equal_p (con0, con1, 0))
3116 op0 = lit0;
3117 op1 = lit1;
3121 /* First do the subtraction as integers;
3122 then drop through to build the divide operator.
3123 Do not do default conversions on the minus operator
3124 in case restype is a short type. */
3126 op0 = build_binary_op (loc,
3127 MINUS_EXPR, convert (restype, op0),
3128 convert (restype, op1), 0);
3129 /* This generates an error if op1 is pointer to incomplete type. */
3130 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3131 error_at (loc, "arithmetic on pointer to an incomplete type");
3133 /* This generates an error if op0 is pointer to incomplete type. */
3134 op1 = c_size_in_bytes (target_type);
3136 /* Divide by the size, in easiest possible way. */
3137 return fold_build2_loc (loc, EXACT_DIV_EXPR, restype,
3138 op0, convert (restype, op1));
3141 /* Construct and perhaps optimize a tree representation
3142 for a unary operation. CODE, a tree_code, specifies the operation
3143 and XARG is the operand.
3144 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3145 the default promotions (such as from short to int).
3146 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3147 allows non-lvalues; this is only used to handle conversion of non-lvalue
3148 arrays to pointers in C99.
3150 LOCATION is the location of the operator. */
3152 tree
3153 build_unary_op (location_t location,
3154 enum tree_code code, tree xarg, int flag)
3156 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3157 tree arg = xarg;
3158 tree argtype = 0;
3159 enum tree_code typecode;
3160 tree val;
3161 tree ret = error_mark_node;
3162 tree eptype = NULL_TREE;
3163 int noconvert = flag;
3164 const char *invalid_op_diag;
3165 bool int_operands;
3167 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3168 if (int_operands)
3169 arg = remove_c_maybe_const_expr (arg);
3171 if (code != ADDR_EXPR)
3172 arg = require_complete_type (arg);
3174 typecode = TREE_CODE (TREE_TYPE (arg));
3175 if (typecode == ERROR_MARK)
3176 return error_mark_node;
3177 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3178 typecode = INTEGER_TYPE;
3180 if ((invalid_op_diag
3181 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3183 error_at (location, invalid_op_diag);
3184 return error_mark_node;
3187 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3189 eptype = TREE_TYPE (arg);
3190 arg = TREE_OPERAND (arg, 0);
3193 switch (code)
3195 case CONVERT_EXPR:
3196 /* This is used for unary plus, because a CONVERT_EXPR
3197 is enough to prevent anybody from looking inside for
3198 associativity, but won't generate any code. */
3199 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3200 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3201 || typecode == VECTOR_TYPE))
3203 error_at (location, "wrong type argument to unary plus");
3204 return error_mark_node;
3206 else if (!noconvert)
3207 arg = default_conversion (arg);
3208 arg = non_lvalue_loc (location, arg);
3209 break;
3211 case NEGATE_EXPR:
3212 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3213 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3214 || typecode == VECTOR_TYPE))
3216 error_at (location, "wrong type argument to unary minus");
3217 return error_mark_node;
3219 else if (!noconvert)
3220 arg = default_conversion (arg);
3221 break;
3223 case BIT_NOT_EXPR:
3224 /* ~ works on integer types and non float vectors. */
3225 if (typecode == INTEGER_TYPE
3226 || (typecode == VECTOR_TYPE
3227 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3229 if (!noconvert)
3230 arg = default_conversion (arg);
3232 else if (typecode == COMPLEX_TYPE)
3234 code = CONJ_EXPR;
3235 pedwarn (location, OPT_pedantic,
3236 "ISO C does not support %<~%> for complex conjugation");
3237 if (!noconvert)
3238 arg = default_conversion (arg);
3240 else
3242 error_at (location, "wrong type argument to bit-complement");
3243 return error_mark_node;
3245 break;
3247 case ABS_EXPR:
3248 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3250 error_at (location, "wrong type argument to abs");
3251 return error_mark_node;
3253 else if (!noconvert)
3254 arg = default_conversion (arg);
3255 break;
3257 case CONJ_EXPR:
3258 /* Conjugating a real value is a no-op, but allow it anyway. */
3259 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3260 || typecode == COMPLEX_TYPE))
3262 error_at (location, "wrong type argument to conjugation");
3263 return error_mark_node;
3265 else if (!noconvert)
3266 arg = default_conversion (arg);
3267 break;
3269 case TRUTH_NOT_EXPR:
3270 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3271 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3272 && typecode != COMPLEX_TYPE)
3274 error_at (location,
3275 "wrong type argument to unary exclamation mark");
3276 return error_mark_node;
3278 arg = c_objc_common_truthvalue_conversion (location, arg);
3279 ret = invert_truthvalue_loc (location, arg);
3280 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3281 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3282 location = EXPR_LOCATION (ret);
3283 goto return_build_unary_op;
3285 case REALPART_EXPR:
3286 if (TREE_CODE (arg) == COMPLEX_CST)
3287 ret = TREE_REALPART (arg);
3288 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3289 ret = fold_build1_loc (location,
3290 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3291 else
3292 ret = arg;
3293 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3294 eptype = TREE_TYPE (eptype);
3295 goto return_build_unary_op;
3297 case IMAGPART_EXPR:
3298 if (TREE_CODE (arg) == COMPLEX_CST)
3299 ret = TREE_IMAGPART (arg);
3300 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3301 ret = fold_build1_loc (location,
3302 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3303 else
3304 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3305 integer_zero_node, arg);
3306 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3307 eptype = TREE_TYPE (eptype);
3308 goto return_build_unary_op;
3310 case PREINCREMENT_EXPR:
3311 case POSTINCREMENT_EXPR:
3312 case PREDECREMENT_EXPR:
3313 case POSTDECREMENT_EXPR:
3315 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3317 tree inner = build_unary_op (location, code,
3318 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3319 if (inner == error_mark_node)
3320 return error_mark_node;
3321 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3322 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3323 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3324 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3325 goto return_build_unary_op;
3328 /* Complain about anything that is not a true lvalue. */
3329 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3330 || code == POSTINCREMENT_EXPR)
3331 ? lv_increment
3332 : lv_decrement)))
3333 return error_mark_node;
3335 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3337 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3338 warning_at (location, OPT_Wc___compat,
3339 "increment of enumeration value is invalid in C++");
3340 else
3341 warning_at (location, OPT_Wc___compat,
3342 "decrement of enumeration value is invalid in C++");
3345 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3346 arg = c_fully_fold (arg, false, NULL);
3348 /* Increment or decrement the real part of the value,
3349 and don't change the imaginary part. */
3350 if (typecode == COMPLEX_TYPE)
3352 tree real, imag;
3354 pedwarn (location, OPT_pedantic,
3355 "ISO C does not support %<++%> and %<--%> on complex types");
3357 arg = stabilize_reference (arg);
3358 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3359 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3360 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3361 if (real == error_mark_node || imag == error_mark_node)
3362 return error_mark_node;
3363 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3364 real, imag);
3365 goto return_build_unary_op;
3368 /* Report invalid types. */
3370 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3371 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3373 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3374 error_at (location, "wrong type argument to increment");
3375 else
3376 error_at (location, "wrong type argument to decrement");
3378 return error_mark_node;
3382 tree inc;
3384 argtype = TREE_TYPE (arg);
3386 /* Compute the increment. */
3388 if (typecode == POINTER_TYPE)
3390 /* If pointer target is an undefined struct,
3391 we just cannot know how to do the arithmetic. */
3392 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3395 error_at (location,
3396 "increment of pointer to unknown structure");
3397 else
3398 error_at (location,
3399 "decrement of pointer to unknown structure");
3401 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3402 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3404 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3405 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3406 "wrong type argument to increment");
3407 else
3408 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3409 "wrong type argument to decrement");
3412 inc = c_size_in_bytes (TREE_TYPE (argtype));
3413 inc = fold_convert_loc (location, sizetype, inc);
3415 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3417 /* For signed fract types, we invert ++ to -- or
3418 -- to ++, and change inc from 1 to -1, because
3419 it is not possible to represent 1 in signed fract constants.
3420 For unsigned fract types, the result always overflows and
3421 we get an undefined (original) or the maximum value. */
3422 if (code == PREINCREMENT_EXPR)
3423 code = PREDECREMENT_EXPR;
3424 else if (code == PREDECREMENT_EXPR)
3425 code = PREINCREMENT_EXPR;
3426 else if (code == POSTINCREMENT_EXPR)
3427 code = POSTDECREMENT_EXPR;
3428 else /* code == POSTDECREMENT_EXPR */
3429 code = POSTINCREMENT_EXPR;
3431 inc = integer_minus_one_node;
3432 inc = convert (argtype, inc);
3434 else
3436 inc = integer_one_node;
3437 inc = convert (argtype, inc);
3440 /* Report a read-only lvalue. */
3441 if (TYPE_READONLY (argtype))
3443 readonly_error (arg,
3444 ((code == PREINCREMENT_EXPR
3445 || code == POSTINCREMENT_EXPR)
3446 ? lv_increment : lv_decrement));
3447 return error_mark_node;
3449 else if (TREE_READONLY (arg))
3450 readonly_warning (arg,
3451 ((code == PREINCREMENT_EXPR
3452 || code == POSTINCREMENT_EXPR)
3453 ? lv_increment : lv_decrement));
3455 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3456 val = boolean_increment (code, arg);
3457 else
3458 val = build2 (code, TREE_TYPE (arg), arg, inc);
3459 TREE_SIDE_EFFECTS (val) = 1;
3460 if (TREE_CODE (val) != code)
3461 TREE_NO_WARNING (val) = 1;
3462 ret = val;
3463 goto return_build_unary_op;
3466 case ADDR_EXPR:
3467 /* Note that this operation never does default_conversion. */
3469 /* The operand of unary '&' must be an lvalue (which excludes
3470 expressions of type void), or, in C99, the result of a [] or
3471 unary '*' operator. */
3472 if (VOID_TYPE_P (TREE_TYPE (arg))
3473 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3474 && (TREE_CODE (arg) != INDIRECT_REF
3475 || !flag_isoc99))
3476 pedwarn (location, 0, "taking address of expression of type %<void%>");
3478 /* Let &* cancel out to simplify resulting code. */
3479 if (TREE_CODE (arg) == INDIRECT_REF)
3481 /* Don't let this be an lvalue. */
3482 if (lvalue_p (TREE_OPERAND (arg, 0)))
3483 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3484 ret = TREE_OPERAND (arg, 0);
3485 goto return_build_unary_op;
3488 /* For &x[y], return x+y */
3489 if (TREE_CODE (arg) == ARRAY_REF)
3491 tree op0 = TREE_OPERAND (arg, 0);
3492 if (!c_mark_addressable (op0))
3493 return error_mark_node;
3494 return build_binary_op (location, PLUS_EXPR,
3495 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3496 ? array_to_pointer_conversion (location,
3497 op0)
3498 : op0),
3499 TREE_OPERAND (arg, 1), 1);
3502 /* Anything not already handled and not a true memory reference
3503 or a non-lvalue array is an error. */
3504 else if (typecode != FUNCTION_TYPE && !flag
3505 && !lvalue_or_else (arg, lv_addressof))
3506 return error_mark_node;
3508 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3509 folding later. */
3510 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3512 tree inner = build_unary_op (location, code,
3513 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3514 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3515 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3516 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3517 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3518 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3519 goto return_build_unary_op;
3522 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3523 argtype = TREE_TYPE (arg);
3525 /* If the lvalue is const or volatile, merge that into the type
3526 to which the address will point. Note that you can't get a
3527 restricted pointer by taking the address of something, so we
3528 only have to deal with `const' and `volatile' here. */
3529 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3530 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3531 argtype = c_build_type_variant (argtype,
3532 TREE_READONLY (arg),
3533 TREE_THIS_VOLATILE (arg));
3535 if (!c_mark_addressable (arg))
3536 return error_mark_node;
3538 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3539 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3541 argtype = build_pointer_type (argtype);
3543 /* ??? Cope with user tricks that amount to offsetof. Delete this
3544 when we have proper support for integer constant expressions. */
3545 val = get_base_address (arg);
3546 if (val && TREE_CODE (val) == INDIRECT_REF
3547 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3549 tree op0 = fold_convert_loc (location, sizetype,
3550 fold_offsetof (arg, val)), op1;
3552 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3553 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3554 goto return_build_unary_op;
3557 val = build1 (ADDR_EXPR, argtype, arg);
3559 ret = val;
3560 goto return_build_unary_op;
3562 default:
3563 gcc_unreachable ();
3566 if (argtype == 0)
3567 argtype = TREE_TYPE (arg);
3568 if (TREE_CODE (arg) == INTEGER_CST)
3569 ret = (require_constant_value
3570 ? fold_build1_initializer_loc (location, code, argtype, arg)
3571 : fold_build1_loc (location, code, argtype, arg));
3572 else
3573 ret = build1 (code, argtype, arg);
3574 return_build_unary_op:
3575 gcc_assert (ret != error_mark_node);
3576 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3577 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3578 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3579 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3580 ret = note_integer_operands (ret);
3581 if (eptype)
3582 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3583 protected_set_expr_location (ret, location);
3584 return ret;
3587 /* Return nonzero if REF is an lvalue valid for this language.
3588 Lvalues can be assigned, unless their type has TYPE_READONLY.
3589 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3591 bool
3592 lvalue_p (const_tree ref)
3594 const enum tree_code code = TREE_CODE (ref);
3596 switch (code)
3598 case REALPART_EXPR:
3599 case IMAGPART_EXPR:
3600 case COMPONENT_REF:
3601 return lvalue_p (TREE_OPERAND (ref, 0));
3603 case C_MAYBE_CONST_EXPR:
3604 return lvalue_p (TREE_OPERAND (ref, 1));
3606 case COMPOUND_LITERAL_EXPR:
3607 case STRING_CST:
3608 return 1;
3610 case INDIRECT_REF:
3611 case ARRAY_REF:
3612 case VAR_DECL:
3613 case PARM_DECL:
3614 case RESULT_DECL:
3615 case ERROR_MARK:
3616 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3617 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3619 case BIND_EXPR:
3620 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3622 default:
3623 return 0;
3627 /* Give an error for storing in something that is 'const'. */
3629 static void
3630 readonly_error (tree arg, enum lvalue_use use)
3632 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3633 || use == lv_asm);
3634 /* Using this macro rather than (for example) arrays of messages
3635 ensures that all the format strings are checked at compile
3636 time. */
3637 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3638 : (use == lv_increment ? (I) \
3639 : (use == lv_decrement ? (D) : (AS))))
3640 if (TREE_CODE (arg) == COMPONENT_REF)
3642 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3643 readonly_error (TREE_OPERAND (arg, 0), use);
3644 else
3645 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3646 G_("increment of read-only member %qD"),
3647 G_("decrement of read-only member %qD"),
3648 G_("read-only member %qD used as %<asm%> output")),
3649 TREE_OPERAND (arg, 1));
3651 else if (TREE_CODE (arg) == VAR_DECL)
3652 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3653 G_("increment of read-only variable %qD"),
3654 G_("decrement of read-only variable %qD"),
3655 G_("read-only variable %qD used as %<asm%> output")),
3656 arg);
3657 else
3658 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3659 G_("increment of read-only location %qE"),
3660 G_("decrement of read-only location %qE"),
3661 G_("read-only location %qE used as %<asm%> output")),
3662 arg);
3665 /* Give a warning for storing in something that is read-only in GCC
3666 terms but not const in ISO C terms. */
3668 static void
3669 readonly_warning (tree arg, enum lvalue_use use)
3671 switch (use)
3673 case lv_assign:
3674 warning (0, "assignment of read-only location %qE", arg);
3675 break;
3676 case lv_increment:
3677 warning (0, "increment of read-only location %qE", arg);
3678 break;
3679 case lv_decrement:
3680 warning (0, "decrement of read-only location %qE", arg);
3681 break;
3682 default:
3683 gcc_unreachable ();
3685 return;
3689 /* Return nonzero if REF is an lvalue valid for this language;
3690 otherwise, print an error message and return zero. USE says
3691 how the lvalue is being used and so selects the error message. */
3693 static int
3694 lvalue_or_else (const_tree ref, enum lvalue_use use)
3696 int win = lvalue_p (ref);
3698 if (!win)
3699 lvalue_error (use);
3701 return win;
3704 /* Mark EXP saying that we need to be able to take the
3705 address of it; it should not be allocated in a register.
3706 Returns true if successful. */
3708 bool
3709 c_mark_addressable (tree exp)
3711 tree x = exp;
3713 while (1)
3714 switch (TREE_CODE (x))
3716 case COMPONENT_REF:
3717 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3719 error
3720 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3721 return false;
3724 /* ... fall through ... */
3726 case ADDR_EXPR:
3727 case ARRAY_REF:
3728 case REALPART_EXPR:
3729 case IMAGPART_EXPR:
3730 x = TREE_OPERAND (x, 0);
3731 break;
3733 case COMPOUND_LITERAL_EXPR:
3734 case CONSTRUCTOR:
3735 TREE_ADDRESSABLE (x) = 1;
3736 return true;
3738 case VAR_DECL:
3739 case CONST_DECL:
3740 case PARM_DECL:
3741 case RESULT_DECL:
3742 if (C_DECL_REGISTER (x)
3743 && DECL_NONLOCAL (x))
3745 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3747 error
3748 ("global register variable %qD used in nested function", x);
3749 return false;
3751 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3753 else if (C_DECL_REGISTER (x))
3755 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3756 error ("address of global register variable %qD requested", x);
3757 else
3758 error ("address of register variable %qD requested", x);
3759 return false;
3762 /* drops in */
3763 case FUNCTION_DECL:
3764 TREE_ADDRESSABLE (x) = 1;
3765 /* drops out */
3766 default:
3767 return true;
3771 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3772 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3773 if folded to an integer constant then the unselected half may
3774 contain arbitrary operations not normally permitted in constant
3775 expressions. Set the location of the expression to LOC. */
3777 tree
3778 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
3779 tree op1, tree op1_original_type, tree op2,
3780 tree op2_original_type)
3782 tree type1;
3783 tree type2;
3784 enum tree_code code1;
3785 enum tree_code code2;
3786 tree result_type = NULL;
3787 tree ep_result_type = NULL;
3788 tree orig_op1 = op1, orig_op2 = op2;
3789 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3790 bool ifexp_int_operands;
3791 tree ret;
3792 bool objc_ok;
3794 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3795 if (op1_int_operands)
3796 op1 = remove_c_maybe_const_expr (op1);
3797 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3798 if (op2_int_operands)
3799 op2 = remove_c_maybe_const_expr (op2);
3800 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3801 if (ifexp_int_operands)
3802 ifexp = remove_c_maybe_const_expr (ifexp);
3804 /* Promote both alternatives. */
3806 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3807 op1 = default_conversion (op1);
3808 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3809 op2 = default_conversion (op2);
3811 if (TREE_CODE (ifexp) == ERROR_MARK
3812 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3813 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3814 return error_mark_node;
3816 type1 = TREE_TYPE (op1);
3817 code1 = TREE_CODE (type1);
3818 type2 = TREE_TYPE (op2);
3819 code2 = TREE_CODE (type2);
3821 /* C90 does not permit non-lvalue arrays in conditional expressions.
3822 In C99 they will be pointers by now. */
3823 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3825 error_at (colon_loc, "non-lvalue array in conditional expression");
3826 return error_mark_node;
3829 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3831 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3832 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3833 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3834 || code1 == COMPLEX_TYPE)
3835 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3836 || code2 == COMPLEX_TYPE))
3838 ep_result_type = c_common_type (type1, type2);
3839 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3841 op1 = TREE_OPERAND (op1, 0);
3842 type1 = TREE_TYPE (op1);
3843 gcc_assert (TREE_CODE (type1) == code1);
3845 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3847 op2 = TREE_OPERAND (op2, 0);
3848 type2 = TREE_TYPE (op2);
3849 gcc_assert (TREE_CODE (type2) == code2);
3853 if (warn_cxx_compat)
3855 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
3856 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
3858 if (TREE_CODE (t1) == ENUMERAL_TYPE
3859 && TREE_CODE (t2) == ENUMERAL_TYPE
3860 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
3861 warning_at (colon_loc, OPT_Wc___compat,
3862 ("different enum types in conditional is "
3863 "invalid in C++: %qT vs %qT"),
3864 t1, t2);
3867 /* Quickly detect the usual case where op1 and op2 have the same type
3868 after promotion. */
3869 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3871 if (type1 == type2)
3872 result_type = type1;
3873 else
3874 result_type = TYPE_MAIN_VARIANT (type1);
3876 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3877 || code1 == COMPLEX_TYPE)
3878 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3879 || code2 == COMPLEX_TYPE))
3881 result_type = c_common_type (type1, type2);
3883 /* If -Wsign-compare, warn here if type1 and type2 have
3884 different signedness. We'll promote the signed to unsigned
3885 and later code won't know it used to be different.
3886 Do this check on the original types, so that explicit casts
3887 will be considered, but default promotions won't. */
3888 if (c_inhibit_evaluation_warnings == 0)
3890 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3891 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3893 if (unsigned_op1 ^ unsigned_op2)
3895 bool ovf;
3897 /* Do not warn if the result type is signed, since the
3898 signed type will only be chosen if it can represent
3899 all the values of the unsigned type. */
3900 if (!TYPE_UNSIGNED (result_type))
3901 /* OK */;
3902 else
3904 bool op1_maybe_const = true;
3905 bool op2_maybe_const = true;
3907 /* Do not warn if the signed quantity is an
3908 unsuffixed integer literal (or some static
3909 constant expression involving such literals) and
3910 it is non-negative. This warning requires the
3911 operands to be folded for best results, so do
3912 that folding in this case even without
3913 warn_sign_compare to avoid warning options
3914 possibly affecting code generation. */
3915 c_inhibit_evaluation_warnings
3916 += (ifexp == truthvalue_false_node);
3917 op1 = c_fully_fold (op1, require_constant_value,
3918 &op1_maybe_const);
3919 c_inhibit_evaluation_warnings
3920 -= (ifexp == truthvalue_false_node);
3922 c_inhibit_evaluation_warnings
3923 += (ifexp == truthvalue_true_node);
3924 op2 = c_fully_fold (op2, require_constant_value,
3925 &op2_maybe_const);
3926 c_inhibit_evaluation_warnings
3927 -= (ifexp == truthvalue_true_node);
3929 if (warn_sign_compare)
3931 if ((unsigned_op2
3932 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3933 || (unsigned_op1
3934 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3935 /* OK */;
3936 else
3937 warning_at (colon_loc, OPT_Wsign_compare,
3938 ("signed and unsigned type in "
3939 "conditional expression"));
3941 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3943 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3944 NULL, op1);
3945 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3947 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3949 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3950 NULL, op2);
3951 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3957 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3959 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3960 pedwarn (colon_loc, OPT_pedantic,
3961 "ISO C forbids conditional expr with only one void side");
3962 result_type = void_type_node;
3964 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3966 if (comp_target_types (colon_loc, type1, type2))
3967 result_type = common_pointer_type (type1, type2);
3968 else if (null_pointer_constant_p (orig_op1))
3969 result_type = qualify_type (type2, type1);
3970 else if (null_pointer_constant_p (orig_op2))
3971 result_type = qualify_type (type1, type2);
3972 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3974 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3975 pedwarn (colon_loc, OPT_pedantic,
3976 "ISO C forbids conditional expr between "
3977 "%<void *%> and function pointer");
3978 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3979 TREE_TYPE (type2)));
3981 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3983 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3984 pedwarn (colon_loc, OPT_pedantic,
3985 "ISO C forbids conditional expr between "
3986 "%<void *%> and function pointer");
3987 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3988 TREE_TYPE (type1)));
3990 else
3992 if (!objc_ok)
3993 pedwarn (colon_loc, 0,
3994 "pointer type mismatch in conditional expression");
3995 result_type = build_pointer_type (void_type_node);
3998 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4000 if (!null_pointer_constant_p (orig_op2))
4001 pedwarn (colon_loc, 0,
4002 "pointer/integer type mismatch in conditional expression");
4003 else
4005 op2 = null_pointer_node;
4007 result_type = type1;
4009 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4011 if (!null_pointer_constant_p (orig_op1))
4012 pedwarn (colon_loc, 0,
4013 "pointer/integer type mismatch in conditional expression");
4014 else
4016 op1 = null_pointer_node;
4018 result_type = type2;
4021 if (!result_type)
4023 if (flag_cond_mismatch)
4024 result_type = void_type_node;
4025 else
4027 error_at (colon_loc, "type mismatch in conditional expression");
4028 return error_mark_node;
4032 /* Merge const and volatile flags of the incoming types. */
4033 result_type
4034 = build_type_variant (result_type,
4035 TREE_READONLY (op1) || TREE_READONLY (op2),
4036 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4038 if (result_type != TREE_TYPE (op1))
4039 op1 = convert_and_check (result_type, op1);
4040 if (result_type != TREE_TYPE (op2))
4041 op2 = convert_and_check (result_type, op2);
4043 if (ifexp_bcp && ifexp == truthvalue_true_node)
4045 op2_int_operands = true;
4046 op1 = c_fully_fold (op1, require_constant_value, NULL);
4048 if (ifexp_bcp && ifexp == truthvalue_false_node)
4050 op1_int_operands = true;
4051 op2 = c_fully_fold (op2, require_constant_value, NULL);
4053 int_const = int_operands = (ifexp_int_operands
4054 && op1_int_operands
4055 && op2_int_operands);
4056 if (int_operands)
4058 int_const = ((ifexp == truthvalue_true_node
4059 && TREE_CODE (orig_op1) == INTEGER_CST
4060 && !TREE_OVERFLOW (orig_op1))
4061 || (ifexp == truthvalue_false_node
4062 && TREE_CODE (orig_op2) == INTEGER_CST
4063 && !TREE_OVERFLOW (orig_op2)));
4065 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4066 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4067 else
4069 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4070 if (int_operands)
4071 ret = note_integer_operands (ret);
4073 if (ep_result_type)
4074 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
4076 protected_set_expr_location (ret, colon_loc);
4077 return ret;
4080 /* Return a compound expression that performs two expressions and
4081 returns the value of the second of them.
4083 LOC is the location of the COMPOUND_EXPR. */
4085 tree
4086 build_compound_expr (location_t loc, tree expr1, tree expr2)
4088 bool expr1_int_operands, expr2_int_operands;
4089 tree eptype = NULL_TREE;
4090 tree ret;
4092 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4093 if (expr1_int_operands)
4094 expr1 = remove_c_maybe_const_expr (expr1);
4095 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4096 if (expr2_int_operands)
4097 expr2 = remove_c_maybe_const_expr (expr2);
4099 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4100 expr1 = TREE_OPERAND (expr1, 0);
4101 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4103 eptype = TREE_TYPE (expr2);
4104 expr2 = TREE_OPERAND (expr2, 0);
4107 if (!TREE_SIDE_EFFECTS (expr1))
4109 /* The left-hand operand of a comma expression is like an expression
4110 statement: with -Wunused, we should warn if it doesn't have
4111 any side-effects, unless it was explicitly cast to (void). */
4112 if (warn_unused_value)
4114 if (VOID_TYPE_P (TREE_TYPE (expr1))
4115 && CONVERT_EXPR_P (expr1))
4116 ; /* (void) a, b */
4117 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4118 && TREE_CODE (expr1) == COMPOUND_EXPR
4119 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4120 ; /* (void) a, (void) b, c */
4121 else
4122 warning_at (loc, OPT_Wunused_value,
4123 "left-hand operand of comma expression has no effect");
4127 /* With -Wunused, we should also warn if the left-hand operand does have
4128 side-effects, but computes a value which is not used. For example, in
4129 `foo() + bar(), baz()' the result of the `+' operator is not used,
4130 so we should issue a warning. */
4131 else if (warn_unused_value)
4132 warn_if_unused_value (expr1, loc);
4134 if (expr2 == error_mark_node)
4135 return error_mark_node;
4137 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4139 if (flag_isoc99
4140 && expr1_int_operands
4141 && expr2_int_operands)
4142 ret = note_integer_operands (ret);
4144 if (eptype)
4145 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4147 protected_set_expr_location (ret, loc);
4148 return ret;
4151 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4152 which we are casting. OTYPE is the type of the expression being
4153 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4154 on the command line. */
4156 static void
4157 handle_warn_cast_qual (tree type, tree otype)
4159 tree in_type = type;
4160 tree in_otype = otype;
4161 int added = 0;
4162 int discarded = 0;
4163 bool is_const;
4165 /* Check that the qualifiers on IN_TYPE are a superset of the
4166 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4167 nodes is uninteresting and we stop as soon as we hit a
4168 non-POINTER_TYPE node on either type. */
4171 in_otype = TREE_TYPE (in_otype);
4172 in_type = TREE_TYPE (in_type);
4174 /* GNU C allows cv-qualified function types. 'const' means the
4175 function is very pure, 'volatile' means it can't return. We
4176 need to warn when such qualifiers are added, not when they're
4177 taken away. */
4178 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4179 && TREE_CODE (in_type) == FUNCTION_TYPE)
4180 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4181 else
4182 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4184 while (TREE_CODE (in_type) == POINTER_TYPE
4185 && TREE_CODE (in_otype) == POINTER_TYPE);
4187 if (added)
4188 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4190 if (discarded)
4191 /* There are qualifiers present in IN_OTYPE that are not present
4192 in IN_TYPE. */
4193 warning (OPT_Wcast_qual,
4194 "cast discards qualifiers from pointer target type");
4196 if (added || discarded)
4197 return;
4199 /* A cast from **T to const **T is unsafe, because it can cause a
4200 const value to be changed with no additional warning. We only
4201 issue this warning if T is the same on both sides, and we only
4202 issue the warning if there are the same number of pointers on
4203 both sides, as otherwise the cast is clearly unsafe anyhow. A
4204 cast is unsafe when a qualifier is added at one level and const
4205 is not present at all outer levels.
4207 To issue this warning, we check at each level whether the cast
4208 adds new qualifiers not already seen. We don't need to special
4209 case function types, as they won't have the same
4210 TYPE_MAIN_VARIANT. */
4212 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4213 return;
4214 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4215 return;
4217 in_type = type;
4218 in_otype = otype;
4219 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4222 in_type = TREE_TYPE (in_type);
4223 in_otype = TREE_TYPE (in_otype);
4224 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4225 && !is_const)
4227 warning (OPT_Wcast_qual,
4228 ("new qualifiers in middle of multi-level non-const cast "
4229 "are unsafe"));
4230 break;
4232 if (is_const)
4233 is_const = TYPE_READONLY (in_type);
4235 while (TREE_CODE (in_type) == POINTER_TYPE);
4238 /* Build an expression representing a cast to type TYPE of expression EXPR.
4239 LOC is the location of the cast-- typically the open paren of the cast. */
4241 tree
4242 build_c_cast (location_t loc, tree type, tree expr)
4244 tree value;
4246 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4247 expr = TREE_OPERAND (expr, 0);
4249 value = expr;
4251 if (type == error_mark_node || expr == error_mark_node)
4252 return error_mark_node;
4254 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4255 only in <protocol> qualifications. But when constructing cast expressions,
4256 the protocols do matter and must be kept around. */
4257 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4258 return build1 (NOP_EXPR, type, expr);
4260 type = TYPE_MAIN_VARIANT (type);
4262 if (TREE_CODE (type) == ARRAY_TYPE)
4264 error_at (loc, "cast specifies array type");
4265 return error_mark_node;
4268 if (TREE_CODE (type) == FUNCTION_TYPE)
4270 error_at (loc, "cast specifies function type");
4271 return error_mark_node;
4274 if (!VOID_TYPE_P (type))
4276 value = require_complete_type (value);
4277 if (value == error_mark_node)
4278 return error_mark_node;
4281 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4283 if (TREE_CODE (type) == RECORD_TYPE
4284 || TREE_CODE (type) == UNION_TYPE)
4285 pedwarn (loc, OPT_pedantic,
4286 "ISO C forbids casting nonscalar to the same type");
4288 else if (TREE_CODE (type) == UNION_TYPE)
4290 tree field;
4292 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4293 if (TREE_TYPE (field) != error_mark_node
4294 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4295 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4296 break;
4298 if (field)
4300 tree t;
4302 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4303 t = digest_init (loc, type,
4304 build_constructor_single (type, field, value),
4305 NULL_TREE, false, true, 0);
4306 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4307 return t;
4309 error_at (loc, "cast to union type from type not present in union");
4310 return error_mark_node;
4312 else
4314 tree otype, ovalue;
4316 if (type == void_type_node)
4318 tree t = build1 (CONVERT_EXPR, type, value);
4319 SET_EXPR_LOCATION (t, loc);
4320 return t;
4323 otype = TREE_TYPE (value);
4325 /* Optionally warn about potentially worrisome casts. */
4326 if (warn_cast_qual
4327 && TREE_CODE (type) == POINTER_TYPE
4328 && TREE_CODE (otype) == POINTER_TYPE)
4329 handle_warn_cast_qual (type, otype);
4331 /* Warn about possible alignment problems. */
4332 if (STRICT_ALIGNMENT
4333 && TREE_CODE (type) == POINTER_TYPE
4334 && TREE_CODE (otype) == POINTER_TYPE
4335 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4336 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4337 /* Don't warn about opaque types, where the actual alignment
4338 restriction is unknown. */
4339 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4340 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4341 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4342 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4343 warning_at (loc, OPT_Wcast_align,
4344 "cast increases required alignment of target type");
4346 if (TREE_CODE (type) == INTEGER_TYPE
4347 && TREE_CODE (otype) == POINTER_TYPE
4348 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4349 /* Unlike conversion of integers to pointers, where the
4350 warning is disabled for converting constants because
4351 of cases such as SIG_*, warn about converting constant
4352 pointers to integers. In some cases it may cause unwanted
4353 sign extension, and a warning is appropriate. */
4354 warning_at (loc, OPT_Wpointer_to_int_cast,
4355 "cast from pointer to integer of different size");
4357 if (TREE_CODE (value) == CALL_EXPR
4358 && TREE_CODE (type) != TREE_CODE (otype))
4359 warning_at (loc, OPT_Wbad_function_cast,
4360 "cast from function call of type %qT "
4361 "to non-matching type %qT", otype, type);
4363 if (TREE_CODE (type) == POINTER_TYPE
4364 && TREE_CODE (otype) == INTEGER_TYPE
4365 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4366 /* Don't warn about converting any constant. */
4367 && !TREE_CONSTANT (value))
4368 warning_at (loc,
4369 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4370 "of different size");
4372 if (warn_strict_aliasing <= 2)
4373 strict_aliasing_warning (otype, type, expr);
4375 /* If pedantic, warn for conversions between function and object
4376 pointer types, except for converting a null pointer constant
4377 to function pointer type. */
4378 if (pedantic
4379 && TREE_CODE (type) == POINTER_TYPE
4380 && TREE_CODE (otype) == POINTER_TYPE
4381 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4382 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4383 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4384 "conversion of function pointer to object pointer type");
4386 if (pedantic
4387 && TREE_CODE (type) == POINTER_TYPE
4388 && TREE_CODE (otype) == POINTER_TYPE
4389 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4390 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4391 && !null_pointer_constant_p (value))
4392 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4393 "conversion of object pointer to function pointer type");
4395 ovalue = value;
4396 value = convert (type, value);
4398 /* Ignore any integer overflow caused by the cast. */
4399 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4401 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4403 if (!TREE_OVERFLOW (value))
4405 /* Avoid clobbering a shared constant. */
4406 value = copy_node (value);
4407 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4410 else if (TREE_OVERFLOW (value))
4411 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4412 value = build_int_cst_wide (TREE_TYPE (value),
4413 TREE_INT_CST_LOW (value),
4414 TREE_INT_CST_HIGH (value));
4418 /* Don't let a cast be an lvalue. */
4419 if (value == expr)
4420 value = non_lvalue_loc (loc, value);
4422 /* Don't allow the results of casting to floating-point or complex
4423 types be confused with actual constants, or casts involving
4424 integer and pointer types other than direct integer-to-integer
4425 and integer-to-pointer be confused with integer constant
4426 expressions and null pointer constants. */
4427 if (TREE_CODE (value) == REAL_CST
4428 || TREE_CODE (value) == COMPLEX_CST
4429 || (TREE_CODE (value) == INTEGER_CST
4430 && !((TREE_CODE (expr) == INTEGER_CST
4431 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4432 || TREE_CODE (expr) == REAL_CST
4433 || TREE_CODE (expr) == COMPLEX_CST)))
4434 value = build1 (NOP_EXPR, type, value);
4436 if (CAN_HAVE_LOCATION_P (value))
4437 SET_EXPR_LOCATION (value, loc);
4438 return value;
4441 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4442 location of the open paren of the cast, or the position of the cast
4443 expr. */
4444 tree
4445 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4447 tree type;
4448 tree type_expr = NULL_TREE;
4449 bool type_expr_const = true;
4450 tree ret;
4451 int saved_wsp = warn_strict_prototypes;
4453 /* This avoids warnings about unprototyped casts on
4454 integers. E.g. "#define SIG_DFL (void(*)())0". */
4455 if (TREE_CODE (expr) == INTEGER_CST)
4456 warn_strict_prototypes = 0;
4457 type = groktypename (type_name, &type_expr, &type_expr_const);
4458 warn_strict_prototypes = saved_wsp;
4460 ret = build_c_cast (loc, type, expr);
4461 if (type_expr)
4463 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4464 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4465 SET_EXPR_LOCATION (ret, loc);
4468 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4469 SET_EXPR_LOCATION (ret, loc);
4471 /* C++ does not permits types to be defined in a cast. */
4472 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4473 warning_at (loc, OPT_Wc___compat,
4474 "defining a type in a cast is invalid in C++");
4476 return ret;
4479 /* Build an assignment expression of lvalue LHS from value RHS.
4480 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4481 may differ from TREE_TYPE (LHS) for an enum bitfield.
4482 MODIFYCODE is the code for a binary operator that we use
4483 to combine the old value of LHS with RHS to get the new value.
4484 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4485 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4486 which may differ from TREE_TYPE (RHS) for an enum value.
4488 LOCATION is the location of the MODIFYCODE operator.
4489 RHS_LOC is the location of the RHS. */
4491 tree
4492 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4493 enum tree_code modifycode,
4494 location_t rhs_loc, tree rhs, tree rhs_origtype)
4496 tree result;
4497 tree newrhs;
4498 tree rhs_semantic_type = NULL_TREE;
4499 tree lhstype = TREE_TYPE (lhs);
4500 tree olhstype = lhstype;
4501 bool npc;
4503 /* Types that aren't fully specified cannot be used in assignments. */
4504 lhs = require_complete_type (lhs);
4506 /* Avoid duplicate error messages from operands that had errors. */
4507 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4508 return error_mark_node;
4510 if (!lvalue_or_else (lhs, lv_assign))
4511 return error_mark_node;
4513 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4515 rhs_semantic_type = TREE_TYPE (rhs);
4516 rhs = TREE_OPERAND (rhs, 0);
4519 newrhs = rhs;
4521 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4523 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4524 lhs_origtype, modifycode, rhs_loc, rhs,
4525 rhs_origtype);
4526 if (inner == error_mark_node)
4527 return error_mark_node;
4528 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4529 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4530 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4531 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4532 protected_set_expr_location (result, location);
4533 return result;
4536 /* If a binary op has been requested, combine the old LHS value with the RHS
4537 producing the value we should actually store into the LHS. */
4539 if (modifycode != NOP_EXPR)
4541 lhs = c_fully_fold (lhs, false, NULL);
4542 lhs = stabilize_reference (lhs);
4543 newrhs = build_binary_op (location,
4544 modifycode, lhs, rhs, 1);
4546 /* The original type of the right hand side is no longer
4547 meaningful. */
4548 rhs_origtype = NULL_TREE;
4551 /* Give an error for storing in something that is 'const'. */
4553 if (TYPE_READONLY (lhstype)
4554 || ((TREE_CODE (lhstype) == RECORD_TYPE
4555 || TREE_CODE (lhstype) == UNION_TYPE)
4556 && C_TYPE_FIELDS_READONLY (lhstype)))
4558 readonly_error (lhs, lv_assign);
4559 return error_mark_node;
4561 else if (TREE_READONLY (lhs))
4562 readonly_warning (lhs, lv_assign);
4564 /* If storing into a structure or union member,
4565 it has probably been given type `int'.
4566 Compute the type that would go with
4567 the actual amount of storage the member occupies. */
4569 if (TREE_CODE (lhs) == COMPONENT_REF
4570 && (TREE_CODE (lhstype) == INTEGER_TYPE
4571 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4572 || TREE_CODE (lhstype) == REAL_TYPE
4573 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4574 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4576 /* If storing in a field that is in actuality a short or narrower than one,
4577 we must store in the field in its actual type. */
4579 if (lhstype != TREE_TYPE (lhs))
4581 lhs = copy_node (lhs);
4582 TREE_TYPE (lhs) = lhstype;
4585 /* Issue -Wc++-compat warnings about an assignment to an enum type
4586 when LHS does not have its original type. This happens for,
4587 e.g., an enum bitfield in a struct. */
4588 if (warn_cxx_compat
4589 && lhs_origtype != NULL_TREE
4590 && lhs_origtype != lhstype
4591 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4593 tree checktype = (rhs_origtype != NULL_TREE
4594 ? rhs_origtype
4595 : TREE_TYPE (rhs));
4596 if (checktype != error_mark_node
4597 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4598 warning_at (location, OPT_Wc___compat,
4599 "enum conversion in assignment is invalid in C++");
4602 /* Convert new value to destination type. Fold it first, then
4603 restore any excess precision information, for the sake of
4604 conversion warnings. */
4606 npc = null_pointer_constant_p (newrhs);
4607 newrhs = c_fully_fold (newrhs, false, NULL);
4608 if (rhs_semantic_type)
4609 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4610 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4611 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4612 if (TREE_CODE (newrhs) == ERROR_MARK)
4613 return error_mark_node;
4615 /* Emit ObjC write barrier, if necessary. */
4616 if (c_dialect_objc () && flag_objc_gc)
4618 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4619 if (result)
4621 protected_set_expr_location (result, location);
4622 return result;
4626 /* Scan operands. */
4628 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4629 TREE_SIDE_EFFECTS (result) = 1;
4630 protected_set_expr_location (result, location);
4632 /* If we got the LHS in a different type for storing in,
4633 convert the result back to the nominal type of LHS
4634 so that the value we return always has the same type
4635 as the LHS argument. */
4637 if (olhstype == TREE_TYPE (result))
4638 return result;
4640 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4641 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4642 protected_set_expr_location (result, location);
4643 return result;
4646 /* Convert value RHS to type TYPE as preparation for an assignment to
4647 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4648 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4649 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4650 constant before any folding.
4651 The real work of conversion is done by `convert'.
4652 The purpose of this function is to generate error messages
4653 for assignments that are not allowed in C.
4654 ERRTYPE says whether it is argument passing, assignment,
4655 initialization or return.
4657 LOCATION is the location of the RHS.
4658 FUNCTION is a tree for the function being called.
4659 PARMNUM is the number of the argument, for printing in error messages. */
4661 static tree
4662 convert_for_assignment (location_t location, tree type, tree rhs,
4663 tree origtype, enum impl_conv errtype,
4664 bool null_pointer_constant, tree fundecl,
4665 tree function, int parmnum)
4667 enum tree_code codel = TREE_CODE (type);
4668 tree orig_rhs = rhs;
4669 tree rhstype;
4670 enum tree_code coder;
4671 tree rname = NULL_TREE;
4672 bool objc_ok = false;
4674 if (errtype == ic_argpass)
4676 tree selector;
4677 /* Change pointer to function to the function itself for
4678 diagnostics. */
4679 if (TREE_CODE (function) == ADDR_EXPR
4680 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4681 function = TREE_OPERAND (function, 0);
4683 /* Handle an ObjC selector specially for diagnostics. */
4684 selector = objc_message_selector ();
4685 rname = function;
4686 if (selector && parmnum > 2)
4688 rname = selector;
4689 parmnum -= 2;
4693 /* This macro is used to emit diagnostics to ensure that all format
4694 strings are complete sentences, visible to gettext and checked at
4695 compile time. */
4696 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4697 do { \
4698 switch (errtype) \
4700 case ic_argpass: \
4701 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4702 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4703 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4704 "expected %qT but argument is of type %qT", \
4705 type, rhstype); \
4706 break; \
4707 case ic_assign: \
4708 pedwarn (LOCATION, OPT, AS); \
4709 break; \
4710 case ic_init: \
4711 pedwarn (LOCATION, OPT, IN); \
4712 break; \
4713 case ic_return: \
4714 pedwarn (LOCATION, OPT, RE); \
4715 break; \
4716 default: \
4717 gcc_unreachable (); \
4719 } while (0)
4721 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4722 rhs = TREE_OPERAND (rhs, 0);
4724 rhstype = TREE_TYPE (rhs);
4725 coder = TREE_CODE (rhstype);
4727 if (coder == ERROR_MARK)
4728 return error_mark_node;
4730 if (c_dialect_objc ())
4732 int parmno;
4734 switch (errtype)
4736 case ic_return:
4737 parmno = 0;
4738 break;
4740 case ic_assign:
4741 parmno = -1;
4742 break;
4744 case ic_init:
4745 parmno = -2;
4746 break;
4748 default:
4749 parmno = parmnum;
4750 break;
4753 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4756 if (warn_cxx_compat)
4758 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4759 if (checktype != error_mark_node
4760 && TREE_CODE (type) == ENUMERAL_TYPE
4761 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4763 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4764 G_("enum conversion when passing argument "
4765 "%d of %qE is invalid in C++"),
4766 G_("enum conversion in assignment is "
4767 "invalid in C++"),
4768 G_("enum conversion in initialization is "
4769 "invalid in C++"),
4770 G_("enum conversion in return is "
4771 "invalid in C++"));
4775 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4776 return rhs;
4778 if (coder == VOID_TYPE)
4780 /* Except for passing an argument to an unprototyped function,
4781 this is a constraint violation. When passing an argument to
4782 an unprototyped function, it is compile-time undefined;
4783 making it a constraint in that case was rejected in
4784 DR#252. */
4785 error_at (location, "void value not ignored as it ought to be");
4786 return error_mark_node;
4788 rhs = require_complete_type (rhs);
4789 if (rhs == error_mark_node)
4790 return error_mark_node;
4791 /* A type converts to a reference to it.
4792 This code doesn't fully support references, it's just for the
4793 special case of va_start and va_copy. */
4794 if (codel == REFERENCE_TYPE
4795 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4797 if (!lvalue_p (rhs))
4799 error_at (location, "cannot pass rvalue to reference parameter");
4800 return error_mark_node;
4802 if (!c_mark_addressable (rhs))
4803 return error_mark_node;
4804 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4805 SET_EXPR_LOCATION (rhs, location);
4807 /* We already know that these two types are compatible, but they
4808 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4809 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4810 likely to be va_list, a typedef to __builtin_va_list, which
4811 is different enough that it will cause problems later. */
4812 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4814 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4815 SET_EXPR_LOCATION (rhs, location);
4818 rhs = build1 (NOP_EXPR, type, rhs);
4819 SET_EXPR_LOCATION (rhs, location);
4820 return rhs;
4822 /* Some types can interconvert without explicit casts. */
4823 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4824 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4825 return convert (type, rhs);
4826 /* Arithmetic types all interconvert, and enum is treated like int. */
4827 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4828 || codel == FIXED_POINT_TYPE
4829 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4830 || codel == BOOLEAN_TYPE)
4831 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4832 || coder == FIXED_POINT_TYPE
4833 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4834 || coder == BOOLEAN_TYPE))
4836 tree ret;
4837 bool save = in_late_binary_op;
4838 if (codel == BOOLEAN_TYPE)
4839 in_late_binary_op = true;
4840 ret = convert_and_check (type, orig_rhs);
4841 if (codel == BOOLEAN_TYPE)
4842 in_late_binary_op = save;
4843 return ret;
4846 /* Aggregates in different TUs might need conversion. */
4847 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4848 && codel == coder
4849 && comptypes (type, rhstype))
4850 return convert_and_check (type, rhs);
4852 /* Conversion to a transparent union from its member types.
4853 This applies only to function arguments. */
4854 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4855 && errtype == ic_argpass)
4857 tree memb, marginal_memb = NULL_TREE;
4859 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4861 tree memb_type = TREE_TYPE (memb);
4863 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4864 TYPE_MAIN_VARIANT (rhstype)))
4865 break;
4867 if (TREE_CODE (memb_type) != POINTER_TYPE)
4868 continue;
4870 if (coder == POINTER_TYPE)
4872 tree ttl = TREE_TYPE (memb_type);
4873 tree ttr = TREE_TYPE (rhstype);
4875 /* Any non-function converts to a [const][volatile] void *
4876 and vice versa; otherwise, targets must be the same.
4877 Meanwhile, the lhs target must have all the qualifiers of
4878 the rhs. */
4879 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4880 || comp_target_types (location, memb_type, rhstype))
4882 /* If this type won't generate any warnings, use it. */
4883 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4884 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4885 && TREE_CODE (ttl) == FUNCTION_TYPE)
4886 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4887 == TYPE_QUALS (ttr))
4888 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4889 == TYPE_QUALS (ttl))))
4890 break;
4892 /* Keep looking for a better type, but remember this one. */
4893 if (!marginal_memb)
4894 marginal_memb = memb;
4898 /* Can convert integer zero to any pointer type. */
4899 if (null_pointer_constant)
4901 rhs = null_pointer_node;
4902 break;
4906 if (memb || marginal_memb)
4908 if (!memb)
4910 /* We have only a marginally acceptable member type;
4911 it needs a warning. */
4912 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4913 tree ttr = TREE_TYPE (rhstype);
4915 /* Const and volatile mean something different for function
4916 types, so the usual warnings are not appropriate. */
4917 if (TREE_CODE (ttr) == FUNCTION_TYPE
4918 && TREE_CODE (ttl) == FUNCTION_TYPE)
4920 /* Because const and volatile on functions are
4921 restrictions that say the function will not do
4922 certain things, it is okay to use a const or volatile
4923 function where an ordinary one is wanted, but not
4924 vice-versa. */
4925 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4926 WARN_FOR_ASSIGNMENT (location, 0,
4927 G_("passing argument %d of %qE "
4928 "makes qualified function "
4929 "pointer from unqualified"),
4930 G_("assignment makes qualified "
4931 "function pointer from "
4932 "unqualified"),
4933 G_("initialization makes qualified "
4934 "function pointer from "
4935 "unqualified"),
4936 G_("return makes qualified function "
4937 "pointer from unqualified"));
4939 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4940 WARN_FOR_ASSIGNMENT (location, 0,
4941 G_("passing argument %d of %qE discards "
4942 "qualifiers from pointer target type"),
4943 G_("assignment discards qualifiers "
4944 "from pointer target type"),
4945 G_("initialization discards qualifiers "
4946 "from pointer target type"),
4947 G_("return discards qualifiers from "
4948 "pointer target type"));
4950 memb = marginal_memb;
4953 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4954 pedwarn (location, OPT_pedantic,
4955 "ISO C prohibits argument conversion to union type");
4957 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
4958 return build_constructor_single (type, memb, rhs);
4962 /* Conversions among pointers */
4963 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4964 && (coder == codel))
4966 tree ttl = TREE_TYPE (type);
4967 tree ttr = TREE_TYPE (rhstype);
4968 tree mvl = ttl;
4969 tree mvr = ttr;
4970 bool is_opaque_pointer;
4971 int target_cmp = 0; /* Cache comp_target_types () result. */
4973 if (TREE_CODE (mvl) != ARRAY_TYPE)
4974 mvl = TYPE_MAIN_VARIANT (mvl);
4975 if (TREE_CODE (mvr) != ARRAY_TYPE)
4976 mvr = TYPE_MAIN_VARIANT (mvr);
4977 /* Opaque pointers are treated like void pointers. */
4978 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4980 /* C++ does not allow the implicit conversion void* -> T*. However,
4981 for the purpose of reducing the number of false positives, we
4982 tolerate the special case of
4984 int *p = NULL;
4986 where NULL is typically defined in C to be '(void *) 0'. */
4987 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4988 warning_at (location, OPT_Wc___compat,
4989 "request for implicit conversion "
4990 "from %qT to %qT not permitted in C++", rhstype, type);
4992 /* Check if the right-hand side has a format attribute but the
4993 left-hand side doesn't. */
4994 if (warn_missing_format_attribute
4995 && check_missing_format_attribute (type, rhstype))
4997 switch (errtype)
4999 case ic_argpass:
5000 warning_at (location, OPT_Wmissing_format_attribute,
5001 "argument %d of %qE might be "
5002 "a candidate for a format attribute",
5003 parmnum, rname);
5004 break;
5005 case ic_assign:
5006 warning_at (location, OPT_Wmissing_format_attribute,
5007 "assignment left-hand side might be "
5008 "a candidate for a format attribute");
5009 break;
5010 case ic_init:
5011 warning_at (location, OPT_Wmissing_format_attribute,
5012 "initialization left-hand side might be "
5013 "a candidate for a format attribute");
5014 break;
5015 case ic_return:
5016 warning_at (location, OPT_Wmissing_format_attribute,
5017 "return type might be "
5018 "a candidate for a format attribute");
5019 break;
5020 default:
5021 gcc_unreachable ();
5025 /* Any non-function converts to a [const][volatile] void *
5026 and vice versa; otherwise, targets must be the same.
5027 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5028 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5029 || (target_cmp = comp_target_types (location, type, rhstype))
5030 || is_opaque_pointer
5031 || (c_common_unsigned_type (mvl)
5032 == c_common_unsigned_type (mvr)))
5034 if (pedantic
5035 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5037 (VOID_TYPE_P (ttr)
5038 && !null_pointer_constant
5039 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5040 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5041 G_("ISO C forbids passing argument %d of "
5042 "%qE between function pointer "
5043 "and %<void *%>"),
5044 G_("ISO C forbids assignment between "
5045 "function pointer and %<void *%>"),
5046 G_("ISO C forbids initialization between "
5047 "function pointer and %<void *%>"),
5048 G_("ISO C forbids return between function "
5049 "pointer and %<void *%>"));
5050 /* Const and volatile mean something different for function types,
5051 so the usual warnings are not appropriate. */
5052 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5053 && TREE_CODE (ttl) != FUNCTION_TYPE)
5055 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
5057 /* Types differing only by the presence of the 'volatile'
5058 qualifier are acceptable if the 'volatile' has been added
5059 in by the Objective-C EH machinery. */
5060 if (!objc_type_quals_match (ttl, ttr))
5061 WARN_FOR_ASSIGNMENT (location, 0,
5062 G_("passing argument %d of %qE discards "
5063 "qualifiers from pointer target type"),
5064 G_("assignment discards qualifiers "
5065 "from pointer target type"),
5066 G_("initialization discards qualifiers "
5067 "from pointer target type"),
5068 G_("return discards qualifiers from "
5069 "pointer target type"));
5071 /* If this is not a case of ignoring a mismatch in signedness,
5072 no warning. */
5073 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5074 || target_cmp)
5076 /* If there is a mismatch, do warn. */
5077 else if (warn_pointer_sign)
5078 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5079 G_("pointer targets in passing argument "
5080 "%d of %qE differ in signedness"),
5081 G_("pointer targets in assignment "
5082 "differ in signedness"),
5083 G_("pointer targets in initialization "
5084 "differ in signedness"),
5085 G_("pointer targets in return differ "
5086 "in signedness"));
5088 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5089 && TREE_CODE (ttr) == FUNCTION_TYPE)
5091 /* Because const and volatile on functions are restrictions
5092 that say the function will not do certain things,
5093 it is okay to use a const or volatile function
5094 where an ordinary one is wanted, but not vice-versa. */
5095 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
5096 WARN_FOR_ASSIGNMENT (location, 0,
5097 G_("passing argument %d of %qE makes "
5098 "qualified function pointer "
5099 "from unqualified"),
5100 G_("assignment makes qualified function "
5101 "pointer from unqualified"),
5102 G_("initialization makes qualified "
5103 "function pointer from unqualified"),
5104 G_("return makes qualified function "
5105 "pointer from unqualified"));
5108 else
5109 /* Avoid warning about the volatile ObjC EH puts on decls. */
5110 if (!objc_ok)
5111 WARN_FOR_ASSIGNMENT (location, 0,
5112 G_("passing argument %d of %qE from "
5113 "incompatible pointer type"),
5114 G_("assignment from incompatible pointer type"),
5115 G_("initialization from incompatible "
5116 "pointer type"),
5117 G_("return from incompatible pointer type"));
5119 return convert (type, rhs);
5121 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5123 /* ??? This should not be an error when inlining calls to
5124 unprototyped functions. */
5125 error_at (location, "invalid use of non-lvalue array");
5126 return error_mark_node;
5128 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5130 /* An explicit constant 0 can convert to a pointer,
5131 or one that results from arithmetic, even including
5132 a cast to integer type. */
5133 if (!null_pointer_constant)
5134 WARN_FOR_ASSIGNMENT (location, 0,
5135 G_("passing argument %d of %qE makes "
5136 "pointer from integer without a cast"),
5137 G_("assignment makes pointer from integer "
5138 "without a cast"),
5139 G_("initialization makes pointer from "
5140 "integer without a cast"),
5141 G_("return makes pointer from integer "
5142 "without a cast"));
5144 return convert (type, rhs);
5146 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5148 WARN_FOR_ASSIGNMENT (location, 0,
5149 G_("passing argument %d of %qE makes integer "
5150 "from pointer without a cast"),
5151 G_("assignment makes integer from pointer "
5152 "without a cast"),
5153 G_("initialization makes integer from pointer "
5154 "without a cast"),
5155 G_("return makes integer from pointer "
5156 "without a cast"));
5157 return convert (type, rhs);
5159 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5161 tree ret;
5162 bool save = in_late_binary_op;
5163 in_late_binary_op = true;
5164 ret = convert (type, rhs);
5165 in_late_binary_op = save;
5166 return ret;
5169 switch (errtype)
5171 case ic_argpass:
5172 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5173 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5174 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5175 "expected %qT but argument is of type %qT", type, rhstype);
5176 break;
5177 case ic_assign:
5178 error_at (location, "incompatible types when assigning to type %qT from "
5179 "type %qT", type, rhstype);
5180 break;
5181 case ic_init:
5182 error_at (location,
5183 "incompatible types when initializing type %qT using type %qT",
5184 type, rhstype);
5185 break;
5186 case ic_return:
5187 error_at (location,
5188 "incompatible types when returning type %qT but %qT was "
5189 "expected", rhstype, type);
5190 break;
5191 default:
5192 gcc_unreachable ();
5195 return error_mark_node;
5198 /* If VALUE is a compound expr all of whose expressions are constant, then
5199 return its value. Otherwise, return error_mark_node.
5201 This is for handling COMPOUND_EXPRs as initializer elements
5202 which is allowed with a warning when -pedantic is specified. */
5204 static tree
5205 valid_compound_expr_initializer (tree value, tree endtype)
5207 if (TREE_CODE (value) == COMPOUND_EXPR)
5209 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5210 == error_mark_node)
5211 return error_mark_node;
5212 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5213 endtype);
5215 else if (!initializer_constant_valid_p (value, endtype))
5216 return error_mark_node;
5217 else
5218 return value;
5221 /* Perform appropriate conversions on the initial value of a variable,
5222 store it in the declaration DECL,
5223 and print any error messages that are appropriate.
5224 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5225 If the init is invalid, store an ERROR_MARK.
5227 INIT_LOC is the location of the initial value. */
5229 void
5230 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5232 tree value, type;
5233 bool npc = false;
5235 /* If variable's type was invalidly declared, just ignore it. */
5237 type = TREE_TYPE (decl);
5238 if (TREE_CODE (type) == ERROR_MARK)
5239 return;
5241 /* Digest the specified initializer into an expression. */
5243 if (init)
5244 npc = null_pointer_constant_p (init);
5245 value = digest_init (init_loc, type, init, origtype, npc,
5246 true, TREE_STATIC (decl));
5248 /* Store the expression if valid; else report error. */
5250 if (!in_system_header
5251 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5252 warning (OPT_Wtraditional, "traditional C rejects automatic "
5253 "aggregate initialization");
5255 DECL_INITIAL (decl) = value;
5257 /* ANSI wants warnings about out-of-range constant initializers. */
5258 STRIP_TYPE_NOPS (value);
5259 if (TREE_STATIC (decl))
5260 constant_expression_warning (value);
5262 /* Check if we need to set array size from compound literal size. */
5263 if (TREE_CODE (type) == ARRAY_TYPE
5264 && TYPE_DOMAIN (type) == 0
5265 && value != error_mark_node)
5267 tree inside_init = init;
5269 STRIP_TYPE_NOPS (inside_init);
5270 inside_init = fold (inside_init);
5272 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5274 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5276 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5278 /* For int foo[] = (int [3]){1}; we need to set array size
5279 now since later on array initializer will be just the
5280 brace enclosed list of the compound literal. */
5281 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5282 TREE_TYPE (decl) = type;
5283 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5284 layout_type (type);
5285 layout_decl (cldecl, 0);
5291 /* Methods for storing and printing names for error messages. */
5293 /* Implement a spelling stack that allows components of a name to be pushed
5294 and popped. Each element on the stack is this structure. */
5296 struct spelling
5298 int kind;
5299 union
5301 unsigned HOST_WIDE_INT i;
5302 const char *s;
5303 } u;
5306 #define SPELLING_STRING 1
5307 #define SPELLING_MEMBER 2
5308 #define SPELLING_BOUNDS 3
5310 static struct spelling *spelling; /* Next stack element (unused). */
5311 static struct spelling *spelling_base; /* Spelling stack base. */
5312 static int spelling_size; /* Size of the spelling stack. */
5314 /* Macros to save and restore the spelling stack around push_... functions.
5315 Alternative to SAVE_SPELLING_STACK. */
5317 #define SPELLING_DEPTH() (spelling - spelling_base)
5318 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5320 /* Push an element on the spelling stack with type KIND and assign VALUE
5321 to MEMBER. */
5323 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5325 int depth = SPELLING_DEPTH (); \
5327 if (depth >= spelling_size) \
5329 spelling_size += 10; \
5330 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5331 spelling_size); \
5332 RESTORE_SPELLING_DEPTH (depth); \
5335 spelling->kind = (KIND); \
5336 spelling->MEMBER = (VALUE); \
5337 spelling++; \
5340 /* Push STRING on the stack. Printed literally. */
5342 static void
5343 push_string (const char *string)
5345 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5348 /* Push a member name on the stack. Printed as '.' STRING. */
5350 static void
5351 push_member_name (tree decl)
5353 const char *const string
5354 = (DECL_NAME (decl)
5355 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5356 : _("<anonymous>"));
5357 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5360 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5362 static void
5363 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5365 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5368 /* Compute the maximum size in bytes of the printed spelling. */
5370 static int
5371 spelling_length (void)
5373 int size = 0;
5374 struct spelling *p;
5376 for (p = spelling_base; p < spelling; p++)
5378 if (p->kind == SPELLING_BOUNDS)
5379 size += 25;
5380 else
5381 size += strlen (p->u.s) + 1;
5384 return size;
5387 /* Print the spelling to BUFFER and return it. */
5389 static char *
5390 print_spelling (char *buffer)
5392 char *d = buffer;
5393 struct spelling *p;
5395 for (p = spelling_base; p < spelling; p++)
5396 if (p->kind == SPELLING_BOUNDS)
5398 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5399 d += strlen (d);
5401 else
5403 const char *s;
5404 if (p->kind == SPELLING_MEMBER)
5405 *d++ = '.';
5406 for (s = p->u.s; (*d = *s++); d++)
5409 *d++ = '\0';
5410 return buffer;
5413 /* Issue an error message for a bad initializer component.
5414 MSGID identifies the message.
5415 The component name is taken from the spelling stack. */
5417 void
5418 error_init (const char *msgid)
5420 char *ofwhat;
5422 error ("%s", _(msgid));
5423 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5424 if (*ofwhat)
5425 error ("(near initialization for %qs)", ofwhat);
5428 /* Issue a pedantic warning for a bad initializer component. OPT is
5429 the option OPT_* (from options.h) controlling this warning or 0 if
5430 it is unconditionally given. MSGID identifies the message. The
5431 component name is taken from the spelling stack. */
5433 void
5434 pedwarn_init (location_t location, int opt, const char *msgid)
5436 char *ofwhat;
5438 pedwarn (location, opt, "%s", _(msgid));
5439 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5440 if (*ofwhat)
5441 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5444 /* Issue a warning for a bad initializer component.
5446 OPT is the OPT_W* value corresponding to the warning option that
5447 controls this warning. MSGID identifies the message. The
5448 component name is taken from the spelling stack. */
5450 static void
5451 warning_init (int opt, const char *msgid)
5453 char *ofwhat;
5455 warning (opt, "%s", _(msgid));
5456 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5457 if (*ofwhat)
5458 warning (opt, "(near initialization for %qs)", ofwhat);
5461 /* If TYPE is an array type and EXPR is a parenthesized string
5462 constant, warn if pedantic that EXPR is being used to initialize an
5463 object of type TYPE. */
5465 void
5466 maybe_warn_string_init (tree type, struct c_expr expr)
5468 if (pedantic
5469 && TREE_CODE (type) == ARRAY_TYPE
5470 && TREE_CODE (expr.value) == STRING_CST
5471 && expr.original_code != STRING_CST)
5472 pedwarn_init (input_location, OPT_pedantic,
5473 "array initialized from parenthesized string constant");
5476 /* Digest the parser output INIT as an initializer for type TYPE.
5477 Return a C expression of type TYPE to represent the initial value.
5479 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5481 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5483 If INIT is a string constant, STRICT_STRING is true if it is
5484 unparenthesized or we should not warn here for it being parenthesized.
5485 For other types of INIT, STRICT_STRING is not used.
5487 INIT_LOC is the location of the INIT.
5489 REQUIRE_CONSTANT requests an error if non-constant initializers or
5490 elements are seen. */
5492 static tree
5493 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5494 bool null_pointer_constant, bool strict_string,
5495 int require_constant)
5497 enum tree_code code = TREE_CODE (type);
5498 tree inside_init = init;
5499 tree semantic_type = NULL_TREE;
5500 bool maybe_const = true;
5502 if (type == error_mark_node
5503 || !init
5504 || init == error_mark_node
5505 || TREE_TYPE (init) == error_mark_node)
5506 return error_mark_node;
5508 STRIP_TYPE_NOPS (inside_init);
5510 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5512 semantic_type = TREE_TYPE (inside_init);
5513 inside_init = TREE_OPERAND (inside_init, 0);
5515 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5516 inside_init = decl_constant_value_for_optimization (inside_init);
5518 /* Initialization of an array of chars from a string constant
5519 optionally enclosed in braces. */
5521 if (code == ARRAY_TYPE && inside_init
5522 && TREE_CODE (inside_init) == STRING_CST)
5524 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5525 /* Note that an array could be both an array of character type
5526 and an array of wchar_t if wchar_t is signed char or unsigned
5527 char. */
5528 bool char_array = (typ1 == char_type_node
5529 || typ1 == signed_char_type_node
5530 || typ1 == unsigned_char_type_node);
5531 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5532 bool char16_array = !!comptypes (typ1, char16_type_node);
5533 bool char32_array = !!comptypes (typ1, char32_type_node);
5535 if (char_array || wchar_array || char16_array || char32_array)
5537 struct c_expr expr;
5538 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5539 expr.value = inside_init;
5540 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5541 expr.original_type = NULL;
5542 maybe_warn_string_init (type, expr);
5544 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5545 pedwarn_init (init_loc, OPT_pedantic,
5546 "initialization of a flexible array member");
5548 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5549 TYPE_MAIN_VARIANT (type)))
5550 return inside_init;
5552 if (char_array)
5554 if (typ2 != char_type_node)
5556 error_init ("char-array initialized from wide string");
5557 return error_mark_node;
5560 else
5562 if (typ2 == char_type_node)
5564 error_init ("wide character array initialized from non-wide "
5565 "string");
5566 return error_mark_node;
5568 else if (!comptypes(typ1, typ2))
5570 error_init ("wide character array initialized from "
5571 "incompatible wide string");
5572 return error_mark_node;
5576 TREE_TYPE (inside_init) = type;
5577 if (TYPE_DOMAIN (type) != 0
5578 && TYPE_SIZE (type) != 0
5579 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5581 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5583 /* Subtract the size of a single (possibly wide) character
5584 because it's ok to ignore the terminating null char
5585 that is counted in the length of the constant. */
5586 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5587 (len
5588 - (TYPE_PRECISION (typ1)
5589 / BITS_PER_UNIT))))
5590 pedwarn_init (init_loc, 0,
5591 ("initializer-string for array of chars "
5592 "is too long"));
5593 else if (warn_cxx_compat
5594 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5595 warning_at (init_loc, OPT_Wc___compat,
5596 ("initializer-string for array chars "
5597 "is too long for C++"));
5600 return inside_init;
5602 else if (INTEGRAL_TYPE_P (typ1))
5604 error_init ("array of inappropriate type initialized "
5605 "from string constant");
5606 return error_mark_node;
5610 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5611 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5612 below and handle as a constructor. */
5613 if (code == VECTOR_TYPE
5614 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5615 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5616 && TREE_CONSTANT (inside_init))
5618 if (TREE_CODE (inside_init) == VECTOR_CST
5619 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5620 TYPE_MAIN_VARIANT (type)))
5621 return inside_init;
5623 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5625 unsigned HOST_WIDE_INT ix;
5626 tree value;
5627 bool constant_p = true;
5629 /* Iterate through elements and check if all constructor
5630 elements are *_CSTs. */
5631 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5632 if (!CONSTANT_CLASS_P (value))
5634 constant_p = false;
5635 break;
5638 if (constant_p)
5639 return build_vector_from_ctor (type,
5640 CONSTRUCTOR_ELTS (inside_init));
5644 if (warn_sequence_point)
5645 verify_sequence_points (inside_init);
5647 /* Any type can be initialized
5648 from an expression of the same type, optionally with braces. */
5650 if (inside_init && TREE_TYPE (inside_init) != 0
5651 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5652 TYPE_MAIN_VARIANT (type))
5653 || (code == ARRAY_TYPE
5654 && comptypes (TREE_TYPE (inside_init), type))
5655 || (code == VECTOR_TYPE
5656 && comptypes (TREE_TYPE (inside_init), type))
5657 || (code == POINTER_TYPE
5658 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5659 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5660 TREE_TYPE (type)))))
5662 if (code == POINTER_TYPE)
5664 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5666 if (TREE_CODE (inside_init) == STRING_CST
5667 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5668 inside_init = array_to_pointer_conversion
5669 (init_loc, inside_init);
5670 else
5672 error_init ("invalid use of non-lvalue array");
5673 return error_mark_node;
5678 if (code == VECTOR_TYPE)
5679 /* Although the types are compatible, we may require a
5680 conversion. */
5681 inside_init = convert (type, inside_init);
5683 if (require_constant
5684 && (code == VECTOR_TYPE || !flag_isoc99)
5685 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5687 /* As an extension, allow initializing objects with static storage
5688 duration with compound literals (which are then treated just as
5689 the brace enclosed list they contain). Also allow this for
5690 vectors, as we can only assign them with compound literals. */
5691 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5692 inside_init = DECL_INITIAL (decl);
5695 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5696 && TREE_CODE (inside_init) != CONSTRUCTOR)
5698 error_init ("array initialized from non-constant array expression");
5699 return error_mark_node;
5702 /* Compound expressions can only occur here if -pedantic or
5703 -pedantic-errors is specified. In the later case, we always want
5704 an error. In the former case, we simply want a warning. */
5705 if (require_constant && pedantic
5706 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5708 inside_init
5709 = valid_compound_expr_initializer (inside_init,
5710 TREE_TYPE (inside_init));
5711 if (inside_init == error_mark_node)
5712 error_init ("initializer element is not constant");
5713 else
5714 pedwarn_init (init_loc, OPT_pedantic,
5715 "initializer element is not constant");
5716 if (flag_pedantic_errors)
5717 inside_init = error_mark_node;
5719 else if (require_constant
5720 && !initializer_constant_valid_p (inside_init,
5721 TREE_TYPE (inside_init)))
5723 error_init ("initializer element is not constant");
5724 inside_init = error_mark_node;
5726 else if (require_constant && !maybe_const)
5727 pedwarn_init (init_loc, 0,
5728 "initializer element is not a constant expression");
5730 /* Added to enable additional -Wmissing-format-attribute warnings. */
5731 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5732 inside_init = convert_for_assignment (init_loc, type, inside_init,
5733 origtype,
5734 ic_init, null_pointer_constant,
5735 NULL_TREE, NULL_TREE, 0);
5736 return inside_init;
5739 /* Handle scalar types, including conversions. */
5741 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5742 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5743 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5745 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5746 && (TREE_CODE (init) == STRING_CST
5747 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5748 inside_init = init = array_to_pointer_conversion (init_loc, init);
5749 if (semantic_type)
5750 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5751 inside_init);
5752 inside_init
5753 = convert_for_assignment (init_loc, type, inside_init, origtype,
5754 ic_init, null_pointer_constant,
5755 NULL_TREE, NULL_TREE, 0);
5757 /* Check to see if we have already given an error message. */
5758 if (inside_init == error_mark_node)
5760 else if (require_constant && !TREE_CONSTANT (inside_init))
5762 error_init ("initializer element is not constant");
5763 inside_init = error_mark_node;
5765 else if (require_constant
5766 && !initializer_constant_valid_p (inside_init,
5767 TREE_TYPE (inside_init)))
5769 error_init ("initializer element is not computable at load time");
5770 inside_init = error_mark_node;
5772 else if (require_constant && !maybe_const)
5773 pedwarn_init (init_loc, 0,
5774 "initializer element is not a constant expression");
5776 return inside_init;
5779 /* Come here only for records and arrays. */
5781 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5783 error_init ("variable-sized object may not be initialized");
5784 return error_mark_node;
5787 error_init ("invalid initializer");
5788 return error_mark_node;
5791 /* Handle initializers that use braces. */
5793 /* Type of object we are accumulating a constructor for.
5794 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5795 static tree constructor_type;
5797 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5798 left to fill. */
5799 static tree constructor_fields;
5801 /* For an ARRAY_TYPE, this is the specified index
5802 at which to store the next element we get. */
5803 static tree constructor_index;
5805 /* For an ARRAY_TYPE, this is the maximum index. */
5806 static tree constructor_max_index;
5808 /* For a RECORD_TYPE, this is the first field not yet written out. */
5809 static tree constructor_unfilled_fields;
5811 /* For an ARRAY_TYPE, this is the index of the first element
5812 not yet written out. */
5813 static tree constructor_unfilled_index;
5815 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5816 This is so we can generate gaps between fields, when appropriate. */
5817 static tree constructor_bit_index;
5819 /* If we are saving up the elements rather than allocating them,
5820 this is the list of elements so far (in reverse order,
5821 most recent first). */
5822 static VEC(constructor_elt,gc) *constructor_elements;
5824 /* 1 if constructor should be incrementally stored into a constructor chain,
5825 0 if all the elements should be kept in AVL tree. */
5826 static int constructor_incremental;
5828 /* 1 if so far this constructor's elements are all compile-time constants. */
5829 static int constructor_constant;
5831 /* 1 if so far this constructor's elements are all valid address constants. */
5832 static int constructor_simple;
5834 /* 1 if this constructor has an element that cannot be part of a
5835 constant expression. */
5836 static int constructor_nonconst;
5838 /* 1 if this constructor is erroneous so far. */
5839 static int constructor_erroneous;
5841 /* Structure for managing pending initializer elements, organized as an
5842 AVL tree. */
5844 struct init_node
5846 struct init_node *left, *right;
5847 struct init_node *parent;
5848 int balance;
5849 tree purpose;
5850 tree value;
5851 tree origtype;
5854 /* Tree of pending elements at this constructor level.
5855 These are elements encountered out of order
5856 which belong at places we haven't reached yet in actually
5857 writing the output.
5858 Will never hold tree nodes across GC runs. */
5859 static struct init_node *constructor_pending_elts;
5861 /* The SPELLING_DEPTH of this constructor. */
5862 static int constructor_depth;
5864 /* DECL node for which an initializer is being read.
5865 0 means we are reading a constructor expression
5866 such as (struct foo) {...}. */
5867 static tree constructor_decl;
5869 /* Nonzero if this is an initializer for a top-level decl. */
5870 static int constructor_top_level;
5872 /* Nonzero if there were any member designators in this initializer. */
5873 static int constructor_designated;
5875 /* Nesting depth of designator list. */
5876 static int designator_depth;
5878 /* Nonzero if there were diagnosed errors in this designator list. */
5879 static int designator_erroneous;
5882 /* This stack has a level for each implicit or explicit level of
5883 structuring in the initializer, including the outermost one. It
5884 saves the values of most of the variables above. */
5886 struct constructor_range_stack;
5888 struct constructor_stack
5890 struct constructor_stack *next;
5891 tree type;
5892 tree fields;
5893 tree index;
5894 tree max_index;
5895 tree unfilled_index;
5896 tree unfilled_fields;
5897 tree bit_index;
5898 VEC(constructor_elt,gc) *elements;
5899 struct init_node *pending_elts;
5900 int offset;
5901 int depth;
5902 /* If value nonzero, this value should replace the entire
5903 constructor at this level. */
5904 struct c_expr replacement_value;
5905 struct constructor_range_stack *range_stack;
5906 char constant;
5907 char simple;
5908 char nonconst;
5909 char implicit;
5910 char erroneous;
5911 char outer;
5912 char incremental;
5913 char designated;
5916 static struct constructor_stack *constructor_stack;
5918 /* This stack represents designators from some range designator up to
5919 the last designator in the list. */
5921 struct constructor_range_stack
5923 struct constructor_range_stack *next, *prev;
5924 struct constructor_stack *stack;
5925 tree range_start;
5926 tree index;
5927 tree range_end;
5928 tree fields;
5931 static struct constructor_range_stack *constructor_range_stack;
5933 /* This stack records separate initializers that are nested.
5934 Nested initializers can't happen in ANSI C, but GNU C allows them
5935 in cases like { ... (struct foo) { ... } ... }. */
5937 struct initializer_stack
5939 struct initializer_stack *next;
5940 tree decl;
5941 struct constructor_stack *constructor_stack;
5942 struct constructor_range_stack *constructor_range_stack;
5943 VEC(constructor_elt,gc) *elements;
5944 struct spelling *spelling;
5945 struct spelling *spelling_base;
5946 int spelling_size;
5947 char top_level;
5948 char require_constant_value;
5949 char require_constant_elements;
5952 static struct initializer_stack *initializer_stack;
5954 /* Prepare to parse and output the initializer for variable DECL. */
5956 void
5957 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5959 const char *locus;
5960 struct initializer_stack *p = XNEW (struct initializer_stack);
5962 p->decl = constructor_decl;
5963 p->require_constant_value = require_constant_value;
5964 p->require_constant_elements = require_constant_elements;
5965 p->constructor_stack = constructor_stack;
5966 p->constructor_range_stack = constructor_range_stack;
5967 p->elements = constructor_elements;
5968 p->spelling = spelling;
5969 p->spelling_base = spelling_base;
5970 p->spelling_size = spelling_size;
5971 p->top_level = constructor_top_level;
5972 p->next = initializer_stack;
5973 initializer_stack = p;
5975 constructor_decl = decl;
5976 constructor_designated = 0;
5977 constructor_top_level = top_level;
5979 if (decl != 0 && decl != error_mark_node)
5981 require_constant_value = TREE_STATIC (decl);
5982 require_constant_elements
5983 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5984 /* For a scalar, you can always use any value to initialize,
5985 even within braces. */
5986 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5987 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5988 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5989 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5990 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
5992 else
5994 require_constant_value = 0;
5995 require_constant_elements = 0;
5996 locus = _("(anonymous)");
5999 constructor_stack = 0;
6000 constructor_range_stack = 0;
6002 missing_braces_mentioned = 0;
6004 spelling_base = 0;
6005 spelling_size = 0;
6006 RESTORE_SPELLING_DEPTH (0);
6008 if (locus)
6009 push_string (locus);
6012 void
6013 finish_init (void)
6015 struct initializer_stack *p = initializer_stack;
6017 /* Free the whole constructor stack of this initializer. */
6018 while (constructor_stack)
6020 struct constructor_stack *q = constructor_stack;
6021 constructor_stack = q->next;
6022 free (q);
6025 gcc_assert (!constructor_range_stack);
6027 /* Pop back to the data of the outer initializer (if any). */
6028 free (spelling_base);
6030 constructor_decl = p->decl;
6031 require_constant_value = p->require_constant_value;
6032 require_constant_elements = p->require_constant_elements;
6033 constructor_stack = p->constructor_stack;
6034 constructor_range_stack = p->constructor_range_stack;
6035 constructor_elements = p->elements;
6036 spelling = p->spelling;
6037 spelling_base = p->spelling_base;
6038 spelling_size = p->spelling_size;
6039 constructor_top_level = p->top_level;
6040 initializer_stack = p->next;
6041 free (p);
6044 /* Call here when we see the initializer is surrounded by braces.
6045 This is instead of a call to push_init_level;
6046 it is matched by a call to pop_init_level.
6048 TYPE is the type to initialize, for a constructor expression.
6049 For an initializer for a decl, TYPE is zero. */
6051 void
6052 really_start_incremental_init (tree type)
6054 struct constructor_stack *p = XNEW (struct constructor_stack);
6056 if (type == 0)
6057 type = TREE_TYPE (constructor_decl);
6059 if (TREE_CODE (type) == VECTOR_TYPE
6060 && TYPE_VECTOR_OPAQUE (type))
6061 error ("opaque vector types cannot be initialized");
6063 p->type = constructor_type;
6064 p->fields = constructor_fields;
6065 p->index = constructor_index;
6066 p->max_index = constructor_max_index;
6067 p->unfilled_index = constructor_unfilled_index;
6068 p->unfilled_fields = constructor_unfilled_fields;
6069 p->bit_index = constructor_bit_index;
6070 p->elements = constructor_elements;
6071 p->constant = constructor_constant;
6072 p->simple = constructor_simple;
6073 p->nonconst = constructor_nonconst;
6074 p->erroneous = constructor_erroneous;
6075 p->pending_elts = constructor_pending_elts;
6076 p->depth = constructor_depth;
6077 p->replacement_value.value = 0;
6078 p->replacement_value.original_code = ERROR_MARK;
6079 p->replacement_value.original_type = NULL;
6080 p->implicit = 0;
6081 p->range_stack = 0;
6082 p->outer = 0;
6083 p->incremental = constructor_incremental;
6084 p->designated = constructor_designated;
6085 p->next = 0;
6086 constructor_stack = p;
6088 constructor_constant = 1;
6089 constructor_simple = 1;
6090 constructor_nonconst = 0;
6091 constructor_depth = SPELLING_DEPTH ();
6092 constructor_elements = 0;
6093 constructor_pending_elts = 0;
6094 constructor_type = type;
6095 constructor_incremental = 1;
6096 constructor_designated = 0;
6097 designator_depth = 0;
6098 designator_erroneous = 0;
6100 if (TREE_CODE (constructor_type) == RECORD_TYPE
6101 || TREE_CODE (constructor_type) == UNION_TYPE)
6103 constructor_fields = TYPE_FIELDS (constructor_type);
6104 /* Skip any nameless bit fields at the beginning. */
6105 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6106 && DECL_NAME (constructor_fields) == 0)
6107 constructor_fields = TREE_CHAIN (constructor_fields);
6109 constructor_unfilled_fields = constructor_fields;
6110 constructor_bit_index = bitsize_zero_node;
6112 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6114 if (TYPE_DOMAIN (constructor_type))
6116 constructor_max_index
6117 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6119 /* Detect non-empty initializations of zero-length arrays. */
6120 if (constructor_max_index == NULL_TREE
6121 && TYPE_SIZE (constructor_type))
6122 constructor_max_index = build_int_cst (NULL_TREE, -1);
6124 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6125 to initialize VLAs will cause a proper error; avoid tree
6126 checking errors as well by setting a safe value. */
6127 if (constructor_max_index
6128 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6129 constructor_max_index = build_int_cst (NULL_TREE, -1);
6131 constructor_index
6132 = convert (bitsizetype,
6133 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6135 else
6137 constructor_index = bitsize_zero_node;
6138 constructor_max_index = NULL_TREE;
6141 constructor_unfilled_index = constructor_index;
6143 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6145 /* Vectors are like simple fixed-size arrays. */
6146 constructor_max_index =
6147 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6148 constructor_index = bitsize_zero_node;
6149 constructor_unfilled_index = constructor_index;
6151 else
6153 /* Handle the case of int x = {5}; */
6154 constructor_fields = constructor_type;
6155 constructor_unfilled_fields = constructor_type;
6159 /* Push down into a subobject, for initialization.
6160 If this is for an explicit set of braces, IMPLICIT is 0.
6161 If it is because the next element belongs at a lower level,
6162 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6164 void
6165 push_init_level (int implicit)
6167 struct constructor_stack *p;
6168 tree value = NULL_TREE;
6170 /* If we've exhausted any levels that didn't have braces,
6171 pop them now. If implicit == 1, this will have been done in
6172 process_init_element; do not repeat it here because in the case
6173 of excess initializers for an empty aggregate this leads to an
6174 infinite cycle of popping a level and immediately recreating
6175 it. */
6176 if (implicit != 1)
6178 while (constructor_stack->implicit)
6180 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6181 || TREE_CODE (constructor_type) == UNION_TYPE)
6182 && constructor_fields == 0)
6183 process_init_element (pop_init_level (1), true);
6184 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6185 && constructor_max_index
6186 && tree_int_cst_lt (constructor_max_index,
6187 constructor_index))
6188 process_init_element (pop_init_level (1), true);
6189 else
6190 break;
6194 /* Unless this is an explicit brace, we need to preserve previous
6195 content if any. */
6196 if (implicit)
6198 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6199 || TREE_CODE (constructor_type) == UNION_TYPE)
6200 && constructor_fields)
6201 value = find_init_member (constructor_fields);
6202 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6203 value = find_init_member (constructor_index);
6206 p = XNEW (struct constructor_stack);
6207 p->type = constructor_type;
6208 p->fields = constructor_fields;
6209 p->index = constructor_index;
6210 p->max_index = constructor_max_index;
6211 p->unfilled_index = constructor_unfilled_index;
6212 p->unfilled_fields = constructor_unfilled_fields;
6213 p->bit_index = constructor_bit_index;
6214 p->elements = constructor_elements;
6215 p->constant = constructor_constant;
6216 p->simple = constructor_simple;
6217 p->nonconst = constructor_nonconst;
6218 p->erroneous = constructor_erroneous;
6219 p->pending_elts = constructor_pending_elts;
6220 p->depth = constructor_depth;
6221 p->replacement_value.value = 0;
6222 p->replacement_value.original_code = ERROR_MARK;
6223 p->replacement_value.original_type = NULL;
6224 p->implicit = implicit;
6225 p->outer = 0;
6226 p->incremental = constructor_incremental;
6227 p->designated = constructor_designated;
6228 p->next = constructor_stack;
6229 p->range_stack = 0;
6230 constructor_stack = p;
6232 constructor_constant = 1;
6233 constructor_simple = 1;
6234 constructor_nonconst = 0;
6235 constructor_depth = SPELLING_DEPTH ();
6236 constructor_elements = 0;
6237 constructor_incremental = 1;
6238 constructor_designated = 0;
6239 constructor_pending_elts = 0;
6240 if (!implicit)
6242 p->range_stack = constructor_range_stack;
6243 constructor_range_stack = 0;
6244 designator_depth = 0;
6245 designator_erroneous = 0;
6248 /* Don't die if an entire brace-pair level is superfluous
6249 in the containing level. */
6250 if (constructor_type == 0)
6252 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6253 || TREE_CODE (constructor_type) == UNION_TYPE)
6255 /* Don't die if there are extra init elts at the end. */
6256 if (constructor_fields == 0)
6257 constructor_type = 0;
6258 else
6260 constructor_type = TREE_TYPE (constructor_fields);
6261 push_member_name (constructor_fields);
6262 constructor_depth++;
6265 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6267 constructor_type = TREE_TYPE (constructor_type);
6268 push_array_bounds (tree_low_cst (constructor_index, 1));
6269 constructor_depth++;
6272 if (constructor_type == 0)
6274 error_init ("extra brace group at end of initializer");
6275 constructor_fields = 0;
6276 constructor_unfilled_fields = 0;
6277 return;
6280 if (value && TREE_CODE (value) == CONSTRUCTOR)
6282 constructor_constant = TREE_CONSTANT (value);
6283 constructor_simple = TREE_STATIC (value);
6284 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6285 constructor_elements = CONSTRUCTOR_ELTS (value);
6286 if (!VEC_empty (constructor_elt, constructor_elements)
6287 && (TREE_CODE (constructor_type) == RECORD_TYPE
6288 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6289 set_nonincremental_init ();
6292 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6294 missing_braces_mentioned = 1;
6295 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6298 if (TREE_CODE (constructor_type) == RECORD_TYPE
6299 || TREE_CODE (constructor_type) == UNION_TYPE)
6301 constructor_fields = TYPE_FIELDS (constructor_type);
6302 /* Skip any nameless bit fields at the beginning. */
6303 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6304 && DECL_NAME (constructor_fields) == 0)
6305 constructor_fields = TREE_CHAIN (constructor_fields);
6307 constructor_unfilled_fields = constructor_fields;
6308 constructor_bit_index = bitsize_zero_node;
6310 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6312 /* Vectors are like simple fixed-size arrays. */
6313 constructor_max_index =
6314 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6315 constructor_index = convert (bitsizetype, integer_zero_node);
6316 constructor_unfilled_index = constructor_index;
6318 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6320 if (TYPE_DOMAIN (constructor_type))
6322 constructor_max_index
6323 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6325 /* Detect non-empty initializations of zero-length arrays. */
6326 if (constructor_max_index == NULL_TREE
6327 && TYPE_SIZE (constructor_type))
6328 constructor_max_index = build_int_cst (NULL_TREE, -1);
6330 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6331 to initialize VLAs will cause a proper error; avoid tree
6332 checking errors as well by setting a safe value. */
6333 if (constructor_max_index
6334 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6335 constructor_max_index = build_int_cst (NULL_TREE, -1);
6337 constructor_index
6338 = convert (bitsizetype,
6339 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6341 else
6342 constructor_index = bitsize_zero_node;
6344 constructor_unfilled_index = constructor_index;
6345 if (value && TREE_CODE (value) == STRING_CST)
6347 /* We need to split the char/wchar array into individual
6348 characters, so that we don't have to special case it
6349 everywhere. */
6350 set_nonincremental_init_from_string (value);
6353 else
6355 if (constructor_type != error_mark_node)
6356 warning_init (0, "braces around scalar initializer");
6357 constructor_fields = constructor_type;
6358 constructor_unfilled_fields = constructor_type;
6362 /* At the end of an implicit or explicit brace level,
6363 finish up that level of constructor. If a single expression
6364 with redundant braces initialized that level, return the
6365 c_expr structure for that expression. Otherwise, the original_code
6366 element is set to ERROR_MARK.
6367 If we were outputting the elements as they are read, return 0 as the value
6368 from inner levels (process_init_element ignores that),
6369 but return error_mark_node as the value from the outermost level
6370 (that's what we want to put in DECL_INITIAL).
6371 Otherwise, return a CONSTRUCTOR expression as the value. */
6373 struct c_expr
6374 pop_init_level (int implicit)
6376 struct constructor_stack *p;
6377 struct c_expr ret;
6378 ret.value = 0;
6379 ret.original_code = ERROR_MARK;
6380 ret.original_type = NULL;
6382 if (implicit == 0)
6384 /* When we come to an explicit close brace,
6385 pop any inner levels that didn't have explicit braces. */
6386 while (constructor_stack->implicit)
6387 process_init_element (pop_init_level (1), true);
6389 gcc_assert (!constructor_range_stack);
6392 /* Now output all pending elements. */
6393 constructor_incremental = 1;
6394 output_pending_init_elements (1);
6396 p = constructor_stack;
6398 /* Error for initializing a flexible array member, or a zero-length
6399 array member in an inappropriate context. */
6400 if (constructor_type && constructor_fields
6401 && TREE_CODE (constructor_type) == ARRAY_TYPE
6402 && TYPE_DOMAIN (constructor_type)
6403 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6405 /* Silently discard empty initializations. The parser will
6406 already have pedwarned for empty brackets. */
6407 if (integer_zerop (constructor_unfilled_index))
6408 constructor_type = NULL_TREE;
6409 else
6411 gcc_assert (!TYPE_SIZE (constructor_type));
6413 if (constructor_depth > 2)
6414 error_init ("initialization of flexible array member in a nested context");
6415 else
6416 pedwarn_init (input_location, OPT_pedantic,
6417 "initialization of a flexible array member");
6419 /* We have already issued an error message for the existence
6420 of a flexible array member not at the end of the structure.
6421 Discard the initializer so that we do not die later. */
6422 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6423 constructor_type = NULL_TREE;
6427 /* Warn when some struct elements are implicitly initialized to zero. */
6428 if (warn_missing_field_initializers
6429 && constructor_type
6430 && TREE_CODE (constructor_type) == RECORD_TYPE
6431 && constructor_unfilled_fields)
6433 /* Do not warn for flexible array members or zero-length arrays. */
6434 while (constructor_unfilled_fields
6435 && (!DECL_SIZE (constructor_unfilled_fields)
6436 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6437 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6439 /* Do not warn if this level of the initializer uses member
6440 designators; it is likely to be deliberate. */
6441 if (constructor_unfilled_fields && !constructor_designated)
6443 push_member_name (constructor_unfilled_fields);
6444 warning_init (OPT_Wmissing_field_initializers,
6445 "missing initializer");
6446 RESTORE_SPELLING_DEPTH (constructor_depth);
6450 /* Pad out the end of the structure. */
6451 if (p->replacement_value.value)
6452 /* If this closes a superfluous brace pair,
6453 just pass out the element between them. */
6454 ret = p->replacement_value;
6455 else if (constructor_type == 0)
6457 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6458 && TREE_CODE (constructor_type) != UNION_TYPE
6459 && TREE_CODE (constructor_type) != ARRAY_TYPE
6460 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6462 /* A nonincremental scalar initializer--just return
6463 the element, after verifying there is just one. */
6464 if (VEC_empty (constructor_elt,constructor_elements))
6466 if (!constructor_erroneous)
6467 error_init ("empty scalar initializer");
6468 ret.value = error_mark_node;
6470 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6472 error_init ("extra elements in scalar initializer");
6473 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6475 else
6476 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6478 else
6480 if (constructor_erroneous)
6481 ret.value = error_mark_node;
6482 else
6484 ret.value = build_constructor (constructor_type,
6485 constructor_elements);
6486 if (constructor_constant)
6487 TREE_CONSTANT (ret.value) = 1;
6488 if (constructor_constant && constructor_simple)
6489 TREE_STATIC (ret.value) = 1;
6490 if (constructor_nonconst)
6491 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6495 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6497 if (constructor_nonconst)
6498 ret.original_code = C_MAYBE_CONST_EXPR;
6499 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6500 ret.original_code = ERROR_MARK;
6503 constructor_type = p->type;
6504 constructor_fields = p->fields;
6505 constructor_index = p->index;
6506 constructor_max_index = p->max_index;
6507 constructor_unfilled_index = p->unfilled_index;
6508 constructor_unfilled_fields = p->unfilled_fields;
6509 constructor_bit_index = p->bit_index;
6510 constructor_elements = p->elements;
6511 constructor_constant = p->constant;
6512 constructor_simple = p->simple;
6513 constructor_nonconst = p->nonconst;
6514 constructor_erroneous = p->erroneous;
6515 constructor_incremental = p->incremental;
6516 constructor_designated = p->designated;
6517 constructor_pending_elts = p->pending_elts;
6518 constructor_depth = p->depth;
6519 if (!p->implicit)
6520 constructor_range_stack = p->range_stack;
6521 RESTORE_SPELLING_DEPTH (constructor_depth);
6523 constructor_stack = p->next;
6524 free (p);
6526 if (ret.value == 0 && constructor_stack == 0)
6527 ret.value = error_mark_node;
6528 return ret;
6531 /* Common handling for both array range and field name designators.
6532 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6534 static int
6535 set_designator (int array)
6537 tree subtype;
6538 enum tree_code subcode;
6540 /* Don't die if an entire brace-pair level is superfluous
6541 in the containing level. */
6542 if (constructor_type == 0)
6543 return 1;
6545 /* If there were errors in this designator list already, bail out
6546 silently. */
6547 if (designator_erroneous)
6548 return 1;
6550 if (!designator_depth)
6552 gcc_assert (!constructor_range_stack);
6554 /* Designator list starts at the level of closest explicit
6555 braces. */
6556 while (constructor_stack->implicit)
6557 process_init_element (pop_init_level (1), true);
6558 constructor_designated = 1;
6559 return 0;
6562 switch (TREE_CODE (constructor_type))
6564 case RECORD_TYPE:
6565 case UNION_TYPE:
6566 subtype = TREE_TYPE (constructor_fields);
6567 if (subtype != error_mark_node)
6568 subtype = TYPE_MAIN_VARIANT (subtype);
6569 break;
6570 case ARRAY_TYPE:
6571 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6572 break;
6573 default:
6574 gcc_unreachable ();
6577 subcode = TREE_CODE (subtype);
6578 if (array && subcode != ARRAY_TYPE)
6580 error_init ("array index in non-array initializer");
6581 return 1;
6583 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6585 error_init ("field name not in record or union initializer");
6586 return 1;
6589 constructor_designated = 1;
6590 push_init_level (2);
6591 return 0;
6594 /* If there are range designators in designator list, push a new designator
6595 to constructor_range_stack. RANGE_END is end of such stack range or
6596 NULL_TREE if there is no range designator at this level. */
6598 static void
6599 push_range_stack (tree range_end)
6601 struct constructor_range_stack *p;
6603 p = GGC_NEW (struct constructor_range_stack);
6604 p->prev = constructor_range_stack;
6605 p->next = 0;
6606 p->fields = constructor_fields;
6607 p->range_start = constructor_index;
6608 p->index = constructor_index;
6609 p->stack = constructor_stack;
6610 p->range_end = range_end;
6611 if (constructor_range_stack)
6612 constructor_range_stack->next = p;
6613 constructor_range_stack = p;
6616 /* Within an array initializer, specify the next index to be initialized.
6617 FIRST is that index. If LAST is nonzero, then initialize a range
6618 of indices, running from FIRST through LAST. */
6620 void
6621 set_init_index (tree first, tree last)
6623 if (set_designator (1))
6624 return;
6626 designator_erroneous = 1;
6628 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6629 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6631 error_init ("array index in initializer not of integer type");
6632 return;
6635 if (TREE_CODE (first) != INTEGER_CST)
6637 first = c_fully_fold (first, false, NULL);
6638 if (TREE_CODE (first) == INTEGER_CST)
6639 pedwarn_init (input_location, OPT_pedantic,
6640 "array index in initializer is not "
6641 "an integer constant expression");
6644 if (last && TREE_CODE (last) != INTEGER_CST)
6646 last = c_fully_fold (last, false, NULL);
6647 if (TREE_CODE (last) == INTEGER_CST)
6648 pedwarn_init (input_location, OPT_pedantic,
6649 "array index in initializer is not "
6650 "an integer constant expression");
6653 if (TREE_CODE (first) != INTEGER_CST)
6654 error_init ("nonconstant array index in initializer");
6655 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6656 error_init ("nonconstant array index in initializer");
6657 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6658 error_init ("array index in non-array initializer");
6659 else if (tree_int_cst_sgn (first) == -1)
6660 error_init ("array index in initializer exceeds array bounds");
6661 else if (constructor_max_index
6662 && tree_int_cst_lt (constructor_max_index, first))
6663 error_init ("array index in initializer exceeds array bounds");
6664 else
6666 constant_expression_warning (first);
6667 if (last)
6668 constant_expression_warning (last);
6669 constructor_index = convert (bitsizetype, first);
6671 if (last)
6673 if (tree_int_cst_equal (first, last))
6674 last = 0;
6675 else if (tree_int_cst_lt (last, first))
6677 error_init ("empty index range in initializer");
6678 last = 0;
6680 else
6682 last = convert (bitsizetype, last);
6683 if (constructor_max_index != 0
6684 && tree_int_cst_lt (constructor_max_index, last))
6686 error_init ("array index range in initializer exceeds array bounds");
6687 last = 0;
6692 designator_depth++;
6693 designator_erroneous = 0;
6694 if (constructor_range_stack || last)
6695 push_range_stack (last);
6699 /* Within a struct initializer, specify the next field to be initialized. */
6701 void
6702 set_init_label (tree fieldname)
6704 tree tail;
6706 if (set_designator (0))
6707 return;
6709 designator_erroneous = 1;
6711 if (TREE_CODE (constructor_type) != RECORD_TYPE
6712 && TREE_CODE (constructor_type) != UNION_TYPE)
6714 error_init ("field name not in record or union initializer");
6715 return;
6718 for (tail = TYPE_FIELDS (constructor_type); tail;
6719 tail = TREE_CHAIN (tail))
6721 if (DECL_NAME (tail) == fieldname)
6722 break;
6725 if (tail == 0)
6726 error ("unknown field %qE specified in initializer", fieldname);
6727 else
6729 constructor_fields = tail;
6730 designator_depth++;
6731 designator_erroneous = 0;
6732 if (constructor_range_stack)
6733 push_range_stack (NULL_TREE);
6737 /* Add a new initializer to the tree of pending initializers. PURPOSE
6738 identifies the initializer, either array index or field in a structure.
6739 VALUE is the value of that index or field. If ORIGTYPE is not
6740 NULL_TREE, it is the original type of VALUE.
6742 IMPLICIT is true if value comes from pop_init_level (1),
6743 the new initializer has been merged with the existing one
6744 and thus no warnings should be emitted about overriding an
6745 existing initializer. */
6747 static void
6748 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6750 struct init_node *p, **q, *r;
6752 q = &constructor_pending_elts;
6753 p = 0;
6755 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6757 while (*q != 0)
6759 p = *q;
6760 if (tree_int_cst_lt (purpose, p->purpose))
6761 q = &p->left;
6762 else if (tree_int_cst_lt (p->purpose, purpose))
6763 q = &p->right;
6764 else
6766 if (!implicit)
6768 if (TREE_SIDE_EFFECTS (p->value))
6769 warning_init (0, "initialized field with side-effects overwritten");
6770 else if (warn_override_init)
6771 warning_init (OPT_Woverride_init, "initialized field overwritten");
6773 p->value = value;
6774 p->origtype = origtype;
6775 return;
6779 else
6781 tree bitpos;
6783 bitpos = bit_position (purpose);
6784 while (*q != NULL)
6786 p = *q;
6787 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6788 q = &p->left;
6789 else if (p->purpose != purpose)
6790 q = &p->right;
6791 else
6793 if (!implicit)
6795 if (TREE_SIDE_EFFECTS (p->value))
6796 warning_init (0, "initialized field with side-effects overwritten");
6797 else if (warn_override_init)
6798 warning_init (OPT_Woverride_init, "initialized field overwritten");
6800 p->value = value;
6801 p->origtype = origtype;
6802 return;
6807 r = GGC_NEW (struct init_node);
6808 r->purpose = purpose;
6809 r->value = value;
6810 r->origtype = origtype;
6812 *q = r;
6813 r->parent = p;
6814 r->left = 0;
6815 r->right = 0;
6816 r->balance = 0;
6818 while (p)
6820 struct init_node *s;
6822 if (r == p->left)
6824 if (p->balance == 0)
6825 p->balance = -1;
6826 else if (p->balance < 0)
6828 if (r->balance < 0)
6830 /* L rotation. */
6831 p->left = r->right;
6832 if (p->left)
6833 p->left->parent = p;
6834 r->right = p;
6836 p->balance = 0;
6837 r->balance = 0;
6839 s = p->parent;
6840 p->parent = r;
6841 r->parent = s;
6842 if (s)
6844 if (s->left == p)
6845 s->left = r;
6846 else
6847 s->right = r;
6849 else
6850 constructor_pending_elts = r;
6852 else
6854 /* LR rotation. */
6855 struct init_node *t = r->right;
6857 r->right = t->left;
6858 if (r->right)
6859 r->right->parent = r;
6860 t->left = r;
6862 p->left = t->right;
6863 if (p->left)
6864 p->left->parent = p;
6865 t->right = p;
6867 p->balance = t->balance < 0;
6868 r->balance = -(t->balance > 0);
6869 t->balance = 0;
6871 s = p->parent;
6872 p->parent = t;
6873 r->parent = t;
6874 t->parent = s;
6875 if (s)
6877 if (s->left == p)
6878 s->left = t;
6879 else
6880 s->right = t;
6882 else
6883 constructor_pending_elts = t;
6885 break;
6887 else
6889 /* p->balance == +1; growth of left side balances the node. */
6890 p->balance = 0;
6891 break;
6894 else /* r == p->right */
6896 if (p->balance == 0)
6897 /* Growth propagation from right side. */
6898 p->balance++;
6899 else if (p->balance > 0)
6901 if (r->balance > 0)
6903 /* R rotation. */
6904 p->right = r->left;
6905 if (p->right)
6906 p->right->parent = p;
6907 r->left = p;
6909 p->balance = 0;
6910 r->balance = 0;
6912 s = p->parent;
6913 p->parent = r;
6914 r->parent = s;
6915 if (s)
6917 if (s->left == p)
6918 s->left = r;
6919 else
6920 s->right = r;
6922 else
6923 constructor_pending_elts = r;
6925 else /* r->balance == -1 */
6927 /* RL rotation */
6928 struct init_node *t = r->left;
6930 r->left = t->right;
6931 if (r->left)
6932 r->left->parent = r;
6933 t->right = r;
6935 p->right = t->left;
6936 if (p->right)
6937 p->right->parent = p;
6938 t->left = p;
6940 r->balance = (t->balance < 0);
6941 p->balance = -(t->balance > 0);
6942 t->balance = 0;
6944 s = p->parent;
6945 p->parent = t;
6946 r->parent = t;
6947 t->parent = s;
6948 if (s)
6950 if (s->left == p)
6951 s->left = t;
6952 else
6953 s->right = t;
6955 else
6956 constructor_pending_elts = t;
6958 break;
6960 else
6962 /* p->balance == -1; growth of right side balances the node. */
6963 p->balance = 0;
6964 break;
6968 r = p;
6969 p = p->parent;
6973 /* Build AVL tree from a sorted chain. */
6975 static void
6976 set_nonincremental_init (void)
6978 unsigned HOST_WIDE_INT ix;
6979 tree index, value;
6981 if (TREE_CODE (constructor_type) != RECORD_TYPE
6982 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6983 return;
6985 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6986 add_pending_init (index, value, NULL_TREE, false);
6987 constructor_elements = 0;
6988 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6990 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6991 /* Skip any nameless bit fields at the beginning. */
6992 while (constructor_unfilled_fields != 0
6993 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6994 && DECL_NAME (constructor_unfilled_fields) == 0)
6995 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6998 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7000 if (TYPE_DOMAIN (constructor_type))
7001 constructor_unfilled_index
7002 = convert (bitsizetype,
7003 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7004 else
7005 constructor_unfilled_index = bitsize_zero_node;
7007 constructor_incremental = 0;
7010 /* Build AVL tree from a string constant. */
7012 static void
7013 set_nonincremental_init_from_string (tree str)
7015 tree value, purpose, type;
7016 HOST_WIDE_INT val[2];
7017 const char *p, *end;
7018 int byte, wchar_bytes, charwidth, bitpos;
7020 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7022 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7023 charwidth = TYPE_PRECISION (char_type_node);
7024 type = TREE_TYPE (constructor_type);
7025 p = TREE_STRING_POINTER (str);
7026 end = p + TREE_STRING_LENGTH (str);
7028 for (purpose = bitsize_zero_node;
7029 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7030 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7032 if (wchar_bytes == 1)
7034 val[1] = (unsigned char) *p++;
7035 val[0] = 0;
7037 else
7039 val[0] = 0;
7040 val[1] = 0;
7041 for (byte = 0; byte < wchar_bytes; byte++)
7043 if (BYTES_BIG_ENDIAN)
7044 bitpos = (wchar_bytes - byte - 1) * charwidth;
7045 else
7046 bitpos = byte * charwidth;
7047 val[bitpos < HOST_BITS_PER_WIDE_INT]
7048 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7049 << (bitpos % HOST_BITS_PER_WIDE_INT);
7053 if (!TYPE_UNSIGNED (type))
7055 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7056 if (bitpos < HOST_BITS_PER_WIDE_INT)
7058 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7060 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7061 val[0] = -1;
7064 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7066 if (val[1] < 0)
7067 val[0] = -1;
7069 else if (val[0] & (((HOST_WIDE_INT) 1)
7070 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7071 val[0] |= ((HOST_WIDE_INT) -1)
7072 << (bitpos - HOST_BITS_PER_WIDE_INT);
7075 value = build_int_cst_wide (type, val[1], val[0]);
7076 add_pending_init (purpose, value, NULL_TREE, false);
7079 constructor_incremental = 0;
7082 /* Return value of FIELD in pending initializer or zero if the field was
7083 not initialized yet. */
7085 static tree
7086 find_init_member (tree field)
7088 struct init_node *p;
7090 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7092 if (constructor_incremental
7093 && tree_int_cst_lt (field, constructor_unfilled_index))
7094 set_nonincremental_init ();
7096 p = constructor_pending_elts;
7097 while (p)
7099 if (tree_int_cst_lt (field, p->purpose))
7100 p = p->left;
7101 else if (tree_int_cst_lt (p->purpose, field))
7102 p = p->right;
7103 else
7104 return p->value;
7107 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7109 tree bitpos = bit_position (field);
7111 if (constructor_incremental
7112 && (!constructor_unfilled_fields
7113 || tree_int_cst_lt (bitpos,
7114 bit_position (constructor_unfilled_fields))))
7115 set_nonincremental_init ();
7117 p = constructor_pending_elts;
7118 while (p)
7120 if (field == p->purpose)
7121 return p->value;
7122 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7123 p = p->left;
7124 else
7125 p = p->right;
7128 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7130 if (!VEC_empty (constructor_elt, constructor_elements)
7131 && (VEC_last (constructor_elt, constructor_elements)->index
7132 == field))
7133 return VEC_last (constructor_elt, constructor_elements)->value;
7135 return 0;
7138 /* "Output" the next constructor element.
7139 At top level, really output it to assembler code now.
7140 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7141 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7142 TYPE is the data type that the containing data type wants here.
7143 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7144 If VALUE is a string constant, STRICT_STRING is true if it is
7145 unparenthesized or we should not warn here for it being parenthesized.
7146 For other types of VALUE, STRICT_STRING is not used.
7148 PENDING if non-nil means output pending elements that belong
7149 right after this element. (PENDING is normally 1;
7150 it is 0 while outputting pending elements, to avoid recursion.)
7152 IMPLICIT is true if value comes from pop_init_level (1),
7153 the new initializer has been merged with the existing one
7154 and thus no warnings should be emitted about overriding an
7155 existing initializer. */
7157 static void
7158 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7159 tree field, int pending, bool implicit)
7161 tree semantic_type = NULL_TREE;
7162 constructor_elt *celt;
7163 bool maybe_const = true;
7164 bool npc;
7166 if (type == error_mark_node || value == error_mark_node)
7168 constructor_erroneous = 1;
7169 return;
7171 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7172 && (TREE_CODE (value) == STRING_CST
7173 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7174 && !(TREE_CODE (value) == STRING_CST
7175 && TREE_CODE (type) == ARRAY_TYPE
7176 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7177 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7178 TYPE_MAIN_VARIANT (type)))
7179 value = array_to_pointer_conversion (input_location, value);
7181 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7182 && require_constant_value && !flag_isoc99 && pending)
7184 /* As an extension, allow initializing objects with static storage
7185 duration with compound literals (which are then treated just as
7186 the brace enclosed list they contain). */
7187 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7188 value = DECL_INITIAL (decl);
7191 npc = null_pointer_constant_p (value);
7192 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7194 semantic_type = TREE_TYPE (value);
7195 value = TREE_OPERAND (value, 0);
7197 value = c_fully_fold (value, require_constant_value, &maybe_const);
7199 if (value == error_mark_node)
7200 constructor_erroneous = 1;
7201 else if (!TREE_CONSTANT (value))
7202 constructor_constant = 0;
7203 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7204 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7205 || TREE_CODE (constructor_type) == UNION_TYPE)
7206 && DECL_C_BIT_FIELD (field)
7207 && TREE_CODE (value) != INTEGER_CST))
7208 constructor_simple = 0;
7209 if (!maybe_const)
7210 constructor_nonconst = 1;
7212 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7214 if (require_constant_value)
7216 error_init ("initializer element is not constant");
7217 value = error_mark_node;
7219 else if (require_constant_elements)
7220 pedwarn (input_location, 0,
7221 "initializer element is not computable at load time");
7223 else if (!maybe_const
7224 && (require_constant_value || require_constant_elements))
7225 pedwarn_init (input_location, 0,
7226 "initializer element is not a constant expression");
7228 /* Issue -Wc++-compat warnings about initializing a bitfield with
7229 enum type. */
7230 if (warn_cxx_compat
7231 && field != NULL_TREE
7232 && TREE_CODE (field) == FIELD_DECL
7233 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7234 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7235 != TYPE_MAIN_VARIANT (type))
7236 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7238 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7239 if (checktype != error_mark_node
7240 && (TYPE_MAIN_VARIANT (checktype)
7241 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7242 warning_init (OPT_Wc___compat,
7243 "enum conversion in initialization is invalid in C++");
7246 /* If this field is empty (and not at the end of structure),
7247 don't do anything other than checking the initializer. */
7248 if (field
7249 && (TREE_TYPE (field) == error_mark_node
7250 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7251 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7252 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7253 || TREE_CHAIN (field)))))
7254 return;
7256 if (semantic_type)
7257 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7258 value = digest_init (input_location, type, value, origtype, npc,
7259 strict_string, require_constant_value);
7260 if (value == error_mark_node)
7262 constructor_erroneous = 1;
7263 return;
7265 if (require_constant_value || require_constant_elements)
7266 constant_expression_warning (value);
7268 /* If this element doesn't come next in sequence,
7269 put it on constructor_pending_elts. */
7270 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7271 && (!constructor_incremental
7272 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7274 if (constructor_incremental
7275 && tree_int_cst_lt (field, constructor_unfilled_index))
7276 set_nonincremental_init ();
7278 add_pending_init (field, value, origtype, implicit);
7279 return;
7281 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7282 && (!constructor_incremental
7283 || field != constructor_unfilled_fields))
7285 /* We do this for records but not for unions. In a union,
7286 no matter which field is specified, it can be initialized
7287 right away since it starts at the beginning of the union. */
7288 if (constructor_incremental)
7290 if (!constructor_unfilled_fields)
7291 set_nonincremental_init ();
7292 else
7294 tree bitpos, unfillpos;
7296 bitpos = bit_position (field);
7297 unfillpos = bit_position (constructor_unfilled_fields);
7299 if (tree_int_cst_lt (bitpos, unfillpos))
7300 set_nonincremental_init ();
7304 add_pending_init (field, value, origtype, implicit);
7305 return;
7307 else if (TREE_CODE (constructor_type) == UNION_TYPE
7308 && !VEC_empty (constructor_elt, constructor_elements))
7310 if (!implicit)
7312 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7313 constructor_elements)->value))
7314 warning_init (0,
7315 "initialized field with side-effects overwritten");
7316 else if (warn_override_init)
7317 warning_init (OPT_Woverride_init, "initialized field overwritten");
7320 /* We can have just one union field set. */
7321 constructor_elements = 0;
7324 /* Otherwise, output this element either to
7325 constructor_elements or to the assembler file. */
7327 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7328 celt->index = field;
7329 celt->value = value;
7331 /* Advance the variable that indicates sequential elements output. */
7332 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7333 constructor_unfilled_index
7334 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7335 bitsize_one_node);
7336 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7338 constructor_unfilled_fields
7339 = TREE_CHAIN (constructor_unfilled_fields);
7341 /* Skip any nameless bit fields. */
7342 while (constructor_unfilled_fields != 0
7343 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7344 && DECL_NAME (constructor_unfilled_fields) == 0)
7345 constructor_unfilled_fields =
7346 TREE_CHAIN (constructor_unfilled_fields);
7348 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7349 constructor_unfilled_fields = 0;
7351 /* Now output any pending elements which have become next. */
7352 if (pending)
7353 output_pending_init_elements (0);
7356 /* Output any pending elements which have become next.
7357 As we output elements, constructor_unfilled_{fields,index}
7358 advances, which may cause other elements to become next;
7359 if so, they too are output.
7361 If ALL is 0, we return when there are
7362 no more pending elements to output now.
7364 If ALL is 1, we output space as necessary so that
7365 we can output all the pending elements. */
7367 static void
7368 output_pending_init_elements (int all)
7370 struct init_node *elt = constructor_pending_elts;
7371 tree next;
7373 retry:
7375 /* Look through the whole pending tree.
7376 If we find an element that should be output now,
7377 output it. Otherwise, set NEXT to the element
7378 that comes first among those still pending. */
7380 next = 0;
7381 while (elt)
7383 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7385 if (tree_int_cst_equal (elt->purpose,
7386 constructor_unfilled_index))
7387 output_init_element (elt->value, elt->origtype, true,
7388 TREE_TYPE (constructor_type),
7389 constructor_unfilled_index, 0, false);
7390 else if (tree_int_cst_lt (constructor_unfilled_index,
7391 elt->purpose))
7393 /* Advance to the next smaller node. */
7394 if (elt->left)
7395 elt = elt->left;
7396 else
7398 /* We have reached the smallest node bigger than the
7399 current unfilled index. Fill the space first. */
7400 next = elt->purpose;
7401 break;
7404 else
7406 /* Advance to the next bigger node. */
7407 if (elt->right)
7408 elt = elt->right;
7409 else
7411 /* We have reached the biggest node in a subtree. Find
7412 the parent of it, which is the next bigger node. */
7413 while (elt->parent && elt->parent->right == elt)
7414 elt = elt->parent;
7415 elt = elt->parent;
7416 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7417 elt->purpose))
7419 next = elt->purpose;
7420 break;
7425 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7426 || TREE_CODE (constructor_type) == UNION_TYPE)
7428 tree ctor_unfilled_bitpos, elt_bitpos;
7430 /* If the current record is complete we are done. */
7431 if (constructor_unfilled_fields == 0)
7432 break;
7434 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7435 elt_bitpos = bit_position (elt->purpose);
7436 /* We can't compare fields here because there might be empty
7437 fields in between. */
7438 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7440 constructor_unfilled_fields = elt->purpose;
7441 output_init_element (elt->value, elt->origtype, true,
7442 TREE_TYPE (elt->purpose),
7443 elt->purpose, 0, false);
7445 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7447 /* Advance to the next smaller node. */
7448 if (elt->left)
7449 elt = elt->left;
7450 else
7452 /* We have reached the smallest node bigger than the
7453 current unfilled field. Fill the space first. */
7454 next = elt->purpose;
7455 break;
7458 else
7460 /* Advance to the next bigger node. */
7461 if (elt->right)
7462 elt = elt->right;
7463 else
7465 /* We have reached the biggest node in a subtree. Find
7466 the parent of it, which is the next bigger node. */
7467 while (elt->parent && elt->parent->right == elt)
7468 elt = elt->parent;
7469 elt = elt->parent;
7470 if (elt
7471 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7472 bit_position (elt->purpose))))
7474 next = elt->purpose;
7475 break;
7482 /* Ordinarily return, but not if we want to output all
7483 and there are elements left. */
7484 if (!(all && next != 0))
7485 return;
7487 /* If it's not incremental, just skip over the gap, so that after
7488 jumping to retry we will output the next successive element. */
7489 if (TREE_CODE (constructor_type) == RECORD_TYPE
7490 || TREE_CODE (constructor_type) == UNION_TYPE)
7491 constructor_unfilled_fields = next;
7492 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7493 constructor_unfilled_index = next;
7495 /* ELT now points to the node in the pending tree with the next
7496 initializer to output. */
7497 goto retry;
7500 /* Add one non-braced element to the current constructor level.
7501 This adjusts the current position within the constructor's type.
7502 This may also start or terminate implicit levels
7503 to handle a partly-braced initializer.
7505 Once this has found the correct level for the new element,
7506 it calls output_init_element.
7508 IMPLICIT is true if value comes from pop_init_level (1),
7509 the new initializer has been merged with the existing one
7510 and thus no warnings should be emitted about overriding an
7511 existing initializer. */
7513 void
7514 process_init_element (struct c_expr value, bool implicit)
7516 tree orig_value = value.value;
7517 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7518 bool strict_string = value.original_code == STRING_CST;
7520 designator_depth = 0;
7521 designator_erroneous = 0;
7523 /* Handle superfluous braces around string cst as in
7524 char x[] = {"foo"}; */
7525 if (string_flag
7526 && constructor_type
7527 && TREE_CODE (constructor_type) == ARRAY_TYPE
7528 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7529 && integer_zerop (constructor_unfilled_index))
7531 if (constructor_stack->replacement_value.value)
7532 error_init ("excess elements in char array initializer");
7533 constructor_stack->replacement_value = value;
7534 return;
7537 if (constructor_stack->replacement_value.value != 0)
7539 error_init ("excess elements in struct initializer");
7540 return;
7543 /* Ignore elements of a brace group if it is entirely superfluous
7544 and has already been diagnosed. */
7545 if (constructor_type == 0)
7546 return;
7548 /* If we've exhausted any levels that didn't have braces,
7549 pop them now. */
7550 while (constructor_stack->implicit)
7552 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7553 || TREE_CODE (constructor_type) == UNION_TYPE)
7554 && constructor_fields == 0)
7555 process_init_element (pop_init_level (1), true);
7556 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7557 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7558 && (constructor_max_index == 0
7559 || tree_int_cst_lt (constructor_max_index,
7560 constructor_index)))
7561 process_init_element (pop_init_level (1), true);
7562 else
7563 break;
7566 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7567 if (constructor_range_stack)
7569 /* If value is a compound literal and we'll be just using its
7570 content, don't put it into a SAVE_EXPR. */
7571 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7572 || !require_constant_value
7573 || flag_isoc99)
7575 tree semantic_type = NULL_TREE;
7576 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7578 semantic_type = TREE_TYPE (value.value);
7579 value.value = TREE_OPERAND (value.value, 0);
7581 value.value = c_save_expr (value.value);
7582 if (semantic_type)
7583 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7584 value.value);
7588 while (1)
7590 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7592 tree fieldtype;
7593 enum tree_code fieldcode;
7595 if (constructor_fields == 0)
7597 pedwarn_init (input_location, 0,
7598 "excess elements in struct initializer");
7599 break;
7602 fieldtype = TREE_TYPE (constructor_fields);
7603 if (fieldtype != error_mark_node)
7604 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7605 fieldcode = TREE_CODE (fieldtype);
7607 /* Error for non-static initialization of a flexible array member. */
7608 if (fieldcode == ARRAY_TYPE
7609 && !require_constant_value
7610 && TYPE_SIZE (fieldtype) == NULL_TREE
7611 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7613 error_init ("non-static initialization of a flexible array member");
7614 break;
7617 /* Accept a string constant to initialize a subarray. */
7618 if (value.value != 0
7619 && fieldcode == ARRAY_TYPE
7620 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7621 && string_flag)
7622 value.value = orig_value;
7623 /* Otherwise, if we have come to a subaggregate,
7624 and we don't have an element of its type, push into it. */
7625 else if (value.value != 0
7626 && value.value != error_mark_node
7627 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7628 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7629 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7631 push_init_level (1);
7632 continue;
7635 if (value.value)
7637 push_member_name (constructor_fields);
7638 output_init_element (value.value, value.original_type,
7639 strict_string, fieldtype,
7640 constructor_fields, 1, implicit);
7641 RESTORE_SPELLING_DEPTH (constructor_depth);
7643 else
7644 /* Do the bookkeeping for an element that was
7645 directly output as a constructor. */
7647 /* For a record, keep track of end position of last field. */
7648 if (DECL_SIZE (constructor_fields))
7649 constructor_bit_index
7650 = size_binop_loc (input_location, PLUS_EXPR,
7651 bit_position (constructor_fields),
7652 DECL_SIZE (constructor_fields));
7654 /* If the current field was the first one not yet written out,
7655 it isn't now, so update. */
7656 if (constructor_unfilled_fields == constructor_fields)
7658 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7659 /* Skip any nameless bit fields. */
7660 while (constructor_unfilled_fields != 0
7661 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7662 && DECL_NAME (constructor_unfilled_fields) == 0)
7663 constructor_unfilled_fields =
7664 TREE_CHAIN (constructor_unfilled_fields);
7668 constructor_fields = TREE_CHAIN (constructor_fields);
7669 /* Skip any nameless bit fields at the beginning. */
7670 while (constructor_fields != 0
7671 && DECL_C_BIT_FIELD (constructor_fields)
7672 && DECL_NAME (constructor_fields) == 0)
7673 constructor_fields = TREE_CHAIN (constructor_fields);
7675 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7677 tree fieldtype;
7678 enum tree_code fieldcode;
7680 if (constructor_fields == 0)
7682 pedwarn_init (input_location, 0,
7683 "excess elements in union initializer");
7684 break;
7687 fieldtype = TREE_TYPE (constructor_fields);
7688 if (fieldtype != error_mark_node)
7689 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7690 fieldcode = TREE_CODE (fieldtype);
7692 /* Warn that traditional C rejects initialization of unions.
7693 We skip the warning if the value is zero. This is done
7694 under the assumption that the zero initializer in user
7695 code appears conditioned on e.g. __STDC__ to avoid
7696 "missing initializer" warnings and relies on default
7697 initialization to zero in the traditional C case.
7698 We also skip the warning if the initializer is designated,
7699 again on the assumption that this must be conditional on
7700 __STDC__ anyway (and we've already complained about the
7701 member-designator already). */
7702 if (!in_system_header && !constructor_designated
7703 && !(value.value && (integer_zerop (value.value)
7704 || real_zerop (value.value))))
7705 warning (OPT_Wtraditional, "traditional C rejects initialization "
7706 "of unions");
7708 /* Accept a string constant to initialize a subarray. */
7709 if (value.value != 0
7710 && fieldcode == ARRAY_TYPE
7711 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7712 && string_flag)
7713 value.value = orig_value;
7714 /* Otherwise, if we have come to a subaggregate,
7715 and we don't have an element of its type, push into it. */
7716 else if (value.value != 0
7717 && value.value != error_mark_node
7718 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7719 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7720 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7722 push_init_level (1);
7723 continue;
7726 if (value.value)
7728 push_member_name (constructor_fields);
7729 output_init_element (value.value, value.original_type,
7730 strict_string, fieldtype,
7731 constructor_fields, 1, implicit);
7732 RESTORE_SPELLING_DEPTH (constructor_depth);
7734 else
7735 /* Do the bookkeeping for an element that was
7736 directly output as a constructor. */
7738 constructor_bit_index = DECL_SIZE (constructor_fields);
7739 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7742 constructor_fields = 0;
7744 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7746 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7747 enum tree_code eltcode = TREE_CODE (elttype);
7749 /* Accept a string constant to initialize a subarray. */
7750 if (value.value != 0
7751 && eltcode == ARRAY_TYPE
7752 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7753 && string_flag)
7754 value.value = orig_value;
7755 /* Otherwise, if we have come to a subaggregate,
7756 and we don't have an element of its type, push into it. */
7757 else if (value.value != 0
7758 && value.value != error_mark_node
7759 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7760 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7761 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7763 push_init_level (1);
7764 continue;
7767 if (constructor_max_index != 0
7768 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7769 || integer_all_onesp (constructor_max_index)))
7771 pedwarn_init (input_location, 0,
7772 "excess elements in array initializer");
7773 break;
7776 /* Now output the actual element. */
7777 if (value.value)
7779 push_array_bounds (tree_low_cst (constructor_index, 1));
7780 output_init_element (value.value, value.original_type,
7781 strict_string, elttype,
7782 constructor_index, 1, implicit);
7783 RESTORE_SPELLING_DEPTH (constructor_depth);
7786 constructor_index
7787 = size_binop_loc (input_location, PLUS_EXPR,
7788 constructor_index, bitsize_one_node);
7790 if (!value.value)
7791 /* If we are doing the bookkeeping for an element that was
7792 directly output as a constructor, we must update
7793 constructor_unfilled_index. */
7794 constructor_unfilled_index = constructor_index;
7796 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7798 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7800 /* Do a basic check of initializer size. Note that vectors
7801 always have a fixed size derived from their type. */
7802 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7804 pedwarn_init (input_location, 0,
7805 "excess elements in vector initializer");
7806 break;
7809 /* Now output the actual element. */
7810 if (value.value)
7812 if (TREE_CODE (value.value) == VECTOR_CST)
7813 elttype = TYPE_MAIN_VARIANT (constructor_type);
7814 output_init_element (value.value, value.original_type,
7815 strict_string, elttype,
7816 constructor_index, 1, implicit);
7819 constructor_index
7820 = size_binop_loc (input_location,
7821 PLUS_EXPR, constructor_index, bitsize_one_node);
7823 if (!value.value)
7824 /* If we are doing the bookkeeping for an element that was
7825 directly output as a constructor, we must update
7826 constructor_unfilled_index. */
7827 constructor_unfilled_index = constructor_index;
7830 /* Handle the sole element allowed in a braced initializer
7831 for a scalar variable. */
7832 else if (constructor_type != error_mark_node
7833 && constructor_fields == 0)
7835 pedwarn_init (input_location, 0,
7836 "excess elements in scalar initializer");
7837 break;
7839 else
7841 if (value.value)
7842 output_init_element (value.value, value.original_type,
7843 strict_string, constructor_type,
7844 NULL_TREE, 1, implicit);
7845 constructor_fields = 0;
7848 /* Handle range initializers either at this level or anywhere higher
7849 in the designator stack. */
7850 if (constructor_range_stack)
7852 struct constructor_range_stack *p, *range_stack;
7853 int finish = 0;
7855 range_stack = constructor_range_stack;
7856 constructor_range_stack = 0;
7857 while (constructor_stack != range_stack->stack)
7859 gcc_assert (constructor_stack->implicit);
7860 process_init_element (pop_init_level (1), true);
7862 for (p = range_stack;
7863 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7864 p = p->prev)
7866 gcc_assert (constructor_stack->implicit);
7867 process_init_element (pop_init_level (1), true);
7870 p->index = size_binop_loc (input_location,
7871 PLUS_EXPR, p->index, bitsize_one_node);
7872 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7873 finish = 1;
7875 while (1)
7877 constructor_index = p->index;
7878 constructor_fields = p->fields;
7879 if (finish && p->range_end && p->index == p->range_start)
7881 finish = 0;
7882 p->prev = 0;
7884 p = p->next;
7885 if (!p)
7886 break;
7887 push_init_level (2);
7888 p->stack = constructor_stack;
7889 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7890 p->index = p->range_start;
7893 if (!finish)
7894 constructor_range_stack = range_stack;
7895 continue;
7898 break;
7901 constructor_range_stack = 0;
7904 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7905 (guaranteed to be 'volatile' or null) and ARGS (represented using
7906 an ASM_EXPR node). */
7907 tree
7908 build_asm_stmt (tree cv_qualifier, tree args)
7910 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7911 ASM_VOLATILE_P (args) = 1;
7912 return add_stmt (args);
7915 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7916 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7917 SIMPLE indicates whether there was anything at all after the
7918 string in the asm expression -- asm("blah") and asm("blah" : )
7919 are subtly different. We use a ASM_EXPR node to represent this. */
7920 tree
7921 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
7922 tree clobbers, bool simple)
7924 tree tail;
7925 tree args;
7926 int i;
7927 const char *constraint;
7928 const char **oconstraints;
7929 bool allows_mem, allows_reg, is_inout;
7930 int ninputs, noutputs;
7932 ninputs = list_length (inputs);
7933 noutputs = list_length (outputs);
7934 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7936 string = resolve_asm_operand_names (string, outputs, inputs);
7938 /* Remove output conversions that change the type but not the mode. */
7939 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7941 tree output = TREE_VALUE (tail);
7943 /* ??? Really, this should not be here. Users should be using a
7944 proper lvalue, dammit. But there's a long history of using casts
7945 in the output operands. In cases like longlong.h, this becomes a
7946 primitive form of typechecking -- if the cast can be removed, then
7947 the output operand had a type of the proper width; otherwise we'll
7948 get an error. Gross, but ... */
7949 STRIP_NOPS (output);
7951 if (!lvalue_or_else (output, lv_asm))
7952 output = error_mark_node;
7954 if (output != error_mark_node
7955 && (TREE_READONLY (output)
7956 || TYPE_READONLY (TREE_TYPE (output))
7957 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7958 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7959 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7960 readonly_error (output, lv_asm);
7962 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7963 oconstraints[i] = constraint;
7965 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7966 &allows_mem, &allows_reg, &is_inout))
7968 /* If the operand is going to end up in memory,
7969 mark it addressable. */
7970 if (!allows_reg && !c_mark_addressable (output))
7971 output = error_mark_node;
7973 else
7974 output = error_mark_node;
7976 TREE_VALUE (tail) = output;
7979 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7981 tree input;
7983 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7984 input = TREE_VALUE (tail);
7986 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7987 oconstraints, &allows_mem, &allows_reg))
7989 /* If the operand is going to end up in memory,
7990 mark it addressable. */
7991 if (!allows_reg && allows_mem)
7993 /* Strip the nops as we allow this case. FIXME, this really
7994 should be rejected or made deprecated. */
7995 STRIP_NOPS (input);
7996 if (!c_mark_addressable (input))
7997 input = error_mark_node;
8000 else
8001 input = error_mark_node;
8003 TREE_VALUE (tail) = input;
8006 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers);
8008 /* asm statements without outputs, including simple ones, are treated
8009 as volatile. */
8010 ASM_INPUT_P (args) = simple;
8011 ASM_VOLATILE_P (args) = (noutputs == 0);
8013 return args;
8016 /* Generate a goto statement to LABEL. LOC is the location of the
8017 GOTO. */
8019 tree
8020 c_finish_goto_label (location_t loc, tree label)
8022 tree decl = lookup_label_for_goto (loc, label);
8023 if (!decl)
8024 return NULL_TREE;
8025 TREE_USED (decl) = 1;
8027 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8028 SET_EXPR_LOCATION (t, loc);
8029 return add_stmt (t);
8033 /* Generate a computed goto statement to EXPR. LOC is the location of
8034 the GOTO. */
8036 tree
8037 c_finish_goto_ptr (location_t loc, tree expr)
8039 tree t;
8040 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8041 expr = c_fully_fold (expr, false, NULL);
8042 expr = convert (ptr_type_node, expr);
8043 t = build1 (GOTO_EXPR, void_type_node, expr);
8044 SET_EXPR_LOCATION (t, loc);
8045 return add_stmt (t);
8048 /* Generate a C `return' statement. RETVAL is the expression for what
8049 to return, or a null pointer for `return;' with no value. LOC is
8050 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8051 is the original type of RETVAL. */
8053 tree
8054 c_finish_return (location_t loc, tree retval, tree origtype)
8056 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8057 bool no_warning = false;
8058 bool npc = false;
8060 if (TREE_THIS_VOLATILE (current_function_decl))
8061 warning_at (loc, 0,
8062 "function declared %<noreturn%> has a %<return%> statement");
8064 if (retval)
8066 tree semantic_type = NULL_TREE;
8067 npc = null_pointer_constant_p (retval);
8068 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8070 semantic_type = TREE_TYPE (retval);
8071 retval = TREE_OPERAND (retval, 0);
8073 retval = c_fully_fold (retval, false, NULL);
8074 if (semantic_type)
8075 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8078 if (!retval)
8080 current_function_returns_null = 1;
8081 if ((warn_return_type || flag_isoc99)
8082 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8084 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8085 "%<return%> with no value, in "
8086 "function returning non-void");
8087 no_warning = true;
8090 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8092 current_function_returns_null = 1;
8093 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8094 pedwarn (loc, 0,
8095 "%<return%> with a value, in function returning void");
8096 else
8097 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8098 "%<return%> with expression, in function returning void");
8100 else
8102 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8103 ic_return,
8104 npc, NULL_TREE, NULL_TREE, 0);
8105 tree res = DECL_RESULT (current_function_decl);
8106 tree inner;
8108 current_function_returns_value = 1;
8109 if (t == error_mark_node)
8110 return NULL_TREE;
8112 inner = t = convert (TREE_TYPE (res), t);
8114 /* Strip any conversions, additions, and subtractions, and see if
8115 we are returning the address of a local variable. Warn if so. */
8116 while (1)
8118 switch (TREE_CODE (inner))
8120 CASE_CONVERT:
8121 case NON_LVALUE_EXPR:
8122 case PLUS_EXPR:
8123 case POINTER_PLUS_EXPR:
8124 inner = TREE_OPERAND (inner, 0);
8125 continue;
8127 case MINUS_EXPR:
8128 /* If the second operand of the MINUS_EXPR has a pointer
8129 type (or is converted from it), this may be valid, so
8130 don't give a warning. */
8132 tree op1 = TREE_OPERAND (inner, 1);
8134 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8135 && (CONVERT_EXPR_P (op1)
8136 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8137 op1 = TREE_OPERAND (op1, 0);
8139 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8140 break;
8142 inner = TREE_OPERAND (inner, 0);
8143 continue;
8146 case ADDR_EXPR:
8147 inner = TREE_OPERAND (inner, 0);
8149 while (REFERENCE_CLASS_P (inner)
8150 && TREE_CODE (inner) != INDIRECT_REF)
8151 inner = TREE_OPERAND (inner, 0);
8153 if (DECL_P (inner)
8154 && !DECL_EXTERNAL (inner)
8155 && !TREE_STATIC (inner)
8156 && DECL_CONTEXT (inner) == current_function_decl)
8157 warning_at (loc,
8158 0, "function returns address of local variable");
8159 break;
8161 default:
8162 break;
8165 break;
8168 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8169 SET_EXPR_LOCATION (retval, loc);
8171 if (warn_sequence_point)
8172 verify_sequence_points (retval);
8175 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8176 TREE_NO_WARNING (ret_stmt) |= no_warning;
8177 return add_stmt (ret_stmt);
8180 struct c_switch {
8181 /* The SWITCH_EXPR being built. */
8182 tree switch_expr;
8184 /* The original type of the testing expression, i.e. before the
8185 default conversion is applied. */
8186 tree orig_type;
8188 /* A splay-tree mapping the low element of a case range to the high
8189 element, or NULL_TREE if there is no high element. Used to
8190 determine whether or not a new case label duplicates an old case
8191 label. We need a tree, rather than simply a hash table, because
8192 of the GNU case range extension. */
8193 splay_tree cases;
8195 /* The bindings at the point of the switch. This is used for
8196 warnings crossing decls when branching to a case label. */
8197 struct c_spot_bindings *bindings;
8199 /* The next node on the stack. */
8200 struct c_switch *next;
8203 /* A stack of the currently active switch statements. The innermost
8204 switch statement is on the top of the stack. There is no need to
8205 mark the stack for garbage collection because it is only active
8206 during the processing of the body of a function, and we never
8207 collect at that point. */
8209 struct c_switch *c_switch_stack;
8211 /* Start a C switch statement, testing expression EXP. Return the new
8212 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8213 SWITCH_COND_LOC is the location of the switch's condition. */
8215 tree
8216 c_start_case (location_t switch_loc,
8217 location_t switch_cond_loc,
8218 tree exp)
8220 tree orig_type = error_mark_node;
8221 struct c_switch *cs;
8223 if (exp != error_mark_node)
8225 orig_type = TREE_TYPE (exp);
8227 if (!INTEGRAL_TYPE_P (orig_type))
8229 if (orig_type != error_mark_node)
8231 error_at (switch_cond_loc, "switch quantity not an integer");
8232 orig_type = error_mark_node;
8234 exp = integer_zero_node;
8236 else
8238 tree type = TYPE_MAIN_VARIANT (orig_type);
8240 if (!in_system_header
8241 && (type == long_integer_type_node
8242 || type == long_unsigned_type_node))
8243 warning_at (switch_cond_loc,
8244 OPT_Wtraditional, "%<long%> switch expression not "
8245 "converted to %<int%> in ISO C");
8247 exp = c_fully_fold (exp, false, NULL);
8248 exp = default_conversion (exp);
8250 if (warn_sequence_point)
8251 verify_sequence_points (exp);
8255 /* Add this new SWITCH_EXPR to the stack. */
8256 cs = XNEW (struct c_switch);
8257 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8258 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8259 cs->orig_type = orig_type;
8260 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8261 cs->bindings = c_get_switch_bindings ();
8262 cs->next = c_switch_stack;
8263 c_switch_stack = cs;
8265 return add_stmt (cs->switch_expr);
8268 /* Process a case label at location LOC. */
8270 tree
8271 do_case (location_t loc, tree low_value, tree high_value)
8273 tree label = NULL_TREE;
8275 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8277 low_value = c_fully_fold (low_value, false, NULL);
8278 if (TREE_CODE (low_value) == INTEGER_CST)
8279 pedwarn (input_location, OPT_pedantic,
8280 "case label is not an integer constant expression");
8283 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8285 high_value = c_fully_fold (high_value, false, NULL);
8286 if (TREE_CODE (high_value) == INTEGER_CST)
8287 pedwarn (input_location, OPT_pedantic,
8288 "case label is not an integer constant expression");
8291 if (c_switch_stack == NULL)
8293 if (low_value)
8294 error_at (loc, "case label not within a switch statement");
8295 else
8296 error_at (loc, "%<default%> label not within a switch statement");
8297 return NULL_TREE;
8300 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8301 EXPR_LOCATION (c_switch_stack->switch_expr),
8302 loc))
8303 return NULL_TREE;
8305 label = c_add_case_label (loc, c_switch_stack->cases,
8306 SWITCH_COND (c_switch_stack->switch_expr),
8307 c_switch_stack->orig_type,
8308 low_value, high_value);
8309 if (label == error_mark_node)
8310 label = NULL_TREE;
8311 return label;
8314 /* Finish the switch statement. */
8316 void
8317 c_finish_case (tree body)
8319 struct c_switch *cs = c_switch_stack;
8320 location_t switch_location;
8322 SWITCH_BODY (cs->switch_expr) = body;
8324 /* Emit warnings as needed. */
8325 switch_location = EXPR_LOCATION (cs->switch_expr);
8326 c_do_switch_warnings (cs->cases, switch_location,
8327 TREE_TYPE (cs->switch_expr),
8328 SWITCH_COND (cs->switch_expr));
8330 /* Pop the stack. */
8331 c_switch_stack = cs->next;
8332 splay_tree_delete (cs->cases);
8333 c_release_switch_bindings (cs->bindings);
8334 XDELETE (cs);
8337 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8338 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8339 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8340 statement, and was not surrounded with parenthesis. */
8342 void
8343 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8344 tree else_block, bool nested_if)
8346 tree stmt;
8348 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8349 if (warn_parentheses && nested_if && else_block == NULL)
8351 tree inner_if = then_block;
8353 /* We know from the grammar productions that there is an IF nested
8354 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8355 it might not be exactly THEN_BLOCK, but should be the last
8356 non-container statement within. */
8357 while (1)
8358 switch (TREE_CODE (inner_if))
8360 case COND_EXPR:
8361 goto found;
8362 case BIND_EXPR:
8363 inner_if = BIND_EXPR_BODY (inner_if);
8364 break;
8365 case STATEMENT_LIST:
8366 inner_if = expr_last (then_block);
8367 break;
8368 case TRY_FINALLY_EXPR:
8369 case TRY_CATCH_EXPR:
8370 inner_if = TREE_OPERAND (inner_if, 0);
8371 break;
8372 default:
8373 gcc_unreachable ();
8375 found:
8377 if (COND_EXPR_ELSE (inner_if))
8378 warning_at (if_locus, OPT_Wparentheses,
8379 "suggest explicit braces to avoid ambiguous %<else%>");
8382 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8383 SET_EXPR_LOCATION (stmt, if_locus);
8384 add_stmt (stmt);
8387 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8388 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8389 is false for DO loops. INCR is the FOR increment expression. BODY is
8390 the statement controlled by the loop. BLAB is the break label. CLAB is
8391 the continue label. Everything is allowed to be NULL. */
8393 void
8394 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8395 tree blab, tree clab, bool cond_is_first)
8397 tree entry = NULL, exit = NULL, t;
8399 /* If the condition is zero don't generate a loop construct. */
8400 if (cond && integer_zerop (cond))
8402 if (cond_is_first)
8404 t = build_and_jump (&blab);
8405 SET_EXPR_LOCATION (t, start_locus);
8406 add_stmt (t);
8409 else
8411 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8413 /* If we have an exit condition, then we build an IF with gotos either
8414 out of the loop, or to the top of it. If there's no exit condition,
8415 then we just build a jump back to the top. */
8416 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8418 if (cond && !integer_nonzerop (cond))
8420 /* Canonicalize the loop condition to the end. This means
8421 generating a branch to the loop condition. Reuse the
8422 continue label, if possible. */
8423 if (cond_is_first)
8425 if (incr || !clab)
8427 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8428 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8430 else
8431 t = build1 (GOTO_EXPR, void_type_node, clab);
8432 SET_EXPR_LOCATION (t, start_locus);
8433 add_stmt (t);
8436 t = build_and_jump (&blab);
8437 if (cond_is_first)
8438 exit = fold_build3_loc (start_locus,
8439 COND_EXPR, void_type_node, cond, exit, t);
8440 else
8441 exit = fold_build3_loc (input_location,
8442 COND_EXPR, void_type_node, cond, exit, t);
8445 add_stmt (top);
8448 if (body)
8449 add_stmt (body);
8450 if (clab)
8451 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8452 if (incr)
8453 add_stmt (incr);
8454 if (entry)
8455 add_stmt (entry);
8456 if (exit)
8457 add_stmt (exit);
8458 if (blab)
8459 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8462 tree
8463 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8465 bool skip;
8466 tree label = *label_p;
8468 /* In switch statements break is sometimes stylistically used after
8469 a return statement. This can lead to spurious warnings about
8470 control reaching the end of a non-void function when it is
8471 inlined. Note that we are calling block_may_fallthru with
8472 language specific tree nodes; this works because
8473 block_may_fallthru returns true when given something it does not
8474 understand. */
8475 skip = !block_may_fallthru (cur_stmt_list);
8477 if (!label)
8479 if (!skip)
8480 *label_p = label = create_artificial_label (loc);
8482 else if (TREE_CODE (label) == LABEL_DECL)
8484 else switch (TREE_INT_CST_LOW (label))
8486 case 0:
8487 if (is_break)
8488 error_at (loc, "break statement not within loop or switch");
8489 else
8490 error_at (loc, "continue statement not within a loop");
8491 return NULL_TREE;
8493 case 1:
8494 gcc_assert (is_break);
8495 error_at (loc, "break statement used with OpenMP for loop");
8496 return NULL_TREE;
8498 default:
8499 gcc_unreachable ();
8502 if (skip)
8503 return NULL_TREE;
8505 if (!is_break)
8506 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8508 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8511 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8513 static void
8514 emit_side_effect_warnings (location_t loc, tree expr)
8516 if (expr == error_mark_node)
8518 else if (!TREE_SIDE_EFFECTS (expr))
8520 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8521 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8523 else
8524 warn_if_unused_value (expr, loc);
8527 /* Process an expression as if it were a complete statement. Emit
8528 diagnostics, but do not call ADD_STMT. LOC is the location of the
8529 statement. */
8531 tree
8532 c_process_expr_stmt (location_t loc, tree expr)
8534 if (!expr)
8535 return NULL_TREE;
8537 expr = c_fully_fold (expr, false, NULL);
8539 if (warn_sequence_point)
8540 verify_sequence_points (expr);
8542 if (TREE_TYPE (expr) != error_mark_node
8543 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8544 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8545 error_at (loc, "expression statement has incomplete type");
8547 /* If we're not processing a statement expression, warn about unused values.
8548 Warnings for statement expressions will be emitted later, once we figure
8549 out which is the result. */
8550 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8551 && warn_unused_value)
8552 emit_side_effect_warnings (loc, expr);
8554 /* If the expression is not of a type to which we cannot assign a line
8555 number, wrap the thing in a no-op NOP_EXPR. */
8556 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8558 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8559 SET_EXPR_LOCATION (expr, loc);
8562 return expr;
8565 /* Emit an expression as a statement. LOC is the location of the
8566 expression. */
8568 tree
8569 c_finish_expr_stmt (location_t loc, tree expr)
8571 if (expr)
8572 return add_stmt (c_process_expr_stmt (loc, expr));
8573 else
8574 return NULL;
8577 /* Do the opposite and emit a statement as an expression. To begin,
8578 create a new binding level and return it. */
8580 tree
8581 c_begin_stmt_expr (void)
8583 tree ret;
8585 /* We must force a BLOCK for this level so that, if it is not expanded
8586 later, there is a way to turn off the entire subtree of blocks that
8587 are contained in it. */
8588 keep_next_level ();
8589 ret = c_begin_compound_stmt (true);
8591 c_bindings_start_stmt_expr (c_switch_stack == NULL
8592 ? NULL
8593 : c_switch_stack->bindings);
8595 /* Mark the current statement list as belonging to a statement list. */
8596 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8598 return ret;
8601 /* LOC is the location of the compound statement to which this body
8602 belongs. */
8604 tree
8605 c_finish_stmt_expr (location_t loc, tree body)
8607 tree last, type, tmp, val;
8608 tree *last_p;
8610 body = c_end_compound_stmt (loc, body, true);
8612 c_bindings_end_stmt_expr (c_switch_stack == NULL
8613 ? NULL
8614 : c_switch_stack->bindings);
8616 /* Locate the last statement in BODY. See c_end_compound_stmt
8617 about always returning a BIND_EXPR. */
8618 last_p = &BIND_EXPR_BODY (body);
8619 last = BIND_EXPR_BODY (body);
8621 continue_searching:
8622 if (TREE_CODE (last) == STATEMENT_LIST)
8624 tree_stmt_iterator i;
8626 /* This can happen with degenerate cases like ({ }). No value. */
8627 if (!TREE_SIDE_EFFECTS (last))
8628 return body;
8630 /* If we're supposed to generate side effects warnings, process
8631 all of the statements except the last. */
8632 if (warn_unused_value)
8634 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8636 location_t tloc;
8637 tree t = tsi_stmt (i);
8639 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
8640 emit_side_effect_warnings (tloc, t);
8643 else
8644 i = tsi_last (last);
8645 last_p = tsi_stmt_ptr (i);
8646 last = *last_p;
8649 /* If the end of the list is exception related, then the list was split
8650 by a call to push_cleanup. Continue searching. */
8651 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8652 || TREE_CODE (last) == TRY_CATCH_EXPR)
8654 last_p = &TREE_OPERAND (last, 0);
8655 last = *last_p;
8656 goto continue_searching;
8659 /* In the case that the BIND_EXPR is not necessary, return the
8660 expression out from inside it. */
8661 if (last == error_mark_node
8662 || (last == BIND_EXPR_BODY (body)
8663 && BIND_EXPR_VARS (body) == NULL))
8665 /* Even if this looks constant, do not allow it in a constant
8666 expression. */
8667 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8668 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8669 /* Do not warn if the return value of a statement expression is
8670 unused. */
8671 TREE_NO_WARNING (last) = 1;
8672 return last;
8675 /* Extract the type of said expression. */
8676 type = TREE_TYPE (last);
8678 /* If we're not returning a value at all, then the BIND_EXPR that
8679 we already have is a fine expression to return. */
8680 if (!type || VOID_TYPE_P (type))
8681 return body;
8683 /* Now that we've located the expression containing the value, it seems
8684 silly to make voidify_wrapper_expr repeat the process. Create a
8685 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8686 tmp = create_tmp_var_raw (type, NULL);
8688 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8689 tree_expr_nonnegative_p giving up immediately. */
8690 val = last;
8691 if (TREE_CODE (val) == NOP_EXPR
8692 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8693 val = TREE_OPERAND (val, 0);
8695 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8696 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
8699 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8700 SET_EXPR_LOCATION (t, loc);
8701 return t;
8705 /* Begin and end compound statements. This is as simple as pushing
8706 and popping new statement lists from the tree. */
8708 tree
8709 c_begin_compound_stmt (bool do_scope)
8711 tree stmt = push_stmt_list ();
8712 if (do_scope)
8713 push_scope ();
8714 return stmt;
8717 /* End a compound statement. STMT is the statement. LOC is the
8718 location of the compound statement-- this is usually the location
8719 of the opening brace. */
8721 tree
8722 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
8724 tree block = NULL;
8726 if (do_scope)
8728 if (c_dialect_objc ())
8729 objc_clear_super_receiver ();
8730 block = pop_scope ();
8733 stmt = pop_stmt_list (stmt);
8734 stmt = c_build_bind_expr (loc, block, stmt);
8736 /* If this compound statement is nested immediately inside a statement
8737 expression, then force a BIND_EXPR to be created. Otherwise we'll
8738 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8739 STATEMENT_LISTs merge, and thus we can lose track of what statement
8740 was really last. */
8741 if (cur_stmt_list
8742 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8743 && TREE_CODE (stmt) != BIND_EXPR)
8745 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8746 TREE_SIDE_EFFECTS (stmt) = 1;
8747 SET_EXPR_LOCATION (stmt, loc);
8750 return stmt;
8753 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8754 when the current scope is exited. EH_ONLY is true when this is not
8755 meant to apply to normal control flow transfer. */
8757 void
8758 push_cleanup (tree decl, tree cleanup, bool eh_only)
8760 enum tree_code code;
8761 tree stmt, list;
8762 bool stmt_expr;
8764 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8765 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
8766 add_stmt (stmt);
8767 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8768 list = push_stmt_list ();
8769 TREE_OPERAND (stmt, 0) = list;
8770 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8773 /* Build a binary-operation expression without default conversions.
8774 CODE is the kind of expression to build.
8775 LOCATION is the operator's location.
8776 This function differs from `build' in several ways:
8777 the data type of the result is computed and recorded in it,
8778 warnings are generated if arg data types are invalid,
8779 special handling for addition and subtraction of pointers is known,
8780 and some optimization is done (operations on narrow ints
8781 are done in the narrower type when that gives the same result).
8782 Constant folding is also done before the result is returned.
8784 Note that the operands will never have enumeral types, or function
8785 or array types, because either they will have the default conversions
8786 performed or they have both just been converted to some other type in which
8787 the arithmetic is to be done. */
8789 tree
8790 build_binary_op (location_t location, enum tree_code code,
8791 tree orig_op0, tree orig_op1, int convert_p)
8793 tree type0, type1, orig_type0, orig_type1;
8794 tree eptype;
8795 enum tree_code code0, code1;
8796 tree op0, op1;
8797 tree ret = error_mark_node;
8798 const char *invalid_op_diag;
8799 bool op0_int_operands, op1_int_operands;
8800 bool int_const, int_const_or_overflow, int_operands;
8802 /* Expression code to give to the expression when it is built.
8803 Normally this is CODE, which is what the caller asked for,
8804 but in some special cases we change it. */
8805 enum tree_code resultcode = code;
8807 /* Data type in which the computation is to be performed.
8808 In the simplest cases this is the common type of the arguments. */
8809 tree result_type = NULL;
8811 /* When the computation is in excess precision, the type of the
8812 final EXCESS_PRECISION_EXPR. */
8813 tree real_result_type = NULL;
8815 /* Nonzero means operands have already been type-converted
8816 in whatever way is necessary.
8817 Zero means they need to be converted to RESULT_TYPE. */
8818 int converted = 0;
8820 /* Nonzero means create the expression with this type, rather than
8821 RESULT_TYPE. */
8822 tree build_type = 0;
8824 /* Nonzero means after finally constructing the expression
8825 convert it to this type. */
8826 tree final_type = 0;
8828 /* Nonzero if this is an operation like MIN or MAX which can
8829 safely be computed in short if both args are promoted shorts.
8830 Also implies COMMON.
8831 -1 indicates a bitwise operation; this makes a difference
8832 in the exact conditions for when it is safe to do the operation
8833 in a narrower mode. */
8834 int shorten = 0;
8836 /* Nonzero if this is a comparison operation;
8837 if both args are promoted shorts, compare the original shorts.
8838 Also implies COMMON. */
8839 int short_compare = 0;
8841 /* Nonzero if this is a right-shift operation, which can be computed on the
8842 original short and then promoted if the operand is a promoted short. */
8843 int short_shift = 0;
8845 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8846 int common = 0;
8848 /* True means types are compatible as far as ObjC is concerned. */
8849 bool objc_ok;
8851 /* True means this is an arithmetic operation that may need excess
8852 precision. */
8853 bool may_need_excess_precision;
8855 if (location == UNKNOWN_LOCATION)
8856 location = input_location;
8858 op0 = orig_op0;
8859 op1 = orig_op1;
8861 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8862 if (op0_int_operands)
8863 op0 = remove_c_maybe_const_expr (op0);
8864 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8865 if (op1_int_operands)
8866 op1 = remove_c_maybe_const_expr (op1);
8867 int_operands = (op0_int_operands && op1_int_operands);
8868 if (int_operands)
8870 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8871 && TREE_CODE (orig_op1) == INTEGER_CST);
8872 int_const = (int_const_or_overflow
8873 && !TREE_OVERFLOW (orig_op0)
8874 && !TREE_OVERFLOW (orig_op1));
8876 else
8877 int_const = int_const_or_overflow = false;
8879 if (convert_p)
8881 op0 = default_conversion (op0);
8882 op1 = default_conversion (op1);
8885 orig_type0 = type0 = TREE_TYPE (op0);
8886 orig_type1 = type1 = TREE_TYPE (op1);
8888 /* The expression codes of the data types of the arguments tell us
8889 whether the arguments are integers, floating, pointers, etc. */
8890 code0 = TREE_CODE (type0);
8891 code1 = TREE_CODE (type1);
8893 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8894 STRIP_TYPE_NOPS (op0);
8895 STRIP_TYPE_NOPS (op1);
8897 /* If an error was already reported for one of the arguments,
8898 avoid reporting another error. */
8900 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8901 return error_mark_node;
8903 if ((invalid_op_diag
8904 = targetm.invalid_binary_op (code, type0, type1)))
8906 error_at (location, invalid_op_diag);
8907 return error_mark_node;
8910 switch (code)
8912 case PLUS_EXPR:
8913 case MINUS_EXPR:
8914 case MULT_EXPR:
8915 case TRUNC_DIV_EXPR:
8916 case CEIL_DIV_EXPR:
8917 case FLOOR_DIV_EXPR:
8918 case ROUND_DIV_EXPR:
8919 case EXACT_DIV_EXPR:
8920 may_need_excess_precision = true;
8921 break;
8922 default:
8923 may_need_excess_precision = false;
8924 break;
8926 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8928 op0 = TREE_OPERAND (op0, 0);
8929 type0 = TREE_TYPE (op0);
8931 else if (may_need_excess_precision
8932 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8934 type0 = eptype;
8935 op0 = convert (eptype, op0);
8937 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8939 op1 = TREE_OPERAND (op1, 0);
8940 type1 = TREE_TYPE (op1);
8942 else if (may_need_excess_precision
8943 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8945 type1 = eptype;
8946 op1 = convert (eptype, op1);
8949 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8951 switch (code)
8953 case PLUS_EXPR:
8954 /* Handle the pointer + int case. */
8955 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8957 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8958 goto return_build_binary_op;
8960 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8962 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8963 goto return_build_binary_op;
8965 else
8966 common = 1;
8967 break;
8969 case MINUS_EXPR:
8970 /* Subtraction of two similar pointers.
8971 We must subtract them as integers, then divide by object size. */
8972 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8973 && comp_target_types (location, type0, type1))
8975 ret = pointer_diff (location, op0, op1);
8976 goto return_build_binary_op;
8978 /* Handle pointer minus int. Just like pointer plus int. */
8979 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8981 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8982 goto return_build_binary_op;
8984 else
8985 common = 1;
8986 break;
8988 case MULT_EXPR:
8989 common = 1;
8990 break;
8992 case TRUNC_DIV_EXPR:
8993 case CEIL_DIV_EXPR:
8994 case FLOOR_DIV_EXPR:
8995 case ROUND_DIV_EXPR:
8996 case EXACT_DIV_EXPR:
8997 warn_for_div_by_zero (location, op1);
8999 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9000 || code0 == FIXED_POINT_TYPE
9001 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9002 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9003 || code1 == FIXED_POINT_TYPE
9004 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9006 enum tree_code tcode0 = code0, tcode1 = code1;
9008 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9009 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9010 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9011 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9013 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9014 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9015 resultcode = RDIV_EXPR;
9016 else
9017 /* Although it would be tempting to shorten always here, that
9018 loses on some targets, since the modulo instruction is
9019 undefined if the quotient can't be represented in the
9020 computation mode. We shorten only if unsigned or if
9021 dividing by something we know != -1. */
9022 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9023 || (TREE_CODE (op1) == INTEGER_CST
9024 && !integer_all_onesp (op1)));
9025 common = 1;
9027 break;
9029 case BIT_AND_EXPR:
9030 case BIT_IOR_EXPR:
9031 case BIT_XOR_EXPR:
9032 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9033 shorten = -1;
9034 /* Allow vector types which are not floating point types. */
9035 else if (code0 == VECTOR_TYPE
9036 && code1 == VECTOR_TYPE
9037 && !VECTOR_FLOAT_TYPE_P (type0)
9038 && !VECTOR_FLOAT_TYPE_P (type1))
9039 common = 1;
9040 break;
9042 case TRUNC_MOD_EXPR:
9043 case FLOOR_MOD_EXPR:
9044 warn_for_div_by_zero (location, op1);
9046 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9047 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9048 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9049 common = 1;
9050 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9052 /* Although it would be tempting to shorten always here, that loses
9053 on some targets, since the modulo instruction is undefined if the
9054 quotient can't be represented in the computation mode. We shorten
9055 only if unsigned or if dividing by something we know != -1. */
9056 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9057 || (TREE_CODE (op1) == INTEGER_CST
9058 && !integer_all_onesp (op1)));
9059 common = 1;
9061 break;
9063 case TRUTH_ANDIF_EXPR:
9064 case TRUTH_ORIF_EXPR:
9065 case TRUTH_AND_EXPR:
9066 case TRUTH_OR_EXPR:
9067 case TRUTH_XOR_EXPR:
9068 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9069 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9070 || code0 == FIXED_POINT_TYPE)
9071 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9072 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9073 || code1 == FIXED_POINT_TYPE))
9075 /* Result of these operations is always an int,
9076 but that does not mean the operands should be
9077 converted to ints! */
9078 result_type = integer_type_node;
9079 op0 = c_common_truthvalue_conversion (location, op0);
9080 op1 = c_common_truthvalue_conversion (location, op1);
9081 converted = 1;
9083 if (code == TRUTH_ANDIF_EXPR)
9085 int_const_or_overflow = (int_operands
9086 && TREE_CODE (orig_op0) == INTEGER_CST
9087 && (op0 == truthvalue_false_node
9088 || TREE_CODE (orig_op1) == INTEGER_CST));
9089 int_const = (int_const_or_overflow
9090 && !TREE_OVERFLOW (orig_op0)
9091 && (op0 == truthvalue_false_node
9092 || !TREE_OVERFLOW (orig_op1)));
9094 else if (code == TRUTH_ORIF_EXPR)
9096 int_const_or_overflow = (int_operands
9097 && TREE_CODE (orig_op0) == INTEGER_CST
9098 && (op0 == truthvalue_true_node
9099 || TREE_CODE (orig_op1) == INTEGER_CST));
9100 int_const = (int_const_or_overflow
9101 && !TREE_OVERFLOW (orig_op0)
9102 && (op0 == truthvalue_true_node
9103 || !TREE_OVERFLOW (orig_op1)));
9105 break;
9107 /* Shift operations: result has same type as first operand;
9108 always convert second operand to int.
9109 Also set SHORT_SHIFT if shifting rightward. */
9111 case RSHIFT_EXPR:
9112 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9113 && code1 == INTEGER_TYPE)
9115 if (TREE_CODE (op1) == INTEGER_CST)
9117 if (tree_int_cst_sgn (op1) < 0)
9119 int_const = false;
9120 if (c_inhibit_evaluation_warnings == 0)
9121 warning (0, "right shift count is negative");
9123 else
9125 if (!integer_zerop (op1))
9126 short_shift = 1;
9128 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9130 int_const = false;
9131 if (c_inhibit_evaluation_warnings == 0)
9132 warning (0, "right shift count >= width of type");
9137 /* Use the type of the value to be shifted. */
9138 result_type = type0;
9139 /* Convert the shift-count to an integer, regardless of size
9140 of value being shifted. */
9141 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9142 op1 = convert (integer_type_node, op1);
9143 /* Avoid converting op1 to result_type later. */
9144 converted = 1;
9146 break;
9148 case LSHIFT_EXPR:
9149 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9150 && code1 == INTEGER_TYPE)
9152 if (TREE_CODE (op1) == INTEGER_CST)
9154 if (tree_int_cst_sgn (op1) < 0)
9156 int_const = false;
9157 if (c_inhibit_evaluation_warnings == 0)
9158 warning (0, "left shift count is negative");
9161 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9163 int_const = false;
9164 if (c_inhibit_evaluation_warnings == 0)
9165 warning (0, "left shift count >= width of type");
9169 /* Use the type of the value to be shifted. */
9170 result_type = type0;
9171 /* Convert the shift-count to an integer, regardless of size
9172 of value being shifted. */
9173 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9174 op1 = convert (integer_type_node, op1);
9175 /* Avoid converting op1 to result_type later. */
9176 converted = 1;
9178 break;
9180 case EQ_EXPR:
9181 case NE_EXPR:
9182 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9183 warning_at (location,
9184 OPT_Wfloat_equal,
9185 "comparing floating point with == or != is unsafe");
9186 /* Result of comparison is always int,
9187 but don't convert the args to int! */
9188 build_type = integer_type_node;
9189 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9190 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9191 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9192 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9193 short_compare = 1;
9194 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9196 tree tt0 = TREE_TYPE (type0);
9197 tree tt1 = TREE_TYPE (type1);
9198 /* Anything compares with void *. void * compares with anything.
9199 Otherwise, the targets must be compatible
9200 and both must be object or both incomplete. */
9201 if (comp_target_types (location, type0, type1))
9202 result_type = common_pointer_type (type0, type1);
9203 else if (VOID_TYPE_P (tt0))
9205 /* op0 != orig_op0 detects the case of something
9206 whose value is 0 but which isn't a valid null ptr const. */
9207 if (pedantic && !null_pointer_constant_p (orig_op0)
9208 && TREE_CODE (tt1) == FUNCTION_TYPE)
9209 pedwarn (location, OPT_pedantic, "ISO C forbids "
9210 "comparison of %<void *%> with function pointer");
9212 else if (VOID_TYPE_P (tt1))
9214 if (pedantic && !null_pointer_constant_p (orig_op1)
9215 && TREE_CODE (tt0) == FUNCTION_TYPE)
9216 pedwarn (location, OPT_pedantic, "ISO C forbids "
9217 "comparison of %<void *%> with function pointer");
9219 else
9220 /* Avoid warning about the volatile ObjC EH puts on decls. */
9221 if (!objc_ok)
9222 pedwarn (location, 0,
9223 "comparison of distinct pointer types lacks a cast");
9225 if (result_type == NULL_TREE)
9226 result_type = ptr_type_node;
9228 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9230 if (TREE_CODE (op0) == ADDR_EXPR
9231 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9232 warning_at (location,
9233 OPT_Waddress, "the address of %qD will never be NULL",
9234 TREE_OPERAND (op0, 0));
9235 result_type = type0;
9237 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9239 if (TREE_CODE (op1) == ADDR_EXPR
9240 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9241 warning_at (location,
9242 OPT_Waddress, "the address of %qD will never be NULL",
9243 TREE_OPERAND (op1, 0));
9244 result_type = type1;
9246 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9248 result_type = type0;
9249 pedwarn (location, 0, "comparison between pointer and integer");
9251 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9253 result_type = type1;
9254 pedwarn (location, 0, "comparison between pointer and integer");
9256 break;
9258 case LE_EXPR:
9259 case GE_EXPR:
9260 case LT_EXPR:
9261 case GT_EXPR:
9262 build_type = integer_type_node;
9263 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9264 || code0 == FIXED_POINT_TYPE)
9265 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9266 || code1 == FIXED_POINT_TYPE))
9267 short_compare = 1;
9268 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9270 if (comp_target_types (location, type0, type1))
9272 result_type = common_pointer_type (type0, type1);
9273 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9274 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9275 pedwarn (location, 0,
9276 "comparison of complete and incomplete pointers");
9277 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9278 pedwarn (location, OPT_pedantic, "ISO C forbids "
9279 "ordered comparisons of pointers to functions");
9281 else
9283 result_type = ptr_type_node;
9284 pedwarn (location, 0,
9285 "comparison of distinct pointer types lacks a cast");
9288 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9290 result_type = type0;
9291 if (pedantic)
9292 pedwarn (location, OPT_pedantic,
9293 "ordered comparison of pointer with integer zero");
9294 else if (extra_warnings)
9295 warning_at (location, OPT_Wextra,
9296 "ordered comparison of pointer with integer zero");
9298 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9300 result_type = type1;
9301 pedwarn (location, OPT_pedantic,
9302 "ordered comparison of pointer with integer zero");
9304 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9306 result_type = type0;
9307 pedwarn (location, 0, "comparison between pointer and integer");
9309 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9311 result_type = type1;
9312 pedwarn (location, 0, "comparison between pointer and integer");
9314 break;
9316 default:
9317 gcc_unreachable ();
9320 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9321 return error_mark_node;
9323 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9324 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9325 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9326 TREE_TYPE (type1))))
9328 binary_op_error (location, code, type0, type1);
9329 return error_mark_node;
9332 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9333 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9335 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9336 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9338 bool first_complex = (code0 == COMPLEX_TYPE);
9339 bool second_complex = (code1 == COMPLEX_TYPE);
9340 int none_complex = (!first_complex && !second_complex);
9342 if (shorten || common || short_compare)
9344 result_type = c_common_type (type0, type1);
9345 if (result_type == error_mark_node)
9346 return error_mark_node;
9349 if (first_complex != second_complex
9350 && (code == PLUS_EXPR
9351 || code == MINUS_EXPR
9352 || code == MULT_EXPR
9353 || (code == TRUNC_DIV_EXPR && first_complex))
9354 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9355 && flag_signed_zeros)
9357 /* An operation on mixed real/complex operands must be
9358 handled specially, but the language-independent code can
9359 more easily optimize the plain complex arithmetic if
9360 -fno-signed-zeros. */
9361 tree real_type = TREE_TYPE (result_type);
9362 tree real, imag;
9363 if (type0 != orig_type0 || type1 != orig_type1)
9365 gcc_assert (may_need_excess_precision && common);
9366 real_result_type = c_common_type (orig_type0, orig_type1);
9368 if (first_complex)
9370 if (TREE_TYPE (op0) != result_type)
9371 op0 = convert_and_check (result_type, op0);
9372 if (TREE_TYPE (op1) != real_type)
9373 op1 = convert_and_check (real_type, op1);
9375 else
9377 if (TREE_TYPE (op0) != real_type)
9378 op0 = convert_and_check (real_type, op0);
9379 if (TREE_TYPE (op1) != result_type)
9380 op1 = convert_and_check (result_type, op1);
9382 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9383 return error_mark_node;
9384 if (first_complex)
9386 op0 = c_save_expr (op0);
9387 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9388 op0, 1);
9389 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9390 op0, 1);
9391 switch (code)
9393 case MULT_EXPR:
9394 case TRUNC_DIV_EXPR:
9395 imag = build2 (resultcode, real_type, imag, op1);
9396 /* Fall through. */
9397 case PLUS_EXPR:
9398 case MINUS_EXPR:
9399 real = build2 (resultcode, real_type, real, op1);
9400 break;
9401 default:
9402 gcc_unreachable();
9405 else
9407 op1 = c_save_expr (op1);
9408 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9409 op1, 1);
9410 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9411 op1, 1);
9412 switch (code)
9414 case MULT_EXPR:
9415 imag = build2 (resultcode, real_type, op0, imag);
9416 /* Fall through. */
9417 case PLUS_EXPR:
9418 real = build2 (resultcode, real_type, op0, real);
9419 break;
9420 case MINUS_EXPR:
9421 real = build2 (resultcode, real_type, op0, real);
9422 imag = build1 (NEGATE_EXPR, real_type, imag);
9423 break;
9424 default:
9425 gcc_unreachable();
9428 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9429 goto return_build_binary_op;
9432 /* For certain operations (which identify themselves by shorten != 0)
9433 if both args were extended from the same smaller type,
9434 do the arithmetic in that type and then extend.
9436 shorten !=0 and !=1 indicates a bitwise operation.
9437 For them, this optimization is safe only if
9438 both args are zero-extended or both are sign-extended.
9439 Otherwise, we might change the result.
9440 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9441 but calculated in (unsigned short) it would be (unsigned short)-1. */
9443 if (shorten && none_complex)
9445 final_type = result_type;
9446 result_type = shorten_binary_op (result_type, op0, op1,
9447 shorten == -1);
9450 /* Shifts can be shortened if shifting right. */
9452 if (short_shift)
9454 int unsigned_arg;
9455 tree arg0 = get_narrower (op0, &unsigned_arg);
9457 final_type = result_type;
9459 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9460 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9462 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9463 /* We can shorten only if the shift count is less than the
9464 number of bits in the smaller type size. */
9465 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9466 /* We cannot drop an unsigned shift after sign-extension. */
9467 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9469 /* Do an unsigned shift if the operand was zero-extended. */
9470 result_type
9471 = c_common_signed_or_unsigned_type (unsigned_arg,
9472 TREE_TYPE (arg0));
9473 /* Convert value-to-be-shifted to that type. */
9474 if (TREE_TYPE (op0) != result_type)
9475 op0 = convert (result_type, op0);
9476 converted = 1;
9480 /* Comparison operations are shortened too but differently.
9481 They identify themselves by setting short_compare = 1. */
9483 if (short_compare)
9485 /* Don't write &op0, etc., because that would prevent op0
9486 from being kept in a register.
9487 Instead, make copies of the our local variables and
9488 pass the copies by reference, then copy them back afterward. */
9489 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9490 enum tree_code xresultcode = resultcode;
9491 tree val
9492 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9494 if (val != 0)
9496 ret = val;
9497 goto return_build_binary_op;
9500 op0 = xop0, op1 = xop1;
9501 converted = 1;
9502 resultcode = xresultcode;
9504 if (c_inhibit_evaluation_warnings == 0)
9506 bool op0_maybe_const = true;
9507 bool op1_maybe_const = true;
9508 tree orig_op0_folded, orig_op1_folded;
9510 if (in_late_binary_op)
9512 orig_op0_folded = orig_op0;
9513 orig_op1_folded = orig_op1;
9515 else
9517 /* Fold for the sake of possible warnings, as in
9518 build_conditional_expr. This requires the
9519 "original" values to be folded, not just op0 and
9520 op1. */
9521 c_inhibit_evaluation_warnings++;
9522 op0 = c_fully_fold (op0, require_constant_value,
9523 &op0_maybe_const);
9524 op1 = c_fully_fold (op1, require_constant_value,
9525 &op1_maybe_const);
9526 c_inhibit_evaluation_warnings--;
9527 orig_op0_folded = c_fully_fold (orig_op0,
9528 require_constant_value,
9529 NULL);
9530 orig_op1_folded = c_fully_fold (orig_op1,
9531 require_constant_value,
9532 NULL);
9535 if (warn_sign_compare)
9536 warn_for_sign_compare (location, orig_op0_folded,
9537 orig_op1_folded, op0, op1,
9538 result_type, resultcode);
9539 if (!in_late_binary_op)
9541 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9543 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9544 NULL, op0);
9545 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9547 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9549 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9550 NULL, op1);
9551 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9558 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9559 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9560 Then the expression will be built.
9561 It will be given type FINAL_TYPE if that is nonzero;
9562 otherwise, it will be given type RESULT_TYPE. */
9564 if (!result_type)
9566 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9567 return error_mark_node;
9570 if (!converted)
9572 if (TREE_TYPE (op0) != result_type)
9573 op0 = convert_and_check (result_type, op0);
9574 if (TREE_TYPE (op1) != result_type)
9575 op1 = convert_and_check (result_type, op1);
9577 /* This can happen if one operand has a vector type, and the other
9578 has a different type. */
9579 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9580 return error_mark_node;
9583 if (build_type == NULL_TREE)
9585 build_type = result_type;
9586 if (type0 != orig_type0 || type1 != orig_type1)
9588 gcc_assert (may_need_excess_precision && common);
9589 real_result_type = c_common_type (orig_type0, orig_type1);
9593 /* Treat expressions in initializers specially as they can't trap. */
9594 if (int_const_or_overflow)
9595 ret = (require_constant_value
9596 ? fold_build2_initializer_loc (location, resultcode, build_type,
9597 op0, op1)
9598 : fold_build2_loc (location, resultcode, build_type, op0, op1));
9599 else
9600 ret = build2 (resultcode, build_type, op0, op1);
9601 if (final_type != 0)
9602 ret = convert (final_type, ret);
9604 return_build_binary_op:
9605 gcc_assert (ret != error_mark_node);
9606 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9607 ret = (int_operands
9608 ? note_integer_operands (ret)
9609 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9610 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9611 && !in_late_binary_op)
9612 ret = note_integer_operands (ret);
9613 if (real_result_type)
9614 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9615 protected_set_expr_location (ret, location);
9616 return ret;
9620 /* Convert EXPR to be a truth-value, validating its type for this
9621 purpose. LOCATION is the source location for the expression. */
9623 tree
9624 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9626 bool int_const, int_operands;
9628 switch (TREE_CODE (TREE_TYPE (expr)))
9630 case ARRAY_TYPE:
9631 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9632 return error_mark_node;
9634 case RECORD_TYPE:
9635 error_at (location, "used struct type value where scalar is required");
9636 return error_mark_node;
9638 case UNION_TYPE:
9639 error_at (location, "used union type value where scalar is required");
9640 return error_mark_node;
9642 case FUNCTION_TYPE:
9643 gcc_unreachable ();
9645 default:
9646 break;
9649 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9650 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9651 if (int_operands)
9652 expr = remove_c_maybe_const_expr (expr);
9654 /* ??? Should we also give an error for void and vectors rather than
9655 leaving those to give errors later? */
9656 expr = c_common_truthvalue_conversion (location, expr);
9658 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9660 if (TREE_OVERFLOW (expr))
9661 return expr;
9662 else
9663 return note_integer_operands (expr);
9665 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9666 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9667 return expr;
9671 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9672 required. */
9674 tree
9675 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9677 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9679 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9680 /* Executing a compound literal inside a function reinitializes
9681 it. */
9682 if (!TREE_STATIC (decl))
9683 *se = true;
9684 return decl;
9686 else
9687 return expr;
9690 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9692 tree
9693 c_begin_omp_parallel (void)
9695 tree block;
9697 keep_next_level ();
9698 block = c_begin_compound_stmt (true);
9700 return block;
9703 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9704 statement. LOC is the location of the OMP_PARALLEL. */
9706 tree
9707 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
9709 tree stmt;
9711 block = c_end_compound_stmt (loc, block, true);
9713 stmt = make_node (OMP_PARALLEL);
9714 TREE_TYPE (stmt) = void_type_node;
9715 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9716 OMP_PARALLEL_BODY (stmt) = block;
9717 SET_EXPR_LOCATION (stmt, loc);
9719 return add_stmt (stmt);
9722 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9724 tree
9725 c_begin_omp_task (void)
9727 tree block;
9729 keep_next_level ();
9730 block = c_begin_compound_stmt (true);
9732 return block;
9735 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9736 statement. LOC is the location of the #pragma. */
9738 tree
9739 c_finish_omp_task (location_t loc, tree clauses, tree block)
9741 tree stmt;
9743 block = c_end_compound_stmt (loc, block, true);
9745 stmt = make_node (OMP_TASK);
9746 TREE_TYPE (stmt) = void_type_node;
9747 OMP_TASK_CLAUSES (stmt) = clauses;
9748 OMP_TASK_BODY (stmt) = block;
9749 SET_EXPR_LOCATION (stmt, loc);
9751 return add_stmt (stmt);
9754 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9755 Remove any elements from the list that are invalid. */
9757 tree
9758 c_finish_omp_clauses (tree clauses)
9760 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9761 tree c, t, *pc = &clauses;
9762 const char *name;
9764 bitmap_obstack_initialize (NULL);
9765 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9766 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9767 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9769 for (pc = &clauses, c = clauses; c ; c = *pc)
9771 bool remove = false;
9772 bool need_complete = false;
9773 bool need_implicitly_determined = false;
9775 switch (OMP_CLAUSE_CODE (c))
9777 case OMP_CLAUSE_SHARED:
9778 name = "shared";
9779 need_implicitly_determined = true;
9780 goto check_dup_generic;
9782 case OMP_CLAUSE_PRIVATE:
9783 name = "private";
9784 need_complete = true;
9785 need_implicitly_determined = true;
9786 goto check_dup_generic;
9788 case OMP_CLAUSE_REDUCTION:
9789 name = "reduction";
9790 need_implicitly_determined = true;
9791 t = OMP_CLAUSE_DECL (c);
9792 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9793 || POINTER_TYPE_P (TREE_TYPE (t)))
9795 error_at (OMP_CLAUSE_LOCATION (c),
9796 "%qE has invalid type for %<reduction%>", t);
9797 remove = true;
9799 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9801 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9802 const char *r_name = NULL;
9804 switch (r_code)
9806 case PLUS_EXPR:
9807 case MULT_EXPR:
9808 case MINUS_EXPR:
9809 break;
9810 case BIT_AND_EXPR:
9811 r_name = "&";
9812 break;
9813 case BIT_XOR_EXPR:
9814 r_name = "^";
9815 break;
9816 case BIT_IOR_EXPR:
9817 r_name = "|";
9818 break;
9819 case TRUTH_ANDIF_EXPR:
9820 r_name = "&&";
9821 break;
9822 case TRUTH_ORIF_EXPR:
9823 r_name = "||";
9824 break;
9825 default:
9826 gcc_unreachable ();
9828 if (r_name)
9830 error_at (OMP_CLAUSE_LOCATION (c),
9831 "%qE has invalid type for %<reduction(%s)%>",
9832 t, r_name);
9833 remove = true;
9836 goto check_dup_generic;
9838 case OMP_CLAUSE_COPYPRIVATE:
9839 name = "copyprivate";
9840 goto check_dup_generic;
9842 case OMP_CLAUSE_COPYIN:
9843 name = "copyin";
9844 t = OMP_CLAUSE_DECL (c);
9845 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9847 error_at (OMP_CLAUSE_LOCATION (c),
9848 "%qE must be %<threadprivate%> for %<copyin%>", t);
9849 remove = true;
9851 goto check_dup_generic;
9853 check_dup_generic:
9854 t = OMP_CLAUSE_DECL (c);
9855 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9857 error_at (OMP_CLAUSE_LOCATION (c),
9858 "%qE is not a variable in clause %qs", t, name);
9859 remove = true;
9861 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9862 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9863 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9865 error_at (OMP_CLAUSE_LOCATION (c),
9866 "%qE appears more than once in data clauses", t);
9867 remove = true;
9869 else
9870 bitmap_set_bit (&generic_head, DECL_UID (t));
9871 break;
9873 case OMP_CLAUSE_FIRSTPRIVATE:
9874 name = "firstprivate";
9875 t = OMP_CLAUSE_DECL (c);
9876 need_complete = true;
9877 need_implicitly_determined = true;
9878 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9880 error_at (OMP_CLAUSE_LOCATION (c),
9881 "%qE is not a variable in clause %<firstprivate%>", t);
9882 remove = true;
9884 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9885 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9887 error_at (OMP_CLAUSE_LOCATION (c),
9888 "%qE appears more than once in data clauses", t);
9889 remove = true;
9891 else
9892 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9893 break;
9895 case OMP_CLAUSE_LASTPRIVATE:
9896 name = "lastprivate";
9897 t = OMP_CLAUSE_DECL (c);
9898 need_complete = true;
9899 need_implicitly_determined = true;
9900 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9902 error_at (OMP_CLAUSE_LOCATION (c),
9903 "%qE is not a variable in clause %<lastprivate%>", t);
9904 remove = true;
9906 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9907 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9909 error_at (OMP_CLAUSE_LOCATION (c),
9910 "%qE appears more than once in data clauses", t);
9911 remove = true;
9913 else
9914 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9915 break;
9917 case OMP_CLAUSE_IF:
9918 case OMP_CLAUSE_NUM_THREADS:
9919 case OMP_CLAUSE_SCHEDULE:
9920 case OMP_CLAUSE_NOWAIT:
9921 case OMP_CLAUSE_ORDERED:
9922 case OMP_CLAUSE_DEFAULT:
9923 case OMP_CLAUSE_UNTIED:
9924 case OMP_CLAUSE_COLLAPSE:
9925 pc = &OMP_CLAUSE_CHAIN (c);
9926 continue;
9928 default:
9929 gcc_unreachable ();
9932 if (!remove)
9934 t = OMP_CLAUSE_DECL (c);
9936 if (need_complete)
9938 t = require_complete_type (t);
9939 if (t == error_mark_node)
9940 remove = true;
9943 if (need_implicitly_determined)
9945 const char *share_name = NULL;
9947 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9948 share_name = "threadprivate";
9949 else switch (c_omp_predetermined_sharing (t))
9951 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9952 break;
9953 case OMP_CLAUSE_DEFAULT_SHARED:
9954 share_name = "shared";
9955 break;
9956 case OMP_CLAUSE_DEFAULT_PRIVATE:
9957 share_name = "private";
9958 break;
9959 default:
9960 gcc_unreachable ();
9962 if (share_name)
9964 error_at (OMP_CLAUSE_LOCATION (c),
9965 "%qE is predetermined %qs for %qs",
9966 t, share_name, name);
9967 remove = true;
9972 if (remove)
9973 *pc = OMP_CLAUSE_CHAIN (c);
9974 else
9975 pc = &OMP_CLAUSE_CHAIN (c);
9978 bitmap_obstack_release (NULL);
9979 return clauses;
9982 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9983 down to the element type of an array. */
9985 tree
9986 c_build_qualified_type (tree type, int type_quals)
9988 if (type == error_mark_node)
9989 return type;
9991 if (TREE_CODE (type) == ARRAY_TYPE)
9993 tree t;
9994 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9995 type_quals);
9997 /* See if we already have an identically qualified type. */
9998 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10000 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10001 && TYPE_NAME (t) == TYPE_NAME (type)
10002 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10003 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10004 TYPE_ATTRIBUTES (type)))
10005 break;
10007 if (!t)
10009 tree domain = TYPE_DOMAIN (type);
10011 t = build_variant_type_copy (type);
10012 TREE_TYPE (t) = element_type;
10014 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10015 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10016 SET_TYPE_STRUCTURAL_EQUALITY (t);
10017 else if (TYPE_CANONICAL (element_type) != element_type
10018 || (domain && TYPE_CANONICAL (domain) != domain))
10020 tree unqualified_canon
10021 = build_array_type (TYPE_CANONICAL (element_type),
10022 domain? TYPE_CANONICAL (domain)
10023 : NULL_TREE);
10024 TYPE_CANONICAL (t)
10025 = c_build_qualified_type (unqualified_canon, type_quals);
10027 else
10028 TYPE_CANONICAL (t) = t;
10030 return t;
10033 /* A restrict-qualified pointer type must be a pointer to object or
10034 incomplete type. Note that the use of POINTER_TYPE_P also allows
10035 REFERENCE_TYPEs, which is appropriate for C++. */
10036 if ((type_quals & TYPE_QUAL_RESTRICT)
10037 && (!POINTER_TYPE_P (type)
10038 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10040 error ("invalid use of %<restrict%>");
10041 type_quals &= ~TYPE_QUAL_RESTRICT;
10044 return build_qualified_type (type, type_quals);
10047 /* Build a VA_ARG_EXPR for the C parser. */
10049 tree
10050 c_build_va_arg (location_t loc, tree expr, tree type)
10052 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10053 warning_at (loc, OPT_Wc___compat,
10054 "C++ requires promoted type, not enum type, in %<va_arg%>");
10055 return build_va_arg (loc, expr, type);