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