2008-09-24 Michael J. Eager <eager@eagercon.com>
[official-gcc.git] / gcc / c-typeck.c
blob14244051f17ddf168377a0231ea83976ef7b18f0
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree);
80 static int comp_target_types (tree, tree);
81 static int function_types_compatible_p (const_tree, const_tree);
82 static int type_lists_compatible_p (const_tree, const_tree);
83 static tree decl_constant_value_for_broken_optimization (tree);
84 static tree lookup_field (tree, tree);
85 static int convert_arguments (int, tree *, tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (int, const char *);
95 static tree digest_init (tree, tree, bool, int);
96 static void output_init_element (tree, bool, tree, tree, int);
97 static void output_pending_init_elements (int);
98 static int set_designator (int);
99 static void push_range_stack (tree);
100 static void add_pending_init (tree, tree);
101 static void set_nonincremental_init (void);
102 static void set_nonincremental_init_from_string (tree);
103 static tree find_init_member (tree);
104 static void readonly_error (tree, enum lvalue_use);
105 static int lvalue_or_else (const_tree, enum lvalue_use);
106 static int lvalue_p (const_tree);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (const_tree, const_tree);
110 /* Return true if EXP is a null pointer constant, false otherwise. */
112 static bool
113 null_pointer_constant_p (const_tree expr)
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
119 && !TREE_OVERFLOW (expr)
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
126 \f/* This is a cache to hold if two types are compatible or not. */
128 struct tagged_tu_seen_cache {
129 const struct tagged_tu_seen_cache * next;
130 const_tree t1;
131 const_tree t2;
132 /* The return value of tagged_types_tu_compatible_p if we had seen
133 these two types already. */
134 int val;
137 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
138 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
140 /* Do `exp = require_complete_type (exp);' to make sure exp
141 does not have an incomplete type. (That includes void types.) */
143 tree
144 require_complete_type (tree value)
146 tree type = TREE_TYPE (value);
148 if (value == error_mark_node || type == error_mark_node)
149 return error_mark_node;
151 /* First, detect a valid value with a complete type. */
152 if (COMPLETE_TYPE_P (type))
153 return value;
155 c_incomplete_type_error (value, type);
156 return error_mark_node;
159 /* Print an error message for invalid use of an incomplete type.
160 VALUE is the expression that was used (or 0 if that isn't known)
161 and TYPE is the type that was invalid. */
163 void
164 c_incomplete_type_error (const_tree value, const_tree type)
166 const char *type_code_string;
168 /* Avoid duplicate error message. */
169 if (TREE_CODE (type) == ERROR_MARK)
170 return;
172 if (value != 0 && (TREE_CODE (value) == VAR_DECL
173 || TREE_CODE (value) == PARM_DECL))
174 error ("%qD has an incomplete type", value);
175 else
177 retry:
178 /* We must print an error message. Be clever about what it says. */
180 switch (TREE_CODE (type))
182 case RECORD_TYPE:
183 type_code_string = "struct";
184 break;
186 case UNION_TYPE:
187 type_code_string = "union";
188 break;
190 case ENUMERAL_TYPE:
191 type_code_string = "enum";
192 break;
194 case VOID_TYPE:
195 error ("invalid use of void expression");
196 return;
198 case ARRAY_TYPE:
199 if (TYPE_DOMAIN (type))
201 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
203 error ("invalid use of flexible array member");
204 return;
206 type = TREE_TYPE (type);
207 goto retry;
209 error ("invalid use of array with unspecified bounds");
210 return;
212 default:
213 gcc_unreachable ();
216 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
217 error ("invalid use of undefined type %<%s %E%>",
218 type_code_string, TYPE_NAME (type));
219 else
220 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
221 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
225 /* Given a type, apply default promotions wrt unnamed function
226 arguments and return the new type. */
228 tree
229 c_type_promotes_to (tree type)
231 if (TYPE_MAIN_VARIANT (type) == float_type_node)
232 return double_type_node;
234 if (c_promoting_integer_type_p (type))
236 /* Preserve unsignedness if not really getting any wider. */
237 if (TYPE_UNSIGNED (type)
238 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
239 return unsigned_type_node;
240 return integer_type_node;
243 return type;
246 /* Return a variant of TYPE which has all the type qualifiers of LIKE
247 as well as those of TYPE. */
249 static tree
250 qualify_type (tree type, tree like)
252 return c_build_qualified_type (type,
253 TYPE_QUALS (type) | TYPE_QUALS (like));
256 /* Return true iff the given tree T is a variable length array. */
258 bool
259 c_vla_type_p (const_tree t)
261 if (TREE_CODE (t) == ARRAY_TYPE
262 && C_TYPE_VARIABLE_SIZE (t))
263 return true;
264 return false;
267 /* Return the composite type of two compatible types.
269 We assume that comptypes has already been done and returned
270 nonzero; if that isn't so, this may crash. In particular, we
271 assume that qualifiers match. */
273 tree
274 composite_type (tree t1, tree t2)
276 enum tree_code code1;
277 enum tree_code code2;
278 tree attributes;
280 /* Save time if the two types are the same. */
282 if (t1 == t2) return t1;
284 /* If one type is nonsense, use the other. */
285 if (t1 == error_mark_node)
286 return t2;
287 if (t2 == error_mark_node)
288 return t1;
290 code1 = TREE_CODE (t1);
291 code2 = TREE_CODE (t2);
293 /* Merge the attributes. */
294 attributes = targetm.merge_type_attributes (t1, t2);
296 /* If one is an enumerated type and the other is the compatible
297 integer type, the composite type might be either of the two
298 (DR#013 question 3). For consistency, use the enumerated type as
299 the composite type. */
301 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
302 return t1;
303 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
304 return t2;
306 gcc_assert (code1 == code2);
308 switch (code1)
310 case POINTER_TYPE:
311 /* For two pointers, do this recursively on the target type. */
313 tree pointed_to_1 = TREE_TYPE (t1);
314 tree pointed_to_2 = TREE_TYPE (t2);
315 tree target = composite_type (pointed_to_1, pointed_to_2);
316 t1 = build_pointer_type (target);
317 t1 = build_type_attribute_variant (t1, attributes);
318 return qualify_type (t1, t2);
321 case ARRAY_TYPE:
323 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
324 int quals;
325 tree unqual_elt;
326 tree d1 = TYPE_DOMAIN (t1);
327 tree d2 = TYPE_DOMAIN (t2);
328 bool d1_variable, d2_variable;
329 bool d1_zero, d2_zero;
331 /* We should not have any type quals on arrays at all. */
332 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
334 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
335 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
337 d1_variable = (!d1_zero
338 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
339 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
340 d2_variable = (!d2_zero
341 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
342 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
343 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
344 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
346 /* Save space: see if the result is identical to one of the args. */
347 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
348 && (d2_variable || d2_zero || !d1_variable))
349 return build_type_attribute_variant (t1, attributes);
350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
351 && (d1_variable || d1_zero || !d2_variable))
352 return build_type_attribute_variant (t2, attributes);
354 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
355 return build_type_attribute_variant (t1, attributes);
356 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Merge the element types, and have a size if either arg has
360 one. We may have qualifiers on the element types. To set
361 up TYPE_MAIN_VARIANT correctly, we need to form the
362 composite of the unqualified types and add the qualifiers
363 back at the end. */
364 quals = TYPE_QUALS (strip_array_types (elt));
365 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
366 t1 = build_array_type (unqual_elt,
367 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
368 && (d2_variable
369 || d2_zero
370 || !d1_variable))
371 ? t1
372 : t2));
373 t1 = c_build_qualified_type (t1, quals);
374 return build_type_attribute_variant (t1, attributes);
377 case ENUMERAL_TYPE:
378 case RECORD_TYPE:
379 case UNION_TYPE:
380 if (attributes != NULL)
382 /* Try harder not to create a new aggregate type. */
383 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
384 return t1;
385 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
386 return t2;
388 return build_type_attribute_variant (t1, attributes);
390 case FUNCTION_TYPE:
391 /* Function types: prefer the one that specified arg types.
392 If both do, merge the arg types. Also merge the return types. */
394 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
395 tree p1 = TYPE_ARG_TYPES (t1);
396 tree p2 = TYPE_ARG_TYPES (t2);
397 int len;
398 tree newargs, n;
399 int i;
401 /* Save space: see if the result is identical to one of the args. */
402 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
403 return build_type_attribute_variant (t1, attributes);
404 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
405 return build_type_attribute_variant (t2, attributes);
407 /* Simple way if one arg fails to specify argument types. */
408 if (TYPE_ARG_TYPES (t1) == 0)
410 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
414 if (TYPE_ARG_TYPES (t2) == 0)
416 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
417 t1 = build_type_attribute_variant (t1, attributes);
418 return qualify_type (t1, t2);
421 /* If both args specify argument types, we must merge the two
422 lists, argument by argument. */
423 /* Tell global_bindings_p to return false so that variable_size
424 doesn't die on VLAs in parameter types. */
425 c_override_global_bindings_to_false = true;
427 len = list_length (p1);
428 newargs = 0;
430 for (i = 0; i < len; i++)
431 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
433 n = newargs;
435 for (; p1;
436 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
438 /* A null type means arg type is not specified.
439 Take whatever the other function type has. */
440 if (TREE_VALUE (p1) == 0)
442 TREE_VALUE (n) = TREE_VALUE (p2);
443 goto parm_done;
445 if (TREE_VALUE (p2) == 0)
447 TREE_VALUE (n) = TREE_VALUE (p1);
448 goto parm_done;
451 /* Given wait (union {union wait *u; int *i} *)
452 and wait (union wait *),
453 prefer union wait * as type of parm. */
454 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
455 && TREE_VALUE (p1) != TREE_VALUE (p2))
457 tree memb;
458 tree mv2 = TREE_VALUE (p2);
459 if (mv2 && mv2 != error_mark_node
460 && TREE_CODE (mv2) != ARRAY_TYPE)
461 mv2 = TYPE_MAIN_VARIANT (mv2);
462 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
463 memb; memb = TREE_CHAIN (memb))
465 tree mv3 = TREE_TYPE (memb);
466 if (mv3 && mv3 != error_mark_node
467 && TREE_CODE (mv3) != ARRAY_TYPE)
468 mv3 = TYPE_MAIN_VARIANT (mv3);
469 if (comptypes (mv3, mv2))
471 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
472 TREE_VALUE (p2));
473 pedwarn (input_location, OPT_pedantic,
474 "function types not truly compatible in ISO C");
475 goto parm_done;
479 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
480 && TREE_VALUE (p2) != TREE_VALUE (p1))
482 tree memb;
483 tree mv1 = TREE_VALUE (p1);
484 if (mv1 && mv1 != error_mark_node
485 && TREE_CODE (mv1) != ARRAY_TYPE)
486 mv1 = TYPE_MAIN_VARIANT (mv1);
487 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
488 memb; memb = TREE_CHAIN (memb))
490 tree mv3 = TREE_TYPE (memb);
491 if (mv3 && mv3 != error_mark_node
492 && TREE_CODE (mv3) != ARRAY_TYPE)
493 mv3 = TYPE_MAIN_VARIANT (mv3);
494 if (comptypes (mv3, mv1))
496 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
497 TREE_VALUE (p1));
498 pedwarn (input_location, OPT_pedantic,
499 "function types not truly compatible in ISO C");
500 goto parm_done;
504 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
505 parm_done: ;
508 c_override_global_bindings_to_false = false;
509 t1 = build_function_type (valtype, newargs);
510 t1 = qualify_type (t1, t2);
511 /* ... falls through ... */
514 default:
515 return build_type_attribute_variant (t1, attributes);
520 /* Return the type of a conditional expression between pointers to
521 possibly differently qualified versions of compatible types.
523 We assume that comp_target_types has already been done and returned
524 nonzero; if that isn't so, this may crash. */
526 static tree
527 common_pointer_type (tree t1, tree t2)
529 tree attributes;
530 tree pointed_to_1, mv1;
531 tree pointed_to_2, mv2;
532 tree target;
533 unsigned target_quals;
535 /* Save time if the two types are the same. */
537 if (t1 == t2) return t1;
539 /* If one type is nonsense, use the other. */
540 if (t1 == error_mark_node)
541 return t2;
542 if (t2 == error_mark_node)
543 return t1;
545 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
546 && TREE_CODE (t2) == POINTER_TYPE);
548 /* Merge the attributes. */
549 attributes = targetm.merge_type_attributes (t1, t2);
551 /* Find the composite type of the target types, and combine the
552 qualifiers of the two types' targets. Do not lose qualifiers on
553 array element types by taking the TYPE_MAIN_VARIANT. */
554 mv1 = pointed_to_1 = TREE_TYPE (t1);
555 mv2 = pointed_to_2 = TREE_TYPE (t2);
556 if (TREE_CODE (mv1) != ARRAY_TYPE)
557 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
558 if (TREE_CODE (mv2) != ARRAY_TYPE)
559 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
560 target = composite_type (mv1, mv2);
562 /* For function types do not merge const qualifiers, but drop them
563 if used inconsistently. The middle-end uses these to mark const
564 and noreturn functions. */
565 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
566 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
567 else
568 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
569 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
570 return build_type_attribute_variant (t1, attributes);
573 /* Return the common type for two arithmetic types under the usual
574 arithmetic conversions. The default conversions have already been
575 applied, and enumerated types converted to their compatible integer
576 types. The resulting type is unqualified and has no attributes.
578 This is the type for the result of most arithmetic operations
579 if the operands have the given two types. */
581 static tree
582 c_common_type (tree t1, tree t2)
584 enum tree_code code1;
585 enum tree_code code2;
587 /* If one type is nonsense, use the other. */
588 if (t1 == error_mark_node)
589 return t2;
590 if (t2 == error_mark_node)
591 return t1;
593 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
594 t1 = TYPE_MAIN_VARIANT (t1);
596 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
597 t2 = TYPE_MAIN_VARIANT (t2);
599 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
600 t1 = build_type_attribute_variant (t1, NULL_TREE);
602 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
603 t2 = build_type_attribute_variant (t2, NULL_TREE);
605 /* Save time if the two types are the same. */
607 if (t1 == t2) return t1;
609 code1 = TREE_CODE (t1);
610 code2 = TREE_CODE (t2);
612 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
613 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
614 || code1 == INTEGER_TYPE);
615 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
616 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
617 || code2 == INTEGER_TYPE);
619 /* When one operand is a decimal float type, the other operand cannot be
620 a generic float type or a complex type. We also disallow vector types
621 here. */
622 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
623 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
625 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
627 error ("can%'t mix operands of decimal float and vector types");
628 return error_mark_node;
630 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
632 error ("can%'t mix operands of decimal float and complex types");
633 return error_mark_node;
635 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
637 error ("can%'t mix operands of decimal float and other float types");
638 return error_mark_node;
642 /* If one type is a vector type, return that type. (How the usual
643 arithmetic conversions apply to the vector types extension is not
644 precisely specified.) */
645 if (code1 == VECTOR_TYPE)
646 return t1;
648 if (code2 == VECTOR_TYPE)
649 return t2;
651 /* If one type is complex, form the common type of the non-complex
652 components, then make that complex. Use T1 or T2 if it is the
653 required type. */
654 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
656 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
657 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
658 tree subtype = c_common_type (subtype1, subtype2);
660 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
661 return t1;
662 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
663 return t2;
664 else
665 return build_complex_type (subtype);
668 /* If only one is real, use it as the result. */
670 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
671 return t1;
673 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
674 return t2;
676 /* If both are real and either are decimal floating point types, use
677 the decimal floating point type with the greater precision. */
679 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
681 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
682 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
683 return dfloat128_type_node;
684 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
685 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
686 return dfloat64_type_node;
687 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
688 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
689 return dfloat32_type_node;
692 /* Deal with fixed-point types. */
693 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
695 unsigned int unsignedp = 0, satp = 0;
696 enum machine_mode m1, m2;
697 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
699 m1 = TYPE_MODE (t1);
700 m2 = TYPE_MODE (t2);
702 /* If one input type is saturating, the result type is saturating. */
703 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
704 satp = 1;
706 /* If both fixed-point types are unsigned, the result type is unsigned.
707 When mixing fixed-point and integer types, follow the sign of the
708 fixed-point type.
709 Otherwise, the result type is signed. */
710 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
711 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
712 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
713 && TYPE_UNSIGNED (t1))
714 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
715 && TYPE_UNSIGNED (t2)))
716 unsignedp = 1;
718 /* The result type is signed. */
719 if (unsignedp == 0)
721 /* If the input type is unsigned, we need to convert to the
722 signed type. */
723 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
725 enum mode_class mclass = (enum mode_class) 0;
726 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
727 mclass = MODE_FRACT;
728 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
729 mclass = MODE_ACCUM;
730 else
731 gcc_unreachable ();
732 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
734 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
736 enum mode_class mclass = (enum mode_class) 0;
737 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
738 mclass = MODE_FRACT;
739 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
740 mclass = MODE_ACCUM;
741 else
742 gcc_unreachable ();
743 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
747 if (code1 == FIXED_POINT_TYPE)
749 fbit1 = GET_MODE_FBIT (m1);
750 ibit1 = GET_MODE_IBIT (m1);
752 else
754 fbit1 = 0;
755 /* Signed integers need to subtract one sign bit. */
756 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
759 if (code2 == FIXED_POINT_TYPE)
761 fbit2 = GET_MODE_FBIT (m2);
762 ibit2 = GET_MODE_IBIT (m2);
764 else
766 fbit2 = 0;
767 /* Signed integers need to subtract one sign bit. */
768 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
771 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
772 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
773 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
774 satp);
777 /* Both real or both integers; use the one with greater precision. */
779 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
780 return t1;
781 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
782 return t2;
784 /* Same precision. Prefer long longs to longs to ints when the
785 same precision, following the C99 rules on integer type rank
786 (which are equivalent to the C90 rules for C90 types). */
788 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
789 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
790 return long_long_unsigned_type_node;
792 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
793 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
795 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
796 return long_long_unsigned_type_node;
797 else
798 return long_long_integer_type_node;
801 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
802 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
803 return long_unsigned_type_node;
805 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
806 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
808 /* But preserve unsignedness from the other type,
809 since long cannot hold all the values of an unsigned int. */
810 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
811 return long_unsigned_type_node;
812 else
813 return long_integer_type_node;
816 /* Likewise, prefer long double to double even if same size. */
817 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
818 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
819 return long_double_type_node;
821 /* Otherwise prefer the unsigned one. */
823 if (TYPE_UNSIGNED (t1))
824 return t1;
825 else
826 return t2;
829 /* Wrapper around c_common_type that is used by c-common.c and other
830 front end optimizations that remove promotions. ENUMERAL_TYPEs
831 are allowed here and are converted to their compatible integer types.
832 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
833 preferably a non-Boolean type as the common type. */
834 tree
835 common_type (tree t1, tree t2)
837 if (TREE_CODE (t1) == ENUMERAL_TYPE)
838 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
839 if (TREE_CODE (t2) == ENUMERAL_TYPE)
840 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
842 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
843 if (TREE_CODE (t1) == BOOLEAN_TYPE
844 && TREE_CODE (t2) == BOOLEAN_TYPE)
845 return boolean_type_node;
847 /* If either type is BOOLEAN_TYPE, then return the other. */
848 if (TREE_CODE (t1) == BOOLEAN_TYPE)
849 return t2;
850 if (TREE_CODE (t2) == BOOLEAN_TYPE)
851 return t1;
853 return c_common_type (t1, t2);
856 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
857 or various other operations. Return 2 if they are compatible
858 but a warning may be needed if you use them together. */
861 comptypes (tree type1, tree type2)
863 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
864 int val;
866 val = comptypes_internal (type1, type2);
867 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
869 return val;
872 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
873 or various other operations. Return 2 if they are compatible
874 but a warning may be needed if you use them together. This
875 differs from comptypes, in that we don't free the seen types. */
877 static int
878 comptypes_internal (const_tree type1, const_tree type2)
880 const_tree t1 = type1;
881 const_tree t2 = type2;
882 int attrval, val;
884 /* Suppress errors caused by previously reported errors. */
886 if (t1 == t2 || !t1 || !t2
887 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
888 return 1;
890 /* If either type is the internal version of sizetype, return the
891 language version. */
892 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
893 && TYPE_ORIG_SIZE_TYPE (t1))
894 t1 = TYPE_ORIG_SIZE_TYPE (t1);
896 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
897 && TYPE_ORIG_SIZE_TYPE (t2))
898 t2 = TYPE_ORIG_SIZE_TYPE (t2);
901 /* Enumerated types are compatible with integer types, but this is
902 not transitive: two enumerated types in the same translation unit
903 are compatible with each other only if they are the same type. */
905 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
906 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
907 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
908 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
910 if (t1 == t2)
911 return 1;
913 /* Different classes of types can't be compatible. */
915 if (TREE_CODE (t1) != TREE_CODE (t2))
916 return 0;
918 /* Qualifiers must match. C99 6.7.3p9 */
920 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
921 return 0;
923 /* Allow for two different type nodes which have essentially the same
924 definition. Note that we already checked for equality of the type
925 qualifiers (just above). */
927 if (TREE_CODE (t1) != ARRAY_TYPE
928 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
929 return 1;
931 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
932 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
933 return 0;
935 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
936 val = 0;
938 switch (TREE_CODE (t1))
940 case POINTER_TYPE:
941 /* Do not remove mode or aliasing information. */
942 if (TYPE_MODE (t1) != TYPE_MODE (t2)
943 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
944 break;
945 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
946 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
947 break;
949 case FUNCTION_TYPE:
950 val = function_types_compatible_p (t1, t2);
951 break;
953 case ARRAY_TYPE:
955 tree d1 = TYPE_DOMAIN (t1);
956 tree d2 = TYPE_DOMAIN (t2);
957 bool d1_variable, d2_variable;
958 bool d1_zero, d2_zero;
959 val = 1;
961 /* Target types must match incl. qualifiers. */
962 if (TREE_TYPE (t1) != TREE_TYPE (t2)
963 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
964 return 0;
966 /* Sizes must match unless one is missing or variable. */
967 if (d1 == 0 || d2 == 0 || d1 == d2)
968 break;
970 d1_zero = !TYPE_MAX_VALUE (d1);
971 d2_zero = !TYPE_MAX_VALUE (d2);
973 d1_variable = (!d1_zero
974 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
975 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
976 d2_variable = (!d2_zero
977 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
978 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
979 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
980 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
982 if (d1_variable || d2_variable)
983 break;
984 if (d1_zero && d2_zero)
985 break;
986 if (d1_zero || d2_zero
987 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
988 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
989 val = 0;
991 break;
994 case ENUMERAL_TYPE:
995 case RECORD_TYPE:
996 case UNION_TYPE:
997 if (val != 1 && !same_translation_unit_p (t1, t2))
999 tree a1 = TYPE_ATTRIBUTES (t1);
1000 tree a2 = TYPE_ATTRIBUTES (t2);
1002 if (! attribute_list_contained (a1, a2)
1003 && ! attribute_list_contained (a2, a1))
1004 break;
1006 if (attrval != 2)
1007 return tagged_types_tu_compatible_p (t1, t2);
1008 val = tagged_types_tu_compatible_p (t1, t2);
1010 break;
1012 case VECTOR_TYPE:
1013 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1014 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
1015 break;
1017 default:
1018 break;
1020 return attrval == 2 && val == 1 ? 2 : val;
1023 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1024 ignoring their qualifiers. */
1026 static int
1027 comp_target_types (tree ttl, tree ttr)
1029 int val;
1030 tree mvl, mvr;
1032 /* Do not lose qualifiers on element types of array types that are
1033 pointer targets by taking their TYPE_MAIN_VARIANT. */
1034 mvl = TREE_TYPE (ttl);
1035 mvr = TREE_TYPE (ttr);
1036 if (TREE_CODE (mvl) != ARRAY_TYPE)
1037 mvl = TYPE_MAIN_VARIANT (mvl);
1038 if (TREE_CODE (mvr) != ARRAY_TYPE)
1039 mvr = TYPE_MAIN_VARIANT (mvr);
1040 val = comptypes (mvl, mvr);
1042 if (val == 2)
1043 pedwarn (input_location, OPT_pedantic, "types are not quite compatible");
1044 return val;
1047 /* Subroutines of `comptypes'. */
1049 /* Determine whether two trees derive from the same translation unit.
1050 If the CONTEXT chain ends in a null, that tree's context is still
1051 being parsed, so if two trees have context chains ending in null,
1052 they're in the same translation unit. */
1054 same_translation_unit_p (const_tree t1, const_tree t2)
1056 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1057 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1059 case tcc_declaration:
1060 t1 = DECL_CONTEXT (t1); break;
1061 case tcc_type:
1062 t1 = TYPE_CONTEXT (t1); break;
1063 case tcc_exceptional:
1064 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1065 default: gcc_unreachable ();
1068 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1069 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1071 case tcc_declaration:
1072 t2 = DECL_CONTEXT (t2); break;
1073 case tcc_type:
1074 t2 = TYPE_CONTEXT (t2); break;
1075 case tcc_exceptional:
1076 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1077 default: gcc_unreachable ();
1080 return t1 == t2;
1083 /* Allocate the seen two types, assuming that they are compatible. */
1085 static struct tagged_tu_seen_cache *
1086 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1088 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1089 tu->next = tagged_tu_seen_base;
1090 tu->t1 = t1;
1091 tu->t2 = t2;
1093 tagged_tu_seen_base = tu;
1095 /* The C standard says that two structures in different translation
1096 units are compatible with each other only if the types of their
1097 fields are compatible (among other things). We assume that they
1098 are compatible until proven otherwise when building the cache.
1099 An example where this can occur is:
1100 struct a
1102 struct a *next;
1104 If we are comparing this against a similar struct in another TU,
1105 and did not assume they were compatible, we end up with an infinite
1106 loop. */
1107 tu->val = 1;
1108 return tu;
1111 /* Free the seen types until we get to TU_TIL. */
1113 static void
1114 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1116 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1117 while (tu != tu_til)
1119 const struct tagged_tu_seen_cache *const tu1
1120 = (const struct tagged_tu_seen_cache *) tu;
1121 tu = tu1->next;
1122 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1124 tagged_tu_seen_base = tu_til;
1127 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1128 compatible. If the two types are not the same (which has been
1129 checked earlier), this can only happen when multiple translation
1130 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1131 rules. */
1133 static int
1134 tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
1136 tree s1, s2;
1137 bool needs_warning = false;
1139 /* We have to verify that the tags of the types are the same. This
1140 is harder than it looks because this may be a typedef, so we have
1141 to go look at the original type. It may even be a typedef of a
1142 typedef...
1143 In the case of compiler-created builtin structs the TYPE_DECL
1144 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1145 while (TYPE_NAME (t1)
1146 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1147 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1148 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1150 while (TYPE_NAME (t2)
1151 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1152 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1153 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1155 /* C90 didn't have the requirement that the two tags be the same. */
1156 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1157 return 0;
1159 /* C90 didn't say what happened if one or both of the types were
1160 incomplete; we choose to follow C99 rules here, which is that they
1161 are compatible. */
1162 if (TYPE_SIZE (t1) == NULL
1163 || TYPE_SIZE (t2) == NULL)
1164 return 1;
1167 const struct tagged_tu_seen_cache * tts_i;
1168 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1169 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1170 return tts_i->val;
1173 switch (TREE_CODE (t1))
1175 case ENUMERAL_TYPE:
1177 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1178 /* Speed up the case where the type values are in the same order. */
1179 tree tv1 = TYPE_VALUES (t1);
1180 tree tv2 = TYPE_VALUES (t2);
1182 if (tv1 == tv2)
1184 return 1;
1187 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1189 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1190 break;
1191 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1193 tu->val = 0;
1194 return 0;
1198 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1200 return 1;
1202 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1204 tu->val = 0;
1205 return 0;
1208 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1210 tu->val = 0;
1211 return 0;
1214 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1216 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1217 if (s2 == NULL
1218 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1220 tu->val = 0;
1221 return 0;
1224 return 1;
1227 case UNION_TYPE:
1229 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1230 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1232 tu->val = 0;
1233 return 0;
1236 /* Speed up the common case where the fields are in the same order. */
1237 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1238 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1240 int result;
1242 if (DECL_NAME (s1) != DECL_NAME (s2))
1243 break;
1244 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1246 if (result != 1 && !DECL_NAME (s1))
1247 break;
1248 if (result == 0)
1250 tu->val = 0;
1251 return 0;
1253 if (result == 2)
1254 needs_warning = true;
1256 if (TREE_CODE (s1) == FIELD_DECL
1257 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1258 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1260 tu->val = 0;
1261 return 0;
1264 if (!s1 && !s2)
1266 tu->val = needs_warning ? 2 : 1;
1267 return tu->val;
1270 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1272 bool ok = false;
1274 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1275 if (DECL_NAME (s1) == DECL_NAME (s2))
1277 int result;
1279 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1281 if (result != 1 && !DECL_NAME (s1))
1282 continue;
1283 if (result == 0)
1285 tu->val = 0;
1286 return 0;
1288 if (result == 2)
1289 needs_warning = true;
1291 if (TREE_CODE (s1) == FIELD_DECL
1292 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1293 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1294 break;
1296 ok = true;
1297 break;
1299 if (!ok)
1301 tu->val = 0;
1302 return 0;
1305 tu->val = needs_warning ? 2 : 10;
1306 return tu->val;
1309 case RECORD_TYPE:
1311 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1313 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1314 s1 && s2;
1315 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1317 int result;
1318 if (TREE_CODE (s1) != TREE_CODE (s2)
1319 || DECL_NAME (s1) != DECL_NAME (s2))
1320 break;
1321 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1322 if (result == 0)
1323 break;
1324 if (result == 2)
1325 needs_warning = true;
1327 if (TREE_CODE (s1) == FIELD_DECL
1328 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1329 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1330 break;
1332 if (s1 && s2)
1333 tu->val = 0;
1334 else
1335 tu->val = needs_warning ? 2 : 1;
1336 return tu->val;
1339 default:
1340 gcc_unreachable ();
1344 /* Return 1 if two function types F1 and F2 are compatible.
1345 If either type specifies no argument types,
1346 the other must specify a fixed number of self-promoting arg types.
1347 Otherwise, if one type specifies only the number of arguments,
1348 the other must specify that number of self-promoting arg types.
1349 Otherwise, the argument types must match. */
1351 static int
1352 function_types_compatible_p (const_tree f1, const_tree f2)
1354 tree args1, args2;
1355 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1356 int val = 1;
1357 int val1;
1358 tree ret1, ret2;
1360 ret1 = TREE_TYPE (f1);
1361 ret2 = TREE_TYPE (f2);
1363 /* 'volatile' qualifiers on a function's return type used to mean
1364 the function is noreturn. */
1365 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1366 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1367 if (TYPE_VOLATILE (ret1))
1368 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1369 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1370 if (TYPE_VOLATILE (ret2))
1371 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1372 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1373 val = comptypes_internal (ret1, ret2);
1374 if (val == 0)
1375 return 0;
1377 args1 = TYPE_ARG_TYPES (f1);
1378 args2 = TYPE_ARG_TYPES (f2);
1380 /* An unspecified parmlist matches any specified parmlist
1381 whose argument types don't need default promotions. */
1383 if (args1 == 0)
1385 if (!self_promoting_args_p (args2))
1386 return 0;
1387 /* If one of these types comes from a non-prototype fn definition,
1388 compare that with the other type's arglist.
1389 If they don't match, ask for a warning (but no error). */
1390 if (TYPE_ACTUAL_ARG_TYPES (f1)
1391 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1392 val = 2;
1393 return val;
1395 if (args2 == 0)
1397 if (!self_promoting_args_p (args1))
1398 return 0;
1399 if (TYPE_ACTUAL_ARG_TYPES (f2)
1400 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1401 val = 2;
1402 return val;
1405 /* Both types have argument lists: compare them and propagate results. */
1406 val1 = type_lists_compatible_p (args1, args2);
1407 return val1 != 1 ? val1 : val;
1410 /* Check two lists of types for compatibility,
1411 returning 0 for incompatible, 1 for compatible,
1412 or 2 for compatible with warning. */
1414 static int
1415 type_lists_compatible_p (const_tree args1, const_tree args2)
1417 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1418 int val = 1;
1419 int newval = 0;
1421 while (1)
1423 tree a1, mv1, a2, mv2;
1424 if (args1 == 0 && args2 == 0)
1425 return val;
1426 /* If one list is shorter than the other,
1427 they fail to match. */
1428 if (args1 == 0 || args2 == 0)
1429 return 0;
1430 mv1 = a1 = TREE_VALUE (args1);
1431 mv2 = a2 = TREE_VALUE (args2);
1432 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1433 mv1 = TYPE_MAIN_VARIANT (mv1);
1434 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1435 mv2 = TYPE_MAIN_VARIANT (mv2);
1436 /* A null pointer instead of a type
1437 means there is supposed to be an argument
1438 but nothing is specified about what type it has.
1439 So match anything that self-promotes. */
1440 if (a1 == 0)
1442 if (c_type_promotes_to (a2) != a2)
1443 return 0;
1445 else if (a2 == 0)
1447 if (c_type_promotes_to (a1) != a1)
1448 return 0;
1450 /* If one of the lists has an error marker, ignore this arg. */
1451 else if (TREE_CODE (a1) == ERROR_MARK
1452 || TREE_CODE (a2) == ERROR_MARK)
1454 else if (!(newval = comptypes_internal (mv1, mv2)))
1456 /* Allow wait (union {union wait *u; int *i} *)
1457 and wait (union wait *) to be compatible. */
1458 if (TREE_CODE (a1) == UNION_TYPE
1459 && (TYPE_NAME (a1) == 0
1460 || TYPE_TRANSPARENT_UNION (a1))
1461 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1462 && tree_int_cst_equal (TYPE_SIZE (a1),
1463 TYPE_SIZE (a2)))
1465 tree memb;
1466 for (memb = TYPE_FIELDS (a1);
1467 memb; memb = TREE_CHAIN (memb))
1469 tree mv3 = TREE_TYPE (memb);
1470 if (mv3 && mv3 != error_mark_node
1471 && TREE_CODE (mv3) != ARRAY_TYPE)
1472 mv3 = TYPE_MAIN_VARIANT (mv3);
1473 if (comptypes_internal (mv3, mv2))
1474 break;
1476 if (memb == 0)
1477 return 0;
1479 else if (TREE_CODE (a2) == UNION_TYPE
1480 && (TYPE_NAME (a2) == 0
1481 || TYPE_TRANSPARENT_UNION (a2))
1482 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1483 && tree_int_cst_equal (TYPE_SIZE (a2),
1484 TYPE_SIZE (a1)))
1486 tree memb;
1487 for (memb = TYPE_FIELDS (a2);
1488 memb; memb = TREE_CHAIN (memb))
1490 tree mv3 = TREE_TYPE (memb);
1491 if (mv3 && mv3 != error_mark_node
1492 && TREE_CODE (mv3) != ARRAY_TYPE)
1493 mv3 = TYPE_MAIN_VARIANT (mv3);
1494 if (comptypes_internal (mv3, mv1))
1495 break;
1497 if (memb == 0)
1498 return 0;
1500 else
1501 return 0;
1504 /* comptypes said ok, but record if it said to warn. */
1505 if (newval > val)
1506 val = newval;
1508 args1 = TREE_CHAIN (args1);
1509 args2 = TREE_CHAIN (args2);
1513 /* Compute the size to increment a pointer by. */
1515 static tree
1516 c_size_in_bytes (const_tree type)
1518 enum tree_code code = TREE_CODE (type);
1520 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1521 return size_one_node;
1523 if (!COMPLETE_OR_VOID_TYPE_P (type))
1525 error ("arithmetic on pointer to an incomplete type");
1526 return size_one_node;
1529 /* Convert in case a char is more than one unit. */
1530 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1531 size_int (TYPE_PRECISION (char_type_node)
1532 / BITS_PER_UNIT));
1535 /* Return either DECL or its known constant value (if it has one). */
1537 tree
1538 decl_constant_value (tree decl)
1540 if (/* Don't change a variable array bound or initial value to a constant
1541 in a place where a variable is invalid. Note that DECL_INITIAL
1542 isn't valid for a PARM_DECL. */
1543 current_function_decl != 0
1544 && TREE_CODE (decl) != PARM_DECL
1545 && !TREE_THIS_VOLATILE (decl)
1546 && TREE_READONLY (decl)
1547 && DECL_INITIAL (decl) != 0
1548 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1549 /* This is invalid if initial value is not constant.
1550 If it has either a function call, a memory reference,
1551 or a variable, then re-evaluating it could give different results. */
1552 && TREE_CONSTANT (DECL_INITIAL (decl))
1553 /* Check for cases where this is sub-optimal, even though valid. */
1554 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1555 return DECL_INITIAL (decl);
1556 return decl;
1559 /* Return either DECL or its known constant value (if it has one), but
1560 return DECL if pedantic or DECL has mode BLKmode. This is for
1561 bug-compatibility with the old behavior of decl_constant_value
1562 (before GCC 3.0); every use of this function is a bug and it should
1563 be removed before GCC 3.1. It is not appropriate to use pedantic
1564 in a way that affects optimization, and BLKmode is probably not the
1565 right test for avoiding misoptimizations either. */
1567 static tree
1568 decl_constant_value_for_broken_optimization (tree decl)
1570 tree ret;
1572 if (pedantic || DECL_MODE (decl) == BLKmode)
1573 return decl;
1575 ret = decl_constant_value (decl);
1576 /* Avoid unwanted tree sharing between the initializer and current
1577 function's body where the tree can be modified e.g. by the
1578 gimplifier. */
1579 if (ret != decl && TREE_STATIC (decl))
1580 ret = unshare_expr (ret);
1581 return ret;
1584 /* Convert the array expression EXP to a pointer. */
1585 static tree
1586 array_to_pointer_conversion (tree exp)
1588 tree orig_exp = exp;
1589 tree type = TREE_TYPE (exp);
1590 tree adr;
1591 tree restype = TREE_TYPE (type);
1592 tree ptrtype;
1594 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1596 STRIP_TYPE_NOPS (exp);
1598 if (TREE_NO_WARNING (orig_exp))
1599 TREE_NO_WARNING (exp) = 1;
1601 ptrtype = build_pointer_type (restype);
1603 if (TREE_CODE (exp) == INDIRECT_REF)
1604 return convert (ptrtype, TREE_OPERAND (exp, 0));
1606 if (TREE_CODE (exp) == VAR_DECL)
1608 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1609 ADDR_EXPR because it's the best way of representing what
1610 happens in C when we take the address of an array and place
1611 it in a pointer to the element type. */
1612 adr = build1 (ADDR_EXPR, ptrtype, exp);
1613 if (!c_mark_addressable (exp))
1614 return error_mark_node;
1615 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1616 return adr;
1619 /* This way is better for a COMPONENT_REF since it can
1620 simplify the offset for a component. */
1621 adr = build_unary_op (ADDR_EXPR, exp, 1);
1622 return convert (ptrtype, adr);
1625 /* Convert the function expression EXP to a pointer. */
1626 static tree
1627 function_to_pointer_conversion (tree exp)
1629 tree orig_exp = exp;
1631 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1633 STRIP_TYPE_NOPS (exp);
1635 if (TREE_NO_WARNING (orig_exp))
1636 TREE_NO_WARNING (exp) = 1;
1638 return build_unary_op (ADDR_EXPR, exp, 0);
1641 /* Perform the default conversion of arrays and functions to pointers.
1642 Return the result of converting EXP. For any other expression, just
1643 return EXP after removing NOPs. */
1645 struct c_expr
1646 default_function_array_conversion (struct c_expr exp)
1648 tree orig_exp = exp.value;
1649 tree type = TREE_TYPE (exp.value);
1650 enum tree_code code = TREE_CODE (type);
1652 switch (code)
1654 case ARRAY_TYPE:
1656 bool not_lvalue = false;
1657 bool lvalue_array_p;
1659 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1660 || CONVERT_EXPR_P (exp.value))
1661 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1663 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1664 not_lvalue = true;
1665 exp.value = TREE_OPERAND (exp.value, 0);
1668 if (TREE_NO_WARNING (orig_exp))
1669 TREE_NO_WARNING (exp.value) = 1;
1671 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1672 if (!flag_isoc99 && !lvalue_array_p)
1674 /* Before C99, non-lvalue arrays do not decay to pointers.
1675 Normally, using such an array would be invalid; but it can
1676 be used correctly inside sizeof or as a statement expression.
1677 Thus, do not give an error here; an error will result later. */
1678 return exp;
1681 exp.value = array_to_pointer_conversion (exp.value);
1683 break;
1684 case FUNCTION_TYPE:
1685 exp.value = function_to_pointer_conversion (exp.value);
1686 break;
1687 default:
1688 STRIP_TYPE_NOPS (exp.value);
1689 if (TREE_NO_WARNING (orig_exp))
1690 TREE_NO_WARNING (exp.value) = 1;
1691 break;
1694 return exp;
1698 /* EXP is an expression of integer type. Apply the integer promotions
1699 to it and return the promoted value. */
1701 tree
1702 perform_integral_promotions (tree exp)
1704 tree type = TREE_TYPE (exp);
1705 enum tree_code code = TREE_CODE (type);
1707 gcc_assert (INTEGRAL_TYPE_P (type));
1709 /* Normally convert enums to int,
1710 but convert wide enums to something wider. */
1711 if (code == ENUMERAL_TYPE)
1713 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1714 TYPE_PRECISION (integer_type_node)),
1715 ((TYPE_PRECISION (type)
1716 >= TYPE_PRECISION (integer_type_node))
1717 && TYPE_UNSIGNED (type)));
1719 return convert (type, exp);
1722 /* ??? This should no longer be needed now bit-fields have their
1723 proper types. */
1724 if (TREE_CODE (exp) == COMPONENT_REF
1725 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1726 /* If it's thinner than an int, promote it like a
1727 c_promoting_integer_type_p, otherwise leave it alone. */
1728 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1729 TYPE_PRECISION (integer_type_node)))
1730 return convert (integer_type_node, exp);
1732 if (c_promoting_integer_type_p (type))
1734 /* Preserve unsignedness if not really getting any wider. */
1735 if (TYPE_UNSIGNED (type)
1736 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1737 return convert (unsigned_type_node, exp);
1739 return convert (integer_type_node, exp);
1742 return exp;
1746 /* Perform default promotions for C data used in expressions.
1747 Enumeral types or short or char are converted to int.
1748 In addition, manifest constants symbols are replaced by their values. */
1750 tree
1751 default_conversion (tree exp)
1753 tree orig_exp;
1754 tree type = TREE_TYPE (exp);
1755 enum tree_code code = TREE_CODE (type);
1757 /* Functions and arrays have been converted during parsing. */
1758 gcc_assert (code != FUNCTION_TYPE);
1759 if (code == ARRAY_TYPE)
1760 return exp;
1762 /* Constants can be used directly unless they're not loadable. */
1763 if (TREE_CODE (exp) == CONST_DECL)
1764 exp = DECL_INITIAL (exp);
1766 /* Replace a nonvolatile const static variable with its value unless
1767 it is an array, in which case we must be sure that taking the
1768 address of the array produces consistent results. */
1769 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1771 exp = decl_constant_value_for_broken_optimization (exp);
1772 type = TREE_TYPE (exp);
1775 /* Strip no-op conversions. */
1776 orig_exp = exp;
1777 STRIP_TYPE_NOPS (exp);
1779 if (TREE_NO_WARNING (orig_exp))
1780 TREE_NO_WARNING (exp) = 1;
1782 if (code == VOID_TYPE)
1784 error ("void value not ignored as it ought to be");
1785 return error_mark_node;
1788 exp = require_complete_type (exp);
1789 if (exp == error_mark_node)
1790 return error_mark_node;
1792 if (INTEGRAL_TYPE_P (type))
1793 return perform_integral_promotions (exp);
1795 return exp;
1798 /* Look up COMPONENT in a structure or union DECL.
1800 If the component name is not found, returns NULL_TREE. Otherwise,
1801 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1802 stepping down the chain to the component, which is in the last
1803 TREE_VALUE of the list. Normally the list is of length one, but if
1804 the component is embedded within (nested) anonymous structures or
1805 unions, the list steps down the chain to the component. */
1807 static tree
1808 lookup_field (tree decl, tree component)
1810 tree type = TREE_TYPE (decl);
1811 tree field;
1813 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1814 to the field elements. Use a binary search on this array to quickly
1815 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1816 will always be set for structures which have many elements. */
1818 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1820 int bot, top, half;
1821 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1823 field = TYPE_FIELDS (type);
1824 bot = 0;
1825 top = TYPE_LANG_SPECIFIC (type)->s->len;
1826 while (top - bot > 1)
1828 half = (top - bot + 1) >> 1;
1829 field = field_array[bot+half];
1831 if (DECL_NAME (field) == NULL_TREE)
1833 /* Step through all anon unions in linear fashion. */
1834 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1836 field = field_array[bot++];
1837 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1838 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1840 tree anon = lookup_field (field, component);
1842 if (anon)
1843 return tree_cons (NULL_TREE, field, anon);
1847 /* Entire record is only anon unions. */
1848 if (bot > top)
1849 return NULL_TREE;
1851 /* Restart the binary search, with new lower bound. */
1852 continue;
1855 if (DECL_NAME (field) == component)
1856 break;
1857 if (DECL_NAME (field) < component)
1858 bot += half;
1859 else
1860 top = bot + half;
1863 if (DECL_NAME (field_array[bot]) == component)
1864 field = field_array[bot];
1865 else if (DECL_NAME (field) != component)
1866 return NULL_TREE;
1868 else
1870 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1872 if (DECL_NAME (field) == NULL_TREE
1873 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1874 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1876 tree anon = lookup_field (field, component);
1878 if (anon)
1879 return tree_cons (NULL_TREE, field, anon);
1882 if (DECL_NAME (field) == component)
1883 break;
1886 if (field == NULL_TREE)
1887 return NULL_TREE;
1890 return tree_cons (NULL_TREE, field, NULL_TREE);
1893 /* Make an expression to refer to the COMPONENT field of
1894 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1896 tree
1897 build_component_ref (tree datum, tree component)
1899 tree type = TREE_TYPE (datum);
1900 enum tree_code code = TREE_CODE (type);
1901 tree field = NULL;
1902 tree ref;
1904 if (!objc_is_public (datum, component))
1905 return error_mark_node;
1907 /* See if there is a field or component with name COMPONENT. */
1909 if (code == RECORD_TYPE || code == UNION_TYPE)
1911 if (!COMPLETE_TYPE_P (type))
1913 c_incomplete_type_error (NULL_TREE, type);
1914 return error_mark_node;
1917 field = lookup_field (datum, component);
1919 if (!field)
1921 error ("%qT has no member named %qE", type, component);
1922 return error_mark_node;
1925 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1926 This might be better solved in future the way the C++ front
1927 end does it - by giving the anonymous entities each a
1928 separate name and type, and then have build_component_ref
1929 recursively call itself. We can't do that here. */
1932 tree subdatum = TREE_VALUE (field);
1933 int quals;
1934 tree subtype;
1936 if (TREE_TYPE (subdatum) == error_mark_node)
1937 return error_mark_node;
1939 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1940 quals |= TYPE_QUALS (TREE_TYPE (datum));
1941 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1943 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1944 NULL_TREE);
1945 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1946 TREE_READONLY (ref) = 1;
1947 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1948 TREE_THIS_VOLATILE (ref) = 1;
1950 if (TREE_DEPRECATED (subdatum))
1951 warn_deprecated_use (subdatum);
1953 datum = ref;
1955 field = TREE_CHAIN (field);
1957 while (field);
1959 return ref;
1961 else if (code != ERROR_MARK)
1962 error ("request for member %qE in something not a structure or union",
1963 component);
1965 return error_mark_node;
1968 /* Given an expression PTR for a pointer, return an expression
1969 for the value pointed to.
1970 ERRORSTRING is the name of the operator to appear in error messages.
1972 LOC is the location to use for the generated tree. */
1974 tree
1975 build_indirect_ref (tree ptr, const char *errorstring, location_t loc)
1977 tree pointer = default_conversion (ptr);
1978 tree type = TREE_TYPE (pointer);
1979 tree ref;
1981 if (TREE_CODE (type) == POINTER_TYPE)
1983 if (CONVERT_EXPR_P (pointer)
1984 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
1986 /* If a warning is issued, mark it to avoid duplicates from
1987 the backend. This only needs to be done at
1988 warn_strict_aliasing > 2. */
1989 if (warn_strict_aliasing > 2)
1990 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
1991 type, TREE_OPERAND (pointer, 0)))
1992 TREE_NO_WARNING (pointer) = 1;
1995 if (TREE_CODE (pointer) == ADDR_EXPR
1996 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1997 == TREE_TYPE (type)))
1999 ref = TREE_OPERAND (pointer, 0);
2000 protected_set_expr_location (ref, loc);
2001 return ref;
2003 else
2005 tree t = TREE_TYPE (type);
2007 ref = build1 (INDIRECT_REF, t, pointer);
2009 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2011 error ("dereferencing pointer to incomplete type");
2012 return error_mark_node;
2014 if (VOID_TYPE_P (t) && skip_evaluation == 0)
2015 warning (0, "dereferencing %<void *%> pointer");
2017 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2018 so that we get the proper error message if the result is used
2019 to assign to. Also, &* is supposed to be a no-op.
2020 And ANSI C seems to specify that the type of the result
2021 should be the const type. */
2022 /* A de-reference of a pointer to const is not a const. It is valid
2023 to change it via some other pointer. */
2024 TREE_READONLY (ref) = TYPE_READONLY (t);
2025 TREE_SIDE_EFFECTS (ref)
2026 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2027 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2028 protected_set_expr_location (ref, loc);
2029 return ref;
2032 else if (TREE_CODE (pointer) != ERROR_MARK)
2033 error ("invalid type argument of %qs (have %qT)", errorstring, type);
2034 return error_mark_node;
2037 /* This handles expressions of the form "a[i]", which denotes
2038 an array reference.
2040 This is logically equivalent in C to *(a+i), but we may do it differently.
2041 If A is a variable or a member, we generate a primitive ARRAY_REF.
2042 This avoids forcing the array out of registers, and can work on
2043 arrays that are not lvalues (for example, members of structures returned
2044 by functions).
2046 LOC is the location to use for the returned expression. */
2048 tree
2049 build_array_ref (tree array, tree index, location_t loc)
2051 tree ret;
2052 bool swapped = false;
2053 if (TREE_TYPE (array) == error_mark_node
2054 || TREE_TYPE (index) == error_mark_node)
2055 return error_mark_node;
2057 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2058 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2060 tree temp;
2061 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2062 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2064 error_at (loc, "subscripted value is neither array nor pointer");
2065 return error_mark_node;
2067 temp = array;
2068 array = index;
2069 index = temp;
2070 swapped = true;
2073 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2075 error_at (loc, "array subscript is not an integer");
2076 return error_mark_node;
2079 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2081 error_at (loc, "subscripted value is pointer to function");
2082 return error_mark_node;
2085 /* ??? Existing practice has been to warn only when the char
2086 index is syntactically the index, not for char[array]. */
2087 if (!swapped)
2088 warn_array_subscript_with_type_char (index);
2090 /* Apply default promotions *after* noticing character types. */
2091 index = default_conversion (index);
2093 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2095 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2097 tree rval, type;
2099 /* An array that is indexed by a non-constant
2100 cannot be stored in a register; we must be able to do
2101 address arithmetic on its address.
2102 Likewise an array of elements of variable size. */
2103 if (TREE_CODE (index) != INTEGER_CST
2104 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2105 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2107 if (!c_mark_addressable (array))
2108 return error_mark_node;
2110 /* An array that is indexed by a constant value which is not within
2111 the array bounds cannot be stored in a register either; because we
2112 would get a crash in store_bit_field/extract_bit_field when trying
2113 to access a non-existent part of the register. */
2114 if (TREE_CODE (index) == INTEGER_CST
2115 && TYPE_DOMAIN (TREE_TYPE (array))
2116 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2118 if (!c_mark_addressable (array))
2119 return error_mark_node;
2122 if (pedantic)
2124 tree foo = array;
2125 while (TREE_CODE (foo) == COMPONENT_REF)
2126 foo = TREE_OPERAND (foo, 0);
2127 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2128 pedwarn (loc, OPT_pedantic,
2129 "ISO C forbids subscripting %<register%> array");
2130 else if (!flag_isoc99 && !lvalue_p (foo))
2131 pedwarn (loc, OPT_pedantic,
2132 "ISO C90 forbids subscripting non-lvalue array");
2135 type = TREE_TYPE (TREE_TYPE (array));
2136 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2137 /* Array ref is const/volatile if the array elements are
2138 or if the array is. */
2139 TREE_READONLY (rval)
2140 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2141 | TREE_READONLY (array));
2142 TREE_SIDE_EFFECTS (rval)
2143 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2144 | TREE_SIDE_EFFECTS (array));
2145 TREE_THIS_VOLATILE (rval)
2146 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2147 /* This was added by rms on 16 Nov 91.
2148 It fixes vol struct foo *a; a->elts[1]
2149 in an inline function.
2150 Hope it doesn't break something else. */
2151 | TREE_THIS_VOLATILE (array));
2152 ret = require_complete_type (fold (rval));
2153 protected_set_expr_location (ret, loc);
2154 return ret;
2156 else
2158 tree ar = default_conversion (array);
2160 if (ar == error_mark_node)
2161 return ar;
2163 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2164 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2166 return build_indirect_ref
2167 (build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2168 "array indexing", loc);
2172 /* Build an external reference to identifier ID. FUN indicates
2173 whether this will be used for a function call. LOC is the source
2174 location of the identifier. */
2175 tree
2176 build_external_ref (tree id, int fun, location_t loc)
2178 tree ref;
2179 tree decl = lookup_name (id);
2181 /* In Objective-C, an instance variable (ivar) may be preferred to
2182 whatever lookup_name() found. */
2183 decl = objc_lookup_ivar (decl, id);
2185 if (decl && decl != error_mark_node)
2186 ref = decl;
2187 else if (fun)
2188 /* Implicit function declaration. */
2189 ref = implicitly_declare (id);
2190 else if (decl == error_mark_node)
2191 /* Don't complain about something that's already been
2192 complained about. */
2193 return error_mark_node;
2194 else
2196 undeclared_variable (id, loc);
2197 return error_mark_node;
2200 if (TREE_TYPE (ref) == error_mark_node)
2201 return error_mark_node;
2203 if (TREE_DEPRECATED (ref))
2204 warn_deprecated_use (ref);
2206 /* Recursive call does not count as usage. */
2207 if (ref != current_function_decl)
2209 TREE_USED (ref) = 1;
2212 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2214 if (!in_sizeof && !in_typeof)
2215 C_DECL_USED (ref) = 1;
2216 else if (DECL_INITIAL (ref) == 0
2217 && DECL_EXTERNAL (ref)
2218 && !TREE_PUBLIC (ref))
2219 record_maybe_used_decl (ref);
2222 if (TREE_CODE (ref) == CONST_DECL)
2224 used_types_insert (TREE_TYPE (ref));
2225 ref = DECL_INITIAL (ref);
2226 TREE_CONSTANT (ref) = 1;
2228 else if (current_function_decl != 0
2229 && !DECL_FILE_SCOPE_P (current_function_decl)
2230 && (TREE_CODE (ref) == VAR_DECL
2231 || TREE_CODE (ref) == PARM_DECL
2232 || TREE_CODE (ref) == FUNCTION_DECL))
2234 tree context = decl_function_context (ref);
2236 if (context != 0 && context != current_function_decl)
2237 DECL_NONLOCAL (ref) = 1;
2239 /* C99 6.7.4p3: An inline definition of a function with external
2240 linkage ... shall not contain a reference to an identifier with
2241 internal linkage. */
2242 else if (current_function_decl != 0
2243 && DECL_DECLARED_INLINE_P (current_function_decl)
2244 && DECL_EXTERNAL (current_function_decl)
2245 && VAR_OR_FUNCTION_DECL_P (ref)
2246 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2247 && ! TREE_PUBLIC (ref)
2248 && DECL_CONTEXT (ref) != current_function_decl)
2249 pedwarn (loc, 0, "%qD is static but used in inline function %qD "
2250 "which is not static", ref, current_function_decl);
2252 return ref;
2255 /* Record details of decls possibly used inside sizeof or typeof. */
2256 struct maybe_used_decl
2258 /* The decl. */
2259 tree decl;
2260 /* The level seen at (in_sizeof + in_typeof). */
2261 int level;
2262 /* The next one at this level or above, or NULL. */
2263 struct maybe_used_decl *next;
2266 static struct maybe_used_decl *maybe_used_decls;
2268 /* Record that DECL, an undefined static function reference seen
2269 inside sizeof or typeof, might be used if the operand of sizeof is
2270 a VLA type or the operand of typeof is a variably modified
2271 type. */
2273 static void
2274 record_maybe_used_decl (tree decl)
2276 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2277 t->decl = decl;
2278 t->level = in_sizeof + in_typeof;
2279 t->next = maybe_used_decls;
2280 maybe_used_decls = t;
2283 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2284 USED is false, just discard them. If it is true, mark them used
2285 (if no longer inside sizeof or typeof) or move them to the next
2286 level up (if still inside sizeof or typeof). */
2288 void
2289 pop_maybe_used (bool used)
2291 struct maybe_used_decl *p = maybe_used_decls;
2292 int cur_level = in_sizeof + in_typeof;
2293 while (p && p->level > cur_level)
2295 if (used)
2297 if (cur_level == 0)
2298 C_DECL_USED (p->decl) = 1;
2299 else
2300 p->level = cur_level;
2302 p = p->next;
2304 if (!used || cur_level == 0)
2305 maybe_used_decls = p;
2308 /* Return the result of sizeof applied to EXPR. */
2310 struct c_expr
2311 c_expr_sizeof_expr (struct c_expr expr)
2313 struct c_expr ret;
2314 if (expr.value == error_mark_node)
2316 ret.value = error_mark_node;
2317 ret.original_code = ERROR_MARK;
2318 pop_maybe_used (false);
2320 else
2322 ret.value = c_sizeof (TREE_TYPE (expr.value));
2323 ret.original_code = ERROR_MARK;
2324 if (c_vla_type_p (TREE_TYPE (expr.value)))
2326 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2327 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2329 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2331 return ret;
2334 /* Return the result of sizeof applied to T, a structure for the type
2335 name passed to sizeof (rather than the type itself). */
2337 struct c_expr
2338 c_expr_sizeof_type (struct c_type_name *t)
2340 tree type;
2341 struct c_expr ret;
2342 type = groktypename (t);
2343 ret.value = c_sizeof (type);
2344 ret.original_code = ERROR_MARK;
2345 pop_maybe_used (type != error_mark_node
2346 ? C_TYPE_VARIABLE_SIZE (type) : false);
2347 return ret;
2350 /* Build a function call to function FUNCTION with parameters PARAMS.
2351 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2352 TREE_VALUE of each node is a parameter-expression.
2353 FUNCTION's data type may be a function type or a pointer-to-function. */
2355 tree
2356 build_function_call (tree function, tree params)
2358 tree fntype, fundecl = 0;
2359 tree name = NULL_TREE, result;
2360 tree tem;
2361 int nargs;
2362 tree *argarray;
2365 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2366 STRIP_TYPE_NOPS (function);
2368 /* Convert anything with function type to a pointer-to-function. */
2369 if (TREE_CODE (function) == FUNCTION_DECL)
2371 /* Implement type-directed function overloading for builtins.
2372 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2373 handle all the type checking. The result is a complete expression
2374 that implements this function call. */
2375 tem = resolve_overloaded_builtin (function, params);
2376 if (tem)
2377 return tem;
2379 name = DECL_NAME (function);
2380 fundecl = function;
2382 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2383 function = function_to_pointer_conversion (function);
2385 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2386 expressions, like those used for ObjC messenger dispatches. */
2387 function = objc_rewrite_function_call (function, params);
2389 fntype = TREE_TYPE (function);
2391 if (TREE_CODE (fntype) == ERROR_MARK)
2392 return error_mark_node;
2394 if (!(TREE_CODE (fntype) == POINTER_TYPE
2395 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2397 error ("called object %qE is not a function", function);
2398 return error_mark_node;
2401 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2402 current_function_returns_abnormally = 1;
2404 /* fntype now gets the type of function pointed to. */
2405 fntype = TREE_TYPE (fntype);
2407 /* Check that the function is called through a compatible prototype.
2408 If it is not, replace the call by a trap, wrapped up in a compound
2409 expression if necessary. This has the nice side-effect to prevent
2410 the tree-inliner from generating invalid assignment trees which may
2411 blow up in the RTL expander later. */
2412 if (CONVERT_EXPR_P (function)
2413 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2414 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2415 && !comptypes (fntype, TREE_TYPE (tem)))
2417 tree return_type = TREE_TYPE (fntype);
2418 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2419 NULL_TREE);
2421 /* This situation leads to run-time undefined behavior. We can't,
2422 therefore, simply error unless we can prove that all possible
2423 executions of the program must execute the code. */
2424 if (warning (0, "function called through a non-compatible type"))
2425 /* We can, however, treat "undefined" any way we please.
2426 Call abort to encourage the user to fix the program. */
2427 inform (input_location, "if this code is reached, the program will abort");
2429 if (VOID_TYPE_P (return_type))
2430 return trap;
2431 else
2433 tree rhs;
2435 if (AGGREGATE_TYPE_P (return_type))
2436 rhs = build_compound_literal (return_type,
2437 build_constructor (return_type, 0));
2438 else
2439 rhs = fold_convert (return_type, integer_zero_node);
2441 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2445 /* Convert the parameters to the types declared in the
2446 function prototype, or apply default promotions. */
2448 nargs = list_length (params);
2449 argarray = (tree *) alloca (nargs * sizeof (tree));
2450 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2451 params, function, fundecl);
2452 if (nargs < 0)
2453 return error_mark_node;
2455 /* Check that arguments to builtin functions match the expectations. */
2456 if (fundecl
2457 && DECL_BUILT_IN (fundecl)
2458 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2459 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2460 return error_mark_node;
2462 /* Check that the arguments to the function are valid. */
2463 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2464 TYPE_ARG_TYPES (fntype));
2466 if (require_constant_value)
2468 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2469 function, nargs, argarray);
2470 if (TREE_CONSTANT (result)
2471 && (name == NULL_TREE
2472 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2473 pedwarn_init (input_location, 0, "initializer element is not constant");
2475 else
2476 result = fold_build_call_array (TREE_TYPE (fntype),
2477 function, nargs, argarray);
2479 if (VOID_TYPE_P (TREE_TYPE (result)))
2480 return result;
2481 return require_complete_type (result);
2484 /* Convert the argument expressions in the list VALUES
2485 to the types in the list TYPELIST. The resulting arguments are
2486 stored in the array ARGARRAY which has size NARGS.
2488 If TYPELIST is exhausted, or when an element has NULL as its type,
2489 perform the default conversions.
2491 PARMLIST is the chain of parm decls for the function being called.
2492 It may be 0, if that info is not available.
2493 It is used only for generating error messages.
2495 FUNCTION is a tree for the called function. It is used only for
2496 error messages, where it is formatted with %qE.
2498 This is also where warnings about wrong number of args are generated.
2500 VALUES is a chain of TREE_LIST nodes with the elements of the list
2501 in the TREE_VALUE slots of those nodes.
2503 Returns the actual number of arguments processed (which may be less
2504 than NARGS in some error situations), or -1 on failure. */
2506 static int
2507 convert_arguments (int nargs, tree *argarray,
2508 tree typelist, tree values, tree function, tree fundecl)
2510 tree typetail, valtail;
2511 int parmnum;
2512 const bool type_generic = fundecl
2513 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2514 tree selector;
2516 /* Change pointer to function to the function itself for
2517 diagnostics. */
2518 if (TREE_CODE (function) == ADDR_EXPR
2519 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2520 function = TREE_OPERAND (function, 0);
2522 /* Handle an ObjC selector specially for diagnostics. */
2523 selector = objc_message_selector ();
2525 /* Scan the given expressions and types, producing individual
2526 converted arguments and storing them in ARGARRAY. */
2528 for (valtail = values, typetail = typelist, parmnum = 0;
2529 valtail;
2530 valtail = TREE_CHAIN (valtail), parmnum++)
2532 tree type = typetail ? TREE_VALUE (typetail) : 0;
2533 tree val = TREE_VALUE (valtail);
2534 tree rname = function;
2535 int argnum = parmnum + 1;
2536 const char *invalid_func_diag;
2538 if (type == void_type_node)
2540 error ("too many arguments to function %qE", function);
2541 return parmnum;
2544 if (selector && argnum > 2)
2546 rname = selector;
2547 argnum -= 2;
2550 STRIP_TYPE_NOPS (val);
2552 val = require_complete_type (val);
2554 if (type != 0)
2556 /* Formal parm type is specified by a function prototype. */
2557 tree parmval;
2559 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2561 error ("type of formal parameter %d is incomplete", parmnum + 1);
2562 parmval = val;
2564 else
2566 /* Optionally warn about conversions that
2567 differ from the default conversions. */
2568 if (warn_traditional_conversion || warn_traditional)
2570 unsigned int formal_prec = TYPE_PRECISION (type);
2572 if (INTEGRAL_TYPE_P (type)
2573 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2574 warning (0, "passing argument %d of %qE as integer "
2575 "rather than floating due to prototype",
2576 argnum, rname);
2577 if (INTEGRAL_TYPE_P (type)
2578 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2579 warning (0, "passing argument %d of %qE as integer "
2580 "rather than complex due to prototype",
2581 argnum, rname);
2582 else if (TREE_CODE (type) == COMPLEX_TYPE
2583 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2584 warning (0, "passing argument %d of %qE as complex "
2585 "rather than floating due to prototype",
2586 argnum, rname);
2587 else if (TREE_CODE (type) == REAL_TYPE
2588 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2589 warning (0, "passing argument %d of %qE as floating "
2590 "rather than integer due to prototype",
2591 argnum, rname);
2592 else if (TREE_CODE (type) == COMPLEX_TYPE
2593 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2594 warning (0, "passing argument %d of %qE as complex "
2595 "rather than integer due to prototype",
2596 argnum, rname);
2597 else if (TREE_CODE (type) == REAL_TYPE
2598 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2599 warning (0, "passing argument %d of %qE as floating "
2600 "rather than complex due to prototype",
2601 argnum, rname);
2602 /* ??? At some point, messages should be written about
2603 conversions between complex types, but that's too messy
2604 to do now. */
2605 else if (TREE_CODE (type) == REAL_TYPE
2606 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2608 /* Warn if any argument is passed as `float',
2609 since without a prototype it would be `double'. */
2610 if (formal_prec == TYPE_PRECISION (float_type_node)
2611 && type != dfloat32_type_node)
2612 warning (0, "passing argument %d of %qE as %<float%> "
2613 "rather than %<double%> due to prototype",
2614 argnum, rname);
2616 /* Warn if mismatch between argument and prototype
2617 for decimal float types. Warn of conversions with
2618 binary float types and of precision narrowing due to
2619 prototype. */
2620 else if (type != TREE_TYPE (val)
2621 && (type == dfloat32_type_node
2622 || type == dfloat64_type_node
2623 || type == dfloat128_type_node
2624 || TREE_TYPE (val) == dfloat32_type_node
2625 || TREE_TYPE (val) == dfloat64_type_node
2626 || TREE_TYPE (val) == dfloat128_type_node)
2627 && (formal_prec
2628 <= TYPE_PRECISION (TREE_TYPE (val))
2629 || (type == dfloat128_type_node
2630 && (TREE_TYPE (val)
2631 != dfloat64_type_node
2632 && (TREE_TYPE (val)
2633 != dfloat32_type_node)))
2634 || (type == dfloat64_type_node
2635 && (TREE_TYPE (val)
2636 != dfloat32_type_node))))
2637 warning (0, "passing argument %d of %qE as %qT "
2638 "rather than %qT due to prototype",
2639 argnum, rname, type, TREE_TYPE (val));
2642 /* Detect integer changing in width or signedness.
2643 These warnings are only activated with
2644 -Wtraditional-conversion, not with -Wtraditional. */
2645 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2646 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2648 tree would_have_been = default_conversion (val);
2649 tree type1 = TREE_TYPE (would_have_been);
2651 if (TREE_CODE (type) == ENUMERAL_TYPE
2652 && (TYPE_MAIN_VARIANT (type)
2653 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2654 /* No warning if function asks for enum
2655 and the actual arg is that enum type. */
2657 else if (formal_prec != TYPE_PRECISION (type1))
2658 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2659 "with different width due to prototype",
2660 argnum, rname);
2661 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2663 /* Don't complain if the formal parameter type
2664 is an enum, because we can't tell now whether
2665 the value was an enum--even the same enum. */
2666 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2668 else if (TREE_CODE (val) == INTEGER_CST
2669 && int_fits_type_p (val, type))
2670 /* Change in signedness doesn't matter
2671 if a constant value is unaffected. */
2673 /* If the value is extended from a narrower
2674 unsigned type, it doesn't matter whether we
2675 pass it as signed or unsigned; the value
2676 certainly is the same either way. */
2677 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2678 && TYPE_UNSIGNED (TREE_TYPE (val)))
2680 else if (TYPE_UNSIGNED (type))
2681 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2682 "as unsigned due to prototype",
2683 argnum, rname);
2684 else
2685 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
2686 "as signed due to prototype", argnum, rname);
2690 parmval = convert_for_assignment (type, val, ic_argpass,
2691 fundecl, function,
2692 parmnum + 1);
2694 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2695 && INTEGRAL_TYPE_P (type)
2696 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2697 parmval = default_conversion (parmval);
2699 argarray[parmnum] = parmval;
2701 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2702 && (TYPE_PRECISION (TREE_TYPE (val))
2703 < TYPE_PRECISION (double_type_node))
2704 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2706 if (type_generic)
2707 argarray[parmnum] = val;
2708 else
2709 /* Convert `float' to `double'. */
2710 argarray[parmnum] = convert (double_type_node, val);
2712 else if ((invalid_func_diag =
2713 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2715 error (invalid_func_diag);
2716 return -1;
2718 else
2719 /* Convert `short' and `char' to full-size `int'. */
2720 argarray[parmnum] = default_conversion (val);
2722 if (typetail)
2723 typetail = TREE_CHAIN (typetail);
2726 gcc_assert (parmnum == nargs);
2728 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2730 error ("too few arguments to function %qE", function);
2731 return -1;
2734 return parmnum;
2737 /* This is the entry point used by the parser to build unary operators
2738 in the input. CODE, a tree_code, specifies the unary operator, and
2739 ARG is the operand. For unary plus, the C parser currently uses
2740 CONVERT_EXPR for code.
2742 LOC is the location to use for the tree generated.
2745 struct c_expr
2746 parser_build_unary_op (enum tree_code code, struct c_expr arg, location_t loc)
2748 struct c_expr result;
2750 result.original_code = ERROR_MARK;
2751 result.value = build_unary_op (code, arg.value, 0);
2752 protected_set_expr_location (result.value, loc);
2754 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2755 overflow_warning (result.value);
2757 return result;
2760 /* This is the entry point used by the parser to build binary operators
2761 in the input. CODE, a tree_code, specifies the binary operator, and
2762 ARG1 and ARG2 are the operands. In addition to constructing the
2763 expression, we check for operands that were written with other binary
2764 operators in a way that is likely to confuse the user.
2766 LOCATION is the location of the binary operator. */
2768 struct c_expr
2769 parser_build_binary_op (location_t location, enum tree_code code,
2770 struct c_expr arg1, struct c_expr arg2)
2772 struct c_expr result;
2774 enum tree_code code1 = arg1.original_code;
2775 enum tree_code code2 = arg2.original_code;
2777 result.value = build_binary_op (location, code,
2778 arg1.value, arg2.value, 1);
2779 result.original_code = code;
2781 if (TREE_CODE (result.value) == ERROR_MARK)
2782 return result;
2784 if (location != UNKNOWN_LOCATION)
2785 protected_set_expr_location (result.value, location);
2787 /* Check for cases such as x+y<<z which users are likely
2788 to misinterpret. */
2789 if (warn_parentheses)
2790 warn_about_parentheses (code, code1, code2);
2792 if (TREE_CODE_CLASS (code1) != tcc_comparison)
2793 warn_logical_operator (code, arg1.value, arg2.value);
2795 /* Warn about comparisons against string literals, with the exception
2796 of testing for equality or inequality of a string literal with NULL. */
2797 if (code == EQ_EXPR || code == NE_EXPR)
2799 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2800 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2801 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2803 else if (TREE_CODE_CLASS (code) == tcc_comparison
2804 && (code1 == STRING_CST || code2 == STRING_CST))
2805 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
2807 if (TREE_OVERFLOW_P (result.value)
2808 && !TREE_OVERFLOW_P (arg1.value)
2809 && !TREE_OVERFLOW_P (arg2.value))
2810 overflow_warning (result.value);
2812 return result;
2815 /* Return a tree for the difference of pointers OP0 and OP1.
2816 The resulting tree has type int. */
2818 static tree
2819 pointer_diff (tree op0, tree op1)
2821 tree restype = ptrdiff_type_node;
2823 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2824 tree con0, con1, lit0, lit1;
2825 tree orig_op1 = op1;
2827 if (TREE_CODE (target_type) == VOID_TYPE)
2828 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2829 "pointer of type %<void *%> used in subtraction");
2830 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2831 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
2832 "pointer to a function used in subtraction");
2834 /* If the conversion to ptrdiff_type does anything like widening or
2835 converting a partial to an integral mode, we get a convert_expression
2836 that is in the way to do any simplifications.
2837 (fold-const.c doesn't know that the extra bits won't be needed.
2838 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2839 different mode in place.)
2840 So first try to find a common term here 'by hand'; we want to cover
2841 at least the cases that occur in legal static initializers. */
2842 if (CONVERT_EXPR_P (op0)
2843 && (TYPE_PRECISION (TREE_TYPE (op0))
2844 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2845 con0 = TREE_OPERAND (op0, 0);
2846 else
2847 con0 = op0;
2848 if (CONVERT_EXPR_P (op1)
2849 && (TYPE_PRECISION (TREE_TYPE (op1))
2850 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2851 con1 = TREE_OPERAND (op1, 0);
2852 else
2853 con1 = op1;
2855 if (TREE_CODE (con0) == PLUS_EXPR)
2857 lit0 = TREE_OPERAND (con0, 1);
2858 con0 = TREE_OPERAND (con0, 0);
2860 else
2861 lit0 = integer_zero_node;
2863 if (TREE_CODE (con1) == PLUS_EXPR)
2865 lit1 = TREE_OPERAND (con1, 1);
2866 con1 = TREE_OPERAND (con1, 0);
2868 else
2869 lit1 = integer_zero_node;
2871 if (operand_equal_p (con0, con1, 0))
2873 op0 = lit0;
2874 op1 = lit1;
2878 /* First do the subtraction as integers;
2879 then drop through to build the divide operator.
2880 Do not do default conversions on the minus operator
2881 in case restype is a short type. */
2883 op0 = build_binary_op (input_location,
2884 MINUS_EXPR, convert (restype, op0),
2885 convert (restype, op1), 0);
2886 /* This generates an error if op1 is pointer to incomplete type. */
2887 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2888 error ("arithmetic on pointer to an incomplete type");
2890 /* This generates an error if op0 is pointer to incomplete type. */
2891 op1 = c_size_in_bytes (target_type);
2893 /* Divide by the size, in easiest possible way. */
2894 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2897 /* Construct and perhaps optimize a tree representation
2898 for a unary operation. CODE, a tree_code, specifies the operation
2899 and XARG is the operand.
2900 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2901 the default promotions (such as from short to int).
2902 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2903 allows non-lvalues; this is only used to handle conversion of non-lvalue
2904 arrays to pointers in C99. */
2906 tree
2907 build_unary_op (enum tree_code code, tree xarg, int flag)
2909 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2910 tree arg = xarg;
2911 tree argtype = 0;
2912 enum tree_code typecode;
2913 tree val;
2914 int noconvert = flag;
2915 const char *invalid_op_diag;
2917 if (code != ADDR_EXPR)
2918 arg = require_complete_type (arg);
2920 typecode = TREE_CODE (TREE_TYPE (arg));
2921 if (typecode == ERROR_MARK)
2922 return error_mark_node;
2923 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2924 typecode = INTEGER_TYPE;
2926 if ((invalid_op_diag
2927 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2929 error (invalid_op_diag);
2930 return error_mark_node;
2933 switch (code)
2935 case CONVERT_EXPR:
2936 /* This is used for unary plus, because a CONVERT_EXPR
2937 is enough to prevent anybody from looking inside for
2938 associativity, but won't generate any code. */
2939 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2940 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
2941 || typecode == VECTOR_TYPE))
2943 error ("wrong type argument to unary plus");
2944 return error_mark_node;
2946 else if (!noconvert)
2947 arg = default_conversion (arg);
2948 arg = non_lvalue (arg);
2949 break;
2951 case NEGATE_EXPR:
2952 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2953 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
2954 || typecode == VECTOR_TYPE))
2956 error ("wrong type argument to unary minus");
2957 return error_mark_node;
2959 else if (!noconvert)
2960 arg = default_conversion (arg);
2961 break;
2963 case BIT_NOT_EXPR:
2964 /* ~ works on integer types and non float vectors. */
2965 if (typecode == INTEGER_TYPE
2966 || (typecode == VECTOR_TYPE
2967 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
2969 if (!noconvert)
2970 arg = default_conversion (arg);
2972 else if (typecode == COMPLEX_TYPE)
2974 code = CONJ_EXPR;
2975 pedwarn (input_location, OPT_pedantic,
2976 "ISO C does not support %<~%> for complex conjugation");
2977 if (!noconvert)
2978 arg = default_conversion (arg);
2980 else
2982 error ("wrong type argument to bit-complement");
2983 return error_mark_node;
2985 break;
2987 case ABS_EXPR:
2988 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2990 error ("wrong type argument to abs");
2991 return error_mark_node;
2993 else if (!noconvert)
2994 arg = default_conversion (arg);
2995 break;
2997 case CONJ_EXPR:
2998 /* Conjugating a real value is a no-op, but allow it anyway. */
2999 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3000 || typecode == COMPLEX_TYPE))
3002 error ("wrong type argument to conjugation");
3003 return error_mark_node;
3005 else if (!noconvert)
3006 arg = default_conversion (arg);
3007 break;
3009 case TRUTH_NOT_EXPR:
3010 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3011 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3012 && typecode != COMPLEX_TYPE)
3014 error ("wrong type argument to unary exclamation mark");
3015 return error_mark_node;
3017 arg = c_objc_common_truthvalue_conversion (input_location, arg);
3018 return invert_truthvalue (arg);
3020 case REALPART_EXPR:
3021 if (TREE_CODE (arg) == COMPLEX_CST)
3022 return TREE_REALPART (arg);
3023 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3024 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3025 else
3026 return arg;
3028 case IMAGPART_EXPR:
3029 if (TREE_CODE (arg) == COMPLEX_CST)
3030 return TREE_IMAGPART (arg);
3031 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3032 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3033 else
3034 return convert (TREE_TYPE (arg), integer_zero_node);
3036 case PREINCREMENT_EXPR:
3037 case POSTINCREMENT_EXPR:
3038 case PREDECREMENT_EXPR:
3039 case POSTDECREMENT_EXPR:
3041 /* Increment or decrement the real part of the value,
3042 and don't change the imaginary part. */
3043 if (typecode == COMPLEX_TYPE)
3045 tree real, imag;
3047 pedwarn (input_location, OPT_pedantic,
3048 "ISO C does not support %<++%> and %<--%> on complex types");
3050 arg = stabilize_reference (arg);
3051 real = build_unary_op (REALPART_EXPR, arg, 1);
3052 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3053 real = build_unary_op (code, real, 1);
3054 if (real == error_mark_node || imag == error_mark_node)
3055 return error_mark_node;
3056 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3057 real, imag);
3060 /* Report invalid types. */
3062 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3063 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3065 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3066 error ("wrong type argument to increment");
3067 else
3068 error ("wrong type argument to decrement");
3070 return error_mark_node;
3074 tree inc;
3075 tree result_type = TREE_TYPE (arg);
3077 arg = get_unwidened (arg, 0);
3078 argtype = TREE_TYPE (arg);
3080 /* Compute the increment. */
3082 if (typecode == POINTER_TYPE)
3084 /* If pointer target is an undefined struct,
3085 we just cannot know how to do the arithmetic. */
3086 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
3088 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3089 error ("increment of pointer to unknown structure");
3090 else
3091 error ("decrement of pointer to unknown structure");
3093 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3094 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3096 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3097 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3098 "wrong type argument to increment");
3099 else
3100 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3101 "wrong type argument to decrement");
3104 inc = c_size_in_bytes (TREE_TYPE (result_type));
3105 inc = fold_convert (sizetype, inc);
3107 else if (FRACT_MODE_P (TYPE_MODE (result_type)))
3109 /* For signed fract types, we invert ++ to -- or
3110 -- to ++, and change inc from 1 to -1, because
3111 it is not possible to represent 1 in signed fract constants.
3112 For unsigned fract types, the result always overflows and
3113 we get an undefined (original) or the maximum value. */
3114 if (code == PREINCREMENT_EXPR)
3115 code = PREDECREMENT_EXPR;
3116 else if (code == PREDECREMENT_EXPR)
3117 code = PREINCREMENT_EXPR;
3118 else if (code == POSTINCREMENT_EXPR)
3119 code = POSTDECREMENT_EXPR;
3120 else /* code == POSTDECREMENT_EXPR */
3121 code = POSTINCREMENT_EXPR;
3123 inc = integer_minus_one_node;
3124 inc = convert (argtype, inc);
3126 else
3128 inc = integer_one_node;
3129 inc = convert (argtype, inc);
3132 /* Complain about anything else that is not a true lvalue. */
3133 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3134 || code == POSTINCREMENT_EXPR)
3135 ? lv_increment
3136 : lv_decrement)))
3137 return error_mark_node;
3139 /* Report a read-only lvalue. */
3140 if (TREE_READONLY (arg))
3142 readonly_error (arg,
3143 ((code == PREINCREMENT_EXPR
3144 || code == POSTINCREMENT_EXPR)
3145 ? lv_increment : lv_decrement));
3146 return error_mark_node;
3149 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3150 val = boolean_increment (code, arg);
3151 else
3152 val = build2 (code, TREE_TYPE (arg), arg, inc);
3153 TREE_SIDE_EFFECTS (val) = 1;
3154 val = convert (result_type, val);
3155 if (TREE_CODE (val) != code)
3156 TREE_NO_WARNING (val) = 1;
3157 return val;
3160 case ADDR_EXPR:
3161 /* Note that this operation never does default_conversion. */
3163 /* Let &* cancel out to simplify resulting code. */
3164 if (TREE_CODE (arg) == INDIRECT_REF)
3166 /* Don't let this be an lvalue. */
3167 if (lvalue_p (TREE_OPERAND (arg, 0)))
3168 return non_lvalue (TREE_OPERAND (arg, 0));
3169 return TREE_OPERAND (arg, 0);
3172 /* For &x[y], return x+y */
3173 if (TREE_CODE (arg) == ARRAY_REF)
3175 tree op0 = TREE_OPERAND (arg, 0);
3176 if (!c_mark_addressable (op0))
3177 return error_mark_node;
3178 return build_binary_op (EXPR_LOCATION (xarg), PLUS_EXPR,
3179 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3180 ? array_to_pointer_conversion (op0)
3181 : op0),
3182 TREE_OPERAND (arg, 1), 1);
3185 /* Anything not already handled and not a true memory reference
3186 or a non-lvalue array is an error. */
3187 else if (typecode != FUNCTION_TYPE && !flag
3188 && !lvalue_or_else (arg, lv_addressof))
3189 return error_mark_node;
3191 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3192 argtype = TREE_TYPE (arg);
3194 /* If the lvalue is const or volatile, merge that into the type
3195 to which the address will point. Note that you can't get a
3196 restricted pointer by taking the address of something, so we
3197 only have to deal with `const' and `volatile' here. */
3198 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3199 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3200 argtype = c_build_type_variant (argtype,
3201 TREE_READONLY (arg),
3202 TREE_THIS_VOLATILE (arg));
3204 if (!c_mark_addressable (arg))
3205 return error_mark_node;
3207 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3208 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3210 argtype = build_pointer_type (argtype);
3212 /* ??? Cope with user tricks that amount to offsetof. Delete this
3213 when we have proper support for integer constant expressions. */
3214 val = get_base_address (arg);
3215 if (val && TREE_CODE (val) == INDIRECT_REF
3216 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3218 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3220 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3221 return fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3224 val = build1 (ADDR_EXPR, argtype, arg);
3226 return val;
3228 default:
3229 gcc_unreachable ();
3232 if (argtype == 0)
3233 argtype = TREE_TYPE (arg);
3234 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3235 : fold_build1 (code, argtype, arg);
3238 /* Return nonzero if REF is an lvalue valid for this language.
3239 Lvalues can be assigned, unless their type has TYPE_READONLY.
3240 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3242 static int
3243 lvalue_p (const_tree ref)
3245 const enum tree_code code = TREE_CODE (ref);
3247 switch (code)
3249 case REALPART_EXPR:
3250 case IMAGPART_EXPR:
3251 case COMPONENT_REF:
3252 return lvalue_p (TREE_OPERAND (ref, 0));
3254 case COMPOUND_LITERAL_EXPR:
3255 case STRING_CST:
3256 return 1;
3258 case INDIRECT_REF:
3259 case ARRAY_REF:
3260 case VAR_DECL:
3261 case PARM_DECL:
3262 case RESULT_DECL:
3263 case ERROR_MARK:
3264 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3265 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3267 case BIND_EXPR:
3268 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3270 default:
3271 return 0;
3275 /* Give an error for storing in something that is 'const'. */
3277 static void
3278 readonly_error (tree arg, enum lvalue_use use)
3280 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3281 || use == lv_asm);
3282 /* Using this macro rather than (for example) arrays of messages
3283 ensures that all the format strings are checked at compile
3284 time. */
3285 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3286 : (use == lv_increment ? (I) \
3287 : (use == lv_decrement ? (D) : (AS))))
3288 if (TREE_CODE (arg) == COMPONENT_REF)
3290 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3291 readonly_error (TREE_OPERAND (arg, 0), use);
3292 else
3293 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3294 G_("increment of read-only member %qD"),
3295 G_("decrement of read-only member %qD"),
3296 G_("read-only member %qD used as %<asm%> output")),
3297 TREE_OPERAND (arg, 1));
3299 else if (TREE_CODE (arg) == VAR_DECL)
3300 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3301 G_("increment of read-only variable %qD"),
3302 G_("decrement of read-only variable %qD"),
3303 G_("read-only variable %qD used as %<asm%> output")),
3304 arg);
3305 else
3306 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3307 G_("increment of read-only location %qE"),
3308 G_("decrement of read-only location %qE"),
3309 G_("read-only location %qE used as %<asm%> output")),
3310 arg);
3314 /* Return nonzero if REF is an lvalue valid for this language;
3315 otherwise, print an error message and return zero. USE says
3316 how the lvalue is being used and so selects the error message. */
3318 static int
3319 lvalue_or_else (const_tree ref, enum lvalue_use use)
3321 int win = lvalue_p (ref);
3323 if (!win)
3324 lvalue_error (use);
3326 return win;
3329 /* Mark EXP saying that we need to be able to take the
3330 address of it; it should not be allocated in a register.
3331 Returns true if successful. */
3333 bool
3334 c_mark_addressable (tree exp)
3336 tree x = exp;
3338 while (1)
3339 switch (TREE_CODE (x))
3341 case COMPONENT_REF:
3342 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3344 error
3345 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3346 return false;
3349 /* ... fall through ... */
3351 case ADDR_EXPR:
3352 case ARRAY_REF:
3353 case REALPART_EXPR:
3354 case IMAGPART_EXPR:
3355 x = TREE_OPERAND (x, 0);
3356 break;
3358 case COMPOUND_LITERAL_EXPR:
3359 case CONSTRUCTOR:
3360 TREE_ADDRESSABLE (x) = 1;
3361 return true;
3363 case VAR_DECL:
3364 case CONST_DECL:
3365 case PARM_DECL:
3366 case RESULT_DECL:
3367 if (C_DECL_REGISTER (x)
3368 && DECL_NONLOCAL (x))
3370 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3372 error
3373 ("global register variable %qD used in nested function", x);
3374 return false;
3376 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3378 else if (C_DECL_REGISTER (x))
3380 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3381 error ("address of global register variable %qD requested", x);
3382 else
3383 error ("address of register variable %qD requested", x);
3384 return false;
3387 /* drops in */
3388 case FUNCTION_DECL:
3389 TREE_ADDRESSABLE (x) = 1;
3390 /* drops out */
3391 default:
3392 return true;
3396 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3398 tree
3399 build_conditional_expr (tree ifexp, tree op1, tree op2)
3401 tree type1;
3402 tree type2;
3403 enum tree_code code1;
3404 enum tree_code code2;
3405 tree result_type = NULL;
3406 tree orig_op1 = op1, orig_op2 = op2;
3408 /* Promote both alternatives. */
3410 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3411 op1 = default_conversion (op1);
3412 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3413 op2 = default_conversion (op2);
3415 if (TREE_CODE (ifexp) == ERROR_MARK
3416 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3417 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3418 return error_mark_node;
3420 type1 = TREE_TYPE (op1);
3421 code1 = TREE_CODE (type1);
3422 type2 = TREE_TYPE (op2);
3423 code2 = TREE_CODE (type2);
3425 /* C90 does not permit non-lvalue arrays in conditional expressions.
3426 In C99 they will be pointers by now. */
3427 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3429 error ("non-lvalue array in conditional expression");
3430 return error_mark_node;
3433 /* Quickly detect the usual case where op1 and op2 have the same type
3434 after promotion. */
3435 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3437 if (type1 == type2)
3438 result_type = type1;
3439 else
3440 result_type = TYPE_MAIN_VARIANT (type1);
3442 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3443 || code1 == COMPLEX_TYPE)
3444 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3445 || code2 == COMPLEX_TYPE))
3447 result_type = c_common_type (type1, type2);
3449 /* If -Wsign-compare, warn here if type1 and type2 have
3450 different signedness. We'll promote the signed to unsigned
3451 and later code won't know it used to be different.
3452 Do this check on the original types, so that explicit casts
3453 will be considered, but default promotions won't. */
3454 if (warn_sign_compare && !skip_evaluation)
3456 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3457 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3459 if (unsigned_op1 ^ unsigned_op2)
3461 bool ovf;
3463 /* Do not warn if the result type is signed, since the
3464 signed type will only be chosen if it can represent
3465 all the values of the unsigned type. */
3466 if (!TYPE_UNSIGNED (result_type))
3467 /* OK */;
3468 /* Do not warn if the signed quantity is an unsuffixed
3469 integer literal (or some static constant expression
3470 involving such literals) and it is non-negative. */
3471 else if ((unsigned_op2
3472 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3473 || (unsigned_op1
3474 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3475 /* OK */;
3476 else
3477 warning (OPT_Wsign_compare, "signed and unsigned type in conditional expression");
3481 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3483 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3484 pedwarn (input_location, OPT_pedantic,
3485 "ISO C forbids conditional expr with only one void side");
3486 result_type = void_type_node;
3488 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3490 if (comp_target_types (type1, type2))
3491 result_type = common_pointer_type (type1, type2);
3492 else if (null_pointer_constant_p (orig_op1))
3493 result_type = qualify_type (type2, type1);
3494 else if (null_pointer_constant_p (orig_op2))
3495 result_type = qualify_type (type1, type2);
3496 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3498 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3499 pedwarn (input_location, OPT_pedantic,
3500 "ISO C forbids conditional expr between "
3501 "%<void *%> and function pointer");
3502 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3503 TREE_TYPE (type2)));
3505 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3507 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3508 pedwarn (input_location, OPT_pedantic,
3509 "ISO C forbids conditional expr between "
3510 "%<void *%> and function pointer");
3511 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3512 TREE_TYPE (type1)));
3514 else
3516 pedwarn (input_location, 0,
3517 "pointer type mismatch in conditional expression");
3518 result_type = build_pointer_type (void_type_node);
3521 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3523 if (!null_pointer_constant_p (orig_op2))
3524 pedwarn (input_location, 0,
3525 "pointer/integer type mismatch in conditional expression");
3526 else
3528 op2 = null_pointer_node;
3530 result_type = type1;
3532 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3534 if (!null_pointer_constant_p (orig_op1))
3535 pedwarn (input_location, 0,
3536 "pointer/integer type mismatch in conditional expression");
3537 else
3539 op1 = null_pointer_node;
3541 result_type = type2;
3544 if (!result_type)
3546 if (flag_cond_mismatch)
3547 result_type = void_type_node;
3548 else
3550 error ("type mismatch in conditional expression");
3551 return error_mark_node;
3555 /* Merge const and volatile flags of the incoming types. */
3556 result_type
3557 = build_type_variant (result_type,
3558 TREE_READONLY (op1) || TREE_READONLY (op2),
3559 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3561 if (result_type != TREE_TYPE (op1))
3562 op1 = convert_and_check (result_type, op1);
3563 if (result_type != TREE_TYPE (op2))
3564 op2 = convert_and_check (result_type, op2);
3566 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3569 /* Return a compound expression that performs two expressions and
3570 returns the value of the second of them. */
3572 tree
3573 build_compound_expr (tree expr1, tree expr2)
3575 if (!TREE_SIDE_EFFECTS (expr1))
3577 /* The left-hand operand of a comma expression is like an expression
3578 statement: with -Wunused, we should warn if it doesn't have
3579 any side-effects, unless it was explicitly cast to (void). */
3580 if (warn_unused_value)
3582 if (VOID_TYPE_P (TREE_TYPE (expr1))
3583 && CONVERT_EXPR_P (expr1))
3584 ; /* (void) a, b */
3585 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3586 && TREE_CODE (expr1) == COMPOUND_EXPR
3587 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
3588 ; /* (void) a, (void) b, c */
3589 else
3590 warning (OPT_Wunused_value,
3591 "left-hand operand of comma expression has no effect");
3595 /* With -Wunused, we should also warn if the left-hand operand does have
3596 side-effects, but computes a value which is not used. For example, in
3597 `foo() + bar(), baz()' the result of the `+' operator is not used,
3598 so we should issue a warning. */
3599 else if (warn_unused_value)
3600 warn_if_unused_value (expr1, input_location);
3602 if (expr2 == error_mark_node)
3603 return error_mark_node;
3605 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3608 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3610 tree
3611 build_c_cast (tree type, tree expr)
3613 tree value = expr;
3615 if (type == error_mark_node || expr == error_mark_node)
3616 return error_mark_node;
3618 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3619 only in <protocol> qualifications. But when constructing cast expressions,
3620 the protocols do matter and must be kept around. */
3621 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3622 return build1 (NOP_EXPR, type, expr);
3624 type = TYPE_MAIN_VARIANT (type);
3626 if (TREE_CODE (type) == ARRAY_TYPE)
3628 error ("cast specifies array type");
3629 return error_mark_node;
3632 if (TREE_CODE (type) == FUNCTION_TYPE)
3634 error ("cast specifies function type");
3635 return error_mark_node;
3638 if (!VOID_TYPE_P (type))
3640 value = require_complete_type (value);
3641 if (value == error_mark_node)
3642 return error_mark_node;
3645 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3647 if (TREE_CODE (type) == RECORD_TYPE
3648 || TREE_CODE (type) == UNION_TYPE)
3649 pedwarn (input_location, OPT_pedantic,
3650 "ISO C forbids casting nonscalar to the same type");
3652 else if (TREE_CODE (type) == UNION_TYPE)
3654 tree field;
3656 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3657 if (TREE_TYPE (field) != error_mark_node
3658 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3659 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3660 break;
3662 if (field)
3664 tree t;
3666 pedwarn (input_location, OPT_pedantic,
3667 "ISO C forbids casts to union type");
3668 t = digest_init (type,
3669 build_constructor_single (type, field, value),
3670 true, 0);
3671 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3672 return t;
3674 error ("cast to union type from type not present in union");
3675 return error_mark_node;
3677 else
3679 tree otype, ovalue;
3681 if (type == void_type_node)
3682 return build1 (CONVERT_EXPR, type, value);
3684 otype = TREE_TYPE (value);
3686 /* Optionally warn about potentially worrisome casts. */
3688 if (warn_cast_qual
3689 && TREE_CODE (type) == POINTER_TYPE
3690 && TREE_CODE (otype) == POINTER_TYPE)
3692 tree in_type = type;
3693 tree in_otype = otype;
3694 int added = 0;
3695 int discarded = 0;
3697 /* Check that the qualifiers on IN_TYPE are a superset of
3698 the qualifiers of IN_OTYPE. The outermost level of
3699 POINTER_TYPE nodes is uninteresting and we stop as soon
3700 as we hit a non-POINTER_TYPE node on either type. */
3703 in_otype = TREE_TYPE (in_otype);
3704 in_type = TREE_TYPE (in_type);
3706 /* GNU C allows cv-qualified function types. 'const'
3707 means the function is very pure, 'volatile' means it
3708 can't return. We need to warn when such qualifiers
3709 are added, not when they're taken away. */
3710 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3711 && TREE_CODE (in_type) == FUNCTION_TYPE)
3712 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3713 else
3714 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3716 while (TREE_CODE (in_type) == POINTER_TYPE
3717 && TREE_CODE (in_otype) == POINTER_TYPE);
3719 if (added)
3720 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
3722 if (discarded)
3723 /* There are qualifiers present in IN_OTYPE that are not
3724 present in IN_TYPE. */
3725 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
3728 /* Warn about possible alignment problems. */
3729 if (STRICT_ALIGNMENT
3730 && TREE_CODE (type) == POINTER_TYPE
3731 && TREE_CODE (otype) == POINTER_TYPE
3732 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3733 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3734 /* Don't warn about opaque types, where the actual alignment
3735 restriction is unknown. */
3736 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3737 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3738 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3739 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3740 warning (OPT_Wcast_align,
3741 "cast increases required alignment of target type");
3743 if (TREE_CODE (type) == INTEGER_TYPE
3744 && TREE_CODE (otype) == POINTER_TYPE
3745 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3746 /* Unlike conversion of integers to pointers, where the
3747 warning is disabled for converting constants because
3748 of cases such as SIG_*, warn about converting constant
3749 pointers to integers. In some cases it may cause unwanted
3750 sign extension, and a warning is appropriate. */
3751 warning (OPT_Wpointer_to_int_cast,
3752 "cast from pointer to integer of different size");
3754 if (TREE_CODE (value) == CALL_EXPR
3755 && TREE_CODE (type) != TREE_CODE (otype))
3756 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3757 "to non-matching type %qT", otype, type);
3759 if (TREE_CODE (type) == POINTER_TYPE
3760 && TREE_CODE (otype) == INTEGER_TYPE
3761 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3762 /* Don't warn about converting any constant. */
3763 && !TREE_CONSTANT (value))
3764 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3765 "of different size");
3767 if (warn_strict_aliasing <= 2)
3768 strict_aliasing_warning (otype, type, expr);
3770 /* If pedantic, warn for conversions between function and object
3771 pointer types, except for converting a null pointer constant
3772 to function pointer type. */
3773 if (pedantic
3774 && TREE_CODE (type) == POINTER_TYPE
3775 && TREE_CODE (otype) == POINTER_TYPE
3776 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3777 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3778 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
3779 "conversion of function pointer to object pointer type");
3781 if (pedantic
3782 && TREE_CODE (type) == POINTER_TYPE
3783 && TREE_CODE (otype) == POINTER_TYPE
3784 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3785 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3786 && !null_pointer_constant_p (value))
3787 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
3788 "conversion of object pointer to function pointer type");
3790 ovalue = value;
3791 value = convert (type, value);
3793 /* Ignore any integer overflow caused by the cast. */
3794 if (TREE_CODE (value) == INTEGER_CST)
3796 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
3798 if (!TREE_OVERFLOW (value))
3800 /* Avoid clobbering a shared constant. */
3801 value = copy_node (value);
3802 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3805 else if (TREE_OVERFLOW (value))
3806 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3807 value = build_int_cst_wide (TREE_TYPE (value),
3808 TREE_INT_CST_LOW (value),
3809 TREE_INT_CST_HIGH (value));
3813 /* Don't let a cast be an lvalue. */
3814 if (value == expr)
3815 value = non_lvalue (value);
3817 return value;
3820 /* Interpret a cast of expression EXPR to type TYPE. */
3821 tree
3822 c_cast_expr (struct c_type_name *type_name, tree expr)
3824 tree type;
3825 int saved_wsp = warn_strict_prototypes;
3827 /* This avoids warnings about unprototyped casts on
3828 integers. E.g. "#define SIG_DFL (void(*)())0". */
3829 if (TREE_CODE (expr) == INTEGER_CST)
3830 warn_strict_prototypes = 0;
3831 type = groktypename (type_name);
3832 warn_strict_prototypes = saved_wsp;
3834 return build_c_cast (type, expr);
3837 /* Build an assignment expression of lvalue LHS from value RHS.
3838 MODIFYCODE is the code for a binary operator that we use
3839 to combine the old value of LHS with RHS to get the new value.
3840 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3842 tree
3843 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3845 tree result;
3846 tree newrhs;
3847 tree lhstype = TREE_TYPE (lhs);
3848 tree olhstype = lhstype;
3850 /* Types that aren't fully specified cannot be used in assignments. */
3851 lhs = require_complete_type (lhs);
3853 /* Avoid duplicate error messages from operands that had errors. */
3854 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3855 return error_mark_node;
3857 if (!lvalue_or_else (lhs, lv_assign))
3858 return error_mark_node;
3860 STRIP_TYPE_NOPS (rhs);
3862 newrhs = rhs;
3864 /* If a binary op has been requested, combine the old LHS value with the RHS
3865 producing the value we should actually store into the LHS. */
3867 if (modifycode != NOP_EXPR)
3869 lhs = stabilize_reference (lhs);
3870 newrhs = build_binary_op (EXPR_LOCATION (lhs),
3871 modifycode, lhs, rhs, 1);
3874 /* Give an error for storing in something that is 'const'. */
3876 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3877 || ((TREE_CODE (lhstype) == RECORD_TYPE
3878 || TREE_CODE (lhstype) == UNION_TYPE)
3879 && C_TYPE_FIELDS_READONLY (lhstype)))
3881 readonly_error (lhs, lv_assign);
3882 return error_mark_node;
3885 /* If storing into a structure or union member,
3886 it has probably been given type `int'.
3887 Compute the type that would go with
3888 the actual amount of storage the member occupies. */
3890 if (TREE_CODE (lhs) == COMPONENT_REF
3891 && (TREE_CODE (lhstype) == INTEGER_TYPE
3892 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3893 || TREE_CODE (lhstype) == REAL_TYPE
3894 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3895 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3897 /* If storing in a field that is in actuality a short or narrower than one,
3898 we must store in the field in its actual type. */
3900 if (lhstype != TREE_TYPE (lhs))
3902 lhs = copy_node (lhs);
3903 TREE_TYPE (lhs) = lhstype;
3906 /* Convert new value to destination type. */
3908 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3909 NULL_TREE, NULL_TREE, 0);
3910 if (TREE_CODE (newrhs) == ERROR_MARK)
3911 return error_mark_node;
3913 /* Emit ObjC write barrier, if necessary. */
3914 if (c_dialect_objc () && flag_objc_gc)
3916 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3917 if (result)
3918 return result;
3921 /* Scan operands. */
3923 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3924 TREE_SIDE_EFFECTS (result) = 1;
3926 /* If we got the LHS in a different type for storing in,
3927 convert the result back to the nominal type of LHS
3928 so that the value we return always has the same type
3929 as the LHS argument. */
3931 if (olhstype == TREE_TYPE (result))
3932 return result;
3933 return convert_for_assignment (olhstype, result, ic_assign,
3934 NULL_TREE, NULL_TREE, 0);
3937 /* Convert value RHS to type TYPE as preparation for an assignment
3938 to an lvalue of type TYPE.
3939 The real work of conversion is done by `convert'.
3940 The purpose of this function is to generate error messages
3941 for assignments that are not allowed in C.
3942 ERRTYPE says whether it is argument passing, assignment,
3943 initialization or return.
3945 FUNCTION is a tree for the function being called.
3946 PARMNUM is the number of the argument, for printing in error messages. */
3948 static tree
3949 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3950 tree fundecl, tree function, int parmnum)
3952 enum tree_code codel = TREE_CODE (type);
3953 tree rhstype;
3954 enum tree_code coder;
3955 tree rname = NULL_TREE;
3956 bool objc_ok = false;
3958 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3960 tree selector;
3961 /* Change pointer to function to the function itself for
3962 diagnostics. */
3963 if (TREE_CODE (function) == ADDR_EXPR
3964 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3965 function = TREE_OPERAND (function, 0);
3967 /* Handle an ObjC selector specially for diagnostics. */
3968 selector = objc_message_selector ();
3969 rname = function;
3970 if (selector && parmnum > 2)
3972 rname = selector;
3973 parmnum -= 2;
3977 /* This macro is used to emit diagnostics to ensure that all format
3978 strings are complete sentences, visible to gettext and checked at
3979 compile time. */
3980 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
3981 do { \
3982 switch (errtype) \
3984 case ic_argpass: \
3985 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
3986 inform (fundecl ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
3987 "expected %qT but argument is of type %qT", \
3988 type, rhstype); \
3989 break; \
3990 case ic_argpass_nonproto: \
3991 warning (OPT, AR, parmnum, rname); \
3992 break; \
3993 case ic_assign: \
3994 pedwarn (LOCATION, OPT, AS); \
3995 break; \
3996 case ic_init: \
3997 pedwarn (LOCATION, OPT, IN); \
3998 break; \
3999 case ic_return: \
4000 pedwarn (LOCATION, OPT, RE); \
4001 break; \
4002 default: \
4003 gcc_unreachable (); \
4005 } while (0)
4007 STRIP_TYPE_NOPS (rhs);
4009 if (optimize && TREE_CODE (rhs) == VAR_DECL
4010 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
4011 rhs = decl_constant_value_for_broken_optimization (rhs);
4013 rhstype = TREE_TYPE (rhs);
4014 coder = TREE_CODE (rhstype);
4016 if (coder == ERROR_MARK)
4017 return error_mark_node;
4019 if (c_dialect_objc ())
4021 int parmno;
4023 switch (errtype)
4025 case ic_return:
4026 parmno = 0;
4027 break;
4029 case ic_assign:
4030 parmno = -1;
4031 break;
4033 case ic_init:
4034 parmno = -2;
4035 break;
4037 default:
4038 parmno = parmnum;
4039 break;
4042 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4045 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4046 return rhs;
4048 if (coder == VOID_TYPE)
4050 /* Except for passing an argument to an unprototyped function,
4051 this is a constraint violation. When passing an argument to
4052 an unprototyped function, it is compile-time undefined;
4053 making it a constraint in that case was rejected in
4054 DR#252. */
4055 error ("void value not ignored as it ought to be");
4056 return error_mark_node;
4058 rhs = require_complete_type (rhs);
4059 if (rhs == error_mark_node)
4060 return error_mark_node;
4061 /* A type converts to a reference to it.
4062 This code doesn't fully support references, it's just for the
4063 special case of va_start and va_copy. */
4064 if (codel == REFERENCE_TYPE
4065 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4067 if (!lvalue_p (rhs))
4069 error ("cannot pass rvalue to reference parameter");
4070 return error_mark_node;
4072 if (!c_mark_addressable (rhs))
4073 return error_mark_node;
4074 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4076 /* We already know that these two types are compatible, but they
4077 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4078 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4079 likely to be va_list, a typedef to __builtin_va_list, which
4080 is different enough that it will cause problems later. */
4081 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4082 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4084 rhs = build1 (NOP_EXPR, type, rhs);
4085 return rhs;
4087 /* Some types can interconvert without explicit casts. */
4088 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4089 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4090 return convert (type, rhs);
4091 /* Arithmetic types all interconvert, and enum is treated like int. */
4092 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4093 || codel == FIXED_POINT_TYPE
4094 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4095 || codel == BOOLEAN_TYPE)
4096 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4097 || coder == FIXED_POINT_TYPE
4098 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4099 || coder == BOOLEAN_TYPE))
4100 return convert_and_check (type, rhs);
4102 /* Aggregates in different TUs might need conversion. */
4103 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4104 && codel == coder
4105 && comptypes (type, rhstype))
4106 return convert_and_check (type, rhs);
4108 /* Conversion to a transparent union from its member types.
4109 This applies only to function arguments. */
4110 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4111 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
4113 tree memb, marginal_memb = NULL_TREE;
4115 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4117 tree memb_type = TREE_TYPE (memb);
4119 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4120 TYPE_MAIN_VARIANT (rhstype)))
4121 break;
4123 if (TREE_CODE (memb_type) != POINTER_TYPE)
4124 continue;
4126 if (coder == POINTER_TYPE)
4128 tree ttl = TREE_TYPE (memb_type);
4129 tree ttr = TREE_TYPE (rhstype);
4131 /* Any non-function converts to a [const][volatile] void *
4132 and vice versa; otherwise, targets must be the same.
4133 Meanwhile, the lhs target must have all the qualifiers of
4134 the rhs. */
4135 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4136 || comp_target_types (memb_type, rhstype))
4138 /* If this type won't generate any warnings, use it. */
4139 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4140 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4141 && TREE_CODE (ttl) == FUNCTION_TYPE)
4142 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4143 == TYPE_QUALS (ttr))
4144 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4145 == TYPE_QUALS (ttl))))
4146 break;
4148 /* Keep looking for a better type, but remember this one. */
4149 if (!marginal_memb)
4150 marginal_memb = memb;
4154 /* Can convert integer zero to any pointer type. */
4155 if (null_pointer_constant_p (rhs))
4157 rhs = null_pointer_node;
4158 break;
4162 if (memb || marginal_memb)
4164 if (!memb)
4166 /* We have only a marginally acceptable member type;
4167 it needs a warning. */
4168 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4169 tree ttr = TREE_TYPE (rhstype);
4171 /* Const and volatile mean something different for function
4172 types, so the usual warnings are not appropriate. */
4173 if (TREE_CODE (ttr) == FUNCTION_TYPE
4174 && TREE_CODE (ttl) == FUNCTION_TYPE)
4176 /* Because const and volatile on functions are
4177 restrictions that say the function will not do
4178 certain things, it is okay to use a const or volatile
4179 function where an ordinary one is wanted, but not
4180 vice-versa. */
4181 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4182 WARN_FOR_ASSIGNMENT (input_location, 0,
4183 G_("passing argument %d of %qE "
4184 "makes qualified function "
4185 "pointer from unqualified"),
4186 G_("assignment makes qualified "
4187 "function pointer from "
4188 "unqualified"),
4189 G_("initialization makes qualified "
4190 "function pointer from "
4191 "unqualified"),
4192 G_("return makes qualified function "
4193 "pointer from unqualified"));
4195 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4196 WARN_FOR_ASSIGNMENT (input_location, 0,
4197 G_("passing argument %d of %qE discards "
4198 "qualifiers from pointer target type"),
4199 G_("assignment discards qualifiers "
4200 "from pointer target type"),
4201 G_("initialization discards qualifiers "
4202 "from pointer target type"),
4203 G_("return discards qualifiers from "
4204 "pointer target type"));
4206 memb = marginal_memb;
4209 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4210 pedwarn (input_location, OPT_pedantic,
4211 "ISO C prohibits argument conversion to union type");
4213 rhs = fold_convert (TREE_TYPE (memb), rhs);
4214 return build_constructor_single (type, memb, rhs);
4218 /* Conversions among pointers */
4219 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4220 && (coder == codel))
4222 tree ttl = TREE_TYPE (type);
4223 tree ttr = TREE_TYPE (rhstype);
4224 tree mvl = ttl;
4225 tree mvr = ttr;
4226 bool is_opaque_pointer;
4227 int target_cmp = 0; /* Cache comp_target_types () result. */
4229 if (TREE_CODE (mvl) != ARRAY_TYPE)
4230 mvl = TYPE_MAIN_VARIANT (mvl);
4231 if (TREE_CODE (mvr) != ARRAY_TYPE)
4232 mvr = TYPE_MAIN_VARIANT (mvr);
4233 /* Opaque pointers are treated like void pointers. */
4234 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4236 /* C++ does not allow the implicit conversion void* -> T*. However,
4237 for the purpose of reducing the number of false positives, we
4238 tolerate the special case of
4240 int *p = NULL;
4242 where NULL is typically defined in C to be '(void *) 0'. */
4243 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4244 warning (OPT_Wc___compat, "request for implicit conversion from "
4245 "%qT to %qT not permitted in C++", rhstype, type);
4247 /* Check if the right-hand side has a format attribute but the
4248 left-hand side doesn't. */
4249 if (warn_missing_format_attribute
4250 && check_missing_format_attribute (type, rhstype))
4252 switch (errtype)
4254 case ic_argpass:
4255 case ic_argpass_nonproto:
4256 warning (OPT_Wmissing_format_attribute,
4257 "argument %d of %qE might be "
4258 "a candidate for a format attribute",
4259 parmnum, rname);
4260 break;
4261 case ic_assign:
4262 warning (OPT_Wmissing_format_attribute,
4263 "assignment left-hand side might be "
4264 "a candidate for a format attribute");
4265 break;
4266 case ic_init:
4267 warning (OPT_Wmissing_format_attribute,
4268 "initialization left-hand side might be "
4269 "a candidate for a format attribute");
4270 break;
4271 case ic_return:
4272 warning (OPT_Wmissing_format_attribute,
4273 "return type might be "
4274 "a candidate for a format attribute");
4275 break;
4276 default:
4277 gcc_unreachable ();
4281 /* Any non-function converts to a [const][volatile] void *
4282 and vice versa; otherwise, targets must be the same.
4283 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4284 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4285 || (target_cmp = comp_target_types (type, rhstype))
4286 || is_opaque_pointer
4287 || (c_common_unsigned_type (mvl)
4288 == c_common_unsigned_type (mvr)))
4290 if (pedantic
4291 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4293 (VOID_TYPE_P (ttr)
4294 && !null_pointer_constant_p (rhs)
4295 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4296 WARN_FOR_ASSIGNMENT (input_location, OPT_pedantic,
4297 G_("ISO C forbids passing argument %d of "
4298 "%qE between function pointer "
4299 "and %<void *%>"),
4300 G_("ISO C forbids assignment between "
4301 "function pointer and %<void *%>"),
4302 G_("ISO C forbids initialization between "
4303 "function pointer and %<void *%>"),
4304 G_("ISO C forbids return between function "
4305 "pointer and %<void *%>"));
4306 /* Const and volatile mean something different for function types,
4307 so the usual warnings are not appropriate. */
4308 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4309 && TREE_CODE (ttl) != FUNCTION_TYPE)
4311 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4313 /* Types differing only by the presence of the 'volatile'
4314 qualifier are acceptable if the 'volatile' has been added
4315 in by the Objective-C EH machinery. */
4316 if (!objc_type_quals_match (ttl, ttr))
4317 WARN_FOR_ASSIGNMENT (input_location, 0,
4318 G_("passing argument %d of %qE discards "
4319 "qualifiers from pointer target type"),
4320 G_("assignment discards qualifiers "
4321 "from pointer target type"),
4322 G_("initialization discards qualifiers "
4323 "from pointer target type"),
4324 G_("return discards qualifiers from "
4325 "pointer target type"));
4327 /* If this is not a case of ignoring a mismatch in signedness,
4328 no warning. */
4329 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4330 || target_cmp)
4332 /* If there is a mismatch, do warn. */
4333 else if (warn_pointer_sign)
4334 WARN_FOR_ASSIGNMENT (input_location, OPT_Wpointer_sign,
4335 G_("pointer targets in passing argument "
4336 "%d of %qE differ in signedness"),
4337 G_("pointer targets in assignment "
4338 "differ in signedness"),
4339 G_("pointer targets in initialization "
4340 "differ in signedness"),
4341 G_("pointer targets in return differ "
4342 "in signedness"));
4344 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4345 && TREE_CODE (ttr) == FUNCTION_TYPE)
4347 /* Because const and volatile on functions are restrictions
4348 that say the function will not do certain things,
4349 it is okay to use a const or volatile function
4350 where an ordinary one is wanted, but not vice-versa. */
4351 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4352 WARN_FOR_ASSIGNMENT (input_location, 0,
4353 G_("passing argument %d of %qE makes "
4354 "qualified function pointer "
4355 "from unqualified"),
4356 G_("assignment makes qualified function "
4357 "pointer from unqualified"),
4358 G_("initialization makes qualified "
4359 "function pointer from unqualified"),
4360 G_("return makes qualified function "
4361 "pointer from unqualified"));
4364 else
4365 /* Avoid warning about the volatile ObjC EH puts on decls. */
4366 if (!objc_ok)
4367 WARN_FOR_ASSIGNMENT (input_location, 0,
4368 G_("passing argument %d of %qE from "
4369 "incompatible pointer type"),
4370 G_("assignment from incompatible pointer type"),
4371 G_("initialization from incompatible "
4372 "pointer type"),
4373 G_("return from incompatible pointer type"));
4375 return convert (type, rhs);
4377 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4379 /* ??? This should not be an error when inlining calls to
4380 unprototyped functions. */
4381 error ("invalid use of non-lvalue array");
4382 return error_mark_node;
4384 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4386 /* An explicit constant 0 can convert to a pointer,
4387 or one that results from arithmetic, even including
4388 a cast to integer type. */
4389 if (!null_pointer_constant_p (rhs))
4390 WARN_FOR_ASSIGNMENT (input_location, 0,
4391 G_("passing argument %d of %qE makes "
4392 "pointer from integer without a cast"),
4393 G_("assignment makes pointer from integer "
4394 "without a cast"),
4395 G_("initialization makes pointer from "
4396 "integer without a cast"),
4397 G_("return makes pointer from integer "
4398 "without a cast"));
4400 return convert (type, rhs);
4402 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4404 WARN_FOR_ASSIGNMENT (input_location, 0,
4405 G_("passing argument %d of %qE makes integer "
4406 "from pointer without a cast"),
4407 G_("assignment makes integer from pointer "
4408 "without a cast"),
4409 G_("initialization makes integer from pointer "
4410 "without a cast"),
4411 G_("return makes integer from pointer "
4412 "without a cast"));
4413 return convert (type, rhs);
4415 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4416 return convert (type, rhs);
4418 switch (errtype)
4420 case ic_argpass:
4421 case ic_argpass_nonproto:
4422 /* ??? This should not be an error when inlining calls to
4423 unprototyped functions. */
4424 error ("incompatible type for argument %d of %qE", parmnum, rname);
4425 break;
4426 case ic_assign:
4427 error ("incompatible types in assignment");
4428 break;
4429 case ic_init:
4430 error ("incompatible types in initialization");
4431 break;
4432 case ic_return:
4433 error ("incompatible types in return");
4434 break;
4435 default:
4436 gcc_unreachable ();
4439 return error_mark_node;
4442 /* If VALUE is a compound expr all of whose expressions are constant, then
4443 return its value. Otherwise, return error_mark_node.
4445 This is for handling COMPOUND_EXPRs as initializer elements
4446 which is allowed with a warning when -pedantic is specified. */
4448 static tree
4449 valid_compound_expr_initializer (tree value, tree endtype)
4451 if (TREE_CODE (value) == COMPOUND_EXPR)
4453 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4454 == error_mark_node)
4455 return error_mark_node;
4456 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4457 endtype);
4459 else if (!initializer_constant_valid_p (value, endtype))
4460 return error_mark_node;
4461 else
4462 return value;
4465 /* Perform appropriate conversions on the initial value of a variable,
4466 store it in the declaration DECL,
4467 and print any error messages that are appropriate.
4468 If the init is invalid, store an ERROR_MARK. */
4470 void
4471 store_init_value (tree decl, tree init)
4473 tree value, type;
4475 /* If variable's type was invalidly declared, just ignore it. */
4477 type = TREE_TYPE (decl);
4478 if (TREE_CODE (type) == ERROR_MARK)
4479 return;
4481 /* Digest the specified initializer into an expression. */
4483 value = digest_init (type, init, true, TREE_STATIC (decl));
4485 /* Store the expression if valid; else report error. */
4487 if (!in_system_header
4488 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4489 warning (OPT_Wtraditional, "traditional C rejects automatic "
4490 "aggregate initialization");
4492 DECL_INITIAL (decl) = value;
4494 /* ANSI wants warnings about out-of-range constant initializers. */
4495 STRIP_TYPE_NOPS (value);
4496 if (TREE_STATIC (decl))
4497 constant_expression_warning (value);
4499 /* Check if we need to set array size from compound literal size. */
4500 if (TREE_CODE (type) == ARRAY_TYPE
4501 && TYPE_DOMAIN (type) == 0
4502 && value != error_mark_node)
4504 tree inside_init = init;
4506 STRIP_TYPE_NOPS (inside_init);
4507 inside_init = fold (inside_init);
4509 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4511 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4513 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4515 /* For int foo[] = (int [3]){1}; we need to set array size
4516 now since later on array initializer will be just the
4517 brace enclosed list of the compound literal. */
4518 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4519 TREE_TYPE (decl) = type;
4520 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4521 layout_type (type);
4522 layout_decl (cldecl, 0);
4528 /* Methods for storing and printing names for error messages. */
4530 /* Implement a spelling stack that allows components of a name to be pushed
4531 and popped. Each element on the stack is this structure. */
4533 struct spelling
4535 int kind;
4536 union
4538 unsigned HOST_WIDE_INT i;
4539 const char *s;
4540 } u;
4543 #define SPELLING_STRING 1
4544 #define SPELLING_MEMBER 2
4545 #define SPELLING_BOUNDS 3
4547 static struct spelling *spelling; /* Next stack element (unused). */
4548 static struct spelling *spelling_base; /* Spelling stack base. */
4549 static int spelling_size; /* Size of the spelling stack. */
4551 /* Macros to save and restore the spelling stack around push_... functions.
4552 Alternative to SAVE_SPELLING_STACK. */
4554 #define SPELLING_DEPTH() (spelling - spelling_base)
4555 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4557 /* Push an element on the spelling stack with type KIND and assign VALUE
4558 to MEMBER. */
4560 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4562 int depth = SPELLING_DEPTH (); \
4564 if (depth >= spelling_size) \
4566 spelling_size += 10; \
4567 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4568 spelling_size); \
4569 RESTORE_SPELLING_DEPTH (depth); \
4572 spelling->kind = (KIND); \
4573 spelling->MEMBER = (VALUE); \
4574 spelling++; \
4577 /* Push STRING on the stack. Printed literally. */
4579 static void
4580 push_string (const char *string)
4582 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4585 /* Push a member name on the stack. Printed as '.' STRING. */
4587 static void
4588 push_member_name (tree decl)
4590 const char *const string
4591 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4592 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4595 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4597 static void
4598 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4600 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4603 /* Compute the maximum size in bytes of the printed spelling. */
4605 static int
4606 spelling_length (void)
4608 int size = 0;
4609 struct spelling *p;
4611 for (p = spelling_base; p < spelling; p++)
4613 if (p->kind == SPELLING_BOUNDS)
4614 size += 25;
4615 else
4616 size += strlen (p->u.s) + 1;
4619 return size;
4622 /* Print the spelling to BUFFER and return it. */
4624 static char *
4625 print_spelling (char *buffer)
4627 char *d = buffer;
4628 struct spelling *p;
4630 for (p = spelling_base; p < spelling; p++)
4631 if (p->kind == SPELLING_BOUNDS)
4633 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4634 d += strlen (d);
4636 else
4638 const char *s;
4639 if (p->kind == SPELLING_MEMBER)
4640 *d++ = '.';
4641 for (s = p->u.s; (*d = *s++); d++)
4644 *d++ = '\0';
4645 return buffer;
4648 /* Issue an error message for a bad initializer component.
4649 MSGID identifies the message.
4650 The component name is taken from the spelling stack. */
4652 void
4653 error_init (const char *msgid)
4655 char *ofwhat;
4657 error ("%s", _(msgid));
4658 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4659 if (*ofwhat)
4660 error ("(near initialization for %qs)", ofwhat);
4663 /* Issue a pedantic warning for a bad initializer component. OPT is
4664 the option OPT_* (from options.h) controlling this warning or 0 if
4665 it is unconditionally given. MSGID identifies the message. The
4666 component name is taken from the spelling stack. */
4668 void
4669 pedwarn_init (location_t location, int opt, const char *msgid)
4671 char *ofwhat;
4673 pedwarn (location, opt, "%s", _(msgid));
4674 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4675 if (*ofwhat)
4676 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
4679 /* Issue a warning for a bad initializer component.
4681 OPT is the OPT_W* value corresponding to the warning option that
4682 controls this warning. MSGID identifies the message. The
4683 component name is taken from the spelling stack. */
4685 static void
4686 warning_init (int opt, const char *msgid)
4688 char *ofwhat;
4690 warning (opt, "%s", _(msgid));
4691 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4692 if (*ofwhat)
4693 warning (opt, "(near initialization for %qs)", ofwhat);
4696 /* If TYPE is an array type and EXPR is a parenthesized string
4697 constant, warn if pedantic that EXPR is being used to initialize an
4698 object of type TYPE. */
4700 void
4701 maybe_warn_string_init (tree type, struct c_expr expr)
4703 if (pedantic
4704 && TREE_CODE (type) == ARRAY_TYPE
4705 && TREE_CODE (expr.value) == STRING_CST
4706 && expr.original_code != STRING_CST)
4707 pedwarn_init (input_location, OPT_pedantic,
4708 "array initialized from parenthesized string constant");
4711 /* Digest the parser output INIT as an initializer for type TYPE.
4712 Return a C expression of type TYPE to represent the initial value.
4714 If INIT is a string constant, STRICT_STRING is true if it is
4715 unparenthesized or we should not warn here for it being parenthesized.
4716 For other types of INIT, STRICT_STRING is not used.
4718 REQUIRE_CONSTANT requests an error if non-constant initializers or
4719 elements are seen. */
4721 static tree
4722 digest_init (tree type, tree init, bool strict_string, int require_constant)
4724 enum tree_code code = TREE_CODE (type);
4725 tree inside_init = init;
4727 if (type == error_mark_node
4728 || !init
4729 || init == error_mark_node
4730 || TREE_TYPE (init) == error_mark_node)
4731 return error_mark_node;
4733 STRIP_TYPE_NOPS (inside_init);
4735 inside_init = fold (inside_init);
4737 /* Initialization of an array of chars from a string constant
4738 optionally enclosed in braces. */
4740 if (code == ARRAY_TYPE && inside_init
4741 && TREE_CODE (inside_init) == STRING_CST)
4743 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4744 /* Note that an array could be both an array of character type
4745 and an array of wchar_t if wchar_t is signed char or unsigned
4746 char. */
4747 bool char_array = (typ1 == char_type_node
4748 || typ1 == signed_char_type_node
4749 || typ1 == unsigned_char_type_node);
4750 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4751 bool char16_array = !!comptypes (typ1, char16_type_node);
4752 bool char32_array = !!comptypes (typ1, char32_type_node);
4754 if (char_array || wchar_array || char16_array || char32_array)
4756 struct c_expr expr;
4757 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
4758 expr.value = inside_init;
4759 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4760 maybe_warn_string_init (type, expr);
4762 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4763 TYPE_MAIN_VARIANT (type)))
4764 return inside_init;
4766 if (char_array)
4768 if (typ2 != char_type_node)
4770 error_init ("char-array initialized from wide string");
4771 return error_mark_node;
4774 else
4776 if (typ2 == char_type_node)
4778 error_init ("wide character array initialized from non-wide "
4779 "string");
4780 return error_mark_node;
4782 else if (!comptypes(typ1, typ2))
4784 error_init ("wide character array initialized from "
4785 "incompatible wide string");
4786 return error_mark_node;
4790 TREE_TYPE (inside_init) = type;
4791 if (TYPE_DOMAIN (type) != 0
4792 && TYPE_SIZE (type) != 0
4793 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4794 /* Subtract the size of a single (possibly wide) character
4795 because it's ok to ignore the terminating null char
4796 that is counted in the length of the constant. */
4797 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4798 TREE_STRING_LENGTH (inside_init)
4799 - (TYPE_PRECISION (typ1)
4800 / BITS_PER_UNIT)))
4801 pedwarn_init (input_location, 0,
4802 "initializer-string for array of chars is too long");
4804 return inside_init;
4806 else if (INTEGRAL_TYPE_P (typ1))
4808 error_init ("array of inappropriate type initialized "
4809 "from string constant");
4810 return error_mark_node;
4814 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4815 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4816 below and handle as a constructor. */
4817 if (code == VECTOR_TYPE
4818 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4819 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
4820 && TREE_CONSTANT (inside_init))
4822 if (TREE_CODE (inside_init) == VECTOR_CST
4823 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4824 TYPE_MAIN_VARIANT (type)))
4825 return inside_init;
4827 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4829 unsigned HOST_WIDE_INT ix;
4830 tree value;
4831 bool constant_p = true;
4833 /* Iterate through elements and check if all constructor
4834 elements are *_CSTs. */
4835 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4836 if (!CONSTANT_CLASS_P (value))
4838 constant_p = false;
4839 break;
4842 if (constant_p)
4843 return build_vector_from_ctor (type,
4844 CONSTRUCTOR_ELTS (inside_init));
4848 if (warn_sequence_point)
4849 verify_sequence_points (inside_init);
4851 /* Any type can be initialized
4852 from an expression of the same type, optionally with braces. */
4854 if (inside_init && TREE_TYPE (inside_init) != 0
4855 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4856 TYPE_MAIN_VARIANT (type))
4857 || (code == ARRAY_TYPE
4858 && comptypes (TREE_TYPE (inside_init), type))
4859 || (code == VECTOR_TYPE
4860 && comptypes (TREE_TYPE (inside_init), type))
4861 || (code == POINTER_TYPE
4862 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4863 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4864 TREE_TYPE (type)))))
4866 if (code == POINTER_TYPE)
4868 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4870 if (TREE_CODE (inside_init) == STRING_CST
4871 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4872 inside_init = array_to_pointer_conversion (inside_init);
4873 else
4875 error_init ("invalid use of non-lvalue array");
4876 return error_mark_node;
4881 if (code == VECTOR_TYPE)
4882 /* Although the types are compatible, we may require a
4883 conversion. */
4884 inside_init = convert (type, inside_init);
4886 if (require_constant
4887 && (code == VECTOR_TYPE || !flag_isoc99)
4888 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4890 /* As an extension, allow initializing objects with static storage
4891 duration with compound literals (which are then treated just as
4892 the brace enclosed list they contain). Also allow this for
4893 vectors, as we can only assign them with compound literals. */
4894 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4895 inside_init = DECL_INITIAL (decl);
4898 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4899 && TREE_CODE (inside_init) != CONSTRUCTOR)
4901 error_init ("array initialized from non-constant array expression");
4902 return error_mark_node;
4905 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4906 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4908 /* Compound expressions can only occur here if -pedantic or
4909 -pedantic-errors is specified. In the later case, we always want
4910 an error. In the former case, we simply want a warning. */
4911 if (require_constant && pedantic
4912 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4914 inside_init
4915 = valid_compound_expr_initializer (inside_init,
4916 TREE_TYPE (inside_init));
4917 if (inside_init == error_mark_node)
4918 error_init ("initializer element is not constant");
4919 else
4920 pedwarn_init (input_location, OPT_pedantic,
4921 "initializer element is not constant");
4922 if (flag_pedantic_errors)
4923 inside_init = error_mark_node;
4925 else if (require_constant
4926 && !initializer_constant_valid_p (inside_init,
4927 TREE_TYPE (inside_init)))
4929 error_init ("initializer element is not constant");
4930 inside_init = error_mark_node;
4933 /* Added to enable additional -Wmissing-format-attribute warnings. */
4934 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4935 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4936 NULL_TREE, 0);
4937 return inside_init;
4940 /* Handle scalar types, including conversions. */
4942 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
4943 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
4944 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
4946 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4947 && (TREE_CODE (init) == STRING_CST
4948 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4949 init = array_to_pointer_conversion (init);
4950 inside_init
4951 = convert_for_assignment (type, init, ic_init,
4952 NULL_TREE, NULL_TREE, 0);
4954 /* Check to see if we have already given an error message. */
4955 if (inside_init == error_mark_node)
4957 else if (require_constant && !TREE_CONSTANT (inside_init))
4959 error_init ("initializer element is not constant");
4960 inside_init = error_mark_node;
4962 else if (require_constant
4963 && !initializer_constant_valid_p (inside_init,
4964 TREE_TYPE (inside_init)))
4966 error_init ("initializer element is not computable at load time");
4967 inside_init = error_mark_node;
4970 return inside_init;
4973 /* Come here only for records and arrays. */
4975 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4977 error_init ("variable-sized object may not be initialized");
4978 return error_mark_node;
4981 error_init ("invalid initializer");
4982 return error_mark_node;
4985 /* Handle initializers that use braces. */
4987 /* Type of object we are accumulating a constructor for.
4988 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4989 static tree constructor_type;
4991 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4992 left to fill. */
4993 static tree constructor_fields;
4995 /* For an ARRAY_TYPE, this is the specified index
4996 at which to store the next element we get. */
4997 static tree constructor_index;
4999 /* For an ARRAY_TYPE, this is the maximum index. */
5000 static tree constructor_max_index;
5002 /* For a RECORD_TYPE, this is the first field not yet written out. */
5003 static tree constructor_unfilled_fields;
5005 /* For an ARRAY_TYPE, this is the index of the first element
5006 not yet written out. */
5007 static tree constructor_unfilled_index;
5009 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5010 This is so we can generate gaps between fields, when appropriate. */
5011 static tree constructor_bit_index;
5013 /* If we are saving up the elements rather than allocating them,
5014 this is the list of elements so far (in reverse order,
5015 most recent first). */
5016 static VEC(constructor_elt,gc) *constructor_elements;
5018 /* 1 if constructor should be incrementally stored into a constructor chain,
5019 0 if all the elements should be kept in AVL tree. */
5020 static int constructor_incremental;
5022 /* 1 if so far this constructor's elements are all compile-time constants. */
5023 static int constructor_constant;
5025 /* 1 if so far this constructor's elements are all valid address constants. */
5026 static int constructor_simple;
5028 /* 1 if this constructor is erroneous so far. */
5029 static int constructor_erroneous;
5031 /* Structure for managing pending initializer elements, organized as an
5032 AVL tree. */
5034 struct init_node
5036 struct init_node *left, *right;
5037 struct init_node *parent;
5038 int balance;
5039 tree purpose;
5040 tree value;
5043 /* Tree of pending elements at this constructor level.
5044 These are elements encountered out of order
5045 which belong at places we haven't reached yet in actually
5046 writing the output.
5047 Will never hold tree nodes across GC runs. */
5048 static struct init_node *constructor_pending_elts;
5050 /* The SPELLING_DEPTH of this constructor. */
5051 static int constructor_depth;
5053 /* DECL node for which an initializer is being read.
5054 0 means we are reading a constructor expression
5055 such as (struct foo) {...}. */
5056 static tree constructor_decl;
5058 /* Nonzero if this is an initializer for a top-level decl. */
5059 static int constructor_top_level;
5061 /* Nonzero if there were any member designators in this initializer. */
5062 static int constructor_designated;
5064 /* Nesting depth of designator list. */
5065 static int designator_depth;
5067 /* Nonzero if there were diagnosed errors in this designator list. */
5068 static int designator_erroneous;
5071 /* This stack has a level for each implicit or explicit level of
5072 structuring in the initializer, including the outermost one. It
5073 saves the values of most of the variables above. */
5075 struct constructor_range_stack;
5077 struct constructor_stack
5079 struct constructor_stack *next;
5080 tree type;
5081 tree fields;
5082 tree index;
5083 tree max_index;
5084 tree unfilled_index;
5085 tree unfilled_fields;
5086 tree bit_index;
5087 VEC(constructor_elt,gc) *elements;
5088 struct init_node *pending_elts;
5089 int offset;
5090 int depth;
5091 /* If value nonzero, this value should replace the entire
5092 constructor at this level. */
5093 struct c_expr replacement_value;
5094 struct constructor_range_stack *range_stack;
5095 char constant;
5096 char simple;
5097 char implicit;
5098 char erroneous;
5099 char outer;
5100 char incremental;
5101 char designated;
5104 static struct constructor_stack *constructor_stack;
5106 /* This stack represents designators from some range designator up to
5107 the last designator in the list. */
5109 struct constructor_range_stack
5111 struct constructor_range_stack *next, *prev;
5112 struct constructor_stack *stack;
5113 tree range_start;
5114 tree index;
5115 tree range_end;
5116 tree fields;
5119 static struct constructor_range_stack *constructor_range_stack;
5121 /* This stack records separate initializers that are nested.
5122 Nested initializers can't happen in ANSI C, but GNU C allows them
5123 in cases like { ... (struct foo) { ... } ... }. */
5125 struct initializer_stack
5127 struct initializer_stack *next;
5128 tree decl;
5129 struct constructor_stack *constructor_stack;
5130 struct constructor_range_stack *constructor_range_stack;
5131 VEC(constructor_elt,gc) *elements;
5132 struct spelling *spelling;
5133 struct spelling *spelling_base;
5134 int spelling_size;
5135 char top_level;
5136 char require_constant_value;
5137 char require_constant_elements;
5140 static struct initializer_stack *initializer_stack;
5142 /* Prepare to parse and output the initializer for variable DECL. */
5144 void
5145 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5147 const char *locus;
5148 struct initializer_stack *p = XNEW (struct initializer_stack);
5150 p->decl = constructor_decl;
5151 p->require_constant_value = require_constant_value;
5152 p->require_constant_elements = require_constant_elements;
5153 p->constructor_stack = constructor_stack;
5154 p->constructor_range_stack = constructor_range_stack;
5155 p->elements = constructor_elements;
5156 p->spelling = spelling;
5157 p->spelling_base = spelling_base;
5158 p->spelling_size = spelling_size;
5159 p->top_level = constructor_top_level;
5160 p->next = initializer_stack;
5161 initializer_stack = p;
5163 constructor_decl = decl;
5164 constructor_designated = 0;
5165 constructor_top_level = top_level;
5167 if (decl != 0 && decl != error_mark_node)
5169 require_constant_value = TREE_STATIC (decl);
5170 require_constant_elements
5171 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5172 /* For a scalar, you can always use any value to initialize,
5173 even within braces. */
5174 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5175 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5176 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5177 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5178 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5180 else
5182 require_constant_value = 0;
5183 require_constant_elements = 0;
5184 locus = "(anonymous)";
5187 constructor_stack = 0;
5188 constructor_range_stack = 0;
5190 missing_braces_mentioned = 0;
5192 spelling_base = 0;
5193 spelling_size = 0;
5194 RESTORE_SPELLING_DEPTH (0);
5196 if (locus)
5197 push_string (locus);
5200 void
5201 finish_init (void)
5203 struct initializer_stack *p = initializer_stack;
5205 /* Free the whole constructor stack of this initializer. */
5206 while (constructor_stack)
5208 struct constructor_stack *q = constructor_stack;
5209 constructor_stack = q->next;
5210 free (q);
5213 gcc_assert (!constructor_range_stack);
5215 /* Pop back to the data of the outer initializer (if any). */
5216 free (spelling_base);
5218 constructor_decl = p->decl;
5219 require_constant_value = p->require_constant_value;
5220 require_constant_elements = p->require_constant_elements;
5221 constructor_stack = p->constructor_stack;
5222 constructor_range_stack = p->constructor_range_stack;
5223 constructor_elements = p->elements;
5224 spelling = p->spelling;
5225 spelling_base = p->spelling_base;
5226 spelling_size = p->spelling_size;
5227 constructor_top_level = p->top_level;
5228 initializer_stack = p->next;
5229 free (p);
5232 /* Call here when we see the initializer is surrounded by braces.
5233 This is instead of a call to push_init_level;
5234 it is matched by a call to pop_init_level.
5236 TYPE is the type to initialize, for a constructor expression.
5237 For an initializer for a decl, TYPE is zero. */
5239 void
5240 really_start_incremental_init (tree type)
5242 struct constructor_stack *p = XNEW (struct constructor_stack);
5244 if (type == 0)
5245 type = TREE_TYPE (constructor_decl);
5247 if (targetm.vector_opaque_p (type))
5248 error ("opaque vector types cannot be initialized");
5250 p->type = constructor_type;
5251 p->fields = constructor_fields;
5252 p->index = constructor_index;
5253 p->max_index = constructor_max_index;
5254 p->unfilled_index = constructor_unfilled_index;
5255 p->unfilled_fields = constructor_unfilled_fields;
5256 p->bit_index = constructor_bit_index;
5257 p->elements = constructor_elements;
5258 p->constant = constructor_constant;
5259 p->simple = constructor_simple;
5260 p->erroneous = constructor_erroneous;
5261 p->pending_elts = constructor_pending_elts;
5262 p->depth = constructor_depth;
5263 p->replacement_value.value = 0;
5264 p->replacement_value.original_code = ERROR_MARK;
5265 p->implicit = 0;
5266 p->range_stack = 0;
5267 p->outer = 0;
5268 p->incremental = constructor_incremental;
5269 p->designated = constructor_designated;
5270 p->next = 0;
5271 constructor_stack = p;
5273 constructor_constant = 1;
5274 constructor_simple = 1;
5275 constructor_depth = SPELLING_DEPTH ();
5276 constructor_elements = 0;
5277 constructor_pending_elts = 0;
5278 constructor_type = type;
5279 constructor_incremental = 1;
5280 constructor_designated = 0;
5281 designator_depth = 0;
5282 designator_erroneous = 0;
5284 if (TREE_CODE (constructor_type) == RECORD_TYPE
5285 || TREE_CODE (constructor_type) == UNION_TYPE)
5287 constructor_fields = TYPE_FIELDS (constructor_type);
5288 /* Skip any nameless bit fields at the beginning. */
5289 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5290 && DECL_NAME (constructor_fields) == 0)
5291 constructor_fields = TREE_CHAIN (constructor_fields);
5293 constructor_unfilled_fields = constructor_fields;
5294 constructor_bit_index = bitsize_zero_node;
5296 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5298 if (TYPE_DOMAIN (constructor_type))
5300 constructor_max_index
5301 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5303 /* Detect non-empty initializations of zero-length arrays. */
5304 if (constructor_max_index == NULL_TREE
5305 && TYPE_SIZE (constructor_type))
5306 constructor_max_index = build_int_cst (NULL_TREE, -1);
5308 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5309 to initialize VLAs will cause a proper error; avoid tree
5310 checking errors as well by setting a safe value. */
5311 if (constructor_max_index
5312 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5313 constructor_max_index = build_int_cst (NULL_TREE, -1);
5315 constructor_index
5316 = convert (bitsizetype,
5317 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5319 else
5321 constructor_index = bitsize_zero_node;
5322 constructor_max_index = NULL_TREE;
5325 constructor_unfilled_index = constructor_index;
5327 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5329 /* Vectors are like simple fixed-size arrays. */
5330 constructor_max_index =
5331 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5332 constructor_index = bitsize_zero_node;
5333 constructor_unfilled_index = constructor_index;
5335 else
5337 /* Handle the case of int x = {5}; */
5338 constructor_fields = constructor_type;
5339 constructor_unfilled_fields = constructor_type;
5343 /* Push down into a subobject, for initialization.
5344 If this is for an explicit set of braces, IMPLICIT is 0.
5345 If it is because the next element belongs at a lower level,
5346 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5348 void
5349 push_init_level (int implicit)
5351 struct constructor_stack *p;
5352 tree value = NULL_TREE;
5354 /* If we've exhausted any levels that didn't have braces,
5355 pop them now. If implicit == 1, this will have been done in
5356 process_init_element; do not repeat it here because in the case
5357 of excess initializers for an empty aggregate this leads to an
5358 infinite cycle of popping a level and immediately recreating
5359 it. */
5360 if (implicit != 1)
5362 while (constructor_stack->implicit)
5364 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5365 || TREE_CODE (constructor_type) == UNION_TYPE)
5366 && constructor_fields == 0)
5367 process_init_element (pop_init_level (1));
5368 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5369 && constructor_max_index
5370 && tree_int_cst_lt (constructor_max_index,
5371 constructor_index))
5372 process_init_element (pop_init_level (1));
5373 else
5374 break;
5378 /* Unless this is an explicit brace, we need to preserve previous
5379 content if any. */
5380 if (implicit)
5382 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5383 || TREE_CODE (constructor_type) == UNION_TYPE)
5384 && constructor_fields)
5385 value = find_init_member (constructor_fields);
5386 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5387 value = find_init_member (constructor_index);
5390 p = XNEW (struct constructor_stack);
5391 p->type = constructor_type;
5392 p->fields = constructor_fields;
5393 p->index = constructor_index;
5394 p->max_index = constructor_max_index;
5395 p->unfilled_index = constructor_unfilled_index;
5396 p->unfilled_fields = constructor_unfilled_fields;
5397 p->bit_index = constructor_bit_index;
5398 p->elements = constructor_elements;
5399 p->constant = constructor_constant;
5400 p->simple = constructor_simple;
5401 p->erroneous = constructor_erroneous;
5402 p->pending_elts = constructor_pending_elts;
5403 p->depth = constructor_depth;
5404 p->replacement_value.value = 0;
5405 p->replacement_value.original_code = ERROR_MARK;
5406 p->implicit = implicit;
5407 p->outer = 0;
5408 p->incremental = constructor_incremental;
5409 p->designated = constructor_designated;
5410 p->next = constructor_stack;
5411 p->range_stack = 0;
5412 constructor_stack = p;
5414 constructor_constant = 1;
5415 constructor_simple = 1;
5416 constructor_depth = SPELLING_DEPTH ();
5417 constructor_elements = 0;
5418 constructor_incremental = 1;
5419 constructor_designated = 0;
5420 constructor_pending_elts = 0;
5421 if (!implicit)
5423 p->range_stack = constructor_range_stack;
5424 constructor_range_stack = 0;
5425 designator_depth = 0;
5426 designator_erroneous = 0;
5429 /* Don't die if an entire brace-pair level is superfluous
5430 in the containing level. */
5431 if (constructor_type == 0)
5433 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5434 || TREE_CODE (constructor_type) == UNION_TYPE)
5436 /* Don't die if there are extra init elts at the end. */
5437 if (constructor_fields == 0)
5438 constructor_type = 0;
5439 else
5441 constructor_type = TREE_TYPE (constructor_fields);
5442 push_member_name (constructor_fields);
5443 constructor_depth++;
5446 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5448 constructor_type = TREE_TYPE (constructor_type);
5449 push_array_bounds (tree_low_cst (constructor_index, 1));
5450 constructor_depth++;
5453 if (constructor_type == 0)
5455 error_init ("extra brace group at end of initializer");
5456 constructor_fields = 0;
5457 constructor_unfilled_fields = 0;
5458 return;
5461 if (value && TREE_CODE (value) == CONSTRUCTOR)
5463 constructor_constant = TREE_CONSTANT (value);
5464 constructor_simple = TREE_STATIC (value);
5465 constructor_elements = CONSTRUCTOR_ELTS (value);
5466 if (!VEC_empty (constructor_elt, constructor_elements)
5467 && (TREE_CODE (constructor_type) == RECORD_TYPE
5468 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5469 set_nonincremental_init ();
5472 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5474 missing_braces_mentioned = 1;
5475 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
5478 if (TREE_CODE (constructor_type) == RECORD_TYPE
5479 || TREE_CODE (constructor_type) == UNION_TYPE)
5481 constructor_fields = TYPE_FIELDS (constructor_type);
5482 /* Skip any nameless bit fields at the beginning. */
5483 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5484 && DECL_NAME (constructor_fields) == 0)
5485 constructor_fields = TREE_CHAIN (constructor_fields);
5487 constructor_unfilled_fields = constructor_fields;
5488 constructor_bit_index = bitsize_zero_node;
5490 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5492 /* Vectors are like simple fixed-size arrays. */
5493 constructor_max_index =
5494 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5495 constructor_index = convert (bitsizetype, integer_zero_node);
5496 constructor_unfilled_index = constructor_index;
5498 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5500 if (TYPE_DOMAIN (constructor_type))
5502 constructor_max_index
5503 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5505 /* Detect non-empty initializations of zero-length arrays. */
5506 if (constructor_max_index == NULL_TREE
5507 && TYPE_SIZE (constructor_type))
5508 constructor_max_index = build_int_cst (NULL_TREE, -1);
5510 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5511 to initialize VLAs will cause a proper error; avoid tree
5512 checking errors as well by setting a safe value. */
5513 if (constructor_max_index
5514 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5515 constructor_max_index = build_int_cst (NULL_TREE, -1);
5517 constructor_index
5518 = convert (bitsizetype,
5519 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5521 else
5522 constructor_index = bitsize_zero_node;
5524 constructor_unfilled_index = constructor_index;
5525 if (value && TREE_CODE (value) == STRING_CST)
5527 /* We need to split the char/wchar array into individual
5528 characters, so that we don't have to special case it
5529 everywhere. */
5530 set_nonincremental_init_from_string (value);
5533 else
5535 if (constructor_type != error_mark_node)
5536 warning_init (0, "braces around scalar initializer");
5537 constructor_fields = constructor_type;
5538 constructor_unfilled_fields = constructor_type;
5542 /* At the end of an implicit or explicit brace level,
5543 finish up that level of constructor. If a single expression
5544 with redundant braces initialized that level, return the
5545 c_expr structure for that expression. Otherwise, the original_code
5546 element is set to ERROR_MARK.
5547 If we were outputting the elements as they are read, return 0 as the value
5548 from inner levels (process_init_element ignores that),
5549 but return error_mark_node as the value from the outermost level
5550 (that's what we want to put in DECL_INITIAL).
5551 Otherwise, return a CONSTRUCTOR expression as the value. */
5553 struct c_expr
5554 pop_init_level (int implicit)
5556 struct constructor_stack *p;
5557 struct c_expr ret;
5558 ret.value = 0;
5559 ret.original_code = ERROR_MARK;
5561 if (implicit == 0)
5563 /* When we come to an explicit close brace,
5564 pop any inner levels that didn't have explicit braces. */
5565 while (constructor_stack->implicit)
5566 process_init_element (pop_init_level (1));
5568 gcc_assert (!constructor_range_stack);
5571 /* Now output all pending elements. */
5572 constructor_incremental = 1;
5573 output_pending_init_elements (1);
5575 p = constructor_stack;
5577 /* Error for initializing a flexible array member, or a zero-length
5578 array member in an inappropriate context. */
5579 if (constructor_type && constructor_fields
5580 && TREE_CODE (constructor_type) == ARRAY_TYPE
5581 && TYPE_DOMAIN (constructor_type)
5582 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5584 /* Silently discard empty initializations. The parser will
5585 already have pedwarned for empty brackets. */
5586 if (integer_zerop (constructor_unfilled_index))
5587 constructor_type = NULL_TREE;
5588 else
5590 gcc_assert (!TYPE_SIZE (constructor_type));
5592 if (constructor_depth > 2)
5593 error_init ("initialization of flexible array member in a nested context");
5594 else
5595 pedwarn_init (input_location, OPT_pedantic,
5596 "initialization of a flexible array member");
5598 /* We have already issued an error message for the existence
5599 of a flexible array member not at the end of the structure.
5600 Discard the initializer so that we do not die later. */
5601 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5602 constructor_type = NULL_TREE;
5606 /* Warn when some struct elements are implicitly initialized to zero. */
5607 if (warn_missing_field_initializers
5608 && constructor_type
5609 && TREE_CODE (constructor_type) == RECORD_TYPE
5610 && constructor_unfilled_fields)
5612 /* Do not warn for flexible array members or zero-length arrays. */
5613 while (constructor_unfilled_fields
5614 && (!DECL_SIZE (constructor_unfilled_fields)
5615 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5616 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5618 /* Do not warn if this level of the initializer uses member
5619 designators; it is likely to be deliberate. */
5620 if (constructor_unfilled_fields && !constructor_designated)
5622 push_member_name (constructor_unfilled_fields);
5623 warning_init (OPT_Wmissing_field_initializers,
5624 "missing initializer");
5625 RESTORE_SPELLING_DEPTH (constructor_depth);
5629 /* Pad out the end of the structure. */
5630 if (p->replacement_value.value)
5631 /* If this closes a superfluous brace pair,
5632 just pass out the element between them. */
5633 ret = p->replacement_value;
5634 else if (constructor_type == 0)
5636 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5637 && TREE_CODE (constructor_type) != UNION_TYPE
5638 && TREE_CODE (constructor_type) != ARRAY_TYPE
5639 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5641 /* A nonincremental scalar initializer--just return
5642 the element, after verifying there is just one. */
5643 if (VEC_empty (constructor_elt,constructor_elements))
5645 if (!constructor_erroneous)
5646 error_init ("empty scalar initializer");
5647 ret.value = error_mark_node;
5649 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5651 error_init ("extra elements in scalar initializer");
5652 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5654 else
5655 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5657 else
5659 if (constructor_erroneous)
5660 ret.value = error_mark_node;
5661 else
5663 ret.value = build_constructor (constructor_type,
5664 constructor_elements);
5665 if (constructor_constant)
5666 TREE_CONSTANT (ret.value) = 1;
5667 if (constructor_constant && constructor_simple)
5668 TREE_STATIC (ret.value) = 1;
5672 constructor_type = p->type;
5673 constructor_fields = p->fields;
5674 constructor_index = p->index;
5675 constructor_max_index = p->max_index;
5676 constructor_unfilled_index = p->unfilled_index;
5677 constructor_unfilled_fields = p->unfilled_fields;
5678 constructor_bit_index = p->bit_index;
5679 constructor_elements = p->elements;
5680 constructor_constant = p->constant;
5681 constructor_simple = p->simple;
5682 constructor_erroneous = p->erroneous;
5683 constructor_incremental = p->incremental;
5684 constructor_designated = p->designated;
5685 constructor_pending_elts = p->pending_elts;
5686 constructor_depth = p->depth;
5687 if (!p->implicit)
5688 constructor_range_stack = p->range_stack;
5689 RESTORE_SPELLING_DEPTH (constructor_depth);
5691 constructor_stack = p->next;
5692 free (p);
5694 if (ret.value == 0 && constructor_stack == 0)
5695 ret.value = error_mark_node;
5696 return ret;
5699 /* Common handling for both array range and field name designators.
5700 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5702 static int
5703 set_designator (int array)
5705 tree subtype;
5706 enum tree_code subcode;
5708 /* Don't die if an entire brace-pair level is superfluous
5709 in the containing level. */
5710 if (constructor_type == 0)
5711 return 1;
5713 /* If there were errors in this designator list already, bail out
5714 silently. */
5715 if (designator_erroneous)
5716 return 1;
5718 if (!designator_depth)
5720 gcc_assert (!constructor_range_stack);
5722 /* Designator list starts at the level of closest explicit
5723 braces. */
5724 while (constructor_stack->implicit)
5725 process_init_element (pop_init_level (1));
5726 constructor_designated = 1;
5727 return 0;
5730 switch (TREE_CODE (constructor_type))
5732 case RECORD_TYPE:
5733 case UNION_TYPE:
5734 subtype = TREE_TYPE (constructor_fields);
5735 if (subtype != error_mark_node)
5736 subtype = TYPE_MAIN_VARIANT (subtype);
5737 break;
5738 case ARRAY_TYPE:
5739 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5740 break;
5741 default:
5742 gcc_unreachable ();
5745 subcode = TREE_CODE (subtype);
5746 if (array && subcode != ARRAY_TYPE)
5748 error_init ("array index in non-array initializer");
5749 return 1;
5751 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5753 error_init ("field name not in record or union initializer");
5754 return 1;
5757 constructor_designated = 1;
5758 push_init_level (2);
5759 return 0;
5762 /* If there are range designators in designator list, push a new designator
5763 to constructor_range_stack. RANGE_END is end of such stack range or
5764 NULL_TREE if there is no range designator at this level. */
5766 static void
5767 push_range_stack (tree range_end)
5769 struct constructor_range_stack *p;
5771 p = GGC_NEW (struct constructor_range_stack);
5772 p->prev = constructor_range_stack;
5773 p->next = 0;
5774 p->fields = constructor_fields;
5775 p->range_start = constructor_index;
5776 p->index = constructor_index;
5777 p->stack = constructor_stack;
5778 p->range_end = range_end;
5779 if (constructor_range_stack)
5780 constructor_range_stack->next = p;
5781 constructor_range_stack = p;
5784 /* Within an array initializer, specify the next index to be initialized.
5785 FIRST is that index. If LAST is nonzero, then initialize a range
5786 of indices, running from FIRST through LAST. */
5788 void
5789 set_init_index (tree first, tree last)
5791 if (set_designator (1))
5792 return;
5794 designator_erroneous = 1;
5796 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5797 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5799 error_init ("array index in initializer not of integer type");
5800 return;
5803 if (TREE_CODE (first) != INTEGER_CST)
5804 error_init ("nonconstant array index in initializer");
5805 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5806 error_init ("nonconstant array index in initializer");
5807 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5808 error_init ("array index in non-array initializer");
5809 else if (tree_int_cst_sgn (first) == -1)
5810 error_init ("array index in initializer exceeds array bounds");
5811 else if (constructor_max_index
5812 && tree_int_cst_lt (constructor_max_index, first))
5813 error_init ("array index in initializer exceeds array bounds");
5814 else
5816 constructor_index = convert (bitsizetype, first);
5818 if (last)
5820 if (tree_int_cst_equal (first, last))
5821 last = 0;
5822 else if (tree_int_cst_lt (last, first))
5824 error_init ("empty index range in initializer");
5825 last = 0;
5827 else
5829 last = convert (bitsizetype, last);
5830 if (constructor_max_index != 0
5831 && tree_int_cst_lt (constructor_max_index, last))
5833 error_init ("array index range in initializer exceeds array bounds");
5834 last = 0;
5839 designator_depth++;
5840 designator_erroneous = 0;
5841 if (constructor_range_stack || last)
5842 push_range_stack (last);
5846 /* Within a struct initializer, specify the next field to be initialized. */
5848 void
5849 set_init_label (tree fieldname)
5851 tree tail;
5853 if (set_designator (0))
5854 return;
5856 designator_erroneous = 1;
5858 if (TREE_CODE (constructor_type) != RECORD_TYPE
5859 && TREE_CODE (constructor_type) != UNION_TYPE)
5861 error_init ("field name not in record or union initializer");
5862 return;
5865 for (tail = TYPE_FIELDS (constructor_type); tail;
5866 tail = TREE_CHAIN (tail))
5868 if (DECL_NAME (tail) == fieldname)
5869 break;
5872 if (tail == 0)
5873 error ("unknown field %qE specified in initializer", fieldname);
5874 else
5876 constructor_fields = tail;
5877 designator_depth++;
5878 designator_erroneous = 0;
5879 if (constructor_range_stack)
5880 push_range_stack (NULL_TREE);
5884 /* Add a new initializer to the tree of pending initializers. PURPOSE
5885 identifies the initializer, either array index or field in a structure.
5886 VALUE is the value of that index or field. */
5888 static void
5889 add_pending_init (tree purpose, tree value)
5891 struct init_node *p, **q, *r;
5893 q = &constructor_pending_elts;
5894 p = 0;
5896 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5898 while (*q != 0)
5900 p = *q;
5901 if (tree_int_cst_lt (purpose, p->purpose))
5902 q = &p->left;
5903 else if (tree_int_cst_lt (p->purpose, purpose))
5904 q = &p->right;
5905 else
5907 if (TREE_SIDE_EFFECTS (p->value))
5908 warning_init (0, "initialized field with side-effects overwritten");
5909 else if (warn_override_init)
5910 warning_init (OPT_Woverride_init, "initialized field overwritten");
5911 p->value = value;
5912 return;
5916 else
5918 tree bitpos;
5920 bitpos = bit_position (purpose);
5921 while (*q != NULL)
5923 p = *q;
5924 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5925 q = &p->left;
5926 else if (p->purpose != purpose)
5927 q = &p->right;
5928 else
5930 if (TREE_SIDE_EFFECTS (p->value))
5931 warning_init (0, "initialized field with side-effects overwritten");
5932 else if (warn_override_init)
5933 warning_init (OPT_Woverride_init, "initialized field overwritten");
5934 p->value = value;
5935 return;
5940 r = GGC_NEW (struct init_node);
5941 r->purpose = purpose;
5942 r->value = value;
5944 *q = r;
5945 r->parent = p;
5946 r->left = 0;
5947 r->right = 0;
5948 r->balance = 0;
5950 while (p)
5952 struct init_node *s;
5954 if (r == p->left)
5956 if (p->balance == 0)
5957 p->balance = -1;
5958 else if (p->balance < 0)
5960 if (r->balance < 0)
5962 /* L rotation. */
5963 p->left = r->right;
5964 if (p->left)
5965 p->left->parent = p;
5966 r->right = p;
5968 p->balance = 0;
5969 r->balance = 0;
5971 s = p->parent;
5972 p->parent = r;
5973 r->parent = s;
5974 if (s)
5976 if (s->left == p)
5977 s->left = r;
5978 else
5979 s->right = r;
5981 else
5982 constructor_pending_elts = r;
5984 else
5986 /* LR rotation. */
5987 struct init_node *t = r->right;
5989 r->right = t->left;
5990 if (r->right)
5991 r->right->parent = r;
5992 t->left = r;
5994 p->left = t->right;
5995 if (p->left)
5996 p->left->parent = p;
5997 t->right = p;
5999 p->balance = t->balance < 0;
6000 r->balance = -(t->balance > 0);
6001 t->balance = 0;
6003 s = p->parent;
6004 p->parent = t;
6005 r->parent = t;
6006 t->parent = s;
6007 if (s)
6009 if (s->left == p)
6010 s->left = t;
6011 else
6012 s->right = t;
6014 else
6015 constructor_pending_elts = t;
6017 break;
6019 else
6021 /* p->balance == +1; growth of left side balances the node. */
6022 p->balance = 0;
6023 break;
6026 else /* r == p->right */
6028 if (p->balance == 0)
6029 /* Growth propagation from right side. */
6030 p->balance++;
6031 else if (p->balance > 0)
6033 if (r->balance > 0)
6035 /* R rotation. */
6036 p->right = r->left;
6037 if (p->right)
6038 p->right->parent = p;
6039 r->left = p;
6041 p->balance = 0;
6042 r->balance = 0;
6044 s = p->parent;
6045 p->parent = r;
6046 r->parent = s;
6047 if (s)
6049 if (s->left == p)
6050 s->left = r;
6051 else
6052 s->right = r;
6054 else
6055 constructor_pending_elts = r;
6057 else /* r->balance == -1 */
6059 /* RL rotation */
6060 struct init_node *t = r->left;
6062 r->left = t->right;
6063 if (r->left)
6064 r->left->parent = r;
6065 t->right = r;
6067 p->right = t->left;
6068 if (p->right)
6069 p->right->parent = p;
6070 t->left = p;
6072 r->balance = (t->balance < 0);
6073 p->balance = -(t->balance > 0);
6074 t->balance = 0;
6076 s = p->parent;
6077 p->parent = t;
6078 r->parent = t;
6079 t->parent = s;
6080 if (s)
6082 if (s->left == p)
6083 s->left = t;
6084 else
6085 s->right = t;
6087 else
6088 constructor_pending_elts = t;
6090 break;
6092 else
6094 /* p->balance == -1; growth of right side balances the node. */
6095 p->balance = 0;
6096 break;
6100 r = p;
6101 p = p->parent;
6105 /* Build AVL tree from a sorted chain. */
6107 static void
6108 set_nonincremental_init (void)
6110 unsigned HOST_WIDE_INT ix;
6111 tree index, value;
6113 if (TREE_CODE (constructor_type) != RECORD_TYPE
6114 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6115 return;
6117 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6118 add_pending_init (index, value);
6119 constructor_elements = 0;
6120 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6122 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6123 /* Skip any nameless bit fields at the beginning. */
6124 while (constructor_unfilled_fields != 0
6125 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6126 && DECL_NAME (constructor_unfilled_fields) == 0)
6127 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6130 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6132 if (TYPE_DOMAIN (constructor_type))
6133 constructor_unfilled_index
6134 = convert (bitsizetype,
6135 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6136 else
6137 constructor_unfilled_index = bitsize_zero_node;
6139 constructor_incremental = 0;
6142 /* Build AVL tree from a string constant. */
6144 static void
6145 set_nonincremental_init_from_string (tree str)
6147 tree value, purpose, type;
6148 HOST_WIDE_INT val[2];
6149 const char *p, *end;
6150 int byte, wchar_bytes, charwidth, bitpos;
6152 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6154 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
6155 charwidth = TYPE_PRECISION (char_type_node);
6156 type = TREE_TYPE (constructor_type);
6157 p = TREE_STRING_POINTER (str);
6158 end = p + TREE_STRING_LENGTH (str);
6160 for (purpose = bitsize_zero_node;
6161 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6162 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6164 if (wchar_bytes == 1)
6166 val[1] = (unsigned char) *p++;
6167 val[0] = 0;
6169 else
6171 val[0] = 0;
6172 val[1] = 0;
6173 for (byte = 0; byte < wchar_bytes; byte++)
6175 if (BYTES_BIG_ENDIAN)
6176 bitpos = (wchar_bytes - byte - 1) * charwidth;
6177 else
6178 bitpos = byte * charwidth;
6179 val[bitpos < HOST_BITS_PER_WIDE_INT]
6180 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6181 << (bitpos % HOST_BITS_PER_WIDE_INT);
6185 if (!TYPE_UNSIGNED (type))
6187 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6188 if (bitpos < HOST_BITS_PER_WIDE_INT)
6190 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6192 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6193 val[0] = -1;
6196 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6198 if (val[1] < 0)
6199 val[0] = -1;
6201 else if (val[0] & (((HOST_WIDE_INT) 1)
6202 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6203 val[0] |= ((HOST_WIDE_INT) -1)
6204 << (bitpos - HOST_BITS_PER_WIDE_INT);
6207 value = build_int_cst_wide (type, val[1], val[0]);
6208 add_pending_init (purpose, value);
6211 constructor_incremental = 0;
6214 /* Return value of FIELD in pending initializer or zero if the field was
6215 not initialized yet. */
6217 static tree
6218 find_init_member (tree field)
6220 struct init_node *p;
6222 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6224 if (constructor_incremental
6225 && tree_int_cst_lt (field, constructor_unfilled_index))
6226 set_nonincremental_init ();
6228 p = constructor_pending_elts;
6229 while (p)
6231 if (tree_int_cst_lt (field, p->purpose))
6232 p = p->left;
6233 else if (tree_int_cst_lt (p->purpose, field))
6234 p = p->right;
6235 else
6236 return p->value;
6239 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6241 tree bitpos = bit_position (field);
6243 if (constructor_incremental
6244 && (!constructor_unfilled_fields
6245 || tree_int_cst_lt (bitpos,
6246 bit_position (constructor_unfilled_fields))))
6247 set_nonincremental_init ();
6249 p = constructor_pending_elts;
6250 while (p)
6252 if (field == p->purpose)
6253 return p->value;
6254 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6255 p = p->left;
6256 else
6257 p = p->right;
6260 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6262 if (!VEC_empty (constructor_elt, constructor_elements)
6263 && (VEC_last (constructor_elt, constructor_elements)->index
6264 == field))
6265 return VEC_last (constructor_elt, constructor_elements)->value;
6267 return 0;
6270 /* "Output" the next constructor element.
6271 At top level, really output it to assembler code now.
6272 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6273 TYPE is the data type that the containing data type wants here.
6274 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6275 If VALUE is a string constant, STRICT_STRING is true if it is
6276 unparenthesized or we should not warn here for it being parenthesized.
6277 For other types of VALUE, STRICT_STRING is not used.
6279 PENDING if non-nil means output pending elements that belong
6280 right after this element. (PENDING is normally 1;
6281 it is 0 while outputting pending elements, to avoid recursion.) */
6283 static void
6284 output_init_element (tree value, bool strict_string, tree type, tree field,
6285 int pending)
6287 constructor_elt *celt;
6289 if (type == error_mark_node || value == error_mark_node)
6291 constructor_erroneous = 1;
6292 return;
6294 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6295 && (TREE_CODE (value) == STRING_CST
6296 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6297 && !(TREE_CODE (value) == STRING_CST
6298 && TREE_CODE (type) == ARRAY_TYPE
6299 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6300 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6301 TYPE_MAIN_VARIANT (type)))
6302 value = array_to_pointer_conversion (value);
6304 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6305 && require_constant_value && !flag_isoc99 && pending)
6307 /* As an extension, allow initializing objects with static storage
6308 duration with compound literals (which are then treated just as
6309 the brace enclosed list they contain). */
6310 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6311 value = DECL_INITIAL (decl);
6314 if (value == error_mark_node)
6315 constructor_erroneous = 1;
6316 else if (!TREE_CONSTANT (value))
6317 constructor_constant = 0;
6318 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6319 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6320 || TREE_CODE (constructor_type) == UNION_TYPE)
6321 && DECL_C_BIT_FIELD (field)
6322 && TREE_CODE (value) != INTEGER_CST))
6323 constructor_simple = 0;
6325 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6327 if (require_constant_value)
6329 error_init ("initializer element is not constant");
6330 value = error_mark_node;
6332 else if (require_constant_elements)
6333 pedwarn (input_location, 0,
6334 "initializer element is not computable at load time");
6337 /* If this field is empty (and not at the end of structure),
6338 don't do anything other than checking the initializer. */
6339 if (field
6340 && (TREE_TYPE (field) == error_mark_node
6341 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6342 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6343 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6344 || TREE_CHAIN (field)))))
6345 return;
6347 value = digest_init (type, value, strict_string, require_constant_value);
6348 if (value == error_mark_node)
6350 constructor_erroneous = 1;
6351 return;
6354 /* If this element doesn't come next in sequence,
6355 put it on constructor_pending_elts. */
6356 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6357 && (!constructor_incremental
6358 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6360 if (constructor_incremental
6361 && tree_int_cst_lt (field, constructor_unfilled_index))
6362 set_nonincremental_init ();
6364 add_pending_init (field, value);
6365 return;
6367 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6368 && (!constructor_incremental
6369 || field != constructor_unfilled_fields))
6371 /* We do this for records but not for unions. In a union,
6372 no matter which field is specified, it can be initialized
6373 right away since it starts at the beginning of the union. */
6374 if (constructor_incremental)
6376 if (!constructor_unfilled_fields)
6377 set_nonincremental_init ();
6378 else
6380 tree bitpos, unfillpos;
6382 bitpos = bit_position (field);
6383 unfillpos = bit_position (constructor_unfilled_fields);
6385 if (tree_int_cst_lt (bitpos, unfillpos))
6386 set_nonincremental_init ();
6390 add_pending_init (field, value);
6391 return;
6393 else if (TREE_CODE (constructor_type) == UNION_TYPE
6394 && !VEC_empty (constructor_elt, constructor_elements))
6396 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6397 constructor_elements)->value))
6398 warning_init (0, "initialized field with side-effects overwritten");
6399 else if (warn_override_init)
6400 warning_init (OPT_Woverride_init, "initialized field overwritten");
6402 /* We can have just one union field set. */
6403 constructor_elements = 0;
6406 /* Otherwise, output this element either to
6407 constructor_elements or to the assembler file. */
6409 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6410 celt->index = field;
6411 celt->value = value;
6413 /* Advance the variable that indicates sequential elements output. */
6414 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6415 constructor_unfilled_index
6416 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6417 bitsize_one_node);
6418 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6420 constructor_unfilled_fields
6421 = TREE_CHAIN (constructor_unfilled_fields);
6423 /* Skip any nameless bit fields. */
6424 while (constructor_unfilled_fields != 0
6425 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6426 && DECL_NAME (constructor_unfilled_fields) == 0)
6427 constructor_unfilled_fields =
6428 TREE_CHAIN (constructor_unfilled_fields);
6430 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6431 constructor_unfilled_fields = 0;
6433 /* Now output any pending elements which have become next. */
6434 if (pending)
6435 output_pending_init_elements (0);
6438 /* Output any pending elements which have become next.
6439 As we output elements, constructor_unfilled_{fields,index}
6440 advances, which may cause other elements to become next;
6441 if so, they too are output.
6443 If ALL is 0, we return when there are
6444 no more pending elements to output now.
6446 If ALL is 1, we output space as necessary so that
6447 we can output all the pending elements. */
6449 static void
6450 output_pending_init_elements (int all)
6452 struct init_node *elt = constructor_pending_elts;
6453 tree next;
6455 retry:
6457 /* Look through the whole pending tree.
6458 If we find an element that should be output now,
6459 output it. Otherwise, set NEXT to the element
6460 that comes first among those still pending. */
6462 next = 0;
6463 while (elt)
6465 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6467 if (tree_int_cst_equal (elt->purpose,
6468 constructor_unfilled_index))
6469 output_init_element (elt->value, true,
6470 TREE_TYPE (constructor_type),
6471 constructor_unfilled_index, 0);
6472 else if (tree_int_cst_lt (constructor_unfilled_index,
6473 elt->purpose))
6475 /* Advance to the next smaller node. */
6476 if (elt->left)
6477 elt = elt->left;
6478 else
6480 /* We have reached the smallest node bigger than the
6481 current unfilled index. Fill the space first. */
6482 next = elt->purpose;
6483 break;
6486 else
6488 /* Advance to the next bigger node. */
6489 if (elt->right)
6490 elt = elt->right;
6491 else
6493 /* We have reached the biggest node in a subtree. Find
6494 the parent of it, which is the next bigger node. */
6495 while (elt->parent && elt->parent->right == elt)
6496 elt = elt->parent;
6497 elt = elt->parent;
6498 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6499 elt->purpose))
6501 next = elt->purpose;
6502 break;
6507 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6508 || TREE_CODE (constructor_type) == UNION_TYPE)
6510 tree ctor_unfilled_bitpos, elt_bitpos;
6512 /* If the current record is complete we are done. */
6513 if (constructor_unfilled_fields == 0)
6514 break;
6516 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6517 elt_bitpos = bit_position (elt->purpose);
6518 /* We can't compare fields here because there might be empty
6519 fields in between. */
6520 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6522 constructor_unfilled_fields = elt->purpose;
6523 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6524 elt->purpose, 0);
6526 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6528 /* Advance to the next smaller node. */
6529 if (elt->left)
6530 elt = elt->left;
6531 else
6533 /* We have reached the smallest node bigger than the
6534 current unfilled field. Fill the space first. */
6535 next = elt->purpose;
6536 break;
6539 else
6541 /* Advance to the next bigger node. */
6542 if (elt->right)
6543 elt = elt->right;
6544 else
6546 /* We have reached the biggest node in a subtree. Find
6547 the parent of it, which is the next bigger node. */
6548 while (elt->parent && elt->parent->right == elt)
6549 elt = elt->parent;
6550 elt = elt->parent;
6551 if (elt
6552 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6553 bit_position (elt->purpose))))
6555 next = elt->purpose;
6556 break;
6563 /* Ordinarily return, but not if we want to output all
6564 and there are elements left. */
6565 if (!(all && next != 0))
6566 return;
6568 /* If it's not incremental, just skip over the gap, so that after
6569 jumping to retry we will output the next successive element. */
6570 if (TREE_CODE (constructor_type) == RECORD_TYPE
6571 || TREE_CODE (constructor_type) == UNION_TYPE)
6572 constructor_unfilled_fields = next;
6573 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6574 constructor_unfilled_index = next;
6576 /* ELT now points to the node in the pending tree with the next
6577 initializer to output. */
6578 goto retry;
6581 /* Add one non-braced element to the current constructor level.
6582 This adjusts the current position within the constructor's type.
6583 This may also start or terminate implicit levels
6584 to handle a partly-braced initializer.
6586 Once this has found the correct level for the new element,
6587 it calls output_init_element. */
6589 void
6590 process_init_element (struct c_expr value)
6592 tree orig_value = value.value;
6593 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6594 bool strict_string = value.original_code == STRING_CST;
6596 designator_depth = 0;
6597 designator_erroneous = 0;
6599 /* Handle superfluous braces around string cst as in
6600 char x[] = {"foo"}; */
6601 if (string_flag
6602 && constructor_type
6603 && TREE_CODE (constructor_type) == ARRAY_TYPE
6604 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6605 && integer_zerop (constructor_unfilled_index))
6607 if (constructor_stack->replacement_value.value)
6608 error_init ("excess elements in char array initializer");
6609 constructor_stack->replacement_value = value;
6610 return;
6613 if (constructor_stack->replacement_value.value != 0)
6615 error_init ("excess elements in struct initializer");
6616 return;
6619 /* Ignore elements of a brace group if it is entirely superfluous
6620 and has already been diagnosed. */
6621 if (constructor_type == 0)
6622 return;
6624 /* If we've exhausted any levels that didn't have braces,
6625 pop them now. */
6626 while (constructor_stack->implicit)
6628 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6629 || TREE_CODE (constructor_type) == UNION_TYPE)
6630 && constructor_fields == 0)
6631 process_init_element (pop_init_level (1));
6632 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6633 && (constructor_max_index == 0
6634 || tree_int_cst_lt (constructor_max_index,
6635 constructor_index)))
6636 process_init_element (pop_init_level (1));
6637 else
6638 break;
6641 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6642 if (constructor_range_stack)
6644 /* If value is a compound literal and we'll be just using its
6645 content, don't put it into a SAVE_EXPR. */
6646 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6647 || !require_constant_value
6648 || flag_isoc99)
6649 value.value = save_expr (value.value);
6652 while (1)
6654 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6656 tree fieldtype;
6657 enum tree_code fieldcode;
6659 if (constructor_fields == 0)
6661 pedwarn_init (input_location, 0,
6662 "excess elements in struct initializer");
6663 break;
6666 fieldtype = TREE_TYPE (constructor_fields);
6667 if (fieldtype != error_mark_node)
6668 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6669 fieldcode = TREE_CODE (fieldtype);
6671 /* Error for non-static initialization of a flexible array member. */
6672 if (fieldcode == ARRAY_TYPE
6673 && !require_constant_value
6674 && TYPE_SIZE (fieldtype) == NULL_TREE
6675 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6677 error_init ("non-static initialization of a flexible array member");
6678 break;
6681 /* Accept a string constant to initialize a subarray. */
6682 if (value.value != 0
6683 && fieldcode == ARRAY_TYPE
6684 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6685 && string_flag)
6686 value.value = orig_value;
6687 /* Otherwise, if we have come to a subaggregate,
6688 and we don't have an element of its type, push into it. */
6689 else if (value.value != 0
6690 && value.value != error_mark_node
6691 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6692 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6693 || fieldcode == UNION_TYPE))
6695 push_init_level (1);
6696 continue;
6699 if (value.value)
6701 push_member_name (constructor_fields);
6702 output_init_element (value.value, strict_string,
6703 fieldtype, constructor_fields, 1);
6704 RESTORE_SPELLING_DEPTH (constructor_depth);
6706 else
6707 /* Do the bookkeeping for an element that was
6708 directly output as a constructor. */
6710 /* For a record, keep track of end position of last field. */
6711 if (DECL_SIZE (constructor_fields))
6712 constructor_bit_index
6713 = size_binop (PLUS_EXPR,
6714 bit_position (constructor_fields),
6715 DECL_SIZE (constructor_fields));
6717 /* If the current field was the first one not yet written out,
6718 it isn't now, so update. */
6719 if (constructor_unfilled_fields == constructor_fields)
6721 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6722 /* Skip any nameless bit fields. */
6723 while (constructor_unfilled_fields != 0
6724 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6725 && DECL_NAME (constructor_unfilled_fields) == 0)
6726 constructor_unfilled_fields =
6727 TREE_CHAIN (constructor_unfilled_fields);
6731 constructor_fields = TREE_CHAIN (constructor_fields);
6732 /* Skip any nameless bit fields at the beginning. */
6733 while (constructor_fields != 0
6734 && DECL_C_BIT_FIELD (constructor_fields)
6735 && DECL_NAME (constructor_fields) == 0)
6736 constructor_fields = TREE_CHAIN (constructor_fields);
6738 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6740 tree fieldtype;
6741 enum tree_code fieldcode;
6743 if (constructor_fields == 0)
6745 pedwarn_init (input_location, 0,
6746 "excess elements in union initializer");
6747 break;
6750 fieldtype = TREE_TYPE (constructor_fields);
6751 if (fieldtype != error_mark_node)
6752 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6753 fieldcode = TREE_CODE (fieldtype);
6755 /* Warn that traditional C rejects initialization of unions.
6756 We skip the warning if the value is zero. This is done
6757 under the assumption that the zero initializer in user
6758 code appears conditioned on e.g. __STDC__ to avoid
6759 "missing initializer" warnings and relies on default
6760 initialization to zero in the traditional C case.
6761 We also skip the warning if the initializer is designated,
6762 again on the assumption that this must be conditional on
6763 __STDC__ anyway (and we've already complained about the
6764 member-designator already). */
6765 if (!in_system_header && !constructor_designated
6766 && !(value.value && (integer_zerop (value.value)
6767 || real_zerop (value.value))))
6768 warning (OPT_Wtraditional, "traditional C rejects initialization "
6769 "of unions");
6771 /* Accept a string constant to initialize a subarray. */
6772 if (value.value != 0
6773 && fieldcode == ARRAY_TYPE
6774 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6775 && string_flag)
6776 value.value = orig_value;
6777 /* Otherwise, if we have come to a subaggregate,
6778 and we don't have an element of its type, push into it. */
6779 else if (value.value != 0
6780 && value.value != error_mark_node
6781 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6782 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6783 || fieldcode == UNION_TYPE))
6785 push_init_level (1);
6786 continue;
6789 if (value.value)
6791 push_member_name (constructor_fields);
6792 output_init_element (value.value, strict_string,
6793 fieldtype, constructor_fields, 1);
6794 RESTORE_SPELLING_DEPTH (constructor_depth);
6796 else
6797 /* Do the bookkeeping for an element that was
6798 directly output as a constructor. */
6800 constructor_bit_index = DECL_SIZE (constructor_fields);
6801 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6804 constructor_fields = 0;
6806 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6808 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6809 enum tree_code eltcode = TREE_CODE (elttype);
6811 /* Accept a string constant to initialize a subarray. */
6812 if (value.value != 0
6813 && eltcode == ARRAY_TYPE
6814 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6815 && string_flag)
6816 value.value = orig_value;
6817 /* Otherwise, if we have come to a subaggregate,
6818 and we don't have an element of its type, push into it. */
6819 else if (value.value != 0
6820 && value.value != error_mark_node
6821 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6822 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6823 || eltcode == UNION_TYPE))
6825 push_init_level (1);
6826 continue;
6829 if (constructor_max_index != 0
6830 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6831 || integer_all_onesp (constructor_max_index)))
6833 pedwarn_init (input_location, 0,
6834 "excess elements in array initializer");
6835 break;
6838 /* Now output the actual element. */
6839 if (value.value)
6841 push_array_bounds (tree_low_cst (constructor_index, 1));
6842 output_init_element (value.value, strict_string,
6843 elttype, constructor_index, 1);
6844 RESTORE_SPELLING_DEPTH (constructor_depth);
6847 constructor_index
6848 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6850 if (!value.value)
6851 /* If we are doing the bookkeeping for an element that was
6852 directly output as a constructor, we must update
6853 constructor_unfilled_index. */
6854 constructor_unfilled_index = constructor_index;
6856 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6858 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6860 /* Do a basic check of initializer size. Note that vectors
6861 always have a fixed size derived from their type. */
6862 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6864 pedwarn_init (input_location, 0,
6865 "excess elements in vector initializer");
6866 break;
6869 /* Now output the actual element. */
6870 if (value.value)
6871 output_init_element (value.value, strict_string,
6872 elttype, constructor_index, 1);
6874 constructor_index
6875 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6877 if (!value.value)
6878 /* If we are doing the bookkeeping for an element that was
6879 directly output as a constructor, we must update
6880 constructor_unfilled_index. */
6881 constructor_unfilled_index = constructor_index;
6884 /* Handle the sole element allowed in a braced initializer
6885 for a scalar variable. */
6886 else if (constructor_type != error_mark_node
6887 && constructor_fields == 0)
6889 pedwarn_init (input_location, 0,
6890 "excess elements in scalar initializer");
6891 break;
6893 else
6895 if (value.value)
6896 output_init_element (value.value, strict_string,
6897 constructor_type, NULL_TREE, 1);
6898 constructor_fields = 0;
6901 /* Handle range initializers either at this level or anywhere higher
6902 in the designator stack. */
6903 if (constructor_range_stack)
6905 struct constructor_range_stack *p, *range_stack;
6906 int finish = 0;
6908 range_stack = constructor_range_stack;
6909 constructor_range_stack = 0;
6910 while (constructor_stack != range_stack->stack)
6912 gcc_assert (constructor_stack->implicit);
6913 process_init_element (pop_init_level (1));
6915 for (p = range_stack;
6916 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6917 p = p->prev)
6919 gcc_assert (constructor_stack->implicit);
6920 process_init_element (pop_init_level (1));
6923 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6924 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6925 finish = 1;
6927 while (1)
6929 constructor_index = p->index;
6930 constructor_fields = p->fields;
6931 if (finish && p->range_end && p->index == p->range_start)
6933 finish = 0;
6934 p->prev = 0;
6936 p = p->next;
6937 if (!p)
6938 break;
6939 push_init_level (2);
6940 p->stack = constructor_stack;
6941 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6942 p->index = p->range_start;
6945 if (!finish)
6946 constructor_range_stack = range_stack;
6947 continue;
6950 break;
6953 constructor_range_stack = 0;
6956 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6957 (guaranteed to be 'volatile' or null) and ARGS (represented using
6958 an ASM_EXPR node). */
6959 tree
6960 build_asm_stmt (tree cv_qualifier, tree args)
6962 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6963 ASM_VOLATILE_P (args) = 1;
6964 return add_stmt (args);
6967 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6968 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6969 SIMPLE indicates whether there was anything at all after the
6970 string in the asm expression -- asm("blah") and asm("blah" : )
6971 are subtly different. We use a ASM_EXPR node to represent this. */
6972 tree
6973 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6974 bool simple)
6976 tree tail;
6977 tree args;
6978 int i;
6979 const char *constraint;
6980 const char **oconstraints;
6981 bool allows_mem, allows_reg, is_inout;
6982 int ninputs, noutputs;
6984 ninputs = list_length (inputs);
6985 noutputs = list_length (outputs);
6986 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6988 string = resolve_asm_operand_names (string, outputs, inputs);
6990 /* Remove output conversions that change the type but not the mode. */
6991 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6993 tree output = TREE_VALUE (tail);
6995 /* ??? Really, this should not be here. Users should be using a
6996 proper lvalue, dammit. But there's a long history of using casts
6997 in the output operands. In cases like longlong.h, this becomes a
6998 primitive form of typechecking -- if the cast can be removed, then
6999 the output operand had a type of the proper width; otherwise we'll
7000 get an error. Gross, but ... */
7001 STRIP_NOPS (output);
7003 if (!lvalue_or_else (output, lv_asm))
7004 output = error_mark_node;
7006 if (output != error_mark_node
7007 && (TREE_READONLY (output)
7008 || TYPE_READONLY (TREE_TYPE (output))
7009 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7010 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7011 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7012 readonly_error (output, lv_asm);
7014 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7015 oconstraints[i] = constraint;
7017 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7018 &allows_mem, &allows_reg, &is_inout))
7020 /* If the operand is going to end up in memory,
7021 mark it addressable. */
7022 if (!allows_reg && !c_mark_addressable (output))
7023 output = error_mark_node;
7025 else
7026 output = error_mark_node;
7028 TREE_VALUE (tail) = output;
7031 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7033 tree input;
7035 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7036 input = TREE_VALUE (tail);
7038 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7039 oconstraints, &allows_mem, &allows_reg))
7041 /* If the operand is going to end up in memory,
7042 mark it addressable. */
7043 if (!allows_reg && allows_mem)
7045 /* Strip the nops as we allow this case. FIXME, this really
7046 should be rejected or made deprecated. */
7047 STRIP_NOPS (input);
7048 if (!c_mark_addressable (input))
7049 input = error_mark_node;
7052 else
7053 input = error_mark_node;
7055 TREE_VALUE (tail) = input;
7058 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7060 /* asm statements without outputs, including simple ones, are treated
7061 as volatile. */
7062 ASM_INPUT_P (args) = simple;
7063 ASM_VOLATILE_P (args) = (noutputs == 0);
7065 return args;
7068 /* Generate a goto statement to LABEL. */
7070 tree
7071 c_finish_goto_label (tree label)
7073 tree decl = lookup_label (label);
7074 if (!decl)
7075 return NULL_TREE;
7077 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7079 error ("jump into statement expression");
7080 return NULL_TREE;
7083 if (C_DECL_UNJUMPABLE_VM (decl))
7085 error ("jump into scope of identifier with variably modified type");
7086 return NULL_TREE;
7089 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7091 /* No jump from outside this statement expression context, so
7092 record that there is a jump from within this context. */
7093 struct c_label_list *nlist;
7094 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7095 nlist->next = label_context_stack_se->labels_used;
7096 nlist->label = decl;
7097 label_context_stack_se->labels_used = nlist;
7100 if (!C_DECL_UNDEFINABLE_VM (decl))
7102 /* No jump from outside this context context of identifiers with
7103 variably modified type, so record that there is a jump from
7104 within this context. */
7105 struct c_label_list *nlist;
7106 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7107 nlist->next = label_context_stack_vm->labels_used;
7108 nlist->label = decl;
7109 label_context_stack_vm->labels_used = nlist;
7112 TREE_USED (decl) = 1;
7113 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7116 /* Generate a computed goto statement to EXPR. */
7118 tree
7119 c_finish_goto_ptr (tree expr)
7121 pedwarn (input_location, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
7122 expr = convert (ptr_type_node, expr);
7123 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7126 /* Generate a C `return' statement. RETVAL is the expression for what
7127 to return, or a null pointer for `return;' with no value. */
7129 tree
7130 c_finish_return (tree retval)
7132 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7133 bool no_warning = false;
7135 if (TREE_THIS_VOLATILE (current_function_decl))
7136 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7138 if (!retval)
7140 current_function_returns_null = 1;
7141 if ((warn_return_type || flag_isoc99)
7142 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7144 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wreturn_type,
7145 "%<return%> with no value, in "
7146 "function returning non-void");
7147 no_warning = true;
7150 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7152 current_function_returns_null = 1;
7153 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7154 pedwarn (input_location, 0,
7155 "%<return%> with a value, in function returning void");
7156 else
7157 pedwarn (input_location, OPT_pedantic, "ISO C forbids "
7158 "%<return%> with expression, in function returning void");
7160 else
7162 tree t = convert_for_assignment (valtype, retval, ic_return,
7163 NULL_TREE, NULL_TREE, 0);
7164 tree res = DECL_RESULT (current_function_decl);
7165 tree inner;
7167 current_function_returns_value = 1;
7168 if (t == error_mark_node)
7169 return NULL_TREE;
7171 inner = t = convert (TREE_TYPE (res), t);
7173 /* Strip any conversions, additions, and subtractions, and see if
7174 we are returning the address of a local variable. Warn if so. */
7175 while (1)
7177 switch (TREE_CODE (inner))
7179 CASE_CONVERT: case NON_LVALUE_EXPR:
7180 case PLUS_EXPR:
7181 inner = TREE_OPERAND (inner, 0);
7182 continue;
7184 case MINUS_EXPR:
7185 /* If the second operand of the MINUS_EXPR has a pointer
7186 type (or is converted from it), this may be valid, so
7187 don't give a warning. */
7189 tree op1 = TREE_OPERAND (inner, 1);
7191 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7192 && (CONVERT_EXPR_P (op1)
7193 || TREE_CODE (op1) == NON_LVALUE_EXPR))
7194 op1 = TREE_OPERAND (op1, 0);
7196 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7197 break;
7199 inner = TREE_OPERAND (inner, 0);
7200 continue;
7203 case ADDR_EXPR:
7204 inner = TREE_OPERAND (inner, 0);
7206 while (REFERENCE_CLASS_P (inner)
7207 && TREE_CODE (inner) != INDIRECT_REF)
7208 inner = TREE_OPERAND (inner, 0);
7210 if (DECL_P (inner)
7211 && !DECL_EXTERNAL (inner)
7212 && !TREE_STATIC (inner)
7213 && DECL_CONTEXT (inner) == current_function_decl)
7214 warning (0, "function returns address of local variable");
7215 break;
7217 default:
7218 break;
7221 break;
7224 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7226 if (warn_sequence_point)
7227 verify_sequence_points (retval);
7230 ret_stmt = build_stmt (RETURN_EXPR, retval);
7231 TREE_NO_WARNING (ret_stmt) |= no_warning;
7232 return add_stmt (ret_stmt);
7235 struct c_switch {
7236 /* The SWITCH_EXPR being built. */
7237 tree switch_expr;
7239 /* The original type of the testing expression, i.e. before the
7240 default conversion is applied. */
7241 tree orig_type;
7243 /* A splay-tree mapping the low element of a case range to the high
7244 element, or NULL_TREE if there is no high element. Used to
7245 determine whether or not a new case label duplicates an old case
7246 label. We need a tree, rather than simply a hash table, because
7247 of the GNU case range extension. */
7248 splay_tree cases;
7250 /* Number of nested statement expressions within this switch
7251 statement; if nonzero, case and default labels may not
7252 appear. */
7253 unsigned int blocked_stmt_expr;
7255 /* Scope of outermost declarations of identifiers with variably
7256 modified type within this switch statement; if nonzero, case and
7257 default labels may not appear. */
7258 unsigned int blocked_vm;
7260 /* The next node on the stack. */
7261 struct c_switch *next;
7264 /* A stack of the currently active switch statements. The innermost
7265 switch statement is on the top of the stack. There is no need to
7266 mark the stack for garbage collection because it is only active
7267 during the processing of the body of a function, and we never
7268 collect at that point. */
7270 struct c_switch *c_switch_stack;
7272 /* Start a C switch statement, testing expression EXP. Return the new
7273 SWITCH_EXPR. */
7275 tree
7276 c_start_case (tree exp)
7278 tree orig_type = error_mark_node;
7279 struct c_switch *cs;
7281 if (exp != error_mark_node)
7283 orig_type = TREE_TYPE (exp);
7285 if (!INTEGRAL_TYPE_P (orig_type))
7287 if (orig_type != error_mark_node)
7289 error ("switch quantity not an integer");
7290 orig_type = error_mark_node;
7292 exp = integer_zero_node;
7294 else
7296 tree type = TYPE_MAIN_VARIANT (orig_type);
7298 if (!in_system_header
7299 && (type == long_integer_type_node
7300 || type == long_unsigned_type_node))
7301 warning (OPT_Wtraditional, "%<long%> switch expression not "
7302 "converted to %<int%> in ISO C");
7304 exp = default_conversion (exp);
7306 if (warn_sequence_point)
7307 verify_sequence_points (exp);
7311 /* Add this new SWITCH_EXPR to the stack. */
7312 cs = XNEW (struct c_switch);
7313 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7314 cs->orig_type = orig_type;
7315 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7316 cs->blocked_stmt_expr = 0;
7317 cs->blocked_vm = 0;
7318 cs->next = c_switch_stack;
7319 c_switch_stack = cs;
7321 return add_stmt (cs->switch_expr);
7324 /* Process a case label. */
7326 tree
7327 do_case (tree low_value, tree high_value)
7329 tree label = NULL_TREE;
7331 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7332 && !c_switch_stack->blocked_vm)
7334 label = c_add_case_label (c_switch_stack->cases,
7335 SWITCH_COND (c_switch_stack->switch_expr),
7336 c_switch_stack->orig_type,
7337 low_value, high_value);
7338 if (label == error_mark_node)
7339 label = NULL_TREE;
7341 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7343 if (low_value)
7344 error ("case label in statement expression not containing "
7345 "enclosing switch statement");
7346 else
7347 error ("%<default%> label in statement expression not containing "
7348 "enclosing switch statement");
7350 else if (c_switch_stack && c_switch_stack->blocked_vm)
7352 if (low_value)
7353 error ("case label in scope of identifier with variably modified "
7354 "type not containing enclosing switch statement");
7355 else
7356 error ("%<default%> label in scope of identifier with variably "
7357 "modified type not containing enclosing switch statement");
7359 else if (low_value)
7360 error ("case label not within a switch statement");
7361 else
7362 error ("%<default%> label not within a switch statement");
7364 return label;
7367 /* Finish the switch statement. */
7369 void
7370 c_finish_case (tree body)
7372 struct c_switch *cs = c_switch_stack;
7373 location_t switch_location;
7375 SWITCH_BODY (cs->switch_expr) = body;
7377 /* We must not be within a statement expression nested in the switch
7378 at this point; we might, however, be within the scope of an
7379 identifier with variably modified type nested in the switch. */
7380 gcc_assert (!cs->blocked_stmt_expr);
7382 /* Emit warnings as needed. */
7383 if (EXPR_HAS_LOCATION (cs->switch_expr))
7384 switch_location = EXPR_LOCATION (cs->switch_expr);
7385 else
7386 switch_location = input_location;
7387 c_do_switch_warnings (cs->cases, switch_location,
7388 TREE_TYPE (cs->switch_expr),
7389 SWITCH_COND (cs->switch_expr));
7391 /* Pop the stack. */
7392 c_switch_stack = cs->next;
7393 splay_tree_delete (cs->cases);
7394 XDELETE (cs);
7397 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7398 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7399 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7400 statement, and was not surrounded with parenthesis. */
7402 void
7403 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7404 tree else_block, bool nested_if)
7406 tree stmt;
7408 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7409 if (warn_parentheses && nested_if && else_block == NULL)
7411 tree inner_if = then_block;
7413 /* We know from the grammar productions that there is an IF nested
7414 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7415 it might not be exactly THEN_BLOCK, but should be the last
7416 non-container statement within. */
7417 while (1)
7418 switch (TREE_CODE (inner_if))
7420 case COND_EXPR:
7421 goto found;
7422 case BIND_EXPR:
7423 inner_if = BIND_EXPR_BODY (inner_if);
7424 break;
7425 case STATEMENT_LIST:
7426 inner_if = expr_last (then_block);
7427 break;
7428 case TRY_FINALLY_EXPR:
7429 case TRY_CATCH_EXPR:
7430 inner_if = TREE_OPERAND (inner_if, 0);
7431 break;
7432 default:
7433 gcc_unreachable ();
7435 found:
7437 if (COND_EXPR_ELSE (inner_if))
7438 warning (OPT_Wparentheses,
7439 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7440 &if_locus);
7443 empty_if_body_warning (then_block, else_block);
7445 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7446 SET_EXPR_LOCATION (stmt, if_locus);
7447 add_stmt (stmt);
7450 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7451 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7452 is false for DO loops. INCR is the FOR increment expression. BODY is
7453 the statement controlled by the loop. BLAB is the break label. CLAB is
7454 the continue label. Everything is allowed to be NULL. */
7456 void
7457 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7458 tree blab, tree clab, bool cond_is_first)
7460 tree entry = NULL, exit = NULL, t;
7462 /* If the condition is zero don't generate a loop construct. */
7463 if (cond && integer_zerop (cond))
7465 if (cond_is_first)
7467 t = build_and_jump (&blab);
7468 SET_EXPR_LOCATION (t, start_locus);
7469 add_stmt (t);
7472 else
7474 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7476 /* If we have an exit condition, then we build an IF with gotos either
7477 out of the loop, or to the top of it. If there's no exit condition,
7478 then we just build a jump back to the top. */
7479 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7481 if (cond && !integer_nonzerop (cond))
7483 /* Canonicalize the loop condition to the end. This means
7484 generating a branch to the loop condition. Reuse the
7485 continue label, if possible. */
7486 if (cond_is_first)
7488 if (incr || !clab)
7490 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7491 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7493 else
7494 t = build1 (GOTO_EXPR, void_type_node, clab);
7495 SET_EXPR_LOCATION (t, start_locus);
7496 add_stmt (t);
7499 t = build_and_jump (&blab);
7500 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7501 if (cond_is_first)
7502 SET_EXPR_LOCATION (exit, start_locus);
7503 else
7504 SET_EXPR_LOCATION (exit, input_location);
7507 add_stmt (top);
7510 if (body)
7511 add_stmt (body);
7512 if (clab)
7513 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7514 if (incr)
7515 add_stmt (incr);
7516 if (entry)
7517 add_stmt (entry);
7518 if (exit)
7519 add_stmt (exit);
7520 if (blab)
7521 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7524 tree
7525 c_finish_bc_stmt (tree *label_p, bool is_break)
7527 bool skip;
7528 tree label = *label_p;
7530 /* In switch statements break is sometimes stylistically used after
7531 a return statement. This can lead to spurious warnings about
7532 control reaching the end of a non-void function when it is
7533 inlined. Note that we are calling block_may_fallthru with
7534 language specific tree nodes; this works because
7535 block_may_fallthru returns true when given something it does not
7536 understand. */
7537 skip = !block_may_fallthru (cur_stmt_list);
7539 if (!label)
7541 if (!skip)
7542 *label_p = label = create_artificial_label ();
7544 else if (TREE_CODE (label) == LABEL_DECL)
7546 else switch (TREE_INT_CST_LOW (label))
7548 case 0:
7549 if (is_break)
7550 error ("break statement not within loop or switch");
7551 else
7552 error ("continue statement not within a loop");
7553 return NULL_TREE;
7555 case 1:
7556 gcc_assert (is_break);
7557 error ("break statement used with OpenMP for loop");
7558 return NULL_TREE;
7560 default:
7561 gcc_unreachable ();
7564 if (skip)
7565 return NULL_TREE;
7567 if (!is_break)
7568 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
7570 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7573 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7575 static void
7576 emit_side_effect_warnings (tree expr)
7578 if (expr == error_mark_node)
7580 else if (!TREE_SIDE_EFFECTS (expr))
7582 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7583 warning (OPT_Wunused_value, "%Hstatement with no effect",
7584 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7586 else
7587 warn_if_unused_value (expr, input_location);
7590 /* Process an expression as if it were a complete statement. Emit
7591 diagnostics, but do not call ADD_STMT. */
7593 tree
7594 c_process_expr_stmt (tree expr)
7596 if (!expr)
7597 return NULL_TREE;
7599 if (warn_sequence_point)
7600 verify_sequence_points (expr);
7602 if (TREE_TYPE (expr) != error_mark_node
7603 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7604 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7605 error ("expression statement has incomplete type");
7607 /* If we're not processing a statement expression, warn about unused values.
7608 Warnings for statement expressions will be emitted later, once we figure
7609 out which is the result. */
7610 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7611 && warn_unused_value)
7612 emit_side_effect_warnings (expr);
7614 /* If the expression is not of a type to which we cannot assign a line
7615 number, wrap the thing in a no-op NOP_EXPR. */
7616 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7617 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7619 if (CAN_HAVE_LOCATION_P (expr))
7620 SET_EXPR_LOCATION (expr, input_location);
7622 return expr;
7625 /* Emit an expression as a statement. */
7627 tree
7628 c_finish_expr_stmt (tree expr)
7630 if (expr)
7631 return add_stmt (c_process_expr_stmt (expr));
7632 else
7633 return NULL;
7636 /* Do the opposite and emit a statement as an expression. To begin,
7637 create a new binding level and return it. */
7639 tree
7640 c_begin_stmt_expr (void)
7642 tree ret;
7643 struct c_label_context_se *nstack;
7644 struct c_label_list *glist;
7646 /* We must force a BLOCK for this level so that, if it is not expanded
7647 later, there is a way to turn off the entire subtree of blocks that
7648 are contained in it. */
7649 keep_next_level ();
7650 ret = c_begin_compound_stmt (true);
7651 if (c_switch_stack)
7653 c_switch_stack->blocked_stmt_expr++;
7654 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7656 for (glist = label_context_stack_se->labels_used;
7657 glist != NULL;
7658 glist = glist->next)
7660 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7662 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7663 nstack->labels_def = NULL;
7664 nstack->labels_used = NULL;
7665 nstack->next = label_context_stack_se;
7666 label_context_stack_se = nstack;
7668 /* Mark the current statement list as belonging to a statement list. */
7669 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7671 return ret;
7674 tree
7675 c_finish_stmt_expr (tree body)
7677 tree last, type, tmp, val;
7678 tree *last_p;
7679 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7681 body = c_end_compound_stmt (body, true);
7682 if (c_switch_stack)
7684 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7685 c_switch_stack->blocked_stmt_expr--;
7687 /* It is no longer possible to jump to labels defined within this
7688 statement expression. */
7689 for (dlist = label_context_stack_se->labels_def;
7690 dlist != NULL;
7691 dlist = dlist->next)
7693 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7695 /* It is again possible to define labels with a goto just outside
7696 this statement expression. */
7697 for (glist = label_context_stack_se->next->labels_used;
7698 glist != NULL;
7699 glist = glist->next)
7701 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7702 glist_prev = glist;
7704 if (glist_prev != NULL)
7705 glist_prev->next = label_context_stack_se->labels_used;
7706 else
7707 label_context_stack_se->next->labels_used
7708 = label_context_stack_se->labels_used;
7709 label_context_stack_se = label_context_stack_se->next;
7711 /* Locate the last statement in BODY. See c_end_compound_stmt
7712 about always returning a BIND_EXPR. */
7713 last_p = &BIND_EXPR_BODY (body);
7714 last = BIND_EXPR_BODY (body);
7716 continue_searching:
7717 if (TREE_CODE (last) == STATEMENT_LIST)
7719 tree_stmt_iterator i;
7721 /* This can happen with degenerate cases like ({ }). No value. */
7722 if (!TREE_SIDE_EFFECTS (last))
7723 return body;
7725 /* If we're supposed to generate side effects warnings, process
7726 all of the statements except the last. */
7727 if (warn_unused_value)
7729 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7730 emit_side_effect_warnings (tsi_stmt (i));
7732 else
7733 i = tsi_last (last);
7734 last_p = tsi_stmt_ptr (i);
7735 last = *last_p;
7738 /* If the end of the list is exception related, then the list was split
7739 by a call to push_cleanup. Continue searching. */
7740 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7741 || TREE_CODE (last) == TRY_CATCH_EXPR)
7743 last_p = &TREE_OPERAND (last, 0);
7744 last = *last_p;
7745 goto continue_searching;
7748 /* In the case that the BIND_EXPR is not necessary, return the
7749 expression out from inside it. */
7750 if (last == error_mark_node
7751 || (last == BIND_EXPR_BODY (body)
7752 && BIND_EXPR_VARS (body) == NULL))
7754 /* Do not warn if the return value of a statement expression is
7755 unused. */
7756 if (CAN_HAVE_LOCATION_P (last))
7757 TREE_NO_WARNING (last) = 1;
7758 return last;
7761 /* Extract the type of said expression. */
7762 type = TREE_TYPE (last);
7764 /* If we're not returning a value at all, then the BIND_EXPR that
7765 we already have is a fine expression to return. */
7766 if (!type || VOID_TYPE_P (type))
7767 return body;
7769 /* Now that we've located the expression containing the value, it seems
7770 silly to make voidify_wrapper_expr repeat the process. Create a
7771 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7772 tmp = create_tmp_var_raw (type, NULL);
7774 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7775 tree_expr_nonnegative_p giving up immediately. */
7776 val = last;
7777 if (TREE_CODE (val) == NOP_EXPR
7778 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7779 val = TREE_OPERAND (val, 0);
7781 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7782 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7784 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7787 /* Begin the scope of an identifier of variably modified type, scope
7788 number SCOPE. Jumping from outside this scope to inside it is not
7789 permitted. */
7791 void
7792 c_begin_vm_scope (unsigned int scope)
7794 struct c_label_context_vm *nstack;
7795 struct c_label_list *glist;
7797 gcc_assert (scope > 0);
7799 /* At file_scope, we don't have to do any processing. */
7800 if (label_context_stack_vm == NULL)
7801 return;
7803 if (c_switch_stack && !c_switch_stack->blocked_vm)
7804 c_switch_stack->blocked_vm = scope;
7805 for (glist = label_context_stack_vm->labels_used;
7806 glist != NULL;
7807 glist = glist->next)
7809 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7811 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7812 nstack->labels_def = NULL;
7813 nstack->labels_used = NULL;
7814 nstack->scope = scope;
7815 nstack->next = label_context_stack_vm;
7816 label_context_stack_vm = nstack;
7819 /* End a scope which may contain identifiers of variably modified
7820 type, scope number SCOPE. */
7822 void
7823 c_end_vm_scope (unsigned int scope)
7825 if (label_context_stack_vm == NULL)
7826 return;
7827 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7828 c_switch_stack->blocked_vm = 0;
7829 /* We may have a number of nested scopes of identifiers with
7830 variably modified type, all at this depth. Pop each in turn. */
7831 while (label_context_stack_vm->scope == scope)
7833 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7835 /* It is no longer possible to jump to labels defined within this
7836 scope. */
7837 for (dlist = label_context_stack_vm->labels_def;
7838 dlist != NULL;
7839 dlist = dlist->next)
7841 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7843 /* It is again possible to define labels with a goto just outside
7844 this scope. */
7845 for (glist = label_context_stack_vm->next->labels_used;
7846 glist != NULL;
7847 glist = glist->next)
7849 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7850 glist_prev = glist;
7852 if (glist_prev != NULL)
7853 glist_prev->next = label_context_stack_vm->labels_used;
7854 else
7855 label_context_stack_vm->next->labels_used
7856 = label_context_stack_vm->labels_used;
7857 label_context_stack_vm = label_context_stack_vm->next;
7861 /* Begin and end compound statements. This is as simple as pushing
7862 and popping new statement lists from the tree. */
7864 tree
7865 c_begin_compound_stmt (bool do_scope)
7867 tree stmt = push_stmt_list ();
7868 if (do_scope)
7869 push_scope ();
7870 return stmt;
7873 tree
7874 c_end_compound_stmt (tree stmt, bool do_scope)
7876 tree block = NULL;
7878 if (do_scope)
7880 if (c_dialect_objc ())
7881 objc_clear_super_receiver ();
7882 block = pop_scope ();
7885 stmt = pop_stmt_list (stmt);
7886 stmt = c_build_bind_expr (block, stmt);
7888 /* If this compound statement is nested immediately inside a statement
7889 expression, then force a BIND_EXPR to be created. Otherwise we'll
7890 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7891 STATEMENT_LISTs merge, and thus we can lose track of what statement
7892 was really last. */
7893 if (cur_stmt_list
7894 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7895 && TREE_CODE (stmt) != BIND_EXPR)
7897 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7898 TREE_SIDE_EFFECTS (stmt) = 1;
7901 return stmt;
7904 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7905 when the current scope is exited. EH_ONLY is true when this is not
7906 meant to apply to normal control flow transfer. */
7908 void
7909 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7911 enum tree_code code;
7912 tree stmt, list;
7913 bool stmt_expr;
7915 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7916 stmt = build_stmt (code, NULL, cleanup);
7917 add_stmt (stmt);
7918 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7919 list = push_stmt_list ();
7920 TREE_OPERAND (stmt, 0) = list;
7921 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7924 /* Build a binary-operation expression without default conversions.
7925 CODE is the kind of expression to build.
7926 LOCATION is the operator's location.
7927 This function differs from `build' in several ways:
7928 the data type of the result is computed and recorded in it,
7929 warnings are generated if arg data types are invalid,
7930 special handling for addition and subtraction of pointers is known,
7931 and some optimization is done (operations on narrow ints
7932 are done in the narrower type when that gives the same result).
7933 Constant folding is also done before the result is returned.
7935 Note that the operands will never have enumeral types, or function
7936 or array types, because either they will have the default conversions
7937 performed or they have both just been converted to some other type in which
7938 the arithmetic is to be done. */
7940 tree
7941 build_binary_op (location_t location, enum tree_code code,
7942 tree orig_op0, tree orig_op1, int convert_p)
7944 tree type0, type1;
7945 enum tree_code code0, code1;
7946 tree op0, op1;
7947 const char *invalid_op_diag;
7949 /* Expression code to give to the expression when it is built.
7950 Normally this is CODE, which is what the caller asked for,
7951 but in some special cases we change it. */
7952 enum tree_code resultcode = code;
7954 /* Data type in which the computation is to be performed.
7955 In the simplest cases this is the common type of the arguments. */
7956 tree result_type = NULL;
7958 /* Nonzero means operands have already been type-converted
7959 in whatever way is necessary.
7960 Zero means they need to be converted to RESULT_TYPE. */
7961 int converted = 0;
7963 /* Nonzero means create the expression with this type, rather than
7964 RESULT_TYPE. */
7965 tree build_type = 0;
7967 /* Nonzero means after finally constructing the expression
7968 convert it to this type. */
7969 tree final_type = 0;
7971 /* Nonzero if this is an operation like MIN or MAX which can
7972 safely be computed in short if both args are promoted shorts.
7973 Also implies COMMON.
7974 -1 indicates a bitwise operation; this makes a difference
7975 in the exact conditions for when it is safe to do the operation
7976 in a narrower mode. */
7977 int shorten = 0;
7979 /* Nonzero if this is a comparison operation;
7980 if both args are promoted shorts, compare the original shorts.
7981 Also implies COMMON. */
7982 int short_compare = 0;
7984 /* Nonzero if this is a right-shift operation, which can be computed on the
7985 original short and then promoted if the operand is a promoted short. */
7986 int short_shift = 0;
7988 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7989 int common = 0;
7991 /* True means types are compatible as far as ObjC is concerned. */
7992 bool objc_ok;
7994 if (location == UNKNOWN_LOCATION)
7995 location = input_location;
7997 if (convert_p)
7999 op0 = default_conversion (orig_op0);
8000 op1 = default_conversion (orig_op1);
8002 else
8004 op0 = orig_op0;
8005 op1 = orig_op1;
8008 type0 = TREE_TYPE (op0);
8009 type1 = TREE_TYPE (op1);
8011 /* The expression codes of the data types of the arguments tell us
8012 whether the arguments are integers, floating, pointers, etc. */
8013 code0 = TREE_CODE (type0);
8014 code1 = TREE_CODE (type1);
8016 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8017 STRIP_TYPE_NOPS (op0);
8018 STRIP_TYPE_NOPS (op1);
8020 /* If an error was already reported for one of the arguments,
8021 avoid reporting another error. */
8023 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8024 return error_mark_node;
8026 if ((invalid_op_diag
8027 = targetm.invalid_binary_op (code, type0, type1)))
8029 error_at (location, invalid_op_diag);
8030 return error_mark_node;
8033 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8035 switch (code)
8037 case PLUS_EXPR:
8038 /* Handle the pointer + int case. */
8039 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8040 return pointer_int_sum (PLUS_EXPR, op0, op1);
8041 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8042 return pointer_int_sum (PLUS_EXPR, op1, op0);
8043 else
8044 common = 1;
8045 break;
8047 case MINUS_EXPR:
8048 /* Subtraction of two similar pointers.
8049 We must subtract them as integers, then divide by object size. */
8050 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8051 && comp_target_types (type0, type1))
8052 return pointer_diff (op0, op1);
8053 /* Handle pointer minus int. Just like pointer plus int. */
8054 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8055 return pointer_int_sum (MINUS_EXPR, op0, op1);
8056 else
8057 common = 1;
8058 break;
8060 case MULT_EXPR:
8061 common = 1;
8062 break;
8064 case TRUNC_DIV_EXPR:
8065 case CEIL_DIV_EXPR:
8066 case FLOOR_DIV_EXPR:
8067 case ROUND_DIV_EXPR:
8068 case EXACT_DIV_EXPR:
8069 warn_for_div_by_zero (op1);
8071 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8072 || code0 == FIXED_POINT_TYPE
8073 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8074 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8075 || code1 == FIXED_POINT_TYPE
8076 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8078 enum tree_code tcode0 = code0, tcode1 = code1;
8080 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8081 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8082 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8083 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8085 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
8086 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
8087 resultcode = RDIV_EXPR;
8088 else
8089 /* Although it would be tempting to shorten always here, that
8090 loses on some targets, since the modulo instruction is
8091 undefined if the quotient can't be represented in the
8092 computation mode. We shorten only if unsigned or if
8093 dividing by something we know != -1. */
8094 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8095 || (TREE_CODE (op1) == INTEGER_CST
8096 && !integer_all_onesp (op1)));
8097 common = 1;
8099 break;
8101 case BIT_AND_EXPR:
8102 case BIT_IOR_EXPR:
8103 case BIT_XOR_EXPR:
8104 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8105 shorten = -1;
8106 /* Allow vector types which are not floating point types. */
8107 else if (code0 == VECTOR_TYPE
8108 && code1 == VECTOR_TYPE
8109 && !VECTOR_FLOAT_TYPE_P (type0)
8110 && !VECTOR_FLOAT_TYPE_P (type1))
8111 common = 1;
8112 break;
8114 case TRUNC_MOD_EXPR:
8115 case FLOOR_MOD_EXPR:
8116 warn_for_div_by_zero (op1);
8118 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8120 /* Although it would be tempting to shorten always here, that loses
8121 on some targets, since the modulo instruction is undefined if the
8122 quotient can't be represented in the computation mode. We shorten
8123 only if unsigned or if dividing by something we know != -1. */
8124 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8125 || (TREE_CODE (op1) == INTEGER_CST
8126 && !integer_all_onesp (op1)));
8127 common = 1;
8129 break;
8131 case TRUTH_ANDIF_EXPR:
8132 case TRUTH_ORIF_EXPR:
8133 case TRUTH_AND_EXPR:
8134 case TRUTH_OR_EXPR:
8135 case TRUTH_XOR_EXPR:
8136 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8137 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8138 || code0 == FIXED_POINT_TYPE)
8139 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8140 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8141 || code1 == FIXED_POINT_TYPE))
8143 /* Result of these operations is always an int,
8144 but that does not mean the operands should be
8145 converted to ints! */
8146 result_type = integer_type_node;
8147 op0 = c_common_truthvalue_conversion (location, op0);
8148 op1 = c_common_truthvalue_conversion (location, op1);
8149 converted = 1;
8151 break;
8153 /* Shift operations: result has same type as first operand;
8154 always convert second operand to int.
8155 Also set SHORT_SHIFT if shifting rightward. */
8157 case RSHIFT_EXPR:
8158 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8159 && code1 == INTEGER_TYPE)
8161 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8163 if (tree_int_cst_sgn (op1) < 0)
8164 warning (0, "right shift count is negative");
8165 else
8167 if (!integer_zerop (op1))
8168 short_shift = 1;
8170 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8171 warning (0, "right shift count >= width of type");
8175 /* Use the type of the value to be shifted. */
8176 result_type = type0;
8177 /* Convert the shift-count to an integer, regardless of size
8178 of value being shifted. */
8179 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8180 op1 = convert (integer_type_node, op1);
8181 /* Avoid converting op1 to result_type later. */
8182 converted = 1;
8184 break;
8186 case LSHIFT_EXPR:
8187 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8188 && code1 == INTEGER_TYPE)
8190 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8192 if (tree_int_cst_sgn (op1) < 0)
8193 warning (0, "left shift count is negative");
8195 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8196 warning (0, "left shift count >= width of type");
8199 /* Use the type of the value to be shifted. */
8200 result_type = type0;
8201 /* Convert the shift-count to an integer, regardless of size
8202 of value being shifted. */
8203 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8204 op1 = convert (integer_type_node, op1);
8205 /* Avoid converting op1 to result_type later. */
8206 converted = 1;
8208 break;
8210 case EQ_EXPR:
8211 case NE_EXPR:
8212 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8213 warning_at (location,
8214 OPT_Wfloat_equal,
8215 "comparing floating point with == or != is unsafe");
8216 /* Result of comparison is always int,
8217 but don't convert the args to int! */
8218 build_type = integer_type_node;
8219 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8220 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
8221 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8222 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
8223 short_compare = 1;
8224 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8226 tree tt0 = TREE_TYPE (type0);
8227 tree tt1 = TREE_TYPE (type1);
8228 /* Anything compares with void *. void * compares with anything.
8229 Otherwise, the targets must be compatible
8230 and both must be object or both incomplete. */
8231 if (comp_target_types (type0, type1))
8232 result_type = common_pointer_type (type0, type1);
8233 else if (VOID_TYPE_P (tt0))
8235 /* op0 != orig_op0 detects the case of something
8236 whose value is 0 but which isn't a valid null ptr const. */
8237 if (pedantic && !null_pointer_constant_p (orig_op0)
8238 && TREE_CODE (tt1) == FUNCTION_TYPE)
8239 pedwarn (location, OPT_pedantic, "ISO C forbids "
8240 "comparison of %<void *%> with function pointer");
8242 else if (VOID_TYPE_P (tt1))
8244 if (pedantic && !null_pointer_constant_p (orig_op1)
8245 && TREE_CODE (tt0) == FUNCTION_TYPE)
8246 pedwarn (location, OPT_pedantic, "ISO C forbids "
8247 "comparison of %<void *%> with function pointer");
8249 else
8250 /* Avoid warning about the volatile ObjC EH puts on decls. */
8251 if (!objc_ok)
8252 pedwarn (location, 0,
8253 "comparison of distinct pointer types lacks a cast");
8255 if (result_type == NULL_TREE)
8256 result_type = ptr_type_node;
8258 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8260 if (TREE_CODE (op0) == ADDR_EXPR
8261 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
8262 warning_at (location,
8263 OPT_Waddress, "the address of %qD will never be NULL",
8264 TREE_OPERAND (op0, 0));
8265 result_type = type0;
8267 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8269 if (TREE_CODE (op1) == ADDR_EXPR
8270 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
8271 warning_at (location,
8272 OPT_Waddress, "the address of %qD will never be NULL",
8273 TREE_OPERAND (op1, 0));
8274 result_type = type1;
8276 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8278 result_type = type0;
8279 pedwarn (location, 0, "comparison between pointer and integer");
8281 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8283 result_type = type1;
8284 pedwarn (location, 0, "comparison between pointer and integer");
8286 break;
8288 case LE_EXPR:
8289 case GE_EXPR:
8290 case LT_EXPR:
8291 case GT_EXPR:
8292 build_type = integer_type_node;
8293 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8294 || code0 == FIXED_POINT_TYPE)
8295 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8296 || code1 == FIXED_POINT_TYPE))
8297 short_compare = 1;
8298 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8300 if (comp_target_types (type0, type1))
8302 result_type = common_pointer_type (type0, type1);
8303 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8304 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8305 pedwarn (location, 0,
8306 "comparison of complete and incomplete pointers");
8307 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8308 pedwarn (location, OPT_pedantic, "ISO C forbids "
8309 "ordered comparisons of pointers to functions");
8311 else
8313 result_type = ptr_type_node;
8314 pedwarn (location, 0,
8315 "comparison of distinct pointer types lacks a cast");
8318 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8320 result_type = type0;
8321 if (pedantic)
8322 pedwarn (location, OPT_pedantic,
8323 "ordered comparison of pointer with integer zero");
8324 else if (extra_warnings)
8325 warning_at (location, OPT_Wextra,
8326 "ordered comparison of pointer with integer zero");
8328 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8330 result_type = type1;
8331 pedwarn (location, OPT_pedantic,
8332 "ordered comparison of pointer with integer zero");
8334 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8336 result_type = type0;
8337 pedwarn (location, 0, "comparison between pointer and integer");
8339 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8341 result_type = type1;
8342 pedwarn (location, 0, "comparison between pointer and integer");
8344 break;
8346 default:
8347 gcc_unreachable ();
8350 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8351 return error_mark_node;
8353 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8354 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8355 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8356 TREE_TYPE (type1))))
8358 binary_op_error (location, code, type0, type1);
8359 return error_mark_node;
8362 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8363 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
8365 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8366 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
8368 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8370 if (shorten || common || short_compare)
8372 result_type = c_common_type (type0, type1);
8373 if (result_type == error_mark_node)
8374 return error_mark_node;
8377 /* For certain operations (which identify themselves by shorten != 0)
8378 if both args were extended from the same smaller type,
8379 do the arithmetic in that type and then extend.
8381 shorten !=0 and !=1 indicates a bitwise operation.
8382 For them, this optimization is safe only if
8383 both args are zero-extended or both are sign-extended.
8384 Otherwise, we might change the result.
8385 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8386 but calculated in (unsigned short) it would be (unsigned short)-1. */
8388 if (shorten && none_complex)
8390 final_type = result_type;
8391 result_type = shorten_binary_op (result_type, op0, op1,
8392 shorten == -1);
8395 /* Shifts can be shortened if shifting right. */
8397 if (short_shift)
8399 int unsigned_arg;
8400 tree arg0 = get_narrower (op0, &unsigned_arg);
8402 final_type = result_type;
8404 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8405 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8407 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8408 /* We can shorten only if the shift count is less than the
8409 number of bits in the smaller type size. */
8410 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8411 /* We cannot drop an unsigned shift after sign-extension. */
8412 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8414 /* Do an unsigned shift if the operand was zero-extended. */
8415 result_type
8416 = c_common_signed_or_unsigned_type (unsigned_arg,
8417 TREE_TYPE (arg0));
8418 /* Convert value-to-be-shifted to that type. */
8419 if (TREE_TYPE (op0) != result_type)
8420 op0 = convert (result_type, op0);
8421 converted = 1;
8425 /* Comparison operations are shortened too but differently.
8426 They identify themselves by setting short_compare = 1. */
8428 if (short_compare)
8430 /* Don't write &op0, etc., because that would prevent op0
8431 from being kept in a register.
8432 Instead, make copies of the our local variables and
8433 pass the copies by reference, then copy them back afterward. */
8434 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8435 enum tree_code xresultcode = resultcode;
8436 tree val
8437 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8439 if (val != 0)
8440 return val;
8442 op0 = xop0, op1 = xop1;
8443 converted = 1;
8444 resultcode = xresultcode;
8446 if (warn_sign_compare && !skip_evaluation)
8448 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
8449 result_type, resultcode);
8454 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8455 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8456 Then the expression will be built.
8457 It will be given type FINAL_TYPE if that is nonzero;
8458 otherwise, it will be given type RESULT_TYPE. */
8460 if (!result_type)
8462 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
8463 return error_mark_node;
8466 if (!converted)
8468 if (TREE_TYPE (op0) != result_type)
8469 op0 = convert_and_check (result_type, op0);
8470 if (TREE_TYPE (op1) != result_type)
8471 op1 = convert_and_check (result_type, op1);
8473 /* This can happen if one operand has a vector type, and the other
8474 has a different type. */
8475 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8476 return error_mark_node;
8479 if (build_type == NULL_TREE)
8480 build_type = result_type;
8483 /* Treat expressions in initializers specially as they can't trap. */
8484 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8485 build_type,
8486 op0, op1)
8487 : fold_build2 (resultcode, build_type,
8488 op0, op1);
8490 if (final_type != 0)
8491 result = convert (final_type, result);
8492 return result;
8497 /* Convert EXPR to be a truth-value, validating its type for this
8498 purpose. LOCATION is the source location for the expression. */
8500 tree
8501 c_objc_common_truthvalue_conversion (location_t location, tree expr)
8503 switch (TREE_CODE (TREE_TYPE (expr)))
8505 case ARRAY_TYPE:
8506 error_at (location, "used array that cannot be converted to pointer where scalar is required");
8507 return error_mark_node;
8509 case RECORD_TYPE:
8510 error_at (location, "used struct type value where scalar is required");
8511 return error_mark_node;
8513 case UNION_TYPE:
8514 error_at (location, "used union type value where scalar is required");
8515 return error_mark_node;
8517 case FUNCTION_TYPE:
8518 gcc_unreachable ();
8520 default:
8521 break;
8524 /* ??? Should we also give an error for void and vectors rather than
8525 leaving those to give errors later? */
8526 return c_common_truthvalue_conversion (location, expr);
8530 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8531 required. */
8533 tree
8534 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
8536 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8538 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8539 /* Executing a compound literal inside a function reinitializes
8540 it. */
8541 if (!TREE_STATIC (decl))
8542 *se = true;
8543 return decl;
8545 else
8546 return expr;
8549 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8551 tree
8552 c_begin_omp_parallel (void)
8554 tree block;
8556 keep_next_level ();
8557 block = c_begin_compound_stmt (true);
8559 return block;
8562 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound statement. */
8564 tree
8565 c_finish_omp_parallel (tree clauses, tree block)
8567 tree stmt;
8569 block = c_end_compound_stmt (block, true);
8571 stmt = make_node (OMP_PARALLEL);
8572 TREE_TYPE (stmt) = void_type_node;
8573 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8574 OMP_PARALLEL_BODY (stmt) = block;
8576 return add_stmt (stmt);
8579 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
8581 tree
8582 c_begin_omp_task (void)
8584 tree block;
8586 keep_next_level ();
8587 block = c_begin_compound_stmt (true);
8589 return block;
8592 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound statement. */
8594 tree
8595 c_finish_omp_task (tree clauses, tree block)
8597 tree stmt;
8599 block = c_end_compound_stmt (block, true);
8601 stmt = make_node (OMP_TASK);
8602 TREE_TYPE (stmt) = void_type_node;
8603 OMP_TASK_CLAUSES (stmt) = clauses;
8604 OMP_TASK_BODY (stmt) = block;
8606 return add_stmt (stmt);
8609 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
8610 Remove any elements from the list that are invalid. */
8612 tree
8613 c_finish_omp_clauses (tree clauses)
8615 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8616 tree c, t, *pc = &clauses;
8617 const char *name;
8619 bitmap_obstack_initialize (NULL);
8620 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8621 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8622 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8624 for (pc = &clauses, c = clauses; c ; c = *pc)
8626 bool remove = false;
8627 bool need_complete = false;
8628 bool need_implicitly_determined = false;
8630 switch (OMP_CLAUSE_CODE (c))
8632 case OMP_CLAUSE_SHARED:
8633 name = "shared";
8634 need_implicitly_determined = true;
8635 goto check_dup_generic;
8637 case OMP_CLAUSE_PRIVATE:
8638 name = "private";
8639 need_complete = true;
8640 need_implicitly_determined = true;
8641 goto check_dup_generic;
8643 case OMP_CLAUSE_REDUCTION:
8644 name = "reduction";
8645 need_implicitly_determined = true;
8646 t = OMP_CLAUSE_DECL (c);
8647 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8648 || POINTER_TYPE_P (TREE_TYPE (t)))
8650 error ("%qE has invalid type for %<reduction%>", t);
8651 remove = true;
8653 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8655 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8656 const char *r_name = NULL;
8658 switch (r_code)
8660 case PLUS_EXPR:
8661 case MULT_EXPR:
8662 case MINUS_EXPR:
8663 break;
8664 case BIT_AND_EXPR:
8665 r_name = "&";
8666 break;
8667 case BIT_XOR_EXPR:
8668 r_name = "^";
8669 break;
8670 case BIT_IOR_EXPR:
8671 r_name = "|";
8672 break;
8673 case TRUTH_ANDIF_EXPR:
8674 r_name = "&&";
8675 break;
8676 case TRUTH_ORIF_EXPR:
8677 r_name = "||";
8678 break;
8679 default:
8680 gcc_unreachable ();
8682 if (r_name)
8684 error ("%qE has invalid type for %<reduction(%s)%>",
8685 t, r_name);
8686 remove = true;
8689 goto check_dup_generic;
8691 case OMP_CLAUSE_COPYPRIVATE:
8692 name = "copyprivate";
8693 goto check_dup_generic;
8695 case OMP_CLAUSE_COPYIN:
8696 name = "copyin";
8697 t = OMP_CLAUSE_DECL (c);
8698 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8700 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8701 remove = true;
8703 goto check_dup_generic;
8705 check_dup_generic:
8706 t = OMP_CLAUSE_DECL (c);
8707 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8709 error ("%qE is not a variable in clause %qs", t, name);
8710 remove = true;
8712 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8713 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8714 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8716 error ("%qE appears more than once in data clauses", t);
8717 remove = true;
8719 else
8720 bitmap_set_bit (&generic_head, DECL_UID (t));
8721 break;
8723 case OMP_CLAUSE_FIRSTPRIVATE:
8724 name = "firstprivate";
8725 t = OMP_CLAUSE_DECL (c);
8726 need_complete = true;
8727 need_implicitly_determined = true;
8728 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8730 error ("%qE is not a variable in clause %<firstprivate%>", t);
8731 remove = true;
8733 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8734 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8736 error ("%qE appears more than once in data clauses", t);
8737 remove = true;
8739 else
8740 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8741 break;
8743 case OMP_CLAUSE_LASTPRIVATE:
8744 name = "lastprivate";
8745 t = OMP_CLAUSE_DECL (c);
8746 need_complete = true;
8747 need_implicitly_determined = true;
8748 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8750 error ("%qE is not a variable in clause %<lastprivate%>", t);
8751 remove = true;
8753 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8754 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8756 error ("%qE appears more than once in data clauses", t);
8757 remove = true;
8759 else
8760 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8761 break;
8763 case OMP_CLAUSE_IF:
8764 case OMP_CLAUSE_NUM_THREADS:
8765 case OMP_CLAUSE_SCHEDULE:
8766 case OMP_CLAUSE_NOWAIT:
8767 case OMP_CLAUSE_ORDERED:
8768 case OMP_CLAUSE_DEFAULT:
8769 case OMP_CLAUSE_UNTIED:
8770 case OMP_CLAUSE_COLLAPSE:
8771 pc = &OMP_CLAUSE_CHAIN (c);
8772 continue;
8774 default:
8775 gcc_unreachable ();
8778 if (!remove)
8780 t = OMP_CLAUSE_DECL (c);
8782 if (need_complete)
8784 t = require_complete_type (t);
8785 if (t == error_mark_node)
8786 remove = true;
8789 if (need_implicitly_determined)
8791 const char *share_name = NULL;
8793 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8794 share_name = "threadprivate";
8795 else switch (c_omp_predetermined_sharing (t))
8797 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8798 break;
8799 case OMP_CLAUSE_DEFAULT_SHARED:
8800 share_name = "shared";
8801 break;
8802 case OMP_CLAUSE_DEFAULT_PRIVATE:
8803 share_name = "private";
8804 break;
8805 default:
8806 gcc_unreachable ();
8808 if (share_name)
8810 error ("%qE is predetermined %qs for %qs",
8811 t, share_name, name);
8812 remove = true;
8817 if (remove)
8818 *pc = OMP_CLAUSE_CHAIN (c);
8819 else
8820 pc = &OMP_CLAUSE_CHAIN (c);
8823 bitmap_obstack_release (NULL);
8824 return clauses;
8827 /* Make a variant type in the proper way for C/C++, propagating qualifiers
8828 down to the element type of an array. */
8830 tree
8831 c_build_qualified_type (tree type, int type_quals)
8833 if (type == error_mark_node)
8834 return type;
8836 if (TREE_CODE (type) == ARRAY_TYPE)
8838 tree t;
8839 tree element_type = c_build_qualified_type (TREE_TYPE (type),
8840 type_quals);
8842 /* See if we already have an identically qualified type. */
8843 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8845 if (TYPE_QUALS (strip_array_types (t)) == type_quals
8846 && TYPE_NAME (t) == TYPE_NAME (type)
8847 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
8848 && attribute_list_equal (TYPE_ATTRIBUTES (t),
8849 TYPE_ATTRIBUTES (type)))
8850 break;
8852 if (!t)
8854 tree domain = TYPE_DOMAIN (type);
8856 t = build_variant_type_copy (type);
8857 TREE_TYPE (t) = element_type;
8859 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
8860 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
8861 SET_TYPE_STRUCTURAL_EQUALITY (t);
8862 else if (TYPE_CANONICAL (element_type) != element_type
8863 || (domain && TYPE_CANONICAL (domain) != domain))
8865 tree unqualified_canon
8866 = build_array_type (TYPE_CANONICAL (element_type),
8867 domain? TYPE_CANONICAL (domain)
8868 : NULL_TREE);
8869 TYPE_CANONICAL (t)
8870 = c_build_qualified_type (unqualified_canon, type_quals);
8872 else
8873 TYPE_CANONICAL (t) = t;
8875 return t;
8878 /* A restrict-qualified pointer type must be a pointer to object or
8879 incomplete type. Note that the use of POINTER_TYPE_P also allows
8880 REFERENCE_TYPEs, which is appropriate for C++. */
8881 if ((type_quals & TYPE_QUAL_RESTRICT)
8882 && (!POINTER_TYPE_P (type)
8883 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
8885 error ("invalid use of %<restrict%>");
8886 type_quals &= ~TYPE_QUAL_RESTRICT;
8889 return build_qualified_type (type, type_quals);