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