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