a89c87a36012b6aaf1faf6f6c213760659e88c1a
[gcc.git] / gcc / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
47
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
56 };
57
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
60
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
63
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
66
67 /* Nonzero if we've already printed a "missing braces around initializer"
68 message within this initializer. */
69 static int missing_braces_mentioned;
70
71 static int require_constant_value;
72 static int require_constant_elements;
73
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (tree, tree);
76 static int comp_target_types (tree, tree, int);
77 static int function_types_compatible_p (tree, tree);
78 static int type_lists_compatible_p (tree, tree);
79 static tree decl_constant_value_for_broken_optimization (tree);
80 static tree default_function_array_conversion (tree);
81 static tree lookup_field (tree, tree);
82 static tree convert_arguments (tree, tree, tree, tree);
83 static tree pointer_diff (tree, tree);
84 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
85 int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static void push_array_bounds (int);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (const char *);
93 static tree digest_init (tree, tree, bool, int);
94 static void output_init_element (tree, bool, tree, tree, int);
95 static void output_pending_init_elements (int);
96 static int set_designator (int);
97 static void push_range_stack (tree);
98 static void add_pending_init (tree, tree);
99 static void set_nonincremental_init (void);
100 static void set_nonincremental_init_from_string (tree);
101 static tree find_init_member (tree);
102 static void readonly_error (tree, enum lvalue_use);
103 static int lvalue_or_else (tree, enum lvalue_use);
104 static int lvalue_p (tree);
105 static void record_maybe_used_decl (tree);
106 \f
107 /* Do `exp = require_complete_type (exp);' to make sure exp
108 does not have an incomplete type. (That includes void types.) */
109
110 tree
111 require_complete_type (tree value)
112 {
113 tree type = TREE_TYPE (value);
114
115 if (value == error_mark_node || type == error_mark_node)
116 return error_mark_node;
117
118 /* First, detect a valid value with a complete type. */
119 if (COMPLETE_TYPE_P (type))
120 return value;
121
122 c_incomplete_type_error (value, type);
123 return error_mark_node;
124 }
125
126 /* Print an error message for invalid use of an incomplete type.
127 VALUE is the expression that was used (or 0 if that isn't known)
128 and TYPE is the type that was invalid. */
129
130 void
131 c_incomplete_type_error (tree value, tree type)
132 {
133 const char *type_code_string;
134
135 /* Avoid duplicate error message. */
136 if (TREE_CODE (type) == ERROR_MARK)
137 return;
138
139 if (value != 0 && (TREE_CODE (value) == VAR_DECL
140 || TREE_CODE (value) == PARM_DECL))
141 error ("%qD has an incomplete type", value);
142 else
143 {
144 retry:
145 /* We must print an error message. Be clever about what it says. */
146
147 switch (TREE_CODE (type))
148 {
149 case RECORD_TYPE:
150 type_code_string = "struct";
151 break;
152
153 case UNION_TYPE:
154 type_code_string = "union";
155 break;
156
157 case ENUMERAL_TYPE:
158 type_code_string = "enum";
159 break;
160
161 case VOID_TYPE:
162 error ("invalid use of void expression");
163 return;
164
165 case ARRAY_TYPE:
166 if (TYPE_DOMAIN (type))
167 {
168 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
169 {
170 error ("invalid use of flexible array member");
171 return;
172 }
173 type = TREE_TYPE (type);
174 goto retry;
175 }
176 error ("invalid use of array with unspecified bounds");
177 return;
178
179 default:
180 gcc_unreachable ();
181 }
182
183 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
184 error ("invalid use of undefined type %<%s %E%>",
185 type_code_string, TYPE_NAME (type));
186 else
187 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
188 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
189 }
190 }
191
192 /* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
194
195 tree
196 c_type_promotes_to (tree type)
197 {
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
200
201 if (c_promoting_integer_type_p (type))
202 {
203 /* Preserve unsignedness if not really getting any wider. */
204 if (TYPE_UNSIGNED (type)
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
208 }
209
210 return type;
211 }
212
213 /* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
215
216 static tree
217 qualify_type (tree type, tree like)
218 {
219 return c_build_qualified_type (type,
220 TYPE_QUALS (type) | TYPE_QUALS (like));
221 }
222 \f
223 /* Return the composite type of two compatible types.
224
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
228
229 tree
230 composite_type (tree t1, tree t2)
231 {
232 enum tree_code code1;
233 enum tree_code code2;
234 tree attributes;
235
236 /* Save time if the two types are the same. */
237
238 if (t1 == t2) return t1;
239
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
245
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
248
249 /* Merge the attributes. */
250 attributes = targetm.merge_type_attributes (t1, t2);
251
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
256
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
261
262 gcc_assert (code1 == code2);
263
264 switch (code1)
265 {
266 case POINTER_TYPE:
267 /* For two pointers, do this recursively on the target type. */
268 {
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
275 }
276
277 case ARRAY_TYPE:
278 {
279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
280 int quals;
281 tree unqual_elt;
282
283 /* We should not have any type quals on arrays at all. */
284 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
285
286 /* Save space: see if the result is identical to one of the args. */
287 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
288 return build_type_attribute_variant (t1, attributes);
289 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
290 return build_type_attribute_variant (t2, attributes);
291
292 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t1, attributes);
294 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t2, attributes);
296
297 /* Merge the element types, and have a size if either arg has
298 one. We may have qualifiers on the element types. To set
299 up TYPE_MAIN_VARIANT correctly, we need to form the
300 composite of the unqualified types and add the qualifiers
301 back at the end. */
302 quals = TYPE_QUALS (strip_array_types (elt));
303 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
304 t1 = build_array_type (unqual_elt,
305 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
306 t1 = c_build_qualified_type (t1, quals);
307 return build_type_attribute_variant (t1, attributes);
308 }
309
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
313 {
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
320
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
326
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
329 {
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
333 }
334 if (TYPE_ARG_TYPES (t2) == 0)
335 {
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
339 }
340
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
346
347 len = list_length (p1);
348 newargs = 0;
349
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
352
353 n = newargs;
354
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
357 {
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
361 {
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
364 }
365 if (TREE_VALUE (p2) == 0)
366 {
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
369 }
370
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
376 {
377 tree memb;
378 tree mv2 = TREE_VALUE (p2);
379 if (mv2 && mv2 != error_mark_node
380 && TREE_CODE (mv2) != ARRAY_TYPE)
381 mv2 = TYPE_MAIN_VARIANT (mv2);
382 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
383 memb; memb = TREE_CHAIN (memb))
384 {
385 tree mv3 = TREE_TYPE (memb);
386 if (mv3 && mv3 != error_mark_node
387 && TREE_CODE (mv3) != ARRAY_TYPE)
388 mv3 = TYPE_MAIN_VARIANT (mv3);
389 if (comptypes (mv3, mv2))
390 {
391 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
392 TREE_VALUE (p2));
393 if (pedantic)
394 pedwarn ("function types not truly compatible in ISO C");
395 goto parm_done;
396 }
397 }
398 }
399 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
400 && TREE_VALUE (p2) != TREE_VALUE (p1))
401 {
402 tree memb;
403 tree mv1 = TREE_VALUE (p1);
404 if (mv1 && mv1 != error_mark_node
405 && TREE_CODE (mv1) != ARRAY_TYPE)
406 mv1 = TYPE_MAIN_VARIANT (mv1);
407 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
408 memb; memb = TREE_CHAIN (memb))
409 {
410 tree mv3 = TREE_TYPE (memb);
411 if (mv3 && mv3 != error_mark_node
412 && TREE_CODE (mv3) != ARRAY_TYPE)
413 mv3 = TYPE_MAIN_VARIANT (mv3);
414 if (comptypes (mv3, mv1))
415 {
416 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
417 TREE_VALUE (p1));
418 if (pedantic)
419 pedwarn ("function types not truly compatible in ISO C");
420 goto parm_done;
421 }
422 }
423 }
424 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
425 parm_done: ;
426 }
427
428 c_override_global_bindings_to_false = false;
429 t1 = build_function_type (valtype, newargs);
430 t1 = qualify_type (t1, t2);
431 /* ... falls through ... */
432 }
433
434 default:
435 return build_type_attribute_variant (t1, attributes);
436 }
437
438 }
439
440 /* Return the type of a conditional expression between pointers to
441 possibly differently qualified versions of compatible types.
442
443 We assume that comp_target_types has already been done and returned
444 nonzero; if that isn't so, this may crash. */
445
446 static tree
447 common_pointer_type (tree t1, tree t2)
448 {
449 tree attributes;
450 tree pointed_to_1, mv1;
451 tree pointed_to_2, mv2;
452 tree target;
453
454 /* Save time if the two types are the same. */
455
456 if (t1 == t2) return t1;
457
458 /* If one type is nonsense, use the other. */
459 if (t1 == error_mark_node)
460 return t2;
461 if (t2 == error_mark_node)
462 return t1;
463
464 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
465 && TREE_CODE (t2) == POINTER_TYPE);
466
467 /* Merge the attributes. */
468 attributes = targetm.merge_type_attributes (t1, t2);
469
470 /* Find the composite type of the target types, and combine the
471 qualifiers of the two types' targets. Do not lose qualifiers on
472 array element types by taking the TYPE_MAIN_VARIANT. */
473 mv1 = pointed_to_1 = TREE_TYPE (t1);
474 mv2 = pointed_to_2 = TREE_TYPE (t2);
475 if (TREE_CODE (mv1) != ARRAY_TYPE)
476 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
477 if (TREE_CODE (mv2) != ARRAY_TYPE)
478 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
479 target = composite_type (mv1, mv2);
480 t1 = build_pointer_type (c_build_qualified_type
481 (target,
482 TYPE_QUALS (pointed_to_1) |
483 TYPE_QUALS (pointed_to_2)));
484 return build_type_attribute_variant (t1, attributes);
485 }
486
487 /* Return the common type for two arithmetic types under the usual
488 arithmetic conversions. The default conversions have already been
489 applied, and enumerated types converted to their compatible integer
490 types. The resulting type is unqualified and has no attributes.
491
492 This is the type for the result of most arithmetic operations
493 if the operands have the given two types. */
494
495 static tree
496 c_common_type (tree t1, tree t2)
497 {
498 enum tree_code code1;
499 enum tree_code code2;
500
501 /* If one type is nonsense, use the other. */
502 if (t1 == error_mark_node)
503 return t2;
504 if (t2 == error_mark_node)
505 return t1;
506
507 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
508 t1 = TYPE_MAIN_VARIANT (t1);
509
510 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
511 t2 = TYPE_MAIN_VARIANT (t2);
512
513 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
514 t1 = build_type_attribute_variant (t1, NULL_TREE);
515
516 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
517 t2 = build_type_attribute_variant (t2, NULL_TREE);
518
519 /* Save time if the two types are the same. */
520
521 if (t1 == t2) return t1;
522
523 code1 = TREE_CODE (t1);
524 code2 = TREE_CODE (t2);
525
526 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
527 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
528 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
529 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
530
531 /* If one type is a vector type, return that type. (How the usual
532 arithmetic conversions apply to the vector types extension is not
533 precisely specified.) */
534 if (code1 == VECTOR_TYPE)
535 return t1;
536
537 if (code2 == VECTOR_TYPE)
538 return t2;
539
540 /* If one type is complex, form the common type of the non-complex
541 components, then make that complex. Use T1 or T2 if it is the
542 required type. */
543 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
544 {
545 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
546 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
547 tree subtype = c_common_type (subtype1, subtype2);
548
549 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
550 return t1;
551 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
552 return t2;
553 else
554 return build_complex_type (subtype);
555 }
556
557 /* If only one is real, use it as the result. */
558
559 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
560 return t1;
561
562 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
563 return t2;
564
565 /* Both real or both integers; use the one with greater precision. */
566
567 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
568 return t1;
569 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
570 return t2;
571
572 /* Same precision. Prefer long longs to longs to ints when the
573 same precision, following the C99 rules on integer type rank
574 (which are equivalent to the C90 rules for C90 types). */
575
576 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
577 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
578 return long_long_unsigned_type_node;
579
580 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
581 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
582 {
583 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
584 return long_long_unsigned_type_node;
585 else
586 return long_long_integer_type_node;
587 }
588
589 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
590 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
591 return long_unsigned_type_node;
592
593 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
594 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
595 {
596 /* But preserve unsignedness from the other type,
597 since long cannot hold all the values of an unsigned int. */
598 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
599 return long_unsigned_type_node;
600 else
601 return long_integer_type_node;
602 }
603
604 /* Likewise, prefer long double to double even if same size. */
605 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
606 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
607 return long_double_type_node;
608
609 /* Otherwise prefer the unsigned one. */
610
611 if (TYPE_UNSIGNED (t1))
612 return t1;
613 else
614 return t2;
615 }
616 \f
617 /* Wrapper around c_common_type that is used by c-common.c. ENUMERAL_TYPEs
618 are allowed here and are converted to their compatible integer types. */
619 tree
620 common_type (tree t1, tree t2)
621 {
622 if (TREE_CODE (t1) == ENUMERAL_TYPE)
623 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
624 if (TREE_CODE (t2) == ENUMERAL_TYPE)
625 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
626 return c_common_type (t1, t2);
627 }
628 \f
629 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
630 or various other operations. Return 2 if they are compatible
631 but a warning may be needed if you use them together. */
632
633 int
634 comptypes (tree type1, tree type2)
635 {
636 tree t1 = type1;
637 tree t2 = type2;
638 int attrval, val;
639
640 /* Suppress errors caused by previously reported errors. */
641
642 if (t1 == t2 || !t1 || !t2
643 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
644 return 1;
645
646 /* If either type is the internal version of sizetype, return the
647 language version. */
648 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
649 && TYPE_ORIG_SIZE_TYPE (t1))
650 t1 = TYPE_ORIG_SIZE_TYPE (t1);
651
652 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
653 && TYPE_ORIG_SIZE_TYPE (t2))
654 t2 = TYPE_ORIG_SIZE_TYPE (t2);
655
656
657 /* Enumerated types are compatible with integer types, but this is
658 not transitive: two enumerated types in the same translation unit
659 are compatible with each other only if they are the same type. */
660
661 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
662 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
663 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
664 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
665
666 if (t1 == t2)
667 return 1;
668
669 /* Different classes of types can't be compatible. */
670
671 if (TREE_CODE (t1) != TREE_CODE (t2))
672 return 0;
673
674 /* Qualifiers must match. C99 6.7.3p9 */
675
676 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
677 return 0;
678
679 /* Allow for two different type nodes which have essentially the same
680 definition. Note that we already checked for equality of the type
681 qualifiers (just above). */
682
683 if (TREE_CODE (t1) != ARRAY_TYPE
684 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
685 return 1;
686
687 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
688 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
689 return 0;
690
691 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
692 val = 0;
693
694 switch (TREE_CODE (t1))
695 {
696 case POINTER_TYPE:
697 /* We must give ObjC the first crack at comparing pointers, since
698 protocol qualifiers may be involved. */
699 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
700 break;
701 /* Do not remove mode or aliasing information. */
702 if (TYPE_MODE (t1) != TYPE_MODE (t2)
703 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
704 break;
705 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
706 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
707 break;
708
709 case FUNCTION_TYPE:
710 val = function_types_compatible_p (t1, t2);
711 break;
712
713 case ARRAY_TYPE:
714 {
715 tree d1 = TYPE_DOMAIN (t1);
716 tree d2 = TYPE_DOMAIN (t2);
717 bool d1_variable, d2_variable;
718 bool d1_zero, d2_zero;
719 val = 1;
720
721 /* Target types must match incl. qualifiers. */
722 if (TREE_TYPE (t1) != TREE_TYPE (t2)
723 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
724 return 0;
725
726 /* Sizes must match unless one is missing or variable. */
727 if (d1 == 0 || d2 == 0 || d1 == d2)
728 break;
729
730 d1_zero = !TYPE_MAX_VALUE (d1);
731 d2_zero = !TYPE_MAX_VALUE (d2);
732
733 d1_variable = (!d1_zero
734 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
735 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
736 d2_variable = (!d2_zero
737 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
738 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
739
740 if (d1_variable || d2_variable)
741 break;
742 if (d1_zero && d2_zero)
743 break;
744 if (d1_zero || d2_zero
745 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
746 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
747 val = 0;
748
749 break;
750 }
751
752 case RECORD_TYPE:
753 /* We are dealing with two distinct structs. In assorted Objective-C
754 corner cases, however, these can still be deemed equivalent. */
755 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
756 val = 1;
757
758 case ENUMERAL_TYPE:
759 case UNION_TYPE:
760 if (val != 1 && !same_translation_unit_p (t1, t2))
761 val = tagged_types_tu_compatible_p (t1, t2);
762 break;
763
764 case VECTOR_TYPE:
765 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
766 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
767 break;
768
769 default:
770 break;
771 }
772 return attrval == 2 && val == 1 ? 2 : val;
773 }
774
775 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
776 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
777 to 1 or 0 depending if the check of the pointer types is meant to
778 be reflexive or not (typically, assignments are not reflexive,
779 while comparisons are reflexive).
780 */
781
782 static int
783 comp_target_types (tree ttl, tree ttr, int reflexive)
784 {
785 int val;
786 tree mvl, mvr;
787
788 /* Give objc_comptypes a crack at letting these types through. */
789 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
790 return val;
791
792 /* Do not lose qualifiers on element types of array types that are
793 pointer targets by taking their TYPE_MAIN_VARIANT. */
794 mvl = TREE_TYPE (ttl);
795 mvr = TREE_TYPE (ttr);
796 if (TREE_CODE (mvl) != ARRAY_TYPE)
797 mvl = TYPE_MAIN_VARIANT (mvl);
798 if (TREE_CODE (mvr) != ARRAY_TYPE)
799 mvr = TYPE_MAIN_VARIANT (mvr);
800 val = comptypes (mvl, mvr);
801
802 if (val == 2 && pedantic)
803 pedwarn ("types are not quite compatible");
804 return val;
805 }
806 \f
807 /* Subroutines of `comptypes'. */
808
809 /* Determine whether two trees derive from the same translation unit.
810 If the CONTEXT chain ends in a null, that tree's context is still
811 being parsed, so if two trees have context chains ending in null,
812 they're in the same translation unit. */
813 int
814 same_translation_unit_p (tree t1, tree t2)
815 {
816 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
817 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
818 {
819 case tcc_declaration:
820 t1 = DECL_CONTEXT (t1); break;
821 case tcc_type:
822 t1 = TYPE_CONTEXT (t1); break;
823 case tcc_exceptional:
824 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
825 default: gcc_unreachable ();
826 }
827
828 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
829 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
830 {
831 case tcc_declaration:
832 t2 = DECL_CONTEXT (t2); break;
833 case tcc_type:
834 t2 = TYPE_CONTEXT (t2); break;
835 case tcc_exceptional:
836 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
837 default: gcc_unreachable ();
838 }
839
840 return t1 == t2;
841 }
842
843 /* The C standard says that two structures in different translation
844 units are compatible with each other only if the types of their
845 fields are compatible (among other things). So, consider two copies
846 of this structure: */
847
848 struct tagged_tu_seen {
849 const struct tagged_tu_seen * next;
850 tree t1;
851 tree t2;
852 };
853
854 /* Can they be compatible with each other? We choose to break the
855 recursion by allowing those types to be compatible. */
856
857 static const struct tagged_tu_seen * tagged_tu_seen_base;
858
859 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
860 compatible. If the two types are not the same (which has been
861 checked earlier), this can only happen when multiple translation
862 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
863 rules. */
864
865 static int
866 tagged_types_tu_compatible_p (tree t1, tree t2)
867 {
868 tree s1, s2;
869 bool needs_warning = false;
870
871 /* We have to verify that the tags of the types are the same. This
872 is harder than it looks because this may be a typedef, so we have
873 to go look at the original type. It may even be a typedef of a
874 typedef...
875 In the case of compiler-created builtin structs the TYPE_DECL
876 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
877 while (TYPE_NAME (t1)
878 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
879 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
880 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
881
882 while (TYPE_NAME (t2)
883 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
884 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
885 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
886
887 /* C90 didn't have the requirement that the two tags be the same. */
888 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
889 return 0;
890
891 /* C90 didn't say what happened if one or both of the types were
892 incomplete; we choose to follow C99 rules here, which is that they
893 are compatible. */
894 if (TYPE_SIZE (t1) == NULL
895 || TYPE_SIZE (t2) == NULL)
896 return 1;
897
898 {
899 const struct tagged_tu_seen * tts_i;
900 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
901 if (tts_i->t1 == t1 && tts_i->t2 == t2)
902 return 1;
903 }
904
905 switch (TREE_CODE (t1))
906 {
907 case ENUMERAL_TYPE:
908 {
909
910 /* Speed up the case where the type values are in the same order. */
911 tree tv1 = TYPE_VALUES (t1);
912 tree tv2 = TYPE_VALUES (t2);
913
914 if (tv1 == tv2)
915 return 1;
916
917 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
918 {
919 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
920 break;
921 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
922 return 0;
923 }
924
925 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
926 return 1;
927 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
928 return 0;
929
930 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
931 return 0;
932
933 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
934 {
935 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
936 if (s2 == NULL
937 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
938 return 0;
939 }
940 return 1;
941 }
942
943 case UNION_TYPE:
944 {
945 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
946 return 0;
947
948 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
949 {
950 bool ok = false;
951 struct tagged_tu_seen tts;
952
953 tts.next = tagged_tu_seen_base;
954 tts.t1 = t1;
955 tts.t2 = t2;
956 tagged_tu_seen_base = &tts;
957
958 if (DECL_NAME (s1) != NULL)
959 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
960 if (DECL_NAME (s1) == DECL_NAME (s2))
961 {
962 int result;
963 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
964 if (result == 0)
965 break;
966 if (result == 2)
967 needs_warning = true;
968
969 if (TREE_CODE (s1) == FIELD_DECL
970 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
971 DECL_FIELD_BIT_OFFSET (s2)) != 1)
972 break;
973
974 ok = true;
975 break;
976 }
977 tagged_tu_seen_base = tts.next;
978 if (!ok)
979 return 0;
980 }
981 return needs_warning ? 2 : 1;
982 }
983
984 case RECORD_TYPE:
985 {
986 struct tagged_tu_seen tts;
987
988 tts.next = tagged_tu_seen_base;
989 tts.t1 = t1;
990 tts.t2 = t2;
991 tagged_tu_seen_base = &tts;
992
993 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
994 s1 && s2;
995 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
996 {
997 int result;
998 if (TREE_CODE (s1) != TREE_CODE (s2)
999 || DECL_NAME (s1) != DECL_NAME (s2))
1000 break;
1001 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1002 if (result == 0)
1003 break;
1004 if (result == 2)
1005 needs_warning = true;
1006
1007 if (TREE_CODE (s1) == FIELD_DECL
1008 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1009 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1010 break;
1011 }
1012 tagged_tu_seen_base = tts.next;
1013 if (s1 && s2)
1014 return 0;
1015 return needs_warning ? 2 : 1;
1016 }
1017
1018 default:
1019 gcc_unreachable ();
1020 }
1021 }
1022
1023 /* Return 1 if two function types F1 and F2 are compatible.
1024 If either type specifies no argument types,
1025 the other must specify a fixed number of self-promoting arg types.
1026 Otherwise, if one type specifies only the number of arguments,
1027 the other must specify that number of self-promoting arg types.
1028 Otherwise, the argument types must match. */
1029
1030 static int
1031 function_types_compatible_p (tree f1, tree f2)
1032 {
1033 tree args1, args2;
1034 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1035 int val = 1;
1036 int val1;
1037 tree ret1, ret2;
1038
1039 ret1 = TREE_TYPE (f1);
1040 ret2 = TREE_TYPE (f2);
1041
1042 /* 'volatile' qualifiers on a function's return type used to mean
1043 the function is noreturn. */
1044 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1045 pedwarn ("function return types not compatible due to %<volatile%>");
1046 if (TYPE_VOLATILE (ret1))
1047 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1048 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1049 if (TYPE_VOLATILE (ret2))
1050 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1051 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1052 val = comptypes (ret1, ret2);
1053 if (val == 0)
1054 return 0;
1055
1056 args1 = TYPE_ARG_TYPES (f1);
1057 args2 = TYPE_ARG_TYPES (f2);
1058
1059 /* An unspecified parmlist matches any specified parmlist
1060 whose argument types don't need default promotions. */
1061
1062 if (args1 == 0)
1063 {
1064 if (!self_promoting_args_p (args2))
1065 return 0;
1066 /* If one of these types comes from a non-prototype fn definition,
1067 compare that with the other type's arglist.
1068 If they don't match, ask for a warning (but no error). */
1069 if (TYPE_ACTUAL_ARG_TYPES (f1)
1070 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1071 val = 2;
1072 return val;
1073 }
1074 if (args2 == 0)
1075 {
1076 if (!self_promoting_args_p (args1))
1077 return 0;
1078 if (TYPE_ACTUAL_ARG_TYPES (f2)
1079 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1080 val = 2;
1081 return val;
1082 }
1083
1084 /* Both types have argument lists: compare them and propagate results. */
1085 val1 = type_lists_compatible_p (args1, args2);
1086 return val1 != 1 ? val1 : val;
1087 }
1088
1089 /* Check two lists of types for compatibility,
1090 returning 0 for incompatible, 1 for compatible,
1091 or 2 for compatible with warning. */
1092
1093 static int
1094 type_lists_compatible_p (tree args1, tree args2)
1095 {
1096 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1097 int val = 1;
1098 int newval = 0;
1099
1100 while (1)
1101 {
1102 tree a1, mv1, a2, mv2;
1103 if (args1 == 0 && args2 == 0)
1104 return val;
1105 /* If one list is shorter than the other,
1106 they fail to match. */
1107 if (args1 == 0 || args2 == 0)
1108 return 0;
1109 mv1 = a1 = TREE_VALUE (args1);
1110 mv2 = a2 = TREE_VALUE (args2);
1111 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1112 mv1 = TYPE_MAIN_VARIANT (mv1);
1113 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1114 mv2 = TYPE_MAIN_VARIANT (mv2);
1115 /* A null pointer instead of a type
1116 means there is supposed to be an argument
1117 but nothing is specified about what type it has.
1118 So match anything that self-promotes. */
1119 if (a1 == 0)
1120 {
1121 if (c_type_promotes_to (a2) != a2)
1122 return 0;
1123 }
1124 else if (a2 == 0)
1125 {
1126 if (c_type_promotes_to (a1) != a1)
1127 return 0;
1128 }
1129 /* If one of the lists has an error marker, ignore this arg. */
1130 else if (TREE_CODE (a1) == ERROR_MARK
1131 || TREE_CODE (a2) == ERROR_MARK)
1132 ;
1133 else if (!(newval = comptypes (mv1, mv2)))
1134 {
1135 /* Allow wait (union {union wait *u; int *i} *)
1136 and wait (union wait *) to be compatible. */
1137 if (TREE_CODE (a1) == UNION_TYPE
1138 && (TYPE_NAME (a1) == 0
1139 || TYPE_TRANSPARENT_UNION (a1))
1140 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1141 && tree_int_cst_equal (TYPE_SIZE (a1),
1142 TYPE_SIZE (a2)))
1143 {
1144 tree memb;
1145 for (memb = TYPE_FIELDS (a1);
1146 memb; memb = TREE_CHAIN (memb))
1147 {
1148 tree mv3 = TREE_TYPE (memb);
1149 if (mv3 && mv3 != error_mark_node
1150 && TREE_CODE (mv3) != ARRAY_TYPE)
1151 mv3 = TYPE_MAIN_VARIANT (mv3);
1152 if (comptypes (mv3, mv2))
1153 break;
1154 }
1155 if (memb == 0)
1156 return 0;
1157 }
1158 else if (TREE_CODE (a2) == UNION_TYPE
1159 && (TYPE_NAME (a2) == 0
1160 || TYPE_TRANSPARENT_UNION (a2))
1161 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1162 && tree_int_cst_equal (TYPE_SIZE (a2),
1163 TYPE_SIZE (a1)))
1164 {
1165 tree memb;
1166 for (memb = TYPE_FIELDS (a2);
1167 memb; memb = TREE_CHAIN (memb))
1168 {
1169 tree mv3 = TREE_TYPE (memb);
1170 if (mv3 && mv3 != error_mark_node
1171 && TREE_CODE (mv3) != ARRAY_TYPE)
1172 mv3 = TYPE_MAIN_VARIANT (mv3);
1173 if (comptypes (mv3, mv1))
1174 break;
1175 }
1176 if (memb == 0)
1177 return 0;
1178 }
1179 else
1180 return 0;
1181 }
1182
1183 /* comptypes said ok, but record if it said to warn. */
1184 if (newval > val)
1185 val = newval;
1186
1187 args1 = TREE_CHAIN (args1);
1188 args2 = TREE_CHAIN (args2);
1189 }
1190 }
1191 \f
1192 /* Compute the size to increment a pointer by. */
1193
1194 static tree
1195 c_size_in_bytes (tree type)
1196 {
1197 enum tree_code code = TREE_CODE (type);
1198
1199 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1200 return size_one_node;
1201
1202 if (!COMPLETE_OR_VOID_TYPE_P (type))
1203 {
1204 error ("arithmetic on pointer to an incomplete type");
1205 return size_one_node;
1206 }
1207
1208 /* Convert in case a char is more than one unit. */
1209 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1210 size_int (TYPE_PRECISION (char_type_node)
1211 / BITS_PER_UNIT));
1212 }
1213 \f
1214 /* Return either DECL or its known constant value (if it has one). */
1215
1216 tree
1217 decl_constant_value (tree decl)
1218 {
1219 if (/* Don't change a variable array bound or initial value to a constant
1220 in a place where a variable is invalid. Note that DECL_INITIAL
1221 isn't valid for a PARM_DECL. */
1222 current_function_decl != 0
1223 && TREE_CODE (decl) != PARM_DECL
1224 && !TREE_THIS_VOLATILE (decl)
1225 && TREE_READONLY (decl)
1226 && DECL_INITIAL (decl) != 0
1227 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1228 /* This is invalid if initial value is not constant.
1229 If it has either a function call, a memory reference,
1230 or a variable, then re-evaluating it could give different results. */
1231 && TREE_CONSTANT (DECL_INITIAL (decl))
1232 /* Check for cases where this is sub-optimal, even though valid. */
1233 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1234 return DECL_INITIAL (decl);
1235 return decl;
1236 }
1237
1238 /* Return either DECL or its known constant value (if it has one), but
1239 return DECL if pedantic or DECL has mode BLKmode. This is for
1240 bug-compatibility with the old behavior of decl_constant_value
1241 (before GCC 3.0); every use of this function is a bug and it should
1242 be removed before GCC 3.1. It is not appropriate to use pedantic
1243 in a way that affects optimization, and BLKmode is probably not the
1244 right test for avoiding misoptimizations either. */
1245
1246 static tree
1247 decl_constant_value_for_broken_optimization (tree decl)
1248 {
1249 if (pedantic || DECL_MODE (decl) == BLKmode)
1250 return decl;
1251 else
1252 return decl_constant_value (decl);
1253 }
1254
1255
1256 /* Perform the default conversion of arrays and functions to pointers.
1257 Return the result of converting EXP. For any other expression, just
1258 return EXP. */
1259
1260 static tree
1261 default_function_array_conversion (tree exp)
1262 {
1263 tree orig_exp;
1264 tree type = TREE_TYPE (exp);
1265 enum tree_code code = TREE_CODE (type);
1266 int not_lvalue = 0;
1267
1268 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1269 an lvalue.
1270
1271 Do not use STRIP_NOPS here! It will remove conversions from pointer
1272 to integer and cause infinite recursion. */
1273 orig_exp = exp;
1274 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1275 || (TREE_CODE (exp) == NOP_EXPR
1276 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1277 {
1278 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1279 not_lvalue = 1;
1280 exp = TREE_OPERAND (exp, 0);
1281 }
1282
1283 if (TREE_NO_WARNING (orig_exp))
1284 TREE_NO_WARNING (exp) = 1;
1285
1286 if (code == FUNCTION_TYPE)
1287 {
1288 return build_unary_op (ADDR_EXPR, exp, 0);
1289 }
1290 if (code == ARRAY_TYPE)
1291 {
1292 tree adr;
1293 tree restype = TREE_TYPE (type);
1294 tree ptrtype;
1295 int constp = 0;
1296 int volatilep = 0;
1297 int lvalue_array_p;
1298
1299 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1300 {
1301 constp = TREE_READONLY (exp);
1302 volatilep = TREE_THIS_VOLATILE (exp);
1303 }
1304
1305 if (TYPE_QUALS (type) || constp || volatilep)
1306 restype
1307 = c_build_qualified_type (restype,
1308 TYPE_QUALS (type)
1309 | (constp * TYPE_QUAL_CONST)
1310 | (volatilep * TYPE_QUAL_VOLATILE));
1311
1312 if (TREE_CODE (exp) == INDIRECT_REF)
1313 return convert (build_pointer_type (restype),
1314 TREE_OPERAND (exp, 0));
1315
1316 if (TREE_CODE (exp) == COMPOUND_EXPR)
1317 {
1318 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1319 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1320 TREE_OPERAND (exp, 0), op1);
1321 }
1322
1323 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1324 if (!flag_isoc99 && !lvalue_array_p)
1325 {
1326 /* Before C99, non-lvalue arrays do not decay to pointers.
1327 Normally, using such an array would be invalid; but it can
1328 be used correctly inside sizeof or as a statement expression.
1329 Thus, do not give an error here; an error will result later. */
1330 return exp;
1331 }
1332
1333 ptrtype = build_pointer_type (restype);
1334
1335 if (TREE_CODE (exp) == VAR_DECL)
1336 {
1337 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1338 ADDR_EXPR because it's the best way of representing what
1339 happens in C when we take the address of an array and place
1340 it in a pointer to the element type. */
1341 adr = build1 (ADDR_EXPR, ptrtype, exp);
1342 if (!c_mark_addressable (exp))
1343 return error_mark_node;
1344 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1345 return adr;
1346 }
1347 /* This way is better for a COMPONENT_REF since it can
1348 simplify the offset for a component. */
1349 adr = build_unary_op (ADDR_EXPR, exp, 1);
1350 return convert (ptrtype, adr);
1351 }
1352 return exp;
1353 }
1354
1355
1356 /* EXP is an expression of integer type. Apply the integer promotions
1357 to it and return the promoted value. */
1358
1359 tree
1360 perform_integral_promotions (tree exp)
1361 {
1362 tree type = TREE_TYPE (exp);
1363 enum tree_code code = TREE_CODE (type);
1364
1365 gcc_assert (INTEGRAL_TYPE_P (type));
1366
1367 /* Normally convert enums to int,
1368 but convert wide enums to something wider. */
1369 if (code == ENUMERAL_TYPE)
1370 {
1371 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1372 TYPE_PRECISION (integer_type_node)),
1373 ((TYPE_PRECISION (type)
1374 >= TYPE_PRECISION (integer_type_node))
1375 && TYPE_UNSIGNED (type)));
1376
1377 return convert (type, exp);
1378 }
1379
1380 /* ??? This should no longer be needed now bit-fields have their
1381 proper types. */
1382 if (TREE_CODE (exp) == COMPONENT_REF
1383 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1384 /* If it's thinner than an int, promote it like a
1385 c_promoting_integer_type_p, otherwise leave it alone. */
1386 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1387 TYPE_PRECISION (integer_type_node)))
1388 return convert (integer_type_node, exp);
1389
1390 if (c_promoting_integer_type_p (type))
1391 {
1392 /* Preserve unsignedness if not really getting any wider. */
1393 if (TYPE_UNSIGNED (type)
1394 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1395 return convert (unsigned_type_node, exp);
1396
1397 return convert (integer_type_node, exp);
1398 }
1399
1400 return exp;
1401 }
1402
1403
1404 /* Perform default promotions for C data used in expressions.
1405 Arrays and functions are converted to pointers;
1406 enumeral types or short or char, to int.
1407 In addition, manifest constants symbols are replaced by their values. */
1408
1409 tree
1410 default_conversion (tree exp)
1411 {
1412 tree orig_exp;
1413 tree type = TREE_TYPE (exp);
1414 enum tree_code code = TREE_CODE (type);
1415
1416 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1417 return default_function_array_conversion (exp);
1418
1419 /* Constants can be used directly unless they're not loadable. */
1420 if (TREE_CODE (exp) == CONST_DECL)
1421 exp = DECL_INITIAL (exp);
1422
1423 /* Replace a nonvolatile const static variable with its value unless
1424 it is an array, in which case we must be sure that taking the
1425 address of the array produces consistent results. */
1426 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1427 {
1428 exp = decl_constant_value_for_broken_optimization (exp);
1429 type = TREE_TYPE (exp);
1430 }
1431
1432 /* Strip no-op conversions. */
1433 orig_exp = exp;
1434 STRIP_TYPE_NOPS (exp);
1435
1436 if (TREE_NO_WARNING (orig_exp))
1437 TREE_NO_WARNING (exp) = 1;
1438
1439 if (INTEGRAL_TYPE_P (type))
1440 return perform_integral_promotions (exp);
1441
1442 if (code == VOID_TYPE)
1443 {
1444 error ("void value not ignored as it ought to be");
1445 return error_mark_node;
1446 }
1447 return exp;
1448 }
1449 \f
1450 /* Look up COMPONENT in a structure or union DECL.
1451
1452 If the component name is not found, returns NULL_TREE. Otherwise,
1453 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1454 stepping down the chain to the component, which is in the last
1455 TREE_VALUE of the list. Normally the list is of length one, but if
1456 the component is embedded within (nested) anonymous structures or
1457 unions, the list steps down the chain to the component. */
1458
1459 static tree
1460 lookup_field (tree decl, tree component)
1461 {
1462 tree type = TREE_TYPE (decl);
1463 tree field;
1464
1465 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1466 to the field elements. Use a binary search on this array to quickly
1467 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1468 will always be set for structures which have many elements. */
1469
1470 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1471 {
1472 int bot, top, half;
1473 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1474
1475 field = TYPE_FIELDS (type);
1476 bot = 0;
1477 top = TYPE_LANG_SPECIFIC (type)->s->len;
1478 while (top - bot > 1)
1479 {
1480 half = (top - bot + 1) >> 1;
1481 field = field_array[bot+half];
1482
1483 if (DECL_NAME (field) == NULL_TREE)
1484 {
1485 /* Step through all anon unions in linear fashion. */
1486 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1487 {
1488 field = field_array[bot++];
1489 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1490 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1491 {
1492 tree anon = lookup_field (field, component);
1493
1494 if (anon)
1495 return tree_cons (NULL_TREE, field, anon);
1496 }
1497 }
1498
1499 /* Entire record is only anon unions. */
1500 if (bot > top)
1501 return NULL_TREE;
1502
1503 /* Restart the binary search, with new lower bound. */
1504 continue;
1505 }
1506
1507 if (DECL_NAME (field) == component)
1508 break;
1509 if (DECL_NAME (field) < component)
1510 bot += half;
1511 else
1512 top = bot + half;
1513 }
1514
1515 if (DECL_NAME (field_array[bot]) == component)
1516 field = field_array[bot];
1517 else if (DECL_NAME (field) != component)
1518 return NULL_TREE;
1519 }
1520 else
1521 {
1522 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1523 {
1524 if (DECL_NAME (field) == NULL_TREE
1525 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1526 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1527 {
1528 tree anon = lookup_field (field, component);
1529
1530 if (anon)
1531 return tree_cons (NULL_TREE, field, anon);
1532 }
1533
1534 if (DECL_NAME (field) == component)
1535 break;
1536 }
1537
1538 if (field == NULL_TREE)
1539 return NULL_TREE;
1540 }
1541
1542 return tree_cons (NULL_TREE, field, NULL_TREE);
1543 }
1544
1545 /* Make an expression to refer to the COMPONENT field of
1546 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1547
1548 tree
1549 build_component_ref (tree datum, tree component)
1550 {
1551 tree type = TREE_TYPE (datum);
1552 enum tree_code code = TREE_CODE (type);
1553 tree field = NULL;
1554 tree ref;
1555
1556 if (!objc_is_public (datum, component))
1557 return error_mark_node;
1558
1559 /* See if there is a field or component with name COMPONENT. */
1560
1561 if (code == RECORD_TYPE || code == UNION_TYPE)
1562 {
1563 if (!COMPLETE_TYPE_P (type))
1564 {
1565 c_incomplete_type_error (NULL_TREE, type);
1566 return error_mark_node;
1567 }
1568
1569 field = lookup_field (datum, component);
1570
1571 if (!field)
1572 {
1573 error ("%qT has no member named %qE", type, component);
1574 return error_mark_node;
1575 }
1576
1577 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1578 This might be better solved in future the way the C++ front
1579 end does it - by giving the anonymous entities each a
1580 separate name and type, and then have build_component_ref
1581 recursively call itself. We can't do that here. */
1582 do
1583 {
1584 tree subdatum = TREE_VALUE (field);
1585
1586 if (TREE_TYPE (subdatum) == error_mark_node)
1587 return error_mark_node;
1588
1589 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1590 NULL_TREE);
1591 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1592 TREE_READONLY (ref) = 1;
1593 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1594 TREE_THIS_VOLATILE (ref) = 1;
1595
1596 if (TREE_DEPRECATED (subdatum))
1597 warn_deprecated_use (subdatum);
1598
1599 datum = ref;
1600
1601 field = TREE_CHAIN (field);
1602 }
1603 while (field);
1604
1605 return ref;
1606 }
1607 else if (code != ERROR_MARK)
1608 error ("request for member %qE in something not a structure or union",
1609 component);
1610
1611 return error_mark_node;
1612 }
1613 \f
1614 /* Given an expression PTR for a pointer, return an expression
1615 for the value pointed to.
1616 ERRORSTRING is the name of the operator to appear in error messages. */
1617
1618 tree
1619 build_indirect_ref (tree ptr, const char *errorstring)
1620 {
1621 tree pointer = default_conversion (ptr);
1622 tree type = TREE_TYPE (pointer);
1623
1624 if (TREE_CODE (type) == POINTER_TYPE)
1625 {
1626 if (TREE_CODE (pointer) == ADDR_EXPR
1627 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1628 == TREE_TYPE (type)))
1629 return TREE_OPERAND (pointer, 0);
1630 else
1631 {
1632 tree t = TREE_TYPE (type);
1633 tree mvt = t;
1634 tree ref;
1635
1636 if (TREE_CODE (mvt) != ARRAY_TYPE)
1637 mvt = TYPE_MAIN_VARIANT (mvt);
1638 ref = build1 (INDIRECT_REF, mvt, pointer);
1639
1640 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1641 {
1642 error ("dereferencing pointer to incomplete type");
1643 return error_mark_node;
1644 }
1645 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1646 warning ("dereferencing %<void *%> pointer");
1647
1648 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1649 so that we get the proper error message if the result is used
1650 to assign to. Also, &* is supposed to be a no-op.
1651 And ANSI C seems to specify that the type of the result
1652 should be the const type. */
1653 /* A de-reference of a pointer to const is not a const. It is valid
1654 to change it via some other pointer. */
1655 TREE_READONLY (ref) = TYPE_READONLY (t);
1656 TREE_SIDE_EFFECTS (ref)
1657 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1658 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1659 return ref;
1660 }
1661 }
1662 else if (TREE_CODE (pointer) != ERROR_MARK)
1663 error ("invalid type argument of %qs", errorstring);
1664 return error_mark_node;
1665 }
1666
1667 /* This handles expressions of the form "a[i]", which denotes
1668 an array reference.
1669
1670 This is logically equivalent in C to *(a+i), but we may do it differently.
1671 If A is a variable or a member, we generate a primitive ARRAY_REF.
1672 This avoids forcing the array out of registers, and can work on
1673 arrays that are not lvalues (for example, members of structures returned
1674 by functions). */
1675
1676 tree
1677 build_array_ref (tree array, tree index)
1678 {
1679 bool swapped = false;
1680 if (TREE_TYPE (array) == error_mark_node
1681 || TREE_TYPE (index) == error_mark_node)
1682 return error_mark_node;
1683
1684 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1685 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1686 {
1687 tree temp;
1688 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1689 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1690 {
1691 error ("subscripted value is neither array nor pointer");
1692 return error_mark_node;
1693 }
1694 temp = array;
1695 array = index;
1696 index = temp;
1697 swapped = true;
1698 }
1699
1700 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1701 {
1702 error ("array subscript is not an integer");
1703 return error_mark_node;
1704 }
1705
1706 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1707 {
1708 error ("subscripted value is pointer to function");
1709 return error_mark_node;
1710 }
1711
1712 /* Subscripting with type char is likely to lose on a machine where
1713 chars are signed. So warn on any machine, but optionally. Don't
1714 warn for unsigned char since that type is safe. Don't warn for
1715 signed char because anyone who uses that must have done so
1716 deliberately. ??? Existing practice has also been to warn only
1717 when the char index is syntactically the index, not for
1718 char[array]. */
1719 if (warn_char_subscripts && !swapped
1720 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1721 warning ("array subscript has type %<char%>");
1722
1723 /* Apply default promotions *after* noticing character types. */
1724 index = default_conversion (index);
1725
1726 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1727
1728 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1729 {
1730 tree rval, type;
1731
1732 /* An array that is indexed by a non-constant
1733 cannot be stored in a register; we must be able to do
1734 address arithmetic on its address.
1735 Likewise an array of elements of variable size. */
1736 if (TREE_CODE (index) != INTEGER_CST
1737 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1738 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1739 {
1740 if (!c_mark_addressable (array))
1741 return error_mark_node;
1742 }
1743 /* An array that is indexed by a constant value which is not within
1744 the array bounds cannot be stored in a register either; because we
1745 would get a crash in store_bit_field/extract_bit_field when trying
1746 to access a non-existent part of the register. */
1747 if (TREE_CODE (index) == INTEGER_CST
1748 && TYPE_DOMAIN (TREE_TYPE (array))
1749 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1750 {
1751 if (!c_mark_addressable (array))
1752 return error_mark_node;
1753 }
1754
1755 if (pedantic)
1756 {
1757 tree foo = array;
1758 while (TREE_CODE (foo) == COMPONENT_REF)
1759 foo = TREE_OPERAND (foo, 0);
1760 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1761 pedwarn ("ISO C forbids subscripting %<register%> array");
1762 else if (!flag_isoc99 && !lvalue_p (foo))
1763 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1764 }
1765
1766 type = TREE_TYPE (TREE_TYPE (array));
1767 if (TREE_CODE (type) != ARRAY_TYPE)
1768 type = TYPE_MAIN_VARIANT (type);
1769 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1770 /* Array ref is const/volatile if the array elements are
1771 or if the array is. */
1772 TREE_READONLY (rval)
1773 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1774 | TREE_READONLY (array));
1775 TREE_SIDE_EFFECTS (rval)
1776 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1777 | TREE_SIDE_EFFECTS (array));
1778 TREE_THIS_VOLATILE (rval)
1779 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1780 /* This was added by rms on 16 Nov 91.
1781 It fixes vol struct foo *a; a->elts[1]
1782 in an inline function.
1783 Hope it doesn't break something else. */
1784 | TREE_THIS_VOLATILE (array));
1785 return require_complete_type (fold (rval));
1786 }
1787 else
1788 {
1789 tree ar = default_conversion (array);
1790
1791 if (ar == error_mark_node)
1792 return ar;
1793
1794 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1795 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1796
1797 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1798 "array indexing");
1799 }
1800 }
1801 \f
1802 /* Build an external reference to identifier ID. FUN indicates
1803 whether this will be used for a function call. LOC is the source
1804 location of the identifier. */
1805 tree
1806 build_external_ref (tree id, int fun, location_t loc)
1807 {
1808 tree ref;
1809 tree decl = lookup_name (id);
1810
1811 /* In Objective-C, an instance variable (ivar) may be preferred to
1812 whatever lookup_name() found. */
1813 decl = objc_lookup_ivar (decl, id);
1814
1815 if (decl && decl != error_mark_node)
1816 ref = decl;
1817 else if (fun)
1818 /* Implicit function declaration. */
1819 ref = implicitly_declare (id);
1820 else if (decl == error_mark_node)
1821 /* Don't complain about something that's already been
1822 complained about. */
1823 return error_mark_node;
1824 else
1825 {
1826 undeclared_variable (id, loc);
1827 return error_mark_node;
1828 }
1829
1830 if (TREE_TYPE (ref) == error_mark_node)
1831 return error_mark_node;
1832
1833 if (TREE_DEPRECATED (ref))
1834 warn_deprecated_use (ref);
1835
1836 if (!skip_evaluation)
1837 assemble_external (ref);
1838 TREE_USED (ref) = 1;
1839
1840 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1841 {
1842 if (!in_sizeof && !in_typeof)
1843 C_DECL_USED (ref) = 1;
1844 else if (DECL_INITIAL (ref) == 0
1845 && DECL_EXTERNAL (ref)
1846 && !TREE_PUBLIC (ref))
1847 record_maybe_used_decl (ref);
1848 }
1849
1850 if (TREE_CODE (ref) == CONST_DECL)
1851 {
1852 ref = DECL_INITIAL (ref);
1853 TREE_CONSTANT (ref) = 1;
1854 TREE_INVARIANT (ref) = 1;
1855 }
1856 else if (current_function_decl != 0
1857 && !DECL_FILE_SCOPE_P (current_function_decl)
1858 && (TREE_CODE (ref) == VAR_DECL
1859 || TREE_CODE (ref) == PARM_DECL
1860 || TREE_CODE (ref) == FUNCTION_DECL))
1861 {
1862 tree context = decl_function_context (ref);
1863
1864 if (context != 0 && context != current_function_decl)
1865 DECL_NONLOCAL (ref) = 1;
1866 }
1867
1868 return ref;
1869 }
1870
1871 /* Record details of decls possibly used inside sizeof or typeof. */
1872 struct maybe_used_decl
1873 {
1874 /* The decl. */
1875 tree decl;
1876 /* The level seen at (in_sizeof + in_typeof). */
1877 int level;
1878 /* The next one at this level or above, or NULL. */
1879 struct maybe_used_decl *next;
1880 };
1881
1882 static struct maybe_used_decl *maybe_used_decls;
1883
1884 /* Record that DECL, an undefined static function reference seen
1885 inside sizeof or typeof, might be used if the operand of sizeof is
1886 a VLA type or the operand of typeof is a variably modified
1887 type. */
1888
1889 static void
1890 record_maybe_used_decl (tree decl)
1891 {
1892 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1893 t->decl = decl;
1894 t->level = in_sizeof + in_typeof;
1895 t->next = maybe_used_decls;
1896 maybe_used_decls = t;
1897 }
1898
1899 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1900 USED is false, just discard them. If it is true, mark them used
1901 (if no longer inside sizeof or typeof) or move them to the next
1902 level up (if still inside sizeof or typeof). */
1903
1904 void
1905 pop_maybe_used (bool used)
1906 {
1907 struct maybe_used_decl *p = maybe_used_decls;
1908 int cur_level = in_sizeof + in_typeof;
1909 while (p && p->level > cur_level)
1910 {
1911 if (used)
1912 {
1913 if (cur_level == 0)
1914 C_DECL_USED (p->decl) = 1;
1915 else
1916 p->level = cur_level;
1917 }
1918 p = p->next;
1919 }
1920 if (!used || cur_level == 0)
1921 maybe_used_decls = p;
1922 }
1923
1924 /* Return the result of sizeof applied to EXPR. */
1925
1926 struct c_expr
1927 c_expr_sizeof_expr (struct c_expr expr)
1928 {
1929 struct c_expr ret;
1930 if (expr.value == error_mark_node)
1931 {
1932 ret.value = error_mark_node;
1933 ret.original_code = ERROR_MARK;
1934 pop_maybe_used (false);
1935 }
1936 else
1937 {
1938 ret.value = c_sizeof (TREE_TYPE (expr.value));
1939 ret.original_code = ERROR_MARK;
1940 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1941 }
1942 return ret;
1943 }
1944
1945 /* Return the result of sizeof applied to T, a structure for the type
1946 name passed to sizeof (rather than the type itself). */
1947
1948 struct c_expr
1949 c_expr_sizeof_type (struct c_type_name *t)
1950 {
1951 tree type;
1952 struct c_expr ret;
1953 type = groktypename (t);
1954 ret.value = c_sizeof (type);
1955 ret.original_code = ERROR_MARK;
1956 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1957 return ret;
1958 }
1959
1960 /* Build a function call to function FUNCTION with parameters PARAMS.
1961 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1962 TREE_VALUE of each node is a parameter-expression.
1963 FUNCTION's data type may be a function type or a pointer-to-function. */
1964
1965 tree
1966 build_function_call (tree function, tree params)
1967 {
1968 tree fntype, fundecl = 0;
1969 tree coerced_params;
1970 tree name = NULL_TREE, result;
1971 tree tem;
1972
1973 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1974 STRIP_TYPE_NOPS (function);
1975
1976 /* Convert anything with function type to a pointer-to-function. */
1977 if (TREE_CODE (function) == FUNCTION_DECL)
1978 {
1979 name = DECL_NAME (function);
1980
1981 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1982 (because calling an inline function does not mean the function
1983 needs to be separately compiled). */
1984 fntype = build_type_variant (TREE_TYPE (function),
1985 TREE_READONLY (function),
1986 TREE_THIS_VOLATILE (function));
1987 fundecl = function;
1988 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1989 }
1990 else
1991 function = default_conversion (function);
1992
1993 fntype = TREE_TYPE (function);
1994
1995 if (TREE_CODE (fntype) == ERROR_MARK)
1996 return error_mark_node;
1997
1998 if (!(TREE_CODE (fntype) == POINTER_TYPE
1999 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2000 {
2001 error ("called object %qE is not a function", function);
2002 return error_mark_node;
2003 }
2004
2005 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2006 current_function_returns_abnormally = 1;
2007
2008 /* fntype now gets the type of function pointed to. */
2009 fntype = TREE_TYPE (fntype);
2010
2011 /* Check that the function is called through a compatible prototype.
2012 If it is not, replace the call by a trap, wrapped up in a compound
2013 expression if necessary. This has the nice side-effect to prevent
2014 the tree-inliner from generating invalid assignment trees which may
2015 blow up in the RTL expander later.
2016
2017 ??? This doesn't work for Objective-C because objc_comptypes
2018 refuses to compare function prototypes, yet the compiler appears
2019 to build calls that are flagged as invalid by C's comptypes. */
2020 if (!c_dialect_objc ()
2021 && TREE_CODE (function) == NOP_EXPR
2022 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2023 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2024 && !comptypes (fntype, TREE_TYPE (tem)))
2025 {
2026 tree return_type = TREE_TYPE (fntype);
2027 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2028 NULL_TREE);
2029
2030 /* This situation leads to run-time undefined behavior. We can't,
2031 therefore, simply error unless we can prove that all possible
2032 executions of the program must execute the code. */
2033 warning ("function called through a non-compatible type");
2034
2035 /* We can, however, treat "undefined" any way we please.
2036 Call abort to encourage the user to fix the program. */
2037 inform ("if this code is reached, the program will abort");
2038
2039 if (VOID_TYPE_P (return_type))
2040 return trap;
2041 else
2042 {
2043 tree rhs;
2044
2045 if (AGGREGATE_TYPE_P (return_type))
2046 rhs = build_compound_literal (return_type,
2047 build_constructor (return_type,
2048 NULL_TREE));
2049 else
2050 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2051
2052 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2053 }
2054 }
2055
2056 /* Convert the parameters to the types declared in the
2057 function prototype, or apply default promotions. */
2058
2059 coerced_params
2060 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2061
2062 if (coerced_params == error_mark_node)
2063 return error_mark_node;
2064
2065 /* Check that the arguments to the function are valid. */
2066
2067 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2068
2069 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2070 function, coerced_params, NULL_TREE);
2071 TREE_SIDE_EFFECTS (result) = 1;
2072
2073 if (require_constant_value)
2074 {
2075 result = fold_initializer (result);
2076
2077 if (TREE_CONSTANT (result)
2078 && (name == NULL_TREE
2079 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2080 pedwarn_init ("initializer element is not constant");
2081 }
2082 else
2083 result = fold (result);
2084
2085 if (VOID_TYPE_P (TREE_TYPE (result)))
2086 return result;
2087 return require_complete_type (result);
2088 }
2089 \f
2090 /* Convert the argument expressions in the list VALUES
2091 to the types in the list TYPELIST. The result is a list of converted
2092 argument expressions, unless there are too few arguments in which
2093 case it is error_mark_node.
2094
2095 If TYPELIST is exhausted, or when an element has NULL as its type,
2096 perform the default conversions.
2097
2098 PARMLIST is the chain of parm decls for the function being called.
2099 It may be 0, if that info is not available.
2100 It is used only for generating error messages.
2101
2102 FUNCTION is a tree for the called function. It is used only for
2103 error messages, where it is formatted with %qE.
2104
2105 This is also where warnings about wrong number of args are generated.
2106
2107 Both VALUES and the returned value are chains of TREE_LIST nodes
2108 with the elements of the list in the TREE_VALUE slots of those nodes. */
2109
2110 static tree
2111 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2112 {
2113 tree typetail, valtail;
2114 tree result = NULL;
2115 int parmnum;
2116 tree selector;
2117
2118 /* Change pointer to function to the function itself for
2119 diagnostics. */
2120 if (TREE_CODE (function) == ADDR_EXPR
2121 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2122 function = TREE_OPERAND (function, 0);
2123
2124 /* Handle an ObjC selector specially for diagnostics. */
2125 selector = objc_message_selector ();
2126
2127 /* Scan the given expressions and types, producing individual
2128 converted arguments and pushing them on RESULT in reverse order. */
2129
2130 for (valtail = values, typetail = typelist, parmnum = 0;
2131 valtail;
2132 valtail = TREE_CHAIN (valtail), parmnum++)
2133 {
2134 tree type = typetail ? TREE_VALUE (typetail) : 0;
2135 tree val = TREE_VALUE (valtail);
2136 tree rname = function;
2137 int argnum = parmnum + 1;
2138 const char *invalid_func_diag;
2139
2140 if (type == void_type_node)
2141 {
2142 error ("too many arguments to function %qE", function);
2143 break;
2144 }
2145
2146 if (selector && argnum > 2)
2147 {
2148 rname = selector;
2149 argnum -= 2;
2150 }
2151
2152 STRIP_TYPE_NOPS (val);
2153
2154 val = default_function_array_conversion (val);
2155
2156 val = require_complete_type (val);
2157
2158 if (type != 0)
2159 {
2160 /* Formal parm type is specified by a function prototype. */
2161 tree parmval;
2162
2163 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2164 {
2165 error ("type of formal parameter %d is incomplete", parmnum + 1);
2166 parmval = val;
2167 }
2168 else
2169 {
2170 /* Optionally warn about conversions that
2171 differ from the default conversions. */
2172 if (warn_conversion || warn_traditional)
2173 {
2174 unsigned int formal_prec = TYPE_PRECISION (type);
2175
2176 if (INTEGRAL_TYPE_P (type)
2177 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2178 warning ("passing argument %d of %qE as integer "
2179 "rather than floating due to prototype",
2180 argnum, rname);
2181 if (INTEGRAL_TYPE_P (type)
2182 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2183 warning ("passing argument %d of %qE as integer "
2184 "rather than complex due to prototype",
2185 argnum, rname);
2186 else if (TREE_CODE (type) == COMPLEX_TYPE
2187 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2188 warning ("passing argument %d of %qE as complex "
2189 "rather than floating due to prototype",
2190 argnum, rname);
2191 else if (TREE_CODE (type) == REAL_TYPE
2192 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2193 warning ("passing argument %d of %qE as floating "
2194 "rather than integer due to prototype",
2195 argnum, rname);
2196 else if (TREE_CODE (type) == COMPLEX_TYPE
2197 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2198 warning ("passing argument %d of %qE as complex "
2199 "rather than integer due to prototype",
2200 argnum, rname);
2201 else if (TREE_CODE (type) == REAL_TYPE
2202 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2203 warning ("passing argument %d of %qE as floating "
2204 "rather than complex due to prototype",
2205 argnum, rname);
2206 /* ??? At some point, messages should be written about
2207 conversions between complex types, but that's too messy
2208 to do now. */
2209 else if (TREE_CODE (type) == REAL_TYPE
2210 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2211 {
2212 /* Warn if any argument is passed as `float',
2213 since without a prototype it would be `double'. */
2214 if (formal_prec == TYPE_PRECISION (float_type_node))
2215 warning ("passing argument %d of %qE as %<float%> "
2216 "rather than %<double%> due to prototype",
2217 argnum, rname);
2218 }
2219 /* Detect integer changing in width or signedness.
2220 These warnings are only activated with
2221 -Wconversion, not with -Wtraditional. */
2222 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2223 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2224 {
2225 tree would_have_been = default_conversion (val);
2226 tree type1 = TREE_TYPE (would_have_been);
2227
2228 if (TREE_CODE (type) == ENUMERAL_TYPE
2229 && (TYPE_MAIN_VARIANT (type)
2230 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2231 /* No warning if function asks for enum
2232 and the actual arg is that enum type. */
2233 ;
2234 else if (formal_prec != TYPE_PRECISION (type1))
2235 warning ("passing argument %d of %qE with different "
2236 "width due to prototype", argnum, rname);
2237 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2238 ;
2239 /* Don't complain if the formal parameter type
2240 is an enum, because we can't tell now whether
2241 the value was an enum--even the same enum. */
2242 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2243 ;
2244 else if (TREE_CODE (val) == INTEGER_CST
2245 && int_fits_type_p (val, type))
2246 /* Change in signedness doesn't matter
2247 if a constant value is unaffected. */
2248 ;
2249 /* If the value is extended from a narrower
2250 unsigned type, it doesn't matter whether we
2251 pass it as signed or unsigned; the value
2252 certainly is the same either way. */
2253 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2254 && TYPE_UNSIGNED (TREE_TYPE (val)))
2255 ;
2256 else if (TYPE_UNSIGNED (type))
2257 warning ("passing argument %d of %qE as unsigned "
2258 "due to prototype", argnum, rname);
2259 else
2260 warning ("passing argument %d of %qE as signed "
2261 "due to prototype", argnum, rname);
2262 }
2263 }
2264
2265 parmval = convert_for_assignment (type, val, ic_argpass,
2266 fundecl, function,
2267 parmnum + 1);
2268
2269 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2270 && INTEGRAL_TYPE_P (type)
2271 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2272 parmval = default_conversion (parmval);
2273 }
2274 result = tree_cons (NULL_TREE, parmval, result);
2275 }
2276 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2277 && (TYPE_PRECISION (TREE_TYPE (val))
2278 < TYPE_PRECISION (double_type_node)))
2279 /* Convert `float' to `double'. */
2280 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2281 else if ((invalid_func_diag =
2282 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2283 {
2284 error (invalid_func_diag);
2285 return error_mark_node;
2286 }
2287 else
2288 /* Convert `short' and `char' to full-size `int'. */
2289 result = tree_cons (NULL_TREE, default_conversion (val), result);
2290
2291 if (typetail)
2292 typetail = TREE_CHAIN (typetail);
2293 }
2294
2295 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2296 {
2297 error ("too few arguments to function %qE", function);
2298 return error_mark_node;
2299 }
2300
2301 return nreverse (result);
2302 }
2303 \f
2304 /* This is the entry point used by the parser
2305 for binary operators in the input.
2306 In addition to constructing the expression,
2307 we check for operands that were written with other binary operators
2308 in a way that is likely to confuse the user. */
2309
2310 struct c_expr
2311 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2312 struct c_expr arg2)
2313 {
2314 struct c_expr result;
2315
2316 enum tree_code code1 = arg1.original_code;
2317 enum tree_code code2 = arg2.original_code;
2318
2319 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2320 result.original_code = code;
2321
2322 if (TREE_CODE (result.value) == ERROR_MARK)
2323 return result;
2324
2325 /* Check for cases such as x+y<<z which users are likely
2326 to misinterpret. */
2327 if (warn_parentheses)
2328 {
2329 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2330 {
2331 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2332 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2333 warning ("suggest parentheses around + or - inside shift");
2334 }
2335
2336 if (code == TRUTH_ORIF_EXPR)
2337 {
2338 if (code1 == TRUTH_ANDIF_EXPR
2339 || code2 == TRUTH_ANDIF_EXPR)
2340 warning ("suggest parentheses around && within ||");
2341 }
2342
2343 if (code == BIT_IOR_EXPR)
2344 {
2345 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2346 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2347 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2348 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2349 warning ("suggest parentheses around arithmetic in operand of |");
2350 /* Check cases like x|y==z */
2351 if (TREE_CODE_CLASS (code1) == tcc_comparison
2352 || TREE_CODE_CLASS (code2) == tcc_comparison)
2353 warning ("suggest parentheses around comparison in operand of |");
2354 }
2355
2356 if (code == BIT_XOR_EXPR)
2357 {
2358 if (code1 == BIT_AND_EXPR
2359 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2360 || code2 == BIT_AND_EXPR
2361 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2362 warning ("suggest parentheses around arithmetic in operand of ^");
2363 /* Check cases like x^y==z */
2364 if (TREE_CODE_CLASS (code1) == tcc_comparison
2365 || TREE_CODE_CLASS (code2) == tcc_comparison)
2366 warning ("suggest parentheses around comparison in operand of ^");
2367 }
2368
2369 if (code == BIT_AND_EXPR)
2370 {
2371 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2372 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2373 warning ("suggest parentheses around + or - in operand of &");
2374 /* Check cases like x&y==z */
2375 if (TREE_CODE_CLASS (code1) == tcc_comparison
2376 || TREE_CODE_CLASS (code2) == tcc_comparison)
2377 warning ("suggest parentheses around comparison in operand of &");
2378 }
2379 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2380 if (TREE_CODE_CLASS (code) == tcc_comparison
2381 && (TREE_CODE_CLASS (code1) == tcc_comparison
2382 || TREE_CODE_CLASS (code2) == tcc_comparison))
2383 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2384
2385 }
2386
2387 unsigned_conversion_warning (result.value, arg1.value);
2388 unsigned_conversion_warning (result.value, arg2.value);
2389 overflow_warning (result.value);
2390
2391 return result;
2392 }
2393 \f
2394 /* Return a tree for the difference of pointers OP0 and OP1.
2395 The resulting tree has type int. */
2396
2397 static tree
2398 pointer_diff (tree op0, tree op1)
2399 {
2400 tree restype = ptrdiff_type_node;
2401
2402 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2403 tree con0, con1, lit0, lit1;
2404 tree orig_op1 = op1;
2405
2406 if (pedantic || warn_pointer_arith)
2407 {
2408 if (TREE_CODE (target_type) == VOID_TYPE)
2409 pedwarn ("pointer of type %<void *%> used in subtraction");
2410 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2411 pedwarn ("pointer to a function used in subtraction");
2412 }
2413
2414 /* If the conversion to ptrdiff_type does anything like widening or
2415 converting a partial to an integral mode, we get a convert_expression
2416 that is in the way to do any simplifications.
2417 (fold-const.c doesn't know that the extra bits won't be needed.
2418 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2419 different mode in place.)
2420 So first try to find a common term here 'by hand'; we want to cover
2421 at least the cases that occur in legal static initializers. */
2422 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2423 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2424
2425 if (TREE_CODE (con0) == PLUS_EXPR)
2426 {
2427 lit0 = TREE_OPERAND (con0, 1);
2428 con0 = TREE_OPERAND (con0, 0);
2429 }
2430 else
2431 lit0 = integer_zero_node;
2432
2433 if (TREE_CODE (con1) == PLUS_EXPR)
2434 {
2435 lit1 = TREE_OPERAND (con1, 1);
2436 con1 = TREE_OPERAND (con1, 0);
2437 }
2438 else
2439 lit1 = integer_zero_node;
2440
2441 if (operand_equal_p (con0, con1, 0))
2442 {
2443 op0 = lit0;
2444 op1 = lit1;
2445 }
2446
2447
2448 /* First do the subtraction as integers;
2449 then drop through to build the divide operator.
2450 Do not do default conversions on the minus operator
2451 in case restype is a short type. */
2452
2453 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2454 convert (restype, op1), 0);
2455 /* This generates an error if op1 is pointer to incomplete type. */
2456 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2457 error ("arithmetic on pointer to an incomplete type");
2458
2459 /* This generates an error if op0 is pointer to incomplete type. */
2460 op1 = c_size_in_bytes (target_type);
2461
2462 /* Divide by the size, in easiest possible way. */
2463 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2464 }
2465 \f
2466 /* Construct and perhaps optimize a tree representation
2467 for a unary operation. CODE, a tree_code, specifies the operation
2468 and XARG is the operand.
2469 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2470 the default promotions (such as from short to int).
2471 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2472 allows non-lvalues; this is only used to handle conversion of non-lvalue
2473 arrays to pointers in C99. */
2474
2475 tree
2476 build_unary_op (enum tree_code code, tree xarg, int flag)
2477 {
2478 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2479 tree arg = xarg;
2480 tree argtype = 0;
2481 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2482 tree val;
2483 int noconvert = flag;
2484
2485 if (typecode == ERROR_MARK)
2486 return error_mark_node;
2487 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2488 typecode = INTEGER_TYPE;
2489
2490 switch (code)
2491 {
2492 case CONVERT_EXPR:
2493 /* This is used for unary plus, because a CONVERT_EXPR
2494 is enough to prevent anybody from looking inside for
2495 associativity, but won't generate any code. */
2496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2497 || typecode == COMPLEX_TYPE
2498 || typecode == VECTOR_TYPE))
2499 {
2500 error ("wrong type argument to unary plus");
2501 return error_mark_node;
2502 }
2503 else if (!noconvert)
2504 arg = default_conversion (arg);
2505 arg = non_lvalue (arg);
2506 break;
2507
2508 case NEGATE_EXPR:
2509 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2510 || typecode == COMPLEX_TYPE
2511 || typecode == VECTOR_TYPE))
2512 {
2513 error ("wrong type argument to unary minus");
2514 return error_mark_node;
2515 }
2516 else if (!noconvert)
2517 arg = default_conversion (arg);
2518 break;
2519
2520 case BIT_NOT_EXPR:
2521 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2522 {
2523 if (!noconvert)
2524 arg = default_conversion (arg);
2525 }
2526 else if (typecode == COMPLEX_TYPE)
2527 {
2528 code = CONJ_EXPR;
2529 if (pedantic)
2530 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2531 if (!noconvert)
2532 arg = default_conversion (arg);
2533 }
2534 else
2535 {
2536 error ("wrong type argument to bit-complement");
2537 return error_mark_node;
2538 }
2539 break;
2540
2541 case ABS_EXPR:
2542 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2543 {
2544 error ("wrong type argument to abs");
2545 return error_mark_node;
2546 }
2547 else if (!noconvert)
2548 arg = default_conversion (arg);
2549 break;
2550
2551 case CONJ_EXPR:
2552 /* Conjugating a real value is a no-op, but allow it anyway. */
2553 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2554 || typecode == COMPLEX_TYPE))
2555 {
2556 error ("wrong type argument to conjugation");
2557 return error_mark_node;
2558 }
2559 else if (!noconvert)
2560 arg = default_conversion (arg);
2561 break;
2562
2563 case TRUTH_NOT_EXPR:
2564 if (typecode != INTEGER_TYPE
2565 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2566 && typecode != COMPLEX_TYPE
2567 /* These will convert to a pointer. */
2568 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2569 {
2570 error ("wrong type argument to unary exclamation mark");
2571 return error_mark_node;
2572 }
2573 arg = lang_hooks.truthvalue_conversion (arg);
2574 return invert_truthvalue (arg);
2575
2576 case NOP_EXPR:
2577 break;
2578
2579 case REALPART_EXPR:
2580 if (TREE_CODE (arg) == COMPLEX_CST)
2581 return TREE_REALPART (arg);
2582 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2583 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2584 else
2585 return arg;
2586
2587 case IMAGPART_EXPR:
2588 if (TREE_CODE (arg) == COMPLEX_CST)
2589 return TREE_IMAGPART (arg);
2590 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2591 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2592 else
2593 return convert (TREE_TYPE (arg), integer_zero_node);
2594
2595 case PREINCREMENT_EXPR:
2596 case POSTINCREMENT_EXPR:
2597 case PREDECREMENT_EXPR:
2598 case POSTDECREMENT_EXPR:
2599
2600 /* Increment or decrement the real part of the value,
2601 and don't change the imaginary part. */
2602 if (typecode == COMPLEX_TYPE)
2603 {
2604 tree real, imag;
2605
2606 if (pedantic)
2607 pedwarn ("ISO C does not support %<++%> and %<--%>"
2608 " on complex types");
2609
2610 arg = stabilize_reference (arg);
2611 real = build_unary_op (REALPART_EXPR, arg, 1);
2612 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2613 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2614 build_unary_op (code, real, 1), imag);
2615 }
2616
2617 /* Report invalid types. */
2618
2619 if (typecode != POINTER_TYPE
2620 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2621 {
2622 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2623 error ("wrong type argument to increment");
2624 else
2625 error ("wrong type argument to decrement");
2626
2627 return error_mark_node;
2628 }
2629
2630 {
2631 tree inc;
2632 tree result_type = TREE_TYPE (arg);
2633
2634 arg = get_unwidened (arg, 0);
2635 argtype = TREE_TYPE (arg);
2636
2637 /* Compute the increment. */
2638
2639 if (typecode == POINTER_TYPE)
2640 {
2641 /* If pointer target is an undefined struct,
2642 we just cannot know how to do the arithmetic. */
2643 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2644 {
2645 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2646 error ("increment of pointer to unknown structure");
2647 else
2648 error ("decrement of pointer to unknown structure");
2649 }
2650 else if ((pedantic || warn_pointer_arith)
2651 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2652 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2653 {
2654 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2655 pedwarn ("wrong type argument to increment");
2656 else
2657 pedwarn ("wrong type argument to decrement");
2658 }
2659
2660 inc = c_size_in_bytes (TREE_TYPE (result_type));
2661 }
2662 else
2663 inc = integer_one_node;
2664
2665 inc = convert (argtype, inc);
2666
2667 /* Complain about anything else that is not a true lvalue. */
2668 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2669 || code == POSTINCREMENT_EXPR)
2670 ? lv_increment
2671 : lv_decrement)))
2672 return error_mark_node;
2673
2674 /* Report a read-only lvalue. */
2675 if (TREE_READONLY (arg))
2676 readonly_error (arg,
2677 ((code == PREINCREMENT_EXPR
2678 || code == POSTINCREMENT_EXPR)
2679 ? lv_increment : lv_decrement));
2680
2681 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2682 val = boolean_increment (code, arg);
2683 else
2684 val = build2 (code, TREE_TYPE (arg), arg, inc);
2685 TREE_SIDE_EFFECTS (val) = 1;
2686 val = convert (result_type, val);
2687 if (TREE_CODE (val) != code)
2688 TREE_NO_WARNING (val) = 1;
2689 return val;
2690 }
2691
2692 case ADDR_EXPR:
2693 /* Note that this operation never does default_conversion. */
2694
2695 /* Let &* cancel out to simplify resulting code. */
2696 if (TREE_CODE (arg) == INDIRECT_REF)
2697 {
2698 /* Don't let this be an lvalue. */
2699 if (lvalue_p (TREE_OPERAND (arg, 0)))
2700 return non_lvalue (TREE_OPERAND (arg, 0));
2701 return TREE_OPERAND (arg, 0);
2702 }
2703
2704 /* For &x[y], return x+y */
2705 if (TREE_CODE (arg) == ARRAY_REF)
2706 {
2707 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2708 return error_mark_node;
2709 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2710 TREE_OPERAND (arg, 1), 1);
2711 }
2712
2713 /* Anything not already handled and not a true memory reference
2714 or a non-lvalue array is an error. */
2715 else if (typecode != FUNCTION_TYPE && !flag
2716 && !lvalue_or_else (arg, lv_addressof))
2717 return error_mark_node;
2718
2719 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2720 argtype = TREE_TYPE (arg);
2721
2722 /* If the lvalue is const or volatile, merge that into the type
2723 to which the address will point. Note that you can't get a
2724 restricted pointer by taking the address of something, so we
2725 only have to deal with `const' and `volatile' here. */
2726 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2727 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2728 argtype = c_build_type_variant (argtype,
2729 TREE_READONLY (arg),
2730 TREE_THIS_VOLATILE (arg));
2731
2732 if (!c_mark_addressable (arg))
2733 return error_mark_node;
2734
2735 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2736 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2737
2738 argtype = build_pointer_type (argtype);
2739
2740 /* ??? Cope with user tricks that amount to offsetof. Delete this
2741 when we have proper support for integer constant expressions. */
2742 val = get_base_address (arg);
2743 if (val && TREE_CODE (val) == INDIRECT_REF
2744 && integer_zerop (TREE_OPERAND (val, 0)))
2745 return fold_convert (argtype, fold_offsetof (arg));
2746
2747 val = build1 (ADDR_EXPR, argtype, arg);
2748
2749 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2750 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2751
2752 return val;
2753
2754 default:
2755 break;
2756 }
2757
2758 if (argtype == 0)
2759 argtype = TREE_TYPE (arg);
2760 val = build1 (code, argtype, arg);
2761 return require_constant_value ? fold_initializer (val) : fold (val);
2762 }
2763
2764 /* Return nonzero if REF is an lvalue valid for this language.
2765 Lvalues can be assigned, unless their type has TYPE_READONLY.
2766 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2767
2768 static int
2769 lvalue_p (tree ref)
2770 {
2771 enum tree_code code = TREE_CODE (ref);
2772
2773 switch (code)
2774 {
2775 case REALPART_EXPR:
2776 case IMAGPART_EXPR:
2777 case COMPONENT_REF:
2778 return lvalue_p (TREE_OPERAND (ref, 0));
2779
2780 case COMPOUND_LITERAL_EXPR:
2781 case STRING_CST:
2782 return 1;
2783
2784 case INDIRECT_REF:
2785 case ARRAY_REF:
2786 case VAR_DECL:
2787 case PARM_DECL:
2788 case RESULT_DECL:
2789 case ERROR_MARK:
2790 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2791 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2792
2793 case BIND_EXPR:
2794 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2795
2796 default:
2797 return 0;
2798 }
2799 }
2800 \f
2801 /* Give an error for storing in something that is 'const'. */
2802
2803 static void
2804 readonly_error (tree arg, enum lvalue_use use)
2805 {
2806 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2807 /* Using this macro rather than (for example) arrays of messages
2808 ensures that all the format strings are checked at compile
2809 time. */
2810 #define READONLY_MSG(A, I, D) (use == lv_assign \
2811 ? (A) \
2812 : (use == lv_increment ? (I) : (D)))
2813 if (TREE_CODE (arg) == COMPONENT_REF)
2814 {
2815 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2816 readonly_error (TREE_OPERAND (arg, 0), use);
2817 else
2818 error (READONLY_MSG (N_("assignment of read-only member %qD"),
2819 N_("increment of read-only member %qD"),
2820 N_("decrement of read-only member %qD")),
2821 TREE_OPERAND (arg, 1));
2822 }
2823 else if (TREE_CODE (arg) == VAR_DECL)
2824 error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2825 N_("increment of read-only variable %qD"),
2826 N_("decrement of read-only variable %qD")),
2827 arg);
2828 else
2829 error (READONLY_MSG (N_("assignment of read-only location"),
2830 N_("increment of read-only location"),
2831 N_("decrement of read-only location")));
2832 }
2833
2834
2835 /* Return nonzero if REF is an lvalue valid for this language;
2836 otherwise, print an error message and return zero. USE says
2837 how the lvalue is being used and so selects the error message. */
2838
2839 static int
2840 lvalue_or_else (tree ref, enum lvalue_use use)
2841 {
2842 int win = lvalue_p (ref);
2843
2844 if (!win)
2845 lvalue_error (use);
2846
2847 return win;
2848 }
2849 \f
2850 /* Mark EXP saying that we need to be able to take the
2851 address of it; it should not be allocated in a register.
2852 Returns true if successful. */
2853
2854 bool
2855 c_mark_addressable (tree exp)
2856 {
2857 tree x = exp;
2858
2859 while (1)
2860 switch (TREE_CODE (x))
2861 {
2862 case COMPONENT_REF:
2863 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2864 {
2865 error
2866 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2867 return false;
2868 }
2869
2870 /* ... fall through ... */
2871
2872 case ADDR_EXPR:
2873 case ARRAY_REF:
2874 case REALPART_EXPR:
2875 case IMAGPART_EXPR:
2876 x = TREE_OPERAND (x, 0);
2877 break;
2878
2879 case COMPOUND_LITERAL_EXPR:
2880 case CONSTRUCTOR:
2881 TREE_ADDRESSABLE (x) = 1;
2882 return true;
2883
2884 case VAR_DECL:
2885 case CONST_DECL:
2886 case PARM_DECL:
2887 case RESULT_DECL:
2888 if (C_DECL_REGISTER (x)
2889 && DECL_NONLOCAL (x))
2890 {
2891 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2892 {
2893 error
2894 ("global register variable %qD used in nested function", x);
2895 return false;
2896 }
2897 pedwarn ("register variable %qD used in nested function", x);
2898 }
2899 else if (C_DECL_REGISTER (x))
2900 {
2901 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2902 error ("address of global register variable %qD requested", x);
2903 else
2904 error ("address of register variable %qD requested", x);
2905 return false;
2906 }
2907
2908 /* drops in */
2909 case FUNCTION_DECL:
2910 TREE_ADDRESSABLE (x) = 1;
2911 /* drops out */
2912 default:
2913 return true;
2914 }
2915 }
2916 \f
2917 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2918
2919 tree
2920 build_conditional_expr (tree ifexp, tree op1, tree op2)
2921 {
2922 tree type1;
2923 tree type2;
2924 enum tree_code code1;
2925 enum tree_code code2;
2926 tree result_type = NULL;
2927 tree orig_op1 = op1, orig_op2 = op2;
2928
2929 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2930
2931 /* Promote both alternatives. */
2932
2933 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2934 op1 = default_conversion (op1);
2935 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2936 op2 = default_conversion (op2);
2937
2938 if (TREE_CODE (ifexp) == ERROR_MARK
2939 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2940 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2941 return error_mark_node;
2942
2943 type1 = TREE_TYPE (op1);
2944 code1 = TREE_CODE (type1);
2945 type2 = TREE_TYPE (op2);
2946 code2 = TREE_CODE (type2);
2947
2948 /* C90 does not permit non-lvalue arrays in conditional expressions.
2949 In C99 they will be pointers by now. */
2950 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2951 {
2952 error ("non-lvalue array in conditional expression");
2953 return error_mark_node;
2954 }
2955
2956 /* Quickly detect the usual case where op1 and op2 have the same type
2957 after promotion. */
2958 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2959 {
2960 if (type1 == type2)
2961 result_type = type1;
2962 else
2963 result_type = TYPE_MAIN_VARIANT (type1);
2964 }
2965 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2966 || code1 == COMPLEX_TYPE)
2967 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2968 || code2 == COMPLEX_TYPE))
2969 {
2970 result_type = c_common_type (type1, type2);
2971
2972 /* If -Wsign-compare, warn here if type1 and type2 have
2973 different signedness. We'll promote the signed to unsigned
2974 and later code won't know it used to be different.
2975 Do this check on the original types, so that explicit casts
2976 will be considered, but default promotions won't. */
2977 if (warn_sign_compare && !skip_evaluation)
2978 {
2979 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2980 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2981
2982 if (unsigned_op1 ^ unsigned_op2)
2983 {
2984 /* Do not warn if the result type is signed, since the
2985 signed type will only be chosen if it can represent
2986 all the values of the unsigned type. */
2987 if (!TYPE_UNSIGNED (result_type))
2988 /* OK */;
2989 /* Do not warn if the signed quantity is an unsuffixed
2990 integer literal (or some static constant expression
2991 involving such literals) and it is non-negative. */
2992 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2993 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2994 /* OK */;
2995 else
2996 warning ("signed and unsigned type in conditional expression");
2997 }
2998 }
2999 }
3000 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3001 {
3002 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3003 pedwarn ("ISO C forbids conditional expr with only one void side");
3004 result_type = void_type_node;
3005 }
3006 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3007 {
3008 if (comp_target_types (type1, type2, 1))
3009 result_type = common_pointer_type (type1, type2);
3010 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3011 && TREE_CODE (orig_op1) != NOP_EXPR)
3012 result_type = qualify_type (type2, type1);
3013 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3014 && TREE_CODE (orig_op2) != NOP_EXPR)
3015 result_type = qualify_type (type1, type2);
3016 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3017 {
3018 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3019 pedwarn ("ISO C forbids conditional expr between "
3020 "%<void *%> and function pointer");
3021 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3022 TREE_TYPE (type2)));
3023 }
3024 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3025 {
3026 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3027 pedwarn ("ISO C forbids conditional expr between "
3028 "%<void *%> and function pointer");
3029 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3030 TREE_TYPE (type1)));
3031 }
3032 else
3033 {
3034 pedwarn ("pointer type mismatch in conditional expression");
3035 result_type = build_pointer_type (void_type_node);
3036 }
3037 }
3038 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3039 {
3040 if (!integer_zerop (op2))
3041 pedwarn ("pointer/integer type mismatch in conditional expression");
3042 else
3043 {
3044 op2 = null_pointer_node;
3045 }
3046 result_type = type1;
3047 }
3048 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3049 {
3050 if (!integer_zerop (op1))
3051 pedwarn ("pointer/integer type mismatch in conditional expression");
3052 else
3053 {
3054 op1 = null_pointer_node;
3055 }
3056 result_type = type2;
3057 }
3058
3059 if (!result_type)
3060 {
3061 if (flag_cond_mismatch)
3062 result_type = void_type_node;
3063 else
3064 {
3065 error ("type mismatch in conditional expression");
3066 return error_mark_node;
3067 }
3068 }
3069
3070 /* Merge const and volatile flags of the incoming types. */
3071 result_type
3072 = build_type_variant (result_type,
3073 TREE_READONLY (op1) || TREE_READONLY (op2),
3074 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3075
3076 if (result_type != TREE_TYPE (op1))
3077 op1 = convert_and_check (result_type, op1);
3078 if (result_type != TREE_TYPE (op2))
3079 op2 = convert_and_check (result_type, op2);
3080
3081 if (TREE_CODE (ifexp) == INTEGER_CST)
3082 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3083
3084 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3085 }
3086 \f
3087 /* Return a compound expression that performs two expressions and
3088 returns the value of the second of them. */
3089
3090 tree
3091 build_compound_expr (tree expr1, tree expr2)
3092 {
3093 /* Convert arrays and functions to pointers. */
3094 expr2 = default_function_array_conversion (expr2);
3095
3096 if (!TREE_SIDE_EFFECTS (expr1))
3097 {
3098 /* The left-hand operand of a comma expression is like an expression
3099 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3100 any side-effects, unless it was explicitly cast to (void). */
3101 if (warn_unused_value
3102 && !(TREE_CODE (expr1) == CONVERT_EXPR
3103 && VOID_TYPE_P (TREE_TYPE (expr1))))
3104 warning ("left-hand operand of comma expression has no effect");
3105 }
3106
3107 /* With -Wunused, we should also warn if the left-hand operand does have
3108 side-effects, but computes a value which is not used. For example, in
3109 `foo() + bar(), baz()' the result of the `+' operator is not used,
3110 so we should issue a warning. */
3111 else if (warn_unused_value)
3112 warn_if_unused_value (expr1, input_location);
3113
3114 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3115 }
3116
3117 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3118
3119 tree
3120 build_c_cast (tree type, tree expr)
3121 {
3122 tree value = expr;
3123
3124 if (type == error_mark_node || expr == error_mark_node)
3125 return error_mark_node;
3126
3127 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3128 only in <protocol> qualifications. But when constructing cast expressions,
3129 the protocols do matter and must be kept around. */
3130 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3131 return build1 (NOP_EXPR, type, expr);
3132
3133 type = TYPE_MAIN_VARIANT (type);
3134
3135 if (TREE_CODE (type) == ARRAY_TYPE)
3136 {
3137 error ("cast specifies array type");
3138 return error_mark_node;
3139 }
3140
3141 if (TREE_CODE (type) == FUNCTION_TYPE)
3142 {
3143 error ("cast specifies function type");
3144 return error_mark_node;
3145 }
3146
3147 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3148 {
3149 if (pedantic)
3150 {
3151 if (TREE_CODE (type) == RECORD_TYPE
3152 || TREE_CODE (type) == UNION_TYPE)
3153 pedwarn ("ISO C forbids casting nonscalar to the same type");
3154 }
3155 }
3156 else if (TREE_CODE (type) == UNION_TYPE)
3157 {
3158 tree field;
3159 value = default_function_array_conversion (value);
3160
3161 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3162 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3163 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3164 break;
3165
3166 if (field)
3167 {
3168 tree t;
3169
3170 if (pedantic)
3171 pedwarn ("ISO C forbids casts to union type");
3172 t = digest_init (type,
3173 build_constructor (type,
3174 build_tree_list (field, value)),
3175 true, 0);
3176 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3177 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3178 return t;
3179 }
3180 error ("cast to union type from type not present in union");
3181 return error_mark_node;
3182 }
3183 else
3184 {
3185 tree otype, ovalue;
3186
3187 /* If casting to void, avoid the error that would come
3188 from default_conversion in the case of a non-lvalue array. */
3189 if (type == void_type_node)
3190 return build1 (CONVERT_EXPR, type, value);
3191
3192 /* Convert functions and arrays to pointers,
3193 but don't convert any other types. */
3194 value = default_function_array_conversion (value);
3195 otype = TREE_TYPE (value);
3196
3197 /* Optionally warn about potentially worrisome casts. */
3198
3199 if (warn_cast_qual
3200 && TREE_CODE (type) == POINTER_TYPE
3201 && TREE_CODE (otype) == POINTER_TYPE)
3202 {
3203 tree in_type = type;
3204 tree in_otype = otype;
3205 int added = 0;
3206 int discarded = 0;
3207
3208 /* Check that the qualifiers on IN_TYPE are a superset of
3209 the qualifiers of IN_OTYPE. The outermost level of
3210 POINTER_TYPE nodes is uninteresting and we stop as soon
3211 as we hit a non-POINTER_TYPE node on either type. */
3212 do
3213 {
3214 in_otype = TREE_TYPE (in_otype);
3215 in_type = TREE_TYPE (in_type);
3216
3217 /* GNU C allows cv-qualified function types. 'const'
3218 means the function is very pure, 'volatile' means it
3219 can't return. We need to warn when such qualifiers
3220 are added, not when they're taken away. */
3221 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3222 && TREE_CODE (in_type) == FUNCTION_TYPE)
3223 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3224 else
3225 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3226 }
3227 while (TREE_CODE (in_type) == POINTER_TYPE
3228 && TREE_CODE (in_otype) == POINTER_TYPE);
3229
3230 if (added)
3231 warning ("cast adds new qualifiers to function type");
3232
3233 if (discarded)
3234 /* There are qualifiers present in IN_OTYPE that are not
3235 present in IN_TYPE. */
3236 warning ("cast discards qualifiers from pointer target type");
3237 }
3238
3239 /* Warn about possible alignment problems. */
3240 if (STRICT_ALIGNMENT && warn_cast_align
3241 && TREE_CODE (type) == POINTER_TYPE
3242 && TREE_CODE (otype) == POINTER_TYPE
3243 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3244 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3245 /* Don't warn about opaque types, where the actual alignment
3246 restriction is unknown. */
3247 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3248 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3249 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3250 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3251 warning ("cast increases required alignment of target type");
3252
3253 if (TREE_CODE (type) == INTEGER_TYPE
3254 && TREE_CODE (otype) == POINTER_TYPE
3255 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3256 && !TREE_CONSTANT (value))
3257 warning ("cast from pointer to integer of different size");
3258
3259 if (warn_bad_function_cast
3260 && TREE_CODE (value) == CALL_EXPR
3261 && TREE_CODE (type) != TREE_CODE (otype))
3262 warning ("cast from function call of type %qT to non-matching "
3263 "type %qT", otype, type);
3264
3265 if (TREE_CODE (type) == POINTER_TYPE
3266 && TREE_CODE (otype) == INTEGER_TYPE
3267 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3268 /* Don't warn about converting any constant. */
3269 && !TREE_CONSTANT (value))
3270 warning ("cast to pointer from integer of different size");
3271
3272 if (TREE_CODE (type) == POINTER_TYPE
3273 && TREE_CODE (otype) == POINTER_TYPE
3274 && TREE_CODE (expr) == ADDR_EXPR
3275 && DECL_P (TREE_OPERAND (expr, 0))
3276 && flag_strict_aliasing && warn_strict_aliasing
3277 && !VOID_TYPE_P (TREE_TYPE (type)))
3278 {
3279 /* Casting the address of a decl to non void pointer. Warn
3280 if the cast breaks type based aliasing. */
3281 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3282 warning ("type-punning to incomplete type might break strict-aliasing rules");
3283 else
3284 {
3285 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3286 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3287
3288 if (!alias_sets_conflict_p (set1, set2))
3289 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3290 else if (warn_strict_aliasing > 1
3291 && !alias_sets_might_conflict_p (set1, set2))
3292 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3293 }
3294 }
3295
3296 /* If pedantic, warn for conversions between function and object
3297 pointer types, except for converting a null pointer constant
3298 to function pointer type. */
3299 if (pedantic
3300 && TREE_CODE (type) == POINTER_TYPE
3301 && TREE_CODE (otype) == POINTER_TYPE
3302 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3303 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3304 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3305
3306 if (pedantic
3307 && TREE_CODE (type) == POINTER_TYPE
3308 && TREE_CODE (otype) == POINTER_TYPE
3309 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3310 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3311 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3312 && TREE_CODE (expr) != NOP_EXPR))
3313 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3314
3315 ovalue = value;
3316 value = convert (type, value);
3317
3318 /* Ignore any integer overflow caused by the cast. */
3319 if (TREE_CODE (value) == INTEGER_CST)
3320 {
3321 if (EXPR_P (ovalue))
3322 /* If OVALUE had overflow set, then so will VALUE, so it
3323 is safe to overwrite. */
3324 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3325 else
3326 TREE_OVERFLOW (value) = 0;
3327
3328 if (CONSTANT_CLASS_P (ovalue))
3329 /* Similarly, constant_overflow cannot have become
3330 cleared. */
3331 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3332 }
3333 }
3334
3335 /* Don't let a cast be an lvalue. */
3336 if (value == expr)
3337 value = non_lvalue (value);
3338
3339 return value;
3340 }
3341
3342 /* Interpret a cast of expression EXPR to type TYPE. */
3343 tree
3344 c_cast_expr (struct c_type_name *type_name, tree expr)
3345 {
3346 tree type;
3347 int saved_wsp = warn_strict_prototypes;
3348
3349 /* This avoids warnings about unprototyped casts on
3350 integers. E.g. "#define SIG_DFL (void(*)())0". */
3351 if (TREE_CODE (expr) == INTEGER_CST)
3352 warn_strict_prototypes = 0;
3353 type = groktypename (type_name);
3354 warn_strict_prototypes = saved_wsp;
3355
3356 return build_c_cast (type, expr);
3357 }
3358
3359 \f
3360 /* Build an assignment expression of lvalue LHS from value RHS.
3361 MODIFYCODE is the code for a binary operator that we use
3362 to combine the old value of LHS with RHS to get the new value.
3363 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3364
3365 tree
3366 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3367 {
3368 tree result;
3369 tree newrhs;
3370 tree lhstype = TREE_TYPE (lhs);
3371 tree olhstype = lhstype;
3372
3373 /* Types that aren't fully specified cannot be used in assignments. */
3374 lhs = require_complete_type (lhs);
3375
3376 /* Avoid duplicate error messages from operands that had errors. */
3377 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3378 return error_mark_node;
3379
3380 STRIP_TYPE_NOPS (rhs);
3381
3382 newrhs = rhs;
3383
3384 /* If a binary op has been requested, combine the old LHS value with the RHS
3385 producing the value we should actually store into the LHS. */
3386
3387 if (modifycode != NOP_EXPR)
3388 {
3389 lhs = stabilize_reference (lhs);
3390 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3391 }
3392
3393 if (!lvalue_or_else (lhs, lv_assign))
3394 return error_mark_node;
3395
3396 /* Give an error for storing in something that is 'const'. */
3397
3398 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3399 || ((TREE_CODE (lhstype) == RECORD_TYPE
3400 || TREE_CODE (lhstype) == UNION_TYPE)
3401 && C_TYPE_FIELDS_READONLY (lhstype)))
3402 readonly_error (lhs, lv_assign);
3403
3404 /* If storing into a structure or union member,
3405 it has probably been given type `int'.
3406 Compute the type that would go with
3407 the actual amount of storage the member occupies. */
3408
3409 if (TREE_CODE (lhs) == COMPONENT_REF
3410 && (TREE_CODE (lhstype) == INTEGER_TYPE
3411 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3412 || TREE_CODE (lhstype) == REAL_TYPE
3413 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3414 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3415
3416 /* If storing in a field that is in actuality a short or narrower than one,
3417 we must store in the field in its actual type. */
3418
3419 if (lhstype != TREE_TYPE (lhs))
3420 {
3421 lhs = copy_node (lhs);
3422 TREE_TYPE (lhs) = lhstype;
3423 }
3424
3425 /* Convert new value to destination type. */
3426
3427 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3428 NULL_TREE, NULL_TREE, 0);
3429 if (TREE_CODE (newrhs) == ERROR_MARK)
3430 return error_mark_node;
3431
3432 /* Scan operands. */
3433
3434 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3435 TREE_SIDE_EFFECTS (result) = 1;
3436
3437 /* If we got the LHS in a different type for storing in,
3438 convert the result back to the nominal type of LHS
3439 so that the value we return always has the same type
3440 as the LHS argument. */
3441
3442 if (olhstype == TREE_TYPE (result))
3443 return result;
3444 return convert_for_assignment (olhstype, result, ic_assign,
3445 NULL_TREE, NULL_TREE, 0);
3446 }
3447 \f
3448 /* Convert value RHS to type TYPE as preparation for an assignment
3449 to an lvalue of type TYPE.
3450 The real work of conversion is done by `convert'.
3451 The purpose of this function is to generate error messages
3452 for assignments that are not allowed in C.
3453 ERRTYPE says whether it is argument passing, assignment,
3454 initialization or return.
3455
3456 FUNCTION is a tree for the function being called.
3457 PARMNUM is the number of the argument, for printing in error messages. */
3458
3459 static tree
3460 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3461 tree fundecl, tree function, int parmnum)
3462 {
3463 enum tree_code codel = TREE_CODE (type);
3464 tree rhstype;
3465 enum tree_code coder;
3466 tree rname = NULL_TREE;
3467
3468 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3469 {
3470 tree selector;
3471 /* Change pointer to function to the function itself for
3472 diagnostics. */
3473 if (TREE_CODE (function) == ADDR_EXPR
3474 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3475 function = TREE_OPERAND (function, 0);
3476
3477 /* Handle an ObjC selector specially for diagnostics. */
3478 selector = objc_message_selector ();
3479 rname = function;
3480 if (selector && parmnum > 2)
3481 {
3482 rname = selector;
3483 parmnum -= 2;
3484 }
3485 }
3486
3487 /* This macro is used to emit diagnostics to ensure that all format
3488 strings are complete sentences, visible to gettext and checked at
3489 compile time. */
3490 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3491 do { \
3492 switch (errtype) \
3493 { \
3494 case ic_argpass: \
3495 pedwarn (AR, parmnum, rname); \
3496 break; \
3497 case ic_argpass_nonproto: \
3498 warning (AR, parmnum, rname); \
3499 break; \
3500 case ic_assign: \
3501 pedwarn (AS); \
3502 break; \
3503 case ic_init: \
3504 pedwarn (IN); \
3505 break; \
3506 case ic_return: \
3507 pedwarn (RE); \
3508 break; \
3509 default: \
3510 gcc_unreachable (); \
3511 } \
3512 } while (0)
3513
3514 STRIP_TYPE_NOPS (rhs);
3515
3516 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3517 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3518 rhs = default_conversion (rhs);
3519 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3520 rhs = decl_constant_value_for_broken_optimization (rhs);
3521
3522 rhstype = TREE_TYPE (rhs);
3523 coder = TREE_CODE (rhstype);
3524
3525 if (coder == ERROR_MARK)
3526 return error_mark_node;
3527
3528 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3529 {
3530 overflow_warning (rhs);
3531 /* Check for Objective-C protocols. This will automatically
3532 issue a warning if there are protocol violations. No need to
3533 use the return value. */
3534 if (c_dialect_objc ())
3535 objc_comptypes (type, rhstype, 0);
3536 return rhs;
3537 }
3538
3539 if (coder == VOID_TYPE)
3540 {
3541 /* Except for passing an argument to an unprototyped function,
3542 this is a constraint violation. When passing an argument to
3543 an unprototyped function, it is compile-time undefined;
3544 making it a constraint in that case was rejected in
3545 DR#252. */
3546 error ("void value not ignored as it ought to be");
3547 return error_mark_node;
3548 }
3549 /* A type converts to a reference to it.
3550 This code doesn't fully support references, it's just for the
3551 special case of va_start and va_copy. */
3552 if (codel == REFERENCE_TYPE
3553 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3554 {
3555 if (!lvalue_p (rhs))
3556 {
3557 error ("cannot pass rvalue to reference parameter");
3558 return error_mark_node;
3559 }
3560 if (!c_mark_addressable (rhs))
3561 return error_mark_node;
3562 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3563
3564 /* We already know that these two types are compatible, but they
3565 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3566 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3567 likely to be va_list, a typedef to __builtin_va_list, which
3568 is different enough that it will cause problems later. */
3569 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3570 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3571
3572 rhs = build1 (NOP_EXPR, type, rhs);
3573 return rhs;
3574 }
3575 /* Some types can interconvert without explicit casts. */
3576 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3577 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3578 return convert (type, rhs);
3579 /* Arithmetic types all interconvert, and enum is treated like int. */
3580 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3581 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3582 || codel == BOOLEAN_TYPE)
3583 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3584 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3585 || coder == BOOLEAN_TYPE))
3586 return convert_and_check (type, rhs);
3587
3588 /* Conversion to a transparent union from its member types.
3589 This applies only to function arguments. */
3590 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3591 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3592 {
3593 tree memb_types;
3594 tree marginal_memb_type = 0;
3595
3596 for (memb_types = TYPE_FIELDS (type); memb_types;
3597 memb_types = TREE_CHAIN (memb_types))
3598 {
3599 tree memb_type = TREE_TYPE (memb_types);
3600
3601 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3602 TYPE_MAIN_VARIANT (rhstype)))
3603 break;
3604
3605 if (TREE_CODE (memb_type) != POINTER_TYPE)
3606 continue;
3607
3608 if (coder == POINTER_TYPE)
3609 {
3610 tree ttl = TREE_TYPE (memb_type);
3611 tree ttr = TREE_TYPE (rhstype);
3612
3613 /* Any non-function converts to a [const][volatile] void *
3614 and vice versa; otherwise, targets must be the same.
3615 Meanwhile, the lhs target must have all the qualifiers of
3616 the rhs. */
3617 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3618 || comp_target_types (memb_type, rhstype, 0))
3619 {
3620 /* If this type won't generate any warnings, use it. */
3621 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3622 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3623 && TREE_CODE (ttl) == FUNCTION_TYPE)
3624 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3625 == TYPE_QUALS (ttr))
3626 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3627 == TYPE_QUALS (ttl))))
3628 break;
3629
3630 /* Keep looking for a better type, but remember this one. */
3631 if (!marginal_memb_type)
3632 marginal_memb_type = memb_type;
3633 }
3634 }
3635
3636 /* Can convert integer zero to any pointer type. */
3637 if (integer_zerop (rhs)
3638 || (TREE_CODE (rhs) == NOP_EXPR
3639 && integer_zerop (TREE_OPERAND (rhs, 0))))
3640 {
3641 rhs = null_pointer_node;
3642 break;
3643 }
3644 }
3645
3646 if (memb_types || marginal_memb_type)
3647 {
3648 if (!memb_types)
3649 {
3650 /* We have only a marginally acceptable member type;
3651 it needs a warning. */
3652 tree ttl = TREE_TYPE (marginal_memb_type);
3653 tree ttr = TREE_TYPE (rhstype);
3654
3655 /* Const and volatile mean something different for function
3656 types, so the usual warnings are not appropriate. */
3657 if (TREE_CODE (ttr) == FUNCTION_TYPE
3658 && TREE_CODE (ttl) == FUNCTION_TYPE)
3659 {
3660 /* Because const and volatile on functions are
3661 restrictions that say the function will not do
3662 certain things, it is okay to use a const or volatile
3663 function where an ordinary one is wanted, but not
3664 vice-versa. */
3665 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3666 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3667 "makes qualified function "
3668 "pointer from unqualified"),
3669 N_("assignment makes qualified "
3670 "function pointer from "
3671 "unqualified"),
3672 N_("initialization makes qualified "
3673 "function pointer from "
3674 "unqualified"),
3675 N_("return makes qualified function "
3676 "pointer from unqualified"));
3677 }
3678 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3679 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3680 "qualifiers from pointer target type"),
3681 N_("assignment discards qualifiers "
3682 "from pointer target type"),
3683 N_("initialization discards qualifiers "
3684 "from pointer target type"),
3685 N_("return discards qualifiers from "
3686 "pointer target type"));
3687 }
3688
3689 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3690 pedwarn ("ISO C prohibits argument conversion to union type");
3691
3692 return build1 (NOP_EXPR, type, rhs);
3693 }
3694 }
3695
3696 /* Conversions among pointers */
3697 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3698 && (coder == codel))
3699 {
3700 tree ttl = TREE_TYPE (type);
3701 tree ttr = TREE_TYPE (rhstype);
3702 tree mvl = ttl;
3703 tree mvr = ttr;
3704 bool is_opaque_pointer;
3705 int target_cmp = 0; /* Cache comp_target_types () result. */
3706
3707 if (TREE_CODE (mvl) != ARRAY_TYPE)
3708 mvl = TYPE_MAIN_VARIANT (mvl);
3709 if (TREE_CODE (mvr) != ARRAY_TYPE)
3710 mvr = TYPE_MAIN_VARIANT (mvr);
3711 /* Opaque pointers are treated like void pointers. */
3712 is_opaque_pointer = (targetm.vector_opaque_p (type)
3713 || targetm.vector_opaque_p (rhstype))
3714 && TREE_CODE (ttl) == VECTOR_TYPE
3715 && TREE_CODE (ttr) == VECTOR_TYPE;
3716
3717 /* Any non-function converts to a [const][volatile] void *
3718 and vice versa; otherwise, targets must be the same.
3719 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3720 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3721 || (target_cmp = comp_target_types (type, rhstype, 0))
3722 || is_opaque_pointer
3723 || (c_common_unsigned_type (mvl)
3724 == c_common_unsigned_type (mvr)))
3725 {
3726 if (pedantic
3727 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3728 ||
3729 (VOID_TYPE_P (ttr)
3730 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3731 which are not ANSI null ptr constants. */
3732 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3733 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3734 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3735 "%qE between function pointer "
3736 "and %<void *%>"),
3737 N_("ISO C forbids assignment between "
3738 "function pointer and %<void *%>"),
3739 N_("ISO C forbids initialization between "
3740 "function pointer and %<void *%>"),
3741 N_("ISO C forbids return between function "
3742 "pointer and %<void *%>"));
3743 /* Const and volatile mean something different for function types,
3744 so the usual warnings are not appropriate. */
3745 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3746 && TREE_CODE (ttl) != FUNCTION_TYPE)
3747 {
3748 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3749 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3750 "qualifiers from pointer target type"),
3751 N_("assignment discards qualifiers "
3752 "from pointer target type"),
3753 N_("initialization discards qualifiers "
3754 "from pointer target type"),
3755 N_("return discards qualifiers from "
3756 "pointer target type"));
3757 /* If this is not a case of ignoring a mismatch in signedness,
3758 no warning. */
3759 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3760 || target_cmp)
3761 ;
3762 /* If there is a mismatch, do warn. */
3763 else if (warn_pointer_sign)
3764 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3765 "%d of %qE differ in signedness"),
3766 N_("pointer targets in assignment "
3767 "differ in signedness"),
3768 N_("pointer targets in initialization "
3769 "differ in signedness"),
3770 N_("pointer targets in return differ "
3771 "in signedness"));
3772 }
3773 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3774 && TREE_CODE (ttr) == FUNCTION_TYPE)
3775 {
3776 /* Because const and volatile on functions are restrictions
3777 that say the function will not do certain things,
3778 it is okay to use a const or volatile function
3779 where an ordinary one is wanted, but not vice-versa. */
3780 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3781 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3782 "qualified function pointer "
3783 "from unqualified"),
3784 N_("assignment makes qualified function "
3785 "pointer from unqualified"),
3786 N_("initialization makes qualified "
3787 "function pointer from unqualified"),
3788 N_("return makes qualified function "
3789 "pointer from unqualified"));
3790 }
3791 }
3792 else
3793 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3794 "incompatible pointer type"),
3795 N_("assignment from incompatible pointer type"),
3796 N_("initialization from incompatible "
3797 "pointer type"),
3798 N_("return from incompatible pointer type"));
3799 return convert (type, rhs);
3800 }
3801 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3802 {
3803 /* ??? This should not be an error when inlining calls to
3804 unprototyped functions. */
3805 error ("invalid use of non-lvalue array");
3806 return error_mark_node;
3807 }
3808 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3809 {
3810 /* An explicit constant 0 can convert to a pointer,
3811 or one that results from arithmetic, even including
3812 a cast to integer type. */
3813 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3814 &&
3815 !(TREE_CODE (rhs) == NOP_EXPR
3816 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3817 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3818 && integer_zerop (TREE_OPERAND (rhs, 0))))
3819 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3820 "pointer from integer without a cast"),
3821 N_("assignment makes pointer from integer "
3822 "without a cast"),
3823 N_("initialization makes pointer from "
3824 "integer without a cast"),
3825 N_("return makes pointer from integer "
3826 "without a cast"));
3827
3828 return convert (type, rhs);
3829 }
3830 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3831 {
3832 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3833 "from pointer without a cast"),
3834 N_("assignment makes integer from pointer "
3835 "without a cast"),
3836 N_("initialization makes integer from pointer "
3837 "without a cast"),
3838 N_("return makes integer from pointer "
3839 "without a cast"));
3840 return convert (type, rhs);
3841 }
3842 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3843 return convert (type, rhs);
3844
3845 switch (errtype)
3846 {
3847 case ic_argpass:
3848 case ic_argpass_nonproto:
3849 /* ??? This should not be an error when inlining calls to
3850 unprototyped functions. */
3851 error ("incompatible type for argument %d of %qE", parmnum, rname);
3852 break;
3853 case ic_assign:
3854 error ("incompatible types in assignment");
3855 break;
3856 case ic_init:
3857 error ("incompatible types in initialization");
3858 break;
3859 case ic_return:
3860 error ("incompatible types in return");
3861 break;
3862 default:
3863 gcc_unreachable ();
3864 }
3865
3866 return error_mark_node;
3867 }
3868
3869 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3870 is used for error and waring reporting and indicates which argument
3871 is being processed. */
3872
3873 tree
3874 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3875 {
3876 tree ret, type;
3877
3878 /* If FN was prototyped, the value has been converted already
3879 in convert_arguments. */
3880 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3881 return value;
3882
3883 type = TREE_TYPE (parm);
3884 ret = convert_for_assignment (type, value,
3885 ic_argpass_nonproto, fn,
3886 fn, argnum);
3887 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3888 && INTEGRAL_TYPE_P (type)
3889 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3890 ret = default_conversion (ret);
3891 return ret;
3892 }
3893 \f
3894 /* If VALUE is a compound expr all of whose expressions are constant, then
3895 return its value. Otherwise, return error_mark_node.
3896
3897 This is for handling COMPOUND_EXPRs as initializer elements
3898 which is allowed with a warning when -pedantic is specified. */
3899
3900 static tree
3901 valid_compound_expr_initializer (tree value, tree endtype)
3902 {
3903 if (TREE_CODE (value) == COMPOUND_EXPR)
3904 {
3905 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3906 == error_mark_node)
3907 return error_mark_node;
3908 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3909 endtype);
3910 }
3911 else if (!initializer_constant_valid_p (value, endtype))
3912 return error_mark_node;
3913 else
3914 return value;
3915 }
3916 \f
3917 /* Perform appropriate conversions on the initial value of a variable,
3918 store it in the declaration DECL,
3919 and print any error messages that are appropriate.
3920 If the init is invalid, store an ERROR_MARK. */
3921
3922 void
3923 store_init_value (tree decl, tree init)
3924 {
3925 tree value, type;
3926
3927 /* If variable's type was invalidly declared, just ignore it. */
3928
3929 type = TREE_TYPE (decl);
3930 if (TREE_CODE (type) == ERROR_MARK)
3931 return;
3932
3933 /* Digest the specified initializer into an expression. */
3934
3935 value = digest_init (type, init, true, TREE_STATIC (decl));
3936
3937 /* Store the expression if valid; else report error. */
3938
3939 if (warn_traditional && !in_system_header
3940 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3941 warning ("traditional C rejects automatic aggregate initialization");
3942
3943 DECL_INITIAL (decl) = value;
3944
3945 /* ANSI wants warnings about out-of-range constant initializers. */
3946 STRIP_TYPE_NOPS (value);
3947 constant_expression_warning (value);
3948
3949 /* Check if we need to set array size from compound literal size. */
3950 if (TREE_CODE (type) == ARRAY_TYPE
3951 && TYPE_DOMAIN (type) == 0
3952 && value != error_mark_node)
3953 {
3954 tree inside_init = init;
3955
3956 STRIP_TYPE_NOPS (inside_init);
3957 inside_init = fold (inside_init);
3958
3959 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3960 {
3961 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3962
3963 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3964 {
3965 /* For int foo[] = (int [3]){1}; we need to set array size
3966 now since later on array initializer will be just the
3967 brace enclosed list of the compound literal. */
3968 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3969 layout_type (type);
3970 layout_decl (decl, 0);
3971 }
3972 }
3973 }
3974 }
3975 \f
3976 /* Methods for storing and printing names for error messages. */
3977
3978 /* Implement a spelling stack that allows components of a name to be pushed
3979 and popped. Each element on the stack is this structure. */
3980
3981 struct spelling
3982 {
3983 int kind;
3984 union
3985 {
3986 int i;
3987 const char *s;
3988 } u;
3989 };
3990
3991 #define SPELLING_STRING 1
3992 #define SPELLING_MEMBER 2
3993 #define SPELLING_BOUNDS 3
3994
3995 static struct spelling *spelling; /* Next stack element (unused). */
3996 static struct spelling *spelling_base; /* Spelling stack base. */
3997 static int spelling_size; /* Size of the spelling stack. */
3998
3999 /* Macros to save and restore the spelling stack around push_... functions.
4000 Alternative to SAVE_SPELLING_STACK. */
4001
4002 #define SPELLING_DEPTH() (spelling - spelling_base)
4003 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4004
4005 /* Push an element on the spelling stack with type KIND and assign VALUE
4006 to MEMBER. */
4007
4008 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4009 { \
4010 int depth = SPELLING_DEPTH (); \
4011 \
4012 if (depth >= spelling_size) \
4013 { \
4014 spelling_size += 10; \
4015 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4016 spelling_size); \
4017 RESTORE_SPELLING_DEPTH (depth); \
4018 } \
4019 \
4020 spelling->kind = (KIND); \
4021 spelling->MEMBER = (VALUE); \
4022 spelling++; \
4023 }
4024
4025 /* Push STRING on the stack. Printed literally. */
4026
4027 static void
4028 push_string (const char *string)
4029 {
4030 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4031 }
4032
4033 /* Push a member name on the stack. Printed as '.' STRING. */
4034
4035 static void
4036 push_member_name (tree decl)
4037 {
4038 const char *const string
4039 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4040 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4041 }
4042
4043 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4044
4045 static void
4046 push_array_bounds (int bounds)
4047 {
4048 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4049 }
4050
4051 /* Compute the maximum size in bytes of the printed spelling. */
4052
4053 static int
4054 spelling_length (void)
4055 {
4056 int size = 0;
4057 struct spelling *p;
4058
4059 for (p = spelling_base; p < spelling; p++)
4060 {
4061 if (p->kind == SPELLING_BOUNDS)
4062 size += 25;
4063 else
4064 size += strlen (p->u.s) + 1;
4065 }
4066
4067 return size;
4068 }
4069
4070 /* Print the spelling to BUFFER and return it. */
4071
4072 static char *
4073 print_spelling (char *buffer)
4074 {
4075 char *d = buffer;
4076 struct spelling *p;
4077
4078 for (p = spelling_base; p < spelling; p++)
4079 if (p->kind == SPELLING_BOUNDS)
4080 {
4081 sprintf (d, "[%d]", p->u.i);
4082 d += strlen (d);
4083 }
4084 else
4085 {
4086 const char *s;
4087 if (p->kind == SPELLING_MEMBER)
4088 *d++ = '.';
4089 for (s = p->u.s; (*d = *s++); d++)
4090 ;
4091 }
4092 *d++ = '\0';
4093 return buffer;
4094 }
4095
4096 /* Issue an error message for a bad initializer component.
4097 MSGID identifies the message.
4098 The component name is taken from the spelling stack. */
4099
4100 void
4101 error_init (const char *msgid)
4102 {
4103 char *ofwhat;
4104
4105 error ("%s", _(msgid));
4106 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4107 if (*ofwhat)
4108 error ("(near initialization for %qs)", ofwhat);
4109 }
4110
4111 /* Issue a pedantic warning for a bad initializer component.
4112 MSGID identifies the message.
4113 The component name is taken from the spelling stack. */
4114
4115 void
4116 pedwarn_init (const char *msgid)
4117 {
4118 char *ofwhat;
4119
4120 pedwarn ("%s", _(msgid));
4121 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4122 if (*ofwhat)
4123 pedwarn ("(near initialization for %qs)", ofwhat);
4124 }
4125
4126 /* Issue a warning for a bad initializer component.
4127 MSGID identifies the message.
4128 The component name is taken from the spelling stack. */
4129
4130 static void
4131 warning_init (const char *msgid)
4132 {
4133 char *ofwhat;
4134
4135 warning ("%s", _(msgid));
4136 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4137 if (*ofwhat)
4138 warning ("(near initialization for %qs)", ofwhat);
4139 }
4140 \f
4141 /* If TYPE is an array type and EXPR is a parenthesized string
4142 constant, warn if pedantic that EXPR is being used to initialize an
4143 object of type TYPE. */
4144
4145 void
4146 maybe_warn_string_init (tree type, struct c_expr expr)
4147 {
4148 if (pedantic
4149 && TREE_CODE (type) == ARRAY_TYPE
4150 && TREE_CODE (expr.value) == STRING_CST
4151 && expr.original_code != STRING_CST)
4152 pedwarn_init ("array initialized from parenthesized string constant");
4153 }
4154
4155 /* Digest the parser output INIT as an initializer for type TYPE.
4156 Return a C expression of type TYPE to represent the initial value.
4157
4158 If INIT is a string constant, STRICT_STRING is true if it is
4159 unparenthesized or we should not warn here for it being parenthesized.
4160 For other types of INIT, STRICT_STRING is not used.
4161
4162 REQUIRE_CONSTANT requests an error if non-constant initializers or
4163 elements are seen. */
4164
4165 static tree
4166 digest_init (tree type, tree init, bool strict_string, int require_constant)
4167 {
4168 enum tree_code code = TREE_CODE (type);
4169 tree inside_init = init;
4170
4171 if (type == error_mark_node
4172 || init == error_mark_node
4173 || TREE_TYPE (init) == error_mark_node)
4174 return error_mark_node;
4175
4176 STRIP_TYPE_NOPS (inside_init);
4177
4178 inside_init = fold (inside_init);
4179
4180 /* Initialization of an array of chars from a string constant
4181 optionally enclosed in braces. */
4182
4183 if (code == ARRAY_TYPE && inside_init
4184 && TREE_CODE (inside_init) == STRING_CST)
4185 {
4186 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4187 /* Note that an array could be both an array of character type
4188 and an array of wchar_t if wchar_t is signed char or unsigned
4189 char. */
4190 bool char_array = (typ1 == char_type_node
4191 || typ1 == signed_char_type_node
4192 || typ1 == unsigned_char_type_node);
4193 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4194 if (char_array || wchar_array)
4195 {
4196 struct c_expr expr;
4197 bool char_string;
4198 expr.value = inside_init;
4199 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4200 maybe_warn_string_init (type, expr);
4201
4202 char_string
4203 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4204 == char_type_node);
4205
4206 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4207 TYPE_MAIN_VARIANT (type)))
4208 return inside_init;
4209
4210 if (!wchar_array && !char_string)
4211 {
4212 error_init ("char-array initialized from wide string");
4213 return error_mark_node;
4214 }
4215 if (char_string && !char_array)
4216 {
4217 error_init ("wchar_t-array initialized from non-wide string");
4218 return error_mark_node;
4219 }
4220
4221 TREE_TYPE (inside_init) = type;
4222 if (TYPE_DOMAIN (type) != 0
4223 && TYPE_SIZE (type) != 0
4224 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4225 /* Subtract 1 (or sizeof (wchar_t))
4226 because it's ok to ignore the terminating null char
4227 that is counted in the length of the constant. */
4228 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4229 TREE_STRING_LENGTH (inside_init)
4230 - ((TYPE_PRECISION (typ1)
4231 != TYPE_PRECISION (char_type_node))
4232 ? (TYPE_PRECISION (wchar_type_node)
4233 / BITS_PER_UNIT)
4234 : 1)))
4235 pedwarn_init ("initializer-string for array of chars is too long");
4236
4237 return inside_init;
4238 }
4239 else if (INTEGRAL_TYPE_P (typ1))
4240 {
4241 error_init ("array of inappropriate type initialized "
4242 "from string constant");
4243 return error_mark_node;
4244 }
4245 }
4246
4247 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4248 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4249 below and handle as a constructor. */
4250 if (code == VECTOR_TYPE
4251 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4252 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4253 && TREE_CONSTANT (inside_init))
4254 {
4255 if (TREE_CODE (inside_init) == VECTOR_CST
4256 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4257 TYPE_MAIN_VARIANT (type)))
4258 return inside_init;
4259
4260 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4261 {
4262 tree link;
4263
4264 /* Iterate through elements and check if all constructor
4265 elements are *_CSTs. */
4266 for (link = CONSTRUCTOR_ELTS (inside_init);
4267 link;
4268 link = TREE_CHAIN (link))
4269 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4270 break;
4271
4272 if (link == NULL)
4273 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4274 }
4275 }
4276
4277 /* Any type can be initialized
4278 from an expression of the same type, optionally with braces. */
4279
4280 if (inside_init && TREE_TYPE (inside_init) != 0
4281 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4282 TYPE_MAIN_VARIANT (type))
4283 || (code == ARRAY_TYPE
4284 && comptypes (TREE_TYPE (inside_init), type))
4285 || (code == VECTOR_TYPE
4286 && comptypes (TREE_TYPE (inside_init), type))
4287 || (code == POINTER_TYPE
4288 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4289 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4290 TREE_TYPE (type)))
4291 || (code == POINTER_TYPE
4292 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4293 && comptypes (TREE_TYPE (inside_init),
4294 TREE_TYPE (type)))))
4295 {
4296 if (code == POINTER_TYPE)
4297 {
4298 inside_init = default_function_array_conversion (inside_init);
4299
4300 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4301 {
4302 error_init ("invalid use of non-lvalue array");
4303 return error_mark_node;
4304 }
4305 }
4306
4307 if (code == VECTOR_TYPE)
4308 /* Although the types are compatible, we may require a
4309 conversion. */
4310 inside_init = convert (type, inside_init);
4311
4312 if (require_constant && !flag_isoc99
4313 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4314 {
4315 /* As an extension, allow initializing objects with static storage
4316 duration with compound literals (which are then treated just as
4317 the brace enclosed list they contain). */
4318 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4319 inside_init = DECL_INITIAL (decl);
4320 }
4321
4322 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4323 && TREE_CODE (inside_init) != CONSTRUCTOR)
4324 {
4325 error_init ("array initialized from non-constant array expression");
4326 return error_mark_node;
4327 }
4328
4329 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4330 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4331
4332 /* Compound expressions can only occur here if -pedantic or
4333 -pedantic-errors is specified. In the later case, we always want
4334 an error. In the former case, we simply want a warning. */
4335 if (require_constant && pedantic
4336 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4337 {
4338 inside_init
4339 = valid_compound_expr_initializer (inside_init,
4340 TREE_TYPE (inside_init));
4341 if (inside_init == error_mark_node)
4342 error_init ("initializer element is not constant");
4343 else
4344 pedwarn_init ("initializer element is not constant");
4345 if (flag_pedantic_errors)
4346 inside_init = error_mark_node;
4347 }
4348 else if (require_constant
4349 && !initializer_constant_valid_p (inside_init,
4350 TREE_TYPE (inside_init)))
4351 {
4352 error_init ("initializer element is not constant");
4353 inside_init = error_mark_node;
4354 }
4355
4356 return inside_init;
4357 }
4358
4359 /* Handle scalar types, including conversions. */
4360
4361 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4362 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4363 || code == VECTOR_TYPE)
4364 {
4365 /* Note that convert_for_assignment calls default_conversion
4366 for arrays and functions. We must not call it in the
4367 case where inside_init is a null pointer constant. */
4368 inside_init
4369 = convert_for_assignment (type, init, ic_init,
4370 NULL_TREE, NULL_TREE, 0);
4371
4372 /* Check to see if we have already given an error message. */
4373 if (inside_init == error_mark_node)
4374 ;
4375 else if (require_constant && !TREE_CONSTANT (inside_init))
4376 {
4377 error_init ("initializer element is not constant");
4378 inside_init = error_mark_node;
4379 }
4380 else if (require_constant
4381 && !initializer_constant_valid_p (inside_init,
4382 TREE_TYPE (inside_init)))
4383 {
4384 error_init ("initializer element is not computable at load time");
4385 inside_init = error_mark_node;
4386 }
4387
4388 return inside_init;
4389 }
4390
4391 /* Come here only for records and arrays. */
4392
4393 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4394 {
4395 error_init ("variable-sized object may not be initialized");
4396 return error_mark_node;
4397 }
4398
4399 error_init ("invalid initializer");
4400 return error_mark_node;
4401 }
4402 \f
4403 /* Handle initializers that use braces. */
4404
4405 /* Type of object we are accumulating a constructor for.
4406 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4407 static tree constructor_type;
4408
4409 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4410 left to fill. */
4411 static tree constructor_fields;
4412
4413 /* For an ARRAY_TYPE, this is the specified index
4414 at which to store the next element we get. */
4415 static tree constructor_index;
4416
4417 /* For an ARRAY_TYPE, this is the maximum index. */
4418 static tree constructor_max_index;
4419
4420 /* For a RECORD_TYPE, this is the first field not yet written out. */
4421 static tree constructor_unfilled_fields;
4422
4423 /* For an ARRAY_TYPE, this is the index of the first element
4424 not yet written out. */
4425 static tree constructor_unfilled_index;
4426
4427 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4428 This is so we can generate gaps between fields, when appropriate. */
4429 static tree constructor_bit_index;
4430
4431 /* If we are saving up the elements rather than allocating them,
4432 this is the list of elements so far (in reverse order,
4433 most recent first). */
4434 static tree constructor_elements;
4435
4436 /* 1 if constructor should be incrementally stored into a constructor chain,
4437 0 if all the elements should be kept in AVL tree. */
4438 static int constructor_incremental;
4439
4440 /* 1 if so far this constructor's elements are all compile-time constants. */
4441 static int constructor_constant;
4442
4443 /* 1 if so far this constructor's elements are all valid address constants. */
4444 static int constructor_simple;
4445
4446 /* 1 if this constructor is erroneous so far. */
4447 static int constructor_erroneous;
4448
4449 /* Structure for managing pending initializer elements, organized as an
4450 AVL tree. */
4451
4452 struct init_node
4453 {
4454 struct init_node *left, *right;
4455 struct init_node *parent;
4456 int balance;
4457 tree purpose;
4458 tree value;
4459 };
4460
4461 /* Tree of pending elements at this constructor level.
4462 These are elements encountered out of order
4463 which belong at places we haven't reached yet in actually
4464 writing the output.
4465 Will never hold tree nodes across GC runs. */
4466 static struct init_node *constructor_pending_elts;
4467
4468 /* The SPELLING_DEPTH of this constructor. */
4469 static int constructor_depth;
4470
4471 /* DECL node for which an initializer is being read.
4472 0 means we are reading a constructor expression
4473 such as (struct foo) {...}. */
4474 static tree constructor_decl;
4475
4476 /* Nonzero if this is an initializer for a top-level decl. */
4477 static int constructor_top_level;
4478
4479 /* Nonzero if there were any member designators in this initializer. */
4480 static int constructor_designated;
4481
4482 /* Nesting depth of designator list. */
4483 static int designator_depth;
4484
4485 /* Nonzero if there were diagnosed errors in this designator list. */
4486 static int designator_errorneous;
4487
4488 \f
4489 /* This stack has a level for each implicit or explicit level of
4490 structuring in the initializer, including the outermost one. It
4491 saves the values of most of the variables above. */
4492
4493 struct constructor_range_stack;
4494
4495 struct constructor_stack
4496 {
4497 struct constructor_stack *next;
4498 tree type;
4499 tree fields;
4500 tree index;
4501 tree max_index;
4502 tree unfilled_index;
4503 tree unfilled_fields;
4504 tree bit_index;
4505 tree elements;
4506 struct init_node *pending_elts;
4507 int offset;
4508 int depth;
4509 /* If value nonzero, this value should replace the entire
4510 constructor at this level. */
4511 struct c_expr replacement_value;
4512 struct constructor_range_stack *range_stack;
4513 char constant;
4514 char simple;
4515 char implicit;
4516 char erroneous;
4517 char outer;
4518 char incremental;
4519 char designated;
4520 };
4521
4522 static struct constructor_stack *constructor_stack;
4523
4524 /* This stack represents designators from some range designator up to
4525 the last designator in the list. */
4526
4527 struct constructor_range_stack
4528 {
4529 struct constructor_range_stack *next, *prev;
4530 struct constructor_stack *stack;
4531 tree range_start;
4532 tree index;
4533 tree range_end;
4534 tree fields;
4535 };
4536
4537 static struct constructor_range_stack *constructor_range_stack;
4538
4539 /* This stack records separate initializers that are nested.
4540 Nested initializers can't happen in ANSI C, but GNU C allows them
4541 in cases like { ... (struct foo) { ... } ... }. */
4542
4543 struct initializer_stack
4544 {
4545 struct initializer_stack *next;
4546 tree decl;
4547 struct constructor_stack *constructor_stack;
4548 struct constructor_range_stack *constructor_range_stack;
4549 tree elements;
4550 struct spelling *spelling;
4551 struct spelling *spelling_base;
4552 int spelling_size;
4553 char top_level;
4554 char require_constant_value;
4555 char require_constant_elements;
4556 };
4557
4558 static struct initializer_stack *initializer_stack;
4559 \f
4560 /* Prepare to parse and output the initializer for variable DECL. */
4561
4562 void
4563 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4564 {
4565 const char *locus;
4566 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4567
4568 p->decl = constructor_decl;
4569 p->require_constant_value = require_constant_value;
4570 p->require_constant_elements = require_constant_elements;
4571 p->constructor_stack = constructor_stack;
4572 p->constructor_range_stack = constructor_range_stack;
4573 p->elements = constructor_elements;
4574 p->spelling = spelling;
4575 p->spelling_base = spelling_base;
4576 p->spelling_size = spelling_size;
4577 p->top_level = constructor_top_level;
4578 p->next = initializer_stack;
4579 initializer_stack = p;
4580
4581 constructor_decl = decl;
4582 constructor_designated = 0;
4583 constructor_top_level = top_level;
4584
4585 if (decl != 0 && decl != error_mark_node)
4586 {
4587 require_constant_value = TREE_STATIC (decl);
4588 require_constant_elements
4589 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4590 /* For a scalar, you can always use any value to initialize,
4591 even within braces. */
4592 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4593 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4594 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4595 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4596 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4597 }
4598 else
4599 {
4600 require_constant_value = 0;
4601 require_constant_elements = 0;
4602 locus = "(anonymous)";
4603 }
4604
4605 constructor_stack = 0;
4606 constructor_range_stack = 0;
4607
4608 missing_braces_mentioned = 0;
4609
4610 spelling_base = 0;
4611 spelling_size = 0;
4612 RESTORE_SPELLING_DEPTH (0);
4613
4614 if (locus)
4615 push_string (locus);
4616 }
4617
4618 void
4619 finish_init (void)
4620 {
4621 struct initializer_stack *p = initializer_stack;
4622
4623 /* Free the whole constructor stack of this initializer. */
4624 while (constructor_stack)
4625 {
4626 struct constructor_stack *q = constructor_stack;
4627 constructor_stack = q->next;
4628 free (q);
4629 }
4630
4631 gcc_assert (!constructor_range_stack);
4632
4633 /* Pop back to the data of the outer initializer (if any). */
4634 free (spelling_base);
4635
4636 constructor_decl = p->decl;
4637 require_constant_value = p->require_constant_value;
4638 require_constant_elements = p->require_constant_elements;
4639 constructor_stack = p->constructor_stack;
4640 constructor_range_stack = p->constructor_range_stack;
4641 constructor_elements = p->elements;
4642 spelling = p->spelling;
4643 spelling_base = p->spelling_base;
4644 spelling_size = p->spelling_size;
4645 constructor_top_level = p->top_level;
4646 initializer_stack = p->next;
4647 free (p);
4648 }
4649 \f
4650 /* Call here when we see the initializer is surrounded by braces.
4651 This is instead of a call to push_init_level;
4652 it is matched by a call to pop_init_level.
4653
4654 TYPE is the type to initialize, for a constructor expression.
4655 For an initializer for a decl, TYPE is zero. */
4656
4657 void
4658 really_start_incremental_init (tree type)
4659 {
4660 struct constructor_stack *p = XNEW (struct constructor_stack);
4661
4662 if (type == 0)
4663 type = TREE_TYPE (constructor_decl);
4664
4665 if (targetm.vector_opaque_p (type))
4666 error ("opaque vector types cannot be initialized");
4667
4668 p->type = constructor_type;
4669 p->fields = constructor_fields;
4670 p->index = constructor_index;
4671 p->max_index = constructor_max_index;
4672 p->unfilled_index = constructor_unfilled_index;
4673 p->unfilled_fields = constructor_unfilled_fields;
4674 p->bit_index = constructor_bit_index;
4675 p->elements = constructor_elements;
4676 p->constant = constructor_constant;
4677 p->simple = constructor_simple;
4678 p->erroneous = constructor_erroneous;
4679 p->pending_elts = constructor_pending_elts;
4680 p->depth = constructor_depth;
4681 p->replacement_value.value = 0;
4682 p->replacement_value.original_code = ERROR_MARK;
4683 p->implicit = 0;
4684 p->range_stack = 0;
4685 p->outer = 0;
4686 p->incremental = constructor_incremental;
4687 p->designated = constructor_designated;
4688 p->next = 0;
4689 constructor_stack = p;
4690
4691 constructor_constant = 1;
4692 constructor_simple = 1;
4693 constructor_depth = SPELLING_DEPTH ();
4694 constructor_elements = 0;
4695 constructor_pending_elts = 0;
4696 constructor_type = type;
4697 constructor_incremental = 1;
4698 constructor_designated = 0;
4699 designator_depth = 0;
4700 designator_errorneous = 0;
4701
4702 if (TREE_CODE (constructor_type) == RECORD_TYPE
4703 || TREE_CODE (constructor_type) == UNION_TYPE)
4704 {
4705 constructor_fields = TYPE_FIELDS (constructor_type);
4706 /* Skip any nameless bit fields at the beginning. */
4707 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4708 && DECL_NAME (constructor_fields) == 0)
4709 constructor_fields = TREE_CHAIN (constructor_fields);
4710
4711 constructor_unfilled_fields = constructor_fields;
4712 constructor_bit_index = bitsize_zero_node;
4713 }
4714 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4715 {
4716 if (TYPE_DOMAIN (constructor_type))
4717 {
4718 constructor_max_index
4719 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4720
4721 /* Detect non-empty initializations of zero-length arrays. */
4722 if (constructor_max_index == NULL_TREE
4723 && TYPE_SIZE (constructor_type))
4724 constructor_max_index = build_int_cst (NULL_TREE, -1);
4725
4726 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4727 to initialize VLAs will cause a proper error; avoid tree
4728 checking errors as well by setting a safe value. */
4729 if (constructor_max_index
4730 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4731 constructor_max_index = build_int_cst (NULL_TREE, -1);
4732
4733 constructor_index
4734 = convert (bitsizetype,
4735 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4736 }
4737 else
4738 {
4739 constructor_index = bitsize_zero_node;
4740 constructor_max_index = NULL_TREE;
4741 }
4742
4743 constructor_unfilled_index = constructor_index;
4744 }
4745 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4746 {
4747 /* Vectors are like simple fixed-size arrays. */
4748 constructor_max_index =
4749 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4750 constructor_index = convert (bitsizetype, bitsize_zero_node);
4751 constructor_unfilled_index = constructor_index;
4752 }
4753 else
4754 {
4755 /* Handle the case of int x = {5}; */
4756 constructor_fields = constructor_type;
4757 constructor_unfilled_fields = constructor_type;
4758 }
4759 }
4760 \f
4761 /* Push down into a subobject, for initialization.
4762 If this is for an explicit set of braces, IMPLICIT is 0.
4763 If it is because the next element belongs at a lower level,
4764 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4765
4766 void
4767 push_init_level (int implicit)
4768 {
4769 struct constructor_stack *p;
4770 tree value = NULL_TREE;
4771
4772 /* If we've exhausted any levels that didn't have braces,
4773 pop them now. */
4774 while (constructor_stack->implicit)
4775 {
4776 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4777 || TREE_CODE (constructor_type) == UNION_TYPE)
4778 && constructor_fields == 0)
4779 process_init_element (pop_init_level (1));
4780 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4781 && constructor_max_index
4782 && tree_int_cst_lt (constructor_max_index, constructor_index))
4783 process_init_element (pop_init_level (1));
4784 else
4785 break;
4786 }
4787
4788 /* Unless this is an explicit brace, we need to preserve previous
4789 content if any. */
4790 if (implicit)
4791 {
4792 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4793 || TREE_CODE (constructor_type) == UNION_TYPE)
4794 && constructor_fields)
4795 value = find_init_member (constructor_fields);
4796 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4797 value = find_init_member (constructor_index);
4798 }
4799
4800 p = XNEW (struct constructor_stack);
4801 p->type = constructor_type;
4802 p->fields = constructor_fields;
4803 p->index = constructor_index;
4804 p->max_index = constructor_max_index;
4805 p->unfilled_index = constructor_unfilled_index;
4806 p->unfilled_fields = constructor_unfilled_fields;
4807 p->bit_index = constructor_bit_index;
4808 p->elements = constructor_elements;
4809 p->constant = constructor_constant;
4810 p->simple = constructor_simple;
4811 p->erroneous = constructor_erroneous;
4812 p->pending_elts = constructor_pending_elts;
4813 p->depth = constructor_depth;
4814 p->replacement_value.value = 0;
4815 p->replacement_value.original_code = ERROR_MARK;
4816 p->implicit = implicit;
4817 p->outer = 0;
4818 p->incremental = constructor_incremental;
4819 p->designated = constructor_designated;
4820 p->next = constructor_stack;
4821 p->range_stack = 0;
4822 constructor_stack = p;
4823
4824 constructor_constant = 1;
4825 constructor_simple = 1;
4826 constructor_depth = SPELLING_DEPTH ();
4827 constructor_elements = 0;
4828 constructor_incremental = 1;
4829 constructor_designated = 0;
4830 constructor_pending_elts = 0;
4831 if (!implicit)
4832 {
4833 p->range_stack = constructor_range_stack;
4834 constructor_range_stack = 0;
4835 designator_depth = 0;
4836 designator_errorneous = 0;
4837 }
4838
4839 /* Don't die if an entire brace-pair level is superfluous
4840 in the containing level. */
4841 if (constructor_type == 0)
4842 ;
4843 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4844 || TREE_CODE (constructor_type) == UNION_TYPE)
4845 {
4846 /* Don't die if there are extra init elts at the end. */
4847 if (constructor_fields == 0)
4848 constructor_type = 0;
4849 else
4850 {
4851 constructor_type = TREE_TYPE (constructor_fields);
4852 push_member_name (constructor_fields);
4853 constructor_depth++;
4854 }
4855 }
4856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4857 {
4858 constructor_type = TREE_TYPE (constructor_type);
4859 push_array_bounds (tree_low_cst (constructor_index, 0));
4860 constructor_depth++;
4861 }
4862
4863 if (constructor_type == 0)
4864 {
4865 error_init ("extra brace group at end of initializer");
4866 constructor_fields = 0;
4867 constructor_unfilled_fields = 0;
4868 return;
4869 }
4870
4871 if (value && TREE_CODE (value) == CONSTRUCTOR)
4872 {
4873 constructor_constant = TREE_CONSTANT (value);
4874 constructor_simple = TREE_STATIC (value);
4875 constructor_elements = CONSTRUCTOR_ELTS (value);
4876 if (constructor_elements
4877 && (TREE_CODE (constructor_type) == RECORD_TYPE
4878 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4879 set_nonincremental_init ();
4880 }
4881
4882 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4883 {
4884 missing_braces_mentioned = 1;
4885 warning_init ("missing braces around initializer");
4886 }
4887
4888 if (TREE_CODE (constructor_type) == RECORD_TYPE
4889 || TREE_CODE (constructor_type) == UNION_TYPE)
4890 {
4891 constructor_fields = TYPE_FIELDS (constructor_type);
4892 /* Skip any nameless bit fields at the beginning. */
4893 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4894 && DECL_NAME (constructor_fields) == 0)
4895 constructor_fields = TREE_CHAIN (constructor_fields);
4896
4897 constructor_unfilled_fields = constructor_fields;
4898 constructor_bit_index = bitsize_zero_node;
4899 }
4900 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4901 {
4902 /* Vectors are like simple fixed-size arrays. */
4903 constructor_max_index =
4904 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4905 constructor_index = convert (bitsizetype, integer_zero_node);
4906 constructor_unfilled_index = constructor_index;
4907 }
4908 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4909 {
4910 if (TYPE_DOMAIN (constructor_type))
4911 {
4912 constructor_max_index
4913 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4914
4915 /* Detect non-empty initializations of zero-length arrays. */
4916 if (constructor_max_index == NULL_TREE
4917 && TYPE_SIZE (constructor_type))
4918 constructor_max_index = build_int_cst (NULL_TREE, -1);
4919
4920 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4921 to initialize VLAs will cause a proper error; avoid tree
4922 checking errors as well by setting a safe value. */
4923 if (constructor_max_index
4924 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4925 constructor_max_index = build_int_cst (NULL_TREE, -1);
4926
4927 constructor_index
4928 = convert (bitsizetype,
4929 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4930 }
4931 else
4932 constructor_index = bitsize_zero_node;
4933
4934 constructor_unfilled_index = constructor_index;
4935 if (value && TREE_CODE (value) == STRING_CST)
4936 {
4937 /* We need to split the char/wchar array into individual
4938 characters, so that we don't have to special case it
4939 everywhere. */
4940 set_nonincremental_init_from_string (value);
4941 }
4942 }
4943 else
4944 {
4945 if (constructor_type != error_mark_node)
4946 warning_init ("braces around scalar initializer");
4947 constructor_fields = constructor_type;
4948 constructor_unfilled_fields = constructor_type;
4949 }
4950 }
4951
4952 /* At the end of an implicit or explicit brace level,
4953 finish up that level of constructor. If a single expression
4954 with redundant braces initialized that level, return the
4955 c_expr structure for that expression. Otherwise, the original_code
4956 element is set to ERROR_MARK.
4957 If we were outputting the elements as they are read, return 0 as the value
4958 from inner levels (process_init_element ignores that),
4959 but return error_mark_node as the value from the outermost level
4960 (that's what we want to put in DECL_INITIAL).
4961 Otherwise, return a CONSTRUCTOR expression as the value. */
4962
4963 struct c_expr
4964 pop_init_level (int implicit)
4965 {
4966 struct constructor_stack *p;
4967 struct c_expr ret;
4968 ret.value = 0;
4969 ret.original_code = ERROR_MARK;
4970
4971 if (implicit == 0)
4972 {
4973 /* When we come to an explicit close brace,
4974 pop any inner levels that didn't have explicit braces. */
4975 while (constructor_stack->implicit)
4976 process_init_element (pop_init_level (1));
4977
4978 gcc_assert (!constructor_range_stack);
4979 }
4980
4981 /* Now output all pending elements. */
4982 constructor_incremental = 1;
4983 output_pending_init_elements (1);
4984
4985 p = constructor_stack;
4986
4987 /* Error for initializing a flexible array member, or a zero-length
4988 array member in an inappropriate context. */
4989 if (constructor_type && constructor_fields
4990 && TREE_CODE (constructor_type) == ARRAY_TYPE
4991 && TYPE_DOMAIN (constructor_type)
4992 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4993 {
4994 /* Silently discard empty initializations. The parser will
4995 already have pedwarned for empty brackets. */
4996 if (integer_zerop (constructor_unfilled_index))
4997 constructor_type = NULL_TREE;
4998 else
4999 {
5000 gcc_assert (!TYPE_SIZE (constructor_type));
5001
5002 if (constructor_depth > 2)
5003 error_init ("initialization of flexible array member in a nested context");
5004 else if (pedantic)
5005 pedwarn_init ("initialization of a flexible array member");
5006
5007 /* We have already issued an error message for the existence
5008 of a flexible array member not at the end of the structure.
5009 Discard the initializer so that we do not abort later. */
5010 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5011 constructor_type = NULL_TREE;
5012 }
5013 }
5014
5015 /* Warn when some struct elements are implicitly initialized to zero. */
5016 if (warn_missing_field_initializers
5017 && constructor_type
5018 && TREE_CODE (constructor_type) == RECORD_TYPE
5019 && constructor_unfilled_fields)
5020 {
5021 /* Do not warn for flexible array members or zero-length arrays. */
5022 while (constructor_unfilled_fields
5023 && (!DECL_SIZE (constructor_unfilled_fields)
5024 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5025 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5026
5027 /* Do not warn if this level of the initializer uses member
5028 designators; it is likely to be deliberate. */
5029 if (constructor_unfilled_fields && !constructor_designated)
5030 {
5031 push_member_name (constructor_unfilled_fields);
5032 warning_init ("missing initializer");
5033 RESTORE_SPELLING_DEPTH (constructor_depth);
5034 }
5035 }
5036
5037 /* Pad out the end of the structure. */
5038 if (p->replacement_value.value)
5039 /* If this closes a superfluous brace pair,
5040 just pass out the element between them. */
5041 ret = p->replacement_value;
5042 else if (constructor_type == 0)
5043 ;
5044 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5045 && TREE_CODE (constructor_type) != UNION_TYPE
5046 && TREE_CODE (constructor_type) != ARRAY_TYPE
5047 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5048 {
5049 /* A nonincremental scalar initializer--just return
5050 the element, after verifying there is just one. */
5051 if (constructor_elements == 0)
5052 {
5053 if (!constructor_erroneous)
5054 error_init ("empty scalar initializer");
5055 ret.value = error_mark_node;
5056 }
5057 else if (TREE_CHAIN (constructor_elements) != 0)
5058 {
5059 error_init ("extra elements in scalar initializer");
5060 ret.value = TREE_VALUE (constructor_elements);
5061 }
5062 else
5063 ret.value = TREE_VALUE (constructor_elements);
5064 }
5065 else
5066 {
5067 if (constructor_erroneous)
5068 ret.value = error_mark_node;
5069 else
5070 {
5071 ret.value = build_constructor (constructor_type,
5072 nreverse (constructor_elements));
5073 if (constructor_constant)
5074 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5075 if (constructor_constant && constructor_simple)
5076 TREE_STATIC (ret.value) = 1;
5077 }
5078 }
5079
5080 constructor_type = p->type;
5081 constructor_fields = p->fields;
5082 constructor_index = p->index;
5083 constructor_max_index = p->max_index;
5084 constructor_unfilled_index = p->unfilled_index;
5085 constructor_unfilled_fields = p->unfilled_fields;
5086 constructor_bit_index = p->bit_index;
5087 constructor_elements = p->elements;
5088 constructor_constant = p->constant;
5089 constructor_simple = p->simple;
5090 constructor_erroneous = p->erroneous;
5091 constructor_incremental = p->incremental;
5092 constructor_designated = p->designated;
5093 constructor_pending_elts = p->pending_elts;
5094 constructor_depth = p->depth;
5095 if (!p->implicit)
5096 constructor_range_stack = p->range_stack;
5097 RESTORE_SPELLING_DEPTH (constructor_depth);
5098
5099 constructor_stack = p->next;
5100 free (p);
5101
5102 if (ret.value == 0)
5103 {
5104 if (constructor_stack == 0)
5105 {
5106 ret.value = error_mark_node;
5107 return ret;
5108 }
5109 return ret;
5110 }
5111 return ret;
5112 }
5113
5114 /* Common handling for both array range and field name designators.
5115 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5116
5117 static int
5118 set_designator (int array)
5119 {
5120 tree subtype;
5121 enum tree_code subcode;
5122
5123 /* Don't die if an entire brace-pair level is superfluous
5124 in the containing level. */
5125 if (constructor_type == 0)
5126 return 1;
5127
5128 /* If there were errors in this designator list already, bail out
5129 silently. */
5130 if (designator_errorneous)
5131 return 1;
5132
5133 if (!designator_depth)
5134 {
5135 gcc_assert (!constructor_range_stack);
5136
5137 /* Designator list starts at the level of closest explicit
5138 braces. */
5139 while (constructor_stack->implicit)
5140 process_init_element (pop_init_level (1));
5141 constructor_designated = 1;
5142 return 0;
5143 }
5144
5145 switch (TREE_CODE (constructor_type))
5146 {
5147 case RECORD_TYPE:
5148 case UNION_TYPE:
5149 subtype = TREE_TYPE (constructor_fields);
5150 if (subtype != error_mark_node)
5151 subtype = TYPE_MAIN_VARIANT (subtype);
5152 break;
5153 case ARRAY_TYPE:
5154 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5155 break;
5156 default:
5157 gcc_unreachable ();
5158 }
5159
5160 subcode = TREE_CODE (subtype);
5161 if (array && subcode != ARRAY_TYPE)
5162 {
5163 error_init ("array index in non-array initializer");
5164 return 1;
5165 }
5166 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5167 {
5168 error_init ("field name not in record or union initializer");
5169 return 1;
5170 }
5171
5172 constructor_designated = 1;
5173 push_init_level (2);
5174 return 0;
5175 }
5176
5177 /* If there are range designators in designator list, push a new designator
5178 to constructor_range_stack. RANGE_END is end of such stack range or
5179 NULL_TREE if there is no range designator at this level. */
5180
5181 static void
5182 push_range_stack (tree range_end)
5183 {
5184 struct constructor_range_stack *p;
5185
5186 p = GGC_NEW (struct constructor_range_stack);
5187 p->prev = constructor_range_stack;
5188 p->next = 0;
5189 p->fields = constructor_fields;
5190 p->range_start = constructor_index;
5191 p->index = constructor_index;
5192 p->stack = constructor_stack;
5193 p->range_end = range_end;
5194 if (constructor_range_stack)
5195 constructor_range_stack->next = p;
5196 constructor_range_stack = p;
5197 }
5198
5199 /* Within an array initializer, specify the next index to be initialized.
5200 FIRST is that index. If LAST is nonzero, then initialize a range
5201 of indices, running from FIRST through LAST. */
5202
5203 void
5204 set_init_index (tree first, tree last)
5205 {
5206 if (set_designator (1))
5207 return;
5208
5209 designator_errorneous = 1;
5210
5211 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5212 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5213 {
5214 error_init ("array index in initializer not of integer type");
5215 return;
5216 }
5217
5218 if (TREE_CODE (first) != INTEGER_CST)
5219 error_init ("nonconstant array index in initializer");
5220 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5221 error_init ("nonconstant array index in initializer");
5222 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5223 error_init ("array index in non-array initializer");
5224 else if (tree_int_cst_sgn (first) == -1)
5225 error_init ("array index in initializer exceeds array bounds");
5226 else if (constructor_max_index
5227 && tree_int_cst_lt (constructor_max_index, first))
5228 error_init ("array index in initializer exceeds array bounds");
5229 else
5230 {
5231 constructor_index = convert (bitsizetype, first);
5232
5233 if (last)
5234 {
5235 if (tree_int_cst_equal (first, last))
5236 last = 0;
5237 else if (tree_int_cst_lt (last, first))
5238 {
5239 error_init ("empty index range in initializer");
5240 last = 0;
5241 }
5242 else
5243 {
5244 last = convert (bitsizetype, last);
5245 if (constructor_max_index != 0
5246 && tree_int_cst_lt (constructor_max_index, last))
5247 {
5248 error_init ("array index range in initializer exceeds array bounds");
5249 last = 0;
5250 }
5251 }
5252 }
5253
5254 designator_depth++;
5255 designator_errorneous = 0;
5256 if (constructor_range_stack || last)
5257 push_range_stack (last);
5258 }
5259 }
5260
5261 /* Within a struct initializer, specify the next field to be initialized. */
5262
5263 void
5264 set_init_label (tree fieldname)
5265 {
5266 tree tail;
5267
5268 if (set_designator (0))
5269 return;
5270
5271 designator_errorneous = 1;
5272
5273 if (TREE_CODE (constructor_type) != RECORD_TYPE
5274 && TREE_CODE (constructor_type) != UNION_TYPE)
5275 {
5276 error_init ("field name not in record or union initializer");
5277 return;
5278 }
5279
5280 for (tail = TYPE_FIELDS (constructor_type); tail;
5281 tail = TREE_CHAIN (tail))
5282 {
5283 if (DECL_NAME (tail) == fieldname)
5284 break;
5285 }
5286
5287 if (tail == 0)
5288 error ("unknown field %qE specified in initializer", fieldname);
5289 else
5290 {
5291 constructor_fields = tail;
5292 designator_depth++;
5293 designator_errorneous = 0;
5294 if (constructor_range_stack)
5295 push_range_stack (NULL_TREE);
5296 }
5297 }
5298 \f
5299 /* Add a new initializer to the tree of pending initializers. PURPOSE
5300 identifies the initializer, either array index or field in a structure.
5301 VALUE is the value of that index or field. */
5302
5303 static void
5304 add_pending_init (tree purpose, tree value)
5305 {
5306 struct init_node *p, **q, *r;
5307
5308 q = &constructor_pending_elts;
5309 p = 0;
5310
5311 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5312 {
5313 while (*q != 0)
5314 {
5315 p = *q;
5316 if (tree_int_cst_lt (purpose, p->purpose))
5317 q = &p->left;
5318 else if (tree_int_cst_lt (p->purpose, purpose))
5319 q = &p->right;
5320 else
5321 {
5322 if (TREE_SIDE_EFFECTS (p->value))
5323 warning_init ("initialized field with side-effects overwritten");
5324 p->value = value;
5325 return;
5326 }
5327 }
5328 }
5329 else
5330 {
5331 tree bitpos;
5332
5333 bitpos = bit_position (purpose);
5334 while (*q != NULL)
5335 {
5336 p = *q;
5337 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5338 q = &p->left;
5339 else if (p->purpose != purpose)
5340 q = &p->right;
5341 else
5342 {
5343 if (TREE_SIDE_EFFECTS (p->value))
5344 warning_init ("initialized field with side-effects overwritten");
5345 p->value = value;
5346 return;
5347 }
5348 }
5349 }
5350
5351 r = GGC_NEW (struct init_node);
5352 r->purpose = purpose;
5353 r->value = value;
5354
5355 *q = r;
5356 r->parent = p;
5357 r->left = 0;
5358 r->right = 0;
5359 r->balance = 0;
5360
5361 while (p)
5362 {
5363 struct init_node *s;
5364
5365 if (r == p->left)
5366 {
5367 if (p->balance == 0)
5368 p->balance = -1;
5369 else if (p->balance < 0)
5370 {
5371 if (r->balance < 0)
5372 {
5373 /* L rotation. */
5374 p->left = r->right;
5375 if (p->left)
5376 p->left->parent = p;
5377 r->right = p;
5378
5379 p->balance = 0;
5380 r->balance = 0;
5381
5382 s = p->parent;
5383 p->parent = r;
5384 r->parent = s;
5385 if (s)
5386 {
5387 if (s->left == p)
5388 s->left = r;
5389 else
5390 s->right = r;
5391 }
5392 else
5393 constructor_pending_elts = r;
5394 }
5395 else
5396 {
5397 /* LR rotation. */
5398 struct init_node *t = r->right;
5399
5400 r->right = t->left;
5401 if (r->right)
5402 r->right->parent = r;
5403 t->left = r;
5404
5405 p->left = t->right;
5406 if (p->left)
5407 p->left->parent = p;
5408 t->right = p;
5409
5410 p->balance = t->balance < 0;
5411 r->balance = -(t->balance > 0);
5412 t->balance = 0;
5413
5414 s = p->parent;
5415 p->parent = t;
5416 r->parent = t;
5417 t->parent = s;
5418 if (s)
5419 {
5420 if (s->left == p)
5421 s->left = t;
5422 else
5423 s->right = t;
5424 }
5425 else
5426 constructor_pending_elts = t;
5427 }
5428 break;
5429 }
5430 else
5431 {
5432 /* p->balance == +1; growth of left side balances the node. */
5433 p->balance = 0;
5434 break;
5435 }
5436 }
5437 else /* r == p->right */
5438 {
5439 if (p->balance == 0)
5440 /* Growth propagation from right side. */
5441 p->balance++;
5442 else if (p->balance > 0)
5443 {
5444 if (r->balance > 0)
5445 {
5446 /* R rotation. */
5447 p->right = r->left;
5448 if (p->right)
5449 p->right->parent = p;
5450 r->left = p;
5451
5452 p->balance = 0;
5453 r->balance = 0;
5454
5455 s = p->parent;
5456 p->parent = r;
5457 r->parent = s;
5458 if (s)
5459 {
5460 if (s->left == p)
5461 s->left = r;
5462 else
5463 s->right = r;
5464 }
5465 else
5466 constructor_pending_elts = r;
5467 }
5468 else /* r->balance == -1 */
5469 {
5470 /* RL rotation */
5471 struct init_node *t = r->left;
5472
5473 r->left = t->right;
5474 if (r->left)
5475 r->left->parent = r;
5476 t->right = r;
5477
5478 p->right = t->left;
5479 if (p->right)
5480 p->right->parent = p;
5481 t->left = p;
5482
5483 r->balance = (t->balance < 0);
5484 p->balance = -(t->balance > 0);
5485 t->balance = 0;
5486
5487 s = p->parent;
5488 p->parent = t;
5489 r->parent = t;
5490 t->parent = s;
5491 if (s)
5492 {
5493 if (s->left == p)
5494 s->left = t;
5495 else
5496 s->right = t;
5497 }
5498 else
5499 constructor_pending_elts = t;
5500 }
5501 break;
5502 }
5503 else
5504 {
5505 /* p->balance == -1; growth of right side balances the node. */
5506 p->balance = 0;
5507 break;
5508 }
5509 }
5510
5511 r = p;
5512 p = p->parent;
5513 }
5514 }
5515
5516 /* Build AVL tree from a sorted chain. */
5517
5518 static void
5519 set_nonincremental_init (void)
5520 {
5521 tree chain;
5522
5523 if (TREE_CODE (constructor_type) != RECORD_TYPE
5524 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5525 return;
5526
5527 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5528 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5529 constructor_elements = 0;
5530 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5531 {
5532 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5533 /* Skip any nameless bit fields at the beginning. */
5534 while (constructor_unfilled_fields != 0
5535 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5536 && DECL_NAME (constructor_unfilled_fields) == 0)
5537 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5538
5539 }
5540 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5541 {
5542 if (TYPE_DOMAIN (constructor_type))
5543 constructor_unfilled_index
5544 = convert (bitsizetype,
5545 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5546 else
5547 constructor_unfilled_index = bitsize_zero_node;
5548 }
5549 constructor_incremental = 0;
5550 }
5551
5552 /* Build AVL tree from a string constant. */
5553
5554 static void
5555 set_nonincremental_init_from_string (tree str)
5556 {
5557 tree value, purpose, type;
5558 HOST_WIDE_INT val[2];
5559 const char *p, *end;
5560 int byte, wchar_bytes, charwidth, bitpos;
5561
5562 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5563
5564 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5565 == TYPE_PRECISION (char_type_node))
5566 wchar_bytes = 1;
5567 else
5568 {
5569 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5570 == TYPE_PRECISION (wchar_type_node));
5571 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5572 }
5573 charwidth = TYPE_PRECISION (char_type_node);
5574 type = TREE_TYPE (constructor_type);
5575 p = TREE_STRING_POINTER (str);
5576 end = p + TREE_STRING_LENGTH (str);
5577
5578 for (purpose = bitsize_zero_node;
5579 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5580 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5581 {
5582 if (wchar_bytes == 1)
5583 {
5584 val[1] = (unsigned char) *p++;
5585 val[0] = 0;
5586 }
5587 else
5588 {
5589 val[0] = 0;
5590 val[1] = 0;
5591 for (byte = 0; byte < wchar_bytes; byte++)
5592 {
5593 if (BYTES_BIG_ENDIAN)
5594 bitpos = (wchar_bytes - byte - 1) * charwidth;
5595 else
5596 bitpos = byte * charwidth;
5597 val[bitpos < HOST_BITS_PER_WIDE_INT]
5598 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5599 << (bitpos % HOST_BITS_PER_WIDE_INT);
5600 }
5601 }
5602
5603 if (!TYPE_UNSIGNED (type))
5604 {
5605 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5606 if (bitpos < HOST_BITS_PER_WIDE_INT)
5607 {
5608 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5609 {
5610 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5611 val[0] = -1;
5612 }
5613 }
5614 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5615 {
5616 if (val[1] < 0)
5617 val[0] = -1;
5618 }
5619 else if (val[0] & (((HOST_WIDE_INT) 1)
5620 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5621 val[0] |= ((HOST_WIDE_INT) -1)
5622 << (bitpos - HOST_BITS_PER_WIDE_INT);
5623 }
5624
5625 value = build_int_cst_wide (type, val[1], val[0]);
5626 add_pending_init (purpose, value);
5627 }
5628
5629 constructor_incremental = 0;
5630 }
5631
5632 /* Return value of FIELD in pending initializer or zero if the field was
5633 not initialized yet. */
5634
5635 static tree
5636 find_init_member (tree field)
5637 {
5638 struct init_node *p;
5639
5640 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5641 {
5642 if (constructor_incremental
5643 && tree_int_cst_lt (field, constructor_unfilled_index))
5644 set_nonincremental_init ();
5645
5646 p = constructor_pending_elts;
5647 while (p)
5648 {
5649 if (tree_int_cst_lt (field, p->purpose))
5650 p = p->left;
5651 else if (tree_int_cst_lt (p->purpose, field))
5652 p = p->right;
5653 else
5654 return p->value;
5655 }
5656 }
5657 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5658 {
5659 tree bitpos = bit_position (field);
5660
5661 if (constructor_incremental
5662 && (!constructor_unfilled_fields
5663 || tree_int_cst_lt (bitpos,
5664 bit_position (constructor_unfilled_fields))))
5665 set_nonincremental_init ();
5666
5667 p = constructor_pending_elts;
5668 while (p)
5669 {
5670 if (field == p->purpose)
5671 return p->value;
5672 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5673 p = p->left;
5674 else
5675 p = p->right;
5676 }
5677 }
5678 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5679 {
5680 if (constructor_elements
5681 && TREE_PURPOSE (constructor_elements) == field)
5682 return TREE_VALUE (constructor_elements);
5683 }
5684 return 0;
5685 }
5686
5687 /* "Output" the next constructor element.
5688 At top level, really output it to assembler code now.
5689 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5690 TYPE is the data type that the containing data type wants here.
5691 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5692 If VALUE is a string constant, STRICT_STRING is true if it is
5693 unparenthesized or we should not warn here for it being parenthesized.
5694 For other types of VALUE, STRICT_STRING is not used.
5695
5696 PENDING if non-nil means output pending elements that belong
5697 right after this element. (PENDING is normally 1;
5698 it is 0 while outputting pending elements, to avoid recursion.) */
5699
5700 static void
5701 output_init_element (tree value, bool strict_string, tree type, tree field,
5702 int pending)
5703 {
5704 if (type == error_mark_node || value == error_mark_node)
5705 {
5706 constructor_erroneous = 1;
5707 return;
5708 }
5709 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5710 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5711 && !(TREE_CODE (value) == STRING_CST
5712 && TREE_CODE (type) == ARRAY_TYPE
5713 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5714 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5715 TYPE_MAIN_VARIANT (type))))
5716 value = default_conversion (value);
5717
5718 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5719 && require_constant_value && !flag_isoc99 && pending)
5720 {
5721 /* As an extension, allow initializing objects with static storage
5722 duration with compound literals (which are then treated just as
5723 the brace enclosed list they contain). */
5724 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5725 value = DECL_INITIAL (decl);
5726 }
5727
5728 if (value == error_mark_node)
5729 constructor_erroneous = 1;
5730 else if (!TREE_CONSTANT (value))
5731 constructor_constant = 0;
5732 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5733 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5734 || TREE_CODE (constructor_type) == UNION_TYPE)
5735 && DECL_C_BIT_FIELD (field)
5736 && TREE_CODE (value) != INTEGER_CST))
5737 constructor_simple = 0;
5738
5739 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5740 {
5741 if (require_constant_value)
5742 {
5743 error_init ("initializer element is not constant");
5744 value = error_mark_node;
5745 }
5746 else if (require_constant_elements)
5747 pedwarn ("initializer element is not computable at load time");
5748 }
5749
5750 /* If this field is empty (and not at the end of structure),
5751 don't do anything other than checking the initializer. */
5752 if (field
5753 && (TREE_TYPE (field) == error_mark_node
5754 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5755 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5756 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5757 || TREE_CHAIN (field)))))
5758 return;
5759
5760 value = digest_init (type, value, strict_string, require_constant_value);
5761 if (value == error_mark_node)
5762 {
5763 constructor_erroneous = 1;
5764 return;
5765 }
5766
5767 /* If this element doesn't come next in sequence,
5768 put it on constructor_pending_elts. */
5769 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5770 && (!constructor_incremental
5771 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5772 {
5773 if (constructor_incremental
5774 && tree_int_cst_lt (field, constructor_unfilled_index))
5775 set_nonincremental_init ();
5776
5777 add_pending_init (field, value);
5778 return;
5779 }
5780 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5781 && (!constructor_incremental
5782 || field != constructor_unfilled_fields))
5783 {
5784 /* We do this for records but not for unions. In a union,
5785 no matter which field is specified, it can be initialized
5786 right away since it starts at the beginning of the union. */
5787 if (constructor_incremental)
5788 {
5789 if (!constructor_unfilled_fields)
5790 set_nonincremental_init ();
5791 else
5792 {
5793 tree bitpos, unfillpos;
5794
5795 bitpos = bit_position (field);
5796 unfillpos = bit_position (constructor_unfilled_fields);
5797
5798 if (tree_int_cst_lt (bitpos, unfillpos))
5799 set_nonincremental_init ();
5800 }
5801 }
5802
5803 add_pending_init (field, value);
5804 return;
5805 }
5806 else if (TREE_CODE (constructor_type) == UNION_TYPE
5807 && constructor_elements)
5808 {
5809 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5810 warning_init ("initialized field with side-effects overwritten");
5811
5812 /* We can have just one union field set. */
5813 constructor_elements = 0;
5814 }
5815
5816 /* Otherwise, output this element either to
5817 constructor_elements or to the assembler file. */
5818
5819 if (field && TREE_CODE (field) == INTEGER_CST)
5820 field = copy_node (field);
5821 constructor_elements
5822 = tree_cons (field, value, constructor_elements);
5823
5824 /* Advance the variable that indicates sequential elements output. */
5825 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5826 constructor_unfilled_index
5827 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5828 bitsize_one_node);
5829 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5830 {
5831 constructor_unfilled_fields
5832 = TREE_CHAIN (constructor_unfilled_fields);
5833
5834 /* Skip any nameless bit fields. */
5835 while (constructor_unfilled_fields != 0
5836 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5837 && DECL_NAME (constructor_unfilled_fields) == 0)
5838 constructor_unfilled_fields =
5839 TREE_CHAIN (constructor_unfilled_fields);
5840 }
5841 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5842 constructor_unfilled_fields = 0;
5843
5844 /* Now output any pending elements which have become next. */
5845 if (pending)
5846 output_pending_init_elements (0);
5847 }
5848
5849 /* Output any pending elements which have become next.
5850 As we output elements, constructor_unfilled_{fields,index}
5851 advances, which may cause other elements to become next;
5852 if so, they too are output.
5853
5854 If ALL is 0, we return when there are
5855 no more pending elements to output now.
5856
5857 If ALL is 1, we output space as necessary so that
5858 we can output all the pending elements. */
5859
5860 static void
5861 output_pending_init_elements (int all)
5862 {
5863 struct init_node *elt = constructor_pending_elts;
5864 tree next;
5865
5866 retry:
5867
5868 /* Look through the whole pending tree.
5869 If we find an element that should be output now,
5870 output it. Otherwise, set NEXT to the element
5871 that comes first among those still pending. */
5872
5873 next = 0;
5874 while (elt)
5875 {
5876 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5877 {
5878 if (tree_int_cst_equal (elt->purpose,
5879 constructor_unfilled_index))
5880 output_init_element (elt->value, true,
5881 TREE_TYPE (constructor_type),
5882 constructor_unfilled_index, 0);
5883 else if (tree_int_cst_lt (constructor_unfilled_index,
5884 elt->purpose))
5885 {
5886 /* Advance to the next smaller node. */
5887 if (elt->left)
5888 elt = elt->left;
5889 else
5890 {
5891 /* We have reached the smallest node bigger than the
5892 current unfilled index. Fill the space first. */
5893 next = elt->purpose;
5894 break;
5895 }
5896 }
5897 else
5898 {
5899 /* Advance to the next bigger node. */
5900 if (elt->right)
5901 elt = elt->right;
5902 else
5903 {
5904 /* We have reached the biggest node in a subtree. Find
5905 the parent of it, which is the next bigger node. */
5906 while (elt->parent && elt->parent->right == elt)
5907 elt = elt->parent;
5908 elt = elt->parent;
5909 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5910 elt->purpose))
5911 {
5912 next = elt->purpose;
5913 break;
5914 }
5915 }
5916 }
5917 }
5918 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5919 || TREE_CODE (constructor_type) == UNION_TYPE)
5920 {
5921 tree ctor_unfilled_bitpos, elt_bitpos;
5922
5923 /* If the current record is complete we are done. */
5924 if (constructor_unfilled_fields == 0)
5925 break;
5926
5927 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5928 elt_bitpos = bit_position (elt->purpose);
5929 /* We can't compare fields here because there might be empty
5930 fields in between. */
5931 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5932 {
5933 constructor_unfilled_fields = elt->purpose;
5934 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5935 elt->purpose, 0);
5936 }
5937 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5938 {
5939 /* Advance to the next smaller node. */
5940 if (elt->left)
5941 elt = elt->left;
5942 else
5943 {
5944 /* We have reached the smallest node bigger than the
5945 current unfilled field. Fill the space first. */
5946 next = elt->purpose;
5947 break;
5948 }
5949 }
5950 else
5951 {
5952 /* Advance to the next bigger node. */
5953 if (elt->right)
5954 elt = elt->right;
5955 else
5956 {
5957 /* We have reached the biggest node in a subtree. Find
5958 the parent of it, which is the next bigger node. */
5959 while (elt->parent && elt->parent->right == elt)
5960 elt = elt->parent;
5961 elt = elt->parent;
5962 if (elt
5963 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5964 bit_position (elt->purpose))))
5965 {
5966 next = elt->purpose;
5967 break;
5968 }
5969 }
5970 }
5971 }
5972 }
5973
5974 /* Ordinarily return, but not if we want to output all
5975 and there are elements left. */
5976 if (!(all && next != 0))
5977 return;
5978
5979 /* If it's not incremental, just skip over the gap, so that after
5980 jumping to retry we will output the next successive element. */
5981 if (TREE_CODE (constructor_type) == RECORD_TYPE
5982 || TREE_CODE (constructor_type) == UNION_TYPE)
5983 constructor_unfilled_fields = next;
5984 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5985 constructor_unfilled_index = next;
5986
5987 /* ELT now points to the node in the pending tree with the next
5988 initializer to output. */
5989 goto retry;
5990 }
5991 \f
5992 /* Add one non-braced element to the current constructor level.
5993 This adjusts the current position within the constructor's type.
5994 This may also start or terminate implicit levels
5995 to handle a partly-braced initializer.
5996
5997 Once this has found the correct level for the new element,
5998 it calls output_init_element. */
5999
6000 void
6001 process_init_element (struct c_expr value)
6002 {
6003 tree orig_value = value.value;
6004 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6005 bool strict_string = value.original_code == STRING_CST;
6006
6007 designator_depth = 0;
6008 designator_errorneous = 0;
6009
6010 /* Handle superfluous braces around string cst as in
6011 char x[] = {"foo"}; */
6012 if (string_flag
6013 && constructor_type
6014 && TREE_CODE (constructor_type) == ARRAY_TYPE
6015 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6016 && integer_zerop (constructor_unfilled_index))
6017 {
6018 if (constructor_stack->replacement_value.value)
6019 error_init ("excess elements in char array initializer");
6020 constructor_stack->replacement_value = value;
6021 return;
6022 }
6023
6024 if (constructor_stack->replacement_value.value != 0)
6025 {
6026 error_init ("excess elements in struct initializer");
6027 return;
6028 }
6029
6030 /* Ignore elements of a brace group if it is entirely superfluous
6031 and has already been diagnosed. */
6032 if (constructor_type == 0)
6033 return;
6034
6035 /* If we've exhausted any levels that didn't have braces,
6036 pop them now. */
6037 while (constructor_stack->implicit)
6038 {
6039 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6040 || TREE_CODE (constructor_type) == UNION_TYPE)
6041 && constructor_fields == 0)
6042 process_init_element (pop_init_level (1));
6043 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6044 && (constructor_max_index == 0
6045 || tree_int_cst_lt (constructor_max_index,
6046 constructor_index)))
6047 process_init_element (pop_init_level (1));
6048 else
6049 break;
6050 }
6051
6052 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6053 if (constructor_range_stack)
6054 {
6055 /* If value is a compound literal and we'll be just using its
6056 content, don't put it into a SAVE_EXPR. */
6057 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6058 || !require_constant_value
6059 || flag_isoc99)
6060 value.value = save_expr (value.value);
6061 }
6062
6063 while (1)
6064 {
6065 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6066 {
6067 tree fieldtype;
6068 enum tree_code fieldcode;
6069
6070 if (constructor_fields == 0)
6071 {
6072 pedwarn_init ("excess elements in struct initializer");
6073 break;
6074 }
6075
6076 fieldtype = TREE_TYPE (constructor_fields);
6077 if (fieldtype != error_mark_node)
6078 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6079 fieldcode = TREE_CODE (fieldtype);
6080
6081 /* Error for non-static initialization of a flexible array member. */
6082 if (fieldcode == ARRAY_TYPE
6083 && !require_constant_value
6084 && TYPE_SIZE (fieldtype) == NULL_TREE
6085 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6086 {
6087 error_init ("non-static initialization of a flexible array member");
6088 break;
6089 }
6090
6091 /* Accept a string constant to initialize a subarray. */
6092 if (value.value != 0
6093 && fieldcode == ARRAY_TYPE
6094 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6095 && string_flag)
6096 value.value = orig_value;
6097 /* Otherwise, if we have come to a subaggregate,
6098 and we don't have an element of its type, push into it. */
6099 else if (value.value != 0
6100 && value.value != error_mark_node
6101 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6102 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6103 || fieldcode == UNION_TYPE))
6104 {
6105 push_init_level (1);
6106 continue;
6107 }
6108
6109 if (value.value)
6110 {
6111 push_member_name (constructor_fields);
6112 output_init_element (value.value, strict_string,
6113 fieldtype, constructor_fields, 1);
6114 RESTORE_SPELLING_DEPTH (constructor_depth);
6115 }
6116 else
6117 /* Do the bookkeeping for an element that was
6118 directly output as a constructor. */
6119 {
6120 /* For a record, keep track of end position of last field. */
6121 if (DECL_SIZE (constructor_fields))
6122 constructor_bit_index
6123 = size_binop (PLUS_EXPR,
6124 bit_position (constructor_fields),
6125 DECL_SIZE (constructor_fields));
6126
6127 /* If the current field was the first one not yet written out,
6128 it isn't now, so update. */
6129 if (constructor_unfilled_fields == constructor_fields)
6130 {
6131 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6132 /* Skip any nameless bit fields. */
6133 while (constructor_unfilled_fields != 0
6134 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6135 && DECL_NAME (constructor_unfilled_fields) == 0)
6136 constructor_unfilled_fields =
6137 TREE_CHAIN (constructor_unfilled_fields);
6138 }
6139 }
6140
6141 constructor_fields = TREE_CHAIN (constructor_fields);
6142 /* Skip any nameless bit fields at the beginning. */
6143 while (constructor_fields != 0
6144 && DECL_C_BIT_FIELD (constructor_fields)
6145 && DECL_NAME (constructor_fields) == 0)
6146 constructor_fields = TREE_CHAIN (constructor_fields);
6147 }
6148 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6149 {
6150 tree fieldtype;
6151 enum tree_code fieldcode;
6152
6153 if (constructor_fields == 0)
6154 {
6155 pedwarn_init ("excess elements in union initializer");
6156 break;
6157 }
6158
6159 fieldtype = TREE_TYPE (constructor_fields);
6160 if (fieldtype != error_mark_node)
6161 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6162 fieldcode = TREE_CODE (fieldtype);
6163
6164 /* Warn that traditional C rejects initialization of unions.
6165 We skip the warning if the value is zero. This is done
6166 under the assumption that the zero initializer in user
6167 code appears conditioned on e.g. __STDC__ to avoid
6168 "missing initializer" warnings and relies on default
6169 initialization to zero in the traditional C case.
6170 We also skip the warning if the initializer is designated,
6171 again on the assumption that this must be conditional on
6172 __STDC__ anyway (and we've already complained about the
6173 member-designator already). */
6174 if (warn_traditional && !in_system_header && !constructor_designated
6175 && !(value.value && (integer_zerop (value.value)
6176 || real_zerop (value.value))))
6177 warning ("traditional C rejects initialization of unions");
6178
6179 /* Accept a string constant to initialize a subarray. */
6180 if (value.value != 0
6181 && fieldcode == ARRAY_TYPE
6182 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6183 && string_flag)
6184 value.value = orig_value;
6185 /* Otherwise, if we have come to a subaggregate,
6186 and we don't have an element of its type, push into it. */
6187 else if (value.value != 0
6188 && value.value != error_mark_node
6189 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6190 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6191 || fieldcode == UNION_TYPE))
6192 {
6193 push_init_level (1);
6194 continue;
6195 }
6196
6197 if (value.value)
6198 {
6199 push_member_name (constructor_fields);
6200 output_init_element (value.value, strict_string,
6201 fieldtype, constructor_fields, 1);
6202 RESTORE_SPELLING_DEPTH (constructor_depth);
6203 }
6204 else
6205 /* Do the bookkeeping for an element that was
6206 directly output as a constructor. */
6207 {
6208 constructor_bit_index = DECL_SIZE (constructor_fields);
6209 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6210 }
6211
6212 constructor_fields = 0;
6213 }
6214 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6215 {
6216 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6217 enum tree_code eltcode = TREE_CODE (elttype);
6218
6219 /* Accept a string constant to initialize a subarray. */
6220 if (value.value != 0
6221 && eltcode == ARRAY_TYPE
6222 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6223 && string_flag)
6224 value.value = orig_value;
6225 /* Otherwise, if we have come to a subaggregate,
6226 and we don't have an element of its type, push into it. */
6227 else if (value.value != 0
6228 && value.value != error_mark_node
6229 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6230 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6231 || eltcode == UNION_TYPE))
6232 {
6233 push_init_level (1);
6234 continue;
6235 }
6236
6237 if (constructor_max_index != 0
6238 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6239 || integer_all_onesp (constructor_max_index)))
6240 {
6241 pedwarn_init ("excess elements in array initializer");
6242 break;
6243 }
6244
6245 /* Now output the actual element. */
6246 if (value.value)
6247 {
6248 push_array_bounds (tree_low_cst (constructor_index, 0));
6249 output_init_element (value.value, strict_string,
6250 elttype, constructor_index, 1);
6251 RESTORE_SPELLING_DEPTH (constructor_depth);
6252 }
6253
6254 constructor_index
6255 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6256
6257 if (!value.value)
6258 /* If we are doing the bookkeeping for an element that was
6259 directly output as a constructor, we must update
6260 constructor_unfilled_index. */
6261 constructor_unfilled_index = constructor_index;
6262 }
6263 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6264 {
6265 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6266
6267 /* Do a basic check of initializer size. Note that vectors
6268 always have a fixed size derived from their type. */
6269 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6270 {
6271 pedwarn_init ("excess elements in vector initializer");
6272 break;
6273 }
6274
6275 /* Now output the actual element. */
6276 if (value.value)
6277 output_init_element (value.value, strict_string,
6278 elttype, constructor_index, 1);
6279
6280 constructor_index
6281 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6282
6283 if (!value.value)
6284 /* If we are doing the bookkeeping for an element that was
6285 directly output as a constructor, we must update
6286 constructor_unfilled_index. */
6287 constructor_unfilled_index = constructor_index;
6288 }
6289
6290 /* Handle the sole element allowed in a braced initializer
6291 for a scalar variable. */
6292 else if (constructor_type != error_mark_node
6293 && constructor_fields == 0)
6294 {
6295 pedwarn_init ("excess elements in scalar initializer");
6296 break;
6297 }
6298 else
6299 {
6300 if (value.value)
6301 output_init_element (value.value, strict_string,
6302 constructor_type, NULL_TREE, 1);
6303 constructor_fields = 0;
6304 }
6305
6306 /* Handle range initializers either at this level or anywhere higher
6307 in the designator stack. */
6308 if (constructor_range_stack)
6309 {
6310 struct constructor_range_stack *p, *range_stack;
6311 int finish = 0;
6312
6313 range_stack = constructor_range_stack;
6314 constructor_range_stack = 0;
6315 while (constructor_stack != range_stack->stack)
6316 {
6317 gcc_assert (constructor_stack->implicit);
6318 process_init_element (pop_init_level (1));
6319 }
6320 for (p = range_stack;
6321 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6322 p = p->prev)
6323 {
6324 gcc_assert (constructor_stack->implicit);
6325 process_init_element (pop_init_level (1));
6326 }
6327
6328 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6329 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6330 finish = 1;
6331
6332 while (1)
6333 {
6334 constructor_index = p->index;
6335 constructor_fields = p->fields;
6336 if (finish && p->range_end && p->index == p->range_start)
6337 {
6338 finish = 0;
6339 p->prev = 0;
6340 }
6341 p = p->next;
6342 if (!p)
6343 break;
6344 push_init_level (2);
6345 p->stack = constructor_stack;
6346 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6347 p->index = p->range_start;
6348 }
6349
6350 if (!finish)
6351 constructor_range_stack = range_stack;
6352 continue;
6353 }
6354
6355 break;
6356 }
6357
6358 constructor_range_stack = 0;
6359 }
6360 \f
6361 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6362 (guaranteed to be 'volatile' or null) and ARGS (represented using
6363 an ASM_EXPR node). */
6364 tree
6365 build_asm_stmt (tree cv_qualifier, tree args)
6366 {
6367 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6368 ASM_VOLATILE_P (args) = 1;
6369 return add_stmt (args);
6370 }
6371
6372 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6373 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6374 SIMPLE indicates whether there was anything at all after the
6375 string in the asm expression -- asm("blah") and asm("blah" : )
6376 are subtly different. We use a ASM_EXPR node to represent this. */
6377 tree
6378 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6379 bool simple)
6380 {
6381 tree tail;
6382 tree args;
6383 int i;
6384 const char *constraint;
6385 const char **oconstraints;
6386 bool allows_mem, allows_reg, is_inout;
6387 int ninputs, noutputs;
6388
6389 ninputs = list_length (inputs);
6390 noutputs = list_length (outputs);
6391 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6392
6393 string = resolve_asm_operand_names (string, outputs, inputs);
6394
6395 /* Remove output conversions that change the type but not the mode. */
6396 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6397 {
6398 tree output = TREE_VALUE (tail);
6399
6400 /* ??? Really, this should not be here. Users should be using a
6401 proper lvalue, dammit. But there's a long history of using casts
6402 in the output operands. In cases like longlong.h, this becomes a
6403 primitive form of typechecking -- if the cast can be removed, then
6404 the output operand had a type of the proper width; otherwise we'll
6405 get an error. Gross, but ... */
6406 STRIP_NOPS (output);
6407
6408 if (!lvalue_or_else (output, lv_asm))
6409 output = error_mark_node;
6410
6411 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6412 oconstraints[i] = constraint;
6413
6414 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6415 &allows_mem, &allows_reg, &is_inout))
6416 {
6417 /* If the operand is going to end up in memory,
6418 mark it addressable. */
6419 if (!allows_reg && !c_mark_addressable (output))
6420 output = error_mark_node;
6421 }
6422 else
6423 output = error_mark_node;
6424
6425 TREE_VALUE (tail) = output;
6426 }
6427
6428 /* Perform default conversions on array and function inputs.
6429 Don't do this for other types as it would screw up operands
6430 expected to be in memory. */
6431 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6432 {
6433 tree input;
6434
6435 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6436 input = TREE_VALUE (tail);
6437
6438 input = default_function_array_conversion (input);
6439
6440 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6441 oconstraints, &allows_mem, &allows_reg))
6442 {
6443 /* If the operand is going to end up in memory,
6444 mark it addressable. */
6445 if (!allows_reg && allows_mem)
6446 {
6447 /* Strip the nops as we allow this case. FIXME, this really
6448 should be rejected or made deprecated. */
6449 STRIP_NOPS (input);
6450 if (!c_mark_addressable (input))
6451 input = error_mark_node;
6452 }
6453 }
6454 else
6455 input = error_mark_node;
6456
6457 TREE_VALUE (tail) = input;
6458 }
6459
6460 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6461
6462 /* Simple asm statements are treated as volatile. */
6463 if (simple)
6464 {
6465 ASM_VOLATILE_P (args) = 1;
6466 ASM_INPUT_P (args) = 1;
6467 }
6468
6469 return args;
6470 }
6471 \f
6472 /* Generate a goto statement to LABEL. */
6473
6474 tree
6475 c_finish_goto_label (tree label)
6476 {
6477 tree decl = lookup_label (label);
6478 if (!decl)
6479 return NULL_TREE;
6480
6481 TREE_USED (decl) = 1;
6482 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6483 }
6484
6485 /* Generate a computed goto statement to EXPR. */
6486
6487 tree
6488 c_finish_goto_ptr (tree expr)
6489 {
6490 if (pedantic)
6491 pedwarn ("ISO C forbids %<goto *expr;%>");
6492 expr = convert (ptr_type_node, expr);
6493 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6494 }
6495
6496 /* Generate a C `return' statement. RETVAL is the expression for what
6497 to return, or a null pointer for `return;' with no value. */
6498
6499 tree
6500 c_finish_return (tree retval)
6501 {
6502 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6503
6504 if (TREE_THIS_VOLATILE (current_function_decl))
6505 warning ("function declared %<noreturn%> has a %<return%> statement");
6506
6507 if (!retval)
6508 {
6509 current_function_returns_null = 1;
6510 if ((warn_return_type || flag_isoc99)
6511 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6512 pedwarn_c99 ("%<return%> with no value, in "
6513 "function returning non-void");
6514 }
6515 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6516 {
6517 current_function_returns_null = 1;
6518 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6519 pedwarn ("%<return%> with a value, in function returning void");
6520 }
6521 else
6522 {
6523 tree t = convert_for_assignment (valtype, retval, ic_return,
6524 NULL_TREE, NULL_TREE, 0);
6525 tree res = DECL_RESULT (current_function_decl);
6526 tree inner;
6527
6528 current_function_returns_value = 1;
6529 if (t == error_mark_node)
6530 return NULL_TREE;
6531
6532 inner = t = convert (TREE_TYPE (res), t);
6533
6534 /* Strip any conversions, additions, and subtractions, and see if
6535 we are returning the address of a local variable. Warn if so. */
6536 while (1)
6537 {
6538 switch (TREE_CODE (inner))
6539 {
6540 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6541 case PLUS_EXPR:
6542 inner = TREE_OPERAND (inner, 0);
6543 continue;
6544
6545 case MINUS_EXPR:
6546 /* If the second operand of the MINUS_EXPR has a pointer
6547 type (or is converted from it), this may be valid, so
6548 don't give a warning. */
6549 {
6550 tree op1 = TREE_OPERAND (inner, 1);
6551
6552 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6553 && (TREE_CODE (op1) == NOP_EXPR
6554 || TREE_CODE (op1) == NON_LVALUE_EXPR
6555 || TREE_CODE (op1) == CONVERT_EXPR))
6556 op1 = TREE_OPERAND (op1, 0);
6557
6558 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6559 break;
6560
6561 inner = TREE_OPERAND (inner, 0);
6562 continue;
6563 }
6564
6565 case ADDR_EXPR:
6566 inner = TREE_OPERAND (inner, 0);
6567
6568 while (REFERENCE_CLASS_P (inner)
6569 && TREE_CODE (inner) != INDIRECT_REF)
6570 inner = TREE_OPERAND (inner, 0);
6571
6572 if (DECL_P (inner)
6573 && !DECL_EXTERNAL (inner)
6574 && !TREE_STATIC (inner)
6575 && DECL_CONTEXT (inner) == current_function_decl)
6576 warning ("function returns address of local variable");
6577 break;
6578
6579 default:
6580 break;
6581 }
6582
6583 break;
6584 }
6585
6586 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6587 }
6588
6589 return add_stmt (build_stmt (RETURN_EXPR, retval));
6590 }
6591 \f
6592 struct c_switch {
6593 /* The SWITCH_STMT being built. */
6594 tree switch_stmt;
6595
6596 /* The original type of the testing expression, i.e. before the
6597 default conversion is applied. */
6598 tree orig_type;
6599
6600 /* A splay-tree mapping the low element of a case range to the high
6601 element, or NULL_TREE if there is no high element. Used to
6602 determine whether or not a new case label duplicates an old case
6603 label. We need a tree, rather than simply a hash table, because
6604 of the GNU case range extension. */
6605 splay_tree cases;
6606
6607 /* The next node on the stack. */
6608 struct c_switch *next;
6609 };
6610
6611 /* A stack of the currently active switch statements. The innermost
6612 switch statement is on the top of the stack. There is no need to
6613 mark the stack for garbage collection because it is only active
6614 during the processing of the body of a function, and we never
6615 collect at that point. */
6616
6617 struct c_switch *c_switch_stack;
6618
6619 /* Start a C switch statement, testing expression EXP. Return the new
6620 SWITCH_STMT. */
6621
6622 tree
6623 c_start_case (tree exp)
6624 {
6625 enum tree_code code;
6626 tree type, orig_type = error_mark_node;
6627 struct c_switch *cs;
6628
6629 if (exp != error_mark_node)
6630 {
6631 code = TREE_CODE (TREE_TYPE (exp));
6632 orig_type = TREE_TYPE (exp);
6633
6634 if (!INTEGRAL_TYPE_P (orig_type)
6635 && code != ERROR_MARK)
6636 {
6637 error ("switch quantity not an integer");
6638 exp = integer_zero_node;
6639 orig_type = error_mark_node;
6640 }
6641 else
6642 {
6643 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6644
6645 if (warn_traditional && !in_system_header
6646 && (type == long_integer_type_node
6647 || type == long_unsigned_type_node))
6648 warning ("%<long%> switch expression not converted to "
6649 "%<int%> in ISO C");
6650
6651 exp = default_conversion (exp);
6652 type = TREE_TYPE (exp);
6653 }
6654 }
6655
6656 /* Add this new SWITCH_STMT to the stack. */
6657 cs = XNEW (struct c_switch);
6658 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6659 cs->orig_type = orig_type;
6660 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6661 cs->next = c_switch_stack;
6662 c_switch_stack = cs;
6663
6664 return add_stmt (cs->switch_stmt);
6665 }
6666
6667 /* Process a case label. */
6668
6669 tree
6670 do_case (tree low_value, tree high_value)
6671 {
6672 tree label = NULL_TREE;
6673
6674 if (c_switch_stack)
6675 {
6676 label = c_add_case_label (c_switch_stack->cases,
6677 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6678 c_switch_stack->orig_type,
6679 low_value, high_value);
6680 if (label == error_mark_node)
6681 label = NULL_TREE;
6682 }
6683 else if (low_value)
6684 error ("case label not within a switch statement");
6685 else
6686 error ("%<default%> label not within a switch statement");
6687
6688 return label;
6689 }
6690
6691 /* Finish the switch statement. */
6692
6693 void
6694 c_finish_case (tree body)
6695 {
6696 struct c_switch *cs = c_switch_stack;
6697
6698 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6699
6700 /* Emit warnings as needed. */
6701 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6702
6703 /* Pop the stack. */
6704 c_switch_stack = cs->next;
6705 splay_tree_delete (cs->cases);
6706 XDELETE (cs);
6707 }
6708 \f
6709 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6710 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6711 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6712 statement, and was not surrounded with parenthesis. */
6713
6714 void
6715 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6716 tree else_block, bool nested_if)
6717 {
6718 tree stmt;
6719
6720 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6721 if (warn_parentheses && nested_if && else_block == NULL)
6722 {
6723 tree inner_if = then_block;
6724
6725 /* We know from the grammar productions that there is an IF nested
6726 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6727 it might not be exactly THEN_BLOCK, but should be the last
6728 non-container statement within. */
6729 while (1)
6730 switch (TREE_CODE (inner_if))
6731 {
6732 case COND_EXPR:
6733 goto found;
6734 case BIND_EXPR:
6735 inner_if = BIND_EXPR_BODY (inner_if);
6736 break;
6737 case STATEMENT_LIST:
6738 inner_if = expr_last (then_block);
6739 break;
6740 case TRY_FINALLY_EXPR:
6741 case TRY_CATCH_EXPR:
6742 inner_if = TREE_OPERAND (inner_if, 0);
6743 break;
6744 default:
6745 gcc_unreachable ();
6746 }
6747 found:
6748
6749 if (COND_EXPR_ELSE (inner_if))
6750 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6751 &if_locus);
6752 }
6753
6754 /* Diagnose ";" via the special empty statement node that we create. */
6755 if (extra_warnings)
6756 {
6757 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6758 {
6759 if (!else_block)
6760 warning ("%Hempty body in an if-statement",
6761 EXPR_LOCUS (then_block));
6762 then_block = alloc_stmt_list ();
6763 }
6764 if (else_block
6765 && TREE_CODE (else_block) == NOP_EXPR
6766 && !TREE_TYPE (else_block))
6767 {
6768 warning ("%Hempty body in an else-statement",
6769 EXPR_LOCUS (else_block));
6770 else_block = alloc_stmt_list ();
6771 }
6772 }
6773
6774 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6775 SET_EXPR_LOCATION (stmt, if_locus);
6776 add_stmt (stmt);
6777 }
6778
6779 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6780 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6781 is false for DO loops. INCR is the FOR increment expression. BODY is
6782 the statement controlled by the loop. BLAB is the break label. CLAB is
6783 the continue label. Everything is allowed to be NULL. */
6784
6785 void
6786 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6787 tree blab, tree clab, bool cond_is_first)
6788 {
6789 tree entry = NULL, exit = NULL, t;
6790
6791 /* If the condition is zero don't generate a loop construct. */
6792 if (cond && integer_zerop (cond))
6793 {
6794 if (cond_is_first)
6795 {
6796 t = build_and_jump (&blab);
6797 SET_EXPR_LOCATION (t, start_locus);
6798 add_stmt (t);
6799 }
6800 }
6801 else
6802 {
6803 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6804
6805 /* If we have an exit condition, then we build an IF with gotos either
6806 out of the loop, or to the top of it. If there's no exit condition,
6807 then we just build a jump back to the top. */
6808 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6809
6810 if (cond && !integer_nonzerop (cond))
6811 {
6812 /* Canonicalize the loop condition to the end. This means
6813 generating a branch to the loop condition. Reuse the
6814 continue label, if possible. */
6815 if (cond_is_first)
6816 {
6817 if (incr || !clab)
6818 {
6819 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6820 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6821 }
6822 else
6823 t = build1 (GOTO_EXPR, void_type_node, clab);
6824 SET_EXPR_LOCATION (t, start_locus);
6825 add_stmt (t);
6826 }
6827
6828 t = build_and_jump (&blab);
6829 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6830 exit = fold (exit);
6831 if (cond_is_first)
6832 SET_EXPR_LOCATION (exit, start_locus);
6833 else
6834 SET_EXPR_LOCATION (exit, input_location);
6835 }
6836
6837 add_stmt (top);
6838 }
6839
6840 if (body)
6841 add_stmt (body);
6842 if (clab)
6843 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6844 if (incr)
6845 add_stmt (incr);
6846 if (entry)
6847 add_stmt (entry);
6848 if (exit)
6849 add_stmt (exit);
6850 if (blab)
6851 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6852 }
6853
6854 tree
6855 c_finish_bc_stmt (tree *label_p, bool is_break)
6856 {
6857 bool skip;
6858 tree label = *label_p;
6859
6860 /* In switch statements break is sometimes stylistically used after
6861 a return statement. This can lead to spurious warnings about
6862 control reaching the end of a non-void function when it is
6863 inlined. Note that we are calling block_may_fallthru with
6864 language specific tree nodes; this works because
6865 block_may_fallthru returns true when given something it does not
6866 understand. */
6867 skip = !block_may_fallthru (cur_stmt_list);
6868
6869 if (!label)
6870 {
6871 if (!skip)
6872 *label_p = label = create_artificial_label ();
6873 }
6874 else if (TREE_CODE (label) != LABEL_DECL)
6875 {
6876 if (is_break)
6877 error ("break statement not within loop or switch");
6878 else
6879 error ("continue statement not within a loop");
6880 return NULL_TREE;
6881 }
6882
6883 if (skip)
6884 return NULL_TREE;
6885
6886 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6887 }
6888
6889 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6890
6891 static void
6892 emit_side_effect_warnings (tree expr)
6893 {
6894 if (expr == error_mark_node)
6895 ;
6896 else if (!TREE_SIDE_EFFECTS (expr))
6897 {
6898 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6899 warning ("%Hstatement with no effect",
6900 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6901 }
6902 else if (warn_unused_value)
6903 warn_if_unused_value (expr, input_location);
6904 }
6905
6906 /* Process an expression as if it were a complete statement. Emit
6907 diagnostics, but do not call ADD_STMT. */
6908
6909 tree
6910 c_process_expr_stmt (tree expr)
6911 {
6912 if (!expr)
6913 return NULL_TREE;
6914
6915 /* Do default conversion if safe and possibly important,
6916 in case within ({...}). */
6917 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6918 && (flag_isoc99 || lvalue_p (expr)))
6919 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6920 expr = default_conversion (expr);
6921
6922 if (warn_sequence_point)
6923 verify_sequence_points (expr);
6924
6925 if (TREE_TYPE (expr) != error_mark_node
6926 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6927 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6928 error ("expression statement has incomplete type");
6929
6930 /* If we're not processing a statement expression, warn about unused values.
6931 Warnings for statement expressions will be emitted later, once we figure
6932 out which is the result. */
6933 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6934 && (extra_warnings || warn_unused_value))
6935 emit_side_effect_warnings (expr);
6936
6937 /* If the expression is not of a type to which we cannot assign a line
6938 number, wrap the thing in a no-op NOP_EXPR. */
6939 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6940 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6941
6942 if (EXPR_P (expr))
6943 SET_EXPR_LOCATION (expr, input_location);
6944
6945 return expr;
6946 }
6947
6948 /* Emit an expression as a statement. */
6949
6950 tree
6951 c_finish_expr_stmt (tree expr)
6952 {
6953 if (expr)
6954 return add_stmt (c_process_expr_stmt (expr));
6955 else
6956 return NULL;
6957 }
6958
6959 /* Do the opposite and emit a statement as an expression. To begin,
6960 create a new binding level and return it. */
6961
6962 tree
6963 c_begin_stmt_expr (void)
6964 {
6965 tree ret;
6966
6967 /* We must force a BLOCK for this level so that, if it is not expanded
6968 later, there is a way to turn off the entire subtree of blocks that
6969 are contained in it. */
6970 keep_next_level ();
6971 ret = c_begin_compound_stmt (true);
6972
6973 /* Mark the current statement list as belonging to a statement list. */
6974 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6975
6976 return ret;
6977 }
6978
6979 tree
6980 c_finish_stmt_expr (tree body)
6981 {
6982 tree last, type, tmp, val;
6983 tree *last_p;
6984
6985 body = c_end_compound_stmt (body, true);
6986
6987 /* Locate the last statement in BODY. See c_end_compound_stmt
6988 about always returning a BIND_EXPR. */
6989 last_p = &BIND_EXPR_BODY (body);
6990 last = BIND_EXPR_BODY (body);
6991
6992 continue_searching:
6993 if (TREE_CODE (last) == STATEMENT_LIST)
6994 {
6995 tree_stmt_iterator i;
6996
6997 /* This can happen with degenerate cases like ({ }). No value. */
6998 if (!TREE_SIDE_EFFECTS (last))
6999 return body;
7000
7001 /* If we're supposed to generate side effects warnings, process
7002 all of the statements except the last. */
7003 if (extra_warnings || warn_unused_value)
7004 {
7005 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7006 emit_side_effect_warnings (tsi_stmt (i));
7007 }
7008 else
7009 i = tsi_last (last);
7010 last_p = tsi_stmt_ptr (i);
7011 last = *last_p;
7012 }
7013
7014 /* If the end of the list is exception related, then the list was split
7015 by a call to push_cleanup. Continue searching. */
7016 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7017 || TREE_CODE (last) == TRY_CATCH_EXPR)
7018 {
7019 last_p = &TREE_OPERAND (last, 0);
7020 last = *last_p;
7021 goto continue_searching;
7022 }
7023
7024 /* In the case that the BIND_EXPR is not necessary, return the
7025 expression out from inside it. */
7026 if (last == error_mark_node
7027 || (last == BIND_EXPR_BODY (body)
7028 && BIND_EXPR_VARS (body) == NULL))
7029 return last;
7030
7031 /* Extract the type of said expression. */
7032 type = TREE_TYPE (last);
7033
7034 /* If we're not returning a value at all, then the BIND_EXPR that
7035 we already have is a fine expression to return. */
7036 if (!type || VOID_TYPE_P (type))
7037 return body;
7038
7039 /* Now that we've located the expression containing the value, it seems
7040 silly to make voidify_wrapper_expr repeat the process. Create a
7041 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7042 tmp = create_tmp_var_raw (type, NULL);
7043
7044 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7045 tree_expr_nonnegative_p giving up immediately. */
7046 val = last;
7047 if (TREE_CODE (val) == NOP_EXPR
7048 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7049 val = TREE_OPERAND (val, 0);
7050
7051 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7052 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7053
7054 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7055 }
7056 \f
7057 /* Begin and end compound statements. This is as simple as pushing
7058 and popping new statement lists from the tree. */
7059
7060 tree
7061 c_begin_compound_stmt (bool do_scope)
7062 {
7063 tree stmt = push_stmt_list ();
7064 if (do_scope)
7065 push_scope ();
7066 return stmt;
7067 }
7068
7069 tree
7070 c_end_compound_stmt (tree stmt, bool do_scope)
7071 {
7072 tree block = NULL;
7073
7074 if (do_scope)
7075 {
7076 if (c_dialect_objc ())
7077 objc_clear_super_receiver ();
7078 block = pop_scope ();
7079 }
7080
7081 stmt = pop_stmt_list (stmt);
7082 stmt = c_build_bind_expr (block, stmt);
7083
7084 /* If this compound statement is nested immediately inside a statement
7085 expression, then force a BIND_EXPR to be created. Otherwise we'll
7086 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7087 STATEMENT_LISTs merge, and thus we can lose track of what statement
7088 was really last. */
7089 if (cur_stmt_list
7090 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7091 && TREE_CODE (stmt) != BIND_EXPR)
7092 {
7093 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7094 TREE_SIDE_EFFECTS (stmt) = 1;
7095 }
7096
7097 return stmt;
7098 }
7099
7100 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7101 when the current scope is exited. EH_ONLY is true when this is not
7102 meant to apply to normal control flow transfer. */
7103
7104 void
7105 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7106 {
7107 enum tree_code code;
7108 tree stmt, list;
7109 bool stmt_expr;
7110
7111 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7112 stmt = build_stmt (code, NULL, cleanup);
7113 add_stmt (stmt);
7114 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7115 list = push_stmt_list ();
7116 TREE_OPERAND (stmt, 0) = list;
7117 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7118 }
7119 \f
7120 /* Build a binary-operation expression without default conversions.
7121 CODE is the kind of expression to build.
7122 This function differs from `build' in several ways:
7123 the data type of the result is computed and recorded in it,
7124 warnings are generated if arg data types are invalid,
7125 special handling for addition and subtraction of pointers is known,
7126 and some optimization is done (operations on narrow ints
7127 are done in the narrower type when that gives the same result).
7128 Constant folding is also done before the result is returned.
7129
7130 Note that the operands will never have enumeral types, or function
7131 or array types, because either they will have the default conversions
7132 performed or they have both just been converted to some other type in which
7133 the arithmetic is to be done. */
7134
7135 tree
7136 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7137 int convert_p)
7138 {
7139 tree type0, type1;
7140 enum tree_code code0, code1;
7141 tree op0, op1;
7142
7143 /* Expression code to give to the expression when it is built.
7144 Normally this is CODE, which is what the caller asked for,
7145 but in some special cases we change it. */
7146 enum tree_code resultcode = code;
7147
7148 /* Data type in which the computation is to be performed.
7149 In the simplest cases this is the common type of the arguments. */
7150 tree result_type = NULL;
7151
7152 /* Nonzero means operands have already been type-converted
7153 in whatever way is necessary.
7154 Zero means they need to be converted to RESULT_TYPE. */
7155 int converted = 0;
7156
7157 /* Nonzero means create the expression with this type, rather than
7158 RESULT_TYPE. */
7159 tree build_type = 0;
7160
7161 /* Nonzero means after finally constructing the expression
7162 convert it to this type. */
7163 tree final_type = 0;
7164
7165 /* Nonzero if this is an operation like MIN or MAX which can
7166 safely be computed in short if both args are promoted shorts.
7167 Also implies COMMON.
7168 -1 indicates a bitwise operation; this makes a difference
7169 in the exact conditions for when it is safe to do the operation
7170 in a narrower mode. */
7171 int shorten = 0;
7172
7173 /* Nonzero if this is a comparison operation;
7174 if both args are promoted shorts, compare the original shorts.
7175 Also implies COMMON. */
7176 int short_compare = 0;
7177
7178 /* Nonzero if this is a right-shift operation, which can be computed on the
7179 original short and then promoted if the operand is a promoted short. */
7180 int short_shift = 0;
7181
7182 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7183 int common = 0;
7184
7185 if (convert_p)
7186 {
7187 op0 = default_conversion (orig_op0);
7188 op1 = default_conversion (orig_op1);
7189 }
7190 else
7191 {
7192 op0 = orig_op0;
7193 op1 = orig_op1;
7194 }
7195
7196 type0 = TREE_TYPE (op0);
7197 type1 = TREE_TYPE (op1);
7198
7199 /* The expression codes of the data types of the arguments tell us
7200 whether the arguments are integers, floating, pointers, etc. */
7201 code0 = TREE_CODE (type0);
7202 code1 = TREE_CODE (type1);
7203
7204 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7205 STRIP_TYPE_NOPS (op0);
7206 STRIP_TYPE_NOPS (op1);
7207
7208 /* If an error was already reported for one of the arguments,
7209 avoid reporting another error. */
7210
7211 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7212 return error_mark_node;
7213
7214 switch (code)
7215 {
7216 case PLUS_EXPR:
7217 /* Handle the pointer + int case. */
7218 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7219 return pointer_int_sum (PLUS_EXPR, op0, op1);
7220 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7221 return pointer_int_sum (PLUS_EXPR, op1, op0);
7222 else
7223 common = 1;
7224 break;
7225
7226 case MINUS_EXPR:
7227 /* Subtraction of two similar pointers.
7228 We must subtract them as integers, then divide by object size. */
7229 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7230 && comp_target_types (type0, type1, 1))
7231 return pointer_diff (op0, op1);
7232 /* Handle pointer minus int. Just like pointer plus int. */
7233 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7234 return pointer_int_sum (MINUS_EXPR, op0, op1);
7235 else
7236 common = 1;
7237 break;
7238
7239 case MULT_EXPR:
7240 common = 1;
7241 break;
7242
7243 case TRUNC_DIV_EXPR:
7244 case CEIL_DIV_EXPR:
7245 case FLOOR_DIV_EXPR:
7246 case ROUND_DIV_EXPR:
7247 case EXACT_DIV_EXPR:
7248 /* Floating point division by zero is a legitimate way to obtain
7249 infinities and NaNs. */
7250 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7251 warning ("division by zero");
7252
7253 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7254 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7255 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7256 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7257 {
7258 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7259 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7260 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7261 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7262
7263 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7264 resultcode = RDIV_EXPR;
7265 else
7266 /* Although it would be tempting to shorten always here, that
7267 loses on some targets, since the modulo instruction is
7268 undefined if the quotient can't be represented in the
7269 computation mode. We shorten only if unsigned or if
7270 dividing by something we know != -1. */
7271 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7272 || (TREE_CODE (op1) == INTEGER_CST
7273 && !integer_all_onesp (op1)));
7274 common = 1;
7275 }
7276 break;
7277
7278 case BIT_AND_EXPR:
7279 case BIT_IOR_EXPR:
7280 case BIT_XOR_EXPR:
7281 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7282 shorten = -1;
7283 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7284 common = 1;
7285 break;
7286
7287 case TRUNC_MOD_EXPR:
7288 case FLOOR_MOD_EXPR:
7289 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7290 warning ("division by zero");
7291
7292 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7293 {
7294 /* Although it would be tempting to shorten always here, that loses
7295 on some targets, since the modulo instruction is undefined if the
7296 quotient can't be represented in the computation mode. We shorten
7297 only if unsigned or if dividing by something we know != -1. */
7298 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7299 || (TREE_CODE (op1) == INTEGER_CST
7300 && !integer_all_onesp (op1)));
7301 common = 1;
7302 }
7303 break;
7304
7305 case TRUTH_ANDIF_EXPR:
7306 case TRUTH_ORIF_EXPR:
7307 case TRUTH_AND_EXPR:
7308 case TRUTH_OR_EXPR:
7309 case TRUTH_XOR_EXPR:
7310 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7311 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7312 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7313 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7314 {
7315 /* Result of these operations is always an int,
7316 but that does not mean the operands should be
7317 converted to ints! */
7318 result_type = integer_type_node;
7319 op0 = lang_hooks.truthvalue_conversion (op0);
7320 op1 = lang_hooks.truthvalue_conversion (op1);
7321 converted = 1;
7322 }
7323 break;
7324
7325 /* Shift operations: result has same type as first operand;
7326 always convert second operand to int.
7327 Also set SHORT_SHIFT if shifting rightward. */
7328
7329 case RSHIFT_EXPR:
7330 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7331 {
7332 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7333 {
7334 if (tree_int_cst_sgn (op1) < 0)
7335 warning ("right shift count is negative");
7336 else
7337 {
7338 if (!integer_zerop (op1))
7339 short_shift = 1;
7340
7341 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7342 warning ("right shift count >= width of type");
7343 }
7344 }
7345
7346 /* Use the type of the value to be shifted. */
7347 result_type = type0;
7348 /* Convert the shift-count to an integer, regardless of size
7349 of value being shifted. */
7350 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7351 op1 = convert (integer_type_node, op1);
7352 /* Avoid converting op1 to result_type later. */
7353 converted = 1;
7354 }
7355 break;
7356
7357 case LSHIFT_EXPR:
7358 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7359 {
7360 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7361 {
7362 if (tree_int_cst_sgn (op1) < 0)
7363 warning ("left shift count is negative");
7364
7365 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7366 warning ("left shift count >= width of type");
7367 }
7368
7369 /* Use the type of the value to be shifted. */
7370 result_type = type0;
7371 /* Convert the shift-count to an integer, regardless of size
7372 of value being shifted. */
7373 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7374 op1 = convert (integer_type_node, op1);
7375 /* Avoid converting op1 to result_type later. */
7376 converted = 1;
7377 }
7378 break;
7379
7380 case EQ_EXPR:
7381 case NE_EXPR:
7382 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7383 warning ("comparing floating point with == or != is unsafe");
7384 /* Result of comparison is always int,
7385 but don't convert the args to int! */
7386 build_type = integer_type_node;
7387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7388 || code0 == COMPLEX_TYPE)
7389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7390 || code1 == COMPLEX_TYPE))
7391 short_compare = 1;
7392 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7393 {
7394 tree tt0 = TREE_TYPE (type0);
7395 tree tt1 = TREE_TYPE (type1);
7396 /* Anything compares with void *. void * compares with anything.
7397 Otherwise, the targets must be compatible
7398 and both must be object or both incomplete. */
7399 if (comp_target_types (type0, type1, 1))
7400 result_type = common_pointer_type (type0, type1);
7401 else if (VOID_TYPE_P (tt0))
7402 {
7403 /* op0 != orig_op0 detects the case of something
7404 whose value is 0 but which isn't a valid null ptr const. */
7405 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7406 && TREE_CODE (tt1) == FUNCTION_TYPE)
7407 pedwarn ("ISO C forbids comparison of %<void *%>"
7408 " with function pointer");
7409 }
7410 else if (VOID_TYPE_P (tt1))
7411 {
7412 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7413 && TREE_CODE (tt0) == FUNCTION_TYPE)
7414 pedwarn ("ISO C forbids comparison of %<void *%>"
7415 " with function pointer");
7416 }
7417 else
7418 pedwarn ("comparison of distinct pointer types lacks a cast");
7419
7420 if (result_type == NULL_TREE)
7421 result_type = ptr_type_node;
7422 }
7423 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7424 && integer_zerop (op1))
7425 result_type = type0;
7426 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7427 && integer_zerop (op0))
7428 result_type = type1;
7429 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7430 {
7431 result_type = type0;
7432 pedwarn ("comparison between pointer and integer");
7433 }
7434 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7435 {
7436 result_type = type1;
7437 pedwarn ("comparison between pointer and integer");
7438 }
7439 break;
7440
7441 case LE_EXPR:
7442 case GE_EXPR:
7443 case LT_EXPR:
7444 case GT_EXPR:
7445 build_type = integer_type_node;
7446 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7448 short_compare = 1;
7449 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7450 {
7451 if (comp_target_types (type0, type1, 1))
7452 {
7453 result_type = common_pointer_type (type0, type1);
7454 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7455 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7456 pedwarn ("comparison of complete and incomplete pointers");
7457 else if (pedantic
7458 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7459 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7460 }
7461 else
7462 {
7463 result_type = ptr_type_node;
7464 pedwarn ("comparison of distinct pointer types lacks a cast");
7465 }
7466 }
7467 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7468 && integer_zerop (op1))
7469 {
7470 result_type = type0;
7471 if (pedantic || extra_warnings)
7472 pedwarn ("ordered comparison of pointer with integer zero");
7473 }
7474 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7475 && integer_zerop (op0))
7476 {
7477 result_type = type1;
7478 if (pedantic)
7479 pedwarn ("ordered comparison of pointer with integer zero");
7480 }
7481 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7482 {
7483 result_type = type0;
7484 pedwarn ("comparison between pointer and integer");
7485 }
7486 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7487 {
7488 result_type = type1;
7489 pedwarn ("comparison between pointer and integer");
7490 }
7491 break;
7492
7493 default:
7494 gcc_unreachable ();
7495 }
7496
7497 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7498 return error_mark_node;
7499
7500 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7501 || code0 == VECTOR_TYPE)
7502 &&
7503 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7504 || code1 == VECTOR_TYPE))
7505 {
7506 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7507
7508 if (shorten || common || short_compare)
7509 result_type = c_common_type (type0, type1);
7510
7511 /* For certain operations (which identify themselves by shorten != 0)
7512 if both args were extended from the same smaller type,
7513 do the arithmetic in that type and then extend.
7514
7515 shorten !=0 and !=1 indicates a bitwise operation.
7516 For them, this optimization is safe only if
7517 both args are zero-extended or both are sign-extended.
7518 Otherwise, we might change the result.
7519 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7520 but calculated in (unsigned short) it would be (unsigned short)-1. */
7521
7522 if (shorten && none_complex)
7523 {
7524 int unsigned0, unsigned1;
7525 tree arg0 = get_narrower (op0, &unsigned0);
7526 tree arg1 = get_narrower (op1, &unsigned1);
7527 /* UNS is 1 if the operation to be done is an unsigned one. */
7528 int uns = TYPE_UNSIGNED (result_type);
7529 tree type;
7530
7531 final_type = result_type;
7532
7533 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7534 but it *requires* conversion to FINAL_TYPE. */
7535
7536 if ((TYPE_PRECISION (TREE_TYPE (op0))
7537 == TYPE_PRECISION (TREE_TYPE (arg0)))
7538 && TREE_TYPE (op0) != final_type)
7539 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7540 if ((TYPE_PRECISION (TREE_TYPE (op1))
7541 == TYPE_PRECISION (TREE_TYPE (arg1)))
7542 && TREE_TYPE (op1) != final_type)
7543 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7544
7545 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7546
7547 /* For bitwise operations, signedness of nominal type
7548 does not matter. Consider only how operands were extended. */
7549 if (shorten == -1)
7550 uns = unsigned0;
7551
7552 /* Note that in all three cases below we refrain from optimizing
7553 an unsigned operation on sign-extended args.
7554 That would not be valid. */
7555
7556 /* Both args variable: if both extended in same way
7557 from same width, do it in that width.
7558 Do it unsigned if args were zero-extended. */
7559 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7560 < TYPE_PRECISION (result_type))
7561 && (TYPE_PRECISION (TREE_TYPE (arg1))
7562 == TYPE_PRECISION (TREE_TYPE (arg0)))
7563 && unsigned0 == unsigned1
7564 && (unsigned0 || !uns))
7565 result_type
7566 = c_common_signed_or_unsigned_type
7567 (unsigned0, c_common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7568 else if (TREE_CODE (arg0) == INTEGER_CST
7569 && (unsigned1 || !uns)
7570 && (TYPE_PRECISION (TREE_TYPE (arg1))
7571 < TYPE_PRECISION (result_type))
7572 && (type
7573 = c_common_signed_or_unsigned_type (unsigned1,
7574 TREE_TYPE (arg1)),
7575 int_fits_type_p (arg0, type)))
7576 result_type = type;
7577 else if (TREE_CODE (arg1) == INTEGER_CST
7578 && (unsigned0 || !uns)
7579 && (TYPE_PRECISION (TREE_TYPE (arg0))
7580 < TYPE_PRECISION (result_type))
7581 && (type
7582 = c_common_signed_or_unsigned_type (unsigned0,
7583 TREE_TYPE (arg0)),
7584 int_fits_type_p (arg1, type)))
7585 result_type = type;
7586 }
7587
7588 /* Shifts can be shortened if shifting right. */
7589
7590 if (short_shift)
7591 {
7592 int unsigned_arg;
7593 tree arg0 = get_narrower (op0, &unsigned_arg);
7594
7595 final_type = result_type;
7596
7597 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7598 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7599
7600 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7601 /* We can shorten only if the shift count is less than the
7602 number of bits in the smaller type size. */
7603 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7604 /* We cannot drop an unsigned shift after sign-extension. */
7605 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7606 {
7607 /* Do an unsigned shift if the operand was zero-extended. */
7608 result_type
7609 = c_common_signed_or_unsigned_type (unsigned_arg,
7610 TREE_TYPE (arg0));
7611 /* Convert value-to-be-shifted to that type. */
7612 if (TREE_TYPE (op0) != result_type)
7613 op0 = convert (result_type, op0);
7614 converted = 1;
7615 }
7616 }
7617
7618 /* Comparison operations are shortened too but differently.
7619 They identify themselves by setting short_compare = 1. */
7620
7621 if (short_compare)
7622 {
7623 /* Don't write &op0, etc., because that would prevent op0
7624 from being kept in a register.
7625 Instead, make copies of the our local variables and
7626 pass the copies by reference, then copy them back afterward. */
7627 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7628 enum tree_code xresultcode = resultcode;
7629 tree val
7630 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7631
7632 if (val != 0)
7633 return val;
7634
7635 op0 = xop0, op1 = xop1;
7636 converted = 1;
7637 resultcode = xresultcode;
7638
7639 if (warn_sign_compare && skip_evaluation == 0)
7640 {
7641 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7642 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7643 int unsignedp0, unsignedp1;
7644 tree primop0 = get_narrower (op0, &unsignedp0);
7645 tree primop1 = get_narrower (op1, &unsignedp1);
7646
7647 xop0 = orig_op0;
7648 xop1 = orig_op1;
7649 STRIP_TYPE_NOPS (xop0);
7650 STRIP_TYPE_NOPS (xop1);
7651
7652 /* Give warnings for comparisons between signed and unsigned
7653 quantities that may fail.
7654
7655 Do the checking based on the original operand trees, so that
7656 casts will be considered, but default promotions won't be.
7657
7658 Do not warn if the comparison is being done in a signed type,
7659 since the signed type will only be chosen if it can represent
7660 all the values of the unsigned type. */
7661 if (!TYPE_UNSIGNED (result_type))
7662 /* OK */;
7663 /* Do not warn if both operands are the same signedness. */
7664 else if (op0_signed == op1_signed)
7665 /* OK */;
7666 else
7667 {
7668 tree sop, uop;
7669
7670 if (op0_signed)
7671 sop = xop0, uop = xop1;
7672 else
7673 sop = xop1, uop = xop0;
7674
7675 /* Do not warn if the signed quantity is an
7676 unsuffixed integer literal (or some static
7677 constant expression involving such literals or a
7678 conditional expression involving such literals)
7679 and it is non-negative. */
7680 if (tree_expr_nonnegative_p (sop))
7681 /* OK */;
7682 /* Do not warn if the comparison is an equality operation,
7683 the unsigned quantity is an integral constant, and it
7684 would fit in the result if the result were signed. */
7685 else if (TREE_CODE (uop) == INTEGER_CST
7686 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7687 && int_fits_type_p
7688 (uop, c_common_signed_type (result_type)))
7689 /* OK */;
7690 /* Do not warn if the unsigned quantity is an enumeration
7691 constant and its maximum value would fit in the result
7692 if the result were signed. */
7693 else if (TREE_CODE (uop) == INTEGER_CST
7694 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7695 && int_fits_type_p
7696 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7697 c_common_signed_type (result_type)))
7698 /* OK */;
7699 else
7700 warning ("comparison between signed and unsigned");
7701 }
7702
7703 /* Warn if two unsigned values are being compared in a size
7704 larger than their original size, and one (and only one) is the
7705 result of a `~' operator. This comparison will always fail.
7706
7707 Also warn if one operand is a constant, and the constant
7708 does not have all bits set that are set in the ~ operand
7709 when it is extended. */
7710
7711 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7712 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7713 {
7714 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7715 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7716 &unsignedp0);
7717 else
7718 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7719 &unsignedp1);
7720
7721 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7722 {
7723 tree primop;
7724 HOST_WIDE_INT constant, mask;
7725 int unsignedp, bits;
7726
7727 if (host_integerp (primop0, 0))
7728 {
7729 primop = primop1;
7730 unsignedp = unsignedp1;
7731 constant = tree_low_cst (primop0, 0);
7732 }
7733 else
7734 {
7735 primop = primop0;
7736 unsignedp = unsignedp0;
7737 constant = tree_low_cst (primop1, 0);
7738 }
7739
7740 bits = TYPE_PRECISION (TREE_TYPE (primop));
7741 if (bits < TYPE_PRECISION (result_type)
7742 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7743 {
7744 mask = (~(HOST_WIDE_INT) 0) << bits;
7745 if ((mask & constant) != mask)
7746 warning ("comparison of promoted ~unsigned with constant");
7747 }
7748 }
7749 else if (unsignedp0 && unsignedp1
7750 && (TYPE_PRECISION (TREE_TYPE (primop0))
7751 < TYPE_PRECISION (result_type))
7752 && (TYPE_PRECISION (TREE_TYPE (primop1))
7753 < TYPE_PRECISION (result_type)))
7754 warning ("comparison of promoted ~unsigned with unsigned");
7755 }
7756 }
7757 }
7758 }
7759
7760 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7761 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7762 Then the expression will be built.
7763 It will be given type FINAL_TYPE if that is nonzero;
7764 otherwise, it will be given type RESULT_TYPE. */
7765
7766 if (!result_type)
7767 {
7768 binary_op_error (code);
7769 return error_mark_node;
7770 }
7771
7772 if (!converted)
7773 {
7774 if (TREE_TYPE (op0) != result_type)
7775 op0 = convert (result_type, op0);
7776 if (TREE_TYPE (op1) != result_type)
7777 op1 = convert (result_type, op1);
7778
7779 /* This can happen if one operand has a vector type, and the other
7780 has a different type. */
7781 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7782 return error_mark_node;
7783 }
7784
7785 if (build_type == NULL_TREE)
7786 build_type = result_type;
7787
7788 {
7789 tree result = build2 (resultcode, build_type, op0, op1);
7790
7791 /* Treat expressions in initializers specially as they can't trap. */
7792 result = require_constant_value ? fold_initializer (result)
7793 : fold (result);
7794
7795 if (final_type != 0)
7796 result = convert (final_type, result);
7797 return result;
7798 }
7799 }