sh-modes.def: comment pasto fix.
[official-gcc.git] / gcc / c-typeck.c
blobff27c973094005f09d0f5eed1ec4896114257e42
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
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file is part of the C front end.
25 It contains routines to build C expressions given their operands,
26 including computing the types of the result, C-specific error checks,
27 and some optimization. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "ggc.h"
44 #include "target.h"
45 #include "tree-iterator.h"
46 #include "tree-gimple.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
51 enum impl_conv {
52 ic_argpass,
53 ic_argpass_nonproto,
54 ic_assign,
55 ic_init,
56 ic_return
59 /* The level of nesting inside "__alignof__". */
60 int in_alignof;
62 /* The level of nesting inside "sizeof". */
63 int in_sizeof;
65 /* The level of nesting inside "typeof". */
66 int in_typeof;
68 struct c_label_context_se *label_context_stack_se;
69 struct c_label_context_vm *label_context_stack_vm;
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (tree, tree);
81 static int comp_target_types (tree, tree);
82 static int function_types_compatible_p (tree, tree);
83 static int type_lists_compatible_p (tree, tree);
84 static tree decl_constant_value_for_broken_optimization (tree);
85 static tree lookup_field (tree, tree);
86 static tree convert_arguments (tree, tree, tree, tree);
87 static tree pointer_diff (tree, tree);
88 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
89 int);
90 static tree valid_compound_expr_initializer (tree, tree);
91 static void push_string (const char *);
92 static void push_member_name (tree);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
109 static int comptypes_internal (tree, tree);
111 /* Return true if EXP is a null pointer constant, false otherwise. */
113 static bool
114 null_pointer_constant_p (tree expr)
116 /* This should really operate on c_expr structures, but they aren't
117 yet available everywhere required. */
118 tree type = TREE_TYPE (expr);
119 return (TREE_CODE (expr) == INTEGER_CST
120 && !TREE_CONSTANT_OVERFLOW (expr)
121 && integer_zerop (expr)
122 && (INTEGRAL_TYPE_P (type)
123 || (TREE_CODE (type) == POINTER_TYPE
124 && VOID_TYPE_P (TREE_TYPE (type))
125 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
127 \f/* This is a cache to hold if two types are compatible or not. */
129 struct tagged_tu_seen_cache {
130 const struct tagged_tu_seen_cache * next;
131 tree t1;
132 tree t2;
133 /* The return value of tagged_types_tu_compatible_p if we had seen
134 these two types already. */
135 int val;
138 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
139 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
141 /* Do `exp = require_complete_type (exp);' to make sure exp
142 does not have an incomplete type. (That includes void types.) */
144 tree
145 require_complete_type (tree value)
147 tree type = TREE_TYPE (value);
149 if (value == error_mark_node || type == error_mark_node)
150 return error_mark_node;
152 /* First, detect a valid value with a complete type. */
153 if (COMPLETE_TYPE_P (type))
154 return value;
156 c_incomplete_type_error (value, type);
157 return error_mark_node;
160 /* Print an error message for invalid use of an incomplete type.
161 VALUE is the expression that was used (or 0 if that isn't known)
162 and TYPE is the type that was invalid. */
164 void
165 c_incomplete_type_error (tree value, tree type)
167 const char *type_code_string;
169 /* Avoid duplicate error message. */
170 if (TREE_CODE (type) == ERROR_MARK)
171 return;
173 if (value != 0 && (TREE_CODE (value) == VAR_DECL
174 || TREE_CODE (value) == PARM_DECL))
175 error ("%qD has an incomplete type", value);
176 else
178 retry:
179 /* We must print an error message. Be clever about what it says. */
181 switch (TREE_CODE (type))
183 case RECORD_TYPE:
184 type_code_string = "struct";
185 break;
187 case UNION_TYPE:
188 type_code_string = "union";
189 break;
191 case ENUMERAL_TYPE:
192 type_code_string = "enum";
193 break;
195 case VOID_TYPE:
196 error ("invalid use of void expression");
197 return;
199 case ARRAY_TYPE:
200 if (TYPE_DOMAIN (type))
202 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
204 error ("invalid use of flexible array member");
205 return;
207 type = TREE_TYPE (type);
208 goto retry;
210 error ("invalid use of array with unspecified bounds");
211 return;
213 default:
214 gcc_unreachable ();
217 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
218 error ("invalid use of undefined type %<%s %E%>",
219 type_code_string, TYPE_NAME (type));
220 else
221 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
222 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
226 /* Given a type, apply default promotions wrt unnamed function
227 arguments and return the new type. */
229 tree
230 c_type_promotes_to (tree type)
232 if (TYPE_MAIN_VARIANT (type) == float_type_node)
233 return double_type_node;
235 if (c_promoting_integer_type_p (type))
237 /* Preserve unsignedness if not really getting any wider. */
238 if (TYPE_UNSIGNED (type)
239 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
240 return unsigned_type_node;
241 return integer_type_node;
244 return type;
247 /* Return a variant of TYPE which has all the type qualifiers of LIKE
248 as well as those of TYPE. */
250 static tree
251 qualify_type (tree type, tree like)
253 return c_build_qualified_type (type,
254 TYPE_QUALS (type) | TYPE_QUALS (like));
257 /* Return true iff the given tree T is a variable length array. */
259 bool
260 c_vla_type_p (tree t)
262 if (TREE_CODE (t) == ARRAY_TYPE
263 && C_TYPE_VARIABLE_SIZE (t))
264 return true;
265 return false;
268 /* Return the composite type of two compatible types.
270 We assume that comptypes has already been done and returned
271 nonzero; if that isn't so, this may crash. In particular, we
272 assume that qualifiers match. */
274 tree
275 composite_type (tree t1, tree t2)
277 enum tree_code code1;
278 enum tree_code code2;
279 tree attributes;
281 /* Save time if the two types are the same. */
283 if (t1 == t2) return t1;
285 /* If one type is nonsense, use the other. */
286 if (t1 == error_mark_node)
287 return t2;
288 if (t2 == error_mark_node)
289 return t1;
291 code1 = TREE_CODE (t1);
292 code2 = TREE_CODE (t2);
294 /* Merge the attributes. */
295 attributes = targetm.merge_type_attributes (t1, t2);
297 /* If one is an enumerated type and the other is the compatible
298 integer type, the composite type might be either of the two
299 (DR#013 question 3). For consistency, use the enumerated type as
300 the composite type. */
302 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
303 return t1;
304 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
305 return t2;
307 gcc_assert (code1 == code2);
309 switch (code1)
311 case POINTER_TYPE:
312 /* For two pointers, do this recursively on the target type. */
314 tree pointed_to_1 = TREE_TYPE (t1);
315 tree pointed_to_2 = TREE_TYPE (t2);
316 tree target = composite_type (pointed_to_1, pointed_to_2);
317 t1 = build_pointer_type (target);
318 t1 = build_type_attribute_variant (t1, attributes);
319 return qualify_type (t1, t2);
322 case ARRAY_TYPE:
324 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
325 int quals;
326 tree unqual_elt;
327 tree d1 = TYPE_DOMAIN (t1);
328 tree d2 = TYPE_DOMAIN (t2);
329 bool d1_variable, d2_variable;
330 bool d1_zero, d2_zero;
332 /* We should not have any type quals on arrays at all. */
333 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
335 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
336 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
338 d1_variable = (!d1_zero
339 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
340 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
341 d2_variable = (!d2_zero
342 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
343 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
344 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
345 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
347 /* Save space: see if the result is identical to one of the args. */
348 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
349 && (d2_variable || d2_zero || !d1_variable))
350 return build_type_attribute_variant (t1, attributes);
351 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
352 && (d1_variable || d1_zero || !d2_variable))
353 return build_type_attribute_variant (t2, attributes);
355 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
356 return build_type_attribute_variant (t1, attributes);
357 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
358 return build_type_attribute_variant (t2, attributes);
360 /* Merge the element types, and have a size if either arg has
361 one. We may have qualifiers on the element types. To set
362 up TYPE_MAIN_VARIANT correctly, we need to form the
363 composite of the unqualified types and add the qualifiers
364 back at the end. */
365 quals = TYPE_QUALS (strip_array_types (elt));
366 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
367 t1 = build_array_type (unqual_elt,
368 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
369 && (d2_variable
370 || d2_zero
371 || !d1_variable))
372 ? t1
373 : t2));
374 t1 = c_build_qualified_type (t1, quals);
375 return build_type_attribute_variant (t1, attributes);
378 case FUNCTION_TYPE:
379 /* Function types: prefer the one that specified arg types.
380 If both do, merge the arg types. Also merge the return types. */
382 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
383 tree p1 = TYPE_ARG_TYPES (t1);
384 tree p2 = TYPE_ARG_TYPES (t2);
385 int len;
386 tree newargs, n;
387 int i;
389 /* Save space: see if the result is identical to one of the args. */
390 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
391 return build_type_attribute_variant (t1, attributes);
392 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
393 return build_type_attribute_variant (t2, attributes);
395 /* Simple way if one arg fails to specify argument types. */
396 if (TYPE_ARG_TYPES (t1) == 0)
398 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
402 if (TYPE_ARG_TYPES (t2) == 0)
404 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
409 /* If both args specify argument types, we must merge the two
410 lists, argument by argument. */
411 /* Tell global_bindings_p to return false so that variable_size
412 doesn't die on VLAs in parameter types. */
413 c_override_global_bindings_to_false = true;
415 len = list_length (p1);
416 newargs = 0;
418 for (i = 0; i < len; i++)
419 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
421 n = newargs;
423 for (; p1;
424 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
426 /* A null type means arg type is not specified.
427 Take whatever the other function type has. */
428 if (TREE_VALUE (p1) == 0)
430 TREE_VALUE (n) = TREE_VALUE (p2);
431 goto parm_done;
433 if (TREE_VALUE (p2) == 0)
435 TREE_VALUE (n) = TREE_VALUE (p1);
436 goto parm_done;
439 /* Given wait (union {union wait *u; int *i} *)
440 and wait (union wait *),
441 prefer union wait * as type of parm. */
442 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
443 && TREE_VALUE (p1) != TREE_VALUE (p2))
445 tree memb;
446 tree mv2 = TREE_VALUE (p2);
447 if (mv2 && mv2 != error_mark_node
448 && TREE_CODE (mv2) != ARRAY_TYPE)
449 mv2 = TYPE_MAIN_VARIANT (mv2);
450 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
451 memb; memb = TREE_CHAIN (memb))
453 tree mv3 = TREE_TYPE (memb);
454 if (mv3 && mv3 != error_mark_node
455 && TREE_CODE (mv3) != ARRAY_TYPE)
456 mv3 = TYPE_MAIN_VARIANT (mv3);
457 if (comptypes (mv3, mv2))
459 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
460 TREE_VALUE (p2));
461 if (pedantic)
462 pedwarn ("function types not truly compatible in ISO C");
463 goto parm_done;
467 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
468 && TREE_VALUE (p2) != TREE_VALUE (p1))
470 tree memb;
471 tree mv1 = TREE_VALUE (p1);
472 if (mv1 && mv1 != error_mark_node
473 && TREE_CODE (mv1) != ARRAY_TYPE)
474 mv1 = TYPE_MAIN_VARIANT (mv1);
475 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
476 memb; memb = TREE_CHAIN (memb))
478 tree mv3 = TREE_TYPE (memb);
479 if (mv3 && mv3 != error_mark_node
480 && TREE_CODE (mv3) != ARRAY_TYPE)
481 mv3 = TYPE_MAIN_VARIANT (mv3);
482 if (comptypes (mv3, mv1))
484 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
485 TREE_VALUE (p1));
486 if (pedantic)
487 pedwarn ("function types not truly compatible in ISO C");
488 goto parm_done;
492 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
493 parm_done: ;
496 c_override_global_bindings_to_false = false;
497 t1 = build_function_type (valtype, newargs);
498 t1 = qualify_type (t1, t2);
499 /* ... falls through ... */
502 default:
503 return build_type_attribute_variant (t1, attributes);
508 /* Return the type of a conditional expression between pointers to
509 possibly differently qualified versions of compatible types.
511 We assume that comp_target_types has already been done and returned
512 nonzero; if that isn't so, this may crash. */
514 static tree
515 common_pointer_type (tree t1, tree t2)
517 tree attributes;
518 tree pointed_to_1, mv1;
519 tree pointed_to_2, mv2;
520 tree target;
522 /* Save time if the two types are the same. */
524 if (t1 == t2) return t1;
526 /* If one type is nonsense, use the other. */
527 if (t1 == error_mark_node)
528 return t2;
529 if (t2 == error_mark_node)
530 return t1;
532 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
533 && TREE_CODE (t2) == POINTER_TYPE);
535 /* Merge the attributes. */
536 attributes = targetm.merge_type_attributes (t1, t2);
538 /* Find the composite type of the target types, and combine the
539 qualifiers of the two types' targets. Do not lose qualifiers on
540 array element types by taking the TYPE_MAIN_VARIANT. */
541 mv1 = pointed_to_1 = TREE_TYPE (t1);
542 mv2 = pointed_to_2 = TREE_TYPE (t2);
543 if (TREE_CODE (mv1) != ARRAY_TYPE)
544 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
545 if (TREE_CODE (mv2) != ARRAY_TYPE)
546 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
547 target = composite_type (mv1, mv2);
548 t1 = build_pointer_type (c_build_qualified_type
549 (target,
550 TYPE_QUALS (pointed_to_1) |
551 TYPE_QUALS (pointed_to_2)));
552 return build_type_attribute_variant (t1, attributes);
555 /* Return the common type for two arithmetic types under the usual
556 arithmetic conversions. The default conversions have already been
557 applied, and enumerated types converted to their compatible integer
558 types. The resulting type is unqualified and has no attributes.
560 This is the type for the result of most arithmetic operations
561 if the operands have the given two types. */
563 static tree
564 c_common_type (tree t1, tree t2)
566 enum tree_code code1;
567 enum tree_code code2;
569 /* If one type is nonsense, use the other. */
570 if (t1 == error_mark_node)
571 return t2;
572 if (t2 == error_mark_node)
573 return t1;
575 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
576 t1 = TYPE_MAIN_VARIANT (t1);
578 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
579 t2 = TYPE_MAIN_VARIANT (t2);
581 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
582 t1 = build_type_attribute_variant (t1, NULL_TREE);
584 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
585 t2 = build_type_attribute_variant (t2, NULL_TREE);
587 /* Save time if the two types are the same. */
589 if (t1 == t2) return t1;
591 code1 = TREE_CODE (t1);
592 code2 = TREE_CODE (t2);
594 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
595 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
596 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
597 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
599 /* When one operand is a decimal float type, the other operand cannot be
600 a generic float type or a complex type. We also disallow vector types
601 here. */
602 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
603 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
605 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
607 error ("can%'t mix operands of decimal float and vector types");
608 return error_mark_node;
610 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
612 error ("can%'t mix operands of decimal float and complex types");
613 return error_mark_node;
615 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
617 error ("can%'t mix operands of decimal float and other float types");
618 return error_mark_node;
622 /* If one type is a vector type, return that type. (How the usual
623 arithmetic conversions apply to the vector types extension is not
624 precisely specified.) */
625 if (code1 == VECTOR_TYPE)
626 return t1;
628 if (code2 == VECTOR_TYPE)
629 return t2;
631 /* If one type is complex, form the common type of the non-complex
632 components, then make that complex. Use T1 or T2 if it is the
633 required type. */
634 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
636 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
637 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
638 tree subtype = c_common_type (subtype1, subtype2);
640 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
641 return t1;
642 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
643 return t2;
644 else
645 return build_complex_type (subtype);
648 /* If only one is real, use it as the result. */
650 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
651 return t1;
653 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
654 return t2;
656 /* If both are real and either are decimal floating point types, use
657 the decimal floating point type with the greater precision. */
659 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
661 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
662 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
663 return dfloat128_type_node;
664 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
665 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
666 return dfloat64_type_node;
667 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
668 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
669 return dfloat32_type_node;
672 /* Both real or both integers; use the one with greater precision. */
674 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
675 return t1;
676 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
677 return t2;
679 /* Same precision. Prefer long longs to longs to ints when the
680 same precision, following the C99 rules on integer type rank
681 (which are equivalent to the C90 rules for C90 types). */
683 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
684 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
685 return long_long_unsigned_type_node;
687 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
688 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
690 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
691 return long_long_unsigned_type_node;
692 else
693 return long_long_integer_type_node;
696 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
697 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
698 return long_unsigned_type_node;
700 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
701 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
703 /* But preserve unsignedness from the other type,
704 since long cannot hold all the values of an unsigned int. */
705 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
706 return long_unsigned_type_node;
707 else
708 return long_integer_type_node;
711 /* Likewise, prefer long double to double even if same size. */
712 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
713 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
714 return long_double_type_node;
716 /* Otherwise prefer the unsigned one. */
718 if (TYPE_UNSIGNED (t1))
719 return t1;
720 else
721 return t2;
724 /* Wrapper around c_common_type that is used by c-common.c and other
725 front end optimizations that remove promotions. ENUMERAL_TYPEs
726 are allowed here and are converted to their compatible integer types.
727 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
728 preferably a non-Boolean type as the common type. */
729 tree
730 common_type (tree t1, tree t2)
732 if (TREE_CODE (t1) == ENUMERAL_TYPE)
733 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
734 if (TREE_CODE (t2) == ENUMERAL_TYPE)
735 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
737 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
738 if (TREE_CODE (t1) == BOOLEAN_TYPE
739 && TREE_CODE (t2) == BOOLEAN_TYPE)
740 return boolean_type_node;
742 /* If either type is BOOLEAN_TYPE, then return the other. */
743 if (TREE_CODE (t1) == BOOLEAN_TYPE)
744 return t2;
745 if (TREE_CODE (t2) == BOOLEAN_TYPE)
746 return t1;
748 return c_common_type (t1, t2);
751 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
752 or various other operations. Return 2 if they are compatible
753 but a warning may be needed if you use them together. */
756 comptypes (tree type1, tree type2)
758 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
759 int val;
761 val = comptypes_internal (type1, type2);
762 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
764 return val;
767 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
768 or various other operations. Return 2 if they are compatible
769 but a warning may be needed if you use them together. This
770 differs from comptypes, in that we don't free the seen types. */
772 static int
773 comptypes_internal (tree type1, tree type2)
775 tree t1 = type1;
776 tree t2 = type2;
777 int attrval, val;
779 /* Suppress errors caused by previously reported errors. */
781 if (t1 == t2 || !t1 || !t2
782 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
783 return 1;
785 /* If either type is the internal version of sizetype, return the
786 language version. */
787 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
788 && TYPE_ORIG_SIZE_TYPE (t1))
789 t1 = TYPE_ORIG_SIZE_TYPE (t1);
791 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
792 && TYPE_ORIG_SIZE_TYPE (t2))
793 t2 = TYPE_ORIG_SIZE_TYPE (t2);
796 /* Enumerated types are compatible with integer types, but this is
797 not transitive: two enumerated types in the same translation unit
798 are compatible with each other only if they are the same type. */
800 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
801 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
802 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
803 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
805 if (t1 == t2)
806 return 1;
808 /* Different classes of types can't be compatible. */
810 if (TREE_CODE (t1) != TREE_CODE (t2))
811 return 0;
813 /* Qualifiers must match. C99 6.7.3p9 */
815 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
816 return 0;
818 /* Allow for two different type nodes which have essentially the same
819 definition. Note that we already checked for equality of the type
820 qualifiers (just above). */
822 if (TREE_CODE (t1) != ARRAY_TYPE
823 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
824 return 1;
826 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
827 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
828 return 0;
830 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
831 val = 0;
833 switch (TREE_CODE (t1))
835 case POINTER_TYPE:
836 /* Do not remove mode or aliasing information. */
837 if (TYPE_MODE (t1) != TYPE_MODE (t2)
838 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
839 break;
840 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
841 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
842 break;
844 case FUNCTION_TYPE:
845 val = function_types_compatible_p (t1, t2);
846 break;
848 case ARRAY_TYPE:
850 tree d1 = TYPE_DOMAIN (t1);
851 tree d2 = TYPE_DOMAIN (t2);
852 bool d1_variable, d2_variable;
853 bool d1_zero, d2_zero;
854 val = 1;
856 /* Target types must match incl. qualifiers. */
857 if (TREE_TYPE (t1) != TREE_TYPE (t2)
858 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
859 return 0;
861 /* Sizes must match unless one is missing or variable. */
862 if (d1 == 0 || d2 == 0 || d1 == d2)
863 break;
865 d1_zero = !TYPE_MAX_VALUE (d1);
866 d2_zero = !TYPE_MAX_VALUE (d2);
868 d1_variable = (!d1_zero
869 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
870 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
871 d2_variable = (!d2_zero
872 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
873 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
874 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
875 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
877 if (d1_variable || d2_variable)
878 break;
879 if (d1_zero && d2_zero)
880 break;
881 if (d1_zero || d2_zero
882 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
883 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
884 val = 0;
886 break;
889 case ENUMERAL_TYPE:
890 case RECORD_TYPE:
891 case UNION_TYPE:
892 if (val != 1 && !same_translation_unit_p (t1, t2))
894 if (attrval != 2)
895 return tagged_types_tu_compatible_p (t1, t2);
896 val = tagged_types_tu_compatible_p (t1, t2);
898 break;
900 case VECTOR_TYPE:
901 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
902 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
903 break;
905 default:
906 break;
908 return attrval == 2 && val == 1 ? 2 : val;
911 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
912 ignoring their qualifiers. */
914 static int
915 comp_target_types (tree ttl, tree ttr)
917 int val;
918 tree mvl, mvr;
920 /* Do not lose qualifiers on element types of array types that are
921 pointer targets by taking their TYPE_MAIN_VARIANT. */
922 mvl = TREE_TYPE (ttl);
923 mvr = TREE_TYPE (ttr);
924 if (TREE_CODE (mvl) != ARRAY_TYPE)
925 mvl = TYPE_MAIN_VARIANT (mvl);
926 if (TREE_CODE (mvr) != ARRAY_TYPE)
927 mvr = TYPE_MAIN_VARIANT (mvr);
928 val = comptypes (mvl, mvr);
930 if (val == 2 && pedantic)
931 pedwarn ("types are not quite compatible");
932 return val;
935 /* Subroutines of `comptypes'. */
937 /* Determine whether two trees derive from the same translation unit.
938 If the CONTEXT chain ends in a null, that tree's context is still
939 being parsed, so if two trees have context chains ending in null,
940 they're in the same translation unit. */
942 same_translation_unit_p (tree t1, tree t2)
944 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
945 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
947 case tcc_declaration:
948 t1 = DECL_CONTEXT (t1); break;
949 case tcc_type:
950 t1 = TYPE_CONTEXT (t1); break;
951 case tcc_exceptional:
952 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
953 default: gcc_unreachable ();
956 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
957 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
959 case tcc_declaration:
960 t2 = DECL_CONTEXT (t2); break;
961 case tcc_type:
962 t2 = TYPE_CONTEXT (t2); break;
963 case tcc_exceptional:
964 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
965 default: gcc_unreachable ();
968 return t1 == t2;
971 /* Allocate the seen two types, assuming that they are compatible. */
973 static struct tagged_tu_seen_cache *
974 alloc_tagged_tu_seen_cache (tree t1, tree t2)
976 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
977 tu->next = tagged_tu_seen_base;
978 tu->t1 = t1;
979 tu->t2 = t2;
981 tagged_tu_seen_base = tu;
983 /* The C standard says that two structures in different translation
984 units are compatible with each other only if the types of their
985 fields are compatible (among other things). We assume that they
986 are compatible until proven otherwise when building the cache.
987 An example where this can occur is:
988 struct a
990 struct a *next;
992 If we are comparing this against a similar struct in another TU,
993 and did not assume they were compatible, we end up with an infinite
994 loop. */
995 tu->val = 1;
996 return tu;
999 /* Free the seen types until we get to TU_TIL. */
1001 static void
1002 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1004 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1005 while (tu != tu_til)
1007 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
1008 tu = tu1->next;
1009 free (tu1);
1011 tagged_tu_seen_base = tu_til;
1014 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1015 compatible. If the two types are not the same (which has been
1016 checked earlier), this can only happen when multiple translation
1017 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1018 rules. */
1020 static int
1021 tagged_types_tu_compatible_p (tree t1, tree t2)
1023 tree s1, s2;
1024 bool needs_warning = false;
1026 /* We have to verify that the tags of the types are the same. This
1027 is harder than it looks because this may be a typedef, so we have
1028 to go look at the original type. It may even be a typedef of a
1029 typedef...
1030 In the case of compiler-created builtin structs the TYPE_DECL
1031 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1032 while (TYPE_NAME (t1)
1033 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1034 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1035 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1037 while (TYPE_NAME (t2)
1038 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1039 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1040 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1042 /* C90 didn't have the requirement that the two tags be the same. */
1043 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1044 return 0;
1046 /* C90 didn't say what happened if one or both of the types were
1047 incomplete; we choose to follow C99 rules here, which is that they
1048 are compatible. */
1049 if (TYPE_SIZE (t1) == NULL
1050 || TYPE_SIZE (t2) == NULL)
1051 return 1;
1054 const struct tagged_tu_seen_cache * tts_i;
1055 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1056 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1057 return tts_i->val;
1060 switch (TREE_CODE (t1))
1062 case ENUMERAL_TYPE:
1064 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1065 /* Speed up the case where the type values are in the same order. */
1066 tree tv1 = TYPE_VALUES (t1);
1067 tree tv2 = TYPE_VALUES (t2);
1069 if (tv1 == tv2)
1071 return 1;
1074 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1076 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1077 break;
1078 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1080 tu->val = 0;
1081 return 0;
1085 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1087 return 1;
1089 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1091 tu->val = 0;
1092 return 0;
1095 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1097 tu->val = 0;
1098 return 0;
1101 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1103 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1104 if (s2 == NULL
1105 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1107 tu->val = 0;
1108 return 0;
1111 return 1;
1114 case UNION_TYPE:
1116 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1117 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1119 tu->val = 0;
1120 return 0;
1123 /* Speed up the common case where the fields are in the same order. */
1124 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1125 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1127 int result;
1130 if (DECL_NAME (s1) == NULL
1131 || DECL_NAME (s1) != DECL_NAME (s2))
1132 break;
1133 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1134 if (result == 0)
1136 tu->val = 0;
1137 return 0;
1139 if (result == 2)
1140 needs_warning = true;
1142 if (TREE_CODE (s1) == FIELD_DECL
1143 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1144 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1146 tu->val = 0;
1147 return 0;
1150 if (!s1 && !s2)
1152 tu->val = needs_warning ? 2 : 1;
1153 return tu->val;
1156 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1158 bool ok = false;
1160 if (DECL_NAME (s1) != NULL)
1161 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1162 if (DECL_NAME (s1) == DECL_NAME (s2))
1164 int result;
1165 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1166 if (result == 0)
1168 tu->val = 0;
1169 return 0;
1171 if (result == 2)
1172 needs_warning = true;
1174 if (TREE_CODE (s1) == FIELD_DECL
1175 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1176 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1177 break;
1179 ok = true;
1180 break;
1182 if (!ok)
1184 tu->val = 0;
1185 return 0;
1188 tu->val = needs_warning ? 2 : 10;
1189 return tu->val;
1192 case RECORD_TYPE:
1194 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1196 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1197 s1 && s2;
1198 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1200 int result;
1201 if (TREE_CODE (s1) != TREE_CODE (s2)
1202 || DECL_NAME (s1) != DECL_NAME (s2))
1203 break;
1204 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1205 if (result == 0)
1206 break;
1207 if (result == 2)
1208 needs_warning = true;
1210 if (TREE_CODE (s1) == FIELD_DECL
1211 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1212 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1213 break;
1215 if (s1 && s2)
1216 tu->val = 0;
1217 else
1218 tu->val = needs_warning ? 2 : 1;
1219 return tu->val;
1222 default:
1223 gcc_unreachable ();
1227 /* Return 1 if two function types F1 and F2 are compatible.
1228 If either type specifies no argument types,
1229 the other must specify a fixed number of self-promoting arg types.
1230 Otherwise, if one type specifies only the number of arguments,
1231 the other must specify that number of self-promoting arg types.
1232 Otherwise, the argument types must match. */
1234 static int
1235 function_types_compatible_p (tree f1, tree f2)
1237 tree args1, args2;
1238 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1239 int val = 1;
1240 int val1;
1241 tree ret1, ret2;
1243 ret1 = TREE_TYPE (f1);
1244 ret2 = TREE_TYPE (f2);
1246 /* 'volatile' qualifiers on a function's return type used to mean
1247 the function is noreturn. */
1248 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1249 pedwarn ("function return types not compatible due to %<volatile%>");
1250 if (TYPE_VOLATILE (ret1))
1251 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1252 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1253 if (TYPE_VOLATILE (ret2))
1254 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1255 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1256 val = comptypes_internal (ret1, ret2);
1257 if (val == 0)
1258 return 0;
1260 args1 = TYPE_ARG_TYPES (f1);
1261 args2 = TYPE_ARG_TYPES (f2);
1263 /* An unspecified parmlist matches any specified parmlist
1264 whose argument types don't need default promotions. */
1266 if (args1 == 0)
1268 if (!self_promoting_args_p (args2))
1269 return 0;
1270 /* If one of these types comes from a non-prototype fn definition,
1271 compare that with the other type's arglist.
1272 If they don't match, ask for a warning (but no error). */
1273 if (TYPE_ACTUAL_ARG_TYPES (f1)
1274 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1275 val = 2;
1276 return val;
1278 if (args2 == 0)
1280 if (!self_promoting_args_p (args1))
1281 return 0;
1282 if (TYPE_ACTUAL_ARG_TYPES (f2)
1283 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1284 val = 2;
1285 return val;
1288 /* Both types have argument lists: compare them and propagate results. */
1289 val1 = type_lists_compatible_p (args1, args2);
1290 return val1 != 1 ? val1 : val;
1293 /* Check two lists of types for compatibility,
1294 returning 0 for incompatible, 1 for compatible,
1295 or 2 for compatible with warning. */
1297 static int
1298 type_lists_compatible_p (tree args1, tree args2)
1300 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1301 int val = 1;
1302 int newval = 0;
1304 while (1)
1306 tree a1, mv1, a2, mv2;
1307 if (args1 == 0 && args2 == 0)
1308 return val;
1309 /* If one list is shorter than the other,
1310 they fail to match. */
1311 if (args1 == 0 || args2 == 0)
1312 return 0;
1313 mv1 = a1 = TREE_VALUE (args1);
1314 mv2 = a2 = TREE_VALUE (args2);
1315 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1316 mv1 = TYPE_MAIN_VARIANT (mv1);
1317 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1318 mv2 = TYPE_MAIN_VARIANT (mv2);
1319 /* A null pointer instead of a type
1320 means there is supposed to be an argument
1321 but nothing is specified about what type it has.
1322 So match anything that self-promotes. */
1323 if (a1 == 0)
1325 if (c_type_promotes_to (a2) != a2)
1326 return 0;
1328 else if (a2 == 0)
1330 if (c_type_promotes_to (a1) != a1)
1331 return 0;
1333 /* If one of the lists has an error marker, ignore this arg. */
1334 else if (TREE_CODE (a1) == ERROR_MARK
1335 || TREE_CODE (a2) == ERROR_MARK)
1337 else if (!(newval = comptypes_internal (mv1, mv2)))
1339 /* Allow wait (union {union wait *u; int *i} *)
1340 and wait (union wait *) to be compatible. */
1341 if (TREE_CODE (a1) == UNION_TYPE
1342 && (TYPE_NAME (a1) == 0
1343 || TYPE_TRANSPARENT_UNION (a1))
1344 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1345 && tree_int_cst_equal (TYPE_SIZE (a1),
1346 TYPE_SIZE (a2)))
1348 tree memb;
1349 for (memb = TYPE_FIELDS (a1);
1350 memb; memb = TREE_CHAIN (memb))
1352 tree mv3 = TREE_TYPE (memb);
1353 if (mv3 && mv3 != error_mark_node
1354 && TREE_CODE (mv3) != ARRAY_TYPE)
1355 mv3 = TYPE_MAIN_VARIANT (mv3);
1356 if (comptypes_internal (mv3, mv2))
1357 break;
1359 if (memb == 0)
1360 return 0;
1362 else if (TREE_CODE (a2) == UNION_TYPE
1363 && (TYPE_NAME (a2) == 0
1364 || TYPE_TRANSPARENT_UNION (a2))
1365 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1366 && tree_int_cst_equal (TYPE_SIZE (a2),
1367 TYPE_SIZE (a1)))
1369 tree memb;
1370 for (memb = TYPE_FIELDS (a2);
1371 memb; memb = TREE_CHAIN (memb))
1373 tree mv3 = TREE_TYPE (memb);
1374 if (mv3 && mv3 != error_mark_node
1375 && TREE_CODE (mv3) != ARRAY_TYPE)
1376 mv3 = TYPE_MAIN_VARIANT (mv3);
1377 if (comptypes_internal (mv3, mv1))
1378 break;
1380 if (memb == 0)
1381 return 0;
1383 else
1384 return 0;
1387 /* comptypes said ok, but record if it said to warn. */
1388 if (newval > val)
1389 val = newval;
1391 args1 = TREE_CHAIN (args1);
1392 args2 = TREE_CHAIN (args2);
1396 /* Compute the size to increment a pointer by. */
1398 static tree
1399 c_size_in_bytes (tree type)
1401 enum tree_code code = TREE_CODE (type);
1403 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1404 return size_one_node;
1406 if (!COMPLETE_OR_VOID_TYPE_P (type))
1408 error ("arithmetic on pointer to an incomplete type");
1409 return size_one_node;
1412 /* Convert in case a char is more than one unit. */
1413 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1414 size_int (TYPE_PRECISION (char_type_node)
1415 / BITS_PER_UNIT));
1418 /* Return either DECL or its known constant value (if it has one). */
1420 tree
1421 decl_constant_value (tree decl)
1423 if (/* Don't change a variable array bound or initial value to a constant
1424 in a place where a variable is invalid. Note that DECL_INITIAL
1425 isn't valid for a PARM_DECL. */
1426 current_function_decl != 0
1427 && TREE_CODE (decl) != PARM_DECL
1428 && !TREE_THIS_VOLATILE (decl)
1429 && TREE_READONLY (decl)
1430 && DECL_INITIAL (decl) != 0
1431 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1432 /* This is invalid if initial value is not constant.
1433 If it has either a function call, a memory reference,
1434 or a variable, then re-evaluating it could give different results. */
1435 && TREE_CONSTANT (DECL_INITIAL (decl))
1436 /* Check for cases where this is sub-optimal, even though valid. */
1437 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1438 return DECL_INITIAL (decl);
1439 return decl;
1442 /* Return either DECL or its known constant value (if it has one), but
1443 return DECL if pedantic or DECL has mode BLKmode. This is for
1444 bug-compatibility with the old behavior of decl_constant_value
1445 (before GCC 3.0); every use of this function is a bug and it should
1446 be removed before GCC 3.1. It is not appropriate to use pedantic
1447 in a way that affects optimization, and BLKmode is probably not the
1448 right test for avoiding misoptimizations either. */
1450 static tree
1451 decl_constant_value_for_broken_optimization (tree decl)
1453 tree ret;
1455 if (pedantic || DECL_MODE (decl) == BLKmode)
1456 return decl;
1458 ret = decl_constant_value (decl);
1459 /* Avoid unwanted tree sharing between the initializer and current
1460 function's body where the tree can be modified e.g. by the
1461 gimplifier. */
1462 if (ret != decl && TREE_STATIC (decl))
1463 ret = unshare_expr (ret);
1464 return ret;
1467 /* Convert the array expression EXP to a pointer. */
1468 static tree
1469 array_to_pointer_conversion (tree exp)
1471 tree orig_exp = exp;
1472 tree type = TREE_TYPE (exp);
1473 tree adr;
1474 tree restype = TREE_TYPE (type);
1475 tree ptrtype;
1477 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1479 STRIP_TYPE_NOPS (exp);
1481 if (TREE_NO_WARNING (orig_exp))
1482 TREE_NO_WARNING (exp) = 1;
1484 ptrtype = build_pointer_type (restype);
1486 if (TREE_CODE (exp) == INDIRECT_REF)
1487 return convert (ptrtype, TREE_OPERAND (exp, 0));
1489 if (TREE_CODE (exp) == VAR_DECL)
1491 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1492 ADDR_EXPR because it's the best way of representing what
1493 happens in C when we take the address of an array and place
1494 it in a pointer to the element type. */
1495 adr = build1 (ADDR_EXPR, ptrtype, exp);
1496 if (!c_mark_addressable (exp))
1497 return error_mark_node;
1498 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1499 return adr;
1502 /* This way is better for a COMPONENT_REF since it can
1503 simplify the offset for a component. */
1504 adr = build_unary_op (ADDR_EXPR, exp, 1);
1505 return convert (ptrtype, adr);
1508 /* Convert the function expression EXP to a pointer. */
1509 static tree
1510 function_to_pointer_conversion (tree exp)
1512 tree orig_exp = exp;
1514 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1516 STRIP_TYPE_NOPS (exp);
1518 if (TREE_NO_WARNING (orig_exp))
1519 TREE_NO_WARNING (exp) = 1;
1521 return build_unary_op (ADDR_EXPR, exp, 0);
1524 /* Perform the default conversion of arrays and functions to pointers.
1525 Return the result of converting EXP. For any other expression, just
1526 return EXP after removing NOPs. */
1528 struct c_expr
1529 default_function_array_conversion (struct c_expr exp)
1531 tree orig_exp = exp.value;
1532 tree type = TREE_TYPE (exp.value);
1533 enum tree_code code = TREE_CODE (type);
1535 switch (code)
1537 case ARRAY_TYPE:
1539 bool not_lvalue = false;
1540 bool lvalue_array_p;
1542 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1543 || TREE_CODE (exp.value) == NOP_EXPR
1544 || TREE_CODE (exp.value) == CONVERT_EXPR)
1545 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1547 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1548 not_lvalue = true;
1549 exp.value = TREE_OPERAND (exp.value, 0);
1552 if (TREE_NO_WARNING (orig_exp))
1553 TREE_NO_WARNING (exp.value) = 1;
1555 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1556 if (!flag_isoc99 && !lvalue_array_p)
1558 /* Before C99, non-lvalue arrays do not decay to pointers.
1559 Normally, using such an array would be invalid; but it can
1560 be used correctly inside sizeof or as a statement expression.
1561 Thus, do not give an error here; an error will result later. */
1562 return exp;
1565 exp.value = array_to_pointer_conversion (exp.value);
1567 break;
1568 case FUNCTION_TYPE:
1569 exp.value = function_to_pointer_conversion (exp.value);
1570 break;
1571 default:
1572 STRIP_TYPE_NOPS (exp.value);
1573 if (TREE_NO_WARNING (orig_exp))
1574 TREE_NO_WARNING (exp.value) = 1;
1575 break;
1578 return exp;
1582 /* EXP is an expression of integer type. Apply the integer promotions
1583 to it and return the promoted value. */
1585 tree
1586 perform_integral_promotions (tree exp)
1588 tree type = TREE_TYPE (exp);
1589 enum tree_code code = TREE_CODE (type);
1591 gcc_assert (INTEGRAL_TYPE_P (type));
1593 /* Normally convert enums to int,
1594 but convert wide enums to something wider. */
1595 if (code == ENUMERAL_TYPE)
1597 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1598 TYPE_PRECISION (integer_type_node)),
1599 ((TYPE_PRECISION (type)
1600 >= TYPE_PRECISION (integer_type_node))
1601 && TYPE_UNSIGNED (type)));
1603 return convert (type, exp);
1606 /* ??? This should no longer be needed now bit-fields have their
1607 proper types. */
1608 if (TREE_CODE (exp) == COMPONENT_REF
1609 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1610 /* If it's thinner than an int, promote it like a
1611 c_promoting_integer_type_p, otherwise leave it alone. */
1612 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1613 TYPE_PRECISION (integer_type_node)))
1614 return convert (integer_type_node, exp);
1616 if (c_promoting_integer_type_p (type))
1618 /* Preserve unsignedness if not really getting any wider. */
1619 if (TYPE_UNSIGNED (type)
1620 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1621 return convert (unsigned_type_node, exp);
1623 return convert (integer_type_node, exp);
1626 return exp;
1630 /* Perform default promotions for C data used in expressions.
1631 Enumeral types or short or char are converted to int.
1632 In addition, manifest constants symbols are replaced by their values. */
1634 tree
1635 default_conversion (tree exp)
1637 tree orig_exp;
1638 tree type = TREE_TYPE (exp);
1639 enum tree_code code = TREE_CODE (type);
1641 /* Functions and arrays have been converted during parsing. */
1642 gcc_assert (code != FUNCTION_TYPE);
1643 if (code == ARRAY_TYPE)
1644 return exp;
1646 /* Constants can be used directly unless they're not loadable. */
1647 if (TREE_CODE (exp) == CONST_DECL)
1648 exp = DECL_INITIAL (exp);
1650 /* Replace a nonvolatile const static variable with its value unless
1651 it is an array, in which case we must be sure that taking the
1652 address of the array produces consistent results. */
1653 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1655 exp = decl_constant_value_for_broken_optimization (exp);
1656 type = TREE_TYPE (exp);
1659 /* Strip no-op conversions. */
1660 orig_exp = exp;
1661 STRIP_TYPE_NOPS (exp);
1663 if (TREE_NO_WARNING (orig_exp))
1664 TREE_NO_WARNING (exp) = 1;
1666 if (INTEGRAL_TYPE_P (type))
1667 return perform_integral_promotions (exp);
1669 if (code == VOID_TYPE)
1671 error ("void value not ignored as it ought to be");
1672 return error_mark_node;
1674 return exp;
1677 /* Look up COMPONENT in a structure or union DECL.
1679 If the component name is not found, returns NULL_TREE. Otherwise,
1680 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1681 stepping down the chain to the component, which is in the last
1682 TREE_VALUE of the list. Normally the list is of length one, but if
1683 the component is embedded within (nested) anonymous structures or
1684 unions, the list steps down the chain to the component. */
1686 static tree
1687 lookup_field (tree decl, tree component)
1689 tree type = TREE_TYPE (decl);
1690 tree field;
1692 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1693 to the field elements. Use a binary search on this array to quickly
1694 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1695 will always be set for structures which have many elements. */
1697 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1699 int bot, top, half;
1700 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1702 field = TYPE_FIELDS (type);
1703 bot = 0;
1704 top = TYPE_LANG_SPECIFIC (type)->s->len;
1705 while (top - bot > 1)
1707 half = (top - bot + 1) >> 1;
1708 field = field_array[bot+half];
1710 if (DECL_NAME (field) == NULL_TREE)
1712 /* Step through all anon unions in linear fashion. */
1713 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1715 field = field_array[bot++];
1716 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1717 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1719 tree anon = lookup_field (field, component);
1721 if (anon)
1722 return tree_cons (NULL_TREE, field, anon);
1726 /* Entire record is only anon unions. */
1727 if (bot > top)
1728 return NULL_TREE;
1730 /* Restart the binary search, with new lower bound. */
1731 continue;
1734 if (DECL_NAME (field) == component)
1735 break;
1736 if (DECL_NAME (field) < component)
1737 bot += half;
1738 else
1739 top = bot + half;
1742 if (DECL_NAME (field_array[bot]) == component)
1743 field = field_array[bot];
1744 else if (DECL_NAME (field) != component)
1745 return NULL_TREE;
1747 else
1749 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1751 if (DECL_NAME (field) == NULL_TREE
1752 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1753 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1755 tree anon = lookup_field (field, component);
1757 if (anon)
1758 return tree_cons (NULL_TREE, field, anon);
1761 if (DECL_NAME (field) == component)
1762 break;
1765 if (field == NULL_TREE)
1766 return NULL_TREE;
1769 return tree_cons (NULL_TREE, field, NULL_TREE);
1772 /* Make an expression to refer to the COMPONENT field of
1773 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1775 tree
1776 build_component_ref (tree datum, tree component)
1778 tree type = TREE_TYPE (datum);
1779 enum tree_code code = TREE_CODE (type);
1780 tree field = NULL;
1781 tree ref;
1783 if (!objc_is_public (datum, component))
1784 return error_mark_node;
1786 /* See if there is a field or component with name COMPONENT. */
1788 if (code == RECORD_TYPE || code == UNION_TYPE)
1790 if (!COMPLETE_TYPE_P (type))
1792 c_incomplete_type_error (NULL_TREE, type);
1793 return error_mark_node;
1796 field = lookup_field (datum, component);
1798 if (!field)
1800 error ("%qT has no member named %qE", type, component);
1801 return error_mark_node;
1804 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1805 This might be better solved in future the way the C++ front
1806 end does it - by giving the anonymous entities each a
1807 separate name and type, and then have build_component_ref
1808 recursively call itself. We can't do that here. */
1811 tree subdatum = TREE_VALUE (field);
1813 if (TREE_TYPE (subdatum) == error_mark_node)
1814 return error_mark_node;
1816 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1817 NULL_TREE);
1818 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1819 TREE_READONLY (ref) = 1;
1820 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1821 TREE_THIS_VOLATILE (ref) = 1;
1823 if (TREE_DEPRECATED (subdatum))
1824 warn_deprecated_use (subdatum);
1826 datum = ref;
1828 field = TREE_CHAIN (field);
1830 while (field);
1832 return ref;
1834 else if (code != ERROR_MARK)
1835 error ("request for member %qE in something not a structure or union",
1836 component);
1838 return error_mark_node;
1841 /* Given an expression PTR for a pointer, return an expression
1842 for the value pointed to.
1843 ERRORSTRING is the name of the operator to appear in error messages. */
1845 tree
1846 build_indirect_ref (tree ptr, const char *errorstring)
1848 tree pointer = default_conversion (ptr);
1849 tree type = TREE_TYPE (pointer);
1851 if (TREE_CODE (type) == POINTER_TYPE)
1853 if (TREE_CODE (pointer) == ADDR_EXPR
1854 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1855 == TREE_TYPE (type)))
1856 return TREE_OPERAND (pointer, 0);
1857 else
1859 tree t = TREE_TYPE (type);
1860 tree ref;
1862 ref = build1 (INDIRECT_REF, t, pointer);
1864 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1866 error ("dereferencing pointer to incomplete type");
1867 return error_mark_node;
1869 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1870 warning (0, "dereferencing %<void *%> pointer");
1872 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1873 so that we get the proper error message if the result is used
1874 to assign to. Also, &* is supposed to be a no-op.
1875 And ANSI C seems to specify that the type of the result
1876 should be the const type. */
1877 /* A de-reference of a pointer to const is not a const. It is valid
1878 to change it via some other pointer. */
1879 TREE_READONLY (ref) = TYPE_READONLY (t);
1880 TREE_SIDE_EFFECTS (ref)
1881 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1882 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1883 return ref;
1886 else if (TREE_CODE (pointer) != ERROR_MARK)
1887 error ("invalid type argument of %qs", errorstring);
1888 return error_mark_node;
1891 /* This handles expressions of the form "a[i]", which denotes
1892 an array reference.
1894 This is logically equivalent in C to *(a+i), but we may do it differently.
1895 If A is a variable or a member, we generate a primitive ARRAY_REF.
1896 This avoids forcing the array out of registers, and can work on
1897 arrays that are not lvalues (for example, members of structures returned
1898 by functions). */
1900 tree
1901 build_array_ref (tree array, tree index)
1903 bool swapped = false;
1904 if (TREE_TYPE (array) == error_mark_node
1905 || TREE_TYPE (index) == error_mark_node)
1906 return error_mark_node;
1908 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1909 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1911 tree temp;
1912 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1913 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1915 error ("subscripted value is neither array nor pointer");
1916 return error_mark_node;
1918 temp = array;
1919 array = index;
1920 index = temp;
1921 swapped = true;
1924 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1926 error ("array subscript is not an integer");
1927 return error_mark_node;
1930 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1932 error ("subscripted value is pointer to function");
1933 return error_mark_node;
1936 /* ??? Existing practice has been to warn only when the char
1937 index is syntactically the index, not for char[array]. */
1938 if (!swapped)
1939 warn_array_subscript_with_type_char (index);
1941 /* Apply default promotions *after* noticing character types. */
1942 index = default_conversion (index);
1944 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1946 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1948 tree rval, type;
1950 /* An array that is indexed by a non-constant
1951 cannot be stored in a register; we must be able to do
1952 address arithmetic on its address.
1953 Likewise an array of elements of variable size. */
1954 if (TREE_CODE (index) != INTEGER_CST
1955 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1956 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1958 if (!c_mark_addressable (array))
1959 return error_mark_node;
1961 /* An array that is indexed by a constant value which is not within
1962 the array bounds cannot be stored in a register either; because we
1963 would get a crash in store_bit_field/extract_bit_field when trying
1964 to access a non-existent part of the register. */
1965 if (TREE_CODE (index) == INTEGER_CST
1966 && TYPE_DOMAIN (TREE_TYPE (array))
1967 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1969 if (!c_mark_addressable (array))
1970 return error_mark_node;
1973 if (pedantic)
1975 tree foo = array;
1976 while (TREE_CODE (foo) == COMPONENT_REF)
1977 foo = TREE_OPERAND (foo, 0);
1978 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1979 pedwarn ("ISO C forbids subscripting %<register%> array");
1980 else if (!flag_isoc99 && !lvalue_p (foo))
1981 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1984 type = TREE_TYPE (TREE_TYPE (array));
1985 if (TREE_CODE (type) != ARRAY_TYPE)
1986 type = TYPE_MAIN_VARIANT (type);
1987 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1988 /* Array ref is const/volatile if the array elements are
1989 or if the array is. */
1990 TREE_READONLY (rval)
1991 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1992 | TREE_READONLY (array));
1993 TREE_SIDE_EFFECTS (rval)
1994 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1995 | TREE_SIDE_EFFECTS (array));
1996 TREE_THIS_VOLATILE (rval)
1997 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1998 /* This was added by rms on 16 Nov 91.
1999 It fixes vol struct foo *a; a->elts[1]
2000 in an inline function.
2001 Hope it doesn't break something else. */
2002 | TREE_THIS_VOLATILE (array));
2003 return require_complete_type (fold (rval));
2005 else
2007 tree ar = default_conversion (array);
2009 if (ar == error_mark_node)
2010 return ar;
2012 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2013 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2015 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2016 "array indexing");
2020 /* Build an external reference to identifier ID. FUN indicates
2021 whether this will be used for a function call. LOC is the source
2022 location of the identifier. */
2023 tree
2024 build_external_ref (tree id, int fun, location_t loc)
2026 tree ref;
2027 tree decl = lookup_name (id);
2029 /* In Objective-C, an instance variable (ivar) may be preferred to
2030 whatever lookup_name() found. */
2031 decl = objc_lookup_ivar (decl, id);
2033 if (decl && decl != error_mark_node)
2034 ref = decl;
2035 else if (fun)
2036 /* Implicit function declaration. */
2037 ref = implicitly_declare (id);
2038 else if (decl == error_mark_node)
2039 /* Don't complain about something that's already been
2040 complained about. */
2041 return error_mark_node;
2042 else
2044 undeclared_variable (id, loc);
2045 return error_mark_node;
2048 if (TREE_TYPE (ref) == error_mark_node)
2049 return error_mark_node;
2051 if (TREE_DEPRECATED (ref))
2052 warn_deprecated_use (ref);
2054 if (!skip_evaluation)
2055 assemble_external (ref);
2056 TREE_USED (ref) = 1;
2058 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2060 if (!in_sizeof && !in_typeof)
2061 C_DECL_USED (ref) = 1;
2062 else if (DECL_INITIAL (ref) == 0
2063 && DECL_EXTERNAL (ref)
2064 && !TREE_PUBLIC (ref))
2065 record_maybe_used_decl (ref);
2068 if (TREE_CODE (ref) == CONST_DECL)
2070 used_types_insert (TREE_TYPE (ref));
2071 ref = DECL_INITIAL (ref);
2072 TREE_CONSTANT (ref) = 1;
2073 TREE_INVARIANT (ref) = 1;
2075 else if (current_function_decl != 0
2076 && !DECL_FILE_SCOPE_P (current_function_decl)
2077 && (TREE_CODE (ref) == VAR_DECL
2078 || TREE_CODE (ref) == PARM_DECL
2079 || TREE_CODE (ref) == FUNCTION_DECL))
2081 tree context = decl_function_context (ref);
2083 if (context != 0 && context != current_function_decl)
2084 DECL_NONLOCAL (ref) = 1;
2087 return ref;
2090 /* Record details of decls possibly used inside sizeof or typeof. */
2091 struct maybe_used_decl
2093 /* The decl. */
2094 tree decl;
2095 /* The level seen at (in_sizeof + in_typeof). */
2096 int level;
2097 /* The next one at this level or above, or NULL. */
2098 struct maybe_used_decl *next;
2101 static struct maybe_used_decl *maybe_used_decls;
2103 /* Record that DECL, an undefined static function reference seen
2104 inside sizeof or typeof, might be used if the operand of sizeof is
2105 a VLA type or the operand of typeof is a variably modified
2106 type. */
2108 static void
2109 record_maybe_used_decl (tree decl)
2111 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2112 t->decl = decl;
2113 t->level = in_sizeof + in_typeof;
2114 t->next = maybe_used_decls;
2115 maybe_used_decls = t;
2118 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2119 USED is false, just discard them. If it is true, mark them used
2120 (if no longer inside sizeof or typeof) or move them to the next
2121 level up (if still inside sizeof or typeof). */
2123 void
2124 pop_maybe_used (bool used)
2126 struct maybe_used_decl *p = maybe_used_decls;
2127 int cur_level = in_sizeof + in_typeof;
2128 while (p && p->level > cur_level)
2130 if (used)
2132 if (cur_level == 0)
2133 C_DECL_USED (p->decl) = 1;
2134 else
2135 p->level = cur_level;
2137 p = p->next;
2139 if (!used || cur_level == 0)
2140 maybe_used_decls = p;
2143 /* Return the result of sizeof applied to EXPR. */
2145 struct c_expr
2146 c_expr_sizeof_expr (struct c_expr expr)
2148 struct c_expr ret;
2149 if (expr.value == error_mark_node)
2151 ret.value = error_mark_node;
2152 ret.original_code = ERROR_MARK;
2153 pop_maybe_used (false);
2155 else
2157 ret.value = c_sizeof (TREE_TYPE (expr.value));
2158 ret.original_code = ERROR_MARK;
2159 if (c_vla_type_p (TREE_TYPE (expr.value)))
2161 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2162 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2164 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2166 return ret;
2169 /* Return the result of sizeof applied to T, a structure for the type
2170 name passed to sizeof (rather than the type itself). */
2172 struct c_expr
2173 c_expr_sizeof_type (struct c_type_name *t)
2175 tree type;
2176 struct c_expr ret;
2177 type = groktypename (t);
2178 ret.value = c_sizeof (type);
2179 ret.original_code = ERROR_MARK;
2180 pop_maybe_used (type != error_mark_node
2181 ? C_TYPE_VARIABLE_SIZE (type) : false);
2182 return ret;
2185 /* Build a function call to function FUNCTION with parameters PARAMS.
2186 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2187 TREE_VALUE of each node is a parameter-expression.
2188 FUNCTION's data type may be a function type or a pointer-to-function. */
2190 tree
2191 build_function_call (tree function, tree params)
2193 tree fntype, fundecl = 0;
2194 tree coerced_params;
2195 tree name = NULL_TREE, result;
2196 tree tem;
2198 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2199 STRIP_TYPE_NOPS (function);
2201 /* Convert anything with function type to a pointer-to-function. */
2202 if (TREE_CODE (function) == FUNCTION_DECL)
2204 /* Implement type-directed function overloading for builtins.
2205 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2206 handle all the type checking. The result is a complete expression
2207 that implements this function call. */
2208 tem = resolve_overloaded_builtin (function, params);
2209 if (tem)
2210 return tem;
2212 name = DECL_NAME (function);
2213 fundecl = function;
2215 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2216 function = function_to_pointer_conversion (function);
2218 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2219 expressions, like those used for ObjC messenger dispatches. */
2220 function = objc_rewrite_function_call (function, params);
2222 fntype = TREE_TYPE (function);
2224 if (TREE_CODE (fntype) == ERROR_MARK)
2225 return error_mark_node;
2227 if (!(TREE_CODE (fntype) == POINTER_TYPE
2228 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2230 error ("called object %qE is not a function", function);
2231 return error_mark_node;
2234 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2235 current_function_returns_abnormally = 1;
2237 /* fntype now gets the type of function pointed to. */
2238 fntype = TREE_TYPE (fntype);
2240 /* Check that the function is called through a compatible prototype.
2241 If it is not, replace the call by a trap, wrapped up in a compound
2242 expression if necessary. This has the nice side-effect to prevent
2243 the tree-inliner from generating invalid assignment trees which may
2244 blow up in the RTL expander later. */
2245 if ((TREE_CODE (function) == NOP_EXPR
2246 || TREE_CODE (function) == CONVERT_EXPR)
2247 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2248 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2249 && !comptypes (fntype, TREE_TYPE (tem)))
2251 tree return_type = TREE_TYPE (fntype);
2252 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2253 NULL_TREE);
2255 /* This situation leads to run-time undefined behavior. We can't,
2256 therefore, simply error unless we can prove that all possible
2257 executions of the program must execute the code. */
2258 warning (0, "function called through a non-compatible type");
2260 /* We can, however, treat "undefined" any way we please.
2261 Call abort to encourage the user to fix the program. */
2262 inform ("if this code is reached, the program will abort");
2264 if (VOID_TYPE_P (return_type))
2265 return trap;
2266 else
2268 tree rhs;
2270 if (AGGREGATE_TYPE_P (return_type))
2271 rhs = build_compound_literal (return_type,
2272 build_constructor (return_type, 0));
2273 else
2274 rhs = fold_convert (return_type, integer_zero_node);
2276 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2280 /* Convert the parameters to the types declared in the
2281 function prototype, or apply default promotions. */
2283 coerced_params
2284 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2286 if (coerced_params == error_mark_node)
2287 return error_mark_node;
2289 /* Check that the arguments to the function are valid. */
2291 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2292 TYPE_ARG_TYPES (fntype));
2294 if (require_constant_value)
2296 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2297 function, coerced_params, NULL_TREE);
2299 if (TREE_CONSTANT (result)
2300 && (name == NULL_TREE
2301 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2302 pedwarn_init ("initializer element is not constant");
2304 else
2305 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2306 function, coerced_params, NULL_TREE);
2308 if (VOID_TYPE_P (TREE_TYPE (result)))
2309 return result;
2310 return require_complete_type (result);
2313 /* Convert the argument expressions in the list VALUES
2314 to the types in the list TYPELIST. The result is a list of converted
2315 argument expressions, unless there are too few arguments in which
2316 case it is error_mark_node.
2318 If TYPELIST is exhausted, or when an element has NULL as its type,
2319 perform the default conversions.
2321 PARMLIST is the chain of parm decls for the function being called.
2322 It may be 0, if that info is not available.
2323 It is used only for generating error messages.
2325 FUNCTION is a tree for the called function. It is used only for
2326 error messages, where it is formatted with %qE.
2328 This is also where warnings about wrong number of args are generated.
2330 Both VALUES and the returned value are chains of TREE_LIST nodes
2331 with the elements of the list in the TREE_VALUE slots of those nodes. */
2333 static tree
2334 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2336 tree typetail, valtail;
2337 tree result = NULL;
2338 int parmnum;
2339 tree selector;
2341 /* Change pointer to function to the function itself for
2342 diagnostics. */
2343 if (TREE_CODE (function) == ADDR_EXPR
2344 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2345 function = TREE_OPERAND (function, 0);
2347 /* Handle an ObjC selector specially for diagnostics. */
2348 selector = objc_message_selector ();
2350 /* Scan the given expressions and types, producing individual
2351 converted arguments and pushing them on RESULT in reverse order. */
2353 for (valtail = values, typetail = typelist, parmnum = 0;
2354 valtail;
2355 valtail = TREE_CHAIN (valtail), parmnum++)
2357 tree type = typetail ? TREE_VALUE (typetail) : 0;
2358 tree val = TREE_VALUE (valtail);
2359 tree rname = function;
2360 int argnum = parmnum + 1;
2361 const char *invalid_func_diag;
2363 if (type == void_type_node)
2365 error ("too many arguments to function %qE", function);
2366 break;
2369 if (selector && argnum > 2)
2371 rname = selector;
2372 argnum -= 2;
2375 STRIP_TYPE_NOPS (val);
2377 val = require_complete_type (val);
2379 if (type != 0)
2381 /* Formal parm type is specified by a function prototype. */
2382 tree parmval;
2384 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2386 error ("type of formal parameter %d is incomplete", parmnum + 1);
2387 parmval = val;
2389 else
2391 /* Optionally warn about conversions that
2392 differ from the default conversions. */
2393 if (warn_conversion || warn_traditional)
2395 unsigned int formal_prec = TYPE_PRECISION (type);
2397 if (INTEGRAL_TYPE_P (type)
2398 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2399 warning (0, "passing argument %d of %qE as integer "
2400 "rather than floating due to prototype",
2401 argnum, rname);
2402 if (INTEGRAL_TYPE_P (type)
2403 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2404 warning (0, "passing argument %d of %qE as integer "
2405 "rather than complex due to prototype",
2406 argnum, rname);
2407 else if (TREE_CODE (type) == COMPLEX_TYPE
2408 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2409 warning (0, "passing argument %d of %qE as complex "
2410 "rather than floating due to prototype",
2411 argnum, rname);
2412 else if (TREE_CODE (type) == REAL_TYPE
2413 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2414 warning (0, "passing argument %d of %qE as floating "
2415 "rather than integer due to prototype",
2416 argnum, rname);
2417 else if (TREE_CODE (type) == COMPLEX_TYPE
2418 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2419 warning (0, "passing argument %d of %qE as complex "
2420 "rather than integer due to prototype",
2421 argnum, rname);
2422 else if (TREE_CODE (type) == REAL_TYPE
2423 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2424 warning (0, "passing argument %d of %qE as floating "
2425 "rather than complex due to prototype",
2426 argnum, rname);
2427 /* ??? At some point, messages should be written about
2428 conversions between complex types, but that's too messy
2429 to do now. */
2430 else if (TREE_CODE (type) == REAL_TYPE
2431 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2433 /* Warn if any argument is passed as `float',
2434 since without a prototype it would be `double'. */
2435 if (formal_prec == TYPE_PRECISION (float_type_node)
2436 && type != dfloat32_type_node)
2437 warning (0, "passing argument %d of %qE as %<float%> "
2438 "rather than %<double%> due to prototype",
2439 argnum, rname);
2441 /* Warn if mismatch between argument and prototype
2442 for decimal float types. Warn of conversions with
2443 binary float types and of precision narrowing due to
2444 prototype. */
2445 else if (type != TREE_TYPE (val)
2446 && (type == dfloat32_type_node
2447 || type == dfloat64_type_node
2448 || type == dfloat128_type_node
2449 || TREE_TYPE (val) == dfloat32_type_node
2450 || TREE_TYPE (val) == dfloat64_type_node
2451 || TREE_TYPE (val) == dfloat128_type_node)
2452 && (formal_prec
2453 <= TYPE_PRECISION (TREE_TYPE (val))
2454 || (type == dfloat128_type_node
2455 && (TREE_TYPE (val)
2456 != dfloat64_type_node
2457 && (TREE_TYPE (val)
2458 != dfloat32_type_node)))
2459 || (type == dfloat64_type_node
2460 && (TREE_TYPE (val)
2461 != dfloat32_type_node))))
2462 warning (0, "passing argument %d of %qE as %qT "
2463 "rather than %qT due to prototype",
2464 argnum, rname, type, TREE_TYPE (val));
2467 /* Detect integer changing in width or signedness.
2468 These warnings are only activated with
2469 -Wconversion, not with -Wtraditional. */
2470 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2471 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2473 tree would_have_been = default_conversion (val);
2474 tree type1 = TREE_TYPE (would_have_been);
2476 if (TREE_CODE (type) == ENUMERAL_TYPE
2477 && (TYPE_MAIN_VARIANT (type)
2478 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2479 /* No warning if function asks for enum
2480 and the actual arg is that enum type. */
2482 else if (formal_prec != TYPE_PRECISION (type1))
2483 warning (OPT_Wconversion, "passing argument %d of %qE "
2484 "with different width due to prototype",
2485 argnum, rname);
2486 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2488 /* Don't complain if the formal parameter type
2489 is an enum, because we can't tell now whether
2490 the value was an enum--even the same enum. */
2491 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2493 else if (TREE_CODE (val) == INTEGER_CST
2494 && int_fits_type_p (val, type))
2495 /* Change in signedness doesn't matter
2496 if a constant value is unaffected. */
2498 /* If the value is extended from a narrower
2499 unsigned type, it doesn't matter whether we
2500 pass it as signed or unsigned; the value
2501 certainly is the same either way. */
2502 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2503 && TYPE_UNSIGNED (TREE_TYPE (val)))
2505 else if (TYPE_UNSIGNED (type))
2506 warning (OPT_Wconversion, "passing argument %d of %qE "
2507 "as unsigned due to prototype",
2508 argnum, rname);
2509 else
2510 warning (OPT_Wconversion, "passing argument %d of %qE "
2511 "as signed due to prototype", argnum, rname);
2515 parmval = convert_for_assignment (type, val, ic_argpass,
2516 fundecl, function,
2517 parmnum + 1);
2519 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2520 && INTEGRAL_TYPE_P (type)
2521 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2522 parmval = default_conversion (parmval);
2524 result = tree_cons (NULL_TREE, parmval, result);
2526 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2527 && (TYPE_PRECISION (TREE_TYPE (val))
2528 < TYPE_PRECISION (double_type_node))
2529 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2530 /* Convert `float' to `double'. */
2531 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2532 else if ((invalid_func_diag =
2533 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2535 error (invalid_func_diag);
2536 return error_mark_node;
2538 else
2539 /* Convert `short' and `char' to full-size `int'. */
2540 result = tree_cons (NULL_TREE, default_conversion (val), result);
2542 if (typetail)
2543 typetail = TREE_CHAIN (typetail);
2546 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2548 error ("too few arguments to function %qE", function);
2549 return error_mark_node;
2552 return nreverse (result);
2555 /* This is the entry point used by the parser to build unary operators
2556 in the input. CODE, a tree_code, specifies the unary operator, and
2557 ARG is the operand. For unary plus, the C parser currently uses
2558 CONVERT_EXPR for code. */
2560 struct c_expr
2561 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2563 struct c_expr result;
2565 result.original_code = ERROR_MARK;
2566 result.value = build_unary_op (code, arg.value, 0);
2567 overflow_warning (result.value);
2568 return result;
2571 /* This is the entry point used by the parser to build binary operators
2572 in the input. CODE, a tree_code, specifies the binary operator, and
2573 ARG1 and ARG2 are the operands. In addition to constructing the
2574 expression, we check for operands that were written with other binary
2575 operators in a way that is likely to confuse the user. */
2577 struct c_expr
2578 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2579 struct c_expr arg2)
2581 struct c_expr result;
2583 enum tree_code code1 = arg1.original_code;
2584 enum tree_code code2 = arg2.original_code;
2586 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2587 result.original_code = code;
2589 if (TREE_CODE (result.value) == ERROR_MARK)
2590 return result;
2592 /* Check for cases such as x+y<<z which users are likely
2593 to misinterpret. */
2594 if (warn_parentheses)
2596 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2598 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2599 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2600 warning (OPT_Wparentheses,
2601 "suggest parentheses around + or - inside shift");
2604 if (code == TRUTH_ORIF_EXPR)
2606 if (code1 == TRUTH_ANDIF_EXPR
2607 || code2 == TRUTH_ANDIF_EXPR)
2608 warning (OPT_Wparentheses,
2609 "suggest parentheses around && within ||");
2612 if (code == BIT_IOR_EXPR)
2614 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2615 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2616 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2617 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2618 warning (OPT_Wparentheses,
2619 "suggest parentheses around arithmetic in operand of |");
2620 /* Check cases like x|y==z */
2621 if (TREE_CODE_CLASS (code1) == tcc_comparison
2622 || TREE_CODE_CLASS (code2) == tcc_comparison)
2623 warning (OPT_Wparentheses,
2624 "suggest parentheses around comparison in operand of |");
2627 if (code == BIT_XOR_EXPR)
2629 if (code1 == BIT_AND_EXPR
2630 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2631 || code2 == BIT_AND_EXPR
2632 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2633 warning (OPT_Wparentheses,
2634 "suggest parentheses around arithmetic in operand of ^");
2635 /* Check cases like x^y==z */
2636 if (TREE_CODE_CLASS (code1) == tcc_comparison
2637 || TREE_CODE_CLASS (code2) == tcc_comparison)
2638 warning (OPT_Wparentheses,
2639 "suggest parentheses around comparison in operand of ^");
2642 if (code == BIT_AND_EXPR)
2644 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2645 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2646 warning (OPT_Wparentheses,
2647 "suggest parentheses around + or - in operand of &");
2648 /* Check cases like x&y==z */
2649 if (TREE_CODE_CLASS (code1) == tcc_comparison
2650 || TREE_CODE_CLASS (code2) == tcc_comparison)
2651 warning (OPT_Wparentheses,
2652 "suggest parentheses around comparison in operand of &");
2654 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2655 if (TREE_CODE_CLASS (code) == tcc_comparison
2656 && (TREE_CODE_CLASS (code1) == tcc_comparison
2657 || TREE_CODE_CLASS (code2) == tcc_comparison))
2658 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2659 "have their mathematical meaning");
2663 /* Warn about comparisons against string literals, with the exception
2664 of testing for equality or inequality of a string literal with NULL. */
2665 if (code == EQ_EXPR || code == NE_EXPR)
2667 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2668 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2669 warning (OPT_Wstring_literal_comparison,
2670 "comparison with string literal");
2672 else if (TREE_CODE_CLASS (code) == tcc_comparison
2673 && (code1 == STRING_CST || code2 == STRING_CST))
2674 warning (OPT_Wstring_literal_comparison,
2675 "comparison with string literal");
2677 overflow_warning (result.value);
2679 return result;
2682 /* Return a tree for the difference of pointers OP0 and OP1.
2683 The resulting tree has type int. */
2685 static tree
2686 pointer_diff (tree op0, tree op1)
2688 tree restype = ptrdiff_type_node;
2690 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2691 tree con0, con1, lit0, lit1;
2692 tree orig_op1 = op1;
2694 if (pedantic || warn_pointer_arith)
2696 if (TREE_CODE (target_type) == VOID_TYPE)
2697 pedwarn ("pointer of type %<void *%> used in subtraction");
2698 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2699 pedwarn ("pointer to a function used in subtraction");
2702 /* If the conversion to ptrdiff_type does anything like widening or
2703 converting a partial to an integral mode, we get a convert_expression
2704 that is in the way to do any simplifications.
2705 (fold-const.c doesn't know that the extra bits won't be needed.
2706 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2707 different mode in place.)
2708 So first try to find a common term here 'by hand'; we want to cover
2709 at least the cases that occur in legal static initializers. */
2710 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2711 && (TYPE_PRECISION (TREE_TYPE (op0))
2712 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2713 con0 = TREE_OPERAND (op0, 0);
2714 else
2715 con0 = op0;
2716 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2717 && (TYPE_PRECISION (TREE_TYPE (op1))
2718 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2719 con1 = TREE_OPERAND (op1, 0);
2720 else
2721 con1 = op1;
2723 if (TREE_CODE (con0) == PLUS_EXPR)
2725 lit0 = TREE_OPERAND (con0, 1);
2726 con0 = TREE_OPERAND (con0, 0);
2728 else
2729 lit0 = integer_zero_node;
2731 if (TREE_CODE (con1) == PLUS_EXPR)
2733 lit1 = TREE_OPERAND (con1, 1);
2734 con1 = TREE_OPERAND (con1, 0);
2736 else
2737 lit1 = integer_zero_node;
2739 if (operand_equal_p (con0, con1, 0))
2741 op0 = lit0;
2742 op1 = lit1;
2746 /* First do the subtraction as integers;
2747 then drop through to build the divide operator.
2748 Do not do default conversions on the minus operator
2749 in case restype is a short type. */
2751 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2752 convert (restype, op1), 0);
2753 /* This generates an error if op1 is pointer to incomplete type. */
2754 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2755 error ("arithmetic on pointer to an incomplete type");
2757 /* This generates an error if op0 is pointer to incomplete type. */
2758 op1 = c_size_in_bytes (target_type);
2760 /* Divide by the size, in easiest possible way. */
2761 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2764 /* Construct and perhaps optimize a tree representation
2765 for a unary operation. CODE, a tree_code, specifies the operation
2766 and XARG is the operand.
2767 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2768 the default promotions (such as from short to int).
2769 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2770 allows non-lvalues; this is only used to handle conversion of non-lvalue
2771 arrays to pointers in C99. */
2773 tree
2774 build_unary_op (enum tree_code code, tree xarg, int flag)
2776 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2777 tree arg = xarg;
2778 tree argtype = 0;
2779 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2780 tree val;
2781 int noconvert = flag;
2782 const char *invalid_op_diag;
2784 if (typecode == ERROR_MARK)
2785 return error_mark_node;
2786 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2787 typecode = INTEGER_TYPE;
2789 if ((invalid_op_diag
2790 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2792 error (invalid_op_diag);
2793 return error_mark_node;
2796 switch (code)
2798 case CONVERT_EXPR:
2799 /* This is used for unary plus, because a CONVERT_EXPR
2800 is enough to prevent anybody from looking inside for
2801 associativity, but won't generate any code. */
2802 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2803 || typecode == COMPLEX_TYPE
2804 || typecode == VECTOR_TYPE))
2806 error ("wrong type argument to unary plus");
2807 return error_mark_node;
2809 else if (!noconvert)
2810 arg = default_conversion (arg);
2811 arg = non_lvalue (arg);
2812 break;
2814 case NEGATE_EXPR:
2815 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2816 || typecode == COMPLEX_TYPE
2817 || typecode == VECTOR_TYPE))
2819 error ("wrong type argument to unary minus");
2820 return error_mark_node;
2822 else if (!noconvert)
2823 arg = default_conversion (arg);
2824 break;
2826 case BIT_NOT_EXPR:
2827 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2829 if (!noconvert)
2830 arg = default_conversion (arg);
2832 else if (typecode == COMPLEX_TYPE)
2834 code = CONJ_EXPR;
2835 if (pedantic)
2836 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2837 if (!noconvert)
2838 arg = default_conversion (arg);
2840 else
2842 error ("wrong type argument to bit-complement");
2843 return error_mark_node;
2845 break;
2847 case ABS_EXPR:
2848 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2850 error ("wrong type argument to abs");
2851 return error_mark_node;
2853 else if (!noconvert)
2854 arg = default_conversion (arg);
2855 break;
2857 case CONJ_EXPR:
2858 /* Conjugating a real value is a no-op, but allow it anyway. */
2859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2860 || typecode == COMPLEX_TYPE))
2862 error ("wrong type argument to conjugation");
2863 return error_mark_node;
2865 else if (!noconvert)
2866 arg = default_conversion (arg);
2867 break;
2869 case TRUTH_NOT_EXPR:
2870 if (typecode != INTEGER_TYPE
2871 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2872 && typecode != COMPLEX_TYPE)
2874 error ("wrong type argument to unary exclamation mark");
2875 return error_mark_node;
2877 arg = c_objc_common_truthvalue_conversion (arg);
2878 return invert_truthvalue (arg);
2880 case REALPART_EXPR:
2881 if (TREE_CODE (arg) == COMPLEX_CST)
2882 return TREE_REALPART (arg);
2883 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2884 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2885 else
2886 return arg;
2888 case IMAGPART_EXPR:
2889 if (TREE_CODE (arg) == COMPLEX_CST)
2890 return TREE_IMAGPART (arg);
2891 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2892 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2893 else
2894 return convert (TREE_TYPE (arg), integer_zero_node);
2896 case PREINCREMENT_EXPR:
2897 case POSTINCREMENT_EXPR:
2898 case PREDECREMENT_EXPR:
2899 case POSTDECREMENT_EXPR:
2901 /* Increment or decrement the real part of the value,
2902 and don't change the imaginary part. */
2903 if (typecode == COMPLEX_TYPE)
2905 tree real, imag;
2907 if (pedantic)
2908 pedwarn ("ISO C does not support %<++%> and %<--%>"
2909 " on complex types");
2911 arg = stabilize_reference (arg);
2912 real = build_unary_op (REALPART_EXPR, arg, 1);
2913 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2914 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2915 build_unary_op (code, real, 1), imag);
2918 /* Report invalid types. */
2920 if (typecode != POINTER_TYPE
2921 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2923 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2924 error ("wrong type argument to increment");
2925 else
2926 error ("wrong type argument to decrement");
2928 return error_mark_node;
2932 tree inc;
2933 tree result_type = TREE_TYPE (arg);
2935 arg = get_unwidened (arg, 0);
2936 argtype = TREE_TYPE (arg);
2938 /* Compute the increment. */
2940 if (typecode == POINTER_TYPE)
2942 /* If pointer target is an undefined struct,
2943 we just cannot know how to do the arithmetic. */
2944 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2946 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2947 error ("increment of pointer to unknown structure");
2948 else
2949 error ("decrement of pointer to unknown structure");
2951 else if ((pedantic || warn_pointer_arith)
2952 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2953 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2955 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2956 pedwarn ("wrong type argument to increment");
2957 else
2958 pedwarn ("wrong type argument to decrement");
2961 inc = c_size_in_bytes (TREE_TYPE (result_type));
2963 else
2964 inc = integer_one_node;
2966 inc = convert (argtype, inc);
2968 /* Complain about anything else that is not a true lvalue. */
2969 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2970 || code == POSTINCREMENT_EXPR)
2971 ? lv_increment
2972 : lv_decrement)))
2973 return error_mark_node;
2975 /* Report a read-only lvalue. */
2976 if (TREE_READONLY (arg))
2978 readonly_error (arg,
2979 ((code == PREINCREMENT_EXPR
2980 || code == POSTINCREMENT_EXPR)
2981 ? lv_increment : lv_decrement));
2982 return error_mark_node;
2985 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2986 val = boolean_increment (code, arg);
2987 else
2988 val = build2 (code, TREE_TYPE (arg), arg, inc);
2989 TREE_SIDE_EFFECTS (val) = 1;
2990 val = convert (result_type, val);
2991 if (TREE_CODE (val) != code)
2992 TREE_NO_WARNING (val) = 1;
2993 return val;
2996 case ADDR_EXPR:
2997 /* Note that this operation never does default_conversion. */
2999 /* Let &* cancel out to simplify resulting code. */
3000 if (TREE_CODE (arg) == INDIRECT_REF)
3002 /* Don't let this be an lvalue. */
3003 if (lvalue_p (TREE_OPERAND (arg, 0)))
3004 return non_lvalue (TREE_OPERAND (arg, 0));
3005 return TREE_OPERAND (arg, 0);
3008 /* For &x[y], return x+y */
3009 if (TREE_CODE (arg) == ARRAY_REF)
3011 tree op0 = TREE_OPERAND (arg, 0);
3012 if (!c_mark_addressable (op0))
3013 return error_mark_node;
3014 return build_binary_op (PLUS_EXPR,
3015 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3016 ? array_to_pointer_conversion (op0)
3017 : op0),
3018 TREE_OPERAND (arg, 1), 1);
3021 /* Anything not already handled and not a true memory reference
3022 or a non-lvalue array is an error. */
3023 else if (typecode != FUNCTION_TYPE && !flag
3024 && !lvalue_or_else (arg, lv_addressof))
3025 return error_mark_node;
3027 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3028 argtype = TREE_TYPE (arg);
3030 /* If the lvalue is const or volatile, merge that into the type
3031 to which the address will point. Note that you can't get a
3032 restricted pointer by taking the address of something, so we
3033 only have to deal with `const' and `volatile' here. */
3034 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3035 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3036 argtype = c_build_type_variant (argtype,
3037 TREE_READONLY (arg),
3038 TREE_THIS_VOLATILE (arg));
3040 if (!c_mark_addressable (arg))
3041 return error_mark_node;
3043 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3044 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3046 argtype = build_pointer_type (argtype);
3048 /* ??? Cope with user tricks that amount to offsetof. Delete this
3049 when we have proper support for integer constant expressions. */
3050 val = get_base_address (arg);
3051 if (val && TREE_CODE (val) == INDIRECT_REF
3052 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3054 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
3056 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3057 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3060 val = build1 (ADDR_EXPR, argtype, arg);
3062 return val;
3064 default:
3065 gcc_unreachable ();
3068 if (argtype == 0)
3069 argtype = TREE_TYPE (arg);
3070 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3071 : fold_build1 (code, argtype, arg);
3074 /* Return nonzero if REF is an lvalue valid for this language.
3075 Lvalues can be assigned, unless their type has TYPE_READONLY.
3076 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3078 static int
3079 lvalue_p (tree ref)
3081 enum tree_code code = TREE_CODE (ref);
3083 switch (code)
3085 case REALPART_EXPR:
3086 case IMAGPART_EXPR:
3087 case COMPONENT_REF:
3088 return lvalue_p (TREE_OPERAND (ref, 0));
3090 case COMPOUND_LITERAL_EXPR:
3091 case STRING_CST:
3092 return 1;
3094 case INDIRECT_REF:
3095 case ARRAY_REF:
3096 case VAR_DECL:
3097 case PARM_DECL:
3098 case RESULT_DECL:
3099 case ERROR_MARK:
3100 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3101 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3103 case BIND_EXPR:
3104 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3106 default:
3107 return 0;
3111 /* Give an error for storing in something that is 'const'. */
3113 static void
3114 readonly_error (tree arg, enum lvalue_use use)
3116 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3117 || use == lv_asm);
3118 /* Using this macro rather than (for example) arrays of messages
3119 ensures that all the format strings are checked at compile
3120 time. */
3121 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3122 : (use == lv_increment ? (I) \
3123 : (use == lv_decrement ? (D) : (AS))))
3124 if (TREE_CODE (arg) == COMPONENT_REF)
3126 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3127 readonly_error (TREE_OPERAND (arg, 0), use);
3128 else
3129 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3130 G_("increment of read-only member %qD"),
3131 G_("decrement of read-only member %qD"),
3132 G_("read-only member %qD used as %<asm%> output")),
3133 TREE_OPERAND (arg, 1));
3135 else if (TREE_CODE (arg) == VAR_DECL)
3136 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3137 G_("increment of read-only variable %qD"),
3138 G_("decrement of read-only variable %qD"),
3139 G_("read-only variable %qD used as %<asm%> output")),
3140 arg);
3141 else
3142 error (READONLY_MSG (G_("assignment of read-only location"),
3143 G_("increment of read-only location"),
3144 G_("decrement of read-only location"),
3145 G_("read-only location used as %<asm%> output")));
3149 /* Return nonzero if REF is an lvalue valid for this language;
3150 otherwise, print an error message and return zero. USE says
3151 how the lvalue is being used and so selects the error message. */
3153 static int
3154 lvalue_or_else (tree ref, enum lvalue_use use)
3156 int win = lvalue_p (ref);
3158 if (!win)
3159 lvalue_error (use);
3161 return win;
3164 /* Mark EXP saying that we need to be able to take the
3165 address of it; it should not be allocated in a register.
3166 Returns true if successful. */
3168 bool
3169 c_mark_addressable (tree exp)
3171 tree x = exp;
3173 while (1)
3174 switch (TREE_CODE (x))
3176 case COMPONENT_REF:
3177 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3179 error
3180 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3181 return false;
3184 /* ... fall through ... */
3186 case ADDR_EXPR:
3187 case ARRAY_REF:
3188 case REALPART_EXPR:
3189 case IMAGPART_EXPR:
3190 x = TREE_OPERAND (x, 0);
3191 break;
3193 case COMPOUND_LITERAL_EXPR:
3194 case CONSTRUCTOR:
3195 TREE_ADDRESSABLE (x) = 1;
3196 return true;
3198 case VAR_DECL:
3199 case CONST_DECL:
3200 case PARM_DECL:
3201 case RESULT_DECL:
3202 if (C_DECL_REGISTER (x)
3203 && DECL_NONLOCAL (x))
3205 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3207 error
3208 ("global register variable %qD used in nested function", x);
3209 return false;
3211 pedwarn ("register variable %qD used in nested function", x);
3213 else if (C_DECL_REGISTER (x))
3215 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3216 error ("address of global register variable %qD requested", x);
3217 else
3218 error ("address of register variable %qD requested", x);
3219 return false;
3222 /* drops in */
3223 case FUNCTION_DECL:
3224 TREE_ADDRESSABLE (x) = 1;
3225 /* drops out */
3226 default:
3227 return true;
3231 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3233 tree
3234 build_conditional_expr (tree ifexp, tree op1, tree op2)
3236 tree type1;
3237 tree type2;
3238 enum tree_code code1;
3239 enum tree_code code2;
3240 tree result_type = NULL;
3241 tree orig_op1 = op1, orig_op2 = op2;
3243 /* Promote both alternatives. */
3245 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3246 op1 = default_conversion (op1);
3247 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3248 op2 = default_conversion (op2);
3250 if (TREE_CODE (ifexp) == ERROR_MARK
3251 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3252 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3253 return error_mark_node;
3255 type1 = TREE_TYPE (op1);
3256 code1 = TREE_CODE (type1);
3257 type2 = TREE_TYPE (op2);
3258 code2 = TREE_CODE (type2);
3260 /* C90 does not permit non-lvalue arrays in conditional expressions.
3261 In C99 they will be pointers by now. */
3262 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3264 error ("non-lvalue array in conditional expression");
3265 return error_mark_node;
3268 /* Quickly detect the usual case where op1 and op2 have the same type
3269 after promotion. */
3270 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3272 if (type1 == type2)
3273 result_type = type1;
3274 else
3275 result_type = TYPE_MAIN_VARIANT (type1);
3277 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3278 || code1 == COMPLEX_TYPE)
3279 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3280 || code2 == COMPLEX_TYPE))
3282 result_type = c_common_type (type1, type2);
3284 /* If -Wsign-compare, warn here if type1 and type2 have
3285 different signedness. We'll promote the signed to unsigned
3286 and later code won't know it used to be different.
3287 Do this check on the original types, so that explicit casts
3288 will be considered, but default promotions won't. */
3289 if (warn_sign_compare && !skip_evaluation)
3291 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3292 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3294 if (unsigned_op1 ^ unsigned_op2)
3296 /* Do not warn if the result type is signed, since the
3297 signed type will only be chosen if it can represent
3298 all the values of the unsigned type. */
3299 if (!TYPE_UNSIGNED (result_type))
3300 /* OK */;
3301 /* Do not warn if the signed quantity is an unsuffixed
3302 integer literal (or some static constant expression
3303 involving such literals) and it is non-negative. */
3304 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3305 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3306 /* OK */;
3307 else
3308 warning (0, "signed and unsigned type in conditional expression");
3312 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3314 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3315 pedwarn ("ISO C forbids conditional expr with only one void side");
3316 result_type = void_type_node;
3318 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3320 if (comp_target_types (type1, type2))
3321 result_type = common_pointer_type (type1, type2);
3322 else if (null_pointer_constant_p (orig_op1))
3323 result_type = qualify_type (type2, type1);
3324 else if (null_pointer_constant_p (orig_op2))
3325 result_type = qualify_type (type1, type2);
3326 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3328 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3329 pedwarn ("ISO C forbids conditional expr between "
3330 "%<void *%> and function pointer");
3331 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3332 TREE_TYPE (type2)));
3334 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3336 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3337 pedwarn ("ISO C forbids conditional expr between "
3338 "%<void *%> and function pointer");
3339 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3340 TREE_TYPE (type1)));
3342 else
3344 pedwarn ("pointer type mismatch in conditional expression");
3345 result_type = build_pointer_type (void_type_node);
3348 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3350 if (!null_pointer_constant_p (orig_op2))
3351 pedwarn ("pointer/integer type mismatch in conditional expression");
3352 else
3354 op2 = null_pointer_node;
3356 result_type = type1;
3358 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3360 if (!null_pointer_constant_p (orig_op1))
3361 pedwarn ("pointer/integer type mismatch in conditional expression");
3362 else
3364 op1 = null_pointer_node;
3366 result_type = type2;
3369 if (!result_type)
3371 if (flag_cond_mismatch)
3372 result_type = void_type_node;
3373 else
3375 error ("type mismatch in conditional expression");
3376 return error_mark_node;
3380 /* Merge const and volatile flags of the incoming types. */
3381 result_type
3382 = build_type_variant (result_type,
3383 TREE_READONLY (op1) || TREE_READONLY (op2),
3384 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3386 if (result_type != TREE_TYPE (op1))
3387 op1 = convert_and_check (result_type, op1);
3388 if (result_type != TREE_TYPE (op2))
3389 op2 = convert_and_check (result_type, op2);
3391 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3394 /* Return a compound expression that performs two expressions and
3395 returns the value of the second of them. */
3397 tree
3398 build_compound_expr (tree expr1, tree expr2)
3400 if (!TREE_SIDE_EFFECTS (expr1))
3402 /* The left-hand operand of a comma expression is like an expression
3403 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3404 any side-effects, unless it was explicitly cast to (void). */
3405 if (warn_unused_value)
3407 if (VOID_TYPE_P (TREE_TYPE (expr1))
3408 && (TREE_CODE (expr1) == NOP_EXPR
3409 || TREE_CODE (expr1) == CONVERT_EXPR))
3410 ; /* (void) a, b */
3411 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3412 && TREE_CODE (expr1) == COMPOUND_EXPR
3413 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3414 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3415 ; /* (void) a, (void) b, c */
3416 else
3417 warning (0, "left-hand operand of comma expression has no effect");
3421 /* With -Wunused, we should also warn if the left-hand operand does have
3422 side-effects, but computes a value which is not used. For example, in
3423 `foo() + bar(), baz()' the result of the `+' operator is not used,
3424 so we should issue a warning. */
3425 else if (warn_unused_value)
3426 warn_if_unused_value (expr1, input_location);
3428 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3431 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3433 tree
3434 build_c_cast (tree type, tree expr)
3436 tree value = expr;
3438 if (type == error_mark_node || expr == error_mark_node)
3439 return error_mark_node;
3441 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3442 only in <protocol> qualifications. But when constructing cast expressions,
3443 the protocols do matter and must be kept around. */
3444 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3445 return build1 (NOP_EXPR, type, expr);
3447 type = TYPE_MAIN_VARIANT (type);
3449 if (TREE_CODE (type) == ARRAY_TYPE)
3451 error ("cast specifies array type");
3452 return error_mark_node;
3455 if (TREE_CODE (type) == FUNCTION_TYPE)
3457 error ("cast specifies function type");
3458 return error_mark_node;
3461 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3463 if (pedantic)
3465 if (TREE_CODE (type) == RECORD_TYPE
3466 || TREE_CODE (type) == UNION_TYPE)
3467 pedwarn ("ISO C forbids casting nonscalar to the same type");
3470 else if (TREE_CODE (type) == UNION_TYPE)
3472 tree field;
3474 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3475 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3476 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3477 break;
3479 if (field)
3481 tree t;
3483 if (pedantic)
3484 pedwarn ("ISO C forbids casts to union type");
3485 t = digest_init (type,
3486 build_constructor_single (type, field, value),
3487 true, 0);
3488 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3489 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3490 return t;
3492 error ("cast to union type from type not present in union");
3493 return error_mark_node;
3495 else
3497 tree otype, ovalue;
3499 if (type == void_type_node)
3500 return build1 (CONVERT_EXPR, type, value);
3502 otype = TREE_TYPE (value);
3504 /* Optionally warn about potentially worrisome casts. */
3506 if (warn_cast_qual
3507 && TREE_CODE (type) == POINTER_TYPE
3508 && TREE_CODE (otype) == POINTER_TYPE)
3510 tree in_type = type;
3511 tree in_otype = otype;
3512 int added = 0;
3513 int discarded = 0;
3515 /* Check that the qualifiers on IN_TYPE are a superset of
3516 the qualifiers of IN_OTYPE. The outermost level of
3517 POINTER_TYPE nodes is uninteresting and we stop as soon
3518 as we hit a non-POINTER_TYPE node on either type. */
3521 in_otype = TREE_TYPE (in_otype);
3522 in_type = TREE_TYPE (in_type);
3524 /* GNU C allows cv-qualified function types. 'const'
3525 means the function is very pure, 'volatile' means it
3526 can't return. We need to warn when such qualifiers
3527 are added, not when they're taken away. */
3528 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3529 && TREE_CODE (in_type) == FUNCTION_TYPE)
3530 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3531 else
3532 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3534 while (TREE_CODE (in_type) == POINTER_TYPE
3535 && TREE_CODE (in_otype) == POINTER_TYPE);
3537 if (added)
3538 warning (0, "cast adds new qualifiers to function type");
3540 if (discarded)
3541 /* There are qualifiers present in IN_OTYPE that are not
3542 present in IN_TYPE. */
3543 warning (0, "cast discards qualifiers from pointer target type");
3546 /* Warn about possible alignment problems. */
3547 if (STRICT_ALIGNMENT
3548 && TREE_CODE (type) == POINTER_TYPE
3549 && TREE_CODE (otype) == POINTER_TYPE
3550 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3551 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3552 /* Don't warn about opaque types, where the actual alignment
3553 restriction is unknown. */
3554 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3555 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3556 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3557 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3558 warning (OPT_Wcast_align,
3559 "cast increases required alignment of target type");
3561 if (TREE_CODE (type) == INTEGER_TYPE
3562 && TREE_CODE (otype) == POINTER_TYPE
3563 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3564 /* Unlike conversion of integers to pointers, where the
3565 warning is disabled for converting constants because
3566 of cases such as SIG_*, warn about converting constant
3567 pointers to integers. In some cases it may cause unwanted
3568 sign extension, and a warning is appropriate. */
3569 warning (OPT_Wpointer_to_int_cast,
3570 "cast from pointer to integer of different size");
3572 if (TREE_CODE (value) == CALL_EXPR
3573 && TREE_CODE (type) != TREE_CODE (otype))
3574 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3575 "to non-matching type %qT", otype, type);
3577 if (TREE_CODE (type) == POINTER_TYPE
3578 && TREE_CODE (otype) == INTEGER_TYPE
3579 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3580 /* Don't warn about converting any constant. */
3581 && !TREE_CONSTANT (value))
3582 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3583 "of different size");
3585 strict_aliasing_warning (otype, type, expr);
3587 /* If pedantic, warn for conversions between function and object
3588 pointer types, except for converting a null pointer constant
3589 to function pointer type. */
3590 if (pedantic
3591 && TREE_CODE (type) == POINTER_TYPE
3592 && TREE_CODE (otype) == POINTER_TYPE
3593 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3594 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3595 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3597 if (pedantic
3598 && TREE_CODE (type) == POINTER_TYPE
3599 && TREE_CODE (otype) == POINTER_TYPE
3600 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3601 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3602 && !null_pointer_constant_p (value))
3603 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3605 ovalue = value;
3606 value = convert (type, value);
3608 /* Ignore any integer overflow caused by the cast. */
3609 if (TREE_CODE (value) == INTEGER_CST)
3611 if (CONSTANT_CLASS_P (ovalue)
3612 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3614 /* Avoid clobbering a shared constant. */
3615 value = copy_node (value);
3616 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3617 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3619 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3620 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3621 value = build_int_cst_wide (TREE_TYPE (value),
3622 TREE_INT_CST_LOW (value),
3623 TREE_INT_CST_HIGH (value));
3627 /* Don't let a cast be an lvalue. */
3628 if (value == expr)
3629 value = non_lvalue (value);
3631 return value;
3634 /* Interpret a cast of expression EXPR to type TYPE. */
3635 tree
3636 c_cast_expr (struct c_type_name *type_name, tree expr)
3638 tree type;
3639 int saved_wsp = warn_strict_prototypes;
3641 /* This avoids warnings about unprototyped casts on
3642 integers. E.g. "#define SIG_DFL (void(*)())0". */
3643 if (TREE_CODE (expr) == INTEGER_CST)
3644 warn_strict_prototypes = 0;
3645 type = groktypename (type_name);
3646 warn_strict_prototypes = saved_wsp;
3648 return build_c_cast (type, expr);
3651 /* Build an assignment expression of lvalue LHS from value RHS.
3652 MODIFYCODE is the code for a binary operator that we use
3653 to combine the old value of LHS with RHS to get the new value.
3654 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3656 tree
3657 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3659 tree result;
3660 tree newrhs;
3661 tree lhstype = TREE_TYPE (lhs);
3662 tree olhstype = lhstype;
3664 /* Types that aren't fully specified cannot be used in assignments. */
3665 lhs = require_complete_type (lhs);
3667 /* Avoid duplicate error messages from operands that had errors. */
3668 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3669 return error_mark_node;
3671 if (!lvalue_or_else (lhs, lv_assign))
3672 return error_mark_node;
3674 STRIP_TYPE_NOPS (rhs);
3676 newrhs = rhs;
3678 /* If a binary op has been requested, combine the old LHS value with the RHS
3679 producing the value we should actually store into the LHS. */
3681 if (modifycode != NOP_EXPR)
3683 lhs = stabilize_reference (lhs);
3684 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3687 /* Give an error for storing in something that is 'const'. */
3689 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3690 || ((TREE_CODE (lhstype) == RECORD_TYPE
3691 || TREE_CODE (lhstype) == UNION_TYPE)
3692 && C_TYPE_FIELDS_READONLY (lhstype)))
3694 readonly_error (lhs, lv_assign);
3695 return error_mark_node;
3698 /* If storing into a structure or union member,
3699 it has probably been given type `int'.
3700 Compute the type that would go with
3701 the actual amount of storage the member occupies. */
3703 if (TREE_CODE (lhs) == COMPONENT_REF
3704 && (TREE_CODE (lhstype) == INTEGER_TYPE
3705 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3706 || TREE_CODE (lhstype) == REAL_TYPE
3707 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3708 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3710 /* If storing in a field that is in actuality a short or narrower than one,
3711 we must store in the field in its actual type. */
3713 if (lhstype != TREE_TYPE (lhs))
3715 lhs = copy_node (lhs);
3716 TREE_TYPE (lhs) = lhstype;
3719 /* Convert new value to destination type. */
3721 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3722 NULL_TREE, NULL_TREE, 0);
3723 if (TREE_CODE (newrhs) == ERROR_MARK)
3724 return error_mark_node;
3726 /* Emit ObjC write barrier, if necessary. */
3727 if (c_dialect_objc () && flag_objc_gc)
3729 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3730 if (result)
3731 return result;
3734 /* Scan operands. */
3736 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3737 TREE_SIDE_EFFECTS (result) = 1;
3739 /* If we got the LHS in a different type for storing in,
3740 convert the result back to the nominal type of LHS
3741 so that the value we return always has the same type
3742 as the LHS argument. */
3744 if (olhstype == TREE_TYPE (result))
3745 return result;
3746 return convert_for_assignment (olhstype, result, ic_assign,
3747 NULL_TREE, NULL_TREE, 0);
3750 /* Convert value RHS to type TYPE as preparation for an assignment
3751 to an lvalue of type TYPE.
3752 The real work of conversion is done by `convert'.
3753 The purpose of this function is to generate error messages
3754 for assignments that are not allowed in C.
3755 ERRTYPE says whether it is argument passing, assignment,
3756 initialization or return.
3758 FUNCTION is a tree for the function being called.
3759 PARMNUM is the number of the argument, for printing in error messages. */
3761 static tree
3762 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3763 tree fundecl, tree function, int parmnum)
3765 enum tree_code codel = TREE_CODE (type);
3766 tree rhstype;
3767 enum tree_code coder;
3768 tree rname = NULL_TREE;
3769 bool objc_ok = false;
3771 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3773 tree selector;
3774 /* Change pointer to function to the function itself for
3775 diagnostics. */
3776 if (TREE_CODE (function) == ADDR_EXPR
3777 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3778 function = TREE_OPERAND (function, 0);
3780 /* Handle an ObjC selector specially for diagnostics. */
3781 selector = objc_message_selector ();
3782 rname = function;
3783 if (selector && parmnum > 2)
3785 rname = selector;
3786 parmnum -= 2;
3790 /* This macro is used to emit diagnostics to ensure that all format
3791 strings are complete sentences, visible to gettext and checked at
3792 compile time. */
3793 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3794 do { \
3795 switch (errtype) \
3797 case ic_argpass: \
3798 pedwarn (AR, parmnum, rname); \
3799 break; \
3800 case ic_argpass_nonproto: \
3801 warning (0, AR, parmnum, rname); \
3802 break; \
3803 case ic_assign: \
3804 pedwarn (AS); \
3805 break; \
3806 case ic_init: \
3807 pedwarn (IN); \
3808 break; \
3809 case ic_return: \
3810 pedwarn (RE); \
3811 break; \
3812 default: \
3813 gcc_unreachable (); \
3815 } while (0)
3817 STRIP_TYPE_NOPS (rhs);
3819 if (optimize && TREE_CODE (rhs) == VAR_DECL
3820 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3821 rhs = decl_constant_value_for_broken_optimization (rhs);
3823 rhstype = TREE_TYPE (rhs);
3824 coder = TREE_CODE (rhstype);
3826 if (coder == ERROR_MARK)
3827 return error_mark_node;
3829 if (c_dialect_objc ())
3831 int parmno;
3833 switch (errtype)
3835 case ic_return:
3836 parmno = 0;
3837 break;
3839 case ic_assign:
3840 parmno = -1;
3841 break;
3843 case ic_init:
3844 parmno = -2;
3845 break;
3847 default:
3848 parmno = parmnum;
3849 break;
3852 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3855 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3857 overflow_warning (rhs);
3858 return rhs;
3861 if (coder == VOID_TYPE)
3863 /* Except for passing an argument to an unprototyped function,
3864 this is a constraint violation. When passing an argument to
3865 an unprototyped function, it is compile-time undefined;
3866 making it a constraint in that case was rejected in
3867 DR#252. */
3868 error ("void value not ignored as it ought to be");
3869 return error_mark_node;
3871 /* A type converts to a reference to it.
3872 This code doesn't fully support references, it's just for the
3873 special case of va_start and va_copy. */
3874 if (codel == REFERENCE_TYPE
3875 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3877 if (!lvalue_p (rhs))
3879 error ("cannot pass rvalue to reference parameter");
3880 return error_mark_node;
3882 if (!c_mark_addressable (rhs))
3883 return error_mark_node;
3884 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3886 /* We already know that these two types are compatible, but they
3887 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3888 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3889 likely to be va_list, a typedef to __builtin_va_list, which
3890 is different enough that it will cause problems later. */
3891 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3892 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3894 rhs = build1 (NOP_EXPR, type, rhs);
3895 return rhs;
3897 /* Some types can interconvert without explicit casts. */
3898 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3899 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3900 return convert (type, rhs);
3901 /* Arithmetic types all interconvert, and enum is treated like int. */
3902 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3903 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3904 || codel == BOOLEAN_TYPE)
3905 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3906 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3907 || coder == BOOLEAN_TYPE))
3908 return convert_and_check (type, rhs);
3910 /* Conversion to a transparent union from its member types.
3911 This applies only to function arguments. */
3912 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3913 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3915 tree memb, marginal_memb = NULL_TREE;
3917 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3919 tree memb_type = TREE_TYPE (memb);
3921 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3922 TYPE_MAIN_VARIANT (rhstype)))
3923 break;
3925 if (TREE_CODE (memb_type) != POINTER_TYPE)
3926 continue;
3928 if (coder == POINTER_TYPE)
3930 tree ttl = TREE_TYPE (memb_type);
3931 tree ttr = TREE_TYPE (rhstype);
3933 /* Any non-function converts to a [const][volatile] void *
3934 and vice versa; otherwise, targets must be the same.
3935 Meanwhile, the lhs target must have all the qualifiers of
3936 the rhs. */
3937 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3938 || comp_target_types (memb_type, rhstype))
3940 /* If this type won't generate any warnings, use it. */
3941 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3942 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3943 && TREE_CODE (ttl) == FUNCTION_TYPE)
3944 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3945 == TYPE_QUALS (ttr))
3946 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3947 == TYPE_QUALS (ttl))))
3948 break;
3950 /* Keep looking for a better type, but remember this one. */
3951 if (!marginal_memb)
3952 marginal_memb = memb;
3956 /* Can convert integer zero to any pointer type. */
3957 if (null_pointer_constant_p (rhs))
3959 rhs = null_pointer_node;
3960 break;
3964 if (memb || marginal_memb)
3966 if (!memb)
3968 /* We have only a marginally acceptable member type;
3969 it needs a warning. */
3970 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3971 tree ttr = TREE_TYPE (rhstype);
3973 /* Const and volatile mean something different for function
3974 types, so the usual warnings are not appropriate. */
3975 if (TREE_CODE (ttr) == FUNCTION_TYPE
3976 && TREE_CODE (ttl) == FUNCTION_TYPE)
3978 /* Because const and volatile on functions are
3979 restrictions that say the function will not do
3980 certain things, it is okay to use a const or volatile
3981 function where an ordinary one is wanted, but not
3982 vice-versa. */
3983 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3984 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3985 "makes qualified function "
3986 "pointer from unqualified"),
3987 G_("assignment makes qualified "
3988 "function pointer from "
3989 "unqualified"),
3990 G_("initialization makes qualified "
3991 "function pointer from "
3992 "unqualified"),
3993 G_("return makes qualified function "
3994 "pointer from unqualified"));
3996 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3997 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3998 "qualifiers from pointer target type"),
3999 G_("assignment discards qualifiers "
4000 "from pointer target type"),
4001 G_("initialization discards qualifiers "
4002 "from pointer target type"),
4003 G_("return discards qualifiers from "
4004 "pointer target type"));
4006 memb = marginal_memb;
4009 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4010 pedwarn ("ISO C prohibits argument conversion to union type");
4012 return build_constructor_single (type, memb, rhs);
4016 /* Conversions among pointers */
4017 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4018 && (coder == codel))
4020 tree ttl = TREE_TYPE (type);
4021 tree ttr = TREE_TYPE (rhstype);
4022 tree mvl = ttl;
4023 tree mvr = ttr;
4024 bool is_opaque_pointer;
4025 int target_cmp = 0; /* Cache comp_target_types () result. */
4027 if (TREE_CODE (mvl) != ARRAY_TYPE)
4028 mvl = TYPE_MAIN_VARIANT (mvl);
4029 if (TREE_CODE (mvr) != ARRAY_TYPE)
4030 mvr = TYPE_MAIN_VARIANT (mvr);
4031 /* Opaque pointers are treated like void pointers. */
4032 is_opaque_pointer = (targetm.vector_opaque_p (type)
4033 || targetm.vector_opaque_p (rhstype))
4034 && TREE_CODE (ttl) == VECTOR_TYPE
4035 && TREE_CODE (ttr) == VECTOR_TYPE;
4037 /* C++ does not allow the implicit conversion void* -> T*. However,
4038 for the purpose of reducing the number of false positives, we
4039 tolerate the special case of
4041 int *p = NULL;
4043 where NULL is typically defined in C to be '(void *) 0'. */
4044 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4045 warning (OPT_Wc___compat, "request for implicit conversion from "
4046 "%qT to %qT not permitted in C++", rhstype, type);
4048 /* Check if the right-hand side has a format attribute but the
4049 left-hand side doesn't. */
4050 if (warn_missing_format_attribute
4051 && check_missing_format_attribute (type, rhstype))
4053 switch (errtype)
4055 case ic_argpass:
4056 case ic_argpass_nonproto:
4057 warning (OPT_Wmissing_format_attribute,
4058 "argument %d of %qE might be "
4059 "a candidate for a format attribute",
4060 parmnum, rname);
4061 break;
4062 case ic_assign:
4063 warning (OPT_Wmissing_format_attribute,
4064 "assignment left-hand side might be "
4065 "a candidate for a format attribute");
4066 break;
4067 case ic_init:
4068 warning (OPT_Wmissing_format_attribute,
4069 "initialization left-hand side might be "
4070 "a candidate for a format attribute");
4071 break;
4072 case ic_return:
4073 warning (OPT_Wmissing_format_attribute,
4074 "return type might be "
4075 "a candidate for a format attribute");
4076 break;
4077 default:
4078 gcc_unreachable ();
4082 /* Any non-function converts to a [const][volatile] void *
4083 and vice versa; otherwise, targets must be the same.
4084 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4085 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4086 || (target_cmp = comp_target_types (type, rhstype))
4087 || is_opaque_pointer
4088 || (c_common_unsigned_type (mvl)
4089 == c_common_unsigned_type (mvr)))
4091 if (pedantic
4092 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4094 (VOID_TYPE_P (ttr)
4095 && !null_pointer_constant_p (rhs)
4096 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4097 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4098 "%qE between function pointer "
4099 "and %<void *%>"),
4100 G_("ISO C forbids assignment between "
4101 "function pointer and %<void *%>"),
4102 G_("ISO C forbids initialization between "
4103 "function pointer and %<void *%>"),
4104 G_("ISO C forbids return between function "
4105 "pointer and %<void *%>"));
4106 /* Const and volatile mean something different for function types,
4107 so the usual warnings are not appropriate. */
4108 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4109 && TREE_CODE (ttl) != FUNCTION_TYPE)
4111 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4113 /* Types differing only by the presence of the 'volatile'
4114 qualifier are acceptable if the 'volatile' has been added
4115 in by the Objective-C EH machinery. */
4116 if (!objc_type_quals_match (ttl, ttr))
4117 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4118 "qualifiers from pointer target type"),
4119 G_("assignment discards qualifiers "
4120 "from pointer target type"),
4121 G_("initialization discards qualifiers "
4122 "from pointer target type"),
4123 G_("return discards qualifiers from "
4124 "pointer target type"));
4126 /* If this is not a case of ignoring a mismatch in signedness,
4127 no warning. */
4128 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4129 || target_cmp)
4131 /* If there is a mismatch, do warn. */
4132 else if (warn_pointer_sign)
4133 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4134 "%d of %qE differ in signedness"),
4135 G_("pointer targets in assignment "
4136 "differ in signedness"),
4137 G_("pointer targets in initialization "
4138 "differ in signedness"),
4139 G_("pointer targets in return differ "
4140 "in signedness"));
4142 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4143 && TREE_CODE (ttr) == FUNCTION_TYPE)
4145 /* Because const and volatile on functions are restrictions
4146 that say the function will not do certain things,
4147 it is okay to use a const or volatile function
4148 where an ordinary one is wanted, but not vice-versa. */
4149 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4150 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4151 "qualified function pointer "
4152 "from unqualified"),
4153 G_("assignment makes qualified function "
4154 "pointer from unqualified"),
4155 G_("initialization makes qualified "
4156 "function pointer from unqualified"),
4157 G_("return makes qualified function "
4158 "pointer from unqualified"));
4161 else
4162 /* Avoid warning about the volatile ObjC EH puts on decls. */
4163 if (!objc_ok)
4164 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4165 "incompatible pointer type"),
4166 G_("assignment from incompatible pointer type"),
4167 G_("initialization from incompatible "
4168 "pointer type"),
4169 G_("return from incompatible pointer type"));
4171 return convert (type, rhs);
4173 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4175 /* ??? This should not be an error when inlining calls to
4176 unprototyped functions. */
4177 error ("invalid use of non-lvalue array");
4178 return error_mark_node;
4180 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4182 /* An explicit constant 0 can convert to a pointer,
4183 or one that results from arithmetic, even including
4184 a cast to integer type. */
4185 if (!null_pointer_constant_p (rhs))
4186 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4187 "pointer from integer without a cast"),
4188 G_("assignment makes pointer from integer "
4189 "without a cast"),
4190 G_("initialization makes pointer from "
4191 "integer without a cast"),
4192 G_("return makes pointer from integer "
4193 "without a cast"));
4195 return convert (type, rhs);
4197 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4199 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4200 "from pointer without a cast"),
4201 G_("assignment makes integer from pointer "
4202 "without a cast"),
4203 G_("initialization makes integer from pointer "
4204 "without a cast"),
4205 G_("return makes integer from pointer "
4206 "without a cast"));
4207 return convert (type, rhs);
4209 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4210 return convert (type, rhs);
4212 switch (errtype)
4214 case ic_argpass:
4215 case ic_argpass_nonproto:
4216 /* ??? This should not be an error when inlining calls to
4217 unprototyped functions. */
4218 error ("incompatible type for argument %d of %qE", parmnum, rname);
4219 break;
4220 case ic_assign:
4221 error ("incompatible types in assignment");
4222 break;
4223 case ic_init:
4224 error ("incompatible types in initialization");
4225 break;
4226 case ic_return:
4227 error ("incompatible types in return");
4228 break;
4229 default:
4230 gcc_unreachable ();
4233 return error_mark_node;
4236 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4237 is used for error and waring reporting and indicates which argument
4238 is being processed. */
4240 tree
4241 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4243 tree ret, type;
4245 /* If FN was prototyped, the value has been converted already
4246 in convert_arguments. */
4247 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4248 return value;
4250 type = TREE_TYPE (parm);
4251 ret = convert_for_assignment (type, value,
4252 ic_argpass_nonproto, fn,
4253 fn, argnum);
4254 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4255 && INTEGRAL_TYPE_P (type)
4256 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4257 ret = default_conversion (ret);
4258 return ret;
4261 /* If VALUE is a compound expr all of whose expressions are constant, then
4262 return its value. Otherwise, return error_mark_node.
4264 This is for handling COMPOUND_EXPRs as initializer elements
4265 which is allowed with a warning when -pedantic is specified. */
4267 static tree
4268 valid_compound_expr_initializer (tree value, tree endtype)
4270 if (TREE_CODE (value) == COMPOUND_EXPR)
4272 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4273 == error_mark_node)
4274 return error_mark_node;
4275 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4276 endtype);
4278 else if (!initializer_constant_valid_p (value, endtype))
4279 return error_mark_node;
4280 else
4281 return value;
4284 /* Perform appropriate conversions on the initial value of a variable,
4285 store it in the declaration DECL,
4286 and print any error messages that are appropriate.
4287 If the init is invalid, store an ERROR_MARK. */
4289 void
4290 store_init_value (tree decl, tree init)
4292 tree value, type;
4294 /* If variable's type was invalidly declared, just ignore it. */
4296 type = TREE_TYPE (decl);
4297 if (TREE_CODE (type) == ERROR_MARK)
4298 return;
4300 /* Digest the specified initializer into an expression. */
4302 value = digest_init (type, init, true, TREE_STATIC (decl));
4304 /* Store the expression if valid; else report error. */
4306 if (!in_system_header
4307 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4308 warning (OPT_Wtraditional, "traditional C rejects automatic "
4309 "aggregate initialization");
4311 DECL_INITIAL (decl) = value;
4313 /* ANSI wants warnings about out-of-range constant initializers. */
4314 STRIP_TYPE_NOPS (value);
4315 constant_expression_warning (value);
4317 /* Check if we need to set array size from compound literal size. */
4318 if (TREE_CODE (type) == ARRAY_TYPE
4319 && TYPE_DOMAIN (type) == 0
4320 && value != error_mark_node)
4322 tree inside_init = init;
4324 STRIP_TYPE_NOPS (inside_init);
4325 inside_init = fold (inside_init);
4327 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4329 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4331 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4333 /* For int foo[] = (int [3]){1}; we need to set array size
4334 now since later on array initializer will be just the
4335 brace enclosed list of the compound literal. */
4336 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4337 layout_type (type);
4338 layout_decl (decl, 0);
4344 /* Methods for storing and printing names for error messages. */
4346 /* Implement a spelling stack that allows components of a name to be pushed
4347 and popped. Each element on the stack is this structure. */
4349 struct spelling
4351 int kind;
4352 union
4354 unsigned HOST_WIDE_INT i;
4355 const char *s;
4356 } u;
4359 #define SPELLING_STRING 1
4360 #define SPELLING_MEMBER 2
4361 #define SPELLING_BOUNDS 3
4363 static struct spelling *spelling; /* Next stack element (unused). */
4364 static struct spelling *spelling_base; /* Spelling stack base. */
4365 static int spelling_size; /* Size of the spelling stack. */
4367 /* Macros to save and restore the spelling stack around push_... functions.
4368 Alternative to SAVE_SPELLING_STACK. */
4370 #define SPELLING_DEPTH() (spelling - spelling_base)
4371 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4373 /* Push an element on the spelling stack with type KIND and assign VALUE
4374 to MEMBER. */
4376 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4378 int depth = SPELLING_DEPTH (); \
4380 if (depth >= spelling_size) \
4382 spelling_size += 10; \
4383 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4384 spelling_size); \
4385 RESTORE_SPELLING_DEPTH (depth); \
4388 spelling->kind = (KIND); \
4389 spelling->MEMBER = (VALUE); \
4390 spelling++; \
4393 /* Push STRING on the stack. Printed literally. */
4395 static void
4396 push_string (const char *string)
4398 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4401 /* Push a member name on the stack. Printed as '.' STRING. */
4403 static void
4404 push_member_name (tree decl)
4406 const char *const string
4407 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4408 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4411 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4413 static void
4414 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4416 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4419 /* Compute the maximum size in bytes of the printed spelling. */
4421 static int
4422 spelling_length (void)
4424 int size = 0;
4425 struct spelling *p;
4427 for (p = spelling_base; p < spelling; p++)
4429 if (p->kind == SPELLING_BOUNDS)
4430 size += 25;
4431 else
4432 size += strlen (p->u.s) + 1;
4435 return size;
4438 /* Print the spelling to BUFFER and return it. */
4440 static char *
4441 print_spelling (char *buffer)
4443 char *d = buffer;
4444 struct spelling *p;
4446 for (p = spelling_base; p < spelling; p++)
4447 if (p->kind == SPELLING_BOUNDS)
4449 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4450 d += strlen (d);
4452 else
4454 const char *s;
4455 if (p->kind == SPELLING_MEMBER)
4456 *d++ = '.';
4457 for (s = p->u.s; (*d = *s++); d++)
4460 *d++ = '\0';
4461 return buffer;
4464 /* Issue an error message for a bad initializer component.
4465 MSGID identifies the message.
4466 The component name is taken from the spelling stack. */
4468 void
4469 error_init (const char *msgid)
4471 char *ofwhat;
4473 error ("%s", _(msgid));
4474 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4475 if (*ofwhat)
4476 error ("(near initialization for %qs)", ofwhat);
4479 /* Issue a pedantic warning for a bad initializer component.
4480 MSGID identifies the message.
4481 The component name is taken from the spelling stack. */
4483 void
4484 pedwarn_init (const char *msgid)
4486 char *ofwhat;
4488 pedwarn ("%s", _(msgid));
4489 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4490 if (*ofwhat)
4491 pedwarn ("(near initialization for %qs)", ofwhat);
4494 /* Issue a warning for a bad initializer component.
4495 MSGID identifies the message.
4496 The component name is taken from the spelling stack. */
4498 static void
4499 warning_init (const char *msgid)
4501 char *ofwhat;
4503 warning (0, "%s", _(msgid));
4504 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4505 if (*ofwhat)
4506 warning (0, "(near initialization for %qs)", ofwhat);
4509 /* If TYPE is an array type and EXPR is a parenthesized string
4510 constant, warn if pedantic that EXPR is being used to initialize an
4511 object of type TYPE. */
4513 void
4514 maybe_warn_string_init (tree type, struct c_expr expr)
4516 if (pedantic
4517 && TREE_CODE (type) == ARRAY_TYPE
4518 && TREE_CODE (expr.value) == STRING_CST
4519 && expr.original_code != STRING_CST)
4520 pedwarn_init ("array initialized from parenthesized string constant");
4523 /* Digest the parser output INIT as an initializer for type TYPE.
4524 Return a C expression of type TYPE to represent the initial value.
4526 If INIT is a string constant, STRICT_STRING is true if it is
4527 unparenthesized or we should not warn here for it being parenthesized.
4528 For other types of INIT, STRICT_STRING is not used.
4530 REQUIRE_CONSTANT requests an error if non-constant initializers or
4531 elements are seen. */
4533 static tree
4534 digest_init (tree type, tree init, bool strict_string, int require_constant)
4536 enum tree_code code = TREE_CODE (type);
4537 tree inside_init = init;
4539 if (type == error_mark_node
4540 || !init
4541 || init == error_mark_node
4542 || TREE_TYPE (init) == error_mark_node)
4543 return error_mark_node;
4545 STRIP_TYPE_NOPS (inside_init);
4547 inside_init = fold (inside_init);
4549 /* Initialization of an array of chars from a string constant
4550 optionally enclosed in braces. */
4552 if (code == ARRAY_TYPE && inside_init
4553 && TREE_CODE (inside_init) == STRING_CST)
4555 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4556 /* Note that an array could be both an array of character type
4557 and an array of wchar_t if wchar_t is signed char or unsigned
4558 char. */
4559 bool char_array = (typ1 == char_type_node
4560 || typ1 == signed_char_type_node
4561 || typ1 == unsigned_char_type_node);
4562 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4563 if (char_array || wchar_array)
4565 struct c_expr expr;
4566 bool char_string;
4567 expr.value = inside_init;
4568 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4569 maybe_warn_string_init (type, expr);
4571 char_string
4572 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4573 == char_type_node);
4575 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4576 TYPE_MAIN_VARIANT (type)))
4577 return inside_init;
4579 if (!wchar_array && !char_string)
4581 error_init ("char-array initialized from wide string");
4582 return error_mark_node;
4584 if (char_string && !char_array)
4586 error_init ("wchar_t-array initialized from non-wide string");
4587 return error_mark_node;
4590 TREE_TYPE (inside_init) = type;
4591 if (TYPE_DOMAIN (type) != 0
4592 && TYPE_SIZE (type) != 0
4593 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4594 /* Subtract 1 (or sizeof (wchar_t))
4595 because it's ok to ignore the terminating null char
4596 that is counted in the length of the constant. */
4597 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4598 TREE_STRING_LENGTH (inside_init)
4599 - ((TYPE_PRECISION (typ1)
4600 != TYPE_PRECISION (char_type_node))
4601 ? (TYPE_PRECISION (wchar_type_node)
4602 / BITS_PER_UNIT)
4603 : 1)))
4604 pedwarn_init ("initializer-string for array of chars is too long");
4606 return inside_init;
4608 else if (INTEGRAL_TYPE_P (typ1))
4610 error_init ("array of inappropriate type initialized "
4611 "from string constant");
4612 return error_mark_node;
4616 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4617 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4618 below and handle as a constructor. */
4619 if (code == VECTOR_TYPE
4620 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4621 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4622 && TREE_CONSTANT (inside_init))
4624 if (TREE_CODE (inside_init) == VECTOR_CST
4625 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4626 TYPE_MAIN_VARIANT (type)))
4627 return inside_init;
4629 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4631 unsigned HOST_WIDE_INT ix;
4632 tree value;
4633 bool constant_p = true;
4635 /* Iterate through elements and check if all constructor
4636 elements are *_CSTs. */
4637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4638 if (!CONSTANT_CLASS_P (value))
4640 constant_p = false;
4641 break;
4644 if (constant_p)
4645 return build_vector_from_ctor (type,
4646 CONSTRUCTOR_ELTS (inside_init));
4650 /* Any type can be initialized
4651 from an expression of the same type, optionally with braces. */
4653 if (inside_init && TREE_TYPE (inside_init) != 0
4654 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4655 TYPE_MAIN_VARIANT (type))
4656 || (code == ARRAY_TYPE
4657 && comptypes (TREE_TYPE (inside_init), type))
4658 || (code == VECTOR_TYPE
4659 && comptypes (TREE_TYPE (inside_init), type))
4660 || (code == POINTER_TYPE
4661 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4662 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4663 TREE_TYPE (type)))))
4665 if (code == POINTER_TYPE)
4667 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4669 if (TREE_CODE (inside_init) == STRING_CST
4670 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4671 inside_init = array_to_pointer_conversion (inside_init);
4672 else
4674 error_init ("invalid use of non-lvalue array");
4675 return error_mark_node;
4680 if (code == VECTOR_TYPE)
4681 /* Although the types are compatible, we may require a
4682 conversion. */
4683 inside_init = convert (type, inside_init);
4685 if (require_constant && !flag_isoc99
4686 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4688 /* As an extension, allow initializing objects with static storage
4689 duration with compound literals (which are then treated just as
4690 the brace enclosed list they contain). */
4691 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4692 inside_init = DECL_INITIAL (decl);
4695 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4696 && TREE_CODE (inside_init) != CONSTRUCTOR)
4698 error_init ("array initialized from non-constant array expression");
4699 return error_mark_node;
4702 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4703 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4705 /* Compound expressions can only occur here if -pedantic or
4706 -pedantic-errors is specified. In the later case, we always want
4707 an error. In the former case, we simply want a warning. */
4708 if (require_constant && pedantic
4709 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4711 inside_init
4712 = valid_compound_expr_initializer (inside_init,
4713 TREE_TYPE (inside_init));
4714 if (inside_init == error_mark_node)
4715 error_init ("initializer element is not constant");
4716 else
4717 pedwarn_init ("initializer element is not constant");
4718 if (flag_pedantic_errors)
4719 inside_init = error_mark_node;
4721 else if (require_constant
4722 && !initializer_constant_valid_p (inside_init,
4723 TREE_TYPE (inside_init)))
4725 error_init ("initializer element is not constant");
4726 inside_init = error_mark_node;
4729 /* Added to enable additional -Wmissing-format-attribute warnings. */
4730 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4731 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4732 NULL_TREE, 0);
4733 return inside_init;
4736 /* Handle scalar types, including conversions. */
4738 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4739 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4740 || code == VECTOR_TYPE)
4742 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4743 && (TREE_CODE (init) == STRING_CST
4744 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4745 init = array_to_pointer_conversion (init);
4746 inside_init
4747 = convert_for_assignment (type, init, ic_init,
4748 NULL_TREE, NULL_TREE, 0);
4750 /* Check to see if we have already given an error message. */
4751 if (inside_init == error_mark_node)
4753 else if (require_constant && !TREE_CONSTANT (inside_init))
4755 error_init ("initializer element is not constant");
4756 inside_init = error_mark_node;
4758 else if (require_constant
4759 && !initializer_constant_valid_p (inside_init,
4760 TREE_TYPE (inside_init)))
4762 error_init ("initializer element is not computable at load time");
4763 inside_init = error_mark_node;
4766 return inside_init;
4769 /* Come here only for records and arrays. */
4771 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4773 error_init ("variable-sized object may not be initialized");
4774 return error_mark_node;
4777 error_init ("invalid initializer");
4778 return error_mark_node;
4781 /* Handle initializers that use braces. */
4783 /* Type of object we are accumulating a constructor for.
4784 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4785 static tree constructor_type;
4787 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4788 left to fill. */
4789 static tree constructor_fields;
4791 /* For an ARRAY_TYPE, this is the specified index
4792 at which to store the next element we get. */
4793 static tree constructor_index;
4795 /* For an ARRAY_TYPE, this is the maximum index. */
4796 static tree constructor_max_index;
4798 /* For a RECORD_TYPE, this is the first field not yet written out. */
4799 static tree constructor_unfilled_fields;
4801 /* For an ARRAY_TYPE, this is the index of the first element
4802 not yet written out. */
4803 static tree constructor_unfilled_index;
4805 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4806 This is so we can generate gaps between fields, when appropriate. */
4807 static tree constructor_bit_index;
4809 /* If we are saving up the elements rather than allocating them,
4810 this is the list of elements so far (in reverse order,
4811 most recent first). */
4812 static VEC(constructor_elt,gc) *constructor_elements;
4814 /* 1 if constructor should be incrementally stored into a constructor chain,
4815 0 if all the elements should be kept in AVL tree. */
4816 static int constructor_incremental;
4818 /* 1 if so far this constructor's elements are all compile-time constants. */
4819 static int constructor_constant;
4821 /* 1 if so far this constructor's elements are all valid address constants. */
4822 static int constructor_simple;
4824 /* 1 if this constructor is erroneous so far. */
4825 static int constructor_erroneous;
4827 /* Structure for managing pending initializer elements, organized as an
4828 AVL tree. */
4830 struct init_node
4832 struct init_node *left, *right;
4833 struct init_node *parent;
4834 int balance;
4835 tree purpose;
4836 tree value;
4839 /* Tree of pending elements at this constructor level.
4840 These are elements encountered out of order
4841 which belong at places we haven't reached yet in actually
4842 writing the output.
4843 Will never hold tree nodes across GC runs. */
4844 static struct init_node *constructor_pending_elts;
4846 /* The SPELLING_DEPTH of this constructor. */
4847 static int constructor_depth;
4849 /* DECL node for which an initializer is being read.
4850 0 means we are reading a constructor expression
4851 such as (struct foo) {...}. */
4852 static tree constructor_decl;
4854 /* Nonzero if this is an initializer for a top-level decl. */
4855 static int constructor_top_level;
4857 /* Nonzero if there were any member designators in this initializer. */
4858 static int constructor_designated;
4860 /* Nesting depth of designator list. */
4861 static int designator_depth;
4863 /* Nonzero if there were diagnosed errors in this designator list. */
4864 static int designator_erroneous;
4867 /* This stack has a level for each implicit or explicit level of
4868 structuring in the initializer, including the outermost one. It
4869 saves the values of most of the variables above. */
4871 struct constructor_range_stack;
4873 struct constructor_stack
4875 struct constructor_stack *next;
4876 tree type;
4877 tree fields;
4878 tree index;
4879 tree max_index;
4880 tree unfilled_index;
4881 tree unfilled_fields;
4882 tree bit_index;
4883 VEC(constructor_elt,gc) *elements;
4884 struct init_node *pending_elts;
4885 int offset;
4886 int depth;
4887 /* If value nonzero, this value should replace the entire
4888 constructor at this level. */
4889 struct c_expr replacement_value;
4890 struct constructor_range_stack *range_stack;
4891 char constant;
4892 char simple;
4893 char implicit;
4894 char erroneous;
4895 char outer;
4896 char incremental;
4897 char designated;
4900 static struct constructor_stack *constructor_stack;
4902 /* This stack represents designators from some range designator up to
4903 the last designator in the list. */
4905 struct constructor_range_stack
4907 struct constructor_range_stack *next, *prev;
4908 struct constructor_stack *stack;
4909 tree range_start;
4910 tree index;
4911 tree range_end;
4912 tree fields;
4915 static struct constructor_range_stack *constructor_range_stack;
4917 /* This stack records separate initializers that are nested.
4918 Nested initializers can't happen in ANSI C, but GNU C allows them
4919 in cases like { ... (struct foo) { ... } ... }. */
4921 struct initializer_stack
4923 struct initializer_stack *next;
4924 tree decl;
4925 struct constructor_stack *constructor_stack;
4926 struct constructor_range_stack *constructor_range_stack;
4927 VEC(constructor_elt,gc) *elements;
4928 struct spelling *spelling;
4929 struct spelling *spelling_base;
4930 int spelling_size;
4931 char top_level;
4932 char require_constant_value;
4933 char require_constant_elements;
4936 static struct initializer_stack *initializer_stack;
4938 /* Prepare to parse and output the initializer for variable DECL. */
4940 void
4941 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4943 const char *locus;
4944 struct initializer_stack *p = XNEW (struct initializer_stack);
4946 p->decl = constructor_decl;
4947 p->require_constant_value = require_constant_value;
4948 p->require_constant_elements = require_constant_elements;
4949 p->constructor_stack = constructor_stack;
4950 p->constructor_range_stack = constructor_range_stack;
4951 p->elements = constructor_elements;
4952 p->spelling = spelling;
4953 p->spelling_base = spelling_base;
4954 p->spelling_size = spelling_size;
4955 p->top_level = constructor_top_level;
4956 p->next = initializer_stack;
4957 initializer_stack = p;
4959 constructor_decl = decl;
4960 constructor_designated = 0;
4961 constructor_top_level = top_level;
4963 if (decl != 0 && decl != error_mark_node)
4965 require_constant_value = TREE_STATIC (decl);
4966 require_constant_elements
4967 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4968 /* For a scalar, you can always use any value to initialize,
4969 even within braces. */
4970 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4971 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4972 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4973 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4974 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4976 else
4978 require_constant_value = 0;
4979 require_constant_elements = 0;
4980 locus = "(anonymous)";
4983 constructor_stack = 0;
4984 constructor_range_stack = 0;
4986 missing_braces_mentioned = 0;
4988 spelling_base = 0;
4989 spelling_size = 0;
4990 RESTORE_SPELLING_DEPTH (0);
4992 if (locus)
4993 push_string (locus);
4996 void
4997 finish_init (void)
4999 struct initializer_stack *p = initializer_stack;
5001 /* Free the whole constructor stack of this initializer. */
5002 while (constructor_stack)
5004 struct constructor_stack *q = constructor_stack;
5005 constructor_stack = q->next;
5006 free (q);
5009 gcc_assert (!constructor_range_stack);
5011 /* Pop back to the data of the outer initializer (if any). */
5012 free (spelling_base);
5014 constructor_decl = p->decl;
5015 require_constant_value = p->require_constant_value;
5016 require_constant_elements = p->require_constant_elements;
5017 constructor_stack = p->constructor_stack;
5018 constructor_range_stack = p->constructor_range_stack;
5019 constructor_elements = p->elements;
5020 spelling = p->spelling;
5021 spelling_base = p->spelling_base;
5022 spelling_size = p->spelling_size;
5023 constructor_top_level = p->top_level;
5024 initializer_stack = p->next;
5025 free (p);
5028 /* Call here when we see the initializer is surrounded by braces.
5029 This is instead of a call to push_init_level;
5030 it is matched by a call to pop_init_level.
5032 TYPE is the type to initialize, for a constructor expression.
5033 For an initializer for a decl, TYPE is zero. */
5035 void
5036 really_start_incremental_init (tree type)
5038 struct constructor_stack *p = XNEW (struct constructor_stack);
5040 if (type == 0)
5041 type = TREE_TYPE (constructor_decl);
5043 if (targetm.vector_opaque_p (type))
5044 error ("opaque vector types cannot be initialized");
5046 p->type = constructor_type;
5047 p->fields = constructor_fields;
5048 p->index = constructor_index;
5049 p->max_index = constructor_max_index;
5050 p->unfilled_index = constructor_unfilled_index;
5051 p->unfilled_fields = constructor_unfilled_fields;
5052 p->bit_index = constructor_bit_index;
5053 p->elements = constructor_elements;
5054 p->constant = constructor_constant;
5055 p->simple = constructor_simple;
5056 p->erroneous = constructor_erroneous;
5057 p->pending_elts = constructor_pending_elts;
5058 p->depth = constructor_depth;
5059 p->replacement_value.value = 0;
5060 p->replacement_value.original_code = ERROR_MARK;
5061 p->implicit = 0;
5062 p->range_stack = 0;
5063 p->outer = 0;
5064 p->incremental = constructor_incremental;
5065 p->designated = constructor_designated;
5066 p->next = 0;
5067 constructor_stack = p;
5069 constructor_constant = 1;
5070 constructor_simple = 1;
5071 constructor_depth = SPELLING_DEPTH ();
5072 constructor_elements = 0;
5073 constructor_pending_elts = 0;
5074 constructor_type = type;
5075 constructor_incremental = 1;
5076 constructor_designated = 0;
5077 designator_depth = 0;
5078 designator_erroneous = 0;
5080 if (TREE_CODE (constructor_type) == RECORD_TYPE
5081 || TREE_CODE (constructor_type) == UNION_TYPE)
5083 constructor_fields = TYPE_FIELDS (constructor_type);
5084 /* Skip any nameless bit fields at the beginning. */
5085 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5086 && DECL_NAME (constructor_fields) == 0)
5087 constructor_fields = TREE_CHAIN (constructor_fields);
5089 constructor_unfilled_fields = constructor_fields;
5090 constructor_bit_index = bitsize_zero_node;
5092 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5094 if (TYPE_DOMAIN (constructor_type))
5096 constructor_max_index
5097 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5099 /* Detect non-empty initializations of zero-length arrays. */
5100 if (constructor_max_index == NULL_TREE
5101 && TYPE_SIZE (constructor_type))
5102 constructor_max_index = build_int_cst (NULL_TREE, -1);
5104 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5105 to initialize VLAs will cause a proper error; avoid tree
5106 checking errors as well by setting a safe value. */
5107 if (constructor_max_index
5108 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5109 constructor_max_index = build_int_cst (NULL_TREE, -1);
5111 constructor_index
5112 = convert (bitsizetype,
5113 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5115 else
5117 constructor_index = bitsize_zero_node;
5118 constructor_max_index = NULL_TREE;
5121 constructor_unfilled_index = constructor_index;
5123 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5125 /* Vectors are like simple fixed-size arrays. */
5126 constructor_max_index =
5127 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5128 constructor_index = bitsize_zero_node;
5129 constructor_unfilled_index = constructor_index;
5131 else
5133 /* Handle the case of int x = {5}; */
5134 constructor_fields = constructor_type;
5135 constructor_unfilled_fields = constructor_type;
5139 /* Push down into a subobject, for initialization.
5140 If this is for an explicit set of braces, IMPLICIT is 0.
5141 If it is because the next element belongs at a lower level,
5142 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5144 void
5145 push_init_level (int implicit)
5147 struct constructor_stack *p;
5148 tree value = NULL_TREE;
5150 /* If we've exhausted any levels that didn't have braces,
5151 pop them now. If implicit == 1, this will have been done in
5152 process_init_element; do not repeat it here because in the case
5153 of excess initializers for an empty aggregate this leads to an
5154 infinite cycle of popping a level and immediately recreating
5155 it. */
5156 if (implicit != 1)
5158 while (constructor_stack->implicit)
5160 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5161 || TREE_CODE (constructor_type) == UNION_TYPE)
5162 && constructor_fields == 0)
5163 process_init_element (pop_init_level (1));
5164 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5165 && constructor_max_index
5166 && tree_int_cst_lt (constructor_max_index,
5167 constructor_index))
5168 process_init_element (pop_init_level (1));
5169 else
5170 break;
5174 /* Unless this is an explicit brace, we need to preserve previous
5175 content if any. */
5176 if (implicit)
5178 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5179 || TREE_CODE (constructor_type) == UNION_TYPE)
5180 && constructor_fields)
5181 value = find_init_member (constructor_fields);
5182 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5183 value = find_init_member (constructor_index);
5186 p = XNEW (struct constructor_stack);
5187 p->type = constructor_type;
5188 p->fields = constructor_fields;
5189 p->index = constructor_index;
5190 p->max_index = constructor_max_index;
5191 p->unfilled_index = constructor_unfilled_index;
5192 p->unfilled_fields = constructor_unfilled_fields;
5193 p->bit_index = constructor_bit_index;
5194 p->elements = constructor_elements;
5195 p->constant = constructor_constant;
5196 p->simple = constructor_simple;
5197 p->erroneous = constructor_erroneous;
5198 p->pending_elts = constructor_pending_elts;
5199 p->depth = constructor_depth;
5200 p->replacement_value.value = 0;
5201 p->replacement_value.original_code = ERROR_MARK;
5202 p->implicit = implicit;
5203 p->outer = 0;
5204 p->incremental = constructor_incremental;
5205 p->designated = constructor_designated;
5206 p->next = constructor_stack;
5207 p->range_stack = 0;
5208 constructor_stack = p;
5210 constructor_constant = 1;
5211 constructor_simple = 1;
5212 constructor_depth = SPELLING_DEPTH ();
5213 constructor_elements = 0;
5214 constructor_incremental = 1;
5215 constructor_designated = 0;
5216 constructor_pending_elts = 0;
5217 if (!implicit)
5219 p->range_stack = constructor_range_stack;
5220 constructor_range_stack = 0;
5221 designator_depth = 0;
5222 designator_erroneous = 0;
5225 /* Don't die if an entire brace-pair level is superfluous
5226 in the containing level. */
5227 if (constructor_type == 0)
5229 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5230 || TREE_CODE (constructor_type) == UNION_TYPE)
5232 /* Don't die if there are extra init elts at the end. */
5233 if (constructor_fields == 0)
5234 constructor_type = 0;
5235 else
5237 constructor_type = TREE_TYPE (constructor_fields);
5238 push_member_name (constructor_fields);
5239 constructor_depth++;
5242 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5244 constructor_type = TREE_TYPE (constructor_type);
5245 push_array_bounds (tree_low_cst (constructor_index, 1));
5246 constructor_depth++;
5249 if (constructor_type == 0)
5251 error_init ("extra brace group at end of initializer");
5252 constructor_fields = 0;
5253 constructor_unfilled_fields = 0;
5254 return;
5257 if (value && TREE_CODE (value) == CONSTRUCTOR)
5259 constructor_constant = TREE_CONSTANT (value);
5260 constructor_simple = TREE_STATIC (value);
5261 constructor_elements = CONSTRUCTOR_ELTS (value);
5262 if (!VEC_empty (constructor_elt, constructor_elements)
5263 && (TREE_CODE (constructor_type) == RECORD_TYPE
5264 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5265 set_nonincremental_init ();
5268 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5270 missing_braces_mentioned = 1;
5271 warning_init ("missing braces around initializer");
5274 if (TREE_CODE (constructor_type) == RECORD_TYPE
5275 || TREE_CODE (constructor_type) == UNION_TYPE)
5277 constructor_fields = TYPE_FIELDS (constructor_type);
5278 /* Skip any nameless bit fields at the beginning. */
5279 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5280 && DECL_NAME (constructor_fields) == 0)
5281 constructor_fields = TREE_CHAIN (constructor_fields);
5283 constructor_unfilled_fields = constructor_fields;
5284 constructor_bit_index = bitsize_zero_node;
5286 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5288 /* Vectors are like simple fixed-size arrays. */
5289 constructor_max_index =
5290 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5291 constructor_index = convert (bitsizetype, integer_zero_node);
5292 constructor_unfilled_index = constructor_index;
5294 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5296 if (TYPE_DOMAIN (constructor_type))
5298 constructor_max_index
5299 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5301 /* Detect non-empty initializations of zero-length arrays. */
5302 if (constructor_max_index == NULL_TREE
5303 && TYPE_SIZE (constructor_type))
5304 constructor_max_index = build_int_cst (NULL_TREE, -1);
5306 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5307 to initialize VLAs will cause a proper error; avoid tree
5308 checking errors as well by setting a safe value. */
5309 if (constructor_max_index
5310 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5311 constructor_max_index = build_int_cst (NULL_TREE, -1);
5313 constructor_index
5314 = convert (bitsizetype,
5315 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5317 else
5318 constructor_index = bitsize_zero_node;
5320 constructor_unfilled_index = constructor_index;
5321 if (value && TREE_CODE (value) == STRING_CST)
5323 /* We need to split the char/wchar array into individual
5324 characters, so that we don't have to special case it
5325 everywhere. */
5326 set_nonincremental_init_from_string (value);
5329 else
5331 if (constructor_type != error_mark_node)
5332 warning_init ("braces around scalar initializer");
5333 constructor_fields = constructor_type;
5334 constructor_unfilled_fields = constructor_type;
5338 /* At the end of an implicit or explicit brace level,
5339 finish up that level of constructor. If a single expression
5340 with redundant braces initialized that level, return the
5341 c_expr structure for that expression. Otherwise, the original_code
5342 element is set to ERROR_MARK.
5343 If we were outputting the elements as they are read, return 0 as the value
5344 from inner levels (process_init_element ignores that),
5345 but return error_mark_node as the value from the outermost level
5346 (that's what we want to put in DECL_INITIAL).
5347 Otherwise, return a CONSTRUCTOR expression as the value. */
5349 struct c_expr
5350 pop_init_level (int implicit)
5352 struct constructor_stack *p;
5353 struct c_expr ret;
5354 ret.value = 0;
5355 ret.original_code = ERROR_MARK;
5357 if (implicit == 0)
5359 /* When we come to an explicit close brace,
5360 pop any inner levels that didn't have explicit braces. */
5361 while (constructor_stack->implicit)
5362 process_init_element (pop_init_level (1));
5364 gcc_assert (!constructor_range_stack);
5367 /* Now output all pending elements. */
5368 constructor_incremental = 1;
5369 output_pending_init_elements (1);
5371 p = constructor_stack;
5373 /* Error for initializing a flexible array member, or a zero-length
5374 array member in an inappropriate context. */
5375 if (constructor_type && constructor_fields
5376 && TREE_CODE (constructor_type) == ARRAY_TYPE
5377 && TYPE_DOMAIN (constructor_type)
5378 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5380 /* Silently discard empty initializations. The parser will
5381 already have pedwarned for empty brackets. */
5382 if (integer_zerop (constructor_unfilled_index))
5383 constructor_type = NULL_TREE;
5384 else
5386 gcc_assert (!TYPE_SIZE (constructor_type));
5388 if (constructor_depth > 2)
5389 error_init ("initialization of flexible array member in a nested context");
5390 else if (pedantic)
5391 pedwarn_init ("initialization of a flexible array member");
5393 /* We have already issued an error message for the existence
5394 of a flexible array member not at the end of the structure.
5395 Discard the initializer so that we do not die later. */
5396 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5397 constructor_type = NULL_TREE;
5401 /* Warn when some struct elements are implicitly initialized to zero. */
5402 if (warn_missing_field_initializers
5403 && constructor_type
5404 && TREE_CODE (constructor_type) == RECORD_TYPE
5405 && constructor_unfilled_fields)
5407 /* Do not warn for flexible array members or zero-length arrays. */
5408 while (constructor_unfilled_fields
5409 && (!DECL_SIZE (constructor_unfilled_fields)
5410 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5411 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5413 /* Do not warn if this level of the initializer uses member
5414 designators; it is likely to be deliberate. */
5415 if (constructor_unfilled_fields && !constructor_designated)
5417 push_member_name (constructor_unfilled_fields);
5418 warning_init ("missing initializer");
5419 RESTORE_SPELLING_DEPTH (constructor_depth);
5423 /* Pad out the end of the structure. */
5424 if (p->replacement_value.value)
5425 /* If this closes a superfluous brace pair,
5426 just pass out the element between them. */
5427 ret = p->replacement_value;
5428 else if (constructor_type == 0)
5430 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5431 && TREE_CODE (constructor_type) != UNION_TYPE
5432 && TREE_CODE (constructor_type) != ARRAY_TYPE
5433 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5435 /* A nonincremental scalar initializer--just return
5436 the element, after verifying there is just one. */
5437 if (VEC_empty (constructor_elt,constructor_elements))
5439 if (!constructor_erroneous)
5440 error_init ("empty scalar initializer");
5441 ret.value = error_mark_node;
5443 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5445 error_init ("extra elements in scalar initializer");
5446 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5448 else
5449 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5451 else
5453 if (constructor_erroneous)
5454 ret.value = error_mark_node;
5455 else
5457 ret.value = build_constructor (constructor_type,
5458 constructor_elements);
5459 if (constructor_constant)
5460 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5461 if (constructor_constant && constructor_simple)
5462 TREE_STATIC (ret.value) = 1;
5466 constructor_type = p->type;
5467 constructor_fields = p->fields;
5468 constructor_index = p->index;
5469 constructor_max_index = p->max_index;
5470 constructor_unfilled_index = p->unfilled_index;
5471 constructor_unfilled_fields = p->unfilled_fields;
5472 constructor_bit_index = p->bit_index;
5473 constructor_elements = p->elements;
5474 constructor_constant = p->constant;
5475 constructor_simple = p->simple;
5476 constructor_erroneous = p->erroneous;
5477 constructor_incremental = p->incremental;
5478 constructor_designated = p->designated;
5479 constructor_pending_elts = p->pending_elts;
5480 constructor_depth = p->depth;
5481 if (!p->implicit)
5482 constructor_range_stack = p->range_stack;
5483 RESTORE_SPELLING_DEPTH (constructor_depth);
5485 constructor_stack = p->next;
5486 free (p);
5488 if (ret.value == 0 && constructor_stack == 0)
5489 ret.value = error_mark_node;
5490 return ret;
5493 /* Common handling for both array range and field name designators.
5494 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5496 static int
5497 set_designator (int array)
5499 tree subtype;
5500 enum tree_code subcode;
5502 /* Don't die if an entire brace-pair level is superfluous
5503 in the containing level. */
5504 if (constructor_type == 0)
5505 return 1;
5507 /* If there were errors in this designator list already, bail out
5508 silently. */
5509 if (designator_erroneous)
5510 return 1;
5512 if (!designator_depth)
5514 gcc_assert (!constructor_range_stack);
5516 /* Designator list starts at the level of closest explicit
5517 braces. */
5518 while (constructor_stack->implicit)
5519 process_init_element (pop_init_level (1));
5520 constructor_designated = 1;
5521 return 0;
5524 switch (TREE_CODE (constructor_type))
5526 case RECORD_TYPE:
5527 case UNION_TYPE:
5528 subtype = TREE_TYPE (constructor_fields);
5529 if (subtype != error_mark_node)
5530 subtype = TYPE_MAIN_VARIANT (subtype);
5531 break;
5532 case ARRAY_TYPE:
5533 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5534 break;
5535 default:
5536 gcc_unreachable ();
5539 subcode = TREE_CODE (subtype);
5540 if (array && subcode != ARRAY_TYPE)
5542 error_init ("array index in non-array initializer");
5543 return 1;
5545 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5547 error_init ("field name not in record or union initializer");
5548 return 1;
5551 constructor_designated = 1;
5552 push_init_level (2);
5553 return 0;
5556 /* If there are range designators in designator list, push a new designator
5557 to constructor_range_stack. RANGE_END is end of such stack range or
5558 NULL_TREE if there is no range designator at this level. */
5560 static void
5561 push_range_stack (tree range_end)
5563 struct constructor_range_stack *p;
5565 p = GGC_NEW (struct constructor_range_stack);
5566 p->prev = constructor_range_stack;
5567 p->next = 0;
5568 p->fields = constructor_fields;
5569 p->range_start = constructor_index;
5570 p->index = constructor_index;
5571 p->stack = constructor_stack;
5572 p->range_end = range_end;
5573 if (constructor_range_stack)
5574 constructor_range_stack->next = p;
5575 constructor_range_stack = p;
5578 /* Within an array initializer, specify the next index to be initialized.
5579 FIRST is that index. If LAST is nonzero, then initialize a range
5580 of indices, running from FIRST through LAST. */
5582 void
5583 set_init_index (tree first, tree last)
5585 if (set_designator (1))
5586 return;
5588 designator_erroneous = 1;
5590 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5591 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5593 error_init ("array index in initializer not of integer type");
5594 return;
5597 if (TREE_CODE (first) != INTEGER_CST)
5598 error_init ("nonconstant array index in initializer");
5599 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5600 error_init ("nonconstant array index in initializer");
5601 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5602 error_init ("array index in non-array initializer");
5603 else if (tree_int_cst_sgn (first) == -1)
5604 error_init ("array index in initializer exceeds array bounds");
5605 else if (constructor_max_index
5606 && tree_int_cst_lt (constructor_max_index, first))
5607 error_init ("array index in initializer exceeds array bounds");
5608 else
5610 constructor_index = convert (bitsizetype, first);
5612 if (last)
5614 if (tree_int_cst_equal (first, last))
5615 last = 0;
5616 else if (tree_int_cst_lt (last, first))
5618 error_init ("empty index range in initializer");
5619 last = 0;
5621 else
5623 last = convert (bitsizetype, last);
5624 if (constructor_max_index != 0
5625 && tree_int_cst_lt (constructor_max_index, last))
5627 error_init ("array index range in initializer exceeds array bounds");
5628 last = 0;
5633 designator_depth++;
5634 designator_erroneous = 0;
5635 if (constructor_range_stack || last)
5636 push_range_stack (last);
5640 /* Within a struct initializer, specify the next field to be initialized. */
5642 void
5643 set_init_label (tree fieldname)
5645 tree tail;
5647 if (set_designator (0))
5648 return;
5650 designator_erroneous = 1;
5652 if (TREE_CODE (constructor_type) != RECORD_TYPE
5653 && TREE_CODE (constructor_type) != UNION_TYPE)
5655 error_init ("field name not in record or union initializer");
5656 return;
5659 for (tail = TYPE_FIELDS (constructor_type); tail;
5660 tail = TREE_CHAIN (tail))
5662 if (DECL_NAME (tail) == fieldname)
5663 break;
5666 if (tail == 0)
5667 error ("unknown field %qE specified in initializer", fieldname);
5668 else
5670 constructor_fields = tail;
5671 designator_depth++;
5672 designator_erroneous = 0;
5673 if (constructor_range_stack)
5674 push_range_stack (NULL_TREE);
5678 /* Add a new initializer to the tree of pending initializers. PURPOSE
5679 identifies the initializer, either array index or field in a structure.
5680 VALUE is the value of that index or field. */
5682 static void
5683 add_pending_init (tree purpose, tree value)
5685 struct init_node *p, **q, *r;
5687 q = &constructor_pending_elts;
5688 p = 0;
5690 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5692 while (*q != 0)
5694 p = *q;
5695 if (tree_int_cst_lt (purpose, p->purpose))
5696 q = &p->left;
5697 else if (tree_int_cst_lt (p->purpose, purpose))
5698 q = &p->right;
5699 else
5701 if (TREE_SIDE_EFFECTS (p->value))
5702 warning_init ("initialized field with side-effects overwritten");
5703 p->value = value;
5704 return;
5708 else
5710 tree bitpos;
5712 bitpos = bit_position (purpose);
5713 while (*q != NULL)
5715 p = *q;
5716 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5717 q = &p->left;
5718 else if (p->purpose != purpose)
5719 q = &p->right;
5720 else
5722 if (TREE_SIDE_EFFECTS (p->value))
5723 warning_init ("initialized field with side-effects overwritten");
5724 p->value = value;
5725 return;
5730 r = GGC_NEW (struct init_node);
5731 r->purpose = purpose;
5732 r->value = value;
5734 *q = r;
5735 r->parent = p;
5736 r->left = 0;
5737 r->right = 0;
5738 r->balance = 0;
5740 while (p)
5742 struct init_node *s;
5744 if (r == p->left)
5746 if (p->balance == 0)
5747 p->balance = -1;
5748 else if (p->balance < 0)
5750 if (r->balance < 0)
5752 /* L rotation. */
5753 p->left = r->right;
5754 if (p->left)
5755 p->left->parent = p;
5756 r->right = p;
5758 p->balance = 0;
5759 r->balance = 0;
5761 s = p->parent;
5762 p->parent = r;
5763 r->parent = s;
5764 if (s)
5766 if (s->left == p)
5767 s->left = r;
5768 else
5769 s->right = r;
5771 else
5772 constructor_pending_elts = r;
5774 else
5776 /* LR rotation. */
5777 struct init_node *t = r->right;
5779 r->right = t->left;
5780 if (r->right)
5781 r->right->parent = r;
5782 t->left = r;
5784 p->left = t->right;
5785 if (p->left)
5786 p->left->parent = p;
5787 t->right = p;
5789 p->balance = t->balance < 0;
5790 r->balance = -(t->balance > 0);
5791 t->balance = 0;
5793 s = p->parent;
5794 p->parent = t;
5795 r->parent = t;
5796 t->parent = s;
5797 if (s)
5799 if (s->left == p)
5800 s->left = t;
5801 else
5802 s->right = t;
5804 else
5805 constructor_pending_elts = t;
5807 break;
5809 else
5811 /* p->balance == +1; growth of left side balances the node. */
5812 p->balance = 0;
5813 break;
5816 else /* r == p->right */
5818 if (p->balance == 0)
5819 /* Growth propagation from right side. */
5820 p->balance++;
5821 else if (p->balance > 0)
5823 if (r->balance > 0)
5825 /* R rotation. */
5826 p->right = r->left;
5827 if (p->right)
5828 p->right->parent = p;
5829 r->left = p;
5831 p->balance = 0;
5832 r->balance = 0;
5834 s = p->parent;
5835 p->parent = r;
5836 r->parent = s;
5837 if (s)
5839 if (s->left == p)
5840 s->left = r;
5841 else
5842 s->right = r;
5844 else
5845 constructor_pending_elts = r;
5847 else /* r->balance == -1 */
5849 /* RL rotation */
5850 struct init_node *t = r->left;
5852 r->left = t->right;
5853 if (r->left)
5854 r->left->parent = r;
5855 t->right = r;
5857 p->right = t->left;
5858 if (p->right)
5859 p->right->parent = p;
5860 t->left = p;
5862 r->balance = (t->balance < 0);
5863 p->balance = -(t->balance > 0);
5864 t->balance = 0;
5866 s = p->parent;
5867 p->parent = t;
5868 r->parent = t;
5869 t->parent = s;
5870 if (s)
5872 if (s->left == p)
5873 s->left = t;
5874 else
5875 s->right = t;
5877 else
5878 constructor_pending_elts = t;
5880 break;
5882 else
5884 /* p->balance == -1; growth of right side balances the node. */
5885 p->balance = 0;
5886 break;
5890 r = p;
5891 p = p->parent;
5895 /* Build AVL tree from a sorted chain. */
5897 static void
5898 set_nonincremental_init (void)
5900 unsigned HOST_WIDE_INT ix;
5901 tree index, value;
5903 if (TREE_CODE (constructor_type) != RECORD_TYPE
5904 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5905 return;
5907 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5908 add_pending_init (index, value);
5909 constructor_elements = 0;
5910 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5912 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5913 /* Skip any nameless bit fields at the beginning. */
5914 while (constructor_unfilled_fields != 0
5915 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5916 && DECL_NAME (constructor_unfilled_fields) == 0)
5917 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5920 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5922 if (TYPE_DOMAIN (constructor_type))
5923 constructor_unfilled_index
5924 = convert (bitsizetype,
5925 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5926 else
5927 constructor_unfilled_index = bitsize_zero_node;
5929 constructor_incremental = 0;
5932 /* Build AVL tree from a string constant. */
5934 static void
5935 set_nonincremental_init_from_string (tree str)
5937 tree value, purpose, type;
5938 HOST_WIDE_INT val[2];
5939 const char *p, *end;
5940 int byte, wchar_bytes, charwidth, bitpos;
5942 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5944 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5945 == TYPE_PRECISION (char_type_node))
5946 wchar_bytes = 1;
5947 else
5949 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5950 == TYPE_PRECISION (wchar_type_node));
5951 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5953 charwidth = TYPE_PRECISION (char_type_node);
5954 type = TREE_TYPE (constructor_type);
5955 p = TREE_STRING_POINTER (str);
5956 end = p + TREE_STRING_LENGTH (str);
5958 for (purpose = bitsize_zero_node;
5959 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5960 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5962 if (wchar_bytes == 1)
5964 val[1] = (unsigned char) *p++;
5965 val[0] = 0;
5967 else
5969 val[0] = 0;
5970 val[1] = 0;
5971 for (byte = 0; byte < wchar_bytes; byte++)
5973 if (BYTES_BIG_ENDIAN)
5974 bitpos = (wchar_bytes - byte - 1) * charwidth;
5975 else
5976 bitpos = byte * charwidth;
5977 val[bitpos < HOST_BITS_PER_WIDE_INT]
5978 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5979 << (bitpos % HOST_BITS_PER_WIDE_INT);
5983 if (!TYPE_UNSIGNED (type))
5985 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5986 if (bitpos < HOST_BITS_PER_WIDE_INT)
5988 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5990 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5991 val[0] = -1;
5994 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5996 if (val[1] < 0)
5997 val[0] = -1;
5999 else if (val[0] & (((HOST_WIDE_INT) 1)
6000 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6001 val[0] |= ((HOST_WIDE_INT) -1)
6002 << (bitpos - HOST_BITS_PER_WIDE_INT);
6005 value = build_int_cst_wide (type, val[1], val[0]);
6006 add_pending_init (purpose, value);
6009 constructor_incremental = 0;
6012 /* Return value of FIELD in pending initializer or zero if the field was
6013 not initialized yet. */
6015 static tree
6016 find_init_member (tree field)
6018 struct init_node *p;
6020 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6022 if (constructor_incremental
6023 && tree_int_cst_lt (field, constructor_unfilled_index))
6024 set_nonincremental_init ();
6026 p = constructor_pending_elts;
6027 while (p)
6029 if (tree_int_cst_lt (field, p->purpose))
6030 p = p->left;
6031 else if (tree_int_cst_lt (p->purpose, field))
6032 p = p->right;
6033 else
6034 return p->value;
6037 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6039 tree bitpos = bit_position (field);
6041 if (constructor_incremental
6042 && (!constructor_unfilled_fields
6043 || tree_int_cst_lt (bitpos,
6044 bit_position (constructor_unfilled_fields))))
6045 set_nonincremental_init ();
6047 p = constructor_pending_elts;
6048 while (p)
6050 if (field == p->purpose)
6051 return p->value;
6052 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6053 p = p->left;
6054 else
6055 p = p->right;
6058 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6060 if (!VEC_empty (constructor_elt, constructor_elements)
6061 && (VEC_last (constructor_elt, constructor_elements)->index
6062 == field))
6063 return VEC_last (constructor_elt, constructor_elements)->value;
6065 return 0;
6068 /* "Output" the next constructor element.
6069 At top level, really output it to assembler code now.
6070 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6071 TYPE is the data type that the containing data type wants here.
6072 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6073 If VALUE is a string constant, STRICT_STRING is true if it is
6074 unparenthesized or we should not warn here for it being parenthesized.
6075 For other types of VALUE, STRICT_STRING is not used.
6077 PENDING if non-nil means output pending elements that belong
6078 right after this element. (PENDING is normally 1;
6079 it is 0 while outputting pending elements, to avoid recursion.) */
6081 static void
6082 output_init_element (tree value, bool strict_string, tree type, tree field,
6083 int pending)
6085 constructor_elt *celt;
6087 if (type == error_mark_node || value == error_mark_node)
6089 constructor_erroneous = 1;
6090 return;
6092 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6093 && (TREE_CODE (value) == STRING_CST
6094 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6095 && !(TREE_CODE (value) == STRING_CST
6096 && TREE_CODE (type) == ARRAY_TYPE
6097 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6098 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6099 TYPE_MAIN_VARIANT (type)))
6100 value = array_to_pointer_conversion (value);
6102 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6103 && require_constant_value && !flag_isoc99 && pending)
6105 /* As an extension, allow initializing objects with static storage
6106 duration with compound literals (which are then treated just as
6107 the brace enclosed list they contain). */
6108 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6109 value = DECL_INITIAL (decl);
6112 if (value == error_mark_node)
6113 constructor_erroneous = 1;
6114 else if (!TREE_CONSTANT (value))
6115 constructor_constant = 0;
6116 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6117 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6118 || TREE_CODE (constructor_type) == UNION_TYPE)
6119 && DECL_C_BIT_FIELD (field)
6120 && TREE_CODE (value) != INTEGER_CST))
6121 constructor_simple = 0;
6123 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6125 if (require_constant_value)
6127 error_init ("initializer element is not constant");
6128 value = error_mark_node;
6130 else if (require_constant_elements)
6131 pedwarn ("initializer element is not computable at load time");
6134 /* If this field is empty (and not at the end of structure),
6135 don't do anything other than checking the initializer. */
6136 if (field
6137 && (TREE_TYPE (field) == error_mark_node
6138 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6139 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6140 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6141 || TREE_CHAIN (field)))))
6142 return;
6144 value = digest_init (type, value, strict_string, require_constant_value);
6145 if (value == error_mark_node)
6147 constructor_erroneous = 1;
6148 return;
6151 /* If this element doesn't come next in sequence,
6152 put it on constructor_pending_elts. */
6153 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6154 && (!constructor_incremental
6155 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6157 if (constructor_incremental
6158 && tree_int_cst_lt (field, constructor_unfilled_index))
6159 set_nonincremental_init ();
6161 add_pending_init (field, value);
6162 return;
6164 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6165 && (!constructor_incremental
6166 || field != constructor_unfilled_fields))
6168 /* We do this for records but not for unions. In a union,
6169 no matter which field is specified, it can be initialized
6170 right away since it starts at the beginning of the union. */
6171 if (constructor_incremental)
6173 if (!constructor_unfilled_fields)
6174 set_nonincremental_init ();
6175 else
6177 tree bitpos, unfillpos;
6179 bitpos = bit_position (field);
6180 unfillpos = bit_position (constructor_unfilled_fields);
6182 if (tree_int_cst_lt (bitpos, unfillpos))
6183 set_nonincremental_init ();
6187 add_pending_init (field, value);
6188 return;
6190 else if (TREE_CODE (constructor_type) == UNION_TYPE
6191 && !VEC_empty (constructor_elt, constructor_elements))
6193 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6194 constructor_elements)->value))
6195 warning_init ("initialized field with side-effects overwritten");
6197 /* We can have just one union field set. */
6198 constructor_elements = 0;
6201 /* Otherwise, output this element either to
6202 constructor_elements or to the assembler file. */
6204 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6205 celt->index = field;
6206 celt->value = value;
6208 /* Advance the variable that indicates sequential elements output. */
6209 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6210 constructor_unfilled_index
6211 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6212 bitsize_one_node);
6213 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6215 constructor_unfilled_fields
6216 = TREE_CHAIN (constructor_unfilled_fields);
6218 /* Skip any nameless bit fields. */
6219 while (constructor_unfilled_fields != 0
6220 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6221 && DECL_NAME (constructor_unfilled_fields) == 0)
6222 constructor_unfilled_fields =
6223 TREE_CHAIN (constructor_unfilled_fields);
6225 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6226 constructor_unfilled_fields = 0;
6228 /* Now output any pending elements which have become next. */
6229 if (pending)
6230 output_pending_init_elements (0);
6233 /* Output any pending elements which have become next.
6234 As we output elements, constructor_unfilled_{fields,index}
6235 advances, which may cause other elements to become next;
6236 if so, they too are output.
6238 If ALL is 0, we return when there are
6239 no more pending elements to output now.
6241 If ALL is 1, we output space as necessary so that
6242 we can output all the pending elements. */
6244 static void
6245 output_pending_init_elements (int all)
6247 struct init_node *elt = constructor_pending_elts;
6248 tree next;
6250 retry:
6252 /* Look through the whole pending tree.
6253 If we find an element that should be output now,
6254 output it. Otherwise, set NEXT to the element
6255 that comes first among those still pending. */
6257 next = 0;
6258 while (elt)
6260 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6262 if (tree_int_cst_equal (elt->purpose,
6263 constructor_unfilled_index))
6264 output_init_element (elt->value, true,
6265 TREE_TYPE (constructor_type),
6266 constructor_unfilled_index, 0);
6267 else if (tree_int_cst_lt (constructor_unfilled_index,
6268 elt->purpose))
6270 /* Advance to the next smaller node. */
6271 if (elt->left)
6272 elt = elt->left;
6273 else
6275 /* We have reached the smallest node bigger than the
6276 current unfilled index. Fill the space first. */
6277 next = elt->purpose;
6278 break;
6281 else
6283 /* Advance to the next bigger node. */
6284 if (elt->right)
6285 elt = elt->right;
6286 else
6288 /* We have reached the biggest node in a subtree. Find
6289 the parent of it, which is the next bigger node. */
6290 while (elt->parent && elt->parent->right == elt)
6291 elt = elt->parent;
6292 elt = elt->parent;
6293 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6294 elt->purpose))
6296 next = elt->purpose;
6297 break;
6302 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6303 || TREE_CODE (constructor_type) == UNION_TYPE)
6305 tree ctor_unfilled_bitpos, elt_bitpos;
6307 /* If the current record is complete we are done. */
6308 if (constructor_unfilled_fields == 0)
6309 break;
6311 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6312 elt_bitpos = bit_position (elt->purpose);
6313 /* We can't compare fields here because there might be empty
6314 fields in between. */
6315 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6317 constructor_unfilled_fields = elt->purpose;
6318 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6319 elt->purpose, 0);
6321 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6323 /* Advance to the next smaller node. */
6324 if (elt->left)
6325 elt = elt->left;
6326 else
6328 /* We have reached the smallest node bigger than the
6329 current unfilled field. Fill the space first. */
6330 next = elt->purpose;
6331 break;
6334 else
6336 /* Advance to the next bigger node. */
6337 if (elt->right)
6338 elt = elt->right;
6339 else
6341 /* We have reached the biggest node in a subtree. Find
6342 the parent of it, which is the next bigger node. */
6343 while (elt->parent && elt->parent->right == elt)
6344 elt = elt->parent;
6345 elt = elt->parent;
6346 if (elt
6347 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6348 bit_position (elt->purpose))))
6350 next = elt->purpose;
6351 break;
6358 /* Ordinarily return, but not if we want to output all
6359 and there are elements left. */
6360 if (!(all && next != 0))
6361 return;
6363 /* If it's not incremental, just skip over the gap, so that after
6364 jumping to retry we will output the next successive element. */
6365 if (TREE_CODE (constructor_type) == RECORD_TYPE
6366 || TREE_CODE (constructor_type) == UNION_TYPE)
6367 constructor_unfilled_fields = next;
6368 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6369 constructor_unfilled_index = next;
6371 /* ELT now points to the node in the pending tree with the next
6372 initializer to output. */
6373 goto retry;
6376 /* Add one non-braced element to the current constructor level.
6377 This adjusts the current position within the constructor's type.
6378 This may also start or terminate implicit levels
6379 to handle a partly-braced initializer.
6381 Once this has found the correct level for the new element,
6382 it calls output_init_element. */
6384 void
6385 process_init_element (struct c_expr value)
6387 tree orig_value = value.value;
6388 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6389 bool strict_string = value.original_code == STRING_CST;
6391 designator_depth = 0;
6392 designator_erroneous = 0;
6394 /* Handle superfluous braces around string cst as in
6395 char x[] = {"foo"}; */
6396 if (string_flag
6397 && constructor_type
6398 && TREE_CODE (constructor_type) == ARRAY_TYPE
6399 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6400 && integer_zerop (constructor_unfilled_index))
6402 if (constructor_stack->replacement_value.value)
6403 error_init ("excess elements in char array initializer");
6404 constructor_stack->replacement_value = value;
6405 return;
6408 if (constructor_stack->replacement_value.value != 0)
6410 error_init ("excess elements in struct initializer");
6411 return;
6414 /* Ignore elements of a brace group if it is entirely superfluous
6415 and has already been diagnosed. */
6416 if (constructor_type == 0)
6417 return;
6419 /* If we've exhausted any levels that didn't have braces,
6420 pop them now. */
6421 while (constructor_stack->implicit)
6423 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6424 || TREE_CODE (constructor_type) == UNION_TYPE)
6425 && constructor_fields == 0)
6426 process_init_element (pop_init_level (1));
6427 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6428 && (constructor_max_index == 0
6429 || tree_int_cst_lt (constructor_max_index,
6430 constructor_index)))
6431 process_init_element (pop_init_level (1));
6432 else
6433 break;
6436 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6437 if (constructor_range_stack)
6439 /* If value is a compound literal and we'll be just using its
6440 content, don't put it into a SAVE_EXPR. */
6441 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6442 || !require_constant_value
6443 || flag_isoc99)
6444 value.value = save_expr (value.value);
6447 while (1)
6449 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6451 tree fieldtype;
6452 enum tree_code fieldcode;
6454 if (constructor_fields == 0)
6456 pedwarn_init ("excess elements in struct initializer");
6457 break;
6460 fieldtype = TREE_TYPE (constructor_fields);
6461 if (fieldtype != error_mark_node)
6462 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6463 fieldcode = TREE_CODE (fieldtype);
6465 /* Error for non-static initialization of a flexible array member. */
6466 if (fieldcode == ARRAY_TYPE
6467 && !require_constant_value
6468 && TYPE_SIZE (fieldtype) == NULL_TREE
6469 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6471 error_init ("non-static initialization of a flexible array member");
6472 break;
6475 /* Accept a string constant to initialize a subarray. */
6476 if (value.value != 0
6477 && fieldcode == ARRAY_TYPE
6478 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6479 && string_flag)
6480 value.value = orig_value;
6481 /* Otherwise, if we have come to a subaggregate,
6482 and we don't have an element of its type, push into it. */
6483 else if (value.value != 0
6484 && value.value != error_mark_node
6485 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6486 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6487 || fieldcode == UNION_TYPE))
6489 push_init_level (1);
6490 continue;
6493 if (value.value)
6495 push_member_name (constructor_fields);
6496 output_init_element (value.value, strict_string,
6497 fieldtype, constructor_fields, 1);
6498 RESTORE_SPELLING_DEPTH (constructor_depth);
6500 else
6501 /* Do the bookkeeping for an element that was
6502 directly output as a constructor. */
6504 /* For a record, keep track of end position of last field. */
6505 if (DECL_SIZE (constructor_fields))
6506 constructor_bit_index
6507 = size_binop (PLUS_EXPR,
6508 bit_position (constructor_fields),
6509 DECL_SIZE (constructor_fields));
6511 /* If the current field was the first one not yet written out,
6512 it isn't now, so update. */
6513 if (constructor_unfilled_fields == constructor_fields)
6515 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6516 /* Skip any nameless bit fields. */
6517 while (constructor_unfilled_fields != 0
6518 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6519 && DECL_NAME (constructor_unfilled_fields) == 0)
6520 constructor_unfilled_fields =
6521 TREE_CHAIN (constructor_unfilled_fields);
6525 constructor_fields = TREE_CHAIN (constructor_fields);
6526 /* Skip any nameless bit fields at the beginning. */
6527 while (constructor_fields != 0
6528 && DECL_C_BIT_FIELD (constructor_fields)
6529 && DECL_NAME (constructor_fields) == 0)
6530 constructor_fields = TREE_CHAIN (constructor_fields);
6532 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6534 tree fieldtype;
6535 enum tree_code fieldcode;
6537 if (constructor_fields == 0)
6539 pedwarn_init ("excess elements in union initializer");
6540 break;
6543 fieldtype = TREE_TYPE (constructor_fields);
6544 if (fieldtype != error_mark_node)
6545 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6546 fieldcode = TREE_CODE (fieldtype);
6548 /* Warn that traditional C rejects initialization of unions.
6549 We skip the warning if the value is zero. This is done
6550 under the assumption that the zero initializer in user
6551 code appears conditioned on e.g. __STDC__ to avoid
6552 "missing initializer" warnings and relies on default
6553 initialization to zero in the traditional C case.
6554 We also skip the warning if the initializer is designated,
6555 again on the assumption that this must be conditional on
6556 __STDC__ anyway (and we've already complained about the
6557 member-designator already). */
6558 if (!in_system_header && !constructor_designated
6559 && !(value.value && (integer_zerop (value.value)
6560 || real_zerop (value.value))))
6561 warning (OPT_Wtraditional, "traditional C rejects initialization "
6562 "of unions");
6564 /* Accept a string constant to initialize a subarray. */
6565 if (value.value != 0
6566 && fieldcode == ARRAY_TYPE
6567 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6568 && string_flag)
6569 value.value = orig_value;
6570 /* Otherwise, if we have come to a subaggregate,
6571 and we don't have an element of its type, push into it. */
6572 else if (value.value != 0
6573 && value.value != error_mark_node
6574 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6575 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6576 || fieldcode == UNION_TYPE))
6578 push_init_level (1);
6579 continue;
6582 if (value.value)
6584 push_member_name (constructor_fields);
6585 output_init_element (value.value, strict_string,
6586 fieldtype, constructor_fields, 1);
6587 RESTORE_SPELLING_DEPTH (constructor_depth);
6589 else
6590 /* Do the bookkeeping for an element that was
6591 directly output as a constructor. */
6593 constructor_bit_index = DECL_SIZE (constructor_fields);
6594 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6597 constructor_fields = 0;
6599 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6601 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6602 enum tree_code eltcode = TREE_CODE (elttype);
6604 /* Accept a string constant to initialize a subarray. */
6605 if (value.value != 0
6606 && eltcode == ARRAY_TYPE
6607 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6608 && string_flag)
6609 value.value = orig_value;
6610 /* Otherwise, if we have come to a subaggregate,
6611 and we don't have an element of its type, push into it. */
6612 else if (value.value != 0
6613 && value.value != error_mark_node
6614 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6615 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6616 || eltcode == UNION_TYPE))
6618 push_init_level (1);
6619 continue;
6622 if (constructor_max_index != 0
6623 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6624 || integer_all_onesp (constructor_max_index)))
6626 pedwarn_init ("excess elements in array initializer");
6627 break;
6630 /* Now output the actual element. */
6631 if (value.value)
6633 push_array_bounds (tree_low_cst (constructor_index, 1));
6634 output_init_element (value.value, strict_string,
6635 elttype, constructor_index, 1);
6636 RESTORE_SPELLING_DEPTH (constructor_depth);
6639 constructor_index
6640 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6642 if (!value.value)
6643 /* If we are doing the bookkeeping for an element that was
6644 directly output as a constructor, we must update
6645 constructor_unfilled_index. */
6646 constructor_unfilled_index = constructor_index;
6648 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6650 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6652 /* Do a basic check of initializer size. Note that vectors
6653 always have a fixed size derived from their type. */
6654 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6656 pedwarn_init ("excess elements in vector initializer");
6657 break;
6660 /* Now output the actual element. */
6661 if (value.value)
6662 output_init_element (value.value, strict_string,
6663 elttype, constructor_index, 1);
6665 constructor_index
6666 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6668 if (!value.value)
6669 /* If we are doing the bookkeeping for an element that was
6670 directly output as a constructor, we must update
6671 constructor_unfilled_index. */
6672 constructor_unfilled_index = constructor_index;
6675 /* Handle the sole element allowed in a braced initializer
6676 for a scalar variable. */
6677 else if (constructor_type != error_mark_node
6678 && constructor_fields == 0)
6680 pedwarn_init ("excess elements in scalar initializer");
6681 break;
6683 else
6685 if (value.value)
6686 output_init_element (value.value, strict_string,
6687 constructor_type, NULL_TREE, 1);
6688 constructor_fields = 0;
6691 /* Handle range initializers either at this level or anywhere higher
6692 in the designator stack. */
6693 if (constructor_range_stack)
6695 struct constructor_range_stack *p, *range_stack;
6696 int finish = 0;
6698 range_stack = constructor_range_stack;
6699 constructor_range_stack = 0;
6700 while (constructor_stack != range_stack->stack)
6702 gcc_assert (constructor_stack->implicit);
6703 process_init_element (pop_init_level (1));
6705 for (p = range_stack;
6706 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6707 p = p->prev)
6709 gcc_assert (constructor_stack->implicit);
6710 process_init_element (pop_init_level (1));
6713 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6714 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6715 finish = 1;
6717 while (1)
6719 constructor_index = p->index;
6720 constructor_fields = p->fields;
6721 if (finish && p->range_end && p->index == p->range_start)
6723 finish = 0;
6724 p->prev = 0;
6726 p = p->next;
6727 if (!p)
6728 break;
6729 push_init_level (2);
6730 p->stack = constructor_stack;
6731 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6732 p->index = p->range_start;
6735 if (!finish)
6736 constructor_range_stack = range_stack;
6737 continue;
6740 break;
6743 constructor_range_stack = 0;
6746 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6747 (guaranteed to be 'volatile' or null) and ARGS (represented using
6748 an ASM_EXPR node). */
6749 tree
6750 build_asm_stmt (tree cv_qualifier, tree args)
6752 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6753 ASM_VOLATILE_P (args) = 1;
6754 return add_stmt (args);
6757 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6758 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6759 SIMPLE indicates whether there was anything at all after the
6760 string in the asm expression -- asm("blah") and asm("blah" : )
6761 are subtly different. We use a ASM_EXPR node to represent this. */
6762 tree
6763 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6764 bool simple)
6766 tree tail;
6767 tree args;
6768 int i;
6769 const char *constraint;
6770 const char **oconstraints;
6771 bool allows_mem, allows_reg, is_inout;
6772 int ninputs, noutputs;
6774 ninputs = list_length (inputs);
6775 noutputs = list_length (outputs);
6776 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6778 string = resolve_asm_operand_names (string, outputs, inputs);
6780 /* Remove output conversions that change the type but not the mode. */
6781 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6783 tree output = TREE_VALUE (tail);
6785 /* ??? Really, this should not be here. Users should be using a
6786 proper lvalue, dammit. But there's a long history of using casts
6787 in the output operands. In cases like longlong.h, this becomes a
6788 primitive form of typechecking -- if the cast can be removed, then
6789 the output operand had a type of the proper width; otherwise we'll
6790 get an error. Gross, but ... */
6791 STRIP_NOPS (output);
6793 if (!lvalue_or_else (output, lv_asm))
6794 output = error_mark_node;
6796 if (output != error_mark_node
6797 && (TREE_READONLY (output)
6798 || TYPE_READONLY (TREE_TYPE (output))
6799 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6800 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6801 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6802 readonly_error (output, lv_asm);
6804 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6805 oconstraints[i] = constraint;
6807 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6808 &allows_mem, &allows_reg, &is_inout))
6810 /* If the operand is going to end up in memory,
6811 mark it addressable. */
6812 if (!allows_reg && !c_mark_addressable (output))
6813 output = error_mark_node;
6815 else
6816 output = error_mark_node;
6818 TREE_VALUE (tail) = output;
6821 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6823 tree input;
6825 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6826 input = TREE_VALUE (tail);
6828 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6829 oconstraints, &allows_mem, &allows_reg))
6831 /* If the operand is going to end up in memory,
6832 mark it addressable. */
6833 if (!allows_reg && allows_mem)
6835 /* Strip the nops as we allow this case. FIXME, this really
6836 should be rejected or made deprecated. */
6837 STRIP_NOPS (input);
6838 if (!c_mark_addressable (input))
6839 input = error_mark_node;
6842 else
6843 input = error_mark_node;
6845 TREE_VALUE (tail) = input;
6848 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6850 /* asm statements without outputs, including simple ones, are treated
6851 as volatile. */
6852 ASM_INPUT_P (args) = simple;
6853 ASM_VOLATILE_P (args) = (noutputs == 0);
6855 return args;
6858 /* Generate a goto statement to LABEL. */
6860 tree
6861 c_finish_goto_label (tree label)
6863 tree decl = lookup_label (label);
6864 if (!decl)
6865 return NULL_TREE;
6867 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6869 error ("jump into statement expression");
6870 return NULL_TREE;
6873 if (C_DECL_UNJUMPABLE_VM (decl))
6875 error ("jump into scope of identifier with variably modified type");
6876 return NULL_TREE;
6879 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6881 /* No jump from outside this statement expression context, so
6882 record that there is a jump from within this context. */
6883 struct c_label_list *nlist;
6884 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6885 nlist->next = label_context_stack_se->labels_used;
6886 nlist->label = decl;
6887 label_context_stack_se->labels_used = nlist;
6890 if (!C_DECL_UNDEFINABLE_VM (decl))
6892 /* No jump from outside this context context of identifiers with
6893 variably modified type, so record that there is a jump from
6894 within this context. */
6895 struct c_label_list *nlist;
6896 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6897 nlist->next = label_context_stack_vm->labels_used;
6898 nlist->label = decl;
6899 label_context_stack_vm->labels_used = nlist;
6902 TREE_USED (decl) = 1;
6903 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6906 /* Generate a computed goto statement to EXPR. */
6908 tree
6909 c_finish_goto_ptr (tree expr)
6911 if (pedantic)
6912 pedwarn ("ISO C forbids %<goto *expr;%>");
6913 expr = convert (ptr_type_node, expr);
6914 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6917 /* Generate a C `return' statement. RETVAL is the expression for what
6918 to return, or a null pointer for `return;' with no value. */
6920 tree
6921 c_finish_return (tree retval)
6923 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6924 bool no_warning = false;
6926 if (TREE_THIS_VOLATILE (current_function_decl))
6927 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6929 if (!retval)
6931 current_function_returns_null = 1;
6932 if ((warn_return_type || flag_isoc99)
6933 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6935 pedwarn_c99 ("%<return%> with no value, in "
6936 "function returning non-void");
6937 no_warning = true;
6940 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6942 current_function_returns_null = 1;
6943 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6944 pedwarn ("%<return%> with a value, in function returning void");
6946 else
6948 tree t = convert_for_assignment (valtype, retval, ic_return,
6949 NULL_TREE, NULL_TREE, 0);
6950 tree res = DECL_RESULT (current_function_decl);
6951 tree inner;
6953 current_function_returns_value = 1;
6954 if (t == error_mark_node)
6955 return NULL_TREE;
6957 inner = t = convert (TREE_TYPE (res), t);
6959 /* Strip any conversions, additions, and subtractions, and see if
6960 we are returning the address of a local variable. Warn if so. */
6961 while (1)
6963 switch (TREE_CODE (inner))
6965 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6966 case PLUS_EXPR:
6967 inner = TREE_OPERAND (inner, 0);
6968 continue;
6970 case MINUS_EXPR:
6971 /* If the second operand of the MINUS_EXPR has a pointer
6972 type (or is converted from it), this may be valid, so
6973 don't give a warning. */
6975 tree op1 = TREE_OPERAND (inner, 1);
6977 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6978 && (TREE_CODE (op1) == NOP_EXPR
6979 || TREE_CODE (op1) == NON_LVALUE_EXPR
6980 || TREE_CODE (op1) == CONVERT_EXPR))
6981 op1 = TREE_OPERAND (op1, 0);
6983 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6984 break;
6986 inner = TREE_OPERAND (inner, 0);
6987 continue;
6990 case ADDR_EXPR:
6991 inner = TREE_OPERAND (inner, 0);
6993 while (REFERENCE_CLASS_P (inner)
6994 && TREE_CODE (inner) != INDIRECT_REF)
6995 inner = TREE_OPERAND (inner, 0);
6997 if (DECL_P (inner)
6998 && !DECL_EXTERNAL (inner)
6999 && !TREE_STATIC (inner)
7000 && DECL_CONTEXT (inner) == current_function_decl)
7001 warning (0, "function returns address of local variable");
7002 break;
7004 default:
7005 break;
7008 break;
7011 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7014 ret_stmt = build_stmt (RETURN_EXPR, retval);
7015 TREE_NO_WARNING (ret_stmt) |= no_warning;
7016 return add_stmt (ret_stmt);
7019 struct c_switch {
7020 /* The SWITCH_EXPR being built. */
7021 tree switch_expr;
7023 /* The original type of the testing expression, i.e. before the
7024 default conversion is applied. */
7025 tree orig_type;
7027 /* A splay-tree mapping the low element of a case range to the high
7028 element, or NULL_TREE if there is no high element. Used to
7029 determine whether or not a new case label duplicates an old case
7030 label. We need a tree, rather than simply a hash table, because
7031 of the GNU case range extension. */
7032 splay_tree cases;
7034 /* Number of nested statement expressions within this switch
7035 statement; if nonzero, case and default labels may not
7036 appear. */
7037 unsigned int blocked_stmt_expr;
7039 /* Scope of outermost declarations of identifiers with variably
7040 modified type within this switch statement; if nonzero, case and
7041 default labels may not appear. */
7042 unsigned int blocked_vm;
7044 /* The next node on the stack. */
7045 struct c_switch *next;
7048 /* A stack of the currently active switch statements. The innermost
7049 switch statement is on the top of the stack. There is no need to
7050 mark the stack for garbage collection because it is only active
7051 during the processing of the body of a function, and we never
7052 collect at that point. */
7054 struct c_switch *c_switch_stack;
7056 /* Start a C switch statement, testing expression EXP. Return the new
7057 SWITCH_EXPR. */
7059 tree
7060 c_start_case (tree exp)
7062 enum tree_code code;
7063 tree type, orig_type = error_mark_node;
7064 struct c_switch *cs;
7066 if (exp != error_mark_node)
7068 code = TREE_CODE (TREE_TYPE (exp));
7069 orig_type = TREE_TYPE (exp);
7071 if (!INTEGRAL_TYPE_P (orig_type)
7072 && code != ERROR_MARK)
7074 error ("switch quantity not an integer");
7075 exp = integer_zero_node;
7076 orig_type = error_mark_node;
7078 else
7080 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7082 if (!in_system_header
7083 && (type == long_integer_type_node
7084 || type == long_unsigned_type_node))
7085 warning (OPT_Wtraditional, "%<long%> switch expression not "
7086 "converted to %<int%> in ISO C");
7088 exp = default_conversion (exp);
7089 type = TREE_TYPE (exp);
7093 /* Add this new SWITCH_EXPR to the stack. */
7094 cs = XNEW (struct c_switch);
7095 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7096 cs->orig_type = orig_type;
7097 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7098 cs->blocked_stmt_expr = 0;
7099 cs->blocked_vm = 0;
7100 cs->next = c_switch_stack;
7101 c_switch_stack = cs;
7103 return add_stmt (cs->switch_expr);
7106 /* Process a case label. */
7108 tree
7109 do_case (tree low_value, tree high_value)
7111 tree label = NULL_TREE;
7113 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7114 && !c_switch_stack->blocked_vm)
7116 label = c_add_case_label (c_switch_stack->cases,
7117 SWITCH_COND (c_switch_stack->switch_expr),
7118 c_switch_stack->orig_type,
7119 low_value, high_value);
7120 if (label == error_mark_node)
7121 label = NULL_TREE;
7123 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7125 if (low_value)
7126 error ("case label in statement expression not containing "
7127 "enclosing switch statement");
7128 else
7129 error ("%<default%> label in statement expression not containing "
7130 "enclosing switch statement");
7132 else if (c_switch_stack && c_switch_stack->blocked_vm)
7134 if (low_value)
7135 error ("case label in scope of identifier with variably modified "
7136 "type not containing enclosing switch statement");
7137 else
7138 error ("%<default%> label in scope of identifier with variably "
7139 "modified type not containing enclosing switch statement");
7141 else if (low_value)
7142 error ("case label not within a switch statement");
7143 else
7144 error ("%<default%> label not within a switch statement");
7146 return label;
7149 /* Finish the switch statement. */
7151 void
7152 c_finish_case (tree body)
7154 struct c_switch *cs = c_switch_stack;
7155 location_t switch_location;
7157 SWITCH_BODY (cs->switch_expr) = body;
7159 /* We must not be within a statement expression nested in the switch
7160 at this point; we might, however, be within the scope of an
7161 identifier with variably modified type nested in the switch. */
7162 gcc_assert (!cs->blocked_stmt_expr);
7164 /* Emit warnings as needed. */
7165 if (EXPR_HAS_LOCATION (cs->switch_expr))
7166 switch_location = EXPR_LOCATION (cs->switch_expr);
7167 else
7168 switch_location = input_location;
7169 c_do_switch_warnings (cs->cases, switch_location,
7170 TREE_TYPE (cs->switch_expr),
7171 SWITCH_COND (cs->switch_expr));
7173 /* Pop the stack. */
7174 c_switch_stack = cs->next;
7175 splay_tree_delete (cs->cases);
7176 XDELETE (cs);
7179 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7180 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7181 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7182 statement, and was not surrounded with parenthesis. */
7184 void
7185 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7186 tree else_block, bool nested_if)
7188 tree stmt;
7190 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7191 if (warn_parentheses && nested_if && else_block == NULL)
7193 tree inner_if = then_block;
7195 /* We know from the grammar productions that there is an IF nested
7196 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7197 it might not be exactly THEN_BLOCK, but should be the last
7198 non-container statement within. */
7199 while (1)
7200 switch (TREE_CODE (inner_if))
7202 case COND_EXPR:
7203 goto found;
7204 case BIND_EXPR:
7205 inner_if = BIND_EXPR_BODY (inner_if);
7206 break;
7207 case STATEMENT_LIST:
7208 inner_if = expr_last (then_block);
7209 break;
7210 case TRY_FINALLY_EXPR:
7211 case TRY_CATCH_EXPR:
7212 inner_if = TREE_OPERAND (inner_if, 0);
7213 break;
7214 default:
7215 gcc_unreachable ();
7217 found:
7219 if (COND_EXPR_ELSE (inner_if))
7220 warning (OPT_Wparentheses,
7221 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7222 &if_locus);
7225 empty_body_warning (then_block, else_block);
7227 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7228 SET_EXPR_LOCATION (stmt, if_locus);
7229 add_stmt (stmt);
7232 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7233 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7234 is false for DO loops. INCR is the FOR increment expression. BODY is
7235 the statement controlled by the loop. BLAB is the break label. CLAB is
7236 the continue label. Everything is allowed to be NULL. */
7238 void
7239 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7240 tree blab, tree clab, bool cond_is_first)
7242 tree entry = NULL, exit = NULL, t;
7244 /* If the condition is zero don't generate a loop construct. */
7245 if (cond && integer_zerop (cond))
7247 if (cond_is_first)
7249 t = build_and_jump (&blab);
7250 SET_EXPR_LOCATION (t, start_locus);
7251 add_stmt (t);
7254 else
7256 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7258 /* If we have an exit condition, then we build an IF with gotos either
7259 out of the loop, or to the top of it. If there's no exit condition,
7260 then we just build a jump back to the top. */
7261 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7263 if (cond && !integer_nonzerop (cond))
7265 /* Canonicalize the loop condition to the end. This means
7266 generating a branch to the loop condition. Reuse the
7267 continue label, if possible. */
7268 if (cond_is_first)
7270 if (incr || !clab)
7272 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7273 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7275 else
7276 t = build1 (GOTO_EXPR, void_type_node, clab);
7277 SET_EXPR_LOCATION (t, start_locus);
7278 add_stmt (t);
7281 t = build_and_jump (&blab);
7282 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7283 if (cond_is_first)
7284 SET_EXPR_LOCATION (exit, start_locus);
7285 else
7286 SET_EXPR_LOCATION (exit, input_location);
7289 add_stmt (top);
7292 if (body)
7293 add_stmt (body);
7294 if (clab)
7295 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7296 if (incr)
7297 add_stmt (incr);
7298 if (entry)
7299 add_stmt (entry);
7300 if (exit)
7301 add_stmt (exit);
7302 if (blab)
7303 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7306 tree
7307 c_finish_bc_stmt (tree *label_p, bool is_break)
7309 bool skip;
7310 tree label = *label_p;
7312 /* In switch statements break is sometimes stylistically used after
7313 a return statement. This can lead to spurious warnings about
7314 control reaching the end of a non-void function when it is
7315 inlined. Note that we are calling block_may_fallthru with
7316 language specific tree nodes; this works because
7317 block_may_fallthru returns true when given something it does not
7318 understand. */
7319 skip = !block_may_fallthru (cur_stmt_list);
7321 if (!label)
7323 if (!skip)
7324 *label_p = label = create_artificial_label ();
7326 else if (TREE_CODE (label) == LABEL_DECL)
7328 else switch (TREE_INT_CST_LOW (label))
7330 case 0:
7331 if (is_break)
7332 error ("break statement not within loop or switch");
7333 else
7334 error ("continue statement not within a loop");
7335 return NULL_TREE;
7337 case 1:
7338 gcc_assert (is_break);
7339 error ("break statement used with OpenMP for loop");
7340 return NULL_TREE;
7342 default:
7343 gcc_unreachable ();
7346 if (skip)
7347 return NULL_TREE;
7349 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7352 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7354 static void
7355 emit_side_effect_warnings (tree expr)
7357 if (expr == error_mark_node)
7359 else if (!TREE_SIDE_EFFECTS (expr))
7361 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7362 warning (0, "%Hstatement with no effect",
7363 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7365 else if (warn_unused_value)
7366 warn_if_unused_value (expr, input_location);
7369 /* Process an expression as if it were a complete statement. Emit
7370 diagnostics, but do not call ADD_STMT. */
7372 tree
7373 c_process_expr_stmt (tree expr)
7375 if (!expr)
7376 return NULL_TREE;
7378 if (warn_sequence_point)
7379 verify_sequence_points (expr);
7381 if (TREE_TYPE (expr) != error_mark_node
7382 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7383 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7384 error ("expression statement has incomplete type");
7386 /* If we're not processing a statement expression, warn about unused values.
7387 Warnings for statement expressions will be emitted later, once we figure
7388 out which is the result. */
7389 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7390 && (extra_warnings || warn_unused_value))
7391 emit_side_effect_warnings (expr);
7393 /* If the expression is not of a type to which we cannot assign a line
7394 number, wrap the thing in a no-op NOP_EXPR. */
7395 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7396 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7398 if (EXPR_P (expr))
7399 SET_EXPR_LOCATION (expr, input_location);
7401 return expr;
7404 /* Emit an expression as a statement. */
7406 tree
7407 c_finish_expr_stmt (tree expr)
7409 if (expr)
7410 return add_stmt (c_process_expr_stmt (expr));
7411 else
7412 return NULL;
7415 /* Do the opposite and emit a statement as an expression. To begin,
7416 create a new binding level and return it. */
7418 tree
7419 c_begin_stmt_expr (void)
7421 tree ret;
7422 struct c_label_context_se *nstack;
7423 struct c_label_list *glist;
7425 /* We must force a BLOCK for this level so that, if it is not expanded
7426 later, there is a way to turn off the entire subtree of blocks that
7427 are contained in it. */
7428 keep_next_level ();
7429 ret = c_begin_compound_stmt (true);
7430 if (c_switch_stack)
7432 c_switch_stack->blocked_stmt_expr++;
7433 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7435 for (glist = label_context_stack_se->labels_used;
7436 glist != NULL;
7437 glist = glist->next)
7439 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7441 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7442 nstack->labels_def = NULL;
7443 nstack->labels_used = NULL;
7444 nstack->next = label_context_stack_se;
7445 label_context_stack_se = nstack;
7447 /* Mark the current statement list as belonging to a statement list. */
7448 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7450 return ret;
7453 tree
7454 c_finish_stmt_expr (tree body)
7456 tree last, type, tmp, val;
7457 tree *last_p;
7458 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7460 body = c_end_compound_stmt (body, true);
7461 if (c_switch_stack)
7463 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7464 c_switch_stack->blocked_stmt_expr--;
7466 /* It is no longer possible to jump to labels defined within this
7467 statement expression. */
7468 for (dlist = label_context_stack_se->labels_def;
7469 dlist != NULL;
7470 dlist = dlist->next)
7472 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7474 /* It is again possible to define labels with a goto just outside
7475 this statement expression. */
7476 for (glist = label_context_stack_se->next->labels_used;
7477 glist != NULL;
7478 glist = glist->next)
7480 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7481 glist_prev = glist;
7483 if (glist_prev != NULL)
7484 glist_prev->next = label_context_stack_se->labels_used;
7485 else
7486 label_context_stack_se->next->labels_used
7487 = label_context_stack_se->labels_used;
7488 label_context_stack_se = label_context_stack_se->next;
7490 /* Locate the last statement in BODY. See c_end_compound_stmt
7491 about always returning a BIND_EXPR. */
7492 last_p = &BIND_EXPR_BODY (body);
7493 last = BIND_EXPR_BODY (body);
7495 continue_searching:
7496 if (TREE_CODE (last) == STATEMENT_LIST)
7498 tree_stmt_iterator i;
7500 /* This can happen with degenerate cases like ({ }). No value. */
7501 if (!TREE_SIDE_EFFECTS (last))
7502 return body;
7504 /* If we're supposed to generate side effects warnings, process
7505 all of the statements except the last. */
7506 if (extra_warnings || warn_unused_value)
7508 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7509 emit_side_effect_warnings (tsi_stmt (i));
7511 else
7512 i = tsi_last (last);
7513 last_p = tsi_stmt_ptr (i);
7514 last = *last_p;
7517 /* If the end of the list is exception related, then the list was split
7518 by a call to push_cleanup. Continue searching. */
7519 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7520 || TREE_CODE (last) == TRY_CATCH_EXPR)
7522 last_p = &TREE_OPERAND (last, 0);
7523 last = *last_p;
7524 goto continue_searching;
7527 /* In the case that the BIND_EXPR is not necessary, return the
7528 expression out from inside it. */
7529 if (last == error_mark_node
7530 || (last == BIND_EXPR_BODY (body)
7531 && BIND_EXPR_VARS (body) == NULL))
7533 /* Do not warn if the return value of a statement expression is
7534 unused. */
7535 if (EXPR_P (last))
7536 TREE_NO_WARNING (last) = 1;
7537 return last;
7540 /* Extract the type of said expression. */
7541 type = TREE_TYPE (last);
7543 /* If we're not returning a value at all, then the BIND_EXPR that
7544 we already have is a fine expression to return. */
7545 if (!type || VOID_TYPE_P (type))
7546 return body;
7548 /* Now that we've located the expression containing the value, it seems
7549 silly to make voidify_wrapper_expr repeat the process. Create a
7550 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7551 tmp = create_tmp_var_raw (type, NULL);
7553 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7554 tree_expr_nonnegative_p giving up immediately. */
7555 val = last;
7556 if (TREE_CODE (val) == NOP_EXPR
7557 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7558 val = TREE_OPERAND (val, 0);
7560 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7561 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7563 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7566 /* Begin the scope of an identifier of variably modified type, scope
7567 number SCOPE. Jumping from outside this scope to inside it is not
7568 permitted. */
7570 void
7571 c_begin_vm_scope (unsigned int scope)
7573 struct c_label_context_vm *nstack;
7574 struct c_label_list *glist;
7576 gcc_assert (scope > 0);
7578 /* At file_scope, we don't have to do any processing. */
7579 if (label_context_stack_vm == NULL)
7580 return;
7582 if (c_switch_stack && !c_switch_stack->blocked_vm)
7583 c_switch_stack->blocked_vm = scope;
7584 for (glist = label_context_stack_vm->labels_used;
7585 glist != NULL;
7586 glist = glist->next)
7588 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7590 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7591 nstack->labels_def = NULL;
7592 nstack->labels_used = NULL;
7593 nstack->scope = scope;
7594 nstack->next = label_context_stack_vm;
7595 label_context_stack_vm = nstack;
7598 /* End a scope which may contain identifiers of variably modified
7599 type, scope number SCOPE. */
7601 void
7602 c_end_vm_scope (unsigned int scope)
7604 if (label_context_stack_vm == NULL)
7605 return;
7606 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7607 c_switch_stack->blocked_vm = 0;
7608 /* We may have a number of nested scopes of identifiers with
7609 variably modified type, all at this depth. Pop each in turn. */
7610 while (label_context_stack_vm->scope == scope)
7612 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7614 /* It is no longer possible to jump to labels defined within this
7615 scope. */
7616 for (dlist = label_context_stack_vm->labels_def;
7617 dlist != NULL;
7618 dlist = dlist->next)
7620 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7622 /* It is again possible to define labels with a goto just outside
7623 this scope. */
7624 for (glist = label_context_stack_vm->next->labels_used;
7625 glist != NULL;
7626 glist = glist->next)
7628 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7629 glist_prev = glist;
7631 if (glist_prev != NULL)
7632 glist_prev->next = label_context_stack_vm->labels_used;
7633 else
7634 label_context_stack_vm->next->labels_used
7635 = label_context_stack_vm->labels_used;
7636 label_context_stack_vm = label_context_stack_vm->next;
7640 /* Begin and end compound statements. This is as simple as pushing
7641 and popping new statement lists from the tree. */
7643 tree
7644 c_begin_compound_stmt (bool do_scope)
7646 tree stmt = push_stmt_list ();
7647 if (do_scope)
7648 push_scope ();
7649 return stmt;
7652 tree
7653 c_end_compound_stmt (tree stmt, bool do_scope)
7655 tree block = NULL;
7657 if (do_scope)
7659 if (c_dialect_objc ())
7660 objc_clear_super_receiver ();
7661 block = pop_scope ();
7664 stmt = pop_stmt_list (stmt);
7665 stmt = c_build_bind_expr (block, stmt);
7667 /* If this compound statement is nested immediately inside a statement
7668 expression, then force a BIND_EXPR to be created. Otherwise we'll
7669 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7670 STATEMENT_LISTs merge, and thus we can lose track of what statement
7671 was really last. */
7672 if (cur_stmt_list
7673 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7674 && TREE_CODE (stmt) != BIND_EXPR)
7676 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7677 TREE_SIDE_EFFECTS (stmt) = 1;
7680 return stmt;
7683 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7684 when the current scope is exited. EH_ONLY is true when this is not
7685 meant to apply to normal control flow transfer. */
7687 void
7688 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7690 enum tree_code code;
7691 tree stmt, list;
7692 bool stmt_expr;
7694 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7695 stmt = build_stmt (code, NULL, cleanup);
7696 add_stmt (stmt);
7697 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7698 list = push_stmt_list ();
7699 TREE_OPERAND (stmt, 0) = list;
7700 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7703 /* Build a binary-operation expression without default conversions.
7704 CODE is the kind of expression to build.
7705 This function differs from `build' in several ways:
7706 the data type of the result is computed and recorded in it,
7707 warnings are generated if arg data types are invalid,
7708 special handling for addition and subtraction of pointers is known,
7709 and some optimization is done (operations on narrow ints
7710 are done in the narrower type when that gives the same result).
7711 Constant folding is also done before the result is returned.
7713 Note that the operands will never have enumeral types, or function
7714 or array types, because either they will have the default conversions
7715 performed or they have both just been converted to some other type in which
7716 the arithmetic is to be done. */
7718 tree
7719 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7720 int convert_p)
7722 tree type0, type1;
7723 enum tree_code code0, code1;
7724 tree op0, op1;
7725 const char *invalid_op_diag;
7727 /* Expression code to give to the expression when it is built.
7728 Normally this is CODE, which is what the caller asked for,
7729 but in some special cases we change it. */
7730 enum tree_code resultcode = code;
7732 /* Data type in which the computation is to be performed.
7733 In the simplest cases this is the common type of the arguments. */
7734 tree result_type = NULL;
7736 /* Nonzero means operands have already been type-converted
7737 in whatever way is necessary.
7738 Zero means they need to be converted to RESULT_TYPE. */
7739 int converted = 0;
7741 /* Nonzero means create the expression with this type, rather than
7742 RESULT_TYPE. */
7743 tree build_type = 0;
7745 /* Nonzero means after finally constructing the expression
7746 convert it to this type. */
7747 tree final_type = 0;
7749 /* Nonzero if this is an operation like MIN or MAX which can
7750 safely be computed in short if both args are promoted shorts.
7751 Also implies COMMON.
7752 -1 indicates a bitwise operation; this makes a difference
7753 in the exact conditions for when it is safe to do the operation
7754 in a narrower mode. */
7755 int shorten = 0;
7757 /* Nonzero if this is a comparison operation;
7758 if both args are promoted shorts, compare the original shorts.
7759 Also implies COMMON. */
7760 int short_compare = 0;
7762 /* Nonzero if this is a right-shift operation, which can be computed on the
7763 original short and then promoted if the operand is a promoted short. */
7764 int short_shift = 0;
7766 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7767 int common = 0;
7769 /* True means types are compatible as far as ObjC is concerned. */
7770 bool objc_ok;
7772 if (convert_p)
7774 op0 = default_conversion (orig_op0);
7775 op1 = default_conversion (orig_op1);
7777 else
7779 op0 = orig_op0;
7780 op1 = orig_op1;
7783 type0 = TREE_TYPE (op0);
7784 type1 = TREE_TYPE (op1);
7786 /* The expression codes of the data types of the arguments tell us
7787 whether the arguments are integers, floating, pointers, etc. */
7788 code0 = TREE_CODE (type0);
7789 code1 = TREE_CODE (type1);
7791 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7792 STRIP_TYPE_NOPS (op0);
7793 STRIP_TYPE_NOPS (op1);
7795 /* If an error was already reported for one of the arguments,
7796 avoid reporting another error. */
7798 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7799 return error_mark_node;
7801 if ((invalid_op_diag
7802 = targetm.invalid_binary_op (code, type0, type1)))
7804 error (invalid_op_diag);
7805 return error_mark_node;
7808 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7810 switch (code)
7812 case PLUS_EXPR:
7813 /* Handle the pointer + int case. */
7814 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7815 return pointer_int_sum (PLUS_EXPR, op0, op1);
7816 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7817 return pointer_int_sum (PLUS_EXPR, op1, op0);
7818 else
7819 common = 1;
7820 break;
7822 case MINUS_EXPR:
7823 /* Subtraction of two similar pointers.
7824 We must subtract them as integers, then divide by object size. */
7825 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7826 && comp_target_types (type0, type1))
7827 return pointer_diff (op0, op1);
7828 /* Handle pointer minus int. Just like pointer plus int. */
7829 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7830 return pointer_int_sum (MINUS_EXPR, op0, op1);
7831 else
7832 common = 1;
7833 break;
7835 case MULT_EXPR:
7836 common = 1;
7837 break;
7839 case TRUNC_DIV_EXPR:
7840 case CEIL_DIV_EXPR:
7841 case FLOOR_DIV_EXPR:
7842 case ROUND_DIV_EXPR:
7843 case EXACT_DIV_EXPR:
7844 /* Floating point division by zero is a legitimate way to obtain
7845 infinities and NaNs. */
7846 if (skip_evaluation == 0 && integer_zerop (op1))
7847 warning (OPT_Wdiv_by_zero, "division by zero");
7849 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7850 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7851 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7852 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7854 enum tree_code tcode0 = code0, tcode1 = code1;
7856 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7857 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7858 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7859 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7861 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7862 resultcode = RDIV_EXPR;
7863 else
7864 /* Although it would be tempting to shorten always here, that
7865 loses on some targets, since the modulo instruction is
7866 undefined if the quotient can't be represented in the
7867 computation mode. We shorten only if unsigned or if
7868 dividing by something we know != -1. */
7869 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7870 || (TREE_CODE (op1) == INTEGER_CST
7871 && !integer_all_onesp (op1)));
7872 common = 1;
7874 break;
7876 case BIT_AND_EXPR:
7877 case BIT_IOR_EXPR:
7878 case BIT_XOR_EXPR:
7879 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7880 shorten = -1;
7881 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7882 common = 1;
7883 break;
7885 case TRUNC_MOD_EXPR:
7886 case FLOOR_MOD_EXPR:
7887 if (skip_evaluation == 0 && integer_zerop (op1))
7888 warning (OPT_Wdiv_by_zero, "division by zero");
7890 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7892 /* Although it would be tempting to shorten always here, that loses
7893 on some targets, since the modulo instruction is undefined if the
7894 quotient can't be represented in the computation mode. We shorten
7895 only if unsigned or if dividing by something we know != -1. */
7896 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7897 || (TREE_CODE (op1) == INTEGER_CST
7898 && !integer_all_onesp (op1)));
7899 common = 1;
7901 break;
7903 case TRUTH_ANDIF_EXPR:
7904 case TRUTH_ORIF_EXPR:
7905 case TRUTH_AND_EXPR:
7906 case TRUTH_OR_EXPR:
7907 case TRUTH_XOR_EXPR:
7908 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7909 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7910 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7911 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7913 /* Result of these operations is always an int,
7914 but that does not mean the operands should be
7915 converted to ints! */
7916 result_type = integer_type_node;
7917 op0 = c_common_truthvalue_conversion (op0);
7918 op1 = c_common_truthvalue_conversion (op1);
7919 converted = 1;
7921 break;
7923 /* Shift operations: result has same type as first operand;
7924 always convert second operand to int.
7925 Also set SHORT_SHIFT if shifting rightward. */
7927 case RSHIFT_EXPR:
7928 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7930 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7932 if (tree_int_cst_sgn (op1) < 0)
7933 warning (0, "right shift count is negative");
7934 else
7936 if (!integer_zerop (op1))
7937 short_shift = 1;
7939 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7940 warning (0, "right shift count >= width of type");
7944 /* Use the type of the value to be shifted. */
7945 result_type = type0;
7946 /* Convert the shift-count to an integer, regardless of size
7947 of value being shifted. */
7948 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7949 op1 = convert (integer_type_node, op1);
7950 /* Avoid converting op1 to result_type later. */
7951 converted = 1;
7953 break;
7955 case LSHIFT_EXPR:
7956 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7958 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7960 if (tree_int_cst_sgn (op1) < 0)
7961 warning (0, "left shift count is negative");
7963 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7964 warning (0, "left shift count >= width of type");
7967 /* Use the type of the value to be shifted. */
7968 result_type = type0;
7969 /* Convert the shift-count to an integer, regardless of size
7970 of value being shifted. */
7971 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7972 op1 = convert (integer_type_node, op1);
7973 /* Avoid converting op1 to result_type later. */
7974 converted = 1;
7976 break;
7978 case EQ_EXPR:
7979 case NE_EXPR:
7980 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
7981 warning (OPT_Wfloat_equal,
7982 "comparing floating point with == or != is unsafe");
7983 /* Result of comparison is always int,
7984 but don't convert the args to int! */
7985 build_type = integer_type_node;
7986 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7987 || code0 == COMPLEX_TYPE)
7988 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7989 || code1 == COMPLEX_TYPE))
7990 short_compare = 1;
7991 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7993 tree tt0 = TREE_TYPE (type0);
7994 tree tt1 = TREE_TYPE (type1);
7995 /* Anything compares with void *. void * compares with anything.
7996 Otherwise, the targets must be compatible
7997 and both must be object or both incomplete. */
7998 if (comp_target_types (type0, type1))
7999 result_type = common_pointer_type (type0, type1);
8000 else if (VOID_TYPE_P (tt0))
8002 /* op0 != orig_op0 detects the case of something
8003 whose value is 0 but which isn't a valid null ptr const. */
8004 if (pedantic && !null_pointer_constant_p (orig_op0)
8005 && TREE_CODE (tt1) == FUNCTION_TYPE)
8006 pedwarn ("ISO C forbids comparison of %<void *%>"
8007 " with function pointer");
8009 else if (VOID_TYPE_P (tt1))
8011 if (pedantic && !null_pointer_constant_p (orig_op1)
8012 && TREE_CODE (tt0) == FUNCTION_TYPE)
8013 pedwarn ("ISO C forbids comparison of %<void *%>"
8014 " with function pointer");
8016 else
8017 /* Avoid warning about the volatile ObjC EH puts on decls. */
8018 if (!objc_ok)
8019 pedwarn ("comparison of distinct pointer types lacks a cast");
8021 if (result_type == NULL_TREE)
8022 result_type = ptr_type_node;
8024 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8026 if (TREE_CODE (op0) == ADDR_EXPR
8027 && DECL_P (TREE_OPERAND (op0, 0))
8028 && (TREE_CODE (TREE_OPERAND (op0, 0)) == PARM_DECL
8029 || TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
8030 || !DECL_WEAK (TREE_OPERAND (op0, 0))))
8031 warning (OPT_Walways_true, "the address of %qD will never be NULL",
8032 TREE_OPERAND (op0, 0));
8033 result_type = type0;
8035 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8037 if (TREE_CODE (op1) == ADDR_EXPR
8038 && DECL_P (TREE_OPERAND (op1, 0))
8039 && (TREE_CODE (TREE_OPERAND (op1, 0)) == PARM_DECL
8040 || TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL
8041 || !DECL_WEAK (TREE_OPERAND (op1, 0))))
8042 warning (OPT_Walways_true, "the address of %qD will never be NULL",
8043 TREE_OPERAND (op1, 0));
8044 result_type = type1;
8046 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8048 result_type = type0;
8049 pedwarn ("comparison between pointer and integer");
8051 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8053 result_type = type1;
8054 pedwarn ("comparison between pointer and integer");
8056 break;
8058 case LE_EXPR:
8059 case GE_EXPR:
8060 case LT_EXPR:
8061 case GT_EXPR:
8062 build_type = integer_type_node;
8063 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
8064 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
8065 short_compare = 1;
8066 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8068 if (comp_target_types (type0, type1))
8070 result_type = common_pointer_type (type0, type1);
8071 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8072 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8073 pedwarn ("comparison of complete and incomplete pointers");
8074 else if (pedantic
8075 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8076 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8078 else
8080 result_type = ptr_type_node;
8081 pedwarn ("comparison of distinct pointer types lacks a cast");
8084 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8086 result_type = type0;
8087 if (pedantic || extra_warnings)
8088 pedwarn ("ordered comparison of pointer with integer zero");
8090 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8092 result_type = type1;
8093 if (pedantic)
8094 pedwarn ("ordered comparison of pointer with integer zero");
8096 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8098 result_type = type0;
8099 pedwarn ("comparison between pointer and integer");
8101 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8103 result_type = type1;
8104 pedwarn ("comparison between pointer and integer");
8106 break;
8108 default:
8109 gcc_unreachable ();
8112 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8113 return error_mark_node;
8115 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8116 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8117 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8118 TREE_TYPE (type1))))
8120 binary_op_error (code);
8121 return error_mark_node;
8124 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8125 || code0 == VECTOR_TYPE)
8127 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8128 || code1 == VECTOR_TYPE))
8130 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8132 if (shorten || common || short_compare)
8133 result_type = c_common_type (type0, type1);
8135 /* For certain operations (which identify themselves by shorten != 0)
8136 if both args were extended from the same smaller type,
8137 do the arithmetic in that type and then extend.
8139 shorten !=0 and !=1 indicates a bitwise operation.
8140 For them, this optimization is safe only if
8141 both args are zero-extended or both are sign-extended.
8142 Otherwise, we might change the result.
8143 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8144 but calculated in (unsigned short) it would be (unsigned short)-1. */
8146 if (shorten && none_complex)
8148 int unsigned0, unsigned1;
8149 tree arg0, arg1;
8150 int uns;
8151 tree type;
8153 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8154 excessive narrowing when we call get_narrower below. For
8155 example, suppose that OP0 is of unsigned int extended
8156 from signed char and that RESULT_TYPE is long long int.
8157 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8158 like
8160 (long long int) (unsigned int) signed_char
8162 which get_narrower would narrow down to
8164 (unsigned int) signed char
8166 If we do not cast OP0 first, get_narrower would return
8167 signed_char, which is inconsistent with the case of the
8168 explicit cast. */
8169 op0 = convert (result_type, op0);
8170 op1 = convert (result_type, op1);
8172 arg0 = get_narrower (op0, &unsigned0);
8173 arg1 = get_narrower (op1, &unsigned1);
8175 /* UNS is 1 if the operation to be done is an unsigned one. */
8176 uns = TYPE_UNSIGNED (result_type);
8178 final_type = result_type;
8180 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8181 but it *requires* conversion to FINAL_TYPE. */
8183 if ((TYPE_PRECISION (TREE_TYPE (op0))
8184 == TYPE_PRECISION (TREE_TYPE (arg0)))
8185 && TREE_TYPE (op0) != final_type)
8186 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8187 if ((TYPE_PRECISION (TREE_TYPE (op1))
8188 == TYPE_PRECISION (TREE_TYPE (arg1)))
8189 && TREE_TYPE (op1) != final_type)
8190 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8192 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8194 /* For bitwise operations, signedness of nominal type
8195 does not matter. Consider only how operands were extended. */
8196 if (shorten == -1)
8197 uns = unsigned0;
8199 /* Note that in all three cases below we refrain from optimizing
8200 an unsigned operation on sign-extended args.
8201 That would not be valid. */
8203 /* Both args variable: if both extended in same way
8204 from same width, do it in that width.
8205 Do it unsigned if args were zero-extended. */
8206 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8207 < TYPE_PRECISION (result_type))
8208 && (TYPE_PRECISION (TREE_TYPE (arg1))
8209 == TYPE_PRECISION (TREE_TYPE (arg0)))
8210 && unsigned0 == unsigned1
8211 && (unsigned0 || !uns))
8212 result_type
8213 = c_common_signed_or_unsigned_type
8214 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8215 else if (TREE_CODE (arg0) == INTEGER_CST
8216 && (unsigned1 || !uns)
8217 && (TYPE_PRECISION (TREE_TYPE (arg1))
8218 < TYPE_PRECISION (result_type))
8219 && (type
8220 = c_common_signed_or_unsigned_type (unsigned1,
8221 TREE_TYPE (arg1)),
8222 int_fits_type_p (arg0, type)))
8223 result_type = type;
8224 else if (TREE_CODE (arg1) == INTEGER_CST
8225 && (unsigned0 || !uns)
8226 && (TYPE_PRECISION (TREE_TYPE (arg0))
8227 < TYPE_PRECISION (result_type))
8228 && (type
8229 = c_common_signed_or_unsigned_type (unsigned0,
8230 TREE_TYPE (arg0)),
8231 int_fits_type_p (arg1, type)))
8232 result_type = type;
8235 /* Shifts can be shortened if shifting right. */
8237 if (short_shift)
8239 int unsigned_arg;
8240 tree arg0 = get_narrower (op0, &unsigned_arg);
8242 final_type = result_type;
8244 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8245 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8247 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8248 /* We can shorten only if the shift count is less than the
8249 number of bits in the smaller type size. */
8250 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8251 /* We cannot drop an unsigned shift after sign-extension. */
8252 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8254 /* Do an unsigned shift if the operand was zero-extended. */
8255 result_type
8256 = c_common_signed_or_unsigned_type (unsigned_arg,
8257 TREE_TYPE (arg0));
8258 /* Convert value-to-be-shifted to that type. */
8259 if (TREE_TYPE (op0) != result_type)
8260 op0 = convert (result_type, op0);
8261 converted = 1;
8265 /* Comparison operations are shortened too but differently.
8266 They identify themselves by setting short_compare = 1. */
8268 if (short_compare)
8270 /* Don't write &op0, etc., because that would prevent op0
8271 from being kept in a register.
8272 Instead, make copies of the our local variables and
8273 pass the copies by reference, then copy them back afterward. */
8274 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8275 enum tree_code xresultcode = resultcode;
8276 tree val
8277 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8279 if (val != 0)
8280 return val;
8282 op0 = xop0, op1 = xop1;
8283 converted = 1;
8284 resultcode = xresultcode;
8286 if (warn_sign_compare && skip_evaluation == 0)
8288 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8289 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8290 int unsignedp0, unsignedp1;
8291 tree primop0 = get_narrower (op0, &unsignedp0);
8292 tree primop1 = get_narrower (op1, &unsignedp1);
8294 xop0 = orig_op0;
8295 xop1 = orig_op1;
8296 STRIP_TYPE_NOPS (xop0);
8297 STRIP_TYPE_NOPS (xop1);
8299 /* Give warnings for comparisons between signed and unsigned
8300 quantities that may fail.
8302 Do the checking based on the original operand trees, so that
8303 casts will be considered, but default promotions won't be.
8305 Do not warn if the comparison is being done in a signed type,
8306 since the signed type will only be chosen if it can represent
8307 all the values of the unsigned type. */
8308 if (!TYPE_UNSIGNED (result_type))
8309 /* OK */;
8310 /* Do not warn if both operands are the same signedness. */
8311 else if (op0_signed == op1_signed)
8312 /* OK */;
8313 else
8315 tree sop, uop;
8317 if (op0_signed)
8318 sop = xop0, uop = xop1;
8319 else
8320 sop = xop1, uop = xop0;
8322 /* Do not warn if the signed quantity is an
8323 unsuffixed integer literal (or some static
8324 constant expression involving such literals or a
8325 conditional expression involving such literals)
8326 and it is non-negative. */
8327 if (tree_expr_nonnegative_p (sop))
8328 /* OK */;
8329 /* Do not warn if the comparison is an equality operation,
8330 the unsigned quantity is an integral constant, and it
8331 would fit in the result if the result were signed. */
8332 else if (TREE_CODE (uop) == INTEGER_CST
8333 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8334 && int_fits_type_p
8335 (uop, c_common_signed_type (result_type)))
8336 /* OK */;
8337 /* Do not warn if the unsigned quantity is an enumeration
8338 constant and its maximum value would fit in the result
8339 if the result were signed. */
8340 else if (TREE_CODE (uop) == INTEGER_CST
8341 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8342 && int_fits_type_p
8343 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8344 c_common_signed_type (result_type)))
8345 /* OK */;
8346 else
8347 warning (0, "comparison between signed and unsigned");
8350 /* Warn if two unsigned values are being compared in a size
8351 larger than their original size, and one (and only one) is the
8352 result of a `~' operator. This comparison will always fail.
8354 Also warn if one operand is a constant, and the constant
8355 does not have all bits set that are set in the ~ operand
8356 when it is extended. */
8358 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8359 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8361 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8362 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8363 &unsignedp0);
8364 else
8365 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8366 &unsignedp1);
8368 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8370 tree primop;
8371 HOST_WIDE_INT constant, mask;
8372 int unsignedp, bits;
8374 if (host_integerp (primop0, 0))
8376 primop = primop1;
8377 unsignedp = unsignedp1;
8378 constant = tree_low_cst (primop0, 0);
8380 else
8382 primop = primop0;
8383 unsignedp = unsignedp0;
8384 constant = tree_low_cst (primop1, 0);
8387 bits = TYPE_PRECISION (TREE_TYPE (primop));
8388 if (bits < TYPE_PRECISION (result_type)
8389 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8391 mask = (~(HOST_WIDE_INT) 0) << bits;
8392 if ((mask & constant) != mask)
8393 warning (0, "comparison of promoted ~unsigned with constant");
8396 else if (unsignedp0 && unsignedp1
8397 && (TYPE_PRECISION (TREE_TYPE (primop0))
8398 < TYPE_PRECISION (result_type))
8399 && (TYPE_PRECISION (TREE_TYPE (primop1))
8400 < TYPE_PRECISION (result_type)))
8401 warning (0, "comparison of promoted ~unsigned with unsigned");
8407 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8408 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8409 Then the expression will be built.
8410 It will be given type FINAL_TYPE if that is nonzero;
8411 otherwise, it will be given type RESULT_TYPE. */
8413 if (!result_type)
8415 binary_op_error (code);
8416 return error_mark_node;
8419 if (!converted)
8421 if (TREE_TYPE (op0) != result_type)
8422 op0 = convert_and_check (result_type, op0);
8423 if (TREE_TYPE (op1) != result_type)
8424 op1 = convert_and_check (result_type, op1);
8426 /* This can happen if one operand has a vector type, and the other
8427 has a different type. */
8428 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8429 return error_mark_node;
8432 if (build_type == NULL_TREE)
8433 build_type = result_type;
8436 /* Treat expressions in initializers specially as they can't trap. */
8437 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8438 build_type,
8439 op0, op1)
8440 : fold_build2 (resultcode, build_type,
8441 op0, op1);
8443 if (final_type != 0)
8444 result = convert (final_type, result);
8445 return result;
8450 /* Convert EXPR to be a truth-value, validating its type for this
8451 purpose. */
8453 tree
8454 c_objc_common_truthvalue_conversion (tree expr)
8456 switch (TREE_CODE (TREE_TYPE (expr)))
8458 case ARRAY_TYPE:
8459 error ("used array that cannot be converted to pointer where scalar is required");
8460 return error_mark_node;
8462 case RECORD_TYPE:
8463 error ("used struct type value where scalar is required");
8464 return error_mark_node;
8466 case UNION_TYPE:
8467 error ("used union type value where scalar is required");
8468 return error_mark_node;
8470 case FUNCTION_TYPE:
8471 gcc_unreachable ();
8473 default:
8474 break;
8477 /* ??? Should we also give an error for void and vectors rather than
8478 leaving those to give errors later? */
8479 return c_common_truthvalue_conversion (expr);
8483 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8484 required. */
8486 tree
8487 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8488 bool *ti ATTRIBUTE_UNUSED, bool *se)
8490 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8492 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8493 /* Executing a compound literal inside a function reinitializes
8494 it. */
8495 if (!TREE_STATIC (decl))
8496 *se = true;
8497 return decl;
8499 else
8500 return expr;
8503 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8505 tree
8506 c_begin_omp_parallel (void)
8508 tree block;
8510 keep_next_level ();
8511 block = c_begin_compound_stmt (true);
8513 return block;
8516 tree
8517 c_finish_omp_parallel (tree clauses, tree block)
8519 tree stmt;
8521 block = c_end_compound_stmt (block, true);
8523 stmt = make_node (OMP_PARALLEL);
8524 TREE_TYPE (stmt) = void_type_node;
8525 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8526 OMP_PARALLEL_BODY (stmt) = block;
8528 return add_stmt (stmt);
8531 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
8532 Remove any elements from the list that are invalid. */
8534 tree
8535 c_finish_omp_clauses (tree clauses)
8537 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8538 tree c, t, *pc = &clauses;
8539 const char *name;
8541 bitmap_obstack_initialize (NULL);
8542 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8543 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8544 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8546 for (pc = &clauses, c = clauses; c ; c = *pc)
8548 bool remove = false;
8549 bool need_complete = false;
8550 bool need_implicitly_determined = false;
8552 switch (OMP_CLAUSE_CODE (c))
8554 case OMP_CLAUSE_SHARED:
8555 name = "shared";
8556 need_implicitly_determined = true;
8557 goto check_dup_generic;
8559 case OMP_CLAUSE_PRIVATE:
8560 name = "private";
8561 need_complete = true;
8562 need_implicitly_determined = true;
8563 goto check_dup_generic;
8565 case OMP_CLAUSE_REDUCTION:
8566 name = "reduction";
8567 need_implicitly_determined = true;
8568 t = OMP_CLAUSE_DECL (c);
8569 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8570 || POINTER_TYPE_P (TREE_TYPE (t)))
8572 error ("%qE has invalid type for %<reduction%>", t);
8573 remove = true;
8575 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8577 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8578 const char *r_name = NULL;
8580 switch (r_code)
8582 case PLUS_EXPR:
8583 case MULT_EXPR:
8584 case MINUS_EXPR:
8585 break;
8586 case BIT_AND_EXPR:
8587 r_name = "&";
8588 break;
8589 case BIT_XOR_EXPR:
8590 r_name = "^";
8591 break;
8592 case BIT_IOR_EXPR:
8593 r_name = "|";
8594 break;
8595 case TRUTH_ANDIF_EXPR:
8596 r_name = "&&";
8597 break;
8598 case TRUTH_ORIF_EXPR:
8599 r_name = "||";
8600 break;
8601 default:
8602 gcc_unreachable ();
8604 if (r_name)
8606 error ("%qE has invalid type for %<reduction(%s)%>",
8607 t, r_name);
8608 remove = true;
8611 goto check_dup_generic;
8613 case OMP_CLAUSE_COPYPRIVATE:
8614 name = "copyprivate";
8615 goto check_dup_generic;
8617 case OMP_CLAUSE_COPYIN:
8618 name = "copyin";
8619 t = OMP_CLAUSE_DECL (c);
8620 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8622 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8623 remove = true;
8625 goto check_dup_generic;
8627 check_dup_generic:
8628 t = OMP_CLAUSE_DECL (c);
8629 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8631 error ("%qE is not a variable in clause %qs", t, name);
8632 remove = true;
8634 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8635 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8636 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8638 error ("%qE appears more than once in data clauses", t);
8639 remove = true;
8641 else
8642 bitmap_set_bit (&generic_head, DECL_UID (t));
8643 break;
8645 case OMP_CLAUSE_FIRSTPRIVATE:
8646 name = "firstprivate";
8647 t = OMP_CLAUSE_DECL (c);
8648 need_complete = true;
8649 need_implicitly_determined = true;
8650 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8652 error ("%qE is not a variable in clause %<firstprivate%>", t);
8653 remove = true;
8655 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8656 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8658 error ("%qE appears more than once in data clauses", t);
8659 remove = true;
8661 else
8662 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8663 break;
8665 case OMP_CLAUSE_LASTPRIVATE:
8666 name = "lastprivate";
8667 t = OMP_CLAUSE_DECL (c);
8668 need_complete = true;
8669 need_implicitly_determined = true;
8670 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8672 error ("%qE is not a variable in clause %<lastprivate%>", t);
8673 remove = true;
8675 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8676 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8678 error ("%qE appears more than once in data clauses", t);
8679 remove = true;
8681 else
8682 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8683 break;
8685 case OMP_CLAUSE_IF:
8686 case OMP_CLAUSE_NUM_THREADS:
8687 case OMP_CLAUSE_SCHEDULE:
8688 case OMP_CLAUSE_NOWAIT:
8689 case OMP_CLAUSE_ORDERED:
8690 case OMP_CLAUSE_DEFAULT:
8691 pc = &OMP_CLAUSE_CHAIN (c);
8692 continue;
8694 default:
8695 gcc_unreachable ();
8698 if (!remove)
8700 t = OMP_CLAUSE_DECL (c);
8702 if (need_complete)
8704 t = require_complete_type (t);
8705 if (t == error_mark_node)
8706 remove = true;
8709 if (need_implicitly_determined)
8711 const char *share_name = NULL;
8713 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8714 share_name = "threadprivate";
8715 else switch (c_omp_predetermined_sharing (t))
8717 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8718 break;
8719 case OMP_CLAUSE_DEFAULT_SHARED:
8720 share_name = "shared";
8721 break;
8722 case OMP_CLAUSE_DEFAULT_PRIVATE:
8723 share_name = "private";
8724 break;
8725 default:
8726 gcc_unreachable ();
8728 if (share_name)
8730 error ("%qE is predetermined %qs for %qs",
8731 t, share_name, name);
8732 remove = true;
8737 if (remove)
8738 *pc = OMP_CLAUSE_CHAIN (c);
8739 else
8740 pc = &OMP_CLAUSE_CHAIN (c);
8743 bitmap_obstack_release (NULL);
8744 return clauses;