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