2003-07-31 Andreas Tobler <a.tobler@schweiz.ch>
[official-gcc.git] / gcc / c-typeck.c
blob922cda89cf092a16ae60c198e69352103ed90967
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
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 "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static tree qualify_type (tree, tree);
53 static int tagged_types_tu_compatible_p (tree, tree, int);
54 static int comp_target_types (tree, tree, int);
55 static int function_types_compatible_p (tree, tree, int);
56 static int type_lists_compatible_p (tree, tree, int);
57 static tree decl_constant_value_for_broken_optimization (tree);
58 static tree default_function_array_conversion (tree);
59 static tree lookup_field (tree, tree);
60 static tree convert_arguments (tree, tree, tree, tree);
61 static tree pointer_diff (tree, tree);
62 static tree unary_complex_lvalue (enum tree_code, tree, int);
63 static void pedantic_lvalue_warning (enum tree_code);
64 static tree internal_build_compound_expr (tree, int);
65 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66 int);
67 static void warn_for_assignment (const char *, const char *, tree, int);
68 static tree valid_compound_expr_initializer (tree, tree);
69 static void push_string (const char *);
70 static void push_member_name (tree);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree digest_init (tree, tree, int);
76 static void output_init_element (tree, tree, tree, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree);
80 static void add_pending_init (tree, tree);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree);
83 static tree find_init_member (tree);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.) */
88 tree
89 require_complete_type (tree value)
91 tree type = TREE_TYPE (value);
93 if (value == error_mark_node || type == error_mark_node)
94 return error_mark_node;
96 /* First, detect a valid value with a complete type. */
97 if (COMPLETE_TYPE_P (type))
98 return value;
100 c_incomplete_type_error (value, type);
101 return error_mark_node;
104 /* Print an error message for invalid use of an incomplete type.
105 VALUE is the expression that was used (or 0 if that isn't known)
106 and TYPE is the type that was invalid. */
108 void
109 c_incomplete_type_error (tree value, tree type)
111 const char *type_code_string;
113 /* Avoid duplicate error message. */
114 if (TREE_CODE (type) == ERROR_MARK)
115 return;
117 if (value != 0 && (TREE_CODE (value) == VAR_DECL
118 || TREE_CODE (value) == PARM_DECL))
119 error ("`%s' has an incomplete type",
120 IDENTIFIER_POINTER (DECL_NAME (value)));
121 else
123 retry:
124 /* We must print an error message. Be clever about what it says. */
126 switch (TREE_CODE (type))
128 case RECORD_TYPE:
129 type_code_string = "struct";
130 break;
132 case UNION_TYPE:
133 type_code_string = "union";
134 break;
136 case ENUMERAL_TYPE:
137 type_code_string = "enum";
138 break;
140 case VOID_TYPE:
141 error ("invalid use of void expression");
142 return;
144 case ARRAY_TYPE:
145 if (TYPE_DOMAIN (type))
147 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
149 error ("invalid use of flexible array member");
150 return;
152 type = TREE_TYPE (type);
153 goto retry;
155 error ("invalid use of array with unspecified bounds");
156 return;
158 default:
159 abort ();
162 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
163 error ("invalid use of undefined type `%s %s'",
164 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 else
166 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
167 error ("invalid use of incomplete typedef `%s'",
168 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
172 /* Given a type, apply default promotions wrt unnamed function
173 arguments and return the new type. */
175 tree
176 c_type_promotes_to (tree type)
178 if (TYPE_MAIN_VARIANT (type) == float_type_node)
179 return double_type_node;
181 if (c_promoting_integer_type_p (type))
183 /* Preserve unsignedness if not really getting any wider. */
184 if (TREE_UNSIGNED (type)
185 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
186 return unsigned_type_node;
187 return integer_type_node;
190 return type;
193 /* Return a variant of TYPE which has all the type qualifiers of LIKE
194 as well as those of TYPE. */
196 static tree
197 qualify_type (tree type, tree like)
199 return c_build_qualified_type (type,
200 TYPE_QUALS (type) | TYPE_QUALS (like));
203 /* Return the common type of two types.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash. In particular, we assume that qualifiers
206 match.
208 This is the type for the result of most arithmetic operations
209 if the operands have the given two types. */
211 tree
212 common_type (tree t1, tree t2)
214 enum tree_code code1;
215 enum tree_code code2;
216 tree attributes;
218 /* Save time if the two types are the same. */
220 if (t1 == t2) return t1;
222 /* If one type is nonsense, use the other. */
223 if (t1 == error_mark_node)
224 return t2;
225 if (t2 == error_mark_node)
226 return t1;
228 /* Merge the attributes. */
229 attributes = (*targetm.merge_type_attributes) (t1, t2);
231 /* Treat an enum type as the unsigned integer type of the same width. */
233 if (TREE_CODE (t1) == ENUMERAL_TYPE)
234 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
235 if (TREE_CODE (t2) == ENUMERAL_TYPE)
236 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
238 code1 = TREE_CODE (t1);
239 code2 = TREE_CODE (t2);
241 /* If one type is complex, form the common type of the non-complex
242 components, then make that complex. Use T1 or T2 if it is the
243 required type. */
244 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
246 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
247 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
248 tree subtype = common_type (subtype1, subtype2);
250 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
251 return build_type_attribute_variant (t1, attributes);
252 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
253 return build_type_attribute_variant (t2, attributes);
254 else
255 return build_type_attribute_variant (build_complex_type (subtype),
256 attributes);
259 switch (code1)
261 case INTEGER_TYPE:
262 case REAL_TYPE:
263 /* If only one is real, use it as the result. */
265 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
266 return build_type_attribute_variant (t1, attributes);
268 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
269 return build_type_attribute_variant (t2, attributes);
271 /* Both real or both integers; use the one with greater precision. */
273 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
274 return build_type_attribute_variant (t1, attributes);
275 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
276 return build_type_attribute_variant (t2, attributes);
278 /* Same precision. Prefer longs to ints even when same size. */
280 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
281 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
282 return build_type_attribute_variant (long_unsigned_type_node,
283 attributes);
285 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
286 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
288 /* But preserve unsignedness from the other type,
289 since long cannot hold all the values of an unsigned int. */
290 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
291 t1 = long_unsigned_type_node;
292 else
293 t1 = long_integer_type_node;
294 return build_type_attribute_variant (t1, attributes);
297 /* Likewise, prefer long double to double even if same size. */
298 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
299 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
300 return build_type_attribute_variant (long_double_type_node,
301 attributes);
303 /* Otherwise prefer the unsigned one. */
305 if (TREE_UNSIGNED (t1))
306 return build_type_attribute_variant (t1, attributes);
307 else
308 return build_type_attribute_variant (t2, attributes);
310 case POINTER_TYPE:
311 /* For two pointers, do this recursively on the target type,
312 and combine the qualifiers of the two types' targets. */
313 /* This code was turned off; I don't know why.
314 But ANSI C specifies doing this with the qualifiers.
315 So I turned it on again. */
317 tree pointed_to_1 = TREE_TYPE (t1);
318 tree pointed_to_2 = TREE_TYPE (t2);
319 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
320 TYPE_MAIN_VARIANT (pointed_to_2));
321 t1 = build_pointer_type (c_build_qualified_type
322 (target,
323 TYPE_QUALS (pointed_to_1) |
324 TYPE_QUALS (pointed_to_2)));
325 return build_type_attribute_variant (t1, attributes);
328 case ARRAY_TYPE:
330 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
331 /* Save space: see if the result is identical to one of the args. */
332 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
333 return build_type_attribute_variant (t1, attributes);
334 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
335 return build_type_attribute_variant (t2, attributes);
336 /* Merge the element types, and have a size if either arg has one. */
337 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
338 return build_type_attribute_variant (t1, attributes);
341 case FUNCTION_TYPE:
342 /* Function types: prefer the one that specified arg types.
343 If both do, merge the arg types. Also merge the return types. */
345 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
346 tree p1 = TYPE_ARG_TYPES (t1);
347 tree p2 = TYPE_ARG_TYPES (t2);
348 int len;
349 tree newargs, n;
350 int i;
352 /* Save space: see if the result is identical to one of the args. */
353 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
354 return build_type_attribute_variant (t1, attributes);
355 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
356 return build_type_attribute_variant (t2, attributes);
358 /* Simple way if one arg fails to specify argument types. */
359 if (TYPE_ARG_TYPES (t1) == 0)
361 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
362 return build_type_attribute_variant (t1, attributes);
364 if (TYPE_ARG_TYPES (t2) == 0)
366 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
367 return build_type_attribute_variant (t1, attributes);
370 /* If both args specify argument types, we must merge the two
371 lists, argument by argument. */
373 pushlevel (0);
374 declare_parm_level ();
376 len = list_length (p1);
377 newargs = 0;
379 for (i = 0; i < len; i++)
380 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
382 n = newargs;
384 for (; p1;
385 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
387 /* A null type means arg type is not specified.
388 Take whatever the other function type has. */
389 if (TREE_VALUE (p1) == 0)
391 TREE_VALUE (n) = TREE_VALUE (p2);
392 goto parm_done;
394 if (TREE_VALUE (p2) == 0)
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 goto parm_done;
400 /* Given wait (union {union wait *u; int *i} *)
401 and wait (union wait *),
402 prefer union wait * as type of parm. */
403 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
404 && TREE_VALUE (p1) != TREE_VALUE (p2))
406 tree memb;
407 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
408 memb; memb = TREE_CHAIN (memb))
409 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
410 COMPARE_STRICT))
412 TREE_VALUE (n) = TREE_VALUE (p2);
413 if (pedantic)
414 pedwarn ("function types not truly compatible in ISO C");
415 goto parm_done;
418 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
419 && TREE_VALUE (p2) != TREE_VALUE (p1))
421 tree memb;
422 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
423 memb; memb = TREE_CHAIN (memb))
424 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
425 COMPARE_STRICT))
427 TREE_VALUE (n) = TREE_VALUE (p1);
428 if (pedantic)
429 pedwarn ("function types not truly compatible in ISO C");
430 goto parm_done;
433 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
434 parm_done: ;
437 poplevel (0, 0, 0);
439 t1 = build_function_type (valtype, newargs);
440 /* ... falls through ... */
443 default:
444 return build_type_attribute_variant (t1, attributes);
449 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
450 or various other operations. Return 2 if they are compatible
451 but a warning may be needed if you use them together. */
454 comptypes (tree type1, tree type2, int flags)
456 tree t1 = type1;
457 tree t2 = type2;
458 int attrval, val;
460 /* Suppress errors caused by previously reported errors. */
462 if (t1 == t2 || !t1 || !t2
463 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
464 return 1;
466 /* If either type is the internal version of sizetype, return the
467 language version. */
468 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
469 && TYPE_DOMAIN (t1) != 0)
470 t1 = TYPE_DOMAIN (t1);
472 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
473 && TYPE_DOMAIN (t2) != 0)
474 t2 = TYPE_DOMAIN (t2);
476 /* Treat an enum type as the integer type of the same width and
477 signedness. */
479 if (TREE_CODE (t1) == ENUMERAL_TYPE)
480 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
481 if (TREE_CODE (t2) == ENUMERAL_TYPE)
482 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
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 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
514 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
515 break;
517 case FUNCTION_TYPE:
518 val = function_types_compatible_p (t1, t2, flags);
519 break;
521 case ARRAY_TYPE:
523 tree d1 = TYPE_DOMAIN (t1);
524 tree d2 = TYPE_DOMAIN (t2);
525 bool d1_variable, d2_variable;
526 bool d1_zero, d2_zero;
527 val = 1;
529 /* Target types must match incl. qualifiers. */
530 if (TREE_TYPE (t1) != TREE_TYPE (t2)
531 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
532 flags)))
533 return 0;
535 /* Sizes must match unless one is missing or variable. */
536 if (d1 == 0 || d2 == 0 || d1 == d2)
537 break;
539 d1_zero = ! TYPE_MAX_VALUE (d1);
540 d2_zero = ! TYPE_MAX_VALUE (d2);
542 d1_variable = (! d1_zero
543 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
545 d2_variable = (! d2_zero
546 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
547 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
549 if (d1_variable || d2_variable)
550 break;
551 if (d1_zero && d2_zero)
552 break;
553 if (d1_zero || d2_zero
554 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
555 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
556 val = 0;
558 break;
561 case RECORD_TYPE:
562 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
563 val = 1;
565 case ENUMERAL_TYPE:
566 case UNION_TYPE:
567 if (val != 1 && (flags & COMPARE_DIFFERENT_TU))
568 val = tagged_types_tu_compatible_p (t1, t2, flags);
569 break;
571 case VECTOR_TYPE:
572 /* The target might allow certain vector types to be compatible. */
573 val = (*targetm.vector_opaque_p) (t1)
574 || (*targetm.vector_opaque_p) (t2);
575 break;
577 default:
578 break;
580 return attrval == 2 && val == 1 ? 2 : val;
583 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
584 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
585 to 1 or 0 depending if the check of the pointer types is meant to
586 be reflexive or not (typically, assignments are not reflexive,
587 while comparisons are reflexive).
590 static int
591 comp_target_types (tree ttl, tree ttr, int reflexive)
593 int val;
595 /* Give objc_comptypes a crack at letting these types through. */
596 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
597 return val;
599 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
600 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
602 if (val == 2 && pedantic)
603 pedwarn ("types are not quite compatible");
604 return val;
607 /* Subroutines of `comptypes'. */
609 /* The C standard says that two structures in different translation
610 units are compatible with each other only if the types of their
611 fields are compatible (among other things). So, consider two copies
612 of this structure: */
614 struct tagged_tu_seen {
615 const struct tagged_tu_seen * next;
616 tree t1;
617 tree t2;
620 /* Can they be compatible with each other? We choose to break the
621 recursion by allowing those types to be compatible. */
623 static const struct tagged_tu_seen * tagged_tu_seen_base;
625 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
626 compatible. If the two types are not the same (which has been
627 checked earlier), this can only happen when multiple translation
628 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
629 rules. */
631 static int
632 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
634 tree s1, s2;
635 bool needs_warning = false;
637 /* We have to verify that the tags of the types are the same. This
638 is harder than it looks because this may be a typedef, so we have
639 to go look at the original type. It may even be a typedef of a
640 typedef... */
641 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
642 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
644 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
645 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
647 /* C90 didn't have the requirement that the two tags be the same. */
648 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
649 return 0;
651 /* C90 didn't say what happened if one or both of the types were
652 incomplete; we choose to follow C99 rules here, which is that they
653 are compatible. */
654 if (TYPE_SIZE (t1) == NULL
655 || TYPE_SIZE (t2) == NULL)
656 return 1;
659 const struct tagged_tu_seen * tts_i;
660 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
661 if (tts_i->t1 == t1 && tts_i->t2 == t2)
662 return 1;
665 switch (TREE_CODE (t1))
667 case ENUMERAL_TYPE:
669 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
670 return 0;
672 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
674 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
675 if (s2 == NULL
676 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
677 return 0;
679 return 1;
682 case UNION_TYPE:
684 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
685 return 0;
687 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
689 bool ok = false;
690 struct tagged_tu_seen tts;
692 tts.next = tagged_tu_seen_base;
693 tts.t1 = t1;
694 tts.t2 = t2;
695 tagged_tu_seen_base = &tts;
697 if (DECL_NAME (s1) != NULL)
698 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
699 if (DECL_NAME (s1) == DECL_NAME (s2))
701 int result;
702 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
703 if (result == 0)
704 break;
705 if (result == 2)
706 needs_warning = true;
708 if (TREE_CODE (s1) == FIELD_DECL
709 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
710 DECL_FIELD_BIT_OFFSET (s2)) != 1)
711 break;
713 ok = true;
714 break;
716 tagged_tu_seen_base = tts.next;
717 if (! ok)
718 return 0;
720 return needs_warning ? 2 : 1;
723 case RECORD_TYPE:
725 struct tagged_tu_seen tts;
727 tts.next = tagged_tu_seen_base;
728 tts.t1 = t1;
729 tts.t2 = t2;
730 tagged_tu_seen_base = &tts;
732 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
733 s1 && s2;
734 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
736 int result;
737 if (TREE_CODE (s1) != TREE_CODE (s2)
738 || DECL_NAME (s1) != DECL_NAME (s2))
739 break;
740 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
741 if (result == 0)
742 break;
743 if (result == 2)
744 needs_warning = true;
746 if (TREE_CODE (s1) == FIELD_DECL
747 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
748 DECL_FIELD_BIT_OFFSET (s2)) != 1)
749 break;
751 tagged_tu_seen_base = tts.next;
752 if (s1 && s2)
753 return 0;
754 return needs_warning ? 2 : 1;
757 default:
758 abort ();
762 /* Return 1 if two function types F1 and F2 are compatible.
763 If either type specifies no argument types,
764 the other must specify a fixed number of self-promoting arg types.
765 Otherwise, if one type specifies only the number of arguments,
766 the other must specify that number of self-promoting arg types.
767 Otherwise, the argument types must match. */
769 static int
770 function_types_compatible_p (tree f1, tree f2, int flags)
772 tree args1, args2;
773 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
774 int val = 1;
775 int val1;
776 tree ret1, ret2;
778 ret1 = TREE_TYPE (f1);
779 ret2 = TREE_TYPE (f2);
781 /* 'volatile' qualifiers on a function's return type mean the function
782 is noreturn. */
783 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
784 pedwarn ("function return types not compatible due to `volatile'");
785 if (TYPE_VOLATILE (ret1))
786 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
787 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
788 if (TYPE_VOLATILE (ret2))
789 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
790 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
791 val = comptypes (ret1, ret2, flags);
792 if (val == 0)
793 return 0;
795 args1 = TYPE_ARG_TYPES (f1);
796 args2 = TYPE_ARG_TYPES (f2);
798 /* An unspecified parmlist matches any specified parmlist
799 whose argument types don't need default promotions. */
801 if (args1 == 0)
803 if (!self_promoting_args_p (args2))
804 return 0;
805 /* If one of these types comes from a non-prototype fn definition,
806 compare that with the other type's arglist.
807 If they don't match, ask for a warning (but no error). */
808 if (TYPE_ACTUAL_ARG_TYPES (f1)
809 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
810 flags))
811 val = 2;
812 return val;
814 if (args2 == 0)
816 if (!self_promoting_args_p (args1))
817 return 0;
818 if (TYPE_ACTUAL_ARG_TYPES (f2)
819 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
820 flags))
821 val = 2;
822 return val;
825 /* Both types have argument lists: compare them and propagate results. */
826 val1 = type_lists_compatible_p (args1, args2, flags);
827 return val1 != 1 ? val1 : val;
830 /* Check two lists of types for compatibility,
831 returning 0 for incompatible, 1 for compatible,
832 or 2 for compatible with warning. */
834 static int
835 type_lists_compatible_p (tree args1, tree args2, int flags)
837 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
838 int val = 1;
839 int newval = 0;
841 while (1)
843 if (args1 == 0 && args2 == 0)
844 return val;
845 /* If one list is shorter than the other,
846 they fail to match. */
847 if (args1 == 0 || args2 == 0)
848 return 0;
849 /* A null pointer instead of a type
850 means there is supposed to be an argument
851 but nothing is specified about what type it has.
852 So match anything that self-promotes. */
853 if (TREE_VALUE (args1) == 0)
855 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
856 return 0;
858 else if (TREE_VALUE (args2) == 0)
860 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
861 return 0;
863 /* If one of the lists has an error marker, ignore this arg. */
864 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
865 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
867 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
868 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
869 flags)))
871 /* Allow wait (union {union wait *u; int *i} *)
872 and wait (union wait *) to be compatible. */
873 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
874 && (TYPE_NAME (TREE_VALUE (args1)) == 0
875 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
876 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
877 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
878 TYPE_SIZE (TREE_VALUE (args2))))
880 tree memb;
881 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
882 memb; memb = TREE_CHAIN (memb))
883 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
884 flags))
885 break;
886 if (memb == 0)
887 return 0;
889 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
890 && (TYPE_NAME (TREE_VALUE (args2)) == 0
891 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
892 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
893 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
894 TYPE_SIZE (TREE_VALUE (args1))))
896 tree memb;
897 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
898 memb; memb = TREE_CHAIN (memb))
899 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
900 flags))
901 break;
902 if (memb == 0)
903 return 0;
905 else
906 return 0;
909 /* comptypes said ok, but record if it said to warn. */
910 if (newval > val)
911 val = newval;
913 args1 = TREE_CHAIN (args1);
914 args2 = TREE_CHAIN (args2);
918 /* Compute the size to increment a pointer by. */
920 tree
921 c_size_in_bytes (tree type)
923 enum tree_code code = TREE_CODE (type);
925 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
926 return size_one_node;
928 if (!COMPLETE_OR_VOID_TYPE_P (type))
930 error ("arithmetic on pointer to an incomplete type");
931 return size_one_node;
934 /* Convert in case a char is more than one unit. */
935 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
936 size_int (TYPE_PRECISION (char_type_node)
937 / BITS_PER_UNIT));
940 /* Return either DECL or its known constant value (if it has one). */
942 tree
943 decl_constant_value (tree decl)
945 if (/* Don't change a variable array bound or initial value to a constant
946 in a place where a variable is invalid. */
947 current_function_decl != 0
948 && ! TREE_THIS_VOLATILE (decl)
949 && TREE_READONLY (decl)
950 && DECL_INITIAL (decl) != 0
951 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
952 /* This is invalid if initial value is not constant.
953 If it has either a function call, a memory reference,
954 or a variable, then re-evaluating it could give different results. */
955 && TREE_CONSTANT (DECL_INITIAL (decl))
956 /* Check for cases where this is sub-optimal, even though valid. */
957 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
958 return DECL_INITIAL (decl);
959 return decl;
962 /* Return either DECL or its known constant value (if it has one), but
963 return DECL if pedantic or DECL has mode BLKmode. This is for
964 bug-compatibility with the old behavior of decl_constant_value
965 (before GCC 3.0); every use of this function is a bug and it should
966 be removed before GCC 3.1. It is not appropriate to use pedantic
967 in a way that affects optimization, and BLKmode is probably not the
968 right test for avoiding misoptimizations either. */
970 static tree
971 decl_constant_value_for_broken_optimization (tree decl)
973 if (pedantic || DECL_MODE (decl) == BLKmode)
974 return decl;
975 else
976 return decl_constant_value (decl);
980 /* Perform the default conversion of arrays and functions to pointers.
981 Return the result of converting EXP. For any other expression, just
982 return EXP. */
984 static tree
985 default_function_array_conversion (tree exp)
987 tree orig_exp;
988 tree type = TREE_TYPE (exp);
989 enum tree_code code = TREE_CODE (type);
990 int not_lvalue = 0;
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
993 an lvalue.
995 Do not use STRIP_NOPS here! It will remove conversions from pointer
996 to integer and cause infinite recursion. */
997 orig_exp = exp;
998 while (TREE_CODE (exp) == NON_LVALUE_EXPR
999 || (TREE_CODE (exp) == NOP_EXPR
1000 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1002 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1003 not_lvalue = 1;
1004 exp = TREE_OPERAND (exp, 0);
1007 /* Preserve the original expression code. */
1008 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1009 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1011 if (code == FUNCTION_TYPE)
1013 return build_unary_op (ADDR_EXPR, exp, 0);
1015 if (code == ARRAY_TYPE)
1017 tree adr;
1018 tree restype = TREE_TYPE (type);
1019 tree ptrtype;
1020 int constp = 0;
1021 int volatilep = 0;
1022 int lvalue_array_p;
1024 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1026 constp = TREE_READONLY (exp);
1027 volatilep = TREE_THIS_VOLATILE (exp);
1030 if (TYPE_QUALS (type) || constp || volatilep)
1031 restype
1032 = c_build_qualified_type (restype,
1033 TYPE_QUALS (type)
1034 | (constp * TYPE_QUAL_CONST)
1035 | (volatilep * TYPE_QUAL_VOLATILE));
1037 if (TREE_CODE (exp) == INDIRECT_REF)
1038 return convert (TYPE_POINTER_TO (restype),
1039 TREE_OPERAND (exp, 0));
1041 if (TREE_CODE (exp) == COMPOUND_EXPR)
1043 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1044 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1045 TREE_OPERAND (exp, 0), op1);
1048 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1049 if (!flag_isoc99 && !lvalue_array_p)
1051 /* Before C99, non-lvalue arrays do not decay to pointers.
1052 Normally, using such an array would be invalid; but it can
1053 be used correctly inside sizeof or as a statement expression.
1054 Thus, do not give an error here; an error will result later. */
1055 return exp;
1058 ptrtype = build_pointer_type (restype);
1060 if (TREE_CODE (exp) == VAR_DECL)
1062 /* ??? This is not really quite correct
1063 in that the type of the operand of ADDR_EXPR
1064 is not the target type of the type of the ADDR_EXPR itself.
1065 Question is, can this lossage be avoided? */
1066 adr = build1 (ADDR_EXPR, ptrtype, exp);
1067 if (!c_mark_addressable (exp))
1068 return error_mark_node;
1069 TREE_CONSTANT (adr) = staticp (exp);
1070 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1071 return adr;
1073 /* This way is better for a COMPONENT_REF since it can
1074 simplify the offset for a component. */
1075 adr = build_unary_op (ADDR_EXPR, exp, 1);
1076 return convert (ptrtype, adr);
1078 return exp;
1081 /* Perform default promotions for C data used in expressions.
1082 Arrays and functions are converted to pointers;
1083 enumeral types or short or char, to int.
1084 In addition, manifest constants symbols are replaced by their values. */
1086 tree
1087 default_conversion (tree exp)
1089 tree orig_exp;
1090 tree type = TREE_TYPE (exp);
1091 enum tree_code code = TREE_CODE (type);
1093 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1094 return default_function_array_conversion (exp);
1096 /* Constants can be used directly unless they're not loadable. */
1097 if (TREE_CODE (exp) == CONST_DECL)
1098 exp = DECL_INITIAL (exp);
1100 /* Replace a nonvolatile const static variable with its value unless
1101 it is an array, in which case we must be sure that taking the
1102 address of the array produces consistent results. */
1103 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1105 exp = decl_constant_value_for_broken_optimization (exp);
1106 type = TREE_TYPE (exp);
1109 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1110 an lvalue.
1112 Do not use STRIP_NOPS here! It will remove conversions from pointer
1113 to integer and cause infinite recursion. */
1114 orig_exp = exp;
1115 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1116 || (TREE_CODE (exp) == NOP_EXPR
1117 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1118 exp = TREE_OPERAND (exp, 0);
1120 /* Preserve the original expression code. */
1121 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1122 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1124 /* Normally convert enums to int,
1125 but convert wide enums to something wider. */
1126 if (code == ENUMERAL_TYPE)
1128 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1129 TYPE_PRECISION (integer_type_node)),
1130 ((TYPE_PRECISION (type)
1131 >= TYPE_PRECISION (integer_type_node))
1132 && TREE_UNSIGNED (type)));
1134 return convert (type, exp);
1137 if (TREE_CODE (exp) == COMPONENT_REF
1138 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1139 /* If it's thinner than an int, promote it like a
1140 c_promoting_integer_type_p, otherwise leave it alone. */
1141 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1142 TYPE_PRECISION (integer_type_node)))
1143 return convert (integer_type_node, exp);
1145 if (c_promoting_integer_type_p (type))
1147 /* Preserve unsignedness if not really getting any wider. */
1148 if (TREE_UNSIGNED (type)
1149 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1150 return convert (unsigned_type_node, exp);
1152 return convert (integer_type_node, exp);
1155 if (code == VOID_TYPE)
1157 error ("void value not ignored as it ought to be");
1158 return error_mark_node;
1160 return exp;
1163 /* Look up COMPONENT in a structure or union DECL.
1165 If the component name is not found, returns NULL_TREE. Otherwise,
1166 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1167 stepping down the chain to the component, which is in the last
1168 TREE_VALUE of the list. Normally the list is of length one, but if
1169 the component is embedded within (nested) anonymous structures or
1170 unions, the list steps down the chain to the component. */
1172 static tree
1173 lookup_field (tree decl, tree component)
1175 tree type = TREE_TYPE (decl);
1176 tree field;
1178 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1179 to the field elements. Use a binary search on this array to quickly
1180 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1181 will always be set for structures which have many elements. */
1183 if (TYPE_LANG_SPECIFIC (type))
1185 int bot, top, half;
1186 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1188 field = TYPE_FIELDS (type);
1189 bot = 0;
1190 top = TYPE_LANG_SPECIFIC (type)->s->len;
1191 while (top - bot > 1)
1193 half = (top - bot + 1) >> 1;
1194 field = field_array[bot+half];
1196 if (DECL_NAME (field) == NULL_TREE)
1198 /* Step through all anon unions in linear fashion. */
1199 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1201 field = field_array[bot++];
1202 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1203 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1205 tree anon = lookup_field (field, component);
1207 if (anon)
1208 return tree_cons (NULL_TREE, field, anon);
1212 /* Entire record is only anon unions. */
1213 if (bot > top)
1214 return NULL_TREE;
1216 /* Restart the binary search, with new lower bound. */
1217 continue;
1220 if (DECL_NAME (field) == component)
1221 break;
1222 if (DECL_NAME (field) < component)
1223 bot += half;
1224 else
1225 top = bot + half;
1228 if (DECL_NAME (field_array[bot]) == component)
1229 field = field_array[bot];
1230 else if (DECL_NAME (field) != component)
1231 return NULL_TREE;
1233 else
1235 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1237 if (DECL_NAME (field) == NULL_TREE
1238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1241 tree anon = lookup_field (field, component);
1243 if (anon)
1244 return tree_cons (NULL_TREE, field, anon);
1247 if (DECL_NAME (field) == component)
1248 break;
1251 if (field == NULL_TREE)
1252 return NULL_TREE;
1255 return tree_cons (NULL_TREE, field, NULL_TREE);
1258 /* Make an expression to refer to the COMPONENT field of
1259 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1261 tree
1262 build_component_ref (tree datum, tree component)
1264 tree type = TREE_TYPE (datum);
1265 enum tree_code code = TREE_CODE (type);
1266 tree field = NULL;
1267 tree ref;
1269 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1270 If pedantic ensure that the arguments are not lvalues; otherwise,
1271 if the component is an array, it would wrongly decay to a pointer in
1272 C89 mode.
1273 We cannot do this with a COND_EXPR, because in a conditional expression
1274 the default promotions are applied to both sides, and this would yield
1275 the wrong type of the result; for example, if the components have
1276 type "char". */
1277 switch (TREE_CODE (datum))
1279 case COMPOUND_EXPR:
1281 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1282 return build (COMPOUND_EXPR, TREE_TYPE (value),
1283 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1285 default:
1286 break;
1289 /* See if there is a field or component with name COMPONENT. */
1291 if (code == RECORD_TYPE || code == UNION_TYPE)
1293 if (!COMPLETE_TYPE_P (type))
1295 c_incomplete_type_error (NULL_TREE, type);
1296 return error_mark_node;
1299 field = lookup_field (datum, component);
1301 if (!field)
1303 error ("%s has no member named `%s'",
1304 code == RECORD_TYPE ? "structure" : "union",
1305 IDENTIFIER_POINTER (component));
1306 return error_mark_node;
1309 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1310 This might be better solved in future the way the C++ front
1311 end does it - by giving the anonymous entities each a
1312 separate name and type, and then have build_component_ref
1313 recursively call itself. We can't do that here. */
1316 tree subdatum = TREE_VALUE (field);
1318 if (TREE_TYPE (subdatum) == error_mark_node)
1319 return error_mark_node;
1321 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1322 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1323 TREE_READONLY (ref) = 1;
1324 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1325 TREE_THIS_VOLATILE (ref) = 1;
1327 if (TREE_DEPRECATED (subdatum))
1328 warn_deprecated_use (subdatum);
1330 datum = ref;
1332 field = TREE_CHAIN (field);
1334 while (field);
1336 return ref;
1338 else if (code != ERROR_MARK)
1339 error ("request for member `%s' in something not a structure or union",
1340 IDENTIFIER_POINTER (component));
1342 return error_mark_node;
1345 /* Given an expression PTR for a pointer, return an expression
1346 for the value pointed to.
1347 ERRORSTRING is the name of the operator to appear in error messages. */
1349 tree
1350 build_indirect_ref (tree ptr, const char *errorstring)
1352 tree pointer = default_conversion (ptr);
1353 tree type = TREE_TYPE (pointer);
1355 if (TREE_CODE (type) == POINTER_TYPE)
1357 if (TREE_CODE (pointer) == ADDR_EXPR
1358 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1359 == TREE_TYPE (type)))
1360 return TREE_OPERAND (pointer, 0);
1361 else
1363 tree t = TREE_TYPE (type);
1364 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1366 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1368 error ("dereferencing pointer to incomplete type");
1369 return error_mark_node;
1371 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1372 warning ("dereferencing `void *' pointer");
1374 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1375 so that we get the proper error message if the result is used
1376 to assign to. Also, &* is supposed to be a no-op.
1377 And ANSI C seems to specify that the type of the result
1378 should be the const type. */
1379 /* A de-reference of a pointer to const is not a const. It is valid
1380 to change it via some other pointer. */
1381 TREE_READONLY (ref) = TYPE_READONLY (t);
1382 TREE_SIDE_EFFECTS (ref)
1383 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1384 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1385 return ref;
1388 else if (TREE_CODE (pointer) != ERROR_MARK)
1389 error ("invalid type argument of `%s'", errorstring);
1390 return error_mark_node;
1393 /* This handles expressions of the form "a[i]", which denotes
1394 an array reference.
1396 This is logically equivalent in C to *(a+i), but we may do it differently.
1397 If A is a variable or a member, we generate a primitive ARRAY_REF.
1398 This avoids forcing the array out of registers, and can work on
1399 arrays that are not lvalues (for example, members of structures returned
1400 by functions). */
1402 tree
1403 build_array_ref (tree array, tree index)
1405 if (index == 0)
1407 error ("subscript missing in array reference");
1408 return error_mark_node;
1411 if (TREE_TYPE (array) == error_mark_node
1412 || TREE_TYPE (index) == error_mark_node)
1413 return error_mark_node;
1415 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1416 && TREE_CODE (array) != INDIRECT_REF)
1418 tree rval, type;
1420 /* Subscripting with type char is likely to lose
1421 on a machine where chars are signed.
1422 So warn on any machine, but optionally.
1423 Don't warn for unsigned char since that type is safe.
1424 Don't warn for signed char because anyone who uses that
1425 must have done so deliberately. */
1426 if (warn_char_subscripts
1427 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1428 warning ("array subscript has type `char'");
1430 /* Apply default promotions *after* noticing character types. */
1431 index = default_conversion (index);
1433 /* Require integer *after* promotion, for sake of enums. */
1434 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1436 error ("array subscript is not an integer");
1437 return error_mark_node;
1440 /* An array that is indexed by a non-constant
1441 cannot be stored in a register; we must be able to do
1442 address arithmetic on its address.
1443 Likewise an array of elements of variable size. */
1444 if (TREE_CODE (index) != INTEGER_CST
1445 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1446 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1448 if (!c_mark_addressable (array))
1449 return error_mark_node;
1451 /* An array that is indexed by a constant value which is not within
1452 the array bounds cannot be stored in a register either; because we
1453 would get a crash in store_bit_field/extract_bit_field when trying
1454 to access a non-existent part of the register. */
1455 if (TREE_CODE (index) == INTEGER_CST
1456 && TYPE_VALUES (TREE_TYPE (array))
1457 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1459 if (!c_mark_addressable (array))
1460 return error_mark_node;
1463 if (pedantic)
1465 tree foo = array;
1466 while (TREE_CODE (foo) == COMPONENT_REF)
1467 foo = TREE_OPERAND (foo, 0);
1468 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1469 pedwarn ("ISO C forbids subscripting `register' array");
1470 else if (! flag_isoc99 && ! lvalue_p (foo))
1471 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1474 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1475 rval = build (ARRAY_REF, type, array, index);
1476 /* Array ref is const/volatile if the array elements are
1477 or if the array is. */
1478 TREE_READONLY (rval)
1479 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1480 | TREE_READONLY (array));
1481 TREE_SIDE_EFFECTS (rval)
1482 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1483 | TREE_SIDE_EFFECTS (array));
1484 TREE_THIS_VOLATILE (rval)
1485 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1486 /* This was added by rms on 16 Nov 91.
1487 It fixes vol struct foo *a; a->elts[1]
1488 in an inline function.
1489 Hope it doesn't break something else. */
1490 | TREE_THIS_VOLATILE (array));
1491 return require_complete_type (fold (rval));
1495 tree ar = default_conversion (array);
1496 tree ind = default_conversion (index);
1498 /* Do the same warning check as above, but only on the part that's
1499 syntactically the index and only if it is also semantically
1500 the index. */
1501 if (warn_char_subscripts
1502 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1503 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1504 warning ("subscript has type `char'");
1506 /* Put the integer in IND to simplify error checking. */
1507 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1509 tree temp = ar;
1510 ar = ind;
1511 ind = temp;
1514 if (ar == error_mark_node)
1515 return ar;
1517 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1518 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1520 error ("subscripted value is neither array nor pointer");
1521 return error_mark_node;
1523 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1525 error ("array subscript is not an integer");
1526 return error_mark_node;
1529 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1530 "array indexing");
1534 /* Build an external reference to identifier ID. FUN indicates
1535 whether this will be used for a function call. */
1536 tree
1537 build_external_ref (tree id, int fun)
1539 tree ref;
1540 tree decl = lookup_name (id);
1541 tree objc_ivar = lookup_objc_ivar (id);
1543 if (decl && decl != error_mark_node)
1545 /* Properly declared variable or function reference. */
1546 if (!objc_ivar)
1547 ref = decl;
1548 else if (decl != objc_ivar && !C_DECL_FILE_SCOPE (decl))
1550 warning ("local declaration of `%s' hides instance variable",
1551 IDENTIFIER_POINTER (id));
1552 ref = decl;
1554 else
1555 ref = objc_ivar;
1557 else if (objc_ivar)
1558 ref = objc_ivar;
1559 else if (fun)
1560 /* Implicit function declaration. */
1561 ref = implicitly_declare (id);
1562 else if (decl == error_mark_node)
1563 /* Don't complain about something that's already been
1564 complained about. */
1565 return error_mark_node;
1566 else
1568 undeclared_variable (id);
1569 return error_mark_node;
1572 if (TREE_TYPE (ref) == error_mark_node)
1573 return error_mark_node;
1575 if (TREE_DEPRECATED (ref))
1576 warn_deprecated_use (ref);
1578 if (!skip_evaluation)
1579 assemble_external (ref);
1580 TREE_USED (ref) = 1;
1582 if (TREE_CODE (ref) == CONST_DECL)
1584 ref = DECL_INITIAL (ref);
1585 TREE_CONSTANT (ref) = 1;
1587 else if (current_function_decl != 0
1588 && !C_DECL_FILE_SCOPE (current_function_decl)
1589 && (TREE_CODE (ref) == VAR_DECL
1590 || TREE_CODE (ref) == PARM_DECL
1591 || TREE_CODE (ref) == FUNCTION_DECL))
1593 tree context = decl_function_context (ref);
1595 if (context != 0 && context != current_function_decl)
1596 DECL_NONLOCAL (ref) = 1;
1599 return ref;
1602 /* Build a function call to function FUNCTION with parameters PARAMS.
1603 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1604 TREE_VALUE of each node is a parameter-expression.
1605 FUNCTION's data type may be a function type or a pointer-to-function. */
1607 tree
1608 build_function_call (tree function, tree params)
1610 tree fntype, fundecl = 0;
1611 tree coerced_params;
1612 tree name = NULL_TREE, result;
1614 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1615 STRIP_TYPE_NOPS (function);
1617 /* Convert anything with function type to a pointer-to-function. */
1618 if (TREE_CODE (function) == FUNCTION_DECL)
1620 name = DECL_NAME (function);
1622 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1623 (because calling an inline function does not mean the function
1624 needs to be separately compiled). */
1625 fntype = build_type_variant (TREE_TYPE (function),
1626 TREE_READONLY (function),
1627 TREE_THIS_VOLATILE (function));
1628 fundecl = function;
1629 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1631 else
1632 function = default_conversion (function);
1634 fntype = TREE_TYPE (function);
1636 if (TREE_CODE (fntype) == ERROR_MARK)
1637 return error_mark_node;
1639 if (!(TREE_CODE (fntype) == POINTER_TYPE
1640 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1642 error ("called object is not a function");
1643 return error_mark_node;
1646 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1647 current_function_returns_abnormally = 1;
1649 /* fntype now gets the type of function pointed to. */
1650 fntype = TREE_TYPE (fntype);
1652 /* Convert the parameters to the types declared in the
1653 function prototype, or apply default promotions. */
1655 coerced_params
1656 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1658 /* Check that the arguments to the function are valid. */
1660 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1662 /* Recognize certain built-in functions so we can make tree-codes
1663 other than CALL_EXPR. We do this when it enables fold-const.c
1664 to do something useful. */
1666 if (TREE_CODE (function) == ADDR_EXPR
1667 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1668 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1670 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1671 params, coerced_params);
1672 if (result)
1673 return result;
1676 result = build (CALL_EXPR, TREE_TYPE (fntype),
1677 function, coerced_params, NULL_TREE);
1678 TREE_SIDE_EFFECTS (result) = 1;
1679 result = fold (result);
1681 if (VOID_TYPE_P (TREE_TYPE (result)))
1682 return result;
1683 return require_complete_type (result);
1686 /* Convert the argument expressions in the list VALUES
1687 to the types in the list TYPELIST. The result is a list of converted
1688 argument expressions.
1690 If TYPELIST is exhausted, or when an element has NULL as its type,
1691 perform the default conversions.
1693 PARMLIST is the chain of parm decls for the function being called.
1694 It may be 0, if that info is not available.
1695 It is used only for generating error messages.
1697 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1699 This is also where warnings about wrong number of args are generated.
1701 Both VALUES and the returned value are chains of TREE_LIST nodes
1702 with the elements of the list in the TREE_VALUE slots of those nodes. */
1704 static tree
1705 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1707 tree typetail, valtail;
1708 tree result = NULL;
1709 int parmnum;
1711 /* Scan the given expressions and types, producing individual
1712 converted arguments and pushing them on RESULT in reverse order. */
1714 for (valtail = values, typetail = typelist, parmnum = 0;
1715 valtail;
1716 valtail = TREE_CHAIN (valtail), parmnum++)
1718 tree type = typetail ? TREE_VALUE (typetail) : 0;
1719 tree val = TREE_VALUE (valtail);
1721 if (type == void_type_node)
1723 if (name)
1724 error ("too many arguments to function `%s'",
1725 IDENTIFIER_POINTER (name));
1726 else
1727 error ("too many arguments to function");
1728 break;
1731 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1732 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1733 to convert automatically to a pointer. */
1734 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1735 val = TREE_OPERAND (val, 0);
1737 val = default_function_array_conversion (val);
1739 val = require_complete_type (val);
1741 if (type != 0)
1743 /* Formal parm type is specified by a function prototype. */
1744 tree parmval;
1746 if (!COMPLETE_TYPE_P (type))
1748 error ("type of formal parameter %d is incomplete", parmnum + 1);
1749 parmval = val;
1751 else
1753 /* Optionally warn about conversions that
1754 differ from the default conversions. */
1755 if (warn_conversion || warn_traditional)
1757 int formal_prec = TYPE_PRECISION (type);
1759 if (INTEGRAL_TYPE_P (type)
1760 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1761 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1762 if (INTEGRAL_TYPE_P (type)
1763 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1764 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1765 else if (TREE_CODE (type) == COMPLEX_TYPE
1766 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1767 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1768 else if (TREE_CODE (type) == REAL_TYPE
1769 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1770 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1771 else if (TREE_CODE (type) == COMPLEX_TYPE
1772 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1773 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1774 else if (TREE_CODE (type) == REAL_TYPE
1775 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1776 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1777 /* ??? At some point, messages should be written about
1778 conversions between complex types, but that's too messy
1779 to do now. */
1780 else if (TREE_CODE (type) == REAL_TYPE
1781 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1783 /* Warn if any argument is passed as `float',
1784 since without a prototype it would be `double'. */
1785 if (formal_prec == TYPE_PRECISION (float_type_node))
1786 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1788 /* Detect integer changing in width or signedness.
1789 These warnings are only activated with
1790 -Wconversion, not with -Wtraditional. */
1791 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1792 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1794 tree would_have_been = default_conversion (val);
1795 tree type1 = TREE_TYPE (would_have_been);
1797 if (TREE_CODE (type) == ENUMERAL_TYPE
1798 && (TYPE_MAIN_VARIANT (type)
1799 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1800 /* No warning if function asks for enum
1801 and the actual arg is that enum type. */
1803 else if (formal_prec != TYPE_PRECISION (type1))
1804 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1805 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1807 /* Don't complain if the formal parameter type
1808 is an enum, because we can't tell now whether
1809 the value was an enum--even the same enum. */
1810 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1812 else if (TREE_CODE (val) == INTEGER_CST
1813 && int_fits_type_p (val, type))
1814 /* Change in signedness doesn't matter
1815 if a constant value is unaffected. */
1817 /* Likewise for a constant in a NOP_EXPR. */
1818 else if (TREE_CODE (val) == NOP_EXPR
1819 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1820 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1822 /* If the value is extended from a narrower
1823 unsigned type, it doesn't matter whether we
1824 pass it as signed or unsigned; the value
1825 certainly is the same either way. */
1826 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1827 && TREE_UNSIGNED (TREE_TYPE (val)))
1829 else if (TREE_UNSIGNED (type))
1830 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1831 else
1832 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1836 parmval = convert_for_assignment (type, val,
1837 (char *) 0, /* arg passing */
1838 fundecl, name, parmnum + 1);
1840 if (PROMOTE_PROTOTYPES
1841 && INTEGRAL_TYPE_P (type)
1842 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1843 parmval = default_conversion (parmval);
1845 result = tree_cons (NULL_TREE, parmval, result);
1847 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1848 && (TYPE_PRECISION (TREE_TYPE (val))
1849 < TYPE_PRECISION (double_type_node)))
1850 /* Convert `float' to `double'. */
1851 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1852 else
1853 /* Convert `short' and `char' to full-size `int'. */
1854 result = tree_cons (NULL_TREE, default_conversion (val), result);
1856 if (typetail)
1857 typetail = TREE_CHAIN (typetail);
1860 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1862 if (name)
1863 error ("too few arguments to function `%s'",
1864 IDENTIFIER_POINTER (name));
1865 else
1866 error ("too few arguments to function");
1869 return nreverse (result);
1872 /* This is the entry point used by the parser
1873 for binary operators in the input.
1874 In addition to constructing the expression,
1875 we check for operands that were written with other binary operators
1876 in a way that is likely to confuse the user. */
1878 tree
1879 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1881 tree result = build_binary_op (code, arg1, arg2, 1);
1883 char class;
1884 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1885 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1886 enum tree_code code1 = ERROR_MARK;
1887 enum tree_code code2 = ERROR_MARK;
1889 if (TREE_CODE (result) == ERROR_MARK)
1890 return error_mark_node;
1892 if (IS_EXPR_CODE_CLASS (class1))
1893 code1 = C_EXP_ORIGINAL_CODE (arg1);
1894 if (IS_EXPR_CODE_CLASS (class2))
1895 code2 = C_EXP_ORIGINAL_CODE (arg2);
1897 /* Check for cases such as x+y<<z which users are likely
1898 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1899 is cleared to prevent these warnings. */
1900 if (warn_parentheses)
1902 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1904 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1905 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1906 warning ("suggest parentheses around + or - inside shift");
1909 if (code == TRUTH_ORIF_EXPR)
1911 if (code1 == TRUTH_ANDIF_EXPR
1912 || code2 == TRUTH_ANDIF_EXPR)
1913 warning ("suggest parentheses around && within ||");
1916 if (code == BIT_IOR_EXPR)
1918 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1919 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1920 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1921 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1922 warning ("suggest parentheses around arithmetic in operand of |");
1923 /* Check cases like x|y==z */
1924 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1925 warning ("suggest parentheses around comparison in operand of |");
1928 if (code == BIT_XOR_EXPR)
1930 if (code1 == BIT_AND_EXPR
1931 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1932 || code2 == BIT_AND_EXPR
1933 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1934 warning ("suggest parentheses around arithmetic in operand of ^");
1935 /* Check cases like x^y==z */
1936 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1937 warning ("suggest parentheses around comparison in operand of ^");
1940 if (code == BIT_AND_EXPR)
1942 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1943 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1944 warning ("suggest parentheses around + or - in operand of &");
1945 /* Check cases like x&y==z */
1946 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1947 warning ("suggest parentheses around comparison in operand of &");
1951 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1952 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1953 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1954 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1956 unsigned_conversion_warning (result, arg1);
1957 unsigned_conversion_warning (result, arg2);
1958 overflow_warning (result);
1960 class = TREE_CODE_CLASS (TREE_CODE (result));
1962 /* Record the code that was specified in the source,
1963 for the sake of warnings about confusing nesting. */
1964 if (IS_EXPR_CODE_CLASS (class))
1965 C_SET_EXP_ORIGINAL_CODE (result, code);
1966 else
1968 int flag = TREE_CONSTANT (result);
1969 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1970 so that convert_for_assignment wouldn't strip it.
1971 That way, we got warnings for things like p = (1 - 1).
1972 But it turns out we should not get those warnings. */
1973 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1974 C_SET_EXP_ORIGINAL_CODE (result, code);
1975 TREE_CONSTANT (result) = flag;
1978 return result;
1982 /* Return true if `t' is known to be non-negative. */
1985 c_tree_expr_nonnegative_p (tree t)
1987 if (TREE_CODE (t) == STMT_EXPR)
1989 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
1991 /* Find the last statement in the chain, ignoring the final
1992 * scope statement */
1993 while (TREE_CHAIN (t) != NULL_TREE
1994 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
1995 t = TREE_CHAIN (t);
1996 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
1998 return tree_expr_nonnegative_p (t);
2001 /* Return a tree for the difference of pointers OP0 and OP1.
2002 The resulting tree has type int. */
2004 static tree
2005 pointer_diff (tree op0, tree op1)
2007 tree result, folded;
2008 tree restype = ptrdiff_type_node;
2010 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2011 tree con0, con1, lit0, lit1;
2012 tree orig_op1 = op1;
2014 if (pedantic || warn_pointer_arith)
2016 if (TREE_CODE (target_type) == VOID_TYPE)
2017 pedwarn ("pointer of type `void *' used in subtraction");
2018 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2019 pedwarn ("pointer to a function used in subtraction");
2022 /* If the conversion to ptrdiff_type does anything like widening or
2023 converting a partial to an integral mode, we get a convert_expression
2024 that is in the way to do any simplifications.
2025 (fold-const.c doesn't know that the extra bits won't be needed.
2026 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2027 different mode in place.)
2028 So first try to find a common term here 'by hand'; we want to cover
2029 at least the cases that occur in legal static initializers. */
2030 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2031 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2033 if (TREE_CODE (con0) == PLUS_EXPR)
2035 lit0 = TREE_OPERAND (con0, 1);
2036 con0 = TREE_OPERAND (con0, 0);
2038 else
2039 lit0 = integer_zero_node;
2041 if (TREE_CODE (con1) == PLUS_EXPR)
2043 lit1 = TREE_OPERAND (con1, 1);
2044 con1 = TREE_OPERAND (con1, 0);
2046 else
2047 lit1 = integer_zero_node;
2049 if (operand_equal_p (con0, con1, 0))
2051 op0 = lit0;
2052 op1 = lit1;
2056 /* First do the subtraction as integers;
2057 then drop through to build the divide operator.
2058 Do not do default conversions on the minus operator
2059 in case restype is a short type. */
2061 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2062 convert (restype, op1), 0);
2063 /* This generates an error if op1 is pointer to incomplete type. */
2064 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2065 error ("arithmetic on pointer to an incomplete type");
2067 /* This generates an error if op0 is pointer to incomplete type. */
2068 op1 = c_size_in_bytes (target_type);
2070 /* Divide by the size, in easiest possible way. */
2072 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2074 folded = fold (result);
2075 if (folded == result)
2076 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2077 return folded;
2080 /* Construct and perhaps optimize a tree representation
2081 for a unary operation. CODE, a tree_code, specifies the operation
2082 and XARG is the operand.
2083 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2084 the default promotions (such as from short to int).
2085 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2086 allows non-lvalues; this is only used to handle conversion of non-lvalue
2087 arrays to pointers in C99. */
2089 tree
2090 build_unary_op (enum tree_code code, tree xarg, int flag)
2092 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2093 tree arg = xarg;
2094 tree argtype = 0;
2095 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2096 tree val;
2097 int noconvert = flag;
2099 if (typecode == ERROR_MARK)
2100 return error_mark_node;
2101 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2102 typecode = INTEGER_TYPE;
2104 switch (code)
2106 case CONVERT_EXPR:
2107 /* This is used for unary plus, because a CONVERT_EXPR
2108 is enough to prevent anybody from looking inside for
2109 associativity, but won't generate any code. */
2110 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2111 || typecode == COMPLEX_TYPE))
2113 error ("wrong type argument to unary plus");
2114 return error_mark_node;
2116 else if (!noconvert)
2117 arg = default_conversion (arg);
2118 arg = non_lvalue (arg);
2119 break;
2121 case NEGATE_EXPR:
2122 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2123 || typecode == COMPLEX_TYPE
2124 || typecode == VECTOR_TYPE))
2126 error ("wrong type argument to unary minus");
2127 return error_mark_node;
2129 else if (!noconvert)
2130 arg = default_conversion (arg);
2131 break;
2133 case BIT_NOT_EXPR:
2134 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2136 if (!noconvert)
2137 arg = default_conversion (arg);
2139 else if (typecode == COMPLEX_TYPE)
2141 code = CONJ_EXPR;
2142 if (pedantic)
2143 pedwarn ("ISO C does not support `~' for complex conjugation");
2144 if (!noconvert)
2145 arg = default_conversion (arg);
2147 else
2149 error ("wrong type argument to bit-complement");
2150 return error_mark_node;
2152 break;
2154 case ABS_EXPR:
2155 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2156 || typecode == COMPLEX_TYPE))
2158 error ("wrong type argument to abs");
2159 return error_mark_node;
2161 else if (!noconvert)
2162 arg = default_conversion (arg);
2163 break;
2165 case CONJ_EXPR:
2166 /* Conjugating a real value is a no-op, but allow it anyway. */
2167 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2168 || typecode == COMPLEX_TYPE))
2170 error ("wrong type argument to conjugation");
2171 return error_mark_node;
2173 else if (!noconvert)
2174 arg = default_conversion (arg);
2175 break;
2177 case TRUTH_NOT_EXPR:
2178 if (typecode != INTEGER_TYPE
2179 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2180 && typecode != COMPLEX_TYPE
2181 /* These will convert to a pointer. */
2182 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2184 error ("wrong type argument to unary exclamation mark");
2185 return error_mark_node;
2187 arg = c_common_truthvalue_conversion (arg);
2188 return invert_truthvalue (arg);
2190 case NOP_EXPR:
2191 break;
2193 case REALPART_EXPR:
2194 if (TREE_CODE (arg) == COMPLEX_CST)
2195 return TREE_REALPART (arg);
2196 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2197 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2198 else
2199 return arg;
2201 case IMAGPART_EXPR:
2202 if (TREE_CODE (arg) == COMPLEX_CST)
2203 return TREE_IMAGPART (arg);
2204 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2205 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2206 else
2207 return convert (TREE_TYPE (arg), integer_zero_node);
2209 case PREINCREMENT_EXPR:
2210 case POSTINCREMENT_EXPR:
2211 case PREDECREMENT_EXPR:
2212 case POSTDECREMENT_EXPR:
2213 /* Handle complex lvalues (when permitted)
2214 by reduction to simpler cases. */
2216 val = unary_complex_lvalue (code, arg, 0);
2217 if (val != 0)
2218 return val;
2220 /* Increment or decrement the real part of the value,
2221 and don't change the imaginary part. */
2222 if (typecode == COMPLEX_TYPE)
2224 tree real, imag;
2226 if (pedantic)
2227 pedwarn ("ISO C does not support `++' and `--' on complex types");
2229 arg = stabilize_reference (arg);
2230 real = build_unary_op (REALPART_EXPR, arg, 1);
2231 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2232 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2233 build_unary_op (code, real, 1), imag);
2236 /* Report invalid types. */
2238 if (typecode != POINTER_TYPE
2239 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2241 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2242 error ("wrong type argument to increment");
2243 else
2244 error ("wrong type argument to decrement");
2246 return error_mark_node;
2250 tree inc;
2251 tree result_type = TREE_TYPE (arg);
2253 arg = get_unwidened (arg, 0);
2254 argtype = TREE_TYPE (arg);
2256 /* Compute the increment. */
2258 if (typecode == POINTER_TYPE)
2260 /* If pointer target is an undefined struct,
2261 we just cannot know how to do the arithmetic. */
2262 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2264 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2265 error ("increment of pointer to unknown structure");
2266 else
2267 error ("decrement of pointer to unknown structure");
2269 else if ((pedantic || warn_pointer_arith)
2270 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2271 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2273 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2274 pedwarn ("wrong type argument to increment");
2275 else
2276 pedwarn ("wrong type argument to decrement");
2279 inc = c_size_in_bytes (TREE_TYPE (result_type));
2281 else
2282 inc = integer_one_node;
2284 inc = convert (argtype, inc);
2286 /* Handle incrementing a cast-expression. */
2288 while (1)
2289 switch (TREE_CODE (arg))
2291 case NOP_EXPR:
2292 case CONVERT_EXPR:
2293 case FLOAT_EXPR:
2294 case FIX_TRUNC_EXPR:
2295 case FIX_FLOOR_EXPR:
2296 case FIX_ROUND_EXPR:
2297 case FIX_CEIL_EXPR:
2298 pedantic_lvalue_warning (CONVERT_EXPR);
2299 /* If the real type has the same machine representation
2300 as the type it is cast to, we can make better output
2301 by adding directly to the inside of the cast. */
2302 if ((TREE_CODE (TREE_TYPE (arg))
2303 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2304 && (TYPE_MODE (TREE_TYPE (arg))
2305 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2306 arg = TREE_OPERAND (arg, 0);
2307 else
2309 tree incremented, modify, value;
2310 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2311 value = boolean_increment (code, arg);
2312 else
2314 arg = stabilize_reference (arg);
2315 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2316 value = arg;
2317 else
2318 value = save_expr (arg);
2319 incremented = build (((code == PREINCREMENT_EXPR
2320 || code == POSTINCREMENT_EXPR)
2321 ? PLUS_EXPR : MINUS_EXPR),
2322 argtype, value, inc);
2323 TREE_SIDE_EFFECTS (incremented) = 1;
2324 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2325 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2327 TREE_USED (value) = 1;
2328 return value;
2330 break;
2332 default:
2333 goto give_up;
2335 give_up:
2337 /* Complain about anything else that is not a true lvalue. */
2338 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2339 || code == POSTINCREMENT_EXPR)
2340 ? "invalid lvalue in increment"
2341 : "invalid lvalue in decrement")))
2342 return error_mark_node;
2344 /* Report a read-only lvalue. */
2345 if (TREE_READONLY (arg))
2346 readonly_warning (arg,
2347 ((code == PREINCREMENT_EXPR
2348 || code == POSTINCREMENT_EXPR)
2349 ? "increment" : "decrement"));
2351 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2352 val = boolean_increment (code, arg);
2353 else
2354 val = build (code, TREE_TYPE (arg), arg, inc);
2355 TREE_SIDE_EFFECTS (val) = 1;
2356 val = convert (result_type, val);
2357 if (TREE_CODE (val) != code)
2358 TREE_NO_UNUSED_WARNING (val) = 1;
2359 return val;
2362 case ADDR_EXPR:
2363 /* Note that this operation never does default_conversion. */
2365 /* Let &* cancel out to simplify resulting code. */
2366 if (TREE_CODE (arg) == INDIRECT_REF)
2368 /* Don't let this be an lvalue. */
2369 if (lvalue_p (TREE_OPERAND (arg, 0)))
2370 return non_lvalue (TREE_OPERAND (arg, 0));
2371 return TREE_OPERAND (arg, 0);
2374 /* For &x[y], return x+y */
2375 if (TREE_CODE (arg) == ARRAY_REF)
2377 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2378 return error_mark_node;
2379 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2380 TREE_OPERAND (arg, 1), 1);
2383 /* Handle complex lvalues (when permitted)
2384 by reduction to simpler cases. */
2385 val = unary_complex_lvalue (code, arg, flag);
2386 if (val != 0)
2387 return val;
2389 /* Anything not already handled and not a true memory reference
2390 or a non-lvalue array is an error. */
2391 else if (typecode != FUNCTION_TYPE && !flag
2392 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2393 return error_mark_node;
2395 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2396 argtype = TREE_TYPE (arg);
2398 /* If the lvalue is const or volatile, merge that into the type
2399 to which the address will point. Note that you can't get a
2400 restricted pointer by taking the address of something, so we
2401 only have to deal with `const' and `volatile' here. */
2402 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2403 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2404 argtype = c_build_type_variant (argtype,
2405 TREE_READONLY (arg),
2406 TREE_THIS_VOLATILE (arg));
2408 argtype = build_pointer_type (argtype);
2410 if (!c_mark_addressable (arg))
2411 return error_mark_node;
2414 tree addr;
2416 if (TREE_CODE (arg) == COMPONENT_REF)
2418 tree field = TREE_OPERAND (arg, 1);
2420 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2422 if (DECL_C_BIT_FIELD (field))
2424 error ("attempt to take address of bit-field structure member `%s'",
2425 IDENTIFIER_POINTER (DECL_NAME (field)));
2426 return error_mark_node;
2429 addr = fold (build (PLUS_EXPR, argtype,
2430 convert (argtype, addr),
2431 convert (argtype, byte_position (field))));
2433 else
2434 addr = build1 (code, argtype, arg);
2436 /* Address of a static or external variable or
2437 file-scope function counts as a constant. */
2438 if (staticp (arg)
2439 && ! (TREE_CODE (arg) == FUNCTION_DECL
2440 && !C_DECL_FILE_SCOPE (arg)))
2441 TREE_CONSTANT (addr) = 1;
2442 return addr;
2445 default:
2446 break;
2449 if (argtype == 0)
2450 argtype = TREE_TYPE (arg);
2451 return fold (build1 (code, argtype, arg));
2454 /* Return nonzero if REF is an lvalue valid for this language.
2455 Lvalues can be assigned, unless their type has TYPE_READONLY.
2456 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2459 lvalue_p (tree ref)
2461 enum tree_code code = TREE_CODE (ref);
2463 switch (code)
2465 case REALPART_EXPR:
2466 case IMAGPART_EXPR:
2467 case COMPONENT_REF:
2468 return lvalue_p (TREE_OPERAND (ref, 0));
2470 case COMPOUND_LITERAL_EXPR:
2471 case STRING_CST:
2472 return 1;
2474 case INDIRECT_REF:
2475 case ARRAY_REF:
2476 case VAR_DECL:
2477 case PARM_DECL:
2478 case RESULT_DECL:
2479 case ERROR_MARK:
2480 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2481 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2483 case BIND_EXPR:
2484 case RTL_EXPR:
2485 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2487 default:
2488 return 0;
2492 /* Return nonzero if REF is an lvalue valid for this language;
2493 otherwise, print an error message and return zero. */
2496 lvalue_or_else (tree ref, const char *msgid)
2498 int win = lvalue_p (ref);
2500 if (! win)
2501 error ("%s", msgid);
2503 return win;
2506 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2507 for certain kinds of expressions which are not really lvalues
2508 but which we can accept as lvalues. If FLAG is nonzero, then
2509 non-lvalues are OK since we may be converting a non-lvalue array to
2510 a pointer in C99.
2512 If ARG is not a kind of expression we can handle, return zero. */
2514 static tree
2515 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2517 /* Handle (a, b) used as an "lvalue". */
2518 if (TREE_CODE (arg) == COMPOUND_EXPR)
2520 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2522 /* If this returns a function type, it isn't really being used as
2523 an lvalue, so don't issue a warning about it. */
2524 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2525 pedantic_lvalue_warning (COMPOUND_EXPR);
2527 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2528 TREE_OPERAND (arg, 0), real_result);
2531 /* Handle (a ? b : c) used as an "lvalue". */
2532 if (TREE_CODE (arg) == COND_EXPR)
2534 if (!flag)
2535 pedantic_lvalue_warning (COND_EXPR);
2536 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2537 pedantic_lvalue_warning (COMPOUND_EXPR);
2539 return (build_conditional_expr
2540 (TREE_OPERAND (arg, 0),
2541 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2542 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2545 return 0;
2548 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2549 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2551 static void
2552 pedantic_lvalue_warning (enum tree_code code)
2554 if (pedantic)
2555 switch (code)
2557 case COND_EXPR:
2558 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
2559 break;
2560 case COMPOUND_EXPR:
2561 pedwarn ("ISO C forbids use of compound expressions as lvalues");
2562 break;
2563 default:
2564 pedwarn ("ISO C forbids use of cast expressions as lvalues");
2565 break;
2569 /* Warn about storing in something that is `const'. */
2571 void
2572 readonly_warning (tree arg, const char *msgid)
2574 if (TREE_CODE (arg) == COMPONENT_REF)
2576 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2577 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2578 else
2579 pedwarn ("%s of read-only member `%s'", _(msgid),
2580 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2582 else if (TREE_CODE (arg) == VAR_DECL)
2583 pedwarn ("%s of read-only variable `%s'", _(msgid),
2584 IDENTIFIER_POINTER (DECL_NAME (arg)));
2585 else
2586 pedwarn ("%s of read-only location", _(msgid));
2589 /* Mark EXP saying that we need to be able to take the
2590 address of it; it should not be allocated in a register.
2591 Returns true if successful. */
2593 bool
2594 c_mark_addressable (tree exp)
2596 tree x = exp;
2598 while (1)
2599 switch (TREE_CODE (x))
2601 case COMPONENT_REF:
2602 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2604 error ("cannot take address of bit-field `%s'",
2605 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2606 return false;
2609 /* ... fall through ... */
2611 case ADDR_EXPR:
2612 case ARRAY_REF:
2613 case REALPART_EXPR:
2614 case IMAGPART_EXPR:
2615 x = TREE_OPERAND (x, 0);
2616 break;
2618 case COMPOUND_LITERAL_EXPR:
2619 case CONSTRUCTOR:
2620 TREE_ADDRESSABLE (x) = 1;
2621 return true;
2623 case VAR_DECL:
2624 case CONST_DECL:
2625 case PARM_DECL:
2626 case RESULT_DECL:
2627 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2628 && DECL_NONLOCAL (x))
2630 if (TREE_PUBLIC (x))
2632 error ("global register variable `%s' used in nested function",
2633 IDENTIFIER_POINTER (DECL_NAME (x)));
2634 return false;
2636 pedwarn ("register variable `%s' used in nested function",
2637 IDENTIFIER_POINTER (DECL_NAME (x)));
2639 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2641 if (TREE_PUBLIC (x))
2643 error ("address of global register variable `%s' requested",
2644 IDENTIFIER_POINTER (DECL_NAME (x)));
2645 return false;
2648 /* If we are making this addressable due to its having
2649 volatile components, give a different error message. Also
2650 handle the case of an unnamed parameter by not trying
2651 to give the name. */
2653 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2655 error ("cannot put object with volatile field into register");
2656 return false;
2659 pedwarn ("address of register variable `%s' requested",
2660 IDENTIFIER_POINTER (DECL_NAME (x)));
2662 put_var_into_stack (x, /*rescan=*/true);
2664 /* drops in */
2665 case FUNCTION_DECL:
2666 TREE_ADDRESSABLE (x) = 1;
2667 /* drops out */
2668 default:
2669 return true;
2673 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2675 tree
2676 build_conditional_expr (tree ifexp, tree op1, tree op2)
2678 tree type1;
2679 tree type2;
2680 enum tree_code code1;
2681 enum tree_code code2;
2682 tree result_type = NULL;
2683 tree orig_op1 = op1, orig_op2 = op2;
2685 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2687 /* Promote both alternatives. */
2689 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2690 op1 = default_conversion (op1);
2691 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2692 op2 = default_conversion (op2);
2694 if (TREE_CODE (ifexp) == ERROR_MARK
2695 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2696 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2697 return error_mark_node;
2699 type1 = TREE_TYPE (op1);
2700 code1 = TREE_CODE (type1);
2701 type2 = TREE_TYPE (op2);
2702 code2 = TREE_CODE (type2);
2704 /* Quickly detect the usual case where op1 and op2 have the same type
2705 after promotion. */
2706 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2708 if (type1 == type2)
2709 result_type = type1;
2710 else
2711 result_type = TYPE_MAIN_VARIANT (type1);
2713 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2714 || code1 == COMPLEX_TYPE)
2715 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2716 || code2 == COMPLEX_TYPE))
2718 result_type = common_type (type1, type2);
2720 /* If -Wsign-compare, warn here if type1 and type2 have
2721 different signedness. We'll promote the signed to unsigned
2722 and later code won't know it used to be different.
2723 Do this check on the original types, so that explicit casts
2724 will be considered, but default promotions won't. */
2725 if (warn_sign_compare && !skip_evaluation)
2727 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2728 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2730 if (unsigned_op1 ^ unsigned_op2)
2732 /* Do not warn if the result type is signed, since the
2733 signed type will only be chosen if it can represent
2734 all the values of the unsigned type. */
2735 if (! TREE_UNSIGNED (result_type))
2736 /* OK */;
2737 /* Do not warn if the signed quantity is an unsuffixed
2738 integer literal (or some static constant expression
2739 involving such literals) and it is non-negative. */
2740 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2741 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2742 /* OK */;
2743 else
2744 warning ("signed and unsigned type in conditional expression");
2748 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2750 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2751 pedwarn ("ISO C forbids conditional expr with only one void side");
2752 result_type = void_type_node;
2754 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2756 if (comp_target_types (type1, type2, 1))
2757 result_type = common_type (type1, type2);
2758 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2759 && TREE_CODE (orig_op1) != NOP_EXPR)
2760 result_type = qualify_type (type2, type1);
2761 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2762 && TREE_CODE (orig_op2) != NOP_EXPR)
2763 result_type = qualify_type (type1, type2);
2764 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2766 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2767 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2768 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2769 TREE_TYPE (type2)));
2771 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2773 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2774 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2775 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2776 TREE_TYPE (type1)));
2778 else
2780 pedwarn ("pointer type mismatch in conditional expression");
2781 result_type = build_pointer_type (void_type_node);
2784 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2786 if (! integer_zerop (op2))
2787 pedwarn ("pointer/integer type mismatch in conditional expression");
2788 else
2790 op2 = null_pointer_node;
2792 result_type = type1;
2794 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2796 if (!integer_zerop (op1))
2797 pedwarn ("pointer/integer type mismatch in conditional expression");
2798 else
2800 op1 = null_pointer_node;
2802 result_type = type2;
2805 if (!result_type)
2807 if (flag_cond_mismatch)
2808 result_type = void_type_node;
2809 else
2811 error ("type mismatch in conditional expression");
2812 return error_mark_node;
2816 /* Merge const and volatile flags of the incoming types. */
2817 result_type
2818 = build_type_variant (result_type,
2819 TREE_READONLY (op1) || TREE_READONLY (op2),
2820 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2822 if (result_type != TREE_TYPE (op1))
2823 op1 = convert_and_check (result_type, op1);
2824 if (result_type != TREE_TYPE (op2))
2825 op2 = convert_and_check (result_type, op2);
2827 if (TREE_CODE (ifexp) == INTEGER_CST)
2828 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2830 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2833 /* Given a list of expressions, return a compound expression
2834 that performs them all and returns the value of the last of them. */
2836 tree
2837 build_compound_expr (tree list)
2839 return internal_build_compound_expr (list, TRUE);
2842 static tree
2843 internal_build_compound_expr (tree list, int first_p)
2845 tree rest;
2847 if (TREE_CHAIN (list) == 0)
2849 /* Convert arrays and functions to pointers when there
2850 really is a comma operator. */
2851 if (!first_p)
2852 TREE_VALUE (list)
2853 = default_function_array_conversion (TREE_VALUE (list));
2855 /* Don't let (0, 0) be null pointer constant. */
2856 if (!first_p && integer_zerop (TREE_VALUE (list)))
2857 return non_lvalue (TREE_VALUE (list));
2858 return TREE_VALUE (list);
2861 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2863 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2865 /* The left-hand operand of a comma expression is like an expression
2866 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2867 any side-effects, unless it was explicitly cast to (void). */
2868 if (warn_unused_value
2869 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2870 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2871 warning ("left-hand operand of comma expression has no effect");
2873 /* When pedantic, a compound expression can be neither an lvalue
2874 nor an integer constant expression. */
2875 if (! pedantic)
2876 return rest;
2879 /* With -Wunused, we should also warn if the left-hand operand does have
2880 side-effects, but computes a value which is not used. For example, in
2881 `foo() + bar(), baz()' the result of the `+' operator is not used,
2882 so we should issue a warning. */
2883 else if (warn_unused_value)
2884 warn_if_unused_value (TREE_VALUE (list));
2886 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2889 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2891 tree
2892 build_c_cast (tree type, tree expr)
2894 tree value = expr;
2896 if (type == error_mark_node || expr == error_mark_node)
2897 return error_mark_node;
2899 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2900 only in <protocol> qualifications. But when constructing cast expressions,
2901 the protocols do matter and must be kept around. */
2902 if (!c_dialect_objc () || !objc_is_id (type))
2903 type = TYPE_MAIN_VARIANT (type);
2905 if (TREE_CODE (type) == ARRAY_TYPE)
2907 error ("cast specifies array type");
2908 return error_mark_node;
2911 if (TREE_CODE (type) == FUNCTION_TYPE)
2913 error ("cast specifies function type");
2914 return error_mark_node;
2917 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2919 if (pedantic)
2921 if (TREE_CODE (type) == RECORD_TYPE
2922 || TREE_CODE (type) == UNION_TYPE)
2923 pedwarn ("ISO C forbids casting nonscalar to the same type");
2926 else if (TREE_CODE (type) == UNION_TYPE)
2928 tree field;
2929 value = default_function_array_conversion (value);
2931 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2932 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2933 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2934 break;
2936 if (field)
2938 tree t;
2940 if (pedantic)
2941 pedwarn ("ISO C forbids casts to union type");
2942 t = digest_init (type,
2943 build_constructor (type,
2944 build_tree_list (field, value)),
2946 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2947 return t;
2949 error ("cast to union type from type not present in union");
2950 return error_mark_node;
2952 else
2954 tree otype, ovalue;
2956 /* If casting to void, avoid the error that would come
2957 from default_conversion in the case of a non-lvalue array. */
2958 if (type == void_type_node)
2959 return build1 (CONVERT_EXPR, type, value);
2961 /* Convert functions and arrays to pointers,
2962 but don't convert any other types. */
2963 value = default_function_array_conversion (value);
2964 otype = TREE_TYPE (value);
2966 /* Optionally warn about potentially worrisome casts. */
2968 if (warn_cast_qual
2969 && TREE_CODE (type) == POINTER_TYPE
2970 && TREE_CODE (otype) == POINTER_TYPE)
2972 tree in_type = type;
2973 tree in_otype = otype;
2974 int added = 0;
2975 int discarded = 0;
2977 /* Check that the qualifiers on IN_TYPE are a superset of
2978 the qualifiers of IN_OTYPE. The outermost level of
2979 POINTER_TYPE nodes is uninteresting and we stop as soon
2980 as we hit a non-POINTER_TYPE node on either type. */
2983 in_otype = TREE_TYPE (in_otype);
2984 in_type = TREE_TYPE (in_type);
2986 /* GNU C allows cv-qualified function types. 'const'
2987 means the function is very pure, 'volatile' means it
2988 can't return. We need to warn when such qualifiers
2989 are added, not when they're taken away. */
2990 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2991 && TREE_CODE (in_type) == FUNCTION_TYPE)
2992 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2993 else
2994 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2996 while (TREE_CODE (in_type) == POINTER_TYPE
2997 && TREE_CODE (in_otype) == POINTER_TYPE);
2999 if (added)
3000 warning ("cast adds new qualifiers to function type");
3002 if (discarded)
3003 /* There are qualifiers present in IN_OTYPE that are not
3004 present in IN_TYPE. */
3005 warning ("cast discards qualifiers from pointer target type");
3008 /* Warn about possible alignment problems. */
3009 if (STRICT_ALIGNMENT && warn_cast_align
3010 && TREE_CODE (type) == POINTER_TYPE
3011 && TREE_CODE (otype) == POINTER_TYPE
3012 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3013 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3014 /* Don't warn about opaque types, where the actual alignment
3015 restriction is unknown. */
3016 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3017 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3018 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3019 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3020 warning ("cast increases required alignment of target type");
3022 if (TREE_CODE (type) == INTEGER_TYPE
3023 && TREE_CODE (otype) == POINTER_TYPE
3024 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3025 && !TREE_CONSTANT (value))
3026 warning ("cast from pointer to integer of different size");
3028 if (warn_bad_function_cast
3029 && TREE_CODE (value) == CALL_EXPR
3030 && TREE_CODE (type) != TREE_CODE (otype))
3031 warning ("cast does not match function type");
3033 if (TREE_CODE (type) == POINTER_TYPE
3034 && TREE_CODE (otype) == INTEGER_TYPE
3035 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3036 /* Don't warn about converting any constant. */
3037 && !TREE_CONSTANT (value))
3038 warning ("cast to pointer from integer of different size");
3040 if (TREE_CODE (type) == POINTER_TYPE
3041 && TREE_CODE (otype) == POINTER_TYPE
3042 && TREE_CODE (expr) == ADDR_EXPR
3043 && DECL_P (TREE_OPERAND (expr, 0))
3044 && flag_strict_aliasing && warn_strict_aliasing
3045 && !VOID_TYPE_P (TREE_TYPE (type)))
3047 /* Casting the address of a decl to non void pointer. Warn
3048 if the cast breaks type based aliasing. */
3049 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3050 warning ("type-punning to incomplete type might break strict-aliasing rules");
3051 else if (!alias_sets_conflict_p
3052 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3053 get_alias_set (TREE_TYPE (type))))
3054 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3057 ovalue = value;
3058 /* Replace a nonvolatile const static variable with its value. */
3059 if (optimize && TREE_CODE (value) == VAR_DECL)
3060 value = decl_constant_value (value);
3061 value = convert (type, value);
3063 /* Ignore any integer overflow caused by the cast. */
3064 if (TREE_CODE (value) == INTEGER_CST)
3066 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3067 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3071 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3072 if (pedantic && 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 /* If pedantic, don't let a cast be an lvalue. */
3078 if (value == expr && pedantic)
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 /* Handle control structure constructs used as "lvalues". */
3131 switch (TREE_CODE (lhs))
3133 /* Handle (a, b) used as an "lvalue". */
3134 case COMPOUND_EXPR:
3135 pedantic_lvalue_warning (COMPOUND_EXPR);
3136 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3137 if (TREE_CODE (newrhs) == ERROR_MARK)
3138 return error_mark_node;
3139 return build (COMPOUND_EXPR, lhstype,
3140 TREE_OPERAND (lhs, 0), newrhs);
3142 /* Handle (a ? b : c) used as an "lvalue". */
3143 case COND_EXPR:
3144 pedantic_lvalue_warning (COND_EXPR);
3145 rhs = save_expr (rhs);
3147 /* Produce (a ? (b = rhs) : (c = rhs))
3148 except that the RHS goes through a save-expr
3149 so the code to compute it is only emitted once. */
3150 tree cond
3151 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3152 build_modify_expr (TREE_OPERAND (lhs, 1),
3153 modifycode, rhs),
3154 build_modify_expr (TREE_OPERAND (lhs, 2),
3155 modifycode, rhs));
3156 if (TREE_CODE (cond) == ERROR_MARK)
3157 return cond;
3158 /* Make sure the code to compute the rhs comes out
3159 before the split. */
3160 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3161 /* But cast it to void to avoid an "unused" error. */
3162 convert (void_type_node, rhs), cond);
3164 default:
3165 break;
3168 /* If a binary op has been requested, combine the old LHS value with the RHS
3169 producing the value we should actually store into the LHS. */
3171 if (modifycode != NOP_EXPR)
3173 lhs = stabilize_reference (lhs);
3174 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3177 /* Handle a cast used as an "lvalue".
3178 We have already performed any binary operator using the value as cast.
3179 Now convert the result to the cast type of the lhs,
3180 and then true type of the lhs and store it there;
3181 then convert result back to the cast type to be the value
3182 of the assignment. */
3184 switch (TREE_CODE (lhs))
3186 case NOP_EXPR:
3187 case CONVERT_EXPR:
3188 case FLOAT_EXPR:
3189 case FIX_TRUNC_EXPR:
3190 case FIX_FLOOR_EXPR:
3191 case FIX_ROUND_EXPR:
3192 case FIX_CEIL_EXPR:
3193 newrhs = default_function_array_conversion (newrhs);
3195 tree inner_lhs = TREE_OPERAND (lhs, 0);
3196 tree result;
3197 result = build_modify_expr (inner_lhs, NOP_EXPR,
3198 convert (TREE_TYPE (inner_lhs),
3199 convert (lhstype, newrhs)));
3200 if (TREE_CODE (result) == ERROR_MARK)
3201 return result;
3202 pedantic_lvalue_warning (CONVERT_EXPR);
3203 return convert (TREE_TYPE (lhs), result);
3206 default:
3207 break;
3210 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3211 Reject anything strange now. */
3213 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3214 return error_mark_node;
3216 /* Warn about storing in something that is `const'. */
3218 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3219 || ((TREE_CODE (lhstype) == RECORD_TYPE
3220 || TREE_CODE (lhstype) == UNION_TYPE)
3221 && C_TYPE_FIELDS_READONLY (lhstype)))
3222 readonly_warning (lhs, "assignment");
3224 /* If storing into a structure or union member,
3225 it has probably been given type `int'.
3226 Compute the type that would go with
3227 the actual amount of storage the member occupies. */
3229 if (TREE_CODE (lhs) == COMPONENT_REF
3230 && (TREE_CODE (lhstype) == INTEGER_TYPE
3231 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3232 || TREE_CODE (lhstype) == REAL_TYPE
3233 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3234 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3236 /* If storing in a field that is in actuality a short or narrower than one,
3237 we must store in the field in its actual type. */
3239 if (lhstype != TREE_TYPE (lhs))
3241 lhs = copy_node (lhs);
3242 TREE_TYPE (lhs) = lhstype;
3245 /* Convert new value to destination type. */
3247 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3248 NULL_TREE, NULL_TREE, 0);
3249 if (TREE_CODE (newrhs) == ERROR_MARK)
3250 return error_mark_node;
3252 /* Scan operands */
3254 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3255 TREE_SIDE_EFFECTS (result) = 1;
3257 /* If we got the LHS in a different type for storing in,
3258 convert the result back to the nominal type of LHS
3259 so that the value we return always has the same type
3260 as the LHS argument. */
3262 if (olhstype == TREE_TYPE (result))
3263 return result;
3264 return convert_for_assignment (olhstype, result, _("assignment"),
3265 NULL_TREE, NULL_TREE, 0);
3268 /* Convert value RHS to type TYPE as preparation for an assignment
3269 to an lvalue of type TYPE.
3270 The real work of conversion is done by `convert'.
3271 The purpose of this function is to generate error messages
3272 for assignments that are not allowed in C.
3273 ERRTYPE is a string to use in error messages:
3274 "assignment", "return", etc. If it is null, this is parameter passing
3275 for a function call (and different error messages are output).
3277 FUNNAME is the name of the function being called,
3278 as an IDENTIFIER_NODE, or null.
3279 PARMNUM is the number of the argument, for printing in error messages. */
3281 static tree
3282 convert_for_assignment (tree type, tree rhs, const char *errtype,
3283 tree fundecl, tree funname, int parmnum)
3285 enum tree_code codel = TREE_CODE (type);
3286 tree rhstype;
3287 enum tree_code coder;
3289 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3290 /* Do not use STRIP_NOPS here. We do not want an enumerator
3291 whose value is 0 to count as a null pointer constant. */
3292 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3293 rhs = TREE_OPERAND (rhs, 0);
3295 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3296 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3297 rhs = default_conversion (rhs);
3298 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3299 rhs = decl_constant_value_for_broken_optimization (rhs);
3301 rhstype = TREE_TYPE (rhs);
3302 coder = TREE_CODE (rhstype);
3304 if (coder == ERROR_MARK)
3305 return error_mark_node;
3307 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3309 overflow_warning (rhs);
3310 /* Check for Objective-C protocols. This will automatically
3311 issue a warning if there are protocol violations. No need to
3312 use the return value. */
3313 if (c_dialect_objc ())
3314 objc_comptypes (type, rhstype, 0);
3315 return rhs;
3318 if (coder == VOID_TYPE)
3320 error ("void value not ignored as it ought to be");
3321 return error_mark_node;
3323 /* A type converts to a reference to it.
3324 This code doesn't fully support references, it's just for the
3325 special case of va_start and va_copy. */
3326 if (codel == REFERENCE_TYPE
3327 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3329 if (!lvalue_p (rhs))
3331 error ("cannot pass rvalue to reference parameter");
3332 return error_mark_node;
3334 if (!c_mark_addressable (rhs))
3335 return error_mark_node;
3336 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3338 /* We already know that these two types are compatible, but they
3339 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3340 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3341 likely to be va_list, a typedef to __builtin_va_list, which
3342 is different enough that it will cause problems later. */
3343 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3344 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3346 rhs = build1 (NOP_EXPR, type, rhs);
3347 return rhs;
3349 /* Some types can interconvert without explicit casts. */
3350 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3351 && ((*targetm.vector_opaque_p) (type)
3352 || (*targetm.vector_opaque_p) (rhstype)))
3353 return convert (type, rhs);
3354 /* Arithmetic types all interconvert, and enum is treated like int. */
3355 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3356 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3357 || codel == BOOLEAN_TYPE)
3358 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3359 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3360 || coder == BOOLEAN_TYPE))
3361 return convert_and_check (type, rhs);
3363 /* Conversion to a transparent union from its member types.
3364 This applies only to function arguments. */
3365 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3367 tree memb_types;
3368 tree marginal_memb_type = 0;
3370 for (memb_types = TYPE_FIELDS (type); memb_types;
3371 memb_types = TREE_CHAIN (memb_types))
3373 tree memb_type = TREE_TYPE (memb_types);
3375 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3376 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3377 break;
3379 if (TREE_CODE (memb_type) != POINTER_TYPE)
3380 continue;
3382 if (coder == POINTER_TYPE)
3384 tree ttl = TREE_TYPE (memb_type);
3385 tree ttr = TREE_TYPE (rhstype);
3387 /* Any non-function converts to a [const][volatile] void *
3388 and vice versa; otherwise, targets must be the same.
3389 Meanwhile, the lhs target must have all the qualifiers of
3390 the rhs. */
3391 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3392 || comp_target_types (memb_type, rhstype, 0))
3394 /* If this type won't generate any warnings, use it. */
3395 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3396 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3397 && TREE_CODE (ttl) == FUNCTION_TYPE)
3398 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3399 == TYPE_QUALS (ttr))
3400 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3401 == TYPE_QUALS (ttl))))
3402 break;
3404 /* Keep looking for a better type, but remember this one. */
3405 if (! marginal_memb_type)
3406 marginal_memb_type = memb_type;
3410 /* Can convert integer zero to any pointer type. */
3411 if (integer_zerop (rhs)
3412 || (TREE_CODE (rhs) == NOP_EXPR
3413 && integer_zerop (TREE_OPERAND (rhs, 0))))
3415 rhs = null_pointer_node;
3416 break;
3420 if (memb_types || marginal_memb_type)
3422 if (! memb_types)
3424 /* We have only a marginally acceptable member type;
3425 it needs a warning. */
3426 tree ttl = TREE_TYPE (marginal_memb_type);
3427 tree ttr = TREE_TYPE (rhstype);
3429 /* Const and volatile mean something different for function
3430 types, so the usual warnings are not appropriate. */
3431 if (TREE_CODE (ttr) == FUNCTION_TYPE
3432 && TREE_CODE (ttl) == FUNCTION_TYPE)
3434 /* Because const and volatile on functions are
3435 restrictions that say the function will not do
3436 certain things, it is okay to use a const or volatile
3437 function where an ordinary one is wanted, but not
3438 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);
3443 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3444 warn_for_assignment ("%s discards qualifiers from pointer target type",
3445 errtype, funname,
3446 parmnum);
3449 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3450 pedwarn ("ISO C prohibits argument conversion to union type");
3452 return build1 (NOP_EXPR, type, rhs);
3456 /* Conversions among pointers */
3457 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3458 && (coder == codel))
3460 tree ttl = TREE_TYPE (type);
3461 tree ttr = TREE_TYPE (rhstype);
3462 bool is_opaque_pointer;
3464 /* Opaque pointers are treated like void pointers. */
3465 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3466 || (*targetm.vector_opaque_p) (rhstype))
3467 && TREE_CODE (ttl) == VECTOR_TYPE
3468 && TREE_CODE (ttr) == VECTOR_TYPE;
3470 /* Any non-function converts to a [const][volatile] void *
3471 and vice versa; otherwise, targets must be the same.
3472 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3473 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3474 || comp_target_types (type, rhstype, 0)
3475 || is_opaque_pointer
3476 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3477 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3479 if (pedantic
3480 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3482 (VOID_TYPE_P (ttr)
3483 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3484 which are not ANSI null ptr constants. */
3485 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3486 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3487 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3488 errtype, funname, parmnum);
3489 /* Const and volatile mean something different for function types,
3490 so the usual warnings are not appropriate. */
3491 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3492 && TREE_CODE (ttl) != FUNCTION_TYPE)
3494 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3495 warn_for_assignment ("%s discards qualifiers from pointer target type",
3496 errtype, funname, parmnum);
3497 /* If this is not a case of ignoring a mismatch in signedness,
3498 no warning. */
3499 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3500 || comp_target_types (type, rhstype, 0))
3502 /* If there is a mismatch, do warn. */
3503 else if (pedantic)
3504 warn_for_assignment ("pointer targets in %s differ in signedness",
3505 errtype, funname, parmnum);
3507 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3508 && TREE_CODE (ttr) == FUNCTION_TYPE)
3510 /* Because const and volatile on functions are restrictions
3511 that say the function will not do certain things,
3512 it is okay to use a const or volatile function
3513 where an ordinary one is wanted, but not vice-versa. */
3514 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3515 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3516 errtype, funname, parmnum);
3519 else
3520 warn_for_assignment ("%s from incompatible pointer type",
3521 errtype, funname, parmnum);
3522 return convert (type, rhs);
3524 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3526 /* An explicit constant 0 can convert to a pointer,
3527 or one that results from arithmetic, even including
3528 a cast to integer type. */
3529 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3531 ! (TREE_CODE (rhs) == NOP_EXPR
3532 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3533 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3534 && integer_zerop (TREE_OPERAND (rhs, 0))))
3536 warn_for_assignment ("%s makes pointer from integer without a cast",
3537 errtype, funname, parmnum);
3538 return convert (type, rhs);
3540 return null_pointer_node;
3542 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3544 warn_for_assignment ("%s makes integer from pointer without a cast",
3545 errtype, funname, parmnum);
3546 return convert (type, rhs);
3548 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3549 return convert (type, rhs);
3551 if (!errtype)
3553 if (funname)
3555 tree selector = objc_message_selector ();
3557 if (selector && parmnum > 2)
3558 error ("incompatible type for argument %d of `%s'",
3559 parmnum - 2, IDENTIFIER_POINTER (selector));
3560 else
3561 error ("incompatible type for argument %d of `%s'",
3562 parmnum, IDENTIFIER_POINTER (funname));
3564 else
3565 error ("incompatible type for argument %d of indirect function call",
3566 parmnum);
3568 else
3569 error ("incompatible types in %s", errtype);
3571 return error_mark_node;
3574 /* Convert VALUE for assignment into inlined parameter PARM. */
3576 tree
3577 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
3579 tree ret, type;
3581 /* If FN was prototyped, the value has been converted already
3582 in convert_arguments. */
3583 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3584 return value;
3586 type = TREE_TYPE (parm);
3587 ret = convert_for_assignment (type, value,
3588 (char *) 0 /* arg passing */, fn,
3589 DECL_NAME (fn), 0);
3590 if (PROMOTE_PROTOTYPES
3591 && INTEGRAL_TYPE_P (type)
3592 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3593 ret = default_conversion (ret);
3594 return ret;
3597 /* Print a warning using MSGID.
3598 It gets OPNAME as its one parameter.
3599 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3600 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3601 FUNCTION and ARGNUM are handled specially if we are building an
3602 Objective-C selector. */
3604 static void
3605 warn_for_assignment (const char *msgid, const char *opname, tree function,
3606 int argnum)
3608 if (opname == 0)
3610 tree selector = objc_message_selector ();
3611 char * new_opname;
3613 if (selector && argnum > 2)
3615 function = selector;
3616 argnum -= 2;
3618 if (argnum == 0)
3620 if (function)
3622 /* Function name is known; supply it. */
3623 const char *const argstring = _("passing arg of `%s'");
3624 new_opname = alloca (IDENTIFIER_LENGTH (function)
3625 + strlen (argstring) + 1 + 1);
3626 sprintf (new_opname, argstring,
3627 IDENTIFIER_POINTER (function));
3629 else
3631 /* Function name unknown (call through ptr). */
3632 const char *const argnofun = _("passing arg of pointer to function");
3633 new_opname = alloca (strlen (argnofun) + 1 + 1);
3634 sprintf (new_opname, argnofun);
3637 else if (function)
3639 /* Function name is known; supply it. */
3640 const char *const argstring = _("passing arg %d of `%s'");
3641 new_opname = alloca (IDENTIFIER_LENGTH (function)
3642 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3643 sprintf (new_opname, argstring, argnum,
3644 IDENTIFIER_POINTER (function));
3646 else
3648 /* Function name unknown (call through ptr); just give arg number. */
3649 const char *const argnofun = _("passing arg %d of pointer to function");
3650 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3651 sprintf (new_opname, argnofun, argnum);
3653 opname = new_opname;
3655 pedwarn (msgid, opname);
3658 /* If VALUE is a compound expr all of whose expressions are constant, then
3659 return its value. Otherwise, return error_mark_node.
3661 This is for handling COMPOUND_EXPRs as initializer elements
3662 which is allowed with a warning when -pedantic is specified. */
3664 static tree
3665 valid_compound_expr_initializer (tree value, tree endtype)
3667 if (TREE_CODE (value) == COMPOUND_EXPR)
3669 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3670 == error_mark_node)
3671 return error_mark_node;
3672 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3673 endtype);
3675 else if (! TREE_CONSTANT (value)
3676 && ! initializer_constant_valid_p (value, endtype))
3677 return error_mark_node;
3678 else
3679 return value;
3682 /* Perform appropriate conversions on the initial value of a variable,
3683 store it in the declaration DECL,
3684 and print any error messages that are appropriate.
3685 If the init is invalid, store an ERROR_MARK. */
3687 void
3688 store_init_value (tree decl, tree init)
3690 tree value, type;
3692 /* If variable's type was invalidly declared, just ignore it. */
3694 type = TREE_TYPE (decl);
3695 if (TREE_CODE (type) == ERROR_MARK)
3696 return;
3698 /* Digest the specified initializer into an expression. */
3700 value = digest_init (type, init, TREE_STATIC (decl));
3702 /* Store the expression if valid; else report error. */
3704 if (warn_traditional && !in_system_header
3705 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3706 warning ("traditional C rejects automatic aggregate initialization");
3708 DECL_INITIAL (decl) = value;
3710 /* ANSI wants warnings about out-of-range constant initializers. */
3711 STRIP_TYPE_NOPS (value);
3712 constant_expression_warning (value);
3714 /* Check if we need to set array size from compound literal size. */
3715 if (TREE_CODE (type) == ARRAY_TYPE
3716 && TYPE_DOMAIN (type) == 0
3717 && value != error_mark_node)
3719 tree inside_init = init;
3721 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3722 inside_init = TREE_OPERAND (init, 0);
3723 inside_init = fold (inside_init);
3725 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3727 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3729 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3731 /* For int foo[] = (int [3]){1}; we need to set array size
3732 now since later on array initializer will be just the
3733 brace enclosed list of the compound literal. */
3734 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3735 layout_type (type);
3736 layout_decl (decl, 0);
3742 /* Methods for storing and printing names for error messages. */
3744 /* Implement a spelling stack that allows components of a name to be pushed
3745 and popped. Each element on the stack is this structure. */
3747 struct spelling
3749 int kind;
3750 union
3752 int i;
3753 const char *s;
3754 } u;
3757 #define SPELLING_STRING 1
3758 #define SPELLING_MEMBER 2
3759 #define SPELLING_BOUNDS 3
3761 static struct spelling *spelling; /* Next stack element (unused). */
3762 static struct spelling *spelling_base; /* Spelling stack base. */
3763 static int spelling_size; /* Size of the spelling stack. */
3765 /* Macros to save and restore the spelling stack around push_... functions.
3766 Alternative to SAVE_SPELLING_STACK. */
3768 #define SPELLING_DEPTH() (spelling - spelling_base)
3769 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3771 /* Push an element on the spelling stack with type KIND and assign VALUE
3772 to MEMBER. */
3774 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3776 int depth = SPELLING_DEPTH (); \
3778 if (depth >= spelling_size) \
3780 spelling_size += 10; \
3781 if (spelling_base == 0) \
3782 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3783 else \
3784 spelling_base = xrealloc (spelling_base, \
3785 spelling_size * sizeof (struct spelling)); \
3786 RESTORE_SPELLING_DEPTH (depth); \
3789 spelling->kind = (KIND); \
3790 spelling->MEMBER = (VALUE); \
3791 spelling++; \
3794 /* Push STRING on the stack. Printed literally. */
3796 static void
3797 push_string (const char *string)
3799 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3802 /* Push a member name on the stack. Printed as '.' STRING. */
3804 static void
3805 push_member_name (tree decl)
3807 const char *const string
3808 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3809 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3812 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3814 static void
3815 push_array_bounds (int bounds)
3817 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3820 /* Compute the maximum size in bytes of the printed spelling. */
3822 static int
3823 spelling_length (void)
3825 int size = 0;
3826 struct spelling *p;
3828 for (p = spelling_base; p < spelling; p++)
3830 if (p->kind == SPELLING_BOUNDS)
3831 size += 25;
3832 else
3833 size += strlen (p->u.s) + 1;
3836 return size;
3839 /* Print the spelling to BUFFER and return it. */
3841 static char *
3842 print_spelling (char *buffer)
3844 char *d = buffer;
3845 struct spelling *p;
3847 for (p = spelling_base; p < spelling; p++)
3848 if (p->kind == SPELLING_BOUNDS)
3850 sprintf (d, "[%d]", p->u.i);
3851 d += strlen (d);
3853 else
3855 const char *s;
3856 if (p->kind == SPELLING_MEMBER)
3857 *d++ = '.';
3858 for (s = p->u.s; (*d = *s++); d++)
3861 *d++ = '\0';
3862 return buffer;
3865 /* Issue an error message for a bad initializer component.
3866 MSGID identifies the message.
3867 The component name is taken from the spelling stack. */
3869 void
3870 error_init (const char *msgid)
3872 char *ofwhat;
3874 error ("%s", _(msgid));
3875 ofwhat = print_spelling (alloca (spelling_length () + 1));
3876 if (*ofwhat)
3877 error ("(near initialization for `%s')", ofwhat);
3880 /* Issue a pedantic warning for a bad initializer component.
3881 MSGID identifies the message.
3882 The component name is taken from the spelling stack. */
3884 void
3885 pedwarn_init (const char *msgid)
3887 char *ofwhat;
3889 pedwarn ("%s", _(msgid));
3890 ofwhat = print_spelling (alloca (spelling_length () + 1));
3891 if (*ofwhat)
3892 pedwarn ("(near initialization for `%s')", ofwhat);
3895 /* Issue a warning for a bad initializer component.
3896 MSGID identifies the message.
3897 The component name is taken from the spelling stack. */
3899 static void
3900 warning_init (const char *msgid)
3902 char *ofwhat;
3904 warning ("%s", _(msgid));
3905 ofwhat = print_spelling (alloca (spelling_length () + 1));
3906 if (*ofwhat)
3907 warning ("(near initialization for `%s')", ofwhat);
3910 /* Digest the parser output INIT as an initializer for type TYPE.
3911 Return a C expression of type TYPE to represent the initial value.
3913 REQUIRE_CONSTANT requests an error if non-constant initializers or
3914 elements are seen. */
3916 static tree
3917 digest_init (tree type, tree init, int require_constant)
3919 enum tree_code code = TREE_CODE (type);
3920 tree inside_init = init;
3922 if (type == error_mark_node
3923 || init == error_mark_node
3924 || TREE_TYPE (init) == error_mark_node)
3925 return error_mark_node;
3927 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3928 /* Do not use STRIP_NOPS here. We do not want an enumerator
3929 whose value is 0 to count as a null pointer constant. */
3930 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3931 inside_init = TREE_OPERAND (init, 0);
3933 inside_init = fold (inside_init);
3935 /* Initialization of an array of chars from a string constant
3936 optionally enclosed in braces. */
3938 if (code == ARRAY_TYPE)
3940 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3941 if ((typ1 == char_type_node
3942 || typ1 == signed_char_type_node
3943 || typ1 == unsigned_char_type_node
3944 || typ1 == unsigned_wchar_type_node
3945 || typ1 == signed_wchar_type_node)
3946 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3948 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3949 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3950 return inside_init;
3952 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3953 != char_type_node)
3954 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3956 error_init ("char-array initialized from wide string");
3957 return error_mark_node;
3959 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3960 == char_type_node)
3961 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3963 error_init ("int-array initialized from non-wide string");
3964 return error_mark_node;
3967 TREE_TYPE (inside_init) = type;
3968 if (TYPE_DOMAIN (type) != 0
3969 && TYPE_SIZE (type) != 0
3970 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3971 /* Subtract 1 (or sizeof (wchar_t))
3972 because it's ok to ignore the terminating null char
3973 that is counted in the length of the constant. */
3974 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3975 TREE_STRING_LENGTH (inside_init)
3976 - ((TYPE_PRECISION (typ1)
3977 != TYPE_PRECISION (char_type_node))
3978 ? (TYPE_PRECISION (wchar_type_node)
3979 / BITS_PER_UNIT)
3980 : 1)))
3981 pedwarn_init ("initializer-string for array of chars is too long");
3983 return inside_init;
3987 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3988 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3989 below and handle as a constructor. */
3990 if (code == VECTOR_TYPE
3991 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3992 && TREE_CONSTANT (inside_init))
3994 if (TREE_CODE (inside_init) == VECTOR_CST
3995 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3996 TYPE_MAIN_VARIANT (type),
3997 COMPARE_STRICT))
3998 return inside_init;
3999 else
4000 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4003 /* Any type can be initialized
4004 from an expression of the same type, optionally with braces. */
4006 if (inside_init && TREE_TYPE (inside_init) != 0
4007 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4008 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4009 || (code == ARRAY_TYPE
4010 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4011 || (code == VECTOR_TYPE
4012 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4013 || (code == POINTER_TYPE
4014 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4015 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4016 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4017 TREE_TYPE (type), COMPARE_STRICT))))
4019 if (code == POINTER_TYPE)
4020 inside_init = default_function_array_conversion (inside_init);
4022 if (require_constant && !flag_isoc99
4023 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4025 /* As an extension, allow initializing objects with static storage
4026 duration with compound literals (which are then treated just as
4027 the brace enclosed list they contain). */
4028 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4029 inside_init = DECL_INITIAL (decl);
4032 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4033 && TREE_CODE (inside_init) != CONSTRUCTOR)
4035 error_init ("array initialized from non-constant array expression");
4036 return error_mark_node;
4039 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4040 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4042 /* Compound expressions can only occur here if -pedantic or
4043 -pedantic-errors is specified. In the later case, we always want
4044 an error. In the former case, we simply want a warning. */
4045 if (require_constant && pedantic
4046 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4048 inside_init
4049 = valid_compound_expr_initializer (inside_init,
4050 TREE_TYPE (inside_init));
4051 if (inside_init == error_mark_node)
4052 error_init ("initializer element is not constant");
4053 else
4054 pedwarn_init ("initializer element is not constant");
4055 if (flag_pedantic_errors)
4056 inside_init = error_mark_node;
4058 else if (require_constant
4059 && (!TREE_CONSTANT (inside_init)
4060 /* This test catches things like `7 / 0' which
4061 result in an expression for which TREE_CONSTANT
4062 is true, but which is not actually something
4063 that is a legal constant. We really should not
4064 be using this function, because it is a part of
4065 the back-end. Instead, the expression should
4066 already have been turned into ERROR_MARK_NODE. */
4067 || !initializer_constant_valid_p (inside_init,
4068 TREE_TYPE (inside_init))))
4070 error_init ("initializer element is not constant");
4071 inside_init = error_mark_node;
4074 return inside_init;
4077 /* Handle scalar types, including conversions. */
4079 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4080 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4082 /* Note that convert_for_assignment calls default_conversion
4083 for arrays and functions. We must not call it in the
4084 case where inside_init is a null pointer constant. */
4085 inside_init
4086 = convert_for_assignment (type, init, _("initialization"),
4087 NULL_TREE, NULL_TREE, 0);
4089 if (require_constant && ! TREE_CONSTANT (inside_init))
4091 error_init ("initializer element is not constant");
4092 inside_init = error_mark_node;
4094 else if (require_constant
4095 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4097 error_init ("initializer element is not computable at load time");
4098 inside_init = error_mark_node;
4101 return inside_init;
4104 /* Come here only for records and arrays. */
4106 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4108 error_init ("variable-sized object may not be initialized");
4109 return error_mark_node;
4112 error_init ("invalid initializer");
4113 return error_mark_node;
4116 /* Handle initializers that use braces. */
4118 /* Type of object we are accumulating a constructor for.
4119 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4120 static tree constructor_type;
4122 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4123 left to fill. */
4124 static tree constructor_fields;
4126 /* For an ARRAY_TYPE, this is the specified index
4127 at which to store the next element we get. */
4128 static tree constructor_index;
4130 /* For an ARRAY_TYPE, this is the maximum index. */
4131 static tree constructor_max_index;
4133 /* For a RECORD_TYPE, this is the first field not yet written out. */
4134 static tree constructor_unfilled_fields;
4136 /* For an ARRAY_TYPE, this is the index of the first element
4137 not yet written out. */
4138 static tree constructor_unfilled_index;
4140 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4141 This is so we can generate gaps between fields, when appropriate. */
4142 static tree constructor_bit_index;
4144 /* If we are saving up the elements rather than allocating them,
4145 this is the list of elements so far (in reverse order,
4146 most recent first). */
4147 static tree constructor_elements;
4149 /* 1 if constructor should be incrementally stored into a constructor chain,
4150 0 if all the elements should be kept in AVL tree. */
4151 static int constructor_incremental;
4153 /* 1 if so far this constructor's elements are all compile-time constants. */
4154 static int constructor_constant;
4156 /* 1 if so far this constructor's elements are all valid address constants. */
4157 static int constructor_simple;
4159 /* 1 if this constructor is erroneous so far. */
4160 static int constructor_erroneous;
4162 /* Structure for managing pending initializer elements, organized as an
4163 AVL tree. */
4165 struct init_node
4167 struct init_node *left, *right;
4168 struct init_node *parent;
4169 int balance;
4170 tree purpose;
4171 tree value;
4174 /* Tree of pending elements at this constructor level.
4175 These are elements encountered out of order
4176 which belong at places we haven't reached yet in actually
4177 writing the output.
4178 Will never hold tree nodes across GC runs. */
4179 static struct init_node *constructor_pending_elts;
4181 /* The SPELLING_DEPTH of this constructor. */
4182 static int constructor_depth;
4184 /* 0 if implicitly pushing constructor levels is allowed. */
4185 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4187 static int require_constant_value;
4188 static int require_constant_elements;
4190 /* DECL node for which an initializer is being read.
4191 0 means we are reading a constructor expression
4192 such as (struct foo) {...}. */
4193 static tree constructor_decl;
4195 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4196 static const char *constructor_asmspec;
4198 /* Nonzero if this is an initializer for a top-level decl. */
4199 static int constructor_top_level;
4201 /* Nonzero if there were any member designators in this initializer. */
4202 static int constructor_designated;
4204 /* Nesting depth of designator list. */
4205 static int designator_depth;
4207 /* Nonzero if there were diagnosed errors in this designator list. */
4208 static int designator_errorneous;
4211 /* This stack has a level for each implicit or explicit level of
4212 structuring in the initializer, including the outermost one. It
4213 saves the values of most of the variables above. */
4215 struct constructor_range_stack;
4217 struct constructor_stack
4219 struct constructor_stack *next;
4220 tree type;
4221 tree fields;
4222 tree index;
4223 tree max_index;
4224 tree unfilled_index;
4225 tree unfilled_fields;
4226 tree bit_index;
4227 tree elements;
4228 struct init_node *pending_elts;
4229 int offset;
4230 int depth;
4231 /* If nonzero, this value should replace the entire
4232 constructor at this level. */
4233 tree replacement_value;
4234 struct constructor_range_stack *range_stack;
4235 char constant;
4236 char simple;
4237 char implicit;
4238 char erroneous;
4239 char outer;
4240 char incremental;
4241 char designated;
4244 struct constructor_stack *constructor_stack;
4246 /* This stack represents designators from some range designator up to
4247 the last designator in the list. */
4249 struct constructor_range_stack
4251 struct constructor_range_stack *next, *prev;
4252 struct constructor_stack *stack;
4253 tree range_start;
4254 tree index;
4255 tree range_end;
4256 tree fields;
4259 struct constructor_range_stack *constructor_range_stack;
4261 /* This stack records separate initializers that are nested.
4262 Nested initializers can't happen in ANSI C, but GNU C allows them
4263 in cases like { ... (struct foo) { ... } ... }. */
4265 struct initializer_stack
4267 struct initializer_stack *next;
4268 tree decl;
4269 const char *asmspec;
4270 struct constructor_stack *constructor_stack;
4271 struct constructor_range_stack *constructor_range_stack;
4272 tree elements;
4273 struct spelling *spelling;
4274 struct spelling *spelling_base;
4275 int spelling_size;
4276 char top_level;
4277 char require_constant_value;
4278 char require_constant_elements;
4281 struct initializer_stack *initializer_stack;
4283 /* Prepare to parse and output the initializer for variable DECL. */
4285 void
4286 start_init (tree decl, tree asmspec_tree, int top_level)
4288 const char *locus;
4289 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4290 const char *asmspec = 0;
4292 if (asmspec_tree)
4293 asmspec = TREE_STRING_POINTER (asmspec_tree);
4295 p->decl = constructor_decl;
4296 p->asmspec = constructor_asmspec;
4297 p->require_constant_value = require_constant_value;
4298 p->require_constant_elements = require_constant_elements;
4299 p->constructor_stack = constructor_stack;
4300 p->constructor_range_stack = constructor_range_stack;
4301 p->elements = constructor_elements;
4302 p->spelling = spelling;
4303 p->spelling_base = spelling_base;
4304 p->spelling_size = spelling_size;
4305 p->top_level = constructor_top_level;
4306 p->next = initializer_stack;
4307 initializer_stack = p;
4309 constructor_decl = decl;
4310 constructor_asmspec = asmspec;
4311 constructor_designated = 0;
4312 constructor_top_level = top_level;
4314 if (decl != 0)
4316 require_constant_value = TREE_STATIC (decl);
4317 require_constant_elements
4318 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4319 /* For a scalar, you can always use any value to initialize,
4320 even within braces. */
4321 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4322 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4323 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4324 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4325 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4327 else
4329 require_constant_value = 0;
4330 require_constant_elements = 0;
4331 locus = "(anonymous)";
4334 constructor_stack = 0;
4335 constructor_range_stack = 0;
4337 missing_braces_mentioned = 0;
4339 spelling_base = 0;
4340 spelling_size = 0;
4341 RESTORE_SPELLING_DEPTH (0);
4343 if (locus)
4344 push_string (locus);
4347 void
4348 finish_init (void)
4350 struct initializer_stack *p = initializer_stack;
4352 /* Free the whole constructor stack of this initializer. */
4353 while (constructor_stack)
4355 struct constructor_stack *q = constructor_stack;
4356 constructor_stack = q->next;
4357 free (q);
4360 if (constructor_range_stack)
4361 abort ();
4363 /* Pop back to the data of the outer initializer (if any). */
4364 constructor_decl = p->decl;
4365 constructor_asmspec = p->asmspec;
4366 require_constant_value = p->require_constant_value;
4367 require_constant_elements = p->require_constant_elements;
4368 constructor_stack = p->constructor_stack;
4369 constructor_range_stack = p->constructor_range_stack;
4370 constructor_elements = p->elements;
4371 spelling = p->spelling;
4372 spelling_base = p->spelling_base;
4373 spelling_size = p->spelling_size;
4374 constructor_top_level = p->top_level;
4375 initializer_stack = p->next;
4376 free (p);
4379 /* Call here when we see the initializer is surrounded by braces.
4380 This is instead of a call to push_init_level;
4381 it is matched by a call to pop_init_level.
4383 TYPE is the type to initialize, for a constructor expression.
4384 For an initializer for a decl, TYPE is zero. */
4386 void
4387 really_start_incremental_init (tree type)
4389 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4391 if (type == 0)
4392 type = TREE_TYPE (constructor_decl);
4394 if ((*targetm.vector_opaque_p) (type))
4395 error ("opaque vector types cannot be initialized");
4397 p->type = constructor_type;
4398 p->fields = constructor_fields;
4399 p->index = constructor_index;
4400 p->max_index = constructor_max_index;
4401 p->unfilled_index = constructor_unfilled_index;
4402 p->unfilled_fields = constructor_unfilled_fields;
4403 p->bit_index = constructor_bit_index;
4404 p->elements = constructor_elements;
4405 p->constant = constructor_constant;
4406 p->simple = constructor_simple;
4407 p->erroneous = constructor_erroneous;
4408 p->pending_elts = constructor_pending_elts;
4409 p->depth = constructor_depth;
4410 p->replacement_value = 0;
4411 p->implicit = 0;
4412 p->range_stack = 0;
4413 p->outer = 0;
4414 p->incremental = constructor_incremental;
4415 p->designated = constructor_designated;
4416 p->next = 0;
4417 constructor_stack = p;
4419 constructor_constant = 1;
4420 constructor_simple = 1;
4421 constructor_depth = SPELLING_DEPTH ();
4422 constructor_elements = 0;
4423 constructor_pending_elts = 0;
4424 constructor_type = type;
4425 constructor_incremental = 1;
4426 constructor_designated = 0;
4427 designator_depth = 0;
4428 designator_errorneous = 0;
4430 if (TREE_CODE (constructor_type) == RECORD_TYPE
4431 || TREE_CODE (constructor_type) == UNION_TYPE)
4433 constructor_fields = TYPE_FIELDS (constructor_type);
4434 /* Skip any nameless bit fields at the beginning. */
4435 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4436 && DECL_NAME (constructor_fields) == 0)
4437 constructor_fields = TREE_CHAIN (constructor_fields);
4439 constructor_unfilled_fields = constructor_fields;
4440 constructor_bit_index = bitsize_zero_node;
4442 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4444 if (TYPE_DOMAIN (constructor_type))
4446 constructor_max_index
4447 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4449 /* Detect non-empty initializations of zero-length arrays. */
4450 if (constructor_max_index == NULL_TREE
4451 && TYPE_SIZE (constructor_type))
4452 constructor_max_index = build_int_2 (-1, -1);
4454 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4455 to initialize VLAs will cause a proper error; avoid tree
4456 checking errors as well by setting a safe value. */
4457 if (constructor_max_index
4458 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4459 constructor_max_index = build_int_2 (-1, -1);
4461 constructor_index
4462 = convert (bitsizetype,
4463 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4465 else
4466 constructor_index = bitsize_zero_node;
4468 constructor_unfilled_index = constructor_index;
4470 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4472 /* Vectors are like simple fixed-size arrays. */
4473 constructor_max_index =
4474 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4475 constructor_index = convert (bitsizetype, bitsize_zero_node);
4476 constructor_unfilled_index = constructor_index;
4478 else
4480 /* Handle the case of int x = {5}; */
4481 constructor_fields = constructor_type;
4482 constructor_unfilled_fields = constructor_type;
4486 /* Push down into a subobject, for initialization.
4487 If this is for an explicit set of braces, IMPLICIT is 0.
4488 If it is because the next element belongs at a lower level,
4489 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4491 void
4492 push_init_level (int implicit)
4494 struct constructor_stack *p;
4495 tree value = NULL_TREE;
4497 /* If we've exhausted any levels that didn't have braces,
4498 pop them now. */
4499 while (constructor_stack->implicit)
4501 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4502 || TREE_CODE (constructor_type) == UNION_TYPE)
4503 && constructor_fields == 0)
4504 process_init_element (pop_init_level (1));
4505 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4506 && constructor_max_index
4507 && tree_int_cst_lt (constructor_max_index, constructor_index))
4508 process_init_element (pop_init_level (1));
4509 else
4510 break;
4513 /* Unless this is an explicit brace, we need to preserve previous
4514 content if any. */
4515 if (implicit)
4517 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4518 || TREE_CODE (constructor_type) == UNION_TYPE)
4519 && constructor_fields)
4520 value = find_init_member (constructor_fields);
4521 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4522 value = find_init_member (constructor_index);
4525 p = xmalloc (sizeof (struct constructor_stack));
4526 p->type = constructor_type;
4527 p->fields = constructor_fields;
4528 p->index = constructor_index;
4529 p->max_index = constructor_max_index;
4530 p->unfilled_index = constructor_unfilled_index;
4531 p->unfilled_fields = constructor_unfilled_fields;
4532 p->bit_index = constructor_bit_index;
4533 p->elements = constructor_elements;
4534 p->constant = constructor_constant;
4535 p->simple = constructor_simple;
4536 p->erroneous = constructor_erroneous;
4537 p->pending_elts = constructor_pending_elts;
4538 p->depth = constructor_depth;
4539 p->replacement_value = 0;
4540 p->implicit = implicit;
4541 p->outer = 0;
4542 p->incremental = constructor_incremental;
4543 p->designated = constructor_designated;
4544 p->next = constructor_stack;
4545 p->range_stack = 0;
4546 constructor_stack = p;
4548 constructor_constant = 1;
4549 constructor_simple = 1;
4550 constructor_depth = SPELLING_DEPTH ();
4551 constructor_elements = 0;
4552 constructor_incremental = 1;
4553 constructor_designated = 0;
4554 constructor_pending_elts = 0;
4555 if (!implicit)
4557 p->range_stack = constructor_range_stack;
4558 constructor_range_stack = 0;
4559 designator_depth = 0;
4560 designator_errorneous = 0;
4563 /* Don't die if an entire brace-pair level is superfluous
4564 in the containing level. */
4565 if (constructor_type == 0)
4567 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4568 || TREE_CODE (constructor_type) == UNION_TYPE)
4570 /* Don't die if there are extra init elts at the end. */
4571 if (constructor_fields == 0)
4572 constructor_type = 0;
4573 else
4575 constructor_type = TREE_TYPE (constructor_fields);
4576 push_member_name (constructor_fields);
4577 constructor_depth++;
4580 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4582 constructor_type = TREE_TYPE (constructor_type);
4583 push_array_bounds (tree_low_cst (constructor_index, 0));
4584 constructor_depth++;
4587 if (constructor_type == 0)
4589 error_init ("extra brace group at end of initializer");
4590 constructor_fields = 0;
4591 constructor_unfilled_fields = 0;
4592 return;
4595 if (value && TREE_CODE (value) == CONSTRUCTOR)
4597 constructor_constant = TREE_CONSTANT (value);
4598 constructor_simple = TREE_STATIC (value);
4599 constructor_elements = CONSTRUCTOR_ELTS (value);
4600 if (constructor_elements
4601 && (TREE_CODE (constructor_type) == RECORD_TYPE
4602 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4603 set_nonincremental_init ();
4606 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4608 missing_braces_mentioned = 1;
4609 warning_init ("missing braces around initializer");
4612 if (TREE_CODE (constructor_type) == RECORD_TYPE
4613 || TREE_CODE (constructor_type) == UNION_TYPE)
4615 constructor_fields = TYPE_FIELDS (constructor_type);
4616 /* Skip any nameless bit fields at the beginning. */
4617 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4618 && DECL_NAME (constructor_fields) == 0)
4619 constructor_fields = TREE_CHAIN (constructor_fields);
4621 constructor_unfilled_fields = constructor_fields;
4622 constructor_bit_index = bitsize_zero_node;
4624 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4626 /* Vectors are like simple fixed-size arrays. */
4627 constructor_max_index =
4628 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4629 constructor_index = convert (bitsizetype, integer_zero_node);
4630 constructor_unfilled_index = constructor_index;
4632 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4634 if (TYPE_DOMAIN (constructor_type))
4636 constructor_max_index
4637 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4639 /* Detect non-empty initializations of zero-length arrays. */
4640 if (constructor_max_index == NULL_TREE
4641 && TYPE_SIZE (constructor_type))
4642 constructor_max_index = build_int_2 (-1, -1);
4644 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4645 to initialize VLAs will cause a proper error; avoid tree
4646 checking errors as well by setting a safe value. */
4647 if (constructor_max_index
4648 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4649 constructor_max_index = build_int_2 (-1, -1);
4651 constructor_index
4652 = convert (bitsizetype,
4653 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4655 else
4656 constructor_index = bitsize_zero_node;
4658 constructor_unfilled_index = constructor_index;
4659 if (value && TREE_CODE (value) == STRING_CST)
4661 /* We need to split the char/wchar array into individual
4662 characters, so that we don't have to special case it
4663 everywhere. */
4664 set_nonincremental_init_from_string (value);
4667 else
4669 warning_init ("braces around scalar initializer");
4670 constructor_fields = constructor_type;
4671 constructor_unfilled_fields = constructor_type;
4675 /* At the end of an implicit or explicit brace level,
4676 finish up that level of constructor.
4677 If we were outputting the elements as they are read, return 0
4678 from inner levels (process_init_element ignores that),
4679 but return error_mark_node from the outermost level
4680 (that's what we want to put in DECL_INITIAL).
4681 Otherwise, return a CONSTRUCTOR expression. */
4683 tree
4684 pop_init_level (int implicit)
4686 struct constructor_stack *p;
4687 tree constructor = 0;
4689 if (implicit == 0)
4691 /* When we come to an explicit close brace,
4692 pop any inner levels that didn't have explicit braces. */
4693 while (constructor_stack->implicit)
4694 process_init_element (pop_init_level (1));
4696 if (constructor_range_stack)
4697 abort ();
4700 p = constructor_stack;
4702 /* Error for initializing a flexible array member, or a zero-length
4703 array member in an inappropriate context. */
4704 if (constructor_type && constructor_fields
4705 && TREE_CODE (constructor_type) == ARRAY_TYPE
4706 && TYPE_DOMAIN (constructor_type)
4707 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4709 /* Silently discard empty initializations. The parser will
4710 already have pedwarned for empty brackets. */
4711 if (integer_zerop (constructor_unfilled_index))
4712 constructor_type = NULL_TREE;
4713 else if (! TYPE_SIZE (constructor_type))
4715 if (constructor_depth > 2)
4716 error_init ("initialization of flexible array member in a nested context");
4717 else if (pedantic)
4718 pedwarn_init ("initialization of a flexible array member");
4720 /* We have already issued an error message for the existence
4721 of a flexible array member not at the end of the structure.
4722 Discard the initializer so that we do not abort later. */
4723 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4724 constructor_type = NULL_TREE;
4726 else
4727 /* Zero-length arrays are no longer special, so we should no longer
4728 get here. */
4729 abort ();
4732 /* Warn when some struct elements are implicitly initialized to zero. */
4733 if (extra_warnings
4734 && constructor_type
4735 && TREE_CODE (constructor_type) == RECORD_TYPE
4736 && constructor_unfilled_fields)
4738 /* Do not warn for flexible array members or zero-length arrays. */
4739 while (constructor_unfilled_fields
4740 && (! DECL_SIZE (constructor_unfilled_fields)
4741 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4742 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4744 /* Do not warn if this level of the initializer uses member
4745 designators; it is likely to be deliberate. */
4746 if (constructor_unfilled_fields && !constructor_designated)
4748 push_member_name (constructor_unfilled_fields);
4749 warning_init ("missing initializer");
4750 RESTORE_SPELLING_DEPTH (constructor_depth);
4754 /* Now output all pending elements. */
4755 constructor_incremental = 1;
4756 output_pending_init_elements (1);
4758 /* Pad out the end of the structure. */
4759 if (p->replacement_value)
4760 /* If this closes a superfluous brace pair,
4761 just pass out the element between them. */
4762 constructor = p->replacement_value;
4763 else if (constructor_type == 0)
4765 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4766 && TREE_CODE (constructor_type) != UNION_TYPE
4767 && TREE_CODE (constructor_type) != ARRAY_TYPE
4768 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4770 /* A nonincremental scalar initializer--just return
4771 the element, after verifying there is just one. */
4772 if (constructor_elements == 0)
4774 if (!constructor_erroneous)
4775 error_init ("empty scalar initializer");
4776 constructor = error_mark_node;
4778 else if (TREE_CHAIN (constructor_elements) != 0)
4780 error_init ("extra elements in scalar initializer");
4781 constructor = TREE_VALUE (constructor_elements);
4783 else
4784 constructor = TREE_VALUE (constructor_elements);
4786 else
4788 if (constructor_erroneous)
4789 constructor = error_mark_node;
4790 else
4792 constructor = build_constructor (constructor_type,
4793 nreverse (constructor_elements));
4794 if (constructor_constant)
4795 TREE_CONSTANT (constructor) = 1;
4796 if (constructor_constant && constructor_simple)
4797 TREE_STATIC (constructor) = 1;
4801 constructor_type = p->type;
4802 constructor_fields = p->fields;
4803 constructor_index = p->index;
4804 constructor_max_index = p->max_index;
4805 constructor_unfilled_index = p->unfilled_index;
4806 constructor_unfilled_fields = p->unfilled_fields;
4807 constructor_bit_index = p->bit_index;
4808 constructor_elements = p->elements;
4809 constructor_constant = p->constant;
4810 constructor_simple = p->simple;
4811 constructor_erroneous = p->erroneous;
4812 constructor_incremental = p->incremental;
4813 constructor_designated = p->designated;
4814 constructor_pending_elts = p->pending_elts;
4815 constructor_depth = p->depth;
4816 if (!p->implicit)
4817 constructor_range_stack = p->range_stack;
4818 RESTORE_SPELLING_DEPTH (constructor_depth);
4820 constructor_stack = p->next;
4821 free (p);
4823 if (constructor == 0)
4825 if (constructor_stack == 0)
4826 return error_mark_node;
4827 return NULL_TREE;
4829 return constructor;
4832 /* Common handling for both array range and field name designators.
4833 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4835 static int
4836 set_designator (int array)
4838 tree subtype;
4839 enum tree_code subcode;
4841 /* Don't die if an entire brace-pair level is superfluous
4842 in the containing level. */
4843 if (constructor_type == 0)
4844 return 1;
4846 /* If there were errors in this designator list already, bail out silently. */
4847 if (designator_errorneous)
4848 return 1;
4850 if (!designator_depth)
4852 if (constructor_range_stack)
4853 abort ();
4855 /* Designator list starts at the level of closest explicit
4856 braces. */
4857 while (constructor_stack->implicit)
4858 process_init_element (pop_init_level (1));
4859 constructor_designated = 1;
4860 return 0;
4863 if (constructor_no_implicit)
4865 error_init ("initialization designators may not nest");
4866 return 1;
4869 if (TREE_CODE (constructor_type) == RECORD_TYPE
4870 || TREE_CODE (constructor_type) == UNION_TYPE)
4872 subtype = TREE_TYPE (constructor_fields);
4873 if (subtype != error_mark_node)
4874 subtype = TYPE_MAIN_VARIANT (subtype);
4876 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4878 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4880 else
4881 abort ();
4883 subcode = TREE_CODE (subtype);
4884 if (array && subcode != ARRAY_TYPE)
4886 error_init ("array index in non-array initializer");
4887 return 1;
4889 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4891 error_init ("field name not in record or union initializer");
4892 return 1;
4895 constructor_designated = 1;
4896 push_init_level (2);
4897 return 0;
4900 /* If there are range designators in designator list, push a new designator
4901 to constructor_range_stack. RANGE_END is end of such stack range or
4902 NULL_TREE if there is no range designator at this level. */
4904 static void
4905 push_range_stack (tree range_end)
4907 struct constructor_range_stack *p;
4909 p = ggc_alloc (sizeof (struct constructor_range_stack));
4910 p->prev = constructor_range_stack;
4911 p->next = 0;
4912 p->fields = constructor_fields;
4913 p->range_start = constructor_index;
4914 p->index = constructor_index;
4915 p->stack = constructor_stack;
4916 p->range_end = range_end;
4917 if (constructor_range_stack)
4918 constructor_range_stack->next = p;
4919 constructor_range_stack = p;
4922 /* Within an array initializer, specify the next index to be initialized.
4923 FIRST is that index. If LAST is nonzero, then initialize a range
4924 of indices, running from FIRST through LAST. */
4926 void
4927 set_init_index (tree first, tree last)
4929 if (set_designator (1))
4930 return;
4932 designator_errorneous = 1;
4934 while ((TREE_CODE (first) == NOP_EXPR
4935 || TREE_CODE (first) == CONVERT_EXPR
4936 || TREE_CODE (first) == NON_LVALUE_EXPR)
4937 && (TYPE_MODE (TREE_TYPE (first))
4938 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4939 first = TREE_OPERAND (first, 0);
4941 if (last)
4942 while ((TREE_CODE (last) == NOP_EXPR
4943 || TREE_CODE (last) == CONVERT_EXPR
4944 || TREE_CODE (last) == NON_LVALUE_EXPR)
4945 && (TYPE_MODE (TREE_TYPE (last))
4946 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4947 last = TREE_OPERAND (last, 0);
4949 if (TREE_CODE (first) != INTEGER_CST)
4950 error_init ("nonconstant array index in initializer");
4951 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4952 error_init ("nonconstant array index in initializer");
4953 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4954 error_init ("array index in non-array initializer");
4955 else if (constructor_max_index
4956 && tree_int_cst_lt (constructor_max_index, first))
4957 error_init ("array index in initializer exceeds array bounds");
4958 else
4960 constructor_index = convert (bitsizetype, first);
4962 if (last)
4964 if (tree_int_cst_equal (first, last))
4965 last = 0;
4966 else if (tree_int_cst_lt (last, first))
4968 error_init ("empty index range in initializer");
4969 last = 0;
4971 else
4973 last = convert (bitsizetype, last);
4974 if (constructor_max_index != 0
4975 && tree_int_cst_lt (constructor_max_index, last))
4977 error_init ("array index range in initializer exceeds array bounds");
4978 last = 0;
4983 designator_depth++;
4984 designator_errorneous = 0;
4985 if (constructor_range_stack || last)
4986 push_range_stack (last);
4990 /* Within a struct initializer, specify the next field to be initialized. */
4992 void
4993 set_init_label (tree fieldname)
4995 tree tail;
4997 if (set_designator (0))
4998 return;
5000 designator_errorneous = 1;
5002 if (TREE_CODE (constructor_type) != RECORD_TYPE
5003 && TREE_CODE (constructor_type) != UNION_TYPE)
5005 error_init ("field name not in record or union initializer");
5006 return;
5009 for (tail = TYPE_FIELDS (constructor_type); tail;
5010 tail = TREE_CHAIN (tail))
5012 if (DECL_NAME (tail) == fieldname)
5013 break;
5016 if (tail == 0)
5017 error ("unknown field `%s' specified in initializer",
5018 IDENTIFIER_POINTER (fieldname));
5019 else
5021 constructor_fields = tail;
5022 designator_depth++;
5023 designator_errorneous = 0;
5024 if (constructor_range_stack)
5025 push_range_stack (NULL_TREE);
5029 /* Add a new initializer to the tree of pending initializers. PURPOSE
5030 identifies the initializer, either array index or field in a structure.
5031 VALUE is the value of that index or field. */
5033 static void
5034 add_pending_init (tree purpose, tree value)
5036 struct init_node *p, **q, *r;
5038 q = &constructor_pending_elts;
5039 p = 0;
5041 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5043 while (*q != 0)
5045 p = *q;
5046 if (tree_int_cst_lt (purpose, p->purpose))
5047 q = &p->left;
5048 else if (tree_int_cst_lt (p->purpose, purpose))
5049 q = &p->right;
5050 else
5052 if (TREE_SIDE_EFFECTS (p->value))
5053 warning_init ("initialized field with side-effects overwritten");
5054 p->value = value;
5055 return;
5059 else
5061 tree bitpos;
5063 bitpos = bit_position (purpose);
5064 while (*q != NULL)
5066 p = *q;
5067 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5068 q = &p->left;
5069 else if (p->purpose != purpose)
5070 q = &p->right;
5071 else
5073 if (TREE_SIDE_EFFECTS (p->value))
5074 warning_init ("initialized field with side-effects overwritten");
5075 p->value = value;
5076 return;
5081 r = ggc_alloc (sizeof (struct init_node));
5082 r->purpose = purpose;
5083 r->value = value;
5085 *q = r;
5086 r->parent = p;
5087 r->left = 0;
5088 r->right = 0;
5089 r->balance = 0;
5091 while (p)
5093 struct init_node *s;
5095 if (r == p->left)
5097 if (p->balance == 0)
5098 p->balance = -1;
5099 else if (p->balance < 0)
5101 if (r->balance < 0)
5103 /* L rotation. */
5104 p->left = r->right;
5105 if (p->left)
5106 p->left->parent = p;
5107 r->right = p;
5109 p->balance = 0;
5110 r->balance = 0;
5112 s = p->parent;
5113 p->parent = r;
5114 r->parent = s;
5115 if (s)
5117 if (s->left == p)
5118 s->left = r;
5119 else
5120 s->right = r;
5122 else
5123 constructor_pending_elts = r;
5125 else
5127 /* LR rotation. */
5128 struct init_node *t = r->right;
5130 r->right = t->left;
5131 if (r->right)
5132 r->right->parent = r;
5133 t->left = r;
5135 p->left = t->right;
5136 if (p->left)
5137 p->left->parent = p;
5138 t->right = p;
5140 p->balance = t->balance < 0;
5141 r->balance = -(t->balance > 0);
5142 t->balance = 0;
5144 s = p->parent;
5145 p->parent = t;
5146 r->parent = t;
5147 t->parent = s;
5148 if (s)
5150 if (s->left == p)
5151 s->left = t;
5152 else
5153 s->right = t;
5155 else
5156 constructor_pending_elts = t;
5158 break;
5160 else
5162 /* p->balance == +1; growth of left side balances the node. */
5163 p->balance = 0;
5164 break;
5167 else /* r == p->right */
5169 if (p->balance == 0)
5170 /* Growth propagation from right side. */
5171 p->balance++;
5172 else if (p->balance > 0)
5174 if (r->balance > 0)
5176 /* R rotation. */
5177 p->right = r->left;
5178 if (p->right)
5179 p->right->parent = p;
5180 r->left = p;
5182 p->balance = 0;
5183 r->balance = 0;
5185 s = p->parent;
5186 p->parent = r;
5187 r->parent = s;
5188 if (s)
5190 if (s->left == p)
5191 s->left = r;
5192 else
5193 s->right = r;
5195 else
5196 constructor_pending_elts = r;
5198 else /* r->balance == -1 */
5200 /* RL rotation */
5201 struct init_node *t = r->left;
5203 r->left = t->right;
5204 if (r->left)
5205 r->left->parent = r;
5206 t->right = r;
5208 p->right = t->left;
5209 if (p->right)
5210 p->right->parent = p;
5211 t->left = p;
5213 r->balance = (t->balance < 0);
5214 p->balance = -(t->balance > 0);
5215 t->balance = 0;
5217 s = p->parent;
5218 p->parent = t;
5219 r->parent = t;
5220 t->parent = s;
5221 if (s)
5223 if (s->left == p)
5224 s->left = t;
5225 else
5226 s->right = t;
5228 else
5229 constructor_pending_elts = t;
5231 break;
5233 else
5235 /* p->balance == -1; growth of right side balances the node. */
5236 p->balance = 0;
5237 break;
5241 r = p;
5242 p = p->parent;
5246 /* Build AVL tree from a sorted chain. */
5248 static void
5249 set_nonincremental_init (void)
5251 tree chain;
5253 if (TREE_CODE (constructor_type) != RECORD_TYPE
5254 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5255 return;
5257 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5258 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5259 constructor_elements = 0;
5260 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5262 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5263 /* Skip any nameless bit fields at the beginning. */
5264 while (constructor_unfilled_fields != 0
5265 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5266 && DECL_NAME (constructor_unfilled_fields) == 0)
5267 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5270 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5272 if (TYPE_DOMAIN (constructor_type))
5273 constructor_unfilled_index
5274 = convert (bitsizetype,
5275 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5276 else
5277 constructor_unfilled_index = bitsize_zero_node;
5279 constructor_incremental = 0;
5282 /* Build AVL tree from a string constant. */
5284 static void
5285 set_nonincremental_init_from_string (tree str)
5287 tree value, purpose, type;
5288 HOST_WIDE_INT val[2];
5289 const char *p, *end;
5290 int byte, wchar_bytes, charwidth, bitpos;
5292 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5293 abort ();
5295 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5296 == TYPE_PRECISION (char_type_node))
5297 wchar_bytes = 1;
5298 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5299 == TYPE_PRECISION (wchar_type_node))
5300 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5301 else
5302 abort ();
5304 charwidth = TYPE_PRECISION (char_type_node);
5305 type = TREE_TYPE (constructor_type);
5306 p = TREE_STRING_POINTER (str);
5307 end = p + TREE_STRING_LENGTH (str);
5309 for (purpose = bitsize_zero_node;
5310 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5311 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5313 if (wchar_bytes == 1)
5315 val[1] = (unsigned char) *p++;
5316 val[0] = 0;
5318 else
5320 val[0] = 0;
5321 val[1] = 0;
5322 for (byte = 0; byte < wchar_bytes; byte++)
5324 if (BYTES_BIG_ENDIAN)
5325 bitpos = (wchar_bytes - byte - 1) * charwidth;
5326 else
5327 bitpos = byte * charwidth;
5328 val[bitpos < HOST_BITS_PER_WIDE_INT]
5329 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5330 << (bitpos % HOST_BITS_PER_WIDE_INT);
5334 if (!TREE_UNSIGNED (type))
5336 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5337 if (bitpos < HOST_BITS_PER_WIDE_INT)
5339 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5341 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5342 val[0] = -1;
5345 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5347 if (val[1] < 0)
5348 val[0] = -1;
5350 else if (val[0] & (((HOST_WIDE_INT) 1)
5351 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5352 val[0] |= ((HOST_WIDE_INT) -1)
5353 << (bitpos - HOST_BITS_PER_WIDE_INT);
5356 value = build_int_2 (val[1], val[0]);
5357 TREE_TYPE (value) = type;
5358 add_pending_init (purpose, value);
5361 constructor_incremental = 0;
5364 /* Return value of FIELD in pending initializer or zero if the field was
5365 not initialized yet. */
5367 static tree
5368 find_init_member (tree field)
5370 struct init_node *p;
5372 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5374 if (constructor_incremental
5375 && tree_int_cst_lt (field, constructor_unfilled_index))
5376 set_nonincremental_init ();
5378 p = constructor_pending_elts;
5379 while (p)
5381 if (tree_int_cst_lt (field, p->purpose))
5382 p = p->left;
5383 else if (tree_int_cst_lt (p->purpose, field))
5384 p = p->right;
5385 else
5386 return p->value;
5389 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5391 tree bitpos = bit_position (field);
5393 if (constructor_incremental
5394 && (!constructor_unfilled_fields
5395 || tree_int_cst_lt (bitpos,
5396 bit_position (constructor_unfilled_fields))))
5397 set_nonincremental_init ();
5399 p = constructor_pending_elts;
5400 while (p)
5402 if (field == p->purpose)
5403 return p->value;
5404 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5405 p = p->left;
5406 else
5407 p = p->right;
5410 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5412 if (constructor_elements
5413 && TREE_PURPOSE (constructor_elements) == field)
5414 return TREE_VALUE (constructor_elements);
5416 return 0;
5419 /* "Output" the next constructor element.
5420 At top level, really output it to assembler code now.
5421 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5422 TYPE is the data type that the containing data type wants here.
5423 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5425 PENDING if non-nil means output pending elements that belong
5426 right after this element. (PENDING is normally 1;
5427 it is 0 while outputting pending elements, to avoid recursion.) */
5429 static void
5430 output_init_element (tree value, tree type, tree field, int pending)
5432 if (type == error_mark_node)
5434 constructor_erroneous = 1;
5435 return;
5437 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5438 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5439 && !(TREE_CODE (value) == STRING_CST
5440 && TREE_CODE (type) == ARRAY_TYPE
5441 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5442 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5443 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5444 value = default_conversion (value);
5446 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5447 && require_constant_value && !flag_isoc99 && pending)
5449 /* As an extension, allow initializing objects with static storage
5450 duration with compound literals (which are then treated just as
5451 the brace enclosed list they contain). */
5452 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5453 value = DECL_INITIAL (decl);
5456 if (value == error_mark_node)
5457 constructor_erroneous = 1;
5458 else if (!TREE_CONSTANT (value))
5459 constructor_constant = 0;
5460 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5461 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5462 || TREE_CODE (constructor_type) == UNION_TYPE)
5463 && DECL_C_BIT_FIELD (field)
5464 && TREE_CODE (value) != INTEGER_CST))
5465 constructor_simple = 0;
5467 if (require_constant_value && ! TREE_CONSTANT (value))
5469 error_init ("initializer element is not constant");
5470 value = error_mark_node;
5472 else if (require_constant_elements
5473 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5474 pedwarn ("initializer element is not computable at load time");
5476 /* If this field is empty (and not at the end of structure),
5477 don't do anything other than checking the initializer. */
5478 if (field
5479 && (TREE_TYPE (field) == error_mark_node
5480 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5481 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5482 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5483 || TREE_CHAIN (field)))))
5484 return;
5486 value = digest_init (type, value, require_constant_value);
5487 if (value == error_mark_node)
5489 constructor_erroneous = 1;
5490 return;
5493 /* If this element doesn't come next in sequence,
5494 put it on constructor_pending_elts. */
5495 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5496 && (!constructor_incremental
5497 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5499 if (constructor_incremental
5500 && tree_int_cst_lt (field, constructor_unfilled_index))
5501 set_nonincremental_init ();
5503 add_pending_init (field, value);
5504 return;
5506 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5507 && (!constructor_incremental
5508 || field != constructor_unfilled_fields))
5510 /* We do this for records but not for unions. In a union,
5511 no matter which field is specified, it can be initialized
5512 right away since it starts at the beginning of the union. */
5513 if (constructor_incremental)
5515 if (!constructor_unfilled_fields)
5516 set_nonincremental_init ();
5517 else
5519 tree bitpos, unfillpos;
5521 bitpos = bit_position (field);
5522 unfillpos = bit_position (constructor_unfilled_fields);
5524 if (tree_int_cst_lt (bitpos, unfillpos))
5525 set_nonincremental_init ();
5529 add_pending_init (field, value);
5530 return;
5532 else if (TREE_CODE (constructor_type) == UNION_TYPE
5533 && constructor_elements)
5535 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5536 warning_init ("initialized field with side-effects overwritten");
5538 /* We can have just one union field set. */
5539 constructor_elements = 0;
5542 /* Otherwise, output this element either to
5543 constructor_elements or to the assembler file. */
5545 if (field && TREE_CODE (field) == INTEGER_CST)
5546 field = copy_node (field);
5547 constructor_elements
5548 = tree_cons (field, value, constructor_elements);
5550 /* Advance the variable that indicates sequential elements output. */
5551 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5552 constructor_unfilled_index
5553 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5554 bitsize_one_node);
5555 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5557 constructor_unfilled_fields
5558 = TREE_CHAIN (constructor_unfilled_fields);
5560 /* Skip any nameless bit fields. */
5561 while (constructor_unfilled_fields != 0
5562 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5563 && DECL_NAME (constructor_unfilled_fields) == 0)
5564 constructor_unfilled_fields =
5565 TREE_CHAIN (constructor_unfilled_fields);
5567 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5568 constructor_unfilled_fields = 0;
5570 /* Now output any pending elements which have become next. */
5571 if (pending)
5572 output_pending_init_elements (0);
5575 /* Output any pending elements which have become next.
5576 As we output elements, constructor_unfilled_{fields,index}
5577 advances, which may cause other elements to become next;
5578 if so, they too are output.
5580 If ALL is 0, we return when there are
5581 no more pending elements to output now.
5583 If ALL is 1, we output space as necessary so that
5584 we can output all the pending elements. */
5586 static void
5587 output_pending_init_elements (int all)
5589 struct init_node *elt = constructor_pending_elts;
5590 tree next;
5592 retry:
5594 /* Look thru the whole pending tree.
5595 If we find an element that should be output now,
5596 output it. Otherwise, set NEXT to the element
5597 that comes first among those still pending. */
5599 next = 0;
5600 while (elt)
5602 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5604 if (tree_int_cst_equal (elt->purpose,
5605 constructor_unfilled_index))
5606 output_init_element (elt->value,
5607 TREE_TYPE (constructor_type),
5608 constructor_unfilled_index, 0);
5609 else if (tree_int_cst_lt (constructor_unfilled_index,
5610 elt->purpose))
5612 /* Advance to the next smaller node. */
5613 if (elt->left)
5614 elt = elt->left;
5615 else
5617 /* We have reached the smallest node bigger than the
5618 current unfilled index. Fill the space first. */
5619 next = elt->purpose;
5620 break;
5623 else
5625 /* Advance to the next bigger node. */
5626 if (elt->right)
5627 elt = elt->right;
5628 else
5630 /* We have reached the biggest node in a subtree. Find
5631 the parent of it, which is the next bigger node. */
5632 while (elt->parent && elt->parent->right == elt)
5633 elt = elt->parent;
5634 elt = elt->parent;
5635 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5636 elt->purpose))
5638 next = elt->purpose;
5639 break;
5644 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5645 || TREE_CODE (constructor_type) == UNION_TYPE)
5647 tree ctor_unfilled_bitpos, elt_bitpos;
5649 /* If the current record is complete we are done. */
5650 if (constructor_unfilled_fields == 0)
5651 break;
5653 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5654 elt_bitpos = bit_position (elt->purpose);
5655 /* We can't compare fields here because there might be empty
5656 fields in between. */
5657 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5659 constructor_unfilled_fields = elt->purpose;
5660 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5661 elt->purpose, 0);
5663 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5665 /* Advance to the next smaller node. */
5666 if (elt->left)
5667 elt = elt->left;
5668 else
5670 /* We have reached the smallest node bigger than the
5671 current unfilled field. Fill the space first. */
5672 next = elt->purpose;
5673 break;
5676 else
5678 /* Advance to the next bigger node. */
5679 if (elt->right)
5680 elt = elt->right;
5681 else
5683 /* We have reached the biggest node in a subtree. Find
5684 the parent of it, which is the next bigger node. */
5685 while (elt->parent && elt->parent->right == elt)
5686 elt = elt->parent;
5687 elt = elt->parent;
5688 if (elt
5689 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5690 bit_position (elt->purpose))))
5692 next = elt->purpose;
5693 break;
5700 /* Ordinarily return, but not if we want to output all
5701 and there are elements left. */
5702 if (! (all && next != 0))
5703 return;
5705 /* If it's not incremental, just skip over the gap, so that after
5706 jumping to retry we will output the next successive element. */
5707 if (TREE_CODE (constructor_type) == RECORD_TYPE
5708 || TREE_CODE (constructor_type) == UNION_TYPE)
5709 constructor_unfilled_fields = next;
5710 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5711 constructor_unfilled_index = next;
5713 /* ELT now points to the node in the pending tree with the next
5714 initializer to output. */
5715 goto retry;
5718 /* Add one non-braced element to the current constructor level.
5719 This adjusts the current position within the constructor's type.
5720 This may also start or terminate implicit levels
5721 to handle a partly-braced initializer.
5723 Once this has found the correct level for the new element,
5724 it calls output_init_element. */
5726 void
5727 process_init_element (tree value)
5729 tree orig_value = value;
5730 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5732 designator_depth = 0;
5733 designator_errorneous = 0;
5735 /* Handle superfluous braces around string cst as in
5736 char x[] = {"foo"}; */
5737 if (string_flag
5738 && constructor_type
5739 && TREE_CODE (constructor_type) == ARRAY_TYPE
5740 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5741 && integer_zerop (constructor_unfilled_index))
5743 if (constructor_stack->replacement_value)
5744 error_init ("excess elements in char array initializer");
5745 constructor_stack->replacement_value = value;
5746 return;
5749 if (constructor_stack->replacement_value != 0)
5751 error_init ("excess elements in struct initializer");
5752 return;
5755 /* Ignore elements of a brace group if it is entirely superfluous
5756 and has already been diagnosed. */
5757 if (constructor_type == 0)
5758 return;
5760 /* If we've exhausted any levels that didn't have braces,
5761 pop them now. */
5762 while (constructor_stack->implicit)
5764 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5765 || TREE_CODE (constructor_type) == UNION_TYPE)
5766 && constructor_fields == 0)
5767 process_init_element (pop_init_level (1));
5768 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5769 && (constructor_max_index == 0
5770 || tree_int_cst_lt (constructor_max_index,
5771 constructor_index)))
5772 process_init_element (pop_init_level (1));
5773 else
5774 break;
5777 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5778 if (constructor_range_stack)
5780 /* If value is a compound literal and we'll be just using its
5781 content, don't put it into a SAVE_EXPR. */
5782 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5783 || !require_constant_value
5784 || flag_isoc99)
5785 value = save_expr (value);
5788 while (1)
5790 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5792 tree fieldtype;
5793 enum tree_code fieldcode;
5795 if (constructor_fields == 0)
5797 pedwarn_init ("excess elements in struct initializer");
5798 break;
5801 fieldtype = TREE_TYPE (constructor_fields);
5802 if (fieldtype != error_mark_node)
5803 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5804 fieldcode = TREE_CODE (fieldtype);
5806 /* Error for non-static initialization of a flexible array member. */
5807 if (fieldcode == ARRAY_TYPE
5808 && !require_constant_value
5809 && TYPE_SIZE (fieldtype) == NULL_TREE
5810 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5812 error_init ("non-static initialization of a flexible array member");
5813 break;
5816 /* Accept a string constant to initialize a subarray. */
5817 if (value != 0
5818 && fieldcode == ARRAY_TYPE
5819 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5820 && string_flag)
5821 value = orig_value;
5822 /* Otherwise, if we have come to a subaggregate,
5823 and we don't have an element of its type, push into it. */
5824 else if (value != 0 && !constructor_no_implicit
5825 && value != error_mark_node
5826 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5827 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5828 || fieldcode == UNION_TYPE))
5830 push_init_level (1);
5831 continue;
5834 if (value)
5836 push_member_name (constructor_fields);
5837 output_init_element (value, fieldtype, constructor_fields, 1);
5838 RESTORE_SPELLING_DEPTH (constructor_depth);
5840 else
5841 /* Do the bookkeeping for an element that was
5842 directly output as a constructor. */
5844 /* For a record, keep track of end position of last field. */
5845 if (DECL_SIZE (constructor_fields))
5846 constructor_bit_index
5847 = size_binop (PLUS_EXPR,
5848 bit_position (constructor_fields),
5849 DECL_SIZE (constructor_fields));
5851 /* If the current field was the first one not yet written out,
5852 it isn't now, so update. */
5853 if (constructor_unfilled_fields == constructor_fields)
5855 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5856 /* Skip any nameless bit fields. */
5857 while (constructor_unfilled_fields != 0
5858 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5859 && DECL_NAME (constructor_unfilled_fields) == 0)
5860 constructor_unfilled_fields =
5861 TREE_CHAIN (constructor_unfilled_fields);
5865 constructor_fields = TREE_CHAIN (constructor_fields);
5866 /* Skip any nameless bit fields at the beginning. */
5867 while (constructor_fields != 0
5868 && DECL_C_BIT_FIELD (constructor_fields)
5869 && DECL_NAME (constructor_fields) == 0)
5870 constructor_fields = TREE_CHAIN (constructor_fields);
5872 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5874 tree fieldtype;
5875 enum tree_code fieldcode;
5877 if (constructor_fields == 0)
5879 pedwarn_init ("excess elements in union initializer");
5880 break;
5883 fieldtype = TREE_TYPE (constructor_fields);
5884 if (fieldtype != error_mark_node)
5885 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5886 fieldcode = TREE_CODE (fieldtype);
5888 /* Warn that traditional C rejects initialization of unions.
5889 We skip the warning if the value is zero. This is done
5890 under the assumption that the zero initializer in user
5891 code appears conditioned on e.g. __STDC__ to avoid
5892 "missing initializer" warnings and relies on default
5893 initialization to zero in the traditional C case.
5894 We also skip the warning if the initializer is designated,
5895 again on the assumption that this must be conditional on
5896 __STDC__ anyway (and we've already complained about the
5897 member-designator already). */
5898 if (warn_traditional && !in_system_header && !constructor_designated
5899 && !(value && (integer_zerop (value) || real_zerop (value))))
5900 warning ("traditional C rejects initialization of unions");
5902 /* Accept a string constant to initialize a subarray. */
5903 if (value != 0
5904 && fieldcode == ARRAY_TYPE
5905 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5906 && string_flag)
5907 value = orig_value;
5908 /* Otherwise, if we have come to a subaggregate,
5909 and we don't have an element of its type, push into it. */
5910 else if (value != 0 && !constructor_no_implicit
5911 && value != error_mark_node
5912 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5913 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5914 || fieldcode == UNION_TYPE))
5916 push_init_level (1);
5917 continue;
5920 if (value)
5922 push_member_name (constructor_fields);
5923 output_init_element (value, fieldtype, constructor_fields, 1);
5924 RESTORE_SPELLING_DEPTH (constructor_depth);
5926 else
5927 /* Do the bookkeeping for an element that was
5928 directly output as a constructor. */
5930 constructor_bit_index = DECL_SIZE (constructor_fields);
5931 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5934 constructor_fields = 0;
5936 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5938 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5939 enum tree_code eltcode = TREE_CODE (elttype);
5941 /* Accept a string constant to initialize a subarray. */
5942 if (value != 0
5943 && eltcode == ARRAY_TYPE
5944 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5945 && string_flag)
5946 value = orig_value;
5947 /* Otherwise, if we have come to a subaggregate,
5948 and we don't have an element of its type, push into it. */
5949 else if (value != 0 && !constructor_no_implicit
5950 && value != error_mark_node
5951 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5952 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5953 || eltcode == UNION_TYPE))
5955 push_init_level (1);
5956 continue;
5959 if (constructor_max_index != 0
5960 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5961 || integer_all_onesp (constructor_max_index)))
5963 pedwarn_init ("excess elements in array initializer");
5964 break;
5967 /* Now output the actual element. */
5968 if (value)
5970 push_array_bounds (tree_low_cst (constructor_index, 0));
5971 output_init_element (value, elttype, constructor_index, 1);
5972 RESTORE_SPELLING_DEPTH (constructor_depth);
5975 constructor_index
5976 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5978 if (! value)
5979 /* If we are doing the bookkeeping for an element that was
5980 directly output as a constructor, we must update
5981 constructor_unfilled_index. */
5982 constructor_unfilled_index = constructor_index;
5984 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5986 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5988 /* Do a basic check of initializer size. Note that vectors
5989 always have a fixed size derived from their type. */
5990 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5992 pedwarn_init ("excess elements in vector initializer");
5993 break;
5996 /* Now output the actual element. */
5997 if (value)
5998 output_init_element (value, elttype, constructor_index, 1);
6000 constructor_index
6001 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6003 if (! value)
6004 /* If we are doing the bookkeeping for an element that was
6005 directly output as a constructor, we must update
6006 constructor_unfilled_index. */
6007 constructor_unfilled_index = constructor_index;
6010 /* Handle the sole element allowed in a braced initializer
6011 for a scalar variable. */
6012 else if (constructor_fields == 0)
6014 pedwarn_init ("excess elements in scalar initializer");
6015 break;
6017 else
6019 if (value)
6020 output_init_element (value, constructor_type, NULL_TREE, 1);
6021 constructor_fields = 0;
6024 /* Handle range initializers either at this level or anywhere higher
6025 in the designator stack. */
6026 if (constructor_range_stack)
6028 struct constructor_range_stack *p, *range_stack;
6029 int finish = 0;
6031 range_stack = constructor_range_stack;
6032 constructor_range_stack = 0;
6033 while (constructor_stack != range_stack->stack)
6035 if (!constructor_stack->implicit)
6036 abort ();
6037 process_init_element (pop_init_level (1));
6039 for (p = range_stack;
6040 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6041 p = p->prev)
6043 if (!constructor_stack->implicit)
6044 abort ();
6045 process_init_element (pop_init_level (1));
6048 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6049 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6050 finish = 1;
6052 while (1)
6054 constructor_index = p->index;
6055 constructor_fields = p->fields;
6056 if (finish && p->range_end && p->index == p->range_start)
6058 finish = 0;
6059 p->prev = 0;
6061 p = p->next;
6062 if (!p)
6063 break;
6064 push_init_level (2);
6065 p->stack = constructor_stack;
6066 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6067 p->index = p->range_start;
6070 if (!finish)
6071 constructor_range_stack = range_stack;
6072 continue;
6075 break;
6078 constructor_range_stack = 0;
6081 /* Build a simple asm-statement, from one string literal. */
6082 tree
6083 simple_asm_stmt (tree expr)
6085 STRIP_NOPS (expr);
6087 if (TREE_CODE (expr) == ADDR_EXPR)
6088 expr = TREE_OPERAND (expr, 0);
6090 if (TREE_CODE (expr) == STRING_CST)
6092 tree stmt;
6094 /* Simple asm statements are treated as volatile. */
6095 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6096 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6097 ASM_INPUT_P (stmt) = 1;
6098 return stmt;
6101 error ("argument of `asm' is not a constant string");
6102 return NULL_TREE;
6105 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6106 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6108 tree
6109 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6110 tree clobbers)
6112 tree tail;
6114 if (TREE_CODE (string) != STRING_CST)
6116 error ("asm template is not a string constant");
6117 return NULL_TREE;
6120 if (cv_qualifier != NULL_TREE
6121 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6123 warning ("%s qualifier ignored on asm",
6124 IDENTIFIER_POINTER (cv_qualifier));
6125 cv_qualifier = NULL_TREE;
6128 /* We can remove output conversions that change the type,
6129 but not the mode. */
6130 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6132 tree output = TREE_VALUE (tail);
6134 STRIP_NOPS (output);
6135 TREE_VALUE (tail) = output;
6137 /* Allow conversions as LHS here. build_modify_expr as called below
6138 will do the right thing with them. */
6139 while (TREE_CODE (output) == NOP_EXPR
6140 || TREE_CODE (output) == CONVERT_EXPR
6141 || TREE_CODE (output) == FLOAT_EXPR
6142 || TREE_CODE (output) == FIX_TRUNC_EXPR
6143 || TREE_CODE (output) == FIX_FLOOR_EXPR
6144 || TREE_CODE (output) == FIX_ROUND_EXPR
6145 || TREE_CODE (output) == FIX_CEIL_EXPR)
6146 output = TREE_OPERAND (output, 0);
6148 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6151 /* Remove output conversions that change the type but not the mode. */
6152 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6154 tree output = TREE_VALUE (tail);
6155 STRIP_NOPS (output);
6156 TREE_VALUE (tail) = output;
6159 /* Perform default conversions on array and function inputs.
6160 Don't do this for other types as it would screw up operands
6161 expected to be in memory. */
6162 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6163 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6165 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6166 outputs, inputs, clobbers));
6169 /* Expand an ASM statement with operands, handling output operands
6170 that are not variables or INDIRECT_REFS by transforming such
6171 cases into cases that expand_asm_operands can handle.
6173 Arguments are same as for expand_asm_operands. */
6175 void
6176 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6177 tree clobbers, int vol, const char *filename,
6178 int line)
6180 int noutputs = list_length (outputs);
6181 int i;
6182 /* o[I] is the place that output number I should be written. */
6183 tree *o = alloca (noutputs * sizeof (tree));
6184 tree tail;
6186 /* Record the contents of OUTPUTS before it is modified. */
6187 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6189 o[i] = TREE_VALUE (tail);
6190 if (o[i] == error_mark_node)
6191 return;
6194 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6195 OUTPUTS some trees for where the values were actually stored. */
6196 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6198 /* Copy all the intermediate outputs into the specified outputs. */
6199 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6201 if (o[i] != TREE_VALUE (tail))
6203 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6204 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6205 free_temp_slots ();
6207 /* Restore the original value so that it's correct the next
6208 time we expand this function. */
6209 TREE_VALUE (tail) = o[i];
6211 /* Detect modification of read-only values.
6212 (Otherwise done by build_modify_expr.) */
6213 else
6215 tree type = TREE_TYPE (o[i]);
6216 if (TREE_READONLY (o[i])
6217 || TYPE_READONLY (type)
6218 || ((TREE_CODE (type) == RECORD_TYPE
6219 || TREE_CODE (type) == UNION_TYPE)
6220 && C_TYPE_FIELDS_READONLY (type)))
6221 readonly_warning (o[i], "modification by `asm'");
6225 /* Those MODIFY_EXPRs could do autoincrements. */
6226 emit_queue ();
6229 /* Expand a C `return' statement.
6230 RETVAL is the expression for what to return,
6231 or a null pointer for `return;' with no value. */
6233 tree
6234 c_expand_return (tree retval)
6236 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6238 if (TREE_THIS_VOLATILE (current_function_decl))
6239 warning ("function declared `noreturn' has a `return' statement");
6241 if (!retval)
6243 current_function_returns_null = 1;
6244 if ((warn_return_type || flag_isoc99)
6245 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6246 pedwarn_c99 ("`return' with no value, in function returning non-void");
6248 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6250 current_function_returns_null = 1;
6251 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6252 pedwarn ("`return' with a value, in function returning void");
6254 else
6256 tree t = convert_for_assignment (valtype, retval, _("return"),
6257 NULL_TREE, NULL_TREE, 0);
6258 tree res = DECL_RESULT (current_function_decl);
6259 tree inner;
6261 current_function_returns_value = 1;
6262 if (t == error_mark_node)
6263 return NULL_TREE;
6265 inner = t = convert (TREE_TYPE (res), t);
6267 /* Strip any conversions, additions, and subtractions, and see if
6268 we are returning the address of a local variable. Warn if so. */
6269 while (1)
6271 switch (TREE_CODE (inner))
6273 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6274 case PLUS_EXPR:
6275 inner = TREE_OPERAND (inner, 0);
6276 continue;
6278 case MINUS_EXPR:
6279 /* If the second operand of the MINUS_EXPR has a pointer
6280 type (or is converted from it), this may be valid, so
6281 don't give a warning. */
6283 tree op1 = TREE_OPERAND (inner, 1);
6285 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6286 && (TREE_CODE (op1) == NOP_EXPR
6287 || TREE_CODE (op1) == NON_LVALUE_EXPR
6288 || TREE_CODE (op1) == CONVERT_EXPR))
6289 op1 = TREE_OPERAND (op1, 0);
6291 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6292 break;
6294 inner = TREE_OPERAND (inner, 0);
6295 continue;
6298 case ADDR_EXPR:
6299 inner = TREE_OPERAND (inner, 0);
6301 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6302 inner = TREE_OPERAND (inner, 0);
6304 if (TREE_CODE (inner) == VAR_DECL
6305 && ! DECL_EXTERNAL (inner)
6306 && ! TREE_STATIC (inner)
6307 && DECL_CONTEXT (inner) == current_function_decl)
6308 warning ("function returns address of local variable");
6309 break;
6311 default:
6312 break;
6315 break;
6318 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6321 return add_stmt (build_return_stmt (retval));
6324 struct c_switch {
6325 /* The SWITCH_STMT being built. */
6326 tree switch_stmt;
6327 /* A splay-tree mapping the low element of a case range to the high
6328 element, or NULL_TREE if there is no high element. Used to
6329 determine whether or not a new case label duplicates an old case
6330 label. We need a tree, rather than simply a hash table, because
6331 of the GNU case range extension. */
6332 splay_tree cases;
6333 /* The next node on the stack. */
6334 struct c_switch *next;
6337 /* A stack of the currently active switch statements. The innermost
6338 switch statement is on the top of the stack. There is no need to
6339 mark the stack for garbage collection because it is only active
6340 during the processing of the body of a function, and we never
6341 collect at that point. */
6343 static struct c_switch *switch_stack;
6345 /* Start a C switch statement, testing expression EXP. Return the new
6346 SWITCH_STMT. */
6348 tree
6349 c_start_case (tree exp)
6351 enum tree_code code;
6352 tree type, orig_type = error_mark_node;
6353 struct c_switch *cs;
6355 if (exp != error_mark_node)
6357 code = TREE_CODE (TREE_TYPE (exp));
6358 orig_type = TREE_TYPE (exp);
6360 if (! INTEGRAL_TYPE_P (orig_type)
6361 && code != ERROR_MARK)
6363 error ("switch quantity not an integer");
6364 exp = integer_zero_node;
6366 else
6368 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6370 if (warn_traditional && !in_system_header
6371 && (type == long_integer_type_node
6372 || type == long_unsigned_type_node))
6373 warning ("`long' switch expression not converted to `int' in ISO C");
6375 exp = default_conversion (exp);
6376 type = TREE_TYPE (exp);
6380 /* Add this new SWITCH_STMT to the stack. */
6381 cs = xmalloc (sizeof (*cs));
6382 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6383 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6384 cs->next = switch_stack;
6385 switch_stack = cs;
6387 return add_stmt (switch_stack->switch_stmt);
6390 /* Process a case label. */
6392 tree
6393 do_case (tree low_value, tree high_value)
6395 tree label = NULL_TREE;
6397 if (switch_stack)
6399 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6401 label = c_add_case_label (switch_stack->cases,
6402 SWITCH_COND (switch_stack->switch_stmt),
6403 low_value, high_value);
6404 if (label == error_mark_node)
6405 label = NULL_TREE;
6406 else if (switch_was_empty_p)
6408 /* Attach the first case label to the SWITCH_BODY. */
6409 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6410 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6413 else if (low_value)
6414 error ("case label not within a switch statement");
6415 else
6416 error ("`default' label not within a switch statement");
6418 return label;
6421 /* Finish the switch statement. */
6423 void
6424 c_finish_case (void)
6426 struct c_switch *cs = switch_stack;
6428 /* Rechain the next statements to the SWITCH_STMT. */
6429 last_tree = cs->switch_stmt;
6431 /* Pop the stack. */
6432 switch_stack = switch_stack->next;
6433 splay_tree_delete (cs->cases);
6434 free (cs);
6437 /* Build a binary-operation expression without default conversions.
6438 CODE is the kind of expression to build.
6439 This function differs from `build' in several ways:
6440 the data type of the result is computed and recorded in it,
6441 warnings are generated if arg data types are invalid,
6442 special handling for addition and subtraction of pointers is known,
6443 and some optimization is done (operations on narrow ints
6444 are done in the narrower type when that gives the same result).
6445 Constant folding is also done before the result is returned.
6447 Note that the operands will never have enumeral types, or function
6448 or array types, because either they will have the default conversions
6449 performed or they have both just been converted to some other type in which
6450 the arithmetic is to be done. */
6452 tree
6453 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6454 int convert_p)
6456 tree type0, type1;
6457 enum tree_code code0, code1;
6458 tree op0, op1;
6460 /* Expression code to give to the expression when it is built.
6461 Normally this is CODE, which is what the caller asked for,
6462 but in some special cases we change it. */
6463 enum tree_code resultcode = code;
6465 /* Data type in which the computation is to be performed.
6466 In the simplest cases this is the common type of the arguments. */
6467 tree result_type = NULL;
6469 /* Nonzero means operands have already been type-converted
6470 in whatever way is necessary.
6471 Zero means they need to be converted to RESULT_TYPE. */
6472 int converted = 0;
6474 /* Nonzero means create the expression with this type, rather than
6475 RESULT_TYPE. */
6476 tree build_type = 0;
6478 /* Nonzero means after finally constructing the expression
6479 convert it to this type. */
6480 tree final_type = 0;
6482 /* Nonzero if this is an operation like MIN or MAX which can
6483 safely be computed in short if both args are promoted shorts.
6484 Also implies COMMON.
6485 -1 indicates a bitwise operation; this makes a difference
6486 in the exact conditions for when it is safe to do the operation
6487 in a narrower mode. */
6488 int shorten = 0;
6490 /* Nonzero if this is a comparison operation;
6491 if both args are promoted shorts, compare the original shorts.
6492 Also implies COMMON. */
6493 int short_compare = 0;
6495 /* Nonzero if this is a right-shift operation, which can be computed on the
6496 original short and then promoted if the operand is a promoted short. */
6497 int short_shift = 0;
6499 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6500 int common = 0;
6502 if (convert_p)
6504 op0 = default_conversion (orig_op0);
6505 op1 = default_conversion (orig_op1);
6507 else
6509 op0 = orig_op0;
6510 op1 = orig_op1;
6513 type0 = TREE_TYPE (op0);
6514 type1 = TREE_TYPE (op1);
6516 /* The expression codes of the data types of the arguments tell us
6517 whether the arguments are integers, floating, pointers, etc. */
6518 code0 = TREE_CODE (type0);
6519 code1 = TREE_CODE (type1);
6521 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6522 STRIP_TYPE_NOPS (op0);
6523 STRIP_TYPE_NOPS (op1);
6525 /* If an error was already reported for one of the arguments,
6526 avoid reporting another error. */
6528 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6529 return error_mark_node;
6531 switch (code)
6533 case PLUS_EXPR:
6534 /* Handle the pointer + int case. */
6535 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6536 return pointer_int_sum (PLUS_EXPR, op0, op1);
6537 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6538 return pointer_int_sum (PLUS_EXPR, op1, op0);
6539 else
6540 common = 1;
6541 break;
6543 case MINUS_EXPR:
6544 /* Subtraction of two similar pointers.
6545 We must subtract them as integers, then divide by object size. */
6546 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6547 && comp_target_types (type0, type1, 1))
6548 return pointer_diff (op0, op1);
6549 /* Handle pointer minus int. Just like pointer plus int. */
6550 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6551 return pointer_int_sum (MINUS_EXPR, op0, op1);
6552 else
6553 common = 1;
6554 break;
6556 case MULT_EXPR:
6557 common = 1;
6558 break;
6560 case TRUNC_DIV_EXPR:
6561 case CEIL_DIV_EXPR:
6562 case FLOOR_DIV_EXPR:
6563 case ROUND_DIV_EXPR:
6564 case EXACT_DIV_EXPR:
6565 /* Floating point division by zero is a legitimate way to obtain
6566 infinities and NaNs. */
6567 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6568 warning ("division by zero");
6570 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6571 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6572 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6573 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6575 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6576 resultcode = RDIV_EXPR;
6577 else
6578 /* Although it would be tempting to shorten always here, that
6579 loses on some targets, since the modulo instruction is
6580 undefined if the quotient can't be represented in the
6581 computation mode. We shorten only if unsigned or if
6582 dividing by something we know != -1. */
6583 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6584 || (TREE_CODE (op1) == INTEGER_CST
6585 && ! integer_all_onesp (op1)));
6586 common = 1;
6588 break;
6590 case BIT_AND_EXPR:
6591 case BIT_ANDTC_EXPR:
6592 case BIT_IOR_EXPR:
6593 case BIT_XOR_EXPR:
6594 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6595 shorten = -1;
6596 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6597 common = 1;
6598 break;
6600 case TRUNC_MOD_EXPR:
6601 case FLOOR_MOD_EXPR:
6602 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6603 warning ("division by zero");
6605 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6607 /* Although it would be tempting to shorten always here, that loses
6608 on some targets, since the modulo instruction is undefined if the
6609 quotient can't be represented in the computation mode. We shorten
6610 only if unsigned or if dividing by something we know != -1. */
6611 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6612 || (TREE_CODE (op1) == INTEGER_CST
6613 && ! integer_all_onesp (op1)));
6614 common = 1;
6616 break;
6618 case TRUTH_ANDIF_EXPR:
6619 case TRUTH_ORIF_EXPR:
6620 case TRUTH_AND_EXPR:
6621 case TRUTH_OR_EXPR:
6622 case TRUTH_XOR_EXPR:
6623 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6624 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6625 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6626 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6628 /* Result of these operations is always an int,
6629 but that does not mean the operands should be
6630 converted to ints! */
6631 result_type = integer_type_node;
6632 op0 = c_common_truthvalue_conversion (op0);
6633 op1 = c_common_truthvalue_conversion (op1);
6634 converted = 1;
6636 break;
6638 /* Shift operations: result has same type as first operand;
6639 always convert second operand to int.
6640 Also set SHORT_SHIFT if shifting rightward. */
6642 case RSHIFT_EXPR:
6643 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6645 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6647 if (tree_int_cst_sgn (op1) < 0)
6648 warning ("right shift count is negative");
6649 else
6651 if (! integer_zerop (op1))
6652 short_shift = 1;
6654 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6655 warning ("right shift count >= width of type");
6659 /* Use the type of the value to be shifted. */
6660 result_type = type0;
6661 /* Convert the shift-count to an integer, regardless of size
6662 of value being shifted. */
6663 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6664 op1 = convert (integer_type_node, op1);
6665 /* Avoid converting op1 to result_type later. */
6666 converted = 1;
6668 break;
6670 case LSHIFT_EXPR:
6671 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6673 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6675 if (tree_int_cst_sgn (op1) < 0)
6676 warning ("left shift count is negative");
6678 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6679 warning ("left shift count >= width of type");
6682 /* Use the type of the value to be shifted. */
6683 result_type = type0;
6684 /* Convert the shift-count to an integer, regardless of size
6685 of value being shifted. */
6686 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6687 op1 = convert (integer_type_node, op1);
6688 /* Avoid converting op1 to result_type later. */
6689 converted = 1;
6691 break;
6693 case RROTATE_EXPR:
6694 case LROTATE_EXPR:
6695 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6697 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6699 if (tree_int_cst_sgn (op1) < 0)
6700 warning ("shift count is negative");
6701 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6702 warning ("shift count >= width of type");
6705 /* Use the type of the value to be shifted. */
6706 result_type = type0;
6707 /* Convert the shift-count to an integer, regardless of size
6708 of value being shifted. */
6709 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6710 op1 = convert (integer_type_node, op1);
6711 /* Avoid converting op1 to result_type later. */
6712 converted = 1;
6714 break;
6716 case EQ_EXPR:
6717 case NE_EXPR:
6718 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6719 warning ("comparing floating point with == or != is unsafe");
6720 /* Result of comparison is always int,
6721 but don't convert the args to int! */
6722 build_type = integer_type_node;
6723 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6724 || code0 == COMPLEX_TYPE
6725 || code0 == VECTOR_TYPE)
6726 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6727 || code1 == COMPLEX_TYPE
6728 || code1 == VECTOR_TYPE))
6729 short_compare = 1;
6730 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6732 tree tt0 = TREE_TYPE (type0);
6733 tree tt1 = TREE_TYPE (type1);
6734 /* Anything compares with void *. void * compares with anything.
6735 Otherwise, the targets must be compatible
6736 and both must be object or both incomplete. */
6737 if (comp_target_types (type0, type1, 1))
6738 result_type = common_type (type0, type1);
6739 else if (VOID_TYPE_P (tt0))
6741 /* op0 != orig_op0 detects the case of something
6742 whose value is 0 but which isn't a valid null ptr const. */
6743 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6744 && TREE_CODE (tt1) == FUNCTION_TYPE)
6745 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6747 else if (VOID_TYPE_P (tt1))
6749 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6750 && TREE_CODE (tt0) == FUNCTION_TYPE)
6751 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6753 else
6754 pedwarn ("comparison of distinct pointer types lacks a cast");
6756 if (result_type == NULL_TREE)
6757 result_type = ptr_type_node;
6759 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6760 && integer_zerop (op1))
6761 result_type = type0;
6762 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6763 && integer_zerop (op0))
6764 result_type = type1;
6765 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6767 result_type = type0;
6768 pedwarn ("comparison between pointer and integer");
6770 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6772 result_type = type1;
6773 pedwarn ("comparison between pointer and integer");
6775 break;
6777 case MAX_EXPR:
6778 case MIN_EXPR:
6779 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6780 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6781 shorten = 1;
6782 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6784 if (comp_target_types (type0, type1, 1))
6786 result_type = common_type (type0, type1);
6787 if (pedantic
6788 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6789 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6791 else
6793 result_type = ptr_type_node;
6794 pedwarn ("comparison of distinct pointer types lacks a cast");
6797 break;
6799 case LE_EXPR:
6800 case GE_EXPR:
6801 case LT_EXPR:
6802 case GT_EXPR:
6803 build_type = integer_type_node;
6804 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6805 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6806 short_compare = 1;
6807 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6809 if (comp_target_types (type0, type1, 1))
6811 result_type = common_type (type0, type1);
6812 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6813 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6814 pedwarn ("comparison of complete and incomplete pointers");
6815 else if (pedantic
6816 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6817 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6819 else
6821 result_type = ptr_type_node;
6822 pedwarn ("comparison of distinct pointer types lacks a cast");
6825 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6826 && integer_zerop (op1))
6828 result_type = type0;
6829 if (pedantic || extra_warnings)
6830 pedwarn ("ordered comparison of pointer with integer zero");
6832 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6833 && integer_zerop (op0))
6835 result_type = type1;
6836 if (pedantic)
6837 pedwarn ("ordered comparison of pointer with integer zero");
6839 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6841 result_type = type0;
6842 pedwarn ("comparison between pointer and integer");
6844 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6846 result_type = type1;
6847 pedwarn ("comparison between pointer and integer");
6849 break;
6851 case UNORDERED_EXPR:
6852 case ORDERED_EXPR:
6853 case UNLT_EXPR:
6854 case UNLE_EXPR:
6855 case UNGT_EXPR:
6856 case UNGE_EXPR:
6857 case UNEQ_EXPR:
6858 build_type = integer_type_node;
6859 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6861 error ("unordered comparison on non-floating point argument");
6862 return error_mark_node;
6864 common = 1;
6865 break;
6867 default:
6868 break;
6871 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6872 || code0 == VECTOR_TYPE)
6874 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6875 || code1 == VECTOR_TYPE))
6877 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6879 if (shorten || common || short_compare)
6880 result_type = common_type (type0, type1);
6882 /* For certain operations (which identify themselves by shorten != 0)
6883 if both args were extended from the same smaller type,
6884 do the arithmetic in that type and then extend.
6886 shorten !=0 and !=1 indicates a bitwise operation.
6887 For them, this optimization is safe only if
6888 both args are zero-extended or both are sign-extended.
6889 Otherwise, we might change the result.
6890 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6891 but calculated in (unsigned short) it would be (unsigned short)-1. */
6893 if (shorten && none_complex)
6895 int unsigned0, unsigned1;
6896 tree arg0 = get_narrower (op0, &unsigned0);
6897 tree arg1 = get_narrower (op1, &unsigned1);
6898 /* UNS is 1 if the operation to be done is an unsigned one. */
6899 int uns = TREE_UNSIGNED (result_type);
6900 tree type;
6902 final_type = result_type;
6904 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6905 but it *requires* conversion to FINAL_TYPE. */
6907 if ((TYPE_PRECISION (TREE_TYPE (op0))
6908 == TYPE_PRECISION (TREE_TYPE (arg0)))
6909 && TREE_TYPE (op0) != final_type)
6910 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6911 if ((TYPE_PRECISION (TREE_TYPE (op1))
6912 == TYPE_PRECISION (TREE_TYPE (arg1)))
6913 && TREE_TYPE (op1) != final_type)
6914 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6916 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6918 /* For bitwise operations, signedness of nominal type
6919 does not matter. Consider only how operands were extended. */
6920 if (shorten == -1)
6921 uns = unsigned0;
6923 /* Note that in all three cases below we refrain from optimizing
6924 an unsigned operation on sign-extended args.
6925 That would not be valid. */
6927 /* Both args variable: if both extended in same way
6928 from same width, do it in that width.
6929 Do it unsigned if args were zero-extended. */
6930 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6931 < TYPE_PRECISION (result_type))
6932 && (TYPE_PRECISION (TREE_TYPE (arg1))
6933 == TYPE_PRECISION (TREE_TYPE (arg0)))
6934 && unsigned0 == unsigned1
6935 && (unsigned0 || !uns))
6936 result_type
6937 = c_common_signed_or_unsigned_type
6938 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6939 else if (TREE_CODE (arg0) == INTEGER_CST
6940 && (unsigned1 || !uns)
6941 && (TYPE_PRECISION (TREE_TYPE (arg1))
6942 < TYPE_PRECISION (result_type))
6943 && (type
6944 = c_common_signed_or_unsigned_type (unsigned1,
6945 TREE_TYPE (arg1)),
6946 int_fits_type_p (arg0, type)))
6947 result_type = type;
6948 else if (TREE_CODE (arg1) == INTEGER_CST
6949 && (unsigned0 || !uns)
6950 && (TYPE_PRECISION (TREE_TYPE (arg0))
6951 < TYPE_PRECISION (result_type))
6952 && (type
6953 = c_common_signed_or_unsigned_type (unsigned0,
6954 TREE_TYPE (arg0)),
6955 int_fits_type_p (arg1, type)))
6956 result_type = type;
6959 /* Shifts can be shortened if shifting right. */
6961 if (short_shift)
6963 int unsigned_arg;
6964 tree arg0 = get_narrower (op0, &unsigned_arg);
6966 final_type = result_type;
6968 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6969 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6971 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6972 /* We can shorten only if the shift count is less than the
6973 number of bits in the smaller type size. */
6974 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6975 /* We cannot drop an unsigned shift after sign-extension. */
6976 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6978 /* Do an unsigned shift if the operand was zero-extended. */
6979 result_type
6980 = c_common_signed_or_unsigned_type (unsigned_arg,
6981 TREE_TYPE (arg0));
6982 /* Convert value-to-be-shifted to that type. */
6983 if (TREE_TYPE (op0) != result_type)
6984 op0 = convert (result_type, op0);
6985 converted = 1;
6989 /* Comparison operations are shortened too but differently.
6990 They identify themselves by setting short_compare = 1. */
6992 if (short_compare)
6994 /* Don't write &op0, etc., because that would prevent op0
6995 from being kept in a register.
6996 Instead, make copies of the our local variables and
6997 pass the copies by reference, then copy them back afterward. */
6998 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6999 enum tree_code xresultcode = resultcode;
7000 tree val
7001 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7003 if (val != 0)
7004 return val;
7006 op0 = xop0, op1 = xop1;
7007 converted = 1;
7008 resultcode = xresultcode;
7010 if (warn_sign_compare && skip_evaluation == 0)
7012 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7013 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7014 int unsignedp0, unsignedp1;
7015 tree primop0 = get_narrower (op0, &unsignedp0);
7016 tree primop1 = get_narrower (op1, &unsignedp1);
7018 xop0 = orig_op0;
7019 xop1 = orig_op1;
7020 STRIP_TYPE_NOPS (xop0);
7021 STRIP_TYPE_NOPS (xop1);
7023 /* Give warnings for comparisons between signed and unsigned
7024 quantities that may fail.
7026 Do the checking based on the original operand trees, so that
7027 casts will be considered, but default promotions won't be.
7029 Do not warn if the comparison is being done in a signed type,
7030 since the signed type will only be chosen if it can represent
7031 all the values of the unsigned type. */
7032 if (! TREE_UNSIGNED (result_type))
7033 /* OK */;
7034 /* Do not warn if both operands are the same signedness. */
7035 else if (op0_signed == op1_signed)
7036 /* OK */;
7037 else
7039 tree sop, uop;
7041 if (op0_signed)
7042 sop = xop0, uop = xop1;
7043 else
7044 sop = xop1, uop = xop0;
7046 /* Do not warn if the signed quantity is an
7047 unsuffixed integer literal (or some static
7048 constant expression involving such literals or a
7049 conditional expression involving such literals)
7050 and it is non-negative. */
7051 if (c_tree_expr_nonnegative_p (sop))
7052 /* OK */;
7053 /* Do not warn if the comparison is an equality operation,
7054 the unsigned quantity is an integral constant, and it
7055 would fit in the result if the result were signed. */
7056 else if (TREE_CODE (uop) == INTEGER_CST
7057 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7058 && int_fits_type_p
7059 (uop, c_common_signed_type (result_type)))
7060 /* OK */;
7061 /* Do not warn if the unsigned quantity is an enumeration
7062 constant and its maximum value would fit in the result
7063 if the result were signed. */
7064 else if (TREE_CODE (uop) == INTEGER_CST
7065 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7066 && int_fits_type_p
7067 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7068 c_common_signed_type (result_type)))
7069 /* OK */;
7070 else
7071 warning ("comparison between signed and unsigned");
7074 /* Warn if two unsigned values are being compared in a size
7075 larger than their original size, and one (and only one) is the
7076 result of a `~' operator. This comparison will always fail.
7078 Also warn if one operand is a constant, and the constant
7079 does not have all bits set that are set in the ~ operand
7080 when it is extended. */
7082 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7083 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7085 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7086 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7087 &unsignedp0);
7088 else
7089 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7090 &unsignedp1);
7092 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7094 tree primop;
7095 HOST_WIDE_INT constant, mask;
7096 int unsignedp, bits;
7098 if (host_integerp (primop0, 0))
7100 primop = primop1;
7101 unsignedp = unsignedp1;
7102 constant = tree_low_cst (primop0, 0);
7104 else
7106 primop = primop0;
7107 unsignedp = unsignedp0;
7108 constant = tree_low_cst (primop1, 0);
7111 bits = TYPE_PRECISION (TREE_TYPE (primop));
7112 if (bits < TYPE_PRECISION (result_type)
7113 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7115 mask = (~ (HOST_WIDE_INT) 0) << bits;
7116 if ((mask & constant) != mask)
7117 warning ("comparison of promoted ~unsigned with constant");
7120 else if (unsignedp0 && unsignedp1
7121 && (TYPE_PRECISION (TREE_TYPE (primop0))
7122 < TYPE_PRECISION (result_type))
7123 && (TYPE_PRECISION (TREE_TYPE (primop1))
7124 < TYPE_PRECISION (result_type)))
7125 warning ("comparison of promoted ~unsigned with unsigned");
7131 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7132 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7133 Then the expression will be built.
7134 It will be given type FINAL_TYPE if that is nonzero;
7135 otherwise, it will be given type RESULT_TYPE. */
7137 if (!result_type)
7139 binary_op_error (code);
7140 return error_mark_node;
7143 if (! converted)
7145 if (TREE_TYPE (op0) != result_type)
7146 op0 = convert (result_type, op0);
7147 if (TREE_TYPE (op1) != result_type)
7148 op1 = convert (result_type, op1);
7151 if (build_type == NULL_TREE)
7152 build_type = result_type;
7155 tree result = build (resultcode, build_type, op0, op1);
7156 tree folded;
7158 /* Treat expressions in initializers specially as they can't trap. */
7159 folded = initializer_stack ? fold_initializer (result)
7160 : fold (result);
7161 if (folded == result)
7162 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7163 if (final_type != 0)
7164 return convert (final_type, folded);
7165 return folded;