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