* xcoffout.h (xcoffout_source_line): Update prototype.
[official-gcc.git] / gcc / c-typeck.c
blob28fb8b8c049181a422eb06a454186ece44e5d6ee
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_assign,
53 ic_init,
54 ic_return
57 /* Whether we are building a boolean conversion inside
58 convert_for_assignment, or some other late binary operation. If
59 build_binary_op is called (from code shared with C++) in this case,
60 then the operands have already been folded and the result will not
61 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
62 bool in_late_binary_op;
64 /* The level of nesting inside "__alignof__". */
65 int in_alignof;
67 /* The level of nesting inside "sizeof". */
68 int in_sizeof;
70 /* The level of nesting inside "typeof". */
71 int in_typeof;
73 struct c_label_context_se *label_context_stack_se;
74 struct c_label_context_vm *label_context_stack_vm;
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree);
86 static int comp_target_types (tree, tree);
87 static int function_types_compatible_p (const_tree, const_tree);
88 static int type_lists_compatible_p (const_tree, const_tree);
89 static tree lookup_field (tree, tree);
90 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
91 tree);
92 static tree pointer_diff (tree, tree);
93 static tree convert_for_assignment (tree, tree, tree, enum impl_conv, bool,
94 tree, tree, int);
95 static tree valid_compound_expr_initializer (tree, tree);
96 static void push_string (const char *);
97 static void push_member_name (tree);
98 static int spelling_length (void);
99 static char *print_spelling (char *);
100 static void warning_init (int, const char *);
101 static tree digest_init (tree, tree, tree, bool, bool, int);
102 static void output_init_element (tree, tree, bool, tree, tree, int, bool);
103 static void output_pending_init_elements (int);
104 static int set_designator (int);
105 static void push_range_stack (tree);
106 static void add_pending_init (tree, tree, tree, bool);
107 static void set_nonincremental_init (void);
108 static void set_nonincremental_init_from_string (tree);
109 static tree find_init_member (tree);
110 static void readonly_error (tree, enum lvalue_use);
111 static void readonly_warning (tree, enum lvalue_use);
112 static int lvalue_or_else (const_tree, enum lvalue_use);
113 static void record_maybe_used_decl (tree);
114 static int comptypes_internal (const_tree, const_tree);
116 /* Return true if EXP is a null pointer constant, false otherwise. */
118 static bool
119 null_pointer_constant_p (const_tree expr)
121 /* This should really operate on c_expr structures, but they aren't
122 yet available everywhere required. */
123 tree type = TREE_TYPE (expr);
124 return (TREE_CODE (expr) == INTEGER_CST
125 && !TREE_OVERFLOW (expr)
126 && integer_zerop (expr)
127 && (INTEGRAL_TYPE_P (type)
128 || (TREE_CODE (type) == POINTER_TYPE
129 && VOID_TYPE_P (TREE_TYPE (type))
130 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133 /* EXPR may appear in an unevaluated part of an integer constant
134 expression, but not in an evaluated part. Wrap it in a
135 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
136 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 static tree
139 note_integer_operands (tree expr)
141 tree ret;
142 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
144 ret = copy_node (expr);
145 TREE_OVERFLOW (ret) = 1;
147 else
149 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
150 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
152 return ret;
155 /* Having checked whether EXPR may appear in an unevaluated part of an
156 integer constant expression and found that it may, remove any
157 C_MAYBE_CONST_EXPR noting this fact and return the resulting
158 expression. */
160 static inline tree
161 remove_c_maybe_const_expr (tree expr)
163 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
164 return C_MAYBE_CONST_EXPR_EXPR (expr);
165 else
166 return expr;
169 \f/* This is a cache to hold if two types are compatible or not. */
171 struct tagged_tu_seen_cache {
172 const struct tagged_tu_seen_cache * next;
173 const_tree t1;
174 const_tree t2;
175 /* The return value of tagged_types_tu_compatible_p if we had seen
176 these two types already. */
177 int val;
180 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
181 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
183 /* Do `exp = require_complete_type (exp);' to make sure exp
184 does not have an incomplete type. (That includes void types.) */
186 tree
187 require_complete_type (tree value)
189 tree type = TREE_TYPE (value);
191 if (value == error_mark_node || type == error_mark_node)
192 return error_mark_node;
194 /* First, detect a valid value with a complete type. */
195 if (COMPLETE_TYPE_P (type))
196 return value;
198 c_incomplete_type_error (value, type);
199 return error_mark_node;
202 /* Print an error message for invalid use of an incomplete type.
203 VALUE is the expression that was used (or 0 if that isn't known)
204 and TYPE is the type that was invalid. */
206 void
207 c_incomplete_type_error (const_tree value, const_tree type)
209 const char *type_code_string;
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
213 return;
215 if (value != 0 && (TREE_CODE (value) == VAR_DECL
216 || TREE_CODE (value) == PARM_DECL))
217 error ("%qD has an incomplete type", value);
218 else
220 retry:
221 /* We must print an error message. Be clever about what it says. */
223 switch (TREE_CODE (type))
225 case RECORD_TYPE:
226 type_code_string = "struct";
227 break;
229 case UNION_TYPE:
230 type_code_string = "union";
231 break;
233 case ENUMERAL_TYPE:
234 type_code_string = "enum";
235 break;
237 case VOID_TYPE:
238 error ("invalid use of void expression");
239 return;
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error ("invalid use of flexible array member");
247 return;
249 type = TREE_TYPE (type);
250 goto retry;
252 error ("invalid use of array with unspecified bounds");
253 return;
255 default:
256 gcc_unreachable ();
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error ("invalid use of undefined type %<%s %E%>",
261 type_code_string, TYPE_NAME (type));
262 else
263 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
264 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
268 /* Given a type, apply default promotions wrt unnamed function
269 arguments and return the new type. */
271 tree
272 c_type_promotes_to (tree type)
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 return double_type_node;
277 if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 return unsigned_type_node;
283 return integer_type_node;
286 return type;
289 /* Return a variant of TYPE which has all the type qualifiers of LIKE
290 as well as those of TYPE. */
292 static tree
293 qualify_type (tree type, tree like)
295 return c_build_qualified_type (type,
296 TYPE_QUALS (type) | TYPE_QUALS (like));
299 /* Return true iff the given tree T is a variable length array. */
301 bool
302 c_vla_type_p (const_tree t)
304 if (TREE_CODE (t) == ARRAY_TYPE
305 && C_TYPE_VARIABLE_SIZE (t))
306 return true;
307 return false;
310 /* Return the composite type of two compatible types.
312 We assume that comptypes has already been done and returned
313 nonzero; if that isn't so, this may crash. In particular, we
314 assume that qualifiers match. */
316 tree
317 composite_type (tree t1, tree t2)
319 enum tree_code code1;
320 enum tree_code code2;
321 tree attributes;
323 /* Save time if the two types are the same. */
325 if (t1 == t2) return t1;
327 /* If one type is nonsense, use the other. */
328 if (t1 == error_mark_node)
329 return t2;
330 if (t2 == error_mark_node)
331 return t1;
333 code1 = TREE_CODE (t1);
334 code2 = TREE_CODE (t2);
336 /* Merge the attributes. */
337 attributes = targetm.merge_type_attributes (t1, t2);
339 /* If one is an enumerated type and the other is the compatible
340 integer type, the composite type might be either of the two
341 (DR#013 question 3). For consistency, use the enumerated type as
342 the composite type. */
344 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
345 return t1;
346 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
347 return t2;
349 gcc_assert (code1 == code2);
351 switch (code1)
353 case POINTER_TYPE:
354 /* For two pointers, do this recursively on the target type. */
356 tree pointed_to_1 = TREE_TYPE (t1);
357 tree pointed_to_2 = TREE_TYPE (t2);
358 tree target = composite_type (pointed_to_1, pointed_to_2);
359 t1 = build_pointer_type (target);
360 t1 = build_type_attribute_variant (t1, attributes);
361 return qualify_type (t1, t2);
364 case ARRAY_TYPE:
366 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
367 int quals;
368 tree unqual_elt;
369 tree d1 = TYPE_DOMAIN (t1);
370 tree d2 = TYPE_DOMAIN (t2);
371 bool d1_variable, d2_variable;
372 bool d1_zero, d2_zero;
373 bool t1_complete, t2_complete;
375 /* We should not have any type quals on arrays at all. */
376 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
378 t1_complete = COMPLETE_TYPE_P (t1);
379 t2_complete = COMPLETE_TYPE_P (t2);
381 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
382 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
384 d1_variable = (!d1_zero
385 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
386 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
387 d2_variable = (!d2_zero
388 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
389 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
390 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
391 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
393 /* Save space: see if the result is identical to one of the args. */
394 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
395 && (d2_variable || d2_zero || !d1_variable))
396 return build_type_attribute_variant (t1, attributes);
397 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
398 && (d1_variable || d1_zero || !d2_variable))
399 return build_type_attribute_variant (t2, attributes);
401 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
402 return build_type_attribute_variant (t1, attributes);
403 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
404 return build_type_attribute_variant (t2, attributes);
406 /* Merge the element types, and have a size if either arg has
407 one. We may have qualifiers on the element types. To set
408 up TYPE_MAIN_VARIANT correctly, we need to form the
409 composite of the unqualified types and add the qualifiers
410 back at the end. */
411 quals = TYPE_QUALS (strip_array_types (elt));
412 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
413 t1 = build_array_type (unqual_elt,
414 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
415 && (d2_variable
416 || d2_zero
417 || !d1_variable))
418 ? t1
419 : t2));
420 /* Ensure a composite type involving a zero-length array type
421 is a zero-length type not an incomplete type. */
422 if (d1_zero && d2_zero
423 && (t1_complete || t2_complete)
424 && !COMPLETE_TYPE_P (t1))
426 TYPE_SIZE (t1) = bitsize_zero_node;
427 TYPE_SIZE_UNIT (t1) = size_zero_node;
429 t1 = c_build_qualified_type (t1, quals);
430 return build_type_attribute_variant (t1, attributes);
433 case ENUMERAL_TYPE:
434 case RECORD_TYPE:
435 case UNION_TYPE:
436 if (attributes != NULL)
438 /* Try harder not to create a new aggregate type. */
439 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
440 return t1;
441 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
442 return t2;
444 return build_type_attribute_variant (t1, attributes);
446 case FUNCTION_TYPE:
447 /* Function types: prefer the one that specified arg types.
448 If both do, merge the arg types. Also merge the return types. */
450 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
451 tree p1 = TYPE_ARG_TYPES (t1);
452 tree p2 = TYPE_ARG_TYPES (t2);
453 int len;
454 tree newargs, n;
455 int i;
457 /* Save space: see if the result is identical to one of the args. */
458 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
459 return build_type_attribute_variant (t1, attributes);
460 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
461 return build_type_attribute_variant (t2, attributes);
463 /* Simple way if one arg fails to specify argument types. */
464 if (TYPE_ARG_TYPES (t1) == 0)
466 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
467 t1 = build_type_attribute_variant (t1, attributes);
468 return qualify_type (t1, t2);
470 if (TYPE_ARG_TYPES (t2) == 0)
472 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
473 t1 = build_type_attribute_variant (t1, attributes);
474 return qualify_type (t1, t2);
477 /* If both args specify argument types, we must merge the two
478 lists, argument by argument. */
479 /* Tell global_bindings_p to return false so that variable_size
480 doesn't die on VLAs in parameter types. */
481 c_override_global_bindings_to_false = true;
483 len = list_length (p1);
484 newargs = 0;
486 for (i = 0; i < len; i++)
487 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
489 n = newargs;
491 for (; p1;
492 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
494 /* A null type means arg type is not specified.
495 Take whatever the other function type has. */
496 if (TREE_VALUE (p1) == 0)
498 TREE_VALUE (n) = TREE_VALUE (p2);
499 goto parm_done;
501 if (TREE_VALUE (p2) == 0)
503 TREE_VALUE (n) = TREE_VALUE (p1);
504 goto parm_done;
507 /* Given wait (union {union wait *u; int *i} *)
508 and wait (union wait *),
509 prefer union wait * as type of parm. */
510 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
511 && TREE_VALUE (p1) != TREE_VALUE (p2))
513 tree memb;
514 tree mv2 = TREE_VALUE (p2);
515 if (mv2 && mv2 != error_mark_node
516 && TREE_CODE (mv2) != ARRAY_TYPE)
517 mv2 = TYPE_MAIN_VARIANT (mv2);
518 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
519 memb; memb = TREE_CHAIN (memb))
521 tree mv3 = TREE_TYPE (memb);
522 if (mv3 && mv3 != error_mark_node
523 && TREE_CODE (mv3) != ARRAY_TYPE)
524 mv3 = TYPE_MAIN_VARIANT (mv3);
525 if (comptypes (mv3, mv2))
527 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
528 TREE_VALUE (p2));
529 pedwarn (input_location, OPT_pedantic,
530 "function types not truly compatible in ISO C");
531 goto parm_done;
535 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
536 && TREE_VALUE (p2) != TREE_VALUE (p1))
538 tree memb;
539 tree mv1 = TREE_VALUE (p1);
540 if (mv1 && mv1 != error_mark_node
541 && TREE_CODE (mv1) != ARRAY_TYPE)
542 mv1 = TYPE_MAIN_VARIANT (mv1);
543 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
544 memb; memb = TREE_CHAIN (memb))
546 tree mv3 = TREE_TYPE (memb);
547 if (mv3 && mv3 != error_mark_node
548 && TREE_CODE (mv3) != ARRAY_TYPE)
549 mv3 = TYPE_MAIN_VARIANT (mv3);
550 if (comptypes (mv3, mv1))
552 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
553 TREE_VALUE (p1));
554 pedwarn (input_location, OPT_pedantic,
555 "function types not truly compatible in ISO C");
556 goto parm_done;
560 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
561 parm_done: ;
564 c_override_global_bindings_to_false = false;
565 t1 = build_function_type (valtype, newargs);
566 t1 = qualify_type (t1, t2);
567 /* ... falls through ... */
570 default:
571 return build_type_attribute_variant (t1, attributes);
576 /* Return the type of a conditional expression between pointers to
577 possibly differently qualified versions of compatible types.
579 We assume that comp_target_types has already been done and returned
580 nonzero; if that isn't so, this may crash. */
582 static tree
583 common_pointer_type (tree t1, tree t2)
585 tree attributes;
586 tree pointed_to_1, mv1;
587 tree pointed_to_2, mv2;
588 tree target;
589 unsigned target_quals;
591 /* Save time if the two types are the same. */
593 if (t1 == t2) return t1;
595 /* If one type is nonsense, use the other. */
596 if (t1 == error_mark_node)
597 return t2;
598 if (t2 == error_mark_node)
599 return t1;
601 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
602 && TREE_CODE (t2) == POINTER_TYPE);
604 /* Merge the attributes. */
605 attributes = targetm.merge_type_attributes (t1, t2);
607 /* Find the composite type of the target types, and combine the
608 qualifiers of the two types' targets. Do not lose qualifiers on
609 array element types by taking the TYPE_MAIN_VARIANT. */
610 mv1 = pointed_to_1 = TREE_TYPE (t1);
611 mv2 = pointed_to_2 = TREE_TYPE (t2);
612 if (TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
614 if (TREE_CODE (mv2) != ARRAY_TYPE)
615 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
616 target = composite_type (mv1, mv2);
618 /* For function types do not merge const qualifiers, but drop them
619 if used inconsistently. The middle-end uses these to mark const
620 and noreturn functions. */
621 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
622 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
623 else
624 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
625 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
626 return build_type_attribute_variant (t1, attributes);
629 /* Return the common type for two arithmetic types under the usual
630 arithmetic conversions. The default conversions have already been
631 applied, and enumerated types converted to their compatible integer
632 types. The resulting type is unqualified and has no attributes.
634 This is the type for the result of most arithmetic operations
635 if the operands have the given two types. */
637 static tree
638 c_common_type (tree t1, tree t2)
640 enum tree_code code1;
641 enum tree_code code2;
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
649 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
650 t1 = TYPE_MAIN_VARIANT (t1);
652 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
653 t2 = TYPE_MAIN_VARIANT (t2);
655 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
656 t1 = build_type_attribute_variant (t1, NULL_TREE);
658 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
659 t2 = build_type_attribute_variant (t2, NULL_TREE);
661 /* Save time if the two types are the same. */
663 if (t1 == t2) return t1;
665 code1 = TREE_CODE (t1);
666 code2 = TREE_CODE (t2);
668 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
669 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
670 || code1 == INTEGER_TYPE);
671 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
672 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
673 || code2 == INTEGER_TYPE);
675 /* When one operand is a decimal float type, the other operand cannot be
676 a generic float type or a complex type. We also disallow vector types
677 here. */
678 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
679 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
681 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
683 error ("can%'t mix operands of decimal float and vector types");
684 return error_mark_node;
686 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
688 error ("can%'t mix operands of decimal float and complex types");
689 return error_mark_node;
691 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
693 error ("can%'t mix operands of decimal float and other float types");
694 return error_mark_node;
698 /* If one type is a vector type, return that type. (How the usual
699 arithmetic conversions apply to the vector types extension is not
700 precisely specified.) */
701 if (code1 == VECTOR_TYPE)
702 return t1;
704 if (code2 == VECTOR_TYPE)
705 return t2;
707 /* If one type is complex, form the common type of the non-complex
708 components, then make that complex. Use T1 or T2 if it is the
709 required type. */
710 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
712 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
713 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
714 tree subtype = c_common_type (subtype1, subtype2);
716 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
717 return t1;
718 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
719 return t2;
720 else
721 return build_complex_type (subtype);
724 /* If only one is real, use it as the result. */
726 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
727 return t1;
729 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
730 return t2;
732 /* If both are real and either are decimal floating point types, use
733 the decimal floating point type with the greater precision. */
735 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
737 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
738 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
739 return dfloat128_type_node;
740 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
741 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
742 return dfloat64_type_node;
743 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
744 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
745 return dfloat32_type_node;
748 /* Deal with fixed-point types. */
749 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
751 unsigned int unsignedp = 0, satp = 0;
752 enum machine_mode m1, m2;
753 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
755 m1 = TYPE_MODE (t1);
756 m2 = TYPE_MODE (t2);
758 /* If one input type is saturating, the result type is saturating. */
759 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
760 satp = 1;
762 /* If both fixed-point types are unsigned, the result type is unsigned.
763 When mixing fixed-point and integer types, follow the sign of the
764 fixed-point type.
765 Otherwise, the result type is signed. */
766 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
767 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
768 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
769 && TYPE_UNSIGNED (t1))
770 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
771 && TYPE_UNSIGNED (t2)))
772 unsignedp = 1;
774 /* The result type is signed. */
775 if (unsignedp == 0)
777 /* If the input type is unsigned, we need to convert to the
778 signed type. */
779 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
781 enum mode_class mclass = (enum mode_class) 0;
782 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
783 mclass = MODE_FRACT;
784 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
785 mclass = MODE_ACCUM;
786 else
787 gcc_unreachable ();
788 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
790 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
792 enum mode_class mclass = (enum mode_class) 0;
793 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
794 mclass = MODE_FRACT;
795 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
796 mclass = MODE_ACCUM;
797 else
798 gcc_unreachable ();
799 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
803 if (code1 == FIXED_POINT_TYPE)
805 fbit1 = GET_MODE_FBIT (m1);
806 ibit1 = GET_MODE_IBIT (m1);
808 else
810 fbit1 = 0;
811 /* Signed integers need to subtract one sign bit. */
812 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
815 if (code2 == FIXED_POINT_TYPE)
817 fbit2 = GET_MODE_FBIT (m2);
818 ibit2 = GET_MODE_IBIT (m2);
820 else
822 fbit2 = 0;
823 /* Signed integers need to subtract one sign bit. */
824 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
827 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
828 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
829 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
830 satp);
833 /* Both real or both integers; use the one with greater precision. */
835 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
836 return t1;
837 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
838 return t2;
840 /* Same precision. Prefer long longs to longs to ints when the
841 same precision, following the C99 rules on integer type rank
842 (which are equivalent to the C90 rules for C90 types). */
844 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
845 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
846 return long_long_unsigned_type_node;
848 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
849 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
851 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
852 return long_long_unsigned_type_node;
853 else
854 return long_long_integer_type_node;
857 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
858 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
859 return long_unsigned_type_node;
861 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
862 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
864 /* But preserve unsignedness from the other type,
865 since long cannot hold all the values of an unsigned int. */
866 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
867 return long_unsigned_type_node;
868 else
869 return long_integer_type_node;
872 /* Likewise, prefer long double to double even if same size. */
873 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
874 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
875 return long_double_type_node;
877 /* Otherwise prefer the unsigned one. */
879 if (TYPE_UNSIGNED (t1))
880 return t1;
881 else
882 return t2;
885 /* Wrapper around c_common_type that is used by c-common.c and other
886 front end optimizations that remove promotions. ENUMERAL_TYPEs
887 are allowed here and are converted to their compatible integer types.
888 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
889 preferably a non-Boolean type as the common type. */
890 tree
891 common_type (tree t1, tree t2)
893 if (TREE_CODE (t1) == ENUMERAL_TYPE)
894 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
895 if (TREE_CODE (t2) == ENUMERAL_TYPE)
896 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
898 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
899 if (TREE_CODE (t1) == BOOLEAN_TYPE
900 && TREE_CODE (t2) == BOOLEAN_TYPE)
901 return boolean_type_node;
903 /* If either type is BOOLEAN_TYPE, then return the other. */
904 if (TREE_CODE (t1) == BOOLEAN_TYPE)
905 return t2;
906 if (TREE_CODE (t2) == BOOLEAN_TYPE)
907 return t1;
909 return c_common_type (t1, t2);
912 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
913 or various other operations. Return 2 if they are compatible
914 but a warning may be needed if you use them together. */
917 comptypes (tree type1, tree type2)
919 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
920 int val;
922 val = comptypes_internal (type1, type2);
923 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
925 return val;
928 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
929 or various other operations. Return 2 if they are compatible
930 but a warning may be needed if you use them together. This
931 differs from comptypes, in that we don't free the seen types. */
933 static int
934 comptypes_internal (const_tree type1, const_tree type2)
936 const_tree t1 = type1;
937 const_tree t2 = type2;
938 int attrval, val;
940 /* Suppress errors caused by previously reported errors. */
942 if (t1 == t2 || !t1 || !t2
943 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
944 return 1;
946 /* If either type is the internal version of sizetype, return the
947 language version. */
948 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
949 && TYPE_ORIG_SIZE_TYPE (t1))
950 t1 = TYPE_ORIG_SIZE_TYPE (t1);
952 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
953 && TYPE_ORIG_SIZE_TYPE (t2))
954 t2 = TYPE_ORIG_SIZE_TYPE (t2);
957 /* Enumerated types are compatible with integer types, but this is
958 not transitive: two enumerated types in the same translation unit
959 are compatible with each other only if they are the same type. */
961 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
962 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
963 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
964 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
966 if (t1 == t2)
967 return 1;
969 /* Different classes of types can't be compatible. */
971 if (TREE_CODE (t1) != TREE_CODE (t2))
972 return 0;
974 /* Qualifiers must match. C99 6.7.3p9 */
976 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
977 return 0;
979 /* Allow for two different type nodes which have essentially the same
980 definition. Note that we already checked for equality of the type
981 qualifiers (just above). */
983 if (TREE_CODE (t1) != ARRAY_TYPE
984 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
985 return 1;
987 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
988 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
989 return 0;
991 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
992 val = 0;
994 switch (TREE_CODE (t1))
996 case POINTER_TYPE:
997 /* Do not remove mode or aliasing information. */
998 if (TYPE_MODE (t1) != TYPE_MODE (t2)
999 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1000 break;
1001 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1002 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
1003 break;
1005 case FUNCTION_TYPE:
1006 val = function_types_compatible_p (t1, t2);
1007 break;
1009 case ARRAY_TYPE:
1011 tree d1 = TYPE_DOMAIN (t1);
1012 tree d2 = TYPE_DOMAIN (t2);
1013 bool d1_variable, d2_variable;
1014 bool d1_zero, d2_zero;
1015 val = 1;
1017 /* Target types must match incl. qualifiers. */
1018 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1019 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
1020 return 0;
1022 /* Sizes must match unless one is missing or variable. */
1023 if (d1 == 0 || d2 == 0 || d1 == d2)
1024 break;
1026 d1_zero = !TYPE_MAX_VALUE (d1);
1027 d2_zero = !TYPE_MAX_VALUE (d2);
1029 d1_variable = (!d1_zero
1030 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1031 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1032 d2_variable = (!d2_zero
1033 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1034 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1035 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1036 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1038 if (d1_variable || d2_variable)
1039 break;
1040 if (d1_zero && d2_zero)
1041 break;
1042 if (d1_zero || d2_zero
1043 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1044 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1045 val = 0;
1047 break;
1050 case ENUMERAL_TYPE:
1051 case RECORD_TYPE:
1052 case UNION_TYPE:
1053 if (val != 1 && !same_translation_unit_p (t1, t2))
1055 tree a1 = TYPE_ATTRIBUTES (t1);
1056 tree a2 = TYPE_ATTRIBUTES (t2);
1058 if (! attribute_list_contained (a1, a2)
1059 && ! attribute_list_contained (a2, a1))
1060 break;
1062 if (attrval != 2)
1063 return tagged_types_tu_compatible_p (t1, t2);
1064 val = tagged_types_tu_compatible_p (t1, t2);
1066 break;
1068 case VECTOR_TYPE:
1069 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1070 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1071 break;
1073 default:
1074 break;
1076 return attrval == 2 && val == 1 ? 2 : val;
1079 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1080 ignoring their qualifiers. */
1082 static int
1083 comp_target_types (tree ttl, tree ttr)
1085 int val;
1086 tree mvl, mvr;
1088 /* Do not lose qualifiers on element types of array types that are
1089 pointer targets by taking their TYPE_MAIN_VARIANT. */
1090 mvl = TREE_TYPE (ttl);
1091 mvr = TREE_TYPE (ttr);
1092 if (TREE_CODE (mvl) != ARRAY_TYPE)
1093 mvl = TYPE_MAIN_VARIANT (mvl);
1094 if (TREE_CODE (mvr) != ARRAY_TYPE)
1095 mvr = TYPE_MAIN_VARIANT (mvr);
1096 val = comptypes (mvl, mvr);
1098 if (val == 2)
1099 pedwarn (input_location, OPT_pedantic, "types are not quite compatible");
1100 return val;
1103 /* Subroutines of `comptypes'. */
1105 /* Determine whether two trees derive from the same translation unit.
1106 If the CONTEXT chain ends in a null, that tree's context is still
1107 being parsed, so if two trees have context chains ending in null,
1108 they're in the same translation unit. */
1110 same_translation_unit_p (const_tree t1, const_tree t2)
1112 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1113 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1115 case tcc_declaration:
1116 t1 = DECL_CONTEXT (t1); break;
1117 case tcc_type:
1118 t1 = TYPE_CONTEXT (t1); break;
1119 case tcc_exceptional:
1120 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1121 default: gcc_unreachable ();
1124 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1125 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1127 case tcc_declaration:
1128 t2 = DECL_CONTEXT (t2); break;
1129 case tcc_type:
1130 t2 = TYPE_CONTEXT (t2); break;
1131 case tcc_exceptional:
1132 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1133 default: gcc_unreachable ();
1136 return t1 == t2;
1139 /* Allocate the seen two types, assuming that they are compatible. */
1141 static struct tagged_tu_seen_cache *
1142 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1144 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1145 tu->next = tagged_tu_seen_base;
1146 tu->t1 = t1;
1147 tu->t2 = t2;
1149 tagged_tu_seen_base = tu;
1151 /* The C standard says that two structures in different translation
1152 units are compatible with each other only if the types of their
1153 fields are compatible (among other things). We assume that they
1154 are compatible until proven otherwise when building the cache.
1155 An example where this can occur is:
1156 struct a
1158 struct a *next;
1160 If we are comparing this against a similar struct in another TU,
1161 and did not assume they were compatible, we end up with an infinite
1162 loop. */
1163 tu->val = 1;
1164 return tu;
1167 /* Free the seen types until we get to TU_TIL. */
1169 static void
1170 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1172 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1173 while (tu != tu_til)
1175 const struct tagged_tu_seen_cache *const tu1
1176 = (const struct tagged_tu_seen_cache *) tu;
1177 tu = tu1->next;
1178 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1180 tagged_tu_seen_base = tu_til;
1183 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1184 compatible. If the two types are not the same (which has been
1185 checked earlier), this can only happen when multiple translation
1186 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1187 rules. */
1189 static int
1190 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1192 tree s1, s2;
1193 bool needs_warning = false;
1195 /* We have to verify that the tags of the types are the same. This
1196 is harder than it looks because this may be a typedef, so we have
1197 to go look at the original type. It may even be a typedef of a
1198 typedef...
1199 In the case of compiler-created builtin structs the TYPE_DECL
1200 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1201 while (TYPE_NAME (t1)
1202 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1203 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1204 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1206 while (TYPE_NAME (t2)
1207 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1208 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1209 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1211 /* C90 didn't have the requirement that the two tags be the same. */
1212 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1213 return 0;
1215 /* C90 didn't say what happened if one or both of the types were
1216 incomplete; we choose to follow C99 rules here, which is that they
1217 are compatible. */
1218 if (TYPE_SIZE (t1) == NULL
1219 || TYPE_SIZE (t2) == NULL)
1220 return 1;
1223 const struct tagged_tu_seen_cache * tts_i;
1224 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1225 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1226 return tts_i->val;
1229 switch (TREE_CODE (t1))
1231 case ENUMERAL_TYPE:
1233 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1234 /* Speed up the case where the type values are in the same order. */
1235 tree tv1 = TYPE_VALUES (t1);
1236 tree tv2 = TYPE_VALUES (t2);
1238 if (tv1 == tv2)
1240 return 1;
1243 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1245 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1246 break;
1247 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1249 tu->val = 0;
1250 return 0;
1254 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1256 return 1;
1258 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1260 tu->val = 0;
1261 return 0;
1264 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1266 tu->val = 0;
1267 return 0;
1270 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1272 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1273 if (s2 == NULL
1274 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1276 tu->val = 0;
1277 return 0;
1280 return 1;
1283 case UNION_TYPE:
1285 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1286 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1288 tu->val = 0;
1289 return 0;
1292 /* Speed up the common case where the fields are in the same order. */
1293 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1294 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1296 int result;
1298 if (DECL_NAME (s1) != DECL_NAME (s2))
1299 break;
1300 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1302 if (result != 1 && !DECL_NAME (s1))
1303 break;
1304 if (result == 0)
1306 tu->val = 0;
1307 return 0;
1309 if (result == 2)
1310 needs_warning = true;
1312 if (TREE_CODE (s1) == FIELD_DECL
1313 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1314 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1316 tu->val = 0;
1317 return 0;
1320 if (!s1 && !s2)
1322 tu->val = needs_warning ? 2 : 1;
1323 return tu->val;
1326 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1328 bool ok = false;
1330 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1331 if (DECL_NAME (s1) == DECL_NAME (s2))
1333 int result;
1335 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1337 if (result != 1 && !DECL_NAME (s1))
1338 continue;
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)
1350 break;
1352 ok = true;
1353 break;
1355 if (!ok)
1357 tu->val = 0;
1358 return 0;
1361 tu->val = needs_warning ? 2 : 10;
1362 return tu->val;
1365 case RECORD_TYPE:
1367 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1369 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1370 s1 && s2;
1371 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1373 int result;
1374 if (TREE_CODE (s1) != TREE_CODE (s2)
1375 || DECL_NAME (s1) != DECL_NAME (s2))
1376 break;
1377 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1378 if (result == 0)
1379 break;
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 if (s1 && s2)
1389 tu->val = 0;
1390 else
1391 tu->val = needs_warning ? 2 : 1;
1392 return tu->val;
1395 default:
1396 gcc_unreachable ();
1400 /* Return 1 if two function types F1 and F2 are compatible.
1401 If either type specifies no argument types,
1402 the other must specify a fixed number of self-promoting arg types.
1403 Otherwise, if one type specifies only the number of arguments,
1404 the other must specify that number of self-promoting arg types.
1405 Otherwise, the argument types must match. */
1407 static int
1408 function_types_compatible_p (const_tree f1, const_tree f2)
1410 tree args1, args2;
1411 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1412 int val = 1;
1413 int val1;
1414 tree ret1, ret2;
1416 ret1 = TREE_TYPE (f1);
1417 ret2 = TREE_TYPE (f2);
1419 /* 'volatile' qualifiers on a function's return type used to mean
1420 the function is noreturn. */
1421 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1422 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1423 if (TYPE_VOLATILE (ret1))
1424 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1425 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1426 if (TYPE_VOLATILE (ret2))
1427 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1428 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1429 val = comptypes_internal (ret1, ret2);
1430 if (val == 0)
1431 return 0;
1433 args1 = TYPE_ARG_TYPES (f1);
1434 args2 = TYPE_ARG_TYPES (f2);
1436 /* An unspecified parmlist matches any specified parmlist
1437 whose argument types don't need default promotions. */
1439 if (args1 == 0)
1441 if (!self_promoting_args_p (args2))
1442 return 0;
1443 /* If one of these types comes from a non-prototype fn definition,
1444 compare that with the other type's arglist.
1445 If they don't match, ask for a warning (but no error). */
1446 if (TYPE_ACTUAL_ARG_TYPES (f1)
1447 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1448 val = 2;
1449 return val;
1451 if (args2 == 0)
1453 if (!self_promoting_args_p (args1))
1454 return 0;
1455 if (TYPE_ACTUAL_ARG_TYPES (f2)
1456 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1457 val = 2;
1458 return val;
1461 /* Both types have argument lists: compare them and propagate results. */
1462 val1 = type_lists_compatible_p (args1, args2);
1463 return val1 != 1 ? val1 : val;
1466 /* Check two lists of types for compatibility,
1467 returning 0 for incompatible, 1 for compatible,
1468 or 2 for compatible with warning. */
1470 static int
1471 type_lists_compatible_p (const_tree args1, const_tree args2)
1473 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1474 int val = 1;
1475 int newval = 0;
1477 while (1)
1479 tree a1, mv1, a2, mv2;
1480 if (args1 == 0 && args2 == 0)
1481 return val;
1482 /* If one list is shorter than the other,
1483 they fail to match. */
1484 if (args1 == 0 || args2 == 0)
1485 return 0;
1486 mv1 = a1 = TREE_VALUE (args1);
1487 mv2 = a2 = TREE_VALUE (args2);
1488 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1489 mv1 = TYPE_MAIN_VARIANT (mv1);
1490 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1491 mv2 = TYPE_MAIN_VARIANT (mv2);
1492 /* A null pointer instead of a type
1493 means there is supposed to be an argument
1494 but nothing is specified about what type it has.
1495 So match anything that self-promotes. */
1496 if (a1 == 0)
1498 if (c_type_promotes_to (a2) != a2)
1499 return 0;
1501 else if (a2 == 0)
1503 if (c_type_promotes_to (a1) != a1)
1504 return 0;
1506 /* If one of the lists has an error marker, ignore this arg. */
1507 else if (TREE_CODE (a1) == ERROR_MARK
1508 || TREE_CODE (a2) == ERROR_MARK)
1510 else if (!(newval = comptypes_internal (mv1, mv2)))
1512 /* Allow wait (union {union wait *u; int *i} *)
1513 and wait (union wait *) to be compatible. */
1514 if (TREE_CODE (a1) == UNION_TYPE
1515 && (TYPE_NAME (a1) == 0
1516 || TYPE_TRANSPARENT_UNION (a1))
1517 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1518 && tree_int_cst_equal (TYPE_SIZE (a1),
1519 TYPE_SIZE (a2)))
1521 tree memb;
1522 for (memb = TYPE_FIELDS (a1);
1523 memb; memb = TREE_CHAIN (memb))
1525 tree mv3 = TREE_TYPE (memb);
1526 if (mv3 && mv3 != error_mark_node
1527 && TREE_CODE (mv3) != ARRAY_TYPE)
1528 mv3 = TYPE_MAIN_VARIANT (mv3);
1529 if (comptypes_internal (mv3, mv2))
1530 break;
1532 if (memb == 0)
1533 return 0;
1535 else if (TREE_CODE (a2) == UNION_TYPE
1536 && (TYPE_NAME (a2) == 0
1537 || TYPE_TRANSPARENT_UNION (a2))
1538 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1539 && tree_int_cst_equal (TYPE_SIZE (a2),
1540 TYPE_SIZE (a1)))
1542 tree memb;
1543 for (memb = TYPE_FIELDS (a2);
1544 memb; memb = TREE_CHAIN (memb))
1546 tree mv3 = TREE_TYPE (memb);
1547 if (mv3 && mv3 != error_mark_node
1548 && TREE_CODE (mv3) != ARRAY_TYPE)
1549 mv3 = TYPE_MAIN_VARIANT (mv3);
1550 if (comptypes_internal (mv3, mv1))
1551 break;
1553 if (memb == 0)
1554 return 0;
1556 else
1557 return 0;
1560 /* comptypes said ok, but record if it said to warn. */
1561 if (newval > val)
1562 val = newval;
1564 args1 = TREE_CHAIN (args1);
1565 args2 = TREE_CHAIN (args2);
1569 /* Compute the size to increment a pointer by. */
1571 static tree
1572 c_size_in_bytes (const_tree type)
1574 enum tree_code code = TREE_CODE (type);
1576 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1577 return size_one_node;
1579 if (!COMPLETE_OR_VOID_TYPE_P (type))
1581 error ("arithmetic on pointer to an incomplete type");
1582 return size_one_node;
1585 /* Convert in case a char is more than one unit. */
1586 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1587 size_int (TYPE_PRECISION (char_type_node)
1588 / BITS_PER_UNIT));
1591 /* Return either DECL or its known constant value (if it has one). */
1593 tree
1594 decl_constant_value (tree decl)
1596 if (/* Don't change a variable array bound or initial value to a constant
1597 in a place where a variable is invalid. Note that DECL_INITIAL
1598 isn't valid for a PARM_DECL. */
1599 current_function_decl != 0
1600 && TREE_CODE (decl) != PARM_DECL
1601 && !TREE_THIS_VOLATILE (decl)
1602 && TREE_READONLY (decl)
1603 && DECL_INITIAL (decl) != 0
1604 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1605 /* This is invalid if initial value is not constant.
1606 If it has either a function call, a memory reference,
1607 or a variable, then re-evaluating it could give different results. */
1608 && TREE_CONSTANT (DECL_INITIAL (decl))
1609 /* Check for cases where this is sub-optimal, even though valid. */
1610 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1611 return DECL_INITIAL (decl);
1612 return decl;
1615 /* Convert the array expression EXP to a pointer. */
1616 static tree
1617 array_to_pointer_conversion (tree exp)
1619 tree orig_exp = exp;
1620 tree type = TREE_TYPE (exp);
1621 tree adr;
1622 tree restype = TREE_TYPE (type);
1623 tree ptrtype;
1625 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1627 STRIP_TYPE_NOPS (exp);
1629 if (TREE_NO_WARNING (orig_exp))
1630 TREE_NO_WARNING (exp) = 1;
1632 ptrtype = build_pointer_type (restype);
1634 if (TREE_CODE (exp) == INDIRECT_REF)
1635 return convert (ptrtype, TREE_OPERAND (exp, 0));
1637 adr = build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 1);
1638 return convert (ptrtype, adr);
1641 /* Convert the function expression EXP to a pointer. */
1642 static tree
1643 function_to_pointer_conversion (tree exp)
1645 tree orig_exp = exp;
1647 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1649 STRIP_TYPE_NOPS (exp);
1651 if (TREE_NO_WARNING (orig_exp))
1652 TREE_NO_WARNING (exp) = 1;
1654 return build_unary_op (EXPR_LOCATION (exp), ADDR_EXPR, exp, 0);
1657 /* Perform the default conversion of arrays and functions to pointers.
1658 Return the result of converting EXP. For any other expression, just
1659 return EXP. */
1661 struct c_expr
1662 default_function_array_conversion (struct c_expr exp)
1664 tree orig_exp = exp.value;
1665 tree type = TREE_TYPE (exp.value);
1666 enum tree_code code = TREE_CODE (type);
1668 switch (code)
1670 case ARRAY_TYPE:
1672 bool not_lvalue = false;
1673 bool lvalue_array_p;
1675 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1676 || CONVERT_EXPR_P (exp.value))
1677 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1679 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1680 not_lvalue = true;
1681 exp.value = TREE_OPERAND (exp.value, 0);
1684 if (TREE_NO_WARNING (orig_exp))
1685 TREE_NO_WARNING (exp.value) = 1;
1687 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1688 if (!flag_isoc99 && !lvalue_array_p)
1690 /* Before C99, non-lvalue arrays do not decay to pointers.
1691 Normally, using such an array would be invalid; but it can
1692 be used correctly inside sizeof or as a statement expression.
1693 Thus, do not give an error here; an error will result later. */
1694 return exp;
1697 exp.value = array_to_pointer_conversion (exp.value);
1699 break;
1700 case FUNCTION_TYPE:
1701 exp.value = function_to_pointer_conversion (exp.value);
1702 break;
1703 default:
1704 break;
1707 return exp;
1711 /* EXP is an expression of integer type. Apply the integer promotions
1712 to it and return the promoted value. */
1714 tree
1715 perform_integral_promotions (tree exp)
1717 tree type = TREE_TYPE (exp);
1718 enum tree_code code = TREE_CODE (type);
1720 gcc_assert (INTEGRAL_TYPE_P (type));
1722 /* Normally convert enums to int,
1723 but convert wide enums to something wider. */
1724 if (code == ENUMERAL_TYPE)
1726 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1727 TYPE_PRECISION (integer_type_node)),
1728 ((TYPE_PRECISION (type)
1729 >= TYPE_PRECISION (integer_type_node))
1730 && TYPE_UNSIGNED (type)));
1732 return convert (type, exp);
1735 /* ??? This should no longer be needed now bit-fields have their
1736 proper types. */
1737 if (TREE_CODE (exp) == COMPONENT_REF
1738 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1739 /* If it's thinner than an int, promote it like a
1740 c_promoting_integer_type_p, otherwise leave it alone. */
1741 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1742 TYPE_PRECISION (integer_type_node)))
1743 return convert (integer_type_node, exp);
1745 if (c_promoting_integer_type_p (type))
1747 /* Preserve unsignedness if not really getting any wider. */
1748 if (TYPE_UNSIGNED (type)
1749 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1750 return convert (unsigned_type_node, exp);
1752 return convert (integer_type_node, exp);
1755 return exp;
1759 /* Perform default promotions for C data used in expressions.
1760 Enumeral types or short or char are converted to int.
1761 In addition, manifest constants symbols are replaced by their values. */
1763 tree
1764 default_conversion (tree exp)
1766 tree orig_exp;
1767 tree type = TREE_TYPE (exp);
1768 enum tree_code code = TREE_CODE (type);
1769 tree promoted_type;
1771 /* Functions and arrays have been converted during parsing. */
1772 gcc_assert (code != FUNCTION_TYPE);
1773 if (code == ARRAY_TYPE)
1774 return exp;
1776 /* Constants can be used directly unless they're not loadable. */
1777 if (TREE_CODE (exp) == CONST_DECL)
1778 exp = DECL_INITIAL (exp);
1780 /* Strip no-op conversions. */
1781 orig_exp = exp;
1782 STRIP_TYPE_NOPS (exp);
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
1787 if (code == VOID_TYPE)
1789 error ("void value not ignored as it ought to be");
1790 return error_mark_node;
1793 exp = require_complete_type (exp);
1794 if (exp == error_mark_node)
1795 return error_mark_node;
1797 promoted_type = targetm.promoted_type (type);
1798 if (promoted_type)
1799 return convert (promoted_type, exp);
1801 if (INTEGRAL_TYPE_P (type))
1802 return perform_integral_promotions (exp);
1804 return exp;
1807 /* Look up COMPONENT in a structure or union DECL.
1809 If the component name is not found, returns NULL_TREE. Otherwise,
1810 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1811 stepping down the chain to the component, which is in the last
1812 TREE_VALUE of the list. Normally the list is of length one, but if
1813 the component is embedded within (nested) anonymous structures or
1814 unions, the list steps down the chain to the component. */
1816 static tree
1817 lookup_field (tree decl, tree component)
1819 tree type = TREE_TYPE (decl);
1820 tree field;
1822 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1823 to the field elements. Use a binary search on this array to quickly
1824 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1825 will always be set for structures which have many elements. */
1827 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1829 int bot, top, half;
1830 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1832 field = TYPE_FIELDS (type);
1833 bot = 0;
1834 top = TYPE_LANG_SPECIFIC (type)->s->len;
1835 while (top - bot > 1)
1837 half = (top - bot + 1) >> 1;
1838 field = field_array[bot+half];
1840 if (DECL_NAME (field) == NULL_TREE)
1842 /* Step through all anon unions in linear fashion. */
1843 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1845 field = field_array[bot++];
1846 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1847 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1849 tree anon = lookup_field (field, component);
1851 if (anon)
1852 return tree_cons (NULL_TREE, field, anon);
1856 /* Entire record is only anon unions. */
1857 if (bot > top)
1858 return NULL_TREE;
1860 /* Restart the binary search, with new lower bound. */
1861 continue;
1864 if (DECL_NAME (field) == component)
1865 break;
1866 if (DECL_NAME (field) < component)
1867 bot += half;
1868 else
1869 top = bot + half;
1872 if (DECL_NAME (field_array[bot]) == component)
1873 field = field_array[bot];
1874 else if (DECL_NAME (field) != component)
1875 return NULL_TREE;
1877 else
1879 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1881 if (DECL_NAME (field) == NULL_TREE
1882 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1883 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1885 tree anon = lookup_field (field, component);
1887 if (anon)
1888 return tree_cons (NULL_TREE, field, anon);
1891 if (DECL_NAME (field) == component)
1892 break;
1895 if (field == NULL_TREE)
1896 return NULL_TREE;
1899 return tree_cons (NULL_TREE, field, NULL_TREE);
1902 /* Make an expression to refer to the COMPONENT field of
1903 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1905 tree
1906 build_component_ref (tree datum, tree component)
1908 tree type = TREE_TYPE (datum);
1909 enum tree_code code = TREE_CODE (type);
1910 tree field = NULL;
1911 tree ref;
1912 bool datum_lvalue = lvalue_p (datum);
1914 if (!objc_is_public (datum, component))
1915 return error_mark_node;
1917 /* See if there is a field or component with name COMPONENT. */
1919 if (code == RECORD_TYPE || code == UNION_TYPE)
1921 if (!COMPLETE_TYPE_P (type))
1923 c_incomplete_type_error (NULL_TREE, type);
1924 return error_mark_node;
1927 field = lookup_field (datum, component);
1929 if (!field)
1931 error ("%qT has no member named %qE", type, component);
1932 return error_mark_node;
1935 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1936 This might be better solved in future the way the C++ front
1937 end does it - by giving the anonymous entities each a
1938 separate name and type, and then have build_component_ref
1939 recursively call itself. We can't do that here. */
1942 tree subdatum = TREE_VALUE (field);
1943 int quals;
1944 tree subtype;
1945 bool use_datum_quals;
1947 if (TREE_TYPE (subdatum) == error_mark_node)
1948 return error_mark_node;
1950 /* If this is an rvalue, it does not have qualifiers in C
1951 standard terms and we must avoid propagating such
1952 qualifiers down to a non-lvalue array that is then
1953 converted to a pointer. */
1954 use_datum_quals = (datum_lvalue
1955 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
1957 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1958 if (use_datum_quals)
1959 quals |= TYPE_QUALS (TREE_TYPE (datum));
1960 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1962 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1963 NULL_TREE);
1964 if (TREE_READONLY (subdatum)
1965 || (use_datum_quals && TREE_READONLY (datum)))
1966 TREE_READONLY (ref) = 1;
1967 if (TREE_THIS_VOLATILE (subdatum)
1968 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
1969 TREE_THIS_VOLATILE (ref) = 1;
1971 if (TREE_DEPRECATED (subdatum))
1972 warn_deprecated_use (subdatum, NULL_TREE);
1974 datum = ref;
1976 field = TREE_CHAIN (field);
1978 while (field);
1980 return ref;
1982 else if (code != ERROR_MARK)
1983 error ("request for member %qE in something not a structure or union",
1984 component);
1986 return error_mark_node;
1989 /* Given an expression PTR for a pointer, return an expression
1990 for the value pointed to.
1991 ERRORSTRING is the name of the operator to appear in error messages.
1993 LOC is the location to use for the generated tree. */
1995 tree
1996 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
1998 tree pointer = default_conversion (ptr);
1999 tree type = TREE_TYPE (pointer);
2000 tree ref;
2002 if (TREE_CODE (type) == POINTER_TYPE)
2004 if (CONVERT_EXPR_P (pointer)
2005 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2007 /* If a warning is issued, mark it to avoid duplicates from
2008 the backend. This only needs to be done at
2009 warn_strict_aliasing > 2. */
2010 if (warn_strict_aliasing > 2)
2011 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2012 type, TREE_OPERAND (pointer, 0)))
2013 TREE_NO_WARNING (pointer) = 1;
2016 if (TREE_CODE (pointer) == ADDR_EXPR
2017 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2018 == TREE_TYPE (type)))
2020 ref = TREE_OPERAND (pointer, 0);
2021 protected_set_expr_location (ref, loc);
2022 return ref;
2024 else
2026 tree t = TREE_TYPE (type);
2028 ref = build1 (INDIRECT_REF, t, pointer);
2030 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2032 error_at (loc, "dereferencing pointer to incomplete type");
2033 return error_mark_node;
2035 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2036 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2038 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2039 so that we get the proper error message if the result is used
2040 to assign to. Also, &* is supposed to be a no-op.
2041 And ANSI C seems to specify that the type of the result
2042 should be the const type. */
2043 /* A de-reference of a pointer to const is not a const. It is valid
2044 to change it via some other pointer. */
2045 TREE_READONLY (ref) = TYPE_READONLY (t);
2046 TREE_SIDE_EFFECTS (ref)
2047 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2048 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2049 protected_set_expr_location (ref, loc);
2050 return ref;
2053 else if (TREE_CODE (pointer) != ERROR_MARK)
2054 error_at (loc,
2055 "invalid type argument of %qs (have %qT)", errorstring, type);
2056 return error_mark_node;
2059 /* This handles expressions of the form "a[i]", which denotes
2060 an array reference.
2062 This is logically equivalent in C to *(a+i), but we may do it differently.
2063 If A is a variable or a member, we generate a primitive ARRAY_REF.
2064 This avoids forcing the array out of registers, and can work on
2065 arrays that are not lvalues (for example, members of structures returned
2066 by functions).
2068 LOC is the location to use for the returned expression. */
2070 tree
2071 build_array_ref (tree array, tree index, location_t loc)
2073 tree ret;
2074 bool swapped = false;
2075 if (TREE_TYPE (array) == error_mark_node
2076 || TREE_TYPE (index) == error_mark_node)
2077 return error_mark_node;
2079 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2080 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2082 tree temp;
2083 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2084 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2086 error_at (loc, "subscripted value is neither array nor pointer");
2087 return error_mark_node;
2089 temp = array;
2090 array = index;
2091 index = temp;
2092 swapped = true;
2095 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2097 error_at (loc, "array subscript is not an integer");
2098 return error_mark_node;
2101 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2103 error_at (loc, "subscripted value is pointer to function");
2104 return error_mark_node;
2107 /* ??? Existing practice has been to warn only when the char
2108 index is syntactically the index, not for char[array]. */
2109 if (!swapped)
2110 warn_array_subscript_with_type_char (index);
2112 /* Apply default promotions *after* noticing character types. */
2113 index = default_conversion (index);
2115 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2117 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2119 tree rval, type;
2121 /* An array that is indexed by a non-constant
2122 cannot be stored in a register; we must be able to do
2123 address arithmetic on its address.
2124 Likewise an array of elements of variable size. */
2125 if (TREE_CODE (index) != INTEGER_CST
2126 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2127 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2129 if (!c_mark_addressable (array))
2130 return error_mark_node;
2132 /* An array that is indexed by a constant value which is not within
2133 the array bounds cannot be stored in a register either; because we
2134 would get a crash in store_bit_field/extract_bit_field when trying
2135 to access a non-existent part of the register. */
2136 if (TREE_CODE (index) == INTEGER_CST
2137 && TYPE_DOMAIN (TREE_TYPE (array))
2138 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2140 if (!c_mark_addressable (array))
2141 return error_mark_node;
2144 if (pedantic)
2146 tree foo = array;
2147 while (TREE_CODE (foo) == COMPONENT_REF)
2148 foo = TREE_OPERAND (foo, 0);
2149 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2150 pedwarn (loc, OPT_pedantic,
2151 "ISO C forbids subscripting %<register%> array");
2152 else if (!flag_isoc99 && !lvalue_p (foo))
2153 pedwarn (loc, OPT_pedantic,
2154 "ISO C90 forbids subscripting non-lvalue array");
2157 type = TREE_TYPE (TREE_TYPE (array));
2158 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2159 /* Array ref is const/volatile if the array elements are
2160 or if the array is. */
2161 TREE_READONLY (rval)
2162 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2163 | TREE_READONLY (array));
2164 TREE_SIDE_EFFECTS (rval)
2165 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2166 | TREE_SIDE_EFFECTS (array));
2167 TREE_THIS_VOLATILE (rval)
2168 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2169 /* This was added by rms on 16 Nov 91.
2170 It fixes vol struct foo *a; a->elts[1]
2171 in an inline function.
2172 Hope it doesn't break something else. */
2173 | TREE_THIS_VOLATILE (array));
2174 ret = require_complete_type (rval);
2175 protected_set_expr_location (ret, loc);
2176 return ret;
2178 else
2180 tree ar = default_conversion (array);
2182 if (ar == error_mark_node)
2183 return ar;
2185 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2186 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2188 return build_indirect_ref
2189 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2190 "array indexing");
2194 /* Build an external reference to identifier ID. FUN indicates
2195 whether this will be used for a function call. LOC is the source
2196 location of the identifier. This sets *TYPE to the type of the
2197 identifier, which is not the same as the type of the returned value
2198 for CONST_DECLs defined as enum constants. If the type of the
2199 identifier is not available, *TYPE is set to NULL. */
2200 tree
2201 build_external_ref (tree id, int fun, location_t loc, tree *type)
2203 tree ref;
2204 tree decl = lookup_name (id);
2206 /* In Objective-C, an instance variable (ivar) may be preferred to
2207 whatever lookup_name() found. */
2208 decl = objc_lookup_ivar (decl, id);
2210 *type = NULL;
2211 if (decl && decl != error_mark_node)
2213 ref = decl;
2214 *type = TREE_TYPE (ref);
2216 else if (fun)
2217 /* Implicit function declaration. */
2218 ref = implicitly_declare (id);
2219 else if (decl == error_mark_node)
2220 /* Don't complain about something that's already been
2221 complained about. */
2222 return error_mark_node;
2223 else
2225 undeclared_variable (id, loc);
2226 return error_mark_node;
2229 if (TREE_TYPE (ref) == error_mark_node)
2230 return error_mark_node;
2232 if (TREE_DEPRECATED (ref))
2233 warn_deprecated_use (ref, NULL_TREE);
2235 /* Recursive call does not count as usage. */
2236 if (ref != current_function_decl)
2238 TREE_USED (ref) = 1;
2241 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2243 if (!in_sizeof && !in_typeof)
2244 C_DECL_USED (ref) = 1;
2245 else if (DECL_INITIAL (ref) == 0
2246 && DECL_EXTERNAL (ref)
2247 && !TREE_PUBLIC (ref))
2248 record_maybe_used_decl (ref);
2251 if (TREE_CODE (ref) == CONST_DECL)
2253 used_types_insert (TREE_TYPE (ref));
2255 if (warn_cxx_compat
2256 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2257 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2259 warning_at (loc, OPT_Wc___compat,
2260 ("enum constant defined in struct or union "
2261 "is not visible in C++"));
2262 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2265 ref = DECL_INITIAL (ref);
2266 TREE_CONSTANT (ref) = 1;
2268 else if (current_function_decl != 0
2269 && !DECL_FILE_SCOPE_P (current_function_decl)
2270 && (TREE_CODE (ref) == VAR_DECL
2271 || TREE_CODE (ref) == PARM_DECL
2272 || TREE_CODE (ref) == FUNCTION_DECL))
2274 tree context = decl_function_context (ref);
2276 if (context != 0 && context != current_function_decl)
2277 DECL_NONLOCAL (ref) = 1;
2279 /* C99 6.7.4p3: An inline definition of a function with external
2280 linkage ... shall not contain a reference to an identifier with
2281 internal linkage. */
2282 else if (current_function_decl != 0
2283 && DECL_DECLARED_INLINE_P (current_function_decl)
2284 && DECL_EXTERNAL (current_function_decl)
2285 && VAR_OR_FUNCTION_DECL_P (ref)
2286 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2287 && ! TREE_PUBLIC (ref)
2288 && DECL_CONTEXT (ref) != current_function_decl)
2289 record_inline_static (loc, current_function_decl, ref,
2290 csi_internal);
2292 return ref;
2295 /* Record details of decls possibly used inside sizeof or typeof. */
2296 struct maybe_used_decl
2298 /* The decl. */
2299 tree decl;
2300 /* The level seen at (in_sizeof + in_typeof). */
2301 int level;
2302 /* The next one at this level or above, or NULL. */
2303 struct maybe_used_decl *next;
2306 static struct maybe_used_decl *maybe_used_decls;
2308 /* Record that DECL, an undefined static function reference seen
2309 inside sizeof or typeof, might be used if the operand of sizeof is
2310 a VLA type or the operand of typeof is a variably modified
2311 type. */
2313 static void
2314 record_maybe_used_decl (tree decl)
2316 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2317 t->decl = decl;
2318 t->level = in_sizeof + in_typeof;
2319 t->next = maybe_used_decls;
2320 maybe_used_decls = t;
2323 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2324 USED is false, just discard them. If it is true, mark them used
2325 (if no longer inside sizeof or typeof) or move them to the next
2326 level up (if still inside sizeof or typeof). */
2328 void
2329 pop_maybe_used (bool used)
2331 struct maybe_used_decl *p = maybe_used_decls;
2332 int cur_level = in_sizeof + in_typeof;
2333 while (p && p->level > cur_level)
2335 if (used)
2337 if (cur_level == 0)
2338 C_DECL_USED (p->decl) = 1;
2339 else
2340 p->level = cur_level;
2342 p = p->next;
2344 if (!used || cur_level == 0)
2345 maybe_used_decls = p;
2348 /* Return the result of sizeof applied to EXPR. */
2350 struct c_expr
2351 c_expr_sizeof_expr (struct c_expr expr)
2353 struct c_expr ret;
2354 if (expr.value == error_mark_node)
2356 ret.value = error_mark_node;
2357 ret.original_code = ERROR_MARK;
2358 ret.original_type = NULL;
2359 pop_maybe_used (false);
2361 else
2363 bool expr_const_operands = true;
2364 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2365 &expr_const_operands);
2366 ret.value = c_sizeof (TREE_TYPE (folded_expr));
2367 ret.original_code = ERROR_MARK;
2368 ret.original_type = NULL;
2369 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2371 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2372 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2373 folded_expr, ret.value);
2374 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2376 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2378 return ret;
2381 /* Return the result of sizeof applied to T, a structure for the type
2382 name passed to sizeof (rather than the type itself). */
2384 struct c_expr
2385 c_expr_sizeof_type (struct c_type_name *t)
2387 tree type;
2388 struct c_expr ret;
2389 tree type_expr = NULL_TREE;
2390 bool type_expr_const = true;
2391 type = groktypename (t, &type_expr, &type_expr_const);
2392 ret.value = c_sizeof (type);
2393 ret.original_code = ERROR_MARK;
2394 ret.original_type = NULL;
2395 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2396 && c_vla_type_p (type))
2398 /* If the type is a [*] array, it is a VLA but is represented as
2399 having a size of zero. In such a case we must ensure that
2400 the result of sizeof does not get folded to a constant by
2401 c_fully_fold, because if the size is evaluated the result is
2402 not constant and so constraints on zero or negative size
2403 arrays must not be applied when this sizeof call is inside
2404 another array declarator. */
2405 if (!type_expr)
2406 type_expr = integer_zero_node;
2407 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2408 type_expr, ret.value);
2409 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2411 pop_maybe_used (type != error_mark_node
2412 ? C_TYPE_VARIABLE_SIZE (type) : false);
2413 return ret;
2416 /* Build a function call to function FUNCTION with parameters PARAMS.
2417 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2418 TREE_VALUE of each node is a parameter-expression.
2419 FUNCTION's data type may be a function type or a pointer-to-function. */
2421 tree
2422 build_function_call (tree function, tree params)
2424 VEC(tree,gc) *vec;
2425 tree ret;
2427 vec = VEC_alloc (tree, gc, list_length (params));
2428 for (; params; params = TREE_CHAIN (params))
2429 VEC_quick_push (tree, vec, TREE_VALUE (params));
2430 ret = build_function_call_vec (function, vec, NULL);
2431 VEC_free (tree, gc, vec);
2432 return ret;
2435 /* Build a function call to function FUNCTION with parameters PARAMS.
2436 ORIGTYPES, if not NULL, is a vector of types; each element is
2437 either NULL or the original type of the corresponding element in
2438 PARAMS. The original type may differ from TREE_TYPE of the
2439 parameter for enums. FUNCTION's data type may be a function type
2440 or pointer-to-function. This function changes the elements of
2441 PARAMS. */
2443 tree
2444 build_function_call_vec (tree function, VEC(tree,gc) *params,
2445 VEC(tree,gc) *origtypes)
2447 tree fntype, fundecl = 0;
2448 tree name = NULL_TREE, result;
2449 tree tem;
2450 int nargs;
2451 tree *argarray;
2454 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2455 STRIP_TYPE_NOPS (function);
2457 /* Convert anything with function type to a pointer-to-function. */
2458 if (TREE_CODE (function) == FUNCTION_DECL)
2460 /* Implement type-directed function overloading for builtins.
2461 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2462 handle all the type checking. The result is a complete expression
2463 that implements this function call. */
2464 tem = resolve_overloaded_builtin (function, params);
2465 if (tem)
2466 return tem;
2468 name = DECL_NAME (function);
2469 fundecl = function;
2471 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2472 function = function_to_pointer_conversion (function);
2474 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2475 expressions, like those used for ObjC messenger dispatches. */
2476 if (!VEC_empty (tree, params))
2477 function = objc_rewrite_function_call (function,
2478 VEC_index (tree, params, 0));
2480 function = c_fully_fold (function, false, NULL);
2482 fntype = TREE_TYPE (function);
2484 if (TREE_CODE (fntype) == ERROR_MARK)
2485 return error_mark_node;
2487 if (!(TREE_CODE (fntype) == POINTER_TYPE
2488 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2490 error ("called object %qE is not a function", function);
2491 return error_mark_node;
2494 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2495 current_function_returns_abnormally = 1;
2497 /* fntype now gets the type of function pointed to. */
2498 fntype = TREE_TYPE (fntype);
2500 /* Convert the parameters to the types declared in the
2501 function prototype, or apply default promotions. */
2503 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2504 function, fundecl);
2505 if (nargs < 0)
2506 return error_mark_node;
2508 /* Check that the function is called through a compatible prototype.
2509 If it is not, replace the call by a trap, wrapped up in a compound
2510 expression if necessary. This has the nice side-effect to prevent
2511 the tree-inliner from generating invalid assignment trees which may
2512 blow up in the RTL expander later. */
2513 if (CONVERT_EXPR_P (function)
2514 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2515 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2516 && !comptypes (fntype, TREE_TYPE (tem)))
2518 tree return_type = TREE_TYPE (fntype);
2519 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2520 NULL_TREE);
2521 int i;
2523 /* This situation leads to run-time undefined behavior. We can't,
2524 therefore, simply error unless we can prove that all possible
2525 executions of the program must execute the code. */
2526 if (warning (0, "function called through a non-compatible type"))
2527 /* We can, however, treat "undefined" any way we please.
2528 Call abort to encourage the user to fix the program. */
2529 inform (input_location, "if this code is reached, the program will abort");
2530 /* Before the abort, allow the function arguments to exit or
2531 call longjmp. */
2532 for (i = 0; i < nargs; i++)
2533 trap = build2 (COMPOUND_EXPR, void_type_node,
2534 VEC_index (tree, params, i), trap);
2536 if (VOID_TYPE_P (return_type))
2538 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2539 pedwarn (input_location, 0,
2540 "function with qualified void return type called");
2541 return trap;
2543 else
2545 tree rhs;
2547 if (AGGREGATE_TYPE_P (return_type))
2548 rhs = build_compound_literal (return_type,
2549 build_constructor (return_type, 0),
2550 false);
2551 else
2552 rhs = fold_convert (return_type, integer_zero_node);
2554 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2555 trap, rhs));
2559 argarray = VEC_address (tree, params);
2561 /* Check that arguments to builtin functions match the expectations. */
2562 if (fundecl
2563 && DECL_BUILT_IN (fundecl)
2564 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2565 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2566 return error_mark_node;
2568 /* Check that the arguments to the function are valid. */
2569 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2570 TYPE_ARG_TYPES (fntype));
2572 if (name != NULL_TREE
2573 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2575 if (require_constant_value)
2576 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2577 function, nargs, argarray);
2578 else
2579 result = fold_build_call_array (TREE_TYPE (fntype),
2580 function, nargs, argarray);
2581 if (TREE_CODE (result) == NOP_EXPR
2582 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2583 STRIP_TYPE_NOPS (result);
2585 else
2586 result = build_call_array (TREE_TYPE (fntype),
2587 function, nargs, argarray);
2589 if (VOID_TYPE_P (TREE_TYPE (result)))
2591 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2592 pedwarn (input_location, 0,
2593 "function with qualified void return type called");
2594 return result;
2596 return require_complete_type (result);
2599 /* Convert the argument expressions in the vector VALUES
2600 to the types in the list TYPELIST.
2602 If TYPELIST is exhausted, or when an element has NULL as its type,
2603 perform the default conversions.
2605 ORIGTYPES is the original types of the expressions in VALUES. This
2606 holds the type of enum values which have been converted to integral
2607 types. It may be NULL.
2609 FUNCTION is a tree for the called function. It is used only for
2610 error messages, where it is formatted with %qE.
2612 This is also where warnings about wrong number of args are generated.
2614 Returns the actual number of arguments processed (which may be less
2615 than the length of VALUES in some error situations), or -1 on
2616 failure. */
2618 static int
2619 convert_arguments (tree typelist, VEC(tree,gc) *values,
2620 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2622 tree typetail, val;
2623 unsigned int parmnum;
2624 const bool type_generic = fundecl
2625 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2626 bool type_generic_remove_excess_precision = false;
2627 tree selector;
2629 /* Change pointer to function to the function itself for
2630 diagnostics. */
2631 if (TREE_CODE (function) == ADDR_EXPR
2632 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2633 function = TREE_OPERAND (function, 0);
2635 /* Handle an ObjC selector specially for diagnostics. */
2636 selector = objc_message_selector ();
2638 /* For type-generic built-in functions, determine whether excess
2639 precision should be removed (classification) or not
2640 (comparison). */
2641 if (type_generic
2642 && DECL_BUILT_IN (fundecl)
2643 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2645 switch (DECL_FUNCTION_CODE (fundecl))
2647 case BUILT_IN_ISFINITE:
2648 case BUILT_IN_ISINF:
2649 case BUILT_IN_ISINF_SIGN:
2650 case BUILT_IN_ISNAN:
2651 case BUILT_IN_ISNORMAL:
2652 case BUILT_IN_FPCLASSIFY:
2653 type_generic_remove_excess_precision = true;
2654 break;
2656 default:
2657 type_generic_remove_excess_precision = false;
2658 break;
2662 /* Scan the given expressions and types, producing individual
2663 converted arguments. */
2665 for (typetail = typelist, parmnum = 0;
2666 VEC_iterate (tree, values, parmnum, val);
2667 ++parmnum)
2669 tree type = typetail ? TREE_VALUE (typetail) : 0;
2670 tree valtype = TREE_TYPE (val);
2671 tree rname = function;
2672 int argnum = parmnum + 1;
2673 const char *invalid_func_diag;
2674 bool excess_precision = false;
2675 bool npc;
2676 tree parmval;
2678 if (type == void_type_node)
2680 error ("too many arguments to function %qE", function);
2681 return parmnum;
2684 if (selector && argnum > 2)
2686 rname = selector;
2687 argnum -= 2;
2690 npc = null_pointer_constant_p (val);
2692 /* If there is excess precision and a prototype, convert once to
2693 the required type rather than converting via the semantic
2694 type. Likewise without a prototype a float value represented
2695 as long double should be converted once to double. But for
2696 type-generic classification functions excess precision must
2697 be removed here. */
2698 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2699 && (type || !type_generic || !type_generic_remove_excess_precision))
2701 val = TREE_OPERAND (val, 0);
2702 excess_precision = true;
2704 val = c_fully_fold (val, false, NULL);
2705 STRIP_TYPE_NOPS (val);
2707 val = require_complete_type (val);
2709 if (type != 0)
2711 /* Formal parm type is specified by a function prototype. */
2713 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2715 error ("type of formal parameter %d is incomplete", parmnum + 1);
2716 parmval = val;
2718 else
2720 tree origtype;
2722 /* Optionally warn about conversions that
2723 differ from the default conversions. */
2724 if (warn_traditional_conversion || warn_traditional)
2726 unsigned int formal_prec = TYPE_PRECISION (type);
2728 if (INTEGRAL_TYPE_P (type)
2729 && TREE_CODE (valtype) == REAL_TYPE)
2730 warning (0, "passing argument %d of %qE as integer "
2731 "rather than floating due to prototype",
2732 argnum, rname);
2733 if (INTEGRAL_TYPE_P (type)
2734 && TREE_CODE (valtype) == COMPLEX_TYPE)
2735 warning (0, "passing argument %d of %qE as integer "
2736 "rather than complex due to prototype",
2737 argnum, rname);
2738 else if (TREE_CODE (type) == COMPLEX_TYPE
2739 && TREE_CODE (valtype) == REAL_TYPE)
2740 warning (0, "passing argument %d of %qE as complex "
2741 "rather than floating due to prototype",
2742 argnum, rname);
2743 else if (TREE_CODE (type) == REAL_TYPE
2744 && INTEGRAL_TYPE_P (valtype))
2745 warning (0, "passing argument %d of %qE as floating "
2746 "rather than integer due to prototype",
2747 argnum, rname);
2748 else if (TREE_CODE (type) == COMPLEX_TYPE
2749 && INTEGRAL_TYPE_P (valtype))
2750 warning (0, "passing argument %d of %qE as complex "
2751 "rather than integer due to prototype",
2752 argnum, rname);
2753 else if (TREE_CODE (type) == REAL_TYPE
2754 && TREE_CODE (valtype) == COMPLEX_TYPE)
2755 warning (0, "passing argument %d of %qE as floating "
2756 "rather than complex due to prototype",
2757 argnum, rname);
2758 /* ??? At some point, messages should be written about
2759 conversions between complex types, but that's too messy
2760 to do now. */
2761 else if (TREE_CODE (type) == REAL_TYPE
2762 && TREE_CODE (valtype) == REAL_TYPE)
2764 /* Warn if any argument is passed as `float',
2765 since without a prototype it would be `double'. */
2766 if (formal_prec == TYPE_PRECISION (float_type_node)
2767 && type != dfloat32_type_node)
2768 warning (0, "passing argument %d of %qE as %<float%> "
2769 "rather than %<double%> due to prototype",
2770 argnum, rname);
2772 /* Warn if mismatch between argument and prototype
2773 for decimal float types. Warn of conversions with
2774 binary float types and of precision narrowing due to
2775 prototype. */
2776 else if (type != valtype
2777 && (type == dfloat32_type_node
2778 || type == dfloat64_type_node
2779 || type == dfloat128_type_node
2780 || valtype == dfloat32_type_node
2781 || valtype == dfloat64_type_node
2782 || valtype == dfloat128_type_node)
2783 && (formal_prec
2784 <= TYPE_PRECISION (valtype)
2785 || (type == dfloat128_type_node
2786 && (valtype
2787 != dfloat64_type_node
2788 && (valtype
2789 != dfloat32_type_node)))
2790 || (type == dfloat64_type_node
2791 && (valtype
2792 != dfloat32_type_node))))
2793 warning (0, "passing argument %d of %qE as %qT "
2794 "rather than %qT due to prototype",
2795 argnum, rname, type, valtype);
2798 /* Detect integer changing in width or signedness.
2799 These warnings are only activated with
2800 -Wtraditional-conversion, not with -Wtraditional. */
2801 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2802 && INTEGRAL_TYPE_P (valtype))
2804 tree would_have_been = default_conversion (val);
2805 tree type1 = TREE_TYPE (would_have_been);
2807 if (TREE_CODE (type) == ENUMERAL_TYPE
2808 && (TYPE_MAIN_VARIANT (type)
2809 == TYPE_MAIN_VARIANT (valtype)))
2810 /* No warning if function asks for enum
2811 and the actual arg is that enum type. */
2813 else if (formal_prec != TYPE_PRECISION (type1))
2814 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2815 "with different width due to prototype",
2816 argnum, rname);
2817 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2819 /* Don't complain if the formal parameter type
2820 is an enum, because we can't tell now whether
2821 the value was an enum--even the same enum. */
2822 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2824 else if (TREE_CODE (val) == INTEGER_CST
2825 && int_fits_type_p (val, type))
2826 /* Change in signedness doesn't matter
2827 if a constant value is unaffected. */
2829 /* If the value is extended from a narrower
2830 unsigned type, it doesn't matter whether we
2831 pass it as signed or unsigned; the value
2832 certainly is the same either way. */
2833 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2834 && TYPE_UNSIGNED (valtype))
2836 else if (TYPE_UNSIGNED (type))
2837 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2838 "as unsigned due to prototype",
2839 argnum, rname);
2840 else
2841 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2842 "as signed due to prototype", argnum, rname);
2846 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2847 sake of better warnings from convert_and_check. */
2848 if (excess_precision)
2849 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2850 origtype = (origtypes == NULL
2851 ? NULL_TREE
2852 : VEC_index (tree, origtypes, parmnum));
2853 parmval = convert_for_assignment (type, val, origtype,
2854 ic_argpass, npc,
2855 fundecl, function,
2856 parmnum + 1);
2858 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2859 && INTEGRAL_TYPE_P (type)
2860 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2861 parmval = default_conversion (parmval);
2864 else if (TREE_CODE (valtype) == REAL_TYPE
2865 && (TYPE_PRECISION (valtype)
2866 < TYPE_PRECISION (double_type_node))
2867 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2869 if (type_generic)
2870 parmval = val;
2871 else
2872 /* Convert `float' to `double'. */
2873 parmval = convert (double_type_node, val);
2875 else if (excess_precision && !type_generic)
2876 /* A "double" argument with excess precision being passed
2877 without a prototype or in variable arguments. */
2878 parmval = convert (valtype, val);
2879 else if ((invalid_func_diag =
2880 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2882 error (invalid_func_diag);
2883 return -1;
2885 else
2886 /* Convert `short' and `char' to full-size `int'. */
2887 parmval = default_conversion (val);
2889 VEC_replace (tree, values, parmnum, parmval);
2891 if (typetail)
2892 typetail = TREE_CHAIN (typetail);
2895 gcc_assert (parmnum == VEC_length (tree, values));
2897 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2899 error ("too few arguments to function %qE", function);
2900 return -1;
2903 return parmnum;
2906 /* This is the entry point used by the parser to build unary operators
2907 in the input. CODE, a tree_code, specifies the unary operator, and
2908 ARG is the operand. For unary plus, the C parser currently uses
2909 CONVERT_EXPR for code.
2911 LOC is the location to use for the tree generated.
2914 struct c_expr
2915 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2917 struct c_expr result;
2919 result.value = build_unary_op (loc, code, arg.value, 0);
2920 result.original_code = code;
2921 result.original_type = NULL;
2923 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2924 overflow_warning (result.value);
2926 return result;
2929 /* This is the entry point used by the parser to build binary operators
2930 in the input. CODE, a tree_code, specifies the binary operator, and
2931 ARG1 and ARG2 are the operands. In addition to constructing the
2932 expression, we check for operands that were written with other binary
2933 operators in a way that is likely to confuse the user.
2935 LOCATION is the location of the binary operator. */
2937 struct c_expr
2938 parser_build_binary_op (location_t location, enum tree_code code,
2939 struct c_expr arg1, struct c_expr arg2)
2941 struct c_expr result;
2943 enum tree_code code1 = arg1.original_code;
2944 enum tree_code code2 = arg2.original_code;
2945 tree type1 = (arg1.original_type
2946 ? arg1.original_type
2947 : TREE_TYPE (arg1.value));
2948 tree type2 = (arg2.original_type
2949 ? arg2.original_type
2950 : TREE_TYPE (arg2.value));
2952 result.value = build_binary_op (location, code,
2953 arg1.value, arg2.value, 1);
2954 result.original_code = code;
2955 result.original_type = NULL;
2957 if (TREE_CODE (result.value) == ERROR_MARK)
2958 return result;
2960 if (location != UNKNOWN_LOCATION)
2961 protected_set_expr_location (result.value, location);
2963 /* Check for cases such as x+y<<z which users are likely
2964 to misinterpret. */
2965 if (warn_parentheses)
2966 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
2968 if (warn_logical_op)
2969 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
2970 code1, arg1.value, code2, arg2.value);
2972 /* Warn about comparisons against string literals, with the exception
2973 of testing for equality or inequality of a string literal with NULL. */
2974 if (code == EQ_EXPR || code == NE_EXPR)
2976 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2977 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2978 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2980 else if (TREE_CODE_CLASS (code) == tcc_comparison
2981 && (code1 == STRING_CST || code2 == STRING_CST))
2982 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2984 if (TREE_OVERFLOW_P (result.value)
2985 && !TREE_OVERFLOW_P (arg1.value)
2986 && !TREE_OVERFLOW_P (arg2.value))
2987 overflow_warning (result.value);
2989 /* Warn about comparisons of different enum types. */
2990 if (warn_enum_compare
2991 && TREE_CODE_CLASS (code) == tcc_comparison
2992 && TREE_CODE (type1) == ENUMERAL_TYPE
2993 && TREE_CODE (type2) == ENUMERAL_TYPE
2994 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
2995 warning_at (location, OPT_Wenum_compare,
2996 "comparison between %qT and %qT",
2997 type1, type2);
2999 return result;
3002 /* Return a tree for the difference of pointers OP0 and OP1.
3003 The resulting tree has type int. */
3005 static tree
3006 pointer_diff (tree op0, tree op1)
3008 tree restype = ptrdiff_type_node;
3010 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3011 tree con0, con1, lit0, lit1;
3012 tree orig_op1 = op1;
3014 if (TREE_CODE (target_type) == VOID_TYPE)
3015 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3016 "pointer of type %<void *%> used in subtraction");
3017 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3018 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3019 "pointer to a function used in subtraction");
3021 /* If the conversion to ptrdiff_type does anything like widening or
3022 converting a partial to an integral mode, we get a convert_expression
3023 that is in the way to do any simplifications.
3024 (fold-const.c doesn't know that the extra bits won't be needed.
3025 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3026 different mode in place.)
3027 So first try to find a common term here 'by hand'; we want to cover
3028 at least the cases that occur in legal static initializers. */
3029 if (CONVERT_EXPR_P (op0)
3030 && (TYPE_PRECISION (TREE_TYPE (op0))
3031 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3032 con0 = TREE_OPERAND (op0, 0);
3033 else
3034 con0 = op0;
3035 if (CONVERT_EXPR_P (op1)
3036 && (TYPE_PRECISION (TREE_TYPE (op1))
3037 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3038 con1 = TREE_OPERAND (op1, 0);
3039 else
3040 con1 = op1;
3042 if (TREE_CODE (con0) == PLUS_EXPR)
3044 lit0 = TREE_OPERAND (con0, 1);
3045 con0 = TREE_OPERAND (con0, 0);
3047 else
3048 lit0 = integer_zero_node;
3050 if (TREE_CODE (con1) == PLUS_EXPR)
3052 lit1 = TREE_OPERAND (con1, 1);
3053 con1 = TREE_OPERAND (con1, 0);
3055 else
3056 lit1 = integer_zero_node;
3058 if (operand_equal_p (con0, con1, 0))
3060 op0 = lit0;
3061 op1 = lit1;
3065 /* First do the subtraction as integers;
3066 then drop through to build the divide operator.
3067 Do not do default conversions on the minus operator
3068 in case restype is a short type. */
3070 op0 = build_binary_op (input_location,
3071 MINUS_EXPR, convert (restype, op0),
3072 convert (restype, op1), 0);
3073 /* This generates an error if op1 is pointer to incomplete type. */
3074 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3075 error ("arithmetic on pointer to an incomplete type");
3077 /* This generates an error if op0 is pointer to incomplete type. */
3078 op1 = c_size_in_bytes (target_type);
3080 /* Divide by the size, in easiest possible way. */
3081 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3084 /* Construct and perhaps optimize a tree representation
3085 for a unary operation. CODE, a tree_code, specifies the operation
3086 and XARG is the operand.
3087 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3088 the default promotions (such as from short to int).
3089 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3090 allows non-lvalues; this is only used to handle conversion of non-lvalue
3091 arrays to pointers in C99.
3093 LOCATION is the location of the operator. */
3095 tree
3096 build_unary_op (location_t location,
3097 enum tree_code code, tree xarg, int flag)
3099 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3100 tree arg = xarg;
3101 tree argtype = 0;
3102 enum tree_code typecode;
3103 tree val;
3104 tree ret = error_mark_node;
3105 tree eptype = NULL_TREE;
3106 int noconvert = flag;
3107 const char *invalid_op_diag;
3108 bool int_operands;
3110 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3111 if (int_operands)
3112 arg = remove_c_maybe_const_expr (arg);
3114 if (code != ADDR_EXPR)
3115 arg = require_complete_type (arg);
3117 typecode = TREE_CODE (TREE_TYPE (arg));
3118 if (typecode == ERROR_MARK)
3119 return error_mark_node;
3120 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3121 typecode = INTEGER_TYPE;
3123 if ((invalid_op_diag
3124 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3126 error_at (location, invalid_op_diag);
3127 return error_mark_node;
3130 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3132 eptype = TREE_TYPE (arg);
3133 arg = TREE_OPERAND (arg, 0);
3136 switch (code)
3138 case CONVERT_EXPR:
3139 /* This is used for unary plus, because a CONVERT_EXPR
3140 is enough to prevent anybody from looking inside for
3141 associativity, but won't generate any code. */
3142 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3143 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3144 || typecode == VECTOR_TYPE))
3146 error_at (location, "wrong type argument to unary plus");
3147 return error_mark_node;
3149 else if (!noconvert)
3150 arg = default_conversion (arg);
3151 arg = non_lvalue (arg);
3152 break;
3154 case NEGATE_EXPR:
3155 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3156 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3157 || typecode == VECTOR_TYPE))
3159 error_at (location, "wrong type argument to unary minus");
3160 return error_mark_node;
3162 else if (!noconvert)
3163 arg = default_conversion (arg);
3164 break;
3166 case BIT_NOT_EXPR:
3167 /* ~ works on integer types and non float vectors. */
3168 if (typecode == INTEGER_TYPE
3169 || (typecode == VECTOR_TYPE
3170 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3172 if (!noconvert)
3173 arg = default_conversion (arg);
3175 else if (typecode == COMPLEX_TYPE)
3177 code = CONJ_EXPR;
3178 pedwarn (location, OPT_pedantic,
3179 "ISO C does not support %<~%> for complex conjugation");
3180 if (!noconvert)
3181 arg = default_conversion (arg);
3183 else
3185 error_at (location, "wrong type argument to bit-complement");
3186 return error_mark_node;
3188 break;
3190 case ABS_EXPR:
3191 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3193 error_at (location, "wrong type argument to abs");
3194 return error_mark_node;
3196 else if (!noconvert)
3197 arg = default_conversion (arg);
3198 break;
3200 case CONJ_EXPR:
3201 /* Conjugating a real value is a no-op, but allow it anyway. */
3202 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3203 || typecode == COMPLEX_TYPE))
3205 error_at (location, "wrong type argument to conjugation");
3206 return error_mark_node;
3208 else if (!noconvert)
3209 arg = default_conversion (arg);
3210 break;
3212 case TRUTH_NOT_EXPR:
3213 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3214 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3215 && typecode != COMPLEX_TYPE)
3217 error_at (location,
3218 "wrong type argument to unary exclamation mark");
3219 return error_mark_node;
3221 arg = c_objc_common_truthvalue_conversion (location, arg);
3222 ret = invert_truthvalue (arg);
3223 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3224 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3225 location = EXPR_LOCATION (ret);
3226 goto return_build_unary_op;
3228 case REALPART_EXPR:
3229 if (TREE_CODE (arg) == COMPLEX_CST)
3230 ret = TREE_REALPART (arg);
3231 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3232 ret = fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3233 else
3234 ret = arg;
3235 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3236 eptype = TREE_TYPE (eptype);
3237 goto return_build_unary_op;
3239 case IMAGPART_EXPR:
3240 if (TREE_CODE (arg) == COMPLEX_CST)
3241 ret = TREE_IMAGPART (arg);
3242 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3243 ret = fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3244 else
3245 ret = omit_one_operand (TREE_TYPE (arg), integer_zero_node, arg);
3246 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3247 eptype = TREE_TYPE (eptype);
3248 goto return_build_unary_op;
3250 case PREINCREMENT_EXPR:
3251 case POSTINCREMENT_EXPR:
3252 case PREDECREMENT_EXPR:
3253 case POSTDECREMENT_EXPR:
3255 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3257 tree inner = build_unary_op (location, code,
3258 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3259 if (inner == error_mark_node)
3260 return error_mark_node;
3261 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3262 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3263 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3264 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3265 goto return_build_unary_op;
3268 /* Complain about anything that is not a true lvalue. */
3269 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3270 || code == POSTINCREMENT_EXPR)
3271 ? lv_increment
3272 : lv_decrement)))
3273 return error_mark_node;
3275 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3277 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3278 warning_at (location, OPT_Wc___compat,
3279 "increment of enumeration value is invalid in C++");
3280 else
3281 warning_at (location, OPT_Wc___compat,
3282 "decrement of enumeration value is invalid in C++");
3285 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3286 arg = c_fully_fold (arg, false, NULL);
3288 /* Increment or decrement the real part of the value,
3289 and don't change the imaginary part. */
3290 if (typecode == COMPLEX_TYPE)
3292 tree real, imag;
3294 pedwarn (location, OPT_pedantic,
3295 "ISO C does not support %<++%> and %<--%> on complex types");
3297 arg = stabilize_reference (arg);
3298 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3299 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3300 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3301 if (real == error_mark_node || imag == error_mark_node)
3302 return error_mark_node;
3303 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3304 real, imag);
3305 goto return_build_unary_op;
3308 /* Report invalid types. */
3310 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3311 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3313 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3314 error_at (location, "wrong type argument to increment");
3315 else
3316 error_at (location, "wrong type argument to decrement");
3318 return error_mark_node;
3322 tree inc;
3324 argtype = TREE_TYPE (arg);
3326 /* Compute the increment. */
3328 if (typecode == POINTER_TYPE)
3330 /* If pointer target is an undefined struct,
3331 we just cannot know how to do the arithmetic. */
3332 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3334 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3335 error_at (location,
3336 "increment of pointer to unknown structure");
3337 else
3338 error_at (location,
3339 "decrement of pointer to unknown structure");
3341 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3342 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3344 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3345 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3346 "wrong type argument to increment");
3347 else
3348 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3349 "wrong type argument to decrement");
3352 inc = c_size_in_bytes (TREE_TYPE (argtype));
3353 inc = fold_convert (sizetype, inc);
3355 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3357 /* For signed fract types, we invert ++ to -- or
3358 -- to ++, and change inc from 1 to -1, because
3359 it is not possible to represent 1 in signed fract constants.
3360 For unsigned fract types, the result always overflows and
3361 we get an undefined (original) or the maximum value. */
3362 if (code == PREINCREMENT_EXPR)
3363 code = PREDECREMENT_EXPR;
3364 else if (code == PREDECREMENT_EXPR)
3365 code = PREINCREMENT_EXPR;
3366 else if (code == POSTINCREMENT_EXPR)
3367 code = POSTDECREMENT_EXPR;
3368 else /* code == POSTDECREMENT_EXPR */
3369 code = POSTINCREMENT_EXPR;
3371 inc = integer_minus_one_node;
3372 inc = convert (argtype, inc);
3374 else
3376 inc = integer_one_node;
3377 inc = convert (argtype, inc);
3380 /* Report a read-only lvalue. */
3381 if (TYPE_READONLY (argtype))
3383 readonly_error (arg,
3384 ((code == PREINCREMENT_EXPR
3385 || code == POSTINCREMENT_EXPR)
3386 ? lv_increment : lv_decrement));
3387 return error_mark_node;
3389 else if (TREE_READONLY (arg))
3390 readonly_warning (arg,
3391 ((code == PREINCREMENT_EXPR
3392 || code == POSTINCREMENT_EXPR)
3393 ? lv_increment : lv_decrement));
3395 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3396 val = boolean_increment (code, arg);
3397 else
3398 val = build2 (code, TREE_TYPE (arg), arg, inc);
3399 TREE_SIDE_EFFECTS (val) = 1;
3400 if (TREE_CODE (val) != code)
3401 TREE_NO_WARNING (val) = 1;
3402 ret = val;
3403 goto return_build_unary_op;
3406 case ADDR_EXPR:
3407 /* Note that this operation never does default_conversion. */
3409 /* The operand of unary '&' must be an lvalue (which excludes
3410 expressions of type void), or, in C99, the result of a [] or
3411 unary '*' operator. */
3412 if (VOID_TYPE_P (TREE_TYPE (arg))
3413 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3414 && (TREE_CODE (arg) != INDIRECT_REF
3415 || !flag_isoc99))
3416 pedwarn (location, 0, "taking address of expression of type %<void%>");
3418 /* Let &* cancel out to simplify resulting code. */
3419 if (TREE_CODE (arg) == INDIRECT_REF)
3421 /* Don't let this be an lvalue. */
3422 if (lvalue_p (TREE_OPERAND (arg, 0)))
3423 return non_lvalue (TREE_OPERAND (arg, 0));
3424 ret = TREE_OPERAND (arg, 0);
3425 goto return_build_unary_op;
3428 /* For &x[y], return x+y */
3429 if (TREE_CODE (arg) == ARRAY_REF)
3431 tree op0 = TREE_OPERAND (arg, 0);
3432 if (!c_mark_addressable (op0))
3433 return error_mark_node;
3434 return build_binary_op (location, PLUS_EXPR,
3435 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3436 ? array_to_pointer_conversion (op0)
3437 : op0),
3438 TREE_OPERAND (arg, 1), 1);
3441 /* Anything not already handled and not a true memory reference
3442 or a non-lvalue array is an error. */
3443 else if (typecode != FUNCTION_TYPE && !flag
3444 && !lvalue_or_else (arg, lv_addressof))
3445 return error_mark_node;
3447 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3448 folding later. */
3449 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3451 tree inner = build_unary_op (location, code,
3452 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3453 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3454 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3455 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3456 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3457 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3458 goto return_build_unary_op;
3461 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3462 argtype = TREE_TYPE (arg);
3464 /* If the lvalue is const or volatile, merge that into the type
3465 to which the address will point. Note that you can't get a
3466 restricted pointer by taking the address of something, so we
3467 only have to deal with `const' and `volatile' here. */
3468 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3469 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3470 argtype = c_build_type_variant (argtype,
3471 TREE_READONLY (arg),
3472 TREE_THIS_VOLATILE (arg));
3474 if (!c_mark_addressable (arg))
3475 return error_mark_node;
3477 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3478 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3480 argtype = build_pointer_type (argtype);
3482 /* ??? Cope with user tricks that amount to offsetof. Delete this
3483 when we have proper support for integer constant expressions. */
3484 val = get_base_address (arg);
3485 if (val && TREE_CODE (val) == INDIRECT_REF
3486 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3488 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3490 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3491 ret = fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3492 goto return_build_unary_op;
3495 val = build1 (ADDR_EXPR, argtype, arg);
3497 ret = val;
3498 goto return_build_unary_op;
3500 default:
3501 gcc_unreachable ();
3504 if (argtype == 0)
3505 argtype = TREE_TYPE (arg);
3506 if (TREE_CODE (arg) == INTEGER_CST)
3507 ret = (require_constant_value
3508 ? fold_build1_initializer (code, argtype, arg)
3509 : fold_build1 (code, argtype, arg));
3510 else
3511 ret = build1 (code, argtype, arg);
3512 return_build_unary_op:
3513 gcc_assert (ret != error_mark_node);
3514 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3515 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3516 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3517 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3518 ret = note_integer_operands (ret);
3519 if (eptype)
3520 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3521 protected_set_expr_location (ret, location);
3522 return ret;
3525 /* Return nonzero if REF is an lvalue valid for this language.
3526 Lvalues can be assigned, unless their type has TYPE_READONLY.
3527 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3529 bool
3530 lvalue_p (const_tree ref)
3532 const enum tree_code code = TREE_CODE (ref);
3534 switch (code)
3536 case REALPART_EXPR:
3537 case IMAGPART_EXPR:
3538 case COMPONENT_REF:
3539 return lvalue_p (TREE_OPERAND (ref, 0));
3541 case C_MAYBE_CONST_EXPR:
3542 return lvalue_p (TREE_OPERAND (ref, 1));
3544 case COMPOUND_LITERAL_EXPR:
3545 case STRING_CST:
3546 return 1;
3548 case INDIRECT_REF:
3549 case ARRAY_REF:
3550 case VAR_DECL:
3551 case PARM_DECL:
3552 case RESULT_DECL:
3553 case ERROR_MARK:
3554 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3555 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3557 case BIND_EXPR:
3558 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3560 default:
3561 return 0;
3565 /* Give an error for storing in something that is 'const'. */
3567 static void
3568 readonly_error (tree arg, enum lvalue_use use)
3570 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3571 || use == lv_asm);
3572 /* Using this macro rather than (for example) arrays of messages
3573 ensures that all the format strings are checked at compile
3574 time. */
3575 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3576 : (use == lv_increment ? (I) \
3577 : (use == lv_decrement ? (D) : (AS))))
3578 if (TREE_CODE (arg) == COMPONENT_REF)
3580 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3581 readonly_error (TREE_OPERAND (arg, 0), use);
3582 else
3583 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3584 G_("increment of read-only member %qD"),
3585 G_("decrement of read-only member %qD"),
3586 G_("read-only member %qD used as %<asm%> output")),
3587 TREE_OPERAND (arg, 1));
3589 else if (TREE_CODE (arg) == VAR_DECL)
3590 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3591 G_("increment of read-only variable %qD"),
3592 G_("decrement of read-only variable %qD"),
3593 G_("read-only variable %qD used as %<asm%> output")),
3594 arg);
3595 else
3596 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3597 G_("increment of read-only location %qE"),
3598 G_("decrement of read-only location %qE"),
3599 G_("read-only location %qE used as %<asm%> output")),
3600 arg);
3603 /* Give a warning for storing in something that is read-only in GCC
3604 terms but not const in ISO C terms. */
3606 static void
3607 readonly_warning (tree arg, enum lvalue_use use)
3609 switch (use)
3611 case lv_assign:
3612 warning (0, "assignment of read-only location %qE", arg);
3613 break;
3614 case lv_increment:
3615 warning (0, "increment of read-only location %qE", arg);
3616 break;
3617 case lv_decrement:
3618 warning (0, "decrement of read-only location %qE", arg);
3619 break;
3620 default:
3621 gcc_unreachable ();
3623 return;
3627 /* Return nonzero if REF is an lvalue valid for this language;
3628 otherwise, print an error message and return zero. USE says
3629 how the lvalue is being used and so selects the error message. */
3631 static int
3632 lvalue_or_else (const_tree ref, enum lvalue_use use)
3634 int win = lvalue_p (ref);
3636 if (!win)
3637 lvalue_error (use);
3639 return win;
3642 /* Mark EXP saying that we need to be able to take the
3643 address of it; it should not be allocated in a register.
3644 Returns true if successful. */
3646 bool
3647 c_mark_addressable (tree exp)
3649 tree x = exp;
3651 while (1)
3652 switch (TREE_CODE (x))
3654 case COMPONENT_REF:
3655 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3657 error
3658 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3659 return false;
3662 /* ... fall through ... */
3664 case ADDR_EXPR:
3665 case ARRAY_REF:
3666 case REALPART_EXPR:
3667 case IMAGPART_EXPR:
3668 x = TREE_OPERAND (x, 0);
3669 break;
3671 case COMPOUND_LITERAL_EXPR:
3672 case CONSTRUCTOR:
3673 TREE_ADDRESSABLE (x) = 1;
3674 return true;
3676 case VAR_DECL:
3677 case CONST_DECL:
3678 case PARM_DECL:
3679 case RESULT_DECL:
3680 if (C_DECL_REGISTER (x)
3681 && DECL_NONLOCAL (x))
3683 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3685 error
3686 ("global register variable %qD used in nested function", x);
3687 return false;
3689 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3691 else if (C_DECL_REGISTER (x))
3693 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3694 error ("address of global register variable %qD requested", x);
3695 else
3696 error ("address of register variable %qD requested", x);
3697 return false;
3700 /* drops in */
3701 case FUNCTION_DECL:
3702 TREE_ADDRESSABLE (x) = 1;
3703 /* drops out */
3704 default:
3705 return true;
3709 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3710 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3711 if folded to an integer constant then the unselected half may
3712 contain arbitrary operations not normally permitted in constant
3713 expressions. */
3715 tree
3716 build_conditional_expr (tree ifexp, bool ifexp_bcp, tree op1, tree op2)
3718 tree type1;
3719 tree type2;
3720 enum tree_code code1;
3721 enum tree_code code2;
3722 tree result_type = NULL;
3723 tree ep_result_type = NULL;
3724 tree orig_op1 = op1, orig_op2 = op2;
3725 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3726 bool ifexp_int_operands;
3727 tree ret;
3728 bool objc_ok;
3730 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3731 if (op1_int_operands)
3732 op1 = remove_c_maybe_const_expr (op1);
3733 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3734 if (op2_int_operands)
3735 op2 = remove_c_maybe_const_expr (op2);
3736 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3737 if (ifexp_int_operands)
3738 ifexp = remove_c_maybe_const_expr (ifexp);
3740 /* Promote both alternatives. */
3742 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3743 op1 = default_conversion (op1);
3744 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3745 op2 = default_conversion (op2);
3747 if (TREE_CODE (ifexp) == ERROR_MARK
3748 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3749 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3750 return error_mark_node;
3752 type1 = TREE_TYPE (op1);
3753 code1 = TREE_CODE (type1);
3754 type2 = TREE_TYPE (op2);
3755 code2 = TREE_CODE (type2);
3757 /* C90 does not permit non-lvalue arrays in conditional expressions.
3758 In C99 they will be pointers by now. */
3759 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3761 error ("non-lvalue array in conditional expression");
3762 return error_mark_node;
3765 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3767 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3768 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3769 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3770 || code1 == COMPLEX_TYPE)
3771 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3772 || code2 == COMPLEX_TYPE))
3774 ep_result_type = c_common_type (type1, type2);
3775 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3777 op1 = TREE_OPERAND (op1, 0);
3778 type1 = TREE_TYPE (op1);
3779 gcc_assert (TREE_CODE (type1) == code1);
3781 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3783 op2 = TREE_OPERAND (op2, 0);
3784 type2 = TREE_TYPE (op2);
3785 gcc_assert (TREE_CODE (type2) == code2);
3789 /* Quickly detect the usual case where op1 and op2 have the same type
3790 after promotion. */
3791 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3793 if (type1 == type2)
3794 result_type = type1;
3795 else
3796 result_type = TYPE_MAIN_VARIANT (type1);
3798 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3799 || code1 == COMPLEX_TYPE)
3800 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3801 || code2 == COMPLEX_TYPE))
3803 result_type = c_common_type (type1, type2);
3805 /* If -Wsign-compare, warn here if type1 and type2 have
3806 different signedness. We'll promote the signed to unsigned
3807 and later code won't know it used to be different.
3808 Do this check on the original types, so that explicit casts
3809 will be considered, but default promotions won't. */
3810 if (!skip_evaluation)
3812 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3813 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3815 if (unsigned_op1 ^ unsigned_op2)
3817 bool ovf;
3819 /* Do not warn if the result type is signed, since the
3820 signed type will only be chosen if it can represent
3821 all the values of the unsigned type. */
3822 if (!TYPE_UNSIGNED (result_type))
3823 /* OK */;
3824 else
3826 bool op1_maybe_const = true;
3827 bool op2_maybe_const = true;
3829 /* Do not warn if the signed quantity is an
3830 unsuffixed integer literal (or some static
3831 constant expression involving such literals) and
3832 it is non-negative. This warning requires the
3833 operands to be folded for best results, so do
3834 that folding in this case even without
3835 warn_sign_compare to avoid warning options
3836 possibly affecting code generation. */
3837 op1 = c_fully_fold (op1, require_constant_value,
3838 &op1_maybe_const);
3839 op2 = c_fully_fold (op2, require_constant_value,
3840 &op2_maybe_const);
3842 if (warn_sign_compare)
3844 if ((unsigned_op2
3845 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3846 || (unsigned_op1
3847 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3848 /* OK */;
3849 else
3850 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3852 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
3854 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
3855 NULL, op1);
3856 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
3858 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3860 op2 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op2),
3861 NULL, op2);
3862 C_MAYBE_CONST_EXPR_NON_CONST (op2) = !op2_maybe_const;
3868 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3870 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3871 pedwarn (input_location, OPT_pedantic,
3872 "ISO C forbids conditional expr with only one void side");
3873 result_type = void_type_node;
3875 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3877 if (comp_target_types (type1, type2))
3878 result_type = common_pointer_type (type1, type2);
3879 else if (null_pointer_constant_p (orig_op1))
3880 result_type = qualify_type (type2, type1);
3881 else if (null_pointer_constant_p (orig_op2))
3882 result_type = qualify_type (type1, type2);
3883 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3885 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3886 pedwarn (input_location, OPT_pedantic,
3887 "ISO C forbids conditional expr between "
3888 "%<void *%> and function pointer");
3889 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3890 TREE_TYPE (type2)));
3892 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3894 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3895 pedwarn (input_location, OPT_pedantic,
3896 "ISO C forbids conditional expr between "
3897 "%<void *%> and function pointer");
3898 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3899 TREE_TYPE (type1)));
3901 else
3903 if (!objc_ok)
3904 pedwarn (input_location, 0,
3905 "pointer type mismatch in conditional expression");
3906 result_type = build_pointer_type (void_type_node);
3909 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3911 if (!null_pointer_constant_p (orig_op2))
3912 pedwarn (input_location, 0,
3913 "pointer/integer type mismatch in conditional expression");
3914 else
3916 op2 = null_pointer_node;
3918 result_type = type1;
3920 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3922 if (!null_pointer_constant_p (orig_op1))
3923 pedwarn (input_location, 0,
3924 "pointer/integer type mismatch in conditional expression");
3925 else
3927 op1 = null_pointer_node;
3929 result_type = type2;
3932 if (!result_type)
3934 if (flag_cond_mismatch)
3935 result_type = void_type_node;
3936 else
3938 error ("type mismatch in conditional expression");
3939 return error_mark_node;
3943 /* Merge const and volatile flags of the incoming types. */
3944 result_type
3945 = build_type_variant (result_type,
3946 TREE_READONLY (op1) || TREE_READONLY (op2),
3947 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3949 if (result_type != TREE_TYPE (op1))
3950 op1 = convert_and_check (result_type, op1);
3951 if (result_type != TREE_TYPE (op2))
3952 op2 = convert_and_check (result_type, op2);
3954 if (ifexp_bcp && ifexp == truthvalue_true_node)
3956 op2_int_operands = true;
3957 op1 = c_fully_fold (op1, require_constant_value, NULL);
3959 if (ifexp_bcp && ifexp == truthvalue_false_node)
3961 op1_int_operands = true;
3962 op2 = c_fully_fold (op2, require_constant_value, NULL);
3964 int_const = int_operands = (ifexp_int_operands
3965 && op1_int_operands
3966 && op2_int_operands);
3967 if (int_operands)
3969 int_const = ((ifexp == truthvalue_true_node
3970 && TREE_CODE (orig_op1) == INTEGER_CST
3971 && !TREE_OVERFLOW (orig_op1))
3972 || (ifexp == truthvalue_false_node
3973 && TREE_CODE (orig_op2) == INTEGER_CST
3974 && !TREE_OVERFLOW (orig_op2)));
3976 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
3977 ret = fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3978 else
3980 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
3981 if (int_operands)
3982 ret = note_integer_operands (ret);
3984 if (ep_result_type)
3985 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
3987 return ret;
3990 /* Return a compound expression that performs two expressions and
3991 returns the value of the second of them. */
3993 tree
3994 build_compound_expr (tree expr1, tree expr2)
3996 bool expr1_int_operands, expr2_int_operands;
3997 tree eptype = NULL_TREE;
3998 tree ret;
4000 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4001 if (expr1_int_operands)
4002 expr1 = remove_c_maybe_const_expr (expr1);
4003 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4004 if (expr2_int_operands)
4005 expr2 = remove_c_maybe_const_expr (expr2);
4007 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4008 expr1 = TREE_OPERAND (expr1, 0);
4009 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4011 eptype = TREE_TYPE (expr2);
4012 expr2 = TREE_OPERAND (expr2, 0);
4015 if (!TREE_SIDE_EFFECTS (expr1))
4017 /* The left-hand operand of a comma expression is like an expression
4018 statement: with -Wunused, we should warn if it doesn't have
4019 any side-effects, unless it was explicitly cast to (void). */
4020 if (warn_unused_value)
4022 if (VOID_TYPE_P (TREE_TYPE (expr1))
4023 && CONVERT_EXPR_P (expr1))
4024 ; /* (void) a, b */
4025 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4026 && TREE_CODE (expr1) == COMPOUND_EXPR
4027 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4028 ; /* (void) a, (void) b, c */
4029 else
4030 warning (OPT_Wunused_value,
4031 "left-hand operand of comma expression has no effect");
4035 /* With -Wunused, we should also warn if the left-hand operand does have
4036 side-effects, but computes a value which is not used. For example, in
4037 `foo() + bar(), baz()' the result of the `+' operator is not used,
4038 so we should issue a warning. */
4039 else if (warn_unused_value)
4040 warn_if_unused_value (expr1, input_location);
4042 if (expr2 == error_mark_node)
4043 return error_mark_node;
4045 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4047 if (flag_isoc99
4048 && expr1_int_operands
4049 && expr2_int_operands)
4050 ret = note_integer_operands (ret);
4052 if (eptype)
4053 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4055 return ret;
4058 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4059 which we are casting. OTYPE is the type of the expression being
4060 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4061 on the command line. */
4063 static void
4064 handle_warn_cast_qual (tree type, tree otype)
4066 tree in_type = type;
4067 tree in_otype = otype;
4068 int added = 0;
4069 int discarded = 0;
4070 bool is_const;
4072 /* Check that the qualifiers on IN_TYPE are a superset of the
4073 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4074 nodes is uninteresting and we stop as soon as we hit a
4075 non-POINTER_TYPE node on either type. */
4078 in_otype = TREE_TYPE (in_otype);
4079 in_type = TREE_TYPE (in_type);
4081 /* GNU C allows cv-qualified function types. 'const' means the
4082 function is very pure, 'volatile' means it can't return. We
4083 need to warn when such qualifiers are added, not when they're
4084 taken away. */
4085 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4086 && TREE_CODE (in_type) == FUNCTION_TYPE)
4087 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4088 else
4089 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4091 while (TREE_CODE (in_type) == POINTER_TYPE
4092 && TREE_CODE (in_otype) == POINTER_TYPE);
4094 if (added)
4095 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4097 if (discarded)
4098 /* There are qualifiers present in IN_OTYPE that are not present
4099 in IN_TYPE. */
4100 warning (OPT_Wcast_qual,
4101 "cast discards qualifiers from pointer target type");
4103 if (added || discarded)
4104 return;
4106 /* A cast from **T to const **T is unsafe, because it can cause a
4107 const value to be changed with no additional warning. We only
4108 issue this warning if T is the same on both sides, and we only
4109 issue the warning if there are the same number of pointers on
4110 both sides, as otherwise the cast is clearly unsafe anyhow. A
4111 cast is unsafe when a qualifier is added at one level and const
4112 is not present at all outer levels.
4114 To issue this warning, we check at each level whether the cast
4115 adds new qualifiers not already seen. We don't need to special
4116 case function types, as they won't have the same
4117 TYPE_MAIN_VARIANT. */
4119 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4120 return;
4121 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4122 return;
4124 in_type = type;
4125 in_otype = otype;
4126 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4129 in_type = TREE_TYPE (in_type);
4130 in_otype = TREE_TYPE (in_otype);
4131 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4132 && !is_const)
4134 warning (OPT_Wcast_qual,
4135 ("new qualifiers in middle of multi-level non-const cast "
4136 "are unsafe"));
4137 break;
4139 if (is_const)
4140 is_const = TYPE_READONLY (in_type);
4142 while (TREE_CODE (in_type) == POINTER_TYPE);
4145 /* Build an expression representing a cast to type TYPE of expression EXPR. */
4147 tree
4148 build_c_cast (tree type, tree expr)
4150 tree value;
4152 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4153 expr = TREE_OPERAND (expr, 0);
4155 value = expr;
4157 if (type == error_mark_node || expr == error_mark_node)
4158 return error_mark_node;
4160 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4161 only in <protocol> qualifications. But when constructing cast expressions,
4162 the protocols do matter and must be kept around. */
4163 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4164 return build1 (NOP_EXPR, type, expr);
4166 type = TYPE_MAIN_VARIANT (type);
4168 if (TREE_CODE (type) == ARRAY_TYPE)
4170 error ("cast specifies array type");
4171 return error_mark_node;
4174 if (TREE_CODE (type) == FUNCTION_TYPE)
4176 error ("cast specifies function type");
4177 return error_mark_node;
4180 if (!VOID_TYPE_P (type))
4182 value = require_complete_type (value);
4183 if (value == error_mark_node)
4184 return error_mark_node;
4187 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4189 if (TREE_CODE (type) == RECORD_TYPE
4190 || TREE_CODE (type) == UNION_TYPE)
4191 pedwarn (input_location, OPT_pedantic,
4192 "ISO C forbids casting nonscalar to the same type");
4194 else if (TREE_CODE (type) == UNION_TYPE)
4196 tree field;
4198 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4199 if (TREE_TYPE (field) != error_mark_node
4200 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4201 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4202 break;
4204 if (field)
4206 tree t;
4208 pedwarn (input_location, OPT_pedantic,
4209 "ISO C forbids casts to union type");
4210 t = digest_init (type,
4211 build_constructor_single (type, field, value),
4212 NULL_TREE, false, true, 0);
4213 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4214 return t;
4216 error ("cast to union type from type not present in union");
4217 return error_mark_node;
4219 else
4221 tree otype, ovalue;
4223 if (type == void_type_node)
4224 return build1 (CONVERT_EXPR, type, value);
4226 otype = TREE_TYPE (value);
4228 /* Optionally warn about potentially worrisome casts. */
4229 if (warn_cast_qual
4230 && TREE_CODE (type) == POINTER_TYPE
4231 && TREE_CODE (otype) == POINTER_TYPE)
4232 handle_warn_cast_qual (type, otype);
4234 /* Warn about possible alignment problems. */
4235 if (STRICT_ALIGNMENT
4236 && TREE_CODE (type) == POINTER_TYPE
4237 && TREE_CODE (otype) == POINTER_TYPE
4238 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4239 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4240 /* Don't warn about opaque types, where the actual alignment
4241 restriction is unknown. */
4242 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4243 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4244 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4245 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4246 warning (OPT_Wcast_align,
4247 "cast increases required alignment of target type");
4249 if (TREE_CODE (type) == INTEGER_TYPE
4250 && TREE_CODE (otype) == POINTER_TYPE
4251 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4252 /* Unlike conversion of integers to pointers, where the
4253 warning is disabled for converting constants because
4254 of cases such as SIG_*, warn about converting constant
4255 pointers to integers. In some cases it may cause unwanted
4256 sign extension, and a warning is appropriate. */
4257 warning (OPT_Wpointer_to_int_cast,
4258 "cast from pointer to integer of different size");
4260 if (TREE_CODE (value) == CALL_EXPR
4261 && TREE_CODE (type) != TREE_CODE (otype))
4262 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
4263 "to non-matching type %qT", otype, type);
4265 if (TREE_CODE (type) == POINTER_TYPE
4266 && TREE_CODE (otype) == INTEGER_TYPE
4267 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4268 /* Don't warn about converting any constant. */
4269 && !TREE_CONSTANT (value))
4270 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4271 "of different size");
4273 if (warn_strict_aliasing <= 2)
4274 strict_aliasing_warning (otype, type, expr);
4276 /* If pedantic, warn for conversions between function and object
4277 pointer types, except for converting a null pointer constant
4278 to function pointer type. */
4279 if (pedantic
4280 && TREE_CODE (type) == POINTER_TYPE
4281 && TREE_CODE (otype) == POINTER_TYPE
4282 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4283 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4284 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4285 "conversion of function pointer to object pointer type");
4287 if (pedantic
4288 && TREE_CODE (type) == POINTER_TYPE
4289 && TREE_CODE (otype) == POINTER_TYPE
4290 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4291 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4292 && !null_pointer_constant_p (value))
4293 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
4294 "conversion of object pointer to function pointer type");
4296 ovalue = value;
4297 value = convert (type, value);
4299 /* Ignore any integer overflow caused by the cast. */
4300 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4302 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4304 if (!TREE_OVERFLOW (value))
4306 /* Avoid clobbering a shared constant. */
4307 value = copy_node (value);
4308 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4311 else if (TREE_OVERFLOW (value))
4312 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4313 value = build_int_cst_wide (TREE_TYPE (value),
4314 TREE_INT_CST_LOW (value),
4315 TREE_INT_CST_HIGH (value));
4319 /* Don't let a cast be an lvalue. */
4320 if (value == expr)
4321 value = non_lvalue (value);
4323 /* Don't allow the results of casting to floating-point or complex
4324 types be confused with actual constants, or casts involving
4325 integer and pointer types other than direct integer-to-integer
4326 and integer-to-pointer be confused with integer constant
4327 expressions and null pointer constants. */
4328 if (TREE_CODE (value) == REAL_CST
4329 || TREE_CODE (value) == COMPLEX_CST
4330 || (TREE_CODE (value) == INTEGER_CST
4331 && !((TREE_CODE (expr) == INTEGER_CST
4332 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4333 || TREE_CODE (expr) == REAL_CST
4334 || TREE_CODE (expr) == COMPLEX_CST)))
4335 value = build1 (NOP_EXPR, type, value);
4337 return value;
4340 /* Interpret a cast of expression EXPR to type TYPE. */
4341 tree
4342 c_cast_expr (struct c_type_name *type_name, tree expr, location_t loc)
4344 tree type;
4345 tree type_expr = NULL_TREE;
4346 bool type_expr_const = true;
4347 tree ret;
4348 int saved_wsp = warn_strict_prototypes;
4350 /* This avoids warnings about unprototyped casts on
4351 integers. E.g. "#define SIG_DFL (void(*)())0". */
4352 if (TREE_CODE (expr) == INTEGER_CST)
4353 warn_strict_prototypes = 0;
4354 type = groktypename (type_name, &type_expr, &type_expr_const);
4355 warn_strict_prototypes = saved_wsp;
4357 ret = build_c_cast (type, expr);
4358 if (type_expr)
4360 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4361 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4364 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4365 SET_EXPR_LOCATION (ret, loc);
4367 /* C++ does not permits types to be defined in a cast. */
4368 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4369 warning_at (loc, OPT_Wc___compat,
4370 "defining a type in a cast is invalid in C++");
4372 return ret;
4375 /* Build an assignment expression of lvalue LHS from value RHS.
4376 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4377 may differ from TREE_TYPE (LHS) for an enum bitfield.
4378 MODIFYCODE is the code for a binary operator that we use
4379 to combine the old value of LHS with RHS to get the new value.
4380 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4381 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4382 which may differ from TREE_TYPE (RHS) for an enum value.
4384 LOCATION is the location of the MODIFYCODE operator. */
4386 tree
4387 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4388 enum tree_code modifycode, tree rhs, tree rhs_origtype)
4390 tree result;
4391 tree newrhs;
4392 tree rhs_semantic_type = NULL_TREE;
4393 tree lhstype = TREE_TYPE (lhs);
4394 tree olhstype = lhstype;
4395 bool npc;
4397 /* Types that aren't fully specified cannot be used in assignments. */
4398 lhs = require_complete_type (lhs);
4400 /* Avoid duplicate error messages from operands that had errors. */
4401 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4402 return error_mark_node;
4404 if (!lvalue_or_else (lhs, lv_assign))
4405 return error_mark_node;
4407 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4409 rhs_semantic_type = TREE_TYPE (rhs);
4410 rhs = TREE_OPERAND (rhs, 0);
4413 newrhs = rhs;
4415 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4417 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4418 lhs_origtype, modifycode, rhs,
4419 rhs_origtype);
4420 if (inner == error_mark_node)
4421 return error_mark_node;
4422 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4423 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4424 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4425 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4426 protected_set_expr_location (result, location);
4427 return result;
4430 /* If a binary op has been requested, combine the old LHS value with the RHS
4431 producing the value we should actually store into the LHS. */
4433 if (modifycode != NOP_EXPR)
4435 lhs = c_fully_fold (lhs, false, NULL);
4436 lhs = stabilize_reference (lhs);
4437 newrhs = build_binary_op (location,
4438 modifycode, lhs, rhs, 1);
4440 /* The original type of the right hand side is no longer
4441 meaningful. */
4442 rhs_origtype = NULL_TREE;
4445 /* Give an error for storing in something that is 'const'. */
4447 if (TYPE_READONLY (lhstype)
4448 || ((TREE_CODE (lhstype) == RECORD_TYPE
4449 || TREE_CODE (lhstype) == UNION_TYPE)
4450 && C_TYPE_FIELDS_READONLY (lhstype)))
4452 readonly_error (lhs, lv_assign);
4453 return error_mark_node;
4455 else if (TREE_READONLY (lhs))
4456 readonly_warning (lhs, lv_assign);
4458 /* If storing into a structure or union member,
4459 it has probably been given type `int'.
4460 Compute the type that would go with
4461 the actual amount of storage the member occupies. */
4463 if (TREE_CODE (lhs) == COMPONENT_REF
4464 && (TREE_CODE (lhstype) == INTEGER_TYPE
4465 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4466 || TREE_CODE (lhstype) == REAL_TYPE
4467 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4468 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4470 /* If storing in a field that is in actuality a short or narrower than one,
4471 we must store in the field in its actual type. */
4473 if (lhstype != TREE_TYPE (lhs))
4475 lhs = copy_node (lhs);
4476 TREE_TYPE (lhs) = lhstype;
4479 /* Issue -Wc++-compat warnings about an assignment to an enum type
4480 when LHS does not have its original type. This happens for,
4481 e.g., an enum bitfield in a struct. */
4482 if (warn_cxx_compat
4483 && lhs_origtype != NULL_TREE
4484 && lhs_origtype != lhstype
4485 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4487 tree checktype = (rhs_origtype != NULL_TREE
4488 ? rhs_origtype
4489 : TREE_TYPE (rhs));
4490 if (checktype != error_mark_node
4491 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4492 warning_at (location, OPT_Wc___compat,
4493 "enum conversion in assignment is invalid in C++");
4496 /* Convert new value to destination type. Fold it first, then
4497 restore any excess precision information, for the sake of
4498 conversion warnings. */
4500 npc = null_pointer_constant_p (newrhs);
4501 newrhs = c_fully_fold (newrhs, false, NULL);
4502 if (rhs_semantic_type)
4503 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4504 newrhs = convert_for_assignment (lhstype, newrhs, rhs_origtype, ic_assign,
4505 npc, NULL_TREE, NULL_TREE, 0);
4506 if (TREE_CODE (newrhs) == ERROR_MARK)
4507 return error_mark_node;
4509 /* Emit ObjC write barrier, if necessary. */
4510 if (c_dialect_objc () && flag_objc_gc)
4512 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4513 if (result)
4515 protected_set_expr_location (result, location);
4516 return result;
4520 /* Scan operands. */
4522 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4523 TREE_SIDE_EFFECTS (result) = 1;
4524 protected_set_expr_location (result, location);
4526 /* If we got the LHS in a different type for storing in,
4527 convert the result back to the nominal type of LHS
4528 so that the value we return always has the same type
4529 as the LHS argument. */
4531 if (olhstype == TREE_TYPE (result))
4532 return result;
4534 result = convert_for_assignment (olhstype, result, rhs_origtype, ic_assign,
4535 false, NULL_TREE, NULL_TREE, 0);
4536 protected_set_expr_location (result, location);
4537 return result;
4540 /* Convert value RHS to type TYPE as preparation for an assignment to
4541 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4542 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4543 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4544 constant before any folding.
4545 The real work of conversion is done by `convert'.
4546 The purpose of this function is to generate error messages
4547 for assignments that are not allowed in C.
4548 ERRTYPE says whether it is argument passing, assignment,
4549 initialization or return.
4551 FUNCTION is a tree for the function being called.
4552 PARMNUM is the number of the argument, for printing in error messages. */
4554 static tree
4555 convert_for_assignment (tree type, tree rhs, tree origtype,
4556 enum impl_conv errtype, bool null_pointer_constant,
4557 tree fundecl, tree function, int parmnum)
4559 enum tree_code codel = TREE_CODE (type);
4560 tree orig_rhs = rhs;
4561 tree rhstype;
4562 enum tree_code coder;
4563 tree rname = NULL_TREE;
4564 bool objc_ok = false;
4566 if (errtype == ic_argpass)
4568 tree selector;
4569 /* Change pointer to function to the function itself for
4570 diagnostics. */
4571 if (TREE_CODE (function) == ADDR_EXPR
4572 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4573 function = TREE_OPERAND (function, 0);
4575 /* Handle an ObjC selector specially for diagnostics. */
4576 selector = objc_message_selector ();
4577 rname = function;
4578 if (selector && parmnum > 2)
4580 rname = selector;
4581 parmnum -= 2;
4585 /* This macro is used to emit diagnostics to ensure that all format
4586 strings are complete sentences, visible to gettext and checked at
4587 compile time. */
4588 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4589 do { \
4590 switch (errtype) \
4592 case ic_argpass: \
4593 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4594 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4595 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4596 "expected %qT but argument is of type %qT", \
4597 type, rhstype); \
4598 break; \
4599 case ic_assign: \
4600 pedwarn (LOCATION, OPT, AS); \
4601 break; \
4602 case ic_init: \
4603 pedwarn (LOCATION, OPT, IN); \
4604 break; \
4605 case ic_return: \
4606 pedwarn (LOCATION, OPT, RE); \
4607 break; \
4608 default: \
4609 gcc_unreachable (); \
4611 } while (0)
4613 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4614 rhs = TREE_OPERAND (rhs, 0);
4616 rhstype = TREE_TYPE (rhs);
4617 coder = TREE_CODE (rhstype);
4619 if (coder == ERROR_MARK)
4620 return error_mark_node;
4622 if (c_dialect_objc ())
4624 int parmno;
4626 switch (errtype)
4628 case ic_return:
4629 parmno = 0;
4630 break;
4632 case ic_assign:
4633 parmno = -1;
4634 break;
4636 case ic_init:
4637 parmno = -2;
4638 break;
4640 default:
4641 parmno = parmnum;
4642 break;
4645 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4648 if (warn_cxx_compat)
4650 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4651 if (checktype != error_mark_node
4652 && TREE_CODE (type) == ENUMERAL_TYPE
4653 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4655 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4656 G_("enum conversion when passing argument "
4657 "%d of %qE is invalid in C++"),
4658 G_("enum conversion in assignment is "
4659 "invalid in C++"),
4660 G_("enum conversion in initialization is "
4661 "invalid in C++"),
4662 G_("enum conversion in return is "
4663 "invalid in C++"));
4667 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4668 return rhs;
4670 if (coder == VOID_TYPE)
4672 /* Except for passing an argument to an unprototyped function,
4673 this is a constraint violation. When passing an argument to
4674 an unprototyped function, it is compile-time undefined;
4675 making it a constraint in that case was rejected in
4676 DR#252. */
4677 error ("void value not ignored as it ought to be");
4678 return error_mark_node;
4680 rhs = require_complete_type (rhs);
4681 if (rhs == error_mark_node)
4682 return error_mark_node;
4683 /* A type converts to a reference to it.
4684 This code doesn't fully support references, it's just for the
4685 special case of va_start and va_copy. */
4686 if (codel == REFERENCE_TYPE
4687 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4689 if (!lvalue_p (rhs))
4691 error ("cannot pass rvalue to reference parameter");
4692 return error_mark_node;
4694 if (!c_mark_addressable (rhs))
4695 return error_mark_node;
4696 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4698 /* We already know that these two types are compatible, but they
4699 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4700 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4701 likely to be va_list, a typedef to __builtin_va_list, which
4702 is different enough that it will cause problems later. */
4703 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4704 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4706 rhs = build1 (NOP_EXPR, type, rhs);
4707 return rhs;
4709 /* Some types can interconvert without explicit casts. */
4710 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4711 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4712 return convert (type, rhs);
4713 /* Arithmetic types all interconvert, and enum is treated like int. */
4714 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4715 || codel == FIXED_POINT_TYPE
4716 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4717 || codel == BOOLEAN_TYPE)
4718 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4719 || coder == FIXED_POINT_TYPE
4720 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4721 || coder == BOOLEAN_TYPE))
4723 tree ret;
4724 bool save = in_late_binary_op;
4725 if (codel == BOOLEAN_TYPE)
4726 in_late_binary_op = true;
4727 ret = convert_and_check (type, orig_rhs);
4728 if (codel == BOOLEAN_TYPE)
4729 in_late_binary_op = save;
4730 return ret;
4733 /* Aggregates in different TUs might need conversion. */
4734 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4735 && codel == coder
4736 && comptypes (type, rhstype))
4737 return convert_and_check (type, rhs);
4739 /* Conversion to a transparent union from its member types.
4740 This applies only to function arguments. */
4741 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4742 && errtype == ic_argpass)
4744 tree memb, marginal_memb = NULL_TREE;
4746 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4748 tree memb_type = TREE_TYPE (memb);
4750 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4751 TYPE_MAIN_VARIANT (rhstype)))
4752 break;
4754 if (TREE_CODE (memb_type) != POINTER_TYPE)
4755 continue;
4757 if (coder == POINTER_TYPE)
4759 tree ttl = TREE_TYPE (memb_type);
4760 tree ttr = TREE_TYPE (rhstype);
4762 /* Any non-function converts to a [const][volatile] void *
4763 and vice versa; otherwise, targets must be the same.
4764 Meanwhile, the lhs target must have all the qualifiers of
4765 the rhs. */
4766 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4767 || comp_target_types (memb_type, rhstype))
4769 /* If this type won't generate any warnings, use it. */
4770 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4771 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4772 && TREE_CODE (ttl) == FUNCTION_TYPE)
4773 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4774 == TYPE_QUALS (ttr))
4775 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4776 == TYPE_QUALS (ttl))))
4777 break;
4779 /* Keep looking for a better type, but remember this one. */
4780 if (!marginal_memb)
4781 marginal_memb = memb;
4785 /* Can convert integer zero to any pointer type. */
4786 if (null_pointer_constant)
4788 rhs = null_pointer_node;
4789 break;
4793 if (memb || marginal_memb)
4795 if (!memb)
4797 /* We have only a marginally acceptable member type;
4798 it needs a warning. */
4799 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4800 tree ttr = TREE_TYPE (rhstype);
4802 /* Const and volatile mean something different for function
4803 types, so the usual warnings are not appropriate. */
4804 if (TREE_CODE (ttr) == FUNCTION_TYPE
4805 && TREE_CODE (ttl) == FUNCTION_TYPE)
4807 /* Because const and volatile on functions are
4808 restrictions that say the function will not do
4809 certain things, it is okay to use a const or volatile
4810 function where an ordinary one is wanted, but not
4811 vice-versa. */
4812 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4813 WARN_FOR_ASSIGNMENT (input_location, 0,
4814 G_("passing argument %d of %qE "
4815 "makes qualified function "
4816 "pointer from unqualified"),
4817 G_("assignment makes qualified "
4818 "function pointer from "
4819 "unqualified"),
4820 G_("initialization makes qualified "
4821 "function pointer from "
4822 "unqualified"),
4823 G_("return makes qualified function "
4824 "pointer from unqualified"));
4826 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4827 WARN_FOR_ASSIGNMENT (input_location, 0,
4828 G_("passing argument %d of %qE discards "
4829 "qualifiers from pointer target type"),
4830 G_("assignment discards qualifiers "
4831 "from pointer target type"),
4832 G_("initialization discards qualifiers "
4833 "from pointer target type"),
4834 G_("return discards qualifiers from "
4835 "pointer target type"));
4837 memb = marginal_memb;
4840 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4841 pedwarn (input_location, OPT_pedantic,
4842 "ISO C prohibits argument conversion to union type");
4844 rhs = fold_convert (TREE_TYPE (memb), rhs);
4845 return build_constructor_single (type, memb, rhs);
4849 /* Conversions among pointers */
4850 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4851 && (coder == codel))
4853 tree ttl = TREE_TYPE (type);
4854 tree ttr = TREE_TYPE (rhstype);
4855 tree mvl = ttl;
4856 tree mvr = ttr;
4857 bool is_opaque_pointer;
4858 int target_cmp = 0; /* Cache comp_target_types () result. */
4860 if (TREE_CODE (mvl) != ARRAY_TYPE)
4861 mvl = TYPE_MAIN_VARIANT (mvl);
4862 if (TREE_CODE (mvr) != ARRAY_TYPE)
4863 mvr = TYPE_MAIN_VARIANT (mvr);
4864 /* Opaque pointers are treated like void pointers. */
4865 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4867 /* C++ does not allow the implicit conversion void* -> T*. However,
4868 for the purpose of reducing the number of false positives, we
4869 tolerate the special case of
4871 int *p = NULL;
4873 where NULL is typically defined in C to be '(void *) 0'. */
4874 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4875 warning (OPT_Wc___compat, "request for implicit conversion from "
4876 "%qT to %qT not permitted in C++", rhstype, type);
4878 /* Check if the right-hand side has a format attribute but the
4879 left-hand side doesn't. */
4880 if (warn_missing_format_attribute
4881 && check_missing_format_attribute (type, rhstype))
4883 switch (errtype)
4885 case ic_argpass:
4886 warning (OPT_Wmissing_format_attribute,
4887 "argument %d of %qE might be "
4888 "a candidate for a format attribute",
4889 parmnum, rname);
4890 break;
4891 case ic_assign:
4892 warning (OPT_Wmissing_format_attribute,
4893 "assignment left-hand side might be "
4894 "a candidate for a format attribute");
4895 break;
4896 case ic_init:
4897 warning (OPT_Wmissing_format_attribute,
4898 "initialization left-hand side might be "
4899 "a candidate for a format attribute");
4900 break;
4901 case ic_return:
4902 warning (OPT_Wmissing_format_attribute,
4903 "return type might be "
4904 "a candidate for a format attribute");
4905 break;
4906 default:
4907 gcc_unreachable ();
4911 /* Any non-function converts to a [const][volatile] void *
4912 and vice versa; otherwise, targets must be the same.
4913 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4914 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4915 || (target_cmp = comp_target_types (type, rhstype))
4916 || is_opaque_pointer
4917 || (c_common_unsigned_type (mvl)
4918 == c_common_unsigned_type (mvr)))
4920 if (pedantic
4921 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4923 (VOID_TYPE_P (ttr)
4924 && !null_pointer_constant
4925 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4926 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4927 G_("ISO C forbids passing argument %d of "
4928 "%qE between function pointer "
4929 "and %<void *%>"),
4930 G_("ISO C forbids assignment between "
4931 "function pointer and %<void *%>"),
4932 G_("ISO C forbids initialization between "
4933 "function pointer and %<void *%>"),
4934 G_("ISO C forbids return between function "
4935 "pointer and %<void *%>"));
4936 /* Const and volatile mean something different for function types,
4937 so the usual warnings are not appropriate. */
4938 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4939 && TREE_CODE (ttl) != FUNCTION_TYPE)
4941 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4943 /* Types differing only by the presence of the 'volatile'
4944 qualifier are acceptable if the 'volatile' has been added
4945 in by the Objective-C EH machinery. */
4946 if (!objc_type_quals_match (ttl, ttr))
4947 WARN_FOR_ASSIGNMENT (input_location, 0,
4948 G_("passing argument %d of %qE discards "
4949 "qualifiers from pointer target type"),
4950 G_("assignment discards qualifiers "
4951 "from pointer target type"),
4952 G_("initialization discards qualifiers "
4953 "from pointer target type"),
4954 G_("return discards qualifiers from "
4955 "pointer target type"));
4957 /* If this is not a case of ignoring a mismatch in signedness,
4958 no warning. */
4959 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4960 || target_cmp)
4962 /* If there is a mismatch, do warn. */
4963 else if (warn_pointer_sign)
4964 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4965 G_("pointer targets in passing argument "
4966 "%d of %qE differ in signedness"),
4967 G_("pointer targets in assignment "
4968 "differ in signedness"),
4969 G_("pointer targets in initialization "
4970 "differ in signedness"),
4971 G_("pointer targets in return differ "
4972 "in signedness"));
4974 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4975 && TREE_CODE (ttr) == FUNCTION_TYPE)
4977 /* Because const and volatile on functions are restrictions
4978 that say the function will not do certain things,
4979 it is okay to use a const or volatile function
4980 where an ordinary one is wanted, but not vice-versa. */
4981 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4982 WARN_FOR_ASSIGNMENT (input_location, 0,
4983 G_("passing argument %d of %qE makes "
4984 "qualified function pointer "
4985 "from unqualified"),
4986 G_("assignment makes qualified function "
4987 "pointer from unqualified"),
4988 G_("initialization makes qualified "
4989 "function pointer from unqualified"),
4990 G_("return makes qualified function "
4991 "pointer from unqualified"));
4994 else
4995 /* Avoid warning about the volatile ObjC EH puts on decls. */
4996 if (!objc_ok)
4997 WARN_FOR_ASSIGNMENT (input_location, 0,
4998 G_("passing argument %d of %qE from "
4999 "incompatible pointer type"),
5000 G_("assignment from incompatible pointer type"),
5001 G_("initialization from incompatible "
5002 "pointer type"),
5003 G_("return from incompatible pointer type"));
5005 return convert (type, rhs);
5007 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5009 /* ??? This should not be an error when inlining calls to
5010 unprototyped functions. */
5011 error ("invalid use of non-lvalue array");
5012 return error_mark_node;
5014 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5016 /* An explicit constant 0 can convert to a pointer,
5017 or one that results from arithmetic, even including
5018 a cast to integer type. */
5019 if (!null_pointer_constant)
5020 WARN_FOR_ASSIGNMENT (input_location, 0,
5021 G_("passing argument %d of %qE makes "
5022 "pointer from integer without a cast"),
5023 G_("assignment makes pointer from integer "
5024 "without a cast"),
5025 G_("initialization makes pointer from "
5026 "integer without a cast"),
5027 G_("return makes pointer from integer "
5028 "without a cast"));
5030 return convert (type, rhs);
5032 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5034 WARN_FOR_ASSIGNMENT (input_location, 0,
5035 G_("passing argument %d of %qE makes integer "
5036 "from pointer without a cast"),
5037 G_("assignment makes integer from pointer "
5038 "without a cast"),
5039 G_("initialization makes integer from pointer "
5040 "without a cast"),
5041 G_("return makes integer from pointer "
5042 "without a cast"));
5043 return convert (type, rhs);
5045 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5047 tree ret;
5048 bool save = in_late_binary_op;
5049 in_late_binary_op = true;
5050 ret = convert (type, rhs);
5051 in_late_binary_op = save;
5052 return ret;
5055 switch (errtype)
5057 case ic_argpass:
5058 error ("incompatible type for argument %d of %qE", parmnum, rname);
5059 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5060 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5061 "expected %qT but argument is of type %qT", type, rhstype);
5062 break;
5063 case ic_assign:
5064 error ("incompatible types when assigning to type %qT from type %qT",
5065 type, rhstype);
5066 break;
5067 case ic_init:
5068 error ("incompatible types when initializing type %qT using type %qT",
5069 type, rhstype);
5070 break;
5071 case ic_return:
5072 error ("incompatible types when returning type %qT but %qT was expected",
5073 rhstype, type);
5074 break;
5075 default:
5076 gcc_unreachable ();
5079 return error_mark_node;
5082 /* If VALUE is a compound expr all of whose expressions are constant, then
5083 return its value. Otherwise, return error_mark_node.
5085 This is for handling COMPOUND_EXPRs as initializer elements
5086 which is allowed with a warning when -pedantic is specified. */
5088 static tree
5089 valid_compound_expr_initializer (tree value, tree endtype)
5091 if (TREE_CODE (value) == COMPOUND_EXPR)
5093 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5094 == error_mark_node)
5095 return error_mark_node;
5096 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5097 endtype);
5099 else if (!initializer_constant_valid_p (value, endtype))
5100 return error_mark_node;
5101 else
5102 return value;
5105 /* Perform appropriate conversions on the initial value of a variable,
5106 store it in the declaration DECL,
5107 and print any error messages that are appropriate.
5108 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5109 If the init is invalid, store an ERROR_MARK. */
5111 void
5112 store_init_value (tree decl, tree init, tree origtype)
5114 tree value, type;
5115 bool npc = false;
5117 /* If variable's type was invalidly declared, just ignore it. */
5119 type = TREE_TYPE (decl);
5120 if (TREE_CODE (type) == ERROR_MARK)
5121 return;
5123 /* Digest the specified initializer into an expression. */
5125 if (init)
5126 npc = null_pointer_constant_p (init);
5127 value = digest_init (type, init, origtype, npc, true, TREE_STATIC (decl));
5129 /* Store the expression if valid; else report error. */
5131 if (!in_system_header
5132 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5133 warning (OPT_Wtraditional, "traditional C rejects automatic "
5134 "aggregate initialization");
5136 DECL_INITIAL (decl) = value;
5138 /* ANSI wants warnings about out-of-range constant initializers. */
5139 STRIP_TYPE_NOPS (value);
5140 if (TREE_STATIC (decl))
5141 constant_expression_warning (value);
5143 /* Check if we need to set array size from compound literal size. */
5144 if (TREE_CODE (type) == ARRAY_TYPE
5145 && TYPE_DOMAIN (type) == 0
5146 && value != error_mark_node)
5148 tree inside_init = init;
5150 STRIP_TYPE_NOPS (inside_init);
5151 inside_init = fold (inside_init);
5153 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5155 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5157 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5159 /* For int foo[] = (int [3]){1}; we need to set array size
5160 now since later on array initializer will be just the
5161 brace enclosed list of the compound literal. */
5162 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5163 TREE_TYPE (decl) = type;
5164 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5165 layout_type (type);
5166 layout_decl (cldecl, 0);
5172 /* Methods for storing and printing names for error messages. */
5174 /* Implement a spelling stack that allows components of a name to be pushed
5175 and popped. Each element on the stack is this structure. */
5177 struct spelling
5179 int kind;
5180 union
5182 unsigned HOST_WIDE_INT i;
5183 const char *s;
5184 } u;
5187 #define SPELLING_STRING 1
5188 #define SPELLING_MEMBER 2
5189 #define SPELLING_BOUNDS 3
5191 static struct spelling *spelling; /* Next stack element (unused). */
5192 static struct spelling *spelling_base; /* Spelling stack base. */
5193 static int spelling_size; /* Size of the spelling stack. */
5195 /* Macros to save and restore the spelling stack around push_... functions.
5196 Alternative to SAVE_SPELLING_STACK. */
5198 #define SPELLING_DEPTH() (spelling - spelling_base)
5199 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5201 /* Push an element on the spelling stack with type KIND and assign VALUE
5202 to MEMBER. */
5204 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5206 int depth = SPELLING_DEPTH (); \
5208 if (depth >= spelling_size) \
5210 spelling_size += 10; \
5211 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5212 spelling_size); \
5213 RESTORE_SPELLING_DEPTH (depth); \
5216 spelling->kind = (KIND); \
5217 spelling->MEMBER = (VALUE); \
5218 spelling++; \
5221 /* Push STRING on the stack. Printed literally. */
5223 static void
5224 push_string (const char *string)
5226 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5229 /* Push a member name on the stack. Printed as '.' STRING. */
5231 static void
5232 push_member_name (tree decl)
5234 const char *const string
5235 = (DECL_NAME (decl)
5236 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5237 : _("<anonymous>"));
5238 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5241 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5243 static void
5244 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5246 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5249 /* Compute the maximum size in bytes of the printed spelling. */
5251 static int
5252 spelling_length (void)
5254 int size = 0;
5255 struct spelling *p;
5257 for (p = spelling_base; p < spelling; p++)
5259 if (p->kind == SPELLING_BOUNDS)
5260 size += 25;
5261 else
5262 size += strlen (p->u.s) + 1;
5265 return size;
5268 /* Print the spelling to BUFFER and return it. */
5270 static char *
5271 print_spelling (char *buffer)
5273 char *d = buffer;
5274 struct spelling *p;
5276 for (p = spelling_base; p < spelling; p++)
5277 if (p->kind == SPELLING_BOUNDS)
5279 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5280 d += strlen (d);
5282 else
5284 const char *s;
5285 if (p->kind == SPELLING_MEMBER)
5286 *d++ = '.';
5287 for (s = p->u.s; (*d = *s++); d++)
5290 *d++ = '\0';
5291 return buffer;
5294 /* Issue an error message for a bad initializer component.
5295 MSGID identifies the message.
5296 The component name is taken from the spelling stack. */
5298 void
5299 error_init (const char *msgid)
5301 char *ofwhat;
5303 error ("%s", _(msgid));
5304 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5305 if (*ofwhat)
5306 error ("(near initialization for %qs)", ofwhat);
5309 /* Issue a pedantic warning for a bad initializer component. OPT is
5310 the option OPT_* (from options.h) controlling this warning or 0 if
5311 it is unconditionally given. MSGID identifies the message. The
5312 component name is taken from the spelling stack. */
5314 void
5315 pedwarn_init (location_t location, int opt, const char *msgid)
5317 char *ofwhat;
5319 pedwarn (location, opt, "%s", _(msgid));
5320 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5321 if (*ofwhat)
5322 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5325 /* Issue a warning for a bad initializer component.
5327 OPT is the OPT_W* value corresponding to the warning option that
5328 controls this warning. MSGID identifies the message. The
5329 component name is taken from the spelling stack. */
5331 static void
5332 warning_init (int opt, const char *msgid)
5334 char *ofwhat;
5336 warning (opt, "%s", _(msgid));
5337 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5338 if (*ofwhat)
5339 warning (opt, "(near initialization for %qs)", ofwhat);
5342 /* If TYPE is an array type and EXPR is a parenthesized string
5343 constant, warn if pedantic that EXPR is being used to initialize an
5344 object of type TYPE. */
5346 void
5347 maybe_warn_string_init (tree type, struct c_expr expr)
5349 if (pedantic
5350 && TREE_CODE (type) == ARRAY_TYPE
5351 && TREE_CODE (expr.value) == STRING_CST
5352 && expr.original_code != STRING_CST)
5353 pedwarn_init (input_location, OPT_pedantic,
5354 "array initialized from parenthesized string constant");
5357 /* Digest the parser output INIT as an initializer for type TYPE.
5358 Return a C expression of type TYPE to represent the initial value.
5360 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5362 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5364 If INIT is a string constant, STRICT_STRING is true if it is
5365 unparenthesized or we should not warn here for it being parenthesized.
5366 For other types of INIT, STRICT_STRING is not used.
5368 REQUIRE_CONSTANT requests an error if non-constant initializers or
5369 elements are seen. */
5371 static tree
5372 digest_init (tree type, tree init, tree origtype, bool null_pointer_constant,
5373 bool strict_string, int require_constant)
5375 enum tree_code code = TREE_CODE (type);
5376 tree inside_init = init;
5377 tree semantic_type = NULL_TREE;
5378 bool maybe_const = true;
5380 if (type == error_mark_node
5381 || !init
5382 || init == error_mark_node
5383 || TREE_TYPE (init) == error_mark_node)
5384 return error_mark_node;
5386 STRIP_TYPE_NOPS (inside_init);
5388 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5390 semantic_type = TREE_TYPE (inside_init);
5391 inside_init = TREE_OPERAND (inside_init, 0);
5393 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5394 inside_init = decl_constant_value_for_optimization (inside_init);
5396 /* Initialization of an array of chars from a string constant
5397 optionally enclosed in braces. */
5399 if (code == ARRAY_TYPE && inside_init
5400 && TREE_CODE (inside_init) == STRING_CST)
5402 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5403 /* Note that an array could be both an array of character type
5404 and an array of wchar_t if wchar_t is signed char or unsigned
5405 char. */
5406 bool char_array = (typ1 == char_type_node
5407 || typ1 == signed_char_type_node
5408 || typ1 == unsigned_char_type_node);
5409 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5410 bool char16_array = !!comptypes (typ1, char16_type_node);
5411 bool char32_array = !!comptypes (typ1, char32_type_node);
5413 if (char_array || wchar_array || char16_array || char32_array)
5415 struct c_expr expr;
5416 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5417 expr.value = inside_init;
5418 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5419 expr.original_type = NULL;
5420 maybe_warn_string_init (type, expr);
5422 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5423 pedwarn_init (input_location, OPT_pedantic,
5424 "initialization of a flexible array member");
5426 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5427 TYPE_MAIN_VARIANT (type)))
5428 return inside_init;
5430 if (char_array)
5432 if (typ2 != char_type_node)
5434 error_init ("char-array initialized from wide string");
5435 return error_mark_node;
5438 else
5440 if (typ2 == char_type_node)
5442 error_init ("wide character array initialized from non-wide "
5443 "string");
5444 return error_mark_node;
5446 else if (!comptypes(typ1, typ2))
5448 error_init ("wide character array initialized from "
5449 "incompatible wide string");
5450 return error_mark_node;
5454 TREE_TYPE (inside_init) = type;
5455 if (TYPE_DOMAIN (type) != 0
5456 && TYPE_SIZE (type) != 0
5457 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
5458 /* Subtract the size of a single (possibly wide) character
5459 because it's ok to ignore the terminating null char
5460 that is counted in the length of the constant. */
5461 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5462 TREE_STRING_LENGTH (inside_init)
5463 - (TYPE_PRECISION (typ1)
5464 / BITS_PER_UNIT)))
5465 pedwarn_init (input_location, 0,
5466 "initializer-string for array of chars is too long");
5468 return inside_init;
5470 else if (INTEGRAL_TYPE_P (typ1))
5472 error_init ("array of inappropriate type initialized "
5473 "from string constant");
5474 return error_mark_node;
5478 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5479 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5480 below and handle as a constructor. */
5481 if (code == VECTOR_TYPE
5482 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5483 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5484 && TREE_CONSTANT (inside_init))
5486 if (TREE_CODE (inside_init) == VECTOR_CST
5487 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5488 TYPE_MAIN_VARIANT (type)))
5489 return inside_init;
5491 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5493 unsigned HOST_WIDE_INT ix;
5494 tree value;
5495 bool constant_p = true;
5497 /* Iterate through elements and check if all constructor
5498 elements are *_CSTs. */
5499 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5500 if (!CONSTANT_CLASS_P (value))
5502 constant_p = false;
5503 break;
5506 if (constant_p)
5507 return build_vector_from_ctor (type,
5508 CONSTRUCTOR_ELTS (inside_init));
5512 if (warn_sequence_point)
5513 verify_sequence_points (inside_init);
5515 /* Any type can be initialized
5516 from an expression of the same type, optionally with braces. */
5518 if (inside_init && TREE_TYPE (inside_init) != 0
5519 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5520 TYPE_MAIN_VARIANT (type))
5521 || (code == ARRAY_TYPE
5522 && comptypes (TREE_TYPE (inside_init), type))
5523 || (code == VECTOR_TYPE
5524 && comptypes (TREE_TYPE (inside_init), type))
5525 || (code == POINTER_TYPE
5526 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5527 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5528 TREE_TYPE (type)))))
5530 if (code == POINTER_TYPE)
5532 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5534 if (TREE_CODE (inside_init) == STRING_CST
5535 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5536 inside_init = array_to_pointer_conversion (inside_init);
5537 else
5539 error_init ("invalid use of non-lvalue array");
5540 return error_mark_node;
5545 if (code == VECTOR_TYPE)
5546 /* Although the types are compatible, we may require a
5547 conversion. */
5548 inside_init = convert (type, inside_init);
5550 if (require_constant
5551 && (code == VECTOR_TYPE || !flag_isoc99)
5552 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5554 /* As an extension, allow initializing objects with static storage
5555 duration with compound literals (which are then treated just as
5556 the brace enclosed list they contain). Also allow this for
5557 vectors, as we can only assign them with compound literals. */
5558 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5559 inside_init = DECL_INITIAL (decl);
5562 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5563 && TREE_CODE (inside_init) != CONSTRUCTOR)
5565 error_init ("array initialized from non-constant array expression");
5566 return error_mark_node;
5569 /* Compound expressions can only occur here if -pedantic or
5570 -pedantic-errors is specified. In the later case, we always want
5571 an error. In the former case, we simply want a warning. */
5572 if (require_constant && pedantic
5573 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5575 inside_init
5576 = valid_compound_expr_initializer (inside_init,
5577 TREE_TYPE (inside_init));
5578 if (inside_init == error_mark_node)
5579 error_init ("initializer element is not constant");
5580 else
5581 pedwarn_init (input_location, OPT_pedantic,
5582 "initializer element is not constant");
5583 if (flag_pedantic_errors)
5584 inside_init = error_mark_node;
5586 else if (require_constant
5587 && !initializer_constant_valid_p (inside_init,
5588 TREE_TYPE (inside_init)))
5590 error_init ("initializer element is not constant");
5591 inside_init = error_mark_node;
5593 else if (require_constant && !maybe_const)
5594 pedwarn_init (input_location, 0,
5595 "initializer element is not a constant expression");
5597 /* Added to enable additional -Wmissing-format-attribute warnings. */
5598 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5599 inside_init = convert_for_assignment (type, inside_init, origtype,
5600 ic_init, null_pointer_constant,
5601 NULL_TREE, NULL_TREE, 0);
5602 return inside_init;
5605 /* Handle scalar types, including conversions. */
5607 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5608 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5609 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5611 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5612 && (TREE_CODE (init) == STRING_CST
5613 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5614 inside_init = init = array_to_pointer_conversion (init);
5615 if (semantic_type)
5616 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5617 inside_init);
5618 inside_init
5619 = convert_for_assignment (type, inside_init, origtype, ic_init,
5620 null_pointer_constant,
5621 NULL_TREE, NULL_TREE, 0);
5623 /* Check to see if we have already given an error message. */
5624 if (inside_init == error_mark_node)
5626 else if (require_constant && !TREE_CONSTANT (inside_init))
5628 error_init ("initializer element is not constant");
5629 inside_init = error_mark_node;
5631 else if (require_constant
5632 && !initializer_constant_valid_p (inside_init,
5633 TREE_TYPE (inside_init)))
5635 error_init ("initializer element is not computable at load time");
5636 inside_init = error_mark_node;
5638 else if (require_constant && !maybe_const)
5639 pedwarn_init (input_location, 0,
5640 "initializer element is not a constant expression");
5642 return inside_init;
5645 /* Come here only for records and arrays. */
5647 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5649 error_init ("variable-sized object may not be initialized");
5650 return error_mark_node;
5653 error_init ("invalid initializer");
5654 return error_mark_node;
5657 /* Handle initializers that use braces. */
5659 /* Type of object we are accumulating a constructor for.
5660 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5661 static tree constructor_type;
5663 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5664 left to fill. */
5665 static tree constructor_fields;
5667 /* For an ARRAY_TYPE, this is the specified index
5668 at which to store the next element we get. */
5669 static tree constructor_index;
5671 /* For an ARRAY_TYPE, this is the maximum index. */
5672 static tree constructor_max_index;
5674 /* For a RECORD_TYPE, this is the first field not yet written out. */
5675 static tree constructor_unfilled_fields;
5677 /* For an ARRAY_TYPE, this is the index of the first element
5678 not yet written out. */
5679 static tree constructor_unfilled_index;
5681 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5682 This is so we can generate gaps between fields, when appropriate. */
5683 static tree constructor_bit_index;
5685 /* If we are saving up the elements rather than allocating them,
5686 this is the list of elements so far (in reverse order,
5687 most recent first). */
5688 static VEC(constructor_elt,gc) *constructor_elements;
5690 /* 1 if constructor should be incrementally stored into a constructor chain,
5691 0 if all the elements should be kept in AVL tree. */
5692 static int constructor_incremental;
5694 /* 1 if so far this constructor's elements are all compile-time constants. */
5695 static int constructor_constant;
5697 /* 1 if so far this constructor's elements are all valid address constants. */
5698 static int constructor_simple;
5700 /* 1 if this constructor has an element that cannot be part of a
5701 constant expression. */
5702 static int constructor_nonconst;
5704 /* 1 if this constructor is erroneous so far. */
5705 static int constructor_erroneous;
5707 /* Structure for managing pending initializer elements, organized as an
5708 AVL tree. */
5710 struct init_node
5712 struct init_node *left, *right;
5713 struct init_node *parent;
5714 int balance;
5715 tree purpose;
5716 tree value;
5717 tree origtype;
5720 /* Tree of pending elements at this constructor level.
5721 These are elements encountered out of order
5722 which belong at places we haven't reached yet in actually
5723 writing the output.
5724 Will never hold tree nodes across GC runs. */
5725 static struct init_node *constructor_pending_elts;
5727 /* The SPELLING_DEPTH of this constructor. */
5728 static int constructor_depth;
5730 /* DECL node for which an initializer is being read.
5731 0 means we are reading a constructor expression
5732 such as (struct foo) {...}. */
5733 static tree constructor_decl;
5735 /* Nonzero if this is an initializer for a top-level decl. */
5736 static int constructor_top_level;
5738 /* Nonzero if there were any member designators in this initializer. */
5739 static int constructor_designated;
5741 /* Nesting depth of designator list. */
5742 static int designator_depth;
5744 /* Nonzero if there were diagnosed errors in this designator list. */
5745 static int designator_erroneous;
5748 /* This stack has a level for each implicit or explicit level of
5749 structuring in the initializer, including the outermost one. It
5750 saves the values of most of the variables above. */
5752 struct constructor_range_stack;
5754 struct constructor_stack
5756 struct constructor_stack *next;
5757 tree type;
5758 tree fields;
5759 tree index;
5760 tree max_index;
5761 tree unfilled_index;
5762 tree unfilled_fields;
5763 tree bit_index;
5764 VEC(constructor_elt,gc) *elements;
5765 struct init_node *pending_elts;
5766 int offset;
5767 int depth;
5768 /* If value nonzero, this value should replace the entire
5769 constructor at this level. */
5770 struct c_expr replacement_value;
5771 struct constructor_range_stack *range_stack;
5772 char constant;
5773 char simple;
5774 char nonconst;
5775 char implicit;
5776 char erroneous;
5777 char outer;
5778 char incremental;
5779 char designated;
5782 static struct constructor_stack *constructor_stack;
5784 /* This stack represents designators from some range designator up to
5785 the last designator in the list. */
5787 struct constructor_range_stack
5789 struct constructor_range_stack *next, *prev;
5790 struct constructor_stack *stack;
5791 tree range_start;
5792 tree index;
5793 tree range_end;
5794 tree fields;
5797 static struct constructor_range_stack *constructor_range_stack;
5799 /* This stack records separate initializers that are nested.
5800 Nested initializers can't happen in ANSI C, but GNU C allows them
5801 in cases like { ... (struct foo) { ... } ... }. */
5803 struct initializer_stack
5805 struct initializer_stack *next;
5806 tree decl;
5807 struct constructor_stack *constructor_stack;
5808 struct constructor_range_stack *constructor_range_stack;
5809 VEC(constructor_elt,gc) *elements;
5810 struct spelling *spelling;
5811 struct spelling *spelling_base;
5812 int spelling_size;
5813 char top_level;
5814 char require_constant_value;
5815 char require_constant_elements;
5818 static struct initializer_stack *initializer_stack;
5820 /* Prepare to parse and output the initializer for variable DECL. */
5822 void
5823 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5825 const char *locus;
5826 struct initializer_stack *p = XNEW (struct initializer_stack);
5828 p->decl = constructor_decl;
5829 p->require_constant_value = require_constant_value;
5830 p->require_constant_elements = require_constant_elements;
5831 p->constructor_stack = constructor_stack;
5832 p->constructor_range_stack = constructor_range_stack;
5833 p->elements = constructor_elements;
5834 p->spelling = spelling;
5835 p->spelling_base = spelling_base;
5836 p->spelling_size = spelling_size;
5837 p->top_level = constructor_top_level;
5838 p->next = initializer_stack;
5839 initializer_stack = p;
5841 constructor_decl = decl;
5842 constructor_designated = 0;
5843 constructor_top_level = top_level;
5845 if (decl != 0 && decl != error_mark_node)
5847 require_constant_value = TREE_STATIC (decl);
5848 require_constant_elements
5849 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5850 /* For a scalar, you can always use any value to initialize,
5851 even within braces. */
5852 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5853 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5854 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5855 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5856 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
5858 else
5860 require_constant_value = 0;
5861 require_constant_elements = 0;
5862 locus = _("(anonymous)");
5865 constructor_stack = 0;
5866 constructor_range_stack = 0;
5868 missing_braces_mentioned = 0;
5870 spelling_base = 0;
5871 spelling_size = 0;
5872 RESTORE_SPELLING_DEPTH (0);
5874 if (locus)
5875 push_string (locus);
5878 void
5879 finish_init (void)
5881 struct initializer_stack *p = initializer_stack;
5883 /* Free the whole constructor stack of this initializer. */
5884 while (constructor_stack)
5886 struct constructor_stack *q = constructor_stack;
5887 constructor_stack = q->next;
5888 free (q);
5891 gcc_assert (!constructor_range_stack);
5893 /* Pop back to the data of the outer initializer (if any). */
5894 free (spelling_base);
5896 constructor_decl = p->decl;
5897 require_constant_value = p->require_constant_value;
5898 require_constant_elements = p->require_constant_elements;
5899 constructor_stack = p->constructor_stack;
5900 constructor_range_stack = p->constructor_range_stack;
5901 constructor_elements = p->elements;
5902 spelling = p->spelling;
5903 spelling_base = p->spelling_base;
5904 spelling_size = p->spelling_size;
5905 constructor_top_level = p->top_level;
5906 initializer_stack = p->next;
5907 free (p);
5910 /* Call here when we see the initializer is surrounded by braces.
5911 This is instead of a call to push_init_level;
5912 it is matched by a call to pop_init_level.
5914 TYPE is the type to initialize, for a constructor expression.
5915 For an initializer for a decl, TYPE is zero. */
5917 void
5918 really_start_incremental_init (tree type)
5920 struct constructor_stack *p = XNEW (struct constructor_stack);
5922 if (type == 0)
5923 type = TREE_TYPE (constructor_decl);
5925 if (TREE_CODE (type) == VECTOR_TYPE
5926 && TYPE_VECTOR_OPAQUE (type))
5927 error ("opaque vector types cannot be initialized");
5929 p->type = constructor_type;
5930 p->fields = constructor_fields;
5931 p->index = constructor_index;
5932 p->max_index = constructor_max_index;
5933 p->unfilled_index = constructor_unfilled_index;
5934 p->unfilled_fields = constructor_unfilled_fields;
5935 p->bit_index = constructor_bit_index;
5936 p->elements = constructor_elements;
5937 p->constant = constructor_constant;
5938 p->simple = constructor_simple;
5939 p->nonconst = constructor_nonconst;
5940 p->erroneous = constructor_erroneous;
5941 p->pending_elts = constructor_pending_elts;
5942 p->depth = constructor_depth;
5943 p->replacement_value.value = 0;
5944 p->replacement_value.original_code = ERROR_MARK;
5945 p->replacement_value.original_type = NULL;
5946 p->implicit = 0;
5947 p->range_stack = 0;
5948 p->outer = 0;
5949 p->incremental = constructor_incremental;
5950 p->designated = constructor_designated;
5951 p->next = 0;
5952 constructor_stack = p;
5954 constructor_constant = 1;
5955 constructor_simple = 1;
5956 constructor_nonconst = 0;
5957 constructor_depth = SPELLING_DEPTH ();
5958 constructor_elements = 0;
5959 constructor_pending_elts = 0;
5960 constructor_type = type;
5961 constructor_incremental = 1;
5962 constructor_designated = 0;
5963 designator_depth = 0;
5964 designator_erroneous = 0;
5966 if (TREE_CODE (constructor_type) == RECORD_TYPE
5967 || TREE_CODE (constructor_type) == UNION_TYPE)
5969 constructor_fields = TYPE_FIELDS (constructor_type);
5970 /* Skip any nameless bit fields at the beginning. */
5971 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5972 && DECL_NAME (constructor_fields) == 0)
5973 constructor_fields = TREE_CHAIN (constructor_fields);
5975 constructor_unfilled_fields = constructor_fields;
5976 constructor_bit_index = bitsize_zero_node;
5978 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5980 if (TYPE_DOMAIN (constructor_type))
5982 constructor_max_index
5983 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5985 /* Detect non-empty initializations of zero-length arrays. */
5986 if (constructor_max_index == NULL_TREE
5987 && TYPE_SIZE (constructor_type))
5988 constructor_max_index = build_int_cst (NULL_TREE, -1);
5990 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5991 to initialize VLAs will cause a proper error; avoid tree
5992 checking errors as well by setting a safe value. */
5993 if (constructor_max_index
5994 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5995 constructor_max_index = build_int_cst (NULL_TREE, -1);
5997 constructor_index
5998 = convert (bitsizetype,
5999 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6001 else
6003 constructor_index = bitsize_zero_node;
6004 constructor_max_index = NULL_TREE;
6007 constructor_unfilled_index = constructor_index;
6009 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6011 /* Vectors are like simple fixed-size arrays. */
6012 constructor_max_index =
6013 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6014 constructor_index = bitsize_zero_node;
6015 constructor_unfilled_index = constructor_index;
6017 else
6019 /* Handle the case of int x = {5}; */
6020 constructor_fields = constructor_type;
6021 constructor_unfilled_fields = constructor_type;
6025 /* Push down into a subobject, for initialization.
6026 If this is for an explicit set of braces, IMPLICIT is 0.
6027 If it is because the next element belongs at a lower level,
6028 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6030 void
6031 push_init_level (int implicit)
6033 struct constructor_stack *p;
6034 tree value = NULL_TREE;
6036 /* If we've exhausted any levels that didn't have braces,
6037 pop them now. If implicit == 1, this will have been done in
6038 process_init_element; do not repeat it here because in the case
6039 of excess initializers for an empty aggregate this leads to an
6040 infinite cycle of popping a level and immediately recreating
6041 it. */
6042 if (implicit != 1)
6044 while (constructor_stack->implicit)
6046 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6047 || TREE_CODE (constructor_type) == UNION_TYPE)
6048 && constructor_fields == 0)
6049 process_init_element (pop_init_level (1), true);
6050 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6051 && constructor_max_index
6052 && tree_int_cst_lt (constructor_max_index,
6053 constructor_index))
6054 process_init_element (pop_init_level (1), true);
6055 else
6056 break;
6060 /* Unless this is an explicit brace, we need to preserve previous
6061 content if any. */
6062 if (implicit)
6064 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6065 || TREE_CODE (constructor_type) == UNION_TYPE)
6066 && constructor_fields)
6067 value = find_init_member (constructor_fields);
6068 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6069 value = find_init_member (constructor_index);
6072 p = XNEW (struct constructor_stack);
6073 p->type = constructor_type;
6074 p->fields = constructor_fields;
6075 p->index = constructor_index;
6076 p->max_index = constructor_max_index;
6077 p->unfilled_index = constructor_unfilled_index;
6078 p->unfilled_fields = constructor_unfilled_fields;
6079 p->bit_index = constructor_bit_index;
6080 p->elements = constructor_elements;
6081 p->constant = constructor_constant;
6082 p->simple = constructor_simple;
6083 p->nonconst = constructor_nonconst;
6084 p->erroneous = constructor_erroneous;
6085 p->pending_elts = constructor_pending_elts;
6086 p->depth = constructor_depth;
6087 p->replacement_value.value = 0;
6088 p->replacement_value.original_code = ERROR_MARK;
6089 p->replacement_value.original_type = NULL;
6090 p->implicit = implicit;
6091 p->outer = 0;
6092 p->incremental = constructor_incremental;
6093 p->designated = constructor_designated;
6094 p->next = constructor_stack;
6095 p->range_stack = 0;
6096 constructor_stack = p;
6098 constructor_constant = 1;
6099 constructor_simple = 1;
6100 constructor_nonconst = 0;
6101 constructor_depth = SPELLING_DEPTH ();
6102 constructor_elements = 0;
6103 constructor_incremental = 1;
6104 constructor_designated = 0;
6105 constructor_pending_elts = 0;
6106 if (!implicit)
6108 p->range_stack = constructor_range_stack;
6109 constructor_range_stack = 0;
6110 designator_depth = 0;
6111 designator_erroneous = 0;
6114 /* Don't die if an entire brace-pair level is superfluous
6115 in the containing level. */
6116 if (constructor_type == 0)
6118 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6119 || TREE_CODE (constructor_type) == UNION_TYPE)
6121 /* Don't die if there are extra init elts at the end. */
6122 if (constructor_fields == 0)
6123 constructor_type = 0;
6124 else
6126 constructor_type = TREE_TYPE (constructor_fields);
6127 push_member_name (constructor_fields);
6128 constructor_depth++;
6131 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6133 constructor_type = TREE_TYPE (constructor_type);
6134 push_array_bounds (tree_low_cst (constructor_index, 1));
6135 constructor_depth++;
6138 if (constructor_type == 0)
6140 error_init ("extra brace group at end of initializer");
6141 constructor_fields = 0;
6142 constructor_unfilled_fields = 0;
6143 return;
6146 if (value && TREE_CODE (value) == CONSTRUCTOR)
6148 constructor_constant = TREE_CONSTANT (value);
6149 constructor_simple = TREE_STATIC (value);
6150 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6151 constructor_elements = CONSTRUCTOR_ELTS (value);
6152 if (!VEC_empty (constructor_elt, constructor_elements)
6153 && (TREE_CODE (constructor_type) == RECORD_TYPE
6154 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6155 set_nonincremental_init ();
6158 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6160 missing_braces_mentioned = 1;
6161 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6164 if (TREE_CODE (constructor_type) == RECORD_TYPE
6165 || TREE_CODE (constructor_type) == UNION_TYPE)
6167 constructor_fields = TYPE_FIELDS (constructor_type);
6168 /* Skip any nameless bit fields at the beginning. */
6169 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6170 && DECL_NAME (constructor_fields) == 0)
6171 constructor_fields = TREE_CHAIN (constructor_fields);
6173 constructor_unfilled_fields = constructor_fields;
6174 constructor_bit_index = bitsize_zero_node;
6176 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6178 /* Vectors are like simple fixed-size arrays. */
6179 constructor_max_index =
6180 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6181 constructor_index = convert (bitsizetype, integer_zero_node);
6182 constructor_unfilled_index = constructor_index;
6184 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6186 if (TYPE_DOMAIN (constructor_type))
6188 constructor_max_index
6189 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6191 /* Detect non-empty initializations of zero-length arrays. */
6192 if (constructor_max_index == NULL_TREE
6193 && TYPE_SIZE (constructor_type))
6194 constructor_max_index = build_int_cst (NULL_TREE, -1);
6196 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6197 to initialize VLAs will cause a proper error; avoid tree
6198 checking errors as well by setting a safe value. */
6199 if (constructor_max_index
6200 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6201 constructor_max_index = build_int_cst (NULL_TREE, -1);
6203 constructor_index
6204 = convert (bitsizetype,
6205 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6207 else
6208 constructor_index = bitsize_zero_node;
6210 constructor_unfilled_index = constructor_index;
6211 if (value && TREE_CODE (value) == STRING_CST)
6213 /* We need to split the char/wchar array into individual
6214 characters, so that we don't have to special case it
6215 everywhere. */
6216 set_nonincremental_init_from_string (value);
6219 else
6221 if (constructor_type != error_mark_node)
6222 warning_init (0, "braces around scalar initializer");
6223 constructor_fields = constructor_type;
6224 constructor_unfilled_fields = constructor_type;
6228 /* At the end of an implicit or explicit brace level,
6229 finish up that level of constructor. If a single expression
6230 with redundant braces initialized that level, return the
6231 c_expr structure for that expression. Otherwise, the original_code
6232 element is set to ERROR_MARK.
6233 If we were outputting the elements as they are read, return 0 as the value
6234 from inner levels (process_init_element ignores that),
6235 but return error_mark_node as the value from the outermost level
6236 (that's what we want to put in DECL_INITIAL).
6237 Otherwise, return a CONSTRUCTOR expression as the value. */
6239 struct c_expr
6240 pop_init_level (int implicit)
6242 struct constructor_stack *p;
6243 struct c_expr ret;
6244 ret.value = 0;
6245 ret.original_code = ERROR_MARK;
6246 ret.original_type = NULL;
6248 if (implicit == 0)
6250 /* When we come to an explicit close brace,
6251 pop any inner levels that didn't have explicit braces. */
6252 while (constructor_stack->implicit)
6253 process_init_element (pop_init_level (1), true);
6255 gcc_assert (!constructor_range_stack);
6258 /* Now output all pending elements. */
6259 constructor_incremental = 1;
6260 output_pending_init_elements (1);
6262 p = constructor_stack;
6264 /* Error for initializing a flexible array member, or a zero-length
6265 array member in an inappropriate context. */
6266 if (constructor_type && constructor_fields
6267 && TREE_CODE (constructor_type) == ARRAY_TYPE
6268 && TYPE_DOMAIN (constructor_type)
6269 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6271 /* Silently discard empty initializations. The parser will
6272 already have pedwarned for empty brackets. */
6273 if (integer_zerop (constructor_unfilled_index))
6274 constructor_type = NULL_TREE;
6275 else
6277 gcc_assert (!TYPE_SIZE (constructor_type));
6279 if (constructor_depth > 2)
6280 error_init ("initialization of flexible array member in a nested context");
6281 else
6282 pedwarn_init (input_location, OPT_pedantic,
6283 "initialization of a flexible array member");
6285 /* We have already issued an error message for the existence
6286 of a flexible array member not at the end of the structure.
6287 Discard the initializer so that we do not die later. */
6288 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6289 constructor_type = NULL_TREE;
6293 /* Warn when some struct elements are implicitly initialized to zero. */
6294 if (warn_missing_field_initializers
6295 && constructor_type
6296 && TREE_CODE (constructor_type) == RECORD_TYPE
6297 && constructor_unfilled_fields)
6299 /* Do not warn for flexible array members or zero-length arrays. */
6300 while (constructor_unfilled_fields
6301 && (!DECL_SIZE (constructor_unfilled_fields)
6302 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6303 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6305 /* Do not warn if this level of the initializer uses member
6306 designators; it is likely to be deliberate. */
6307 if (constructor_unfilled_fields && !constructor_designated)
6309 push_member_name (constructor_unfilled_fields);
6310 warning_init (OPT_Wmissing_field_initializers,
6311 "missing initializer");
6312 RESTORE_SPELLING_DEPTH (constructor_depth);
6316 /* Pad out the end of the structure. */
6317 if (p->replacement_value.value)
6318 /* If this closes a superfluous brace pair,
6319 just pass out the element between them. */
6320 ret = p->replacement_value;
6321 else if (constructor_type == 0)
6323 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6324 && TREE_CODE (constructor_type) != UNION_TYPE
6325 && TREE_CODE (constructor_type) != ARRAY_TYPE
6326 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6328 /* A nonincremental scalar initializer--just return
6329 the element, after verifying there is just one. */
6330 if (VEC_empty (constructor_elt,constructor_elements))
6332 if (!constructor_erroneous)
6333 error_init ("empty scalar initializer");
6334 ret.value = error_mark_node;
6336 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6338 error_init ("extra elements in scalar initializer");
6339 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6341 else
6342 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6344 else
6346 if (constructor_erroneous)
6347 ret.value = error_mark_node;
6348 else
6350 ret.value = build_constructor (constructor_type,
6351 constructor_elements);
6352 if (constructor_constant)
6353 TREE_CONSTANT (ret.value) = 1;
6354 if (constructor_constant && constructor_simple)
6355 TREE_STATIC (ret.value) = 1;
6356 if (constructor_nonconst)
6357 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6361 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6363 if (constructor_nonconst)
6364 ret.original_code = C_MAYBE_CONST_EXPR;
6365 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6366 ret.original_code = ERROR_MARK;
6369 constructor_type = p->type;
6370 constructor_fields = p->fields;
6371 constructor_index = p->index;
6372 constructor_max_index = p->max_index;
6373 constructor_unfilled_index = p->unfilled_index;
6374 constructor_unfilled_fields = p->unfilled_fields;
6375 constructor_bit_index = p->bit_index;
6376 constructor_elements = p->elements;
6377 constructor_constant = p->constant;
6378 constructor_simple = p->simple;
6379 constructor_nonconst = p->nonconst;
6380 constructor_erroneous = p->erroneous;
6381 constructor_incremental = p->incremental;
6382 constructor_designated = p->designated;
6383 constructor_pending_elts = p->pending_elts;
6384 constructor_depth = p->depth;
6385 if (!p->implicit)
6386 constructor_range_stack = p->range_stack;
6387 RESTORE_SPELLING_DEPTH (constructor_depth);
6389 constructor_stack = p->next;
6390 free (p);
6392 if (ret.value == 0 && constructor_stack == 0)
6393 ret.value = error_mark_node;
6394 return ret;
6397 /* Common handling for both array range and field name designators.
6398 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6400 static int
6401 set_designator (int array)
6403 tree subtype;
6404 enum tree_code subcode;
6406 /* Don't die if an entire brace-pair level is superfluous
6407 in the containing level. */
6408 if (constructor_type == 0)
6409 return 1;
6411 /* If there were errors in this designator list already, bail out
6412 silently. */
6413 if (designator_erroneous)
6414 return 1;
6416 if (!designator_depth)
6418 gcc_assert (!constructor_range_stack);
6420 /* Designator list starts at the level of closest explicit
6421 braces. */
6422 while (constructor_stack->implicit)
6423 process_init_element (pop_init_level (1), true);
6424 constructor_designated = 1;
6425 return 0;
6428 switch (TREE_CODE (constructor_type))
6430 case RECORD_TYPE:
6431 case UNION_TYPE:
6432 subtype = TREE_TYPE (constructor_fields);
6433 if (subtype != error_mark_node)
6434 subtype = TYPE_MAIN_VARIANT (subtype);
6435 break;
6436 case ARRAY_TYPE:
6437 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6438 break;
6439 default:
6440 gcc_unreachable ();
6443 subcode = TREE_CODE (subtype);
6444 if (array && subcode != ARRAY_TYPE)
6446 error_init ("array index in non-array initializer");
6447 return 1;
6449 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6451 error_init ("field name not in record or union initializer");
6452 return 1;
6455 constructor_designated = 1;
6456 push_init_level (2);
6457 return 0;
6460 /* If there are range designators in designator list, push a new designator
6461 to constructor_range_stack. RANGE_END is end of such stack range or
6462 NULL_TREE if there is no range designator at this level. */
6464 static void
6465 push_range_stack (tree range_end)
6467 struct constructor_range_stack *p;
6469 p = GGC_NEW (struct constructor_range_stack);
6470 p->prev = constructor_range_stack;
6471 p->next = 0;
6472 p->fields = constructor_fields;
6473 p->range_start = constructor_index;
6474 p->index = constructor_index;
6475 p->stack = constructor_stack;
6476 p->range_end = range_end;
6477 if (constructor_range_stack)
6478 constructor_range_stack->next = p;
6479 constructor_range_stack = p;
6482 /* Within an array initializer, specify the next index to be initialized.
6483 FIRST is that index. If LAST is nonzero, then initialize a range
6484 of indices, running from FIRST through LAST. */
6486 void
6487 set_init_index (tree first, tree last)
6489 if (set_designator (1))
6490 return;
6492 designator_erroneous = 1;
6494 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6495 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6497 error_init ("array index in initializer not of integer type");
6498 return;
6501 if (TREE_CODE (first) != INTEGER_CST)
6503 first = c_fully_fold (first, false, NULL);
6504 if (TREE_CODE (first) == INTEGER_CST)
6505 pedwarn_init (input_location, OPT_pedantic,
6506 "array index in initializer is not "
6507 "an integer constant expression");
6510 if (last && TREE_CODE (last) != INTEGER_CST)
6512 last = c_fully_fold (last, false, NULL);
6513 if (TREE_CODE (last) == INTEGER_CST)
6514 pedwarn_init (input_location, OPT_pedantic,
6515 "array index in initializer is not "
6516 "an integer constant expression");
6519 if (TREE_CODE (first) != INTEGER_CST)
6520 error_init ("nonconstant array index in initializer");
6521 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6522 error_init ("nonconstant array index in initializer");
6523 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6524 error_init ("array index in non-array initializer");
6525 else if (tree_int_cst_sgn (first) == -1)
6526 error_init ("array index in initializer exceeds array bounds");
6527 else if (constructor_max_index
6528 && tree_int_cst_lt (constructor_max_index, first))
6529 error_init ("array index in initializer exceeds array bounds");
6530 else
6532 constant_expression_warning (first);
6533 if (last)
6534 constant_expression_warning (last);
6535 constructor_index = convert (bitsizetype, first);
6537 if (last)
6539 if (tree_int_cst_equal (first, last))
6540 last = 0;
6541 else if (tree_int_cst_lt (last, first))
6543 error_init ("empty index range in initializer");
6544 last = 0;
6546 else
6548 last = convert (bitsizetype, last);
6549 if (constructor_max_index != 0
6550 && tree_int_cst_lt (constructor_max_index, last))
6552 error_init ("array index range in initializer exceeds array bounds");
6553 last = 0;
6558 designator_depth++;
6559 designator_erroneous = 0;
6560 if (constructor_range_stack || last)
6561 push_range_stack (last);
6565 /* Within a struct initializer, specify the next field to be initialized. */
6567 void
6568 set_init_label (tree fieldname)
6570 tree tail;
6572 if (set_designator (0))
6573 return;
6575 designator_erroneous = 1;
6577 if (TREE_CODE (constructor_type) != RECORD_TYPE
6578 && TREE_CODE (constructor_type) != UNION_TYPE)
6580 error_init ("field name not in record or union initializer");
6581 return;
6584 for (tail = TYPE_FIELDS (constructor_type); tail;
6585 tail = TREE_CHAIN (tail))
6587 if (DECL_NAME (tail) == fieldname)
6588 break;
6591 if (tail == 0)
6592 error ("unknown field %qE specified in initializer", fieldname);
6593 else
6595 constructor_fields = tail;
6596 designator_depth++;
6597 designator_erroneous = 0;
6598 if (constructor_range_stack)
6599 push_range_stack (NULL_TREE);
6603 /* Add a new initializer to the tree of pending initializers. PURPOSE
6604 identifies the initializer, either array index or field in a structure.
6605 VALUE is the value of that index or field. If ORIGTYPE is not
6606 NULL_TREE, it is the original type of VALUE.
6608 IMPLICIT is true if value comes from pop_init_level (1),
6609 the new initializer has been merged with the existing one
6610 and thus no warnings should be emitted about overriding an
6611 existing initializer. */
6613 static void
6614 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6616 struct init_node *p, **q, *r;
6618 q = &constructor_pending_elts;
6619 p = 0;
6621 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6623 while (*q != 0)
6625 p = *q;
6626 if (tree_int_cst_lt (purpose, p->purpose))
6627 q = &p->left;
6628 else if (tree_int_cst_lt (p->purpose, purpose))
6629 q = &p->right;
6630 else
6632 if (!implicit)
6634 if (TREE_SIDE_EFFECTS (p->value))
6635 warning_init (0, "initialized field with side-effects overwritten");
6636 else if (warn_override_init)
6637 warning_init (OPT_Woverride_init, "initialized field overwritten");
6639 p->value = value;
6640 p->origtype = origtype;
6641 return;
6645 else
6647 tree bitpos;
6649 bitpos = bit_position (purpose);
6650 while (*q != NULL)
6652 p = *q;
6653 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6654 q = &p->left;
6655 else if (p->purpose != purpose)
6656 q = &p->right;
6657 else
6659 if (!implicit)
6661 if (TREE_SIDE_EFFECTS (p->value))
6662 warning_init (0, "initialized field with side-effects overwritten");
6663 else if (warn_override_init)
6664 warning_init (OPT_Woverride_init, "initialized field overwritten");
6666 p->value = value;
6667 p->origtype = origtype;
6668 return;
6673 r = GGC_NEW (struct init_node);
6674 r->purpose = purpose;
6675 r->value = value;
6676 r->origtype = origtype;
6678 *q = r;
6679 r->parent = p;
6680 r->left = 0;
6681 r->right = 0;
6682 r->balance = 0;
6684 while (p)
6686 struct init_node *s;
6688 if (r == p->left)
6690 if (p->balance == 0)
6691 p->balance = -1;
6692 else if (p->balance < 0)
6694 if (r->balance < 0)
6696 /* L rotation. */
6697 p->left = r->right;
6698 if (p->left)
6699 p->left->parent = p;
6700 r->right = p;
6702 p->balance = 0;
6703 r->balance = 0;
6705 s = p->parent;
6706 p->parent = r;
6707 r->parent = s;
6708 if (s)
6710 if (s->left == p)
6711 s->left = r;
6712 else
6713 s->right = r;
6715 else
6716 constructor_pending_elts = r;
6718 else
6720 /* LR rotation. */
6721 struct init_node *t = r->right;
6723 r->right = t->left;
6724 if (r->right)
6725 r->right->parent = r;
6726 t->left = r;
6728 p->left = t->right;
6729 if (p->left)
6730 p->left->parent = p;
6731 t->right = p;
6733 p->balance = t->balance < 0;
6734 r->balance = -(t->balance > 0);
6735 t->balance = 0;
6737 s = p->parent;
6738 p->parent = t;
6739 r->parent = t;
6740 t->parent = s;
6741 if (s)
6743 if (s->left == p)
6744 s->left = t;
6745 else
6746 s->right = t;
6748 else
6749 constructor_pending_elts = t;
6751 break;
6753 else
6755 /* p->balance == +1; growth of left side balances the node. */
6756 p->balance = 0;
6757 break;
6760 else /* r == p->right */
6762 if (p->balance == 0)
6763 /* Growth propagation from right side. */
6764 p->balance++;
6765 else if (p->balance > 0)
6767 if (r->balance > 0)
6769 /* R rotation. */
6770 p->right = r->left;
6771 if (p->right)
6772 p->right->parent = p;
6773 r->left = p;
6775 p->balance = 0;
6776 r->balance = 0;
6778 s = p->parent;
6779 p->parent = r;
6780 r->parent = s;
6781 if (s)
6783 if (s->left == p)
6784 s->left = r;
6785 else
6786 s->right = r;
6788 else
6789 constructor_pending_elts = r;
6791 else /* r->balance == -1 */
6793 /* RL rotation */
6794 struct init_node *t = r->left;
6796 r->left = t->right;
6797 if (r->left)
6798 r->left->parent = r;
6799 t->right = r;
6801 p->right = t->left;
6802 if (p->right)
6803 p->right->parent = p;
6804 t->left = p;
6806 r->balance = (t->balance < 0);
6807 p->balance = -(t->balance > 0);
6808 t->balance = 0;
6810 s = p->parent;
6811 p->parent = t;
6812 r->parent = t;
6813 t->parent = s;
6814 if (s)
6816 if (s->left == p)
6817 s->left = t;
6818 else
6819 s->right = t;
6821 else
6822 constructor_pending_elts = t;
6824 break;
6826 else
6828 /* p->balance == -1; growth of right side balances the node. */
6829 p->balance = 0;
6830 break;
6834 r = p;
6835 p = p->parent;
6839 /* Build AVL tree from a sorted chain. */
6841 static void
6842 set_nonincremental_init (void)
6844 unsigned HOST_WIDE_INT ix;
6845 tree index, value;
6847 if (TREE_CODE (constructor_type) != RECORD_TYPE
6848 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6849 return;
6851 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6852 add_pending_init (index, value, NULL_TREE, false);
6853 constructor_elements = 0;
6854 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6856 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6857 /* Skip any nameless bit fields at the beginning. */
6858 while (constructor_unfilled_fields != 0
6859 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6860 && DECL_NAME (constructor_unfilled_fields) == 0)
6861 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6864 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6866 if (TYPE_DOMAIN (constructor_type))
6867 constructor_unfilled_index
6868 = convert (bitsizetype,
6869 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6870 else
6871 constructor_unfilled_index = bitsize_zero_node;
6873 constructor_incremental = 0;
6876 /* Build AVL tree from a string constant. */
6878 static void
6879 set_nonincremental_init_from_string (tree str)
6881 tree value, purpose, type;
6882 HOST_WIDE_INT val[2];
6883 const char *p, *end;
6884 int byte, wchar_bytes, charwidth, bitpos;
6886 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6888 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6889 charwidth = TYPE_PRECISION (char_type_node);
6890 type = TREE_TYPE (constructor_type);
6891 p = TREE_STRING_POINTER (str);
6892 end = p + TREE_STRING_LENGTH (str);
6894 for (purpose = bitsize_zero_node;
6895 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6896 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6898 if (wchar_bytes == 1)
6900 val[1] = (unsigned char) *p++;
6901 val[0] = 0;
6903 else
6905 val[0] = 0;
6906 val[1] = 0;
6907 for (byte = 0; byte < wchar_bytes; byte++)
6909 if (BYTES_BIG_ENDIAN)
6910 bitpos = (wchar_bytes - byte - 1) * charwidth;
6911 else
6912 bitpos = byte * charwidth;
6913 val[bitpos < HOST_BITS_PER_WIDE_INT]
6914 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6915 << (bitpos % HOST_BITS_PER_WIDE_INT);
6919 if (!TYPE_UNSIGNED (type))
6921 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6922 if (bitpos < HOST_BITS_PER_WIDE_INT)
6924 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6926 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6927 val[0] = -1;
6930 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6932 if (val[1] < 0)
6933 val[0] = -1;
6935 else if (val[0] & (((HOST_WIDE_INT) 1)
6936 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6937 val[0] |= ((HOST_WIDE_INT) -1)
6938 << (bitpos - HOST_BITS_PER_WIDE_INT);
6941 value = build_int_cst_wide (type, val[1], val[0]);
6942 add_pending_init (purpose, value, NULL_TREE, false);
6945 constructor_incremental = 0;
6948 /* Return value of FIELD in pending initializer or zero if the field was
6949 not initialized yet. */
6951 static tree
6952 find_init_member (tree field)
6954 struct init_node *p;
6956 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6958 if (constructor_incremental
6959 && tree_int_cst_lt (field, constructor_unfilled_index))
6960 set_nonincremental_init ();
6962 p = constructor_pending_elts;
6963 while (p)
6965 if (tree_int_cst_lt (field, p->purpose))
6966 p = p->left;
6967 else if (tree_int_cst_lt (p->purpose, field))
6968 p = p->right;
6969 else
6970 return p->value;
6973 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6975 tree bitpos = bit_position (field);
6977 if (constructor_incremental
6978 && (!constructor_unfilled_fields
6979 || tree_int_cst_lt (bitpos,
6980 bit_position (constructor_unfilled_fields))))
6981 set_nonincremental_init ();
6983 p = constructor_pending_elts;
6984 while (p)
6986 if (field == p->purpose)
6987 return p->value;
6988 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6989 p = p->left;
6990 else
6991 p = p->right;
6994 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6996 if (!VEC_empty (constructor_elt, constructor_elements)
6997 && (VEC_last (constructor_elt, constructor_elements)->index
6998 == field))
6999 return VEC_last (constructor_elt, constructor_elements)->value;
7001 return 0;
7004 /* "Output" the next constructor element.
7005 At top level, really output it to assembler code now.
7006 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7007 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7008 TYPE is the data type that the containing data type wants here.
7009 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7010 If VALUE is a string constant, STRICT_STRING is true if it is
7011 unparenthesized or we should not warn here for it being parenthesized.
7012 For other types of VALUE, STRICT_STRING is not used.
7014 PENDING if non-nil means output pending elements that belong
7015 right after this element. (PENDING is normally 1;
7016 it is 0 while outputting pending elements, to avoid recursion.)
7018 IMPLICIT is true if value comes from pop_init_level (1),
7019 the new initializer has been merged with the existing one
7020 and thus no warnings should be emitted about overriding an
7021 existing initializer. */
7023 static void
7024 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7025 tree field, int pending, bool implicit)
7027 tree semantic_type = NULL_TREE;
7028 constructor_elt *celt;
7029 bool maybe_const = true;
7030 bool npc;
7032 if (type == error_mark_node || value == error_mark_node)
7034 constructor_erroneous = 1;
7035 return;
7037 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7038 && (TREE_CODE (value) == STRING_CST
7039 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7040 && !(TREE_CODE (value) == STRING_CST
7041 && TREE_CODE (type) == ARRAY_TYPE
7042 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7043 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7044 TYPE_MAIN_VARIANT (type)))
7045 value = array_to_pointer_conversion (value);
7047 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7048 && require_constant_value && !flag_isoc99 && pending)
7050 /* As an extension, allow initializing objects with static storage
7051 duration with compound literals (which are then treated just as
7052 the brace enclosed list they contain). */
7053 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7054 value = DECL_INITIAL (decl);
7057 npc = null_pointer_constant_p (value);
7058 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7060 semantic_type = TREE_TYPE (value);
7061 value = TREE_OPERAND (value, 0);
7063 value = c_fully_fold (value, require_constant_value, &maybe_const);
7065 if (value == error_mark_node)
7066 constructor_erroneous = 1;
7067 else if (!TREE_CONSTANT (value))
7068 constructor_constant = 0;
7069 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7070 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7071 || TREE_CODE (constructor_type) == UNION_TYPE)
7072 && DECL_C_BIT_FIELD (field)
7073 && TREE_CODE (value) != INTEGER_CST))
7074 constructor_simple = 0;
7075 if (!maybe_const)
7076 constructor_nonconst = 1;
7078 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7080 if (require_constant_value)
7082 error_init ("initializer element is not constant");
7083 value = error_mark_node;
7085 else if (require_constant_elements)
7086 pedwarn (input_location, 0,
7087 "initializer element is not computable at load time");
7089 else if (!maybe_const
7090 && (require_constant_value || require_constant_elements))
7091 pedwarn_init (input_location, 0,
7092 "initializer element is not a constant expression");
7094 /* Issue -Wc++-compat warnings about initializing a bitfield with
7095 enum type. */
7096 if (warn_cxx_compat
7097 && field != NULL_TREE
7098 && TREE_CODE (field) == FIELD_DECL
7099 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7100 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7101 != TYPE_MAIN_VARIANT (type))
7102 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7104 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7105 if (checktype != error_mark_node
7106 && (TYPE_MAIN_VARIANT (checktype)
7107 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7108 warning_init (OPT_Wc___compat,
7109 "enum conversion in initialization is invalid in C++");
7112 /* If this field is empty (and not at the end of structure),
7113 don't do anything other than checking the initializer. */
7114 if (field
7115 && (TREE_TYPE (field) == error_mark_node
7116 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7117 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7118 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7119 || TREE_CHAIN (field)))))
7120 return;
7122 if (semantic_type)
7123 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7124 value = digest_init (type, value, origtype, npc, strict_string,
7125 require_constant_value);
7126 if (value == error_mark_node)
7128 constructor_erroneous = 1;
7129 return;
7131 if (require_constant_value || require_constant_elements)
7132 constant_expression_warning (value);
7134 /* If this element doesn't come next in sequence,
7135 put it on constructor_pending_elts. */
7136 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7137 && (!constructor_incremental
7138 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7140 if (constructor_incremental
7141 && tree_int_cst_lt (field, constructor_unfilled_index))
7142 set_nonincremental_init ();
7144 add_pending_init (field, value, origtype, implicit);
7145 return;
7147 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7148 && (!constructor_incremental
7149 || field != constructor_unfilled_fields))
7151 /* We do this for records but not for unions. In a union,
7152 no matter which field is specified, it can be initialized
7153 right away since it starts at the beginning of the union. */
7154 if (constructor_incremental)
7156 if (!constructor_unfilled_fields)
7157 set_nonincremental_init ();
7158 else
7160 tree bitpos, unfillpos;
7162 bitpos = bit_position (field);
7163 unfillpos = bit_position (constructor_unfilled_fields);
7165 if (tree_int_cst_lt (bitpos, unfillpos))
7166 set_nonincremental_init ();
7170 add_pending_init (field, value, origtype, implicit);
7171 return;
7173 else if (TREE_CODE (constructor_type) == UNION_TYPE
7174 && !VEC_empty (constructor_elt, constructor_elements))
7176 if (!implicit)
7178 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7179 constructor_elements)->value))
7180 warning_init (0,
7181 "initialized field with side-effects overwritten");
7182 else if (warn_override_init)
7183 warning_init (OPT_Woverride_init, "initialized field overwritten");
7186 /* We can have just one union field set. */
7187 constructor_elements = 0;
7190 /* Otherwise, output this element either to
7191 constructor_elements or to the assembler file. */
7193 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7194 celt->index = field;
7195 celt->value = value;
7197 /* Advance the variable that indicates sequential elements output. */
7198 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7199 constructor_unfilled_index
7200 = size_binop (PLUS_EXPR, constructor_unfilled_index,
7201 bitsize_one_node);
7202 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7204 constructor_unfilled_fields
7205 = TREE_CHAIN (constructor_unfilled_fields);
7207 /* Skip any nameless bit fields. */
7208 while (constructor_unfilled_fields != 0
7209 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7210 && DECL_NAME (constructor_unfilled_fields) == 0)
7211 constructor_unfilled_fields =
7212 TREE_CHAIN (constructor_unfilled_fields);
7214 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7215 constructor_unfilled_fields = 0;
7217 /* Now output any pending elements which have become next. */
7218 if (pending)
7219 output_pending_init_elements (0);
7222 /* Output any pending elements which have become next.
7223 As we output elements, constructor_unfilled_{fields,index}
7224 advances, which may cause other elements to become next;
7225 if so, they too are output.
7227 If ALL is 0, we return when there are
7228 no more pending elements to output now.
7230 If ALL is 1, we output space as necessary so that
7231 we can output all the pending elements. */
7233 static void
7234 output_pending_init_elements (int all)
7236 struct init_node *elt = constructor_pending_elts;
7237 tree next;
7239 retry:
7241 /* Look through the whole pending tree.
7242 If we find an element that should be output now,
7243 output it. Otherwise, set NEXT to the element
7244 that comes first among those still pending. */
7246 next = 0;
7247 while (elt)
7249 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7251 if (tree_int_cst_equal (elt->purpose,
7252 constructor_unfilled_index))
7253 output_init_element (elt->value, elt->origtype, true,
7254 TREE_TYPE (constructor_type),
7255 constructor_unfilled_index, 0, false);
7256 else if (tree_int_cst_lt (constructor_unfilled_index,
7257 elt->purpose))
7259 /* Advance to the next smaller node. */
7260 if (elt->left)
7261 elt = elt->left;
7262 else
7264 /* We have reached the smallest node bigger than the
7265 current unfilled index. Fill the space first. */
7266 next = elt->purpose;
7267 break;
7270 else
7272 /* Advance to the next bigger node. */
7273 if (elt->right)
7274 elt = elt->right;
7275 else
7277 /* We have reached the biggest node in a subtree. Find
7278 the parent of it, which is the next bigger node. */
7279 while (elt->parent && elt->parent->right == elt)
7280 elt = elt->parent;
7281 elt = elt->parent;
7282 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7283 elt->purpose))
7285 next = elt->purpose;
7286 break;
7291 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7292 || TREE_CODE (constructor_type) == UNION_TYPE)
7294 tree ctor_unfilled_bitpos, elt_bitpos;
7296 /* If the current record is complete we are done. */
7297 if (constructor_unfilled_fields == 0)
7298 break;
7300 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7301 elt_bitpos = bit_position (elt->purpose);
7302 /* We can't compare fields here because there might be empty
7303 fields in between. */
7304 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7306 constructor_unfilled_fields = elt->purpose;
7307 output_init_element (elt->value, elt->origtype, true,
7308 TREE_TYPE (elt->purpose),
7309 elt->purpose, 0, false);
7311 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7313 /* Advance to the next smaller node. */
7314 if (elt->left)
7315 elt = elt->left;
7316 else
7318 /* We have reached the smallest node bigger than the
7319 current unfilled field. Fill the space first. */
7320 next = elt->purpose;
7321 break;
7324 else
7326 /* Advance to the next bigger node. */
7327 if (elt->right)
7328 elt = elt->right;
7329 else
7331 /* We have reached the biggest node in a subtree. Find
7332 the parent of it, which is the next bigger node. */
7333 while (elt->parent && elt->parent->right == elt)
7334 elt = elt->parent;
7335 elt = elt->parent;
7336 if (elt
7337 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7338 bit_position (elt->purpose))))
7340 next = elt->purpose;
7341 break;
7348 /* Ordinarily return, but not if we want to output all
7349 and there are elements left. */
7350 if (!(all && next != 0))
7351 return;
7353 /* If it's not incremental, just skip over the gap, so that after
7354 jumping to retry we will output the next successive element. */
7355 if (TREE_CODE (constructor_type) == RECORD_TYPE
7356 || TREE_CODE (constructor_type) == UNION_TYPE)
7357 constructor_unfilled_fields = next;
7358 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7359 constructor_unfilled_index = next;
7361 /* ELT now points to the node in the pending tree with the next
7362 initializer to output. */
7363 goto retry;
7366 /* Add one non-braced element to the current constructor level.
7367 This adjusts the current position within the constructor's type.
7368 This may also start or terminate implicit levels
7369 to handle a partly-braced initializer.
7371 Once this has found the correct level for the new element,
7372 it calls output_init_element.
7374 IMPLICIT is true if value comes from pop_init_level (1),
7375 the new initializer has been merged with the existing one
7376 and thus no warnings should be emitted about overriding an
7377 existing initializer. */
7379 void
7380 process_init_element (struct c_expr value, bool implicit)
7382 tree orig_value = value.value;
7383 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7384 bool strict_string = value.original_code == STRING_CST;
7386 designator_depth = 0;
7387 designator_erroneous = 0;
7389 /* Handle superfluous braces around string cst as in
7390 char x[] = {"foo"}; */
7391 if (string_flag
7392 && constructor_type
7393 && TREE_CODE (constructor_type) == ARRAY_TYPE
7394 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7395 && integer_zerop (constructor_unfilled_index))
7397 if (constructor_stack->replacement_value.value)
7398 error_init ("excess elements in char array initializer");
7399 constructor_stack->replacement_value = value;
7400 return;
7403 if (constructor_stack->replacement_value.value != 0)
7405 error_init ("excess elements in struct initializer");
7406 return;
7409 /* Ignore elements of a brace group if it is entirely superfluous
7410 and has already been diagnosed. */
7411 if (constructor_type == 0)
7412 return;
7414 /* If we've exhausted any levels that didn't have braces,
7415 pop them now. */
7416 while (constructor_stack->implicit)
7418 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7419 || TREE_CODE (constructor_type) == UNION_TYPE)
7420 && constructor_fields == 0)
7421 process_init_element (pop_init_level (1), true);
7422 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7423 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7424 && (constructor_max_index == 0
7425 || tree_int_cst_lt (constructor_max_index,
7426 constructor_index)))
7427 process_init_element (pop_init_level (1), true);
7428 else
7429 break;
7432 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7433 if (constructor_range_stack)
7435 /* If value is a compound literal and we'll be just using its
7436 content, don't put it into a SAVE_EXPR. */
7437 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7438 || !require_constant_value
7439 || flag_isoc99)
7441 tree semantic_type = NULL_TREE;
7442 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7444 semantic_type = TREE_TYPE (value.value);
7445 value.value = TREE_OPERAND (value.value, 0);
7447 value.value = c_save_expr (value.value);
7448 if (semantic_type)
7449 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7450 value.value);
7454 while (1)
7456 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7458 tree fieldtype;
7459 enum tree_code fieldcode;
7461 if (constructor_fields == 0)
7463 pedwarn_init (input_location, 0,
7464 "excess elements in struct initializer");
7465 break;
7468 fieldtype = TREE_TYPE (constructor_fields);
7469 if (fieldtype != error_mark_node)
7470 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7471 fieldcode = TREE_CODE (fieldtype);
7473 /* Error for non-static initialization of a flexible array member. */
7474 if (fieldcode == ARRAY_TYPE
7475 && !require_constant_value
7476 && TYPE_SIZE (fieldtype) == NULL_TREE
7477 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7479 error_init ("non-static initialization of a flexible array member");
7480 break;
7483 /* Accept a string constant to initialize a subarray. */
7484 if (value.value != 0
7485 && fieldcode == ARRAY_TYPE
7486 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7487 && string_flag)
7488 value.value = orig_value;
7489 /* Otherwise, if we have come to a subaggregate,
7490 and we don't have an element of its type, push into it. */
7491 else if (value.value != 0
7492 && value.value != error_mark_node
7493 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7494 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7495 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7497 push_init_level (1);
7498 continue;
7501 if (value.value)
7503 push_member_name (constructor_fields);
7504 output_init_element (value.value, value.original_type,
7505 strict_string, fieldtype,
7506 constructor_fields, 1, implicit);
7507 RESTORE_SPELLING_DEPTH (constructor_depth);
7509 else
7510 /* Do the bookkeeping for an element that was
7511 directly output as a constructor. */
7513 /* For a record, keep track of end position of last field. */
7514 if (DECL_SIZE (constructor_fields))
7515 constructor_bit_index
7516 = size_binop (PLUS_EXPR,
7517 bit_position (constructor_fields),
7518 DECL_SIZE (constructor_fields));
7520 /* If the current field was the first one not yet written out,
7521 it isn't now, so update. */
7522 if (constructor_unfilled_fields == constructor_fields)
7524 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7525 /* Skip any nameless bit fields. */
7526 while (constructor_unfilled_fields != 0
7527 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7528 && DECL_NAME (constructor_unfilled_fields) == 0)
7529 constructor_unfilled_fields =
7530 TREE_CHAIN (constructor_unfilled_fields);
7534 constructor_fields = TREE_CHAIN (constructor_fields);
7535 /* Skip any nameless bit fields at the beginning. */
7536 while (constructor_fields != 0
7537 && DECL_C_BIT_FIELD (constructor_fields)
7538 && DECL_NAME (constructor_fields) == 0)
7539 constructor_fields = TREE_CHAIN (constructor_fields);
7541 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7543 tree fieldtype;
7544 enum tree_code fieldcode;
7546 if (constructor_fields == 0)
7548 pedwarn_init (input_location, 0,
7549 "excess elements in union initializer");
7550 break;
7553 fieldtype = TREE_TYPE (constructor_fields);
7554 if (fieldtype != error_mark_node)
7555 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7556 fieldcode = TREE_CODE (fieldtype);
7558 /* Warn that traditional C rejects initialization of unions.
7559 We skip the warning if the value is zero. This is done
7560 under the assumption that the zero initializer in user
7561 code appears conditioned on e.g. __STDC__ to avoid
7562 "missing initializer" warnings and relies on default
7563 initialization to zero in the traditional C case.
7564 We also skip the warning if the initializer is designated,
7565 again on the assumption that this must be conditional on
7566 __STDC__ anyway (and we've already complained about the
7567 member-designator already). */
7568 if (!in_system_header && !constructor_designated
7569 && !(value.value && (integer_zerop (value.value)
7570 || real_zerop (value.value))))
7571 warning (OPT_Wtraditional, "traditional C rejects initialization "
7572 "of unions");
7574 /* Accept a string constant to initialize a subarray. */
7575 if (value.value != 0
7576 && fieldcode == ARRAY_TYPE
7577 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7578 && string_flag)
7579 value.value = orig_value;
7580 /* Otherwise, if we have come to a subaggregate,
7581 and we don't have an element of its type, push into it. */
7582 else if (value.value != 0
7583 && value.value != error_mark_node
7584 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7585 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7586 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7588 push_init_level (1);
7589 continue;
7592 if (value.value)
7594 push_member_name (constructor_fields);
7595 output_init_element (value.value, value.original_type,
7596 strict_string, fieldtype,
7597 constructor_fields, 1, implicit);
7598 RESTORE_SPELLING_DEPTH (constructor_depth);
7600 else
7601 /* Do the bookkeeping for an element that was
7602 directly output as a constructor. */
7604 constructor_bit_index = DECL_SIZE (constructor_fields);
7605 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7608 constructor_fields = 0;
7610 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7612 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7613 enum tree_code eltcode = TREE_CODE (elttype);
7615 /* Accept a string constant to initialize a subarray. */
7616 if (value.value != 0
7617 && eltcode == ARRAY_TYPE
7618 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7619 && string_flag)
7620 value.value = orig_value;
7621 /* Otherwise, if we have come to a subaggregate,
7622 and we don't have an element of its type, push into it. */
7623 else if (value.value != 0
7624 && value.value != error_mark_node
7625 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7626 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7627 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7629 push_init_level (1);
7630 continue;
7633 if (constructor_max_index != 0
7634 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7635 || integer_all_onesp (constructor_max_index)))
7637 pedwarn_init (input_location, 0,
7638 "excess elements in array initializer");
7639 break;
7642 /* Now output the actual element. */
7643 if (value.value)
7645 push_array_bounds (tree_low_cst (constructor_index, 1));
7646 output_init_element (value.value, value.original_type,
7647 strict_string, elttype,
7648 constructor_index, 1, implicit);
7649 RESTORE_SPELLING_DEPTH (constructor_depth);
7652 constructor_index
7653 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7655 if (!value.value)
7656 /* If we are doing the bookkeeping for an element that was
7657 directly output as a constructor, we must update
7658 constructor_unfilled_index. */
7659 constructor_unfilled_index = constructor_index;
7661 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7663 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7665 /* Do a basic check of initializer size. Note that vectors
7666 always have a fixed size derived from their type. */
7667 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7669 pedwarn_init (input_location, 0,
7670 "excess elements in vector initializer");
7671 break;
7674 /* Now output the actual element. */
7675 if (value.value)
7677 if (TREE_CODE (value.value) == VECTOR_CST)
7678 elttype = TYPE_MAIN_VARIANT (constructor_type);
7679 output_init_element (value.value, value.original_type,
7680 strict_string, elttype,
7681 constructor_index, 1, implicit);
7684 constructor_index
7685 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7687 if (!value.value)
7688 /* If we are doing the bookkeeping for an element that was
7689 directly output as a constructor, we must update
7690 constructor_unfilled_index. */
7691 constructor_unfilled_index = constructor_index;
7694 /* Handle the sole element allowed in a braced initializer
7695 for a scalar variable. */
7696 else if (constructor_type != error_mark_node
7697 && constructor_fields == 0)
7699 pedwarn_init (input_location, 0,
7700 "excess elements in scalar initializer");
7701 break;
7703 else
7705 if (value.value)
7706 output_init_element (value.value, value.original_type,
7707 strict_string, constructor_type,
7708 NULL_TREE, 1, implicit);
7709 constructor_fields = 0;
7712 /* Handle range initializers either at this level or anywhere higher
7713 in the designator stack. */
7714 if (constructor_range_stack)
7716 struct constructor_range_stack *p, *range_stack;
7717 int finish = 0;
7719 range_stack = constructor_range_stack;
7720 constructor_range_stack = 0;
7721 while (constructor_stack != range_stack->stack)
7723 gcc_assert (constructor_stack->implicit);
7724 process_init_element (pop_init_level (1), true);
7726 for (p = range_stack;
7727 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7728 p = p->prev)
7730 gcc_assert (constructor_stack->implicit);
7731 process_init_element (pop_init_level (1), true);
7734 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7735 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7736 finish = 1;
7738 while (1)
7740 constructor_index = p->index;
7741 constructor_fields = p->fields;
7742 if (finish && p->range_end && p->index == p->range_start)
7744 finish = 0;
7745 p->prev = 0;
7747 p = p->next;
7748 if (!p)
7749 break;
7750 push_init_level (2);
7751 p->stack = constructor_stack;
7752 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7753 p->index = p->range_start;
7756 if (!finish)
7757 constructor_range_stack = range_stack;
7758 continue;
7761 break;
7764 constructor_range_stack = 0;
7767 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7768 (guaranteed to be 'volatile' or null) and ARGS (represented using
7769 an ASM_EXPR node). */
7770 tree
7771 build_asm_stmt (tree cv_qualifier, tree args)
7773 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7774 ASM_VOLATILE_P (args) = 1;
7775 return add_stmt (args);
7778 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7779 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7780 SIMPLE indicates whether there was anything at all after the
7781 string in the asm expression -- asm("blah") and asm("blah" : )
7782 are subtly different. We use a ASM_EXPR node to represent this. */
7783 tree
7784 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7785 bool simple)
7787 tree tail;
7788 tree args;
7789 int i;
7790 const char *constraint;
7791 const char **oconstraints;
7792 bool allows_mem, allows_reg, is_inout;
7793 int ninputs, noutputs;
7795 ninputs = list_length (inputs);
7796 noutputs = list_length (outputs);
7797 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7799 string = resolve_asm_operand_names (string, outputs, inputs);
7801 /* Remove output conversions that change the type but not the mode. */
7802 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7804 tree output = TREE_VALUE (tail);
7806 /* ??? Really, this should not be here. Users should be using a
7807 proper lvalue, dammit. But there's a long history of using casts
7808 in the output operands. In cases like longlong.h, this becomes a
7809 primitive form of typechecking -- if the cast can be removed, then
7810 the output operand had a type of the proper width; otherwise we'll
7811 get an error. Gross, but ... */
7812 STRIP_NOPS (output);
7814 if (!lvalue_or_else (output, lv_asm))
7815 output = error_mark_node;
7817 if (output != error_mark_node
7818 && (TREE_READONLY (output)
7819 || TYPE_READONLY (TREE_TYPE (output))
7820 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7821 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7822 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7823 readonly_error (output, lv_asm);
7825 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7826 oconstraints[i] = constraint;
7828 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7829 &allows_mem, &allows_reg, &is_inout))
7831 /* If the operand is going to end up in memory,
7832 mark it addressable. */
7833 if (!allows_reg && !c_mark_addressable (output))
7834 output = error_mark_node;
7836 else
7837 output = error_mark_node;
7839 TREE_VALUE (tail) = output;
7842 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7844 tree input;
7846 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7847 input = TREE_VALUE (tail);
7849 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7850 oconstraints, &allows_mem, &allows_reg))
7852 /* If the operand is going to end up in memory,
7853 mark it addressable. */
7854 if (!allows_reg && allows_mem)
7856 /* Strip the nops as we allow this case. FIXME, this really
7857 should be rejected or made deprecated. */
7858 STRIP_NOPS (input);
7859 if (!c_mark_addressable (input))
7860 input = error_mark_node;
7863 else
7864 input = error_mark_node;
7866 TREE_VALUE (tail) = input;
7869 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7871 /* asm statements without outputs, including simple ones, are treated
7872 as volatile. */
7873 ASM_INPUT_P (args) = simple;
7874 ASM_VOLATILE_P (args) = (noutputs == 0);
7876 return args;
7879 /* Generate a goto statement to LABEL. */
7881 tree
7882 c_finish_goto_label (tree label)
7884 tree decl = lookup_label (label);
7885 if (!decl)
7886 return NULL_TREE;
7888 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7890 error ("jump into statement expression");
7891 return NULL_TREE;
7894 if (C_DECL_UNJUMPABLE_VM (decl))
7896 error ("jump into scope of identifier with variably modified type");
7897 return NULL_TREE;
7900 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7902 /* No jump from outside this statement expression context, so
7903 record that there is a jump from within this context. */
7904 struct c_label_list *nlist;
7905 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7906 nlist->next = label_context_stack_se->labels_used;
7907 nlist->label = decl;
7908 label_context_stack_se->labels_used = nlist;
7911 if (!C_DECL_UNDEFINABLE_VM (decl))
7913 /* No jump from outside this context context of identifiers with
7914 variably modified type, so record that there is a jump from
7915 within this context. */
7916 struct c_label_list *nlist;
7917 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7918 nlist->next = label_context_stack_vm->labels_used;
7919 nlist->label = decl;
7920 label_context_stack_vm->labels_used = nlist;
7923 TREE_USED (decl) = 1;
7924 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7927 /* Generate a computed goto statement to EXPR. */
7929 tree
7930 c_finish_goto_ptr (tree expr)
7932 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7933 expr = c_fully_fold (expr, false, NULL);
7934 expr = convert (ptr_type_node, expr);
7935 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7938 /* Generate a C `return' statement. RETVAL is the expression for what
7939 to return, or a null pointer for `return;' with no value. If
7940 ORIGTYPE is not NULL_TREE, it is the original type of RETVAL. */
7942 tree
7943 c_finish_return (tree retval, tree origtype)
7945 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7946 bool no_warning = false;
7947 bool npc = false;
7949 if (TREE_THIS_VOLATILE (current_function_decl))
7950 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7952 if (retval)
7954 tree semantic_type = NULL_TREE;
7955 npc = null_pointer_constant_p (retval);
7956 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
7958 semantic_type = TREE_TYPE (retval);
7959 retval = TREE_OPERAND (retval, 0);
7961 retval = c_fully_fold (retval, false, NULL);
7962 if (semantic_type)
7963 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
7966 if (!retval)
7968 current_function_returns_null = 1;
7969 if ((warn_return_type || flag_isoc99)
7970 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7972 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7973 "%<return%> with no value, in "
7974 "function returning non-void");
7975 no_warning = true;
7978 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7980 current_function_returns_null = 1;
7981 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7982 pedwarn (input_location, 0,
7983 "%<return%> with a value, in function returning void");
7984 else
7985 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7986 "%<return%> with expression, in function returning void");
7988 else
7990 tree t = convert_for_assignment (valtype, retval, origtype, ic_return,
7991 npc, NULL_TREE, NULL_TREE, 0);
7992 tree res = DECL_RESULT (current_function_decl);
7993 tree inner;
7995 current_function_returns_value = 1;
7996 if (t == error_mark_node)
7997 return NULL_TREE;
7999 inner = t = convert (TREE_TYPE (res), t);
8001 /* Strip any conversions, additions, and subtractions, and see if
8002 we are returning the address of a local variable. Warn if so. */
8003 while (1)
8005 switch (TREE_CODE (inner))
8007 CASE_CONVERT:
8008 case NON_LVALUE_EXPR:
8009 case PLUS_EXPR:
8010 case POINTER_PLUS_EXPR:
8011 inner = TREE_OPERAND (inner, 0);
8012 continue;
8014 case MINUS_EXPR:
8015 /* If the second operand of the MINUS_EXPR has a pointer
8016 type (or is converted from it), this may be valid, so
8017 don't give a warning. */
8019 tree op1 = TREE_OPERAND (inner, 1);
8021 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8022 && (CONVERT_EXPR_P (op1)
8023 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8024 op1 = TREE_OPERAND (op1, 0);
8026 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8027 break;
8029 inner = TREE_OPERAND (inner, 0);
8030 continue;
8033 case ADDR_EXPR:
8034 inner = TREE_OPERAND (inner, 0);
8036 while (REFERENCE_CLASS_P (inner)
8037 && TREE_CODE (inner) != INDIRECT_REF)
8038 inner = TREE_OPERAND (inner, 0);
8040 if (DECL_P (inner)
8041 && !DECL_EXTERNAL (inner)
8042 && !TREE_STATIC (inner)
8043 && DECL_CONTEXT (inner) == current_function_decl)
8044 warning (0, "function returns address of local variable");
8045 break;
8047 default:
8048 break;
8051 break;
8054 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8056 if (warn_sequence_point)
8057 verify_sequence_points (retval);
8060 ret_stmt = build_stmt (RETURN_EXPR, retval);
8061 TREE_NO_WARNING (ret_stmt) |= no_warning;
8062 return add_stmt (ret_stmt);
8065 struct c_switch {
8066 /* The SWITCH_EXPR being built. */
8067 tree switch_expr;
8069 /* The original type of the testing expression, i.e. before the
8070 default conversion is applied. */
8071 tree orig_type;
8073 /* A splay-tree mapping the low element of a case range to the high
8074 element, or NULL_TREE if there is no high element. Used to
8075 determine whether or not a new case label duplicates an old case
8076 label. We need a tree, rather than simply a hash table, because
8077 of the GNU case range extension. */
8078 splay_tree cases;
8080 /* Number of nested statement expressions within this switch
8081 statement; if nonzero, case and default labels may not
8082 appear. */
8083 unsigned int blocked_stmt_expr;
8085 /* Scope of outermost declarations of identifiers with variably
8086 modified type within this switch statement; if nonzero, case and
8087 default labels may not appear. */
8088 unsigned int blocked_vm;
8090 /* The next node on the stack. */
8091 struct c_switch *next;
8094 /* A stack of the currently active switch statements. The innermost
8095 switch statement is on the top of the stack. There is no need to
8096 mark the stack for garbage collection because it is only active
8097 during the processing of the body of a function, and we never
8098 collect at that point. */
8100 struct c_switch *c_switch_stack;
8102 /* Start a C switch statement, testing expression EXP. Return the new
8103 SWITCH_EXPR. */
8105 tree
8106 c_start_case (tree exp)
8108 tree orig_type = error_mark_node;
8109 struct c_switch *cs;
8111 if (exp != error_mark_node)
8113 orig_type = TREE_TYPE (exp);
8115 if (!INTEGRAL_TYPE_P (orig_type))
8117 if (orig_type != error_mark_node)
8119 error ("switch quantity not an integer");
8120 orig_type = error_mark_node;
8122 exp = integer_zero_node;
8124 else
8126 tree type = TYPE_MAIN_VARIANT (orig_type);
8128 if (!in_system_header
8129 && (type == long_integer_type_node
8130 || type == long_unsigned_type_node))
8131 warning (OPT_Wtraditional, "%<long%> switch expression not "
8132 "converted to %<int%> in ISO C");
8134 exp = c_fully_fold (exp, false, NULL);
8135 exp = default_conversion (exp);
8137 if (warn_sequence_point)
8138 verify_sequence_points (exp);
8142 /* Add this new SWITCH_EXPR to the stack. */
8143 cs = XNEW (struct c_switch);
8144 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8145 cs->orig_type = orig_type;
8146 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8147 cs->blocked_stmt_expr = 0;
8148 cs->blocked_vm = 0;
8149 cs->next = c_switch_stack;
8150 c_switch_stack = cs;
8152 return add_stmt (cs->switch_expr);
8155 /* Process a case label. */
8157 tree
8158 do_case (tree low_value, tree high_value)
8160 tree label = NULL_TREE;
8162 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8164 low_value = c_fully_fold (low_value, false, NULL);
8165 if (TREE_CODE (low_value) == INTEGER_CST)
8166 pedwarn (input_location, OPT_pedantic,
8167 "case label is not an integer constant expression");
8170 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8172 high_value = c_fully_fold (high_value, false, NULL);
8173 if (TREE_CODE (high_value) == INTEGER_CST)
8174 pedwarn (input_location, OPT_pedantic,
8175 "case label is not an integer constant expression");
8178 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
8179 && !c_switch_stack->blocked_vm)
8181 label = c_add_case_label (c_switch_stack->cases,
8182 SWITCH_COND (c_switch_stack->switch_expr),
8183 c_switch_stack->orig_type,
8184 low_value, high_value);
8185 if (label == error_mark_node)
8186 label = NULL_TREE;
8188 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
8190 if (low_value)
8191 error ("case label in statement expression not containing "
8192 "enclosing switch statement");
8193 else
8194 error ("%<default%> label in statement expression not containing "
8195 "enclosing switch statement");
8197 else if (c_switch_stack && c_switch_stack->blocked_vm)
8199 if (low_value)
8200 error ("case label in scope of identifier with variably modified "
8201 "type not containing enclosing switch statement");
8202 else
8203 error ("%<default%> label in scope of identifier with variably "
8204 "modified type not containing enclosing switch statement");
8206 else if (low_value)
8207 error ("case label not within a switch statement");
8208 else
8209 error ("%<default%> label not within a switch statement");
8211 return label;
8214 /* Finish the switch statement. */
8216 void
8217 c_finish_case (tree body)
8219 struct c_switch *cs = c_switch_stack;
8220 location_t switch_location;
8222 SWITCH_BODY (cs->switch_expr) = body;
8224 /* We must not be within a statement expression nested in the switch
8225 at this point; we might, however, be within the scope of an
8226 identifier with variably modified type nested in the switch. */
8227 gcc_assert (!cs->blocked_stmt_expr);
8229 /* Emit warnings as needed. */
8230 if (EXPR_HAS_LOCATION (cs->switch_expr))
8231 switch_location = EXPR_LOCATION (cs->switch_expr);
8232 else
8233 switch_location = input_location;
8234 c_do_switch_warnings (cs->cases, switch_location,
8235 TREE_TYPE (cs->switch_expr),
8236 SWITCH_COND (cs->switch_expr));
8238 /* Pop the stack. */
8239 c_switch_stack = cs->next;
8240 splay_tree_delete (cs->cases);
8241 XDELETE (cs);
8244 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8245 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8246 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8247 statement, and was not surrounded with parenthesis. */
8249 void
8250 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8251 tree else_block, bool nested_if)
8253 tree stmt;
8255 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8256 if (warn_parentheses && nested_if && else_block == NULL)
8258 tree inner_if = then_block;
8260 /* We know from the grammar productions that there is an IF nested
8261 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8262 it might not be exactly THEN_BLOCK, but should be the last
8263 non-container statement within. */
8264 while (1)
8265 switch (TREE_CODE (inner_if))
8267 case COND_EXPR:
8268 goto found;
8269 case BIND_EXPR:
8270 inner_if = BIND_EXPR_BODY (inner_if);
8271 break;
8272 case STATEMENT_LIST:
8273 inner_if = expr_last (then_block);
8274 break;
8275 case TRY_FINALLY_EXPR:
8276 case TRY_CATCH_EXPR:
8277 inner_if = TREE_OPERAND (inner_if, 0);
8278 break;
8279 default:
8280 gcc_unreachable ();
8282 found:
8284 if (COND_EXPR_ELSE (inner_if))
8285 warning (OPT_Wparentheses,
8286 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
8287 &if_locus);
8290 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8291 SET_EXPR_LOCATION (stmt, if_locus);
8292 add_stmt (stmt);
8295 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8296 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8297 is false for DO loops. INCR is the FOR increment expression. BODY is
8298 the statement controlled by the loop. BLAB is the break label. CLAB is
8299 the continue label. Everything is allowed to be NULL. */
8301 void
8302 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8303 tree blab, tree clab, bool cond_is_first)
8305 tree entry = NULL, exit = NULL, t;
8307 /* If the condition is zero don't generate a loop construct. */
8308 if (cond && integer_zerop (cond))
8310 if (cond_is_first)
8312 t = build_and_jump (&blab);
8313 SET_EXPR_LOCATION (t, start_locus);
8314 add_stmt (t);
8317 else
8319 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8321 /* If we have an exit condition, then we build an IF with gotos either
8322 out of the loop, or to the top of it. If there's no exit condition,
8323 then we just build a jump back to the top. */
8324 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8326 if (cond && !integer_nonzerop (cond))
8328 /* Canonicalize the loop condition to the end. This means
8329 generating a branch to the loop condition. Reuse the
8330 continue label, if possible. */
8331 if (cond_is_first)
8333 if (incr || !clab)
8335 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8336 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8338 else
8339 t = build1 (GOTO_EXPR, void_type_node, clab);
8340 SET_EXPR_LOCATION (t, start_locus);
8341 add_stmt (t);
8344 t = build_and_jump (&blab);
8345 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
8346 if (cond_is_first)
8347 SET_EXPR_LOCATION (exit, start_locus);
8348 else
8349 SET_EXPR_LOCATION (exit, input_location);
8352 add_stmt (top);
8355 if (body)
8356 add_stmt (body);
8357 if (clab)
8358 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8359 if (incr)
8360 add_stmt (incr);
8361 if (entry)
8362 add_stmt (entry);
8363 if (exit)
8364 add_stmt (exit);
8365 if (blab)
8366 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8369 tree
8370 c_finish_bc_stmt (tree *label_p, bool is_break)
8372 bool skip;
8373 tree label = *label_p;
8375 /* In switch statements break is sometimes stylistically used after
8376 a return statement. This can lead to spurious warnings about
8377 control reaching the end of a non-void function when it is
8378 inlined. Note that we are calling block_may_fallthru with
8379 language specific tree nodes; this works because
8380 block_may_fallthru returns true when given something it does not
8381 understand. */
8382 skip = !block_may_fallthru (cur_stmt_list);
8384 if (!label)
8386 if (!skip)
8387 *label_p = label = create_artificial_label ();
8389 else if (TREE_CODE (label) == LABEL_DECL)
8391 else switch (TREE_INT_CST_LOW (label))
8393 case 0:
8394 if (is_break)
8395 error ("break statement not within loop or switch");
8396 else
8397 error ("continue statement not within a loop");
8398 return NULL_TREE;
8400 case 1:
8401 gcc_assert (is_break);
8402 error ("break statement used with OpenMP for loop");
8403 return NULL_TREE;
8405 default:
8406 gcc_unreachable ();
8409 if (skip)
8410 return NULL_TREE;
8412 if (!is_break)
8413 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8415 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8418 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8420 static void
8421 emit_side_effect_warnings (tree expr)
8423 if (expr == error_mark_node)
8425 else if (!TREE_SIDE_EFFECTS (expr))
8427 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8428 warning (OPT_Wunused_value, "%Hstatement with no effect",
8429 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
8431 else
8432 warn_if_unused_value (expr, input_location);
8435 /* Process an expression as if it were a complete statement. Emit
8436 diagnostics, but do not call ADD_STMT. */
8438 tree
8439 c_process_expr_stmt (tree expr)
8441 if (!expr)
8442 return NULL_TREE;
8444 expr = c_fully_fold (expr, false, NULL);
8446 if (warn_sequence_point)
8447 verify_sequence_points (expr);
8449 if (TREE_TYPE (expr) != error_mark_node
8450 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8451 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8452 error ("expression statement has incomplete type");
8454 /* If we're not processing a statement expression, warn about unused values.
8455 Warnings for statement expressions will be emitted later, once we figure
8456 out which is the result. */
8457 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8458 && warn_unused_value)
8459 emit_side_effect_warnings (expr);
8461 /* If the expression is not of a type to which we cannot assign a line
8462 number, wrap the thing in a no-op NOP_EXPR. */
8463 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8464 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8466 if (CAN_HAVE_LOCATION_P (expr))
8467 SET_EXPR_LOCATION (expr, input_location);
8469 return expr;
8472 /* Emit an expression as a statement. */
8474 tree
8475 c_finish_expr_stmt (tree expr)
8477 if (expr)
8478 return add_stmt (c_process_expr_stmt (expr));
8479 else
8480 return NULL;
8483 /* Do the opposite and emit a statement as an expression. To begin,
8484 create a new binding level and return it. */
8486 tree
8487 c_begin_stmt_expr (void)
8489 tree ret;
8490 struct c_label_context_se *nstack;
8491 struct c_label_list *glist;
8493 /* We must force a BLOCK for this level so that, if it is not expanded
8494 later, there is a way to turn off the entire subtree of blocks that
8495 are contained in it. */
8496 keep_next_level ();
8497 ret = c_begin_compound_stmt (true);
8498 if (c_switch_stack)
8500 c_switch_stack->blocked_stmt_expr++;
8501 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8503 for (glist = label_context_stack_se->labels_used;
8504 glist != NULL;
8505 glist = glist->next)
8507 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
8509 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
8510 nstack->labels_def = NULL;
8511 nstack->labels_used = NULL;
8512 nstack->next = label_context_stack_se;
8513 label_context_stack_se = nstack;
8515 /* Mark the current statement list as belonging to a statement list. */
8516 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8518 return ret;
8521 tree
8522 c_finish_stmt_expr (tree body)
8524 tree last, type, tmp, val;
8525 tree *last_p;
8526 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8528 body = c_end_compound_stmt (body, true);
8529 if (c_switch_stack)
8531 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
8532 c_switch_stack->blocked_stmt_expr--;
8534 /* It is no longer possible to jump to labels defined within this
8535 statement expression. */
8536 for (dlist = label_context_stack_se->labels_def;
8537 dlist != NULL;
8538 dlist = dlist->next)
8540 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
8542 /* It is again possible to define labels with a goto just outside
8543 this statement expression. */
8544 for (glist = label_context_stack_se->next->labels_used;
8545 glist != NULL;
8546 glist = glist->next)
8548 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
8549 glist_prev = glist;
8551 if (glist_prev != NULL)
8552 glist_prev->next = label_context_stack_se->labels_used;
8553 else
8554 label_context_stack_se->next->labels_used
8555 = label_context_stack_se->labels_used;
8556 label_context_stack_se = label_context_stack_se->next;
8558 /* Locate the last statement in BODY. See c_end_compound_stmt
8559 about always returning a BIND_EXPR. */
8560 last_p = &BIND_EXPR_BODY (body);
8561 last = BIND_EXPR_BODY (body);
8563 continue_searching:
8564 if (TREE_CODE (last) == STATEMENT_LIST)
8566 tree_stmt_iterator i;
8568 /* This can happen with degenerate cases like ({ }). No value. */
8569 if (!TREE_SIDE_EFFECTS (last))
8570 return body;
8572 /* If we're supposed to generate side effects warnings, process
8573 all of the statements except the last. */
8574 if (warn_unused_value)
8576 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8577 emit_side_effect_warnings (tsi_stmt (i));
8579 else
8580 i = tsi_last (last);
8581 last_p = tsi_stmt_ptr (i);
8582 last = *last_p;
8585 /* If the end of the list is exception related, then the list was split
8586 by a call to push_cleanup. Continue searching. */
8587 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8588 || TREE_CODE (last) == TRY_CATCH_EXPR)
8590 last_p = &TREE_OPERAND (last, 0);
8591 last = *last_p;
8592 goto continue_searching;
8595 /* In the case that the BIND_EXPR is not necessary, return the
8596 expression out from inside it. */
8597 if (last == error_mark_node
8598 || (last == BIND_EXPR_BODY (body)
8599 && BIND_EXPR_VARS (body) == NULL))
8601 /* Even if this looks constant, do not allow it in a constant
8602 expression. */
8603 last = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (last), NULL_TREE, last);
8604 C_MAYBE_CONST_EXPR_NON_CONST (last) = 1;
8605 /* Do not warn if the return value of a statement expression is
8606 unused. */
8607 TREE_NO_WARNING (last) = 1;
8608 return last;
8611 /* Extract the type of said expression. */
8612 type = TREE_TYPE (last);
8614 /* If we're not returning a value at all, then the BIND_EXPR that
8615 we already have is a fine expression to return. */
8616 if (!type || VOID_TYPE_P (type))
8617 return body;
8619 /* Now that we've located the expression containing the value, it seems
8620 silly to make voidify_wrapper_expr repeat the process. Create a
8621 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8622 tmp = create_tmp_var_raw (type, NULL);
8624 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8625 tree_expr_nonnegative_p giving up immediately. */
8626 val = last;
8627 if (TREE_CODE (val) == NOP_EXPR
8628 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8629 val = TREE_OPERAND (val, 0);
8631 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8632 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8634 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8637 /* Begin the scope of an identifier of variably modified type, scope
8638 number SCOPE. Jumping from outside this scope to inside it is not
8639 permitted. */
8641 void
8642 c_begin_vm_scope (unsigned int scope)
8644 struct c_label_context_vm *nstack;
8645 struct c_label_list *glist;
8647 gcc_assert (scope > 0);
8649 /* At file_scope, we don't have to do any processing. */
8650 if (label_context_stack_vm == NULL)
8651 return;
8653 if (c_switch_stack && !c_switch_stack->blocked_vm)
8654 c_switch_stack->blocked_vm = scope;
8655 for (glist = label_context_stack_vm->labels_used;
8656 glist != NULL;
8657 glist = glist->next)
8659 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8661 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8662 nstack->labels_def = NULL;
8663 nstack->labels_used = NULL;
8664 nstack->scope = scope;
8665 nstack->next = label_context_stack_vm;
8666 label_context_stack_vm = nstack;
8669 /* End a scope which may contain identifiers of variably modified
8670 type, scope number SCOPE. */
8672 void
8673 c_end_vm_scope (unsigned int scope)
8675 if (label_context_stack_vm == NULL)
8676 return;
8677 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8678 c_switch_stack->blocked_vm = 0;
8679 /* We may have a number of nested scopes of identifiers with
8680 variably modified type, all at this depth. Pop each in turn. */
8681 while (label_context_stack_vm->scope == scope)
8683 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8685 /* It is no longer possible to jump to labels defined within this
8686 scope. */
8687 for (dlist = label_context_stack_vm->labels_def;
8688 dlist != NULL;
8689 dlist = dlist->next)
8691 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8693 /* It is again possible to define labels with a goto just outside
8694 this scope. */
8695 for (glist = label_context_stack_vm->next->labels_used;
8696 glist != NULL;
8697 glist = glist->next)
8699 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8700 glist_prev = glist;
8702 if (glist_prev != NULL)
8703 glist_prev->next = label_context_stack_vm->labels_used;
8704 else
8705 label_context_stack_vm->next->labels_used
8706 = label_context_stack_vm->labels_used;
8707 label_context_stack_vm = label_context_stack_vm->next;
8711 /* Begin and end compound statements. This is as simple as pushing
8712 and popping new statement lists from the tree. */
8714 tree
8715 c_begin_compound_stmt (bool do_scope)
8717 tree stmt = push_stmt_list ();
8718 if (do_scope)
8719 push_scope ();
8720 return stmt;
8723 tree
8724 c_end_compound_stmt (tree stmt, bool do_scope)
8726 tree block = NULL;
8728 if (do_scope)
8730 if (c_dialect_objc ())
8731 objc_clear_super_receiver ();
8732 block = pop_scope ();
8735 stmt = pop_stmt_list (stmt);
8736 stmt = c_build_bind_expr (block, stmt);
8738 /* If this compound statement is nested immediately inside a statement
8739 expression, then force a BIND_EXPR to be created. Otherwise we'll
8740 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8741 STATEMENT_LISTs merge, and thus we can lose track of what statement
8742 was really last. */
8743 if (cur_stmt_list
8744 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8745 && TREE_CODE (stmt) != BIND_EXPR)
8747 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8748 TREE_SIDE_EFFECTS (stmt) = 1;
8751 return stmt;
8754 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8755 when the current scope is exited. EH_ONLY is true when this is not
8756 meant to apply to normal control flow transfer. */
8758 void
8759 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8761 enum tree_code code;
8762 tree stmt, list;
8763 bool stmt_expr;
8765 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8766 stmt = build_stmt (code, NULL, cleanup);
8767 add_stmt (stmt);
8768 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8769 list = push_stmt_list ();
8770 TREE_OPERAND (stmt, 0) = list;
8771 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8774 /* Build a binary-operation expression without default conversions.
8775 CODE is the kind of expression to build.
8776 LOCATION is the operator's location.
8777 This function differs from `build' in several ways:
8778 the data type of the result is computed and recorded in it,
8779 warnings are generated if arg data types are invalid,
8780 special handling for addition and subtraction of pointers is known,
8781 and some optimization is done (operations on narrow ints
8782 are done in the narrower type when that gives the same result).
8783 Constant folding is also done before the result is returned.
8785 Note that the operands will never have enumeral types, or function
8786 or array types, because either they will have the default conversions
8787 performed or they have both just been converted to some other type in which
8788 the arithmetic is to be done. */
8790 tree
8791 build_binary_op (location_t location, enum tree_code code,
8792 tree orig_op0, tree orig_op1, int convert_p)
8794 tree type0, type1, orig_type0, orig_type1;
8795 tree eptype;
8796 enum tree_code code0, code1;
8797 tree op0, op1;
8798 tree ret = error_mark_node;
8799 const char *invalid_op_diag;
8800 bool op0_int_operands, op1_int_operands;
8801 bool int_const, int_const_or_overflow, int_operands;
8803 /* Expression code to give to the expression when it is built.
8804 Normally this is CODE, which is what the caller asked for,
8805 but in some special cases we change it. */
8806 enum tree_code resultcode = code;
8808 /* Data type in which the computation is to be performed.
8809 In the simplest cases this is the common type of the arguments. */
8810 tree result_type = NULL;
8812 /* When the computation is in excess precision, the type of the
8813 final EXCESS_PRECISION_EXPR. */
8814 tree real_result_type = NULL;
8816 /* Nonzero means operands have already been type-converted
8817 in whatever way is necessary.
8818 Zero means they need to be converted to RESULT_TYPE. */
8819 int converted = 0;
8821 /* Nonzero means create the expression with this type, rather than
8822 RESULT_TYPE. */
8823 tree build_type = 0;
8825 /* Nonzero means after finally constructing the expression
8826 convert it to this type. */
8827 tree final_type = 0;
8829 /* Nonzero if this is an operation like MIN or MAX which can
8830 safely be computed in short if both args are promoted shorts.
8831 Also implies COMMON.
8832 -1 indicates a bitwise operation; this makes a difference
8833 in the exact conditions for when it is safe to do the operation
8834 in a narrower mode. */
8835 int shorten = 0;
8837 /* Nonzero if this is a comparison operation;
8838 if both args are promoted shorts, compare the original shorts.
8839 Also implies COMMON. */
8840 int short_compare = 0;
8842 /* Nonzero if this is a right-shift operation, which can be computed on the
8843 original short and then promoted if the operand is a promoted short. */
8844 int short_shift = 0;
8846 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8847 int common = 0;
8849 /* True means types are compatible as far as ObjC is concerned. */
8850 bool objc_ok;
8852 /* True means this is an arithmetic operation that may need excess
8853 precision. */
8854 bool may_need_excess_precision;
8856 if (location == UNKNOWN_LOCATION)
8857 location = input_location;
8859 op0 = orig_op0;
8860 op1 = orig_op1;
8862 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8863 if (op0_int_operands)
8864 op0 = remove_c_maybe_const_expr (op0);
8865 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8866 if (op1_int_operands)
8867 op1 = remove_c_maybe_const_expr (op1);
8868 int_operands = (op0_int_operands && op1_int_operands);
8869 if (int_operands)
8871 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8872 && TREE_CODE (orig_op1) == INTEGER_CST);
8873 int_const = (int_const_or_overflow
8874 && !TREE_OVERFLOW (orig_op0)
8875 && !TREE_OVERFLOW (orig_op1));
8877 else
8878 int_const = int_const_or_overflow = false;
8880 if (convert_p)
8882 op0 = default_conversion (op0);
8883 op1 = default_conversion (op1);
8886 orig_type0 = type0 = TREE_TYPE (op0);
8887 orig_type1 = type1 = TREE_TYPE (op1);
8889 /* The expression codes of the data types of the arguments tell us
8890 whether the arguments are integers, floating, pointers, etc. */
8891 code0 = TREE_CODE (type0);
8892 code1 = TREE_CODE (type1);
8894 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8895 STRIP_TYPE_NOPS (op0);
8896 STRIP_TYPE_NOPS (op1);
8898 /* If an error was already reported for one of the arguments,
8899 avoid reporting another error. */
8901 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8902 return error_mark_node;
8904 if ((invalid_op_diag
8905 = targetm.invalid_binary_op (code, type0, type1)))
8907 error_at (location, invalid_op_diag);
8908 return error_mark_node;
8911 switch (code)
8913 case PLUS_EXPR:
8914 case MINUS_EXPR:
8915 case MULT_EXPR:
8916 case TRUNC_DIV_EXPR:
8917 case CEIL_DIV_EXPR:
8918 case FLOOR_DIV_EXPR:
8919 case ROUND_DIV_EXPR:
8920 case EXACT_DIV_EXPR:
8921 may_need_excess_precision = true;
8922 break;
8923 default:
8924 may_need_excess_precision = false;
8925 break;
8927 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8929 op0 = TREE_OPERAND (op0, 0);
8930 type0 = TREE_TYPE (op0);
8932 else if (may_need_excess_precision
8933 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8935 type0 = eptype;
8936 op0 = convert (eptype, op0);
8938 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8940 op1 = TREE_OPERAND (op1, 0);
8941 type1 = TREE_TYPE (op1);
8943 else if (may_need_excess_precision
8944 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8946 type1 = eptype;
8947 op1 = convert (eptype, op1);
8950 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8952 switch (code)
8954 case PLUS_EXPR:
8955 /* Handle the pointer + int case. */
8956 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8958 ret = pointer_int_sum (PLUS_EXPR, op0, op1);
8959 goto return_build_binary_op;
8961 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8963 ret = pointer_int_sum (PLUS_EXPR, op1, op0);
8964 goto return_build_binary_op;
8966 else
8967 common = 1;
8968 break;
8970 case MINUS_EXPR:
8971 /* Subtraction of two similar pointers.
8972 We must subtract them as integers, then divide by object size. */
8973 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8974 && comp_target_types (type0, type1))
8976 ret = pointer_diff (op0, op1);
8977 goto return_build_binary_op;
8979 /* Handle pointer minus int. Just like pointer plus int. */
8980 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8982 ret = pointer_int_sum (MINUS_EXPR, op0, op1);
8983 goto return_build_binary_op;
8985 else
8986 common = 1;
8987 break;
8989 case MULT_EXPR:
8990 common = 1;
8991 break;
8993 case TRUNC_DIV_EXPR:
8994 case CEIL_DIV_EXPR:
8995 case FLOOR_DIV_EXPR:
8996 case ROUND_DIV_EXPR:
8997 case EXACT_DIV_EXPR:
8998 warn_for_div_by_zero (location, op1);
9000 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9001 || code0 == FIXED_POINT_TYPE
9002 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9003 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9004 || code1 == FIXED_POINT_TYPE
9005 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9007 enum tree_code tcode0 = code0, tcode1 = code1;
9009 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9010 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9011 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9012 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9014 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9015 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9016 resultcode = RDIV_EXPR;
9017 else
9018 /* Although it would be tempting to shorten always here, that
9019 loses on some targets, since the modulo instruction is
9020 undefined if the quotient can't be represented in the
9021 computation mode. We shorten only if unsigned or if
9022 dividing by something we know != -1. */
9023 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9024 || (TREE_CODE (op1) == INTEGER_CST
9025 && !integer_all_onesp (op1)));
9026 common = 1;
9028 break;
9030 case BIT_AND_EXPR:
9031 case BIT_IOR_EXPR:
9032 case BIT_XOR_EXPR:
9033 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9034 shorten = -1;
9035 /* Allow vector types which are not floating point types. */
9036 else if (code0 == VECTOR_TYPE
9037 && code1 == VECTOR_TYPE
9038 && !VECTOR_FLOAT_TYPE_P (type0)
9039 && !VECTOR_FLOAT_TYPE_P (type1))
9040 common = 1;
9041 break;
9043 case TRUNC_MOD_EXPR:
9044 case FLOOR_MOD_EXPR:
9045 warn_for_div_by_zero (location, op1);
9047 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9048 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9049 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9050 common = 1;
9051 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9053 /* Although it would be tempting to shorten always here, that loses
9054 on some targets, since the modulo instruction is undefined if the
9055 quotient can't be represented in the computation mode. We shorten
9056 only if unsigned or if dividing by something we know != -1. */
9057 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9058 || (TREE_CODE (op1) == INTEGER_CST
9059 && !integer_all_onesp (op1)));
9060 common = 1;
9062 break;
9064 case TRUTH_ANDIF_EXPR:
9065 case TRUTH_ORIF_EXPR:
9066 case TRUTH_AND_EXPR:
9067 case TRUTH_OR_EXPR:
9068 case TRUTH_XOR_EXPR:
9069 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9070 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9071 || code0 == FIXED_POINT_TYPE)
9072 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9073 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9074 || code1 == FIXED_POINT_TYPE))
9076 /* Result of these operations is always an int,
9077 but that does not mean the operands should be
9078 converted to ints! */
9079 result_type = integer_type_node;
9080 op0 = c_common_truthvalue_conversion (location, op0);
9081 op1 = c_common_truthvalue_conversion (location, op1);
9082 converted = 1;
9084 if (code == TRUTH_ANDIF_EXPR)
9086 int_const_or_overflow = (int_operands
9087 && TREE_CODE (orig_op0) == INTEGER_CST
9088 && (op0 == truthvalue_false_node
9089 || TREE_CODE (orig_op1) == INTEGER_CST));
9090 int_const = (int_const_or_overflow
9091 && !TREE_OVERFLOW (orig_op0)
9092 && (op0 == truthvalue_false_node
9093 || !TREE_OVERFLOW (orig_op1)));
9095 else if (code == TRUTH_ORIF_EXPR)
9097 int_const_or_overflow = (int_operands
9098 && TREE_CODE (orig_op0) == INTEGER_CST
9099 && (op0 == truthvalue_true_node
9100 || TREE_CODE (orig_op1) == INTEGER_CST));
9101 int_const = (int_const_or_overflow
9102 && !TREE_OVERFLOW (orig_op0)
9103 && (op0 == truthvalue_true_node
9104 || !TREE_OVERFLOW (orig_op1)));
9106 break;
9108 /* Shift operations: result has same type as first operand;
9109 always convert second operand to int.
9110 Also set SHORT_SHIFT if shifting rightward. */
9112 case RSHIFT_EXPR:
9113 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9114 && code1 == INTEGER_TYPE)
9116 if (TREE_CODE (op1) == INTEGER_CST)
9118 if (tree_int_cst_sgn (op1) < 0)
9120 int_const = false;
9121 if (skip_evaluation == 0)
9122 warning (0, "right shift count is negative");
9124 else
9126 if (!integer_zerop (op1))
9127 short_shift = 1;
9129 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9131 int_const = false;
9132 if (skip_evaluation == 0)
9133 warning (0, "right shift count >= width of type");
9138 /* Use the type of the value to be shifted. */
9139 result_type = type0;
9140 /* Convert the shift-count to an integer, regardless of size
9141 of value being shifted. */
9142 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9143 op1 = convert (integer_type_node, op1);
9144 /* Avoid converting op1 to result_type later. */
9145 converted = 1;
9147 break;
9149 case LSHIFT_EXPR:
9150 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9151 && code1 == INTEGER_TYPE)
9153 if (TREE_CODE (op1) == INTEGER_CST)
9155 if (tree_int_cst_sgn (op1) < 0)
9157 int_const = false;
9158 if (skip_evaluation == 0)
9159 warning (0, "left shift count is negative");
9162 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9164 int_const = false;
9165 if (skip_evaluation == 0)
9166 warning (0, "left shift count >= width of type");
9170 /* Use the type of the value to be shifted. */
9171 result_type = type0;
9172 /* Convert the shift-count to an integer, regardless of size
9173 of value being shifted. */
9174 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9175 op1 = convert (integer_type_node, op1);
9176 /* Avoid converting op1 to result_type later. */
9177 converted = 1;
9179 break;
9181 case EQ_EXPR:
9182 case NE_EXPR:
9183 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9184 warning_at (location,
9185 OPT_Wfloat_equal,
9186 "comparing floating point with == or != is unsafe");
9187 /* Result of comparison is always int,
9188 but don't convert the args to int! */
9189 build_type = integer_type_node;
9190 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9191 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9192 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9193 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9194 short_compare = 1;
9195 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9197 tree tt0 = TREE_TYPE (type0);
9198 tree tt1 = TREE_TYPE (type1);
9199 /* Anything compares with void *. void * compares with anything.
9200 Otherwise, the targets must be compatible
9201 and both must be object or both incomplete. */
9202 if (comp_target_types (type0, type1))
9203 result_type = common_pointer_type (type0, type1);
9204 else if (VOID_TYPE_P (tt0))
9206 /* op0 != orig_op0 detects the case of something
9207 whose value is 0 but which isn't a valid null ptr const. */
9208 if (pedantic && !null_pointer_constant_p (orig_op0)
9209 && TREE_CODE (tt1) == FUNCTION_TYPE)
9210 pedwarn (location, OPT_pedantic, "ISO C forbids "
9211 "comparison of %<void *%> with function pointer");
9213 else if (VOID_TYPE_P (tt1))
9215 if (pedantic && !null_pointer_constant_p (orig_op1)
9216 && TREE_CODE (tt0) == FUNCTION_TYPE)
9217 pedwarn (location, OPT_pedantic, "ISO C forbids "
9218 "comparison of %<void *%> with function pointer");
9220 else
9221 /* Avoid warning about the volatile ObjC EH puts on decls. */
9222 if (!objc_ok)
9223 pedwarn (location, 0,
9224 "comparison of distinct pointer types lacks a cast");
9226 if (result_type == NULL_TREE)
9227 result_type = ptr_type_node;
9229 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9231 if (TREE_CODE (op0) == ADDR_EXPR
9232 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9233 warning_at (location,
9234 OPT_Waddress, "the address of %qD will never be NULL",
9235 TREE_OPERAND (op0, 0));
9236 result_type = type0;
9238 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9240 if (TREE_CODE (op1) == ADDR_EXPR
9241 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9242 warning_at (location,
9243 OPT_Waddress, "the address of %qD will never be NULL",
9244 TREE_OPERAND (op1, 0));
9245 result_type = type1;
9247 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9249 result_type = type0;
9250 pedwarn (location, 0, "comparison between pointer and integer");
9252 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9254 result_type = type1;
9255 pedwarn (location, 0, "comparison between pointer and integer");
9257 break;
9259 case LE_EXPR:
9260 case GE_EXPR:
9261 case LT_EXPR:
9262 case GT_EXPR:
9263 build_type = integer_type_node;
9264 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9265 || code0 == FIXED_POINT_TYPE)
9266 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9267 || code1 == FIXED_POINT_TYPE))
9268 short_compare = 1;
9269 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9271 if (comp_target_types (type0, type1))
9273 result_type = common_pointer_type (type0, type1);
9274 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9275 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9276 pedwarn (location, 0,
9277 "comparison of complete and incomplete pointers");
9278 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9279 pedwarn (location, OPT_pedantic, "ISO C forbids "
9280 "ordered comparisons of pointers to functions");
9282 else
9284 result_type = ptr_type_node;
9285 pedwarn (location, 0,
9286 "comparison of distinct pointer types lacks a cast");
9289 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9291 result_type = type0;
9292 if (pedantic)
9293 pedwarn (location, OPT_pedantic,
9294 "ordered comparison of pointer with integer zero");
9295 else if (extra_warnings)
9296 warning_at (location, OPT_Wextra,
9297 "ordered comparison of pointer with integer zero");
9299 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9301 result_type = type1;
9302 pedwarn (location, OPT_pedantic,
9303 "ordered comparison of pointer with integer zero");
9305 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9307 result_type = type0;
9308 pedwarn (location, 0, "comparison between pointer and integer");
9310 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9312 result_type = type1;
9313 pedwarn (location, 0, "comparison between pointer and integer");
9315 break;
9317 default:
9318 gcc_unreachable ();
9321 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9322 return error_mark_node;
9324 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9325 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9326 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9327 TREE_TYPE (type1))))
9329 binary_op_error (location, code, type0, type1);
9330 return error_mark_node;
9333 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9334 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9336 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9337 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9339 bool first_complex = (code0 == COMPLEX_TYPE);
9340 bool second_complex = (code1 == COMPLEX_TYPE);
9341 int none_complex = (!first_complex && !second_complex);
9343 if (shorten || common || short_compare)
9345 result_type = c_common_type (type0, type1);
9346 if (result_type == error_mark_node)
9347 return error_mark_node;
9350 if (first_complex != second_complex
9351 && (code == PLUS_EXPR
9352 || code == MINUS_EXPR
9353 || code == MULT_EXPR
9354 || (code == TRUNC_DIV_EXPR && first_complex))
9355 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9356 && flag_signed_zeros)
9358 /* An operation on mixed real/complex operands must be
9359 handled specially, but the language-independent code can
9360 more easily optimize the plain complex arithmetic if
9361 -fno-signed-zeros. */
9362 tree real_type = TREE_TYPE (result_type);
9363 tree real, imag;
9364 if (type0 != orig_type0 || type1 != orig_type1)
9366 gcc_assert (may_need_excess_precision && common);
9367 real_result_type = c_common_type (orig_type0, orig_type1);
9369 if (first_complex)
9371 if (TREE_TYPE (op0) != result_type)
9372 op0 = convert_and_check (result_type, op0);
9373 if (TREE_TYPE (op1) != real_type)
9374 op1 = convert_and_check (real_type, op1);
9376 else
9378 if (TREE_TYPE (op0) != real_type)
9379 op0 = convert_and_check (real_type, op0);
9380 if (TREE_TYPE (op1) != result_type)
9381 op1 = convert_and_check (result_type, op1);
9383 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9384 return error_mark_node;
9385 if (first_complex)
9387 op0 = c_save_expr (op0);
9388 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9389 op0, 1);
9390 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9391 op0, 1);
9392 switch (code)
9394 case MULT_EXPR:
9395 case TRUNC_DIV_EXPR:
9396 imag = build2 (resultcode, real_type, imag, op1);
9397 /* Fall through. */
9398 case PLUS_EXPR:
9399 case MINUS_EXPR:
9400 real = build2 (resultcode, real_type, real, op1);
9401 break;
9402 default:
9403 gcc_unreachable();
9406 else
9408 op1 = c_save_expr (op1);
9409 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9410 op1, 1);
9411 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9412 op1, 1);
9413 switch (code)
9415 case MULT_EXPR:
9416 imag = build2 (resultcode, real_type, op0, imag);
9417 /* Fall through. */
9418 case PLUS_EXPR:
9419 real = build2 (resultcode, real_type, op0, real);
9420 break;
9421 case MINUS_EXPR:
9422 real = build2 (resultcode, real_type, op0, real);
9423 imag = build1 (NEGATE_EXPR, real_type, imag);
9424 break;
9425 default:
9426 gcc_unreachable();
9429 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9430 goto return_build_binary_op;
9433 /* For certain operations (which identify themselves by shorten != 0)
9434 if both args were extended from the same smaller type,
9435 do the arithmetic in that type and then extend.
9437 shorten !=0 and !=1 indicates a bitwise operation.
9438 For them, this optimization is safe only if
9439 both args are zero-extended or both are sign-extended.
9440 Otherwise, we might change the result.
9441 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9442 but calculated in (unsigned short) it would be (unsigned short)-1. */
9444 if (shorten && none_complex)
9446 final_type = result_type;
9447 result_type = shorten_binary_op (result_type, op0, op1,
9448 shorten == -1);
9451 /* Shifts can be shortened if shifting right. */
9453 if (short_shift)
9455 int unsigned_arg;
9456 tree arg0 = get_narrower (op0, &unsigned_arg);
9458 final_type = result_type;
9460 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9461 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9463 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9464 /* We can shorten only if the shift count is less than the
9465 number of bits in the smaller type size. */
9466 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9467 /* We cannot drop an unsigned shift after sign-extension. */
9468 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9470 /* Do an unsigned shift if the operand was zero-extended. */
9471 result_type
9472 = c_common_signed_or_unsigned_type (unsigned_arg,
9473 TREE_TYPE (arg0));
9474 /* Convert value-to-be-shifted to that type. */
9475 if (TREE_TYPE (op0) != result_type)
9476 op0 = convert (result_type, op0);
9477 converted = 1;
9481 /* Comparison operations are shortened too but differently.
9482 They identify themselves by setting short_compare = 1. */
9484 if (short_compare)
9486 /* Don't write &op0, etc., because that would prevent op0
9487 from being kept in a register.
9488 Instead, make copies of the our local variables and
9489 pass the copies by reference, then copy them back afterward. */
9490 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9491 enum tree_code xresultcode = resultcode;
9492 tree val
9493 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9495 if (val != 0)
9497 ret = val;
9498 goto return_build_binary_op;
9501 op0 = xop0, op1 = xop1;
9502 converted = 1;
9503 resultcode = xresultcode;
9505 if (!skip_evaluation)
9507 bool op0_maybe_const = true;
9508 bool op1_maybe_const = true;
9509 tree orig_op0_folded, orig_op1_folded;
9511 if (in_late_binary_op)
9513 orig_op0_folded = orig_op0;
9514 orig_op1_folded = orig_op1;
9516 else
9518 /* Fold for the sake of possible warnings, as in
9519 build_conditional_expr. This requires the
9520 "original" values to be folded, not just op0 and
9521 op1. */
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 orig_op0_folded = c_fully_fold (orig_op0,
9527 require_constant_value,
9528 NULL);
9529 orig_op1_folded = c_fully_fold (orig_op1,
9530 require_constant_value,
9531 NULL);
9534 if (warn_sign_compare)
9535 warn_for_sign_compare (location, orig_op0_folded,
9536 orig_op1_folded, op0, op1,
9537 result_type, resultcode);
9538 if (!in_late_binary_op)
9540 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9542 op0 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op0),
9543 NULL, op0);
9544 C_MAYBE_CONST_EXPR_NON_CONST (op0) = !op0_maybe_const;
9546 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9548 op1 = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (op1),
9549 NULL, op1);
9550 C_MAYBE_CONST_EXPR_NON_CONST (op1) = !op1_maybe_const;
9557 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9558 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9559 Then the expression will be built.
9560 It will be given type FINAL_TYPE if that is nonzero;
9561 otherwise, it will be given type RESULT_TYPE. */
9563 if (!result_type)
9565 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9566 return error_mark_node;
9569 if (!converted)
9571 if (TREE_TYPE (op0) != result_type)
9572 op0 = convert_and_check (result_type, op0);
9573 if (TREE_TYPE (op1) != result_type)
9574 op1 = convert_and_check (result_type, op1);
9576 /* This can happen if one operand has a vector type, and the other
9577 has a different type. */
9578 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9579 return error_mark_node;
9582 if (build_type == NULL_TREE)
9584 build_type = result_type;
9585 if (type0 != orig_type0 || type1 != orig_type1)
9587 gcc_assert (may_need_excess_precision && common);
9588 real_result_type = c_common_type (orig_type0, orig_type1);
9592 /* Treat expressions in initializers specially as they can't trap. */
9593 if (int_const_or_overflow)
9594 ret = (require_constant_value
9595 ? fold_build2_initializer (resultcode, build_type, op0, op1)
9596 : fold_build2 (resultcode, build_type, op0, op1));
9597 else
9598 ret = build2 (resultcode, build_type, op0, op1);
9599 if (final_type != 0)
9600 ret = convert (final_type, ret);
9602 return_build_binary_op:
9603 gcc_assert (ret != error_mark_node);
9604 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9605 ret = (int_operands
9606 ? note_integer_operands (ret)
9607 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9608 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9609 && !in_late_binary_op)
9610 ret = note_integer_operands (ret);
9611 if (real_result_type)
9612 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9613 protected_set_expr_location (ret, location);
9614 return ret;
9618 /* Convert EXPR to be a truth-value, validating its type for this
9619 purpose. LOCATION is the source location for the expression. */
9621 tree
9622 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9624 bool int_const, int_operands;
9626 switch (TREE_CODE (TREE_TYPE (expr)))
9628 case ARRAY_TYPE:
9629 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9630 return error_mark_node;
9632 case RECORD_TYPE:
9633 error_at (location, "used struct type value where scalar is required");
9634 return error_mark_node;
9636 case UNION_TYPE:
9637 error_at (location, "used union type value where scalar is required");
9638 return error_mark_node;
9640 case FUNCTION_TYPE:
9641 gcc_unreachable ();
9643 default:
9644 break;
9647 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9648 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9649 if (int_operands)
9650 expr = remove_c_maybe_const_expr (expr);
9652 /* ??? Should we also give an error for void and vectors rather than
9653 leaving those to give errors later? */
9654 expr = c_common_truthvalue_conversion (location, expr);
9656 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9658 if (TREE_OVERFLOW (expr))
9659 return expr;
9660 else
9661 return note_integer_operands (expr);
9663 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9664 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9665 return expr;
9669 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9670 required. */
9672 tree
9673 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9675 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9677 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9678 /* Executing a compound literal inside a function reinitializes
9679 it. */
9680 if (!TREE_STATIC (decl))
9681 *se = true;
9682 return decl;
9684 else
9685 return expr;
9688 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9690 tree
9691 c_begin_omp_parallel (void)
9693 tree block;
9695 keep_next_level ();
9696 block = c_begin_compound_stmt (true);
9698 return block;
9701 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
9703 tree
9704 c_finish_omp_parallel (tree clauses, tree block)
9706 tree stmt;
9708 block = c_end_compound_stmt (block, true);
9710 stmt = make_node (OMP_PARALLEL);
9711 TREE_TYPE (stmt) = void_type_node;
9712 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9713 OMP_PARALLEL_BODY (stmt) = block;
9715 return add_stmt (stmt);
9718 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9720 tree
9721 c_begin_omp_task (void)
9723 tree block;
9725 keep_next_level ();
9726 block = c_begin_compound_stmt (true);
9728 return block;
9731 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
9733 tree
9734 c_finish_omp_task (tree clauses, tree block)
9736 tree stmt;
9738 block = c_end_compound_stmt (block, true);
9740 stmt = make_node (OMP_TASK);
9741 TREE_TYPE (stmt) = void_type_node;
9742 OMP_TASK_CLAUSES (stmt) = clauses;
9743 OMP_TASK_BODY (stmt) = block;
9745 return add_stmt (stmt);
9748 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9749 Remove any elements from the list that are invalid. */
9751 tree
9752 c_finish_omp_clauses (tree clauses)
9754 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9755 tree c, t, *pc = &clauses;
9756 const char *name;
9758 bitmap_obstack_initialize (NULL);
9759 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9760 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9761 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9763 for (pc = &clauses, c = clauses; c ; c = *pc)
9765 bool remove = false;
9766 bool need_complete = false;
9767 bool need_implicitly_determined = false;
9769 switch (OMP_CLAUSE_CODE (c))
9771 case OMP_CLAUSE_SHARED:
9772 name = "shared";
9773 need_implicitly_determined = true;
9774 goto check_dup_generic;
9776 case OMP_CLAUSE_PRIVATE:
9777 name = "private";
9778 need_complete = true;
9779 need_implicitly_determined = true;
9780 goto check_dup_generic;
9782 case OMP_CLAUSE_REDUCTION:
9783 name = "reduction";
9784 need_implicitly_determined = true;
9785 t = OMP_CLAUSE_DECL (c);
9786 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9787 || POINTER_TYPE_P (TREE_TYPE (t)))
9789 error ("%qE has invalid type for %<reduction%>", t);
9790 remove = true;
9792 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9794 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9795 const char *r_name = NULL;
9797 switch (r_code)
9799 case PLUS_EXPR:
9800 case MULT_EXPR:
9801 case MINUS_EXPR:
9802 break;
9803 case BIT_AND_EXPR:
9804 r_name = "&";
9805 break;
9806 case BIT_XOR_EXPR:
9807 r_name = "^";
9808 break;
9809 case BIT_IOR_EXPR:
9810 r_name = "|";
9811 break;
9812 case TRUTH_ANDIF_EXPR:
9813 r_name = "&&";
9814 break;
9815 case TRUTH_ORIF_EXPR:
9816 r_name = "||";
9817 break;
9818 default:
9819 gcc_unreachable ();
9821 if (r_name)
9823 error ("%qE has invalid type for %<reduction(%s)%>",
9824 t, r_name);
9825 remove = true;
9828 goto check_dup_generic;
9830 case OMP_CLAUSE_COPYPRIVATE:
9831 name = "copyprivate";
9832 goto check_dup_generic;
9834 case OMP_CLAUSE_COPYIN:
9835 name = "copyin";
9836 t = OMP_CLAUSE_DECL (c);
9837 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9839 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9840 remove = true;
9842 goto check_dup_generic;
9844 check_dup_generic:
9845 t = OMP_CLAUSE_DECL (c);
9846 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9848 error ("%qE is not a variable in clause %qs", t, name);
9849 remove = true;
9851 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9852 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9853 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9855 error ("%qE appears more than once in data clauses", t);
9856 remove = true;
9858 else
9859 bitmap_set_bit (&generic_head, DECL_UID (t));
9860 break;
9862 case OMP_CLAUSE_FIRSTPRIVATE:
9863 name = "firstprivate";
9864 t = OMP_CLAUSE_DECL (c);
9865 need_complete = true;
9866 need_implicitly_determined = true;
9867 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9869 error ("%qE is not a variable in clause %<firstprivate%>", t);
9870 remove = true;
9872 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9873 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9875 error ("%qE appears more than once in data clauses", t);
9876 remove = true;
9878 else
9879 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9880 break;
9882 case OMP_CLAUSE_LASTPRIVATE:
9883 name = "lastprivate";
9884 t = OMP_CLAUSE_DECL (c);
9885 need_complete = true;
9886 need_implicitly_determined = true;
9887 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9889 error ("%qE is not a variable in clause %<lastprivate%>", t);
9890 remove = true;
9892 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9893 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9895 error ("%qE appears more than once in data clauses", t);
9896 remove = true;
9898 else
9899 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9900 break;
9902 case OMP_CLAUSE_IF:
9903 case OMP_CLAUSE_NUM_THREADS:
9904 case OMP_CLAUSE_SCHEDULE:
9905 case OMP_CLAUSE_NOWAIT:
9906 case OMP_CLAUSE_ORDERED:
9907 case OMP_CLAUSE_DEFAULT:
9908 case OMP_CLAUSE_UNTIED:
9909 case OMP_CLAUSE_COLLAPSE:
9910 pc = &OMP_CLAUSE_CHAIN (c);
9911 continue;
9913 default:
9914 gcc_unreachable ();
9917 if (!remove)
9919 t = OMP_CLAUSE_DECL (c);
9921 if (need_complete)
9923 t = require_complete_type (t);
9924 if (t == error_mark_node)
9925 remove = true;
9928 if (need_implicitly_determined)
9930 const char *share_name = NULL;
9932 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9933 share_name = "threadprivate";
9934 else switch (c_omp_predetermined_sharing (t))
9936 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9937 break;
9938 case OMP_CLAUSE_DEFAULT_SHARED:
9939 share_name = "shared";
9940 break;
9941 case OMP_CLAUSE_DEFAULT_PRIVATE:
9942 share_name = "private";
9943 break;
9944 default:
9945 gcc_unreachable ();
9947 if (share_name)
9949 error ("%qE is predetermined %qs for %qs",
9950 t, share_name, name);
9951 remove = true;
9956 if (remove)
9957 *pc = OMP_CLAUSE_CHAIN (c);
9958 else
9959 pc = &OMP_CLAUSE_CHAIN (c);
9962 bitmap_obstack_release (NULL);
9963 return clauses;
9966 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9967 down to the element type of an array. */
9969 tree
9970 c_build_qualified_type (tree type, int type_quals)
9972 if (type == error_mark_node)
9973 return type;
9975 if (TREE_CODE (type) == ARRAY_TYPE)
9977 tree t;
9978 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9979 type_quals);
9981 /* See if we already have an identically qualified type. */
9982 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9984 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9985 && TYPE_NAME (t) == TYPE_NAME (type)
9986 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9987 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9988 TYPE_ATTRIBUTES (type)))
9989 break;
9991 if (!t)
9993 tree domain = TYPE_DOMAIN (type);
9995 t = build_variant_type_copy (type);
9996 TREE_TYPE (t) = element_type;
9998 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
9999 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10000 SET_TYPE_STRUCTURAL_EQUALITY (t);
10001 else if (TYPE_CANONICAL (element_type) != element_type
10002 || (domain && TYPE_CANONICAL (domain) != domain))
10004 tree unqualified_canon
10005 = build_array_type (TYPE_CANONICAL (element_type),
10006 domain? TYPE_CANONICAL (domain)
10007 : NULL_TREE);
10008 TYPE_CANONICAL (t)
10009 = c_build_qualified_type (unqualified_canon, type_quals);
10011 else
10012 TYPE_CANONICAL (t) = t;
10014 return t;
10017 /* A restrict-qualified pointer type must be a pointer to object or
10018 incomplete type. Note that the use of POINTER_TYPE_P also allows
10019 REFERENCE_TYPEs, which is appropriate for C++. */
10020 if ((type_quals & TYPE_QUAL_RESTRICT)
10021 && (!POINTER_TYPE_P (type)
10022 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10024 error ("invalid use of %<restrict%>");
10025 type_quals &= ~TYPE_QUAL_RESTRICT;
10028 return build_qualified_type (type, type_quals);
10031 /* Build a VA_ARG_EXPR for the C parser. */
10033 tree
10034 c_build_va_arg (tree expr, tree type, location_t loc)
10036 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10037 warning_at (loc, OPT_Wc___compat,
10038 "C++ requires promoted type, not enum type, in %<va_arg%>");
10039 return build_va_arg (expr, type);