* config/i386/uwin.h: Remove SUBTARGET_PROLOGUE.
[official-gcc.git] / gcc / c-typeck.c
blobc4e482ee240b990d186387753e54890b2be59309
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 /* 1 if we explained undeclared var errors. */
53 static int undeclared_variable_notice;
55 static tree qualify_type PARAMS ((tree, tree));
56 static int comp_target_types PARAMS ((tree, tree, int));
57 static int function_types_compatible_p PARAMS ((tree, tree));
58 static int type_lists_compatible_p PARAMS ((tree, tree));
59 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
60 static tree default_function_array_conversion PARAMS ((tree));
61 static tree lookup_field PARAMS ((tree, tree));
62 static void undeclared_variable PARAMS ((tree));
63 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
64 static tree pointer_diff PARAMS ((tree, tree));
65 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
66 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
67 static tree internal_build_compound_expr PARAMS ((tree, int));
68 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
69 tree, tree, int));
70 static void warn_for_assignment PARAMS ((const char *, const char *,
71 tree, int));
72 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
73 static void push_string PARAMS ((const char *));
74 static void push_member_name PARAMS ((tree));
75 static void push_array_bounds PARAMS ((int));
76 static int spelling_length PARAMS ((void));
77 static char *print_spelling PARAMS ((char *));
78 static void warning_init PARAMS ((const char *));
79 static tree digest_init PARAMS ((tree, tree, int));
80 static void output_init_element PARAMS ((tree, tree, tree, int));
81 static void output_pending_init_elements PARAMS ((int));
82 static int set_designator PARAMS ((int));
83 static void push_range_stack PARAMS ((tree));
84 static void add_pending_init PARAMS ((tree, tree));
85 static void set_nonincremental_init PARAMS ((void));
86 static void set_nonincremental_init_from_string PARAMS ((tree));
87 static tree find_init_member PARAMS ((tree));
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.) */
92 tree
93 require_complete_type (value)
94 tree value;
96 tree type = TREE_TYPE (value);
98 if (value == error_mark_node || type == error_mark_node)
99 return error_mark_node;
101 /* First, detect a valid value with a complete type. */
102 if (COMPLETE_TYPE_P (type))
103 return value;
105 c_incomplete_type_error (value, type);
106 return error_mark_node;
109 /* Print an error message for invalid use of an incomplete type.
110 VALUE is the expression that was used (or 0 if that isn't known)
111 and TYPE is the type that was invalid. */
113 void
114 c_incomplete_type_error (value, type)
115 tree value;
116 tree type;
118 const char *type_code_string;
120 /* Avoid duplicate error message. */
121 if (TREE_CODE (type) == ERROR_MARK)
122 return;
124 if (value != 0 && (TREE_CODE (value) == VAR_DECL
125 || TREE_CODE (value) == PARM_DECL))
126 error ("`%s' has an incomplete type",
127 IDENTIFIER_POINTER (DECL_NAME (value)));
128 else
130 retry:
131 /* We must print an error message. Be clever about what it says. */
133 switch (TREE_CODE (type))
135 case RECORD_TYPE:
136 type_code_string = "struct";
137 break;
139 case UNION_TYPE:
140 type_code_string = "union";
141 break;
143 case ENUMERAL_TYPE:
144 type_code_string = "enum";
145 break;
147 case VOID_TYPE:
148 error ("invalid use of void expression");
149 return;
151 case ARRAY_TYPE:
152 if (TYPE_DOMAIN (type))
154 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
156 error ("invalid use of flexible array member");
157 return;
159 type = TREE_TYPE (type);
160 goto retry;
162 error ("invalid use of array with unspecified bounds");
163 return;
165 default:
166 abort ();
169 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
170 error ("invalid use of undefined type `%s %s'",
171 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
172 else
173 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
174 error ("invalid use of incomplete typedef `%s'",
175 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
179 /* Given a type, apply default promotions wrt unnamed function
180 arguments and return the new type. */
182 tree
183 c_type_promotes_to (type)
184 tree type;
186 if (TYPE_MAIN_VARIANT (type) == float_type_node)
187 return double_type_node;
189 if (c_promoting_integer_type_p (type))
191 /* Preserve unsignedness if not really getting any wider. */
192 if (TREE_UNSIGNED (type)
193 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
194 return unsigned_type_node;
195 return integer_type_node;
198 return type;
201 /* Return a variant of TYPE which has all the type qualifiers of LIKE
202 as well as those of TYPE. */
204 static tree
205 qualify_type (type, like)
206 tree type, like;
208 return c_build_qualified_type (type,
209 TYPE_QUALS (type) | TYPE_QUALS (like));
212 /* Return the common type of two types.
213 We assume that comptypes has already been done and returned 1;
214 if that isn't so, this may crash. In particular, we assume that qualifiers
215 match.
217 This is the type for the result of most arithmetic operations
218 if the operands have the given two types. */
220 tree
221 common_type (t1, t2)
222 tree t1, t2;
224 enum tree_code code1;
225 enum tree_code code2;
226 tree attributes;
228 /* Save time if the two types are the same. */
230 if (t1 == t2) return t1;
232 /* If one type is nonsense, use the other. */
233 if (t1 == error_mark_node)
234 return t2;
235 if (t2 == error_mark_node)
236 return t1;
238 /* Merge the attributes. */
239 attributes = (*targetm.merge_type_attributes) (t1, t2);
241 /* Treat an enum type as the unsigned integer type of the same width. */
243 if (TREE_CODE (t1) == ENUMERAL_TYPE)
244 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
245 if (TREE_CODE (t2) == ENUMERAL_TYPE)
246 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
248 code1 = TREE_CODE (t1);
249 code2 = TREE_CODE (t2);
251 /* If one type is complex, form the common type of the non-complex
252 components, then make that complex. Use T1 or T2 if it is the
253 required type. */
254 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
256 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
257 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
258 tree subtype = common_type (subtype1, subtype2);
260 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
261 return build_type_attribute_variant (t1, attributes);
262 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
263 return build_type_attribute_variant (t2, attributes);
264 else
265 return build_type_attribute_variant (build_complex_type (subtype),
266 attributes);
269 switch (code1)
271 case INTEGER_TYPE:
272 case REAL_TYPE:
273 /* If only one is real, use it as the result. */
275 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
276 return build_type_attribute_variant (t1, attributes);
278 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
279 return build_type_attribute_variant (t2, attributes);
281 /* Both real or both integers; use the one with greater precision. */
283 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
284 return build_type_attribute_variant (t1, attributes);
285 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
286 return build_type_attribute_variant (t2, attributes);
288 /* Same precision. Prefer longs to ints even when same size. */
290 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
291 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
292 return build_type_attribute_variant (long_unsigned_type_node,
293 attributes);
295 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
296 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
298 /* But preserve unsignedness from the other type,
299 since long cannot hold all the values of an unsigned int. */
300 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
301 t1 = long_unsigned_type_node;
302 else
303 t1 = long_integer_type_node;
304 return build_type_attribute_variant (t1, attributes);
307 /* Likewise, prefer long double to double even if same size. */
308 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
309 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
310 return build_type_attribute_variant (long_double_type_node,
311 attributes);
313 /* Otherwise prefer the unsigned one. */
315 if (TREE_UNSIGNED (t1))
316 return build_type_attribute_variant (t1, attributes);
317 else
318 return build_type_attribute_variant (t2, attributes);
320 case POINTER_TYPE:
321 /* For two pointers, do this recursively on the target type,
322 and combine the qualifiers of the two types' targets. */
323 /* This code was turned off; I don't know why.
324 But ANSI C specifies doing this with the qualifiers.
325 So I turned it on again. */
327 tree pointed_to_1 = TREE_TYPE (t1);
328 tree pointed_to_2 = TREE_TYPE (t2);
329 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
330 TYPE_MAIN_VARIANT (pointed_to_2));
331 t1 = build_pointer_type (c_build_qualified_type
332 (target,
333 TYPE_QUALS (pointed_to_1) |
334 TYPE_QUALS (pointed_to_2)));
335 return build_type_attribute_variant (t1, attributes);
337 #if 0
338 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
339 return build_type_attribute_variant (t1, attributes);
340 #endif
342 case ARRAY_TYPE:
344 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
345 /* Save space: see if the result is identical to one of the args. */
346 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
347 return build_type_attribute_variant (t1, attributes);
348 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
349 return build_type_attribute_variant (t2, attributes);
350 /* Merge the element types, and have a size if either arg has one. */
351 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
352 return build_type_attribute_variant (t1, attributes);
355 case FUNCTION_TYPE:
356 /* Function types: prefer the one that specified arg types.
357 If both do, merge the arg types. Also merge the return types. */
359 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
360 tree p1 = TYPE_ARG_TYPES (t1);
361 tree p2 = TYPE_ARG_TYPES (t2);
362 int len;
363 tree newargs, n;
364 int i;
366 /* Save space: see if the result is identical to one of the args. */
367 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
368 return build_type_attribute_variant (t1, attributes);
369 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
370 return build_type_attribute_variant (t2, attributes);
372 /* Simple way if one arg fails to specify argument types. */
373 if (TYPE_ARG_TYPES (t1) == 0)
375 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
376 return build_type_attribute_variant (t1, attributes);
378 if (TYPE_ARG_TYPES (t2) == 0)
380 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
381 return build_type_attribute_variant (t1, attributes);
384 /* If both args specify argument types, we must merge the two
385 lists, argument by argument. */
387 pushlevel (0);
388 declare_parm_level (1);
390 len = list_length (p1);
391 newargs = 0;
393 for (i = 0; i < len; i++)
394 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
396 n = newargs;
398 for (; p1;
399 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
401 /* A null type means arg type is not specified.
402 Take whatever the other function type has. */
403 if (TREE_VALUE (p1) == 0)
405 TREE_VALUE (n) = TREE_VALUE (p2);
406 goto parm_done;
408 if (TREE_VALUE (p2) == 0)
410 TREE_VALUE (n) = TREE_VALUE (p1);
411 goto parm_done;
414 /* Given wait (union {union wait *u; int *i} *)
415 and wait (union wait *),
416 prefer union wait * as type of parm. */
417 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
418 && TREE_VALUE (p1) != TREE_VALUE (p2))
420 tree memb;
421 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
422 memb; memb = TREE_CHAIN (memb))
423 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
425 TREE_VALUE (n) = TREE_VALUE (p2);
426 if (pedantic)
427 pedwarn ("function types not truly compatible in ISO C");
428 goto parm_done;
431 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
432 && TREE_VALUE (p2) != TREE_VALUE (p1))
434 tree memb;
435 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
436 memb; memb = TREE_CHAIN (memb))
437 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
439 TREE_VALUE (n) = TREE_VALUE (p1);
440 if (pedantic)
441 pedwarn ("function types not truly compatible in ISO C");
442 goto parm_done;
445 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
446 parm_done: ;
449 poplevel (0, 0, 0);
451 t1 = build_function_type (valtype, newargs);
452 /* ... falls through ... */
455 default:
456 return build_type_attribute_variant (t1, attributes);
461 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
462 or various other operations. Return 2 if they are compatible
463 but a warning may be needed if you use them together. */
466 comptypes (type1, type2)
467 tree type1, type2;
469 tree t1 = type1;
470 tree t2 = type2;
471 int attrval, val;
473 /* Suppress errors caused by previously reported errors. */
475 if (t1 == t2 || !t1 || !t2
476 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
477 return 1;
479 /* If either type is the internal version of sizetype, return the
480 language version. */
481 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
482 && TYPE_DOMAIN (t1) != 0)
483 t1 = TYPE_DOMAIN (t1);
485 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
486 && TYPE_DOMAIN (t2) != 0)
487 t2 = TYPE_DOMAIN (t2);
489 /* Treat an enum type as the integer type of the same width and
490 signedness. */
492 if (TREE_CODE (t1) == ENUMERAL_TYPE)
493 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
494 if (TREE_CODE (t2) == ENUMERAL_TYPE)
495 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
497 if (t1 == t2)
498 return 1;
500 /* Different classes of types can't be compatible. */
502 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
504 /* Qualifiers must match. */
506 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
507 return 0;
509 /* Allow for two different type nodes which have essentially the same
510 definition. Note that we already checked for equality of the type
511 qualifiers (just above). */
513 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
514 return 1;
516 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
517 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
518 return 0;
520 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
521 val = 0;
523 switch (TREE_CODE (t1))
525 case POINTER_TYPE:
526 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
527 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
528 break;
530 case FUNCTION_TYPE:
531 val = function_types_compatible_p (t1, t2);
532 break;
534 case ARRAY_TYPE:
536 tree d1 = TYPE_DOMAIN (t1);
537 tree d2 = TYPE_DOMAIN (t2);
538 bool d1_variable, d2_variable;
539 bool d1_zero, d2_zero;
540 val = 1;
542 /* Target types must match incl. qualifiers. */
543 if (TREE_TYPE (t1) != TREE_TYPE (t2)
544 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
545 return 0;
547 /* Sizes must match unless one is missing or variable. */
548 if (d1 == 0 || d2 == 0 || d1 == d2)
549 break;
551 d1_zero = ! TYPE_MAX_VALUE (d1);
552 d2_zero = ! TYPE_MAX_VALUE (d2);
554 d1_variable = (! d1_zero
555 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
556 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
557 d2_variable = (! d2_zero
558 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
559 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
561 if (d1_variable || d2_variable)
562 break;
563 if (d1_zero && d2_zero)
564 break;
565 if (d1_zero || d2_zero
566 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
567 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
568 val = 0;
570 break;
573 case RECORD_TYPE:
574 if (flag_objc && objc_comptypes (t1, t2, 0) == 1)
575 val = 1;
576 break;
578 case VECTOR_TYPE:
579 /* The target might allow certain vector types to be compatible. */
580 val = (*targetm.vector_opaque_p) (t1)
581 || (*targetm.vector_opaque_p) (t2);
582 break;
584 default:
585 break;
587 return attrval == 2 && val == 1 ? 2 : val;
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
592 to 1 or 0 depending if the check of the pointer types is meant to
593 be reflexive or not (typically, assignments are not reflexive,
594 while comparisons are reflexive).
597 static int
598 comp_target_types (ttl, ttr, reflexive)
599 tree ttl, ttr;
600 int reflexive;
602 int val;
604 /* Give objc_comptypes a crack at letting these types through. */
605 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
606 return val;
608 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
609 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
611 if (val == 2 && pedantic)
612 pedwarn ("types are not quite compatible");
613 return val;
616 /* Subroutines of `comptypes'. */
618 /* Return 1 if two function types F1 and F2 are compatible.
619 If either type specifies no argument types,
620 the other must specify a fixed number of self-promoting arg types.
621 Otherwise, if one type specifies only the number of arguments,
622 the other must specify that number of self-promoting arg types.
623 Otherwise, the argument types must match. */
625 static int
626 function_types_compatible_p (f1, f2)
627 tree f1, f2;
629 tree args1, args2;
630 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
631 int val = 1;
632 int val1;
633 tree ret1, ret2;
635 ret1 = TREE_TYPE (f1);
636 ret2 = TREE_TYPE (f2);
638 /* 'volatile' qualifiers on a function's return type mean the function
639 is noreturn. */
640 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
641 pedwarn ("function return types not compatible due to `volatile'");
642 if (TYPE_VOLATILE (ret1))
643 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
644 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
645 if (TYPE_VOLATILE (ret2))
646 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
647 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
648 val = comptypes (ret1, ret2);
649 if (val == 0)
650 return 0;
652 args1 = TYPE_ARG_TYPES (f1);
653 args2 = TYPE_ARG_TYPES (f2);
655 /* An unspecified parmlist matches any specified parmlist
656 whose argument types don't need default promotions. */
658 if (args1 == 0)
660 if (!self_promoting_args_p (args2))
661 return 0;
662 /* If one of these types comes from a non-prototype fn definition,
663 compare that with the other type's arglist.
664 If they don't match, ask for a warning (but no error). */
665 if (TYPE_ACTUAL_ARG_TYPES (f1)
666 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
667 val = 2;
668 return val;
670 if (args2 == 0)
672 if (!self_promoting_args_p (args1))
673 return 0;
674 if (TYPE_ACTUAL_ARG_TYPES (f2)
675 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
676 val = 2;
677 return val;
680 /* Both types have argument lists: compare them and propagate results. */
681 val1 = type_lists_compatible_p (args1, args2);
682 return val1 != 1 ? val1 : val;
685 /* Check two lists of types for compatibility,
686 returning 0 for incompatible, 1 for compatible,
687 or 2 for compatible with warning. */
689 static int
690 type_lists_compatible_p (args1, args2)
691 tree args1, args2;
693 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
694 int val = 1;
695 int newval = 0;
697 while (1)
699 if (args1 == 0 && args2 == 0)
700 return val;
701 /* If one list is shorter than the other,
702 they fail to match. */
703 if (args1 == 0 || args2 == 0)
704 return 0;
705 /* A null pointer instead of a type
706 means there is supposed to be an argument
707 but nothing is specified about what type it has.
708 So match anything that self-promotes. */
709 if (TREE_VALUE (args1) == 0)
711 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
712 return 0;
714 else if (TREE_VALUE (args2) == 0)
716 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
717 return 0;
719 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
720 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
722 /* Allow wait (union {union wait *u; int *i} *)
723 and wait (union wait *) to be compatible. */
724 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
725 && (TYPE_NAME (TREE_VALUE (args1)) == 0
726 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
727 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
728 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
729 TYPE_SIZE (TREE_VALUE (args2))))
731 tree memb;
732 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
733 memb; memb = TREE_CHAIN (memb))
734 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
735 break;
736 if (memb == 0)
737 return 0;
739 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
740 && (TYPE_NAME (TREE_VALUE (args2)) == 0
741 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
742 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
743 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
744 TYPE_SIZE (TREE_VALUE (args1))))
746 tree memb;
747 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
748 memb; memb = TREE_CHAIN (memb))
749 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
750 break;
751 if (memb == 0)
752 return 0;
754 else
755 return 0;
758 /* comptypes said ok, but record if it said to warn. */
759 if (newval > val)
760 val = newval;
762 args1 = TREE_CHAIN (args1);
763 args2 = TREE_CHAIN (args2);
767 /* Compute the size to increment a pointer by. */
769 tree
770 c_size_in_bytes (type)
771 tree type;
773 enum tree_code code = TREE_CODE (type);
775 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
776 return size_one_node;
778 if (!COMPLETE_OR_VOID_TYPE_P (type))
780 error ("arithmetic on pointer to an incomplete type");
781 return size_one_node;
784 /* Convert in case a char is more than one unit. */
785 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
786 size_int (TYPE_PRECISION (char_type_node)
787 / BITS_PER_UNIT));
790 /* Return either DECL or its known constant value (if it has one). */
792 tree
793 decl_constant_value (decl)
794 tree decl;
796 if (/* Don't change a variable array bound or initial value to a constant
797 in a place where a variable is invalid. */
798 current_function_decl != 0
799 && ! TREE_THIS_VOLATILE (decl)
800 && TREE_READONLY (decl)
801 && DECL_INITIAL (decl) != 0
802 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
803 /* This is invalid if initial value is not constant.
804 If it has either a function call, a memory reference,
805 or a variable, then re-evaluating it could give different results. */
806 && TREE_CONSTANT (DECL_INITIAL (decl))
807 /* Check for cases where this is sub-optimal, even though valid. */
808 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
809 return DECL_INITIAL (decl);
810 return decl;
813 /* Return either DECL or its known constant value (if it has one), but
814 return DECL if pedantic or DECL has mode BLKmode. This is for
815 bug-compatibility with the old behavior of decl_constant_value
816 (before GCC 3.0); every use of this function is a bug and it should
817 be removed before GCC 3.1. It is not appropriate to use pedantic
818 in a way that affects optimization, and BLKmode is probably not the
819 right test for avoiding misoptimizations either. */
821 static tree
822 decl_constant_value_for_broken_optimization (decl)
823 tree decl;
825 if (pedantic || DECL_MODE (decl) == BLKmode)
826 return decl;
827 else
828 return decl_constant_value (decl);
832 /* Perform the default conversion of arrays and functions to pointers.
833 Return the result of converting EXP. For any other expression, just
834 return EXP. */
836 static tree
837 default_function_array_conversion (exp)
838 tree exp;
840 tree orig_exp;
841 tree type = TREE_TYPE (exp);
842 enum tree_code code = TREE_CODE (type);
843 int not_lvalue = 0;
845 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
846 an lvalue.
848 Do not use STRIP_NOPS here! It will remove conversions from pointer
849 to integer and cause infinite recursion. */
850 orig_exp = exp;
851 while (TREE_CODE (exp) == NON_LVALUE_EXPR
852 || (TREE_CODE (exp) == NOP_EXPR
853 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
855 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
856 not_lvalue = 1;
857 exp = TREE_OPERAND (exp, 0);
860 /* Preserve the original expression code. */
861 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
862 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
864 if (code == FUNCTION_TYPE)
866 return build_unary_op (ADDR_EXPR, exp, 0);
868 if (code == ARRAY_TYPE)
870 tree adr;
871 tree restype = TREE_TYPE (type);
872 tree ptrtype;
873 int constp = 0;
874 int volatilep = 0;
875 int lvalue_array_p;
877 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
879 constp = TREE_READONLY (exp);
880 volatilep = TREE_THIS_VOLATILE (exp);
883 if (TYPE_QUALS (type) || constp || volatilep)
884 restype
885 = c_build_qualified_type (restype,
886 TYPE_QUALS (type)
887 | (constp * TYPE_QUAL_CONST)
888 | (volatilep * TYPE_QUAL_VOLATILE));
890 if (TREE_CODE (exp) == INDIRECT_REF)
891 return convert (TYPE_POINTER_TO (restype),
892 TREE_OPERAND (exp, 0));
894 if (TREE_CODE (exp) == COMPOUND_EXPR)
896 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
897 return build (COMPOUND_EXPR, TREE_TYPE (op1),
898 TREE_OPERAND (exp, 0), op1);
901 lvalue_array_p = !not_lvalue && lvalue_p (exp);
902 if (!flag_isoc99 && !lvalue_array_p)
904 /* Before C99, non-lvalue arrays do not decay to pointers.
905 Normally, using such an array would be invalid; but it can
906 be used correctly inside sizeof or as a statement expression.
907 Thus, do not give an error here; an error will result later. */
908 return exp;
911 ptrtype = build_pointer_type (restype);
913 if (TREE_CODE (exp) == VAR_DECL)
915 /* ??? This is not really quite correct
916 in that the type of the operand of ADDR_EXPR
917 is not the target type of the type of the ADDR_EXPR itself.
918 Question is, can this lossage be avoided? */
919 adr = build1 (ADDR_EXPR, ptrtype, exp);
920 if (!c_mark_addressable (exp))
921 return error_mark_node;
922 TREE_CONSTANT (adr) = staticp (exp);
923 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
924 return adr;
926 /* This way is better for a COMPONENT_REF since it can
927 simplify the offset for a component. */
928 adr = build_unary_op (ADDR_EXPR, exp, 1);
929 return convert (ptrtype, adr);
931 return exp;
934 /* Perform default promotions for C data used in expressions.
935 Arrays and functions are converted to pointers;
936 enumeral types or short or char, to int.
937 In addition, manifest constants symbols are replaced by their values. */
939 tree
940 default_conversion (exp)
941 tree exp;
943 tree orig_exp;
944 tree type = TREE_TYPE (exp);
945 enum tree_code code = TREE_CODE (type);
947 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
948 return default_function_array_conversion (exp);
950 /* Constants can be used directly unless they're not loadable. */
951 if (TREE_CODE (exp) == CONST_DECL)
952 exp = DECL_INITIAL (exp);
954 /* Replace a nonvolatile const static variable with its value unless
955 it is an array, in which case we must be sure that taking the
956 address of the array produces consistent results. */
957 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
959 exp = decl_constant_value_for_broken_optimization (exp);
960 type = TREE_TYPE (exp);
963 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
964 an lvalue.
966 Do not use STRIP_NOPS here! It will remove conversions from pointer
967 to integer and cause infinite recursion. */
968 orig_exp = exp;
969 while (TREE_CODE (exp) == NON_LVALUE_EXPR
970 || (TREE_CODE (exp) == NOP_EXPR
971 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
972 exp = TREE_OPERAND (exp, 0);
974 /* Preserve the original expression code. */
975 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
976 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
978 /* Normally convert enums to int,
979 but convert wide enums to something wider. */
980 if (code == ENUMERAL_TYPE)
982 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
983 TYPE_PRECISION (integer_type_node)),
984 ((TYPE_PRECISION (type)
985 >= TYPE_PRECISION (integer_type_node))
986 && TREE_UNSIGNED (type)));
988 return convert (type, exp);
991 if (TREE_CODE (exp) == COMPONENT_REF
992 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
993 /* If it's thinner than an int, promote it like a
994 c_promoting_integer_type_p, otherwise leave it alone. */
995 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
996 TYPE_PRECISION (integer_type_node)))
997 return convert (integer_type_node, exp);
999 if (c_promoting_integer_type_p (type))
1001 /* Preserve unsignedness if not really getting any wider. */
1002 if (TREE_UNSIGNED (type)
1003 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1004 return convert (unsigned_type_node, exp);
1006 return convert (integer_type_node, exp);
1009 if (code == VOID_TYPE)
1011 error ("void value not ignored as it ought to be");
1012 return error_mark_node;
1014 return exp;
1017 /* Look up COMPONENT in a structure or union DECL.
1019 If the component name is not found, returns NULL_TREE. Otherwise,
1020 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1021 stepping down the chain to the component, which is in the last
1022 TREE_VALUE of the list. Normally the list is of length one, but if
1023 the component is embedded within (nested) anonymous structures or
1024 unions, the list steps down the chain to the component. */
1026 static tree
1027 lookup_field (decl, component)
1028 tree decl, component;
1030 tree type = TREE_TYPE (decl);
1031 tree field;
1033 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1034 to the field elements. Use a binary search on this array to quickly
1035 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1036 will always be set for structures which have many elements. */
1038 if (TYPE_LANG_SPECIFIC (type))
1040 int bot, top, half;
1041 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1043 field = TYPE_FIELDS (type);
1044 bot = 0;
1045 top = TYPE_LANG_SPECIFIC (type)->len;
1046 while (top - bot > 1)
1048 half = (top - bot + 1) >> 1;
1049 field = field_array[bot+half];
1051 if (DECL_NAME (field) == NULL_TREE)
1053 /* Step through all anon unions in linear fashion. */
1054 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1056 field = field_array[bot++];
1057 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1058 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1060 tree anon = lookup_field (field, component);
1062 if (anon)
1063 return tree_cons (NULL_TREE, field, anon);
1067 /* Entire record is only anon unions. */
1068 if (bot > top)
1069 return NULL_TREE;
1071 /* Restart the binary search, with new lower bound. */
1072 continue;
1075 if (DECL_NAME (field) == component)
1076 break;
1077 if (DECL_NAME (field) < component)
1078 bot += half;
1079 else
1080 top = bot + half;
1083 if (DECL_NAME (field_array[bot]) == component)
1084 field = field_array[bot];
1085 else if (DECL_NAME (field) != component)
1086 return NULL_TREE;
1088 else
1090 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1092 if (DECL_NAME (field) == NULL_TREE
1093 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1094 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1096 tree anon = lookup_field (field, component);
1098 if (anon)
1099 return tree_cons (NULL_TREE, field, anon);
1102 if (DECL_NAME (field) == component)
1103 break;
1106 if (field == NULL_TREE)
1107 return NULL_TREE;
1110 return tree_cons (NULL_TREE, field, NULL_TREE);
1113 /* Make an expression to refer to the COMPONENT field of
1114 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1116 tree
1117 build_component_ref (datum, component)
1118 tree datum, component;
1120 tree type = TREE_TYPE (datum);
1121 enum tree_code code = TREE_CODE (type);
1122 tree field = NULL;
1123 tree ref;
1125 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1126 If pedantic ensure that the arguments are not lvalues; otherwise,
1127 if the component is an array, it would wrongly decay to a pointer in
1128 C89 mode.
1129 We cannot do this with a COND_EXPR, because in a conditional expression
1130 the default promotions are applied to both sides, and this would yield
1131 the wrong type of the result; for example, if the components have
1132 type "char". */
1133 switch (TREE_CODE (datum))
1135 case COMPOUND_EXPR:
1137 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1138 return build (COMPOUND_EXPR, TREE_TYPE (value),
1139 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1141 default:
1142 break;
1145 /* See if there is a field or component with name COMPONENT. */
1147 if (code == RECORD_TYPE || code == UNION_TYPE)
1149 if (!COMPLETE_TYPE_P (type))
1151 c_incomplete_type_error (NULL_TREE, type);
1152 return error_mark_node;
1155 field = lookup_field (datum, component);
1157 if (!field)
1159 error ("%s has no member named `%s'",
1160 code == RECORD_TYPE ? "structure" : "union",
1161 IDENTIFIER_POINTER (component));
1162 return error_mark_node;
1165 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1166 This might be better solved in future the way the C++ front
1167 end does it - by giving the anonymous entities each a
1168 separate name and type, and then have build_component_ref
1169 recursively call itself. We can't do that here. */
1172 tree subdatum = TREE_VALUE (field);
1174 if (TREE_TYPE (subdatum) == error_mark_node)
1175 return error_mark_node;
1177 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1178 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1179 TREE_READONLY (ref) = 1;
1180 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1181 TREE_THIS_VOLATILE (ref) = 1;
1183 if (TREE_DEPRECATED (subdatum))
1184 warn_deprecated_use (subdatum);
1186 datum = ref;
1188 field = TREE_CHAIN (field);
1190 while (field);
1192 return ref;
1194 else if (code != ERROR_MARK)
1195 error ("request for member `%s' in something not a structure or union",
1196 IDENTIFIER_POINTER (component));
1198 return error_mark_node;
1201 /* Given an expression PTR for a pointer, return an expression
1202 for the value pointed to.
1203 ERRORSTRING is the name of the operator to appear in error messages. */
1205 tree
1206 build_indirect_ref (ptr, errorstring)
1207 tree ptr;
1208 const char *errorstring;
1210 tree pointer = default_conversion (ptr);
1211 tree type = TREE_TYPE (pointer);
1213 if (TREE_CODE (type) == POINTER_TYPE)
1215 if (TREE_CODE (pointer) == ADDR_EXPR
1216 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1217 == TREE_TYPE (type)))
1218 return TREE_OPERAND (pointer, 0);
1219 else
1221 tree t = TREE_TYPE (type);
1222 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1224 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1226 error ("dereferencing pointer to incomplete type");
1227 return error_mark_node;
1229 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1230 warning ("dereferencing `void *' pointer");
1232 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1233 so that we get the proper error message if the result is used
1234 to assign to. Also, &* is supposed to be a no-op.
1235 And ANSI C seems to specify that the type of the result
1236 should be the const type. */
1237 /* A de-reference of a pointer to const is not a const. It is valid
1238 to change it via some other pointer. */
1239 TREE_READONLY (ref) = TYPE_READONLY (t);
1240 TREE_SIDE_EFFECTS (ref)
1241 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1242 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1243 return ref;
1246 else if (TREE_CODE (pointer) != ERROR_MARK)
1247 error ("invalid type argument of `%s'", errorstring);
1248 return error_mark_node;
1251 /* This handles expressions of the form "a[i]", which denotes
1252 an array reference.
1254 This is logically equivalent in C to *(a+i), but we may do it differently.
1255 If A is a variable or a member, we generate a primitive ARRAY_REF.
1256 This avoids forcing the array out of registers, and can work on
1257 arrays that are not lvalues (for example, members of structures returned
1258 by functions). */
1260 tree
1261 build_array_ref (array, index)
1262 tree array, index;
1264 if (index == 0)
1266 error ("subscript missing in array reference");
1267 return error_mark_node;
1270 if (TREE_TYPE (array) == error_mark_node
1271 || TREE_TYPE (index) == error_mark_node)
1272 return error_mark_node;
1274 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1275 && TREE_CODE (array) != INDIRECT_REF)
1277 tree rval, type;
1279 /* Subscripting with type char is likely to lose
1280 on a machine where chars are signed.
1281 So warn on any machine, but optionally.
1282 Don't warn for unsigned char since that type is safe.
1283 Don't warn for signed char because anyone who uses that
1284 must have done so deliberately. */
1285 if (warn_char_subscripts
1286 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1287 warning ("array subscript has type `char'");
1289 /* Apply default promotions *after* noticing character types. */
1290 index = default_conversion (index);
1292 /* Require integer *after* promotion, for sake of enums. */
1293 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1295 error ("array subscript is not an integer");
1296 return error_mark_node;
1299 /* An array that is indexed by a non-constant
1300 cannot be stored in a register; we must be able to do
1301 address arithmetic on its address.
1302 Likewise an array of elements of variable size. */
1303 if (TREE_CODE (index) != INTEGER_CST
1304 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1305 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1307 if (!c_mark_addressable (array))
1308 return error_mark_node;
1310 /* An array that is indexed by a constant value which is not within
1311 the array bounds cannot be stored in a register either; because we
1312 would get a crash in store_bit_field/extract_bit_field when trying
1313 to access a non-existent part of the register. */
1314 if (TREE_CODE (index) == INTEGER_CST
1315 && TYPE_VALUES (TREE_TYPE (array))
1316 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1318 if (!c_mark_addressable (array))
1319 return error_mark_node;
1322 if (pedantic)
1324 tree foo = array;
1325 while (TREE_CODE (foo) == COMPONENT_REF)
1326 foo = TREE_OPERAND (foo, 0);
1327 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1328 pedwarn ("ISO C forbids subscripting `register' array");
1329 else if (! flag_isoc99 && ! lvalue_p (foo))
1330 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1333 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1334 rval = build (ARRAY_REF, type, array, index);
1335 /* Array ref is const/volatile if the array elements are
1336 or if the array is. */
1337 TREE_READONLY (rval)
1338 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1339 | TREE_READONLY (array));
1340 TREE_SIDE_EFFECTS (rval)
1341 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1342 | TREE_SIDE_EFFECTS (array));
1343 TREE_THIS_VOLATILE (rval)
1344 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1345 /* This was added by rms on 16 Nov 91.
1346 It fixes vol struct foo *a; a->elts[1]
1347 in an inline function.
1348 Hope it doesn't break something else. */
1349 | TREE_THIS_VOLATILE (array));
1350 return require_complete_type (fold (rval));
1354 tree ar = default_conversion (array);
1355 tree ind = default_conversion (index);
1357 /* Do the same warning check as above, but only on the part that's
1358 syntactically the index and only if it is also semantically
1359 the index. */
1360 if (warn_char_subscripts
1361 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1362 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1363 warning ("subscript has type `char'");
1365 /* Put the integer in IND to simplify error checking. */
1366 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1368 tree temp = ar;
1369 ar = ind;
1370 ind = temp;
1373 if (ar == error_mark_node)
1374 return ar;
1376 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1377 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1379 error ("subscripted value is neither array nor pointer");
1380 return error_mark_node;
1382 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1384 error ("array subscript is not an integer");
1385 return error_mark_node;
1388 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1389 "array indexing");
1393 /* Issue an error message for a reference to an undeclared variable ID,
1394 including a reference to a builtin outside of function-call context.
1395 Arrange to suppress further errors for the same identifier. */
1396 static void
1397 undeclared_variable (id)
1398 tree id;
1400 if (current_function_decl == 0)
1402 error ("`%s' undeclared here (not in a function)",
1403 IDENTIFIER_POINTER (id));
1404 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1406 else
1408 error ("`%s' undeclared (first use in this function)",
1409 IDENTIFIER_POINTER (id));
1411 if (! undeclared_variable_notice)
1413 error ("(Each undeclared identifier is reported only once");
1414 error ("for each function it appears in.)");
1415 undeclared_variable_notice = 1;
1418 /* Set IDENTIFIER_SYMBOL_VALUE (id) to error_mark_node
1419 at function scope. This suppresses further warnings
1420 about this undeclared identifier in this function. */
1421 pushdecl_function_level (error_mark_node, id);
1425 /* Build an external reference to identifier ID. FUN indicates
1426 whether this will be used for a function call. */
1427 tree
1428 build_external_ref (id, fun)
1429 tree id;
1430 int fun;
1432 tree ref;
1433 tree decl = lookup_name (id);
1434 tree objc_ivar = lookup_objc_ivar (id);
1436 if (decl && decl != error_mark_node)
1438 /* Properly declared variable or function reference. */
1439 if (!objc_ivar)
1440 ref = decl;
1441 else if (decl != objc_ivar && DECL_CONTEXT (decl) != 0)
1443 warning ("local declaration of `%s' hides instance variable",
1444 IDENTIFIER_POINTER (id));
1445 ref = decl;
1447 else
1448 ref = objc_ivar;
1450 else if (objc_ivar)
1451 ref = objc_ivar;
1452 else if (fun)
1453 /* Implicit function declaration. */
1454 ref = implicitly_declare (id);
1455 else if (decl == error_mark_node)
1456 /* Don't complain about something that's already been
1457 complained about. */
1458 return error_mark_node;
1459 else
1461 undeclared_variable (id);
1462 return error_mark_node;
1465 if (TREE_TYPE (ref) == error_mark_node)
1466 return error_mark_node;
1468 if (TREE_DEPRECATED (ref))
1469 warn_deprecated_use (ref);
1471 if (!skip_evaluation)
1472 assemble_external (ref);
1473 TREE_USED (ref) = 1;
1475 if (TREE_CODE (ref) == CONST_DECL)
1477 ref = DECL_INITIAL (ref);
1478 TREE_CONSTANT (ref) = 1;
1480 else if (current_function_decl != 0
1481 && DECL_CONTEXT (current_function_decl) != 0
1482 && (TREE_CODE (ref) == VAR_DECL
1483 || TREE_CODE (ref) == PARM_DECL
1484 || TREE_CODE (ref) == FUNCTION_DECL))
1486 tree context = decl_function_context (ref);
1488 if (context != 0 && context != current_function_decl)
1489 DECL_NONLOCAL (ref) = 1;
1492 return ref;
1495 /* Build a function call to function FUNCTION with parameters PARAMS.
1496 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1497 TREE_VALUE of each node is a parameter-expression.
1498 FUNCTION's data type may be a function type or a pointer-to-function. */
1500 tree
1501 build_function_call (function, params)
1502 tree function, params;
1504 tree fntype, fundecl = 0;
1505 tree coerced_params;
1506 tree name = NULL_TREE, result;
1508 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1509 STRIP_TYPE_NOPS (function);
1511 /* Convert anything with function type to a pointer-to-function. */
1512 if (TREE_CODE (function) == FUNCTION_DECL)
1514 name = DECL_NAME (function);
1516 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1517 (because calling an inline function does not mean the function
1518 needs to be separately compiled). */
1519 fntype = build_type_variant (TREE_TYPE (function),
1520 TREE_READONLY (function),
1521 TREE_THIS_VOLATILE (function));
1522 fundecl = function;
1523 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1525 else
1526 function = default_conversion (function);
1528 fntype = TREE_TYPE (function);
1530 if (TREE_CODE (fntype) == ERROR_MARK)
1531 return error_mark_node;
1533 if (!(TREE_CODE (fntype) == POINTER_TYPE
1534 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1536 error ("called object is not a function");
1537 return error_mark_node;
1540 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1541 current_function_returns_abnormally = 1;
1543 /* fntype now gets the type of function pointed to. */
1544 fntype = TREE_TYPE (fntype);
1546 /* Convert the parameters to the types declared in the
1547 function prototype, or apply default promotions. */
1549 coerced_params
1550 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1552 /* Check that the arguments to the function are valid. */
1554 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1556 /* Recognize certain built-in functions so we can make tree-codes
1557 other than CALL_EXPR. We do this when it enables fold-const.c
1558 to do something useful. */
1560 if (TREE_CODE (function) == ADDR_EXPR
1561 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1562 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1564 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1565 params, coerced_params);
1566 if (result)
1567 return result;
1570 result = build (CALL_EXPR, TREE_TYPE (fntype),
1571 function, coerced_params, NULL_TREE);
1572 TREE_SIDE_EFFECTS (result) = 1;
1573 result = fold (result);
1575 if (VOID_TYPE_P (TREE_TYPE (result)))
1576 return result;
1577 return require_complete_type (result);
1580 /* Convert the argument expressions in the list VALUES
1581 to the types in the list TYPELIST. The result is a list of converted
1582 argument expressions.
1584 If TYPELIST is exhausted, or when an element has NULL as its type,
1585 perform the default conversions.
1587 PARMLIST is the chain of parm decls for the function being called.
1588 It may be 0, if that info is not available.
1589 It is used only for generating error messages.
1591 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1593 This is also where warnings about wrong number of args are generated.
1595 Both VALUES and the returned value are chains of TREE_LIST nodes
1596 with the elements of the list in the TREE_VALUE slots of those nodes. */
1598 static tree
1599 convert_arguments (typelist, values, name, fundecl)
1600 tree typelist, values, name, fundecl;
1602 tree typetail, valtail;
1603 tree result = NULL;
1604 int parmnum;
1606 /* Scan the given expressions and types, producing individual
1607 converted arguments and pushing them on RESULT in reverse order. */
1609 for (valtail = values, typetail = typelist, parmnum = 0;
1610 valtail;
1611 valtail = TREE_CHAIN (valtail), parmnum++)
1613 tree type = typetail ? TREE_VALUE (typetail) : 0;
1614 tree val = TREE_VALUE (valtail);
1616 if (type == void_type_node)
1618 if (name)
1619 error ("too many arguments to function `%s'",
1620 IDENTIFIER_POINTER (name));
1621 else
1622 error ("too many arguments to function");
1623 break;
1626 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1627 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1628 to convert automatically to a pointer. */
1629 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1630 val = TREE_OPERAND (val, 0);
1632 val = default_function_array_conversion (val);
1634 val = require_complete_type (val);
1636 if (type != 0)
1638 /* Formal parm type is specified by a function prototype. */
1639 tree parmval;
1641 if (!COMPLETE_TYPE_P (type))
1643 error ("type of formal parameter %d is incomplete", parmnum + 1);
1644 parmval = val;
1646 else
1648 /* Optionally warn about conversions that
1649 differ from the default conversions. */
1650 if (warn_conversion || warn_traditional)
1652 int formal_prec = TYPE_PRECISION (type);
1654 if (INTEGRAL_TYPE_P (type)
1655 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1656 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1657 if (INTEGRAL_TYPE_P (type)
1658 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1659 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1660 else if (TREE_CODE (type) == COMPLEX_TYPE
1661 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1662 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1663 else if (TREE_CODE (type) == REAL_TYPE
1664 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1665 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1666 else if (TREE_CODE (type) == COMPLEX_TYPE
1667 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1668 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1669 else if (TREE_CODE (type) == REAL_TYPE
1670 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1671 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1672 /* ??? At some point, messages should be written about
1673 conversions between complex types, but that's too messy
1674 to do now. */
1675 else if (TREE_CODE (type) == REAL_TYPE
1676 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1678 /* Warn if any argument is passed as `float',
1679 since without a prototype it would be `double'. */
1680 if (formal_prec == TYPE_PRECISION (float_type_node))
1681 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1683 /* Detect integer changing in width or signedness.
1684 These warnings are only activated with
1685 -Wconversion, not with -Wtraditional. */
1686 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1687 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1689 tree would_have_been = default_conversion (val);
1690 tree type1 = TREE_TYPE (would_have_been);
1692 if (TREE_CODE (type) == ENUMERAL_TYPE
1693 && (TYPE_MAIN_VARIANT (type)
1694 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1695 /* No warning if function asks for enum
1696 and the actual arg is that enum type. */
1698 else if (formal_prec != TYPE_PRECISION (type1))
1699 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1700 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1702 /* Don't complain if the formal parameter type
1703 is an enum, because we can't tell now whether
1704 the value was an enum--even the same enum. */
1705 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1707 else if (TREE_CODE (val) == INTEGER_CST
1708 && int_fits_type_p (val, type))
1709 /* Change in signedness doesn't matter
1710 if a constant value is unaffected. */
1712 /* Likewise for a constant in a NOP_EXPR. */
1713 else if (TREE_CODE (val) == NOP_EXPR
1714 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1715 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1717 #if 0 /* We never get such tree structure here. */
1718 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1719 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1720 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1721 /* Change in signedness doesn't matter
1722 if an enum value is unaffected. */
1724 #endif
1725 /* If the value is extended from a narrower
1726 unsigned type, it doesn't matter whether we
1727 pass it as signed or unsigned; the value
1728 certainly is the same either way. */
1729 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1730 && TREE_UNSIGNED (TREE_TYPE (val)))
1732 else if (TREE_UNSIGNED (type))
1733 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1734 else
1735 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1739 parmval = convert_for_assignment (type, val,
1740 (char *) 0, /* arg passing */
1741 fundecl, name, parmnum + 1);
1743 if (PROMOTE_PROTOTYPES
1744 && INTEGRAL_TYPE_P (type)
1745 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1746 parmval = default_conversion (parmval);
1748 result = tree_cons (NULL_TREE, parmval, result);
1750 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1751 && (TYPE_PRECISION (TREE_TYPE (val))
1752 < TYPE_PRECISION (double_type_node)))
1753 /* Convert `float' to `double'. */
1754 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1755 else
1756 /* Convert `short' and `char' to full-size `int'. */
1757 result = tree_cons (NULL_TREE, default_conversion (val), result);
1759 if (typetail)
1760 typetail = TREE_CHAIN (typetail);
1763 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1765 if (name)
1766 error ("too few arguments to function `%s'",
1767 IDENTIFIER_POINTER (name));
1768 else
1769 error ("too few arguments to function");
1772 return nreverse (result);
1775 /* This is the entry point used by the parser
1776 for binary operators in the input.
1777 In addition to constructing the expression,
1778 we check for operands that were written with other binary operators
1779 in a way that is likely to confuse the user. */
1781 tree
1782 parser_build_binary_op (code, arg1, arg2)
1783 enum tree_code code;
1784 tree arg1, arg2;
1786 tree result = build_binary_op (code, arg1, arg2, 1);
1788 char class;
1789 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1790 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1791 enum tree_code code1 = ERROR_MARK;
1792 enum tree_code code2 = ERROR_MARK;
1794 if (TREE_CODE (result) == ERROR_MARK)
1795 return error_mark_node;
1797 if (IS_EXPR_CODE_CLASS (class1))
1798 code1 = C_EXP_ORIGINAL_CODE (arg1);
1799 if (IS_EXPR_CODE_CLASS (class2))
1800 code2 = C_EXP_ORIGINAL_CODE (arg2);
1802 /* Check for cases such as x+y<<z which users are likely
1803 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1804 is cleared to prevent these warnings. */
1805 if (warn_parentheses)
1807 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1809 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1810 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1811 warning ("suggest parentheses around + or - inside shift");
1814 if (code == TRUTH_ORIF_EXPR)
1816 if (code1 == TRUTH_ANDIF_EXPR
1817 || code2 == TRUTH_ANDIF_EXPR)
1818 warning ("suggest parentheses around && within ||");
1821 if (code == BIT_IOR_EXPR)
1823 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1824 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1825 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1826 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1827 warning ("suggest parentheses around arithmetic in operand of |");
1828 /* Check cases like x|y==z */
1829 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1830 warning ("suggest parentheses around comparison in operand of |");
1833 if (code == BIT_XOR_EXPR)
1835 if (code1 == BIT_AND_EXPR
1836 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1837 || code2 == BIT_AND_EXPR
1838 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1839 warning ("suggest parentheses around arithmetic in operand of ^");
1840 /* Check cases like x^y==z */
1841 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1842 warning ("suggest parentheses around comparison in operand of ^");
1845 if (code == BIT_AND_EXPR)
1847 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1848 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1849 warning ("suggest parentheses around + or - in operand of &");
1850 /* Check cases like x&y==z */
1851 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1852 warning ("suggest parentheses around comparison in operand of &");
1856 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1857 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1858 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1859 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1861 unsigned_conversion_warning (result, arg1);
1862 unsigned_conversion_warning (result, arg2);
1863 overflow_warning (result);
1865 class = TREE_CODE_CLASS (TREE_CODE (result));
1867 /* Record the code that was specified in the source,
1868 for the sake of warnings about confusing nesting. */
1869 if (IS_EXPR_CODE_CLASS (class))
1870 C_SET_EXP_ORIGINAL_CODE (result, code);
1871 else
1873 int flag = TREE_CONSTANT (result);
1874 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1875 so that convert_for_assignment wouldn't strip it.
1876 That way, we got warnings for things like p = (1 - 1).
1877 But it turns out we should not get those warnings. */
1878 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1879 C_SET_EXP_ORIGINAL_CODE (result, code);
1880 TREE_CONSTANT (result) = flag;
1883 return result;
1886 /* Build a binary-operation expression without default conversions.
1887 CODE is the kind of expression to build.
1888 This function differs from `build' in several ways:
1889 the data type of the result is computed and recorded in it,
1890 warnings are generated if arg data types are invalid,
1891 special handling for addition and subtraction of pointers is known,
1892 and some optimization is done (operations on narrow ints
1893 are done in the narrower type when that gives the same result).
1894 Constant folding is also done before the result is returned.
1896 Note that the operands will never have enumeral types, or function
1897 or array types, because either they will have the default conversions
1898 performed or they have both just been converted to some other type in which
1899 the arithmetic is to be done. */
1901 tree
1902 build_binary_op (code, orig_op0, orig_op1, convert_p)
1903 enum tree_code code;
1904 tree orig_op0, orig_op1;
1905 int convert_p;
1907 tree type0, type1;
1908 enum tree_code code0, code1;
1909 tree op0, op1;
1911 /* Expression code to give to the expression when it is built.
1912 Normally this is CODE, which is what the caller asked for,
1913 but in some special cases we change it. */
1914 enum tree_code resultcode = code;
1916 /* Data type in which the computation is to be performed.
1917 In the simplest cases this is the common type of the arguments. */
1918 tree result_type = NULL;
1920 /* Nonzero means operands have already been type-converted
1921 in whatever way is necessary.
1922 Zero means they need to be converted to RESULT_TYPE. */
1923 int converted = 0;
1925 /* Nonzero means create the expression with this type, rather than
1926 RESULT_TYPE. */
1927 tree build_type = 0;
1929 /* Nonzero means after finally constructing the expression
1930 convert it to this type. */
1931 tree final_type = 0;
1933 /* Nonzero if this is an operation like MIN or MAX which can
1934 safely be computed in short if both args are promoted shorts.
1935 Also implies COMMON.
1936 -1 indicates a bitwise operation; this makes a difference
1937 in the exact conditions for when it is safe to do the operation
1938 in a narrower mode. */
1939 int shorten = 0;
1941 /* Nonzero if this is a comparison operation;
1942 if both args are promoted shorts, compare the original shorts.
1943 Also implies COMMON. */
1944 int short_compare = 0;
1946 /* Nonzero if this is a right-shift operation, which can be computed on the
1947 original short and then promoted if the operand is a promoted short. */
1948 int short_shift = 0;
1950 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1951 int common = 0;
1953 if (convert_p)
1955 op0 = default_conversion (orig_op0);
1956 op1 = default_conversion (orig_op1);
1958 else
1960 op0 = orig_op0;
1961 op1 = orig_op1;
1964 type0 = TREE_TYPE (op0);
1965 type1 = TREE_TYPE (op1);
1967 /* The expression codes of the data types of the arguments tell us
1968 whether the arguments are integers, floating, pointers, etc. */
1969 code0 = TREE_CODE (type0);
1970 code1 = TREE_CODE (type1);
1972 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1973 STRIP_TYPE_NOPS (op0);
1974 STRIP_TYPE_NOPS (op1);
1976 /* If an error was already reported for one of the arguments,
1977 avoid reporting another error. */
1979 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1980 return error_mark_node;
1982 switch (code)
1984 case PLUS_EXPR:
1985 /* Handle the pointer + int case. */
1986 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1987 return pointer_int_sum (PLUS_EXPR, op0, op1);
1988 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1989 return pointer_int_sum (PLUS_EXPR, op1, op0);
1990 else
1991 common = 1;
1992 break;
1994 case MINUS_EXPR:
1995 /* Subtraction of two similar pointers.
1996 We must subtract them as integers, then divide by object size. */
1997 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1998 && comp_target_types (type0, type1, 1))
1999 return pointer_diff (op0, op1);
2000 /* Handle pointer minus int. Just like pointer plus int. */
2001 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2002 return pointer_int_sum (MINUS_EXPR, op0, op1);
2003 else
2004 common = 1;
2005 break;
2007 case MULT_EXPR:
2008 common = 1;
2009 break;
2011 case TRUNC_DIV_EXPR:
2012 case CEIL_DIV_EXPR:
2013 case FLOOR_DIV_EXPR:
2014 case ROUND_DIV_EXPR:
2015 case EXACT_DIV_EXPR:
2016 /* Floating point division by zero is a legitimate way to obtain
2017 infinities and NaNs. */
2018 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2019 warning ("division by zero");
2021 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2022 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2023 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2024 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2026 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2027 resultcode = RDIV_EXPR;
2028 else
2029 /* Although it would be tempting to shorten always here, that
2030 loses on some targets, since the modulo instruction is
2031 undefined if the quotient can't be represented in the
2032 computation mode. We shorten only if unsigned or if
2033 dividing by something we know != -1. */
2034 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2035 || (TREE_CODE (op1) == INTEGER_CST
2036 && ! integer_all_onesp (op1)));
2037 common = 1;
2039 break;
2041 case BIT_AND_EXPR:
2042 case BIT_ANDTC_EXPR:
2043 case BIT_IOR_EXPR:
2044 case BIT_XOR_EXPR:
2045 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2046 shorten = -1;
2047 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2048 common = 1;
2049 break;
2051 case TRUNC_MOD_EXPR:
2052 case FLOOR_MOD_EXPR:
2053 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2054 warning ("division by zero");
2056 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2058 /* Although it would be tempting to shorten always here, that loses
2059 on some targets, since the modulo instruction is undefined if the
2060 quotient can't be represented in the computation mode. We shorten
2061 only if unsigned or if dividing by something we know != -1. */
2062 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2063 || (TREE_CODE (op1) == INTEGER_CST
2064 && ! integer_all_onesp (op1)));
2065 common = 1;
2067 break;
2069 case TRUTH_ANDIF_EXPR:
2070 case TRUTH_ORIF_EXPR:
2071 case TRUTH_AND_EXPR:
2072 case TRUTH_OR_EXPR:
2073 case TRUTH_XOR_EXPR:
2074 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2075 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2076 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2077 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2079 /* Result of these operations is always an int,
2080 but that does not mean the operands should be
2081 converted to ints! */
2082 result_type = integer_type_node;
2083 op0 = c_common_truthvalue_conversion (op0);
2084 op1 = c_common_truthvalue_conversion (op1);
2085 converted = 1;
2087 break;
2089 /* Shift operations: result has same type as first operand;
2090 always convert second operand to int.
2091 Also set SHORT_SHIFT if shifting rightward. */
2093 case RSHIFT_EXPR:
2094 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2096 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2098 if (tree_int_cst_sgn (op1) < 0)
2099 warning ("right shift count is negative");
2100 else
2102 if (! integer_zerop (op1))
2103 short_shift = 1;
2105 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2106 warning ("right shift count >= width of type");
2110 /* Use the type of the value to be shifted. */
2111 result_type = type0;
2112 /* Convert the shift-count to an integer, regardless of size
2113 of value being shifted. */
2114 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2115 op1 = convert (integer_type_node, op1);
2116 /* Avoid converting op1 to result_type later. */
2117 converted = 1;
2119 break;
2121 case LSHIFT_EXPR:
2122 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2124 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2126 if (tree_int_cst_sgn (op1) < 0)
2127 warning ("left shift count is negative");
2129 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2130 warning ("left shift count >= width of type");
2133 /* Use the type of the value to be shifted. */
2134 result_type = type0;
2135 /* Convert the shift-count to an integer, regardless of size
2136 of value being shifted. */
2137 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2138 op1 = convert (integer_type_node, op1);
2139 /* Avoid converting op1 to result_type later. */
2140 converted = 1;
2142 break;
2144 case RROTATE_EXPR:
2145 case LROTATE_EXPR:
2146 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2148 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2150 if (tree_int_cst_sgn (op1) < 0)
2151 warning ("shift count is negative");
2152 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2153 warning ("shift count >= width of type");
2156 /* Use the type of the value to be shifted. */
2157 result_type = type0;
2158 /* Convert the shift-count to an integer, regardless of size
2159 of value being shifted. */
2160 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2161 op1 = convert (integer_type_node, op1);
2162 /* Avoid converting op1 to result_type later. */
2163 converted = 1;
2165 break;
2167 case EQ_EXPR:
2168 case NE_EXPR:
2169 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2170 warning ("comparing floating point with == or != is unsafe");
2171 /* Result of comparison is always int,
2172 but don't convert the args to int! */
2173 build_type = integer_type_node;
2174 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2175 || code0 == COMPLEX_TYPE
2176 || code0 == VECTOR_TYPE)
2177 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2178 || code1 == COMPLEX_TYPE
2179 || code1 == VECTOR_TYPE))
2180 short_compare = 1;
2181 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2183 tree tt0 = TREE_TYPE (type0);
2184 tree tt1 = TREE_TYPE (type1);
2185 /* Anything compares with void *. void * compares with anything.
2186 Otherwise, the targets must be compatible
2187 and both must be object or both incomplete. */
2188 if (comp_target_types (type0, type1, 1))
2189 result_type = common_type (type0, type1);
2190 else if (VOID_TYPE_P (tt0))
2192 /* op0 != orig_op0 detects the case of something
2193 whose value is 0 but which isn't a valid null ptr const. */
2194 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2195 && TREE_CODE (tt1) == FUNCTION_TYPE)
2196 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2198 else if (VOID_TYPE_P (tt1))
2200 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2201 && TREE_CODE (tt0) == FUNCTION_TYPE)
2202 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2204 else
2205 pedwarn ("comparison of distinct pointer types lacks a cast");
2207 if (result_type == NULL_TREE)
2208 result_type = ptr_type_node;
2210 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2211 && integer_zerop (op1))
2212 result_type = type0;
2213 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2214 && integer_zerop (op0))
2215 result_type = type1;
2216 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2218 result_type = type0;
2219 pedwarn ("comparison between pointer and integer");
2221 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2223 result_type = type1;
2224 pedwarn ("comparison between pointer and integer");
2226 break;
2228 case MAX_EXPR:
2229 case MIN_EXPR:
2230 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2231 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2232 shorten = 1;
2233 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2235 if (comp_target_types (type0, type1, 1))
2237 result_type = common_type (type0, type1);
2238 if (pedantic
2239 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2240 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2242 else
2244 result_type = ptr_type_node;
2245 pedwarn ("comparison of distinct pointer types lacks a cast");
2248 break;
2250 case LE_EXPR:
2251 case GE_EXPR:
2252 case LT_EXPR:
2253 case GT_EXPR:
2254 build_type = integer_type_node;
2255 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2256 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2257 short_compare = 1;
2258 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2260 if (comp_target_types (type0, type1, 1))
2262 result_type = common_type (type0, type1);
2263 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2264 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2265 pedwarn ("comparison of complete and incomplete pointers");
2266 else if (pedantic
2267 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2268 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2270 else
2272 result_type = ptr_type_node;
2273 pedwarn ("comparison of distinct pointer types lacks a cast");
2276 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2277 && integer_zerop (op1))
2279 result_type = type0;
2280 if (pedantic || extra_warnings)
2281 pedwarn ("ordered comparison of pointer with integer zero");
2283 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2284 && integer_zerop (op0))
2286 result_type = type1;
2287 if (pedantic)
2288 pedwarn ("ordered comparison of pointer with integer zero");
2290 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2292 result_type = type0;
2293 pedwarn ("comparison between pointer and integer");
2295 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2297 result_type = type1;
2298 pedwarn ("comparison between pointer and integer");
2300 break;
2302 case UNORDERED_EXPR:
2303 case ORDERED_EXPR:
2304 case UNLT_EXPR:
2305 case UNLE_EXPR:
2306 case UNGT_EXPR:
2307 case UNGE_EXPR:
2308 case UNEQ_EXPR:
2309 build_type = integer_type_node;
2310 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2312 error ("unordered comparison on non-floating point argument");
2313 return error_mark_node;
2315 common = 1;
2316 break;
2318 default:
2319 break;
2322 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2323 || code0 == VECTOR_TYPE)
2325 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2326 || code1 == VECTOR_TYPE))
2328 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2330 if (shorten || common || short_compare)
2331 result_type = common_type (type0, type1);
2333 /* For certain operations (which identify themselves by shorten != 0)
2334 if both args were extended from the same smaller type,
2335 do the arithmetic in that type and then extend.
2337 shorten !=0 and !=1 indicates a bitwise operation.
2338 For them, this optimization is safe only if
2339 both args are zero-extended or both are sign-extended.
2340 Otherwise, we might change the result.
2341 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2342 but calculated in (unsigned short) it would be (unsigned short)-1. */
2344 if (shorten && none_complex)
2346 int unsigned0, unsigned1;
2347 tree arg0 = get_narrower (op0, &unsigned0);
2348 tree arg1 = get_narrower (op1, &unsigned1);
2349 /* UNS is 1 if the operation to be done is an unsigned one. */
2350 int uns = TREE_UNSIGNED (result_type);
2351 tree type;
2353 final_type = result_type;
2355 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2356 but it *requires* conversion to FINAL_TYPE. */
2358 if ((TYPE_PRECISION (TREE_TYPE (op0))
2359 == TYPE_PRECISION (TREE_TYPE (arg0)))
2360 && TREE_TYPE (op0) != final_type)
2361 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2362 if ((TYPE_PRECISION (TREE_TYPE (op1))
2363 == TYPE_PRECISION (TREE_TYPE (arg1)))
2364 && TREE_TYPE (op1) != final_type)
2365 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2367 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2369 /* For bitwise operations, signedness of nominal type
2370 does not matter. Consider only how operands were extended. */
2371 if (shorten == -1)
2372 uns = unsigned0;
2374 /* Note that in all three cases below we refrain from optimizing
2375 an unsigned operation on sign-extended args.
2376 That would not be valid. */
2378 /* Both args variable: if both extended in same way
2379 from same width, do it in that width.
2380 Do it unsigned if args were zero-extended. */
2381 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2382 < TYPE_PRECISION (result_type))
2383 && (TYPE_PRECISION (TREE_TYPE (arg1))
2384 == TYPE_PRECISION (TREE_TYPE (arg0)))
2385 && unsigned0 == unsigned1
2386 && (unsigned0 || !uns))
2387 result_type
2388 = c_common_signed_or_unsigned_type
2389 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2390 else if (TREE_CODE (arg0) == INTEGER_CST
2391 && (unsigned1 || !uns)
2392 && (TYPE_PRECISION (TREE_TYPE (arg1))
2393 < TYPE_PRECISION (result_type))
2394 && (type
2395 = c_common_signed_or_unsigned_type (unsigned1,
2396 TREE_TYPE (arg1)),
2397 int_fits_type_p (arg0, type)))
2398 result_type = type;
2399 else if (TREE_CODE (arg1) == INTEGER_CST
2400 && (unsigned0 || !uns)
2401 && (TYPE_PRECISION (TREE_TYPE (arg0))
2402 < TYPE_PRECISION (result_type))
2403 && (type
2404 = c_common_signed_or_unsigned_type (unsigned0,
2405 TREE_TYPE (arg0)),
2406 int_fits_type_p (arg1, type)))
2407 result_type = type;
2410 /* Shifts can be shortened if shifting right. */
2412 if (short_shift)
2414 int unsigned_arg;
2415 tree arg0 = get_narrower (op0, &unsigned_arg);
2417 final_type = result_type;
2419 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2420 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2422 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2423 /* We can shorten only if the shift count is less than the
2424 number of bits in the smaller type size. */
2425 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2426 /* We cannot drop an unsigned shift after sign-extension. */
2427 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2429 /* Do an unsigned shift if the operand was zero-extended. */
2430 result_type
2431 = c_common_signed_or_unsigned_type (unsigned_arg,
2432 TREE_TYPE (arg0));
2433 /* Convert value-to-be-shifted to that type. */
2434 if (TREE_TYPE (op0) != result_type)
2435 op0 = convert (result_type, op0);
2436 converted = 1;
2440 /* Comparison operations are shortened too but differently.
2441 They identify themselves by setting short_compare = 1. */
2443 if (short_compare)
2445 /* Don't write &op0, etc., because that would prevent op0
2446 from being kept in a register.
2447 Instead, make copies of the our local variables and
2448 pass the copies by reference, then copy them back afterward. */
2449 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2450 enum tree_code xresultcode = resultcode;
2451 tree val
2452 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2454 if (val != 0)
2455 return val;
2457 op0 = xop0, op1 = xop1;
2458 converted = 1;
2459 resultcode = xresultcode;
2461 if (warn_sign_compare && skip_evaluation == 0)
2463 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2464 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2465 int unsignedp0, unsignedp1;
2466 tree primop0 = get_narrower (op0, &unsignedp0);
2467 tree primop1 = get_narrower (op1, &unsignedp1);
2469 xop0 = orig_op0;
2470 xop1 = orig_op1;
2471 STRIP_TYPE_NOPS (xop0);
2472 STRIP_TYPE_NOPS (xop1);
2474 /* Give warnings for comparisons between signed and unsigned
2475 quantities that may fail.
2477 Do the checking based on the original operand trees, so that
2478 casts will be considered, but default promotions won't be.
2480 Do not warn if the comparison is being done in a signed type,
2481 since the signed type will only be chosen if it can represent
2482 all the values of the unsigned type. */
2483 if (! TREE_UNSIGNED (result_type))
2484 /* OK */;
2485 /* Do not warn if both operands are the same signedness. */
2486 else if (op0_signed == op1_signed)
2487 /* OK */;
2488 else
2490 tree sop, uop;
2492 if (op0_signed)
2493 sop = xop0, uop = xop1;
2494 else
2495 sop = xop1, uop = xop0;
2497 /* Do not warn if the signed quantity is an
2498 unsuffixed integer literal (or some static
2499 constant expression involving such literals or a
2500 conditional expression involving such literals)
2501 and it is non-negative. */
2502 if (c_tree_expr_nonnegative_p (sop))
2503 /* OK */;
2504 /* Do not warn if the comparison is an equality operation,
2505 the unsigned quantity is an integral constant, and it
2506 would fit in the result if the result were signed. */
2507 else if (TREE_CODE (uop) == INTEGER_CST
2508 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2509 && int_fits_type_p
2510 (uop, c_common_signed_type (result_type)))
2511 /* OK */;
2512 /* Do not warn if the unsigned quantity is an enumeration
2513 constant and its maximum value would fit in the result
2514 if the result were signed. */
2515 else if (TREE_CODE (uop) == INTEGER_CST
2516 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2517 && int_fits_type_p
2518 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2519 c_common_signed_type (result_type)))
2520 /* OK */;
2521 else
2522 warning ("comparison between signed and unsigned");
2525 /* Warn if two unsigned values are being compared in a size
2526 larger than their original size, and one (and only one) is the
2527 result of a `~' operator. This comparison will always fail.
2529 Also warn if one operand is a constant, and the constant
2530 does not have all bits set that are set in the ~ operand
2531 when it is extended. */
2533 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2534 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2536 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2537 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2538 &unsignedp0);
2539 else
2540 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2541 &unsignedp1);
2543 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2545 tree primop;
2546 HOST_WIDE_INT constant, mask;
2547 int unsignedp, bits;
2549 if (host_integerp (primop0, 0))
2551 primop = primop1;
2552 unsignedp = unsignedp1;
2553 constant = tree_low_cst (primop0, 0);
2555 else
2557 primop = primop0;
2558 unsignedp = unsignedp0;
2559 constant = tree_low_cst (primop1, 0);
2562 bits = TYPE_PRECISION (TREE_TYPE (primop));
2563 if (bits < TYPE_PRECISION (result_type)
2564 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2566 mask = (~ (HOST_WIDE_INT) 0) << bits;
2567 if ((mask & constant) != mask)
2568 warning ("comparison of promoted ~unsigned with constant");
2571 else if (unsignedp0 && unsignedp1
2572 && (TYPE_PRECISION (TREE_TYPE (primop0))
2573 < TYPE_PRECISION (result_type))
2574 && (TYPE_PRECISION (TREE_TYPE (primop1))
2575 < TYPE_PRECISION (result_type)))
2576 warning ("comparison of promoted ~unsigned with unsigned");
2582 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2583 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2584 Then the expression will be built.
2585 It will be given type FINAL_TYPE if that is nonzero;
2586 otherwise, it will be given type RESULT_TYPE. */
2588 if (!result_type)
2590 binary_op_error (code);
2591 return error_mark_node;
2594 if (! converted)
2596 if (TREE_TYPE (op0) != result_type)
2597 op0 = convert (result_type, op0);
2598 if (TREE_TYPE (op1) != result_type)
2599 op1 = convert (result_type, op1);
2602 if (build_type == NULL_TREE)
2603 build_type = result_type;
2606 tree result = build (resultcode, build_type, op0, op1);
2607 tree folded;
2609 folded = fold (result);
2610 if (folded == result)
2611 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2612 if (final_type != 0)
2613 return convert (final_type, folded);
2614 return folded;
2619 /* Return true if `t' is known to be non-negative. */
2622 c_tree_expr_nonnegative_p (t)
2623 tree t;
2625 if (TREE_CODE (t) == STMT_EXPR)
2627 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2629 /* Find the last statement in the chain, ignoring the final
2630 * scope statement */
2631 while (TREE_CHAIN (t) != NULL_TREE
2632 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2633 t = TREE_CHAIN (t);
2634 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2636 return tree_expr_nonnegative_p (t);
2639 /* Return a tree for the difference of pointers OP0 and OP1.
2640 The resulting tree has type int. */
2642 static tree
2643 pointer_diff (op0, op1)
2644 tree op0, op1;
2646 tree result, folded;
2647 tree restype = ptrdiff_type_node;
2649 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2650 tree con0, con1, lit0, lit1;
2651 tree orig_op1 = op1;
2653 if (pedantic || warn_pointer_arith)
2655 if (TREE_CODE (target_type) == VOID_TYPE)
2656 pedwarn ("pointer of type `void *' used in subtraction");
2657 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2658 pedwarn ("pointer to a function used in subtraction");
2661 /* If the conversion to ptrdiff_type does anything like widening or
2662 converting a partial to an integral mode, we get a convert_expression
2663 that is in the way to do any simplifications.
2664 (fold-const.c doesn't know that the extra bits won't be needed.
2665 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2666 different mode in place.)
2667 So first try to find a common term here 'by hand'; we want to cover
2668 at least the cases that occur in legal static initializers. */
2669 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2670 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2672 if (TREE_CODE (con0) == PLUS_EXPR)
2674 lit0 = TREE_OPERAND (con0, 1);
2675 con0 = TREE_OPERAND (con0, 0);
2677 else
2678 lit0 = integer_zero_node;
2680 if (TREE_CODE (con1) == PLUS_EXPR)
2682 lit1 = TREE_OPERAND (con1, 1);
2683 con1 = TREE_OPERAND (con1, 0);
2685 else
2686 lit1 = integer_zero_node;
2688 if (operand_equal_p (con0, con1, 0))
2690 op0 = lit0;
2691 op1 = lit1;
2695 /* First do the subtraction as integers;
2696 then drop through to build the divide operator.
2697 Do not do default conversions on the minus operator
2698 in case restype is a short type. */
2700 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2701 convert (restype, op1), 0);
2702 /* This generates an error if op1 is pointer to incomplete type. */
2703 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2704 error ("arithmetic on pointer to an incomplete type");
2706 /* This generates an error if op0 is pointer to incomplete type. */
2707 op1 = c_size_in_bytes (target_type);
2709 /* Divide by the size, in easiest possible way. */
2711 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2713 folded = fold (result);
2714 if (folded == result)
2715 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2716 return folded;
2719 /* Construct and perhaps optimize a tree representation
2720 for a unary operation. CODE, a tree_code, specifies the operation
2721 and XARG is the operand.
2722 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2723 the default promotions (such as from short to int).
2724 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2725 allows non-lvalues; this is only used to handle conversion of non-lvalue
2726 arrays to pointers in C99. */
2728 tree
2729 build_unary_op (code, xarg, flag)
2730 enum tree_code code;
2731 tree xarg;
2732 int flag;
2734 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2735 tree arg = xarg;
2736 tree argtype = 0;
2737 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2738 tree val;
2739 int noconvert = flag;
2741 if (typecode == ERROR_MARK)
2742 return error_mark_node;
2743 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2744 typecode = INTEGER_TYPE;
2746 switch (code)
2748 case CONVERT_EXPR:
2749 /* This is used for unary plus, because a CONVERT_EXPR
2750 is enough to prevent anybody from looking inside for
2751 associativity, but won't generate any code. */
2752 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2753 || typecode == COMPLEX_TYPE))
2755 error ("wrong type argument to unary plus");
2756 return error_mark_node;
2758 else if (!noconvert)
2759 arg = default_conversion (arg);
2760 arg = non_lvalue (arg);
2761 break;
2763 case NEGATE_EXPR:
2764 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2765 || typecode == COMPLEX_TYPE
2766 || typecode == VECTOR_TYPE))
2768 error ("wrong type argument to unary minus");
2769 return error_mark_node;
2771 else if (!noconvert)
2772 arg = default_conversion (arg);
2773 break;
2775 case BIT_NOT_EXPR:
2776 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2778 if (!noconvert)
2779 arg = default_conversion (arg);
2781 else if (typecode == COMPLEX_TYPE)
2783 code = CONJ_EXPR;
2784 if (pedantic)
2785 pedwarn ("ISO C does not support `~' for complex conjugation");
2786 if (!noconvert)
2787 arg = default_conversion (arg);
2789 else
2791 error ("wrong type argument to bit-complement");
2792 return error_mark_node;
2794 break;
2796 case ABS_EXPR:
2797 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2798 || typecode == COMPLEX_TYPE))
2800 error ("wrong type argument to abs");
2801 return error_mark_node;
2803 else if (!noconvert)
2804 arg = default_conversion (arg);
2805 break;
2807 case CONJ_EXPR:
2808 /* Conjugating a real value is a no-op, but allow it anyway. */
2809 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2810 || typecode == COMPLEX_TYPE))
2812 error ("wrong type argument to conjugation");
2813 return error_mark_node;
2815 else if (!noconvert)
2816 arg = default_conversion (arg);
2817 break;
2819 case TRUTH_NOT_EXPR:
2820 if (typecode != INTEGER_TYPE
2821 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2822 && typecode != COMPLEX_TYPE
2823 /* These will convert to a pointer. */
2824 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2826 error ("wrong type argument to unary exclamation mark");
2827 return error_mark_node;
2829 arg = c_common_truthvalue_conversion (arg);
2830 return invert_truthvalue (arg);
2832 case NOP_EXPR:
2833 break;
2835 case REALPART_EXPR:
2836 if (TREE_CODE (arg) == COMPLEX_CST)
2837 return TREE_REALPART (arg);
2838 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2839 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2840 else
2841 return arg;
2843 case IMAGPART_EXPR:
2844 if (TREE_CODE (arg) == COMPLEX_CST)
2845 return TREE_IMAGPART (arg);
2846 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2847 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2848 else
2849 return convert (TREE_TYPE (arg), integer_zero_node);
2851 case PREINCREMENT_EXPR:
2852 case POSTINCREMENT_EXPR:
2853 case PREDECREMENT_EXPR:
2854 case POSTDECREMENT_EXPR:
2855 /* Handle complex lvalues (when permitted)
2856 by reduction to simpler cases. */
2858 val = unary_complex_lvalue (code, arg, 0);
2859 if (val != 0)
2860 return val;
2862 /* Increment or decrement the real part of the value,
2863 and don't change the imaginary part. */
2864 if (typecode == COMPLEX_TYPE)
2866 tree real, imag;
2868 if (pedantic)
2869 pedwarn ("ISO C does not support `++' and `--' on complex types");
2871 arg = stabilize_reference (arg);
2872 real = build_unary_op (REALPART_EXPR, arg, 1);
2873 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2874 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2875 build_unary_op (code, real, 1), imag);
2878 /* Report invalid types. */
2880 if (typecode != POINTER_TYPE
2881 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2883 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2884 error ("wrong type argument to increment");
2885 else
2886 error ("wrong type argument to decrement");
2888 return error_mark_node;
2892 tree inc;
2893 tree result_type = TREE_TYPE (arg);
2895 arg = get_unwidened (arg, 0);
2896 argtype = TREE_TYPE (arg);
2898 /* Compute the increment. */
2900 if (typecode == POINTER_TYPE)
2902 /* If pointer target is an undefined struct,
2903 we just cannot know how to do the arithmetic. */
2904 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2906 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2907 error ("increment of pointer to unknown structure");
2908 else
2909 error ("decrement of pointer to unknown structure");
2911 else if ((pedantic || warn_pointer_arith)
2912 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2913 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2915 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2916 pedwarn ("wrong type argument to increment");
2917 else
2918 pedwarn ("wrong type argument to decrement");
2921 inc = c_size_in_bytes (TREE_TYPE (result_type));
2923 else
2924 inc = integer_one_node;
2926 inc = convert (argtype, inc);
2928 /* Handle incrementing a cast-expression. */
2930 while (1)
2931 switch (TREE_CODE (arg))
2933 case NOP_EXPR:
2934 case CONVERT_EXPR:
2935 case FLOAT_EXPR:
2936 case FIX_TRUNC_EXPR:
2937 case FIX_FLOOR_EXPR:
2938 case FIX_ROUND_EXPR:
2939 case FIX_CEIL_EXPR:
2940 pedantic_lvalue_warning (CONVERT_EXPR);
2941 /* If the real type has the same machine representation
2942 as the type it is cast to, we can make better output
2943 by adding directly to the inside of the cast. */
2944 if ((TREE_CODE (TREE_TYPE (arg))
2945 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2946 && (TYPE_MODE (TREE_TYPE (arg))
2947 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2948 arg = TREE_OPERAND (arg, 0);
2949 else
2951 tree incremented, modify, value;
2952 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2953 value = boolean_increment (code, arg);
2954 else
2956 arg = stabilize_reference (arg);
2957 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2958 value = arg;
2959 else
2960 value = save_expr (arg);
2961 incremented = build (((code == PREINCREMENT_EXPR
2962 || code == POSTINCREMENT_EXPR)
2963 ? PLUS_EXPR : MINUS_EXPR),
2964 argtype, value, inc);
2965 TREE_SIDE_EFFECTS (incremented) = 1;
2966 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2967 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2969 TREE_USED (value) = 1;
2970 return value;
2972 break;
2974 default:
2975 goto give_up;
2977 give_up:
2979 /* Complain about anything else that is not a true lvalue. */
2980 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2981 || code == POSTINCREMENT_EXPR)
2982 ? "invalid lvalue in increment"
2983 : "invalid lvalue in decrement")))
2984 return error_mark_node;
2986 /* Report a read-only lvalue. */
2987 if (TREE_READONLY (arg))
2988 readonly_warning (arg,
2989 ((code == PREINCREMENT_EXPR
2990 || code == POSTINCREMENT_EXPR)
2991 ? "increment" : "decrement"));
2993 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2994 val = boolean_increment (code, arg);
2995 else
2996 val = build (code, TREE_TYPE (arg), arg, inc);
2997 TREE_SIDE_EFFECTS (val) = 1;
2998 val = convert (result_type, val);
2999 if (TREE_CODE (val) != code)
3000 TREE_NO_UNUSED_WARNING (val) = 1;
3001 return val;
3004 case ADDR_EXPR:
3005 /* Note that this operation never does default_conversion. */
3007 /* Let &* cancel out to simplify resulting code. */
3008 if (TREE_CODE (arg) == INDIRECT_REF)
3010 /* Don't let this be an lvalue. */
3011 if (lvalue_p (TREE_OPERAND (arg, 0)))
3012 return non_lvalue (TREE_OPERAND (arg, 0));
3013 return TREE_OPERAND (arg, 0);
3016 /* For &x[y], return x+y */
3017 if (TREE_CODE (arg) == ARRAY_REF)
3019 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
3020 return error_mark_node;
3021 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3022 TREE_OPERAND (arg, 1), 1);
3025 /* Handle complex lvalues (when permitted)
3026 by reduction to simpler cases. */
3027 val = unary_complex_lvalue (code, arg, flag);
3028 if (val != 0)
3029 return val;
3031 #if 0 /* Turned off because inconsistent;
3032 float f; *&(int)f = 3.4 stores in int format
3033 whereas (int)f = 3.4 stores in float format. */
3034 /* Address of a cast is just a cast of the address
3035 of the operand of the cast. */
3036 switch (TREE_CODE (arg))
3038 case NOP_EXPR:
3039 case CONVERT_EXPR:
3040 case FLOAT_EXPR:
3041 case FIX_TRUNC_EXPR:
3042 case FIX_FLOOR_EXPR:
3043 case FIX_ROUND_EXPR:
3044 case FIX_CEIL_EXPR:
3045 if (pedantic)
3046 pedwarn ("ISO C forbids the address of a cast expression");
3047 return convert (build_pointer_type (TREE_TYPE (arg)),
3048 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3049 0));
3051 #endif
3053 /* Anything not already handled and not a true memory reference
3054 or a non-lvalue array is an error. */
3055 else if (typecode != FUNCTION_TYPE && !flag
3056 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3057 return error_mark_node;
3059 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3060 argtype = TREE_TYPE (arg);
3062 /* If the lvalue is const or volatile, merge that into the type
3063 to which the address will point. Note that you can't get a
3064 restricted pointer by taking the address of something, so we
3065 only have to deal with `const' and `volatile' here. */
3066 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3067 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3068 argtype = c_build_type_variant (argtype,
3069 TREE_READONLY (arg),
3070 TREE_THIS_VOLATILE (arg));
3072 argtype = build_pointer_type (argtype);
3074 if (!c_mark_addressable (arg))
3075 return error_mark_node;
3078 tree addr;
3080 if (TREE_CODE (arg) == COMPONENT_REF)
3082 tree field = TREE_OPERAND (arg, 1);
3084 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3086 if (DECL_C_BIT_FIELD (field))
3088 error ("attempt to take address of bit-field structure member `%s'",
3089 IDENTIFIER_POINTER (DECL_NAME (field)));
3090 return error_mark_node;
3093 addr = fold (build (PLUS_EXPR, argtype,
3094 convert (argtype, addr),
3095 convert (argtype, byte_position (field))));
3097 else
3098 addr = build1 (code, argtype, arg);
3100 /* Address of a static or external variable or
3101 file-scope function counts as a constant. */
3102 if (staticp (arg)
3103 && ! (TREE_CODE (arg) == FUNCTION_DECL
3104 && DECL_CONTEXT (arg) != 0))
3105 TREE_CONSTANT (addr) = 1;
3106 return addr;
3109 default:
3110 break;
3113 if (argtype == 0)
3114 argtype = TREE_TYPE (arg);
3115 return fold (build1 (code, argtype, arg));
3118 #if 0
3119 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3120 convert ARG with the same conversions in the same order
3121 and return the result. */
3123 static tree
3124 convert_sequence (conversions, arg)
3125 tree conversions;
3126 tree arg;
3128 switch (TREE_CODE (conversions))
3130 case NOP_EXPR:
3131 case CONVERT_EXPR:
3132 case FLOAT_EXPR:
3133 case FIX_TRUNC_EXPR:
3134 case FIX_FLOOR_EXPR:
3135 case FIX_ROUND_EXPR:
3136 case FIX_CEIL_EXPR:
3137 return convert (TREE_TYPE (conversions),
3138 convert_sequence (TREE_OPERAND (conversions, 0),
3139 arg));
3141 default:
3142 return arg;
3145 #endif /* 0 */
3147 /* Return nonzero if REF is an lvalue valid for this language.
3148 Lvalues can be assigned, unless their type has TYPE_READONLY.
3149 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3152 lvalue_p (ref)
3153 tree ref;
3155 enum tree_code code = TREE_CODE (ref);
3157 switch (code)
3159 case REALPART_EXPR:
3160 case IMAGPART_EXPR:
3161 case COMPONENT_REF:
3162 return lvalue_p (TREE_OPERAND (ref, 0));
3164 case COMPOUND_LITERAL_EXPR:
3165 case STRING_CST:
3166 return 1;
3168 case INDIRECT_REF:
3169 case ARRAY_REF:
3170 case VAR_DECL:
3171 case PARM_DECL:
3172 case RESULT_DECL:
3173 case ERROR_MARK:
3174 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3175 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3177 case BIND_EXPR:
3178 case RTL_EXPR:
3179 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3181 default:
3182 return 0;
3186 /* Return nonzero if REF is an lvalue valid for this language;
3187 otherwise, print an error message and return zero. */
3190 lvalue_or_else (ref, msgid)
3191 tree ref;
3192 const char *msgid;
3194 int win = lvalue_p (ref);
3196 if (! win)
3197 error ("%s", msgid);
3199 return win;
3202 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3203 for certain kinds of expressions which are not really lvalues
3204 but which we can accept as lvalues. If FLAG is nonzero, then
3205 non-lvalues are OK since we may be converting a non-lvalue array to
3206 a pointer in C99.
3208 If ARG is not a kind of expression we can handle, return zero. */
3210 static tree
3211 unary_complex_lvalue (code, arg, flag)
3212 enum tree_code code;
3213 tree arg;
3214 int flag;
3216 /* Handle (a, b) used as an "lvalue". */
3217 if (TREE_CODE (arg) == COMPOUND_EXPR)
3219 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3221 /* If this returns a function type, it isn't really being used as
3222 an lvalue, so don't issue a warning about it. */
3223 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3224 pedantic_lvalue_warning (COMPOUND_EXPR);
3226 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3227 TREE_OPERAND (arg, 0), real_result);
3230 /* Handle (a ? b : c) used as an "lvalue". */
3231 if (TREE_CODE (arg) == COND_EXPR)
3233 if (!flag)
3234 pedantic_lvalue_warning (COND_EXPR);
3235 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3236 pedantic_lvalue_warning (COMPOUND_EXPR);
3238 return (build_conditional_expr
3239 (TREE_OPERAND (arg, 0),
3240 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3241 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3244 return 0;
3247 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3248 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3250 static void
3251 pedantic_lvalue_warning (code)
3252 enum tree_code code;
3254 if (pedantic)
3255 switch (code)
3257 case COND_EXPR:
3258 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3259 break;
3260 case COMPOUND_EXPR:
3261 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3262 break;
3263 default:
3264 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3265 break;
3269 /* Warn about storing in something that is `const'. */
3271 void
3272 readonly_warning (arg, msgid)
3273 tree arg;
3274 const char *msgid;
3276 if (TREE_CODE (arg) == COMPONENT_REF)
3278 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3279 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3280 else
3281 pedwarn ("%s of read-only member `%s'", _(msgid),
3282 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3284 else if (TREE_CODE (arg) == VAR_DECL)
3285 pedwarn ("%s of read-only variable `%s'", _(msgid),
3286 IDENTIFIER_POINTER (DECL_NAME (arg)));
3287 else
3288 pedwarn ("%s of read-only location", _(msgid));
3291 /* Mark EXP saying that we need to be able to take the
3292 address of it; it should not be allocated in a register.
3293 Returns true if successful. */
3295 bool
3296 c_mark_addressable (exp)
3297 tree exp;
3299 tree x = exp;
3301 while (1)
3302 switch (TREE_CODE (x))
3304 case COMPONENT_REF:
3305 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3307 error ("cannot take address of bit-field `%s'",
3308 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3309 return false;
3312 /* ... fall through ... */
3314 case ADDR_EXPR:
3315 case ARRAY_REF:
3316 case REALPART_EXPR:
3317 case IMAGPART_EXPR:
3318 x = TREE_OPERAND (x, 0);
3319 break;
3321 case COMPOUND_LITERAL_EXPR:
3322 case CONSTRUCTOR:
3323 TREE_ADDRESSABLE (x) = 1;
3324 return true;
3326 case VAR_DECL:
3327 case CONST_DECL:
3328 case PARM_DECL:
3329 case RESULT_DECL:
3330 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3331 && DECL_NONLOCAL (x))
3333 if (TREE_PUBLIC (x))
3335 error ("global register variable `%s' used in nested function",
3336 IDENTIFIER_POINTER (DECL_NAME (x)));
3337 return false;
3339 pedwarn ("register variable `%s' used in nested function",
3340 IDENTIFIER_POINTER (DECL_NAME (x)));
3342 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3344 if (TREE_PUBLIC (x))
3346 error ("address of global register variable `%s' requested",
3347 IDENTIFIER_POINTER (DECL_NAME (x)));
3348 return false;
3351 /* If we are making this addressable due to its having
3352 volatile components, give a different error message. Also
3353 handle the case of an unnamed parameter by not trying
3354 to give the name. */
3356 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3358 error ("cannot put object with volatile field into register");
3359 return false;
3362 pedwarn ("address of register variable `%s' requested",
3363 IDENTIFIER_POINTER (DECL_NAME (x)));
3365 put_var_into_stack (x, /*rescan=*/true);
3367 /* drops in */
3368 case FUNCTION_DECL:
3369 TREE_ADDRESSABLE (x) = 1;
3370 #if 0 /* poplevel deals with this now. */
3371 if (DECL_CONTEXT (x) == 0)
3372 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3373 #endif
3375 default:
3376 return true;
3380 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3382 tree
3383 build_conditional_expr (ifexp, op1, op2)
3384 tree ifexp, op1, op2;
3386 tree type1;
3387 tree type2;
3388 enum tree_code code1;
3389 enum tree_code code2;
3390 tree result_type = NULL;
3391 tree orig_op1 = op1, orig_op2 = op2;
3393 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3395 #if 0 /* Produces wrong result if within sizeof. */
3396 /* Don't promote the operands separately if they promote
3397 the same way. Return the unpromoted type and let the combined
3398 value get promoted if necessary. */
3400 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3401 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3402 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3403 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3405 if (TREE_CODE (ifexp) == INTEGER_CST)
3406 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3408 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3410 #endif
3412 /* Promote both alternatives. */
3414 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3415 op1 = default_conversion (op1);
3416 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3417 op2 = default_conversion (op2);
3419 if (TREE_CODE (ifexp) == ERROR_MARK
3420 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3421 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3422 return error_mark_node;
3424 type1 = TREE_TYPE (op1);
3425 code1 = TREE_CODE (type1);
3426 type2 = TREE_TYPE (op2);
3427 code2 = TREE_CODE (type2);
3429 /* Quickly detect the usual case where op1 and op2 have the same type
3430 after promotion. */
3431 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3433 if (type1 == type2)
3434 result_type = type1;
3435 else
3436 result_type = TYPE_MAIN_VARIANT (type1);
3438 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3439 || code1 == COMPLEX_TYPE)
3440 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3441 || code2 == COMPLEX_TYPE))
3443 result_type = common_type (type1, type2);
3445 /* If -Wsign-compare, warn here if type1 and type2 have
3446 different signedness. We'll promote the signed to unsigned
3447 and later code won't know it used to be different.
3448 Do this check on the original types, so that explicit casts
3449 will be considered, but default promotions won't. */
3450 if (warn_sign_compare && !skip_evaluation)
3452 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3453 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3455 if (unsigned_op1 ^ unsigned_op2)
3457 /* Do not warn if the result type is signed, since the
3458 signed type will only be chosen if it can represent
3459 all the values of the unsigned type. */
3460 if (! TREE_UNSIGNED (result_type))
3461 /* OK */;
3462 /* Do not warn if the signed quantity is an unsuffixed
3463 integer literal (or some static constant expression
3464 involving such literals) and it is non-negative. */
3465 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3466 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3467 /* OK */;
3468 else
3469 warning ("signed and unsigned type in conditional expression");
3473 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3475 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3476 pedwarn ("ISO C forbids conditional expr with only one void side");
3477 result_type = void_type_node;
3479 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3481 if (comp_target_types (type1, type2, 1))
3482 result_type = common_type (type1, type2);
3483 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3484 && TREE_CODE (orig_op1) != NOP_EXPR)
3485 result_type = qualify_type (type2, type1);
3486 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3487 && TREE_CODE (orig_op2) != NOP_EXPR)
3488 result_type = qualify_type (type1, type2);
3489 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3491 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3492 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3493 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3494 TREE_TYPE (type2)));
3496 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3498 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3499 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3500 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3501 TREE_TYPE (type1)));
3503 else
3505 pedwarn ("pointer type mismatch in conditional expression");
3506 result_type = build_pointer_type (void_type_node);
3509 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3511 if (! integer_zerop (op2))
3512 pedwarn ("pointer/integer type mismatch in conditional expression");
3513 else
3515 op2 = null_pointer_node;
3517 result_type = type1;
3519 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3521 if (!integer_zerop (op1))
3522 pedwarn ("pointer/integer type mismatch in conditional expression");
3523 else
3525 op1 = null_pointer_node;
3527 result_type = type2;
3530 if (!result_type)
3532 if (flag_cond_mismatch)
3533 result_type = void_type_node;
3534 else
3536 error ("type mismatch in conditional expression");
3537 return error_mark_node;
3541 /* Merge const and volatile flags of the incoming types. */
3542 result_type
3543 = build_type_variant (result_type,
3544 TREE_READONLY (op1) || TREE_READONLY (op2),
3545 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3547 if (result_type != TREE_TYPE (op1))
3548 op1 = convert_and_check (result_type, op1);
3549 if (result_type != TREE_TYPE (op2))
3550 op2 = convert_and_check (result_type, op2);
3552 if (TREE_CODE (ifexp) == INTEGER_CST)
3553 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3555 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3558 /* Given a list of expressions, return a compound expression
3559 that performs them all and returns the value of the last of them. */
3561 tree
3562 build_compound_expr (list)
3563 tree list;
3565 return internal_build_compound_expr (list, TRUE);
3568 static tree
3569 internal_build_compound_expr (list, first_p)
3570 tree list;
3571 int first_p;
3573 tree rest;
3575 if (TREE_CHAIN (list) == 0)
3577 /* Convert arrays and functions to pointers when there
3578 really is a comma operator. */
3579 if (!first_p)
3580 TREE_VALUE (list)
3581 = default_function_array_conversion (TREE_VALUE (list));
3583 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3584 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3586 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3587 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3588 list = TREE_OPERAND (list, 0);
3589 #endif
3591 /* Don't let (0, 0) be null pointer constant. */
3592 if (!first_p && integer_zerop (TREE_VALUE (list)))
3593 return non_lvalue (TREE_VALUE (list));
3594 return TREE_VALUE (list);
3597 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3599 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3601 /* The left-hand operand of a comma expression is like an expression
3602 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3603 any side-effects, unless it was explicitly cast to (void). */
3604 if (warn_unused_value
3605 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3606 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3607 warning ("left-hand operand of comma expression has no effect");
3609 /* When pedantic, a compound expression can be neither an lvalue
3610 nor an integer constant expression. */
3611 if (! pedantic)
3612 return rest;
3615 /* With -Wunused, we should also warn if the left-hand operand does have
3616 side-effects, but computes a value which is not used. For example, in
3617 `foo() + bar(), baz()' the result of the `+' operator is not used,
3618 so we should issue a warning. */
3619 else if (warn_unused_value)
3620 warn_if_unused_value (TREE_VALUE (list));
3622 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3625 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3627 tree
3628 build_c_cast (type, expr)
3629 tree type;
3630 tree expr;
3632 tree value = expr;
3634 if (type == error_mark_node || expr == error_mark_node)
3635 return error_mark_node;
3637 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3638 only in <protocol> qualifications. But when constructing cast expressions,
3639 the protocols do matter and must be kept around. */
3640 if (!flag_objc || !objc_is_id (type))
3641 type = TYPE_MAIN_VARIANT (type);
3643 #if 0
3644 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3645 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3646 value = TREE_OPERAND (value, 0);
3647 #endif
3649 if (TREE_CODE (type) == ARRAY_TYPE)
3651 error ("cast specifies array type");
3652 return error_mark_node;
3655 if (TREE_CODE (type) == FUNCTION_TYPE)
3657 error ("cast specifies function type");
3658 return error_mark_node;
3661 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3663 if (pedantic)
3665 if (TREE_CODE (type) == RECORD_TYPE
3666 || TREE_CODE (type) == UNION_TYPE)
3667 pedwarn ("ISO C forbids casting nonscalar to the same type");
3670 else if (TREE_CODE (type) == UNION_TYPE)
3672 tree field;
3673 value = default_function_array_conversion (value);
3675 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3676 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3677 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3678 break;
3680 if (field)
3682 tree t;
3684 if (pedantic)
3685 pedwarn ("ISO C forbids casts to union type");
3686 t = digest_init (type,
3687 build_constructor (type,
3688 build_tree_list (field, value)),
3690 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3691 return t;
3693 error ("cast to union type from type not present in union");
3694 return error_mark_node;
3696 else
3698 tree otype, ovalue;
3700 /* If casting to void, avoid the error that would come
3701 from default_conversion in the case of a non-lvalue array. */
3702 if (type == void_type_node)
3703 return build1 (CONVERT_EXPR, type, value);
3705 /* Convert functions and arrays to pointers,
3706 but don't convert any other types. */
3707 value = default_function_array_conversion (value);
3708 otype = TREE_TYPE (value);
3710 /* Optionally warn about potentially worrisome casts. */
3712 if (warn_cast_qual
3713 && TREE_CODE (type) == POINTER_TYPE
3714 && TREE_CODE (otype) == POINTER_TYPE)
3716 tree in_type = type;
3717 tree in_otype = otype;
3718 int added = 0;
3719 int discarded = 0;
3721 /* Check that the qualifiers on IN_TYPE are a superset of
3722 the qualifiers of IN_OTYPE. The outermost level of
3723 POINTER_TYPE nodes is uninteresting and we stop as soon
3724 as we hit a non-POINTER_TYPE node on either type. */
3727 in_otype = TREE_TYPE (in_otype);
3728 in_type = TREE_TYPE (in_type);
3730 /* GNU C allows cv-qualified function types. 'const'
3731 means the function is very pure, 'volatile' means it
3732 can't return. We need to warn when such qualifiers
3733 are added, not when they're taken away. */
3734 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3735 && TREE_CODE (in_type) == FUNCTION_TYPE)
3736 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3737 else
3738 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3740 while (TREE_CODE (in_type) == POINTER_TYPE
3741 && TREE_CODE (in_otype) == POINTER_TYPE);
3743 if (added)
3744 warning ("cast adds new qualifiers to function type");
3746 if (discarded)
3747 /* There are qualifiers present in IN_OTYPE that are not
3748 present in IN_TYPE. */
3749 warning ("cast discards qualifiers from pointer target type");
3752 /* Warn about possible alignment problems. */
3753 if (STRICT_ALIGNMENT && warn_cast_align
3754 && TREE_CODE (type) == POINTER_TYPE
3755 && TREE_CODE (otype) == POINTER_TYPE
3756 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3757 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3758 /* Don't warn about opaque types, where the actual alignment
3759 restriction is unknown. */
3760 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3761 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3762 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3763 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3764 warning ("cast increases required alignment of target type");
3766 if (TREE_CODE (type) == INTEGER_TYPE
3767 && TREE_CODE (otype) == POINTER_TYPE
3768 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3769 && !TREE_CONSTANT (value))
3770 warning ("cast from pointer to integer of different size");
3772 if (warn_bad_function_cast
3773 && TREE_CODE (value) == CALL_EXPR
3774 && TREE_CODE (type) != TREE_CODE (otype))
3775 warning ("cast does not match function type");
3777 if (TREE_CODE (type) == POINTER_TYPE
3778 && TREE_CODE (otype) == INTEGER_TYPE
3779 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3780 /* Don't warn about converting any constant. */
3781 && !TREE_CONSTANT (value))
3782 warning ("cast to pointer from integer of different size");
3784 if (TREE_CODE (type) == POINTER_TYPE
3785 && TREE_CODE (otype) == POINTER_TYPE
3786 && TREE_CODE (expr) == ADDR_EXPR
3787 && DECL_P (TREE_OPERAND (expr, 0))
3788 && flag_strict_aliasing && warn_strict_aliasing
3789 && !VOID_TYPE_P (TREE_TYPE (type)))
3791 /* Casting the address of a decl to non void pointer. Warn
3792 if the cast breaks type based aliasing. */
3793 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3794 warning ("type-punning to incomplete type might break strict-aliasing rules");
3795 else if (!alias_sets_conflict_p
3796 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3797 get_alias_set (TREE_TYPE (type))))
3798 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3801 ovalue = value;
3802 /* Replace a nonvolatile const static variable with its value. */
3803 if (optimize && TREE_CODE (value) == VAR_DECL)
3804 value = decl_constant_value (value);
3805 value = convert (type, value);
3807 /* Ignore any integer overflow caused by the cast. */
3808 if (TREE_CODE (value) == INTEGER_CST)
3810 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3811 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3815 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3816 if (pedantic && TREE_CODE (value) == INTEGER_CST
3817 && TREE_CODE (expr) == INTEGER_CST
3818 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3819 value = non_lvalue (value);
3821 /* If pedantic, don't let a cast be an lvalue. */
3822 if (value == expr && pedantic)
3823 value = non_lvalue (value);
3825 return value;
3828 /* Interpret a cast of expression EXPR to type TYPE. */
3829 tree
3830 c_cast_expr (type, expr)
3831 tree type, expr;
3833 int saved_wsp = warn_strict_prototypes;
3835 /* This avoids warnings about unprototyped casts on
3836 integers. E.g. "#define SIG_DFL (void(*)())0". */
3837 if (TREE_CODE (expr) == INTEGER_CST)
3838 warn_strict_prototypes = 0;
3839 type = groktypename (type);
3840 warn_strict_prototypes = saved_wsp;
3842 return build_c_cast (type, expr);
3846 /* Build an assignment expression of lvalue LHS from value RHS.
3847 MODIFYCODE is the code for a binary operator that we use
3848 to combine the old value of LHS with RHS to get the new value.
3849 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3851 tree
3852 build_modify_expr (lhs, modifycode, rhs)
3853 tree lhs, rhs;
3854 enum tree_code modifycode;
3856 tree result;
3857 tree newrhs;
3858 tree lhstype = TREE_TYPE (lhs);
3859 tree olhstype = lhstype;
3861 /* Types that aren't fully specified cannot be used in assignments. */
3862 lhs = require_complete_type (lhs);
3864 /* Avoid duplicate error messages from operands that had errors. */
3865 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3866 return error_mark_node;
3868 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3869 /* Do not use STRIP_NOPS here. We do not want an enumerator
3870 whose value is 0 to count as a null pointer constant. */
3871 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3872 rhs = TREE_OPERAND (rhs, 0);
3874 newrhs = rhs;
3876 /* Handle control structure constructs used as "lvalues". */
3878 switch (TREE_CODE (lhs))
3880 /* Handle (a, b) used as an "lvalue". */
3881 case COMPOUND_EXPR:
3882 pedantic_lvalue_warning (COMPOUND_EXPR);
3883 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3884 if (TREE_CODE (newrhs) == ERROR_MARK)
3885 return error_mark_node;
3886 return build (COMPOUND_EXPR, lhstype,
3887 TREE_OPERAND (lhs, 0), newrhs);
3889 /* Handle (a ? b : c) used as an "lvalue". */
3890 case COND_EXPR:
3891 pedantic_lvalue_warning (COND_EXPR);
3892 rhs = save_expr (rhs);
3894 /* Produce (a ? (b = rhs) : (c = rhs))
3895 except that the RHS goes through a save-expr
3896 so the code to compute it is only emitted once. */
3897 tree cond
3898 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3899 build_modify_expr (TREE_OPERAND (lhs, 1),
3900 modifycode, rhs),
3901 build_modify_expr (TREE_OPERAND (lhs, 2),
3902 modifycode, rhs));
3903 if (TREE_CODE (cond) == ERROR_MARK)
3904 return cond;
3905 /* Make sure the code to compute the rhs comes out
3906 before the split. */
3907 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3908 /* But cast it to void to avoid an "unused" error. */
3909 convert (void_type_node, rhs), cond);
3911 default:
3912 break;
3915 /* If a binary op has been requested, combine the old LHS value with the RHS
3916 producing the value we should actually store into the LHS. */
3918 if (modifycode != NOP_EXPR)
3920 lhs = stabilize_reference (lhs);
3921 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3924 /* Handle a cast used as an "lvalue".
3925 We have already performed any binary operator using the value as cast.
3926 Now convert the result to the cast type of the lhs,
3927 and then true type of the lhs and store it there;
3928 then convert result back to the cast type to be the value
3929 of the assignment. */
3931 switch (TREE_CODE (lhs))
3933 case NOP_EXPR:
3934 case CONVERT_EXPR:
3935 case FLOAT_EXPR:
3936 case FIX_TRUNC_EXPR:
3937 case FIX_FLOOR_EXPR:
3938 case FIX_ROUND_EXPR:
3939 case FIX_CEIL_EXPR:
3940 newrhs = default_function_array_conversion (newrhs);
3942 tree inner_lhs = TREE_OPERAND (lhs, 0);
3943 tree result;
3944 result = build_modify_expr (inner_lhs, NOP_EXPR,
3945 convert (TREE_TYPE (inner_lhs),
3946 convert (lhstype, newrhs)));
3947 if (TREE_CODE (result) == ERROR_MARK)
3948 return result;
3949 pedantic_lvalue_warning (CONVERT_EXPR);
3950 return convert (TREE_TYPE (lhs), result);
3953 default:
3954 break;
3957 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3958 Reject anything strange now. */
3960 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3961 return error_mark_node;
3963 /* Warn about storing in something that is `const'. */
3965 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3966 || ((TREE_CODE (lhstype) == RECORD_TYPE
3967 || TREE_CODE (lhstype) == UNION_TYPE)
3968 && C_TYPE_FIELDS_READONLY (lhstype)))
3969 readonly_warning (lhs, "assignment");
3971 /* If storing into a structure or union member,
3972 it has probably been given type `int'.
3973 Compute the type that would go with
3974 the actual amount of storage the member occupies. */
3976 if (TREE_CODE (lhs) == COMPONENT_REF
3977 && (TREE_CODE (lhstype) == INTEGER_TYPE
3978 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3979 || TREE_CODE (lhstype) == REAL_TYPE
3980 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3981 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3983 /* If storing in a field that is in actuality a short or narrower than one,
3984 we must store in the field in its actual type. */
3986 if (lhstype != TREE_TYPE (lhs))
3988 lhs = copy_node (lhs);
3989 TREE_TYPE (lhs) = lhstype;
3992 /* Convert new value to destination type. */
3994 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3995 NULL_TREE, NULL_TREE, 0);
3996 if (TREE_CODE (newrhs) == ERROR_MARK)
3997 return error_mark_node;
3999 /* Scan operands */
4001 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4002 TREE_SIDE_EFFECTS (result) = 1;
4004 /* If we got the LHS in a different type for storing in,
4005 convert the result back to the nominal type of LHS
4006 so that the value we return always has the same type
4007 as the LHS argument. */
4009 if (olhstype == TREE_TYPE (result))
4010 return result;
4011 return convert_for_assignment (olhstype, result, _("assignment"),
4012 NULL_TREE, NULL_TREE, 0);
4015 /* Convert value RHS to type TYPE as preparation for an assignment
4016 to an lvalue of type TYPE.
4017 The real work of conversion is done by `convert'.
4018 The purpose of this function is to generate error messages
4019 for assignments that are not allowed in C.
4020 ERRTYPE is a string to use in error messages:
4021 "assignment", "return", etc. If it is null, this is parameter passing
4022 for a function call (and different error messages are output).
4024 FUNNAME is the name of the function being called,
4025 as an IDENTIFIER_NODE, or null.
4026 PARMNUM is the number of the argument, for printing in error messages. */
4028 static tree
4029 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4030 tree type, rhs;
4031 const char *errtype;
4032 tree fundecl, funname;
4033 int parmnum;
4035 enum tree_code codel = TREE_CODE (type);
4036 tree rhstype;
4037 enum tree_code coder;
4039 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4040 /* Do not use STRIP_NOPS here. We do not want an enumerator
4041 whose value is 0 to count as a null pointer constant. */
4042 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4043 rhs = TREE_OPERAND (rhs, 0);
4045 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4046 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4047 rhs = default_conversion (rhs);
4048 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4049 rhs = decl_constant_value_for_broken_optimization (rhs);
4051 rhstype = TREE_TYPE (rhs);
4052 coder = TREE_CODE (rhstype);
4054 if (coder == ERROR_MARK)
4055 return error_mark_node;
4057 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4059 overflow_warning (rhs);
4060 /* Check for Objective-C protocols. This will automatically
4061 issue a warning if there are protocol violations. No need to
4062 use the return value. */
4063 if (flag_objc)
4064 objc_comptypes (type, rhstype, 0);
4065 return rhs;
4068 if (coder == VOID_TYPE)
4070 error ("void value not ignored as it ought to be");
4071 return error_mark_node;
4073 /* A type converts to a reference to it.
4074 This code doesn't fully support references, it's just for the
4075 special case of va_start and va_copy. */
4076 if (codel == REFERENCE_TYPE
4077 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4079 if (!lvalue_p (rhs))
4081 error ("cannot pass rvalue to reference parameter");
4082 return error_mark_node;
4084 if (!c_mark_addressable (rhs))
4085 return error_mark_node;
4086 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4088 /* We already know that these two types are compatible, but they
4089 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4090 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4091 likely to be va_list, a typedef to __builtin_va_list, which
4092 is different enough that it will cause problems later. */
4093 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4094 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4096 rhs = build1 (NOP_EXPR, type, rhs);
4097 return rhs;
4099 /* Some types can interconvert without explicit casts. */
4100 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4101 && ((*targetm.vector_opaque_p) (type)
4102 || (*targetm.vector_opaque_p) (rhstype)))
4103 return convert (type, rhs);
4104 /* Arithmetic types all interconvert, and enum is treated like int. */
4105 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4106 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4107 || codel == BOOLEAN_TYPE)
4108 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4109 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4110 || coder == BOOLEAN_TYPE))
4111 return convert_and_check (type, rhs);
4113 /* Conversion to a transparent union from its member types.
4114 This applies only to function arguments. */
4115 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4117 tree memb_types;
4118 tree marginal_memb_type = 0;
4120 for (memb_types = TYPE_FIELDS (type); memb_types;
4121 memb_types = TREE_CHAIN (memb_types))
4123 tree memb_type = TREE_TYPE (memb_types);
4125 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4126 TYPE_MAIN_VARIANT (rhstype)))
4127 break;
4129 if (TREE_CODE (memb_type) != POINTER_TYPE)
4130 continue;
4132 if (coder == POINTER_TYPE)
4134 tree ttl = TREE_TYPE (memb_type);
4135 tree ttr = TREE_TYPE (rhstype);
4137 /* Any non-function converts to a [const][volatile] void *
4138 and vice versa; otherwise, targets must be the same.
4139 Meanwhile, the lhs target must have all the qualifiers of
4140 the rhs. */
4141 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4142 || comp_target_types (memb_type, rhstype, 0))
4144 /* If this type won't generate any warnings, use it. */
4145 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4146 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4147 && TREE_CODE (ttl) == FUNCTION_TYPE)
4148 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4149 == TYPE_QUALS (ttr))
4150 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4151 == TYPE_QUALS (ttl))))
4152 break;
4154 /* Keep looking for a better type, but remember this one. */
4155 if (! marginal_memb_type)
4156 marginal_memb_type = memb_type;
4160 /* Can convert integer zero to any pointer type. */
4161 if (integer_zerop (rhs)
4162 || (TREE_CODE (rhs) == NOP_EXPR
4163 && integer_zerop (TREE_OPERAND (rhs, 0))))
4165 rhs = null_pointer_node;
4166 break;
4170 if (memb_types || marginal_memb_type)
4172 if (! memb_types)
4174 /* We have only a marginally acceptable member type;
4175 it needs a warning. */
4176 tree ttl = TREE_TYPE (marginal_memb_type);
4177 tree ttr = TREE_TYPE (rhstype);
4179 /* Const and volatile mean something different for function
4180 types, so the usual warnings are not appropriate. */
4181 if (TREE_CODE (ttr) == FUNCTION_TYPE
4182 && TREE_CODE (ttl) == FUNCTION_TYPE)
4184 /* Because const and volatile on functions are
4185 restrictions that say the function will not do
4186 certain things, it is okay to use a const or volatile
4187 function where an ordinary one is wanted, but not
4188 vice-versa. */
4189 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4190 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4191 errtype, funname, parmnum);
4193 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4194 warn_for_assignment ("%s discards qualifiers from pointer target type",
4195 errtype, funname,
4196 parmnum);
4199 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4200 pedwarn ("ISO C prohibits argument conversion to union type");
4202 return build1 (NOP_EXPR, type, rhs);
4206 /* Conversions among pointers */
4207 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4208 && (coder == codel))
4210 tree ttl = TREE_TYPE (type);
4211 tree ttr = TREE_TYPE (rhstype);
4212 bool is_opaque_pointer;
4214 /* Opaque pointers are treated like void pointers. */
4215 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
4216 || (*targetm.vector_opaque_p) (rhstype))
4217 && TREE_CODE (ttl) == VECTOR_TYPE
4218 && TREE_CODE (ttr) == VECTOR_TYPE;
4220 /* Any non-function converts to a [const][volatile] void *
4221 and vice versa; otherwise, targets must be the same.
4222 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4223 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4224 || comp_target_types (type, rhstype, 0)
4225 || is_opaque_pointer
4226 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4227 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4229 if (pedantic
4230 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4232 (VOID_TYPE_P (ttr)
4233 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4234 which are not ANSI null ptr constants. */
4235 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4236 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4237 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4238 errtype, funname, parmnum);
4239 /* Const and volatile mean something different for function types,
4240 so the usual warnings are not appropriate. */
4241 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4242 && TREE_CODE (ttl) != FUNCTION_TYPE)
4244 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4245 warn_for_assignment ("%s discards qualifiers from pointer target type",
4246 errtype, funname, parmnum);
4247 /* If this is not a case of ignoring a mismatch in signedness,
4248 no warning. */
4249 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4250 || comp_target_types (type, rhstype, 0))
4252 /* If there is a mismatch, do warn. */
4253 else if (pedantic)
4254 warn_for_assignment ("pointer targets in %s differ in signedness",
4255 errtype, funname, parmnum);
4257 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4258 && TREE_CODE (ttr) == FUNCTION_TYPE)
4260 /* Because const and volatile on functions are restrictions
4261 that say the function will not do certain things,
4262 it is okay to use a const or volatile function
4263 where an ordinary one is wanted, but not vice-versa. */
4264 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4265 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4266 errtype, funname, parmnum);
4269 else
4270 warn_for_assignment ("%s from incompatible pointer type",
4271 errtype, funname, parmnum);
4272 return convert (type, rhs);
4274 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4276 /* An explicit constant 0 can convert to a pointer,
4277 or one that results from arithmetic, even including
4278 a cast to integer type. */
4279 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4281 ! (TREE_CODE (rhs) == NOP_EXPR
4282 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4283 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4284 && integer_zerop (TREE_OPERAND (rhs, 0))))
4286 warn_for_assignment ("%s makes pointer from integer without a cast",
4287 errtype, funname, parmnum);
4288 return convert (type, rhs);
4290 return null_pointer_node;
4292 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4294 warn_for_assignment ("%s makes integer from pointer without a cast",
4295 errtype, funname, parmnum);
4296 return convert (type, rhs);
4298 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4299 return convert (type, rhs);
4301 if (!errtype)
4303 if (funname)
4305 tree selector = objc_message_selector ();
4307 if (selector && parmnum > 2)
4308 error ("incompatible type for argument %d of `%s'",
4309 parmnum - 2, IDENTIFIER_POINTER (selector));
4310 else
4311 error ("incompatible type for argument %d of `%s'",
4312 parmnum, IDENTIFIER_POINTER (funname));
4314 else
4315 error ("incompatible type for argument %d of indirect function call",
4316 parmnum);
4318 else
4319 error ("incompatible types in %s", errtype);
4321 return error_mark_node;
4324 /* Convert VALUE for assignment into inlined parameter PARM. */
4326 tree
4327 c_convert_parm_for_inlining (parm, value, fn)
4328 tree parm, value, fn;
4330 tree ret, type;
4332 /* If FN was prototyped, the value has been converted already
4333 in convert_arguments. */
4334 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4335 return value;
4337 type = TREE_TYPE (parm);
4338 ret = convert_for_assignment (type, value,
4339 (char *) 0 /* arg passing */, fn,
4340 DECL_NAME (fn), 0);
4341 if (PROMOTE_PROTOTYPES
4342 && INTEGRAL_TYPE_P (type)
4343 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4344 ret = default_conversion (ret);
4345 return ret;
4348 /* Print a warning using MSGID.
4349 It gets OPNAME as its one parameter.
4350 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4351 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4352 FUNCTION and ARGNUM are handled specially if we are building an
4353 Objective-C selector. */
4355 static void
4356 warn_for_assignment (msgid, opname, function, argnum)
4357 const char *msgid;
4358 const char *opname;
4359 tree function;
4360 int argnum;
4362 if (opname == 0)
4364 tree selector = objc_message_selector ();
4365 char * new_opname;
4367 if (selector && argnum > 2)
4369 function = selector;
4370 argnum -= 2;
4372 if (argnum == 0)
4374 if (function)
4376 /* Function name is known; supply it. */
4377 const char *const argstring = _("passing arg of `%s'");
4378 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4379 + strlen (argstring) + 1
4380 + 1);
4381 sprintf (new_opname, argstring,
4382 IDENTIFIER_POINTER (function));
4384 else
4386 /* Function name unknown (call through ptr). */
4387 const char *const argnofun = _("passing arg of pointer to function");
4388 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4389 sprintf (new_opname, argnofun);
4392 else if (function)
4394 /* Function name is known; supply it. */
4395 const char *const argstring = _("passing arg %d of `%s'");
4396 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4397 + strlen (argstring) + 1 + 25
4398 /*%d*/ + 1);
4399 sprintf (new_opname, argstring, argnum,
4400 IDENTIFIER_POINTER (function));
4402 else
4404 /* Function name unknown (call through ptr); just give arg number. */
4405 const char *const argnofun = _("passing arg %d of pointer to function");
4406 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4407 sprintf (new_opname, argnofun, argnum);
4409 opname = new_opname;
4411 pedwarn (msgid, opname);
4414 /* If VALUE is a compound expr all of whose expressions are constant, then
4415 return its value. Otherwise, return error_mark_node.
4417 This is for handling COMPOUND_EXPRs as initializer elements
4418 which is allowed with a warning when -pedantic is specified. */
4420 static tree
4421 valid_compound_expr_initializer (value, endtype)
4422 tree value;
4423 tree endtype;
4425 if (TREE_CODE (value) == COMPOUND_EXPR)
4427 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4428 == error_mark_node)
4429 return error_mark_node;
4430 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4431 endtype);
4433 else if (! TREE_CONSTANT (value)
4434 && ! initializer_constant_valid_p (value, endtype))
4435 return error_mark_node;
4436 else
4437 return value;
4440 /* Perform appropriate conversions on the initial value of a variable,
4441 store it in the declaration DECL,
4442 and print any error messages that are appropriate.
4443 If the init is invalid, store an ERROR_MARK. */
4445 void
4446 store_init_value (decl, init)
4447 tree decl, init;
4449 tree value, type;
4451 /* If variable's type was invalidly declared, just ignore it. */
4453 type = TREE_TYPE (decl);
4454 if (TREE_CODE (type) == ERROR_MARK)
4455 return;
4457 /* Digest the specified initializer into an expression. */
4459 value = digest_init (type, init, TREE_STATIC (decl));
4461 /* Store the expression if valid; else report error. */
4463 #if 0
4464 /* Note that this is the only place we can detect the error
4465 in a case such as struct foo bar = (struct foo) { x, y };
4466 where there is one initial value which is a constructor expression. */
4467 if (value == error_mark_node)
4469 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4471 error ("initializer for static variable is not constant");
4472 value = error_mark_node;
4474 else if (TREE_STATIC (decl)
4475 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4477 error ("initializer for static variable uses complicated arithmetic");
4478 value = error_mark_node;
4480 else
4482 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4484 if (! TREE_CONSTANT (value))
4485 pedwarn ("aggregate initializer is not constant");
4486 else if (! TREE_STATIC (value))
4487 pedwarn ("aggregate initializer uses complicated arithmetic");
4490 #endif
4492 if (warn_traditional && !in_system_header
4493 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4494 warning ("traditional C rejects automatic aggregate initialization");
4496 DECL_INITIAL (decl) = value;
4498 /* ANSI wants warnings about out-of-range constant initializers. */
4499 STRIP_TYPE_NOPS (value);
4500 constant_expression_warning (value);
4502 /* Check if we need to set array size from compound literal size. */
4503 if (TREE_CODE (type) == ARRAY_TYPE
4504 && TYPE_DOMAIN (type) == 0
4505 && value != error_mark_node)
4507 tree inside_init = init;
4509 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4510 inside_init = TREE_OPERAND (init, 0);
4511 inside_init = fold (inside_init);
4513 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4515 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4517 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4519 /* For int foo[] = (int [3]){1}; we need to set array size
4520 now since later on array initializer will be just the
4521 brace enclosed list of the compound literal. */
4522 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4523 layout_type (type);
4524 layout_decl (decl, 0);
4530 /* Methods for storing and printing names for error messages. */
4532 /* Implement a spelling stack that allows components of a name to be pushed
4533 and popped. Each element on the stack is this structure. */
4535 struct spelling
4537 int kind;
4538 union
4540 int i;
4541 const char *s;
4542 } u;
4545 #define SPELLING_STRING 1
4546 #define SPELLING_MEMBER 2
4547 #define SPELLING_BOUNDS 3
4549 static struct spelling *spelling; /* Next stack element (unused). */
4550 static struct spelling *spelling_base; /* Spelling stack base. */
4551 static int spelling_size; /* Size of the spelling stack. */
4553 /* Macros to save and restore the spelling stack around push_... functions.
4554 Alternative to SAVE_SPELLING_STACK. */
4556 #define SPELLING_DEPTH() (spelling - spelling_base)
4557 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4559 /* Push an element on the spelling stack with type KIND and assign VALUE
4560 to MEMBER. */
4562 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4564 int depth = SPELLING_DEPTH (); \
4566 if (depth >= spelling_size) \
4568 spelling_size += 10; \
4569 if (spelling_base == 0) \
4570 spelling_base \
4571 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4572 else \
4573 spelling_base \
4574 = (struct spelling *) xrealloc (spelling_base, \
4575 spelling_size * sizeof (struct spelling)); \
4576 RESTORE_SPELLING_DEPTH (depth); \
4579 spelling->kind = (KIND); \
4580 spelling->MEMBER = (VALUE); \
4581 spelling++; \
4584 /* Push STRING on the stack. Printed literally. */
4586 static void
4587 push_string (string)
4588 const char *string;
4590 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4593 /* Push a member name on the stack. Printed as '.' STRING. */
4595 static void
4596 push_member_name (decl)
4597 tree decl;
4600 const char *const string
4601 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4602 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4605 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4607 static void
4608 push_array_bounds (bounds)
4609 int bounds;
4611 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4614 /* Compute the maximum size in bytes of the printed spelling. */
4616 static int
4617 spelling_length ()
4619 int size = 0;
4620 struct spelling *p;
4622 for (p = spelling_base; p < spelling; p++)
4624 if (p->kind == SPELLING_BOUNDS)
4625 size += 25;
4626 else
4627 size += strlen (p->u.s) + 1;
4630 return size;
4633 /* Print the spelling to BUFFER and return it. */
4635 static char *
4636 print_spelling (buffer)
4637 char *buffer;
4639 char *d = buffer;
4640 struct spelling *p;
4642 for (p = spelling_base; p < spelling; p++)
4643 if (p->kind == SPELLING_BOUNDS)
4645 sprintf (d, "[%d]", p->u.i);
4646 d += strlen (d);
4648 else
4650 const char *s;
4651 if (p->kind == SPELLING_MEMBER)
4652 *d++ = '.';
4653 for (s = p->u.s; (*d = *s++); d++)
4656 *d++ = '\0';
4657 return buffer;
4660 /* Issue an error message for a bad initializer component.
4661 MSGID identifies the message.
4662 The component name is taken from the spelling stack. */
4664 void
4665 error_init (msgid)
4666 const char *msgid;
4668 char *ofwhat;
4670 error ("%s", _(msgid));
4671 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4672 if (*ofwhat)
4673 error ("(near initialization for `%s')", ofwhat);
4676 /* Issue a pedantic warning for a bad initializer component.
4677 MSGID identifies the message.
4678 The component name is taken from the spelling stack. */
4680 void
4681 pedwarn_init (msgid)
4682 const char *msgid;
4684 char *ofwhat;
4686 pedwarn ("%s", _(msgid));
4687 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4688 if (*ofwhat)
4689 pedwarn ("(near initialization for `%s')", ofwhat);
4692 /* Issue a warning for a bad initializer component.
4693 MSGID identifies the message.
4694 The component name is taken from the spelling stack. */
4696 static void
4697 warning_init (msgid)
4698 const char *msgid;
4700 char *ofwhat;
4702 warning ("%s", _(msgid));
4703 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4704 if (*ofwhat)
4705 warning ("(near initialization for `%s')", ofwhat);
4708 /* Digest the parser output INIT as an initializer for type TYPE.
4709 Return a C expression of type TYPE to represent the initial value.
4711 REQUIRE_CONSTANT requests an error if non-constant initializers or
4712 elements are seen. */
4714 static tree
4715 digest_init (type, init, require_constant)
4716 tree type, init;
4717 int require_constant;
4719 enum tree_code code = TREE_CODE (type);
4720 tree inside_init = init;
4722 if (type == error_mark_node
4723 || init == error_mark_node
4724 || TREE_TYPE (init) == error_mark_node)
4725 return error_mark_node;
4727 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4728 /* Do not use STRIP_NOPS here. We do not want an enumerator
4729 whose value is 0 to count as a null pointer constant. */
4730 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4731 inside_init = TREE_OPERAND (init, 0);
4733 inside_init = fold (inside_init);
4735 /* Initialization of an array of chars from a string constant
4736 optionally enclosed in braces. */
4738 if (code == ARRAY_TYPE)
4740 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4741 if ((typ1 == char_type_node
4742 || typ1 == signed_char_type_node
4743 || typ1 == unsigned_char_type_node
4744 || typ1 == unsigned_wchar_type_node
4745 || typ1 == signed_wchar_type_node)
4746 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4748 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4749 TYPE_MAIN_VARIANT (type)))
4750 return inside_init;
4752 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4753 != char_type_node)
4754 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4756 error_init ("char-array initialized from wide string");
4757 return error_mark_node;
4759 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4760 == char_type_node)
4761 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4763 error_init ("int-array initialized from non-wide string");
4764 return error_mark_node;
4767 TREE_TYPE (inside_init) = type;
4768 if (TYPE_DOMAIN (type) != 0
4769 && TYPE_SIZE (type) != 0
4770 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4771 /* Subtract 1 (or sizeof (wchar_t))
4772 because it's ok to ignore the terminating null char
4773 that is counted in the length of the constant. */
4774 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4775 TREE_STRING_LENGTH (inside_init)
4776 - ((TYPE_PRECISION (typ1)
4777 != TYPE_PRECISION (char_type_node))
4778 ? (TYPE_PRECISION (wchar_type_node)
4779 / BITS_PER_UNIT)
4780 : 1)))
4781 pedwarn_init ("initializer-string for array of chars is too long");
4783 return inside_init;
4787 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4788 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4789 below and handle as a constructor. */
4790 if (code == VECTOR_TYPE
4791 && comptypes (TREE_TYPE (inside_init), type)
4792 && TREE_CONSTANT (inside_init))
4794 if (TREE_CODE (inside_init) == VECTOR_CST
4795 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4796 TYPE_MAIN_VARIANT (type)))
4797 return inside_init;
4798 else
4799 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4802 /* Any type can be initialized
4803 from an expression of the same type, optionally with braces. */
4805 if (inside_init && TREE_TYPE (inside_init) != 0
4806 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4807 TYPE_MAIN_VARIANT (type))
4808 || (code == ARRAY_TYPE
4809 && comptypes (TREE_TYPE (inside_init), type))
4810 || (code == VECTOR_TYPE
4811 && comptypes (TREE_TYPE (inside_init), type))
4812 || (code == POINTER_TYPE
4813 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4814 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4815 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4816 TREE_TYPE (type)))))
4818 if (code == POINTER_TYPE)
4819 inside_init = default_function_array_conversion (inside_init);
4821 if (require_constant && !flag_isoc99
4822 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4824 /* As an extension, allow initializing objects with static storage
4825 duration with compound literals (which are then treated just as
4826 the brace enclosed list they contain). */
4827 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4828 inside_init = DECL_INITIAL (decl);
4831 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4832 && TREE_CODE (inside_init) != CONSTRUCTOR)
4834 error_init ("array initialized from non-constant array expression");
4835 return error_mark_node;
4838 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4839 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4841 /* Compound expressions can only occur here if -pedantic or
4842 -pedantic-errors is specified. In the later case, we always want
4843 an error. In the former case, we simply want a warning. */
4844 if (require_constant && pedantic
4845 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4847 inside_init
4848 = valid_compound_expr_initializer (inside_init,
4849 TREE_TYPE (inside_init));
4850 if (inside_init == error_mark_node)
4851 error_init ("initializer element is not constant");
4852 else
4853 pedwarn_init ("initializer element is not constant");
4854 if (flag_pedantic_errors)
4855 inside_init = error_mark_node;
4857 else if (require_constant
4858 && (!TREE_CONSTANT (inside_init)
4859 /* This test catches things like `7 / 0' which
4860 result in an expression for which TREE_CONSTANT
4861 is true, but which is not actually something
4862 that is a legal constant. We really should not
4863 be using this function, because it is a part of
4864 the back-end. Instead, the expression should
4865 already have been turned into ERROR_MARK_NODE. */
4866 || !initializer_constant_valid_p (inside_init,
4867 TREE_TYPE (inside_init))))
4869 error_init ("initializer element is not constant");
4870 inside_init = error_mark_node;
4873 return inside_init;
4876 /* Handle scalar types, including conversions. */
4878 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4879 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4881 /* Note that convert_for_assignment calls default_conversion
4882 for arrays and functions. We must not call it in the
4883 case where inside_init is a null pointer constant. */
4884 inside_init
4885 = convert_for_assignment (type, init, _("initialization"),
4886 NULL_TREE, NULL_TREE, 0);
4888 if (require_constant && ! TREE_CONSTANT (inside_init))
4890 error_init ("initializer element is not constant");
4891 inside_init = error_mark_node;
4893 else if (require_constant
4894 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4896 error_init ("initializer element is not computable at load time");
4897 inside_init = error_mark_node;
4900 return inside_init;
4903 /* Come here only for records and arrays. */
4905 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4907 error_init ("variable-sized object may not be initialized");
4908 return error_mark_node;
4911 error_init ("invalid initializer");
4912 return error_mark_node;
4915 /* Handle initializers that use braces. */
4917 /* Type of object we are accumulating a constructor for.
4918 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4919 static tree constructor_type;
4921 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4922 left to fill. */
4923 static tree constructor_fields;
4925 /* For an ARRAY_TYPE, this is the specified index
4926 at which to store the next element we get. */
4927 static tree constructor_index;
4929 /* For an ARRAY_TYPE, this is the maximum index. */
4930 static tree constructor_max_index;
4932 /* For a RECORD_TYPE, this is the first field not yet written out. */
4933 static tree constructor_unfilled_fields;
4935 /* For an ARRAY_TYPE, this is the index of the first element
4936 not yet written out. */
4937 static tree constructor_unfilled_index;
4939 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4940 This is so we can generate gaps between fields, when appropriate. */
4941 static tree constructor_bit_index;
4943 /* If we are saving up the elements rather than allocating them,
4944 this is the list of elements so far (in reverse order,
4945 most recent first). */
4946 static tree constructor_elements;
4948 /* 1 if constructor should be incrementally stored into a constructor chain,
4949 0 if all the elements should be kept in AVL tree. */
4950 static int constructor_incremental;
4952 /* 1 if so far this constructor's elements are all compile-time constants. */
4953 static int constructor_constant;
4955 /* 1 if so far this constructor's elements are all valid address constants. */
4956 static int constructor_simple;
4958 /* 1 if this constructor is erroneous so far. */
4959 static int constructor_erroneous;
4961 /* Structure for managing pending initializer elements, organized as an
4962 AVL tree. */
4964 struct init_node
4966 struct init_node *left, *right;
4967 struct init_node *parent;
4968 int balance;
4969 tree purpose;
4970 tree value;
4973 /* Tree of pending elements at this constructor level.
4974 These are elements encountered out of order
4975 which belong at places we haven't reached yet in actually
4976 writing the output.
4977 Will never hold tree nodes across GC runs. */
4978 static struct init_node *constructor_pending_elts;
4980 /* The SPELLING_DEPTH of this constructor. */
4981 static int constructor_depth;
4983 /* 0 if implicitly pushing constructor levels is allowed. */
4984 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4986 static int require_constant_value;
4987 static int require_constant_elements;
4989 /* DECL node for which an initializer is being read.
4990 0 means we are reading a constructor expression
4991 such as (struct foo) {...}. */
4992 static tree constructor_decl;
4994 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4995 static const char *constructor_asmspec;
4997 /* Nonzero if this is an initializer for a top-level decl. */
4998 static int constructor_top_level;
5000 /* Nonzero if there were any member designators in this initializer. */
5001 static int constructor_designated;
5003 /* Nesting depth of designator list. */
5004 static int designator_depth;
5006 /* Nonzero if there were diagnosed errors in this designator list. */
5007 static int designator_errorneous;
5010 /* This stack has a level for each implicit or explicit level of
5011 structuring in the initializer, including the outermost one. It
5012 saves the values of most of the variables above. */
5014 struct constructor_range_stack;
5016 struct constructor_stack
5018 struct constructor_stack *next;
5019 tree type;
5020 tree fields;
5021 tree index;
5022 tree max_index;
5023 tree unfilled_index;
5024 tree unfilled_fields;
5025 tree bit_index;
5026 tree elements;
5027 struct init_node *pending_elts;
5028 int offset;
5029 int depth;
5030 /* If nonzero, this value should replace the entire
5031 constructor at this level. */
5032 tree replacement_value;
5033 struct constructor_range_stack *range_stack;
5034 char constant;
5035 char simple;
5036 char implicit;
5037 char erroneous;
5038 char outer;
5039 char incremental;
5040 char designated;
5043 struct constructor_stack *constructor_stack;
5045 /* This stack represents designators from some range designator up to
5046 the last designator in the list. */
5048 struct constructor_range_stack
5050 struct constructor_range_stack *next, *prev;
5051 struct constructor_stack *stack;
5052 tree range_start;
5053 tree index;
5054 tree range_end;
5055 tree fields;
5058 struct constructor_range_stack *constructor_range_stack;
5060 /* This stack records separate initializers that are nested.
5061 Nested initializers can't happen in ANSI C, but GNU C allows them
5062 in cases like { ... (struct foo) { ... } ... }. */
5064 struct initializer_stack
5066 struct initializer_stack *next;
5067 tree decl;
5068 const char *asmspec;
5069 struct constructor_stack *constructor_stack;
5070 struct constructor_range_stack *constructor_range_stack;
5071 tree elements;
5072 struct spelling *spelling;
5073 struct spelling *spelling_base;
5074 int spelling_size;
5075 char top_level;
5076 char require_constant_value;
5077 char require_constant_elements;
5080 struct initializer_stack *initializer_stack;
5082 /* Prepare to parse and output the initializer for variable DECL. */
5084 void
5085 start_init (decl, asmspec_tree, top_level)
5086 tree decl;
5087 tree asmspec_tree;
5088 int top_level;
5090 const char *locus;
5091 struct initializer_stack *p
5092 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5093 const char *asmspec = 0;
5095 if (asmspec_tree)
5096 asmspec = TREE_STRING_POINTER (asmspec_tree);
5098 p->decl = constructor_decl;
5099 p->asmspec = constructor_asmspec;
5100 p->require_constant_value = require_constant_value;
5101 p->require_constant_elements = require_constant_elements;
5102 p->constructor_stack = constructor_stack;
5103 p->constructor_range_stack = constructor_range_stack;
5104 p->elements = constructor_elements;
5105 p->spelling = spelling;
5106 p->spelling_base = spelling_base;
5107 p->spelling_size = spelling_size;
5108 p->top_level = constructor_top_level;
5109 p->next = initializer_stack;
5110 initializer_stack = p;
5112 constructor_decl = decl;
5113 constructor_asmspec = asmspec;
5114 constructor_designated = 0;
5115 constructor_top_level = top_level;
5117 if (decl != 0)
5119 require_constant_value = TREE_STATIC (decl);
5120 require_constant_elements
5121 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5122 /* For a scalar, you can always use any value to initialize,
5123 even within braces. */
5124 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5125 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5126 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5127 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5128 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5130 else
5132 require_constant_value = 0;
5133 require_constant_elements = 0;
5134 locus = "(anonymous)";
5137 constructor_stack = 0;
5138 constructor_range_stack = 0;
5140 missing_braces_mentioned = 0;
5142 spelling_base = 0;
5143 spelling_size = 0;
5144 RESTORE_SPELLING_DEPTH (0);
5146 if (locus)
5147 push_string (locus);
5150 void
5151 finish_init ()
5153 struct initializer_stack *p = initializer_stack;
5155 /* Free the whole constructor stack of this initializer. */
5156 while (constructor_stack)
5158 struct constructor_stack *q = constructor_stack;
5159 constructor_stack = q->next;
5160 free (q);
5163 if (constructor_range_stack)
5164 abort ();
5166 /* Pop back to the data of the outer initializer (if any). */
5167 constructor_decl = p->decl;
5168 constructor_asmspec = p->asmspec;
5169 require_constant_value = p->require_constant_value;
5170 require_constant_elements = p->require_constant_elements;
5171 constructor_stack = p->constructor_stack;
5172 constructor_range_stack = p->constructor_range_stack;
5173 constructor_elements = p->elements;
5174 spelling = p->spelling;
5175 spelling_base = p->spelling_base;
5176 spelling_size = p->spelling_size;
5177 constructor_top_level = p->top_level;
5178 initializer_stack = p->next;
5179 free (p);
5182 /* Call here when we see the initializer is surrounded by braces.
5183 This is instead of a call to push_init_level;
5184 it is matched by a call to pop_init_level.
5186 TYPE is the type to initialize, for a constructor expression.
5187 For an initializer for a decl, TYPE is zero. */
5189 void
5190 really_start_incremental_init (type)
5191 tree type;
5193 struct constructor_stack *p
5194 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5196 if (type == 0)
5197 type = TREE_TYPE (constructor_decl);
5199 if ((*targetm.vector_opaque_p) (type))
5200 error ("opaque vector types cannot be initialized");
5202 p->type = constructor_type;
5203 p->fields = constructor_fields;
5204 p->index = constructor_index;
5205 p->max_index = constructor_max_index;
5206 p->unfilled_index = constructor_unfilled_index;
5207 p->unfilled_fields = constructor_unfilled_fields;
5208 p->bit_index = constructor_bit_index;
5209 p->elements = constructor_elements;
5210 p->constant = constructor_constant;
5211 p->simple = constructor_simple;
5212 p->erroneous = constructor_erroneous;
5213 p->pending_elts = constructor_pending_elts;
5214 p->depth = constructor_depth;
5215 p->replacement_value = 0;
5216 p->implicit = 0;
5217 p->range_stack = 0;
5218 p->outer = 0;
5219 p->incremental = constructor_incremental;
5220 p->designated = constructor_designated;
5221 p->next = 0;
5222 constructor_stack = p;
5224 constructor_constant = 1;
5225 constructor_simple = 1;
5226 constructor_depth = SPELLING_DEPTH ();
5227 constructor_elements = 0;
5228 constructor_pending_elts = 0;
5229 constructor_type = type;
5230 constructor_incremental = 1;
5231 constructor_designated = 0;
5232 designator_depth = 0;
5233 designator_errorneous = 0;
5235 if (TREE_CODE (constructor_type) == RECORD_TYPE
5236 || TREE_CODE (constructor_type) == UNION_TYPE)
5238 constructor_fields = TYPE_FIELDS (constructor_type);
5239 /* Skip any nameless bit fields at the beginning. */
5240 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5241 && DECL_NAME (constructor_fields) == 0)
5242 constructor_fields = TREE_CHAIN (constructor_fields);
5244 constructor_unfilled_fields = constructor_fields;
5245 constructor_bit_index = bitsize_zero_node;
5247 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5249 if (TYPE_DOMAIN (constructor_type))
5251 constructor_max_index
5252 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5254 /* Detect non-empty initializations of zero-length arrays. */
5255 if (constructor_max_index == NULL_TREE
5256 && TYPE_SIZE (constructor_type))
5257 constructor_max_index = build_int_2 (-1, -1);
5259 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5260 to initialize VLAs will cause a proper error; avoid tree
5261 checking errors as well by setting a safe value. */
5262 if (constructor_max_index
5263 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5264 constructor_max_index = build_int_2 (-1, -1);
5266 constructor_index
5267 = convert (bitsizetype,
5268 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5270 else
5271 constructor_index = bitsize_zero_node;
5273 constructor_unfilled_index = constructor_index;
5275 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5277 /* Vectors are like simple fixed-size arrays. */
5278 constructor_max_index =
5279 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5280 constructor_index = convert (bitsizetype, bitsize_zero_node);
5281 constructor_unfilled_index = constructor_index;
5283 else
5285 /* Handle the case of int x = {5}; */
5286 constructor_fields = constructor_type;
5287 constructor_unfilled_fields = constructor_type;
5291 /* Push down into a subobject, for initialization.
5292 If this is for an explicit set of braces, IMPLICIT is 0.
5293 If it is because the next element belongs at a lower level,
5294 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5296 void
5297 push_init_level (implicit)
5298 int implicit;
5300 struct constructor_stack *p;
5301 tree value = NULL_TREE;
5303 /* If we've exhausted any levels that didn't have braces,
5304 pop them now. */
5305 while (constructor_stack->implicit)
5307 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5308 || TREE_CODE (constructor_type) == UNION_TYPE)
5309 && constructor_fields == 0)
5310 process_init_element (pop_init_level (1));
5311 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5312 && constructor_max_index
5313 && tree_int_cst_lt (constructor_max_index, constructor_index))
5314 process_init_element (pop_init_level (1));
5315 else
5316 break;
5319 /* Unless this is an explicit brace, we need to preserve previous
5320 content if any. */
5321 if (implicit)
5323 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5324 || TREE_CODE (constructor_type) == UNION_TYPE)
5325 && constructor_fields)
5326 value = find_init_member (constructor_fields);
5327 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5328 value = find_init_member (constructor_index);
5331 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5332 p->type = constructor_type;
5333 p->fields = constructor_fields;
5334 p->index = constructor_index;
5335 p->max_index = constructor_max_index;
5336 p->unfilled_index = constructor_unfilled_index;
5337 p->unfilled_fields = constructor_unfilled_fields;
5338 p->bit_index = constructor_bit_index;
5339 p->elements = constructor_elements;
5340 p->constant = constructor_constant;
5341 p->simple = constructor_simple;
5342 p->erroneous = constructor_erroneous;
5343 p->pending_elts = constructor_pending_elts;
5344 p->depth = constructor_depth;
5345 p->replacement_value = 0;
5346 p->implicit = implicit;
5347 p->outer = 0;
5348 p->incremental = constructor_incremental;
5349 p->designated = constructor_designated;
5350 p->next = constructor_stack;
5351 p->range_stack = 0;
5352 constructor_stack = p;
5354 constructor_constant = 1;
5355 constructor_simple = 1;
5356 constructor_depth = SPELLING_DEPTH ();
5357 constructor_elements = 0;
5358 constructor_incremental = 1;
5359 constructor_designated = 0;
5360 constructor_pending_elts = 0;
5361 if (!implicit)
5363 p->range_stack = constructor_range_stack;
5364 constructor_range_stack = 0;
5365 designator_depth = 0;
5366 designator_errorneous = 0;
5369 /* Don't die if an entire brace-pair level is superfluous
5370 in the containing level. */
5371 if (constructor_type == 0)
5373 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5374 || TREE_CODE (constructor_type) == UNION_TYPE)
5376 /* Don't die if there are extra init elts at the end. */
5377 if (constructor_fields == 0)
5378 constructor_type = 0;
5379 else
5381 constructor_type = TREE_TYPE (constructor_fields);
5382 push_member_name (constructor_fields);
5383 constructor_depth++;
5386 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5388 constructor_type = TREE_TYPE (constructor_type);
5389 push_array_bounds (tree_low_cst (constructor_index, 0));
5390 constructor_depth++;
5393 if (constructor_type == 0)
5395 error_init ("extra brace group at end of initializer");
5396 constructor_fields = 0;
5397 constructor_unfilled_fields = 0;
5398 return;
5401 if (value && TREE_CODE (value) == CONSTRUCTOR)
5403 constructor_constant = TREE_CONSTANT (value);
5404 constructor_simple = TREE_STATIC (value);
5405 constructor_elements = CONSTRUCTOR_ELTS (value);
5406 if (constructor_elements
5407 && (TREE_CODE (constructor_type) == RECORD_TYPE
5408 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5409 set_nonincremental_init ();
5412 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5414 missing_braces_mentioned = 1;
5415 warning_init ("missing braces around initializer");
5418 if (TREE_CODE (constructor_type) == RECORD_TYPE
5419 || TREE_CODE (constructor_type) == UNION_TYPE)
5421 constructor_fields = TYPE_FIELDS (constructor_type);
5422 /* Skip any nameless bit fields at the beginning. */
5423 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5424 && DECL_NAME (constructor_fields) == 0)
5425 constructor_fields = TREE_CHAIN (constructor_fields);
5427 constructor_unfilled_fields = constructor_fields;
5428 constructor_bit_index = bitsize_zero_node;
5430 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5432 /* Vectors are like simple fixed-size arrays. */
5433 constructor_max_index =
5434 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5435 constructor_index = convert (bitsizetype, integer_zero_node);
5436 constructor_unfilled_index = constructor_index;
5438 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5440 if (TYPE_DOMAIN (constructor_type))
5442 constructor_max_index
5443 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5445 /* Detect non-empty initializations of zero-length arrays. */
5446 if (constructor_max_index == NULL_TREE
5447 && TYPE_SIZE (constructor_type))
5448 constructor_max_index = build_int_2 (-1, -1);
5450 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5451 to initialize VLAs will cause a proper error; avoid tree
5452 checking errors as well by setting a safe value. */
5453 if (constructor_max_index
5454 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5455 constructor_max_index = build_int_2 (-1, -1);
5457 constructor_index
5458 = convert (bitsizetype,
5459 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5461 else
5462 constructor_index = bitsize_zero_node;
5464 constructor_unfilled_index = constructor_index;
5465 if (value && TREE_CODE (value) == STRING_CST)
5467 /* We need to split the char/wchar array into individual
5468 characters, so that we don't have to special case it
5469 everywhere. */
5470 set_nonincremental_init_from_string (value);
5473 else
5475 warning_init ("braces around scalar initializer");
5476 constructor_fields = constructor_type;
5477 constructor_unfilled_fields = constructor_type;
5481 /* At the end of an implicit or explicit brace level,
5482 finish up that level of constructor.
5483 If we were outputting the elements as they are read, return 0
5484 from inner levels (process_init_element ignores that),
5485 but return error_mark_node from the outermost level
5486 (that's what we want to put in DECL_INITIAL).
5487 Otherwise, return a CONSTRUCTOR expression. */
5489 tree
5490 pop_init_level (implicit)
5491 int implicit;
5493 struct constructor_stack *p;
5494 tree constructor = 0;
5496 if (implicit == 0)
5498 /* When we come to an explicit close brace,
5499 pop any inner levels that didn't have explicit braces. */
5500 while (constructor_stack->implicit)
5501 process_init_element (pop_init_level (1));
5503 if (constructor_range_stack)
5504 abort ();
5507 p = constructor_stack;
5509 /* Error for initializing a flexible array member, or a zero-length
5510 array member in an inappropriate context. */
5511 if (constructor_type && constructor_fields
5512 && TREE_CODE (constructor_type) == ARRAY_TYPE
5513 && TYPE_DOMAIN (constructor_type)
5514 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5516 /* Silently discard empty initializations. The parser will
5517 already have pedwarned for empty brackets. */
5518 if (integer_zerop (constructor_unfilled_index))
5519 constructor_type = NULL_TREE;
5520 else if (! TYPE_SIZE (constructor_type))
5522 if (constructor_depth > 2)
5523 error_init ("initialization of flexible array member in a nested context");
5524 else if (pedantic)
5525 pedwarn_init ("initialization of a flexible array member");
5527 /* We have already issued an error message for the existence
5528 of a flexible array member not at the end of the structure.
5529 Discard the initializer so that we do not abort later. */
5530 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5531 constructor_type = NULL_TREE;
5533 else
5534 /* Zero-length arrays are no longer special, so we should no longer
5535 get here. */
5536 abort ();
5539 /* Warn when some struct elements are implicitly initialized to zero. */
5540 if (extra_warnings
5541 && constructor_type
5542 && TREE_CODE (constructor_type) == RECORD_TYPE
5543 && constructor_unfilled_fields)
5545 /* Do not warn for flexible array members or zero-length arrays. */
5546 while (constructor_unfilled_fields
5547 && (! DECL_SIZE (constructor_unfilled_fields)
5548 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5549 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5551 /* Do not warn if this level of the initializer uses member
5552 designators; it is likely to be deliberate. */
5553 if (constructor_unfilled_fields && !constructor_designated)
5555 push_member_name (constructor_unfilled_fields);
5556 warning_init ("missing initializer");
5557 RESTORE_SPELLING_DEPTH (constructor_depth);
5561 /* Now output all pending elements. */
5562 constructor_incremental = 1;
5563 output_pending_init_elements (1);
5565 /* Pad out the end of the structure. */
5566 if (p->replacement_value)
5567 /* If this closes a superfluous brace pair,
5568 just pass out the element between them. */
5569 constructor = p->replacement_value;
5570 else if (constructor_type == 0)
5572 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5573 && TREE_CODE (constructor_type) != UNION_TYPE
5574 && TREE_CODE (constructor_type) != ARRAY_TYPE
5575 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5577 /* A nonincremental scalar initializer--just return
5578 the element, after verifying there is just one. */
5579 if (constructor_elements == 0)
5581 if (!constructor_erroneous)
5582 error_init ("empty scalar initializer");
5583 constructor = error_mark_node;
5585 else if (TREE_CHAIN (constructor_elements) != 0)
5587 error_init ("extra elements in scalar initializer");
5588 constructor = TREE_VALUE (constructor_elements);
5590 else
5591 constructor = TREE_VALUE (constructor_elements);
5593 else
5595 if (constructor_erroneous)
5596 constructor = error_mark_node;
5597 else
5599 constructor = build_constructor (constructor_type,
5600 nreverse (constructor_elements));
5601 if (constructor_constant)
5602 TREE_CONSTANT (constructor) = 1;
5603 if (constructor_constant && constructor_simple)
5604 TREE_STATIC (constructor) = 1;
5608 constructor_type = p->type;
5609 constructor_fields = p->fields;
5610 constructor_index = p->index;
5611 constructor_max_index = p->max_index;
5612 constructor_unfilled_index = p->unfilled_index;
5613 constructor_unfilled_fields = p->unfilled_fields;
5614 constructor_bit_index = p->bit_index;
5615 constructor_elements = p->elements;
5616 constructor_constant = p->constant;
5617 constructor_simple = p->simple;
5618 constructor_erroneous = p->erroneous;
5619 constructor_incremental = p->incremental;
5620 constructor_designated = p->designated;
5621 constructor_pending_elts = p->pending_elts;
5622 constructor_depth = p->depth;
5623 if (!p->implicit)
5624 constructor_range_stack = p->range_stack;
5625 RESTORE_SPELLING_DEPTH (constructor_depth);
5627 constructor_stack = p->next;
5628 free (p);
5630 if (constructor == 0)
5632 if (constructor_stack == 0)
5633 return error_mark_node;
5634 return NULL_TREE;
5636 return constructor;
5639 /* Common handling for both array range and field name designators.
5640 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5642 static int
5643 set_designator (array)
5644 int array;
5646 tree subtype;
5647 enum tree_code subcode;
5649 /* Don't die if an entire brace-pair level is superfluous
5650 in the containing level. */
5651 if (constructor_type == 0)
5652 return 1;
5654 /* If there were errors in this designator list already, bail out silently. */
5655 if (designator_errorneous)
5656 return 1;
5658 if (!designator_depth)
5660 if (constructor_range_stack)
5661 abort ();
5663 /* Designator list starts at the level of closest explicit
5664 braces. */
5665 while (constructor_stack->implicit)
5666 process_init_element (pop_init_level (1));
5667 constructor_designated = 1;
5668 return 0;
5671 if (constructor_no_implicit)
5673 error_init ("initialization designators may not nest");
5674 return 1;
5677 if (TREE_CODE (constructor_type) == RECORD_TYPE
5678 || TREE_CODE (constructor_type) == UNION_TYPE)
5680 subtype = TREE_TYPE (constructor_fields);
5681 if (subtype != error_mark_node)
5682 subtype = TYPE_MAIN_VARIANT (subtype);
5684 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5686 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5688 else
5689 abort ();
5691 subcode = TREE_CODE (subtype);
5692 if (array && subcode != ARRAY_TYPE)
5694 error_init ("array index in non-array initializer");
5695 return 1;
5697 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5699 error_init ("field name not in record or union initializer");
5700 return 1;
5703 constructor_designated = 1;
5704 push_init_level (2);
5705 return 0;
5708 /* If there are range designators in designator list, push a new designator
5709 to constructor_range_stack. RANGE_END is end of such stack range or
5710 NULL_TREE if there is no range designator at this level. */
5712 static void
5713 push_range_stack (range_end)
5714 tree range_end;
5716 struct constructor_range_stack *p;
5718 p = (struct constructor_range_stack *)
5719 ggc_alloc (sizeof (struct constructor_range_stack));
5720 p->prev = constructor_range_stack;
5721 p->next = 0;
5722 p->fields = constructor_fields;
5723 p->range_start = constructor_index;
5724 p->index = constructor_index;
5725 p->stack = constructor_stack;
5726 p->range_end = range_end;
5727 if (constructor_range_stack)
5728 constructor_range_stack->next = p;
5729 constructor_range_stack = p;
5732 /* Within an array initializer, specify the next index to be initialized.
5733 FIRST is that index. If LAST is nonzero, then initialize a range
5734 of indices, running from FIRST through LAST. */
5736 void
5737 set_init_index (first, last)
5738 tree first, last;
5740 if (set_designator (1))
5741 return;
5743 designator_errorneous = 1;
5745 while ((TREE_CODE (first) == NOP_EXPR
5746 || TREE_CODE (first) == CONVERT_EXPR
5747 || TREE_CODE (first) == NON_LVALUE_EXPR)
5748 && (TYPE_MODE (TREE_TYPE (first))
5749 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5750 first = TREE_OPERAND (first, 0);
5752 if (last)
5753 while ((TREE_CODE (last) == NOP_EXPR
5754 || TREE_CODE (last) == CONVERT_EXPR
5755 || TREE_CODE (last) == NON_LVALUE_EXPR)
5756 && (TYPE_MODE (TREE_TYPE (last))
5757 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5758 last = TREE_OPERAND (last, 0);
5760 if (TREE_CODE (first) != INTEGER_CST)
5761 error_init ("nonconstant array index in initializer");
5762 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5763 error_init ("nonconstant array index in initializer");
5764 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5765 error_init ("array index in non-array initializer");
5766 else if (constructor_max_index
5767 && tree_int_cst_lt (constructor_max_index, first))
5768 error_init ("array index in initializer exceeds array bounds");
5769 else
5771 constructor_index = convert (bitsizetype, first);
5773 if (last)
5775 if (tree_int_cst_equal (first, last))
5776 last = 0;
5777 else if (tree_int_cst_lt (last, first))
5779 error_init ("empty index range in initializer");
5780 last = 0;
5782 else
5784 last = convert (bitsizetype, last);
5785 if (constructor_max_index != 0
5786 && tree_int_cst_lt (constructor_max_index, last))
5788 error_init ("array index range in initializer exceeds array bounds");
5789 last = 0;
5794 designator_depth++;
5795 designator_errorneous = 0;
5796 if (constructor_range_stack || last)
5797 push_range_stack (last);
5801 /* Within a struct initializer, specify the next field to be initialized. */
5803 void
5804 set_init_label (fieldname)
5805 tree fieldname;
5807 tree tail;
5809 if (set_designator (0))
5810 return;
5812 designator_errorneous = 1;
5814 if (TREE_CODE (constructor_type) != RECORD_TYPE
5815 && TREE_CODE (constructor_type) != UNION_TYPE)
5817 error_init ("field name not in record or union initializer");
5818 return;
5821 for (tail = TYPE_FIELDS (constructor_type); tail;
5822 tail = TREE_CHAIN (tail))
5824 if (DECL_NAME (tail) == fieldname)
5825 break;
5828 if (tail == 0)
5829 error ("unknown field `%s' specified in initializer",
5830 IDENTIFIER_POINTER (fieldname));
5831 else
5833 constructor_fields = tail;
5834 designator_depth++;
5835 designator_errorneous = 0;
5836 if (constructor_range_stack)
5837 push_range_stack (NULL_TREE);
5841 /* Add a new initializer to the tree of pending initializers. PURPOSE
5842 identifies the initializer, either array index or field in a structure.
5843 VALUE is the value of that index or field. */
5845 static void
5846 add_pending_init (purpose, value)
5847 tree purpose, value;
5849 struct init_node *p, **q, *r;
5851 q = &constructor_pending_elts;
5852 p = 0;
5854 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5856 while (*q != 0)
5858 p = *q;
5859 if (tree_int_cst_lt (purpose, p->purpose))
5860 q = &p->left;
5861 else if (tree_int_cst_lt (p->purpose, purpose))
5862 q = &p->right;
5863 else
5865 if (TREE_SIDE_EFFECTS (p->value))
5866 warning_init ("initialized field with side-effects overwritten");
5867 p->value = value;
5868 return;
5872 else
5874 tree bitpos;
5876 bitpos = bit_position (purpose);
5877 while (*q != NULL)
5879 p = *q;
5880 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5881 q = &p->left;
5882 else if (p->purpose != purpose)
5883 q = &p->right;
5884 else
5886 if (TREE_SIDE_EFFECTS (p->value))
5887 warning_init ("initialized field with side-effects overwritten");
5888 p->value = value;
5889 return;
5894 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5895 r->purpose = purpose;
5896 r->value = value;
5898 *q = r;
5899 r->parent = p;
5900 r->left = 0;
5901 r->right = 0;
5902 r->balance = 0;
5904 while (p)
5906 struct init_node *s;
5908 if (r == p->left)
5910 if (p->balance == 0)
5911 p->balance = -1;
5912 else if (p->balance < 0)
5914 if (r->balance < 0)
5916 /* L rotation. */
5917 p->left = r->right;
5918 if (p->left)
5919 p->left->parent = p;
5920 r->right = p;
5922 p->balance = 0;
5923 r->balance = 0;
5925 s = p->parent;
5926 p->parent = r;
5927 r->parent = s;
5928 if (s)
5930 if (s->left == p)
5931 s->left = r;
5932 else
5933 s->right = r;
5935 else
5936 constructor_pending_elts = r;
5938 else
5940 /* LR rotation. */
5941 struct init_node *t = r->right;
5943 r->right = t->left;
5944 if (r->right)
5945 r->right->parent = r;
5946 t->left = r;
5948 p->left = t->right;
5949 if (p->left)
5950 p->left->parent = p;
5951 t->right = p;
5953 p->balance = t->balance < 0;
5954 r->balance = -(t->balance > 0);
5955 t->balance = 0;
5957 s = p->parent;
5958 p->parent = t;
5959 r->parent = t;
5960 t->parent = s;
5961 if (s)
5963 if (s->left == p)
5964 s->left = t;
5965 else
5966 s->right = t;
5968 else
5969 constructor_pending_elts = t;
5971 break;
5973 else
5975 /* p->balance == +1; growth of left side balances the node. */
5976 p->balance = 0;
5977 break;
5980 else /* r == p->right */
5982 if (p->balance == 0)
5983 /* Growth propagation from right side. */
5984 p->balance++;
5985 else if (p->balance > 0)
5987 if (r->balance > 0)
5989 /* R rotation. */
5990 p->right = r->left;
5991 if (p->right)
5992 p->right->parent = p;
5993 r->left = p;
5995 p->balance = 0;
5996 r->balance = 0;
5998 s = p->parent;
5999 p->parent = r;
6000 r->parent = s;
6001 if (s)
6003 if (s->left == p)
6004 s->left = r;
6005 else
6006 s->right = r;
6008 else
6009 constructor_pending_elts = r;
6011 else /* r->balance == -1 */
6013 /* RL rotation */
6014 struct init_node *t = r->left;
6016 r->left = t->right;
6017 if (r->left)
6018 r->left->parent = r;
6019 t->right = r;
6021 p->right = t->left;
6022 if (p->right)
6023 p->right->parent = p;
6024 t->left = p;
6026 r->balance = (t->balance < 0);
6027 p->balance = -(t->balance > 0);
6028 t->balance = 0;
6030 s = p->parent;
6031 p->parent = t;
6032 r->parent = t;
6033 t->parent = s;
6034 if (s)
6036 if (s->left == p)
6037 s->left = t;
6038 else
6039 s->right = t;
6041 else
6042 constructor_pending_elts = t;
6044 break;
6046 else
6048 /* p->balance == -1; growth of right side balances the node. */
6049 p->balance = 0;
6050 break;
6054 r = p;
6055 p = p->parent;
6059 /* Build AVL tree from a sorted chain. */
6061 static void
6062 set_nonincremental_init ()
6064 tree chain;
6066 if (TREE_CODE (constructor_type) != RECORD_TYPE
6067 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6068 return;
6070 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6071 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6072 constructor_elements = 0;
6073 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6075 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6076 /* Skip any nameless bit fields at the beginning. */
6077 while (constructor_unfilled_fields != 0
6078 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6079 && DECL_NAME (constructor_unfilled_fields) == 0)
6080 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6083 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6085 if (TYPE_DOMAIN (constructor_type))
6086 constructor_unfilled_index
6087 = convert (bitsizetype,
6088 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6089 else
6090 constructor_unfilled_index = bitsize_zero_node;
6092 constructor_incremental = 0;
6095 /* Build AVL tree from a string constant. */
6097 static void
6098 set_nonincremental_init_from_string (str)
6099 tree str;
6101 tree value, purpose, type;
6102 HOST_WIDE_INT val[2];
6103 const char *p, *end;
6104 int byte, wchar_bytes, charwidth, bitpos;
6106 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6107 abort ();
6109 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6110 == TYPE_PRECISION (char_type_node))
6111 wchar_bytes = 1;
6112 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6113 == TYPE_PRECISION (wchar_type_node))
6114 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6115 else
6116 abort ();
6118 charwidth = TYPE_PRECISION (char_type_node);
6119 type = TREE_TYPE (constructor_type);
6120 p = TREE_STRING_POINTER (str);
6121 end = p + TREE_STRING_LENGTH (str);
6123 for (purpose = bitsize_zero_node;
6124 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6125 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6127 if (wchar_bytes == 1)
6129 val[1] = (unsigned char) *p++;
6130 val[0] = 0;
6132 else
6134 val[0] = 0;
6135 val[1] = 0;
6136 for (byte = 0; byte < wchar_bytes; byte++)
6138 if (BYTES_BIG_ENDIAN)
6139 bitpos = (wchar_bytes - byte - 1) * charwidth;
6140 else
6141 bitpos = byte * charwidth;
6142 val[bitpos < HOST_BITS_PER_WIDE_INT]
6143 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6144 << (bitpos % HOST_BITS_PER_WIDE_INT);
6148 if (!TREE_UNSIGNED (type))
6150 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6151 if (bitpos < HOST_BITS_PER_WIDE_INT)
6153 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6155 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6156 val[0] = -1;
6159 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6161 if (val[1] < 0)
6162 val[0] = -1;
6164 else if (val[0] & (((HOST_WIDE_INT) 1)
6165 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6166 val[0] |= ((HOST_WIDE_INT) -1)
6167 << (bitpos - HOST_BITS_PER_WIDE_INT);
6170 value = build_int_2 (val[1], val[0]);
6171 TREE_TYPE (value) = type;
6172 add_pending_init (purpose, value);
6175 constructor_incremental = 0;
6178 /* Return value of FIELD in pending initializer or zero if the field was
6179 not initialized yet. */
6181 static tree
6182 find_init_member (field)
6183 tree field;
6185 struct init_node *p;
6187 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6189 if (constructor_incremental
6190 && tree_int_cst_lt (field, constructor_unfilled_index))
6191 set_nonincremental_init ();
6193 p = constructor_pending_elts;
6194 while (p)
6196 if (tree_int_cst_lt (field, p->purpose))
6197 p = p->left;
6198 else if (tree_int_cst_lt (p->purpose, field))
6199 p = p->right;
6200 else
6201 return p->value;
6204 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6206 tree bitpos = bit_position (field);
6208 if (constructor_incremental
6209 && (!constructor_unfilled_fields
6210 || tree_int_cst_lt (bitpos,
6211 bit_position (constructor_unfilled_fields))))
6212 set_nonincremental_init ();
6214 p = constructor_pending_elts;
6215 while (p)
6217 if (field == p->purpose)
6218 return p->value;
6219 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6220 p = p->left;
6221 else
6222 p = p->right;
6225 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6227 if (constructor_elements
6228 && TREE_PURPOSE (constructor_elements) == field)
6229 return TREE_VALUE (constructor_elements);
6231 return 0;
6234 /* "Output" the next constructor element.
6235 At top level, really output it to assembler code now.
6236 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6237 TYPE is the data type that the containing data type wants here.
6238 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6240 PENDING if non-nil means output pending elements that belong
6241 right after this element. (PENDING is normally 1;
6242 it is 0 while outputting pending elements, to avoid recursion.) */
6244 static void
6245 output_init_element (value, type, field, pending)
6246 tree value, type, field;
6247 int pending;
6249 if (type == error_mark_node)
6251 constructor_erroneous = 1;
6252 return;
6254 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6255 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6256 && !(TREE_CODE (value) == STRING_CST
6257 && TREE_CODE (type) == ARRAY_TYPE
6258 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6259 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6260 TYPE_MAIN_VARIANT (type))))
6261 value = default_conversion (value);
6263 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6264 && require_constant_value && !flag_isoc99 && pending)
6266 /* As an extension, allow initializing objects with static storage
6267 duration with compound literals (which are then treated just as
6268 the brace enclosed list they contain). */
6269 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6270 value = DECL_INITIAL (decl);
6273 if (value == error_mark_node)
6274 constructor_erroneous = 1;
6275 else if (!TREE_CONSTANT (value))
6276 constructor_constant = 0;
6277 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6278 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6279 || TREE_CODE (constructor_type) == UNION_TYPE)
6280 && DECL_C_BIT_FIELD (field)
6281 && TREE_CODE (value) != INTEGER_CST))
6282 constructor_simple = 0;
6284 if (require_constant_value && ! TREE_CONSTANT (value))
6286 error_init ("initializer element is not constant");
6287 value = error_mark_node;
6289 else if (require_constant_elements
6290 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6291 pedwarn ("initializer element is not computable at load time");
6293 /* If this field is empty (and not at the end of structure),
6294 don't do anything other than checking the initializer. */
6295 if (field
6296 && (TREE_TYPE (field) == error_mark_node
6297 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6298 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6299 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6300 || TREE_CHAIN (field)))))
6301 return;
6303 value = digest_init (type, value, require_constant_value);
6304 if (value == error_mark_node)
6306 constructor_erroneous = 1;
6307 return;
6310 /* If this element doesn't come next in sequence,
6311 put it on constructor_pending_elts. */
6312 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6313 && (!constructor_incremental
6314 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6316 if (constructor_incremental
6317 && tree_int_cst_lt (field, constructor_unfilled_index))
6318 set_nonincremental_init ();
6320 add_pending_init (field, value);
6321 return;
6323 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6324 && (!constructor_incremental
6325 || field != constructor_unfilled_fields))
6327 /* We do this for records but not for unions. In a union,
6328 no matter which field is specified, it can be initialized
6329 right away since it starts at the beginning of the union. */
6330 if (constructor_incremental)
6332 if (!constructor_unfilled_fields)
6333 set_nonincremental_init ();
6334 else
6336 tree bitpos, unfillpos;
6338 bitpos = bit_position (field);
6339 unfillpos = bit_position (constructor_unfilled_fields);
6341 if (tree_int_cst_lt (bitpos, unfillpos))
6342 set_nonincremental_init ();
6346 add_pending_init (field, value);
6347 return;
6349 else if (TREE_CODE (constructor_type) == UNION_TYPE
6350 && constructor_elements)
6352 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6353 warning_init ("initialized field with side-effects overwritten");
6355 /* We can have just one union field set. */
6356 constructor_elements = 0;
6359 /* Otherwise, output this element either to
6360 constructor_elements or to the assembler file. */
6362 if (field && TREE_CODE (field) == INTEGER_CST)
6363 field = copy_node (field);
6364 constructor_elements
6365 = tree_cons (field, value, constructor_elements);
6367 /* Advance the variable that indicates sequential elements output. */
6368 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6369 constructor_unfilled_index
6370 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6371 bitsize_one_node);
6372 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6374 constructor_unfilled_fields
6375 = TREE_CHAIN (constructor_unfilled_fields);
6377 /* Skip any nameless bit fields. */
6378 while (constructor_unfilled_fields != 0
6379 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6380 && DECL_NAME (constructor_unfilled_fields) == 0)
6381 constructor_unfilled_fields =
6382 TREE_CHAIN (constructor_unfilled_fields);
6384 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6385 constructor_unfilled_fields = 0;
6387 /* Now output any pending elements which have become next. */
6388 if (pending)
6389 output_pending_init_elements (0);
6392 /* Output any pending elements which have become next.
6393 As we output elements, constructor_unfilled_{fields,index}
6394 advances, which may cause other elements to become next;
6395 if so, they too are output.
6397 If ALL is 0, we return when there are
6398 no more pending elements to output now.
6400 If ALL is 1, we output space as necessary so that
6401 we can output all the pending elements. */
6403 static void
6404 output_pending_init_elements (all)
6405 int all;
6407 struct init_node *elt = constructor_pending_elts;
6408 tree next;
6410 retry:
6412 /* Look thru the whole pending tree.
6413 If we find an element that should be output now,
6414 output it. Otherwise, set NEXT to the element
6415 that comes first among those still pending. */
6417 next = 0;
6418 while (elt)
6420 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6422 if (tree_int_cst_equal (elt->purpose,
6423 constructor_unfilled_index))
6424 output_init_element (elt->value,
6425 TREE_TYPE (constructor_type),
6426 constructor_unfilled_index, 0);
6427 else if (tree_int_cst_lt (constructor_unfilled_index,
6428 elt->purpose))
6430 /* Advance to the next smaller node. */
6431 if (elt->left)
6432 elt = elt->left;
6433 else
6435 /* We have reached the smallest node bigger than the
6436 current unfilled index. Fill the space first. */
6437 next = elt->purpose;
6438 break;
6441 else
6443 /* Advance to the next bigger node. */
6444 if (elt->right)
6445 elt = elt->right;
6446 else
6448 /* We have reached the biggest node in a subtree. Find
6449 the parent of it, which is the next bigger node. */
6450 while (elt->parent && elt->parent->right == elt)
6451 elt = elt->parent;
6452 elt = elt->parent;
6453 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6454 elt->purpose))
6456 next = elt->purpose;
6457 break;
6462 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6463 || TREE_CODE (constructor_type) == UNION_TYPE)
6465 tree ctor_unfilled_bitpos, elt_bitpos;
6467 /* If the current record is complete we are done. */
6468 if (constructor_unfilled_fields == 0)
6469 break;
6471 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6472 elt_bitpos = bit_position (elt->purpose);
6473 /* We can't compare fields here because there might be empty
6474 fields in between. */
6475 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6477 constructor_unfilled_fields = elt->purpose;
6478 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6479 elt->purpose, 0);
6481 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6483 /* Advance to the next smaller node. */
6484 if (elt->left)
6485 elt = elt->left;
6486 else
6488 /* We have reached the smallest node bigger than the
6489 current unfilled field. Fill the space first. */
6490 next = elt->purpose;
6491 break;
6494 else
6496 /* Advance to the next bigger node. */
6497 if (elt->right)
6498 elt = elt->right;
6499 else
6501 /* We have reached the biggest node in a subtree. Find
6502 the parent of it, which is the next bigger node. */
6503 while (elt->parent && elt->parent->right == elt)
6504 elt = elt->parent;
6505 elt = elt->parent;
6506 if (elt
6507 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6508 bit_position (elt->purpose))))
6510 next = elt->purpose;
6511 break;
6518 /* Ordinarily return, but not if we want to output all
6519 and there are elements left. */
6520 if (! (all && next != 0))
6521 return;
6523 /* If it's not incremental, just skip over the gap, so that after
6524 jumping to retry we will output the next successive element. */
6525 if (TREE_CODE (constructor_type) == RECORD_TYPE
6526 || TREE_CODE (constructor_type) == UNION_TYPE)
6527 constructor_unfilled_fields = next;
6528 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6529 constructor_unfilled_index = next;
6531 /* ELT now points to the node in the pending tree with the next
6532 initializer to output. */
6533 goto retry;
6536 /* Add one non-braced element to the current constructor level.
6537 This adjusts the current position within the constructor's type.
6538 This may also start or terminate implicit levels
6539 to handle a partly-braced initializer.
6541 Once this has found the correct level for the new element,
6542 it calls output_init_element. */
6544 void
6545 process_init_element (value)
6546 tree value;
6548 tree orig_value = value;
6549 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6551 designator_depth = 0;
6552 designator_errorneous = 0;
6554 /* Handle superfluous braces around string cst as in
6555 char x[] = {"foo"}; */
6556 if (string_flag
6557 && constructor_type
6558 && TREE_CODE (constructor_type) == ARRAY_TYPE
6559 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6560 && integer_zerop (constructor_unfilled_index))
6562 if (constructor_stack->replacement_value)
6563 error_init ("excess elements in char array initializer");
6564 constructor_stack->replacement_value = value;
6565 return;
6568 if (constructor_stack->replacement_value != 0)
6570 error_init ("excess elements in struct initializer");
6571 return;
6574 /* Ignore elements of a brace group if it is entirely superfluous
6575 and has already been diagnosed. */
6576 if (constructor_type == 0)
6577 return;
6579 /* If we've exhausted any levels that didn't have braces,
6580 pop them now. */
6581 while (constructor_stack->implicit)
6583 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6584 || TREE_CODE (constructor_type) == UNION_TYPE)
6585 && constructor_fields == 0)
6586 process_init_element (pop_init_level (1));
6587 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6588 && (constructor_max_index == 0
6589 || tree_int_cst_lt (constructor_max_index,
6590 constructor_index)))
6591 process_init_element (pop_init_level (1));
6592 else
6593 break;
6596 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6597 if (constructor_range_stack)
6599 /* If value is a compound literal and we'll be just using its
6600 content, don't put it into a SAVE_EXPR. */
6601 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6602 || !require_constant_value
6603 || flag_isoc99)
6604 value = save_expr (value);
6607 while (1)
6609 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6611 tree fieldtype;
6612 enum tree_code fieldcode;
6614 if (constructor_fields == 0)
6616 pedwarn_init ("excess elements in struct initializer");
6617 break;
6620 fieldtype = TREE_TYPE (constructor_fields);
6621 if (fieldtype != error_mark_node)
6622 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6623 fieldcode = TREE_CODE (fieldtype);
6625 /* Error for non-static initialization of a flexible array member. */
6626 if (fieldcode == ARRAY_TYPE
6627 && !require_constant_value
6628 && TYPE_SIZE (fieldtype) == NULL_TREE
6629 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6631 error_init ("non-static initialization of a flexible array member");
6632 break;
6635 /* Accept a string constant to initialize a subarray. */
6636 if (value != 0
6637 && fieldcode == ARRAY_TYPE
6638 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6639 && string_flag)
6640 value = orig_value;
6641 /* Otherwise, if we have come to a subaggregate,
6642 and we don't have an element of its type, push into it. */
6643 else if (value != 0 && !constructor_no_implicit
6644 && value != error_mark_node
6645 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6646 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6647 || fieldcode == UNION_TYPE))
6649 push_init_level (1);
6650 continue;
6653 if (value)
6655 push_member_name (constructor_fields);
6656 output_init_element (value, fieldtype, constructor_fields, 1);
6657 RESTORE_SPELLING_DEPTH (constructor_depth);
6659 else
6660 /* Do the bookkeeping for an element that was
6661 directly output as a constructor. */
6663 /* For a record, keep track of end position of last field. */
6664 if (DECL_SIZE (constructor_fields))
6665 constructor_bit_index
6666 = size_binop (PLUS_EXPR,
6667 bit_position (constructor_fields),
6668 DECL_SIZE (constructor_fields));
6670 /* If the current field was the first one not yet written out,
6671 it isn't now, so update. */
6672 if (constructor_unfilled_fields == constructor_fields)
6674 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6675 /* Skip any nameless bit fields. */
6676 while (constructor_unfilled_fields != 0
6677 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6678 && DECL_NAME (constructor_unfilled_fields) == 0)
6679 constructor_unfilled_fields =
6680 TREE_CHAIN (constructor_unfilled_fields);
6684 constructor_fields = TREE_CHAIN (constructor_fields);
6685 /* Skip any nameless bit fields at the beginning. */
6686 while (constructor_fields != 0
6687 && DECL_C_BIT_FIELD (constructor_fields)
6688 && DECL_NAME (constructor_fields) == 0)
6689 constructor_fields = TREE_CHAIN (constructor_fields);
6691 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6693 tree fieldtype;
6694 enum tree_code fieldcode;
6696 if (constructor_fields == 0)
6698 pedwarn_init ("excess elements in union initializer");
6699 break;
6702 fieldtype = TREE_TYPE (constructor_fields);
6703 if (fieldtype != error_mark_node)
6704 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6705 fieldcode = TREE_CODE (fieldtype);
6707 /* Warn that traditional C rejects initialization of unions.
6708 We skip the warning if the value is zero. This is done
6709 under the assumption that the zero initializer in user
6710 code appears conditioned on e.g. __STDC__ to avoid
6711 "missing initializer" warnings and relies on default
6712 initialization to zero in the traditional C case.
6713 We also skip the warning if the initializer is designated,
6714 again on the assumption that this must be conditional on
6715 __STDC__ anyway (and we've already complained about the
6716 member-designator already). */
6717 if (warn_traditional && !in_system_header && !constructor_designated
6718 && !(value && (integer_zerop (value) || real_zerop (value))))
6719 warning ("traditional C rejects initialization of unions");
6721 /* Accept a string constant to initialize a subarray. */
6722 if (value != 0
6723 && fieldcode == ARRAY_TYPE
6724 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6725 && string_flag)
6726 value = orig_value;
6727 /* Otherwise, if we have come to a subaggregate,
6728 and we don't have an element of its type, push into it. */
6729 else if (value != 0 && !constructor_no_implicit
6730 && value != error_mark_node
6731 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6732 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6733 || fieldcode == UNION_TYPE))
6735 push_init_level (1);
6736 continue;
6739 if (value)
6741 push_member_name (constructor_fields);
6742 output_init_element (value, fieldtype, constructor_fields, 1);
6743 RESTORE_SPELLING_DEPTH (constructor_depth);
6745 else
6746 /* Do the bookkeeping for an element that was
6747 directly output as a constructor. */
6749 constructor_bit_index = DECL_SIZE (constructor_fields);
6750 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6753 constructor_fields = 0;
6755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6757 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6758 enum tree_code eltcode = TREE_CODE (elttype);
6760 /* Accept a string constant to initialize a subarray. */
6761 if (value != 0
6762 && eltcode == ARRAY_TYPE
6763 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6764 && string_flag)
6765 value = orig_value;
6766 /* Otherwise, if we have come to a subaggregate,
6767 and we don't have an element of its type, push into it. */
6768 else if (value != 0 && !constructor_no_implicit
6769 && value != error_mark_node
6770 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6771 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6772 || eltcode == UNION_TYPE))
6774 push_init_level (1);
6775 continue;
6778 if (constructor_max_index != 0
6779 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6780 || integer_all_onesp (constructor_max_index)))
6782 pedwarn_init ("excess elements in array initializer");
6783 break;
6786 /* Now output the actual element. */
6787 if (value)
6789 push_array_bounds (tree_low_cst (constructor_index, 0));
6790 output_init_element (value, elttype, constructor_index, 1);
6791 RESTORE_SPELLING_DEPTH (constructor_depth);
6794 constructor_index
6795 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6797 if (! value)
6798 /* If we are doing the bookkeeping for an element that was
6799 directly output as a constructor, we must update
6800 constructor_unfilled_index. */
6801 constructor_unfilled_index = constructor_index;
6803 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6805 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6807 /* Do a basic check of initializer size. Note that vectors
6808 always have a fixed size derived from their type. */
6809 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6811 pedwarn_init ("excess elements in vector initializer");
6812 break;
6815 /* Now output the actual element. */
6816 if (value)
6817 output_init_element (value, elttype, constructor_index, 1);
6819 constructor_index
6820 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6822 if (! value)
6823 /* If we are doing the bookkeeping for an element that was
6824 directly output as a constructor, we must update
6825 constructor_unfilled_index. */
6826 constructor_unfilled_index = constructor_index;
6829 /* Handle the sole element allowed in a braced initializer
6830 for a scalar variable. */
6831 else if (constructor_fields == 0)
6833 pedwarn_init ("excess elements in scalar initializer");
6834 break;
6836 else
6838 if (value)
6839 output_init_element (value, constructor_type, NULL_TREE, 1);
6840 constructor_fields = 0;
6843 /* Handle range initializers either at this level or anywhere higher
6844 in the designator stack. */
6845 if (constructor_range_stack)
6847 struct constructor_range_stack *p, *range_stack;
6848 int finish = 0;
6850 range_stack = constructor_range_stack;
6851 constructor_range_stack = 0;
6852 while (constructor_stack != range_stack->stack)
6854 if (!constructor_stack->implicit)
6855 abort ();
6856 process_init_element (pop_init_level (1));
6858 for (p = range_stack;
6859 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6860 p = p->prev)
6862 if (!constructor_stack->implicit)
6863 abort ();
6864 process_init_element (pop_init_level (1));
6867 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6868 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6869 finish = 1;
6871 while (1)
6873 constructor_index = p->index;
6874 constructor_fields = p->fields;
6875 if (finish && p->range_end && p->index == p->range_start)
6877 finish = 0;
6878 p->prev = 0;
6880 p = p->next;
6881 if (!p)
6882 break;
6883 push_init_level (2);
6884 p->stack = constructor_stack;
6885 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6886 p->index = p->range_start;
6889 if (!finish)
6890 constructor_range_stack = range_stack;
6891 continue;
6894 break;
6897 constructor_range_stack = 0;
6900 /* Build a simple asm-statement, from one string literal. */
6901 tree
6902 simple_asm_stmt (expr)
6903 tree expr;
6905 STRIP_NOPS (expr);
6907 if (TREE_CODE (expr) == ADDR_EXPR)
6908 expr = TREE_OPERAND (expr, 0);
6910 if (TREE_CODE (expr) == STRING_CST)
6912 tree stmt;
6914 /* Simple asm statements are treated as volatile. */
6915 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6916 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6917 ASM_INPUT_P (stmt) = 1;
6918 return stmt;
6921 error ("argument of `asm' is not a constant string");
6922 return NULL_TREE;
6925 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6926 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6928 tree
6929 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6930 tree cv_qualifier;
6931 tree string;
6932 tree outputs;
6933 tree inputs;
6934 tree clobbers;
6936 tree tail;
6938 if (TREE_CODE (string) != STRING_CST)
6940 error ("asm template is not a string constant");
6941 return NULL_TREE;
6944 if (cv_qualifier != NULL_TREE
6945 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6947 warning ("%s qualifier ignored on asm",
6948 IDENTIFIER_POINTER (cv_qualifier));
6949 cv_qualifier = NULL_TREE;
6952 /* We can remove output conversions that change the type,
6953 but not the mode. */
6954 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6956 tree output = TREE_VALUE (tail);
6958 STRIP_NOPS (output);
6959 TREE_VALUE (tail) = output;
6961 /* Allow conversions as LHS here. build_modify_expr as called below
6962 will do the right thing with them. */
6963 while (TREE_CODE (output) == NOP_EXPR
6964 || TREE_CODE (output) == CONVERT_EXPR
6965 || TREE_CODE (output) == FLOAT_EXPR
6966 || TREE_CODE (output) == FIX_TRUNC_EXPR
6967 || TREE_CODE (output) == FIX_FLOOR_EXPR
6968 || TREE_CODE (output) == FIX_ROUND_EXPR
6969 || TREE_CODE (output) == FIX_CEIL_EXPR)
6970 output = TREE_OPERAND (output, 0);
6972 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6975 /* Remove output conversions that change the type but not the mode. */
6976 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6978 tree output = TREE_VALUE (tail);
6979 STRIP_NOPS (output);
6980 TREE_VALUE (tail) = output;
6983 /* Perform default conversions on array and function inputs.
6984 Don't do this for other types as it would screw up operands
6985 expected to be in memory. */
6986 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6987 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6989 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6990 outputs, inputs, clobbers));
6993 /* Expand an ASM statement with operands, handling output operands
6994 that are not variables or INDIRECT_REFS by transforming such
6995 cases into cases that expand_asm_operands can handle.
6997 Arguments are same as for expand_asm_operands. */
6999 void
7000 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7001 tree string, outputs, inputs, clobbers;
7002 int vol;
7003 const char *filename;
7004 int line;
7006 int noutputs = list_length (outputs);
7007 int i;
7008 /* o[I] is the place that output number I should be written. */
7009 tree *o = (tree *) alloca (noutputs * sizeof (tree));
7010 tree tail;
7012 /* Record the contents of OUTPUTS before it is modified. */
7013 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7015 o[i] = TREE_VALUE (tail);
7016 if (o[i] == error_mark_node)
7017 return;
7020 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
7021 OUTPUTS some trees for where the values were actually stored. */
7022 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7024 /* Copy all the intermediate outputs into the specified outputs. */
7025 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7027 if (o[i] != TREE_VALUE (tail))
7029 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7030 NULL_RTX, VOIDmode, EXPAND_NORMAL);
7031 free_temp_slots ();
7033 /* Restore the original value so that it's correct the next
7034 time we expand this function. */
7035 TREE_VALUE (tail) = o[i];
7037 /* Detect modification of read-only values.
7038 (Otherwise done by build_modify_expr.) */
7039 else
7041 tree type = TREE_TYPE (o[i]);
7042 if (TREE_READONLY (o[i])
7043 || TYPE_READONLY (type)
7044 || ((TREE_CODE (type) == RECORD_TYPE
7045 || TREE_CODE (type) == UNION_TYPE)
7046 && C_TYPE_FIELDS_READONLY (type)))
7047 readonly_warning (o[i], "modification by `asm'");
7051 /* Those MODIFY_EXPRs could do autoincrements. */
7052 emit_queue ();
7055 /* Expand a C `return' statement.
7056 RETVAL is the expression for what to return,
7057 or a null pointer for `return;' with no value. */
7059 tree
7060 c_expand_return (retval)
7061 tree retval;
7063 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7065 if (TREE_THIS_VOLATILE (current_function_decl))
7066 warning ("function declared `noreturn' has a `return' statement");
7068 if (!retval)
7070 current_function_returns_null = 1;
7071 if ((warn_return_type || flag_isoc99)
7072 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7073 pedwarn_c99 ("`return' with no value, in function returning non-void");
7075 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7077 current_function_returns_null = 1;
7078 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7079 pedwarn ("`return' with a value, in function returning void");
7081 else
7083 tree t = convert_for_assignment (valtype, retval, _("return"),
7084 NULL_TREE, NULL_TREE, 0);
7085 tree res = DECL_RESULT (current_function_decl);
7086 tree inner;
7088 current_function_returns_value = 1;
7089 if (t == error_mark_node)
7090 return NULL_TREE;
7092 inner = t = convert (TREE_TYPE (res), t);
7094 /* Strip any conversions, additions, and subtractions, and see if
7095 we are returning the address of a local variable. Warn if so. */
7096 while (1)
7098 switch (TREE_CODE (inner))
7100 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7101 case PLUS_EXPR:
7102 inner = TREE_OPERAND (inner, 0);
7103 continue;
7105 case MINUS_EXPR:
7106 /* If the second operand of the MINUS_EXPR has a pointer
7107 type (or is converted from it), this may be valid, so
7108 don't give a warning. */
7110 tree op1 = TREE_OPERAND (inner, 1);
7112 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7113 && (TREE_CODE (op1) == NOP_EXPR
7114 || TREE_CODE (op1) == NON_LVALUE_EXPR
7115 || TREE_CODE (op1) == CONVERT_EXPR))
7116 op1 = TREE_OPERAND (op1, 0);
7118 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7119 break;
7121 inner = TREE_OPERAND (inner, 0);
7122 continue;
7125 case ADDR_EXPR:
7126 inner = TREE_OPERAND (inner, 0);
7128 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7129 inner = TREE_OPERAND (inner, 0);
7131 if (TREE_CODE (inner) == VAR_DECL
7132 && ! DECL_EXTERNAL (inner)
7133 && ! TREE_STATIC (inner)
7134 && DECL_CONTEXT (inner) == current_function_decl)
7135 warning ("function returns address of local variable");
7136 break;
7138 default:
7139 break;
7142 break;
7145 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7148 return add_stmt (build_return_stmt (retval));
7151 struct c_switch {
7152 /* The SWITCH_STMT being built. */
7153 tree switch_stmt;
7154 /* A splay-tree mapping the low element of a case range to the high
7155 element, or NULL_TREE if there is no high element. Used to
7156 determine whether or not a new case label duplicates an old case
7157 label. We need a tree, rather than simply a hash table, because
7158 of the GNU case range extension. */
7159 splay_tree cases;
7160 /* The next node on the stack. */
7161 struct c_switch *next;
7164 /* A stack of the currently active switch statements. The innermost
7165 switch statement is on the top of the stack. There is no need to
7166 mark the stack for garbage collection because it is only active
7167 during the processing of the body of a function, and we never
7168 collect at that point. */
7170 static struct c_switch *switch_stack;
7172 /* Start a C switch statement, testing expression EXP. Return the new
7173 SWITCH_STMT. */
7175 tree
7176 c_start_case (exp)
7177 tree exp;
7179 enum tree_code code;
7180 tree type, orig_type = error_mark_node;
7181 struct c_switch *cs;
7183 if (exp != error_mark_node)
7185 code = TREE_CODE (TREE_TYPE (exp));
7186 orig_type = TREE_TYPE (exp);
7188 if (! INTEGRAL_TYPE_P (orig_type)
7189 && code != ERROR_MARK)
7191 error ("switch quantity not an integer");
7192 exp = integer_zero_node;
7194 else
7196 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7198 if (warn_traditional && !in_system_header
7199 && (type == long_integer_type_node
7200 || type == long_unsigned_type_node))
7201 warning ("`long' switch expression not converted to `int' in ISO C");
7203 exp = default_conversion (exp);
7204 type = TREE_TYPE (exp);
7208 /* Add this new SWITCH_STMT to the stack. */
7209 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7210 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7211 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7212 cs->next = switch_stack;
7213 switch_stack = cs;
7215 return add_stmt (switch_stack->switch_stmt);
7218 /* Process a case label. */
7220 tree
7221 do_case (low_value, high_value)
7222 tree low_value;
7223 tree high_value;
7225 tree label = NULL_TREE;
7227 if (switch_stack)
7229 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7231 label = c_add_case_label (switch_stack->cases,
7232 SWITCH_COND (switch_stack->switch_stmt),
7233 low_value, high_value);
7234 if (label == error_mark_node)
7235 label = NULL_TREE;
7236 else if (switch_was_empty_p)
7238 /* Attach the first case label to the SWITCH_BODY. */
7239 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7240 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7243 else if (low_value)
7244 error ("case label not within a switch statement");
7245 else
7246 error ("`default' label not within a switch statement");
7248 return label;
7251 /* Finish the switch statement. */
7253 void
7254 c_finish_case ()
7256 struct c_switch *cs = switch_stack;
7258 /* Rechain the next statements to the SWITCH_STMT. */
7259 last_tree = cs->switch_stmt;
7261 /* Pop the stack. */
7262 switch_stack = switch_stack->next;
7263 splay_tree_delete (cs->cases);
7264 free (cs);