(thumb_jump): Reduce the backward branch range, and increase the forward branch
[official-gcc.git] / gcc / c-typeck.c
blob64b568cc2185e6de42c0d0a7d23311b5ef391380
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
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.
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). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "langhooks.h"
39 #include "c-tree.h"
40 #include "tm_p.h"
41 #include "flags.h"
42 #include "output.h"
43 #include "expr.h"
44 #include "toplev.h"
45 #include "intl.h"
46 #include "ggc.h"
47 #include "target.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static tree qualify_type (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree internal_build_compound_expr (tree, int);
64 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
65 int);
66 static void warn_for_assignment (const char *, const char *, tree, int);
67 static tree valid_compound_expr_initializer (tree, tree);
68 static void push_string (const char *);
69 static void push_member_name (tree);
70 static void push_array_bounds (int);
71 static int spelling_length (void);
72 static char *print_spelling (char *);
73 static void warning_init (const char *);
74 static tree digest_init (tree, tree, int);
75 static void output_init_element (tree, tree, tree, int);
76 static void output_pending_init_elements (int);
77 static int set_designator (int);
78 static void push_range_stack (tree);
79 static void add_pending_init (tree, tree);
80 static void set_nonincremental_init (void);
81 static void set_nonincremental_init_from_string (tree);
82 static tree find_init_member (tree);
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
87 tree
88 require_complete_type (tree value)
90 tree type = TREE_TYPE (value);
92 if (value == error_mark_node || type == error_mark_node)
93 return error_mark_node;
95 /* First, detect a valid value with a complete type. */
96 if (COMPLETE_TYPE_P (type))
97 return value;
99 c_incomplete_type_error (value, type);
100 return error_mark_node;
103 /* Print an error message for invalid use of an incomplete type.
104 VALUE is the expression that was used (or 0 if that isn't known)
105 and TYPE is the type that was invalid. */
107 void
108 c_incomplete_type_error (tree value, tree type)
110 const char *type_code_string;
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
114 return;
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
120 else
122 retry:
123 /* We must print an error message. Be clever about what it says. */
125 switch (TREE_CODE (type))
127 case RECORD_TYPE:
128 type_code_string = "struct";
129 break;
131 case UNION_TYPE:
132 type_code_string = "union";
133 break;
135 case ENUMERAL_TYPE:
136 type_code_string = "enum";
137 break;
139 case VOID_TYPE:
140 error ("invalid use of void expression");
141 return;
143 case ARRAY_TYPE:
144 if (TYPE_DOMAIN (type))
146 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
148 error ("invalid use of flexible array member");
149 return;
151 type = TREE_TYPE (type);
152 goto retry;
154 error ("invalid use of array with unspecified bounds");
155 return;
157 default:
158 abort ();
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
164 else
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Given a type, apply default promotions wrt unnamed function
172 arguments and return the new type. */
174 tree
175 c_type_promotes_to (tree type)
177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
178 return double_type_node;
180 if (c_promoting_integer_type_p (type))
182 /* Preserve unsignedness if not really getting any wider. */
183 if (TREE_UNSIGNED (type)
184 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
185 return unsigned_type_node;
186 return integer_type_node;
189 return type;
192 /* Return a variant of TYPE which has all the type qualifiers of LIKE
193 as well as those of TYPE. */
195 static tree
196 qualify_type (tree type, tree like)
198 return c_build_qualified_type (type,
199 TYPE_QUALS (type) | TYPE_QUALS (like));
202 /* Return the common type of two types.
203 We assume that comptypes has already been done and returned 1;
204 if that isn't so, this may crash. In particular, we assume that qualifiers
205 match.
207 This is the type for the result of most arithmetic operations
208 if the operands have the given two types. */
210 tree
211 common_type (tree t1, tree t2)
213 enum tree_code code1;
214 enum tree_code code2;
215 tree attributes;
217 /* Save time if the two types are the same. */
219 if (t1 == t2) return t1;
221 /* If one type is nonsense, use the other. */
222 if (t1 == error_mark_node)
223 return t2;
224 if (t2 == error_mark_node)
225 return t1;
227 /* Merge the attributes. */
228 attributes = targetm.merge_type_attributes (t1, t2);
230 /* Treat an enum type as the unsigned integer type of the same width. */
232 if (TREE_CODE (t1) == ENUMERAL_TYPE)
233 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
234 if (TREE_CODE (t2) == ENUMERAL_TYPE)
235 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
237 code1 = TREE_CODE (t1);
238 code2 = TREE_CODE (t2);
240 /* If one type is complex, form the common type of the non-complex
241 components, then make that complex. Use T1 or T2 if it is the
242 required type. */
243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
247 tree subtype = common_type (subtype1, subtype2);
249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
250 return build_type_attribute_variant (t1, attributes);
251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
252 return build_type_attribute_variant (t2, attributes);
253 else
254 return build_type_attribute_variant (build_complex_type (subtype),
255 attributes);
258 switch (code1)
260 case INTEGER_TYPE:
261 case REAL_TYPE:
262 /* If only one is real, use it as the result. */
264 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
265 return build_type_attribute_variant (t1, attributes);
267 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
268 return build_type_attribute_variant (t2, attributes);
270 /* Both real or both integers; use the one with greater precision. */
272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
273 return build_type_attribute_variant (t1, attributes);
274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
275 return build_type_attribute_variant (t2, attributes);
277 /* Same precision. Prefer longs to ints even when same size. */
279 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
280 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
281 return build_type_attribute_variant (long_unsigned_type_node,
282 attributes);
284 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
287 /* But preserve unsignedness from the other type,
288 since long cannot hold all the values of an unsigned int. */
289 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
290 t1 = long_unsigned_type_node;
291 else
292 t1 = long_integer_type_node;
293 return build_type_attribute_variant (t1, attributes);
296 /* Likewise, prefer long double to double even if same size. */
297 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
298 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
299 return build_type_attribute_variant (long_double_type_node,
300 attributes);
302 /* Otherwise prefer the unsigned one. */
304 if (TREE_UNSIGNED (t1))
305 return build_type_attribute_variant (t1, attributes);
306 else
307 return build_type_attribute_variant (t2, attributes);
309 case POINTER_TYPE:
310 /* For two pointers, do this recursively on the target type,
311 and combine the qualifiers of the two types' targets. */
312 /* This code was turned off; I don't know why.
313 But ANSI C specifies doing this with the qualifiers.
314 So I turned it on again. */
316 tree pointed_to_1 = TREE_TYPE (t1);
317 tree pointed_to_2 = TREE_TYPE (t2);
318 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
319 TYPE_MAIN_VARIANT (pointed_to_2));
320 t1 = build_pointer_type (c_build_qualified_type
321 (target,
322 TYPE_QUALS (pointed_to_1) |
323 TYPE_QUALS (pointed_to_2)));
324 return build_type_attribute_variant (t1, attributes);
327 case ARRAY_TYPE:
329 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
330 /* Save space: see if the result is identical to one of the args. */
331 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
332 return build_type_attribute_variant (t1, attributes);
333 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
334 return build_type_attribute_variant (t2, attributes);
335 /* Merge the element types, and have a size if either arg has one. */
336 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
337 return build_type_attribute_variant (t1, attributes);
340 case FUNCTION_TYPE:
341 /* Function types: prefer the one that specified arg types.
342 If both do, merge the arg types. Also merge the return types. */
344 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
345 tree p1 = TYPE_ARG_TYPES (t1);
346 tree p2 = TYPE_ARG_TYPES (t2);
347 int len;
348 tree newargs, n;
349 int i;
351 /* Save space: see if the result is identical to one of the args. */
352 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
353 return build_type_attribute_variant (t1, attributes);
354 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
355 return build_type_attribute_variant (t2, attributes);
357 /* Simple way if one arg fails to specify argument types. */
358 if (TYPE_ARG_TYPES (t1) == 0)
360 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
361 return build_type_attribute_variant (t1, attributes);
363 if (TYPE_ARG_TYPES (t2) == 0)
365 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
366 return build_type_attribute_variant (t1, attributes);
369 /* If both args specify argument types, we must merge the two
370 lists, argument by argument. */
371 /* Tell global_bindings_p to return false so that variable_size
372 doesn't abort on VLAs in parameter types. */
373 c_override_global_bindings_to_false = true;
375 len = list_length (p1);
376 newargs = 0;
378 for (i = 0; i < len; i++)
379 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
381 n = newargs;
383 for (; p1;
384 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
386 /* A null type means arg type is not specified.
387 Take whatever the other function type has. */
388 if (TREE_VALUE (p1) == 0)
390 TREE_VALUE (n) = TREE_VALUE (p2);
391 goto parm_done;
393 if (TREE_VALUE (p2) == 0)
395 TREE_VALUE (n) = TREE_VALUE (p1);
396 goto parm_done;
399 /* Given wait (union {union wait *u; int *i} *)
400 and wait (union wait *),
401 prefer union wait * as type of parm. */
402 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
403 && TREE_VALUE (p1) != TREE_VALUE (p2))
405 tree memb;
406 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
407 memb; memb = TREE_CHAIN (memb))
408 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
409 COMPARE_STRICT))
411 TREE_VALUE (n) = TREE_VALUE (p2);
412 if (pedantic)
413 pedwarn ("function types not truly compatible in ISO C");
414 goto parm_done;
417 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
418 && TREE_VALUE (p2) != TREE_VALUE (p1))
420 tree memb;
421 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
422 memb; memb = TREE_CHAIN (memb))
423 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
424 COMPARE_STRICT))
426 TREE_VALUE (n) = TREE_VALUE (p1);
427 if (pedantic)
428 pedwarn ("function types not truly compatible in ISO C");
429 goto parm_done;
432 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
433 parm_done: ;
436 c_override_global_bindings_to_false = false;
437 t1 = build_function_type (valtype, newargs);
438 /* ... falls through ... */
441 default:
442 return build_type_attribute_variant (t1, attributes);
447 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
448 or various other operations. Return 2 if they are compatible
449 but a warning may be needed if you use them together. */
452 comptypes (tree type1, tree type2, int flags)
454 tree t1 = type1;
455 tree t2 = type2;
456 int attrval, val;
458 /* Suppress errors caused by previously reported errors. */
460 if (t1 == t2 || !t1 || !t2
461 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
462 return 1;
464 /* If either type is the internal version of sizetype, return the
465 language version. */
466 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
467 && TYPE_ORIG_SIZE_TYPE (t1))
468 t1 = TYPE_ORIG_SIZE_TYPE (t1);
470 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
471 && TYPE_ORIG_SIZE_TYPE (t2))
472 t2 = TYPE_ORIG_SIZE_TYPE (t2);
475 /* Enumerated types are compatible with integer types, but this is
476 not transitive: two enumerated types in the same translation unit
477 are compatible with each other only if they are the same type. */
479 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
480 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
481 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
482 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
484 if (t1 == t2)
485 return 1;
487 /* Different classes of types can't be compatible. */
489 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
491 /* Qualifiers must match. */
493 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
494 return 0;
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). */
500 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
501 return 1;
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;
507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
508 val = 0;
510 switch (TREE_CODE (t1))
512 case POINTER_TYPE:
513 /* We must give ObjC the first crack at comparing pointers, since
514 protocol qualifiers may be involved. */
515 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
516 break;
517 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
518 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
519 break;
521 case FUNCTION_TYPE:
522 val = function_types_compatible_p (t1, t2, flags);
523 break;
525 case ARRAY_TYPE:
527 tree d1 = TYPE_DOMAIN (t1);
528 tree d2 = TYPE_DOMAIN (t2);
529 bool d1_variable, d2_variable;
530 bool d1_zero, d2_zero;
531 val = 1;
533 /* Target types must match incl. qualifiers. */
534 if (TREE_TYPE (t1) != TREE_TYPE (t2)
535 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
536 flags)))
537 return 0;
539 /* Sizes must match unless one is missing or variable. */
540 if (d1 == 0 || d2 == 0 || d1 == d2)
541 break;
543 d1_zero = ! TYPE_MAX_VALUE (d1);
544 d2_zero = ! TYPE_MAX_VALUE (d2);
546 d1_variable = (! d1_zero
547 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
548 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
549 d2_variable = (! d2_zero
550 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
551 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
553 if (d1_variable || d2_variable)
554 break;
555 if (d1_zero && d2_zero)
556 break;
557 if (d1_zero || d2_zero
558 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
559 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
560 val = 0;
562 break;
565 case RECORD_TYPE:
566 /* We are dealing with two distinct structs. In assorted Objective-C
567 corner cases, however, these can still be deemed equivalent. */
568 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
569 val = 1;
571 case ENUMERAL_TYPE:
572 case UNION_TYPE:
573 if (val != 1 && !same_translation_unit_p (t1, t2))
574 val = tagged_types_tu_compatible_p (t1, t2, flags);
575 break;
577 case VECTOR_TYPE:
578 /* The target might allow certain vector types to be compatible. */
579 val = targetm.vector_opaque_p (t1)
580 || targetm.vector_opaque_p (t2)
581 || TYPE_MODE (t1) == TYPE_MODE (t2);
582 break;
584 default:
585 break;
587 return attrval == 2 && val == 1 ? 2 : val;
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
592 to 1 or 0 depending if the check of the pointer types is meant to
593 be reflexive or not (typically, assignments are not reflexive,
594 while comparisons are reflexive).
597 static int
598 comp_target_types (tree ttl, tree ttr, int reflexive)
600 int val;
602 /* Give objc_comptypes a crack at letting these types through. */
603 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
604 return val;
606 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
607 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
609 if (val == 2 && pedantic)
610 pedwarn ("types are not quite compatible");
611 return val;
614 /* Subroutines of `comptypes'. */
616 /* Determine whether two trees derive from the same translation unit.
617 If the CONTEXT chain ends in a null, that tree's context is still
618 being parsed, so if two trees have context chains ending in null,
619 they're in the same translation unit. */
621 same_translation_unit_p (tree t1, tree t2)
623 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
624 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
626 case 'd': t1 = DECL_CONTEXT (t1); break;
627 case 't': t1 = TYPE_CONTEXT (t1); break;
628 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
629 default: abort ();
632 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
633 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
635 case 'd': t2 = DECL_CONTEXT (t2); break;
636 case 't': t2 = TYPE_CONTEXT (t2); break;
637 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
638 default: abort ();
641 return t1 == t2;
644 /* The C standard says that two structures in different translation
645 units are compatible with each other only if the types of their
646 fields are compatible (among other things). So, consider two copies
647 of this structure: */
649 struct tagged_tu_seen {
650 const struct tagged_tu_seen * next;
651 tree t1;
652 tree t2;
655 /* Can they be compatible with each other? We choose to break the
656 recursion by allowing those types to be compatible. */
658 static const struct tagged_tu_seen * tagged_tu_seen_base;
660 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
661 compatible. If the two types are not the same (which has been
662 checked earlier), this can only happen when multiple translation
663 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
664 rules. */
666 static int
667 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
669 tree s1, s2;
670 bool needs_warning = false;
672 /* We have to verify that the tags of the types are the same. This
673 is harder than it looks because this may be a typedef, so we have
674 to go look at the original type. It may even be a typedef of a
675 typedef... */
676 while (TYPE_NAME (t1)
677 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
678 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
679 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
681 while (TYPE_NAME (t2)
682 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
683 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
684 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
686 /* C90 didn't have the requirement that the two tags be the same. */
687 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
688 return 0;
690 /* C90 didn't say what happened if one or both of the types were
691 incomplete; we choose to follow C99 rules here, which is that they
692 are compatible. */
693 if (TYPE_SIZE (t1) == NULL
694 || TYPE_SIZE (t2) == NULL)
695 return 1;
698 const struct tagged_tu_seen * tts_i;
699 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
700 if (tts_i->t1 == t1 && tts_i->t2 == t2)
701 return 1;
704 switch (TREE_CODE (t1))
706 case ENUMERAL_TYPE:
709 /* Speed up the case where the type values are in the same order. */
710 tree tv1 = TYPE_VALUES (t1);
711 tree tv2 = TYPE_VALUES (t2);
713 if (tv1 == tv2)
714 return 1;
716 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
718 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
719 break;
720 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
721 return 0;
724 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
725 return 1;
726 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
727 return 0;
729 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
730 return 0;
732 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
734 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
735 if (s2 == NULL
736 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
737 return 0;
739 return 1;
742 case UNION_TYPE:
744 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
745 return 0;
747 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
749 bool ok = false;
750 struct tagged_tu_seen tts;
752 tts.next = tagged_tu_seen_base;
753 tts.t1 = t1;
754 tts.t2 = t2;
755 tagged_tu_seen_base = &tts;
757 if (DECL_NAME (s1) != NULL)
758 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
759 if (DECL_NAME (s1) == DECL_NAME (s2))
761 int result;
762 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
763 if (result == 0)
764 break;
765 if (result == 2)
766 needs_warning = true;
768 if (TREE_CODE (s1) == FIELD_DECL
769 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
770 DECL_FIELD_BIT_OFFSET (s2)) != 1)
771 break;
773 ok = true;
774 break;
776 tagged_tu_seen_base = tts.next;
777 if (! ok)
778 return 0;
780 return needs_warning ? 2 : 1;
783 case RECORD_TYPE:
785 struct tagged_tu_seen tts;
787 tts.next = tagged_tu_seen_base;
788 tts.t1 = t1;
789 tts.t2 = t2;
790 tagged_tu_seen_base = &tts;
792 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
793 s1 && s2;
794 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
796 int result;
797 if (TREE_CODE (s1) != TREE_CODE (s2)
798 || DECL_NAME (s1) != DECL_NAME (s2))
799 break;
800 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
801 if (result == 0)
802 break;
803 if (result == 2)
804 needs_warning = true;
806 if (TREE_CODE (s1) == FIELD_DECL
807 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
808 DECL_FIELD_BIT_OFFSET (s2)) != 1)
809 break;
811 tagged_tu_seen_base = tts.next;
812 if (s1 && s2)
813 return 0;
814 return needs_warning ? 2 : 1;
817 default:
818 abort ();
822 /* Return 1 if two function types F1 and F2 are compatible.
823 If either type specifies no argument types,
824 the other must specify a fixed number of self-promoting arg types.
825 Otherwise, if one type specifies only the number of arguments,
826 the other must specify that number of self-promoting arg types.
827 Otherwise, the argument types must match. */
829 static int
830 function_types_compatible_p (tree f1, tree f2, int flags)
832 tree args1, args2;
833 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
834 int val = 1;
835 int val1;
836 tree ret1, ret2;
838 ret1 = TREE_TYPE (f1);
839 ret2 = TREE_TYPE (f2);
841 /* 'volatile' qualifiers on a function's return type mean the function
842 is noreturn. */
843 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
844 pedwarn ("function return types not compatible due to `volatile'");
845 if (TYPE_VOLATILE (ret1))
846 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
847 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
848 if (TYPE_VOLATILE (ret2))
849 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
850 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
851 val = comptypes (ret1, ret2, flags);
852 if (val == 0)
853 return 0;
855 args1 = TYPE_ARG_TYPES (f1);
856 args2 = TYPE_ARG_TYPES (f2);
858 /* An unspecified parmlist matches any specified parmlist
859 whose argument types don't need default promotions. */
861 if (args1 == 0)
863 if (!self_promoting_args_p (args2))
864 return 0;
865 /* If one of these types comes from a non-prototype fn definition,
866 compare that with the other type's arglist.
867 If they don't match, ask for a warning (but no error). */
868 if (TYPE_ACTUAL_ARG_TYPES (f1)
869 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
870 flags))
871 val = 2;
872 return val;
874 if (args2 == 0)
876 if (!self_promoting_args_p (args1))
877 return 0;
878 if (TYPE_ACTUAL_ARG_TYPES (f2)
879 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
880 flags))
881 val = 2;
882 return val;
885 /* Both types have argument lists: compare them and propagate results. */
886 val1 = type_lists_compatible_p (args1, args2, flags);
887 return val1 != 1 ? val1 : val;
890 /* Check two lists of types for compatibility,
891 returning 0 for incompatible, 1 for compatible,
892 or 2 for compatible with warning. */
894 static int
895 type_lists_compatible_p (tree args1, tree args2, int flags)
897 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
898 int val = 1;
899 int newval = 0;
901 while (1)
903 if (args1 == 0 && args2 == 0)
904 return val;
905 /* If one list is shorter than the other,
906 they fail to match. */
907 if (args1 == 0 || args2 == 0)
908 return 0;
909 /* A null pointer instead of a type
910 means there is supposed to be an argument
911 but nothing is specified about what type it has.
912 So match anything that self-promotes. */
913 if (TREE_VALUE (args1) == 0)
915 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
916 return 0;
918 else if (TREE_VALUE (args2) == 0)
920 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
921 return 0;
923 /* If one of the lists has an error marker, ignore this arg. */
924 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
925 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
927 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
928 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
929 flags)))
931 /* Allow wait (union {union wait *u; int *i} *)
932 and wait (union wait *) to be compatible. */
933 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
934 && (TYPE_NAME (TREE_VALUE (args1)) == 0
935 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
936 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
937 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
938 TYPE_SIZE (TREE_VALUE (args2))))
940 tree memb;
941 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
942 memb; memb = TREE_CHAIN (memb))
943 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
944 flags))
945 break;
946 if (memb == 0)
947 return 0;
949 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
950 && (TYPE_NAME (TREE_VALUE (args2)) == 0
951 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
952 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
953 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
954 TYPE_SIZE (TREE_VALUE (args1))))
956 tree memb;
957 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
958 memb; memb = TREE_CHAIN (memb))
959 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
960 flags))
961 break;
962 if (memb == 0)
963 return 0;
965 else
966 return 0;
969 /* comptypes said ok, but record if it said to warn. */
970 if (newval > val)
971 val = newval;
973 args1 = TREE_CHAIN (args1);
974 args2 = TREE_CHAIN (args2);
978 /* Compute the size to increment a pointer by. */
980 tree
981 c_size_in_bytes (tree type)
983 enum tree_code code = TREE_CODE (type);
985 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
986 return size_one_node;
988 if (!COMPLETE_OR_VOID_TYPE_P (type))
990 error ("arithmetic on pointer to an incomplete type");
991 return size_one_node;
994 /* Convert in case a char is more than one unit. */
995 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
996 size_int (TYPE_PRECISION (char_type_node)
997 / BITS_PER_UNIT));
1000 /* Return either DECL or its known constant value (if it has one). */
1002 tree
1003 decl_constant_value (tree decl)
1005 if (/* Don't change a variable array bound or initial value to a constant
1006 in a place where a variable is invalid. */
1007 current_function_decl != 0
1008 && ! TREE_THIS_VOLATILE (decl)
1009 && TREE_READONLY (decl)
1010 && DECL_INITIAL (decl) != 0
1011 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1012 /* This is invalid if initial value is not constant.
1013 If it has either a function call, a memory reference,
1014 or a variable, then re-evaluating it could give different results. */
1015 && TREE_CONSTANT (DECL_INITIAL (decl))
1016 /* Check for cases where this is sub-optimal, even though valid. */
1017 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1018 return DECL_INITIAL (decl);
1019 return decl;
1022 /* Return either DECL or its known constant value (if it has one), but
1023 return DECL if pedantic or DECL has mode BLKmode. This is for
1024 bug-compatibility with the old behavior of decl_constant_value
1025 (before GCC 3.0); every use of this function is a bug and it should
1026 be removed before GCC 3.1. It is not appropriate to use pedantic
1027 in a way that affects optimization, and BLKmode is probably not the
1028 right test for avoiding misoptimizations either. */
1030 static tree
1031 decl_constant_value_for_broken_optimization (tree decl)
1033 if (pedantic || DECL_MODE (decl) == BLKmode)
1034 return decl;
1035 else
1036 return decl_constant_value (decl);
1040 /* Perform the default conversion of arrays and functions to pointers.
1041 Return the result of converting EXP. For any other expression, just
1042 return EXP. */
1044 static tree
1045 default_function_array_conversion (tree exp)
1047 tree orig_exp;
1048 tree type = TREE_TYPE (exp);
1049 enum tree_code code = TREE_CODE (type);
1050 int not_lvalue = 0;
1052 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1053 an lvalue.
1055 Do not use STRIP_NOPS here! It will remove conversions from pointer
1056 to integer and cause infinite recursion. */
1057 orig_exp = exp;
1058 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1059 || (TREE_CODE (exp) == NOP_EXPR
1060 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1062 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1063 not_lvalue = 1;
1064 exp = TREE_OPERAND (exp, 0);
1067 /* Preserve the original expression code. */
1068 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1069 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1071 if (code == FUNCTION_TYPE)
1073 return build_unary_op (ADDR_EXPR, exp, 0);
1075 if (code == ARRAY_TYPE)
1077 tree adr;
1078 tree restype = TREE_TYPE (type);
1079 tree ptrtype;
1080 int constp = 0;
1081 int volatilep = 0;
1082 int lvalue_array_p;
1084 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1086 constp = TREE_READONLY (exp);
1087 volatilep = TREE_THIS_VOLATILE (exp);
1090 if (TYPE_QUALS (type) || constp || volatilep)
1091 restype
1092 = c_build_qualified_type (restype,
1093 TYPE_QUALS (type)
1094 | (constp * TYPE_QUAL_CONST)
1095 | (volatilep * TYPE_QUAL_VOLATILE));
1097 if (TREE_CODE (exp) == INDIRECT_REF)
1098 return convert (TYPE_POINTER_TO (restype),
1099 TREE_OPERAND (exp, 0));
1101 if (TREE_CODE (exp) == COMPOUND_EXPR)
1103 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1104 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1105 TREE_OPERAND (exp, 0), op1);
1108 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1109 if (!flag_isoc99 && !lvalue_array_p)
1111 /* Before C99, non-lvalue arrays do not decay to pointers.
1112 Normally, using such an array would be invalid; but it can
1113 be used correctly inside sizeof or as a statement expression.
1114 Thus, do not give an error here; an error will result later. */
1115 return exp;
1118 ptrtype = build_pointer_type (restype);
1120 if (TREE_CODE (exp) == VAR_DECL)
1122 /* ??? This is not really quite correct
1123 in that the type of the operand of ADDR_EXPR
1124 is not the target type of the type of the ADDR_EXPR itself.
1125 Question is, can this lossage be avoided? */
1126 adr = build1 (ADDR_EXPR, ptrtype, exp);
1127 if (!c_mark_addressable (exp))
1128 return error_mark_node;
1129 TREE_CONSTANT (adr) = staticp (exp);
1130 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1131 return adr;
1133 /* This way is better for a COMPONENT_REF since it can
1134 simplify the offset for a component. */
1135 adr = build_unary_op (ADDR_EXPR, exp, 1);
1136 return convert (ptrtype, adr);
1138 return exp;
1141 /* Perform default promotions for C data used in expressions.
1142 Arrays and functions are converted to pointers;
1143 enumeral types or short or char, to int.
1144 In addition, manifest constants symbols are replaced by their values. */
1146 tree
1147 default_conversion (tree exp)
1149 tree orig_exp;
1150 tree type = TREE_TYPE (exp);
1151 enum tree_code code = TREE_CODE (type);
1153 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1154 return default_function_array_conversion (exp);
1156 /* Constants can be used directly unless they're not loadable. */
1157 if (TREE_CODE (exp) == CONST_DECL)
1158 exp = DECL_INITIAL (exp);
1160 /* Replace a nonvolatile const static variable with its value unless
1161 it is an array, in which case we must be sure that taking the
1162 address of the array produces consistent results. */
1163 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1165 exp = decl_constant_value_for_broken_optimization (exp);
1166 type = TREE_TYPE (exp);
1169 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1170 an lvalue.
1172 Do not use STRIP_NOPS here! It will remove conversions from pointer
1173 to integer and cause infinite recursion. */
1174 orig_exp = exp;
1175 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1176 || (TREE_CODE (exp) == NOP_EXPR
1177 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1178 exp = TREE_OPERAND (exp, 0);
1180 /* Preserve the original expression code. */
1181 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1182 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1184 /* Normally convert enums to int,
1185 but convert wide enums to something wider. */
1186 if (code == ENUMERAL_TYPE)
1188 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1189 TYPE_PRECISION (integer_type_node)),
1190 ((TYPE_PRECISION (type)
1191 >= TYPE_PRECISION (integer_type_node))
1192 && TREE_UNSIGNED (type)));
1194 return convert (type, exp);
1197 if (TREE_CODE (exp) == COMPONENT_REF
1198 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1199 /* If it's thinner than an int, promote it like a
1200 c_promoting_integer_type_p, otherwise leave it alone. */
1201 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1202 TYPE_PRECISION (integer_type_node)))
1203 return convert (integer_type_node, exp);
1205 if (c_promoting_integer_type_p (type))
1207 /* Preserve unsignedness if not really getting any wider. */
1208 if (TREE_UNSIGNED (type)
1209 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1210 return convert (unsigned_type_node, exp);
1212 return convert (integer_type_node, exp);
1215 if (code == VOID_TYPE)
1217 error ("void value not ignored as it ought to be");
1218 return error_mark_node;
1220 return exp;
1223 /* Look up COMPONENT in a structure or union DECL.
1225 If the component name is not found, returns NULL_TREE. Otherwise,
1226 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1227 stepping down the chain to the component, which is in the last
1228 TREE_VALUE of the list. Normally the list is of length one, but if
1229 the component is embedded within (nested) anonymous structures or
1230 unions, the list steps down the chain to the component. */
1232 static tree
1233 lookup_field (tree decl, tree component)
1235 tree type = TREE_TYPE (decl);
1236 tree field;
1238 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1239 to the field elements. Use a binary search on this array to quickly
1240 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1241 will always be set for structures which have many elements. */
1243 if (TYPE_LANG_SPECIFIC (type))
1245 int bot, top, half;
1246 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1248 field = TYPE_FIELDS (type);
1249 bot = 0;
1250 top = TYPE_LANG_SPECIFIC (type)->s->len;
1251 while (top - bot > 1)
1253 half = (top - bot + 1) >> 1;
1254 field = field_array[bot+half];
1256 if (DECL_NAME (field) == NULL_TREE)
1258 /* Step through all anon unions in linear fashion. */
1259 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1261 field = field_array[bot++];
1262 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1263 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1265 tree anon = lookup_field (field, component);
1267 if (anon)
1268 return tree_cons (NULL_TREE, field, anon);
1272 /* Entire record is only anon unions. */
1273 if (bot > top)
1274 return NULL_TREE;
1276 /* Restart the binary search, with new lower bound. */
1277 continue;
1280 if (DECL_NAME (field) == component)
1281 break;
1282 if (DECL_NAME (field) < component)
1283 bot += half;
1284 else
1285 top = bot + half;
1288 if (DECL_NAME (field_array[bot]) == component)
1289 field = field_array[bot];
1290 else if (DECL_NAME (field) != component)
1291 return NULL_TREE;
1293 else
1295 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1297 if (DECL_NAME (field) == NULL_TREE
1298 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1299 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1301 tree anon = lookup_field (field, component);
1303 if (anon)
1304 return tree_cons (NULL_TREE, field, anon);
1307 if (DECL_NAME (field) == component)
1308 break;
1311 if (field == NULL_TREE)
1312 return NULL_TREE;
1315 return tree_cons (NULL_TREE, field, NULL_TREE);
1318 /* Make an expression to refer to the COMPONENT field of
1319 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1321 tree
1322 build_component_ref (tree datum, tree component)
1324 tree type = TREE_TYPE (datum);
1325 enum tree_code code = TREE_CODE (type);
1326 tree field = NULL;
1327 tree ref;
1329 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1330 Ensure that the arguments are not lvalues; otherwise,
1331 if the component is an array, it would wrongly decay to a pointer in
1332 C89 mode.
1333 We cannot do this with a COND_EXPR, because in a conditional expression
1334 the default promotions are applied to both sides, and this would yield
1335 the wrong type of the result; for example, if the components have
1336 type "char". */
1337 switch (TREE_CODE (datum))
1339 case COMPOUND_EXPR:
1341 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1342 return build (COMPOUND_EXPR, TREE_TYPE (value),
1343 TREE_OPERAND (datum, 0), non_lvalue (value));
1345 default:
1346 break;
1349 /* See if there is a field or component with name COMPONENT. */
1351 if (code == RECORD_TYPE || code == UNION_TYPE)
1353 if (!COMPLETE_TYPE_P (type))
1355 c_incomplete_type_error (NULL_TREE, type);
1356 return error_mark_node;
1359 field = lookup_field (datum, component);
1361 if (!field)
1363 error ("%s has no member named `%s'",
1364 code == RECORD_TYPE ? "structure" : "union",
1365 IDENTIFIER_POINTER (component));
1366 return error_mark_node;
1369 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1370 This might be better solved in future the way the C++ front
1371 end does it - by giving the anonymous entities each a
1372 separate name and type, and then have build_component_ref
1373 recursively call itself. We can't do that here. */
1376 tree subdatum = TREE_VALUE (field);
1378 if (TREE_TYPE (subdatum) == error_mark_node)
1379 return error_mark_node;
1381 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1382 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1383 TREE_READONLY (ref) = 1;
1384 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1385 TREE_THIS_VOLATILE (ref) = 1;
1387 if (TREE_DEPRECATED (subdatum))
1388 warn_deprecated_use (subdatum);
1390 datum = ref;
1392 field = TREE_CHAIN (field);
1394 while (field);
1396 return ref;
1398 else if (code != ERROR_MARK)
1399 error ("request for member `%s' in something not a structure or union",
1400 IDENTIFIER_POINTER (component));
1402 return error_mark_node;
1405 /* Given an expression PTR for a pointer, return an expression
1406 for the value pointed to.
1407 ERRORSTRING is the name of the operator to appear in error messages. */
1409 tree
1410 build_indirect_ref (tree ptr, const char *errorstring)
1412 tree pointer = default_conversion (ptr);
1413 tree type = TREE_TYPE (pointer);
1415 if (TREE_CODE (type) == POINTER_TYPE)
1417 if (TREE_CODE (pointer) == ADDR_EXPR
1418 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1419 == TREE_TYPE (type)))
1420 return TREE_OPERAND (pointer, 0);
1421 else
1423 tree t = TREE_TYPE (type);
1424 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1426 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1428 error ("dereferencing pointer to incomplete type");
1429 return error_mark_node;
1431 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1432 warning ("dereferencing `void *' pointer");
1434 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1435 so that we get the proper error message if the result is used
1436 to assign to. Also, &* is supposed to be a no-op.
1437 And ANSI C seems to specify that the type of the result
1438 should be the const type. */
1439 /* A de-reference of a pointer to const is not a const. It is valid
1440 to change it via some other pointer. */
1441 TREE_READONLY (ref) = TYPE_READONLY (t);
1442 TREE_SIDE_EFFECTS (ref)
1443 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1444 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1445 return ref;
1448 else if (TREE_CODE (pointer) != ERROR_MARK)
1449 error ("invalid type argument of `%s'", errorstring);
1450 return error_mark_node;
1453 /* This handles expressions of the form "a[i]", which denotes
1454 an array reference.
1456 This is logically equivalent in C to *(a+i), but we may do it differently.
1457 If A is a variable or a member, we generate a primitive ARRAY_REF.
1458 This avoids forcing the array out of registers, and can work on
1459 arrays that are not lvalues (for example, members of structures returned
1460 by functions). */
1462 tree
1463 build_array_ref (tree array, tree index)
1465 if (index == 0)
1467 error ("subscript missing in array reference");
1468 return error_mark_node;
1471 if (TREE_TYPE (array) == error_mark_node
1472 || TREE_TYPE (index) == error_mark_node)
1473 return error_mark_node;
1475 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1476 && TREE_CODE (array) != INDIRECT_REF)
1478 tree rval, type;
1480 /* Subscripting with type char is likely to lose
1481 on a machine where chars are signed.
1482 So warn on any machine, but optionally.
1483 Don't warn for unsigned char since that type is safe.
1484 Don't warn for signed char because anyone who uses that
1485 must have done so deliberately. */
1486 if (warn_char_subscripts
1487 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1488 warning ("array subscript has type `char'");
1490 /* Apply default promotions *after* noticing character types. */
1491 index = default_conversion (index);
1493 /* Require integer *after* promotion, for sake of enums. */
1494 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1496 error ("array subscript is not an integer");
1497 return error_mark_node;
1500 /* An array that is indexed by a non-constant
1501 cannot be stored in a register; we must be able to do
1502 address arithmetic on its address.
1503 Likewise an array of elements of variable size. */
1504 if (TREE_CODE (index) != INTEGER_CST
1505 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1506 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1508 if (!c_mark_addressable (array))
1509 return error_mark_node;
1511 /* An array that is indexed by a constant value which is not within
1512 the array bounds cannot be stored in a register either; because we
1513 would get a crash in store_bit_field/extract_bit_field when trying
1514 to access a non-existent part of the register. */
1515 if (TREE_CODE (index) == INTEGER_CST
1516 && TYPE_DOMAIN (TREE_TYPE (array))
1517 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1519 if (!c_mark_addressable (array))
1520 return error_mark_node;
1523 if (pedantic)
1525 tree foo = array;
1526 while (TREE_CODE (foo) == COMPONENT_REF)
1527 foo = TREE_OPERAND (foo, 0);
1528 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1529 pedwarn ("ISO C forbids subscripting `register' array");
1530 else if (! flag_isoc99 && ! lvalue_p (foo))
1531 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1534 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1535 rval = build (ARRAY_REF, type, array, index);
1536 /* Array ref is const/volatile if the array elements are
1537 or if the array is. */
1538 TREE_READONLY (rval)
1539 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1540 | TREE_READONLY (array));
1541 TREE_SIDE_EFFECTS (rval)
1542 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1543 | TREE_SIDE_EFFECTS (array));
1544 TREE_THIS_VOLATILE (rval)
1545 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1546 /* This was added by rms on 16 Nov 91.
1547 It fixes vol struct foo *a; a->elts[1]
1548 in an inline function.
1549 Hope it doesn't break something else. */
1550 | TREE_THIS_VOLATILE (array));
1551 return require_complete_type (fold (rval));
1555 tree ar = default_conversion (array);
1556 tree ind = default_conversion (index);
1558 /* Do the same warning check as above, but only on the part that's
1559 syntactically the index and only if it is also semantically
1560 the index. */
1561 if (warn_char_subscripts
1562 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1563 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1564 warning ("subscript has type `char'");
1566 /* Put the integer in IND to simplify error checking. */
1567 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1569 tree temp = ar;
1570 ar = ind;
1571 ind = temp;
1574 if (ar == error_mark_node)
1575 return ar;
1577 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1578 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1580 error ("subscripted value is neither array nor pointer");
1581 return error_mark_node;
1583 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1585 error ("array subscript is not an integer");
1586 return error_mark_node;
1589 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1590 "array indexing");
1594 /* Build an external reference to identifier ID. FUN indicates
1595 whether this will be used for a function call. */
1596 tree
1597 build_external_ref (tree id, int fun)
1599 tree ref;
1600 tree decl = lookup_name (id);
1601 tree objc_ivar = lookup_objc_ivar (id);
1603 if (decl && decl != error_mark_node)
1605 /* Properly declared variable or function reference. */
1606 if (!objc_ivar)
1607 ref = decl;
1608 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1610 warning ("local declaration of `%s' hides instance variable",
1611 IDENTIFIER_POINTER (id));
1612 ref = decl;
1614 else
1615 ref = objc_ivar;
1617 else if (objc_ivar)
1618 ref = objc_ivar;
1619 else if (fun)
1620 /* Implicit function declaration. */
1621 ref = implicitly_declare (id);
1622 else if (decl == error_mark_node)
1623 /* Don't complain about something that's already been
1624 complained about. */
1625 return error_mark_node;
1626 else
1628 undeclared_variable (id);
1629 return error_mark_node;
1632 if (TREE_TYPE (ref) == error_mark_node)
1633 return error_mark_node;
1635 if (TREE_DEPRECATED (ref))
1636 warn_deprecated_use (ref);
1638 if (!skip_evaluation)
1639 assemble_external (ref);
1640 TREE_USED (ref) = 1;
1642 if (TREE_CODE (ref) == CONST_DECL)
1644 ref = DECL_INITIAL (ref);
1645 TREE_CONSTANT (ref) = 1;
1647 else if (current_function_decl != 0
1648 && !DECL_FILE_SCOPE_P (current_function_decl)
1649 && (TREE_CODE (ref) == VAR_DECL
1650 || TREE_CODE (ref) == PARM_DECL
1651 || TREE_CODE (ref) == FUNCTION_DECL))
1653 tree context = decl_function_context (ref);
1655 if (context != 0 && context != current_function_decl)
1656 DECL_NONLOCAL (ref) = 1;
1659 return ref;
1662 /* Build a function call to function FUNCTION with parameters PARAMS.
1663 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1664 TREE_VALUE of each node is a parameter-expression.
1665 FUNCTION's data type may be a function type or a pointer-to-function. */
1667 tree
1668 build_function_call (tree function, tree params)
1670 tree fntype, fundecl = 0;
1671 tree coerced_params;
1672 tree name = NULL_TREE, result;
1673 tree tem;
1675 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1676 STRIP_TYPE_NOPS (function);
1678 /* Convert anything with function type to a pointer-to-function. */
1679 if (TREE_CODE (function) == FUNCTION_DECL)
1681 name = DECL_NAME (function);
1683 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1684 (because calling an inline function does not mean the function
1685 needs to be separately compiled). */
1686 fntype = build_type_variant (TREE_TYPE (function),
1687 TREE_READONLY (function),
1688 TREE_THIS_VOLATILE (function));
1689 fundecl = function;
1690 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1692 else
1693 function = default_conversion (function);
1695 fntype = TREE_TYPE (function);
1697 if (TREE_CODE (fntype) == ERROR_MARK)
1698 return error_mark_node;
1700 if (!(TREE_CODE (fntype) == POINTER_TYPE
1701 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1703 error ("called object is not a function");
1704 return error_mark_node;
1707 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1708 current_function_returns_abnormally = 1;
1710 /* fntype now gets the type of function pointed to. */
1711 fntype = TREE_TYPE (fntype);
1713 /* Check that the function is called through a compatible prototype.
1714 If it is not, replace the call by a trap, wrapped up in a compound
1715 expression if necessary. This has the nice side-effect to prevent
1716 the tree-inliner from generating invalid assignment trees which may
1717 blow up in the RTL expander later.
1719 ??? This doesn't work for Objective-C because objc_comptypes
1720 refuses to compare function prototypes, yet the compiler appears
1721 to build calls that are flagged as invalid by C's comptypes. */
1722 if (! c_dialect_objc ()
1723 && TREE_CODE (function) == NOP_EXPR
1724 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1725 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1726 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1728 tree return_type = TREE_TYPE (fntype);
1729 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1730 NULL_TREE);
1732 /* This situation leads to run-time undefined behavior. We can't,
1733 therefore, simply error unless we can prove that all possible
1734 executions of the program must execute the code. */
1735 warning ("function called through a non-compatible type");
1737 /* We can, however, treat "undefined" any way we please.
1738 Call abort to encourage the user to fix the program. */
1739 inform ("if this code is reached, the program will abort");
1741 if (VOID_TYPE_P (return_type))
1742 return trap;
1743 else
1745 tree rhs;
1747 if (AGGREGATE_TYPE_P (return_type))
1748 rhs = build_compound_literal (return_type,
1749 build_constructor (return_type,
1750 NULL_TREE));
1751 else
1752 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1754 return build (COMPOUND_EXPR, return_type, trap, rhs);
1758 /* Convert the parameters to the types declared in the
1759 function prototype, or apply default promotions. */
1761 coerced_params
1762 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1764 /* Check that the arguments to the function are valid. */
1766 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1768 /* Recognize certain built-in functions so we can make tree-codes
1769 other than CALL_EXPR. We do this when it enables fold-const.c
1770 to do something useful. */
1772 if (TREE_CODE (function) == ADDR_EXPR
1773 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1774 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1776 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1777 params, coerced_params);
1778 if (result)
1779 return result;
1782 result = build (CALL_EXPR, TREE_TYPE (fntype),
1783 function, coerced_params, NULL_TREE);
1784 TREE_SIDE_EFFECTS (result) = 1;
1785 result = fold (result);
1787 if (VOID_TYPE_P (TREE_TYPE (result)))
1788 return result;
1789 return require_complete_type (result);
1792 /* Convert the argument expressions in the list VALUES
1793 to the types in the list TYPELIST. The result is a list of converted
1794 argument expressions.
1796 If TYPELIST is exhausted, or when an element has NULL as its type,
1797 perform the default conversions.
1799 PARMLIST is the chain of parm decls for the function being called.
1800 It may be 0, if that info is not available.
1801 It is used only for generating error messages.
1803 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1805 This is also where warnings about wrong number of args are generated.
1807 Both VALUES and the returned value are chains of TREE_LIST nodes
1808 with the elements of the list in the TREE_VALUE slots of those nodes. */
1810 static tree
1811 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1813 tree typetail, valtail;
1814 tree result = NULL;
1815 int parmnum;
1817 /* Scan the given expressions and types, producing individual
1818 converted arguments and pushing them on RESULT in reverse order. */
1820 for (valtail = values, typetail = typelist, parmnum = 0;
1821 valtail;
1822 valtail = TREE_CHAIN (valtail), parmnum++)
1824 tree type = typetail ? TREE_VALUE (typetail) : 0;
1825 tree val = TREE_VALUE (valtail);
1827 if (type == void_type_node)
1829 if (name)
1830 error ("too many arguments to function `%s'",
1831 IDENTIFIER_POINTER (name));
1832 else
1833 error ("too many arguments to function");
1834 break;
1837 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1838 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1839 to convert automatically to a pointer. */
1840 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1841 val = TREE_OPERAND (val, 0);
1843 val = default_function_array_conversion (val);
1845 val = require_complete_type (val);
1847 if (type != 0)
1849 /* Formal parm type is specified by a function prototype. */
1850 tree parmval;
1852 if (!COMPLETE_TYPE_P (type))
1854 error ("type of formal parameter %d is incomplete", parmnum + 1);
1855 parmval = val;
1857 else
1859 /* Optionally warn about conversions that
1860 differ from the default conversions. */
1861 if (warn_conversion || warn_traditional)
1863 int formal_prec = TYPE_PRECISION (type);
1865 if (INTEGRAL_TYPE_P (type)
1866 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1867 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1868 if (INTEGRAL_TYPE_P (type)
1869 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1870 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1871 else if (TREE_CODE (type) == COMPLEX_TYPE
1872 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1873 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1874 else if (TREE_CODE (type) == REAL_TYPE
1875 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1876 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1877 else if (TREE_CODE (type) == COMPLEX_TYPE
1878 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1879 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1880 else if (TREE_CODE (type) == REAL_TYPE
1881 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1882 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1883 /* ??? At some point, messages should be written about
1884 conversions between complex types, but that's too messy
1885 to do now. */
1886 else if (TREE_CODE (type) == REAL_TYPE
1887 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1889 /* Warn if any argument is passed as `float',
1890 since without a prototype it would be `double'. */
1891 if (formal_prec == TYPE_PRECISION (float_type_node))
1892 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1894 /* Detect integer changing in width or signedness.
1895 These warnings are only activated with
1896 -Wconversion, not with -Wtraditional. */
1897 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1898 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1900 tree would_have_been = default_conversion (val);
1901 tree type1 = TREE_TYPE (would_have_been);
1903 if (TREE_CODE (type) == ENUMERAL_TYPE
1904 && (TYPE_MAIN_VARIANT (type)
1905 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1906 /* No warning if function asks for enum
1907 and the actual arg is that enum type. */
1909 else if (formal_prec != TYPE_PRECISION (type1))
1910 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1911 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1913 /* Don't complain if the formal parameter type
1914 is an enum, because we can't tell now whether
1915 the value was an enum--even the same enum. */
1916 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1918 else if (TREE_CODE (val) == INTEGER_CST
1919 && int_fits_type_p (val, type))
1920 /* Change in signedness doesn't matter
1921 if a constant value is unaffected. */
1923 /* Likewise for a constant in a NOP_EXPR. */
1924 else if (TREE_CODE (val) == NOP_EXPR
1925 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1926 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1928 /* If the value is extended from a narrower
1929 unsigned type, it doesn't matter whether we
1930 pass it as signed or unsigned; the value
1931 certainly is the same either way. */
1932 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1933 && TREE_UNSIGNED (TREE_TYPE (val)))
1935 else if (TREE_UNSIGNED (type))
1936 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1937 else
1938 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1942 parmval = convert_for_assignment (type, val,
1943 (char *) 0, /* arg passing */
1944 fundecl, name, parmnum + 1);
1946 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1947 && INTEGRAL_TYPE_P (type)
1948 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1949 parmval = default_conversion (parmval);
1951 result = tree_cons (NULL_TREE, parmval, result);
1953 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1954 && (TYPE_PRECISION (TREE_TYPE (val))
1955 < TYPE_PRECISION (double_type_node)))
1956 /* Convert `float' to `double'. */
1957 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1958 else
1959 /* Convert `short' and `char' to full-size `int'. */
1960 result = tree_cons (NULL_TREE, default_conversion (val), result);
1962 if (typetail)
1963 typetail = TREE_CHAIN (typetail);
1966 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1968 if (name)
1969 error ("too few arguments to function `%s'",
1970 IDENTIFIER_POINTER (name));
1971 else
1972 error ("too few arguments to function");
1975 return nreverse (result);
1978 /* This is the entry point used by the parser
1979 for binary operators in the input.
1980 In addition to constructing the expression,
1981 we check for operands that were written with other binary operators
1982 in a way that is likely to confuse the user. */
1984 tree
1985 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1987 tree result = build_binary_op (code, arg1, arg2, 1);
1989 char class;
1990 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1991 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1992 enum tree_code code1 = ERROR_MARK;
1993 enum tree_code code2 = ERROR_MARK;
1995 if (TREE_CODE (result) == ERROR_MARK)
1996 return error_mark_node;
1998 if (IS_EXPR_CODE_CLASS (class1))
1999 code1 = C_EXP_ORIGINAL_CODE (arg1);
2000 if (IS_EXPR_CODE_CLASS (class2))
2001 code2 = C_EXP_ORIGINAL_CODE (arg2);
2003 /* Check for cases such as x+y<<z which users are likely
2004 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2005 is cleared to prevent these warnings. */
2006 if (warn_parentheses)
2008 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2010 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2011 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2012 warning ("suggest parentheses around + or - inside shift");
2015 if (code == TRUTH_ORIF_EXPR)
2017 if (code1 == TRUTH_ANDIF_EXPR
2018 || code2 == TRUTH_ANDIF_EXPR)
2019 warning ("suggest parentheses around && within ||");
2022 if (code == BIT_IOR_EXPR)
2024 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2025 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2026 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2027 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2028 warning ("suggest parentheses around arithmetic in operand of |");
2029 /* Check cases like x|y==z */
2030 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2031 warning ("suggest parentheses around comparison in operand of |");
2034 if (code == BIT_XOR_EXPR)
2036 if (code1 == BIT_AND_EXPR
2037 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2038 || code2 == BIT_AND_EXPR
2039 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2040 warning ("suggest parentheses around arithmetic in operand of ^");
2041 /* Check cases like x^y==z */
2042 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2043 warning ("suggest parentheses around comparison in operand of ^");
2046 if (code == BIT_AND_EXPR)
2048 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2049 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2050 warning ("suggest parentheses around + or - in operand of &");
2051 /* Check cases like x&y==z */
2052 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2053 warning ("suggest parentheses around comparison in operand of &");
2057 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2058 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2059 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2060 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2062 unsigned_conversion_warning (result, arg1);
2063 unsigned_conversion_warning (result, arg2);
2064 overflow_warning (result);
2066 class = TREE_CODE_CLASS (TREE_CODE (result));
2068 /* Record the code that was specified in the source,
2069 for the sake of warnings about confusing nesting. */
2070 if (IS_EXPR_CODE_CLASS (class))
2071 C_SET_EXP_ORIGINAL_CODE (result, code);
2072 else
2074 int flag = TREE_CONSTANT (result);
2075 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2076 so that convert_for_assignment wouldn't strip it.
2077 That way, we got warnings for things like p = (1 - 1).
2078 But it turns out we should not get those warnings. */
2079 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2080 C_SET_EXP_ORIGINAL_CODE (result, code);
2081 TREE_CONSTANT (result) = flag;
2084 return result;
2088 /* Return true if `t' is known to be non-negative. */
2091 c_tree_expr_nonnegative_p (tree t)
2093 if (TREE_CODE (t) == STMT_EXPR)
2095 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2097 /* Find the last statement in the chain, ignoring the final
2098 * scope statement */
2099 while (TREE_CHAIN (t) != NULL_TREE
2100 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2101 t = TREE_CHAIN (t);
2102 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2104 return tree_expr_nonnegative_p (t);
2107 /* Return a tree for the difference of pointers OP0 and OP1.
2108 The resulting tree has type int. */
2110 static tree
2111 pointer_diff (tree op0, tree op1)
2113 tree result, folded;
2114 tree restype = ptrdiff_type_node;
2116 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2117 tree con0, con1, lit0, lit1;
2118 tree orig_op1 = op1;
2120 if (pedantic || warn_pointer_arith)
2122 if (TREE_CODE (target_type) == VOID_TYPE)
2123 pedwarn ("pointer of type `void *' used in subtraction");
2124 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2125 pedwarn ("pointer to a function used in subtraction");
2128 /* If the conversion to ptrdiff_type does anything like widening or
2129 converting a partial to an integral mode, we get a convert_expression
2130 that is in the way to do any simplifications.
2131 (fold-const.c doesn't know that the extra bits won't be needed.
2132 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2133 different mode in place.)
2134 So first try to find a common term here 'by hand'; we want to cover
2135 at least the cases that occur in legal static initializers. */
2136 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2137 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2139 if (TREE_CODE (con0) == PLUS_EXPR)
2141 lit0 = TREE_OPERAND (con0, 1);
2142 con0 = TREE_OPERAND (con0, 0);
2144 else
2145 lit0 = integer_zero_node;
2147 if (TREE_CODE (con1) == PLUS_EXPR)
2149 lit1 = TREE_OPERAND (con1, 1);
2150 con1 = TREE_OPERAND (con1, 0);
2152 else
2153 lit1 = integer_zero_node;
2155 if (operand_equal_p (con0, con1, 0))
2157 op0 = lit0;
2158 op1 = lit1;
2162 /* First do the subtraction as integers;
2163 then drop through to build the divide operator.
2164 Do not do default conversions on the minus operator
2165 in case restype is a short type. */
2167 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2168 convert (restype, op1), 0);
2169 /* This generates an error if op1 is pointer to incomplete type. */
2170 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2171 error ("arithmetic on pointer to an incomplete type");
2173 /* This generates an error if op0 is pointer to incomplete type. */
2174 op1 = c_size_in_bytes (target_type);
2176 /* Divide by the size, in easiest possible way. */
2178 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2180 folded = fold (result);
2181 if (folded == result)
2182 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2183 return folded;
2186 /* Construct and perhaps optimize a tree representation
2187 for a unary operation. CODE, a tree_code, specifies the operation
2188 and XARG is the operand.
2189 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2190 the default promotions (such as from short to int).
2191 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2192 allows non-lvalues; this is only used to handle conversion of non-lvalue
2193 arrays to pointers in C99. */
2195 tree
2196 build_unary_op (enum tree_code code, tree xarg, int flag)
2198 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2199 tree arg = xarg;
2200 tree argtype = 0;
2201 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2202 tree val;
2203 int noconvert = flag;
2205 if (typecode == ERROR_MARK)
2206 return error_mark_node;
2207 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2208 typecode = INTEGER_TYPE;
2210 switch (code)
2212 case CONVERT_EXPR:
2213 /* This is used for unary plus, because a CONVERT_EXPR
2214 is enough to prevent anybody from looking inside for
2215 associativity, but won't generate any code. */
2216 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2217 || typecode == COMPLEX_TYPE))
2219 error ("wrong type argument to unary plus");
2220 return error_mark_node;
2222 else if (!noconvert)
2223 arg = default_conversion (arg);
2224 arg = non_lvalue (arg);
2225 break;
2227 case NEGATE_EXPR:
2228 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2229 || typecode == COMPLEX_TYPE
2230 || typecode == VECTOR_TYPE))
2232 error ("wrong type argument to unary minus");
2233 return error_mark_node;
2235 else if (!noconvert)
2236 arg = default_conversion (arg);
2237 break;
2239 case BIT_NOT_EXPR:
2240 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2242 if (!noconvert)
2243 arg = default_conversion (arg);
2245 else if (typecode == COMPLEX_TYPE)
2247 code = CONJ_EXPR;
2248 if (pedantic)
2249 pedwarn ("ISO C does not support `~' for complex conjugation");
2250 if (!noconvert)
2251 arg = default_conversion (arg);
2253 else
2255 error ("wrong type argument to bit-complement");
2256 return error_mark_node;
2258 break;
2260 case ABS_EXPR:
2261 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2263 error ("wrong type argument to abs");
2264 return error_mark_node;
2266 else if (!noconvert)
2267 arg = default_conversion (arg);
2268 break;
2270 case CONJ_EXPR:
2271 /* Conjugating a real value is a no-op, but allow it anyway. */
2272 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2273 || typecode == COMPLEX_TYPE))
2275 error ("wrong type argument to conjugation");
2276 return error_mark_node;
2278 else if (!noconvert)
2279 arg = default_conversion (arg);
2280 break;
2282 case TRUTH_NOT_EXPR:
2283 if (typecode != INTEGER_TYPE
2284 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2285 && typecode != COMPLEX_TYPE
2286 /* These will convert to a pointer. */
2287 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2289 error ("wrong type argument to unary exclamation mark");
2290 return error_mark_node;
2292 arg = lang_hooks.truthvalue_conversion (arg);
2293 return invert_truthvalue (arg);
2295 case NOP_EXPR:
2296 break;
2298 case REALPART_EXPR:
2299 if (TREE_CODE (arg) == COMPLEX_CST)
2300 return TREE_REALPART (arg);
2301 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2302 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2303 else
2304 return arg;
2306 case IMAGPART_EXPR:
2307 if (TREE_CODE (arg) == COMPLEX_CST)
2308 return TREE_IMAGPART (arg);
2309 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2310 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2311 else
2312 return convert (TREE_TYPE (arg), integer_zero_node);
2314 case PREINCREMENT_EXPR:
2315 case POSTINCREMENT_EXPR:
2316 case PREDECREMENT_EXPR:
2317 case POSTDECREMENT_EXPR:
2319 /* Increment or decrement the real part of the value,
2320 and don't change the imaginary part. */
2321 if (typecode == COMPLEX_TYPE)
2323 tree real, imag;
2325 if (pedantic)
2326 pedwarn ("ISO C does not support `++' and `--' on complex types");
2328 arg = stabilize_reference (arg);
2329 real = build_unary_op (REALPART_EXPR, arg, 1);
2330 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2331 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2332 build_unary_op (code, real, 1), imag);
2335 /* Report invalid types. */
2337 if (typecode != POINTER_TYPE
2338 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2340 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2341 error ("wrong type argument to increment");
2342 else
2343 error ("wrong type argument to decrement");
2345 return error_mark_node;
2349 tree inc;
2350 tree result_type = TREE_TYPE (arg);
2352 arg = get_unwidened (arg, 0);
2353 argtype = TREE_TYPE (arg);
2355 /* Compute the increment. */
2357 if (typecode == POINTER_TYPE)
2359 /* If pointer target is an undefined struct,
2360 we just cannot know how to do the arithmetic. */
2361 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2363 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2364 error ("increment of pointer to unknown structure");
2365 else
2366 error ("decrement of pointer to unknown structure");
2368 else if ((pedantic || warn_pointer_arith)
2369 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2370 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2372 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2373 pedwarn ("wrong type argument to increment");
2374 else
2375 pedwarn ("wrong type argument to decrement");
2378 inc = c_size_in_bytes (TREE_TYPE (result_type));
2380 else
2381 inc = integer_one_node;
2383 inc = convert (argtype, inc);
2385 /* Complain about anything else that is not a true lvalue. */
2386 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2387 || code == POSTINCREMENT_EXPR)
2388 ? "invalid lvalue in increment"
2389 : "invalid lvalue in decrement")))
2390 return error_mark_node;
2392 /* Report a read-only lvalue. */
2393 if (TREE_READONLY (arg))
2394 readonly_error (arg,
2395 ((code == PREINCREMENT_EXPR
2396 || code == POSTINCREMENT_EXPR)
2397 ? "increment" : "decrement"));
2399 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2400 val = boolean_increment (code, arg);
2401 else
2402 val = build (code, TREE_TYPE (arg), arg, inc);
2403 TREE_SIDE_EFFECTS (val) = 1;
2404 val = convert (result_type, val);
2405 if (TREE_CODE (val) != code)
2406 TREE_NO_UNUSED_WARNING (val) = 1;
2407 return val;
2410 case ADDR_EXPR:
2411 /* Note that this operation never does default_conversion. */
2413 /* Let &* cancel out to simplify resulting code. */
2414 if (TREE_CODE (arg) == INDIRECT_REF)
2416 /* Don't let this be an lvalue. */
2417 if (lvalue_p (TREE_OPERAND (arg, 0)))
2418 return non_lvalue (TREE_OPERAND (arg, 0));
2419 return TREE_OPERAND (arg, 0);
2422 /* For &x[y], return x+y */
2423 if (TREE_CODE (arg) == ARRAY_REF)
2425 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2426 return error_mark_node;
2427 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2428 TREE_OPERAND (arg, 1), 1);
2431 /* Anything not already handled and not a true memory reference
2432 or a non-lvalue array is an error. */
2433 else if (typecode != FUNCTION_TYPE && !flag
2434 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2435 return error_mark_node;
2437 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2438 argtype = TREE_TYPE (arg);
2440 /* If the lvalue is const or volatile, merge that into the type
2441 to which the address will point. Note that you can't get a
2442 restricted pointer by taking the address of something, so we
2443 only have to deal with `const' and `volatile' here. */
2444 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2445 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2446 argtype = c_build_type_variant (argtype,
2447 TREE_READONLY (arg),
2448 TREE_THIS_VOLATILE (arg));
2450 argtype = build_pointer_type (argtype);
2452 if (!c_mark_addressable (arg))
2453 return error_mark_node;
2456 tree addr;
2458 if (TREE_CODE (arg) == COMPONENT_REF)
2460 tree field = TREE_OPERAND (arg, 1);
2462 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2464 if (DECL_C_BIT_FIELD (field))
2466 error ("attempt to take address of bit-field structure member `%s'",
2467 IDENTIFIER_POINTER (DECL_NAME (field)));
2468 return error_mark_node;
2471 addr = fold (build (PLUS_EXPR, argtype,
2472 convert (argtype, addr),
2473 convert (argtype, byte_position (field))));
2475 else
2476 addr = build1 (code, argtype, arg);
2478 /* Address of a static or external variable or
2479 file-scope function counts as a constant. */
2480 if (staticp (arg)
2481 && ! (TREE_CODE (arg) == FUNCTION_DECL
2482 && !DECL_FILE_SCOPE_P (arg)))
2483 TREE_CONSTANT (addr) = 1;
2484 return addr;
2487 default:
2488 break;
2491 if (argtype == 0)
2492 argtype = TREE_TYPE (arg);
2493 return fold (build1 (code, argtype, arg));
2496 /* Return nonzero if REF is an lvalue valid for this language.
2497 Lvalues can be assigned, unless their type has TYPE_READONLY.
2498 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2501 lvalue_p (tree ref)
2503 enum tree_code code = TREE_CODE (ref);
2505 switch (code)
2507 case REALPART_EXPR:
2508 case IMAGPART_EXPR:
2509 case COMPONENT_REF:
2510 return lvalue_p (TREE_OPERAND (ref, 0));
2512 case COMPOUND_LITERAL_EXPR:
2513 case STRING_CST:
2514 return 1;
2516 case INDIRECT_REF:
2517 case ARRAY_REF:
2518 case VAR_DECL:
2519 case PARM_DECL:
2520 case RESULT_DECL:
2521 case ERROR_MARK:
2522 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2523 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2525 case BIND_EXPR:
2526 case RTL_EXPR:
2527 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2529 default:
2530 return 0;
2534 /* Return nonzero if REF is an lvalue valid for this language;
2535 otherwise, print an error message and return zero. */
2538 lvalue_or_else (tree ref, const char *msgid)
2540 int win = lvalue_p (ref);
2542 if (! win)
2543 error ("%s", msgid);
2545 return win;
2549 /* Warn about storing in something that is `const'. */
2551 void
2552 readonly_error (tree arg, const char *msgid)
2554 if (TREE_CODE (arg) == COMPONENT_REF)
2556 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2557 readonly_error (TREE_OPERAND (arg, 0), msgid);
2558 else
2559 error ("%s of read-only member `%s'", _(msgid),
2560 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2562 else if (TREE_CODE (arg) == VAR_DECL)
2563 error ("%s of read-only variable `%s'", _(msgid),
2564 IDENTIFIER_POINTER (DECL_NAME (arg)));
2565 else
2566 error ("%s of read-only location", _(msgid));
2569 /* Mark EXP saying that we need to be able to take the
2570 address of it; it should not be allocated in a register.
2571 Returns true if successful. */
2573 bool
2574 c_mark_addressable (tree exp)
2576 tree x = exp;
2578 while (1)
2579 switch (TREE_CODE (x))
2581 case COMPONENT_REF:
2582 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2584 error ("cannot take address of bit-field `%s'",
2585 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2586 return false;
2589 /* ... fall through ... */
2591 case ADDR_EXPR:
2592 case ARRAY_REF:
2593 case REALPART_EXPR:
2594 case IMAGPART_EXPR:
2595 x = TREE_OPERAND (x, 0);
2596 break;
2598 case COMPOUND_LITERAL_EXPR:
2599 case CONSTRUCTOR:
2600 TREE_ADDRESSABLE (x) = 1;
2601 return true;
2603 case VAR_DECL:
2604 case CONST_DECL:
2605 case PARM_DECL:
2606 case RESULT_DECL:
2607 if (C_DECL_REGISTER (x)
2608 && DECL_NONLOCAL (x))
2610 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2612 error ("global register variable `%s' used in nested function",
2613 IDENTIFIER_POINTER (DECL_NAME (x)));
2614 return false;
2616 pedwarn ("register variable `%s' used in nested function",
2617 IDENTIFIER_POINTER (DECL_NAME (x)));
2619 else if (C_DECL_REGISTER (x))
2621 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2623 error ("address of global register variable `%s' requested",
2624 IDENTIFIER_POINTER (DECL_NAME (x)));
2625 return false;
2628 pedwarn ("address of register variable `%s' requested",
2629 IDENTIFIER_POINTER (DECL_NAME (x)));
2631 put_var_into_stack (x, /*rescan=*/true);
2633 /* drops in */
2634 case FUNCTION_DECL:
2635 TREE_ADDRESSABLE (x) = 1;
2636 /* drops out */
2637 default:
2638 return true;
2642 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2644 tree
2645 build_conditional_expr (tree ifexp, tree op1, tree op2)
2647 tree type1;
2648 tree type2;
2649 enum tree_code code1;
2650 enum tree_code code2;
2651 tree result_type = NULL;
2652 tree orig_op1 = op1, orig_op2 = op2;
2654 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2656 /* Promote both alternatives. */
2658 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2659 op1 = default_conversion (op1);
2660 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2661 op2 = default_conversion (op2);
2663 if (TREE_CODE (ifexp) == ERROR_MARK
2664 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2665 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2666 return error_mark_node;
2668 type1 = TREE_TYPE (op1);
2669 code1 = TREE_CODE (type1);
2670 type2 = TREE_TYPE (op2);
2671 code2 = TREE_CODE (type2);
2673 /* C90 does not permit non-lvalue arrays in conditional expressions.
2674 In C99 they will be pointers by now. */
2675 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2677 error ("non-lvalue array in conditional expression");
2678 return error_mark_node;
2681 /* Quickly detect the usual case where op1 and op2 have the same type
2682 after promotion. */
2683 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2685 if (type1 == type2)
2686 result_type = type1;
2687 else
2688 result_type = TYPE_MAIN_VARIANT (type1);
2690 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2691 || code1 == COMPLEX_TYPE)
2692 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2693 || code2 == COMPLEX_TYPE))
2695 result_type = common_type (type1, type2);
2697 /* If -Wsign-compare, warn here if type1 and type2 have
2698 different signedness. We'll promote the signed to unsigned
2699 and later code won't know it used to be different.
2700 Do this check on the original types, so that explicit casts
2701 will be considered, but default promotions won't. */
2702 if (warn_sign_compare && !skip_evaluation)
2704 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2705 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2707 if (unsigned_op1 ^ unsigned_op2)
2709 /* Do not warn if the result type is signed, since the
2710 signed type will only be chosen if it can represent
2711 all the values of the unsigned type. */
2712 if (! TREE_UNSIGNED (result_type))
2713 /* OK */;
2714 /* Do not warn if the signed quantity is an unsuffixed
2715 integer literal (or some static constant expression
2716 involving such literals) and it is non-negative. */
2717 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2718 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2719 /* OK */;
2720 else
2721 warning ("signed and unsigned type in conditional expression");
2725 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2727 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2728 pedwarn ("ISO C forbids conditional expr with only one void side");
2729 result_type = void_type_node;
2731 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2733 if (comp_target_types (type1, type2, 1))
2734 result_type = common_type (type1, type2);
2735 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2736 && TREE_CODE (orig_op1) != NOP_EXPR)
2737 result_type = qualify_type (type2, type1);
2738 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2739 && TREE_CODE (orig_op2) != NOP_EXPR)
2740 result_type = qualify_type (type1, type2);
2741 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2743 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2744 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2745 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2746 TREE_TYPE (type2)));
2748 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2750 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2751 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2752 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2753 TREE_TYPE (type1)));
2755 else
2757 pedwarn ("pointer type mismatch in conditional expression");
2758 result_type = build_pointer_type (void_type_node);
2761 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2763 if (! integer_zerop (op2))
2764 pedwarn ("pointer/integer type mismatch in conditional expression");
2765 else
2767 op2 = null_pointer_node;
2769 result_type = type1;
2771 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2773 if (!integer_zerop (op1))
2774 pedwarn ("pointer/integer type mismatch in conditional expression");
2775 else
2777 op1 = null_pointer_node;
2779 result_type = type2;
2782 if (!result_type)
2784 if (flag_cond_mismatch)
2785 result_type = void_type_node;
2786 else
2788 error ("type mismatch in conditional expression");
2789 return error_mark_node;
2793 /* Merge const and volatile flags of the incoming types. */
2794 result_type
2795 = build_type_variant (result_type,
2796 TREE_READONLY (op1) || TREE_READONLY (op2),
2797 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2799 if (result_type != TREE_TYPE (op1))
2800 op1 = convert_and_check (result_type, op1);
2801 if (result_type != TREE_TYPE (op2))
2802 op2 = convert_and_check (result_type, op2);
2804 if (TREE_CODE (ifexp) == INTEGER_CST)
2805 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2807 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2810 /* Given a list of expressions, return a compound expression
2811 that performs them all and returns the value of the last of them. */
2813 tree
2814 build_compound_expr (tree list)
2816 return internal_build_compound_expr (list, TRUE);
2819 static tree
2820 internal_build_compound_expr (tree list, int first_p)
2822 tree rest;
2824 if (TREE_CHAIN (list) == 0)
2826 /* Convert arrays and functions to pointers when there
2827 really is a comma operator. */
2828 if (!first_p)
2829 TREE_VALUE (list)
2830 = default_function_array_conversion (TREE_VALUE (list));
2832 /* Don't let (0, 0) be null pointer constant. */
2833 if (!first_p && integer_zerop (TREE_VALUE (list)))
2834 return non_lvalue (TREE_VALUE (list));
2835 return TREE_VALUE (list);
2838 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2840 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2842 /* The left-hand operand of a comma expression is like an expression
2843 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2844 any side-effects, unless it was explicitly cast to (void). */
2845 if (warn_unused_value
2846 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2847 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2848 warning ("left-hand operand of comma expression has no effect");
2851 /* With -Wunused, we should also warn if the left-hand operand does have
2852 side-effects, but computes a value which is not used. For example, in
2853 `foo() + bar(), baz()' the result of the `+' operator is not used,
2854 so we should issue a warning. */
2855 else if (warn_unused_value)
2856 warn_if_unused_value (TREE_VALUE (list));
2858 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2861 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2863 tree
2864 build_c_cast (tree type, tree expr)
2866 tree value = expr;
2868 if (type == error_mark_node || expr == error_mark_node)
2869 return error_mark_node;
2871 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2872 only in <protocol> qualifications. But when constructing cast expressions,
2873 the protocols do matter and must be kept around. */
2874 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2875 type = TYPE_MAIN_VARIANT (type);
2877 if (TREE_CODE (type) == ARRAY_TYPE)
2879 error ("cast specifies array type");
2880 return error_mark_node;
2883 if (TREE_CODE (type) == FUNCTION_TYPE)
2885 error ("cast specifies function type");
2886 return error_mark_node;
2889 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2891 if (pedantic)
2893 if (TREE_CODE (type) == RECORD_TYPE
2894 || TREE_CODE (type) == UNION_TYPE)
2895 pedwarn ("ISO C forbids casting nonscalar to the same type");
2898 else if (TREE_CODE (type) == UNION_TYPE)
2900 tree field;
2901 value = default_function_array_conversion (value);
2903 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2904 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2905 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2906 break;
2908 if (field)
2910 tree t;
2912 if (pedantic)
2913 pedwarn ("ISO C forbids casts to union type");
2914 t = digest_init (type,
2915 build_constructor (type,
2916 build_tree_list (field, value)),
2918 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2919 return t;
2921 error ("cast to union type from type not present in union");
2922 return error_mark_node;
2924 else
2926 tree otype, ovalue;
2928 /* If casting to void, avoid the error that would come
2929 from default_conversion in the case of a non-lvalue array. */
2930 if (type == void_type_node)
2931 return build1 (CONVERT_EXPR, type, value);
2933 /* Convert functions and arrays to pointers,
2934 but don't convert any other types. */
2935 value = default_function_array_conversion (value);
2936 otype = TREE_TYPE (value);
2938 /* Optionally warn about potentially worrisome casts. */
2940 if (warn_cast_qual
2941 && TREE_CODE (type) == POINTER_TYPE
2942 && TREE_CODE (otype) == POINTER_TYPE)
2944 tree in_type = type;
2945 tree in_otype = otype;
2946 int added = 0;
2947 int discarded = 0;
2949 /* Check that the qualifiers on IN_TYPE are a superset of
2950 the qualifiers of IN_OTYPE. The outermost level of
2951 POINTER_TYPE nodes is uninteresting and we stop as soon
2952 as we hit a non-POINTER_TYPE node on either type. */
2955 in_otype = TREE_TYPE (in_otype);
2956 in_type = TREE_TYPE (in_type);
2958 /* GNU C allows cv-qualified function types. 'const'
2959 means the function is very pure, 'volatile' means it
2960 can't return. We need to warn when such qualifiers
2961 are added, not when they're taken away. */
2962 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2963 && TREE_CODE (in_type) == FUNCTION_TYPE)
2964 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2965 else
2966 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2968 while (TREE_CODE (in_type) == POINTER_TYPE
2969 && TREE_CODE (in_otype) == POINTER_TYPE);
2971 if (added)
2972 warning ("cast adds new qualifiers to function type");
2974 if (discarded)
2975 /* There are qualifiers present in IN_OTYPE that are not
2976 present in IN_TYPE. */
2977 warning ("cast discards qualifiers from pointer target type");
2980 /* Warn about possible alignment problems. */
2981 if (STRICT_ALIGNMENT && warn_cast_align
2982 && TREE_CODE (type) == POINTER_TYPE
2983 && TREE_CODE (otype) == POINTER_TYPE
2984 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2985 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2986 /* Don't warn about opaque types, where the actual alignment
2987 restriction is unknown. */
2988 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
2989 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
2990 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
2991 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
2992 warning ("cast increases required alignment of target type");
2994 if (TREE_CODE (type) == INTEGER_TYPE
2995 && TREE_CODE (otype) == POINTER_TYPE
2996 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
2997 && !TREE_CONSTANT (value))
2998 warning ("cast from pointer to integer of different size");
3000 if (warn_bad_function_cast
3001 && TREE_CODE (value) == CALL_EXPR
3002 && TREE_CODE (type) != TREE_CODE (otype))
3003 warning ("cast does not match function type");
3005 if (TREE_CODE (type) == POINTER_TYPE
3006 && TREE_CODE (otype) == INTEGER_TYPE
3007 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3008 /* Don't warn about converting any constant. */
3009 && !TREE_CONSTANT (value))
3010 warning ("cast to pointer from integer of different size");
3012 if (TREE_CODE (type) == POINTER_TYPE
3013 && TREE_CODE (otype) == POINTER_TYPE
3014 && TREE_CODE (expr) == ADDR_EXPR
3015 && DECL_P (TREE_OPERAND (expr, 0))
3016 && flag_strict_aliasing && warn_strict_aliasing
3017 && !VOID_TYPE_P (TREE_TYPE (type)))
3019 /* Casting the address of a decl to non void pointer. Warn
3020 if the cast breaks type based aliasing. */
3021 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3022 warning ("type-punning to incomplete type might break strict-aliasing rules");
3023 else
3025 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3026 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3028 if (!alias_sets_conflict_p (set1, set2))
3029 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3030 else if (warn_strict_aliasing > 1
3031 && !alias_sets_might_conflict_p (set1, set2))
3032 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3036 /* If pedantic, warn for conversions between function and object
3037 pointer types, except for converting a null pointer constant
3038 to function pointer type. */
3039 if (pedantic
3040 && TREE_CODE (type) == POINTER_TYPE
3041 && TREE_CODE (otype) == POINTER_TYPE
3042 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3043 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3044 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3046 if (pedantic
3047 && TREE_CODE (type) == POINTER_TYPE
3048 && TREE_CODE (otype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3050 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3051 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3052 && TREE_CODE (expr) != NOP_EXPR))
3053 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3055 ovalue = value;
3056 /* Replace a nonvolatile const static variable with its value. */
3057 if (optimize && TREE_CODE (value) == VAR_DECL)
3058 value = decl_constant_value (value);
3059 value = convert (type, value);
3061 /* Ignore any integer overflow caused by the cast. */
3062 if (TREE_CODE (value) == INTEGER_CST)
3064 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3066 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3067 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3071 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3072 if (TREE_CODE (value) == INTEGER_CST
3073 && TREE_CODE (expr) == INTEGER_CST
3074 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3075 value = non_lvalue (value);
3077 /* Don't let a cast be an lvalue. */
3078 if (value == expr)
3079 value = non_lvalue (value);
3081 return value;
3084 /* Interpret a cast of expression EXPR to type TYPE. */
3085 tree
3086 c_cast_expr (tree type, tree expr)
3088 int saved_wsp = warn_strict_prototypes;
3090 /* This avoids warnings about unprototyped casts on
3091 integers. E.g. "#define SIG_DFL (void(*)())0". */
3092 if (TREE_CODE (expr) == INTEGER_CST)
3093 warn_strict_prototypes = 0;
3094 type = groktypename (type);
3095 warn_strict_prototypes = saved_wsp;
3097 return build_c_cast (type, expr);
3101 /* Build an assignment expression of lvalue LHS from value RHS.
3102 MODIFYCODE is the code for a binary operator that we use
3103 to combine the old value of LHS with RHS to get the new value.
3104 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3106 tree
3107 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3109 tree result;
3110 tree newrhs;
3111 tree lhstype = TREE_TYPE (lhs);
3112 tree olhstype = lhstype;
3114 /* Types that aren't fully specified cannot be used in assignments. */
3115 lhs = require_complete_type (lhs);
3117 /* Avoid duplicate error messages from operands that had errors. */
3118 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3119 return error_mark_node;
3121 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3122 /* Do not use STRIP_NOPS here. We do not want an enumerator
3123 whose value is 0 to count as a null pointer constant. */
3124 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3125 rhs = TREE_OPERAND (rhs, 0);
3127 newrhs = rhs;
3129 /* If a binary op has been requested, combine the old LHS value with the RHS
3130 producing the value we should actually store into the LHS. */
3132 if (modifycode != NOP_EXPR)
3134 lhs = stabilize_reference (lhs);
3135 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3138 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3139 return error_mark_node;
3141 /* Warn about storing in something that is `const'. */
3143 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3144 || ((TREE_CODE (lhstype) == RECORD_TYPE
3145 || TREE_CODE (lhstype) == UNION_TYPE)
3146 && C_TYPE_FIELDS_READONLY (lhstype)))
3147 readonly_error (lhs, "assignment");
3149 /* If storing into a structure or union member,
3150 it has probably been given type `int'.
3151 Compute the type that would go with
3152 the actual amount of storage the member occupies. */
3154 if (TREE_CODE (lhs) == COMPONENT_REF
3155 && (TREE_CODE (lhstype) == INTEGER_TYPE
3156 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3157 || TREE_CODE (lhstype) == REAL_TYPE
3158 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3159 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3161 /* If storing in a field that is in actuality a short or narrower than one,
3162 we must store in the field in its actual type. */
3164 if (lhstype != TREE_TYPE (lhs))
3166 lhs = copy_node (lhs);
3167 TREE_TYPE (lhs) = lhstype;
3170 /* Convert new value to destination type. */
3172 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3173 NULL_TREE, NULL_TREE, 0);
3174 if (TREE_CODE (newrhs) == ERROR_MARK)
3175 return error_mark_node;
3177 /* Scan operands */
3179 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3180 TREE_SIDE_EFFECTS (result) = 1;
3182 /* If we got the LHS in a different type for storing in,
3183 convert the result back to the nominal type of LHS
3184 so that the value we return always has the same type
3185 as the LHS argument. */
3187 if (olhstype == TREE_TYPE (result))
3188 return result;
3189 return convert_for_assignment (olhstype, result, _("assignment"),
3190 NULL_TREE, NULL_TREE, 0);
3193 /* Convert value RHS to type TYPE as preparation for an assignment
3194 to an lvalue of type TYPE.
3195 The real work of conversion is done by `convert'.
3196 The purpose of this function is to generate error messages
3197 for assignments that are not allowed in C.
3198 ERRTYPE is a string to use in error messages:
3199 "assignment", "return", etc. If it is null, this is parameter passing
3200 for a function call (and different error messages are output).
3202 FUNNAME is the name of the function being called,
3203 as an IDENTIFIER_NODE, or null.
3204 PARMNUM is the number of the argument, for printing in error messages. */
3206 static tree
3207 convert_for_assignment (tree type, tree rhs, const char *errtype,
3208 tree fundecl, tree funname, int parmnum)
3210 enum tree_code codel = TREE_CODE (type);
3211 tree rhstype;
3212 enum tree_code coder;
3214 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3215 /* Do not use STRIP_NOPS here. We do not want an enumerator
3216 whose value is 0 to count as a null pointer constant. */
3217 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3218 rhs = TREE_OPERAND (rhs, 0);
3220 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3221 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3222 rhs = default_conversion (rhs);
3223 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3224 rhs = decl_constant_value_for_broken_optimization (rhs);
3226 rhstype = TREE_TYPE (rhs);
3227 coder = TREE_CODE (rhstype);
3229 if (coder == ERROR_MARK)
3230 return error_mark_node;
3232 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3234 overflow_warning (rhs);
3235 /* Check for Objective-C protocols. This will automatically
3236 issue a warning if there are protocol violations. No need to
3237 use the return value. */
3238 if (c_dialect_objc ())
3239 objc_comptypes (type, rhstype, 0);
3240 return rhs;
3243 if (coder == VOID_TYPE)
3245 error ("void value not ignored as it ought to be");
3246 return error_mark_node;
3248 /* A type converts to a reference to it.
3249 This code doesn't fully support references, it's just for the
3250 special case of va_start and va_copy. */
3251 if (codel == REFERENCE_TYPE
3252 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3254 if (!lvalue_p (rhs))
3256 error ("cannot pass rvalue to reference parameter");
3257 return error_mark_node;
3259 if (!c_mark_addressable (rhs))
3260 return error_mark_node;
3261 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3263 /* We already know that these two types are compatible, but they
3264 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3265 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3266 likely to be va_list, a typedef to __builtin_va_list, which
3267 is different enough that it will cause problems later. */
3268 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3269 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3271 rhs = build1 (NOP_EXPR, type, rhs);
3272 return rhs;
3274 /* Some types can interconvert without explicit casts. */
3275 else if (codel == VECTOR_TYPE
3276 && comptypes (type, TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3277 return convert (type, rhs);
3278 /* Arithmetic types all interconvert, and enum is treated like int. */
3279 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3280 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3281 || codel == BOOLEAN_TYPE)
3282 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3283 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3284 || coder == BOOLEAN_TYPE))
3285 return convert_and_check (type, rhs);
3287 /* Conversion to a transparent union from its member types.
3288 This applies only to function arguments. */
3289 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3291 tree memb_types;
3292 tree marginal_memb_type = 0;
3294 for (memb_types = TYPE_FIELDS (type); memb_types;
3295 memb_types = TREE_CHAIN (memb_types))
3297 tree memb_type = TREE_TYPE (memb_types);
3299 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3300 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3301 break;
3303 if (TREE_CODE (memb_type) != POINTER_TYPE)
3304 continue;
3306 if (coder == POINTER_TYPE)
3308 tree ttl = TREE_TYPE (memb_type);
3309 tree ttr = TREE_TYPE (rhstype);
3311 /* Any non-function converts to a [const][volatile] void *
3312 and vice versa; otherwise, targets must be the same.
3313 Meanwhile, the lhs target must have all the qualifiers of
3314 the rhs. */
3315 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3316 || comp_target_types (memb_type, rhstype, 0))
3318 /* If this type won't generate any warnings, use it. */
3319 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3320 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3321 && TREE_CODE (ttl) == FUNCTION_TYPE)
3322 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3323 == TYPE_QUALS (ttr))
3324 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3325 == TYPE_QUALS (ttl))))
3326 break;
3328 /* Keep looking for a better type, but remember this one. */
3329 if (! marginal_memb_type)
3330 marginal_memb_type = memb_type;
3334 /* Can convert integer zero to any pointer type. */
3335 if (integer_zerop (rhs)
3336 || (TREE_CODE (rhs) == NOP_EXPR
3337 && integer_zerop (TREE_OPERAND (rhs, 0))))
3339 rhs = null_pointer_node;
3340 break;
3344 if (memb_types || marginal_memb_type)
3346 if (! memb_types)
3348 /* We have only a marginally acceptable member type;
3349 it needs a warning. */
3350 tree ttl = TREE_TYPE (marginal_memb_type);
3351 tree ttr = TREE_TYPE (rhstype);
3353 /* Const and volatile mean something different for function
3354 types, so the usual warnings are not appropriate. */
3355 if (TREE_CODE (ttr) == FUNCTION_TYPE
3356 && TREE_CODE (ttl) == FUNCTION_TYPE)
3358 /* Because const and volatile on functions are
3359 restrictions that say the function will not do
3360 certain things, it is okay to use a const or volatile
3361 function where an ordinary one is wanted, but not
3362 vice-versa. */
3363 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3364 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3365 errtype, funname, parmnum);
3367 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3368 warn_for_assignment ("%s discards qualifiers from pointer target type",
3369 errtype, funname,
3370 parmnum);
3373 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3374 pedwarn ("ISO C prohibits argument conversion to union type");
3376 return build1 (NOP_EXPR, type, rhs);
3380 /* Conversions among pointers */
3381 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3382 && (coder == codel))
3384 tree ttl = TREE_TYPE (type);
3385 tree ttr = TREE_TYPE (rhstype);
3386 bool is_opaque_pointer;
3387 int target_cmp = 0; /* Cache comp_target_types () result. */
3389 /* Opaque pointers are treated like void pointers. */
3390 is_opaque_pointer = (targetm.vector_opaque_p (type)
3391 || targetm.vector_opaque_p (rhstype))
3392 && TREE_CODE (ttl) == VECTOR_TYPE
3393 && TREE_CODE (ttr) == VECTOR_TYPE;
3395 /* Any non-function converts to a [const][volatile] void *
3396 and vice versa; otherwise, targets must be the same.
3397 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3398 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3399 || (target_cmp = comp_target_types (type, rhstype, 0))
3400 || is_opaque_pointer
3401 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3402 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3404 if (pedantic
3405 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3407 (VOID_TYPE_P (ttr)
3408 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3409 which are not ANSI null ptr constants. */
3410 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3411 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3412 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3413 errtype, funname, parmnum);
3414 /* Const and volatile mean something different for function types,
3415 so the usual warnings are not appropriate. */
3416 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3417 && TREE_CODE (ttl) != FUNCTION_TYPE)
3419 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3420 warn_for_assignment ("%s discards qualifiers from pointer target type",
3421 errtype, funname, parmnum);
3422 /* If this is not a case of ignoring a mismatch in signedness,
3423 no warning. */
3424 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3425 || target_cmp)
3427 /* If there is a mismatch, do warn. */
3428 else if (pedantic)
3429 warn_for_assignment ("pointer targets in %s differ in signedness",
3430 errtype, funname, parmnum);
3432 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3433 && TREE_CODE (ttr) == FUNCTION_TYPE)
3435 /* Because const and volatile on functions are restrictions
3436 that say the function will not do certain things,
3437 it is okay to use a const or volatile function
3438 where an ordinary one is wanted, but not vice-versa. */
3439 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3440 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3441 errtype, funname, parmnum);
3444 else
3445 warn_for_assignment ("%s from incompatible pointer type",
3446 errtype, funname, parmnum);
3447 return convert (type, rhs);
3449 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3451 error ("invalid use of non-lvalue array");
3452 return error_mark_node;
3454 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3456 /* An explicit constant 0 can convert to a pointer,
3457 or one that results from arithmetic, even including
3458 a cast to integer type. */
3459 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3461 ! (TREE_CODE (rhs) == NOP_EXPR
3462 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3463 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3464 && integer_zerop (TREE_OPERAND (rhs, 0))))
3465 warn_for_assignment ("%s makes pointer from integer without a cast",
3466 errtype, funname, parmnum);
3468 return convert (type, rhs);
3470 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3472 warn_for_assignment ("%s makes integer from pointer without a cast",
3473 errtype, funname, parmnum);
3474 return convert (type, rhs);
3476 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3477 return convert (type, rhs);
3479 if (!errtype)
3481 if (funname)
3483 tree selector = objc_message_selector ();
3485 if (selector && parmnum > 2)
3486 error ("incompatible type for argument %d of `%s'",
3487 parmnum - 2, IDENTIFIER_POINTER (selector));
3488 else
3489 error ("incompatible type for argument %d of `%s'",
3490 parmnum, IDENTIFIER_POINTER (funname));
3492 else
3493 error ("incompatible type for argument %d of indirect function call",
3494 parmnum);
3496 else
3497 error ("incompatible types in %s", errtype);
3499 return error_mark_node;
3502 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3503 is used for error and waring reporting and indicates which argument
3504 is being processed. */
3506 tree
3507 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3509 tree ret, type;
3511 /* If FN was prototyped, the value has been converted already
3512 in convert_arguments. */
3513 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3514 return value;
3516 type = TREE_TYPE (parm);
3517 ret = convert_for_assignment (type, value,
3518 (char *) 0 /* arg passing */, fn,
3519 DECL_NAME (fn), argnum);
3520 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3521 && INTEGRAL_TYPE_P (type)
3522 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3523 ret = default_conversion (ret);
3524 return ret;
3527 /* Print a warning using MSGID.
3528 It gets OPNAME as its one parameter.
3529 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3530 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3531 FUNCTION and ARGNUM are handled specially if we are building an
3532 Objective-C selector. */
3534 static void
3535 warn_for_assignment (const char *msgid, const char *opname, tree function,
3536 int argnum)
3538 if (opname == 0)
3540 tree selector = objc_message_selector ();
3541 char * new_opname;
3543 if (selector && argnum > 2)
3545 function = selector;
3546 argnum -= 2;
3548 if (argnum == 0)
3550 if (function)
3552 /* Function name is known; supply it. */
3553 const char *const argstring = _("passing arg of `%s'");
3554 new_opname = alloca (IDENTIFIER_LENGTH (function)
3555 + strlen (argstring) + 1 + 1);
3556 sprintf (new_opname, argstring,
3557 IDENTIFIER_POINTER (function));
3559 else
3561 /* Function name unknown (call through ptr). */
3562 const char *const argnofun = _("passing arg of pointer to function");
3563 new_opname = alloca (strlen (argnofun) + 1 + 1);
3564 sprintf (new_opname, argnofun);
3567 else if (function)
3569 /* Function name is known; supply it. */
3570 const char *const argstring = _("passing arg %d of `%s'");
3571 new_opname = alloca (IDENTIFIER_LENGTH (function)
3572 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3573 sprintf (new_opname, argstring, argnum,
3574 IDENTIFIER_POINTER (function));
3576 else
3578 /* Function name unknown (call through ptr); just give arg number. */
3579 const char *const argnofun = _("passing arg %d of pointer to function");
3580 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3581 sprintf (new_opname, argnofun, argnum);
3583 opname = new_opname;
3585 pedwarn (msgid, opname);
3588 /* If VALUE is a compound expr all of whose expressions are constant, then
3589 return its value. Otherwise, return error_mark_node.
3591 This is for handling COMPOUND_EXPRs as initializer elements
3592 which is allowed with a warning when -pedantic is specified. */
3594 static tree
3595 valid_compound_expr_initializer (tree value, tree endtype)
3597 if (TREE_CODE (value) == COMPOUND_EXPR)
3599 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3600 == error_mark_node)
3601 return error_mark_node;
3602 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3603 endtype);
3605 else if (! TREE_CONSTANT (value)
3606 && ! initializer_constant_valid_p (value, endtype))
3607 return error_mark_node;
3608 else
3609 return value;
3612 /* Perform appropriate conversions on the initial value of a variable,
3613 store it in the declaration DECL,
3614 and print any error messages that are appropriate.
3615 If the init is invalid, store an ERROR_MARK. */
3617 void
3618 store_init_value (tree decl, tree init)
3620 tree value, type;
3622 /* If variable's type was invalidly declared, just ignore it. */
3624 type = TREE_TYPE (decl);
3625 if (TREE_CODE (type) == ERROR_MARK)
3626 return;
3628 /* Digest the specified initializer into an expression. */
3630 value = digest_init (type, init, TREE_STATIC (decl));
3632 /* Store the expression if valid; else report error. */
3634 if (warn_traditional && !in_system_header
3635 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3636 warning ("traditional C rejects automatic aggregate initialization");
3638 DECL_INITIAL (decl) = value;
3640 /* ANSI wants warnings about out-of-range constant initializers. */
3641 STRIP_TYPE_NOPS (value);
3642 constant_expression_warning (value);
3644 /* Check if we need to set array size from compound literal size. */
3645 if (TREE_CODE (type) == ARRAY_TYPE
3646 && TYPE_DOMAIN (type) == 0
3647 && value != error_mark_node)
3649 tree inside_init = init;
3651 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3652 inside_init = TREE_OPERAND (init, 0);
3653 inside_init = fold (inside_init);
3655 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3657 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3659 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3661 /* For int foo[] = (int [3]){1}; we need to set array size
3662 now since later on array initializer will be just the
3663 brace enclosed list of the compound literal. */
3664 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3665 layout_type (type);
3666 layout_decl (decl, 0);
3672 /* Methods for storing and printing names for error messages. */
3674 /* Implement a spelling stack that allows components of a name to be pushed
3675 and popped. Each element on the stack is this structure. */
3677 struct spelling
3679 int kind;
3680 union
3682 int i;
3683 const char *s;
3684 } u;
3687 #define SPELLING_STRING 1
3688 #define SPELLING_MEMBER 2
3689 #define SPELLING_BOUNDS 3
3691 static struct spelling *spelling; /* Next stack element (unused). */
3692 static struct spelling *spelling_base; /* Spelling stack base. */
3693 static int spelling_size; /* Size of the spelling stack. */
3695 /* Macros to save and restore the spelling stack around push_... functions.
3696 Alternative to SAVE_SPELLING_STACK. */
3698 #define SPELLING_DEPTH() (spelling - spelling_base)
3699 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3701 /* Push an element on the spelling stack with type KIND and assign VALUE
3702 to MEMBER. */
3704 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3706 int depth = SPELLING_DEPTH (); \
3708 if (depth >= spelling_size) \
3710 spelling_size += 10; \
3711 if (spelling_base == 0) \
3712 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3713 else \
3714 spelling_base = xrealloc (spelling_base, \
3715 spelling_size * sizeof (struct spelling)); \
3716 RESTORE_SPELLING_DEPTH (depth); \
3719 spelling->kind = (KIND); \
3720 spelling->MEMBER = (VALUE); \
3721 spelling++; \
3724 /* Push STRING on the stack. Printed literally. */
3726 static void
3727 push_string (const char *string)
3729 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3732 /* Push a member name on the stack. Printed as '.' STRING. */
3734 static void
3735 push_member_name (tree decl)
3737 const char *const string
3738 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3739 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3742 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3744 static void
3745 push_array_bounds (int bounds)
3747 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3750 /* Compute the maximum size in bytes of the printed spelling. */
3752 static int
3753 spelling_length (void)
3755 int size = 0;
3756 struct spelling *p;
3758 for (p = spelling_base; p < spelling; p++)
3760 if (p->kind == SPELLING_BOUNDS)
3761 size += 25;
3762 else
3763 size += strlen (p->u.s) + 1;
3766 return size;
3769 /* Print the spelling to BUFFER and return it. */
3771 static char *
3772 print_spelling (char *buffer)
3774 char *d = buffer;
3775 struct spelling *p;
3777 for (p = spelling_base; p < spelling; p++)
3778 if (p->kind == SPELLING_BOUNDS)
3780 sprintf (d, "[%d]", p->u.i);
3781 d += strlen (d);
3783 else
3785 const char *s;
3786 if (p->kind == SPELLING_MEMBER)
3787 *d++ = '.';
3788 for (s = p->u.s; (*d = *s++); d++)
3791 *d++ = '\0';
3792 return buffer;
3795 /* Issue an error message for a bad initializer component.
3796 MSGID identifies the message.
3797 The component name is taken from the spelling stack. */
3799 void
3800 error_init (const char *msgid)
3802 char *ofwhat;
3804 error ("%s", _(msgid));
3805 ofwhat = print_spelling (alloca (spelling_length () + 1));
3806 if (*ofwhat)
3807 error ("(near initialization for `%s')", ofwhat);
3810 /* Issue a pedantic warning for a bad initializer component.
3811 MSGID identifies the message.
3812 The component name is taken from the spelling stack. */
3814 void
3815 pedwarn_init (const char *msgid)
3817 char *ofwhat;
3819 pedwarn ("%s", _(msgid));
3820 ofwhat = print_spelling (alloca (spelling_length () + 1));
3821 if (*ofwhat)
3822 pedwarn ("(near initialization for `%s')", ofwhat);
3825 /* Issue a warning for a bad initializer component.
3826 MSGID identifies the message.
3827 The component name is taken from the spelling stack. */
3829 static void
3830 warning_init (const char *msgid)
3832 char *ofwhat;
3834 warning ("%s", _(msgid));
3835 ofwhat = print_spelling (alloca (spelling_length () + 1));
3836 if (*ofwhat)
3837 warning ("(near initialization for `%s')", ofwhat);
3840 /* Digest the parser output INIT as an initializer for type TYPE.
3841 Return a C expression of type TYPE to represent the initial value.
3843 REQUIRE_CONSTANT requests an error if non-constant initializers or
3844 elements are seen. */
3846 static tree
3847 digest_init (tree type, tree init, int require_constant)
3849 enum tree_code code = TREE_CODE (type);
3850 tree inside_init = init;
3852 if (type == error_mark_node
3853 || init == error_mark_node
3854 || TREE_TYPE (init) == error_mark_node)
3855 return error_mark_node;
3857 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3858 /* Do not use STRIP_NOPS here. We do not want an enumerator
3859 whose value is 0 to count as a null pointer constant. */
3860 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3861 inside_init = TREE_OPERAND (init, 0);
3863 inside_init = fold (inside_init);
3865 /* Initialization of an array of chars from a string constant
3866 optionally enclosed in braces. */
3868 if (code == ARRAY_TYPE)
3870 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3871 if ((typ1 == char_type_node
3872 || typ1 == signed_char_type_node
3873 || typ1 == unsigned_char_type_node
3874 || typ1 == unsigned_wchar_type_node
3875 || typ1 == signed_wchar_type_node)
3876 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3878 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3879 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3880 return inside_init;
3882 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3883 != char_type_node)
3884 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3886 error_init ("char-array initialized from wide string");
3887 return error_mark_node;
3889 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3890 == char_type_node)
3891 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3893 error_init ("int-array initialized from non-wide string");
3894 return error_mark_node;
3897 TREE_TYPE (inside_init) = type;
3898 if (TYPE_DOMAIN (type) != 0
3899 && TYPE_SIZE (type) != 0
3900 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3901 /* Subtract 1 (or sizeof (wchar_t))
3902 because it's ok to ignore the terminating null char
3903 that is counted in the length of the constant. */
3904 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3905 TREE_STRING_LENGTH (inside_init)
3906 - ((TYPE_PRECISION (typ1)
3907 != TYPE_PRECISION (char_type_node))
3908 ? (TYPE_PRECISION (wchar_type_node)
3909 / BITS_PER_UNIT)
3910 : 1)))
3911 pedwarn_init ("initializer-string for array of chars is too long");
3913 return inside_init;
3917 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3918 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3919 below and handle as a constructor. */
3920 if (code == VECTOR_TYPE
3921 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3922 && TREE_CONSTANT (inside_init))
3924 if (TREE_CODE (inside_init) == VECTOR_CST
3925 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3926 TYPE_MAIN_VARIANT (type),
3927 COMPARE_STRICT))
3928 return inside_init;
3929 else
3930 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3933 /* Any type can be initialized
3934 from an expression of the same type, optionally with braces. */
3936 if (inside_init && TREE_TYPE (inside_init) != 0
3937 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3938 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3939 || (code == ARRAY_TYPE
3940 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3941 || (code == VECTOR_TYPE
3942 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3943 || (code == POINTER_TYPE
3944 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3945 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3946 TREE_TYPE (type), COMPARE_STRICT))
3947 || (code == POINTER_TYPE
3948 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3949 && comptypes (TREE_TYPE (inside_init),
3950 TREE_TYPE (type), COMPARE_STRICT))))
3952 if (code == POINTER_TYPE)
3954 inside_init = default_function_array_conversion (inside_init);
3956 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3958 error_init ("invalid use of non-lvalue array");
3959 return error_mark_node;
3963 if (code == VECTOR_TYPE)
3964 /* Although the types are compatible, we may require a
3965 conversion. */
3966 inside_init = convert (type, inside_init);
3968 if (require_constant && !flag_isoc99
3969 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3971 /* As an extension, allow initializing objects with static storage
3972 duration with compound literals (which are then treated just as
3973 the brace enclosed list they contain). */
3974 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3975 inside_init = DECL_INITIAL (decl);
3978 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3979 && TREE_CODE (inside_init) != CONSTRUCTOR)
3981 error_init ("array initialized from non-constant array expression");
3982 return error_mark_node;
3985 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
3986 inside_init = decl_constant_value_for_broken_optimization (inside_init);
3988 /* Compound expressions can only occur here if -pedantic or
3989 -pedantic-errors is specified. In the later case, we always want
3990 an error. In the former case, we simply want a warning. */
3991 if (require_constant && pedantic
3992 && TREE_CODE (inside_init) == COMPOUND_EXPR)
3994 inside_init
3995 = valid_compound_expr_initializer (inside_init,
3996 TREE_TYPE (inside_init));
3997 if (inside_init == error_mark_node)
3998 error_init ("initializer element is not constant");
3999 else
4000 pedwarn_init ("initializer element is not constant");
4001 if (flag_pedantic_errors)
4002 inside_init = error_mark_node;
4004 else if (require_constant
4005 && (!TREE_CONSTANT (inside_init)
4006 /* This test catches things like `7 / 0' which
4007 result in an expression for which TREE_CONSTANT
4008 is true, but which is not actually something
4009 that is a legal constant. We really should not
4010 be using this function, because it is a part of
4011 the back-end. Instead, the expression should
4012 already have been turned into ERROR_MARK_NODE. */
4013 || !initializer_constant_valid_p (inside_init,
4014 TREE_TYPE (inside_init))))
4016 error_init ("initializer element is not constant");
4017 inside_init = error_mark_node;
4020 return inside_init;
4023 /* Handle scalar types, including conversions. */
4025 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4026 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4028 /* Note that convert_for_assignment calls default_conversion
4029 for arrays and functions. We must not call it in the
4030 case where inside_init is a null pointer constant. */
4031 inside_init
4032 = convert_for_assignment (type, init, _("initialization"),
4033 NULL_TREE, NULL_TREE, 0);
4035 if (require_constant && ! TREE_CONSTANT (inside_init))
4037 error_init ("initializer element is not constant");
4038 inside_init = error_mark_node;
4040 else if (require_constant
4041 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4043 error_init ("initializer element is not computable at load time");
4044 inside_init = error_mark_node;
4047 return inside_init;
4050 /* Come here only for records and arrays. */
4052 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4054 error_init ("variable-sized object may not be initialized");
4055 return error_mark_node;
4058 error_init ("invalid initializer");
4059 return error_mark_node;
4062 /* Handle initializers that use braces. */
4064 /* Type of object we are accumulating a constructor for.
4065 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4066 static tree constructor_type;
4068 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4069 left to fill. */
4070 static tree constructor_fields;
4072 /* For an ARRAY_TYPE, this is the specified index
4073 at which to store the next element we get. */
4074 static tree constructor_index;
4076 /* For an ARRAY_TYPE, this is the maximum index. */
4077 static tree constructor_max_index;
4079 /* For a RECORD_TYPE, this is the first field not yet written out. */
4080 static tree constructor_unfilled_fields;
4082 /* For an ARRAY_TYPE, this is the index of the first element
4083 not yet written out. */
4084 static tree constructor_unfilled_index;
4086 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4087 This is so we can generate gaps between fields, when appropriate. */
4088 static tree constructor_bit_index;
4090 /* If we are saving up the elements rather than allocating them,
4091 this is the list of elements so far (in reverse order,
4092 most recent first). */
4093 static tree constructor_elements;
4095 /* 1 if constructor should be incrementally stored into a constructor chain,
4096 0 if all the elements should be kept in AVL tree. */
4097 static int constructor_incremental;
4099 /* 1 if so far this constructor's elements are all compile-time constants. */
4100 static int constructor_constant;
4102 /* 1 if so far this constructor's elements are all valid address constants. */
4103 static int constructor_simple;
4105 /* 1 if this constructor is erroneous so far. */
4106 static int constructor_erroneous;
4108 /* Structure for managing pending initializer elements, organized as an
4109 AVL tree. */
4111 struct init_node
4113 struct init_node *left, *right;
4114 struct init_node *parent;
4115 int balance;
4116 tree purpose;
4117 tree value;
4120 /* Tree of pending elements at this constructor level.
4121 These are elements encountered out of order
4122 which belong at places we haven't reached yet in actually
4123 writing the output.
4124 Will never hold tree nodes across GC runs. */
4125 static struct init_node *constructor_pending_elts;
4127 /* The SPELLING_DEPTH of this constructor. */
4128 static int constructor_depth;
4130 /* 0 if implicitly pushing constructor levels is allowed. */
4131 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4133 static int require_constant_value;
4134 static int require_constant_elements;
4136 /* DECL node for which an initializer is being read.
4137 0 means we are reading a constructor expression
4138 such as (struct foo) {...}. */
4139 static tree constructor_decl;
4141 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4142 static const char *constructor_asmspec;
4144 /* Nonzero if this is an initializer for a top-level decl. */
4145 static int constructor_top_level;
4147 /* Nonzero if there were any member designators in this initializer. */
4148 static int constructor_designated;
4150 /* Nesting depth of designator list. */
4151 static int designator_depth;
4153 /* Nonzero if there were diagnosed errors in this designator list. */
4154 static int designator_errorneous;
4157 /* This stack has a level for each implicit or explicit level of
4158 structuring in the initializer, including the outermost one. It
4159 saves the values of most of the variables above. */
4161 struct constructor_range_stack;
4163 struct constructor_stack
4165 struct constructor_stack *next;
4166 tree type;
4167 tree fields;
4168 tree index;
4169 tree max_index;
4170 tree unfilled_index;
4171 tree unfilled_fields;
4172 tree bit_index;
4173 tree elements;
4174 struct init_node *pending_elts;
4175 int offset;
4176 int depth;
4177 /* If nonzero, this value should replace the entire
4178 constructor at this level. */
4179 tree replacement_value;
4180 struct constructor_range_stack *range_stack;
4181 char constant;
4182 char simple;
4183 char implicit;
4184 char erroneous;
4185 char outer;
4186 char incremental;
4187 char designated;
4190 struct constructor_stack *constructor_stack;
4192 /* This stack represents designators from some range designator up to
4193 the last designator in the list. */
4195 struct constructor_range_stack
4197 struct constructor_range_stack *next, *prev;
4198 struct constructor_stack *stack;
4199 tree range_start;
4200 tree index;
4201 tree range_end;
4202 tree fields;
4205 struct constructor_range_stack *constructor_range_stack;
4207 /* This stack records separate initializers that are nested.
4208 Nested initializers can't happen in ANSI C, but GNU C allows them
4209 in cases like { ... (struct foo) { ... } ... }. */
4211 struct initializer_stack
4213 struct initializer_stack *next;
4214 tree decl;
4215 const char *asmspec;
4216 struct constructor_stack *constructor_stack;
4217 struct constructor_range_stack *constructor_range_stack;
4218 tree elements;
4219 struct spelling *spelling;
4220 struct spelling *spelling_base;
4221 int spelling_size;
4222 char top_level;
4223 char require_constant_value;
4224 char require_constant_elements;
4227 struct initializer_stack *initializer_stack;
4229 /* Prepare to parse and output the initializer for variable DECL. */
4231 void
4232 start_init (tree decl, tree asmspec_tree, int top_level)
4234 const char *locus;
4235 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4236 const char *asmspec = 0;
4238 if (asmspec_tree)
4239 asmspec = TREE_STRING_POINTER (asmspec_tree);
4241 p->decl = constructor_decl;
4242 p->asmspec = constructor_asmspec;
4243 p->require_constant_value = require_constant_value;
4244 p->require_constant_elements = require_constant_elements;
4245 p->constructor_stack = constructor_stack;
4246 p->constructor_range_stack = constructor_range_stack;
4247 p->elements = constructor_elements;
4248 p->spelling = spelling;
4249 p->spelling_base = spelling_base;
4250 p->spelling_size = spelling_size;
4251 p->top_level = constructor_top_level;
4252 p->next = initializer_stack;
4253 initializer_stack = p;
4255 constructor_decl = decl;
4256 constructor_asmspec = asmspec;
4257 constructor_designated = 0;
4258 constructor_top_level = top_level;
4260 if (decl != 0)
4262 require_constant_value = TREE_STATIC (decl);
4263 require_constant_elements
4264 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4265 /* For a scalar, you can always use any value to initialize,
4266 even within braces. */
4267 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4268 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4269 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4270 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4271 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4273 else
4275 require_constant_value = 0;
4276 require_constant_elements = 0;
4277 locus = "(anonymous)";
4280 constructor_stack = 0;
4281 constructor_range_stack = 0;
4283 missing_braces_mentioned = 0;
4285 spelling_base = 0;
4286 spelling_size = 0;
4287 RESTORE_SPELLING_DEPTH (0);
4289 if (locus)
4290 push_string (locus);
4293 void
4294 finish_init (void)
4296 struct initializer_stack *p = initializer_stack;
4298 /* Free the whole constructor stack of this initializer. */
4299 while (constructor_stack)
4301 struct constructor_stack *q = constructor_stack;
4302 constructor_stack = q->next;
4303 free (q);
4306 if (constructor_range_stack)
4307 abort ();
4309 /* Pop back to the data of the outer initializer (if any). */
4310 free (spelling_base);
4312 constructor_decl = p->decl;
4313 constructor_asmspec = p->asmspec;
4314 require_constant_value = p->require_constant_value;
4315 require_constant_elements = p->require_constant_elements;
4316 constructor_stack = p->constructor_stack;
4317 constructor_range_stack = p->constructor_range_stack;
4318 constructor_elements = p->elements;
4319 spelling = p->spelling;
4320 spelling_base = p->spelling_base;
4321 spelling_size = p->spelling_size;
4322 constructor_top_level = p->top_level;
4323 initializer_stack = p->next;
4324 free (p);
4327 /* Call here when we see the initializer is surrounded by braces.
4328 This is instead of a call to push_init_level;
4329 it is matched by a call to pop_init_level.
4331 TYPE is the type to initialize, for a constructor expression.
4332 For an initializer for a decl, TYPE is zero. */
4334 void
4335 really_start_incremental_init (tree type)
4337 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4339 if (type == 0)
4340 type = TREE_TYPE (constructor_decl);
4342 if (targetm.vector_opaque_p (type))
4343 error ("opaque vector types cannot be initialized");
4345 p->type = constructor_type;
4346 p->fields = constructor_fields;
4347 p->index = constructor_index;
4348 p->max_index = constructor_max_index;
4349 p->unfilled_index = constructor_unfilled_index;
4350 p->unfilled_fields = constructor_unfilled_fields;
4351 p->bit_index = constructor_bit_index;
4352 p->elements = constructor_elements;
4353 p->constant = constructor_constant;
4354 p->simple = constructor_simple;
4355 p->erroneous = constructor_erroneous;
4356 p->pending_elts = constructor_pending_elts;
4357 p->depth = constructor_depth;
4358 p->replacement_value = 0;
4359 p->implicit = 0;
4360 p->range_stack = 0;
4361 p->outer = 0;
4362 p->incremental = constructor_incremental;
4363 p->designated = constructor_designated;
4364 p->next = 0;
4365 constructor_stack = p;
4367 constructor_constant = 1;
4368 constructor_simple = 1;
4369 constructor_depth = SPELLING_DEPTH ();
4370 constructor_elements = 0;
4371 constructor_pending_elts = 0;
4372 constructor_type = type;
4373 constructor_incremental = 1;
4374 constructor_designated = 0;
4375 designator_depth = 0;
4376 designator_errorneous = 0;
4378 if (TREE_CODE (constructor_type) == RECORD_TYPE
4379 || TREE_CODE (constructor_type) == UNION_TYPE)
4381 constructor_fields = TYPE_FIELDS (constructor_type);
4382 /* Skip any nameless bit fields at the beginning. */
4383 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4384 && DECL_NAME (constructor_fields) == 0)
4385 constructor_fields = TREE_CHAIN (constructor_fields);
4387 constructor_unfilled_fields = constructor_fields;
4388 constructor_bit_index = bitsize_zero_node;
4390 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4392 if (TYPE_DOMAIN (constructor_type))
4394 constructor_max_index
4395 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4397 /* Detect non-empty initializations of zero-length arrays. */
4398 if (constructor_max_index == NULL_TREE
4399 && TYPE_SIZE (constructor_type))
4400 constructor_max_index = build_int_2 (-1, -1);
4402 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4403 to initialize VLAs will cause a proper error; avoid tree
4404 checking errors as well by setting a safe value. */
4405 if (constructor_max_index
4406 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4407 constructor_max_index = build_int_2 (-1, -1);
4409 constructor_index
4410 = convert (bitsizetype,
4411 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4413 else
4414 constructor_index = bitsize_zero_node;
4416 constructor_unfilled_index = constructor_index;
4418 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4420 /* Vectors are like simple fixed-size arrays. */
4421 constructor_max_index =
4422 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4423 constructor_index = convert (bitsizetype, bitsize_zero_node);
4424 constructor_unfilled_index = constructor_index;
4426 else
4428 /* Handle the case of int x = {5}; */
4429 constructor_fields = constructor_type;
4430 constructor_unfilled_fields = constructor_type;
4434 /* Push down into a subobject, for initialization.
4435 If this is for an explicit set of braces, IMPLICIT is 0.
4436 If it is because the next element belongs at a lower level,
4437 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4439 void
4440 push_init_level (int implicit)
4442 struct constructor_stack *p;
4443 tree value = NULL_TREE;
4445 /* If we've exhausted any levels that didn't have braces,
4446 pop them now. */
4447 while (constructor_stack->implicit)
4449 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4450 || TREE_CODE (constructor_type) == UNION_TYPE)
4451 && constructor_fields == 0)
4452 process_init_element (pop_init_level (1));
4453 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4454 && constructor_max_index
4455 && tree_int_cst_lt (constructor_max_index, constructor_index))
4456 process_init_element (pop_init_level (1));
4457 else
4458 break;
4461 /* Unless this is an explicit brace, we need to preserve previous
4462 content if any. */
4463 if (implicit)
4465 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4466 || TREE_CODE (constructor_type) == UNION_TYPE)
4467 && constructor_fields)
4468 value = find_init_member (constructor_fields);
4469 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4470 value = find_init_member (constructor_index);
4473 p = xmalloc (sizeof (struct constructor_stack));
4474 p->type = constructor_type;
4475 p->fields = constructor_fields;
4476 p->index = constructor_index;
4477 p->max_index = constructor_max_index;
4478 p->unfilled_index = constructor_unfilled_index;
4479 p->unfilled_fields = constructor_unfilled_fields;
4480 p->bit_index = constructor_bit_index;
4481 p->elements = constructor_elements;
4482 p->constant = constructor_constant;
4483 p->simple = constructor_simple;
4484 p->erroneous = constructor_erroneous;
4485 p->pending_elts = constructor_pending_elts;
4486 p->depth = constructor_depth;
4487 p->replacement_value = 0;
4488 p->implicit = implicit;
4489 p->outer = 0;
4490 p->incremental = constructor_incremental;
4491 p->designated = constructor_designated;
4492 p->next = constructor_stack;
4493 p->range_stack = 0;
4494 constructor_stack = p;
4496 constructor_constant = 1;
4497 constructor_simple = 1;
4498 constructor_depth = SPELLING_DEPTH ();
4499 constructor_elements = 0;
4500 constructor_incremental = 1;
4501 constructor_designated = 0;
4502 constructor_pending_elts = 0;
4503 if (!implicit)
4505 p->range_stack = constructor_range_stack;
4506 constructor_range_stack = 0;
4507 designator_depth = 0;
4508 designator_errorneous = 0;
4511 /* Don't die if an entire brace-pair level is superfluous
4512 in the containing level. */
4513 if (constructor_type == 0)
4515 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4516 || TREE_CODE (constructor_type) == UNION_TYPE)
4518 /* Don't die if there are extra init elts at the end. */
4519 if (constructor_fields == 0)
4520 constructor_type = 0;
4521 else
4523 constructor_type = TREE_TYPE (constructor_fields);
4524 push_member_name (constructor_fields);
4525 constructor_depth++;
4528 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4530 constructor_type = TREE_TYPE (constructor_type);
4531 push_array_bounds (tree_low_cst (constructor_index, 0));
4532 constructor_depth++;
4535 if (constructor_type == 0)
4537 error_init ("extra brace group at end of initializer");
4538 constructor_fields = 0;
4539 constructor_unfilled_fields = 0;
4540 return;
4543 if (value && TREE_CODE (value) == CONSTRUCTOR)
4545 constructor_constant = TREE_CONSTANT (value);
4546 constructor_simple = TREE_STATIC (value);
4547 constructor_elements = CONSTRUCTOR_ELTS (value);
4548 if (constructor_elements
4549 && (TREE_CODE (constructor_type) == RECORD_TYPE
4550 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4551 set_nonincremental_init ();
4554 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4556 missing_braces_mentioned = 1;
4557 warning_init ("missing braces around initializer");
4560 if (TREE_CODE (constructor_type) == RECORD_TYPE
4561 || TREE_CODE (constructor_type) == UNION_TYPE)
4563 constructor_fields = TYPE_FIELDS (constructor_type);
4564 /* Skip any nameless bit fields at the beginning. */
4565 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4566 && DECL_NAME (constructor_fields) == 0)
4567 constructor_fields = TREE_CHAIN (constructor_fields);
4569 constructor_unfilled_fields = constructor_fields;
4570 constructor_bit_index = bitsize_zero_node;
4572 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4574 /* Vectors are like simple fixed-size arrays. */
4575 constructor_max_index =
4576 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4577 constructor_index = convert (bitsizetype, integer_zero_node);
4578 constructor_unfilled_index = constructor_index;
4580 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4582 if (TYPE_DOMAIN (constructor_type))
4584 constructor_max_index
4585 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4587 /* Detect non-empty initializations of zero-length arrays. */
4588 if (constructor_max_index == NULL_TREE
4589 && TYPE_SIZE (constructor_type))
4590 constructor_max_index = build_int_2 (-1, -1);
4592 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4593 to initialize VLAs will cause a proper error; avoid tree
4594 checking errors as well by setting a safe value. */
4595 if (constructor_max_index
4596 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4597 constructor_max_index = build_int_2 (-1, -1);
4599 constructor_index
4600 = convert (bitsizetype,
4601 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4603 else
4604 constructor_index = bitsize_zero_node;
4606 constructor_unfilled_index = constructor_index;
4607 if (value && TREE_CODE (value) == STRING_CST)
4609 /* We need to split the char/wchar array into individual
4610 characters, so that we don't have to special case it
4611 everywhere. */
4612 set_nonincremental_init_from_string (value);
4615 else
4617 warning_init ("braces around scalar initializer");
4618 constructor_fields = constructor_type;
4619 constructor_unfilled_fields = constructor_type;
4623 /* At the end of an implicit or explicit brace level,
4624 finish up that level of constructor.
4625 If we were outputting the elements as they are read, return 0
4626 from inner levels (process_init_element ignores that),
4627 but return error_mark_node from the outermost level
4628 (that's what we want to put in DECL_INITIAL).
4629 Otherwise, return a CONSTRUCTOR expression. */
4631 tree
4632 pop_init_level (int implicit)
4634 struct constructor_stack *p;
4635 tree constructor = 0;
4637 if (implicit == 0)
4639 /* When we come to an explicit close brace,
4640 pop any inner levels that didn't have explicit braces. */
4641 while (constructor_stack->implicit)
4642 process_init_element (pop_init_level (1));
4644 if (constructor_range_stack)
4645 abort ();
4648 /* Now output all pending elements. */
4649 constructor_incremental = 1;
4650 output_pending_init_elements (1);
4652 p = constructor_stack;
4654 /* Error for initializing a flexible array member, or a zero-length
4655 array member in an inappropriate context. */
4656 if (constructor_type && constructor_fields
4657 && TREE_CODE (constructor_type) == ARRAY_TYPE
4658 && TYPE_DOMAIN (constructor_type)
4659 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4661 /* Silently discard empty initializations. The parser will
4662 already have pedwarned for empty brackets. */
4663 if (integer_zerop (constructor_unfilled_index))
4664 constructor_type = NULL_TREE;
4665 else if (! TYPE_SIZE (constructor_type))
4667 if (constructor_depth > 2)
4668 error_init ("initialization of flexible array member in a nested context");
4669 else if (pedantic)
4670 pedwarn_init ("initialization of a flexible array member");
4672 /* We have already issued an error message for the existence
4673 of a flexible array member not at the end of the structure.
4674 Discard the initializer so that we do not abort later. */
4675 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4676 constructor_type = NULL_TREE;
4678 else
4679 /* Zero-length arrays are no longer special, so we should no longer
4680 get here. */
4681 abort ();
4684 /* Warn when some struct elements are implicitly initialized to zero. */
4685 if (extra_warnings
4686 && constructor_type
4687 && TREE_CODE (constructor_type) == RECORD_TYPE
4688 && constructor_unfilled_fields)
4690 /* Do not warn for flexible array members or zero-length arrays. */
4691 while (constructor_unfilled_fields
4692 && (! DECL_SIZE (constructor_unfilled_fields)
4693 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4694 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4696 /* Do not warn if this level of the initializer uses member
4697 designators; it is likely to be deliberate. */
4698 if (constructor_unfilled_fields && !constructor_designated)
4700 push_member_name (constructor_unfilled_fields);
4701 warning_init ("missing initializer");
4702 RESTORE_SPELLING_DEPTH (constructor_depth);
4706 /* Pad out the end of the structure. */
4707 if (p->replacement_value)
4708 /* If this closes a superfluous brace pair,
4709 just pass out the element between them. */
4710 constructor = p->replacement_value;
4711 else if (constructor_type == 0)
4713 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4714 && TREE_CODE (constructor_type) != UNION_TYPE
4715 && TREE_CODE (constructor_type) != ARRAY_TYPE
4716 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4718 /* A nonincremental scalar initializer--just return
4719 the element, after verifying there is just one. */
4720 if (constructor_elements == 0)
4722 if (!constructor_erroneous)
4723 error_init ("empty scalar initializer");
4724 constructor = error_mark_node;
4726 else if (TREE_CHAIN (constructor_elements) != 0)
4728 error_init ("extra elements in scalar initializer");
4729 constructor = TREE_VALUE (constructor_elements);
4731 else
4732 constructor = TREE_VALUE (constructor_elements);
4734 else
4736 if (constructor_erroneous)
4737 constructor = error_mark_node;
4738 else
4740 constructor = build_constructor (constructor_type,
4741 nreverse (constructor_elements));
4742 if (constructor_constant)
4743 TREE_CONSTANT (constructor) = 1;
4744 if (constructor_constant && constructor_simple)
4745 TREE_STATIC (constructor) = 1;
4749 constructor_type = p->type;
4750 constructor_fields = p->fields;
4751 constructor_index = p->index;
4752 constructor_max_index = p->max_index;
4753 constructor_unfilled_index = p->unfilled_index;
4754 constructor_unfilled_fields = p->unfilled_fields;
4755 constructor_bit_index = p->bit_index;
4756 constructor_elements = p->elements;
4757 constructor_constant = p->constant;
4758 constructor_simple = p->simple;
4759 constructor_erroneous = p->erroneous;
4760 constructor_incremental = p->incremental;
4761 constructor_designated = p->designated;
4762 constructor_pending_elts = p->pending_elts;
4763 constructor_depth = p->depth;
4764 if (!p->implicit)
4765 constructor_range_stack = p->range_stack;
4766 RESTORE_SPELLING_DEPTH (constructor_depth);
4768 constructor_stack = p->next;
4769 free (p);
4771 if (constructor == 0)
4773 if (constructor_stack == 0)
4774 return error_mark_node;
4775 return NULL_TREE;
4777 return constructor;
4780 /* Common handling for both array range and field name designators.
4781 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4783 static int
4784 set_designator (int array)
4786 tree subtype;
4787 enum tree_code subcode;
4789 /* Don't die if an entire brace-pair level is superfluous
4790 in the containing level. */
4791 if (constructor_type == 0)
4792 return 1;
4794 /* If there were errors in this designator list already, bail out silently. */
4795 if (designator_errorneous)
4796 return 1;
4798 if (!designator_depth)
4800 if (constructor_range_stack)
4801 abort ();
4803 /* Designator list starts at the level of closest explicit
4804 braces. */
4805 while (constructor_stack->implicit)
4806 process_init_element (pop_init_level (1));
4807 constructor_designated = 1;
4808 return 0;
4811 if (constructor_no_implicit)
4813 error_init ("initialization designators may not nest");
4814 return 1;
4817 if (TREE_CODE (constructor_type) == RECORD_TYPE
4818 || TREE_CODE (constructor_type) == UNION_TYPE)
4820 subtype = TREE_TYPE (constructor_fields);
4821 if (subtype != error_mark_node)
4822 subtype = TYPE_MAIN_VARIANT (subtype);
4824 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4826 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4828 else
4829 abort ();
4831 subcode = TREE_CODE (subtype);
4832 if (array && subcode != ARRAY_TYPE)
4834 error_init ("array index in non-array initializer");
4835 return 1;
4837 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4839 error_init ("field name not in record or union initializer");
4840 return 1;
4843 constructor_designated = 1;
4844 push_init_level (2);
4845 return 0;
4848 /* If there are range designators in designator list, push a new designator
4849 to constructor_range_stack. RANGE_END is end of such stack range or
4850 NULL_TREE if there is no range designator at this level. */
4852 static void
4853 push_range_stack (tree range_end)
4855 struct constructor_range_stack *p;
4857 p = ggc_alloc (sizeof (struct constructor_range_stack));
4858 p->prev = constructor_range_stack;
4859 p->next = 0;
4860 p->fields = constructor_fields;
4861 p->range_start = constructor_index;
4862 p->index = constructor_index;
4863 p->stack = constructor_stack;
4864 p->range_end = range_end;
4865 if (constructor_range_stack)
4866 constructor_range_stack->next = p;
4867 constructor_range_stack = p;
4870 /* Within an array initializer, specify the next index to be initialized.
4871 FIRST is that index. If LAST is nonzero, then initialize a range
4872 of indices, running from FIRST through LAST. */
4874 void
4875 set_init_index (tree first, tree last)
4877 if (set_designator (1))
4878 return;
4880 designator_errorneous = 1;
4882 while ((TREE_CODE (first) == NOP_EXPR
4883 || TREE_CODE (first) == CONVERT_EXPR
4884 || TREE_CODE (first) == NON_LVALUE_EXPR)
4885 && (TYPE_MODE (TREE_TYPE (first))
4886 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4887 first = TREE_OPERAND (first, 0);
4889 if (last)
4890 while ((TREE_CODE (last) == NOP_EXPR
4891 || TREE_CODE (last) == CONVERT_EXPR
4892 || TREE_CODE (last) == NON_LVALUE_EXPR)
4893 && (TYPE_MODE (TREE_TYPE (last))
4894 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4895 last = TREE_OPERAND (last, 0);
4897 if (TREE_CODE (first) != INTEGER_CST)
4898 error_init ("nonconstant array index in initializer");
4899 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4900 error_init ("nonconstant array index in initializer");
4901 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4902 error_init ("array index in non-array initializer");
4903 else if (tree_int_cst_sgn (first) == -1)
4904 error_init ("array index in initializer exceeds array bounds");
4905 else if (constructor_max_index
4906 && tree_int_cst_lt (constructor_max_index, first))
4907 error_init ("array index in initializer exceeds array bounds");
4908 else
4910 constructor_index = convert (bitsizetype, first);
4912 if (last)
4914 if (tree_int_cst_equal (first, last))
4915 last = 0;
4916 else if (tree_int_cst_lt (last, first))
4918 error_init ("empty index range in initializer");
4919 last = 0;
4921 else
4923 last = convert (bitsizetype, last);
4924 if (constructor_max_index != 0
4925 && tree_int_cst_lt (constructor_max_index, last))
4927 error_init ("array index range in initializer exceeds array bounds");
4928 last = 0;
4933 designator_depth++;
4934 designator_errorneous = 0;
4935 if (constructor_range_stack || last)
4936 push_range_stack (last);
4940 /* Within a struct initializer, specify the next field to be initialized. */
4942 void
4943 set_init_label (tree fieldname)
4945 tree tail;
4947 if (set_designator (0))
4948 return;
4950 designator_errorneous = 1;
4952 if (TREE_CODE (constructor_type) != RECORD_TYPE
4953 && TREE_CODE (constructor_type) != UNION_TYPE)
4955 error_init ("field name not in record or union initializer");
4956 return;
4959 for (tail = TYPE_FIELDS (constructor_type); tail;
4960 tail = TREE_CHAIN (tail))
4962 if (DECL_NAME (tail) == fieldname)
4963 break;
4966 if (tail == 0)
4967 error ("unknown field `%s' specified in initializer",
4968 IDENTIFIER_POINTER (fieldname));
4969 else
4971 constructor_fields = tail;
4972 designator_depth++;
4973 designator_errorneous = 0;
4974 if (constructor_range_stack)
4975 push_range_stack (NULL_TREE);
4979 /* Add a new initializer to the tree of pending initializers. PURPOSE
4980 identifies the initializer, either array index or field in a structure.
4981 VALUE is the value of that index or field. */
4983 static void
4984 add_pending_init (tree purpose, tree value)
4986 struct init_node *p, **q, *r;
4988 q = &constructor_pending_elts;
4989 p = 0;
4991 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4993 while (*q != 0)
4995 p = *q;
4996 if (tree_int_cst_lt (purpose, p->purpose))
4997 q = &p->left;
4998 else if (tree_int_cst_lt (p->purpose, purpose))
4999 q = &p->right;
5000 else
5002 if (TREE_SIDE_EFFECTS (p->value))
5003 warning_init ("initialized field with side-effects overwritten");
5004 p->value = value;
5005 return;
5009 else
5011 tree bitpos;
5013 bitpos = bit_position (purpose);
5014 while (*q != NULL)
5016 p = *q;
5017 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5018 q = &p->left;
5019 else if (p->purpose != purpose)
5020 q = &p->right;
5021 else
5023 if (TREE_SIDE_EFFECTS (p->value))
5024 warning_init ("initialized field with side-effects overwritten");
5025 p->value = value;
5026 return;
5031 r = ggc_alloc (sizeof (struct init_node));
5032 r->purpose = purpose;
5033 r->value = value;
5035 *q = r;
5036 r->parent = p;
5037 r->left = 0;
5038 r->right = 0;
5039 r->balance = 0;
5041 while (p)
5043 struct init_node *s;
5045 if (r == p->left)
5047 if (p->balance == 0)
5048 p->balance = -1;
5049 else if (p->balance < 0)
5051 if (r->balance < 0)
5053 /* L rotation. */
5054 p->left = r->right;
5055 if (p->left)
5056 p->left->parent = p;
5057 r->right = p;
5059 p->balance = 0;
5060 r->balance = 0;
5062 s = p->parent;
5063 p->parent = r;
5064 r->parent = s;
5065 if (s)
5067 if (s->left == p)
5068 s->left = r;
5069 else
5070 s->right = r;
5072 else
5073 constructor_pending_elts = r;
5075 else
5077 /* LR rotation. */
5078 struct init_node *t = r->right;
5080 r->right = t->left;
5081 if (r->right)
5082 r->right->parent = r;
5083 t->left = r;
5085 p->left = t->right;
5086 if (p->left)
5087 p->left->parent = p;
5088 t->right = p;
5090 p->balance = t->balance < 0;
5091 r->balance = -(t->balance > 0);
5092 t->balance = 0;
5094 s = p->parent;
5095 p->parent = t;
5096 r->parent = t;
5097 t->parent = s;
5098 if (s)
5100 if (s->left == p)
5101 s->left = t;
5102 else
5103 s->right = t;
5105 else
5106 constructor_pending_elts = t;
5108 break;
5110 else
5112 /* p->balance == +1; growth of left side balances the node. */
5113 p->balance = 0;
5114 break;
5117 else /* r == p->right */
5119 if (p->balance == 0)
5120 /* Growth propagation from right side. */
5121 p->balance++;
5122 else if (p->balance > 0)
5124 if (r->balance > 0)
5126 /* R rotation. */
5127 p->right = r->left;
5128 if (p->right)
5129 p->right->parent = p;
5130 r->left = p;
5132 p->balance = 0;
5133 r->balance = 0;
5135 s = p->parent;
5136 p->parent = r;
5137 r->parent = s;
5138 if (s)
5140 if (s->left == p)
5141 s->left = r;
5142 else
5143 s->right = r;
5145 else
5146 constructor_pending_elts = r;
5148 else /* r->balance == -1 */
5150 /* RL rotation */
5151 struct init_node *t = r->left;
5153 r->left = t->right;
5154 if (r->left)
5155 r->left->parent = r;
5156 t->right = r;
5158 p->right = t->left;
5159 if (p->right)
5160 p->right->parent = p;
5161 t->left = p;
5163 r->balance = (t->balance < 0);
5164 p->balance = -(t->balance > 0);
5165 t->balance = 0;
5167 s = p->parent;
5168 p->parent = t;
5169 r->parent = t;
5170 t->parent = s;
5171 if (s)
5173 if (s->left == p)
5174 s->left = t;
5175 else
5176 s->right = t;
5178 else
5179 constructor_pending_elts = t;
5181 break;
5183 else
5185 /* p->balance == -1; growth of right side balances the node. */
5186 p->balance = 0;
5187 break;
5191 r = p;
5192 p = p->parent;
5196 /* Build AVL tree from a sorted chain. */
5198 static void
5199 set_nonincremental_init (void)
5201 tree chain;
5203 if (TREE_CODE (constructor_type) != RECORD_TYPE
5204 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5205 return;
5207 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5208 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5209 constructor_elements = 0;
5210 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5212 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5213 /* Skip any nameless bit fields at the beginning. */
5214 while (constructor_unfilled_fields != 0
5215 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5216 && DECL_NAME (constructor_unfilled_fields) == 0)
5217 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5220 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5222 if (TYPE_DOMAIN (constructor_type))
5223 constructor_unfilled_index
5224 = convert (bitsizetype,
5225 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5226 else
5227 constructor_unfilled_index = bitsize_zero_node;
5229 constructor_incremental = 0;
5232 /* Build AVL tree from a string constant. */
5234 static void
5235 set_nonincremental_init_from_string (tree str)
5237 tree value, purpose, type;
5238 HOST_WIDE_INT val[2];
5239 const char *p, *end;
5240 int byte, wchar_bytes, charwidth, bitpos;
5242 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5243 abort ();
5245 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5246 == TYPE_PRECISION (char_type_node))
5247 wchar_bytes = 1;
5248 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5249 == TYPE_PRECISION (wchar_type_node))
5250 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5251 else
5252 abort ();
5254 charwidth = TYPE_PRECISION (char_type_node);
5255 type = TREE_TYPE (constructor_type);
5256 p = TREE_STRING_POINTER (str);
5257 end = p + TREE_STRING_LENGTH (str);
5259 for (purpose = bitsize_zero_node;
5260 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5261 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5263 if (wchar_bytes == 1)
5265 val[1] = (unsigned char) *p++;
5266 val[0] = 0;
5268 else
5270 val[0] = 0;
5271 val[1] = 0;
5272 for (byte = 0; byte < wchar_bytes; byte++)
5274 if (BYTES_BIG_ENDIAN)
5275 bitpos = (wchar_bytes - byte - 1) * charwidth;
5276 else
5277 bitpos = byte * charwidth;
5278 val[bitpos < HOST_BITS_PER_WIDE_INT]
5279 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5280 << (bitpos % HOST_BITS_PER_WIDE_INT);
5284 if (!TREE_UNSIGNED (type))
5286 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5287 if (bitpos < HOST_BITS_PER_WIDE_INT)
5289 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5291 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5292 val[0] = -1;
5295 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5297 if (val[1] < 0)
5298 val[0] = -1;
5300 else if (val[0] & (((HOST_WIDE_INT) 1)
5301 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5302 val[0] |= ((HOST_WIDE_INT) -1)
5303 << (bitpos - HOST_BITS_PER_WIDE_INT);
5306 value = build_int_2 (val[1], val[0]);
5307 TREE_TYPE (value) = type;
5308 add_pending_init (purpose, value);
5311 constructor_incremental = 0;
5314 /* Return value of FIELD in pending initializer or zero if the field was
5315 not initialized yet. */
5317 static tree
5318 find_init_member (tree field)
5320 struct init_node *p;
5322 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5324 if (constructor_incremental
5325 && tree_int_cst_lt (field, constructor_unfilled_index))
5326 set_nonincremental_init ();
5328 p = constructor_pending_elts;
5329 while (p)
5331 if (tree_int_cst_lt (field, p->purpose))
5332 p = p->left;
5333 else if (tree_int_cst_lt (p->purpose, field))
5334 p = p->right;
5335 else
5336 return p->value;
5339 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5341 tree bitpos = bit_position (field);
5343 if (constructor_incremental
5344 && (!constructor_unfilled_fields
5345 || tree_int_cst_lt (bitpos,
5346 bit_position (constructor_unfilled_fields))))
5347 set_nonincremental_init ();
5349 p = constructor_pending_elts;
5350 while (p)
5352 if (field == p->purpose)
5353 return p->value;
5354 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5355 p = p->left;
5356 else
5357 p = p->right;
5360 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5362 if (constructor_elements
5363 && TREE_PURPOSE (constructor_elements) == field)
5364 return TREE_VALUE (constructor_elements);
5366 return 0;
5369 /* "Output" the next constructor element.
5370 At top level, really output it to assembler code now.
5371 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5372 TYPE is the data type that the containing data type wants here.
5373 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5375 PENDING if non-nil means output pending elements that belong
5376 right after this element. (PENDING is normally 1;
5377 it is 0 while outputting pending elements, to avoid recursion.) */
5379 static void
5380 output_init_element (tree value, tree type, tree field, int pending)
5382 if (type == error_mark_node)
5384 constructor_erroneous = 1;
5385 return;
5387 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5388 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5389 && !(TREE_CODE (value) == STRING_CST
5390 && TREE_CODE (type) == ARRAY_TYPE
5391 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5392 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5393 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5394 value = default_conversion (value);
5396 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5397 && require_constant_value && !flag_isoc99 && pending)
5399 /* As an extension, allow initializing objects with static storage
5400 duration with compound literals (which are then treated just as
5401 the brace enclosed list they contain). */
5402 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5403 value = DECL_INITIAL (decl);
5406 if (value == error_mark_node)
5407 constructor_erroneous = 1;
5408 else if (!TREE_CONSTANT (value))
5409 constructor_constant = 0;
5410 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5411 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5412 || TREE_CODE (constructor_type) == UNION_TYPE)
5413 && DECL_C_BIT_FIELD (field)
5414 && TREE_CODE (value) != INTEGER_CST))
5415 constructor_simple = 0;
5417 if (require_constant_value && ! TREE_CONSTANT (value))
5419 error_init ("initializer element is not constant");
5420 value = error_mark_node;
5422 else if (require_constant_elements
5423 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5424 pedwarn ("initializer element is not computable at load time");
5426 /* If this field is empty (and not at the end of structure),
5427 don't do anything other than checking the initializer. */
5428 if (field
5429 && (TREE_TYPE (field) == error_mark_node
5430 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5431 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5432 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5433 || TREE_CHAIN (field)))))
5434 return;
5436 value = digest_init (type, value, require_constant_value);
5437 if (value == error_mark_node)
5439 constructor_erroneous = 1;
5440 return;
5443 /* If this element doesn't come next in sequence,
5444 put it on constructor_pending_elts. */
5445 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5446 && (!constructor_incremental
5447 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5449 if (constructor_incremental
5450 && tree_int_cst_lt (field, constructor_unfilled_index))
5451 set_nonincremental_init ();
5453 add_pending_init (field, value);
5454 return;
5456 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5457 && (!constructor_incremental
5458 || field != constructor_unfilled_fields))
5460 /* We do this for records but not for unions. In a union,
5461 no matter which field is specified, it can be initialized
5462 right away since it starts at the beginning of the union. */
5463 if (constructor_incremental)
5465 if (!constructor_unfilled_fields)
5466 set_nonincremental_init ();
5467 else
5469 tree bitpos, unfillpos;
5471 bitpos = bit_position (field);
5472 unfillpos = bit_position (constructor_unfilled_fields);
5474 if (tree_int_cst_lt (bitpos, unfillpos))
5475 set_nonincremental_init ();
5479 add_pending_init (field, value);
5480 return;
5482 else if (TREE_CODE (constructor_type) == UNION_TYPE
5483 && constructor_elements)
5485 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5486 warning_init ("initialized field with side-effects overwritten");
5488 /* We can have just one union field set. */
5489 constructor_elements = 0;
5492 /* Otherwise, output this element either to
5493 constructor_elements or to the assembler file. */
5495 if (field && TREE_CODE (field) == INTEGER_CST)
5496 field = copy_node (field);
5497 constructor_elements
5498 = tree_cons (field, value, constructor_elements);
5500 /* Advance the variable that indicates sequential elements output. */
5501 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5502 constructor_unfilled_index
5503 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5504 bitsize_one_node);
5505 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5507 constructor_unfilled_fields
5508 = TREE_CHAIN (constructor_unfilled_fields);
5510 /* Skip any nameless bit fields. */
5511 while (constructor_unfilled_fields != 0
5512 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5513 && DECL_NAME (constructor_unfilled_fields) == 0)
5514 constructor_unfilled_fields =
5515 TREE_CHAIN (constructor_unfilled_fields);
5517 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5518 constructor_unfilled_fields = 0;
5520 /* Now output any pending elements which have become next. */
5521 if (pending)
5522 output_pending_init_elements (0);
5525 /* Output any pending elements which have become next.
5526 As we output elements, constructor_unfilled_{fields,index}
5527 advances, which may cause other elements to become next;
5528 if so, they too are output.
5530 If ALL is 0, we return when there are
5531 no more pending elements to output now.
5533 If ALL is 1, we output space as necessary so that
5534 we can output all the pending elements. */
5536 static void
5537 output_pending_init_elements (int all)
5539 struct init_node *elt = constructor_pending_elts;
5540 tree next;
5542 retry:
5544 /* Look through the whole pending tree.
5545 If we find an element that should be output now,
5546 output it. Otherwise, set NEXT to the element
5547 that comes first among those still pending. */
5549 next = 0;
5550 while (elt)
5552 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5554 if (tree_int_cst_equal (elt->purpose,
5555 constructor_unfilled_index))
5556 output_init_element (elt->value,
5557 TREE_TYPE (constructor_type),
5558 constructor_unfilled_index, 0);
5559 else if (tree_int_cst_lt (constructor_unfilled_index,
5560 elt->purpose))
5562 /* Advance to the next smaller node. */
5563 if (elt->left)
5564 elt = elt->left;
5565 else
5567 /* We have reached the smallest node bigger than the
5568 current unfilled index. Fill the space first. */
5569 next = elt->purpose;
5570 break;
5573 else
5575 /* Advance to the next bigger node. */
5576 if (elt->right)
5577 elt = elt->right;
5578 else
5580 /* We have reached the biggest node in a subtree. Find
5581 the parent of it, which is the next bigger node. */
5582 while (elt->parent && elt->parent->right == elt)
5583 elt = elt->parent;
5584 elt = elt->parent;
5585 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5586 elt->purpose))
5588 next = elt->purpose;
5589 break;
5594 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5595 || TREE_CODE (constructor_type) == UNION_TYPE)
5597 tree ctor_unfilled_bitpos, elt_bitpos;
5599 /* If the current record is complete we are done. */
5600 if (constructor_unfilled_fields == 0)
5601 break;
5603 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5604 elt_bitpos = bit_position (elt->purpose);
5605 /* We can't compare fields here because there might be empty
5606 fields in between. */
5607 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5609 constructor_unfilled_fields = elt->purpose;
5610 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5611 elt->purpose, 0);
5613 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5615 /* Advance to the next smaller node. */
5616 if (elt->left)
5617 elt = elt->left;
5618 else
5620 /* We have reached the smallest node bigger than the
5621 current unfilled field. Fill the space first. */
5622 next = elt->purpose;
5623 break;
5626 else
5628 /* Advance to the next bigger node. */
5629 if (elt->right)
5630 elt = elt->right;
5631 else
5633 /* We have reached the biggest node in a subtree. Find
5634 the parent of it, which is the next bigger node. */
5635 while (elt->parent && elt->parent->right == elt)
5636 elt = elt->parent;
5637 elt = elt->parent;
5638 if (elt
5639 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5640 bit_position (elt->purpose))))
5642 next = elt->purpose;
5643 break;
5650 /* Ordinarily return, but not if we want to output all
5651 and there are elements left. */
5652 if (! (all && next != 0))
5653 return;
5655 /* If it's not incremental, just skip over the gap, so that after
5656 jumping to retry we will output the next successive element. */
5657 if (TREE_CODE (constructor_type) == RECORD_TYPE
5658 || TREE_CODE (constructor_type) == UNION_TYPE)
5659 constructor_unfilled_fields = next;
5660 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5661 constructor_unfilled_index = next;
5663 /* ELT now points to the node in the pending tree with the next
5664 initializer to output. */
5665 goto retry;
5668 /* Add one non-braced element to the current constructor level.
5669 This adjusts the current position within the constructor's type.
5670 This may also start or terminate implicit levels
5671 to handle a partly-braced initializer.
5673 Once this has found the correct level for the new element,
5674 it calls output_init_element. */
5676 void
5677 process_init_element (tree value)
5679 tree orig_value = value;
5680 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5682 designator_depth = 0;
5683 designator_errorneous = 0;
5685 /* Handle superfluous braces around string cst as in
5686 char x[] = {"foo"}; */
5687 if (string_flag
5688 && constructor_type
5689 && TREE_CODE (constructor_type) == ARRAY_TYPE
5690 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5691 && integer_zerop (constructor_unfilled_index))
5693 if (constructor_stack->replacement_value)
5694 error_init ("excess elements in char array initializer");
5695 constructor_stack->replacement_value = value;
5696 return;
5699 if (constructor_stack->replacement_value != 0)
5701 error_init ("excess elements in struct initializer");
5702 return;
5705 /* Ignore elements of a brace group if it is entirely superfluous
5706 and has already been diagnosed. */
5707 if (constructor_type == 0)
5708 return;
5710 /* If we've exhausted any levels that didn't have braces,
5711 pop them now. */
5712 while (constructor_stack->implicit)
5714 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5715 || TREE_CODE (constructor_type) == UNION_TYPE)
5716 && constructor_fields == 0)
5717 process_init_element (pop_init_level (1));
5718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5719 && (constructor_max_index == 0
5720 || tree_int_cst_lt (constructor_max_index,
5721 constructor_index)))
5722 process_init_element (pop_init_level (1));
5723 else
5724 break;
5727 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5728 if (constructor_range_stack)
5730 /* If value is a compound literal and we'll be just using its
5731 content, don't put it into a SAVE_EXPR. */
5732 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5733 || !require_constant_value
5734 || flag_isoc99)
5735 value = save_expr (value);
5738 while (1)
5740 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5742 tree fieldtype;
5743 enum tree_code fieldcode;
5745 if (constructor_fields == 0)
5747 pedwarn_init ("excess elements in struct initializer");
5748 break;
5751 fieldtype = TREE_TYPE (constructor_fields);
5752 if (fieldtype != error_mark_node)
5753 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5754 fieldcode = TREE_CODE (fieldtype);
5756 /* Error for non-static initialization of a flexible array member. */
5757 if (fieldcode == ARRAY_TYPE
5758 && !require_constant_value
5759 && TYPE_SIZE (fieldtype) == NULL_TREE
5760 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5762 error_init ("non-static initialization of a flexible array member");
5763 break;
5766 /* Accept a string constant to initialize a subarray. */
5767 if (value != 0
5768 && fieldcode == ARRAY_TYPE
5769 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5770 && string_flag)
5771 value = orig_value;
5772 /* Otherwise, if we have come to a subaggregate,
5773 and we don't have an element of its type, push into it. */
5774 else if (value != 0 && !constructor_no_implicit
5775 && value != error_mark_node
5776 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5777 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5778 || fieldcode == UNION_TYPE))
5780 push_init_level (1);
5781 continue;
5784 if (value)
5786 push_member_name (constructor_fields);
5787 output_init_element (value, fieldtype, constructor_fields, 1);
5788 RESTORE_SPELLING_DEPTH (constructor_depth);
5790 else
5791 /* Do the bookkeeping for an element that was
5792 directly output as a constructor. */
5794 /* For a record, keep track of end position of last field. */
5795 if (DECL_SIZE (constructor_fields))
5796 constructor_bit_index
5797 = size_binop (PLUS_EXPR,
5798 bit_position (constructor_fields),
5799 DECL_SIZE (constructor_fields));
5801 /* If the current field was the first one not yet written out,
5802 it isn't now, so update. */
5803 if (constructor_unfilled_fields == constructor_fields)
5805 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5806 /* Skip any nameless bit fields. */
5807 while (constructor_unfilled_fields != 0
5808 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5809 && DECL_NAME (constructor_unfilled_fields) == 0)
5810 constructor_unfilled_fields =
5811 TREE_CHAIN (constructor_unfilled_fields);
5815 constructor_fields = TREE_CHAIN (constructor_fields);
5816 /* Skip any nameless bit fields at the beginning. */
5817 while (constructor_fields != 0
5818 && DECL_C_BIT_FIELD (constructor_fields)
5819 && DECL_NAME (constructor_fields) == 0)
5820 constructor_fields = TREE_CHAIN (constructor_fields);
5822 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5824 tree fieldtype;
5825 enum tree_code fieldcode;
5827 if (constructor_fields == 0)
5829 pedwarn_init ("excess elements in union initializer");
5830 break;
5833 fieldtype = TREE_TYPE (constructor_fields);
5834 if (fieldtype != error_mark_node)
5835 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5836 fieldcode = TREE_CODE (fieldtype);
5838 /* Warn that traditional C rejects initialization of unions.
5839 We skip the warning if the value is zero. This is done
5840 under the assumption that the zero initializer in user
5841 code appears conditioned on e.g. __STDC__ to avoid
5842 "missing initializer" warnings and relies on default
5843 initialization to zero in the traditional C case.
5844 We also skip the warning if the initializer is designated,
5845 again on the assumption that this must be conditional on
5846 __STDC__ anyway (and we've already complained about the
5847 member-designator already). */
5848 if (warn_traditional && !in_system_header && !constructor_designated
5849 && !(value && (integer_zerop (value) || real_zerop (value))))
5850 warning ("traditional C rejects initialization of unions");
5852 /* Accept a string constant to initialize a subarray. */
5853 if (value != 0
5854 && fieldcode == ARRAY_TYPE
5855 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5856 && string_flag)
5857 value = orig_value;
5858 /* Otherwise, if we have come to a subaggregate,
5859 and we don't have an element of its type, push into it. */
5860 else if (value != 0 && !constructor_no_implicit
5861 && value != error_mark_node
5862 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5863 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5864 || fieldcode == UNION_TYPE))
5866 push_init_level (1);
5867 continue;
5870 if (value)
5872 push_member_name (constructor_fields);
5873 output_init_element (value, fieldtype, constructor_fields, 1);
5874 RESTORE_SPELLING_DEPTH (constructor_depth);
5876 else
5877 /* Do the bookkeeping for an element that was
5878 directly output as a constructor. */
5880 constructor_bit_index = DECL_SIZE (constructor_fields);
5881 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5884 constructor_fields = 0;
5886 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5888 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5889 enum tree_code eltcode = TREE_CODE (elttype);
5891 /* Accept a string constant to initialize a subarray. */
5892 if (value != 0
5893 && eltcode == ARRAY_TYPE
5894 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5895 && string_flag)
5896 value = orig_value;
5897 /* Otherwise, if we have come to a subaggregate,
5898 and we don't have an element of its type, push into it. */
5899 else if (value != 0 && !constructor_no_implicit
5900 && value != error_mark_node
5901 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5902 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5903 || eltcode == UNION_TYPE))
5905 push_init_level (1);
5906 continue;
5909 if (constructor_max_index != 0
5910 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5911 || integer_all_onesp (constructor_max_index)))
5913 pedwarn_init ("excess elements in array initializer");
5914 break;
5917 /* Now output the actual element. */
5918 if (value)
5920 push_array_bounds (tree_low_cst (constructor_index, 0));
5921 output_init_element (value, elttype, constructor_index, 1);
5922 RESTORE_SPELLING_DEPTH (constructor_depth);
5925 constructor_index
5926 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5928 if (! value)
5929 /* If we are doing the bookkeeping for an element that was
5930 directly output as a constructor, we must update
5931 constructor_unfilled_index. */
5932 constructor_unfilled_index = constructor_index;
5934 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5936 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5938 /* Do a basic check of initializer size. Note that vectors
5939 always have a fixed size derived from their type. */
5940 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5942 pedwarn_init ("excess elements in vector initializer");
5943 break;
5946 /* Now output the actual element. */
5947 if (value)
5948 output_init_element (value, elttype, constructor_index, 1);
5950 constructor_index
5951 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5953 if (! value)
5954 /* If we are doing the bookkeeping for an element that was
5955 directly output as a constructor, we must update
5956 constructor_unfilled_index. */
5957 constructor_unfilled_index = constructor_index;
5960 /* Handle the sole element allowed in a braced initializer
5961 for a scalar variable. */
5962 else if (constructor_fields == 0)
5964 pedwarn_init ("excess elements in scalar initializer");
5965 break;
5967 else
5969 if (value)
5970 output_init_element (value, constructor_type, NULL_TREE, 1);
5971 constructor_fields = 0;
5974 /* Handle range initializers either at this level or anywhere higher
5975 in the designator stack. */
5976 if (constructor_range_stack)
5978 struct constructor_range_stack *p, *range_stack;
5979 int finish = 0;
5981 range_stack = constructor_range_stack;
5982 constructor_range_stack = 0;
5983 while (constructor_stack != range_stack->stack)
5985 if (!constructor_stack->implicit)
5986 abort ();
5987 process_init_element (pop_init_level (1));
5989 for (p = range_stack;
5990 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
5991 p = p->prev)
5993 if (!constructor_stack->implicit)
5994 abort ();
5995 process_init_element (pop_init_level (1));
5998 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
5999 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6000 finish = 1;
6002 while (1)
6004 constructor_index = p->index;
6005 constructor_fields = p->fields;
6006 if (finish && p->range_end && p->index == p->range_start)
6008 finish = 0;
6009 p->prev = 0;
6011 p = p->next;
6012 if (!p)
6013 break;
6014 push_init_level (2);
6015 p->stack = constructor_stack;
6016 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6017 p->index = p->range_start;
6020 if (!finish)
6021 constructor_range_stack = range_stack;
6022 continue;
6025 break;
6028 constructor_range_stack = 0;
6031 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6032 (guaranteed to be 'volatile' or null) and ARGS (represented using
6033 an ASM_STMT node). */
6034 tree
6035 build_asm_stmt (tree cv_qualifier, tree args)
6037 if (!TREE_OPERAND (args, 0))
6038 TREE_OPERAND (args, 0) = cv_qualifier;
6039 return add_stmt (args);
6042 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6043 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6044 SIMPLE indicates whether there was anything at all after the
6045 string in the asm expression -- asm("blah") and asm("blah" : )
6046 are subtly different. We use a ASM_STMT node to represent this. */
6047 tree
6048 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6049 bool simple)
6051 tree tail;
6052 tree args;
6054 /* We can remove output conversions that change the type,
6055 but not the mode. */
6056 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6058 tree output = TREE_VALUE (tail);
6060 STRIP_NOPS (output);
6061 TREE_VALUE (tail) = output;
6063 /* Allow conversions as LHS here. build_modify_expr as called below
6064 will do the right thing with them. */
6065 while (TREE_CODE (output) == NOP_EXPR
6066 || TREE_CODE (output) == CONVERT_EXPR
6067 || TREE_CODE (output) == FLOAT_EXPR
6068 || TREE_CODE (output) == FIX_TRUNC_EXPR
6069 || TREE_CODE (output) == FIX_FLOOR_EXPR
6070 || TREE_CODE (output) == FIX_ROUND_EXPR
6071 || TREE_CODE (output) == FIX_CEIL_EXPR)
6072 output = TREE_OPERAND (output, 0);
6074 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6077 /* Remove output conversions that change the type but not the mode. */
6078 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6080 tree output = TREE_VALUE (tail);
6081 STRIP_NOPS (output);
6082 TREE_VALUE (tail) = output;
6085 /* Perform default conversions on array and function inputs.
6086 Don't do this for other types as it would screw up operands
6087 expected to be in memory. */
6088 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6089 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6091 args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6093 /* Simple asm statements are treated as volatile. */
6094 if (simple)
6096 TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6097 ASM_INPUT_P (args) = 1;
6099 return args;
6102 /* Expand an ASM statement with operands, handling output operands
6103 that are not variables or INDIRECT_REFS by transforming such
6104 cases into cases that expand_asm_operands can handle.
6106 Arguments are same as for expand_asm_operands. */
6108 void
6109 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6110 tree clobbers, int vol, location_t locus)
6112 int noutputs = list_length (outputs);
6113 int i;
6114 /* o[I] is the place that output number I should be written. */
6115 tree *o = alloca (noutputs * sizeof (tree));
6116 tree tail;
6118 /* Record the contents of OUTPUTS before it is modified. */
6119 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6121 o[i] = TREE_VALUE (tail);
6122 if (o[i] == error_mark_node)
6123 return;
6126 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6127 OUTPUTS some trees for where the values were actually stored. */
6128 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6130 /* Copy all the intermediate outputs into the specified outputs. */
6131 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6133 if (o[i] != TREE_VALUE (tail))
6135 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6136 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6137 free_temp_slots ();
6139 /* Restore the original value so that it's correct the next
6140 time we expand this function. */
6141 TREE_VALUE (tail) = o[i];
6143 /* Detect modification of read-only values.
6144 (Otherwise done by build_modify_expr.) */
6145 else
6147 tree type = TREE_TYPE (o[i]);
6148 if (TREE_READONLY (o[i])
6149 || TYPE_READONLY (type)
6150 || ((TREE_CODE (type) == RECORD_TYPE
6151 || TREE_CODE (type) == UNION_TYPE)
6152 && C_TYPE_FIELDS_READONLY (type)))
6153 readonly_error (o[i], "modification by `asm'");
6157 /* Those MODIFY_EXPRs could do autoincrements. */
6158 emit_queue ();
6161 /* Expand a C `return' statement.
6162 RETVAL is the expression for what to return,
6163 or a null pointer for `return;' with no value. */
6165 tree
6166 c_expand_return (tree retval)
6168 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6170 if (TREE_THIS_VOLATILE (current_function_decl))
6171 warning ("function declared `noreturn' has a `return' statement");
6173 if (!retval)
6175 current_function_returns_null = 1;
6176 if ((warn_return_type || flag_isoc99)
6177 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6178 pedwarn_c99 ("`return' with no value, in function returning non-void");
6180 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6182 current_function_returns_null = 1;
6183 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6184 pedwarn ("`return' with a value, in function returning void");
6186 else
6188 tree t = convert_for_assignment (valtype, retval, _("return"),
6189 NULL_TREE, NULL_TREE, 0);
6190 tree res = DECL_RESULT (current_function_decl);
6191 tree inner;
6193 current_function_returns_value = 1;
6194 if (t == error_mark_node)
6195 return NULL_TREE;
6197 inner = t = convert (TREE_TYPE (res), t);
6199 /* Strip any conversions, additions, and subtractions, and see if
6200 we are returning the address of a local variable. Warn if so. */
6201 while (1)
6203 switch (TREE_CODE (inner))
6205 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6206 case PLUS_EXPR:
6207 inner = TREE_OPERAND (inner, 0);
6208 continue;
6210 case MINUS_EXPR:
6211 /* If the second operand of the MINUS_EXPR has a pointer
6212 type (or is converted from it), this may be valid, so
6213 don't give a warning. */
6215 tree op1 = TREE_OPERAND (inner, 1);
6217 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6218 && (TREE_CODE (op1) == NOP_EXPR
6219 || TREE_CODE (op1) == NON_LVALUE_EXPR
6220 || TREE_CODE (op1) == CONVERT_EXPR))
6221 op1 = TREE_OPERAND (op1, 0);
6223 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6224 break;
6226 inner = TREE_OPERAND (inner, 0);
6227 continue;
6230 case ADDR_EXPR:
6231 inner = TREE_OPERAND (inner, 0);
6233 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6234 inner = TREE_OPERAND (inner, 0);
6236 if (DECL_P (inner)
6237 && ! DECL_EXTERNAL (inner)
6238 && ! TREE_STATIC (inner)
6239 && DECL_CONTEXT (inner) == current_function_decl)
6240 warning ("function returns address of local variable");
6241 break;
6243 default:
6244 break;
6247 break;
6250 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6253 return add_stmt (build_return_stmt (retval));
6256 struct c_switch {
6257 /* The SWITCH_STMT being built. */
6258 tree switch_stmt;
6259 /* A splay-tree mapping the low element of a case range to the high
6260 element, or NULL_TREE if there is no high element. Used to
6261 determine whether or not a new case label duplicates an old case
6262 label. We need a tree, rather than simply a hash table, because
6263 of the GNU case range extension. */
6264 splay_tree cases;
6265 /* The next node on the stack. */
6266 struct c_switch *next;
6269 /* A stack of the currently active switch statements. The innermost
6270 switch statement is on the top of the stack. There is no need to
6271 mark the stack for garbage collection because it is only active
6272 during the processing of the body of a function, and we never
6273 collect at that point. */
6275 static struct c_switch *switch_stack;
6277 /* Start a C switch statement, testing expression EXP. Return the new
6278 SWITCH_STMT. */
6280 tree
6281 c_start_case (tree exp)
6283 enum tree_code code;
6284 tree type, orig_type = error_mark_node;
6285 struct c_switch *cs;
6287 if (exp != error_mark_node)
6289 code = TREE_CODE (TREE_TYPE (exp));
6290 orig_type = TREE_TYPE (exp);
6292 if (! INTEGRAL_TYPE_P (orig_type)
6293 && code != ERROR_MARK)
6295 error ("switch quantity not an integer");
6296 exp = integer_zero_node;
6298 else
6300 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6302 if (warn_traditional && !in_system_header
6303 && (type == long_integer_type_node
6304 || type == long_unsigned_type_node))
6305 warning ("`long' switch expression not converted to `int' in ISO C");
6307 exp = default_conversion (exp);
6308 type = TREE_TYPE (exp);
6312 /* Add this new SWITCH_STMT to the stack. */
6313 cs = xmalloc (sizeof (*cs));
6314 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6315 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6316 cs->next = switch_stack;
6317 switch_stack = cs;
6319 return add_stmt (switch_stack->switch_stmt);
6322 /* Process a case label. */
6324 tree
6325 do_case (tree low_value, tree high_value)
6327 tree label = NULL_TREE;
6329 if (switch_stack)
6331 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6333 label = c_add_case_label (switch_stack->cases,
6334 SWITCH_COND (switch_stack->switch_stmt),
6335 low_value, high_value);
6336 if (label == error_mark_node)
6337 label = NULL_TREE;
6338 else if (switch_was_empty_p)
6340 /* Attach the first case label to the SWITCH_BODY. */
6341 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6342 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6345 else if (low_value)
6346 error ("case label not within a switch statement");
6347 else
6348 error ("`default' label not within a switch statement");
6350 return label;
6353 /* Finish the switch statement. */
6355 void
6356 c_finish_case (void)
6358 struct c_switch *cs = switch_stack;
6360 /* Rechain the next statements to the SWITCH_STMT. */
6361 last_tree = cs->switch_stmt;
6363 /* Pop the stack. */
6364 switch_stack = switch_stack->next;
6365 splay_tree_delete (cs->cases);
6366 free (cs);
6369 /* Build a binary-operation expression without default conversions.
6370 CODE is the kind of expression to build.
6371 This function differs from `build' in several ways:
6372 the data type of the result is computed and recorded in it,
6373 warnings are generated if arg data types are invalid,
6374 special handling for addition and subtraction of pointers is known,
6375 and some optimization is done (operations on narrow ints
6376 are done in the narrower type when that gives the same result).
6377 Constant folding is also done before the result is returned.
6379 Note that the operands will never have enumeral types, or function
6380 or array types, because either they will have the default conversions
6381 performed or they have both just been converted to some other type in which
6382 the arithmetic is to be done. */
6384 tree
6385 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6386 int convert_p)
6388 tree type0, type1;
6389 enum tree_code code0, code1;
6390 tree op0, op1;
6392 /* Expression code to give to the expression when it is built.
6393 Normally this is CODE, which is what the caller asked for,
6394 but in some special cases we change it. */
6395 enum tree_code resultcode = code;
6397 /* Data type in which the computation is to be performed.
6398 In the simplest cases this is the common type of the arguments. */
6399 tree result_type = NULL;
6401 /* Nonzero means operands have already been type-converted
6402 in whatever way is necessary.
6403 Zero means they need to be converted to RESULT_TYPE. */
6404 int converted = 0;
6406 /* Nonzero means create the expression with this type, rather than
6407 RESULT_TYPE. */
6408 tree build_type = 0;
6410 /* Nonzero means after finally constructing the expression
6411 convert it to this type. */
6412 tree final_type = 0;
6414 /* Nonzero if this is an operation like MIN or MAX which can
6415 safely be computed in short if both args are promoted shorts.
6416 Also implies COMMON.
6417 -1 indicates a bitwise operation; this makes a difference
6418 in the exact conditions for when it is safe to do the operation
6419 in a narrower mode. */
6420 int shorten = 0;
6422 /* Nonzero if this is a comparison operation;
6423 if both args are promoted shorts, compare the original shorts.
6424 Also implies COMMON. */
6425 int short_compare = 0;
6427 /* Nonzero if this is a right-shift operation, which can be computed on the
6428 original short and then promoted if the operand is a promoted short. */
6429 int short_shift = 0;
6431 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6432 int common = 0;
6434 if (convert_p)
6436 op0 = default_conversion (orig_op0);
6437 op1 = default_conversion (orig_op1);
6439 else
6441 op0 = orig_op0;
6442 op1 = orig_op1;
6445 type0 = TREE_TYPE (op0);
6446 type1 = TREE_TYPE (op1);
6448 /* The expression codes of the data types of the arguments tell us
6449 whether the arguments are integers, floating, pointers, etc. */
6450 code0 = TREE_CODE (type0);
6451 code1 = TREE_CODE (type1);
6453 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6454 STRIP_TYPE_NOPS (op0);
6455 STRIP_TYPE_NOPS (op1);
6457 /* If an error was already reported for one of the arguments,
6458 avoid reporting another error. */
6460 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6461 return error_mark_node;
6463 switch (code)
6465 case PLUS_EXPR:
6466 /* Handle the pointer + int case. */
6467 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6468 return pointer_int_sum (PLUS_EXPR, op0, op1);
6469 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6470 return pointer_int_sum (PLUS_EXPR, op1, op0);
6471 else
6472 common = 1;
6473 break;
6475 case MINUS_EXPR:
6476 /* Subtraction of two similar pointers.
6477 We must subtract them as integers, then divide by object size. */
6478 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6479 && comp_target_types (type0, type1, 1))
6480 return pointer_diff (op0, op1);
6481 /* Handle pointer minus int. Just like pointer plus int. */
6482 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6483 return pointer_int_sum (MINUS_EXPR, op0, op1);
6484 else
6485 common = 1;
6486 break;
6488 case MULT_EXPR:
6489 common = 1;
6490 break;
6492 case TRUNC_DIV_EXPR:
6493 case CEIL_DIV_EXPR:
6494 case FLOOR_DIV_EXPR:
6495 case ROUND_DIV_EXPR:
6496 case EXACT_DIV_EXPR:
6497 /* Floating point division by zero is a legitimate way to obtain
6498 infinities and NaNs. */
6499 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6500 warning ("division by zero");
6502 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6503 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6504 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6505 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6507 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6508 resultcode = RDIV_EXPR;
6509 else
6510 /* Although it would be tempting to shorten always here, that
6511 loses on some targets, since the modulo instruction is
6512 undefined if the quotient can't be represented in the
6513 computation mode. We shorten only if unsigned or if
6514 dividing by something we know != -1. */
6515 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6516 || (TREE_CODE (op1) == INTEGER_CST
6517 && ! integer_all_onesp (op1)));
6518 common = 1;
6520 break;
6522 case BIT_AND_EXPR:
6523 case BIT_IOR_EXPR:
6524 case BIT_XOR_EXPR:
6525 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6526 shorten = -1;
6527 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6528 common = 1;
6529 break;
6531 case TRUNC_MOD_EXPR:
6532 case FLOOR_MOD_EXPR:
6533 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6534 warning ("division by zero");
6536 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6538 /* Although it would be tempting to shorten always here, that loses
6539 on some targets, since the modulo instruction is undefined if the
6540 quotient can't be represented in the computation mode. We shorten
6541 only if unsigned or if dividing by something we know != -1. */
6542 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6543 || (TREE_CODE (op1) == INTEGER_CST
6544 && ! integer_all_onesp (op1)));
6545 common = 1;
6547 break;
6549 case TRUTH_ANDIF_EXPR:
6550 case TRUTH_ORIF_EXPR:
6551 case TRUTH_AND_EXPR:
6552 case TRUTH_OR_EXPR:
6553 case TRUTH_XOR_EXPR:
6554 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6555 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6556 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6557 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6559 /* Result of these operations is always an int,
6560 but that does not mean the operands should be
6561 converted to ints! */
6562 result_type = integer_type_node;
6563 op0 = lang_hooks.truthvalue_conversion (op0);
6564 op1 = lang_hooks.truthvalue_conversion (op1);
6565 converted = 1;
6567 break;
6569 /* Shift operations: result has same type as first operand;
6570 always convert second operand to int.
6571 Also set SHORT_SHIFT if shifting rightward. */
6573 case RSHIFT_EXPR:
6574 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6576 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6578 if (tree_int_cst_sgn (op1) < 0)
6579 warning ("right shift count is negative");
6580 else
6582 if (! integer_zerop (op1))
6583 short_shift = 1;
6585 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6586 warning ("right shift count >= width of type");
6590 /* Use the type of the value to be shifted. */
6591 result_type = type0;
6592 /* Convert the shift-count to an integer, regardless of size
6593 of value being shifted. */
6594 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6595 op1 = convert (integer_type_node, op1);
6596 /* Avoid converting op1 to result_type later. */
6597 converted = 1;
6599 break;
6601 case LSHIFT_EXPR:
6602 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6604 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6606 if (tree_int_cst_sgn (op1) < 0)
6607 warning ("left shift count is negative");
6609 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6610 warning ("left shift count >= width of type");
6613 /* Use the type of the value to be shifted. */
6614 result_type = type0;
6615 /* Convert the shift-count to an integer, regardless of size
6616 of value being shifted. */
6617 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6618 op1 = convert (integer_type_node, op1);
6619 /* Avoid converting op1 to result_type later. */
6620 converted = 1;
6622 break;
6624 case RROTATE_EXPR:
6625 case LROTATE_EXPR:
6626 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6628 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6630 if (tree_int_cst_sgn (op1) < 0)
6631 warning ("shift count is negative");
6632 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6633 warning ("shift count >= width of type");
6636 /* Use the type of the value to be shifted. */
6637 result_type = type0;
6638 /* Convert the shift-count to an integer, regardless of size
6639 of value being shifted. */
6640 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6641 op1 = convert (integer_type_node, op1);
6642 /* Avoid converting op1 to result_type later. */
6643 converted = 1;
6645 break;
6647 case EQ_EXPR:
6648 case NE_EXPR:
6649 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6650 warning ("comparing floating point with == or != is unsafe");
6651 /* Result of comparison is always int,
6652 but don't convert the args to int! */
6653 build_type = integer_type_node;
6654 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6655 || code0 == COMPLEX_TYPE)
6656 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6657 || code1 == COMPLEX_TYPE))
6658 short_compare = 1;
6659 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6661 tree tt0 = TREE_TYPE (type0);
6662 tree tt1 = TREE_TYPE (type1);
6663 /* Anything compares with void *. void * compares with anything.
6664 Otherwise, the targets must be compatible
6665 and both must be object or both incomplete. */
6666 if (comp_target_types (type0, type1, 1))
6667 result_type = common_type (type0, type1);
6668 else if (VOID_TYPE_P (tt0))
6670 /* op0 != orig_op0 detects the case of something
6671 whose value is 0 but which isn't a valid null ptr const. */
6672 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6673 && TREE_CODE (tt1) == FUNCTION_TYPE)
6674 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6676 else if (VOID_TYPE_P (tt1))
6678 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6679 && TREE_CODE (tt0) == FUNCTION_TYPE)
6680 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6682 else
6683 pedwarn ("comparison of distinct pointer types lacks a cast");
6685 if (result_type == NULL_TREE)
6686 result_type = ptr_type_node;
6688 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6689 && integer_zerop (op1))
6690 result_type = type0;
6691 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6692 && integer_zerop (op0))
6693 result_type = type1;
6694 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6696 result_type = type0;
6697 pedwarn ("comparison between pointer and integer");
6699 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6701 result_type = type1;
6702 pedwarn ("comparison between pointer and integer");
6704 break;
6706 case MAX_EXPR:
6707 case MIN_EXPR:
6708 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6709 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6710 shorten = 1;
6711 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6713 if (comp_target_types (type0, type1, 1))
6715 result_type = common_type (type0, type1);
6716 if (pedantic
6717 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6718 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6720 else
6722 result_type = ptr_type_node;
6723 pedwarn ("comparison of distinct pointer types lacks a cast");
6726 break;
6728 case LE_EXPR:
6729 case GE_EXPR:
6730 case LT_EXPR:
6731 case GT_EXPR:
6732 build_type = integer_type_node;
6733 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6734 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6735 short_compare = 1;
6736 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6738 if (comp_target_types (type0, type1, 1))
6740 result_type = common_type (type0, type1);
6741 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6742 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6743 pedwarn ("comparison of complete and incomplete pointers");
6744 else if (pedantic
6745 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6746 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6748 else
6750 result_type = ptr_type_node;
6751 pedwarn ("comparison of distinct pointer types lacks a cast");
6754 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6755 && integer_zerop (op1))
6757 result_type = type0;
6758 if (pedantic || extra_warnings)
6759 pedwarn ("ordered comparison of pointer with integer zero");
6761 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6762 && integer_zerop (op0))
6764 result_type = type1;
6765 if (pedantic)
6766 pedwarn ("ordered comparison of pointer with integer zero");
6768 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6770 result_type = type0;
6771 pedwarn ("comparison between pointer and integer");
6773 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6775 result_type = type1;
6776 pedwarn ("comparison between pointer and integer");
6778 break;
6780 case UNORDERED_EXPR:
6781 case ORDERED_EXPR:
6782 case UNLT_EXPR:
6783 case UNLE_EXPR:
6784 case UNGT_EXPR:
6785 case UNGE_EXPR:
6786 case UNEQ_EXPR:
6787 build_type = integer_type_node;
6788 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6790 error ("unordered comparison on non-floating point argument");
6791 return error_mark_node;
6793 common = 1;
6794 break;
6796 default:
6797 break;
6800 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6801 return error_mark_node;
6803 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6804 || code0 == VECTOR_TYPE)
6806 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6807 || code1 == VECTOR_TYPE))
6809 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6811 if (shorten || common || short_compare)
6812 result_type = common_type (type0, type1);
6814 /* For certain operations (which identify themselves by shorten != 0)
6815 if both args were extended from the same smaller type,
6816 do the arithmetic in that type and then extend.
6818 shorten !=0 and !=1 indicates a bitwise operation.
6819 For them, this optimization is safe only if
6820 both args are zero-extended or both are sign-extended.
6821 Otherwise, we might change the result.
6822 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6823 but calculated in (unsigned short) it would be (unsigned short)-1. */
6825 if (shorten && none_complex)
6827 int unsigned0, unsigned1;
6828 tree arg0 = get_narrower (op0, &unsigned0);
6829 tree arg1 = get_narrower (op1, &unsigned1);
6830 /* UNS is 1 if the operation to be done is an unsigned one. */
6831 int uns = TREE_UNSIGNED (result_type);
6832 tree type;
6834 final_type = result_type;
6836 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6837 but it *requires* conversion to FINAL_TYPE. */
6839 if ((TYPE_PRECISION (TREE_TYPE (op0))
6840 == TYPE_PRECISION (TREE_TYPE (arg0)))
6841 && TREE_TYPE (op0) != final_type)
6842 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6843 if ((TYPE_PRECISION (TREE_TYPE (op1))
6844 == TYPE_PRECISION (TREE_TYPE (arg1)))
6845 && TREE_TYPE (op1) != final_type)
6846 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6848 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6850 /* For bitwise operations, signedness of nominal type
6851 does not matter. Consider only how operands were extended. */
6852 if (shorten == -1)
6853 uns = unsigned0;
6855 /* Note that in all three cases below we refrain from optimizing
6856 an unsigned operation on sign-extended args.
6857 That would not be valid. */
6859 /* Both args variable: if both extended in same way
6860 from same width, do it in that width.
6861 Do it unsigned if args were zero-extended. */
6862 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6863 < TYPE_PRECISION (result_type))
6864 && (TYPE_PRECISION (TREE_TYPE (arg1))
6865 == TYPE_PRECISION (TREE_TYPE (arg0)))
6866 && unsigned0 == unsigned1
6867 && (unsigned0 || !uns))
6868 result_type
6869 = c_common_signed_or_unsigned_type
6870 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6871 else if (TREE_CODE (arg0) == INTEGER_CST
6872 && (unsigned1 || !uns)
6873 && (TYPE_PRECISION (TREE_TYPE (arg1))
6874 < TYPE_PRECISION (result_type))
6875 && (type
6876 = c_common_signed_or_unsigned_type (unsigned1,
6877 TREE_TYPE (arg1)),
6878 int_fits_type_p (arg0, type)))
6879 result_type = type;
6880 else if (TREE_CODE (arg1) == INTEGER_CST
6881 && (unsigned0 || !uns)
6882 && (TYPE_PRECISION (TREE_TYPE (arg0))
6883 < TYPE_PRECISION (result_type))
6884 && (type
6885 = c_common_signed_or_unsigned_type (unsigned0,
6886 TREE_TYPE (arg0)),
6887 int_fits_type_p (arg1, type)))
6888 result_type = type;
6891 /* Shifts can be shortened if shifting right. */
6893 if (short_shift)
6895 int unsigned_arg;
6896 tree arg0 = get_narrower (op0, &unsigned_arg);
6898 final_type = result_type;
6900 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6901 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6903 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6904 /* We can shorten only if the shift count is less than the
6905 number of bits in the smaller type size. */
6906 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6907 /* We cannot drop an unsigned shift after sign-extension. */
6908 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6910 /* Do an unsigned shift if the operand was zero-extended. */
6911 result_type
6912 = c_common_signed_or_unsigned_type (unsigned_arg,
6913 TREE_TYPE (arg0));
6914 /* Convert value-to-be-shifted to that type. */
6915 if (TREE_TYPE (op0) != result_type)
6916 op0 = convert (result_type, op0);
6917 converted = 1;
6921 /* Comparison operations are shortened too but differently.
6922 They identify themselves by setting short_compare = 1. */
6924 if (short_compare)
6926 /* Don't write &op0, etc., because that would prevent op0
6927 from being kept in a register.
6928 Instead, make copies of the our local variables and
6929 pass the copies by reference, then copy them back afterward. */
6930 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6931 enum tree_code xresultcode = resultcode;
6932 tree val
6933 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
6935 if (val != 0)
6936 return val;
6938 op0 = xop0, op1 = xop1;
6939 converted = 1;
6940 resultcode = xresultcode;
6942 if (warn_sign_compare && skip_evaluation == 0)
6944 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
6945 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
6946 int unsignedp0, unsignedp1;
6947 tree primop0 = get_narrower (op0, &unsignedp0);
6948 tree primop1 = get_narrower (op1, &unsignedp1);
6950 xop0 = orig_op0;
6951 xop1 = orig_op1;
6952 STRIP_TYPE_NOPS (xop0);
6953 STRIP_TYPE_NOPS (xop1);
6955 /* Give warnings for comparisons between signed and unsigned
6956 quantities that may fail.
6958 Do the checking based on the original operand trees, so that
6959 casts will be considered, but default promotions won't be.
6961 Do not warn if the comparison is being done in a signed type,
6962 since the signed type will only be chosen if it can represent
6963 all the values of the unsigned type. */
6964 if (! TREE_UNSIGNED (result_type))
6965 /* OK */;
6966 /* Do not warn if both operands are the same signedness. */
6967 else if (op0_signed == op1_signed)
6968 /* OK */;
6969 else
6971 tree sop, uop;
6973 if (op0_signed)
6974 sop = xop0, uop = xop1;
6975 else
6976 sop = xop1, uop = xop0;
6978 /* Do not warn if the signed quantity is an
6979 unsuffixed integer literal (or some static
6980 constant expression involving such literals or a
6981 conditional expression involving such literals)
6982 and it is non-negative. */
6983 if (c_tree_expr_nonnegative_p (sop))
6984 /* OK */;
6985 /* Do not warn if the comparison is an equality operation,
6986 the unsigned quantity is an integral constant, and it
6987 would fit in the result if the result were signed. */
6988 else if (TREE_CODE (uop) == INTEGER_CST
6989 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
6990 && int_fits_type_p
6991 (uop, c_common_signed_type (result_type)))
6992 /* OK */;
6993 /* Do not warn if the unsigned quantity is an enumeration
6994 constant and its maximum value would fit in the result
6995 if the result were signed. */
6996 else if (TREE_CODE (uop) == INTEGER_CST
6997 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
6998 && int_fits_type_p
6999 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7000 c_common_signed_type (result_type)))
7001 /* OK */;
7002 else
7003 warning ("comparison between signed and unsigned");
7006 /* Warn if two unsigned values are being compared in a size
7007 larger than their original size, and one (and only one) is the
7008 result of a `~' operator. This comparison will always fail.
7010 Also warn if one operand is a constant, and the constant
7011 does not have all bits set that are set in the ~ operand
7012 when it is extended. */
7014 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7015 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7017 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7018 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7019 &unsignedp0);
7020 else
7021 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7022 &unsignedp1);
7024 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7026 tree primop;
7027 HOST_WIDE_INT constant, mask;
7028 int unsignedp, bits;
7030 if (host_integerp (primop0, 0))
7032 primop = primop1;
7033 unsignedp = unsignedp1;
7034 constant = tree_low_cst (primop0, 0);
7036 else
7038 primop = primop0;
7039 unsignedp = unsignedp0;
7040 constant = tree_low_cst (primop1, 0);
7043 bits = TYPE_PRECISION (TREE_TYPE (primop));
7044 if (bits < TYPE_PRECISION (result_type)
7045 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7047 mask = (~ (HOST_WIDE_INT) 0) << bits;
7048 if ((mask & constant) != mask)
7049 warning ("comparison of promoted ~unsigned with constant");
7052 else if (unsignedp0 && unsignedp1
7053 && (TYPE_PRECISION (TREE_TYPE (primop0))
7054 < TYPE_PRECISION (result_type))
7055 && (TYPE_PRECISION (TREE_TYPE (primop1))
7056 < TYPE_PRECISION (result_type)))
7057 warning ("comparison of promoted ~unsigned with unsigned");
7063 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7064 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7065 Then the expression will be built.
7066 It will be given type FINAL_TYPE if that is nonzero;
7067 otherwise, it will be given type RESULT_TYPE. */
7069 if (!result_type)
7071 binary_op_error (code);
7072 return error_mark_node;
7075 if (! converted)
7077 if (TREE_TYPE (op0) != result_type)
7078 op0 = convert (result_type, op0);
7079 if (TREE_TYPE (op1) != result_type)
7080 op1 = convert (result_type, op1);
7083 if (build_type == NULL_TREE)
7084 build_type = result_type;
7087 tree result = build (resultcode, build_type, op0, op1);
7088 tree folded;
7090 /* Treat expressions in initializers specially as they can't trap. */
7091 folded = initializer_stack ? fold_initializer (result)
7092 : fold (result);
7093 if (folded == result)
7094 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7095 if (final_type != 0)
7096 return convert (final_type, folded);
7097 return folded;