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