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