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