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