c-decl.c (c_override_global_bindings_to_false): Remove.
[gcc.git] / gcc / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
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. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "gimple.h"
43 #include "c-family/c-objc.h"
44
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
52 };
53
54 /* The level of nesting inside "__alignof__". */
55 int in_alignof;
56
57 /* The level of nesting inside "sizeof". */
58 int in_sizeof;
59
60 /* The level of nesting inside "typeof". */
61 int in_typeof;
62
63 /* Nonzero if we've already printed a "missing braces around initializer"
64 message within this initializer. */
65 static int missing_braces_mentioned;
66
67 static int require_constant_value;
68 static int require_constant_elements;
69
70 static bool null_pointer_constant_p (const_tree);
71 static tree qualify_type (tree, tree);
72 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
73 bool *);
74 static int comp_target_types (location_t, tree, tree);
75 static int function_types_compatible_p (const_tree, const_tree, bool *,
76 bool *);
77 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
78 static tree lookup_field (tree, tree);
79 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
80 tree);
81 static tree pointer_diff (location_t, tree, tree);
82 static tree convert_for_assignment (location_t, tree, tree, tree,
83 enum impl_conv, bool, tree, tree, int);
84 static tree valid_compound_expr_initializer (tree, tree);
85 static void push_string (const char *);
86 static void push_member_name (tree);
87 static int spelling_length (void);
88 static char *print_spelling (char *);
89 static void warning_init (int, const char *);
90 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
91 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
92 struct obstack *);
93 static void output_pending_init_elements (int, struct obstack *);
94 static int set_designator (int, struct obstack *);
95 static void push_range_stack (tree, struct obstack *);
96 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
97 static void set_nonincremental_init (struct obstack *);
98 static void set_nonincremental_init_from_string (tree, struct obstack *);
99 static tree find_init_member (tree, struct obstack *);
100 static void readonly_warning (tree, enum lvalue_use);
101 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
102 static void record_maybe_used_decl (tree);
103 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
104 \f
105 /* Return true if EXP is a null pointer constant, false otherwise. */
106
107 static bool
108 null_pointer_constant_p (const_tree expr)
109 {
110 /* This should really operate on c_expr structures, but they aren't
111 yet available everywhere required. */
112 tree type = TREE_TYPE (expr);
113 return (TREE_CODE (expr) == INTEGER_CST
114 && !TREE_OVERFLOW (expr)
115 && integer_zerop (expr)
116 && (INTEGRAL_TYPE_P (type)
117 || (TREE_CODE (type) == POINTER_TYPE
118 && VOID_TYPE_P (TREE_TYPE (type))
119 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
120 }
121
122 /* EXPR may appear in an unevaluated part of an integer constant
123 expression, but not in an evaluated part. Wrap it in a
124 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
125 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
126
127 static tree
128 note_integer_operands (tree expr)
129 {
130 tree ret;
131 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
132 {
133 ret = copy_node (expr);
134 TREE_OVERFLOW (ret) = 1;
135 }
136 else
137 {
138 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
139 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
140 }
141 return ret;
142 }
143
144 /* Having checked whether EXPR may appear in an unevaluated part of an
145 integer constant expression and found that it may, remove any
146 C_MAYBE_CONST_EXPR noting this fact and return the resulting
147 expression. */
148
149 static inline tree
150 remove_c_maybe_const_expr (tree expr)
151 {
152 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
153 return C_MAYBE_CONST_EXPR_EXPR (expr);
154 else
155 return expr;
156 }
157
158 \f/* This is a cache to hold if two types are compatible or not. */
159
160 struct tagged_tu_seen_cache {
161 const struct tagged_tu_seen_cache * next;
162 const_tree t1;
163 const_tree t2;
164 /* The return value of tagged_types_tu_compatible_p if we had seen
165 these two types already. */
166 int val;
167 };
168
169 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
170 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
171
172 /* Do `exp = require_complete_type (exp);' to make sure exp
173 does not have an incomplete type. (That includes void types.) */
174
175 tree
176 require_complete_type (tree value)
177 {
178 tree type = TREE_TYPE (value);
179
180 if (value == error_mark_node || type == error_mark_node)
181 return error_mark_node;
182
183 /* First, detect a valid value with a complete type. */
184 if (COMPLETE_TYPE_P (type))
185 return value;
186
187 c_incomplete_type_error (value, type);
188 return error_mark_node;
189 }
190
191 /* Print an error message for invalid use of an incomplete type.
192 VALUE is the expression that was used (or 0 if that isn't known)
193 and TYPE is the type that was invalid. */
194
195 void
196 c_incomplete_type_error (const_tree value, const_tree type)
197 {
198 const char *type_code_string;
199
200 /* Avoid duplicate error message. */
201 if (TREE_CODE (type) == ERROR_MARK)
202 return;
203
204 if (value != 0 && (TREE_CODE (value) == VAR_DECL
205 || TREE_CODE (value) == PARM_DECL))
206 error ("%qD has an incomplete type", value);
207 else
208 {
209 retry:
210 /* We must print an error message. Be clever about what it says. */
211
212 switch (TREE_CODE (type))
213 {
214 case RECORD_TYPE:
215 type_code_string = "struct";
216 break;
217
218 case UNION_TYPE:
219 type_code_string = "union";
220 break;
221
222 case ENUMERAL_TYPE:
223 type_code_string = "enum";
224 break;
225
226 case VOID_TYPE:
227 error ("invalid use of void expression");
228 return;
229
230 case ARRAY_TYPE:
231 if (TYPE_DOMAIN (type))
232 {
233 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
234 {
235 error ("invalid use of flexible array member");
236 return;
237 }
238 type = TREE_TYPE (type);
239 goto retry;
240 }
241 error ("invalid use of array with unspecified bounds");
242 return;
243
244 default:
245 gcc_unreachable ();
246 }
247
248 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
249 error ("invalid use of undefined type %<%s %E%>",
250 type_code_string, TYPE_NAME (type));
251 else
252 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
253 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
254 }
255 }
256
257 /* Given a type, apply default promotions wrt unnamed function
258 arguments and return the new type. */
259
260 tree
261 c_type_promotes_to (tree type)
262 {
263 if (TYPE_MAIN_VARIANT (type) == float_type_node)
264 return double_type_node;
265
266 if (c_promoting_integer_type_p (type))
267 {
268 /* Preserve unsignedness if not really getting any wider. */
269 if (TYPE_UNSIGNED (type)
270 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
271 return unsigned_type_node;
272 return integer_type_node;
273 }
274
275 return type;
276 }
277
278 /* Return true if between two named address spaces, whether there is a superset
279 named address space that encompasses both address spaces. If there is a
280 superset, return which address space is the superset. */
281
282 static bool
283 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
284 {
285 if (as1 == as2)
286 {
287 *common = as1;
288 return true;
289 }
290 else if (targetm.addr_space.subset_p (as1, as2))
291 {
292 *common = as2;
293 return true;
294 }
295 else if (targetm.addr_space.subset_p (as2, as1))
296 {
297 *common = as1;
298 return true;
299 }
300 else
301 return false;
302 }
303
304 /* Return a variant of TYPE which has all the type qualifiers of LIKE
305 as well as those of TYPE. */
306
307 static tree
308 qualify_type (tree type, tree like)
309 {
310 addr_space_t as_type = TYPE_ADDR_SPACE (type);
311 addr_space_t as_like = TYPE_ADDR_SPACE (like);
312 addr_space_t as_common;
313
314 /* If the two named address spaces are different, determine the common
315 superset address space. If there isn't one, raise an error. */
316 if (!addr_space_superset (as_type, as_like, &as_common))
317 {
318 as_common = as_type;
319 error ("%qT and %qT are in disjoint named address spaces",
320 type, like);
321 }
322
323 return c_build_qualified_type (type,
324 TYPE_QUALS_NO_ADDR_SPACE (type)
325 | TYPE_QUALS_NO_ADDR_SPACE (like)
326 | ENCODE_QUAL_ADDR_SPACE (as_common));
327 }
328
329 /* Return true iff the given tree T is a variable length array. */
330
331 bool
332 c_vla_type_p (const_tree t)
333 {
334 if (TREE_CODE (t) == ARRAY_TYPE
335 && C_TYPE_VARIABLE_SIZE (t))
336 return true;
337 return false;
338 }
339 \f
340 /* Return the composite type of two compatible types.
341
342 We assume that comptypes has already been done and returned
343 nonzero; if that isn't so, this may crash. In particular, we
344 assume that qualifiers match. */
345
346 tree
347 composite_type (tree t1, tree t2)
348 {
349 enum tree_code code1;
350 enum tree_code code2;
351 tree attributes;
352
353 /* Save time if the two types are the same. */
354
355 if (t1 == t2) return t1;
356
357 /* If one type is nonsense, use the other. */
358 if (t1 == error_mark_node)
359 return t2;
360 if (t2 == error_mark_node)
361 return t1;
362
363 code1 = TREE_CODE (t1);
364 code2 = TREE_CODE (t2);
365
366 /* Merge the attributes. */
367 attributes = targetm.merge_type_attributes (t1, t2);
368
369 /* If one is an enumerated type and the other is the compatible
370 integer type, the composite type might be either of the two
371 (DR#013 question 3). For consistency, use the enumerated type as
372 the composite type. */
373
374 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
375 return t1;
376 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
377 return t2;
378
379 gcc_assert (code1 == code2);
380
381 switch (code1)
382 {
383 case POINTER_TYPE:
384 /* For two pointers, do this recursively on the target type. */
385 {
386 tree pointed_to_1 = TREE_TYPE (t1);
387 tree pointed_to_2 = TREE_TYPE (t2);
388 tree target = composite_type (pointed_to_1, pointed_to_2);
389 t1 = build_pointer_type (target);
390 t1 = build_type_attribute_variant (t1, attributes);
391 return qualify_type (t1, t2);
392 }
393
394 case ARRAY_TYPE:
395 {
396 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
397 int quals;
398 tree unqual_elt;
399 tree d1 = TYPE_DOMAIN (t1);
400 tree d2 = TYPE_DOMAIN (t2);
401 bool d1_variable, d2_variable;
402 bool d1_zero, d2_zero;
403 bool t1_complete, t2_complete;
404
405 /* We should not have any type quals on arrays at all. */
406 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
407 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
408
409 t1_complete = COMPLETE_TYPE_P (t1);
410 t2_complete = COMPLETE_TYPE_P (t2);
411
412 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
413 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
414
415 d1_variable = (!d1_zero
416 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
417 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
418 d2_variable = (!d2_zero
419 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
420 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
421 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
422 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
423
424 /* Save space: see if the result is identical to one of the args. */
425 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
426 && (d2_variable || d2_zero || !d1_variable))
427 return build_type_attribute_variant (t1, attributes);
428 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
429 && (d1_variable || d1_zero || !d2_variable))
430 return build_type_attribute_variant (t2, attributes);
431
432 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
433 return build_type_attribute_variant (t1, attributes);
434 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
435 return build_type_attribute_variant (t2, attributes);
436
437 /* Merge the element types, and have a size if either arg has
438 one. We may have qualifiers on the element types. To set
439 up TYPE_MAIN_VARIANT correctly, we need to form the
440 composite of the unqualified types and add the qualifiers
441 back at the end. */
442 quals = TYPE_QUALS (strip_array_types (elt));
443 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
444 t1 = build_array_type (unqual_elt,
445 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
446 && (d2_variable
447 || d2_zero
448 || !d1_variable))
449 ? t1
450 : t2));
451 /* Ensure a composite type involving a zero-length array type
452 is a zero-length type not an incomplete type. */
453 if (d1_zero && d2_zero
454 && (t1_complete || t2_complete)
455 && !COMPLETE_TYPE_P (t1))
456 {
457 TYPE_SIZE (t1) = bitsize_zero_node;
458 TYPE_SIZE_UNIT (t1) = size_zero_node;
459 }
460 t1 = c_build_qualified_type (t1, quals);
461 return build_type_attribute_variant (t1, attributes);
462 }
463
464 case ENUMERAL_TYPE:
465 case RECORD_TYPE:
466 case UNION_TYPE:
467 if (attributes != NULL)
468 {
469 /* Try harder not to create a new aggregate type. */
470 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
471 return t1;
472 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
473 return t2;
474 }
475 return build_type_attribute_variant (t1, attributes);
476
477 case FUNCTION_TYPE:
478 /* Function types: prefer the one that specified arg types.
479 If both do, merge the arg types. Also merge the return types. */
480 {
481 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
482 tree p1 = TYPE_ARG_TYPES (t1);
483 tree p2 = TYPE_ARG_TYPES (t2);
484 int len;
485 tree newargs, n;
486 int i;
487
488 /* Save space: see if the result is identical to one of the args. */
489 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
490 return build_type_attribute_variant (t1, attributes);
491 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
492 return build_type_attribute_variant (t2, attributes);
493
494 /* Simple way if one arg fails to specify argument types. */
495 if (TYPE_ARG_TYPES (t1) == 0)
496 {
497 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
498 t1 = build_type_attribute_variant (t1, attributes);
499 return qualify_type (t1, t2);
500 }
501 if (TYPE_ARG_TYPES (t2) == 0)
502 {
503 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
504 t1 = build_type_attribute_variant (t1, attributes);
505 return qualify_type (t1, t2);
506 }
507
508 /* If both args specify argument types, we must merge the two
509 lists, argument by argument. */
510
511 len = list_length (p1);
512 newargs = 0;
513
514 for (i = 0; i < len; i++)
515 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
516
517 n = newargs;
518
519 for (; p1;
520 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
521 {
522 /* A null type means arg type is not specified.
523 Take whatever the other function type has. */
524 if (TREE_VALUE (p1) == 0)
525 {
526 TREE_VALUE (n) = TREE_VALUE (p2);
527 goto parm_done;
528 }
529 if (TREE_VALUE (p2) == 0)
530 {
531 TREE_VALUE (n) = TREE_VALUE (p1);
532 goto parm_done;
533 }
534
535 /* Given wait (union {union wait *u; int *i} *)
536 and wait (union wait *),
537 prefer union wait * as type of parm. */
538 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
539 && TREE_VALUE (p1) != TREE_VALUE (p2))
540 {
541 tree memb;
542 tree mv2 = TREE_VALUE (p2);
543 if (mv2 && mv2 != error_mark_node
544 && TREE_CODE (mv2) != ARRAY_TYPE)
545 mv2 = TYPE_MAIN_VARIANT (mv2);
546 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
547 memb; memb = DECL_CHAIN (memb))
548 {
549 tree mv3 = TREE_TYPE (memb);
550 if (mv3 && mv3 != error_mark_node
551 && TREE_CODE (mv3) != ARRAY_TYPE)
552 mv3 = TYPE_MAIN_VARIANT (mv3);
553 if (comptypes (mv3, mv2))
554 {
555 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
556 TREE_VALUE (p2));
557 pedwarn (input_location, OPT_pedantic,
558 "function types not truly compatible in ISO C");
559 goto parm_done;
560 }
561 }
562 }
563 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
564 && TREE_VALUE (p2) != TREE_VALUE (p1))
565 {
566 tree memb;
567 tree mv1 = TREE_VALUE (p1);
568 if (mv1 && mv1 != error_mark_node
569 && TREE_CODE (mv1) != ARRAY_TYPE)
570 mv1 = TYPE_MAIN_VARIANT (mv1);
571 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
572 memb; memb = DECL_CHAIN (memb))
573 {
574 tree mv3 = TREE_TYPE (memb);
575 if (mv3 && mv3 != error_mark_node
576 && TREE_CODE (mv3) != ARRAY_TYPE)
577 mv3 = TYPE_MAIN_VARIANT (mv3);
578 if (comptypes (mv3, mv1))
579 {
580 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
581 TREE_VALUE (p1));
582 pedwarn (input_location, OPT_pedantic,
583 "function types not truly compatible in ISO C");
584 goto parm_done;
585 }
586 }
587 }
588 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
589 parm_done: ;
590 }
591
592 t1 = build_function_type (valtype, newargs);
593 t1 = qualify_type (t1, t2);
594 /* ... falls through ... */
595 }
596
597 default:
598 return build_type_attribute_variant (t1, attributes);
599 }
600
601 }
602
603 /* Return the type of a conditional expression between pointers to
604 possibly differently qualified versions of compatible types.
605
606 We assume that comp_target_types has already been done and returned
607 nonzero; if that isn't so, this may crash. */
608
609 static tree
610 common_pointer_type (tree t1, tree t2)
611 {
612 tree attributes;
613 tree pointed_to_1, mv1;
614 tree pointed_to_2, mv2;
615 tree target;
616 unsigned target_quals;
617 addr_space_t as1, as2, as_common;
618 int quals1, quals2;
619
620 /* Save time if the two types are the same. */
621
622 if (t1 == t2) return t1;
623
624 /* If one type is nonsense, use the other. */
625 if (t1 == error_mark_node)
626 return t2;
627 if (t2 == error_mark_node)
628 return t1;
629
630 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
631 && TREE_CODE (t2) == POINTER_TYPE);
632
633 /* Merge the attributes. */
634 attributes = targetm.merge_type_attributes (t1, t2);
635
636 /* Find the composite type of the target types, and combine the
637 qualifiers of the two types' targets. Do not lose qualifiers on
638 array element types by taking the TYPE_MAIN_VARIANT. */
639 mv1 = pointed_to_1 = TREE_TYPE (t1);
640 mv2 = pointed_to_2 = TREE_TYPE (t2);
641 if (TREE_CODE (mv1) != ARRAY_TYPE)
642 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
643 if (TREE_CODE (mv2) != ARRAY_TYPE)
644 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
645 target = composite_type (mv1, mv2);
646
647 /* For function types do not merge const qualifiers, but drop them
648 if used inconsistently. The middle-end uses these to mark const
649 and noreturn functions. */
650 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
651 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
652
653 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
654 target_quals = (quals1 & quals2);
655 else
656 target_quals = (quals1 | quals2);
657
658 /* If the two named address spaces are different, determine the common
659 superset address space. This is guaranteed to exist due to the
660 assumption that comp_target_type returned non-zero. */
661 as1 = TYPE_ADDR_SPACE (pointed_to_1);
662 as2 = TYPE_ADDR_SPACE (pointed_to_2);
663 if (!addr_space_superset (as1, as2, &as_common))
664 gcc_unreachable ();
665
666 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
667
668 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
669 return build_type_attribute_variant (t1, attributes);
670 }
671
672 /* Return the common type for two arithmetic types under the usual
673 arithmetic conversions. The default conversions have already been
674 applied, and enumerated types converted to their compatible integer
675 types. The resulting type is unqualified and has no attributes.
676
677 This is the type for the result of most arithmetic operations
678 if the operands have the given two types. */
679
680 static tree
681 c_common_type (tree t1, tree t2)
682 {
683 enum tree_code code1;
684 enum tree_code code2;
685
686 /* If one type is nonsense, use the other. */
687 if (t1 == error_mark_node)
688 return t2;
689 if (t2 == error_mark_node)
690 return t1;
691
692 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
693 t1 = TYPE_MAIN_VARIANT (t1);
694
695 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
696 t2 = TYPE_MAIN_VARIANT (t2);
697
698 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
699 t1 = build_type_attribute_variant (t1, NULL_TREE);
700
701 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
702 t2 = build_type_attribute_variant (t2, NULL_TREE);
703
704 /* Save time if the two types are the same. */
705
706 if (t1 == t2) return t1;
707
708 code1 = TREE_CODE (t1);
709 code2 = TREE_CODE (t2);
710
711 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
712 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
713 || code1 == INTEGER_TYPE);
714 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
715 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
716 || code2 == INTEGER_TYPE);
717
718 /* When one operand is a decimal float type, the other operand cannot be
719 a generic float type or a complex type. We also disallow vector types
720 here. */
721 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
722 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
723 {
724 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
725 {
726 error ("can%'t mix operands of decimal float and vector types");
727 return error_mark_node;
728 }
729 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
730 {
731 error ("can%'t mix operands of decimal float and complex types");
732 return error_mark_node;
733 }
734 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
735 {
736 error ("can%'t mix operands of decimal float and other float types");
737 return error_mark_node;
738 }
739 }
740
741 /* If one type is a vector type, return that type. (How the usual
742 arithmetic conversions apply to the vector types extension is not
743 precisely specified.) */
744 if (code1 == VECTOR_TYPE)
745 return t1;
746
747 if (code2 == VECTOR_TYPE)
748 return t2;
749
750 /* If one type is complex, form the common type of the non-complex
751 components, then make that complex. Use T1 or T2 if it is the
752 required type. */
753 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
754 {
755 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
756 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
757 tree subtype = c_common_type (subtype1, subtype2);
758
759 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
760 return t1;
761 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
762 return t2;
763 else
764 return build_complex_type (subtype);
765 }
766
767 /* If only one is real, use it as the result. */
768
769 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
770 return t1;
771
772 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
773 return t2;
774
775 /* If both are real and either are decimal floating point types, use
776 the decimal floating point type with the greater precision. */
777
778 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
779 {
780 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
781 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
782 return dfloat128_type_node;
783 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
784 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
785 return dfloat64_type_node;
786 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
787 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
788 return dfloat32_type_node;
789 }
790
791 /* Deal with fixed-point types. */
792 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
793 {
794 unsigned int unsignedp = 0, satp = 0;
795 enum machine_mode m1, m2;
796 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
797
798 m1 = TYPE_MODE (t1);
799 m2 = TYPE_MODE (t2);
800
801 /* If one input type is saturating, the result type is saturating. */
802 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
803 satp = 1;
804
805 /* If both fixed-point types are unsigned, the result type is unsigned.
806 When mixing fixed-point and integer types, follow the sign of the
807 fixed-point type.
808 Otherwise, the result type is signed. */
809 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
810 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
811 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
812 && TYPE_UNSIGNED (t1))
813 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
814 && TYPE_UNSIGNED (t2)))
815 unsignedp = 1;
816
817 /* The result type is signed. */
818 if (unsignedp == 0)
819 {
820 /* If the input type is unsigned, we need to convert to the
821 signed type. */
822 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
823 {
824 enum mode_class mclass = (enum mode_class) 0;
825 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
826 mclass = MODE_FRACT;
827 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
828 mclass = MODE_ACCUM;
829 else
830 gcc_unreachable ();
831 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
832 }
833 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
834 {
835 enum mode_class mclass = (enum mode_class) 0;
836 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
837 mclass = MODE_FRACT;
838 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
839 mclass = MODE_ACCUM;
840 else
841 gcc_unreachable ();
842 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
843 }
844 }
845
846 if (code1 == FIXED_POINT_TYPE)
847 {
848 fbit1 = GET_MODE_FBIT (m1);
849 ibit1 = GET_MODE_IBIT (m1);
850 }
851 else
852 {
853 fbit1 = 0;
854 /* Signed integers need to subtract one sign bit. */
855 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
856 }
857
858 if (code2 == FIXED_POINT_TYPE)
859 {
860 fbit2 = GET_MODE_FBIT (m2);
861 ibit2 = GET_MODE_IBIT (m2);
862 }
863 else
864 {
865 fbit2 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
868 }
869
870 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
871 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
872 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
873 satp);
874 }
875
876 /* Both real or both integers; use the one with greater precision. */
877
878 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
879 return t1;
880 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
881 return t2;
882
883 /* Same precision. Prefer long longs to longs to ints when the
884 same precision, following the C99 rules on integer type rank
885 (which are equivalent to the C90 rules for C90 types). */
886
887 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
888 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
889 return long_long_unsigned_type_node;
890
891 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
892 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
893 {
894 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
895 return long_long_unsigned_type_node;
896 else
897 return long_long_integer_type_node;
898 }
899
900 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
901 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
902 return long_unsigned_type_node;
903
904 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
906 {
907 /* But preserve unsignedness from the other type,
908 since long cannot hold all the values of an unsigned int. */
909 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
910 return long_unsigned_type_node;
911 else
912 return long_integer_type_node;
913 }
914
915 /* Likewise, prefer long double to double even if same size. */
916 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
918 return long_double_type_node;
919
920 /* Otherwise prefer the unsigned one. */
921
922 if (TYPE_UNSIGNED (t1))
923 return t1;
924 else
925 return t2;
926 }
927 \f
928 /* Wrapper around c_common_type that is used by c-common.c and other
929 front end optimizations that remove promotions. ENUMERAL_TYPEs
930 are allowed here and are converted to their compatible integer types.
931 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
932 preferably a non-Boolean type as the common type. */
933 tree
934 common_type (tree t1, tree t2)
935 {
936 if (TREE_CODE (t1) == ENUMERAL_TYPE)
937 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
938 if (TREE_CODE (t2) == ENUMERAL_TYPE)
939 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
940
941 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
942 if (TREE_CODE (t1) == BOOLEAN_TYPE
943 && TREE_CODE (t2) == BOOLEAN_TYPE)
944 return boolean_type_node;
945
946 /* If either type is BOOLEAN_TYPE, then return the other. */
947 if (TREE_CODE (t1) == BOOLEAN_TYPE)
948 return t2;
949 if (TREE_CODE (t2) == BOOLEAN_TYPE)
950 return t1;
951
952 return c_common_type (t1, t2);
953 }
954
955 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
956 or various other operations. Return 2 if they are compatible
957 but a warning may be needed if you use them together. */
958
959 int
960 comptypes (tree type1, tree type2)
961 {
962 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
963 int val;
964
965 val = comptypes_internal (type1, type2, NULL, NULL);
966 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
967
968 return val;
969 }
970
971 /* Like comptypes, but if it returns non-zero because enum and int are
972 compatible, it sets *ENUM_AND_INT_P to true. */
973
974 static int
975 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
976 {
977 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
978 int val;
979
980 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
981 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
982
983 return val;
984 }
985
986 /* Like comptypes, but if it returns nonzero for different types, it
987 sets *DIFFERENT_TYPES_P to true. */
988
989 int
990 comptypes_check_different_types (tree type1, tree type2,
991 bool *different_types_p)
992 {
993 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
994 int val;
995
996 val = comptypes_internal (type1, type2, NULL, different_types_p);
997 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
998
999 return val;
1000 }
1001 \f
1002 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1003 or various other operations. Return 2 if they are compatible
1004 but a warning may be needed if you use them together. If
1005 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1006 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1007 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1008 NULL, and the types are compatible but different enough not to be
1009 permitted in C1X typedef redeclarations, then this sets
1010 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1011 false, but may or may not be set if the types are incompatible.
1012 This differs from comptypes, in that we don't free the seen
1013 types. */
1014
1015 static int
1016 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1017 bool *different_types_p)
1018 {
1019 const_tree t1 = type1;
1020 const_tree t2 = type2;
1021 int attrval, val;
1022
1023 /* Suppress errors caused by previously reported errors. */
1024
1025 if (t1 == t2 || !t1 || !t2
1026 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1027 return 1;
1028
1029 /* Enumerated types are compatible with integer types, but this is
1030 not transitive: two enumerated types in the same translation unit
1031 are compatible with each other only if they are the same type. */
1032
1033 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1034 {
1035 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1036 if (TREE_CODE (t2) != VOID_TYPE)
1037 {
1038 if (enum_and_int_p != NULL)
1039 *enum_and_int_p = true;
1040 if (different_types_p != NULL)
1041 *different_types_p = true;
1042 }
1043 }
1044 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1045 {
1046 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1047 if (TREE_CODE (t1) != VOID_TYPE)
1048 {
1049 if (enum_and_int_p != NULL)
1050 *enum_and_int_p = true;
1051 if (different_types_p != NULL)
1052 *different_types_p = true;
1053 }
1054 }
1055
1056 if (t1 == t2)
1057 return 1;
1058
1059 /* Different classes of types can't be compatible. */
1060
1061 if (TREE_CODE (t1) != TREE_CODE (t2))
1062 return 0;
1063
1064 /* Qualifiers must match. C99 6.7.3p9 */
1065
1066 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1067 return 0;
1068
1069 /* Allow for two different type nodes which have essentially the same
1070 definition. Note that we already checked for equality of the type
1071 qualifiers (just above). */
1072
1073 if (TREE_CODE (t1) != ARRAY_TYPE
1074 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1075 return 1;
1076
1077 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1078 if (!(attrval = comp_type_attributes (t1, t2)))
1079 return 0;
1080
1081 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1082 val = 0;
1083
1084 switch (TREE_CODE (t1))
1085 {
1086 case POINTER_TYPE:
1087 /* Do not remove mode or aliasing information. */
1088 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1089 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1090 break;
1091 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1092 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1093 enum_and_int_p, different_types_p));
1094 break;
1095
1096 case FUNCTION_TYPE:
1097 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1098 different_types_p);
1099 break;
1100
1101 case ARRAY_TYPE:
1102 {
1103 tree d1 = TYPE_DOMAIN (t1);
1104 tree d2 = TYPE_DOMAIN (t2);
1105 bool d1_variable, d2_variable;
1106 bool d1_zero, d2_zero;
1107 val = 1;
1108
1109 /* Target types must match incl. qualifiers. */
1110 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1111 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1112 enum_and_int_p,
1113 different_types_p)))
1114 return 0;
1115
1116 if (different_types_p != NULL
1117 && (d1 == 0) != (d2 == 0))
1118 *different_types_p = true;
1119 /* Sizes must match unless one is missing or variable. */
1120 if (d1 == 0 || d2 == 0 || d1 == d2)
1121 break;
1122
1123 d1_zero = !TYPE_MAX_VALUE (d1);
1124 d2_zero = !TYPE_MAX_VALUE (d2);
1125
1126 d1_variable = (!d1_zero
1127 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1128 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1129 d2_variable = (!d2_zero
1130 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1131 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1132 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1133 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1134
1135 if (different_types_p != NULL
1136 && d1_variable != d2_variable)
1137 *different_types_p = true;
1138 if (d1_variable || d2_variable)
1139 break;
1140 if (d1_zero && d2_zero)
1141 break;
1142 if (d1_zero || d2_zero
1143 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1144 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1145 val = 0;
1146
1147 break;
1148 }
1149
1150 case ENUMERAL_TYPE:
1151 case RECORD_TYPE:
1152 case UNION_TYPE:
1153 if (val != 1 && !same_translation_unit_p (t1, t2))
1154 {
1155 tree a1 = TYPE_ATTRIBUTES (t1);
1156 tree a2 = TYPE_ATTRIBUTES (t2);
1157
1158 if (! attribute_list_contained (a1, a2)
1159 && ! attribute_list_contained (a2, a1))
1160 break;
1161
1162 if (attrval != 2)
1163 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1164 different_types_p);
1165 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1166 different_types_p);
1167 }
1168 break;
1169
1170 case VECTOR_TYPE:
1171 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1172 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1173 enum_and_int_p, different_types_p));
1174 break;
1175
1176 default:
1177 break;
1178 }
1179 return attrval == 2 && val == 1 ? 2 : val;
1180 }
1181
1182 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1183 their qualifiers, except for named address spaces. If the pointers point to
1184 different named addresses, then we must determine if one address space is a
1185 subset of the other. */
1186
1187 static int
1188 comp_target_types (location_t location, tree ttl, tree ttr)
1189 {
1190 int val;
1191 tree mvl = TREE_TYPE (ttl);
1192 tree mvr = TREE_TYPE (ttr);
1193 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1194 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1195 addr_space_t as_common;
1196 bool enum_and_int_p;
1197
1198 /* Fail if pointers point to incompatible address spaces. */
1199 if (!addr_space_superset (asl, asr, &as_common))
1200 return 0;
1201
1202 /* Do not lose qualifiers on element types of array types that are
1203 pointer targets by taking their TYPE_MAIN_VARIANT. */
1204 if (TREE_CODE (mvl) != ARRAY_TYPE)
1205 mvl = TYPE_MAIN_VARIANT (mvl);
1206 if (TREE_CODE (mvr) != ARRAY_TYPE)
1207 mvr = TYPE_MAIN_VARIANT (mvr);
1208 enum_and_int_p = false;
1209 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1210
1211 if (val == 2)
1212 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1213
1214 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1215 warning_at (location, OPT_Wc___compat,
1216 "pointer target types incompatible in C++");
1217
1218 return val;
1219 }
1220 \f
1221 /* Subroutines of `comptypes'. */
1222
1223 /* Determine whether two trees derive from the same translation unit.
1224 If the CONTEXT chain ends in a null, that tree's context is still
1225 being parsed, so if two trees have context chains ending in null,
1226 they're in the same translation unit. */
1227 int
1228 same_translation_unit_p (const_tree t1, const_tree t2)
1229 {
1230 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1231 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1232 {
1233 case tcc_declaration:
1234 t1 = DECL_CONTEXT (t1); break;
1235 case tcc_type:
1236 t1 = TYPE_CONTEXT (t1); break;
1237 case tcc_exceptional:
1238 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1239 default: gcc_unreachable ();
1240 }
1241
1242 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1244 {
1245 case tcc_declaration:
1246 t2 = DECL_CONTEXT (t2); break;
1247 case tcc_type:
1248 t2 = TYPE_CONTEXT (t2); break;
1249 case tcc_exceptional:
1250 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1251 default: gcc_unreachable ();
1252 }
1253
1254 return t1 == t2;
1255 }
1256
1257 /* Allocate the seen two types, assuming that they are compatible. */
1258
1259 static struct tagged_tu_seen_cache *
1260 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1261 {
1262 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1263 tu->next = tagged_tu_seen_base;
1264 tu->t1 = t1;
1265 tu->t2 = t2;
1266
1267 tagged_tu_seen_base = tu;
1268
1269 /* The C standard says that two structures in different translation
1270 units are compatible with each other only if the types of their
1271 fields are compatible (among other things). We assume that they
1272 are compatible until proven otherwise when building the cache.
1273 An example where this can occur is:
1274 struct a
1275 {
1276 struct a *next;
1277 };
1278 If we are comparing this against a similar struct in another TU,
1279 and did not assume they were compatible, we end up with an infinite
1280 loop. */
1281 tu->val = 1;
1282 return tu;
1283 }
1284
1285 /* Free the seen types until we get to TU_TIL. */
1286
1287 static void
1288 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1289 {
1290 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1291 while (tu != tu_til)
1292 {
1293 const struct tagged_tu_seen_cache *const tu1
1294 = (const struct tagged_tu_seen_cache *) tu;
1295 tu = tu1->next;
1296 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1297 }
1298 tagged_tu_seen_base = tu_til;
1299 }
1300
1301 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1302 compatible. If the two types are not the same (which has been
1303 checked earlier), this can only happen when multiple translation
1304 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1305 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1306 comptypes_internal. */
1307
1308 static int
1309 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1310 bool *enum_and_int_p, bool *different_types_p)
1311 {
1312 tree s1, s2;
1313 bool needs_warning = false;
1314
1315 /* We have to verify that the tags of the types are the same. This
1316 is harder than it looks because this may be a typedef, so we have
1317 to go look at the original type. It may even be a typedef of a
1318 typedef...
1319 In the case of compiler-created builtin structs the TYPE_DECL
1320 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1321 while (TYPE_NAME (t1)
1322 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1323 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1324 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1325
1326 while (TYPE_NAME (t2)
1327 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1328 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1329 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1330
1331 /* C90 didn't have the requirement that the two tags be the same. */
1332 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1333 return 0;
1334
1335 /* C90 didn't say what happened if one or both of the types were
1336 incomplete; we choose to follow C99 rules here, which is that they
1337 are compatible. */
1338 if (TYPE_SIZE (t1) == NULL
1339 || TYPE_SIZE (t2) == NULL)
1340 return 1;
1341
1342 {
1343 const struct tagged_tu_seen_cache * tts_i;
1344 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1345 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1346 return tts_i->val;
1347 }
1348
1349 switch (TREE_CODE (t1))
1350 {
1351 case ENUMERAL_TYPE:
1352 {
1353 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1354 /* Speed up the case where the type values are in the same order. */
1355 tree tv1 = TYPE_VALUES (t1);
1356 tree tv2 = TYPE_VALUES (t2);
1357
1358 if (tv1 == tv2)
1359 {
1360 return 1;
1361 }
1362
1363 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1364 {
1365 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1366 break;
1367 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1368 {
1369 tu->val = 0;
1370 return 0;
1371 }
1372 }
1373
1374 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1375 {
1376 return 1;
1377 }
1378 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1379 {
1380 tu->val = 0;
1381 return 0;
1382 }
1383
1384 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1385 {
1386 tu->val = 0;
1387 return 0;
1388 }
1389
1390 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1391 {
1392 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1393 if (s2 == NULL
1394 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1395 {
1396 tu->val = 0;
1397 return 0;
1398 }
1399 }
1400 return 1;
1401 }
1402
1403 case UNION_TYPE:
1404 {
1405 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1406 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1407 {
1408 tu->val = 0;
1409 return 0;
1410 }
1411
1412 /* Speed up the common case where the fields are in the same order. */
1413 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1414 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1415 {
1416 int result;
1417
1418 if (DECL_NAME (s1) != DECL_NAME (s2))
1419 break;
1420 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1421 enum_and_int_p, different_types_p);
1422
1423 if (result != 1 && !DECL_NAME (s1))
1424 break;
1425 if (result == 0)
1426 {
1427 tu->val = 0;
1428 return 0;
1429 }
1430 if (result == 2)
1431 needs_warning = true;
1432
1433 if (TREE_CODE (s1) == FIELD_DECL
1434 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1435 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1436 {
1437 tu->val = 0;
1438 return 0;
1439 }
1440 }
1441 if (!s1 && !s2)
1442 {
1443 tu->val = needs_warning ? 2 : 1;
1444 return tu->val;
1445 }
1446
1447 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1448 {
1449 bool ok = false;
1450
1451 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1452 if (DECL_NAME (s1) == DECL_NAME (s2))
1453 {
1454 int result;
1455
1456 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1457 enum_and_int_p,
1458 different_types_p);
1459
1460 if (result != 1 && !DECL_NAME (s1))
1461 continue;
1462 if (result == 0)
1463 {
1464 tu->val = 0;
1465 return 0;
1466 }
1467 if (result == 2)
1468 needs_warning = true;
1469
1470 if (TREE_CODE (s1) == FIELD_DECL
1471 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1472 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1473 break;
1474
1475 ok = true;
1476 break;
1477 }
1478 if (!ok)
1479 {
1480 tu->val = 0;
1481 return 0;
1482 }
1483 }
1484 tu->val = needs_warning ? 2 : 10;
1485 return tu->val;
1486 }
1487
1488 case RECORD_TYPE:
1489 {
1490 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1491
1492 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1493 s1 && s2;
1494 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1495 {
1496 int result;
1497 if (TREE_CODE (s1) != TREE_CODE (s2)
1498 || DECL_NAME (s1) != DECL_NAME (s2))
1499 break;
1500 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1501 enum_and_int_p, different_types_p);
1502 if (result == 0)
1503 break;
1504 if (result == 2)
1505 needs_warning = true;
1506
1507 if (TREE_CODE (s1) == FIELD_DECL
1508 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1509 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1510 break;
1511 }
1512 if (s1 && s2)
1513 tu->val = 0;
1514 else
1515 tu->val = needs_warning ? 2 : 1;
1516 return tu->val;
1517 }
1518
1519 default:
1520 gcc_unreachable ();
1521 }
1522 }
1523
1524 /* Return 1 if two function types F1 and F2 are compatible.
1525 If either type specifies no argument types,
1526 the other must specify a fixed number of self-promoting arg types.
1527 Otherwise, if one type specifies only the number of arguments,
1528 the other must specify that number of self-promoting arg types.
1529 Otherwise, the argument types must match.
1530 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1531
1532 static int
1533 function_types_compatible_p (const_tree f1, const_tree f2,
1534 bool *enum_and_int_p, bool *different_types_p)
1535 {
1536 tree args1, args2;
1537 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1538 int val = 1;
1539 int val1;
1540 tree ret1, ret2;
1541
1542 ret1 = TREE_TYPE (f1);
1543 ret2 = TREE_TYPE (f2);
1544
1545 /* 'volatile' qualifiers on a function's return type used to mean
1546 the function is noreturn. */
1547 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1548 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1549 if (TYPE_VOLATILE (ret1))
1550 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1551 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1552 if (TYPE_VOLATILE (ret2))
1553 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1554 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1555 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1556 if (val == 0)
1557 return 0;
1558
1559 args1 = TYPE_ARG_TYPES (f1);
1560 args2 = TYPE_ARG_TYPES (f2);
1561
1562 if (different_types_p != NULL
1563 && (args1 == 0) != (args2 == 0))
1564 *different_types_p = true;
1565
1566 /* An unspecified parmlist matches any specified parmlist
1567 whose argument types don't need default promotions. */
1568
1569 if (args1 == 0)
1570 {
1571 if (!self_promoting_args_p (args2))
1572 return 0;
1573 /* If one of these types comes from a non-prototype fn definition,
1574 compare that with the other type's arglist.
1575 If they don't match, ask for a warning (but no error). */
1576 if (TYPE_ACTUAL_ARG_TYPES (f1)
1577 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1578 enum_and_int_p, different_types_p))
1579 val = 2;
1580 return val;
1581 }
1582 if (args2 == 0)
1583 {
1584 if (!self_promoting_args_p (args1))
1585 return 0;
1586 if (TYPE_ACTUAL_ARG_TYPES (f2)
1587 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1588 enum_and_int_p, different_types_p))
1589 val = 2;
1590 return val;
1591 }
1592
1593 /* Both types have argument lists: compare them and propagate results. */
1594 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1595 different_types_p);
1596 return val1 != 1 ? val1 : val;
1597 }
1598
1599 /* Check two lists of types for compatibility, returning 0 for
1600 incompatible, 1 for compatible, or 2 for compatible with
1601 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1602 comptypes_internal. */
1603
1604 static int
1605 type_lists_compatible_p (const_tree args1, const_tree args2,
1606 bool *enum_and_int_p, bool *different_types_p)
1607 {
1608 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1609 int val = 1;
1610 int newval = 0;
1611
1612 while (1)
1613 {
1614 tree a1, mv1, a2, mv2;
1615 if (args1 == 0 && args2 == 0)
1616 return val;
1617 /* If one list is shorter than the other,
1618 they fail to match. */
1619 if (args1 == 0 || args2 == 0)
1620 return 0;
1621 mv1 = a1 = TREE_VALUE (args1);
1622 mv2 = a2 = TREE_VALUE (args2);
1623 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1624 mv1 = TYPE_MAIN_VARIANT (mv1);
1625 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1626 mv2 = TYPE_MAIN_VARIANT (mv2);
1627 /* A null pointer instead of a type
1628 means there is supposed to be an argument
1629 but nothing is specified about what type it has.
1630 So match anything that self-promotes. */
1631 if (different_types_p != NULL
1632 && (a1 == 0) != (a2 == 0))
1633 *different_types_p = true;
1634 if (a1 == 0)
1635 {
1636 if (c_type_promotes_to (a2) != a2)
1637 return 0;
1638 }
1639 else if (a2 == 0)
1640 {
1641 if (c_type_promotes_to (a1) != a1)
1642 return 0;
1643 }
1644 /* If one of the lists has an error marker, ignore this arg. */
1645 else if (TREE_CODE (a1) == ERROR_MARK
1646 || TREE_CODE (a2) == ERROR_MARK)
1647 ;
1648 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1649 different_types_p)))
1650 {
1651 if (different_types_p != NULL)
1652 *different_types_p = true;
1653 /* Allow wait (union {union wait *u; int *i} *)
1654 and wait (union wait *) to be compatible. */
1655 if (TREE_CODE (a1) == UNION_TYPE
1656 && (TYPE_NAME (a1) == 0
1657 || TYPE_TRANSPARENT_AGGR (a1))
1658 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1659 && tree_int_cst_equal (TYPE_SIZE (a1),
1660 TYPE_SIZE (a2)))
1661 {
1662 tree memb;
1663 for (memb = TYPE_FIELDS (a1);
1664 memb; memb = DECL_CHAIN (memb))
1665 {
1666 tree mv3 = TREE_TYPE (memb);
1667 if (mv3 && mv3 != error_mark_node
1668 && TREE_CODE (mv3) != ARRAY_TYPE)
1669 mv3 = TYPE_MAIN_VARIANT (mv3);
1670 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1671 different_types_p))
1672 break;
1673 }
1674 if (memb == 0)
1675 return 0;
1676 }
1677 else if (TREE_CODE (a2) == UNION_TYPE
1678 && (TYPE_NAME (a2) == 0
1679 || TYPE_TRANSPARENT_AGGR (a2))
1680 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1681 && tree_int_cst_equal (TYPE_SIZE (a2),
1682 TYPE_SIZE (a1)))
1683 {
1684 tree memb;
1685 for (memb = TYPE_FIELDS (a2);
1686 memb; memb = DECL_CHAIN (memb))
1687 {
1688 tree mv3 = TREE_TYPE (memb);
1689 if (mv3 && mv3 != error_mark_node
1690 && TREE_CODE (mv3) != ARRAY_TYPE)
1691 mv3 = TYPE_MAIN_VARIANT (mv3);
1692 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1693 different_types_p))
1694 break;
1695 }
1696 if (memb == 0)
1697 return 0;
1698 }
1699 else
1700 return 0;
1701 }
1702
1703 /* comptypes said ok, but record if it said to warn. */
1704 if (newval > val)
1705 val = newval;
1706
1707 args1 = TREE_CHAIN (args1);
1708 args2 = TREE_CHAIN (args2);
1709 }
1710 }
1711 \f
1712 /* Compute the size to increment a pointer by. */
1713
1714 static tree
1715 c_size_in_bytes (const_tree type)
1716 {
1717 enum tree_code code = TREE_CODE (type);
1718
1719 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1720 return size_one_node;
1721
1722 if (!COMPLETE_OR_VOID_TYPE_P (type))
1723 {
1724 error ("arithmetic on pointer to an incomplete type");
1725 return size_one_node;
1726 }
1727
1728 /* Convert in case a char is more than one unit. */
1729 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1730 size_int (TYPE_PRECISION (char_type_node)
1731 / BITS_PER_UNIT));
1732 }
1733 \f
1734 /* Return either DECL or its known constant value (if it has one). */
1735
1736 tree
1737 decl_constant_value (tree decl)
1738 {
1739 if (/* Don't change a variable array bound or initial value to a constant
1740 in a place where a variable is invalid. Note that DECL_INITIAL
1741 isn't valid for a PARM_DECL. */
1742 current_function_decl != 0
1743 && TREE_CODE (decl) != PARM_DECL
1744 && !TREE_THIS_VOLATILE (decl)
1745 && TREE_READONLY (decl)
1746 && DECL_INITIAL (decl) != 0
1747 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1748 /* This is invalid if initial value is not constant.
1749 If it has either a function call, a memory reference,
1750 or a variable, then re-evaluating it could give different results. */
1751 && TREE_CONSTANT (DECL_INITIAL (decl))
1752 /* Check for cases where this is sub-optimal, even though valid. */
1753 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1754 return DECL_INITIAL (decl);
1755 return decl;
1756 }
1757
1758 /* Convert the array expression EXP to a pointer. */
1759 static tree
1760 array_to_pointer_conversion (location_t loc, tree exp)
1761 {
1762 tree orig_exp = exp;
1763 tree type = TREE_TYPE (exp);
1764 tree adr;
1765 tree restype = TREE_TYPE (type);
1766 tree ptrtype;
1767
1768 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1769
1770 STRIP_TYPE_NOPS (exp);
1771
1772 if (TREE_NO_WARNING (orig_exp))
1773 TREE_NO_WARNING (exp) = 1;
1774
1775 ptrtype = build_pointer_type (restype);
1776
1777 if (TREE_CODE (exp) == INDIRECT_REF)
1778 return convert (ptrtype, TREE_OPERAND (exp, 0));
1779
1780 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1781 return convert (ptrtype, adr);
1782 }
1783
1784 /* Convert the function expression EXP to a pointer. */
1785 static tree
1786 function_to_pointer_conversion (location_t loc, tree exp)
1787 {
1788 tree orig_exp = exp;
1789
1790 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1791
1792 STRIP_TYPE_NOPS (exp);
1793
1794 if (TREE_NO_WARNING (orig_exp))
1795 TREE_NO_WARNING (exp) = 1;
1796
1797 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1798 }
1799
1800 /* Mark EXP as read, not just set, for set but not used -Wunused
1801 warning purposes. */
1802
1803 void
1804 mark_exp_read (tree exp)
1805 {
1806 switch (TREE_CODE (exp))
1807 {
1808 case VAR_DECL:
1809 case PARM_DECL:
1810 DECL_READ_P (exp) = 1;
1811 break;
1812 case ARRAY_REF:
1813 case COMPONENT_REF:
1814 case MODIFY_EXPR:
1815 case REALPART_EXPR:
1816 case IMAGPART_EXPR:
1817 CASE_CONVERT:
1818 case ADDR_EXPR:
1819 mark_exp_read (TREE_OPERAND (exp, 0));
1820 break;
1821 case COMPOUND_EXPR:
1822 case C_MAYBE_CONST_EXPR:
1823 mark_exp_read (TREE_OPERAND (exp, 1));
1824 break;
1825 default:
1826 break;
1827 }
1828 }
1829
1830 /* Perform the default conversion of arrays and functions to pointers.
1831 Return the result of converting EXP. For any other expression, just
1832 return EXP.
1833
1834 LOC is the location of the expression. */
1835
1836 struct c_expr
1837 default_function_array_conversion (location_t loc, struct c_expr exp)
1838 {
1839 tree orig_exp = exp.value;
1840 tree type = TREE_TYPE (exp.value);
1841 enum tree_code code = TREE_CODE (type);
1842
1843 switch (code)
1844 {
1845 case ARRAY_TYPE:
1846 {
1847 bool not_lvalue = false;
1848 bool lvalue_array_p;
1849
1850 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1851 || CONVERT_EXPR_P (exp.value))
1852 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1853 {
1854 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1855 not_lvalue = true;
1856 exp.value = TREE_OPERAND (exp.value, 0);
1857 }
1858
1859 if (TREE_NO_WARNING (orig_exp))
1860 TREE_NO_WARNING (exp.value) = 1;
1861
1862 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1863 if (!flag_isoc99 && !lvalue_array_p)
1864 {
1865 /* Before C99, non-lvalue arrays do not decay to pointers.
1866 Normally, using such an array would be invalid; but it can
1867 be used correctly inside sizeof or as a statement expression.
1868 Thus, do not give an error here; an error will result later. */
1869 return exp;
1870 }
1871
1872 exp.value = array_to_pointer_conversion (loc, exp.value);
1873 }
1874 break;
1875 case FUNCTION_TYPE:
1876 exp.value = function_to_pointer_conversion (loc, exp.value);
1877 break;
1878 default:
1879 break;
1880 }
1881
1882 return exp;
1883 }
1884
1885 struct c_expr
1886 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1887 {
1888 mark_exp_read (exp.value);
1889 return default_function_array_conversion (loc, exp);
1890 }
1891
1892 /* EXP is an expression of integer type. Apply the integer promotions
1893 to it and return the promoted value. */
1894
1895 tree
1896 perform_integral_promotions (tree exp)
1897 {
1898 tree type = TREE_TYPE (exp);
1899 enum tree_code code = TREE_CODE (type);
1900
1901 gcc_assert (INTEGRAL_TYPE_P (type));
1902
1903 /* Normally convert enums to int,
1904 but convert wide enums to something wider. */
1905 if (code == ENUMERAL_TYPE)
1906 {
1907 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1908 TYPE_PRECISION (integer_type_node)),
1909 ((TYPE_PRECISION (type)
1910 >= TYPE_PRECISION (integer_type_node))
1911 && TYPE_UNSIGNED (type)));
1912
1913 return convert (type, exp);
1914 }
1915
1916 /* ??? This should no longer be needed now bit-fields have their
1917 proper types. */
1918 if (TREE_CODE (exp) == COMPONENT_REF
1919 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1920 /* If it's thinner than an int, promote it like a
1921 c_promoting_integer_type_p, otherwise leave it alone. */
1922 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1923 TYPE_PRECISION (integer_type_node)))
1924 return convert (integer_type_node, exp);
1925
1926 if (c_promoting_integer_type_p (type))
1927 {
1928 /* Preserve unsignedness if not really getting any wider. */
1929 if (TYPE_UNSIGNED (type)
1930 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1931 return convert (unsigned_type_node, exp);
1932
1933 return convert (integer_type_node, exp);
1934 }
1935
1936 return exp;
1937 }
1938
1939
1940 /* Perform default promotions for C data used in expressions.
1941 Enumeral types or short or char are converted to int.
1942 In addition, manifest constants symbols are replaced by their values. */
1943
1944 tree
1945 default_conversion (tree exp)
1946 {
1947 tree orig_exp;
1948 tree type = TREE_TYPE (exp);
1949 enum tree_code code = TREE_CODE (type);
1950 tree promoted_type;
1951
1952 mark_exp_read (exp);
1953
1954 /* Functions and arrays have been converted during parsing. */
1955 gcc_assert (code != FUNCTION_TYPE);
1956 if (code == ARRAY_TYPE)
1957 return exp;
1958
1959 /* Constants can be used directly unless they're not loadable. */
1960 if (TREE_CODE (exp) == CONST_DECL)
1961 exp = DECL_INITIAL (exp);
1962
1963 /* Strip no-op conversions. */
1964 orig_exp = exp;
1965 STRIP_TYPE_NOPS (exp);
1966
1967 if (TREE_NO_WARNING (orig_exp))
1968 TREE_NO_WARNING (exp) = 1;
1969
1970 if (code == VOID_TYPE)
1971 {
1972 error ("void value not ignored as it ought to be");
1973 return error_mark_node;
1974 }
1975
1976 exp = require_complete_type (exp);
1977 if (exp == error_mark_node)
1978 return error_mark_node;
1979
1980 promoted_type = targetm.promoted_type (type);
1981 if (promoted_type)
1982 return convert (promoted_type, exp);
1983
1984 if (INTEGRAL_TYPE_P (type))
1985 return perform_integral_promotions (exp);
1986
1987 return exp;
1988 }
1989 \f
1990 /* Look up COMPONENT in a structure or union TYPE.
1991
1992 If the component name is not found, returns NULL_TREE. Otherwise,
1993 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1994 stepping down the chain to the component, which is in the last
1995 TREE_VALUE of the list. Normally the list is of length one, but if
1996 the component is embedded within (nested) anonymous structures or
1997 unions, the list steps down the chain to the component. */
1998
1999 static tree
2000 lookup_field (tree type, tree component)
2001 {
2002 tree field;
2003
2004 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2005 to the field elements. Use a binary search on this array to quickly
2006 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2007 will always be set for structures which have many elements. */
2008
2009 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2010 {
2011 int bot, top, half;
2012 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2013
2014 field = TYPE_FIELDS (type);
2015 bot = 0;
2016 top = TYPE_LANG_SPECIFIC (type)->s->len;
2017 while (top - bot > 1)
2018 {
2019 half = (top - bot + 1) >> 1;
2020 field = field_array[bot+half];
2021
2022 if (DECL_NAME (field) == NULL_TREE)
2023 {
2024 /* Step through all anon unions in linear fashion. */
2025 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2026 {
2027 field = field_array[bot++];
2028 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2029 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2030 {
2031 tree anon = lookup_field (TREE_TYPE (field), component);
2032
2033 if (anon)
2034 return tree_cons (NULL_TREE, field, anon);
2035
2036 /* The Plan 9 compiler permits referring
2037 directly to an anonymous struct/union field
2038 using a typedef name. */
2039 if (flag_plan9_extensions
2040 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2041 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2042 == TYPE_DECL)
2043 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2044 == component))
2045 break;
2046 }
2047 }
2048
2049 /* Entire record is only anon unions. */
2050 if (bot > top)
2051 return NULL_TREE;
2052
2053 /* Restart the binary search, with new lower bound. */
2054 continue;
2055 }
2056
2057 if (DECL_NAME (field) == component)
2058 break;
2059 if (DECL_NAME (field) < component)
2060 bot += half;
2061 else
2062 top = bot + half;
2063 }
2064
2065 if (DECL_NAME (field_array[bot]) == component)
2066 field = field_array[bot];
2067 else if (DECL_NAME (field) != component)
2068 return NULL_TREE;
2069 }
2070 else
2071 {
2072 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2073 {
2074 if (DECL_NAME (field) == NULL_TREE
2075 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2076 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2077 {
2078 tree anon = lookup_field (TREE_TYPE (field), component);
2079
2080 if (anon)
2081 return tree_cons (NULL_TREE, field, anon);
2082
2083 /* The Plan 9 compiler permits referring directly to an
2084 anonymous struct/union field using a typedef
2085 name. */
2086 if (flag_plan9_extensions
2087 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2088 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2089 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2090 == component))
2091 break;
2092 }
2093
2094 if (DECL_NAME (field) == component)
2095 break;
2096 }
2097
2098 if (field == NULL_TREE)
2099 return NULL_TREE;
2100 }
2101
2102 return tree_cons (NULL_TREE, field, NULL_TREE);
2103 }
2104
2105 /* Make an expression to refer to the COMPONENT field of structure or
2106 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2107 location of the COMPONENT_REF. */
2108
2109 tree
2110 build_component_ref (location_t loc, tree datum, tree component)
2111 {
2112 tree type = TREE_TYPE (datum);
2113 enum tree_code code = TREE_CODE (type);
2114 tree field = NULL;
2115 tree ref;
2116 bool datum_lvalue = lvalue_p (datum);
2117
2118 if (!objc_is_public (datum, component))
2119 return error_mark_node;
2120
2121 /* Detect Objective-C property syntax object.property. */
2122 if (c_dialect_objc ()
2123 && (ref = objc_maybe_build_component_ref (datum, component)))
2124 return ref;
2125
2126 /* See if there is a field or component with name COMPONENT. */
2127
2128 if (code == RECORD_TYPE || code == UNION_TYPE)
2129 {
2130 if (!COMPLETE_TYPE_P (type))
2131 {
2132 c_incomplete_type_error (NULL_TREE, type);
2133 return error_mark_node;
2134 }
2135
2136 field = lookup_field (type, component);
2137
2138 if (!field)
2139 {
2140 error_at (loc, "%qT has no member named %qE", type, component);
2141 return error_mark_node;
2142 }
2143
2144 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2145 This might be better solved in future the way the C++ front
2146 end does it - by giving the anonymous entities each a
2147 separate name and type, and then have build_component_ref
2148 recursively call itself. We can't do that here. */
2149 do
2150 {
2151 tree subdatum = TREE_VALUE (field);
2152 int quals;
2153 tree subtype;
2154 bool use_datum_quals;
2155
2156 if (TREE_TYPE (subdatum) == error_mark_node)
2157 return error_mark_node;
2158
2159 /* If this is an rvalue, it does not have qualifiers in C
2160 standard terms and we must avoid propagating such
2161 qualifiers down to a non-lvalue array that is then
2162 converted to a pointer. */
2163 use_datum_quals = (datum_lvalue
2164 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2165
2166 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2167 if (use_datum_quals)
2168 quals |= TYPE_QUALS (TREE_TYPE (datum));
2169 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2170
2171 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2172 NULL_TREE);
2173 SET_EXPR_LOCATION (ref, loc);
2174 if (TREE_READONLY (subdatum)
2175 || (use_datum_quals && TREE_READONLY (datum)))
2176 TREE_READONLY (ref) = 1;
2177 if (TREE_THIS_VOLATILE (subdatum)
2178 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2179 TREE_THIS_VOLATILE (ref) = 1;
2180
2181 if (TREE_DEPRECATED (subdatum))
2182 warn_deprecated_use (subdatum, NULL_TREE);
2183
2184 datum = ref;
2185
2186 field = TREE_CHAIN (field);
2187 }
2188 while (field);
2189
2190 return ref;
2191 }
2192 else if (code != ERROR_MARK)
2193 error_at (loc,
2194 "request for member %qE in something not a structure or union",
2195 component);
2196
2197 return error_mark_node;
2198 }
2199 \f
2200 /* Given an expression PTR for a pointer, return an expression
2201 for the value pointed to.
2202 ERRORSTRING is the name of the operator to appear in error messages.
2203
2204 LOC is the location to use for the generated tree. */
2205
2206 tree
2207 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2208 {
2209 tree pointer = default_conversion (ptr);
2210 tree type = TREE_TYPE (pointer);
2211 tree ref;
2212
2213 if (TREE_CODE (type) == POINTER_TYPE)
2214 {
2215 if (CONVERT_EXPR_P (pointer)
2216 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2217 {
2218 /* If a warning is issued, mark it to avoid duplicates from
2219 the backend. This only needs to be done at
2220 warn_strict_aliasing > 2. */
2221 if (warn_strict_aliasing > 2)
2222 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2223 type, TREE_OPERAND (pointer, 0)))
2224 TREE_NO_WARNING (pointer) = 1;
2225 }
2226
2227 if (TREE_CODE (pointer) == ADDR_EXPR
2228 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2229 == TREE_TYPE (type)))
2230 {
2231 ref = TREE_OPERAND (pointer, 0);
2232 protected_set_expr_location (ref, loc);
2233 return ref;
2234 }
2235 else
2236 {
2237 tree t = TREE_TYPE (type);
2238
2239 ref = build1 (INDIRECT_REF, t, pointer);
2240
2241 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2242 {
2243 error_at (loc, "dereferencing pointer to incomplete type");
2244 return error_mark_node;
2245 }
2246 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2247 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2248
2249 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2250 so that we get the proper error message if the result is used
2251 to assign to. Also, &* is supposed to be a no-op.
2252 And ANSI C seems to specify that the type of the result
2253 should be the const type. */
2254 /* A de-reference of a pointer to const is not a const. It is valid
2255 to change it via some other pointer. */
2256 TREE_READONLY (ref) = TYPE_READONLY (t);
2257 TREE_SIDE_EFFECTS (ref)
2258 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2259 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2260 protected_set_expr_location (ref, loc);
2261 return ref;
2262 }
2263 }
2264 else if (TREE_CODE (pointer) != ERROR_MARK)
2265 invalid_indirection_error (loc, type, errstring);
2266
2267 return error_mark_node;
2268 }
2269
2270 /* This handles expressions of the form "a[i]", which denotes
2271 an array reference.
2272
2273 This is logically equivalent in C to *(a+i), but we may do it differently.
2274 If A is a variable or a member, we generate a primitive ARRAY_REF.
2275 This avoids forcing the array out of registers, and can work on
2276 arrays that are not lvalues (for example, members of structures returned
2277 by functions).
2278
2279 For vector types, allow vector[i] but not i[vector], and create
2280 *(((type*)&vectortype) + i) for the expression.
2281
2282 LOC is the location to use for the returned expression. */
2283
2284 tree
2285 build_array_ref (location_t loc, tree array, tree index)
2286 {
2287 tree ret;
2288 bool swapped = false;
2289 if (TREE_TYPE (array) == error_mark_node
2290 || TREE_TYPE (index) == error_mark_node)
2291 return error_mark_node;
2292
2293 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2294 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2295 /* Allow vector[index] but not index[vector]. */
2296 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2297 {
2298 tree temp;
2299 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2300 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2301 {
2302 error_at (loc,
2303 "subscripted value is neither array nor pointer nor vector");
2304
2305 return error_mark_node;
2306 }
2307 temp = array;
2308 array = index;
2309 index = temp;
2310 swapped = true;
2311 }
2312
2313 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2314 {
2315 error_at (loc, "array subscript is not an integer");
2316 return error_mark_node;
2317 }
2318
2319 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2320 {
2321 error_at (loc, "subscripted value is pointer to function");
2322 return error_mark_node;
2323 }
2324
2325 /* ??? Existing practice has been to warn only when the char
2326 index is syntactically the index, not for char[array]. */
2327 if (!swapped)
2328 warn_array_subscript_with_type_char (index);
2329
2330 /* Apply default promotions *after* noticing character types. */
2331 index = default_conversion (index);
2332
2333 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2334
2335 /* For vector[index], convert the vector to a
2336 pointer of the underlying type. */
2337 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2338 {
2339 tree type = TREE_TYPE (array);
2340 tree type1;
2341
2342 if (TREE_CODE (index) == INTEGER_CST)
2343 if (!host_integerp (index, 1)
2344 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2345 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2346 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2347
2348 c_common_mark_addressable_vec (array);
2349 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2350 type = build_pointer_type (type);
2351 type1 = build_pointer_type (TREE_TYPE (array));
2352 array = build1 (ADDR_EXPR, type1, array);
2353 array = convert (type, array);
2354 }
2355
2356 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2357 {
2358 tree rval, type;
2359
2360 /* An array that is indexed by a non-constant
2361 cannot be stored in a register; we must be able to do
2362 address arithmetic on its address.
2363 Likewise an array of elements of variable size. */
2364 if (TREE_CODE (index) != INTEGER_CST
2365 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2366 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2367 {
2368 if (!c_mark_addressable (array))
2369 return error_mark_node;
2370 }
2371 /* An array that is indexed by a constant value which is not within
2372 the array bounds cannot be stored in a register either; because we
2373 would get a crash in store_bit_field/extract_bit_field when trying
2374 to access a non-existent part of the register. */
2375 if (TREE_CODE (index) == INTEGER_CST
2376 && TYPE_DOMAIN (TREE_TYPE (array))
2377 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2378 {
2379 if (!c_mark_addressable (array))
2380 return error_mark_node;
2381 }
2382
2383 if (pedantic)
2384 {
2385 tree foo = array;
2386 while (TREE_CODE (foo) == COMPONENT_REF)
2387 foo = TREE_OPERAND (foo, 0);
2388 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2389 pedwarn (loc, OPT_pedantic,
2390 "ISO C forbids subscripting %<register%> array");
2391 else if (!flag_isoc99 && !lvalue_p (foo))
2392 pedwarn (loc, OPT_pedantic,
2393 "ISO C90 forbids subscripting non-lvalue array");
2394 }
2395
2396 type = TREE_TYPE (TREE_TYPE (array));
2397 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2398 /* Array ref is const/volatile if the array elements are
2399 or if the array is. */
2400 TREE_READONLY (rval)
2401 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2402 | TREE_READONLY (array));
2403 TREE_SIDE_EFFECTS (rval)
2404 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2405 | TREE_SIDE_EFFECTS (array));
2406 TREE_THIS_VOLATILE (rval)
2407 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2408 /* This was added by rms on 16 Nov 91.
2409 It fixes vol struct foo *a; a->elts[1]
2410 in an inline function.
2411 Hope it doesn't break something else. */
2412 | TREE_THIS_VOLATILE (array));
2413 ret = require_complete_type (rval);
2414 protected_set_expr_location (ret, loc);
2415 return ret;
2416 }
2417 else
2418 {
2419 tree ar = default_conversion (array);
2420
2421 if (ar == error_mark_node)
2422 return ar;
2423
2424 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2425 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2426
2427 return build_indirect_ref
2428 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2429 RO_ARRAY_INDEXING);
2430 }
2431 }
2432 \f
2433 /* Build an external reference to identifier ID. FUN indicates
2434 whether this will be used for a function call. LOC is the source
2435 location of the identifier. This sets *TYPE to the type of the
2436 identifier, which is not the same as the type of the returned value
2437 for CONST_DECLs defined as enum constants. If the type of the
2438 identifier is not available, *TYPE is set to NULL. */
2439 tree
2440 build_external_ref (location_t loc, tree id, int fun, tree *type)
2441 {
2442 tree ref;
2443 tree decl = lookup_name (id);
2444
2445 /* In Objective-C, an instance variable (ivar) may be preferred to
2446 whatever lookup_name() found. */
2447 decl = objc_lookup_ivar (decl, id);
2448
2449 *type = NULL;
2450 if (decl && decl != error_mark_node)
2451 {
2452 ref = decl;
2453 *type = TREE_TYPE (ref);
2454 }
2455 else if (fun)
2456 /* Implicit function declaration. */
2457 ref = implicitly_declare (loc, id);
2458 else if (decl == error_mark_node)
2459 /* Don't complain about something that's already been
2460 complained about. */
2461 return error_mark_node;
2462 else
2463 {
2464 undeclared_variable (loc, id);
2465 return error_mark_node;
2466 }
2467
2468 if (TREE_TYPE (ref) == error_mark_node)
2469 return error_mark_node;
2470
2471 if (TREE_DEPRECATED (ref))
2472 warn_deprecated_use (ref, NULL_TREE);
2473
2474 /* Recursive call does not count as usage. */
2475 if (ref != current_function_decl)
2476 {
2477 TREE_USED (ref) = 1;
2478 }
2479
2480 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2481 {
2482 if (!in_sizeof && !in_typeof)
2483 C_DECL_USED (ref) = 1;
2484 else if (DECL_INITIAL (ref) == 0
2485 && DECL_EXTERNAL (ref)
2486 && !TREE_PUBLIC (ref))
2487 record_maybe_used_decl (ref);
2488 }
2489
2490 if (TREE_CODE (ref) == CONST_DECL)
2491 {
2492 used_types_insert (TREE_TYPE (ref));
2493
2494 if (warn_cxx_compat
2495 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2496 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2497 {
2498 warning_at (loc, OPT_Wc___compat,
2499 ("enum constant defined in struct or union "
2500 "is not visible in C++"));
2501 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2502 }
2503
2504 ref = DECL_INITIAL (ref);
2505 TREE_CONSTANT (ref) = 1;
2506 }
2507 else if (current_function_decl != 0
2508 && !DECL_FILE_SCOPE_P (current_function_decl)
2509 && (TREE_CODE (ref) == VAR_DECL
2510 || TREE_CODE (ref) == PARM_DECL
2511 || TREE_CODE (ref) == FUNCTION_DECL))
2512 {
2513 tree context = decl_function_context (ref);
2514
2515 if (context != 0 && context != current_function_decl)
2516 DECL_NONLOCAL (ref) = 1;
2517 }
2518 /* C99 6.7.4p3: An inline definition of a function with external
2519 linkage ... shall not contain a reference to an identifier with
2520 internal linkage. */
2521 else if (current_function_decl != 0
2522 && DECL_DECLARED_INLINE_P (current_function_decl)
2523 && DECL_EXTERNAL (current_function_decl)
2524 && VAR_OR_FUNCTION_DECL_P (ref)
2525 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2526 && ! TREE_PUBLIC (ref)
2527 && DECL_CONTEXT (ref) != current_function_decl)
2528 record_inline_static (loc, current_function_decl, ref,
2529 csi_internal);
2530
2531 return ref;
2532 }
2533
2534 /* Record details of decls possibly used inside sizeof or typeof. */
2535 struct maybe_used_decl
2536 {
2537 /* The decl. */
2538 tree decl;
2539 /* The level seen at (in_sizeof + in_typeof). */
2540 int level;
2541 /* The next one at this level or above, or NULL. */
2542 struct maybe_used_decl *next;
2543 };
2544
2545 static struct maybe_used_decl *maybe_used_decls;
2546
2547 /* Record that DECL, an undefined static function reference seen
2548 inside sizeof or typeof, might be used if the operand of sizeof is
2549 a VLA type or the operand of typeof is a variably modified
2550 type. */
2551
2552 static void
2553 record_maybe_used_decl (tree decl)
2554 {
2555 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2556 t->decl = decl;
2557 t->level = in_sizeof + in_typeof;
2558 t->next = maybe_used_decls;
2559 maybe_used_decls = t;
2560 }
2561
2562 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2563 USED is false, just discard them. If it is true, mark them used
2564 (if no longer inside sizeof or typeof) or move them to the next
2565 level up (if still inside sizeof or typeof). */
2566
2567 void
2568 pop_maybe_used (bool used)
2569 {
2570 struct maybe_used_decl *p = maybe_used_decls;
2571 int cur_level = in_sizeof + in_typeof;
2572 while (p && p->level > cur_level)
2573 {
2574 if (used)
2575 {
2576 if (cur_level == 0)
2577 C_DECL_USED (p->decl) = 1;
2578 else
2579 p->level = cur_level;
2580 }
2581 p = p->next;
2582 }
2583 if (!used || cur_level == 0)
2584 maybe_used_decls = p;
2585 }
2586
2587 /* Return the result of sizeof applied to EXPR. */
2588
2589 struct c_expr
2590 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2591 {
2592 struct c_expr ret;
2593 if (expr.value == error_mark_node)
2594 {
2595 ret.value = error_mark_node;
2596 ret.original_code = ERROR_MARK;
2597 ret.original_type = NULL;
2598 pop_maybe_used (false);
2599 }
2600 else
2601 {
2602 bool expr_const_operands = true;
2603 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2604 &expr_const_operands);
2605 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2606 ret.original_code = ERROR_MARK;
2607 ret.original_type = NULL;
2608 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2609 {
2610 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2611 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2612 folded_expr, ret.value);
2613 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2614 SET_EXPR_LOCATION (ret.value, loc);
2615 }
2616 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2617 }
2618 return ret;
2619 }
2620
2621 /* Return the result of sizeof applied to T, a structure for the type
2622 name passed to sizeof (rather than the type itself). LOC is the
2623 location of the original expression. */
2624
2625 struct c_expr
2626 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2627 {
2628 tree type;
2629 struct c_expr ret;
2630 tree type_expr = NULL_TREE;
2631 bool type_expr_const = true;
2632 type = groktypename (t, &type_expr, &type_expr_const);
2633 ret.value = c_sizeof (loc, type);
2634 ret.original_code = ERROR_MARK;
2635 ret.original_type = NULL;
2636 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2637 && c_vla_type_p (type))
2638 {
2639 /* If the type is a [*] array, it is a VLA but is represented as
2640 having a size of zero. In such a case we must ensure that
2641 the result of sizeof does not get folded to a constant by
2642 c_fully_fold, because if the size is evaluated the result is
2643 not constant and so constraints on zero or negative size
2644 arrays must not be applied when this sizeof call is inside
2645 another array declarator. */
2646 if (!type_expr)
2647 type_expr = integer_zero_node;
2648 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2649 type_expr, ret.value);
2650 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2651 }
2652 pop_maybe_used (type != error_mark_node
2653 ? C_TYPE_VARIABLE_SIZE (type) : false);
2654 return ret;
2655 }
2656
2657 /* Build a function call to function FUNCTION with parameters PARAMS.
2658 The function call is at LOC.
2659 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2660 TREE_VALUE of each node is a parameter-expression.
2661 FUNCTION's data type may be a function type or a pointer-to-function. */
2662
2663 tree
2664 build_function_call (location_t loc, tree function, tree params)
2665 {
2666 VEC(tree,gc) *vec;
2667 tree ret;
2668
2669 vec = VEC_alloc (tree, gc, list_length (params));
2670 for (; params; params = TREE_CHAIN (params))
2671 VEC_quick_push (tree, vec, TREE_VALUE (params));
2672 ret = build_function_call_vec (loc, function, vec, NULL);
2673 VEC_free (tree, gc, vec);
2674 return ret;
2675 }
2676
2677 /* Build a function call to function FUNCTION with parameters PARAMS.
2678 ORIGTYPES, if not NULL, is a vector of types; each element is
2679 either NULL or the original type of the corresponding element in
2680 PARAMS. The original type may differ from TREE_TYPE of the
2681 parameter for enums. FUNCTION's data type may be a function type
2682 or pointer-to-function. This function changes the elements of
2683 PARAMS. */
2684
2685 tree
2686 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2687 VEC(tree,gc) *origtypes)
2688 {
2689 tree fntype, fundecl = 0;
2690 tree name = NULL_TREE, result;
2691 tree tem;
2692 int nargs;
2693 tree *argarray;
2694
2695
2696 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2697 STRIP_TYPE_NOPS (function);
2698
2699 /* Convert anything with function type to a pointer-to-function. */
2700 if (TREE_CODE (function) == FUNCTION_DECL)
2701 {
2702 /* Implement type-directed function overloading for builtins.
2703 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2704 handle all the type checking. The result is a complete expression
2705 that implements this function call. */
2706 tem = resolve_overloaded_builtin (loc, function, params);
2707 if (tem)
2708 return tem;
2709
2710 name = DECL_NAME (function);
2711 fundecl = function;
2712 }
2713 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2714 function = function_to_pointer_conversion (loc, function);
2715
2716 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2717 expressions, like those used for ObjC messenger dispatches. */
2718 if (!VEC_empty (tree, params))
2719 function = objc_rewrite_function_call (function,
2720 VEC_index (tree, params, 0));
2721
2722 function = c_fully_fold (function, false, NULL);
2723
2724 fntype = TREE_TYPE (function);
2725
2726 if (TREE_CODE (fntype) == ERROR_MARK)
2727 return error_mark_node;
2728
2729 if (!(TREE_CODE (fntype) == POINTER_TYPE
2730 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2731 {
2732 error_at (loc, "called object %qE is not a function", function);
2733 return error_mark_node;
2734 }
2735
2736 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2737 current_function_returns_abnormally = 1;
2738
2739 /* fntype now gets the type of function pointed to. */
2740 fntype = TREE_TYPE (fntype);
2741
2742 /* Convert the parameters to the types declared in the
2743 function prototype, or apply default promotions. */
2744
2745 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2746 function, fundecl);
2747 if (nargs < 0)
2748 return error_mark_node;
2749
2750 /* Check that the function is called through a compatible prototype.
2751 If it is not, replace the call by a trap, wrapped up in a compound
2752 expression if necessary. This has the nice side-effect to prevent
2753 the tree-inliner from generating invalid assignment trees which may
2754 blow up in the RTL expander later. */
2755 if (CONVERT_EXPR_P (function)
2756 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2757 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2758 && !comptypes (fntype, TREE_TYPE (tem)))
2759 {
2760 tree return_type = TREE_TYPE (fntype);
2761 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2762 NULL_TREE);
2763 int i;
2764
2765 /* This situation leads to run-time undefined behavior. We can't,
2766 therefore, simply error unless we can prove that all possible
2767 executions of the program must execute the code. */
2768 if (warning_at (loc, 0, "function called through a non-compatible type"))
2769 /* We can, however, treat "undefined" any way we please.
2770 Call abort to encourage the user to fix the program. */
2771 inform (loc, "if this code is reached, the program will abort");
2772 /* Before the abort, allow the function arguments to exit or
2773 call longjmp. */
2774 for (i = 0; i < nargs; i++)
2775 trap = build2 (COMPOUND_EXPR, void_type_node,
2776 VEC_index (tree, params, i), trap);
2777
2778 if (VOID_TYPE_P (return_type))
2779 {
2780 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2781 pedwarn (loc, 0,
2782 "function with qualified void return type called");
2783 return trap;
2784 }
2785 else
2786 {
2787 tree rhs;
2788
2789 if (AGGREGATE_TYPE_P (return_type))
2790 rhs = build_compound_literal (loc, return_type,
2791 build_constructor (return_type, 0),
2792 false);
2793 else
2794 rhs = build_zero_cst (return_type);
2795
2796 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2797 trap, rhs));
2798 }
2799 }
2800
2801 argarray = VEC_address (tree, params);
2802
2803 /* Check that arguments to builtin functions match the expectations. */
2804 if (fundecl
2805 && DECL_BUILT_IN (fundecl)
2806 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2807 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2808 return error_mark_node;
2809
2810 /* Check that the arguments to the function are valid. */
2811 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2812 TYPE_ARG_TYPES (fntype));
2813
2814 if (name != NULL_TREE
2815 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2816 {
2817 if (require_constant_value)
2818 result =
2819 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2820 function, nargs, argarray);
2821 else
2822 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2823 function, nargs, argarray);
2824 if (TREE_CODE (result) == NOP_EXPR
2825 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2826 STRIP_TYPE_NOPS (result);
2827 }
2828 else
2829 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2830 function, nargs, argarray);
2831
2832 if (VOID_TYPE_P (TREE_TYPE (result)))
2833 {
2834 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2835 pedwarn (loc, 0,
2836 "function with qualified void return type called");
2837 return result;
2838 }
2839 return require_complete_type (result);
2840 }
2841 \f
2842 /* Convert the argument expressions in the vector VALUES
2843 to the types in the list TYPELIST.
2844
2845 If TYPELIST is exhausted, or when an element has NULL as its type,
2846 perform the default conversions.
2847
2848 ORIGTYPES is the original types of the expressions in VALUES. This
2849 holds the type of enum values which have been converted to integral
2850 types. It may be NULL.
2851
2852 FUNCTION is a tree for the called function. It is used only for
2853 error messages, where it is formatted with %qE.
2854
2855 This is also where warnings about wrong number of args are generated.
2856
2857 Returns the actual number of arguments processed (which may be less
2858 than the length of VALUES in some error situations), or -1 on
2859 failure. */
2860
2861 static int
2862 convert_arguments (tree typelist, VEC(tree,gc) *values,
2863 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2864 {
2865 tree typetail, val;
2866 unsigned int parmnum;
2867 bool error_args = false;
2868 const bool type_generic = fundecl
2869 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2870 bool type_generic_remove_excess_precision = false;
2871 tree selector;
2872
2873 /* Change pointer to function to the function itself for
2874 diagnostics. */
2875 if (TREE_CODE (function) == ADDR_EXPR
2876 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2877 function = TREE_OPERAND (function, 0);
2878
2879 /* Handle an ObjC selector specially for diagnostics. */
2880 selector = objc_message_selector ();
2881
2882 /* For type-generic built-in functions, determine whether excess
2883 precision should be removed (classification) or not
2884 (comparison). */
2885 if (type_generic
2886 && DECL_BUILT_IN (fundecl)
2887 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2888 {
2889 switch (DECL_FUNCTION_CODE (fundecl))
2890 {
2891 case BUILT_IN_ISFINITE:
2892 case BUILT_IN_ISINF:
2893 case BUILT_IN_ISINF_SIGN:
2894 case BUILT_IN_ISNAN:
2895 case BUILT_IN_ISNORMAL:
2896 case BUILT_IN_FPCLASSIFY:
2897 type_generic_remove_excess_precision = true;
2898 break;
2899
2900 default:
2901 type_generic_remove_excess_precision = false;
2902 break;
2903 }
2904 }
2905
2906 /* Scan the given expressions and types, producing individual
2907 converted arguments. */
2908
2909 for (typetail = typelist, parmnum = 0;
2910 VEC_iterate (tree, values, parmnum, val);
2911 ++parmnum)
2912 {
2913 tree type = typetail ? TREE_VALUE (typetail) : 0;
2914 tree valtype = TREE_TYPE (val);
2915 tree rname = function;
2916 int argnum = parmnum + 1;
2917 const char *invalid_func_diag;
2918 bool excess_precision = false;
2919 bool npc;
2920 tree parmval;
2921
2922 if (type == void_type_node)
2923 {
2924 if (selector)
2925 error_at (input_location,
2926 "too many arguments to method %qE", selector);
2927 else
2928 error_at (input_location,
2929 "too many arguments to function %qE", function);
2930
2931 if (fundecl && !DECL_BUILT_IN (fundecl))
2932 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2933 return parmnum;
2934 }
2935
2936 if (selector && argnum > 2)
2937 {
2938 rname = selector;
2939 argnum -= 2;
2940 }
2941
2942 npc = null_pointer_constant_p (val);
2943
2944 /* If there is excess precision and a prototype, convert once to
2945 the required type rather than converting via the semantic
2946 type. Likewise without a prototype a float value represented
2947 as long double should be converted once to double. But for
2948 type-generic classification functions excess precision must
2949 be removed here. */
2950 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2951 && (type || !type_generic || !type_generic_remove_excess_precision))
2952 {
2953 val = TREE_OPERAND (val, 0);
2954 excess_precision = true;
2955 }
2956 val = c_fully_fold (val, false, NULL);
2957 STRIP_TYPE_NOPS (val);
2958
2959 val = require_complete_type (val);
2960
2961 if (type != 0)
2962 {
2963 /* Formal parm type is specified by a function prototype. */
2964
2965 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2966 {
2967 error ("type of formal parameter %d is incomplete", parmnum + 1);
2968 parmval = val;
2969 }
2970 else
2971 {
2972 tree origtype;
2973
2974 /* Optionally warn about conversions that
2975 differ from the default conversions. */
2976 if (warn_traditional_conversion || warn_traditional)
2977 {
2978 unsigned int formal_prec = TYPE_PRECISION (type);
2979
2980 if (INTEGRAL_TYPE_P (type)
2981 && TREE_CODE (valtype) == REAL_TYPE)
2982 warning (0, "passing argument %d of %qE as integer "
2983 "rather than floating due to prototype",
2984 argnum, rname);
2985 if (INTEGRAL_TYPE_P (type)
2986 && TREE_CODE (valtype) == COMPLEX_TYPE)
2987 warning (0, "passing argument %d of %qE as integer "
2988 "rather than complex due to prototype",
2989 argnum, rname);
2990 else if (TREE_CODE (type) == COMPLEX_TYPE
2991 && TREE_CODE (valtype) == REAL_TYPE)
2992 warning (0, "passing argument %d of %qE as complex "
2993 "rather than floating due to prototype",
2994 argnum, rname);
2995 else if (TREE_CODE (type) == REAL_TYPE
2996 && INTEGRAL_TYPE_P (valtype))
2997 warning (0, "passing argument %d of %qE as floating "
2998 "rather than integer due to prototype",
2999 argnum, rname);
3000 else if (TREE_CODE (type) == COMPLEX_TYPE
3001 && INTEGRAL_TYPE_P (valtype))
3002 warning (0, "passing argument %d of %qE as complex "
3003 "rather than integer due to prototype",
3004 argnum, rname);
3005 else if (TREE_CODE (type) == REAL_TYPE
3006 && TREE_CODE (valtype) == COMPLEX_TYPE)
3007 warning (0, "passing argument %d of %qE as floating "
3008 "rather than complex due to prototype",
3009 argnum, rname);
3010 /* ??? At some point, messages should be written about
3011 conversions between complex types, but that's too messy
3012 to do now. */
3013 else if (TREE_CODE (type) == REAL_TYPE
3014 && TREE_CODE (valtype) == REAL_TYPE)
3015 {
3016 /* Warn if any argument is passed as `float',
3017 since without a prototype it would be `double'. */
3018 if (formal_prec == TYPE_PRECISION (float_type_node)
3019 && type != dfloat32_type_node)
3020 warning (0, "passing argument %d of %qE as %<float%> "
3021 "rather than %<double%> due to prototype",
3022 argnum, rname);
3023
3024 /* Warn if mismatch between argument and prototype
3025 for decimal float types. Warn of conversions with
3026 binary float types and of precision narrowing due to
3027 prototype. */
3028 else if (type != valtype
3029 && (type == dfloat32_type_node
3030 || type == dfloat64_type_node
3031 || type == dfloat128_type_node
3032 || valtype == dfloat32_type_node
3033 || valtype == dfloat64_type_node
3034 || valtype == dfloat128_type_node)
3035 && (formal_prec
3036 <= TYPE_PRECISION (valtype)
3037 || (type == dfloat128_type_node
3038 && (valtype
3039 != dfloat64_type_node
3040 && (valtype
3041 != dfloat32_type_node)))
3042 || (type == dfloat64_type_node
3043 && (valtype
3044 != dfloat32_type_node))))
3045 warning (0, "passing argument %d of %qE as %qT "
3046 "rather than %qT due to prototype",
3047 argnum, rname, type, valtype);
3048
3049 }
3050 /* Detect integer changing in width or signedness.
3051 These warnings are only activated with
3052 -Wtraditional-conversion, not with -Wtraditional. */
3053 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3054 && INTEGRAL_TYPE_P (valtype))
3055 {
3056 tree would_have_been = default_conversion (val);
3057 tree type1 = TREE_TYPE (would_have_been);
3058
3059 if (TREE_CODE (type) == ENUMERAL_TYPE
3060 && (TYPE_MAIN_VARIANT (type)
3061 == TYPE_MAIN_VARIANT (valtype)))
3062 /* No warning if function asks for enum
3063 and the actual arg is that enum type. */
3064 ;
3065 else if (formal_prec != TYPE_PRECISION (type1))
3066 warning (OPT_Wtraditional_conversion,
3067 "passing argument %d of %qE "
3068 "with different width due to prototype",
3069 argnum, rname);
3070 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3071 ;
3072 /* Don't complain if the formal parameter type
3073 is an enum, because we can't tell now whether
3074 the value was an enum--even the same enum. */
3075 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3076 ;
3077 else if (TREE_CODE (val) == INTEGER_CST
3078 && int_fits_type_p (val, type))
3079 /* Change in signedness doesn't matter
3080 if a constant value is unaffected. */
3081 ;
3082 /* If the value is extended from a narrower
3083 unsigned type, it doesn't matter whether we
3084 pass it as signed or unsigned; the value
3085 certainly is the same either way. */
3086 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3087 && TYPE_UNSIGNED (valtype))
3088 ;
3089 else if (TYPE_UNSIGNED (type))
3090 warning (OPT_Wtraditional_conversion,
3091 "passing argument %d of %qE "
3092 "as unsigned due to prototype",
3093 argnum, rname);
3094 else
3095 warning (OPT_Wtraditional_conversion,
3096 "passing argument %d of %qE "
3097 "as signed due to prototype", argnum, rname);
3098 }
3099 }
3100
3101 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3102 sake of better warnings from convert_and_check. */
3103 if (excess_precision)
3104 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3105 origtype = (origtypes == NULL
3106 ? NULL_TREE
3107 : VEC_index (tree, origtypes, parmnum));
3108 parmval = convert_for_assignment (input_location, type, val,
3109 origtype, ic_argpass, npc,
3110 fundecl, function,
3111 parmnum + 1);
3112
3113 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3114 && INTEGRAL_TYPE_P (type)
3115 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3116 parmval = default_conversion (parmval);
3117 }
3118 }
3119 else if (TREE_CODE (valtype) == REAL_TYPE
3120 && (TYPE_PRECISION (valtype)
3121 < TYPE_PRECISION (double_type_node))
3122 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3123 {
3124 if (type_generic)
3125 parmval = val;
3126 else
3127 {
3128 /* Convert `float' to `double'. */
3129 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3130 warning (OPT_Wdouble_promotion,
3131 "implicit conversion from %qT to %qT when passing "
3132 "argument to function",
3133 valtype, double_type_node);
3134 parmval = convert (double_type_node, val);
3135 }
3136 }
3137 else if (excess_precision && !type_generic)
3138 /* A "double" argument with excess precision being passed
3139 without a prototype or in variable arguments. */
3140 parmval = convert (valtype, val);
3141 else if ((invalid_func_diag =
3142 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3143 {
3144 error (invalid_func_diag);
3145 return -1;
3146 }
3147 else
3148 /* Convert `short' and `char' to full-size `int'. */
3149 parmval = default_conversion (val);
3150
3151 VEC_replace (tree, values, parmnum, parmval);
3152 if (parmval == error_mark_node)
3153 error_args = true;
3154
3155 if (typetail)
3156 typetail = TREE_CHAIN (typetail);
3157 }
3158
3159 gcc_assert (parmnum == VEC_length (tree, values));
3160
3161 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3162 {
3163 error_at (input_location,
3164 "too few arguments to function %qE", function);
3165 if (fundecl && !DECL_BUILT_IN (fundecl))
3166 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3167 return -1;
3168 }
3169
3170 return error_args ? -1 : (int) parmnum;
3171 }
3172 \f
3173 /* This is the entry point used by the parser to build unary operators
3174 in the input. CODE, a tree_code, specifies the unary operator, and
3175 ARG is the operand. For unary plus, the C parser currently uses
3176 CONVERT_EXPR for code.
3177
3178 LOC is the location to use for the tree generated.
3179 */
3180
3181 struct c_expr
3182 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3183 {
3184 struct c_expr result;
3185
3186 result.value = build_unary_op (loc, code, arg.value, 0);
3187 result.original_code = code;
3188 result.original_type = NULL;
3189
3190 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3191 overflow_warning (loc, result.value);
3192
3193 return result;
3194 }
3195
3196 /* This is the entry point used by the parser to build binary operators
3197 in the input. CODE, a tree_code, specifies the binary operator, and
3198 ARG1 and ARG2 are the operands. In addition to constructing the
3199 expression, we check for operands that were written with other binary
3200 operators in a way that is likely to confuse the user.
3201
3202 LOCATION is the location of the binary operator. */
3203
3204 struct c_expr
3205 parser_build_binary_op (location_t location, enum tree_code code,
3206 struct c_expr arg1, struct c_expr arg2)
3207 {
3208 struct c_expr result;
3209
3210 enum tree_code code1 = arg1.original_code;
3211 enum tree_code code2 = arg2.original_code;
3212 tree type1 = (arg1.original_type
3213 ? arg1.original_type
3214 : TREE_TYPE (arg1.value));
3215 tree type2 = (arg2.original_type
3216 ? arg2.original_type
3217 : TREE_TYPE (arg2.value));
3218
3219 result.value = build_binary_op (location, code,
3220 arg1.value, arg2.value, 1);
3221 result.original_code = code;
3222 result.original_type = NULL;
3223
3224 if (TREE_CODE (result.value) == ERROR_MARK)
3225 return result;
3226
3227 if (location != UNKNOWN_LOCATION)
3228 protected_set_expr_location (result.value, location);
3229
3230 /* Check for cases such as x+y<<z which users are likely
3231 to misinterpret. */
3232 if (warn_parentheses)
3233 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3234
3235 if (warn_logical_op)
3236 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3237 code1, arg1.value, code2, arg2.value);
3238
3239 /* Warn about comparisons against string literals, with the exception
3240 of testing for equality or inequality of a string literal with NULL. */
3241 if (code == EQ_EXPR || code == NE_EXPR)
3242 {
3243 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3244 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3245 warning_at (location, OPT_Waddress,
3246 "comparison with string literal results in unspecified behavior");
3247 }
3248 else if (TREE_CODE_CLASS (code) == tcc_comparison
3249 && (code1 == STRING_CST || code2 == STRING_CST))
3250 warning_at (location, OPT_Waddress,
3251 "comparison with string literal results in unspecified behavior");
3252
3253 if (TREE_OVERFLOW_P (result.value)
3254 && !TREE_OVERFLOW_P (arg1.value)
3255 && !TREE_OVERFLOW_P (arg2.value))
3256 overflow_warning (location, result.value);
3257
3258 /* Warn about comparisons of different enum types. */
3259 if (warn_enum_compare
3260 && TREE_CODE_CLASS (code) == tcc_comparison
3261 && TREE_CODE (type1) == ENUMERAL_TYPE
3262 && TREE_CODE (type2) == ENUMERAL_TYPE
3263 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3264 warning_at (location, OPT_Wenum_compare,
3265 "comparison between %qT and %qT",
3266 type1, type2);
3267
3268 return result;
3269 }
3270 \f
3271 /* Return a tree for the difference of pointers OP0 and OP1.
3272 The resulting tree has type int. */
3273
3274 static tree
3275 pointer_diff (location_t loc, tree op0, tree op1)
3276 {
3277 tree restype = ptrdiff_type_node;
3278 tree result, inttype;
3279
3280 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3281 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3282 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3283 tree con0, con1, lit0, lit1;
3284 tree orig_op1 = op1;
3285
3286 /* If the operands point into different address spaces, we need to
3287 explicitly convert them to pointers into the common address space
3288 before we can subtract the numerical address values. */
3289 if (as0 != as1)
3290 {
3291 addr_space_t as_common;
3292 tree common_type;
3293
3294 /* Determine the common superset address space. This is guaranteed
3295 to exist because the caller verified that comp_target_types
3296 returned non-zero. */
3297 if (!addr_space_superset (as0, as1, &as_common))
3298 gcc_unreachable ();
3299
3300 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3301 op0 = convert (common_type, op0);
3302 op1 = convert (common_type, op1);
3303 }
3304
3305 /* Determine integer type to perform computations in. This will usually
3306 be the same as the result type (ptrdiff_t), but may need to be a wider
3307 type if pointers for the address space are wider than ptrdiff_t. */
3308 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3309 inttype = lang_hooks.types.type_for_size
3310 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3311 else
3312 inttype = restype;
3313
3314
3315 if (TREE_CODE (target_type) == VOID_TYPE)
3316 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3317 "pointer of type %<void *%> used in subtraction");
3318 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3319 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3320 "pointer to a function used in subtraction");
3321
3322 /* If the conversion to ptrdiff_type does anything like widening or
3323 converting a partial to an integral mode, we get a convert_expression
3324 that is in the way to do any simplifications.
3325 (fold-const.c doesn't know that the extra bits won't be needed.
3326 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3327 different mode in place.)
3328 So first try to find a common term here 'by hand'; we want to cover
3329 at least the cases that occur in legal static initializers. */
3330 if (CONVERT_EXPR_P (op0)
3331 && (TYPE_PRECISION (TREE_TYPE (op0))
3332 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3333 con0 = TREE_OPERAND (op0, 0);
3334 else
3335 con0 = op0;
3336 if (CONVERT_EXPR_P (op1)
3337 && (TYPE_PRECISION (TREE_TYPE (op1))
3338 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3339 con1 = TREE_OPERAND (op1, 0);
3340 else
3341 con1 = op1;
3342
3343 if (TREE_CODE (con0) == PLUS_EXPR)
3344 {
3345 lit0 = TREE_OPERAND (con0, 1);
3346 con0 = TREE_OPERAND (con0, 0);
3347 }
3348 else
3349 lit0 = integer_zero_node;
3350
3351 if (TREE_CODE (con1) == PLUS_EXPR)
3352 {
3353 lit1 = TREE_OPERAND (con1, 1);
3354 con1 = TREE_OPERAND (con1, 0);
3355 }
3356 else
3357 lit1 = integer_zero_node;
3358
3359 if (operand_equal_p (con0, con1, 0))
3360 {
3361 op0 = lit0;
3362 op1 = lit1;
3363 }
3364
3365
3366 /* First do the subtraction as integers;
3367 then drop through to build the divide operator.
3368 Do not do default conversions on the minus operator
3369 in case restype is a short type. */
3370
3371 op0 = build_binary_op (loc,
3372 MINUS_EXPR, convert (inttype, op0),
3373 convert (inttype, op1), 0);
3374 /* This generates an error if op1 is pointer to incomplete type. */
3375 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3376 error_at (loc, "arithmetic on pointer to an incomplete type");
3377
3378 /* This generates an error if op0 is pointer to incomplete type. */
3379 op1 = c_size_in_bytes (target_type);
3380
3381 /* Divide by the size, in easiest possible way. */
3382 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3383 op0, convert (inttype, op1));
3384
3385 /* Convert to final result type if necessary. */
3386 return convert (restype, result);
3387 }
3388 \f
3389 /* Construct and perhaps optimize a tree representation
3390 for a unary operation. CODE, a tree_code, specifies the operation
3391 and XARG is the operand.
3392 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3393 the default promotions (such as from short to int).
3394 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3395 allows non-lvalues; this is only used to handle conversion of non-lvalue
3396 arrays to pointers in C99.
3397
3398 LOCATION is the location of the operator. */
3399
3400 tree
3401 build_unary_op (location_t location,
3402 enum tree_code code, tree xarg, int flag)
3403 {
3404 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3405 tree arg = xarg;
3406 tree argtype = 0;
3407 enum tree_code typecode;
3408 tree val;
3409 tree ret = error_mark_node;
3410 tree eptype = NULL_TREE;
3411 int noconvert = flag;
3412 const char *invalid_op_diag;
3413 bool int_operands;
3414
3415 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3416 if (int_operands)
3417 arg = remove_c_maybe_const_expr (arg);
3418
3419 if (code != ADDR_EXPR)
3420 arg = require_complete_type (arg);
3421
3422 typecode = TREE_CODE (TREE_TYPE (arg));
3423 if (typecode == ERROR_MARK)
3424 return error_mark_node;
3425 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3426 typecode = INTEGER_TYPE;
3427
3428 if ((invalid_op_diag
3429 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3430 {
3431 error_at (location, invalid_op_diag);
3432 return error_mark_node;
3433 }
3434
3435 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3436 {
3437 eptype = TREE_TYPE (arg);
3438 arg = TREE_OPERAND (arg, 0);
3439 }
3440
3441 switch (code)
3442 {
3443 case CONVERT_EXPR:
3444 /* This is used for unary plus, because a CONVERT_EXPR
3445 is enough to prevent anybody from looking inside for
3446 associativity, but won't generate any code. */
3447 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3448 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3449 || typecode == VECTOR_TYPE))
3450 {
3451 error_at (location, "wrong type argument to unary plus");
3452 return error_mark_node;
3453 }
3454 else if (!noconvert)
3455 arg = default_conversion (arg);
3456 arg = non_lvalue_loc (location, arg);
3457 break;
3458
3459 case NEGATE_EXPR:
3460 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3461 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3462 || typecode == VECTOR_TYPE))
3463 {
3464 error_at (location, "wrong type argument to unary minus");
3465 return error_mark_node;
3466 }
3467 else if (!noconvert)
3468 arg = default_conversion (arg);
3469 break;
3470
3471 case BIT_NOT_EXPR:
3472 /* ~ works on integer types and non float vectors. */
3473 if (typecode == INTEGER_TYPE
3474 || (typecode == VECTOR_TYPE
3475 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3476 {
3477 if (!noconvert)
3478 arg = default_conversion (arg);
3479 }
3480 else if (typecode == COMPLEX_TYPE)
3481 {
3482 code = CONJ_EXPR;
3483 pedwarn (location, OPT_pedantic,
3484 "ISO C does not support %<~%> for complex conjugation");
3485 if (!noconvert)
3486 arg = default_conversion (arg);
3487 }
3488 else
3489 {
3490 error_at (location, "wrong type argument to bit-complement");
3491 return error_mark_node;
3492 }
3493 break;
3494
3495 case ABS_EXPR:
3496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3497 {
3498 error_at (location, "wrong type argument to abs");
3499 return error_mark_node;
3500 }
3501 else if (!noconvert)
3502 arg = default_conversion (arg);
3503 break;
3504
3505 case CONJ_EXPR:
3506 /* Conjugating a real value is a no-op, but allow it anyway. */
3507 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3508 || typecode == COMPLEX_TYPE))
3509 {
3510 error_at (location, "wrong type argument to conjugation");
3511 return error_mark_node;
3512 }
3513 else if (!noconvert)
3514 arg = default_conversion (arg);
3515 break;
3516
3517 case TRUTH_NOT_EXPR:
3518 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3519 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3520 && typecode != COMPLEX_TYPE)
3521 {
3522 error_at (location,
3523 "wrong type argument to unary exclamation mark");
3524 return error_mark_node;
3525 }
3526 arg = c_objc_common_truthvalue_conversion (location, arg);
3527 ret = invert_truthvalue_loc (location, arg);
3528 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3529 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3530 location = EXPR_LOCATION (ret);
3531 goto return_build_unary_op;
3532
3533 case REALPART_EXPR:
3534 case IMAGPART_EXPR:
3535 ret = build_real_imag_expr (location, code, arg);
3536 if (ret == error_mark_node)
3537 return error_mark_node;
3538 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3539 eptype = TREE_TYPE (eptype);
3540 goto return_build_unary_op;
3541
3542 case PREINCREMENT_EXPR:
3543 case POSTINCREMENT_EXPR:
3544 case PREDECREMENT_EXPR:
3545 case POSTDECREMENT_EXPR:
3546
3547 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3548 {
3549 tree inner = build_unary_op (location, code,
3550 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3551 if (inner == error_mark_node)
3552 return error_mark_node;
3553 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3554 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3555 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3556 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3557 goto return_build_unary_op;
3558 }
3559
3560 /* Complain about anything that is not a true lvalue. In
3561 Objective-C, skip this check for property_refs. */
3562 if (!objc_is_property_ref (arg)
3563 && !lvalue_or_else (location,
3564 arg, ((code == PREINCREMENT_EXPR
3565 || code == POSTINCREMENT_EXPR)
3566 ? lv_increment
3567 : lv_decrement)))
3568 return error_mark_node;
3569
3570 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3571 {
3572 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3573 warning_at (location, OPT_Wc___compat,
3574 "increment of enumeration value is invalid in C++");
3575 else
3576 warning_at (location, OPT_Wc___compat,
3577 "decrement of enumeration value is invalid in C++");
3578 }
3579
3580 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3581 arg = c_fully_fold (arg, false, NULL);
3582
3583 /* Increment or decrement the real part of the value,
3584 and don't change the imaginary part. */
3585 if (typecode == COMPLEX_TYPE)
3586 {
3587 tree real, imag;
3588
3589 pedwarn (location, OPT_pedantic,
3590 "ISO C does not support %<++%> and %<--%> on complex types");
3591
3592 arg = stabilize_reference (arg);
3593 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3594 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3595 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3596 if (real == error_mark_node || imag == error_mark_node)
3597 return error_mark_node;
3598 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3599 real, imag);
3600 goto return_build_unary_op;
3601 }
3602
3603 /* Report invalid types. */
3604
3605 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3606 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3607 {
3608 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3609 error_at (location, "wrong type argument to increment");
3610 else
3611 error_at (location, "wrong type argument to decrement");
3612
3613 return error_mark_node;
3614 }
3615
3616 {
3617 tree inc;
3618
3619 argtype = TREE_TYPE (arg);
3620
3621 /* Compute the increment. */
3622
3623 if (typecode == POINTER_TYPE)
3624 {
3625 /* If pointer target is an undefined struct,
3626 we just cannot know how to do the arithmetic. */
3627 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3628 {
3629 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3630 error_at (location,
3631 "increment of pointer to unknown structure");
3632 else
3633 error_at (location,
3634 "decrement of pointer to unknown structure");
3635 }
3636 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3637 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3638 {
3639 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3640 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3641 "wrong type argument to increment");
3642 else
3643 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3644 "wrong type argument to decrement");
3645 }
3646
3647 inc = c_size_in_bytes (TREE_TYPE (argtype));
3648 inc = fold_convert_loc (location, sizetype, inc);
3649 }
3650 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3651 {
3652 /* For signed fract types, we invert ++ to -- or
3653 -- to ++, and change inc from 1 to -1, because
3654 it is not possible to represent 1 in signed fract constants.
3655 For unsigned fract types, the result always overflows and
3656 we get an undefined (original) or the maximum value. */
3657 if (code == PREINCREMENT_EXPR)
3658 code = PREDECREMENT_EXPR;
3659 else if (code == PREDECREMENT_EXPR)
3660 code = PREINCREMENT_EXPR;
3661 else if (code == POSTINCREMENT_EXPR)
3662 code = POSTDECREMENT_EXPR;
3663 else /* code == POSTDECREMENT_EXPR */
3664 code = POSTINCREMENT_EXPR;
3665
3666 inc = integer_minus_one_node;
3667 inc = convert (argtype, inc);
3668 }
3669 else
3670 {
3671 inc = integer_one_node;
3672 inc = convert (argtype, inc);
3673 }
3674
3675 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3676 need to ask Objective-C to build the increment or decrement
3677 expression for it. */
3678 if (objc_is_property_ref (arg))
3679 return objc_build_incr_expr_for_property_ref (location, code,
3680 arg, inc);
3681
3682 /* Report a read-only lvalue. */
3683 if (TYPE_READONLY (argtype))
3684 {
3685 readonly_error (arg,
3686 ((code == PREINCREMENT_EXPR
3687 || code == POSTINCREMENT_EXPR)
3688 ? lv_increment : lv_decrement));
3689 return error_mark_node;
3690 }
3691 else if (TREE_READONLY (arg))
3692 readonly_warning (arg,
3693 ((code == PREINCREMENT_EXPR
3694 || code == POSTINCREMENT_EXPR)
3695 ? lv_increment : lv_decrement));
3696
3697 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3698 val = boolean_increment (code, arg);
3699 else
3700 val = build2 (code, TREE_TYPE (arg), arg, inc);
3701 TREE_SIDE_EFFECTS (val) = 1;
3702 if (TREE_CODE (val) != code)
3703 TREE_NO_WARNING (val) = 1;
3704 ret = val;
3705 goto return_build_unary_op;
3706 }
3707
3708 case ADDR_EXPR:
3709 /* Note that this operation never does default_conversion. */
3710
3711 /* The operand of unary '&' must be an lvalue (which excludes
3712 expressions of type void), or, in C99, the result of a [] or
3713 unary '*' operator. */
3714 if (VOID_TYPE_P (TREE_TYPE (arg))
3715 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3716 && (TREE_CODE (arg) != INDIRECT_REF
3717 || !flag_isoc99))
3718 pedwarn (location, 0, "taking address of expression of type %<void%>");
3719
3720 /* Let &* cancel out to simplify resulting code. */
3721 if (TREE_CODE (arg) == INDIRECT_REF)
3722 {
3723 /* Don't let this be an lvalue. */
3724 if (lvalue_p (TREE_OPERAND (arg, 0)))
3725 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3726 ret = TREE_OPERAND (arg, 0);
3727 goto return_build_unary_op;
3728 }
3729
3730 /* For &x[y], return x+y */
3731 if (TREE_CODE (arg) == ARRAY_REF)
3732 {
3733 tree op0 = TREE_OPERAND (arg, 0);
3734 if (!c_mark_addressable (op0))
3735 return error_mark_node;
3736 }
3737
3738 /* Anything not already handled and not a true memory reference
3739 or a non-lvalue array is an error. */
3740 else if (typecode != FUNCTION_TYPE && !flag
3741 && !lvalue_or_else (location, arg, lv_addressof))
3742 return error_mark_node;
3743
3744 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3745 folding later. */
3746 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3747 {
3748 tree inner = build_unary_op (location, code,
3749 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3750 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3751 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3752 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3753 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3754 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3755 goto return_build_unary_op;
3756 }
3757
3758 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3759 argtype = TREE_TYPE (arg);
3760
3761 /* If the lvalue is const or volatile, merge that into the type
3762 to which the address will point. This is only needed
3763 for function types. */
3764 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3765 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3766 && TREE_CODE (argtype) == FUNCTION_TYPE)
3767 {
3768 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3769 int quals = orig_quals;
3770
3771 if (TREE_READONLY (arg))
3772 quals |= TYPE_QUAL_CONST;
3773 if (TREE_THIS_VOLATILE (arg))
3774 quals |= TYPE_QUAL_VOLATILE;
3775
3776 argtype = c_build_qualified_type (argtype, quals);
3777 }
3778
3779 if (!c_mark_addressable (arg))
3780 return error_mark_node;
3781
3782 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3783 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3784
3785 argtype = build_pointer_type (argtype);
3786
3787 /* ??? Cope with user tricks that amount to offsetof. Delete this
3788 when we have proper support for integer constant expressions. */
3789 val = get_base_address (arg);
3790 if (val && TREE_CODE (val) == INDIRECT_REF
3791 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3792 {
3793 tree op0 = fold_convert_loc (location, sizetype,
3794 fold_offsetof (arg, val)), op1;
3795
3796 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3797 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3798 goto return_build_unary_op;
3799 }
3800
3801 val = build1 (ADDR_EXPR, argtype, arg);
3802
3803 ret = val;
3804 goto return_build_unary_op;
3805
3806 default:
3807 gcc_unreachable ();
3808 }
3809
3810 if (argtype == 0)
3811 argtype = TREE_TYPE (arg);
3812 if (TREE_CODE (arg) == INTEGER_CST)
3813 ret = (require_constant_value
3814 ? fold_build1_initializer_loc (location, code, argtype, arg)
3815 : fold_build1_loc (location, code, argtype, arg));
3816 else
3817 ret = build1 (code, argtype, arg);
3818 return_build_unary_op:
3819 gcc_assert (ret != error_mark_node);
3820 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3821 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3822 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3823 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3824 ret = note_integer_operands (ret);
3825 if (eptype)
3826 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3827 protected_set_expr_location (ret, location);
3828 return ret;
3829 }
3830
3831 /* Return nonzero if REF is an lvalue valid for this language.
3832 Lvalues can be assigned, unless their type has TYPE_READONLY.
3833 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3834
3835 bool
3836 lvalue_p (const_tree ref)
3837 {
3838 const enum tree_code code = TREE_CODE (ref);
3839
3840 switch (code)
3841 {
3842 case REALPART_EXPR:
3843 case IMAGPART_EXPR:
3844 case COMPONENT_REF:
3845 return lvalue_p (TREE_OPERAND (ref, 0));
3846
3847 case C_MAYBE_CONST_EXPR:
3848 return lvalue_p (TREE_OPERAND (ref, 1));
3849
3850 case COMPOUND_LITERAL_EXPR:
3851 case STRING_CST:
3852 return 1;
3853
3854 case INDIRECT_REF:
3855 case ARRAY_REF:
3856 case VAR_DECL:
3857 case PARM_DECL:
3858 case RESULT_DECL:
3859 case ERROR_MARK:
3860 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3861 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3862
3863 case BIND_EXPR:
3864 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3865
3866 default:
3867 return 0;
3868 }
3869 }
3870 \f
3871 /* Give a warning for storing in something that is read-only in GCC
3872 terms but not const in ISO C terms. */
3873
3874 static void
3875 readonly_warning (tree arg, enum lvalue_use use)
3876 {
3877 switch (use)
3878 {
3879 case lv_assign:
3880 warning (0, "assignment of read-only location %qE", arg);
3881 break;
3882 case lv_increment:
3883 warning (0, "increment of read-only location %qE", arg);
3884 break;
3885 case lv_decrement:
3886 warning (0, "decrement of read-only location %qE", arg);
3887 break;
3888 default:
3889 gcc_unreachable ();
3890 }
3891 return;
3892 }
3893
3894
3895 /* Return nonzero if REF is an lvalue valid for this language;
3896 otherwise, print an error message and return zero. USE says
3897 how the lvalue is being used and so selects the error message.
3898 LOCATION is the location at which any error should be reported. */
3899
3900 static int
3901 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3902 {
3903 int win = lvalue_p (ref);
3904
3905 if (!win)
3906 lvalue_error (loc, use);
3907
3908 return win;
3909 }
3910 \f
3911 /* Mark EXP saying that we need to be able to take the
3912 address of it; it should not be allocated in a register.
3913 Returns true if successful. */
3914
3915 bool
3916 c_mark_addressable (tree exp)
3917 {
3918 tree x = exp;
3919
3920 while (1)
3921 switch (TREE_CODE (x))
3922 {
3923 case COMPONENT_REF:
3924 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3925 {
3926 error
3927 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3928 return false;
3929 }
3930
3931 /* ... fall through ... */
3932
3933 case ADDR_EXPR:
3934 case ARRAY_REF:
3935 case REALPART_EXPR:
3936 case IMAGPART_EXPR:
3937 x = TREE_OPERAND (x, 0);
3938 break;
3939
3940 case COMPOUND_LITERAL_EXPR:
3941 case CONSTRUCTOR:
3942 TREE_ADDRESSABLE (x) = 1;
3943 return true;
3944
3945 case VAR_DECL:
3946 case CONST_DECL:
3947 case PARM_DECL:
3948 case RESULT_DECL:
3949 if (C_DECL_REGISTER (x)
3950 && DECL_NONLOCAL (x))
3951 {
3952 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3953 {
3954 error
3955 ("global register variable %qD used in nested function", x);
3956 return false;
3957 }
3958 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3959 }
3960 else if (C_DECL_REGISTER (x))
3961 {
3962 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3963 error ("address of global register variable %qD requested", x);
3964 else
3965 error ("address of register variable %qD requested", x);
3966 return false;
3967 }
3968
3969 /* drops in */
3970 case FUNCTION_DECL:
3971 TREE_ADDRESSABLE (x) = 1;
3972 /* drops out */
3973 default:
3974 return true;
3975 }
3976 }
3977 \f
3978 /* Convert EXPR to TYPE, warning about conversion problems with
3979 constants. SEMANTIC_TYPE is the type this conversion would use
3980 without excess precision. If SEMANTIC_TYPE is NULL, this function
3981 is equivalent to convert_and_check. This function is a wrapper that
3982 handles conversions that may be different than
3983 the usual ones because of excess precision. */
3984
3985 static tree
3986 ep_convert_and_check (tree type, tree expr, tree semantic_type)
3987 {
3988 if (TREE_TYPE (expr) == type)
3989 return expr;
3990
3991 if (!semantic_type)
3992 return convert_and_check (type, expr);
3993
3994 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3995 && TREE_TYPE (expr) != semantic_type)
3996 {
3997 /* For integers, we need to check the real conversion, not
3998 the conversion to the excess precision type. */
3999 expr = convert_and_check (semantic_type, expr);
4000 }
4001 /* Result type is the excess precision type, which should be
4002 large enough, so do not check. */
4003 return convert (type, expr);
4004 }
4005
4006 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4007 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4008 if folded to an integer constant then the unselected half may
4009 contain arbitrary operations not normally permitted in constant
4010 expressions. Set the location of the expression to LOC. */
4011
4012 tree
4013 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4014 tree op1, tree op1_original_type, tree op2,
4015 tree op2_original_type)
4016 {
4017 tree type1;
4018 tree type2;
4019 enum tree_code code1;
4020 enum tree_code code2;
4021 tree result_type = NULL;
4022 tree semantic_result_type = NULL;
4023 tree orig_op1 = op1, orig_op2 = op2;
4024 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4025 bool ifexp_int_operands;
4026 tree ret;
4027
4028 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4029 if (op1_int_operands)
4030 op1 = remove_c_maybe_const_expr (op1);
4031 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4032 if (op2_int_operands)
4033 op2 = remove_c_maybe_const_expr (op2);
4034 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4035 if (ifexp_int_operands)
4036 ifexp = remove_c_maybe_const_expr (ifexp);
4037
4038 /* Promote both alternatives. */
4039
4040 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4041 op1 = default_conversion (op1);
4042 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4043 op2 = default_conversion (op2);
4044
4045 if (TREE_CODE (ifexp) == ERROR_MARK
4046 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4047 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4048 return error_mark_node;
4049
4050 type1 = TREE_TYPE (op1);
4051 code1 = TREE_CODE (type1);
4052 type2 = TREE_TYPE (op2);
4053 code2 = TREE_CODE (type2);
4054
4055 /* C90 does not permit non-lvalue arrays in conditional expressions.
4056 In C99 they will be pointers by now. */
4057 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4058 {
4059 error_at (colon_loc, "non-lvalue array in conditional expression");
4060 return error_mark_node;
4061 }
4062
4063 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4064 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4065 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4066 || code1 == COMPLEX_TYPE)
4067 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4068 || code2 == COMPLEX_TYPE))
4069 {
4070 semantic_result_type = c_common_type (type1, type2);
4071 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4072 {
4073 op1 = TREE_OPERAND (op1, 0);
4074 type1 = TREE_TYPE (op1);
4075 gcc_assert (TREE_CODE (type1) == code1);
4076 }
4077 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4078 {
4079 op2 = TREE_OPERAND (op2, 0);
4080 type2 = TREE_TYPE (op2);
4081 gcc_assert (TREE_CODE (type2) == code2);
4082 }
4083 }
4084
4085 if (warn_cxx_compat)
4086 {
4087 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4088 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4089
4090 if (TREE_CODE (t1) == ENUMERAL_TYPE
4091 && TREE_CODE (t2) == ENUMERAL_TYPE
4092 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4093 warning_at (colon_loc, OPT_Wc___compat,
4094 ("different enum types in conditional is "
4095 "invalid in C++: %qT vs %qT"),
4096 t1, t2);
4097 }
4098
4099 /* Quickly detect the usual case where op1 and op2 have the same type
4100 after promotion. */
4101 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4102 {
4103 if (type1 == type2)
4104 result_type = type1;
4105 else
4106 result_type = TYPE_MAIN_VARIANT (type1);
4107 }
4108 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4109 || code1 == COMPLEX_TYPE)
4110 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4111 || code2 == COMPLEX_TYPE))
4112 {
4113 result_type = c_common_type (type1, type2);
4114 do_warn_double_promotion (result_type, type1, type2,
4115 "implicit conversion from %qT to %qT to "
4116 "match other result of conditional",
4117 colon_loc);
4118
4119 /* If -Wsign-compare, warn here if type1 and type2 have
4120 different signedness. We'll promote the signed to unsigned
4121 and later code won't know it used to be different.
4122 Do this check on the original types, so that explicit casts
4123 will be considered, but default promotions won't. */
4124 if (c_inhibit_evaluation_warnings == 0)
4125 {
4126 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4127 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4128
4129 if (unsigned_op1 ^ unsigned_op2)
4130 {
4131 bool ovf;
4132
4133 /* Do not warn if the result type is signed, since the
4134 signed type will only be chosen if it can represent
4135 all the values of the unsigned type. */
4136 if (!TYPE_UNSIGNED (result_type))
4137 /* OK */;
4138 else
4139 {
4140 bool op1_maybe_const = true;
4141 bool op2_maybe_const = true;
4142
4143 /* Do not warn if the signed quantity is an
4144 unsuffixed integer literal (or some static
4145 constant expression involving such literals) and
4146 it is non-negative. This warning requires the
4147 operands to be folded for best results, so do
4148 that folding in this case even without
4149 warn_sign_compare to avoid warning options
4150 possibly affecting code generation. */
4151 c_inhibit_evaluation_warnings
4152 += (ifexp == truthvalue_false_node);
4153 op1 = c_fully_fold (op1, require_constant_value,
4154 &op1_maybe_const);
4155 c_inhibit_evaluation_warnings
4156 -= (ifexp == truthvalue_false_node);
4157
4158 c_inhibit_evaluation_warnings
4159 += (ifexp == truthvalue_true_node);
4160 op2 = c_fully_fold (op2, require_constant_value,
4161 &op2_maybe_const);
4162 c_inhibit_evaluation_warnings
4163 -= (ifexp == truthvalue_true_node);
4164
4165 if (warn_sign_compare)
4166 {
4167 if ((unsigned_op2
4168 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4169 || (unsigned_op1
4170 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4171 /* OK */;
4172 else
4173 warning_at (colon_loc, OPT_Wsign_compare,
4174 ("signed and unsigned type in "
4175 "conditional expression"));
4176 }
4177 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4178 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4179 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4180 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4181 }
4182 }
4183 }
4184 }
4185 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4186 {
4187 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4188 pedwarn (colon_loc, OPT_pedantic,
4189 "ISO C forbids conditional expr with only one void side");
4190 result_type = void_type_node;
4191 }
4192 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4193 {
4194 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4195 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4196 addr_space_t as_common;
4197
4198 if (comp_target_types (colon_loc, type1, type2))
4199 result_type = common_pointer_type (type1, type2);
4200 else if (null_pointer_constant_p (orig_op1))
4201 result_type = type2;
4202 else if (null_pointer_constant_p (orig_op2))
4203 result_type = type1;
4204 else if (!addr_space_superset (as1, as2, &as_common))
4205 {
4206 error_at (colon_loc, "pointers to disjoint address spaces "
4207 "used in conditional expression");
4208 return error_mark_node;
4209 }
4210 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4211 {
4212 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4213 pedwarn (colon_loc, OPT_pedantic,
4214 "ISO C forbids conditional expr between "
4215 "%<void *%> and function pointer");
4216 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4217 TREE_TYPE (type2)));
4218 }
4219 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4220 {
4221 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4222 pedwarn (colon_loc, OPT_pedantic,
4223 "ISO C forbids conditional expr between "
4224 "%<void *%> and function pointer");
4225 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4226 TREE_TYPE (type1)));
4227 }
4228 /* Objective-C pointer comparisons are a bit more lenient. */
4229 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4230 result_type = objc_common_type (type1, type2);
4231 else
4232 {
4233 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4234
4235 pedwarn (colon_loc, 0,
4236 "pointer type mismatch in conditional expression");
4237 result_type = build_pointer_type
4238 (build_qualified_type (void_type_node, qual));
4239 }
4240 }
4241 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4242 {
4243 if (!null_pointer_constant_p (orig_op2))
4244 pedwarn (colon_loc, 0,
4245 "pointer/integer type mismatch in conditional expression");
4246 else
4247 {
4248 op2 = null_pointer_node;
4249 }
4250 result_type = type1;
4251 }
4252 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4253 {
4254 if (!null_pointer_constant_p (orig_op1))
4255 pedwarn (colon_loc, 0,
4256 "pointer/integer type mismatch in conditional expression");
4257 else
4258 {
4259 op1 = null_pointer_node;
4260 }
4261 result_type = type2;
4262 }
4263
4264 if (!result_type)
4265 {
4266 if (flag_cond_mismatch)
4267 result_type = void_type_node;
4268 else
4269 {
4270 error_at (colon_loc, "type mismatch in conditional expression");
4271 return error_mark_node;
4272 }
4273 }
4274
4275 /* Merge const and volatile flags of the incoming types. */
4276 result_type
4277 = build_type_variant (result_type,
4278 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4279 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4280
4281 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4282 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4283
4284 if (ifexp_bcp && ifexp == truthvalue_true_node)
4285 {
4286 op2_int_operands = true;
4287 op1 = c_fully_fold (op1, require_constant_value, NULL);
4288 }
4289 if (ifexp_bcp && ifexp == truthvalue_false_node)
4290 {
4291 op1_int_operands = true;
4292 op2 = c_fully_fold (op2, require_constant_value, NULL);
4293 }
4294 int_const = int_operands = (ifexp_int_operands
4295 && op1_int_operands
4296 && op2_int_operands);
4297 if (int_operands)
4298 {
4299 int_const = ((ifexp == truthvalue_true_node
4300 && TREE_CODE (orig_op1) == INTEGER_CST
4301 && !TREE_OVERFLOW (orig_op1))
4302 || (ifexp == truthvalue_false_node
4303 && TREE_CODE (orig_op2) == INTEGER_CST
4304 && !TREE_OVERFLOW (orig_op2)));
4305 }
4306 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4307 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4308 else
4309 {
4310 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4311 if (int_operands)
4312 ret = note_integer_operands (ret);
4313 }
4314 if (semantic_result_type)
4315 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4316
4317 protected_set_expr_location (ret, colon_loc);
4318 return ret;
4319 }
4320 \f
4321 /* Return a compound expression that performs two expressions and
4322 returns the value of the second of them.
4323
4324 LOC is the location of the COMPOUND_EXPR. */
4325
4326 tree
4327 build_compound_expr (location_t loc, tree expr1, tree expr2)
4328 {
4329 bool expr1_int_operands, expr2_int_operands;
4330 tree eptype = NULL_TREE;
4331 tree ret;
4332
4333 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4334 if (expr1_int_operands)
4335 expr1 = remove_c_maybe_const_expr (expr1);
4336 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4337 if (expr2_int_operands)
4338 expr2 = remove_c_maybe_const_expr (expr2);
4339
4340 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4341 expr1 = TREE_OPERAND (expr1, 0);
4342 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4343 {
4344 eptype = TREE_TYPE (expr2);
4345 expr2 = TREE_OPERAND (expr2, 0);
4346 }
4347
4348 if (!TREE_SIDE_EFFECTS (expr1))
4349 {
4350 /* The left-hand operand of a comma expression is like an expression
4351 statement: with -Wunused, we should warn if it doesn't have
4352 any side-effects, unless it was explicitly cast to (void). */
4353 if (warn_unused_value)
4354 {
4355 if (VOID_TYPE_P (TREE_TYPE (expr1))
4356 && CONVERT_EXPR_P (expr1))
4357 ; /* (void) a, b */
4358 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4359 && TREE_CODE (expr1) == COMPOUND_EXPR
4360 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4361 ; /* (void) a, (void) b, c */
4362 else
4363 warning_at (loc, OPT_Wunused_value,
4364 "left-hand operand of comma expression has no effect");
4365 }
4366 }
4367
4368 /* With -Wunused, we should also warn if the left-hand operand does have
4369 side-effects, but computes a value which is not used. For example, in
4370 `foo() + bar(), baz()' the result of the `+' operator is not used,
4371 so we should issue a warning. */
4372 else if (warn_unused_value)
4373 warn_if_unused_value (expr1, loc);
4374
4375 if (expr2 == error_mark_node)
4376 return error_mark_node;
4377
4378 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4379
4380 if (flag_isoc99
4381 && expr1_int_operands
4382 && expr2_int_operands)
4383 ret = note_integer_operands (ret);
4384
4385 if (eptype)
4386 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4387
4388 protected_set_expr_location (ret, loc);
4389 return ret;
4390 }
4391
4392 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4393 which we are casting. OTYPE is the type of the expression being
4394 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4395 of the cast. -Wcast-qual appeared on the command line. Named
4396 address space qualifiers are not handled here, because they result
4397 in different warnings. */
4398
4399 static void
4400 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4401 {
4402 tree in_type = type;
4403 tree in_otype = otype;
4404 int added = 0;
4405 int discarded = 0;
4406 bool is_const;
4407
4408 /* Check that the qualifiers on IN_TYPE are a superset of the
4409 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4410 nodes is uninteresting and we stop as soon as we hit a
4411 non-POINTER_TYPE node on either type. */
4412 do
4413 {
4414 in_otype = TREE_TYPE (in_otype);
4415 in_type = TREE_TYPE (in_type);
4416
4417 /* GNU C allows cv-qualified function types. 'const' means the
4418 function is very pure, 'volatile' means it can't return. We
4419 need to warn when such qualifiers are added, not when they're
4420 taken away. */
4421 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4422 && TREE_CODE (in_type) == FUNCTION_TYPE)
4423 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4424 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4425 else
4426 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4427 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4428 }
4429 while (TREE_CODE (in_type) == POINTER_TYPE
4430 && TREE_CODE (in_otype) == POINTER_TYPE);
4431
4432 if (added)
4433 warning_at (loc, OPT_Wcast_qual,
4434 "cast adds %q#v qualifier to function type", added);
4435
4436 if (discarded)
4437 /* There are qualifiers present in IN_OTYPE that are not present
4438 in IN_TYPE. */
4439 warning_at (loc, OPT_Wcast_qual,
4440 "cast discards %q#v qualifier from pointer target type",
4441 discarded);
4442
4443 if (added || discarded)
4444 return;
4445
4446 /* A cast from **T to const **T is unsafe, because it can cause a
4447 const value to be changed with no additional warning. We only
4448 issue this warning if T is the same on both sides, and we only
4449 issue the warning if there are the same number of pointers on
4450 both sides, as otherwise the cast is clearly unsafe anyhow. A
4451 cast is unsafe when a qualifier is added at one level and const
4452 is not present at all outer levels.
4453
4454 To issue this warning, we check at each level whether the cast
4455 adds new qualifiers not already seen. We don't need to special
4456 case function types, as they won't have the same
4457 TYPE_MAIN_VARIANT. */
4458
4459 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4460 return;
4461 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4462 return;
4463
4464 in_type = type;
4465 in_otype = otype;
4466 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4467 do
4468 {
4469 in_type = TREE_TYPE (in_type);
4470 in_otype = TREE_TYPE (in_otype);
4471 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4472 && !is_const)
4473 {
4474 warning_at (loc, OPT_Wcast_qual,
4475 "to be safe all intermediate pointers in cast from "
4476 "%qT to %qT must be %<const%> qualified",
4477 otype, type);
4478 break;
4479 }
4480 if (is_const)
4481 is_const = TYPE_READONLY (in_type);
4482 }
4483 while (TREE_CODE (in_type) == POINTER_TYPE);
4484 }
4485
4486 /* Build an expression representing a cast to type TYPE of expression EXPR.
4487 LOC is the location of the cast-- typically the open paren of the cast. */
4488
4489 tree
4490 build_c_cast (location_t loc, tree type, tree expr)
4491 {
4492 tree value;
4493
4494 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4495 expr = TREE_OPERAND (expr, 0);
4496
4497 value = expr;
4498
4499 if (type == error_mark_node || expr == error_mark_node)
4500 return error_mark_node;
4501
4502 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4503 only in <protocol> qualifications. But when constructing cast expressions,
4504 the protocols do matter and must be kept around. */
4505 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4506 return build1 (NOP_EXPR, type, expr);
4507
4508 type = TYPE_MAIN_VARIANT (type);
4509
4510 if (TREE_CODE (type) == ARRAY_TYPE)
4511 {
4512 error_at (loc, "cast specifies array type");
4513 return error_mark_node;
4514 }
4515
4516 if (TREE_CODE (type) == FUNCTION_TYPE)
4517 {
4518 error_at (loc, "cast specifies function type");
4519 return error_mark_node;
4520 }
4521
4522 if (!VOID_TYPE_P (type))
4523 {
4524 value = require_complete_type (value);
4525 if (value == error_mark_node)
4526 return error_mark_node;
4527 }
4528
4529 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4530 {
4531 if (TREE_CODE (type) == RECORD_TYPE
4532 || TREE_CODE (type) == UNION_TYPE)
4533 pedwarn (loc, OPT_pedantic,
4534 "ISO C forbids casting nonscalar to the same type");
4535 }
4536 else if (TREE_CODE (type) == UNION_TYPE)
4537 {
4538 tree field;
4539
4540 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4541 if (TREE_TYPE (field) != error_mark_node
4542 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4543 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4544 break;
4545
4546 if (field)
4547 {
4548 tree t;
4549 bool maybe_const = true;
4550
4551 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4552 t = c_fully_fold (value, false, &maybe_const);
4553 t = build_constructor_single (type, field, t);
4554 if (!maybe_const)
4555 t = c_wrap_maybe_const (t, true);
4556 t = digest_init (loc, type, t,
4557 NULL_TREE, false, true, 0);
4558 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4559 return t;
4560 }
4561 error_at (loc, "cast to union type from type not present in union");
4562 return error_mark_node;
4563 }
4564 else
4565 {
4566 tree otype, ovalue;
4567
4568 if (type == void_type_node)
4569 {
4570 tree t = build1 (CONVERT_EXPR, type, value);
4571 SET_EXPR_LOCATION (t, loc);
4572 return t;
4573 }
4574
4575 otype = TREE_TYPE (value);
4576
4577 /* Optionally warn about potentially worrisome casts. */
4578 if (warn_cast_qual
4579 && TREE_CODE (type) == POINTER_TYPE
4580 && TREE_CODE (otype) == POINTER_TYPE)
4581 handle_warn_cast_qual (loc, type, otype);
4582
4583 /* Warn about conversions between pointers to disjoint
4584 address spaces. */
4585 if (TREE_CODE (type) == POINTER_TYPE
4586 && TREE_CODE (otype) == POINTER_TYPE
4587 && !null_pointer_constant_p (value))
4588 {
4589 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4590 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4591 addr_space_t as_common;
4592
4593 if (!addr_space_superset (as_to, as_from, &as_common))
4594 {
4595 if (ADDR_SPACE_GENERIC_P (as_from))
4596 warning_at (loc, 0, "cast to %s address space pointer "
4597 "from disjoint generic address space pointer",
4598 c_addr_space_name (as_to));
4599
4600 else if (ADDR_SPACE_GENERIC_P (as_to))
4601 warning_at (loc, 0, "cast to generic address space pointer "
4602 "from disjoint %s address space pointer",
4603 c_addr_space_name (as_from));
4604
4605 else
4606 warning_at (loc, 0, "cast to %s address space pointer "
4607 "from disjoint %s address space pointer",
4608 c_addr_space_name (as_to),
4609 c_addr_space_name (as_from));
4610 }
4611 }
4612
4613 /* Warn about possible alignment problems. */
4614 if (STRICT_ALIGNMENT
4615 && TREE_CODE (type) == POINTER_TYPE
4616 && TREE_CODE (otype) == POINTER_TYPE
4617 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4618 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4619 /* Don't warn about opaque types, where the actual alignment
4620 restriction is unknown. */
4621 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4622 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4623 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4624 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4625 warning_at (loc, OPT_Wcast_align,
4626 "cast increases required alignment of target type");
4627
4628 if (TREE_CODE (type) == INTEGER_TYPE
4629 && TREE_CODE (otype) == POINTER_TYPE
4630 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4631 /* Unlike conversion of integers to pointers, where the
4632 warning is disabled for converting constants because
4633 of cases such as SIG_*, warn about converting constant
4634 pointers to integers. In some cases it may cause unwanted
4635 sign extension, and a warning is appropriate. */
4636 warning_at (loc, OPT_Wpointer_to_int_cast,
4637 "cast from pointer to integer of different size");
4638
4639 if (TREE_CODE (value) == CALL_EXPR
4640 && TREE_CODE (type) != TREE_CODE (otype))
4641 warning_at (loc, OPT_Wbad_function_cast,
4642 "cast from function call of type %qT "
4643 "to non-matching type %qT", otype, type);
4644
4645 if (TREE_CODE (type) == POINTER_TYPE
4646 && TREE_CODE (otype) == INTEGER_TYPE
4647 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4648 /* Don't warn about converting any constant. */
4649 && !TREE_CONSTANT (value))
4650 warning_at (loc,
4651 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4652 "of different size");
4653
4654 if (warn_strict_aliasing <= 2)
4655 strict_aliasing_warning (otype, type, expr);
4656
4657 /* If pedantic, warn for conversions between function and object
4658 pointer types, except for converting a null pointer constant
4659 to function pointer type. */
4660 if (pedantic
4661 && TREE_CODE (type) == POINTER_TYPE
4662 && TREE_CODE (otype) == POINTER_TYPE
4663 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4664 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4665 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4666 "conversion of function pointer to object pointer type");
4667
4668 if (pedantic
4669 && TREE_CODE (type) == POINTER_TYPE
4670 && TREE_CODE (otype) == POINTER_TYPE
4671 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4672 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4673 && !null_pointer_constant_p (value))
4674 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4675 "conversion of object pointer to function pointer type");
4676
4677 ovalue = value;
4678 value = convert (type, value);
4679
4680 /* Ignore any integer overflow caused by the cast. */
4681 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4682 {
4683 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4684 {
4685 if (!TREE_OVERFLOW (value))
4686 {
4687 /* Avoid clobbering a shared constant. */
4688 value = copy_node (value);
4689 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4690 }
4691 }
4692 else if (TREE_OVERFLOW (value))
4693 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4694 value = build_int_cst_wide (TREE_TYPE (value),
4695 TREE_INT_CST_LOW (value),
4696 TREE_INT_CST_HIGH (value));
4697 }
4698 }
4699
4700 /* Don't let a cast be an lvalue. */
4701 if (value == expr)
4702 value = non_lvalue_loc (loc, value);
4703
4704 /* Don't allow the results of casting to floating-point or complex
4705 types be confused with actual constants, or casts involving
4706 integer and pointer types other than direct integer-to-integer
4707 and integer-to-pointer be confused with integer constant
4708 expressions and null pointer constants. */
4709 if (TREE_CODE (value) == REAL_CST
4710 || TREE_CODE (value) == COMPLEX_CST
4711 || (TREE_CODE (value) == INTEGER_CST
4712 && !((TREE_CODE (expr) == INTEGER_CST
4713 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4714 || TREE_CODE (expr) == REAL_CST
4715 || TREE_CODE (expr) == COMPLEX_CST)))
4716 value = build1 (NOP_EXPR, type, value);
4717
4718 if (CAN_HAVE_LOCATION_P (value))
4719 SET_EXPR_LOCATION (value, loc);
4720 return value;
4721 }
4722
4723 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4724 location of the open paren of the cast, or the position of the cast
4725 expr. */
4726 tree
4727 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4728 {
4729 tree type;
4730 tree type_expr = NULL_TREE;
4731 bool type_expr_const = true;
4732 tree ret;
4733 int saved_wsp = warn_strict_prototypes;
4734
4735 /* This avoids warnings about unprototyped casts on
4736 integers. E.g. "#define SIG_DFL (void(*)())0". */
4737 if (TREE_CODE (expr) == INTEGER_CST)
4738 warn_strict_prototypes = 0;
4739 type = groktypename (type_name, &type_expr, &type_expr_const);
4740 warn_strict_prototypes = saved_wsp;
4741
4742 ret = build_c_cast (loc, type, expr);
4743 if (type_expr)
4744 {
4745 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4746 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4747 SET_EXPR_LOCATION (ret, loc);
4748 }
4749
4750 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4751 SET_EXPR_LOCATION (ret, loc);
4752
4753 /* C++ does not permits types to be defined in a cast, but it
4754 allows references to incomplete types. */
4755 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4756 warning_at (loc, OPT_Wc___compat,
4757 "defining a type in a cast is invalid in C++");
4758
4759 return ret;
4760 }
4761 \f
4762 /* Build an assignment expression of lvalue LHS from value RHS.
4763 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4764 may differ from TREE_TYPE (LHS) for an enum bitfield.
4765 MODIFYCODE is the code for a binary operator that we use
4766 to combine the old value of LHS with RHS to get the new value.
4767 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4768 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4769 which may differ from TREE_TYPE (RHS) for an enum value.
4770
4771 LOCATION is the location of the MODIFYCODE operator.
4772 RHS_LOC is the location of the RHS. */
4773
4774 tree
4775 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4776 enum tree_code modifycode,
4777 location_t rhs_loc, tree rhs, tree rhs_origtype)
4778 {
4779 tree result;
4780 tree newrhs;
4781 tree rhs_semantic_type = NULL_TREE;
4782 tree lhstype = TREE_TYPE (lhs);
4783 tree olhstype = lhstype;
4784 bool npc;
4785
4786 /* Types that aren't fully specified cannot be used in assignments. */
4787 lhs = require_complete_type (lhs);
4788
4789 /* Avoid duplicate error messages from operands that had errors. */
4790 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4791 return error_mark_node;
4792
4793 /* For ObjC properties, defer this check. */
4794 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4795 return error_mark_node;
4796
4797 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4798 {
4799 rhs_semantic_type = TREE_TYPE (rhs);
4800 rhs = TREE_OPERAND (rhs, 0);
4801 }
4802
4803 newrhs = rhs;
4804
4805 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4806 {
4807 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4808 lhs_origtype, modifycode, rhs_loc, rhs,
4809 rhs_origtype);
4810 if (inner == error_mark_node)
4811 return error_mark_node;
4812 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4813 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4814 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4815 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4816 protected_set_expr_location (result, location);
4817 return result;
4818 }
4819
4820 /* If a binary op has been requested, combine the old LHS value with the RHS
4821 producing the value we should actually store into the LHS. */
4822
4823 if (modifycode != NOP_EXPR)
4824 {
4825 lhs = c_fully_fold (lhs, false, NULL);
4826 lhs = stabilize_reference (lhs);
4827 newrhs = build_binary_op (location,
4828 modifycode, lhs, rhs, 1);
4829
4830 /* The original type of the right hand side is no longer
4831 meaningful. */
4832 rhs_origtype = NULL_TREE;
4833 }
4834
4835 if (c_dialect_objc ())
4836 {
4837 /* Check if we are modifying an Objective-C property reference;
4838 if so, we need to generate setter calls. */
4839 result = objc_maybe_build_modify_expr (lhs, newrhs);
4840 if (result)
4841 return result;
4842
4843 /* Else, do the check that we postponed for Objective-C. */
4844 if (!lvalue_or_else (location, lhs, lv_assign))
4845 return error_mark_node;
4846 }
4847
4848 /* Give an error for storing in something that is 'const'. */
4849
4850 if (TYPE_READONLY (lhstype)
4851 || ((TREE_CODE (lhstype) == RECORD_TYPE
4852 || TREE_CODE (lhstype) == UNION_TYPE)
4853 && C_TYPE_FIELDS_READONLY (lhstype)))
4854 {
4855 readonly_error (lhs, lv_assign);
4856 return error_mark_node;
4857 }
4858 else if (TREE_READONLY (lhs))
4859 readonly_warning (lhs, lv_assign);
4860
4861 /* If storing into a structure or union member,
4862 it has probably been given type `int'.
4863 Compute the type that would go with
4864 the actual amount of storage the member occupies. */
4865
4866 if (TREE_CODE (lhs) == COMPONENT_REF
4867 && (TREE_CODE (lhstype) == INTEGER_TYPE
4868 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4869 || TREE_CODE (lhstype) == REAL_TYPE
4870 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4871 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4872
4873 /* If storing in a field that is in actuality a short or narrower than one,
4874 we must store in the field in its actual type. */
4875
4876 if (lhstype != TREE_TYPE (lhs))
4877 {
4878 lhs = copy_node (lhs);
4879 TREE_TYPE (lhs) = lhstype;
4880 }
4881
4882 /* Issue -Wc++-compat warnings about an assignment to an enum type
4883 when LHS does not have its original type. This happens for,
4884 e.g., an enum bitfield in a struct. */
4885 if (warn_cxx_compat
4886 && lhs_origtype != NULL_TREE
4887 && lhs_origtype != lhstype
4888 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4889 {
4890 tree checktype = (rhs_origtype != NULL_TREE
4891 ? rhs_origtype
4892 : TREE_TYPE (rhs));
4893 if (checktype != error_mark_node
4894 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4895 warning_at (location, OPT_Wc___compat,
4896 "enum conversion in assignment is invalid in C++");
4897 }
4898
4899 /* Convert new value to destination type. Fold it first, then
4900 restore any excess precision information, for the sake of
4901 conversion warnings. */
4902
4903 npc = null_pointer_constant_p (newrhs);
4904 newrhs = c_fully_fold (newrhs, false, NULL);
4905 if (rhs_semantic_type)
4906 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4907 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4908 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4909 if (TREE_CODE (newrhs) == ERROR_MARK)
4910 return error_mark_node;
4911
4912 /* Emit ObjC write barrier, if necessary. */
4913 if (c_dialect_objc () && flag_objc_gc)
4914 {
4915 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4916 if (result)
4917 {
4918 protected_set_expr_location (result, location);
4919 return result;
4920 }
4921 }
4922
4923 /* Scan operands. */
4924
4925 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4926 TREE_SIDE_EFFECTS (result) = 1;
4927 protected_set_expr_location (result, location);
4928
4929 /* If we got the LHS in a different type for storing in,
4930 convert the result back to the nominal type of LHS
4931 so that the value we return always has the same type
4932 as the LHS argument. */
4933
4934 if (olhstype == TREE_TYPE (result))
4935 return result;
4936
4937 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4938 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4939 protected_set_expr_location (result, location);
4940 return result;
4941 }
4942 \f
4943 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
4944 This is used to implement -fplan9-extensions. */
4945
4946 static bool
4947 find_anonymous_field_with_type (tree struct_type, tree type)
4948 {
4949 tree field;
4950 bool found;
4951
4952 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
4953 || TREE_CODE (struct_type) == UNION_TYPE);
4954 found = false;
4955 for (field = TYPE_FIELDS (struct_type);
4956 field != NULL_TREE;
4957 field = TREE_CHAIN (field))
4958 {
4959 if (DECL_NAME (field) == NULL
4960 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
4961 {
4962 if (found)
4963 return false;
4964 found = true;
4965 }
4966 else if (DECL_NAME (field) == NULL
4967 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
4968 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
4969 && find_anonymous_field_with_type (TREE_TYPE (field), type))
4970 {
4971 if (found)
4972 return false;
4973 found = true;
4974 }
4975 }
4976 return found;
4977 }
4978
4979 /* RHS is an expression whose type is pointer to struct. If there is
4980 an anonymous field in RHS with type TYPE, then return a pointer to
4981 that field in RHS. This is used with -fplan9-extensions. This
4982 returns NULL if no conversion could be found. */
4983
4984 static tree
4985 convert_to_anonymous_field (location_t location, tree type, tree rhs)
4986 {
4987 tree rhs_struct_type, lhs_main_type;
4988 tree field, found_field;
4989 bool found_sub_field;
4990 tree ret;
4991
4992 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
4993 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
4994 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
4995 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
4996
4997 gcc_assert (POINTER_TYPE_P (type));
4998 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4999
5000 found_field = NULL_TREE;
5001 found_sub_field = false;
5002 for (field = TYPE_FIELDS (rhs_struct_type);
5003 field != NULL_TREE;
5004 field = TREE_CHAIN (field))
5005 {
5006 if (DECL_NAME (field) != NULL_TREE
5007 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5008 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5009 continue;
5010 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5011 {
5012 if (found_field != NULL_TREE)
5013 return NULL_TREE;
5014 found_field = field;
5015 }
5016 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5017 lhs_main_type))
5018 {
5019 if (found_field != NULL_TREE)
5020 return NULL_TREE;
5021 found_field = field;
5022 found_sub_field = true;
5023 }
5024 }
5025
5026 if (found_field == NULL_TREE)
5027 return NULL_TREE;
5028
5029 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5030 build_fold_indirect_ref (rhs), found_field,
5031 NULL_TREE);
5032 ret = build_fold_addr_expr_loc (location, ret);
5033
5034 if (found_sub_field)
5035 {
5036 ret = convert_to_anonymous_field (location, type, ret);
5037 gcc_assert (ret != NULL_TREE);
5038 }
5039
5040 return ret;
5041 }
5042
5043 /* Convert value RHS to type TYPE as preparation for an assignment to
5044 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5045 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5046 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5047 constant before any folding.
5048 The real work of conversion is done by `convert'.
5049 The purpose of this function is to generate error messages
5050 for assignments that are not allowed in C.
5051 ERRTYPE says whether it is argument passing, assignment,
5052 initialization or return.
5053
5054 LOCATION is the location of the RHS.
5055 FUNCTION is a tree for the function being called.
5056 PARMNUM is the number of the argument, for printing in error messages. */
5057
5058 static tree
5059 convert_for_assignment (location_t location, tree type, tree rhs,
5060 tree origtype, enum impl_conv errtype,
5061 bool null_pointer_constant, tree fundecl,
5062 tree function, int parmnum)
5063 {
5064 enum tree_code codel = TREE_CODE (type);
5065 tree orig_rhs = rhs;
5066 tree rhstype;
5067 enum tree_code coder;
5068 tree rname = NULL_TREE;
5069 bool objc_ok = false;
5070
5071 if (errtype == ic_argpass)
5072 {
5073 tree selector;
5074 /* Change pointer to function to the function itself for
5075 diagnostics. */
5076 if (TREE_CODE (function) == ADDR_EXPR
5077 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5078 function = TREE_OPERAND (function, 0);
5079
5080 /* Handle an ObjC selector specially for diagnostics. */
5081 selector = objc_message_selector ();
5082 rname = function;
5083 if (selector && parmnum > 2)
5084 {
5085 rname = selector;
5086 parmnum -= 2;
5087 }
5088 }
5089
5090 /* This macro is used to emit diagnostics to ensure that all format
5091 strings are complete sentences, visible to gettext and checked at
5092 compile time. */
5093 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5094 do { \
5095 switch (errtype) \
5096 { \
5097 case ic_argpass: \
5098 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5099 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5100 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5101 "expected %qT but argument is of type %qT", \
5102 type, rhstype); \
5103 break; \
5104 case ic_assign: \
5105 pedwarn (LOCATION, OPT, AS); \
5106 break; \
5107 case ic_init: \
5108 pedwarn_init (LOCATION, OPT, IN); \
5109 break; \
5110 case ic_return: \
5111 pedwarn (LOCATION, OPT, RE); \
5112 break; \
5113 default: \
5114 gcc_unreachable (); \
5115 } \
5116 } while (0)
5117
5118 /* This macro is used to emit diagnostics to ensure that all format
5119 strings are complete sentences, visible to gettext and checked at
5120 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5121 extra parameter to enumerate qualifiers. */
5122
5123 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5124 do { \
5125 switch (errtype) \
5126 { \
5127 case ic_argpass: \
5128 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5129 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5130 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5131 "expected %qT but argument is of type %qT", \
5132 type, rhstype); \
5133 break; \
5134 case ic_assign: \
5135 pedwarn (LOCATION, OPT, AS, QUALS); \
5136 break; \
5137 case ic_init: \
5138 pedwarn (LOCATION, OPT, IN, QUALS); \
5139 break; \
5140 case ic_return: \
5141 pedwarn (LOCATION, OPT, RE, QUALS); \
5142 break; \
5143 default: \
5144 gcc_unreachable (); \
5145 } \
5146 } while (0)
5147
5148 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5149 rhs = TREE_OPERAND (rhs, 0);
5150
5151 rhstype = TREE_TYPE (rhs);
5152 coder = TREE_CODE (rhstype);
5153
5154 if (coder == ERROR_MARK)
5155 return error_mark_node;
5156
5157 if (c_dialect_objc ())
5158 {
5159 int parmno;
5160
5161 switch (errtype)
5162 {
5163 case ic_return:
5164 parmno = 0;
5165 break;
5166
5167 case ic_assign:
5168 parmno = -1;
5169 break;
5170
5171 case ic_init:
5172 parmno = -2;
5173 break;
5174
5175 default:
5176 parmno = parmnum;
5177 break;
5178 }
5179
5180 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5181 }
5182
5183 if (warn_cxx_compat)
5184 {
5185 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5186 if (checktype != error_mark_node
5187 && TREE_CODE (type) == ENUMERAL_TYPE
5188 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5189 {
5190 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5191 G_("enum conversion when passing argument "
5192 "%d of %qE is invalid in C++"),
5193 G_("enum conversion in assignment is "
5194 "invalid in C++"),
5195 G_("enum conversion in initialization is "
5196 "invalid in C++"),
5197 G_("enum conversion in return is "
5198 "invalid in C++"));
5199 }
5200 }
5201
5202 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5203 return rhs;
5204
5205 if (coder == VOID_TYPE)
5206 {
5207 /* Except for passing an argument to an unprototyped function,
5208 this is a constraint violation. When passing an argument to
5209 an unprototyped function, it is compile-time undefined;
5210 making it a constraint in that case was rejected in
5211 DR#252. */
5212 error_at (location, "void value not ignored as it ought to be");
5213 return error_mark_node;
5214 }
5215 rhs = require_complete_type (rhs);
5216 if (rhs == error_mark_node)
5217 return error_mark_node;
5218 /* A type converts to a reference to it.
5219 This code doesn't fully support references, it's just for the
5220 special case of va_start and va_copy. */
5221 if (codel == REFERENCE_TYPE
5222 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5223 {
5224 if (!lvalue_p (rhs))
5225 {
5226 error_at (location, "cannot pass rvalue to reference parameter");
5227 return error_mark_node;
5228 }
5229 if (!c_mark_addressable (rhs))
5230 return error_mark_node;
5231 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5232 SET_EXPR_LOCATION (rhs, location);
5233
5234 /* We already know that these two types are compatible, but they
5235 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5236 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5237 likely to be va_list, a typedef to __builtin_va_list, which
5238 is different enough that it will cause problems later. */
5239 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5240 {
5241 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5242 SET_EXPR_LOCATION (rhs, location);
5243 }
5244
5245 rhs = build1 (NOP_EXPR, type, rhs);
5246 SET_EXPR_LOCATION (rhs, location);
5247 return rhs;
5248 }
5249 /* Some types can interconvert without explicit casts. */
5250 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5251 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5252 return convert (type, rhs);
5253 /* Arithmetic types all interconvert, and enum is treated like int. */
5254 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5255 || codel == FIXED_POINT_TYPE
5256 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5257 || codel == BOOLEAN_TYPE)
5258 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5259 || coder == FIXED_POINT_TYPE
5260 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5261 || coder == BOOLEAN_TYPE))
5262 {
5263 tree ret;
5264 bool save = in_late_binary_op;
5265 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5266 in_late_binary_op = true;
5267 ret = convert_and_check (type, orig_rhs);
5268 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5269 in_late_binary_op = save;
5270 return ret;
5271 }
5272
5273 /* Aggregates in different TUs might need conversion. */
5274 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5275 && codel == coder
5276 && comptypes (type, rhstype))
5277 return convert_and_check (type, rhs);
5278
5279 /* Conversion to a transparent union or record from its member types.
5280 This applies only to function arguments. */
5281 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5282 && TYPE_TRANSPARENT_AGGR (type))
5283 && errtype == ic_argpass)
5284 {
5285 tree memb, marginal_memb = NULL_TREE;
5286
5287 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5288 {
5289 tree memb_type = TREE_TYPE (memb);
5290
5291 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5292 TYPE_MAIN_VARIANT (rhstype)))
5293 break;
5294
5295 if (TREE_CODE (memb_type) != POINTER_TYPE)
5296 continue;
5297
5298 if (coder == POINTER_TYPE)
5299 {
5300 tree ttl = TREE_TYPE (memb_type);
5301 tree ttr = TREE_TYPE (rhstype);
5302
5303 /* Any non-function converts to a [const][volatile] void *
5304 and vice versa; otherwise, targets must be the same.
5305 Meanwhile, the lhs target must have all the qualifiers of
5306 the rhs. */
5307 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5308 || comp_target_types (location, memb_type, rhstype))
5309 {
5310 /* If this type won't generate any warnings, use it. */
5311 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5312 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5313 && TREE_CODE (ttl) == FUNCTION_TYPE)
5314 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5315 == TYPE_QUALS (ttr))
5316 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5317 == TYPE_QUALS (ttl))))
5318 break;
5319
5320 /* Keep looking for a better type, but remember this one. */
5321 if (!marginal_memb)
5322 marginal_memb = memb;
5323 }
5324 }
5325
5326 /* Can convert integer zero to any pointer type. */
5327 if (null_pointer_constant)
5328 {
5329 rhs = null_pointer_node;
5330 break;
5331 }
5332 }
5333
5334 if (memb || marginal_memb)
5335 {
5336 if (!memb)
5337 {
5338 /* We have only a marginally acceptable member type;
5339 it needs a warning. */
5340 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5341 tree ttr = TREE_TYPE (rhstype);
5342
5343 /* Const and volatile mean something different for function
5344 types, so the usual warnings are not appropriate. */
5345 if (TREE_CODE (ttr) == FUNCTION_TYPE
5346 && TREE_CODE (ttl) == FUNCTION_TYPE)
5347 {
5348 /* Because const and volatile on functions are
5349 restrictions that say the function will not do
5350 certain things, it is okay to use a const or volatile
5351 function where an ordinary one is wanted, but not
5352 vice-versa. */
5353 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5354 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5355 WARN_FOR_QUALIFIERS (location, 0,
5356 G_("passing argument %d of %qE "
5357 "makes %q#v qualified function "
5358 "pointer from unqualified"),
5359 G_("assignment makes %q#v qualified "
5360 "function pointer from "
5361 "unqualified"),
5362 G_("initialization makes %q#v qualified "
5363 "function pointer from "
5364 "unqualified"),
5365 G_("return makes %q#v qualified function "
5366 "pointer from unqualified"),
5367 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5368 }
5369 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5370 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5371 WARN_FOR_QUALIFIERS (location, 0,
5372 G_("passing argument %d of %qE discards "
5373 "%qv qualifier from pointer target type"),
5374 G_("assignment discards %qv qualifier "
5375 "from pointer target type"),
5376 G_("initialization discards %qv qualifier "
5377 "from pointer target type"),
5378 G_("return discards %qv qualifier from "
5379 "pointer target type"),
5380 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5381
5382 memb = marginal_memb;
5383 }
5384
5385 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5386 pedwarn (location, OPT_pedantic,
5387 "ISO C prohibits argument conversion to union type");
5388
5389 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5390 return build_constructor_single (type, memb, rhs);
5391 }
5392 }
5393
5394 /* Conversions among pointers */
5395 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5396 && (coder == codel))
5397 {
5398 tree ttl = TREE_TYPE (type);
5399 tree ttr = TREE_TYPE (rhstype);
5400 tree mvl = ttl;
5401 tree mvr = ttr;
5402 bool is_opaque_pointer;
5403 int target_cmp = 0; /* Cache comp_target_types () result. */
5404 addr_space_t asl;
5405 addr_space_t asr;
5406
5407 if (TREE_CODE (mvl) != ARRAY_TYPE)
5408 mvl = TYPE_MAIN_VARIANT (mvl);
5409 if (TREE_CODE (mvr) != ARRAY_TYPE)
5410 mvr = TYPE_MAIN_VARIANT (mvr);
5411 /* Opaque pointers are treated like void pointers. */
5412 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5413
5414 /* The Plan 9 compiler permits a pointer to a struct to be
5415 automatically converted into a pointer to an anonymous field
5416 within the struct. */
5417 if (flag_plan9_extensions
5418 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5419 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5420 && mvl != mvr)
5421 {
5422 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5423 if (new_rhs != NULL_TREE)
5424 {
5425 rhs = new_rhs;
5426 rhstype = TREE_TYPE (rhs);
5427 coder = TREE_CODE (rhstype);
5428 ttr = TREE_TYPE (rhstype);
5429 mvr = TYPE_MAIN_VARIANT (ttr);
5430 }
5431 }
5432
5433 /* C++ does not allow the implicit conversion void* -> T*. However,
5434 for the purpose of reducing the number of false positives, we
5435 tolerate the special case of
5436
5437 int *p = NULL;
5438
5439 where NULL is typically defined in C to be '(void *) 0'. */
5440 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5441 warning_at (location, OPT_Wc___compat,
5442 "request for implicit conversion "
5443 "from %qT to %qT not permitted in C++", rhstype, type);
5444
5445 /* See if the pointers point to incompatible address spaces. */
5446 asl = TYPE_ADDR_SPACE (ttl);
5447 asr = TYPE_ADDR_SPACE (ttr);
5448 if (!null_pointer_constant_p (rhs)
5449 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5450 {
5451 switch (errtype)
5452 {
5453 case ic_argpass:
5454 error_at (location, "passing argument %d of %qE from pointer to "
5455 "non-enclosed address space", parmnum, rname);
5456 break;
5457 case ic_assign:
5458 error_at (location, "assignment from pointer to "
5459 "non-enclosed address space");
5460 break;
5461 case ic_init:
5462 error_at (location, "initialization from pointer to "
5463 "non-enclosed address space");
5464 break;
5465 case ic_return:
5466 error_at (location, "return from pointer to "
5467 "non-enclosed address space");
5468 break;
5469 default:
5470 gcc_unreachable ();
5471 }
5472 return error_mark_node;
5473 }
5474
5475 /* Check if the right-hand side has a format attribute but the
5476 left-hand side doesn't. */
5477 if (warn_missing_format_attribute
5478 && check_missing_format_attribute (type, rhstype))
5479 {
5480 switch (errtype)
5481 {
5482 case ic_argpass:
5483 warning_at (location, OPT_Wmissing_format_attribute,
5484 "argument %d of %qE might be "
5485 "a candidate for a format attribute",
5486 parmnum, rname);
5487 break;
5488 case ic_assign:
5489 warning_at (location, OPT_Wmissing_format_attribute,
5490 "assignment left-hand side might be "
5491 "a candidate for a format attribute");
5492 break;
5493 case ic_init:
5494 warning_at (location, OPT_Wmissing_format_attribute,
5495 "initialization left-hand side might be "
5496 "a candidate for a format attribute");
5497 break;
5498 case ic_return:
5499 warning_at (location, OPT_Wmissing_format_attribute,
5500 "return type might be "
5501 "a candidate for a format attribute");
5502 break;
5503 default:
5504 gcc_unreachable ();
5505 }
5506 }
5507
5508 /* Any non-function converts to a [const][volatile] void *
5509 and vice versa; otherwise, targets must be the same.
5510 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5511 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5512 || (target_cmp = comp_target_types (location, type, rhstype))
5513 || is_opaque_pointer
5514 || (c_common_unsigned_type (mvl)
5515 == c_common_unsigned_type (mvr)))
5516 {
5517 if (pedantic
5518 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5519 ||
5520 (VOID_TYPE_P (ttr)
5521 && !null_pointer_constant
5522 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5523 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5524 G_("ISO C forbids passing argument %d of "
5525 "%qE between function pointer "
5526 "and %<void *%>"),
5527 G_("ISO C forbids assignment between "
5528 "function pointer and %<void *%>"),
5529 G_("ISO C forbids initialization between "
5530 "function pointer and %<void *%>"),
5531 G_("ISO C forbids return between function "
5532 "pointer and %<void *%>"));
5533 /* Const and volatile mean something different for function types,
5534 so the usual warnings are not appropriate. */
5535 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5536 && TREE_CODE (ttl) != FUNCTION_TYPE)
5537 {
5538 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5539 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5540 {
5541 WARN_FOR_QUALIFIERS (location, 0,
5542 G_("passing argument %d of %qE discards "
5543 "%qv qualifier from pointer target type"),
5544 G_("assignment discards %qv qualifier "
5545 "from pointer target type"),
5546 G_("initialization discards %qv qualifier "
5547 "from pointer target type"),
5548 G_("return discards %qv qualifier from "
5549 "pointer target type"),
5550 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5551 }
5552 /* If this is not a case of ignoring a mismatch in signedness,
5553 no warning. */
5554 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5555 || target_cmp)
5556 ;
5557 /* If there is a mismatch, do warn. */
5558 else if (warn_pointer_sign)
5559 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5560 G_("pointer targets in passing argument "
5561 "%d of %qE differ in signedness"),
5562 G_("pointer targets in assignment "
5563 "differ in signedness"),
5564 G_("pointer targets in initialization "
5565 "differ in signedness"),
5566 G_("pointer targets in return differ "
5567 "in signedness"));
5568 }
5569 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5570 && TREE_CODE (ttr) == FUNCTION_TYPE)
5571 {
5572 /* Because const and volatile on functions are restrictions
5573 that say the function will not do certain things,
5574 it is okay to use a const or volatile function
5575 where an ordinary one is wanted, but not vice-versa. */
5576 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5577 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5578 WARN_FOR_QUALIFIERS (location, 0,
5579 G_("passing argument %d of %qE makes "
5580 "%q#v qualified function pointer "
5581 "from unqualified"),
5582 G_("assignment makes %q#v qualified function "
5583 "pointer from unqualified"),
5584 G_("initialization makes %q#v qualified "
5585 "function pointer from unqualified"),
5586 G_("return makes %q#v qualified function "
5587 "pointer from unqualified"),
5588 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5589 }
5590 }
5591 else
5592 /* Avoid warning about the volatile ObjC EH puts on decls. */
5593 if (!objc_ok)
5594 WARN_FOR_ASSIGNMENT (location, 0,
5595 G_("passing argument %d of %qE from "
5596 "incompatible pointer type"),
5597 G_("assignment from incompatible pointer type"),
5598 G_("initialization from incompatible "
5599 "pointer type"),
5600 G_("return from incompatible pointer type"));
5601
5602 return convert (type, rhs);
5603 }
5604 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5605 {
5606 /* ??? This should not be an error when inlining calls to
5607 unprototyped functions. */
5608 error_at (location, "invalid use of non-lvalue array");
5609 return error_mark_node;
5610 }
5611 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5612 {
5613 /* An explicit constant 0 can convert to a pointer,
5614 or one that results from arithmetic, even including
5615 a cast to integer type. */
5616 if (!null_pointer_constant)
5617 WARN_FOR_ASSIGNMENT (location, 0,
5618 G_("passing argument %d of %qE makes "
5619 "pointer from integer without a cast"),
5620 G_("assignment makes pointer from integer "
5621 "without a cast"),
5622 G_("initialization makes pointer from "
5623 "integer without a cast"),
5624 G_("return makes pointer from integer "
5625 "without a cast"));
5626
5627 return convert (type, rhs);
5628 }
5629 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5630 {
5631 WARN_FOR_ASSIGNMENT (location, 0,
5632 G_("passing argument %d of %qE makes integer "
5633 "from pointer without a cast"),
5634 G_("assignment makes integer from pointer "
5635 "without a cast"),
5636 G_("initialization makes integer from pointer "
5637 "without a cast"),
5638 G_("return makes integer from pointer "
5639 "without a cast"));
5640 return convert (type, rhs);
5641 }
5642 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5643 {
5644 tree ret;
5645 bool save = in_late_binary_op;
5646 in_late_binary_op = true;
5647 ret = convert (type, rhs);
5648 in_late_binary_op = save;
5649 return ret;
5650 }
5651
5652 switch (errtype)
5653 {
5654 case ic_argpass:
5655 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5656 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5657 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5658 "expected %qT but argument is of type %qT", type, rhstype);
5659 break;
5660 case ic_assign:
5661 error_at (location, "incompatible types when assigning to type %qT from "
5662 "type %qT", type, rhstype);
5663 break;
5664 case ic_init:
5665 error_at (location,
5666 "incompatible types when initializing type %qT using type %qT",
5667 type, rhstype);
5668 break;
5669 case ic_return:
5670 error_at (location,
5671 "incompatible types when returning type %qT but %qT was "
5672 "expected", rhstype, type);
5673 break;
5674 default:
5675 gcc_unreachable ();
5676 }
5677
5678 return error_mark_node;
5679 }
5680 \f
5681 /* If VALUE is a compound expr all of whose expressions are constant, then
5682 return its value. Otherwise, return error_mark_node.
5683
5684 This is for handling COMPOUND_EXPRs as initializer elements
5685 which is allowed with a warning when -pedantic is specified. */
5686
5687 static tree
5688 valid_compound_expr_initializer (tree value, tree endtype)
5689 {
5690 if (TREE_CODE (value) == COMPOUND_EXPR)
5691 {
5692 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5693 == error_mark_node)
5694 return error_mark_node;
5695 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5696 endtype);
5697 }
5698 else if (!initializer_constant_valid_p (value, endtype))
5699 return error_mark_node;
5700 else
5701 return value;
5702 }
5703 \f
5704 /* Perform appropriate conversions on the initial value of a variable,
5705 store it in the declaration DECL,
5706 and print any error messages that are appropriate.
5707 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5708 If the init is invalid, store an ERROR_MARK.
5709
5710 INIT_LOC is the location of the initial value. */
5711
5712 void
5713 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5714 {
5715 tree value, type;
5716 bool npc = false;
5717
5718 /* If variable's type was invalidly declared, just ignore it. */
5719
5720 type = TREE_TYPE (decl);
5721 if (TREE_CODE (type) == ERROR_MARK)
5722 return;
5723
5724 /* Digest the specified initializer into an expression. */
5725
5726 if (init)
5727 npc = null_pointer_constant_p (init);
5728 value = digest_init (init_loc, type, init, origtype, npc,
5729 true, TREE_STATIC (decl));
5730
5731 /* Store the expression if valid; else report error. */
5732
5733 if (!in_system_header
5734 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5735 warning (OPT_Wtraditional, "traditional C rejects automatic "
5736 "aggregate initialization");
5737
5738 DECL_INITIAL (decl) = value;
5739
5740 /* ANSI wants warnings about out-of-range constant initializers. */
5741 STRIP_TYPE_NOPS (value);
5742 if (TREE_STATIC (decl))
5743 constant_expression_warning (value);
5744
5745 /* Check if we need to set array size from compound literal size. */
5746 if (TREE_CODE (type) == ARRAY_TYPE
5747 && TYPE_DOMAIN (type) == 0
5748 && value != error_mark_node)
5749 {
5750 tree inside_init = init;
5751
5752 STRIP_TYPE_NOPS (inside_init);
5753 inside_init = fold (inside_init);
5754
5755 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5756 {
5757 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5758
5759 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5760 {
5761 /* For int foo[] = (int [3]){1}; we need to set array size
5762 now since later on array initializer will be just the
5763 brace enclosed list of the compound literal. */
5764 tree etype = strip_array_types (TREE_TYPE (decl));
5765 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5766 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5767 layout_type (type);
5768 layout_decl (cldecl, 0);
5769 TREE_TYPE (decl)
5770 = c_build_qualified_type (type, TYPE_QUALS (etype));
5771 }
5772 }
5773 }
5774 }
5775 \f
5776 /* Methods for storing and printing names for error messages. */
5777
5778 /* Implement a spelling stack that allows components of a name to be pushed
5779 and popped. Each element on the stack is this structure. */
5780
5781 struct spelling
5782 {
5783 int kind;
5784 union
5785 {
5786 unsigned HOST_WIDE_INT i;
5787 const char *s;
5788 } u;
5789 };
5790
5791 #define SPELLING_STRING 1
5792 #define SPELLING_MEMBER 2
5793 #define SPELLING_BOUNDS 3
5794
5795 static struct spelling *spelling; /* Next stack element (unused). */
5796 static struct spelling *spelling_base; /* Spelling stack base. */
5797 static int spelling_size; /* Size of the spelling stack. */
5798
5799 /* Macros to save and restore the spelling stack around push_... functions.
5800 Alternative to SAVE_SPELLING_STACK. */
5801
5802 #define SPELLING_DEPTH() (spelling - spelling_base)
5803 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5804
5805 /* Push an element on the spelling stack with type KIND and assign VALUE
5806 to MEMBER. */
5807
5808 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5809 { \
5810 int depth = SPELLING_DEPTH (); \
5811 \
5812 if (depth >= spelling_size) \
5813 { \
5814 spelling_size += 10; \
5815 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5816 spelling_size); \
5817 RESTORE_SPELLING_DEPTH (depth); \
5818 } \
5819 \
5820 spelling->kind = (KIND); \
5821 spelling->MEMBER = (VALUE); \
5822 spelling++; \
5823 }
5824
5825 /* Push STRING on the stack. Printed literally. */
5826
5827 static void
5828 push_string (const char *string)
5829 {
5830 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5831 }
5832
5833 /* Push a member name on the stack. Printed as '.' STRING. */
5834
5835 static void
5836 push_member_name (tree decl)
5837 {
5838 const char *const string
5839 = (DECL_NAME (decl)
5840 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5841 : _("<anonymous>"));
5842 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5843 }
5844
5845 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5846
5847 static void
5848 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5849 {
5850 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5851 }
5852
5853 /* Compute the maximum size in bytes of the printed spelling. */
5854
5855 static int
5856 spelling_length (void)
5857 {
5858 int size = 0;
5859 struct spelling *p;
5860
5861 for (p = spelling_base; p < spelling; p++)
5862 {
5863 if (p->kind == SPELLING_BOUNDS)
5864 size += 25;
5865 else
5866 size += strlen (p->u.s) + 1;
5867 }
5868
5869 return size;
5870 }
5871
5872 /* Print the spelling to BUFFER and return it. */
5873
5874 static char *
5875 print_spelling (char *buffer)
5876 {
5877 char *d = buffer;
5878 struct spelling *p;
5879
5880 for (p = spelling_base; p < spelling; p++)
5881 if (p->kind == SPELLING_BOUNDS)
5882 {
5883 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5884 d += strlen (d);
5885 }
5886 else
5887 {
5888 const char *s;
5889 if (p->kind == SPELLING_MEMBER)
5890 *d++ = '.';
5891 for (s = p->u.s; (*d = *s++); d++)
5892 ;
5893 }
5894 *d++ = '\0';
5895 return buffer;
5896 }
5897
5898 /* Issue an error message for a bad initializer component.
5899 GMSGID identifies the message.
5900 The component name is taken from the spelling stack. */
5901
5902 void
5903 error_init (const char *gmsgid)
5904 {
5905 char *ofwhat;
5906
5907 /* The gmsgid may be a format string with %< and %>. */
5908 error (gmsgid);
5909 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5910 if (*ofwhat)
5911 error ("(near initialization for %qs)", ofwhat);
5912 }
5913
5914 /* Issue a pedantic warning for a bad initializer component. OPT is
5915 the option OPT_* (from options.h) controlling this warning or 0 if
5916 it is unconditionally given. GMSGID identifies the message. The
5917 component name is taken from the spelling stack. */
5918
5919 void
5920 pedwarn_init (location_t location, int opt, const char *gmsgid)
5921 {
5922 char *ofwhat;
5923
5924 /* The gmsgid may be a format string with %< and %>. */
5925 pedwarn (location, opt, gmsgid);
5926 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5927 if (*ofwhat)
5928 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5929 }
5930
5931 /* Issue a warning for a bad initializer component.
5932
5933 OPT is the OPT_W* value corresponding to the warning option that
5934 controls this warning. GMSGID identifies the message. The
5935 component name is taken from the spelling stack. */
5936
5937 static void
5938 warning_init (int opt, const char *gmsgid)
5939 {
5940 char *ofwhat;
5941
5942 /* The gmsgid may be a format string with %< and %>. */
5943 warning (opt, gmsgid);
5944 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5945 if (*ofwhat)
5946 warning (opt, "(near initialization for %qs)", ofwhat);
5947 }
5948 \f
5949 /* If TYPE is an array type and EXPR is a parenthesized string
5950 constant, warn if pedantic that EXPR is being used to initialize an
5951 object of type TYPE. */
5952
5953 void
5954 maybe_warn_string_init (tree type, struct c_expr expr)
5955 {
5956 if (pedantic
5957 && TREE_CODE (type) == ARRAY_TYPE
5958 && TREE_CODE (expr.value) == STRING_CST
5959 && expr.original_code != STRING_CST)
5960 pedwarn_init (input_location, OPT_pedantic,
5961 "array initialized from parenthesized string constant");
5962 }
5963
5964 /* Digest the parser output INIT as an initializer for type TYPE.
5965 Return a C expression of type TYPE to represent the initial value.
5966
5967 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5968
5969 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5970
5971 If INIT is a string constant, STRICT_STRING is true if it is
5972 unparenthesized or we should not warn here for it being parenthesized.
5973 For other types of INIT, STRICT_STRING is not used.
5974
5975 INIT_LOC is the location of the INIT.
5976
5977 REQUIRE_CONSTANT requests an error if non-constant initializers or
5978 elements are seen. */
5979
5980 static tree
5981 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5982 bool null_pointer_constant, bool strict_string,
5983 int require_constant)
5984 {
5985 enum tree_code code = TREE_CODE (type);
5986 tree inside_init = init;
5987 tree semantic_type = NULL_TREE;
5988 bool maybe_const = true;
5989
5990 if (type == error_mark_node
5991 || !init
5992 || init == error_mark_node
5993 || TREE_TYPE (init) == error_mark_node)
5994 return error_mark_node;
5995
5996 STRIP_TYPE_NOPS (inside_init);
5997
5998 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5999 {
6000 semantic_type = TREE_TYPE (inside_init);
6001 inside_init = TREE_OPERAND (inside_init, 0);
6002 }
6003 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6004 inside_init = decl_constant_value_for_optimization (inside_init);
6005
6006 /* Initialization of an array of chars from a string constant
6007 optionally enclosed in braces. */
6008
6009 if (code == ARRAY_TYPE && inside_init
6010 && TREE_CODE (inside_init) == STRING_CST)
6011 {
6012 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6013 /* Note that an array could be both an array of character type
6014 and an array of wchar_t if wchar_t is signed char or unsigned
6015 char. */
6016 bool char_array = (typ1 == char_type_node
6017 || typ1 == signed_char_type_node
6018 || typ1 == unsigned_char_type_node);
6019 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6020 bool char16_array = !!comptypes (typ1, char16_type_node);
6021 bool char32_array = !!comptypes (typ1, char32_type_node);
6022
6023 if (char_array || wchar_array || char16_array || char32_array)
6024 {
6025 struct c_expr expr;
6026 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6027 expr.value = inside_init;
6028 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6029 expr.original_type = NULL;
6030 maybe_warn_string_init (type, expr);
6031
6032 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6033 pedwarn_init (init_loc, OPT_pedantic,
6034 "initialization of a flexible array member");
6035
6036 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6037 TYPE_MAIN_VARIANT (type)))
6038 return inside_init;
6039
6040 if (char_array)
6041 {
6042 if (typ2 != char_type_node)
6043 {
6044 error_init ("char-array initialized from wide string");
6045 return error_mark_node;
6046 }
6047 }
6048 else
6049 {
6050 if (typ2 == char_type_node)
6051 {
6052 error_init ("wide character array initialized from non-wide "
6053 "string");
6054 return error_mark_node;
6055 }
6056 else if (!comptypes(typ1, typ2))
6057 {
6058 error_init ("wide character array initialized from "
6059 "incompatible wide string");
6060 return error_mark_node;
6061 }
6062 }
6063
6064 TREE_TYPE (inside_init) = type;
6065 if (TYPE_DOMAIN (type) != 0
6066 && TYPE_SIZE (type) != 0
6067 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6068 {
6069 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6070
6071 /* Subtract the size of a single (possibly wide) character
6072 because it's ok to ignore the terminating null char
6073 that is counted in the length of the constant. */
6074 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6075 (len
6076 - (TYPE_PRECISION (typ1)
6077 / BITS_PER_UNIT))))
6078 pedwarn_init (init_loc, 0,
6079 ("initializer-string for array of chars "
6080 "is too long"));
6081 else if (warn_cxx_compat
6082 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6083 warning_at (init_loc, OPT_Wc___compat,
6084 ("initializer-string for array chars "
6085 "is too long for C++"));
6086 }
6087
6088 return inside_init;
6089 }
6090 else if (INTEGRAL_TYPE_P (typ1))
6091 {
6092 error_init ("array of inappropriate type initialized "
6093 "from string constant");
6094 return error_mark_node;
6095 }
6096 }
6097
6098 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6099 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6100 below and handle as a constructor. */
6101 if (code == VECTOR_TYPE
6102 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6103 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6104 && TREE_CONSTANT (inside_init))
6105 {
6106 if (TREE_CODE (inside_init) == VECTOR_CST
6107 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6108 TYPE_MAIN_VARIANT (type)))
6109 return inside_init;
6110
6111 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6112 {
6113 unsigned HOST_WIDE_INT ix;
6114 tree value;
6115 bool constant_p = true;
6116
6117 /* Iterate through elements and check if all constructor
6118 elements are *_CSTs. */
6119 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6120 if (!CONSTANT_CLASS_P (value))
6121 {
6122 constant_p = false;
6123 break;
6124 }
6125
6126 if (constant_p)
6127 return build_vector_from_ctor (type,
6128 CONSTRUCTOR_ELTS (inside_init));
6129 }
6130 }
6131
6132 if (warn_sequence_point)
6133 verify_sequence_points (inside_init);
6134
6135 /* Any type can be initialized
6136 from an expression of the same type, optionally with braces. */
6137
6138 if (inside_init && TREE_TYPE (inside_init) != 0
6139 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6140 TYPE_MAIN_VARIANT (type))
6141 || (code == ARRAY_TYPE
6142 && comptypes (TREE_TYPE (inside_init), type))
6143 || (code == VECTOR_TYPE
6144 && comptypes (TREE_TYPE (inside_init), type))
6145 || (code == POINTER_TYPE
6146 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6147 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6148 TREE_TYPE (type)))))
6149 {
6150 if (code == POINTER_TYPE)
6151 {
6152 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6153 {
6154 if (TREE_CODE (inside_init) == STRING_CST
6155 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6156 inside_init = array_to_pointer_conversion
6157 (init_loc, inside_init);
6158 else
6159 {
6160 error_init ("invalid use of non-lvalue array");
6161 return error_mark_node;
6162 }
6163 }
6164 }
6165
6166 if (code == VECTOR_TYPE)
6167 /* Although the types are compatible, we may require a
6168 conversion. */
6169 inside_init = convert (type, inside_init);
6170
6171 if (require_constant
6172 && (code == VECTOR_TYPE || !flag_isoc99)
6173 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6174 {
6175 /* As an extension, allow initializing objects with static storage
6176 duration with compound literals (which are then treated just as
6177 the brace enclosed list they contain). Also allow this for
6178 vectors, as we can only assign them with compound literals. */
6179 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6180 inside_init = DECL_INITIAL (decl);
6181 }
6182
6183 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6184 && TREE_CODE (inside_init) != CONSTRUCTOR)
6185 {
6186 error_init ("array initialized from non-constant array expression");
6187 return error_mark_node;
6188 }
6189
6190 /* Compound expressions can only occur here if -pedantic or
6191 -pedantic-errors is specified. In the later case, we always want
6192 an error. In the former case, we simply want a warning. */
6193 if (require_constant && pedantic
6194 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6195 {
6196 inside_init
6197 = valid_compound_expr_initializer (inside_init,
6198 TREE_TYPE (inside_init));
6199 if (inside_init == error_mark_node)
6200 error_init ("initializer element is not constant");
6201 else
6202 pedwarn_init (init_loc, OPT_pedantic,
6203 "initializer element is not constant");
6204 if (flag_pedantic_errors)
6205 inside_init = error_mark_node;
6206 }
6207 else if (require_constant
6208 && !initializer_constant_valid_p (inside_init,
6209 TREE_TYPE (inside_init)))
6210 {
6211 error_init ("initializer element is not constant");
6212 inside_init = error_mark_node;
6213 }
6214 else if (require_constant && !maybe_const)
6215 pedwarn_init (init_loc, 0,
6216 "initializer element is not a constant expression");
6217
6218 /* Added to enable additional -Wmissing-format-attribute warnings. */
6219 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6220 inside_init = convert_for_assignment (init_loc, type, inside_init,
6221 origtype,
6222 ic_init, null_pointer_constant,
6223 NULL_TREE, NULL_TREE, 0);
6224 return inside_init;
6225 }
6226
6227 /* Handle scalar types, including conversions. */
6228
6229 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6230 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6231 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6232 {
6233 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6234 && (TREE_CODE (init) == STRING_CST
6235 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6236 inside_init = init = array_to_pointer_conversion (init_loc, init);
6237 if (semantic_type)
6238 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6239 inside_init);
6240 inside_init
6241 = convert_for_assignment (init_loc, type, inside_init, origtype,
6242 ic_init, null_pointer_constant,
6243 NULL_TREE, NULL_TREE, 0);
6244
6245 /* Check to see if we have already given an error message. */
6246 if (inside_init == error_mark_node)
6247 ;
6248 else if (require_constant && !TREE_CONSTANT (inside_init))
6249 {
6250 error_init ("initializer element is not constant");
6251 inside_init = error_mark_node;
6252 }
6253 else if (require_constant
6254 && !initializer_constant_valid_p (inside_init,
6255 TREE_TYPE (inside_init)))
6256 {
6257 error_init ("initializer element is not computable at load time");
6258 inside_init = error_mark_node;
6259 }
6260 else if (require_constant && !maybe_const)
6261 pedwarn_init (init_loc, 0,
6262 "initializer element is not a constant expression");
6263
6264 return inside_init;
6265 }
6266
6267 /* Come here only for records and arrays. */
6268
6269 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6270 {
6271 error_init ("variable-sized object may not be initialized");
6272 return error_mark_node;
6273 }
6274
6275 error_init ("invalid initializer");
6276 return error_mark_node;
6277 }
6278 \f
6279 /* Handle initializers that use braces. */
6280
6281 /* Type of object we are accumulating a constructor for.
6282 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6283 static tree constructor_type;
6284
6285 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6286 left to fill. */
6287 static tree constructor_fields;
6288
6289 /* For an ARRAY_TYPE, this is the specified index
6290 at which to store the next element we get. */
6291 static tree constructor_index;
6292
6293 /* For an ARRAY_TYPE, this is the maximum index. */
6294 static tree constructor_max_index;
6295
6296 /* For a RECORD_TYPE, this is the first field not yet written out. */
6297 static tree constructor_unfilled_fields;
6298
6299 /* For an ARRAY_TYPE, this is the index of the first element
6300 not yet written out. */
6301 static tree constructor_unfilled_index;
6302
6303 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6304 This is so we can generate gaps between fields, when appropriate. */
6305 static tree constructor_bit_index;
6306
6307 /* If we are saving up the elements rather than allocating them,
6308 this is the list of elements so far (in reverse order,
6309 most recent first). */
6310 static VEC(constructor_elt,gc) *constructor_elements;
6311
6312 /* 1 if constructor should be incrementally stored into a constructor chain,
6313 0 if all the elements should be kept in AVL tree. */
6314 static int constructor_incremental;
6315
6316 /* 1 if so far this constructor's elements are all compile-time constants. */
6317 static int constructor_constant;
6318
6319 /* 1 if so far this constructor's elements are all valid address constants. */
6320 static int constructor_simple;
6321
6322 /* 1 if this constructor has an element that cannot be part of a
6323 constant expression. */
6324 static int constructor_nonconst;
6325
6326 /* 1 if this constructor is erroneous so far. */
6327 static int constructor_erroneous;
6328
6329 /* Structure for managing pending initializer elements, organized as an
6330 AVL tree. */
6331
6332 struct init_node
6333 {
6334 struct init_node *left, *right;
6335 struct init_node *parent;
6336 int balance;
6337 tree purpose;
6338 tree value;
6339 tree origtype;
6340 };
6341
6342 /* Tree of pending elements at this constructor level.
6343 These are elements encountered out of order
6344 which belong at places we haven't reached yet in actually
6345 writing the output.
6346 Will never hold tree nodes across GC runs. */
6347 static struct init_node *constructor_pending_elts;
6348
6349 /* The SPELLING_DEPTH of this constructor. */
6350 static int constructor_depth;
6351
6352 /* DECL node for which an initializer is being read.
6353 0 means we are reading a constructor expression
6354 such as (struct foo) {...}. */
6355 static tree constructor_decl;
6356
6357 /* Nonzero if this is an initializer for a top-level decl. */
6358 static int constructor_top_level;
6359
6360 /* Nonzero if there were any member designators in this initializer. */
6361 static int constructor_designated;
6362
6363 /* Nesting depth of designator list. */
6364 static int designator_depth;
6365
6366 /* Nonzero if there were diagnosed errors in this designator list. */
6367 static int designator_erroneous;
6368
6369 \f
6370 /* This stack has a level for each implicit or explicit level of
6371 structuring in the initializer, including the outermost one. It
6372 saves the values of most of the variables above. */
6373
6374 struct constructor_range_stack;
6375
6376 struct constructor_stack
6377 {
6378 struct constructor_stack *next;
6379 tree type;
6380 tree fields;
6381 tree index;
6382 tree max_index;
6383 tree unfilled_index;
6384 tree unfilled_fields;
6385 tree bit_index;
6386 VEC(constructor_elt,gc) *elements;
6387 struct init_node *pending_elts;
6388 int offset;
6389 int depth;
6390 /* If value nonzero, this value should replace the entire
6391 constructor at this level. */
6392 struct c_expr replacement_value;
6393 struct constructor_range_stack *range_stack;
6394 char constant;
6395 char simple;
6396 char nonconst;
6397 char implicit;
6398 char erroneous;
6399 char outer;
6400 char incremental;
6401 char designated;
6402 };
6403
6404 static struct constructor_stack *constructor_stack;
6405
6406 /* This stack represents designators from some range designator up to
6407 the last designator in the list. */
6408
6409 struct constructor_range_stack
6410 {
6411 struct constructor_range_stack *next, *prev;
6412 struct constructor_stack *stack;
6413 tree range_start;
6414 tree index;
6415 tree range_end;
6416 tree fields;
6417 };
6418
6419 static struct constructor_range_stack *constructor_range_stack;
6420
6421 /* This stack records separate initializers that are nested.
6422 Nested initializers can't happen in ANSI C, but GNU C allows them
6423 in cases like { ... (struct foo) { ... } ... }. */
6424
6425 struct initializer_stack
6426 {
6427 struct initializer_stack *next;
6428 tree decl;
6429 struct constructor_stack *constructor_stack;
6430 struct constructor_range_stack *constructor_range_stack;
6431 VEC(constructor_elt,gc) *elements;
6432 struct spelling *spelling;
6433 struct spelling *spelling_base;
6434 int spelling_size;
6435 char top_level;
6436 char require_constant_value;
6437 char require_constant_elements;
6438 };
6439
6440 static struct initializer_stack *initializer_stack;
6441 \f
6442 /* Prepare to parse and output the initializer for variable DECL. */
6443
6444 void
6445 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6446 {
6447 const char *locus;
6448 struct initializer_stack *p = XNEW (struct initializer_stack);
6449
6450 p->decl = constructor_decl;
6451 p->require_constant_value = require_constant_value;
6452 p->require_constant_elements = require_constant_elements;
6453 p->constructor_stack = constructor_stack;
6454 p->constructor_range_stack = constructor_range_stack;
6455 p->elements = constructor_elements;
6456 p->spelling = spelling;
6457 p->spelling_base = spelling_base;
6458 p->spelling_size = spelling_size;
6459 p->top_level = constructor_top_level;
6460 p->next = initializer_stack;
6461 initializer_stack = p;
6462
6463 constructor_decl = decl;
6464 constructor_designated = 0;
6465 constructor_top_level = top_level;
6466
6467 if (decl != 0 && decl != error_mark_node)
6468 {
6469 require_constant_value = TREE_STATIC (decl);
6470 require_constant_elements
6471 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6472 /* For a scalar, you can always use any value to initialize,
6473 even within braces. */
6474 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6475 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6476 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6477 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6478 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6479 }
6480 else
6481 {
6482 require_constant_value = 0;
6483 require_constant_elements = 0;
6484 locus = _("(anonymous)");
6485 }
6486
6487 constructor_stack = 0;
6488 constructor_range_stack = 0;
6489
6490 missing_braces_mentioned = 0;
6491
6492 spelling_base = 0;
6493 spelling_size = 0;
6494 RESTORE_SPELLING_DEPTH (0);
6495
6496 if (locus)
6497 push_string (locus);
6498 }
6499
6500 void
6501 finish_init (void)
6502 {
6503 struct initializer_stack *p = initializer_stack;
6504
6505 /* Free the whole constructor stack of this initializer. */
6506 while (constructor_stack)
6507 {
6508 struct constructor_stack *q = constructor_stack;
6509 constructor_stack = q->next;
6510 free (q);
6511 }
6512
6513 gcc_assert (!constructor_range_stack);
6514
6515 /* Pop back to the data of the outer initializer (if any). */
6516 free (spelling_base);
6517
6518 constructor_decl = p->decl;
6519 require_constant_value = p->require_constant_value;
6520 require_constant_elements = p->require_constant_elements;
6521 constructor_stack = p->constructor_stack;
6522 constructor_range_stack = p->constructor_range_stack;
6523 constructor_elements = p->elements;
6524 spelling = p->spelling;
6525 spelling_base = p->spelling_base;
6526 spelling_size = p->spelling_size;
6527 constructor_top_level = p->top_level;
6528 initializer_stack = p->next;
6529 free (p);
6530 }
6531 \f
6532 /* Call here when we see the initializer is surrounded by braces.
6533 This is instead of a call to push_init_level;
6534 it is matched by a call to pop_init_level.
6535
6536 TYPE is the type to initialize, for a constructor expression.
6537 For an initializer for a decl, TYPE is zero. */
6538
6539 void
6540 really_start_incremental_init (tree type)
6541 {
6542 struct constructor_stack *p = XNEW (struct constructor_stack);
6543
6544 if (type == 0)
6545 type = TREE_TYPE (constructor_decl);
6546
6547 if (TREE_CODE (type) == VECTOR_TYPE
6548 && TYPE_VECTOR_OPAQUE (type))
6549 error ("opaque vector types cannot be initialized");
6550
6551 p->type = constructor_type;
6552 p->fields = constructor_fields;
6553 p->index = constructor_index;
6554 p->max_index = constructor_max_index;
6555 p->unfilled_index = constructor_unfilled_index;
6556 p->unfilled_fields = constructor_unfilled_fields;
6557 p->bit_index = constructor_bit_index;
6558 p->elements = constructor_elements;
6559 p->constant = constructor_constant;
6560 p->simple = constructor_simple;
6561 p->nonconst = constructor_nonconst;
6562 p->erroneous = constructor_erroneous;
6563 p->pending_elts = constructor_pending_elts;
6564 p->depth = constructor_depth;
6565 p->replacement_value.value = 0;
6566 p->replacement_value.original_code = ERROR_MARK;
6567 p->replacement_value.original_type = NULL;
6568 p->implicit = 0;
6569 p->range_stack = 0;
6570 p->outer = 0;
6571 p->incremental = constructor_incremental;
6572 p->designated = constructor_designated;
6573 p->next = 0;
6574 constructor_stack = p;
6575
6576 constructor_constant = 1;
6577 constructor_simple = 1;
6578 constructor_nonconst = 0;
6579 constructor_depth = SPELLING_DEPTH ();
6580 constructor_elements = 0;
6581 constructor_pending_elts = 0;
6582 constructor_type = type;
6583 constructor_incremental = 1;
6584 constructor_designated = 0;
6585 designator_depth = 0;
6586 designator_erroneous = 0;
6587
6588 if (TREE_CODE (constructor_type) == RECORD_TYPE
6589 || TREE_CODE (constructor_type) == UNION_TYPE)
6590 {
6591 constructor_fields = TYPE_FIELDS (constructor_type);
6592 /* Skip any nameless bit fields at the beginning. */
6593 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6594 && DECL_NAME (constructor_fields) == 0)
6595 constructor_fields = DECL_CHAIN (constructor_fields);
6596
6597 constructor_unfilled_fields = constructor_fields;
6598 constructor_bit_index = bitsize_zero_node;
6599 }
6600 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6601 {
6602 if (TYPE_DOMAIN (constructor_type))
6603 {
6604 constructor_max_index
6605 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6606
6607 /* Detect non-empty initializations of zero-length arrays. */
6608 if (constructor_max_index == NULL_TREE
6609 && TYPE_SIZE (constructor_type))
6610 constructor_max_index = integer_minus_one_node;
6611
6612 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6613 to initialize VLAs will cause a proper error; avoid tree
6614 checking errors as well by setting a safe value. */
6615 if (constructor_max_index
6616 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6617 constructor_max_index = integer_minus_one_node;
6618
6619 constructor_index
6620 = convert (bitsizetype,
6621 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6622 }
6623 else
6624 {
6625 constructor_index = bitsize_zero_node;
6626 constructor_max_index = NULL_TREE;
6627 }
6628
6629 constructor_unfilled_index = constructor_index;
6630 }
6631 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6632 {
6633 /* Vectors are like simple fixed-size arrays. */
6634 constructor_max_index =
6635 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6636 constructor_index = bitsize_zero_node;
6637 constructor_unfilled_index = constructor_index;
6638 }
6639 else
6640 {
6641 /* Handle the case of int x = {5}; */
6642 constructor_fields = constructor_type;
6643 constructor_unfilled_fields = constructor_type;
6644 }
6645 }
6646 \f
6647 /* Push down into a subobject, for initialization.
6648 If this is for an explicit set of braces, IMPLICIT is 0.
6649 If it is because the next element belongs at a lower level,
6650 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6651
6652 void
6653 push_init_level (int implicit, struct obstack * braced_init_obstack)
6654 {
6655 struct constructor_stack *p;
6656 tree value = NULL_TREE;
6657
6658 /* If we've exhausted any levels that didn't have braces,
6659 pop them now. If implicit == 1, this will have been done in
6660 process_init_element; do not repeat it here because in the case
6661 of excess initializers for an empty aggregate this leads to an
6662 infinite cycle of popping a level and immediately recreating
6663 it. */
6664 if (implicit != 1)
6665 {
6666 while (constructor_stack->implicit)
6667 {
6668 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6669 || TREE_CODE (constructor_type) == UNION_TYPE)
6670 && constructor_fields == 0)
6671 process_init_element (pop_init_level (1, braced_init_obstack),
6672 true, braced_init_obstack);
6673 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6674 && constructor_max_index
6675 && tree_int_cst_lt (constructor_max_index,
6676 constructor_index))
6677 process_init_element (pop_init_level (1, braced_init_obstack),
6678 true, braced_init_obstack);
6679 else
6680 break;
6681 }
6682 }
6683
6684 /* Unless this is an explicit brace, we need to preserve previous
6685 content if any. */
6686 if (implicit)
6687 {
6688 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6689 || TREE_CODE (constructor_type) == UNION_TYPE)
6690 && constructor_fields)
6691 value = find_init_member (constructor_fields, braced_init_obstack);
6692 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6693 value = find_init_member (constructor_index, braced_init_obstack);
6694 }
6695
6696 p = XNEW (struct constructor_stack);
6697 p->type = constructor_type;
6698 p->fields = constructor_fields;
6699 p->index = constructor_index;
6700 p->max_index = constructor_max_index;
6701 p->unfilled_index = constructor_unfilled_index;
6702 p->unfilled_fields = constructor_unfilled_fields;
6703 p->bit_index = constructor_bit_index;
6704 p->elements = constructor_elements;
6705 p->constant = constructor_constant;
6706 p->simple = constructor_simple;
6707 p->nonconst = constructor_nonconst;
6708 p->erroneous = constructor_erroneous;
6709 p->pending_elts = constructor_pending_elts;
6710 p->depth = constructor_depth;
6711 p->replacement_value.value = 0;
6712 p->replacement_value.original_code = ERROR_MARK;
6713 p->replacement_value.original_type = NULL;
6714 p->implicit = implicit;
6715 p->outer = 0;
6716 p->incremental = constructor_incremental;
6717 p->designated = constructor_designated;
6718 p->next = constructor_stack;
6719 p->range_stack = 0;
6720 constructor_stack = p;
6721
6722 constructor_constant = 1;
6723 constructor_simple = 1;
6724 constructor_nonconst = 0;
6725 constructor_depth = SPELLING_DEPTH ();
6726 constructor_elements = 0;
6727 constructor_incremental = 1;
6728 constructor_designated = 0;
6729 constructor_pending_elts = 0;
6730 if (!implicit)
6731 {
6732 p->range_stack = constructor_range_stack;
6733 constructor_range_stack = 0;
6734 designator_depth = 0;
6735 designator_erroneous = 0;
6736 }
6737
6738 /* Don't die if an entire brace-pair level is superfluous
6739 in the containing level. */
6740 if (constructor_type == 0)
6741 ;
6742 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6743 || TREE_CODE (constructor_type) == UNION_TYPE)
6744 {
6745 /* Don't die if there are extra init elts at the end. */
6746 if (constructor_fields == 0)
6747 constructor_type = 0;
6748 else
6749 {
6750 constructor_type = TREE_TYPE (constructor_fields);
6751 push_member_name (constructor_fields);
6752 constructor_depth++;
6753 }
6754 }
6755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6756 {
6757 constructor_type = TREE_TYPE (constructor_type);
6758 push_array_bounds (tree_low_cst (constructor_index, 1));
6759 constructor_depth++;
6760 }
6761
6762 if (constructor_type == 0)
6763 {
6764 error_init ("extra brace group at end of initializer");
6765 constructor_fields = 0;
6766 constructor_unfilled_fields = 0;
6767 return;
6768 }
6769
6770 if (value && TREE_CODE (value) == CONSTRUCTOR)
6771 {
6772 constructor_constant = TREE_CONSTANT (value);
6773 constructor_simple = TREE_STATIC (value);
6774 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6775 constructor_elements = CONSTRUCTOR_ELTS (value);
6776 if (!VEC_empty (constructor_elt, constructor_elements)
6777 && (TREE_CODE (constructor_type) == RECORD_TYPE
6778 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6779 set_nonincremental_init (braced_init_obstack);
6780 }
6781
6782 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6783 {
6784 missing_braces_mentioned = 1;
6785 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6786 }
6787
6788 if (TREE_CODE (constructor_type) == RECORD_TYPE
6789 || TREE_CODE (constructor_type) == UNION_TYPE)
6790 {
6791 constructor_fields = TYPE_FIELDS (constructor_type);
6792 /* Skip any nameless bit fields at the beginning. */
6793 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6794 && DECL_NAME (constructor_fields) == 0)
6795 constructor_fields = DECL_CHAIN (constructor_fields);
6796
6797 constructor_unfilled_fields = constructor_fields;
6798 constructor_bit_index = bitsize_zero_node;
6799 }
6800 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6801 {
6802 /* Vectors are like simple fixed-size arrays. */
6803 constructor_max_index =
6804 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6805 constructor_index = bitsize_int (0);
6806 constructor_unfilled_index = constructor_index;
6807 }
6808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6809 {
6810 if (TYPE_DOMAIN (constructor_type))
6811 {
6812 constructor_max_index
6813 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6814
6815 /* Detect non-empty initializations of zero-length arrays. */
6816 if (constructor_max_index == NULL_TREE
6817 && TYPE_SIZE (constructor_type))
6818 constructor_max_index = integer_minus_one_node;
6819
6820 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6821 to initialize VLAs will cause a proper error; avoid tree
6822 checking errors as well by setting a safe value. */
6823 if (constructor_max_index
6824 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6825 constructor_max_index = integer_minus_one_node;
6826
6827 constructor_index
6828 = convert (bitsizetype,
6829 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6830 }
6831 else
6832 constructor_index = bitsize_zero_node;
6833
6834 constructor_unfilled_index = constructor_index;
6835 if (value && TREE_CODE (value) == STRING_CST)
6836 {
6837 /* We need to split the char/wchar array into individual
6838 characters, so that we don't have to special case it
6839 everywhere. */
6840 set_nonincremental_init_from_string (value, braced_init_obstack);
6841 }
6842 }
6843 else
6844 {
6845 if (constructor_type != error_mark_node)
6846 warning_init (0, "braces around scalar initializer");
6847 constructor_fields = constructor_type;
6848 constructor_unfilled_fields = constructor_type;
6849 }
6850 }
6851
6852 /* At the end of an implicit or explicit brace level,
6853 finish up that level of constructor. If a single expression
6854 with redundant braces initialized that level, return the
6855 c_expr structure for that expression. Otherwise, the original_code
6856 element is set to ERROR_MARK.
6857 If we were outputting the elements as they are read, return 0 as the value
6858 from inner levels (process_init_element ignores that),
6859 but return error_mark_node as the value from the outermost level
6860 (that's what we want to put in DECL_INITIAL).
6861 Otherwise, return a CONSTRUCTOR expression as the value. */
6862
6863 struct c_expr
6864 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6865 {
6866 struct constructor_stack *p;
6867 struct c_expr ret;
6868 ret.value = 0;
6869 ret.original_code = ERROR_MARK;
6870 ret.original_type = NULL;
6871
6872 if (implicit == 0)
6873 {
6874 /* When we come to an explicit close brace,
6875 pop any inner levels that didn't have explicit braces. */
6876 while (constructor_stack->implicit)
6877 {
6878 process_init_element (pop_init_level (1, braced_init_obstack),
6879 true, braced_init_obstack);
6880 }
6881 gcc_assert (!constructor_range_stack);
6882 }
6883
6884 /* Now output all pending elements. */
6885 constructor_incremental = 1;
6886 output_pending_init_elements (1, braced_init_obstack);
6887
6888 p = constructor_stack;
6889
6890 /* Error for initializing a flexible array member, or a zero-length
6891 array member in an inappropriate context. */
6892 if (constructor_type && constructor_fields
6893 && TREE_CODE (constructor_type) == ARRAY_TYPE
6894 && TYPE_DOMAIN (constructor_type)
6895 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6896 {
6897 /* Silently discard empty initializations. The parser will
6898 already have pedwarned for empty brackets. */
6899 if (integer_zerop (constructor_unfilled_index))
6900 constructor_type = NULL_TREE;
6901 else
6902 {
6903 gcc_assert (!TYPE_SIZE (constructor_type));
6904
6905 if (constructor_depth > 2)
6906 error_init ("initialization of flexible array member in a nested context");
6907 else
6908 pedwarn_init (input_location, OPT_pedantic,
6909 "initialization of a flexible array member");
6910
6911 /* We have already issued an error message for the existence
6912 of a flexible array member not at the end of the structure.
6913 Discard the initializer so that we do not die later. */
6914 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6915 constructor_type = NULL_TREE;
6916 }
6917 }
6918
6919 /* Warn when some struct elements are implicitly initialized to zero. */
6920 if (warn_missing_field_initializers
6921 && constructor_type
6922 && TREE_CODE (constructor_type) == RECORD_TYPE
6923 && constructor_unfilled_fields)
6924 {
6925 bool constructor_zeroinit =
6926 (VEC_length (constructor_elt, constructor_elements) == 1
6927 && integer_zerop
6928 (VEC_index (constructor_elt, constructor_elements, 0)->value));
6929
6930 /* Do not warn for flexible array members or zero-length arrays. */
6931 while (constructor_unfilled_fields
6932 && (!DECL_SIZE (constructor_unfilled_fields)
6933 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6934 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6935
6936 if (constructor_unfilled_fields
6937 /* Do not warn if this level of the initializer uses member
6938 designators; it is likely to be deliberate. */
6939 && !constructor_designated
6940 /* Do not warn about initializing with ` = {0}'. */
6941 && !constructor_zeroinit)
6942 {
6943 push_member_name (constructor_unfilled_fields);
6944 warning_init (OPT_Wmissing_field_initializers,
6945 "missing initializer");
6946 RESTORE_SPELLING_DEPTH (constructor_depth);
6947 }
6948 }
6949
6950 /* Pad out the end of the structure. */
6951 if (p->replacement_value.value)
6952 /* If this closes a superfluous brace pair,
6953 just pass out the element between them. */
6954 ret = p->replacement_value;
6955 else if (constructor_type == 0)
6956 ;
6957 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6958 && TREE_CODE (constructor_type) != UNION_TYPE
6959 && TREE_CODE (constructor_type) != ARRAY_TYPE
6960 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6961 {
6962 /* A nonincremental scalar initializer--just return
6963 the element, after verifying there is just one. */
6964 if (VEC_empty (constructor_elt,constructor_elements))
6965 {
6966 if (!constructor_erroneous)
6967 error_init ("empty scalar initializer");
6968 ret.value = error_mark_node;
6969 }
6970 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6971 {
6972 error_init ("extra elements in scalar initializer");
6973 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6974 }
6975 else
6976 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6977 }
6978 else
6979 {
6980 if (constructor_erroneous)
6981 ret.value = error_mark_node;
6982 else
6983 {
6984 ret.value = build_constructor (constructor_type,
6985 constructor_elements);
6986 if (constructor_constant)
6987 TREE_CONSTANT (ret.value) = 1;
6988 if (constructor_constant && constructor_simple)
6989 TREE_STATIC (ret.value) = 1;
6990 if (constructor_nonconst)
6991 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6992 }
6993 }
6994
6995 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6996 {
6997 if (constructor_nonconst)
6998 ret.original_code = C_MAYBE_CONST_EXPR;
6999 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7000 ret.original_code = ERROR_MARK;
7001 }
7002
7003 constructor_type = p->type;
7004 constructor_fields = p->fields;
7005 constructor_index = p->index;
7006 constructor_max_index = p->max_index;
7007 constructor_unfilled_index = p->unfilled_index;
7008 constructor_unfilled_fields = p->unfilled_fields;
7009 constructor_bit_index = p->bit_index;
7010 constructor_elements = p->elements;
7011 constructor_constant = p->constant;
7012 constructor_simple = p->simple;
7013 constructor_nonconst = p->nonconst;
7014 constructor_erroneous = p->erroneous;
7015 constructor_incremental = p->incremental;
7016 constructor_designated = p->designated;
7017 constructor_pending_elts = p->pending_elts;
7018 constructor_depth = p->depth;
7019 if (!p->implicit)
7020 constructor_range_stack = p->range_stack;
7021 RESTORE_SPELLING_DEPTH (constructor_depth);
7022
7023 constructor_stack = p->next;
7024 free (p);
7025
7026 if (ret.value == 0 && constructor_stack == 0)
7027 ret.value = error_mark_node;
7028 return ret;
7029 }
7030
7031 /* Common handling for both array range and field name designators.
7032 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7033
7034 static int
7035 set_designator (int array, struct obstack * braced_init_obstack)
7036 {
7037 tree subtype;
7038 enum tree_code subcode;
7039
7040 /* Don't die if an entire brace-pair level is superfluous
7041 in the containing level. */
7042 if (constructor_type == 0)
7043 return 1;
7044
7045 /* If there were errors in this designator list already, bail out
7046 silently. */
7047 if (designator_erroneous)
7048 return 1;
7049
7050 if (!designator_depth)
7051 {
7052 gcc_assert (!constructor_range_stack);
7053
7054 /* Designator list starts at the level of closest explicit
7055 braces. */
7056 while (constructor_stack->implicit)
7057 {
7058 process_init_element (pop_init_level (1, braced_init_obstack),
7059 true, braced_init_obstack);
7060 }
7061 constructor_designated = 1;
7062 return 0;
7063 }
7064
7065 switch (TREE_CODE (constructor_type))
7066 {
7067 case RECORD_TYPE:
7068 case UNION_TYPE:
7069 subtype = TREE_TYPE (constructor_fields);
7070 if (subtype != error_mark_node)
7071 subtype = TYPE_MAIN_VARIANT (subtype);
7072 break;
7073 case ARRAY_TYPE:
7074 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7075 break;
7076 default:
7077 gcc_unreachable ();
7078 }
7079
7080 subcode = TREE_CODE (subtype);
7081 if (array && subcode != ARRAY_TYPE)
7082 {
7083 error_init ("array index in non-array initializer");
7084 return 1;
7085 }
7086 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7087 {
7088 error_init ("field name not in record or union initializer");
7089 return 1;
7090 }
7091
7092 constructor_designated = 1;
7093 push_init_level (2, braced_init_obstack);
7094 return 0;
7095 }
7096
7097 /* If there are range designators in designator list, push a new designator
7098 to constructor_range_stack. RANGE_END is end of such stack range or
7099 NULL_TREE if there is no range designator at this level. */
7100
7101 static void
7102 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7103 {
7104 struct constructor_range_stack *p;
7105
7106 p = (struct constructor_range_stack *)
7107 obstack_alloc (braced_init_obstack,
7108 sizeof (struct constructor_range_stack));
7109 p->prev = constructor_range_stack;
7110 p->next = 0;
7111 p->fields = constructor_fields;
7112 p->range_start = constructor_index;
7113 p->index = constructor_index;
7114 p->stack = constructor_stack;
7115 p->range_end = range_end;
7116 if (constructor_range_stack)
7117 constructor_range_stack->next = p;
7118 constructor_range_stack = p;
7119 }
7120
7121 /* Within an array initializer, specify the next index to be initialized.
7122 FIRST is that index. If LAST is nonzero, then initialize a range
7123 of indices, running from FIRST through LAST. */
7124
7125 void
7126 set_init_index (tree first, tree last,
7127 struct obstack * braced_init_obstack)
7128 {
7129 if (set_designator (1, braced_init_obstack))
7130 return;
7131
7132 designator_erroneous = 1;
7133
7134 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7135 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7136 {
7137 error_init ("array index in initializer not of integer type");
7138 return;
7139 }
7140
7141 if (TREE_CODE (first) != INTEGER_CST)
7142 {
7143 first = c_fully_fold (first, false, NULL);
7144 if (TREE_CODE (first) == INTEGER_CST)
7145 pedwarn_init (input_location, OPT_pedantic,
7146 "array index in initializer is not "
7147 "an integer constant expression");
7148 }
7149
7150 if (last && TREE_CODE (last) != INTEGER_CST)
7151 {
7152 last = c_fully_fold (last, false, NULL);
7153 if (TREE_CODE (last) == INTEGER_CST)
7154 pedwarn_init (input_location, OPT_pedantic,
7155 "array index in initializer is not "
7156 "an integer constant expression");
7157 }
7158
7159 if (TREE_CODE (first) != INTEGER_CST)
7160 error_init ("nonconstant array index in initializer");
7161 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7162 error_init ("nonconstant array index in initializer");
7163 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7164 error_init ("array index in non-array initializer");
7165 else if (tree_int_cst_sgn (first) == -1)
7166 error_init ("array index in initializer exceeds array bounds");
7167 else if (constructor_max_index
7168 && tree_int_cst_lt (constructor_max_index, first))
7169 error_init ("array index in initializer exceeds array bounds");
7170 else
7171 {
7172 constant_expression_warning (first);
7173 if (last)
7174 constant_expression_warning (last);
7175 constructor_index = convert (bitsizetype, first);
7176
7177 if (last)
7178 {
7179 if (tree_int_cst_equal (first, last))
7180 last = 0;
7181 else if (tree_int_cst_lt (last, first))
7182 {
7183 error_init ("empty index range in initializer");
7184 last = 0;
7185 }
7186 else
7187 {
7188 last = convert (bitsizetype, last);
7189 if (constructor_max_index != 0
7190 && tree_int_cst_lt (constructor_max_index, last))
7191 {
7192 error_init ("array index range in initializer exceeds array bounds");
7193 last = 0;
7194 }
7195 }
7196 }
7197
7198 designator_depth++;
7199 designator_erroneous = 0;
7200 if (constructor_range_stack || last)
7201 push_range_stack (last, braced_init_obstack);
7202 }
7203 }
7204
7205 /* Within a struct initializer, specify the next field to be initialized. */
7206
7207 void
7208 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7209 {
7210 tree field;
7211
7212 if (set_designator (0, braced_init_obstack))
7213 return;
7214
7215 designator_erroneous = 1;
7216
7217 if (TREE_CODE (constructor_type) != RECORD_TYPE
7218 && TREE_CODE (constructor_type) != UNION_TYPE)
7219 {
7220 error_init ("field name not in record or union initializer");
7221 return;
7222 }
7223
7224 field = lookup_field (constructor_type, fieldname);
7225
7226 if (field == 0)
7227 error ("unknown field %qE specified in initializer", fieldname);
7228 else
7229 do
7230 {
7231 constructor_fields = TREE_VALUE (field);
7232 designator_depth++;
7233 designator_erroneous = 0;
7234 if (constructor_range_stack)
7235 push_range_stack (NULL_TREE, braced_init_obstack);
7236 field = TREE_CHAIN (field);
7237 if (field)
7238 {
7239 if (set_designator (0, braced_init_obstack))
7240 return;
7241 }
7242 }
7243 while (field != NULL_TREE);
7244 }
7245 \f
7246 /* Add a new initializer to the tree of pending initializers. PURPOSE
7247 identifies the initializer, either array index or field in a structure.
7248 VALUE is the value of that index or field. If ORIGTYPE is not
7249 NULL_TREE, it is the original type of VALUE.
7250
7251 IMPLICIT is true if value comes from pop_init_level (1),
7252 the new initializer has been merged with the existing one
7253 and thus no warnings should be emitted about overriding an
7254 existing initializer. */
7255
7256 static void
7257 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7258 struct obstack * braced_init_obstack)
7259 {
7260 struct init_node *p, **q, *r;
7261
7262 q = &constructor_pending_elts;
7263 p = 0;
7264
7265 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7266 {
7267 while (*q != 0)
7268 {
7269 p = *q;
7270 if (tree_int_cst_lt (purpose, p->purpose))
7271 q = &p->left;
7272 else if (tree_int_cst_lt (p->purpose, purpose))
7273 q = &p->right;
7274 else
7275 {
7276 if (!implicit)
7277 {
7278 if (TREE_SIDE_EFFECTS (p->value))
7279 warning_init (0, "initialized field with side-effects overwritten");
7280 else if (warn_override_init)
7281 warning_init (OPT_Woverride_init, "initialized field overwritten");
7282 }
7283 p->value = value;
7284 p->origtype = origtype;
7285 return;
7286 }
7287 }
7288 }
7289 else
7290 {
7291 tree bitpos;
7292
7293 bitpos = bit_position (purpose);
7294 while (*q != NULL)
7295 {
7296 p = *q;
7297 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7298 q = &p->left;
7299 else if (p->purpose != purpose)
7300 q = &p->right;
7301 else
7302 {
7303 if (!implicit)
7304 {
7305 if (TREE_SIDE_EFFECTS (p->value))
7306 warning_init (0, "initialized field with side-effects overwritten");
7307 else if (warn_override_init)
7308 warning_init (OPT_Woverride_init, "initialized field overwritten");
7309 }
7310 p->value = value;
7311 p->origtype = origtype;
7312 return;
7313 }
7314 }
7315 }
7316
7317 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7318 sizeof (struct init_node));
7319 r->purpose = purpose;
7320 r->value = value;
7321 r->origtype = origtype;
7322
7323 *q = r;
7324 r->parent = p;
7325 r->left = 0;
7326 r->right = 0;
7327 r->balance = 0;
7328
7329 while (p)
7330 {
7331 struct init_node *s;
7332
7333 if (r == p->left)
7334 {
7335 if (p->balance == 0)
7336 p->balance = -1;
7337 else if (p->balance < 0)
7338 {
7339 if (r->balance < 0)
7340 {
7341 /* L rotation. */
7342 p->left = r->right;
7343 if (p->left)
7344 p->left->parent = p;
7345 r->right = p;
7346
7347 p->balance = 0;
7348 r->balance = 0;
7349
7350 s = p->parent;
7351 p->parent = r;
7352 r->parent = s;
7353 if (s)
7354 {
7355 if (s->left == p)
7356 s->left = r;
7357 else
7358 s->right = r;
7359 }
7360 else
7361 constructor_pending_elts = r;
7362 }
7363 else
7364 {
7365 /* LR rotation. */
7366 struct init_node *t = r->right;
7367
7368 r->right = t->left;
7369 if (r->right)
7370 r->right->parent = r;
7371 t->left = r;
7372
7373 p->left = t->right;
7374 if (p->left)
7375 p->left->parent = p;
7376 t->right = p;
7377
7378 p->balance = t->balance < 0;
7379 r->balance = -(t->balance > 0);
7380 t->balance = 0;
7381
7382 s = p->parent;
7383 p->parent = t;
7384 r->parent = t;
7385 t->parent = s;
7386 if (s)
7387 {
7388 if (s->left == p)
7389 s->left = t;
7390 else
7391 s->right = t;
7392 }
7393 else
7394 constructor_pending_elts = t;
7395 }
7396 break;
7397 }
7398 else
7399 {
7400 /* p->balance == +1; growth of left side balances the node. */
7401 p->balance = 0;
7402 break;
7403 }
7404 }
7405 else /* r == p->right */
7406 {
7407 if (p->balance == 0)
7408 /* Growth propagation from right side. */
7409 p->balance++;
7410 else if (p->balance > 0)
7411 {
7412 if (r->balance > 0)
7413 {
7414 /* R rotation. */
7415 p->right = r->left;
7416 if (p->right)
7417 p->right->parent = p;
7418 r->left = p;
7419
7420 p->balance = 0;
7421 r->balance = 0;
7422
7423 s = p->parent;
7424 p->parent = r;
7425 r->parent = s;
7426 if (s)
7427 {
7428 if (s->left == p)
7429 s->left = r;
7430 else
7431 s->right = r;
7432 }
7433 else
7434 constructor_pending_elts = r;
7435 }
7436 else /* r->balance == -1 */
7437 {
7438 /* RL rotation */
7439 struct init_node *t = r->left;
7440
7441 r->left = t->right;
7442 if (r->left)
7443 r->left->parent = r;
7444 t->right = r;
7445
7446 p->right = t->left;
7447 if (p->right)
7448 p->right->parent = p;
7449 t->left = p;
7450
7451 r->balance = (t->balance < 0);
7452 p->balance = -(t->balance > 0);
7453 t->balance = 0;
7454
7455 s = p->parent;
7456 p->parent = t;
7457 r->parent = t;
7458 t->parent = s;
7459 if (s)
7460 {
7461 if (s->left == p)
7462 s->left = t;
7463 else
7464 s->right = t;
7465 }
7466 else
7467 constructor_pending_elts = t;
7468 }
7469 break;
7470 }
7471 else
7472 {
7473 /* p->balance == -1; growth of right side balances the node. */
7474 p->balance = 0;
7475 break;
7476 }
7477 }
7478
7479 r = p;
7480 p = p->parent;
7481 }
7482 }
7483
7484 /* Build AVL tree from a sorted chain. */
7485
7486 static void
7487 set_nonincremental_init (struct obstack * braced_init_obstack)
7488 {
7489 unsigned HOST_WIDE_INT ix;
7490 tree index, value;
7491
7492 if (TREE_CODE (constructor_type) != RECORD_TYPE
7493 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7494 return;
7495
7496 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7497 {
7498 add_pending_init (index, value, NULL_TREE, false,
7499 braced_init_obstack);
7500 }
7501 constructor_elements = 0;
7502 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7503 {
7504 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7505 /* Skip any nameless bit fields at the beginning. */
7506 while (constructor_unfilled_fields != 0
7507 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7508 && DECL_NAME (constructor_unfilled_fields) == 0)
7509 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7510
7511 }
7512 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7513 {
7514 if (TYPE_DOMAIN (constructor_type))
7515 constructor_unfilled_index
7516 = convert (bitsizetype,
7517 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7518 else
7519 constructor_unfilled_index = bitsize_zero_node;
7520 }
7521 constructor_incremental = 0;
7522 }
7523
7524 /* Build AVL tree from a string constant. */
7525
7526 static void
7527 set_nonincremental_init_from_string (tree str,
7528 struct obstack * braced_init_obstack)
7529 {
7530 tree value, purpose, type;
7531 HOST_WIDE_INT val[2];
7532 const char *p, *end;
7533 int byte, wchar_bytes, charwidth, bitpos;
7534
7535 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7536
7537 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7538 charwidth = TYPE_PRECISION (char_type_node);
7539 type = TREE_TYPE (constructor_type);
7540 p = TREE_STRING_POINTER (str);
7541 end = p + TREE_STRING_LENGTH (str);
7542
7543 for (purpose = bitsize_zero_node;
7544 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7545 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7546 {
7547 if (wchar_bytes == 1)
7548 {
7549 val[1] = (unsigned char) *p++;
7550 val[0] = 0;
7551 }
7552 else
7553 {
7554 val[0] = 0;
7555 val[1] = 0;
7556 for (byte = 0; byte < wchar_bytes; byte++)
7557 {
7558 if (BYTES_BIG_ENDIAN)
7559 bitpos = (wchar_bytes - byte - 1) * charwidth;
7560 else
7561 bitpos = byte * charwidth;
7562 val[bitpos < HOST_BITS_PER_WIDE_INT]
7563 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7564 << (bitpos % HOST_BITS_PER_WIDE_INT);
7565 }
7566 }
7567
7568 if (!TYPE_UNSIGNED (type))
7569 {
7570 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7571 if (bitpos < HOST_BITS_PER_WIDE_INT)
7572 {
7573 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7574 {
7575 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7576 val[0] = -1;
7577 }
7578 }
7579 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7580 {
7581 if (val[1] < 0)
7582 val[0] = -1;
7583 }
7584 else if (val[0] & (((HOST_WIDE_INT) 1)
7585 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7586 val[0] |= ((HOST_WIDE_INT) -1)
7587 << (bitpos - HOST_BITS_PER_WIDE_INT);
7588 }
7589
7590 value = build_int_cst_wide (type, val[1], val[0]);
7591 add_pending_init (purpose, value, NULL_TREE, false,
7592 braced_init_obstack);
7593 }
7594
7595 constructor_incremental = 0;
7596 }
7597
7598 /* Return value of FIELD in pending initializer or zero if the field was
7599 not initialized yet. */
7600
7601 static tree
7602 find_init_member (tree field, struct obstack * braced_init_obstack)
7603 {
7604 struct init_node *p;
7605
7606 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7607 {
7608 if (constructor_incremental
7609 && tree_int_cst_lt (field, constructor_unfilled_index))
7610 set_nonincremental_init (braced_init_obstack);
7611
7612 p = constructor_pending_elts;
7613 while (p)
7614 {
7615 if (tree_int_cst_lt (field, p->purpose))
7616 p = p->left;
7617 else if (tree_int_cst_lt (p->purpose, field))
7618 p = p->right;
7619 else
7620 return p->value;
7621 }
7622 }
7623 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7624 {
7625 tree bitpos = bit_position (field);
7626
7627 if (constructor_incremental
7628 && (!constructor_unfilled_fields
7629 || tree_int_cst_lt (bitpos,
7630 bit_position (constructor_unfilled_fields))))
7631 set_nonincremental_init (braced_init_obstack);
7632
7633 p = constructor_pending_elts;
7634 while (p)
7635 {
7636 if (field == p->purpose)
7637 return p->value;
7638 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7639 p = p->left;
7640 else
7641 p = p->right;
7642 }
7643 }
7644 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7645 {
7646 if (!VEC_empty (constructor_elt, constructor_elements)
7647 && (VEC_last (constructor_elt, constructor_elements)->index
7648 == field))
7649 return VEC_last (constructor_elt, constructor_elements)->value;
7650 }
7651 return 0;
7652 }
7653
7654 /* "Output" the next constructor element.
7655 At top level, really output it to assembler code now.
7656 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7657 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7658 TYPE is the data type that the containing data type wants here.
7659 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7660 If VALUE is a string constant, STRICT_STRING is true if it is
7661 unparenthesized or we should not warn here for it being parenthesized.
7662 For other types of VALUE, STRICT_STRING is not used.
7663
7664 PENDING if non-nil means output pending elements that belong
7665 right after this element. (PENDING is normally 1;
7666 it is 0 while outputting pending elements, to avoid recursion.)
7667
7668 IMPLICIT is true if value comes from pop_init_level (1),
7669 the new initializer has been merged with the existing one
7670 and thus no warnings should be emitted about overriding an
7671 existing initializer. */
7672
7673 static void
7674 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7675 tree field, int pending, bool implicit,
7676 struct obstack * braced_init_obstack)
7677 {
7678 tree semantic_type = NULL_TREE;
7679 constructor_elt *celt;
7680 bool maybe_const = true;
7681 bool npc;
7682
7683 if (type == error_mark_node || value == error_mark_node)
7684 {
7685 constructor_erroneous = 1;
7686 return;
7687 }
7688 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7689 && (TREE_CODE (value) == STRING_CST
7690 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7691 && !(TREE_CODE (value) == STRING_CST
7692 && TREE_CODE (type) == ARRAY_TYPE
7693 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7694 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7695 TYPE_MAIN_VARIANT (type)))
7696 value = array_to_pointer_conversion (input_location, value);
7697
7698 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7699 && require_constant_value && !flag_isoc99 && pending)
7700 {
7701 /* As an extension, allow initializing objects with static storage
7702 duration with compound literals (which are then treated just as
7703 the brace enclosed list they contain). */
7704 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7705 value = DECL_INITIAL (decl);
7706 }
7707
7708 npc = null_pointer_constant_p (value);
7709 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7710 {
7711 semantic_type = TREE_TYPE (value);
7712 value = TREE_OPERAND (value, 0);
7713 }
7714 value = c_fully_fold (value, require_constant_value, &maybe_const);
7715
7716 if (value == error_mark_node)
7717 constructor_erroneous = 1;
7718 else if (!TREE_CONSTANT (value))
7719 constructor_constant = 0;
7720 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7721 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7722 || TREE_CODE (constructor_type) == UNION_TYPE)
7723 && DECL_C_BIT_FIELD (field)
7724 && TREE_CODE (value) != INTEGER_CST))
7725 constructor_simple = 0;
7726 if (!maybe_const)
7727 constructor_nonconst = 1;
7728
7729 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7730 {
7731 if (require_constant_value)
7732 {
7733 error_init ("initializer element is not constant");
7734 value = error_mark_node;
7735 }
7736 else if (require_constant_elements)
7737 pedwarn (input_location, 0,
7738 "initializer element is not computable at load time");
7739 }
7740 else if (!maybe_const
7741 && (require_constant_value || require_constant_elements))
7742 pedwarn_init (input_location, 0,
7743 "initializer element is not a constant expression");
7744
7745 /* Issue -Wc++-compat warnings about initializing a bitfield with
7746 enum type. */
7747 if (warn_cxx_compat
7748 && field != NULL_TREE
7749 && TREE_CODE (field) == FIELD_DECL
7750 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7751 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7752 != TYPE_MAIN_VARIANT (type))
7753 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7754 {
7755 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7756 if (checktype != error_mark_node
7757 && (TYPE_MAIN_VARIANT (checktype)
7758 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7759 warning_init (OPT_Wc___compat,
7760 "enum conversion in initialization is invalid in C++");
7761 }
7762
7763 /* If this field is empty (and not at the end of structure),
7764 don't do anything other than checking the initializer. */
7765 if (field
7766 && (TREE_TYPE (field) == error_mark_node
7767 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7768 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7769 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7770 || DECL_CHAIN (field)))))
7771 return;
7772
7773 if (semantic_type)
7774 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7775 value = digest_init (input_location, type, value, origtype, npc,
7776 strict_string, require_constant_value);
7777 if (value == error_mark_node)
7778 {
7779 constructor_erroneous = 1;
7780 return;
7781 }
7782 if (require_constant_value || require_constant_elements)
7783 constant_expression_warning (value);
7784
7785 /* If this element doesn't come next in sequence,
7786 put it on constructor_pending_elts. */
7787 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7788 && (!constructor_incremental
7789 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7790 {
7791 if (constructor_incremental
7792 && tree_int_cst_lt (field, constructor_unfilled_index))
7793 set_nonincremental_init (braced_init_obstack);
7794
7795 add_pending_init (field, value, origtype, implicit,
7796 braced_init_obstack);
7797 return;
7798 }
7799 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7800 && (!constructor_incremental
7801 || field != constructor_unfilled_fields))
7802 {
7803 /* We do this for records but not for unions. In a union,
7804 no matter which field is specified, it can be initialized
7805 right away since it starts at the beginning of the union. */
7806 if (constructor_incremental)
7807 {
7808 if (!constructor_unfilled_fields)
7809 set_nonincremental_init (braced_init_obstack);
7810 else
7811 {
7812 tree bitpos, unfillpos;
7813
7814 bitpos = bit_position (field);
7815 unfillpos = bit_position (constructor_unfilled_fields);
7816
7817 if (tree_int_cst_lt (bitpos, unfillpos))
7818 set_nonincremental_init (braced_init_obstack);
7819 }
7820 }
7821
7822 add_pending_init (field, value, origtype, implicit,
7823 braced_init_obstack);
7824 return;
7825 }
7826 else if (TREE_CODE (constructor_type) == UNION_TYPE
7827 && !VEC_empty (constructor_elt, constructor_elements))
7828 {
7829 if (!implicit)
7830 {
7831 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7832 constructor_elements)->value))
7833 warning_init (0,
7834 "initialized field with side-effects overwritten");
7835 else if (warn_override_init)
7836 warning_init (OPT_Woverride_init, "initialized field overwritten");
7837 }
7838
7839 /* We can have just one union field set. */
7840 constructor_elements = 0;
7841 }
7842
7843 /* Otherwise, output this element either to
7844 constructor_elements or to the assembler file. */
7845
7846 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7847 celt->index = field;
7848 celt->value = value;
7849
7850 /* Advance the variable that indicates sequential elements output. */
7851 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7852 constructor_unfilled_index
7853 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7854 bitsize_one_node);
7855 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7856 {
7857 constructor_unfilled_fields
7858 = DECL_CHAIN (constructor_unfilled_fields);
7859
7860 /* Skip any nameless bit fields. */
7861 while (constructor_unfilled_fields != 0
7862 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7863 && DECL_NAME (constructor_unfilled_fields) == 0)
7864 constructor_unfilled_fields =
7865 DECL_CHAIN (constructor_unfilled_fields);
7866 }
7867 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7868 constructor_unfilled_fields = 0;
7869
7870 /* Now output any pending elements which have become next. */
7871 if (pending)
7872 output_pending_init_elements (0, braced_init_obstack);
7873 }
7874
7875 /* Output any pending elements which have become next.
7876 As we output elements, constructor_unfilled_{fields,index}
7877 advances, which may cause other elements to become next;
7878 if so, they too are output.
7879
7880 If ALL is 0, we return when there are
7881 no more pending elements to output now.
7882
7883 If ALL is 1, we output space as necessary so that
7884 we can output all the pending elements. */
7885 static void
7886 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7887 {
7888 struct init_node *elt = constructor_pending_elts;
7889 tree next;
7890
7891 retry:
7892
7893 /* Look through the whole pending tree.
7894 If we find an element that should be output now,
7895 output it. Otherwise, set NEXT to the element
7896 that comes first among those still pending. */
7897
7898 next = 0;
7899 while (elt)
7900 {
7901 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7902 {
7903 if (tree_int_cst_equal (elt->purpose,
7904 constructor_unfilled_index))
7905 output_init_element (elt->value, elt->origtype, true,
7906 TREE_TYPE (constructor_type),
7907 constructor_unfilled_index, 0, false,
7908 braced_init_obstack);
7909 else if (tree_int_cst_lt (constructor_unfilled_index,
7910 elt->purpose))
7911 {
7912 /* Advance to the next smaller node. */
7913 if (elt->left)
7914 elt = elt->left;
7915 else
7916 {
7917 /* We have reached the smallest node bigger than the
7918 current unfilled index. Fill the space first. */
7919 next = elt->purpose;
7920 break;
7921 }
7922 }
7923 else
7924 {
7925 /* Advance to the next bigger node. */
7926 if (elt->right)
7927 elt = elt->right;
7928 else
7929 {
7930 /* We have reached the biggest node in a subtree. Find
7931 the parent of it, which is the next bigger node. */
7932 while (elt->parent && elt->parent->right == elt)
7933 elt = elt->parent;
7934 elt = elt->parent;
7935 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7936 elt->purpose))
7937 {
7938 next = elt->purpose;
7939 break;
7940 }
7941 }
7942 }
7943 }
7944 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7945 || TREE_CODE (constructor_type) == UNION_TYPE)
7946 {
7947 tree ctor_unfilled_bitpos, elt_bitpos;
7948
7949 /* If the current record is complete we are done. */
7950 if (constructor_unfilled_fields == 0)
7951 break;
7952
7953 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7954 elt_bitpos = bit_position (elt->purpose);
7955 /* We can't compare fields here because there might be empty
7956 fields in between. */
7957 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7958 {
7959 constructor_unfilled_fields = elt->purpose;
7960 output_init_element (elt->value, elt->origtype, true,
7961 TREE_TYPE (elt->purpose),
7962 elt->purpose, 0, false,
7963 braced_init_obstack);
7964 }
7965 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7966 {
7967 /* Advance to the next smaller node. */
7968 if (elt->left)
7969 elt = elt->left;
7970 else
7971 {
7972 /* We have reached the smallest node bigger than the
7973 current unfilled field. Fill the space first. */
7974 next = elt->purpose;
7975 break;
7976 }
7977 }
7978 else
7979 {
7980 /* Advance to the next bigger node. */
7981 if (elt->right)
7982 elt = elt->right;
7983 else
7984 {
7985 /* We have reached the biggest node in a subtree. Find
7986 the parent of it, which is the next bigger node. */
7987 while (elt->parent && elt->parent->right == elt)
7988 elt = elt->parent;
7989 elt = elt->parent;
7990 if (elt
7991 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7992 bit_position (elt->purpose))))
7993 {
7994 next = elt->purpose;
7995 break;
7996 }
7997 }
7998 }
7999 }
8000 }
8001
8002 /* Ordinarily return, but not if we want to output all
8003 and there are elements left. */
8004 if (!(all && next != 0))
8005 return;
8006
8007 /* If it's not incremental, just skip over the gap, so that after
8008 jumping to retry we will output the next successive element. */
8009 if (TREE_CODE (constructor_type) == RECORD_TYPE
8010 || TREE_CODE (constructor_type) == UNION_TYPE)
8011 constructor_unfilled_fields = next;
8012 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8013 constructor_unfilled_index = next;
8014
8015 /* ELT now points to the node in the pending tree with the next
8016 initializer to output. */
8017 goto retry;
8018 }
8019 \f
8020 /* Add one non-braced element to the current constructor level.
8021 This adjusts the current position within the constructor's type.
8022 This may also start or terminate implicit levels
8023 to handle a partly-braced initializer.
8024
8025 Once this has found the correct level for the new element,
8026 it calls output_init_element.
8027
8028 IMPLICIT is true if value comes from pop_init_level (1),
8029 the new initializer has been merged with the existing one
8030 and thus no warnings should be emitted about overriding an
8031 existing initializer. */
8032
8033 void
8034 process_init_element (struct c_expr value, bool implicit,
8035 struct obstack * braced_init_obstack)
8036 {
8037 tree orig_value = value.value;
8038 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8039 bool strict_string = value.original_code == STRING_CST;
8040
8041 designator_depth = 0;
8042 designator_erroneous = 0;
8043
8044 /* Handle superfluous braces around string cst as in
8045 char x[] = {"foo"}; */
8046 if (string_flag
8047 && constructor_type
8048 && TREE_CODE (constructor_type) == ARRAY_TYPE
8049 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8050 && integer_zerop (constructor_unfilled_index))
8051 {
8052 if (constructor_stack->replacement_value.value)
8053 error_init ("excess elements in char array initializer");
8054 constructor_stack->replacement_value = value;
8055 return;
8056 }
8057
8058 if (constructor_stack->replacement_value.value != 0)
8059 {
8060 error_init ("excess elements in struct initializer");
8061 return;
8062 }
8063
8064 /* Ignore elements of a brace group if it is entirely superfluous
8065 and has already been diagnosed. */
8066 if (constructor_type == 0)
8067 return;
8068
8069 /* If we've exhausted any levels that didn't have braces,
8070 pop them now. */
8071 while (constructor_stack->implicit)
8072 {
8073 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8074 || TREE_CODE (constructor_type) == UNION_TYPE)
8075 && constructor_fields == 0)
8076 process_init_element (pop_init_level (1, braced_init_obstack),
8077 true, braced_init_obstack);
8078 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8079 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8080 && (constructor_max_index == 0
8081 || tree_int_cst_lt (constructor_max_index,
8082 constructor_index)))
8083 process_init_element (pop_init_level (1, braced_init_obstack),
8084 true, braced_init_obstack);
8085 else
8086 break;
8087 }
8088
8089 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8090 if (constructor_range_stack)
8091 {
8092 /* If value is a compound literal and we'll be just using its
8093 content, don't put it into a SAVE_EXPR. */
8094 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8095 || !require_constant_value
8096 || flag_isoc99)
8097 {
8098 tree semantic_type = NULL_TREE;
8099 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8100 {
8101 semantic_type = TREE_TYPE (value.value);
8102 value.value = TREE_OPERAND (value.value, 0);
8103 }
8104 value.value = c_save_expr (value.value);
8105 if (semantic_type)
8106 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8107 value.value);
8108 }
8109 }
8110
8111 while (1)
8112 {
8113 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8114 {
8115 tree fieldtype;
8116 enum tree_code fieldcode;
8117
8118 if (constructor_fields == 0)
8119 {
8120 pedwarn_init (input_location, 0,
8121 "excess elements in struct initializer");
8122 break;
8123 }
8124
8125 fieldtype = TREE_TYPE (constructor_fields);
8126 if (fieldtype != error_mark_node)
8127 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8128 fieldcode = TREE_CODE (fieldtype);
8129
8130 /* Error for non-static initialization of a flexible array member. */
8131 if (fieldcode == ARRAY_TYPE
8132 && !require_constant_value
8133 && TYPE_SIZE (fieldtype) == NULL_TREE
8134 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8135 {
8136 error_init ("non-static initialization of a flexible array member");
8137 break;
8138 }
8139
8140 /* Accept a string constant to initialize a subarray. */
8141 if (value.value != 0
8142 && fieldcode == ARRAY_TYPE
8143 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8144 && string_flag)
8145 value.value = orig_value;
8146 /* Otherwise, if we have come to a subaggregate,
8147 and we don't have an element of its type, push into it. */
8148 else if (value.value != 0
8149 && value.value != error_mark_node
8150 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8151 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8152 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8153 {
8154 push_init_level (1, braced_init_obstack);
8155 continue;
8156 }
8157
8158 if (value.value)
8159 {
8160 push_member_name (constructor_fields);
8161 output_init_element (value.value, value.original_type,
8162 strict_string, fieldtype,
8163 constructor_fields, 1, implicit,
8164 braced_init_obstack);
8165 RESTORE_SPELLING_DEPTH (constructor_depth);
8166 }
8167 else
8168 /* Do the bookkeeping for an element that was
8169 directly output as a constructor. */
8170 {
8171 /* For a record, keep track of end position of last field. */
8172 if (DECL_SIZE (constructor_fields))
8173 constructor_bit_index
8174 = size_binop_loc (input_location, PLUS_EXPR,
8175 bit_position (constructor_fields),
8176 DECL_SIZE (constructor_fields));
8177
8178 /* If the current field was the first one not yet written out,
8179 it isn't now, so update. */
8180 if (constructor_unfilled_fields == constructor_fields)
8181 {
8182 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8183 /* Skip any nameless bit fields. */
8184 while (constructor_unfilled_fields != 0
8185 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8186 && DECL_NAME (constructor_unfilled_fields) == 0)
8187 constructor_unfilled_fields =
8188 DECL_CHAIN (constructor_unfilled_fields);
8189 }
8190 }
8191
8192 constructor_fields = DECL_CHAIN (constructor_fields);
8193 /* Skip any nameless bit fields at the beginning. */
8194 while (constructor_fields != 0
8195 && DECL_C_BIT_FIELD (constructor_fields)
8196 && DECL_NAME (constructor_fields) == 0)
8197 constructor_fields = DECL_CHAIN (constructor_fields);
8198 }
8199 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8200 {
8201 tree fieldtype;
8202 enum tree_code fieldcode;
8203
8204 if (constructor_fields == 0)
8205 {
8206 pedwarn_init (input_location, 0,
8207 "excess elements in union initializer");
8208 break;
8209 }
8210
8211 fieldtype = TREE_TYPE (constructor_fields);
8212 if (fieldtype != error_mark_node)
8213 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8214 fieldcode = TREE_CODE (fieldtype);
8215
8216 /* Warn that traditional C rejects initialization of unions.
8217 We skip the warning if the value is zero. This is done
8218 under the assumption that the zero initializer in user
8219 code appears conditioned on e.g. __STDC__ to avoid
8220 "missing initializer" warnings and relies on default
8221 initialization to zero in the traditional C case.
8222 We also skip the warning if the initializer is designated,
8223 again on the assumption that this must be conditional on
8224 __STDC__ anyway (and we've already complained about the
8225 member-designator already). */
8226 if (!in_system_header && !constructor_designated
8227 && !(value.value && (integer_zerop (value.value)
8228 || real_zerop (value.value))))
8229 warning (OPT_Wtraditional, "traditional C rejects initialization "
8230 "of unions");
8231
8232 /* Accept a string constant to initialize a subarray. */
8233 if (value.value != 0
8234 && fieldcode == ARRAY_TYPE
8235 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8236 && string_flag)
8237 value.value = orig_value;
8238 /* Otherwise, if we have come to a subaggregate,
8239 and we don't have an element of its type, push into it. */
8240 else if (value.value != 0
8241 && value.value != error_mark_node
8242 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8243 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8244 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8245 {
8246 push_init_level (1, braced_init_obstack);
8247 continue;
8248 }
8249
8250 if (value.value)
8251 {
8252 push_member_name (constructor_fields);
8253 output_init_element (value.value, value.original_type,
8254 strict_string, fieldtype,
8255 constructor_fields, 1, implicit,
8256 braced_init_obstack);
8257 RESTORE_SPELLING_DEPTH (constructor_depth);
8258 }
8259 else
8260 /* Do the bookkeeping for an element that was
8261 directly output as a constructor. */
8262 {
8263 constructor_bit_index = DECL_SIZE (constructor_fields);
8264 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8265 }
8266
8267 constructor_fields = 0;
8268 }
8269 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8270 {
8271 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8272 enum tree_code eltcode = TREE_CODE (elttype);
8273
8274 /* Accept a string constant to initialize a subarray. */
8275 if (value.value != 0
8276 && eltcode == ARRAY_TYPE
8277 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8278 && string_flag)
8279 value.value = orig_value;
8280 /* Otherwise, if we have come to a subaggregate,
8281 and we don't have an element of its type, push into it. */
8282 else if (value.value != 0
8283 && value.value != error_mark_node
8284 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8285 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8286 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8287 {
8288 push_init_level (1, braced_init_obstack);
8289 continue;
8290 }
8291
8292 if (constructor_max_index != 0
8293 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8294 || integer_all_onesp (constructor_max_index)))
8295 {
8296 pedwarn_init (input_location, 0,
8297 "excess elements in array initializer");
8298 break;
8299 }
8300
8301 /* Now output the actual element. */
8302 if (value.value)
8303 {
8304 push_array_bounds (tree_low_cst (constructor_index, 1));
8305 output_init_element (value.value, value.original_type,
8306 strict_string, elttype,
8307 constructor_index, 1, implicit,
8308 braced_init_obstack);
8309 RESTORE_SPELLING_DEPTH (constructor_depth);
8310 }
8311
8312 constructor_index
8313 = size_binop_loc (input_location, PLUS_EXPR,
8314 constructor_index, bitsize_one_node);
8315
8316 if (!value.value)
8317 /* If we are doing the bookkeeping for an element that was
8318 directly output as a constructor, we must update
8319 constructor_unfilled_index. */
8320 constructor_unfilled_index = constructor_index;
8321 }
8322 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8323 {
8324 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8325
8326 /* Do a basic check of initializer size. Note that vectors
8327 always have a fixed size derived from their type. */
8328 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8329 {
8330 pedwarn_init (input_location, 0,
8331 "excess elements in vector initializer");
8332 break;
8333 }
8334
8335 /* Now output the actual element. */
8336 if (value.value)
8337 {
8338 if (TREE_CODE (value.value) == VECTOR_CST)
8339 elttype = TYPE_MAIN_VARIANT (constructor_type);
8340 output_init_element (value.value, value.original_type,
8341 strict_string, elttype,
8342 constructor_index, 1, implicit,
8343 braced_init_obstack);
8344 }
8345
8346 constructor_index
8347 = size_binop_loc (input_location,
8348 PLUS_EXPR, constructor_index, bitsize_one_node);
8349
8350 if (!value.value)
8351 /* If we are doing the bookkeeping for an element that was
8352 directly output as a constructor, we must update
8353 constructor_unfilled_index. */
8354 constructor_unfilled_index = constructor_index;
8355 }
8356
8357 /* Handle the sole element allowed in a braced initializer
8358 for a scalar variable. */
8359 else if (constructor_type != error_mark_node
8360 && constructor_fields == 0)
8361 {
8362 pedwarn_init (input_location, 0,
8363 "excess elements in scalar initializer");
8364 break;
8365 }
8366 else
8367 {
8368 if (value.value)
8369 output_init_element (value.value, value.original_type,
8370 strict_string, constructor_type,
8371 NULL_TREE, 1, implicit,
8372 braced_init_obstack);
8373 constructor_fields = 0;
8374 }
8375
8376 /* Handle range initializers either at this level or anywhere higher
8377 in the designator stack. */
8378 if (constructor_range_stack)
8379 {
8380 struct constructor_range_stack *p, *range_stack;
8381 int finish = 0;
8382
8383 range_stack = constructor_range_stack;
8384 constructor_range_stack = 0;
8385 while (constructor_stack != range_stack->stack)
8386 {
8387 gcc_assert (constructor_stack->implicit);
8388 process_init_element (pop_init_level (1,
8389 braced_init_obstack),
8390 true, braced_init_obstack);
8391 }
8392 for (p = range_stack;
8393 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8394 p = p->prev)
8395 {
8396 gcc_assert (constructor_stack->implicit);
8397 process_init_element (pop_init_level (1, braced_init_obstack),
8398 true, braced_init_obstack);
8399 }
8400
8401 p->index = size_binop_loc (input_location,
8402 PLUS_EXPR, p->index, bitsize_one_node);
8403 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8404 finish = 1;
8405
8406 while (1)
8407 {
8408 constructor_index = p->index;
8409 constructor_fields = p->fields;
8410 if (finish && p->range_end && p->index == p->range_start)
8411 {
8412 finish = 0;
8413 p->prev = 0;
8414 }
8415 p = p->next;
8416 if (!p)
8417 break;
8418 push_init_level (2, braced_init_obstack);
8419 p->stack = constructor_stack;
8420 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8421 p->index = p->range_start;
8422 }
8423
8424 if (!finish)
8425 constructor_range_stack = range_stack;
8426 continue;
8427 }
8428
8429 break;
8430 }
8431
8432 constructor_range_stack = 0;
8433 }
8434 \f
8435 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8436 (guaranteed to be 'volatile' or null) and ARGS (represented using
8437 an ASM_EXPR node). */
8438 tree
8439 build_asm_stmt (tree cv_qualifier, tree args)
8440 {
8441 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8442 ASM_VOLATILE_P (args) = 1;
8443 return add_stmt (args);
8444 }
8445
8446 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8447 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8448 SIMPLE indicates whether there was anything at all after the
8449 string in the asm expression -- asm("blah") and asm("blah" : )
8450 are subtly different. We use a ASM_EXPR node to represent this. */
8451 tree
8452 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8453 tree clobbers, tree labels, bool simple)
8454 {
8455 tree tail;
8456 tree args;
8457 int i;
8458 const char *constraint;
8459 const char **oconstraints;
8460 bool allows_mem, allows_reg, is_inout;
8461 int ninputs, noutputs;
8462
8463 ninputs = list_length (inputs);
8464 noutputs = list_length (outputs);
8465 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8466
8467 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8468
8469 /* Remove output conversions that change the type but not the mode. */
8470 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8471 {
8472 tree output = TREE_VALUE (tail);
8473
8474 /* ??? Really, this should not be here. Users should be using a
8475 proper lvalue, dammit. But there's a long history of using casts
8476 in the output operands. In cases like longlong.h, this becomes a
8477 primitive form of typechecking -- if the cast can be removed, then
8478 the output operand had a type of the proper width; otherwise we'll
8479 get an error. Gross, but ... */
8480 STRIP_NOPS (output);
8481
8482 if (!lvalue_or_else (loc, output, lv_asm))
8483 output = error_mark_node;
8484
8485 if (output != error_mark_node
8486 && (TREE_READONLY (output)
8487 || TYPE_READONLY (TREE_TYPE (output))
8488 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8489 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8490 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8491 readonly_error (output, lv_asm);
8492
8493 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8494 oconstraints[i] = constraint;
8495
8496 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8497 &allows_mem, &allows_reg, &is_inout))
8498 {
8499 /* If the operand is going to end up in memory,
8500 mark it addressable. */
8501 if (!allows_reg && !c_mark_addressable (output))
8502 output = error_mark_node;
8503 if (!(!allows_reg && allows_mem)
8504 && output != error_mark_node
8505 && VOID_TYPE_P (TREE_TYPE (output)))
8506 {
8507 error_at (loc, "invalid use of void expression");
8508 output = error_mark_node;
8509 }
8510 }
8511 else
8512 output = error_mark_node;
8513
8514 TREE_VALUE (tail) = output;
8515 }
8516
8517 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8518 {
8519 tree input;
8520
8521 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8522 input = TREE_VALUE (tail);
8523
8524 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8525 oconstraints, &allows_mem, &allows_reg))
8526 {
8527 /* If the operand is going to end up in memory,
8528 mark it addressable. */
8529 if (!allows_reg && allows_mem)
8530 {
8531 /* Strip the nops as we allow this case. FIXME, this really
8532 should be rejected or made deprecated. */
8533 STRIP_NOPS (input);
8534 if (!c_mark_addressable (input))
8535 input = error_mark_node;
8536 }
8537 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8538 {
8539 error_at (loc, "invalid use of void expression");
8540 input = error_mark_node;
8541 }
8542 }
8543 else
8544 input = error_mark_node;
8545
8546 TREE_VALUE (tail) = input;
8547 }
8548
8549 /* ASMs with labels cannot have outputs. This should have been
8550 enforced by the parser. */
8551 gcc_assert (outputs == NULL || labels == NULL);
8552
8553 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8554
8555 /* asm statements without outputs, including simple ones, are treated
8556 as volatile. */
8557 ASM_INPUT_P (args) = simple;
8558 ASM_VOLATILE_P (args) = (noutputs == 0);
8559
8560 return args;
8561 }
8562 \f
8563 /* Generate a goto statement to LABEL. LOC is the location of the
8564 GOTO. */
8565
8566 tree
8567 c_finish_goto_label (location_t loc, tree label)
8568 {
8569 tree decl = lookup_label_for_goto (loc, label);
8570 if (!decl)
8571 return NULL_TREE;
8572 TREE_USED (decl) = 1;
8573 {
8574 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8575 SET_EXPR_LOCATION (t, loc);
8576 return add_stmt (t);
8577 }
8578 }
8579
8580 /* Generate a computed goto statement to EXPR. LOC is the location of
8581 the GOTO. */
8582
8583 tree
8584 c_finish_goto_ptr (location_t loc, tree expr)
8585 {
8586 tree t;
8587 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8588 expr = c_fully_fold (expr, false, NULL);
8589 expr = convert (ptr_type_node, expr);
8590 t = build1 (GOTO_EXPR, void_type_node, expr);
8591 SET_EXPR_LOCATION (t, loc);
8592 return add_stmt (t);
8593 }
8594
8595 /* Generate a C `return' statement. RETVAL is the expression for what
8596 to return, or a null pointer for `return;' with no value. LOC is
8597 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8598 is the original type of RETVAL. */
8599
8600 tree
8601 c_finish_return (location_t loc, tree retval, tree origtype)
8602 {
8603 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8604 bool no_warning = false;
8605 bool npc = false;
8606
8607 if (TREE_THIS_VOLATILE (current_function_decl))
8608 warning_at (loc, 0,
8609 "function declared %<noreturn%> has a %<return%> statement");
8610
8611 if (retval)
8612 {
8613 tree semantic_type = NULL_TREE;
8614 npc = null_pointer_constant_p (retval);
8615 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8616 {
8617 semantic_type = TREE_TYPE (retval);
8618 retval = TREE_OPERAND (retval, 0);
8619 }
8620 retval = c_fully_fold (retval, false, NULL);
8621 if (semantic_type)
8622 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8623 }
8624
8625 if (!retval)
8626 {
8627 current_function_returns_null = 1;
8628 if ((warn_return_type || flag_isoc99)
8629 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8630 {
8631 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8632 "%<return%> with no value, in "
8633 "function returning non-void");
8634 no_warning = true;
8635 }
8636 }
8637 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8638 {
8639 current_function_returns_null = 1;
8640 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8641 pedwarn (loc, 0,
8642 "%<return%> with a value, in function returning void");
8643 else
8644 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8645 "%<return%> with expression, in function returning void");
8646 }
8647 else
8648 {
8649 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8650 ic_return,
8651 npc, NULL_TREE, NULL_TREE, 0);
8652 tree res = DECL_RESULT (current_function_decl);
8653 tree inner;
8654
8655 current_function_returns_value = 1;
8656 if (t == error_mark_node)
8657 return NULL_TREE;
8658
8659 inner = t = convert (TREE_TYPE (res), t);
8660
8661 /* Strip any conversions, additions, and subtractions, and see if
8662 we are returning the address of a local variable. Warn if so. */
8663 while (1)
8664 {
8665 switch (TREE_CODE (inner))
8666 {
8667 CASE_CONVERT:
8668 case NON_LVALUE_EXPR:
8669 case PLUS_EXPR:
8670 case POINTER_PLUS_EXPR:
8671 inner = TREE_OPERAND (inner, 0);
8672 continue;
8673
8674 case MINUS_EXPR:
8675 /* If the second operand of the MINUS_EXPR has a pointer
8676 type (or is converted from it), this may be valid, so
8677 don't give a warning. */
8678 {
8679 tree op1 = TREE_OPERAND (inner, 1);
8680
8681 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8682 && (CONVERT_EXPR_P (op1)
8683 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8684 op1 = TREE_OPERAND (op1, 0);
8685
8686 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8687 break;
8688
8689 inner = TREE_OPERAND (inner, 0);
8690 continue;
8691 }
8692
8693 case ADDR_EXPR:
8694 inner = TREE_OPERAND (inner, 0);
8695
8696 while (REFERENCE_CLASS_P (inner)
8697 && TREE_CODE (inner) != INDIRECT_REF)
8698 inner = TREE_OPERAND (inner, 0);
8699
8700 if (DECL_P (inner)
8701 && !DECL_EXTERNAL (inner)
8702 && !TREE_STATIC (inner)
8703 && DECL_CONTEXT (inner) == current_function_decl)
8704 warning_at (loc,
8705 0, "function returns address of local variable");
8706 break;
8707
8708 default:
8709 break;
8710 }
8711
8712 break;
8713 }
8714
8715 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8716 SET_EXPR_LOCATION (retval, loc);
8717
8718 if (warn_sequence_point)
8719 verify_sequence_points (retval);
8720 }
8721
8722 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8723 TREE_NO_WARNING (ret_stmt) |= no_warning;
8724 return add_stmt (ret_stmt);
8725 }
8726 \f
8727 struct c_switch {
8728 /* The SWITCH_EXPR being built. */
8729 tree switch_expr;
8730
8731 /* The original type of the testing expression, i.e. before the
8732 default conversion is applied. */
8733 tree orig_type;
8734
8735 /* A splay-tree mapping the low element of a case range to the high
8736 element, or NULL_TREE if there is no high element. Used to
8737 determine whether or not a new case label duplicates an old case
8738 label. We need a tree, rather than simply a hash table, because
8739 of the GNU case range extension. */
8740 splay_tree cases;
8741
8742 /* The bindings at the point of the switch. This is used for
8743 warnings crossing decls when branching to a case label. */
8744 struct c_spot_bindings *bindings;
8745
8746 /* The next node on the stack. */
8747 struct c_switch *next;
8748 };
8749
8750 /* A stack of the currently active switch statements. The innermost
8751 switch statement is on the top of the stack. There is no need to
8752 mark the stack for garbage collection because it is only active
8753 during the processing of the body of a function, and we never
8754 collect at that point. */
8755
8756 struct c_switch *c_switch_stack;
8757
8758 /* Start a C switch statement, testing expression EXP. Return the new
8759 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8760 SWITCH_COND_LOC is the location of the switch's condition. */
8761
8762 tree
8763 c_start_case (location_t switch_loc,
8764 location_t switch_cond_loc,
8765 tree exp)
8766 {
8767 tree orig_type = error_mark_node;
8768 struct c_switch *cs;
8769
8770 if (exp != error_mark_node)
8771 {
8772 orig_type = TREE_TYPE (exp);
8773
8774 if (!INTEGRAL_TYPE_P (orig_type))
8775 {
8776 if (orig_type != error_mark_node)
8777 {
8778 error_at (switch_cond_loc, "switch quantity not an integer");
8779 orig_type = error_mark_node;
8780 }
8781 exp = integer_zero_node;
8782 }
8783 else
8784 {
8785 tree type = TYPE_MAIN_VARIANT (orig_type);
8786
8787 if (!in_system_header
8788 && (type == long_integer_type_node
8789 || type == long_unsigned_type_node))
8790 warning_at (switch_cond_loc,
8791 OPT_Wtraditional, "%<long%> switch expression not "
8792 "converted to %<int%> in ISO C");
8793
8794 exp = c_fully_fold (exp, false, NULL);
8795 exp = default_conversion (exp);
8796
8797 if (warn_sequence_point)
8798 verify_sequence_points (exp);
8799 }
8800 }
8801
8802 /* Add this new SWITCH_EXPR to the stack. */
8803 cs = XNEW (struct c_switch);
8804 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8805 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8806 cs->orig_type = orig_type;
8807 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8808 cs->bindings = c_get_switch_bindings ();
8809 cs->next = c_switch_stack;
8810 c_switch_stack = cs;
8811
8812 return add_stmt (cs->switch_expr);
8813 }
8814
8815 /* Process a case label at location LOC. */
8816
8817 tree
8818 do_case (location_t loc, tree low_value, tree high_value)
8819 {
8820 tree label = NULL_TREE;
8821
8822 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8823 {
8824 low_value = c_fully_fold (low_value, false, NULL);
8825 if (TREE_CODE (low_value) == INTEGER_CST)
8826 pedwarn (input_location, OPT_pedantic,
8827 "case label is not an integer constant expression");
8828 }
8829
8830 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8831 {
8832 high_value = c_fully_fold (high_value, false, NULL);
8833 if (TREE_CODE (high_value) == INTEGER_CST)
8834 pedwarn (input_location, OPT_pedantic,
8835 "case label is not an integer constant expression");
8836 }
8837
8838 if (c_switch_stack == NULL)
8839 {
8840 if (low_value)
8841 error_at (loc, "case label not within a switch statement");
8842 else
8843 error_at (loc, "%<default%> label not within a switch statement");
8844 return NULL_TREE;
8845 }
8846
8847 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8848 EXPR_LOCATION (c_switch_stack->switch_expr),
8849 loc))
8850 return NULL_TREE;
8851
8852 label = c_add_case_label (loc, c_switch_stack->cases,
8853 SWITCH_COND (c_switch_stack->switch_expr),
8854 c_switch_stack->orig_type,
8855 low_value, high_value);
8856 if (label == error_mark_node)
8857 label = NULL_TREE;
8858 return label;
8859 }
8860
8861 /* Finish the switch statement. */
8862
8863 void
8864 c_finish_case (tree body)
8865 {
8866 struct c_switch *cs = c_switch_stack;
8867 location_t switch_location;
8868
8869 SWITCH_BODY (cs->switch_expr) = body;
8870
8871 /* Emit warnings as needed. */
8872 switch_location = EXPR_LOCATION (cs->switch_expr);
8873 c_do_switch_warnings (cs->cases, switch_location,
8874 TREE_TYPE (cs->switch_expr),
8875 SWITCH_COND (cs->switch_expr));
8876
8877 /* Pop the stack. */
8878 c_switch_stack = cs->next;
8879 splay_tree_delete (cs->cases);
8880 c_release_switch_bindings (cs->bindings);
8881 XDELETE (cs);
8882 }
8883 \f
8884 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8885 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8886 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8887 statement, and was not surrounded with parenthesis. */
8888
8889 void
8890 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8891 tree else_block, bool nested_if)
8892 {
8893 tree stmt;
8894
8895 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8896 if (warn_parentheses && nested_if && else_block == NULL)
8897 {
8898 tree inner_if = then_block;
8899
8900 /* We know from the grammar productions that there is an IF nested
8901 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8902 it might not be exactly THEN_BLOCK, but should be the last
8903 non-container statement within. */
8904 while (1)
8905 switch (TREE_CODE (inner_if))
8906 {
8907 case COND_EXPR:
8908 goto found;
8909 case BIND_EXPR:
8910 inner_if = BIND_EXPR_BODY (inner_if);
8911 break;
8912 case STATEMENT_LIST:
8913 inner_if = expr_last (then_block);
8914 break;
8915 case TRY_FINALLY_EXPR:
8916 case TRY_CATCH_EXPR:
8917 inner_if = TREE_OPERAND (inner_if, 0);
8918 break;
8919 default:
8920 gcc_unreachable ();
8921 }
8922 found:
8923
8924 if (COND_EXPR_ELSE (inner_if))
8925 warning_at (if_locus, OPT_Wparentheses,
8926 "suggest explicit braces to avoid ambiguous %<else%>");
8927 }
8928
8929 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8930 SET_EXPR_LOCATION (stmt, if_locus);
8931 add_stmt (stmt);
8932 }
8933
8934 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8935 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8936 is false for DO loops. INCR is the FOR increment expression. BODY is
8937 the statement controlled by the loop. BLAB is the break label. CLAB is
8938 the continue label. Everything is allowed to be NULL. */
8939
8940 void
8941 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8942 tree blab, tree clab, bool cond_is_first)
8943 {
8944 tree entry = NULL, exit = NULL, t;
8945
8946 /* If the condition is zero don't generate a loop construct. */
8947 if (cond && integer_zerop (cond))
8948 {
8949 if (cond_is_first)
8950 {
8951 t = build_and_jump (&blab);
8952 SET_EXPR_LOCATION (t, start_locus);
8953 add_stmt (t);
8954 }
8955 }
8956 else
8957 {
8958 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8959
8960 /* If we have an exit condition, then we build an IF with gotos either
8961 out of the loop, or to the top of it. If there's no exit condition,
8962 then we just build a jump back to the top. */
8963 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8964
8965 if (cond && !integer_nonzerop (cond))
8966 {
8967 /* Canonicalize the loop condition to the end. This means
8968 generating a branch to the loop condition. Reuse the
8969 continue label, if possible. */
8970 if (cond_is_first)
8971 {
8972 if (incr || !clab)
8973 {
8974 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8975 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8976 }
8977 else
8978 t = build1 (GOTO_EXPR, void_type_node, clab);
8979 SET_EXPR_LOCATION (t, start_locus);
8980 add_stmt (t);
8981 }
8982
8983 t = build_and_jump (&blab);
8984 if (cond_is_first)
8985 exit = fold_build3_loc (start_locus,
8986 COND_EXPR, void_type_node, cond, exit, t);
8987 else
8988 exit = fold_build3_loc (input_location,
8989 COND_EXPR, void_type_node, cond, exit, t);
8990 }
8991
8992 add_stmt (top);
8993 }
8994
8995 if (body)
8996 add_stmt (body);
8997 if (clab)
8998 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8999 if (incr)
9000 add_stmt (incr);
9001 if (entry)
9002 add_stmt (entry);
9003 if (exit)
9004 add_stmt (exit);
9005 if (blab)
9006 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9007 }
9008
9009 tree
9010 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9011 {
9012 bool skip;
9013 tree label = *label_p;
9014
9015 /* In switch statements break is sometimes stylistically used after
9016 a return statement. This can lead to spurious warnings about
9017 control reaching the end of a non-void function when it is
9018 inlined. Note that we are calling block_may_fallthru with
9019 language specific tree nodes; this works because
9020 block_may_fallthru returns true when given something it does not
9021 understand. */
9022 skip = !block_may_fallthru (cur_stmt_list);
9023
9024 if (!label)
9025 {
9026 if (!skip)
9027 *label_p = label = create_artificial_label (loc);
9028 }
9029 else if (TREE_CODE (label) == LABEL_DECL)
9030 ;
9031 else switch (TREE_INT_CST_LOW (label))
9032 {
9033 case 0:
9034 if (is_break)
9035 error_at (loc, "break statement not within loop or switch");
9036 else
9037 error_at (loc, "continue statement not within a loop");
9038 return NULL_TREE;
9039
9040 case 1:
9041 gcc_assert (is_break);
9042 error_at (loc, "break statement used with OpenMP for loop");
9043 return NULL_TREE;
9044
9045 default:
9046 gcc_unreachable ();
9047 }
9048
9049 if (skip)
9050 return NULL_TREE;
9051
9052 if (!is_break)
9053 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9054
9055 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9056 }
9057
9058 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9059
9060 static void
9061 emit_side_effect_warnings (location_t loc, tree expr)
9062 {
9063 if (expr == error_mark_node)
9064 ;
9065 else if (!TREE_SIDE_EFFECTS (expr))
9066 {
9067 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9068 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9069 }
9070 else
9071 warn_if_unused_value (expr, loc);
9072 }
9073
9074 /* Process an expression as if it were a complete statement. Emit
9075 diagnostics, but do not call ADD_STMT. LOC is the location of the
9076 statement. */
9077
9078 tree
9079 c_process_expr_stmt (location_t loc, tree expr)
9080 {
9081 tree exprv;
9082
9083 if (!expr)
9084 return NULL_TREE;
9085
9086 expr = c_fully_fold (expr, false, NULL);
9087
9088 if (warn_sequence_point)
9089 verify_sequence_points (expr);
9090
9091 if (TREE_TYPE (expr) != error_mark_node
9092 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9093 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9094 error_at (loc, "expression statement has incomplete type");
9095
9096 /* If we're not processing a statement expression, warn about unused values.
9097 Warnings for statement expressions will be emitted later, once we figure
9098 out which is the result. */
9099 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9100 && warn_unused_value)
9101 emit_side_effect_warnings (loc, expr);
9102
9103 exprv = expr;
9104 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9105 exprv = TREE_OPERAND (exprv, 1);
9106 if (DECL_P (exprv) || handled_component_p (exprv))
9107 mark_exp_read (exprv);
9108
9109 /* If the expression is not of a type to which we cannot assign a line
9110 number, wrap the thing in a no-op NOP_EXPR. */
9111 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9112 {
9113 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9114 SET_EXPR_LOCATION (expr, loc);
9115 }
9116
9117 return expr;
9118 }
9119
9120 /* Emit an expression as a statement. LOC is the location of the
9121 expression. */
9122
9123 tree
9124 c_finish_expr_stmt (location_t loc, tree expr)
9125 {
9126 if (expr)
9127 return add_stmt (c_process_expr_stmt (loc, expr));
9128 else
9129 return NULL;
9130 }
9131
9132 /* Do the opposite and emit a statement as an expression. To begin,
9133 create a new binding level and return it. */
9134
9135 tree
9136 c_begin_stmt_expr (void)
9137 {
9138 tree ret;
9139
9140 /* We must force a BLOCK for this level so that, if it is not expanded
9141 later, there is a way to turn off the entire subtree of blocks that
9142 are contained in it. */
9143 keep_next_level ();
9144 ret = c_begin_compound_stmt (true);
9145
9146 c_bindings_start_stmt_expr (c_switch_stack == NULL
9147 ? NULL
9148 : c_switch_stack->bindings);
9149
9150 /* Mark the current statement list as belonging to a statement list. */
9151 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9152
9153 return ret;
9154 }
9155
9156 /* LOC is the location of the compound statement to which this body
9157 belongs. */
9158
9159 tree
9160 c_finish_stmt_expr (location_t loc, tree body)
9161 {
9162 tree last, type, tmp, val;
9163 tree *last_p;
9164
9165 body = c_end_compound_stmt (loc, body, true);
9166
9167 c_bindings_end_stmt_expr (c_switch_stack == NULL
9168 ? NULL
9169 : c_switch_stack->bindings);
9170
9171 /* Locate the last statement in BODY. See c_end_compound_stmt
9172 about always returning a BIND_EXPR. */
9173 last_p = &BIND_EXPR_BODY (body);
9174 last = BIND_EXPR_BODY (body);
9175
9176 continue_searching:
9177 if (TREE_CODE (last) == STATEMENT_LIST)
9178 {
9179 tree_stmt_iterator i;
9180
9181 /* This can happen with degenerate cases like ({ }). No value. */
9182 if (!TREE_SIDE_EFFECTS (last))
9183 return body;
9184
9185 /* If we're supposed to generate side effects warnings, process
9186 all of the statements except the last. */
9187 if (warn_unused_value)
9188 {
9189 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9190 {
9191 location_t tloc;
9192 tree t = tsi_stmt (i);
9193
9194 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9195 emit_side_effect_warnings (tloc, t);
9196 }
9197 }
9198 else
9199 i = tsi_last (last);
9200 last_p = tsi_stmt_ptr (i);
9201 last = *last_p;
9202 }
9203
9204 /* If the end of the list is exception related, then the list was split
9205 by a call to push_cleanup. Continue searching. */
9206 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9207 || TREE_CODE (last) == TRY_CATCH_EXPR)
9208 {
9209 last_p = &TREE_OPERAND (last, 0);
9210 last = *last_p;
9211 goto continue_searching;
9212 }
9213
9214 if (last == error_mark_node)
9215 return last;
9216
9217 /* In the case that the BIND_EXPR is not necessary, return the
9218 expression out from inside it. */
9219 if (last == BIND_EXPR_BODY (body)
9220 && BIND_EXPR_VARS (body) == NULL)
9221 {
9222 /* Even if this looks constant, do not allow it in a constant
9223 expression. */
9224 last = c_wrap_maybe_const (last, true);
9225 /* Do not warn if the return value of a statement expression is
9226 unused. */
9227 TREE_NO_WARNING (last) = 1;
9228 return last;
9229 }
9230
9231 /* Extract the type of said expression. */
9232 type = TREE_TYPE (last);
9233
9234 /* If we're not returning a value at all, then the BIND_EXPR that
9235 we already have is a fine expression to return. */
9236 if (!type || VOID_TYPE_P (type))
9237 return body;
9238
9239 /* Now that we've located the expression containing the value, it seems
9240 silly to make voidify_wrapper_expr repeat the process. Create a
9241 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9242 tmp = create_tmp_var_raw (type, NULL);
9243
9244 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9245 tree_expr_nonnegative_p giving up immediately. */
9246 val = last;
9247 if (TREE_CODE (val) == NOP_EXPR
9248 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9249 val = TREE_OPERAND (val, 0);
9250
9251 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9252 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9253
9254 {
9255 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9256 SET_EXPR_LOCATION (t, loc);
9257 return t;
9258 }
9259 }
9260 \f
9261 /* Begin and end compound statements. This is as simple as pushing
9262 and popping new statement lists from the tree. */
9263
9264 tree
9265 c_begin_compound_stmt (bool do_scope)
9266 {
9267 tree stmt = push_stmt_list ();
9268 if (do_scope)
9269 push_scope ();
9270 return stmt;
9271 }
9272
9273 /* End a compound statement. STMT is the statement. LOC is the
9274 location of the compound statement-- this is usually the location
9275 of the opening brace. */
9276
9277 tree
9278 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9279 {
9280 tree block = NULL;
9281
9282 if (do_scope)
9283 {
9284 if (c_dialect_objc ())
9285 objc_clear_super_receiver ();
9286 block = pop_scope ();
9287 }
9288
9289 stmt = pop_stmt_list (stmt);
9290 stmt = c_build_bind_expr (loc, block, stmt);
9291
9292 /* If this compound statement is nested immediately inside a statement
9293 expression, then force a BIND_EXPR to be created. Otherwise we'll
9294 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9295 STATEMENT_LISTs merge, and thus we can lose track of what statement
9296 was really last. */
9297 if (cur_stmt_list
9298 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9299 && TREE_CODE (stmt) != BIND_EXPR)
9300 {
9301 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9302 TREE_SIDE_EFFECTS (stmt) = 1;
9303 SET_EXPR_LOCATION (stmt, loc);
9304 }
9305
9306 return stmt;
9307 }
9308
9309 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9310 when the current scope is exited. EH_ONLY is true when this is not
9311 meant to apply to normal control flow transfer. */
9312
9313 void
9314 push_cleanup (tree decl, tree cleanup, bool eh_only)
9315 {
9316 enum tree_code code;
9317 tree stmt, list;
9318 bool stmt_expr;
9319
9320 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9321 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9322 add_stmt (stmt);
9323 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9324 list = push_stmt_list ();
9325 TREE_OPERAND (stmt, 0) = list;
9326 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9327 }
9328 \f
9329 /* Build a binary-operation expression without default conversions.
9330 CODE is the kind of expression to build.
9331 LOCATION is the operator's location.
9332 This function differs from `build' in several ways:
9333 the data type of the result is computed and recorded in it,
9334 warnings are generated if arg data types are invalid,
9335 special handling for addition and subtraction of pointers is known,
9336 and some optimization is done (operations on narrow ints
9337 are done in the narrower type when that gives the same result).
9338 Constant folding is also done before the result is returned.
9339
9340 Note that the operands will never have enumeral types, or function
9341 or array types, because either they will have the default conversions
9342 performed or they have both just been converted to some other type in which
9343 the arithmetic is to be done. */
9344
9345 tree
9346 build_binary_op (location_t location, enum tree_code code,
9347 tree orig_op0, tree orig_op1, int convert_p)
9348 {
9349 tree type0, type1, orig_type0, orig_type1;
9350 tree eptype;
9351 enum tree_code code0, code1;
9352 tree op0, op1;
9353 tree ret = error_mark_node;
9354 const char *invalid_op_diag;
9355 bool op0_int_operands, op1_int_operands;
9356 bool int_const, int_const_or_overflow, int_operands;
9357
9358 /* Expression code to give to the expression when it is built.
9359 Normally this is CODE, which is what the caller asked for,
9360 but in some special cases we change it. */
9361 enum tree_code resultcode = code;
9362
9363 /* Data type in which the computation is to be performed.
9364 In the simplest cases this is the common type of the arguments. */
9365 tree result_type = NULL;
9366
9367 /* When the computation is in excess precision, the type of the
9368 final EXCESS_PRECISION_EXPR. */
9369 tree semantic_result_type = NULL;
9370
9371 /* Nonzero means operands have already been type-converted
9372 in whatever way is necessary.
9373 Zero means they need to be converted to RESULT_TYPE. */
9374 int converted = 0;
9375
9376 /* Nonzero means create the expression with this type, rather than
9377 RESULT_TYPE. */
9378 tree build_type = 0;
9379
9380 /* Nonzero means after finally constructing the expression
9381 convert it to this type. */
9382 tree final_type = 0;
9383
9384 /* Nonzero if this is an operation like MIN or MAX which can
9385 safely be computed in short if both args are promoted shorts.
9386 Also implies COMMON.
9387 -1 indicates a bitwise operation; this makes a difference
9388 in the exact conditions for when it is safe to do the operation
9389 in a narrower mode. */
9390 int shorten = 0;
9391
9392 /* Nonzero if this is a comparison operation;
9393 if both args are promoted shorts, compare the original shorts.
9394 Also implies COMMON. */
9395 int short_compare = 0;
9396
9397 /* Nonzero if this is a right-shift operation, which can be computed on the
9398 original short and then promoted if the operand is a promoted short. */
9399 int short_shift = 0;
9400
9401 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9402 int common = 0;
9403
9404 /* True means types are compatible as far as ObjC is concerned. */
9405 bool objc_ok;
9406
9407 /* True means this is an arithmetic operation that may need excess
9408 precision. */
9409 bool may_need_excess_precision;
9410
9411 /* True means this is a boolean operation that converts both its
9412 operands to truth-values. */
9413 bool boolean_op = false;
9414
9415 if (location == UNKNOWN_LOCATION)
9416 location = input_location;
9417
9418 op0 = orig_op0;
9419 op1 = orig_op1;
9420
9421 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9422 if (op0_int_operands)
9423 op0 = remove_c_maybe_const_expr (op0);
9424 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9425 if (op1_int_operands)
9426 op1 = remove_c_maybe_const_expr (op1);
9427 int_operands = (op0_int_operands && op1_int_operands);
9428 if (int_operands)
9429 {
9430 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9431 && TREE_CODE (orig_op1) == INTEGER_CST);
9432 int_const = (int_const_or_overflow
9433 && !TREE_OVERFLOW (orig_op0)
9434 && !TREE_OVERFLOW (orig_op1));
9435 }
9436 else
9437 int_const = int_const_or_overflow = false;
9438
9439 if (convert_p)
9440 {
9441 op0 = default_conversion (op0);
9442 op1 = default_conversion (op1);
9443 }
9444
9445 orig_type0 = type0 = TREE_TYPE (op0);
9446 orig_type1 = type1 = TREE_TYPE (op1);
9447
9448 /* The expression codes of the data types of the arguments tell us
9449 whether the arguments are integers, floating, pointers, etc. */
9450 code0 = TREE_CODE (type0);
9451 code1 = TREE_CODE (type1);
9452
9453 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9454 STRIP_TYPE_NOPS (op0);
9455 STRIP_TYPE_NOPS (op1);
9456
9457 /* If an error was already reported for one of the arguments,
9458 avoid reporting another error. */
9459
9460 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9461 return error_mark_node;
9462
9463 if ((invalid_op_diag
9464 = targetm.invalid_binary_op (code, type0, type1)))
9465 {
9466 error_at (location, invalid_op_diag);
9467 return error_mark_node;
9468 }
9469
9470 switch (code)
9471 {
9472 case PLUS_EXPR:
9473 case MINUS_EXPR:
9474 case MULT_EXPR:
9475 case TRUNC_DIV_EXPR:
9476 case CEIL_DIV_EXPR:
9477 case FLOOR_DIV_EXPR:
9478 case ROUND_DIV_EXPR:
9479 case EXACT_DIV_EXPR:
9480 may_need_excess_precision = true;
9481 break;
9482 default:
9483 may_need_excess_precision = false;
9484 break;
9485 }
9486 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9487 {
9488 op0 = TREE_OPERAND (op0, 0);
9489 type0 = TREE_TYPE (op0);
9490 }
9491 else if (may_need_excess_precision
9492 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9493 {
9494 type0 = eptype;
9495 op0 = convert (eptype, op0);
9496 }
9497 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9498 {
9499 op1 = TREE_OPERAND (op1, 0);
9500 type1 = TREE_TYPE (op1);
9501 }
9502 else if (may_need_excess_precision
9503 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9504 {
9505 type1 = eptype;
9506 op1 = convert (eptype, op1);
9507 }
9508
9509 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9510
9511 switch (code)
9512 {
9513 case PLUS_EXPR:
9514 /* Handle the pointer + int case. */
9515 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9516 {
9517 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9518 goto return_build_binary_op;
9519 }
9520 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9521 {
9522 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9523 goto return_build_binary_op;
9524 }
9525 else
9526 common = 1;
9527 break;
9528
9529 case MINUS_EXPR:
9530 /* Subtraction of two similar pointers.
9531 We must subtract them as integers, then divide by object size. */
9532 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9533 && comp_target_types (location, type0, type1))
9534 {
9535 ret = pointer_diff (location, op0, op1);
9536 goto return_build_binary_op;
9537 }
9538 /* Handle pointer minus int. Just like pointer plus int. */
9539 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9540 {
9541 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9542 goto return_build_binary_op;
9543 }
9544 else
9545 common = 1;
9546 break;
9547
9548 case MULT_EXPR:
9549 common = 1;
9550 break;
9551
9552 case TRUNC_DIV_EXPR:
9553 case CEIL_DIV_EXPR:
9554 case FLOOR_DIV_EXPR:
9555 case ROUND_DIV_EXPR:
9556 case EXACT_DIV_EXPR:
9557 warn_for_div_by_zero (location, op1);
9558
9559 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9560 || code0 == FIXED_POINT_TYPE
9561 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9562 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9563 || code1 == FIXED_POINT_TYPE
9564 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9565 {
9566 enum tree_code tcode0 = code0, tcode1 = code1;
9567
9568 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9569 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9570 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9571 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9572
9573 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9574 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9575 resultcode = RDIV_EXPR;
9576 else
9577 /* Although it would be tempting to shorten always here, that
9578 loses on some targets, since the modulo instruction is
9579 undefined if the quotient can't be represented in the
9580 computation mode. We shorten only if unsigned or if
9581 dividing by something we know != -1. */
9582 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9583 || (TREE_CODE (op1) == INTEGER_CST
9584 && !integer_all_onesp (op1)));
9585 common = 1;
9586 }
9587 break;
9588
9589 case BIT_AND_EXPR:
9590 case BIT_IOR_EXPR:
9591 case BIT_XOR_EXPR:
9592 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9593 shorten = -1;
9594 /* Allow vector types which are not floating point types. */
9595 else if (code0 == VECTOR_TYPE
9596 && code1 == VECTOR_TYPE
9597 && !VECTOR_FLOAT_TYPE_P (type0)
9598 && !VECTOR_FLOAT_TYPE_P (type1))
9599 common = 1;
9600 break;
9601
9602 case TRUNC_MOD_EXPR:
9603 case FLOOR_MOD_EXPR:
9604 warn_for_div_by_zero (location, op1);
9605
9606 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9607 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9608 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9609 common = 1;
9610 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9611 {
9612 /* Although it would be tempting to shorten always here, that loses
9613 on some targets, since the modulo instruction is undefined if the
9614 quotient can't be represented in the computation mode. We shorten
9615 only if unsigned or if dividing by something we know != -1. */
9616 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9617 || (TREE_CODE (op1) == INTEGER_CST
9618 && !integer_all_onesp (op1)));
9619 common = 1;
9620 }
9621 break;
9622
9623 case TRUTH_ANDIF_EXPR:
9624 case TRUTH_ORIF_EXPR:
9625 case TRUTH_AND_EXPR:
9626 case TRUTH_OR_EXPR:
9627 case TRUTH_XOR_EXPR:
9628 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9629 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9630 || code0 == FIXED_POINT_TYPE)
9631 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9632 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9633 || code1 == FIXED_POINT_TYPE))
9634 {
9635 /* Result of these operations is always an int,
9636 but that does not mean the operands should be
9637 converted to ints! */
9638 result_type = integer_type_node;
9639 op0 = c_common_truthvalue_conversion (location, op0);
9640 op1 = c_common_truthvalue_conversion (location, op1);
9641 converted = 1;
9642 boolean_op = true;
9643 }
9644 if (code == TRUTH_ANDIF_EXPR)
9645 {
9646 int_const_or_overflow = (int_operands
9647 && TREE_CODE (orig_op0) == INTEGER_CST
9648 && (op0 == truthvalue_false_node
9649 || TREE_CODE (orig_op1) == INTEGER_CST));
9650 int_const = (int_const_or_overflow
9651 && !TREE_OVERFLOW (orig_op0)
9652 && (op0 == truthvalue_false_node
9653 || !TREE_OVERFLOW (orig_op1)));
9654 }
9655 else if (code == TRUTH_ORIF_EXPR)
9656 {
9657 int_const_or_overflow = (int_operands
9658 && TREE_CODE (orig_op0) == INTEGER_CST
9659 && (op0 == truthvalue_true_node
9660 || TREE_CODE (orig_op1) == INTEGER_CST));
9661 int_const = (int_const_or_overflow
9662 && !TREE_OVERFLOW (orig_op0)
9663 && (op0 == truthvalue_true_node
9664 || !TREE_OVERFLOW (orig_op1)));
9665 }
9666 break;
9667
9668 /* Shift operations: result has same type as first operand;
9669 always convert second operand to int.
9670 Also set SHORT_SHIFT if shifting rightward. */
9671
9672 case RSHIFT_EXPR:
9673 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9674 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9675 {
9676 result_type = type0;
9677 converted = 1;
9678 }
9679 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9680 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9681 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9682 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9683 {
9684 result_type = type0;
9685 converted = 1;
9686 }
9687 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9688 && code1 == INTEGER_TYPE)
9689 {
9690 if (TREE_CODE (op1) == INTEGER_CST)
9691 {
9692 if (tree_int_cst_sgn (op1) < 0)
9693 {
9694 int_const = false;
9695 if (c_inhibit_evaluation_warnings == 0)
9696 warning (0, "right shift count is negative");
9697 }
9698 else
9699 {
9700 if (!integer_zerop (op1))
9701 short_shift = 1;
9702
9703 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9704 {
9705 int_const = false;
9706 if (c_inhibit_evaluation_warnings == 0)
9707 warning (0, "right shift count >= width of type");
9708 }
9709 }
9710 }
9711
9712 /* Use the type of the value to be shifted. */
9713 result_type = type0;
9714 /* Convert the non vector shift-count to an integer, regardless
9715 of size of value being shifted. */
9716 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9717 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9718 op1 = convert (integer_type_node, op1);
9719 /* Avoid converting op1 to result_type later. */
9720 converted = 1;
9721 }
9722 break;
9723
9724 case LSHIFT_EXPR:
9725 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9726 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9727 {
9728 result_type = type0;
9729 converted = 1;
9730 }
9731 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9732 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9733 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9734 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9735 {
9736 result_type = type0;
9737 converted = 1;
9738 }
9739 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9740 && code1 == INTEGER_TYPE)
9741 {
9742 if (TREE_CODE (op1) == INTEGER_CST)
9743 {
9744 if (tree_int_cst_sgn (op1) < 0)
9745 {
9746 int_const = false;
9747 if (c_inhibit_evaluation_warnings == 0)
9748 warning (0, "left shift count is negative");
9749 }
9750
9751 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9752 {
9753 int_const = false;
9754 if (c_inhibit_evaluation_warnings == 0)
9755 warning (0, "left shift count >= width of type");
9756 }
9757 }
9758
9759 /* Use the type of the value to be shifted. */
9760 result_type = type0;
9761 /* Convert the non vector shift-count to an integer, regardless
9762 of size of value being shifted. */
9763 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9764 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9765 op1 = convert (integer_type_node, op1);
9766 /* Avoid converting op1 to result_type later. */
9767 converted = 1;
9768 }
9769 break;
9770
9771 case EQ_EXPR:
9772 case NE_EXPR:
9773 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9774 warning_at (location,
9775 OPT_Wfloat_equal,
9776 "comparing floating point with == or != is unsafe");
9777 /* Result of comparison is always int,
9778 but don't convert the args to int! */
9779 build_type = integer_type_node;
9780 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9781 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9782 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9783 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9784 short_compare = 1;
9785 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9786 {
9787 if (TREE_CODE (op0) == ADDR_EXPR
9788 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9789 {
9790 if (code == EQ_EXPR)
9791 warning_at (location,
9792 OPT_Waddress,
9793 "the comparison will always evaluate as %<false%> "
9794 "for the address of %qD will never be NULL",
9795 TREE_OPERAND (op0, 0));
9796 else
9797 warning_at (location,
9798 OPT_Waddress,
9799 "the comparison will always evaluate as %<true%> "
9800 "for the address of %qD will never be NULL",
9801 TREE_OPERAND (op0, 0));
9802 }
9803 result_type = type0;
9804 }
9805 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9806 {
9807 if (TREE_CODE (op1) == ADDR_EXPR
9808 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9809 {
9810 if (code == EQ_EXPR)
9811 warning_at (location,
9812 OPT_Waddress,
9813 "the comparison will always evaluate as %<false%> "
9814 "for the address of %qD will never be NULL",
9815 TREE_OPERAND (op1, 0));
9816 else
9817 warning_at (location,
9818 OPT_Waddress,
9819 "the comparison will always evaluate as %<true%> "
9820 "for the address of %qD will never be NULL",
9821 TREE_OPERAND (op1, 0));
9822 }
9823 result_type = type1;
9824 }
9825 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9826 {
9827 tree tt0 = TREE_TYPE (type0);
9828 tree tt1 = TREE_TYPE (type1);
9829 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9830 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9831 addr_space_t as_common = ADDR_SPACE_GENERIC;
9832
9833 /* Anything compares with void *. void * compares with anything.
9834 Otherwise, the targets must be compatible
9835 and both must be object or both incomplete. */
9836 if (comp_target_types (location, type0, type1))
9837 result_type = common_pointer_type (type0, type1);
9838 else if (!addr_space_superset (as0, as1, &as_common))
9839 {
9840 error_at (location, "comparison of pointers to "
9841 "disjoint address spaces");
9842 return error_mark_node;
9843 }
9844 else if (VOID_TYPE_P (tt0))
9845 {
9846 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9847 pedwarn (location, OPT_pedantic, "ISO C forbids "
9848 "comparison of %<void *%> with function pointer");
9849 }
9850 else if (VOID_TYPE_P (tt1))
9851 {
9852 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9853 pedwarn (location, OPT_pedantic, "ISO C forbids "
9854 "comparison of %<void *%> with function pointer");
9855 }
9856 else
9857 /* Avoid warning about the volatile ObjC EH puts on decls. */
9858 if (!objc_ok)
9859 pedwarn (location, 0,
9860 "comparison of distinct pointer types lacks a cast");
9861
9862 if (result_type == NULL_TREE)
9863 {
9864 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9865 result_type = build_pointer_type
9866 (build_qualified_type (void_type_node, qual));
9867 }
9868 }
9869 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9870 {
9871 result_type = type0;
9872 pedwarn (location, 0, "comparison between pointer and integer");
9873 }
9874 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9875 {
9876 result_type = type1;
9877 pedwarn (location, 0, "comparison between pointer and integer");
9878 }
9879 break;
9880
9881 case LE_EXPR:
9882 case GE_EXPR:
9883 case LT_EXPR:
9884 case GT_EXPR:
9885 build_type = integer_type_node;
9886 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9887 || code0 == FIXED_POINT_TYPE)
9888 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9889 || code1 == FIXED_POINT_TYPE))
9890 short_compare = 1;
9891 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9892 {
9893 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9894 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9895 addr_space_t as_common;
9896
9897 if (comp_target_types (location, type0, type1))
9898 {
9899 result_type = common_pointer_type (type0, type1);
9900 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9901 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9902 pedwarn (location, 0,
9903 "comparison of complete and incomplete pointers");
9904 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9905 pedwarn (location, OPT_pedantic, "ISO C forbids "
9906 "ordered comparisons of pointers to functions");
9907 else if (null_pointer_constant_p (orig_op0)
9908 || null_pointer_constant_p (orig_op1))
9909 warning_at (location, OPT_Wextra,
9910 "ordered comparison of pointer with null pointer");
9911
9912 }
9913 else if (!addr_space_superset (as0, as1, &as_common))
9914 {
9915 error_at (location, "comparison of pointers to "
9916 "disjoint address spaces");
9917 return error_mark_node;
9918 }
9919 else
9920 {
9921 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9922 result_type = build_pointer_type
9923 (build_qualified_type (void_type_node, qual));
9924 pedwarn (location, 0,
9925 "comparison of distinct pointer types lacks a cast");
9926 }
9927 }
9928 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9929 {
9930 result_type = type0;
9931 if (pedantic)
9932 pedwarn (location, OPT_pedantic,
9933 "ordered comparison of pointer with integer zero");
9934 else if (extra_warnings)
9935 warning_at (location, OPT_Wextra,
9936 "ordered comparison of pointer with integer zero");
9937 }
9938 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9939 {
9940 result_type = type1;
9941 if (pedantic)
9942 pedwarn (location, OPT_pedantic,
9943 "ordered comparison of pointer with integer zero");
9944 else if (extra_warnings)
9945 warning_at (location, OPT_Wextra,
9946 "ordered comparison of pointer with integer zero");
9947 }
9948 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9949 {
9950 result_type = type0;
9951 pedwarn (location, 0, "comparison between pointer and integer");
9952 }
9953 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9954 {
9955 result_type = type1;
9956 pedwarn (location, 0, "comparison between pointer and integer");
9957 }
9958 break;
9959
9960 default:
9961 gcc_unreachable ();
9962 }
9963
9964 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9965 return error_mark_node;
9966
9967 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9968 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9969 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9970 TREE_TYPE (type1))))
9971 {
9972 binary_op_error (location, code, type0, type1);
9973 return error_mark_node;
9974 }
9975
9976 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9977 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9978 &&
9979 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9980 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9981 {
9982 bool first_complex = (code0 == COMPLEX_TYPE);
9983 bool second_complex = (code1 == COMPLEX_TYPE);
9984 int none_complex = (!first_complex && !second_complex);
9985
9986 if (shorten || common || short_compare)
9987 {
9988 result_type = c_common_type (type0, type1);
9989 do_warn_double_promotion (result_type, type0, type1,
9990 "implicit conversion from %qT to %qT "
9991 "to match other operand of binary "
9992 "expression",
9993 location);
9994 if (result_type == error_mark_node)
9995 return error_mark_node;
9996 }
9997
9998 if (first_complex != second_complex
9999 && (code == PLUS_EXPR
10000 || code == MINUS_EXPR
10001 || code == MULT_EXPR
10002 || (code == TRUNC_DIV_EXPR && first_complex))
10003 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10004 && flag_signed_zeros)
10005 {
10006 /* An operation on mixed real/complex operands must be
10007 handled specially, but the language-independent code can
10008 more easily optimize the plain complex arithmetic if
10009 -fno-signed-zeros. */
10010 tree real_type = TREE_TYPE (result_type);
10011 tree real, imag;
10012 if (type0 != orig_type0 || type1 != orig_type1)
10013 {
10014 gcc_assert (may_need_excess_precision && common);
10015 semantic_result_type = c_common_type (orig_type0, orig_type1);
10016 }
10017 if (first_complex)
10018 {
10019 if (TREE_TYPE (op0) != result_type)
10020 op0 = convert_and_check (result_type, op0);
10021 if (TREE_TYPE (op1) != real_type)
10022 op1 = convert_and_check (real_type, op1);
10023 }
10024 else
10025 {
10026 if (TREE_TYPE (op0) != real_type)
10027 op0 = convert_and_check (real_type, op0);
10028 if (TREE_TYPE (op1) != result_type)
10029 op1 = convert_and_check (result_type, op1);
10030 }
10031 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10032 return error_mark_node;
10033 if (first_complex)
10034 {
10035 op0 = c_save_expr (op0);
10036 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10037 op0, 1);
10038 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10039 op0, 1);
10040 switch (code)
10041 {
10042 case MULT_EXPR:
10043 case TRUNC_DIV_EXPR:
10044 imag = build2 (resultcode, real_type, imag, op1);
10045 /* Fall through. */
10046 case PLUS_EXPR:
10047 case MINUS_EXPR:
10048 real = build2 (resultcode, real_type, real, op1);
10049 break;
10050 default:
10051 gcc_unreachable();
10052 }
10053 }
10054 else
10055 {
10056 op1 = c_save_expr (op1);
10057 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10058 op1, 1);
10059 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10060 op1, 1);
10061 switch (code)
10062 {
10063 case MULT_EXPR:
10064 imag = build2 (resultcode, real_type, op0, imag);
10065 /* Fall through. */
10066 case PLUS_EXPR:
10067 real = build2 (resultcode, real_type, op0, real);
10068 break;
10069 case MINUS_EXPR:
10070 real = build2 (resultcode, real_type, op0, real);
10071 imag = build1 (NEGATE_EXPR, real_type, imag);
10072 break;
10073 default:
10074 gcc_unreachable();
10075 }
10076 }
10077 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10078 goto return_build_binary_op;
10079 }
10080
10081 /* For certain operations (which identify themselves by shorten != 0)
10082 if both args were extended from the same smaller type,
10083 do the arithmetic in that type and then extend.
10084
10085 shorten !=0 and !=1 indicates a bitwise operation.
10086 For them, this optimization is safe only if
10087 both args are zero-extended or both are sign-extended.
10088 Otherwise, we might change the result.
10089 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10090 but calculated in (unsigned short) it would be (unsigned short)-1. */
10091
10092 if (shorten && none_complex)
10093 {
10094 final_type = result_type;
10095 result_type = shorten_binary_op (result_type, op0, op1,
10096 shorten == -1);
10097 }
10098
10099 /* Shifts can be shortened if shifting right. */
10100
10101 if (short_shift)
10102 {
10103 int unsigned_arg;
10104 tree arg0 = get_narrower (op0, &unsigned_arg);
10105
10106 final_type = result_type;
10107
10108 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10109 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10110
10111 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10112 && tree_int_cst_sgn (op1) > 0
10113 /* We can shorten only if the shift count is less than the
10114 number of bits in the smaller type size. */
10115 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10116 /* We cannot drop an unsigned shift after sign-extension. */
10117 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10118 {
10119 /* Do an unsigned shift if the operand was zero-extended. */
10120 result_type
10121 = c_common_signed_or_unsigned_type (unsigned_arg,
10122 TREE_TYPE (arg0));
10123 /* Convert value-to-be-shifted to that type. */
10124 if (TREE_TYPE (op0) != result_type)
10125 op0 = convert (result_type, op0);
10126 converted = 1;
10127 }
10128 }
10129
10130 /* Comparison operations are shortened too but differently.
10131 They identify themselves by setting short_compare = 1. */
10132
10133 if (short_compare)
10134 {
10135 /* Don't write &op0, etc., because that would prevent op0
10136 from being kept in a register.
10137 Instead, make copies of the our local variables and
10138 pass the copies by reference, then copy them back afterward. */
10139 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10140 enum tree_code xresultcode = resultcode;
10141 tree val
10142 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10143
10144 if (val != 0)
10145 {
10146 ret = val;
10147 goto return_build_binary_op;
10148 }
10149
10150 op0 = xop0, op1 = xop1;
10151 converted = 1;
10152 resultcode = xresultcode;
10153
10154 if (c_inhibit_evaluation_warnings == 0)
10155 {
10156 bool op0_maybe_const = true;
10157 bool op1_maybe_const = true;
10158 tree orig_op0_folded, orig_op1_folded;
10159
10160 if (in_late_binary_op)
10161 {
10162 orig_op0_folded = orig_op0;
10163 orig_op1_folded = orig_op1;
10164 }
10165 else
10166 {
10167 /* Fold for the sake of possible warnings, as in
10168 build_conditional_expr. This requires the
10169 "original" values to be folded, not just op0 and
10170 op1. */
10171 c_inhibit_evaluation_warnings++;
10172 op0 = c_fully_fold (op0, require_constant_value,
10173 &op0_maybe_const);
10174 op1 = c_fully_fold (op1, require_constant_value,
10175 &op1_maybe_const);
10176 c_inhibit_evaluation_warnings--;
10177 orig_op0_folded = c_fully_fold (orig_op0,
10178 require_constant_value,
10179 NULL);
10180 orig_op1_folded = c_fully_fold (orig_op1,
10181 require_constant_value,
10182 NULL);
10183 }
10184
10185 if (warn_sign_compare)
10186 warn_for_sign_compare (location, orig_op0_folded,
10187 orig_op1_folded, op0, op1,
10188 result_type, resultcode);
10189 if (!in_late_binary_op && !int_operands)
10190 {
10191 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10192 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10193 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10194 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10195 }
10196 }
10197 }
10198 }
10199
10200 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10201 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10202 Then the expression will be built.
10203 It will be given type FINAL_TYPE if that is nonzero;
10204 otherwise, it will be given type RESULT_TYPE. */
10205
10206 if (!result_type)
10207 {
10208 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10209 return error_mark_node;
10210 }
10211
10212 if (build_type == NULL_TREE)
10213 {
10214 build_type = result_type;
10215 if ((type0 != orig_type0 || type1 != orig_type1)
10216 && !boolean_op)
10217 {
10218 gcc_assert (may_need_excess_precision && common);
10219 semantic_result_type = c_common_type (orig_type0, orig_type1);
10220 }
10221 }
10222
10223 if (!converted)
10224 {
10225 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10226 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10227
10228 /* This can happen if one operand has a vector type, and the other
10229 has a different type. */
10230 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10231 return error_mark_node;
10232 }
10233
10234 /* Treat expressions in initializers specially as they can't trap. */
10235 if (int_const_or_overflow)
10236 ret = (require_constant_value
10237 ? fold_build2_initializer_loc (location, resultcode, build_type,
10238 op0, op1)
10239 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10240 else
10241 ret = build2 (resultcode, build_type, op0, op1);
10242 if (final_type != 0)
10243 ret = convert (final_type, ret);
10244
10245 return_build_binary_op:
10246 gcc_assert (ret != error_mark_node);
10247 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10248 ret = (int_operands
10249 ? note_integer_operands (ret)
10250 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10251 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10252 && !in_late_binary_op)
10253 ret = note_integer_operands (ret);
10254 if (semantic_result_type)
10255 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10256 protected_set_expr_location (ret, location);
10257 return ret;
10258 }
10259
10260
10261 /* Convert EXPR to be a truth-value, validating its type for this
10262 purpose. LOCATION is the source location for the expression. */
10263
10264 tree
10265 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10266 {
10267 bool int_const, int_operands;
10268
10269 switch (TREE_CODE (TREE_TYPE (expr)))
10270 {
10271 case ARRAY_TYPE:
10272 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10273 return error_mark_node;
10274
10275 case RECORD_TYPE:
10276 error_at (location, "used struct type value where scalar is required");
10277 return error_mark_node;
10278
10279 case UNION_TYPE:
10280 error_at (location, "used union type value where scalar is required");
10281 return error_mark_node;
10282
10283 case VOID_TYPE:
10284 error_at (location, "void value not ignored as it ought to be");
10285 return error_mark_node;
10286
10287 case FUNCTION_TYPE:
10288 gcc_unreachable ();
10289
10290 default:
10291 break;
10292 }
10293
10294 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10295 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10296 if (int_operands)
10297 expr = remove_c_maybe_const_expr (expr);
10298
10299 /* ??? Should we also give an error for vectors rather than leaving
10300 those to give errors later? */
10301 expr = c_common_truthvalue_conversion (location, expr);
10302
10303 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10304 {
10305 if (TREE_OVERFLOW (expr))
10306 return expr;
10307 else
10308 return note_integer_operands (expr);
10309 }
10310 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10311 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10312 return expr;
10313 }
10314 \f
10315
10316 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10317 required. */
10318
10319 tree
10320 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10321 {
10322 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10323 {
10324 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10325 /* Executing a compound literal inside a function reinitializes
10326 it. */
10327 if (!TREE_STATIC (decl))
10328 *se = true;
10329 return decl;
10330 }
10331 else
10332 return expr;
10333 }
10334 \f
10335 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10336
10337 tree
10338 c_begin_omp_parallel (void)
10339 {
10340 tree block;
10341
10342 keep_next_level ();
10343 block = c_begin_compound_stmt (true);
10344
10345 return block;
10346 }
10347
10348 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10349 statement. LOC is the location of the OMP_PARALLEL. */
10350
10351 tree
10352 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10353 {
10354 tree stmt;
10355
10356 block = c_end_compound_stmt (loc, block, true);
10357
10358 stmt = make_node (OMP_PARALLEL);
10359 TREE_TYPE (stmt) = void_type_node;
10360 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10361 OMP_PARALLEL_BODY (stmt) = block;
10362 SET_EXPR_LOCATION (stmt, loc);
10363
10364 return add_stmt (stmt);
10365 }
10366
10367 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10368
10369 tree
10370 c_begin_omp_task (void)
10371 {
10372 tree block;
10373
10374 keep_next_level ();
10375 block = c_begin_compound_stmt (true);
10376
10377 return block;
10378 }
10379
10380 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10381 statement. LOC is the location of the #pragma. */
10382
10383 tree
10384 c_finish_omp_task (location_t loc, tree clauses, tree block)
10385 {
10386 tree stmt;
10387
10388 block = c_end_compound_stmt (loc, block, true);
10389
10390 stmt = make_node (OMP_TASK);
10391 TREE_TYPE (stmt) = void_type_node;
10392 OMP_TASK_CLAUSES (stmt) = clauses;
10393 OMP_TASK_BODY (stmt) = block;
10394 SET_EXPR_LOCATION (stmt, loc);
10395
10396 return add_stmt (stmt);
10397 }
10398
10399 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10400 Remove any elements from the list that are invalid. */
10401
10402 tree
10403 c_finish_omp_clauses (tree clauses)
10404 {
10405 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10406 tree c, t, *pc = &clauses;
10407 const char *name;
10408
10409 bitmap_obstack_initialize (NULL);
10410 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10411 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10412 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10413
10414 for (pc = &clauses, c = clauses; c ; c = *pc)
10415 {
10416 bool remove = false;
10417 bool need_complete = false;
10418 bool need_implicitly_determined = false;
10419
10420 switch (OMP_CLAUSE_CODE (c))
10421 {
10422 case OMP_CLAUSE_SHARED:
10423 name = "shared";
10424 need_implicitly_determined = true;
10425 goto check_dup_generic;
10426
10427 case OMP_CLAUSE_PRIVATE:
10428 name = "private";
10429 need_complete = true;
10430 need_implicitly_determined = true;
10431 goto check_dup_generic;
10432
10433 case OMP_CLAUSE_REDUCTION:
10434 name = "reduction";
10435 need_implicitly_determined = true;
10436 t = OMP_CLAUSE_DECL (c);
10437 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10438 || POINTER_TYPE_P (TREE_TYPE (t)))
10439 {
10440 error_at (OMP_CLAUSE_LOCATION (c),
10441 "%qE has invalid type for %<reduction%>", t);
10442 remove = true;
10443 }
10444 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10445 {
10446 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10447 const char *r_name = NULL;
10448
10449 switch (r_code)
10450 {
10451 case PLUS_EXPR:
10452 case MULT_EXPR:
10453 case MINUS_EXPR:
10454 break;
10455 case BIT_AND_EXPR:
10456 r_name = "&";
10457 break;
10458 case BIT_XOR_EXPR:
10459 r_name = "^";
10460 break;
10461 case BIT_IOR_EXPR:
10462 r_name = "|";
10463 break;
10464 case TRUTH_ANDIF_EXPR:
10465 r_name = "&&";
10466 break;
10467 case TRUTH_ORIF_EXPR:
10468 r_name = "||";
10469 break;
10470 default:
10471 gcc_unreachable ();
10472 }
10473 if (r_name)
10474 {
10475 error_at (OMP_CLAUSE_LOCATION (c),
10476 "%qE has invalid type for %<reduction(%s)%>",
10477 t, r_name);
10478 remove = true;
10479 }
10480 }
10481 goto check_dup_generic;
10482
10483 case OMP_CLAUSE_COPYPRIVATE:
10484 name = "copyprivate";
10485 goto check_dup_generic;
10486
10487 case OMP_CLAUSE_COPYIN:
10488 name = "copyin";
10489 t = OMP_CLAUSE_DECL (c);
10490 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10491 {
10492 error_at (OMP_CLAUSE_LOCATION (c),
10493 "%qE must be %<threadprivate%> for %<copyin%>", t);
10494 remove = true;
10495 }
10496 goto check_dup_generic;
10497
10498 check_dup_generic:
10499 t = OMP_CLAUSE_DECL (c);
10500 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10501 {
10502 error_at (OMP_CLAUSE_LOCATION (c),
10503 "%qE is not a variable in clause %qs", t, name);
10504 remove = true;
10505 }
10506 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10507 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10508 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10509 {
10510 error_at (OMP_CLAUSE_LOCATION (c),
10511 "%qE appears more than once in data clauses", t);
10512 remove = true;
10513 }
10514 else
10515 bitmap_set_bit (&generic_head, DECL_UID (t));
10516 break;
10517
10518 case OMP_CLAUSE_FIRSTPRIVATE:
10519 name = "firstprivate";
10520 t = OMP_CLAUSE_DECL (c);
10521 need_complete = true;
10522 need_implicitly_determined = true;
10523 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10524 {
10525 error_at (OMP_CLAUSE_LOCATION (c),
10526 "%qE is not a variable in clause %<firstprivate%>", t);
10527 remove = true;
10528 }
10529 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10530 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10531 {
10532 error_at (OMP_CLAUSE_LOCATION (c),
10533 "%qE appears more than once in data clauses", t);
10534 remove = true;
10535 }
10536 else
10537 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10538 break;
10539
10540 case OMP_CLAUSE_LASTPRIVATE:
10541 name = "lastprivate";
10542 t = OMP_CLAUSE_DECL (c);
10543 need_complete = true;
10544 need_implicitly_determined = true;
10545 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10546 {
10547 error_at (OMP_CLAUSE_LOCATION (c),
10548 "%qE is not a variable in clause %<lastprivate%>", t);
10549 remove = true;
10550 }
10551 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10552 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10553 {
10554 error_at (OMP_CLAUSE_LOCATION (c),
10555 "%qE appears more than once in data clauses", t);
10556 remove = true;
10557 }
10558 else
10559 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10560 break;
10561
10562 case OMP_CLAUSE_IF:
10563 case OMP_CLAUSE_NUM_THREADS:
10564 case OMP_CLAUSE_SCHEDULE:
10565 case OMP_CLAUSE_NOWAIT:
10566 case OMP_CLAUSE_ORDERED:
10567 case OMP_CLAUSE_DEFAULT:
10568 case OMP_CLAUSE_UNTIED:
10569 case OMP_CLAUSE_COLLAPSE:
10570 pc = &OMP_CLAUSE_CHAIN (c);
10571 continue;
10572
10573 default:
10574 gcc_unreachable ();
10575 }
10576
10577 if (!remove)
10578 {
10579 t = OMP_CLAUSE_DECL (c);
10580
10581 if (need_complete)
10582 {
10583 t = require_complete_type (t);
10584 if (t == error_mark_node)
10585 remove = true;
10586 }
10587
10588 if (need_implicitly_determined)
10589 {
10590 const char *share_name = NULL;
10591
10592 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10593 share_name = "threadprivate";
10594 else switch (c_omp_predetermined_sharing (t))
10595 {
10596 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10597 break;
10598 case OMP_CLAUSE_DEFAULT_SHARED:
10599 share_name = "shared";
10600 break;
10601 case OMP_CLAUSE_DEFAULT_PRIVATE:
10602 share_name = "private";
10603 break;
10604 default:
10605 gcc_unreachable ();
10606 }
10607 if (share_name)
10608 {
10609 error_at (OMP_CLAUSE_LOCATION (c),
10610 "%qE is predetermined %qs for %qs",
10611 t, share_name, name);
10612 remove = true;
10613 }
10614 }
10615 }
10616
10617 if (remove)
10618 *pc = OMP_CLAUSE_CHAIN (c);
10619 else
10620 pc = &OMP_CLAUSE_CHAIN (c);
10621 }
10622
10623 bitmap_obstack_release (NULL);
10624 return clauses;
10625 }
10626
10627 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10628 down to the element type of an array. */
10629
10630 tree
10631 c_build_qualified_type (tree type, int type_quals)
10632 {
10633 if (type == error_mark_node)
10634 return type;
10635
10636 if (TREE_CODE (type) == ARRAY_TYPE)
10637 {
10638 tree t;
10639 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10640 type_quals);
10641
10642 /* See if we already have an identically qualified type. */
10643 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10644 {
10645 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10646 && TYPE_NAME (t) == TYPE_NAME (type)
10647 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10648 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10649 TYPE_ATTRIBUTES (type)))
10650 break;
10651 }
10652 if (!t)
10653 {
10654 tree domain = TYPE_DOMAIN (type);
10655
10656 t = build_variant_type_copy (type);
10657 TREE_TYPE (t) = element_type;
10658
10659 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10660 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10661 SET_TYPE_STRUCTURAL_EQUALITY (t);
10662 else if (TYPE_CANONICAL (element_type) != element_type
10663 || (domain && TYPE_CANONICAL (domain) != domain))
10664 {
10665 tree unqualified_canon
10666 = build_array_type (TYPE_CANONICAL (element_type),
10667 domain? TYPE_CANONICAL (domain)
10668 : NULL_TREE);
10669 TYPE_CANONICAL (t)
10670 = c_build_qualified_type (unqualified_canon, type_quals);
10671 }
10672 else
10673 TYPE_CANONICAL (t) = t;
10674 }
10675 return t;
10676 }
10677
10678 /* A restrict-qualified pointer type must be a pointer to object or
10679 incomplete type. Note that the use of POINTER_TYPE_P also allows
10680 REFERENCE_TYPEs, which is appropriate for C++. */
10681 if ((type_quals & TYPE_QUAL_RESTRICT)
10682 && (!POINTER_TYPE_P (type)
10683 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10684 {
10685 error ("invalid use of %<restrict%>");
10686 type_quals &= ~TYPE_QUAL_RESTRICT;
10687 }
10688
10689 return build_qualified_type (type, type_quals);
10690 }
10691
10692 /* Build a VA_ARG_EXPR for the C parser. */
10693
10694 tree
10695 c_build_va_arg (location_t loc, tree expr, tree type)
10696 {
10697 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10698 warning_at (loc, OPT_Wc___compat,
10699 "C++ requires promoted type, not enum type, in %<va_arg%>");
10700 return build_va_arg (loc, expr, type);
10701 }