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