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