* config/xtensa/lib1funcs.asm (__mulsi3): Use symbolic name for ACCLO.
[official-gcc.git] / gcc / c-typeck.c
blobd5046d5f425ea133a4bc6645faf51821843f5784
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 /* Nonzero if we've already printed a "missing braces around initializer"
68 message within this initializer. */
69 static int missing_braces_mentioned;
71 static int require_constant_value;
72 static int require_constant_elements;
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (tree, tree);
76 static int comp_target_types (tree, tree, int);
77 static int function_types_compatible_p (tree, tree);
78 static int type_lists_compatible_p (tree, tree);
79 static tree decl_constant_value_for_broken_optimization (tree);
80 static tree default_function_array_conversion (tree);
81 static tree lookup_field (tree, tree);
82 static tree convert_arguments (tree, tree, tree, tree);
83 static tree pointer_diff (tree, tree);
84 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
85 int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static void push_array_bounds (int);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (const char *);
93 static tree digest_init (tree, tree, bool, int);
94 static void output_init_element (tree, bool, tree, tree, int);
95 static void output_pending_init_elements (int);
96 static int set_designator (int);
97 static void push_range_stack (tree);
98 static void add_pending_init (tree, tree);
99 static void set_nonincremental_init (void);
100 static void set_nonincremental_init_from_string (tree);
101 static tree find_init_member (tree);
102 static void readonly_error (tree, enum lvalue_use);
103 static int lvalue_or_else (tree, enum lvalue_use);
104 static int lvalue_p (tree);
105 static void record_maybe_used_decl (tree);
107 /* Do `exp = require_complete_type (exp);' to make sure exp
108 does not have an incomplete type. (That includes void types.) */
110 tree
111 require_complete_type (tree value)
113 tree type = TREE_TYPE (value);
115 if (value == error_mark_node || type == error_mark_node)
116 return error_mark_node;
118 /* First, detect a valid value with a complete type. */
119 if (COMPLETE_TYPE_P (type))
120 return value;
122 c_incomplete_type_error (value, type);
123 return error_mark_node;
126 /* Print an error message for invalid use of an incomplete type.
127 VALUE is the expression that was used (or 0 if that isn't known)
128 and TYPE is the type that was invalid. */
130 void
131 c_incomplete_type_error (tree value, tree type)
133 const char *type_code_string;
135 /* Avoid duplicate error message. */
136 if (TREE_CODE (type) == ERROR_MARK)
137 return;
139 if (value != 0 && (TREE_CODE (value) == VAR_DECL
140 || TREE_CODE (value) == PARM_DECL))
141 error ("%qD has an incomplete type", value);
142 else
144 retry:
145 /* We must print an error message. Be clever about what it says. */
147 switch (TREE_CODE (type))
149 case RECORD_TYPE:
150 type_code_string = "struct";
151 break;
153 case UNION_TYPE:
154 type_code_string = "union";
155 break;
157 case ENUMERAL_TYPE:
158 type_code_string = "enum";
159 break;
161 case VOID_TYPE:
162 error ("invalid use of void expression");
163 return;
165 case ARRAY_TYPE:
166 if (TYPE_DOMAIN (type))
168 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
170 error ("invalid use of flexible array member");
171 return;
173 type = TREE_TYPE (type);
174 goto retry;
176 error ("invalid use of array with unspecified bounds");
177 return;
179 default:
180 gcc_unreachable ();
183 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
184 error ("invalid use of undefined type %<%s %E%>",
185 type_code_string, TYPE_NAME (type));
186 else
187 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
188 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
192 /* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
195 tree
196 c_type_promotes_to (tree type)
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
201 if (c_promoting_integer_type_p (type))
203 /* Preserve unsignedness if not really getting any wider. */
204 if (TYPE_UNSIGNED (type)
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
210 return type;
213 /* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
216 static tree
217 qualify_type (tree type, tree like)
219 return c_build_qualified_type (type,
220 TYPE_QUALS (type) | TYPE_QUALS (like));
223 /* Return the composite type of two compatible types.
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
229 tree
230 composite_type (tree t1, tree t2)
232 enum tree_code code1;
233 enum tree_code code2;
234 tree attributes;
236 /* Save time if the two types are the same. */
238 if (t1 == t2) return t1;
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
249 /* Merge the attributes. */
250 attributes = targetm.merge_type_attributes (t1, t2);
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
262 gcc_assert (code1 == code2);
264 switch (code1)
266 case POINTER_TYPE:
267 /* For two pointers, do this recursively on the target type. */
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
277 case ARRAY_TYPE:
279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
280 int quals;
281 tree unqual_elt;
283 /* We should not have any type quals on arrays at all. */
284 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
286 /* Save space: see if the result is identical to one of the args. */
287 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
288 return build_type_attribute_variant (t1, attributes);
289 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
290 return build_type_attribute_variant (t2, attributes);
292 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t1, attributes);
294 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t2, attributes);
297 /* Merge the element types, and have a size if either arg has
298 one. We may have qualifiers on the element types. To set
299 up TYPE_MAIN_VARIANT correctly, we need to form the
300 composite of the unqualified types and add the qualifiers
301 back at the end. */
302 quals = TYPE_QUALS (strip_array_types (elt));
303 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
304 t1 = build_array_type (unqual_elt,
305 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
306 t1 = c_build_qualified_type (t1, quals);
307 return build_type_attribute_variant (t1, attributes);
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
334 if (TYPE_ARG_TYPES (t2) == 0)
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
347 len = list_length (p1);
348 newargs = 0;
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
353 n = newargs;
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
365 if (TREE_VALUE (p2) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
377 tree memb;
378 tree mv2 = TREE_VALUE (p2);
379 if (mv2 && mv2 != error_mark_node
380 && TREE_CODE (mv2) != ARRAY_TYPE)
381 mv2 = TYPE_MAIN_VARIANT (mv2);
382 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
383 memb; memb = TREE_CHAIN (memb))
385 tree mv3 = TREE_TYPE (memb);
386 if (mv3 && mv3 != error_mark_node
387 && TREE_CODE (mv3) != ARRAY_TYPE)
388 mv3 = TYPE_MAIN_VARIANT (mv3);
389 if (comptypes (mv3, mv2))
391 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
392 TREE_VALUE (p2));
393 if (pedantic)
394 pedwarn ("function types not truly compatible in ISO C");
395 goto parm_done;
399 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
400 && TREE_VALUE (p2) != TREE_VALUE (p1))
402 tree memb;
403 tree mv1 = TREE_VALUE (p1);
404 if (mv1 && mv1 != error_mark_node
405 && TREE_CODE (mv1) != ARRAY_TYPE)
406 mv1 = TYPE_MAIN_VARIANT (mv1);
407 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
408 memb; memb = TREE_CHAIN (memb))
410 tree mv3 = TREE_TYPE (memb);
411 if (mv3 && mv3 != error_mark_node
412 && TREE_CODE (mv3) != ARRAY_TYPE)
413 mv3 = TYPE_MAIN_VARIANT (mv3);
414 if (comptypes (mv3, mv1))
416 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
417 TREE_VALUE (p1));
418 if (pedantic)
419 pedwarn ("function types not truly compatible in ISO C");
420 goto parm_done;
424 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
425 parm_done: ;
428 c_override_global_bindings_to_false = false;
429 t1 = build_function_type (valtype, newargs);
430 t1 = qualify_type (t1, t2);
431 /* ... falls through ... */
434 default:
435 return build_type_attribute_variant (t1, attributes);
440 /* Return the type of a conditional expression between pointers to
441 possibly differently qualified versions of compatible types.
443 We assume that comp_target_types has already been done and returned
444 nonzero; if that isn't so, this may crash. */
446 static tree
447 common_pointer_type (tree t1, tree t2)
449 tree attributes;
450 tree pointed_to_1, mv1;
451 tree pointed_to_2, mv2;
452 tree target;
454 /* Save time if the two types are the same. */
456 if (t1 == t2) return t1;
458 /* If one type is nonsense, use the other. */
459 if (t1 == error_mark_node)
460 return t2;
461 if (t2 == error_mark_node)
462 return t1;
464 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
465 && TREE_CODE (t2) == POINTER_TYPE);
467 /* Merge the attributes. */
468 attributes = targetm.merge_type_attributes (t1, t2);
470 /* Find the composite type of the target types, and combine the
471 qualifiers of the two types' targets. Do not lose qualifiers on
472 array element types by taking the TYPE_MAIN_VARIANT. */
473 mv1 = pointed_to_1 = TREE_TYPE (t1);
474 mv2 = pointed_to_2 = TREE_TYPE (t2);
475 if (TREE_CODE (mv1) != ARRAY_TYPE)
476 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
477 if (TREE_CODE (mv2) != ARRAY_TYPE)
478 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
479 target = composite_type (mv1, mv2);
480 t1 = build_pointer_type (c_build_qualified_type
481 (target,
482 TYPE_QUALS (pointed_to_1) |
483 TYPE_QUALS (pointed_to_2)));
484 return build_type_attribute_variant (t1, attributes);
487 /* Return the common type for two arithmetic types under the usual
488 arithmetic conversions. The default conversions have already been
489 applied, and enumerated types converted to their compatible integer
490 types. The resulting type is unqualified and has no attributes.
492 This is the type for the result of most arithmetic operations
493 if the operands have the given two types. */
495 static tree
496 c_common_type (tree t1, tree t2)
498 enum tree_code code1;
499 enum tree_code code2;
501 /* If one type is nonsense, use the other. */
502 if (t1 == error_mark_node)
503 return t2;
504 if (t2 == error_mark_node)
505 return t1;
507 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
508 t1 = TYPE_MAIN_VARIANT (t1);
510 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
511 t2 = TYPE_MAIN_VARIANT (t2);
513 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
514 t1 = build_type_attribute_variant (t1, NULL_TREE);
516 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
517 t2 = build_type_attribute_variant (t2, NULL_TREE);
519 /* Save time if the two types are the same. */
521 if (t1 == t2) return t1;
523 code1 = TREE_CODE (t1);
524 code2 = TREE_CODE (t2);
526 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
527 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
528 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
529 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
531 /* If one type is a vector type, return that type. (How the usual
532 arithmetic conversions apply to the vector types extension is not
533 precisely specified.) */
534 if (code1 == VECTOR_TYPE)
535 return t1;
537 if (code2 == VECTOR_TYPE)
538 return t2;
540 /* If one type is complex, form the common type of the non-complex
541 components, then make that complex. Use T1 or T2 if it is the
542 required type. */
543 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
545 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
546 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
547 tree subtype = c_common_type (subtype1, subtype2);
549 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
550 return t1;
551 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
552 return t2;
553 else
554 return build_complex_type (subtype);
557 /* If only one is real, use it as the result. */
559 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
560 return t1;
562 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
563 return t2;
565 /* Both real or both integers; use the one with greater precision. */
567 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
568 return t1;
569 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
570 return t2;
572 /* Same precision. Prefer long longs to longs to ints when the
573 same precision, following the C99 rules on integer type rank
574 (which are equivalent to the C90 rules for C90 types). */
576 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
577 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
578 return long_long_unsigned_type_node;
580 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
581 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
583 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
584 return long_long_unsigned_type_node;
585 else
586 return long_long_integer_type_node;
589 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
590 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
591 return long_unsigned_type_node;
593 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
594 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
596 /* But preserve unsignedness from the other type,
597 since long cannot hold all the values of an unsigned int. */
598 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
599 return long_unsigned_type_node;
600 else
601 return long_integer_type_node;
604 /* Likewise, prefer long double to double even if same size. */
605 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
606 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
607 return long_double_type_node;
609 /* Otherwise prefer the unsigned one. */
611 if (TYPE_UNSIGNED (t1))
612 return t1;
613 else
614 return t2;
617 /* Wrapper around c_common_type that is used by c-common.c. ENUMERAL_TYPEs
618 are allowed here and are converted to their compatible integer types. */
619 tree
620 common_type (tree t1, tree t2)
622 if (TREE_CODE (t1) == ENUMERAL_TYPE)
623 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
624 if (TREE_CODE (t2) == ENUMERAL_TYPE)
625 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
626 return c_common_type (t1, t2);
629 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
630 or various other operations. Return 2 if they are compatible
631 but a warning may be needed if you use them together. */
634 comptypes (tree type1, tree type2)
636 tree t1 = type1;
637 tree t2 = type2;
638 int attrval, val;
640 /* Suppress errors caused by previously reported errors. */
642 if (t1 == t2 || !t1 || !t2
643 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
644 return 1;
646 /* If either type is the internal version of sizetype, return the
647 language version. */
648 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
649 && TYPE_ORIG_SIZE_TYPE (t1))
650 t1 = TYPE_ORIG_SIZE_TYPE (t1);
652 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
653 && TYPE_ORIG_SIZE_TYPE (t2))
654 t2 = TYPE_ORIG_SIZE_TYPE (t2);
657 /* Enumerated types are compatible with integer types, but this is
658 not transitive: two enumerated types in the same translation unit
659 are compatible with each other only if they are the same type. */
661 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
662 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
663 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
664 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
666 if (t1 == t2)
667 return 1;
669 /* Different classes of types can't be compatible. */
671 if (TREE_CODE (t1) != TREE_CODE (t2))
672 return 0;
674 /* Qualifiers must match. C99 6.7.3p9 */
676 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
677 return 0;
679 /* Allow for two different type nodes which have essentially the same
680 definition. Note that we already checked for equality of the type
681 qualifiers (just above). */
683 if (TREE_CODE (t1) != ARRAY_TYPE
684 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
685 return 1;
687 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
688 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
689 return 0;
691 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
692 val = 0;
694 switch (TREE_CODE (t1))
696 case POINTER_TYPE:
697 /* We must give ObjC the first crack at comparing pointers, since
698 protocol qualifiers may be involved. */
699 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
700 break;
701 /* Do not remove mode or aliasing information. */
702 if (TYPE_MODE (t1) != TYPE_MODE (t2)
703 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
704 break;
705 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
706 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
707 break;
709 case FUNCTION_TYPE:
710 val = function_types_compatible_p (t1, t2);
711 break;
713 case ARRAY_TYPE:
715 tree d1 = TYPE_DOMAIN (t1);
716 tree d2 = TYPE_DOMAIN (t2);
717 bool d1_variable, d2_variable;
718 bool d1_zero, d2_zero;
719 val = 1;
721 /* Target types must match incl. qualifiers. */
722 if (TREE_TYPE (t1) != TREE_TYPE (t2)
723 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
724 return 0;
726 /* Sizes must match unless one is missing or variable. */
727 if (d1 == 0 || d2 == 0 || d1 == d2)
728 break;
730 d1_zero = !TYPE_MAX_VALUE (d1);
731 d2_zero = !TYPE_MAX_VALUE (d2);
733 d1_variable = (!d1_zero
734 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
735 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
736 d2_variable = (!d2_zero
737 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
738 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
740 if (d1_variable || d2_variable)
741 break;
742 if (d1_zero && d2_zero)
743 break;
744 if (d1_zero || d2_zero
745 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
746 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
747 val = 0;
749 break;
752 case RECORD_TYPE:
753 /* We are dealing with two distinct structs. In assorted Objective-C
754 corner cases, however, these can still be deemed equivalent. */
755 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
756 val = 1;
758 case ENUMERAL_TYPE:
759 case UNION_TYPE:
760 if (val != 1 && !same_translation_unit_p (t1, t2))
761 val = tagged_types_tu_compatible_p (t1, t2);
762 break;
764 case VECTOR_TYPE:
765 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
766 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
767 break;
769 default:
770 break;
772 return attrval == 2 && val == 1 ? 2 : val;
775 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
776 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
777 to 1 or 0 depending if the check of the pointer types is meant to
778 be reflexive or not (typically, assignments are not reflexive,
779 while comparisons are reflexive).
782 static int
783 comp_target_types (tree ttl, tree ttr, int reflexive)
785 int val;
786 tree mvl, mvr;
788 /* Give objc_comptypes a crack at letting these types through. */
789 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
790 return val;
792 /* Do not lose qualifiers on element types of array types that are
793 pointer targets by taking their TYPE_MAIN_VARIANT. */
794 mvl = TREE_TYPE (ttl);
795 mvr = TREE_TYPE (ttr);
796 if (TREE_CODE (mvl) != ARRAY_TYPE)
797 mvl = TYPE_MAIN_VARIANT (mvl);
798 if (TREE_CODE (mvr) != ARRAY_TYPE)
799 mvr = TYPE_MAIN_VARIANT (mvr);
800 val = comptypes (mvl, mvr);
802 if (val == 2 && pedantic)
803 pedwarn ("types are not quite compatible");
804 return val;
807 /* Subroutines of `comptypes'. */
809 /* Determine whether two trees derive from the same translation unit.
810 If the CONTEXT chain ends in a null, that tree's context is still
811 being parsed, so if two trees have context chains ending in null,
812 they're in the same translation unit. */
814 same_translation_unit_p (tree t1, tree t2)
816 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
817 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
819 case tcc_declaration:
820 t1 = DECL_CONTEXT (t1); break;
821 case tcc_type:
822 t1 = TYPE_CONTEXT (t1); break;
823 case tcc_exceptional:
824 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
825 default: gcc_unreachable ();
828 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
829 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
831 case tcc_declaration:
832 t2 = DECL_CONTEXT (t2); break;
833 case tcc_type:
834 t2 = TYPE_CONTEXT (t2); break;
835 case tcc_exceptional:
836 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
837 default: gcc_unreachable ();
840 return t1 == t2;
843 /* The C standard says that two structures in different translation
844 units are compatible with each other only if the types of their
845 fields are compatible (among other things). So, consider two copies
846 of this structure: */
848 struct tagged_tu_seen {
849 const struct tagged_tu_seen * next;
850 tree t1;
851 tree t2;
854 /* Can they be compatible with each other? We choose to break the
855 recursion by allowing those types to be compatible. */
857 static const struct tagged_tu_seen * tagged_tu_seen_base;
859 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
860 compatible. If the two types are not the same (which has been
861 checked earlier), this can only happen when multiple translation
862 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
863 rules. */
865 static int
866 tagged_types_tu_compatible_p (tree t1, tree t2)
868 tree s1, s2;
869 bool needs_warning = false;
871 /* We have to verify that the tags of the types are the same. This
872 is harder than it looks because this may be a typedef, so we have
873 to go look at the original type. It may even be a typedef of a
874 typedef...
875 In the case of compiler-created builtin structs the TYPE_DECL
876 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
877 while (TYPE_NAME (t1)
878 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
879 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
880 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
882 while (TYPE_NAME (t2)
883 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
884 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
885 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
887 /* C90 didn't have the requirement that the two tags be the same. */
888 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
889 return 0;
891 /* C90 didn't say what happened if one or both of the types were
892 incomplete; we choose to follow C99 rules here, which is that they
893 are compatible. */
894 if (TYPE_SIZE (t1) == NULL
895 || TYPE_SIZE (t2) == NULL)
896 return 1;
899 const struct tagged_tu_seen * tts_i;
900 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
901 if (tts_i->t1 == t1 && tts_i->t2 == t2)
902 return 1;
905 switch (TREE_CODE (t1))
907 case ENUMERAL_TYPE:
910 /* Speed up the case where the type values are in the same order. */
911 tree tv1 = TYPE_VALUES (t1);
912 tree tv2 = TYPE_VALUES (t2);
914 if (tv1 == tv2)
915 return 1;
917 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
919 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
920 break;
921 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
922 return 0;
925 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
926 return 1;
927 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
928 return 0;
930 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
931 return 0;
933 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
935 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
936 if (s2 == NULL
937 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
938 return 0;
940 return 1;
943 case UNION_TYPE:
945 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
946 return 0;
948 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
950 bool ok = false;
951 struct tagged_tu_seen tts;
953 tts.next = tagged_tu_seen_base;
954 tts.t1 = t1;
955 tts.t2 = t2;
956 tagged_tu_seen_base = &tts;
958 if (DECL_NAME (s1) != NULL)
959 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
960 if (DECL_NAME (s1) == DECL_NAME (s2))
962 int result;
963 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
964 if (result == 0)
965 break;
966 if (result == 2)
967 needs_warning = true;
969 if (TREE_CODE (s1) == FIELD_DECL
970 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
971 DECL_FIELD_BIT_OFFSET (s2)) != 1)
972 break;
974 ok = true;
975 break;
977 tagged_tu_seen_base = tts.next;
978 if (!ok)
979 return 0;
981 return needs_warning ? 2 : 1;
984 case RECORD_TYPE:
986 struct tagged_tu_seen tts;
988 tts.next = tagged_tu_seen_base;
989 tts.t1 = t1;
990 tts.t2 = t2;
991 tagged_tu_seen_base = &tts;
993 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
994 s1 && s2;
995 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
997 int result;
998 if (TREE_CODE (s1) != TREE_CODE (s2)
999 || DECL_NAME (s1) != DECL_NAME (s2))
1000 break;
1001 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1002 if (result == 0)
1003 break;
1004 if (result == 2)
1005 needs_warning = true;
1007 if (TREE_CODE (s1) == FIELD_DECL
1008 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1009 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1010 break;
1012 tagged_tu_seen_base = tts.next;
1013 if (s1 && s2)
1014 return 0;
1015 return needs_warning ? 2 : 1;
1018 default:
1019 gcc_unreachable ();
1023 /* Return 1 if two function types F1 and F2 are compatible.
1024 If either type specifies no argument types,
1025 the other must specify a fixed number of self-promoting arg types.
1026 Otherwise, if one type specifies only the number of arguments,
1027 the other must specify that number of self-promoting arg types.
1028 Otherwise, the argument types must match. */
1030 static int
1031 function_types_compatible_p (tree f1, tree f2)
1033 tree args1, args2;
1034 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1035 int val = 1;
1036 int val1;
1037 tree ret1, ret2;
1039 ret1 = TREE_TYPE (f1);
1040 ret2 = TREE_TYPE (f2);
1042 /* 'volatile' qualifiers on a function's return type used to mean
1043 the function is noreturn. */
1044 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1045 pedwarn ("function return types not compatible due to %<volatile%>");
1046 if (TYPE_VOLATILE (ret1))
1047 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1048 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1049 if (TYPE_VOLATILE (ret2))
1050 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1051 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1052 val = comptypes (ret1, ret2);
1053 if (val == 0)
1054 return 0;
1056 args1 = TYPE_ARG_TYPES (f1);
1057 args2 = TYPE_ARG_TYPES (f2);
1059 /* An unspecified parmlist matches any specified parmlist
1060 whose argument types don't need default promotions. */
1062 if (args1 == 0)
1064 if (!self_promoting_args_p (args2))
1065 return 0;
1066 /* If one of these types comes from a non-prototype fn definition,
1067 compare that with the other type's arglist.
1068 If they don't match, ask for a warning (but no error). */
1069 if (TYPE_ACTUAL_ARG_TYPES (f1)
1070 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1071 val = 2;
1072 return val;
1074 if (args2 == 0)
1076 if (!self_promoting_args_p (args1))
1077 return 0;
1078 if (TYPE_ACTUAL_ARG_TYPES (f2)
1079 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1080 val = 2;
1081 return val;
1084 /* Both types have argument lists: compare them and propagate results. */
1085 val1 = type_lists_compatible_p (args1, args2);
1086 return val1 != 1 ? val1 : val;
1089 /* Check two lists of types for compatibility,
1090 returning 0 for incompatible, 1 for compatible,
1091 or 2 for compatible with warning. */
1093 static int
1094 type_lists_compatible_p (tree args1, tree args2)
1096 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1097 int val = 1;
1098 int newval = 0;
1100 while (1)
1102 tree a1, mv1, a2, mv2;
1103 if (args1 == 0 && args2 == 0)
1104 return val;
1105 /* If one list is shorter than the other,
1106 they fail to match. */
1107 if (args1 == 0 || args2 == 0)
1108 return 0;
1109 mv1 = a1 = TREE_VALUE (args1);
1110 mv2 = a2 = TREE_VALUE (args2);
1111 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1112 mv1 = TYPE_MAIN_VARIANT (mv1);
1113 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1114 mv2 = TYPE_MAIN_VARIANT (mv2);
1115 /* A null pointer instead of a type
1116 means there is supposed to be an argument
1117 but nothing is specified about what type it has.
1118 So match anything that self-promotes. */
1119 if (a1 == 0)
1121 if (c_type_promotes_to (a2) != a2)
1122 return 0;
1124 else if (a2 == 0)
1126 if (c_type_promotes_to (a1) != a1)
1127 return 0;
1129 /* If one of the lists has an error marker, ignore this arg. */
1130 else if (TREE_CODE (a1) == ERROR_MARK
1131 || TREE_CODE (a2) == ERROR_MARK)
1133 else if (!(newval = comptypes (mv1, mv2)))
1135 /* Allow wait (union {union wait *u; int *i} *)
1136 and wait (union wait *) to be compatible. */
1137 if (TREE_CODE (a1) == UNION_TYPE
1138 && (TYPE_NAME (a1) == 0
1139 || TYPE_TRANSPARENT_UNION (a1))
1140 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1141 && tree_int_cst_equal (TYPE_SIZE (a1),
1142 TYPE_SIZE (a2)))
1144 tree memb;
1145 for (memb = TYPE_FIELDS (a1);
1146 memb; memb = TREE_CHAIN (memb))
1148 tree mv3 = TREE_TYPE (memb);
1149 if (mv3 && mv3 != error_mark_node
1150 && TREE_CODE (mv3) != ARRAY_TYPE)
1151 mv3 = TYPE_MAIN_VARIANT (mv3);
1152 if (comptypes (mv3, mv2))
1153 break;
1155 if (memb == 0)
1156 return 0;
1158 else if (TREE_CODE (a2) == UNION_TYPE
1159 && (TYPE_NAME (a2) == 0
1160 || TYPE_TRANSPARENT_UNION (a2))
1161 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1162 && tree_int_cst_equal (TYPE_SIZE (a2),
1163 TYPE_SIZE (a1)))
1165 tree memb;
1166 for (memb = TYPE_FIELDS (a2);
1167 memb; memb = TREE_CHAIN (memb))
1169 tree mv3 = TREE_TYPE (memb);
1170 if (mv3 && mv3 != error_mark_node
1171 && TREE_CODE (mv3) != ARRAY_TYPE)
1172 mv3 = TYPE_MAIN_VARIANT (mv3);
1173 if (comptypes (mv3, mv1))
1174 break;
1176 if (memb == 0)
1177 return 0;
1179 else
1180 return 0;
1183 /* comptypes said ok, but record if it said to warn. */
1184 if (newval > val)
1185 val = newval;
1187 args1 = TREE_CHAIN (args1);
1188 args2 = TREE_CHAIN (args2);
1192 /* Compute the size to increment a pointer by. */
1194 static tree
1195 c_size_in_bytes (tree type)
1197 enum tree_code code = TREE_CODE (type);
1199 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1200 return size_one_node;
1202 if (!COMPLETE_OR_VOID_TYPE_P (type))
1204 error ("arithmetic on pointer to an incomplete type");
1205 return size_one_node;
1208 /* Convert in case a char is more than one unit. */
1209 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1210 size_int (TYPE_PRECISION (char_type_node)
1211 / BITS_PER_UNIT));
1214 /* Return either DECL or its known constant value (if it has one). */
1216 tree
1217 decl_constant_value (tree decl)
1219 if (/* Don't change a variable array bound or initial value to a constant
1220 in a place where a variable is invalid. Note that DECL_INITIAL
1221 isn't valid for a PARM_DECL. */
1222 current_function_decl != 0
1223 && TREE_CODE (decl) != PARM_DECL
1224 && !TREE_THIS_VOLATILE (decl)
1225 && TREE_READONLY (decl)
1226 && DECL_INITIAL (decl) != 0
1227 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1228 /* This is invalid if initial value is not constant.
1229 If it has either a function call, a memory reference,
1230 or a variable, then re-evaluating it could give different results. */
1231 && TREE_CONSTANT (DECL_INITIAL (decl))
1232 /* Check for cases where this is sub-optimal, even though valid. */
1233 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1234 return DECL_INITIAL (decl);
1235 return decl;
1238 /* Return either DECL or its known constant value (if it has one), but
1239 return DECL if pedantic or DECL has mode BLKmode. This is for
1240 bug-compatibility with the old behavior of decl_constant_value
1241 (before GCC 3.0); every use of this function is a bug and it should
1242 be removed before GCC 3.1. It is not appropriate to use pedantic
1243 in a way that affects optimization, and BLKmode is probably not the
1244 right test for avoiding misoptimizations either. */
1246 static tree
1247 decl_constant_value_for_broken_optimization (tree decl)
1249 if (pedantic || DECL_MODE (decl) == BLKmode)
1250 return decl;
1251 else
1252 return decl_constant_value (decl);
1256 /* Perform the default conversion of arrays and functions to pointers.
1257 Return the result of converting EXP. For any other expression, just
1258 return EXP. */
1260 static tree
1261 default_function_array_conversion (tree exp)
1263 tree orig_exp;
1264 tree type = TREE_TYPE (exp);
1265 enum tree_code code = TREE_CODE (type);
1266 int not_lvalue = 0;
1268 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1269 an lvalue.
1271 Do not use STRIP_NOPS here! It will remove conversions from pointer
1272 to integer and cause infinite recursion. */
1273 orig_exp = exp;
1274 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1275 || (TREE_CODE (exp) == NOP_EXPR
1276 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1278 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1279 not_lvalue = 1;
1280 exp = TREE_OPERAND (exp, 0);
1283 if (TREE_NO_WARNING (orig_exp))
1284 TREE_NO_WARNING (exp) = 1;
1286 if (code == FUNCTION_TYPE)
1288 return build_unary_op (ADDR_EXPR, exp, 0);
1290 if (code == ARRAY_TYPE)
1292 tree adr;
1293 tree restype = TREE_TYPE (type);
1294 tree ptrtype;
1295 int constp = 0;
1296 int volatilep = 0;
1297 int lvalue_array_p;
1299 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1301 constp = TREE_READONLY (exp);
1302 volatilep = TREE_THIS_VOLATILE (exp);
1305 if (TYPE_QUALS (type) || constp || volatilep)
1306 restype
1307 = c_build_qualified_type (restype,
1308 TYPE_QUALS (type)
1309 | (constp * TYPE_QUAL_CONST)
1310 | (volatilep * TYPE_QUAL_VOLATILE));
1312 if (TREE_CODE (exp) == INDIRECT_REF)
1313 return convert (build_pointer_type (restype),
1314 TREE_OPERAND (exp, 0));
1316 if (TREE_CODE (exp) == COMPOUND_EXPR)
1318 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1319 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1320 TREE_OPERAND (exp, 0), op1);
1323 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1324 if (!flag_isoc99 && !lvalue_array_p)
1326 /* Before C99, non-lvalue arrays do not decay to pointers.
1327 Normally, using such an array would be invalid; but it can
1328 be used correctly inside sizeof or as a statement expression.
1329 Thus, do not give an error here; an error will result later. */
1330 return exp;
1333 ptrtype = build_pointer_type (restype);
1335 if (TREE_CODE (exp) == VAR_DECL)
1337 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1338 ADDR_EXPR because it's the best way of representing what
1339 happens in C when we take the address of an array and place
1340 it in a pointer to the element type. */
1341 adr = build1 (ADDR_EXPR, ptrtype, exp);
1342 if (!c_mark_addressable (exp))
1343 return error_mark_node;
1344 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1345 return adr;
1347 /* This way is better for a COMPONENT_REF since it can
1348 simplify the offset for a component. */
1349 adr = build_unary_op (ADDR_EXPR, exp, 1);
1350 return convert (ptrtype, adr);
1352 return exp;
1355 /* Perform default promotions for C data used in expressions.
1356 Arrays and functions are converted to pointers;
1357 enumeral types or short or char, to int.
1358 In addition, manifest constants symbols are replaced by their values. */
1360 tree
1361 default_conversion (tree exp)
1363 tree orig_exp;
1364 tree type = TREE_TYPE (exp);
1365 enum tree_code code = TREE_CODE (type);
1367 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1368 return default_function_array_conversion (exp);
1370 /* Constants can be used directly unless they're not loadable. */
1371 if (TREE_CODE (exp) == CONST_DECL)
1372 exp = DECL_INITIAL (exp);
1374 /* Replace a nonvolatile const static variable with its value unless
1375 it is an array, in which case we must be sure that taking the
1376 address of the array produces consistent results. */
1377 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1379 exp = decl_constant_value_for_broken_optimization (exp);
1380 type = TREE_TYPE (exp);
1383 /* Strip no-op conversions. */
1384 orig_exp = exp;
1385 STRIP_TYPE_NOPS (exp);
1387 if (TREE_NO_WARNING (orig_exp))
1388 TREE_NO_WARNING (exp) = 1;
1390 /* Normally convert enums to int,
1391 but convert wide enums to something wider. */
1392 if (code == ENUMERAL_TYPE)
1394 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1395 TYPE_PRECISION (integer_type_node)),
1396 ((TYPE_PRECISION (type)
1397 >= TYPE_PRECISION (integer_type_node))
1398 && TYPE_UNSIGNED (type)));
1400 return convert (type, exp);
1403 if (TREE_CODE (exp) == COMPONENT_REF
1404 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1405 /* If it's thinner than an int, promote it like a
1406 c_promoting_integer_type_p, otherwise leave it alone. */
1407 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1408 TYPE_PRECISION (integer_type_node)))
1409 return convert (integer_type_node, exp);
1411 if (c_promoting_integer_type_p (type))
1413 /* Preserve unsignedness if not really getting any wider. */
1414 if (TYPE_UNSIGNED (type)
1415 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1416 return convert (unsigned_type_node, exp);
1418 return convert (integer_type_node, exp);
1421 if (code == VOID_TYPE)
1423 error ("void value not ignored as it ought to be");
1424 return error_mark_node;
1426 return exp;
1429 /* Look up COMPONENT in a structure or union DECL.
1431 If the component name is not found, returns NULL_TREE. Otherwise,
1432 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1433 stepping down the chain to the component, which is in the last
1434 TREE_VALUE of the list. Normally the list is of length one, but if
1435 the component is embedded within (nested) anonymous structures or
1436 unions, the list steps down the chain to the component. */
1438 static tree
1439 lookup_field (tree decl, tree component)
1441 tree type = TREE_TYPE (decl);
1442 tree field;
1444 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1445 to the field elements. Use a binary search on this array to quickly
1446 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1447 will always be set for structures which have many elements. */
1449 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1451 int bot, top, half;
1452 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1454 field = TYPE_FIELDS (type);
1455 bot = 0;
1456 top = TYPE_LANG_SPECIFIC (type)->s->len;
1457 while (top - bot > 1)
1459 half = (top - bot + 1) >> 1;
1460 field = field_array[bot+half];
1462 if (DECL_NAME (field) == NULL_TREE)
1464 /* Step through all anon unions in linear fashion. */
1465 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1467 field = field_array[bot++];
1468 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1469 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1471 tree anon = lookup_field (field, component);
1473 if (anon)
1474 return tree_cons (NULL_TREE, field, anon);
1478 /* Entire record is only anon unions. */
1479 if (bot > top)
1480 return NULL_TREE;
1482 /* Restart the binary search, with new lower bound. */
1483 continue;
1486 if (DECL_NAME (field) == component)
1487 break;
1488 if (DECL_NAME (field) < component)
1489 bot += half;
1490 else
1491 top = bot + half;
1494 if (DECL_NAME (field_array[bot]) == component)
1495 field = field_array[bot];
1496 else if (DECL_NAME (field) != component)
1497 return NULL_TREE;
1499 else
1501 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1503 if (DECL_NAME (field) == NULL_TREE
1504 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1505 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1507 tree anon = lookup_field (field, component);
1509 if (anon)
1510 return tree_cons (NULL_TREE, field, anon);
1513 if (DECL_NAME (field) == component)
1514 break;
1517 if (field == NULL_TREE)
1518 return NULL_TREE;
1521 return tree_cons (NULL_TREE, field, NULL_TREE);
1524 /* Make an expression to refer to the COMPONENT field of
1525 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1527 tree
1528 build_component_ref (tree datum, tree component)
1530 tree type = TREE_TYPE (datum);
1531 enum tree_code code = TREE_CODE (type);
1532 tree field = NULL;
1533 tree ref;
1535 if (!objc_is_public (datum, component))
1536 return error_mark_node;
1538 /* See if there is a field or component with name COMPONENT. */
1540 if (code == RECORD_TYPE || code == UNION_TYPE)
1542 if (!COMPLETE_TYPE_P (type))
1544 c_incomplete_type_error (NULL_TREE, type);
1545 return error_mark_node;
1548 field = lookup_field (datum, component);
1550 if (!field)
1552 error ("%qT has no member named %qE", type, component);
1553 return error_mark_node;
1556 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1557 This might be better solved in future the way the C++ front
1558 end does it - by giving the anonymous entities each a
1559 separate name and type, and then have build_component_ref
1560 recursively call itself. We can't do that here. */
1563 tree subdatum = TREE_VALUE (field);
1565 if (TREE_TYPE (subdatum) == error_mark_node)
1566 return error_mark_node;
1568 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1569 NULL_TREE);
1570 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1571 TREE_READONLY (ref) = 1;
1572 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1573 TREE_THIS_VOLATILE (ref) = 1;
1575 if (TREE_DEPRECATED (subdatum))
1576 warn_deprecated_use (subdatum);
1578 datum = ref;
1580 field = TREE_CHAIN (field);
1582 while (field);
1584 return ref;
1586 else if (code != ERROR_MARK)
1587 error ("request for member %qE in something not a structure or union",
1588 component);
1590 return error_mark_node;
1593 /* Given an expression PTR for a pointer, return an expression
1594 for the value pointed to.
1595 ERRORSTRING is the name of the operator to appear in error messages. */
1597 tree
1598 build_indirect_ref (tree ptr, const char *errorstring)
1600 tree pointer = default_conversion (ptr);
1601 tree type = TREE_TYPE (pointer);
1603 if (TREE_CODE (type) == POINTER_TYPE)
1605 if (TREE_CODE (pointer) == ADDR_EXPR
1606 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1607 == TREE_TYPE (type)))
1608 return TREE_OPERAND (pointer, 0);
1609 else
1611 tree t = TREE_TYPE (type);
1612 tree mvt = t;
1613 tree ref;
1615 if (TREE_CODE (mvt) != ARRAY_TYPE)
1616 mvt = TYPE_MAIN_VARIANT (mvt);
1617 ref = build1 (INDIRECT_REF, mvt, pointer);
1619 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1621 error ("dereferencing pointer to incomplete type");
1622 return error_mark_node;
1624 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1625 warning ("dereferencing %<void *%> pointer");
1627 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1628 so that we get the proper error message if the result is used
1629 to assign to. Also, &* is supposed to be a no-op.
1630 And ANSI C seems to specify that the type of the result
1631 should be the const type. */
1632 /* A de-reference of a pointer to const is not a const. It is valid
1633 to change it via some other pointer. */
1634 TREE_READONLY (ref) = TYPE_READONLY (t);
1635 TREE_SIDE_EFFECTS (ref)
1636 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1637 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1638 return ref;
1641 else if (TREE_CODE (pointer) != ERROR_MARK)
1642 error ("invalid type argument of %qs", errorstring);
1643 return error_mark_node;
1646 /* This handles expressions of the form "a[i]", which denotes
1647 an array reference.
1649 This is logically equivalent in C to *(a+i), but we may do it differently.
1650 If A is a variable or a member, we generate a primitive ARRAY_REF.
1651 This avoids forcing the array out of registers, and can work on
1652 arrays that are not lvalues (for example, members of structures returned
1653 by functions). */
1655 tree
1656 build_array_ref (tree array, tree index)
1658 bool swapped = false;
1659 if (TREE_TYPE (array) == error_mark_node
1660 || TREE_TYPE (index) == error_mark_node)
1661 return error_mark_node;
1663 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1664 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1666 tree temp;
1667 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1668 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1670 error ("subscripted value is neither array nor pointer");
1671 return error_mark_node;
1673 temp = array;
1674 array = index;
1675 index = temp;
1676 swapped = true;
1679 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1681 error ("array subscript is not an integer");
1682 return error_mark_node;
1685 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1687 error ("subscripted value is pointer to function");
1688 return error_mark_node;
1691 /* Subscripting with type char is likely to lose on a machine where
1692 chars are signed. So warn on any machine, but optionally. Don't
1693 warn for unsigned char since that type is safe. Don't warn for
1694 signed char because anyone who uses that must have done so
1695 deliberately. ??? Existing practice has also been to warn only
1696 when the char index is syntactically the index, not for
1697 char[array]. */
1698 if (warn_char_subscripts && !swapped
1699 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1700 warning ("array subscript has type %<char%>");
1702 /* Apply default promotions *after* noticing character types. */
1703 index = default_conversion (index);
1705 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1707 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1709 tree rval, type;
1711 /* An array that is indexed by a non-constant
1712 cannot be stored in a register; we must be able to do
1713 address arithmetic on its address.
1714 Likewise an array of elements of variable size. */
1715 if (TREE_CODE (index) != INTEGER_CST
1716 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1717 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1719 if (!c_mark_addressable (array))
1720 return error_mark_node;
1722 /* An array that is indexed by a constant value which is not within
1723 the array bounds cannot be stored in a register either; because we
1724 would get a crash in store_bit_field/extract_bit_field when trying
1725 to access a non-existent part of the register. */
1726 if (TREE_CODE (index) == INTEGER_CST
1727 && TYPE_DOMAIN (TREE_TYPE (array))
1728 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1730 if (!c_mark_addressable (array))
1731 return error_mark_node;
1734 if (pedantic)
1736 tree foo = array;
1737 while (TREE_CODE (foo) == COMPONENT_REF)
1738 foo = TREE_OPERAND (foo, 0);
1739 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1740 pedwarn ("ISO C forbids subscripting %<register%> array");
1741 else if (!flag_isoc99 && !lvalue_p (foo))
1742 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1745 type = TREE_TYPE (TREE_TYPE (array));
1746 if (TREE_CODE (type) != ARRAY_TYPE)
1747 type = TYPE_MAIN_VARIANT (type);
1748 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1749 /* Array ref is const/volatile if the array elements are
1750 or if the array is. */
1751 TREE_READONLY (rval)
1752 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1753 | TREE_READONLY (array));
1754 TREE_SIDE_EFFECTS (rval)
1755 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1756 | TREE_SIDE_EFFECTS (array));
1757 TREE_THIS_VOLATILE (rval)
1758 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1759 /* This was added by rms on 16 Nov 91.
1760 It fixes vol struct foo *a; a->elts[1]
1761 in an inline function.
1762 Hope it doesn't break something else. */
1763 | TREE_THIS_VOLATILE (array));
1764 return require_complete_type (fold (rval));
1766 else
1768 tree ar = default_conversion (array);
1770 if (ar == error_mark_node)
1771 return ar;
1773 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1774 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1776 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1777 "array indexing");
1781 /* Build an external reference to identifier ID. FUN indicates
1782 whether this will be used for a function call. LOC is the source
1783 location of the identifier. */
1784 tree
1785 build_external_ref (tree id, int fun, location_t loc)
1787 tree ref;
1788 tree decl = lookup_name (id);
1790 /* In Objective-C, an instance variable (ivar) may be preferred to
1791 whatever lookup_name() found. */
1792 decl = objc_lookup_ivar (decl, id);
1794 if (decl && decl != error_mark_node)
1795 ref = decl;
1796 else if (fun)
1797 /* Implicit function declaration. */
1798 ref = implicitly_declare (id);
1799 else if (decl == error_mark_node)
1800 /* Don't complain about something that's already been
1801 complained about. */
1802 return error_mark_node;
1803 else
1805 undeclared_variable (id, loc);
1806 return error_mark_node;
1809 if (TREE_TYPE (ref) == error_mark_node)
1810 return error_mark_node;
1812 if (TREE_DEPRECATED (ref))
1813 warn_deprecated_use (ref);
1815 if (!skip_evaluation)
1816 assemble_external (ref);
1817 TREE_USED (ref) = 1;
1819 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1821 if (!in_sizeof && !in_typeof)
1822 C_DECL_USED (ref) = 1;
1823 else if (DECL_INITIAL (ref) == 0
1824 && DECL_EXTERNAL (ref)
1825 && !TREE_PUBLIC (ref))
1826 record_maybe_used_decl (ref);
1829 if (TREE_CODE (ref) == CONST_DECL)
1831 ref = DECL_INITIAL (ref);
1832 TREE_CONSTANT (ref) = 1;
1833 TREE_INVARIANT (ref) = 1;
1835 else if (current_function_decl != 0
1836 && !DECL_FILE_SCOPE_P (current_function_decl)
1837 && (TREE_CODE (ref) == VAR_DECL
1838 || TREE_CODE (ref) == PARM_DECL
1839 || TREE_CODE (ref) == FUNCTION_DECL))
1841 tree context = decl_function_context (ref);
1843 if (context != 0 && context != current_function_decl)
1844 DECL_NONLOCAL (ref) = 1;
1847 return ref;
1850 /* Record details of decls possibly used inside sizeof or typeof. */
1851 struct maybe_used_decl
1853 /* The decl. */
1854 tree decl;
1855 /* The level seen at (in_sizeof + in_typeof). */
1856 int level;
1857 /* The next one at this level or above, or NULL. */
1858 struct maybe_used_decl *next;
1861 static struct maybe_used_decl *maybe_used_decls;
1863 /* Record that DECL, an undefined static function reference seen
1864 inside sizeof or typeof, might be used if the operand of sizeof is
1865 a VLA type or the operand of typeof is a variably modified
1866 type. */
1868 static void
1869 record_maybe_used_decl (tree decl)
1871 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1872 t->decl = decl;
1873 t->level = in_sizeof + in_typeof;
1874 t->next = maybe_used_decls;
1875 maybe_used_decls = t;
1878 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1879 USED is false, just discard them. If it is true, mark them used
1880 (if no longer inside sizeof or typeof) or move them to the next
1881 level up (if still inside sizeof or typeof). */
1883 void
1884 pop_maybe_used (bool used)
1886 struct maybe_used_decl *p = maybe_used_decls;
1887 int cur_level = in_sizeof + in_typeof;
1888 while (p && p->level > cur_level)
1890 if (used)
1892 if (cur_level == 0)
1893 C_DECL_USED (p->decl) = 1;
1894 else
1895 p->level = cur_level;
1897 p = p->next;
1899 if (!used || cur_level == 0)
1900 maybe_used_decls = p;
1903 /* Return the result of sizeof applied to EXPR. */
1905 struct c_expr
1906 c_expr_sizeof_expr (struct c_expr expr)
1908 struct c_expr ret;
1909 if (expr.value == error_mark_node)
1911 ret.value = error_mark_node;
1912 ret.original_code = ERROR_MARK;
1913 pop_maybe_used (false);
1915 else
1917 ret.value = c_sizeof (TREE_TYPE (expr.value));
1918 ret.original_code = ERROR_MARK;
1919 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1921 return ret;
1924 /* Return the result of sizeof applied to T, a structure for the type
1925 name passed to sizeof (rather than the type itself). */
1927 struct c_expr
1928 c_expr_sizeof_type (struct c_type_name *t)
1930 tree type;
1931 struct c_expr ret;
1932 type = groktypename (t);
1933 ret.value = c_sizeof (type);
1934 ret.original_code = ERROR_MARK;
1935 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1936 return ret;
1939 /* Build a function call to function FUNCTION with parameters PARAMS.
1940 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1941 TREE_VALUE of each node is a parameter-expression.
1942 FUNCTION's data type may be a function type or a pointer-to-function. */
1944 tree
1945 build_function_call (tree function, tree params)
1947 tree fntype, fundecl = 0;
1948 tree coerced_params;
1949 tree name = NULL_TREE, result;
1950 tree tem;
1952 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1953 STRIP_TYPE_NOPS (function);
1955 /* Convert anything with function type to a pointer-to-function. */
1956 if (TREE_CODE (function) == FUNCTION_DECL)
1958 name = DECL_NAME (function);
1960 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1961 (because calling an inline function does not mean the function
1962 needs to be separately compiled). */
1963 fntype = build_type_variant (TREE_TYPE (function),
1964 TREE_READONLY (function),
1965 TREE_THIS_VOLATILE (function));
1966 fundecl = function;
1967 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1969 else
1970 function = default_conversion (function);
1972 fntype = TREE_TYPE (function);
1974 if (TREE_CODE (fntype) == ERROR_MARK)
1975 return error_mark_node;
1977 if (!(TREE_CODE (fntype) == POINTER_TYPE
1978 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1980 error ("called object %qE is not a function", function);
1981 return error_mark_node;
1984 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1985 current_function_returns_abnormally = 1;
1987 /* fntype now gets the type of function pointed to. */
1988 fntype = TREE_TYPE (fntype);
1990 /* Check that the function is called through a compatible prototype.
1991 If it is not, replace the call by a trap, wrapped up in a compound
1992 expression if necessary. This has the nice side-effect to prevent
1993 the tree-inliner from generating invalid assignment trees which may
1994 blow up in the RTL expander later.
1996 ??? This doesn't work for Objective-C because objc_comptypes
1997 refuses to compare function prototypes, yet the compiler appears
1998 to build calls that are flagged as invalid by C's comptypes. */
1999 if (!c_dialect_objc ()
2000 && TREE_CODE (function) == NOP_EXPR
2001 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2002 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2003 && !comptypes (fntype, TREE_TYPE (tem)))
2005 tree return_type = TREE_TYPE (fntype);
2006 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2007 NULL_TREE);
2009 /* This situation leads to run-time undefined behavior. We can't,
2010 therefore, simply error unless we can prove that all possible
2011 executions of the program must execute the code. */
2012 warning ("function called through a non-compatible type");
2014 /* We can, however, treat "undefined" any way we please.
2015 Call abort to encourage the user to fix the program. */
2016 inform ("if this code is reached, the program will abort");
2018 if (VOID_TYPE_P (return_type))
2019 return trap;
2020 else
2022 tree rhs;
2024 if (AGGREGATE_TYPE_P (return_type))
2025 rhs = build_compound_literal (return_type,
2026 build_constructor (return_type,
2027 NULL_TREE));
2028 else
2029 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2031 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2035 /* Convert the parameters to the types declared in the
2036 function prototype, or apply default promotions. */
2038 coerced_params
2039 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2041 if (coerced_params == error_mark_node)
2042 return error_mark_node;
2044 /* Check that the arguments to the function are valid. */
2046 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2048 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2049 function, coerced_params, NULL_TREE);
2050 TREE_SIDE_EFFECTS (result) = 1;
2052 if (require_constant_value)
2054 result = fold_initializer (result);
2056 if (TREE_CONSTANT (result)
2057 && (name == NULL_TREE
2058 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2059 pedwarn_init ("initializer element is not constant");
2061 else
2062 result = fold (result);
2064 if (VOID_TYPE_P (TREE_TYPE (result)))
2065 return result;
2066 return require_complete_type (result);
2069 /* Convert the argument expressions in the list VALUES
2070 to the types in the list TYPELIST. The result is a list of converted
2071 argument expressions, unless there are too few arguments in which
2072 case it is error_mark_node.
2074 If TYPELIST is exhausted, or when an element has NULL as its type,
2075 perform the default conversions.
2077 PARMLIST is the chain of parm decls for the function being called.
2078 It may be 0, if that info is not available.
2079 It is used only for generating error messages.
2081 FUNCTION is a tree for the called function. It is used only for
2082 error messages, where it is formatted with %qE.
2084 This is also where warnings about wrong number of args are generated.
2086 Both VALUES and the returned value are chains of TREE_LIST nodes
2087 with the elements of the list in the TREE_VALUE slots of those nodes. */
2089 static tree
2090 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2092 tree typetail, valtail;
2093 tree result = NULL;
2094 int parmnum;
2095 tree selector;
2097 /* Change pointer to function to the function itself for
2098 diagnostics. */
2099 if (TREE_CODE (function) == ADDR_EXPR
2100 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2101 function = TREE_OPERAND (function, 0);
2103 /* Handle an ObjC selector specially for diagnostics. */
2104 selector = objc_message_selector ();
2106 /* Scan the given expressions and types, producing individual
2107 converted arguments and pushing them on RESULT in reverse order. */
2109 for (valtail = values, typetail = typelist, parmnum = 0;
2110 valtail;
2111 valtail = TREE_CHAIN (valtail), parmnum++)
2113 tree type = typetail ? TREE_VALUE (typetail) : 0;
2114 tree val = TREE_VALUE (valtail);
2115 tree rname = function;
2116 int argnum = parmnum + 1;
2117 const char *invalid_func_diag;
2119 if (type == void_type_node)
2121 error ("too many arguments to function %qE", function);
2122 break;
2125 if (selector && argnum > 2)
2127 rname = selector;
2128 argnum -= 2;
2131 STRIP_TYPE_NOPS (val);
2133 val = default_function_array_conversion (val);
2135 val = require_complete_type (val);
2137 if (type != 0)
2139 /* Formal parm type is specified by a function prototype. */
2140 tree parmval;
2142 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2144 error ("type of formal parameter %d is incomplete", parmnum + 1);
2145 parmval = val;
2147 else
2149 /* Optionally warn about conversions that
2150 differ from the default conversions. */
2151 if (warn_conversion || warn_traditional)
2153 unsigned int formal_prec = TYPE_PRECISION (type);
2155 if (INTEGRAL_TYPE_P (type)
2156 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2157 warning ("passing argument %d of %qE as integer "
2158 "rather than floating due to prototype",
2159 argnum, rname);
2160 if (INTEGRAL_TYPE_P (type)
2161 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2162 warning ("passing argument %d of %qE as integer "
2163 "rather than complex due to prototype",
2164 argnum, rname);
2165 else if (TREE_CODE (type) == COMPLEX_TYPE
2166 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2167 warning ("passing argument %d of %qE as complex "
2168 "rather than floating due to prototype",
2169 argnum, rname);
2170 else if (TREE_CODE (type) == REAL_TYPE
2171 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2172 warning ("passing argument %d of %qE as floating "
2173 "rather than integer due to prototype",
2174 argnum, rname);
2175 else if (TREE_CODE (type) == COMPLEX_TYPE
2176 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2177 warning ("passing argument %d of %qE as complex "
2178 "rather than integer due to prototype",
2179 argnum, rname);
2180 else if (TREE_CODE (type) == REAL_TYPE
2181 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2182 warning ("passing argument %d of %qE as floating "
2183 "rather than complex due to prototype",
2184 argnum, rname);
2185 /* ??? At some point, messages should be written about
2186 conversions between complex types, but that's too messy
2187 to do now. */
2188 else if (TREE_CODE (type) == REAL_TYPE
2189 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2191 /* Warn if any argument is passed as `float',
2192 since without a prototype it would be `double'. */
2193 if (formal_prec == TYPE_PRECISION (float_type_node))
2194 warning ("passing argument %d of %qE as %<float%> "
2195 "rather than %<double%> due to prototype",
2196 argnum, rname);
2198 /* Detect integer changing in width or signedness.
2199 These warnings are only activated with
2200 -Wconversion, not with -Wtraditional. */
2201 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2202 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2204 tree would_have_been = default_conversion (val);
2205 tree type1 = TREE_TYPE (would_have_been);
2207 if (TREE_CODE (type) == ENUMERAL_TYPE
2208 && (TYPE_MAIN_VARIANT (type)
2209 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2210 /* No warning if function asks for enum
2211 and the actual arg is that enum type. */
2213 else if (formal_prec != TYPE_PRECISION (type1))
2214 warning ("passing argument %d of %qE with different "
2215 "width due to prototype", argnum, rname);
2216 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2218 /* Don't complain if the formal parameter type
2219 is an enum, because we can't tell now whether
2220 the value was an enum--even the same enum. */
2221 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2223 else if (TREE_CODE (val) == INTEGER_CST
2224 && int_fits_type_p (val, type))
2225 /* Change in signedness doesn't matter
2226 if a constant value is unaffected. */
2228 /* If the value is extended from a narrower
2229 unsigned type, it doesn't matter whether we
2230 pass it as signed or unsigned; the value
2231 certainly is the same either way. */
2232 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2233 && TYPE_UNSIGNED (TREE_TYPE (val)))
2235 else if (TYPE_UNSIGNED (type))
2236 warning ("passing argument %d of %qE as unsigned "
2237 "due to prototype", argnum, rname);
2238 else
2239 warning ("passing argument %d of %qE as signed "
2240 "due to prototype", argnum, rname);
2244 parmval = convert_for_assignment (type, val, ic_argpass,
2245 fundecl, function,
2246 parmnum + 1);
2248 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2249 && INTEGRAL_TYPE_P (type)
2250 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2251 parmval = default_conversion (parmval);
2253 result = tree_cons (NULL_TREE, parmval, result);
2255 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2256 && (TYPE_PRECISION (TREE_TYPE (val))
2257 < TYPE_PRECISION (double_type_node)))
2258 /* Convert `float' to `double'. */
2259 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2260 else if ((invalid_func_diag =
2261 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2263 error (invalid_func_diag);
2264 return error_mark_node;
2266 else
2267 /* Convert `short' and `char' to full-size `int'. */
2268 result = tree_cons (NULL_TREE, default_conversion (val), result);
2270 if (typetail)
2271 typetail = TREE_CHAIN (typetail);
2274 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2276 error ("too few arguments to function %qE", function);
2277 return error_mark_node;
2280 return nreverse (result);
2283 /* This is the entry point used by the parser
2284 for binary operators in the input.
2285 In addition to constructing the expression,
2286 we check for operands that were written with other binary operators
2287 in a way that is likely to confuse the user. */
2289 struct c_expr
2290 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2291 struct c_expr arg2)
2293 struct c_expr result;
2295 enum tree_code code1 = arg1.original_code;
2296 enum tree_code code2 = arg2.original_code;
2298 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2299 result.original_code = code;
2301 if (TREE_CODE (result.value) == ERROR_MARK)
2302 return result;
2304 /* Check for cases such as x+y<<z which users are likely
2305 to misinterpret. */
2306 if (warn_parentheses)
2308 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2310 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2311 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2312 warning ("suggest parentheses around + or - inside shift");
2315 if (code == TRUTH_ORIF_EXPR)
2317 if (code1 == TRUTH_ANDIF_EXPR
2318 || code2 == TRUTH_ANDIF_EXPR)
2319 warning ("suggest parentheses around && within ||");
2322 if (code == BIT_IOR_EXPR)
2324 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2325 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2326 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2327 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2328 warning ("suggest parentheses around arithmetic in operand of |");
2329 /* Check cases like x|y==z */
2330 if (TREE_CODE_CLASS (code1) == tcc_comparison
2331 || TREE_CODE_CLASS (code2) == tcc_comparison)
2332 warning ("suggest parentheses around comparison in operand of |");
2335 if (code == BIT_XOR_EXPR)
2337 if (code1 == BIT_AND_EXPR
2338 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2339 || code2 == BIT_AND_EXPR
2340 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2341 warning ("suggest parentheses around arithmetic in operand of ^");
2342 /* Check cases like x^y==z */
2343 if (TREE_CODE_CLASS (code1) == tcc_comparison
2344 || TREE_CODE_CLASS (code2) == tcc_comparison)
2345 warning ("suggest parentheses around comparison in operand of ^");
2348 if (code == BIT_AND_EXPR)
2350 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2351 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2352 warning ("suggest parentheses around + or - in operand of &");
2353 /* Check cases like x&y==z */
2354 if (TREE_CODE_CLASS (code1) == tcc_comparison
2355 || TREE_CODE_CLASS (code2) == tcc_comparison)
2356 warning ("suggest parentheses around comparison in operand of &");
2358 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2359 if (TREE_CODE_CLASS (code) == tcc_comparison
2360 && (TREE_CODE_CLASS (code1) == tcc_comparison
2361 || TREE_CODE_CLASS (code2) == tcc_comparison))
2362 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2366 unsigned_conversion_warning (result.value, arg1.value);
2367 unsigned_conversion_warning (result.value, arg2.value);
2368 overflow_warning (result.value);
2370 return result;
2373 /* Return a tree for the difference of pointers OP0 and OP1.
2374 The resulting tree has type int. */
2376 static tree
2377 pointer_diff (tree op0, tree op1)
2379 tree restype = ptrdiff_type_node;
2381 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2382 tree con0, con1, lit0, lit1;
2383 tree orig_op1 = op1;
2385 if (pedantic || warn_pointer_arith)
2387 if (TREE_CODE (target_type) == VOID_TYPE)
2388 pedwarn ("pointer of type %<void *%> used in subtraction");
2389 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2390 pedwarn ("pointer to a function used in subtraction");
2393 /* If the conversion to ptrdiff_type does anything like widening or
2394 converting a partial to an integral mode, we get a convert_expression
2395 that is in the way to do any simplifications.
2396 (fold-const.c doesn't know that the extra bits won't be needed.
2397 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2398 different mode in place.)
2399 So first try to find a common term here 'by hand'; we want to cover
2400 at least the cases that occur in legal static initializers. */
2401 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2402 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2404 if (TREE_CODE (con0) == PLUS_EXPR)
2406 lit0 = TREE_OPERAND (con0, 1);
2407 con0 = TREE_OPERAND (con0, 0);
2409 else
2410 lit0 = integer_zero_node;
2412 if (TREE_CODE (con1) == PLUS_EXPR)
2414 lit1 = TREE_OPERAND (con1, 1);
2415 con1 = TREE_OPERAND (con1, 0);
2417 else
2418 lit1 = integer_zero_node;
2420 if (operand_equal_p (con0, con1, 0))
2422 op0 = lit0;
2423 op1 = lit1;
2427 /* First do the subtraction as integers;
2428 then drop through to build the divide operator.
2429 Do not do default conversions on the minus operator
2430 in case restype is a short type. */
2432 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2433 convert (restype, op1), 0);
2434 /* This generates an error if op1 is pointer to incomplete type. */
2435 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2436 error ("arithmetic on pointer to an incomplete type");
2438 /* This generates an error if op0 is pointer to incomplete type. */
2439 op1 = c_size_in_bytes (target_type);
2441 /* Divide by the size, in easiest possible way. */
2442 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2445 /* Construct and perhaps optimize a tree representation
2446 for a unary operation. CODE, a tree_code, specifies the operation
2447 and XARG is the operand.
2448 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2449 the default promotions (such as from short to int).
2450 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2451 allows non-lvalues; this is only used to handle conversion of non-lvalue
2452 arrays to pointers in C99. */
2454 tree
2455 build_unary_op (enum tree_code code, tree xarg, int flag)
2457 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2458 tree arg = xarg;
2459 tree argtype = 0;
2460 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2461 tree val;
2462 int noconvert = flag;
2464 if (typecode == ERROR_MARK)
2465 return error_mark_node;
2466 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2467 typecode = INTEGER_TYPE;
2469 switch (code)
2471 case CONVERT_EXPR:
2472 /* This is used for unary plus, because a CONVERT_EXPR
2473 is enough to prevent anybody from looking inside for
2474 associativity, but won't generate any code. */
2475 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2476 || typecode == COMPLEX_TYPE
2477 || typecode == VECTOR_TYPE))
2479 error ("wrong type argument to unary plus");
2480 return error_mark_node;
2482 else if (!noconvert)
2483 arg = default_conversion (arg);
2484 arg = non_lvalue (arg);
2485 break;
2487 case NEGATE_EXPR:
2488 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2489 || typecode == COMPLEX_TYPE
2490 || typecode == VECTOR_TYPE))
2492 error ("wrong type argument to unary minus");
2493 return error_mark_node;
2495 else if (!noconvert)
2496 arg = default_conversion (arg);
2497 break;
2499 case BIT_NOT_EXPR:
2500 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2502 if (!noconvert)
2503 arg = default_conversion (arg);
2505 else if (typecode == COMPLEX_TYPE)
2507 code = CONJ_EXPR;
2508 if (pedantic)
2509 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2510 if (!noconvert)
2511 arg = default_conversion (arg);
2513 else
2515 error ("wrong type argument to bit-complement");
2516 return error_mark_node;
2518 break;
2520 case ABS_EXPR:
2521 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2523 error ("wrong type argument to abs");
2524 return error_mark_node;
2526 else if (!noconvert)
2527 arg = default_conversion (arg);
2528 break;
2530 case CONJ_EXPR:
2531 /* Conjugating a real value is a no-op, but allow it anyway. */
2532 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2533 || typecode == COMPLEX_TYPE))
2535 error ("wrong type argument to conjugation");
2536 return error_mark_node;
2538 else if (!noconvert)
2539 arg = default_conversion (arg);
2540 break;
2542 case TRUTH_NOT_EXPR:
2543 if (typecode != INTEGER_TYPE
2544 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2545 && typecode != COMPLEX_TYPE
2546 /* These will convert to a pointer. */
2547 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2549 error ("wrong type argument to unary exclamation mark");
2550 return error_mark_node;
2552 arg = lang_hooks.truthvalue_conversion (arg);
2553 return invert_truthvalue (arg);
2555 case NOP_EXPR:
2556 break;
2558 case REALPART_EXPR:
2559 if (TREE_CODE (arg) == COMPLEX_CST)
2560 return TREE_REALPART (arg);
2561 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2562 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2563 else
2564 return arg;
2566 case IMAGPART_EXPR:
2567 if (TREE_CODE (arg) == COMPLEX_CST)
2568 return TREE_IMAGPART (arg);
2569 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2570 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2571 else
2572 return convert (TREE_TYPE (arg), integer_zero_node);
2574 case PREINCREMENT_EXPR:
2575 case POSTINCREMENT_EXPR:
2576 case PREDECREMENT_EXPR:
2577 case POSTDECREMENT_EXPR:
2579 /* Increment or decrement the real part of the value,
2580 and don't change the imaginary part. */
2581 if (typecode == COMPLEX_TYPE)
2583 tree real, imag;
2585 if (pedantic)
2586 pedwarn ("ISO C does not support %<++%> and %<--%>"
2587 " on complex types");
2589 arg = stabilize_reference (arg);
2590 real = build_unary_op (REALPART_EXPR, arg, 1);
2591 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2592 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2593 build_unary_op (code, real, 1), imag);
2596 /* Report invalid types. */
2598 if (typecode != POINTER_TYPE
2599 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2601 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2602 error ("wrong type argument to increment");
2603 else
2604 error ("wrong type argument to decrement");
2606 return error_mark_node;
2610 tree inc;
2611 tree result_type = TREE_TYPE (arg);
2613 arg = get_unwidened (arg, 0);
2614 argtype = TREE_TYPE (arg);
2616 /* Compute the increment. */
2618 if (typecode == POINTER_TYPE)
2620 /* If pointer target is an undefined struct,
2621 we just cannot know how to do the arithmetic. */
2622 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2624 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2625 error ("increment of pointer to unknown structure");
2626 else
2627 error ("decrement of pointer to unknown structure");
2629 else if ((pedantic || warn_pointer_arith)
2630 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2631 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2633 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2634 pedwarn ("wrong type argument to increment");
2635 else
2636 pedwarn ("wrong type argument to decrement");
2639 inc = c_size_in_bytes (TREE_TYPE (result_type));
2641 else
2642 inc = integer_one_node;
2644 inc = convert (argtype, inc);
2646 /* Complain about anything else that is not a true lvalue. */
2647 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2648 || code == POSTINCREMENT_EXPR)
2649 ? lv_increment
2650 : lv_decrement)))
2651 return error_mark_node;
2653 /* Report a read-only lvalue. */
2654 if (TREE_READONLY (arg))
2655 readonly_error (arg,
2656 ((code == PREINCREMENT_EXPR
2657 || code == POSTINCREMENT_EXPR)
2658 ? lv_increment : lv_decrement));
2660 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2661 val = boolean_increment (code, arg);
2662 else
2663 val = build2 (code, TREE_TYPE (arg), arg, inc);
2664 TREE_SIDE_EFFECTS (val) = 1;
2665 val = convert (result_type, val);
2666 if (TREE_CODE (val) != code)
2667 TREE_NO_WARNING (val) = 1;
2668 return val;
2671 case ADDR_EXPR:
2672 /* Note that this operation never does default_conversion. */
2674 /* Let &* cancel out to simplify resulting code. */
2675 if (TREE_CODE (arg) == INDIRECT_REF)
2677 /* Don't let this be an lvalue. */
2678 if (lvalue_p (TREE_OPERAND (arg, 0)))
2679 return non_lvalue (TREE_OPERAND (arg, 0));
2680 return TREE_OPERAND (arg, 0);
2683 /* For &x[y], return x+y */
2684 if (TREE_CODE (arg) == ARRAY_REF)
2686 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2687 return error_mark_node;
2688 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2689 TREE_OPERAND (arg, 1), 1);
2692 /* Anything not already handled and not a true memory reference
2693 or a non-lvalue array is an error. */
2694 else if (typecode != FUNCTION_TYPE && !flag
2695 && !lvalue_or_else (arg, lv_addressof))
2696 return error_mark_node;
2698 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2699 argtype = TREE_TYPE (arg);
2701 /* If the lvalue is const or volatile, merge that into the type
2702 to which the address will point. Note that you can't get a
2703 restricted pointer by taking the address of something, so we
2704 only have to deal with `const' and `volatile' here. */
2705 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2706 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2707 argtype = c_build_type_variant (argtype,
2708 TREE_READONLY (arg),
2709 TREE_THIS_VOLATILE (arg));
2711 if (!c_mark_addressable (arg))
2712 return error_mark_node;
2714 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2715 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2717 argtype = build_pointer_type (argtype);
2719 /* ??? Cope with user tricks that amount to offsetof. Delete this
2720 when we have proper support for integer constant expressions. */
2721 val = get_base_address (arg);
2722 if (val && TREE_CODE (val) == INDIRECT_REF
2723 && integer_zerop (TREE_OPERAND (val, 0)))
2724 return fold_convert (argtype, fold_offsetof (arg));
2726 val = build1 (ADDR_EXPR, argtype, arg);
2728 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2729 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2731 return val;
2733 default:
2734 break;
2737 if (argtype == 0)
2738 argtype = TREE_TYPE (arg);
2739 val = build1 (code, argtype, arg);
2740 return require_constant_value ? fold_initializer (val) : fold (val);
2743 /* Return nonzero if REF is an lvalue valid for this language.
2744 Lvalues can be assigned, unless their type has TYPE_READONLY.
2745 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2747 static int
2748 lvalue_p (tree ref)
2750 enum tree_code code = TREE_CODE (ref);
2752 switch (code)
2754 case REALPART_EXPR:
2755 case IMAGPART_EXPR:
2756 case COMPONENT_REF:
2757 return lvalue_p (TREE_OPERAND (ref, 0));
2759 case COMPOUND_LITERAL_EXPR:
2760 case STRING_CST:
2761 return 1;
2763 case INDIRECT_REF:
2764 case ARRAY_REF:
2765 case VAR_DECL:
2766 case PARM_DECL:
2767 case RESULT_DECL:
2768 case ERROR_MARK:
2769 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2770 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2772 case BIND_EXPR:
2773 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2775 default:
2776 return 0;
2780 /* Give an error for storing in something that is 'const'. */
2782 static void
2783 readonly_error (tree arg, enum lvalue_use use)
2785 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2786 /* Using this macro rather than (for example) arrays of messages
2787 ensures that all the format strings are checked at compile
2788 time. */
2789 #define READONLY_MSG(A, I, D) (use == lv_assign \
2790 ? (A) \
2791 : (use == lv_increment ? (I) : (D)))
2792 if (TREE_CODE (arg) == COMPONENT_REF)
2794 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2795 readonly_error (TREE_OPERAND (arg, 0), use);
2796 else
2797 error (READONLY_MSG (N_("assignment of read-only member %qD"),
2798 N_("increment of read-only member %qD"),
2799 N_("decrement of read-only member %qD")),
2800 TREE_OPERAND (arg, 1));
2802 else if (TREE_CODE (arg) == VAR_DECL)
2803 error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2804 N_("increment of read-only variable %qD"),
2805 N_("decrement of read-only variable %qD")),
2806 arg);
2807 else
2808 error (READONLY_MSG (N_("assignment of read-only location"),
2809 N_("increment of read-only location"),
2810 N_("decrement of read-only location")));
2814 /* Return nonzero if REF is an lvalue valid for this language;
2815 otherwise, print an error message and return zero. USE says
2816 how the lvalue is being used and so selects the error message. */
2818 static int
2819 lvalue_or_else (tree ref, enum lvalue_use use)
2821 int win = lvalue_p (ref);
2823 if (!win)
2824 lvalue_error (use);
2826 return win;
2829 /* Mark EXP saying that we need to be able to take the
2830 address of it; it should not be allocated in a register.
2831 Returns true if successful. */
2833 bool
2834 c_mark_addressable (tree exp)
2836 tree x = exp;
2838 while (1)
2839 switch (TREE_CODE (x))
2841 case COMPONENT_REF:
2842 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2844 error
2845 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2846 return false;
2849 /* ... fall through ... */
2851 case ADDR_EXPR:
2852 case ARRAY_REF:
2853 case REALPART_EXPR:
2854 case IMAGPART_EXPR:
2855 x = TREE_OPERAND (x, 0);
2856 break;
2858 case COMPOUND_LITERAL_EXPR:
2859 case CONSTRUCTOR:
2860 TREE_ADDRESSABLE (x) = 1;
2861 return true;
2863 case VAR_DECL:
2864 case CONST_DECL:
2865 case PARM_DECL:
2866 case RESULT_DECL:
2867 if (C_DECL_REGISTER (x)
2868 && DECL_NONLOCAL (x))
2870 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2872 error
2873 ("global register variable %qD used in nested function", x);
2874 return false;
2876 pedwarn ("register variable %qD used in nested function", x);
2878 else if (C_DECL_REGISTER (x))
2880 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2881 error ("address of global register variable %qD requested", x);
2882 else
2883 error ("address of register variable %qD requested", x);
2884 return false;
2887 /* drops in */
2888 case FUNCTION_DECL:
2889 TREE_ADDRESSABLE (x) = 1;
2890 /* drops out */
2891 default:
2892 return true;
2896 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2898 tree
2899 build_conditional_expr (tree ifexp, tree op1, tree op2)
2901 tree type1;
2902 tree type2;
2903 enum tree_code code1;
2904 enum tree_code code2;
2905 tree result_type = NULL;
2906 tree orig_op1 = op1, orig_op2 = op2;
2908 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2910 /* Promote both alternatives. */
2912 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2913 op1 = default_conversion (op1);
2914 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2915 op2 = default_conversion (op2);
2917 if (TREE_CODE (ifexp) == ERROR_MARK
2918 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2919 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2920 return error_mark_node;
2922 type1 = TREE_TYPE (op1);
2923 code1 = TREE_CODE (type1);
2924 type2 = TREE_TYPE (op2);
2925 code2 = TREE_CODE (type2);
2927 /* C90 does not permit non-lvalue arrays in conditional expressions.
2928 In C99 they will be pointers by now. */
2929 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2931 error ("non-lvalue array in conditional expression");
2932 return error_mark_node;
2935 /* Quickly detect the usual case where op1 and op2 have the same type
2936 after promotion. */
2937 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2939 if (type1 == type2)
2940 result_type = type1;
2941 else
2942 result_type = TYPE_MAIN_VARIANT (type1);
2944 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2945 || code1 == COMPLEX_TYPE)
2946 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2947 || code2 == COMPLEX_TYPE))
2949 result_type = c_common_type (type1, type2);
2951 /* If -Wsign-compare, warn here if type1 and type2 have
2952 different signedness. We'll promote the signed to unsigned
2953 and later code won't know it used to be different.
2954 Do this check on the original types, so that explicit casts
2955 will be considered, but default promotions won't. */
2956 if (warn_sign_compare && !skip_evaluation)
2958 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2959 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2961 if (unsigned_op1 ^ unsigned_op2)
2963 /* Do not warn if the result type is signed, since the
2964 signed type will only be chosen if it can represent
2965 all the values of the unsigned type. */
2966 if (!TYPE_UNSIGNED (result_type))
2967 /* OK */;
2968 /* Do not warn if the signed quantity is an unsuffixed
2969 integer literal (or some static constant expression
2970 involving such literals) and it is non-negative. */
2971 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2972 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2973 /* OK */;
2974 else
2975 warning ("signed and unsigned type in conditional expression");
2979 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2981 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2982 pedwarn ("ISO C forbids conditional expr with only one void side");
2983 result_type = void_type_node;
2985 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2987 if (comp_target_types (type1, type2, 1))
2988 result_type = common_pointer_type (type1, type2);
2989 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2990 && TREE_CODE (orig_op1) != NOP_EXPR)
2991 result_type = qualify_type (type2, type1);
2992 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2993 && TREE_CODE (orig_op2) != NOP_EXPR)
2994 result_type = qualify_type (type1, type2);
2995 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2997 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2998 pedwarn ("ISO C forbids conditional expr between "
2999 "%<void *%> and function pointer");
3000 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3001 TREE_TYPE (type2)));
3003 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3005 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3006 pedwarn ("ISO C forbids conditional expr between "
3007 "%<void *%> and function pointer");
3008 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3009 TREE_TYPE (type1)));
3011 else
3013 pedwarn ("pointer type mismatch in conditional expression");
3014 result_type = build_pointer_type (void_type_node);
3017 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3019 if (!integer_zerop (op2))
3020 pedwarn ("pointer/integer type mismatch in conditional expression");
3021 else
3023 op2 = null_pointer_node;
3025 result_type = type1;
3027 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3029 if (!integer_zerop (op1))
3030 pedwarn ("pointer/integer type mismatch in conditional expression");
3031 else
3033 op1 = null_pointer_node;
3035 result_type = type2;
3038 if (!result_type)
3040 if (flag_cond_mismatch)
3041 result_type = void_type_node;
3042 else
3044 error ("type mismatch in conditional expression");
3045 return error_mark_node;
3049 /* Merge const and volatile flags of the incoming types. */
3050 result_type
3051 = build_type_variant (result_type,
3052 TREE_READONLY (op1) || TREE_READONLY (op2),
3053 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3055 if (result_type != TREE_TYPE (op1))
3056 op1 = convert_and_check (result_type, op1);
3057 if (result_type != TREE_TYPE (op2))
3058 op2 = convert_and_check (result_type, op2);
3060 if (TREE_CODE (ifexp) == INTEGER_CST)
3061 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3063 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3066 /* Return a compound expression that performs two expressions and
3067 returns the value of the second of them. */
3069 tree
3070 build_compound_expr (tree expr1, tree expr2)
3072 /* Convert arrays and functions to pointers. */
3073 expr2 = default_function_array_conversion (expr2);
3075 if (!TREE_SIDE_EFFECTS (expr1))
3077 /* The left-hand operand of a comma expression is like an expression
3078 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3079 any side-effects, unless it was explicitly cast to (void). */
3080 if (warn_unused_value
3081 && !(TREE_CODE (expr1) == CONVERT_EXPR
3082 && VOID_TYPE_P (TREE_TYPE (expr1))))
3083 warning ("left-hand operand of comma expression has no effect");
3086 /* With -Wunused, we should also warn if the left-hand operand does have
3087 side-effects, but computes a value which is not used. For example, in
3088 `foo() + bar(), baz()' the result of the `+' operator is not used,
3089 so we should issue a warning. */
3090 else if (warn_unused_value)
3091 warn_if_unused_value (expr1, input_location);
3093 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3096 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3098 tree
3099 build_c_cast (tree type, tree expr)
3101 tree value = expr;
3103 if (type == error_mark_node || expr == error_mark_node)
3104 return error_mark_node;
3106 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3107 only in <protocol> qualifications. But when constructing cast expressions,
3108 the protocols do matter and must be kept around. */
3109 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3110 return build1 (NOP_EXPR, type, expr);
3112 type = TYPE_MAIN_VARIANT (type);
3114 if (TREE_CODE (type) == ARRAY_TYPE)
3116 error ("cast specifies array type");
3117 return error_mark_node;
3120 if (TREE_CODE (type) == FUNCTION_TYPE)
3122 error ("cast specifies function type");
3123 return error_mark_node;
3126 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3128 if (pedantic)
3130 if (TREE_CODE (type) == RECORD_TYPE
3131 || TREE_CODE (type) == UNION_TYPE)
3132 pedwarn ("ISO C forbids casting nonscalar to the same type");
3135 else if (TREE_CODE (type) == UNION_TYPE)
3137 tree field;
3138 value = default_function_array_conversion (value);
3140 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3141 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3142 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3143 break;
3145 if (field)
3147 tree t;
3149 if (pedantic)
3150 pedwarn ("ISO C forbids casts to union type");
3151 t = digest_init (type,
3152 build_constructor (type,
3153 build_tree_list (field, value)),
3154 true, 0);
3155 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3156 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3157 return t;
3159 error ("cast to union type from type not present in union");
3160 return error_mark_node;
3162 else
3164 tree otype, ovalue;
3166 /* If casting to void, avoid the error that would come
3167 from default_conversion in the case of a non-lvalue array. */
3168 if (type == void_type_node)
3169 return build1 (CONVERT_EXPR, type, value);
3171 /* Convert functions and arrays to pointers,
3172 but don't convert any other types. */
3173 value = default_function_array_conversion (value);
3174 otype = TREE_TYPE (value);
3176 /* Optionally warn about potentially worrisome casts. */
3178 if (warn_cast_qual
3179 && TREE_CODE (type) == POINTER_TYPE
3180 && TREE_CODE (otype) == POINTER_TYPE)
3182 tree in_type = type;
3183 tree in_otype = otype;
3184 int added = 0;
3185 int discarded = 0;
3187 /* Check that the qualifiers on IN_TYPE are a superset of
3188 the qualifiers of IN_OTYPE. The outermost level of
3189 POINTER_TYPE nodes is uninteresting and we stop as soon
3190 as we hit a non-POINTER_TYPE node on either type. */
3193 in_otype = TREE_TYPE (in_otype);
3194 in_type = TREE_TYPE (in_type);
3196 /* GNU C allows cv-qualified function types. 'const'
3197 means the function is very pure, 'volatile' means it
3198 can't return. We need to warn when such qualifiers
3199 are added, not when they're taken away. */
3200 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3201 && TREE_CODE (in_type) == FUNCTION_TYPE)
3202 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3203 else
3204 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3206 while (TREE_CODE (in_type) == POINTER_TYPE
3207 && TREE_CODE (in_otype) == POINTER_TYPE);
3209 if (added)
3210 warning ("cast adds new qualifiers to function type");
3212 if (discarded)
3213 /* There are qualifiers present in IN_OTYPE that are not
3214 present in IN_TYPE. */
3215 warning ("cast discards qualifiers from pointer target type");
3218 /* Warn about possible alignment problems. */
3219 if (STRICT_ALIGNMENT && warn_cast_align
3220 && TREE_CODE (type) == POINTER_TYPE
3221 && TREE_CODE (otype) == POINTER_TYPE
3222 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3223 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3224 /* Don't warn about opaque types, where the actual alignment
3225 restriction is unknown. */
3226 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3227 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3228 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3229 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3230 warning ("cast increases required alignment of target type");
3232 if (TREE_CODE (type) == INTEGER_TYPE
3233 && TREE_CODE (otype) == POINTER_TYPE
3234 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3235 && !TREE_CONSTANT (value))
3236 warning ("cast from pointer to integer of different size");
3238 if (warn_bad_function_cast
3239 && TREE_CODE (value) == CALL_EXPR
3240 && TREE_CODE (type) != TREE_CODE (otype))
3241 warning ("cast from function call of type %qT to non-matching "
3242 "type %qT", otype, type);
3244 if (TREE_CODE (type) == POINTER_TYPE
3245 && TREE_CODE (otype) == INTEGER_TYPE
3246 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3247 /* Don't warn about converting any constant. */
3248 && !TREE_CONSTANT (value))
3249 warning ("cast to pointer from integer of different size");
3251 if (TREE_CODE (type) == POINTER_TYPE
3252 && TREE_CODE (otype) == POINTER_TYPE
3253 && TREE_CODE (expr) == ADDR_EXPR
3254 && DECL_P (TREE_OPERAND (expr, 0))
3255 && flag_strict_aliasing && warn_strict_aliasing
3256 && !VOID_TYPE_P (TREE_TYPE (type)))
3258 /* Casting the address of a decl to non void pointer. Warn
3259 if the cast breaks type based aliasing. */
3260 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3261 warning ("type-punning to incomplete type might break strict-aliasing rules");
3262 else
3264 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3265 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3267 if (!alias_sets_conflict_p (set1, set2))
3268 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3269 else if (warn_strict_aliasing > 1
3270 && !alias_sets_might_conflict_p (set1, set2))
3271 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3275 /* If pedantic, warn for conversions between function and object
3276 pointer types, except for converting a null pointer constant
3277 to function pointer type. */
3278 if (pedantic
3279 && TREE_CODE (type) == POINTER_TYPE
3280 && TREE_CODE (otype) == POINTER_TYPE
3281 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3282 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3283 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3285 if (pedantic
3286 && TREE_CODE (type) == POINTER_TYPE
3287 && TREE_CODE (otype) == POINTER_TYPE
3288 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3289 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3290 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3291 && TREE_CODE (expr) != NOP_EXPR))
3292 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3294 ovalue = value;
3295 value = convert (type, value);
3297 /* Ignore any integer overflow caused by the cast. */
3298 if (TREE_CODE (value) == INTEGER_CST)
3300 if (EXPR_P (ovalue))
3301 /* If OVALUE had overflow set, then so will VALUE, so it
3302 is safe to overwrite. */
3303 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3304 else
3305 TREE_OVERFLOW (value) = 0;
3307 if (CONSTANT_CLASS_P (ovalue))
3308 /* Similarly, constant_overflow cannot have become
3309 cleared. */
3310 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3314 /* Don't let a cast be an lvalue. */
3315 if (value == expr)
3316 value = non_lvalue (value);
3318 return value;
3321 /* Interpret a cast of expression EXPR to type TYPE. */
3322 tree
3323 c_cast_expr (struct c_type_name *type_name, tree expr)
3325 tree type;
3326 int saved_wsp = warn_strict_prototypes;
3328 /* This avoids warnings about unprototyped casts on
3329 integers. E.g. "#define SIG_DFL (void(*)())0". */
3330 if (TREE_CODE (expr) == INTEGER_CST)
3331 warn_strict_prototypes = 0;
3332 type = groktypename (type_name);
3333 warn_strict_prototypes = saved_wsp;
3335 return build_c_cast (type, expr);
3339 /* Build an assignment expression of lvalue LHS from value RHS.
3340 MODIFYCODE is the code for a binary operator that we use
3341 to combine the old value of LHS with RHS to get the new value.
3342 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3344 tree
3345 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3347 tree result;
3348 tree newrhs;
3349 tree lhstype = TREE_TYPE (lhs);
3350 tree olhstype = lhstype;
3352 /* Types that aren't fully specified cannot be used in assignments. */
3353 lhs = require_complete_type (lhs);
3355 /* Avoid duplicate error messages from operands that had errors. */
3356 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3357 return error_mark_node;
3359 STRIP_TYPE_NOPS (rhs);
3361 newrhs = rhs;
3363 /* If a binary op has been requested, combine the old LHS value with the RHS
3364 producing the value we should actually store into the LHS. */
3366 if (modifycode != NOP_EXPR)
3368 lhs = stabilize_reference (lhs);
3369 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3372 if (!lvalue_or_else (lhs, lv_assign))
3373 return error_mark_node;
3375 /* Give an error for storing in something that is 'const'. */
3377 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3378 || ((TREE_CODE (lhstype) == RECORD_TYPE
3379 || TREE_CODE (lhstype) == UNION_TYPE)
3380 && C_TYPE_FIELDS_READONLY (lhstype)))
3381 readonly_error (lhs, lv_assign);
3383 /* If storing into a structure or union member,
3384 it has probably been given type `int'.
3385 Compute the type that would go with
3386 the actual amount of storage the member occupies. */
3388 if (TREE_CODE (lhs) == COMPONENT_REF
3389 && (TREE_CODE (lhstype) == INTEGER_TYPE
3390 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3391 || TREE_CODE (lhstype) == REAL_TYPE
3392 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3393 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3395 /* If storing in a field that is in actuality a short or narrower than one,
3396 we must store in the field in its actual type. */
3398 if (lhstype != TREE_TYPE (lhs))
3400 lhs = copy_node (lhs);
3401 TREE_TYPE (lhs) = lhstype;
3404 /* Convert new value to destination type. */
3406 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3407 NULL_TREE, NULL_TREE, 0);
3408 if (TREE_CODE (newrhs) == ERROR_MARK)
3409 return error_mark_node;
3411 /* Scan operands. */
3413 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3414 TREE_SIDE_EFFECTS (result) = 1;
3416 /* If we got the LHS in a different type for storing in,
3417 convert the result back to the nominal type of LHS
3418 so that the value we return always has the same type
3419 as the LHS argument. */
3421 if (olhstype == TREE_TYPE (result))
3422 return result;
3423 return convert_for_assignment (olhstype, result, ic_assign,
3424 NULL_TREE, NULL_TREE, 0);
3427 /* Convert value RHS to type TYPE as preparation for an assignment
3428 to an lvalue of type TYPE.
3429 The real work of conversion is done by `convert'.
3430 The purpose of this function is to generate error messages
3431 for assignments that are not allowed in C.
3432 ERRTYPE says whether it is argument passing, assignment,
3433 initialization or return.
3435 FUNCTION is a tree for the function being called.
3436 PARMNUM is the number of the argument, for printing in error messages. */
3438 static tree
3439 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3440 tree fundecl, tree function, int parmnum)
3442 enum tree_code codel = TREE_CODE (type);
3443 tree rhstype;
3444 enum tree_code coder;
3445 tree rname = NULL_TREE;
3447 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3449 tree selector;
3450 /* Change pointer to function to the function itself for
3451 diagnostics. */
3452 if (TREE_CODE (function) == ADDR_EXPR
3453 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3454 function = TREE_OPERAND (function, 0);
3456 /* Handle an ObjC selector specially for diagnostics. */
3457 selector = objc_message_selector ();
3458 rname = function;
3459 if (selector && parmnum > 2)
3461 rname = selector;
3462 parmnum -= 2;
3466 /* This macro is used to emit diagnostics to ensure that all format
3467 strings are complete sentences, visible to gettext and checked at
3468 compile time. */
3469 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3470 do { \
3471 switch (errtype) \
3473 case ic_argpass: \
3474 pedwarn (AR, parmnum, rname); \
3475 break; \
3476 case ic_argpass_nonproto: \
3477 warning (AR, parmnum, rname); \
3478 break; \
3479 case ic_assign: \
3480 pedwarn (AS); \
3481 break; \
3482 case ic_init: \
3483 pedwarn (IN); \
3484 break; \
3485 case ic_return: \
3486 pedwarn (RE); \
3487 break; \
3488 default: \
3489 gcc_unreachable (); \
3491 } while (0)
3493 STRIP_TYPE_NOPS (rhs);
3495 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3496 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3497 rhs = default_conversion (rhs);
3498 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3499 rhs = decl_constant_value_for_broken_optimization (rhs);
3501 rhstype = TREE_TYPE (rhs);
3502 coder = TREE_CODE (rhstype);
3504 if (coder == ERROR_MARK)
3505 return error_mark_node;
3507 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3509 overflow_warning (rhs);
3510 /* Check for Objective-C protocols. This will automatically
3511 issue a warning if there are protocol violations. No need to
3512 use the return value. */
3513 if (c_dialect_objc ())
3514 objc_comptypes (type, rhstype, 0);
3515 return rhs;
3518 if (coder == VOID_TYPE)
3520 /* Except for passing an argument to an unprototyped function,
3521 this is a constraint violation. When passing an argument to
3522 an unprototyped function, it is compile-time undefined;
3523 making it a constraint in that case was rejected in
3524 DR#252. */
3525 error ("void value not ignored as it ought to be");
3526 return error_mark_node;
3528 /* A type converts to a reference to it.
3529 This code doesn't fully support references, it's just for the
3530 special case of va_start and va_copy. */
3531 if (codel == REFERENCE_TYPE
3532 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3534 if (!lvalue_p (rhs))
3536 error ("cannot pass rvalue to reference parameter");
3537 return error_mark_node;
3539 if (!c_mark_addressable (rhs))
3540 return error_mark_node;
3541 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3543 /* We already know that these two types are compatible, but they
3544 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3545 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3546 likely to be va_list, a typedef to __builtin_va_list, which
3547 is different enough that it will cause problems later. */
3548 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3549 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3551 rhs = build1 (NOP_EXPR, type, rhs);
3552 return rhs;
3554 /* Some types can interconvert without explicit casts. */
3555 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3556 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3557 return convert (type, rhs);
3558 /* Arithmetic types all interconvert, and enum is treated like int. */
3559 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3560 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3561 || codel == BOOLEAN_TYPE)
3562 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3563 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3564 || coder == BOOLEAN_TYPE))
3565 return convert_and_check (type, rhs);
3567 /* Conversion to a transparent union from its member types.
3568 This applies only to function arguments. */
3569 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3570 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3572 tree memb_types;
3573 tree marginal_memb_type = 0;
3575 for (memb_types = TYPE_FIELDS (type); memb_types;
3576 memb_types = TREE_CHAIN (memb_types))
3578 tree memb_type = TREE_TYPE (memb_types);
3580 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3581 TYPE_MAIN_VARIANT (rhstype)))
3582 break;
3584 if (TREE_CODE (memb_type) != POINTER_TYPE)
3585 continue;
3587 if (coder == POINTER_TYPE)
3589 tree ttl = TREE_TYPE (memb_type);
3590 tree ttr = TREE_TYPE (rhstype);
3592 /* Any non-function converts to a [const][volatile] void *
3593 and vice versa; otherwise, targets must be the same.
3594 Meanwhile, the lhs target must have all the qualifiers of
3595 the rhs. */
3596 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3597 || comp_target_types (memb_type, rhstype, 0))
3599 /* If this type won't generate any warnings, use it. */
3600 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3601 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3602 && TREE_CODE (ttl) == FUNCTION_TYPE)
3603 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3604 == TYPE_QUALS (ttr))
3605 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3606 == TYPE_QUALS (ttl))))
3607 break;
3609 /* Keep looking for a better type, but remember this one. */
3610 if (!marginal_memb_type)
3611 marginal_memb_type = memb_type;
3615 /* Can convert integer zero to any pointer type. */
3616 if (integer_zerop (rhs)
3617 || (TREE_CODE (rhs) == NOP_EXPR
3618 && integer_zerop (TREE_OPERAND (rhs, 0))))
3620 rhs = null_pointer_node;
3621 break;
3625 if (memb_types || marginal_memb_type)
3627 if (!memb_types)
3629 /* We have only a marginally acceptable member type;
3630 it needs a warning. */
3631 tree ttl = TREE_TYPE (marginal_memb_type);
3632 tree ttr = TREE_TYPE (rhstype);
3634 /* Const and volatile mean something different for function
3635 types, so the usual warnings are not appropriate. */
3636 if (TREE_CODE (ttr) == FUNCTION_TYPE
3637 && TREE_CODE (ttl) == FUNCTION_TYPE)
3639 /* Because const and volatile on functions are
3640 restrictions that say the function will not do
3641 certain things, it is okay to use a const or volatile
3642 function where an ordinary one is wanted, but not
3643 vice-versa. */
3644 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3645 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3646 "makes qualified function "
3647 "pointer from unqualified"),
3648 N_("assignment makes qualified "
3649 "function pointer from "
3650 "unqualified"),
3651 N_("initialization makes qualified "
3652 "function pointer from "
3653 "unqualified"),
3654 N_("return makes qualified function "
3655 "pointer from unqualified"));
3657 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3658 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3659 "qualifiers from pointer target type"),
3660 N_("assignment discards qualifiers "
3661 "from pointer target type"),
3662 N_("initialization discards qualifiers "
3663 "from pointer target type"),
3664 N_("return discards qualifiers from "
3665 "pointer target type"));
3668 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3669 pedwarn ("ISO C prohibits argument conversion to union type");
3671 return build1 (NOP_EXPR, type, rhs);
3675 /* Conversions among pointers */
3676 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3677 && (coder == codel))
3679 tree ttl = TREE_TYPE (type);
3680 tree ttr = TREE_TYPE (rhstype);
3681 tree mvl = ttl;
3682 tree mvr = ttr;
3683 bool is_opaque_pointer;
3684 int target_cmp = 0; /* Cache comp_target_types () result. */
3686 if (TREE_CODE (mvl) != ARRAY_TYPE)
3687 mvl = TYPE_MAIN_VARIANT (mvl);
3688 if (TREE_CODE (mvr) != ARRAY_TYPE)
3689 mvr = TYPE_MAIN_VARIANT (mvr);
3690 /* Opaque pointers are treated like void pointers. */
3691 is_opaque_pointer = (targetm.vector_opaque_p (type)
3692 || targetm.vector_opaque_p (rhstype))
3693 && TREE_CODE (ttl) == VECTOR_TYPE
3694 && TREE_CODE (ttr) == VECTOR_TYPE;
3696 /* Any non-function converts to a [const][volatile] void *
3697 and vice versa; otherwise, targets must be the same.
3698 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3699 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3700 || (target_cmp = comp_target_types (type, rhstype, 0))
3701 || is_opaque_pointer
3702 || (c_common_unsigned_type (mvl)
3703 == c_common_unsigned_type (mvr)))
3705 if (pedantic
3706 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3708 (VOID_TYPE_P (ttr)
3709 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3710 which are not ANSI null ptr constants. */
3711 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3712 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3713 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3714 "%qE between function pointer "
3715 "and %<void *%>"),
3716 N_("ISO C forbids assignment between "
3717 "function pointer and %<void *%>"),
3718 N_("ISO C forbids initialization between "
3719 "function pointer and %<void *%>"),
3720 N_("ISO C forbids return between function "
3721 "pointer and %<void *%>"));
3722 /* Const and volatile mean something different for function types,
3723 so the usual warnings are not appropriate. */
3724 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3725 && TREE_CODE (ttl) != FUNCTION_TYPE)
3727 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3728 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3729 "qualifiers from pointer target type"),
3730 N_("assignment discards qualifiers "
3731 "from pointer target type"),
3732 N_("initialization discards qualifiers "
3733 "from pointer target type"),
3734 N_("return discards qualifiers from "
3735 "pointer target type"));
3736 /* If this is not a case of ignoring a mismatch in signedness,
3737 no warning. */
3738 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3739 || target_cmp)
3741 /* If there is a mismatch, do warn. */
3742 else if (warn_pointer_sign)
3743 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3744 "%d of %qE differ in signedness"),
3745 N_("pointer targets in assignment "
3746 "differ in signedness"),
3747 N_("pointer targets in initialization "
3748 "differ in signedness"),
3749 N_("pointer targets in return differ "
3750 "in signedness"));
3752 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3753 && TREE_CODE (ttr) == FUNCTION_TYPE)
3755 /* Because const and volatile on functions are restrictions
3756 that say the function will not do certain things,
3757 it is okay to use a const or volatile function
3758 where an ordinary one is wanted, but not vice-versa. */
3759 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3760 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3761 "qualified function pointer "
3762 "from unqualified"),
3763 N_("assignment makes qualified function "
3764 "pointer from unqualified"),
3765 N_("initialization makes qualified "
3766 "function pointer from unqualified"),
3767 N_("return makes qualified function "
3768 "pointer from unqualified"));
3771 else
3772 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3773 "incompatible pointer type"),
3774 N_("assignment from incompatible pointer type"),
3775 N_("initialization from incompatible "
3776 "pointer type"),
3777 N_("return from incompatible pointer type"));
3778 return convert (type, rhs);
3780 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3782 /* ??? This should not be an error when inlining calls to
3783 unprototyped functions. */
3784 error ("invalid use of non-lvalue array");
3785 return error_mark_node;
3787 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3789 /* An explicit constant 0 can convert to a pointer,
3790 or one that results from arithmetic, even including
3791 a cast to integer type. */
3792 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3794 !(TREE_CODE (rhs) == NOP_EXPR
3795 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3796 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3797 && integer_zerop (TREE_OPERAND (rhs, 0))))
3798 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3799 "pointer from integer without a cast"),
3800 N_("assignment makes pointer from integer "
3801 "without a cast"),
3802 N_("initialization makes pointer from "
3803 "integer without a cast"),
3804 N_("return makes pointer from integer "
3805 "without a cast"));
3807 return convert (type, rhs);
3809 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3811 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3812 "from pointer without a cast"),
3813 N_("assignment makes integer from pointer "
3814 "without a cast"),
3815 N_("initialization makes integer from pointer "
3816 "without a cast"),
3817 N_("return makes integer from pointer "
3818 "without a cast"));
3819 return convert (type, rhs);
3821 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3822 return convert (type, rhs);
3824 switch (errtype)
3826 case ic_argpass:
3827 case ic_argpass_nonproto:
3828 /* ??? This should not be an error when inlining calls to
3829 unprototyped functions. */
3830 error ("incompatible type for argument %d of %qE", parmnum, rname);
3831 break;
3832 case ic_assign:
3833 error ("incompatible types in assignment");
3834 break;
3835 case ic_init:
3836 error ("incompatible types in initialization");
3837 break;
3838 case ic_return:
3839 error ("incompatible types in return");
3840 break;
3841 default:
3842 gcc_unreachable ();
3845 return error_mark_node;
3848 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3849 is used for error and waring reporting and indicates which argument
3850 is being processed. */
3852 tree
3853 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3855 tree ret, type;
3857 /* If FN was prototyped, the value has been converted already
3858 in convert_arguments. */
3859 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3860 return value;
3862 type = TREE_TYPE (parm);
3863 ret = convert_for_assignment (type, value,
3864 ic_argpass_nonproto, fn,
3865 fn, argnum);
3866 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3867 && INTEGRAL_TYPE_P (type)
3868 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3869 ret = default_conversion (ret);
3870 return ret;
3873 /* If VALUE is a compound expr all of whose expressions are constant, then
3874 return its value. Otherwise, return error_mark_node.
3876 This is for handling COMPOUND_EXPRs as initializer elements
3877 which is allowed with a warning when -pedantic is specified. */
3879 static tree
3880 valid_compound_expr_initializer (tree value, tree endtype)
3882 if (TREE_CODE (value) == COMPOUND_EXPR)
3884 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3885 == error_mark_node)
3886 return error_mark_node;
3887 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3888 endtype);
3890 else if (!initializer_constant_valid_p (value, endtype))
3891 return error_mark_node;
3892 else
3893 return value;
3896 /* Perform appropriate conversions on the initial value of a variable,
3897 store it in the declaration DECL,
3898 and print any error messages that are appropriate.
3899 If the init is invalid, store an ERROR_MARK. */
3901 void
3902 store_init_value (tree decl, tree init)
3904 tree value, type;
3906 /* If variable's type was invalidly declared, just ignore it. */
3908 type = TREE_TYPE (decl);
3909 if (TREE_CODE (type) == ERROR_MARK)
3910 return;
3912 /* Digest the specified initializer into an expression. */
3914 value = digest_init (type, init, true, TREE_STATIC (decl));
3916 /* Store the expression if valid; else report error. */
3918 if (warn_traditional && !in_system_header
3919 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3920 warning ("traditional C rejects automatic aggregate initialization");
3922 DECL_INITIAL (decl) = value;
3924 /* ANSI wants warnings about out-of-range constant initializers. */
3925 STRIP_TYPE_NOPS (value);
3926 constant_expression_warning (value);
3928 /* Check if we need to set array size from compound literal size. */
3929 if (TREE_CODE (type) == ARRAY_TYPE
3930 && TYPE_DOMAIN (type) == 0
3931 && value != error_mark_node)
3933 tree inside_init = init;
3935 STRIP_TYPE_NOPS (inside_init);
3936 inside_init = fold (inside_init);
3938 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3940 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3942 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3944 /* For int foo[] = (int [3]){1}; we need to set array size
3945 now since later on array initializer will be just the
3946 brace enclosed list of the compound literal. */
3947 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3948 layout_type (type);
3949 layout_decl (decl, 0);
3955 /* Methods for storing and printing names for error messages. */
3957 /* Implement a spelling stack that allows components of a name to be pushed
3958 and popped. Each element on the stack is this structure. */
3960 struct spelling
3962 int kind;
3963 union
3965 int i;
3966 const char *s;
3967 } u;
3970 #define SPELLING_STRING 1
3971 #define SPELLING_MEMBER 2
3972 #define SPELLING_BOUNDS 3
3974 static struct spelling *spelling; /* Next stack element (unused). */
3975 static struct spelling *spelling_base; /* Spelling stack base. */
3976 static int spelling_size; /* Size of the spelling stack. */
3978 /* Macros to save and restore the spelling stack around push_... functions.
3979 Alternative to SAVE_SPELLING_STACK. */
3981 #define SPELLING_DEPTH() (spelling - spelling_base)
3982 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3984 /* Push an element on the spelling stack with type KIND and assign VALUE
3985 to MEMBER. */
3987 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3989 int depth = SPELLING_DEPTH (); \
3991 if (depth >= spelling_size) \
3993 spelling_size += 10; \
3994 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3995 spelling_size); \
3996 RESTORE_SPELLING_DEPTH (depth); \
3999 spelling->kind = (KIND); \
4000 spelling->MEMBER = (VALUE); \
4001 spelling++; \
4004 /* Push STRING on the stack. Printed literally. */
4006 static void
4007 push_string (const char *string)
4009 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4012 /* Push a member name on the stack. Printed as '.' STRING. */
4014 static void
4015 push_member_name (tree decl)
4017 const char *const string
4018 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4019 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4022 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4024 static void
4025 push_array_bounds (int bounds)
4027 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4030 /* Compute the maximum size in bytes of the printed spelling. */
4032 static int
4033 spelling_length (void)
4035 int size = 0;
4036 struct spelling *p;
4038 for (p = spelling_base; p < spelling; p++)
4040 if (p->kind == SPELLING_BOUNDS)
4041 size += 25;
4042 else
4043 size += strlen (p->u.s) + 1;
4046 return size;
4049 /* Print the spelling to BUFFER and return it. */
4051 static char *
4052 print_spelling (char *buffer)
4054 char *d = buffer;
4055 struct spelling *p;
4057 for (p = spelling_base; p < spelling; p++)
4058 if (p->kind == SPELLING_BOUNDS)
4060 sprintf (d, "[%d]", p->u.i);
4061 d += strlen (d);
4063 else
4065 const char *s;
4066 if (p->kind == SPELLING_MEMBER)
4067 *d++ = '.';
4068 for (s = p->u.s; (*d = *s++); d++)
4071 *d++ = '\0';
4072 return buffer;
4075 /* Issue an error message for a bad initializer component.
4076 MSGID identifies the message.
4077 The component name is taken from the spelling stack. */
4079 void
4080 error_init (const char *msgid)
4082 char *ofwhat;
4084 error ("%s", _(msgid));
4085 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4086 if (*ofwhat)
4087 error ("(near initialization for %qs)", ofwhat);
4090 /* Issue a pedantic warning for a bad initializer component.
4091 MSGID identifies the message.
4092 The component name is taken from the spelling stack. */
4094 void
4095 pedwarn_init (const char *msgid)
4097 char *ofwhat;
4099 pedwarn ("%s", _(msgid));
4100 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4101 if (*ofwhat)
4102 pedwarn ("(near initialization for %qs)", ofwhat);
4105 /* Issue a warning for a bad initializer component.
4106 MSGID identifies the message.
4107 The component name is taken from the spelling stack. */
4109 static void
4110 warning_init (const char *msgid)
4112 char *ofwhat;
4114 warning ("%s", _(msgid));
4115 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4116 if (*ofwhat)
4117 warning ("(near initialization for %qs)", ofwhat);
4120 /* If TYPE is an array type and EXPR is a parenthesized string
4121 constant, warn if pedantic that EXPR is being used to initialize an
4122 object of type TYPE. */
4124 void
4125 maybe_warn_string_init (tree type, struct c_expr expr)
4127 if (pedantic
4128 && TREE_CODE (type) == ARRAY_TYPE
4129 && TREE_CODE (expr.value) == STRING_CST
4130 && expr.original_code != STRING_CST)
4131 pedwarn_init ("array initialized from parenthesized string constant");
4134 /* Digest the parser output INIT as an initializer for type TYPE.
4135 Return a C expression of type TYPE to represent the initial value.
4137 If INIT is a string constant, STRICT_STRING is true if it is
4138 unparenthesized or we should not warn here for it being parenthesized.
4139 For other types of INIT, STRICT_STRING is not used.
4141 REQUIRE_CONSTANT requests an error if non-constant initializers or
4142 elements are seen. */
4144 static tree
4145 digest_init (tree type, tree init, bool strict_string, int require_constant)
4147 enum tree_code code = TREE_CODE (type);
4148 tree inside_init = init;
4150 if (type == error_mark_node
4151 || init == error_mark_node
4152 || TREE_TYPE (init) == error_mark_node)
4153 return error_mark_node;
4155 STRIP_TYPE_NOPS (inside_init);
4157 inside_init = fold (inside_init);
4159 /* Initialization of an array of chars from a string constant
4160 optionally enclosed in braces. */
4162 if (code == ARRAY_TYPE && inside_init
4163 && TREE_CODE (inside_init) == STRING_CST)
4165 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4166 /* Note that an array could be both an array of character type
4167 and an array of wchar_t if wchar_t is signed char or unsigned
4168 char. */
4169 bool char_array = (typ1 == char_type_node
4170 || typ1 == signed_char_type_node
4171 || typ1 == unsigned_char_type_node);
4172 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4173 if (char_array || wchar_array)
4175 struct c_expr expr;
4176 bool char_string;
4177 expr.value = inside_init;
4178 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4179 maybe_warn_string_init (type, expr);
4181 char_string
4182 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4183 == char_type_node);
4185 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4186 TYPE_MAIN_VARIANT (type)))
4187 return inside_init;
4189 if (!wchar_array && !char_string)
4191 error_init ("char-array initialized from wide string");
4192 return error_mark_node;
4194 if (char_string && !char_array)
4196 error_init ("wchar_t-array initialized from non-wide string");
4197 return error_mark_node;
4200 TREE_TYPE (inside_init) = type;
4201 if (TYPE_DOMAIN (type) != 0
4202 && TYPE_SIZE (type) != 0
4203 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4204 /* Subtract 1 (or sizeof (wchar_t))
4205 because it's ok to ignore the terminating null char
4206 that is counted in the length of the constant. */
4207 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4208 TREE_STRING_LENGTH (inside_init)
4209 - ((TYPE_PRECISION (typ1)
4210 != TYPE_PRECISION (char_type_node))
4211 ? (TYPE_PRECISION (wchar_type_node)
4212 / BITS_PER_UNIT)
4213 : 1)))
4214 pedwarn_init ("initializer-string for array of chars is too long");
4216 return inside_init;
4218 else if (INTEGRAL_TYPE_P (typ1))
4220 error_init ("array of inappropriate type initialized "
4221 "from string constant");
4222 return error_mark_node;
4226 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4227 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4228 below and handle as a constructor. */
4229 if (code == VECTOR_TYPE
4230 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4231 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4232 && TREE_CONSTANT (inside_init))
4234 if (TREE_CODE (inside_init) == VECTOR_CST
4235 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4236 TYPE_MAIN_VARIANT (type)))
4237 return inside_init;
4239 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4241 tree link;
4243 /* Iterate through elements and check if all constructor
4244 elements are *_CSTs. */
4245 for (link = CONSTRUCTOR_ELTS (inside_init);
4246 link;
4247 link = TREE_CHAIN (link))
4248 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4249 break;
4251 if (link == NULL)
4252 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4256 /* Any type can be initialized
4257 from an expression of the same type, optionally with braces. */
4259 if (inside_init && TREE_TYPE (inside_init) != 0
4260 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4261 TYPE_MAIN_VARIANT (type))
4262 || (code == ARRAY_TYPE
4263 && comptypes (TREE_TYPE (inside_init), type))
4264 || (code == VECTOR_TYPE
4265 && comptypes (TREE_TYPE (inside_init), type))
4266 || (code == POINTER_TYPE
4267 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4268 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4269 TREE_TYPE (type)))
4270 || (code == POINTER_TYPE
4271 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4272 && comptypes (TREE_TYPE (inside_init),
4273 TREE_TYPE (type)))))
4275 if (code == POINTER_TYPE)
4277 inside_init = default_function_array_conversion (inside_init);
4279 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4281 error_init ("invalid use of non-lvalue array");
4282 return error_mark_node;
4286 if (code == VECTOR_TYPE)
4287 /* Although the types are compatible, we may require a
4288 conversion. */
4289 inside_init = convert (type, inside_init);
4291 if (require_constant && !flag_isoc99
4292 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4294 /* As an extension, allow initializing objects with static storage
4295 duration with compound literals (which are then treated just as
4296 the brace enclosed list they contain). */
4297 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4298 inside_init = DECL_INITIAL (decl);
4301 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4302 && TREE_CODE (inside_init) != CONSTRUCTOR)
4304 error_init ("array initialized from non-constant array expression");
4305 return error_mark_node;
4308 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4309 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4311 /* Compound expressions can only occur here if -pedantic or
4312 -pedantic-errors is specified. In the later case, we always want
4313 an error. In the former case, we simply want a warning. */
4314 if (require_constant && pedantic
4315 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4317 inside_init
4318 = valid_compound_expr_initializer (inside_init,
4319 TREE_TYPE (inside_init));
4320 if (inside_init == error_mark_node)
4321 error_init ("initializer element is not constant");
4322 else
4323 pedwarn_init ("initializer element is not constant");
4324 if (flag_pedantic_errors)
4325 inside_init = error_mark_node;
4327 else if (require_constant
4328 && !initializer_constant_valid_p (inside_init,
4329 TREE_TYPE (inside_init)))
4331 error_init ("initializer element is not constant");
4332 inside_init = error_mark_node;
4335 return inside_init;
4338 /* Handle scalar types, including conversions. */
4340 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4341 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4342 || code == VECTOR_TYPE)
4344 /* Note that convert_for_assignment calls default_conversion
4345 for arrays and functions. We must not call it in the
4346 case where inside_init is a null pointer constant. */
4347 inside_init
4348 = convert_for_assignment (type, init, ic_init,
4349 NULL_TREE, NULL_TREE, 0);
4351 /* Check to see if we have already given an error message. */
4352 if (inside_init == error_mark_node)
4354 else if (require_constant && !TREE_CONSTANT (inside_init))
4356 error_init ("initializer element is not constant");
4357 inside_init = error_mark_node;
4359 else if (require_constant
4360 && !initializer_constant_valid_p (inside_init,
4361 TREE_TYPE (inside_init)))
4363 error_init ("initializer element is not computable at load time");
4364 inside_init = error_mark_node;
4367 return inside_init;
4370 /* Come here only for records and arrays. */
4372 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4374 error_init ("variable-sized object may not be initialized");
4375 return error_mark_node;
4378 error_init ("invalid initializer");
4379 return error_mark_node;
4382 /* Handle initializers that use braces. */
4384 /* Type of object we are accumulating a constructor for.
4385 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4386 static tree constructor_type;
4388 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4389 left to fill. */
4390 static tree constructor_fields;
4392 /* For an ARRAY_TYPE, this is the specified index
4393 at which to store the next element we get. */
4394 static tree constructor_index;
4396 /* For an ARRAY_TYPE, this is the maximum index. */
4397 static tree constructor_max_index;
4399 /* For a RECORD_TYPE, this is the first field not yet written out. */
4400 static tree constructor_unfilled_fields;
4402 /* For an ARRAY_TYPE, this is the index of the first element
4403 not yet written out. */
4404 static tree constructor_unfilled_index;
4406 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4407 This is so we can generate gaps between fields, when appropriate. */
4408 static tree constructor_bit_index;
4410 /* If we are saving up the elements rather than allocating them,
4411 this is the list of elements so far (in reverse order,
4412 most recent first). */
4413 static tree constructor_elements;
4415 /* 1 if constructor should be incrementally stored into a constructor chain,
4416 0 if all the elements should be kept in AVL tree. */
4417 static int constructor_incremental;
4419 /* 1 if so far this constructor's elements are all compile-time constants. */
4420 static int constructor_constant;
4422 /* 1 if so far this constructor's elements are all valid address constants. */
4423 static int constructor_simple;
4425 /* 1 if this constructor is erroneous so far. */
4426 static int constructor_erroneous;
4428 /* Structure for managing pending initializer elements, organized as an
4429 AVL tree. */
4431 struct init_node
4433 struct init_node *left, *right;
4434 struct init_node *parent;
4435 int balance;
4436 tree purpose;
4437 tree value;
4440 /* Tree of pending elements at this constructor level.
4441 These are elements encountered out of order
4442 which belong at places we haven't reached yet in actually
4443 writing the output.
4444 Will never hold tree nodes across GC runs. */
4445 static struct init_node *constructor_pending_elts;
4447 /* The SPELLING_DEPTH of this constructor. */
4448 static int constructor_depth;
4450 /* DECL node for which an initializer is being read.
4451 0 means we are reading a constructor expression
4452 such as (struct foo) {...}. */
4453 static tree constructor_decl;
4455 /* Nonzero if this is an initializer for a top-level decl. */
4456 static int constructor_top_level;
4458 /* Nonzero if there were any member designators in this initializer. */
4459 static int constructor_designated;
4461 /* Nesting depth of designator list. */
4462 static int designator_depth;
4464 /* Nonzero if there were diagnosed errors in this designator list. */
4465 static int designator_errorneous;
4468 /* This stack has a level for each implicit or explicit level of
4469 structuring in the initializer, including the outermost one. It
4470 saves the values of most of the variables above. */
4472 struct constructor_range_stack;
4474 struct constructor_stack
4476 struct constructor_stack *next;
4477 tree type;
4478 tree fields;
4479 tree index;
4480 tree max_index;
4481 tree unfilled_index;
4482 tree unfilled_fields;
4483 tree bit_index;
4484 tree elements;
4485 struct init_node *pending_elts;
4486 int offset;
4487 int depth;
4488 /* If value nonzero, this value should replace the entire
4489 constructor at this level. */
4490 struct c_expr replacement_value;
4491 struct constructor_range_stack *range_stack;
4492 char constant;
4493 char simple;
4494 char implicit;
4495 char erroneous;
4496 char outer;
4497 char incremental;
4498 char designated;
4501 static struct constructor_stack *constructor_stack;
4503 /* This stack represents designators from some range designator up to
4504 the last designator in the list. */
4506 struct constructor_range_stack
4508 struct constructor_range_stack *next, *prev;
4509 struct constructor_stack *stack;
4510 tree range_start;
4511 tree index;
4512 tree range_end;
4513 tree fields;
4516 static struct constructor_range_stack *constructor_range_stack;
4518 /* This stack records separate initializers that are nested.
4519 Nested initializers can't happen in ANSI C, but GNU C allows them
4520 in cases like { ... (struct foo) { ... } ... }. */
4522 struct initializer_stack
4524 struct initializer_stack *next;
4525 tree decl;
4526 struct constructor_stack *constructor_stack;
4527 struct constructor_range_stack *constructor_range_stack;
4528 tree elements;
4529 struct spelling *spelling;
4530 struct spelling *spelling_base;
4531 int spelling_size;
4532 char top_level;
4533 char require_constant_value;
4534 char require_constant_elements;
4537 static struct initializer_stack *initializer_stack;
4539 /* Prepare to parse and output the initializer for variable DECL. */
4541 void
4542 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4544 const char *locus;
4545 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4547 p->decl = constructor_decl;
4548 p->require_constant_value = require_constant_value;
4549 p->require_constant_elements = require_constant_elements;
4550 p->constructor_stack = constructor_stack;
4551 p->constructor_range_stack = constructor_range_stack;
4552 p->elements = constructor_elements;
4553 p->spelling = spelling;
4554 p->spelling_base = spelling_base;
4555 p->spelling_size = spelling_size;
4556 p->top_level = constructor_top_level;
4557 p->next = initializer_stack;
4558 initializer_stack = p;
4560 constructor_decl = decl;
4561 constructor_designated = 0;
4562 constructor_top_level = top_level;
4564 if (decl != 0 && decl != error_mark_node)
4566 require_constant_value = TREE_STATIC (decl);
4567 require_constant_elements
4568 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4569 /* For a scalar, you can always use any value to initialize,
4570 even within braces. */
4571 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4572 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4573 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4574 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4575 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4577 else
4579 require_constant_value = 0;
4580 require_constant_elements = 0;
4581 locus = "(anonymous)";
4584 constructor_stack = 0;
4585 constructor_range_stack = 0;
4587 missing_braces_mentioned = 0;
4589 spelling_base = 0;
4590 spelling_size = 0;
4591 RESTORE_SPELLING_DEPTH (0);
4593 if (locus)
4594 push_string (locus);
4597 void
4598 finish_init (void)
4600 struct initializer_stack *p = initializer_stack;
4602 /* Free the whole constructor stack of this initializer. */
4603 while (constructor_stack)
4605 struct constructor_stack *q = constructor_stack;
4606 constructor_stack = q->next;
4607 free (q);
4610 gcc_assert (!constructor_range_stack);
4612 /* Pop back to the data of the outer initializer (if any). */
4613 free (spelling_base);
4615 constructor_decl = p->decl;
4616 require_constant_value = p->require_constant_value;
4617 require_constant_elements = p->require_constant_elements;
4618 constructor_stack = p->constructor_stack;
4619 constructor_range_stack = p->constructor_range_stack;
4620 constructor_elements = p->elements;
4621 spelling = p->spelling;
4622 spelling_base = p->spelling_base;
4623 spelling_size = p->spelling_size;
4624 constructor_top_level = p->top_level;
4625 initializer_stack = p->next;
4626 free (p);
4629 /* Call here when we see the initializer is surrounded by braces.
4630 This is instead of a call to push_init_level;
4631 it is matched by a call to pop_init_level.
4633 TYPE is the type to initialize, for a constructor expression.
4634 For an initializer for a decl, TYPE is zero. */
4636 void
4637 really_start_incremental_init (tree type)
4639 struct constructor_stack *p = XNEW (struct constructor_stack);
4641 if (type == 0)
4642 type = TREE_TYPE (constructor_decl);
4644 if (targetm.vector_opaque_p (type))
4645 error ("opaque vector types cannot be initialized");
4647 p->type = constructor_type;
4648 p->fields = constructor_fields;
4649 p->index = constructor_index;
4650 p->max_index = constructor_max_index;
4651 p->unfilled_index = constructor_unfilled_index;
4652 p->unfilled_fields = constructor_unfilled_fields;
4653 p->bit_index = constructor_bit_index;
4654 p->elements = constructor_elements;
4655 p->constant = constructor_constant;
4656 p->simple = constructor_simple;
4657 p->erroneous = constructor_erroneous;
4658 p->pending_elts = constructor_pending_elts;
4659 p->depth = constructor_depth;
4660 p->replacement_value.value = 0;
4661 p->replacement_value.original_code = ERROR_MARK;
4662 p->implicit = 0;
4663 p->range_stack = 0;
4664 p->outer = 0;
4665 p->incremental = constructor_incremental;
4666 p->designated = constructor_designated;
4667 p->next = 0;
4668 constructor_stack = p;
4670 constructor_constant = 1;
4671 constructor_simple = 1;
4672 constructor_depth = SPELLING_DEPTH ();
4673 constructor_elements = 0;
4674 constructor_pending_elts = 0;
4675 constructor_type = type;
4676 constructor_incremental = 1;
4677 constructor_designated = 0;
4678 designator_depth = 0;
4679 designator_errorneous = 0;
4681 if (TREE_CODE (constructor_type) == RECORD_TYPE
4682 || TREE_CODE (constructor_type) == UNION_TYPE)
4684 constructor_fields = TYPE_FIELDS (constructor_type);
4685 /* Skip any nameless bit fields at the beginning. */
4686 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4687 && DECL_NAME (constructor_fields) == 0)
4688 constructor_fields = TREE_CHAIN (constructor_fields);
4690 constructor_unfilled_fields = constructor_fields;
4691 constructor_bit_index = bitsize_zero_node;
4693 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4695 if (TYPE_DOMAIN (constructor_type))
4697 constructor_max_index
4698 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4700 /* Detect non-empty initializations of zero-length arrays. */
4701 if (constructor_max_index == NULL_TREE
4702 && TYPE_SIZE (constructor_type))
4703 constructor_max_index = build_int_cst (NULL_TREE, -1);
4705 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4706 to initialize VLAs will cause a proper error; avoid tree
4707 checking errors as well by setting a safe value. */
4708 if (constructor_max_index
4709 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4710 constructor_max_index = build_int_cst (NULL_TREE, -1);
4712 constructor_index
4713 = convert (bitsizetype,
4714 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4716 else
4718 constructor_index = bitsize_zero_node;
4719 constructor_max_index = NULL_TREE;
4722 constructor_unfilled_index = constructor_index;
4724 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4726 /* Vectors are like simple fixed-size arrays. */
4727 constructor_max_index =
4728 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4729 constructor_index = convert (bitsizetype, bitsize_zero_node);
4730 constructor_unfilled_index = constructor_index;
4732 else
4734 /* Handle the case of int x = {5}; */
4735 constructor_fields = constructor_type;
4736 constructor_unfilled_fields = constructor_type;
4740 /* Push down into a subobject, for initialization.
4741 If this is for an explicit set of braces, IMPLICIT is 0.
4742 If it is because the next element belongs at a lower level,
4743 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4745 void
4746 push_init_level (int implicit)
4748 struct constructor_stack *p;
4749 tree value = NULL_TREE;
4751 /* If we've exhausted any levels that didn't have braces,
4752 pop them now. */
4753 while (constructor_stack->implicit)
4755 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4756 || TREE_CODE (constructor_type) == UNION_TYPE)
4757 && constructor_fields == 0)
4758 process_init_element (pop_init_level (1));
4759 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4760 && constructor_max_index
4761 && tree_int_cst_lt (constructor_max_index, constructor_index))
4762 process_init_element (pop_init_level (1));
4763 else
4764 break;
4767 /* Unless this is an explicit brace, we need to preserve previous
4768 content if any. */
4769 if (implicit)
4771 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4772 || TREE_CODE (constructor_type) == UNION_TYPE)
4773 && constructor_fields)
4774 value = find_init_member (constructor_fields);
4775 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4776 value = find_init_member (constructor_index);
4779 p = XNEW (struct constructor_stack);
4780 p->type = constructor_type;
4781 p->fields = constructor_fields;
4782 p->index = constructor_index;
4783 p->max_index = constructor_max_index;
4784 p->unfilled_index = constructor_unfilled_index;
4785 p->unfilled_fields = constructor_unfilled_fields;
4786 p->bit_index = constructor_bit_index;
4787 p->elements = constructor_elements;
4788 p->constant = constructor_constant;
4789 p->simple = constructor_simple;
4790 p->erroneous = constructor_erroneous;
4791 p->pending_elts = constructor_pending_elts;
4792 p->depth = constructor_depth;
4793 p->replacement_value.value = 0;
4794 p->replacement_value.original_code = ERROR_MARK;
4795 p->implicit = implicit;
4796 p->outer = 0;
4797 p->incremental = constructor_incremental;
4798 p->designated = constructor_designated;
4799 p->next = constructor_stack;
4800 p->range_stack = 0;
4801 constructor_stack = p;
4803 constructor_constant = 1;
4804 constructor_simple = 1;
4805 constructor_depth = SPELLING_DEPTH ();
4806 constructor_elements = 0;
4807 constructor_incremental = 1;
4808 constructor_designated = 0;
4809 constructor_pending_elts = 0;
4810 if (!implicit)
4812 p->range_stack = constructor_range_stack;
4813 constructor_range_stack = 0;
4814 designator_depth = 0;
4815 designator_errorneous = 0;
4818 /* Don't die if an entire brace-pair level is superfluous
4819 in the containing level. */
4820 if (constructor_type == 0)
4822 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4823 || TREE_CODE (constructor_type) == UNION_TYPE)
4825 /* Don't die if there are extra init elts at the end. */
4826 if (constructor_fields == 0)
4827 constructor_type = 0;
4828 else
4830 constructor_type = TREE_TYPE (constructor_fields);
4831 push_member_name (constructor_fields);
4832 constructor_depth++;
4835 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4837 constructor_type = TREE_TYPE (constructor_type);
4838 push_array_bounds (tree_low_cst (constructor_index, 0));
4839 constructor_depth++;
4842 if (constructor_type == 0)
4844 error_init ("extra brace group at end of initializer");
4845 constructor_fields = 0;
4846 constructor_unfilled_fields = 0;
4847 return;
4850 if (value && TREE_CODE (value) == CONSTRUCTOR)
4852 constructor_constant = TREE_CONSTANT (value);
4853 constructor_simple = TREE_STATIC (value);
4854 constructor_elements = CONSTRUCTOR_ELTS (value);
4855 if (constructor_elements
4856 && (TREE_CODE (constructor_type) == RECORD_TYPE
4857 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4858 set_nonincremental_init ();
4861 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4863 missing_braces_mentioned = 1;
4864 warning_init ("missing braces around initializer");
4867 if (TREE_CODE (constructor_type) == RECORD_TYPE
4868 || TREE_CODE (constructor_type) == UNION_TYPE)
4870 constructor_fields = TYPE_FIELDS (constructor_type);
4871 /* Skip any nameless bit fields at the beginning. */
4872 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4873 && DECL_NAME (constructor_fields) == 0)
4874 constructor_fields = TREE_CHAIN (constructor_fields);
4876 constructor_unfilled_fields = constructor_fields;
4877 constructor_bit_index = bitsize_zero_node;
4879 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4881 /* Vectors are like simple fixed-size arrays. */
4882 constructor_max_index =
4883 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4884 constructor_index = convert (bitsizetype, integer_zero_node);
4885 constructor_unfilled_index = constructor_index;
4887 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4889 if (TYPE_DOMAIN (constructor_type))
4891 constructor_max_index
4892 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4894 /* Detect non-empty initializations of zero-length arrays. */
4895 if (constructor_max_index == NULL_TREE
4896 && TYPE_SIZE (constructor_type))
4897 constructor_max_index = build_int_cst (NULL_TREE, -1);
4899 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4900 to initialize VLAs will cause a proper error; avoid tree
4901 checking errors as well by setting a safe value. */
4902 if (constructor_max_index
4903 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4904 constructor_max_index = build_int_cst (NULL_TREE, -1);
4906 constructor_index
4907 = convert (bitsizetype,
4908 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4910 else
4911 constructor_index = bitsize_zero_node;
4913 constructor_unfilled_index = constructor_index;
4914 if (value && TREE_CODE (value) == STRING_CST)
4916 /* We need to split the char/wchar array into individual
4917 characters, so that we don't have to special case it
4918 everywhere. */
4919 set_nonincremental_init_from_string (value);
4922 else
4924 if (constructor_type != error_mark_node)
4925 warning_init ("braces around scalar initializer");
4926 constructor_fields = constructor_type;
4927 constructor_unfilled_fields = constructor_type;
4931 /* At the end of an implicit or explicit brace level,
4932 finish up that level of constructor. If a single expression
4933 with redundant braces initialized that level, return the
4934 c_expr structure for that expression. Otherwise, the original_code
4935 element is set to ERROR_MARK.
4936 If we were outputting the elements as they are read, return 0 as the value
4937 from inner levels (process_init_element ignores that),
4938 but return error_mark_node as the value from the outermost level
4939 (that's what we want to put in DECL_INITIAL).
4940 Otherwise, return a CONSTRUCTOR expression as the value. */
4942 struct c_expr
4943 pop_init_level (int implicit)
4945 struct constructor_stack *p;
4946 struct c_expr ret;
4947 ret.value = 0;
4948 ret.original_code = ERROR_MARK;
4950 if (implicit == 0)
4952 /* When we come to an explicit close brace,
4953 pop any inner levels that didn't have explicit braces. */
4954 while (constructor_stack->implicit)
4955 process_init_element (pop_init_level (1));
4957 gcc_assert (!constructor_range_stack);
4960 /* Now output all pending elements. */
4961 constructor_incremental = 1;
4962 output_pending_init_elements (1);
4964 p = constructor_stack;
4966 /* Error for initializing a flexible array member, or a zero-length
4967 array member in an inappropriate context. */
4968 if (constructor_type && constructor_fields
4969 && TREE_CODE (constructor_type) == ARRAY_TYPE
4970 && TYPE_DOMAIN (constructor_type)
4971 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4973 /* Silently discard empty initializations. The parser will
4974 already have pedwarned for empty brackets. */
4975 if (integer_zerop (constructor_unfilled_index))
4976 constructor_type = NULL_TREE;
4977 else
4979 gcc_assert (!TYPE_SIZE (constructor_type));
4981 if (constructor_depth > 2)
4982 error_init ("initialization of flexible array member in a nested context");
4983 else if (pedantic)
4984 pedwarn_init ("initialization of a flexible array member");
4986 /* We have already issued an error message for the existence
4987 of a flexible array member not at the end of the structure.
4988 Discard the initializer so that we do not abort later. */
4989 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4990 constructor_type = NULL_TREE;
4994 /* Warn when some struct elements are implicitly initialized to zero. */
4995 if (warn_missing_field_initializers
4996 && constructor_type
4997 && TREE_CODE (constructor_type) == RECORD_TYPE
4998 && constructor_unfilled_fields)
5000 /* Do not warn for flexible array members or zero-length arrays. */
5001 while (constructor_unfilled_fields
5002 && (!DECL_SIZE (constructor_unfilled_fields)
5003 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5004 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5006 /* Do not warn if this level of the initializer uses member
5007 designators; it is likely to be deliberate. */
5008 if (constructor_unfilled_fields && !constructor_designated)
5010 push_member_name (constructor_unfilled_fields);
5011 warning_init ("missing initializer");
5012 RESTORE_SPELLING_DEPTH (constructor_depth);
5016 /* Pad out the end of the structure. */
5017 if (p->replacement_value.value)
5018 /* If this closes a superfluous brace pair,
5019 just pass out the element between them. */
5020 ret = p->replacement_value;
5021 else if (constructor_type == 0)
5023 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5024 && TREE_CODE (constructor_type) != UNION_TYPE
5025 && TREE_CODE (constructor_type) != ARRAY_TYPE
5026 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5028 /* A nonincremental scalar initializer--just return
5029 the element, after verifying there is just one. */
5030 if (constructor_elements == 0)
5032 if (!constructor_erroneous)
5033 error_init ("empty scalar initializer");
5034 ret.value = error_mark_node;
5036 else if (TREE_CHAIN (constructor_elements) != 0)
5038 error_init ("extra elements in scalar initializer");
5039 ret.value = TREE_VALUE (constructor_elements);
5041 else
5042 ret.value = TREE_VALUE (constructor_elements);
5044 else
5046 if (constructor_erroneous)
5047 ret.value = error_mark_node;
5048 else
5050 ret.value = build_constructor (constructor_type,
5051 nreverse (constructor_elements));
5052 if (constructor_constant)
5053 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5054 if (constructor_constant && constructor_simple)
5055 TREE_STATIC (ret.value) = 1;
5059 constructor_type = p->type;
5060 constructor_fields = p->fields;
5061 constructor_index = p->index;
5062 constructor_max_index = p->max_index;
5063 constructor_unfilled_index = p->unfilled_index;
5064 constructor_unfilled_fields = p->unfilled_fields;
5065 constructor_bit_index = p->bit_index;
5066 constructor_elements = p->elements;
5067 constructor_constant = p->constant;
5068 constructor_simple = p->simple;
5069 constructor_erroneous = p->erroneous;
5070 constructor_incremental = p->incremental;
5071 constructor_designated = p->designated;
5072 constructor_pending_elts = p->pending_elts;
5073 constructor_depth = p->depth;
5074 if (!p->implicit)
5075 constructor_range_stack = p->range_stack;
5076 RESTORE_SPELLING_DEPTH (constructor_depth);
5078 constructor_stack = p->next;
5079 free (p);
5081 if (ret.value == 0)
5083 if (constructor_stack == 0)
5085 ret.value = error_mark_node;
5086 return ret;
5088 return ret;
5090 return ret;
5093 /* Common handling for both array range and field name designators.
5094 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5096 static int
5097 set_designator (int array)
5099 tree subtype;
5100 enum tree_code subcode;
5102 /* Don't die if an entire brace-pair level is superfluous
5103 in the containing level. */
5104 if (constructor_type == 0)
5105 return 1;
5107 /* If there were errors in this designator list already, bail out
5108 silently. */
5109 if (designator_errorneous)
5110 return 1;
5112 if (!designator_depth)
5114 gcc_assert (!constructor_range_stack);
5116 /* Designator list starts at the level of closest explicit
5117 braces. */
5118 while (constructor_stack->implicit)
5119 process_init_element (pop_init_level (1));
5120 constructor_designated = 1;
5121 return 0;
5124 switch (TREE_CODE (constructor_type))
5126 case RECORD_TYPE:
5127 case UNION_TYPE:
5128 subtype = TREE_TYPE (constructor_fields);
5129 if (subtype != error_mark_node)
5130 subtype = TYPE_MAIN_VARIANT (subtype);
5131 break;
5132 case ARRAY_TYPE:
5133 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5134 break;
5135 default:
5136 gcc_unreachable ();
5139 subcode = TREE_CODE (subtype);
5140 if (array && subcode != ARRAY_TYPE)
5142 error_init ("array index in non-array initializer");
5143 return 1;
5145 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5147 error_init ("field name not in record or union initializer");
5148 return 1;
5151 constructor_designated = 1;
5152 push_init_level (2);
5153 return 0;
5156 /* If there are range designators in designator list, push a new designator
5157 to constructor_range_stack. RANGE_END is end of such stack range or
5158 NULL_TREE if there is no range designator at this level. */
5160 static void
5161 push_range_stack (tree range_end)
5163 struct constructor_range_stack *p;
5165 p = GGC_NEW (struct constructor_range_stack);
5166 p->prev = constructor_range_stack;
5167 p->next = 0;
5168 p->fields = constructor_fields;
5169 p->range_start = constructor_index;
5170 p->index = constructor_index;
5171 p->stack = constructor_stack;
5172 p->range_end = range_end;
5173 if (constructor_range_stack)
5174 constructor_range_stack->next = p;
5175 constructor_range_stack = p;
5178 /* Within an array initializer, specify the next index to be initialized.
5179 FIRST is that index. If LAST is nonzero, then initialize a range
5180 of indices, running from FIRST through LAST. */
5182 void
5183 set_init_index (tree first, tree last)
5185 if (set_designator (1))
5186 return;
5188 designator_errorneous = 1;
5190 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5191 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5193 error_init ("array index in initializer not of integer type");
5194 return;
5197 if (TREE_CODE (first) != INTEGER_CST)
5198 error_init ("nonconstant array index in initializer");
5199 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5200 error_init ("nonconstant array index in initializer");
5201 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5202 error_init ("array index in non-array initializer");
5203 else if (tree_int_cst_sgn (first) == -1)
5204 error_init ("array index in initializer exceeds array bounds");
5205 else if (constructor_max_index
5206 && tree_int_cst_lt (constructor_max_index, first))
5207 error_init ("array index in initializer exceeds array bounds");
5208 else
5210 constructor_index = convert (bitsizetype, first);
5212 if (last)
5214 if (tree_int_cst_equal (first, last))
5215 last = 0;
5216 else if (tree_int_cst_lt (last, first))
5218 error_init ("empty index range in initializer");
5219 last = 0;
5221 else
5223 last = convert (bitsizetype, last);
5224 if (constructor_max_index != 0
5225 && tree_int_cst_lt (constructor_max_index, last))
5227 error_init ("array index range in initializer exceeds array bounds");
5228 last = 0;
5233 designator_depth++;
5234 designator_errorneous = 0;
5235 if (constructor_range_stack || last)
5236 push_range_stack (last);
5240 /* Within a struct initializer, specify the next field to be initialized. */
5242 void
5243 set_init_label (tree fieldname)
5245 tree tail;
5247 if (set_designator (0))
5248 return;
5250 designator_errorneous = 1;
5252 if (TREE_CODE (constructor_type) != RECORD_TYPE
5253 && TREE_CODE (constructor_type) != UNION_TYPE)
5255 error_init ("field name not in record or union initializer");
5256 return;
5259 for (tail = TYPE_FIELDS (constructor_type); tail;
5260 tail = TREE_CHAIN (tail))
5262 if (DECL_NAME (tail) == fieldname)
5263 break;
5266 if (tail == 0)
5267 error ("unknown field %qE specified in initializer", fieldname);
5268 else
5270 constructor_fields = tail;
5271 designator_depth++;
5272 designator_errorneous = 0;
5273 if (constructor_range_stack)
5274 push_range_stack (NULL_TREE);
5278 /* Add a new initializer to the tree of pending initializers. PURPOSE
5279 identifies the initializer, either array index or field in a structure.
5280 VALUE is the value of that index or field. */
5282 static void
5283 add_pending_init (tree purpose, tree value)
5285 struct init_node *p, **q, *r;
5287 q = &constructor_pending_elts;
5288 p = 0;
5290 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5292 while (*q != 0)
5294 p = *q;
5295 if (tree_int_cst_lt (purpose, p->purpose))
5296 q = &p->left;
5297 else if (tree_int_cst_lt (p->purpose, purpose))
5298 q = &p->right;
5299 else
5301 if (TREE_SIDE_EFFECTS (p->value))
5302 warning_init ("initialized field with side-effects overwritten");
5303 p->value = value;
5304 return;
5308 else
5310 tree bitpos;
5312 bitpos = bit_position (purpose);
5313 while (*q != NULL)
5315 p = *q;
5316 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5317 q = &p->left;
5318 else if (p->purpose != purpose)
5319 q = &p->right;
5320 else
5322 if (TREE_SIDE_EFFECTS (p->value))
5323 warning_init ("initialized field with side-effects overwritten");
5324 p->value = value;
5325 return;
5330 r = GGC_NEW (struct init_node);
5331 r->purpose = purpose;
5332 r->value = value;
5334 *q = r;
5335 r->parent = p;
5336 r->left = 0;
5337 r->right = 0;
5338 r->balance = 0;
5340 while (p)
5342 struct init_node *s;
5344 if (r == p->left)
5346 if (p->balance == 0)
5347 p->balance = -1;
5348 else if (p->balance < 0)
5350 if (r->balance < 0)
5352 /* L rotation. */
5353 p->left = r->right;
5354 if (p->left)
5355 p->left->parent = p;
5356 r->right = p;
5358 p->balance = 0;
5359 r->balance = 0;
5361 s = p->parent;
5362 p->parent = r;
5363 r->parent = s;
5364 if (s)
5366 if (s->left == p)
5367 s->left = r;
5368 else
5369 s->right = r;
5371 else
5372 constructor_pending_elts = r;
5374 else
5376 /* LR rotation. */
5377 struct init_node *t = r->right;
5379 r->right = t->left;
5380 if (r->right)
5381 r->right->parent = r;
5382 t->left = r;
5384 p->left = t->right;
5385 if (p->left)
5386 p->left->parent = p;
5387 t->right = p;
5389 p->balance = t->balance < 0;
5390 r->balance = -(t->balance > 0);
5391 t->balance = 0;
5393 s = p->parent;
5394 p->parent = t;
5395 r->parent = t;
5396 t->parent = s;
5397 if (s)
5399 if (s->left == p)
5400 s->left = t;
5401 else
5402 s->right = t;
5404 else
5405 constructor_pending_elts = t;
5407 break;
5409 else
5411 /* p->balance == +1; growth of left side balances the node. */
5412 p->balance = 0;
5413 break;
5416 else /* r == p->right */
5418 if (p->balance == 0)
5419 /* Growth propagation from right side. */
5420 p->balance++;
5421 else if (p->balance > 0)
5423 if (r->balance > 0)
5425 /* R rotation. */
5426 p->right = r->left;
5427 if (p->right)
5428 p->right->parent = p;
5429 r->left = p;
5431 p->balance = 0;
5432 r->balance = 0;
5434 s = p->parent;
5435 p->parent = r;
5436 r->parent = s;
5437 if (s)
5439 if (s->left == p)
5440 s->left = r;
5441 else
5442 s->right = r;
5444 else
5445 constructor_pending_elts = r;
5447 else /* r->balance == -1 */
5449 /* RL rotation */
5450 struct init_node *t = r->left;
5452 r->left = t->right;
5453 if (r->left)
5454 r->left->parent = r;
5455 t->right = r;
5457 p->right = t->left;
5458 if (p->right)
5459 p->right->parent = p;
5460 t->left = p;
5462 r->balance = (t->balance < 0);
5463 p->balance = -(t->balance > 0);
5464 t->balance = 0;
5466 s = p->parent;
5467 p->parent = t;
5468 r->parent = t;
5469 t->parent = s;
5470 if (s)
5472 if (s->left == p)
5473 s->left = t;
5474 else
5475 s->right = t;
5477 else
5478 constructor_pending_elts = t;
5480 break;
5482 else
5484 /* p->balance == -1; growth of right side balances the node. */
5485 p->balance = 0;
5486 break;
5490 r = p;
5491 p = p->parent;
5495 /* Build AVL tree from a sorted chain. */
5497 static void
5498 set_nonincremental_init (void)
5500 tree chain;
5502 if (TREE_CODE (constructor_type) != RECORD_TYPE
5503 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5504 return;
5506 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5507 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5508 constructor_elements = 0;
5509 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5511 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5512 /* Skip any nameless bit fields at the beginning. */
5513 while (constructor_unfilled_fields != 0
5514 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5515 && DECL_NAME (constructor_unfilled_fields) == 0)
5516 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5519 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5521 if (TYPE_DOMAIN (constructor_type))
5522 constructor_unfilled_index
5523 = convert (bitsizetype,
5524 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5525 else
5526 constructor_unfilled_index = bitsize_zero_node;
5528 constructor_incremental = 0;
5531 /* Build AVL tree from a string constant. */
5533 static void
5534 set_nonincremental_init_from_string (tree str)
5536 tree value, purpose, type;
5537 HOST_WIDE_INT val[2];
5538 const char *p, *end;
5539 int byte, wchar_bytes, charwidth, bitpos;
5541 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5543 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5544 == TYPE_PRECISION (char_type_node))
5545 wchar_bytes = 1;
5546 else
5548 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5549 == TYPE_PRECISION (wchar_type_node));
5550 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5552 charwidth = TYPE_PRECISION (char_type_node);
5553 type = TREE_TYPE (constructor_type);
5554 p = TREE_STRING_POINTER (str);
5555 end = p + TREE_STRING_LENGTH (str);
5557 for (purpose = bitsize_zero_node;
5558 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5559 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5561 if (wchar_bytes == 1)
5563 val[1] = (unsigned char) *p++;
5564 val[0] = 0;
5566 else
5568 val[0] = 0;
5569 val[1] = 0;
5570 for (byte = 0; byte < wchar_bytes; byte++)
5572 if (BYTES_BIG_ENDIAN)
5573 bitpos = (wchar_bytes - byte - 1) * charwidth;
5574 else
5575 bitpos = byte * charwidth;
5576 val[bitpos < HOST_BITS_PER_WIDE_INT]
5577 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5578 << (bitpos % HOST_BITS_PER_WIDE_INT);
5582 if (!TYPE_UNSIGNED (type))
5584 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5585 if (bitpos < HOST_BITS_PER_WIDE_INT)
5587 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5589 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5590 val[0] = -1;
5593 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5595 if (val[1] < 0)
5596 val[0] = -1;
5598 else if (val[0] & (((HOST_WIDE_INT) 1)
5599 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5600 val[0] |= ((HOST_WIDE_INT) -1)
5601 << (bitpos - HOST_BITS_PER_WIDE_INT);
5604 value = build_int_cst_wide (type, val[1], val[0]);
5605 add_pending_init (purpose, value);
5608 constructor_incremental = 0;
5611 /* Return value of FIELD in pending initializer or zero if the field was
5612 not initialized yet. */
5614 static tree
5615 find_init_member (tree field)
5617 struct init_node *p;
5619 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5621 if (constructor_incremental
5622 && tree_int_cst_lt (field, constructor_unfilled_index))
5623 set_nonincremental_init ();
5625 p = constructor_pending_elts;
5626 while (p)
5628 if (tree_int_cst_lt (field, p->purpose))
5629 p = p->left;
5630 else if (tree_int_cst_lt (p->purpose, field))
5631 p = p->right;
5632 else
5633 return p->value;
5636 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5638 tree bitpos = bit_position (field);
5640 if (constructor_incremental
5641 && (!constructor_unfilled_fields
5642 || tree_int_cst_lt (bitpos,
5643 bit_position (constructor_unfilled_fields))))
5644 set_nonincremental_init ();
5646 p = constructor_pending_elts;
5647 while (p)
5649 if (field == p->purpose)
5650 return p->value;
5651 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5652 p = p->left;
5653 else
5654 p = p->right;
5657 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5659 if (constructor_elements
5660 && TREE_PURPOSE (constructor_elements) == field)
5661 return TREE_VALUE (constructor_elements);
5663 return 0;
5666 /* "Output" the next constructor element.
5667 At top level, really output it to assembler code now.
5668 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5669 TYPE is the data type that the containing data type wants here.
5670 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5671 If VALUE is a string constant, STRICT_STRING is true if it is
5672 unparenthesized or we should not warn here for it being parenthesized.
5673 For other types of VALUE, STRICT_STRING is not used.
5675 PENDING if non-nil means output pending elements that belong
5676 right after this element. (PENDING is normally 1;
5677 it is 0 while outputting pending elements, to avoid recursion.) */
5679 static void
5680 output_init_element (tree value, bool strict_string, tree type, tree field,
5681 int pending)
5683 if (type == error_mark_node || value == error_mark_node)
5685 constructor_erroneous = 1;
5686 return;
5688 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5689 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5690 && !(TREE_CODE (value) == STRING_CST
5691 && TREE_CODE (type) == ARRAY_TYPE
5692 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5693 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5694 TYPE_MAIN_VARIANT (type))))
5695 value = default_conversion (value);
5697 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5698 && require_constant_value && !flag_isoc99 && pending)
5700 /* As an extension, allow initializing objects with static storage
5701 duration with compound literals (which are then treated just as
5702 the brace enclosed list they contain). */
5703 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5704 value = DECL_INITIAL (decl);
5707 if (value == error_mark_node)
5708 constructor_erroneous = 1;
5709 else if (!TREE_CONSTANT (value))
5710 constructor_constant = 0;
5711 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5712 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5713 || TREE_CODE (constructor_type) == UNION_TYPE)
5714 && DECL_C_BIT_FIELD (field)
5715 && TREE_CODE (value) != INTEGER_CST))
5716 constructor_simple = 0;
5718 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5720 if (require_constant_value)
5722 error_init ("initializer element is not constant");
5723 value = error_mark_node;
5725 else if (require_constant_elements)
5726 pedwarn ("initializer element is not computable at load time");
5729 /* If this field is empty (and not at the end of structure),
5730 don't do anything other than checking the initializer. */
5731 if (field
5732 && (TREE_TYPE (field) == error_mark_node
5733 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5734 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5735 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5736 || TREE_CHAIN (field)))))
5737 return;
5739 value = digest_init (type, value, strict_string, require_constant_value);
5740 if (value == error_mark_node)
5742 constructor_erroneous = 1;
5743 return;
5746 /* If this element doesn't come next in sequence,
5747 put it on constructor_pending_elts. */
5748 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5749 && (!constructor_incremental
5750 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5752 if (constructor_incremental
5753 && tree_int_cst_lt (field, constructor_unfilled_index))
5754 set_nonincremental_init ();
5756 add_pending_init (field, value);
5757 return;
5759 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5760 && (!constructor_incremental
5761 || field != constructor_unfilled_fields))
5763 /* We do this for records but not for unions. In a union,
5764 no matter which field is specified, it can be initialized
5765 right away since it starts at the beginning of the union. */
5766 if (constructor_incremental)
5768 if (!constructor_unfilled_fields)
5769 set_nonincremental_init ();
5770 else
5772 tree bitpos, unfillpos;
5774 bitpos = bit_position (field);
5775 unfillpos = bit_position (constructor_unfilled_fields);
5777 if (tree_int_cst_lt (bitpos, unfillpos))
5778 set_nonincremental_init ();
5782 add_pending_init (field, value);
5783 return;
5785 else if (TREE_CODE (constructor_type) == UNION_TYPE
5786 && constructor_elements)
5788 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5789 warning_init ("initialized field with side-effects overwritten");
5791 /* We can have just one union field set. */
5792 constructor_elements = 0;
5795 /* Otherwise, output this element either to
5796 constructor_elements or to the assembler file. */
5798 if (field && TREE_CODE (field) == INTEGER_CST)
5799 field = copy_node (field);
5800 constructor_elements
5801 = tree_cons (field, value, constructor_elements);
5803 /* Advance the variable that indicates sequential elements output. */
5804 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5805 constructor_unfilled_index
5806 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5807 bitsize_one_node);
5808 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5810 constructor_unfilled_fields
5811 = TREE_CHAIN (constructor_unfilled_fields);
5813 /* Skip any nameless bit fields. */
5814 while (constructor_unfilled_fields != 0
5815 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5816 && DECL_NAME (constructor_unfilled_fields) == 0)
5817 constructor_unfilled_fields =
5818 TREE_CHAIN (constructor_unfilled_fields);
5820 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5821 constructor_unfilled_fields = 0;
5823 /* Now output any pending elements which have become next. */
5824 if (pending)
5825 output_pending_init_elements (0);
5828 /* Output any pending elements which have become next.
5829 As we output elements, constructor_unfilled_{fields,index}
5830 advances, which may cause other elements to become next;
5831 if so, they too are output.
5833 If ALL is 0, we return when there are
5834 no more pending elements to output now.
5836 If ALL is 1, we output space as necessary so that
5837 we can output all the pending elements. */
5839 static void
5840 output_pending_init_elements (int all)
5842 struct init_node *elt = constructor_pending_elts;
5843 tree next;
5845 retry:
5847 /* Look through the whole pending tree.
5848 If we find an element that should be output now,
5849 output it. Otherwise, set NEXT to the element
5850 that comes first among those still pending. */
5852 next = 0;
5853 while (elt)
5855 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5857 if (tree_int_cst_equal (elt->purpose,
5858 constructor_unfilled_index))
5859 output_init_element (elt->value, true,
5860 TREE_TYPE (constructor_type),
5861 constructor_unfilled_index, 0);
5862 else if (tree_int_cst_lt (constructor_unfilled_index,
5863 elt->purpose))
5865 /* Advance to the next smaller node. */
5866 if (elt->left)
5867 elt = elt->left;
5868 else
5870 /* We have reached the smallest node bigger than the
5871 current unfilled index. Fill the space first. */
5872 next = elt->purpose;
5873 break;
5876 else
5878 /* Advance to the next bigger node. */
5879 if (elt->right)
5880 elt = elt->right;
5881 else
5883 /* We have reached the biggest node in a subtree. Find
5884 the parent of it, which is the next bigger node. */
5885 while (elt->parent && elt->parent->right == elt)
5886 elt = elt->parent;
5887 elt = elt->parent;
5888 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5889 elt->purpose))
5891 next = elt->purpose;
5892 break;
5897 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5898 || TREE_CODE (constructor_type) == UNION_TYPE)
5900 tree ctor_unfilled_bitpos, elt_bitpos;
5902 /* If the current record is complete we are done. */
5903 if (constructor_unfilled_fields == 0)
5904 break;
5906 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5907 elt_bitpos = bit_position (elt->purpose);
5908 /* We can't compare fields here because there might be empty
5909 fields in between. */
5910 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5912 constructor_unfilled_fields = elt->purpose;
5913 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5914 elt->purpose, 0);
5916 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5918 /* Advance to the next smaller node. */
5919 if (elt->left)
5920 elt = elt->left;
5921 else
5923 /* We have reached the smallest node bigger than the
5924 current unfilled field. Fill the space first. */
5925 next = elt->purpose;
5926 break;
5929 else
5931 /* Advance to the next bigger node. */
5932 if (elt->right)
5933 elt = elt->right;
5934 else
5936 /* We have reached the biggest node in a subtree. Find
5937 the parent of it, which is the next bigger node. */
5938 while (elt->parent && elt->parent->right == elt)
5939 elt = elt->parent;
5940 elt = elt->parent;
5941 if (elt
5942 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5943 bit_position (elt->purpose))))
5945 next = elt->purpose;
5946 break;
5953 /* Ordinarily return, but not if we want to output all
5954 and there are elements left. */
5955 if (!(all && next != 0))
5956 return;
5958 /* If it's not incremental, just skip over the gap, so that after
5959 jumping to retry we will output the next successive element. */
5960 if (TREE_CODE (constructor_type) == RECORD_TYPE
5961 || TREE_CODE (constructor_type) == UNION_TYPE)
5962 constructor_unfilled_fields = next;
5963 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5964 constructor_unfilled_index = next;
5966 /* ELT now points to the node in the pending tree with the next
5967 initializer to output. */
5968 goto retry;
5971 /* Add one non-braced element to the current constructor level.
5972 This adjusts the current position within the constructor's type.
5973 This may also start or terminate implicit levels
5974 to handle a partly-braced initializer.
5976 Once this has found the correct level for the new element,
5977 it calls output_init_element. */
5979 void
5980 process_init_element (struct c_expr value)
5982 tree orig_value = value.value;
5983 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5984 bool strict_string = value.original_code == STRING_CST;
5986 designator_depth = 0;
5987 designator_errorneous = 0;
5989 /* Handle superfluous braces around string cst as in
5990 char x[] = {"foo"}; */
5991 if (string_flag
5992 && constructor_type
5993 && TREE_CODE (constructor_type) == ARRAY_TYPE
5994 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5995 && integer_zerop (constructor_unfilled_index))
5997 if (constructor_stack->replacement_value.value)
5998 error_init ("excess elements in char array initializer");
5999 constructor_stack->replacement_value = value;
6000 return;
6003 if (constructor_stack->replacement_value.value != 0)
6005 error_init ("excess elements in struct initializer");
6006 return;
6009 /* Ignore elements of a brace group if it is entirely superfluous
6010 and has already been diagnosed. */
6011 if (constructor_type == 0)
6012 return;
6014 /* If we've exhausted any levels that didn't have braces,
6015 pop them now. */
6016 while (constructor_stack->implicit)
6018 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6019 || TREE_CODE (constructor_type) == UNION_TYPE)
6020 && constructor_fields == 0)
6021 process_init_element (pop_init_level (1));
6022 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6023 && (constructor_max_index == 0
6024 || tree_int_cst_lt (constructor_max_index,
6025 constructor_index)))
6026 process_init_element (pop_init_level (1));
6027 else
6028 break;
6031 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6032 if (constructor_range_stack)
6034 /* If value is a compound literal and we'll be just using its
6035 content, don't put it into a SAVE_EXPR. */
6036 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6037 || !require_constant_value
6038 || flag_isoc99)
6039 value.value = save_expr (value.value);
6042 while (1)
6044 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6046 tree fieldtype;
6047 enum tree_code fieldcode;
6049 if (constructor_fields == 0)
6051 pedwarn_init ("excess elements in struct initializer");
6052 break;
6055 fieldtype = TREE_TYPE (constructor_fields);
6056 if (fieldtype != error_mark_node)
6057 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6058 fieldcode = TREE_CODE (fieldtype);
6060 /* Error for non-static initialization of a flexible array member. */
6061 if (fieldcode == ARRAY_TYPE
6062 && !require_constant_value
6063 && TYPE_SIZE (fieldtype) == NULL_TREE
6064 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6066 error_init ("non-static initialization of a flexible array member");
6067 break;
6070 /* Accept a string constant to initialize a subarray. */
6071 if (value.value != 0
6072 && fieldcode == ARRAY_TYPE
6073 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6074 && string_flag)
6075 value.value = orig_value;
6076 /* Otherwise, if we have come to a subaggregate,
6077 and we don't have an element of its type, push into it. */
6078 else if (value.value != 0
6079 && value.value != error_mark_node
6080 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6081 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6082 || fieldcode == UNION_TYPE))
6084 push_init_level (1);
6085 continue;
6088 if (value.value)
6090 push_member_name (constructor_fields);
6091 output_init_element (value.value, strict_string,
6092 fieldtype, constructor_fields, 1);
6093 RESTORE_SPELLING_DEPTH (constructor_depth);
6095 else
6096 /* Do the bookkeeping for an element that was
6097 directly output as a constructor. */
6099 /* For a record, keep track of end position of last field. */
6100 if (DECL_SIZE (constructor_fields))
6101 constructor_bit_index
6102 = size_binop (PLUS_EXPR,
6103 bit_position (constructor_fields),
6104 DECL_SIZE (constructor_fields));
6106 /* If the current field was the first one not yet written out,
6107 it isn't now, so update. */
6108 if (constructor_unfilled_fields == constructor_fields)
6110 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6111 /* Skip any nameless bit fields. */
6112 while (constructor_unfilled_fields != 0
6113 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6114 && DECL_NAME (constructor_unfilled_fields) == 0)
6115 constructor_unfilled_fields =
6116 TREE_CHAIN (constructor_unfilled_fields);
6120 constructor_fields = TREE_CHAIN (constructor_fields);
6121 /* Skip any nameless bit fields at the beginning. */
6122 while (constructor_fields != 0
6123 && DECL_C_BIT_FIELD (constructor_fields)
6124 && DECL_NAME (constructor_fields) == 0)
6125 constructor_fields = TREE_CHAIN (constructor_fields);
6127 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6129 tree fieldtype;
6130 enum tree_code fieldcode;
6132 if (constructor_fields == 0)
6134 pedwarn_init ("excess elements in union initializer");
6135 break;
6138 fieldtype = TREE_TYPE (constructor_fields);
6139 if (fieldtype != error_mark_node)
6140 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6141 fieldcode = TREE_CODE (fieldtype);
6143 /* Warn that traditional C rejects initialization of unions.
6144 We skip the warning if the value is zero. This is done
6145 under the assumption that the zero initializer in user
6146 code appears conditioned on e.g. __STDC__ to avoid
6147 "missing initializer" warnings and relies on default
6148 initialization to zero in the traditional C case.
6149 We also skip the warning if the initializer is designated,
6150 again on the assumption that this must be conditional on
6151 __STDC__ anyway (and we've already complained about the
6152 member-designator already). */
6153 if (warn_traditional && !in_system_header && !constructor_designated
6154 && !(value.value && (integer_zerop (value.value)
6155 || real_zerop (value.value))))
6156 warning ("traditional C rejects initialization of unions");
6158 /* Accept a string constant to initialize a subarray. */
6159 if (value.value != 0
6160 && fieldcode == ARRAY_TYPE
6161 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6162 && string_flag)
6163 value.value = orig_value;
6164 /* Otherwise, if we have come to a subaggregate,
6165 and we don't have an element of its type, push into it. */
6166 else if (value.value != 0
6167 && value.value != error_mark_node
6168 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6169 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6170 || fieldcode == UNION_TYPE))
6172 push_init_level (1);
6173 continue;
6176 if (value.value)
6178 push_member_name (constructor_fields);
6179 output_init_element (value.value, strict_string,
6180 fieldtype, constructor_fields, 1);
6181 RESTORE_SPELLING_DEPTH (constructor_depth);
6183 else
6184 /* Do the bookkeeping for an element that was
6185 directly output as a constructor. */
6187 constructor_bit_index = DECL_SIZE (constructor_fields);
6188 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6191 constructor_fields = 0;
6193 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6195 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6196 enum tree_code eltcode = TREE_CODE (elttype);
6198 /* Accept a string constant to initialize a subarray. */
6199 if (value.value != 0
6200 && eltcode == ARRAY_TYPE
6201 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6202 && string_flag)
6203 value.value = orig_value;
6204 /* Otherwise, if we have come to a subaggregate,
6205 and we don't have an element of its type, push into it. */
6206 else if (value.value != 0
6207 && value.value != error_mark_node
6208 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6209 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6210 || eltcode == UNION_TYPE))
6212 push_init_level (1);
6213 continue;
6216 if (constructor_max_index != 0
6217 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6218 || integer_all_onesp (constructor_max_index)))
6220 pedwarn_init ("excess elements in array initializer");
6221 break;
6224 /* Now output the actual element. */
6225 if (value.value)
6227 push_array_bounds (tree_low_cst (constructor_index, 0));
6228 output_init_element (value.value, strict_string,
6229 elttype, constructor_index, 1);
6230 RESTORE_SPELLING_DEPTH (constructor_depth);
6233 constructor_index
6234 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6236 if (!value.value)
6237 /* If we are doing the bookkeeping for an element that was
6238 directly output as a constructor, we must update
6239 constructor_unfilled_index. */
6240 constructor_unfilled_index = constructor_index;
6242 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6244 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6246 /* Do a basic check of initializer size. Note that vectors
6247 always have a fixed size derived from their type. */
6248 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6250 pedwarn_init ("excess elements in vector initializer");
6251 break;
6254 /* Now output the actual element. */
6255 if (value.value)
6256 output_init_element (value.value, strict_string,
6257 elttype, constructor_index, 1);
6259 constructor_index
6260 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6262 if (!value.value)
6263 /* If we are doing the bookkeeping for an element that was
6264 directly output as a constructor, we must update
6265 constructor_unfilled_index. */
6266 constructor_unfilled_index = constructor_index;
6269 /* Handle the sole element allowed in a braced initializer
6270 for a scalar variable. */
6271 else if (constructor_type != error_mark_node
6272 && constructor_fields == 0)
6274 pedwarn_init ("excess elements in scalar initializer");
6275 break;
6277 else
6279 if (value.value)
6280 output_init_element (value.value, strict_string,
6281 constructor_type, NULL_TREE, 1);
6282 constructor_fields = 0;
6285 /* Handle range initializers either at this level or anywhere higher
6286 in the designator stack. */
6287 if (constructor_range_stack)
6289 struct constructor_range_stack *p, *range_stack;
6290 int finish = 0;
6292 range_stack = constructor_range_stack;
6293 constructor_range_stack = 0;
6294 while (constructor_stack != range_stack->stack)
6296 gcc_assert (constructor_stack->implicit);
6297 process_init_element (pop_init_level (1));
6299 for (p = range_stack;
6300 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6301 p = p->prev)
6303 gcc_assert (constructor_stack->implicit);
6304 process_init_element (pop_init_level (1));
6307 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6308 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6309 finish = 1;
6311 while (1)
6313 constructor_index = p->index;
6314 constructor_fields = p->fields;
6315 if (finish && p->range_end && p->index == p->range_start)
6317 finish = 0;
6318 p->prev = 0;
6320 p = p->next;
6321 if (!p)
6322 break;
6323 push_init_level (2);
6324 p->stack = constructor_stack;
6325 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6326 p->index = p->range_start;
6329 if (!finish)
6330 constructor_range_stack = range_stack;
6331 continue;
6334 break;
6337 constructor_range_stack = 0;
6340 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6341 (guaranteed to be 'volatile' or null) and ARGS (represented using
6342 an ASM_EXPR node). */
6343 tree
6344 build_asm_stmt (tree cv_qualifier, tree args)
6346 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6347 ASM_VOLATILE_P (args) = 1;
6348 return add_stmt (args);
6351 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6352 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6353 SIMPLE indicates whether there was anything at all after the
6354 string in the asm expression -- asm("blah") and asm("blah" : )
6355 are subtly different. We use a ASM_EXPR node to represent this. */
6356 tree
6357 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6358 bool simple)
6360 tree tail;
6361 tree args;
6362 int i;
6363 const char *constraint;
6364 const char **oconstraints;
6365 bool allows_mem, allows_reg, is_inout;
6366 int ninputs, noutputs;
6368 ninputs = list_length (inputs);
6369 noutputs = list_length (outputs);
6370 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6372 string = resolve_asm_operand_names (string, outputs, inputs);
6374 /* Remove output conversions that change the type but not the mode. */
6375 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6377 tree output = TREE_VALUE (tail);
6379 /* ??? Really, this should not be here. Users should be using a
6380 proper lvalue, dammit. But there's a long history of using casts
6381 in the output operands. In cases like longlong.h, this becomes a
6382 primitive form of typechecking -- if the cast can be removed, then
6383 the output operand had a type of the proper width; otherwise we'll
6384 get an error. Gross, but ... */
6385 STRIP_NOPS (output);
6387 if (!lvalue_or_else (output, lv_asm))
6388 output = error_mark_node;
6390 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6391 oconstraints[i] = constraint;
6393 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6394 &allows_mem, &allows_reg, &is_inout))
6396 /* If the operand is going to end up in memory,
6397 mark it addressable. */
6398 if (!allows_reg && !c_mark_addressable (output))
6399 output = error_mark_node;
6401 else
6402 output = error_mark_node;
6404 TREE_VALUE (tail) = output;
6407 /* Perform default conversions on array and function inputs.
6408 Don't do this for other types as it would screw up operands
6409 expected to be in memory. */
6410 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6412 tree input;
6414 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6415 input = TREE_VALUE (tail);
6417 input = default_function_array_conversion (input);
6419 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6420 oconstraints, &allows_mem, &allows_reg))
6422 /* If the operand is going to end up in memory,
6423 mark it addressable. */
6424 if (!allows_reg && allows_mem)
6426 /* Strip the nops as we allow this case. FIXME, this really
6427 should be rejected or made deprecated. */
6428 STRIP_NOPS (input);
6429 if (!c_mark_addressable (input))
6430 input = error_mark_node;
6433 else
6434 input = error_mark_node;
6436 TREE_VALUE (tail) = input;
6439 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6441 /* Simple asm statements are treated as volatile. */
6442 if (simple)
6444 ASM_VOLATILE_P (args) = 1;
6445 ASM_INPUT_P (args) = 1;
6448 return args;
6451 /* Generate a goto statement to LABEL. */
6453 tree
6454 c_finish_goto_label (tree label)
6456 tree decl = lookup_label (label);
6457 if (!decl)
6458 return NULL_TREE;
6460 TREE_USED (decl) = 1;
6461 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6464 /* Generate a computed goto statement to EXPR. */
6466 tree
6467 c_finish_goto_ptr (tree expr)
6469 if (pedantic)
6470 pedwarn ("ISO C forbids %<goto *expr;%>");
6471 expr = convert (ptr_type_node, expr);
6472 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6475 /* Generate a C `return' statement. RETVAL is the expression for what
6476 to return, or a null pointer for `return;' with no value. */
6478 tree
6479 c_finish_return (tree retval)
6481 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6483 if (TREE_THIS_VOLATILE (current_function_decl))
6484 warning ("function declared %<noreturn%> has a %<return%> statement");
6486 if (!retval)
6488 current_function_returns_null = 1;
6489 if ((warn_return_type || flag_isoc99)
6490 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6491 pedwarn_c99 ("%<return%> with no value, in "
6492 "function returning non-void");
6494 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6496 current_function_returns_null = 1;
6497 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6498 pedwarn ("%<return%> with a value, in function returning void");
6500 else
6502 tree t = convert_for_assignment (valtype, retval, ic_return,
6503 NULL_TREE, NULL_TREE, 0);
6504 tree res = DECL_RESULT (current_function_decl);
6505 tree inner;
6507 current_function_returns_value = 1;
6508 if (t == error_mark_node)
6509 return NULL_TREE;
6511 inner = t = convert (TREE_TYPE (res), t);
6513 /* Strip any conversions, additions, and subtractions, and see if
6514 we are returning the address of a local variable. Warn if so. */
6515 while (1)
6517 switch (TREE_CODE (inner))
6519 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6520 case PLUS_EXPR:
6521 inner = TREE_OPERAND (inner, 0);
6522 continue;
6524 case MINUS_EXPR:
6525 /* If the second operand of the MINUS_EXPR has a pointer
6526 type (or is converted from it), this may be valid, so
6527 don't give a warning. */
6529 tree op1 = TREE_OPERAND (inner, 1);
6531 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6532 && (TREE_CODE (op1) == NOP_EXPR
6533 || TREE_CODE (op1) == NON_LVALUE_EXPR
6534 || TREE_CODE (op1) == CONVERT_EXPR))
6535 op1 = TREE_OPERAND (op1, 0);
6537 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6538 break;
6540 inner = TREE_OPERAND (inner, 0);
6541 continue;
6544 case ADDR_EXPR:
6545 inner = TREE_OPERAND (inner, 0);
6547 while (REFERENCE_CLASS_P (inner)
6548 && TREE_CODE (inner) != INDIRECT_REF)
6549 inner = TREE_OPERAND (inner, 0);
6551 if (DECL_P (inner)
6552 && !DECL_EXTERNAL (inner)
6553 && !TREE_STATIC (inner)
6554 && DECL_CONTEXT (inner) == current_function_decl)
6555 warning ("function returns address of local variable");
6556 break;
6558 default:
6559 break;
6562 break;
6565 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6568 return add_stmt (build_stmt (RETURN_EXPR, retval));
6571 struct c_switch {
6572 /* The SWITCH_STMT being built. */
6573 tree switch_stmt;
6575 /* The original type of the testing expression, i.e. before the
6576 default conversion is applied. */
6577 tree orig_type;
6579 /* A splay-tree mapping the low element of a case range to the high
6580 element, or NULL_TREE if there is no high element. Used to
6581 determine whether or not a new case label duplicates an old case
6582 label. We need a tree, rather than simply a hash table, because
6583 of the GNU case range extension. */
6584 splay_tree cases;
6586 /* The next node on the stack. */
6587 struct c_switch *next;
6590 /* A stack of the currently active switch statements. The innermost
6591 switch statement is on the top of the stack. There is no need to
6592 mark the stack for garbage collection because it is only active
6593 during the processing of the body of a function, and we never
6594 collect at that point. */
6596 struct c_switch *c_switch_stack;
6598 /* Start a C switch statement, testing expression EXP. Return the new
6599 SWITCH_STMT. */
6601 tree
6602 c_start_case (tree exp)
6604 enum tree_code code;
6605 tree type, orig_type = error_mark_node;
6606 struct c_switch *cs;
6608 if (exp != error_mark_node)
6610 code = TREE_CODE (TREE_TYPE (exp));
6611 orig_type = TREE_TYPE (exp);
6613 if (!INTEGRAL_TYPE_P (orig_type)
6614 && code != ERROR_MARK)
6616 error ("switch quantity not an integer");
6617 exp = integer_zero_node;
6618 orig_type = error_mark_node;
6620 else
6622 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6624 if (warn_traditional && !in_system_header
6625 && (type == long_integer_type_node
6626 || type == long_unsigned_type_node))
6627 warning ("%<long%> switch expression not converted to "
6628 "%<int%> in ISO C");
6630 exp = default_conversion (exp);
6631 type = TREE_TYPE (exp);
6635 /* Add this new SWITCH_STMT to the stack. */
6636 cs = XNEW (struct c_switch);
6637 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6638 cs->orig_type = orig_type;
6639 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6640 cs->next = c_switch_stack;
6641 c_switch_stack = cs;
6643 return add_stmt (cs->switch_stmt);
6646 /* Process a case label. */
6648 tree
6649 do_case (tree low_value, tree high_value)
6651 tree label = NULL_TREE;
6653 if (c_switch_stack)
6655 label = c_add_case_label (c_switch_stack->cases,
6656 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6657 c_switch_stack->orig_type,
6658 low_value, high_value);
6659 if (label == error_mark_node)
6660 label = NULL_TREE;
6662 else if (low_value)
6663 error ("case label not within a switch statement");
6664 else
6665 error ("%<default%> label not within a switch statement");
6667 return label;
6670 /* Finish the switch statement. */
6672 void
6673 c_finish_case (tree body)
6675 struct c_switch *cs = c_switch_stack;
6677 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6679 /* Emit warnings as needed. */
6680 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6682 /* Pop the stack. */
6683 c_switch_stack = cs->next;
6684 splay_tree_delete (cs->cases);
6685 XDELETE (cs);
6688 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6689 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6690 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6691 statement, and was not surrounded with parenthesis. */
6693 void
6694 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6695 tree else_block, bool nested_if)
6697 tree stmt;
6699 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6700 if (warn_parentheses && nested_if && else_block == NULL)
6702 tree inner_if = then_block;
6704 /* We know from the grammar productions that there is an IF nested
6705 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6706 it might not be exactly THEN_BLOCK, but should be the last
6707 non-container statement within. */
6708 while (1)
6709 switch (TREE_CODE (inner_if))
6711 case COND_EXPR:
6712 goto found;
6713 case BIND_EXPR:
6714 inner_if = BIND_EXPR_BODY (inner_if);
6715 break;
6716 case STATEMENT_LIST:
6717 inner_if = expr_last (then_block);
6718 break;
6719 case TRY_FINALLY_EXPR:
6720 case TRY_CATCH_EXPR:
6721 inner_if = TREE_OPERAND (inner_if, 0);
6722 break;
6723 default:
6724 gcc_unreachable ();
6726 found:
6728 if (COND_EXPR_ELSE (inner_if))
6729 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6730 &if_locus);
6733 /* Diagnose ";" via the special empty statement node that we create. */
6734 if (extra_warnings)
6736 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6738 if (!else_block)
6739 warning ("%Hempty body in an if-statement",
6740 EXPR_LOCUS (then_block));
6741 then_block = alloc_stmt_list ();
6743 if (else_block
6744 && TREE_CODE (else_block) == NOP_EXPR
6745 && !TREE_TYPE (else_block))
6747 warning ("%Hempty body in an else-statement",
6748 EXPR_LOCUS (else_block));
6749 else_block = alloc_stmt_list ();
6753 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6754 SET_EXPR_LOCATION (stmt, if_locus);
6755 add_stmt (stmt);
6758 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6759 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6760 is false for DO loops. INCR is the FOR increment expression. BODY is
6761 the statement controlled by the loop. BLAB is the break label. CLAB is
6762 the continue label. Everything is allowed to be NULL. */
6764 void
6765 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6766 tree blab, tree clab, bool cond_is_first)
6768 tree entry = NULL, exit = NULL, t;
6770 /* If the condition is zero don't generate a loop construct. */
6771 if (cond && integer_zerop (cond))
6773 if (cond_is_first)
6775 t = build_and_jump (&blab);
6776 SET_EXPR_LOCATION (t, start_locus);
6777 add_stmt (t);
6780 else
6782 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6784 /* If we have an exit condition, then we build an IF with gotos either
6785 out of the loop, or to the top of it. If there's no exit condition,
6786 then we just build a jump back to the top. */
6787 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6789 if (cond && !integer_nonzerop (cond))
6791 /* Canonicalize the loop condition to the end. This means
6792 generating a branch to the loop condition. Reuse the
6793 continue label, if possible. */
6794 if (cond_is_first)
6796 if (incr || !clab)
6798 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6799 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6801 else
6802 t = build1 (GOTO_EXPR, void_type_node, clab);
6803 SET_EXPR_LOCATION (t, start_locus);
6804 add_stmt (t);
6807 t = build_and_jump (&blab);
6808 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6809 exit = fold (exit);
6810 if (cond_is_first)
6811 SET_EXPR_LOCATION (exit, start_locus);
6812 else
6813 SET_EXPR_LOCATION (exit, input_location);
6816 add_stmt (top);
6819 if (body)
6820 add_stmt (body);
6821 if (clab)
6822 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6823 if (incr)
6824 add_stmt (incr);
6825 if (entry)
6826 add_stmt (entry);
6827 if (exit)
6828 add_stmt (exit);
6829 if (blab)
6830 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6833 tree
6834 c_finish_bc_stmt (tree *label_p, bool is_break)
6836 bool skip;
6837 tree label = *label_p;
6839 /* In switch statements break is sometimes stylistically used after
6840 a return statement. This can lead to spurious warnings about
6841 control reaching the end of a non-void function when it is
6842 inlined. Note that we are calling block_may_fallthru with
6843 language specific tree nodes; this works because
6844 block_may_fallthru returns true when given something it does not
6845 understand. */
6846 skip = !block_may_fallthru (cur_stmt_list);
6848 if (!label)
6850 if (!skip)
6851 *label_p = label = create_artificial_label ();
6853 else if (TREE_CODE (label) != LABEL_DECL)
6855 if (is_break)
6856 error ("break statement not within loop or switch");
6857 else
6858 error ("continue statement not within a loop");
6859 return NULL_TREE;
6862 if (skip)
6863 return NULL_TREE;
6865 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6868 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6870 static void
6871 emit_side_effect_warnings (tree expr)
6873 if (expr == error_mark_node)
6875 else if (!TREE_SIDE_EFFECTS (expr))
6877 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6878 warning ("%Hstatement with no effect",
6879 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6881 else if (warn_unused_value)
6882 warn_if_unused_value (expr, input_location);
6885 /* Process an expression as if it were a complete statement. Emit
6886 diagnostics, but do not call ADD_STMT. */
6888 tree
6889 c_process_expr_stmt (tree expr)
6891 if (!expr)
6892 return NULL_TREE;
6894 /* Do default conversion if safe and possibly important,
6895 in case within ({...}). */
6896 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6897 && (flag_isoc99 || lvalue_p (expr)))
6898 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6899 expr = default_conversion (expr);
6901 if (warn_sequence_point)
6902 verify_sequence_points (expr);
6904 if (TREE_TYPE (expr) != error_mark_node
6905 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6906 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6907 error ("expression statement has incomplete type");
6909 /* If we're not processing a statement expression, warn about unused values.
6910 Warnings for statement expressions will be emitted later, once we figure
6911 out which is the result. */
6912 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6913 && (extra_warnings || warn_unused_value))
6914 emit_side_effect_warnings (expr);
6916 /* If the expression is not of a type to which we cannot assign a line
6917 number, wrap the thing in a no-op NOP_EXPR. */
6918 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6919 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6921 if (EXPR_P (expr))
6922 SET_EXPR_LOCATION (expr, input_location);
6924 return expr;
6927 /* Emit an expression as a statement. */
6929 tree
6930 c_finish_expr_stmt (tree expr)
6932 if (expr)
6933 return add_stmt (c_process_expr_stmt (expr));
6934 else
6935 return NULL;
6938 /* Do the opposite and emit a statement as an expression. To begin,
6939 create a new binding level and return it. */
6941 tree
6942 c_begin_stmt_expr (void)
6944 tree ret;
6946 /* We must force a BLOCK for this level so that, if it is not expanded
6947 later, there is a way to turn off the entire subtree of blocks that
6948 are contained in it. */
6949 keep_next_level ();
6950 ret = c_begin_compound_stmt (true);
6952 /* Mark the current statement list as belonging to a statement list. */
6953 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6955 return ret;
6958 tree
6959 c_finish_stmt_expr (tree body)
6961 tree last, type, tmp, val;
6962 tree *last_p;
6964 body = c_end_compound_stmt (body, true);
6966 /* Locate the last statement in BODY. See c_end_compound_stmt
6967 about always returning a BIND_EXPR. */
6968 last_p = &BIND_EXPR_BODY (body);
6969 last = BIND_EXPR_BODY (body);
6971 continue_searching:
6972 if (TREE_CODE (last) == STATEMENT_LIST)
6974 tree_stmt_iterator i;
6976 /* This can happen with degenerate cases like ({ }). No value. */
6977 if (!TREE_SIDE_EFFECTS (last))
6978 return body;
6980 /* If we're supposed to generate side effects warnings, process
6981 all of the statements except the last. */
6982 if (extra_warnings || warn_unused_value)
6984 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6985 emit_side_effect_warnings (tsi_stmt (i));
6987 else
6988 i = tsi_last (last);
6989 last_p = tsi_stmt_ptr (i);
6990 last = *last_p;
6993 /* If the end of the list is exception related, then the list was split
6994 by a call to push_cleanup. Continue searching. */
6995 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6996 || TREE_CODE (last) == TRY_CATCH_EXPR)
6998 last_p = &TREE_OPERAND (last, 0);
6999 last = *last_p;
7000 goto continue_searching;
7003 /* In the case that the BIND_EXPR is not necessary, return the
7004 expression out from inside it. */
7005 if (last == error_mark_node
7006 || (last == BIND_EXPR_BODY (body)
7007 && BIND_EXPR_VARS (body) == NULL))
7008 return last;
7010 /* Extract the type of said expression. */
7011 type = TREE_TYPE (last);
7013 /* If we're not returning a value at all, then the BIND_EXPR that
7014 we already have is a fine expression to return. */
7015 if (!type || VOID_TYPE_P (type))
7016 return body;
7018 /* Now that we've located the expression containing the value, it seems
7019 silly to make voidify_wrapper_expr repeat the process. Create a
7020 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7021 tmp = create_tmp_var_raw (type, NULL);
7023 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7024 tree_expr_nonnegative_p giving up immediately. */
7025 val = last;
7026 if (TREE_CODE (val) == NOP_EXPR
7027 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7028 val = TREE_OPERAND (val, 0);
7030 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7031 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7033 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7036 /* Begin and end compound statements. This is as simple as pushing
7037 and popping new statement lists from the tree. */
7039 tree
7040 c_begin_compound_stmt (bool do_scope)
7042 tree stmt = push_stmt_list ();
7043 if (do_scope)
7044 push_scope ();
7045 return stmt;
7048 tree
7049 c_end_compound_stmt (tree stmt, bool do_scope)
7051 tree block = NULL;
7053 if (do_scope)
7055 if (c_dialect_objc ())
7056 objc_clear_super_receiver ();
7057 block = pop_scope ();
7060 stmt = pop_stmt_list (stmt);
7061 stmt = c_build_bind_expr (block, stmt);
7063 /* If this compound statement is nested immediately inside a statement
7064 expression, then force a BIND_EXPR to be created. Otherwise we'll
7065 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7066 STATEMENT_LISTs merge, and thus we can lose track of what statement
7067 was really last. */
7068 if (cur_stmt_list
7069 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7070 && TREE_CODE (stmt) != BIND_EXPR)
7072 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7073 TREE_SIDE_EFFECTS (stmt) = 1;
7076 return stmt;
7079 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7080 when the current scope is exited. EH_ONLY is true when this is not
7081 meant to apply to normal control flow transfer. */
7083 void
7084 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7086 enum tree_code code;
7087 tree stmt, list;
7088 bool stmt_expr;
7090 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7091 stmt = build_stmt (code, NULL, cleanup);
7092 add_stmt (stmt);
7093 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7094 list = push_stmt_list ();
7095 TREE_OPERAND (stmt, 0) = list;
7096 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7099 /* Build a binary-operation expression without default conversions.
7100 CODE is the kind of expression to build.
7101 This function differs from `build' in several ways:
7102 the data type of the result is computed and recorded in it,
7103 warnings are generated if arg data types are invalid,
7104 special handling for addition and subtraction of pointers is known,
7105 and some optimization is done (operations on narrow ints
7106 are done in the narrower type when that gives the same result).
7107 Constant folding is also done before the result is returned.
7109 Note that the operands will never have enumeral types, or function
7110 or array types, because either they will have the default conversions
7111 performed or they have both just been converted to some other type in which
7112 the arithmetic is to be done. */
7114 tree
7115 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7116 int convert_p)
7118 tree type0, type1;
7119 enum tree_code code0, code1;
7120 tree op0, op1;
7122 /* Expression code to give to the expression when it is built.
7123 Normally this is CODE, which is what the caller asked for,
7124 but in some special cases we change it. */
7125 enum tree_code resultcode = code;
7127 /* Data type in which the computation is to be performed.
7128 In the simplest cases this is the common type of the arguments. */
7129 tree result_type = NULL;
7131 /* Nonzero means operands have already been type-converted
7132 in whatever way is necessary.
7133 Zero means they need to be converted to RESULT_TYPE. */
7134 int converted = 0;
7136 /* Nonzero means create the expression with this type, rather than
7137 RESULT_TYPE. */
7138 tree build_type = 0;
7140 /* Nonzero means after finally constructing the expression
7141 convert it to this type. */
7142 tree final_type = 0;
7144 /* Nonzero if this is an operation like MIN or MAX which can
7145 safely be computed in short if both args are promoted shorts.
7146 Also implies COMMON.
7147 -1 indicates a bitwise operation; this makes a difference
7148 in the exact conditions for when it is safe to do the operation
7149 in a narrower mode. */
7150 int shorten = 0;
7152 /* Nonzero if this is a comparison operation;
7153 if both args are promoted shorts, compare the original shorts.
7154 Also implies COMMON. */
7155 int short_compare = 0;
7157 /* Nonzero if this is a right-shift operation, which can be computed on the
7158 original short and then promoted if the operand is a promoted short. */
7159 int short_shift = 0;
7161 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7162 int common = 0;
7164 if (convert_p)
7166 op0 = default_conversion (orig_op0);
7167 op1 = default_conversion (orig_op1);
7169 else
7171 op0 = orig_op0;
7172 op1 = orig_op1;
7175 type0 = TREE_TYPE (op0);
7176 type1 = TREE_TYPE (op1);
7178 /* The expression codes of the data types of the arguments tell us
7179 whether the arguments are integers, floating, pointers, etc. */
7180 code0 = TREE_CODE (type0);
7181 code1 = TREE_CODE (type1);
7183 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7184 STRIP_TYPE_NOPS (op0);
7185 STRIP_TYPE_NOPS (op1);
7187 /* If an error was already reported for one of the arguments,
7188 avoid reporting another error. */
7190 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7191 return error_mark_node;
7193 switch (code)
7195 case PLUS_EXPR:
7196 /* Handle the pointer + int case. */
7197 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7198 return pointer_int_sum (PLUS_EXPR, op0, op1);
7199 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7200 return pointer_int_sum (PLUS_EXPR, op1, op0);
7201 else
7202 common = 1;
7203 break;
7205 case MINUS_EXPR:
7206 /* Subtraction of two similar pointers.
7207 We must subtract them as integers, then divide by object size. */
7208 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7209 && comp_target_types (type0, type1, 1))
7210 return pointer_diff (op0, op1);
7211 /* Handle pointer minus int. Just like pointer plus int. */
7212 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7213 return pointer_int_sum (MINUS_EXPR, op0, op1);
7214 else
7215 common = 1;
7216 break;
7218 case MULT_EXPR:
7219 common = 1;
7220 break;
7222 case TRUNC_DIV_EXPR:
7223 case CEIL_DIV_EXPR:
7224 case FLOOR_DIV_EXPR:
7225 case ROUND_DIV_EXPR:
7226 case EXACT_DIV_EXPR:
7227 /* Floating point division by zero is a legitimate way to obtain
7228 infinities and NaNs. */
7229 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7230 warning ("division by zero");
7232 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7233 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7234 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7235 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7237 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7238 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7239 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7240 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7242 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7243 resultcode = RDIV_EXPR;
7244 else
7245 /* Although it would be tempting to shorten always here, that
7246 loses on some targets, since the modulo instruction is
7247 undefined if the quotient can't be represented in the
7248 computation mode. We shorten only if unsigned or if
7249 dividing by something we know != -1. */
7250 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7251 || (TREE_CODE (op1) == INTEGER_CST
7252 && !integer_all_onesp (op1)));
7253 common = 1;
7255 break;
7257 case BIT_AND_EXPR:
7258 case BIT_IOR_EXPR:
7259 case BIT_XOR_EXPR:
7260 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7261 shorten = -1;
7262 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7263 common = 1;
7264 break;
7266 case TRUNC_MOD_EXPR:
7267 case FLOOR_MOD_EXPR:
7268 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7269 warning ("division by zero");
7271 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7273 /* Although it would be tempting to shorten always here, that loses
7274 on some targets, since the modulo instruction is undefined if the
7275 quotient can't be represented in the computation mode. We shorten
7276 only if unsigned or if dividing by something we know != -1. */
7277 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7278 || (TREE_CODE (op1) == INTEGER_CST
7279 && !integer_all_onesp (op1)));
7280 common = 1;
7282 break;
7284 case TRUTH_ANDIF_EXPR:
7285 case TRUTH_ORIF_EXPR:
7286 case TRUTH_AND_EXPR:
7287 case TRUTH_OR_EXPR:
7288 case TRUTH_XOR_EXPR:
7289 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7290 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7291 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7292 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7294 /* Result of these operations is always an int,
7295 but that does not mean the operands should be
7296 converted to ints! */
7297 result_type = integer_type_node;
7298 op0 = lang_hooks.truthvalue_conversion (op0);
7299 op1 = lang_hooks.truthvalue_conversion (op1);
7300 converted = 1;
7302 break;
7304 /* Shift operations: result has same type as first operand;
7305 always convert second operand to int.
7306 Also set SHORT_SHIFT if shifting rightward. */
7308 case RSHIFT_EXPR:
7309 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7311 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7313 if (tree_int_cst_sgn (op1) < 0)
7314 warning ("right shift count is negative");
7315 else
7317 if (!integer_zerop (op1))
7318 short_shift = 1;
7320 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7321 warning ("right shift count >= width of type");
7325 /* Use the type of the value to be shifted. */
7326 result_type = type0;
7327 /* Convert the shift-count to an integer, regardless of size
7328 of value being shifted. */
7329 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7330 op1 = convert (integer_type_node, op1);
7331 /* Avoid converting op1 to result_type later. */
7332 converted = 1;
7334 break;
7336 case LSHIFT_EXPR:
7337 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7339 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7341 if (tree_int_cst_sgn (op1) < 0)
7342 warning ("left shift count is negative");
7344 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7345 warning ("left shift count >= width of type");
7348 /* Use the type of the value to be shifted. */
7349 result_type = type0;
7350 /* Convert the shift-count to an integer, regardless of size
7351 of value being shifted. */
7352 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7353 op1 = convert (integer_type_node, op1);
7354 /* Avoid converting op1 to result_type later. */
7355 converted = 1;
7357 break;
7359 case EQ_EXPR:
7360 case NE_EXPR:
7361 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7362 warning ("comparing floating point with == or != is unsafe");
7363 /* Result of comparison is always int,
7364 but don't convert the args to int! */
7365 build_type = integer_type_node;
7366 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7367 || code0 == COMPLEX_TYPE)
7368 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7369 || code1 == COMPLEX_TYPE))
7370 short_compare = 1;
7371 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7373 tree tt0 = TREE_TYPE (type0);
7374 tree tt1 = TREE_TYPE (type1);
7375 /* Anything compares with void *. void * compares with anything.
7376 Otherwise, the targets must be compatible
7377 and both must be object or both incomplete. */
7378 if (comp_target_types (type0, type1, 1))
7379 result_type = common_pointer_type (type0, type1);
7380 else if (VOID_TYPE_P (tt0))
7382 /* op0 != orig_op0 detects the case of something
7383 whose value is 0 but which isn't a valid null ptr const. */
7384 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7385 && TREE_CODE (tt1) == FUNCTION_TYPE)
7386 pedwarn ("ISO C forbids comparison of %<void *%>"
7387 " with function pointer");
7389 else if (VOID_TYPE_P (tt1))
7391 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7392 && TREE_CODE (tt0) == FUNCTION_TYPE)
7393 pedwarn ("ISO C forbids comparison of %<void *%>"
7394 " with function pointer");
7396 else
7397 pedwarn ("comparison of distinct pointer types lacks a cast");
7399 if (result_type == NULL_TREE)
7400 result_type = ptr_type_node;
7402 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7403 && integer_zerop (op1))
7404 result_type = type0;
7405 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7406 && integer_zerop (op0))
7407 result_type = type1;
7408 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7410 result_type = type0;
7411 pedwarn ("comparison between pointer and integer");
7413 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7415 result_type = type1;
7416 pedwarn ("comparison between pointer and integer");
7418 break;
7420 case LE_EXPR:
7421 case GE_EXPR:
7422 case LT_EXPR:
7423 case GT_EXPR:
7424 build_type = integer_type_node;
7425 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7426 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7427 short_compare = 1;
7428 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7430 if (comp_target_types (type0, type1, 1))
7432 result_type = common_pointer_type (type0, type1);
7433 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7434 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7435 pedwarn ("comparison of complete and incomplete pointers");
7436 else if (pedantic
7437 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7438 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7440 else
7442 result_type = ptr_type_node;
7443 pedwarn ("comparison of distinct pointer types lacks a cast");
7446 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7447 && integer_zerop (op1))
7449 result_type = type0;
7450 if (pedantic || extra_warnings)
7451 pedwarn ("ordered comparison of pointer with integer zero");
7453 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7454 && integer_zerop (op0))
7456 result_type = type1;
7457 if (pedantic)
7458 pedwarn ("ordered comparison of pointer with integer zero");
7460 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7462 result_type = type0;
7463 pedwarn ("comparison between pointer and integer");
7465 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7467 result_type = type1;
7468 pedwarn ("comparison between pointer and integer");
7470 break;
7472 default:
7473 gcc_unreachable ();
7476 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7477 return error_mark_node;
7479 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7480 || code0 == VECTOR_TYPE)
7482 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7483 || code1 == VECTOR_TYPE))
7485 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7487 if (shorten || common || short_compare)
7488 result_type = c_common_type (type0, type1);
7490 /* For certain operations (which identify themselves by shorten != 0)
7491 if both args were extended from the same smaller type,
7492 do the arithmetic in that type and then extend.
7494 shorten !=0 and !=1 indicates a bitwise operation.
7495 For them, this optimization is safe only if
7496 both args are zero-extended or both are sign-extended.
7497 Otherwise, we might change the result.
7498 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7499 but calculated in (unsigned short) it would be (unsigned short)-1. */
7501 if (shorten && none_complex)
7503 int unsigned0, unsigned1;
7504 tree arg0 = get_narrower (op0, &unsigned0);
7505 tree arg1 = get_narrower (op1, &unsigned1);
7506 /* UNS is 1 if the operation to be done is an unsigned one. */
7507 int uns = TYPE_UNSIGNED (result_type);
7508 tree type;
7510 final_type = result_type;
7512 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7513 but it *requires* conversion to FINAL_TYPE. */
7515 if ((TYPE_PRECISION (TREE_TYPE (op0))
7516 == TYPE_PRECISION (TREE_TYPE (arg0)))
7517 && TREE_TYPE (op0) != final_type)
7518 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7519 if ((TYPE_PRECISION (TREE_TYPE (op1))
7520 == TYPE_PRECISION (TREE_TYPE (arg1)))
7521 && TREE_TYPE (op1) != final_type)
7522 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7524 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7526 /* For bitwise operations, signedness of nominal type
7527 does not matter. Consider only how operands were extended. */
7528 if (shorten == -1)
7529 uns = unsigned0;
7531 /* Note that in all three cases below we refrain from optimizing
7532 an unsigned operation on sign-extended args.
7533 That would not be valid. */
7535 /* Both args variable: if both extended in same way
7536 from same width, do it in that width.
7537 Do it unsigned if args were zero-extended. */
7538 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7539 < TYPE_PRECISION (result_type))
7540 && (TYPE_PRECISION (TREE_TYPE (arg1))
7541 == TYPE_PRECISION (TREE_TYPE (arg0)))
7542 && unsigned0 == unsigned1
7543 && (unsigned0 || !uns))
7544 result_type
7545 = c_common_signed_or_unsigned_type
7546 (unsigned0, c_common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7547 else if (TREE_CODE (arg0) == INTEGER_CST
7548 && (unsigned1 || !uns)
7549 && (TYPE_PRECISION (TREE_TYPE (arg1))
7550 < TYPE_PRECISION (result_type))
7551 && (type
7552 = c_common_signed_or_unsigned_type (unsigned1,
7553 TREE_TYPE (arg1)),
7554 int_fits_type_p (arg0, type)))
7555 result_type = type;
7556 else if (TREE_CODE (arg1) == INTEGER_CST
7557 && (unsigned0 || !uns)
7558 && (TYPE_PRECISION (TREE_TYPE (arg0))
7559 < TYPE_PRECISION (result_type))
7560 && (type
7561 = c_common_signed_or_unsigned_type (unsigned0,
7562 TREE_TYPE (arg0)),
7563 int_fits_type_p (arg1, type)))
7564 result_type = type;
7567 /* Shifts can be shortened if shifting right. */
7569 if (short_shift)
7571 int unsigned_arg;
7572 tree arg0 = get_narrower (op0, &unsigned_arg);
7574 final_type = result_type;
7576 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7577 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7579 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7580 /* We can shorten only if the shift count is less than the
7581 number of bits in the smaller type size. */
7582 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7583 /* We cannot drop an unsigned shift after sign-extension. */
7584 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7586 /* Do an unsigned shift if the operand was zero-extended. */
7587 result_type
7588 = c_common_signed_or_unsigned_type (unsigned_arg,
7589 TREE_TYPE (arg0));
7590 /* Convert value-to-be-shifted to that type. */
7591 if (TREE_TYPE (op0) != result_type)
7592 op0 = convert (result_type, op0);
7593 converted = 1;
7597 /* Comparison operations are shortened too but differently.
7598 They identify themselves by setting short_compare = 1. */
7600 if (short_compare)
7602 /* Don't write &op0, etc., because that would prevent op0
7603 from being kept in a register.
7604 Instead, make copies of the our local variables and
7605 pass the copies by reference, then copy them back afterward. */
7606 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7607 enum tree_code xresultcode = resultcode;
7608 tree val
7609 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7611 if (val != 0)
7612 return val;
7614 op0 = xop0, op1 = xop1;
7615 converted = 1;
7616 resultcode = xresultcode;
7618 if (warn_sign_compare && skip_evaluation == 0)
7620 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7621 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7622 int unsignedp0, unsignedp1;
7623 tree primop0 = get_narrower (op0, &unsignedp0);
7624 tree primop1 = get_narrower (op1, &unsignedp1);
7626 xop0 = orig_op0;
7627 xop1 = orig_op1;
7628 STRIP_TYPE_NOPS (xop0);
7629 STRIP_TYPE_NOPS (xop1);
7631 /* Give warnings for comparisons between signed and unsigned
7632 quantities that may fail.
7634 Do the checking based on the original operand trees, so that
7635 casts will be considered, but default promotions won't be.
7637 Do not warn if the comparison is being done in a signed type,
7638 since the signed type will only be chosen if it can represent
7639 all the values of the unsigned type. */
7640 if (!TYPE_UNSIGNED (result_type))
7641 /* OK */;
7642 /* Do not warn if both operands are the same signedness. */
7643 else if (op0_signed == op1_signed)
7644 /* OK */;
7645 else
7647 tree sop, uop;
7649 if (op0_signed)
7650 sop = xop0, uop = xop1;
7651 else
7652 sop = xop1, uop = xop0;
7654 /* Do not warn if the signed quantity is an
7655 unsuffixed integer literal (or some static
7656 constant expression involving such literals or a
7657 conditional expression involving such literals)
7658 and it is non-negative. */
7659 if (tree_expr_nonnegative_p (sop))
7660 /* OK */;
7661 /* Do not warn if the comparison is an equality operation,
7662 the unsigned quantity is an integral constant, and it
7663 would fit in the result if the result were signed. */
7664 else if (TREE_CODE (uop) == INTEGER_CST
7665 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7666 && int_fits_type_p
7667 (uop, c_common_signed_type (result_type)))
7668 /* OK */;
7669 /* Do not warn if the unsigned quantity is an enumeration
7670 constant and its maximum value would fit in the result
7671 if the result were signed. */
7672 else if (TREE_CODE (uop) == INTEGER_CST
7673 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7674 && int_fits_type_p
7675 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7676 c_common_signed_type (result_type)))
7677 /* OK */;
7678 else
7679 warning ("comparison between signed and unsigned");
7682 /* Warn if two unsigned values are being compared in a size
7683 larger than their original size, and one (and only one) is the
7684 result of a `~' operator. This comparison will always fail.
7686 Also warn if one operand is a constant, and the constant
7687 does not have all bits set that are set in the ~ operand
7688 when it is extended. */
7690 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7691 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7693 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7694 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7695 &unsignedp0);
7696 else
7697 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7698 &unsignedp1);
7700 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7702 tree primop;
7703 HOST_WIDE_INT constant, mask;
7704 int unsignedp, bits;
7706 if (host_integerp (primop0, 0))
7708 primop = primop1;
7709 unsignedp = unsignedp1;
7710 constant = tree_low_cst (primop0, 0);
7712 else
7714 primop = primop0;
7715 unsignedp = unsignedp0;
7716 constant = tree_low_cst (primop1, 0);
7719 bits = TYPE_PRECISION (TREE_TYPE (primop));
7720 if (bits < TYPE_PRECISION (result_type)
7721 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7723 mask = (~(HOST_WIDE_INT) 0) << bits;
7724 if ((mask & constant) != mask)
7725 warning ("comparison of promoted ~unsigned with constant");
7728 else if (unsignedp0 && unsignedp1
7729 && (TYPE_PRECISION (TREE_TYPE (primop0))
7730 < TYPE_PRECISION (result_type))
7731 && (TYPE_PRECISION (TREE_TYPE (primop1))
7732 < TYPE_PRECISION (result_type)))
7733 warning ("comparison of promoted ~unsigned with unsigned");
7739 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7740 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7741 Then the expression will be built.
7742 It will be given type FINAL_TYPE if that is nonzero;
7743 otherwise, it will be given type RESULT_TYPE. */
7745 if (!result_type)
7747 binary_op_error (code);
7748 return error_mark_node;
7751 if (!converted)
7753 if (TREE_TYPE (op0) != result_type)
7754 op0 = convert (result_type, op0);
7755 if (TREE_TYPE (op1) != result_type)
7756 op1 = convert (result_type, op1);
7758 /* This can happen if one operand has a vector type, and the other
7759 has a different type. */
7760 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7761 return error_mark_node;
7764 if (build_type == NULL_TREE)
7765 build_type = result_type;
7768 tree result = build2 (resultcode, build_type, op0, op1);
7770 /* Treat expressions in initializers specially as they can't trap. */
7771 result = require_constant_value ? fold_initializer (result)
7772 : fold (result);
7774 if (final_type != 0)
7775 result = convert (final_type, result);
7776 return result;