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