* fixinc/inclhack.def (avoid_bool_define, avoid_bool_type): Bypass
[official-gcc.git] / gcc / c-typeck.c
blob7564660cd012094dea9c8e66b949c4079b3b42c3
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 /* 1 if we explained undeclared var errors. */
53 static int undeclared_variable_notice;
55 static tree qualify_type (tree, tree);
56 static int tagged_types_tu_compatible_p (tree, tree, int);
57 static int comp_target_types (tree, tree, int);
58 static int function_types_compatible_p (tree, tree, int);
59 static int type_lists_compatible_p (tree, tree, int);
60 static tree decl_constant_value_for_broken_optimization (tree);
61 static tree default_function_array_conversion (tree);
62 static tree lookup_field (tree, tree);
63 static void undeclared_variable (tree);
64 static tree convert_arguments (tree, tree, tree, tree);
65 static tree pointer_diff (tree, tree);
66 static tree unary_complex_lvalue (enum tree_code, tree, int);
67 static void pedantic_lvalue_warning (enum tree_code);
68 static tree internal_build_compound_expr (tree, int);
69 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
70 int);
71 static void warn_for_assignment (const char *, const char *, tree, int);
72 static tree valid_compound_expr_initializer (tree, tree);
73 static void push_string (const char *);
74 static void push_member_name (tree);
75 static void push_array_bounds (int);
76 static int spelling_length (void);
77 static char *print_spelling (char *);
78 static void warning_init (const char *);
79 static tree digest_init (tree, tree, int);
80 static void output_init_element (tree, tree, tree, int);
81 static void output_pending_init_elements (int);
82 static int set_designator (int);
83 static void push_range_stack (tree);
84 static void add_pending_init (tree, tree);
85 static void set_nonincremental_init (void);
86 static void set_nonincremental_init_from_string (tree);
87 static tree find_init_member (tree);
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.) */
92 tree
93 require_complete_type (tree value)
95 tree type = TREE_TYPE (value);
97 if (value == error_mark_node || type == error_mark_node)
98 return error_mark_node;
100 /* First, detect a valid value with a complete type. */
101 if (COMPLETE_TYPE_P (type))
102 return value;
104 c_incomplete_type_error (value, type);
105 return error_mark_node;
108 /* Print an error message for invalid use of an incomplete type.
109 VALUE is the expression that was used (or 0 if that isn't known)
110 and TYPE is the type that was invalid. */
112 void
113 c_incomplete_type_error (tree value, tree type)
115 const char *type_code_string;
117 /* Avoid duplicate error message. */
118 if (TREE_CODE (type) == ERROR_MARK)
119 return;
121 if (value != 0 && (TREE_CODE (value) == VAR_DECL
122 || TREE_CODE (value) == PARM_DECL))
123 error ("`%s' has an incomplete type",
124 IDENTIFIER_POINTER (DECL_NAME (value)));
125 else
127 retry:
128 /* We must print an error message. Be clever about what it says. */
130 switch (TREE_CODE (type))
132 case RECORD_TYPE:
133 type_code_string = "struct";
134 break;
136 case UNION_TYPE:
137 type_code_string = "union";
138 break;
140 case ENUMERAL_TYPE:
141 type_code_string = "enum";
142 break;
144 case VOID_TYPE:
145 error ("invalid use of void expression");
146 return;
148 case ARRAY_TYPE:
149 if (TYPE_DOMAIN (type))
151 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
153 error ("invalid use of flexible array member");
154 return;
156 type = TREE_TYPE (type);
157 goto retry;
159 error ("invalid use of array with unspecified bounds");
160 return;
162 default:
163 abort ();
166 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
167 error ("invalid use of undefined type `%s %s'",
168 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
169 else
170 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
171 error ("invalid use of incomplete typedef `%s'",
172 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
176 /* Given a type, apply default promotions wrt unnamed function
177 arguments and return the new type. */
179 tree
180 c_type_promotes_to (tree type)
182 if (TYPE_MAIN_VARIANT (type) == float_type_node)
183 return double_type_node;
185 if (c_promoting_integer_type_p (type))
187 /* Preserve unsignedness if not really getting any wider. */
188 if (TREE_UNSIGNED (type)
189 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
190 return unsigned_type_node;
191 return integer_type_node;
194 return type;
197 /* Return a variant of TYPE which has all the type qualifiers of LIKE
198 as well as those of TYPE. */
200 static tree
201 qualify_type (tree type, tree like)
203 return c_build_qualified_type (type,
204 TYPE_QUALS (type) | TYPE_QUALS (like));
207 /* Return the common type of two types.
208 We assume that comptypes has already been done and returned 1;
209 if that isn't so, this may crash. In particular, we assume that qualifiers
210 match.
212 This is the type for the result of most arithmetic operations
213 if the operands have the given two types. */
215 tree
216 common_type (tree t1, tree t2)
218 enum tree_code code1;
219 enum tree_code code2;
220 tree attributes;
222 /* Save time if the two types are the same. */
224 if (t1 == t2) return t1;
226 /* If one type is nonsense, use the other. */
227 if (t1 == error_mark_node)
228 return t2;
229 if (t2 == error_mark_node)
230 return t1;
232 /* Merge the attributes. */
233 attributes = (*targetm.merge_type_attributes) (t1, t2);
235 /* Treat an enum type as the unsigned integer type of the same width. */
237 if (TREE_CODE (t1) == ENUMERAL_TYPE)
238 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
239 if (TREE_CODE (t2) == ENUMERAL_TYPE)
240 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
242 code1 = TREE_CODE (t1);
243 code2 = TREE_CODE (t2);
245 /* If one type is complex, form the common type of the non-complex
246 components, then make that complex. Use T1 or T2 if it is the
247 required type. */
248 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
250 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
251 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
252 tree subtype = common_type (subtype1, subtype2);
254 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
255 return build_type_attribute_variant (t1, attributes);
256 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
257 return build_type_attribute_variant (t2, attributes);
258 else
259 return build_type_attribute_variant (build_complex_type (subtype),
260 attributes);
263 switch (code1)
265 case INTEGER_TYPE:
266 case REAL_TYPE:
267 /* If only one is real, use it as the result. */
269 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
270 return build_type_attribute_variant (t1, attributes);
272 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
273 return build_type_attribute_variant (t2, attributes);
275 /* Both real or both integers; use the one with greater precision. */
277 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
278 return build_type_attribute_variant (t1, attributes);
279 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
280 return build_type_attribute_variant (t2, attributes);
282 /* Same precision. Prefer longs to ints even when same size. */
284 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
286 return build_type_attribute_variant (long_unsigned_type_node,
287 attributes);
289 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
290 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
292 /* But preserve unsignedness from the other type,
293 since long cannot hold all the values of an unsigned int. */
294 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
295 t1 = long_unsigned_type_node;
296 else
297 t1 = long_integer_type_node;
298 return build_type_attribute_variant (t1, attributes);
301 /* Likewise, prefer long double to double even if same size. */
302 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
303 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
304 return build_type_attribute_variant (long_double_type_node,
305 attributes);
307 /* Otherwise prefer the unsigned one. */
309 if (TREE_UNSIGNED (t1))
310 return build_type_attribute_variant (t1, attributes);
311 else
312 return build_type_attribute_variant (t2, attributes);
314 case POINTER_TYPE:
315 /* For two pointers, do this recursively on the target type,
316 and combine the qualifiers of the two types' targets. */
317 /* This code was turned off; I don't know why.
318 But ANSI C specifies doing this with the qualifiers.
319 So I turned it on again. */
321 tree pointed_to_1 = TREE_TYPE (t1);
322 tree pointed_to_2 = TREE_TYPE (t2);
323 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
324 TYPE_MAIN_VARIANT (pointed_to_2));
325 t1 = build_pointer_type (c_build_qualified_type
326 (target,
327 TYPE_QUALS (pointed_to_1) |
328 TYPE_QUALS (pointed_to_2)));
329 return build_type_attribute_variant (t1, attributes);
332 case ARRAY_TYPE:
334 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
335 /* Save space: see if the result is identical to one of the args. */
336 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
337 return build_type_attribute_variant (t1, attributes);
338 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
339 return build_type_attribute_variant (t2, attributes);
340 /* Merge the element types, and have a size if either arg has one. */
341 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
342 return build_type_attribute_variant (t1, attributes);
345 case FUNCTION_TYPE:
346 /* Function types: prefer the one that specified arg types.
347 If both do, merge the arg types. Also merge the return types. */
349 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
350 tree p1 = TYPE_ARG_TYPES (t1);
351 tree p2 = TYPE_ARG_TYPES (t2);
352 int len;
353 tree newargs, n;
354 int i;
356 /* Save space: see if the result is identical to one of the args. */
357 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
358 return build_type_attribute_variant (t1, attributes);
359 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
360 return build_type_attribute_variant (t2, attributes);
362 /* Simple way if one arg fails to specify argument types. */
363 if (TYPE_ARG_TYPES (t1) == 0)
365 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
366 return build_type_attribute_variant (t1, attributes);
368 if (TYPE_ARG_TYPES (t2) == 0)
370 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
371 return build_type_attribute_variant (t1, attributes);
374 /* If both args specify argument types, we must merge the two
375 lists, argument by argument. */
377 pushlevel (0);
378 declare_parm_level (1);
380 len = list_length (p1);
381 newargs = 0;
383 for (i = 0; i < len; i++)
384 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
386 n = newargs;
388 for (; p1;
389 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
391 /* A null type means arg type is not specified.
392 Take whatever the other function type has. */
393 if (TREE_VALUE (p1) == 0)
395 TREE_VALUE (n) = TREE_VALUE (p2);
396 goto parm_done;
398 if (TREE_VALUE (p2) == 0)
400 TREE_VALUE (n) = TREE_VALUE (p1);
401 goto parm_done;
404 /* Given wait (union {union wait *u; int *i} *)
405 and wait (union wait *),
406 prefer union wait * as type of parm. */
407 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
408 && TREE_VALUE (p1) != TREE_VALUE (p2))
410 tree memb;
411 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
412 memb; memb = TREE_CHAIN (memb))
413 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
414 COMPARE_STRICT))
416 TREE_VALUE (n) = TREE_VALUE (p2);
417 if (pedantic)
418 pedwarn ("function types not truly compatible in ISO C");
419 goto parm_done;
422 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
423 && TREE_VALUE (p2) != TREE_VALUE (p1))
425 tree memb;
426 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
427 memb; memb = TREE_CHAIN (memb))
428 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
429 COMPARE_STRICT))
431 TREE_VALUE (n) = TREE_VALUE (p1);
432 if (pedantic)
433 pedwarn ("function types not truly compatible in ISO C");
434 goto parm_done;
437 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
438 parm_done: ;
441 poplevel (0, 0, 0);
443 t1 = build_function_type (valtype, newargs);
444 /* ... falls through ... */
447 default:
448 return build_type_attribute_variant (t1, attributes);
453 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
454 or various other operations. Return 2 if they are compatible
455 but a warning may be needed if you use them together. */
458 comptypes (tree type1, tree type2, int flags)
460 tree t1 = type1;
461 tree t2 = type2;
462 int attrval, val;
464 /* Suppress errors caused by previously reported errors. */
466 if (t1 == t2 || !t1 || !t2
467 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
468 return 1;
470 /* If either type is the internal version of sizetype, return the
471 language version. */
472 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
473 && TYPE_DOMAIN (t1) != 0)
474 t1 = TYPE_DOMAIN (t1);
476 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
477 && TYPE_DOMAIN (t2) != 0)
478 t2 = TYPE_DOMAIN (t2);
480 /* Treat an enum type as the integer type of the same width and
481 signedness. */
483 if (TREE_CODE (t1) == ENUMERAL_TYPE)
484 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
485 if (TREE_CODE (t2) == ENUMERAL_TYPE)
486 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
488 if (t1 == t2)
489 return 1;
491 /* Different classes of types can't be compatible. */
493 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
495 /* Qualifiers must match. */
497 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
498 return 0;
500 /* Allow for two different type nodes which have essentially the same
501 definition. Note that we already checked for equality of the type
502 qualifiers (just above). */
504 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
505 return 1;
507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
508 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
509 return 0;
511 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
512 val = 0;
514 switch (TREE_CODE (t1))
516 case POINTER_TYPE:
517 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
518 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
519 break;
521 case FUNCTION_TYPE:
522 val = function_types_compatible_p (t1, t2, flags);
523 break;
525 case ARRAY_TYPE:
527 tree d1 = TYPE_DOMAIN (t1);
528 tree d2 = TYPE_DOMAIN (t2);
529 bool d1_variable, d2_variable;
530 bool d1_zero, d2_zero;
531 val = 1;
533 /* Target types must match incl. qualifiers. */
534 if (TREE_TYPE (t1) != TREE_TYPE (t2)
535 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
536 flags)))
537 return 0;
539 /* Sizes must match unless one is missing or variable. */
540 if (d1 == 0 || d2 == 0 || d1 == d2)
541 break;
543 d1_zero = ! TYPE_MAX_VALUE (d1);
544 d2_zero = ! TYPE_MAX_VALUE (d2);
546 d1_variable = (! d1_zero
547 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
548 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
549 d2_variable = (! d2_zero
550 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
551 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
553 if (d1_variable || d2_variable)
554 break;
555 if (d1_zero && d2_zero)
556 break;
557 if (d1_zero || d2_zero
558 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
559 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
560 val = 0;
562 break;
565 case RECORD_TYPE:
566 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
567 val = 1;
569 case ENUMERAL_TYPE:
570 case UNION_TYPE:
571 if (val != 1 && (flags & COMPARE_DIFFERENT_TU))
572 val = tagged_types_tu_compatible_p (t1, t2, flags);
573 break;
575 case VECTOR_TYPE:
576 /* The target might allow certain vector types to be compatible. */
577 val = (*targetm.vector_opaque_p) (t1)
578 || (*targetm.vector_opaque_p) (t2);
579 break;
581 default:
582 break;
584 return attrval == 2 && val == 1 ? 2 : val;
587 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
588 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
589 to 1 or 0 depending if the check of the pointer types is meant to
590 be reflexive or not (typically, assignments are not reflexive,
591 while comparisons are reflexive).
594 static int
595 comp_target_types (tree ttl, tree ttr, int reflexive)
597 int val;
599 /* Give objc_comptypes a crack at letting these types through. */
600 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
601 return val;
603 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
604 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
606 if (val == 2 && pedantic)
607 pedwarn ("types are not quite compatible");
608 return val;
611 /* Subroutines of `comptypes'. */
613 /* The C standard says that two structures in different translation
614 units are compatible with each other only if the types of their
615 fields are compatible (among other things). So, consider two copies
616 of this structure: */
618 struct tagged_tu_seen {
619 const struct tagged_tu_seen * next;
620 tree t1;
621 tree t2;
624 /* Can they be compatible with each other? We choose to break the
625 recursion by allowing those types to be compatible. */
627 static const struct tagged_tu_seen * tagged_tu_seen_base;
629 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
630 compatible. If the two types are not the same (which has been
631 checked earlier), this can only happen when multiple translation
632 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
633 rules. */
635 static int
636 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
638 tree s1, s2;
639 bool needs_warning = false;
641 /* We have to verify that the tags of the types are the same. This
642 is harder than it looks because this may be a typedef, so we have
643 to go look at the original type. It may even be a typedef of a
644 typedef... */
645 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
646 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
648 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
649 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
651 /* C90 didn't have the requirement that the two tags be the same. */
652 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
653 return 0;
655 /* C90 didn't say what happened if one or both of the types were
656 incomplete; we choose to follow C99 rules here, which is that they
657 are compatible. */
658 if (TYPE_SIZE (t1) == NULL
659 || TYPE_SIZE (t2) == NULL)
660 return 1;
663 const struct tagged_tu_seen * tts_i;
664 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
665 if (tts_i->t1 == t1 && tts_i->t2 == t2)
666 return 1;
669 switch (TREE_CODE (t1))
671 case ENUMERAL_TYPE:
673 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
674 return 0;
676 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
678 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
679 if (s2 == NULL
680 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
681 return 0;
683 return 1;
686 case UNION_TYPE:
688 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
689 return 0;
691 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
693 bool ok = false;
694 struct tagged_tu_seen tts;
696 tts.next = tagged_tu_seen_base;
697 tts.t1 = t1;
698 tts.t2 = t2;
699 tagged_tu_seen_base = &tts;
701 if (DECL_NAME (s1) != NULL)
702 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
703 if (DECL_NAME (s1) == DECL_NAME (s2))
705 int result;
706 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
707 if (result == 0)
708 break;
709 if (result == 2)
710 needs_warning = true;
712 if (TREE_CODE (s1) == FIELD_DECL
713 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
714 DECL_FIELD_BIT_OFFSET (s2)) != 1)
715 break;
717 ok = true;
718 break;
720 tagged_tu_seen_base = tts.next;
721 if (! ok)
722 return 0;
724 return needs_warning ? 2 : 1;
727 case RECORD_TYPE:
729 struct tagged_tu_seen tts;
731 tts.next = tagged_tu_seen_base;
732 tts.t1 = t1;
733 tts.t2 = t2;
734 tagged_tu_seen_base = &tts;
736 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
737 s1 && s2;
738 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
740 int result;
741 if (TREE_CODE (s1) != TREE_CODE (s2)
742 || DECL_NAME (s1) != DECL_NAME (s2))
743 break;
744 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
745 if (result == 0)
746 break;
747 if (result == 2)
748 needs_warning = true;
750 if (TREE_CODE (s1) == FIELD_DECL
751 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
752 DECL_FIELD_BIT_OFFSET (s2)) != 1)
753 break;
755 tagged_tu_seen_base = tts.next;
756 if (s1 && s2)
757 return 0;
758 return needs_warning ? 2 : 1;
761 default:
762 abort ();
766 /* Return 1 if two function types F1 and F2 are compatible.
767 If either type specifies no argument types,
768 the other must specify a fixed number of self-promoting arg types.
769 Otherwise, if one type specifies only the number of arguments,
770 the other must specify that number of self-promoting arg types.
771 Otherwise, the argument types must match. */
773 static int
774 function_types_compatible_p (tree f1, tree f2, int flags)
776 tree args1, args2;
777 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
778 int val = 1;
779 int val1;
780 tree ret1, ret2;
782 ret1 = TREE_TYPE (f1);
783 ret2 = TREE_TYPE (f2);
785 /* 'volatile' qualifiers on a function's return type mean the function
786 is noreturn. */
787 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
788 pedwarn ("function return types not compatible due to `volatile'");
789 if (TYPE_VOLATILE (ret1))
790 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
791 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
792 if (TYPE_VOLATILE (ret2))
793 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
794 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
795 val = comptypes (ret1, ret2, flags);
796 if (val == 0)
797 return 0;
799 args1 = TYPE_ARG_TYPES (f1);
800 args2 = TYPE_ARG_TYPES (f2);
802 /* An unspecified parmlist matches any specified parmlist
803 whose argument types don't need default promotions. */
805 if (args1 == 0)
807 if (!self_promoting_args_p (args2))
808 return 0;
809 /* If one of these types comes from a non-prototype fn definition,
810 compare that with the other type's arglist.
811 If they don't match, ask for a warning (but no error). */
812 if (TYPE_ACTUAL_ARG_TYPES (f1)
813 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
814 flags))
815 val = 2;
816 return val;
818 if (args2 == 0)
820 if (!self_promoting_args_p (args1))
821 return 0;
822 if (TYPE_ACTUAL_ARG_TYPES (f2)
823 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
824 flags))
825 val = 2;
826 return val;
829 /* Both types have argument lists: compare them and propagate results. */
830 val1 = type_lists_compatible_p (args1, args2, flags);
831 return val1 != 1 ? val1 : val;
834 /* Check two lists of types for compatibility,
835 returning 0 for incompatible, 1 for compatible,
836 or 2 for compatible with warning. */
838 static int
839 type_lists_compatible_p (tree args1, tree args2, int flags)
841 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
842 int val = 1;
843 int newval = 0;
845 while (1)
847 if (args1 == 0 && args2 == 0)
848 return val;
849 /* If one list is shorter than the other,
850 they fail to match. */
851 if (args1 == 0 || args2 == 0)
852 return 0;
853 /* A null pointer instead of a type
854 means there is supposed to be an argument
855 but nothing is specified about what type it has.
856 So match anything that self-promotes. */
857 if (TREE_VALUE (args1) == 0)
859 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
860 return 0;
862 else if (TREE_VALUE (args2) == 0)
864 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
865 return 0;
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)->elts[0];
1188 field = TYPE_FIELDS (type);
1189 bot = 0;
1190 top = TYPE_LANG_SPECIFIC (type)->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 /* Issue an error message for a reference to an undeclared variable ID,
1535 including a reference to a builtin outside of function-call context.
1536 Arrange to suppress further errors for the same identifier. */
1537 static void
1538 undeclared_variable (tree id)
1540 if (current_function_decl == 0)
1542 error ("`%s' undeclared here (not in a function)",
1543 IDENTIFIER_POINTER (id));
1544 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1546 else
1548 error ("`%s' undeclared (first use in this function)",
1549 IDENTIFIER_POINTER (id));
1551 if (! undeclared_variable_notice)
1553 error ("(Each undeclared identifier is reported only once");
1554 error ("for each function it appears in.)");
1555 undeclared_variable_notice = 1;
1558 /* Set IDENTIFIER_SYMBOL_VALUE (id) to error_mark_node
1559 at function scope. This suppresses further warnings
1560 about this undeclared identifier in this function. */
1561 pushdecl_function_level (error_mark_node, id);
1565 /* Build an external reference to identifier ID. FUN indicates
1566 whether this will be used for a function call. */
1567 tree
1568 build_external_ref (tree id, int fun)
1570 tree ref;
1571 tree decl = lookup_name (id);
1572 tree objc_ivar = lookup_objc_ivar (id);
1574 if (decl && decl != error_mark_node)
1576 /* Properly declared variable or function reference. */
1577 if (!objc_ivar)
1578 ref = decl;
1579 else if (decl != objc_ivar && !C_DECL_FILE_SCOPE (decl))
1581 warning ("local declaration of `%s' hides instance variable",
1582 IDENTIFIER_POINTER (id));
1583 ref = decl;
1585 else
1586 ref = objc_ivar;
1588 else if (objc_ivar)
1589 ref = objc_ivar;
1590 else if (fun)
1591 /* Implicit function declaration. */
1592 ref = implicitly_declare (id);
1593 else if (decl == error_mark_node)
1594 /* Don't complain about something that's already been
1595 complained about. */
1596 return error_mark_node;
1597 else
1599 undeclared_variable (id);
1600 return error_mark_node;
1603 if (TREE_TYPE (ref) == error_mark_node)
1604 return error_mark_node;
1606 if (TREE_DEPRECATED (ref))
1607 warn_deprecated_use (ref);
1609 if (!skip_evaluation)
1610 assemble_external (ref);
1611 TREE_USED (ref) = 1;
1613 if (TREE_CODE (ref) == CONST_DECL)
1615 ref = DECL_INITIAL (ref);
1616 TREE_CONSTANT (ref) = 1;
1618 else if (current_function_decl != 0
1619 && !C_DECL_FILE_SCOPE (current_function_decl)
1620 && (TREE_CODE (ref) == VAR_DECL
1621 || TREE_CODE (ref) == PARM_DECL
1622 || TREE_CODE (ref) == FUNCTION_DECL))
1624 tree context = decl_function_context (ref);
1626 if (context != 0 && context != current_function_decl)
1627 DECL_NONLOCAL (ref) = 1;
1630 return ref;
1633 /* Build a function call to function FUNCTION with parameters PARAMS.
1634 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1635 TREE_VALUE of each node is a parameter-expression.
1636 FUNCTION's data type may be a function type or a pointer-to-function. */
1638 tree
1639 build_function_call (tree function, tree params)
1641 tree fntype, fundecl = 0;
1642 tree coerced_params;
1643 tree name = NULL_TREE, result;
1645 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1646 STRIP_TYPE_NOPS (function);
1648 /* Convert anything with function type to a pointer-to-function. */
1649 if (TREE_CODE (function) == FUNCTION_DECL)
1651 name = DECL_NAME (function);
1653 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1654 (because calling an inline function does not mean the function
1655 needs to be separately compiled). */
1656 fntype = build_type_variant (TREE_TYPE (function),
1657 TREE_READONLY (function),
1658 TREE_THIS_VOLATILE (function));
1659 fundecl = function;
1660 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1662 else
1663 function = default_conversion (function);
1665 fntype = TREE_TYPE (function);
1667 if (TREE_CODE (fntype) == ERROR_MARK)
1668 return error_mark_node;
1670 if (!(TREE_CODE (fntype) == POINTER_TYPE
1671 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1673 error ("called object is not a function");
1674 return error_mark_node;
1677 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1678 current_function_returns_abnormally = 1;
1680 /* fntype now gets the type of function pointed to. */
1681 fntype = TREE_TYPE (fntype);
1683 /* Convert the parameters to the types declared in the
1684 function prototype, or apply default promotions. */
1686 coerced_params
1687 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1689 /* Check that the arguments to the function are valid. */
1691 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1693 /* Recognize certain built-in functions so we can make tree-codes
1694 other than CALL_EXPR. We do this when it enables fold-const.c
1695 to do something useful. */
1697 if (TREE_CODE (function) == ADDR_EXPR
1698 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1699 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1701 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1702 params, coerced_params);
1703 if (result)
1704 return result;
1707 result = build (CALL_EXPR, TREE_TYPE (fntype),
1708 function, coerced_params, NULL_TREE);
1709 TREE_SIDE_EFFECTS (result) = 1;
1710 result = fold (result);
1712 if (VOID_TYPE_P (TREE_TYPE (result)))
1713 return result;
1714 return require_complete_type (result);
1717 /* Convert the argument expressions in the list VALUES
1718 to the types in the list TYPELIST. The result is a list of converted
1719 argument expressions.
1721 If TYPELIST is exhausted, or when an element has NULL as its type,
1722 perform the default conversions.
1724 PARMLIST is the chain of parm decls for the function being called.
1725 It may be 0, if that info is not available.
1726 It is used only for generating error messages.
1728 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1730 This is also where warnings about wrong number of args are generated.
1732 Both VALUES and the returned value are chains of TREE_LIST nodes
1733 with the elements of the list in the TREE_VALUE slots of those nodes. */
1735 static tree
1736 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1738 tree typetail, valtail;
1739 tree result = NULL;
1740 int parmnum;
1742 /* Scan the given expressions and types, producing individual
1743 converted arguments and pushing them on RESULT in reverse order. */
1745 for (valtail = values, typetail = typelist, parmnum = 0;
1746 valtail;
1747 valtail = TREE_CHAIN (valtail), parmnum++)
1749 tree type = typetail ? TREE_VALUE (typetail) : 0;
1750 tree val = TREE_VALUE (valtail);
1752 if (type == void_type_node)
1754 if (name)
1755 error ("too many arguments to function `%s'",
1756 IDENTIFIER_POINTER (name));
1757 else
1758 error ("too many arguments to function");
1759 break;
1762 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1763 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1764 to convert automatically to a pointer. */
1765 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1766 val = TREE_OPERAND (val, 0);
1768 val = default_function_array_conversion (val);
1770 val = require_complete_type (val);
1772 if (type != 0)
1774 /* Formal parm type is specified by a function prototype. */
1775 tree parmval;
1777 if (!COMPLETE_TYPE_P (type))
1779 error ("type of formal parameter %d is incomplete", parmnum + 1);
1780 parmval = val;
1782 else
1784 /* Optionally warn about conversions that
1785 differ from the default conversions. */
1786 if (warn_conversion || warn_traditional)
1788 int formal_prec = TYPE_PRECISION (type);
1790 if (INTEGRAL_TYPE_P (type)
1791 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1792 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1793 if (INTEGRAL_TYPE_P (type)
1794 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1795 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1796 else if (TREE_CODE (type) == COMPLEX_TYPE
1797 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1798 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1799 else if (TREE_CODE (type) == REAL_TYPE
1800 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1801 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1802 else if (TREE_CODE (type) == COMPLEX_TYPE
1803 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1804 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1805 else if (TREE_CODE (type) == REAL_TYPE
1806 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1807 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1808 /* ??? At some point, messages should be written about
1809 conversions between complex types, but that's too messy
1810 to do now. */
1811 else if (TREE_CODE (type) == REAL_TYPE
1812 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1814 /* Warn if any argument is passed as `float',
1815 since without a prototype it would be `double'. */
1816 if (formal_prec == TYPE_PRECISION (float_type_node))
1817 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1819 /* Detect integer changing in width or signedness.
1820 These warnings are only activated with
1821 -Wconversion, not with -Wtraditional. */
1822 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1823 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1825 tree would_have_been = default_conversion (val);
1826 tree type1 = TREE_TYPE (would_have_been);
1828 if (TREE_CODE (type) == ENUMERAL_TYPE
1829 && (TYPE_MAIN_VARIANT (type)
1830 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1831 /* No warning if function asks for enum
1832 and the actual arg is that enum type. */
1834 else if (formal_prec != TYPE_PRECISION (type1))
1835 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1836 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1838 /* Don't complain if the formal parameter type
1839 is an enum, because we can't tell now whether
1840 the value was an enum--even the same enum. */
1841 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1843 else if (TREE_CODE (val) == INTEGER_CST
1844 && int_fits_type_p (val, type))
1845 /* Change in signedness doesn't matter
1846 if a constant value is unaffected. */
1848 /* Likewise for a constant in a NOP_EXPR. */
1849 else if (TREE_CODE (val) == NOP_EXPR
1850 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1851 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1853 /* If the value is extended from a narrower
1854 unsigned type, it doesn't matter whether we
1855 pass it as signed or unsigned; the value
1856 certainly is the same either way. */
1857 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1858 && TREE_UNSIGNED (TREE_TYPE (val)))
1860 else if (TREE_UNSIGNED (type))
1861 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1862 else
1863 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1867 parmval = convert_for_assignment (type, val,
1868 (char *) 0, /* arg passing */
1869 fundecl, name, parmnum + 1);
1871 if (PROMOTE_PROTOTYPES
1872 && INTEGRAL_TYPE_P (type)
1873 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1874 parmval = default_conversion (parmval);
1876 result = tree_cons (NULL_TREE, parmval, result);
1878 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1879 && (TYPE_PRECISION (TREE_TYPE (val))
1880 < TYPE_PRECISION (double_type_node)))
1881 /* Convert `float' to `double'. */
1882 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1883 else
1884 /* Convert `short' and `char' to full-size `int'. */
1885 result = tree_cons (NULL_TREE, default_conversion (val), result);
1887 if (typetail)
1888 typetail = TREE_CHAIN (typetail);
1891 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1893 if (name)
1894 error ("too few arguments to function `%s'",
1895 IDENTIFIER_POINTER (name));
1896 else
1897 error ("too few arguments to function");
1900 return nreverse (result);
1903 /* This is the entry point used by the parser
1904 for binary operators in the input.
1905 In addition to constructing the expression,
1906 we check for operands that were written with other binary operators
1907 in a way that is likely to confuse the user. */
1909 tree
1910 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1912 tree result = build_binary_op (code, arg1, arg2, 1);
1914 char class;
1915 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1916 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1917 enum tree_code code1 = ERROR_MARK;
1918 enum tree_code code2 = ERROR_MARK;
1920 if (TREE_CODE (result) == ERROR_MARK)
1921 return error_mark_node;
1923 if (IS_EXPR_CODE_CLASS (class1))
1924 code1 = C_EXP_ORIGINAL_CODE (arg1);
1925 if (IS_EXPR_CODE_CLASS (class2))
1926 code2 = C_EXP_ORIGINAL_CODE (arg2);
1928 /* Check for cases such as x+y<<z which users are likely
1929 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1930 is cleared to prevent these warnings. */
1931 if (warn_parentheses)
1933 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1935 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1936 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1937 warning ("suggest parentheses around + or - inside shift");
1940 if (code == TRUTH_ORIF_EXPR)
1942 if (code1 == TRUTH_ANDIF_EXPR
1943 || code2 == TRUTH_ANDIF_EXPR)
1944 warning ("suggest parentheses around && within ||");
1947 if (code == BIT_IOR_EXPR)
1949 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1950 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1951 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1952 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1953 warning ("suggest parentheses around arithmetic in operand of |");
1954 /* Check cases like x|y==z */
1955 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1956 warning ("suggest parentheses around comparison in operand of |");
1959 if (code == BIT_XOR_EXPR)
1961 if (code1 == BIT_AND_EXPR
1962 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1963 || code2 == BIT_AND_EXPR
1964 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1965 warning ("suggest parentheses around arithmetic in operand of ^");
1966 /* Check cases like x^y==z */
1967 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1968 warning ("suggest parentheses around comparison in operand of ^");
1971 if (code == BIT_AND_EXPR)
1973 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1974 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1975 warning ("suggest parentheses around + or - in operand of &");
1976 /* Check cases like x&y==z */
1977 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1978 warning ("suggest parentheses around comparison in operand of &");
1982 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1983 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1984 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1985 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1987 unsigned_conversion_warning (result, arg1);
1988 unsigned_conversion_warning (result, arg2);
1989 overflow_warning (result);
1991 class = TREE_CODE_CLASS (TREE_CODE (result));
1993 /* Record the code that was specified in the source,
1994 for the sake of warnings about confusing nesting. */
1995 if (IS_EXPR_CODE_CLASS (class))
1996 C_SET_EXP_ORIGINAL_CODE (result, code);
1997 else
1999 int flag = TREE_CONSTANT (result);
2000 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2001 so that convert_for_assignment wouldn't strip it.
2002 That way, we got warnings for things like p = (1 - 1).
2003 But it turns out we should not get those warnings. */
2004 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2005 C_SET_EXP_ORIGINAL_CODE (result, code);
2006 TREE_CONSTANT (result) = flag;
2009 return result;
2012 /* Build a binary-operation expression without default conversions.
2013 CODE is the kind of expression to build.
2014 This function differs from `build' in several ways:
2015 the data type of the result is computed and recorded in it,
2016 warnings are generated if arg data types are invalid,
2017 special handling for addition and subtraction of pointers is known,
2018 and some optimization is done (operations on narrow ints
2019 are done in the narrower type when that gives the same result).
2020 Constant folding is also done before the result is returned.
2022 Note that the operands will never have enumeral types, or function
2023 or array types, because either they will have the default conversions
2024 performed or they have both just been converted to some other type in which
2025 the arithmetic is to be done. */
2027 tree
2028 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2029 int convert_p)
2031 tree type0, type1;
2032 enum tree_code code0, code1;
2033 tree op0, op1;
2035 /* Expression code to give to the expression when it is built.
2036 Normally this is CODE, which is what the caller asked for,
2037 but in some special cases we change it. */
2038 enum tree_code resultcode = code;
2040 /* Data type in which the computation is to be performed.
2041 In the simplest cases this is the common type of the arguments. */
2042 tree result_type = NULL;
2044 /* Nonzero means operands have already been type-converted
2045 in whatever way is necessary.
2046 Zero means they need to be converted to RESULT_TYPE. */
2047 int converted = 0;
2049 /* Nonzero means create the expression with this type, rather than
2050 RESULT_TYPE. */
2051 tree build_type = 0;
2053 /* Nonzero means after finally constructing the expression
2054 convert it to this type. */
2055 tree final_type = 0;
2057 /* Nonzero if this is an operation like MIN or MAX which can
2058 safely be computed in short if both args are promoted shorts.
2059 Also implies COMMON.
2060 -1 indicates a bitwise operation; this makes a difference
2061 in the exact conditions for when it is safe to do the operation
2062 in a narrower mode. */
2063 int shorten = 0;
2065 /* Nonzero if this is a comparison operation;
2066 if both args are promoted shorts, compare the original shorts.
2067 Also implies COMMON. */
2068 int short_compare = 0;
2070 /* Nonzero if this is a right-shift operation, which can be computed on the
2071 original short and then promoted if the operand is a promoted short. */
2072 int short_shift = 0;
2074 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2075 int common = 0;
2077 if (convert_p)
2079 op0 = default_conversion (orig_op0);
2080 op1 = default_conversion (orig_op1);
2082 else
2084 op0 = orig_op0;
2085 op1 = orig_op1;
2088 type0 = TREE_TYPE (op0);
2089 type1 = TREE_TYPE (op1);
2091 /* The expression codes of the data types of the arguments tell us
2092 whether the arguments are integers, floating, pointers, etc. */
2093 code0 = TREE_CODE (type0);
2094 code1 = TREE_CODE (type1);
2096 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2097 STRIP_TYPE_NOPS (op0);
2098 STRIP_TYPE_NOPS (op1);
2100 /* If an error was already reported for one of the arguments,
2101 avoid reporting another error. */
2103 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2104 return error_mark_node;
2106 switch (code)
2108 case PLUS_EXPR:
2109 /* Handle the pointer + int case. */
2110 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2111 return pointer_int_sum (PLUS_EXPR, op0, op1);
2112 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2113 return pointer_int_sum (PLUS_EXPR, op1, op0);
2114 else
2115 common = 1;
2116 break;
2118 case MINUS_EXPR:
2119 /* Subtraction of two similar pointers.
2120 We must subtract them as integers, then divide by object size. */
2121 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2122 && comp_target_types (type0, type1, 1))
2123 return pointer_diff (op0, op1);
2124 /* Handle pointer minus int. Just like pointer plus int. */
2125 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2126 return pointer_int_sum (MINUS_EXPR, op0, op1);
2127 else
2128 common = 1;
2129 break;
2131 case MULT_EXPR:
2132 common = 1;
2133 break;
2135 case TRUNC_DIV_EXPR:
2136 case CEIL_DIV_EXPR:
2137 case FLOOR_DIV_EXPR:
2138 case ROUND_DIV_EXPR:
2139 case EXACT_DIV_EXPR:
2140 /* Floating point division by zero is a legitimate way to obtain
2141 infinities and NaNs. */
2142 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2143 warning ("division by zero");
2145 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2146 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2147 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2148 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2150 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2151 resultcode = RDIV_EXPR;
2152 else
2153 /* Although it would be tempting to shorten always here, that
2154 loses on some targets, since the modulo instruction is
2155 undefined if the quotient can't be represented in the
2156 computation mode. We shorten only if unsigned or if
2157 dividing by something we know != -1. */
2158 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2159 || (TREE_CODE (op1) == INTEGER_CST
2160 && ! integer_all_onesp (op1)));
2161 common = 1;
2163 break;
2165 case BIT_AND_EXPR:
2166 case BIT_ANDTC_EXPR:
2167 case BIT_IOR_EXPR:
2168 case BIT_XOR_EXPR:
2169 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2170 shorten = -1;
2171 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2172 common = 1;
2173 break;
2175 case TRUNC_MOD_EXPR:
2176 case FLOOR_MOD_EXPR:
2177 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2178 warning ("division by zero");
2180 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2182 /* Although it would be tempting to shorten always here, that loses
2183 on some targets, since the modulo instruction is undefined if the
2184 quotient can't be represented in the computation mode. We shorten
2185 only if unsigned or if dividing by something we know != -1. */
2186 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2187 || (TREE_CODE (op1) == INTEGER_CST
2188 && ! integer_all_onesp (op1)));
2189 common = 1;
2191 break;
2193 case TRUTH_ANDIF_EXPR:
2194 case TRUTH_ORIF_EXPR:
2195 case TRUTH_AND_EXPR:
2196 case TRUTH_OR_EXPR:
2197 case TRUTH_XOR_EXPR:
2198 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2199 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2200 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2201 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2203 /* Result of these operations is always an int,
2204 but that does not mean the operands should be
2205 converted to ints! */
2206 result_type = integer_type_node;
2207 op0 = c_common_truthvalue_conversion (op0);
2208 op1 = c_common_truthvalue_conversion (op1);
2209 converted = 1;
2211 break;
2213 /* Shift operations: result has same type as first operand;
2214 always convert second operand to int.
2215 Also set SHORT_SHIFT if shifting rightward. */
2217 case RSHIFT_EXPR:
2218 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2220 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2222 if (tree_int_cst_sgn (op1) < 0)
2223 warning ("right shift count is negative");
2224 else
2226 if (! integer_zerop (op1))
2227 short_shift = 1;
2229 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2230 warning ("right shift count >= width of type");
2234 /* Use the type of the value to be shifted. */
2235 result_type = type0;
2236 /* Convert the shift-count to an integer, regardless of size
2237 of value being shifted. */
2238 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2239 op1 = convert (integer_type_node, op1);
2240 /* Avoid converting op1 to result_type later. */
2241 converted = 1;
2243 break;
2245 case LSHIFT_EXPR:
2246 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2248 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2250 if (tree_int_cst_sgn (op1) < 0)
2251 warning ("left shift count is negative");
2253 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2254 warning ("left shift count >= width of type");
2257 /* Use the type of the value to be shifted. */
2258 result_type = type0;
2259 /* Convert the shift-count to an integer, regardless of size
2260 of value being shifted. */
2261 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2262 op1 = convert (integer_type_node, op1);
2263 /* Avoid converting op1 to result_type later. */
2264 converted = 1;
2266 break;
2268 case RROTATE_EXPR:
2269 case LROTATE_EXPR:
2270 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2272 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2274 if (tree_int_cst_sgn (op1) < 0)
2275 warning ("shift count is negative");
2276 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2277 warning ("shift count >= width of type");
2280 /* Use the type of the value to be shifted. */
2281 result_type = type0;
2282 /* Convert the shift-count to an integer, regardless of size
2283 of value being shifted. */
2284 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2285 op1 = convert (integer_type_node, op1);
2286 /* Avoid converting op1 to result_type later. */
2287 converted = 1;
2289 break;
2291 case EQ_EXPR:
2292 case NE_EXPR:
2293 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2294 warning ("comparing floating point with == or != is unsafe");
2295 /* Result of comparison is always int,
2296 but don't convert the args to int! */
2297 build_type = integer_type_node;
2298 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2299 || code0 == COMPLEX_TYPE
2300 || code0 == VECTOR_TYPE)
2301 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2302 || code1 == COMPLEX_TYPE
2303 || code1 == VECTOR_TYPE))
2304 short_compare = 1;
2305 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2307 tree tt0 = TREE_TYPE (type0);
2308 tree tt1 = TREE_TYPE (type1);
2309 /* Anything compares with void *. void * compares with anything.
2310 Otherwise, the targets must be compatible
2311 and both must be object or both incomplete. */
2312 if (comp_target_types (type0, type1, 1))
2313 result_type = common_type (type0, type1);
2314 else if (VOID_TYPE_P (tt0))
2316 /* op0 != orig_op0 detects the case of something
2317 whose value is 0 but which isn't a valid null ptr const. */
2318 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2319 && TREE_CODE (tt1) == FUNCTION_TYPE)
2320 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2322 else if (VOID_TYPE_P (tt1))
2324 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2325 && TREE_CODE (tt0) == FUNCTION_TYPE)
2326 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2328 else
2329 pedwarn ("comparison of distinct pointer types lacks a cast");
2331 if (result_type == NULL_TREE)
2332 result_type = ptr_type_node;
2334 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2335 && integer_zerop (op1))
2336 result_type = type0;
2337 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2338 && integer_zerop (op0))
2339 result_type = type1;
2340 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2342 result_type = type0;
2343 pedwarn ("comparison between pointer and integer");
2345 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2347 result_type = type1;
2348 pedwarn ("comparison between pointer and integer");
2350 break;
2352 case MAX_EXPR:
2353 case MIN_EXPR:
2354 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2355 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2356 shorten = 1;
2357 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2359 if (comp_target_types (type0, type1, 1))
2361 result_type = common_type (type0, type1);
2362 if (pedantic
2363 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2364 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2366 else
2368 result_type = ptr_type_node;
2369 pedwarn ("comparison of distinct pointer types lacks a cast");
2372 break;
2374 case LE_EXPR:
2375 case GE_EXPR:
2376 case LT_EXPR:
2377 case GT_EXPR:
2378 build_type = integer_type_node;
2379 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2380 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2381 short_compare = 1;
2382 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2384 if (comp_target_types (type0, type1, 1))
2386 result_type = common_type (type0, type1);
2387 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2388 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2389 pedwarn ("comparison of complete and incomplete pointers");
2390 else if (pedantic
2391 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2392 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2394 else
2396 result_type = ptr_type_node;
2397 pedwarn ("comparison of distinct pointer types lacks a cast");
2400 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2401 && integer_zerop (op1))
2403 result_type = type0;
2404 if (pedantic || extra_warnings)
2405 pedwarn ("ordered comparison of pointer with integer zero");
2407 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2408 && integer_zerop (op0))
2410 result_type = type1;
2411 if (pedantic)
2412 pedwarn ("ordered comparison of pointer with integer zero");
2414 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2416 result_type = type0;
2417 pedwarn ("comparison between pointer and integer");
2419 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2421 result_type = type1;
2422 pedwarn ("comparison between pointer and integer");
2424 break;
2426 case UNORDERED_EXPR:
2427 case ORDERED_EXPR:
2428 case UNLT_EXPR:
2429 case UNLE_EXPR:
2430 case UNGT_EXPR:
2431 case UNGE_EXPR:
2432 case UNEQ_EXPR:
2433 build_type = integer_type_node;
2434 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2436 error ("unordered comparison on non-floating point argument");
2437 return error_mark_node;
2439 common = 1;
2440 break;
2442 default:
2443 break;
2446 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2447 || code0 == VECTOR_TYPE)
2449 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2450 || code1 == VECTOR_TYPE))
2452 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2454 if (shorten || common || short_compare)
2455 result_type = common_type (type0, type1);
2457 /* For certain operations (which identify themselves by shorten != 0)
2458 if both args were extended from the same smaller type,
2459 do the arithmetic in that type and then extend.
2461 shorten !=0 and !=1 indicates a bitwise operation.
2462 For them, this optimization is safe only if
2463 both args are zero-extended or both are sign-extended.
2464 Otherwise, we might change the result.
2465 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2466 but calculated in (unsigned short) it would be (unsigned short)-1. */
2468 if (shorten && none_complex)
2470 int unsigned0, unsigned1;
2471 tree arg0 = get_narrower (op0, &unsigned0);
2472 tree arg1 = get_narrower (op1, &unsigned1);
2473 /* UNS is 1 if the operation to be done is an unsigned one. */
2474 int uns = TREE_UNSIGNED (result_type);
2475 tree type;
2477 final_type = result_type;
2479 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2480 but it *requires* conversion to FINAL_TYPE. */
2482 if ((TYPE_PRECISION (TREE_TYPE (op0))
2483 == TYPE_PRECISION (TREE_TYPE (arg0)))
2484 && TREE_TYPE (op0) != final_type)
2485 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2486 if ((TYPE_PRECISION (TREE_TYPE (op1))
2487 == TYPE_PRECISION (TREE_TYPE (arg1)))
2488 && TREE_TYPE (op1) != final_type)
2489 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2491 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2493 /* For bitwise operations, signedness of nominal type
2494 does not matter. Consider only how operands were extended. */
2495 if (shorten == -1)
2496 uns = unsigned0;
2498 /* Note that in all three cases below we refrain from optimizing
2499 an unsigned operation on sign-extended args.
2500 That would not be valid. */
2502 /* Both args variable: if both extended in same way
2503 from same width, do it in that width.
2504 Do it unsigned if args were zero-extended. */
2505 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2506 < TYPE_PRECISION (result_type))
2507 && (TYPE_PRECISION (TREE_TYPE (arg1))
2508 == TYPE_PRECISION (TREE_TYPE (arg0)))
2509 && unsigned0 == unsigned1
2510 && (unsigned0 || !uns))
2511 result_type
2512 = c_common_signed_or_unsigned_type
2513 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2514 else if (TREE_CODE (arg0) == INTEGER_CST
2515 && (unsigned1 || !uns)
2516 && (TYPE_PRECISION (TREE_TYPE (arg1))
2517 < TYPE_PRECISION (result_type))
2518 && (type
2519 = c_common_signed_or_unsigned_type (unsigned1,
2520 TREE_TYPE (arg1)),
2521 int_fits_type_p (arg0, type)))
2522 result_type = type;
2523 else if (TREE_CODE (arg1) == INTEGER_CST
2524 && (unsigned0 || !uns)
2525 && (TYPE_PRECISION (TREE_TYPE (arg0))
2526 < TYPE_PRECISION (result_type))
2527 && (type
2528 = c_common_signed_or_unsigned_type (unsigned0,
2529 TREE_TYPE (arg0)),
2530 int_fits_type_p (arg1, type)))
2531 result_type = type;
2534 /* Shifts can be shortened if shifting right. */
2536 if (short_shift)
2538 int unsigned_arg;
2539 tree arg0 = get_narrower (op0, &unsigned_arg);
2541 final_type = result_type;
2543 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2544 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2546 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2547 /* We can shorten only if the shift count is less than the
2548 number of bits in the smaller type size. */
2549 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2550 /* We cannot drop an unsigned shift after sign-extension. */
2551 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2553 /* Do an unsigned shift if the operand was zero-extended. */
2554 result_type
2555 = c_common_signed_or_unsigned_type (unsigned_arg,
2556 TREE_TYPE (arg0));
2557 /* Convert value-to-be-shifted to that type. */
2558 if (TREE_TYPE (op0) != result_type)
2559 op0 = convert (result_type, op0);
2560 converted = 1;
2564 /* Comparison operations are shortened too but differently.
2565 They identify themselves by setting short_compare = 1. */
2567 if (short_compare)
2569 /* Don't write &op0, etc., because that would prevent op0
2570 from being kept in a register.
2571 Instead, make copies of the our local variables and
2572 pass the copies by reference, then copy them back afterward. */
2573 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2574 enum tree_code xresultcode = resultcode;
2575 tree val
2576 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2578 if (val != 0)
2579 return val;
2581 op0 = xop0, op1 = xop1;
2582 converted = 1;
2583 resultcode = xresultcode;
2585 if (warn_sign_compare && skip_evaluation == 0)
2587 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2588 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2589 int unsignedp0, unsignedp1;
2590 tree primop0 = get_narrower (op0, &unsignedp0);
2591 tree primop1 = get_narrower (op1, &unsignedp1);
2593 xop0 = orig_op0;
2594 xop1 = orig_op1;
2595 STRIP_TYPE_NOPS (xop0);
2596 STRIP_TYPE_NOPS (xop1);
2598 /* Give warnings for comparisons between signed and unsigned
2599 quantities that may fail.
2601 Do the checking based on the original operand trees, so that
2602 casts will be considered, but default promotions won't be.
2604 Do not warn if the comparison is being done in a signed type,
2605 since the signed type will only be chosen if it can represent
2606 all the values of the unsigned type. */
2607 if (! TREE_UNSIGNED (result_type))
2608 /* OK */;
2609 /* Do not warn if both operands are the same signedness. */
2610 else if (op0_signed == op1_signed)
2611 /* OK */;
2612 else
2614 tree sop, uop;
2616 if (op0_signed)
2617 sop = xop0, uop = xop1;
2618 else
2619 sop = xop1, uop = xop0;
2621 /* Do not warn if the signed quantity is an
2622 unsuffixed integer literal (or some static
2623 constant expression involving such literals or a
2624 conditional expression involving such literals)
2625 and it is non-negative. */
2626 if (c_tree_expr_nonnegative_p (sop))
2627 /* OK */;
2628 /* Do not warn if the comparison is an equality operation,
2629 the unsigned quantity is an integral constant, and it
2630 would fit in the result if the result were signed. */
2631 else if (TREE_CODE (uop) == INTEGER_CST
2632 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2633 && int_fits_type_p
2634 (uop, c_common_signed_type (result_type)))
2635 /* OK */;
2636 /* Do not warn if the unsigned quantity is an enumeration
2637 constant and its maximum value would fit in the result
2638 if the result were signed. */
2639 else if (TREE_CODE (uop) == INTEGER_CST
2640 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2641 && int_fits_type_p
2642 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2643 c_common_signed_type (result_type)))
2644 /* OK */;
2645 else
2646 warning ("comparison between signed and unsigned");
2649 /* Warn if two unsigned values are being compared in a size
2650 larger than their original size, and one (and only one) is the
2651 result of a `~' operator. This comparison will always fail.
2653 Also warn if one operand is a constant, and the constant
2654 does not have all bits set that are set in the ~ operand
2655 when it is extended. */
2657 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2658 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2660 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2661 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2662 &unsignedp0);
2663 else
2664 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2665 &unsignedp1);
2667 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2669 tree primop;
2670 HOST_WIDE_INT constant, mask;
2671 int unsignedp, bits;
2673 if (host_integerp (primop0, 0))
2675 primop = primop1;
2676 unsignedp = unsignedp1;
2677 constant = tree_low_cst (primop0, 0);
2679 else
2681 primop = primop0;
2682 unsignedp = unsignedp0;
2683 constant = tree_low_cst (primop1, 0);
2686 bits = TYPE_PRECISION (TREE_TYPE (primop));
2687 if (bits < TYPE_PRECISION (result_type)
2688 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2690 mask = (~ (HOST_WIDE_INT) 0) << bits;
2691 if ((mask & constant) != mask)
2692 warning ("comparison of promoted ~unsigned with constant");
2695 else if (unsignedp0 && unsignedp1
2696 && (TYPE_PRECISION (TREE_TYPE (primop0))
2697 < TYPE_PRECISION (result_type))
2698 && (TYPE_PRECISION (TREE_TYPE (primop1))
2699 < TYPE_PRECISION (result_type)))
2700 warning ("comparison of promoted ~unsigned with unsigned");
2706 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2707 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2708 Then the expression will be built.
2709 It will be given type FINAL_TYPE if that is nonzero;
2710 otherwise, it will be given type RESULT_TYPE. */
2712 if (!result_type)
2714 binary_op_error (code);
2715 return error_mark_node;
2718 if (! converted)
2720 if (TREE_TYPE (op0) != result_type)
2721 op0 = convert (result_type, op0);
2722 if (TREE_TYPE (op1) != result_type)
2723 op1 = convert (result_type, op1);
2726 if (build_type == NULL_TREE)
2727 build_type = result_type;
2730 tree result = build (resultcode, build_type, op0, op1);
2731 tree folded;
2733 folded = fold (result);
2734 if (folded == result)
2735 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2736 if (final_type != 0)
2737 return convert (final_type, folded);
2738 return folded;
2743 /* Return true if `t' is known to be non-negative. */
2746 c_tree_expr_nonnegative_p (tree t)
2748 if (TREE_CODE (t) == STMT_EXPR)
2750 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2752 /* Find the last statement in the chain, ignoring the final
2753 * scope statement */
2754 while (TREE_CHAIN (t) != NULL_TREE
2755 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2756 t = TREE_CHAIN (t);
2757 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2759 return tree_expr_nonnegative_p (t);
2762 /* Return a tree for the difference of pointers OP0 and OP1.
2763 The resulting tree has type int. */
2765 static tree
2766 pointer_diff (tree op0, tree op1)
2768 tree result, folded;
2769 tree restype = ptrdiff_type_node;
2771 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2772 tree con0, con1, lit0, lit1;
2773 tree orig_op1 = op1;
2775 if (pedantic || warn_pointer_arith)
2777 if (TREE_CODE (target_type) == VOID_TYPE)
2778 pedwarn ("pointer of type `void *' used in subtraction");
2779 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2780 pedwarn ("pointer to a function used in subtraction");
2783 /* If the conversion to ptrdiff_type does anything like widening or
2784 converting a partial to an integral mode, we get a convert_expression
2785 that is in the way to do any simplifications.
2786 (fold-const.c doesn't know that the extra bits won't be needed.
2787 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2788 different mode in place.)
2789 So first try to find a common term here 'by hand'; we want to cover
2790 at least the cases that occur in legal static initializers. */
2791 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2792 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2794 if (TREE_CODE (con0) == PLUS_EXPR)
2796 lit0 = TREE_OPERAND (con0, 1);
2797 con0 = TREE_OPERAND (con0, 0);
2799 else
2800 lit0 = integer_zero_node;
2802 if (TREE_CODE (con1) == PLUS_EXPR)
2804 lit1 = TREE_OPERAND (con1, 1);
2805 con1 = TREE_OPERAND (con1, 0);
2807 else
2808 lit1 = integer_zero_node;
2810 if (operand_equal_p (con0, con1, 0))
2812 op0 = lit0;
2813 op1 = lit1;
2817 /* First do the subtraction as integers;
2818 then drop through to build the divide operator.
2819 Do not do default conversions on the minus operator
2820 in case restype is a short type. */
2822 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2823 convert (restype, op1), 0);
2824 /* This generates an error if op1 is pointer to incomplete type. */
2825 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2826 error ("arithmetic on pointer to an incomplete type");
2828 /* This generates an error if op0 is pointer to incomplete type. */
2829 op1 = c_size_in_bytes (target_type);
2831 /* Divide by the size, in easiest possible way. */
2833 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2835 folded = fold (result);
2836 if (folded == result)
2837 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2838 return folded;
2841 /* Construct and perhaps optimize a tree representation
2842 for a unary operation. CODE, a tree_code, specifies the operation
2843 and XARG is the operand.
2844 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2845 the default promotions (such as from short to int).
2846 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2847 allows non-lvalues; this is only used to handle conversion of non-lvalue
2848 arrays to pointers in C99. */
2850 tree
2851 build_unary_op (enum tree_code code, tree xarg, int flag)
2853 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2854 tree arg = xarg;
2855 tree argtype = 0;
2856 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2857 tree val;
2858 int noconvert = flag;
2860 if (typecode == ERROR_MARK)
2861 return error_mark_node;
2862 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2863 typecode = INTEGER_TYPE;
2865 switch (code)
2867 case CONVERT_EXPR:
2868 /* This is used for unary plus, because a CONVERT_EXPR
2869 is enough to prevent anybody from looking inside for
2870 associativity, but won't generate any code. */
2871 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2872 || typecode == COMPLEX_TYPE))
2874 error ("wrong type argument to unary plus");
2875 return error_mark_node;
2877 else if (!noconvert)
2878 arg = default_conversion (arg);
2879 arg = non_lvalue (arg);
2880 break;
2882 case NEGATE_EXPR:
2883 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2884 || typecode == COMPLEX_TYPE
2885 || typecode == VECTOR_TYPE))
2887 error ("wrong type argument to unary minus");
2888 return error_mark_node;
2890 else if (!noconvert)
2891 arg = default_conversion (arg);
2892 break;
2894 case BIT_NOT_EXPR:
2895 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2897 if (!noconvert)
2898 arg = default_conversion (arg);
2900 else if (typecode == COMPLEX_TYPE)
2902 code = CONJ_EXPR;
2903 if (pedantic)
2904 pedwarn ("ISO C does not support `~' for complex conjugation");
2905 if (!noconvert)
2906 arg = default_conversion (arg);
2908 else
2910 error ("wrong type argument to bit-complement");
2911 return error_mark_node;
2913 break;
2915 case ABS_EXPR:
2916 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2917 || typecode == COMPLEX_TYPE))
2919 error ("wrong type argument to abs");
2920 return error_mark_node;
2922 else if (!noconvert)
2923 arg = default_conversion (arg);
2924 break;
2926 case CONJ_EXPR:
2927 /* Conjugating a real value is a no-op, but allow it anyway. */
2928 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2929 || typecode == COMPLEX_TYPE))
2931 error ("wrong type argument to conjugation");
2932 return error_mark_node;
2934 else if (!noconvert)
2935 arg = default_conversion (arg);
2936 break;
2938 case TRUTH_NOT_EXPR:
2939 if (typecode != INTEGER_TYPE
2940 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2941 && typecode != COMPLEX_TYPE
2942 /* These will convert to a pointer. */
2943 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2945 error ("wrong type argument to unary exclamation mark");
2946 return error_mark_node;
2948 arg = c_common_truthvalue_conversion (arg);
2949 return invert_truthvalue (arg);
2951 case NOP_EXPR:
2952 break;
2954 case REALPART_EXPR:
2955 if (TREE_CODE (arg) == COMPLEX_CST)
2956 return TREE_REALPART (arg);
2957 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2958 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2959 else
2960 return arg;
2962 case IMAGPART_EXPR:
2963 if (TREE_CODE (arg) == COMPLEX_CST)
2964 return TREE_IMAGPART (arg);
2965 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2966 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2967 else
2968 return convert (TREE_TYPE (arg), integer_zero_node);
2970 case PREINCREMENT_EXPR:
2971 case POSTINCREMENT_EXPR:
2972 case PREDECREMENT_EXPR:
2973 case POSTDECREMENT_EXPR:
2974 /* Handle complex lvalues (when permitted)
2975 by reduction to simpler cases. */
2977 val = unary_complex_lvalue (code, arg, 0);
2978 if (val != 0)
2979 return val;
2981 /* Increment or decrement the real part of the value,
2982 and don't change the imaginary part. */
2983 if (typecode == COMPLEX_TYPE)
2985 tree real, imag;
2987 if (pedantic)
2988 pedwarn ("ISO C does not support `++' and `--' on complex types");
2990 arg = stabilize_reference (arg);
2991 real = build_unary_op (REALPART_EXPR, arg, 1);
2992 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2993 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2994 build_unary_op (code, real, 1), imag);
2997 /* Report invalid types. */
2999 if (typecode != POINTER_TYPE
3000 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3003 error ("wrong type argument to increment");
3004 else
3005 error ("wrong type argument to decrement");
3007 return error_mark_node;
3011 tree inc;
3012 tree result_type = TREE_TYPE (arg);
3014 arg = get_unwidened (arg, 0);
3015 argtype = TREE_TYPE (arg);
3017 /* Compute the increment. */
3019 if (typecode == POINTER_TYPE)
3021 /* If pointer target is an undefined struct,
3022 we just cannot know how to do the arithmetic. */
3023 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
3025 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3026 error ("increment of pointer to unknown structure");
3027 else
3028 error ("decrement of pointer to unknown structure");
3030 else if ((pedantic || warn_pointer_arith)
3031 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3032 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3034 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3035 pedwarn ("wrong type argument to increment");
3036 else
3037 pedwarn ("wrong type argument to decrement");
3040 inc = c_size_in_bytes (TREE_TYPE (result_type));
3042 else
3043 inc = integer_one_node;
3045 inc = convert (argtype, inc);
3047 /* Handle incrementing a cast-expression. */
3049 while (1)
3050 switch (TREE_CODE (arg))
3052 case NOP_EXPR:
3053 case CONVERT_EXPR:
3054 case FLOAT_EXPR:
3055 case FIX_TRUNC_EXPR:
3056 case FIX_FLOOR_EXPR:
3057 case FIX_ROUND_EXPR:
3058 case FIX_CEIL_EXPR:
3059 pedantic_lvalue_warning (CONVERT_EXPR);
3060 /* If the real type has the same machine representation
3061 as the type it is cast to, we can make better output
3062 by adding directly to the inside of the cast. */
3063 if ((TREE_CODE (TREE_TYPE (arg))
3064 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3065 && (TYPE_MODE (TREE_TYPE (arg))
3066 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3067 arg = TREE_OPERAND (arg, 0);
3068 else
3070 tree incremented, modify, value;
3071 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3072 value = boolean_increment (code, arg);
3073 else
3075 arg = stabilize_reference (arg);
3076 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3077 value = arg;
3078 else
3079 value = save_expr (arg);
3080 incremented = build (((code == PREINCREMENT_EXPR
3081 || code == POSTINCREMENT_EXPR)
3082 ? PLUS_EXPR : MINUS_EXPR),
3083 argtype, value, inc);
3084 TREE_SIDE_EFFECTS (incremented) = 1;
3085 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3086 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3088 TREE_USED (value) = 1;
3089 return value;
3091 break;
3093 default:
3094 goto give_up;
3096 give_up:
3098 /* Complain about anything else that is not a true lvalue. */
3099 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3100 || code == POSTINCREMENT_EXPR)
3101 ? "invalid lvalue in increment"
3102 : "invalid lvalue in decrement")))
3103 return error_mark_node;
3105 /* Report a read-only lvalue. */
3106 if (TREE_READONLY (arg))
3107 readonly_warning (arg,
3108 ((code == PREINCREMENT_EXPR
3109 || code == POSTINCREMENT_EXPR)
3110 ? "increment" : "decrement"));
3112 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3113 val = boolean_increment (code, arg);
3114 else
3115 val = build (code, TREE_TYPE (arg), arg, inc);
3116 TREE_SIDE_EFFECTS (val) = 1;
3117 val = convert (result_type, val);
3118 if (TREE_CODE (val) != code)
3119 TREE_NO_UNUSED_WARNING (val) = 1;
3120 return val;
3123 case ADDR_EXPR:
3124 /* Note that this operation never does default_conversion. */
3126 /* Let &* cancel out to simplify resulting code. */
3127 if (TREE_CODE (arg) == INDIRECT_REF)
3129 /* Don't let this be an lvalue. */
3130 if (lvalue_p (TREE_OPERAND (arg, 0)))
3131 return non_lvalue (TREE_OPERAND (arg, 0));
3132 return TREE_OPERAND (arg, 0);
3135 /* For &x[y], return x+y */
3136 if (TREE_CODE (arg) == ARRAY_REF)
3138 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
3139 return error_mark_node;
3140 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3141 TREE_OPERAND (arg, 1), 1);
3144 /* Handle complex lvalues (when permitted)
3145 by reduction to simpler cases. */
3146 val = unary_complex_lvalue (code, arg, flag);
3147 if (val != 0)
3148 return val;
3150 /* Anything not already handled and not a true memory reference
3151 or a non-lvalue array is an error. */
3152 else if (typecode != FUNCTION_TYPE && !flag
3153 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3154 return error_mark_node;
3156 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3157 argtype = TREE_TYPE (arg);
3159 /* If the lvalue is const or volatile, merge that into the type
3160 to which the address will point. Note that you can't get a
3161 restricted pointer by taking the address of something, so we
3162 only have to deal with `const' and `volatile' here. */
3163 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3164 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3165 argtype = c_build_type_variant (argtype,
3166 TREE_READONLY (arg),
3167 TREE_THIS_VOLATILE (arg));
3169 argtype = build_pointer_type (argtype);
3171 if (!c_mark_addressable (arg))
3172 return error_mark_node;
3175 tree addr;
3177 if (TREE_CODE (arg) == COMPONENT_REF)
3179 tree field = TREE_OPERAND (arg, 1);
3181 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3183 if (DECL_C_BIT_FIELD (field))
3185 error ("attempt to take address of bit-field structure member `%s'",
3186 IDENTIFIER_POINTER (DECL_NAME (field)));
3187 return error_mark_node;
3190 addr = fold (build (PLUS_EXPR, argtype,
3191 convert (argtype, addr),
3192 convert (argtype, byte_position (field))));
3194 else
3195 addr = build1 (code, argtype, arg);
3197 /* Address of a static or external variable or
3198 file-scope function counts as a constant. */
3199 if (staticp (arg)
3200 && ! (TREE_CODE (arg) == FUNCTION_DECL
3201 && !C_DECL_FILE_SCOPE (arg)))
3202 TREE_CONSTANT (addr) = 1;
3203 return addr;
3206 default:
3207 break;
3210 if (argtype == 0)
3211 argtype = TREE_TYPE (arg);
3212 return fold (build1 (code, argtype, arg));
3215 /* Return nonzero if REF is an lvalue valid for this language.
3216 Lvalues can be assigned, unless their type has TYPE_READONLY.
3217 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3220 lvalue_p (tree ref)
3222 enum tree_code code = TREE_CODE (ref);
3224 switch (code)
3226 case REALPART_EXPR:
3227 case IMAGPART_EXPR:
3228 case COMPONENT_REF:
3229 return lvalue_p (TREE_OPERAND (ref, 0));
3231 case COMPOUND_LITERAL_EXPR:
3232 case STRING_CST:
3233 return 1;
3235 case INDIRECT_REF:
3236 case ARRAY_REF:
3237 case VAR_DECL:
3238 case PARM_DECL:
3239 case RESULT_DECL:
3240 case ERROR_MARK:
3241 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3242 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3244 case BIND_EXPR:
3245 case RTL_EXPR:
3246 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3248 default:
3249 return 0;
3253 /* Return nonzero if REF is an lvalue valid for this language;
3254 otherwise, print an error message and return zero. */
3257 lvalue_or_else (tree ref, const char *msgid)
3259 int win = lvalue_p (ref);
3261 if (! win)
3262 error ("%s", msgid);
3264 return win;
3267 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3268 for certain kinds of expressions which are not really lvalues
3269 but which we can accept as lvalues. If FLAG is nonzero, then
3270 non-lvalues are OK since we may be converting a non-lvalue array to
3271 a pointer in C99.
3273 If ARG is not a kind of expression we can handle, return zero. */
3275 static tree
3276 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
3278 /* Handle (a, b) used as an "lvalue". */
3279 if (TREE_CODE (arg) == COMPOUND_EXPR)
3281 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3283 /* If this returns a function type, it isn't really being used as
3284 an lvalue, so don't issue a warning about it. */
3285 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3286 pedantic_lvalue_warning (COMPOUND_EXPR);
3288 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3289 TREE_OPERAND (arg, 0), real_result);
3292 /* Handle (a ? b : c) used as an "lvalue". */
3293 if (TREE_CODE (arg) == COND_EXPR)
3295 if (!flag)
3296 pedantic_lvalue_warning (COND_EXPR);
3297 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3298 pedantic_lvalue_warning (COMPOUND_EXPR);
3300 return (build_conditional_expr
3301 (TREE_OPERAND (arg, 0),
3302 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3303 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3306 return 0;
3309 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3310 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3312 static void
3313 pedantic_lvalue_warning (enum tree_code code)
3315 if (pedantic)
3316 switch (code)
3318 case COND_EXPR:
3319 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3320 break;
3321 case COMPOUND_EXPR:
3322 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3323 break;
3324 default:
3325 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3326 break;
3330 /* Warn about storing in something that is `const'. */
3332 void
3333 readonly_warning (tree arg, const char *msgid)
3335 if (TREE_CODE (arg) == COMPONENT_REF)
3337 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3338 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3339 else
3340 pedwarn ("%s of read-only member `%s'", _(msgid),
3341 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3343 else if (TREE_CODE (arg) == VAR_DECL)
3344 pedwarn ("%s of read-only variable `%s'", _(msgid),
3345 IDENTIFIER_POINTER (DECL_NAME (arg)));
3346 else
3347 pedwarn ("%s of read-only location", _(msgid));
3350 /* Mark EXP saying that we need to be able to take the
3351 address of it; it should not be allocated in a register.
3352 Returns true if successful. */
3354 bool
3355 c_mark_addressable (tree exp)
3357 tree x = exp;
3359 while (1)
3360 switch (TREE_CODE (x))
3362 case COMPONENT_REF:
3363 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3365 error ("cannot take address of bit-field `%s'",
3366 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3367 return false;
3370 /* ... fall through ... */
3372 case ADDR_EXPR:
3373 case ARRAY_REF:
3374 case REALPART_EXPR:
3375 case IMAGPART_EXPR:
3376 x = TREE_OPERAND (x, 0);
3377 break;
3379 case COMPOUND_LITERAL_EXPR:
3380 case CONSTRUCTOR:
3381 TREE_ADDRESSABLE (x) = 1;
3382 return true;
3384 case VAR_DECL:
3385 case CONST_DECL:
3386 case PARM_DECL:
3387 case RESULT_DECL:
3388 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3389 && DECL_NONLOCAL (x))
3391 if (TREE_PUBLIC (x))
3393 error ("global register variable `%s' used in nested function",
3394 IDENTIFIER_POINTER (DECL_NAME (x)));
3395 return false;
3397 pedwarn ("register variable `%s' used in nested function",
3398 IDENTIFIER_POINTER (DECL_NAME (x)));
3400 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3402 if (TREE_PUBLIC (x))
3404 error ("address of global register variable `%s' requested",
3405 IDENTIFIER_POINTER (DECL_NAME (x)));
3406 return false;
3409 /* If we are making this addressable due to its having
3410 volatile components, give a different error message. Also
3411 handle the case of an unnamed parameter by not trying
3412 to give the name. */
3414 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3416 error ("cannot put object with volatile field into register");
3417 return false;
3420 pedwarn ("address of register variable `%s' requested",
3421 IDENTIFIER_POINTER (DECL_NAME (x)));
3423 put_var_into_stack (x, /*rescan=*/true);
3425 /* drops in */
3426 case FUNCTION_DECL:
3427 TREE_ADDRESSABLE (x) = 1;
3428 /* drops out */
3429 default:
3430 return true;
3434 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3436 tree
3437 build_conditional_expr (tree ifexp, tree op1, tree op2)
3439 tree type1;
3440 tree type2;
3441 enum tree_code code1;
3442 enum tree_code code2;
3443 tree result_type = NULL;
3444 tree orig_op1 = op1, orig_op2 = op2;
3446 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3448 /* Promote both alternatives. */
3450 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3451 op1 = default_conversion (op1);
3452 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3453 op2 = default_conversion (op2);
3455 if (TREE_CODE (ifexp) == ERROR_MARK
3456 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3457 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3458 return error_mark_node;
3460 type1 = TREE_TYPE (op1);
3461 code1 = TREE_CODE (type1);
3462 type2 = TREE_TYPE (op2);
3463 code2 = TREE_CODE (type2);
3465 /* Quickly detect the usual case where op1 and op2 have the same type
3466 after promotion. */
3467 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3469 if (type1 == type2)
3470 result_type = type1;
3471 else
3472 result_type = TYPE_MAIN_VARIANT (type1);
3474 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3475 || code1 == COMPLEX_TYPE)
3476 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3477 || code2 == COMPLEX_TYPE))
3479 result_type = common_type (type1, type2);
3481 /* If -Wsign-compare, warn here if type1 and type2 have
3482 different signedness. We'll promote the signed to unsigned
3483 and later code won't know it used to be different.
3484 Do this check on the original types, so that explicit casts
3485 will be considered, but default promotions won't. */
3486 if (warn_sign_compare && !skip_evaluation)
3488 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3489 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3491 if (unsigned_op1 ^ unsigned_op2)
3493 /* Do not warn if the result type is signed, since the
3494 signed type will only be chosen if it can represent
3495 all the values of the unsigned type. */
3496 if (! TREE_UNSIGNED (result_type))
3497 /* OK */;
3498 /* Do not warn if the signed quantity is an unsuffixed
3499 integer literal (or some static constant expression
3500 involving such literals) and it is non-negative. */
3501 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3502 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3503 /* OK */;
3504 else
3505 warning ("signed and unsigned type in conditional expression");
3509 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3511 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3512 pedwarn ("ISO C forbids conditional expr with only one void side");
3513 result_type = void_type_node;
3515 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3517 if (comp_target_types (type1, type2, 1))
3518 result_type = common_type (type1, type2);
3519 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3520 && TREE_CODE (orig_op1) != NOP_EXPR)
3521 result_type = qualify_type (type2, type1);
3522 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3523 && TREE_CODE (orig_op2) != NOP_EXPR)
3524 result_type = qualify_type (type1, type2);
3525 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3527 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3528 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3529 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3530 TREE_TYPE (type2)));
3532 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3534 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3535 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3536 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3537 TREE_TYPE (type1)));
3539 else
3541 pedwarn ("pointer type mismatch in conditional expression");
3542 result_type = build_pointer_type (void_type_node);
3545 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3547 if (! integer_zerop (op2))
3548 pedwarn ("pointer/integer type mismatch in conditional expression");
3549 else
3551 op2 = null_pointer_node;
3553 result_type = type1;
3555 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3557 if (!integer_zerop (op1))
3558 pedwarn ("pointer/integer type mismatch in conditional expression");
3559 else
3561 op1 = null_pointer_node;
3563 result_type = type2;
3566 if (!result_type)
3568 if (flag_cond_mismatch)
3569 result_type = void_type_node;
3570 else
3572 error ("type mismatch in conditional expression");
3573 return error_mark_node;
3577 /* Merge const and volatile flags of the incoming types. */
3578 result_type
3579 = build_type_variant (result_type,
3580 TREE_READONLY (op1) || TREE_READONLY (op2),
3581 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3583 if (result_type != TREE_TYPE (op1))
3584 op1 = convert_and_check (result_type, op1);
3585 if (result_type != TREE_TYPE (op2))
3586 op2 = convert_and_check (result_type, op2);
3588 if (TREE_CODE (ifexp) == INTEGER_CST)
3589 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3591 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3594 /* Given a list of expressions, return a compound expression
3595 that performs them all and returns the value of the last of them. */
3597 tree
3598 build_compound_expr (tree list)
3600 return internal_build_compound_expr (list, TRUE);
3603 static tree
3604 internal_build_compound_expr (tree list, int first_p)
3606 tree rest;
3608 if (TREE_CHAIN (list) == 0)
3610 /* Convert arrays and functions to pointers when there
3611 really is a comma operator. */
3612 if (!first_p)
3613 TREE_VALUE (list)
3614 = default_function_array_conversion (TREE_VALUE (list));
3616 /* Don't let (0, 0) be null pointer constant. */
3617 if (!first_p && integer_zerop (TREE_VALUE (list)))
3618 return non_lvalue (TREE_VALUE (list));
3619 return TREE_VALUE (list);
3622 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3624 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3626 /* The left-hand operand of a comma expression is like an expression
3627 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3628 any side-effects, unless it was explicitly cast to (void). */
3629 if (warn_unused_value
3630 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3631 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3632 warning ("left-hand operand of comma expression has no effect");
3634 /* When pedantic, a compound expression can be neither an lvalue
3635 nor an integer constant expression. */
3636 if (! pedantic)
3637 return rest;
3640 /* With -Wunused, we should also warn if the left-hand operand does have
3641 side-effects, but computes a value which is not used. For example, in
3642 `foo() + bar(), baz()' the result of the `+' operator is not used,
3643 so we should issue a warning. */
3644 else if (warn_unused_value)
3645 warn_if_unused_value (TREE_VALUE (list));
3647 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3650 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3652 tree
3653 build_c_cast (tree type, tree expr)
3655 tree value = expr;
3657 if (type == error_mark_node || expr == error_mark_node)
3658 return error_mark_node;
3660 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3661 only in <protocol> qualifications. But when constructing cast expressions,
3662 the protocols do matter and must be kept around. */
3663 if (!c_dialect_objc () || !objc_is_id (type))
3664 type = TYPE_MAIN_VARIANT (type);
3666 if (TREE_CODE (type) == ARRAY_TYPE)
3668 error ("cast specifies array type");
3669 return error_mark_node;
3672 if (TREE_CODE (type) == FUNCTION_TYPE)
3674 error ("cast specifies function type");
3675 return error_mark_node;
3678 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3680 if (pedantic)
3682 if (TREE_CODE (type) == RECORD_TYPE
3683 || TREE_CODE (type) == UNION_TYPE)
3684 pedwarn ("ISO C forbids casting nonscalar to the same type");
3687 else if (TREE_CODE (type) == UNION_TYPE)
3689 tree field;
3690 value = default_function_array_conversion (value);
3692 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3693 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3694 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3695 break;
3697 if (field)
3699 tree t;
3701 if (pedantic)
3702 pedwarn ("ISO C forbids casts to union type");
3703 t = digest_init (type,
3704 build_constructor (type,
3705 build_tree_list (field, value)),
3707 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3708 return t;
3710 error ("cast to union type from type not present in union");
3711 return error_mark_node;
3713 else
3715 tree otype, ovalue;
3717 /* If casting to void, avoid the error that would come
3718 from default_conversion in the case of a non-lvalue array. */
3719 if (type == void_type_node)
3720 return build1 (CONVERT_EXPR, type, value);
3722 /* Convert functions and arrays to pointers,
3723 but don't convert any other types. */
3724 value = default_function_array_conversion (value);
3725 otype = TREE_TYPE (value);
3727 /* Optionally warn about potentially worrisome casts. */
3729 if (warn_cast_qual
3730 && TREE_CODE (type) == POINTER_TYPE
3731 && TREE_CODE (otype) == POINTER_TYPE)
3733 tree in_type = type;
3734 tree in_otype = otype;
3735 int added = 0;
3736 int discarded = 0;
3738 /* Check that the qualifiers on IN_TYPE are a superset of
3739 the qualifiers of IN_OTYPE. The outermost level of
3740 POINTER_TYPE nodes is uninteresting and we stop as soon
3741 as we hit a non-POINTER_TYPE node on either type. */
3744 in_otype = TREE_TYPE (in_otype);
3745 in_type = TREE_TYPE (in_type);
3747 /* GNU C allows cv-qualified function types. 'const'
3748 means the function is very pure, 'volatile' means it
3749 can't return. We need to warn when such qualifiers
3750 are added, not when they're taken away. */
3751 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3752 && TREE_CODE (in_type) == FUNCTION_TYPE)
3753 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3754 else
3755 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3757 while (TREE_CODE (in_type) == POINTER_TYPE
3758 && TREE_CODE (in_otype) == POINTER_TYPE);
3760 if (added)
3761 warning ("cast adds new qualifiers to function type");
3763 if (discarded)
3764 /* There are qualifiers present in IN_OTYPE that are not
3765 present in IN_TYPE. */
3766 warning ("cast discards qualifiers from pointer target type");
3769 /* Warn about possible alignment problems. */
3770 if (STRICT_ALIGNMENT && warn_cast_align
3771 && TREE_CODE (type) == POINTER_TYPE
3772 && TREE_CODE (otype) == POINTER_TYPE
3773 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3774 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3775 /* Don't warn about opaque types, where the actual alignment
3776 restriction is unknown. */
3777 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3778 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3779 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3780 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3781 warning ("cast increases required alignment of target type");
3783 if (TREE_CODE (type) == INTEGER_TYPE
3784 && TREE_CODE (otype) == POINTER_TYPE
3785 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3786 && !TREE_CONSTANT (value))
3787 warning ("cast from pointer to integer of different size");
3789 if (warn_bad_function_cast
3790 && TREE_CODE (value) == CALL_EXPR
3791 && TREE_CODE (type) != TREE_CODE (otype))
3792 warning ("cast does not match function type");
3794 if (TREE_CODE (type) == POINTER_TYPE
3795 && TREE_CODE (otype) == INTEGER_TYPE
3796 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3797 /* Don't warn about converting any constant. */
3798 && !TREE_CONSTANT (value))
3799 warning ("cast to pointer from integer of different size");
3801 if (TREE_CODE (type) == POINTER_TYPE
3802 && TREE_CODE (otype) == POINTER_TYPE
3803 && TREE_CODE (expr) == ADDR_EXPR
3804 && DECL_P (TREE_OPERAND (expr, 0))
3805 && flag_strict_aliasing && warn_strict_aliasing
3806 && !VOID_TYPE_P (TREE_TYPE (type)))
3808 /* Casting the address of a decl to non void pointer. Warn
3809 if the cast breaks type based aliasing. */
3810 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3811 warning ("type-punning to incomplete type might break strict-aliasing rules");
3812 else if (!alias_sets_conflict_p
3813 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3814 get_alias_set (TREE_TYPE (type))))
3815 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3818 ovalue = value;
3819 /* Replace a nonvolatile const static variable with its value. */
3820 if (optimize && TREE_CODE (value) == VAR_DECL)
3821 value = decl_constant_value (value);
3822 value = convert (type, value);
3824 /* Ignore any integer overflow caused by the cast. */
3825 if (TREE_CODE (value) == INTEGER_CST)
3827 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3828 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3832 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3833 if (pedantic && TREE_CODE (value) == INTEGER_CST
3834 && TREE_CODE (expr) == INTEGER_CST
3835 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3836 value = non_lvalue (value);
3838 /* If pedantic, don't let a cast be an lvalue. */
3839 if (value == expr && pedantic)
3840 value = non_lvalue (value);
3842 return value;
3845 /* Interpret a cast of expression EXPR to type TYPE. */
3846 tree
3847 c_cast_expr (tree type, tree expr)
3849 int saved_wsp = warn_strict_prototypes;
3851 /* This avoids warnings about unprototyped casts on
3852 integers. E.g. "#define SIG_DFL (void(*)())0". */
3853 if (TREE_CODE (expr) == INTEGER_CST)
3854 warn_strict_prototypes = 0;
3855 type = groktypename (type);
3856 warn_strict_prototypes = saved_wsp;
3858 return build_c_cast (type, expr);
3862 /* Build an assignment expression of lvalue LHS from value RHS.
3863 MODIFYCODE is the code for a binary operator that we use
3864 to combine the old value of LHS with RHS to get the new value.
3865 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3867 tree
3868 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3870 tree result;
3871 tree newrhs;
3872 tree lhstype = TREE_TYPE (lhs);
3873 tree olhstype = lhstype;
3875 /* Types that aren't fully specified cannot be used in assignments. */
3876 lhs = require_complete_type (lhs);
3878 /* Avoid duplicate error messages from operands that had errors. */
3879 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3880 return error_mark_node;
3882 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3883 /* Do not use STRIP_NOPS here. We do not want an enumerator
3884 whose value is 0 to count as a null pointer constant. */
3885 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3886 rhs = TREE_OPERAND (rhs, 0);
3888 newrhs = rhs;
3890 /* Handle control structure constructs used as "lvalues". */
3892 switch (TREE_CODE (lhs))
3894 /* Handle (a, b) used as an "lvalue". */
3895 case COMPOUND_EXPR:
3896 pedantic_lvalue_warning (COMPOUND_EXPR);
3897 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3898 if (TREE_CODE (newrhs) == ERROR_MARK)
3899 return error_mark_node;
3900 return build (COMPOUND_EXPR, lhstype,
3901 TREE_OPERAND (lhs, 0), newrhs);
3903 /* Handle (a ? b : c) used as an "lvalue". */
3904 case COND_EXPR:
3905 pedantic_lvalue_warning (COND_EXPR);
3906 rhs = save_expr (rhs);
3908 /* Produce (a ? (b = rhs) : (c = rhs))
3909 except that the RHS goes through a save-expr
3910 so the code to compute it is only emitted once. */
3911 tree cond
3912 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3913 build_modify_expr (TREE_OPERAND (lhs, 1),
3914 modifycode, rhs),
3915 build_modify_expr (TREE_OPERAND (lhs, 2),
3916 modifycode, rhs));
3917 if (TREE_CODE (cond) == ERROR_MARK)
3918 return cond;
3919 /* Make sure the code to compute the rhs comes out
3920 before the split. */
3921 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3922 /* But cast it to void to avoid an "unused" error. */
3923 convert (void_type_node, rhs), cond);
3925 default:
3926 break;
3929 /* If a binary op has been requested, combine the old LHS value with the RHS
3930 producing the value we should actually store into the LHS. */
3932 if (modifycode != NOP_EXPR)
3934 lhs = stabilize_reference (lhs);
3935 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3938 /* Handle a cast used as an "lvalue".
3939 We have already performed any binary operator using the value as cast.
3940 Now convert the result to the cast type of the lhs,
3941 and then true type of the lhs and store it there;
3942 then convert result back to the cast type to be the value
3943 of the assignment. */
3945 switch (TREE_CODE (lhs))
3947 case NOP_EXPR:
3948 case CONVERT_EXPR:
3949 case FLOAT_EXPR:
3950 case FIX_TRUNC_EXPR:
3951 case FIX_FLOOR_EXPR:
3952 case FIX_ROUND_EXPR:
3953 case FIX_CEIL_EXPR:
3954 newrhs = default_function_array_conversion (newrhs);
3956 tree inner_lhs = TREE_OPERAND (lhs, 0);
3957 tree result;
3958 result = build_modify_expr (inner_lhs, NOP_EXPR,
3959 convert (TREE_TYPE (inner_lhs),
3960 convert (lhstype, newrhs)));
3961 if (TREE_CODE (result) == ERROR_MARK)
3962 return result;
3963 pedantic_lvalue_warning (CONVERT_EXPR);
3964 return convert (TREE_TYPE (lhs), result);
3967 default:
3968 break;
3971 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3972 Reject anything strange now. */
3974 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3975 return error_mark_node;
3977 /* Warn about storing in something that is `const'. */
3979 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3980 || ((TREE_CODE (lhstype) == RECORD_TYPE
3981 || TREE_CODE (lhstype) == UNION_TYPE)
3982 && C_TYPE_FIELDS_READONLY (lhstype)))
3983 readonly_warning (lhs, "assignment");
3985 /* If storing into a structure or union member,
3986 it has probably been given type `int'.
3987 Compute the type that would go with
3988 the actual amount of storage the member occupies. */
3990 if (TREE_CODE (lhs) == COMPONENT_REF
3991 && (TREE_CODE (lhstype) == INTEGER_TYPE
3992 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3993 || TREE_CODE (lhstype) == REAL_TYPE
3994 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3995 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3997 /* If storing in a field that is in actuality a short or narrower than one,
3998 we must store in the field in its actual type. */
4000 if (lhstype != TREE_TYPE (lhs))
4002 lhs = copy_node (lhs);
4003 TREE_TYPE (lhs) = lhstype;
4006 /* Convert new value to destination type. */
4008 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4009 NULL_TREE, NULL_TREE, 0);
4010 if (TREE_CODE (newrhs) == ERROR_MARK)
4011 return error_mark_node;
4013 /* Scan operands */
4015 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4016 TREE_SIDE_EFFECTS (result) = 1;
4018 /* If we got the LHS in a different type for storing in,
4019 convert the result back to the nominal type of LHS
4020 so that the value we return always has the same type
4021 as the LHS argument. */
4023 if (olhstype == TREE_TYPE (result))
4024 return result;
4025 return convert_for_assignment (olhstype, result, _("assignment"),
4026 NULL_TREE, NULL_TREE, 0);
4029 /* Convert value RHS to type TYPE as preparation for an assignment
4030 to an lvalue of type TYPE.
4031 The real work of conversion is done by `convert'.
4032 The purpose of this function is to generate error messages
4033 for assignments that are not allowed in C.
4034 ERRTYPE is a string to use in error messages:
4035 "assignment", "return", etc. If it is null, this is parameter passing
4036 for a function call (and different error messages are output).
4038 FUNNAME is the name of the function being called,
4039 as an IDENTIFIER_NODE, or null.
4040 PARMNUM is the number of the argument, for printing in error messages. */
4042 static tree
4043 convert_for_assignment (tree type, tree rhs, const char *errtype,
4044 tree fundecl, tree funname, int parmnum)
4046 enum tree_code codel = TREE_CODE (type);
4047 tree rhstype;
4048 enum tree_code coder;
4050 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4051 /* Do not use STRIP_NOPS here. We do not want an enumerator
4052 whose value is 0 to count as a null pointer constant. */
4053 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4054 rhs = TREE_OPERAND (rhs, 0);
4056 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4057 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4058 rhs = default_conversion (rhs);
4059 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4060 rhs = decl_constant_value_for_broken_optimization (rhs);
4062 rhstype = TREE_TYPE (rhs);
4063 coder = TREE_CODE (rhstype);
4065 if (coder == ERROR_MARK)
4066 return error_mark_node;
4068 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4070 overflow_warning (rhs);
4071 /* Check for Objective-C protocols. This will automatically
4072 issue a warning if there are protocol violations. No need to
4073 use the return value. */
4074 if (c_dialect_objc ())
4075 objc_comptypes (type, rhstype, 0);
4076 return rhs;
4079 if (coder == VOID_TYPE)
4081 error ("void value not ignored as it ought to be");
4082 return error_mark_node;
4084 /* A type converts to a reference to it.
4085 This code doesn't fully support references, it's just for the
4086 special case of va_start and va_copy. */
4087 if (codel == REFERENCE_TYPE
4088 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
4090 if (!lvalue_p (rhs))
4092 error ("cannot pass rvalue to reference parameter");
4093 return error_mark_node;
4095 if (!c_mark_addressable (rhs))
4096 return error_mark_node;
4097 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4099 /* We already know that these two types are compatible, but they
4100 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4101 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4102 likely to be va_list, a typedef to __builtin_va_list, which
4103 is different enough that it will cause problems later. */
4104 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4105 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4107 rhs = build1 (NOP_EXPR, type, rhs);
4108 return rhs;
4110 /* Some types can interconvert without explicit casts. */
4111 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4112 && ((*targetm.vector_opaque_p) (type)
4113 || (*targetm.vector_opaque_p) (rhstype)))
4114 return convert (type, rhs);
4115 /* Arithmetic types all interconvert, and enum is treated like int. */
4116 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4117 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4118 || codel == BOOLEAN_TYPE)
4119 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4120 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4121 || coder == BOOLEAN_TYPE))
4122 return convert_and_check (type, rhs);
4124 /* Conversion to a transparent union from its member types.
4125 This applies only to function arguments. */
4126 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4128 tree memb_types;
4129 tree marginal_memb_type = 0;
4131 for (memb_types = TYPE_FIELDS (type); memb_types;
4132 memb_types = TREE_CHAIN (memb_types))
4134 tree memb_type = TREE_TYPE (memb_types);
4136 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4137 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
4138 break;
4140 if (TREE_CODE (memb_type) != POINTER_TYPE)
4141 continue;
4143 if (coder == POINTER_TYPE)
4145 tree ttl = TREE_TYPE (memb_type);
4146 tree ttr = TREE_TYPE (rhstype);
4148 /* Any non-function converts to a [const][volatile] void *
4149 and vice versa; otherwise, targets must be the same.
4150 Meanwhile, the lhs target must have all the qualifiers of
4151 the rhs. */
4152 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4153 || comp_target_types (memb_type, rhstype, 0))
4155 /* If this type won't generate any warnings, use it. */
4156 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4157 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4158 && TREE_CODE (ttl) == FUNCTION_TYPE)
4159 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4160 == TYPE_QUALS (ttr))
4161 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4162 == TYPE_QUALS (ttl))))
4163 break;
4165 /* Keep looking for a better type, but remember this one. */
4166 if (! marginal_memb_type)
4167 marginal_memb_type = memb_type;
4171 /* Can convert integer zero to any pointer type. */
4172 if (integer_zerop (rhs)
4173 || (TREE_CODE (rhs) == NOP_EXPR
4174 && integer_zerop (TREE_OPERAND (rhs, 0))))
4176 rhs = null_pointer_node;
4177 break;
4181 if (memb_types || marginal_memb_type)
4183 if (! memb_types)
4185 /* We have only a marginally acceptable member type;
4186 it needs a warning. */
4187 tree ttl = TREE_TYPE (marginal_memb_type);
4188 tree ttr = TREE_TYPE (rhstype);
4190 /* Const and volatile mean something different for function
4191 types, so the usual warnings are not appropriate. */
4192 if (TREE_CODE (ttr) == FUNCTION_TYPE
4193 && TREE_CODE (ttl) == FUNCTION_TYPE)
4195 /* Because const and volatile on functions are
4196 restrictions that say the function will not do
4197 certain things, it is okay to use a const or volatile
4198 function where an ordinary one is wanted, but not
4199 vice-versa. */
4200 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4201 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4202 errtype, funname, parmnum);
4204 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4205 warn_for_assignment ("%s discards qualifiers from pointer target type",
4206 errtype, funname,
4207 parmnum);
4210 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4211 pedwarn ("ISO C prohibits argument conversion to union type");
4213 return build1 (NOP_EXPR, type, rhs);
4217 /* Conversions among pointers */
4218 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4219 && (coder == codel))
4221 tree ttl = TREE_TYPE (type);
4222 tree ttr = TREE_TYPE (rhstype);
4223 bool is_opaque_pointer;
4225 /* Opaque pointers are treated like void pointers. */
4226 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
4227 || (*targetm.vector_opaque_p) (rhstype))
4228 && TREE_CODE (ttl) == VECTOR_TYPE
4229 && TREE_CODE (ttr) == VECTOR_TYPE;
4231 /* Any non-function converts to a [const][volatile] void *
4232 and vice versa; otherwise, targets must be the same.
4233 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4234 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4235 || comp_target_types (type, rhstype, 0)
4236 || is_opaque_pointer
4237 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4238 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4240 if (pedantic
4241 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4243 (VOID_TYPE_P (ttr)
4244 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4245 which are not ANSI null ptr constants. */
4246 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4247 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4248 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4249 errtype, funname, parmnum);
4250 /* Const and volatile mean something different for function types,
4251 so the usual warnings are not appropriate. */
4252 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4253 && TREE_CODE (ttl) != FUNCTION_TYPE)
4255 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4256 warn_for_assignment ("%s discards qualifiers from pointer target type",
4257 errtype, funname, parmnum);
4258 /* If this is not a case of ignoring a mismatch in signedness,
4259 no warning. */
4260 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4261 || comp_target_types (type, rhstype, 0))
4263 /* If there is a mismatch, do warn. */
4264 else if (pedantic)
4265 warn_for_assignment ("pointer targets in %s differ in signedness",
4266 errtype, funname, parmnum);
4268 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4269 && TREE_CODE (ttr) == FUNCTION_TYPE)
4271 /* Because const and volatile on functions are restrictions
4272 that say the function will not do certain things,
4273 it is okay to use a const or volatile function
4274 where an ordinary one is wanted, but not vice-versa. */
4275 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4276 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4277 errtype, funname, parmnum);
4280 else
4281 warn_for_assignment ("%s from incompatible pointer type",
4282 errtype, funname, parmnum);
4283 return convert (type, rhs);
4285 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4287 /* An explicit constant 0 can convert to a pointer,
4288 or one that results from arithmetic, even including
4289 a cast to integer type. */
4290 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4292 ! (TREE_CODE (rhs) == NOP_EXPR
4293 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4294 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4295 && integer_zerop (TREE_OPERAND (rhs, 0))))
4297 warn_for_assignment ("%s makes pointer from integer without a cast",
4298 errtype, funname, parmnum);
4299 return convert (type, rhs);
4301 return null_pointer_node;
4303 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4305 warn_for_assignment ("%s makes integer from pointer without a cast",
4306 errtype, funname, parmnum);
4307 return convert (type, rhs);
4309 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4310 return convert (type, rhs);
4312 if (!errtype)
4314 if (funname)
4316 tree selector = objc_message_selector ();
4318 if (selector && parmnum > 2)
4319 error ("incompatible type for argument %d of `%s'",
4320 parmnum - 2, IDENTIFIER_POINTER (selector));
4321 else
4322 error ("incompatible type for argument %d of `%s'",
4323 parmnum, IDENTIFIER_POINTER (funname));
4325 else
4326 error ("incompatible type for argument %d of indirect function call",
4327 parmnum);
4329 else
4330 error ("incompatible types in %s", errtype);
4332 return error_mark_node;
4335 /* Convert VALUE for assignment into inlined parameter PARM. */
4337 tree
4338 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
4340 tree ret, type;
4342 /* If FN was prototyped, the value has been converted already
4343 in convert_arguments. */
4344 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4345 return value;
4347 type = TREE_TYPE (parm);
4348 ret = convert_for_assignment (type, value,
4349 (char *) 0 /* arg passing */, fn,
4350 DECL_NAME (fn), 0);
4351 if (PROMOTE_PROTOTYPES
4352 && INTEGRAL_TYPE_P (type)
4353 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4354 ret = default_conversion (ret);
4355 return ret;
4358 /* Print a warning using MSGID.
4359 It gets OPNAME as its one parameter.
4360 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4361 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4362 FUNCTION and ARGNUM are handled specially if we are building an
4363 Objective-C selector. */
4365 static void
4366 warn_for_assignment (const char *msgid, const char *opname, tree function,
4367 int argnum)
4369 if (opname == 0)
4371 tree selector = objc_message_selector ();
4372 char * new_opname;
4374 if (selector && argnum > 2)
4376 function = selector;
4377 argnum -= 2;
4379 if (argnum == 0)
4381 if (function)
4383 /* Function name is known; supply it. */
4384 const char *const argstring = _("passing arg of `%s'");
4385 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4386 + strlen (argstring) + 1
4387 + 1);
4388 sprintf (new_opname, argstring,
4389 IDENTIFIER_POINTER (function));
4391 else
4393 /* Function name unknown (call through ptr). */
4394 const char *const argnofun = _("passing arg of pointer to function");
4395 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4396 sprintf (new_opname, argnofun);
4399 else if (function)
4401 /* Function name is known; supply it. */
4402 const char *const argstring = _("passing arg %d of `%s'");
4403 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4404 + strlen (argstring) + 1 + 25
4405 /*%d*/ + 1);
4406 sprintf (new_opname, argstring, argnum,
4407 IDENTIFIER_POINTER (function));
4409 else
4411 /* Function name unknown (call through ptr); just give arg number. */
4412 const char *const argnofun = _("passing arg %d of pointer to function");
4413 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4414 sprintf (new_opname, argnofun, argnum);
4416 opname = new_opname;
4418 pedwarn (msgid, opname);
4421 /* If VALUE is a compound expr all of whose expressions are constant, then
4422 return its value. Otherwise, return error_mark_node.
4424 This is for handling COMPOUND_EXPRs as initializer elements
4425 which is allowed with a warning when -pedantic is specified. */
4427 static tree
4428 valid_compound_expr_initializer (tree value, tree endtype)
4430 if (TREE_CODE (value) == COMPOUND_EXPR)
4432 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4433 == error_mark_node)
4434 return error_mark_node;
4435 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4436 endtype);
4438 else if (! TREE_CONSTANT (value)
4439 && ! initializer_constant_valid_p (value, endtype))
4440 return error_mark_node;
4441 else
4442 return value;
4445 /* Perform appropriate conversions on the initial value of a variable,
4446 store it in the declaration DECL,
4447 and print any error messages that are appropriate.
4448 If the init is invalid, store an ERROR_MARK. */
4450 void
4451 store_init_value (tree decl, tree init)
4453 tree value, type;
4455 /* If variable's type was invalidly declared, just ignore it. */
4457 type = TREE_TYPE (decl);
4458 if (TREE_CODE (type) == ERROR_MARK)
4459 return;
4461 /* Digest the specified initializer into an expression. */
4463 value = digest_init (type, init, TREE_STATIC (decl));
4465 /* Store the expression if valid; else report error. */
4467 if (warn_traditional && !in_system_header
4468 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4469 warning ("traditional C rejects automatic aggregate initialization");
4471 DECL_INITIAL (decl) = value;
4473 /* ANSI wants warnings about out-of-range constant initializers. */
4474 STRIP_TYPE_NOPS (value);
4475 constant_expression_warning (value);
4477 /* Check if we need to set array size from compound literal size. */
4478 if (TREE_CODE (type) == ARRAY_TYPE
4479 && TYPE_DOMAIN (type) == 0
4480 && value != error_mark_node)
4482 tree inside_init = init;
4484 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4485 inside_init = TREE_OPERAND (init, 0);
4486 inside_init = fold (inside_init);
4488 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4490 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4492 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4494 /* For int foo[] = (int [3]){1}; we need to set array size
4495 now since later on array initializer will be just the
4496 brace enclosed list of the compound literal. */
4497 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4498 layout_type (type);
4499 layout_decl (decl, 0);
4505 /* Methods for storing and printing names for error messages. */
4507 /* Implement a spelling stack that allows components of a name to be pushed
4508 and popped. Each element on the stack is this structure. */
4510 struct spelling
4512 int kind;
4513 union
4515 int i;
4516 const char *s;
4517 } u;
4520 #define SPELLING_STRING 1
4521 #define SPELLING_MEMBER 2
4522 #define SPELLING_BOUNDS 3
4524 static struct spelling *spelling; /* Next stack element (unused). */
4525 static struct spelling *spelling_base; /* Spelling stack base. */
4526 static int spelling_size; /* Size of the spelling stack. */
4528 /* Macros to save and restore the spelling stack around push_... functions.
4529 Alternative to SAVE_SPELLING_STACK. */
4531 #define SPELLING_DEPTH() (spelling - spelling_base)
4532 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4534 /* Push an element on the spelling stack with type KIND and assign VALUE
4535 to MEMBER. */
4537 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4539 int depth = SPELLING_DEPTH (); \
4541 if (depth >= spelling_size) \
4543 spelling_size += 10; \
4544 if (spelling_base == 0) \
4545 spelling_base \
4546 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4547 else \
4548 spelling_base \
4549 = (struct spelling *) xrealloc (spelling_base, \
4550 spelling_size * sizeof (struct spelling)); \
4551 RESTORE_SPELLING_DEPTH (depth); \
4554 spelling->kind = (KIND); \
4555 spelling->MEMBER = (VALUE); \
4556 spelling++; \
4559 /* Push STRING on the stack. Printed literally. */
4561 static void
4562 push_string (const char *string)
4564 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4567 /* Push a member name on the stack. Printed as '.' STRING. */
4569 static void
4570 push_member_name (tree decl)
4572 const char *const string
4573 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4574 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4577 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4579 static void
4580 push_array_bounds (int bounds)
4582 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4585 /* Compute the maximum size in bytes of the printed spelling. */
4587 static int
4588 spelling_length (void)
4590 int size = 0;
4591 struct spelling *p;
4593 for (p = spelling_base; p < spelling; p++)
4595 if (p->kind == SPELLING_BOUNDS)
4596 size += 25;
4597 else
4598 size += strlen (p->u.s) + 1;
4601 return size;
4604 /* Print the spelling to BUFFER and return it. */
4606 static char *
4607 print_spelling (char *buffer)
4609 char *d = buffer;
4610 struct spelling *p;
4612 for (p = spelling_base; p < spelling; p++)
4613 if (p->kind == SPELLING_BOUNDS)
4615 sprintf (d, "[%d]", p->u.i);
4616 d += strlen (d);
4618 else
4620 const char *s;
4621 if (p->kind == SPELLING_MEMBER)
4622 *d++ = '.';
4623 for (s = p->u.s; (*d = *s++); d++)
4626 *d++ = '\0';
4627 return buffer;
4630 /* Issue an error message for a bad initializer component.
4631 MSGID identifies the message.
4632 The component name is taken from the spelling stack. */
4634 void
4635 error_init (const char *msgid)
4637 char *ofwhat;
4639 error ("%s", _(msgid));
4640 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4641 if (*ofwhat)
4642 error ("(near initialization for `%s')", ofwhat);
4645 /* Issue a pedantic warning for a bad initializer component.
4646 MSGID identifies the message.
4647 The component name is taken from the spelling stack. */
4649 void
4650 pedwarn_init (const char *msgid)
4652 char *ofwhat;
4654 pedwarn ("%s", _(msgid));
4655 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4656 if (*ofwhat)
4657 pedwarn ("(near initialization for `%s')", ofwhat);
4660 /* Issue a warning for a bad initializer component.
4661 MSGID identifies the message.
4662 The component name is taken from the spelling stack. */
4664 static void
4665 warning_init (const char *msgid)
4667 char *ofwhat;
4669 warning ("%s", _(msgid));
4670 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4671 if (*ofwhat)
4672 warning ("(near initialization for `%s')", ofwhat);
4675 /* Digest the parser output INIT as an initializer for type TYPE.
4676 Return a C expression of type TYPE to represent the initial value.
4678 REQUIRE_CONSTANT requests an error if non-constant initializers or
4679 elements are seen. */
4681 static tree
4682 digest_init (tree type, tree init, int require_constant)
4684 enum tree_code code = TREE_CODE (type);
4685 tree inside_init = init;
4687 if (type == error_mark_node
4688 || init == error_mark_node
4689 || TREE_TYPE (init) == error_mark_node)
4690 return error_mark_node;
4692 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4693 /* Do not use STRIP_NOPS here. We do not want an enumerator
4694 whose value is 0 to count as a null pointer constant. */
4695 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4696 inside_init = TREE_OPERAND (init, 0);
4698 inside_init = fold (inside_init);
4700 /* Initialization of an array of chars from a string constant
4701 optionally enclosed in braces. */
4703 if (code == ARRAY_TYPE)
4705 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4706 if ((typ1 == char_type_node
4707 || typ1 == signed_char_type_node
4708 || typ1 == unsigned_char_type_node
4709 || typ1 == unsigned_wchar_type_node
4710 || typ1 == signed_wchar_type_node)
4711 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4713 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4714 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
4715 return inside_init;
4717 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4718 != char_type_node)
4719 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4721 error_init ("char-array initialized from wide string");
4722 return error_mark_node;
4724 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4725 == char_type_node)
4726 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4728 error_init ("int-array initialized from non-wide string");
4729 return error_mark_node;
4732 TREE_TYPE (inside_init) = type;
4733 if (TYPE_DOMAIN (type) != 0
4734 && TYPE_SIZE (type) != 0
4735 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4736 /* Subtract 1 (or sizeof (wchar_t))
4737 because it's ok to ignore the terminating null char
4738 that is counted in the length of the constant. */
4739 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4740 TREE_STRING_LENGTH (inside_init)
4741 - ((TYPE_PRECISION (typ1)
4742 != TYPE_PRECISION (char_type_node))
4743 ? (TYPE_PRECISION (wchar_type_node)
4744 / BITS_PER_UNIT)
4745 : 1)))
4746 pedwarn_init ("initializer-string for array of chars is too long");
4748 return inside_init;
4752 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4753 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4754 below and handle as a constructor. */
4755 if (code == VECTOR_TYPE
4756 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
4757 && TREE_CONSTANT (inside_init))
4759 if (TREE_CODE (inside_init) == VECTOR_CST
4760 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4761 TYPE_MAIN_VARIANT (type),
4762 COMPARE_STRICT))
4763 return inside_init;
4764 else
4765 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4768 /* Any type can be initialized
4769 from an expression of the same type, optionally with braces. */
4771 if (inside_init && TREE_TYPE (inside_init) != 0
4772 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4773 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4774 || (code == ARRAY_TYPE
4775 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4776 || (code == VECTOR_TYPE
4777 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4778 || (code == POINTER_TYPE
4779 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4780 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4781 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4782 TREE_TYPE (type), COMPARE_STRICT))))
4784 if (code == POINTER_TYPE)
4785 inside_init = default_function_array_conversion (inside_init);
4787 if (require_constant && !flag_isoc99
4788 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4790 /* As an extension, allow initializing objects with static storage
4791 duration with compound literals (which are then treated just as
4792 the brace enclosed list they contain). */
4793 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4794 inside_init = DECL_INITIAL (decl);
4797 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4798 && TREE_CODE (inside_init) != CONSTRUCTOR)
4800 error_init ("array initialized from non-constant array expression");
4801 return error_mark_node;
4804 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4805 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4807 /* Compound expressions can only occur here if -pedantic or
4808 -pedantic-errors is specified. In the later case, we always want
4809 an error. In the former case, we simply want a warning. */
4810 if (require_constant && pedantic
4811 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4813 inside_init
4814 = valid_compound_expr_initializer (inside_init,
4815 TREE_TYPE (inside_init));
4816 if (inside_init == error_mark_node)
4817 error_init ("initializer element is not constant");
4818 else
4819 pedwarn_init ("initializer element is not constant");
4820 if (flag_pedantic_errors)
4821 inside_init = error_mark_node;
4823 else if (require_constant
4824 && (!TREE_CONSTANT (inside_init)
4825 /* This test catches things like `7 / 0' which
4826 result in an expression for which TREE_CONSTANT
4827 is true, but which is not actually something
4828 that is a legal constant. We really should not
4829 be using this function, because it is a part of
4830 the back-end. Instead, the expression should
4831 already have been turned into ERROR_MARK_NODE. */
4832 || !initializer_constant_valid_p (inside_init,
4833 TREE_TYPE (inside_init))))
4835 error_init ("initializer element is not constant");
4836 inside_init = error_mark_node;
4839 return inside_init;
4842 /* Handle scalar types, including conversions. */
4844 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4845 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4847 /* Note that convert_for_assignment calls default_conversion
4848 for arrays and functions. We must not call it in the
4849 case where inside_init is a null pointer constant. */
4850 inside_init
4851 = convert_for_assignment (type, init, _("initialization"),
4852 NULL_TREE, NULL_TREE, 0);
4854 if (require_constant && ! TREE_CONSTANT (inside_init))
4856 error_init ("initializer element is not constant");
4857 inside_init = error_mark_node;
4859 else if (require_constant
4860 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4862 error_init ("initializer element is not computable at load time");
4863 inside_init = error_mark_node;
4866 return inside_init;
4869 /* Come here only for records and arrays. */
4871 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4873 error_init ("variable-sized object may not be initialized");
4874 return error_mark_node;
4877 error_init ("invalid initializer");
4878 return error_mark_node;
4881 /* Handle initializers that use braces. */
4883 /* Type of object we are accumulating a constructor for.
4884 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4885 static tree constructor_type;
4887 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4888 left to fill. */
4889 static tree constructor_fields;
4891 /* For an ARRAY_TYPE, this is the specified index
4892 at which to store the next element we get. */
4893 static tree constructor_index;
4895 /* For an ARRAY_TYPE, this is the maximum index. */
4896 static tree constructor_max_index;
4898 /* For a RECORD_TYPE, this is the first field not yet written out. */
4899 static tree constructor_unfilled_fields;
4901 /* For an ARRAY_TYPE, this is the index of the first element
4902 not yet written out. */
4903 static tree constructor_unfilled_index;
4905 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4906 This is so we can generate gaps between fields, when appropriate. */
4907 static tree constructor_bit_index;
4909 /* If we are saving up the elements rather than allocating them,
4910 this is the list of elements so far (in reverse order,
4911 most recent first). */
4912 static tree constructor_elements;
4914 /* 1 if constructor should be incrementally stored into a constructor chain,
4915 0 if all the elements should be kept in AVL tree. */
4916 static int constructor_incremental;
4918 /* 1 if so far this constructor's elements are all compile-time constants. */
4919 static int constructor_constant;
4921 /* 1 if so far this constructor's elements are all valid address constants. */
4922 static int constructor_simple;
4924 /* 1 if this constructor is erroneous so far. */
4925 static int constructor_erroneous;
4927 /* Structure for managing pending initializer elements, organized as an
4928 AVL tree. */
4930 struct init_node
4932 struct init_node *left, *right;
4933 struct init_node *parent;
4934 int balance;
4935 tree purpose;
4936 tree value;
4939 /* Tree of pending elements at this constructor level.
4940 These are elements encountered out of order
4941 which belong at places we haven't reached yet in actually
4942 writing the output.
4943 Will never hold tree nodes across GC runs. */
4944 static struct init_node *constructor_pending_elts;
4946 /* The SPELLING_DEPTH of this constructor. */
4947 static int constructor_depth;
4949 /* 0 if implicitly pushing constructor levels is allowed. */
4950 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4952 static int require_constant_value;
4953 static int require_constant_elements;
4955 /* DECL node for which an initializer is being read.
4956 0 means we are reading a constructor expression
4957 such as (struct foo) {...}. */
4958 static tree constructor_decl;
4960 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4961 static const char *constructor_asmspec;
4963 /* Nonzero if this is an initializer for a top-level decl. */
4964 static int constructor_top_level;
4966 /* Nonzero if there were any member designators in this initializer. */
4967 static int constructor_designated;
4969 /* Nesting depth of designator list. */
4970 static int designator_depth;
4972 /* Nonzero if there were diagnosed errors in this designator list. */
4973 static int designator_errorneous;
4976 /* This stack has a level for each implicit or explicit level of
4977 structuring in the initializer, including the outermost one. It
4978 saves the values of most of the variables above. */
4980 struct constructor_range_stack;
4982 struct constructor_stack
4984 struct constructor_stack *next;
4985 tree type;
4986 tree fields;
4987 tree index;
4988 tree max_index;
4989 tree unfilled_index;
4990 tree unfilled_fields;
4991 tree bit_index;
4992 tree elements;
4993 struct init_node *pending_elts;
4994 int offset;
4995 int depth;
4996 /* If nonzero, this value should replace the entire
4997 constructor at this level. */
4998 tree replacement_value;
4999 struct constructor_range_stack *range_stack;
5000 char constant;
5001 char simple;
5002 char implicit;
5003 char erroneous;
5004 char outer;
5005 char incremental;
5006 char designated;
5009 struct constructor_stack *constructor_stack;
5011 /* This stack represents designators from some range designator up to
5012 the last designator in the list. */
5014 struct constructor_range_stack
5016 struct constructor_range_stack *next, *prev;
5017 struct constructor_stack *stack;
5018 tree range_start;
5019 tree index;
5020 tree range_end;
5021 tree fields;
5024 struct constructor_range_stack *constructor_range_stack;
5026 /* This stack records separate initializers that are nested.
5027 Nested initializers can't happen in ANSI C, but GNU C allows them
5028 in cases like { ... (struct foo) { ... } ... }. */
5030 struct initializer_stack
5032 struct initializer_stack *next;
5033 tree decl;
5034 const char *asmspec;
5035 struct constructor_stack *constructor_stack;
5036 struct constructor_range_stack *constructor_range_stack;
5037 tree elements;
5038 struct spelling *spelling;
5039 struct spelling *spelling_base;
5040 int spelling_size;
5041 char top_level;
5042 char require_constant_value;
5043 char require_constant_elements;
5046 struct initializer_stack *initializer_stack;
5048 /* Prepare to parse and output the initializer for variable DECL. */
5050 void
5051 start_init (tree decl, tree asmspec_tree, int top_level)
5053 const char *locus;
5054 struct initializer_stack *p
5055 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5056 const char *asmspec = 0;
5058 if (asmspec_tree)
5059 asmspec = TREE_STRING_POINTER (asmspec_tree);
5061 p->decl = constructor_decl;
5062 p->asmspec = constructor_asmspec;
5063 p->require_constant_value = require_constant_value;
5064 p->require_constant_elements = require_constant_elements;
5065 p->constructor_stack = constructor_stack;
5066 p->constructor_range_stack = constructor_range_stack;
5067 p->elements = constructor_elements;
5068 p->spelling = spelling;
5069 p->spelling_base = spelling_base;
5070 p->spelling_size = spelling_size;
5071 p->top_level = constructor_top_level;
5072 p->next = initializer_stack;
5073 initializer_stack = p;
5075 constructor_decl = decl;
5076 constructor_asmspec = asmspec;
5077 constructor_designated = 0;
5078 constructor_top_level = top_level;
5080 if (decl != 0)
5082 require_constant_value = TREE_STATIC (decl);
5083 require_constant_elements
5084 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5085 /* For a scalar, you can always use any value to initialize,
5086 even within braces. */
5087 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5088 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5089 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5090 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5091 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5093 else
5095 require_constant_value = 0;
5096 require_constant_elements = 0;
5097 locus = "(anonymous)";
5100 constructor_stack = 0;
5101 constructor_range_stack = 0;
5103 missing_braces_mentioned = 0;
5105 spelling_base = 0;
5106 spelling_size = 0;
5107 RESTORE_SPELLING_DEPTH (0);
5109 if (locus)
5110 push_string (locus);
5113 void
5114 finish_init (void)
5116 struct initializer_stack *p = initializer_stack;
5118 /* Free the whole constructor stack of this initializer. */
5119 while (constructor_stack)
5121 struct constructor_stack *q = constructor_stack;
5122 constructor_stack = q->next;
5123 free (q);
5126 if (constructor_range_stack)
5127 abort ();
5129 /* Pop back to the data of the outer initializer (if any). */
5130 constructor_decl = p->decl;
5131 constructor_asmspec = p->asmspec;
5132 require_constant_value = p->require_constant_value;
5133 require_constant_elements = p->require_constant_elements;
5134 constructor_stack = p->constructor_stack;
5135 constructor_range_stack = p->constructor_range_stack;
5136 constructor_elements = p->elements;
5137 spelling = p->spelling;
5138 spelling_base = p->spelling_base;
5139 spelling_size = p->spelling_size;
5140 constructor_top_level = p->top_level;
5141 initializer_stack = p->next;
5142 free (p);
5145 /* Call here when we see the initializer is surrounded by braces.
5146 This is instead of a call to push_init_level;
5147 it is matched by a call to pop_init_level.
5149 TYPE is the type to initialize, for a constructor expression.
5150 For an initializer for a decl, TYPE is zero. */
5152 void
5153 really_start_incremental_init (tree type)
5155 struct constructor_stack *p
5156 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5158 if (type == 0)
5159 type = TREE_TYPE (constructor_decl);
5161 if ((*targetm.vector_opaque_p) (type))
5162 error ("opaque vector types cannot be initialized");
5164 p->type = constructor_type;
5165 p->fields = constructor_fields;
5166 p->index = constructor_index;
5167 p->max_index = constructor_max_index;
5168 p->unfilled_index = constructor_unfilled_index;
5169 p->unfilled_fields = constructor_unfilled_fields;
5170 p->bit_index = constructor_bit_index;
5171 p->elements = constructor_elements;
5172 p->constant = constructor_constant;
5173 p->simple = constructor_simple;
5174 p->erroneous = constructor_erroneous;
5175 p->pending_elts = constructor_pending_elts;
5176 p->depth = constructor_depth;
5177 p->replacement_value = 0;
5178 p->implicit = 0;
5179 p->range_stack = 0;
5180 p->outer = 0;
5181 p->incremental = constructor_incremental;
5182 p->designated = constructor_designated;
5183 p->next = 0;
5184 constructor_stack = p;
5186 constructor_constant = 1;
5187 constructor_simple = 1;
5188 constructor_depth = SPELLING_DEPTH ();
5189 constructor_elements = 0;
5190 constructor_pending_elts = 0;
5191 constructor_type = type;
5192 constructor_incremental = 1;
5193 constructor_designated = 0;
5194 designator_depth = 0;
5195 designator_errorneous = 0;
5197 if (TREE_CODE (constructor_type) == RECORD_TYPE
5198 || TREE_CODE (constructor_type) == UNION_TYPE)
5200 constructor_fields = TYPE_FIELDS (constructor_type);
5201 /* Skip any nameless bit fields at the beginning. */
5202 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5203 && DECL_NAME (constructor_fields) == 0)
5204 constructor_fields = TREE_CHAIN (constructor_fields);
5206 constructor_unfilled_fields = constructor_fields;
5207 constructor_bit_index = bitsize_zero_node;
5209 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5211 if (TYPE_DOMAIN (constructor_type))
5213 constructor_max_index
5214 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5216 /* Detect non-empty initializations of zero-length arrays. */
5217 if (constructor_max_index == NULL_TREE
5218 && TYPE_SIZE (constructor_type))
5219 constructor_max_index = build_int_2 (-1, -1);
5221 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5222 to initialize VLAs will cause a proper error; avoid tree
5223 checking errors as well by setting a safe value. */
5224 if (constructor_max_index
5225 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5226 constructor_max_index = build_int_2 (-1, -1);
5228 constructor_index
5229 = convert (bitsizetype,
5230 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5232 else
5233 constructor_index = bitsize_zero_node;
5235 constructor_unfilled_index = constructor_index;
5237 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5239 /* Vectors are like simple fixed-size arrays. */
5240 constructor_max_index =
5241 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5242 constructor_index = convert (bitsizetype, bitsize_zero_node);
5243 constructor_unfilled_index = constructor_index;
5245 else
5247 /* Handle the case of int x = {5}; */
5248 constructor_fields = constructor_type;
5249 constructor_unfilled_fields = constructor_type;
5253 /* Push down into a subobject, for initialization.
5254 If this is for an explicit set of braces, IMPLICIT is 0.
5255 If it is because the next element belongs at a lower level,
5256 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5258 void
5259 push_init_level (int implicit)
5261 struct constructor_stack *p;
5262 tree value = NULL_TREE;
5264 /* If we've exhausted any levels that didn't have braces,
5265 pop them now. */
5266 while (constructor_stack->implicit)
5268 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5269 || TREE_CODE (constructor_type) == UNION_TYPE)
5270 && constructor_fields == 0)
5271 process_init_element (pop_init_level (1));
5272 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5273 && constructor_max_index
5274 && tree_int_cst_lt (constructor_max_index, constructor_index))
5275 process_init_element (pop_init_level (1));
5276 else
5277 break;
5280 /* Unless this is an explicit brace, we need to preserve previous
5281 content if any. */
5282 if (implicit)
5284 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5285 || TREE_CODE (constructor_type) == UNION_TYPE)
5286 && constructor_fields)
5287 value = find_init_member (constructor_fields);
5288 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5289 value = find_init_member (constructor_index);
5292 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5293 p->type = constructor_type;
5294 p->fields = constructor_fields;
5295 p->index = constructor_index;
5296 p->max_index = constructor_max_index;
5297 p->unfilled_index = constructor_unfilled_index;
5298 p->unfilled_fields = constructor_unfilled_fields;
5299 p->bit_index = constructor_bit_index;
5300 p->elements = constructor_elements;
5301 p->constant = constructor_constant;
5302 p->simple = constructor_simple;
5303 p->erroneous = constructor_erroneous;
5304 p->pending_elts = constructor_pending_elts;
5305 p->depth = constructor_depth;
5306 p->replacement_value = 0;
5307 p->implicit = implicit;
5308 p->outer = 0;
5309 p->incremental = constructor_incremental;
5310 p->designated = constructor_designated;
5311 p->next = constructor_stack;
5312 p->range_stack = 0;
5313 constructor_stack = p;
5315 constructor_constant = 1;
5316 constructor_simple = 1;
5317 constructor_depth = SPELLING_DEPTH ();
5318 constructor_elements = 0;
5319 constructor_incremental = 1;
5320 constructor_designated = 0;
5321 constructor_pending_elts = 0;
5322 if (!implicit)
5324 p->range_stack = constructor_range_stack;
5325 constructor_range_stack = 0;
5326 designator_depth = 0;
5327 designator_errorneous = 0;
5330 /* Don't die if an entire brace-pair level is superfluous
5331 in the containing level. */
5332 if (constructor_type == 0)
5334 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5335 || TREE_CODE (constructor_type) == UNION_TYPE)
5337 /* Don't die if there are extra init elts at the end. */
5338 if (constructor_fields == 0)
5339 constructor_type = 0;
5340 else
5342 constructor_type = TREE_TYPE (constructor_fields);
5343 push_member_name (constructor_fields);
5344 constructor_depth++;
5347 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5349 constructor_type = TREE_TYPE (constructor_type);
5350 push_array_bounds (tree_low_cst (constructor_index, 0));
5351 constructor_depth++;
5354 if (constructor_type == 0)
5356 error_init ("extra brace group at end of initializer");
5357 constructor_fields = 0;
5358 constructor_unfilled_fields = 0;
5359 return;
5362 if (value && TREE_CODE (value) == CONSTRUCTOR)
5364 constructor_constant = TREE_CONSTANT (value);
5365 constructor_simple = TREE_STATIC (value);
5366 constructor_elements = CONSTRUCTOR_ELTS (value);
5367 if (constructor_elements
5368 && (TREE_CODE (constructor_type) == RECORD_TYPE
5369 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5370 set_nonincremental_init ();
5373 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5375 missing_braces_mentioned = 1;
5376 warning_init ("missing braces around initializer");
5379 if (TREE_CODE (constructor_type) == RECORD_TYPE
5380 || TREE_CODE (constructor_type) == UNION_TYPE)
5382 constructor_fields = TYPE_FIELDS (constructor_type);
5383 /* Skip any nameless bit fields at the beginning. */
5384 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5385 && DECL_NAME (constructor_fields) == 0)
5386 constructor_fields = TREE_CHAIN (constructor_fields);
5388 constructor_unfilled_fields = constructor_fields;
5389 constructor_bit_index = bitsize_zero_node;
5391 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5393 /* Vectors are like simple fixed-size arrays. */
5394 constructor_max_index =
5395 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5396 constructor_index = convert (bitsizetype, integer_zero_node);
5397 constructor_unfilled_index = constructor_index;
5399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5401 if (TYPE_DOMAIN (constructor_type))
5403 constructor_max_index
5404 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5406 /* Detect non-empty initializations of zero-length arrays. */
5407 if (constructor_max_index == NULL_TREE
5408 && TYPE_SIZE (constructor_type))
5409 constructor_max_index = build_int_2 (-1, -1);
5411 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5412 to initialize VLAs will cause a proper error; avoid tree
5413 checking errors as well by setting a safe value. */
5414 if (constructor_max_index
5415 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5416 constructor_max_index = build_int_2 (-1, -1);
5418 constructor_index
5419 = convert (bitsizetype,
5420 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5422 else
5423 constructor_index = bitsize_zero_node;
5425 constructor_unfilled_index = constructor_index;
5426 if (value && TREE_CODE (value) == STRING_CST)
5428 /* We need to split the char/wchar array into individual
5429 characters, so that we don't have to special case it
5430 everywhere. */
5431 set_nonincremental_init_from_string (value);
5434 else
5436 warning_init ("braces around scalar initializer");
5437 constructor_fields = constructor_type;
5438 constructor_unfilled_fields = constructor_type;
5442 /* At the end of an implicit or explicit brace level,
5443 finish up that level of constructor.
5444 If we were outputting the elements as they are read, return 0
5445 from inner levels (process_init_element ignores that),
5446 but return error_mark_node from the outermost level
5447 (that's what we want to put in DECL_INITIAL).
5448 Otherwise, return a CONSTRUCTOR expression. */
5450 tree
5451 pop_init_level (int implicit)
5453 struct constructor_stack *p;
5454 tree constructor = 0;
5456 if (implicit == 0)
5458 /* When we come to an explicit close brace,
5459 pop any inner levels that didn't have explicit braces. */
5460 while (constructor_stack->implicit)
5461 process_init_element (pop_init_level (1));
5463 if (constructor_range_stack)
5464 abort ();
5467 p = constructor_stack;
5469 /* Error for initializing a flexible array member, or a zero-length
5470 array member in an inappropriate context. */
5471 if (constructor_type && constructor_fields
5472 && TREE_CODE (constructor_type) == ARRAY_TYPE
5473 && TYPE_DOMAIN (constructor_type)
5474 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5476 /* Silently discard empty initializations. The parser will
5477 already have pedwarned for empty brackets. */
5478 if (integer_zerop (constructor_unfilled_index))
5479 constructor_type = NULL_TREE;
5480 else if (! TYPE_SIZE (constructor_type))
5482 if (constructor_depth > 2)
5483 error_init ("initialization of flexible array member in a nested context");
5484 else if (pedantic)
5485 pedwarn_init ("initialization of a flexible array member");
5487 /* We have already issued an error message for the existence
5488 of a flexible array member not at the end of the structure.
5489 Discard the initializer so that we do not abort later. */
5490 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5491 constructor_type = NULL_TREE;
5493 else
5494 /* Zero-length arrays are no longer special, so we should no longer
5495 get here. */
5496 abort ();
5499 /* Warn when some struct elements are implicitly initialized to zero. */
5500 if (extra_warnings
5501 && constructor_type
5502 && TREE_CODE (constructor_type) == RECORD_TYPE
5503 && constructor_unfilled_fields)
5505 /* Do not warn for flexible array members or zero-length arrays. */
5506 while (constructor_unfilled_fields
5507 && (! DECL_SIZE (constructor_unfilled_fields)
5508 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5509 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5511 /* Do not warn if this level of the initializer uses member
5512 designators; it is likely to be deliberate. */
5513 if (constructor_unfilled_fields && !constructor_designated)
5515 push_member_name (constructor_unfilled_fields);
5516 warning_init ("missing initializer");
5517 RESTORE_SPELLING_DEPTH (constructor_depth);
5521 /* Now output all pending elements. */
5522 constructor_incremental = 1;
5523 output_pending_init_elements (1);
5525 /* Pad out the end of the structure. */
5526 if (p->replacement_value)
5527 /* If this closes a superfluous brace pair,
5528 just pass out the element between them. */
5529 constructor = p->replacement_value;
5530 else if (constructor_type == 0)
5532 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5533 && TREE_CODE (constructor_type) != UNION_TYPE
5534 && TREE_CODE (constructor_type) != ARRAY_TYPE
5535 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5537 /* A nonincremental scalar initializer--just return
5538 the element, after verifying there is just one. */
5539 if (constructor_elements == 0)
5541 if (!constructor_erroneous)
5542 error_init ("empty scalar initializer");
5543 constructor = error_mark_node;
5545 else if (TREE_CHAIN (constructor_elements) != 0)
5547 error_init ("extra elements in scalar initializer");
5548 constructor = TREE_VALUE (constructor_elements);
5550 else
5551 constructor = TREE_VALUE (constructor_elements);
5553 else
5555 if (constructor_erroneous)
5556 constructor = error_mark_node;
5557 else
5559 constructor = build_constructor (constructor_type,
5560 nreverse (constructor_elements));
5561 if (constructor_constant)
5562 TREE_CONSTANT (constructor) = 1;
5563 if (constructor_constant && constructor_simple)
5564 TREE_STATIC (constructor) = 1;
5568 constructor_type = p->type;
5569 constructor_fields = p->fields;
5570 constructor_index = p->index;
5571 constructor_max_index = p->max_index;
5572 constructor_unfilled_index = p->unfilled_index;
5573 constructor_unfilled_fields = p->unfilled_fields;
5574 constructor_bit_index = p->bit_index;
5575 constructor_elements = p->elements;
5576 constructor_constant = p->constant;
5577 constructor_simple = p->simple;
5578 constructor_erroneous = p->erroneous;
5579 constructor_incremental = p->incremental;
5580 constructor_designated = p->designated;
5581 constructor_pending_elts = p->pending_elts;
5582 constructor_depth = p->depth;
5583 if (!p->implicit)
5584 constructor_range_stack = p->range_stack;
5585 RESTORE_SPELLING_DEPTH (constructor_depth);
5587 constructor_stack = p->next;
5588 free (p);
5590 if (constructor == 0)
5592 if (constructor_stack == 0)
5593 return error_mark_node;
5594 return NULL_TREE;
5596 return constructor;
5599 /* Common handling for both array range and field name designators.
5600 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5602 static int
5603 set_designator (int array)
5605 tree subtype;
5606 enum tree_code subcode;
5608 /* Don't die if an entire brace-pair level is superfluous
5609 in the containing level. */
5610 if (constructor_type == 0)
5611 return 1;
5613 /* If there were errors in this designator list already, bail out silently. */
5614 if (designator_errorneous)
5615 return 1;
5617 if (!designator_depth)
5619 if (constructor_range_stack)
5620 abort ();
5622 /* Designator list starts at the level of closest explicit
5623 braces. */
5624 while (constructor_stack->implicit)
5625 process_init_element (pop_init_level (1));
5626 constructor_designated = 1;
5627 return 0;
5630 if (constructor_no_implicit)
5632 error_init ("initialization designators may not nest");
5633 return 1;
5636 if (TREE_CODE (constructor_type) == RECORD_TYPE
5637 || TREE_CODE (constructor_type) == UNION_TYPE)
5639 subtype = TREE_TYPE (constructor_fields);
5640 if (subtype != error_mark_node)
5641 subtype = TYPE_MAIN_VARIANT (subtype);
5643 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5645 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5647 else
5648 abort ();
5650 subcode = TREE_CODE (subtype);
5651 if (array && subcode != ARRAY_TYPE)
5653 error_init ("array index in non-array initializer");
5654 return 1;
5656 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5658 error_init ("field name not in record or union initializer");
5659 return 1;
5662 constructor_designated = 1;
5663 push_init_level (2);
5664 return 0;
5667 /* If there are range designators in designator list, push a new designator
5668 to constructor_range_stack. RANGE_END is end of such stack range or
5669 NULL_TREE if there is no range designator at this level. */
5671 static void
5672 push_range_stack (tree range_end)
5674 struct constructor_range_stack *p;
5676 p = (struct constructor_range_stack *)
5677 ggc_alloc (sizeof (struct constructor_range_stack));
5678 p->prev = constructor_range_stack;
5679 p->next = 0;
5680 p->fields = constructor_fields;
5681 p->range_start = constructor_index;
5682 p->index = constructor_index;
5683 p->stack = constructor_stack;
5684 p->range_end = range_end;
5685 if (constructor_range_stack)
5686 constructor_range_stack->next = p;
5687 constructor_range_stack = p;
5690 /* Within an array initializer, specify the next index to be initialized.
5691 FIRST is that index. If LAST is nonzero, then initialize a range
5692 of indices, running from FIRST through LAST. */
5694 void
5695 set_init_index (tree first, tree last)
5697 if (set_designator (1))
5698 return;
5700 designator_errorneous = 1;
5702 while ((TREE_CODE (first) == NOP_EXPR
5703 || TREE_CODE (first) == CONVERT_EXPR
5704 || TREE_CODE (first) == NON_LVALUE_EXPR)
5705 && (TYPE_MODE (TREE_TYPE (first))
5706 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5707 first = TREE_OPERAND (first, 0);
5709 if (last)
5710 while ((TREE_CODE (last) == NOP_EXPR
5711 || TREE_CODE (last) == CONVERT_EXPR
5712 || TREE_CODE (last) == NON_LVALUE_EXPR)
5713 && (TYPE_MODE (TREE_TYPE (last))
5714 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5715 last = TREE_OPERAND (last, 0);
5717 if (TREE_CODE (first) != INTEGER_CST)
5718 error_init ("nonconstant array index in initializer");
5719 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5720 error_init ("nonconstant array index in initializer");
5721 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5722 error_init ("array index in non-array initializer");
5723 else if (constructor_max_index
5724 && tree_int_cst_lt (constructor_max_index, first))
5725 error_init ("array index in initializer exceeds array bounds");
5726 else
5728 constructor_index = convert (bitsizetype, first);
5730 if (last)
5732 if (tree_int_cst_equal (first, last))
5733 last = 0;
5734 else if (tree_int_cst_lt (last, first))
5736 error_init ("empty index range in initializer");
5737 last = 0;
5739 else
5741 last = convert (bitsizetype, last);
5742 if (constructor_max_index != 0
5743 && tree_int_cst_lt (constructor_max_index, last))
5745 error_init ("array index range in initializer exceeds array bounds");
5746 last = 0;
5751 designator_depth++;
5752 designator_errorneous = 0;
5753 if (constructor_range_stack || last)
5754 push_range_stack (last);
5758 /* Within a struct initializer, specify the next field to be initialized. */
5760 void
5761 set_init_label (tree fieldname)
5763 tree tail;
5765 if (set_designator (0))
5766 return;
5768 designator_errorneous = 1;
5770 if (TREE_CODE (constructor_type) != RECORD_TYPE
5771 && TREE_CODE (constructor_type) != UNION_TYPE)
5773 error_init ("field name not in record or union initializer");
5774 return;
5777 for (tail = TYPE_FIELDS (constructor_type); tail;
5778 tail = TREE_CHAIN (tail))
5780 if (DECL_NAME (tail) == fieldname)
5781 break;
5784 if (tail == 0)
5785 error ("unknown field `%s' specified in initializer",
5786 IDENTIFIER_POINTER (fieldname));
5787 else
5789 constructor_fields = tail;
5790 designator_depth++;
5791 designator_errorneous = 0;
5792 if (constructor_range_stack)
5793 push_range_stack (NULL_TREE);
5797 /* Add a new initializer to the tree of pending initializers. PURPOSE
5798 identifies the initializer, either array index or field in a structure.
5799 VALUE is the value of that index or field. */
5801 static void
5802 add_pending_init (tree purpose, tree value)
5804 struct init_node *p, **q, *r;
5806 q = &constructor_pending_elts;
5807 p = 0;
5809 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5811 while (*q != 0)
5813 p = *q;
5814 if (tree_int_cst_lt (purpose, p->purpose))
5815 q = &p->left;
5816 else if (tree_int_cst_lt (p->purpose, purpose))
5817 q = &p->right;
5818 else
5820 if (TREE_SIDE_EFFECTS (p->value))
5821 warning_init ("initialized field with side-effects overwritten");
5822 p->value = value;
5823 return;
5827 else
5829 tree bitpos;
5831 bitpos = bit_position (purpose);
5832 while (*q != NULL)
5834 p = *q;
5835 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5836 q = &p->left;
5837 else if (p->purpose != purpose)
5838 q = &p->right;
5839 else
5841 if (TREE_SIDE_EFFECTS (p->value))
5842 warning_init ("initialized field with side-effects overwritten");
5843 p->value = value;
5844 return;
5849 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5850 r->purpose = purpose;
5851 r->value = value;
5853 *q = r;
5854 r->parent = p;
5855 r->left = 0;
5856 r->right = 0;
5857 r->balance = 0;
5859 while (p)
5861 struct init_node *s;
5863 if (r == p->left)
5865 if (p->balance == 0)
5866 p->balance = -1;
5867 else if (p->balance < 0)
5869 if (r->balance < 0)
5871 /* L rotation. */
5872 p->left = r->right;
5873 if (p->left)
5874 p->left->parent = p;
5875 r->right = p;
5877 p->balance = 0;
5878 r->balance = 0;
5880 s = p->parent;
5881 p->parent = r;
5882 r->parent = s;
5883 if (s)
5885 if (s->left == p)
5886 s->left = r;
5887 else
5888 s->right = r;
5890 else
5891 constructor_pending_elts = r;
5893 else
5895 /* LR rotation. */
5896 struct init_node *t = r->right;
5898 r->right = t->left;
5899 if (r->right)
5900 r->right->parent = r;
5901 t->left = r;
5903 p->left = t->right;
5904 if (p->left)
5905 p->left->parent = p;
5906 t->right = p;
5908 p->balance = t->balance < 0;
5909 r->balance = -(t->balance > 0);
5910 t->balance = 0;
5912 s = p->parent;
5913 p->parent = t;
5914 r->parent = t;
5915 t->parent = s;
5916 if (s)
5918 if (s->left == p)
5919 s->left = t;
5920 else
5921 s->right = t;
5923 else
5924 constructor_pending_elts = t;
5926 break;
5928 else
5930 /* p->balance == +1; growth of left side balances the node. */
5931 p->balance = 0;
5932 break;
5935 else /* r == p->right */
5937 if (p->balance == 0)
5938 /* Growth propagation from right side. */
5939 p->balance++;
5940 else if (p->balance > 0)
5942 if (r->balance > 0)
5944 /* R rotation. */
5945 p->right = r->left;
5946 if (p->right)
5947 p->right->parent = p;
5948 r->left = p;
5950 p->balance = 0;
5951 r->balance = 0;
5953 s = p->parent;
5954 p->parent = r;
5955 r->parent = s;
5956 if (s)
5958 if (s->left == p)
5959 s->left = r;
5960 else
5961 s->right = r;
5963 else
5964 constructor_pending_elts = r;
5966 else /* r->balance == -1 */
5968 /* RL rotation */
5969 struct init_node *t = r->left;
5971 r->left = t->right;
5972 if (r->left)
5973 r->left->parent = r;
5974 t->right = r;
5976 p->right = t->left;
5977 if (p->right)
5978 p->right->parent = p;
5979 t->left = p;
5981 r->balance = (t->balance < 0);
5982 p->balance = -(t->balance > 0);
5983 t->balance = 0;
5985 s = p->parent;
5986 p->parent = t;
5987 r->parent = t;
5988 t->parent = s;
5989 if (s)
5991 if (s->left == p)
5992 s->left = t;
5993 else
5994 s->right = t;
5996 else
5997 constructor_pending_elts = t;
5999 break;
6001 else
6003 /* p->balance == -1; growth of right side balances the node. */
6004 p->balance = 0;
6005 break;
6009 r = p;
6010 p = p->parent;
6014 /* Build AVL tree from a sorted chain. */
6016 static void
6017 set_nonincremental_init (void)
6019 tree chain;
6021 if (TREE_CODE (constructor_type) != RECORD_TYPE
6022 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6023 return;
6025 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6026 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6027 constructor_elements = 0;
6028 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6030 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6031 /* Skip any nameless bit fields at the beginning. */
6032 while (constructor_unfilled_fields != 0
6033 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6034 && DECL_NAME (constructor_unfilled_fields) == 0)
6035 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6038 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6040 if (TYPE_DOMAIN (constructor_type))
6041 constructor_unfilled_index
6042 = convert (bitsizetype,
6043 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6044 else
6045 constructor_unfilled_index = bitsize_zero_node;
6047 constructor_incremental = 0;
6050 /* Build AVL tree from a string constant. */
6052 static void
6053 set_nonincremental_init_from_string (tree str)
6055 tree value, purpose, type;
6056 HOST_WIDE_INT val[2];
6057 const char *p, *end;
6058 int byte, wchar_bytes, charwidth, bitpos;
6060 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6061 abort ();
6063 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6064 == TYPE_PRECISION (char_type_node))
6065 wchar_bytes = 1;
6066 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6067 == TYPE_PRECISION (wchar_type_node))
6068 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6069 else
6070 abort ();
6072 charwidth = TYPE_PRECISION (char_type_node);
6073 type = TREE_TYPE (constructor_type);
6074 p = TREE_STRING_POINTER (str);
6075 end = p + TREE_STRING_LENGTH (str);
6077 for (purpose = bitsize_zero_node;
6078 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6079 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6081 if (wchar_bytes == 1)
6083 val[1] = (unsigned char) *p++;
6084 val[0] = 0;
6086 else
6088 val[0] = 0;
6089 val[1] = 0;
6090 for (byte = 0; byte < wchar_bytes; byte++)
6092 if (BYTES_BIG_ENDIAN)
6093 bitpos = (wchar_bytes - byte - 1) * charwidth;
6094 else
6095 bitpos = byte * charwidth;
6096 val[bitpos < HOST_BITS_PER_WIDE_INT]
6097 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6098 << (bitpos % HOST_BITS_PER_WIDE_INT);
6102 if (!TREE_UNSIGNED (type))
6104 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6105 if (bitpos < HOST_BITS_PER_WIDE_INT)
6107 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6109 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6110 val[0] = -1;
6113 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6115 if (val[1] < 0)
6116 val[0] = -1;
6118 else if (val[0] & (((HOST_WIDE_INT) 1)
6119 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6120 val[0] |= ((HOST_WIDE_INT) -1)
6121 << (bitpos - HOST_BITS_PER_WIDE_INT);
6124 value = build_int_2 (val[1], val[0]);
6125 TREE_TYPE (value) = type;
6126 add_pending_init (purpose, value);
6129 constructor_incremental = 0;
6132 /* Return value of FIELD in pending initializer or zero if the field was
6133 not initialized yet. */
6135 static tree
6136 find_init_member (tree field)
6138 struct init_node *p;
6140 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6142 if (constructor_incremental
6143 && tree_int_cst_lt (field, constructor_unfilled_index))
6144 set_nonincremental_init ();
6146 p = constructor_pending_elts;
6147 while (p)
6149 if (tree_int_cst_lt (field, p->purpose))
6150 p = p->left;
6151 else if (tree_int_cst_lt (p->purpose, field))
6152 p = p->right;
6153 else
6154 return p->value;
6157 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6159 tree bitpos = bit_position (field);
6161 if (constructor_incremental
6162 && (!constructor_unfilled_fields
6163 || tree_int_cst_lt (bitpos,
6164 bit_position (constructor_unfilled_fields))))
6165 set_nonincremental_init ();
6167 p = constructor_pending_elts;
6168 while (p)
6170 if (field == p->purpose)
6171 return p->value;
6172 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6173 p = p->left;
6174 else
6175 p = p->right;
6178 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6180 if (constructor_elements
6181 && TREE_PURPOSE (constructor_elements) == field)
6182 return TREE_VALUE (constructor_elements);
6184 return 0;
6187 /* "Output" the next constructor element.
6188 At top level, really output it to assembler code now.
6189 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6190 TYPE is the data type that the containing data type wants here.
6191 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6193 PENDING if non-nil means output pending elements that belong
6194 right after this element. (PENDING is normally 1;
6195 it is 0 while outputting pending elements, to avoid recursion.) */
6197 static void
6198 output_init_element (tree value, tree type, tree field, int pending)
6200 if (type == error_mark_node)
6202 constructor_erroneous = 1;
6203 return;
6205 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6206 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6207 && !(TREE_CODE (value) == STRING_CST
6208 && TREE_CODE (type) == ARRAY_TYPE
6209 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6210 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6211 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
6212 value = default_conversion (value);
6214 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6215 && require_constant_value && !flag_isoc99 && pending)
6217 /* As an extension, allow initializing objects with static storage
6218 duration with compound literals (which are then treated just as
6219 the brace enclosed list they contain). */
6220 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6221 value = DECL_INITIAL (decl);
6224 if (value == error_mark_node)
6225 constructor_erroneous = 1;
6226 else if (!TREE_CONSTANT (value))
6227 constructor_constant = 0;
6228 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6229 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6230 || TREE_CODE (constructor_type) == UNION_TYPE)
6231 && DECL_C_BIT_FIELD (field)
6232 && TREE_CODE (value) != INTEGER_CST))
6233 constructor_simple = 0;
6235 if (require_constant_value && ! TREE_CONSTANT (value))
6237 error_init ("initializer element is not constant");
6238 value = error_mark_node;
6240 else if (require_constant_elements
6241 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6242 pedwarn ("initializer element is not computable at load time");
6244 /* If this field is empty (and not at the end of structure),
6245 don't do anything other than checking the initializer. */
6246 if (field
6247 && (TREE_TYPE (field) == error_mark_node
6248 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6249 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6250 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6251 || TREE_CHAIN (field)))))
6252 return;
6254 value = digest_init (type, value, require_constant_value);
6255 if (value == error_mark_node)
6257 constructor_erroneous = 1;
6258 return;
6261 /* If this element doesn't come next in sequence,
6262 put it on constructor_pending_elts. */
6263 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6264 && (!constructor_incremental
6265 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6267 if (constructor_incremental
6268 && tree_int_cst_lt (field, constructor_unfilled_index))
6269 set_nonincremental_init ();
6271 add_pending_init (field, value);
6272 return;
6274 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6275 && (!constructor_incremental
6276 || field != constructor_unfilled_fields))
6278 /* We do this for records but not for unions. In a union,
6279 no matter which field is specified, it can be initialized
6280 right away since it starts at the beginning of the union. */
6281 if (constructor_incremental)
6283 if (!constructor_unfilled_fields)
6284 set_nonincremental_init ();
6285 else
6287 tree bitpos, unfillpos;
6289 bitpos = bit_position (field);
6290 unfillpos = bit_position (constructor_unfilled_fields);
6292 if (tree_int_cst_lt (bitpos, unfillpos))
6293 set_nonincremental_init ();
6297 add_pending_init (field, value);
6298 return;
6300 else if (TREE_CODE (constructor_type) == UNION_TYPE
6301 && constructor_elements)
6303 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6304 warning_init ("initialized field with side-effects overwritten");
6306 /* We can have just one union field set. */
6307 constructor_elements = 0;
6310 /* Otherwise, output this element either to
6311 constructor_elements or to the assembler file. */
6313 if (field && TREE_CODE (field) == INTEGER_CST)
6314 field = copy_node (field);
6315 constructor_elements
6316 = tree_cons (field, value, constructor_elements);
6318 /* Advance the variable that indicates sequential elements output. */
6319 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6320 constructor_unfilled_index
6321 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6322 bitsize_one_node);
6323 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6325 constructor_unfilled_fields
6326 = TREE_CHAIN (constructor_unfilled_fields);
6328 /* Skip any nameless bit fields. */
6329 while (constructor_unfilled_fields != 0
6330 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6331 && DECL_NAME (constructor_unfilled_fields) == 0)
6332 constructor_unfilled_fields =
6333 TREE_CHAIN (constructor_unfilled_fields);
6335 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6336 constructor_unfilled_fields = 0;
6338 /* Now output any pending elements which have become next. */
6339 if (pending)
6340 output_pending_init_elements (0);
6343 /* Output any pending elements which have become next.
6344 As we output elements, constructor_unfilled_{fields,index}
6345 advances, which may cause other elements to become next;
6346 if so, they too are output.
6348 If ALL is 0, we return when there are
6349 no more pending elements to output now.
6351 If ALL is 1, we output space as necessary so that
6352 we can output all the pending elements. */
6354 static void
6355 output_pending_init_elements (int all)
6357 struct init_node *elt = constructor_pending_elts;
6358 tree next;
6360 retry:
6362 /* Look thru the whole pending tree.
6363 If we find an element that should be output now,
6364 output it. Otherwise, set NEXT to the element
6365 that comes first among those still pending. */
6367 next = 0;
6368 while (elt)
6370 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6372 if (tree_int_cst_equal (elt->purpose,
6373 constructor_unfilled_index))
6374 output_init_element (elt->value,
6375 TREE_TYPE (constructor_type),
6376 constructor_unfilled_index, 0);
6377 else if (tree_int_cst_lt (constructor_unfilled_index,
6378 elt->purpose))
6380 /* Advance to the next smaller node. */
6381 if (elt->left)
6382 elt = elt->left;
6383 else
6385 /* We have reached the smallest node bigger than the
6386 current unfilled index. Fill the space first. */
6387 next = elt->purpose;
6388 break;
6391 else
6393 /* Advance to the next bigger node. */
6394 if (elt->right)
6395 elt = elt->right;
6396 else
6398 /* We have reached the biggest node in a subtree. Find
6399 the parent of it, which is the next bigger node. */
6400 while (elt->parent && elt->parent->right == elt)
6401 elt = elt->parent;
6402 elt = elt->parent;
6403 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6404 elt->purpose))
6406 next = elt->purpose;
6407 break;
6412 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6413 || TREE_CODE (constructor_type) == UNION_TYPE)
6415 tree ctor_unfilled_bitpos, elt_bitpos;
6417 /* If the current record is complete we are done. */
6418 if (constructor_unfilled_fields == 0)
6419 break;
6421 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6422 elt_bitpos = bit_position (elt->purpose);
6423 /* We can't compare fields here because there might be empty
6424 fields in between. */
6425 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6427 constructor_unfilled_fields = elt->purpose;
6428 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6429 elt->purpose, 0);
6431 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6433 /* Advance to the next smaller node. */
6434 if (elt->left)
6435 elt = elt->left;
6436 else
6438 /* We have reached the smallest node bigger than the
6439 current unfilled field. Fill the space first. */
6440 next = elt->purpose;
6441 break;
6444 else
6446 /* Advance to the next bigger node. */
6447 if (elt->right)
6448 elt = elt->right;
6449 else
6451 /* We have reached the biggest node in a subtree. Find
6452 the parent of it, which is the next bigger node. */
6453 while (elt->parent && elt->parent->right == elt)
6454 elt = elt->parent;
6455 elt = elt->parent;
6456 if (elt
6457 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6458 bit_position (elt->purpose))))
6460 next = elt->purpose;
6461 break;
6468 /* Ordinarily return, but not if we want to output all
6469 and there are elements left. */
6470 if (! (all && next != 0))
6471 return;
6473 /* If it's not incremental, just skip over the gap, so that after
6474 jumping to retry we will output the next successive element. */
6475 if (TREE_CODE (constructor_type) == RECORD_TYPE
6476 || TREE_CODE (constructor_type) == UNION_TYPE)
6477 constructor_unfilled_fields = next;
6478 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6479 constructor_unfilled_index = next;
6481 /* ELT now points to the node in the pending tree with the next
6482 initializer to output. */
6483 goto retry;
6486 /* Add one non-braced element to the current constructor level.
6487 This adjusts the current position within the constructor's type.
6488 This may also start or terminate implicit levels
6489 to handle a partly-braced initializer.
6491 Once this has found the correct level for the new element,
6492 it calls output_init_element. */
6494 void
6495 process_init_element (tree value)
6497 tree orig_value = value;
6498 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6500 designator_depth = 0;
6501 designator_errorneous = 0;
6503 /* Handle superfluous braces around string cst as in
6504 char x[] = {"foo"}; */
6505 if (string_flag
6506 && constructor_type
6507 && TREE_CODE (constructor_type) == ARRAY_TYPE
6508 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6509 && integer_zerop (constructor_unfilled_index))
6511 if (constructor_stack->replacement_value)
6512 error_init ("excess elements in char array initializer");
6513 constructor_stack->replacement_value = value;
6514 return;
6517 if (constructor_stack->replacement_value != 0)
6519 error_init ("excess elements in struct initializer");
6520 return;
6523 /* Ignore elements of a brace group if it is entirely superfluous
6524 and has already been diagnosed. */
6525 if (constructor_type == 0)
6526 return;
6528 /* If we've exhausted any levels that didn't have braces,
6529 pop them now. */
6530 while (constructor_stack->implicit)
6532 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6533 || TREE_CODE (constructor_type) == UNION_TYPE)
6534 && constructor_fields == 0)
6535 process_init_element (pop_init_level (1));
6536 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6537 && (constructor_max_index == 0
6538 || tree_int_cst_lt (constructor_max_index,
6539 constructor_index)))
6540 process_init_element (pop_init_level (1));
6541 else
6542 break;
6545 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6546 if (constructor_range_stack)
6548 /* If value is a compound literal and we'll be just using its
6549 content, don't put it into a SAVE_EXPR. */
6550 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6551 || !require_constant_value
6552 || flag_isoc99)
6553 value = save_expr (value);
6556 while (1)
6558 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6560 tree fieldtype;
6561 enum tree_code fieldcode;
6563 if (constructor_fields == 0)
6565 pedwarn_init ("excess elements in struct initializer");
6566 break;
6569 fieldtype = TREE_TYPE (constructor_fields);
6570 if (fieldtype != error_mark_node)
6571 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6572 fieldcode = TREE_CODE (fieldtype);
6574 /* Error for non-static initialization of a flexible array member. */
6575 if (fieldcode == ARRAY_TYPE
6576 && !require_constant_value
6577 && TYPE_SIZE (fieldtype) == NULL_TREE
6578 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6580 error_init ("non-static initialization of a flexible array member");
6581 break;
6584 /* Accept a string constant to initialize a subarray. */
6585 if (value != 0
6586 && fieldcode == ARRAY_TYPE
6587 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6588 && string_flag)
6589 value = orig_value;
6590 /* Otherwise, if we have come to a subaggregate,
6591 and we don't have an element of its type, push into it. */
6592 else if (value != 0 && !constructor_no_implicit
6593 && value != error_mark_node
6594 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6595 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6596 || fieldcode == UNION_TYPE))
6598 push_init_level (1);
6599 continue;
6602 if (value)
6604 push_member_name (constructor_fields);
6605 output_init_element (value, fieldtype, constructor_fields, 1);
6606 RESTORE_SPELLING_DEPTH (constructor_depth);
6608 else
6609 /* Do the bookkeeping for an element that was
6610 directly output as a constructor. */
6612 /* For a record, keep track of end position of last field. */
6613 if (DECL_SIZE (constructor_fields))
6614 constructor_bit_index
6615 = size_binop (PLUS_EXPR,
6616 bit_position (constructor_fields),
6617 DECL_SIZE (constructor_fields));
6619 /* If the current field was the first one not yet written out,
6620 it isn't now, so update. */
6621 if (constructor_unfilled_fields == constructor_fields)
6623 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6624 /* Skip any nameless bit fields. */
6625 while (constructor_unfilled_fields != 0
6626 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6627 && DECL_NAME (constructor_unfilled_fields) == 0)
6628 constructor_unfilled_fields =
6629 TREE_CHAIN (constructor_unfilled_fields);
6633 constructor_fields = TREE_CHAIN (constructor_fields);
6634 /* Skip any nameless bit fields at the beginning. */
6635 while (constructor_fields != 0
6636 && DECL_C_BIT_FIELD (constructor_fields)
6637 && DECL_NAME (constructor_fields) == 0)
6638 constructor_fields = TREE_CHAIN (constructor_fields);
6640 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6642 tree fieldtype;
6643 enum tree_code fieldcode;
6645 if (constructor_fields == 0)
6647 pedwarn_init ("excess elements in union initializer");
6648 break;
6651 fieldtype = TREE_TYPE (constructor_fields);
6652 if (fieldtype != error_mark_node)
6653 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6654 fieldcode = TREE_CODE (fieldtype);
6656 /* Warn that traditional C rejects initialization of unions.
6657 We skip the warning if the value is zero. This is done
6658 under the assumption that the zero initializer in user
6659 code appears conditioned on e.g. __STDC__ to avoid
6660 "missing initializer" warnings and relies on default
6661 initialization to zero in the traditional C case.
6662 We also skip the warning if the initializer is designated,
6663 again on the assumption that this must be conditional on
6664 __STDC__ anyway (and we've already complained about the
6665 member-designator already). */
6666 if (warn_traditional && !in_system_header && !constructor_designated
6667 && !(value && (integer_zerop (value) || real_zerop (value))))
6668 warning ("traditional C rejects initialization of unions");
6670 /* Accept a string constant to initialize a subarray. */
6671 if (value != 0
6672 && fieldcode == ARRAY_TYPE
6673 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6674 && string_flag)
6675 value = orig_value;
6676 /* Otherwise, if we have come to a subaggregate,
6677 and we don't have an element of its type, push into it. */
6678 else if (value != 0 && !constructor_no_implicit
6679 && value != error_mark_node
6680 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6681 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6682 || fieldcode == UNION_TYPE))
6684 push_init_level (1);
6685 continue;
6688 if (value)
6690 push_member_name (constructor_fields);
6691 output_init_element (value, fieldtype, constructor_fields, 1);
6692 RESTORE_SPELLING_DEPTH (constructor_depth);
6694 else
6695 /* Do the bookkeeping for an element that was
6696 directly output as a constructor. */
6698 constructor_bit_index = DECL_SIZE (constructor_fields);
6699 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6702 constructor_fields = 0;
6704 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6706 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6707 enum tree_code eltcode = TREE_CODE (elttype);
6709 /* Accept a string constant to initialize a subarray. */
6710 if (value != 0
6711 && eltcode == ARRAY_TYPE
6712 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6713 && string_flag)
6714 value = orig_value;
6715 /* Otherwise, if we have come to a subaggregate,
6716 and we don't have an element of its type, push into it. */
6717 else if (value != 0 && !constructor_no_implicit
6718 && value != error_mark_node
6719 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6720 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6721 || eltcode == UNION_TYPE))
6723 push_init_level (1);
6724 continue;
6727 if (constructor_max_index != 0
6728 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6729 || integer_all_onesp (constructor_max_index)))
6731 pedwarn_init ("excess elements in array initializer");
6732 break;
6735 /* Now output the actual element. */
6736 if (value)
6738 push_array_bounds (tree_low_cst (constructor_index, 0));
6739 output_init_element (value, elttype, constructor_index, 1);
6740 RESTORE_SPELLING_DEPTH (constructor_depth);
6743 constructor_index
6744 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6746 if (! value)
6747 /* If we are doing the bookkeeping for an element that was
6748 directly output as a constructor, we must update
6749 constructor_unfilled_index. */
6750 constructor_unfilled_index = constructor_index;
6752 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6754 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6756 /* Do a basic check of initializer size. Note that vectors
6757 always have a fixed size derived from their type. */
6758 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6760 pedwarn_init ("excess elements in vector initializer");
6761 break;
6764 /* Now output the actual element. */
6765 if (value)
6766 output_init_element (value, elttype, constructor_index, 1);
6768 constructor_index
6769 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6771 if (! value)
6772 /* If we are doing the bookkeeping for an element that was
6773 directly output as a constructor, we must update
6774 constructor_unfilled_index. */
6775 constructor_unfilled_index = constructor_index;
6778 /* Handle the sole element allowed in a braced initializer
6779 for a scalar variable. */
6780 else if (constructor_fields == 0)
6782 pedwarn_init ("excess elements in scalar initializer");
6783 break;
6785 else
6787 if (value)
6788 output_init_element (value, constructor_type, NULL_TREE, 1);
6789 constructor_fields = 0;
6792 /* Handle range initializers either at this level or anywhere higher
6793 in the designator stack. */
6794 if (constructor_range_stack)
6796 struct constructor_range_stack *p, *range_stack;
6797 int finish = 0;
6799 range_stack = constructor_range_stack;
6800 constructor_range_stack = 0;
6801 while (constructor_stack != range_stack->stack)
6803 if (!constructor_stack->implicit)
6804 abort ();
6805 process_init_element (pop_init_level (1));
6807 for (p = range_stack;
6808 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6809 p = p->prev)
6811 if (!constructor_stack->implicit)
6812 abort ();
6813 process_init_element (pop_init_level (1));
6816 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6817 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6818 finish = 1;
6820 while (1)
6822 constructor_index = p->index;
6823 constructor_fields = p->fields;
6824 if (finish && p->range_end && p->index == p->range_start)
6826 finish = 0;
6827 p->prev = 0;
6829 p = p->next;
6830 if (!p)
6831 break;
6832 push_init_level (2);
6833 p->stack = constructor_stack;
6834 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6835 p->index = p->range_start;
6838 if (!finish)
6839 constructor_range_stack = range_stack;
6840 continue;
6843 break;
6846 constructor_range_stack = 0;
6849 /* Build a simple asm-statement, from one string literal. */
6850 tree
6851 simple_asm_stmt (tree expr)
6853 STRIP_NOPS (expr);
6855 if (TREE_CODE (expr) == ADDR_EXPR)
6856 expr = TREE_OPERAND (expr, 0);
6858 if (TREE_CODE (expr) == STRING_CST)
6860 tree stmt;
6862 /* Simple asm statements are treated as volatile. */
6863 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6864 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6865 ASM_INPUT_P (stmt) = 1;
6866 return stmt;
6869 error ("argument of `asm' is not a constant string");
6870 return NULL_TREE;
6873 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6874 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6876 tree
6877 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6878 tree clobbers)
6880 tree tail;
6882 if (TREE_CODE (string) != STRING_CST)
6884 error ("asm template is not a string constant");
6885 return NULL_TREE;
6888 if (cv_qualifier != NULL_TREE
6889 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6891 warning ("%s qualifier ignored on asm",
6892 IDENTIFIER_POINTER (cv_qualifier));
6893 cv_qualifier = NULL_TREE;
6896 /* We can remove output conversions that change the type,
6897 but not the mode. */
6898 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6900 tree output = TREE_VALUE (tail);
6902 STRIP_NOPS (output);
6903 TREE_VALUE (tail) = output;
6905 /* Allow conversions as LHS here. build_modify_expr as called below
6906 will do the right thing with them. */
6907 while (TREE_CODE (output) == NOP_EXPR
6908 || TREE_CODE (output) == CONVERT_EXPR
6909 || TREE_CODE (output) == FLOAT_EXPR
6910 || TREE_CODE (output) == FIX_TRUNC_EXPR
6911 || TREE_CODE (output) == FIX_FLOOR_EXPR
6912 || TREE_CODE (output) == FIX_ROUND_EXPR
6913 || TREE_CODE (output) == FIX_CEIL_EXPR)
6914 output = TREE_OPERAND (output, 0);
6916 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6919 /* Remove output conversions that change the type but not the mode. */
6920 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6922 tree output = TREE_VALUE (tail);
6923 STRIP_NOPS (output);
6924 TREE_VALUE (tail) = output;
6927 /* Perform default conversions on array and function inputs.
6928 Don't do this for other types as it would screw up operands
6929 expected to be in memory. */
6930 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6931 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6933 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6934 outputs, inputs, clobbers));
6937 /* Expand an ASM statement with operands, handling output operands
6938 that are not variables or INDIRECT_REFS by transforming such
6939 cases into cases that expand_asm_operands can handle.
6941 Arguments are same as for expand_asm_operands. */
6943 void
6944 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6945 tree clobbers, int vol, const char *filename,
6946 int line)
6948 int noutputs = list_length (outputs);
6949 int i;
6950 /* o[I] is the place that output number I should be written. */
6951 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6952 tree tail;
6954 /* Record the contents of OUTPUTS before it is modified. */
6955 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6957 o[i] = TREE_VALUE (tail);
6958 if (o[i] == error_mark_node)
6959 return;
6962 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6963 OUTPUTS some trees for where the values were actually stored. */
6964 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6966 /* Copy all the intermediate outputs into the specified outputs. */
6967 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6969 if (o[i] != TREE_VALUE (tail))
6971 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6972 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6973 free_temp_slots ();
6975 /* Restore the original value so that it's correct the next
6976 time we expand this function. */
6977 TREE_VALUE (tail) = o[i];
6979 /* Detect modification of read-only values.
6980 (Otherwise done by build_modify_expr.) */
6981 else
6983 tree type = TREE_TYPE (o[i]);
6984 if (TREE_READONLY (o[i])
6985 || TYPE_READONLY (type)
6986 || ((TREE_CODE (type) == RECORD_TYPE
6987 || TREE_CODE (type) == UNION_TYPE)
6988 && C_TYPE_FIELDS_READONLY (type)))
6989 readonly_warning (o[i], "modification by `asm'");
6993 /* Those MODIFY_EXPRs could do autoincrements. */
6994 emit_queue ();
6997 /* Expand a C `return' statement.
6998 RETVAL is the expression for what to return,
6999 or a null pointer for `return;' with no value. */
7001 tree
7002 c_expand_return (tree retval)
7004 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7006 if (TREE_THIS_VOLATILE (current_function_decl))
7007 warning ("function declared `noreturn' has a `return' statement");
7009 if (!retval)
7011 current_function_returns_null = 1;
7012 if ((warn_return_type || flag_isoc99)
7013 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7014 pedwarn_c99 ("`return' with no value, in function returning non-void");
7016 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7018 current_function_returns_null = 1;
7019 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7020 pedwarn ("`return' with a value, in function returning void");
7022 else
7024 tree t = convert_for_assignment (valtype, retval, _("return"),
7025 NULL_TREE, NULL_TREE, 0);
7026 tree res = DECL_RESULT (current_function_decl);
7027 tree inner;
7029 current_function_returns_value = 1;
7030 if (t == error_mark_node)
7031 return NULL_TREE;
7033 inner = t = convert (TREE_TYPE (res), t);
7035 /* Strip any conversions, additions, and subtractions, and see if
7036 we are returning the address of a local variable. Warn if so. */
7037 while (1)
7039 switch (TREE_CODE (inner))
7041 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7042 case PLUS_EXPR:
7043 inner = TREE_OPERAND (inner, 0);
7044 continue;
7046 case MINUS_EXPR:
7047 /* If the second operand of the MINUS_EXPR has a pointer
7048 type (or is converted from it), this may be valid, so
7049 don't give a warning. */
7051 tree op1 = TREE_OPERAND (inner, 1);
7053 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7054 && (TREE_CODE (op1) == NOP_EXPR
7055 || TREE_CODE (op1) == NON_LVALUE_EXPR
7056 || TREE_CODE (op1) == CONVERT_EXPR))
7057 op1 = TREE_OPERAND (op1, 0);
7059 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7060 break;
7062 inner = TREE_OPERAND (inner, 0);
7063 continue;
7066 case ADDR_EXPR:
7067 inner = TREE_OPERAND (inner, 0);
7069 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7070 inner = TREE_OPERAND (inner, 0);
7072 if (TREE_CODE (inner) == VAR_DECL
7073 && ! DECL_EXTERNAL (inner)
7074 && ! TREE_STATIC (inner)
7075 && DECL_CONTEXT (inner) == current_function_decl)
7076 warning ("function returns address of local variable");
7077 break;
7079 default:
7080 break;
7083 break;
7086 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7089 return add_stmt (build_return_stmt (retval));
7092 struct c_switch {
7093 /* The SWITCH_STMT being built. */
7094 tree switch_stmt;
7095 /* A splay-tree mapping the low element of a case range to the high
7096 element, or NULL_TREE if there is no high element. Used to
7097 determine whether or not a new case label duplicates an old case
7098 label. We need a tree, rather than simply a hash table, because
7099 of the GNU case range extension. */
7100 splay_tree cases;
7101 /* The next node on the stack. */
7102 struct c_switch *next;
7105 /* A stack of the currently active switch statements. The innermost
7106 switch statement is on the top of the stack. There is no need to
7107 mark the stack for garbage collection because it is only active
7108 during the processing of the body of a function, and we never
7109 collect at that point. */
7111 static struct c_switch *switch_stack;
7113 /* Start a C switch statement, testing expression EXP. Return the new
7114 SWITCH_STMT. */
7116 tree
7117 c_start_case (tree exp)
7119 enum tree_code code;
7120 tree type, orig_type = error_mark_node;
7121 struct c_switch *cs;
7123 if (exp != error_mark_node)
7125 code = TREE_CODE (TREE_TYPE (exp));
7126 orig_type = TREE_TYPE (exp);
7128 if (! INTEGRAL_TYPE_P (orig_type)
7129 && code != ERROR_MARK)
7131 error ("switch quantity not an integer");
7132 exp = integer_zero_node;
7134 else
7136 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7138 if (warn_traditional && !in_system_header
7139 && (type == long_integer_type_node
7140 || type == long_unsigned_type_node))
7141 warning ("`long' switch expression not converted to `int' in ISO C");
7143 exp = default_conversion (exp);
7144 type = TREE_TYPE (exp);
7148 /* Add this new SWITCH_STMT to the stack. */
7149 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7150 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7151 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7152 cs->next = switch_stack;
7153 switch_stack = cs;
7155 return add_stmt (switch_stack->switch_stmt);
7158 /* Process a case label. */
7160 tree
7161 do_case (tree low_value, tree high_value)
7163 tree label = NULL_TREE;
7165 if (switch_stack)
7167 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7169 label = c_add_case_label (switch_stack->cases,
7170 SWITCH_COND (switch_stack->switch_stmt),
7171 low_value, high_value);
7172 if (label == error_mark_node)
7173 label = NULL_TREE;
7174 else if (switch_was_empty_p)
7176 /* Attach the first case label to the SWITCH_BODY. */
7177 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7178 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7181 else if (low_value)
7182 error ("case label not within a switch statement");
7183 else
7184 error ("`default' label not within a switch statement");
7186 return label;
7189 /* Finish the switch statement. */
7191 void
7192 c_finish_case (void)
7194 struct c_switch *cs = switch_stack;
7196 /* Rechain the next statements to the SWITCH_STMT. */
7197 last_tree = cs->switch_stmt;
7199 /* Pop the stack. */
7200 switch_stack = switch_stack->next;
7201 splay_tree_delete (cs->cases);
7202 free (cs);