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