re PR c/41182 (Revision 145254 caused ICE: tree check: expected integer_cst, have...
[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 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
3944 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
3945 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
3946 }
3947 }
3948 }
3949 }
3950 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3951 {
3952 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
3953 pedwarn (colon_loc, OPT_pedantic,
3954 "ISO C forbids conditional expr with only one void side");
3955 result_type = void_type_node;
3956 }
3957 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3958 {
3959 if (comp_target_types (colon_loc, type1, type2))
3960 result_type = common_pointer_type (type1, type2);
3961 else if (null_pointer_constant_p (orig_op1))
3962 result_type = qualify_type (type2, type1);
3963 else if (null_pointer_constant_p (orig_op2))
3964 result_type = qualify_type (type1, type2);
3965 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3966 {
3967 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3968 pedwarn (colon_loc, OPT_pedantic,
3969 "ISO C forbids conditional expr between "
3970 "%<void *%> and function pointer");
3971 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3972 TREE_TYPE (type2)));
3973 }
3974 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3975 {
3976 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3977 pedwarn (colon_loc, OPT_pedantic,
3978 "ISO C forbids conditional expr between "
3979 "%<void *%> and function pointer");
3980 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3981 TREE_TYPE (type1)));
3982 }
3983 else
3984 {
3985 if (!objc_ok)
3986 pedwarn (colon_loc, 0,
3987 "pointer type mismatch in conditional expression");
3988 result_type = build_pointer_type (void_type_node);
3989 }
3990 }
3991 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3992 {
3993 if (!null_pointer_constant_p (orig_op2))
3994 pedwarn (colon_loc, 0,
3995 "pointer/integer type mismatch in conditional expression");
3996 else
3997 {
3998 op2 = null_pointer_node;
3999 }
4000 result_type = type1;
4001 }
4002 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4003 {
4004 if (!null_pointer_constant_p (orig_op1))
4005 pedwarn (colon_loc, 0,
4006 "pointer/integer type mismatch in conditional expression");
4007 else
4008 {
4009 op1 = null_pointer_node;
4010 }
4011 result_type = type2;
4012 }
4013
4014 if (!result_type)
4015 {
4016 if (flag_cond_mismatch)
4017 result_type = void_type_node;
4018 else
4019 {
4020 error_at (colon_loc, "type mismatch in conditional expression");
4021 return error_mark_node;
4022 }
4023 }
4024
4025 /* Merge const and volatile flags of the incoming types. */
4026 result_type
4027 = build_type_variant (result_type,
4028 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4029 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4030
4031 if (result_type != type1)
4032 op1 = convert_and_check (result_type, op1);
4033 if (result_type != type2)
4034 op2 = convert_and_check (result_type, op2);
4035
4036 if (ifexp_bcp && ifexp == truthvalue_true_node)
4037 {
4038 op2_int_operands = true;
4039 op1 = c_fully_fold (op1, require_constant_value, NULL);
4040 }
4041 if (ifexp_bcp && ifexp == truthvalue_false_node)
4042 {
4043 op1_int_operands = true;
4044 op2 = c_fully_fold (op2, require_constant_value, NULL);
4045 }
4046 int_const = int_operands = (ifexp_int_operands
4047 && op1_int_operands
4048 && op2_int_operands);
4049 if (int_operands)
4050 {
4051 int_const = ((ifexp == truthvalue_true_node
4052 && TREE_CODE (orig_op1) == INTEGER_CST
4053 && !TREE_OVERFLOW (orig_op1))
4054 || (ifexp == truthvalue_false_node
4055 && TREE_CODE (orig_op2) == INTEGER_CST
4056 && !TREE_OVERFLOW (orig_op2)));
4057 }
4058 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4059 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4060 else
4061 {
4062 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4063 if (int_operands)
4064 ret = note_integer_operands (ret);
4065 }
4066 if (ep_result_type)
4067 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
4068
4069 protected_set_expr_location (ret, colon_loc);
4070 return ret;
4071 }
4072 \f
4073 /* Return a compound expression that performs two expressions and
4074 returns the value of the second of them.
4075
4076 LOC is the location of the COMPOUND_EXPR. */
4077
4078 tree
4079 build_compound_expr (location_t loc, tree expr1, tree expr2)
4080 {
4081 bool expr1_int_operands, expr2_int_operands;
4082 tree eptype = NULL_TREE;
4083 tree ret;
4084
4085 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4086 if (expr1_int_operands)
4087 expr1 = remove_c_maybe_const_expr (expr1);
4088 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4089 if (expr2_int_operands)
4090 expr2 = remove_c_maybe_const_expr (expr2);
4091
4092 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4093 expr1 = TREE_OPERAND (expr1, 0);
4094 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4095 {
4096 eptype = TREE_TYPE (expr2);
4097 expr2 = TREE_OPERAND (expr2, 0);
4098 }
4099
4100 if (!TREE_SIDE_EFFECTS (expr1))
4101 {
4102 /* The left-hand operand of a comma expression is like an expression
4103 statement: with -Wunused, we should warn if it doesn't have
4104 any side-effects, unless it was explicitly cast to (void). */
4105 if (warn_unused_value)
4106 {
4107 if (VOID_TYPE_P (TREE_TYPE (expr1))
4108 && CONVERT_EXPR_P (expr1))
4109 ; /* (void) a, b */
4110 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4111 && TREE_CODE (expr1) == COMPOUND_EXPR
4112 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4113 ; /* (void) a, (void) b, c */
4114 else
4115 warning_at (loc, OPT_Wunused_value,
4116 "left-hand operand of comma expression has no effect");
4117 }
4118 }
4119
4120 /* With -Wunused, we should also warn if the left-hand operand does have
4121 side-effects, but computes a value which is not used. For example, in
4122 `foo() + bar(), baz()' the result of the `+' operator is not used,
4123 so we should issue a warning. */
4124 else if (warn_unused_value)
4125 warn_if_unused_value (expr1, loc);
4126
4127 if (expr2 == error_mark_node)
4128 return error_mark_node;
4129
4130 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4131
4132 if (flag_isoc99
4133 && expr1_int_operands
4134 && expr2_int_operands)
4135 ret = note_integer_operands (ret);
4136
4137 if (eptype)
4138 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4139
4140 protected_set_expr_location (ret, loc);
4141 return ret;
4142 }
4143
4144 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4145 which we are casting. OTYPE is the type of the expression being
4146 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4147 on the command line. */
4148
4149 static void
4150 handle_warn_cast_qual (tree type, tree otype)
4151 {
4152 tree in_type = type;
4153 tree in_otype = otype;
4154 int added = 0;
4155 int discarded = 0;
4156 bool is_const;
4157
4158 /* Check that the qualifiers on IN_TYPE are a superset of the
4159 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4160 nodes is uninteresting and we stop as soon as we hit a
4161 non-POINTER_TYPE node on either type. */
4162 do
4163 {
4164 in_otype = TREE_TYPE (in_otype);
4165 in_type = TREE_TYPE (in_type);
4166
4167 /* GNU C allows cv-qualified function types. 'const' means the
4168 function is very pure, 'volatile' means it can't return. We
4169 need to warn when such qualifiers are added, not when they're
4170 taken away. */
4171 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4172 && TREE_CODE (in_type) == FUNCTION_TYPE)
4173 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
4174 else
4175 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
4176 }
4177 while (TREE_CODE (in_type) == POINTER_TYPE
4178 && TREE_CODE (in_otype) == POINTER_TYPE);
4179
4180 if (added)
4181 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4182
4183 if (discarded)
4184 /* There are qualifiers present in IN_OTYPE that are not present
4185 in IN_TYPE. */
4186 warning (OPT_Wcast_qual,
4187 "cast discards qualifiers from pointer target type");
4188
4189 if (added || discarded)
4190 return;
4191
4192 /* A cast from **T to const **T is unsafe, because it can cause a
4193 const value to be changed with no additional warning. We only
4194 issue this warning if T is the same on both sides, and we only
4195 issue the warning if there are the same number of pointers on
4196 both sides, as otherwise the cast is clearly unsafe anyhow. A
4197 cast is unsafe when a qualifier is added at one level and const
4198 is not present at all outer levels.
4199
4200 To issue this warning, we check at each level whether the cast
4201 adds new qualifiers not already seen. We don't need to special
4202 case function types, as they won't have the same
4203 TYPE_MAIN_VARIANT. */
4204
4205 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4206 return;
4207 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4208 return;
4209
4210 in_type = type;
4211 in_otype = otype;
4212 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4213 do
4214 {
4215 in_type = TREE_TYPE (in_type);
4216 in_otype = TREE_TYPE (in_otype);
4217 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4218 && !is_const)
4219 {
4220 warning (OPT_Wcast_qual,
4221 ("new qualifiers in middle of multi-level non-const cast "
4222 "are unsafe"));
4223 break;
4224 }
4225 if (is_const)
4226 is_const = TYPE_READONLY (in_type);
4227 }
4228 while (TREE_CODE (in_type) == POINTER_TYPE);
4229 }
4230
4231 /* Build an expression representing a cast to type TYPE of expression EXPR.
4232 LOC is the location of the cast-- typically the open paren of the cast. */
4233
4234 tree
4235 build_c_cast (location_t loc, tree type, tree expr)
4236 {
4237 tree value;
4238
4239 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4240 expr = TREE_OPERAND (expr, 0);
4241
4242 value = expr;
4243
4244 if (type == error_mark_node || expr == error_mark_node)
4245 return error_mark_node;
4246
4247 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4248 only in <protocol> qualifications. But when constructing cast expressions,
4249 the protocols do matter and must be kept around. */
4250 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4251 return build1 (NOP_EXPR, type, expr);
4252
4253 type = TYPE_MAIN_VARIANT (type);
4254
4255 if (TREE_CODE (type) == ARRAY_TYPE)
4256 {
4257 error_at (loc, "cast specifies array type");
4258 return error_mark_node;
4259 }
4260
4261 if (TREE_CODE (type) == FUNCTION_TYPE)
4262 {
4263 error_at (loc, "cast specifies function type");
4264 return error_mark_node;
4265 }
4266
4267 if (!VOID_TYPE_P (type))
4268 {
4269 value = require_complete_type (value);
4270 if (value == error_mark_node)
4271 return error_mark_node;
4272 }
4273
4274 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4275 {
4276 if (TREE_CODE (type) == RECORD_TYPE
4277 || TREE_CODE (type) == UNION_TYPE)
4278 pedwarn (loc, OPT_pedantic,
4279 "ISO C forbids casting nonscalar to the same type");
4280 }
4281 else if (TREE_CODE (type) == UNION_TYPE)
4282 {
4283 tree field;
4284
4285 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4286 if (TREE_TYPE (field) != error_mark_node
4287 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4288 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4289 break;
4290
4291 if (field)
4292 {
4293 tree t;
4294
4295 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4296 t = digest_init (loc, type,
4297 build_constructor_single (type, field, value),
4298 NULL_TREE, false, true, 0);
4299 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4300 return t;
4301 }
4302 error_at (loc, "cast to union type from type not present in union");
4303 return error_mark_node;
4304 }
4305 else
4306 {
4307 tree otype, ovalue;
4308
4309 if (type == void_type_node)
4310 {
4311 tree t = build1 (CONVERT_EXPR, type, value);
4312 SET_EXPR_LOCATION (t, loc);
4313 return t;
4314 }
4315
4316 otype = TREE_TYPE (value);
4317
4318 /* Optionally warn about potentially worrisome casts. */
4319 if (warn_cast_qual
4320 && TREE_CODE (type) == POINTER_TYPE
4321 && TREE_CODE (otype) == POINTER_TYPE)
4322 handle_warn_cast_qual (type, otype);
4323
4324 /* Warn about possible alignment problems. */
4325 if (STRICT_ALIGNMENT
4326 && TREE_CODE (type) == POINTER_TYPE
4327 && TREE_CODE (otype) == POINTER_TYPE
4328 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4329 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4330 /* Don't warn about opaque types, where the actual alignment
4331 restriction is unknown. */
4332 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4333 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4334 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4335 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4336 warning_at (loc, OPT_Wcast_align,
4337 "cast increases required alignment of target type");
4338
4339 if (TREE_CODE (type) == INTEGER_TYPE
4340 && TREE_CODE (otype) == POINTER_TYPE
4341 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4342 /* Unlike conversion of integers to pointers, where the
4343 warning is disabled for converting constants because
4344 of cases such as SIG_*, warn about converting constant
4345 pointers to integers. In some cases it may cause unwanted
4346 sign extension, and a warning is appropriate. */
4347 warning_at (loc, OPT_Wpointer_to_int_cast,
4348 "cast from pointer to integer of different size");
4349
4350 if (TREE_CODE (value) == CALL_EXPR
4351 && TREE_CODE (type) != TREE_CODE (otype))
4352 warning_at (loc, OPT_Wbad_function_cast,
4353 "cast from function call of type %qT "
4354 "to non-matching type %qT", otype, type);
4355
4356 if (TREE_CODE (type) == POINTER_TYPE
4357 && TREE_CODE (otype) == INTEGER_TYPE
4358 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4359 /* Don't warn about converting any constant. */
4360 && !TREE_CONSTANT (value))
4361 warning_at (loc,
4362 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4363 "of different size");
4364
4365 if (warn_strict_aliasing <= 2)
4366 strict_aliasing_warning (otype, type, expr);
4367
4368 /* If pedantic, warn for conversions between function and object
4369 pointer types, except for converting a null pointer constant
4370 to function pointer type. */
4371 if (pedantic
4372 && TREE_CODE (type) == POINTER_TYPE
4373 && TREE_CODE (otype) == POINTER_TYPE
4374 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4375 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4376 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4377 "conversion of function pointer to object pointer type");
4378
4379 if (pedantic
4380 && TREE_CODE (type) == POINTER_TYPE
4381 && TREE_CODE (otype) == POINTER_TYPE
4382 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4383 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4384 && !null_pointer_constant_p (value))
4385 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4386 "conversion of object pointer to function pointer type");
4387
4388 ovalue = value;
4389 value = convert (type, value);
4390
4391 /* Ignore any integer overflow caused by the cast. */
4392 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4393 {
4394 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4395 {
4396 if (!TREE_OVERFLOW (value))
4397 {
4398 /* Avoid clobbering a shared constant. */
4399 value = copy_node (value);
4400 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4401 }
4402 }
4403 else if (TREE_OVERFLOW (value))
4404 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4405 value = build_int_cst_wide (TREE_TYPE (value),
4406 TREE_INT_CST_LOW (value),
4407 TREE_INT_CST_HIGH (value));
4408 }
4409 }
4410
4411 /* Don't let a cast be an lvalue. */
4412 if (value == expr)
4413 value = non_lvalue_loc (loc, value);
4414
4415 /* Don't allow the results of casting to floating-point or complex
4416 types be confused with actual constants, or casts involving
4417 integer and pointer types other than direct integer-to-integer
4418 and integer-to-pointer be confused with integer constant
4419 expressions and null pointer constants. */
4420 if (TREE_CODE (value) == REAL_CST
4421 || TREE_CODE (value) == COMPLEX_CST
4422 || (TREE_CODE (value) == INTEGER_CST
4423 && !((TREE_CODE (expr) == INTEGER_CST
4424 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4425 || TREE_CODE (expr) == REAL_CST
4426 || TREE_CODE (expr) == COMPLEX_CST)))
4427 value = build1 (NOP_EXPR, type, value);
4428
4429 if (CAN_HAVE_LOCATION_P (value))
4430 SET_EXPR_LOCATION (value, loc);
4431 return value;
4432 }
4433
4434 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4435 location of the open paren of the cast, or the position of the cast
4436 expr. */
4437 tree
4438 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4439 {
4440 tree type;
4441 tree type_expr = NULL_TREE;
4442 bool type_expr_const = true;
4443 tree ret;
4444 int saved_wsp = warn_strict_prototypes;
4445
4446 /* This avoids warnings about unprototyped casts on
4447 integers. E.g. "#define SIG_DFL (void(*)())0". */
4448 if (TREE_CODE (expr) == INTEGER_CST)
4449 warn_strict_prototypes = 0;
4450 type = groktypename (type_name, &type_expr, &type_expr_const);
4451 warn_strict_prototypes = saved_wsp;
4452
4453 ret = build_c_cast (loc, type, expr);
4454 if (type_expr)
4455 {
4456 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4457 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4458 SET_EXPR_LOCATION (ret, loc);
4459 }
4460
4461 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4462 SET_EXPR_LOCATION (ret, loc);
4463
4464 /* C++ does not permits types to be defined in a cast. */
4465 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4466 warning_at (loc, OPT_Wc___compat,
4467 "defining a type in a cast is invalid in C++");
4468
4469 return ret;
4470 }
4471 \f
4472 /* Build an assignment expression of lvalue LHS from value RHS.
4473 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4474 may differ from TREE_TYPE (LHS) for an enum bitfield.
4475 MODIFYCODE is the code for a binary operator that we use
4476 to combine the old value of LHS with RHS to get the new value.
4477 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4478 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4479 which may differ from TREE_TYPE (RHS) for an enum value.
4480
4481 LOCATION is the location of the MODIFYCODE operator.
4482 RHS_LOC is the location of the RHS. */
4483
4484 tree
4485 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4486 enum tree_code modifycode,
4487 location_t rhs_loc, tree rhs, tree rhs_origtype)
4488 {
4489 tree result;
4490 tree newrhs;
4491 tree rhs_semantic_type = NULL_TREE;
4492 tree lhstype = TREE_TYPE (lhs);
4493 tree olhstype = lhstype;
4494 bool npc;
4495
4496 /* Types that aren't fully specified cannot be used in assignments. */
4497 lhs = require_complete_type (lhs);
4498
4499 /* Avoid duplicate error messages from operands that had errors. */
4500 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4501 return error_mark_node;
4502
4503 if (!lvalue_or_else (lhs, lv_assign))
4504 return error_mark_node;
4505
4506 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4507 {
4508 rhs_semantic_type = TREE_TYPE (rhs);
4509 rhs = TREE_OPERAND (rhs, 0);
4510 }
4511
4512 newrhs = rhs;
4513
4514 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4515 {
4516 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4517 lhs_origtype, modifycode, rhs_loc, rhs,
4518 rhs_origtype);
4519 if (inner == error_mark_node)
4520 return error_mark_node;
4521 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4522 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4523 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4524 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4525 protected_set_expr_location (result, location);
4526 return result;
4527 }
4528
4529 /* If a binary op has been requested, combine the old LHS value with the RHS
4530 producing the value we should actually store into the LHS. */
4531
4532 if (modifycode != NOP_EXPR)
4533 {
4534 lhs = c_fully_fold (lhs, false, NULL);
4535 lhs = stabilize_reference (lhs);
4536 newrhs = build_binary_op (location,
4537 modifycode, lhs, rhs, 1);
4538
4539 /* The original type of the right hand side is no longer
4540 meaningful. */
4541 rhs_origtype = NULL_TREE;
4542 }
4543
4544 /* Give an error for storing in something that is 'const'. */
4545
4546 if (TYPE_READONLY (lhstype)
4547 || ((TREE_CODE (lhstype) == RECORD_TYPE
4548 || TREE_CODE (lhstype) == UNION_TYPE)
4549 && C_TYPE_FIELDS_READONLY (lhstype)))
4550 {
4551 readonly_error (lhs, lv_assign);
4552 return error_mark_node;
4553 }
4554 else if (TREE_READONLY (lhs))
4555 readonly_warning (lhs, lv_assign);
4556
4557 /* If storing into a structure or union member,
4558 it has probably been given type `int'.
4559 Compute the type that would go with
4560 the actual amount of storage the member occupies. */
4561
4562 if (TREE_CODE (lhs) == COMPONENT_REF
4563 && (TREE_CODE (lhstype) == INTEGER_TYPE
4564 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4565 || TREE_CODE (lhstype) == REAL_TYPE
4566 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4567 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4568
4569 /* If storing in a field that is in actuality a short or narrower than one,
4570 we must store in the field in its actual type. */
4571
4572 if (lhstype != TREE_TYPE (lhs))
4573 {
4574 lhs = copy_node (lhs);
4575 TREE_TYPE (lhs) = lhstype;
4576 }
4577
4578 /* Issue -Wc++-compat warnings about an assignment to an enum type
4579 when LHS does not have its original type. This happens for,
4580 e.g., an enum bitfield in a struct. */
4581 if (warn_cxx_compat
4582 && lhs_origtype != NULL_TREE
4583 && lhs_origtype != lhstype
4584 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4585 {
4586 tree checktype = (rhs_origtype != NULL_TREE
4587 ? rhs_origtype
4588 : TREE_TYPE (rhs));
4589 if (checktype != error_mark_node
4590 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4591 warning_at (location, OPT_Wc___compat,
4592 "enum conversion in assignment is invalid in C++");
4593 }
4594
4595 /* Convert new value to destination type. Fold it first, then
4596 restore any excess precision information, for the sake of
4597 conversion warnings. */
4598
4599 npc = null_pointer_constant_p (newrhs);
4600 newrhs = c_fully_fold (newrhs, false, NULL);
4601 if (rhs_semantic_type)
4602 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4603 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4604 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4605 if (TREE_CODE (newrhs) == ERROR_MARK)
4606 return error_mark_node;
4607
4608 /* Emit ObjC write barrier, if necessary. */
4609 if (c_dialect_objc () && flag_objc_gc)
4610 {
4611 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4612 if (result)
4613 {
4614 protected_set_expr_location (result, location);
4615 return result;
4616 }
4617 }
4618
4619 /* Scan operands. */
4620
4621 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4622 TREE_SIDE_EFFECTS (result) = 1;
4623 protected_set_expr_location (result, location);
4624
4625 /* If we got the LHS in a different type for storing in,
4626 convert the result back to the nominal type of LHS
4627 so that the value we return always has the same type
4628 as the LHS argument. */
4629
4630 if (olhstype == TREE_TYPE (result))
4631 return result;
4632
4633 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4634 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4635 protected_set_expr_location (result, location);
4636 return result;
4637 }
4638 \f
4639 /* Convert value RHS to type TYPE as preparation for an assignment to
4640 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4641 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4642 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4643 constant before any folding.
4644 The real work of conversion is done by `convert'.
4645 The purpose of this function is to generate error messages
4646 for assignments that are not allowed in C.
4647 ERRTYPE says whether it is argument passing, assignment,
4648 initialization or return.
4649
4650 LOCATION is the location of the RHS.
4651 FUNCTION is a tree for the function being called.
4652 PARMNUM is the number of the argument, for printing in error messages. */
4653
4654 static tree
4655 convert_for_assignment (location_t location, tree type, tree rhs,
4656 tree origtype, enum impl_conv errtype,
4657 bool null_pointer_constant, tree fundecl,
4658 tree function, int parmnum)
4659 {
4660 enum tree_code codel = TREE_CODE (type);
4661 tree orig_rhs = rhs;
4662 tree rhstype;
4663 enum tree_code coder;
4664 tree rname = NULL_TREE;
4665 bool objc_ok = false;
4666
4667 if (errtype == ic_argpass)
4668 {
4669 tree selector;
4670 /* Change pointer to function to the function itself for
4671 diagnostics. */
4672 if (TREE_CODE (function) == ADDR_EXPR
4673 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4674 function = TREE_OPERAND (function, 0);
4675
4676 /* Handle an ObjC selector specially for diagnostics. */
4677 selector = objc_message_selector ();
4678 rname = function;
4679 if (selector && parmnum > 2)
4680 {
4681 rname = selector;
4682 parmnum -= 2;
4683 }
4684 }
4685
4686 /* This macro is used to emit diagnostics to ensure that all format
4687 strings are complete sentences, visible to gettext and checked at
4688 compile time. */
4689 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4690 do { \
4691 switch (errtype) \
4692 { \
4693 case ic_argpass: \
4694 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4695 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4696 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4697 "expected %qT but argument is of type %qT", \
4698 type, rhstype); \
4699 break; \
4700 case ic_assign: \
4701 pedwarn (LOCATION, OPT, AS); \
4702 break; \
4703 case ic_init: \
4704 pedwarn (LOCATION, OPT, IN); \
4705 break; \
4706 case ic_return: \
4707 pedwarn (LOCATION, OPT, RE); \
4708 break; \
4709 default: \
4710 gcc_unreachable (); \
4711 } \
4712 } while (0)
4713
4714 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4715 rhs = TREE_OPERAND (rhs, 0);
4716
4717 rhstype = TREE_TYPE (rhs);
4718 coder = TREE_CODE (rhstype);
4719
4720 if (coder == ERROR_MARK)
4721 return error_mark_node;
4722
4723 if (c_dialect_objc ())
4724 {
4725 int parmno;
4726
4727 switch (errtype)
4728 {
4729 case ic_return:
4730 parmno = 0;
4731 break;
4732
4733 case ic_assign:
4734 parmno = -1;
4735 break;
4736
4737 case ic_init:
4738 parmno = -2;
4739 break;
4740
4741 default:
4742 parmno = parmnum;
4743 break;
4744 }
4745
4746 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4747 }
4748
4749 if (warn_cxx_compat)
4750 {
4751 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4752 if (checktype != error_mark_node
4753 && TREE_CODE (type) == ENUMERAL_TYPE
4754 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4755 {
4756 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4757 G_("enum conversion when passing argument "
4758 "%d of %qE is invalid in C++"),
4759 G_("enum conversion in assignment is "
4760 "invalid in C++"),
4761 G_("enum conversion in initialization is "
4762 "invalid in C++"),
4763 G_("enum conversion in return is "
4764 "invalid in C++"));
4765 }
4766 }
4767
4768 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4769 return rhs;
4770
4771 if (coder == VOID_TYPE)
4772 {
4773 /* Except for passing an argument to an unprototyped function,
4774 this is a constraint violation. When passing an argument to
4775 an unprototyped function, it is compile-time undefined;
4776 making it a constraint in that case was rejected in
4777 DR#252. */
4778 error_at (location, "void value not ignored as it ought to be");
4779 return error_mark_node;
4780 }
4781 rhs = require_complete_type (rhs);
4782 if (rhs == error_mark_node)
4783 return error_mark_node;
4784 /* A type converts to a reference to it.
4785 This code doesn't fully support references, it's just for the
4786 special case of va_start and va_copy. */
4787 if (codel == REFERENCE_TYPE
4788 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4789 {
4790 if (!lvalue_p (rhs))
4791 {
4792 error_at (location, "cannot pass rvalue to reference parameter");
4793 return error_mark_node;
4794 }
4795 if (!c_mark_addressable (rhs))
4796 return error_mark_node;
4797 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4798 SET_EXPR_LOCATION (rhs, location);
4799
4800 /* We already know that these two types are compatible, but they
4801 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4802 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4803 likely to be va_list, a typedef to __builtin_va_list, which
4804 is different enough that it will cause problems later. */
4805 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4806 {
4807 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4808 SET_EXPR_LOCATION (rhs, location);
4809 }
4810
4811 rhs = build1 (NOP_EXPR, type, rhs);
4812 SET_EXPR_LOCATION (rhs, location);
4813 return rhs;
4814 }
4815 /* Some types can interconvert without explicit casts. */
4816 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4817 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4818 return convert (type, rhs);
4819 /* Arithmetic types all interconvert, and enum is treated like int. */
4820 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4821 || codel == FIXED_POINT_TYPE
4822 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4823 || codel == BOOLEAN_TYPE)
4824 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4825 || coder == FIXED_POINT_TYPE
4826 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4827 || coder == BOOLEAN_TYPE))
4828 {
4829 tree ret;
4830 bool save = in_late_binary_op;
4831 if (codel == BOOLEAN_TYPE)
4832 in_late_binary_op = true;
4833 ret = convert_and_check (type, orig_rhs);
4834 if (codel == BOOLEAN_TYPE)
4835 in_late_binary_op = save;
4836 return ret;
4837 }
4838
4839 /* Aggregates in different TUs might need conversion. */
4840 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4841 && codel == coder
4842 && comptypes (type, rhstype))
4843 return convert_and_check (type, rhs);
4844
4845 /* Conversion to a transparent union from its member types.
4846 This applies only to function arguments. */
4847 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4848 && errtype == ic_argpass)
4849 {
4850 tree memb, marginal_memb = NULL_TREE;
4851
4852 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4853 {
4854 tree memb_type = TREE_TYPE (memb);
4855
4856 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4857 TYPE_MAIN_VARIANT (rhstype)))
4858 break;
4859
4860 if (TREE_CODE (memb_type) != POINTER_TYPE)
4861 continue;
4862
4863 if (coder == POINTER_TYPE)
4864 {
4865 tree ttl = TREE_TYPE (memb_type);
4866 tree ttr = TREE_TYPE (rhstype);
4867
4868 /* Any non-function converts to a [const][volatile] void *
4869 and vice versa; otherwise, targets must be the same.
4870 Meanwhile, the lhs target must have all the qualifiers of
4871 the rhs. */
4872 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4873 || comp_target_types (location, memb_type, rhstype))
4874 {
4875 /* If this type won't generate any warnings, use it. */
4876 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4877 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4878 && TREE_CODE (ttl) == FUNCTION_TYPE)
4879 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4880 == TYPE_QUALS (ttr))
4881 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4882 == TYPE_QUALS (ttl))))
4883 break;
4884
4885 /* Keep looking for a better type, but remember this one. */
4886 if (!marginal_memb)
4887 marginal_memb = memb;
4888 }
4889 }
4890
4891 /* Can convert integer zero to any pointer type. */
4892 if (null_pointer_constant)
4893 {
4894 rhs = null_pointer_node;
4895 break;
4896 }
4897 }
4898
4899 if (memb || marginal_memb)
4900 {
4901 if (!memb)
4902 {
4903 /* We have only a marginally acceptable member type;
4904 it needs a warning. */
4905 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4906 tree ttr = TREE_TYPE (rhstype);
4907
4908 /* Const and volatile mean something different for function
4909 types, so the usual warnings are not appropriate. */
4910 if (TREE_CODE (ttr) == FUNCTION_TYPE
4911 && TREE_CODE (ttl) == FUNCTION_TYPE)
4912 {
4913 /* Because const and volatile on functions are
4914 restrictions that say the function will not do
4915 certain things, it is okay to use a const or volatile
4916 function where an ordinary one is wanted, but not
4917 vice-versa. */
4918 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4919 WARN_FOR_ASSIGNMENT (location, 0,
4920 G_("passing argument %d of %qE "
4921 "makes qualified function "
4922 "pointer from unqualified"),
4923 G_("assignment makes qualified "
4924 "function pointer from "
4925 "unqualified"),
4926 G_("initialization makes qualified "
4927 "function pointer from "
4928 "unqualified"),
4929 G_("return makes qualified function "
4930 "pointer from unqualified"));
4931 }
4932 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4933 WARN_FOR_ASSIGNMENT (location, 0,
4934 G_("passing argument %d of %qE discards "
4935 "qualifiers from pointer target type"),
4936 G_("assignment discards qualifiers "
4937 "from pointer target type"),
4938 G_("initialization discards qualifiers "
4939 "from pointer target type"),
4940 G_("return discards qualifiers from "
4941 "pointer target type"));
4942
4943 memb = marginal_memb;
4944 }
4945
4946 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
4947 pedwarn (location, OPT_pedantic,
4948 "ISO C prohibits argument conversion to union type");
4949
4950 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
4951 return build_constructor_single (type, memb, rhs);
4952 }
4953 }
4954
4955 /* Conversions among pointers */
4956 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4957 && (coder == codel))
4958 {
4959 tree ttl = TREE_TYPE (type);
4960 tree ttr = TREE_TYPE (rhstype);
4961 tree mvl = ttl;
4962 tree mvr = ttr;
4963 bool is_opaque_pointer;
4964 int target_cmp = 0; /* Cache comp_target_types () result. */
4965
4966 if (TREE_CODE (mvl) != ARRAY_TYPE)
4967 mvl = TYPE_MAIN_VARIANT (mvl);
4968 if (TREE_CODE (mvr) != ARRAY_TYPE)
4969 mvr = TYPE_MAIN_VARIANT (mvr);
4970 /* Opaque pointers are treated like void pointers. */
4971 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
4972
4973 /* C++ does not allow the implicit conversion void* -> T*. However,
4974 for the purpose of reducing the number of false positives, we
4975 tolerate the special case of
4976
4977 int *p = NULL;
4978
4979 where NULL is typically defined in C to be '(void *) 0'. */
4980 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4981 warning_at (location, OPT_Wc___compat,
4982 "request for implicit conversion "
4983 "from %qT to %qT not permitted in C++", rhstype, type);
4984
4985 /* Check if the right-hand side has a format attribute but the
4986 left-hand side doesn't. */
4987 if (warn_missing_format_attribute
4988 && check_missing_format_attribute (type, rhstype))
4989 {
4990 switch (errtype)
4991 {
4992 case ic_argpass:
4993 warning_at (location, OPT_Wmissing_format_attribute,
4994 "argument %d of %qE might be "
4995 "a candidate for a format attribute",
4996 parmnum, rname);
4997 break;
4998 case ic_assign:
4999 warning_at (location, OPT_Wmissing_format_attribute,
5000 "assignment left-hand side might be "
5001 "a candidate for a format attribute");
5002 break;
5003 case ic_init:
5004 warning_at (location, OPT_Wmissing_format_attribute,
5005 "initialization left-hand side might be "
5006 "a candidate for a format attribute");
5007 break;
5008 case ic_return:
5009 warning_at (location, OPT_Wmissing_format_attribute,
5010 "return type might be "
5011 "a candidate for a format attribute");
5012 break;
5013 default:
5014 gcc_unreachable ();
5015 }
5016 }
5017
5018 /* Any non-function converts to a [const][volatile] void *
5019 and vice versa; otherwise, targets must be the same.
5020 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5021 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5022 || (target_cmp = comp_target_types (location, type, rhstype))
5023 || is_opaque_pointer
5024 || (c_common_unsigned_type (mvl)
5025 == c_common_unsigned_type (mvr)))
5026 {
5027 if (pedantic
5028 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5029 ||
5030 (VOID_TYPE_P (ttr)
5031 && !null_pointer_constant
5032 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5033 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5034 G_("ISO C forbids passing argument %d of "
5035 "%qE between function pointer "
5036 "and %<void *%>"),
5037 G_("ISO C forbids assignment between "
5038 "function pointer and %<void *%>"),
5039 G_("ISO C forbids initialization between "
5040 "function pointer and %<void *%>"),
5041 G_("ISO C forbids return between function "
5042 "pointer and %<void *%>"));
5043 /* Const and volatile mean something different for function types,
5044 so the usual warnings are not appropriate. */
5045 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5046 && TREE_CODE (ttl) != FUNCTION_TYPE)
5047 {
5048 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
5049 {
5050 /* Types differing only by the presence of the 'volatile'
5051 qualifier are acceptable if the 'volatile' has been added
5052 in by the Objective-C EH machinery. */
5053 if (!objc_type_quals_match (ttl, ttr))
5054 WARN_FOR_ASSIGNMENT (location, 0,
5055 G_("passing argument %d of %qE discards "
5056 "qualifiers from pointer target type"),
5057 G_("assignment discards qualifiers "
5058 "from pointer target type"),
5059 G_("initialization discards qualifiers "
5060 "from pointer target type"),
5061 G_("return discards qualifiers from "
5062 "pointer target type"));
5063 }
5064 /* If this is not a case of ignoring a mismatch in signedness,
5065 no warning. */
5066 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5067 || target_cmp)
5068 ;
5069 /* If there is a mismatch, do warn. */
5070 else if (warn_pointer_sign)
5071 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5072 G_("pointer targets in passing argument "
5073 "%d of %qE differ in signedness"),
5074 G_("pointer targets in assignment "
5075 "differ in signedness"),
5076 G_("pointer targets in initialization "
5077 "differ in signedness"),
5078 G_("pointer targets in return differ "
5079 "in signedness"));
5080 }
5081 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5082 && TREE_CODE (ttr) == FUNCTION_TYPE)
5083 {
5084 /* Because const and volatile on functions are restrictions
5085 that say the function will not do certain things,
5086 it is okay to use a const or volatile function
5087 where an ordinary one is wanted, but not vice-versa. */
5088 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
5089 WARN_FOR_ASSIGNMENT (location, 0,
5090 G_("passing argument %d of %qE makes "
5091 "qualified function pointer "
5092 "from unqualified"),
5093 G_("assignment makes qualified function "
5094 "pointer from unqualified"),
5095 G_("initialization makes qualified "
5096 "function pointer from unqualified"),
5097 G_("return makes qualified function "
5098 "pointer from unqualified"));
5099 }
5100 }
5101 else
5102 /* Avoid warning about the volatile ObjC EH puts on decls. */
5103 if (!objc_ok)
5104 WARN_FOR_ASSIGNMENT (location, 0,
5105 G_("passing argument %d of %qE from "
5106 "incompatible pointer type"),
5107 G_("assignment from incompatible pointer type"),
5108 G_("initialization from incompatible "
5109 "pointer type"),
5110 G_("return from incompatible pointer type"));
5111
5112 return convert (type, rhs);
5113 }
5114 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5115 {
5116 /* ??? This should not be an error when inlining calls to
5117 unprototyped functions. */
5118 error_at (location, "invalid use of non-lvalue array");
5119 return error_mark_node;
5120 }
5121 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5122 {
5123 /* An explicit constant 0 can convert to a pointer,
5124 or one that results from arithmetic, even including
5125 a cast to integer type. */
5126 if (!null_pointer_constant)
5127 WARN_FOR_ASSIGNMENT (location, 0,
5128 G_("passing argument %d of %qE makes "
5129 "pointer from integer without a cast"),
5130 G_("assignment makes pointer from integer "
5131 "without a cast"),
5132 G_("initialization makes pointer from "
5133 "integer without a cast"),
5134 G_("return makes pointer from integer "
5135 "without a cast"));
5136
5137 return convert (type, rhs);
5138 }
5139 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5140 {
5141 WARN_FOR_ASSIGNMENT (location, 0,
5142 G_("passing argument %d of %qE makes integer "
5143 "from pointer without a cast"),
5144 G_("assignment makes integer from pointer "
5145 "without a cast"),
5146 G_("initialization makes integer from pointer "
5147 "without a cast"),
5148 G_("return makes integer from pointer "
5149 "without a cast"));
5150 return convert (type, rhs);
5151 }
5152 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5153 {
5154 tree ret;
5155 bool save = in_late_binary_op;
5156 in_late_binary_op = true;
5157 ret = convert (type, rhs);
5158 in_late_binary_op = save;
5159 return ret;
5160 }
5161
5162 switch (errtype)
5163 {
5164 case ic_argpass:
5165 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5166 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5167 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5168 "expected %qT but argument is of type %qT", type, rhstype);
5169 break;
5170 case ic_assign:
5171 error_at (location, "incompatible types when assigning to type %qT from "
5172 "type %qT", type, rhstype);
5173 break;
5174 case ic_init:
5175 error_at (location,
5176 "incompatible types when initializing type %qT using type %qT",
5177 type, rhstype);
5178 break;
5179 case ic_return:
5180 error_at (location,
5181 "incompatible types when returning type %qT but %qT was "
5182 "expected", rhstype, type);
5183 break;
5184 default:
5185 gcc_unreachable ();
5186 }
5187
5188 return error_mark_node;
5189 }
5190 \f
5191 /* If VALUE is a compound expr all of whose expressions are constant, then
5192 return its value. Otherwise, return error_mark_node.
5193
5194 This is for handling COMPOUND_EXPRs as initializer elements
5195 which is allowed with a warning when -pedantic is specified. */
5196
5197 static tree
5198 valid_compound_expr_initializer (tree value, tree endtype)
5199 {
5200 if (TREE_CODE (value) == COMPOUND_EXPR)
5201 {
5202 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5203 == error_mark_node)
5204 return error_mark_node;
5205 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5206 endtype);
5207 }
5208 else if (!initializer_constant_valid_p (value, endtype))
5209 return error_mark_node;
5210 else
5211 return value;
5212 }
5213 \f
5214 /* Perform appropriate conversions on the initial value of a variable,
5215 store it in the declaration DECL,
5216 and print any error messages that are appropriate.
5217 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5218 If the init is invalid, store an ERROR_MARK.
5219
5220 INIT_LOC is the location of the initial value. */
5221
5222 void
5223 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5224 {
5225 tree value, type;
5226 bool npc = false;
5227
5228 /* If variable's type was invalidly declared, just ignore it. */
5229
5230 type = TREE_TYPE (decl);
5231 if (TREE_CODE (type) == ERROR_MARK)
5232 return;
5233
5234 /* Digest the specified initializer into an expression. */
5235
5236 if (init)
5237 npc = null_pointer_constant_p (init);
5238 value = digest_init (init_loc, type, init, origtype, npc,
5239 true, TREE_STATIC (decl));
5240
5241 /* Store the expression if valid; else report error. */
5242
5243 if (!in_system_header
5244 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5245 warning (OPT_Wtraditional, "traditional C rejects automatic "
5246 "aggregate initialization");
5247
5248 DECL_INITIAL (decl) = value;
5249
5250 /* ANSI wants warnings about out-of-range constant initializers. */
5251 STRIP_TYPE_NOPS (value);
5252 if (TREE_STATIC (decl))
5253 constant_expression_warning (value);
5254
5255 /* Check if we need to set array size from compound literal size. */
5256 if (TREE_CODE (type) == ARRAY_TYPE
5257 && TYPE_DOMAIN (type) == 0
5258 && value != error_mark_node)
5259 {
5260 tree inside_init = init;
5261
5262 STRIP_TYPE_NOPS (inside_init);
5263 inside_init = fold (inside_init);
5264
5265 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5266 {
5267 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5268
5269 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5270 {
5271 /* For int foo[] = (int [3]){1}; we need to set array size
5272 now since later on array initializer will be just the
5273 brace enclosed list of the compound literal. */
5274 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5275 TREE_TYPE (decl) = type;
5276 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5277 layout_type (type);
5278 layout_decl (cldecl, 0);
5279 }
5280 }
5281 }
5282 }
5283 \f
5284 /* Methods for storing and printing names for error messages. */
5285
5286 /* Implement a spelling stack that allows components of a name to be pushed
5287 and popped. Each element on the stack is this structure. */
5288
5289 struct spelling
5290 {
5291 int kind;
5292 union
5293 {
5294 unsigned HOST_WIDE_INT i;
5295 const char *s;
5296 } u;
5297 };
5298
5299 #define SPELLING_STRING 1
5300 #define SPELLING_MEMBER 2
5301 #define SPELLING_BOUNDS 3
5302
5303 static struct spelling *spelling; /* Next stack element (unused). */
5304 static struct spelling *spelling_base; /* Spelling stack base. */
5305 static int spelling_size; /* Size of the spelling stack. */
5306
5307 /* Macros to save and restore the spelling stack around push_... functions.
5308 Alternative to SAVE_SPELLING_STACK. */
5309
5310 #define SPELLING_DEPTH() (spelling - spelling_base)
5311 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5312
5313 /* Push an element on the spelling stack with type KIND and assign VALUE
5314 to MEMBER. */
5315
5316 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5317 { \
5318 int depth = SPELLING_DEPTH (); \
5319 \
5320 if (depth >= spelling_size) \
5321 { \
5322 spelling_size += 10; \
5323 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5324 spelling_size); \
5325 RESTORE_SPELLING_DEPTH (depth); \
5326 } \
5327 \
5328 spelling->kind = (KIND); \
5329 spelling->MEMBER = (VALUE); \
5330 spelling++; \
5331 }
5332
5333 /* Push STRING on the stack. Printed literally. */
5334
5335 static void
5336 push_string (const char *string)
5337 {
5338 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5339 }
5340
5341 /* Push a member name on the stack. Printed as '.' STRING. */
5342
5343 static void
5344 push_member_name (tree decl)
5345 {
5346 const char *const string
5347 = (DECL_NAME (decl)
5348 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5349 : _("<anonymous>"));
5350 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5351 }
5352
5353 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5354
5355 static void
5356 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5357 {
5358 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5359 }
5360
5361 /* Compute the maximum size in bytes of the printed spelling. */
5362
5363 static int
5364 spelling_length (void)
5365 {
5366 int size = 0;
5367 struct spelling *p;
5368
5369 for (p = spelling_base; p < spelling; p++)
5370 {
5371 if (p->kind == SPELLING_BOUNDS)
5372 size += 25;
5373 else
5374 size += strlen (p->u.s) + 1;
5375 }
5376
5377 return size;
5378 }
5379
5380 /* Print the spelling to BUFFER and return it. */
5381
5382 static char *
5383 print_spelling (char *buffer)
5384 {
5385 char *d = buffer;
5386 struct spelling *p;
5387
5388 for (p = spelling_base; p < spelling; p++)
5389 if (p->kind == SPELLING_BOUNDS)
5390 {
5391 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5392 d += strlen (d);
5393 }
5394 else
5395 {
5396 const char *s;
5397 if (p->kind == SPELLING_MEMBER)
5398 *d++ = '.';
5399 for (s = p->u.s; (*d = *s++); d++)
5400 ;
5401 }
5402 *d++ = '\0';
5403 return buffer;
5404 }
5405
5406 /* Issue an error message for a bad initializer component.
5407 MSGID identifies the message.
5408 The component name is taken from the spelling stack. */
5409
5410 void
5411 error_init (const char *msgid)
5412 {
5413 char *ofwhat;
5414
5415 error ("%s", _(msgid));
5416 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5417 if (*ofwhat)
5418 error ("(near initialization for %qs)", ofwhat);
5419 }
5420
5421 /* Issue a pedantic warning for a bad initializer component. OPT is
5422 the option OPT_* (from options.h) controlling this warning or 0 if
5423 it is unconditionally given. MSGID identifies the message. The
5424 component name is taken from the spelling stack. */
5425
5426 void
5427 pedwarn_init (location_t location, int opt, const char *msgid)
5428 {
5429 char *ofwhat;
5430
5431 pedwarn (location, opt, "%s", _(msgid));
5432 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5433 if (*ofwhat)
5434 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5435 }
5436
5437 /* Issue a warning for a bad initializer component.
5438
5439 OPT is the OPT_W* value corresponding to the warning option that
5440 controls this warning. MSGID identifies the message. The
5441 component name is taken from the spelling stack. */
5442
5443 static void
5444 warning_init (int opt, const char *msgid)
5445 {
5446 char *ofwhat;
5447
5448 warning (opt, "%s", _(msgid));
5449 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5450 if (*ofwhat)
5451 warning (opt, "(near initialization for %qs)", ofwhat);
5452 }
5453 \f
5454 /* If TYPE is an array type and EXPR is a parenthesized string
5455 constant, warn if pedantic that EXPR is being used to initialize an
5456 object of type TYPE. */
5457
5458 void
5459 maybe_warn_string_init (tree type, struct c_expr expr)
5460 {
5461 if (pedantic
5462 && TREE_CODE (type) == ARRAY_TYPE
5463 && TREE_CODE (expr.value) == STRING_CST
5464 && expr.original_code != STRING_CST)
5465 pedwarn_init (input_location, OPT_pedantic,
5466 "array initialized from parenthesized string constant");
5467 }
5468
5469 /* Digest the parser output INIT as an initializer for type TYPE.
5470 Return a C expression of type TYPE to represent the initial value.
5471
5472 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5473
5474 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5475
5476 If INIT is a string constant, STRICT_STRING is true if it is
5477 unparenthesized or we should not warn here for it being parenthesized.
5478 For other types of INIT, STRICT_STRING is not used.
5479
5480 INIT_LOC is the location of the INIT.
5481
5482 REQUIRE_CONSTANT requests an error if non-constant initializers or
5483 elements are seen. */
5484
5485 static tree
5486 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5487 bool null_pointer_constant, bool strict_string,
5488 int require_constant)
5489 {
5490 enum tree_code code = TREE_CODE (type);
5491 tree inside_init = init;
5492 tree semantic_type = NULL_TREE;
5493 bool maybe_const = true;
5494
5495 if (type == error_mark_node
5496 || !init
5497 || init == error_mark_node
5498 || TREE_TYPE (init) == error_mark_node)
5499 return error_mark_node;
5500
5501 STRIP_TYPE_NOPS (inside_init);
5502
5503 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5504 {
5505 semantic_type = TREE_TYPE (inside_init);
5506 inside_init = TREE_OPERAND (inside_init, 0);
5507 }
5508 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5509 inside_init = decl_constant_value_for_optimization (inside_init);
5510
5511 /* Initialization of an array of chars from a string constant
5512 optionally enclosed in braces. */
5513
5514 if (code == ARRAY_TYPE && inside_init
5515 && TREE_CODE (inside_init) == STRING_CST)
5516 {
5517 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5518 /* Note that an array could be both an array of character type
5519 and an array of wchar_t if wchar_t is signed char or unsigned
5520 char. */
5521 bool char_array = (typ1 == char_type_node
5522 || typ1 == signed_char_type_node
5523 || typ1 == unsigned_char_type_node);
5524 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5525 bool char16_array = !!comptypes (typ1, char16_type_node);
5526 bool char32_array = !!comptypes (typ1, char32_type_node);
5527
5528 if (char_array || wchar_array || char16_array || char32_array)
5529 {
5530 struct c_expr expr;
5531 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5532 expr.value = inside_init;
5533 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5534 expr.original_type = NULL;
5535 maybe_warn_string_init (type, expr);
5536
5537 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5538 pedwarn_init (init_loc, OPT_pedantic,
5539 "initialization of a flexible array member");
5540
5541 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5542 TYPE_MAIN_VARIANT (type)))
5543 return inside_init;
5544
5545 if (char_array)
5546 {
5547 if (typ2 != char_type_node)
5548 {
5549 error_init ("char-array initialized from wide string");
5550 return error_mark_node;
5551 }
5552 }
5553 else
5554 {
5555 if (typ2 == char_type_node)
5556 {
5557 error_init ("wide character array initialized from non-wide "
5558 "string");
5559 return error_mark_node;
5560 }
5561 else if (!comptypes(typ1, typ2))
5562 {
5563 error_init ("wide character array initialized from "
5564 "incompatible wide string");
5565 return error_mark_node;
5566 }
5567 }
5568
5569 TREE_TYPE (inside_init) = type;
5570 if (TYPE_DOMAIN (type) != 0
5571 && TYPE_SIZE (type) != 0
5572 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5573 {
5574 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5575
5576 /* Subtract the size of a single (possibly wide) character
5577 because it's ok to ignore the terminating null char
5578 that is counted in the length of the constant. */
5579 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5580 (len
5581 - (TYPE_PRECISION (typ1)
5582 / BITS_PER_UNIT))))
5583 pedwarn_init (init_loc, 0,
5584 ("initializer-string for array of chars "
5585 "is too long"));
5586 else if (warn_cxx_compat
5587 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5588 warning_at (init_loc, OPT_Wc___compat,
5589 ("initializer-string for array chars "
5590 "is too long for C++"));
5591 }
5592
5593 return inside_init;
5594 }
5595 else if (INTEGRAL_TYPE_P (typ1))
5596 {
5597 error_init ("array of inappropriate type initialized "
5598 "from string constant");
5599 return error_mark_node;
5600 }
5601 }
5602
5603 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5604 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5605 below and handle as a constructor. */
5606 if (code == VECTOR_TYPE
5607 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5608 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5609 && TREE_CONSTANT (inside_init))
5610 {
5611 if (TREE_CODE (inside_init) == VECTOR_CST
5612 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5613 TYPE_MAIN_VARIANT (type)))
5614 return inside_init;
5615
5616 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5617 {
5618 unsigned HOST_WIDE_INT ix;
5619 tree value;
5620 bool constant_p = true;
5621
5622 /* Iterate through elements and check if all constructor
5623 elements are *_CSTs. */
5624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5625 if (!CONSTANT_CLASS_P (value))
5626 {
5627 constant_p = false;
5628 break;
5629 }
5630
5631 if (constant_p)
5632 return build_vector_from_ctor (type,
5633 CONSTRUCTOR_ELTS (inside_init));
5634 }
5635 }
5636
5637 if (warn_sequence_point)
5638 verify_sequence_points (inside_init);
5639
5640 /* Any type can be initialized
5641 from an expression of the same type, optionally with braces. */
5642
5643 if (inside_init && TREE_TYPE (inside_init) != 0
5644 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5645 TYPE_MAIN_VARIANT (type))
5646 || (code == ARRAY_TYPE
5647 && comptypes (TREE_TYPE (inside_init), type))
5648 || (code == VECTOR_TYPE
5649 && comptypes (TREE_TYPE (inside_init), type))
5650 || (code == POINTER_TYPE
5651 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5652 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5653 TREE_TYPE (type)))))
5654 {
5655 if (code == POINTER_TYPE)
5656 {
5657 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5658 {
5659 if (TREE_CODE (inside_init) == STRING_CST
5660 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5661 inside_init = array_to_pointer_conversion
5662 (init_loc, inside_init);
5663 else
5664 {
5665 error_init ("invalid use of non-lvalue array");
5666 return error_mark_node;
5667 }
5668 }
5669 }
5670
5671 if (code == VECTOR_TYPE)
5672 /* Although the types are compatible, we may require a
5673 conversion. */
5674 inside_init = convert (type, inside_init);
5675
5676 if (require_constant
5677 && (code == VECTOR_TYPE || !flag_isoc99)
5678 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5679 {
5680 /* As an extension, allow initializing objects with static storage
5681 duration with compound literals (which are then treated just as
5682 the brace enclosed list they contain). Also allow this for
5683 vectors, as we can only assign them with compound literals. */
5684 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5685 inside_init = DECL_INITIAL (decl);
5686 }
5687
5688 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5689 && TREE_CODE (inside_init) != CONSTRUCTOR)
5690 {
5691 error_init ("array initialized from non-constant array expression");
5692 return error_mark_node;
5693 }
5694
5695 /* Compound expressions can only occur here if -pedantic or
5696 -pedantic-errors is specified. In the later case, we always want
5697 an error. In the former case, we simply want a warning. */
5698 if (require_constant && pedantic
5699 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5700 {
5701 inside_init
5702 = valid_compound_expr_initializer (inside_init,
5703 TREE_TYPE (inside_init));
5704 if (inside_init == error_mark_node)
5705 error_init ("initializer element is not constant");
5706 else
5707 pedwarn_init (init_loc, OPT_pedantic,
5708 "initializer element is not constant");
5709 if (flag_pedantic_errors)
5710 inside_init = error_mark_node;
5711 }
5712 else if (require_constant
5713 && !initializer_constant_valid_p (inside_init,
5714 TREE_TYPE (inside_init)))
5715 {
5716 error_init ("initializer element is not constant");
5717 inside_init = error_mark_node;
5718 }
5719 else if (require_constant && !maybe_const)
5720 pedwarn_init (init_loc, 0,
5721 "initializer element is not a constant expression");
5722
5723 /* Added to enable additional -Wmissing-format-attribute warnings. */
5724 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5725 inside_init = convert_for_assignment (init_loc, type, inside_init,
5726 origtype,
5727 ic_init, null_pointer_constant,
5728 NULL_TREE, NULL_TREE, 0);
5729 return inside_init;
5730 }
5731
5732 /* Handle scalar types, including conversions. */
5733
5734 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5735 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5736 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5737 {
5738 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5739 && (TREE_CODE (init) == STRING_CST
5740 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5741 inside_init = init = array_to_pointer_conversion (init_loc, init);
5742 if (semantic_type)
5743 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5744 inside_init);
5745 inside_init
5746 = convert_for_assignment (init_loc, type, inside_init, origtype,
5747 ic_init, null_pointer_constant,
5748 NULL_TREE, NULL_TREE, 0);
5749
5750 /* Check to see if we have already given an error message. */
5751 if (inside_init == error_mark_node)
5752 ;
5753 else if (require_constant && !TREE_CONSTANT (inside_init))
5754 {
5755 error_init ("initializer element is not constant");
5756 inside_init = error_mark_node;
5757 }
5758 else if (require_constant
5759 && !initializer_constant_valid_p (inside_init,
5760 TREE_TYPE (inside_init)))
5761 {
5762 error_init ("initializer element is not computable at load time");
5763 inside_init = error_mark_node;
5764 }
5765 else if (require_constant && !maybe_const)
5766 pedwarn_init (init_loc, 0,
5767 "initializer element is not a constant expression");
5768
5769 return inside_init;
5770 }
5771
5772 /* Come here only for records and arrays. */
5773
5774 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5775 {
5776 error_init ("variable-sized object may not be initialized");
5777 return error_mark_node;
5778 }
5779
5780 error_init ("invalid initializer");
5781 return error_mark_node;
5782 }
5783 \f
5784 /* Handle initializers that use braces. */
5785
5786 /* Type of object we are accumulating a constructor for.
5787 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5788 static tree constructor_type;
5789
5790 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5791 left to fill. */
5792 static tree constructor_fields;
5793
5794 /* For an ARRAY_TYPE, this is the specified index
5795 at which to store the next element we get. */
5796 static tree constructor_index;
5797
5798 /* For an ARRAY_TYPE, this is the maximum index. */
5799 static tree constructor_max_index;
5800
5801 /* For a RECORD_TYPE, this is the first field not yet written out. */
5802 static tree constructor_unfilled_fields;
5803
5804 /* For an ARRAY_TYPE, this is the index of the first element
5805 not yet written out. */
5806 static tree constructor_unfilled_index;
5807
5808 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5809 This is so we can generate gaps between fields, when appropriate. */
5810 static tree constructor_bit_index;
5811
5812 /* If we are saving up the elements rather than allocating them,
5813 this is the list of elements so far (in reverse order,
5814 most recent first). */
5815 static VEC(constructor_elt,gc) *constructor_elements;
5816
5817 /* 1 if constructor should be incrementally stored into a constructor chain,
5818 0 if all the elements should be kept in AVL tree. */
5819 static int constructor_incremental;
5820
5821 /* 1 if so far this constructor's elements are all compile-time constants. */
5822 static int constructor_constant;
5823
5824 /* 1 if so far this constructor's elements are all valid address constants. */
5825 static int constructor_simple;
5826
5827 /* 1 if this constructor has an element that cannot be part of a
5828 constant expression. */
5829 static int constructor_nonconst;
5830
5831 /* 1 if this constructor is erroneous so far. */
5832 static int constructor_erroneous;
5833
5834 /* Structure for managing pending initializer elements, organized as an
5835 AVL tree. */
5836
5837 struct init_node
5838 {
5839 struct init_node *left, *right;
5840 struct init_node *parent;
5841 int balance;
5842 tree purpose;
5843 tree value;
5844 tree origtype;
5845 };
5846
5847 /* Tree of pending elements at this constructor level.
5848 These are elements encountered out of order
5849 which belong at places we haven't reached yet in actually
5850 writing the output.
5851 Will never hold tree nodes across GC runs. */
5852 static struct init_node *constructor_pending_elts;
5853
5854 /* The SPELLING_DEPTH of this constructor. */
5855 static int constructor_depth;
5856
5857 /* DECL node for which an initializer is being read.
5858 0 means we are reading a constructor expression
5859 such as (struct foo) {...}. */
5860 static tree constructor_decl;
5861
5862 /* Nonzero if this is an initializer for a top-level decl. */
5863 static int constructor_top_level;
5864
5865 /* Nonzero if there were any member designators in this initializer. */
5866 static int constructor_designated;
5867
5868 /* Nesting depth of designator list. */
5869 static int designator_depth;
5870
5871 /* Nonzero if there were diagnosed errors in this designator list. */
5872 static int designator_erroneous;
5873
5874 \f
5875 /* This stack has a level for each implicit or explicit level of
5876 structuring in the initializer, including the outermost one. It
5877 saves the values of most of the variables above. */
5878
5879 struct constructor_range_stack;
5880
5881 struct constructor_stack
5882 {
5883 struct constructor_stack *next;
5884 tree type;
5885 tree fields;
5886 tree index;
5887 tree max_index;
5888 tree unfilled_index;
5889 tree unfilled_fields;
5890 tree bit_index;
5891 VEC(constructor_elt,gc) *elements;
5892 struct init_node *pending_elts;
5893 int offset;
5894 int depth;
5895 /* If value nonzero, this value should replace the entire
5896 constructor at this level. */
5897 struct c_expr replacement_value;
5898 struct constructor_range_stack *range_stack;
5899 char constant;
5900 char simple;
5901 char nonconst;
5902 char implicit;
5903 char erroneous;
5904 char outer;
5905 char incremental;
5906 char designated;
5907 };
5908
5909 static struct constructor_stack *constructor_stack;
5910
5911 /* This stack represents designators from some range designator up to
5912 the last designator in the list. */
5913
5914 struct constructor_range_stack
5915 {
5916 struct constructor_range_stack *next, *prev;
5917 struct constructor_stack *stack;
5918 tree range_start;
5919 tree index;
5920 tree range_end;
5921 tree fields;
5922 };
5923
5924 static struct constructor_range_stack *constructor_range_stack;
5925
5926 /* This stack records separate initializers that are nested.
5927 Nested initializers can't happen in ANSI C, but GNU C allows them
5928 in cases like { ... (struct foo) { ... } ... }. */
5929
5930 struct initializer_stack
5931 {
5932 struct initializer_stack *next;
5933 tree decl;
5934 struct constructor_stack *constructor_stack;
5935 struct constructor_range_stack *constructor_range_stack;
5936 VEC(constructor_elt,gc) *elements;
5937 struct spelling *spelling;
5938 struct spelling *spelling_base;
5939 int spelling_size;
5940 char top_level;
5941 char require_constant_value;
5942 char require_constant_elements;
5943 };
5944
5945 static struct initializer_stack *initializer_stack;
5946 \f
5947 /* Prepare to parse and output the initializer for variable DECL. */
5948
5949 void
5950 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5951 {
5952 const char *locus;
5953 struct initializer_stack *p = XNEW (struct initializer_stack);
5954
5955 p->decl = constructor_decl;
5956 p->require_constant_value = require_constant_value;
5957 p->require_constant_elements = require_constant_elements;
5958 p->constructor_stack = constructor_stack;
5959 p->constructor_range_stack = constructor_range_stack;
5960 p->elements = constructor_elements;
5961 p->spelling = spelling;
5962 p->spelling_base = spelling_base;
5963 p->spelling_size = spelling_size;
5964 p->top_level = constructor_top_level;
5965 p->next = initializer_stack;
5966 initializer_stack = p;
5967
5968 constructor_decl = decl;
5969 constructor_designated = 0;
5970 constructor_top_level = top_level;
5971
5972 if (decl != 0 && decl != error_mark_node)
5973 {
5974 require_constant_value = TREE_STATIC (decl);
5975 require_constant_elements
5976 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5977 /* For a scalar, you can always use any value to initialize,
5978 even within braces. */
5979 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5980 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5981 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5982 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5983 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
5984 }
5985 else
5986 {
5987 require_constant_value = 0;
5988 require_constant_elements = 0;
5989 locus = _("(anonymous)");
5990 }
5991
5992 constructor_stack = 0;
5993 constructor_range_stack = 0;
5994
5995 missing_braces_mentioned = 0;
5996
5997 spelling_base = 0;
5998 spelling_size = 0;
5999 RESTORE_SPELLING_DEPTH (0);
6000
6001 if (locus)
6002 push_string (locus);
6003 }
6004
6005 void
6006 finish_init (void)
6007 {
6008 struct initializer_stack *p = initializer_stack;
6009
6010 /* Free the whole constructor stack of this initializer. */
6011 while (constructor_stack)
6012 {
6013 struct constructor_stack *q = constructor_stack;
6014 constructor_stack = q->next;
6015 free (q);
6016 }
6017
6018 gcc_assert (!constructor_range_stack);
6019
6020 /* Pop back to the data of the outer initializer (if any). */
6021 free (spelling_base);
6022
6023 constructor_decl = p->decl;
6024 require_constant_value = p->require_constant_value;
6025 require_constant_elements = p->require_constant_elements;
6026 constructor_stack = p->constructor_stack;
6027 constructor_range_stack = p->constructor_range_stack;
6028 constructor_elements = p->elements;
6029 spelling = p->spelling;
6030 spelling_base = p->spelling_base;
6031 spelling_size = p->spelling_size;
6032 constructor_top_level = p->top_level;
6033 initializer_stack = p->next;
6034 free (p);
6035 }
6036 \f
6037 /* Call here when we see the initializer is surrounded by braces.
6038 This is instead of a call to push_init_level;
6039 it is matched by a call to pop_init_level.
6040
6041 TYPE is the type to initialize, for a constructor expression.
6042 For an initializer for a decl, TYPE is zero. */
6043
6044 void
6045 really_start_incremental_init (tree type)
6046 {
6047 struct constructor_stack *p = XNEW (struct constructor_stack);
6048
6049 if (type == 0)
6050 type = TREE_TYPE (constructor_decl);
6051
6052 if (TREE_CODE (type) == VECTOR_TYPE
6053 && TYPE_VECTOR_OPAQUE (type))
6054 error ("opaque vector types cannot be initialized");
6055
6056 p->type = constructor_type;
6057 p->fields = constructor_fields;
6058 p->index = constructor_index;
6059 p->max_index = constructor_max_index;
6060 p->unfilled_index = constructor_unfilled_index;
6061 p->unfilled_fields = constructor_unfilled_fields;
6062 p->bit_index = constructor_bit_index;
6063 p->elements = constructor_elements;
6064 p->constant = constructor_constant;
6065 p->simple = constructor_simple;
6066 p->nonconst = constructor_nonconst;
6067 p->erroneous = constructor_erroneous;
6068 p->pending_elts = constructor_pending_elts;
6069 p->depth = constructor_depth;
6070 p->replacement_value.value = 0;
6071 p->replacement_value.original_code = ERROR_MARK;
6072 p->replacement_value.original_type = NULL;
6073 p->implicit = 0;
6074 p->range_stack = 0;
6075 p->outer = 0;
6076 p->incremental = constructor_incremental;
6077 p->designated = constructor_designated;
6078 p->next = 0;
6079 constructor_stack = p;
6080
6081 constructor_constant = 1;
6082 constructor_simple = 1;
6083 constructor_nonconst = 0;
6084 constructor_depth = SPELLING_DEPTH ();
6085 constructor_elements = 0;
6086 constructor_pending_elts = 0;
6087 constructor_type = type;
6088 constructor_incremental = 1;
6089 constructor_designated = 0;
6090 designator_depth = 0;
6091 designator_erroneous = 0;
6092
6093 if (TREE_CODE (constructor_type) == RECORD_TYPE
6094 || TREE_CODE (constructor_type) == UNION_TYPE)
6095 {
6096 constructor_fields = TYPE_FIELDS (constructor_type);
6097 /* Skip any nameless bit fields at the beginning. */
6098 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6099 && DECL_NAME (constructor_fields) == 0)
6100 constructor_fields = TREE_CHAIN (constructor_fields);
6101
6102 constructor_unfilled_fields = constructor_fields;
6103 constructor_bit_index = bitsize_zero_node;
6104 }
6105 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6106 {
6107 if (TYPE_DOMAIN (constructor_type))
6108 {
6109 constructor_max_index
6110 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6111
6112 /* Detect non-empty initializations of zero-length arrays. */
6113 if (constructor_max_index == NULL_TREE
6114 && TYPE_SIZE (constructor_type))
6115 constructor_max_index = build_int_cst (NULL_TREE, -1);
6116
6117 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6118 to initialize VLAs will cause a proper error; avoid tree
6119 checking errors as well by setting a safe value. */
6120 if (constructor_max_index
6121 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6122 constructor_max_index = build_int_cst (NULL_TREE, -1);
6123
6124 constructor_index
6125 = convert (bitsizetype,
6126 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6127 }
6128 else
6129 {
6130 constructor_index = bitsize_zero_node;
6131 constructor_max_index = NULL_TREE;
6132 }
6133
6134 constructor_unfilled_index = constructor_index;
6135 }
6136 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6137 {
6138 /* Vectors are like simple fixed-size arrays. */
6139 constructor_max_index =
6140 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6141 constructor_index = bitsize_zero_node;
6142 constructor_unfilled_index = constructor_index;
6143 }
6144 else
6145 {
6146 /* Handle the case of int x = {5}; */
6147 constructor_fields = constructor_type;
6148 constructor_unfilled_fields = constructor_type;
6149 }
6150 }
6151 \f
6152 /* Push down into a subobject, for initialization.
6153 If this is for an explicit set of braces, IMPLICIT is 0.
6154 If it is because the next element belongs at a lower level,
6155 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6156
6157 void
6158 push_init_level (int implicit)
6159 {
6160 struct constructor_stack *p;
6161 tree value = NULL_TREE;
6162
6163 /* If we've exhausted any levels that didn't have braces,
6164 pop them now. If implicit == 1, this will have been done in
6165 process_init_element; do not repeat it here because in the case
6166 of excess initializers for an empty aggregate this leads to an
6167 infinite cycle of popping a level and immediately recreating
6168 it. */
6169 if (implicit != 1)
6170 {
6171 while (constructor_stack->implicit)
6172 {
6173 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6174 || TREE_CODE (constructor_type) == UNION_TYPE)
6175 && constructor_fields == 0)
6176 process_init_element (pop_init_level (1), true);
6177 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6178 && constructor_max_index
6179 && tree_int_cst_lt (constructor_max_index,
6180 constructor_index))
6181 process_init_element (pop_init_level (1), true);
6182 else
6183 break;
6184 }
6185 }
6186
6187 /* Unless this is an explicit brace, we need to preserve previous
6188 content if any. */
6189 if (implicit)
6190 {
6191 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6192 || TREE_CODE (constructor_type) == UNION_TYPE)
6193 && constructor_fields)
6194 value = find_init_member (constructor_fields);
6195 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6196 value = find_init_member (constructor_index);
6197 }
6198
6199 p = XNEW (struct constructor_stack);
6200 p->type = constructor_type;
6201 p->fields = constructor_fields;
6202 p->index = constructor_index;
6203 p->max_index = constructor_max_index;
6204 p->unfilled_index = constructor_unfilled_index;
6205 p->unfilled_fields = constructor_unfilled_fields;
6206 p->bit_index = constructor_bit_index;
6207 p->elements = constructor_elements;
6208 p->constant = constructor_constant;
6209 p->simple = constructor_simple;
6210 p->nonconst = constructor_nonconst;
6211 p->erroneous = constructor_erroneous;
6212 p->pending_elts = constructor_pending_elts;
6213 p->depth = constructor_depth;
6214 p->replacement_value.value = 0;
6215 p->replacement_value.original_code = ERROR_MARK;
6216 p->replacement_value.original_type = NULL;
6217 p->implicit = implicit;
6218 p->outer = 0;
6219 p->incremental = constructor_incremental;
6220 p->designated = constructor_designated;
6221 p->next = constructor_stack;
6222 p->range_stack = 0;
6223 constructor_stack = p;
6224
6225 constructor_constant = 1;
6226 constructor_simple = 1;
6227 constructor_nonconst = 0;
6228 constructor_depth = SPELLING_DEPTH ();
6229 constructor_elements = 0;
6230 constructor_incremental = 1;
6231 constructor_designated = 0;
6232 constructor_pending_elts = 0;
6233 if (!implicit)
6234 {
6235 p->range_stack = constructor_range_stack;
6236 constructor_range_stack = 0;
6237 designator_depth = 0;
6238 designator_erroneous = 0;
6239 }
6240
6241 /* Don't die if an entire brace-pair level is superfluous
6242 in the containing level. */
6243 if (constructor_type == 0)
6244 ;
6245 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6246 || TREE_CODE (constructor_type) == UNION_TYPE)
6247 {
6248 /* Don't die if there are extra init elts at the end. */
6249 if (constructor_fields == 0)
6250 constructor_type = 0;
6251 else
6252 {
6253 constructor_type = TREE_TYPE (constructor_fields);
6254 push_member_name (constructor_fields);
6255 constructor_depth++;
6256 }
6257 }
6258 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6259 {
6260 constructor_type = TREE_TYPE (constructor_type);
6261 push_array_bounds (tree_low_cst (constructor_index, 1));
6262 constructor_depth++;
6263 }
6264
6265 if (constructor_type == 0)
6266 {
6267 error_init ("extra brace group at end of initializer");
6268 constructor_fields = 0;
6269 constructor_unfilled_fields = 0;
6270 return;
6271 }
6272
6273 if (value && TREE_CODE (value) == CONSTRUCTOR)
6274 {
6275 constructor_constant = TREE_CONSTANT (value);
6276 constructor_simple = TREE_STATIC (value);
6277 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6278 constructor_elements = CONSTRUCTOR_ELTS (value);
6279 if (!VEC_empty (constructor_elt, constructor_elements)
6280 && (TREE_CODE (constructor_type) == RECORD_TYPE
6281 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6282 set_nonincremental_init ();
6283 }
6284
6285 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6286 {
6287 missing_braces_mentioned = 1;
6288 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6289 }
6290
6291 if (TREE_CODE (constructor_type) == RECORD_TYPE
6292 || TREE_CODE (constructor_type) == UNION_TYPE)
6293 {
6294 constructor_fields = TYPE_FIELDS (constructor_type);
6295 /* Skip any nameless bit fields at the beginning. */
6296 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6297 && DECL_NAME (constructor_fields) == 0)
6298 constructor_fields = TREE_CHAIN (constructor_fields);
6299
6300 constructor_unfilled_fields = constructor_fields;
6301 constructor_bit_index = bitsize_zero_node;
6302 }
6303 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6304 {
6305 /* Vectors are like simple fixed-size arrays. */
6306 constructor_max_index =
6307 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6308 constructor_index = convert (bitsizetype, integer_zero_node);
6309 constructor_unfilled_index = constructor_index;
6310 }
6311 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6312 {
6313 if (TYPE_DOMAIN (constructor_type))
6314 {
6315 constructor_max_index
6316 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6317
6318 /* Detect non-empty initializations of zero-length arrays. */
6319 if (constructor_max_index == NULL_TREE
6320 && TYPE_SIZE (constructor_type))
6321 constructor_max_index = build_int_cst (NULL_TREE, -1);
6322
6323 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6324 to initialize VLAs will cause a proper error; avoid tree
6325 checking errors as well by setting a safe value. */
6326 if (constructor_max_index
6327 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6328 constructor_max_index = build_int_cst (NULL_TREE, -1);
6329
6330 constructor_index
6331 = convert (bitsizetype,
6332 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6333 }
6334 else
6335 constructor_index = bitsize_zero_node;
6336
6337 constructor_unfilled_index = constructor_index;
6338 if (value && TREE_CODE (value) == STRING_CST)
6339 {
6340 /* We need to split the char/wchar array into individual
6341 characters, so that we don't have to special case it
6342 everywhere. */
6343 set_nonincremental_init_from_string (value);
6344 }
6345 }
6346 else
6347 {
6348 if (constructor_type != error_mark_node)
6349 warning_init (0, "braces around scalar initializer");
6350 constructor_fields = constructor_type;
6351 constructor_unfilled_fields = constructor_type;
6352 }
6353 }
6354
6355 /* At the end of an implicit or explicit brace level,
6356 finish up that level of constructor. If a single expression
6357 with redundant braces initialized that level, return the
6358 c_expr structure for that expression. Otherwise, the original_code
6359 element is set to ERROR_MARK.
6360 If we were outputting the elements as they are read, return 0 as the value
6361 from inner levels (process_init_element ignores that),
6362 but return error_mark_node as the value from the outermost level
6363 (that's what we want to put in DECL_INITIAL).
6364 Otherwise, return a CONSTRUCTOR expression as the value. */
6365
6366 struct c_expr
6367 pop_init_level (int implicit)
6368 {
6369 struct constructor_stack *p;
6370 struct c_expr ret;
6371 ret.value = 0;
6372 ret.original_code = ERROR_MARK;
6373 ret.original_type = NULL;
6374
6375 if (implicit == 0)
6376 {
6377 /* When we come to an explicit close brace,
6378 pop any inner levels that didn't have explicit braces. */
6379 while (constructor_stack->implicit)
6380 process_init_element (pop_init_level (1), true);
6381
6382 gcc_assert (!constructor_range_stack);
6383 }
6384
6385 /* Now output all pending elements. */
6386 constructor_incremental = 1;
6387 output_pending_init_elements (1);
6388
6389 p = constructor_stack;
6390
6391 /* Error for initializing a flexible array member, or a zero-length
6392 array member in an inappropriate context. */
6393 if (constructor_type && constructor_fields
6394 && TREE_CODE (constructor_type) == ARRAY_TYPE
6395 && TYPE_DOMAIN (constructor_type)
6396 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6397 {
6398 /* Silently discard empty initializations. The parser will
6399 already have pedwarned for empty brackets. */
6400 if (integer_zerop (constructor_unfilled_index))
6401 constructor_type = NULL_TREE;
6402 else
6403 {
6404 gcc_assert (!TYPE_SIZE (constructor_type));
6405
6406 if (constructor_depth > 2)
6407 error_init ("initialization of flexible array member in a nested context");
6408 else
6409 pedwarn_init (input_location, OPT_pedantic,
6410 "initialization of a flexible array member");
6411
6412 /* We have already issued an error message for the existence
6413 of a flexible array member not at the end of the structure.
6414 Discard the initializer so that we do not die later. */
6415 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6416 constructor_type = NULL_TREE;
6417 }
6418 }
6419
6420 /* Warn when some struct elements are implicitly initialized to zero. */
6421 if (warn_missing_field_initializers
6422 && constructor_type
6423 && TREE_CODE (constructor_type) == RECORD_TYPE
6424 && constructor_unfilled_fields)
6425 {
6426 /* Do not warn for flexible array members or zero-length arrays. */
6427 while (constructor_unfilled_fields
6428 && (!DECL_SIZE (constructor_unfilled_fields)
6429 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6430 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6431
6432 /* Do not warn if this level of the initializer uses member
6433 designators; it is likely to be deliberate. */
6434 if (constructor_unfilled_fields && !constructor_designated)
6435 {
6436 push_member_name (constructor_unfilled_fields);
6437 warning_init (OPT_Wmissing_field_initializers,
6438 "missing initializer");
6439 RESTORE_SPELLING_DEPTH (constructor_depth);
6440 }
6441 }
6442
6443 /* Pad out the end of the structure. */
6444 if (p->replacement_value.value)
6445 /* If this closes a superfluous brace pair,
6446 just pass out the element between them. */
6447 ret = p->replacement_value;
6448 else if (constructor_type == 0)
6449 ;
6450 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6451 && TREE_CODE (constructor_type) != UNION_TYPE
6452 && TREE_CODE (constructor_type) != ARRAY_TYPE
6453 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6454 {
6455 /* A nonincremental scalar initializer--just return
6456 the element, after verifying there is just one. */
6457 if (VEC_empty (constructor_elt,constructor_elements))
6458 {
6459 if (!constructor_erroneous)
6460 error_init ("empty scalar initializer");
6461 ret.value = error_mark_node;
6462 }
6463 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6464 {
6465 error_init ("extra elements in scalar initializer");
6466 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6467 }
6468 else
6469 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6470 }
6471 else
6472 {
6473 if (constructor_erroneous)
6474 ret.value = error_mark_node;
6475 else
6476 {
6477 ret.value = build_constructor (constructor_type,
6478 constructor_elements);
6479 if (constructor_constant)
6480 TREE_CONSTANT (ret.value) = 1;
6481 if (constructor_constant && constructor_simple)
6482 TREE_STATIC (ret.value) = 1;
6483 if (constructor_nonconst)
6484 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6485 }
6486 }
6487
6488 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6489 {
6490 if (constructor_nonconst)
6491 ret.original_code = C_MAYBE_CONST_EXPR;
6492 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6493 ret.original_code = ERROR_MARK;
6494 }
6495
6496 constructor_type = p->type;
6497 constructor_fields = p->fields;
6498 constructor_index = p->index;
6499 constructor_max_index = p->max_index;
6500 constructor_unfilled_index = p->unfilled_index;
6501 constructor_unfilled_fields = p->unfilled_fields;
6502 constructor_bit_index = p->bit_index;
6503 constructor_elements = p->elements;
6504 constructor_constant = p->constant;
6505 constructor_simple = p->simple;
6506 constructor_nonconst = p->nonconst;
6507 constructor_erroneous = p->erroneous;
6508 constructor_incremental = p->incremental;
6509 constructor_designated = p->designated;
6510 constructor_pending_elts = p->pending_elts;
6511 constructor_depth = p->depth;
6512 if (!p->implicit)
6513 constructor_range_stack = p->range_stack;
6514 RESTORE_SPELLING_DEPTH (constructor_depth);
6515
6516 constructor_stack = p->next;
6517 free (p);
6518
6519 if (ret.value == 0 && constructor_stack == 0)
6520 ret.value = error_mark_node;
6521 return ret;
6522 }
6523
6524 /* Common handling for both array range and field name designators.
6525 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6526
6527 static int
6528 set_designator (int array)
6529 {
6530 tree subtype;
6531 enum tree_code subcode;
6532
6533 /* Don't die if an entire brace-pair level is superfluous
6534 in the containing level. */
6535 if (constructor_type == 0)
6536 return 1;
6537
6538 /* If there were errors in this designator list already, bail out
6539 silently. */
6540 if (designator_erroneous)
6541 return 1;
6542
6543 if (!designator_depth)
6544 {
6545 gcc_assert (!constructor_range_stack);
6546
6547 /* Designator list starts at the level of closest explicit
6548 braces. */
6549 while (constructor_stack->implicit)
6550 process_init_element (pop_init_level (1), true);
6551 constructor_designated = 1;
6552 return 0;
6553 }
6554
6555 switch (TREE_CODE (constructor_type))
6556 {
6557 case RECORD_TYPE:
6558 case UNION_TYPE:
6559 subtype = TREE_TYPE (constructor_fields);
6560 if (subtype != error_mark_node)
6561 subtype = TYPE_MAIN_VARIANT (subtype);
6562 break;
6563 case ARRAY_TYPE:
6564 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6565 break;
6566 default:
6567 gcc_unreachable ();
6568 }
6569
6570 subcode = TREE_CODE (subtype);
6571 if (array && subcode != ARRAY_TYPE)
6572 {
6573 error_init ("array index in non-array initializer");
6574 return 1;
6575 }
6576 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6577 {
6578 error_init ("field name not in record or union initializer");
6579 return 1;
6580 }
6581
6582 constructor_designated = 1;
6583 push_init_level (2);
6584 return 0;
6585 }
6586
6587 /* If there are range designators in designator list, push a new designator
6588 to constructor_range_stack. RANGE_END is end of such stack range or
6589 NULL_TREE if there is no range designator at this level. */
6590
6591 static void
6592 push_range_stack (tree range_end)
6593 {
6594 struct constructor_range_stack *p;
6595
6596 p = GGC_NEW (struct constructor_range_stack);
6597 p->prev = constructor_range_stack;
6598 p->next = 0;
6599 p->fields = constructor_fields;
6600 p->range_start = constructor_index;
6601 p->index = constructor_index;
6602 p->stack = constructor_stack;
6603 p->range_end = range_end;
6604 if (constructor_range_stack)
6605 constructor_range_stack->next = p;
6606 constructor_range_stack = p;
6607 }
6608
6609 /* Within an array initializer, specify the next index to be initialized.
6610 FIRST is that index. If LAST is nonzero, then initialize a range
6611 of indices, running from FIRST through LAST. */
6612
6613 void
6614 set_init_index (tree first, tree last)
6615 {
6616 if (set_designator (1))
6617 return;
6618
6619 designator_erroneous = 1;
6620
6621 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6622 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6623 {
6624 error_init ("array index in initializer not of integer type");
6625 return;
6626 }
6627
6628 if (TREE_CODE (first) != INTEGER_CST)
6629 {
6630 first = c_fully_fold (first, false, NULL);
6631 if (TREE_CODE (first) == INTEGER_CST)
6632 pedwarn_init (input_location, OPT_pedantic,
6633 "array index in initializer is not "
6634 "an integer constant expression");
6635 }
6636
6637 if (last && TREE_CODE (last) != INTEGER_CST)
6638 {
6639 last = c_fully_fold (last, false, NULL);
6640 if (TREE_CODE (last) == INTEGER_CST)
6641 pedwarn_init (input_location, OPT_pedantic,
6642 "array index in initializer is not "
6643 "an integer constant expression");
6644 }
6645
6646 if (TREE_CODE (first) != INTEGER_CST)
6647 error_init ("nonconstant array index in initializer");
6648 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6649 error_init ("nonconstant array index in initializer");
6650 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6651 error_init ("array index in non-array initializer");
6652 else if (tree_int_cst_sgn (first) == -1)
6653 error_init ("array index in initializer exceeds array bounds");
6654 else if (constructor_max_index
6655 && tree_int_cst_lt (constructor_max_index, first))
6656 error_init ("array index in initializer exceeds array bounds");
6657 else
6658 {
6659 constant_expression_warning (first);
6660 if (last)
6661 constant_expression_warning (last);
6662 constructor_index = convert (bitsizetype, first);
6663
6664 if (last)
6665 {
6666 if (tree_int_cst_equal (first, last))
6667 last = 0;
6668 else if (tree_int_cst_lt (last, first))
6669 {
6670 error_init ("empty index range in initializer");
6671 last = 0;
6672 }
6673 else
6674 {
6675 last = convert (bitsizetype, last);
6676 if (constructor_max_index != 0
6677 && tree_int_cst_lt (constructor_max_index, last))
6678 {
6679 error_init ("array index range in initializer exceeds array bounds");
6680 last = 0;
6681 }
6682 }
6683 }
6684
6685 designator_depth++;
6686 designator_erroneous = 0;
6687 if (constructor_range_stack || last)
6688 push_range_stack (last);
6689 }
6690 }
6691
6692 /* Within a struct initializer, specify the next field to be initialized. */
6693
6694 void
6695 set_init_label (tree fieldname)
6696 {
6697 tree tail;
6698
6699 if (set_designator (0))
6700 return;
6701
6702 designator_erroneous = 1;
6703
6704 if (TREE_CODE (constructor_type) != RECORD_TYPE
6705 && TREE_CODE (constructor_type) != UNION_TYPE)
6706 {
6707 error_init ("field name not in record or union initializer");
6708 return;
6709 }
6710
6711 for (tail = TYPE_FIELDS (constructor_type); tail;
6712 tail = TREE_CHAIN (tail))
6713 {
6714 if (DECL_NAME (tail) == fieldname)
6715 break;
6716 }
6717
6718 if (tail == 0)
6719 error ("unknown field %qE specified in initializer", fieldname);
6720 else
6721 {
6722 constructor_fields = tail;
6723 designator_depth++;
6724 designator_erroneous = 0;
6725 if (constructor_range_stack)
6726 push_range_stack (NULL_TREE);
6727 }
6728 }
6729 \f
6730 /* Add a new initializer to the tree of pending initializers. PURPOSE
6731 identifies the initializer, either array index or field in a structure.
6732 VALUE is the value of that index or field. If ORIGTYPE is not
6733 NULL_TREE, it is the original type of VALUE.
6734
6735 IMPLICIT is true if value comes from pop_init_level (1),
6736 the new initializer has been merged with the existing one
6737 and thus no warnings should be emitted about overriding an
6738 existing initializer. */
6739
6740 static void
6741 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6742 {
6743 struct init_node *p, **q, *r;
6744
6745 q = &constructor_pending_elts;
6746 p = 0;
6747
6748 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6749 {
6750 while (*q != 0)
6751 {
6752 p = *q;
6753 if (tree_int_cst_lt (purpose, p->purpose))
6754 q = &p->left;
6755 else if (tree_int_cst_lt (p->purpose, purpose))
6756 q = &p->right;
6757 else
6758 {
6759 if (!implicit)
6760 {
6761 if (TREE_SIDE_EFFECTS (p->value))
6762 warning_init (0, "initialized field with side-effects overwritten");
6763 else if (warn_override_init)
6764 warning_init (OPT_Woverride_init, "initialized field overwritten");
6765 }
6766 p->value = value;
6767 p->origtype = origtype;
6768 return;
6769 }
6770 }
6771 }
6772 else
6773 {
6774 tree bitpos;
6775
6776 bitpos = bit_position (purpose);
6777 while (*q != NULL)
6778 {
6779 p = *q;
6780 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6781 q = &p->left;
6782 else if (p->purpose != purpose)
6783 q = &p->right;
6784 else
6785 {
6786 if (!implicit)
6787 {
6788 if (TREE_SIDE_EFFECTS (p->value))
6789 warning_init (0, "initialized field with side-effects overwritten");
6790 else if (warn_override_init)
6791 warning_init (OPT_Woverride_init, "initialized field overwritten");
6792 }
6793 p->value = value;
6794 p->origtype = origtype;
6795 return;
6796 }
6797 }
6798 }
6799
6800 r = GGC_NEW (struct init_node);
6801 r->purpose = purpose;
6802 r->value = value;
6803 r->origtype = origtype;
6804
6805 *q = r;
6806 r->parent = p;
6807 r->left = 0;
6808 r->right = 0;
6809 r->balance = 0;
6810
6811 while (p)
6812 {
6813 struct init_node *s;
6814
6815 if (r == p->left)
6816 {
6817 if (p->balance == 0)
6818 p->balance = -1;
6819 else if (p->balance < 0)
6820 {
6821 if (r->balance < 0)
6822 {
6823 /* L rotation. */
6824 p->left = r->right;
6825 if (p->left)
6826 p->left->parent = p;
6827 r->right = p;
6828
6829 p->balance = 0;
6830 r->balance = 0;
6831
6832 s = p->parent;
6833 p->parent = r;
6834 r->parent = s;
6835 if (s)
6836 {
6837 if (s->left == p)
6838 s->left = r;
6839 else
6840 s->right = r;
6841 }
6842 else
6843 constructor_pending_elts = r;
6844 }
6845 else
6846 {
6847 /* LR rotation. */
6848 struct init_node *t = r->right;
6849
6850 r->right = t->left;
6851 if (r->right)
6852 r->right->parent = r;
6853 t->left = r;
6854
6855 p->left = t->right;
6856 if (p->left)
6857 p->left->parent = p;
6858 t->right = p;
6859
6860 p->balance = t->balance < 0;
6861 r->balance = -(t->balance > 0);
6862 t->balance = 0;
6863
6864 s = p->parent;
6865 p->parent = t;
6866 r->parent = t;
6867 t->parent = s;
6868 if (s)
6869 {
6870 if (s->left == p)
6871 s->left = t;
6872 else
6873 s->right = t;
6874 }
6875 else
6876 constructor_pending_elts = t;
6877 }
6878 break;
6879 }
6880 else
6881 {
6882 /* p->balance == +1; growth of left side balances the node. */
6883 p->balance = 0;
6884 break;
6885 }
6886 }
6887 else /* r == p->right */
6888 {
6889 if (p->balance == 0)
6890 /* Growth propagation from right side. */
6891 p->balance++;
6892 else if (p->balance > 0)
6893 {
6894 if (r->balance > 0)
6895 {
6896 /* R rotation. */
6897 p->right = r->left;
6898 if (p->right)
6899 p->right->parent = p;
6900 r->left = p;
6901
6902 p->balance = 0;
6903 r->balance = 0;
6904
6905 s = p->parent;
6906 p->parent = r;
6907 r->parent = s;
6908 if (s)
6909 {
6910 if (s->left == p)
6911 s->left = r;
6912 else
6913 s->right = r;
6914 }
6915 else
6916 constructor_pending_elts = r;
6917 }
6918 else /* r->balance == -1 */
6919 {
6920 /* RL rotation */
6921 struct init_node *t = r->left;
6922
6923 r->left = t->right;
6924 if (r->left)
6925 r->left->parent = r;
6926 t->right = r;
6927
6928 p->right = t->left;
6929 if (p->right)
6930 p->right->parent = p;
6931 t->left = p;
6932
6933 r->balance = (t->balance < 0);
6934 p->balance = -(t->balance > 0);
6935 t->balance = 0;
6936
6937 s = p->parent;
6938 p->parent = t;
6939 r->parent = t;
6940 t->parent = s;
6941 if (s)
6942 {
6943 if (s->left == p)
6944 s->left = t;
6945 else
6946 s->right = t;
6947 }
6948 else
6949 constructor_pending_elts = t;
6950 }
6951 break;
6952 }
6953 else
6954 {
6955 /* p->balance == -1; growth of right side balances the node. */
6956 p->balance = 0;
6957 break;
6958 }
6959 }
6960
6961 r = p;
6962 p = p->parent;
6963 }
6964 }
6965
6966 /* Build AVL tree from a sorted chain. */
6967
6968 static void
6969 set_nonincremental_init (void)
6970 {
6971 unsigned HOST_WIDE_INT ix;
6972 tree index, value;
6973
6974 if (TREE_CODE (constructor_type) != RECORD_TYPE
6975 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6976 return;
6977
6978 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6979 add_pending_init (index, value, NULL_TREE, false);
6980 constructor_elements = 0;
6981 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6982 {
6983 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6984 /* Skip any nameless bit fields at the beginning. */
6985 while (constructor_unfilled_fields != 0
6986 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6987 && DECL_NAME (constructor_unfilled_fields) == 0)
6988 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6989
6990 }
6991 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6992 {
6993 if (TYPE_DOMAIN (constructor_type))
6994 constructor_unfilled_index
6995 = convert (bitsizetype,
6996 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6997 else
6998 constructor_unfilled_index = bitsize_zero_node;
6999 }
7000 constructor_incremental = 0;
7001 }
7002
7003 /* Build AVL tree from a string constant. */
7004
7005 static void
7006 set_nonincremental_init_from_string (tree str)
7007 {
7008 tree value, purpose, type;
7009 HOST_WIDE_INT val[2];
7010 const char *p, *end;
7011 int byte, wchar_bytes, charwidth, bitpos;
7012
7013 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7014
7015 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7016 charwidth = TYPE_PRECISION (char_type_node);
7017 type = TREE_TYPE (constructor_type);
7018 p = TREE_STRING_POINTER (str);
7019 end = p + TREE_STRING_LENGTH (str);
7020
7021 for (purpose = bitsize_zero_node;
7022 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7023 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7024 {
7025 if (wchar_bytes == 1)
7026 {
7027 val[1] = (unsigned char) *p++;
7028 val[0] = 0;
7029 }
7030 else
7031 {
7032 val[0] = 0;
7033 val[1] = 0;
7034 for (byte = 0; byte < wchar_bytes; byte++)
7035 {
7036 if (BYTES_BIG_ENDIAN)
7037 bitpos = (wchar_bytes - byte - 1) * charwidth;
7038 else
7039 bitpos = byte * charwidth;
7040 val[bitpos < HOST_BITS_PER_WIDE_INT]
7041 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7042 << (bitpos % HOST_BITS_PER_WIDE_INT);
7043 }
7044 }
7045
7046 if (!TYPE_UNSIGNED (type))
7047 {
7048 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7049 if (bitpos < HOST_BITS_PER_WIDE_INT)
7050 {
7051 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7052 {
7053 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7054 val[0] = -1;
7055 }
7056 }
7057 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7058 {
7059 if (val[1] < 0)
7060 val[0] = -1;
7061 }
7062 else if (val[0] & (((HOST_WIDE_INT) 1)
7063 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7064 val[0] |= ((HOST_WIDE_INT) -1)
7065 << (bitpos - HOST_BITS_PER_WIDE_INT);
7066 }
7067
7068 value = build_int_cst_wide (type, val[1], val[0]);
7069 add_pending_init (purpose, value, NULL_TREE, false);
7070 }
7071
7072 constructor_incremental = 0;
7073 }
7074
7075 /* Return value of FIELD in pending initializer or zero if the field was
7076 not initialized yet. */
7077
7078 static tree
7079 find_init_member (tree field)
7080 {
7081 struct init_node *p;
7082
7083 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7084 {
7085 if (constructor_incremental
7086 && tree_int_cst_lt (field, constructor_unfilled_index))
7087 set_nonincremental_init ();
7088
7089 p = constructor_pending_elts;
7090 while (p)
7091 {
7092 if (tree_int_cst_lt (field, p->purpose))
7093 p = p->left;
7094 else if (tree_int_cst_lt (p->purpose, field))
7095 p = p->right;
7096 else
7097 return p->value;
7098 }
7099 }
7100 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7101 {
7102 tree bitpos = bit_position (field);
7103
7104 if (constructor_incremental
7105 && (!constructor_unfilled_fields
7106 || tree_int_cst_lt (bitpos,
7107 bit_position (constructor_unfilled_fields))))
7108 set_nonincremental_init ();
7109
7110 p = constructor_pending_elts;
7111 while (p)
7112 {
7113 if (field == p->purpose)
7114 return p->value;
7115 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7116 p = p->left;
7117 else
7118 p = p->right;
7119 }
7120 }
7121 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7122 {
7123 if (!VEC_empty (constructor_elt, constructor_elements)
7124 && (VEC_last (constructor_elt, constructor_elements)->index
7125 == field))
7126 return VEC_last (constructor_elt, constructor_elements)->value;
7127 }
7128 return 0;
7129 }
7130
7131 /* "Output" the next constructor element.
7132 At top level, really output it to assembler code now.
7133 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7134 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7135 TYPE is the data type that the containing data type wants here.
7136 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7137 If VALUE is a string constant, STRICT_STRING is true if it is
7138 unparenthesized or we should not warn here for it being parenthesized.
7139 For other types of VALUE, STRICT_STRING is not used.
7140
7141 PENDING if non-nil means output pending elements that belong
7142 right after this element. (PENDING is normally 1;
7143 it is 0 while outputting pending elements, to avoid recursion.)
7144
7145 IMPLICIT is true if value comes from pop_init_level (1),
7146 the new initializer has been merged with the existing one
7147 and thus no warnings should be emitted about overriding an
7148 existing initializer. */
7149
7150 static void
7151 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7152 tree field, int pending, bool implicit)
7153 {
7154 tree semantic_type = NULL_TREE;
7155 constructor_elt *celt;
7156 bool maybe_const = true;
7157 bool npc;
7158
7159 if (type == error_mark_node || value == error_mark_node)
7160 {
7161 constructor_erroneous = 1;
7162 return;
7163 }
7164 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7165 && (TREE_CODE (value) == STRING_CST
7166 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7167 && !(TREE_CODE (value) == STRING_CST
7168 && TREE_CODE (type) == ARRAY_TYPE
7169 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7170 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7171 TYPE_MAIN_VARIANT (type)))
7172 value = array_to_pointer_conversion (input_location, value);
7173
7174 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7175 && require_constant_value && !flag_isoc99 && pending)
7176 {
7177 /* As an extension, allow initializing objects with static storage
7178 duration with compound literals (which are then treated just as
7179 the brace enclosed list they contain). */
7180 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7181 value = DECL_INITIAL (decl);
7182 }
7183
7184 npc = null_pointer_constant_p (value);
7185 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7186 {
7187 semantic_type = TREE_TYPE (value);
7188 value = TREE_OPERAND (value, 0);
7189 }
7190 value = c_fully_fold (value, require_constant_value, &maybe_const);
7191
7192 if (value == error_mark_node)
7193 constructor_erroneous = 1;
7194 else if (!TREE_CONSTANT (value))
7195 constructor_constant = 0;
7196 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7197 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7198 || TREE_CODE (constructor_type) == UNION_TYPE)
7199 && DECL_C_BIT_FIELD (field)
7200 && TREE_CODE (value) != INTEGER_CST))
7201 constructor_simple = 0;
7202 if (!maybe_const)
7203 constructor_nonconst = 1;
7204
7205 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7206 {
7207 if (require_constant_value)
7208 {
7209 error_init ("initializer element is not constant");
7210 value = error_mark_node;
7211 }
7212 else if (require_constant_elements)
7213 pedwarn (input_location, 0,
7214 "initializer element is not computable at load time");
7215 }
7216 else if (!maybe_const
7217 && (require_constant_value || require_constant_elements))
7218 pedwarn_init (input_location, 0,
7219 "initializer element is not a constant expression");
7220
7221 /* Issue -Wc++-compat warnings about initializing a bitfield with
7222 enum type. */
7223 if (warn_cxx_compat
7224 && field != NULL_TREE
7225 && TREE_CODE (field) == FIELD_DECL
7226 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7227 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7228 != TYPE_MAIN_VARIANT (type))
7229 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7230 {
7231 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7232 if (checktype != error_mark_node
7233 && (TYPE_MAIN_VARIANT (checktype)
7234 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7235 warning_init (OPT_Wc___compat,
7236 "enum conversion in initialization is invalid in C++");
7237 }
7238
7239 /* If this field is empty (and not at the end of structure),
7240 don't do anything other than checking the initializer. */
7241 if (field
7242 && (TREE_TYPE (field) == error_mark_node
7243 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7244 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7245 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7246 || TREE_CHAIN (field)))))
7247 return;
7248
7249 if (semantic_type)
7250 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7251 value = digest_init (input_location, type, value, origtype, npc,
7252 strict_string, require_constant_value);
7253 if (value == error_mark_node)
7254 {
7255 constructor_erroneous = 1;
7256 return;
7257 }
7258 if (require_constant_value || require_constant_elements)
7259 constant_expression_warning (value);
7260
7261 /* If this element doesn't come next in sequence,
7262 put it on constructor_pending_elts. */
7263 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7264 && (!constructor_incremental
7265 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7266 {
7267 if (constructor_incremental
7268 && tree_int_cst_lt (field, constructor_unfilled_index))
7269 set_nonincremental_init ();
7270
7271 add_pending_init (field, value, origtype, implicit);
7272 return;
7273 }
7274 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7275 && (!constructor_incremental
7276 || field != constructor_unfilled_fields))
7277 {
7278 /* We do this for records but not for unions. In a union,
7279 no matter which field is specified, it can be initialized
7280 right away since it starts at the beginning of the union. */
7281 if (constructor_incremental)
7282 {
7283 if (!constructor_unfilled_fields)
7284 set_nonincremental_init ();
7285 else
7286 {
7287 tree bitpos, unfillpos;
7288
7289 bitpos = bit_position (field);
7290 unfillpos = bit_position (constructor_unfilled_fields);
7291
7292 if (tree_int_cst_lt (bitpos, unfillpos))
7293 set_nonincremental_init ();
7294 }
7295 }
7296
7297 add_pending_init (field, value, origtype, implicit);
7298 return;
7299 }
7300 else if (TREE_CODE (constructor_type) == UNION_TYPE
7301 && !VEC_empty (constructor_elt, constructor_elements))
7302 {
7303 if (!implicit)
7304 {
7305 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7306 constructor_elements)->value))
7307 warning_init (0,
7308 "initialized field with side-effects overwritten");
7309 else if (warn_override_init)
7310 warning_init (OPT_Woverride_init, "initialized field overwritten");
7311 }
7312
7313 /* We can have just one union field set. */
7314 constructor_elements = 0;
7315 }
7316
7317 /* Otherwise, output this element either to
7318 constructor_elements or to the assembler file. */
7319
7320 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7321 celt->index = field;
7322 celt->value = value;
7323
7324 /* Advance the variable that indicates sequential elements output. */
7325 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7326 constructor_unfilled_index
7327 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7328 bitsize_one_node);
7329 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7330 {
7331 constructor_unfilled_fields
7332 = TREE_CHAIN (constructor_unfilled_fields);
7333
7334 /* Skip any nameless bit fields. */
7335 while (constructor_unfilled_fields != 0
7336 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7337 && DECL_NAME (constructor_unfilled_fields) == 0)
7338 constructor_unfilled_fields =
7339 TREE_CHAIN (constructor_unfilled_fields);
7340 }
7341 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7342 constructor_unfilled_fields = 0;
7343
7344 /* Now output any pending elements which have become next. */
7345 if (pending)
7346 output_pending_init_elements (0);
7347 }
7348
7349 /* Output any pending elements which have become next.
7350 As we output elements, constructor_unfilled_{fields,index}
7351 advances, which may cause other elements to become next;
7352 if so, they too are output.
7353
7354 If ALL is 0, we return when there are
7355 no more pending elements to output now.
7356
7357 If ALL is 1, we output space as necessary so that
7358 we can output all the pending elements. */
7359
7360 static void
7361 output_pending_init_elements (int all)
7362 {
7363 struct init_node *elt = constructor_pending_elts;
7364 tree next;
7365
7366 retry:
7367
7368 /* Look through the whole pending tree.
7369 If we find an element that should be output now,
7370 output it. Otherwise, set NEXT to the element
7371 that comes first among those still pending. */
7372
7373 next = 0;
7374 while (elt)
7375 {
7376 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7377 {
7378 if (tree_int_cst_equal (elt->purpose,
7379 constructor_unfilled_index))
7380 output_init_element (elt->value, elt->origtype, true,
7381 TREE_TYPE (constructor_type),
7382 constructor_unfilled_index, 0, false);
7383 else if (tree_int_cst_lt (constructor_unfilled_index,
7384 elt->purpose))
7385 {
7386 /* Advance to the next smaller node. */
7387 if (elt->left)
7388 elt = elt->left;
7389 else
7390 {
7391 /* We have reached the smallest node bigger than the
7392 current unfilled index. Fill the space first. */
7393 next = elt->purpose;
7394 break;
7395 }
7396 }
7397 else
7398 {
7399 /* Advance to the next bigger node. */
7400 if (elt->right)
7401 elt = elt->right;
7402 else
7403 {
7404 /* We have reached the biggest node in a subtree. Find
7405 the parent of it, which is the next bigger node. */
7406 while (elt->parent && elt->parent->right == elt)
7407 elt = elt->parent;
7408 elt = elt->parent;
7409 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7410 elt->purpose))
7411 {
7412 next = elt->purpose;
7413 break;
7414 }
7415 }
7416 }
7417 }
7418 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7419 || TREE_CODE (constructor_type) == UNION_TYPE)
7420 {
7421 tree ctor_unfilled_bitpos, elt_bitpos;
7422
7423 /* If the current record is complete we are done. */
7424 if (constructor_unfilled_fields == 0)
7425 break;
7426
7427 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7428 elt_bitpos = bit_position (elt->purpose);
7429 /* We can't compare fields here because there might be empty
7430 fields in between. */
7431 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7432 {
7433 constructor_unfilled_fields = elt->purpose;
7434 output_init_element (elt->value, elt->origtype, true,
7435 TREE_TYPE (elt->purpose),
7436 elt->purpose, 0, false);
7437 }
7438 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7439 {
7440 /* Advance to the next smaller node. */
7441 if (elt->left)
7442 elt = elt->left;
7443 else
7444 {
7445 /* We have reached the smallest node bigger than the
7446 current unfilled field. Fill the space first. */
7447 next = elt->purpose;
7448 break;
7449 }
7450 }
7451 else
7452 {
7453 /* Advance to the next bigger node. */
7454 if (elt->right)
7455 elt = elt->right;
7456 else
7457 {
7458 /* We have reached the biggest node in a subtree. Find
7459 the parent of it, which is the next bigger node. */
7460 while (elt->parent && elt->parent->right == elt)
7461 elt = elt->parent;
7462 elt = elt->parent;
7463 if (elt
7464 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7465 bit_position (elt->purpose))))
7466 {
7467 next = elt->purpose;
7468 break;
7469 }
7470 }
7471 }
7472 }
7473 }
7474
7475 /* Ordinarily return, but not if we want to output all
7476 and there are elements left. */
7477 if (!(all && next != 0))
7478 return;
7479
7480 /* If it's not incremental, just skip over the gap, so that after
7481 jumping to retry we will output the next successive element. */
7482 if (TREE_CODE (constructor_type) == RECORD_TYPE
7483 || TREE_CODE (constructor_type) == UNION_TYPE)
7484 constructor_unfilled_fields = next;
7485 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7486 constructor_unfilled_index = next;
7487
7488 /* ELT now points to the node in the pending tree with the next
7489 initializer to output. */
7490 goto retry;
7491 }
7492 \f
7493 /* Add one non-braced element to the current constructor level.
7494 This adjusts the current position within the constructor's type.
7495 This may also start or terminate implicit levels
7496 to handle a partly-braced initializer.
7497
7498 Once this has found the correct level for the new element,
7499 it calls output_init_element.
7500
7501 IMPLICIT is true if value comes from pop_init_level (1),
7502 the new initializer has been merged with the existing one
7503 and thus no warnings should be emitted about overriding an
7504 existing initializer. */
7505
7506 void
7507 process_init_element (struct c_expr value, bool implicit)
7508 {
7509 tree orig_value = value.value;
7510 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7511 bool strict_string = value.original_code == STRING_CST;
7512
7513 designator_depth = 0;
7514 designator_erroneous = 0;
7515
7516 /* Handle superfluous braces around string cst as in
7517 char x[] = {"foo"}; */
7518 if (string_flag
7519 && constructor_type
7520 && TREE_CODE (constructor_type) == ARRAY_TYPE
7521 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7522 && integer_zerop (constructor_unfilled_index))
7523 {
7524 if (constructor_stack->replacement_value.value)
7525 error_init ("excess elements in char array initializer");
7526 constructor_stack->replacement_value = value;
7527 return;
7528 }
7529
7530 if (constructor_stack->replacement_value.value != 0)
7531 {
7532 error_init ("excess elements in struct initializer");
7533 return;
7534 }
7535
7536 /* Ignore elements of a brace group if it is entirely superfluous
7537 and has already been diagnosed. */
7538 if (constructor_type == 0)
7539 return;
7540
7541 /* If we've exhausted any levels that didn't have braces,
7542 pop them now. */
7543 while (constructor_stack->implicit)
7544 {
7545 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7546 || TREE_CODE (constructor_type) == UNION_TYPE)
7547 && constructor_fields == 0)
7548 process_init_element (pop_init_level (1), true);
7549 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7550 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7551 && (constructor_max_index == 0
7552 || tree_int_cst_lt (constructor_max_index,
7553 constructor_index)))
7554 process_init_element (pop_init_level (1), true);
7555 else
7556 break;
7557 }
7558
7559 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7560 if (constructor_range_stack)
7561 {
7562 /* If value is a compound literal and we'll be just using its
7563 content, don't put it into a SAVE_EXPR. */
7564 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7565 || !require_constant_value
7566 || flag_isoc99)
7567 {
7568 tree semantic_type = NULL_TREE;
7569 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7570 {
7571 semantic_type = TREE_TYPE (value.value);
7572 value.value = TREE_OPERAND (value.value, 0);
7573 }
7574 value.value = c_save_expr (value.value);
7575 if (semantic_type)
7576 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7577 value.value);
7578 }
7579 }
7580
7581 while (1)
7582 {
7583 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7584 {
7585 tree fieldtype;
7586 enum tree_code fieldcode;
7587
7588 if (constructor_fields == 0)
7589 {
7590 pedwarn_init (input_location, 0,
7591 "excess elements in struct initializer");
7592 break;
7593 }
7594
7595 fieldtype = TREE_TYPE (constructor_fields);
7596 if (fieldtype != error_mark_node)
7597 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7598 fieldcode = TREE_CODE (fieldtype);
7599
7600 /* Error for non-static initialization of a flexible array member. */
7601 if (fieldcode == ARRAY_TYPE
7602 && !require_constant_value
7603 && TYPE_SIZE (fieldtype) == NULL_TREE
7604 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7605 {
7606 error_init ("non-static initialization of a flexible array member");
7607 break;
7608 }
7609
7610 /* Accept a string constant to initialize a subarray. */
7611 if (value.value != 0
7612 && fieldcode == ARRAY_TYPE
7613 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7614 && string_flag)
7615 value.value = orig_value;
7616 /* Otherwise, if we have come to a subaggregate,
7617 and we don't have an element of its type, push into it. */
7618 else if (value.value != 0
7619 && value.value != error_mark_node
7620 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7621 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7622 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7623 {
7624 push_init_level (1);
7625 continue;
7626 }
7627
7628 if (value.value)
7629 {
7630 push_member_name (constructor_fields);
7631 output_init_element (value.value, value.original_type,
7632 strict_string, fieldtype,
7633 constructor_fields, 1, implicit);
7634 RESTORE_SPELLING_DEPTH (constructor_depth);
7635 }
7636 else
7637 /* Do the bookkeeping for an element that was
7638 directly output as a constructor. */
7639 {
7640 /* For a record, keep track of end position of last field. */
7641 if (DECL_SIZE (constructor_fields))
7642 constructor_bit_index
7643 = size_binop_loc (input_location, PLUS_EXPR,
7644 bit_position (constructor_fields),
7645 DECL_SIZE (constructor_fields));
7646
7647 /* If the current field was the first one not yet written out,
7648 it isn't now, so update. */
7649 if (constructor_unfilled_fields == constructor_fields)
7650 {
7651 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7652 /* Skip any nameless bit fields. */
7653 while (constructor_unfilled_fields != 0
7654 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7655 && DECL_NAME (constructor_unfilled_fields) == 0)
7656 constructor_unfilled_fields =
7657 TREE_CHAIN (constructor_unfilled_fields);
7658 }
7659 }
7660
7661 constructor_fields = TREE_CHAIN (constructor_fields);
7662 /* Skip any nameless bit fields at the beginning. */
7663 while (constructor_fields != 0
7664 && DECL_C_BIT_FIELD (constructor_fields)
7665 && DECL_NAME (constructor_fields) == 0)
7666 constructor_fields = TREE_CHAIN (constructor_fields);
7667 }
7668 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7669 {
7670 tree fieldtype;
7671 enum tree_code fieldcode;
7672
7673 if (constructor_fields == 0)
7674 {
7675 pedwarn_init (input_location, 0,
7676 "excess elements in union initializer");
7677 break;
7678 }
7679
7680 fieldtype = TREE_TYPE (constructor_fields);
7681 if (fieldtype != error_mark_node)
7682 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7683 fieldcode = TREE_CODE (fieldtype);
7684
7685 /* Warn that traditional C rejects initialization of unions.
7686 We skip the warning if the value is zero. This is done
7687 under the assumption that the zero initializer in user
7688 code appears conditioned on e.g. __STDC__ to avoid
7689 "missing initializer" warnings and relies on default
7690 initialization to zero in the traditional C case.
7691 We also skip the warning if the initializer is designated,
7692 again on the assumption that this must be conditional on
7693 __STDC__ anyway (and we've already complained about the
7694 member-designator already). */
7695 if (!in_system_header && !constructor_designated
7696 && !(value.value && (integer_zerop (value.value)
7697 || real_zerop (value.value))))
7698 warning (OPT_Wtraditional, "traditional C rejects initialization "
7699 "of unions");
7700
7701 /* Accept a string constant to initialize a subarray. */
7702 if (value.value != 0
7703 && fieldcode == ARRAY_TYPE
7704 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7705 && string_flag)
7706 value.value = orig_value;
7707 /* Otherwise, if we have come to a subaggregate,
7708 and we don't have an element of its type, push into it. */
7709 else if (value.value != 0
7710 && value.value != error_mark_node
7711 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7712 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7713 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7714 {
7715 push_init_level (1);
7716 continue;
7717 }
7718
7719 if (value.value)
7720 {
7721 push_member_name (constructor_fields);
7722 output_init_element (value.value, value.original_type,
7723 strict_string, fieldtype,
7724 constructor_fields, 1, implicit);
7725 RESTORE_SPELLING_DEPTH (constructor_depth);
7726 }
7727 else
7728 /* Do the bookkeeping for an element that was
7729 directly output as a constructor. */
7730 {
7731 constructor_bit_index = DECL_SIZE (constructor_fields);
7732 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7733 }
7734
7735 constructor_fields = 0;
7736 }
7737 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7738 {
7739 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7740 enum tree_code eltcode = TREE_CODE (elttype);
7741
7742 /* Accept a string constant to initialize a subarray. */
7743 if (value.value != 0
7744 && eltcode == ARRAY_TYPE
7745 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7746 && string_flag)
7747 value.value = orig_value;
7748 /* Otherwise, if we have come to a subaggregate,
7749 and we don't have an element of its type, push into it. */
7750 else if (value.value != 0
7751 && value.value != error_mark_node
7752 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7753 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7754 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7755 {
7756 push_init_level (1);
7757 continue;
7758 }
7759
7760 if (constructor_max_index != 0
7761 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7762 || integer_all_onesp (constructor_max_index)))
7763 {
7764 pedwarn_init (input_location, 0,
7765 "excess elements in array initializer");
7766 break;
7767 }
7768
7769 /* Now output the actual element. */
7770 if (value.value)
7771 {
7772 push_array_bounds (tree_low_cst (constructor_index, 1));
7773 output_init_element (value.value, value.original_type,
7774 strict_string, elttype,
7775 constructor_index, 1, implicit);
7776 RESTORE_SPELLING_DEPTH (constructor_depth);
7777 }
7778
7779 constructor_index
7780 = size_binop_loc (input_location, PLUS_EXPR,
7781 constructor_index, bitsize_one_node);
7782
7783 if (!value.value)
7784 /* If we are doing the bookkeeping for an element that was
7785 directly output as a constructor, we must update
7786 constructor_unfilled_index. */
7787 constructor_unfilled_index = constructor_index;
7788 }
7789 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7790 {
7791 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7792
7793 /* Do a basic check of initializer size. Note that vectors
7794 always have a fixed size derived from their type. */
7795 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7796 {
7797 pedwarn_init (input_location, 0,
7798 "excess elements in vector initializer");
7799 break;
7800 }
7801
7802 /* Now output the actual element. */
7803 if (value.value)
7804 {
7805 if (TREE_CODE (value.value) == VECTOR_CST)
7806 elttype = TYPE_MAIN_VARIANT (constructor_type);
7807 output_init_element (value.value, value.original_type,
7808 strict_string, elttype,
7809 constructor_index, 1, implicit);
7810 }
7811
7812 constructor_index
7813 = size_binop_loc (input_location,
7814 PLUS_EXPR, constructor_index, bitsize_one_node);
7815
7816 if (!value.value)
7817 /* If we are doing the bookkeeping for an element that was
7818 directly output as a constructor, we must update
7819 constructor_unfilled_index. */
7820 constructor_unfilled_index = constructor_index;
7821 }
7822
7823 /* Handle the sole element allowed in a braced initializer
7824 for a scalar variable. */
7825 else if (constructor_type != error_mark_node
7826 && constructor_fields == 0)
7827 {
7828 pedwarn_init (input_location, 0,
7829 "excess elements in scalar initializer");
7830 break;
7831 }
7832 else
7833 {
7834 if (value.value)
7835 output_init_element (value.value, value.original_type,
7836 strict_string, constructor_type,
7837 NULL_TREE, 1, implicit);
7838 constructor_fields = 0;
7839 }
7840
7841 /* Handle range initializers either at this level or anywhere higher
7842 in the designator stack. */
7843 if (constructor_range_stack)
7844 {
7845 struct constructor_range_stack *p, *range_stack;
7846 int finish = 0;
7847
7848 range_stack = constructor_range_stack;
7849 constructor_range_stack = 0;
7850 while (constructor_stack != range_stack->stack)
7851 {
7852 gcc_assert (constructor_stack->implicit);
7853 process_init_element (pop_init_level (1), true);
7854 }
7855 for (p = range_stack;
7856 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7857 p = p->prev)
7858 {
7859 gcc_assert (constructor_stack->implicit);
7860 process_init_element (pop_init_level (1), true);
7861 }
7862
7863 p->index = size_binop_loc (input_location,
7864 PLUS_EXPR, p->index, bitsize_one_node);
7865 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7866 finish = 1;
7867
7868 while (1)
7869 {
7870 constructor_index = p->index;
7871 constructor_fields = p->fields;
7872 if (finish && p->range_end && p->index == p->range_start)
7873 {
7874 finish = 0;
7875 p->prev = 0;
7876 }
7877 p = p->next;
7878 if (!p)
7879 break;
7880 push_init_level (2);
7881 p->stack = constructor_stack;
7882 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7883 p->index = p->range_start;
7884 }
7885
7886 if (!finish)
7887 constructor_range_stack = range_stack;
7888 continue;
7889 }
7890
7891 break;
7892 }
7893
7894 constructor_range_stack = 0;
7895 }
7896 \f
7897 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7898 (guaranteed to be 'volatile' or null) and ARGS (represented using
7899 an ASM_EXPR node). */
7900 tree
7901 build_asm_stmt (tree cv_qualifier, tree args)
7902 {
7903 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7904 ASM_VOLATILE_P (args) = 1;
7905 return add_stmt (args);
7906 }
7907
7908 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7909 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7910 SIMPLE indicates whether there was anything at all after the
7911 string in the asm expression -- asm("blah") and asm("blah" : )
7912 are subtly different. We use a ASM_EXPR node to represent this. */
7913 tree
7914 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
7915 tree clobbers, tree labels, bool simple)
7916 {
7917 tree tail;
7918 tree args;
7919 int i;
7920 const char *constraint;
7921 const char **oconstraints;
7922 bool allows_mem, allows_reg, is_inout;
7923 int ninputs, noutputs;
7924
7925 ninputs = list_length (inputs);
7926 noutputs = list_length (outputs);
7927 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7928
7929 string = resolve_asm_operand_names (string, outputs, inputs, labels);
7930
7931 /* Remove output conversions that change the type but not the mode. */
7932 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7933 {
7934 tree output = TREE_VALUE (tail);
7935
7936 /* ??? Really, this should not be here. Users should be using a
7937 proper lvalue, dammit. But there's a long history of using casts
7938 in the output operands. In cases like longlong.h, this becomes a
7939 primitive form of typechecking -- if the cast can be removed, then
7940 the output operand had a type of the proper width; otherwise we'll
7941 get an error. Gross, but ... */
7942 STRIP_NOPS (output);
7943
7944 if (!lvalue_or_else (output, lv_asm))
7945 output = error_mark_node;
7946
7947 if (output != error_mark_node
7948 && (TREE_READONLY (output)
7949 || TYPE_READONLY (TREE_TYPE (output))
7950 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7951 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7952 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7953 readonly_error (output, lv_asm);
7954
7955 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7956 oconstraints[i] = constraint;
7957
7958 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7959 &allows_mem, &allows_reg, &is_inout))
7960 {
7961 /* If the operand is going to end up in memory,
7962 mark it addressable. */
7963 if (!allows_reg && !c_mark_addressable (output))
7964 output = error_mark_node;
7965 }
7966 else
7967 output = error_mark_node;
7968
7969 TREE_VALUE (tail) = output;
7970 }
7971
7972 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7973 {
7974 tree input;
7975
7976 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7977 input = TREE_VALUE (tail);
7978
7979 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7980 oconstraints, &allows_mem, &allows_reg))
7981 {
7982 /* If the operand is going to end up in memory,
7983 mark it addressable. */
7984 if (!allows_reg && allows_mem)
7985 {
7986 /* Strip the nops as we allow this case. FIXME, this really
7987 should be rejected or made deprecated. */
7988 STRIP_NOPS (input);
7989 if (!c_mark_addressable (input))
7990 input = error_mark_node;
7991 }
7992 }
7993 else
7994 input = error_mark_node;
7995
7996 TREE_VALUE (tail) = input;
7997 }
7998
7999 /* ASMs with labels cannot have outputs. This should have been
8000 enforced by the parser. */
8001 gcc_assert (outputs == NULL || labels == NULL);
8002
8003 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8004
8005 /* asm statements without outputs, including simple ones, are treated
8006 as volatile. */
8007 ASM_INPUT_P (args) = simple;
8008 ASM_VOLATILE_P (args) = (noutputs == 0);
8009
8010 return args;
8011 }
8012 \f
8013 /* Generate a goto statement to LABEL. LOC is the location of the
8014 GOTO. */
8015
8016 tree
8017 c_finish_goto_label (location_t loc, tree label)
8018 {
8019 tree decl = lookup_label_for_goto (loc, label);
8020 if (!decl)
8021 return NULL_TREE;
8022 TREE_USED (decl) = 1;
8023 {
8024 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8025 SET_EXPR_LOCATION (t, loc);
8026 return add_stmt (t);
8027 }
8028 }
8029
8030 /* Generate a computed goto statement to EXPR. LOC is the location of
8031 the GOTO. */
8032
8033 tree
8034 c_finish_goto_ptr (location_t loc, tree expr)
8035 {
8036 tree t;
8037 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8038 expr = c_fully_fold (expr, false, NULL);
8039 expr = convert (ptr_type_node, expr);
8040 t = build1 (GOTO_EXPR, void_type_node, expr);
8041 SET_EXPR_LOCATION (t, loc);
8042 return add_stmt (t);
8043 }
8044
8045 /* Generate a C `return' statement. RETVAL is the expression for what
8046 to return, or a null pointer for `return;' with no value. LOC is
8047 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8048 is the original type of RETVAL. */
8049
8050 tree
8051 c_finish_return (location_t loc, tree retval, tree origtype)
8052 {
8053 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8054 bool no_warning = false;
8055 bool npc = false;
8056
8057 if (TREE_THIS_VOLATILE (current_function_decl))
8058 warning_at (loc, 0,
8059 "function declared %<noreturn%> has a %<return%> statement");
8060
8061 if (retval)
8062 {
8063 tree semantic_type = NULL_TREE;
8064 npc = null_pointer_constant_p (retval);
8065 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8066 {
8067 semantic_type = TREE_TYPE (retval);
8068 retval = TREE_OPERAND (retval, 0);
8069 }
8070 retval = c_fully_fold (retval, false, NULL);
8071 if (semantic_type)
8072 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8073 }
8074
8075 if (!retval)
8076 {
8077 current_function_returns_null = 1;
8078 if ((warn_return_type || flag_isoc99)
8079 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8080 {
8081 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8082 "%<return%> with no value, in "
8083 "function returning non-void");
8084 no_warning = true;
8085 }
8086 }
8087 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8088 {
8089 current_function_returns_null = 1;
8090 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8091 pedwarn (loc, 0,
8092 "%<return%> with a value, in function returning void");
8093 else
8094 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8095 "%<return%> with expression, in function returning void");
8096 }
8097 else
8098 {
8099 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8100 ic_return,
8101 npc, NULL_TREE, NULL_TREE, 0);
8102 tree res = DECL_RESULT (current_function_decl);
8103 tree inner;
8104
8105 current_function_returns_value = 1;
8106 if (t == error_mark_node)
8107 return NULL_TREE;
8108
8109 inner = t = convert (TREE_TYPE (res), t);
8110
8111 /* Strip any conversions, additions, and subtractions, and see if
8112 we are returning the address of a local variable. Warn if so. */
8113 while (1)
8114 {
8115 switch (TREE_CODE (inner))
8116 {
8117 CASE_CONVERT:
8118 case NON_LVALUE_EXPR:
8119 case PLUS_EXPR:
8120 case POINTER_PLUS_EXPR:
8121 inner = TREE_OPERAND (inner, 0);
8122 continue;
8123
8124 case MINUS_EXPR:
8125 /* If the second operand of the MINUS_EXPR has a pointer
8126 type (or is converted from it), this may be valid, so
8127 don't give a warning. */
8128 {
8129 tree op1 = TREE_OPERAND (inner, 1);
8130
8131 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8132 && (CONVERT_EXPR_P (op1)
8133 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8134 op1 = TREE_OPERAND (op1, 0);
8135
8136 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8137 break;
8138
8139 inner = TREE_OPERAND (inner, 0);
8140 continue;
8141 }
8142
8143 case ADDR_EXPR:
8144 inner = TREE_OPERAND (inner, 0);
8145
8146 while (REFERENCE_CLASS_P (inner)
8147 && TREE_CODE (inner) != INDIRECT_REF)
8148 inner = TREE_OPERAND (inner, 0);
8149
8150 if (DECL_P (inner)
8151 && !DECL_EXTERNAL (inner)
8152 && !TREE_STATIC (inner)
8153 && DECL_CONTEXT (inner) == current_function_decl)
8154 warning_at (loc,
8155 0, "function returns address of local variable");
8156 break;
8157
8158 default:
8159 break;
8160 }
8161
8162 break;
8163 }
8164
8165 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8166 SET_EXPR_LOCATION (retval, loc);
8167
8168 if (warn_sequence_point)
8169 verify_sequence_points (retval);
8170 }
8171
8172 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8173 TREE_NO_WARNING (ret_stmt) |= no_warning;
8174 return add_stmt (ret_stmt);
8175 }
8176 \f
8177 struct c_switch {
8178 /* The SWITCH_EXPR being built. */
8179 tree switch_expr;
8180
8181 /* The original type of the testing expression, i.e. before the
8182 default conversion is applied. */
8183 tree orig_type;
8184
8185 /* A splay-tree mapping the low element of a case range to the high
8186 element, or NULL_TREE if there is no high element. Used to
8187 determine whether or not a new case label duplicates an old case
8188 label. We need a tree, rather than simply a hash table, because
8189 of the GNU case range extension. */
8190 splay_tree cases;
8191
8192 /* The bindings at the point of the switch. This is used for
8193 warnings crossing decls when branching to a case label. */
8194 struct c_spot_bindings *bindings;
8195
8196 /* The next node on the stack. */
8197 struct c_switch *next;
8198 };
8199
8200 /* A stack of the currently active switch statements. The innermost
8201 switch statement is on the top of the stack. There is no need to
8202 mark the stack for garbage collection because it is only active
8203 during the processing of the body of a function, and we never
8204 collect at that point. */
8205
8206 struct c_switch *c_switch_stack;
8207
8208 /* Start a C switch statement, testing expression EXP. Return the new
8209 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8210 SWITCH_COND_LOC is the location of the switch's condition. */
8211
8212 tree
8213 c_start_case (location_t switch_loc,
8214 location_t switch_cond_loc,
8215 tree exp)
8216 {
8217 tree orig_type = error_mark_node;
8218 struct c_switch *cs;
8219
8220 if (exp != error_mark_node)
8221 {
8222 orig_type = TREE_TYPE (exp);
8223
8224 if (!INTEGRAL_TYPE_P (orig_type))
8225 {
8226 if (orig_type != error_mark_node)
8227 {
8228 error_at (switch_cond_loc, "switch quantity not an integer");
8229 orig_type = error_mark_node;
8230 }
8231 exp = integer_zero_node;
8232 }
8233 else
8234 {
8235 tree type = TYPE_MAIN_VARIANT (orig_type);
8236
8237 if (!in_system_header
8238 && (type == long_integer_type_node
8239 || type == long_unsigned_type_node))
8240 warning_at (switch_cond_loc,
8241 OPT_Wtraditional, "%<long%> switch expression not "
8242 "converted to %<int%> in ISO C");
8243
8244 exp = c_fully_fold (exp, false, NULL);
8245 exp = default_conversion (exp);
8246
8247 if (warn_sequence_point)
8248 verify_sequence_points (exp);
8249 }
8250 }
8251
8252 /* Add this new SWITCH_EXPR to the stack. */
8253 cs = XNEW (struct c_switch);
8254 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8255 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8256 cs->orig_type = orig_type;
8257 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8258 cs->bindings = c_get_switch_bindings ();
8259 cs->next = c_switch_stack;
8260 c_switch_stack = cs;
8261
8262 return add_stmt (cs->switch_expr);
8263 }
8264
8265 /* Process a case label at location LOC. */
8266
8267 tree
8268 do_case (location_t loc, tree low_value, tree high_value)
8269 {
8270 tree label = NULL_TREE;
8271
8272 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8273 {
8274 low_value = c_fully_fold (low_value, false, NULL);
8275 if (TREE_CODE (low_value) == INTEGER_CST)
8276 pedwarn (input_location, OPT_pedantic,
8277 "case label is not an integer constant expression");
8278 }
8279
8280 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8281 {
8282 high_value = c_fully_fold (high_value, false, NULL);
8283 if (TREE_CODE (high_value) == INTEGER_CST)
8284 pedwarn (input_location, OPT_pedantic,
8285 "case label is not an integer constant expression");
8286 }
8287
8288 if (c_switch_stack == NULL)
8289 {
8290 if (low_value)
8291 error_at (loc, "case label not within a switch statement");
8292 else
8293 error_at (loc, "%<default%> label not within a switch statement");
8294 return NULL_TREE;
8295 }
8296
8297 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8298 EXPR_LOCATION (c_switch_stack->switch_expr),
8299 loc))
8300 return NULL_TREE;
8301
8302 label = c_add_case_label (loc, c_switch_stack->cases,
8303 SWITCH_COND (c_switch_stack->switch_expr),
8304 c_switch_stack->orig_type,
8305 low_value, high_value);
8306 if (label == error_mark_node)
8307 label = NULL_TREE;
8308 return label;
8309 }
8310
8311 /* Finish the switch statement. */
8312
8313 void
8314 c_finish_case (tree body)
8315 {
8316 struct c_switch *cs = c_switch_stack;
8317 location_t switch_location;
8318
8319 SWITCH_BODY (cs->switch_expr) = body;
8320
8321 /* Emit warnings as needed. */
8322 switch_location = EXPR_LOCATION (cs->switch_expr);
8323 c_do_switch_warnings (cs->cases, switch_location,
8324 TREE_TYPE (cs->switch_expr),
8325 SWITCH_COND (cs->switch_expr));
8326
8327 /* Pop the stack. */
8328 c_switch_stack = cs->next;
8329 splay_tree_delete (cs->cases);
8330 c_release_switch_bindings (cs->bindings);
8331 XDELETE (cs);
8332 }
8333 \f
8334 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8335 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8336 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8337 statement, and was not surrounded with parenthesis. */
8338
8339 void
8340 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8341 tree else_block, bool nested_if)
8342 {
8343 tree stmt;
8344
8345 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8346 if (warn_parentheses && nested_if && else_block == NULL)
8347 {
8348 tree inner_if = then_block;
8349
8350 /* We know from the grammar productions that there is an IF nested
8351 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8352 it might not be exactly THEN_BLOCK, but should be the last
8353 non-container statement within. */
8354 while (1)
8355 switch (TREE_CODE (inner_if))
8356 {
8357 case COND_EXPR:
8358 goto found;
8359 case BIND_EXPR:
8360 inner_if = BIND_EXPR_BODY (inner_if);
8361 break;
8362 case STATEMENT_LIST:
8363 inner_if = expr_last (then_block);
8364 break;
8365 case TRY_FINALLY_EXPR:
8366 case TRY_CATCH_EXPR:
8367 inner_if = TREE_OPERAND (inner_if, 0);
8368 break;
8369 default:
8370 gcc_unreachable ();
8371 }
8372 found:
8373
8374 if (COND_EXPR_ELSE (inner_if))
8375 warning_at (if_locus, OPT_Wparentheses,
8376 "suggest explicit braces to avoid ambiguous %<else%>");
8377 }
8378
8379 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8380 SET_EXPR_LOCATION (stmt, if_locus);
8381 add_stmt (stmt);
8382 }
8383
8384 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8385 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8386 is false for DO loops. INCR is the FOR increment expression. BODY is
8387 the statement controlled by the loop. BLAB is the break label. CLAB is
8388 the continue label. Everything is allowed to be NULL. */
8389
8390 void
8391 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8392 tree blab, tree clab, bool cond_is_first)
8393 {
8394 tree entry = NULL, exit = NULL, t;
8395
8396 /* If the condition is zero don't generate a loop construct. */
8397 if (cond && integer_zerop (cond))
8398 {
8399 if (cond_is_first)
8400 {
8401 t = build_and_jump (&blab);
8402 SET_EXPR_LOCATION (t, start_locus);
8403 add_stmt (t);
8404 }
8405 }
8406 else
8407 {
8408 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8409
8410 /* If we have an exit condition, then we build an IF with gotos either
8411 out of the loop, or to the top of it. If there's no exit condition,
8412 then we just build a jump back to the top. */
8413 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8414
8415 if (cond && !integer_nonzerop (cond))
8416 {
8417 /* Canonicalize the loop condition to the end. This means
8418 generating a branch to the loop condition. Reuse the
8419 continue label, if possible. */
8420 if (cond_is_first)
8421 {
8422 if (incr || !clab)
8423 {
8424 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8425 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8426 }
8427 else
8428 t = build1 (GOTO_EXPR, void_type_node, clab);
8429 SET_EXPR_LOCATION (t, start_locus);
8430 add_stmt (t);
8431 }
8432
8433 t = build_and_jump (&blab);
8434 if (cond_is_first)
8435 exit = fold_build3_loc (start_locus,
8436 COND_EXPR, void_type_node, cond, exit, t);
8437 else
8438 exit = fold_build3_loc (input_location,
8439 COND_EXPR, void_type_node, cond, exit, t);
8440 }
8441
8442 add_stmt (top);
8443 }
8444
8445 if (body)
8446 add_stmt (body);
8447 if (clab)
8448 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8449 if (incr)
8450 add_stmt (incr);
8451 if (entry)
8452 add_stmt (entry);
8453 if (exit)
8454 add_stmt (exit);
8455 if (blab)
8456 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8457 }
8458
8459 tree
8460 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8461 {
8462 bool skip;
8463 tree label = *label_p;
8464
8465 /* In switch statements break is sometimes stylistically used after
8466 a return statement. This can lead to spurious warnings about
8467 control reaching the end of a non-void function when it is
8468 inlined. Note that we are calling block_may_fallthru with
8469 language specific tree nodes; this works because
8470 block_may_fallthru returns true when given something it does not
8471 understand. */
8472 skip = !block_may_fallthru (cur_stmt_list);
8473
8474 if (!label)
8475 {
8476 if (!skip)
8477 *label_p = label = create_artificial_label (loc);
8478 }
8479 else if (TREE_CODE (label) == LABEL_DECL)
8480 ;
8481 else switch (TREE_INT_CST_LOW (label))
8482 {
8483 case 0:
8484 if (is_break)
8485 error_at (loc, "break statement not within loop or switch");
8486 else
8487 error_at (loc, "continue statement not within a loop");
8488 return NULL_TREE;
8489
8490 case 1:
8491 gcc_assert (is_break);
8492 error_at (loc, "break statement used with OpenMP for loop");
8493 return NULL_TREE;
8494
8495 default:
8496 gcc_unreachable ();
8497 }
8498
8499 if (skip)
8500 return NULL_TREE;
8501
8502 if (!is_break)
8503 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8504
8505 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8506 }
8507
8508 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8509
8510 static void
8511 emit_side_effect_warnings (location_t loc, tree expr)
8512 {
8513 if (expr == error_mark_node)
8514 ;
8515 else if (!TREE_SIDE_EFFECTS (expr))
8516 {
8517 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8518 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8519 }
8520 else
8521 warn_if_unused_value (expr, loc);
8522 }
8523
8524 /* Process an expression as if it were a complete statement. Emit
8525 diagnostics, but do not call ADD_STMT. LOC is the location of the
8526 statement. */
8527
8528 tree
8529 c_process_expr_stmt (location_t loc, tree expr)
8530 {
8531 if (!expr)
8532 return NULL_TREE;
8533
8534 expr = c_fully_fold (expr, false, NULL);
8535
8536 if (warn_sequence_point)
8537 verify_sequence_points (expr);
8538
8539 if (TREE_TYPE (expr) != error_mark_node
8540 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8541 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8542 error_at (loc, "expression statement has incomplete type");
8543
8544 /* If we're not processing a statement expression, warn about unused values.
8545 Warnings for statement expressions will be emitted later, once we figure
8546 out which is the result. */
8547 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8548 && warn_unused_value)
8549 emit_side_effect_warnings (loc, expr);
8550
8551 /* If the expression is not of a type to which we cannot assign a line
8552 number, wrap the thing in a no-op NOP_EXPR. */
8553 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8554 {
8555 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8556 SET_EXPR_LOCATION (expr, loc);
8557 }
8558
8559 return expr;
8560 }
8561
8562 /* Emit an expression as a statement. LOC is the location of the
8563 expression. */
8564
8565 tree
8566 c_finish_expr_stmt (location_t loc, tree expr)
8567 {
8568 if (expr)
8569 return add_stmt (c_process_expr_stmt (loc, expr));
8570 else
8571 return NULL;
8572 }
8573
8574 /* Do the opposite and emit a statement as an expression. To begin,
8575 create a new binding level and return it. */
8576
8577 tree
8578 c_begin_stmt_expr (void)
8579 {
8580 tree ret;
8581
8582 /* We must force a BLOCK for this level so that, if it is not expanded
8583 later, there is a way to turn off the entire subtree of blocks that
8584 are contained in it. */
8585 keep_next_level ();
8586 ret = c_begin_compound_stmt (true);
8587
8588 c_bindings_start_stmt_expr (c_switch_stack == NULL
8589 ? NULL
8590 : c_switch_stack->bindings);
8591
8592 /* Mark the current statement list as belonging to a statement list. */
8593 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8594
8595 return ret;
8596 }
8597
8598 /* LOC is the location of the compound statement to which this body
8599 belongs. */
8600
8601 tree
8602 c_finish_stmt_expr (location_t loc, tree body)
8603 {
8604 tree last, type, tmp, val;
8605 tree *last_p;
8606
8607 body = c_end_compound_stmt (loc, body, true);
8608
8609 c_bindings_end_stmt_expr (c_switch_stack == NULL
8610 ? NULL
8611 : c_switch_stack->bindings);
8612
8613 /* Locate the last statement in BODY. See c_end_compound_stmt
8614 about always returning a BIND_EXPR. */
8615 last_p = &BIND_EXPR_BODY (body);
8616 last = BIND_EXPR_BODY (body);
8617
8618 continue_searching:
8619 if (TREE_CODE (last) == STATEMENT_LIST)
8620 {
8621 tree_stmt_iterator i;
8622
8623 /* This can happen with degenerate cases like ({ }). No value. */
8624 if (!TREE_SIDE_EFFECTS (last))
8625 return body;
8626
8627 /* If we're supposed to generate side effects warnings, process
8628 all of the statements except the last. */
8629 if (warn_unused_value)
8630 {
8631 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8632 {
8633 location_t tloc;
8634 tree t = tsi_stmt (i);
8635
8636 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
8637 emit_side_effect_warnings (tloc, t);
8638 }
8639 }
8640 else
8641 i = tsi_last (last);
8642 last_p = tsi_stmt_ptr (i);
8643 last = *last_p;
8644 }
8645
8646 /* If the end of the list is exception related, then the list was split
8647 by a call to push_cleanup. Continue searching. */
8648 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8649 || TREE_CODE (last) == TRY_CATCH_EXPR)
8650 {
8651 last_p = &TREE_OPERAND (last, 0);
8652 last = *last_p;
8653 goto continue_searching;
8654 }
8655
8656 /* In the case that the BIND_EXPR is not necessary, return the
8657 expression out from inside it. */
8658 if (last == error_mark_node
8659 || (last == BIND_EXPR_BODY (body)
8660 && BIND_EXPR_VARS (body) == NULL))
8661 {
8662 /* Even if this looks constant, do not allow it in a constant
8663 expression. */
8664 last = c_wrap_maybe_const (last, true);
8665 /* Do not warn if the return value of a statement expression is
8666 unused. */
8667 TREE_NO_WARNING (last) = 1;
8668 return last;
8669 }
8670
8671 /* Extract the type of said expression. */
8672 type = TREE_TYPE (last);
8673
8674 /* If we're not returning a value at all, then the BIND_EXPR that
8675 we already have is a fine expression to return. */
8676 if (!type || VOID_TYPE_P (type))
8677 return body;
8678
8679 /* Now that we've located the expression containing the value, it seems
8680 silly to make voidify_wrapper_expr repeat the process. Create a
8681 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8682 tmp = create_tmp_var_raw (type, NULL);
8683
8684 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8685 tree_expr_nonnegative_p giving up immediately. */
8686 val = last;
8687 if (TREE_CODE (val) == NOP_EXPR
8688 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8689 val = TREE_OPERAND (val, 0);
8690
8691 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8692 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
8693
8694 {
8695 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8696 SET_EXPR_LOCATION (t, loc);
8697 return t;
8698 }
8699 }
8700 \f
8701 /* Begin and end compound statements. This is as simple as pushing
8702 and popping new statement lists from the tree. */
8703
8704 tree
8705 c_begin_compound_stmt (bool do_scope)
8706 {
8707 tree stmt = push_stmt_list ();
8708 if (do_scope)
8709 push_scope ();
8710 return stmt;
8711 }
8712
8713 /* End a compound statement. STMT is the statement. LOC is the
8714 location of the compound statement-- this is usually the location
8715 of the opening brace. */
8716
8717 tree
8718 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
8719 {
8720 tree block = NULL;
8721
8722 if (do_scope)
8723 {
8724 if (c_dialect_objc ())
8725 objc_clear_super_receiver ();
8726 block = pop_scope ();
8727 }
8728
8729 stmt = pop_stmt_list (stmt);
8730 stmt = c_build_bind_expr (loc, block, stmt);
8731
8732 /* If this compound statement is nested immediately inside a statement
8733 expression, then force a BIND_EXPR to be created. Otherwise we'll
8734 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8735 STATEMENT_LISTs merge, and thus we can lose track of what statement
8736 was really last. */
8737 if (cur_stmt_list
8738 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8739 && TREE_CODE (stmt) != BIND_EXPR)
8740 {
8741 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8742 TREE_SIDE_EFFECTS (stmt) = 1;
8743 SET_EXPR_LOCATION (stmt, loc);
8744 }
8745
8746 return stmt;
8747 }
8748
8749 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8750 when the current scope is exited. EH_ONLY is true when this is not
8751 meant to apply to normal control flow transfer. */
8752
8753 void
8754 push_cleanup (tree decl, tree cleanup, bool eh_only)
8755 {
8756 enum tree_code code;
8757 tree stmt, list;
8758 bool stmt_expr;
8759
8760 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8761 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
8762 add_stmt (stmt);
8763 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8764 list = push_stmt_list ();
8765 TREE_OPERAND (stmt, 0) = list;
8766 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8767 }
8768 \f
8769 /* Build a binary-operation expression without default conversions.
8770 CODE is the kind of expression to build.
8771 LOCATION is the operator's location.
8772 This function differs from `build' in several ways:
8773 the data type of the result is computed and recorded in it,
8774 warnings are generated if arg data types are invalid,
8775 special handling for addition and subtraction of pointers is known,
8776 and some optimization is done (operations on narrow ints
8777 are done in the narrower type when that gives the same result).
8778 Constant folding is also done before the result is returned.
8779
8780 Note that the operands will never have enumeral types, or function
8781 or array types, because either they will have the default conversions
8782 performed or they have both just been converted to some other type in which
8783 the arithmetic is to be done. */
8784
8785 tree
8786 build_binary_op (location_t location, enum tree_code code,
8787 tree orig_op0, tree orig_op1, int convert_p)
8788 {
8789 tree type0, type1, orig_type0, orig_type1;
8790 tree eptype;
8791 enum tree_code code0, code1;
8792 tree op0, op1;
8793 tree ret = error_mark_node;
8794 const char *invalid_op_diag;
8795 bool op0_int_operands, op1_int_operands;
8796 bool int_const, int_const_or_overflow, int_operands;
8797
8798 /* Expression code to give to the expression when it is built.
8799 Normally this is CODE, which is what the caller asked for,
8800 but in some special cases we change it. */
8801 enum tree_code resultcode = code;
8802
8803 /* Data type in which the computation is to be performed.
8804 In the simplest cases this is the common type of the arguments. */
8805 tree result_type = NULL;
8806
8807 /* When the computation is in excess precision, the type of the
8808 final EXCESS_PRECISION_EXPR. */
8809 tree real_result_type = NULL;
8810
8811 /* Nonzero means operands have already been type-converted
8812 in whatever way is necessary.
8813 Zero means they need to be converted to RESULT_TYPE. */
8814 int converted = 0;
8815
8816 /* Nonzero means create the expression with this type, rather than
8817 RESULT_TYPE. */
8818 tree build_type = 0;
8819
8820 /* Nonzero means after finally constructing the expression
8821 convert it to this type. */
8822 tree final_type = 0;
8823
8824 /* Nonzero if this is an operation like MIN or MAX which can
8825 safely be computed in short if both args are promoted shorts.
8826 Also implies COMMON.
8827 -1 indicates a bitwise operation; this makes a difference
8828 in the exact conditions for when it is safe to do the operation
8829 in a narrower mode. */
8830 int shorten = 0;
8831
8832 /* Nonzero if this is a comparison operation;
8833 if both args are promoted shorts, compare the original shorts.
8834 Also implies COMMON. */
8835 int short_compare = 0;
8836
8837 /* Nonzero if this is a right-shift operation, which can be computed on the
8838 original short and then promoted if the operand is a promoted short. */
8839 int short_shift = 0;
8840
8841 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8842 int common = 0;
8843
8844 /* True means types are compatible as far as ObjC is concerned. */
8845 bool objc_ok;
8846
8847 /* True means this is an arithmetic operation that may need excess
8848 precision. */
8849 bool may_need_excess_precision;
8850
8851 if (location == UNKNOWN_LOCATION)
8852 location = input_location;
8853
8854 op0 = orig_op0;
8855 op1 = orig_op1;
8856
8857 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
8858 if (op0_int_operands)
8859 op0 = remove_c_maybe_const_expr (op0);
8860 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
8861 if (op1_int_operands)
8862 op1 = remove_c_maybe_const_expr (op1);
8863 int_operands = (op0_int_operands && op1_int_operands);
8864 if (int_operands)
8865 {
8866 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
8867 && TREE_CODE (orig_op1) == INTEGER_CST);
8868 int_const = (int_const_or_overflow
8869 && !TREE_OVERFLOW (orig_op0)
8870 && !TREE_OVERFLOW (orig_op1));
8871 }
8872 else
8873 int_const = int_const_or_overflow = false;
8874
8875 if (convert_p)
8876 {
8877 op0 = default_conversion (op0);
8878 op1 = default_conversion (op1);
8879 }
8880
8881 orig_type0 = type0 = TREE_TYPE (op0);
8882 orig_type1 = type1 = TREE_TYPE (op1);
8883
8884 /* The expression codes of the data types of the arguments tell us
8885 whether the arguments are integers, floating, pointers, etc. */
8886 code0 = TREE_CODE (type0);
8887 code1 = TREE_CODE (type1);
8888
8889 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8890 STRIP_TYPE_NOPS (op0);
8891 STRIP_TYPE_NOPS (op1);
8892
8893 /* If an error was already reported for one of the arguments,
8894 avoid reporting another error. */
8895
8896 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8897 return error_mark_node;
8898
8899 if ((invalid_op_diag
8900 = targetm.invalid_binary_op (code, type0, type1)))
8901 {
8902 error_at (location, invalid_op_diag);
8903 return error_mark_node;
8904 }
8905
8906 switch (code)
8907 {
8908 case PLUS_EXPR:
8909 case MINUS_EXPR:
8910 case MULT_EXPR:
8911 case TRUNC_DIV_EXPR:
8912 case CEIL_DIV_EXPR:
8913 case FLOOR_DIV_EXPR:
8914 case ROUND_DIV_EXPR:
8915 case EXACT_DIV_EXPR:
8916 may_need_excess_precision = true;
8917 break;
8918 default:
8919 may_need_excess_precision = false;
8920 break;
8921 }
8922 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
8923 {
8924 op0 = TREE_OPERAND (op0, 0);
8925 type0 = TREE_TYPE (op0);
8926 }
8927 else if (may_need_excess_precision
8928 && (eptype = excess_precision_type (type0)) != NULL_TREE)
8929 {
8930 type0 = eptype;
8931 op0 = convert (eptype, op0);
8932 }
8933 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
8934 {
8935 op1 = TREE_OPERAND (op1, 0);
8936 type1 = TREE_TYPE (op1);
8937 }
8938 else if (may_need_excess_precision
8939 && (eptype = excess_precision_type (type1)) != NULL_TREE)
8940 {
8941 type1 = eptype;
8942 op1 = convert (eptype, op1);
8943 }
8944
8945 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
8946
8947 switch (code)
8948 {
8949 case PLUS_EXPR:
8950 /* Handle the pointer + int case. */
8951 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8952 {
8953 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
8954 goto return_build_binary_op;
8955 }
8956 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8957 {
8958 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
8959 goto return_build_binary_op;
8960 }
8961 else
8962 common = 1;
8963 break;
8964
8965 case MINUS_EXPR:
8966 /* Subtraction of two similar pointers.
8967 We must subtract them as integers, then divide by object size. */
8968 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8969 && comp_target_types (location, type0, type1))
8970 {
8971 ret = pointer_diff (location, op0, op1);
8972 goto return_build_binary_op;
8973 }
8974 /* Handle pointer minus int. Just like pointer plus int. */
8975 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8976 {
8977 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
8978 goto return_build_binary_op;
8979 }
8980 else
8981 common = 1;
8982 break;
8983
8984 case MULT_EXPR:
8985 common = 1;
8986 break;
8987
8988 case TRUNC_DIV_EXPR:
8989 case CEIL_DIV_EXPR:
8990 case FLOOR_DIV_EXPR:
8991 case ROUND_DIV_EXPR:
8992 case EXACT_DIV_EXPR:
8993 warn_for_div_by_zero (location, op1);
8994
8995 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8996 || code0 == FIXED_POINT_TYPE
8997 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8998 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8999 || code1 == FIXED_POINT_TYPE
9000 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9001 {
9002 enum tree_code tcode0 = code0, tcode1 = code1;
9003
9004 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9005 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9006 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9007 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9008
9009 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9010 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9011 resultcode = RDIV_EXPR;
9012 else
9013 /* Although it would be tempting to shorten always here, that
9014 loses on some targets, since the modulo instruction is
9015 undefined if the quotient can't be represented in the
9016 computation mode. We shorten only if unsigned or if
9017 dividing by something we know != -1. */
9018 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9019 || (TREE_CODE (op1) == INTEGER_CST
9020 && !integer_all_onesp (op1)));
9021 common = 1;
9022 }
9023 break;
9024
9025 case BIT_AND_EXPR:
9026 case BIT_IOR_EXPR:
9027 case BIT_XOR_EXPR:
9028 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9029 shorten = -1;
9030 /* Allow vector types which are not floating point types. */
9031 else if (code0 == VECTOR_TYPE
9032 && code1 == VECTOR_TYPE
9033 && !VECTOR_FLOAT_TYPE_P (type0)
9034 && !VECTOR_FLOAT_TYPE_P (type1))
9035 common = 1;
9036 break;
9037
9038 case TRUNC_MOD_EXPR:
9039 case FLOOR_MOD_EXPR:
9040 warn_for_div_by_zero (location, op1);
9041
9042 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9043 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9044 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9045 common = 1;
9046 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9047 {
9048 /* Although it would be tempting to shorten always here, that loses
9049 on some targets, since the modulo instruction is undefined if the
9050 quotient can't be represented in the computation mode. We shorten
9051 only if unsigned or if dividing by something we know != -1. */
9052 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9053 || (TREE_CODE (op1) == INTEGER_CST
9054 && !integer_all_onesp (op1)));
9055 common = 1;
9056 }
9057 break;
9058
9059 case TRUTH_ANDIF_EXPR:
9060 case TRUTH_ORIF_EXPR:
9061 case TRUTH_AND_EXPR:
9062 case TRUTH_OR_EXPR:
9063 case TRUTH_XOR_EXPR:
9064 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9065 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9066 || code0 == FIXED_POINT_TYPE)
9067 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9068 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9069 || code1 == FIXED_POINT_TYPE))
9070 {
9071 /* Result of these operations is always an int,
9072 but that does not mean the operands should be
9073 converted to ints! */
9074 result_type = integer_type_node;
9075 op0 = c_common_truthvalue_conversion (location, op0);
9076 op1 = c_common_truthvalue_conversion (location, op1);
9077 converted = 1;
9078 }
9079 if (code == TRUTH_ANDIF_EXPR)
9080 {
9081 int_const_or_overflow = (int_operands
9082 && TREE_CODE (orig_op0) == INTEGER_CST
9083 && (op0 == truthvalue_false_node
9084 || TREE_CODE (orig_op1) == INTEGER_CST));
9085 int_const = (int_const_or_overflow
9086 && !TREE_OVERFLOW (orig_op0)
9087 && (op0 == truthvalue_false_node
9088 || !TREE_OVERFLOW (orig_op1)));
9089 }
9090 else if (code == TRUTH_ORIF_EXPR)
9091 {
9092 int_const_or_overflow = (int_operands
9093 && TREE_CODE (orig_op0) == INTEGER_CST
9094 && (op0 == truthvalue_true_node
9095 || TREE_CODE (orig_op1) == INTEGER_CST));
9096 int_const = (int_const_or_overflow
9097 && !TREE_OVERFLOW (orig_op0)
9098 && (op0 == truthvalue_true_node
9099 || !TREE_OVERFLOW (orig_op1)));
9100 }
9101 break;
9102
9103 /* Shift operations: result has same type as first operand;
9104 always convert second operand to int.
9105 Also set SHORT_SHIFT if shifting rightward. */
9106
9107 case RSHIFT_EXPR:
9108 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9109 && code1 == INTEGER_TYPE)
9110 {
9111 if (TREE_CODE (op1) == INTEGER_CST)
9112 {
9113 if (tree_int_cst_sgn (op1) < 0)
9114 {
9115 int_const = false;
9116 if (c_inhibit_evaluation_warnings == 0)
9117 warning (0, "right shift count is negative");
9118 }
9119 else
9120 {
9121 if (!integer_zerop (op1))
9122 short_shift = 1;
9123
9124 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9125 {
9126 int_const = false;
9127 if (c_inhibit_evaluation_warnings == 0)
9128 warning (0, "right shift count >= width of type");
9129 }
9130 }
9131 }
9132
9133 /* Use the type of the value to be shifted. */
9134 result_type = type0;
9135 /* Convert the shift-count to an integer, regardless of size
9136 of value being shifted. */
9137 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9138 op1 = convert (integer_type_node, op1);
9139 /* Avoid converting op1 to result_type later. */
9140 converted = 1;
9141 }
9142 break;
9143
9144 case LSHIFT_EXPR:
9145 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9146 && code1 == INTEGER_TYPE)
9147 {
9148 if (TREE_CODE (op1) == INTEGER_CST)
9149 {
9150 if (tree_int_cst_sgn (op1) < 0)
9151 {
9152 int_const = false;
9153 if (c_inhibit_evaluation_warnings == 0)
9154 warning (0, "left shift count is negative");
9155 }
9156
9157 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9158 {
9159 int_const = false;
9160 if (c_inhibit_evaluation_warnings == 0)
9161 warning (0, "left shift count >= width of type");
9162 }
9163 }
9164
9165 /* Use the type of the value to be shifted. */
9166 result_type = type0;
9167 /* Convert the shift-count to an integer, regardless of size
9168 of value being shifted. */
9169 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9170 op1 = convert (integer_type_node, op1);
9171 /* Avoid converting op1 to result_type later. */
9172 converted = 1;
9173 }
9174 break;
9175
9176 case EQ_EXPR:
9177 case NE_EXPR:
9178 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9179 warning_at (location,
9180 OPT_Wfloat_equal,
9181 "comparing floating point with == or != is unsafe");
9182 /* Result of comparison is always int,
9183 but don't convert the args to int! */
9184 build_type = integer_type_node;
9185 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9186 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9187 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9188 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9189 short_compare = 1;
9190 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9191 {
9192 tree tt0 = TREE_TYPE (type0);
9193 tree tt1 = TREE_TYPE (type1);
9194 /* Anything compares with void *. void * compares with anything.
9195 Otherwise, the targets must be compatible
9196 and both must be object or both incomplete. */
9197 if (comp_target_types (location, type0, type1))
9198 result_type = common_pointer_type (type0, type1);
9199 else if (VOID_TYPE_P (tt0))
9200 {
9201 /* op0 != orig_op0 detects the case of something
9202 whose value is 0 but which isn't a valid null ptr const. */
9203 if (pedantic && !null_pointer_constant_p (orig_op0)
9204 && TREE_CODE (tt1) == FUNCTION_TYPE)
9205 pedwarn (location, OPT_pedantic, "ISO C forbids "
9206 "comparison of %<void *%> with function pointer");
9207 }
9208 else if (VOID_TYPE_P (tt1))
9209 {
9210 if (pedantic && !null_pointer_constant_p (orig_op1)
9211 && TREE_CODE (tt0) == FUNCTION_TYPE)
9212 pedwarn (location, OPT_pedantic, "ISO C forbids "
9213 "comparison of %<void *%> with function pointer");
9214 }
9215 else
9216 /* Avoid warning about the volatile ObjC EH puts on decls. */
9217 if (!objc_ok)
9218 pedwarn (location, 0,
9219 "comparison of distinct pointer types lacks a cast");
9220
9221 if (result_type == NULL_TREE)
9222 result_type = ptr_type_node;
9223 }
9224 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9225 {
9226 if (TREE_CODE (op0) == ADDR_EXPR
9227 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9228 warning_at (location,
9229 OPT_Waddress, "the address of %qD will never be NULL",
9230 TREE_OPERAND (op0, 0));
9231 result_type = type0;
9232 }
9233 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9234 {
9235 if (TREE_CODE (op1) == ADDR_EXPR
9236 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9237 warning_at (location,
9238 OPT_Waddress, "the address of %qD will never be NULL",
9239 TREE_OPERAND (op1, 0));
9240 result_type = type1;
9241 }
9242 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9243 {
9244 result_type = type0;
9245 pedwarn (location, 0, "comparison between pointer and integer");
9246 }
9247 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9248 {
9249 result_type = type1;
9250 pedwarn (location, 0, "comparison between pointer and integer");
9251 }
9252 break;
9253
9254 case LE_EXPR:
9255 case GE_EXPR:
9256 case LT_EXPR:
9257 case GT_EXPR:
9258 build_type = integer_type_node;
9259 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9260 || code0 == FIXED_POINT_TYPE)
9261 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9262 || code1 == FIXED_POINT_TYPE))
9263 short_compare = 1;
9264 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9265 {
9266 if (comp_target_types (location, type0, type1))
9267 {
9268 result_type = common_pointer_type (type0, type1);
9269 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9270 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9271 pedwarn (location, 0,
9272 "comparison of complete and incomplete pointers");
9273 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9274 pedwarn (location, OPT_pedantic, "ISO C forbids "
9275 "ordered comparisons of pointers to functions");
9276 }
9277 else
9278 {
9279 result_type = ptr_type_node;
9280 pedwarn (location, 0,
9281 "comparison of distinct pointer types lacks a cast");
9282 }
9283 }
9284 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9285 {
9286 result_type = type0;
9287 if (pedantic)
9288 pedwarn (location, OPT_pedantic,
9289 "ordered comparison of pointer with integer zero");
9290 else if (extra_warnings)
9291 warning_at (location, OPT_Wextra,
9292 "ordered comparison of pointer with integer zero");
9293 }
9294 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9295 {
9296 result_type = type1;
9297 pedwarn (location, OPT_pedantic,
9298 "ordered comparison of pointer with integer zero");
9299 }
9300 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9301 {
9302 result_type = type0;
9303 pedwarn (location, 0, "comparison between pointer and integer");
9304 }
9305 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9306 {
9307 result_type = type1;
9308 pedwarn (location, 0, "comparison between pointer and integer");
9309 }
9310 break;
9311
9312 default:
9313 gcc_unreachable ();
9314 }
9315
9316 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9317 return error_mark_node;
9318
9319 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9320 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9321 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9322 TREE_TYPE (type1))))
9323 {
9324 binary_op_error (location, code, type0, type1);
9325 return error_mark_node;
9326 }
9327
9328 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9329 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9330 &&
9331 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9332 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9333 {
9334 bool first_complex = (code0 == COMPLEX_TYPE);
9335 bool second_complex = (code1 == COMPLEX_TYPE);
9336 int none_complex = (!first_complex && !second_complex);
9337
9338 if (shorten || common || short_compare)
9339 {
9340 result_type = c_common_type (type0, type1);
9341 if (result_type == error_mark_node)
9342 return error_mark_node;
9343 }
9344
9345 if (first_complex != second_complex
9346 && (code == PLUS_EXPR
9347 || code == MINUS_EXPR
9348 || code == MULT_EXPR
9349 || (code == TRUNC_DIV_EXPR && first_complex))
9350 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9351 && flag_signed_zeros)
9352 {
9353 /* An operation on mixed real/complex operands must be
9354 handled specially, but the language-independent code can
9355 more easily optimize the plain complex arithmetic if
9356 -fno-signed-zeros. */
9357 tree real_type = TREE_TYPE (result_type);
9358 tree real, imag;
9359 if (type0 != orig_type0 || type1 != orig_type1)
9360 {
9361 gcc_assert (may_need_excess_precision && common);
9362 real_result_type = c_common_type (orig_type0, orig_type1);
9363 }
9364 if (first_complex)
9365 {
9366 if (TREE_TYPE (op0) != result_type)
9367 op0 = convert_and_check (result_type, op0);
9368 if (TREE_TYPE (op1) != real_type)
9369 op1 = convert_and_check (real_type, op1);
9370 }
9371 else
9372 {
9373 if (TREE_TYPE (op0) != real_type)
9374 op0 = convert_and_check (real_type, op0);
9375 if (TREE_TYPE (op1) != result_type)
9376 op1 = convert_and_check (result_type, op1);
9377 }
9378 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9379 return error_mark_node;
9380 if (first_complex)
9381 {
9382 op0 = c_save_expr (op0);
9383 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9384 op0, 1);
9385 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9386 op0, 1);
9387 switch (code)
9388 {
9389 case MULT_EXPR:
9390 case TRUNC_DIV_EXPR:
9391 imag = build2 (resultcode, real_type, imag, op1);
9392 /* Fall through. */
9393 case PLUS_EXPR:
9394 case MINUS_EXPR:
9395 real = build2 (resultcode, real_type, real, op1);
9396 break;
9397 default:
9398 gcc_unreachable();
9399 }
9400 }
9401 else
9402 {
9403 op1 = c_save_expr (op1);
9404 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9405 op1, 1);
9406 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9407 op1, 1);
9408 switch (code)
9409 {
9410 case MULT_EXPR:
9411 imag = build2 (resultcode, real_type, op0, imag);
9412 /* Fall through. */
9413 case PLUS_EXPR:
9414 real = build2 (resultcode, real_type, op0, real);
9415 break;
9416 case MINUS_EXPR:
9417 real = build2 (resultcode, real_type, op0, real);
9418 imag = build1 (NEGATE_EXPR, real_type, imag);
9419 break;
9420 default:
9421 gcc_unreachable();
9422 }
9423 }
9424 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9425 goto return_build_binary_op;
9426 }
9427
9428 /* For certain operations (which identify themselves by shorten != 0)
9429 if both args were extended from the same smaller type,
9430 do the arithmetic in that type and then extend.
9431
9432 shorten !=0 and !=1 indicates a bitwise operation.
9433 For them, this optimization is safe only if
9434 both args are zero-extended or both are sign-extended.
9435 Otherwise, we might change the result.
9436 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9437 but calculated in (unsigned short) it would be (unsigned short)-1. */
9438
9439 if (shorten && none_complex)
9440 {
9441 final_type = result_type;
9442 result_type = shorten_binary_op (result_type, op0, op1,
9443 shorten == -1);
9444 }
9445
9446 /* Shifts can be shortened if shifting right. */
9447
9448 if (short_shift)
9449 {
9450 int unsigned_arg;
9451 tree arg0 = get_narrower (op0, &unsigned_arg);
9452
9453 final_type = result_type;
9454
9455 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9456 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9457
9458 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9459 && tree_int_cst_sgn (op1) > 0
9460 /* We can shorten only if the shift count is less than the
9461 number of bits in the smaller type size. */
9462 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9463 /* We cannot drop an unsigned shift after sign-extension. */
9464 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9465 {
9466 /* Do an unsigned shift if the operand was zero-extended. */
9467 result_type
9468 = c_common_signed_or_unsigned_type (unsigned_arg,
9469 TREE_TYPE (arg0));
9470 /* Convert value-to-be-shifted to that type. */
9471 if (TREE_TYPE (op0) != result_type)
9472 op0 = convert (result_type, op0);
9473 converted = 1;
9474 }
9475 }
9476
9477 /* Comparison operations are shortened too but differently.
9478 They identify themselves by setting short_compare = 1. */
9479
9480 if (short_compare)
9481 {
9482 /* Don't write &op0, etc., because that would prevent op0
9483 from being kept in a register.
9484 Instead, make copies of the our local variables and
9485 pass the copies by reference, then copy them back afterward. */
9486 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9487 enum tree_code xresultcode = resultcode;
9488 tree val
9489 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9490
9491 if (val != 0)
9492 {
9493 ret = val;
9494 goto return_build_binary_op;
9495 }
9496
9497 op0 = xop0, op1 = xop1;
9498 converted = 1;
9499 resultcode = xresultcode;
9500
9501 if (c_inhibit_evaluation_warnings == 0)
9502 {
9503 bool op0_maybe_const = true;
9504 bool op1_maybe_const = true;
9505 tree orig_op0_folded, orig_op1_folded;
9506
9507 if (in_late_binary_op)
9508 {
9509 orig_op0_folded = orig_op0;
9510 orig_op1_folded = orig_op1;
9511 }
9512 else
9513 {
9514 /* Fold for the sake of possible warnings, as in
9515 build_conditional_expr. This requires the
9516 "original" values to be folded, not just op0 and
9517 op1. */
9518 c_inhibit_evaluation_warnings++;
9519 op0 = c_fully_fold (op0, require_constant_value,
9520 &op0_maybe_const);
9521 op1 = c_fully_fold (op1, require_constant_value,
9522 &op1_maybe_const);
9523 c_inhibit_evaluation_warnings--;
9524 orig_op0_folded = c_fully_fold (orig_op0,
9525 require_constant_value,
9526 NULL);
9527 orig_op1_folded = c_fully_fold (orig_op1,
9528 require_constant_value,
9529 NULL);
9530 }
9531
9532 if (warn_sign_compare)
9533 warn_for_sign_compare (location, orig_op0_folded,
9534 orig_op1_folded, op0, op1,
9535 result_type, resultcode);
9536 if (!in_late_binary_op)
9537 {
9538 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9539 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
9540 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9541 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
9542 }
9543 }
9544 }
9545 }
9546
9547 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9548 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9549 Then the expression will be built.
9550 It will be given type FINAL_TYPE if that is nonzero;
9551 otherwise, it will be given type RESULT_TYPE. */
9552
9553 if (!result_type)
9554 {
9555 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9556 return error_mark_node;
9557 }
9558
9559 if (!converted)
9560 {
9561 if (TREE_TYPE (op0) != result_type)
9562 op0 = convert_and_check (result_type, op0);
9563 if (TREE_TYPE (op1) != result_type)
9564 op1 = convert_and_check (result_type, op1);
9565
9566 /* This can happen if one operand has a vector type, and the other
9567 has a different type. */
9568 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9569 return error_mark_node;
9570 }
9571
9572 if (build_type == NULL_TREE)
9573 {
9574 build_type = result_type;
9575 if (type0 != orig_type0 || type1 != orig_type1)
9576 {
9577 gcc_assert (may_need_excess_precision && common);
9578 real_result_type = c_common_type (orig_type0, orig_type1);
9579 }
9580 }
9581
9582 /* Treat expressions in initializers specially as they can't trap. */
9583 if (int_const_or_overflow)
9584 ret = (require_constant_value
9585 ? fold_build2_initializer_loc (location, resultcode, build_type,
9586 op0, op1)
9587 : fold_build2_loc (location, resultcode, build_type, op0, op1));
9588 else
9589 ret = build2 (resultcode, build_type, op0, op1);
9590 if (final_type != 0)
9591 ret = convert (final_type, ret);
9592
9593 return_build_binary_op:
9594 gcc_assert (ret != error_mark_node);
9595 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9596 ret = (int_operands
9597 ? note_integer_operands (ret)
9598 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9599 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9600 && !in_late_binary_op)
9601 ret = note_integer_operands (ret);
9602 if (real_result_type)
9603 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9604 protected_set_expr_location (ret, location);
9605 return ret;
9606 }
9607
9608
9609 /* Convert EXPR to be a truth-value, validating its type for this
9610 purpose. LOCATION is the source location for the expression. */
9611
9612 tree
9613 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9614 {
9615 bool int_const, int_operands;
9616
9617 switch (TREE_CODE (TREE_TYPE (expr)))
9618 {
9619 case ARRAY_TYPE:
9620 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9621 return error_mark_node;
9622
9623 case RECORD_TYPE:
9624 error_at (location, "used struct type value where scalar is required");
9625 return error_mark_node;
9626
9627 case UNION_TYPE:
9628 error_at (location, "used union type value where scalar is required");
9629 return error_mark_node;
9630
9631 case FUNCTION_TYPE:
9632 gcc_unreachable ();
9633
9634 default:
9635 break;
9636 }
9637
9638 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9639 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9640 if (int_operands)
9641 expr = remove_c_maybe_const_expr (expr);
9642
9643 /* ??? Should we also give an error for void and vectors rather than
9644 leaving those to give errors later? */
9645 expr = c_common_truthvalue_conversion (location, expr);
9646
9647 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9648 {
9649 if (TREE_OVERFLOW (expr))
9650 return expr;
9651 else
9652 return note_integer_operands (expr);
9653 }
9654 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9655 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9656 return expr;
9657 }
9658 \f
9659
9660 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9661 required. */
9662
9663 tree
9664 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9665 {
9666 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9667 {
9668 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9669 /* Executing a compound literal inside a function reinitializes
9670 it. */
9671 if (!TREE_STATIC (decl))
9672 *se = true;
9673 return decl;
9674 }
9675 else
9676 return expr;
9677 }
9678 \f
9679 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9680
9681 tree
9682 c_begin_omp_parallel (void)
9683 {
9684 tree block;
9685
9686 keep_next_level ();
9687 block = c_begin_compound_stmt (true);
9688
9689 return block;
9690 }
9691
9692 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9693 statement. LOC is the location of the OMP_PARALLEL. */
9694
9695 tree
9696 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
9697 {
9698 tree stmt;
9699
9700 block = c_end_compound_stmt (loc, block, true);
9701
9702 stmt = make_node (OMP_PARALLEL);
9703 TREE_TYPE (stmt) = void_type_node;
9704 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9705 OMP_PARALLEL_BODY (stmt) = block;
9706 SET_EXPR_LOCATION (stmt, loc);
9707
9708 return add_stmt (stmt);
9709 }
9710
9711 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9712
9713 tree
9714 c_begin_omp_task (void)
9715 {
9716 tree block;
9717
9718 keep_next_level ();
9719 block = c_begin_compound_stmt (true);
9720
9721 return block;
9722 }
9723
9724 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9725 statement. LOC is the location of the #pragma. */
9726
9727 tree
9728 c_finish_omp_task (location_t loc, tree clauses, tree block)
9729 {
9730 tree stmt;
9731
9732 block = c_end_compound_stmt (loc, block, true);
9733
9734 stmt = make_node (OMP_TASK);
9735 TREE_TYPE (stmt) = void_type_node;
9736 OMP_TASK_CLAUSES (stmt) = clauses;
9737 OMP_TASK_BODY (stmt) = block;
9738 SET_EXPR_LOCATION (stmt, loc);
9739
9740 return add_stmt (stmt);
9741 }
9742
9743 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9744 Remove any elements from the list that are invalid. */
9745
9746 tree
9747 c_finish_omp_clauses (tree clauses)
9748 {
9749 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9750 tree c, t, *pc = &clauses;
9751 const char *name;
9752
9753 bitmap_obstack_initialize (NULL);
9754 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9755 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9756 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9757
9758 for (pc = &clauses, c = clauses; c ; c = *pc)
9759 {
9760 bool remove = false;
9761 bool need_complete = false;
9762 bool need_implicitly_determined = false;
9763
9764 switch (OMP_CLAUSE_CODE (c))
9765 {
9766 case OMP_CLAUSE_SHARED:
9767 name = "shared";
9768 need_implicitly_determined = true;
9769 goto check_dup_generic;
9770
9771 case OMP_CLAUSE_PRIVATE:
9772 name = "private";
9773 need_complete = true;
9774 need_implicitly_determined = true;
9775 goto check_dup_generic;
9776
9777 case OMP_CLAUSE_REDUCTION:
9778 name = "reduction";
9779 need_implicitly_determined = true;
9780 t = OMP_CLAUSE_DECL (c);
9781 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9782 || POINTER_TYPE_P (TREE_TYPE (t)))
9783 {
9784 error_at (OMP_CLAUSE_LOCATION (c),
9785 "%qE has invalid type for %<reduction%>", t);
9786 remove = true;
9787 }
9788 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9789 {
9790 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9791 const char *r_name = NULL;
9792
9793 switch (r_code)
9794 {
9795 case PLUS_EXPR:
9796 case MULT_EXPR:
9797 case MINUS_EXPR:
9798 break;
9799 case BIT_AND_EXPR:
9800 r_name = "&";
9801 break;
9802 case BIT_XOR_EXPR:
9803 r_name = "^";
9804 break;
9805 case BIT_IOR_EXPR:
9806 r_name = "|";
9807 break;
9808 case TRUTH_ANDIF_EXPR:
9809 r_name = "&&";
9810 break;
9811 case TRUTH_ORIF_EXPR:
9812 r_name = "||";
9813 break;
9814 default:
9815 gcc_unreachable ();
9816 }
9817 if (r_name)
9818 {
9819 error_at (OMP_CLAUSE_LOCATION (c),
9820 "%qE has invalid type for %<reduction(%s)%>",
9821 t, r_name);
9822 remove = true;
9823 }
9824 }
9825 goto check_dup_generic;
9826
9827 case OMP_CLAUSE_COPYPRIVATE:
9828 name = "copyprivate";
9829 goto check_dup_generic;
9830
9831 case OMP_CLAUSE_COPYIN:
9832 name = "copyin";
9833 t = OMP_CLAUSE_DECL (c);
9834 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9835 {
9836 error_at (OMP_CLAUSE_LOCATION (c),
9837 "%qE must be %<threadprivate%> for %<copyin%>", t);
9838 remove = true;
9839 }
9840 goto check_dup_generic;
9841
9842 check_dup_generic:
9843 t = OMP_CLAUSE_DECL (c);
9844 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9845 {
9846 error_at (OMP_CLAUSE_LOCATION (c),
9847 "%qE is not a variable in clause %qs", t, name);
9848 remove = true;
9849 }
9850 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9851 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9852 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9853 {
9854 error_at (OMP_CLAUSE_LOCATION (c),
9855 "%qE appears more than once in data clauses", t);
9856 remove = true;
9857 }
9858 else
9859 bitmap_set_bit (&generic_head, DECL_UID (t));
9860 break;
9861
9862 case OMP_CLAUSE_FIRSTPRIVATE:
9863 name = "firstprivate";
9864 t = OMP_CLAUSE_DECL (c);
9865 need_complete = true;
9866 need_implicitly_determined = true;
9867 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9868 {
9869 error_at (OMP_CLAUSE_LOCATION (c),
9870 "%qE is not a variable in clause %<firstprivate%>", t);
9871 remove = true;
9872 }
9873 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9874 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9875 {
9876 error_at (OMP_CLAUSE_LOCATION (c),
9877 "%qE appears more than once in data clauses", t);
9878 remove = true;
9879 }
9880 else
9881 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9882 break;
9883
9884 case OMP_CLAUSE_LASTPRIVATE:
9885 name = "lastprivate";
9886 t = OMP_CLAUSE_DECL (c);
9887 need_complete = true;
9888 need_implicitly_determined = true;
9889 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9890 {
9891 error_at (OMP_CLAUSE_LOCATION (c),
9892 "%qE is not a variable in clause %<lastprivate%>", t);
9893 remove = true;
9894 }
9895 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9896 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9897 {
9898 error_at (OMP_CLAUSE_LOCATION (c),
9899 "%qE appears more than once in data clauses", t);
9900 remove = true;
9901 }
9902 else
9903 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9904 break;
9905
9906 case OMP_CLAUSE_IF:
9907 case OMP_CLAUSE_NUM_THREADS:
9908 case OMP_CLAUSE_SCHEDULE:
9909 case OMP_CLAUSE_NOWAIT:
9910 case OMP_CLAUSE_ORDERED:
9911 case OMP_CLAUSE_DEFAULT:
9912 case OMP_CLAUSE_UNTIED:
9913 case OMP_CLAUSE_COLLAPSE:
9914 pc = &OMP_CLAUSE_CHAIN (c);
9915 continue;
9916
9917 default:
9918 gcc_unreachable ();
9919 }
9920
9921 if (!remove)
9922 {
9923 t = OMP_CLAUSE_DECL (c);
9924
9925 if (need_complete)
9926 {
9927 t = require_complete_type (t);
9928 if (t == error_mark_node)
9929 remove = true;
9930 }
9931
9932 if (need_implicitly_determined)
9933 {
9934 const char *share_name = NULL;
9935
9936 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9937 share_name = "threadprivate";
9938 else switch (c_omp_predetermined_sharing (t))
9939 {
9940 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9941 break;
9942 case OMP_CLAUSE_DEFAULT_SHARED:
9943 share_name = "shared";
9944 break;
9945 case OMP_CLAUSE_DEFAULT_PRIVATE:
9946 share_name = "private";
9947 break;
9948 default:
9949 gcc_unreachable ();
9950 }
9951 if (share_name)
9952 {
9953 error_at (OMP_CLAUSE_LOCATION (c),
9954 "%qE is predetermined %qs for %qs",
9955 t, share_name, name);
9956 remove = true;
9957 }
9958 }
9959 }
9960
9961 if (remove)
9962 *pc = OMP_CLAUSE_CHAIN (c);
9963 else
9964 pc = &OMP_CLAUSE_CHAIN (c);
9965 }
9966
9967 bitmap_obstack_release (NULL);
9968 return clauses;
9969 }
9970
9971 /* Make a variant type in the proper way for C/C++, propagating qualifiers
9972 down to the element type of an array. */
9973
9974 tree
9975 c_build_qualified_type (tree type, int type_quals)
9976 {
9977 if (type == error_mark_node)
9978 return type;
9979
9980 if (TREE_CODE (type) == ARRAY_TYPE)
9981 {
9982 tree t;
9983 tree element_type = c_build_qualified_type (TREE_TYPE (type),
9984 type_quals);
9985
9986 /* See if we already have an identically qualified type. */
9987 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9988 {
9989 if (TYPE_QUALS (strip_array_types (t)) == type_quals
9990 && TYPE_NAME (t) == TYPE_NAME (type)
9991 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
9992 && attribute_list_equal (TYPE_ATTRIBUTES (t),
9993 TYPE_ATTRIBUTES (type)))
9994 break;
9995 }
9996 if (!t)
9997 {
9998 tree domain = TYPE_DOMAIN (type);
9999
10000 t = build_variant_type_copy (type);
10001 TREE_TYPE (t) = element_type;
10002
10003 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10004 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10005 SET_TYPE_STRUCTURAL_EQUALITY (t);
10006 else if (TYPE_CANONICAL (element_type) != element_type
10007 || (domain && TYPE_CANONICAL (domain) != domain))
10008 {
10009 tree unqualified_canon
10010 = build_array_type (TYPE_CANONICAL (element_type),
10011 domain? TYPE_CANONICAL (domain)
10012 : NULL_TREE);
10013 TYPE_CANONICAL (t)
10014 = c_build_qualified_type (unqualified_canon, type_quals);
10015 }
10016 else
10017 TYPE_CANONICAL (t) = t;
10018 }
10019 return t;
10020 }
10021
10022 /* A restrict-qualified pointer type must be a pointer to object or
10023 incomplete type. Note that the use of POINTER_TYPE_P also allows
10024 REFERENCE_TYPEs, which is appropriate for C++. */
10025 if ((type_quals & TYPE_QUAL_RESTRICT)
10026 && (!POINTER_TYPE_P (type)
10027 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10028 {
10029 error ("invalid use of %<restrict%>");
10030 type_quals &= ~TYPE_QUAL_RESTRICT;
10031 }
10032
10033 return build_qualified_type (type, type_quals);
10034 }
10035
10036 /* Build a VA_ARG_EXPR for the C parser. */
10037
10038 tree
10039 c_build_va_arg (location_t loc, tree expr, tree type)
10040 {
10041 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10042 warning_at (loc, OPT_Wc___compat,
10043 "C++ requires promoted type, not enum type, in %<va_arg%>");
10044 return build_va_arg (loc, expr, type);
10045 }