comparison gcc/c-typeck.c.orig @ 57:326d9e06c2e3

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