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