2003-12-21 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / gcc / c-typeck.c
blobeee6d5517835fa33d702c0721696b324acb3aa4b
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static tree qualify_type (tree, tree);
53 static int same_translation_unit_p (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree unary_complex_lvalue (enum tree_code, tree, int);
64 static void pedantic_lvalue_warning (enum tree_code);
65 static tree internal_build_compound_expr (tree, int);
66 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
67 int);
68 static void warn_for_assignment (const char *, const char *, tree, int);
69 static tree valid_compound_expr_initializer (tree, tree);
70 static void push_string (const char *);
71 static void push_member_name (tree);
72 static void push_array_bounds (int);
73 static int spelling_length (void);
74 static char *print_spelling (char *);
75 static void warning_init (const char *);
76 static tree digest_init (tree, tree, int);
77 static void output_init_element (tree, tree, tree, int);
78 static void output_pending_init_elements (int);
79 static int set_designator (int);
80 static void push_range_stack (tree);
81 static void add_pending_init (tree, tree);
82 static void set_nonincremental_init (void);
83 static void set_nonincremental_init_from_string (tree);
84 static tree find_init_member (tree);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
89 tree
90 require_complete_type (tree value)
92 tree type = TREE_TYPE (value);
94 if (value == error_mark_node || type == error_mark_node)
95 return error_mark_node;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type))
99 return value;
101 c_incomplete_type_error (value, type);
102 return error_mark_node;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
109 void
110 c_incomplete_type_error (tree value, tree type)
112 const char *type_code_string;
114 /* Avoid duplicate error message. */
115 if (TREE_CODE (type) == ERROR_MARK)
116 return;
118 if (value != 0 && (TREE_CODE (value) == VAR_DECL
119 || TREE_CODE (value) == PARM_DECL))
120 error ("`%s' has an incomplete type",
121 IDENTIFIER_POINTER (DECL_NAME (value)));
122 else
124 retry:
125 /* We must print an error message. Be clever about what it says. */
127 switch (TREE_CODE (type))
129 case RECORD_TYPE:
130 type_code_string = "struct";
131 break;
133 case UNION_TYPE:
134 type_code_string = "union";
135 break;
137 case ENUMERAL_TYPE:
138 type_code_string = "enum";
139 break;
141 case VOID_TYPE:
142 error ("invalid use of void expression");
143 return;
145 case ARRAY_TYPE:
146 if (TYPE_DOMAIN (type))
148 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
150 error ("invalid use of flexible array member");
151 return;
153 type = TREE_TYPE (type);
154 goto retry;
156 error ("invalid use of array with unspecified bounds");
157 return;
159 default:
160 abort ();
163 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
164 error ("invalid use of undefined type `%s %s'",
165 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
166 else
167 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
168 error ("invalid use of incomplete typedef `%s'",
169 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
173 /* Given a type, apply default promotions wrt unnamed function
174 arguments and return the new type. */
176 tree
177 c_type_promotes_to (tree type)
179 if (TYPE_MAIN_VARIANT (type) == float_type_node)
180 return double_type_node;
182 if (c_promoting_integer_type_p (type))
184 /* Preserve unsignedness if not really getting any wider. */
185 if (TREE_UNSIGNED (type)
186 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
187 return unsigned_type_node;
188 return integer_type_node;
191 return type;
194 /* Return a variant of TYPE which has all the type qualifiers of LIKE
195 as well as those of TYPE. */
197 static tree
198 qualify_type (tree type, tree like)
200 return c_build_qualified_type (type,
201 TYPE_QUALS (type) | TYPE_QUALS (like));
204 /* Return the common type of two types.
205 We assume that comptypes has already been done and returned 1;
206 if that isn't so, this may crash. In particular, we assume that qualifiers
207 match.
209 This is the type for the result of most arithmetic operations
210 if the operands have the given two types. */
212 tree
213 common_type (tree t1, tree t2)
215 enum tree_code code1;
216 enum tree_code code2;
217 tree attributes;
219 /* Save time if the two types are the same. */
221 if (t1 == t2) return t1;
223 /* If one type is nonsense, use the other. */
224 if (t1 == error_mark_node)
225 return t2;
226 if (t2 == error_mark_node)
227 return t1;
229 /* Merge the attributes. */
230 attributes = (*targetm.merge_type_attributes) (t1, t2);
232 /* Treat an enum type as the unsigned integer type of the same width. */
234 if (TREE_CODE (t1) == ENUMERAL_TYPE)
235 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
236 if (TREE_CODE (t2) == ENUMERAL_TYPE)
237 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
239 code1 = TREE_CODE (t1);
240 code2 = TREE_CODE (t2);
242 /* If one type is complex, form the common type of the non-complex
243 components, then make that complex. Use T1 or T2 if it is the
244 required type. */
245 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
247 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
248 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
249 tree subtype = common_type (subtype1, subtype2);
251 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
252 return build_type_attribute_variant (t1, attributes);
253 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
254 return build_type_attribute_variant (t2, attributes);
255 else
256 return build_type_attribute_variant (build_complex_type (subtype),
257 attributes);
260 switch (code1)
262 case INTEGER_TYPE:
263 case REAL_TYPE:
264 /* If only one is real, use it as the result. */
266 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
267 return build_type_attribute_variant (t1, attributes);
269 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
270 return build_type_attribute_variant (t2, attributes);
272 /* Both real or both integers; use the one with greater precision. */
274 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
275 return build_type_attribute_variant (t1, attributes);
276 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
277 return build_type_attribute_variant (t2, attributes);
279 /* Same precision. Prefer longs to ints even when same size. */
281 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
283 return build_type_attribute_variant (long_unsigned_type_node,
284 attributes);
286 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
289 /* But preserve unsignedness from the other type,
290 since long cannot hold all the values of an unsigned int. */
291 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
292 t1 = long_unsigned_type_node;
293 else
294 t1 = long_integer_type_node;
295 return build_type_attribute_variant (t1, attributes);
298 /* Likewise, prefer long double to double even if same size. */
299 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
300 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
301 return build_type_attribute_variant (long_double_type_node,
302 attributes);
304 /* Otherwise prefer the unsigned one. */
306 if (TREE_UNSIGNED (t1))
307 return build_type_attribute_variant (t1, attributes);
308 else
309 return build_type_attribute_variant (t2, attributes);
311 case POINTER_TYPE:
312 /* For two pointers, do this recursively on the target type,
313 and combine the qualifiers of the two types' targets. */
314 /* This code was turned off; I don't know why.
315 But ANSI C specifies doing this with the qualifiers.
316 So I turned it on again. */
318 tree pointed_to_1 = TREE_TYPE (t1);
319 tree pointed_to_2 = TREE_TYPE (t2);
320 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
321 TYPE_MAIN_VARIANT (pointed_to_2));
322 t1 = build_pointer_type (c_build_qualified_type
323 (target,
324 TYPE_QUALS (pointed_to_1) |
325 TYPE_QUALS (pointed_to_2)));
326 return build_type_attribute_variant (t1, attributes);
329 case ARRAY_TYPE:
331 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
332 /* Save space: see if the result is identical to one of the args. */
333 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
334 return build_type_attribute_variant (t1, attributes);
335 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
336 return build_type_attribute_variant (t2, attributes);
337 /* Merge the element types, and have a size if either arg has one. */
338 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
339 return build_type_attribute_variant (t1, attributes);
342 case FUNCTION_TYPE:
343 /* Function types: prefer the one that specified arg types.
344 If both do, merge the arg types. Also merge the return types. */
346 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
347 tree p1 = TYPE_ARG_TYPES (t1);
348 tree p2 = TYPE_ARG_TYPES (t2);
349 int len;
350 tree newargs, n;
351 int i;
353 /* Save space: see if the result is identical to one of the args. */
354 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
355 return build_type_attribute_variant (t1, attributes);
356 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Simple way if one arg fails to specify argument types. */
360 if (TYPE_ARG_TYPES (t1) == 0)
362 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
363 return build_type_attribute_variant (t1, attributes);
365 if (TYPE_ARG_TYPES (t2) == 0)
367 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
368 return build_type_attribute_variant (t1, attributes);
371 /* If both args specify argument types, we must merge the two
372 lists, argument by argument. */
374 pushlevel (0);
375 declare_parm_level ();
377 len = list_length (p1);
378 newargs = 0;
380 for (i = 0; i < len; i++)
381 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
383 n = newargs;
385 for (; p1;
386 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
388 /* A null type means arg type is not specified.
389 Take whatever the other function type has. */
390 if (TREE_VALUE (p1) == 0)
392 TREE_VALUE (n) = TREE_VALUE (p2);
393 goto parm_done;
395 if (TREE_VALUE (p2) == 0)
397 TREE_VALUE (n) = TREE_VALUE (p1);
398 goto parm_done;
401 /* Given wait (union {union wait *u; int *i} *)
402 and wait (union wait *),
403 prefer union wait * as type of parm. */
404 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
405 && TREE_VALUE (p1) != TREE_VALUE (p2))
407 tree memb;
408 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
409 memb; memb = TREE_CHAIN (memb))
410 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
411 COMPARE_STRICT))
413 TREE_VALUE (n) = TREE_VALUE (p2);
414 if (pedantic)
415 pedwarn ("function types not truly compatible in ISO C");
416 goto parm_done;
419 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
420 && TREE_VALUE (p2) != TREE_VALUE (p1))
422 tree memb;
423 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
424 memb; memb = TREE_CHAIN (memb))
425 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
426 COMPARE_STRICT))
428 TREE_VALUE (n) = TREE_VALUE (p1);
429 if (pedantic)
430 pedwarn ("function types not truly compatible in ISO C");
431 goto parm_done;
434 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
435 parm_done: ;
438 poplevel (0, 0, 0);
440 t1 = build_function_type (valtype, newargs);
441 /* ... falls through ... */
444 default:
445 return build_type_attribute_variant (t1, attributes);
450 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
451 or various other operations. Return 2 if they are compatible
452 but a warning may be needed if you use them together. */
455 comptypes (tree type1, tree type2, int flags)
457 tree t1 = type1;
458 tree t2 = type2;
459 int attrval, val;
461 /* Suppress errors caused by previously reported errors. */
463 if (t1 == t2 || !t1 || !t2
464 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
465 return 1;
467 /* If either type is the internal version of sizetype, return the
468 language version. */
469 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
470 && TYPE_DOMAIN (t1) != 0)
471 t1 = TYPE_DOMAIN (t1);
473 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
474 && TYPE_DOMAIN (t2) != 0)
475 t2 = TYPE_DOMAIN (t2);
477 /* Treat an enum type as the integer type of the same width and
478 signedness. */
480 if (TREE_CODE (t1) == ENUMERAL_TYPE)
481 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482 if (TREE_CODE (t2) == ENUMERAL_TYPE)
483 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
485 if (t1 == t2)
486 return 1;
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
495 return 0;
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
502 return 1;
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
506 return 0;
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 val = 0;
511 switch (TREE_CODE (t1))
513 case POINTER_TYPE:
514 /* We must give ObjC the first crack at comparing pointers, since
515 protocol qualifiers may be involved. */
516 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
517 break;
518 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
519 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
520 break;
522 case FUNCTION_TYPE:
523 val = function_types_compatible_p (t1, t2, flags);
524 break;
526 case ARRAY_TYPE:
528 tree d1 = TYPE_DOMAIN (t1);
529 tree d2 = TYPE_DOMAIN (t2);
530 bool d1_variable, d2_variable;
531 bool d1_zero, d2_zero;
532 val = 1;
534 /* Target types must match incl. qualifiers. */
535 if (TREE_TYPE (t1) != TREE_TYPE (t2)
536 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
537 flags)))
538 return 0;
540 /* Sizes must match unless one is missing or variable. */
541 if (d1 == 0 || d2 == 0 || d1 == d2)
542 break;
544 d1_zero = ! TYPE_MAX_VALUE (d1);
545 d2_zero = ! TYPE_MAX_VALUE (d2);
547 d1_variable = (! d1_zero
548 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
549 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
550 d2_variable = (! d2_zero
551 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
552 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
554 if (d1_variable || d2_variable)
555 break;
556 if (d1_zero && d2_zero)
557 break;
558 if (d1_zero || d2_zero
559 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
560 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
561 val = 0;
563 break;
566 case RECORD_TYPE:
567 /* We are dealing with two distinct structs. In assorted Objective-C
568 corner cases, however, these can still be deemed equivalent. */
569 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
570 val = 1;
572 case ENUMERAL_TYPE:
573 case UNION_TYPE:
574 if (val != 1 && !same_translation_unit_p (t1, t2))
575 val = tagged_types_tu_compatible_p (t1, t2, flags);
576 break;
578 case VECTOR_TYPE:
579 /* The target might allow certain vector types to be compatible. */
580 val = (*targetm.vector_opaque_p) (t1)
581 || (*targetm.vector_opaque_p) (t2);
582 break;
584 default:
585 break;
587 return attrval == 2 && val == 1 ? 2 : val;
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
592 to 1 or 0 depending if the check of the pointer types is meant to
593 be reflexive or not (typically, assignments are not reflexive,
594 while comparisons are reflexive).
597 static int
598 comp_target_types (tree ttl, tree ttr, int reflexive)
600 int val;
602 /* Give objc_comptypes a crack at letting these types through. */
603 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
604 return val;
606 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
607 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
609 if (val == 2 && pedantic)
610 pedwarn ("types are not quite compatible");
611 return val;
614 /* Subroutines of `comptypes'. */
616 /* Determine whether two types derive from the same translation unit.
617 If the CONTEXT chain ends in a null, that type's context is still
618 being parsed, so if two types have context chains ending in null,
619 they're in the same translation unit. */
620 static int
621 same_translation_unit_p (tree t1, tree t2)
623 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
624 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
626 case 'd': t1 = DECL_CONTEXT (t1); break;
627 case 't': t1 = TYPE_CONTEXT (t1); break;
628 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
629 default: abort ();
632 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
633 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
635 case 'd': t2 = DECL_CONTEXT (t1); break;
636 case 't': t2 = TYPE_CONTEXT (t2); break;
637 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
638 default: abort ();
641 return t1 == t2;
644 /* The C standard says that two structures in different translation
645 units are compatible with each other only if the types of their
646 fields are compatible (among other things). So, consider two copies
647 of this structure: */
649 struct tagged_tu_seen {
650 const struct tagged_tu_seen * next;
651 tree t1;
652 tree t2;
655 /* Can they be compatible with each other? We choose to break the
656 recursion by allowing those types to be compatible. */
658 static const struct tagged_tu_seen * tagged_tu_seen_base;
660 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
661 compatible. If the two types are not the same (which has been
662 checked earlier), this can only happen when multiple translation
663 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
664 rules. */
666 static int
667 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
669 tree s1, s2;
670 bool needs_warning = false;
672 /* We have to verify that the tags of the types are the same. This
673 is harder than it looks because this may be a typedef, so we have
674 to go look at the original type. It may even be a typedef of a
675 typedef... */
676 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
677 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
679 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
680 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
682 /* C90 didn't have the requirement that the two tags be the same. */
683 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
684 return 0;
686 /* C90 didn't say what happened if one or both of the types were
687 incomplete; we choose to follow C99 rules here, which is that they
688 are compatible. */
689 if (TYPE_SIZE (t1) == NULL
690 || TYPE_SIZE (t2) == NULL)
691 return 1;
694 const struct tagged_tu_seen * tts_i;
695 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
696 if (tts_i->t1 == t1 && tts_i->t2 == t2)
697 return 1;
700 switch (TREE_CODE (t1))
702 case ENUMERAL_TYPE:
704 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
705 return 0;
707 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
709 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
710 if (s2 == NULL
711 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
712 return 0;
714 return 1;
717 case UNION_TYPE:
719 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
720 return 0;
722 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
724 bool ok = false;
725 struct tagged_tu_seen tts;
727 tts.next = tagged_tu_seen_base;
728 tts.t1 = t1;
729 tts.t2 = t2;
730 tagged_tu_seen_base = &tts;
732 if (DECL_NAME (s1) != NULL)
733 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
734 if (DECL_NAME (s1) == DECL_NAME (s2))
736 int result;
737 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
738 if (result == 0)
739 break;
740 if (result == 2)
741 needs_warning = true;
743 if (TREE_CODE (s1) == FIELD_DECL
744 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
745 DECL_FIELD_BIT_OFFSET (s2)) != 1)
746 break;
748 ok = true;
749 break;
751 tagged_tu_seen_base = tts.next;
752 if (! ok)
753 return 0;
755 return needs_warning ? 2 : 1;
758 case RECORD_TYPE:
760 struct tagged_tu_seen tts;
762 tts.next = tagged_tu_seen_base;
763 tts.t1 = t1;
764 tts.t2 = t2;
765 tagged_tu_seen_base = &tts;
767 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
768 s1 && s2;
769 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
771 int result;
772 if (TREE_CODE (s1) != TREE_CODE (s2)
773 || DECL_NAME (s1) != DECL_NAME (s2))
774 break;
775 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
776 if (result == 0)
777 break;
778 if (result == 2)
779 needs_warning = true;
781 if (TREE_CODE (s1) == FIELD_DECL
782 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
783 DECL_FIELD_BIT_OFFSET (s2)) != 1)
784 break;
786 tagged_tu_seen_base = tts.next;
787 if (s1 && s2)
788 return 0;
789 return needs_warning ? 2 : 1;
792 default:
793 abort ();
797 /* Return 1 if two function types F1 and F2 are compatible.
798 If either type specifies no argument types,
799 the other must specify a fixed number of self-promoting arg types.
800 Otherwise, if one type specifies only the number of arguments,
801 the other must specify that number of self-promoting arg types.
802 Otherwise, the argument types must match. */
804 static int
805 function_types_compatible_p (tree f1, tree f2, int flags)
807 tree args1, args2;
808 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
809 int val = 1;
810 int val1;
811 tree ret1, ret2;
813 ret1 = TREE_TYPE (f1);
814 ret2 = TREE_TYPE (f2);
816 /* 'volatile' qualifiers on a function's return type mean the function
817 is noreturn. */
818 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
819 pedwarn ("function return types not compatible due to `volatile'");
820 if (TYPE_VOLATILE (ret1))
821 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
822 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
823 if (TYPE_VOLATILE (ret2))
824 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
825 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
826 val = comptypes (ret1, ret2, flags);
827 if (val == 0)
828 return 0;
830 args1 = TYPE_ARG_TYPES (f1);
831 args2 = TYPE_ARG_TYPES (f2);
833 /* An unspecified parmlist matches any specified parmlist
834 whose argument types don't need default promotions. */
836 if (args1 == 0)
838 if (!self_promoting_args_p (args2))
839 return 0;
840 /* If one of these types comes from a non-prototype fn definition,
841 compare that with the other type's arglist.
842 If they don't match, ask for a warning (but no error). */
843 if (TYPE_ACTUAL_ARG_TYPES (f1)
844 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
845 flags))
846 val = 2;
847 return val;
849 if (args2 == 0)
851 if (!self_promoting_args_p (args1))
852 return 0;
853 if (TYPE_ACTUAL_ARG_TYPES (f2)
854 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
855 flags))
856 val = 2;
857 return val;
860 /* Both types have argument lists: compare them and propagate results. */
861 val1 = type_lists_compatible_p (args1, args2, flags);
862 return val1 != 1 ? val1 : val;
865 /* Check two lists of types for compatibility,
866 returning 0 for incompatible, 1 for compatible,
867 or 2 for compatible with warning. */
869 static int
870 type_lists_compatible_p (tree args1, tree args2, int flags)
872 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
873 int val = 1;
874 int newval = 0;
876 while (1)
878 if (args1 == 0 && args2 == 0)
879 return val;
880 /* If one list is shorter than the other,
881 they fail to match. */
882 if (args1 == 0 || args2 == 0)
883 return 0;
884 /* A null pointer instead of a type
885 means there is supposed to be an argument
886 but nothing is specified about what type it has.
887 So match anything that self-promotes. */
888 if (TREE_VALUE (args1) == 0)
890 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
891 return 0;
893 else if (TREE_VALUE (args2) == 0)
895 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
896 return 0;
898 /* If one of the lists has an error marker, ignore this arg. */
899 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
900 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
902 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
903 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
904 flags)))
906 /* Allow wait (union {union wait *u; int *i} *)
907 and wait (union wait *) to be compatible. */
908 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
909 && (TYPE_NAME (TREE_VALUE (args1)) == 0
910 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
911 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
912 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
913 TYPE_SIZE (TREE_VALUE (args2))))
915 tree memb;
916 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
917 memb; memb = TREE_CHAIN (memb))
918 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
919 flags))
920 break;
921 if (memb == 0)
922 return 0;
924 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
925 && (TYPE_NAME (TREE_VALUE (args2)) == 0
926 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
927 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
928 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
929 TYPE_SIZE (TREE_VALUE (args1))))
931 tree memb;
932 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
933 memb; memb = TREE_CHAIN (memb))
934 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
935 flags))
936 break;
937 if (memb == 0)
938 return 0;
940 else
941 return 0;
944 /* comptypes said ok, but record if it said to warn. */
945 if (newval > val)
946 val = newval;
948 args1 = TREE_CHAIN (args1);
949 args2 = TREE_CHAIN (args2);
953 /* Compute the size to increment a pointer by. */
955 tree
956 c_size_in_bytes (tree type)
958 enum tree_code code = TREE_CODE (type);
960 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
961 return size_one_node;
963 if (!COMPLETE_OR_VOID_TYPE_P (type))
965 error ("arithmetic on pointer to an incomplete type");
966 return size_one_node;
969 /* Convert in case a char is more than one unit. */
970 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
971 size_int (TYPE_PRECISION (char_type_node)
972 / BITS_PER_UNIT));
975 /* Return either DECL or its known constant value (if it has one). */
977 tree
978 decl_constant_value (tree decl)
980 if (/* Don't change a variable array bound or initial value to a constant
981 in a place where a variable is invalid. */
982 current_function_decl != 0
983 && ! TREE_THIS_VOLATILE (decl)
984 && TREE_READONLY (decl)
985 && DECL_INITIAL (decl) != 0
986 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
987 /* This is invalid if initial value is not constant.
988 If it has either a function call, a memory reference,
989 or a variable, then re-evaluating it could give different results. */
990 && TREE_CONSTANT (DECL_INITIAL (decl))
991 /* Check for cases where this is sub-optimal, even though valid. */
992 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
993 return DECL_INITIAL (decl);
994 return decl;
997 /* Return either DECL or its known constant value (if it has one), but
998 return DECL if pedantic or DECL has mode BLKmode. This is for
999 bug-compatibility with the old behavior of decl_constant_value
1000 (before GCC 3.0); every use of this function is a bug and it should
1001 be removed before GCC 3.1. It is not appropriate to use pedantic
1002 in a way that affects optimization, and BLKmode is probably not the
1003 right test for avoiding misoptimizations either. */
1005 static tree
1006 decl_constant_value_for_broken_optimization (tree decl)
1008 if (pedantic || DECL_MODE (decl) == BLKmode)
1009 return decl;
1010 else
1011 return decl_constant_value (decl);
1015 /* Perform the default conversion of arrays and functions to pointers.
1016 Return the result of converting EXP. For any other expression, just
1017 return EXP. */
1019 static tree
1020 default_function_array_conversion (tree exp)
1022 tree orig_exp;
1023 tree type = TREE_TYPE (exp);
1024 enum tree_code code = TREE_CODE (type);
1025 int not_lvalue = 0;
1027 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1028 an lvalue.
1030 Do not use STRIP_NOPS here! It will remove conversions from pointer
1031 to integer and cause infinite recursion. */
1032 orig_exp = exp;
1033 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1034 || (TREE_CODE (exp) == NOP_EXPR
1035 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1037 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1038 not_lvalue = 1;
1039 exp = TREE_OPERAND (exp, 0);
1042 /* Preserve the original expression code. */
1043 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1044 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1046 if (code == FUNCTION_TYPE)
1048 return build_unary_op (ADDR_EXPR, exp, 0);
1050 if (code == ARRAY_TYPE)
1052 tree adr;
1053 tree restype = TREE_TYPE (type);
1054 tree ptrtype;
1055 int constp = 0;
1056 int volatilep = 0;
1057 int lvalue_array_p;
1059 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1061 constp = TREE_READONLY (exp);
1062 volatilep = TREE_THIS_VOLATILE (exp);
1065 if (TYPE_QUALS (type) || constp || volatilep)
1066 restype
1067 = c_build_qualified_type (restype,
1068 TYPE_QUALS (type)
1069 | (constp * TYPE_QUAL_CONST)
1070 | (volatilep * TYPE_QUAL_VOLATILE));
1072 if (TREE_CODE (exp) == INDIRECT_REF)
1073 return convert (TYPE_POINTER_TO (restype),
1074 TREE_OPERAND (exp, 0));
1076 if (TREE_CODE (exp) == COMPOUND_EXPR)
1078 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1079 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1080 TREE_OPERAND (exp, 0), op1);
1083 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1084 if (!flag_isoc99 && !lvalue_array_p)
1086 /* Before C99, non-lvalue arrays do not decay to pointers.
1087 Normally, using such an array would be invalid; but it can
1088 be used correctly inside sizeof or as a statement expression.
1089 Thus, do not give an error here; an error will result later. */
1090 return exp;
1093 ptrtype = build_pointer_type (restype);
1095 if (TREE_CODE (exp) == VAR_DECL)
1097 /* ??? This is not really quite correct
1098 in that the type of the operand of ADDR_EXPR
1099 is not the target type of the type of the ADDR_EXPR itself.
1100 Question is, can this lossage be avoided? */
1101 adr = build1 (ADDR_EXPR, ptrtype, exp);
1102 if (!c_mark_addressable (exp))
1103 return error_mark_node;
1104 TREE_CONSTANT (adr) = staticp (exp);
1105 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1106 return adr;
1108 /* This way is better for a COMPONENT_REF since it can
1109 simplify the offset for a component. */
1110 adr = build_unary_op (ADDR_EXPR, exp, 1);
1111 return convert (ptrtype, adr);
1113 return exp;
1116 /* Perform default promotions for C data used in expressions.
1117 Arrays and functions are converted to pointers;
1118 enumeral types or short or char, to int.
1119 In addition, manifest constants symbols are replaced by their values. */
1121 tree
1122 default_conversion (tree exp)
1124 tree orig_exp;
1125 tree type = TREE_TYPE (exp);
1126 enum tree_code code = TREE_CODE (type);
1128 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1129 return default_function_array_conversion (exp);
1131 /* Constants can be used directly unless they're not loadable. */
1132 if (TREE_CODE (exp) == CONST_DECL)
1133 exp = DECL_INITIAL (exp);
1135 /* Replace a nonvolatile const static variable with its value unless
1136 it is an array, in which case we must be sure that taking the
1137 address of the array produces consistent results. */
1138 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1140 exp = decl_constant_value_for_broken_optimization (exp);
1141 type = TREE_TYPE (exp);
1144 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1145 an lvalue.
1147 Do not use STRIP_NOPS here! It will remove conversions from pointer
1148 to integer and cause infinite recursion. */
1149 orig_exp = exp;
1150 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1151 || (TREE_CODE (exp) == NOP_EXPR
1152 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1153 exp = TREE_OPERAND (exp, 0);
1155 /* Preserve the original expression code. */
1156 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1157 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1159 /* Normally convert enums to int,
1160 but convert wide enums to something wider. */
1161 if (code == ENUMERAL_TYPE)
1163 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1164 TYPE_PRECISION (integer_type_node)),
1165 ((TYPE_PRECISION (type)
1166 >= TYPE_PRECISION (integer_type_node))
1167 && TREE_UNSIGNED (type)));
1169 return convert (type, exp);
1172 if (TREE_CODE (exp) == COMPONENT_REF
1173 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1174 /* If it's thinner than an int, promote it like a
1175 c_promoting_integer_type_p, otherwise leave it alone. */
1176 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1177 TYPE_PRECISION (integer_type_node)))
1178 return convert (integer_type_node, exp);
1180 if (c_promoting_integer_type_p (type))
1182 /* Preserve unsignedness if not really getting any wider. */
1183 if (TREE_UNSIGNED (type)
1184 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1185 return convert (unsigned_type_node, exp);
1187 return convert (integer_type_node, exp);
1190 if (code == VOID_TYPE)
1192 error ("void value not ignored as it ought to be");
1193 return error_mark_node;
1195 return exp;
1198 /* Look up COMPONENT in a structure or union DECL.
1200 If the component name is not found, returns NULL_TREE. Otherwise,
1201 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1202 stepping down the chain to the component, which is in the last
1203 TREE_VALUE of the list. Normally the list is of length one, but if
1204 the component is embedded within (nested) anonymous structures or
1205 unions, the list steps down the chain to the component. */
1207 static tree
1208 lookup_field (tree decl, tree component)
1210 tree type = TREE_TYPE (decl);
1211 tree field;
1213 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1214 to the field elements. Use a binary search on this array to quickly
1215 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1216 will always be set for structures which have many elements. */
1218 if (TYPE_LANG_SPECIFIC (type))
1220 int bot, top, half;
1221 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1223 field = TYPE_FIELDS (type);
1224 bot = 0;
1225 top = TYPE_LANG_SPECIFIC (type)->s->len;
1226 while (top - bot > 1)
1228 half = (top - bot + 1) >> 1;
1229 field = field_array[bot+half];
1231 if (DECL_NAME (field) == NULL_TREE)
1233 /* Step through all anon unions in linear fashion. */
1234 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1236 field = field_array[bot++];
1237 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1238 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1240 tree anon = lookup_field (field, component);
1242 if (anon)
1243 return tree_cons (NULL_TREE, field, anon);
1247 /* Entire record is only anon unions. */
1248 if (bot > top)
1249 return NULL_TREE;
1251 /* Restart the binary search, with new lower bound. */
1252 continue;
1255 if (DECL_NAME (field) == component)
1256 break;
1257 if (DECL_NAME (field) < component)
1258 bot += half;
1259 else
1260 top = bot + half;
1263 if (DECL_NAME (field_array[bot]) == component)
1264 field = field_array[bot];
1265 else if (DECL_NAME (field) != component)
1266 return NULL_TREE;
1268 else
1270 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1272 if (DECL_NAME (field) == NULL_TREE
1273 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1274 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1276 tree anon = lookup_field (field, component);
1278 if (anon)
1279 return tree_cons (NULL_TREE, field, anon);
1282 if (DECL_NAME (field) == component)
1283 break;
1286 if (field == NULL_TREE)
1287 return NULL_TREE;
1290 return tree_cons (NULL_TREE, field, NULL_TREE);
1293 /* Make an expression to refer to the COMPONENT field of
1294 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1296 tree
1297 build_component_ref (tree datum, tree component)
1299 tree type = TREE_TYPE (datum);
1300 enum tree_code code = TREE_CODE (type);
1301 tree field = NULL;
1302 tree ref;
1304 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1305 If pedantic ensure that the arguments are not lvalues; otherwise,
1306 if the component is an array, it would wrongly decay to a pointer in
1307 C89 mode.
1308 We cannot do this with a COND_EXPR, because in a conditional expression
1309 the default promotions are applied to both sides, and this would yield
1310 the wrong type of the result; for example, if the components have
1311 type "char". */
1312 switch (TREE_CODE (datum))
1314 case COMPOUND_EXPR:
1316 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1317 return build (COMPOUND_EXPR, TREE_TYPE (value),
1318 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1320 default:
1321 break;
1324 /* See if there is a field or component with name COMPONENT. */
1326 if (code == RECORD_TYPE || code == UNION_TYPE)
1328 if (!COMPLETE_TYPE_P (type))
1330 c_incomplete_type_error (NULL_TREE, type);
1331 return error_mark_node;
1334 field = lookup_field (datum, component);
1336 if (!field)
1338 error ("%s has no member named `%s'",
1339 code == RECORD_TYPE ? "structure" : "union",
1340 IDENTIFIER_POINTER (component));
1341 return error_mark_node;
1344 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1345 This might be better solved in future the way the C++ front
1346 end does it - by giving the anonymous entities each a
1347 separate name and type, and then have build_component_ref
1348 recursively call itself. We can't do that here. */
1351 tree subdatum = TREE_VALUE (field);
1353 if (TREE_TYPE (subdatum) == error_mark_node)
1354 return error_mark_node;
1356 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1357 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1358 TREE_READONLY (ref) = 1;
1359 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1360 TREE_THIS_VOLATILE (ref) = 1;
1362 if (TREE_DEPRECATED (subdatum))
1363 warn_deprecated_use (subdatum);
1365 datum = ref;
1367 field = TREE_CHAIN (field);
1369 while (field);
1371 return ref;
1373 else if (code != ERROR_MARK)
1374 error ("request for member `%s' in something not a structure or union",
1375 IDENTIFIER_POINTER (component));
1377 return error_mark_node;
1380 /* Given an expression PTR for a pointer, return an expression
1381 for the value pointed to.
1382 ERRORSTRING is the name of the operator to appear in error messages. */
1384 tree
1385 build_indirect_ref (tree ptr, const char *errorstring)
1387 tree pointer = default_conversion (ptr);
1388 tree type = TREE_TYPE (pointer);
1390 if (TREE_CODE (type) == POINTER_TYPE)
1392 if (TREE_CODE (pointer) == ADDR_EXPR
1393 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1394 == TREE_TYPE (type)))
1395 return TREE_OPERAND (pointer, 0);
1396 else
1398 tree t = TREE_TYPE (type);
1399 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1401 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1403 error ("dereferencing pointer to incomplete type");
1404 return error_mark_node;
1406 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1407 warning ("dereferencing `void *' pointer");
1409 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1410 so that we get the proper error message if the result is used
1411 to assign to. Also, &* is supposed to be a no-op.
1412 And ANSI C seems to specify that the type of the result
1413 should be the const type. */
1414 /* A de-reference of a pointer to const is not a const. It is valid
1415 to change it via some other pointer. */
1416 TREE_READONLY (ref) = TYPE_READONLY (t);
1417 TREE_SIDE_EFFECTS (ref)
1418 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1419 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1420 return ref;
1423 else if (TREE_CODE (pointer) != ERROR_MARK)
1424 error ("invalid type argument of `%s'", errorstring);
1425 return error_mark_node;
1428 /* This handles expressions of the form "a[i]", which denotes
1429 an array reference.
1431 This is logically equivalent in C to *(a+i), but we may do it differently.
1432 If A is a variable or a member, we generate a primitive ARRAY_REF.
1433 This avoids forcing the array out of registers, and can work on
1434 arrays that are not lvalues (for example, members of structures returned
1435 by functions). */
1437 tree
1438 build_array_ref (tree array, tree index)
1440 if (index == 0)
1442 error ("subscript missing in array reference");
1443 return error_mark_node;
1446 if (TREE_TYPE (array) == error_mark_node
1447 || TREE_TYPE (index) == error_mark_node)
1448 return error_mark_node;
1450 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1451 && TREE_CODE (array) != INDIRECT_REF)
1453 tree rval, type;
1455 /* Subscripting with type char is likely to lose
1456 on a machine where chars are signed.
1457 So warn on any machine, but optionally.
1458 Don't warn for unsigned char since that type is safe.
1459 Don't warn for signed char because anyone who uses that
1460 must have done so deliberately. */
1461 if (warn_char_subscripts
1462 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1463 warning ("array subscript has type `char'");
1465 /* Apply default promotions *after* noticing character types. */
1466 index = default_conversion (index);
1468 /* Require integer *after* promotion, for sake of enums. */
1469 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1471 error ("array subscript is not an integer");
1472 return error_mark_node;
1475 /* An array that is indexed by a non-constant
1476 cannot be stored in a register; we must be able to do
1477 address arithmetic on its address.
1478 Likewise an array of elements of variable size. */
1479 if (TREE_CODE (index) != INTEGER_CST
1480 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1481 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1483 if (!c_mark_addressable (array))
1484 return error_mark_node;
1486 /* An array that is indexed by a constant value which is not within
1487 the array bounds cannot be stored in a register either; because we
1488 would get a crash in store_bit_field/extract_bit_field when trying
1489 to access a non-existent part of the register. */
1490 if (TREE_CODE (index) == INTEGER_CST
1491 && TYPE_VALUES (TREE_TYPE (array))
1492 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1494 if (!c_mark_addressable (array))
1495 return error_mark_node;
1498 if (pedantic)
1500 tree foo = array;
1501 while (TREE_CODE (foo) == COMPONENT_REF)
1502 foo = TREE_OPERAND (foo, 0);
1503 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1504 pedwarn ("ISO C forbids subscripting `register' array");
1505 else if (! flag_isoc99 && ! lvalue_p (foo))
1506 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1509 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1510 rval = build (ARRAY_REF, type, array, index);
1511 /* Array ref is const/volatile if the array elements are
1512 or if the array is. */
1513 TREE_READONLY (rval)
1514 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1515 | TREE_READONLY (array));
1516 TREE_SIDE_EFFECTS (rval)
1517 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1518 | TREE_SIDE_EFFECTS (array));
1519 TREE_THIS_VOLATILE (rval)
1520 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1521 /* This was added by rms on 16 Nov 91.
1522 It fixes vol struct foo *a; a->elts[1]
1523 in an inline function.
1524 Hope it doesn't break something else. */
1525 | TREE_THIS_VOLATILE (array));
1526 return require_complete_type (fold (rval));
1530 tree ar = default_conversion (array);
1531 tree ind = default_conversion (index);
1533 /* Do the same warning check as above, but only on the part that's
1534 syntactically the index and only if it is also semantically
1535 the index. */
1536 if (warn_char_subscripts
1537 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1538 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1539 warning ("subscript has type `char'");
1541 /* Put the integer in IND to simplify error checking. */
1542 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1544 tree temp = ar;
1545 ar = ind;
1546 ind = temp;
1549 if (ar == error_mark_node)
1550 return ar;
1552 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1553 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1555 error ("subscripted value is neither array nor pointer");
1556 return error_mark_node;
1558 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1560 error ("array subscript is not an integer");
1561 return error_mark_node;
1564 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1565 "array indexing");
1569 /* Build an external reference to identifier ID. FUN indicates
1570 whether this will be used for a function call. */
1571 tree
1572 build_external_ref (tree id, int fun)
1574 tree ref;
1575 tree decl = lookup_name (id);
1576 tree objc_ivar = lookup_objc_ivar (id);
1578 if (decl && decl != error_mark_node)
1580 /* Properly declared variable or function reference. */
1581 if (!objc_ivar)
1582 ref = decl;
1583 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1585 warning ("local declaration of `%s' hides instance variable",
1586 IDENTIFIER_POINTER (id));
1587 ref = decl;
1589 else
1590 ref = objc_ivar;
1592 else if (objc_ivar)
1593 ref = objc_ivar;
1594 else if (fun)
1595 /* Implicit function declaration. */
1596 ref = implicitly_declare (id);
1597 else if (decl == error_mark_node)
1598 /* Don't complain about something that's already been
1599 complained about. */
1600 return error_mark_node;
1601 else
1603 undeclared_variable (id);
1604 return error_mark_node;
1607 if (TREE_TYPE (ref) == error_mark_node)
1608 return error_mark_node;
1610 if (TREE_DEPRECATED (ref))
1611 warn_deprecated_use (ref);
1613 if (!skip_evaluation)
1614 assemble_external (ref);
1615 TREE_USED (ref) = 1;
1617 if (TREE_CODE (ref) == CONST_DECL)
1619 ref = DECL_INITIAL (ref);
1620 TREE_CONSTANT (ref) = 1;
1622 else if (current_function_decl != 0
1623 && !DECL_FILE_SCOPE_P (current_function_decl)
1624 && (TREE_CODE (ref) == VAR_DECL
1625 || TREE_CODE (ref) == PARM_DECL
1626 || TREE_CODE (ref) == FUNCTION_DECL))
1628 tree context = decl_function_context (ref);
1630 if (context != 0 && context != current_function_decl)
1631 DECL_NONLOCAL (ref) = 1;
1634 return ref;
1637 /* Build a function call to function FUNCTION with parameters PARAMS.
1638 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1639 TREE_VALUE of each node is a parameter-expression.
1640 FUNCTION's data type may be a function type or a pointer-to-function. */
1642 tree
1643 build_function_call (tree function, tree params)
1645 tree fntype, fundecl = 0;
1646 tree coerced_params;
1647 tree name = NULL_TREE, result;
1648 tree tem;
1650 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1651 STRIP_TYPE_NOPS (function);
1653 /* Convert anything with function type to a pointer-to-function. */
1654 if (TREE_CODE (function) == FUNCTION_DECL)
1656 name = DECL_NAME (function);
1658 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1659 (because calling an inline function does not mean the function
1660 needs to be separately compiled). */
1661 fntype = build_type_variant (TREE_TYPE (function),
1662 TREE_READONLY (function),
1663 TREE_THIS_VOLATILE (function));
1664 fundecl = function;
1665 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1667 else
1668 function = default_conversion (function);
1670 fntype = TREE_TYPE (function);
1672 if (TREE_CODE (fntype) == ERROR_MARK)
1673 return error_mark_node;
1675 if (!(TREE_CODE (fntype) == POINTER_TYPE
1676 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1678 error ("called object is not a function");
1679 return error_mark_node;
1682 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1683 current_function_returns_abnormally = 1;
1685 /* fntype now gets the type of function pointed to. */
1686 fntype = TREE_TYPE (fntype);
1688 /* Check that the function is called through a compatible prototype.
1689 If it is not, replace the call by a trap, wrapped up in a compound
1690 expression if necessary. This has the nice side-effect to prevent
1691 the tree-inliner from generating invalid assignment trees which may
1692 blow up in the RTL expander later.
1694 ??? This doesn't work for Objective-C because objc_comptypes
1695 refuses to compare function prototypes, yet the compiler appears
1696 to build calls that are flagged as invalid by C's comptypes. */
1697 if (! c_dialect_objc ()
1698 && TREE_CODE (function) == NOP_EXPR
1699 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1700 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1701 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1703 tree return_type = TREE_TYPE (fntype);
1704 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1705 NULL_TREE);
1707 /* This situation leads to run-time undefined behavior. We can't,
1708 therefore, simply error unless we can prove that all possible
1709 executions of the program must execute the code. */
1710 warning ("function called through a non-compatible type");
1712 if (VOID_TYPE_P (return_type))
1713 return trap;
1714 else
1716 tree rhs;
1718 if (AGGREGATE_TYPE_P (return_type))
1719 rhs = build_compound_literal (return_type,
1720 build_constructor (return_type,
1721 NULL_TREE));
1722 else
1723 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1725 return build (COMPOUND_EXPR, return_type, trap, rhs);
1729 /* Convert the parameters to the types declared in the
1730 function prototype, or apply default promotions. */
1732 coerced_params
1733 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1735 /* Check that the arguments to the function are valid. */
1737 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1739 /* Recognize certain built-in functions so we can make tree-codes
1740 other than CALL_EXPR. We do this when it enables fold-const.c
1741 to do something useful. */
1743 if (TREE_CODE (function) == ADDR_EXPR
1744 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1745 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1747 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1748 params, coerced_params);
1749 if (result)
1750 return result;
1753 result = build (CALL_EXPR, TREE_TYPE (fntype),
1754 function, coerced_params, NULL_TREE);
1755 TREE_SIDE_EFFECTS (result) = 1;
1756 result = fold (result);
1758 if (VOID_TYPE_P (TREE_TYPE (result)))
1759 return result;
1760 return require_complete_type (result);
1763 /* Convert the argument expressions in the list VALUES
1764 to the types in the list TYPELIST. The result is a list of converted
1765 argument expressions.
1767 If TYPELIST is exhausted, or when an element has NULL as its type,
1768 perform the default conversions.
1770 PARMLIST is the chain of parm decls for the function being called.
1771 It may be 0, if that info is not available.
1772 It is used only for generating error messages.
1774 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1776 This is also where warnings about wrong number of args are generated.
1778 Both VALUES and the returned value are chains of TREE_LIST nodes
1779 with the elements of the list in the TREE_VALUE slots of those nodes. */
1781 static tree
1782 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1784 tree typetail, valtail;
1785 tree result = NULL;
1786 int parmnum;
1788 /* Scan the given expressions and types, producing individual
1789 converted arguments and pushing them on RESULT in reverse order. */
1791 for (valtail = values, typetail = typelist, parmnum = 0;
1792 valtail;
1793 valtail = TREE_CHAIN (valtail), parmnum++)
1795 tree type = typetail ? TREE_VALUE (typetail) : 0;
1796 tree val = TREE_VALUE (valtail);
1798 if (type == void_type_node)
1800 if (name)
1801 error ("too many arguments to function `%s'",
1802 IDENTIFIER_POINTER (name));
1803 else
1804 error ("too many arguments to function");
1805 break;
1808 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1809 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1810 to convert automatically to a pointer. */
1811 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1812 val = TREE_OPERAND (val, 0);
1814 val = default_function_array_conversion (val);
1816 val = require_complete_type (val);
1818 if (type != 0)
1820 /* Formal parm type is specified by a function prototype. */
1821 tree parmval;
1823 if (!COMPLETE_TYPE_P (type))
1825 error ("type of formal parameter %d is incomplete", parmnum + 1);
1826 parmval = val;
1828 else
1830 /* Optionally warn about conversions that
1831 differ from the default conversions. */
1832 if (warn_conversion || warn_traditional)
1834 int formal_prec = TYPE_PRECISION (type);
1836 if (INTEGRAL_TYPE_P (type)
1837 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1838 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1839 if (INTEGRAL_TYPE_P (type)
1840 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1841 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1842 else if (TREE_CODE (type) == COMPLEX_TYPE
1843 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1844 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1845 else if (TREE_CODE (type) == REAL_TYPE
1846 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1847 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1848 else if (TREE_CODE (type) == COMPLEX_TYPE
1849 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1850 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1851 else if (TREE_CODE (type) == REAL_TYPE
1852 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1853 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1854 /* ??? At some point, messages should be written about
1855 conversions between complex types, but that's too messy
1856 to do now. */
1857 else if (TREE_CODE (type) == REAL_TYPE
1858 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1860 /* Warn if any argument is passed as `float',
1861 since without a prototype it would be `double'. */
1862 if (formal_prec == TYPE_PRECISION (float_type_node))
1863 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1865 /* Detect integer changing in width or signedness.
1866 These warnings are only activated with
1867 -Wconversion, not with -Wtraditional. */
1868 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1869 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1871 tree would_have_been = default_conversion (val);
1872 tree type1 = TREE_TYPE (would_have_been);
1874 if (TREE_CODE (type) == ENUMERAL_TYPE
1875 && (TYPE_MAIN_VARIANT (type)
1876 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1877 /* No warning if function asks for enum
1878 and the actual arg is that enum type. */
1880 else if (formal_prec != TYPE_PRECISION (type1))
1881 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1882 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1884 /* Don't complain if the formal parameter type
1885 is an enum, because we can't tell now whether
1886 the value was an enum--even the same enum. */
1887 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1889 else if (TREE_CODE (val) == INTEGER_CST
1890 && int_fits_type_p (val, type))
1891 /* Change in signedness doesn't matter
1892 if a constant value is unaffected. */
1894 /* Likewise for a constant in a NOP_EXPR. */
1895 else if (TREE_CODE (val) == NOP_EXPR
1896 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1897 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1899 /* If the value is extended from a narrower
1900 unsigned type, it doesn't matter whether we
1901 pass it as signed or unsigned; the value
1902 certainly is the same either way. */
1903 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1904 && TREE_UNSIGNED (TREE_TYPE (val)))
1906 else if (TREE_UNSIGNED (type))
1907 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1908 else
1909 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1913 parmval = convert_for_assignment (type, val,
1914 (char *) 0, /* arg passing */
1915 fundecl, name, parmnum + 1);
1917 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1918 && INTEGRAL_TYPE_P (type)
1919 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1920 parmval = default_conversion (parmval);
1922 result = tree_cons (NULL_TREE, parmval, result);
1924 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1925 && (TYPE_PRECISION (TREE_TYPE (val))
1926 < TYPE_PRECISION (double_type_node)))
1927 /* Convert `float' to `double'. */
1928 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1929 else
1930 /* Convert `short' and `char' to full-size `int'. */
1931 result = tree_cons (NULL_TREE, default_conversion (val), result);
1933 if (typetail)
1934 typetail = TREE_CHAIN (typetail);
1937 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1939 if (name)
1940 error ("too few arguments to function `%s'",
1941 IDENTIFIER_POINTER (name));
1942 else
1943 error ("too few arguments to function");
1946 return nreverse (result);
1949 /* This is the entry point used by the parser
1950 for binary operators in the input.
1951 In addition to constructing the expression,
1952 we check for operands that were written with other binary operators
1953 in a way that is likely to confuse the user. */
1955 tree
1956 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1958 tree result = build_binary_op (code, arg1, arg2, 1);
1960 char class;
1961 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1962 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1963 enum tree_code code1 = ERROR_MARK;
1964 enum tree_code code2 = ERROR_MARK;
1966 if (TREE_CODE (result) == ERROR_MARK)
1967 return error_mark_node;
1969 if (IS_EXPR_CODE_CLASS (class1))
1970 code1 = C_EXP_ORIGINAL_CODE (arg1);
1971 if (IS_EXPR_CODE_CLASS (class2))
1972 code2 = C_EXP_ORIGINAL_CODE (arg2);
1974 /* Check for cases such as x+y<<z which users are likely
1975 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1976 is cleared to prevent these warnings. */
1977 if (warn_parentheses)
1979 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1981 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1982 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1983 warning ("suggest parentheses around + or - inside shift");
1986 if (code == TRUTH_ORIF_EXPR)
1988 if (code1 == TRUTH_ANDIF_EXPR
1989 || code2 == TRUTH_ANDIF_EXPR)
1990 warning ("suggest parentheses around && within ||");
1993 if (code == BIT_IOR_EXPR)
1995 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1996 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1997 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1998 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1999 warning ("suggest parentheses around arithmetic in operand of |");
2000 /* Check cases like x|y==z */
2001 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2002 warning ("suggest parentheses around comparison in operand of |");
2005 if (code == BIT_XOR_EXPR)
2007 if (code1 == BIT_AND_EXPR
2008 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2009 || code2 == BIT_AND_EXPR
2010 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2011 warning ("suggest parentheses around arithmetic in operand of ^");
2012 /* Check cases like x^y==z */
2013 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2014 warning ("suggest parentheses around comparison in operand of ^");
2017 if (code == BIT_AND_EXPR)
2019 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2020 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2021 warning ("suggest parentheses around + or - in operand of &");
2022 /* Check cases like x&y==z */
2023 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2024 warning ("suggest parentheses around comparison in operand of &");
2028 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2029 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2030 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2031 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2033 unsigned_conversion_warning (result, arg1);
2034 unsigned_conversion_warning (result, arg2);
2035 overflow_warning (result);
2037 class = TREE_CODE_CLASS (TREE_CODE (result));
2039 /* Record the code that was specified in the source,
2040 for the sake of warnings about confusing nesting. */
2041 if (IS_EXPR_CODE_CLASS (class))
2042 C_SET_EXP_ORIGINAL_CODE (result, code);
2043 else
2045 int flag = TREE_CONSTANT (result);
2046 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2047 so that convert_for_assignment wouldn't strip it.
2048 That way, we got warnings for things like p = (1 - 1).
2049 But it turns out we should not get those warnings. */
2050 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2051 C_SET_EXP_ORIGINAL_CODE (result, code);
2052 TREE_CONSTANT (result) = flag;
2055 return result;
2059 /* Return true if `t' is known to be non-negative. */
2062 c_tree_expr_nonnegative_p (tree t)
2064 if (TREE_CODE (t) == STMT_EXPR)
2066 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2068 /* Find the last statement in the chain, ignoring the final
2069 * scope statement */
2070 while (TREE_CHAIN (t) != NULL_TREE
2071 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2072 t = TREE_CHAIN (t);
2073 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2075 return tree_expr_nonnegative_p (t);
2078 /* Return a tree for the difference of pointers OP0 and OP1.
2079 The resulting tree has type int. */
2081 static tree
2082 pointer_diff (tree op0, tree op1)
2084 tree result, folded;
2085 tree restype = ptrdiff_type_node;
2087 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2088 tree con0, con1, lit0, lit1;
2089 tree orig_op1 = op1;
2091 if (pedantic || warn_pointer_arith)
2093 if (TREE_CODE (target_type) == VOID_TYPE)
2094 pedwarn ("pointer of type `void *' used in subtraction");
2095 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2096 pedwarn ("pointer to a function used in subtraction");
2099 /* If the conversion to ptrdiff_type does anything like widening or
2100 converting a partial to an integral mode, we get a convert_expression
2101 that is in the way to do any simplifications.
2102 (fold-const.c doesn't know that the extra bits won't be needed.
2103 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2104 different mode in place.)
2105 So first try to find a common term here 'by hand'; we want to cover
2106 at least the cases that occur in legal static initializers. */
2107 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2108 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2110 if (TREE_CODE (con0) == PLUS_EXPR)
2112 lit0 = TREE_OPERAND (con0, 1);
2113 con0 = TREE_OPERAND (con0, 0);
2115 else
2116 lit0 = integer_zero_node;
2118 if (TREE_CODE (con1) == PLUS_EXPR)
2120 lit1 = TREE_OPERAND (con1, 1);
2121 con1 = TREE_OPERAND (con1, 0);
2123 else
2124 lit1 = integer_zero_node;
2126 if (operand_equal_p (con0, con1, 0))
2128 op0 = lit0;
2129 op1 = lit1;
2133 /* First do the subtraction as integers;
2134 then drop through to build the divide operator.
2135 Do not do default conversions on the minus operator
2136 in case restype is a short type. */
2138 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2139 convert (restype, op1), 0);
2140 /* This generates an error if op1 is pointer to incomplete type. */
2141 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2142 error ("arithmetic on pointer to an incomplete type");
2144 /* This generates an error if op0 is pointer to incomplete type. */
2145 op1 = c_size_in_bytes (target_type);
2147 /* Divide by the size, in easiest possible way. */
2149 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2151 folded = fold (result);
2152 if (folded == result)
2153 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2154 return folded;
2157 /* Construct and perhaps optimize a tree representation
2158 for a unary operation. CODE, a tree_code, specifies the operation
2159 and XARG is the operand.
2160 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2161 the default promotions (such as from short to int).
2162 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2163 allows non-lvalues; this is only used to handle conversion of non-lvalue
2164 arrays to pointers in C99. */
2166 tree
2167 build_unary_op (enum tree_code code, tree xarg, int flag)
2169 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2170 tree arg = xarg;
2171 tree argtype = 0;
2172 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2173 tree val;
2174 int noconvert = flag;
2176 if (typecode == ERROR_MARK)
2177 return error_mark_node;
2178 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2179 typecode = INTEGER_TYPE;
2181 switch (code)
2183 case CONVERT_EXPR:
2184 /* This is used for unary plus, because a CONVERT_EXPR
2185 is enough to prevent anybody from looking inside for
2186 associativity, but won't generate any code. */
2187 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2188 || typecode == COMPLEX_TYPE))
2190 error ("wrong type argument to unary plus");
2191 return error_mark_node;
2193 else if (!noconvert)
2194 arg = default_conversion (arg);
2195 arg = non_lvalue (arg);
2196 break;
2198 case NEGATE_EXPR:
2199 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2200 || typecode == COMPLEX_TYPE
2201 || typecode == VECTOR_TYPE))
2203 error ("wrong type argument to unary minus");
2204 return error_mark_node;
2206 else if (!noconvert)
2207 arg = default_conversion (arg);
2208 break;
2210 case BIT_NOT_EXPR:
2211 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2213 if (!noconvert)
2214 arg = default_conversion (arg);
2216 else if (typecode == COMPLEX_TYPE)
2218 code = CONJ_EXPR;
2219 if (pedantic)
2220 pedwarn ("ISO C does not support `~' for complex conjugation");
2221 if (!noconvert)
2222 arg = default_conversion (arg);
2224 else
2226 error ("wrong type argument to bit-complement");
2227 return error_mark_node;
2229 break;
2231 case ABS_EXPR:
2232 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2234 error ("wrong type argument to abs");
2235 return error_mark_node;
2237 else if (!noconvert)
2238 arg = default_conversion (arg);
2239 break;
2241 case CONJ_EXPR:
2242 /* Conjugating a real value is a no-op, but allow it anyway. */
2243 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2244 || typecode == COMPLEX_TYPE))
2246 error ("wrong type argument to conjugation");
2247 return error_mark_node;
2249 else if (!noconvert)
2250 arg = default_conversion (arg);
2251 break;
2253 case TRUTH_NOT_EXPR:
2254 if (typecode != INTEGER_TYPE
2255 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2256 && typecode != COMPLEX_TYPE
2257 /* These will convert to a pointer. */
2258 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2260 error ("wrong type argument to unary exclamation mark");
2261 return error_mark_node;
2263 arg = c_common_truthvalue_conversion (arg);
2264 return invert_truthvalue (arg);
2266 case NOP_EXPR:
2267 break;
2269 case REALPART_EXPR:
2270 if (TREE_CODE (arg) == COMPLEX_CST)
2271 return TREE_REALPART (arg);
2272 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2273 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2274 else
2275 return arg;
2277 case IMAGPART_EXPR:
2278 if (TREE_CODE (arg) == COMPLEX_CST)
2279 return TREE_IMAGPART (arg);
2280 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2281 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2282 else
2283 return convert (TREE_TYPE (arg), integer_zero_node);
2285 case PREINCREMENT_EXPR:
2286 case POSTINCREMENT_EXPR:
2287 case PREDECREMENT_EXPR:
2288 case POSTDECREMENT_EXPR:
2289 /* Handle complex lvalues (when permitted)
2290 by reduction to simpler cases. */
2292 val = unary_complex_lvalue (code, arg, 0);
2293 if (val != 0)
2294 return val;
2296 /* Increment or decrement the real part of the value,
2297 and don't change the imaginary part. */
2298 if (typecode == COMPLEX_TYPE)
2300 tree real, imag;
2302 if (pedantic)
2303 pedwarn ("ISO C does not support `++' and `--' on complex types");
2305 arg = stabilize_reference (arg);
2306 real = build_unary_op (REALPART_EXPR, arg, 1);
2307 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2308 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2309 build_unary_op (code, real, 1), imag);
2312 /* Report invalid types. */
2314 if (typecode != POINTER_TYPE
2315 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2317 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2318 error ("wrong type argument to increment");
2319 else
2320 error ("wrong type argument to decrement");
2322 return error_mark_node;
2326 tree inc;
2327 tree result_type = TREE_TYPE (arg);
2329 arg = get_unwidened (arg, 0);
2330 argtype = TREE_TYPE (arg);
2332 /* Compute the increment. */
2334 if (typecode == POINTER_TYPE)
2336 /* If pointer target is an undefined struct,
2337 we just cannot know how to do the arithmetic. */
2338 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2340 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2341 error ("increment of pointer to unknown structure");
2342 else
2343 error ("decrement of pointer to unknown structure");
2345 else if ((pedantic || warn_pointer_arith)
2346 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2347 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2349 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2350 pedwarn ("wrong type argument to increment");
2351 else
2352 pedwarn ("wrong type argument to decrement");
2355 inc = c_size_in_bytes (TREE_TYPE (result_type));
2357 else
2358 inc = integer_one_node;
2360 inc = convert (argtype, inc);
2362 /* Handle incrementing a cast-expression. */
2364 while (1)
2365 switch (TREE_CODE (arg))
2367 case NOP_EXPR:
2368 case CONVERT_EXPR:
2369 case FLOAT_EXPR:
2370 case FIX_TRUNC_EXPR:
2371 case FIX_FLOOR_EXPR:
2372 case FIX_ROUND_EXPR:
2373 case FIX_CEIL_EXPR:
2374 pedantic_lvalue_warning (CONVERT_EXPR);
2375 /* If the real type has the same machine representation
2376 as the type it is cast to, we can make better output
2377 by adding directly to the inside of the cast. */
2378 if ((TREE_CODE (TREE_TYPE (arg))
2379 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2380 && (TYPE_MODE (TREE_TYPE (arg))
2381 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2382 arg = TREE_OPERAND (arg, 0);
2383 else
2385 tree incremented, modify, value;
2386 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2387 value = boolean_increment (code, arg);
2388 else
2390 arg = stabilize_reference (arg);
2391 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2392 value = arg;
2393 else
2394 value = save_expr (arg);
2395 incremented = build (((code == PREINCREMENT_EXPR
2396 || code == POSTINCREMENT_EXPR)
2397 ? PLUS_EXPR : MINUS_EXPR),
2398 argtype, value, inc);
2399 TREE_SIDE_EFFECTS (incremented) = 1;
2400 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2401 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2403 TREE_USED (value) = 1;
2404 return value;
2406 break;
2408 default:
2409 goto give_up;
2411 give_up:
2413 /* Complain about anything else that is not a true lvalue. */
2414 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2415 || code == POSTINCREMENT_EXPR)
2416 ? "invalid lvalue in increment"
2417 : "invalid lvalue in decrement")))
2418 return error_mark_node;
2420 /* Report a read-only lvalue. */
2421 if (TREE_READONLY (arg))
2422 readonly_warning (arg,
2423 ((code == PREINCREMENT_EXPR
2424 || code == POSTINCREMENT_EXPR)
2425 ? "increment" : "decrement"));
2427 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2428 val = boolean_increment (code, arg);
2429 else
2430 val = build (code, TREE_TYPE (arg), arg, inc);
2431 TREE_SIDE_EFFECTS (val) = 1;
2432 val = convert (result_type, val);
2433 if (TREE_CODE (val) != code)
2434 TREE_NO_UNUSED_WARNING (val) = 1;
2435 return val;
2438 case ADDR_EXPR:
2439 /* Note that this operation never does default_conversion. */
2441 /* Let &* cancel out to simplify resulting code. */
2442 if (TREE_CODE (arg) == INDIRECT_REF)
2444 /* Don't let this be an lvalue. */
2445 if (lvalue_p (TREE_OPERAND (arg, 0)))
2446 return non_lvalue (TREE_OPERAND (arg, 0));
2447 return TREE_OPERAND (arg, 0);
2450 /* For &x[y], return x+y */
2451 if (TREE_CODE (arg) == ARRAY_REF)
2453 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2454 return error_mark_node;
2455 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2456 TREE_OPERAND (arg, 1), 1);
2459 /* Handle complex lvalues (when permitted)
2460 by reduction to simpler cases. */
2461 val = unary_complex_lvalue (code, arg, flag);
2462 if (val != 0)
2463 return val;
2465 /* Anything not already handled and not a true memory reference
2466 or a non-lvalue array is an error. */
2467 else if (typecode != FUNCTION_TYPE && !flag
2468 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2469 return error_mark_node;
2471 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2472 argtype = TREE_TYPE (arg);
2474 /* If the lvalue is const or volatile, merge that into the type
2475 to which the address will point. Note that you can't get a
2476 restricted pointer by taking the address of something, so we
2477 only have to deal with `const' and `volatile' here. */
2478 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2479 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2480 argtype = c_build_type_variant (argtype,
2481 TREE_READONLY (arg),
2482 TREE_THIS_VOLATILE (arg));
2484 argtype = build_pointer_type (argtype);
2486 if (!c_mark_addressable (arg))
2487 return error_mark_node;
2490 tree addr;
2492 if (TREE_CODE (arg) == COMPONENT_REF)
2494 tree field = TREE_OPERAND (arg, 1);
2496 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2498 if (DECL_C_BIT_FIELD (field))
2500 error ("attempt to take address of bit-field structure member `%s'",
2501 IDENTIFIER_POINTER (DECL_NAME (field)));
2502 return error_mark_node;
2505 addr = fold (build (PLUS_EXPR, argtype,
2506 convert (argtype, addr),
2507 convert (argtype, byte_position (field))));
2509 else
2510 addr = build1 (code, argtype, arg);
2512 /* Address of a static or external variable or
2513 file-scope function counts as a constant. */
2514 if (staticp (arg)
2515 && ! (TREE_CODE (arg) == FUNCTION_DECL
2516 && !DECL_FILE_SCOPE_P (arg)))
2517 TREE_CONSTANT (addr) = 1;
2518 return addr;
2521 default:
2522 break;
2525 if (argtype == 0)
2526 argtype = TREE_TYPE (arg);
2527 return fold (build1 (code, argtype, arg));
2530 /* Return nonzero if REF is an lvalue valid for this language.
2531 Lvalues can be assigned, unless their type has TYPE_READONLY.
2532 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2535 lvalue_p (tree ref)
2537 enum tree_code code = TREE_CODE (ref);
2539 switch (code)
2541 case REALPART_EXPR:
2542 case IMAGPART_EXPR:
2543 case COMPONENT_REF:
2544 return lvalue_p (TREE_OPERAND (ref, 0));
2546 case COMPOUND_LITERAL_EXPR:
2547 case STRING_CST:
2548 return 1;
2550 case INDIRECT_REF:
2551 case ARRAY_REF:
2552 case VAR_DECL:
2553 case PARM_DECL:
2554 case RESULT_DECL:
2555 case ERROR_MARK:
2556 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2557 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2559 case BIND_EXPR:
2560 case RTL_EXPR:
2561 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2563 default:
2564 return 0;
2568 /* Return nonzero if REF is an lvalue valid for this language;
2569 otherwise, print an error message and return zero. */
2572 lvalue_or_else (tree ref, const char *msgid)
2574 int win = lvalue_p (ref);
2576 if (! win)
2577 error ("%s", msgid);
2579 return win;
2582 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2583 for certain kinds of expressions which are not really lvalues
2584 but which we can accept as lvalues. If FLAG is nonzero, then
2585 non-lvalues are OK since we may be converting a non-lvalue array to
2586 a pointer in C99.
2588 If ARG is not a kind of expression we can handle, return zero. */
2590 static tree
2591 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2593 /* Handle (a, b) used as an "lvalue". */
2594 if (TREE_CODE (arg) == COMPOUND_EXPR)
2596 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2598 /* If this returns a function type, it isn't really being used as
2599 an lvalue, so don't issue a warning about it. */
2600 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2601 pedantic_lvalue_warning (COMPOUND_EXPR);
2603 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2604 TREE_OPERAND (arg, 0), real_result);
2607 /* Handle (a ? b : c) used as an "lvalue". */
2608 if (TREE_CODE (arg) == COND_EXPR)
2610 if (!flag)
2611 pedantic_lvalue_warning (COND_EXPR);
2612 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2613 pedantic_lvalue_warning (COMPOUND_EXPR);
2615 return (build_conditional_expr
2616 (TREE_OPERAND (arg, 0),
2617 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2618 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2621 return 0;
2624 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2625 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2627 static void
2628 pedantic_lvalue_warning (enum tree_code code)
2630 switch (code)
2632 case COND_EXPR:
2633 pedwarn ("use of conditional expressions as lvalues is deprecated");
2634 break;
2635 case COMPOUND_EXPR:
2636 pedwarn ("use of compound expressions as lvalues is deprecated");
2637 break;
2638 default:
2639 pedwarn ("use of cast expressions as lvalues is deprecated");
2640 break;
2644 /* Warn about storing in something that is `const'. */
2646 void
2647 readonly_warning (tree arg, const char *msgid)
2649 if (TREE_CODE (arg) == COMPONENT_REF)
2651 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2652 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2653 else
2654 pedwarn ("%s of read-only member `%s'", _(msgid),
2655 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2657 else if (TREE_CODE (arg) == VAR_DECL)
2658 pedwarn ("%s of read-only variable `%s'", _(msgid),
2659 IDENTIFIER_POINTER (DECL_NAME (arg)));
2660 else
2661 pedwarn ("%s of read-only location", _(msgid));
2664 /* Mark EXP saying that we need to be able to take the
2665 address of it; it should not be allocated in a register.
2666 Returns true if successful. */
2668 bool
2669 c_mark_addressable (tree exp)
2671 tree x = exp;
2673 while (1)
2674 switch (TREE_CODE (x))
2676 case COMPONENT_REF:
2677 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2679 error ("cannot take address of bit-field `%s'",
2680 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2681 return false;
2684 /* ... fall through ... */
2686 case ADDR_EXPR:
2687 case ARRAY_REF:
2688 case REALPART_EXPR:
2689 case IMAGPART_EXPR:
2690 x = TREE_OPERAND (x, 0);
2691 break;
2693 case COMPOUND_LITERAL_EXPR:
2694 case CONSTRUCTOR:
2695 TREE_ADDRESSABLE (x) = 1;
2696 return true;
2698 case VAR_DECL:
2699 case CONST_DECL:
2700 case PARM_DECL:
2701 case RESULT_DECL:
2702 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2703 && DECL_NONLOCAL (x))
2705 if (TREE_PUBLIC (x))
2707 error ("global register variable `%s' used in nested function",
2708 IDENTIFIER_POINTER (DECL_NAME (x)));
2709 return false;
2711 pedwarn ("register variable `%s' used in nested function",
2712 IDENTIFIER_POINTER (DECL_NAME (x)));
2714 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2716 if (TREE_PUBLIC (x))
2718 error ("address of global register variable `%s' requested",
2719 IDENTIFIER_POINTER (DECL_NAME (x)));
2720 return false;
2723 /* If we are making this addressable due to its having
2724 volatile components, give a different error message. Also
2725 handle the case of an unnamed parameter by not trying
2726 to give the name. */
2728 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2730 error ("cannot put object with volatile field into register");
2731 return false;
2734 pedwarn ("address of register variable `%s' requested",
2735 IDENTIFIER_POINTER (DECL_NAME (x)));
2737 put_var_into_stack (x, /*rescan=*/true);
2739 /* drops in */
2740 case FUNCTION_DECL:
2741 TREE_ADDRESSABLE (x) = 1;
2742 /* drops out */
2743 default:
2744 return true;
2748 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2750 tree
2751 build_conditional_expr (tree ifexp, tree op1, tree op2)
2753 tree type1;
2754 tree type2;
2755 enum tree_code code1;
2756 enum tree_code code2;
2757 tree result_type = NULL;
2758 tree orig_op1 = op1, orig_op2 = op2;
2760 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2762 /* Promote both alternatives. */
2764 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2765 op1 = default_conversion (op1);
2766 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2767 op2 = default_conversion (op2);
2769 if (TREE_CODE (ifexp) == ERROR_MARK
2770 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2771 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2772 return error_mark_node;
2774 type1 = TREE_TYPE (op1);
2775 code1 = TREE_CODE (type1);
2776 type2 = TREE_TYPE (op2);
2777 code2 = TREE_CODE (type2);
2779 /* Quickly detect the usual case where op1 and op2 have the same type
2780 after promotion. */
2781 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2783 if (type1 == type2)
2784 result_type = type1;
2785 else
2786 result_type = TYPE_MAIN_VARIANT (type1);
2788 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2789 || code1 == COMPLEX_TYPE)
2790 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2791 || code2 == COMPLEX_TYPE))
2793 result_type = common_type (type1, type2);
2795 /* If -Wsign-compare, warn here if type1 and type2 have
2796 different signedness. We'll promote the signed to unsigned
2797 and later code won't know it used to be different.
2798 Do this check on the original types, so that explicit casts
2799 will be considered, but default promotions won't. */
2800 if (warn_sign_compare && !skip_evaluation)
2802 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2803 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2805 if (unsigned_op1 ^ unsigned_op2)
2807 /* Do not warn if the result type is signed, since the
2808 signed type will only be chosen if it can represent
2809 all the values of the unsigned type. */
2810 if (! TREE_UNSIGNED (result_type))
2811 /* OK */;
2812 /* Do not warn if the signed quantity is an unsuffixed
2813 integer literal (or some static constant expression
2814 involving such literals) and it is non-negative. */
2815 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2816 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2817 /* OK */;
2818 else
2819 warning ("signed and unsigned type in conditional expression");
2823 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2825 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2826 pedwarn ("ISO C forbids conditional expr with only one void side");
2827 result_type = void_type_node;
2829 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2831 if (comp_target_types (type1, type2, 1))
2832 result_type = common_type (type1, type2);
2833 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2834 && TREE_CODE (orig_op1) != NOP_EXPR)
2835 result_type = qualify_type (type2, type1);
2836 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2837 && TREE_CODE (orig_op2) != NOP_EXPR)
2838 result_type = qualify_type (type1, type2);
2839 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2841 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2842 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2843 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2844 TREE_TYPE (type2)));
2846 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2848 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2849 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2850 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2851 TREE_TYPE (type1)));
2853 else
2855 pedwarn ("pointer type mismatch in conditional expression");
2856 result_type = build_pointer_type (void_type_node);
2859 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2861 if (! integer_zerop (op2))
2862 pedwarn ("pointer/integer type mismatch in conditional expression");
2863 else
2865 op2 = null_pointer_node;
2867 result_type = type1;
2869 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2871 if (!integer_zerop (op1))
2872 pedwarn ("pointer/integer type mismatch in conditional expression");
2873 else
2875 op1 = null_pointer_node;
2877 result_type = type2;
2880 if (!result_type)
2882 if (flag_cond_mismatch)
2883 result_type = void_type_node;
2884 else
2886 error ("type mismatch in conditional expression");
2887 return error_mark_node;
2891 /* Merge const and volatile flags of the incoming types. */
2892 result_type
2893 = build_type_variant (result_type,
2894 TREE_READONLY (op1) || TREE_READONLY (op2),
2895 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2897 if (result_type != TREE_TYPE (op1))
2898 op1 = convert_and_check (result_type, op1);
2899 if (result_type != TREE_TYPE (op2))
2900 op2 = convert_and_check (result_type, op2);
2902 if (TREE_CODE (ifexp) == INTEGER_CST)
2903 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2905 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2908 /* Given a list of expressions, return a compound expression
2909 that performs them all and returns the value of the last of them. */
2911 tree
2912 build_compound_expr (tree list)
2914 return internal_build_compound_expr (list, TRUE);
2917 static tree
2918 internal_build_compound_expr (tree list, int first_p)
2920 tree rest;
2922 if (TREE_CHAIN (list) == 0)
2924 /* Convert arrays and functions to pointers when there
2925 really is a comma operator. */
2926 if (!first_p)
2927 TREE_VALUE (list)
2928 = default_function_array_conversion (TREE_VALUE (list));
2930 /* Don't let (0, 0) be null pointer constant. */
2931 if (!first_p && integer_zerop (TREE_VALUE (list)))
2932 return non_lvalue (TREE_VALUE (list));
2933 return TREE_VALUE (list);
2936 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2938 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2940 /* The left-hand operand of a comma expression is like an expression
2941 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2942 any side-effects, unless it was explicitly cast to (void). */
2943 if (warn_unused_value
2944 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2945 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2946 warning ("left-hand operand of comma expression has no effect");
2949 /* With -Wunused, we should also warn if the left-hand operand does have
2950 side-effects, but computes a value which is not used. For example, in
2951 `foo() + bar(), baz()' the result of the `+' operator is not used,
2952 so we should issue a warning. */
2953 else if (warn_unused_value)
2954 warn_if_unused_value (TREE_VALUE (list));
2956 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2959 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2961 tree
2962 build_c_cast (tree type, tree expr)
2964 tree value = expr;
2966 if (type == error_mark_node || expr == error_mark_node)
2967 return error_mark_node;
2969 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2970 only in <protocol> qualifications. But when constructing cast expressions,
2971 the protocols do matter and must be kept around. */
2972 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2973 type = TYPE_MAIN_VARIANT (type);
2975 if (TREE_CODE (type) == ARRAY_TYPE)
2977 error ("cast specifies array type");
2978 return error_mark_node;
2981 if (TREE_CODE (type) == FUNCTION_TYPE)
2983 error ("cast specifies function type");
2984 return error_mark_node;
2987 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2989 if (pedantic)
2991 if (TREE_CODE (type) == RECORD_TYPE
2992 || TREE_CODE (type) == UNION_TYPE)
2993 pedwarn ("ISO C forbids casting nonscalar to the same type");
2996 else if (TREE_CODE (type) == UNION_TYPE)
2998 tree field;
2999 value = default_function_array_conversion (value);
3001 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3002 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3003 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3004 break;
3006 if (field)
3008 tree t;
3010 if (pedantic)
3011 pedwarn ("ISO C forbids casts to union type");
3012 t = digest_init (type,
3013 build_constructor (type,
3014 build_tree_list (field, value)),
3016 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3017 return t;
3019 error ("cast to union type from type not present in union");
3020 return error_mark_node;
3022 else
3024 tree otype, ovalue;
3026 /* If casting to void, avoid the error that would come
3027 from default_conversion in the case of a non-lvalue array. */
3028 if (type == void_type_node)
3029 return build1 (CONVERT_EXPR, type, value);
3031 /* Convert functions and arrays to pointers,
3032 but don't convert any other types. */
3033 value = default_function_array_conversion (value);
3034 otype = TREE_TYPE (value);
3036 /* Optionally warn about potentially worrisome casts. */
3038 if (warn_cast_qual
3039 && TREE_CODE (type) == POINTER_TYPE
3040 && TREE_CODE (otype) == POINTER_TYPE)
3042 tree in_type = type;
3043 tree in_otype = otype;
3044 int added = 0;
3045 int discarded = 0;
3047 /* Check that the qualifiers on IN_TYPE are a superset of
3048 the qualifiers of IN_OTYPE. The outermost level of
3049 POINTER_TYPE nodes is uninteresting and we stop as soon
3050 as we hit a non-POINTER_TYPE node on either type. */
3053 in_otype = TREE_TYPE (in_otype);
3054 in_type = TREE_TYPE (in_type);
3056 /* GNU C allows cv-qualified function types. 'const'
3057 means the function is very pure, 'volatile' means it
3058 can't return. We need to warn when such qualifiers
3059 are added, not when they're taken away. */
3060 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3061 && TREE_CODE (in_type) == FUNCTION_TYPE)
3062 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3063 else
3064 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3066 while (TREE_CODE (in_type) == POINTER_TYPE
3067 && TREE_CODE (in_otype) == POINTER_TYPE);
3069 if (added)
3070 warning ("cast adds new qualifiers to function type");
3072 if (discarded)
3073 /* There are qualifiers present in IN_OTYPE that are not
3074 present in IN_TYPE. */
3075 warning ("cast discards qualifiers from pointer target type");
3078 /* Warn about possible alignment problems. */
3079 if (STRICT_ALIGNMENT && warn_cast_align
3080 && TREE_CODE (type) == POINTER_TYPE
3081 && TREE_CODE (otype) == POINTER_TYPE
3082 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3083 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3084 /* Don't warn about opaque types, where the actual alignment
3085 restriction is unknown. */
3086 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3087 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3088 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3089 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3090 warning ("cast increases required alignment of target type");
3092 if (TREE_CODE (type) == INTEGER_TYPE
3093 && TREE_CODE (otype) == POINTER_TYPE
3094 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3095 && !TREE_CONSTANT (value))
3096 warning ("cast from pointer to integer of different size");
3098 if (warn_bad_function_cast
3099 && TREE_CODE (value) == CALL_EXPR
3100 && TREE_CODE (type) != TREE_CODE (otype))
3101 warning ("cast does not match function type");
3103 if (TREE_CODE (type) == POINTER_TYPE
3104 && TREE_CODE (otype) == INTEGER_TYPE
3105 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3106 /* Don't warn about converting any constant. */
3107 && !TREE_CONSTANT (value))
3108 warning ("cast to pointer from integer of different size");
3110 if (TREE_CODE (type) == POINTER_TYPE
3111 && TREE_CODE (otype) == POINTER_TYPE
3112 && TREE_CODE (expr) == ADDR_EXPR
3113 && DECL_P (TREE_OPERAND (expr, 0))
3114 && flag_strict_aliasing && warn_strict_aliasing
3115 && !VOID_TYPE_P (TREE_TYPE (type)))
3117 /* Casting the address of a decl to non void pointer. Warn
3118 if the cast breaks type based aliasing. */
3119 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3120 warning ("type-punning to incomplete type might break strict-aliasing rules");
3121 else if (!alias_sets_conflict_p
3122 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3123 get_alias_set (TREE_TYPE (type))))
3124 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3127 ovalue = value;
3128 /* Replace a nonvolatile const static variable with its value. */
3129 if (optimize && TREE_CODE (value) == VAR_DECL)
3130 value = decl_constant_value (value);
3131 value = convert (type, value);
3133 /* Ignore any integer overflow caused by the cast. */
3134 if (TREE_CODE (value) == INTEGER_CST)
3136 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3137 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3141 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3142 if (pedantic && TREE_CODE (value) == INTEGER_CST
3143 && TREE_CODE (expr) == INTEGER_CST
3144 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3145 value = non_lvalue (value);
3147 /* If pedantic, don't let a cast be an lvalue. */
3148 if (value == expr && pedantic)
3149 value = non_lvalue (value);
3151 return value;
3154 /* Interpret a cast of expression EXPR to type TYPE. */
3155 tree
3156 c_cast_expr (tree type, tree expr)
3158 int saved_wsp = warn_strict_prototypes;
3160 /* This avoids warnings about unprototyped casts on
3161 integers. E.g. "#define SIG_DFL (void(*)())0". */
3162 if (TREE_CODE (expr) == INTEGER_CST)
3163 warn_strict_prototypes = 0;
3164 type = groktypename (type);
3165 warn_strict_prototypes = saved_wsp;
3167 return build_c_cast (type, expr);
3171 /* Build an assignment expression of lvalue LHS from value RHS.
3172 MODIFYCODE is the code for a binary operator that we use
3173 to combine the old value of LHS with RHS to get the new value.
3174 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3176 tree
3177 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3179 tree result;
3180 tree newrhs;
3181 tree lhstype = TREE_TYPE (lhs);
3182 tree olhstype = lhstype;
3184 /* Types that aren't fully specified cannot be used in assignments. */
3185 lhs = require_complete_type (lhs);
3187 /* Avoid duplicate error messages from operands that had errors. */
3188 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3189 return error_mark_node;
3191 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3192 /* Do not use STRIP_NOPS here. We do not want an enumerator
3193 whose value is 0 to count as a null pointer constant. */
3194 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3195 rhs = TREE_OPERAND (rhs, 0);
3197 newrhs = rhs;
3199 /* Handle control structure constructs used as "lvalues". */
3201 switch (TREE_CODE (lhs))
3203 /* Handle (a, b) used as an "lvalue". */
3204 case COMPOUND_EXPR:
3205 pedantic_lvalue_warning (COMPOUND_EXPR);
3206 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3207 if (TREE_CODE (newrhs) == ERROR_MARK)
3208 return error_mark_node;
3209 return build (COMPOUND_EXPR, lhstype,
3210 TREE_OPERAND (lhs, 0), newrhs);
3212 /* Handle (a ? b : c) used as an "lvalue". */
3213 case COND_EXPR:
3214 pedantic_lvalue_warning (COND_EXPR);
3215 rhs = save_expr (rhs);
3217 /* Produce (a ? (b = rhs) : (c = rhs))
3218 except that the RHS goes through a save-expr
3219 so the code to compute it is only emitted once. */
3220 tree cond
3221 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3222 build_modify_expr (TREE_OPERAND (lhs, 1),
3223 modifycode, rhs),
3224 build_modify_expr (TREE_OPERAND (lhs, 2),
3225 modifycode, rhs));
3226 if (TREE_CODE (cond) == ERROR_MARK)
3227 return cond;
3228 /* Make sure the code to compute the rhs comes out
3229 before the split. */
3230 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3231 /* But cast it to void to avoid an "unused" error. */
3232 convert (void_type_node, rhs), cond);
3234 default:
3235 break;
3238 /* If a binary op has been requested, combine the old LHS value with the RHS
3239 producing the value we should actually store into the LHS. */
3241 if (modifycode != NOP_EXPR)
3243 lhs = stabilize_reference (lhs);
3244 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3247 /* Handle a cast used as an "lvalue".
3248 We have already performed any binary operator using the value as cast.
3249 Now convert the result to the cast type of the lhs,
3250 and then true type of the lhs and store it there;
3251 then convert result back to the cast type to be the value
3252 of the assignment. */
3254 switch (TREE_CODE (lhs))
3256 case NOP_EXPR:
3257 case CONVERT_EXPR:
3258 case FLOAT_EXPR:
3259 case FIX_TRUNC_EXPR:
3260 case FIX_FLOOR_EXPR:
3261 case FIX_ROUND_EXPR:
3262 case FIX_CEIL_EXPR:
3263 newrhs = default_function_array_conversion (newrhs);
3265 tree inner_lhs = TREE_OPERAND (lhs, 0);
3266 tree result;
3267 result = build_modify_expr (inner_lhs, NOP_EXPR,
3268 convert (TREE_TYPE (inner_lhs),
3269 convert (lhstype, newrhs)));
3270 if (TREE_CODE (result) == ERROR_MARK)
3271 return result;
3272 pedantic_lvalue_warning (CONVERT_EXPR);
3273 return convert (TREE_TYPE (lhs), result);
3276 default:
3277 break;
3280 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3281 Reject anything strange now. */
3283 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3284 return error_mark_node;
3286 /* Warn about storing in something that is `const'. */
3288 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3289 || ((TREE_CODE (lhstype) == RECORD_TYPE
3290 || TREE_CODE (lhstype) == UNION_TYPE)
3291 && C_TYPE_FIELDS_READONLY (lhstype)))
3292 readonly_warning (lhs, "assignment");
3294 /* If storing into a structure or union member,
3295 it has probably been given type `int'.
3296 Compute the type that would go with
3297 the actual amount of storage the member occupies. */
3299 if (TREE_CODE (lhs) == COMPONENT_REF
3300 && (TREE_CODE (lhstype) == INTEGER_TYPE
3301 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3302 || TREE_CODE (lhstype) == REAL_TYPE
3303 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3304 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3306 /* If storing in a field that is in actuality a short or narrower than one,
3307 we must store in the field in its actual type. */
3309 if (lhstype != TREE_TYPE (lhs))
3311 lhs = copy_node (lhs);
3312 TREE_TYPE (lhs) = lhstype;
3315 /* Convert new value to destination type. */
3317 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3318 NULL_TREE, NULL_TREE, 0);
3319 if (TREE_CODE (newrhs) == ERROR_MARK)
3320 return error_mark_node;
3322 /* Scan operands */
3324 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3325 TREE_SIDE_EFFECTS (result) = 1;
3327 /* If we got the LHS in a different type for storing in,
3328 convert the result back to the nominal type of LHS
3329 so that the value we return always has the same type
3330 as the LHS argument. */
3332 if (olhstype == TREE_TYPE (result))
3333 return result;
3334 return convert_for_assignment (olhstype, result, _("assignment"),
3335 NULL_TREE, NULL_TREE, 0);
3338 /* Convert value RHS to type TYPE as preparation for an assignment
3339 to an lvalue of type TYPE.
3340 The real work of conversion is done by `convert'.
3341 The purpose of this function is to generate error messages
3342 for assignments that are not allowed in C.
3343 ERRTYPE is a string to use in error messages:
3344 "assignment", "return", etc. If it is null, this is parameter passing
3345 for a function call (and different error messages are output).
3347 FUNNAME is the name of the function being called,
3348 as an IDENTIFIER_NODE, or null.
3349 PARMNUM is the number of the argument, for printing in error messages. */
3351 static tree
3352 convert_for_assignment (tree type, tree rhs, const char *errtype,
3353 tree fundecl, tree funname, int parmnum)
3355 enum tree_code codel = TREE_CODE (type);
3356 tree rhstype;
3357 enum tree_code coder;
3359 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3360 /* Do not use STRIP_NOPS here. We do not want an enumerator
3361 whose value is 0 to count as a null pointer constant. */
3362 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3363 rhs = TREE_OPERAND (rhs, 0);
3365 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3366 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3367 rhs = default_conversion (rhs);
3368 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3369 rhs = decl_constant_value_for_broken_optimization (rhs);
3371 rhstype = TREE_TYPE (rhs);
3372 coder = TREE_CODE (rhstype);
3374 if (coder == ERROR_MARK)
3375 return error_mark_node;
3377 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3379 overflow_warning (rhs);
3380 /* Check for Objective-C protocols. This will automatically
3381 issue a warning if there are protocol violations. No need to
3382 use the return value. */
3383 if (c_dialect_objc ())
3384 objc_comptypes (type, rhstype, 0);
3385 return rhs;
3388 if (coder == VOID_TYPE)
3390 error ("void value not ignored as it ought to be");
3391 return error_mark_node;
3393 /* A type converts to a reference to it.
3394 This code doesn't fully support references, it's just for the
3395 special case of va_start and va_copy. */
3396 if (codel == REFERENCE_TYPE
3397 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3399 if (!lvalue_p (rhs))
3401 error ("cannot pass rvalue to reference parameter");
3402 return error_mark_node;
3404 if (!c_mark_addressable (rhs))
3405 return error_mark_node;
3406 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3408 /* We already know that these two types are compatible, but they
3409 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3410 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3411 likely to be va_list, a typedef to __builtin_va_list, which
3412 is different enough that it will cause problems later. */
3413 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3414 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3416 rhs = build1 (NOP_EXPR, type, rhs);
3417 return rhs;
3419 /* Some types can interconvert without explicit casts. */
3420 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3421 && ((*targetm.vector_opaque_p) (type)
3422 || (*targetm.vector_opaque_p) (rhstype)))
3423 return convert (type, rhs);
3424 /* Arithmetic types all interconvert, and enum is treated like int. */
3425 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3426 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3427 || codel == BOOLEAN_TYPE)
3428 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3429 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3430 || coder == BOOLEAN_TYPE))
3431 return convert_and_check (type, rhs);
3433 /* Conversion to a transparent union from its member types.
3434 This applies only to function arguments. */
3435 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3437 tree memb_types;
3438 tree marginal_memb_type = 0;
3440 for (memb_types = TYPE_FIELDS (type); memb_types;
3441 memb_types = TREE_CHAIN (memb_types))
3443 tree memb_type = TREE_TYPE (memb_types);
3445 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3446 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3447 break;
3449 if (TREE_CODE (memb_type) != POINTER_TYPE)
3450 continue;
3452 if (coder == POINTER_TYPE)
3454 tree ttl = TREE_TYPE (memb_type);
3455 tree ttr = TREE_TYPE (rhstype);
3457 /* Any non-function converts to a [const][volatile] void *
3458 and vice versa; otherwise, targets must be the same.
3459 Meanwhile, the lhs target must have all the qualifiers of
3460 the rhs. */
3461 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3462 || comp_target_types (memb_type, rhstype, 0))
3464 /* If this type won't generate any warnings, use it. */
3465 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3466 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3467 && TREE_CODE (ttl) == FUNCTION_TYPE)
3468 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3469 == TYPE_QUALS (ttr))
3470 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3471 == TYPE_QUALS (ttl))))
3472 break;
3474 /* Keep looking for a better type, but remember this one. */
3475 if (! marginal_memb_type)
3476 marginal_memb_type = memb_type;
3480 /* Can convert integer zero to any pointer type. */
3481 if (integer_zerop (rhs)
3482 || (TREE_CODE (rhs) == NOP_EXPR
3483 && integer_zerop (TREE_OPERAND (rhs, 0))))
3485 rhs = null_pointer_node;
3486 break;
3490 if (memb_types || marginal_memb_type)
3492 if (! memb_types)
3494 /* We have only a marginally acceptable member type;
3495 it needs a warning. */
3496 tree ttl = TREE_TYPE (marginal_memb_type);
3497 tree ttr = TREE_TYPE (rhstype);
3499 /* Const and volatile mean something different for function
3500 types, so the usual warnings are not appropriate. */
3501 if (TREE_CODE (ttr) == FUNCTION_TYPE
3502 && TREE_CODE (ttl) == FUNCTION_TYPE)
3504 /* Because const and volatile on functions are
3505 restrictions that say the function will not do
3506 certain things, it is okay to use a const or volatile
3507 function where an ordinary one is wanted, but not
3508 vice-versa. */
3509 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3510 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3511 errtype, funname, parmnum);
3513 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3514 warn_for_assignment ("%s discards qualifiers from pointer target type",
3515 errtype, funname,
3516 parmnum);
3519 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3520 pedwarn ("ISO C prohibits argument conversion to union type");
3522 return build1 (NOP_EXPR, type, rhs);
3526 /* Conversions among pointers */
3527 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3528 && (coder == codel))
3530 tree ttl = TREE_TYPE (type);
3531 tree ttr = TREE_TYPE (rhstype);
3532 bool is_opaque_pointer;
3533 int target_cmp = 0; /* Cache comp_target_types () result. */
3535 /* Opaque pointers are treated like void pointers. */
3536 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3537 || (*targetm.vector_opaque_p) (rhstype))
3538 && TREE_CODE (ttl) == VECTOR_TYPE
3539 && TREE_CODE (ttr) == VECTOR_TYPE;
3541 /* Any non-function converts to a [const][volatile] void *
3542 and vice versa; otherwise, targets must be the same.
3543 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3544 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3545 || (target_cmp = comp_target_types (type, rhstype, 0))
3546 || is_opaque_pointer
3547 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3548 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3550 if (pedantic
3551 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3553 (VOID_TYPE_P (ttr)
3554 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3555 which are not ANSI null ptr constants. */
3556 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3557 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3558 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3559 errtype, funname, parmnum);
3560 /* Const and volatile mean something different for function types,
3561 so the usual warnings are not appropriate. */
3562 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3563 && TREE_CODE (ttl) != FUNCTION_TYPE)
3565 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3566 warn_for_assignment ("%s discards qualifiers from pointer target type",
3567 errtype, funname, parmnum);
3568 /* If this is not a case of ignoring a mismatch in signedness,
3569 no warning. */
3570 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3571 || target_cmp)
3573 /* If there is a mismatch, do warn. */
3574 else if (pedantic)
3575 warn_for_assignment ("pointer targets in %s differ in signedness",
3576 errtype, funname, parmnum);
3578 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3579 && TREE_CODE (ttr) == FUNCTION_TYPE)
3581 /* Because const and volatile on functions are restrictions
3582 that say the function will not do certain things,
3583 it is okay to use a const or volatile function
3584 where an ordinary one is wanted, but not vice-versa. */
3585 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3586 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3587 errtype, funname, parmnum);
3590 else
3591 warn_for_assignment ("%s from incompatible pointer type",
3592 errtype, funname, parmnum);
3593 return convert (type, rhs);
3595 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3597 error ("invalid use of non-lvalue array");
3598 return error_mark_node;
3600 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3602 /* An explicit constant 0 can convert to a pointer,
3603 or one that results from arithmetic, even including
3604 a cast to integer type. */
3605 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3607 ! (TREE_CODE (rhs) == NOP_EXPR
3608 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3609 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3610 && integer_zerop (TREE_OPERAND (rhs, 0))))
3612 warn_for_assignment ("%s makes pointer from integer without a cast",
3613 errtype, funname, parmnum);
3614 return convert (type, rhs);
3616 return null_pointer_node;
3618 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3620 warn_for_assignment ("%s makes integer from pointer without a cast",
3621 errtype, funname, parmnum);
3622 return convert (type, rhs);
3624 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3625 return convert (type, rhs);
3627 if (!errtype)
3629 if (funname)
3631 tree selector = objc_message_selector ();
3633 if (selector && parmnum > 2)
3634 error ("incompatible type for argument %d of `%s'",
3635 parmnum - 2, IDENTIFIER_POINTER (selector));
3636 else
3637 error ("incompatible type for argument %d of `%s'",
3638 parmnum, IDENTIFIER_POINTER (funname));
3640 else
3641 error ("incompatible type for argument %d of indirect function call",
3642 parmnum);
3644 else
3645 error ("incompatible types in %s", errtype);
3647 return error_mark_node;
3650 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3651 is used for error and waring reporting and indicates which argument
3652 is being processed. */
3654 tree
3655 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3657 tree ret, type;
3659 /* If FN was prototyped, the value has been converted already
3660 in convert_arguments. */
3661 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3662 return value;
3664 type = TREE_TYPE (parm);
3665 ret = convert_for_assignment (type, value,
3666 (char *) 0 /* arg passing */, fn,
3667 DECL_NAME (fn), argnum);
3668 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3669 && INTEGRAL_TYPE_P (type)
3670 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3671 ret = default_conversion (ret);
3672 return ret;
3675 /* Print a warning using MSGID.
3676 It gets OPNAME as its one parameter.
3677 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3678 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3679 FUNCTION and ARGNUM are handled specially if we are building an
3680 Objective-C selector. */
3682 static void
3683 warn_for_assignment (const char *msgid, const char *opname, tree function,
3684 int argnum)
3686 if (opname == 0)
3688 tree selector = objc_message_selector ();
3689 char * new_opname;
3691 if (selector && argnum > 2)
3693 function = selector;
3694 argnum -= 2;
3696 if (argnum == 0)
3698 if (function)
3700 /* Function name is known; supply it. */
3701 const char *const argstring = _("passing arg of `%s'");
3702 new_opname = alloca (IDENTIFIER_LENGTH (function)
3703 + strlen (argstring) + 1 + 1);
3704 sprintf (new_opname, argstring,
3705 IDENTIFIER_POINTER (function));
3707 else
3709 /* Function name unknown (call through ptr). */
3710 const char *const argnofun = _("passing arg of pointer to function");
3711 new_opname = alloca (strlen (argnofun) + 1 + 1);
3712 sprintf (new_opname, argnofun);
3715 else if (function)
3717 /* Function name is known; supply it. */
3718 const char *const argstring = _("passing arg %d of `%s'");
3719 new_opname = alloca (IDENTIFIER_LENGTH (function)
3720 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3721 sprintf (new_opname, argstring, argnum,
3722 IDENTIFIER_POINTER (function));
3724 else
3726 /* Function name unknown (call through ptr); just give arg number. */
3727 const char *const argnofun = _("passing arg %d of pointer to function");
3728 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3729 sprintf (new_opname, argnofun, argnum);
3731 opname = new_opname;
3733 pedwarn (msgid, opname);
3736 /* If VALUE is a compound expr all of whose expressions are constant, then
3737 return its value. Otherwise, return error_mark_node.
3739 This is for handling COMPOUND_EXPRs as initializer elements
3740 which is allowed with a warning when -pedantic is specified. */
3742 static tree
3743 valid_compound_expr_initializer (tree value, tree endtype)
3745 if (TREE_CODE (value) == COMPOUND_EXPR)
3747 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3748 == error_mark_node)
3749 return error_mark_node;
3750 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3751 endtype);
3753 else if (! TREE_CONSTANT (value)
3754 && ! initializer_constant_valid_p (value, endtype))
3755 return error_mark_node;
3756 else
3757 return value;
3760 /* Perform appropriate conversions on the initial value of a variable,
3761 store it in the declaration DECL,
3762 and print any error messages that are appropriate.
3763 If the init is invalid, store an ERROR_MARK. */
3765 void
3766 store_init_value (tree decl, tree init)
3768 tree value, type;
3770 /* If variable's type was invalidly declared, just ignore it. */
3772 type = TREE_TYPE (decl);
3773 if (TREE_CODE (type) == ERROR_MARK)
3774 return;
3776 /* Digest the specified initializer into an expression. */
3778 value = digest_init (type, init, TREE_STATIC (decl));
3780 /* Store the expression if valid; else report error. */
3782 if (warn_traditional && !in_system_header
3783 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3784 warning ("traditional C rejects automatic aggregate initialization");
3786 DECL_INITIAL (decl) = value;
3788 /* ANSI wants warnings about out-of-range constant initializers. */
3789 STRIP_TYPE_NOPS (value);
3790 constant_expression_warning (value);
3792 /* Check if we need to set array size from compound literal size. */
3793 if (TREE_CODE (type) == ARRAY_TYPE
3794 && TYPE_DOMAIN (type) == 0
3795 && value != error_mark_node)
3797 tree inside_init = init;
3799 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3800 inside_init = TREE_OPERAND (init, 0);
3801 inside_init = fold (inside_init);
3803 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3805 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3807 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3809 /* For int foo[] = (int [3]){1}; we need to set array size
3810 now since later on array initializer will be just the
3811 brace enclosed list of the compound literal. */
3812 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3813 layout_type (type);
3814 layout_decl (decl, 0);
3820 /* Methods for storing and printing names for error messages. */
3822 /* Implement a spelling stack that allows components of a name to be pushed
3823 and popped. Each element on the stack is this structure. */
3825 struct spelling
3827 int kind;
3828 union
3830 int i;
3831 const char *s;
3832 } u;
3835 #define SPELLING_STRING 1
3836 #define SPELLING_MEMBER 2
3837 #define SPELLING_BOUNDS 3
3839 static struct spelling *spelling; /* Next stack element (unused). */
3840 static struct spelling *spelling_base; /* Spelling stack base. */
3841 static int spelling_size; /* Size of the spelling stack. */
3843 /* Macros to save and restore the spelling stack around push_... functions.
3844 Alternative to SAVE_SPELLING_STACK. */
3846 #define SPELLING_DEPTH() (spelling - spelling_base)
3847 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3849 /* Push an element on the spelling stack with type KIND and assign VALUE
3850 to MEMBER. */
3852 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3854 int depth = SPELLING_DEPTH (); \
3856 if (depth >= spelling_size) \
3858 spelling_size += 10; \
3859 if (spelling_base == 0) \
3860 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3861 else \
3862 spelling_base = xrealloc (spelling_base, \
3863 spelling_size * sizeof (struct spelling)); \
3864 RESTORE_SPELLING_DEPTH (depth); \
3867 spelling->kind = (KIND); \
3868 spelling->MEMBER = (VALUE); \
3869 spelling++; \
3872 /* Push STRING on the stack. Printed literally. */
3874 static void
3875 push_string (const char *string)
3877 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3880 /* Push a member name on the stack. Printed as '.' STRING. */
3882 static void
3883 push_member_name (tree decl)
3885 const char *const string
3886 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3887 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3890 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3892 static void
3893 push_array_bounds (int bounds)
3895 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3898 /* Compute the maximum size in bytes of the printed spelling. */
3900 static int
3901 spelling_length (void)
3903 int size = 0;
3904 struct spelling *p;
3906 for (p = spelling_base; p < spelling; p++)
3908 if (p->kind == SPELLING_BOUNDS)
3909 size += 25;
3910 else
3911 size += strlen (p->u.s) + 1;
3914 return size;
3917 /* Print the spelling to BUFFER and return it. */
3919 static char *
3920 print_spelling (char *buffer)
3922 char *d = buffer;
3923 struct spelling *p;
3925 for (p = spelling_base; p < spelling; p++)
3926 if (p->kind == SPELLING_BOUNDS)
3928 sprintf (d, "[%d]", p->u.i);
3929 d += strlen (d);
3931 else
3933 const char *s;
3934 if (p->kind == SPELLING_MEMBER)
3935 *d++ = '.';
3936 for (s = p->u.s; (*d = *s++); d++)
3939 *d++ = '\0';
3940 return buffer;
3943 /* Issue an error message for a bad initializer component.
3944 MSGID identifies the message.
3945 The component name is taken from the spelling stack. */
3947 void
3948 error_init (const char *msgid)
3950 char *ofwhat;
3952 error ("%s", _(msgid));
3953 ofwhat = print_spelling (alloca (spelling_length () + 1));
3954 if (*ofwhat)
3955 error ("(near initialization for `%s')", ofwhat);
3958 /* Issue a pedantic warning for a bad initializer component.
3959 MSGID identifies the message.
3960 The component name is taken from the spelling stack. */
3962 void
3963 pedwarn_init (const char *msgid)
3965 char *ofwhat;
3967 pedwarn ("%s", _(msgid));
3968 ofwhat = print_spelling (alloca (spelling_length () + 1));
3969 if (*ofwhat)
3970 pedwarn ("(near initialization for `%s')", ofwhat);
3973 /* Issue a warning for a bad initializer component.
3974 MSGID identifies the message.
3975 The component name is taken from the spelling stack. */
3977 static void
3978 warning_init (const char *msgid)
3980 char *ofwhat;
3982 warning ("%s", _(msgid));
3983 ofwhat = print_spelling (alloca (spelling_length () + 1));
3984 if (*ofwhat)
3985 warning ("(near initialization for `%s')", ofwhat);
3988 /* Digest the parser output INIT as an initializer for type TYPE.
3989 Return a C expression of type TYPE to represent the initial value.
3991 REQUIRE_CONSTANT requests an error if non-constant initializers or
3992 elements are seen. */
3994 static tree
3995 digest_init (tree type, tree init, int require_constant)
3997 enum tree_code code = TREE_CODE (type);
3998 tree inside_init = init;
4000 if (type == error_mark_node
4001 || init == error_mark_node
4002 || TREE_TYPE (init) == error_mark_node)
4003 return error_mark_node;
4005 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4006 /* Do not use STRIP_NOPS here. We do not want an enumerator
4007 whose value is 0 to count as a null pointer constant. */
4008 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4009 inside_init = TREE_OPERAND (init, 0);
4011 inside_init = fold (inside_init);
4013 /* Initialization of an array of chars from a string constant
4014 optionally enclosed in braces. */
4016 if (code == ARRAY_TYPE)
4018 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4019 if ((typ1 == char_type_node
4020 || typ1 == signed_char_type_node
4021 || typ1 == unsigned_char_type_node
4022 || typ1 == unsigned_wchar_type_node
4023 || typ1 == signed_wchar_type_node)
4024 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4026 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4027 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
4028 return inside_init;
4030 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4031 != char_type_node)
4032 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4034 error_init ("char-array initialized from wide string");
4035 return error_mark_node;
4037 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4038 == char_type_node)
4039 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4041 error_init ("int-array initialized from non-wide string");
4042 return error_mark_node;
4045 TREE_TYPE (inside_init) = type;
4046 if (TYPE_DOMAIN (type) != 0
4047 && TYPE_SIZE (type) != 0
4048 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4049 /* Subtract 1 (or sizeof (wchar_t))
4050 because it's ok to ignore the terminating null char
4051 that is counted in the length of the constant. */
4052 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4053 TREE_STRING_LENGTH (inside_init)
4054 - ((TYPE_PRECISION (typ1)
4055 != TYPE_PRECISION (char_type_node))
4056 ? (TYPE_PRECISION (wchar_type_node)
4057 / BITS_PER_UNIT)
4058 : 1)))
4059 pedwarn_init ("initializer-string for array of chars is too long");
4061 return inside_init;
4065 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4066 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4067 below and handle as a constructor. */
4068 if (code == VECTOR_TYPE
4069 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
4070 && TREE_CONSTANT (inside_init))
4072 if (TREE_CODE (inside_init) == VECTOR_CST
4073 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4074 TYPE_MAIN_VARIANT (type),
4075 COMPARE_STRICT))
4076 return inside_init;
4077 else
4078 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4081 /* Any type can be initialized
4082 from an expression of the same type, optionally with braces. */
4084 if (inside_init && TREE_TYPE (inside_init) != 0
4085 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4086 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4087 || (code == ARRAY_TYPE
4088 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4089 || (code == VECTOR_TYPE
4090 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4091 || (code == POINTER_TYPE
4092 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4093 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4094 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4095 TREE_TYPE (type), COMPARE_STRICT))))
4097 if (code == POINTER_TYPE)
4099 inside_init = default_function_array_conversion (inside_init);
4101 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4103 error_init ("invalid use of non-lvalue array");
4104 return error_mark_node;
4108 if (code == VECTOR_TYPE)
4109 /* Although the types are compatible, we may require a
4110 conversion. */
4111 inside_init = convert (type, inside_init);
4113 if (require_constant && !flag_isoc99
4114 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4116 /* As an extension, allow initializing objects with static storage
4117 duration with compound literals (which are then treated just as
4118 the brace enclosed list they contain). */
4119 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4120 inside_init = DECL_INITIAL (decl);
4123 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4124 && TREE_CODE (inside_init) != CONSTRUCTOR)
4126 error_init ("array initialized from non-constant array expression");
4127 return error_mark_node;
4130 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4131 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4133 /* Compound expressions can only occur here if -pedantic or
4134 -pedantic-errors is specified. In the later case, we always want
4135 an error. In the former case, we simply want a warning. */
4136 if (require_constant && pedantic
4137 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4139 inside_init
4140 = valid_compound_expr_initializer (inside_init,
4141 TREE_TYPE (inside_init));
4142 if (inside_init == error_mark_node)
4143 error_init ("initializer element is not constant");
4144 else
4145 pedwarn_init ("initializer element is not constant");
4146 if (flag_pedantic_errors)
4147 inside_init = error_mark_node;
4149 else if (require_constant
4150 && (!TREE_CONSTANT (inside_init)
4151 /* This test catches things like `7 / 0' which
4152 result in an expression for which TREE_CONSTANT
4153 is true, but which is not actually something
4154 that is a legal constant. We really should not
4155 be using this function, because it is a part of
4156 the back-end. Instead, the expression should
4157 already have been turned into ERROR_MARK_NODE. */
4158 || !initializer_constant_valid_p (inside_init,
4159 TREE_TYPE (inside_init))))
4161 error_init ("initializer element is not constant");
4162 inside_init = error_mark_node;
4165 return inside_init;
4168 /* Handle scalar types, including conversions. */
4170 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4171 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4173 /* Note that convert_for_assignment calls default_conversion
4174 for arrays and functions. We must not call it in the
4175 case where inside_init is a null pointer constant. */
4176 inside_init
4177 = convert_for_assignment (type, init, _("initialization"),
4178 NULL_TREE, NULL_TREE, 0);
4180 if (require_constant && ! TREE_CONSTANT (inside_init))
4182 error_init ("initializer element is not constant");
4183 inside_init = error_mark_node;
4185 else if (require_constant
4186 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4188 error_init ("initializer element is not computable at load time");
4189 inside_init = error_mark_node;
4192 return inside_init;
4195 /* Come here only for records and arrays. */
4197 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4199 error_init ("variable-sized object may not be initialized");
4200 return error_mark_node;
4203 error_init ("invalid initializer");
4204 return error_mark_node;
4207 /* Handle initializers that use braces. */
4209 /* Type of object we are accumulating a constructor for.
4210 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4211 static tree constructor_type;
4213 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4214 left to fill. */
4215 static tree constructor_fields;
4217 /* For an ARRAY_TYPE, this is the specified index
4218 at which to store the next element we get. */
4219 static tree constructor_index;
4221 /* For an ARRAY_TYPE, this is the maximum index. */
4222 static tree constructor_max_index;
4224 /* For a RECORD_TYPE, this is the first field not yet written out. */
4225 static tree constructor_unfilled_fields;
4227 /* For an ARRAY_TYPE, this is the index of the first element
4228 not yet written out. */
4229 static tree constructor_unfilled_index;
4231 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4232 This is so we can generate gaps between fields, when appropriate. */
4233 static tree constructor_bit_index;
4235 /* If we are saving up the elements rather than allocating them,
4236 this is the list of elements so far (in reverse order,
4237 most recent first). */
4238 static tree constructor_elements;
4240 /* 1 if constructor should be incrementally stored into a constructor chain,
4241 0 if all the elements should be kept in AVL tree. */
4242 static int constructor_incremental;
4244 /* 1 if so far this constructor's elements are all compile-time constants. */
4245 static int constructor_constant;
4247 /* 1 if so far this constructor's elements are all valid address constants. */
4248 static int constructor_simple;
4250 /* 1 if this constructor is erroneous so far. */
4251 static int constructor_erroneous;
4253 /* Structure for managing pending initializer elements, organized as an
4254 AVL tree. */
4256 struct init_node
4258 struct init_node *left, *right;
4259 struct init_node *parent;
4260 int balance;
4261 tree purpose;
4262 tree value;
4265 /* Tree of pending elements at this constructor level.
4266 These are elements encountered out of order
4267 which belong at places we haven't reached yet in actually
4268 writing the output.
4269 Will never hold tree nodes across GC runs. */
4270 static struct init_node *constructor_pending_elts;
4272 /* The SPELLING_DEPTH of this constructor. */
4273 static int constructor_depth;
4275 /* 0 if implicitly pushing constructor levels is allowed. */
4276 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4278 static int require_constant_value;
4279 static int require_constant_elements;
4281 /* DECL node for which an initializer is being read.
4282 0 means we are reading a constructor expression
4283 such as (struct foo) {...}. */
4284 static tree constructor_decl;
4286 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4287 static const char *constructor_asmspec;
4289 /* Nonzero if this is an initializer for a top-level decl. */
4290 static int constructor_top_level;
4292 /* Nonzero if there were any member designators in this initializer. */
4293 static int constructor_designated;
4295 /* Nesting depth of designator list. */
4296 static int designator_depth;
4298 /* Nonzero if there were diagnosed errors in this designator list. */
4299 static int designator_errorneous;
4302 /* This stack has a level for each implicit or explicit level of
4303 structuring in the initializer, including the outermost one. It
4304 saves the values of most of the variables above. */
4306 struct constructor_range_stack;
4308 struct constructor_stack
4310 struct constructor_stack *next;
4311 tree type;
4312 tree fields;
4313 tree index;
4314 tree max_index;
4315 tree unfilled_index;
4316 tree unfilled_fields;
4317 tree bit_index;
4318 tree elements;
4319 struct init_node *pending_elts;
4320 int offset;
4321 int depth;
4322 /* If nonzero, this value should replace the entire
4323 constructor at this level. */
4324 tree replacement_value;
4325 struct constructor_range_stack *range_stack;
4326 char constant;
4327 char simple;
4328 char implicit;
4329 char erroneous;
4330 char outer;
4331 char incremental;
4332 char designated;
4335 struct constructor_stack *constructor_stack;
4337 /* This stack represents designators from some range designator up to
4338 the last designator in the list. */
4340 struct constructor_range_stack
4342 struct constructor_range_stack *next, *prev;
4343 struct constructor_stack *stack;
4344 tree range_start;
4345 tree index;
4346 tree range_end;
4347 tree fields;
4350 struct constructor_range_stack *constructor_range_stack;
4352 /* This stack records separate initializers that are nested.
4353 Nested initializers can't happen in ANSI C, but GNU C allows them
4354 in cases like { ... (struct foo) { ... } ... }. */
4356 struct initializer_stack
4358 struct initializer_stack *next;
4359 tree decl;
4360 const char *asmspec;
4361 struct constructor_stack *constructor_stack;
4362 struct constructor_range_stack *constructor_range_stack;
4363 tree elements;
4364 struct spelling *spelling;
4365 struct spelling *spelling_base;
4366 int spelling_size;
4367 char top_level;
4368 char require_constant_value;
4369 char require_constant_elements;
4372 struct initializer_stack *initializer_stack;
4374 /* Prepare to parse and output the initializer for variable DECL. */
4376 void
4377 start_init (tree decl, tree asmspec_tree, int top_level)
4379 const char *locus;
4380 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4381 const char *asmspec = 0;
4383 if (asmspec_tree)
4384 asmspec = TREE_STRING_POINTER (asmspec_tree);
4386 p->decl = constructor_decl;
4387 p->asmspec = constructor_asmspec;
4388 p->require_constant_value = require_constant_value;
4389 p->require_constant_elements = require_constant_elements;
4390 p->constructor_stack = constructor_stack;
4391 p->constructor_range_stack = constructor_range_stack;
4392 p->elements = constructor_elements;
4393 p->spelling = spelling;
4394 p->spelling_base = spelling_base;
4395 p->spelling_size = spelling_size;
4396 p->top_level = constructor_top_level;
4397 p->next = initializer_stack;
4398 initializer_stack = p;
4400 constructor_decl = decl;
4401 constructor_asmspec = asmspec;
4402 constructor_designated = 0;
4403 constructor_top_level = top_level;
4405 if (decl != 0)
4407 require_constant_value = TREE_STATIC (decl);
4408 require_constant_elements
4409 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4410 /* For a scalar, you can always use any value to initialize,
4411 even within braces. */
4412 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4413 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4414 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4415 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4416 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4418 else
4420 require_constant_value = 0;
4421 require_constant_elements = 0;
4422 locus = "(anonymous)";
4425 constructor_stack = 0;
4426 constructor_range_stack = 0;
4428 missing_braces_mentioned = 0;
4430 spelling_base = 0;
4431 spelling_size = 0;
4432 RESTORE_SPELLING_DEPTH (0);
4434 if (locus)
4435 push_string (locus);
4438 void
4439 finish_init (void)
4441 struct initializer_stack *p = initializer_stack;
4443 /* Free the whole constructor stack of this initializer. */
4444 while (constructor_stack)
4446 struct constructor_stack *q = constructor_stack;
4447 constructor_stack = q->next;
4448 free (q);
4451 if (constructor_range_stack)
4452 abort ();
4454 /* Pop back to the data of the outer initializer (if any). */
4455 constructor_decl = p->decl;
4456 constructor_asmspec = p->asmspec;
4457 require_constant_value = p->require_constant_value;
4458 require_constant_elements = p->require_constant_elements;
4459 constructor_stack = p->constructor_stack;
4460 constructor_range_stack = p->constructor_range_stack;
4461 constructor_elements = p->elements;
4462 spelling = p->spelling;
4463 spelling_base = p->spelling_base;
4464 spelling_size = p->spelling_size;
4465 constructor_top_level = p->top_level;
4466 initializer_stack = p->next;
4467 free (p);
4470 /* Call here when we see the initializer is surrounded by braces.
4471 This is instead of a call to push_init_level;
4472 it is matched by a call to pop_init_level.
4474 TYPE is the type to initialize, for a constructor expression.
4475 For an initializer for a decl, TYPE is zero. */
4477 void
4478 really_start_incremental_init (tree type)
4480 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4482 if (type == 0)
4483 type = TREE_TYPE (constructor_decl);
4485 if ((*targetm.vector_opaque_p) (type))
4486 error ("opaque vector types cannot be initialized");
4488 p->type = constructor_type;
4489 p->fields = constructor_fields;
4490 p->index = constructor_index;
4491 p->max_index = constructor_max_index;
4492 p->unfilled_index = constructor_unfilled_index;
4493 p->unfilled_fields = constructor_unfilled_fields;
4494 p->bit_index = constructor_bit_index;
4495 p->elements = constructor_elements;
4496 p->constant = constructor_constant;
4497 p->simple = constructor_simple;
4498 p->erroneous = constructor_erroneous;
4499 p->pending_elts = constructor_pending_elts;
4500 p->depth = constructor_depth;
4501 p->replacement_value = 0;
4502 p->implicit = 0;
4503 p->range_stack = 0;
4504 p->outer = 0;
4505 p->incremental = constructor_incremental;
4506 p->designated = constructor_designated;
4507 p->next = 0;
4508 constructor_stack = p;
4510 constructor_constant = 1;
4511 constructor_simple = 1;
4512 constructor_depth = SPELLING_DEPTH ();
4513 constructor_elements = 0;
4514 constructor_pending_elts = 0;
4515 constructor_type = type;
4516 constructor_incremental = 1;
4517 constructor_designated = 0;
4518 designator_depth = 0;
4519 designator_errorneous = 0;
4521 if (TREE_CODE (constructor_type) == RECORD_TYPE
4522 || TREE_CODE (constructor_type) == UNION_TYPE)
4524 constructor_fields = TYPE_FIELDS (constructor_type);
4525 /* Skip any nameless bit fields at the beginning. */
4526 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4527 && DECL_NAME (constructor_fields) == 0)
4528 constructor_fields = TREE_CHAIN (constructor_fields);
4530 constructor_unfilled_fields = constructor_fields;
4531 constructor_bit_index = bitsize_zero_node;
4533 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4535 if (TYPE_DOMAIN (constructor_type))
4537 constructor_max_index
4538 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4540 /* Detect non-empty initializations of zero-length arrays. */
4541 if (constructor_max_index == NULL_TREE
4542 && TYPE_SIZE (constructor_type))
4543 constructor_max_index = build_int_2 (-1, -1);
4545 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4546 to initialize VLAs will cause a proper error; avoid tree
4547 checking errors as well by setting a safe value. */
4548 if (constructor_max_index
4549 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4550 constructor_max_index = build_int_2 (-1, -1);
4552 constructor_index
4553 = convert (bitsizetype,
4554 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4556 else
4557 constructor_index = bitsize_zero_node;
4559 constructor_unfilled_index = constructor_index;
4561 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4563 /* Vectors are like simple fixed-size arrays. */
4564 constructor_max_index =
4565 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4566 constructor_index = convert (bitsizetype, bitsize_zero_node);
4567 constructor_unfilled_index = constructor_index;
4569 else
4571 /* Handle the case of int x = {5}; */
4572 constructor_fields = constructor_type;
4573 constructor_unfilled_fields = constructor_type;
4577 /* Push down into a subobject, for initialization.
4578 If this is for an explicit set of braces, IMPLICIT is 0.
4579 If it is because the next element belongs at a lower level,
4580 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4582 void
4583 push_init_level (int implicit)
4585 struct constructor_stack *p;
4586 tree value = NULL_TREE;
4588 /* If we've exhausted any levels that didn't have braces,
4589 pop them now. */
4590 while (constructor_stack->implicit)
4592 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4593 || TREE_CODE (constructor_type) == UNION_TYPE)
4594 && constructor_fields == 0)
4595 process_init_element (pop_init_level (1));
4596 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4597 && constructor_max_index
4598 && tree_int_cst_lt (constructor_max_index, constructor_index))
4599 process_init_element (pop_init_level (1));
4600 else
4601 break;
4604 /* Unless this is an explicit brace, we need to preserve previous
4605 content if any. */
4606 if (implicit)
4608 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4609 || TREE_CODE (constructor_type) == UNION_TYPE)
4610 && constructor_fields)
4611 value = find_init_member (constructor_fields);
4612 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4613 value = find_init_member (constructor_index);
4616 p = xmalloc (sizeof (struct constructor_stack));
4617 p->type = constructor_type;
4618 p->fields = constructor_fields;
4619 p->index = constructor_index;
4620 p->max_index = constructor_max_index;
4621 p->unfilled_index = constructor_unfilled_index;
4622 p->unfilled_fields = constructor_unfilled_fields;
4623 p->bit_index = constructor_bit_index;
4624 p->elements = constructor_elements;
4625 p->constant = constructor_constant;
4626 p->simple = constructor_simple;
4627 p->erroneous = constructor_erroneous;
4628 p->pending_elts = constructor_pending_elts;
4629 p->depth = constructor_depth;
4630 p->replacement_value = 0;
4631 p->implicit = implicit;
4632 p->outer = 0;
4633 p->incremental = constructor_incremental;
4634 p->designated = constructor_designated;
4635 p->next = constructor_stack;
4636 p->range_stack = 0;
4637 constructor_stack = p;
4639 constructor_constant = 1;
4640 constructor_simple = 1;
4641 constructor_depth = SPELLING_DEPTH ();
4642 constructor_elements = 0;
4643 constructor_incremental = 1;
4644 constructor_designated = 0;
4645 constructor_pending_elts = 0;
4646 if (!implicit)
4648 p->range_stack = constructor_range_stack;
4649 constructor_range_stack = 0;
4650 designator_depth = 0;
4651 designator_errorneous = 0;
4654 /* Don't die if an entire brace-pair level is superfluous
4655 in the containing level. */
4656 if (constructor_type == 0)
4658 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4659 || TREE_CODE (constructor_type) == UNION_TYPE)
4661 /* Don't die if there are extra init elts at the end. */
4662 if (constructor_fields == 0)
4663 constructor_type = 0;
4664 else
4666 constructor_type = TREE_TYPE (constructor_fields);
4667 push_member_name (constructor_fields);
4668 constructor_depth++;
4671 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4673 constructor_type = TREE_TYPE (constructor_type);
4674 push_array_bounds (tree_low_cst (constructor_index, 0));
4675 constructor_depth++;
4678 if (constructor_type == 0)
4680 error_init ("extra brace group at end of initializer");
4681 constructor_fields = 0;
4682 constructor_unfilled_fields = 0;
4683 return;
4686 if (value && TREE_CODE (value) == CONSTRUCTOR)
4688 constructor_constant = TREE_CONSTANT (value);
4689 constructor_simple = TREE_STATIC (value);
4690 constructor_elements = CONSTRUCTOR_ELTS (value);
4691 if (constructor_elements
4692 && (TREE_CODE (constructor_type) == RECORD_TYPE
4693 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4694 set_nonincremental_init ();
4697 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4699 missing_braces_mentioned = 1;
4700 warning_init ("missing braces around initializer");
4703 if (TREE_CODE (constructor_type) == RECORD_TYPE
4704 || TREE_CODE (constructor_type) == UNION_TYPE)
4706 constructor_fields = TYPE_FIELDS (constructor_type);
4707 /* Skip any nameless bit fields at the beginning. */
4708 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4709 && DECL_NAME (constructor_fields) == 0)
4710 constructor_fields = TREE_CHAIN (constructor_fields);
4712 constructor_unfilled_fields = constructor_fields;
4713 constructor_bit_index = bitsize_zero_node;
4715 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4717 /* Vectors are like simple fixed-size arrays. */
4718 constructor_max_index =
4719 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4720 constructor_index = convert (bitsizetype, integer_zero_node);
4721 constructor_unfilled_index = constructor_index;
4723 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4725 if (TYPE_DOMAIN (constructor_type))
4727 constructor_max_index
4728 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4730 /* Detect non-empty initializations of zero-length arrays. */
4731 if (constructor_max_index == NULL_TREE
4732 && TYPE_SIZE (constructor_type))
4733 constructor_max_index = build_int_2 (-1, -1);
4735 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4736 to initialize VLAs will cause a proper error; avoid tree
4737 checking errors as well by setting a safe value. */
4738 if (constructor_max_index
4739 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4740 constructor_max_index = build_int_2 (-1, -1);
4742 constructor_index
4743 = convert (bitsizetype,
4744 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4746 else
4747 constructor_index = bitsize_zero_node;
4749 constructor_unfilled_index = constructor_index;
4750 if (value && TREE_CODE (value) == STRING_CST)
4752 /* We need to split the char/wchar array into individual
4753 characters, so that we don't have to special case it
4754 everywhere. */
4755 set_nonincremental_init_from_string (value);
4758 else
4760 warning_init ("braces around scalar initializer");
4761 constructor_fields = constructor_type;
4762 constructor_unfilled_fields = constructor_type;
4766 /* At the end of an implicit or explicit brace level,
4767 finish up that level of constructor.
4768 If we were outputting the elements as they are read, return 0
4769 from inner levels (process_init_element ignores that),
4770 but return error_mark_node from the outermost level
4771 (that's what we want to put in DECL_INITIAL).
4772 Otherwise, return a CONSTRUCTOR expression. */
4774 tree
4775 pop_init_level (int implicit)
4777 struct constructor_stack *p;
4778 tree constructor = 0;
4780 if (implicit == 0)
4782 /* When we come to an explicit close brace,
4783 pop any inner levels that didn't have explicit braces. */
4784 while (constructor_stack->implicit)
4785 process_init_element (pop_init_level (1));
4787 if (constructor_range_stack)
4788 abort ();
4791 p = constructor_stack;
4793 /* Error for initializing a flexible array member, or a zero-length
4794 array member in an inappropriate context. */
4795 if (constructor_type && constructor_fields
4796 && TREE_CODE (constructor_type) == ARRAY_TYPE
4797 && TYPE_DOMAIN (constructor_type)
4798 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4800 /* Silently discard empty initializations. The parser will
4801 already have pedwarned for empty brackets. */
4802 if (integer_zerop (constructor_unfilled_index))
4803 constructor_type = NULL_TREE;
4804 else if (! TYPE_SIZE (constructor_type))
4806 if (constructor_depth > 2)
4807 error_init ("initialization of flexible array member in a nested context");
4808 else if (pedantic)
4809 pedwarn_init ("initialization of a flexible array member");
4811 /* We have already issued an error message for the existence
4812 of a flexible array member not at the end of the structure.
4813 Discard the initializer so that we do not abort later. */
4814 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4815 constructor_type = NULL_TREE;
4817 else
4818 /* Zero-length arrays are no longer special, so we should no longer
4819 get here. */
4820 abort ();
4823 /* Warn when some struct elements are implicitly initialized to zero. */
4824 if (extra_warnings
4825 && constructor_type
4826 && TREE_CODE (constructor_type) == RECORD_TYPE
4827 && constructor_unfilled_fields)
4829 /* Do not warn for flexible array members or zero-length arrays. */
4830 while (constructor_unfilled_fields
4831 && (! DECL_SIZE (constructor_unfilled_fields)
4832 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4833 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4835 /* Do not warn if this level of the initializer uses member
4836 designators; it is likely to be deliberate. */
4837 if (constructor_unfilled_fields && !constructor_designated)
4839 push_member_name (constructor_unfilled_fields);
4840 warning_init ("missing initializer");
4841 RESTORE_SPELLING_DEPTH (constructor_depth);
4845 /* Now output all pending elements. */
4846 constructor_incremental = 1;
4847 output_pending_init_elements (1);
4849 /* Pad out the end of the structure. */
4850 if (p->replacement_value)
4851 /* If this closes a superfluous brace pair,
4852 just pass out the element between them. */
4853 constructor = p->replacement_value;
4854 else if (constructor_type == 0)
4856 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4857 && TREE_CODE (constructor_type) != UNION_TYPE
4858 && TREE_CODE (constructor_type) != ARRAY_TYPE
4859 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4861 /* A nonincremental scalar initializer--just return
4862 the element, after verifying there is just one. */
4863 if (constructor_elements == 0)
4865 if (!constructor_erroneous)
4866 error_init ("empty scalar initializer");
4867 constructor = error_mark_node;
4869 else if (TREE_CHAIN (constructor_elements) != 0)
4871 error_init ("extra elements in scalar initializer");
4872 constructor = TREE_VALUE (constructor_elements);
4874 else
4875 constructor = TREE_VALUE (constructor_elements);
4877 else
4879 if (constructor_erroneous)
4880 constructor = error_mark_node;
4881 else
4883 constructor = build_constructor (constructor_type,
4884 nreverse (constructor_elements));
4885 if (constructor_constant)
4886 TREE_CONSTANT (constructor) = 1;
4887 if (constructor_constant && constructor_simple)
4888 TREE_STATIC (constructor) = 1;
4892 constructor_type = p->type;
4893 constructor_fields = p->fields;
4894 constructor_index = p->index;
4895 constructor_max_index = p->max_index;
4896 constructor_unfilled_index = p->unfilled_index;
4897 constructor_unfilled_fields = p->unfilled_fields;
4898 constructor_bit_index = p->bit_index;
4899 constructor_elements = p->elements;
4900 constructor_constant = p->constant;
4901 constructor_simple = p->simple;
4902 constructor_erroneous = p->erroneous;
4903 constructor_incremental = p->incremental;
4904 constructor_designated = p->designated;
4905 constructor_pending_elts = p->pending_elts;
4906 constructor_depth = p->depth;
4907 if (!p->implicit)
4908 constructor_range_stack = p->range_stack;
4909 RESTORE_SPELLING_DEPTH (constructor_depth);
4911 constructor_stack = p->next;
4912 free (p);
4914 if (constructor == 0)
4916 if (constructor_stack == 0)
4917 return error_mark_node;
4918 return NULL_TREE;
4920 return constructor;
4923 /* Common handling for both array range and field name designators.
4924 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4926 static int
4927 set_designator (int array)
4929 tree subtype;
4930 enum tree_code subcode;
4932 /* Don't die if an entire brace-pair level is superfluous
4933 in the containing level. */
4934 if (constructor_type == 0)
4935 return 1;
4937 /* If there were errors in this designator list already, bail out silently. */
4938 if (designator_errorneous)
4939 return 1;
4941 if (!designator_depth)
4943 if (constructor_range_stack)
4944 abort ();
4946 /* Designator list starts at the level of closest explicit
4947 braces. */
4948 while (constructor_stack->implicit)
4949 process_init_element (pop_init_level (1));
4950 constructor_designated = 1;
4951 return 0;
4954 if (constructor_no_implicit)
4956 error_init ("initialization designators may not nest");
4957 return 1;
4960 if (TREE_CODE (constructor_type) == RECORD_TYPE
4961 || TREE_CODE (constructor_type) == UNION_TYPE)
4963 subtype = TREE_TYPE (constructor_fields);
4964 if (subtype != error_mark_node)
4965 subtype = TYPE_MAIN_VARIANT (subtype);
4967 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4969 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4971 else
4972 abort ();
4974 subcode = TREE_CODE (subtype);
4975 if (array && subcode != ARRAY_TYPE)
4977 error_init ("array index in non-array initializer");
4978 return 1;
4980 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4982 error_init ("field name not in record or union initializer");
4983 return 1;
4986 constructor_designated = 1;
4987 push_init_level (2);
4988 return 0;
4991 /* If there are range designators in designator list, push a new designator
4992 to constructor_range_stack. RANGE_END is end of such stack range or
4993 NULL_TREE if there is no range designator at this level. */
4995 static void
4996 push_range_stack (tree range_end)
4998 struct constructor_range_stack *p;
5000 p = ggc_alloc (sizeof (struct constructor_range_stack));
5001 p->prev = constructor_range_stack;
5002 p->next = 0;
5003 p->fields = constructor_fields;
5004 p->range_start = constructor_index;
5005 p->index = constructor_index;
5006 p->stack = constructor_stack;
5007 p->range_end = range_end;
5008 if (constructor_range_stack)
5009 constructor_range_stack->next = p;
5010 constructor_range_stack = p;
5013 /* Within an array initializer, specify the next index to be initialized.
5014 FIRST is that index. If LAST is nonzero, then initialize a range
5015 of indices, running from FIRST through LAST. */
5017 void
5018 set_init_index (tree first, tree last)
5020 if (set_designator (1))
5021 return;
5023 designator_errorneous = 1;
5025 while ((TREE_CODE (first) == NOP_EXPR
5026 || TREE_CODE (first) == CONVERT_EXPR
5027 || TREE_CODE (first) == NON_LVALUE_EXPR)
5028 && (TYPE_MODE (TREE_TYPE (first))
5029 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5030 first = TREE_OPERAND (first, 0);
5032 if (last)
5033 while ((TREE_CODE (last) == NOP_EXPR
5034 || TREE_CODE (last) == CONVERT_EXPR
5035 || TREE_CODE (last) == NON_LVALUE_EXPR)
5036 && (TYPE_MODE (TREE_TYPE (last))
5037 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5038 last = TREE_OPERAND (last, 0);
5040 if (TREE_CODE (first) != INTEGER_CST)
5041 error_init ("nonconstant array index in initializer");
5042 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5043 error_init ("nonconstant array index in initializer");
5044 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5045 error_init ("array index in non-array initializer");
5046 else if (tree_int_cst_sgn (first) == -1)
5047 error_init ("array index in initializer exceeds array bounds");
5048 else if (constructor_max_index
5049 && tree_int_cst_lt (constructor_max_index, first))
5050 error_init ("array index in initializer exceeds array bounds");
5051 else
5053 constructor_index = convert (bitsizetype, first);
5055 if (last)
5057 if (tree_int_cst_equal (first, last))
5058 last = 0;
5059 else if (tree_int_cst_lt (last, first))
5061 error_init ("empty index range in initializer");
5062 last = 0;
5064 else
5066 last = convert (bitsizetype, last);
5067 if (constructor_max_index != 0
5068 && tree_int_cst_lt (constructor_max_index, last))
5070 error_init ("array index range in initializer exceeds array bounds");
5071 last = 0;
5076 designator_depth++;
5077 designator_errorneous = 0;
5078 if (constructor_range_stack || last)
5079 push_range_stack (last);
5083 /* Within a struct initializer, specify the next field to be initialized. */
5085 void
5086 set_init_label (tree fieldname)
5088 tree tail;
5090 if (set_designator (0))
5091 return;
5093 designator_errorneous = 1;
5095 if (TREE_CODE (constructor_type) != RECORD_TYPE
5096 && TREE_CODE (constructor_type) != UNION_TYPE)
5098 error_init ("field name not in record or union initializer");
5099 return;
5102 for (tail = TYPE_FIELDS (constructor_type); tail;
5103 tail = TREE_CHAIN (tail))
5105 if (DECL_NAME (tail) == fieldname)
5106 break;
5109 if (tail == 0)
5110 error ("unknown field `%s' specified in initializer",
5111 IDENTIFIER_POINTER (fieldname));
5112 else
5114 constructor_fields = tail;
5115 designator_depth++;
5116 designator_errorneous = 0;
5117 if (constructor_range_stack)
5118 push_range_stack (NULL_TREE);
5122 /* Add a new initializer to the tree of pending initializers. PURPOSE
5123 identifies the initializer, either array index or field in a structure.
5124 VALUE is the value of that index or field. */
5126 static void
5127 add_pending_init (tree purpose, tree value)
5129 struct init_node *p, **q, *r;
5131 q = &constructor_pending_elts;
5132 p = 0;
5134 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5136 while (*q != 0)
5138 p = *q;
5139 if (tree_int_cst_lt (purpose, p->purpose))
5140 q = &p->left;
5141 else if (tree_int_cst_lt (p->purpose, purpose))
5142 q = &p->right;
5143 else
5145 if (TREE_SIDE_EFFECTS (p->value))
5146 warning_init ("initialized field with side-effects overwritten");
5147 p->value = value;
5148 return;
5152 else
5154 tree bitpos;
5156 bitpos = bit_position (purpose);
5157 while (*q != NULL)
5159 p = *q;
5160 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5161 q = &p->left;
5162 else if (p->purpose != purpose)
5163 q = &p->right;
5164 else
5166 if (TREE_SIDE_EFFECTS (p->value))
5167 warning_init ("initialized field with side-effects overwritten");
5168 p->value = value;
5169 return;
5174 r = ggc_alloc (sizeof (struct init_node));
5175 r->purpose = purpose;
5176 r->value = value;
5178 *q = r;
5179 r->parent = p;
5180 r->left = 0;
5181 r->right = 0;
5182 r->balance = 0;
5184 while (p)
5186 struct init_node *s;
5188 if (r == p->left)
5190 if (p->balance == 0)
5191 p->balance = -1;
5192 else if (p->balance < 0)
5194 if (r->balance < 0)
5196 /* L rotation. */
5197 p->left = r->right;
5198 if (p->left)
5199 p->left->parent = p;
5200 r->right = p;
5202 p->balance = 0;
5203 r->balance = 0;
5205 s = p->parent;
5206 p->parent = r;
5207 r->parent = s;
5208 if (s)
5210 if (s->left == p)
5211 s->left = r;
5212 else
5213 s->right = r;
5215 else
5216 constructor_pending_elts = r;
5218 else
5220 /* LR rotation. */
5221 struct init_node *t = r->right;
5223 r->right = t->left;
5224 if (r->right)
5225 r->right->parent = r;
5226 t->left = r;
5228 p->left = t->right;
5229 if (p->left)
5230 p->left->parent = p;
5231 t->right = p;
5233 p->balance = t->balance < 0;
5234 r->balance = -(t->balance > 0);
5235 t->balance = 0;
5237 s = p->parent;
5238 p->parent = t;
5239 r->parent = t;
5240 t->parent = s;
5241 if (s)
5243 if (s->left == p)
5244 s->left = t;
5245 else
5246 s->right = t;
5248 else
5249 constructor_pending_elts = t;
5251 break;
5253 else
5255 /* p->balance == +1; growth of left side balances the node. */
5256 p->balance = 0;
5257 break;
5260 else /* r == p->right */
5262 if (p->balance == 0)
5263 /* Growth propagation from right side. */
5264 p->balance++;
5265 else if (p->balance > 0)
5267 if (r->balance > 0)
5269 /* R rotation. */
5270 p->right = r->left;
5271 if (p->right)
5272 p->right->parent = p;
5273 r->left = p;
5275 p->balance = 0;
5276 r->balance = 0;
5278 s = p->parent;
5279 p->parent = r;
5280 r->parent = s;
5281 if (s)
5283 if (s->left == p)
5284 s->left = r;
5285 else
5286 s->right = r;
5288 else
5289 constructor_pending_elts = r;
5291 else /* r->balance == -1 */
5293 /* RL rotation */
5294 struct init_node *t = r->left;
5296 r->left = t->right;
5297 if (r->left)
5298 r->left->parent = r;
5299 t->right = r;
5301 p->right = t->left;
5302 if (p->right)
5303 p->right->parent = p;
5304 t->left = p;
5306 r->balance = (t->balance < 0);
5307 p->balance = -(t->balance > 0);
5308 t->balance = 0;
5310 s = p->parent;
5311 p->parent = t;
5312 r->parent = t;
5313 t->parent = s;
5314 if (s)
5316 if (s->left == p)
5317 s->left = t;
5318 else
5319 s->right = t;
5321 else
5322 constructor_pending_elts = t;
5324 break;
5326 else
5328 /* p->balance == -1; growth of right side balances the node. */
5329 p->balance = 0;
5330 break;
5334 r = p;
5335 p = p->parent;
5339 /* Build AVL tree from a sorted chain. */
5341 static void
5342 set_nonincremental_init (void)
5344 tree chain;
5346 if (TREE_CODE (constructor_type) != RECORD_TYPE
5347 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5348 return;
5350 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5351 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5352 constructor_elements = 0;
5353 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5355 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5356 /* Skip any nameless bit fields at the beginning. */
5357 while (constructor_unfilled_fields != 0
5358 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5359 && DECL_NAME (constructor_unfilled_fields) == 0)
5360 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5363 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5365 if (TYPE_DOMAIN (constructor_type))
5366 constructor_unfilled_index
5367 = convert (bitsizetype,
5368 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5369 else
5370 constructor_unfilled_index = bitsize_zero_node;
5372 constructor_incremental = 0;
5375 /* Build AVL tree from a string constant. */
5377 static void
5378 set_nonincremental_init_from_string (tree str)
5380 tree value, purpose, type;
5381 HOST_WIDE_INT val[2];
5382 const char *p, *end;
5383 int byte, wchar_bytes, charwidth, bitpos;
5385 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5386 abort ();
5388 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5389 == TYPE_PRECISION (char_type_node))
5390 wchar_bytes = 1;
5391 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5392 == TYPE_PRECISION (wchar_type_node))
5393 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5394 else
5395 abort ();
5397 charwidth = TYPE_PRECISION (char_type_node);
5398 type = TREE_TYPE (constructor_type);
5399 p = TREE_STRING_POINTER (str);
5400 end = p + TREE_STRING_LENGTH (str);
5402 for (purpose = bitsize_zero_node;
5403 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5404 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5406 if (wchar_bytes == 1)
5408 val[1] = (unsigned char) *p++;
5409 val[0] = 0;
5411 else
5413 val[0] = 0;
5414 val[1] = 0;
5415 for (byte = 0; byte < wchar_bytes; byte++)
5417 if (BYTES_BIG_ENDIAN)
5418 bitpos = (wchar_bytes - byte - 1) * charwidth;
5419 else
5420 bitpos = byte * charwidth;
5421 val[bitpos < HOST_BITS_PER_WIDE_INT]
5422 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5423 << (bitpos % HOST_BITS_PER_WIDE_INT);
5427 if (!TREE_UNSIGNED (type))
5429 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5430 if (bitpos < HOST_BITS_PER_WIDE_INT)
5432 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5434 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5435 val[0] = -1;
5438 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5440 if (val[1] < 0)
5441 val[0] = -1;
5443 else if (val[0] & (((HOST_WIDE_INT) 1)
5444 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5445 val[0] |= ((HOST_WIDE_INT) -1)
5446 << (bitpos - HOST_BITS_PER_WIDE_INT);
5449 value = build_int_2 (val[1], val[0]);
5450 TREE_TYPE (value) = type;
5451 add_pending_init (purpose, value);
5454 constructor_incremental = 0;
5457 /* Return value of FIELD in pending initializer or zero if the field was
5458 not initialized yet. */
5460 static tree
5461 find_init_member (tree field)
5463 struct init_node *p;
5465 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5467 if (constructor_incremental
5468 && tree_int_cst_lt (field, constructor_unfilled_index))
5469 set_nonincremental_init ();
5471 p = constructor_pending_elts;
5472 while (p)
5474 if (tree_int_cst_lt (field, p->purpose))
5475 p = p->left;
5476 else if (tree_int_cst_lt (p->purpose, field))
5477 p = p->right;
5478 else
5479 return p->value;
5482 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5484 tree bitpos = bit_position (field);
5486 if (constructor_incremental
5487 && (!constructor_unfilled_fields
5488 || tree_int_cst_lt (bitpos,
5489 bit_position (constructor_unfilled_fields))))
5490 set_nonincremental_init ();
5492 p = constructor_pending_elts;
5493 while (p)
5495 if (field == p->purpose)
5496 return p->value;
5497 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5498 p = p->left;
5499 else
5500 p = p->right;
5503 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5505 if (constructor_elements
5506 && TREE_PURPOSE (constructor_elements) == field)
5507 return TREE_VALUE (constructor_elements);
5509 return 0;
5512 /* "Output" the next constructor element.
5513 At top level, really output it to assembler code now.
5514 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5515 TYPE is the data type that the containing data type wants here.
5516 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5518 PENDING if non-nil means output pending elements that belong
5519 right after this element. (PENDING is normally 1;
5520 it is 0 while outputting pending elements, to avoid recursion.) */
5522 static void
5523 output_init_element (tree value, tree type, tree field, int pending)
5525 if (type == error_mark_node)
5527 constructor_erroneous = 1;
5528 return;
5530 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5531 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5532 && !(TREE_CODE (value) == STRING_CST
5533 && TREE_CODE (type) == ARRAY_TYPE
5534 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5535 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5536 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5537 value = default_conversion (value);
5539 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5540 && require_constant_value && !flag_isoc99 && pending)
5542 /* As an extension, allow initializing objects with static storage
5543 duration with compound literals (which are then treated just as
5544 the brace enclosed list they contain). */
5545 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5546 value = DECL_INITIAL (decl);
5549 if (value == error_mark_node)
5550 constructor_erroneous = 1;
5551 else if (!TREE_CONSTANT (value))
5552 constructor_constant = 0;
5553 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5554 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5555 || TREE_CODE (constructor_type) == UNION_TYPE)
5556 && DECL_C_BIT_FIELD (field)
5557 && TREE_CODE (value) != INTEGER_CST))
5558 constructor_simple = 0;
5560 if (require_constant_value && ! TREE_CONSTANT (value))
5562 error_init ("initializer element is not constant");
5563 value = error_mark_node;
5565 else if (require_constant_elements
5566 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5567 pedwarn ("initializer element is not computable at load time");
5569 /* If this field is empty (and not at the end of structure),
5570 don't do anything other than checking the initializer. */
5571 if (field
5572 && (TREE_TYPE (field) == error_mark_node
5573 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5574 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5575 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5576 || TREE_CHAIN (field)))))
5577 return;
5579 value = digest_init (type, value, require_constant_value);
5580 if (value == error_mark_node)
5582 constructor_erroneous = 1;
5583 return;
5586 /* If this element doesn't come next in sequence,
5587 put it on constructor_pending_elts. */
5588 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5589 && (!constructor_incremental
5590 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5592 if (constructor_incremental
5593 && tree_int_cst_lt (field, constructor_unfilled_index))
5594 set_nonincremental_init ();
5596 add_pending_init (field, value);
5597 return;
5599 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5600 && (!constructor_incremental
5601 || field != constructor_unfilled_fields))
5603 /* We do this for records but not for unions. In a union,
5604 no matter which field is specified, it can be initialized
5605 right away since it starts at the beginning of the union. */
5606 if (constructor_incremental)
5608 if (!constructor_unfilled_fields)
5609 set_nonincremental_init ();
5610 else
5612 tree bitpos, unfillpos;
5614 bitpos = bit_position (field);
5615 unfillpos = bit_position (constructor_unfilled_fields);
5617 if (tree_int_cst_lt (bitpos, unfillpos))
5618 set_nonincremental_init ();
5622 add_pending_init (field, value);
5623 return;
5625 else if (TREE_CODE (constructor_type) == UNION_TYPE
5626 && constructor_elements)
5628 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5629 warning_init ("initialized field with side-effects overwritten");
5631 /* We can have just one union field set. */
5632 constructor_elements = 0;
5635 /* Otherwise, output this element either to
5636 constructor_elements or to the assembler file. */
5638 if (field && TREE_CODE (field) == INTEGER_CST)
5639 field = copy_node (field);
5640 constructor_elements
5641 = tree_cons (field, value, constructor_elements);
5643 /* Advance the variable that indicates sequential elements output. */
5644 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5645 constructor_unfilled_index
5646 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5647 bitsize_one_node);
5648 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5650 constructor_unfilled_fields
5651 = TREE_CHAIN (constructor_unfilled_fields);
5653 /* Skip any nameless bit fields. */
5654 while (constructor_unfilled_fields != 0
5655 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5656 && DECL_NAME (constructor_unfilled_fields) == 0)
5657 constructor_unfilled_fields =
5658 TREE_CHAIN (constructor_unfilled_fields);
5660 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5661 constructor_unfilled_fields = 0;
5663 /* Now output any pending elements which have become next. */
5664 if (pending)
5665 output_pending_init_elements (0);
5668 /* Output any pending elements which have become next.
5669 As we output elements, constructor_unfilled_{fields,index}
5670 advances, which may cause other elements to become next;
5671 if so, they too are output.
5673 If ALL is 0, we return when there are
5674 no more pending elements to output now.
5676 If ALL is 1, we output space as necessary so that
5677 we can output all the pending elements. */
5679 static void
5680 output_pending_init_elements (int all)
5682 struct init_node *elt = constructor_pending_elts;
5683 tree next;
5685 retry:
5687 /* Look through the whole pending tree.
5688 If we find an element that should be output now,
5689 output it. Otherwise, set NEXT to the element
5690 that comes first among those still pending. */
5692 next = 0;
5693 while (elt)
5695 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5697 if (tree_int_cst_equal (elt->purpose,
5698 constructor_unfilled_index))
5699 output_init_element (elt->value,
5700 TREE_TYPE (constructor_type),
5701 constructor_unfilled_index, 0);
5702 else if (tree_int_cst_lt (constructor_unfilled_index,
5703 elt->purpose))
5705 /* Advance to the next smaller node. */
5706 if (elt->left)
5707 elt = elt->left;
5708 else
5710 /* We have reached the smallest node bigger than the
5711 current unfilled index. Fill the space first. */
5712 next = elt->purpose;
5713 break;
5716 else
5718 /* Advance to the next bigger node. */
5719 if (elt->right)
5720 elt = elt->right;
5721 else
5723 /* We have reached the biggest node in a subtree. Find
5724 the parent of it, which is the next bigger node. */
5725 while (elt->parent && elt->parent->right == elt)
5726 elt = elt->parent;
5727 elt = elt->parent;
5728 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5729 elt->purpose))
5731 next = elt->purpose;
5732 break;
5737 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5738 || TREE_CODE (constructor_type) == UNION_TYPE)
5740 tree ctor_unfilled_bitpos, elt_bitpos;
5742 /* If the current record is complete we are done. */
5743 if (constructor_unfilled_fields == 0)
5744 break;
5746 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5747 elt_bitpos = bit_position (elt->purpose);
5748 /* We can't compare fields here because there might be empty
5749 fields in between. */
5750 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5752 constructor_unfilled_fields = elt->purpose;
5753 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5754 elt->purpose, 0);
5756 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5758 /* Advance to the next smaller node. */
5759 if (elt->left)
5760 elt = elt->left;
5761 else
5763 /* We have reached the smallest node bigger than the
5764 current unfilled field. Fill the space first. */
5765 next = elt->purpose;
5766 break;
5769 else
5771 /* Advance to the next bigger node. */
5772 if (elt->right)
5773 elt = elt->right;
5774 else
5776 /* We have reached the biggest node in a subtree. Find
5777 the parent of it, which is the next bigger node. */
5778 while (elt->parent && elt->parent->right == elt)
5779 elt = elt->parent;
5780 elt = elt->parent;
5781 if (elt
5782 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5783 bit_position (elt->purpose))))
5785 next = elt->purpose;
5786 break;
5793 /* Ordinarily return, but not if we want to output all
5794 and there are elements left. */
5795 if (! (all && next != 0))
5796 return;
5798 /* If it's not incremental, just skip over the gap, so that after
5799 jumping to retry we will output the next successive element. */
5800 if (TREE_CODE (constructor_type) == RECORD_TYPE
5801 || TREE_CODE (constructor_type) == UNION_TYPE)
5802 constructor_unfilled_fields = next;
5803 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5804 constructor_unfilled_index = next;
5806 /* ELT now points to the node in the pending tree with the next
5807 initializer to output. */
5808 goto retry;
5811 /* Add one non-braced element to the current constructor level.
5812 This adjusts the current position within the constructor's type.
5813 This may also start or terminate implicit levels
5814 to handle a partly-braced initializer.
5816 Once this has found the correct level for the new element,
5817 it calls output_init_element. */
5819 void
5820 process_init_element (tree value)
5822 tree orig_value = value;
5823 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5825 designator_depth = 0;
5826 designator_errorneous = 0;
5828 /* Handle superfluous braces around string cst as in
5829 char x[] = {"foo"}; */
5830 if (string_flag
5831 && constructor_type
5832 && TREE_CODE (constructor_type) == ARRAY_TYPE
5833 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5834 && integer_zerop (constructor_unfilled_index))
5836 if (constructor_stack->replacement_value)
5837 error_init ("excess elements in char array initializer");
5838 constructor_stack->replacement_value = value;
5839 return;
5842 if (constructor_stack->replacement_value != 0)
5844 error_init ("excess elements in struct initializer");
5845 return;
5848 /* Ignore elements of a brace group if it is entirely superfluous
5849 and has already been diagnosed. */
5850 if (constructor_type == 0)
5851 return;
5853 /* If we've exhausted any levels that didn't have braces,
5854 pop them now. */
5855 while (constructor_stack->implicit)
5857 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5858 || TREE_CODE (constructor_type) == UNION_TYPE)
5859 && constructor_fields == 0)
5860 process_init_element (pop_init_level (1));
5861 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5862 && (constructor_max_index == 0
5863 || tree_int_cst_lt (constructor_max_index,
5864 constructor_index)))
5865 process_init_element (pop_init_level (1));
5866 else
5867 break;
5870 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5871 if (constructor_range_stack)
5873 /* If value is a compound literal and we'll be just using its
5874 content, don't put it into a SAVE_EXPR. */
5875 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5876 || !require_constant_value
5877 || flag_isoc99)
5878 value = save_expr (value);
5881 while (1)
5883 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5885 tree fieldtype;
5886 enum tree_code fieldcode;
5888 if (constructor_fields == 0)
5890 pedwarn_init ("excess elements in struct initializer");
5891 break;
5894 fieldtype = TREE_TYPE (constructor_fields);
5895 if (fieldtype != error_mark_node)
5896 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5897 fieldcode = TREE_CODE (fieldtype);
5899 /* Error for non-static initialization of a flexible array member. */
5900 if (fieldcode == ARRAY_TYPE
5901 && !require_constant_value
5902 && TYPE_SIZE (fieldtype) == NULL_TREE
5903 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5905 error_init ("non-static initialization of a flexible array member");
5906 break;
5909 /* Accept a string constant to initialize a subarray. */
5910 if (value != 0
5911 && fieldcode == ARRAY_TYPE
5912 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5913 && string_flag)
5914 value = orig_value;
5915 /* Otherwise, if we have come to a subaggregate,
5916 and we don't have an element of its type, push into it. */
5917 else if (value != 0 && !constructor_no_implicit
5918 && value != error_mark_node
5919 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5920 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5921 || fieldcode == UNION_TYPE))
5923 push_init_level (1);
5924 continue;
5927 if (value)
5929 push_member_name (constructor_fields);
5930 output_init_element (value, fieldtype, constructor_fields, 1);
5931 RESTORE_SPELLING_DEPTH (constructor_depth);
5933 else
5934 /* Do the bookkeeping for an element that was
5935 directly output as a constructor. */
5937 /* For a record, keep track of end position of last field. */
5938 if (DECL_SIZE (constructor_fields))
5939 constructor_bit_index
5940 = size_binop (PLUS_EXPR,
5941 bit_position (constructor_fields),
5942 DECL_SIZE (constructor_fields));
5944 /* If the current field was the first one not yet written out,
5945 it isn't now, so update. */
5946 if (constructor_unfilled_fields == constructor_fields)
5948 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5949 /* Skip any nameless bit fields. */
5950 while (constructor_unfilled_fields != 0
5951 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5952 && DECL_NAME (constructor_unfilled_fields) == 0)
5953 constructor_unfilled_fields =
5954 TREE_CHAIN (constructor_unfilled_fields);
5958 constructor_fields = TREE_CHAIN (constructor_fields);
5959 /* Skip any nameless bit fields at the beginning. */
5960 while (constructor_fields != 0
5961 && DECL_C_BIT_FIELD (constructor_fields)
5962 && DECL_NAME (constructor_fields) == 0)
5963 constructor_fields = TREE_CHAIN (constructor_fields);
5965 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5967 tree fieldtype;
5968 enum tree_code fieldcode;
5970 if (constructor_fields == 0)
5972 pedwarn_init ("excess elements in union initializer");
5973 break;
5976 fieldtype = TREE_TYPE (constructor_fields);
5977 if (fieldtype != error_mark_node)
5978 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5979 fieldcode = TREE_CODE (fieldtype);
5981 /* Warn that traditional C rejects initialization of unions.
5982 We skip the warning if the value is zero. This is done
5983 under the assumption that the zero initializer in user
5984 code appears conditioned on e.g. __STDC__ to avoid
5985 "missing initializer" warnings and relies on default
5986 initialization to zero in the traditional C case.
5987 We also skip the warning if the initializer is designated,
5988 again on the assumption that this must be conditional on
5989 __STDC__ anyway (and we've already complained about the
5990 member-designator already). */
5991 if (warn_traditional && !in_system_header && !constructor_designated
5992 && !(value && (integer_zerop (value) || real_zerop (value))))
5993 warning ("traditional C rejects initialization of unions");
5995 /* Accept a string constant to initialize a subarray. */
5996 if (value != 0
5997 && fieldcode == ARRAY_TYPE
5998 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5999 && string_flag)
6000 value = orig_value;
6001 /* Otherwise, if we have come to a subaggregate,
6002 and we don't have an element of its type, push into it. */
6003 else if (value != 0 && !constructor_no_implicit
6004 && value != error_mark_node
6005 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6006 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6007 || fieldcode == UNION_TYPE))
6009 push_init_level (1);
6010 continue;
6013 if (value)
6015 push_member_name (constructor_fields);
6016 output_init_element (value, fieldtype, constructor_fields, 1);
6017 RESTORE_SPELLING_DEPTH (constructor_depth);
6019 else
6020 /* Do the bookkeeping for an element that was
6021 directly output as a constructor. */
6023 constructor_bit_index = DECL_SIZE (constructor_fields);
6024 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6027 constructor_fields = 0;
6029 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6031 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6032 enum tree_code eltcode = TREE_CODE (elttype);
6034 /* Accept a string constant to initialize a subarray. */
6035 if (value != 0
6036 && eltcode == ARRAY_TYPE
6037 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6038 && string_flag)
6039 value = orig_value;
6040 /* Otherwise, if we have come to a subaggregate,
6041 and we don't have an element of its type, push into it. */
6042 else if (value != 0 && !constructor_no_implicit
6043 && value != error_mark_node
6044 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6045 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6046 || eltcode == UNION_TYPE))
6048 push_init_level (1);
6049 continue;
6052 if (constructor_max_index != 0
6053 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6054 || integer_all_onesp (constructor_max_index)))
6056 pedwarn_init ("excess elements in array initializer");
6057 break;
6060 /* Now output the actual element. */
6061 if (value)
6063 push_array_bounds (tree_low_cst (constructor_index, 0));
6064 output_init_element (value, elttype, constructor_index, 1);
6065 RESTORE_SPELLING_DEPTH (constructor_depth);
6068 constructor_index
6069 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6071 if (! value)
6072 /* If we are doing the bookkeeping for an element that was
6073 directly output as a constructor, we must update
6074 constructor_unfilled_index. */
6075 constructor_unfilled_index = constructor_index;
6077 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6079 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6081 /* Do a basic check of initializer size. Note that vectors
6082 always have a fixed size derived from their type. */
6083 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6085 pedwarn_init ("excess elements in vector initializer");
6086 break;
6089 /* Now output the actual element. */
6090 if (value)
6091 output_init_element (value, elttype, constructor_index, 1);
6093 constructor_index
6094 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6096 if (! value)
6097 /* If we are doing the bookkeeping for an element that was
6098 directly output as a constructor, we must update
6099 constructor_unfilled_index. */
6100 constructor_unfilled_index = constructor_index;
6103 /* Handle the sole element allowed in a braced initializer
6104 for a scalar variable. */
6105 else if (constructor_fields == 0)
6107 pedwarn_init ("excess elements in scalar initializer");
6108 break;
6110 else
6112 if (value)
6113 output_init_element (value, constructor_type, NULL_TREE, 1);
6114 constructor_fields = 0;
6117 /* Handle range initializers either at this level or anywhere higher
6118 in the designator stack. */
6119 if (constructor_range_stack)
6121 struct constructor_range_stack *p, *range_stack;
6122 int finish = 0;
6124 range_stack = constructor_range_stack;
6125 constructor_range_stack = 0;
6126 while (constructor_stack != range_stack->stack)
6128 if (!constructor_stack->implicit)
6129 abort ();
6130 process_init_element (pop_init_level (1));
6132 for (p = range_stack;
6133 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6134 p = p->prev)
6136 if (!constructor_stack->implicit)
6137 abort ();
6138 process_init_element (pop_init_level (1));
6141 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6142 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6143 finish = 1;
6145 while (1)
6147 constructor_index = p->index;
6148 constructor_fields = p->fields;
6149 if (finish && p->range_end && p->index == p->range_start)
6151 finish = 0;
6152 p->prev = 0;
6154 p = p->next;
6155 if (!p)
6156 break;
6157 push_init_level (2);
6158 p->stack = constructor_stack;
6159 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6160 p->index = p->range_start;
6163 if (!finish)
6164 constructor_range_stack = range_stack;
6165 continue;
6168 break;
6171 constructor_range_stack = 0;
6174 /* Build a simple asm-statement, from one string literal. */
6175 tree
6176 simple_asm_stmt (tree expr)
6178 STRIP_NOPS (expr);
6180 if (TREE_CODE (expr) == ADDR_EXPR)
6181 expr = TREE_OPERAND (expr, 0);
6183 if (TREE_CODE (expr) == STRING_CST)
6185 tree stmt;
6187 /* Simple asm statements are treated as volatile. */
6188 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6189 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6190 ASM_INPUT_P (stmt) = 1;
6191 return stmt;
6194 error ("argument of `asm' is not a constant string");
6195 return NULL_TREE;
6198 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6199 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6201 tree
6202 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6203 tree clobbers)
6205 tree tail;
6207 if (TREE_CODE (string) != STRING_CST)
6209 error ("asm template is not a string constant");
6210 return NULL_TREE;
6213 if (cv_qualifier != NULL_TREE
6214 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6216 warning ("%s qualifier ignored on asm",
6217 IDENTIFIER_POINTER (cv_qualifier));
6218 cv_qualifier = NULL_TREE;
6221 /* We can remove output conversions that change the type,
6222 but not the mode. */
6223 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6225 tree output = TREE_VALUE (tail);
6227 STRIP_NOPS (output);
6228 TREE_VALUE (tail) = output;
6230 /* Allow conversions as LHS here. build_modify_expr as called below
6231 will do the right thing with them. */
6232 while (TREE_CODE (output) == NOP_EXPR
6233 || TREE_CODE (output) == CONVERT_EXPR
6234 || TREE_CODE (output) == FLOAT_EXPR
6235 || TREE_CODE (output) == FIX_TRUNC_EXPR
6236 || TREE_CODE (output) == FIX_FLOOR_EXPR
6237 || TREE_CODE (output) == FIX_ROUND_EXPR
6238 || TREE_CODE (output) == FIX_CEIL_EXPR)
6239 output = TREE_OPERAND (output, 0);
6241 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6244 /* Remove output conversions that change the type but not the mode. */
6245 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6247 tree output = TREE_VALUE (tail);
6248 STRIP_NOPS (output);
6249 TREE_VALUE (tail) = output;
6252 /* Perform default conversions on array and function inputs.
6253 Don't do this for other types as it would screw up operands
6254 expected to be in memory. */
6255 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6256 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6258 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6259 outputs, inputs, clobbers));
6262 /* Expand an ASM statement with operands, handling output operands
6263 that are not variables or INDIRECT_REFS by transforming such
6264 cases into cases that expand_asm_operands can handle.
6266 Arguments are same as for expand_asm_operands. */
6268 void
6269 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6270 tree clobbers, int vol, location_t locus)
6272 int noutputs = list_length (outputs);
6273 int i;
6274 /* o[I] is the place that output number I should be written. */
6275 tree *o = alloca (noutputs * sizeof (tree));
6276 tree tail;
6278 /* Record the contents of OUTPUTS before it is modified. */
6279 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6281 o[i] = TREE_VALUE (tail);
6282 if (o[i] == error_mark_node)
6283 return;
6286 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6287 OUTPUTS some trees for where the values were actually stored. */
6288 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6290 /* Copy all the intermediate outputs into the specified outputs. */
6291 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6293 if (o[i] != TREE_VALUE (tail))
6295 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6296 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6297 free_temp_slots ();
6299 /* Restore the original value so that it's correct the next
6300 time we expand this function. */
6301 TREE_VALUE (tail) = o[i];
6303 /* Detect modification of read-only values.
6304 (Otherwise done by build_modify_expr.) */
6305 else
6307 tree type = TREE_TYPE (o[i]);
6308 if (TREE_READONLY (o[i])
6309 || TYPE_READONLY (type)
6310 || ((TREE_CODE (type) == RECORD_TYPE
6311 || TREE_CODE (type) == UNION_TYPE)
6312 && C_TYPE_FIELDS_READONLY (type)))
6313 readonly_warning (o[i], "modification by `asm'");
6317 /* Those MODIFY_EXPRs could do autoincrements. */
6318 emit_queue ();
6321 /* Expand a C `return' statement.
6322 RETVAL is the expression for what to return,
6323 or a null pointer for `return;' with no value. */
6325 tree
6326 c_expand_return (tree retval)
6328 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6330 if (TREE_THIS_VOLATILE (current_function_decl))
6331 warning ("function declared `noreturn' has a `return' statement");
6333 if (!retval)
6335 current_function_returns_null = 1;
6336 if ((warn_return_type || flag_isoc99)
6337 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6338 pedwarn_c99 ("`return' with no value, in function returning non-void");
6340 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6342 current_function_returns_null = 1;
6343 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6344 pedwarn ("`return' with a value, in function returning void");
6346 else
6348 tree t = convert_for_assignment (valtype, retval, _("return"),
6349 NULL_TREE, NULL_TREE, 0);
6350 tree res = DECL_RESULT (current_function_decl);
6351 tree inner;
6353 current_function_returns_value = 1;
6354 if (t == error_mark_node)
6355 return NULL_TREE;
6357 inner = t = convert (TREE_TYPE (res), t);
6359 /* Strip any conversions, additions, and subtractions, and see if
6360 we are returning the address of a local variable. Warn if so. */
6361 while (1)
6363 switch (TREE_CODE (inner))
6365 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6366 case PLUS_EXPR:
6367 inner = TREE_OPERAND (inner, 0);
6368 continue;
6370 case MINUS_EXPR:
6371 /* If the second operand of the MINUS_EXPR has a pointer
6372 type (or is converted from it), this may be valid, so
6373 don't give a warning. */
6375 tree op1 = TREE_OPERAND (inner, 1);
6377 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6378 && (TREE_CODE (op1) == NOP_EXPR
6379 || TREE_CODE (op1) == NON_LVALUE_EXPR
6380 || TREE_CODE (op1) == CONVERT_EXPR))
6381 op1 = TREE_OPERAND (op1, 0);
6383 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6384 break;
6386 inner = TREE_OPERAND (inner, 0);
6387 continue;
6390 case ADDR_EXPR:
6391 inner = TREE_OPERAND (inner, 0);
6393 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6394 inner = TREE_OPERAND (inner, 0);
6396 if (TREE_CODE (inner) == VAR_DECL
6397 && ! DECL_EXTERNAL (inner)
6398 && ! TREE_STATIC (inner)
6399 && DECL_CONTEXT (inner) == current_function_decl)
6400 warning ("function returns address of local variable");
6401 break;
6403 default:
6404 break;
6407 break;
6410 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6413 return add_stmt (build_return_stmt (retval));
6416 struct c_switch {
6417 /* The SWITCH_STMT being built. */
6418 tree switch_stmt;
6419 /* A splay-tree mapping the low element of a case range to the high
6420 element, or NULL_TREE if there is no high element. Used to
6421 determine whether or not a new case label duplicates an old case
6422 label. We need a tree, rather than simply a hash table, because
6423 of the GNU case range extension. */
6424 splay_tree cases;
6425 /* The next node on the stack. */
6426 struct c_switch *next;
6429 /* A stack of the currently active switch statements. The innermost
6430 switch statement is on the top of the stack. There is no need to
6431 mark the stack for garbage collection because it is only active
6432 during the processing of the body of a function, and we never
6433 collect at that point. */
6435 static struct c_switch *switch_stack;
6437 /* Start a C switch statement, testing expression EXP. Return the new
6438 SWITCH_STMT. */
6440 tree
6441 c_start_case (tree exp)
6443 enum tree_code code;
6444 tree type, orig_type = error_mark_node;
6445 struct c_switch *cs;
6447 if (exp != error_mark_node)
6449 code = TREE_CODE (TREE_TYPE (exp));
6450 orig_type = TREE_TYPE (exp);
6452 if (! INTEGRAL_TYPE_P (orig_type)
6453 && code != ERROR_MARK)
6455 error ("switch quantity not an integer");
6456 exp = integer_zero_node;
6458 else
6460 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6462 if (warn_traditional && !in_system_header
6463 && (type == long_integer_type_node
6464 || type == long_unsigned_type_node))
6465 warning ("`long' switch expression not converted to `int' in ISO C");
6467 exp = default_conversion (exp);
6468 type = TREE_TYPE (exp);
6472 /* Add this new SWITCH_STMT to the stack. */
6473 cs = xmalloc (sizeof (*cs));
6474 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6475 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6476 cs->next = switch_stack;
6477 switch_stack = cs;
6479 return add_stmt (switch_stack->switch_stmt);
6482 /* Process a case label. */
6484 tree
6485 do_case (tree low_value, tree high_value)
6487 tree label = NULL_TREE;
6489 if (switch_stack)
6491 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6493 label = c_add_case_label (switch_stack->cases,
6494 SWITCH_COND (switch_stack->switch_stmt),
6495 low_value, high_value);
6496 if (label == error_mark_node)
6497 label = NULL_TREE;
6498 else if (switch_was_empty_p)
6500 /* Attach the first case label to the SWITCH_BODY. */
6501 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6502 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6505 else if (low_value)
6506 error ("case label not within a switch statement");
6507 else
6508 error ("`default' label not within a switch statement");
6510 return label;
6513 /* Finish the switch statement. */
6515 void
6516 c_finish_case (void)
6518 struct c_switch *cs = switch_stack;
6520 /* Rechain the next statements to the SWITCH_STMT. */
6521 last_tree = cs->switch_stmt;
6523 /* Pop the stack. */
6524 switch_stack = switch_stack->next;
6525 splay_tree_delete (cs->cases);
6526 free (cs);
6529 /* Build a binary-operation expression without default conversions.
6530 CODE is the kind of expression to build.
6531 This function differs from `build' in several ways:
6532 the data type of the result is computed and recorded in it,
6533 warnings are generated if arg data types are invalid,
6534 special handling for addition and subtraction of pointers is known,
6535 and some optimization is done (operations on narrow ints
6536 are done in the narrower type when that gives the same result).
6537 Constant folding is also done before the result is returned.
6539 Note that the operands will never have enumeral types, or function
6540 or array types, because either they will have the default conversions
6541 performed or they have both just been converted to some other type in which
6542 the arithmetic is to be done. */
6544 tree
6545 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6546 int convert_p)
6548 tree type0, type1;
6549 enum tree_code code0, code1;
6550 tree op0, op1;
6552 /* Expression code to give to the expression when it is built.
6553 Normally this is CODE, which is what the caller asked for,
6554 but in some special cases we change it. */
6555 enum tree_code resultcode = code;
6557 /* Data type in which the computation is to be performed.
6558 In the simplest cases this is the common type of the arguments. */
6559 tree result_type = NULL;
6561 /* Nonzero means operands have already been type-converted
6562 in whatever way is necessary.
6563 Zero means they need to be converted to RESULT_TYPE. */
6564 int converted = 0;
6566 /* Nonzero means create the expression with this type, rather than
6567 RESULT_TYPE. */
6568 tree build_type = 0;
6570 /* Nonzero means after finally constructing the expression
6571 convert it to this type. */
6572 tree final_type = 0;
6574 /* Nonzero if this is an operation like MIN or MAX which can
6575 safely be computed in short if both args are promoted shorts.
6576 Also implies COMMON.
6577 -1 indicates a bitwise operation; this makes a difference
6578 in the exact conditions for when it is safe to do the operation
6579 in a narrower mode. */
6580 int shorten = 0;
6582 /* Nonzero if this is a comparison operation;
6583 if both args are promoted shorts, compare the original shorts.
6584 Also implies COMMON. */
6585 int short_compare = 0;
6587 /* Nonzero if this is a right-shift operation, which can be computed on the
6588 original short and then promoted if the operand is a promoted short. */
6589 int short_shift = 0;
6591 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6592 int common = 0;
6594 if (convert_p)
6596 op0 = default_conversion (orig_op0);
6597 op1 = default_conversion (orig_op1);
6599 else
6601 op0 = orig_op0;
6602 op1 = orig_op1;
6605 type0 = TREE_TYPE (op0);
6606 type1 = TREE_TYPE (op1);
6608 /* The expression codes of the data types of the arguments tell us
6609 whether the arguments are integers, floating, pointers, etc. */
6610 code0 = TREE_CODE (type0);
6611 code1 = TREE_CODE (type1);
6613 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6614 STRIP_TYPE_NOPS (op0);
6615 STRIP_TYPE_NOPS (op1);
6617 /* If an error was already reported for one of the arguments,
6618 avoid reporting another error. */
6620 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6621 return error_mark_node;
6623 switch (code)
6625 case PLUS_EXPR:
6626 /* Handle the pointer + int case. */
6627 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6628 return pointer_int_sum (PLUS_EXPR, op0, op1);
6629 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6630 return pointer_int_sum (PLUS_EXPR, op1, op0);
6631 else
6632 common = 1;
6633 break;
6635 case MINUS_EXPR:
6636 /* Subtraction of two similar pointers.
6637 We must subtract them as integers, then divide by object size. */
6638 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6639 && comp_target_types (type0, type1, 1))
6640 return pointer_diff (op0, op1);
6641 /* Handle pointer minus int. Just like pointer plus int. */
6642 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6643 return pointer_int_sum (MINUS_EXPR, op0, op1);
6644 else
6645 common = 1;
6646 break;
6648 case MULT_EXPR:
6649 common = 1;
6650 break;
6652 case TRUNC_DIV_EXPR:
6653 case CEIL_DIV_EXPR:
6654 case FLOOR_DIV_EXPR:
6655 case ROUND_DIV_EXPR:
6656 case EXACT_DIV_EXPR:
6657 /* Floating point division by zero is a legitimate way to obtain
6658 infinities and NaNs. */
6659 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6660 warning ("division by zero");
6662 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6663 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6664 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6665 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6667 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6668 resultcode = RDIV_EXPR;
6669 else
6670 /* Although it would be tempting to shorten always here, that
6671 loses on some targets, since the modulo instruction is
6672 undefined if the quotient can't be represented in the
6673 computation mode. We shorten only if unsigned or if
6674 dividing by something we know != -1. */
6675 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6676 || (TREE_CODE (op1) == INTEGER_CST
6677 && ! integer_all_onesp (op1)));
6678 common = 1;
6680 break;
6682 case BIT_AND_EXPR:
6683 case BIT_IOR_EXPR:
6684 case BIT_XOR_EXPR:
6685 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6686 shorten = -1;
6687 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6688 common = 1;
6689 break;
6691 case TRUNC_MOD_EXPR:
6692 case FLOOR_MOD_EXPR:
6693 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6694 warning ("division by zero");
6696 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6698 /* Although it would be tempting to shorten always here, that loses
6699 on some targets, since the modulo instruction is undefined if the
6700 quotient can't be represented in the computation mode. We shorten
6701 only if unsigned or if dividing by something we know != -1. */
6702 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6703 || (TREE_CODE (op1) == INTEGER_CST
6704 && ! integer_all_onesp (op1)));
6705 common = 1;
6707 break;
6709 case TRUTH_ANDIF_EXPR:
6710 case TRUTH_ORIF_EXPR:
6711 case TRUTH_AND_EXPR:
6712 case TRUTH_OR_EXPR:
6713 case TRUTH_XOR_EXPR:
6714 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6715 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6716 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6717 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6719 /* Result of these operations is always an int,
6720 but that does not mean the operands should be
6721 converted to ints! */
6722 result_type = integer_type_node;
6723 op0 = c_common_truthvalue_conversion (op0);
6724 op1 = c_common_truthvalue_conversion (op1);
6725 converted = 1;
6727 break;
6729 /* Shift operations: result has same type as first operand;
6730 always convert second operand to int.
6731 Also set SHORT_SHIFT if shifting rightward. */
6733 case RSHIFT_EXPR:
6734 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6736 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6738 if (tree_int_cst_sgn (op1) < 0)
6739 warning ("right shift count is negative");
6740 else
6742 if (! integer_zerop (op1))
6743 short_shift = 1;
6745 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6746 warning ("right shift count >= width of type");
6750 /* Use the type of the value to be shifted. */
6751 result_type = type0;
6752 /* Convert the shift-count to an integer, regardless of size
6753 of value being shifted. */
6754 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6755 op1 = convert (integer_type_node, op1);
6756 /* Avoid converting op1 to result_type later. */
6757 converted = 1;
6759 break;
6761 case LSHIFT_EXPR:
6762 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6764 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6766 if (tree_int_cst_sgn (op1) < 0)
6767 warning ("left shift count is negative");
6769 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6770 warning ("left shift count >= width of type");
6773 /* Use the type of the value to be shifted. */
6774 result_type = type0;
6775 /* Convert the shift-count to an integer, regardless of size
6776 of value being shifted. */
6777 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6778 op1 = convert (integer_type_node, op1);
6779 /* Avoid converting op1 to result_type later. */
6780 converted = 1;
6782 break;
6784 case RROTATE_EXPR:
6785 case LROTATE_EXPR:
6786 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6788 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6790 if (tree_int_cst_sgn (op1) < 0)
6791 warning ("shift count is negative");
6792 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6793 warning ("shift count >= width of type");
6796 /* Use the type of the value to be shifted. */
6797 result_type = type0;
6798 /* Convert the shift-count to an integer, regardless of size
6799 of value being shifted. */
6800 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6801 op1 = convert (integer_type_node, op1);
6802 /* Avoid converting op1 to result_type later. */
6803 converted = 1;
6805 break;
6807 case EQ_EXPR:
6808 case NE_EXPR:
6809 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6810 warning ("comparing floating point with == or != is unsafe");
6811 /* Result of comparison is always int,
6812 but don't convert the args to int! */
6813 build_type = integer_type_node;
6814 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6815 || code0 == COMPLEX_TYPE
6816 || code0 == VECTOR_TYPE)
6817 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6818 || code1 == COMPLEX_TYPE
6819 || code1 == VECTOR_TYPE))
6820 short_compare = 1;
6821 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6823 tree tt0 = TREE_TYPE (type0);
6824 tree tt1 = TREE_TYPE (type1);
6825 /* Anything compares with void *. void * compares with anything.
6826 Otherwise, the targets must be compatible
6827 and both must be object or both incomplete. */
6828 if (comp_target_types (type0, type1, 1))
6829 result_type = common_type (type0, type1);
6830 else if (VOID_TYPE_P (tt0))
6832 /* op0 != orig_op0 detects the case of something
6833 whose value is 0 but which isn't a valid null ptr const. */
6834 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6835 && TREE_CODE (tt1) == FUNCTION_TYPE)
6836 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6838 else if (VOID_TYPE_P (tt1))
6840 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6841 && TREE_CODE (tt0) == FUNCTION_TYPE)
6842 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6844 else
6845 pedwarn ("comparison of distinct pointer types lacks a cast");
6847 if (result_type == NULL_TREE)
6848 result_type = ptr_type_node;
6850 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6851 && integer_zerop (op1))
6852 result_type = type0;
6853 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6854 && integer_zerop (op0))
6855 result_type = type1;
6856 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6858 result_type = type0;
6859 pedwarn ("comparison between pointer and integer");
6861 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6863 result_type = type1;
6864 pedwarn ("comparison between pointer and integer");
6866 break;
6868 case MAX_EXPR:
6869 case MIN_EXPR:
6870 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6871 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6872 shorten = 1;
6873 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6875 if (comp_target_types (type0, type1, 1))
6877 result_type = common_type (type0, type1);
6878 if (pedantic
6879 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6880 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6882 else
6884 result_type = ptr_type_node;
6885 pedwarn ("comparison of distinct pointer types lacks a cast");
6888 break;
6890 case LE_EXPR:
6891 case GE_EXPR:
6892 case LT_EXPR:
6893 case GT_EXPR:
6894 build_type = integer_type_node;
6895 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6896 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6897 short_compare = 1;
6898 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6900 if (comp_target_types (type0, type1, 1))
6902 result_type = common_type (type0, type1);
6903 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6904 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6905 pedwarn ("comparison of complete and incomplete pointers");
6906 else if (pedantic
6907 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6908 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6910 else
6912 result_type = ptr_type_node;
6913 pedwarn ("comparison of distinct pointer types lacks a cast");
6916 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6917 && integer_zerop (op1))
6919 result_type = type0;
6920 if (pedantic || extra_warnings)
6921 pedwarn ("ordered comparison of pointer with integer zero");
6923 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6924 && integer_zerop (op0))
6926 result_type = type1;
6927 if (pedantic)
6928 pedwarn ("ordered comparison of pointer with integer zero");
6930 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6932 result_type = type0;
6933 pedwarn ("comparison between pointer and integer");
6935 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6937 result_type = type1;
6938 pedwarn ("comparison between pointer and integer");
6940 break;
6942 case UNORDERED_EXPR:
6943 case ORDERED_EXPR:
6944 case UNLT_EXPR:
6945 case UNLE_EXPR:
6946 case UNGT_EXPR:
6947 case UNGE_EXPR:
6948 case UNEQ_EXPR:
6949 build_type = integer_type_node;
6950 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6952 error ("unordered comparison on non-floating point argument");
6953 return error_mark_node;
6955 common = 1;
6956 break;
6958 default:
6959 break;
6962 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6963 || code0 == VECTOR_TYPE)
6965 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6966 || code1 == VECTOR_TYPE))
6968 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6970 if (shorten || common || short_compare)
6971 result_type = common_type (type0, type1);
6973 /* For certain operations (which identify themselves by shorten != 0)
6974 if both args were extended from the same smaller type,
6975 do the arithmetic in that type and then extend.
6977 shorten !=0 and !=1 indicates a bitwise operation.
6978 For them, this optimization is safe only if
6979 both args are zero-extended or both are sign-extended.
6980 Otherwise, we might change the result.
6981 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6982 but calculated in (unsigned short) it would be (unsigned short)-1. */
6984 if (shorten && none_complex)
6986 int unsigned0, unsigned1;
6987 tree arg0 = get_narrower (op0, &unsigned0);
6988 tree arg1 = get_narrower (op1, &unsigned1);
6989 /* UNS is 1 if the operation to be done is an unsigned one. */
6990 int uns = TREE_UNSIGNED (result_type);
6991 tree type;
6993 final_type = result_type;
6995 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6996 but it *requires* conversion to FINAL_TYPE. */
6998 if ((TYPE_PRECISION (TREE_TYPE (op0))
6999 == TYPE_PRECISION (TREE_TYPE (arg0)))
7000 && TREE_TYPE (op0) != final_type)
7001 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
7002 if ((TYPE_PRECISION (TREE_TYPE (op1))
7003 == TYPE_PRECISION (TREE_TYPE (arg1)))
7004 && TREE_TYPE (op1) != final_type)
7005 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
7007 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7009 /* For bitwise operations, signedness of nominal type
7010 does not matter. Consider only how operands were extended. */
7011 if (shorten == -1)
7012 uns = unsigned0;
7014 /* Note that in all three cases below we refrain from optimizing
7015 an unsigned operation on sign-extended args.
7016 That would not be valid. */
7018 /* Both args variable: if both extended in same way
7019 from same width, do it in that width.
7020 Do it unsigned if args were zero-extended. */
7021 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7022 < TYPE_PRECISION (result_type))
7023 && (TYPE_PRECISION (TREE_TYPE (arg1))
7024 == TYPE_PRECISION (TREE_TYPE (arg0)))
7025 && unsigned0 == unsigned1
7026 && (unsigned0 || !uns))
7027 result_type
7028 = c_common_signed_or_unsigned_type
7029 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7030 else if (TREE_CODE (arg0) == INTEGER_CST
7031 && (unsigned1 || !uns)
7032 && (TYPE_PRECISION (TREE_TYPE (arg1))
7033 < TYPE_PRECISION (result_type))
7034 && (type
7035 = c_common_signed_or_unsigned_type (unsigned1,
7036 TREE_TYPE (arg1)),
7037 int_fits_type_p (arg0, type)))
7038 result_type = type;
7039 else if (TREE_CODE (arg1) == INTEGER_CST
7040 && (unsigned0 || !uns)
7041 && (TYPE_PRECISION (TREE_TYPE (arg0))
7042 < TYPE_PRECISION (result_type))
7043 && (type
7044 = c_common_signed_or_unsigned_type (unsigned0,
7045 TREE_TYPE (arg0)),
7046 int_fits_type_p (arg1, type)))
7047 result_type = type;
7050 /* Shifts can be shortened if shifting right. */
7052 if (short_shift)
7054 int unsigned_arg;
7055 tree arg0 = get_narrower (op0, &unsigned_arg);
7057 final_type = result_type;
7059 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7060 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
7062 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7063 /* We can shorten only if the shift count is less than the
7064 number of bits in the smaller type size. */
7065 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7066 /* We cannot drop an unsigned shift after sign-extension. */
7067 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
7069 /* Do an unsigned shift if the operand was zero-extended. */
7070 result_type
7071 = c_common_signed_or_unsigned_type (unsigned_arg,
7072 TREE_TYPE (arg0));
7073 /* Convert value-to-be-shifted to that type. */
7074 if (TREE_TYPE (op0) != result_type)
7075 op0 = convert (result_type, op0);
7076 converted = 1;
7080 /* Comparison operations are shortened too but differently.
7081 They identify themselves by setting short_compare = 1. */
7083 if (short_compare)
7085 /* Don't write &op0, etc., because that would prevent op0
7086 from being kept in a register.
7087 Instead, make copies of the our local variables and
7088 pass the copies by reference, then copy them back afterward. */
7089 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7090 enum tree_code xresultcode = resultcode;
7091 tree val
7092 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7094 if (val != 0)
7095 return val;
7097 op0 = xop0, op1 = xop1;
7098 converted = 1;
7099 resultcode = xresultcode;
7101 if (warn_sign_compare && skip_evaluation == 0)
7103 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7104 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7105 int unsignedp0, unsignedp1;
7106 tree primop0 = get_narrower (op0, &unsignedp0);
7107 tree primop1 = get_narrower (op1, &unsignedp1);
7109 xop0 = orig_op0;
7110 xop1 = orig_op1;
7111 STRIP_TYPE_NOPS (xop0);
7112 STRIP_TYPE_NOPS (xop1);
7114 /* Give warnings for comparisons between signed and unsigned
7115 quantities that may fail.
7117 Do the checking based on the original operand trees, so that
7118 casts will be considered, but default promotions won't be.
7120 Do not warn if the comparison is being done in a signed type,
7121 since the signed type will only be chosen if it can represent
7122 all the values of the unsigned type. */
7123 if (! TREE_UNSIGNED (result_type))
7124 /* OK */;
7125 /* Do not warn if both operands are the same signedness. */
7126 else if (op0_signed == op1_signed)
7127 /* OK */;
7128 else
7130 tree sop, uop;
7132 if (op0_signed)
7133 sop = xop0, uop = xop1;
7134 else
7135 sop = xop1, uop = xop0;
7137 /* Do not warn if the signed quantity is an
7138 unsuffixed integer literal (or some static
7139 constant expression involving such literals or a
7140 conditional expression involving such literals)
7141 and it is non-negative. */
7142 if (c_tree_expr_nonnegative_p (sop))
7143 /* OK */;
7144 /* Do not warn if the comparison is an equality operation,
7145 the unsigned quantity is an integral constant, and it
7146 would fit in the result if the result were signed. */
7147 else if (TREE_CODE (uop) == INTEGER_CST
7148 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7149 && int_fits_type_p
7150 (uop, c_common_signed_type (result_type)))
7151 /* OK */;
7152 /* Do not warn if the unsigned quantity is an enumeration
7153 constant and its maximum value would fit in the result
7154 if the result were signed. */
7155 else if (TREE_CODE (uop) == INTEGER_CST
7156 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7157 && int_fits_type_p
7158 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7159 c_common_signed_type (result_type)))
7160 /* OK */;
7161 else
7162 warning ("comparison between signed and unsigned");
7165 /* Warn if two unsigned values are being compared in a size
7166 larger than their original size, and one (and only one) is the
7167 result of a `~' operator. This comparison will always fail.
7169 Also warn if one operand is a constant, and the constant
7170 does not have all bits set that are set in the ~ operand
7171 when it is extended. */
7173 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7174 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7176 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7177 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7178 &unsignedp0);
7179 else
7180 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7181 &unsignedp1);
7183 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7185 tree primop;
7186 HOST_WIDE_INT constant, mask;
7187 int unsignedp, bits;
7189 if (host_integerp (primop0, 0))
7191 primop = primop1;
7192 unsignedp = unsignedp1;
7193 constant = tree_low_cst (primop0, 0);
7195 else
7197 primop = primop0;
7198 unsignedp = unsignedp0;
7199 constant = tree_low_cst (primop1, 0);
7202 bits = TYPE_PRECISION (TREE_TYPE (primop));
7203 if (bits < TYPE_PRECISION (result_type)
7204 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7206 mask = (~ (HOST_WIDE_INT) 0) << bits;
7207 if ((mask & constant) != mask)
7208 warning ("comparison of promoted ~unsigned with constant");
7211 else if (unsignedp0 && unsignedp1
7212 && (TYPE_PRECISION (TREE_TYPE (primop0))
7213 < TYPE_PRECISION (result_type))
7214 && (TYPE_PRECISION (TREE_TYPE (primop1))
7215 < TYPE_PRECISION (result_type)))
7216 warning ("comparison of promoted ~unsigned with unsigned");
7222 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7223 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7224 Then the expression will be built.
7225 It will be given type FINAL_TYPE if that is nonzero;
7226 otherwise, it will be given type RESULT_TYPE. */
7228 if (!result_type)
7230 binary_op_error (code);
7231 return error_mark_node;
7234 if (! converted)
7236 if (TREE_TYPE (op0) != result_type)
7237 op0 = convert (result_type, op0);
7238 if (TREE_TYPE (op1) != result_type)
7239 op1 = convert (result_type, op1);
7242 if (build_type == NULL_TREE)
7243 build_type = result_type;
7246 tree result = build (resultcode, build_type, op0, op1);
7247 tree folded;
7249 /* Treat expressions in initializers specially as they can't trap. */
7250 folded = initializer_stack ? fold_initializer (result)
7251 : fold (result);
7252 if (folded == result)
7253 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7254 if (final_type != 0)
7255 return convert (final_type, folded);
7256 return folded;