EnumSet*.class: Regenerate
[official-gcc.git] / gcc / c-typeck.c
blob4d4c4d1f4e56afdbe914e914ce8daf39ec66ba4c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree);
80 static int comp_target_types (tree, tree);
81 static int function_types_compatible_p (const_tree, const_tree);
82 static int type_lists_compatible_p (const_tree, const_tree);
83 static tree decl_constant_value_for_broken_optimization (tree);
84 static tree lookup_field (tree, tree);
85 static int convert_arguments (int, tree *, tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (const char *);
95 static tree digest_init (tree, tree, bool, int);
96 static void output_init_element (tree, bool, tree, tree, int);
97 static void output_pending_init_elements (int);
98 static int set_designator (int);
99 static void push_range_stack (tree);
100 static void add_pending_init (tree, tree);
101 static void set_nonincremental_init (void);
102 static void set_nonincremental_init_from_string (tree);
103 static tree find_init_member (tree);
104 static void readonly_error (tree, enum lvalue_use);
105 static int lvalue_or_else (const_tree, enum lvalue_use);
106 static int lvalue_p (const_tree);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (const_tree, const_tree);
110 /* Return true if EXP is a null pointer constant, false otherwise. */
112 static bool
113 null_pointer_constant_p (const_tree expr)
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
119 && !TREE_OVERFLOW (expr)
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
126 \f/* This is a cache to hold if two types are compatible or not. */
128 struct tagged_tu_seen_cache {
129 const struct tagged_tu_seen_cache * next;
130 const_tree t1;
131 const_tree t2;
132 /* The return value of tagged_types_tu_compatible_p if we had seen
133 these two types already. */
134 int val;
137 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
138 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
140 /* Do `exp = require_complete_type (exp);' to make sure exp
141 does not have an incomplete type. (That includes void types.) */
143 tree
144 require_complete_type (tree value)
146 tree type = TREE_TYPE (value);
148 if (value == error_mark_node || type == error_mark_node)
149 return error_mark_node;
151 /* First, detect a valid value with a complete type. */
152 if (COMPLETE_TYPE_P (type))
153 return value;
155 c_incomplete_type_error (value, type);
156 return error_mark_node;
159 /* Print an error message for invalid use of an incomplete type.
160 VALUE is the expression that was used (or 0 if that isn't known)
161 and TYPE is the type that was invalid. */
163 void
164 c_incomplete_type_error (const_tree value, const_tree type)
166 const char *type_code_string;
168 /* Avoid duplicate error message. */
169 if (TREE_CODE (type) == ERROR_MARK)
170 return;
172 if (value != 0 && (TREE_CODE (value) == VAR_DECL
173 || TREE_CODE (value) == PARM_DECL))
174 error ("%qD has an incomplete type", value);
175 else
177 retry:
178 /* We must print an error message. Be clever about what it says. */
180 switch (TREE_CODE (type))
182 case RECORD_TYPE:
183 type_code_string = "struct";
184 break;
186 case UNION_TYPE:
187 type_code_string = "union";
188 break;
190 case ENUMERAL_TYPE:
191 type_code_string = "enum";
192 break;
194 case VOID_TYPE:
195 error ("invalid use of void expression");
196 return;
198 case ARRAY_TYPE:
199 if (TYPE_DOMAIN (type))
201 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
203 error ("invalid use of flexible array member");
204 return;
206 type = TREE_TYPE (type);
207 goto retry;
209 error ("invalid use of array with unspecified bounds");
210 return;
212 default:
213 gcc_unreachable ();
216 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
217 error ("invalid use of undefined type %<%s %E%>",
218 type_code_string, TYPE_NAME (type));
219 else
220 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
221 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
225 /* Given a type, apply default promotions wrt unnamed function
226 arguments and return the new type. */
228 tree
229 c_type_promotes_to (tree type)
231 if (TYPE_MAIN_VARIANT (type) == float_type_node)
232 return double_type_node;
234 if (c_promoting_integer_type_p (type))
236 /* Preserve unsignedness if not really getting any wider. */
237 if (TYPE_UNSIGNED (type)
238 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
239 return unsigned_type_node;
240 return integer_type_node;
243 return type;
246 /* Return a variant of TYPE which has all the type qualifiers of LIKE
247 as well as those of TYPE. */
249 static tree
250 qualify_type (tree type, tree like)
252 return c_build_qualified_type (type,
253 TYPE_QUALS (type) | TYPE_QUALS (like));
256 /* Return true iff the given tree T is a variable length array. */
258 bool
259 c_vla_type_p (const_tree t)
261 if (TREE_CODE (t) == ARRAY_TYPE
262 && C_TYPE_VARIABLE_SIZE (t))
263 return true;
264 return false;
267 /* Return the composite type of two compatible types.
269 We assume that comptypes has already been done and returned
270 nonzero; if that isn't so, this may crash. In particular, we
271 assume that qualifiers match. */
273 tree
274 composite_type (tree t1, tree t2)
276 enum tree_code code1;
277 enum tree_code code2;
278 tree attributes;
280 /* Save time if the two types are the same. */
282 if (t1 == t2) return t1;
284 /* If one type is nonsense, use the other. */
285 if (t1 == error_mark_node)
286 return t2;
287 if (t2 == error_mark_node)
288 return t1;
290 code1 = TREE_CODE (t1);
291 code2 = TREE_CODE (t2);
293 /* Merge the attributes. */
294 attributes = targetm.merge_type_attributes (t1, t2);
296 /* If one is an enumerated type and the other is the compatible
297 integer type, the composite type might be either of the two
298 (DR#013 question 3). For consistency, use the enumerated type as
299 the composite type. */
301 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
302 return t1;
303 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
304 return t2;
306 gcc_assert (code1 == code2);
308 switch (code1)
310 case POINTER_TYPE:
311 /* For two pointers, do this recursively on the target type. */
313 tree pointed_to_1 = TREE_TYPE (t1);
314 tree pointed_to_2 = TREE_TYPE (t2);
315 tree target = composite_type (pointed_to_1, pointed_to_2);
316 t1 = build_pointer_type (target);
317 t1 = build_type_attribute_variant (t1, attributes);
318 return qualify_type (t1, t2);
321 case ARRAY_TYPE:
323 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
324 int quals;
325 tree unqual_elt;
326 tree d1 = TYPE_DOMAIN (t1);
327 tree d2 = TYPE_DOMAIN (t2);
328 bool d1_variable, d2_variable;
329 bool d1_zero, d2_zero;
331 /* We should not have any type quals on arrays at all. */
332 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
334 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
335 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
337 d1_variable = (!d1_zero
338 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
339 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
340 d2_variable = (!d2_zero
341 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
342 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
343 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
344 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
346 /* Save space: see if the result is identical to one of the args. */
347 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
348 && (d2_variable || d2_zero || !d1_variable))
349 return build_type_attribute_variant (t1, attributes);
350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
351 && (d1_variable || d1_zero || !d2_variable))
352 return build_type_attribute_variant (t2, attributes);
354 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
355 return build_type_attribute_variant (t1, attributes);
356 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Merge the element types, and have a size if either arg has
360 one. We may have qualifiers on the element types. To set
361 up TYPE_MAIN_VARIANT correctly, we need to form the
362 composite of the unqualified types and add the qualifiers
363 back at the end. */
364 quals = TYPE_QUALS (strip_array_types (elt));
365 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
366 t1 = build_array_type (unqual_elt,
367 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
368 && (d2_variable
369 || d2_zero
370 || !d1_variable))
371 ? t1
372 : t2));
373 t1 = c_build_qualified_type (t1, quals);
374 return build_type_attribute_variant (t1, attributes);
377 case ENUMERAL_TYPE:
378 case RECORD_TYPE:
379 case UNION_TYPE:
380 if (attributes != NULL)
382 /* Try harder not to create a new aggregate type. */
383 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
384 return t1;
385 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
386 return t2;
388 return build_type_attribute_variant (t1, attributes);
390 case FUNCTION_TYPE:
391 /* Function types: prefer the one that specified arg types.
392 If both do, merge the arg types. Also merge the return types. */
394 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
395 tree p1 = TYPE_ARG_TYPES (t1);
396 tree p2 = TYPE_ARG_TYPES (t2);
397 int len;
398 tree newargs, n;
399 int i;
401 /* Save space: see if the result is identical to one of the args. */
402 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
403 return build_type_attribute_variant (t1, attributes);
404 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
405 return build_type_attribute_variant (t2, attributes);
407 /* Simple way if one arg fails to specify argument types. */
408 if (TYPE_ARG_TYPES (t1) == 0)
410 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
414 if (TYPE_ARG_TYPES (t2) == 0)
416 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
417 t1 = build_type_attribute_variant (t1, attributes);
418 return qualify_type (t1, t2);
421 /* If both args specify argument types, we must merge the two
422 lists, argument by argument. */
423 /* Tell global_bindings_p to return false so that variable_size
424 doesn't die on VLAs in parameter types. */
425 c_override_global_bindings_to_false = true;
427 len = list_length (p1);
428 newargs = 0;
430 for (i = 0; i < len; i++)
431 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
433 n = newargs;
435 for (; p1;
436 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
438 /* A null type means arg type is not specified.
439 Take whatever the other function type has. */
440 if (TREE_VALUE (p1) == 0)
442 TREE_VALUE (n) = TREE_VALUE (p2);
443 goto parm_done;
445 if (TREE_VALUE (p2) == 0)
447 TREE_VALUE (n) = TREE_VALUE (p1);
448 goto parm_done;
451 /* Given wait (union {union wait *u; int *i} *)
452 and wait (union wait *),
453 prefer union wait * as type of parm. */
454 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
455 && TREE_VALUE (p1) != TREE_VALUE (p2))
457 tree memb;
458 tree mv2 = TREE_VALUE (p2);
459 if (mv2 && mv2 != error_mark_node
460 && TREE_CODE (mv2) != ARRAY_TYPE)
461 mv2 = TYPE_MAIN_VARIANT (mv2);
462 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
463 memb; memb = TREE_CHAIN (memb))
465 tree mv3 = TREE_TYPE (memb);
466 if (mv3 && mv3 != error_mark_node
467 && TREE_CODE (mv3) != ARRAY_TYPE)
468 mv3 = TYPE_MAIN_VARIANT (mv3);
469 if (comptypes (mv3, mv2))
471 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
472 TREE_VALUE (p2));
473 if (pedantic)
474 pedwarn ("function types not truly compatible in ISO C");
475 goto parm_done;
479 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
480 && TREE_VALUE (p2) != TREE_VALUE (p1))
482 tree memb;
483 tree mv1 = TREE_VALUE (p1);
484 if (mv1 && mv1 != error_mark_node
485 && TREE_CODE (mv1) != ARRAY_TYPE)
486 mv1 = TYPE_MAIN_VARIANT (mv1);
487 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
488 memb; memb = TREE_CHAIN (memb))
490 tree mv3 = TREE_TYPE (memb);
491 if (mv3 && mv3 != error_mark_node
492 && TREE_CODE (mv3) != ARRAY_TYPE)
493 mv3 = TYPE_MAIN_VARIANT (mv3);
494 if (comptypes (mv3, mv1))
496 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
497 TREE_VALUE (p1));
498 if (pedantic)
499 pedwarn ("function types not truly compatible in ISO C");
500 goto parm_done;
504 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
505 parm_done: ;
508 c_override_global_bindings_to_false = false;
509 t1 = build_function_type (valtype, newargs);
510 t1 = qualify_type (t1, t2);
511 /* ... falls through ... */
514 default:
515 return build_type_attribute_variant (t1, attributes);
520 /* Return the type of a conditional expression between pointers to
521 possibly differently qualified versions of compatible types.
523 We assume that comp_target_types has already been done and returned
524 nonzero; if that isn't so, this may crash. */
526 static tree
527 common_pointer_type (tree t1, tree t2)
529 tree attributes;
530 tree pointed_to_1, mv1;
531 tree pointed_to_2, mv2;
532 tree target;
534 /* Save time if the two types are the same. */
536 if (t1 == t2) return t1;
538 /* If one type is nonsense, use the other. */
539 if (t1 == error_mark_node)
540 return t2;
541 if (t2 == error_mark_node)
542 return t1;
544 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
545 && TREE_CODE (t2) == POINTER_TYPE);
547 /* Merge the attributes. */
548 attributes = targetm.merge_type_attributes (t1, t2);
550 /* Find the composite type of the target types, and combine the
551 qualifiers of the two types' targets. Do not lose qualifiers on
552 array element types by taking the TYPE_MAIN_VARIANT. */
553 mv1 = pointed_to_1 = TREE_TYPE (t1);
554 mv2 = pointed_to_2 = TREE_TYPE (t2);
555 if (TREE_CODE (mv1) != ARRAY_TYPE)
556 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
557 if (TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
559 target = composite_type (mv1, mv2);
560 t1 = build_pointer_type (c_build_qualified_type
561 (target,
562 TYPE_QUALS (pointed_to_1) |
563 TYPE_QUALS (pointed_to_2)));
564 return build_type_attribute_variant (t1, attributes);
567 /* Return the common type for two arithmetic types under the usual
568 arithmetic conversions. The default conversions have already been
569 applied, and enumerated types converted to their compatible integer
570 types. The resulting type is unqualified and has no attributes.
572 This is the type for the result of most arithmetic operations
573 if the operands have the given two types. */
575 static tree
576 c_common_type (tree t1, tree t2)
578 enum tree_code code1;
579 enum tree_code code2;
581 /* If one type is nonsense, use the other. */
582 if (t1 == error_mark_node)
583 return t2;
584 if (t2 == error_mark_node)
585 return t1;
587 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
588 t1 = TYPE_MAIN_VARIANT (t1);
590 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
591 t2 = TYPE_MAIN_VARIANT (t2);
593 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
594 t1 = build_type_attribute_variant (t1, NULL_TREE);
596 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
597 t2 = build_type_attribute_variant (t2, NULL_TREE);
599 /* Save time if the two types are the same. */
601 if (t1 == t2) return t1;
603 code1 = TREE_CODE (t1);
604 code2 = TREE_CODE (t2);
606 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
607 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
608 || code1 == INTEGER_TYPE);
609 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
610 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
611 || code2 == INTEGER_TYPE);
613 /* When one operand is a decimal float type, the other operand cannot be
614 a generic float type or a complex type. We also disallow vector types
615 here. */
616 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
617 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
619 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
621 error ("can%'t mix operands of decimal float and vector types");
622 return error_mark_node;
624 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
626 error ("can%'t mix operands of decimal float and complex types");
627 return error_mark_node;
629 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
631 error ("can%'t mix operands of decimal float and other float types");
632 return error_mark_node;
636 /* If one type is a vector type, return that type. (How the usual
637 arithmetic conversions apply to the vector types extension is not
638 precisely specified.) */
639 if (code1 == VECTOR_TYPE)
640 return t1;
642 if (code2 == VECTOR_TYPE)
643 return t2;
645 /* If one type is complex, form the common type of the non-complex
646 components, then make that complex. Use T1 or T2 if it is the
647 required type. */
648 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
650 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
651 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
652 tree subtype = c_common_type (subtype1, subtype2);
654 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
655 return t1;
656 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
657 return t2;
658 else
659 return build_complex_type (subtype);
662 /* If only one is real, use it as the result. */
664 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
665 return t1;
667 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
668 return t2;
670 /* If both are real and either are decimal floating point types, use
671 the decimal floating point type with the greater precision. */
673 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
675 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
676 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
677 return dfloat128_type_node;
678 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
679 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
680 return dfloat64_type_node;
681 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
682 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
683 return dfloat32_type_node;
686 /* Deal with fixed-point types. */
687 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
689 unsigned int unsignedp = 0, satp = 0;
690 enum machine_mode m1, m2;
691 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
693 m1 = TYPE_MODE (t1);
694 m2 = TYPE_MODE (t2);
696 /* If one input type is saturating, the result type is saturating. */
697 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
698 satp = 1;
700 /* If both fixed-point types are unsigned, the result type is unsigned.
701 When mixing fixed-point and integer types, follow the sign of the
702 fixed-point type.
703 Otherwise, the result type is signed. */
704 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
705 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
706 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
707 && TYPE_UNSIGNED (t1))
708 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
709 && TYPE_UNSIGNED (t2)))
710 unsignedp = 1;
712 /* The result type is signed. */
713 if (unsignedp == 0)
715 /* If the input type is unsigned, we need to convert to the
716 signed type. */
717 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
719 unsigned char mclass = 0;
720 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
721 mclass = MODE_FRACT;
722 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
723 mclass = MODE_ACCUM;
724 else
725 gcc_unreachable ();
726 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
728 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
730 unsigned char mclass = 0;
731 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
732 mclass = MODE_FRACT;
733 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
734 mclass = MODE_ACCUM;
735 else
736 gcc_unreachable ();
737 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
741 if (code1 == FIXED_POINT_TYPE)
743 fbit1 = GET_MODE_FBIT (m1);
744 ibit1 = GET_MODE_IBIT (m1);
746 else
748 fbit1 = 0;
749 /* Signed integers need to subtract one sign bit. */
750 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
753 if (code2 == FIXED_POINT_TYPE)
755 fbit2 = GET_MODE_FBIT (m2);
756 ibit2 = GET_MODE_IBIT (m2);
758 else
760 fbit2 = 0;
761 /* Signed integers need to subtract one sign bit. */
762 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
765 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
766 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
767 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
768 satp);
771 /* Both real or both integers; use the one with greater precision. */
773 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
774 return t1;
775 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
776 return t2;
778 /* Same precision. Prefer long longs to longs to ints when the
779 same precision, following the C99 rules on integer type rank
780 (which are equivalent to the C90 rules for C90 types). */
782 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
783 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
784 return long_long_unsigned_type_node;
786 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
787 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
789 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
790 return long_long_unsigned_type_node;
791 else
792 return long_long_integer_type_node;
795 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
796 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
797 return long_unsigned_type_node;
799 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
800 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
802 /* But preserve unsignedness from the other type,
803 since long cannot hold all the values of an unsigned int. */
804 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
805 return long_unsigned_type_node;
806 else
807 return long_integer_type_node;
810 /* Likewise, prefer long double to double even if same size. */
811 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
812 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
813 return long_double_type_node;
815 /* Otherwise prefer the unsigned one. */
817 if (TYPE_UNSIGNED (t1))
818 return t1;
819 else
820 return t2;
823 /* Wrapper around c_common_type that is used by c-common.c and other
824 front end optimizations that remove promotions. ENUMERAL_TYPEs
825 are allowed here and are converted to their compatible integer types.
826 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
827 preferably a non-Boolean type as the common type. */
828 tree
829 common_type (tree t1, tree t2)
831 if (TREE_CODE (t1) == ENUMERAL_TYPE)
832 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
833 if (TREE_CODE (t2) == ENUMERAL_TYPE)
834 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
836 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
837 if (TREE_CODE (t1) == BOOLEAN_TYPE
838 && TREE_CODE (t2) == BOOLEAN_TYPE)
839 return boolean_type_node;
841 /* If either type is BOOLEAN_TYPE, then return the other. */
842 if (TREE_CODE (t1) == BOOLEAN_TYPE)
843 return t2;
844 if (TREE_CODE (t2) == BOOLEAN_TYPE)
845 return t1;
847 return c_common_type (t1, t2);
850 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
851 or various other operations. Return 2 if they are compatible
852 but a warning may be needed if you use them together. */
855 comptypes (tree type1, tree type2)
857 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
858 int val;
860 val = comptypes_internal (type1, type2);
861 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
863 return val;
866 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
867 or various other operations. Return 2 if they are compatible
868 but a warning may be needed if you use them together. This
869 differs from comptypes, in that we don't free the seen types. */
871 static int
872 comptypes_internal (const_tree type1, const_tree type2)
874 const_tree t1 = type1;
875 const_tree t2 = type2;
876 int attrval, val;
878 /* Suppress errors caused by previously reported errors. */
880 if (t1 == t2 || !t1 || !t2
881 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
882 return 1;
884 /* If either type is the internal version of sizetype, return the
885 language version. */
886 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
887 && TYPE_ORIG_SIZE_TYPE (t1))
888 t1 = TYPE_ORIG_SIZE_TYPE (t1);
890 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
891 && TYPE_ORIG_SIZE_TYPE (t2))
892 t2 = TYPE_ORIG_SIZE_TYPE (t2);
895 /* Enumerated types are compatible with integer types, but this is
896 not transitive: two enumerated types in the same translation unit
897 are compatible with each other only if they are the same type. */
899 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
900 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
901 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
902 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
904 if (t1 == t2)
905 return 1;
907 /* Different classes of types can't be compatible. */
909 if (TREE_CODE (t1) != TREE_CODE (t2))
910 return 0;
912 /* Qualifiers must match. C99 6.7.3p9 */
914 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
915 return 0;
917 /* Allow for two different type nodes which have essentially the same
918 definition. Note that we already checked for equality of the type
919 qualifiers (just above). */
921 if (TREE_CODE (t1) != ARRAY_TYPE
922 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
923 return 1;
925 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
926 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
927 return 0;
929 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
930 val = 0;
932 switch (TREE_CODE (t1))
934 case POINTER_TYPE:
935 /* Do not remove mode or aliasing information. */
936 if (TYPE_MODE (t1) != TYPE_MODE (t2)
937 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
938 break;
939 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
940 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
941 break;
943 case FUNCTION_TYPE:
944 val = function_types_compatible_p (t1, t2);
945 break;
947 case ARRAY_TYPE:
949 tree d1 = TYPE_DOMAIN (t1);
950 tree d2 = TYPE_DOMAIN (t2);
951 bool d1_variable, d2_variable;
952 bool d1_zero, d2_zero;
953 val = 1;
955 /* Target types must match incl. qualifiers. */
956 if (TREE_TYPE (t1) != TREE_TYPE (t2)
957 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
958 return 0;
960 /* Sizes must match unless one is missing or variable. */
961 if (d1 == 0 || d2 == 0 || d1 == d2)
962 break;
964 d1_zero = !TYPE_MAX_VALUE (d1);
965 d2_zero = !TYPE_MAX_VALUE (d2);
967 d1_variable = (!d1_zero
968 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
969 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
970 d2_variable = (!d2_zero
971 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
972 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
973 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
974 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
976 if (d1_variable || d2_variable)
977 break;
978 if (d1_zero && d2_zero)
979 break;
980 if (d1_zero || d2_zero
981 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
982 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
983 val = 0;
985 break;
988 case ENUMERAL_TYPE:
989 case RECORD_TYPE:
990 case UNION_TYPE:
991 if (val != 1 && !same_translation_unit_p (t1, t2))
993 tree a1 = TYPE_ATTRIBUTES (t1);
994 tree a2 = TYPE_ATTRIBUTES (t2);
996 if (! attribute_list_contained (a1, a2)
997 && ! attribute_list_contained (a2, a1))
998 break;
1000 if (attrval != 2)
1001 return tagged_types_tu_compatible_p (t1, t2);
1002 val = tagged_types_tu_compatible_p (t1, t2);
1004 break;
1006 case VECTOR_TYPE:
1007 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1008 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1009 break;
1011 default:
1012 break;
1014 return attrval == 2 && val == 1 ? 2 : val;
1017 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1018 ignoring their qualifiers. */
1020 static int
1021 comp_target_types (tree ttl, tree ttr)
1023 int val;
1024 tree mvl, mvr;
1026 /* Do not lose qualifiers on element types of array types that are
1027 pointer targets by taking their TYPE_MAIN_VARIANT. */
1028 mvl = TREE_TYPE (ttl);
1029 mvr = TREE_TYPE (ttr);
1030 if (TREE_CODE (mvl) != ARRAY_TYPE)
1031 mvl = TYPE_MAIN_VARIANT (mvl);
1032 if (TREE_CODE (mvr) != ARRAY_TYPE)
1033 mvr = TYPE_MAIN_VARIANT (mvr);
1034 val = comptypes (mvl, mvr);
1036 if (val == 2 && pedantic)
1037 pedwarn ("types are not quite compatible");
1038 return val;
1041 /* Subroutines of `comptypes'. */
1043 /* Determine whether two trees derive from the same translation unit.
1044 If the CONTEXT chain ends in a null, that tree's context is still
1045 being parsed, so if two trees have context chains ending in null,
1046 they're in the same translation unit. */
1048 same_translation_unit_p (const_tree t1, const_tree t2)
1050 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1051 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1053 case tcc_declaration:
1054 t1 = DECL_CONTEXT (t1); break;
1055 case tcc_type:
1056 t1 = TYPE_CONTEXT (t1); break;
1057 case tcc_exceptional:
1058 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1059 default: gcc_unreachable ();
1062 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1063 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1065 case tcc_declaration:
1066 t2 = DECL_CONTEXT (t2); break;
1067 case tcc_type:
1068 t2 = TYPE_CONTEXT (t2); break;
1069 case tcc_exceptional:
1070 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1071 default: gcc_unreachable ();
1074 return t1 == t2;
1077 /* Allocate the seen two types, assuming that they are compatible. */
1079 static struct tagged_tu_seen_cache *
1080 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1082 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1083 tu->next = tagged_tu_seen_base;
1084 tu->t1 = t1;
1085 tu->t2 = t2;
1087 tagged_tu_seen_base = tu;
1089 /* The C standard says that two structures in different translation
1090 units are compatible with each other only if the types of their
1091 fields are compatible (among other things). We assume that they
1092 are compatible until proven otherwise when building the cache.
1093 An example where this can occur is:
1094 struct a
1096 struct a *next;
1098 If we are comparing this against a similar struct in another TU,
1099 and did not assume they were compatible, we end up with an infinite
1100 loop. */
1101 tu->val = 1;
1102 return tu;
1105 /* Free the seen types until we get to TU_TIL. */
1107 static void
1108 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1110 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1111 while (tu != tu_til)
1113 const struct tagged_tu_seen_cache *const tu1
1114 = (const struct tagged_tu_seen_cache *) tu;
1115 tu = tu1->next;
1116 free (CONST_CAST (tu1));
1118 tagged_tu_seen_base = tu_til;
1121 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1122 compatible. If the two types are not the same (which has been
1123 checked earlier), this can only happen when multiple translation
1124 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1125 rules. */
1127 static int
1128 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1130 tree s1, s2;
1131 bool needs_warning = false;
1133 /* We have to verify that the tags of the types are the same. This
1134 is harder than it looks because this may be a typedef, so we have
1135 to go look at the original type. It may even be a typedef of a
1136 typedef...
1137 In the case of compiler-created builtin structs the TYPE_DECL
1138 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1139 while (TYPE_NAME (t1)
1140 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1141 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1142 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1144 while (TYPE_NAME (t2)
1145 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1146 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1147 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1149 /* C90 didn't have the requirement that the two tags be the same. */
1150 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1151 return 0;
1153 /* C90 didn't say what happened if one or both of the types were
1154 incomplete; we choose to follow C99 rules here, which is that they
1155 are compatible. */
1156 if (TYPE_SIZE (t1) == NULL
1157 || TYPE_SIZE (t2) == NULL)
1158 return 1;
1161 const struct tagged_tu_seen_cache * tts_i;
1162 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1163 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1164 return tts_i->val;
1167 switch (TREE_CODE (t1))
1169 case ENUMERAL_TYPE:
1171 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1172 /* Speed up the case where the type values are in the same order. */
1173 tree tv1 = TYPE_VALUES (t1);
1174 tree tv2 = TYPE_VALUES (t2);
1176 if (tv1 == tv2)
1178 return 1;
1181 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1183 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1184 break;
1185 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1187 tu->val = 0;
1188 return 0;
1192 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1194 return 1;
1196 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1198 tu->val = 0;
1199 return 0;
1202 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1204 tu->val = 0;
1205 return 0;
1208 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1210 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1211 if (s2 == NULL
1212 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1214 tu->val = 0;
1215 return 0;
1218 return 1;
1221 case UNION_TYPE:
1223 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1224 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1226 tu->val = 0;
1227 return 0;
1230 /* Speed up the common case where the fields are in the same order. */
1231 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1232 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1234 int result;
1237 if (DECL_NAME (s1) == NULL
1238 || DECL_NAME (s1) != DECL_NAME (s2))
1239 break;
1240 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1241 if (result == 0)
1243 tu->val = 0;
1244 return 0;
1246 if (result == 2)
1247 needs_warning = true;
1249 if (TREE_CODE (s1) == FIELD_DECL
1250 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1251 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1253 tu->val = 0;
1254 return 0;
1257 if (!s1 && !s2)
1259 tu->val = needs_warning ? 2 : 1;
1260 return tu->val;
1263 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1265 bool ok = false;
1267 if (DECL_NAME (s1) != NULL)
1268 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1269 if (DECL_NAME (s1) == DECL_NAME (s2))
1271 int result;
1272 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1273 if (result == 0)
1275 tu->val = 0;
1276 return 0;
1278 if (result == 2)
1279 needs_warning = true;
1281 if (TREE_CODE (s1) == FIELD_DECL
1282 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1283 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1284 break;
1286 ok = true;
1287 break;
1289 if (!ok)
1291 tu->val = 0;
1292 return 0;
1295 tu->val = needs_warning ? 2 : 10;
1296 return tu->val;
1299 case RECORD_TYPE:
1301 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1303 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1304 s1 && s2;
1305 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1307 int result;
1308 if (TREE_CODE (s1) != TREE_CODE (s2)
1309 || DECL_NAME (s1) != DECL_NAME (s2))
1310 break;
1311 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1312 if (result == 0)
1313 break;
1314 if (result == 2)
1315 needs_warning = true;
1317 if (TREE_CODE (s1) == FIELD_DECL
1318 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1319 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1320 break;
1322 if (s1 && s2)
1323 tu->val = 0;
1324 else
1325 tu->val = needs_warning ? 2 : 1;
1326 return tu->val;
1329 default:
1330 gcc_unreachable ();
1334 /* Return 1 if two function types F1 and F2 are compatible.
1335 If either type specifies no argument types,
1336 the other must specify a fixed number of self-promoting arg types.
1337 Otherwise, if one type specifies only the number of arguments,
1338 the other must specify that number of self-promoting arg types.
1339 Otherwise, the argument types must match. */
1341 static int
1342 function_types_compatible_p (const_tree f1, const_tree f2)
1344 tree args1, args2;
1345 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1346 int val = 1;
1347 int val1;
1348 tree ret1, ret2;
1350 ret1 = TREE_TYPE (f1);
1351 ret2 = TREE_TYPE (f2);
1353 /* 'volatile' qualifiers on a function's return type used to mean
1354 the function is noreturn. */
1355 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1356 pedwarn ("function return types not compatible due to %<volatile%>");
1357 if (TYPE_VOLATILE (ret1))
1358 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1359 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1360 if (TYPE_VOLATILE (ret2))
1361 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1362 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1363 val = comptypes_internal (ret1, ret2);
1364 if (val == 0)
1365 return 0;
1367 args1 = TYPE_ARG_TYPES (f1);
1368 args2 = TYPE_ARG_TYPES (f2);
1370 /* An unspecified parmlist matches any specified parmlist
1371 whose argument types don't need default promotions. */
1373 if (args1 == 0)
1375 if (!self_promoting_args_p (args2))
1376 return 0;
1377 /* If one of these types comes from a non-prototype fn definition,
1378 compare that with the other type's arglist.
1379 If they don't match, ask for a warning (but no error). */
1380 if (TYPE_ACTUAL_ARG_TYPES (f1)
1381 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1382 val = 2;
1383 return val;
1385 if (args2 == 0)
1387 if (!self_promoting_args_p (args1))
1388 return 0;
1389 if (TYPE_ACTUAL_ARG_TYPES (f2)
1390 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1391 val = 2;
1392 return val;
1395 /* Both types have argument lists: compare them and propagate results. */
1396 val1 = type_lists_compatible_p (args1, args2);
1397 return val1 != 1 ? val1 : val;
1400 /* Check two lists of types for compatibility,
1401 returning 0 for incompatible, 1 for compatible,
1402 or 2 for compatible with warning. */
1404 static int
1405 type_lists_compatible_p (const_tree args1, const_tree args2)
1407 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1408 int val = 1;
1409 int newval = 0;
1411 while (1)
1413 tree a1, mv1, a2, mv2;
1414 if (args1 == 0 && args2 == 0)
1415 return val;
1416 /* If one list is shorter than the other,
1417 they fail to match. */
1418 if (args1 == 0 || args2 == 0)
1419 return 0;
1420 mv1 = a1 = TREE_VALUE (args1);
1421 mv2 = a2 = TREE_VALUE (args2);
1422 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1423 mv1 = TYPE_MAIN_VARIANT (mv1);
1424 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1425 mv2 = TYPE_MAIN_VARIANT (mv2);
1426 /* A null pointer instead of a type
1427 means there is supposed to be an argument
1428 but nothing is specified about what type it has.
1429 So match anything that self-promotes. */
1430 if (a1 == 0)
1432 if (c_type_promotes_to (a2) != a2)
1433 return 0;
1435 else if (a2 == 0)
1437 if (c_type_promotes_to (a1) != a1)
1438 return 0;
1440 /* If one of the lists has an error marker, ignore this arg. */
1441 else if (TREE_CODE (a1) == ERROR_MARK
1442 || TREE_CODE (a2) == ERROR_MARK)
1444 else if (!(newval = comptypes_internal (mv1, mv2)))
1446 /* Allow wait (union {union wait *u; int *i} *)
1447 and wait (union wait *) to be compatible. */
1448 if (TREE_CODE (a1) == UNION_TYPE
1449 && (TYPE_NAME (a1) == 0
1450 || TYPE_TRANSPARENT_UNION (a1))
1451 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1452 && tree_int_cst_equal (TYPE_SIZE (a1),
1453 TYPE_SIZE (a2)))
1455 tree memb;
1456 for (memb = TYPE_FIELDS (a1);
1457 memb; memb = TREE_CHAIN (memb))
1459 tree mv3 = TREE_TYPE (memb);
1460 if (mv3 && mv3 != error_mark_node
1461 && TREE_CODE (mv3) != ARRAY_TYPE)
1462 mv3 = TYPE_MAIN_VARIANT (mv3);
1463 if (comptypes_internal (mv3, mv2))
1464 break;
1466 if (memb == 0)
1467 return 0;
1469 else if (TREE_CODE (a2) == UNION_TYPE
1470 && (TYPE_NAME (a2) == 0
1471 || TYPE_TRANSPARENT_UNION (a2))
1472 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1473 && tree_int_cst_equal (TYPE_SIZE (a2),
1474 TYPE_SIZE (a1)))
1476 tree memb;
1477 for (memb = TYPE_FIELDS (a2);
1478 memb; memb = TREE_CHAIN (memb))
1480 tree mv3 = TREE_TYPE (memb);
1481 if (mv3 && mv3 != error_mark_node
1482 && TREE_CODE (mv3) != ARRAY_TYPE)
1483 mv3 = TYPE_MAIN_VARIANT (mv3);
1484 if (comptypes_internal (mv3, mv1))
1485 break;
1487 if (memb == 0)
1488 return 0;
1490 else
1491 return 0;
1494 /* comptypes said ok, but record if it said to warn. */
1495 if (newval > val)
1496 val = newval;
1498 args1 = TREE_CHAIN (args1);
1499 args2 = TREE_CHAIN (args2);
1503 /* Compute the size to increment a pointer by. */
1505 static tree
1506 c_size_in_bytes (const_tree type)
1508 enum tree_code code = TREE_CODE (type);
1510 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1511 return size_one_node;
1513 if (!COMPLETE_OR_VOID_TYPE_P (type))
1515 error ("arithmetic on pointer to an incomplete type");
1516 return size_one_node;
1519 /* Convert in case a char is more than one unit. */
1520 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1521 size_int (TYPE_PRECISION (char_type_node)
1522 / BITS_PER_UNIT));
1525 /* Return either DECL or its known constant value (if it has one). */
1527 tree
1528 decl_constant_value (tree decl)
1530 if (/* Don't change a variable array bound or initial value to a constant
1531 in a place where a variable is invalid. Note that DECL_INITIAL
1532 isn't valid for a PARM_DECL. */
1533 current_function_decl != 0
1534 && TREE_CODE (decl) != PARM_DECL
1535 && !TREE_THIS_VOLATILE (decl)
1536 && TREE_READONLY (decl)
1537 && DECL_INITIAL (decl) != 0
1538 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1539 /* This is invalid if initial value is not constant.
1540 If it has either a function call, a memory reference,
1541 or a variable, then re-evaluating it could give different results. */
1542 && TREE_CONSTANT (DECL_INITIAL (decl))
1543 /* Check for cases where this is sub-optimal, even though valid. */
1544 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1545 return DECL_INITIAL (decl);
1546 return decl;
1549 /* Return either DECL or its known constant value (if it has one), but
1550 return DECL if pedantic or DECL has mode BLKmode. This is for
1551 bug-compatibility with the old behavior of decl_constant_value
1552 (before GCC 3.0); every use of this function is a bug and it should
1553 be removed before GCC 3.1. It is not appropriate to use pedantic
1554 in a way that affects optimization, and BLKmode is probably not the
1555 right test for avoiding misoptimizations either. */
1557 static tree
1558 decl_constant_value_for_broken_optimization (tree decl)
1560 tree ret;
1562 if (pedantic || DECL_MODE (decl) == BLKmode)
1563 return decl;
1565 ret = decl_constant_value (decl);
1566 /* Avoid unwanted tree sharing between the initializer and current
1567 function's body where the tree can be modified e.g. by the
1568 gimplifier. */
1569 if (ret != decl && TREE_STATIC (decl))
1570 ret = unshare_expr (ret);
1571 return ret;
1574 /* Convert the array expression EXP to a pointer. */
1575 static tree
1576 array_to_pointer_conversion (tree exp)
1578 tree orig_exp = exp;
1579 tree type = TREE_TYPE (exp);
1580 tree adr;
1581 tree restype = TREE_TYPE (type);
1582 tree ptrtype;
1584 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1586 STRIP_TYPE_NOPS (exp);
1588 if (TREE_NO_WARNING (orig_exp))
1589 TREE_NO_WARNING (exp) = 1;
1591 ptrtype = build_pointer_type (restype);
1593 if (TREE_CODE (exp) == INDIRECT_REF)
1594 return convert (ptrtype, TREE_OPERAND (exp, 0));
1596 if (TREE_CODE (exp) == VAR_DECL)
1598 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1599 ADDR_EXPR because it's the best way of representing what
1600 happens in C when we take the address of an array and place
1601 it in a pointer to the element type. */
1602 adr = build1 (ADDR_EXPR, ptrtype, exp);
1603 if (!c_mark_addressable (exp))
1604 return error_mark_node;
1605 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1606 return adr;
1609 /* This way is better for a COMPONENT_REF since it can
1610 simplify the offset for a component. */
1611 adr = build_unary_op (ADDR_EXPR, exp, 1);
1612 return convert (ptrtype, adr);
1615 /* Convert the function expression EXP to a pointer. */
1616 static tree
1617 function_to_pointer_conversion (tree exp)
1619 tree orig_exp = exp;
1621 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1623 STRIP_TYPE_NOPS (exp);
1625 if (TREE_NO_WARNING (orig_exp))
1626 TREE_NO_WARNING (exp) = 1;
1628 return build_unary_op (ADDR_EXPR, exp, 0);
1631 /* Perform the default conversion of arrays and functions to pointers.
1632 Return the result of converting EXP. For any other expression, just
1633 return EXP after removing NOPs. */
1635 struct c_expr
1636 default_function_array_conversion (struct c_expr exp)
1638 tree orig_exp = exp.value;
1639 tree type = TREE_TYPE (exp.value);
1640 enum tree_code code = TREE_CODE (type);
1642 switch (code)
1644 case ARRAY_TYPE:
1646 bool not_lvalue = false;
1647 bool lvalue_array_p;
1649 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1650 || TREE_CODE (exp.value) == NOP_EXPR
1651 || TREE_CODE (exp.value) == CONVERT_EXPR)
1652 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1654 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1655 not_lvalue = true;
1656 exp.value = TREE_OPERAND (exp.value, 0);
1659 if (TREE_NO_WARNING (orig_exp))
1660 TREE_NO_WARNING (exp.value) = 1;
1662 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1663 if (!flag_isoc99 && !lvalue_array_p)
1665 /* Before C99, non-lvalue arrays do not decay to pointers.
1666 Normally, using such an array would be invalid; but it can
1667 be used correctly inside sizeof or as a statement expression.
1668 Thus, do not give an error here; an error will result later. */
1669 return exp;
1672 exp.value = array_to_pointer_conversion (exp.value);
1674 break;
1675 case FUNCTION_TYPE:
1676 exp.value = function_to_pointer_conversion (exp.value);
1677 break;
1678 default:
1679 STRIP_TYPE_NOPS (exp.value);
1680 if (TREE_NO_WARNING (orig_exp))
1681 TREE_NO_WARNING (exp.value) = 1;
1682 break;
1685 return exp;
1689 /* EXP is an expression of integer type. Apply the integer promotions
1690 to it and return the promoted value. */
1692 tree
1693 perform_integral_promotions (tree exp)
1695 tree type = TREE_TYPE (exp);
1696 enum tree_code code = TREE_CODE (type);
1698 gcc_assert (INTEGRAL_TYPE_P (type));
1700 /* Normally convert enums to int,
1701 but convert wide enums to something wider. */
1702 if (code == ENUMERAL_TYPE)
1704 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1705 TYPE_PRECISION (integer_type_node)),
1706 ((TYPE_PRECISION (type)
1707 >= TYPE_PRECISION (integer_type_node))
1708 && TYPE_UNSIGNED (type)));
1710 return convert (type, exp);
1713 /* ??? This should no longer be needed now bit-fields have their
1714 proper types. */
1715 if (TREE_CODE (exp) == COMPONENT_REF
1716 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1717 /* If it's thinner than an int, promote it like a
1718 c_promoting_integer_type_p, otherwise leave it alone. */
1719 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1720 TYPE_PRECISION (integer_type_node)))
1721 return convert (integer_type_node, exp);
1723 if (c_promoting_integer_type_p (type))
1725 /* Preserve unsignedness if not really getting any wider. */
1726 if (TYPE_UNSIGNED (type)
1727 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1728 return convert (unsigned_type_node, exp);
1730 return convert (integer_type_node, exp);
1733 return exp;
1737 /* Perform default promotions for C data used in expressions.
1738 Enumeral types or short or char are converted to int.
1739 In addition, manifest constants symbols are replaced by their values. */
1741 tree
1742 default_conversion (tree exp)
1744 tree orig_exp;
1745 tree type = TREE_TYPE (exp);
1746 enum tree_code code = TREE_CODE (type);
1748 /* Functions and arrays have been converted during parsing. */
1749 gcc_assert (code != FUNCTION_TYPE);
1750 if (code == ARRAY_TYPE)
1751 return exp;
1753 /* Constants can be used directly unless they're not loadable. */
1754 if (TREE_CODE (exp) == CONST_DECL)
1755 exp = DECL_INITIAL (exp);
1757 /* Replace a nonvolatile const static variable with its value unless
1758 it is an array, in which case we must be sure that taking the
1759 address of the array produces consistent results. */
1760 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1762 exp = decl_constant_value_for_broken_optimization (exp);
1763 type = TREE_TYPE (exp);
1766 /* Strip no-op conversions. */
1767 orig_exp = exp;
1768 STRIP_TYPE_NOPS (exp);
1770 if (TREE_NO_WARNING (orig_exp))
1771 TREE_NO_WARNING (exp) = 1;
1773 if (INTEGRAL_TYPE_P (type))
1774 return perform_integral_promotions (exp);
1776 if (code == VOID_TYPE)
1778 error ("void value not ignored as it ought to be");
1779 return error_mark_node;
1781 return exp;
1784 /* Look up COMPONENT in a structure or union DECL.
1786 If the component name is not found, returns NULL_TREE. Otherwise,
1787 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1788 stepping down the chain to the component, which is in the last
1789 TREE_VALUE of the list. Normally the list is of length one, but if
1790 the component is embedded within (nested) anonymous structures or
1791 unions, the list steps down the chain to the component. */
1793 static tree
1794 lookup_field (tree decl, tree component)
1796 tree type = TREE_TYPE (decl);
1797 tree field;
1799 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1800 to the field elements. Use a binary search on this array to quickly
1801 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1802 will always be set for structures which have many elements. */
1804 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1806 int bot, top, half;
1807 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1809 field = TYPE_FIELDS (type);
1810 bot = 0;
1811 top = TYPE_LANG_SPECIFIC (type)->s->len;
1812 while (top - bot > 1)
1814 half = (top - bot + 1) >> 1;
1815 field = field_array[bot+half];
1817 if (DECL_NAME (field) == NULL_TREE)
1819 /* Step through all anon unions in linear fashion. */
1820 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1822 field = field_array[bot++];
1823 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1824 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1826 tree anon = lookup_field (field, component);
1828 if (anon)
1829 return tree_cons (NULL_TREE, field, anon);
1833 /* Entire record is only anon unions. */
1834 if (bot > top)
1835 return NULL_TREE;
1837 /* Restart the binary search, with new lower bound. */
1838 continue;
1841 if (DECL_NAME (field) == component)
1842 break;
1843 if (DECL_NAME (field) < component)
1844 bot += half;
1845 else
1846 top = bot + half;
1849 if (DECL_NAME (field_array[bot]) == component)
1850 field = field_array[bot];
1851 else if (DECL_NAME (field) != component)
1852 return NULL_TREE;
1854 else
1856 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1858 if (DECL_NAME (field) == NULL_TREE
1859 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1860 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1862 tree anon = lookup_field (field, component);
1864 if (anon)
1865 return tree_cons (NULL_TREE, field, anon);
1868 if (DECL_NAME (field) == component)
1869 break;
1872 if (field == NULL_TREE)
1873 return NULL_TREE;
1876 return tree_cons (NULL_TREE, field, NULL_TREE);
1879 /* Make an expression to refer to the COMPONENT field of
1880 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1882 tree
1883 build_component_ref (tree datum, tree component)
1885 tree type = TREE_TYPE (datum);
1886 enum tree_code code = TREE_CODE (type);
1887 tree field = NULL;
1888 tree ref;
1890 if (!objc_is_public (datum, component))
1891 return error_mark_node;
1893 /* See if there is a field or component with name COMPONENT. */
1895 if (code == RECORD_TYPE || code == UNION_TYPE)
1897 if (!COMPLETE_TYPE_P (type))
1899 c_incomplete_type_error (NULL_TREE, type);
1900 return error_mark_node;
1903 field = lookup_field (datum, component);
1905 if (!field)
1907 error ("%qT has no member named %qE", type, component);
1908 return error_mark_node;
1911 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1912 This might be better solved in future the way the C++ front
1913 end does it - by giving the anonymous entities each a
1914 separate name and type, and then have build_component_ref
1915 recursively call itself. We can't do that here. */
1918 tree subdatum = TREE_VALUE (field);
1919 int quals;
1920 tree subtype;
1922 if (TREE_TYPE (subdatum) == error_mark_node)
1923 return error_mark_node;
1925 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1926 quals |= TYPE_QUALS (TREE_TYPE (datum));
1927 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1929 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1930 NULL_TREE);
1931 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1932 TREE_READONLY (ref) = 1;
1933 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1934 TREE_THIS_VOLATILE (ref) = 1;
1936 if (TREE_DEPRECATED (subdatum))
1937 warn_deprecated_use (subdatum);
1939 datum = ref;
1941 field = TREE_CHAIN (field);
1943 while (field);
1945 return ref;
1947 else if (code != ERROR_MARK)
1948 error ("request for member %qE in something not a structure or union",
1949 component);
1951 return error_mark_node;
1954 /* Given an expression PTR for a pointer, return an expression
1955 for the value pointed to.
1956 ERRORSTRING is the name of the operator to appear in error messages. */
1958 tree
1959 build_indirect_ref (tree ptr, const char *errorstring)
1961 tree pointer = default_conversion (ptr);
1962 tree type = TREE_TYPE (pointer);
1964 if (TREE_CODE (type) == POINTER_TYPE)
1966 if (TREE_CODE (pointer) == CONVERT_EXPR
1967 || TREE_CODE (pointer) == NOP_EXPR
1968 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
1970 /* If a warning is issued, mark it to avoid duplicates from
1971 the backend. This only needs to be done at
1972 warn_strict_aliasing > 2. */
1973 if (warn_strict_aliasing > 2)
1974 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
1975 type, TREE_OPERAND (pointer, 0)))
1976 TREE_NO_WARNING (pointer) = 1;
1979 if (TREE_CODE (pointer) == ADDR_EXPR
1980 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1981 == TREE_TYPE (type)))
1982 return TREE_OPERAND (pointer, 0);
1983 else
1985 tree t = TREE_TYPE (type);
1986 tree ref;
1988 ref = build1 (INDIRECT_REF, t, pointer);
1990 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1992 error ("dereferencing pointer to incomplete type");
1993 return error_mark_node;
1995 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1996 warning (0, "dereferencing %<void *%> pointer");
1998 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1999 so that we get the proper error message if the result is used
2000 to assign to. Also, &* is supposed to be a no-op.
2001 And ANSI C seems to specify that the type of the result
2002 should be the const type. */
2003 /* A de-reference of a pointer to const is not a const. It is valid
2004 to change it via some other pointer. */
2005 TREE_READONLY (ref) = TYPE_READONLY (t);
2006 TREE_SIDE_EFFECTS (ref)
2007 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2008 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2009 return ref;
2012 else if (TREE_CODE (pointer) != ERROR_MARK)
2013 error ("invalid type argument of %qs (have %qT)", errorstring, type);
2014 return error_mark_node;
2017 /* This handles expressions of the form "a[i]", which denotes
2018 an array reference.
2020 This is logically equivalent in C to *(a+i), but we may do it differently.
2021 If A is a variable or a member, we generate a primitive ARRAY_REF.
2022 This avoids forcing the array out of registers, and can work on
2023 arrays that are not lvalues (for example, members of structures returned
2024 by functions). */
2026 tree
2027 build_array_ref (tree array, tree index)
2029 bool swapped = false;
2030 if (TREE_TYPE (array) == error_mark_node
2031 || TREE_TYPE (index) == error_mark_node)
2032 return error_mark_node;
2034 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2035 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2037 tree temp;
2038 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2039 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2041 error ("subscripted value is neither array nor pointer");
2042 return error_mark_node;
2044 temp = array;
2045 array = index;
2046 index = temp;
2047 swapped = true;
2050 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2052 error ("array subscript is not an integer");
2053 return error_mark_node;
2056 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2058 error ("subscripted value is pointer to function");
2059 return error_mark_node;
2062 /* ??? Existing practice has been to warn only when the char
2063 index is syntactically the index, not for char[array]. */
2064 if (!swapped)
2065 warn_array_subscript_with_type_char (index);
2067 /* Apply default promotions *after* noticing character types. */
2068 index = default_conversion (index);
2070 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2072 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2074 tree rval, type;
2076 /* An array that is indexed by a non-constant
2077 cannot be stored in a register; we must be able to do
2078 address arithmetic on its address.
2079 Likewise an array of elements of variable size. */
2080 if (TREE_CODE (index) != INTEGER_CST
2081 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2082 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2084 if (!c_mark_addressable (array))
2085 return error_mark_node;
2087 /* An array that is indexed by a constant value which is not within
2088 the array bounds cannot be stored in a register either; because we
2089 would get a crash in store_bit_field/extract_bit_field when trying
2090 to access a non-existent part of the register. */
2091 if (TREE_CODE (index) == INTEGER_CST
2092 && TYPE_DOMAIN (TREE_TYPE (array))
2093 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2095 if (!c_mark_addressable (array))
2096 return error_mark_node;
2099 if (pedantic)
2101 tree foo = array;
2102 while (TREE_CODE (foo) == COMPONENT_REF)
2103 foo = TREE_OPERAND (foo, 0);
2104 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2105 pedwarn ("ISO C forbids subscripting %<register%> array");
2106 else if (!flag_isoc99 && !lvalue_p (foo))
2107 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
2110 type = TREE_TYPE (TREE_TYPE (array));
2111 if (TREE_CODE (type) != ARRAY_TYPE)
2112 type = TYPE_MAIN_VARIANT (type);
2113 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2114 /* Array ref is const/volatile if the array elements are
2115 or if the array is. */
2116 TREE_READONLY (rval)
2117 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2118 | TREE_READONLY (array));
2119 TREE_SIDE_EFFECTS (rval)
2120 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2121 | TREE_SIDE_EFFECTS (array));
2122 TREE_THIS_VOLATILE (rval)
2123 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2124 /* This was added by rms on 16 Nov 91.
2125 It fixes vol struct foo *a; a->elts[1]
2126 in an inline function.
2127 Hope it doesn't break something else. */
2128 | TREE_THIS_VOLATILE (array));
2129 return require_complete_type (fold (rval));
2131 else
2133 tree ar = default_conversion (array);
2135 if (ar == error_mark_node)
2136 return ar;
2138 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2139 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2141 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2142 "array indexing");
2146 /* Build an external reference to identifier ID. FUN indicates
2147 whether this will be used for a function call. LOC is the source
2148 location of the identifier. */
2149 tree
2150 build_external_ref (tree id, int fun, location_t loc)
2152 tree ref;
2153 tree decl = lookup_name (id);
2155 /* In Objective-C, an instance variable (ivar) may be preferred to
2156 whatever lookup_name() found. */
2157 decl = objc_lookup_ivar (decl, id);
2159 if (decl && decl != error_mark_node)
2160 ref = decl;
2161 else if (fun)
2162 /* Implicit function declaration. */
2163 ref = implicitly_declare (id);
2164 else if (decl == error_mark_node)
2165 /* Don't complain about something that's already been
2166 complained about. */
2167 return error_mark_node;
2168 else
2170 undeclared_variable (id, loc);
2171 return error_mark_node;
2174 if (TREE_TYPE (ref) == error_mark_node)
2175 return error_mark_node;
2177 if (TREE_DEPRECATED (ref))
2178 warn_deprecated_use (ref);
2180 /* Recursive call does not count as usage. */
2181 if (ref != current_function_decl)
2183 if (!skip_evaluation)
2184 assemble_external (ref);
2185 TREE_USED (ref) = 1;
2188 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2190 if (!in_sizeof && !in_typeof)
2191 C_DECL_USED (ref) = 1;
2192 else if (DECL_INITIAL (ref) == 0
2193 && DECL_EXTERNAL (ref)
2194 && !TREE_PUBLIC (ref))
2195 record_maybe_used_decl (ref);
2198 if (TREE_CODE (ref) == CONST_DECL)
2200 used_types_insert (TREE_TYPE (ref));
2201 ref = DECL_INITIAL (ref);
2202 TREE_CONSTANT (ref) = 1;
2203 TREE_INVARIANT (ref) = 1;
2205 else if (current_function_decl != 0
2206 && !DECL_FILE_SCOPE_P (current_function_decl)
2207 && (TREE_CODE (ref) == VAR_DECL
2208 || TREE_CODE (ref) == PARM_DECL
2209 || TREE_CODE (ref) == FUNCTION_DECL))
2211 tree context = decl_function_context (ref);
2213 if (context != 0 && context != current_function_decl)
2214 DECL_NONLOCAL (ref) = 1;
2216 /* C99 6.7.4p3: An inline definition of a function with external
2217 linkage ... shall not contain a reference to an identifier with
2218 internal linkage. */
2219 else if (current_function_decl != 0
2220 && DECL_DECLARED_INLINE_P (current_function_decl)
2221 && DECL_EXTERNAL (current_function_decl)
2222 && VAR_OR_FUNCTION_DECL_P (ref)
2223 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2224 && ! TREE_PUBLIC (ref))
2225 pedwarn ("%H%qD is static but used in inline function %qD "
2226 "which is not static", &loc, ref, current_function_decl);
2228 return ref;
2231 /* Record details of decls possibly used inside sizeof or typeof. */
2232 struct maybe_used_decl
2234 /* The decl. */
2235 tree decl;
2236 /* The level seen at (in_sizeof + in_typeof). */
2237 int level;
2238 /* The next one at this level or above, or NULL. */
2239 struct maybe_used_decl *next;
2242 static struct maybe_used_decl *maybe_used_decls;
2244 /* Record that DECL, an undefined static function reference seen
2245 inside sizeof or typeof, might be used if the operand of sizeof is
2246 a VLA type or the operand of typeof is a variably modified
2247 type. */
2249 static void
2250 record_maybe_used_decl (tree decl)
2252 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2253 t->decl = decl;
2254 t->level = in_sizeof + in_typeof;
2255 t->next = maybe_used_decls;
2256 maybe_used_decls = t;
2259 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2260 USED is false, just discard them. If it is true, mark them used
2261 (if no longer inside sizeof or typeof) or move them to the next
2262 level up (if still inside sizeof or typeof). */
2264 void
2265 pop_maybe_used (bool used)
2267 struct maybe_used_decl *p = maybe_used_decls;
2268 int cur_level = in_sizeof + in_typeof;
2269 while (p && p->level > cur_level)
2271 if (used)
2273 if (cur_level == 0)
2274 C_DECL_USED (p->decl) = 1;
2275 else
2276 p->level = cur_level;
2278 p = p->next;
2280 if (!used || cur_level == 0)
2281 maybe_used_decls = p;
2284 /* Return the result of sizeof applied to EXPR. */
2286 struct c_expr
2287 c_expr_sizeof_expr (struct c_expr expr)
2289 struct c_expr ret;
2290 if (expr.value == error_mark_node)
2292 ret.value = error_mark_node;
2293 ret.original_code = ERROR_MARK;
2294 pop_maybe_used (false);
2296 else
2298 ret.value = c_sizeof (TREE_TYPE (expr.value));
2299 ret.original_code = ERROR_MARK;
2300 if (c_vla_type_p (TREE_TYPE (expr.value)))
2302 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2303 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2305 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2307 return ret;
2310 /* Return the result of sizeof applied to T, a structure for the type
2311 name passed to sizeof (rather than the type itself). */
2313 struct c_expr
2314 c_expr_sizeof_type (struct c_type_name *t)
2316 tree type;
2317 struct c_expr ret;
2318 type = groktypename (t);
2319 ret.value = c_sizeof (type);
2320 ret.original_code = ERROR_MARK;
2321 pop_maybe_used (type != error_mark_node
2322 ? C_TYPE_VARIABLE_SIZE (type) : false);
2323 return ret;
2326 /* Build a function call to function FUNCTION with parameters PARAMS.
2327 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2328 TREE_VALUE of each node is a parameter-expression.
2329 FUNCTION's data type may be a function type or a pointer-to-function. */
2331 tree
2332 build_function_call (tree function, tree params)
2334 tree fntype, fundecl = 0;
2335 tree name = NULL_TREE, result;
2336 tree tem;
2337 int nargs;
2338 tree *argarray;
2341 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2342 STRIP_TYPE_NOPS (function);
2344 /* Convert anything with function type to a pointer-to-function. */
2345 if (TREE_CODE (function) == FUNCTION_DECL)
2347 /* Implement type-directed function overloading for builtins.
2348 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2349 handle all the type checking. The result is a complete expression
2350 that implements this function call. */
2351 tem = resolve_overloaded_builtin (function, params);
2352 if (tem)
2353 return tem;
2355 name = DECL_NAME (function);
2356 fundecl = function;
2358 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2359 function = function_to_pointer_conversion (function);
2361 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2362 expressions, like those used for ObjC messenger dispatches. */
2363 function = objc_rewrite_function_call (function, params);
2365 fntype = TREE_TYPE (function);
2367 if (TREE_CODE (fntype) == ERROR_MARK)
2368 return error_mark_node;
2370 if (!(TREE_CODE (fntype) == POINTER_TYPE
2371 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2373 error ("called object %qE is not a function", function);
2374 return error_mark_node;
2377 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2378 current_function_returns_abnormally = 1;
2380 /* fntype now gets the type of function pointed to. */
2381 fntype = TREE_TYPE (fntype);
2383 /* Check that the function is called through a compatible prototype.
2384 If it is not, replace the call by a trap, wrapped up in a compound
2385 expression if necessary. This has the nice side-effect to prevent
2386 the tree-inliner from generating invalid assignment trees which may
2387 blow up in the RTL expander later. */
2388 if ((TREE_CODE (function) == NOP_EXPR
2389 || TREE_CODE (function) == CONVERT_EXPR)
2390 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2391 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2392 && !comptypes (fntype, TREE_TYPE (tem)))
2394 tree return_type = TREE_TYPE (fntype);
2395 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2396 NULL_TREE);
2398 /* This situation leads to run-time undefined behavior. We can't,
2399 therefore, simply error unless we can prove that all possible
2400 executions of the program must execute the code. */
2401 warning (0, "function called through a non-compatible type");
2403 /* We can, however, treat "undefined" any way we please.
2404 Call abort to encourage the user to fix the program. */
2405 inform ("if this code is reached, the program will abort");
2407 if (VOID_TYPE_P (return_type))
2408 return trap;
2409 else
2411 tree rhs;
2413 if (AGGREGATE_TYPE_P (return_type))
2414 rhs = build_compound_literal (return_type,
2415 build_constructor (return_type, 0));
2416 else
2417 rhs = fold_convert (return_type, integer_zero_node);
2419 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2423 /* Convert the parameters to the types declared in the
2424 function prototype, or apply default promotions. */
2426 nargs = list_length (params);
2427 argarray = (tree *) alloca (nargs * sizeof (tree));
2428 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2429 params, function, fundecl);
2430 if (nargs < 0)
2431 return error_mark_node;
2433 /* Check that the arguments to the function are valid. */
2435 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2436 TYPE_ARG_TYPES (fntype));
2438 if (require_constant_value)
2440 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2441 function, nargs, argarray);
2442 if (TREE_CONSTANT (result)
2443 && (name == NULL_TREE
2444 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2445 pedwarn_init ("initializer element is not constant");
2447 else
2448 result = fold_build_call_array (TREE_TYPE (fntype),
2449 function, nargs, argarray);
2451 if (VOID_TYPE_P (TREE_TYPE (result)))
2452 return result;
2453 return require_complete_type (result);
2456 /* Convert the argument expressions in the list VALUES
2457 to the types in the list TYPELIST. The resulting arguments are
2458 stored in the array ARGARRAY which has size NARGS.
2460 If TYPELIST is exhausted, or when an element has NULL as its type,
2461 perform the default conversions.
2463 PARMLIST is the chain of parm decls for the function being called.
2464 It may be 0, if that info is not available.
2465 It is used only for generating error messages.
2467 FUNCTION is a tree for the called function. It is used only for
2468 error messages, where it is formatted with %qE.
2470 This is also where warnings about wrong number of args are generated.
2472 VALUES is a chain of TREE_LIST nodes with the elements of the list
2473 in the TREE_VALUE slots of those nodes.
2475 Returns the actual number of arguments processed (which may be less
2476 than NARGS in some error situations), or -1 on failure. */
2478 static int
2479 convert_arguments (int nargs, tree *argarray,
2480 tree typelist, tree values, tree function, tree fundecl)
2482 tree typetail, valtail;
2483 int parmnum;
2484 const bool type_generic = fundecl
2485 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2486 tree selector;
2488 /* Change pointer to function to the function itself for
2489 diagnostics. */
2490 if (TREE_CODE (function) == ADDR_EXPR
2491 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2492 function = TREE_OPERAND (function, 0);
2494 /* Handle an ObjC selector specially for diagnostics. */
2495 selector = objc_message_selector ();
2497 /* Scan the given expressions and types, producing individual
2498 converted arguments and storing them in ARGARRAY. */
2500 for (valtail = values, typetail = typelist, parmnum = 0;
2501 valtail;
2502 valtail = TREE_CHAIN (valtail), parmnum++)
2504 tree type = typetail ? TREE_VALUE (typetail) : 0;
2505 tree val = TREE_VALUE (valtail);
2506 tree rname = function;
2507 int argnum = parmnum + 1;
2508 const char *invalid_func_diag;
2510 if (type == void_type_node)
2512 error ("too many arguments to function %qE", function);
2513 return parmnum;
2516 if (selector && argnum > 2)
2518 rname = selector;
2519 argnum -= 2;
2522 STRIP_TYPE_NOPS (val);
2524 val = require_complete_type (val);
2526 if (type != 0)
2528 /* Formal parm type is specified by a function prototype. */
2529 tree parmval;
2531 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2533 error ("type of formal parameter %d is incomplete", parmnum + 1);
2534 parmval = val;
2536 else
2538 /* Optionally warn about conversions that
2539 differ from the default conversions. */
2540 if (warn_traditional_conversion || warn_traditional)
2542 unsigned int formal_prec = TYPE_PRECISION (type);
2544 if (INTEGRAL_TYPE_P (type)
2545 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2546 warning (0, "passing argument %d of %qE as integer "
2547 "rather than floating due to prototype",
2548 argnum, rname);
2549 if (INTEGRAL_TYPE_P (type)
2550 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2551 warning (0, "passing argument %d of %qE as integer "
2552 "rather than complex due to prototype",
2553 argnum, rname);
2554 else if (TREE_CODE (type) == COMPLEX_TYPE
2555 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2556 warning (0, "passing argument %d of %qE as complex "
2557 "rather than floating due to prototype",
2558 argnum, rname);
2559 else if (TREE_CODE (type) == REAL_TYPE
2560 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2561 warning (0, "passing argument %d of %qE as floating "
2562 "rather than integer due to prototype",
2563 argnum, rname);
2564 else if (TREE_CODE (type) == COMPLEX_TYPE
2565 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2566 warning (0, "passing argument %d of %qE as complex "
2567 "rather than integer due to prototype",
2568 argnum, rname);
2569 else if (TREE_CODE (type) == REAL_TYPE
2570 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2571 warning (0, "passing argument %d of %qE as floating "
2572 "rather than complex due to prototype",
2573 argnum, rname);
2574 /* ??? At some point, messages should be written about
2575 conversions between complex types, but that's too messy
2576 to do now. */
2577 else if (TREE_CODE (type) == REAL_TYPE
2578 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2580 /* Warn if any argument is passed as `float',
2581 since without a prototype it would be `double'. */
2582 if (formal_prec == TYPE_PRECISION (float_type_node)
2583 && type != dfloat32_type_node)
2584 warning (0, "passing argument %d of %qE as %<float%> "
2585 "rather than %<double%> due to prototype",
2586 argnum, rname);
2588 /* Warn if mismatch between argument and prototype
2589 for decimal float types. Warn of conversions with
2590 binary float types and of precision narrowing due to
2591 prototype. */
2592 else if (type != TREE_TYPE (val)
2593 && (type == dfloat32_type_node
2594 || type == dfloat64_type_node
2595 || type == dfloat128_type_node
2596 || TREE_TYPE (val) == dfloat32_type_node
2597 || TREE_TYPE (val) == dfloat64_type_node
2598 || TREE_TYPE (val) == dfloat128_type_node)
2599 && (formal_prec
2600 <= TYPE_PRECISION (TREE_TYPE (val))
2601 || (type == dfloat128_type_node
2602 && (TREE_TYPE (val)
2603 != dfloat64_type_node
2604 && (TREE_TYPE (val)
2605 != dfloat32_type_node)))
2606 || (type == dfloat64_type_node
2607 && (TREE_TYPE (val)
2608 != dfloat32_type_node))))
2609 warning (0, "passing argument %d of %qE as %qT "
2610 "rather than %qT due to prototype",
2611 argnum, rname, type, TREE_TYPE (val));
2614 /* Detect integer changing in width or signedness.
2615 These warnings are only activated with
2616 -Wtraditional-conversion, not with -Wtraditional. */
2617 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2618 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2620 tree would_have_been = default_conversion (val);
2621 tree type1 = TREE_TYPE (would_have_been);
2623 if (TREE_CODE (type) == ENUMERAL_TYPE
2624 && (TYPE_MAIN_VARIANT (type)
2625 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2626 /* No warning if function asks for enum
2627 and the actual arg is that enum type. */
2629 else if (formal_prec != TYPE_PRECISION (type1))
2630 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2631 "with different width due to prototype",
2632 argnum, rname);
2633 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2635 /* Don't complain if the formal parameter type
2636 is an enum, because we can't tell now whether
2637 the value was an enum--even the same enum. */
2638 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2640 else if (TREE_CODE (val) == INTEGER_CST
2641 && int_fits_type_p (val, type))
2642 /* Change in signedness doesn't matter
2643 if a constant value is unaffected. */
2645 /* If the value is extended from a narrower
2646 unsigned type, it doesn't matter whether we
2647 pass it as signed or unsigned; the value
2648 certainly is the same either way. */
2649 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2650 && TYPE_UNSIGNED (TREE_TYPE (val)))
2652 else if (TYPE_UNSIGNED (type))
2653 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2654 "as unsigned due to prototype",
2655 argnum, rname);
2656 else
2657 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2658 "as signed due to prototype", argnum, rname);
2662 parmval = convert_for_assignment (type, val, ic_argpass,
2663 fundecl, function,
2664 parmnum + 1);
2666 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2667 && INTEGRAL_TYPE_P (type)
2668 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2669 parmval = default_conversion (parmval);
2671 argarray[parmnum] = parmval;
2673 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2674 && (TYPE_PRECISION (TREE_TYPE (val))
2675 < TYPE_PRECISION (double_type_node))
2676 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2678 if (type_generic)
2679 argarray[parmnum] = val;
2680 else
2681 /* Convert `float' to `double'. */
2682 argarray[parmnum] = convert (double_type_node, val);
2684 else if ((invalid_func_diag =
2685 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2687 error (invalid_func_diag);
2688 return -1;
2690 else
2691 /* Convert `short' and `char' to full-size `int'. */
2692 argarray[parmnum] = default_conversion (val);
2694 if (typetail)
2695 typetail = TREE_CHAIN (typetail);
2698 gcc_assert (parmnum == nargs);
2700 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2702 error ("too few arguments to function %qE", function);
2703 return -1;
2706 return parmnum;
2709 /* This is the entry point used by the parser to build unary operators
2710 in the input. CODE, a tree_code, specifies the unary operator, and
2711 ARG is the operand. For unary plus, the C parser currently uses
2712 CONVERT_EXPR for code. */
2714 struct c_expr
2715 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2717 struct c_expr result;
2719 result.original_code = ERROR_MARK;
2720 result.value = build_unary_op (code, arg.value, 0);
2722 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2723 overflow_warning (result.value);
2725 return result;
2728 /* This is the entry point used by the parser to build binary operators
2729 in the input. CODE, a tree_code, specifies the binary operator, and
2730 ARG1 and ARG2 are the operands. In addition to constructing the
2731 expression, we check for operands that were written with other binary
2732 operators in a way that is likely to confuse the user. */
2734 struct c_expr
2735 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2736 struct c_expr arg2)
2738 struct c_expr result;
2740 enum tree_code code1 = arg1.original_code;
2741 enum tree_code code2 = arg2.original_code;
2743 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2744 result.original_code = code;
2746 if (TREE_CODE (result.value) == ERROR_MARK)
2747 return result;
2749 /* Check for cases such as x+y<<z which users are likely
2750 to misinterpret. */
2751 if (warn_parentheses)
2752 warn_about_parentheses (code, code1, code2);
2754 if (code1 != tcc_comparison)
2755 warn_logical_operator (code, arg1.value, arg2.value);
2757 /* Warn about comparisons against string literals, with the exception
2758 of testing for equality or inequality of a string literal with NULL. */
2759 if (code == EQ_EXPR || code == NE_EXPR)
2761 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2762 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2763 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2765 else if (TREE_CODE_CLASS (code) == tcc_comparison
2766 && (code1 == STRING_CST || code2 == STRING_CST))
2767 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2769 if (TREE_OVERFLOW_P (result.value)
2770 && !TREE_OVERFLOW_P (arg1.value)
2771 && !TREE_OVERFLOW_P (arg2.value))
2772 overflow_warning (result.value);
2774 return result;
2777 /* Return a tree for the difference of pointers OP0 and OP1.
2778 The resulting tree has type int. */
2780 static tree
2781 pointer_diff (tree op0, tree op1)
2783 tree restype = ptrdiff_type_node;
2785 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2786 tree con0, con1, lit0, lit1;
2787 tree orig_op1 = op1;
2789 if (pedantic || warn_pointer_arith)
2791 if (TREE_CODE (target_type) == VOID_TYPE)
2792 pedwarn ("pointer of type %<void *%> used in subtraction");
2793 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2794 pedwarn ("pointer to a function used in subtraction");
2797 /* If the conversion to ptrdiff_type does anything like widening or
2798 converting a partial to an integral mode, we get a convert_expression
2799 that is in the way to do any simplifications.
2800 (fold-const.c doesn't know that the extra bits won't be needed.
2801 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2802 different mode in place.)
2803 So first try to find a common term here 'by hand'; we want to cover
2804 at least the cases that occur in legal static initializers. */
2805 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2806 && (TYPE_PRECISION (TREE_TYPE (op0))
2807 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2808 con0 = TREE_OPERAND (op0, 0);
2809 else
2810 con0 = op0;
2811 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2812 && (TYPE_PRECISION (TREE_TYPE (op1))
2813 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2814 con1 = TREE_OPERAND (op1, 0);
2815 else
2816 con1 = op1;
2818 if (TREE_CODE (con0) == PLUS_EXPR)
2820 lit0 = TREE_OPERAND (con0, 1);
2821 con0 = TREE_OPERAND (con0, 0);
2823 else
2824 lit0 = integer_zero_node;
2826 if (TREE_CODE (con1) == PLUS_EXPR)
2828 lit1 = TREE_OPERAND (con1, 1);
2829 con1 = TREE_OPERAND (con1, 0);
2831 else
2832 lit1 = integer_zero_node;
2834 if (operand_equal_p (con0, con1, 0))
2836 op0 = lit0;
2837 op1 = lit1;
2841 /* First do the subtraction as integers;
2842 then drop through to build the divide operator.
2843 Do not do default conversions on the minus operator
2844 in case restype is a short type. */
2846 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2847 convert (restype, op1), 0);
2848 /* This generates an error if op1 is pointer to incomplete type. */
2849 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2850 error ("arithmetic on pointer to an incomplete type");
2852 /* This generates an error if op0 is pointer to incomplete type. */
2853 op1 = c_size_in_bytes (target_type);
2855 /* Divide by the size, in easiest possible way. */
2856 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2859 /* Construct and perhaps optimize a tree representation
2860 for a unary operation. CODE, a tree_code, specifies the operation
2861 and XARG is the operand.
2862 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2863 the default promotions (such as from short to int).
2864 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2865 allows non-lvalues; this is only used to handle conversion of non-lvalue
2866 arrays to pointers in C99. */
2868 tree
2869 build_unary_op (enum tree_code code, tree xarg, int flag)
2871 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2872 tree arg = xarg;
2873 tree argtype = 0;
2874 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2875 tree val;
2876 int noconvert = flag;
2877 const char *invalid_op_diag;
2879 if (typecode == ERROR_MARK)
2880 return error_mark_node;
2881 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2882 typecode = INTEGER_TYPE;
2884 if ((invalid_op_diag
2885 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2887 error (invalid_op_diag);
2888 return error_mark_node;
2891 switch (code)
2893 case CONVERT_EXPR:
2894 /* This is used for unary plus, because a CONVERT_EXPR
2895 is enough to prevent anybody from looking inside for
2896 associativity, but won't generate any code. */
2897 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2898 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
2899 || typecode == VECTOR_TYPE))
2901 error ("wrong type argument to unary plus");
2902 return error_mark_node;
2904 else if (!noconvert)
2905 arg = default_conversion (arg);
2906 arg = non_lvalue (arg);
2907 break;
2909 case NEGATE_EXPR:
2910 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2911 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
2912 || typecode == VECTOR_TYPE))
2914 error ("wrong type argument to unary minus");
2915 return error_mark_node;
2917 else if (!noconvert)
2918 arg = default_conversion (arg);
2919 break;
2921 case BIT_NOT_EXPR:
2922 /* ~ works on integer types and non float vectors. */
2923 if (typecode == INTEGER_TYPE
2924 || (typecode == VECTOR_TYPE
2925 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
2927 if (!noconvert)
2928 arg = default_conversion (arg);
2930 else if (typecode == COMPLEX_TYPE)
2932 code = CONJ_EXPR;
2933 if (pedantic)
2934 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2935 if (!noconvert)
2936 arg = default_conversion (arg);
2938 else
2940 error ("wrong type argument to bit-complement");
2941 return error_mark_node;
2943 break;
2945 case ABS_EXPR:
2946 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2948 error ("wrong type argument to abs");
2949 return error_mark_node;
2951 else if (!noconvert)
2952 arg = default_conversion (arg);
2953 break;
2955 case CONJ_EXPR:
2956 /* Conjugating a real value is a no-op, but allow it anyway. */
2957 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2958 || typecode == COMPLEX_TYPE))
2960 error ("wrong type argument to conjugation");
2961 return error_mark_node;
2963 else if (!noconvert)
2964 arg = default_conversion (arg);
2965 break;
2967 case TRUTH_NOT_EXPR:
2968 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
2969 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2970 && typecode != COMPLEX_TYPE)
2972 error ("wrong type argument to unary exclamation mark");
2973 return error_mark_node;
2975 arg = c_objc_common_truthvalue_conversion (arg);
2976 return invert_truthvalue (arg);
2978 case REALPART_EXPR:
2979 if (TREE_CODE (arg) == COMPLEX_CST)
2980 return TREE_REALPART (arg);
2981 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2982 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2983 else
2984 return arg;
2986 case IMAGPART_EXPR:
2987 if (TREE_CODE (arg) == COMPLEX_CST)
2988 return TREE_IMAGPART (arg);
2989 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2990 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2991 else
2992 return convert (TREE_TYPE (arg), integer_zero_node);
2994 case PREINCREMENT_EXPR:
2995 case POSTINCREMENT_EXPR:
2996 case PREDECREMENT_EXPR:
2997 case POSTDECREMENT_EXPR:
2999 /* Increment or decrement the real part of the value,
3000 and don't change the imaginary part. */
3001 if (typecode == COMPLEX_TYPE)
3003 tree real, imag;
3005 if (pedantic)
3006 pedwarn ("ISO C does not support %<++%> and %<--%>"
3007 " on complex types");
3009 arg = stabilize_reference (arg);
3010 real = build_unary_op (REALPART_EXPR, arg, 1);
3011 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3012 real = build_unary_op (code, real, 1);
3013 if (real == error_mark_node || imag == error_mark_node)
3014 return error_mark_node;
3015 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3016 real, imag);
3019 /* Report invalid types. */
3021 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3022 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3024 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3025 error ("wrong type argument to increment");
3026 else
3027 error ("wrong type argument to decrement");
3029 return error_mark_node;
3033 tree inc;
3034 tree result_type = TREE_TYPE (arg);
3036 arg = get_unwidened (arg, 0);
3037 argtype = TREE_TYPE (arg);
3039 /* Compute the increment. */
3041 if (typecode == POINTER_TYPE)
3043 /* If pointer target is an undefined struct,
3044 we just cannot know how to do the arithmetic. */
3045 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
3047 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3048 error ("increment of pointer to unknown structure");
3049 else
3050 error ("decrement of pointer to unknown structure");
3052 else if ((pedantic || warn_pointer_arith)
3053 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3054 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3056 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3057 pedwarn ("wrong type argument to increment");
3058 else
3059 pedwarn ("wrong type argument to decrement");
3062 inc = c_size_in_bytes (TREE_TYPE (result_type));
3063 inc = fold_convert (sizetype, inc);
3065 else if (FRACT_MODE_P (TYPE_MODE (result_type)))
3067 /* For signed fract types, we invert ++ to -- or
3068 -- to ++, and change inc from 1 to -1, because
3069 it is not possible to represent 1 in signed fract constants.
3070 For unsigned fract types, the result always overflows and
3071 we get an undefined (original) or the maximum value. */
3072 if (code == PREINCREMENT_EXPR)
3073 code = PREDECREMENT_EXPR;
3074 else if (code == PREDECREMENT_EXPR)
3075 code = PREINCREMENT_EXPR;
3076 else if (code == POSTINCREMENT_EXPR)
3077 code = POSTDECREMENT_EXPR;
3078 else /* code == POSTDECREMENT_EXPR */
3079 code = POSTINCREMENT_EXPR;
3081 inc = integer_minus_one_node;
3082 inc = convert (argtype, inc);
3084 else
3086 inc = integer_one_node;
3087 inc = convert (argtype, inc);
3090 /* Complain about anything else that is not a true lvalue. */
3091 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3092 || code == POSTINCREMENT_EXPR)
3093 ? lv_increment
3094 : lv_decrement)))
3095 return error_mark_node;
3097 /* Report a read-only lvalue. */
3098 if (TREE_READONLY (arg))
3100 readonly_error (arg,
3101 ((code == PREINCREMENT_EXPR
3102 || code == POSTINCREMENT_EXPR)
3103 ? lv_increment : lv_decrement));
3104 return error_mark_node;
3107 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3108 val = boolean_increment (code, arg);
3109 else
3110 val = build2 (code, TREE_TYPE (arg), arg, inc);
3111 TREE_SIDE_EFFECTS (val) = 1;
3112 val = convert (result_type, val);
3113 if (TREE_CODE (val) != code)
3114 TREE_NO_WARNING (val) = 1;
3115 return val;
3118 case ADDR_EXPR:
3119 /* Note that this operation never does default_conversion. */
3121 /* Let &* cancel out to simplify resulting code. */
3122 if (TREE_CODE (arg) == INDIRECT_REF)
3124 /* Don't let this be an lvalue. */
3125 if (lvalue_p (TREE_OPERAND (arg, 0)))
3126 return non_lvalue (TREE_OPERAND (arg, 0));
3127 return TREE_OPERAND (arg, 0);
3130 /* For &x[y], return x+y */
3131 if (TREE_CODE (arg) == ARRAY_REF)
3133 tree op0 = TREE_OPERAND (arg, 0);
3134 if (!c_mark_addressable (op0))
3135 return error_mark_node;
3136 return build_binary_op (PLUS_EXPR,
3137 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3138 ? array_to_pointer_conversion (op0)
3139 : op0),
3140 TREE_OPERAND (arg, 1), 1);
3143 /* Anything not already handled and not a true memory reference
3144 or a non-lvalue array is an error. */
3145 else if (typecode != FUNCTION_TYPE && !flag
3146 && !lvalue_or_else (arg, lv_addressof))
3147 return error_mark_node;
3149 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3150 argtype = TREE_TYPE (arg);
3152 /* If the lvalue is const or volatile, merge that into the type
3153 to which the address will point. Note that you can't get a
3154 restricted pointer by taking the address of something, so we
3155 only have to deal with `const' and `volatile' here. */
3156 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3157 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3158 argtype = c_build_type_variant (argtype,
3159 TREE_READONLY (arg),
3160 TREE_THIS_VOLATILE (arg));
3162 if (!c_mark_addressable (arg))
3163 return error_mark_node;
3165 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3166 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3168 argtype = build_pointer_type (argtype);
3170 /* ??? Cope with user tricks that amount to offsetof. Delete this
3171 when we have proper support for integer constant expressions. */
3172 val = get_base_address (arg);
3173 if (val && TREE_CODE (val) == INDIRECT_REF
3174 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3176 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3178 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3179 return fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3182 val = build1 (ADDR_EXPR, argtype, arg);
3184 return val;
3186 default:
3187 gcc_unreachable ();
3190 if (argtype == 0)
3191 argtype = TREE_TYPE (arg);
3192 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3193 : fold_build1 (code, argtype, arg);
3196 /* Return nonzero if REF is an lvalue valid for this language.
3197 Lvalues can be assigned, unless their type has TYPE_READONLY.
3198 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3200 static int
3201 lvalue_p (const_tree ref)
3203 const enum tree_code code = TREE_CODE (ref);
3205 switch (code)
3207 case REALPART_EXPR:
3208 case IMAGPART_EXPR:
3209 case COMPONENT_REF:
3210 return lvalue_p (TREE_OPERAND (ref, 0));
3212 case COMPOUND_LITERAL_EXPR:
3213 case STRING_CST:
3214 return 1;
3216 case INDIRECT_REF:
3217 case ARRAY_REF:
3218 case VAR_DECL:
3219 case PARM_DECL:
3220 case RESULT_DECL:
3221 case ERROR_MARK:
3222 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3223 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3225 case BIND_EXPR:
3226 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3228 default:
3229 return 0;
3233 /* Give an error for storing in something that is 'const'. */
3235 static void
3236 readonly_error (tree arg, enum lvalue_use use)
3238 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3239 || use == lv_asm);
3240 /* Using this macro rather than (for example) arrays of messages
3241 ensures that all the format strings are checked at compile
3242 time. */
3243 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3244 : (use == lv_increment ? (I) \
3245 : (use == lv_decrement ? (D) : (AS))))
3246 if (TREE_CODE (arg) == COMPONENT_REF)
3248 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3249 readonly_error (TREE_OPERAND (arg, 0), use);
3250 else
3251 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3252 G_("increment of read-only member %qD"),
3253 G_("decrement of read-only member %qD"),
3254 G_("read-only member %qD used as %<asm%> output")),
3255 TREE_OPERAND (arg, 1));
3257 else if (TREE_CODE (arg) == VAR_DECL)
3258 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3259 G_("increment of read-only variable %qD"),
3260 G_("decrement of read-only variable %qD"),
3261 G_("read-only variable %qD used as %<asm%> output")),
3262 arg);
3263 else
3264 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3265 G_("increment of read-only location %qE"),
3266 G_("decrement of read-only location %qE"),
3267 G_("read-only location %qE used as %<asm%> output")),
3268 arg);
3272 /* Return nonzero if REF is an lvalue valid for this language;
3273 otherwise, print an error message and return zero. USE says
3274 how the lvalue is being used and so selects the error message. */
3276 static int
3277 lvalue_or_else (const_tree ref, enum lvalue_use use)
3279 int win = lvalue_p (ref);
3281 if (!win)
3282 lvalue_error (use);
3284 return win;
3287 /* Mark EXP saying that we need to be able to take the
3288 address of it; it should not be allocated in a register.
3289 Returns true if successful. */
3291 bool
3292 c_mark_addressable (tree exp)
3294 tree x = exp;
3296 while (1)
3297 switch (TREE_CODE (x))
3299 case COMPONENT_REF:
3300 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3302 error
3303 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3304 return false;
3307 /* ... fall through ... */
3309 case ADDR_EXPR:
3310 case ARRAY_REF:
3311 case REALPART_EXPR:
3312 case IMAGPART_EXPR:
3313 x = TREE_OPERAND (x, 0);
3314 break;
3316 case COMPOUND_LITERAL_EXPR:
3317 case CONSTRUCTOR:
3318 TREE_ADDRESSABLE (x) = 1;
3319 return true;
3321 case VAR_DECL:
3322 case CONST_DECL:
3323 case PARM_DECL:
3324 case RESULT_DECL:
3325 if (C_DECL_REGISTER (x)
3326 && DECL_NONLOCAL (x))
3328 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3330 error
3331 ("global register variable %qD used in nested function", x);
3332 return false;
3334 pedwarn ("register variable %qD used in nested function", x);
3336 else if (C_DECL_REGISTER (x))
3338 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3339 error ("address of global register variable %qD requested", x);
3340 else
3341 error ("address of register variable %qD requested", x);
3342 return false;
3345 /* drops in */
3346 case FUNCTION_DECL:
3347 TREE_ADDRESSABLE (x) = 1;
3348 /* drops out */
3349 default:
3350 return true;
3354 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3356 tree
3357 build_conditional_expr (tree ifexp, tree op1, tree op2)
3359 tree type1;
3360 tree type2;
3361 enum tree_code code1;
3362 enum tree_code code2;
3363 tree result_type = NULL;
3364 tree orig_op1 = op1, orig_op2 = op2;
3366 /* Promote both alternatives. */
3368 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3369 op1 = default_conversion (op1);
3370 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3371 op2 = default_conversion (op2);
3373 if (TREE_CODE (ifexp) == ERROR_MARK
3374 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3375 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3376 return error_mark_node;
3378 type1 = TREE_TYPE (op1);
3379 code1 = TREE_CODE (type1);
3380 type2 = TREE_TYPE (op2);
3381 code2 = TREE_CODE (type2);
3383 /* C90 does not permit non-lvalue arrays in conditional expressions.
3384 In C99 they will be pointers by now. */
3385 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3387 error ("non-lvalue array in conditional expression");
3388 return error_mark_node;
3391 /* Quickly detect the usual case where op1 and op2 have the same type
3392 after promotion. */
3393 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3395 if (type1 == type2)
3396 result_type = type1;
3397 else
3398 result_type = TYPE_MAIN_VARIANT (type1);
3400 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3401 || code1 == COMPLEX_TYPE)
3402 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3403 || code2 == COMPLEX_TYPE))
3405 result_type = c_common_type (type1, type2);
3407 /* If -Wsign-compare, warn here if type1 and type2 have
3408 different signedness. We'll promote the signed to unsigned
3409 and later code won't know it used to be different.
3410 Do this check on the original types, so that explicit casts
3411 will be considered, but default promotions won't. */
3412 if (warn_sign_compare && !skip_evaluation)
3414 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3415 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3417 if (unsigned_op1 ^ unsigned_op2)
3419 bool ovf;
3421 /* Do not warn if the result type is signed, since the
3422 signed type will only be chosen if it can represent
3423 all the values of the unsigned type. */
3424 if (!TYPE_UNSIGNED (result_type))
3425 /* OK */;
3426 /* Do not warn if the signed quantity is an unsuffixed
3427 integer literal (or some static constant expression
3428 involving such literals) and it is non-negative. */
3429 else if ((unsigned_op2
3430 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3431 || (unsigned_op1
3432 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3433 /* OK */;
3434 else
3435 warning (0, "signed and unsigned type in conditional expression");
3439 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3441 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3442 pedwarn ("ISO C forbids conditional expr with only one void side");
3443 result_type = void_type_node;
3445 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3447 if (comp_target_types (type1, type2))
3448 result_type = common_pointer_type (type1, type2);
3449 else if (null_pointer_constant_p (orig_op1))
3450 result_type = qualify_type (type2, type1);
3451 else if (null_pointer_constant_p (orig_op2))
3452 result_type = qualify_type (type1, type2);
3453 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3455 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3456 pedwarn ("ISO C forbids conditional expr between "
3457 "%<void *%> and function pointer");
3458 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3459 TREE_TYPE (type2)));
3461 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3463 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3464 pedwarn ("ISO C forbids conditional expr between "
3465 "%<void *%> and function pointer");
3466 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3467 TREE_TYPE (type1)));
3469 else
3471 pedwarn ("pointer type mismatch in conditional expression");
3472 result_type = build_pointer_type (void_type_node);
3475 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3477 if (!null_pointer_constant_p (orig_op2))
3478 pedwarn ("pointer/integer type mismatch in conditional expression");
3479 else
3481 op2 = null_pointer_node;
3483 result_type = type1;
3485 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3487 if (!null_pointer_constant_p (orig_op1))
3488 pedwarn ("pointer/integer type mismatch in conditional expression");
3489 else
3491 op1 = null_pointer_node;
3493 result_type = type2;
3496 if (!result_type)
3498 if (flag_cond_mismatch)
3499 result_type = void_type_node;
3500 else
3502 error ("type mismatch in conditional expression");
3503 return error_mark_node;
3507 /* Merge const and volatile flags of the incoming types. */
3508 result_type
3509 = build_type_variant (result_type,
3510 TREE_READONLY (op1) || TREE_READONLY (op2),
3511 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3513 if (result_type != TREE_TYPE (op1))
3514 op1 = convert_and_check (result_type, op1);
3515 if (result_type != TREE_TYPE (op2))
3516 op2 = convert_and_check (result_type, op2);
3518 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3521 /* Return a compound expression that performs two expressions and
3522 returns the value of the second of them. */
3524 tree
3525 build_compound_expr (tree expr1, tree expr2)
3527 if (!TREE_SIDE_EFFECTS (expr1))
3529 /* The left-hand operand of a comma expression is like an expression
3530 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3531 any side-effects, unless it was explicitly cast to (void). */
3532 if (warn_unused_value)
3534 if (VOID_TYPE_P (TREE_TYPE (expr1))
3535 && (TREE_CODE (expr1) == NOP_EXPR
3536 || TREE_CODE (expr1) == CONVERT_EXPR))
3537 ; /* (void) a, b */
3538 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3539 && TREE_CODE (expr1) == COMPOUND_EXPR
3540 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3541 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3542 ; /* (void) a, (void) b, c */
3543 else
3544 warning (OPT_Wunused_value,
3545 "left-hand operand of comma expression has no effect");
3549 /* With -Wunused, we should also warn if the left-hand operand does have
3550 side-effects, but computes a value which is not used. For example, in
3551 `foo() + bar(), baz()' the result of the `+' operator is not used,
3552 so we should issue a warning. */
3553 else if (warn_unused_value)
3554 warn_if_unused_value (expr1, input_location);
3556 if (expr2 == error_mark_node)
3557 return error_mark_node;
3559 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3562 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3564 tree
3565 build_c_cast (tree type, tree expr)
3567 tree value = expr;
3569 if (type == error_mark_node || expr == error_mark_node)
3570 return error_mark_node;
3572 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3573 only in <protocol> qualifications. But when constructing cast expressions,
3574 the protocols do matter and must be kept around. */
3575 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3576 return build1 (NOP_EXPR, type, expr);
3578 type = TYPE_MAIN_VARIANT (type);
3580 if (TREE_CODE (type) == ARRAY_TYPE)
3582 error ("cast specifies array type");
3583 return error_mark_node;
3586 if (TREE_CODE (type) == FUNCTION_TYPE)
3588 error ("cast specifies function type");
3589 return error_mark_node;
3592 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3594 if (pedantic)
3596 if (TREE_CODE (type) == RECORD_TYPE
3597 || TREE_CODE (type) == UNION_TYPE)
3598 pedwarn ("ISO C forbids casting nonscalar to the same type");
3601 else if (TREE_CODE (type) == UNION_TYPE)
3603 tree field;
3605 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3606 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3607 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3608 break;
3610 if (field)
3612 tree t;
3614 if (pedantic)
3615 pedwarn ("ISO C forbids casts to union type");
3616 t = digest_init (type,
3617 build_constructor_single (type, field, value),
3618 true, 0);
3619 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3620 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3621 return t;
3623 error ("cast to union type from type not present in union");
3624 return error_mark_node;
3626 else
3628 tree otype, ovalue;
3630 if (type == void_type_node)
3631 return build1 (CONVERT_EXPR, type, value);
3633 otype = TREE_TYPE (value);
3635 /* Optionally warn about potentially worrisome casts. */
3637 if (warn_cast_qual
3638 && TREE_CODE (type) == POINTER_TYPE
3639 && TREE_CODE (otype) == POINTER_TYPE)
3641 tree in_type = type;
3642 tree in_otype = otype;
3643 int added = 0;
3644 int discarded = 0;
3646 /* Check that the qualifiers on IN_TYPE are a superset of
3647 the qualifiers of IN_OTYPE. The outermost level of
3648 POINTER_TYPE nodes is uninteresting and we stop as soon
3649 as we hit a non-POINTER_TYPE node on either type. */
3652 in_otype = TREE_TYPE (in_otype);
3653 in_type = TREE_TYPE (in_type);
3655 /* GNU C allows cv-qualified function types. 'const'
3656 means the function is very pure, 'volatile' means it
3657 can't return. We need to warn when such qualifiers
3658 are added, not when they're taken away. */
3659 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3660 && TREE_CODE (in_type) == FUNCTION_TYPE)
3661 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3662 else
3663 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3665 while (TREE_CODE (in_type) == POINTER_TYPE
3666 && TREE_CODE (in_otype) == POINTER_TYPE);
3668 if (added)
3669 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
3671 if (discarded)
3672 /* There are qualifiers present in IN_OTYPE that are not
3673 present in IN_TYPE. */
3674 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
3677 /* Warn about possible alignment problems. */
3678 if (STRICT_ALIGNMENT
3679 && TREE_CODE (type) == POINTER_TYPE
3680 && TREE_CODE (otype) == POINTER_TYPE
3681 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3682 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3683 /* Don't warn about opaque types, where the actual alignment
3684 restriction is unknown. */
3685 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3686 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3687 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3688 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3689 warning (OPT_Wcast_align,
3690 "cast increases required alignment of target type");
3692 if (TREE_CODE (type) == INTEGER_TYPE
3693 && TREE_CODE (otype) == POINTER_TYPE
3694 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3695 /* Unlike conversion of integers to pointers, where the
3696 warning is disabled for converting constants because
3697 of cases such as SIG_*, warn about converting constant
3698 pointers to integers. In some cases it may cause unwanted
3699 sign extension, and a warning is appropriate. */
3700 warning (OPT_Wpointer_to_int_cast,
3701 "cast from pointer to integer of different size");
3703 if (TREE_CODE (value) == CALL_EXPR
3704 && TREE_CODE (type) != TREE_CODE (otype))
3705 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3706 "to non-matching type %qT", otype, type);
3708 if (TREE_CODE (type) == POINTER_TYPE
3709 && TREE_CODE (otype) == INTEGER_TYPE
3710 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3711 /* Don't warn about converting any constant. */
3712 && !TREE_CONSTANT (value))
3713 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3714 "of different size");
3716 if (warn_strict_aliasing <= 2)
3717 strict_aliasing_warning (otype, type, expr);
3719 /* If pedantic, warn for conversions between function and object
3720 pointer types, except for converting a null pointer constant
3721 to function pointer type. */
3722 if (pedantic
3723 && TREE_CODE (type) == POINTER_TYPE
3724 && TREE_CODE (otype) == POINTER_TYPE
3725 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3726 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3727 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3729 if (pedantic
3730 && TREE_CODE (type) == POINTER_TYPE
3731 && TREE_CODE (otype) == POINTER_TYPE
3732 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3733 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3734 && !null_pointer_constant_p (value))
3735 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3737 ovalue = value;
3738 value = convert (type, value);
3740 /* Ignore any integer overflow caused by the cast. */
3741 if (TREE_CODE (value) == INTEGER_CST)
3743 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
3745 if (!TREE_OVERFLOW (value))
3747 /* Avoid clobbering a shared constant. */
3748 value = copy_node (value);
3749 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3752 else if (TREE_OVERFLOW (value))
3753 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3754 value = build_int_cst_wide (TREE_TYPE (value),
3755 TREE_INT_CST_LOW (value),
3756 TREE_INT_CST_HIGH (value));
3760 /* Don't let a cast be an lvalue. */
3761 if (value == expr)
3762 value = non_lvalue (value);
3764 return value;
3767 /* Interpret a cast of expression EXPR to type TYPE. */
3768 tree
3769 c_cast_expr (struct c_type_name *type_name, tree expr)
3771 tree type;
3772 int saved_wsp = warn_strict_prototypes;
3774 /* This avoids warnings about unprototyped casts on
3775 integers. E.g. "#define SIG_DFL (void(*)())0". */
3776 if (TREE_CODE (expr) == INTEGER_CST)
3777 warn_strict_prototypes = 0;
3778 type = groktypename (type_name);
3779 warn_strict_prototypes = saved_wsp;
3781 return build_c_cast (type, expr);
3784 /* Build an assignment expression of lvalue LHS from value RHS.
3785 MODIFYCODE is the code for a binary operator that we use
3786 to combine the old value of LHS with RHS to get the new value.
3787 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3789 tree
3790 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3792 tree result;
3793 tree newrhs;
3794 tree lhstype = TREE_TYPE (lhs);
3795 tree olhstype = lhstype;
3797 /* Types that aren't fully specified cannot be used in assignments. */
3798 lhs = require_complete_type (lhs);
3800 /* Avoid duplicate error messages from operands that had errors. */
3801 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3802 return error_mark_node;
3804 if (!lvalue_or_else (lhs, lv_assign))
3805 return error_mark_node;
3807 STRIP_TYPE_NOPS (rhs);
3809 newrhs = rhs;
3811 /* If a binary op has been requested, combine the old LHS value with the RHS
3812 producing the value we should actually store into the LHS. */
3814 if (modifycode != NOP_EXPR)
3816 lhs = stabilize_reference (lhs);
3817 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3820 /* Give an error for storing in something that is 'const'. */
3822 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3823 || ((TREE_CODE (lhstype) == RECORD_TYPE
3824 || TREE_CODE (lhstype) == UNION_TYPE)
3825 && C_TYPE_FIELDS_READONLY (lhstype)))
3827 readonly_error (lhs, lv_assign);
3828 return error_mark_node;
3831 /* If storing into a structure or union member,
3832 it has probably been given type `int'.
3833 Compute the type that would go with
3834 the actual amount of storage the member occupies. */
3836 if (TREE_CODE (lhs) == COMPONENT_REF
3837 && (TREE_CODE (lhstype) == INTEGER_TYPE
3838 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3839 || TREE_CODE (lhstype) == REAL_TYPE
3840 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3841 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3843 /* If storing in a field that is in actuality a short or narrower than one,
3844 we must store in the field in its actual type. */
3846 if (lhstype != TREE_TYPE (lhs))
3848 lhs = copy_node (lhs);
3849 TREE_TYPE (lhs) = lhstype;
3852 /* Convert new value to destination type. */
3854 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3855 NULL_TREE, NULL_TREE, 0);
3856 if (TREE_CODE (newrhs) == ERROR_MARK)
3857 return error_mark_node;
3859 /* Emit ObjC write barrier, if necessary. */
3860 if (c_dialect_objc () && flag_objc_gc)
3862 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3863 if (result)
3864 return result;
3867 /* Scan operands. */
3869 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3870 TREE_SIDE_EFFECTS (result) = 1;
3872 /* If we got the LHS in a different type for storing in,
3873 convert the result back to the nominal type of LHS
3874 so that the value we return always has the same type
3875 as the LHS argument. */
3877 if (olhstype == TREE_TYPE (result))
3878 return result;
3879 return convert_for_assignment (olhstype, result, ic_assign,
3880 NULL_TREE, NULL_TREE, 0);
3883 /* Convert value RHS to type TYPE as preparation for an assignment
3884 to an lvalue of type TYPE.
3885 The real work of conversion is done by `convert'.
3886 The purpose of this function is to generate error messages
3887 for assignments that are not allowed in C.
3888 ERRTYPE says whether it is argument passing, assignment,
3889 initialization or return.
3891 FUNCTION is a tree for the function being called.
3892 PARMNUM is the number of the argument, for printing in error messages. */
3894 static tree
3895 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3896 tree fundecl, tree function, int parmnum)
3898 enum tree_code codel = TREE_CODE (type);
3899 tree rhstype;
3900 enum tree_code coder;
3901 tree rname = NULL_TREE;
3902 bool objc_ok = false;
3904 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3906 tree selector;
3907 /* Change pointer to function to the function itself for
3908 diagnostics. */
3909 if (TREE_CODE (function) == ADDR_EXPR
3910 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3911 function = TREE_OPERAND (function, 0);
3913 /* Handle an ObjC selector specially for diagnostics. */
3914 selector = objc_message_selector ();
3915 rname = function;
3916 if (selector && parmnum > 2)
3918 rname = selector;
3919 parmnum -= 2;
3923 /* This macro is used to emit diagnostics to ensure that all format
3924 strings are complete sentences, visible to gettext and checked at
3925 compile time. */
3926 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3927 do { \
3928 switch (errtype) \
3930 case ic_argpass: \
3931 pedwarn (AR, parmnum, rname); \
3932 break; \
3933 case ic_argpass_nonproto: \
3934 warning (0, AR, parmnum, rname); \
3935 break; \
3936 case ic_assign: \
3937 pedwarn (AS); \
3938 break; \
3939 case ic_init: \
3940 pedwarn (IN); \
3941 break; \
3942 case ic_return: \
3943 pedwarn (RE); \
3944 break; \
3945 default: \
3946 gcc_unreachable (); \
3948 } while (0)
3950 STRIP_TYPE_NOPS (rhs);
3952 if (optimize && TREE_CODE (rhs) == VAR_DECL
3953 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3954 rhs = decl_constant_value_for_broken_optimization (rhs);
3956 rhstype = TREE_TYPE (rhs);
3957 coder = TREE_CODE (rhstype);
3959 if (coder == ERROR_MARK)
3960 return error_mark_node;
3962 if (c_dialect_objc ())
3964 int parmno;
3966 switch (errtype)
3968 case ic_return:
3969 parmno = 0;
3970 break;
3972 case ic_assign:
3973 parmno = -1;
3974 break;
3976 case ic_init:
3977 parmno = -2;
3978 break;
3980 default:
3981 parmno = parmnum;
3982 break;
3985 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3988 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3989 return rhs;
3991 if (coder == VOID_TYPE)
3993 /* Except for passing an argument to an unprototyped function,
3994 this is a constraint violation. When passing an argument to
3995 an unprototyped function, it is compile-time undefined;
3996 making it a constraint in that case was rejected in
3997 DR#252. */
3998 error ("void value not ignored as it ought to be");
3999 return error_mark_node;
4001 /* A type converts to a reference to it.
4002 This code doesn't fully support references, it's just for the
4003 special case of va_start and va_copy. */
4004 if (codel == REFERENCE_TYPE
4005 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4007 if (!lvalue_p (rhs))
4009 error ("cannot pass rvalue to reference parameter");
4010 return error_mark_node;
4012 if (!c_mark_addressable (rhs))
4013 return error_mark_node;
4014 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4016 /* We already know that these two types are compatible, but they
4017 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4018 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4019 likely to be va_list, a typedef to __builtin_va_list, which
4020 is different enough that it will cause problems later. */
4021 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4022 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4024 rhs = build1 (NOP_EXPR, type, rhs);
4025 return rhs;
4027 /* Some types can interconvert without explicit casts. */
4028 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4029 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4030 return convert (type, rhs);
4031 /* Arithmetic types all interconvert, and enum is treated like int. */
4032 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4033 || codel == FIXED_POINT_TYPE
4034 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4035 || codel == BOOLEAN_TYPE)
4036 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4037 || coder == FIXED_POINT_TYPE
4038 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4039 || coder == BOOLEAN_TYPE))
4040 return convert_and_check (type, rhs);
4042 /* Aggregates in different TUs might need conversion. */
4043 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4044 && codel == coder
4045 && comptypes (type, rhstype))
4046 return convert_and_check (type, rhs);
4048 /* Conversion to a transparent union from its member types.
4049 This applies only to function arguments. */
4050 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4051 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
4053 tree memb, marginal_memb = NULL_TREE;
4055 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4057 tree memb_type = TREE_TYPE (memb);
4059 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4060 TYPE_MAIN_VARIANT (rhstype)))
4061 break;
4063 if (TREE_CODE (memb_type) != POINTER_TYPE)
4064 continue;
4066 if (coder == POINTER_TYPE)
4068 tree ttl = TREE_TYPE (memb_type);
4069 tree ttr = TREE_TYPE (rhstype);
4071 /* Any non-function converts to a [const][volatile] void *
4072 and vice versa; otherwise, targets must be the same.
4073 Meanwhile, the lhs target must have all the qualifiers of
4074 the rhs. */
4075 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4076 || comp_target_types (memb_type, rhstype))
4078 /* If this type won't generate any warnings, use it. */
4079 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4080 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4081 && TREE_CODE (ttl) == FUNCTION_TYPE)
4082 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4083 == TYPE_QUALS (ttr))
4084 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4085 == TYPE_QUALS (ttl))))
4086 break;
4088 /* Keep looking for a better type, but remember this one. */
4089 if (!marginal_memb)
4090 marginal_memb = memb;
4094 /* Can convert integer zero to any pointer type. */
4095 if (null_pointer_constant_p (rhs))
4097 rhs = null_pointer_node;
4098 break;
4102 if (memb || marginal_memb)
4104 if (!memb)
4106 /* We have only a marginally acceptable member type;
4107 it needs a warning. */
4108 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4109 tree ttr = TREE_TYPE (rhstype);
4111 /* Const and volatile mean something different for function
4112 types, so the usual warnings are not appropriate. */
4113 if (TREE_CODE (ttr) == FUNCTION_TYPE
4114 && TREE_CODE (ttl) == FUNCTION_TYPE)
4116 /* Because const and volatile on functions are
4117 restrictions that say the function will not do
4118 certain things, it is okay to use a const or volatile
4119 function where an ordinary one is wanted, but not
4120 vice-versa. */
4121 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4122 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
4123 "makes qualified function "
4124 "pointer from unqualified"),
4125 G_("assignment makes qualified "
4126 "function pointer from "
4127 "unqualified"),
4128 G_("initialization makes qualified "
4129 "function pointer from "
4130 "unqualified"),
4131 G_("return makes qualified function "
4132 "pointer from unqualified"));
4134 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4135 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4136 "qualifiers from pointer target type"),
4137 G_("assignment discards qualifiers "
4138 "from pointer target type"),
4139 G_("initialization discards qualifiers "
4140 "from pointer target type"),
4141 G_("return discards qualifiers from "
4142 "pointer target type"));
4144 memb = marginal_memb;
4147 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4148 pedwarn ("ISO C prohibits argument conversion to union type");
4150 rhs = fold_convert (TREE_TYPE (memb), rhs);
4151 return build_constructor_single (type, memb, rhs);
4155 /* Conversions among pointers */
4156 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4157 && (coder == codel))
4159 tree ttl = TREE_TYPE (type);
4160 tree ttr = TREE_TYPE (rhstype);
4161 tree mvl = ttl;
4162 tree mvr = ttr;
4163 bool is_opaque_pointer;
4164 int target_cmp = 0; /* Cache comp_target_types () result. */
4166 if (TREE_CODE (mvl) != ARRAY_TYPE)
4167 mvl = TYPE_MAIN_VARIANT (mvl);
4168 if (TREE_CODE (mvr) != ARRAY_TYPE)
4169 mvr = TYPE_MAIN_VARIANT (mvr);
4170 /* Opaque pointers are treated like void pointers. */
4171 is_opaque_pointer = (targetm.vector_opaque_p (type)
4172 || targetm.vector_opaque_p (rhstype))
4173 && TREE_CODE (ttl) == VECTOR_TYPE
4174 && TREE_CODE (ttr) == VECTOR_TYPE;
4176 /* C++ does not allow the implicit conversion void* -> T*. However,
4177 for the purpose of reducing the number of false positives, we
4178 tolerate the special case of
4180 int *p = NULL;
4182 where NULL is typically defined in C to be '(void *) 0'. */
4183 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4184 warning (OPT_Wc___compat, "request for implicit conversion from "
4185 "%qT to %qT not permitted in C++", rhstype, type);
4187 /* Check if the right-hand side has a format attribute but the
4188 left-hand side doesn't. */
4189 if (warn_missing_format_attribute
4190 && check_missing_format_attribute (type, rhstype))
4192 switch (errtype)
4194 case ic_argpass:
4195 case ic_argpass_nonproto:
4196 warning (OPT_Wmissing_format_attribute,
4197 "argument %d of %qE might be "
4198 "a candidate for a format attribute",
4199 parmnum, rname);
4200 break;
4201 case ic_assign:
4202 warning (OPT_Wmissing_format_attribute,
4203 "assignment left-hand side might be "
4204 "a candidate for a format attribute");
4205 break;
4206 case ic_init:
4207 warning (OPT_Wmissing_format_attribute,
4208 "initialization left-hand side might be "
4209 "a candidate for a format attribute");
4210 break;
4211 case ic_return:
4212 warning (OPT_Wmissing_format_attribute,
4213 "return type might be "
4214 "a candidate for a format attribute");
4215 break;
4216 default:
4217 gcc_unreachable ();
4221 /* Any non-function converts to a [const][volatile] void *
4222 and vice versa; otherwise, targets must be the same.
4223 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4224 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4225 || (target_cmp = comp_target_types (type, rhstype))
4226 || is_opaque_pointer
4227 || (c_common_unsigned_type (mvl)
4228 == c_common_unsigned_type (mvr)))
4230 if (pedantic
4231 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4233 (VOID_TYPE_P (ttr)
4234 && !null_pointer_constant_p (rhs)
4235 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4236 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4237 "%qE between function pointer "
4238 "and %<void *%>"),
4239 G_("ISO C forbids assignment between "
4240 "function pointer and %<void *%>"),
4241 G_("ISO C forbids initialization between "
4242 "function pointer and %<void *%>"),
4243 G_("ISO C forbids return between function "
4244 "pointer and %<void *%>"));
4245 /* Const and volatile mean something different for function types,
4246 so the usual warnings are not appropriate. */
4247 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4248 && TREE_CODE (ttl) != FUNCTION_TYPE)
4250 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4252 /* Types differing only by the presence of the 'volatile'
4253 qualifier are acceptable if the 'volatile' has been added
4254 in by the Objective-C EH machinery. */
4255 if (!objc_type_quals_match (ttl, ttr))
4256 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4257 "qualifiers from pointer target type"),
4258 G_("assignment discards qualifiers "
4259 "from pointer target type"),
4260 G_("initialization discards qualifiers "
4261 "from pointer target type"),
4262 G_("return discards qualifiers from "
4263 "pointer target type"));
4265 /* If this is not a case of ignoring a mismatch in signedness,
4266 no warning. */
4267 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4268 || target_cmp)
4270 /* If there is a mismatch, do warn. */
4271 else if (warn_pointer_sign)
4272 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4273 "%d of %qE differ in signedness"),
4274 G_("pointer targets in assignment "
4275 "differ in signedness"),
4276 G_("pointer targets in initialization "
4277 "differ in signedness"),
4278 G_("pointer targets in return differ "
4279 "in signedness"));
4281 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4282 && TREE_CODE (ttr) == FUNCTION_TYPE)
4284 /* Because const and volatile on functions are restrictions
4285 that say the function will not do certain things,
4286 it is okay to use a const or volatile function
4287 where an ordinary one is wanted, but not vice-versa. */
4288 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4289 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4290 "qualified function pointer "
4291 "from unqualified"),
4292 G_("assignment makes qualified function "
4293 "pointer from unqualified"),
4294 G_("initialization makes qualified "
4295 "function pointer from unqualified"),
4296 G_("return makes qualified function "
4297 "pointer from unqualified"));
4300 else
4301 /* Avoid warning about the volatile ObjC EH puts on decls. */
4302 if (!objc_ok)
4303 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4304 "incompatible pointer type"),
4305 G_("assignment from incompatible pointer type"),
4306 G_("initialization from incompatible "
4307 "pointer type"),
4308 G_("return from incompatible pointer type"));
4310 return convert (type, rhs);
4312 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4314 /* ??? This should not be an error when inlining calls to
4315 unprototyped functions. */
4316 error ("invalid use of non-lvalue array");
4317 return error_mark_node;
4319 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4321 /* An explicit constant 0 can convert to a pointer,
4322 or one that results from arithmetic, even including
4323 a cast to integer type. */
4324 if (!null_pointer_constant_p (rhs))
4325 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4326 "pointer from integer without a cast"),
4327 G_("assignment makes pointer from integer "
4328 "without a cast"),
4329 G_("initialization makes pointer from "
4330 "integer without a cast"),
4331 G_("return makes pointer from integer "
4332 "without a cast"));
4334 return convert (type, rhs);
4336 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4338 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4339 "from pointer without a cast"),
4340 G_("assignment makes integer from pointer "
4341 "without a cast"),
4342 G_("initialization makes integer from pointer "
4343 "without a cast"),
4344 G_("return makes integer from pointer "
4345 "without a cast"));
4346 return convert (type, rhs);
4348 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4349 return convert (type, rhs);
4351 switch (errtype)
4353 case ic_argpass:
4354 case ic_argpass_nonproto:
4355 /* ??? This should not be an error when inlining calls to
4356 unprototyped functions. */
4357 error ("incompatible type for argument %d of %qE", parmnum, rname);
4358 break;
4359 case ic_assign:
4360 error ("incompatible types in assignment");
4361 break;
4362 case ic_init:
4363 error ("incompatible types in initialization");
4364 break;
4365 case ic_return:
4366 error ("incompatible types in return");
4367 break;
4368 default:
4369 gcc_unreachable ();
4372 return error_mark_node;
4375 /* If VALUE is a compound expr all of whose expressions are constant, then
4376 return its value. Otherwise, return error_mark_node.
4378 This is for handling COMPOUND_EXPRs as initializer elements
4379 which is allowed with a warning when -pedantic is specified. */
4381 static tree
4382 valid_compound_expr_initializer (tree value, tree endtype)
4384 if (TREE_CODE (value) == COMPOUND_EXPR)
4386 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4387 == error_mark_node)
4388 return error_mark_node;
4389 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4390 endtype);
4392 else if (!initializer_constant_valid_p (value, endtype))
4393 return error_mark_node;
4394 else
4395 return value;
4398 /* Perform appropriate conversions on the initial value of a variable,
4399 store it in the declaration DECL,
4400 and print any error messages that are appropriate.
4401 If the init is invalid, store an ERROR_MARK. */
4403 void
4404 store_init_value (tree decl, tree init)
4406 tree value, type;
4408 /* If variable's type was invalidly declared, just ignore it. */
4410 type = TREE_TYPE (decl);
4411 if (TREE_CODE (type) == ERROR_MARK)
4412 return;
4414 /* Digest the specified initializer into an expression. */
4416 value = digest_init (type, init, true, TREE_STATIC (decl));
4418 /* Store the expression if valid; else report error. */
4420 if (!in_system_header
4421 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4422 warning (OPT_Wtraditional, "traditional C rejects automatic "
4423 "aggregate initialization");
4425 DECL_INITIAL (decl) = value;
4427 /* ANSI wants warnings about out-of-range constant initializers. */
4428 STRIP_TYPE_NOPS (value);
4429 if (TREE_STATIC (decl))
4430 constant_expression_warning (value);
4432 /* Check if we need to set array size from compound literal size. */
4433 if (TREE_CODE (type) == ARRAY_TYPE
4434 && TYPE_DOMAIN (type) == 0
4435 && value != error_mark_node)
4437 tree inside_init = init;
4439 STRIP_TYPE_NOPS (inside_init);
4440 inside_init = fold (inside_init);
4442 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4444 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4446 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4448 /* For int foo[] = (int [3]){1}; we need to set array size
4449 now since later on array initializer will be just the
4450 brace enclosed list of the compound literal. */
4451 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4452 TREE_TYPE (decl) = type;
4453 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4454 layout_type (type);
4455 layout_decl (cldecl, 0);
4461 /* Methods for storing and printing names for error messages. */
4463 /* Implement a spelling stack that allows components of a name to be pushed
4464 and popped. Each element on the stack is this structure. */
4466 struct spelling
4468 int kind;
4469 union
4471 unsigned HOST_WIDE_INT i;
4472 const char *s;
4473 } u;
4476 #define SPELLING_STRING 1
4477 #define SPELLING_MEMBER 2
4478 #define SPELLING_BOUNDS 3
4480 static struct spelling *spelling; /* Next stack element (unused). */
4481 static struct spelling *spelling_base; /* Spelling stack base. */
4482 static int spelling_size; /* Size of the spelling stack. */
4484 /* Macros to save and restore the spelling stack around push_... functions.
4485 Alternative to SAVE_SPELLING_STACK. */
4487 #define SPELLING_DEPTH() (spelling - spelling_base)
4488 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4490 /* Push an element on the spelling stack with type KIND and assign VALUE
4491 to MEMBER. */
4493 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4495 int depth = SPELLING_DEPTH (); \
4497 if (depth >= spelling_size) \
4499 spelling_size += 10; \
4500 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4501 spelling_size); \
4502 RESTORE_SPELLING_DEPTH (depth); \
4505 spelling->kind = (KIND); \
4506 spelling->MEMBER = (VALUE); \
4507 spelling++; \
4510 /* Push STRING on the stack. Printed literally. */
4512 static void
4513 push_string (const char *string)
4515 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4518 /* Push a member name on the stack. Printed as '.' STRING. */
4520 static void
4521 push_member_name (tree decl)
4523 const char *const string
4524 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4525 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4528 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4530 static void
4531 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4533 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4536 /* Compute the maximum size in bytes of the printed spelling. */
4538 static int
4539 spelling_length (void)
4541 int size = 0;
4542 struct spelling *p;
4544 for (p = spelling_base; p < spelling; p++)
4546 if (p->kind == SPELLING_BOUNDS)
4547 size += 25;
4548 else
4549 size += strlen (p->u.s) + 1;
4552 return size;
4555 /* Print the spelling to BUFFER and return it. */
4557 static char *
4558 print_spelling (char *buffer)
4560 char *d = buffer;
4561 struct spelling *p;
4563 for (p = spelling_base; p < spelling; p++)
4564 if (p->kind == SPELLING_BOUNDS)
4566 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4567 d += strlen (d);
4569 else
4571 const char *s;
4572 if (p->kind == SPELLING_MEMBER)
4573 *d++ = '.';
4574 for (s = p->u.s; (*d = *s++); d++)
4577 *d++ = '\0';
4578 return buffer;
4581 /* Issue an error message for a bad initializer component.
4582 MSGID identifies the message.
4583 The component name is taken from the spelling stack. */
4585 void
4586 error_init (const char *msgid)
4588 char *ofwhat;
4590 error ("%s", _(msgid));
4591 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4592 if (*ofwhat)
4593 error ("(near initialization for %qs)", ofwhat);
4596 /* Issue a pedantic warning for a bad initializer component.
4597 MSGID identifies the message.
4598 The component name is taken from the spelling stack. */
4600 void
4601 pedwarn_init (const char *msgid)
4603 char *ofwhat;
4605 pedwarn ("%s", _(msgid));
4606 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4607 if (*ofwhat)
4608 pedwarn ("(near initialization for %qs)", ofwhat);
4611 /* Issue a warning for a bad initializer component.
4612 MSGID identifies the message.
4613 The component name is taken from the spelling stack. */
4615 static void
4616 warning_init (const char *msgid)
4618 char *ofwhat;
4620 warning (0, "%s", _(msgid));
4621 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4622 if (*ofwhat)
4623 warning (0, "(near initialization for %qs)", ofwhat);
4626 /* If TYPE is an array type and EXPR is a parenthesized string
4627 constant, warn if pedantic that EXPR is being used to initialize an
4628 object of type TYPE. */
4630 void
4631 maybe_warn_string_init (tree type, struct c_expr expr)
4633 if (pedantic
4634 && TREE_CODE (type) == ARRAY_TYPE
4635 && TREE_CODE (expr.value) == STRING_CST
4636 && expr.original_code != STRING_CST)
4637 pedwarn_init ("array initialized from parenthesized string constant");
4640 /* Digest the parser output INIT as an initializer for type TYPE.
4641 Return a C expression of type TYPE to represent the initial value.
4643 If INIT is a string constant, STRICT_STRING is true if it is
4644 unparenthesized or we should not warn here for it being parenthesized.
4645 For other types of INIT, STRICT_STRING is not used.
4647 REQUIRE_CONSTANT requests an error if non-constant initializers or
4648 elements are seen. */
4650 static tree
4651 digest_init (tree type, tree init, bool strict_string, int require_constant)
4653 enum tree_code code = TREE_CODE (type);
4654 tree inside_init = init;
4656 if (type == error_mark_node
4657 || !init
4658 || init == error_mark_node
4659 || TREE_TYPE (init) == error_mark_node)
4660 return error_mark_node;
4662 STRIP_TYPE_NOPS (inside_init);
4664 inside_init = fold (inside_init);
4666 /* Initialization of an array of chars from a string constant
4667 optionally enclosed in braces. */
4669 if (code == ARRAY_TYPE && inside_init
4670 && TREE_CODE (inside_init) == STRING_CST)
4672 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4673 /* Note that an array could be both an array of character type
4674 and an array of wchar_t if wchar_t is signed char or unsigned
4675 char. */
4676 bool char_array = (typ1 == char_type_node
4677 || typ1 == signed_char_type_node
4678 || typ1 == unsigned_char_type_node);
4679 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4680 if (char_array || wchar_array)
4682 struct c_expr expr;
4683 bool char_string;
4684 expr.value = inside_init;
4685 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4686 maybe_warn_string_init (type, expr);
4688 char_string
4689 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4690 == char_type_node);
4692 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4693 TYPE_MAIN_VARIANT (type)))
4694 return inside_init;
4696 if (!wchar_array && !char_string)
4698 error_init ("char-array initialized from wide string");
4699 return error_mark_node;
4701 if (char_string && !char_array)
4703 error_init ("wchar_t-array initialized from non-wide string");
4704 return error_mark_node;
4707 TREE_TYPE (inside_init) = type;
4708 if (TYPE_DOMAIN (type) != 0
4709 && TYPE_SIZE (type) != 0
4710 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4711 /* Subtract 1 (or sizeof (wchar_t))
4712 because it's ok to ignore the terminating null char
4713 that is counted in the length of the constant. */
4714 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4715 TREE_STRING_LENGTH (inside_init)
4716 - ((TYPE_PRECISION (typ1)
4717 != TYPE_PRECISION (char_type_node))
4718 ? (TYPE_PRECISION (wchar_type_node)
4719 / BITS_PER_UNIT)
4720 : 1)))
4721 pedwarn_init ("initializer-string for array of chars is too long");
4723 return inside_init;
4725 else if (INTEGRAL_TYPE_P (typ1))
4727 error_init ("array of inappropriate type initialized "
4728 "from string constant");
4729 return error_mark_node;
4733 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4734 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4735 below and handle as a constructor. */
4736 if (code == VECTOR_TYPE
4737 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4738 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
4739 && TREE_CONSTANT (inside_init))
4741 if (TREE_CODE (inside_init) == VECTOR_CST
4742 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4743 TYPE_MAIN_VARIANT (type)))
4744 return inside_init;
4746 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4748 unsigned HOST_WIDE_INT ix;
4749 tree value;
4750 bool constant_p = true;
4752 /* Iterate through elements and check if all constructor
4753 elements are *_CSTs. */
4754 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4755 if (!CONSTANT_CLASS_P (value))
4757 constant_p = false;
4758 break;
4761 if (constant_p)
4762 return build_vector_from_ctor (type,
4763 CONSTRUCTOR_ELTS (inside_init));
4767 /* Any type can be initialized
4768 from an expression of the same type, optionally with braces. */
4770 if (inside_init && TREE_TYPE (inside_init) != 0
4771 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4772 TYPE_MAIN_VARIANT (type))
4773 || (code == ARRAY_TYPE
4774 && comptypes (TREE_TYPE (inside_init), type))
4775 || (code == VECTOR_TYPE
4776 && comptypes (TREE_TYPE (inside_init), type))
4777 || (code == POINTER_TYPE
4778 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4779 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4780 TREE_TYPE (type)))))
4782 if (code == POINTER_TYPE)
4784 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4786 if (TREE_CODE (inside_init) == STRING_CST
4787 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4788 inside_init = array_to_pointer_conversion (inside_init);
4789 else
4791 error_init ("invalid use of non-lvalue array");
4792 return error_mark_node;
4797 if (code == VECTOR_TYPE)
4798 /* Although the types are compatible, we may require a
4799 conversion. */
4800 inside_init = convert (type, inside_init);
4802 if (require_constant
4803 && (code == VECTOR_TYPE || !flag_isoc99)
4804 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4806 /* As an extension, allow initializing objects with static storage
4807 duration with compound literals (which are then treated just as
4808 the brace enclosed list they contain). Also allow this for
4809 vectors, as we can only assign them with compound literals. */
4810 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4811 inside_init = DECL_INITIAL (decl);
4814 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4815 && TREE_CODE (inside_init) != CONSTRUCTOR)
4817 error_init ("array initialized from non-constant array expression");
4818 return error_mark_node;
4821 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4822 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4824 /* Compound expressions can only occur here if -pedantic or
4825 -pedantic-errors is specified. In the later case, we always want
4826 an error. In the former case, we simply want a warning. */
4827 if (require_constant && pedantic
4828 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4830 inside_init
4831 = valid_compound_expr_initializer (inside_init,
4832 TREE_TYPE (inside_init));
4833 if (inside_init == error_mark_node)
4834 error_init ("initializer element is not constant");
4835 else
4836 pedwarn_init ("initializer element is not constant");
4837 if (flag_pedantic_errors)
4838 inside_init = error_mark_node;
4840 else if (require_constant
4841 && !initializer_constant_valid_p (inside_init,
4842 TREE_TYPE (inside_init)))
4844 error_init ("initializer element is not constant");
4845 inside_init = error_mark_node;
4848 /* Added to enable additional -Wmissing-format-attribute warnings. */
4849 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4850 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4851 NULL_TREE, 0);
4852 return inside_init;
4855 /* Handle scalar types, including conversions. */
4857 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
4858 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
4859 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
4861 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4862 && (TREE_CODE (init) == STRING_CST
4863 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4864 init = array_to_pointer_conversion (init);
4865 inside_init
4866 = convert_for_assignment (type, init, ic_init,
4867 NULL_TREE, NULL_TREE, 0);
4869 /* Check to see if we have already given an error message. */
4870 if (inside_init == error_mark_node)
4872 else if (require_constant && !TREE_CONSTANT (inside_init))
4874 error_init ("initializer element is not constant");
4875 inside_init = error_mark_node;
4877 else if (require_constant
4878 && !initializer_constant_valid_p (inside_init,
4879 TREE_TYPE (inside_init)))
4881 error_init ("initializer element is not computable at load time");
4882 inside_init = error_mark_node;
4885 return inside_init;
4888 /* Come here only for records and arrays. */
4890 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4892 error_init ("variable-sized object may not be initialized");
4893 return error_mark_node;
4896 error_init ("invalid initializer");
4897 return error_mark_node;
4900 /* Handle initializers that use braces. */
4902 /* Type of object we are accumulating a constructor for.
4903 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4904 static tree constructor_type;
4906 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4907 left to fill. */
4908 static tree constructor_fields;
4910 /* For an ARRAY_TYPE, this is the specified index
4911 at which to store the next element we get. */
4912 static tree constructor_index;
4914 /* For an ARRAY_TYPE, this is the maximum index. */
4915 static tree constructor_max_index;
4917 /* For a RECORD_TYPE, this is the first field not yet written out. */
4918 static tree constructor_unfilled_fields;
4920 /* For an ARRAY_TYPE, this is the index of the first element
4921 not yet written out. */
4922 static tree constructor_unfilled_index;
4924 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4925 This is so we can generate gaps between fields, when appropriate. */
4926 static tree constructor_bit_index;
4928 /* If we are saving up the elements rather than allocating them,
4929 this is the list of elements so far (in reverse order,
4930 most recent first). */
4931 static VEC(constructor_elt,gc) *constructor_elements;
4933 /* 1 if constructor should be incrementally stored into a constructor chain,
4934 0 if all the elements should be kept in AVL tree. */
4935 static int constructor_incremental;
4937 /* 1 if so far this constructor's elements are all compile-time constants. */
4938 static int constructor_constant;
4940 /* 1 if so far this constructor's elements are all valid address constants. */
4941 static int constructor_simple;
4943 /* 1 if this constructor is erroneous so far. */
4944 static int constructor_erroneous;
4946 /* Structure for managing pending initializer elements, organized as an
4947 AVL tree. */
4949 struct init_node
4951 struct init_node *left, *right;
4952 struct init_node *parent;
4953 int balance;
4954 tree purpose;
4955 tree value;
4958 /* Tree of pending elements at this constructor level.
4959 These are elements encountered out of order
4960 which belong at places we haven't reached yet in actually
4961 writing the output.
4962 Will never hold tree nodes across GC runs. */
4963 static struct init_node *constructor_pending_elts;
4965 /* The SPELLING_DEPTH of this constructor. */
4966 static int constructor_depth;
4968 /* DECL node for which an initializer is being read.
4969 0 means we are reading a constructor expression
4970 such as (struct foo) {...}. */
4971 static tree constructor_decl;
4973 /* Nonzero if this is an initializer for a top-level decl. */
4974 static int constructor_top_level;
4976 /* Nonzero if there were any member designators in this initializer. */
4977 static int constructor_designated;
4979 /* Nesting depth of designator list. */
4980 static int designator_depth;
4982 /* Nonzero if there were diagnosed errors in this designator list. */
4983 static int designator_erroneous;
4986 /* This stack has a level for each implicit or explicit level of
4987 structuring in the initializer, including the outermost one. It
4988 saves the values of most of the variables above. */
4990 struct constructor_range_stack;
4992 struct constructor_stack
4994 struct constructor_stack *next;
4995 tree type;
4996 tree fields;
4997 tree index;
4998 tree max_index;
4999 tree unfilled_index;
5000 tree unfilled_fields;
5001 tree bit_index;
5002 VEC(constructor_elt,gc) *elements;
5003 struct init_node *pending_elts;
5004 int offset;
5005 int depth;
5006 /* If value nonzero, this value should replace the entire
5007 constructor at this level. */
5008 struct c_expr replacement_value;
5009 struct constructor_range_stack *range_stack;
5010 char constant;
5011 char simple;
5012 char implicit;
5013 char erroneous;
5014 char outer;
5015 char incremental;
5016 char designated;
5019 static struct constructor_stack *constructor_stack;
5021 /* This stack represents designators from some range designator up to
5022 the last designator in the list. */
5024 struct constructor_range_stack
5026 struct constructor_range_stack *next, *prev;
5027 struct constructor_stack *stack;
5028 tree range_start;
5029 tree index;
5030 tree range_end;
5031 tree fields;
5034 static struct constructor_range_stack *constructor_range_stack;
5036 /* This stack records separate initializers that are nested.
5037 Nested initializers can't happen in ANSI C, but GNU C allows them
5038 in cases like { ... (struct foo) { ... } ... }. */
5040 struct initializer_stack
5042 struct initializer_stack *next;
5043 tree decl;
5044 struct constructor_stack *constructor_stack;
5045 struct constructor_range_stack *constructor_range_stack;
5046 VEC(constructor_elt,gc) *elements;
5047 struct spelling *spelling;
5048 struct spelling *spelling_base;
5049 int spelling_size;
5050 char top_level;
5051 char require_constant_value;
5052 char require_constant_elements;
5055 static struct initializer_stack *initializer_stack;
5057 /* Prepare to parse and output the initializer for variable DECL. */
5059 void
5060 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5062 const char *locus;
5063 struct initializer_stack *p = XNEW (struct initializer_stack);
5065 p->decl = constructor_decl;
5066 p->require_constant_value = require_constant_value;
5067 p->require_constant_elements = require_constant_elements;
5068 p->constructor_stack = constructor_stack;
5069 p->constructor_range_stack = constructor_range_stack;
5070 p->elements = constructor_elements;
5071 p->spelling = spelling;
5072 p->spelling_base = spelling_base;
5073 p->spelling_size = spelling_size;
5074 p->top_level = constructor_top_level;
5075 p->next = initializer_stack;
5076 initializer_stack = p;
5078 constructor_decl = decl;
5079 constructor_designated = 0;
5080 constructor_top_level = top_level;
5082 if (decl != 0 && decl != error_mark_node)
5084 require_constant_value = TREE_STATIC (decl);
5085 require_constant_elements
5086 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5087 /* For a scalar, you can always use any value to initialize,
5088 even within braces. */
5089 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5090 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5091 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5092 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5093 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5095 else
5097 require_constant_value = 0;
5098 require_constant_elements = 0;
5099 locus = "(anonymous)";
5102 constructor_stack = 0;
5103 constructor_range_stack = 0;
5105 missing_braces_mentioned = 0;
5107 spelling_base = 0;
5108 spelling_size = 0;
5109 RESTORE_SPELLING_DEPTH (0);
5111 if (locus)
5112 push_string (locus);
5115 void
5116 finish_init (void)
5118 struct initializer_stack *p = initializer_stack;
5120 /* Free the whole constructor stack of this initializer. */
5121 while (constructor_stack)
5123 struct constructor_stack *q = constructor_stack;
5124 constructor_stack = q->next;
5125 free (q);
5128 gcc_assert (!constructor_range_stack);
5130 /* Pop back to the data of the outer initializer (if any). */
5131 free (spelling_base);
5133 constructor_decl = p->decl;
5134 require_constant_value = p->require_constant_value;
5135 require_constant_elements = p->require_constant_elements;
5136 constructor_stack = p->constructor_stack;
5137 constructor_range_stack = p->constructor_range_stack;
5138 constructor_elements = p->elements;
5139 spelling = p->spelling;
5140 spelling_base = p->spelling_base;
5141 spelling_size = p->spelling_size;
5142 constructor_top_level = p->top_level;
5143 initializer_stack = p->next;
5144 free (p);
5147 /* Call here when we see the initializer is surrounded by braces.
5148 This is instead of a call to push_init_level;
5149 it is matched by a call to pop_init_level.
5151 TYPE is the type to initialize, for a constructor expression.
5152 For an initializer for a decl, TYPE is zero. */
5154 void
5155 really_start_incremental_init (tree type)
5157 struct constructor_stack *p = XNEW (struct constructor_stack);
5159 if (type == 0)
5160 type = TREE_TYPE (constructor_decl);
5162 if (targetm.vector_opaque_p (type))
5163 error ("opaque vector types cannot be initialized");
5165 p->type = constructor_type;
5166 p->fields = constructor_fields;
5167 p->index = constructor_index;
5168 p->max_index = constructor_max_index;
5169 p->unfilled_index = constructor_unfilled_index;
5170 p->unfilled_fields = constructor_unfilled_fields;
5171 p->bit_index = constructor_bit_index;
5172 p->elements = constructor_elements;
5173 p->constant = constructor_constant;
5174 p->simple = constructor_simple;
5175 p->erroneous = constructor_erroneous;
5176 p->pending_elts = constructor_pending_elts;
5177 p->depth = constructor_depth;
5178 p->replacement_value.value = 0;
5179 p->replacement_value.original_code = ERROR_MARK;
5180 p->implicit = 0;
5181 p->range_stack = 0;
5182 p->outer = 0;
5183 p->incremental = constructor_incremental;
5184 p->designated = constructor_designated;
5185 p->next = 0;
5186 constructor_stack = p;
5188 constructor_constant = 1;
5189 constructor_simple = 1;
5190 constructor_depth = SPELLING_DEPTH ();
5191 constructor_elements = 0;
5192 constructor_pending_elts = 0;
5193 constructor_type = type;
5194 constructor_incremental = 1;
5195 constructor_designated = 0;
5196 designator_depth = 0;
5197 designator_erroneous = 0;
5199 if (TREE_CODE (constructor_type) == RECORD_TYPE
5200 || TREE_CODE (constructor_type) == UNION_TYPE)
5202 constructor_fields = TYPE_FIELDS (constructor_type);
5203 /* Skip any nameless bit fields at the beginning. */
5204 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5205 && DECL_NAME (constructor_fields) == 0)
5206 constructor_fields = TREE_CHAIN (constructor_fields);
5208 constructor_unfilled_fields = constructor_fields;
5209 constructor_bit_index = bitsize_zero_node;
5211 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5213 if (TYPE_DOMAIN (constructor_type))
5215 constructor_max_index
5216 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5218 /* Detect non-empty initializations of zero-length arrays. */
5219 if (constructor_max_index == NULL_TREE
5220 && TYPE_SIZE (constructor_type))
5221 constructor_max_index = build_int_cst (NULL_TREE, -1);
5223 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5224 to initialize VLAs will cause a proper error; avoid tree
5225 checking errors as well by setting a safe value. */
5226 if (constructor_max_index
5227 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5228 constructor_max_index = build_int_cst (NULL_TREE, -1);
5230 constructor_index
5231 = convert (bitsizetype,
5232 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5234 else
5236 constructor_index = bitsize_zero_node;
5237 constructor_max_index = NULL_TREE;
5240 constructor_unfilled_index = constructor_index;
5242 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5244 /* Vectors are like simple fixed-size arrays. */
5245 constructor_max_index =
5246 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5247 constructor_index = bitsize_zero_node;
5248 constructor_unfilled_index = constructor_index;
5250 else
5252 /* Handle the case of int x = {5}; */
5253 constructor_fields = constructor_type;
5254 constructor_unfilled_fields = constructor_type;
5258 /* Push down into a subobject, for initialization.
5259 If this is for an explicit set of braces, IMPLICIT is 0.
5260 If it is because the next element belongs at a lower level,
5261 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5263 void
5264 push_init_level (int implicit)
5266 struct constructor_stack *p;
5267 tree value = NULL_TREE;
5269 /* If we've exhausted any levels that didn't have braces,
5270 pop them now. If implicit == 1, this will have been done in
5271 process_init_element; do not repeat it here because in the case
5272 of excess initializers for an empty aggregate this leads to an
5273 infinite cycle of popping a level and immediately recreating
5274 it. */
5275 if (implicit != 1)
5277 while (constructor_stack->implicit)
5279 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5280 || TREE_CODE (constructor_type) == UNION_TYPE)
5281 && constructor_fields == 0)
5282 process_init_element (pop_init_level (1));
5283 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5284 && constructor_max_index
5285 && tree_int_cst_lt (constructor_max_index,
5286 constructor_index))
5287 process_init_element (pop_init_level (1));
5288 else
5289 break;
5293 /* Unless this is an explicit brace, we need to preserve previous
5294 content if any. */
5295 if (implicit)
5297 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5298 || TREE_CODE (constructor_type) == UNION_TYPE)
5299 && constructor_fields)
5300 value = find_init_member (constructor_fields);
5301 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5302 value = find_init_member (constructor_index);
5305 p = XNEW (struct constructor_stack);
5306 p->type = constructor_type;
5307 p->fields = constructor_fields;
5308 p->index = constructor_index;
5309 p->max_index = constructor_max_index;
5310 p->unfilled_index = constructor_unfilled_index;
5311 p->unfilled_fields = constructor_unfilled_fields;
5312 p->bit_index = constructor_bit_index;
5313 p->elements = constructor_elements;
5314 p->constant = constructor_constant;
5315 p->simple = constructor_simple;
5316 p->erroneous = constructor_erroneous;
5317 p->pending_elts = constructor_pending_elts;
5318 p->depth = constructor_depth;
5319 p->replacement_value.value = 0;
5320 p->replacement_value.original_code = ERROR_MARK;
5321 p->implicit = implicit;
5322 p->outer = 0;
5323 p->incremental = constructor_incremental;
5324 p->designated = constructor_designated;
5325 p->next = constructor_stack;
5326 p->range_stack = 0;
5327 constructor_stack = p;
5329 constructor_constant = 1;
5330 constructor_simple = 1;
5331 constructor_depth = SPELLING_DEPTH ();
5332 constructor_elements = 0;
5333 constructor_incremental = 1;
5334 constructor_designated = 0;
5335 constructor_pending_elts = 0;
5336 if (!implicit)
5338 p->range_stack = constructor_range_stack;
5339 constructor_range_stack = 0;
5340 designator_depth = 0;
5341 designator_erroneous = 0;
5344 /* Don't die if an entire brace-pair level is superfluous
5345 in the containing level. */
5346 if (constructor_type == 0)
5348 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5349 || TREE_CODE (constructor_type) == UNION_TYPE)
5351 /* Don't die if there are extra init elts at the end. */
5352 if (constructor_fields == 0)
5353 constructor_type = 0;
5354 else
5356 constructor_type = TREE_TYPE (constructor_fields);
5357 push_member_name (constructor_fields);
5358 constructor_depth++;
5361 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5363 constructor_type = TREE_TYPE (constructor_type);
5364 push_array_bounds (tree_low_cst (constructor_index, 1));
5365 constructor_depth++;
5368 if (constructor_type == 0)
5370 error_init ("extra brace group at end of initializer");
5371 constructor_fields = 0;
5372 constructor_unfilled_fields = 0;
5373 return;
5376 if (value && TREE_CODE (value) == CONSTRUCTOR)
5378 constructor_constant = TREE_CONSTANT (value);
5379 constructor_simple = TREE_STATIC (value);
5380 constructor_elements = CONSTRUCTOR_ELTS (value);
5381 if (!VEC_empty (constructor_elt, constructor_elements)
5382 && (TREE_CODE (constructor_type) == RECORD_TYPE
5383 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5384 set_nonincremental_init ();
5387 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5389 missing_braces_mentioned = 1;
5390 warning_init ("missing braces around initializer");
5393 if (TREE_CODE (constructor_type) == RECORD_TYPE
5394 || TREE_CODE (constructor_type) == UNION_TYPE)
5396 constructor_fields = TYPE_FIELDS (constructor_type);
5397 /* Skip any nameless bit fields at the beginning. */
5398 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5399 && DECL_NAME (constructor_fields) == 0)
5400 constructor_fields = TREE_CHAIN (constructor_fields);
5402 constructor_unfilled_fields = constructor_fields;
5403 constructor_bit_index = bitsize_zero_node;
5405 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5407 /* Vectors are like simple fixed-size arrays. */
5408 constructor_max_index =
5409 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5410 constructor_index = convert (bitsizetype, integer_zero_node);
5411 constructor_unfilled_index = constructor_index;
5413 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5415 if (TYPE_DOMAIN (constructor_type))
5417 constructor_max_index
5418 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5420 /* Detect non-empty initializations of zero-length arrays. */
5421 if (constructor_max_index == NULL_TREE
5422 && TYPE_SIZE (constructor_type))
5423 constructor_max_index = build_int_cst (NULL_TREE, -1);
5425 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5426 to initialize VLAs will cause a proper error; avoid tree
5427 checking errors as well by setting a safe value. */
5428 if (constructor_max_index
5429 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5430 constructor_max_index = build_int_cst (NULL_TREE, -1);
5432 constructor_index
5433 = convert (bitsizetype,
5434 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5436 else
5437 constructor_index = bitsize_zero_node;
5439 constructor_unfilled_index = constructor_index;
5440 if (value && TREE_CODE (value) == STRING_CST)
5442 /* We need to split the char/wchar array into individual
5443 characters, so that we don't have to special case it
5444 everywhere. */
5445 set_nonincremental_init_from_string (value);
5448 else
5450 if (constructor_type != error_mark_node)
5451 warning_init ("braces around scalar initializer");
5452 constructor_fields = constructor_type;
5453 constructor_unfilled_fields = constructor_type;
5457 /* At the end of an implicit or explicit brace level,
5458 finish up that level of constructor. If a single expression
5459 with redundant braces initialized that level, return the
5460 c_expr structure for that expression. Otherwise, the original_code
5461 element is set to ERROR_MARK.
5462 If we were outputting the elements as they are read, return 0 as the value
5463 from inner levels (process_init_element ignores that),
5464 but return error_mark_node as the value from the outermost level
5465 (that's what we want to put in DECL_INITIAL).
5466 Otherwise, return a CONSTRUCTOR expression as the value. */
5468 struct c_expr
5469 pop_init_level (int implicit)
5471 struct constructor_stack *p;
5472 struct c_expr ret;
5473 ret.value = 0;
5474 ret.original_code = ERROR_MARK;
5476 if (implicit == 0)
5478 /* When we come to an explicit close brace,
5479 pop any inner levels that didn't have explicit braces. */
5480 while (constructor_stack->implicit)
5481 process_init_element (pop_init_level (1));
5483 gcc_assert (!constructor_range_stack);
5486 /* Now output all pending elements. */
5487 constructor_incremental = 1;
5488 output_pending_init_elements (1);
5490 p = constructor_stack;
5492 /* Error for initializing a flexible array member, or a zero-length
5493 array member in an inappropriate context. */
5494 if (constructor_type && constructor_fields
5495 && TREE_CODE (constructor_type) == ARRAY_TYPE
5496 && TYPE_DOMAIN (constructor_type)
5497 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5499 /* Silently discard empty initializations. The parser will
5500 already have pedwarned for empty brackets. */
5501 if (integer_zerop (constructor_unfilled_index))
5502 constructor_type = NULL_TREE;
5503 else
5505 gcc_assert (!TYPE_SIZE (constructor_type));
5507 if (constructor_depth > 2)
5508 error_init ("initialization of flexible array member in a nested context");
5509 else if (pedantic)
5510 pedwarn_init ("initialization of a flexible array member");
5512 /* We have already issued an error message for the existence
5513 of a flexible array member not at the end of the structure.
5514 Discard the initializer so that we do not die later. */
5515 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5516 constructor_type = NULL_TREE;
5520 /* Warn when some struct elements are implicitly initialized to zero. */
5521 if (warn_missing_field_initializers
5522 && constructor_type
5523 && TREE_CODE (constructor_type) == RECORD_TYPE
5524 && constructor_unfilled_fields)
5526 /* Do not warn for flexible array members or zero-length arrays. */
5527 while (constructor_unfilled_fields
5528 && (!DECL_SIZE (constructor_unfilled_fields)
5529 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5530 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5532 /* Do not warn if this level of the initializer uses member
5533 designators; it is likely to be deliberate. */
5534 if (constructor_unfilled_fields && !constructor_designated)
5536 push_member_name (constructor_unfilled_fields);
5537 warning_init ("missing initializer");
5538 RESTORE_SPELLING_DEPTH (constructor_depth);
5542 /* Pad out the end of the structure. */
5543 if (p->replacement_value.value)
5544 /* If this closes a superfluous brace pair,
5545 just pass out the element between them. */
5546 ret = p->replacement_value;
5547 else if (constructor_type == 0)
5549 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5550 && TREE_CODE (constructor_type) != UNION_TYPE
5551 && TREE_CODE (constructor_type) != ARRAY_TYPE
5552 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5554 /* A nonincremental scalar initializer--just return
5555 the element, after verifying there is just one. */
5556 if (VEC_empty (constructor_elt,constructor_elements))
5558 if (!constructor_erroneous)
5559 error_init ("empty scalar initializer");
5560 ret.value = error_mark_node;
5562 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5564 error_init ("extra elements in scalar initializer");
5565 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5567 else
5568 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5570 else
5572 if (constructor_erroneous)
5573 ret.value = error_mark_node;
5574 else
5576 ret.value = build_constructor (constructor_type,
5577 constructor_elements);
5578 if (constructor_constant)
5579 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5580 if (constructor_constant && constructor_simple)
5581 TREE_STATIC (ret.value) = 1;
5585 constructor_type = p->type;
5586 constructor_fields = p->fields;
5587 constructor_index = p->index;
5588 constructor_max_index = p->max_index;
5589 constructor_unfilled_index = p->unfilled_index;
5590 constructor_unfilled_fields = p->unfilled_fields;
5591 constructor_bit_index = p->bit_index;
5592 constructor_elements = p->elements;
5593 constructor_constant = p->constant;
5594 constructor_simple = p->simple;
5595 constructor_erroneous = p->erroneous;
5596 constructor_incremental = p->incremental;
5597 constructor_designated = p->designated;
5598 constructor_pending_elts = p->pending_elts;
5599 constructor_depth = p->depth;
5600 if (!p->implicit)
5601 constructor_range_stack = p->range_stack;
5602 RESTORE_SPELLING_DEPTH (constructor_depth);
5604 constructor_stack = p->next;
5605 free (p);
5607 if (ret.value == 0 && constructor_stack == 0)
5608 ret.value = error_mark_node;
5609 return ret;
5612 /* Common handling for both array range and field name designators.
5613 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5615 static int
5616 set_designator (int array)
5618 tree subtype;
5619 enum tree_code subcode;
5621 /* Don't die if an entire brace-pair level is superfluous
5622 in the containing level. */
5623 if (constructor_type == 0)
5624 return 1;
5626 /* If there were errors in this designator list already, bail out
5627 silently. */
5628 if (designator_erroneous)
5629 return 1;
5631 if (!designator_depth)
5633 gcc_assert (!constructor_range_stack);
5635 /* Designator list starts at the level of closest explicit
5636 braces. */
5637 while (constructor_stack->implicit)
5638 process_init_element (pop_init_level (1));
5639 constructor_designated = 1;
5640 return 0;
5643 switch (TREE_CODE (constructor_type))
5645 case RECORD_TYPE:
5646 case UNION_TYPE:
5647 subtype = TREE_TYPE (constructor_fields);
5648 if (subtype != error_mark_node)
5649 subtype = TYPE_MAIN_VARIANT (subtype);
5650 break;
5651 case ARRAY_TYPE:
5652 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5653 break;
5654 default:
5655 gcc_unreachable ();
5658 subcode = TREE_CODE (subtype);
5659 if (array && subcode != ARRAY_TYPE)
5661 error_init ("array index in non-array initializer");
5662 return 1;
5664 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5666 error_init ("field name not in record or union initializer");
5667 return 1;
5670 constructor_designated = 1;
5671 push_init_level (2);
5672 return 0;
5675 /* If there are range designators in designator list, push a new designator
5676 to constructor_range_stack. RANGE_END is end of such stack range or
5677 NULL_TREE if there is no range designator at this level. */
5679 static void
5680 push_range_stack (tree range_end)
5682 struct constructor_range_stack *p;
5684 p = GGC_NEW (struct constructor_range_stack);
5685 p->prev = constructor_range_stack;
5686 p->next = 0;
5687 p->fields = constructor_fields;
5688 p->range_start = constructor_index;
5689 p->index = constructor_index;
5690 p->stack = constructor_stack;
5691 p->range_end = range_end;
5692 if (constructor_range_stack)
5693 constructor_range_stack->next = p;
5694 constructor_range_stack = p;
5697 /* Within an array initializer, specify the next index to be initialized.
5698 FIRST is that index. If LAST is nonzero, then initialize a range
5699 of indices, running from FIRST through LAST. */
5701 void
5702 set_init_index (tree first, tree last)
5704 if (set_designator (1))
5705 return;
5707 designator_erroneous = 1;
5709 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5710 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5712 error_init ("array index in initializer not of integer type");
5713 return;
5716 if (TREE_CODE (first) != INTEGER_CST)
5717 error_init ("nonconstant array index in initializer");
5718 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5719 error_init ("nonconstant array index in initializer");
5720 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5721 error_init ("array index in non-array initializer");
5722 else if (tree_int_cst_sgn (first) == -1)
5723 error_init ("array index in initializer exceeds array bounds");
5724 else if (constructor_max_index
5725 && tree_int_cst_lt (constructor_max_index, first))
5726 error_init ("array index in initializer exceeds array bounds");
5727 else
5729 constructor_index = convert (bitsizetype, first);
5731 if (last)
5733 if (tree_int_cst_equal (first, last))
5734 last = 0;
5735 else if (tree_int_cst_lt (last, first))
5737 error_init ("empty index range in initializer");
5738 last = 0;
5740 else
5742 last = convert (bitsizetype, last);
5743 if (constructor_max_index != 0
5744 && tree_int_cst_lt (constructor_max_index, last))
5746 error_init ("array index range in initializer exceeds array bounds");
5747 last = 0;
5752 designator_depth++;
5753 designator_erroneous = 0;
5754 if (constructor_range_stack || last)
5755 push_range_stack (last);
5759 /* Within a struct initializer, specify the next field to be initialized. */
5761 void
5762 set_init_label (tree fieldname)
5764 tree tail;
5766 if (set_designator (0))
5767 return;
5769 designator_erroneous = 1;
5771 if (TREE_CODE (constructor_type) != RECORD_TYPE
5772 && TREE_CODE (constructor_type) != UNION_TYPE)
5774 error_init ("field name not in record or union initializer");
5775 return;
5778 for (tail = TYPE_FIELDS (constructor_type); tail;
5779 tail = TREE_CHAIN (tail))
5781 if (DECL_NAME (tail) == fieldname)
5782 break;
5785 if (tail == 0)
5786 error ("unknown field %qE specified in initializer", fieldname);
5787 else
5789 constructor_fields = tail;
5790 designator_depth++;
5791 designator_erroneous = 0;
5792 if (constructor_range_stack)
5793 push_range_stack (NULL_TREE);
5797 /* Add a new initializer to the tree of pending initializers. PURPOSE
5798 identifies the initializer, either array index or field in a structure.
5799 VALUE is the value of that index or field. */
5801 static void
5802 add_pending_init (tree purpose, tree value)
5804 struct init_node *p, **q, *r;
5806 q = &constructor_pending_elts;
5807 p = 0;
5809 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5811 while (*q != 0)
5813 p = *q;
5814 if (tree_int_cst_lt (purpose, p->purpose))
5815 q = &p->left;
5816 else if (tree_int_cst_lt (p->purpose, purpose))
5817 q = &p->right;
5818 else
5820 if (TREE_SIDE_EFFECTS (p->value))
5821 warning_init ("initialized field with side-effects overwritten");
5822 else if (warn_override_init)
5823 warning_init ("initialized field overwritten");
5824 p->value = value;
5825 return;
5829 else
5831 tree bitpos;
5833 bitpos = bit_position (purpose);
5834 while (*q != NULL)
5836 p = *q;
5837 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5838 q = &p->left;
5839 else if (p->purpose != purpose)
5840 q = &p->right;
5841 else
5843 if (TREE_SIDE_EFFECTS (p->value))
5844 warning_init ("initialized field with side-effects overwritten");
5845 else if (warn_override_init)
5846 warning_init ("initialized field overwritten");
5847 p->value = value;
5848 return;
5853 r = GGC_NEW (struct init_node);
5854 r->purpose = purpose;
5855 r->value = value;
5857 *q = r;
5858 r->parent = p;
5859 r->left = 0;
5860 r->right = 0;
5861 r->balance = 0;
5863 while (p)
5865 struct init_node *s;
5867 if (r == p->left)
5869 if (p->balance == 0)
5870 p->balance = -1;
5871 else if (p->balance < 0)
5873 if (r->balance < 0)
5875 /* L rotation. */
5876 p->left = r->right;
5877 if (p->left)
5878 p->left->parent = p;
5879 r->right = p;
5881 p->balance = 0;
5882 r->balance = 0;
5884 s = p->parent;
5885 p->parent = r;
5886 r->parent = s;
5887 if (s)
5889 if (s->left == p)
5890 s->left = r;
5891 else
5892 s->right = r;
5894 else
5895 constructor_pending_elts = r;
5897 else
5899 /* LR rotation. */
5900 struct init_node *t = r->right;
5902 r->right = t->left;
5903 if (r->right)
5904 r->right->parent = r;
5905 t->left = r;
5907 p->left = t->right;
5908 if (p->left)
5909 p->left->parent = p;
5910 t->right = p;
5912 p->balance = t->balance < 0;
5913 r->balance = -(t->balance > 0);
5914 t->balance = 0;
5916 s = p->parent;
5917 p->parent = t;
5918 r->parent = t;
5919 t->parent = s;
5920 if (s)
5922 if (s->left == p)
5923 s->left = t;
5924 else
5925 s->right = t;
5927 else
5928 constructor_pending_elts = t;
5930 break;
5932 else
5934 /* p->balance == +1; growth of left side balances the node. */
5935 p->balance = 0;
5936 break;
5939 else /* r == p->right */
5941 if (p->balance == 0)
5942 /* Growth propagation from right side. */
5943 p->balance++;
5944 else if (p->balance > 0)
5946 if (r->balance > 0)
5948 /* R rotation. */
5949 p->right = r->left;
5950 if (p->right)
5951 p->right->parent = p;
5952 r->left = p;
5954 p->balance = 0;
5955 r->balance = 0;
5957 s = p->parent;
5958 p->parent = r;
5959 r->parent = s;
5960 if (s)
5962 if (s->left == p)
5963 s->left = r;
5964 else
5965 s->right = r;
5967 else
5968 constructor_pending_elts = r;
5970 else /* r->balance == -1 */
5972 /* RL rotation */
5973 struct init_node *t = r->left;
5975 r->left = t->right;
5976 if (r->left)
5977 r->left->parent = r;
5978 t->right = r;
5980 p->right = t->left;
5981 if (p->right)
5982 p->right->parent = p;
5983 t->left = p;
5985 r->balance = (t->balance < 0);
5986 p->balance = -(t->balance > 0);
5987 t->balance = 0;
5989 s = p->parent;
5990 p->parent = t;
5991 r->parent = t;
5992 t->parent = s;
5993 if (s)
5995 if (s->left == p)
5996 s->left = t;
5997 else
5998 s->right = t;
6000 else
6001 constructor_pending_elts = t;
6003 break;
6005 else
6007 /* p->balance == -1; growth of right side balances the node. */
6008 p->balance = 0;
6009 break;
6013 r = p;
6014 p = p->parent;
6018 /* Build AVL tree from a sorted chain. */
6020 static void
6021 set_nonincremental_init (void)
6023 unsigned HOST_WIDE_INT ix;
6024 tree index, value;
6026 if (TREE_CODE (constructor_type) != RECORD_TYPE
6027 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6028 return;
6030 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6031 add_pending_init (index, value);
6032 constructor_elements = 0;
6033 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6035 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6036 /* Skip any nameless bit fields at the beginning. */
6037 while (constructor_unfilled_fields != 0
6038 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6039 && DECL_NAME (constructor_unfilled_fields) == 0)
6040 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6043 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6045 if (TYPE_DOMAIN (constructor_type))
6046 constructor_unfilled_index
6047 = convert (bitsizetype,
6048 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6049 else
6050 constructor_unfilled_index = bitsize_zero_node;
6052 constructor_incremental = 0;
6055 /* Build AVL tree from a string constant. */
6057 static void
6058 set_nonincremental_init_from_string (tree str)
6060 tree value, purpose, type;
6061 HOST_WIDE_INT val[2];
6062 const char *p, *end;
6063 int byte, wchar_bytes, charwidth, bitpos;
6065 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6067 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6068 == TYPE_PRECISION (char_type_node))
6069 wchar_bytes = 1;
6070 else
6072 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6073 == TYPE_PRECISION (wchar_type_node));
6074 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6076 charwidth = TYPE_PRECISION (char_type_node);
6077 type = TREE_TYPE (constructor_type);
6078 p = TREE_STRING_POINTER (str);
6079 end = p + TREE_STRING_LENGTH (str);
6081 for (purpose = bitsize_zero_node;
6082 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6083 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6085 if (wchar_bytes == 1)
6087 val[1] = (unsigned char) *p++;
6088 val[0] = 0;
6090 else
6092 val[0] = 0;
6093 val[1] = 0;
6094 for (byte = 0; byte < wchar_bytes; byte++)
6096 if (BYTES_BIG_ENDIAN)
6097 bitpos = (wchar_bytes - byte - 1) * charwidth;
6098 else
6099 bitpos = byte * charwidth;
6100 val[bitpos < HOST_BITS_PER_WIDE_INT]
6101 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6102 << (bitpos % HOST_BITS_PER_WIDE_INT);
6106 if (!TYPE_UNSIGNED (type))
6108 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6109 if (bitpos < HOST_BITS_PER_WIDE_INT)
6111 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6113 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6114 val[0] = -1;
6117 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6119 if (val[1] < 0)
6120 val[0] = -1;
6122 else if (val[0] & (((HOST_WIDE_INT) 1)
6123 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6124 val[0] |= ((HOST_WIDE_INT) -1)
6125 << (bitpos - HOST_BITS_PER_WIDE_INT);
6128 value = build_int_cst_wide (type, val[1], val[0]);
6129 add_pending_init (purpose, value);
6132 constructor_incremental = 0;
6135 /* Return value of FIELD in pending initializer or zero if the field was
6136 not initialized yet. */
6138 static tree
6139 find_init_member (tree field)
6141 struct init_node *p;
6143 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6145 if (constructor_incremental
6146 && tree_int_cst_lt (field, constructor_unfilled_index))
6147 set_nonincremental_init ();
6149 p = constructor_pending_elts;
6150 while (p)
6152 if (tree_int_cst_lt (field, p->purpose))
6153 p = p->left;
6154 else if (tree_int_cst_lt (p->purpose, field))
6155 p = p->right;
6156 else
6157 return p->value;
6160 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6162 tree bitpos = bit_position (field);
6164 if (constructor_incremental
6165 && (!constructor_unfilled_fields
6166 || tree_int_cst_lt (bitpos,
6167 bit_position (constructor_unfilled_fields))))
6168 set_nonincremental_init ();
6170 p = constructor_pending_elts;
6171 while (p)
6173 if (field == p->purpose)
6174 return p->value;
6175 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6176 p = p->left;
6177 else
6178 p = p->right;
6181 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6183 if (!VEC_empty (constructor_elt, constructor_elements)
6184 && (VEC_last (constructor_elt, constructor_elements)->index
6185 == field))
6186 return VEC_last (constructor_elt, constructor_elements)->value;
6188 return 0;
6191 /* "Output" the next constructor element.
6192 At top level, really output it to assembler code now.
6193 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6194 TYPE is the data type that the containing data type wants here.
6195 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6196 If VALUE is a string constant, STRICT_STRING is true if it is
6197 unparenthesized or we should not warn here for it being parenthesized.
6198 For other types of VALUE, STRICT_STRING is not used.
6200 PENDING if non-nil means output pending elements that belong
6201 right after this element. (PENDING is normally 1;
6202 it is 0 while outputting pending elements, to avoid recursion.) */
6204 static void
6205 output_init_element (tree value, bool strict_string, tree type, tree field,
6206 int pending)
6208 constructor_elt *celt;
6210 if (type == error_mark_node || value == error_mark_node)
6212 constructor_erroneous = 1;
6213 return;
6215 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6216 && (TREE_CODE (value) == STRING_CST
6217 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6218 && !(TREE_CODE (value) == STRING_CST
6219 && TREE_CODE (type) == ARRAY_TYPE
6220 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6221 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6222 TYPE_MAIN_VARIANT (type)))
6223 value = array_to_pointer_conversion (value);
6225 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6226 && require_constant_value && !flag_isoc99 && pending)
6228 /* As an extension, allow initializing objects with static storage
6229 duration with compound literals (which are then treated just as
6230 the brace enclosed list they contain). */
6231 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6232 value = DECL_INITIAL (decl);
6235 if (value == error_mark_node)
6236 constructor_erroneous = 1;
6237 else if (!TREE_CONSTANT (value))
6238 constructor_constant = 0;
6239 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6240 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6241 || TREE_CODE (constructor_type) == UNION_TYPE)
6242 && DECL_C_BIT_FIELD (field)
6243 && TREE_CODE (value) != INTEGER_CST))
6244 constructor_simple = 0;
6246 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6248 if (require_constant_value)
6250 error_init ("initializer element is not constant");
6251 value = error_mark_node;
6253 else if (require_constant_elements)
6254 pedwarn ("initializer element is not computable at load time");
6257 /* If this field is empty (and not at the end of structure),
6258 don't do anything other than checking the initializer. */
6259 if (field
6260 && (TREE_TYPE (field) == error_mark_node
6261 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6262 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6263 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6264 || TREE_CHAIN (field)))))
6265 return;
6267 value = digest_init (type, value, strict_string, require_constant_value);
6268 if (value == error_mark_node)
6270 constructor_erroneous = 1;
6271 return;
6274 /* If this element doesn't come next in sequence,
6275 put it on constructor_pending_elts. */
6276 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6277 && (!constructor_incremental
6278 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6280 if (constructor_incremental
6281 && tree_int_cst_lt (field, constructor_unfilled_index))
6282 set_nonincremental_init ();
6284 add_pending_init (field, value);
6285 return;
6287 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6288 && (!constructor_incremental
6289 || field != constructor_unfilled_fields))
6291 /* We do this for records but not for unions. In a union,
6292 no matter which field is specified, it can be initialized
6293 right away since it starts at the beginning of the union. */
6294 if (constructor_incremental)
6296 if (!constructor_unfilled_fields)
6297 set_nonincremental_init ();
6298 else
6300 tree bitpos, unfillpos;
6302 bitpos = bit_position (field);
6303 unfillpos = bit_position (constructor_unfilled_fields);
6305 if (tree_int_cst_lt (bitpos, unfillpos))
6306 set_nonincremental_init ();
6310 add_pending_init (field, value);
6311 return;
6313 else if (TREE_CODE (constructor_type) == UNION_TYPE
6314 && !VEC_empty (constructor_elt, constructor_elements))
6316 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6317 constructor_elements)->value))
6318 warning_init ("initialized field with side-effects overwritten");
6319 else if (warn_override_init)
6320 warning_init ("initialized field overwritten");
6322 /* We can have just one union field set. */
6323 constructor_elements = 0;
6326 /* Otherwise, output this element either to
6327 constructor_elements or to the assembler file. */
6329 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6330 celt->index = field;
6331 celt->value = value;
6333 /* Advance the variable that indicates sequential elements output. */
6334 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6335 constructor_unfilled_index
6336 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6337 bitsize_one_node);
6338 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6340 constructor_unfilled_fields
6341 = TREE_CHAIN (constructor_unfilled_fields);
6343 /* Skip any nameless bit fields. */
6344 while (constructor_unfilled_fields != 0
6345 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6346 && DECL_NAME (constructor_unfilled_fields) == 0)
6347 constructor_unfilled_fields =
6348 TREE_CHAIN (constructor_unfilled_fields);
6350 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6351 constructor_unfilled_fields = 0;
6353 /* Now output any pending elements which have become next. */
6354 if (pending)
6355 output_pending_init_elements (0);
6358 /* Output any pending elements which have become next.
6359 As we output elements, constructor_unfilled_{fields,index}
6360 advances, which may cause other elements to become next;
6361 if so, they too are output.
6363 If ALL is 0, we return when there are
6364 no more pending elements to output now.
6366 If ALL is 1, we output space as necessary so that
6367 we can output all the pending elements. */
6369 static void
6370 output_pending_init_elements (int all)
6372 struct init_node *elt = constructor_pending_elts;
6373 tree next;
6375 retry:
6377 /* Look through the whole pending tree.
6378 If we find an element that should be output now,
6379 output it. Otherwise, set NEXT to the element
6380 that comes first among those still pending. */
6382 next = 0;
6383 while (elt)
6385 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6387 if (tree_int_cst_equal (elt->purpose,
6388 constructor_unfilled_index))
6389 output_init_element (elt->value, true,
6390 TREE_TYPE (constructor_type),
6391 constructor_unfilled_index, 0);
6392 else if (tree_int_cst_lt (constructor_unfilled_index,
6393 elt->purpose))
6395 /* Advance to the next smaller node. */
6396 if (elt->left)
6397 elt = elt->left;
6398 else
6400 /* We have reached the smallest node bigger than the
6401 current unfilled index. Fill the space first. */
6402 next = elt->purpose;
6403 break;
6406 else
6408 /* Advance to the next bigger node. */
6409 if (elt->right)
6410 elt = elt->right;
6411 else
6413 /* We have reached the biggest node in a subtree. Find
6414 the parent of it, which is the next bigger node. */
6415 while (elt->parent && elt->parent->right == elt)
6416 elt = elt->parent;
6417 elt = elt->parent;
6418 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6419 elt->purpose))
6421 next = elt->purpose;
6422 break;
6427 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6428 || TREE_CODE (constructor_type) == UNION_TYPE)
6430 tree ctor_unfilled_bitpos, elt_bitpos;
6432 /* If the current record is complete we are done. */
6433 if (constructor_unfilled_fields == 0)
6434 break;
6436 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6437 elt_bitpos = bit_position (elt->purpose);
6438 /* We can't compare fields here because there might be empty
6439 fields in between. */
6440 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6442 constructor_unfilled_fields = elt->purpose;
6443 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6444 elt->purpose, 0);
6446 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6448 /* Advance to the next smaller node. */
6449 if (elt->left)
6450 elt = elt->left;
6451 else
6453 /* We have reached the smallest node bigger than the
6454 current unfilled field. Fill the space first. */
6455 next = elt->purpose;
6456 break;
6459 else
6461 /* Advance to the next bigger node. */
6462 if (elt->right)
6463 elt = elt->right;
6464 else
6466 /* We have reached the biggest node in a subtree. Find
6467 the parent of it, which is the next bigger node. */
6468 while (elt->parent && elt->parent->right == elt)
6469 elt = elt->parent;
6470 elt = elt->parent;
6471 if (elt
6472 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6473 bit_position (elt->purpose))))
6475 next = elt->purpose;
6476 break;
6483 /* Ordinarily return, but not if we want to output all
6484 and there are elements left. */
6485 if (!(all && next != 0))
6486 return;
6488 /* If it's not incremental, just skip over the gap, so that after
6489 jumping to retry we will output the next successive element. */
6490 if (TREE_CODE (constructor_type) == RECORD_TYPE
6491 || TREE_CODE (constructor_type) == UNION_TYPE)
6492 constructor_unfilled_fields = next;
6493 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6494 constructor_unfilled_index = next;
6496 /* ELT now points to the node in the pending tree with the next
6497 initializer to output. */
6498 goto retry;
6501 /* Add one non-braced element to the current constructor level.
6502 This adjusts the current position within the constructor's type.
6503 This may also start or terminate implicit levels
6504 to handle a partly-braced initializer.
6506 Once this has found the correct level for the new element,
6507 it calls output_init_element. */
6509 void
6510 process_init_element (struct c_expr value)
6512 tree orig_value = value.value;
6513 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6514 bool strict_string = value.original_code == STRING_CST;
6516 designator_depth = 0;
6517 designator_erroneous = 0;
6519 /* Handle superfluous braces around string cst as in
6520 char x[] = {"foo"}; */
6521 if (string_flag
6522 && constructor_type
6523 && TREE_CODE (constructor_type) == ARRAY_TYPE
6524 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6525 && integer_zerop (constructor_unfilled_index))
6527 if (constructor_stack->replacement_value.value)
6528 error_init ("excess elements in char array initializer");
6529 constructor_stack->replacement_value = value;
6530 return;
6533 if (constructor_stack->replacement_value.value != 0)
6535 error_init ("excess elements in struct initializer");
6536 return;
6539 /* Ignore elements of a brace group if it is entirely superfluous
6540 and has already been diagnosed. */
6541 if (constructor_type == 0)
6542 return;
6544 /* If we've exhausted any levels that didn't have braces,
6545 pop them now. */
6546 while (constructor_stack->implicit)
6548 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6549 || TREE_CODE (constructor_type) == UNION_TYPE)
6550 && constructor_fields == 0)
6551 process_init_element (pop_init_level (1));
6552 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6553 && (constructor_max_index == 0
6554 || tree_int_cst_lt (constructor_max_index,
6555 constructor_index)))
6556 process_init_element (pop_init_level (1));
6557 else
6558 break;
6561 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6562 if (constructor_range_stack)
6564 /* If value is a compound literal and we'll be just using its
6565 content, don't put it into a SAVE_EXPR. */
6566 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6567 || !require_constant_value
6568 || flag_isoc99)
6569 value.value = save_expr (value.value);
6572 while (1)
6574 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6576 tree fieldtype;
6577 enum tree_code fieldcode;
6579 if (constructor_fields == 0)
6581 pedwarn_init ("excess elements in struct initializer");
6582 break;
6585 fieldtype = TREE_TYPE (constructor_fields);
6586 if (fieldtype != error_mark_node)
6587 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6588 fieldcode = TREE_CODE (fieldtype);
6590 /* Error for non-static initialization of a flexible array member. */
6591 if (fieldcode == ARRAY_TYPE
6592 && !require_constant_value
6593 && TYPE_SIZE (fieldtype) == NULL_TREE
6594 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6596 error_init ("non-static initialization of a flexible array member");
6597 break;
6600 /* Accept a string constant to initialize a subarray. */
6601 if (value.value != 0
6602 && fieldcode == ARRAY_TYPE
6603 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6604 && string_flag)
6605 value.value = orig_value;
6606 /* Otherwise, if we have come to a subaggregate,
6607 and we don't have an element of its type, push into it. */
6608 else if (value.value != 0
6609 && value.value != error_mark_node
6610 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6611 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6612 || fieldcode == UNION_TYPE))
6614 push_init_level (1);
6615 continue;
6618 if (value.value)
6620 push_member_name (constructor_fields);
6621 output_init_element (value.value, strict_string,
6622 fieldtype, constructor_fields, 1);
6623 RESTORE_SPELLING_DEPTH (constructor_depth);
6625 else
6626 /* Do the bookkeeping for an element that was
6627 directly output as a constructor. */
6629 /* For a record, keep track of end position of last field. */
6630 if (DECL_SIZE (constructor_fields))
6631 constructor_bit_index
6632 = size_binop (PLUS_EXPR,
6633 bit_position (constructor_fields),
6634 DECL_SIZE (constructor_fields));
6636 /* If the current field was the first one not yet written out,
6637 it isn't now, so update. */
6638 if (constructor_unfilled_fields == constructor_fields)
6640 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6641 /* Skip any nameless bit fields. */
6642 while (constructor_unfilled_fields != 0
6643 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6644 && DECL_NAME (constructor_unfilled_fields) == 0)
6645 constructor_unfilled_fields =
6646 TREE_CHAIN (constructor_unfilled_fields);
6650 constructor_fields = TREE_CHAIN (constructor_fields);
6651 /* Skip any nameless bit fields at the beginning. */
6652 while (constructor_fields != 0
6653 && DECL_C_BIT_FIELD (constructor_fields)
6654 && DECL_NAME (constructor_fields) == 0)
6655 constructor_fields = TREE_CHAIN (constructor_fields);
6657 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6659 tree fieldtype;
6660 enum tree_code fieldcode;
6662 if (constructor_fields == 0)
6664 pedwarn_init ("excess elements in union initializer");
6665 break;
6668 fieldtype = TREE_TYPE (constructor_fields);
6669 if (fieldtype != error_mark_node)
6670 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6671 fieldcode = TREE_CODE (fieldtype);
6673 /* Warn that traditional C rejects initialization of unions.
6674 We skip the warning if the value is zero. This is done
6675 under the assumption that the zero initializer in user
6676 code appears conditioned on e.g. __STDC__ to avoid
6677 "missing initializer" warnings and relies on default
6678 initialization to zero in the traditional C case.
6679 We also skip the warning if the initializer is designated,
6680 again on the assumption that this must be conditional on
6681 __STDC__ anyway (and we've already complained about the
6682 member-designator already). */
6683 if (!in_system_header && !constructor_designated
6684 && !(value.value && (integer_zerop (value.value)
6685 || real_zerop (value.value))))
6686 warning (OPT_Wtraditional, "traditional C rejects initialization "
6687 "of unions");
6689 /* Accept a string constant to initialize a subarray. */
6690 if (value.value != 0
6691 && fieldcode == ARRAY_TYPE
6692 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6693 && string_flag)
6694 value.value = orig_value;
6695 /* Otherwise, if we have come to a subaggregate,
6696 and we don't have an element of its type, push into it. */
6697 else if (value.value != 0
6698 && value.value != error_mark_node
6699 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6700 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6701 || fieldcode == UNION_TYPE))
6703 push_init_level (1);
6704 continue;
6707 if (value.value)
6709 push_member_name (constructor_fields);
6710 output_init_element (value.value, strict_string,
6711 fieldtype, constructor_fields, 1);
6712 RESTORE_SPELLING_DEPTH (constructor_depth);
6714 else
6715 /* Do the bookkeeping for an element that was
6716 directly output as a constructor. */
6718 constructor_bit_index = DECL_SIZE (constructor_fields);
6719 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6722 constructor_fields = 0;
6724 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6726 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6727 enum tree_code eltcode = TREE_CODE (elttype);
6729 /* Accept a string constant to initialize a subarray. */
6730 if (value.value != 0
6731 && eltcode == ARRAY_TYPE
6732 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6733 && string_flag)
6734 value.value = orig_value;
6735 /* Otherwise, if we have come to a subaggregate,
6736 and we don't have an element of its type, push into it. */
6737 else if (value.value != 0
6738 && value.value != error_mark_node
6739 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6740 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6741 || eltcode == UNION_TYPE))
6743 push_init_level (1);
6744 continue;
6747 if (constructor_max_index != 0
6748 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6749 || integer_all_onesp (constructor_max_index)))
6751 pedwarn_init ("excess elements in array initializer");
6752 break;
6755 /* Now output the actual element. */
6756 if (value.value)
6758 push_array_bounds (tree_low_cst (constructor_index, 1));
6759 output_init_element (value.value, strict_string,
6760 elttype, constructor_index, 1);
6761 RESTORE_SPELLING_DEPTH (constructor_depth);
6764 constructor_index
6765 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6767 if (!value.value)
6768 /* If we are doing the bookkeeping for an element that was
6769 directly output as a constructor, we must update
6770 constructor_unfilled_index. */
6771 constructor_unfilled_index = constructor_index;
6773 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6775 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6777 /* Do a basic check of initializer size. Note that vectors
6778 always have a fixed size derived from their type. */
6779 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6781 pedwarn_init ("excess elements in vector initializer");
6782 break;
6785 /* Now output the actual element. */
6786 if (value.value)
6787 output_init_element (value.value, strict_string,
6788 elttype, constructor_index, 1);
6790 constructor_index
6791 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6793 if (!value.value)
6794 /* If we are doing the bookkeeping for an element that was
6795 directly output as a constructor, we must update
6796 constructor_unfilled_index. */
6797 constructor_unfilled_index = constructor_index;
6800 /* Handle the sole element allowed in a braced initializer
6801 for a scalar variable. */
6802 else if (constructor_type != error_mark_node
6803 && constructor_fields == 0)
6805 pedwarn_init ("excess elements in scalar initializer");
6806 break;
6808 else
6810 if (value.value)
6811 output_init_element (value.value, strict_string,
6812 constructor_type, NULL_TREE, 1);
6813 constructor_fields = 0;
6816 /* Handle range initializers either at this level or anywhere higher
6817 in the designator stack. */
6818 if (constructor_range_stack)
6820 struct constructor_range_stack *p, *range_stack;
6821 int finish = 0;
6823 range_stack = constructor_range_stack;
6824 constructor_range_stack = 0;
6825 while (constructor_stack != range_stack->stack)
6827 gcc_assert (constructor_stack->implicit);
6828 process_init_element (pop_init_level (1));
6830 for (p = range_stack;
6831 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6832 p = p->prev)
6834 gcc_assert (constructor_stack->implicit);
6835 process_init_element (pop_init_level (1));
6838 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6839 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6840 finish = 1;
6842 while (1)
6844 constructor_index = p->index;
6845 constructor_fields = p->fields;
6846 if (finish && p->range_end && p->index == p->range_start)
6848 finish = 0;
6849 p->prev = 0;
6851 p = p->next;
6852 if (!p)
6853 break;
6854 push_init_level (2);
6855 p->stack = constructor_stack;
6856 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6857 p->index = p->range_start;
6860 if (!finish)
6861 constructor_range_stack = range_stack;
6862 continue;
6865 break;
6868 constructor_range_stack = 0;
6871 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6872 (guaranteed to be 'volatile' or null) and ARGS (represented using
6873 an ASM_EXPR node). */
6874 tree
6875 build_asm_stmt (tree cv_qualifier, tree args)
6877 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6878 ASM_VOLATILE_P (args) = 1;
6879 return add_stmt (args);
6882 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6883 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6884 SIMPLE indicates whether there was anything at all after the
6885 string in the asm expression -- asm("blah") and asm("blah" : )
6886 are subtly different. We use a ASM_EXPR node to represent this. */
6887 tree
6888 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6889 bool simple)
6891 tree tail;
6892 tree args;
6893 int i;
6894 const char *constraint;
6895 const char **oconstraints;
6896 bool allows_mem, allows_reg, is_inout;
6897 int ninputs, noutputs;
6899 ninputs = list_length (inputs);
6900 noutputs = list_length (outputs);
6901 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6903 string = resolve_asm_operand_names (string, outputs, inputs);
6905 /* Remove output conversions that change the type but not the mode. */
6906 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6908 tree output = TREE_VALUE (tail);
6910 /* ??? Really, this should not be here. Users should be using a
6911 proper lvalue, dammit. But there's a long history of using casts
6912 in the output operands. In cases like longlong.h, this becomes a
6913 primitive form of typechecking -- if the cast can be removed, then
6914 the output operand had a type of the proper width; otherwise we'll
6915 get an error. Gross, but ... */
6916 STRIP_NOPS (output);
6918 if (!lvalue_or_else (output, lv_asm))
6919 output = error_mark_node;
6921 if (output != error_mark_node
6922 && (TREE_READONLY (output)
6923 || TYPE_READONLY (TREE_TYPE (output))
6924 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6925 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6926 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6927 readonly_error (output, lv_asm);
6929 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6930 oconstraints[i] = constraint;
6932 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6933 &allows_mem, &allows_reg, &is_inout))
6935 /* If the operand is going to end up in memory,
6936 mark it addressable. */
6937 if (!allows_reg && !c_mark_addressable (output))
6938 output = error_mark_node;
6940 else
6941 output = error_mark_node;
6943 TREE_VALUE (tail) = output;
6946 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6948 tree input;
6950 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6951 input = TREE_VALUE (tail);
6953 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6954 oconstraints, &allows_mem, &allows_reg))
6956 /* If the operand is going to end up in memory,
6957 mark it addressable. */
6958 if (!allows_reg && allows_mem)
6960 /* Strip the nops as we allow this case. FIXME, this really
6961 should be rejected or made deprecated. */
6962 STRIP_NOPS (input);
6963 if (!c_mark_addressable (input))
6964 input = error_mark_node;
6967 else
6968 input = error_mark_node;
6970 TREE_VALUE (tail) = input;
6973 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6975 /* asm statements without outputs, including simple ones, are treated
6976 as volatile. */
6977 ASM_INPUT_P (args) = simple;
6978 ASM_VOLATILE_P (args) = (noutputs == 0);
6980 return args;
6983 /* Generate a goto statement to LABEL. */
6985 tree
6986 c_finish_goto_label (tree label)
6988 tree decl = lookup_label (label);
6989 if (!decl)
6990 return NULL_TREE;
6992 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6994 error ("jump into statement expression");
6995 return NULL_TREE;
6998 if (C_DECL_UNJUMPABLE_VM (decl))
7000 error ("jump into scope of identifier with variably modified type");
7001 return NULL_TREE;
7004 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7006 /* No jump from outside this statement expression context, so
7007 record that there is a jump from within this context. */
7008 struct c_label_list *nlist;
7009 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7010 nlist->next = label_context_stack_se->labels_used;
7011 nlist->label = decl;
7012 label_context_stack_se->labels_used = nlist;
7015 if (!C_DECL_UNDEFINABLE_VM (decl))
7017 /* No jump from outside this context context of identifiers with
7018 variably modified type, so record that there is a jump from
7019 within this context. */
7020 struct c_label_list *nlist;
7021 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7022 nlist->next = label_context_stack_vm->labels_used;
7023 nlist->label = decl;
7024 label_context_stack_vm->labels_used = nlist;
7027 TREE_USED (decl) = 1;
7028 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7031 /* Generate a computed goto statement to EXPR. */
7033 tree
7034 c_finish_goto_ptr (tree expr)
7036 if (pedantic)
7037 pedwarn ("ISO C forbids %<goto *expr;%>");
7038 expr = convert (ptr_type_node, expr);
7039 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7042 /* Generate a C `return' statement. RETVAL is the expression for what
7043 to return, or a null pointer for `return;' with no value. */
7045 tree
7046 c_finish_return (tree retval)
7048 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7049 bool no_warning = false;
7051 if (TREE_THIS_VOLATILE (current_function_decl))
7052 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7054 if (!retval)
7056 current_function_returns_null = 1;
7057 if ((warn_return_type || flag_isoc99)
7058 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7060 pedwarn_c99 ("%<return%> with no value, in "
7061 "function returning non-void");
7062 no_warning = true;
7065 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7067 current_function_returns_null = 1;
7068 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7069 pedwarn ("%<return%> with a value, in function returning void");
7070 else if (pedantic)
7071 pedwarn ("ISO C forbids %<return%> with expression, in function returning void");
7073 else
7075 tree t = convert_for_assignment (valtype, retval, ic_return,
7076 NULL_TREE, NULL_TREE, 0);
7077 tree res = DECL_RESULT (current_function_decl);
7078 tree inner;
7080 current_function_returns_value = 1;
7081 if (t == error_mark_node)
7082 return NULL_TREE;
7084 inner = t = convert (TREE_TYPE (res), t);
7086 /* Strip any conversions, additions, and subtractions, and see if
7087 we are returning the address of a local variable. Warn if so. */
7088 while (1)
7090 switch (TREE_CODE (inner))
7092 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7093 case PLUS_EXPR:
7094 inner = TREE_OPERAND (inner, 0);
7095 continue;
7097 case MINUS_EXPR:
7098 /* If the second operand of the MINUS_EXPR has a pointer
7099 type (or is converted from it), this may be valid, so
7100 don't give a warning. */
7102 tree op1 = TREE_OPERAND (inner, 1);
7104 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7105 && (TREE_CODE (op1) == NOP_EXPR
7106 || TREE_CODE (op1) == NON_LVALUE_EXPR
7107 || TREE_CODE (op1) == CONVERT_EXPR))
7108 op1 = TREE_OPERAND (op1, 0);
7110 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7111 break;
7113 inner = TREE_OPERAND (inner, 0);
7114 continue;
7117 case ADDR_EXPR:
7118 inner = TREE_OPERAND (inner, 0);
7120 while (REFERENCE_CLASS_P (inner)
7121 && TREE_CODE (inner) != INDIRECT_REF)
7122 inner = TREE_OPERAND (inner, 0);
7124 if (DECL_P (inner)
7125 && !DECL_EXTERNAL (inner)
7126 && !TREE_STATIC (inner)
7127 && DECL_CONTEXT (inner) == current_function_decl)
7128 warning (0, "function returns address of local variable");
7129 break;
7131 default:
7132 break;
7135 break;
7138 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7141 ret_stmt = build_stmt (RETURN_EXPR, retval);
7142 TREE_NO_WARNING (ret_stmt) |= no_warning;
7143 return add_stmt (ret_stmt);
7146 struct c_switch {
7147 /* The SWITCH_EXPR being built. */
7148 tree switch_expr;
7150 /* The original type of the testing expression, i.e. before the
7151 default conversion is applied. */
7152 tree orig_type;
7154 /* A splay-tree mapping the low element of a case range to the high
7155 element, or NULL_TREE if there is no high element. Used to
7156 determine whether or not a new case label duplicates an old case
7157 label. We need a tree, rather than simply a hash table, because
7158 of the GNU case range extension. */
7159 splay_tree cases;
7161 /* Number of nested statement expressions within this switch
7162 statement; if nonzero, case and default labels may not
7163 appear. */
7164 unsigned int blocked_stmt_expr;
7166 /* Scope of outermost declarations of identifiers with variably
7167 modified type within this switch statement; if nonzero, case and
7168 default labels may not appear. */
7169 unsigned int blocked_vm;
7171 /* The next node on the stack. */
7172 struct c_switch *next;
7175 /* A stack of the currently active switch statements. The innermost
7176 switch statement is on the top of the stack. There is no need to
7177 mark the stack for garbage collection because it is only active
7178 during the processing of the body of a function, and we never
7179 collect at that point. */
7181 struct c_switch *c_switch_stack;
7183 /* Start a C switch statement, testing expression EXP. Return the new
7184 SWITCH_EXPR. */
7186 tree
7187 c_start_case (tree exp)
7189 tree orig_type = error_mark_node;
7190 struct c_switch *cs;
7192 if (exp != error_mark_node)
7194 orig_type = TREE_TYPE (exp);
7196 if (!INTEGRAL_TYPE_P (orig_type))
7198 if (orig_type != error_mark_node)
7200 error ("switch quantity not an integer");
7201 orig_type = error_mark_node;
7203 exp = integer_zero_node;
7205 else
7207 tree type = TYPE_MAIN_VARIANT (orig_type);
7209 if (!in_system_header
7210 && (type == long_integer_type_node
7211 || type == long_unsigned_type_node))
7212 warning (OPT_Wtraditional, "%<long%> switch expression not "
7213 "converted to %<int%> in ISO C");
7215 exp = default_conversion (exp);
7219 /* Add this new SWITCH_EXPR to the stack. */
7220 cs = XNEW (struct c_switch);
7221 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7222 cs->orig_type = orig_type;
7223 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7224 cs->blocked_stmt_expr = 0;
7225 cs->blocked_vm = 0;
7226 cs->next = c_switch_stack;
7227 c_switch_stack = cs;
7229 return add_stmt (cs->switch_expr);
7232 /* Process a case label. */
7234 tree
7235 do_case (tree low_value, tree high_value)
7237 tree label = NULL_TREE;
7239 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7240 && !c_switch_stack->blocked_vm)
7242 label = c_add_case_label (c_switch_stack->cases,
7243 SWITCH_COND (c_switch_stack->switch_expr),
7244 c_switch_stack->orig_type,
7245 low_value, high_value);
7246 if (label == error_mark_node)
7247 label = NULL_TREE;
7249 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7251 if (low_value)
7252 error ("case label in statement expression not containing "
7253 "enclosing switch statement");
7254 else
7255 error ("%<default%> label in statement expression not containing "
7256 "enclosing switch statement");
7258 else if (c_switch_stack && c_switch_stack->blocked_vm)
7260 if (low_value)
7261 error ("case label in scope of identifier with variably modified "
7262 "type not containing enclosing switch statement");
7263 else
7264 error ("%<default%> label in scope of identifier with variably "
7265 "modified type not containing enclosing switch statement");
7267 else if (low_value)
7268 error ("case label not within a switch statement");
7269 else
7270 error ("%<default%> label not within a switch statement");
7272 return label;
7275 /* Finish the switch statement. */
7277 void
7278 c_finish_case (tree body)
7280 struct c_switch *cs = c_switch_stack;
7281 location_t switch_location;
7283 SWITCH_BODY (cs->switch_expr) = body;
7285 /* We must not be within a statement expression nested in the switch
7286 at this point; we might, however, be within the scope of an
7287 identifier with variably modified type nested in the switch. */
7288 gcc_assert (!cs->blocked_stmt_expr);
7290 /* Emit warnings as needed. */
7291 if (EXPR_HAS_LOCATION (cs->switch_expr))
7292 switch_location = EXPR_LOCATION (cs->switch_expr);
7293 else
7294 switch_location = input_location;
7295 c_do_switch_warnings (cs->cases, switch_location,
7296 TREE_TYPE (cs->switch_expr),
7297 SWITCH_COND (cs->switch_expr));
7299 /* Pop the stack. */
7300 c_switch_stack = cs->next;
7301 splay_tree_delete (cs->cases);
7302 XDELETE (cs);
7305 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7306 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7307 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7308 statement, and was not surrounded with parenthesis. */
7310 void
7311 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7312 tree else_block, bool nested_if)
7314 tree stmt;
7316 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7317 if (warn_parentheses && nested_if && else_block == NULL)
7319 tree inner_if = then_block;
7321 /* We know from the grammar productions that there is an IF nested
7322 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7323 it might not be exactly THEN_BLOCK, but should be the last
7324 non-container statement within. */
7325 while (1)
7326 switch (TREE_CODE (inner_if))
7328 case COND_EXPR:
7329 goto found;
7330 case BIND_EXPR:
7331 inner_if = BIND_EXPR_BODY (inner_if);
7332 break;
7333 case STATEMENT_LIST:
7334 inner_if = expr_last (then_block);
7335 break;
7336 case TRY_FINALLY_EXPR:
7337 case TRY_CATCH_EXPR:
7338 inner_if = TREE_OPERAND (inner_if, 0);
7339 break;
7340 default:
7341 gcc_unreachable ();
7343 found:
7345 if (COND_EXPR_ELSE (inner_if))
7346 warning (OPT_Wparentheses,
7347 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7348 &if_locus);
7351 empty_if_body_warning (then_block, else_block);
7353 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7354 SET_EXPR_LOCATION (stmt, if_locus);
7355 add_stmt (stmt);
7358 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7359 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7360 is false for DO loops. INCR is the FOR increment expression. BODY is
7361 the statement controlled by the loop. BLAB is the break label. CLAB is
7362 the continue label. Everything is allowed to be NULL. */
7364 void
7365 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7366 tree blab, tree clab, bool cond_is_first)
7368 tree entry = NULL, exit = NULL, t;
7370 /* If the condition is zero don't generate a loop construct. */
7371 if (cond && integer_zerop (cond))
7373 if (cond_is_first)
7375 t = build_and_jump (&blab);
7376 SET_EXPR_LOCATION (t, start_locus);
7377 add_stmt (t);
7380 else
7382 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7384 /* If we have an exit condition, then we build an IF with gotos either
7385 out of the loop, or to the top of it. If there's no exit condition,
7386 then we just build a jump back to the top. */
7387 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7389 if (cond && !integer_nonzerop (cond))
7391 /* Canonicalize the loop condition to the end. This means
7392 generating a branch to the loop condition. Reuse the
7393 continue label, if possible. */
7394 if (cond_is_first)
7396 if (incr || !clab)
7398 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7399 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7401 else
7402 t = build1 (GOTO_EXPR, void_type_node, clab);
7403 SET_EXPR_LOCATION (t, start_locus);
7404 add_stmt (t);
7407 t = build_and_jump (&blab);
7408 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7409 if (cond_is_first)
7410 SET_EXPR_LOCATION (exit, start_locus);
7411 else
7412 SET_EXPR_LOCATION (exit, input_location);
7415 add_stmt (top);
7418 if (body)
7419 add_stmt (body);
7420 if (clab)
7421 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7422 if (incr)
7423 add_stmt (incr);
7424 if (entry)
7425 add_stmt (entry);
7426 if (exit)
7427 add_stmt (exit);
7428 if (blab)
7429 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7432 tree
7433 c_finish_bc_stmt (tree *label_p, bool is_break)
7435 bool skip;
7436 tree label = *label_p;
7438 /* In switch statements break is sometimes stylistically used after
7439 a return statement. This can lead to spurious warnings about
7440 control reaching the end of a non-void function when it is
7441 inlined. Note that we are calling block_may_fallthru with
7442 language specific tree nodes; this works because
7443 block_may_fallthru returns true when given something it does not
7444 understand. */
7445 skip = !block_may_fallthru (cur_stmt_list);
7447 if (!label)
7449 if (!skip)
7450 *label_p = label = create_artificial_label ();
7452 else if (TREE_CODE (label) == LABEL_DECL)
7454 else switch (TREE_INT_CST_LOW (label))
7456 case 0:
7457 if (is_break)
7458 error ("break statement not within loop or switch");
7459 else
7460 error ("continue statement not within a loop");
7461 return NULL_TREE;
7463 case 1:
7464 gcc_assert (is_break);
7465 error ("break statement used with OpenMP for loop");
7466 return NULL_TREE;
7468 default:
7469 gcc_unreachable ();
7472 if (skip)
7473 return NULL_TREE;
7475 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7478 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7480 static void
7481 emit_side_effect_warnings (tree expr)
7483 if (expr == error_mark_node)
7485 else if (!TREE_SIDE_EFFECTS (expr))
7487 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7488 warning (OPT_Wunused_value, "%Hstatement with no effect",
7489 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7491 else
7492 warn_if_unused_value (expr, input_location);
7495 /* Process an expression as if it were a complete statement. Emit
7496 diagnostics, but do not call ADD_STMT. */
7498 tree
7499 c_process_expr_stmt (tree expr)
7501 if (!expr)
7502 return NULL_TREE;
7504 if (warn_sequence_point)
7505 verify_sequence_points (expr);
7507 if (TREE_TYPE (expr) != error_mark_node
7508 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7509 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7510 error ("expression statement has incomplete type");
7512 /* If we're not processing a statement expression, warn about unused values.
7513 Warnings for statement expressions will be emitted later, once we figure
7514 out which is the result. */
7515 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7516 && warn_unused_value)
7517 emit_side_effect_warnings (expr);
7519 /* If the expression is not of a type to which we cannot assign a line
7520 number, wrap the thing in a no-op NOP_EXPR. */
7521 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7522 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7524 if (CAN_HAVE_LOCATION_P (expr))
7525 SET_EXPR_LOCATION (expr, input_location);
7527 return expr;
7530 /* Emit an expression as a statement. */
7532 tree
7533 c_finish_expr_stmt (tree expr)
7535 if (expr)
7536 return add_stmt (c_process_expr_stmt (expr));
7537 else
7538 return NULL;
7541 /* Do the opposite and emit a statement as an expression. To begin,
7542 create a new binding level and return it. */
7544 tree
7545 c_begin_stmt_expr (void)
7547 tree ret;
7548 struct c_label_context_se *nstack;
7549 struct c_label_list *glist;
7551 /* We must force a BLOCK for this level so that, if it is not expanded
7552 later, there is a way to turn off the entire subtree of blocks that
7553 are contained in it. */
7554 keep_next_level ();
7555 ret = c_begin_compound_stmt (true);
7556 if (c_switch_stack)
7558 c_switch_stack->blocked_stmt_expr++;
7559 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7561 for (glist = label_context_stack_se->labels_used;
7562 glist != NULL;
7563 glist = glist->next)
7565 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7567 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7568 nstack->labels_def = NULL;
7569 nstack->labels_used = NULL;
7570 nstack->next = label_context_stack_se;
7571 label_context_stack_se = nstack;
7573 /* Mark the current statement list as belonging to a statement list. */
7574 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7576 return ret;
7579 tree
7580 c_finish_stmt_expr (tree body)
7582 tree last, type, tmp, val;
7583 tree *last_p;
7584 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7586 body = c_end_compound_stmt (body, true);
7587 if (c_switch_stack)
7589 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7590 c_switch_stack->blocked_stmt_expr--;
7592 /* It is no longer possible to jump to labels defined within this
7593 statement expression. */
7594 for (dlist = label_context_stack_se->labels_def;
7595 dlist != NULL;
7596 dlist = dlist->next)
7598 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7600 /* It is again possible to define labels with a goto just outside
7601 this statement expression. */
7602 for (glist = label_context_stack_se->next->labels_used;
7603 glist != NULL;
7604 glist = glist->next)
7606 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7607 glist_prev = glist;
7609 if (glist_prev != NULL)
7610 glist_prev->next = label_context_stack_se->labels_used;
7611 else
7612 label_context_stack_se->next->labels_used
7613 = label_context_stack_se->labels_used;
7614 label_context_stack_se = label_context_stack_se->next;
7616 /* Locate the last statement in BODY. See c_end_compound_stmt
7617 about always returning a BIND_EXPR. */
7618 last_p = &BIND_EXPR_BODY (body);
7619 last = BIND_EXPR_BODY (body);
7621 continue_searching:
7622 if (TREE_CODE (last) == STATEMENT_LIST)
7624 tree_stmt_iterator i;
7626 /* This can happen with degenerate cases like ({ }). No value. */
7627 if (!TREE_SIDE_EFFECTS (last))
7628 return body;
7630 /* If we're supposed to generate side effects warnings, process
7631 all of the statements except the last. */
7632 if (warn_unused_value)
7634 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7635 emit_side_effect_warnings (tsi_stmt (i));
7637 else
7638 i = tsi_last (last);
7639 last_p = tsi_stmt_ptr (i);
7640 last = *last_p;
7643 /* If the end of the list is exception related, then the list was split
7644 by a call to push_cleanup. Continue searching. */
7645 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7646 || TREE_CODE (last) == TRY_CATCH_EXPR)
7648 last_p = &TREE_OPERAND (last, 0);
7649 last = *last_p;
7650 goto continue_searching;
7653 /* In the case that the BIND_EXPR is not necessary, return the
7654 expression out from inside it. */
7655 if (last == error_mark_node
7656 || (last == BIND_EXPR_BODY (body)
7657 && BIND_EXPR_VARS (body) == NULL))
7659 /* Do not warn if the return value of a statement expression is
7660 unused. */
7661 if (CAN_HAVE_LOCATION_P (last))
7662 TREE_NO_WARNING (last) = 1;
7663 return last;
7666 /* Extract the type of said expression. */
7667 type = TREE_TYPE (last);
7669 /* If we're not returning a value at all, then the BIND_EXPR that
7670 we already have is a fine expression to return. */
7671 if (!type || VOID_TYPE_P (type))
7672 return body;
7674 /* Now that we've located the expression containing the value, it seems
7675 silly to make voidify_wrapper_expr repeat the process. Create a
7676 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7677 tmp = create_tmp_var_raw (type, NULL);
7679 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7680 tree_expr_nonnegative_p giving up immediately. */
7681 val = last;
7682 if (TREE_CODE (val) == NOP_EXPR
7683 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7684 val = TREE_OPERAND (val, 0);
7686 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7687 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7689 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7692 /* Begin the scope of an identifier of variably modified type, scope
7693 number SCOPE. Jumping from outside this scope to inside it is not
7694 permitted. */
7696 void
7697 c_begin_vm_scope (unsigned int scope)
7699 struct c_label_context_vm *nstack;
7700 struct c_label_list *glist;
7702 gcc_assert (scope > 0);
7704 /* At file_scope, we don't have to do any processing. */
7705 if (label_context_stack_vm == NULL)
7706 return;
7708 if (c_switch_stack && !c_switch_stack->blocked_vm)
7709 c_switch_stack->blocked_vm = scope;
7710 for (glist = label_context_stack_vm->labels_used;
7711 glist != NULL;
7712 glist = glist->next)
7714 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7716 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7717 nstack->labels_def = NULL;
7718 nstack->labels_used = NULL;
7719 nstack->scope = scope;
7720 nstack->next = label_context_stack_vm;
7721 label_context_stack_vm = nstack;
7724 /* End a scope which may contain identifiers of variably modified
7725 type, scope number SCOPE. */
7727 void
7728 c_end_vm_scope (unsigned int scope)
7730 if (label_context_stack_vm == NULL)
7731 return;
7732 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7733 c_switch_stack->blocked_vm = 0;
7734 /* We may have a number of nested scopes of identifiers with
7735 variably modified type, all at this depth. Pop each in turn. */
7736 while (label_context_stack_vm->scope == scope)
7738 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7740 /* It is no longer possible to jump to labels defined within this
7741 scope. */
7742 for (dlist = label_context_stack_vm->labels_def;
7743 dlist != NULL;
7744 dlist = dlist->next)
7746 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7748 /* It is again possible to define labels with a goto just outside
7749 this scope. */
7750 for (glist = label_context_stack_vm->next->labels_used;
7751 glist != NULL;
7752 glist = glist->next)
7754 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7755 glist_prev = glist;
7757 if (glist_prev != NULL)
7758 glist_prev->next = label_context_stack_vm->labels_used;
7759 else
7760 label_context_stack_vm->next->labels_used
7761 = label_context_stack_vm->labels_used;
7762 label_context_stack_vm = label_context_stack_vm->next;
7766 /* Begin and end compound statements. This is as simple as pushing
7767 and popping new statement lists from the tree. */
7769 tree
7770 c_begin_compound_stmt (bool do_scope)
7772 tree stmt = push_stmt_list ();
7773 if (do_scope)
7774 push_scope ();
7775 return stmt;
7778 tree
7779 c_end_compound_stmt (tree stmt, bool do_scope)
7781 tree block = NULL;
7783 if (do_scope)
7785 if (c_dialect_objc ())
7786 objc_clear_super_receiver ();
7787 block = pop_scope ();
7790 stmt = pop_stmt_list (stmt);
7791 stmt = c_build_bind_expr (block, stmt);
7793 /* If this compound statement is nested immediately inside a statement
7794 expression, then force a BIND_EXPR to be created. Otherwise we'll
7795 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7796 STATEMENT_LISTs merge, and thus we can lose track of what statement
7797 was really last. */
7798 if (cur_stmt_list
7799 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7800 && TREE_CODE (stmt) != BIND_EXPR)
7802 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7803 TREE_SIDE_EFFECTS (stmt) = 1;
7806 return stmt;
7809 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7810 when the current scope is exited. EH_ONLY is true when this is not
7811 meant to apply to normal control flow transfer. */
7813 void
7814 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7816 enum tree_code code;
7817 tree stmt, list;
7818 bool stmt_expr;
7820 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7821 stmt = build_stmt (code, NULL, cleanup);
7822 add_stmt (stmt);
7823 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7824 list = push_stmt_list ();
7825 TREE_OPERAND (stmt, 0) = list;
7826 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7829 /* Build a binary-operation expression without default conversions.
7830 CODE is the kind of expression to build.
7831 This function differs from `build' in several ways:
7832 the data type of the result is computed and recorded in it,
7833 warnings are generated if arg data types are invalid,
7834 special handling for addition and subtraction of pointers is known,
7835 and some optimization is done (operations on narrow ints
7836 are done in the narrower type when that gives the same result).
7837 Constant folding is also done before the result is returned.
7839 Note that the operands will never have enumeral types, or function
7840 or array types, because either they will have the default conversions
7841 performed or they have both just been converted to some other type in which
7842 the arithmetic is to be done. */
7844 tree
7845 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7846 int convert_p)
7848 tree type0, type1;
7849 enum tree_code code0, code1;
7850 tree op0, op1;
7851 const char *invalid_op_diag;
7853 /* Expression code to give to the expression when it is built.
7854 Normally this is CODE, which is what the caller asked for,
7855 but in some special cases we change it. */
7856 enum tree_code resultcode = code;
7858 /* Data type in which the computation is to be performed.
7859 In the simplest cases this is the common type of the arguments. */
7860 tree result_type = NULL;
7862 /* Nonzero means operands have already been type-converted
7863 in whatever way is necessary.
7864 Zero means they need to be converted to RESULT_TYPE. */
7865 int converted = 0;
7867 /* Nonzero means create the expression with this type, rather than
7868 RESULT_TYPE. */
7869 tree build_type = 0;
7871 /* Nonzero means after finally constructing the expression
7872 convert it to this type. */
7873 tree final_type = 0;
7875 /* Nonzero if this is an operation like MIN or MAX which can
7876 safely be computed in short if both args are promoted shorts.
7877 Also implies COMMON.
7878 -1 indicates a bitwise operation; this makes a difference
7879 in the exact conditions for when it is safe to do the operation
7880 in a narrower mode. */
7881 int shorten = 0;
7883 /* Nonzero if this is a comparison operation;
7884 if both args are promoted shorts, compare the original shorts.
7885 Also implies COMMON. */
7886 int short_compare = 0;
7888 /* Nonzero if this is a right-shift operation, which can be computed on the
7889 original short and then promoted if the operand is a promoted short. */
7890 int short_shift = 0;
7892 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7893 int common = 0;
7895 /* True means types are compatible as far as ObjC is concerned. */
7896 bool objc_ok;
7898 if (convert_p)
7900 op0 = default_conversion (orig_op0);
7901 op1 = default_conversion (orig_op1);
7903 else
7905 op0 = orig_op0;
7906 op1 = orig_op1;
7909 type0 = TREE_TYPE (op0);
7910 type1 = TREE_TYPE (op1);
7912 /* The expression codes of the data types of the arguments tell us
7913 whether the arguments are integers, floating, pointers, etc. */
7914 code0 = TREE_CODE (type0);
7915 code1 = TREE_CODE (type1);
7917 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7918 STRIP_TYPE_NOPS (op0);
7919 STRIP_TYPE_NOPS (op1);
7921 /* If an error was already reported for one of the arguments,
7922 avoid reporting another error. */
7924 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7925 return error_mark_node;
7927 if ((invalid_op_diag
7928 = targetm.invalid_binary_op (code, type0, type1)))
7930 error (invalid_op_diag);
7931 return error_mark_node;
7934 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7936 switch (code)
7938 case PLUS_EXPR:
7939 /* Handle the pointer + int case. */
7940 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7941 return pointer_int_sum (PLUS_EXPR, op0, op1);
7942 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7943 return pointer_int_sum (PLUS_EXPR, op1, op0);
7944 else
7945 common = 1;
7946 break;
7948 case MINUS_EXPR:
7949 /* Subtraction of two similar pointers.
7950 We must subtract them as integers, then divide by object size. */
7951 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7952 && comp_target_types (type0, type1))
7953 return pointer_diff (op0, op1);
7954 /* Handle pointer minus int. Just like pointer plus int. */
7955 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7956 return pointer_int_sum (MINUS_EXPR, op0, op1);
7957 else
7958 common = 1;
7959 break;
7961 case MULT_EXPR:
7962 common = 1;
7963 break;
7965 case TRUNC_DIV_EXPR:
7966 case CEIL_DIV_EXPR:
7967 case FLOOR_DIV_EXPR:
7968 case ROUND_DIV_EXPR:
7969 case EXACT_DIV_EXPR:
7970 warn_for_div_by_zero (op1);
7972 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7973 || code0 == FIXED_POINT_TYPE
7974 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7975 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7976 || code1 == FIXED_POINT_TYPE
7977 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7979 enum tree_code tcode0 = code0, tcode1 = code1;
7981 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7982 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7983 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7984 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7986 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
7987 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
7988 resultcode = RDIV_EXPR;
7989 else
7990 /* Although it would be tempting to shorten always here, that
7991 loses on some targets, since the modulo instruction is
7992 undefined if the quotient can't be represented in the
7993 computation mode. We shorten only if unsigned or if
7994 dividing by something we know != -1. */
7995 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7996 || (TREE_CODE (op1) == INTEGER_CST
7997 && !integer_all_onesp (op1)));
7998 common = 1;
8000 break;
8002 case BIT_AND_EXPR:
8003 case BIT_IOR_EXPR:
8004 case BIT_XOR_EXPR:
8005 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8006 shorten = -1;
8007 /* Allow vector types which are not floating point types. */
8008 else if (code0 == VECTOR_TYPE
8009 && code1 == VECTOR_TYPE
8010 && !VECTOR_FLOAT_TYPE_P (type0)
8011 && !VECTOR_FLOAT_TYPE_P (type1))
8012 common = 1;
8013 break;
8015 case TRUNC_MOD_EXPR:
8016 case FLOOR_MOD_EXPR:
8017 warn_for_div_by_zero (op1);
8019 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8021 /* Although it would be tempting to shorten always here, that loses
8022 on some targets, since the modulo instruction is undefined if the
8023 quotient can't be represented in the computation mode. We shorten
8024 only if unsigned or if dividing by something we know != -1. */
8025 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8026 || (TREE_CODE (op1) == INTEGER_CST
8027 && !integer_all_onesp (op1)));
8028 common = 1;
8030 break;
8032 case TRUTH_ANDIF_EXPR:
8033 case TRUTH_ORIF_EXPR:
8034 case TRUTH_AND_EXPR:
8035 case TRUTH_OR_EXPR:
8036 case TRUTH_XOR_EXPR:
8037 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8038 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8039 || code0 == FIXED_POINT_TYPE)
8040 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8041 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8042 || code1 == FIXED_POINT_TYPE))
8044 /* Result of these operations is always an int,
8045 but that does not mean the operands should be
8046 converted to ints! */
8047 result_type = integer_type_node;
8048 op0 = c_common_truthvalue_conversion (op0);
8049 op1 = c_common_truthvalue_conversion (op1);
8050 converted = 1;
8052 break;
8054 /* Shift operations: result has same type as first operand;
8055 always convert second operand to int.
8056 Also set SHORT_SHIFT if shifting rightward. */
8058 case RSHIFT_EXPR:
8059 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8060 && code1 == INTEGER_TYPE)
8062 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8064 if (tree_int_cst_sgn (op1) < 0)
8065 warning (0, "right shift count is negative");
8066 else
8068 if (!integer_zerop (op1))
8069 short_shift = 1;
8071 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8072 warning (0, "right shift count >= width of type");
8076 /* Use the type of the value to be shifted. */
8077 result_type = type0;
8078 /* Convert the shift-count to an integer, regardless of size
8079 of value being shifted. */
8080 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8081 op1 = convert (integer_type_node, op1);
8082 /* Avoid converting op1 to result_type later. */
8083 converted = 1;
8085 break;
8087 case LSHIFT_EXPR:
8088 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8089 && code1 == INTEGER_TYPE)
8091 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8093 if (tree_int_cst_sgn (op1) < 0)
8094 warning (0, "left shift count is negative");
8096 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8097 warning (0, "left shift count >= width of type");
8100 /* Use the type of the value to be shifted. */
8101 result_type = type0;
8102 /* Convert the shift-count to an integer, regardless of size
8103 of value being shifted. */
8104 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8105 op1 = convert (integer_type_node, op1);
8106 /* Avoid converting op1 to result_type later. */
8107 converted = 1;
8109 break;
8111 case EQ_EXPR:
8112 case NE_EXPR:
8113 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8114 warning (OPT_Wfloat_equal,
8115 "comparing floating point with == or != is unsafe");
8116 /* Result of comparison is always int,
8117 but don't convert the args to int! */
8118 build_type = integer_type_node;
8119 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8120 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8121 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8122 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8123 short_compare = 1;
8124 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8126 tree tt0 = TREE_TYPE (type0);
8127 tree tt1 = TREE_TYPE (type1);
8128 /* Anything compares with void *. void * compares with anything.
8129 Otherwise, the targets must be compatible
8130 and both must be object or both incomplete. */
8131 if (comp_target_types (type0, type1))
8132 result_type = common_pointer_type (type0, type1);
8133 else if (VOID_TYPE_P (tt0))
8135 /* op0 != orig_op0 detects the case of something
8136 whose value is 0 but which isn't a valid null ptr const. */
8137 if (pedantic && !null_pointer_constant_p (orig_op0)
8138 && TREE_CODE (tt1) == FUNCTION_TYPE)
8139 pedwarn ("ISO C forbids comparison of %<void *%>"
8140 " with function pointer");
8142 else if (VOID_TYPE_P (tt1))
8144 if (pedantic && !null_pointer_constant_p (orig_op1)
8145 && TREE_CODE (tt0) == FUNCTION_TYPE)
8146 pedwarn ("ISO C forbids comparison of %<void *%>"
8147 " with function pointer");
8149 else
8150 /* Avoid warning about the volatile ObjC EH puts on decls. */
8151 if (!objc_ok)
8152 pedwarn ("comparison of distinct pointer types lacks a cast");
8154 if (result_type == NULL_TREE)
8155 result_type = ptr_type_node;
8157 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8159 if (TREE_CODE (op0) == ADDR_EXPR
8160 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
8161 warning (OPT_Waddress, "the address of %qD will never be NULL",
8162 TREE_OPERAND (op0, 0));
8163 result_type = type0;
8165 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8167 if (TREE_CODE (op1) == ADDR_EXPR
8168 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
8169 warning (OPT_Waddress, "the address of %qD will never be NULL",
8170 TREE_OPERAND (op1, 0));
8171 result_type = type1;
8173 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8175 result_type = type0;
8176 pedwarn ("comparison between pointer and integer");
8178 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8180 result_type = type1;
8181 pedwarn ("comparison between pointer and integer");
8183 break;
8185 case LE_EXPR:
8186 case GE_EXPR:
8187 case LT_EXPR:
8188 case GT_EXPR:
8189 build_type = integer_type_node;
8190 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8191 || code0 == FIXED_POINT_TYPE)
8192 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8193 || code1 == FIXED_POINT_TYPE))
8194 short_compare = 1;
8195 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8197 if (comp_target_types (type0, type1))
8199 result_type = common_pointer_type (type0, type1);
8200 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8201 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8202 pedwarn ("comparison of complete and incomplete pointers");
8203 else if (pedantic
8204 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8205 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8207 else
8209 result_type = ptr_type_node;
8210 pedwarn ("comparison of distinct pointer types lacks a cast");
8213 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8215 result_type = type0;
8216 if (pedantic || extra_warnings)
8217 pedwarn ("ordered comparison of pointer with integer zero");
8219 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8221 result_type = type1;
8222 if (pedantic)
8223 pedwarn ("ordered comparison of pointer with integer zero");
8225 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8227 result_type = type0;
8228 pedwarn ("comparison between pointer and integer");
8230 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8232 result_type = type1;
8233 pedwarn ("comparison between pointer and integer");
8235 break;
8237 default:
8238 gcc_unreachable ();
8241 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8242 return error_mark_node;
8244 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8245 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8246 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8247 TREE_TYPE (type1))))
8249 binary_op_error (code, type0, type1);
8250 return error_mark_node;
8253 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8254 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
8256 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8257 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
8259 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8261 if (shorten || common || short_compare)
8263 result_type = c_common_type (type0, type1);
8264 if (result_type == error_mark_node)
8265 return error_mark_node;
8268 /* For certain operations (which identify themselves by shorten != 0)
8269 if both args were extended from the same smaller type,
8270 do the arithmetic in that type and then extend.
8272 shorten !=0 and !=1 indicates a bitwise operation.
8273 For them, this optimization is safe only if
8274 both args are zero-extended or both are sign-extended.
8275 Otherwise, we might change the result.
8276 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8277 but calculated in (unsigned short) it would be (unsigned short)-1. */
8279 if (shorten && none_complex)
8281 int unsigned0, unsigned1;
8282 tree arg0, arg1;
8283 int uns;
8284 tree type;
8286 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8287 excessive narrowing when we call get_narrower below. For
8288 example, suppose that OP0 is of unsigned int extended
8289 from signed char and that RESULT_TYPE is long long int.
8290 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8291 like
8293 (long long int) (unsigned int) signed_char
8295 which get_narrower would narrow down to
8297 (unsigned int) signed char
8299 If we do not cast OP0 first, get_narrower would return
8300 signed_char, which is inconsistent with the case of the
8301 explicit cast. */
8302 op0 = convert (result_type, op0);
8303 op1 = convert (result_type, op1);
8305 arg0 = get_narrower (op0, &unsigned0);
8306 arg1 = get_narrower (op1, &unsigned1);
8308 /* UNS is 1 if the operation to be done is an unsigned one. */
8309 uns = TYPE_UNSIGNED (result_type);
8311 final_type = result_type;
8313 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8314 but it *requires* conversion to FINAL_TYPE. */
8316 if ((TYPE_PRECISION (TREE_TYPE (op0))
8317 == TYPE_PRECISION (TREE_TYPE (arg0)))
8318 && TREE_TYPE (op0) != final_type)
8319 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8320 if ((TYPE_PRECISION (TREE_TYPE (op1))
8321 == TYPE_PRECISION (TREE_TYPE (arg1)))
8322 && TREE_TYPE (op1) != final_type)
8323 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8325 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8327 /* For bitwise operations, signedness of nominal type
8328 does not matter. Consider only how operands were extended. */
8329 if (shorten == -1)
8330 uns = unsigned0;
8332 /* Note that in all three cases below we refrain from optimizing
8333 an unsigned operation on sign-extended args.
8334 That would not be valid. */
8336 /* Both args variable: if both extended in same way
8337 from same width, do it in that width.
8338 Do it unsigned if args were zero-extended. */
8339 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8340 < TYPE_PRECISION (result_type))
8341 && (TYPE_PRECISION (TREE_TYPE (arg1))
8342 == TYPE_PRECISION (TREE_TYPE (arg0)))
8343 && unsigned0 == unsigned1
8344 && (unsigned0 || !uns))
8345 result_type
8346 = c_common_signed_or_unsigned_type
8347 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8348 else if (TREE_CODE (arg0) == INTEGER_CST
8349 && (unsigned1 || !uns)
8350 && (TYPE_PRECISION (TREE_TYPE (arg1))
8351 < TYPE_PRECISION (result_type))
8352 && (type
8353 = c_common_signed_or_unsigned_type (unsigned1,
8354 TREE_TYPE (arg1)))
8355 && !POINTER_TYPE_P (type)
8356 && int_fits_type_p (arg0, type))
8357 result_type = type;
8358 else if (TREE_CODE (arg1) == INTEGER_CST
8359 && (unsigned0 || !uns)
8360 && (TYPE_PRECISION (TREE_TYPE (arg0))
8361 < TYPE_PRECISION (result_type))
8362 && (type
8363 = c_common_signed_or_unsigned_type (unsigned0,
8364 TREE_TYPE (arg0)))
8365 && !POINTER_TYPE_P (type)
8366 && int_fits_type_p (arg1, type))
8367 result_type = type;
8370 /* Shifts can be shortened if shifting right. */
8372 if (short_shift)
8374 int unsigned_arg;
8375 tree arg0 = get_narrower (op0, &unsigned_arg);
8377 final_type = result_type;
8379 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8380 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8382 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8383 /* We can shorten only if the shift count is less than the
8384 number of bits in the smaller type size. */
8385 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8386 /* We cannot drop an unsigned shift after sign-extension. */
8387 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8389 /* Do an unsigned shift if the operand was zero-extended. */
8390 result_type
8391 = c_common_signed_or_unsigned_type (unsigned_arg,
8392 TREE_TYPE (arg0));
8393 /* Convert value-to-be-shifted to that type. */
8394 if (TREE_TYPE (op0) != result_type)
8395 op0 = convert (result_type, op0);
8396 converted = 1;
8400 /* Comparison operations are shortened too but differently.
8401 They identify themselves by setting short_compare = 1. */
8403 if (short_compare)
8405 /* Don't write &op0, etc., because that would prevent op0
8406 from being kept in a register.
8407 Instead, make copies of the our local variables and
8408 pass the copies by reference, then copy them back afterward. */
8409 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8410 enum tree_code xresultcode = resultcode;
8411 tree val
8412 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8414 if (val != 0)
8415 return val;
8417 op0 = xop0, op1 = xop1;
8418 converted = 1;
8419 resultcode = xresultcode;
8421 if (warn_sign_compare && skip_evaluation == 0)
8423 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8424 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8425 int unsignedp0, unsignedp1;
8426 tree primop0 = get_narrower (op0, &unsignedp0);
8427 tree primop1 = get_narrower (op1, &unsignedp1);
8429 xop0 = orig_op0;
8430 xop1 = orig_op1;
8431 STRIP_TYPE_NOPS (xop0);
8432 STRIP_TYPE_NOPS (xop1);
8434 /* Give warnings for comparisons between signed and unsigned
8435 quantities that may fail.
8437 Do the checking based on the original operand trees, so that
8438 casts will be considered, but default promotions won't be.
8440 Do not warn if the comparison is being done in a signed type,
8441 since the signed type will only be chosen if it can represent
8442 all the values of the unsigned type. */
8443 if (!TYPE_UNSIGNED (result_type))
8444 /* OK */;
8445 /* Do not warn if both operands are the same signedness. */
8446 else if (op0_signed == op1_signed)
8447 /* OK */;
8448 else
8450 tree sop, uop;
8451 bool ovf;
8453 if (op0_signed)
8454 sop = xop0, uop = xop1;
8455 else
8456 sop = xop1, uop = xop0;
8458 /* Do not warn if the signed quantity is an
8459 unsuffixed integer literal (or some static
8460 constant expression involving such literals or a
8461 conditional expression involving such literals)
8462 and it is non-negative. */
8463 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
8464 /* OK */;
8465 /* Do not warn if the comparison is an equality operation,
8466 the unsigned quantity is an integral constant, and it
8467 would fit in the result if the result were signed. */
8468 else if (TREE_CODE (uop) == INTEGER_CST
8469 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8470 && int_fits_type_p
8471 (uop, c_common_signed_type (result_type)))
8472 /* OK */;
8473 /* Do not warn if the unsigned quantity is an enumeration
8474 constant and its maximum value would fit in the result
8475 if the result were signed. */
8476 else if (TREE_CODE (uop) == INTEGER_CST
8477 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8478 && int_fits_type_p
8479 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8480 c_common_signed_type (result_type)))
8481 /* OK */;
8482 else
8483 warning (0, "comparison between signed and unsigned");
8486 /* Warn if two unsigned values are being compared in a size
8487 larger than their original size, and one (and only one) is the
8488 result of a `~' operator. This comparison will always fail.
8490 Also warn if one operand is a constant, and the constant
8491 does not have all bits set that are set in the ~ operand
8492 when it is extended. */
8494 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8495 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8497 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8498 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8499 &unsignedp0);
8500 else
8501 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8502 &unsignedp1);
8504 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8506 tree primop;
8507 HOST_WIDE_INT constant, mask;
8508 int unsignedp, bits;
8510 if (host_integerp (primop0, 0))
8512 primop = primop1;
8513 unsignedp = unsignedp1;
8514 constant = tree_low_cst (primop0, 0);
8516 else
8518 primop = primop0;
8519 unsignedp = unsignedp0;
8520 constant = tree_low_cst (primop1, 0);
8523 bits = TYPE_PRECISION (TREE_TYPE (primop));
8524 if (bits < TYPE_PRECISION (result_type)
8525 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8527 mask = (~(HOST_WIDE_INT) 0) << bits;
8528 if ((mask & constant) != mask)
8529 warning (0, "comparison of promoted ~unsigned with constant");
8532 else if (unsignedp0 && unsignedp1
8533 && (TYPE_PRECISION (TREE_TYPE (primop0))
8534 < TYPE_PRECISION (result_type))
8535 && (TYPE_PRECISION (TREE_TYPE (primop1))
8536 < TYPE_PRECISION (result_type)))
8537 warning (0, "comparison of promoted ~unsigned with unsigned");
8543 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8544 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8545 Then the expression will be built.
8546 It will be given type FINAL_TYPE if that is nonzero;
8547 otherwise, it will be given type RESULT_TYPE. */
8549 if (!result_type)
8551 binary_op_error (code, TREE_TYPE (op0), TREE_TYPE (op1));
8552 return error_mark_node;
8555 if (!converted)
8557 if (TREE_TYPE (op0) != result_type)
8558 op0 = convert_and_check (result_type, op0);
8559 if (TREE_TYPE (op1) != result_type)
8560 op1 = convert_and_check (result_type, op1);
8562 /* This can happen if one operand has a vector type, and the other
8563 has a different type. */
8564 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8565 return error_mark_node;
8568 if (build_type == NULL_TREE)
8569 build_type = result_type;
8572 /* Treat expressions in initializers specially as they can't trap. */
8573 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8574 build_type,
8575 op0, op1)
8576 : fold_build2 (resultcode, build_type,
8577 op0, op1);
8579 if (final_type != 0)
8580 result = convert (final_type, result);
8581 return result;
8586 /* Convert EXPR to be a truth-value, validating its type for this
8587 purpose. */
8589 tree
8590 c_objc_common_truthvalue_conversion (tree expr)
8592 switch (TREE_CODE (TREE_TYPE (expr)))
8594 case ARRAY_TYPE:
8595 error ("used array that cannot be converted to pointer where scalar is required");
8596 return error_mark_node;
8598 case RECORD_TYPE:
8599 error ("used struct type value where scalar is required");
8600 return error_mark_node;
8602 case UNION_TYPE:
8603 error ("used union type value where scalar is required");
8604 return error_mark_node;
8606 case FUNCTION_TYPE:
8607 gcc_unreachable ();
8609 default:
8610 break;
8613 /* ??? Should we also give an error for void and vectors rather than
8614 leaving those to give errors later? */
8615 return c_common_truthvalue_conversion (expr);
8619 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8620 required. */
8622 tree
8623 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8624 bool *ti ATTRIBUTE_UNUSED, bool *se)
8626 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8628 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8629 /* Executing a compound literal inside a function reinitializes
8630 it. */
8631 if (!TREE_STATIC (decl))
8632 *se = true;
8633 return decl;
8635 else
8636 return expr;
8639 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8641 tree
8642 c_begin_omp_parallel (void)
8644 tree block;
8646 keep_next_level ();
8647 block = c_begin_compound_stmt (true);
8649 return block;
8652 tree
8653 c_finish_omp_parallel (tree clauses, tree block)
8655 tree stmt;
8657 block = c_end_compound_stmt (block, true);
8659 stmt = make_node (OMP_PARALLEL);
8660 TREE_TYPE (stmt) = void_type_node;
8661 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8662 OMP_PARALLEL_BODY (stmt) = block;
8664 return add_stmt (stmt);
8667 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
8668 Remove any elements from the list that are invalid. */
8670 tree
8671 c_finish_omp_clauses (tree clauses)
8673 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8674 tree c, t, *pc = &clauses;
8675 const char *name;
8677 bitmap_obstack_initialize (NULL);
8678 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8679 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8680 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8682 for (pc = &clauses, c = clauses; c ; c = *pc)
8684 bool remove = false;
8685 bool need_complete = false;
8686 bool need_implicitly_determined = false;
8688 switch (OMP_CLAUSE_CODE (c))
8690 case OMP_CLAUSE_SHARED:
8691 name = "shared";
8692 need_implicitly_determined = true;
8693 goto check_dup_generic;
8695 case OMP_CLAUSE_PRIVATE:
8696 name = "private";
8697 need_complete = true;
8698 need_implicitly_determined = true;
8699 goto check_dup_generic;
8701 case OMP_CLAUSE_REDUCTION:
8702 name = "reduction";
8703 need_implicitly_determined = true;
8704 t = OMP_CLAUSE_DECL (c);
8705 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8706 || POINTER_TYPE_P (TREE_TYPE (t)))
8708 error ("%qE has invalid type for %<reduction%>", t);
8709 remove = true;
8711 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8713 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8714 const char *r_name = NULL;
8716 switch (r_code)
8718 case PLUS_EXPR:
8719 case MULT_EXPR:
8720 case MINUS_EXPR:
8721 break;
8722 case BIT_AND_EXPR:
8723 r_name = "&";
8724 break;
8725 case BIT_XOR_EXPR:
8726 r_name = "^";
8727 break;
8728 case BIT_IOR_EXPR:
8729 r_name = "|";
8730 break;
8731 case TRUTH_ANDIF_EXPR:
8732 r_name = "&&";
8733 break;
8734 case TRUTH_ORIF_EXPR:
8735 r_name = "||";
8736 break;
8737 default:
8738 gcc_unreachable ();
8740 if (r_name)
8742 error ("%qE has invalid type for %<reduction(%s)%>",
8743 t, r_name);
8744 remove = true;
8747 goto check_dup_generic;
8749 case OMP_CLAUSE_COPYPRIVATE:
8750 name = "copyprivate";
8751 goto check_dup_generic;
8753 case OMP_CLAUSE_COPYIN:
8754 name = "copyin";
8755 t = OMP_CLAUSE_DECL (c);
8756 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8758 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8759 remove = true;
8761 goto check_dup_generic;
8763 check_dup_generic:
8764 t = OMP_CLAUSE_DECL (c);
8765 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8767 error ("%qE is not a variable in clause %qs", t, name);
8768 remove = true;
8770 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8771 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8772 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8774 error ("%qE appears more than once in data clauses", t);
8775 remove = true;
8777 else
8778 bitmap_set_bit (&generic_head, DECL_UID (t));
8779 break;
8781 case OMP_CLAUSE_FIRSTPRIVATE:
8782 name = "firstprivate";
8783 t = OMP_CLAUSE_DECL (c);
8784 need_complete = true;
8785 need_implicitly_determined = true;
8786 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8788 error ("%qE is not a variable in clause %<firstprivate%>", t);
8789 remove = true;
8791 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8792 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8794 error ("%qE appears more than once in data clauses", t);
8795 remove = true;
8797 else
8798 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8799 break;
8801 case OMP_CLAUSE_LASTPRIVATE:
8802 name = "lastprivate";
8803 t = OMP_CLAUSE_DECL (c);
8804 need_complete = true;
8805 need_implicitly_determined = true;
8806 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8808 error ("%qE is not a variable in clause %<lastprivate%>", t);
8809 remove = true;
8811 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8812 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8814 error ("%qE appears more than once in data clauses", t);
8815 remove = true;
8817 else
8818 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8819 break;
8821 case OMP_CLAUSE_IF:
8822 case OMP_CLAUSE_NUM_THREADS:
8823 case OMP_CLAUSE_SCHEDULE:
8824 case OMP_CLAUSE_NOWAIT:
8825 case OMP_CLAUSE_ORDERED:
8826 case OMP_CLAUSE_DEFAULT:
8827 pc = &OMP_CLAUSE_CHAIN (c);
8828 continue;
8830 default:
8831 gcc_unreachable ();
8834 if (!remove)
8836 t = OMP_CLAUSE_DECL (c);
8838 if (need_complete)
8840 t = require_complete_type (t);
8841 if (t == error_mark_node)
8842 remove = true;
8845 if (need_implicitly_determined)
8847 const char *share_name = NULL;
8849 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8850 share_name = "threadprivate";
8851 else switch (c_omp_predetermined_sharing (t))
8853 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8854 break;
8855 case OMP_CLAUSE_DEFAULT_SHARED:
8856 share_name = "shared";
8857 break;
8858 case OMP_CLAUSE_DEFAULT_PRIVATE:
8859 share_name = "private";
8860 break;
8861 default:
8862 gcc_unreachable ();
8864 if (share_name)
8866 error ("%qE is predetermined %qs for %qs",
8867 t, share_name, name);
8868 remove = true;
8873 if (remove)
8874 *pc = OMP_CLAUSE_CHAIN (c);
8875 else
8876 pc = &OMP_CLAUSE_CHAIN (c);
8879 bitmap_obstack_release (NULL);
8880 return clauses;