2004-09-17 Jeffrey D. Oldham <oldham@codesourcery.com>
[official-gcc.git] / gcc / c-typeck.c
blob5ec00aa4250eca08c386afd619d0ec6d3fb87b1c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
47 /* The level of nesting inside "__alignof__". */
48 int in_alignof;
50 /* The level of nesting inside "sizeof". */
51 int in_sizeof;
53 /* The level of nesting inside "typeof". */
54 int in_typeof;
56 /* Nonzero if we've already printed a "missing braces around initializer"
57 message within this initializer. */
58 static int missing_braces_mentioned;
60 static int require_constant_value;
61 static int require_constant_elements;
63 static tree qualify_type (tree, tree);
64 static int tagged_types_tu_compatible_p (tree, tree);
65 static int comp_target_types (tree, tree, int);
66 static int function_types_compatible_p (tree, tree);
67 static int type_lists_compatible_p (tree, tree);
68 static tree decl_constant_value_for_broken_optimization (tree);
69 static tree default_function_array_conversion (tree);
70 static tree lookup_field (tree, tree);
71 static tree convert_arguments (tree, tree, tree, tree);
72 static tree pointer_diff (tree, tree);
73 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
74 int);
75 static void warn_for_assignment (const char *, const char *, tree, int);
76 static tree valid_compound_expr_initializer (tree, tree);
77 static void push_string (const char *);
78 static void push_member_name (tree);
79 static void push_array_bounds (int);
80 static int spelling_length (void);
81 static char *print_spelling (char *);
82 static void warning_init (const char *);
83 static tree digest_init (tree, tree, bool, int);
84 static void output_init_element (tree, bool, tree, tree, int);
85 static void output_pending_init_elements (int);
86 static int set_designator (int);
87 static void push_range_stack (tree);
88 static void add_pending_init (tree, tree);
89 static void set_nonincremental_init (void);
90 static void set_nonincremental_init_from_string (tree);
91 static tree find_init_member (tree);
92 static int lvalue_or_else (tree, const char *);
94 /* Do `exp = require_complete_type (exp);' to make sure exp
95 does not have an incomplete type. (That includes void types.) */
97 tree
98 require_complete_type (tree value)
100 tree type = TREE_TYPE (value);
102 if (value == error_mark_node || type == error_mark_node)
103 return error_mark_node;
105 /* First, detect a valid value with a complete type. */
106 if (COMPLETE_TYPE_P (type))
107 return value;
109 c_incomplete_type_error (value, type);
110 return error_mark_node;
113 /* Print an error message for invalid use of an incomplete type.
114 VALUE is the expression that was used (or 0 if that isn't known)
115 and TYPE is the type that was invalid. */
117 void
118 c_incomplete_type_error (tree value, tree type)
120 const char *type_code_string;
122 /* Avoid duplicate error message. */
123 if (TREE_CODE (type) == ERROR_MARK)
124 return;
126 if (value != 0 && (TREE_CODE (value) == VAR_DECL
127 || TREE_CODE (value) == PARM_DECL))
128 error ("%qs has an incomplete type",
129 IDENTIFIER_POINTER (DECL_NAME (value)));
130 else
132 retry:
133 /* We must print an error message. Be clever about what it says. */
135 switch (TREE_CODE (type))
137 case RECORD_TYPE:
138 type_code_string = "struct";
139 break;
141 case UNION_TYPE:
142 type_code_string = "union";
143 break;
145 case ENUMERAL_TYPE:
146 type_code_string = "enum";
147 break;
149 case VOID_TYPE:
150 error ("invalid use of void expression");
151 return;
153 case ARRAY_TYPE:
154 if (TYPE_DOMAIN (type))
156 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
158 error ("invalid use of flexible array member");
159 return;
161 type = TREE_TYPE (type);
162 goto retry;
164 error ("invalid use of array with unspecified bounds");
165 return;
167 default:
168 gcc_unreachable ();
171 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
172 error ("invalid use of undefined type %<%s %s%>",
173 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
174 else
175 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
176 error ("invalid use of incomplete typedef %qs",
177 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
181 /* Given a type, apply default promotions wrt unnamed function
182 arguments and return the new type. */
184 tree
185 c_type_promotes_to (tree type)
187 if (TYPE_MAIN_VARIANT (type) == float_type_node)
188 return double_type_node;
190 if (c_promoting_integer_type_p (type))
192 /* Preserve unsignedness if not really getting any wider. */
193 if (TYPE_UNSIGNED (type)
194 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
195 return unsigned_type_node;
196 return integer_type_node;
199 return type;
202 /* Return a variant of TYPE which has all the type qualifiers of LIKE
203 as well as those of TYPE. */
205 static tree
206 qualify_type (tree type, tree like)
208 return c_build_qualified_type (type,
209 TYPE_QUALS (type) | TYPE_QUALS (like));
212 /* Return the composite type of two compatible types.
214 We assume that comptypes has already been done and returned
215 nonzero; if that isn't so, this may crash. In particular, we
216 assume that qualifiers match. */
218 tree
219 composite_type (tree t1, tree t2)
221 enum tree_code code1;
222 enum tree_code code2;
223 tree attributes;
225 /* Save time if the two types are the same. */
227 if (t1 == t2) return t1;
229 /* If one type is nonsense, use the other. */
230 if (t1 == error_mark_node)
231 return t2;
232 if (t2 == error_mark_node)
233 return t1;
235 code1 = TREE_CODE (t1);
236 code2 = TREE_CODE (t2);
238 /* Merge the attributes. */
239 attributes = targetm.merge_type_attributes (t1, t2);
241 /* If one is an enumerated type and the other is the compatible
242 integer type, the composite type might be either of the two
243 (DR#013 question 3). For consistency, use the enumerated type as
244 the composite type. */
246 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
247 return t1;
248 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
249 return t2;
251 gcc_assert (code1 == code2);
253 switch (code1)
255 case POINTER_TYPE:
256 /* For two pointers, do this recursively on the target type. */
258 tree pointed_to_1 = TREE_TYPE (t1);
259 tree pointed_to_2 = TREE_TYPE (t2);
260 tree target = composite_type (pointed_to_1, pointed_to_2);
261 t1 = build_pointer_type (target);
262 t1 = build_type_attribute_variant (t1, attributes);
263 return qualify_type (t1, t2);
266 case ARRAY_TYPE:
268 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
270 /* We should not have any type quals on arrays at all. */
271 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
273 /* Save space: see if the result is identical to one of the args. */
274 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
275 return build_type_attribute_variant (t1, attributes);
276 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
277 return build_type_attribute_variant (t2, attributes);
279 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
280 return build_type_attribute_variant (t1, attributes);
281 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
282 return build_type_attribute_variant (t2, attributes);
284 /* Merge the element types, and have a size if either arg has one. */
285 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
286 return build_type_attribute_variant (t1, attributes);
289 case FUNCTION_TYPE:
290 /* Function types: prefer the one that specified arg types.
291 If both do, merge the arg types. Also merge the return types. */
293 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
294 tree p1 = TYPE_ARG_TYPES (t1);
295 tree p2 = TYPE_ARG_TYPES (t2);
296 int len;
297 tree newargs, n;
298 int i;
300 /* Save space: see if the result is identical to one of the args. */
301 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
302 return build_type_attribute_variant (t1, attributes);
303 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
304 return build_type_attribute_variant (t2, attributes);
306 /* Simple way if one arg fails to specify argument types. */
307 if (TYPE_ARG_TYPES (t1) == 0)
309 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
310 t1 = build_type_attribute_variant (t1, attributes);
311 return qualify_type (t1, t2);
313 if (TYPE_ARG_TYPES (t2) == 0)
315 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
316 t1 = build_type_attribute_variant (t1, attributes);
317 return qualify_type (t1, t2);
320 /* If both args specify argument types, we must merge the two
321 lists, argument by argument. */
322 /* Tell global_bindings_p to return false so that variable_size
323 doesn't abort on VLAs in parameter types. */
324 c_override_global_bindings_to_false = true;
326 len = list_length (p1);
327 newargs = 0;
329 for (i = 0; i < len; i++)
330 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
332 n = newargs;
334 for (; p1;
335 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
337 /* A null type means arg type is not specified.
338 Take whatever the other function type has. */
339 if (TREE_VALUE (p1) == 0)
341 TREE_VALUE (n) = TREE_VALUE (p2);
342 goto parm_done;
344 if (TREE_VALUE (p2) == 0)
346 TREE_VALUE (n) = TREE_VALUE (p1);
347 goto parm_done;
350 /* Given wait (union {union wait *u; int *i} *)
351 and wait (union wait *),
352 prefer union wait * as type of parm. */
353 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
354 && TREE_VALUE (p1) != TREE_VALUE (p2))
356 tree memb;
357 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
358 memb; memb = TREE_CHAIN (memb))
359 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
361 TREE_VALUE (n) = TREE_VALUE (p2);
362 if (pedantic)
363 pedwarn ("function types not truly compatible in ISO C");
364 goto parm_done;
367 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
368 && TREE_VALUE (p2) != TREE_VALUE (p1))
370 tree memb;
371 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
372 memb; memb = TREE_CHAIN (memb))
373 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
375 TREE_VALUE (n) = TREE_VALUE (p1);
376 if (pedantic)
377 pedwarn ("function types not truly compatible in ISO C");
378 goto parm_done;
381 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
382 parm_done: ;
385 c_override_global_bindings_to_false = false;
386 t1 = build_function_type (valtype, newargs);
387 t1 = qualify_type (t1, t2);
388 /* ... falls through ... */
391 default:
392 return build_type_attribute_variant (t1, attributes);
397 /* Return the type of a conditional expression between pointers to
398 possibly differently qualified versions of compatible types.
400 We assume that comp_target_types has already been done and returned
401 nonzero; if that isn't so, this may crash. */
403 static tree
404 common_pointer_type (tree t1, tree t2)
406 tree attributes;
407 tree pointed_to_1;
408 tree pointed_to_2;
409 tree target;
411 /* Save time if the two types are the same. */
413 if (t1 == t2) return t1;
415 /* If one type is nonsense, use the other. */
416 if (t1 == error_mark_node)
417 return t2;
418 if (t2 == error_mark_node)
419 return t1;
421 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
422 && TREE_CODE (t2) == POINTER_TYPE);
424 /* Merge the attributes. */
425 attributes = targetm.merge_type_attributes (t1, t2);
427 /* Find the composite type of the target types, and combine the
428 qualifiers of the two types' targets. */
429 pointed_to_1 = TREE_TYPE (t1);
430 pointed_to_2 = TREE_TYPE (t2);
431 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
432 TYPE_MAIN_VARIANT (pointed_to_2));
433 t1 = build_pointer_type (c_build_qualified_type
434 (target,
435 TYPE_QUALS (pointed_to_1) |
436 TYPE_QUALS (pointed_to_2)));
437 return build_type_attribute_variant (t1, attributes);
440 /* Return the common type for two arithmetic types under the usual
441 arithmetic conversions. The default conversions have already been
442 applied, and enumerated types converted to their compatible integer
443 types. The resulting type is unqualified and has no attributes.
445 This is the type for the result of most arithmetic operations
446 if the operands have the given two types. */
448 tree
449 common_type (tree t1, tree t2)
451 enum tree_code code1;
452 enum tree_code code2;
454 /* If one type is nonsense, use the other. */
455 if (t1 == error_mark_node)
456 return t2;
457 if (t2 == error_mark_node)
458 return t1;
460 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
461 t1 = TYPE_MAIN_VARIANT (t1);
463 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
464 t2 = TYPE_MAIN_VARIANT (t2);
466 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
467 t1 = build_type_attribute_variant (t1, NULL_TREE);
469 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
470 t2 = build_type_attribute_variant (t2, NULL_TREE);
472 /* Save time if the two types are the same. */
474 if (t1 == t2) return t1;
476 code1 = TREE_CODE (t1);
477 code2 = TREE_CODE (t2);
479 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
480 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
481 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
482 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
484 /* If one type is a vector type, return that type. (How the usual
485 arithmetic conversions apply to the vector types extension is not
486 precisely specified.) */
487 if (code1 == VECTOR_TYPE)
488 return t1;
490 if (code2 == VECTOR_TYPE)
491 return t2;
493 /* If one type is complex, form the common type of the non-complex
494 components, then make that complex. Use T1 or T2 if it is the
495 required type. */
496 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
498 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
499 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
500 tree subtype = common_type (subtype1, subtype2);
502 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
503 return t1;
504 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
505 return t2;
506 else
507 return build_complex_type (subtype);
510 /* If only one is real, use it as the result. */
512 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
513 return t1;
515 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
516 return t2;
518 /* Both real or both integers; use the one with greater precision. */
520 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
521 return t1;
522 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
523 return t2;
525 /* Same precision. Prefer long longs to longs to ints when the
526 same precision, following the C99 rules on integer type rank
527 (which are equivalent to the C90 rules for C90 types). */
529 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
530 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
531 return long_long_unsigned_type_node;
533 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
534 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
536 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
537 return long_long_unsigned_type_node;
538 else
539 return long_long_integer_type_node;
542 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
543 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
544 return long_unsigned_type_node;
546 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
547 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
549 /* But preserve unsignedness from the other type,
550 since long cannot hold all the values of an unsigned int. */
551 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
552 return long_unsigned_type_node;
553 else
554 return long_integer_type_node;
557 /* Likewise, prefer long double to double even if same size. */
558 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
559 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
560 return long_double_type_node;
562 /* Otherwise prefer the unsigned one. */
564 if (TYPE_UNSIGNED (t1))
565 return t1;
566 else
567 return t2;
570 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
571 or various other operations. Return 2 if they are compatible
572 but a warning may be needed if you use them together. */
575 comptypes (tree type1, tree type2)
577 tree t1 = type1;
578 tree t2 = type2;
579 int attrval, val;
581 /* Suppress errors caused by previously reported errors. */
583 if (t1 == t2 || !t1 || !t2
584 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
585 return 1;
587 /* If either type is the internal version of sizetype, return the
588 language version. */
589 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
590 && TYPE_ORIG_SIZE_TYPE (t1))
591 t1 = TYPE_ORIG_SIZE_TYPE (t1);
593 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
594 && TYPE_ORIG_SIZE_TYPE (t2))
595 t2 = TYPE_ORIG_SIZE_TYPE (t2);
598 /* Enumerated types are compatible with integer types, but this is
599 not transitive: two enumerated types in the same translation unit
600 are compatible with each other only if they are the same type. */
602 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
603 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
604 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
605 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
607 if (t1 == t2)
608 return 1;
610 /* Different classes of types can't be compatible. */
612 if (TREE_CODE (t1) != TREE_CODE (t2))
613 return 0;
615 /* Qualifiers must match. C99 6.7.3p9 */
617 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
618 return 0;
620 /* Allow for two different type nodes which have essentially the same
621 definition. Note that we already checked for equality of the type
622 qualifiers (just above). */
624 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
625 return 1;
627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
628 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
629 return 0;
631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
632 val = 0;
634 switch (TREE_CODE (t1))
636 case POINTER_TYPE:
637 /* We must give ObjC the first crack at comparing pointers, since
638 protocol qualifiers may be involved. */
639 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
640 break;
641 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
642 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
643 break;
645 case FUNCTION_TYPE:
646 val = function_types_compatible_p (t1, t2);
647 break;
649 case ARRAY_TYPE:
651 tree d1 = TYPE_DOMAIN (t1);
652 tree d2 = TYPE_DOMAIN (t2);
653 bool d1_variable, d2_variable;
654 bool d1_zero, d2_zero;
655 val = 1;
657 /* Target types must match incl. qualifiers. */
658 if (TREE_TYPE (t1) != TREE_TYPE (t2)
659 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
660 return 0;
662 /* Sizes must match unless one is missing or variable. */
663 if (d1 == 0 || d2 == 0 || d1 == d2)
664 break;
666 d1_zero = ! TYPE_MAX_VALUE (d1);
667 d2_zero = ! TYPE_MAX_VALUE (d2);
669 d1_variable = (! d1_zero
670 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
671 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
672 d2_variable = (! d2_zero
673 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
674 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
676 if (d1_variable || d2_variable)
677 break;
678 if (d1_zero && d2_zero)
679 break;
680 if (d1_zero || d2_zero
681 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
682 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
683 val = 0;
685 break;
688 case RECORD_TYPE:
689 /* We are dealing with two distinct structs. In assorted Objective-C
690 corner cases, however, these can still be deemed equivalent. */
691 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
692 val = 1;
694 case ENUMERAL_TYPE:
695 case UNION_TYPE:
696 if (val != 1 && !same_translation_unit_p (t1, t2))
697 val = tagged_types_tu_compatible_p (t1, t2);
698 break;
700 case VECTOR_TYPE:
701 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
702 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
703 break;
705 default:
706 break;
708 return attrval == 2 && val == 1 ? 2 : val;
711 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
712 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
713 to 1 or 0 depending if the check of the pointer types is meant to
714 be reflexive or not (typically, assignments are not reflexive,
715 while comparisons are reflexive).
718 static int
719 comp_target_types (tree ttl, tree ttr, int reflexive)
721 int val;
723 /* Give objc_comptypes a crack at letting these types through. */
724 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
725 return val;
727 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
728 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
730 if (val == 2 && pedantic)
731 pedwarn ("types are not quite compatible");
732 return val;
735 /* Subroutines of `comptypes'. */
737 /* Determine whether two trees derive from the same translation unit.
738 If the CONTEXT chain ends in a null, that tree's context is still
739 being parsed, so if two trees have context chains ending in null,
740 they're in the same translation unit. */
742 same_translation_unit_p (tree t1, tree t2)
744 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
745 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
747 case tcc_declaration:
748 t1 = DECL_CONTEXT (t1); break;
749 case tcc_type:
750 t1 = TYPE_CONTEXT (t1); break;
751 case tcc_exceptional:
752 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
753 default: gcc_unreachable ();
756 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
757 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
759 case tcc_declaration:
760 t2 = DECL_CONTEXT (t2); break;
761 case tcc_type:
762 t2 = TYPE_CONTEXT (t2); break;
763 case tcc_exceptional:
764 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
765 default: gcc_unreachable ();
768 return t1 == t2;
771 /* The C standard says that two structures in different translation
772 units are compatible with each other only if the types of their
773 fields are compatible (among other things). So, consider two copies
774 of this structure: */
776 struct tagged_tu_seen {
777 const struct tagged_tu_seen * next;
778 tree t1;
779 tree t2;
782 /* Can they be compatible with each other? We choose to break the
783 recursion by allowing those types to be compatible. */
785 static const struct tagged_tu_seen * tagged_tu_seen_base;
787 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
788 compatible. If the two types are not the same (which has been
789 checked earlier), this can only happen when multiple translation
790 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
791 rules. */
793 static int
794 tagged_types_tu_compatible_p (tree t1, tree t2)
796 tree s1, s2;
797 bool needs_warning = false;
799 /* We have to verify that the tags of the types are the same. This
800 is harder than it looks because this may be a typedef, so we have
801 to go look at the original type. It may even be a typedef of a
802 typedef...
803 In the case of compiler-created builtin structs the TYPE_DECL
804 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
805 while (TYPE_NAME (t1)
806 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
807 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
808 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
810 while (TYPE_NAME (t2)
811 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
812 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
813 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
815 /* C90 didn't have the requirement that the two tags be the same. */
816 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
817 return 0;
819 /* C90 didn't say what happened if one or both of the types were
820 incomplete; we choose to follow C99 rules here, which is that they
821 are compatible. */
822 if (TYPE_SIZE (t1) == NULL
823 || TYPE_SIZE (t2) == NULL)
824 return 1;
827 const struct tagged_tu_seen * tts_i;
828 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
829 if (tts_i->t1 == t1 && tts_i->t2 == t2)
830 return 1;
833 switch (TREE_CODE (t1))
835 case ENUMERAL_TYPE:
838 /* Speed up the case where the type values are in the same order. */
839 tree tv1 = TYPE_VALUES (t1);
840 tree tv2 = TYPE_VALUES (t2);
842 if (tv1 == tv2)
843 return 1;
845 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
847 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
848 break;
849 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
850 return 0;
853 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
854 return 1;
855 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
856 return 0;
858 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
859 return 0;
861 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
863 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
864 if (s2 == NULL
865 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
866 return 0;
868 return 1;
871 case UNION_TYPE:
873 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
874 return 0;
876 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
878 bool ok = false;
879 struct tagged_tu_seen tts;
881 tts.next = tagged_tu_seen_base;
882 tts.t1 = t1;
883 tts.t2 = t2;
884 tagged_tu_seen_base = &tts;
886 if (DECL_NAME (s1) != NULL)
887 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
888 if (DECL_NAME (s1) == DECL_NAME (s2))
890 int result;
891 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
892 if (result == 0)
893 break;
894 if (result == 2)
895 needs_warning = true;
897 if (TREE_CODE (s1) == FIELD_DECL
898 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
899 DECL_FIELD_BIT_OFFSET (s2)) != 1)
900 break;
902 ok = true;
903 break;
905 tagged_tu_seen_base = tts.next;
906 if (! ok)
907 return 0;
909 return needs_warning ? 2 : 1;
912 case RECORD_TYPE:
914 struct tagged_tu_seen tts;
916 tts.next = tagged_tu_seen_base;
917 tts.t1 = t1;
918 tts.t2 = t2;
919 tagged_tu_seen_base = &tts;
921 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
922 s1 && s2;
923 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
925 int result;
926 if (TREE_CODE (s1) != TREE_CODE (s2)
927 || DECL_NAME (s1) != DECL_NAME (s2))
928 break;
929 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
930 if (result == 0)
931 break;
932 if (result == 2)
933 needs_warning = true;
935 if (TREE_CODE (s1) == FIELD_DECL
936 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
937 DECL_FIELD_BIT_OFFSET (s2)) != 1)
938 break;
940 tagged_tu_seen_base = tts.next;
941 if (s1 && s2)
942 return 0;
943 return needs_warning ? 2 : 1;
946 default:
947 gcc_unreachable ();
951 /* Return 1 if two function types F1 and F2 are compatible.
952 If either type specifies no argument types,
953 the other must specify a fixed number of self-promoting arg types.
954 Otherwise, if one type specifies only the number of arguments,
955 the other must specify that number of self-promoting arg types.
956 Otherwise, the argument types must match. */
958 static int
959 function_types_compatible_p (tree f1, tree f2)
961 tree args1, args2;
962 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
963 int val = 1;
964 int val1;
965 tree ret1, ret2;
967 ret1 = TREE_TYPE (f1);
968 ret2 = TREE_TYPE (f2);
970 /* 'volatile' qualifiers on a function's return type used to mean
971 the function is noreturn. */
972 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
973 pedwarn ("function return types not compatible due to %<volatile%>");
974 if (TYPE_VOLATILE (ret1))
975 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
976 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
977 if (TYPE_VOLATILE (ret2))
978 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
979 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
980 val = comptypes (ret1, ret2);
981 if (val == 0)
982 return 0;
984 args1 = TYPE_ARG_TYPES (f1);
985 args2 = TYPE_ARG_TYPES (f2);
987 /* An unspecified parmlist matches any specified parmlist
988 whose argument types don't need default promotions. */
990 if (args1 == 0)
992 if (!self_promoting_args_p (args2))
993 return 0;
994 /* If one of these types comes from a non-prototype fn definition,
995 compare that with the other type's arglist.
996 If they don't match, ask for a warning (but no error). */
997 if (TYPE_ACTUAL_ARG_TYPES (f1)
998 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
999 val = 2;
1000 return val;
1002 if (args2 == 0)
1004 if (!self_promoting_args_p (args1))
1005 return 0;
1006 if (TYPE_ACTUAL_ARG_TYPES (f2)
1007 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1008 val = 2;
1009 return val;
1012 /* Both types have argument lists: compare them and propagate results. */
1013 val1 = type_lists_compatible_p (args1, args2);
1014 return val1 != 1 ? val1 : val;
1017 /* Check two lists of types for compatibility,
1018 returning 0 for incompatible, 1 for compatible,
1019 or 2 for compatible with warning. */
1021 static int
1022 type_lists_compatible_p (tree args1, tree args2)
1024 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1025 int val = 1;
1026 int newval = 0;
1028 while (1)
1030 if (args1 == 0 && args2 == 0)
1031 return val;
1032 /* If one list is shorter than the other,
1033 they fail to match. */
1034 if (args1 == 0 || args2 == 0)
1035 return 0;
1036 /* A null pointer instead of a type
1037 means there is supposed to be an argument
1038 but nothing is specified about what type it has.
1039 So match anything that self-promotes. */
1040 if (TREE_VALUE (args1) == 0)
1042 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1043 return 0;
1045 else if (TREE_VALUE (args2) == 0)
1047 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1048 return 0;
1050 /* If one of the lists has an error marker, ignore this arg. */
1051 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1052 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1054 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1055 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1057 /* Allow wait (union {union wait *u; int *i} *)
1058 and wait (union wait *) to be compatible. */
1059 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1060 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1061 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1062 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1063 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1064 TYPE_SIZE (TREE_VALUE (args2))))
1066 tree memb;
1067 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1068 memb; memb = TREE_CHAIN (memb))
1069 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1070 break;
1071 if (memb == 0)
1072 return 0;
1074 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1075 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1076 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1077 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1078 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1079 TYPE_SIZE (TREE_VALUE (args1))))
1081 tree memb;
1082 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1083 memb; memb = TREE_CHAIN (memb))
1084 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1085 break;
1086 if (memb == 0)
1087 return 0;
1089 else
1090 return 0;
1093 /* comptypes said ok, but record if it said to warn. */
1094 if (newval > val)
1095 val = newval;
1097 args1 = TREE_CHAIN (args1);
1098 args2 = TREE_CHAIN (args2);
1102 /* Compute the size to increment a pointer by. */
1104 tree
1105 c_size_in_bytes (tree type)
1107 enum tree_code code = TREE_CODE (type);
1109 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1110 return size_one_node;
1112 if (!COMPLETE_OR_VOID_TYPE_P (type))
1114 error ("arithmetic on pointer to an incomplete type");
1115 return size_one_node;
1118 /* Convert in case a char is more than one unit. */
1119 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1120 size_int (TYPE_PRECISION (char_type_node)
1121 / BITS_PER_UNIT));
1124 /* Return either DECL or its known constant value (if it has one). */
1126 tree
1127 decl_constant_value (tree decl)
1129 if (/* Don't change a variable array bound or initial value to a constant
1130 in a place where a variable is invalid. Note that DECL_INITIAL
1131 isn't valid for a PARM_DECL. */
1132 current_function_decl != 0
1133 && TREE_CODE (decl) != PARM_DECL
1134 && ! TREE_THIS_VOLATILE (decl)
1135 && TREE_READONLY (decl)
1136 && DECL_INITIAL (decl) != 0
1137 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1138 /* This is invalid if initial value is not constant.
1139 If it has either a function call, a memory reference,
1140 or a variable, then re-evaluating it could give different results. */
1141 && TREE_CONSTANT (DECL_INITIAL (decl))
1142 /* Check for cases where this is sub-optimal, even though valid. */
1143 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1144 return DECL_INITIAL (decl);
1145 return decl;
1148 /* Return either DECL or its known constant value (if it has one), but
1149 return DECL if pedantic or DECL has mode BLKmode. This is for
1150 bug-compatibility with the old behavior of decl_constant_value
1151 (before GCC 3.0); every use of this function is a bug and it should
1152 be removed before GCC 3.1. It is not appropriate to use pedantic
1153 in a way that affects optimization, and BLKmode is probably not the
1154 right test for avoiding misoptimizations either. */
1156 static tree
1157 decl_constant_value_for_broken_optimization (tree decl)
1159 if (pedantic || DECL_MODE (decl) == BLKmode)
1160 return decl;
1161 else
1162 return decl_constant_value (decl);
1166 /* Perform the default conversion of arrays and functions to pointers.
1167 Return the result of converting EXP. For any other expression, just
1168 return EXP. */
1170 static tree
1171 default_function_array_conversion (tree exp)
1173 tree orig_exp;
1174 tree type = TREE_TYPE (exp);
1175 enum tree_code code = TREE_CODE (type);
1176 int not_lvalue = 0;
1178 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1179 an lvalue.
1181 Do not use STRIP_NOPS here! It will remove conversions from pointer
1182 to integer and cause infinite recursion. */
1183 orig_exp = exp;
1184 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1185 || (TREE_CODE (exp) == NOP_EXPR
1186 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1188 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1189 not_lvalue = 1;
1190 exp = TREE_OPERAND (exp, 0);
1193 if (TREE_NO_WARNING (orig_exp))
1194 TREE_NO_WARNING (exp) = 1;
1196 if (code == FUNCTION_TYPE)
1198 return build_unary_op (ADDR_EXPR, exp, 0);
1200 if (code == ARRAY_TYPE)
1202 tree adr;
1203 tree restype = TREE_TYPE (type);
1204 tree ptrtype;
1205 int constp = 0;
1206 int volatilep = 0;
1207 int lvalue_array_p;
1209 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1211 constp = TREE_READONLY (exp);
1212 volatilep = TREE_THIS_VOLATILE (exp);
1215 if (TYPE_QUALS (type) || constp || volatilep)
1216 restype
1217 = c_build_qualified_type (restype,
1218 TYPE_QUALS (type)
1219 | (constp * TYPE_QUAL_CONST)
1220 | (volatilep * TYPE_QUAL_VOLATILE));
1222 if (TREE_CODE (exp) == INDIRECT_REF)
1223 return convert (build_pointer_type (restype),
1224 TREE_OPERAND (exp, 0));
1226 if (TREE_CODE (exp) == COMPOUND_EXPR)
1228 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1229 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1230 TREE_OPERAND (exp, 0), op1);
1233 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1234 if (!flag_isoc99 && !lvalue_array_p)
1236 /* Before C99, non-lvalue arrays do not decay to pointers.
1237 Normally, using such an array would be invalid; but it can
1238 be used correctly inside sizeof or as a statement expression.
1239 Thus, do not give an error here; an error will result later. */
1240 return exp;
1243 ptrtype = build_pointer_type (restype);
1245 if (TREE_CODE (exp) == VAR_DECL)
1247 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1248 ADDR_EXPR because it's the best way of representing what
1249 happens in C when we take the address of an array and place
1250 it in a pointer to the element type. */
1251 adr = build1 (ADDR_EXPR, ptrtype, exp);
1252 if (!c_mark_addressable (exp))
1253 return error_mark_node;
1254 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1255 return adr;
1257 /* This way is better for a COMPONENT_REF since it can
1258 simplify the offset for a component. */
1259 adr = build_unary_op (ADDR_EXPR, exp, 1);
1260 return convert (ptrtype, adr);
1262 return exp;
1265 /* Perform default promotions for C data used in expressions.
1266 Arrays and functions are converted to pointers;
1267 enumeral types or short or char, to int.
1268 In addition, manifest constants symbols are replaced by their values. */
1270 tree
1271 default_conversion (tree exp)
1273 tree orig_exp;
1274 tree type = TREE_TYPE (exp);
1275 enum tree_code code = TREE_CODE (type);
1277 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1278 return default_function_array_conversion (exp);
1280 /* Constants can be used directly unless they're not loadable. */
1281 if (TREE_CODE (exp) == CONST_DECL)
1282 exp = DECL_INITIAL (exp);
1284 /* Replace a nonvolatile const static variable with its value unless
1285 it is an array, in which case we must be sure that taking the
1286 address of the array produces consistent results. */
1287 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1289 exp = decl_constant_value_for_broken_optimization (exp);
1290 type = TREE_TYPE (exp);
1293 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1294 an lvalue.
1296 Do not use STRIP_NOPS here! It will remove conversions from pointer
1297 to integer and cause infinite recursion. */
1298 orig_exp = exp;
1299 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1300 || (TREE_CODE (exp) == NOP_EXPR
1301 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1302 exp = TREE_OPERAND (exp, 0);
1304 if (TREE_NO_WARNING (orig_exp))
1305 TREE_NO_WARNING (exp) = 1;
1307 /* Normally convert enums to int,
1308 but convert wide enums to something wider. */
1309 if (code == ENUMERAL_TYPE)
1311 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1312 TYPE_PRECISION (integer_type_node)),
1313 ((TYPE_PRECISION (type)
1314 >= TYPE_PRECISION (integer_type_node))
1315 && TYPE_UNSIGNED (type)));
1317 return convert (type, exp);
1320 if (TREE_CODE (exp) == COMPONENT_REF
1321 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1322 /* If it's thinner than an int, promote it like a
1323 c_promoting_integer_type_p, otherwise leave it alone. */
1324 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1325 TYPE_PRECISION (integer_type_node)))
1326 return convert (integer_type_node, exp);
1328 if (c_promoting_integer_type_p (type))
1330 /* Preserve unsignedness if not really getting any wider. */
1331 if (TYPE_UNSIGNED (type)
1332 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1333 return convert (unsigned_type_node, exp);
1335 return convert (integer_type_node, exp);
1338 if (code == VOID_TYPE)
1340 error ("void value not ignored as it ought to be");
1341 return error_mark_node;
1343 return exp;
1346 /* Look up COMPONENT in a structure or union DECL.
1348 If the component name is not found, returns NULL_TREE. Otherwise,
1349 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1350 stepping down the chain to the component, which is in the last
1351 TREE_VALUE of the list. Normally the list is of length one, but if
1352 the component is embedded within (nested) anonymous structures or
1353 unions, the list steps down the chain to the component. */
1355 static tree
1356 lookup_field (tree decl, tree component)
1358 tree type = TREE_TYPE (decl);
1359 tree field;
1361 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1362 to the field elements. Use a binary search on this array to quickly
1363 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1364 will always be set for structures which have many elements. */
1366 if (TYPE_LANG_SPECIFIC (type))
1368 int bot, top, half;
1369 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1371 field = TYPE_FIELDS (type);
1372 bot = 0;
1373 top = TYPE_LANG_SPECIFIC (type)->s->len;
1374 while (top - bot > 1)
1376 half = (top - bot + 1) >> 1;
1377 field = field_array[bot+half];
1379 if (DECL_NAME (field) == NULL_TREE)
1381 /* Step through all anon unions in linear fashion. */
1382 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1384 field = field_array[bot++];
1385 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1386 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1388 tree anon = lookup_field (field, component);
1390 if (anon)
1391 return tree_cons (NULL_TREE, field, anon);
1395 /* Entire record is only anon unions. */
1396 if (bot > top)
1397 return NULL_TREE;
1399 /* Restart the binary search, with new lower bound. */
1400 continue;
1403 if (DECL_NAME (field) == component)
1404 break;
1405 if (DECL_NAME (field) < component)
1406 bot += half;
1407 else
1408 top = bot + half;
1411 if (DECL_NAME (field_array[bot]) == component)
1412 field = field_array[bot];
1413 else if (DECL_NAME (field) != component)
1414 return NULL_TREE;
1416 else
1418 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1420 if (DECL_NAME (field) == NULL_TREE
1421 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1422 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1424 tree anon = lookup_field (field, component);
1426 if (anon)
1427 return tree_cons (NULL_TREE, field, anon);
1430 if (DECL_NAME (field) == component)
1431 break;
1434 if (field == NULL_TREE)
1435 return NULL_TREE;
1438 return tree_cons (NULL_TREE, field, NULL_TREE);
1441 /* Make an expression to refer to the COMPONENT field of
1442 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1444 tree
1445 build_component_ref (tree datum, tree component)
1447 tree type = TREE_TYPE (datum);
1448 enum tree_code code = TREE_CODE (type);
1449 tree field = NULL;
1450 tree ref;
1452 if (!objc_is_public (datum, component))
1453 return error_mark_node;
1455 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1456 Ensure that the arguments are not lvalues; otherwise,
1457 if the component is an array, it would wrongly decay to a pointer in
1458 C89 mode.
1459 We cannot do this with a COND_EXPR, because in a conditional expression
1460 the default promotions are applied to both sides, and this would yield
1461 the wrong type of the result; for example, if the components have
1462 type "char". */
1463 switch (TREE_CODE (datum))
1465 case COMPOUND_EXPR:
1467 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1468 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
1469 TREE_OPERAND (datum, 0), non_lvalue (value));
1471 default:
1472 break;
1475 /* See if there is a field or component with name COMPONENT. */
1477 if (code == RECORD_TYPE || code == UNION_TYPE)
1479 if (!COMPLETE_TYPE_P (type))
1481 c_incomplete_type_error (NULL_TREE, type);
1482 return error_mark_node;
1485 field = lookup_field (datum, component);
1487 if (!field)
1489 error ("%s has no member named %qs",
1490 code == RECORD_TYPE ? "structure" : "union",
1491 IDENTIFIER_POINTER (component));
1492 return error_mark_node;
1495 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1496 This might be better solved in future the way the C++ front
1497 end does it - by giving the anonymous entities each a
1498 separate name and type, and then have build_component_ref
1499 recursively call itself. We can't do that here. */
1502 tree subdatum = TREE_VALUE (field);
1504 if (TREE_TYPE (subdatum) == error_mark_node)
1505 return error_mark_node;
1507 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1508 NULL_TREE);
1509 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1510 TREE_READONLY (ref) = 1;
1511 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1512 TREE_THIS_VOLATILE (ref) = 1;
1514 if (TREE_DEPRECATED (subdatum))
1515 warn_deprecated_use (subdatum);
1517 datum = ref;
1519 field = TREE_CHAIN (field);
1521 while (field);
1523 return ref;
1525 else if (code != ERROR_MARK)
1526 error ("request for member %qs in something not a structure or union",
1527 IDENTIFIER_POINTER (component));
1529 return error_mark_node;
1532 /* Given an expression PTR for a pointer, return an expression
1533 for the value pointed to.
1534 ERRORSTRING is the name of the operator to appear in error messages. */
1536 tree
1537 build_indirect_ref (tree ptr, const char *errorstring)
1539 tree pointer = default_conversion (ptr);
1540 tree type = TREE_TYPE (pointer);
1542 if (TREE_CODE (type) == POINTER_TYPE)
1544 if (TREE_CODE (pointer) == ADDR_EXPR
1545 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1546 == TREE_TYPE (type)))
1547 return TREE_OPERAND (pointer, 0);
1548 else
1550 tree t = TREE_TYPE (type);
1551 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1553 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1555 error ("dereferencing pointer to incomplete type");
1556 return error_mark_node;
1558 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1559 warning ("dereferencing %<void *%> pointer");
1561 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1562 so that we get the proper error message if the result is used
1563 to assign to. Also, &* is supposed to be a no-op.
1564 And ANSI C seems to specify that the type of the result
1565 should be the const type. */
1566 /* A de-reference of a pointer to const is not a const. It is valid
1567 to change it via some other pointer. */
1568 TREE_READONLY (ref) = TYPE_READONLY (t);
1569 TREE_SIDE_EFFECTS (ref)
1570 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1571 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1572 return ref;
1575 else if (TREE_CODE (pointer) != ERROR_MARK)
1576 error ("invalid type argument of %qs", errorstring);
1577 return error_mark_node;
1580 /* This handles expressions of the form "a[i]", which denotes
1581 an array reference.
1583 This is logically equivalent in C to *(a+i), but we may do it differently.
1584 If A is a variable or a member, we generate a primitive ARRAY_REF.
1585 This avoids forcing the array out of registers, and can work on
1586 arrays that are not lvalues (for example, members of structures returned
1587 by functions). */
1589 tree
1590 build_array_ref (tree array, tree index)
1592 if (index == 0)
1594 error ("subscript missing in array reference");
1595 return error_mark_node;
1598 if (TREE_TYPE (array) == error_mark_node
1599 || TREE_TYPE (index) == error_mark_node)
1600 return error_mark_node;
1602 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1604 tree rval, type;
1606 /* Subscripting with type char is likely to lose
1607 on a machine where chars are signed.
1608 So warn on any machine, but optionally.
1609 Don't warn for unsigned char since that type is safe.
1610 Don't warn for signed char because anyone who uses that
1611 must have done so deliberately. */
1612 if (warn_char_subscripts
1613 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1614 warning ("array subscript has type %<char%>");
1616 /* Apply default promotions *after* noticing character types. */
1617 index = default_conversion (index);
1619 /* Require integer *after* promotion, for sake of enums. */
1620 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1622 error ("array subscript is not an integer");
1623 return error_mark_node;
1626 /* An array that is indexed by a non-constant
1627 cannot be stored in a register; we must be able to do
1628 address arithmetic on its address.
1629 Likewise an array of elements of variable size. */
1630 if (TREE_CODE (index) != INTEGER_CST
1631 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1632 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1634 if (!c_mark_addressable (array))
1635 return error_mark_node;
1637 /* An array that is indexed by a constant value which is not within
1638 the array bounds cannot be stored in a register either; because we
1639 would get a crash in store_bit_field/extract_bit_field when trying
1640 to access a non-existent part of the register. */
1641 if (TREE_CODE (index) == INTEGER_CST
1642 && TYPE_DOMAIN (TREE_TYPE (array))
1643 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1645 if (!c_mark_addressable (array))
1646 return error_mark_node;
1649 if (pedantic)
1651 tree foo = array;
1652 while (TREE_CODE (foo) == COMPONENT_REF)
1653 foo = TREE_OPERAND (foo, 0);
1654 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1655 pedwarn ("ISO C forbids subscripting %<register%> array");
1656 else if (! flag_isoc99 && ! lvalue_p (foo))
1657 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1660 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1661 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1662 /* Array ref is const/volatile if the array elements are
1663 or if the array is. */
1664 TREE_READONLY (rval)
1665 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1666 | TREE_READONLY (array));
1667 TREE_SIDE_EFFECTS (rval)
1668 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1669 | TREE_SIDE_EFFECTS (array));
1670 TREE_THIS_VOLATILE (rval)
1671 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1672 /* This was added by rms on 16 Nov 91.
1673 It fixes vol struct foo *a; a->elts[1]
1674 in an inline function.
1675 Hope it doesn't break something else. */
1676 | TREE_THIS_VOLATILE (array));
1677 return require_complete_type (fold (rval));
1681 tree ar = default_conversion (array);
1682 tree ind = default_conversion (index);
1684 /* Do the same warning check as above, but only on the part that's
1685 syntactically the index and only if it is also semantically
1686 the index. */
1687 if (warn_char_subscripts
1688 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1689 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1690 warning ("subscript has type %<char%>");
1692 /* Put the integer in IND to simplify error checking. */
1693 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1695 tree temp = ar;
1696 ar = ind;
1697 ind = temp;
1700 if (ar == error_mark_node)
1701 return ar;
1703 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1704 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1706 error ("subscripted value is neither array nor pointer");
1707 return error_mark_node;
1709 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1711 error ("array subscript is not an integer");
1712 return error_mark_node;
1715 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1716 "array indexing");
1720 /* Build an external reference to identifier ID. FUN indicates
1721 whether this will be used for a function call. */
1722 tree
1723 build_external_ref (tree id, int fun)
1725 tree ref;
1726 tree decl = lookup_name (id);
1727 tree objc_ivar = objc_lookup_ivar (id);
1729 if (decl && decl != error_mark_node)
1731 /* Properly declared variable or function reference. */
1732 if (!objc_ivar)
1733 ref = decl;
1734 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1736 warning ("local declaration of %qs hides instance variable",
1737 IDENTIFIER_POINTER (id));
1738 ref = decl;
1740 else
1741 ref = objc_ivar;
1743 else if (objc_ivar)
1744 ref = objc_ivar;
1745 else if (fun)
1746 /* Implicit function declaration. */
1747 ref = implicitly_declare (id);
1748 else if (decl == error_mark_node)
1749 /* Don't complain about something that's already been
1750 complained about. */
1751 return error_mark_node;
1752 else
1754 undeclared_variable (id);
1755 return error_mark_node;
1758 if (TREE_TYPE (ref) == error_mark_node)
1759 return error_mark_node;
1761 if (TREE_DEPRECATED (ref))
1762 warn_deprecated_use (ref);
1764 if (!skip_evaluation)
1765 assemble_external (ref);
1766 TREE_USED (ref) = 1;
1768 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1770 if (!in_sizeof && !in_typeof)
1771 C_DECL_USED (ref) = 1;
1772 else if (DECL_INITIAL (ref) == 0
1773 && DECL_EXTERNAL (ref)
1774 && !TREE_PUBLIC (ref))
1775 record_maybe_used_decl (ref);
1778 if (TREE_CODE (ref) == CONST_DECL)
1780 ref = DECL_INITIAL (ref);
1781 TREE_CONSTANT (ref) = 1;
1782 TREE_INVARIANT (ref) = 1;
1784 else if (current_function_decl != 0
1785 && !DECL_FILE_SCOPE_P (current_function_decl)
1786 && (TREE_CODE (ref) == VAR_DECL
1787 || TREE_CODE (ref) == PARM_DECL
1788 || TREE_CODE (ref) == FUNCTION_DECL))
1790 tree context = decl_function_context (ref);
1792 if (context != 0 && context != current_function_decl)
1793 DECL_NONLOCAL (ref) = 1;
1796 return ref;
1799 /* Record details of decls possibly used inside sizeof or typeof. */
1800 struct maybe_used_decl
1802 /* The decl. */
1803 tree decl;
1804 /* The level seen at (in_sizeof + in_typeof). */
1805 int level;
1806 /* The next one at this level or above, or NULL. */
1807 struct maybe_used_decl *next;
1810 static struct maybe_used_decl *maybe_used_decls;
1812 /* Record that DECL, an undefined static function reference seen
1813 inside sizeof or typeof, might be used if the operand of sizeof is
1814 a VLA type or the operand of typeof is a variably modified
1815 type. */
1817 void
1818 record_maybe_used_decl (tree decl)
1820 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1821 t->decl = decl;
1822 t->level = in_sizeof + in_typeof;
1823 t->next = maybe_used_decls;
1824 maybe_used_decls = t;
1827 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1828 USED is false, just discard them. If it is true, mark them used
1829 (if no longer inside sizeof or typeof) or move them to the next
1830 level up (if still inside sizeof or typeof). */
1832 void
1833 pop_maybe_used (bool used)
1835 struct maybe_used_decl *p = maybe_used_decls;
1836 int cur_level = in_sizeof + in_typeof;
1837 while (p && p->level > cur_level)
1839 if (used)
1841 if (cur_level == 0)
1842 C_DECL_USED (p->decl) = 1;
1843 else
1844 p->level = cur_level;
1846 p = p->next;
1848 if (!used || cur_level == 0)
1849 maybe_used_decls = p;
1852 /* Return the result of sizeof applied to EXPR. */
1854 struct c_expr
1855 c_expr_sizeof_expr (struct c_expr expr)
1857 struct c_expr ret;
1858 ret.value = c_sizeof (TREE_TYPE (expr.value));
1859 ret.original_code = ERROR_MARK;
1860 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1861 return ret;
1864 /* Return the result of sizeof applied to T, a structure for the type
1865 name passed to sizeof (rather than the type itself). */
1867 struct c_expr
1868 c_expr_sizeof_type (struct c_type_name *t)
1870 tree type;
1871 struct c_expr ret;
1872 type = groktypename (t);
1873 ret.value = c_sizeof (type);
1874 ret.original_code = ERROR_MARK;
1875 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1876 return ret;
1879 /* Build a function call to function FUNCTION with parameters PARAMS.
1880 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1881 TREE_VALUE of each node is a parameter-expression.
1882 FUNCTION's data type may be a function type or a pointer-to-function. */
1884 tree
1885 build_function_call (tree function, tree params)
1887 tree fntype, fundecl = 0;
1888 tree coerced_params;
1889 tree name = NULL_TREE, result;
1890 tree tem;
1892 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1893 STRIP_TYPE_NOPS (function);
1895 /* Convert anything with function type to a pointer-to-function. */
1896 if (TREE_CODE (function) == FUNCTION_DECL)
1898 name = DECL_NAME (function);
1900 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1901 (because calling an inline function does not mean the function
1902 needs to be separately compiled). */
1903 fntype = build_type_variant (TREE_TYPE (function),
1904 TREE_READONLY (function),
1905 TREE_THIS_VOLATILE (function));
1906 fundecl = function;
1907 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1909 else
1910 function = default_conversion (function);
1912 fntype = TREE_TYPE (function);
1914 if (TREE_CODE (fntype) == ERROR_MARK)
1915 return error_mark_node;
1917 if (!(TREE_CODE (fntype) == POINTER_TYPE
1918 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1920 error ("called object is not a function");
1921 return error_mark_node;
1924 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1925 current_function_returns_abnormally = 1;
1927 /* fntype now gets the type of function pointed to. */
1928 fntype = TREE_TYPE (fntype);
1930 /* Check that the function is called through a compatible prototype.
1931 If it is not, replace the call by a trap, wrapped up in a compound
1932 expression if necessary. This has the nice side-effect to prevent
1933 the tree-inliner from generating invalid assignment trees which may
1934 blow up in the RTL expander later.
1936 ??? This doesn't work for Objective-C because objc_comptypes
1937 refuses to compare function prototypes, yet the compiler appears
1938 to build calls that are flagged as invalid by C's comptypes. */
1939 if (! c_dialect_objc ()
1940 && TREE_CODE (function) == NOP_EXPR
1941 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1942 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1943 && ! comptypes (fntype, TREE_TYPE (tem)))
1945 tree return_type = TREE_TYPE (fntype);
1946 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1947 NULL_TREE);
1949 /* This situation leads to run-time undefined behavior. We can't,
1950 therefore, simply error unless we can prove that all possible
1951 executions of the program must execute the code. */
1952 warning ("function called through a non-compatible type");
1954 /* We can, however, treat "undefined" any way we please.
1955 Call abort to encourage the user to fix the program. */
1956 inform ("if this code is reached, the program will abort");
1958 if (VOID_TYPE_P (return_type))
1959 return trap;
1960 else
1962 tree rhs;
1964 if (AGGREGATE_TYPE_P (return_type))
1965 rhs = build_compound_literal (return_type,
1966 build_constructor (return_type,
1967 NULL_TREE));
1968 else
1969 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1971 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1975 /* Convert the parameters to the types declared in the
1976 function prototype, or apply default promotions. */
1978 coerced_params
1979 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1981 /* Check that the arguments to the function are valid. */
1983 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1985 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1986 function, coerced_params, NULL_TREE);
1987 TREE_SIDE_EFFECTS (result) = 1;
1989 if (require_constant_value)
1991 result = fold_initializer (result);
1993 if (TREE_CONSTANT (result)
1994 && (name == NULL_TREE
1995 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1996 pedwarn_init ("initializer element is not constant");
1998 else
1999 result = fold (result);
2001 if (VOID_TYPE_P (TREE_TYPE (result)))
2002 return result;
2003 return require_complete_type (result);
2006 /* Convert the argument expressions in the list VALUES
2007 to the types in the list TYPELIST. The result is a list of converted
2008 argument expressions.
2010 If TYPELIST is exhausted, or when an element has NULL as its type,
2011 perform the default conversions.
2013 PARMLIST is the chain of parm decls for the function being called.
2014 It may be 0, if that info is not available.
2015 It is used only for generating error messages.
2017 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2019 This is also where warnings about wrong number of args are generated.
2021 Both VALUES and the returned value are chains of TREE_LIST nodes
2022 with the elements of the list in the TREE_VALUE slots of those nodes. */
2024 static tree
2025 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
2027 tree typetail, valtail;
2028 tree result = NULL;
2029 int parmnum;
2031 /* Scan the given expressions and types, producing individual
2032 converted arguments and pushing them on RESULT in reverse order. */
2034 for (valtail = values, typetail = typelist, parmnum = 0;
2035 valtail;
2036 valtail = TREE_CHAIN (valtail), parmnum++)
2038 tree type = typetail ? TREE_VALUE (typetail) : 0;
2039 tree val = TREE_VALUE (valtail);
2041 if (type == void_type_node)
2043 if (name)
2044 error ("too many arguments to function %qs",
2045 IDENTIFIER_POINTER (name));
2046 else
2047 error ("too many arguments to function");
2048 break;
2051 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2052 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2053 to convert automatically to a pointer. */
2054 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2055 val = TREE_OPERAND (val, 0);
2057 val = default_function_array_conversion (val);
2059 val = require_complete_type (val);
2061 if (type != 0)
2063 /* Formal parm type is specified by a function prototype. */
2064 tree parmval;
2066 if (!COMPLETE_TYPE_P (type))
2068 error ("type of formal parameter %d is incomplete", parmnum + 1);
2069 parmval = val;
2071 else
2073 /* Optionally warn about conversions that
2074 differ from the default conversions. */
2075 if (warn_conversion || warn_traditional)
2077 unsigned int formal_prec = TYPE_PRECISION (type);
2079 if (INTEGRAL_TYPE_P (type)
2080 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2081 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
2082 if (INTEGRAL_TYPE_P (type)
2083 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2084 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2085 else if (TREE_CODE (type) == COMPLEX_TYPE
2086 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2087 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
2088 else if (TREE_CODE (type) == REAL_TYPE
2089 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2090 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2091 else if (TREE_CODE (type) == COMPLEX_TYPE
2092 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2093 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2094 else if (TREE_CODE (type) == REAL_TYPE
2095 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2096 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2097 /* ??? At some point, messages should be written about
2098 conversions between complex types, but that's too messy
2099 to do now. */
2100 else if (TREE_CODE (type) == REAL_TYPE
2101 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2103 /* Warn if any argument is passed as `float',
2104 since without a prototype it would be `double'. */
2105 if (formal_prec == TYPE_PRECISION (float_type_node))
2106 warn_for_assignment ("%s as %<float%> rather than "
2107 "%<double%> due to prototype",
2108 (char *) 0, name, parmnum + 1);
2110 /* Detect integer changing in width or signedness.
2111 These warnings are only activated with
2112 -Wconversion, not with -Wtraditional. */
2113 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2114 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2116 tree would_have_been = default_conversion (val);
2117 tree type1 = TREE_TYPE (would_have_been);
2119 if (TREE_CODE (type) == ENUMERAL_TYPE
2120 && (TYPE_MAIN_VARIANT (type)
2121 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2122 /* No warning if function asks for enum
2123 and the actual arg is that enum type. */
2125 else if (formal_prec != TYPE_PRECISION (type1))
2126 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2127 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2129 /* Don't complain if the formal parameter type
2130 is an enum, because we can't tell now whether
2131 the value was an enum--even the same enum. */
2132 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2134 else if (TREE_CODE (val) == INTEGER_CST
2135 && int_fits_type_p (val, type))
2136 /* Change in signedness doesn't matter
2137 if a constant value is unaffected. */
2139 /* Likewise for a constant in a NOP_EXPR. */
2140 else if (TREE_CODE (val) == NOP_EXPR
2141 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2142 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2144 /* If the value is extended from a narrower
2145 unsigned type, it doesn't matter whether we
2146 pass it as signed or unsigned; the value
2147 certainly is the same either way. */
2148 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2149 && TYPE_UNSIGNED (TREE_TYPE (val)))
2151 else if (TYPE_UNSIGNED (type))
2152 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2153 else
2154 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2158 parmval = convert_for_assignment (type, val,
2159 (char *) 0, /* arg passing */
2160 fundecl, name, parmnum + 1);
2162 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2163 && INTEGRAL_TYPE_P (type)
2164 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2165 parmval = default_conversion (parmval);
2167 result = tree_cons (NULL_TREE, parmval, result);
2169 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2170 && (TYPE_PRECISION (TREE_TYPE (val))
2171 < TYPE_PRECISION (double_type_node)))
2172 /* Convert `float' to `double'. */
2173 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2174 else
2175 /* Convert `short' and `char' to full-size `int'. */
2176 result = tree_cons (NULL_TREE, default_conversion (val), result);
2178 if (typetail)
2179 typetail = TREE_CHAIN (typetail);
2182 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2184 if (name)
2185 error ("too few arguments to function %qs",
2186 IDENTIFIER_POINTER (name));
2187 else
2188 error ("too few arguments to function");
2191 return nreverse (result);
2194 /* This is the entry point used by the parser
2195 for binary operators in the input.
2196 In addition to constructing the expression,
2197 we check for operands that were written with other binary operators
2198 in a way that is likely to confuse the user. */
2200 struct c_expr
2201 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2202 struct c_expr arg2)
2204 struct c_expr result;
2206 enum tree_code code1 = arg1.original_code;
2207 enum tree_code code2 = arg2.original_code;
2209 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2210 result.original_code = code;
2212 if (TREE_CODE (result.value) == ERROR_MARK)
2213 return result;
2215 /* Check for cases such as x+y<<z which users are likely
2216 to misinterpret. */
2217 if (warn_parentheses)
2219 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2221 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2222 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2223 warning ("suggest parentheses around + or - inside shift");
2226 if (code == TRUTH_ORIF_EXPR)
2228 if (code1 == TRUTH_ANDIF_EXPR
2229 || code2 == TRUTH_ANDIF_EXPR)
2230 warning ("suggest parentheses around && within ||");
2233 if (code == BIT_IOR_EXPR)
2235 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2236 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2237 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2238 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2239 warning ("suggest parentheses around arithmetic in operand of |");
2240 /* Check cases like x|y==z */
2241 if (TREE_CODE_CLASS (code1) == tcc_comparison
2242 || TREE_CODE_CLASS (code2) == tcc_comparison)
2243 warning ("suggest parentheses around comparison in operand of |");
2246 if (code == BIT_XOR_EXPR)
2248 if (code1 == BIT_AND_EXPR
2249 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2250 || code2 == BIT_AND_EXPR
2251 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2252 warning ("suggest parentheses around arithmetic in operand of ^");
2253 /* Check cases like x^y==z */
2254 if (TREE_CODE_CLASS (code1) == tcc_comparison
2255 || TREE_CODE_CLASS (code2) == tcc_comparison)
2256 warning ("suggest parentheses around comparison in operand of ^");
2259 if (code == BIT_AND_EXPR)
2261 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2262 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2263 warning ("suggest parentheses around + or - in operand of &");
2264 /* Check cases like x&y==z */
2265 if (TREE_CODE_CLASS (code1) == tcc_comparison
2266 || TREE_CODE_CLASS (code2) == tcc_comparison)
2267 warning ("suggest parentheses around comparison in operand of &");
2269 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2270 if (TREE_CODE_CLASS (code) == tcc_comparison
2271 && (TREE_CODE_CLASS (code1) == tcc_comparison
2272 || TREE_CODE_CLASS (code2) == tcc_comparison))
2273 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2277 unsigned_conversion_warning (result.value, arg1.value);
2278 unsigned_conversion_warning (result.value, arg2.value);
2279 overflow_warning (result.value);
2281 return result;
2284 /* Return a tree for the difference of pointers OP0 and OP1.
2285 The resulting tree has type int. */
2287 static tree
2288 pointer_diff (tree op0, tree op1)
2290 tree restype = ptrdiff_type_node;
2292 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2293 tree con0, con1, lit0, lit1;
2294 tree orig_op1 = op1;
2296 if (pedantic || warn_pointer_arith)
2298 if (TREE_CODE (target_type) == VOID_TYPE)
2299 pedwarn ("pointer of type %<void *%> used in subtraction");
2300 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2301 pedwarn ("pointer to a function used in subtraction");
2304 /* If the conversion to ptrdiff_type does anything like widening or
2305 converting a partial to an integral mode, we get a convert_expression
2306 that is in the way to do any simplifications.
2307 (fold-const.c doesn't know that the extra bits won't be needed.
2308 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2309 different mode in place.)
2310 So first try to find a common term here 'by hand'; we want to cover
2311 at least the cases that occur in legal static initializers. */
2312 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2313 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2315 if (TREE_CODE (con0) == PLUS_EXPR)
2317 lit0 = TREE_OPERAND (con0, 1);
2318 con0 = TREE_OPERAND (con0, 0);
2320 else
2321 lit0 = integer_zero_node;
2323 if (TREE_CODE (con1) == PLUS_EXPR)
2325 lit1 = TREE_OPERAND (con1, 1);
2326 con1 = TREE_OPERAND (con1, 0);
2328 else
2329 lit1 = integer_zero_node;
2331 if (operand_equal_p (con0, con1, 0))
2333 op0 = lit0;
2334 op1 = lit1;
2338 /* First do the subtraction as integers;
2339 then drop through to build the divide operator.
2340 Do not do default conversions on the minus operator
2341 in case restype is a short type. */
2343 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2344 convert (restype, op1), 0);
2345 /* This generates an error if op1 is pointer to incomplete type. */
2346 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2347 error ("arithmetic on pointer to an incomplete type");
2349 /* This generates an error if op0 is pointer to incomplete type. */
2350 op1 = c_size_in_bytes (target_type);
2352 /* Divide by the size, in easiest possible way. */
2353 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2356 /* Construct and perhaps optimize a tree representation
2357 for a unary operation. CODE, a tree_code, specifies the operation
2358 and XARG is the operand.
2359 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2360 the default promotions (such as from short to int).
2361 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2362 allows non-lvalues; this is only used to handle conversion of non-lvalue
2363 arrays to pointers in C99. */
2365 tree
2366 build_unary_op (enum tree_code code, tree xarg, int flag)
2368 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2369 tree arg = xarg;
2370 tree argtype = 0;
2371 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2372 tree val;
2373 int noconvert = flag;
2375 if (typecode == ERROR_MARK)
2376 return error_mark_node;
2377 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2378 typecode = INTEGER_TYPE;
2380 switch (code)
2382 case CONVERT_EXPR:
2383 /* This is used for unary plus, because a CONVERT_EXPR
2384 is enough to prevent anybody from looking inside for
2385 associativity, but won't generate any code. */
2386 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2387 || typecode == COMPLEX_TYPE
2388 || typecode == VECTOR_TYPE))
2390 error ("wrong type argument to unary plus");
2391 return error_mark_node;
2393 else if (!noconvert)
2394 arg = default_conversion (arg);
2395 arg = non_lvalue (arg);
2396 break;
2398 case NEGATE_EXPR:
2399 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2400 || typecode == COMPLEX_TYPE
2401 || typecode == VECTOR_TYPE))
2403 error ("wrong type argument to unary minus");
2404 return error_mark_node;
2406 else if (!noconvert)
2407 arg = default_conversion (arg);
2408 break;
2410 case BIT_NOT_EXPR:
2411 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2413 if (!noconvert)
2414 arg = default_conversion (arg);
2416 else if (typecode == COMPLEX_TYPE)
2418 code = CONJ_EXPR;
2419 if (pedantic)
2420 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2421 if (!noconvert)
2422 arg = default_conversion (arg);
2424 else
2426 error ("wrong type argument to bit-complement");
2427 return error_mark_node;
2429 break;
2431 case ABS_EXPR:
2432 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2434 error ("wrong type argument to abs");
2435 return error_mark_node;
2437 else if (!noconvert)
2438 arg = default_conversion (arg);
2439 break;
2441 case CONJ_EXPR:
2442 /* Conjugating a real value is a no-op, but allow it anyway. */
2443 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2444 || typecode == COMPLEX_TYPE))
2446 error ("wrong type argument to conjugation");
2447 return error_mark_node;
2449 else if (!noconvert)
2450 arg = default_conversion (arg);
2451 break;
2453 case TRUTH_NOT_EXPR:
2454 if (typecode != INTEGER_TYPE
2455 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2456 && typecode != COMPLEX_TYPE
2457 /* These will convert to a pointer. */
2458 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2460 error ("wrong type argument to unary exclamation mark");
2461 return error_mark_node;
2463 arg = lang_hooks.truthvalue_conversion (arg);
2464 return invert_truthvalue (arg);
2466 case NOP_EXPR:
2467 break;
2469 case REALPART_EXPR:
2470 if (TREE_CODE (arg) == COMPLEX_CST)
2471 return TREE_REALPART (arg);
2472 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2473 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2474 else
2475 return arg;
2477 case IMAGPART_EXPR:
2478 if (TREE_CODE (arg) == COMPLEX_CST)
2479 return TREE_IMAGPART (arg);
2480 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2481 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2482 else
2483 return convert (TREE_TYPE (arg), integer_zero_node);
2485 case PREINCREMENT_EXPR:
2486 case POSTINCREMENT_EXPR:
2487 case PREDECREMENT_EXPR:
2488 case POSTDECREMENT_EXPR:
2490 /* Increment or decrement the real part of the value,
2491 and don't change the imaginary part. */
2492 if (typecode == COMPLEX_TYPE)
2494 tree real, imag;
2496 if (pedantic)
2497 pedwarn ("ISO C does not support %<++%> and %<--%>"
2498 " on complex types");
2500 arg = stabilize_reference (arg);
2501 real = build_unary_op (REALPART_EXPR, arg, 1);
2502 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2503 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2504 build_unary_op (code, real, 1), imag);
2507 /* Report invalid types. */
2509 if (typecode != POINTER_TYPE
2510 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2512 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2513 error ("wrong type argument to increment");
2514 else
2515 error ("wrong type argument to decrement");
2517 return error_mark_node;
2521 tree inc;
2522 tree result_type = TREE_TYPE (arg);
2524 arg = get_unwidened (arg, 0);
2525 argtype = TREE_TYPE (arg);
2527 /* Compute the increment. */
2529 if (typecode == POINTER_TYPE)
2531 /* If pointer target is an undefined struct,
2532 we just cannot know how to do the arithmetic. */
2533 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2535 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2536 error ("increment of pointer to unknown structure");
2537 else
2538 error ("decrement of pointer to unknown structure");
2540 else if ((pedantic || warn_pointer_arith)
2541 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2542 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2544 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2545 pedwarn ("wrong type argument to increment");
2546 else
2547 pedwarn ("wrong type argument to decrement");
2550 inc = c_size_in_bytes (TREE_TYPE (result_type));
2552 else
2553 inc = integer_one_node;
2555 inc = convert (argtype, inc);
2557 /* Complain about anything else that is not a true lvalue. */
2558 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2559 || code == POSTINCREMENT_EXPR)
2560 ? "invalid lvalue in increment"
2561 : "invalid lvalue in decrement")))
2562 return error_mark_node;
2564 /* Report a read-only lvalue. */
2565 if (TREE_READONLY (arg))
2566 readonly_error (arg,
2567 ((code == PREINCREMENT_EXPR
2568 || code == POSTINCREMENT_EXPR)
2569 ? "increment" : "decrement"));
2571 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2572 val = boolean_increment (code, arg);
2573 else
2574 val = build2 (code, TREE_TYPE (arg), arg, inc);
2575 TREE_SIDE_EFFECTS (val) = 1;
2576 val = convert (result_type, val);
2577 if (TREE_CODE (val) != code)
2578 TREE_NO_WARNING (val) = 1;
2579 return val;
2582 case ADDR_EXPR:
2583 /* Note that this operation never does default_conversion. */
2585 /* Let &* cancel out to simplify resulting code. */
2586 if (TREE_CODE (arg) == INDIRECT_REF)
2588 /* Don't let this be an lvalue. */
2589 if (lvalue_p (TREE_OPERAND (arg, 0)))
2590 return non_lvalue (TREE_OPERAND (arg, 0));
2591 return TREE_OPERAND (arg, 0);
2594 /* For &x[y], return x+y */
2595 if (TREE_CODE (arg) == ARRAY_REF)
2597 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2598 return error_mark_node;
2599 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2600 TREE_OPERAND (arg, 1), 1);
2603 /* Anything not already handled and not a true memory reference
2604 or a non-lvalue array is an error. */
2605 else if (typecode != FUNCTION_TYPE && !flag
2606 && !lvalue_or_else (arg, "invalid lvalue in unary %<&%>"))
2607 return error_mark_node;
2609 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2610 argtype = TREE_TYPE (arg);
2612 /* If the lvalue is const or volatile, merge that into the type
2613 to which the address will point. Note that you can't get a
2614 restricted pointer by taking the address of something, so we
2615 only have to deal with `const' and `volatile' here. */
2616 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2617 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2618 argtype = c_build_type_variant (argtype,
2619 TREE_READONLY (arg),
2620 TREE_THIS_VOLATILE (arg));
2622 if (!c_mark_addressable (arg))
2623 return error_mark_node;
2625 if (TREE_CODE (arg) == COMPONENT_REF
2626 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
2628 error ("attempt to take address of bit-field structure member %qD",
2629 TREE_OPERAND (arg, 1));
2630 return error_mark_node;
2633 argtype = build_pointer_type (argtype);
2635 /* ??? Cope with user tricks that amount to offsetof. Delete this
2636 when we have proper support for integer constant expressions. */
2637 val = get_base_address (arg);
2638 if (val && TREE_CODE (val) == INDIRECT_REF
2639 && integer_zerop (TREE_OPERAND (val, 0)))
2640 return fold_convert (argtype, fold_offsetof (arg));
2642 val = build1 (ADDR_EXPR, argtype, arg);
2644 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2645 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2647 return val;
2649 default:
2650 break;
2653 if (argtype == 0)
2654 argtype = TREE_TYPE (arg);
2655 val = build1 (code, argtype, arg);
2656 return require_constant_value ? fold_initializer (val) : fold (val);
2659 /* Return nonzero if REF is an lvalue valid for this language.
2660 Lvalues can be assigned, unless their type has TYPE_READONLY.
2661 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2664 lvalue_p (tree ref)
2666 enum tree_code code = TREE_CODE (ref);
2668 switch (code)
2670 case REALPART_EXPR:
2671 case IMAGPART_EXPR:
2672 case COMPONENT_REF:
2673 return lvalue_p (TREE_OPERAND (ref, 0));
2675 case COMPOUND_LITERAL_EXPR:
2676 case STRING_CST:
2677 return 1;
2679 case INDIRECT_REF:
2680 case ARRAY_REF:
2681 case VAR_DECL:
2682 case PARM_DECL:
2683 case RESULT_DECL:
2684 case ERROR_MARK:
2685 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2686 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2688 case BIND_EXPR:
2689 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2691 default:
2692 return 0;
2696 /* Return nonzero if REF is an lvalue valid for this language;
2697 otherwise, print an error message and return zero. */
2699 static int
2700 lvalue_or_else (tree ref, const char *msgid)
2702 int win = lvalue_p (ref);
2704 if (! win)
2705 error ("%s", msgid);
2707 return win;
2711 /* Warn about storing in something that is `const'. */
2713 void
2714 readonly_error (tree arg, const char *msgid)
2716 if (TREE_CODE (arg) == COMPONENT_REF)
2718 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2719 readonly_error (TREE_OPERAND (arg, 0), msgid);
2720 else
2721 error ("%s of read-only member %qs", _(msgid),
2722 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2724 else if (TREE_CODE (arg) == VAR_DECL)
2725 error ("%s of read-only variable %qs", _(msgid),
2726 IDENTIFIER_POINTER (DECL_NAME (arg)));
2727 else
2728 error ("%s of read-only location", _(msgid));
2731 /* Mark EXP saying that we need to be able to take the
2732 address of it; it should not be allocated in a register.
2733 Returns true if successful. */
2735 bool
2736 c_mark_addressable (tree exp)
2738 tree x = exp;
2740 while (1)
2741 switch (TREE_CODE (x))
2743 case COMPONENT_REF:
2744 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2746 error ("cannot take address of bit-field %qs",
2747 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2748 return false;
2751 /* ... fall through ... */
2753 case ADDR_EXPR:
2754 case ARRAY_REF:
2755 case REALPART_EXPR:
2756 case IMAGPART_EXPR:
2757 x = TREE_OPERAND (x, 0);
2758 break;
2760 case COMPOUND_LITERAL_EXPR:
2761 case CONSTRUCTOR:
2762 TREE_ADDRESSABLE (x) = 1;
2763 return true;
2765 case VAR_DECL:
2766 case CONST_DECL:
2767 case PARM_DECL:
2768 case RESULT_DECL:
2769 if (C_DECL_REGISTER (x)
2770 && DECL_NONLOCAL (x))
2772 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2774 error ("global register variable %qs used in nested function",
2775 IDENTIFIER_POINTER (DECL_NAME (x)));
2776 return false;
2778 pedwarn ("register variable %qs used in nested function",
2779 IDENTIFIER_POINTER (DECL_NAME (x)));
2781 else if (C_DECL_REGISTER (x))
2783 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2785 error ("address of global register variable %qs requested",
2786 IDENTIFIER_POINTER (DECL_NAME (x)));
2787 return false;
2790 pedwarn ("address of register variable %qs requested",
2791 IDENTIFIER_POINTER (DECL_NAME (x)));
2794 /* drops in */
2795 case FUNCTION_DECL:
2796 TREE_ADDRESSABLE (x) = 1;
2797 /* drops out */
2798 default:
2799 return true;
2803 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2805 tree
2806 build_conditional_expr (tree ifexp, tree op1, tree op2)
2808 tree type1;
2809 tree type2;
2810 enum tree_code code1;
2811 enum tree_code code2;
2812 tree result_type = NULL;
2813 tree orig_op1 = op1, orig_op2 = op2;
2815 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2817 /* Promote both alternatives. */
2819 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2820 op1 = default_conversion (op1);
2821 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2822 op2 = default_conversion (op2);
2824 if (TREE_CODE (ifexp) == ERROR_MARK
2825 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2826 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2827 return error_mark_node;
2829 type1 = TREE_TYPE (op1);
2830 code1 = TREE_CODE (type1);
2831 type2 = TREE_TYPE (op2);
2832 code2 = TREE_CODE (type2);
2834 /* C90 does not permit non-lvalue arrays in conditional expressions.
2835 In C99 they will be pointers by now. */
2836 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2838 error ("non-lvalue array in conditional expression");
2839 return error_mark_node;
2842 /* Quickly detect the usual case where op1 and op2 have the same type
2843 after promotion. */
2844 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2846 if (type1 == type2)
2847 result_type = type1;
2848 else
2849 result_type = TYPE_MAIN_VARIANT (type1);
2851 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2852 || code1 == COMPLEX_TYPE)
2853 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2854 || code2 == COMPLEX_TYPE))
2856 result_type = common_type (type1, type2);
2858 /* If -Wsign-compare, warn here if type1 and type2 have
2859 different signedness. We'll promote the signed to unsigned
2860 and later code won't know it used to be different.
2861 Do this check on the original types, so that explicit casts
2862 will be considered, but default promotions won't. */
2863 if (warn_sign_compare && !skip_evaluation)
2865 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2866 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2868 if (unsigned_op1 ^ unsigned_op2)
2870 /* Do not warn if the result type is signed, since the
2871 signed type will only be chosen if it can represent
2872 all the values of the unsigned type. */
2873 if (! TYPE_UNSIGNED (result_type))
2874 /* OK */;
2875 /* Do not warn if the signed quantity is an unsuffixed
2876 integer literal (or some static constant expression
2877 involving such literals) and it is non-negative. */
2878 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2879 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2880 /* OK */;
2881 else
2882 warning ("signed and unsigned type in conditional expression");
2886 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2888 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2889 pedwarn ("ISO C forbids conditional expr with only one void side");
2890 result_type = void_type_node;
2892 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2894 if (comp_target_types (type1, type2, 1))
2895 result_type = common_pointer_type (type1, type2);
2896 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2897 && TREE_CODE (orig_op1) != NOP_EXPR)
2898 result_type = qualify_type (type2, type1);
2899 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2900 && TREE_CODE (orig_op2) != NOP_EXPR)
2901 result_type = qualify_type (type1, type2);
2902 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2904 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2905 pedwarn ("ISO C forbids conditional expr between "
2906 "%<void *%> and function pointer");
2907 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2908 TREE_TYPE (type2)));
2910 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2912 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2913 pedwarn ("ISO C forbids conditional expr between "
2914 "%<void *%> and function pointer");
2915 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2916 TREE_TYPE (type1)));
2918 else
2920 pedwarn ("pointer type mismatch in conditional expression");
2921 result_type = build_pointer_type (void_type_node);
2924 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2926 if (! integer_zerop (op2))
2927 pedwarn ("pointer/integer type mismatch in conditional expression");
2928 else
2930 op2 = null_pointer_node;
2932 result_type = type1;
2934 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2936 if (!integer_zerop (op1))
2937 pedwarn ("pointer/integer type mismatch in conditional expression");
2938 else
2940 op1 = null_pointer_node;
2942 result_type = type2;
2945 if (!result_type)
2947 if (flag_cond_mismatch)
2948 result_type = void_type_node;
2949 else
2951 error ("type mismatch in conditional expression");
2952 return error_mark_node;
2956 /* Merge const and volatile flags of the incoming types. */
2957 result_type
2958 = build_type_variant (result_type,
2959 TREE_READONLY (op1) || TREE_READONLY (op2),
2960 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2962 if (result_type != TREE_TYPE (op1))
2963 op1 = convert_and_check (result_type, op1);
2964 if (result_type != TREE_TYPE (op2))
2965 op2 = convert_and_check (result_type, op2);
2967 if (TREE_CODE (ifexp) == INTEGER_CST)
2968 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2970 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
2973 /* Return a compound expression that performs two expressions and
2974 returns the value of the second of them. */
2976 tree
2977 build_compound_expr (tree expr1, tree expr2)
2979 /* Convert arrays and functions to pointers. */
2980 expr2 = default_function_array_conversion (expr2);
2982 /* Don't let (0, 0) be null pointer constant. */
2983 if (integer_zerop (expr2))
2984 expr2 = non_lvalue (expr2);
2986 if (! TREE_SIDE_EFFECTS (expr1))
2988 /* The left-hand operand of a comma expression is like an expression
2989 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2990 any side-effects, unless it was explicitly cast to (void). */
2991 if (warn_unused_value
2992 && ! (TREE_CODE (expr1) == CONVERT_EXPR
2993 && VOID_TYPE_P (TREE_TYPE (expr1))))
2994 warning ("left-hand operand of comma expression has no effect");
2997 /* With -Wunused, we should also warn if the left-hand operand does have
2998 side-effects, but computes a value which is not used. For example, in
2999 `foo() + bar(), baz()' the result of the `+' operator is not used,
3000 so we should issue a warning. */
3001 else if (warn_unused_value)
3002 warn_if_unused_value (expr1, input_location);
3004 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3007 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3009 tree
3010 build_c_cast (tree type, tree expr)
3012 tree value = expr;
3014 if (type == error_mark_node || expr == error_mark_node)
3015 return error_mark_node;
3017 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3018 only in <protocol> qualifications. But when constructing cast expressions,
3019 the protocols do matter and must be kept around. */
3020 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3021 return build1 (NOP_EXPR, type, expr);
3023 type = TYPE_MAIN_VARIANT (type);
3025 if (TREE_CODE (type) == ARRAY_TYPE)
3027 error ("cast specifies array type");
3028 return error_mark_node;
3031 if (TREE_CODE (type) == FUNCTION_TYPE)
3033 error ("cast specifies function type");
3034 return error_mark_node;
3037 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3039 if (pedantic)
3041 if (TREE_CODE (type) == RECORD_TYPE
3042 || TREE_CODE (type) == UNION_TYPE)
3043 pedwarn ("ISO C forbids casting nonscalar to the same type");
3046 else if (TREE_CODE (type) == UNION_TYPE)
3048 tree field;
3049 value = default_function_array_conversion (value);
3051 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3052 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3053 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3054 break;
3056 if (field)
3058 tree t;
3060 if (pedantic)
3061 pedwarn ("ISO C forbids casts to union type");
3062 t = digest_init (type,
3063 build_constructor (type,
3064 build_tree_list (field, value)),
3065 true, 0);
3066 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3067 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3068 return t;
3070 error ("cast to union type from type not present in union");
3071 return error_mark_node;
3073 else
3075 tree otype, ovalue;
3077 /* If casting to void, avoid the error that would come
3078 from default_conversion in the case of a non-lvalue array. */
3079 if (type == void_type_node)
3080 return build1 (CONVERT_EXPR, type, value);
3082 /* Convert functions and arrays to pointers,
3083 but don't convert any other types. */
3084 value = default_function_array_conversion (value);
3085 otype = TREE_TYPE (value);
3087 /* Optionally warn about potentially worrisome casts. */
3089 if (warn_cast_qual
3090 && TREE_CODE (type) == POINTER_TYPE
3091 && TREE_CODE (otype) == POINTER_TYPE)
3093 tree in_type = type;
3094 tree in_otype = otype;
3095 int added = 0;
3096 int discarded = 0;
3098 /* Check that the qualifiers on IN_TYPE are a superset of
3099 the qualifiers of IN_OTYPE. The outermost level of
3100 POINTER_TYPE nodes is uninteresting and we stop as soon
3101 as we hit a non-POINTER_TYPE node on either type. */
3104 in_otype = TREE_TYPE (in_otype);
3105 in_type = TREE_TYPE (in_type);
3107 /* GNU C allows cv-qualified function types. 'const'
3108 means the function is very pure, 'volatile' means it
3109 can't return. We need to warn when such qualifiers
3110 are added, not when they're taken away. */
3111 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3112 && TREE_CODE (in_type) == FUNCTION_TYPE)
3113 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3114 else
3115 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3117 while (TREE_CODE (in_type) == POINTER_TYPE
3118 && TREE_CODE (in_otype) == POINTER_TYPE);
3120 if (added)
3121 warning ("cast adds new qualifiers to function type");
3123 if (discarded)
3124 /* There are qualifiers present in IN_OTYPE that are not
3125 present in IN_TYPE. */
3126 warning ("cast discards qualifiers from pointer target type");
3129 /* Warn about possible alignment problems. */
3130 if (STRICT_ALIGNMENT && warn_cast_align
3131 && TREE_CODE (type) == POINTER_TYPE
3132 && TREE_CODE (otype) == POINTER_TYPE
3133 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3134 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3135 /* Don't warn about opaque types, where the actual alignment
3136 restriction is unknown. */
3137 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3138 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3139 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3140 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3141 warning ("cast increases required alignment of target type");
3143 if (TREE_CODE (type) == INTEGER_TYPE
3144 && TREE_CODE (otype) == POINTER_TYPE
3145 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3146 && !TREE_CONSTANT (value))
3147 warning ("cast from pointer to integer of different size");
3149 if (warn_bad_function_cast
3150 && TREE_CODE (value) == CALL_EXPR
3151 && TREE_CODE (type) != TREE_CODE (otype))
3152 warning ("cast does not match function type");
3154 if (TREE_CODE (type) == POINTER_TYPE
3155 && TREE_CODE (otype) == INTEGER_TYPE
3156 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3157 /* Don't warn about converting any constant. */
3158 && !TREE_CONSTANT (value))
3159 warning ("cast to pointer from integer of different size");
3161 if (TREE_CODE (type) == POINTER_TYPE
3162 && TREE_CODE (otype) == POINTER_TYPE
3163 && TREE_CODE (expr) == ADDR_EXPR
3164 && DECL_P (TREE_OPERAND (expr, 0))
3165 && flag_strict_aliasing && warn_strict_aliasing
3166 && !VOID_TYPE_P (TREE_TYPE (type)))
3168 /* Casting the address of a decl to non void pointer. Warn
3169 if the cast breaks type based aliasing. */
3170 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3171 warning ("type-punning to incomplete type might break strict-aliasing rules");
3172 else
3174 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3175 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3177 if (!alias_sets_conflict_p (set1, set2))
3178 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3179 else if (warn_strict_aliasing > 1
3180 && !alias_sets_might_conflict_p (set1, set2))
3181 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3185 /* If pedantic, warn for conversions between function and object
3186 pointer types, except for converting a null pointer constant
3187 to function pointer type. */
3188 if (pedantic
3189 && TREE_CODE (type) == POINTER_TYPE
3190 && TREE_CODE (otype) == POINTER_TYPE
3191 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3192 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3193 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3195 if (pedantic
3196 && TREE_CODE (type) == POINTER_TYPE
3197 && TREE_CODE (otype) == POINTER_TYPE
3198 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3199 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3200 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3201 && TREE_CODE (expr) != NOP_EXPR))
3202 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3204 ovalue = value;
3205 /* Replace a nonvolatile const static variable with its value. */
3206 if (optimize && TREE_CODE (value) == VAR_DECL)
3207 value = decl_constant_value (value);
3208 value = convert (type, value);
3210 /* Ignore any integer overflow caused by the cast. */
3211 if (TREE_CODE (value) == INTEGER_CST)
3213 if (EXPR_P (ovalue))
3214 /* If OVALUE had overflow set, then so will VALUE, so it
3215 is safe to overwrite. */
3216 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3217 else
3218 TREE_OVERFLOW (value) = 0;
3220 if (CONSTANT_CLASS_P (ovalue))
3221 /* Similarly, constant_overflow cannot have become
3222 cleared. */
3223 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3227 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3228 if (TREE_CODE (value) == INTEGER_CST
3229 && TREE_CODE (expr) == INTEGER_CST
3230 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3231 value = non_lvalue (value);
3233 /* Don't let a cast be an lvalue. */
3234 if (value == expr)
3235 value = non_lvalue (value);
3237 return value;
3240 /* Interpret a cast of expression EXPR to type TYPE. */
3241 tree
3242 c_cast_expr (struct c_type_name *type_name, tree expr)
3244 tree type;
3245 int saved_wsp = warn_strict_prototypes;
3247 /* This avoids warnings about unprototyped casts on
3248 integers. E.g. "#define SIG_DFL (void(*)())0". */
3249 if (TREE_CODE (expr) == INTEGER_CST)
3250 warn_strict_prototypes = 0;
3251 type = groktypename (type_name);
3252 warn_strict_prototypes = saved_wsp;
3254 return build_c_cast (type, expr);
3258 /* Build an assignment expression of lvalue LHS from value RHS.
3259 MODIFYCODE is the code for a binary operator that we use
3260 to combine the old value of LHS with RHS to get the new value.
3261 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3263 tree
3264 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3266 tree result;
3267 tree newrhs;
3268 tree lhstype = TREE_TYPE (lhs);
3269 tree olhstype = lhstype;
3271 /* Types that aren't fully specified cannot be used in assignments. */
3272 lhs = require_complete_type (lhs);
3274 /* Avoid duplicate error messages from operands that had errors. */
3275 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3276 return error_mark_node;
3278 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3279 /* Do not use STRIP_NOPS here. We do not want an enumerator
3280 whose value is 0 to count as a null pointer constant. */
3281 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3282 rhs = TREE_OPERAND (rhs, 0);
3284 newrhs = rhs;
3286 /* If a binary op has been requested, combine the old LHS value with the RHS
3287 producing the value we should actually store into the LHS. */
3289 if (modifycode != NOP_EXPR)
3291 lhs = stabilize_reference (lhs);
3292 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3295 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3296 return error_mark_node;
3298 /* Warn about storing in something that is `const'. */
3300 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3301 || ((TREE_CODE (lhstype) == RECORD_TYPE
3302 || TREE_CODE (lhstype) == UNION_TYPE)
3303 && C_TYPE_FIELDS_READONLY (lhstype)))
3304 readonly_error (lhs, "assignment");
3306 /* If storing into a structure or union member,
3307 it has probably been given type `int'.
3308 Compute the type that would go with
3309 the actual amount of storage the member occupies. */
3311 if (TREE_CODE (lhs) == COMPONENT_REF
3312 && (TREE_CODE (lhstype) == INTEGER_TYPE
3313 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3314 || TREE_CODE (lhstype) == REAL_TYPE
3315 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3316 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3318 /* If storing in a field that is in actuality a short or narrower than one,
3319 we must store in the field in its actual type. */
3321 if (lhstype != TREE_TYPE (lhs))
3323 lhs = copy_node (lhs);
3324 TREE_TYPE (lhs) = lhstype;
3327 /* Convert new value to destination type. */
3329 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3330 NULL_TREE, NULL_TREE, 0);
3331 if (TREE_CODE (newrhs) == ERROR_MARK)
3332 return error_mark_node;
3334 /* Scan operands */
3336 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3337 TREE_SIDE_EFFECTS (result) = 1;
3339 /* If we got the LHS in a different type for storing in,
3340 convert the result back to the nominal type of LHS
3341 so that the value we return always has the same type
3342 as the LHS argument. */
3344 if (olhstype == TREE_TYPE (result))
3345 return result;
3346 return convert_for_assignment (olhstype, result, _("assignment"),
3347 NULL_TREE, NULL_TREE, 0);
3350 /* Convert value RHS to type TYPE as preparation for an assignment
3351 to an lvalue of type TYPE.
3352 The real work of conversion is done by `convert'.
3353 The purpose of this function is to generate error messages
3354 for assignments that are not allowed in C.
3355 ERRTYPE is a string to use in error messages:
3356 "assignment", "return", etc. If it is null, this is parameter passing
3357 for a function call (and different error messages are output).
3359 FUNNAME is the name of the function being called,
3360 as an IDENTIFIER_NODE, or null.
3361 PARMNUM is the number of the argument, for printing in error messages. */
3363 static tree
3364 convert_for_assignment (tree type, tree rhs, const char *errtype,
3365 tree fundecl, tree funname, int parmnum)
3367 enum tree_code codel = TREE_CODE (type);
3368 tree rhstype;
3369 enum tree_code coder;
3371 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3372 /* Do not use STRIP_NOPS here. We do not want an enumerator
3373 whose value is 0 to count as a null pointer constant. */
3374 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3375 rhs = TREE_OPERAND (rhs, 0);
3377 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3378 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3379 rhs = default_conversion (rhs);
3380 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3381 rhs = decl_constant_value_for_broken_optimization (rhs);
3383 rhstype = TREE_TYPE (rhs);
3384 coder = TREE_CODE (rhstype);
3386 if (coder == ERROR_MARK)
3387 return error_mark_node;
3389 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3391 overflow_warning (rhs);
3392 /* Check for Objective-C protocols. This will automatically
3393 issue a warning if there are protocol violations. No need to
3394 use the return value. */
3395 if (c_dialect_objc ())
3396 objc_comptypes (type, rhstype, 0);
3397 return rhs;
3400 if (coder == VOID_TYPE)
3402 error ("void value not ignored as it ought to be");
3403 return error_mark_node;
3405 /* A type converts to a reference to it.
3406 This code doesn't fully support references, it's just for the
3407 special case of va_start and va_copy. */
3408 if (codel == REFERENCE_TYPE
3409 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3411 if (!lvalue_p (rhs))
3413 error ("cannot pass rvalue to reference parameter");
3414 return error_mark_node;
3416 if (!c_mark_addressable (rhs))
3417 return error_mark_node;
3418 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3420 /* We already know that these two types are compatible, but they
3421 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3422 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3423 likely to be va_list, a typedef to __builtin_va_list, which
3424 is different enough that it will cause problems later. */
3425 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3426 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3428 rhs = build1 (NOP_EXPR, type, rhs);
3429 return rhs;
3431 /* Some types can interconvert without explicit casts. */
3432 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3433 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3434 return convert (type, rhs);
3435 /* Arithmetic types all interconvert, and enum is treated like int. */
3436 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3437 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3438 || codel == BOOLEAN_TYPE)
3439 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3440 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3441 || coder == BOOLEAN_TYPE))
3442 return convert_and_check (type, rhs);
3444 /* Conversion to a transparent union from its member types.
3445 This applies only to function arguments. */
3446 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3448 tree memb_types;
3449 tree marginal_memb_type = 0;
3451 for (memb_types = TYPE_FIELDS (type); memb_types;
3452 memb_types = TREE_CHAIN (memb_types))
3454 tree memb_type = TREE_TYPE (memb_types);
3456 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3457 TYPE_MAIN_VARIANT (rhstype)))
3458 break;
3460 if (TREE_CODE (memb_type) != POINTER_TYPE)
3461 continue;
3463 if (coder == POINTER_TYPE)
3465 tree ttl = TREE_TYPE (memb_type);
3466 tree ttr = TREE_TYPE (rhstype);
3468 /* Any non-function converts to a [const][volatile] void *
3469 and vice versa; otherwise, targets must be the same.
3470 Meanwhile, the lhs target must have all the qualifiers of
3471 the rhs. */
3472 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3473 || comp_target_types (memb_type, rhstype, 0))
3475 /* If this type won't generate any warnings, use it. */
3476 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3477 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3478 && TREE_CODE (ttl) == FUNCTION_TYPE)
3479 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3480 == TYPE_QUALS (ttr))
3481 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3482 == TYPE_QUALS (ttl))))
3483 break;
3485 /* Keep looking for a better type, but remember this one. */
3486 if (! marginal_memb_type)
3487 marginal_memb_type = memb_type;
3491 /* Can convert integer zero to any pointer type. */
3492 if (integer_zerop (rhs)
3493 || (TREE_CODE (rhs) == NOP_EXPR
3494 && integer_zerop (TREE_OPERAND (rhs, 0))))
3496 rhs = null_pointer_node;
3497 break;
3501 if (memb_types || marginal_memb_type)
3503 if (! memb_types)
3505 /* We have only a marginally acceptable member type;
3506 it needs a warning. */
3507 tree ttl = TREE_TYPE (marginal_memb_type);
3508 tree ttr = TREE_TYPE (rhstype);
3510 /* Const and volatile mean something different for function
3511 types, so the usual warnings are not appropriate. */
3512 if (TREE_CODE (ttr) == FUNCTION_TYPE
3513 && TREE_CODE (ttl) == FUNCTION_TYPE)
3515 /* Because const and volatile on functions are
3516 restrictions that say the function will not do
3517 certain things, it is okay to use a const or volatile
3518 function where an ordinary one is wanted, but not
3519 vice-versa. */
3520 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3521 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3522 errtype, funname, parmnum);
3524 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3525 warn_for_assignment ("%s discards qualifiers from pointer target type",
3526 errtype, funname,
3527 parmnum);
3530 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3531 pedwarn ("ISO C prohibits argument conversion to union type");
3533 return build1 (NOP_EXPR, type, rhs);
3537 /* Conversions among pointers */
3538 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3539 && (coder == codel))
3541 tree ttl = TREE_TYPE (type);
3542 tree ttr = TREE_TYPE (rhstype);
3543 bool is_opaque_pointer;
3544 int target_cmp = 0; /* Cache comp_target_types () result. */
3546 /* Opaque pointers are treated like void pointers. */
3547 is_opaque_pointer = (targetm.vector_opaque_p (type)
3548 || targetm.vector_opaque_p (rhstype))
3549 && TREE_CODE (ttl) == VECTOR_TYPE
3550 && TREE_CODE (ttr) == VECTOR_TYPE;
3552 /* Any non-function converts to a [const][volatile] void *
3553 and vice versa; otherwise, targets must be the same.
3554 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3555 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3556 || (target_cmp = comp_target_types (type, rhstype, 0))
3557 || is_opaque_pointer
3558 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3559 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3561 if (pedantic
3562 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3564 (VOID_TYPE_P (ttr)
3565 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3566 which are not ANSI null ptr constants. */
3567 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3568 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3569 warn_for_assignment ("ISO C forbids %s between function "
3570 "pointer and %<void *%>",
3571 errtype, funname, parmnum);
3572 /* Const and volatile mean something different for function types,
3573 so the usual warnings are not appropriate. */
3574 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3575 && TREE_CODE (ttl) != FUNCTION_TYPE)
3577 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3578 warn_for_assignment ("%s discards qualifiers from pointer target type",
3579 errtype, funname, parmnum);
3580 /* If this is not a case of ignoring a mismatch in signedness,
3581 no warning. */
3582 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3583 || target_cmp)
3585 /* If there is a mismatch, do warn. */
3586 else
3587 warn_for_assignment ("pointer targets in %s differ in signedness",
3588 errtype, funname, parmnum);
3590 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3591 && TREE_CODE (ttr) == FUNCTION_TYPE)
3593 /* Because const and volatile on functions are restrictions
3594 that say the function will not do certain things,
3595 it is okay to use a const or volatile function
3596 where an ordinary one is wanted, but not vice-versa. */
3597 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3598 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3599 errtype, funname, parmnum);
3602 else
3603 warn_for_assignment ("%s from incompatible pointer type",
3604 errtype, funname, parmnum);
3605 return convert (type, rhs);
3607 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3609 error ("invalid use of non-lvalue array");
3610 return error_mark_node;
3612 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3614 /* An explicit constant 0 can convert to a pointer,
3615 or one that results from arithmetic, even including
3616 a cast to integer type. */
3617 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3619 ! (TREE_CODE (rhs) == NOP_EXPR
3620 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3621 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3622 && integer_zerop (TREE_OPERAND (rhs, 0))))
3623 warn_for_assignment ("%s makes pointer from integer without a cast",
3624 errtype, funname, parmnum);
3626 return convert (type, rhs);
3628 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3630 warn_for_assignment ("%s makes integer from pointer without a cast",
3631 errtype, funname, parmnum);
3632 return convert (type, rhs);
3634 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3635 return convert (type, rhs);
3637 if (!errtype)
3639 if (funname)
3641 tree selector = objc_message_selector ();
3643 if (selector && parmnum > 2)
3644 error ("incompatible type for argument %d of %qs",
3645 parmnum - 2, IDENTIFIER_POINTER (selector));
3646 else
3647 error ("incompatible type for argument %d of %qs",
3648 parmnum, IDENTIFIER_POINTER (funname));
3650 else
3651 error ("incompatible type for argument %d of indirect function call",
3652 parmnum);
3654 else
3655 error ("incompatible types in %s", errtype);
3657 return error_mark_node;
3660 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3661 is used for error and waring reporting and indicates which argument
3662 is being processed. */
3664 tree
3665 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3667 tree ret, type;
3669 /* If FN was prototyped, the value has been converted already
3670 in convert_arguments. */
3671 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3672 return value;
3674 type = TREE_TYPE (parm);
3675 ret = convert_for_assignment (type, value,
3676 (char *) 0 /* arg passing */, fn,
3677 DECL_NAME (fn), argnum);
3678 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3679 && INTEGRAL_TYPE_P (type)
3680 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3681 ret = default_conversion (ret);
3682 return ret;
3685 /* Print a warning using MSGID.
3686 It gets OPNAME as its one parameter.
3687 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3688 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3689 FUNCTION and ARGNUM are handled specially if we are building an
3690 Objective-C selector. */
3692 static void
3693 warn_for_assignment (const char *msgid, const char *opname, tree function,
3694 int argnum)
3696 if (opname == 0)
3698 tree selector = objc_message_selector ();
3699 char * new_opname;
3701 if (selector && argnum > 2)
3703 function = selector;
3704 argnum -= 2;
3706 if (argnum == 0)
3708 if (function)
3710 /* Function name is known; supply it. */
3711 const char *const argstring = _("passing arg of '%s'");
3712 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3713 + strlen (argstring) + 1 + 1);
3714 sprintf (new_opname, argstring,
3715 IDENTIFIER_POINTER (function));
3717 else
3719 /* Function name unknown (call through ptr). */
3720 const char *const argnofun = _("passing arg of pointer to function");
3721 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
3722 sprintf (new_opname, argnofun);
3725 else if (function)
3727 /* Function name is known; supply it. */
3728 const char *const argstring = _("passing arg %d of '%s'");
3729 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3730 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3731 sprintf (new_opname, argstring, argnum,
3732 IDENTIFIER_POINTER (function));
3734 else
3736 /* Function name unknown (call through ptr); just give arg number. */
3737 const char *const argnofun = _("passing arg %d of pointer to function");
3738 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3739 sprintf (new_opname, argnofun, argnum);
3741 opname = new_opname;
3743 pedwarn (msgid, opname);
3746 /* If VALUE is a compound expr all of whose expressions are constant, then
3747 return its value. Otherwise, return error_mark_node.
3749 This is for handling COMPOUND_EXPRs as initializer elements
3750 which is allowed with a warning when -pedantic is specified. */
3752 static tree
3753 valid_compound_expr_initializer (tree value, tree endtype)
3755 if (TREE_CODE (value) == COMPOUND_EXPR)
3757 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3758 == error_mark_node)
3759 return error_mark_node;
3760 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3761 endtype);
3763 else if (!initializer_constant_valid_p (value, endtype))
3764 return error_mark_node;
3765 else
3766 return value;
3769 /* Perform appropriate conversions on the initial value of a variable,
3770 store it in the declaration DECL,
3771 and print any error messages that are appropriate.
3772 If the init is invalid, store an ERROR_MARK. */
3774 void
3775 store_init_value (tree decl, tree init)
3777 tree value, type;
3779 /* If variable's type was invalidly declared, just ignore it. */
3781 type = TREE_TYPE (decl);
3782 if (TREE_CODE (type) == ERROR_MARK)
3783 return;
3785 /* Digest the specified initializer into an expression. */
3787 value = digest_init (type, init, true, TREE_STATIC (decl));
3789 /* Store the expression if valid; else report error. */
3791 if (warn_traditional && !in_system_header
3792 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3793 warning ("traditional C rejects automatic aggregate initialization");
3795 DECL_INITIAL (decl) = value;
3797 /* ANSI wants warnings about out-of-range constant initializers. */
3798 STRIP_TYPE_NOPS (value);
3799 constant_expression_warning (value);
3801 /* Check if we need to set array size from compound literal size. */
3802 if (TREE_CODE (type) == ARRAY_TYPE
3803 && TYPE_DOMAIN (type) == 0
3804 && value != error_mark_node)
3806 tree inside_init = init;
3808 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3809 inside_init = TREE_OPERAND (init, 0);
3810 inside_init = fold (inside_init);
3812 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3814 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3816 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3818 /* For int foo[] = (int [3]){1}; we need to set array size
3819 now since later on array initializer will be just the
3820 brace enclosed list of the compound literal. */
3821 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3822 layout_type (type);
3823 layout_decl (decl, 0);
3829 /* Methods for storing and printing names for error messages. */
3831 /* Implement a spelling stack that allows components of a name to be pushed
3832 and popped. Each element on the stack is this structure. */
3834 struct spelling
3836 int kind;
3837 union
3839 int i;
3840 const char *s;
3841 } u;
3844 #define SPELLING_STRING 1
3845 #define SPELLING_MEMBER 2
3846 #define SPELLING_BOUNDS 3
3848 static struct spelling *spelling; /* Next stack element (unused). */
3849 static struct spelling *spelling_base; /* Spelling stack base. */
3850 static int spelling_size; /* Size of the spelling stack. */
3852 /* Macros to save and restore the spelling stack around push_... functions.
3853 Alternative to SAVE_SPELLING_STACK. */
3855 #define SPELLING_DEPTH() (spelling - spelling_base)
3856 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3858 /* Push an element on the spelling stack with type KIND and assign VALUE
3859 to MEMBER. */
3861 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3863 int depth = SPELLING_DEPTH (); \
3865 if (depth >= spelling_size) \
3867 spelling_size += 10; \
3868 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3869 spelling_size); \
3870 RESTORE_SPELLING_DEPTH (depth); \
3873 spelling->kind = (KIND); \
3874 spelling->MEMBER = (VALUE); \
3875 spelling++; \
3878 /* Push STRING on the stack. Printed literally. */
3880 static void
3881 push_string (const char *string)
3883 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3886 /* Push a member name on the stack. Printed as '.' STRING. */
3888 static void
3889 push_member_name (tree decl)
3891 const char *const string
3892 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3893 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3896 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3898 static void
3899 push_array_bounds (int bounds)
3901 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3904 /* Compute the maximum size in bytes of the printed spelling. */
3906 static int
3907 spelling_length (void)
3909 int size = 0;
3910 struct spelling *p;
3912 for (p = spelling_base; p < spelling; p++)
3914 if (p->kind == SPELLING_BOUNDS)
3915 size += 25;
3916 else
3917 size += strlen (p->u.s) + 1;
3920 return size;
3923 /* Print the spelling to BUFFER and return it. */
3925 static char *
3926 print_spelling (char *buffer)
3928 char *d = buffer;
3929 struct spelling *p;
3931 for (p = spelling_base; p < spelling; p++)
3932 if (p->kind == SPELLING_BOUNDS)
3934 sprintf (d, "[%d]", p->u.i);
3935 d += strlen (d);
3937 else
3939 const char *s;
3940 if (p->kind == SPELLING_MEMBER)
3941 *d++ = '.';
3942 for (s = p->u.s; (*d = *s++); d++)
3945 *d++ = '\0';
3946 return buffer;
3949 /* Issue an error message for a bad initializer component.
3950 MSGID identifies the message.
3951 The component name is taken from the spelling stack. */
3953 void
3954 error_init (const char *msgid)
3956 char *ofwhat;
3958 error ("%s", _(msgid));
3959 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3960 if (*ofwhat)
3961 error ("(near initialization for %qs)", ofwhat);
3964 /* Issue a pedantic warning for a bad initializer component.
3965 MSGID identifies the message.
3966 The component name is taken from the spelling stack. */
3968 void
3969 pedwarn_init (const char *msgid)
3971 char *ofwhat;
3973 pedwarn ("%s", _(msgid));
3974 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3975 if (*ofwhat)
3976 pedwarn ("(near initialization for %qs)", ofwhat);
3979 /* Issue a warning for a bad initializer component.
3980 MSGID identifies the message.
3981 The component name is taken from the spelling stack. */
3983 static void
3984 warning_init (const char *msgid)
3986 char *ofwhat;
3988 warning ("%s", _(msgid));
3989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3990 if (*ofwhat)
3991 warning ("(near initialization for %qs)", ofwhat);
3994 /* If TYPE is an array type and EXPR is a parenthesized string
3995 constant, warn if pedantic that EXPR is being used to initialize an
3996 object of type TYPE. */
3998 void
3999 maybe_warn_string_init (tree type, struct c_expr expr)
4001 if (pedantic
4002 && TREE_CODE (type) == ARRAY_TYPE
4003 && TREE_CODE (expr.value) == STRING_CST
4004 && expr.original_code != STRING_CST)
4005 pedwarn_init ("array initialized from parenthesized string constant");
4008 /* Digest the parser output INIT as an initializer for type TYPE.
4009 Return a C expression of type TYPE to represent the initial value.
4011 If INIT is a string constant, STRICT_STRING is true if it is
4012 unparenthesized or we should not warn here for it being parenthesized.
4013 For other types of INIT, STRICT_STRING is not used.
4015 REQUIRE_CONSTANT requests an error if non-constant initializers or
4016 elements are seen. */
4018 static tree
4019 digest_init (tree type, tree init, bool strict_string, int require_constant)
4021 enum tree_code code = TREE_CODE (type);
4022 tree inside_init = init;
4024 if (type == error_mark_node
4025 || init == error_mark_node
4026 || TREE_TYPE (init) == error_mark_node)
4027 return error_mark_node;
4029 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4030 /* Do not use STRIP_NOPS here. We do not want an enumerator
4031 whose value is 0 to count as a null pointer constant. */
4032 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4033 inside_init = TREE_OPERAND (init, 0);
4035 inside_init = fold (inside_init);
4037 /* Initialization of an array of chars from a string constant
4038 optionally enclosed in braces. */
4040 if (code == ARRAY_TYPE && inside_init
4041 && TREE_CODE (inside_init) == STRING_CST)
4043 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4044 /* Note that an array could be both an array of character type
4045 and an array of wchar_t if wchar_t is signed char or unsigned
4046 char. */
4047 bool char_array = (typ1 == char_type_node
4048 || typ1 == signed_char_type_node
4049 || typ1 == unsigned_char_type_node);
4050 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4051 if (char_array || wchar_array)
4053 struct c_expr expr;
4054 bool char_string;
4055 expr.value = inside_init;
4056 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4057 maybe_warn_string_init (type, expr);
4059 char_string
4060 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4061 == char_type_node);
4063 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4064 TYPE_MAIN_VARIANT (type)))
4065 return inside_init;
4067 if (!wchar_array && !char_string)
4069 error_init ("char-array initialized from wide string");
4070 return error_mark_node;
4072 if (char_string && !char_array)
4074 error_init ("wchar_t-array initialized from non-wide string");
4075 return error_mark_node;
4078 TREE_TYPE (inside_init) = type;
4079 if (TYPE_DOMAIN (type) != 0
4080 && TYPE_SIZE (type) != 0
4081 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4082 /* Subtract 1 (or sizeof (wchar_t))
4083 because it's ok to ignore the terminating null char
4084 that is counted in the length of the constant. */
4085 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4086 TREE_STRING_LENGTH (inside_init)
4087 - ((TYPE_PRECISION (typ1)
4088 != TYPE_PRECISION (char_type_node))
4089 ? (TYPE_PRECISION (wchar_type_node)
4090 / BITS_PER_UNIT)
4091 : 1)))
4092 pedwarn_init ("initializer-string for array of chars is too long");
4094 return inside_init;
4096 else if (INTEGRAL_TYPE_P (typ1))
4098 error_init ("array of inappropriate type initialized "
4099 "from string constant");
4100 return error_mark_node;
4104 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4105 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4106 below and handle as a constructor. */
4107 if (code == VECTOR_TYPE
4108 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4109 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4110 && TREE_CONSTANT (inside_init))
4112 if (TREE_CODE (inside_init) == VECTOR_CST
4113 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4114 TYPE_MAIN_VARIANT (type)))
4115 return inside_init;
4116 else
4117 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4120 /* Any type can be initialized
4121 from an expression of the same type, optionally with braces. */
4123 if (inside_init && TREE_TYPE (inside_init) != 0
4124 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4125 TYPE_MAIN_VARIANT (type))
4126 || (code == ARRAY_TYPE
4127 && comptypes (TREE_TYPE (inside_init), type))
4128 || (code == VECTOR_TYPE
4129 && comptypes (TREE_TYPE (inside_init), type))
4130 || (code == POINTER_TYPE
4131 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4132 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4133 TREE_TYPE (type)))
4134 || (code == POINTER_TYPE
4135 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4136 && comptypes (TREE_TYPE (inside_init),
4137 TREE_TYPE (type)))))
4139 if (code == POINTER_TYPE)
4141 inside_init = default_function_array_conversion (inside_init);
4143 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4145 error_init ("invalid use of non-lvalue array");
4146 return error_mark_node;
4150 if (code == VECTOR_TYPE)
4151 /* Although the types are compatible, we may require a
4152 conversion. */
4153 inside_init = convert (type, inside_init);
4155 if (require_constant && !flag_isoc99
4156 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4158 /* As an extension, allow initializing objects with static storage
4159 duration with compound literals (which are then treated just as
4160 the brace enclosed list they contain). */
4161 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4162 inside_init = DECL_INITIAL (decl);
4165 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4166 && TREE_CODE (inside_init) != CONSTRUCTOR)
4168 error_init ("array initialized from non-constant array expression");
4169 return error_mark_node;
4172 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4173 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4175 /* Compound expressions can only occur here if -pedantic or
4176 -pedantic-errors is specified. In the later case, we always want
4177 an error. In the former case, we simply want a warning. */
4178 if (require_constant && pedantic
4179 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4181 inside_init
4182 = valid_compound_expr_initializer (inside_init,
4183 TREE_TYPE (inside_init));
4184 if (inside_init == error_mark_node)
4185 error_init ("initializer element is not constant");
4186 else
4187 pedwarn_init ("initializer element is not constant");
4188 if (flag_pedantic_errors)
4189 inside_init = error_mark_node;
4191 else if (require_constant
4192 && !initializer_constant_valid_p (inside_init,
4193 TREE_TYPE (inside_init)))
4195 error_init ("initializer element is not constant");
4196 inside_init = error_mark_node;
4199 return inside_init;
4202 /* Handle scalar types, including conversions. */
4204 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4205 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4206 || code == VECTOR_TYPE)
4208 /* Note that convert_for_assignment calls default_conversion
4209 for arrays and functions. We must not call it in the
4210 case where inside_init is a null pointer constant. */
4211 inside_init
4212 = convert_for_assignment (type, init, _("initialization"),
4213 NULL_TREE, NULL_TREE, 0);
4215 /* Check to see if we have already given an error message. */
4216 if (inside_init == error_mark_node)
4218 else if (require_constant && ! TREE_CONSTANT (inside_init))
4220 error_init ("initializer element is not constant");
4221 inside_init = error_mark_node;
4223 else if (require_constant
4224 && !initializer_constant_valid_p (inside_init,
4225 TREE_TYPE (inside_init)))
4227 error_init ("initializer element is not computable at load time");
4228 inside_init = error_mark_node;
4231 return inside_init;
4234 /* Come here only for records and arrays. */
4236 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4238 error_init ("variable-sized object may not be initialized");
4239 return error_mark_node;
4242 error_init ("invalid initializer");
4243 return error_mark_node;
4246 /* Handle initializers that use braces. */
4248 /* Type of object we are accumulating a constructor for.
4249 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4250 static tree constructor_type;
4252 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4253 left to fill. */
4254 static tree constructor_fields;
4256 /* For an ARRAY_TYPE, this is the specified index
4257 at which to store the next element we get. */
4258 static tree constructor_index;
4260 /* For an ARRAY_TYPE, this is the maximum index. */
4261 static tree constructor_max_index;
4263 /* For a RECORD_TYPE, this is the first field not yet written out. */
4264 static tree constructor_unfilled_fields;
4266 /* For an ARRAY_TYPE, this is the index of the first element
4267 not yet written out. */
4268 static tree constructor_unfilled_index;
4270 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4271 This is so we can generate gaps between fields, when appropriate. */
4272 static tree constructor_bit_index;
4274 /* If we are saving up the elements rather than allocating them,
4275 this is the list of elements so far (in reverse order,
4276 most recent first). */
4277 static tree constructor_elements;
4279 /* 1 if constructor should be incrementally stored into a constructor chain,
4280 0 if all the elements should be kept in AVL tree. */
4281 static int constructor_incremental;
4283 /* 1 if so far this constructor's elements are all compile-time constants. */
4284 static int constructor_constant;
4286 /* 1 if so far this constructor's elements are all valid address constants. */
4287 static int constructor_simple;
4289 /* 1 if this constructor is erroneous so far. */
4290 static int constructor_erroneous;
4292 /* Structure for managing pending initializer elements, organized as an
4293 AVL tree. */
4295 struct init_node
4297 struct init_node *left, *right;
4298 struct init_node *parent;
4299 int balance;
4300 tree purpose;
4301 tree value;
4304 /* Tree of pending elements at this constructor level.
4305 These are elements encountered out of order
4306 which belong at places we haven't reached yet in actually
4307 writing the output.
4308 Will never hold tree nodes across GC runs. */
4309 static struct init_node *constructor_pending_elts;
4311 /* The SPELLING_DEPTH of this constructor. */
4312 static int constructor_depth;
4314 /* 0 if implicitly pushing constructor levels is allowed. */
4315 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4317 /* DECL node for which an initializer is being read.
4318 0 means we are reading a constructor expression
4319 such as (struct foo) {...}. */
4320 static tree constructor_decl;
4322 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4323 static const char *constructor_asmspec;
4325 /* Nonzero if this is an initializer for a top-level decl. */
4326 static int constructor_top_level;
4328 /* Nonzero if there were any member designators in this initializer. */
4329 static int constructor_designated;
4331 /* Nesting depth of designator list. */
4332 static int designator_depth;
4334 /* Nonzero if there were diagnosed errors in this designator list. */
4335 static int designator_errorneous;
4338 /* This stack has a level for each implicit or explicit level of
4339 structuring in the initializer, including the outermost one. It
4340 saves the values of most of the variables above. */
4342 struct constructor_range_stack;
4344 struct constructor_stack
4346 struct constructor_stack *next;
4347 tree type;
4348 tree fields;
4349 tree index;
4350 tree max_index;
4351 tree unfilled_index;
4352 tree unfilled_fields;
4353 tree bit_index;
4354 tree elements;
4355 struct init_node *pending_elts;
4356 int offset;
4357 int depth;
4358 /* If value nonzero, this value should replace the entire
4359 constructor at this level. */
4360 struct c_expr replacement_value;
4361 struct constructor_range_stack *range_stack;
4362 char constant;
4363 char simple;
4364 char implicit;
4365 char erroneous;
4366 char outer;
4367 char incremental;
4368 char designated;
4371 struct constructor_stack *constructor_stack;
4373 /* This stack represents designators from some range designator up to
4374 the last designator in the list. */
4376 struct constructor_range_stack
4378 struct constructor_range_stack *next, *prev;
4379 struct constructor_stack *stack;
4380 tree range_start;
4381 tree index;
4382 tree range_end;
4383 tree fields;
4386 struct constructor_range_stack *constructor_range_stack;
4388 /* This stack records separate initializers that are nested.
4389 Nested initializers can't happen in ANSI C, but GNU C allows them
4390 in cases like { ... (struct foo) { ... } ... }. */
4392 struct initializer_stack
4394 struct initializer_stack *next;
4395 tree decl;
4396 const char *asmspec;
4397 struct constructor_stack *constructor_stack;
4398 struct constructor_range_stack *constructor_range_stack;
4399 tree elements;
4400 struct spelling *spelling;
4401 struct spelling *spelling_base;
4402 int spelling_size;
4403 char top_level;
4404 char require_constant_value;
4405 char require_constant_elements;
4408 struct initializer_stack *initializer_stack;
4410 /* Prepare to parse and output the initializer for variable DECL. */
4412 void
4413 start_init (tree decl, tree asmspec_tree, int top_level)
4415 const char *locus;
4416 struct initializer_stack *p = XNEW (struct initializer_stack);
4417 const char *asmspec = 0;
4419 if (asmspec_tree)
4420 asmspec = TREE_STRING_POINTER (asmspec_tree);
4422 p->decl = constructor_decl;
4423 p->asmspec = constructor_asmspec;
4424 p->require_constant_value = require_constant_value;
4425 p->require_constant_elements = require_constant_elements;
4426 p->constructor_stack = constructor_stack;
4427 p->constructor_range_stack = constructor_range_stack;
4428 p->elements = constructor_elements;
4429 p->spelling = spelling;
4430 p->spelling_base = spelling_base;
4431 p->spelling_size = spelling_size;
4432 p->top_level = constructor_top_level;
4433 p->next = initializer_stack;
4434 initializer_stack = p;
4436 constructor_decl = decl;
4437 constructor_asmspec = asmspec;
4438 constructor_designated = 0;
4439 constructor_top_level = top_level;
4441 if (decl != 0)
4443 require_constant_value = TREE_STATIC (decl);
4444 require_constant_elements
4445 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4446 /* For a scalar, you can always use any value to initialize,
4447 even within braces. */
4448 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4449 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4450 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4451 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4452 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4454 else
4456 require_constant_value = 0;
4457 require_constant_elements = 0;
4458 locus = "(anonymous)";
4461 constructor_stack = 0;
4462 constructor_range_stack = 0;
4464 missing_braces_mentioned = 0;
4466 spelling_base = 0;
4467 spelling_size = 0;
4468 RESTORE_SPELLING_DEPTH (0);
4470 if (locus)
4471 push_string (locus);
4474 void
4475 finish_init (void)
4477 struct initializer_stack *p = initializer_stack;
4479 /* Free the whole constructor stack of this initializer. */
4480 while (constructor_stack)
4482 struct constructor_stack *q = constructor_stack;
4483 constructor_stack = q->next;
4484 free (q);
4487 gcc_assert (!constructor_range_stack);
4489 /* Pop back to the data of the outer initializer (if any). */
4490 free (spelling_base);
4492 constructor_decl = p->decl;
4493 constructor_asmspec = p->asmspec;
4494 require_constant_value = p->require_constant_value;
4495 require_constant_elements = p->require_constant_elements;
4496 constructor_stack = p->constructor_stack;
4497 constructor_range_stack = p->constructor_range_stack;
4498 constructor_elements = p->elements;
4499 spelling = p->spelling;
4500 spelling_base = p->spelling_base;
4501 spelling_size = p->spelling_size;
4502 constructor_top_level = p->top_level;
4503 initializer_stack = p->next;
4504 free (p);
4507 /* Call here when we see the initializer is surrounded by braces.
4508 This is instead of a call to push_init_level;
4509 it is matched by a call to pop_init_level.
4511 TYPE is the type to initialize, for a constructor expression.
4512 For an initializer for a decl, TYPE is zero. */
4514 void
4515 really_start_incremental_init (tree type)
4517 struct constructor_stack *p = XNEW (struct constructor_stack);
4519 if (type == 0)
4520 type = TREE_TYPE (constructor_decl);
4522 if (targetm.vector_opaque_p (type))
4523 error ("opaque vector types cannot be initialized");
4525 p->type = constructor_type;
4526 p->fields = constructor_fields;
4527 p->index = constructor_index;
4528 p->max_index = constructor_max_index;
4529 p->unfilled_index = constructor_unfilled_index;
4530 p->unfilled_fields = constructor_unfilled_fields;
4531 p->bit_index = constructor_bit_index;
4532 p->elements = constructor_elements;
4533 p->constant = constructor_constant;
4534 p->simple = constructor_simple;
4535 p->erroneous = constructor_erroneous;
4536 p->pending_elts = constructor_pending_elts;
4537 p->depth = constructor_depth;
4538 p->replacement_value.value = 0;
4539 p->replacement_value.original_code = ERROR_MARK;
4540 p->implicit = 0;
4541 p->range_stack = 0;
4542 p->outer = 0;
4543 p->incremental = constructor_incremental;
4544 p->designated = constructor_designated;
4545 p->next = 0;
4546 constructor_stack = p;
4548 constructor_constant = 1;
4549 constructor_simple = 1;
4550 constructor_depth = SPELLING_DEPTH ();
4551 constructor_elements = 0;
4552 constructor_pending_elts = 0;
4553 constructor_type = type;
4554 constructor_incremental = 1;
4555 constructor_designated = 0;
4556 designator_depth = 0;
4557 designator_errorneous = 0;
4559 if (TREE_CODE (constructor_type) == RECORD_TYPE
4560 || TREE_CODE (constructor_type) == UNION_TYPE)
4562 constructor_fields = TYPE_FIELDS (constructor_type);
4563 /* Skip any nameless bit fields at the beginning. */
4564 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4565 && DECL_NAME (constructor_fields) == 0)
4566 constructor_fields = TREE_CHAIN (constructor_fields);
4568 constructor_unfilled_fields = constructor_fields;
4569 constructor_bit_index = bitsize_zero_node;
4571 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4573 if (TYPE_DOMAIN (constructor_type))
4575 constructor_max_index
4576 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4578 /* Detect non-empty initializations of zero-length arrays. */
4579 if (constructor_max_index == NULL_TREE
4580 && TYPE_SIZE (constructor_type))
4581 constructor_max_index = build_int_cst (NULL_TREE, -1);
4583 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4584 to initialize VLAs will cause a proper error; avoid tree
4585 checking errors as well by setting a safe value. */
4586 if (constructor_max_index
4587 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4588 constructor_max_index = build_int_cst (NULL_TREE, -1);
4590 constructor_index
4591 = convert (bitsizetype,
4592 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4594 else
4595 constructor_index = bitsize_zero_node;
4597 constructor_unfilled_index = constructor_index;
4599 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4601 /* Vectors are like simple fixed-size arrays. */
4602 constructor_max_index =
4603 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4604 constructor_index = convert (bitsizetype, bitsize_zero_node);
4605 constructor_unfilled_index = constructor_index;
4607 else
4609 /* Handle the case of int x = {5}; */
4610 constructor_fields = constructor_type;
4611 constructor_unfilled_fields = constructor_type;
4615 /* Push down into a subobject, for initialization.
4616 If this is for an explicit set of braces, IMPLICIT is 0.
4617 If it is because the next element belongs at a lower level,
4618 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4620 void
4621 push_init_level (int implicit)
4623 struct constructor_stack *p;
4624 tree value = NULL_TREE;
4626 /* If we've exhausted any levels that didn't have braces,
4627 pop them now. */
4628 while (constructor_stack->implicit)
4630 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4631 || TREE_CODE (constructor_type) == UNION_TYPE)
4632 && constructor_fields == 0)
4633 process_init_element (pop_init_level (1));
4634 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4635 && constructor_max_index
4636 && tree_int_cst_lt (constructor_max_index, constructor_index))
4637 process_init_element (pop_init_level (1));
4638 else
4639 break;
4642 /* Unless this is an explicit brace, we need to preserve previous
4643 content if any. */
4644 if (implicit)
4646 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4647 || TREE_CODE (constructor_type) == UNION_TYPE)
4648 && constructor_fields)
4649 value = find_init_member (constructor_fields);
4650 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4651 value = find_init_member (constructor_index);
4654 p = XNEW (struct constructor_stack);
4655 p->type = constructor_type;
4656 p->fields = constructor_fields;
4657 p->index = constructor_index;
4658 p->max_index = constructor_max_index;
4659 p->unfilled_index = constructor_unfilled_index;
4660 p->unfilled_fields = constructor_unfilled_fields;
4661 p->bit_index = constructor_bit_index;
4662 p->elements = constructor_elements;
4663 p->constant = constructor_constant;
4664 p->simple = constructor_simple;
4665 p->erroneous = constructor_erroneous;
4666 p->pending_elts = constructor_pending_elts;
4667 p->depth = constructor_depth;
4668 p->replacement_value.value = 0;
4669 p->replacement_value.original_code = ERROR_MARK;
4670 p->implicit = implicit;
4671 p->outer = 0;
4672 p->incremental = constructor_incremental;
4673 p->designated = constructor_designated;
4674 p->next = constructor_stack;
4675 p->range_stack = 0;
4676 constructor_stack = p;
4678 constructor_constant = 1;
4679 constructor_simple = 1;
4680 constructor_depth = SPELLING_DEPTH ();
4681 constructor_elements = 0;
4682 constructor_incremental = 1;
4683 constructor_designated = 0;
4684 constructor_pending_elts = 0;
4685 if (!implicit)
4687 p->range_stack = constructor_range_stack;
4688 constructor_range_stack = 0;
4689 designator_depth = 0;
4690 designator_errorneous = 0;
4693 /* Don't die if an entire brace-pair level is superfluous
4694 in the containing level. */
4695 if (constructor_type == 0)
4697 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4698 || TREE_CODE (constructor_type) == UNION_TYPE)
4700 /* Don't die if there are extra init elts at the end. */
4701 if (constructor_fields == 0)
4702 constructor_type = 0;
4703 else
4705 constructor_type = TREE_TYPE (constructor_fields);
4706 push_member_name (constructor_fields);
4707 constructor_depth++;
4710 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4712 constructor_type = TREE_TYPE (constructor_type);
4713 push_array_bounds (tree_low_cst (constructor_index, 0));
4714 constructor_depth++;
4717 if (constructor_type == 0)
4719 error_init ("extra brace group at end of initializer");
4720 constructor_fields = 0;
4721 constructor_unfilled_fields = 0;
4722 return;
4725 if (value && TREE_CODE (value) == CONSTRUCTOR)
4727 constructor_constant = TREE_CONSTANT (value);
4728 constructor_simple = TREE_STATIC (value);
4729 constructor_elements = CONSTRUCTOR_ELTS (value);
4730 if (constructor_elements
4731 && (TREE_CODE (constructor_type) == RECORD_TYPE
4732 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4733 set_nonincremental_init ();
4736 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4738 missing_braces_mentioned = 1;
4739 warning_init ("missing braces around initializer");
4742 if (TREE_CODE (constructor_type) == RECORD_TYPE
4743 || TREE_CODE (constructor_type) == UNION_TYPE)
4745 constructor_fields = TYPE_FIELDS (constructor_type);
4746 /* Skip any nameless bit fields at the beginning. */
4747 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4748 && DECL_NAME (constructor_fields) == 0)
4749 constructor_fields = TREE_CHAIN (constructor_fields);
4751 constructor_unfilled_fields = constructor_fields;
4752 constructor_bit_index = bitsize_zero_node;
4754 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4756 /* Vectors are like simple fixed-size arrays. */
4757 constructor_max_index =
4758 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4759 constructor_index = convert (bitsizetype, integer_zero_node);
4760 constructor_unfilled_index = constructor_index;
4762 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4764 if (TYPE_DOMAIN (constructor_type))
4766 constructor_max_index
4767 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4769 /* Detect non-empty initializations of zero-length arrays. */
4770 if (constructor_max_index == NULL_TREE
4771 && TYPE_SIZE (constructor_type))
4772 constructor_max_index = build_int_cst (NULL_TREE, -1);
4774 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4775 to initialize VLAs will cause a proper error; avoid tree
4776 checking errors as well by setting a safe value. */
4777 if (constructor_max_index
4778 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4779 constructor_max_index = build_int_cst (NULL_TREE, -1);
4781 constructor_index
4782 = convert (bitsizetype,
4783 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4785 else
4786 constructor_index = bitsize_zero_node;
4788 constructor_unfilled_index = constructor_index;
4789 if (value && TREE_CODE (value) == STRING_CST)
4791 /* We need to split the char/wchar array into individual
4792 characters, so that we don't have to special case it
4793 everywhere. */
4794 set_nonincremental_init_from_string (value);
4797 else
4799 warning_init ("braces around scalar initializer");
4800 constructor_fields = constructor_type;
4801 constructor_unfilled_fields = constructor_type;
4805 /* At the end of an implicit or explicit brace level,
4806 finish up that level of constructor. If a single expression
4807 with redundant braces initialized that level, return the
4808 c_expr structure for that expression. Otherwise, the original_code
4809 element is set to ERROR_MARK.
4810 If we were outputting the elements as they are read, return 0 as the value
4811 from inner levels (process_init_element ignores that),
4812 but return error_mark_node as the value from the outermost level
4813 (that's what we want to put in DECL_INITIAL).
4814 Otherwise, return a CONSTRUCTOR expression as the value. */
4816 struct c_expr
4817 pop_init_level (int implicit)
4819 struct constructor_stack *p;
4820 struct c_expr ret;
4821 ret.value = 0;
4822 ret.original_code = ERROR_MARK;
4824 if (implicit == 0)
4826 /* When we come to an explicit close brace,
4827 pop any inner levels that didn't have explicit braces. */
4828 while (constructor_stack->implicit)
4829 process_init_element (pop_init_level (1));
4831 gcc_assert (!constructor_range_stack);
4834 /* Now output all pending elements. */
4835 constructor_incremental = 1;
4836 output_pending_init_elements (1);
4838 p = constructor_stack;
4840 /* Error for initializing a flexible array member, or a zero-length
4841 array member in an inappropriate context. */
4842 if (constructor_type && constructor_fields
4843 && TREE_CODE (constructor_type) == ARRAY_TYPE
4844 && TYPE_DOMAIN (constructor_type)
4845 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4847 /* Silently discard empty initializations. The parser will
4848 already have pedwarned for empty brackets. */
4849 if (integer_zerop (constructor_unfilled_index))
4850 constructor_type = NULL_TREE;
4851 else
4853 gcc_assert (!TYPE_SIZE (constructor_type));
4855 if (constructor_depth > 2)
4856 error_init ("initialization of flexible array member in a nested context");
4857 else if (pedantic)
4858 pedwarn_init ("initialization of a flexible array member");
4860 /* We have already issued an error message for the existence
4861 of a flexible array member not at the end of the structure.
4862 Discard the initializer so that we do not abort later. */
4863 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4864 constructor_type = NULL_TREE;
4868 /* Warn when some struct elements are implicitly initialized to zero. */
4869 if (warn_missing_field_initializers
4870 && constructor_type
4871 && TREE_CODE (constructor_type) == RECORD_TYPE
4872 && constructor_unfilled_fields)
4874 /* Do not warn for flexible array members or zero-length arrays. */
4875 while (constructor_unfilled_fields
4876 && (! DECL_SIZE (constructor_unfilled_fields)
4877 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4878 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4880 /* Do not warn if this level of the initializer uses member
4881 designators; it is likely to be deliberate. */
4882 if (constructor_unfilled_fields && !constructor_designated)
4884 push_member_name (constructor_unfilled_fields);
4885 warning_init ("missing initializer");
4886 RESTORE_SPELLING_DEPTH (constructor_depth);
4890 /* Pad out the end of the structure. */
4891 if (p->replacement_value.value)
4892 /* If this closes a superfluous brace pair,
4893 just pass out the element between them. */
4894 ret = p->replacement_value;
4895 else if (constructor_type == 0)
4897 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4898 && TREE_CODE (constructor_type) != UNION_TYPE
4899 && TREE_CODE (constructor_type) != ARRAY_TYPE
4900 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4902 /* A nonincremental scalar initializer--just return
4903 the element, after verifying there is just one. */
4904 if (constructor_elements == 0)
4906 if (!constructor_erroneous)
4907 error_init ("empty scalar initializer");
4908 ret.value = error_mark_node;
4910 else if (TREE_CHAIN (constructor_elements) != 0)
4912 error_init ("extra elements in scalar initializer");
4913 ret.value = TREE_VALUE (constructor_elements);
4915 else
4916 ret.value = TREE_VALUE (constructor_elements);
4918 else
4920 if (constructor_erroneous)
4921 ret.value = error_mark_node;
4922 else
4924 ret.value = build_constructor (constructor_type,
4925 nreverse (constructor_elements));
4926 if (constructor_constant)
4927 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
4928 if (constructor_constant && constructor_simple)
4929 TREE_STATIC (ret.value) = 1;
4933 constructor_type = p->type;
4934 constructor_fields = p->fields;
4935 constructor_index = p->index;
4936 constructor_max_index = p->max_index;
4937 constructor_unfilled_index = p->unfilled_index;
4938 constructor_unfilled_fields = p->unfilled_fields;
4939 constructor_bit_index = p->bit_index;
4940 constructor_elements = p->elements;
4941 constructor_constant = p->constant;
4942 constructor_simple = p->simple;
4943 constructor_erroneous = p->erroneous;
4944 constructor_incremental = p->incremental;
4945 constructor_designated = p->designated;
4946 constructor_pending_elts = p->pending_elts;
4947 constructor_depth = p->depth;
4948 if (!p->implicit)
4949 constructor_range_stack = p->range_stack;
4950 RESTORE_SPELLING_DEPTH (constructor_depth);
4952 constructor_stack = p->next;
4953 free (p);
4955 if (ret.value == 0)
4957 if (constructor_stack == 0)
4959 ret.value = error_mark_node;
4960 return ret;
4962 return ret;
4964 return ret;
4967 /* Common handling for both array range and field name designators.
4968 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4970 static int
4971 set_designator (int array)
4973 tree subtype;
4974 enum tree_code subcode;
4976 /* Don't die if an entire brace-pair level is superfluous
4977 in the containing level. */
4978 if (constructor_type == 0)
4979 return 1;
4981 /* If there were errors in this designator list already, bail out
4982 silently. */
4983 if (designator_errorneous)
4984 return 1;
4986 if (!designator_depth)
4988 gcc_assert (!constructor_range_stack);
4990 /* Designator list starts at the level of closest explicit
4991 braces. */
4992 while (constructor_stack->implicit)
4993 process_init_element (pop_init_level (1));
4994 constructor_designated = 1;
4995 return 0;
4998 if (constructor_no_implicit)
5000 error_init ("initialization designators may not nest");
5001 return 1;
5004 switch (TREE_CODE (constructor_type))
5006 case RECORD_TYPE:
5007 case UNION_TYPE:
5008 subtype = TREE_TYPE (constructor_fields);
5009 if (subtype != error_mark_node)
5010 subtype = TYPE_MAIN_VARIANT (subtype);
5011 break;
5012 case ARRAY_TYPE:
5013 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5014 break;
5015 default:
5016 gcc_unreachable ();
5019 subcode = TREE_CODE (subtype);
5020 if (array && subcode != ARRAY_TYPE)
5022 error_init ("array index in non-array initializer");
5023 return 1;
5025 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5027 error_init ("field name not in record or union initializer");
5028 return 1;
5031 constructor_designated = 1;
5032 push_init_level (2);
5033 return 0;
5036 /* If there are range designators in designator list, push a new designator
5037 to constructor_range_stack. RANGE_END is end of such stack range or
5038 NULL_TREE if there is no range designator at this level. */
5040 static void
5041 push_range_stack (tree range_end)
5043 struct constructor_range_stack *p;
5045 p = GGC_NEW (struct constructor_range_stack);
5046 p->prev = constructor_range_stack;
5047 p->next = 0;
5048 p->fields = constructor_fields;
5049 p->range_start = constructor_index;
5050 p->index = constructor_index;
5051 p->stack = constructor_stack;
5052 p->range_end = range_end;
5053 if (constructor_range_stack)
5054 constructor_range_stack->next = p;
5055 constructor_range_stack = p;
5058 /* Within an array initializer, specify the next index to be initialized.
5059 FIRST is that index. If LAST is nonzero, then initialize a range
5060 of indices, running from FIRST through LAST. */
5062 void
5063 set_init_index (tree first, tree last)
5065 if (set_designator (1))
5066 return;
5068 designator_errorneous = 1;
5070 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5071 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5073 error_init ("array index in initializer not of integer type");
5074 return;
5077 while ((TREE_CODE (first) == NOP_EXPR
5078 || TREE_CODE (first) == CONVERT_EXPR
5079 || TREE_CODE (first) == NON_LVALUE_EXPR)
5080 && (TYPE_MODE (TREE_TYPE (first))
5081 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5082 first = TREE_OPERAND (first, 0);
5084 if (last)
5085 while ((TREE_CODE (last) == NOP_EXPR
5086 || TREE_CODE (last) == CONVERT_EXPR
5087 || TREE_CODE (last) == NON_LVALUE_EXPR)
5088 && (TYPE_MODE (TREE_TYPE (last))
5089 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5090 last = TREE_OPERAND (last, 0);
5092 if (TREE_CODE (first) != INTEGER_CST)
5093 error_init ("nonconstant array index in initializer");
5094 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5095 error_init ("nonconstant array index in initializer");
5096 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5097 error_init ("array index in non-array initializer");
5098 else if (tree_int_cst_sgn (first) == -1)
5099 error_init ("array index in initializer exceeds array bounds");
5100 else if (constructor_max_index
5101 && tree_int_cst_lt (constructor_max_index, first))
5102 error_init ("array index in initializer exceeds array bounds");
5103 else
5105 constructor_index = convert (bitsizetype, first);
5107 if (last)
5109 if (tree_int_cst_equal (first, last))
5110 last = 0;
5111 else if (tree_int_cst_lt (last, first))
5113 error_init ("empty index range in initializer");
5114 last = 0;
5116 else
5118 last = convert (bitsizetype, last);
5119 if (constructor_max_index != 0
5120 && tree_int_cst_lt (constructor_max_index, last))
5122 error_init ("array index range in initializer exceeds array bounds");
5123 last = 0;
5128 designator_depth++;
5129 designator_errorneous = 0;
5130 if (constructor_range_stack || last)
5131 push_range_stack (last);
5135 /* Within a struct initializer, specify the next field to be initialized. */
5137 void
5138 set_init_label (tree fieldname)
5140 tree tail;
5142 if (set_designator (0))
5143 return;
5145 designator_errorneous = 1;
5147 if (TREE_CODE (constructor_type) != RECORD_TYPE
5148 && TREE_CODE (constructor_type) != UNION_TYPE)
5150 error_init ("field name not in record or union initializer");
5151 return;
5154 for (tail = TYPE_FIELDS (constructor_type); tail;
5155 tail = TREE_CHAIN (tail))
5157 if (DECL_NAME (tail) == fieldname)
5158 break;
5161 if (tail == 0)
5162 error ("unknown field %qs specified in initializer",
5163 IDENTIFIER_POINTER (fieldname));
5164 else
5166 constructor_fields = tail;
5167 designator_depth++;
5168 designator_errorneous = 0;
5169 if (constructor_range_stack)
5170 push_range_stack (NULL_TREE);
5174 /* Add a new initializer to the tree of pending initializers. PURPOSE
5175 identifies the initializer, either array index or field in a structure.
5176 VALUE is the value of that index or field. */
5178 static void
5179 add_pending_init (tree purpose, tree value)
5181 struct init_node *p, **q, *r;
5183 q = &constructor_pending_elts;
5184 p = 0;
5186 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5188 while (*q != 0)
5190 p = *q;
5191 if (tree_int_cst_lt (purpose, p->purpose))
5192 q = &p->left;
5193 else if (tree_int_cst_lt (p->purpose, purpose))
5194 q = &p->right;
5195 else
5197 if (TREE_SIDE_EFFECTS (p->value))
5198 warning_init ("initialized field with side-effects overwritten");
5199 p->value = value;
5200 return;
5204 else
5206 tree bitpos;
5208 bitpos = bit_position (purpose);
5209 while (*q != NULL)
5211 p = *q;
5212 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5213 q = &p->left;
5214 else if (p->purpose != purpose)
5215 q = &p->right;
5216 else
5218 if (TREE_SIDE_EFFECTS (p->value))
5219 warning_init ("initialized field with side-effects overwritten");
5220 p->value = value;
5221 return;
5226 r = GGC_NEW (struct init_node);
5227 r->purpose = purpose;
5228 r->value = value;
5230 *q = r;
5231 r->parent = p;
5232 r->left = 0;
5233 r->right = 0;
5234 r->balance = 0;
5236 while (p)
5238 struct init_node *s;
5240 if (r == p->left)
5242 if (p->balance == 0)
5243 p->balance = -1;
5244 else if (p->balance < 0)
5246 if (r->balance < 0)
5248 /* L rotation. */
5249 p->left = r->right;
5250 if (p->left)
5251 p->left->parent = p;
5252 r->right = p;
5254 p->balance = 0;
5255 r->balance = 0;
5257 s = p->parent;
5258 p->parent = r;
5259 r->parent = s;
5260 if (s)
5262 if (s->left == p)
5263 s->left = r;
5264 else
5265 s->right = r;
5267 else
5268 constructor_pending_elts = r;
5270 else
5272 /* LR rotation. */
5273 struct init_node *t = r->right;
5275 r->right = t->left;
5276 if (r->right)
5277 r->right->parent = r;
5278 t->left = r;
5280 p->left = t->right;
5281 if (p->left)
5282 p->left->parent = p;
5283 t->right = p;
5285 p->balance = t->balance < 0;
5286 r->balance = -(t->balance > 0);
5287 t->balance = 0;
5289 s = p->parent;
5290 p->parent = t;
5291 r->parent = t;
5292 t->parent = s;
5293 if (s)
5295 if (s->left == p)
5296 s->left = t;
5297 else
5298 s->right = t;
5300 else
5301 constructor_pending_elts = t;
5303 break;
5305 else
5307 /* p->balance == +1; growth of left side balances the node. */
5308 p->balance = 0;
5309 break;
5312 else /* r == p->right */
5314 if (p->balance == 0)
5315 /* Growth propagation from right side. */
5316 p->balance++;
5317 else if (p->balance > 0)
5319 if (r->balance > 0)
5321 /* R rotation. */
5322 p->right = r->left;
5323 if (p->right)
5324 p->right->parent = p;
5325 r->left = p;
5327 p->balance = 0;
5328 r->balance = 0;
5330 s = p->parent;
5331 p->parent = r;
5332 r->parent = s;
5333 if (s)
5335 if (s->left == p)
5336 s->left = r;
5337 else
5338 s->right = r;
5340 else
5341 constructor_pending_elts = r;
5343 else /* r->balance == -1 */
5345 /* RL rotation */
5346 struct init_node *t = r->left;
5348 r->left = t->right;
5349 if (r->left)
5350 r->left->parent = r;
5351 t->right = r;
5353 p->right = t->left;
5354 if (p->right)
5355 p->right->parent = p;
5356 t->left = p;
5358 r->balance = (t->balance < 0);
5359 p->balance = -(t->balance > 0);
5360 t->balance = 0;
5362 s = p->parent;
5363 p->parent = t;
5364 r->parent = t;
5365 t->parent = s;
5366 if (s)
5368 if (s->left == p)
5369 s->left = t;
5370 else
5371 s->right = t;
5373 else
5374 constructor_pending_elts = t;
5376 break;
5378 else
5380 /* p->balance == -1; growth of right side balances the node. */
5381 p->balance = 0;
5382 break;
5386 r = p;
5387 p = p->parent;
5391 /* Build AVL tree from a sorted chain. */
5393 static void
5394 set_nonincremental_init (void)
5396 tree chain;
5398 if (TREE_CODE (constructor_type) != RECORD_TYPE
5399 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5400 return;
5402 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5403 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5404 constructor_elements = 0;
5405 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5407 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5408 /* Skip any nameless bit fields at the beginning. */
5409 while (constructor_unfilled_fields != 0
5410 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5411 && DECL_NAME (constructor_unfilled_fields) == 0)
5412 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5415 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5417 if (TYPE_DOMAIN (constructor_type))
5418 constructor_unfilled_index
5419 = convert (bitsizetype,
5420 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5421 else
5422 constructor_unfilled_index = bitsize_zero_node;
5424 constructor_incremental = 0;
5427 /* Build AVL tree from a string constant. */
5429 static void
5430 set_nonincremental_init_from_string (tree str)
5432 tree value, purpose, type;
5433 HOST_WIDE_INT val[2];
5434 const char *p, *end;
5435 int byte, wchar_bytes, charwidth, bitpos;
5437 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5439 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5440 == TYPE_PRECISION (char_type_node))
5441 wchar_bytes = 1;
5442 else
5444 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5445 == TYPE_PRECISION (wchar_type_node));
5446 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5448 charwidth = TYPE_PRECISION (char_type_node);
5449 type = TREE_TYPE (constructor_type);
5450 p = TREE_STRING_POINTER (str);
5451 end = p + TREE_STRING_LENGTH (str);
5453 for (purpose = bitsize_zero_node;
5454 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5455 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5457 if (wchar_bytes == 1)
5459 val[1] = (unsigned char) *p++;
5460 val[0] = 0;
5462 else
5464 val[0] = 0;
5465 val[1] = 0;
5466 for (byte = 0; byte < wchar_bytes; byte++)
5468 if (BYTES_BIG_ENDIAN)
5469 bitpos = (wchar_bytes - byte - 1) * charwidth;
5470 else
5471 bitpos = byte * charwidth;
5472 val[bitpos < HOST_BITS_PER_WIDE_INT]
5473 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5474 << (bitpos % HOST_BITS_PER_WIDE_INT);
5478 if (!TYPE_UNSIGNED (type))
5480 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5481 if (bitpos < HOST_BITS_PER_WIDE_INT)
5483 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5485 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5486 val[0] = -1;
5489 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5491 if (val[1] < 0)
5492 val[0] = -1;
5494 else if (val[0] & (((HOST_WIDE_INT) 1)
5495 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5496 val[0] |= ((HOST_WIDE_INT) -1)
5497 << (bitpos - HOST_BITS_PER_WIDE_INT);
5500 value = build_int_cst_wide (type, val[1], val[0]);
5501 add_pending_init (purpose, value);
5504 constructor_incremental = 0;
5507 /* Return value of FIELD in pending initializer or zero if the field was
5508 not initialized yet. */
5510 static tree
5511 find_init_member (tree field)
5513 struct init_node *p;
5515 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5517 if (constructor_incremental
5518 && tree_int_cst_lt (field, constructor_unfilled_index))
5519 set_nonincremental_init ();
5521 p = constructor_pending_elts;
5522 while (p)
5524 if (tree_int_cst_lt (field, p->purpose))
5525 p = p->left;
5526 else if (tree_int_cst_lt (p->purpose, field))
5527 p = p->right;
5528 else
5529 return p->value;
5532 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5534 tree bitpos = bit_position (field);
5536 if (constructor_incremental
5537 && (!constructor_unfilled_fields
5538 || tree_int_cst_lt (bitpos,
5539 bit_position (constructor_unfilled_fields))))
5540 set_nonincremental_init ();
5542 p = constructor_pending_elts;
5543 while (p)
5545 if (field == p->purpose)
5546 return p->value;
5547 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5548 p = p->left;
5549 else
5550 p = p->right;
5553 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5555 if (constructor_elements
5556 && TREE_PURPOSE (constructor_elements) == field)
5557 return TREE_VALUE (constructor_elements);
5559 return 0;
5562 /* "Output" the next constructor element.
5563 At top level, really output it to assembler code now.
5564 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5565 TYPE is the data type that the containing data type wants here.
5566 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5567 If VALUE is a string constant, STRICT_STRING is true if it is
5568 unparenthesized or we should not warn here for it being parenthesized.
5569 For other types of VALUE, STRICT_STRING is not used.
5571 PENDING if non-nil means output pending elements that belong
5572 right after this element. (PENDING is normally 1;
5573 it is 0 while outputting pending elements, to avoid recursion.) */
5575 static void
5576 output_init_element (tree value, bool strict_string, tree type, tree field,
5577 int pending)
5579 if (type == error_mark_node)
5581 constructor_erroneous = 1;
5582 return;
5584 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5585 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5586 && !(TREE_CODE (value) == STRING_CST
5587 && TREE_CODE (type) == ARRAY_TYPE
5588 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5589 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5590 TYPE_MAIN_VARIANT (type))))
5591 value = default_conversion (value);
5593 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5594 && require_constant_value && !flag_isoc99 && pending)
5596 /* As an extension, allow initializing objects with static storage
5597 duration with compound literals (which are then treated just as
5598 the brace enclosed list they contain). */
5599 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5600 value = DECL_INITIAL (decl);
5603 if (value == error_mark_node)
5604 constructor_erroneous = 1;
5605 else if (!TREE_CONSTANT (value))
5606 constructor_constant = 0;
5607 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5608 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5609 || TREE_CODE (constructor_type) == UNION_TYPE)
5610 && DECL_C_BIT_FIELD (field)
5611 && TREE_CODE (value) != INTEGER_CST))
5612 constructor_simple = 0;
5614 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5616 if (require_constant_value)
5618 error_init ("initializer element is not constant");
5619 value = error_mark_node;
5621 else if (require_constant_elements)
5622 pedwarn ("initializer element is not computable at load time");
5625 /* If this field is empty (and not at the end of structure),
5626 don't do anything other than checking the initializer. */
5627 if (field
5628 && (TREE_TYPE (field) == error_mark_node
5629 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5630 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5631 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5632 || TREE_CHAIN (field)))))
5633 return;
5635 value = digest_init (type, value, strict_string, require_constant_value);
5636 if (value == error_mark_node)
5638 constructor_erroneous = 1;
5639 return;
5642 /* If this element doesn't come next in sequence,
5643 put it on constructor_pending_elts. */
5644 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5645 && (!constructor_incremental
5646 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5648 if (constructor_incremental
5649 && tree_int_cst_lt (field, constructor_unfilled_index))
5650 set_nonincremental_init ();
5652 add_pending_init (field, value);
5653 return;
5655 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5656 && (!constructor_incremental
5657 || field != constructor_unfilled_fields))
5659 /* We do this for records but not for unions. In a union,
5660 no matter which field is specified, it can be initialized
5661 right away since it starts at the beginning of the union. */
5662 if (constructor_incremental)
5664 if (!constructor_unfilled_fields)
5665 set_nonincremental_init ();
5666 else
5668 tree bitpos, unfillpos;
5670 bitpos = bit_position (field);
5671 unfillpos = bit_position (constructor_unfilled_fields);
5673 if (tree_int_cst_lt (bitpos, unfillpos))
5674 set_nonincremental_init ();
5678 add_pending_init (field, value);
5679 return;
5681 else if (TREE_CODE (constructor_type) == UNION_TYPE
5682 && constructor_elements)
5684 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5685 warning_init ("initialized field with side-effects overwritten");
5687 /* We can have just one union field set. */
5688 constructor_elements = 0;
5691 /* Otherwise, output this element either to
5692 constructor_elements or to the assembler file. */
5694 if (field && TREE_CODE (field) == INTEGER_CST)
5695 field = copy_node (field);
5696 constructor_elements
5697 = tree_cons (field, value, constructor_elements);
5699 /* Advance the variable that indicates sequential elements output. */
5700 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5701 constructor_unfilled_index
5702 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5703 bitsize_one_node);
5704 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5706 constructor_unfilled_fields
5707 = TREE_CHAIN (constructor_unfilled_fields);
5709 /* Skip any nameless bit fields. */
5710 while (constructor_unfilled_fields != 0
5711 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5712 && DECL_NAME (constructor_unfilled_fields) == 0)
5713 constructor_unfilled_fields =
5714 TREE_CHAIN (constructor_unfilled_fields);
5716 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5717 constructor_unfilled_fields = 0;
5719 /* Now output any pending elements which have become next. */
5720 if (pending)
5721 output_pending_init_elements (0);
5724 /* Output any pending elements which have become next.
5725 As we output elements, constructor_unfilled_{fields,index}
5726 advances, which may cause other elements to become next;
5727 if so, they too are output.
5729 If ALL is 0, we return when there are
5730 no more pending elements to output now.
5732 If ALL is 1, we output space as necessary so that
5733 we can output all the pending elements. */
5735 static void
5736 output_pending_init_elements (int all)
5738 struct init_node *elt = constructor_pending_elts;
5739 tree next;
5741 retry:
5743 /* Look through the whole pending tree.
5744 If we find an element that should be output now,
5745 output it. Otherwise, set NEXT to the element
5746 that comes first among those still pending. */
5748 next = 0;
5749 while (elt)
5751 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5753 if (tree_int_cst_equal (elt->purpose,
5754 constructor_unfilled_index))
5755 output_init_element (elt->value, true,
5756 TREE_TYPE (constructor_type),
5757 constructor_unfilled_index, 0);
5758 else if (tree_int_cst_lt (constructor_unfilled_index,
5759 elt->purpose))
5761 /* Advance to the next smaller node. */
5762 if (elt->left)
5763 elt = elt->left;
5764 else
5766 /* We have reached the smallest node bigger than the
5767 current unfilled index. Fill the space first. */
5768 next = elt->purpose;
5769 break;
5772 else
5774 /* Advance to the next bigger node. */
5775 if (elt->right)
5776 elt = elt->right;
5777 else
5779 /* We have reached the biggest node in a subtree. Find
5780 the parent of it, which is the next bigger node. */
5781 while (elt->parent && elt->parent->right == elt)
5782 elt = elt->parent;
5783 elt = elt->parent;
5784 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5785 elt->purpose))
5787 next = elt->purpose;
5788 break;
5793 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5794 || TREE_CODE (constructor_type) == UNION_TYPE)
5796 tree ctor_unfilled_bitpos, elt_bitpos;
5798 /* If the current record is complete we are done. */
5799 if (constructor_unfilled_fields == 0)
5800 break;
5802 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5803 elt_bitpos = bit_position (elt->purpose);
5804 /* We can't compare fields here because there might be empty
5805 fields in between. */
5806 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5808 constructor_unfilled_fields = elt->purpose;
5809 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5810 elt->purpose, 0);
5812 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5814 /* Advance to the next smaller node. */
5815 if (elt->left)
5816 elt = elt->left;
5817 else
5819 /* We have reached the smallest node bigger than the
5820 current unfilled field. Fill the space first. */
5821 next = elt->purpose;
5822 break;
5825 else
5827 /* Advance to the next bigger node. */
5828 if (elt->right)
5829 elt = elt->right;
5830 else
5832 /* We have reached the biggest node in a subtree. Find
5833 the parent of it, which is the next bigger node. */
5834 while (elt->parent && elt->parent->right == elt)
5835 elt = elt->parent;
5836 elt = elt->parent;
5837 if (elt
5838 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5839 bit_position (elt->purpose))))
5841 next = elt->purpose;
5842 break;
5849 /* Ordinarily return, but not if we want to output all
5850 and there are elements left. */
5851 if (! (all && next != 0))
5852 return;
5854 /* If it's not incremental, just skip over the gap, so that after
5855 jumping to retry we will output the next successive element. */
5856 if (TREE_CODE (constructor_type) == RECORD_TYPE
5857 || TREE_CODE (constructor_type) == UNION_TYPE)
5858 constructor_unfilled_fields = next;
5859 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5860 constructor_unfilled_index = next;
5862 /* ELT now points to the node in the pending tree with the next
5863 initializer to output. */
5864 goto retry;
5867 /* Add one non-braced element to the current constructor level.
5868 This adjusts the current position within the constructor's type.
5869 This may also start or terminate implicit levels
5870 to handle a partly-braced initializer.
5872 Once this has found the correct level for the new element,
5873 it calls output_init_element. */
5875 void
5876 process_init_element (struct c_expr value)
5878 tree orig_value = value.value;
5879 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5880 bool strict_string = value.original_code == STRING_CST;
5882 designator_depth = 0;
5883 designator_errorneous = 0;
5885 /* Handle superfluous braces around string cst as in
5886 char x[] = {"foo"}; */
5887 if (string_flag
5888 && constructor_type
5889 && TREE_CODE (constructor_type) == ARRAY_TYPE
5890 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5891 && integer_zerop (constructor_unfilled_index))
5893 if (constructor_stack->replacement_value.value)
5894 error_init ("excess elements in char array initializer");
5895 constructor_stack->replacement_value = value;
5896 return;
5899 if (constructor_stack->replacement_value.value != 0)
5901 error_init ("excess elements in struct initializer");
5902 return;
5905 /* Ignore elements of a brace group if it is entirely superfluous
5906 and has already been diagnosed. */
5907 if (constructor_type == 0)
5908 return;
5910 /* If we've exhausted any levels that didn't have braces,
5911 pop them now. */
5912 while (constructor_stack->implicit)
5914 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5915 || TREE_CODE (constructor_type) == UNION_TYPE)
5916 && constructor_fields == 0)
5917 process_init_element (pop_init_level (1));
5918 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5919 && (constructor_max_index == 0
5920 || tree_int_cst_lt (constructor_max_index,
5921 constructor_index)))
5922 process_init_element (pop_init_level (1));
5923 else
5924 break;
5927 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5928 if (constructor_range_stack)
5930 /* If value is a compound literal and we'll be just using its
5931 content, don't put it into a SAVE_EXPR. */
5932 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
5933 || !require_constant_value
5934 || flag_isoc99)
5935 value.value = save_expr (value.value);
5938 while (1)
5940 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5942 tree fieldtype;
5943 enum tree_code fieldcode;
5945 if (constructor_fields == 0)
5947 pedwarn_init ("excess elements in struct initializer");
5948 break;
5951 fieldtype = TREE_TYPE (constructor_fields);
5952 if (fieldtype != error_mark_node)
5953 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5954 fieldcode = TREE_CODE (fieldtype);
5956 /* Error for non-static initialization of a flexible array member. */
5957 if (fieldcode == ARRAY_TYPE
5958 && !require_constant_value
5959 && TYPE_SIZE (fieldtype) == NULL_TREE
5960 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5962 error_init ("non-static initialization of a flexible array member");
5963 break;
5966 /* Accept a string constant to initialize a subarray. */
5967 if (value.value != 0
5968 && fieldcode == ARRAY_TYPE
5969 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
5970 && string_flag)
5971 value.value = orig_value;
5972 /* Otherwise, if we have come to a subaggregate,
5973 and we don't have an element of its type, push into it. */
5974 else if (value.value != 0 && !constructor_no_implicit
5975 && value.value != error_mark_node
5976 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
5977 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5978 || fieldcode == UNION_TYPE))
5980 push_init_level (1);
5981 continue;
5984 if (value.value)
5986 push_member_name (constructor_fields);
5987 output_init_element (value.value, strict_string,
5988 fieldtype, constructor_fields, 1);
5989 RESTORE_SPELLING_DEPTH (constructor_depth);
5991 else
5992 /* Do the bookkeeping for an element that was
5993 directly output as a constructor. */
5995 /* For a record, keep track of end position of last field. */
5996 if (DECL_SIZE (constructor_fields))
5997 constructor_bit_index
5998 = size_binop (PLUS_EXPR,
5999 bit_position (constructor_fields),
6000 DECL_SIZE (constructor_fields));
6002 /* If the current field was the first one not yet written out,
6003 it isn't now, so update. */
6004 if (constructor_unfilled_fields == constructor_fields)
6006 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6007 /* Skip any nameless bit fields. */
6008 while (constructor_unfilled_fields != 0
6009 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6010 && DECL_NAME (constructor_unfilled_fields) == 0)
6011 constructor_unfilled_fields =
6012 TREE_CHAIN (constructor_unfilled_fields);
6016 constructor_fields = TREE_CHAIN (constructor_fields);
6017 /* Skip any nameless bit fields at the beginning. */
6018 while (constructor_fields != 0
6019 && DECL_C_BIT_FIELD (constructor_fields)
6020 && DECL_NAME (constructor_fields) == 0)
6021 constructor_fields = TREE_CHAIN (constructor_fields);
6023 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6025 tree fieldtype;
6026 enum tree_code fieldcode;
6028 if (constructor_fields == 0)
6030 pedwarn_init ("excess elements in union initializer");
6031 break;
6034 fieldtype = TREE_TYPE (constructor_fields);
6035 if (fieldtype != error_mark_node)
6036 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6037 fieldcode = TREE_CODE (fieldtype);
6039 /* Warn that traditional C rejects initialization of unions.
6040 We skip the warning if the value is zero. This is done
6041 under the assumption that the zero initializer in user
6042 code appears conditioned on e.g. __STDC__ to avoid
6043 "missing initializer" warnings and relies on default
6044 initialization to zero in the traditional C case.
6045 We also skip the warning if the initializer is designated,
6046 again on the assumption that this must be conditional on
6047 __STDC__ anyway (and we've already complained about the
6048 member-designator already). */
6049 if (warn_traditional && !in_system_header && !constructor_designated
6050 && !(value.value && (integer_zerop (value.value)
6051 || real_zerop (value.value))))
6052 warning ("traditional C rejects initialization of unions");
6054 /* Accept a string constant to initialize a subarray. */
6055 if (value.value != 0
6056 && fieldcode == ARRAY_TYPE
6057 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6058 && string_flag)
6059 value.value = orig_value;
6060 /* Otherwise, if we have come to a subaggregate,
6061 and we don't have an element of its type, push into it. */
6062 else if (value.value != 0 && !constructor_no_implicit
6063 && value.value != error_mark_node
6064 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6065 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6066 || fieldcode == UNION_TYPE))
6068 push_init_level (1);
6069 continue;
6072 if (value.value)
6074 push_member_name (constructor_fields);
6075 output_init_element (value.value, strict_string,
6076 fieldtype, constructor_fields, 1);
6077 RESTORE_SPELLING_DEPTH (constructor_depth);
6079 else
6080 /* Do the bookkeeping for an element that was
6081 directly output as a constructor. */
6083 constructor_bit_index = DECL_SIZE (constructor_fields);
6084 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6087 constructor_fields = 0;
6089 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6091 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6092 enum tree_code eltcode = TREE_CODE (elttype);
6094 /* Accept a string constant to initialize a subarray. */
6095 if (value.value != 0
6096 && eltcode == ARRAY_TYPE
6097 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6098 && string_flag)
6099 value.value = orig_value;
6100 /* Otherwise, if we have come to a subaggregate,
6101 and we don't have an element of its type, push into it. */
6102 else if (value.value != 0 && !constructor_no_implicit
6103 && value.value != error_mark_node
6104 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6105 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6106 || eltcode == UNION_TYPE))
6108 push_init_level (1);
6109 continue;
6112 if (constructor_max_index != 0
6113 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6114 || integer_all_onesp (constructor_max_index)))
6116 pedwarn_init ("excess elements in array initializer");
6117 break;
6120 /* Now output the actual element. */
6121 if (value.value)
6123 push_array_bounds (tree_low_cst (constructor_index, 0));
6124 output_init_element (value.value, strict_string,
6125 elttype, constructor_index, 1);
6126 RESTORE_SPELLING_DEPTH (constructor_depth);
6129 constructor_index
6130 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6132 if (!value.value)
6133 /* If we are doing the bookkeeping for an element that was
6134 directly output as a constructor, we must update
6135 constructor_unfilled_index. */
6136 constructor_unfilled_index = constructor_index;
6138 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6140 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6142 /* Do a basic check of initializer size. Note that vectors
6143 always have a fixed size derived from their type. */
6144 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6146 pedwarn_init ("excess elements in vector initializer");
6147 break;
6150 /* Now output the actual element. */
6151 if (value.value)
6152 output_init_element (value.value, strict_string,
6153 elttype, constructor_index, 1);
6155 constructor_index
6156 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6158 if (!value.value)
6159 /* If we are doing the bookkeeping for an element that was
6160 directly output as a constructor, we must update
6161 constructor_unfilled_index. */
6162 constructor_unfilled_index = constructor_index;
6165 /* Handle the sole element allowed in a braced initializer
6166 for a scalar variable. */
6167 else if (constructor_fields == 0)
6169 pedwarn_init ("excess elements in scalar initializer");
6170 break;
6172 else
6174 if (value.value)
6175 output_init_element (value.value, strict_string,
6176 constructor_type, NULL_TREE, 1);
6177 constructor_fields = 0;
6180 /* Handle range initializers either at this level or anywhere higher
6181 in the designator stack. */
6182 if (constructor_range_stack)
6184 struct constructor_range_stack *p, *range_stack;
6185 int finish = 0;
6187 range_stack = constructor_range_stack;
6188 constructor_range_stack = 0;
6189 while (constructor_stack != range_stack->stack)
6191 gcc_assert (constructor_stack->implicit);
6192 process_init_element (pop_init_level (1));
6194 for (p = range_stack;
6195 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6196 p = p->prev)
6198 gcc_assert (constructor_stack->implicit);
6199 process_init_element (pop_init_level (1));
6202 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6203 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6204 finish = 1;
6206 while (1)
6208 constructor_index = p->index;
6209 constructor_fields = p->fields;
6210 if (finish && p->range_end && p->index == p->range_start)
6212 finish = 0;
6213 p->prev = 0;
6215 p = p->next;
6216 if (!p)
6217 break;
6218 push_init_level (2);
6219 p->stack = constructor_stack;
6220 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6221 p->index = p->range_start;
6224 if (!finish)
6225 constructor_range_stack = range_stack;
6226 continue;
6229 break;
6232 constructor_range_stack = 0;
6235 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6236 (guaranteed to be 'volatile' or null) and ARGS (represented using
6237 an ASM_EXPR node). */
6238 tree
6239 build_asm_stmt (tree cv_qualifier, tree args)
6241 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6242 ASM_VOLATILE_P (args) = 1;
6243 return add_stmt (args);
6246 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6247 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6248 SIMPLE indicates whether there was anything at all after the
6249 string in the asm expression -- asm("blah") and asm("blah" : )
6250 are subtly different. We use a ASM_EXPR node to represent this. */
6251 tree
6252 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6253 bool simple)
6255 tree tail;
6256 tree args;
6257 int i;
6258 const char *constraint;
6259 bool allows_mem, allows_reg, is_inout;
6260 int ninputs;
6261 int noutputs;
6263 ninputs = list_length (inputs);
6264 noutputs = list_length (outputs);
6266 /* Remove output conversions that change the type but not the mode. */
6267 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6269 tree output = TREE_VALUE (tail);
6270 STRIP_NOPS (output);
6271 TREE_VALUE (tail) = output;
6272 lvalue_or_else (output, "invalid lvalue in asm statement");
6274 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6276 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6277 &allows_mem, &allows_reg, &is_inout))
6279 /* By marking this operand as erroneous, we will not try
6280 to process this operand again in expand_asm_operands. */
6281 TREE_VALUE (tail) = error_mark_node;
6282 continue;
6285 /* If the operand is a DECL that is going to end up in
6286 memory, assume it is addressable. This is a bit more
6287 conservative than it would ideally be; the exact test is
6288 buried deep in expand_asm_operands and depends on the
6289 DECL_RTL for the OPERAND -- which we don't have at this
6290 point. */
6291 if (!allows_reg && DECL_P (output))
6292 c_mark_addressable (output);
6295 /* Perform default conversions on array and function inputs.
6296 Don't do this for other types as it would screw up operands
6297 expected to be in memory. */
6298 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6299 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6301 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6303 /* Simple asm statements are treated as volatile. */
6304 if (simple)
6306 ASM_VOLATILE_P (args) = 1;
6307 ASM_INPUT_P (args) = 1;
6309 return args;
6312 /* Generate a goto statement to LABEL. */
6314 tree
6315 c_finish_goto_label (tree label)
6317 tree decl = lookup_label (label);
6318 if (!decl)
6319 return NULL_TREE;
6321 TREE_USED (decl) = 1;
6322 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6325 /* Generate a computed goto statement to EXPR. */
6327 tree
6328 c_finish_goto_ptr (tree expr)
6330 if (pedantic)
6331 pedwarn ("ISO C forbids %<goto *expr;%>");
6332 expr = convert (ptr_type_node, expr);
6333 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6336 /* Generate a C `return' statement. RETVAL is the expression for what
6337 to return, or a null pointer for `return;' with no value. */
6339 tree
6340 c_finish_return (tree retval)
6342 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6344 if (TREE_THIS_VOLATILE (current_function_decl))
6345 warning ("function declared %<noreturn%> has a %<return%> statement");
6347 if (!retval)
6349 current_function_returns_null = 1;
6350 if ((warn_return_type || flag_isoc99)
6351 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6352 pedwarn_c99 ("%<return%> with no value, in "
6353 "function returning non-void");
6355 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6357 current_function_returns_null = 1;
6358 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6359 pedwarn ("%<return%> with a value, in function returning void");
6361 else
6363 tree t = convert_for_assignment (valtype, retval, _("return"),
6364 NULL_TREE, NULL_TREE, 0);
6365 tree res = DECL_RESULT (current_function_decl);
6366 tree inner;
6368 current_function_returns_value = 1;
6369 if (t == error_mark_node)
6370 return NULL_TREE;
6372 inner = t = convert (TREE_TYPE (res), t);
6374 /* Strip any conversions, additions, and subtractions, and see if
6375 we are returning the address of a local variable. Warn if so. */
6376 while (1)
6378 switch (TREE_CODE (inner))
6380 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6381 case PLUS_EXPR:
6382 inner = TREE_OPERAND (inner, 0);
6383 continue;
6385 case MINUS_EXPR:
6386 /* If the second operand of the MINUS_EXPR has a pointer
6387 type (or is converted from it), this may be valid, so
6388 don't give a warning. */
6390 tree op1 = TREE_OPERAND (inner, 1);
6392 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6393 && (TREE_CODE (op1) == NOP_EXPR
6394 || TREE_CODE (op1) == NON_LVALUE_EXPR
6395 || TREE_CODE (op1) == CONVERT_EXPR))
6396 op1 = TREE_OPERAND (op1, 0);
6398 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6399 break;
6401 inner = TREE_OPERAND (inner, 0);
6402 continue;
6405 case ADDR_EXPR:
6406 inner = TREE_OPERAND (inner, 0);
6408 while (REFERENCE_CLASS_P (inner)
6409 && TREE_CODE (inner) != INDIRECT_REF)
6410 inner = TREE_OPERAND (inner, 0);
6412 if (DECL_P (inner)
6413 && ! DECL_EXTERNAL (inner)
6414 && ! TREE_STATIC (inner)
6415 && DECL_CONTEXT (inner) == current_function_decl)
6416 warning ("function returns address of local variable");
6417 break;
6419 default:
6420 break;
6423 break;
6426 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6429 return add_stmt (build_stmt (RETURN_EXPR, retval));
6432 struct c_switch {
6433 /* The SWITCH_STMT being built. */
6434 tree switch_stmt;
6436 /* The original type of the testing expression, i.e. before the
6437 default conversion is applied. */
6438 tree orig_type;
6440 /* A splay-tree mapping the low element of a case range to the high
6441 element, or NULL_TREE if there is no high element. Used to
6442 determine whether or not a new case label duplicates an old case
6443 label. We need a tree, rather than simply a hash table, because
6444 of the GNU case range extension. */
6445 splay_tree cases;
6447 /* The next node on the stack. */
6448 struct c_switch *next;
6451 /* A stack of the currently active switch statements. The innermost
6452 switch statement is on the top of the stack. There is no need to
6453 mark the stack for garbage collection because it is only active
6454 during the processing of the body of a function, and we never
6455 collect at that point. */
6457 struct c_switch *c_switch_stack;
6459 /* Start a C switch statement, testing expression EXP. Return the new
6460 SWITCH_STMT. */
6462 tree
6463 c_start_case (tree exp)
6465 enum tree_code code;
6466 tree type, orig_type = error_mark_node;
6467 struct c_switch *cs;
6469 if (exp != error_mark_node)
6471 code = TREE_CODE (TREE_TYPE (exp));
6472 orig_type = TREE_TYPE (exp);
6474 if (! INTEGRAL_TYPE_P (orig_type)
6475 && code != ERROR_MARK)
6477 error ("switch quantity not an integer");
6478 exp = integer_zero_node;
6480 else
6482 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6484 if (warn_traditional && !in_system_header
6485 && (type == long_integer_type_node
6486 || type == long_unsigned_type_node))
6487 warning ("%<long%> switch expression not converted to "
6488 "%<int%> in ISO C");
6490 exp = default_conversion (exp);
6491 type = TREE_TYPE (exp);
6495 /* Add this new SWITCH_STMT to the stack. */
6496 cs = XNEW (struct c_switch);
6497 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6498 orig_type);
6499 cs->orig_type = orig_type;
6500 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6501 cs->next = c_switch_stack;
6502 c_switch_stack = cs;
6504 return add_stmt (cs->switch_stmt);
6507 /* Process a case label. */
6509 tree
6510 do_case (tree low_value, tree high_value)
6512 tree label = NULL_TREE;
6514 if (c_switch_stack)
6516 label = c_add_case_label (c_switch_stack->cases,
6517 SWITCH_COND (c_switch_stack->switch_stmt),
6518 c_switch_stack->orig_type,
6519 low_value, high_value);
6520 if (label == error_mark_node)
6521 label = NULL_TREE;
6523 else if (low_value)
6524 error ("case label not within a switch statement");
6525 else
6526 error ("%<default%> label not within a switch statement");
6528 return label;
6531 /* Finish the switch statement. */
6533 void
6534 c_finish_case (tree body)
6536 struct c_switch *cs = c_switch_stack;
6538 SWITCH_BODY (cs->switch_stmt) = body;
6540 /* Emit warnings as needed. */
6541 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6543 /* Pop the stack. */
6544 c_switch_stack = cs->next;
6545 splay_tree_delete (cs->cases);
6546 XDELETE (cs);
6549 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6550 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6551 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6552 statement, and was not surrounded with parenthesis. */
6554 void
6555 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6556 tree else_block, bool nested_if)
6558 tree stmt;
6560 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6561 if (warn_parentheses && nested_if && else_block == NULL)
6563 tree inner_if = then_block;
6565 /* We know from the grammar productions that there is an IF nested
6566 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6567 it might not be exactly THEN_BLOCK, but should be the last
6568 non-container statement within. */
6569 while (1)
6570 switch (TREE_CODE (inner_if))
6572 case COND_EXPR:
6573 goto found;
6574 case BIND_EXPR:
6575 inner_if = BIND_EXPR_BODY (inner_if);
6576 break;
6577 case STATEMENT_LIST:
6578 inner_if = expr_last (then_block);
6579 break;
6580 case TRY_FINALLY_EXPR:
6581 case TRY_CATCH_EXPR:
6582 inner_if = TREE_OPERAND (inner_if, 0);
6583 break;
6584 default:
6585 gcc_unreachable ();
6587 found:
6589 if (COND_EXPR_ELSE (inner_if))
6590 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6591 &if_locus);
6594 /* Diagnose ";" via the special empty statement node that we create. */
6595 if (extra_warnings)
6597 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6599 if (!else_block)
6600 warning ("%Hempty body in an if-statement",
6601 EXPR_LOCUS (then_block));
6602 then_block = alloc_stmt_list ();
6604 if (else_block
6605 && TREE_CODE (else_block) == NOP_EXPR
6606 && !TREE_TYPE (else_block))
6608 warning ("%Hempty body in an else-statement",
6609 EXPR_LOCUS (else_block));
6610 else_block = alloc_stmt_list ();
6614 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6615 SET_EXPR_LOCATION (stmt, if_locus);
6616 add_stmt (stmt);
6619 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6620 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6621 is false for DO loops. INCR is the FOR increment expression. BODY is
6622 the statement controlled by the loop. BLAB is the break label. CLAB is
6623 the continue label. Everything is allowed to be NULL. */
6625 void
6626 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6627 tree blab, tree clab, bool cond_is_first)
6629 tree entry = NULL, exit = NULL, t;
6631 /* Detect do { ... } while (0) and don't generate loop construct. */
6632 if (cond && !cond_is_first && integer_zerop (cond))
6633 cond = NULL;
6634 if (cond_is_first || cond)
6636 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6638 /* If we have an exit condition, then we build an IF with gotos either
6639 out of the loop, or to the top of it. If there's no exit condition,
6640 then we just build a jump back to the top. */
6641 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6643 if (cond)
6645 /* Canonicalize the loop condition to the end. This means
6646 generating a branch to the loop condition. Reuse the
6647 continue label, if possible. */
6648 if (cond_is_first)
6650 if (incr || !clab)
6652 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6653 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6655 else
6656 t = build1 (GOTO_EXPR, void_type_node, clab);
6657 SET_EXPR_LOCATION (t, start_locus);
6658 add_stmt (t);
6661 t = build_and_jump (&blab);
6662 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6663 exit = fold (exit);
6664 if (cond_is_first)
6665 SET_EXPR_LOCATION (exit, start_locus);
6666 else
6667 SET_EXPR_LOCATION (exit, input_location);
6670 add_stmt (top);
6673 if (body)
6674 add_stmt (body);
6675 if (clab)
6676 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6677 if (incr)
6678 add_stmt (incr);
6679 if (entry)
6680 add_stmt (entry);
6681 if (exit)
6682 add_stmt (exit);
6683 if (blab)
6684 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6687 tree
6688 c_finish_bc_stmt (tree *label_p, bool is_break)
6690 tree label = *label_p;
6692 if (!label)
6693 *label_p = label = create_artificial_label ();
6694 else if (TREE_CODE (label) != LABEL_DECL)
6696 if (is_break)
6697 error ("break statement not within loop or switch");
6698 else
6699 error ("continue statement not within a loop");
6700 return NULL_TREE;
6703 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6706 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6708 static void
6709 emit_side_effect_warnings (tree expr)
6711 if (expr == error_mark_node)
6713 else if (!TREE_SIDE_EFFECTS (expr))
6715 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6716 warning ("%Hstatement with no effect",
6717 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6719 else if (warn_unused_value)
6720 warn_if_unused_value (expr, input_location);
6723 /* Process an expression as if it were a complete statement. Emit
6724 diagnostics, but do not call ADD_STMT. */
6726 tree
6727 c_process_expr_stmt (tree expr)
6729 if (!expr)
6730 return NULL_TREE;
6732 /* Do default conversion if safe and possibly important,
6733 in case within ({...}). */
6734 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6735 && (flag_isoc99 || lvalue_p (expr)))
6736 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6737 expr = default_conversion (expr);
6739 if (warn_sequence_point)
6740 verify_sequence_points (expr);
6742 if (TREE_TYPE (expr) != error_mark_node
6743 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6744 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6745 error ("expression statement has incomplete type");
6747 /* If we're not processing a statement expression, warn about unused values.
6748 Warnings for statement expressions will be emitted later, once we figure
6749 out which is the result. */
6750 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6751 && (extra_warnings || warn_unused_value))
6752 emit_side_effect_warnings (expr);
6754 /* If the expression is not of a type to which we cannot assign a line
6755 number, wrap the thing in a no-op NOP_EXPR. */
6756 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6757 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6759 if (EXPR_P (expr))
6760 SET_EXPR_LOCATION (expr, input_location);
6762 return expr;
6765 /* Emit an expression as a statement. */
6767 tree
6768 c_finish_expr_stmt (tree expr)
6770 if (expr)
6771 return add_stmt (c_process_expr_stmt (expr));
6772 else
6773 return NULL;
6776 /* Do the opposite and emit a statement as an expression. To begin,
6777 create a new binding level and return it. */
6779 tree
6780 c_begin_stmt_expr (void)
6782 tree ret;
6784 /* We must force a BLOCK for this level so that, if it is not expanded
6785 later, there is a way to turn off the entire subtree of blocks that
6786 are contained in it. */
6787 keep_next_level ();
6788 ret = c_begin_compound_stmt (true);
6790 /* Mark the current statement list as belonging to a statement list. */
6791 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6793 return ret;
6796 tree
6797 c_finish_stmt_expr (tree body)
6799 tree last, type, tmp, val;
6800 tree *last_p;
6802 body = c_end_compound_stmt (body, true);
6804 /* Locate the last statement in BODY. See c_end_compound_stmt
6805 about always returning a BIND_EXPR. */
6806 last_p = &BIND_EXPR_BODY (body);
6807 last = BIND_EXPR_BODY (body);
6809 continue_searching:
6810 if (TREE_CODE (last) == STATEMENT_LIST)
6812 tree_stmt_iterator i;
6814 /* This can happen with degenerate cases like ({ }). No value. */
6815 if (!TREE_SIDE_EFFECTS (last))
6816 return body;
6818 /* If we're supposed to generate side effects warnings, process
6819 all of the statements except the last. */
6820 if (extra_warnings || warn_unused_value)
6822 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6823 emit_side_effect_warnings (tsi_stmt (i));
6825 else
6826 i = tsi_last (last);
6827 last_p = tsi_stmt_ptr (i);
6828 last = *last_p;
6831 /* If the end of the list is exception related, then the list was split
6832 by a call to push_cleanup. Continue searching. */
6833 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6834 || TREE_CODE (last) == TRY_CATCH_EXPR)
6836 last_p = &TREE_OPERAND (last, 0);
6837 last = *last_p;
6838 goto continue_searching;
6841 /* In the case that the BIND_EXPR is not necessary, return the
6842 expression out from inside it. */
6843 if (last == error_mark_node
6844 || (last == BIND_EXPR_BODY (body)
6845 && BIND_EXPR_VARS (body) == NULL))
6846 return last;
6848 /* Extract the type of said expression. */
6849 type = TREE_TYPE (last);
6851 /* If we're not returning a value at all, then the BIND_EXPR that
6852 we already have is a fine expression to return. */
6853 if (!type || VOID_TYPE_P (type))
6854 return body;
6856 /* Now that we've located the expression containing the value, it seems
6857 silly to make voidify_wrapper_expr repeat the process. Create a
6858 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6859 tmp = create_tmp_var_raw (type, NULL);
6861 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6862 tree_expr_nonnegative_p giving up immediately. */
6863 val = last;
6864 if (TREE_CODE (val) == NOP_EXPR
6865 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6866 val = TREE_OPERAND (val, 0);
6868 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6869 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6871 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6874 /* Begin and end compound statements. This is as simple as pushing
6875 and popping new statement lists from the tree. */
6877 tree
6878 c_begin_compound_stmt (bool do_scope)
6880 tree stmt = push_stmt_list ();
6881 if (do_scope)
6882 push_scope ();
6883 return stmt;
6886 tree
6887 c_end_compound_stmt (tree stmt, bool do_scope)
6889 tree block = NULL;
6891 if (do_scope)
6893 if (c_dialect_objc ())
6894 objc_clear_super_receiver ();
6895 block = pop_scope ();
6898 stmt = pop_stmt_list (stmt);
6899 stmt = c_build_bind_expr (block, stmt);
6901 /* If this compound statement is nested immediately inside a statement
6902 expression, then force a BIND_EXPR to be created. Otherwise we'll
6903 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6904 STATEMENT_LISTs merge, and thus we can lose track of what statement
6905 was really last. */
6906 if (cur_stmt_list
6907 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6908 && TREE_CODE (stmt) != BIND_EXPR)
6910 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6911 TREE_SIDE_EFFECTS (stmt) = 1;
6914 return stmt;
6917 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6918 when the current scope is exited. EH_ONLY is true when this is not
6919 meant to apply to normal control flow transfer. */
6921 void
6922 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
6924 enum tree_code code;
6925 tree stmt, list;
6926 bool stmt_expr;
6928 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
6929 stmt = build_stmt (code, NULL, cleanup);
6930 add_stmt (stmt);
6931 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
6932 list = push_stmt_list ();
6933 TREE_OPERAND (stmt, 0) = list;
6934 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
6937 /* Build a binary-operation expression without default conversions.
6938 CODE is the kind of expression to build.
6939 This function differs from `build' in several ways:
6940 the data type of the result is computed and recorded in it,
6941 warnings are generated if arg data types are invalid,
6942 special handling for addition and subtraction of pointers is known,
6943 and some optimization is done (operations on narrow ints
6944 are done in the narrower type when that gives the same result).
6945 Constant folding is also done before the result is returned.
6947 Note that the operands will never have enumeral types, or function
6948 or array types, because either they will have the default conversions
6949 performed or they have both just been converted to some other type in which
6950 the arithmetic is to be done. */
6952 tree
6953 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6954 int convert_p)
6956 tree type0, type1;
6957 enum tree_code code0, code1;
6958 tree op0, op1;
6960 /* Expression code to give to the expression when it is built.
6961 Normally this is CODE, which is what the caller asked for,
6962 but in some special cases we change it. */
6963 enum tree_code resultcode = code;
6965 /* Data type in which the computation is to be performed.
6966 In the simplest cases this is the common type of the arguments. */
6967 tree result_type = NULL;
6969 /* Nonzero means operands have already been type-converted
6970 in whatever way is necessary.
6971 Zero means they need to be converted to RESULT_TYPE. */
6972 int converted = 0;
6974 /* Nonzero means create the expression with this type, rather than
6975 RESULT_TYPE. */
6976 tree build_type = 0;
6978 /* Nonzero means after finally constructing the expression
6979 convert it to this type. */
6980 tree final_type = 0;
6982 /* Nonzero if this is an operation like MIN or MAX which can
6983 safely be computed in short if both args are promoted shorts.
6984 Also implies COMMON.
6985 -1 indicates a bitwise operation; this makes a difference
6986 in the exact conditions for when it is safe to do the operation
6987 in a narrower mode. */
6988 int shorten = 0;
6990 /* Nonzero if this is a comparison operation;
6991 if both args are promoted shorts, compare the original shorts.
6992 Also implies COMMON. */
6993 int short_compare = 0;
6995 /* Nonzero if this is a right-shift operation, which can be computed on the
6996 original short and then promoted if the operand is a promoted short. */
6997 int short_shift = 0;
6999 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7000 int common = 0;
7002 if (convert_p)
7004 op0 = default_conversion (orig_op0);
7005 op1 = default_conversion (orig_op1);
7007 else
7009 op0 = orig_op0;
7010 op1 = orig_op1;
7013 type0 = TREE_TYPE (op0);
7014 type1 = TREE_TYPE (op1);
7016 /* The expression codes of the data types of the arguments tell us
7017 whether the arguments are integers, floating, pointers, etc. */
7018 code0 = TREE_CODE (type0);
7019 code1 = TREE_CODE (type1);
7021 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7022 STRIP_TYPE_NOPS (op0);
7023 STRIP_TYPE_NOPS (op1);
7025 /* If an error was already reported for one of the arguments,
7026 avoid reporting another error. */
7028 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7029 return error_mark_node;
7031 switch (code)
7033 case PLUS_EXPR:
7034 /* Handle the pointer + int case. */
7035 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7036 return pointer_int_sum (PLUS_EXPR, op0, op1);
7037 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7038 return pointer_int_sum (PLUS_EXPR, op1, op0);
7039 else
7040 common = 1;
7041 break;
7043 case MINUS_EXPR:
7044 /* Subtraction of two similar pointers.
7045 We must subtract them as integers, then divide by object size. */
7046 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7047 && comp_target_types (type0, type1, 1))
7048 return pointer_diff (op0, op1);
7049 /* Handle pointer minus int. Just like pointer plus int. */
7050 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7051 return pointer_int_sum (MINUS_EXPR, op0, op1);
7052 else
7053 common = 1;
7054 break;
7056 case MULT_EXPR:
7057 common = 1;
7058 break;
7060 case TRUNC_DIV_EXPR:
7061 case CEIL_DIV_EXPR:
7062 case FLOOR_DIV_EXPR:
7063 case ROUND_DIV_EXPR:
7064 case EXACT_DIV_EXPR:
7065 /* Floating point division by zero is a legitimate way to obtain
7066 infinities and NaNs. */
7067 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7068 warning ("division by zero");
7070 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7071 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7072 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7073 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7075 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7076 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7077 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7078 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7080 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7081 resultcode = RDIV_EXPR;
7082 else
7083 /* Although it would be tempting to shorten always here, that
7084 loses on some targets, since the modulo instruction is
7085 undefined if the quotient can't be represented in the
7086 computation mode. We shorten only if unsigned or if
7087 dividing by something we know != -1. */
7088 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7089 || (TREE_CODE (op1) == INTEGER_CST
7090 && ! integer_all_onesp (op1)));
7091 common = 1;
7093 break;
7095 case BIT_AND_EXPR:
7096 case BIT_IOR_EXPR:
7097 case BIT_XOR_EXPR:
7098 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7099 shorten = -1;
7100 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7101 common = 1;
7102 break;
7104 case TRUNC_MOD_EXPR:
7105 case FLOOR_MOD_EXPR:
7106 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7107 warning ("division by zero");
7109 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7111 /* Although it would be tempting to shorten always here, that loses
7112 on some targets, since the modulo instruction is undefined if the
7113 quotient can't be represented in the computation mode. We shorten
7114 only if unsigned or if dividing by something we know != -1. */
7115 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7116 || (TREE_CODE (op1) == INTEGER_CST
7117 && ! integer_all_onesp (op1)));
7118 common = 1;
7120 break;
7122 case TRUTH_ANDIF_EXPR:
7123 case TRUTH_ORIF_EXPR:
7124 case TRUTH_AND_EXPR:
7125 case TRUTH_OR_EXPR:
7126 case TRUTH_XOR_EXPR:
7127 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7128 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7129 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7130 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7132 /* Result of these operations is always an int,
7133 but that does not mean the operands should be
7134 converted to ints! */
7135 result_type = integer_type_node;
7136 op0 = lang_hooks.truthvalue_conversion (op0);
7137 op1 = lang_hooks.truthvalue_conversion (op1);
7138 converted = 1;
7140 break;
7142 /* Shift operations: result has same type as first operand;
7143 always convert second operand to int.
7144 Also set SHORT_SHIFT if shifting rightward. */
7146 case RSHIFT_EXPR:
7147 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7149 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7151 if (tree_int_cst_sgn (op1) < 0)
7152 warning ("right shift count is negative");
7153 else
7155 if (! integer_zerop (op1))
7156 short_shift = 1;
7158 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7159 warning ("right shift count >= width of type");
7163 /* Use the type of the value to be shifted. */
7164 result_type = type0;
7165 /* Convert the shift-count to an integer, regardless of size
7166 of value being shifted. */
7167 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7168 op1 = convert (integer_type_node, op1);
7169 /* Avoid converting op1 to result_type later. */
7170 converted = 1;
7172 break;
7174 case LSHIFT_EXPR:
7175 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7177 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7179 if (tree_int_cst_sgn (op1) < 0)
7180 warning ("left shift count is negative");
7182 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7183 warning ("left shift count >= width of type");
7186 /* Use the type of the value to be shifted. */
7187 result_type = type0;
7188 /* Convert the shift-count to an integer, regardless of size
7189 of value being shifted. */
7190 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7191 op1 = convert (integer_type_node, op1);
7192 /* Avoid converting op1 to result_type later. */
7193 converted = 1;
7195 break;
7197 case RROTATE_EXPR:
7198 case LROTATE_EXPR:
7199 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7201 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7203 if (tree_int_cst_sgn (op1) < 0)
7204 warning ("shift count is negative");
7205 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7206 warning ("shift count >= width of type");
7209 /* Use the type of the value to be shifted. */
7210 result_type = type0;
7211 /* Convert the shift-count to an integer, regardless of size
7212 of value being shifted. */
7213 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7214 op1 = convert (integer_type_node, op1);
7215 /* Avoid converting op1 to result_type later. */
7216 converted = 1;
7218 break;
7220 case EQ_EXPR:
7221 case NE_EXPR:
7222 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7223 warning ("comparing floating point with == or != is unsafe");
7224 /* Result of comparison is always int,
7225 but don't convert the args to int! */
7226 build_type = integer_type_node;
7227 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7228 || code0 == COMPLEX_TYPE)
7229 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7230 || code1 == COMPLEX_TYPE))
7231 short_compare = 1;
7232 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7234 tree tt0 = TREE_TYPE (type0);
7235 tree tt1 = TREE_TYPE (type1);
7236 /* Anything compares with void *. void * compares with anything.
7237 Otherwise, the targets must be compatible
7238 and both must be object or both incomplete. */
7239 if (comp_target_types (type0, type1, 1))
7240 result_type = common_pointer_type (type0, type1);
7241 else if (VOID_TYPE_P (tt0))
7243 /* op0 != orig_op0 detects the case of something
7244 whose value is 0 but which isn't a valid null ptr const. */
7245 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7246 && TREE_CODE (tt1) == FUNCTION_TYPE)
7247 pedwarn ("ISO C forbids comparison of %<void *%>"
7248 " with function pointer");
7250 else if (VOID_TYPE_P (tt1))
7252 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7253 && TREE_CODE (tt0) == FUNCTION_TYPE)
7254 pedwarn ("ISO C forbids comparison of %<void *%>"
7255 " with function pointer");
7257 else
7258 pedwarn ("comparison of distinct pointer types lacks a cast");
7260 if (result_type == NULL_TREE)
7261 result_type = ptr_type_node;
7263 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7264 && integer_zerop (op1))
7265 result_type = type0;
7266 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7267 && integer_zerop (op0))
7268 result_type = type1;
7269 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7271 result_type = type0;
7272 pedwarn ("comparison between pointer and integer");
7274 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7276 result_type = type1;
7277 pedwarn ("comparison between pointer and integer");
7279 break;
7281 case MAX_EXPR:
7282 case MIN_EXPR:
7283 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7284 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7285 shorten = 1;
7286 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7288 if (comp_target_types (type0, type1, 1))
7290 result_type = common_pointer_type (type0, type1);
7291 if (pedantic
7292 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7293 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7295 else
7297 result_type = ptr_type_node;
7298 pedwarn ("comparison of distinct pointer types lacks a cast");
7301 break;
7303 case LE_EXPR:
7304 case GE_EXPR:
7305 case LT_EXPR:
7306 case GT_EXPR:
7307 build_type = integer_type_node;
7308 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7309 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7310 short_compare = 1;
7311 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7313 if (comp_target_types (type0, type1, 1))
7315 result_type = common_pointer_type (type0, type1);
7316 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7317 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7318 pedwarn ("comparison of complete and incomplete pointers");
7319 else if (pedantic
7320 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7321 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7323 else
7325 result_type = ptr_type_node;
7326 pedwarn ("comparison of distinct pointer types lacks a cast");
7329 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7330 && integer_zerop (op1))
7332 result_type = type0;
7333 if (pedantic || extra_warnings)
7334 pedwarn ("ordered comparison of pointer with integer zero");
7336 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7337 && integer_zerop (op0))
7339 result_type = type1;
7340 if (pedantic)
7341 pedwarn ("ordered comparison of pointer with integer zero");
7343 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7345 result_type = type0;
7346 pedwarn ("comparison between pointer and integer");
7348 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7350 result_type = type1;
7351 pedwarn ("comparison between pointer and integer");
7353 break;
7355 case UNORDERED_EXPR:
7356 case ORDERED_EXPR:
7357 case UNLT_EXPR:
7358 case UNLE_EXPR:
7359 case UNGT_EXPR:
7360 case UNGE_EXPR:
7361 case UNEQ_EXPR:
7362 case LTGT_EXPR:
7363 build_type = integer_type_node;
7364 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7366 error ("unordered comparison on non-floating point argument");
7367 return error_mark_node;
7369 common = 1;
7370 break;
7372 default:
7373 break;
7376 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7377 return error_mark_node;
7379 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7380 || code0 == VECTOR_TYPE)
7382 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7383 || code1 == VECTOR_TYPE))
7385 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7387 if (shorten || common || short_compare)
7388 result_type = common_type (type0, type1);
7390 /* For certain operations (which identify themselves by shorten != 0)
7391 if both args were extended from the same smaller type,
7392 do the arithmetic in that type and then extend.
7394 shorten !=0 and !=1 indicates a bitwise operation.
7395 For them, this optimization is safe only if
7396 both args are zero-extended or both are sign-extended.
7397 Otherwise, we might change the result.
7398 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7399 but calculated in (unsigned short) it would be (unsigned short)-1. */
7401 if (shorten && none_complex)
7403 int unsigned0, unsigned1;
7404 tree arg0 = get_narrower (op0, &unsigned0);
7405 tree arg1 = get_narrower (op1, &unsigned1);
7406 /* UNS is 1 if the operation to be done is an unsigned one. */
7407 int uns = TYPE_UNSIGNED (result_type);
7408 tree type;
7410 final_type = result_type;
7412 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7413 but it *requires* conversion to FINAL_TYPE. */
7415 if ((TYPE_PRECISION (TREE_TYPE (op0))
7416 == TYPE_PRECISION (TREE_TYPE (arg0)))
7417 && TREE_TYPE (op0) != final_type)
7418 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7419 if ((TYPE_PRECISION (TREE_TYPE (op1))
7420 == TYPE_PRECISION (TREE_TYPE (arg1)))
7421 && TREE_TYPE (op1) != final_type)
7422 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7424 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7426 /* For bitwise operations, signedness of nominal type
7427 does not matter. Consider only how operands were extended. */
7428 if (shorten == -1)
7429 uns = unsigned0;
7431 /* Note that in all three cases below we refrain from optimizing
7432 an unsigned operation on sign-extended args.
7433 That would not be valid. */
7435 /* Both args variable: if both extended in same way
7436 from same width, do it in that width.
7437 Do it unsigned if args were zero-extended. */
7438 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7439 < TYPE_PRECISION (result_type))
7440 && (TYPE_PRECISION (TREE_TYPE (arg1))
7441 == TYPE_PRECISION (TREE_TYPE (arg0)))
7442 && unsigned0 == unsigned1
7443 && (unsigned0 || !uns))
7444 result_type
7445 = c_common_signed_or_unsigned_type
7446 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7447 else if (TREE_CODE (arg0) == INTEGER_CST
7448 && (unsigned1 || !uns)
7449 && (TYPE_PRECISION (TREE_TYPE (arg1))
7450 < TYPE_PRECISION (result_type))
7451 && (type
7452 = c_common_signed_or_unsigned_type (unsigned1,
7453 TREE_TYPE (arg1)),
7454 int_fits_type_p (arg0, type)))
7455 result_type = type;
7456 else if (TREE_CODE (arg1) == INTEGER_CST
7457 && (unsigned0 || !uns)
7458 && (TYPE_PRECISION (TREE_TYPE (arg0))
7459 < TYPE_PRECISION (result_type))
7460 && (type
7461 = c_common_signed_or_unsigned_type (unsigned0,
7462 TREE_TYPE (arg0)),
7463 int_fits_type_p (arg1, type)))
7464 result_type = type;
7467 /* Shifts can be shortened if shifting right. */
7469 if (short_shift)
7471 int unsigned_arg;
7472 tree arg0 = get_narrower (op0, &unsigned_arg);
7474 final_type = result_type;
7476 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7477 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7479 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7480 /* We can shorten only if the shift count is less than the
7481 number of bits in the smaller type size. */
7482 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7483 /* We cannot drop an unsigned shift after sign-extension. */
7484 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7486 /* Do an unsigned shift if the operand was zero-extended. */
7487 result_type
7488 = c_common_signed_or_unsigned_type (unsigned_arg,
7489 TREE_TYPE (arg0));
7490 /* Convert value-to-be-shifted to that type. */
7491 if (TREE_TYPE (op0) != result_type)
7492 op0 = convert (result_type, op0);
7493 converted = 1;
7497 /* Comparison operations are shortened too but differently.
7498 They identify themselves by setting short_compare = 1. */
7500 if (short_compare)
7502 /* Don't write &op0, etc., because that would prevent op0
7503 from being kept in a register.
7504 Instead, make copies of the our local variables and
7505 pass the copies by reference, then copy them back afterward. */
7506 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7507 enum tree_code xresultcode = resultcode;
7508 tree val
7509 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7511 if (val != 0)
7512 return val;
7514 op0 = xop0, op1 = xop1;
7515 converted = 1;
7516 resultcode = xresultcode;
7518 if (warn_sign_compare && skip_evaluation == 0)
7520 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7521 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7522 int unsignedp0, unsignedp1;
7523 tree primop0 = get_narrower (op0, &unsignedp0);
7524 tree primop1 = get_narrower (op1, &unsignedp1);
7526 xop0 = orig_op0;
7527 xop1 = orig_op1;
7528 STRIP_TYPE_NOPS (xop0);
7529 STRIP_TYPE_NOPS (xop1);
7531 /* Give warnings for comparisons between signed and unsigned
7532 quantities that may fail.
7534 Do the checking based on the original operand trees, so that
7535 casts will be considered, but default promotions won't be.
7537 Do not warn if the comparison is being done in a signed type,
7538 since the signed type will only be chosen if it can represent
7539 all the values of the unsigned type. */
7540 if (! TYPE_UNSIGNED (result_type))
7541 /* OK */;
7542 /* Do not warn if both operands are the same signedness. */
7543 else if (op0_signed == op1_signed)
7544 /* OK */;
7545 else
7547 tree sop, uop;
7549 if (op0_signed)
7550 sop = xop0, uop = xop1;
7551 else
7552 sop = xop1, uop = xop0;
7554 /* Do not warn if the signed quantity is an
7555 unsuffixed integer literal (or some static
7556 constant expression involving such literals or a
7557 conditional expression involving such literals)
7558 and it is non-negative. */
7559 if (tree_expr_nonnegative_p (sop))
7560 /* OK */;
7561 /* Do not warn if the comparison is an equality operation,
7562 the unsigned quantity is an integral constant, and it
7563 would fit in the result if the result were signed. */
7564 else if (TREE_CODE (uop) == INTEGER_CST
7565 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7566 && int_fits_type_p
7567 (uop, c_common_signed_type (result_type)))
7568 /* OK */;
7569 /* Do not warn if the unsigned quantity is an enumeration
7570 constant and its maximum value would fit in the result
7571 if the result were signed. */
7572 else if (TREE_CODE (uop) == INTEGER_CST
7573 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7574 && int_fits_type_p
7575 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7576 c_common_signed_type (result_type)))
7577 /* OK */;
7578 else
7579 warning ("comparison between signed and unsigned");
7582 /* Warn if two unsigned values are being compared in a size
7583 larger than their original size, and one (and only one) is the
7584 result of a `~' operator. This comparison will always fail.
7586 Also warn if one operand is a constant, and the constant
7587 does not have all bits set that are set in the ~ operand
7588 when it is extended. */
7590 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7591 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7593 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7594 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7595 &unsignedp0);
7596 else
7597 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7598 &unsignedp1);
7600 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7602 tree primop;
7603 HOST_WIDE_INT constant, mask;
7604 int unsignedp, bits;
7606 if (host_integerp (primop0, 0))
7608 primop = primop1;
7609 unsignedp = unsignedp1;
7610 constant = tree_low_cst (primop0, 0);
7612 else
7614 primop = primop0;
7615 unsignedp = unsignedp0;
7616 constant = tree_low_cst (primop1, 0);
7619 bits = TYPE_PRECISION (TREE_TYPE (primop));
7620 if (bits < TYPE_PRECISION (result_type)
7621 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7623 mask = (~ (HOST_WIDE_INT) 0) << bits;
7624 if ((mask & constant) != mask)
7625 warning ("comparison of promoted ~unsigned with constant");
7628 else if (unsignedp0 && unsignedp1
7629 && (TYPE_PRECISION (TREE_TYPE (primop0))
7630 < TYPE_PRECISION (result_type))
7631 && (TYPE_PRECISION (TREE_TYPE (primop1))
7632 < TYPE_PRECISION (result_type)))
7633 warning ("comparison of promoted ~unsigned with unsigned");
7639 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7640 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7641 Then the expression will be built.
7642 It will be given type FINAL_TYPE if that is nonzero;
7643 otherwise, it will be given type RESULT_TYPE. */
7645 if (!result_type)
7647 binary_op_error (code);
7648 return error_mark_node;
7651 if (! converted)
7653 if (TREE_TYPE (op0) != result_type)
7654 op0 = convert (result_type, op0);
7655 if (TREE_TYPE (op1) != result_type)
7656 op1 = convert (result_type, op1);
7658 /* This can happen if one operand has a vector type, and the other
7659 has a different type. */
7660 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7661 return error_mark_node;
7664 if (build_type == NULL_TREE)
7665 build_type = result_type;
7668 tree result = build2 (resultcode, build_type, op0, op1);
7670 /* Treat expressions in initializers specially as they can't trap. */
7671 result = require_constant_value ? fold_initializer (result)
7672 : fold (result);
7674 if (final_type != 0)
7675 result = convert (final_type, result);
7676 return result;