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