Fixed rare threading problem
[official-gcc.git] / gcc / c-typeck.c
blob718bd8f59862c0d335919392cd131e7190701734
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 /* 1 if we explained undeclared var errors. */
53 static int undeclared_variable_notice;
55 static tree qualify_type (tree, tree);
56 static int comp_target_types (tree, tree, int);
57 static int function_types_compatible_p (tree, tree);
58 static int type_lists_compatible_p (tree, tree);
59 static tree decl_constant_value_for_broken_optimization (tree);
60 static tree default_function_array_conversion (tree);
61 static tree lookup_field (tree, tree);
62 static void undeclared_variable (tree);
63 static tree convert_arguments (tree, tree, tree, tree);
64 static tree pointer_diff (tree, tree);
65 static tree unary_complex_lvalue (enum tree_code, tree, int);
66 static void pedantic_lvalue_warning (enum tree_code);
67 static tree internal_build_compound_expr (tree, int);
68 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
69 int);
70 static void warn_for_assignment (const char *, const char *, tree, int);
71 static tree valid_compound_expr_initializer (tree, tree);
72 static void push_string (const char *);
73 static void push_member_name (tree);
74 static void push_array_bounds (int);
75 static int spelling_length (void);
76 static char *print_spelling (char *);
77 static void warning_init (const char *);
78 static tree digest_init (tree, tree, int);
79 static void output_init_element (tree, tree, tree, int);
80 static void output_pending_init_elements (int);
81 static int set_designator (int);
82 static void push_range_stack (tree);
83 static void add_pending_init (tree, tree);
84 static void set_nonincremental_init (void);
85 static void set_nonincremental_init_from_string (tree);
86 static tree find_init_member (tree);
88 /* Do `exp = require_complete_type (exp);' to make sure exp
89 does not have an incomplete type. (That includes void types.) */
91 tree
92 require_complete_type (tree value)
94 tree type = TREE_TYPE (value);
96 if (value == error_mark_node || type == error_mark_node)
97 return error_mark_node;
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
101 return value;
103 c_incomplete_type_error (value, type);
104 return error_mark_node;
107 /* Print an error message for invalid use of an incomplete type.
108 VALUE is the expression that was used (or 0 if that isn't known)
109 and TYPE is the type that was invalid. */
111 void
112 c_incomplete_type_error (tree value, tree type)
114 const char *type_code_string;
116 /* Avoid duplicate error message. */
117 if (TREE_CODE (type) == ERROR_MARK)
118 return;
120 if (value != 0 && (TREE_CODE (value) == VAR_DECL
121 || TREE_CODE (value) == PARM_DECL))
122 error ("`%s' has an incomplete type",
123 IDENTIFIER_POINTER (DECL_NAME (value)));
124 else
126 retry:
127 /* We must print an error message. Be clever about what it says. */
129 switch (TREE_CODE (type))
131 case RECORD_TYPE:
132 type_code_string = "struct";
133 break;
135 case UNION_TYPE:
136 type_code_string = "union";
137 break;
139 case ENUMERAL_TYPE:
140 type_code_string = "enum";
141 break;
143 case VOID_TYPE:
144 error ("invalid use of void expression");
145 return;
147 case ARRAY_TYPE:
148 if (TYPE_DOMAIN (type))
150 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
152 error ("invalid use of flexible array member");
153 return;
155 type = TREE_TYPE (type);
156 goto retry;
158 error ("invalid use of array with unspecified bounds");
159 return;
161 default:
162 abort ();
165 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
166 error ("invalid use of undefined type `%s %s'",
167 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
168 else
169 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
170 error ("invalid use of incomplete typedef `%s'",
171 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
175 /* Given a type, apply default promotions wrt unnamed function
176 arguments and return the new type. */
178 tree
179 c_type_promotes_to (tree type)
181 if (TYPE_MAIN_VARIANT (type) == float_type_node)
182 return double_type_node;
184 if (c_promoting_integer_type_p (type))
186 /* Preserve unsignedness if not really getting any wider. */
187 if (TREE_UNSIGNED (type)
188 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
189 return unsigned_type_node;
190 return integer_type_node;
193 return type;
196 /* Return a variant of TYPE which has all the type qualifiers of LIKE
197 as well as those of TYPE. */
199 static tree
200 qualify_type (tree type, tree like)
202 return c_build_qualified_type (type,
203 TYPE_QUALS (type) | TYPE_QUALS (like));
206 /* Return the common type of two types.
207 We assume that comptypes has already been done and returned 1;
208 if that isn't so, this may crash. In particular, we assume that qualifiers
209 match.
211 This is the type for the result of most arithmetic operations
212 if the operands have the given two types. */
214 tree
215 common_type (tree t1, tree t2)
217 enum tree_code code1;
218 enum tree_code code2;
219 tree attributes;
221 /* Save time if the two types are the same. */
223 if (t1 == t2) return t1;
225 /* If one type is nonsense, use the other. */
226 if (t1 == error_mark_node)
227 return t2;
228 if (t2 == error_mark_node)
229 return t1;
231 /* Merge the attributes. */
232 attributes = (*targetm.merge_type_attributes) (t1, t2);
234 /* Treat an enum type as the unsigned integer type of the same width. */
236 if (TREE_CODE (t1) == ENUMERAL_TYPE)
237 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
238 if (TREE_CODE (t2) == ENUMERAL_TYPE)
239 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
241 code1 = TREE_CODE (t1);
242 code2 = TREE_CODE (t2);
244 /* If one type is complex, form the common type of the non-complex
245 components, then make that complex. Use T1 or T2 if it is the
246 required type. */
247 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
249 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
250 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
251 tree subtype = common_type (subtype1, subtype2);
253 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
254 return build_type_attribute_variant (t1, attributes);
255 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
256 return build_type_attribute_variant (t2, attributes);
257 else
258 return build_type_attribute_variant (build_complex_type (subtype),
259 attributes);
262 switch (code1)
264 case INTEGER_TYPE:
265 case REAL_TYPE:
266 /* If only one is real, use it as the result. */
268 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
269 return build_type_attribute_variant (t1, attributes);
271 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
272 return build_type_attribute_variant (t2, attributes);
274 /* Both real or both integers; use the one with greater precision. */
276 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
277 return build_type_attribute_variant (t1, attributes);
278 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
279 return build_type_attribute_variant (t2, attributes);
281 /* Same precision. Prefer longs to ints even when same size. */
283 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
284 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
285 return build_type_attribute_variant (long_unsigned_type_node,
286 attributes);
288 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
289 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
291 /* But preserve unsignedness from the other type,
292 since long cannot hold all the values of an unsigned int. */
293 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
294 t1 = long_unsigned_type_node;
295 else
296 t1 = long_integer_type_node;
297 return build_type_attribute_variant (t1, attributes);
300 /* Likewise, prefer long double to double even if same size. */
301 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
302 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
303 return build_type_attribute_variant (long_double_type_node,
304 attributes);
306 /* Otherwise prefer the unsigned one. */
308 if (TREE_UNSIGNED (t1))
309 return build_type_attribute_variant (t1, attributes);
310 else
311 return build_type_attribute_variant (t2, attributes);
313 case POINTER_TYPE:
314 /* For two pointers, do this recursively on the target type,
315 and combine the qualifiers of the two types' targets. */
316 /* This code was turned off; I don't know why.
317 But ANSI C specifies doing this with the qualifiers.
318 So I turned it on again. */
320 tree pointed_to_1 = TREE_TYPE (t1);
321 tree pointed_to_2 = TREE_TYPE (t2);
322 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
323 TYPE_MAIN_VARIANT (pointed_to_2));
324 t1 = build_pointer_type (c_build_qualified_type
325 (target,
326 TYPE_QUALS (pointed_to_1) |
327 TYPE_QUALS (pointed_to_2)));
328 return build_type_attribute_variant (t1, attributes);
331 case ARRAY_TYPE:
333 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
334 /* Save space: see if the result is identical to one of the args. */
335 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
336 return build_type_attribute_variant (t1, attributes);
337 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
338 return build_type_attribute_variant (t2, attributes);
339 /* Merge the element types, and have a size if either arg has one. */
340 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
341 return build_type_attribute_variant (t1, attributes);
344 case FUNCTION_TYPE:
345 /* Function types: prefer the one that specified arg types.
346 If both do, merge the arg types. Also merge the return types. */
348 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
349 tree p1 = TYPE_ARG_TYPES (t1);
350 tree p2 = TYPE_ARG_TYPES (t2);
351 int len;
352 tree newargs, n;
353 int i;
355 /* Save space: see if the result is identical to one of the args. */
356 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
357 return build_type_attribute_variant (t1, attributes);
358 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
359 return build_type_attribute_variant (t2, attributes);
361 /* Simple way if one arg fails to specify argument types. */
362 if (TYPE_ARG_TYPES (t1) == 0)
364 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
365 return build_type_attribute_variant (t1, attributes);
367 if (TYPE_ARG_TYPES (t2) == 0)
369 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
370 return build_type_attribute_variant (t1, attributes);
373 /* If both args specify argument types, we must merge the two
374 lists, argument by argument. */
376 pushlevel (0);
377 declare_parm_level (1);
379 len = list_length (p1);
380 newargs = 0;
382 for (i = 0; i < len; i++)
383 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
385 n = newargs;
387 for (; p1;
388 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
390 /* A null type means arg type is not specified.
391 Take whatever the other function type has. */
392 if (TREE_VALUE (p1) == 0)
394 TREE_VALUE (n) = TREE_VALUE (p2);
395 goto parm_done;
397 if (TREE_VALUE (p2) == 0)
399 TREE_VALUE (n) = TREE_VALUE (p1);
400 goto parm_done;
403 /* Given wait (union {union wait *u; int *i} *)
404 and wait (union wait *),
405 prefer union wait * as type of parm. */
406 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
407 && TREE_VALUE (p1) != TREE_VALUE (p2))
409 tree memb;
410 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
411 memb; memb = TREE_CHAIN (memb))
412 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
414 TREE_VALUE (n) = TREE_VALUE (p2);
415 if (pedantic)
416 pedwarn ("function types not truly compatible in ISO C");
417 goto parm_done;
420 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
421 && TREE_VALUE (p2) != TREE_VALUE (p1))
423 tree memb;
424 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
425 memb; memb = TREE_CHAIN (memb))
426 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
428 TREE_VALUE (n) = TREE_VALUE (p1);
429 if (pedantic)
430 pedwarn ("function types not truly compatible in ISO C");
431 goto parm_done;
434 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
435 parm_done: ;
438 poplevel (0, 0, 0);
440 t1 = build_function_type (valtype, newargs);
441 /* ... falls through ... */
444 default:
445 return build_type_attribute_variant (t1, attributes);
450 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
451 or various other operations. Return 2 if they are compatible
452 but a warning may be needed if you use them together. */
455 comptypes (tree type1, tree type2)
457 tree t1 = type1;
458 tree t2 = type2;
459 int attrval, val;
461 /* Suppress errors caused by previously reported errors. */
463 if (t1 == t2 || !t1 || !t2
464 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
465 return 1;
467 /* If either type is the internal version of sizetype, return the
468 language version. */
469 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
470 && TYPE_DOMAIN (t1) != 0)
471 t1 = TYPE_DOMAIN (t1);
473 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
474 && TYPE_DOMAIN (t2) != 0)
475 t2 = TYPE_DOMAIN (t2);
477 /* Treat an enum type as the integer type of the same width and
478 signedness. */
480 if (TREE_CODE (t1) == ENUMERAL_TYPE)
481 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482 if (TREE_CODE (t2) == ENUMERAL_TYPE)
483 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
485 if (t1 == t2)
486 return 1;
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
495 return 0;
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
502 return 1;
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
506 return 0;
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 val = 0;
511 switch (TREE_CODE (t1))
513 case POINTER_TYPE:
514 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
515 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
516 break;
518 case FUNCTION_TYPE:
519 val = function_types_compatible_p (t1, t2);
520 break;
522 case ARRAY_TYPE:
524 tree d1 = TYPE_DOMAIN (t1);
525 tree d2 = TYPE_DOMAIN (t2);
526 bool d1_variable, d2_variable;
527 bool d1_zero, d2_zero;
528 val = 1;
530 /* Target types must match incl. qualifiers. */
531 if (TREE_TYPE (t1) != TREE_TYPE (t2)
532 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
533 return 0;
535 /* Sizes must match unless one is missing or variable. */
536 if (d1 == 0 || d2 == 0 || d1 == d2)
537 break;
539 d1_zero = ! TYPE_MAX_VALUE (d1);
540 d2_zero = ! TYPE_MAX_VALUE (d2);
542 d1_variable = (! d1_zero
543 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
545 d2_variable = (! d2_zero
546 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
547 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
549 if (d1_variable || d2_variable)
550 break;
551 if (d1_zero && d2_zero)
552 break;
553 if (d1_zero || d2_zero
554 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
555 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
556 val = 0;
558 break;
561 case RECORD_TYPE:
562 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
563 val = 1;
564 break;
566 case VECTOR_TYPE:
567 /* The target might allow certain vector types to be compatible. */
568 val = (*targetm.vector_opaque_p) (t1)
569 || (*targetm.vector_opaque_p) (t2);
570 break;
572 default:
573 break;
575 return attrval == 2 && val == 1 ? 2 : val;
578 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
579 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
580 to 1 or 0 depending if the check of the pointer types is meant to
581 be reflexive or not (typically, assignments are not reflexive,
582 while comparisons are reflexive).
585 static int
586 comp_target_types (tree ttl, tree ttr, int reflexive)
588 int val;
590 /* Give objc_comptypes a crack at letting these types through. */
591 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
592 return val;
594 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
595 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
597 if (val == 2 && pedantic)
598 pedwarn ("types are not quite compatible");
599 return val;
602 /* Subroutines of `comptypes'. */
604 /* Return 1 if two function types F1 and F2 are compatible.
605 If either type specifies no argument types,
606 the other must specify a fixed number of self-promoting arg types.
607 Otherwise, if one type specifies only the number of arguments,
608 the other must specify that number of self-promoting arg types.
609 Otherwise, the argument types must match. */
611 static int
612 function_types_compatible_p (tree f1, tree f2)
614 tree args1, args2;
615 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
616 int val = 1;
617 int val1;
618 tree ret1, ret2;
620 ret1 = TREE_TYPE (f1);
621 ret2 = TREE_TYPE (f2);
623 /* 'volatile' qualifiers on a function's return type mean the function
624 is noreturn. */
625 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
626 pedwarn ("function return types not compatible due to `volatile'");
627 if (TYPE_VOLATILE (ret1))
628 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
629 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
630 if (TYPE_VOLATILE (ret2))
631 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
632 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
633 val = comptypes (ret1, ret2);
634 if (val == 0)
635 return 0;
637 args1 = TYPE_ARG_TYPES (f1);
638 args2 = TYPE_ARG_TYPES (f2);
640 /* An unspecified parmlist matches any specified parmlist
641 whose argument types don't need default promotions. */
643 if (args1 == 0)
645 if (!self_promoting_args_p (args2))
646 return 0;
647 /* If one of these types comes from a non-prototype fn definition,
648 compare that with the other type's arglist.
649 If they don't match, ask for a warning (but no error). */
650 if (TYPE_ACTUAL_ARG_TYPES (f1)
651 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
652 val = 2;
653 return val;
655 if (args2 == 0)
657 if (!self_promoting_args_p (args1))
658 return 0;
659 if (TYPE_ACTUAL_ARG_TYPES (f2)
660 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
661 val = 2;
662 return val;
665 /* Both types have argument lists: compare them and propagate results. */
666 val1 = type_lists_compatible_p (args1, args2);
667 return val1 != 1 ? val1 : val;
670 /* Check two lists of types for compatibility,
671 returning 0 for incompatible, 1 for compatible,
672 or 2 for compatible with warning. */
674 static int
675 type_lists_compatible_p (tree args1, tree args2)
677 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
678 int val = 1;
679 int newval = 0;
681 while (1)
683 if (args1 == 0 && args2 == 0)
684 return val;
685 /* If one list is shorter than the other,
686 they fail to match. */
687 if (args1 == 0 || args2 == 0)
688 return 0;
689 /* A null pointer instead of a type
690 means there is supposed to be an argument
691 but nothing is specified about what type it has.
692 So match anything that self-promotes. */
693 if (TREE_VALUE (args1) == 0)
695 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
696 return 0;
698 else if (TREE_VALUE (args2) == 0)
700 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
701 return 0;
703 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
704 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
706 /* Allow wait (union {union wait *u; int *i} *)
707 and wait (union wait *) to be compatible. */
708 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
709 && (TYPE_NAME (TREE_VALUE (args1)) == 0
710 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
711 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
712 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
713 TYPE_SIZE (TREE_VALUE (args2))))
715 tree memb;
716 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
717 memb; memb = TREE_CHAIN (memb))
718 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
719 break;
720 if (memb == 0)
721 return 0;
723 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
724 && (TYPE_NAME (TREE_VALUE (args2)) == 0
725 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
726 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
727 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
728 TYPE_SIZE (TREE_VALUE (args1))))
730 tree memb;
731 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
732 memb; memb = TREE_CHAIN (memb))
733 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
734 break;
735 if (memb == 0)
736 return 0;
738 else
739 return 0;
742 /* comptypes said ok, but record if it said to warn. */
743 if (newval > val)
744 val = newval;
746 args1 = TREE_CHAIN (args1);
747 args2 = TREE_CHAIN (args2);
751 /* Compute the size to increment a pointer by. */
753 tree
754 c_size_in_bytes (tree type)
756 enum tree_code code = TREE_CODE (type);
758 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
759 return size_one_node;
761 if (!COMPLETE_OR_VOID_TYPE_P (type))
763 error ("arithmetic on pointer to an incomplete type");
764 return size_one_node;
767 /* Convert in case a char is more than one unit. */
768 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
769 size_int (TYPE_PRECISION (char_type_node)
770 / BITS_PER_UNIT));
773 /* Return either DECL or its known constant value (if it has one). */
775 tree
776 decl_constant_value (tree decl)
778 if (/* Don't change a variable array bound or initial value to a constant
779 in a place where a variable is invalid. */
780 current_function_decl != 0
781 && ! TREE_THIS_VOLATILE (decl)
782 && TREE_READONLY (decl)
783 && DECL_INITIAL (decl) != 0
784 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
785 /* This is invalid if initial value is not constant.
786 If it has either a function call, a memory reference,
787 or a variable, then re-evaluating it could give different results. */
788 && TREE_CONSTANT (DECL_INITIAL (decl))
789 /* Check for cases where this is sub-optimal, even though valid. */
790 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
791 return DECL_INITIAL (decl);
792 return decl;
795 /* Return either DECL or its known constant value (if it has one), but
796 return DECL if pedantic or DECL has mode BLKmode. This is for
797 bug-compatibility with the old behavior of decl_constant_value
798 (before GCC 3.0); every use of this function is a bug and it should
799 be removed before GCC 3.1. It is not appropriate to use pedantic
800 in a way that affects optimization, and BLKmode is probably not the
801 right test for avoiding misoptimizations either. */
803 static tree
804 decl_constant_value_for_broken_optimization (tree decl)
806 if (pedantic || DECL_MODE (decl) == BLKmode)
807 return decl;
808 else
809 return decl_constant_value (decl);
813 /* Perform the default conversion of arrays and functions to pointers.
814 Return the result of converting EXP. For any other expression, just
815 return EXP. */
817 static tree
818 default_function_array_conversion (tree exp)
820 tree orig_exp;
821 tree type = TREE_TYPE (exp);
822 enum tree_code code = TREE_CODE (type);
823 int not_lvalue = 0;
825 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
826 an lvalue.
828 Do not use STRIP_NOPS here! It will remove conversions from pointer
829 to integer and cause infinite recursion. */
830 orig_exp = exp;
831 while (TREE_CODE (exp) == NON_LVALUE_EXPR
832 || (TREE_CODE (exp) == NOP_EXPR
833 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
835 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
836 not_lvalue = 1;
837 exp = TREE_OPERAND (exp, 0);
840 /* Preserve the original expression code. */
841 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
842 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
844 if (code == FUNCTION_TYPE)
846 return build_unary_op (ADDR_EXPR, exp, 0);
848 if (code == ARRAY_TYPE)
850 tree adr;
851 tree restype = TREE_TYPE (type);
852 tree ptrtype;
853 int constp = 0;
854 int volatilep = 0;
855 int lvalue_array_p;
857 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
859 constp = TREE_READONLY (exp);
860 volatilep = TREE_THIS_VOLATILE (exp);
863 if (TYPE_QUALS (type) || constp || volatilep)
864 restype
865 = c_build_qualified_type (restype,
866 TYPE_QUALS (type)
867 | (constp * TYPE_QUAL_CONST)
868 | (volatilep * TYPE_QUAL_VOLATILE));
870 if (TREE_CODE (exp) == INDIRECT_REF)
871 return convert (TYPE_POINTER_TO (restype),
872 TREE_OPERAND (exp, 0));
874 if (TREE_CODE (exp) == COMPOUND_EXPR)
876 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
877 return build (COMPOUND_EXPR, TREE_TYPE (op1),
878 TREE_OPERAND (exp, 0), op1);
881 lvalue_array_p = !not_lvalue && lvalue_p (exp);
882 if (!flag_isoc99 && !lvalue_array_p)
884 /* Before C99, non-lvalue arrays do not decay to pointers.
885 Normally, using such an array would be invalid; but it can
886 be used correctly inside sizeof or as a statement expression.
887 Thus, do not give an error here; an error will result later. */
888 return exp;
891 ptrtype = build_pointer_type (restype);
893 if (TREE_CODE (exp) == VAR_DECL)
895 /* ??? This is not really quite correct
896 in that the type of the operand of ADDR_EXPR
897 is not the target type of the type of the ADDR_EXPR itself.
898 Question is, can this lossage be avoided? */
899 adr = build1 (ADDR_EXPR, ptrtype, exp);
900 if (!c_mark_addressable (exp))
901 return error_mark_node;
902 TREE_CONSTANT (adr) = staticp (exp);
903 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
904 return adr;
906 /* This way is better for a COMPONENT_REF since it can
907 simplify the offset for a component. */
908 adr = build_unary_op (ADDR_EXPR, exp, 1);
909 return convert (ptrtype, adr);
911 return exp;
914 /* Perform default promotions for C data used in expressions.
915 Arrays and functions are converted to pointers;
916 enumeral types or short or char, to int.
917 In addition, manifest constants symbols are replaced by their values. */
919 tree
920 default_conversion (tree exp)
922 tree orig_exp;
923 tree type = TREE_TYPE (exp);
924 enum tree_code code = TREE_CODE (type);
926 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
927 return default_function_array_conversion (exp);
929 /* Constants can be used directly unless they're not loadable. */
930 if (TREE_CODE (exp) == CONST_DECL)
931 exp = DECL_INITIAL (exp);
933 /* Replace a nonvolatile const static variable with its value unless
934 it is an array, in which case we must be sure that taking the
935 address of the array produces consistent results. */
936 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
938 exp = decl_constant_value_for_broken_optimization (exp);
939 type = TREE_TYPE (exp);
942 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
943 an lvalue.
945 Do not use STRIP_NOPS here! It will remove conversions from pointer
946 to integer and cause infinite recursion. */
947 orig_exp = exp;
948 while (TREE_CODE (exp) == NON_LVALUE_EXPR
949 || (TREE_CODE (exp) == NOP_EXPR
950 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
951 exp = TREE_OPERAND (exp, 0);
953 /* Preserve the original expression code. */
954 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
955 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
957 /* Normally convert enums to int,
958 but convert wide enums to something wider. */
959 if (code == ENUMERAL_TYPE)
961 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
962 TYPE_PRECISION (integer_type_node)),
963 ((TYPE_PRECISION (type)
964 >= TYPE_PRECISION (integer_type_node))
965 && TREE_UNSIGNED (type)));
967 return convert (type, exp);
970 if (TREE_CODE (exp) == COMPONENT_REF
971 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
972 /* If it's thinner than an int, promote it like a
973 c_promoting_integer_type_p, otherwise leave it alone. */
974 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
975 TYPE_PRECISION (integer_type_node)))
976 return convert (integer_type_node, exp);
978 if (c_promoting_integer_type_p (type))
980 /* Preserve unsignedness if not really getting any wider. */
981 if (TREE_UNSIGNED (type)
982 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
983 return convert (unsigned_type_node, exp);
985 return convert (integer_type_node, exp);
988 if (code == VOID_TYPE)
990 error ("void value not ignored as it ought to be");
991 return error_mark_node;
993 return exp;
996 /* Look up COMPONENT in a structure or union DECL.
998 If the component name is not found, returns NULL_TREE. Otherwise,
999 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1000 stepping down the chain to the component, which is in the last
1001 TREE_VALUE of the list. Normally the list is of length one, but if
1002 the component is embedded within (nested) anonymous structures or
1003 unions, the list steps down the chain to the component. */
1005 static tree
1006 lookup_field (tree decl, tree component)
1008 tree type = TREE_TYPE (decl);
1009 tree field;
1011 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1012 to the field elements. Use a binary search on this array to quickly
1013 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1014 will always be set for structures which have many elements. */
1016 if (TYPE_LANG_SPECIFIC (type))
1018 int bot, top, half;
1019 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1021 field = TYPE_FIELDS (type);
1022 bot = 0;
1023 top = TYPE_LANG_SPECIFIC (type)->len;
1024 while (top - bot > 1)
1026 half = (top - bot + 1) >> 1;
1027 field = field_array[bot+half];
1029 if (DECL_NAME (field) == NULL_TREE)
1031 /* Step through all anon unions in linear fashion. */
1032 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1034 field = field_array[bot++];
1035 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1036 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1038 tree anon = lookup_field (field, component);
1040 if (anon)
1041 return tree_cons (NULL_TREE, field, anon);
1045 /* Entire record is only anon unions. */
1046 if (bot > top)
1047 return NULL_TREE;
1049 /* Restart the binary search, with new lower bound. */
1050 continue;
1053 if (DECL_NAME (field) == component)
1054 break;
1055 if (DECL_NAME (field) < component)
1056 bot += half;
1057 else
1058 top = bot + half;
1061 if (DECL_NAME (field_array[bot]) == component)
1062 field = field_array[bot];
1063 else if (DECL_NAME (field) != component)
1064 return NULL_TREE;
1066 else
1068 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1070 if (DECL_NAME (field) == NULL_TREE
1071 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1072 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1074 tree anon = lookup_field (field, component);
1076 if (anon)
1077 return tree_cons (NULL_TREE, field, anon);
1080 if (DECL_NAME (field) == component)
1081 break;
1084 if (field == NULL_TREE)
1085 return NULL_TREE;
1088 return tree_cons (NULL_TREE, field, NULL_TREE);
1091 /* Make an expression to refer to the COMPONENT field of
1092 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1094 tree
1095 build_component_ref (tree datum, tree component)
1097 tree type = TREE_TYPE (datum);
1098 enum tree_code code = TREE_CODE (type);
1099 tree field = NULL;
1100 tree ref;
1102 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1103 If pedantic ensure that the arguments are not lvalues; otherwise,
1104 if the component is an array, it would wrongly decay to a pointer in
1105 C89 mode.
1106 We cannot do this with a COND_EXPR, because in a conditional expression
1107 the default promotions are applied to both sides, and this would yield
1108 the wrong type of the result; for example, if the components have
1109 type "char". */
1110 switch (TREE_CODE (datum))
1112 case COMPOUND_EXPR:
1114 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1115 return build (COMPOUND_EXPR, TREE_TYPE (value),
1116 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1118 default:
1119 break;
1122 /* See if there is a field or component with name COMPONENT. */
1124 if (code == RECORD_TYPE || code == UNION_TYPE)
1126 if (!COMPLETE_TYPE_P (type))
1128 c_incomplete_type_error (NULL_TREE, type);
1129 return error_mark_node;
1132 field = lookup_field (datum, component);
1134 if (!field)
1136 error ("%s has no member named `%s'",
1137 code == RECORD_TYPE ? "structure" : "union",
1138 IDENTIFIER_POINTER (component));
1139 return error_mark_node;
1142 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1143 This might be better solved in future the way the C++ front
1144 end does it - by giving the anonymous entities each a
1145 separate name and type, and then have build_component_ref
1146 recursively call itself. We can't do that here. */
1149 tree subdatum = TREE_VALUE (field);
1151 if (TREE_TYPE (subdatum) == error_mark_node)
1152 return error_mark_node;
1154 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1155 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1156 TREE_READONLY (ref) = 1;
1157 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1158 TREE_THIS_VOLATILE (ref) = 1;
1160 if (TREE_DEPRECATED (subdatum))
1161 warn_deprecated_use (subdatum);
1163 datum = ref;
1165 field = TREE_CHAIN (field);
1167 while (field);
1169 return ref;
1171 else if (code != ERROR_MARK)
1172 error ("request for member `%s' in something not a structure or union",
1173 IDENTIFIER_POINTER (component));
1175 return error_mark_node;
1178 /* Given an expression PTR for a pointer, return an expression
1179 for the value pointed to.
1180 ERRORSTRING is the name of the operator to appear in error messages. */
1182 tree
1183 build_indirect_ref (tree ptr, const char *errorstring)
1185 tree pointer = default_conversion (ptr);
1186 tree type = TREE_TYPE (pointer);
1188 if (TREE_CODE (type) == POINTER_TYPE)
1190 if (TREE_CODE (pointer) == ADDR_EXPR
1191 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1192 == TREE_TYPE (type)))
1193 return TREE_OPERAND (pointer, 0);
1194 else
1196 tree t = TREE_TYPE (type);
1197 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1199 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1201 error ("dereferencing pointer to incomplete type");
1202 return error_mark_node;
1204 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1205 warning ("dereferencing `void *' pointer");
1207 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1208 so that we get the proper error message if the result is used
1209 to assign to. Also, &* is supposed to be a no-op.
1210 And ANSI C seems to specify that the type of the result
1211 should be the const type. */
1212 /* A de-reference of a pointer to const is not a const. It is valid
1213 to change it via some other pointer. */
1214 TREE_READONLY (ref) = TYPE_READONLY (t);
1215 TREE_SIDE_EFFECTS (ref)
1216 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1217 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1218 return ref;
1221 else if (TREE_CODE (pointer) != ERROR_MARK)
1222 error ("invalid type argument of `%s'", errorstring);
1223 return error_mark_node;
1226 /* This handles expressions of the form "a[i]", which denotes
1227 an array reference.
1229 This is logically equivalent in C to *(a+i), but we may do it differently.
1230 If A is a variable or a member, we generate a primitive ARRAY_REF.
1231 This avoids forcing the array out of registers, and can work on
1232 arrays that are not lvalues (for example, members of structures returned
1233 by functions). */
1235 tree
1236 build_array_ref (tree array, tree index)
1238 if (index == 0)
1240 error ("subscript missing in array reference");
1241 return error_mark_node;
1244 if (TREE_TYPE (array) == error_mark_node
1245 || TREE_TYPE (index) == error_mark_node)
1246 return error_mark_node;
1248 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1249 && TREE_CODE (array) != INDIRECT_REF)
1251 tree rval, type;
1253 /* Subscripting with type char is likely to lose
1254 on a machine where chars are signed.
1255 So warn on any machine, but optionally.
1256 Don't warn for unsigned char since that type is safe.
1257 Don't warn for signed char because anyone who uses that
1258 must have done so deliberately. */
1259 if (warn_char_subscripts
1260 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1261 warning ("array subscript has type `char'");
1263 /* Apply default promotions *after* noticing character types. */
1264 index = default_conversion (index);
1266 /* Require integer *after* promotion, for sake of enums. */
1267 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1269 error ("array subscript is not an integer");
1270 return error_mark_node;
1273 /* An array that is indexed by a non-constant
1274 cannot be stored in a register; we must be able to do
1275 address arithmetic on its address.
1276 Likewise an array of elements of variable size. */
1277 if (TREE_CODE (index) != INTEGER_CST
1278 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1279 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1281 if (!c_mark_addressable (array))
1282 return error_mark_node;
1284 /* An array that is indexed by a constant value which is not within
1285 the array bounds cannot be stored in a register either; because we
1286 would get a crash in store_bit_field/extract_bit_field when trying
1287 to access a non-existent part of the register. */
1288 if (TREE_CODE (index) == INTEGER_CST
1289 && TYPE_VALUES (TREE_TYPE (array))
1290 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1292 if (!c_mark_addressable (array))
1293 return error_mark_node;
1296 if (pedantic)
1298 tree foo = array;
1299 while (TREE_CODE (foo) == COMPONENT_REF)
1300 foo = TREE_OPERAND (foo, 0);
1301 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1302 pedwarn ("ISO C forbids subscripting `register' array");
1303 else if (! flag_isoc99 && ! lvalue_p (foo))
1304 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1307 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1308 rval = build (ARRAY_REF, type, array, index);
1309 /* Array ref is const/volatile if the array elements are
1310 or if the array is. */
1311 TREE_READONLY (rval)
1312 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1313 | TREE_READONLY (array));
1314 TREE_SIDE_EFFECTS (rval)
1315 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1316 | TREE_SIDE_EFFECTS (array));
1317 TREE_THIS_VOLATILE (rval)
1318 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1319 /* This was added by rms on 16 Nov 91.
1320 It fixes vol struct foo *a; a->elts[1]
1321 in an inline function.
1322 Hope it doesn't break something else. */
1323 | TREE_THIS_VOLATILE (array));
1324 return require_complete_type (fold (rval));
1328 tree ar = default_conversion (array);
1329 tree ind = default_conversion (index);
1331 /* Do the same warning check as above, but only on the part that's
1332 syntactically the index and only if it is also semantically
1333 the index. */
1334 if (warn_char_subscripts
1335 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1336 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1337 warning ("subscript has type `char'");
1339 /* Put the integer in IND to simplify error checking. */
1340 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1342 tree temp = ar;
1343 ar = ind;
1344 ind = temp;
1347 if (ar == error_mark_node)
1348 return ar;
1350 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1351 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1353 error ("subscripted value is neither array nor pointer");
1354 return error_mark_node;
1356 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1358 error ("array subscript is not an integer");
1359 return error_mark_node;
1362 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1363 "array indexing");
1367 /* Issue an error message for a reference to an undeclared variable ID,
1368 including a reference to a builtin outside of function-call context.
1369 Arrange to suppress further errors for the same identifier. */
1370 static void
1371 undeclared_variable (tree id)
1373 if (current_function_decl == 0)
1375 error ("`%s' undeclared here (not in a function)",
1376 IDENTIFIER_POINTER (id));
1377 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1379 else
1381 error ("`%s' undeclared (first use in this function)",
1382 IDENTIFIER_POINTER (id));
1384 if (! undeclared_variable_notice)
1386 error ("(Each undeclared identifier is reported only once");
1387 error ("for each function it appears in.)");
1388 undeclared_variable_notice = 1;
1391 /* Set IDENTIFIER_SYMBOL_VALUE (id) to error_mark_node
1392 at function scope. This suppresses further warnings
1393 about this undeclared identifier in this function. */
1394 pushdecl_function_level (error_mark_node, id);
1398 /* Build an external reference to identifier ID. FUN indicates
1399 whether this will be used for a function call. */
1400 tree
1401 build_external_ref (tree id, int fun)
1403 tree ref;
1404 tree decl = lookup_name (id);
1405 tree objc_ivar = lookup_objc_ivar (id);
1407 if (decl && decl != error_mark_node)
1409 /* Properly declared variable or function reference. */
1410 if (!objc_ivar)
1411 ref = decl;
1412 else if (decl != objc_ivar && DECL_CONTEXT (decl) != 0)
1414 warning ("local declaration of `%s' hides instance variable",
1415 IDENTIFIER_POINTER (id));
1416 ref = decl;
1418 else
1419 ref = objc_ivar;
1421 else if (objc_ivar)
1422 ref = objc_ivar;
1423 else if (fun)
1424 /* Implicit function declaration. */
1425 ref = implicitly_declare (id);
1426 else if (decl == error_mark_node)
1427 /* Don't complain about something that's already been
1428 complained about. */
1429 return error_mark_node;
1430 else
1432 undeclared_variable (id);
1433 return error_mark_node;
1436 if (TREE_TYPE (ref) == error_mark_node)
1437 return error_mark_node;
1439 if (TREE_DEPRECATED (ref))
1440 warn_deprecated_use (ref);
1442 if (!skip_evaluation)
1443 assemble_external (ref);
1444 TREE_USED (ref) = 1;
1446 if (TREE_CODE (ref) == CONST_DECL)
1448 ref = DECL_INITIAL (ref);
1449 TREE_CONSTANT (ref) = 1;
1451 else if (current_function_decl != 0
1452 && DECL_CONTEXT (current_function_decl) != 0
1453 && (TREE_CODE (ref) == VAR_DECL
1454 || TREE_CODE (ref) == PARM_DECL
1455 || TREE_CODE (ref) == FUNCTION_DECL))
1457 tree context = decl_function_context (ref);
1459 if (context != 0 && context != current_function_decl)
1460 DECL_NONLOCAL (ref) = 1;
1463 return ref;
1466 /* Build a function call to function FUNCTION with parameters PARAMS.
1467 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1468 TREE_VALUE of each node is a parameter-expression.
1469 FUNCTION's data type may be a function type or a pointer-to-function. */
1471 tree
1472 build_function_call (tree function, tree params)
1474 tree fntype, fundecl = 0;
1475 tree coerced_params;
1476 tree name = NULL_TREE, result;
1478 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1479 STRIP_TYPE_NOPS (function);
1481 /* Convert anything with function type to a pointer-to-function. */
1482 if (TREE_CODE (function) == FUNCTION_DECL)
1484 name = DECL_NAME (function);
1486 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1487 (because calling an inline function does not mean the function
1488 needs to be separately compiled). */
1489 fntype = build_type_variant (TREE_TYPE (function),
1490 TREE_READONLY (function),
1491 TREE_THIS_VOLATILE (function));
1492 fundecl = function;
1493 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1495 else
1496 function = default_conversion (function);
1498 fntype = TREE_TYPE (function);
1500 if (TREE_CODE (fntype) == ERROR_MARK)
1501 return error_mark_node;
1503 if (!(TREE_CODE (fntype) == POINTER_TYPE
1504 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1506 error ("called object is not a function");
1507 return error_mark_node;
1510 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1511 current_function_returns_abnormally = 1;
1513 /* fntype now gets the type of function pointed to. */
1514 fntype = TREE_TYPE (fntype);
1516 /* Convert the parameters to the types declared in the
1517 function prototype, or apply default promotions. */
1519 coerced_params
1520 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1522 /* Check that the arguments to the function are valid. */
1524 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1526 /* Recognize certain built-in functions so we can make tree-codes
1527 other than CALL_EXPR. We do this when it enables fold-const.c
1528 to do something useful. */
1530 if (TREE_CODE (function) == ADDR_EXPR
1531 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1532 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1534 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1535 params, coerced_params);
1536 if (result)
1537 return result;
1540 result = build (CALL_EXPR, TREE_TYPE (fntype),
1541 function, coerced_params, NULL_TREE);
1542 TREE_SIDE_EFFECTS (result) = 1;
1543 result = fold (result);
1545 if (VOID_TYPE_P (TREE_TYPE (result)))
1546 return result;
1547 return require_complete_type (result);
1550 /* Convert the argument expressions in the list VALUES
1551 to the types in the list TYPELIST. The result is a list of converted
1552 argument expressions.
1554 If TYPELIST is exhausted, or when an element has NULL as its type,
1555 perform the default conversions.
1557 PARMLIST is the chain of parm decls for the function being called.
1558 It may be 0, if that info is not available.
1559 It is used only for generating error messages.
1561 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1563 This is also where warnings about wrong number of args are generated.
1565 Both VALUES and the returned value are chains of TREE_LIST nodes
1566 with the elements of the list in the TREE_VALUE slots of those nodes. */
1568 static tree
1569 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1571 tree typetail, valtail;
1572 tree result = NULL;
1573 int parmnum;
1575 /* Scan the given expressions and types, producing individual
1576 converted arguments and pushing them on RESULT in reverse order. */
1578 for (valtail = values, typetail = typelist, parmnum = 0;
1579 valtail;
1580 valtail = TREE_CHAIN (valtail), parmnum++)
1582 tree type = typetail ? TREE_VALUE (typetail) : 0;
1583 tree val = TREE_VALUE (valtail);
1585 if (type == void_type_node)
1587 if (name)
1588 error ("too many arguments to function `%s'",
1589 IDENTIFIER_POINTER (name));
1590 else
1591 error ("too many arguments to function");
1592 break;
1595 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1596 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1597 to convert automatically to a pointer. */
1598 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1599 val = TREE_OPERAND (val, 0);
1601 val = default_function_array_conversion (val);
1603 val = require_complete_type (val);
1605 if (type != 0)
1607 /* Formal parm type is specified by a function prototype. */
1608 tree parmval;
1610 if (!COMPLETE_TYPE_P (type))
1612 error ("type of formal parameter %d is incomplete", parmnum + 1);
1613 parmval = val;
1615 else
1617 /* Optionally warn about conversions that
1618 differ from the default conversions. */
1619 if (warn_conversion || warn_traditional)
1621 int formal_prec = TYPE_PRECISION (type);
1623 if (INTEGRAL_TYPE_P (type)
1624 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1625 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1626 if (INTEGRAL_TYPE_P (type)
1627 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1628 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1629 else if (TREE_CODE (type) == COMPLEX_TYPE
1630 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1631 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1632 else if (TREE_CODE (type) == REAL_TYPE
1633 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1634 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1635 else if (TREE_CODE (type) == COMPLEX_TYPE
1636 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1637 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1638 else if (TREE_CODE (type) == REAL_TYPE
1639 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1640 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1641 /* ??? At some point, messages should be written about
1642 conversions between complex types, but that's too messy
1643 to do now. */
1644 else if (TREE_CODE (type) == REAL_TYPE
1645 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1647 /* Warn if any argument is passed as `float',
1648 since without a prototype it would be `double'. */
1649 if (formal_prec == TYPE_PRECISION (float_type_node))
1650 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1652 /* Detect integer changing in width or signedness.
1653 These warnings are only activated with
1654 -Wconversion, not with -Wtraditional. */
1655 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1656 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1658 tree would_have_been = default_conversion (val);
1659 tree type1 = TREE_TYPE (would_have_been);
1661 if (TREE_CODE (type) == ENUMERAL_TYPE
1662 && (TYPE_MAIN_VARIANT (type)
1663 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1664 /* No warning if function asks for enum
1665 and the actual arg is that enum type. */
1667 else if (formal_prec != TYPE_PRECISION (type1))
1668 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1669 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1671 /* Don't complain if the formal parameter type
1672 is an enum, because we can't tell now whether
1673 the value was an enum--even the same enum. */
1674 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1676 else if (TREE_CODE (val) == INTEGER_CST
1677 && int_fits_type_p (val, type))
1678 /* Change in signedness doesn't matter
1679 if a constant value is unaffected. */
1681 /* Likewise for a constant in a NOP_EXPR. */
1682 else if (TREE_CODE (val) == NOP_EXPR
1683 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1684 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1686 /* If the value is extended from a narrower
1687 unsigned type, it doesn't matter whether we
1688 pass it as signed or unsigned; the value
1689 certainly is the same either way. */
1690 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1691 && TREE_UNSIGNED (TREE_TYPE (val)))
1693 else if (TREE_UNSIGNED (type))
1694 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1695 else
1696 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1700 parmval = convert_for_assignment (type, val,
1701 (char *) 0, /* arg passing */
1702 fundecl, name, parmnum + 1);
1704 if (PROMOTE_PROTOTYPES
1705 && INTEGRAL_TYPE_P (type)
1706 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1707 parmval = default_conversion (parmval);
1709 result = tree_cons (NULL_TREE, parmval, result);
1711 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1712 && (TYPE_PRECISION (TREE_TYPE (val))
1713 < TYPE_PRECISION (double_type_node)))
1714 /* Convert `float' to `double'. */
1715 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1716 else
1717 /* Convert `short' and `char' to full-size `int'. */
1718 result = tree_cons (NULL_TREE, default_conversion (val), result);
1720 if (typetail)
1721 typetail = TREE_CHAIN (typetail);
1724 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1726 if (name)
1727 error ("too few arguments to function `%s'",
1728 IDENTIFIER_POINTER (name));
1729 else
1730 error ("too few arguments to function");
1733 return nreverse (result);
1736 /* This is the entry point used by the parser
1737 for binary operators in the input.
1738 In addition to constructing the expression,
1739 we check for operands that were written with other binary operators
1740 in a way that is likely to confuse the user. */
1742 tree
1743 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1745 tree result = build_binary_op (code, arg1, arg2, 1);
1747 char class;
1748 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1749 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1750 enum tree_code code1 = ERROR_MARK;
1751 enum tree_code code2 = ERROR_MARK;
1753 if (TREE_CODE (result) == ERROR_MARK)
1754 return error_mark_node;
1756 if (IS_EXPR_CODE_CLASS (class1))
1757 code1 = C_EXP_ORIGINAL_CODE (arg1);
1758 if (IS_EXPR_CODE_CLASS (class2))
1759 code2 = C_EXP_ORIGINAL_CODE (arg2);
1761 /* Check for cases such as x+y<<z which users are likely
1762 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1763 is cleared to prevent these warnings. */
1764 if (warn_parentheses)
1766 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1768 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1769 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1770 warning ("suggest parentheses around + or - inside shift");
1773 if (code == TRUTH_ORIF_EXPR)
1775 if (code1 == TRUTH_ANDIF_EXPR
1776 || code2 == TRUTH_ANDIF_EXPR)
1777 warning ("suggest parentheses around && within ||");
1780 if (code == BIT_IOR_EXPR)
1782 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1783 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1784 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1785 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1786 warning ("suggest parentheses around arithmetic in operand of |");
1787 /* Check cases like x|y==z */
1788 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1789 warning ("suggest parentheses around comparison in operand of |");
1792 if (code == BIT_XOR_EXPR)
1794 if (code1 == BIT_AND_EXPR
1795 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1796 || code2 == BIT_AND_EXPR
1797 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1798 warning ("suggest parentheses around arithmetic in operand of ^");
1799 /* Check cases like x^y==z */
1800 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1801 warning ("suggest parentheses around comparison in operand of ^");
1804 if (code == BIT_AND_EXPR)
1806 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1807 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1808 warning ("suggest parentheses around + or - in operand of &");
1809 /* Check cases like x&y==z */
1810 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1811 warning ("suggest parentheses around comparison in operand of &");
1815 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1816 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1817 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1818 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1820 unsigned_conversion_warning (result, arg1);
1821 unsigned_conversion_warning (result, arg2);
1822 overflow_warning (result);
1824 class = TREE_CODE_CLASS (TREE_CODE (result));
1826 /* Record the code that was specified in the source,
1827 for the sake of warnings about confusing nesting. */
1828 if (IS_EXPR_CODE_CLASS (class))
1829 C_SET_EXP_ORIGINAL_CODE (result, code);
1830 else
1832 int flag = TREE_CONSTANT (result);
1833 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1834 so that convert_for_assignment wouldn't strip it.
1835 That way, we got warnings for things like p = (1 - 1).
1836 But it turns out we should not get those warnings. */
1837 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1838 C_SET_EXP_ORIGINAL_CODE (result, code);
1839 TREE_CONSTANT (result) = flag;
1842 return result;
1845 /* Build a binary-operation expression without default conversions.
1846 CODE is the kind of expression to build.
1847 This function differs from `build' in several ways:
1848 the data type of the result is computed and recorded in it,
1849 warnings are generated if arg data types are invalid,
1850 special handling for addition and subtraction of pointers is known,
1851 and some optimization is done (operations on narrow ints
1852 are done in the narrower type when that gives the same result).
1853 Constant folding is also done before the result is returned.
1855 Note that the operands will never have enumeral types, or function
1856 or array types, because either they will have the default conversions
1857 performed or they have both just been converted to some other type in which
1858 the arithmetic is to be done. */
1860 tree
1861 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
1862 int convert_p)
1864 tree type0, type1;
1865 enum tree_code code0, code1;
1866 tree op0, op1;
1868 /* Expression code to give to the expression when it is built.
1869 Normally this is CODE, which is what the caller asked for,
1870 but in some special cases we change it. */
1871 enum tree_code resultcode = code;
1873 /* Data type in which the computation is to be performed.
1874 In the simplest cases this is the common type of the arguments. */
1875 tree result_type = NULL;
1877 /* Nonzero means operands have already been type-converted
1878 in whatever way is necessary.
1879 Zero means they need to be converted to RESULT_TYPE. */
1880 int converted = 0;
1882 /* Nonzero means create the expression with this type, rather than
1883 RESULT_TYPE. */
1884 tree build_type = 0;
1886 /* Nonzero means after finally constructing the expression
1887 convert it to this type. */
1888 tree final_type = 0;
1890 /* Nonzero if this is an operation like MIN or MAX which can
1891 safely be computed in short if both args are promoted shorts.
1892 Also implies COMMON.
1893 -1 indicates a bitwise operation; this makes a difference
1894 in the exact conditions for when it is safe to do the operation
1895 in a narrower mode. */
1896 int shorten = 0;
1898 /* Nonzero if this is a comparison operation;
1899 if both args are promoted shorts, compare the original shorts.
1900 Also implies COMMON. */
1901 int short_compare = 0;
1903 /* Nonzero if this is a right-shift operation, which can be computed on the
1904 original short and then promoted if the operand is a promoted short. */
1905 int short_shift = 0;
1907 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1908 int common = 0;
1910 if (convert_p)
1912 op0 = default_conversion (orig_op0);
1913 op1 = default_conversion (orig_op1);
1915 else
1917 op0 = orig_op0;
1918 op1 = orig_op1;
1921 type0 = TREE_TYPE (op0);
1922 type1 = TREE_TYPE (op1);
1924 /* The expression codes of the data types of the arguments tell us
1925 whether the arguments are integers, floating, pointers, etc. */
1926 code0 = TREE_CODE (type0);
1927 code1 = TREE_CODE (type1);
1929 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1930 STRIP_TYPE_NOPS (op0);
1931 STRIP_TYPE_NOPS (op1);
1933 /* If an error was already reported for one of the arguments,
1934 avoid reporting another error. */
1936 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1937 return error_mark_node;
1939 switch (code)
1941 case PLUS_EXPR:
1942 /* Handle the pointer + int case. */
1943 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1944 return pointer_int_sum (PLUS_EXPR, op0, op1);
1945 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1946 return pointer_int_sum (PLUS_EXPR, op1, op0);
1947 else
1948 common = 1;
1949 break;
1951 case MINUS_EXPR:
1952 /* Subtraction of two similar pointers.
1953 We must subtract them as integers, then divide by object size. */
1954 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1955 && comp_target_types (type0, type1, 1))
1956 return pointer_diff (op0, op1);
1957 /* Handle pointer minus int. Just like pointer plus int. */
1958 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1959 return pointer_int_sum (MINUS_EXPR, op0, op1);
1960 else
1961 common = 1;
1962 break;
1964 case MULT_EXPR:
1965 common = 1;
1966 break;
1968 case TRUNC_DIV_EXPR:
1969 case CEIL_DIV_EXPR:
1970 case FLOOR_DIV_EXPR:
1971 case ROUND_DIV_EXPR:
1972 case EXACT_DIV_EXPR:
1973 /* Floating point division by zero is a legitimate way to obtain
1974 infinities and NaNs. */
1975 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
1976 warning ("division by zero");
1978 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1979 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
1980 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1981 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
1983 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1984 resultcode = RDIV_EXPR;
1985 else
1986 /* Although it would be tempting to shorten always here, that
1987 loses on some targets, since the modulo instruction is
1988 undefined if the quotient can't be represented in the
1989 computation mode. We shorten only if unsigned or if
1990 dividing by something we know != -1. */
1991 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1992 || (TREE_CODE (op1) == INTEGER_CST
1993 && ! integer_all_onesp (op1)));
1994 common = 1;
1996 break;
1998 case BIT_AND_EXPR:
1999 case BIT_ANDTC_EXPR:
2000 case BIT_IOR_EXPR:
2001 case BIT_XOR_EXPR:
2002 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2003 shorten = -1;
2004 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2005 common = 1;
2006 break;
2008 case TRUNC_MOD_EXPR:
2009 case FLOOR_MOD_EXPR:
2010 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2011 warning ("division by zero");
2013 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2015 /* Although it would be tempting to shorten always here, that loses
2016 on some targets, since the modulo instruction is undefined if the
2017 quotient can't be represented in the computation mode. We shorten
2018 only if unsigned or if dividing by something we know != -1. */
2019 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2020 || (TREE_CODE (op1) == INTEGER_CST
2021 && ! integer_all_onesp (op1)));
2022 common = 1;
2024 break;
2026 case TRUTH_ANDIF_EXPR:
2027 case TRUTH_ORIF_EXPR:
2028 case TRUTH_AND_EXPR:
2029 case TRUTH_OR_EXPR:
2030 case TRUTH_XOR_EXPR:
2031 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2032 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2033 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2034 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2036 /* Result of these operations is always an int,
2037 but that does not mean the operands should be
2038 converted to ints! */
2039 result_type = integer_type_node;
2040 op0 = c_common_truthvalue_conversion (op0);
2041 op1 = c_common_truthvalue_conversion (op1);
2042 converted = 1;
2044 break;
2046 /* Shift operations: result has same type as first operand;
2047 always convert second operand to int.
2048 Also set SHORT_SHIFT if shifting rightward. */
2050 case RSHIFT_EXPR:
2051 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2053 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2055 if (tree_int_cst_sgn (op1) < 0)
2056 warning ("right shift count is negative");
2057 else
2059 if (! integer_zerop (op1))
2060 short_shift = 1;
2062 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2063 warning ("right shift count >= width of type");
2067 /* Use the type of the value to be shifted. */
2068 result_type = type0;
2069 /* Convert the shift-count to an integer, regardless of size
2070 of value being shifted. */
2071 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2072 op1 = convert (integer_type_node, op1);
2073 /* Avoid converting op1 to result_type later. */
2074 converted = 1;
2076 break;
2078 case LSHIFT_EXPR:
2079 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2081 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2083 if (tree_int_cst_sgn (op1) < 0)
2084 warning ("left shift count is negative");
2086 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2087 warning ("left shift count >= width of type");
2090 /* Use the type of the value to be shifted. */
2091 result_type = type0;
2092 /* Convert the shift-count to an integer, regardless of size
2093 of value being shifted. */
2094 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2095 op1 = convert (integer_type_node, op1);
2096 /* Avoid converting op1 to result_type later. */
2097 converted = 1;
2099 break;
2101 case RROTATE_EXPR:
2102 case LROTATE_EXPR:
2103 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2105 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2107 if (tree_int_cst_sgn (op1) < 0)
2108 warning ("shift count is negative");
2109 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2110 warning ("shift count >= width of type");
2113 /* Use the type of the value to be shifted. */
2114 result_type = type0;
2115 /* Convert the shift-count to an integer, regardless of size
2116 of value being shifted. */
2117 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2118 op1 = convert (integer_type_node, op1);
2119 /* Avoid converting op1 to result_type later. */
2120 converted = 1;
2122 break;
2124 case EQ_EXPR:
2125 case NE_EXPR:
2126 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2127 warning ("comparing floating point with == or != is unsafe");
2128 /* Result of comparison is always int,
2129 but don't convert the args to int! */
2130 build_type = integer_type_node;
2131 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2132 || code0 == COMPLEX_TYPE
2133 || code0 == VECTOR_TYPE)
2134 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2135 || code1 == COMPLEX_TYPE
2136 || code1 == VECTOR_TYPE))
2137 short_compare = 1;
2138 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2140 tree tt0 = TREE_TYPE (type0);
2141 tree tt1 = TREE_TYPE (type1);
2142 /* Anything compares with void *. void * compares with anything.
2143 Otherwise, the targets must be compatible
2144 and both must be object or both incomplete. */
2145 if (comp_target_types (type0, type1, 1))
2146 result_type = common_type (type0, type1);
2147 else if (VOID_TYPE_P (tt0))
2149 /* op0 != orig_op0 detects the case of something
2150 whose value is 0 but which isn't a valid null ptr const. */
2151 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2152 && TREE_CODE (tt1) == FUNCTION_TYPE)
2153 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2155 else if (VOID_TYPE_P (tt1))
2157 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2158 && TREE_CODE (tt0) == FUNCTION_TYPE)
2159 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2161 else
2162 pedwarn ("comparison of distinct pointer types lacks a cast");
2164 if (result_type == NULL_TREE)
2165 result_type = ptr_type_node;
2167 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2168 && integer_zerop (op1))
2169 result_type = type0;
2170 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2171 && integer_zerop (op0))
2172 result_type = type1;
2173 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2175 result_type = type0;
2176 pedwarn ("comparison between pointer and integer");
2178 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2180 result_type = type1;
2181 pedwarn ("comparison between pointer and integer");
2183 break;
2185 case MAX_EXPR:
2186 case MIN_EXPR:
2187 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2188 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2189 shorten = 1;
2190 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2192 if (comp_target_types (type0, type1, 1))
2194 result_type = common_type (type0, type1);
2195 if (pedantic
2196 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2197 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2199 else
2201 result_type = ptr_type_node;
2202 pedwarn ("comparison of distinct pointer types lacks a cast");
2205 break;
2207 case LE_EXPR:
2208 case GE_EXPR:
2209 case LT_EXPR:
2210 case GT_EXPR:
2211 build_type = integer_type_node;
2212 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2213 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2214 short_compare = 1;
2215 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2217 if (comp_target_types (type0, type1, 1))
2219 result_type = common_type (type0, type1);
2220 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2221 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2222 pedwarn ("comparison of complete and incomplete pointers");
2223 else if (pedantic
2224 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2225 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2227 else
2229 result_type = ptr_type_node;
2230 pedwarn ("comparison of distinct pointer types lacks a cast");
2233 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2234 && integer_zerop (op1))
2236 result_type = type0;
2237 if (pedantic || extra_warnings)
2238 pedwarn ("ordered comparison of pointer with integer zero");
2240 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2241 && integer_zerop (op0))
2243 result_type = type1;
2244 if (pedantic)
2245 pedwarn ("ordered comparison of pointer with integer zero");
2247 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2249 result_type = type0;
2250 pedwarn ("comparison between pointer and integer");
2252 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2254 result_type = type1;
2255 pedwarn ("comparison between pointer and integer");
2257 break;
2259 case UNORDERED_EXPR:
2260 case ORDERED_EXPR:
2261 case UNLT_EXPR:
2262 case UNLE_EXPR:
2263 case UNGT_EXPR:
2264 case UNGE_EXPR:
2265 case UNEQ_EXPR:
2266 build_type = integer_type_node;
2267 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2269 error ("unordered comparison on non-floating point argument");
2270 return error_mark_node;
2272 common = 1;
2273 break;
2275 default:
2276 break;
2279 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2280 || code0 == VECTOR_TYPE)
2282 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2283 || code1 == VECTOR_TYPE))
2285 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2287 if (shorten || common || short_compare)
2288 result_type = common_type (type0, type1);
2290 /* For certain operations (which identify themselves by shorten != 0)
2291 if both args were extended from the same smaller type,
2292 do the arithmetic in that type and then extend.
2294 shorten !=0 and !=1 indicates a bitwise operation.
2295 For them, this optimization is safe only if
2296 both args are zero-extended or both are sign-extended.
2297 Otherwise, we might change the result.
2298 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2299 but calculated in (unsigned short) it would be (unsigned short)-1. */
2301 if (shorten && none_complex)
2303 int unsigned0, unsigned1;
2304 tree arg0 = get_narrower (op0, &unsigned0);
2305 tree arg1 = get_narrower (op1, &unsigned1);
2306 /* UNS is 1 if the operation to be done is an unsigned one. */
2307 int uns = TREE_UNSIGNED (result_type);
2308 tree type;
2310 final_type = result_type;
2312 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2313 but it *requires* conversion to FINAL_TYPE. */
2315 if ((TYPE_PRECISION (TREE_TYPE (op0))
2316 == TYPE_PRECISION (TREE_TYPE (arg0)))
2317 && TREE_TYPE (op0) != final_type)
2318 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2319 if ((TYPE_PRECISION (TREE_TYPE (op1))
2320 == TYPE_PRECISION (TREE_TYPE (arg1)))
2321 && TREE_TYPE (op1) != final_type)
2322 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2324 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2326 /* For bitwise operations, signedness of nominal type
2327 does not matter. Consider only how operands were extended. */
2328 if (shorten == -1)
2329 uns = unsigned0;
2331 /* Note that in all three cases below we refrain from optimizing
2332 an unsigned operation on sign-extended args.
2333 That would not be valid. */
2335 /* Both args variable: if both extended in same way
2336 from same width, do it in that width.
2337 Do it unsigned if args were zero-extended. */
2338 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2339 < TYPE_PRECISION (result_type))
2340 && (TYPE_PRECISION (TREE_TYPE (arg1))
2341 == TYPE_PRECISION (TREE_TYPE (arg0)))
2342 && unsigned0 == unsigned1
2343 && (unsigned0 || !uns))
2344 result_type
2345 = c_common_signed_or_unsigned_type
2346 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2347 else if (TREE_CODE (arg0) == INTEGER_CST
2348 && (unsigned1 || !uns)
2349 && (TYPE_PRECISION (TREE_TYPE (arg1))
2350 < TYPE_PRECISION (result_type))
2351 && (type
2352 = c_common_signed_or_unsigned_type (unsigned1,
2353 TREE_TYPE (arg1)),
2354 int_fits_type_p (arg0, type)))
2355 result_type = type;
2356 else if (TREE_CODE (arg1) == INTEGER_CST
2357 && (unsigned0 || !uns)
2358 && (TYPE_PRECISION (TREE_TYPE (arg0))
2359 < TYPE_PRECISION (result_type))
2360 && (type
2361 = c_common_signed_or_unsigned_type (unsigned0,
2362 TREE_TYPE (arg0)),
2363 int_fits_type_p (arg1, type)))
2364 result_type = type;
2367 /* Shifts can be shortened if shifting right. */
2369 if (short_shift)
2371 int unsigned_arg;
2372 tree arg0 = get_narrower (op0, &unsigned_arg);
2374 final_type = result_type;
2376 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2377 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2379 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2380 /* We can shorten only if the shift count is less than the
2381 number of bits in the smaller type size. */
2382 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2383 /* We cannot drop an unsigned shift after sign-extension. */
2384 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2386 /* Do an unsigned shift if the operand was zero-extended. */
2387 result_type
2388 = c_common_signed_or_unsigned_type (unsigned_arg,
2389 TREE_TYPE (arg0));
2390 /* Convert value-to-be-shifted to that type. */
2391 if (TREE_TYPE (op0) != result_type)
2392 op0 = convert (result_type, op0);
2393 converted = 1;
2397 /* Comparison operations are shortened too but differently.
2398 They identify themselves by setting short_compare = 1. */
2400 if (short_compare)
2402 /* Don't write &op0, etc., because that would prevent op0
2403 from being kept in a register.
2404 Instead, make copies of the our local variables and
2405 pass the copies by reference, then copy them back afterward. */
2406 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2407 enum tree_code xresultcode = resultcode;
2408 tree val
2409 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2411 if (val != 0)
2412 return val;
2414 op0 = xop0, op1 = xop1;
2415 converted = 1;
2416 resultcode = xresultcode;
2418 if (warn_sign_compare && skip_evaluation == 0)
2420 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2421 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2422 int unsignedp0, unsignedp1;
2423 tree primop0 = get_narrower (op0, &unsignedp0);
2424 tree primop1 = get_narrower (op1, &unsignedp1);
2426 xop0 = orig_op0;
2427 xop1 = orig_op1;
2428 STRIP_TYPE_NOPS (xop0);
2429 STRIP_TYPE_NOPS (xop1);
2431 /* Give warnings for comparisons between signed and unsigned
2432 quantities that may fail.
2434 Do the checking based on the original operand trees, so that
2435 casts will be considered, but default promotions won't be.
2437 Do not warn if the comparison is being done in a signed type,
2438 since the signed type will only be chosen if it can represent
2439 all the values of the unsigned type. */
2440 if (! TREE_UNSIGNED (result_type))
2441 /* OK */;
2442 /* Do not warn if both operands are the same signedness. */
2443 else if (op0_signed == op1_signed)
2444 /* OK */;
2445 else
2447 tree sop, uop;
2449 if (op0_signed)
2450 sop = xop0, uop = xop1;
2451 else
2452 sop = xop1, uop = xop0;
2454 /* Do not warn if the signed quantity is an
2455 unsuffixed integer literal (or some static
2456 constant expression involving such literals or a
2457 conditional expression involving such literals)
2458 and it is non-negative. */
2459 if (c_tree_expr_nonnegative_p (sop))
2460 /* OK */;
2461 /* Do not warn if the comparison is an equality operation,
2462 the unsigned quantity is an integral constant, and it
2463 would fit in the result if the result were signed. */
2464 else if (TREE_CODE (uop) == INTEGER_CST
2465 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2466 && int_fits_type_p
2467 (uop, c_common_signed_type (result_type)))
2468 /* OK */;
2469 /* Do not warn if the unsigned quantity is an enumeration
2470 constant and its maximum value would fit in the result
2471 if the result were signed. */
2472 else if (TREE_CODE (uop) == INTEGER_CST
2473 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2474 && int_fits_type_p
2475 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2476 c_common_signed_type (result_type)))
2477 /* OK */;
2478 else
2479 warning ("comparison between signed and unsigned");
2482 /* Warn if two unsigned values are being compared in a size
2483 larger than their original size, and one (and only one) is the
2484 result of a `~' operator. This comparison will always fail.
2486 Also warn if one operand is a constant, and the constant
2487 does not have all bits set that are set in the ~ operand
2488 when it is extended. */
2490 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2491 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2493 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2494 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2495 &unsignedp0);
2496 else
2497 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2498 &unsignedp1);
2500 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2502 tree primop;
2503 HOST_WIDE_INT constant, mask;
2504 int unsignedp, bits;
2506 if (host_integerp (primop0, 0))
2508 primop = primop1;
2509 unsignedp = unsignedp1;
2510 constant = tree_low_cst (primop0, 0);
2512 else
2514 primop = primop0;
2515 unsignedp = unsignedp0;
2516 constant = tree_low_cst (primop1, 0);
2519 bits = TYPE_PRECISION (TREE_TYPE (primop));
2520 if (bits < TYPE_PRECISION (result_type)
2521 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2523 mask = (~ (HOST_WIDE_INT) 0) << bits;
2524 if ((mask & constant) != mask)
2525 warning ("comparison of promoted ~unsigned with constant");
2528 else if (unsignedp0 && unsignedp1
2529 && (TYPE_PRECISION (TREE_TYPE (primop0))
2530 < TYPE_PRECISION (result_type))
2531 && (TYPE_PRECISION (TREE_TYPE (primop1))
2532 < TYPE_PRECISION (result_type)))
2533 warning ("comparison of promoted ~unsigned with unsigned");
2539 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2540 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2541 Then the expression will be built.
2542 It will be given type FINAL_TYPE if that is nonzero;
2543 otherwise, it will be given type RESULT_TYPE. */
2545 if (!result_type)
2547 binary_op_error (code);
2548 return error_mark_node;
2551 if (! converted)
2553 if (TREE_TYPE (op0) != result_type)
2554 op0 = convert (result_type, op0);
2555 if (TREE_TYPE (op1) != result_type)
2556 op1 = convert (result_type, op1);
2559 if (build_type == NULL_TREE)
2560 build_type = result_type;
2563 tree result = build (resultcode, build_type, op0, op1);
2564 tree folded;
2566 folded = fold (result);
2567 if (folded == result)
2568 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2569 if (final_type != 0)
2570 return convert (final_type, folded);
2571 return folded;
2576 /* Return true if `t' is known to be non-negative. */
2579 c_tree_expr_nonnegative_p (tree t)
2581 if (TREE_CODE (t) == STMT_EXPR)
2583 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2585 /* Find the last statement in the chain, ignoring the final
2586 * scope statement */
2587 while (TREE_CHAIN (t) != NULL_TREE
2588 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2589 t = TREE_CHAIN (t);
2590 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2592 return tree_expr_nonnegative_p (t);
2595 /* Return a tree for the difference of pointers OP0 and OP1.
2596 The resulting tree has type int. */
2598 static tree
2599 pointer_diff (tree op0, tree op1)
2601 tree result, folded;
2602 tree restype = ptrdiff_type_node;
2604 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2605 tree con0, con1, lit0, lit1;
2606 tree orig_op1 = op1;
2608 if (pedantic || warn_pointer_arith)
2610 if (TREE_CODE (target_type) == VOID_TYPE)
2611 pedwarn ("pointer of type `void *' used in subtraction");
2612 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2613 pedwarn ("pointer to a function used in subtraction");
2616 /* If the conversion to ptrdiff_type does anything like widening or
2617 converting a partial to an integral mode, we get a convert_expression
2618 that is in the way to do any simplifications.
2619 (fold-const.c doesn't know that the extra bits won't be needed.
2620 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2621 different mode in place.)
2622 So first try to find a common term here 'by hand'; we want to cover
2623 at least the cases that occur in legal static initializers. */
2624 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2625 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2627 if (TREE_CODE (con0) == PLUS_EXPR)
2629 lit0 = TREE_OPERAND (con0, 1);
2630 con0 = TREE_OPERAND (con0, 0);
2632 else
2633 lit0 = integer_zero_node;
2635 if (TREE_CODE (con1) == PLUS_EXPR)
2637 lit1 = TREE_OPERAND (con1, 1);
2638 con1 = TREE_OPERAND (con1, 0);
2640 else
2641 lit1 = integer_zero_node;
2643 if (operand_equal_p (con0, con1, 0))
2645 op0 = lit0;
2646 op1 = lit1;
2650 /* First do the subtraction as integers;
2651 then drop through to build the divide operator.
2652 Do not do default conversions on the minus operator
2653 in case restype is a short type. */
2655 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2656 convert (restype, op1), 0);
2657 /* This generates an error if op1 is pointer to incomplete type. */
2658 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2659 error ("arithmetic on pointer to an incomplete type");
2661 /* This generates an error if op0 is pointer to incomplete type. */
2662 op1 = c_size_in_bytes (target_type);
2664 /* Divide by the size, in easiest possible way. */
2666 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2668 folded = fold (result);
2669 if (folded == result)
2670 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2671 return folded;
2674 /* Construct and perhaps optimize a tree representation
2675 for a unary operation. CODE, a tree_code, specifies the operation
2676 and XARG is the operand.
2677 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2678 the default promotions (such as from short to int).
2679 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2680 allows non-lvalues; this is only used to handle conversion of non-lvalue
2681 arrays to pointers in C99. */
2683 tree
2684 build_unary_op (enum tree_code code, tree xarg, int flag)
2686 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2687 tree arg = xarg;
2688 tree argtype = 0;
2689 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2690 tree val;
2691 int noconvert = flag;
2693 if (typecode == ERROR_MARK)
2694 return error_mark_node;
2695 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2696 typecode = INTEGER_TYPE;
2698 switch (code)
2700 case CONVERT_EXPR:
2701 /* This is used for unary plus, because a CONVERT_EXPR
2702 is enough to prevent anybody from looking inside for
2703 associativity, but won't generate any code. */
2704 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2705 || typecode == COMPLEX_TYPE))
2707 error ("wrong type argument to unary plus");
2708 return error_mark_node;
2710 else if (!noconvert)
2711 arg = default_conversion (arg);
2712 arg = non_lvalue (arg);
2713 break;
2715 case NEGATE_EXPR:
2716 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2717 || typecode == COMPLEX_TYPE
2718 || typecode == VECTOR_TYPE))
2720 error ("wrong type argument to unary minus");
2721 return error_mark_node;
2723 else if (!noconvert)
2724 arg = default_conversion (arg);
2725 break;
2727 case BIT_NOT_EXPR:
2728 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2730 if (!noconvert)
2731 arg = default_conversion (arg);
2733 else if (typecode == COMPLEX_TYPE)
2735 code = CONJ_EXPR;
2736 if (pedantic)
2737 pedwarn ("ISO C does not support `~' for complex conjugation");
2738 if (!noconvert)
2739 arg = default_conversion (arg);
2741 else
2743 error ("wrong type argument to bit-complement");
2744 return error_mark_node;
2746 break;
2748 case ABS_EXPR:
2749 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2750 || typecode == COMPLEX_TYPE))
2752 error ("wrong type argument to abs");
2753 return error_mark_node;
2755 else if (!noconvert)
2756 arg = default_conversion (arg);
2757 break;
2759 case CONJ_EXPR:
2760 /* Conjugating a real value is a no-op, but allow it anyway. */
2761 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2762 || typecode == COMPLEX_TYPE))
2764 error ("wrong type argument to conjugation");
2765 return error_mark_node;
2767 else if (!noconvert)
2768 arg = default_conversion (arg);
2769 break;
2771 case TRUTH_NOT_EXPR:
2772 if (typecode != INTEGER_TYPE
2773 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2774 && typecode != COMPLEX_TYPE
2775 /* These will convert to a pointer. */
2776 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2778 error ("wrong type argument to unary exclamation mark");
2779 return error_mark_node;
2781 arg = c_common_truthvalue_conversion (arg);
2782 return invert_truthvalue (arg);
2784 case NOP_EXPR:
2785 break;
2787 case REALPART_EXPR:
2788 if (TREE_CODE (arg) == COMPLEX_CST)
2789 return TREE_REALPART (arg);
2790 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2791 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2792 else
2793 return arg;
2795 case IMAGPART_EXPR:
2796 if (TREE_CODE (arg) == COMPLEX_CST)
2797 return TREE_IMAGPART (arg);
2798 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2799 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2800 else
2801 return convert (TREE_TYPE (arg), integer_zero_node);
2803 case PREINCREMENT_EXPR:
2804 case POSTINCREMENT_EXPR:
2805 case PREDECREMENT_EXPR:
2806 case POSTDECREMENT_EXPR:
2807 /* Handle complex lvalues (when permitted)
2808 by reduction to simpler cases. */
2810 val = unary_complex_lvalue (code, arg, 0);
2811 if (val != 0)
2812 return val;
2814 /* Increment or decrement the real part of the value,
2815 and don't change the imaginary part. */
2816 if (typecode == COMPLEX_TYPE)
2818 tree real, imag;
2820 if (pedantic)
2821 pedwarn ("ISO C does not support `++' and `--' on complex types");
2823 arg = stabilize_reference (arg);
2824 real = build_unary_op (REALPART_EXPR, arg, 1);
2825 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2826 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2827 build_unary_op (code, real, 1), imag);
2830 /* Report invalid types. */
2832 if (typecode != POINTER_TYPE
2833 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2835 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2836 error ("wrong type argument to increment");
2837 else
2838 error ("wrong type argument to decrement");
2840 return error_mark_node;
2844 tree inc;
2845 tree result_type = TREE_TYPE (arg);
2847 arg = get_unwidened (arg, 0);
2848 argtype = TREE_TYPE (arg);
2850 /* Compute the increment. */
2852 if (typecode == POINTER_TYPE)
2854 /* If pointer target is an undefined struct,
2855 we just cannot know how to do the arithmetic. */
2856 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2858 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2859 error ("increment of pointer to unknown structure");
2860 else
2861 error ("decrement of pointer to unknown structure");
2863 else if ((pedantic || warn_pointer_arith)
2864 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2865 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2867 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2868 pedwarn ("wrong type argument to increment");
2869 else
2870 pedwarn ("wrong type argument to decrement");
2873 inc = c_size_in_bytes (TREE_TYPE (result_type));
2875 else
2876 inc = integer_one_node;
2878 inc = convert (argtype, inc);
2880 /* Handle incrementing a cast-expression. */
2882 while (1)
2883 switch (TREE_CODE (arg))
2885 case NOP_EXPR:
2886 case CONVERT_EXPR:
2887 case FLOAT_EXPR:
2888 case FIX_TRUNC_EXPR:
2889 case FIX_FLOOR_EXPR:
2890 case FIX_ROUND_EXPR:
2891 case FIX_CEIL_EXPR:
2892 pedantic_lvalue_warning (CONVERT_EXPR);
2893 /* If the real type has the same machine representation
2894 as the type it is cast to, we can make better output
2895 by adding directly to the inside of the cast. */
2896 if ((TREE_CODE (TREE_TYPE (arg))
2897 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2898 && (TYPE_MODE (TREE_TYPE (arg))
2899 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2900 arg = TREE_OPERAND (arg, 0);
2901 else
2903 tree incremented, modify, value;
2904 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2905 value = boolean_increment (code, arg);
2906 else
2908 arg = stabilize_reference (arg);
2909 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2910 value = arg;
2911 else
2912 value = save_expr (arg);
2913 incremented = build (((code == PREINCREMENT_EXPR
2914 || code == POSTINCREMENT_EXPR)
2915 ? PLUS_EXPR : MINUS_EXPR),
2916 argtype, value, inc);
2917 TREE_SIDE_EFFECTS (incremented) = 1;
2918 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2919 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2921 TREE_USED (value) = 1;
2922 return value;
2924 break;
2926 default:
2927 goto give_up;
2929 give_up:
2931 /* Complain about anything else that is not a true lvalue. */
2932 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2933 || code == POSTINCREMENT_EXPR)
2934 ? "invalid lvalue in increment"
2935 : "invalid lvalue in decrement")))
2936 return error_mark_node;
2938 /* Report a read-only lvalue. */
2939 if (TREE_READONLY (arg))
2940 readonly_warning (arg,
2941 ((code == PREINCREMENT_EXPR
2942 || code == POSTINCREMENT_EXPR)
2943 ? "increment" : "decrement"));
2945 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2946 val = boolean_increment (code, arg);
2947 else
2948 val = build (code, TREE_TYPE (arg), arg, inc);
2949 TREE_SIDE_EFFECTS (val) = 1;
2950 val = convert (result_type, val);
2951 if (TREE_CODE (val) != code)
2952 TREE_NO_UNUSED_WARNING (val) = 1;
2953 return val;
2956 case ADDR_EXPR:
2957 /* Note that this operation never does default_conversion. */
2959 /* Let &* cancel out to simplify resulting code. */
2960 if (TREE_CODE (arg) == INDIRECT_REF)
2962 /* Don't let this be an lvalue. */
2963 if (lvalue_p (TREE_OPERAND (arg, 0)))
2964 return non_lvalue (TREE_OPERAND (arg, 0));
2965 return TREE_OPERAND (arg, 0);
2968 /* For &x[y], return x+y */
2969 if (TREE_CODE (arg) == ARRAY_REF)
2971 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2972 return error_mark_node;
2973 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2974 TREE_OPERAND (arg, 1), 1);
2977 /* Handle complex lvalues (when permitted)
2978 by reduction to simpler cases. */
2979 val = unary_complex_lvalue (code, arg, flag);
2980 if (val != 0)
2981 return val;
2983 /* Anything not already handled and not a true memory reference
2984 or a non-lvalue array is an error. */
2985 else if (typecode != FUNCTION_TYPE && !flag
2986 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2987 return error_mark_node;
2989 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2990 argtype = TREE_TYPE (arg);
2992 /* If the lvalue is const or volatile, merge that into the type
2993 to which the address will point. Note that you can't get a
2994 restricted pointer by taking the address of something, so we
2995 only have to deal with `const' and `volatile' here. */
2996 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2997 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2998 argtype = c_build_type_variant (argtype,
2999 TREE_READONLY (arg),
3000 TREE_THIS_VOLATILE (arg));
3002 argtype = build_pointer_type (argtype);
3004 if (!c_mark_addressable (arg))
3005 return error_mark_node;
3008 tree addr;
3010 if (TREE_CODE (arg) == COMPONENT_REF)
3012 tree field = TREE_OPERAND (arg, 1);
3014 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3016 if (DECL_C_BIT_FIELD (field))
3018 error ("attempt to take address of bit-field structure member `%s'",
3019 IDENTIFIER_POINTER (DECL_NAME (field)));
3020 return error_mark_node;
3023 addr = fold (build (PLUS_EXPR, argtype,
3024 convert (argtype, addr),
3025 convert (argtype, byte_position (field))));
3027 else
3028 addr = build1 (code, argtype, arg);
3030 /* Address of a static or external variable or
3031 file-scope function counts as a constant. */
3032 if (staticp (arg)
3033 && ! (TREE_CODE (arg) == FUNCTION_DECL
3034 && DECL_CONTEXT (arg) != 0))
3035 TREE_CONSTANT (addr) = 1;
3036 return addr;
3039 default:
3040 break;
3043 if (argtype == 0)
3044 argtype = TREE_TYPE (arg);
3045 return fold (build1 (code, argtype, arg));
3048 /* Return nonzero if REF is an lvalue valid for this language.
3049 Lvalues can be assigned, unless their type has TYPE_READONLY.
3050 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3053 lvalue_p (tree ref)
3055 enum tree_code code = TREE_CODE (ref);
3057 switch (code)
3059 case REALPART_EXPR:
3060 case IMAGPART_EXPR:
3061 case COMPONENT_REF:
3062 return lvalue_p (TREE_OPERAND (ref, 0));
3064 case COMPOUND_LITERAL_EXPR:
3065 case STRING_CST:
3066 return 1;
3068 case INDIRECT_REF:
3069 case ARRAY_REF:
3070 case VAR_DECL:
3071 case PARM_DECL:
3072 case RESULT_DECL:
3073 case ERROR_MARK:
3074 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3075 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3077 case BIND_EXPR:
3078 case RTL_EXPR:
3079 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3081 default:
3082 return 0;
3086 /* Return nonzero if REF is an lvalue valid for this language;
3087 otherwise, print an error message and return zero. */
3090 lvalue_or_else (tree ref, const char *msgid)
3092 int win = lvalue_p (ref);
3094 if (! win)
3095 error ("%s", msgid);
3097 return win;
3100 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3101 for certain kinds of expressions which are not really lvalues
3102 but which we can accept as lvalues. If FLAG is nonzero, then
3103 non-lvalues are OK since we may be converting a non-lvalue array to
3104 a pointer in C99.
3106 If ARG is not a kind of expression we can handle, return zero. */
3108 static tree
3109 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
3111 /* Handle (a, b) used as an "lvalue". */
3112 if (TREE_CODE (arg) == COMPOUND_EXPR)
3114 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3116 /* If this returns a function type, it isn't really being used as
3117 an lvalue, so don't issue a warning about it. */
3118 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3119 pedantic_lvalue_warning (COMPOUND_EXPR);
3121 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3122 TREE_OPERAND (arg, 0), real_result);
3125 /* Handle (a ? b : c) used as an "lvalue". */
3126 if (TREE_CODE (arg) == COND_EXPR)
3128 if (!flag)
3129 pedantic_lvalue_warning (COND_EXPR);
3130 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3131 pedantic_lvalue_warning (COMPOUND_EXPR);
3133 return (build_conditional_expr
3134 (TREE_OPERAND (arg, 0),
3135 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3136 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3139 return 0;
3142 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3143 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3145 static void
3146 pedantic_lvalue_warning (enum tree_code code)
3148 if (pedantic)
3149 switch (code)
3151 case COND_EXPR:
3152 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3153 break;
3154 case COMPOUND_EXPR:
3155 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3156 break;
3157 default:
3158 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3159 break;
3163 /* Warn about storing in something that is `const'. */
3165 void
3166 readonly_warning (tree arg, const char *msgid)
3168 if (TREE_CODE (arg) == COMPONENT_REF)
3170 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3171 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3172 else
3173 pedwarn ("%s of read-only member `%s'", _(msgid),
3174 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3176 else if (TREE_CODE (arg) == VAR_DECL)
3177 pedwarn ("%s of read-only variable `%s'", _(msgid),
3178 IDENTIFIER_POINTER (DECL_NAME (arg)));
3179 else
3180 pedwarn ("%s of read-only location", _(msgid));
3183 /* Mark EXP saying that we need to be able to take the
3184 address of it; it should not be allocated in a register.
3185 Returns true if successful. */
3187 bool
3188 c_mark_addressable (tree exp)
3190 tree x = exp;
3192 while (1)
3193 switch (TREE_CODE (x))
3195 case COMPONENT_REF:
3196 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3198 error ("cannot take address of bit-field `%s'",
3199 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3200 return false;
3203 /* ... fall through ... */
3205 case ADDR_EXPR:
3206 case ARRAY_REF:
3207 case REALPART_EXPR:
3208 case IMAGPART_EXPR:
3209 x = TREE_OPERAND (x, 0);
3210 break;
3212 case COMPOUND_LITERAL_EXPR:
3213 case CONSTRUCTOR:
3214 TREE_ADDRESSABLE (x) = 1;
3215 return true;
3217 case VAR_DECL:
3218 case CONST_DECL:
3219 case PARM_DECL:
3220 case RESULT_DECL:
3221 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3222 && DECL_NONLOCAL (x))
3224 if (TREE_PUBLIC (x))
3226 error ("global register variable `%s' used in nested function",
3227 IDENTIFIER_POINTER (DECL_NAME (x)));
3228 return false;
3230 pedwarn ("register variable `%s' used in nested function",
3231 IDENTIFIER_POINTER (DECL_NAME (x)));
3233 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3235 if (TREE_PUBLIC (x))
3237 error ("address of global register variable `%s' requested",
3238 IDENTIFIER_POINTER (DECL_NAME (x)));
3239 return false;
3242 /* If we are making this addressable due to its having
3243 volatile components, give a different error message. Also
3244 handle the case of an unnamed parameter by not trying
3245 to give the name. */
3247 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3249 error ("cannot put object with volatile field into register");
3250 return false;
3253 pedwarn ("address of register variable `%s' requested",
3254 IDENTIFIER_POINTER (DECL_NAME (x)));
3256 put_var_into_stack (x, /*rescan=*/true);
3258 /* drops in */
3259 case FUNCTION_DECL:
3260 TREE_ADDRESSABLE (x) = 1;
3261 default:
3262 return true;
3266 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3268 tree
3269 build_conditional_expr (tree ifexp, tree op1, tree op2)
3271 tree type1;
3272 tree type2;
3273 enum tree_code code1;
3274 enum tree_code code2;
3275 tree result_type = NULL;
3276 tree orig_op1 = op1, orig_op2 = op2;
3278 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3280 /* Promote both alternatives. */
3282 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3283 op1 = default_conversion (op1);
3284 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3285 op2 = default_conversion (op2);
3287 if (TREE_CODE (ifexp) == ERROR_MARK
3288 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3289 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3290 return error_mark_node;
3292 type1 = TREE_TYPE (op1);
3293 code1 = TREE_CODE (type1);
3294 type2 = TREE_TYPE (op2);
3295 code2 = TREE_CODE (type2);
3297 /* Quickly detect the usual case where op1 and op2 have the same type
3298 after promotion. */
3299 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3301 if (type1 == type2)
3302 result_type = type1;
3303 else
3304 result_type = TYPE_MAIN_VARIANT (type1);
3306 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3307 || code1 == COMPLEX_TYPE)
3308 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3309 || code2 == COMPLEX_TYPE))
3311 result_type = common_type (type1, type2);
3313 /* If -Wsign-compare, warn here if type1 and type2 have
3314 different signedness. We'll promote the signed to unsigned
3315 and later code won't know it used to be different.
3316 Do this check on the original types, so that explicit casts
3317 will be considered, but default promotions won't. */
3318 if (warn_sign_compare && !skip_evaluation)
3320 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3321 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3323 if (unsigned_op1 ^ unsigned_op2)
3325 /* Do not warn if the result type is signed, since the
3326 signed type will only be chosen if it can represent
3327 all the values of the unsigned type. */
3328 if (! TREE_UNSIGNED (result_type))
3329 /* OK */;
3330 /* Do not warn if the signed quantity is an unsuffixed
3331 integer literal (or some static constant expression
3332 involving such literals) and it is non-negative. */
3333 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3334 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3335 /* OK */;
3336 else
3337 warning ("signed and unsigned type in conditional expression");
3341 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3343 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3344 pedwarn ("ISO C forbids conditional expr with only one void side");
3345 result_type = void_type_node;
3347 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3349 if (comp_target_types (type1, type2, 1))
3350 result_type = common_type (type1, type2);
3351 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3352 && TREE_CODE (orig_op1) != NOP_EXPR)
3353 result_type = qualify_type (type2, type1);
3354 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3355 && TREE_CODE (orig_op2) != NOP_EXPR)
3356 result_type = qualify_type (type1, type2);
3357 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3359 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3360 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3361 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3362 TREE_TYPE (type2)));
3364 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3366 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3367 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3368 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3369 TREE_TYPE (type1)));
3371 else
3373 pedwarn ("pointer type mismatch in conditional expression");
3374 result_type = build_pointer_type (void_type_node);
3377 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3379 if (! integer_zerop (op2))
3380 pedwarn ("pointer/integer type mismatch in conditional expression");
3381 else
3383 op2 = null_pointer_node;
3385 result_type = type1;
3387 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3389 if (!integer_zerop (op1))
3390 pedwarn ("pointer/integer type mismatch in conditional expression");
3391 else
3393 op1 = null_pointer_node;
3395 result_type = type2;
3398 if (!result_type)
3400 if (flag_cond_mismatch)
3401 result_type = void_type_node;
3402 else
3404 error ("type mismatch in conditional expression");
3405 return error_mark_node;
3409 /* Merge const and volatile flags of the incoming types. */
3410 result_type
3411 = build_type_variant (result_type,
3412 TREE_READONLY (op1) || TREE_READONLY (op2),
3413 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3415 if (result_type != TREE_TYPE (op1))
3416 op1 = convert_and_check (result_type, op1);
3417 if (result_type != TREE_TYPE (op2))
3418 op2 = convert_and_check (result_type, op2);
3420 if (TREE_CODE (ifexp) == INTEGER_CST)
3421 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3423 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3426 /* Given a list of expressions, return a compound expression
3427 that performs them all and returns the value of the last of them. */
3429 tree
3430 build_compound_expr (tree list)
3432 return internal_build_compound_expr (list, TRUE);
3435 static tree
3436 internal_build_compound_expr (tree list, int first_p)
3438 tree rest;
3440 if (TREE_CHAIN (list) == 0)
3442 /* Convert arrays and functions to pointers when there
3443 really is a comma operator. */
3444 if (!first_p)
3445 TREE_VALUE (list)
3446 = default_function_array_conversion (TREE_VALUE (list));
3448 /* Don't let (0, 0) be null pointer constant. */
3449 if (!first_p && integer_zerop (TREE_VALUE (list)))
3450 return non_lvalue (TREE_VALUE (list));
3451 return TREE_VALUE (list);
3454 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3456 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3458 /* The left-hand operand of a comma expression is like an expression
3459 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3460 any side-effects, unless it was explicitly cast to (void). */
3461 if (warn_unused_value
3462 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3463 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3464 warning ("left-hand operand of comma expression has no effect");
3466 /* When pedantic, a compound expression can be neither an lvalue
3467 nor an integer constant expression. */
3468 if (! pedantic)
3469 return rest;
3472 /* With -Wunused, we should also warn if the left-hand operand does have
3473 side-effects, but computes a value which is not used. For example, in
3474 `foo() + bar(), baz()' the result of the `+' operator is not used,
3475 so we should issue a warning. */
3476 else if (warn_unused_value)
3477 warn_if_unused_value (TREE_VALUE (list));
3479 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3482 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3484 tree
3485 build_c_cast (tree type, tree expr)
3487 tree value = expr;
3489 if (type == error_mark_node || expr == error_mark_node)
3490 return error_mark_node;
3492 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3493 only in <protocol> qualifications. But when constructing cast expressions,
3494 the protocols do matter and must be kept around. */
3495 if (!c_dialect_objc () || !objc_is_id (type))
3496 type = TYPE_MAIN_VARIANT (type);
3498 if (TREE_CODE (type) == ARRAY_TYPE)
3500 error ("cast specifies array type");
3501 return error_mark_node;
3504 if (TREE_CODE (type) == FUNCTION_TYPE)
3506 error ("cast specifies function type");
3507 return error_mark_node;
3510 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3512 if (pedantic)
3514 if (TREE_CODE (type) == RECORD_TYPE
3515 || TREE_CODE (type) == UNION_TYPE)
3516 pedwarn ("ISO C forbids casting nonscalar to the same type");
3519 else if (TREE_CODE (type) == UNION_TYPE)
3521 tree field;
3522 value = default_function_array_conversion (value);
3524 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3525 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3526 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3527 break;
3529 if (field)
3531 tree t;
3533 if (pedantic)
3534 pedwarn ("ISO C forbids casts to union type");
3535 t = digest_init (type,
3536 build_constructor (type,
3537 build_tree_list (field, value)),
3539 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3540 return t;
3542 error ("cast to union type from type not present in union");
3543 return error_mark_node;
3545 else
3547 tree otype, ovalue;
3549 /* If casting to void, avoid the error that would come
3550 from default_conversion in the case of a non-lvalue array. */
3551 if (type == void_type_node)
3552 return build1 (CONVERT_EXPR, type, value);
3554 /* Convert functions and arrays to pointers,
3555 but don't convert any other types. */
3556 value = default_function_array_conversion (value);
3557 otype = TREE_TYPE (value);
3559 /* Optionally warn about potentially worrisome casts. */
3561 if (warn_cast_qual
3562 && TREE_CODE (type) == POINTER_TYPE
3563 && TREE_CODE (otype) == POINTER_TYPE)
3565 tree in_type = type;
3566 tree in_otype = otype;
3567 int added = 0;
3568 int discarded = 0;
3570 /* Check that the qualifiers on IN_TYPE are a superset of
3571 the qualifiers of IN_OTYPE. The outermost level of
3572 POINTER_TYPE nodes is uninteresting and we stop as soon
3573 as we hit a non-POINTER_TYPE node on either type. */
3576 in_otype = TREE_TYPE (in_otype);
3577 in_type = TREE_TYPE (in_type);
3579 /* GNU C allows cv-qualified function types. 'const'
3580 means the function is very pure, 'volatile' means it
3581 can't return. We need to warn when such qualifiers
3582 are added, not when they're taken away. */
3583 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3584 && TREE_CODE (in_type) == FUNCTION_TYPE)
3585 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3586 else
3587 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3589 while (TREE_CODE (in_type) == POINTER_TYPE
3590 && TREE_CODE (in_otype) == POINTER_TYPE);
3592 if (added)
3593 warning ("cast adds new qualifiers to function type");
3595 if (discarded)
3596 /* There are qualifiers present in IN_OTYPE that are not
3597 present in IN_TYPE. */
3598 warning ("cast discards qualifiers from pointer target type");
3601 /* Warn about possible alignment problems. */
3602 if (STRICT_ALIGNMENT && warn_cast_align
3603 && TREE_CODE (type) == POINTER_TYPE
3604 && TREE_CODE (otype) == POINTER_TYPE
3605 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3606 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3607 /* Don't warn about opaque types, where the actual alignment
3608 restriction is unknown. */
3609 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3610 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3611 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3612 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3613 warning ("cast increases required alignment of target type");
3615 if (TREE_CODE (type) == INTEGER_TYPE
3616 && TREE_CODE (otype) == POINTER_TYPE
3617 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3618 && !TREE_CONSTANT (value))
3619 warning ("cast from pointer to integer of different size");
3621 if (warn_bad_function_cast
3622 && TREE_CODE (value) == CALL_EXPR
3623 && TREE_CODE (type) != TREE_CODE (otype))
3624 warning ("cast does not match function type");
3626 if (TREE_CODE (type) == POINTER_TYPE
3627 && TREE_CODE (otype) == INTEGER_TYPE
3628 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3629 /* Don't warn about converting any constant. */
3630 && !TREE_CONSTANT (value))
3631 warning ("cast to pointer from integer of different size");
3633 if (TREE_CODE (type) == POINTER_TYPE
3634 && TREE_CODE (otype) == POINTER_TYPE
3635 && TREE_CODE (expr) == ADDR_EXPR
3636 && DECL_P (TREE_OPERAND (expr, 0))
3637 && flag_strict_aliasing && warn_strict_aliasing
3638 && !VOID_TYPE_P (TREE_TYPE (type)))
3640 /* Casting the address of a decl to non void pointer. Warn
3641 if the cast breaks type based aliasing. */
3642 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3643 warning ("type-punning to incomplete type might break strict-aliasing rules");
3644 else if (!alias_sets_conflict_p
3645 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3646 get_alias_set (TREE_TYPE (type))))
3647 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3650 ovalue = value;
3651 /* Replace a nonvolatile const static variable with its value. */
3652 if (optimize && TREE_CODE (value) == VAR_DECL)
3653 value = decl_constant_value (value);
3654 value = convert (type, value);
3656 /* Ignore any integer overflow caused by the cast. */
3657 if (TREE_CODE (value) == INTEGER_CST)
3659 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3660 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3664 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3665 if (pedantic && TREE_CODE (value) == INTEGER_CST
3666 && TREE_CODE (expr) == INTEGER_CST
3667 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3668 value = non_lvalue (value);
3670 /* If pedantic, don't let a cast be an lvalue. */
3671 if (value == expr && pedantic)
3672 value = non_lvalue (value);
3674 return value;
3677 /* Interpret a cast of expression EXPR to type TYPE. */
3678 tree
3679 c_cast_expr (tree type, tree expr)
3681 int saved_wsp = warn_strict_prototypes;
3683 /* This avoids warnings about unprototyped casts on
3684 integers. E.g. "#define SIG_DFL (void(*)())0". */
3685 if (TREE_CODE (expr) == INTEGER_CST)
3686 warn_strict_prototypes = 0;
3687 type = groktypename (type);
3688 warn_strict_prototypes = saved_wsp;
3690 return build_c_cast (type, expr);
3694 /* Build an assignment expression of lvalue LHS from value RHS.
3695 MODIFYCODE is the code for a binary operator that we use
3696 to combine the old value of LHS with RHS to get the new value.
3697 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3699 tree
3700 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3702 tree result;
3703 tree newrhs;
3704 tree lhstype = TREE_TYPE (lhs);
3705 tree olhstype = lhstype;
3707 /* Types that aren't fully specified cannot be used in assignments. */
3708 lhs = require_complete_type (lhs);
3710 /* Avoid duplicate error messages from operands that had errors. */
3711 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3712 return error_mark_node;
3714 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3715 /* Do not use STRIP_NOPS here. We do not want an enumerator
3716 whose value is 0 to count as a null pointer constant. */
3717 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3718 rhs = TREE_OPERAND (rhs, 0);
3720 newrhs = rhs;
3722 /* Handle control structure constructs used as "lvalues". */
3724 switch (TREE_CODE (lhs))
3726 /* Handle (a, b) used as an "lvalue". */
3727 case COMPOUND_EXPR:
3728 pedantic_lvalue_warning (COMPOUND_EXPR);
3729 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3730 if (TREE_CODE (newrhs) == ERROR_MARK)
3731 return error_mark_node;
3732 return build (COMPOUND_EXPR, lhstype,
3733 TREE_OPERAND (lhs, 0), newrhs);
3735 /* Handle (a ? b : c) used as an "lvalue". */
3736 case COND_EXPR:
3737 pedantic_lvalue_warning (COND_EXPR);
3738 rhs = save_expr (rhs);
3740 /* Produce (a ? (b = rhs) : (c = rhs))
3741 except that the RHS goes through a save-expr
3742 so the code to compute it is only emitted once. */
3743 tree cond
3744 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3745 build_modify_expr (TREE_OPERAND (lhs, 1),
3746 modifycode, rhs),
3747 build_modify_expr (TREE_OPERAND (lhs, 2),
3748 modifycode, rhs));
3749 if (TREE_CODE (cond) == ERROR_MARK)
3750 return cond;
3751 /* Make sure the code to compute the rhs comes out
3752 before the split. */
3753 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3754 /* But cast it to void to avoid an "unused" error. */
3755 convert (void_type_node, rhs), cond);
3757 default:
3758 break;
3761 /* If a binary op has been requested, combine the old LHS value with the RHS
3762 producing the value we should actually store into the LHS. */
3764 if (modifycode != NOP_EXPR)
3766 lhs = stabilize_reference (lhs);
3767 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3770 /* Handle a cast used as an "lvalue".
3771 We have already performed any binary operator using the value as cast.
3772 Now convert the result to the cast type of the lhs,
3773 and then true type of the lhs and store it there;
3774 then convert result back to the cast type to be the value
3775 of the assignment. */
3777 switch (TREE_CODE (lhs))
3779 case NOP_EXPR:
3780 case CONVERT_EXPR:
3781 case FLOAT_EXPR:
3782 case FIX_TRUNC_EXPR:
3783 case FIX_FLOOR_EXPR:
3784 case FIX_ROUND_EXPR:
3785 case FIX_CEIL_EXPR:
3786 newrhs = default_function_array_conversion (newrhs);
3788 tree inner_lhs = TREE_OPERAND (lhs, 0);
3789 tree result;
3790 result = build_modify_expr (inner_lhs, NOP_EXPR,
3791 convert (TREE_TYPE (inner_lhs),
3792 convert (lhstype, newrhs)));
3793 if (TREE_CODE (result) == ERROR_MARK)
3794 return result;
3795 pedantic_lvalue_warning (CONVERT_EXPR);
3796 return convert (TREE_TYPE (lhs), result);
3799 default:
3800 break;
3803 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3804 Reject anything strange now. */
3806 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3807 return error_mark_node;
3809 /* Warn about storing in something that is `const'. */
3811 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3812 || ((TREE_CODE (lhstype) == RECORD_TYPE
3813 || TREE_CODE (lhstype) == UNION_TYPE)
3814 && C_TYPE_FIELDS_READONLY (lhstype)))
3815 readonly_warning (lhs, "assignment");
3817 /* If storing into a structure or union member,
3818 it has probably been given type `int'.
3819 Compute the type that would go with
3820 the actual amount of storage the member occupies. */
3822 if (TREE_CODE (lhs) == COMPONENT_REF
3823 && (TREE_CODE (lhstype) == INTEGER_TYPE
3824 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3825 || TREE_CODE (lhstype) == REAL_TYPE
3826 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3827 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3829 /* If storing in a field that is in actuality a short or narrower than one,
3830 we must store in the field in its actual type. */
3832 if (lhstype != TREE_TYPE (lhs))
3834 lhs = copy_node (lhs);
3835 TREE_TYPE (lhs) = lhstype;
3838 /* Convert new value to destination type. */
3840 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3841 NULL_TREE, NULL_TREE, 0);
3842 if (TREE_CODE (newrhs) == ERROR_MARK)
3843 return error_mark_node;
3845 /* Scan operands */
3847 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3848 TREE_SIDE_EFFECTS (result) = 1;
3850 /* If we got the LHS in a different type for storing in,
3851 convert the result back to the nominal type of LHS
3852 so that the value we return always has the same type
3853 as the LHS argument. */
3855 if (olhstype == TREE_TYPE (result))
3856 return result;
3857 return convert_for_assignment (olhstype, result, _("assignment"),
3858 NULL_TREE, NULL_TREE, 0);
3861 /* Convert value RHS to type TYPE as preparation for an assignment
3862 to an lvalue of type TYPE.
3863 The real work of conversion is done by `convert'.
3864 The purpose of this function is to generate error messages
3865 for assignments that are not allowed in C.
3866 ERRTYPE is a string to use in error messages:
3867 "assignment", "return", etc. If it is null, this is parameter passing
3868 for a function call (and different error messages are output).
3870 FUNNAME is the name of the function being called,
3871 as an IDENTIFIER_NODE, or null.
3872 PARMNUM is the number of the argument, for printing in error messages. */
3874 static tree
3875 convert_for_assignment (tree type, tree rhs, const char *errtype,
3876 tree fundecl, tree funname, int parmnum)
3878 enum tree_code codel = TREE_CODE (type);
3879 tree rhstype;
3880 enum tree_code coder;
3882 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3883 /* Do not use STRIP_NOPS here. We do not want an enumerator
3884 whose value is 0 to count as a null pointer constant. */
3885 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3886 rhs = TREE_OPERAND (rhs, 0);
3888 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3889 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3890 rhs = default_conversion (rhs);
3891 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3892 rhs = decl_constant_value_for_broken_optimization (rhs);
3894 rhstype = TREE_TYPE (rhs);
3895 coder = TREE_CODE (rhstype);
3897 if (coder == ERROR_MARK)
3898 return error_mark_node;
3900 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3902 overflow_warning (rhs);
3903 /* Check for Objective-C protocols. This will automatically
3904 issue a warning if there are protocol violations. No need to
3905 use the return value. */
3906 if (c_dialect_objc ())
3907 objc_comptypes (type, rhstype, 0);
3908 return rhs;
3911 if (coder == VOID_TYPE)
3913 error ("void value not ignored as it ought to be");
3914 return error_mark_node;
3916 /* A type converts to a reference to it.
3917 This code doesn't fully support references, it's just for the
3918 special case of va_start and va_copy. */
3919 if (codel == REFERENCE_TYPE
3920 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3922 if (!lvalue_p (rhs))
3924 error ("cannot pass rvalue to reference parameter");
3925 return error_mark_node;
3927 if (!c_mark_addressable (rhs))
3928 return error_mark_node;
3929 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3931 /* We already know that these two types are compatible, but they
3932 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3933 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3934 likely to be va_list, a typedef to __builtin_va_list, which
3935 is different enough that it will cause problems later. */
3936 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3937 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3939 rhs = build1 (NOP_EXPR, type, rhs);
3940 return rhs;
3942 /* Some types can interconvert without explicit casts. */
3943 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3944 && ((*targetm.vector_opaque_p) (type)
3945 || (*targetm.vector_opaque_p) (rhstype)))
3946 return convert (type, rhs);
3947 /* Arithmetic types all interconvert, and enum is treated like int. */
3948 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3949 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3950 || codel == BOOLEAN_TYPE)
3951 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3952 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3953 || coder == BOOLEAN_TYPE))
3954 return convert_and_check (type, rhs);
3956 /* Conversion to a transparent union from its member types.
3957 This applies only to function arguments. */
3958 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3960 tree memb_types;
3961 tree marginal_memb_type = 0;
3963 for (memb_types = TYPE_FIELDS (type); memb_types;
3964 memb_types = TREE_CHAIN (memb_types))
3966 tree memb_type = TREE_TYPE (memb_types);
3968 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3969 TYPE_MAIN_VARIANT (rhstype)))
3970 break;
3972 if (TREE_CODE (memb_type) != POINTER_TYPE)
3973 continue;
3975 if (coder == POINTER_TYPE)
3977 tree ttl = TREE_TYPE (memb_type);
3978 tree ttr = TREE_TYPE (rhstype);
3980 /* Any non-function converts to a [const][volatile] void *
3981 and vice versa; otherwise, targets must be the same.
3982 Meanwhile, the lhs target must have all the qualifiers of
3983 the rhs. */
3984 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3985 || comp_target_types (memb_type, rhstype, 0))
3987 /* If this type won't generate any warnings, use it. */
3988 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3989 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3990 && TREE_CODE (ttl) == FUNCTION_TYPE)
3991 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3992 == TYPE_QUALS (ttr))
3993 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3994 == TYPE_QUALS (ttl))))
3995 break;
3997 /* Keep looking for a better type, but remember this one. */
3998 if (! marginal_memb_type)
3999 marginal_memb_type = memb_type;
4003 /* Can convert integer zero to any pointer type. */
4004 if (integer_zerop (rhs)
4005 || (TREE_CODE (rhs) == NOP_EXPR
4006 && integer_zerop (TREE_OPERAND (rhs, 0))))
4008 rhs = null_pointer_node;
4009 break;
4013 if (memb_types || marginal_memb_type)
4015 if (! memb_types)
4017 /* We have only a marginally acceptable member type;
4018 it needs a warning. */
4019 tree ttl = TREE_TYPE (marginal_memb_type);
4020 tree ttr = TREE_TYPE (rhstype);
4022 /* Const and volatile mean something different for function
4023 types, so the usual warnings are not appropriate. */
4024 if (TREE_CODE (ttr) == FUNCTION_TYPE
4025 && TREE_CODE (ttl) == FUNCTION_TYPE)
4027 /* Because const and volatile on functions are
4028 restrictions that say the function will not do
4029 certain things, it is okay to use a const or volatile
4030 function where an ordinary one is wanted, but not
4031 vice-versa. */
4032 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4033 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4034 errtype, funname, parmnum);
4036 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4037 warn_for_assignment ("%s discards qualifiers from pointer target type",
4038 errtype, funname,
4039 parmnum);
4042 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4043 pedwarn ("ISO C prohibits argument conversion to union type");
4045 return build1 (NOP_EXPR, type, rhs);
4049 /* Conversions among pointers */
4050 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4051 && (coder == codel))
4053 tree ttl = TREE_TYPE (type);
4054 tree ttr = TREE_TYPE (rhstype);
4055 bool is_opaque_pointer;
4057 /* Opaque pointers are treated like void pointers. */
4058 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
4059 || (*targetm.vector_opaque_p) (rhstype))
4060 && TREE_CODE (ttl) == VECTOR_TYPE
4061 && TREE_CODE (ttr) == VECTOR_TYPE;
4063 /* Any non-function converts to a [const][volatile] void *
4064 and vice versa; otherwise, targets must be the same.
4065 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4066 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4067 || comp_target_types (type, rhstype, 0)
4068 || is_opaque_pointer
4069 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4070 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4072 if (pedantic
4073 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4075 (VOID_TYPE_P (ttr)
4076 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4077 which are not ANSI null ptr constants. */
4078 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4079 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4080 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4081 errtype, funname, parmnum);
4082 /* Const and volatile mean something different for function types,
4083 so the usual warnings are not appropriate. */
4084 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4085 && TREE_CODE (ttl) != FUNCTION_TYPE)
4087 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4088 warn_for_assignment ("%s discards qualifiers from pointer target type",
4089 errtype, funname, parmnum);
4090 /* If this is not a case of ignoring a mismatch in signedness,
4091 no warning. */
4092 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4093 || comp_target_types (type, rhstype, 0))
4095 /* If there is a mismatch, do warn. */
4096 else if (pedantic)
4097 warn_for_assignment ("pointer targets in %s differ in signedness",
4098 errtype, funname, parmnum);
4100 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4101 && TREE_CODE (ttr) == FUNCTION_TYPE)
4103 /* Because const and volatile on functions are restrictions
4104 that say the function will not do certain things,
4105 it is okay to use a const or volatile function
4106 where an ordinary one is wanted, but not vice-versa. */
4107 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4108 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4109 errtype, funname, parmnum);
4112 else
4113 warn_for_assignment ("%s from incompatible pointer type",
4114 errtype, funname, parmnum);
4115 return convert (type, rhs);
4117 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4119 /* An explicit constant 0 can convert to a pointer,
4120 or one that results from arithmetic, even including
4121 a cast to integer type. */
4122 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4124 ! (TREE_CODE (rhs) == NOP_EXPR
4125 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4126 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4127 && integer_zerop (TREE_OPERAND (rhs, 0))))
4129 warn_for_assignment ("%s makes pointer from integer without a cast",
4130 errtype, funname, parmnum);
4131 return convert (type, rhs);
4133 return null_pointer_node;
4135 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4137 warn_for_assignment ("%s makes integer from pointer without a cast",
4138 errtype, funname, parmnum);
4139 return convert (type, rhs);
4141 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4142 return convert (type, rhs);
4144 if (!errtype)
4146 if (funname)
4148 tree selector = objc_message_selector ();
4150 if (selector && parmnum > 2)
4151 error ("incompatible type for argument %d of `%s'",
4152 parmnum - 2, IDENTIFIER_POINTER (selector));
4153 else
4154 error ("incompatible type for argument %d of `%s'",
4155 parmnum, IDENTIFIER_POINTER (funname));
4157 else
4158 error ("incompatible type for argument %d of indirect function call",
4159 parmnum);
4161 else
4162 error ("incompatible types in %s", errtype);
4164 return error_mark_node;
4167 /* Convert VALUE for assignment into inlined parameter PARM. */
4169 tree
4170 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
4172 tree ret, type;
4174 /* If FN was prototyped, the value has been converted already
4175 in convert_arguments. */
4176 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4177 return value;
4179 type = TREE_TYPE (parm);
4180 ret = convert_for_assignment (type, value,
4181 (char *) 0 /* arg passing */, fn,
4182 DECL_NAME (fn), 0);
4183 if (PROMOTE_PROTOTYPES
4184 && INTEGRAL_TYPE_P (type)
4185 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4186 ret = default_conversion (ret);
4187 return ret;
4190 /* Print a warning using MSGID.
4191 It gets OPNAME as its one parameter.
4192 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4193 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4194 FUNCTION and ARGNUM are handled specially if we are building an
4195 Objective-C selector. */
4197 static void
4198 warn_for_assignment (const char *msgid, const char *opname, tree function,
4199 int argnum)
4201 if (opname == 0)
4203 tree selector = objc_message_selector ();
4204 char * new_opname;
4206 if (selector && argnum > 2)
4208 function = selector;
4209 argnum -= 2;
4211 if (argnum == 0)
4213 if (function)
4215 /* Function name is known; supply it. */
4216 const char *const argstring = _("passing arg of `%s'");
4217 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4218 + strlen (argstring) + 1
4219 + 1);
4220 sprintf (new_opname, argstring,
4221 IDENTIFIER_POINTER (function));
4223 else
4225 /* Function name unknown (call through ptr). */
4226 const char *const argnofun = _("passing arg of pointer to function");
4227 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4228 sprintf (new_opname, argnofun);
4231 else if (function)
4233 /* Function name is known; supply it. */
4234 const char *const argstring = _("passing arg %d of `%s'");
4235 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4236 + strlen (argstring) + 1 + 25
4237 /*%d*/ + 1);
4238 sprintf (new_opname, argstring, argnum,
4239 IDENTIFIER_POINTER (function));
4241 else
4243 /* Function name unknown (call through ptr); just give arg number. */
4244 const char *const argnofun = _("passing arg %d of pointer to function");
4245 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4246 sprintf (new_opname, argnofun, argnum);
4248 opname = new_opname;
4250 pedwarn (msgid, opname);
4253 /* If VALUE is a compound expr all of whose expressions are constant, then
4254 return its value. Otherwise, return error_mark_node.
4256 This is for handling COMPOUND_EXPRs as initializer elements
4257 which is allowed with a warning when -pedantic is specified. */
4259 static tree
4260 valid_compound_expr_initializer (tree value, tree endtype)
4262 if (TREE_CODE (value) == COMPOUND_EXPR)
4264 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4265 == error_mark_node)
4266 return error_mark_node;
4267 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4268 endtype);
4270 else if (! TREE_CONSTANT (value)
4271 && ! initializer_constant_valid_p (value, endtype))
4272 return error_mark_node;
4273 else
4274 return value;
4277 /* Perform appropriate conversions on the initial value of a variable,
4278 store it in the declaration DECL,
4279 and print any error messages that are appropriate.
4280 If the init is invalid, store an ERROR_MARK. */
4282 void
4283 store_init_value (tree decl, tree init)
4285 tree value, type;
4287 /* If variable's type was invalidly declared, just ignore it. */
4289 type = TREE_TYPE (decl);
4290 if (TREE_CODE (type) == ERROR_MARK)
4291 return;
4293 /* Digest the specified initializer into an expression. */
4295 value = digest_init (type, init, TREE_STATIC (decl));
4297 /* Store the expression if valid; else report error. */
4299 if (warn_traditional && !in_system_header
4300 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4301 warning ("traditional C rejects automatic aggregate initialization");
4303 DECL_INITIAL (decl) = value;
4305 /* ANSI wants warnings about out-of-range constant initializers. */
4306 STRIP_TYPE_NOPS (value);
4307 constant_expression_warning (value);
4309 /* Check if we need to set array size from compound literal size. */
4310 if (TREE_CODE (type) == ARRAY_TYPE
4311 && TYPE_DOMAIN (type) == 0
4312 && value != error_mark_node)
4314 tree inside_init = init;
4316 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4317 inside_init = TREE_OPERAND (init, 0);
4318 inside_init = fold (inside_init);
4320 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4322 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4324 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4326 /* For int foo[] = (int [3]){1}; we need to set array size
4327 now since later on array initializer will be just the
4328 brace enclosed list of the compound literal. */
4329 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4330 layout_type (type);
4331 layout_decl (decl, 0);
4337 /* Methods for storing and printing names for error messages. */
4339 /* Implement a spelling stack that allows components of a name to be pushed
4340 and popped. Each element on the stack is this structure. */
4342 struct spelling
4344 int kind;
4345 union
4347 int i;
4348 const char *s;
4349 } u;
4352 #define SPELLING_STRING 1
4353 #define SPELLING_MEMBER 2
4354 #define SPELLING_BOUNDS 3
4356 static struct spelling *spelling; /* Next stack element (unused). */
4357 static struct spelling *spelling_base; /* Spelling stack base. */
4358 static int spelling_size; /* Size of the spelling stack. */
4360 /* Macros to save and restore the spelling stack around push_... functions.
4361 Alternative to SAVE_SPELLING_STACK. */
4363 #define SPELLING_DEPTH() (spelling - spelling_base)
4364 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4366 /* Push an element on the spelling stack with type KIND and assign VALUE
4367 to MEMBER. */
4369 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4371 int depth = SPELLING_DEPTH (); \
4373 if (depth >= spelling_size) \
4375 spelling_size += 10; \
4376 if (spelling_base == 0) \
4377 spelling_base \
4378 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4379 else \
4380 spelling_base \
4381 = (struct spelling *) xrealloc (spelling_base, \
4382 spelling_size * sizeof (struct spelling)); \
4383 RESTORE_SPELLING_DEPTH (depth); \
4386 spelling->kind = (KIND); \
4387 spelling->MEMBER = (VALUE); \
4388 spelling++; \
4391 /* Push STRING on the stack. Printed literally. */
4393 static void
4394 push_string (const char *string)
4396 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4399 /* Push a member name on the stack. Printed as '.' STRING. */
4401 static void
4402 push_member_name (tree decl)
4404 const char *const string
4405 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4406 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4409 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4411 static void
4412 push_array_bounds (int bounds)
4414 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4417 /* Compute the maximum size in bytes of the printed spelling. */
4419 static int
4420 spelling_length (void)
4422 int size = 0;
4423 struct spelling *p;
4425 for (p = spelling_base; p < spelling; p++)
4427 if (p->kind == SPELLING_BOUNDS)
4428 size += 25;
4429 else
4430 size += strlen (p->u.s) + 1;
4433 return size;
4436 /* Print the spelling to BUFFER and return it. */
4438 static char *
4439 print_spelling (char *buffer)
4441 char *d = buffer;
4442 struct spelling *p;
4444 for (p = spelling_base; p < spelling; p++)
4445 if (p->kind == SPELLING_BOUNDS)
4447 sprintf (d, "[%d]", p->u.i);
4448 d += strlen (d);
4450 else
4452 const char *s;
4453 if (p->kind == SPELLING_MEMBER)
4454 *d++ = '.';
4455 for (s = p->u.s; (*d = *s++); d++)
4458 *d++ = '\0';
4459 return buffer;
4462 /* Issue an error message for a bad initializer component.
4463 MSGID identifies the message.
4464 The component name is taken from the spelling stack. */
4466 void
4467 error_init (const char *msgid)
4469 char *ofwhat;
4471 error ("%s", _(msgid));
4472 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4473 if (*ofwhat)
4474 error ("(near initialization for `%s')", ofwhat);
4477 /* Issue a pedantic warning for a bad initializer component.
4478 MSGID identifies the message.
4479 The component name is taken from the spelling stack. */
4481 void
4482 pedwarn_init (const char *msgid)
4484 char *ofwhat;
4486 pedwarn ("%s", _(msgid));
4487 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4488 if (*ofwhat)
4489 pedwarn ("(near initialization for `%s')", ofwhat);
4492 /* Issue a warning for a bad initializer component.
4493 MSGID identifies the message.
4494 The component name is taken from the spelling stack. */
4496 static void
4497 warning_init (const char *msgid)
4499 char *ofwhat;
4501 warning ("%s", _(msgid));
4502 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4503 if (*ofwhat)
4504 warning ("(near initialization for `%s')", ofwhat);
4507 /* Digest the parser output INIT as an initializer for type TYPE.
4508 Return a C expression of type TYPE to represent the initial value.
4510 REQUIRE_CONSTANT requests an error if non-constant initializers or
4511 elements are seen. */
4513 static tree
4514 digest_init (tree type, tree init, int require_constant)
4516 enum tree_code code = TREE_CODE (type);
4517 tree inside_init = init;
4519 if (type == error_mark_node
4520 || init == error_mark_node
4521 || TREE_TYPE (init) == error_mark_node)
4522 return error_mark_node;
4524 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4525 /* Do not use STRIP_NOPS here. We do not want an enumerator
4526 whose value is 0 to count as a null pointer constant. */
4527 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4528 inside_init = TREE_OPERAND (init, 0);
4530 inside_init = fold (inside_init);
4532 /* Initialization of an array of chars from a string constant
4533 optionally enclosed in braces. */
4535 if (code == ARRAY_TYPE)
4537 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4538 if ((typ1 == char_type_node
4539 || typ1 == signed_char_type_node
4540 || typ1 == unsigned_char_type_node
4541 || typ1 == unsigned_wchar_type_node
4542 || typ1 == signed_wchar_type_node)
4543 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4545 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4546 TYPE_MAIN_VARIANT (type)))
4547 return inside_init;
4549 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4550 != char_type_node)
4551 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4553 error_init ("char-array initialized from wide string");
4554 return error_mark_node;
4556 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4557 == char_type_node)
4558 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4560 error_init ("int-array initialized from non-wide string");
4561 return error_mark_node;
4564 TREE_TYPE (inside_init) = type;
4565 if (TYPE_DOMAIN (type) != 0
4566 && TYPE_SIZE (type) != 0
4567 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4568 /* Subtract 1 (or sizeof (wchar_t))
4569 because it's ok to ignore the terminating null char
4570 that is counted in the length of the constant. */
4571 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4572 TREE_STRING_LENGTH (inside_init)
4573 - ((TYPE_PRECISION (typ1)
4574 != TYPE_PRECISION (char_type_node))
4575 ? (TYPE_PRECISION (wchar_type_node)
4576 / BITS_PER_UNIT)
4577 : 1)))
4578 pedwarn_init ("initializer-string for array of chars is too long");
4580 return inside_init;
4584 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4585 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4586 below and handle as a constructor. */
4587 if (code == VECTOR_TYPE
4588 && comptypes (TREE_TYPE (inside_init), type)
4589 && TREE_CONSTANT (inside_init))
4591 if (TREE_CODE (inside_init) == VECTOR_CST
4592 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4593 TYPE_MAIN_VARIANT (type)))
4594 return inside_init;
4595 else
4596 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4599 /* Any type can be initialized
4600 from an expression of the same type, optionally with braces. */
4602 if (inside_init && TREE_TYPE (inside_init) != 0
4603 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4604 TYPE_MAIN_VARIANT (type))
4605 || (code == ARRAY_TYPE
4606 && comptypes (TREE_TYPE (inside_init), type))
4607 || (code == VECTOR_TYPE
4608 && comptypes (TREE_TYPE (inside_init), type))
4609 || (code == POINTER_TYPE
4610 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4611 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4612 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4613 TREE_TYPE (type)))))
4615 if (code == POINTER_TYPE)
4616 inside_init = default_function_array_conversion (inside_init);
4618 if (require_constant && !flag_isoc99
4619 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4621 /* As an extension, allow initializing objects with static storage
4622 duration with compound literals (which are then treated just as
4623 the brace enclosed list they contain). */
4624 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4625 inside_init = DECL_INITIAL (decl);
4628 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4629 && TREE_CODE (inside_init) != CONSTRUCTOR)
4631 error_init ("array initialized from non-constant array expression");
4632 return error_mark_node;
4635 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4636 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4638 /* Compound expressions can only occur here if -pedantic or
4639 -pedantic-errors is specified. In the later case, we always want
4640 an error. In the former case, we simply want a warning. */
4641 if (require_constant && pedantic
4642 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4644 inside_init
4645 = valid_compound_expr_initializer (inside_init,
4646 TREE_TYPE (inside_init));
4647 if (inside_init == error_mark_node)
4648 error_init ("initializer element is not constant");
4649 else
4650 pedwarn_init ("initializer element is not constant");
4651 if (flag_pedantic_errors)
4652 inside_init = error_mark_node;
4654 else if (require_constant
4655 && (!TREE_CONSTANT (inside_init)
4656 /* This test catches things like `7 / 0' which
4657 result in an expression for which TREE_CONSTANT
4658 is true, but which is not actually something
4659 that is a legal constant. We really should not
4660 be using this function, because it is a part of
4661 the back-end. Instead, the expression should
4662 already have been turned into ERROR_MARK_NODE. */
4663 || !initializer_constant_valid_p (inside_init,
4664 TREE_TYPE (inside_init))))
4666 error_init ("initializer element is not constant");
4667 inside_init = error_mark_node;
4670 return inside_init;
4673 /* Handle scalar types, including conversions. */
4675 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4676 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4678 /* Note that convert_for_assignment calls default_conversion
4679 for arrays and functions. We must not call it in the
4680 case where inside_init is a null pointer constant. */
4681 inside_init
4682 = convert_for_assignment (type, init, _("initialization"),
4683 NULL_TREE, NULL_TREE, 0);
4685 if (require_constant && ! TREE_CONSTANT (inside_init))
4687 error_init ("initializer element is not constant");
4688 inside_init = error_mark_node;
4690 else if (require_constant
4691 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4693 error_init ("initializer element is not computable at load time");
4694 inside_init = error_mark_node;
4697 return inside_init;
4700 /* Come here only for records and arrays. */
4702 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4704 error_init ("variable-sized object may not be initialized");
4705 return error_mark_node;
4708 error_init ("invalid initializer");
4709 return error_mark_node;
4712 /* Handle initializers that use braces. */
4714 /* Type of object we are accumulating a constructor for.
4715 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4716 static tree constructor_type;
4718 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4719 left to fill. */
4720 static tree constructor_fields;
4722 /* For an ARRAY_TYPE, this is the specified index
4723 at which to store the next element we get. */
4724 static tree constructor_index;
4726 /* For an ARRAY_TYPE, this is the maximum index. */
4727 static tree constructor_max_index;
4729 /* For a RECORD_TYPE, this is the first field not yet written out. */
4730 static tree constructor_unfilled_fields;
4732 /* For an ARRAY_TYPE, this is the index of the first element
4733 not yet written out. */
4734 static tree constructor_unfilled_index;
4736 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4737 This is so we can generate gaps between fields, when appropriate. */
4738 static tree constructor_bit_index;
4740 /* If we are saving up the elements rather than allocating them,
4741 this is the list of elements so far (in reverse order,
4742 most recent first). */
4743 static tree constructor_elements;
4745 /* 1 if constructor should be incrementally stored into a constructor chain,
4746 0 if all the elements should be kept in AVL tree. */
4747 static int constructor_incremental;
4749 /* 1 if so far this constructor's elements are all compile-time constants. */
4750 static int constructor_constant;
4752 /* 1 if so far this constructor's elements are all valid address constants. */
4753 static int constructor_simple;
4755 /* 1 if this constructor is erroneous so far. */
4756 static int constructor_erroneous;
4758 /* Structure for managing pending initializer elements, organized as an
4759 AVL tree. */
4761 struct init_node
4763 struct init_node *left, *right;
4764 struct init_node *parent;
4765 int balance;
4766 tree purpose;
4767 tree value;
4770 /* Tree of pending elements at this constructor level.
4771 These are elements encountered out of order
4772 which belong at places we haven't reached yet in actually
4773 writing the output.
4774 Will never hold tree nodes across GC runs. */
4775 static struct init_node *constructor_pending_elts;
4777 /* The SPELLING_DEPTH of this constructor. */
4778 static int constructor_depth;
4780 /* 0 if implicitly pushing constructor levels is allowed. */
4781 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4783 static int require_constant_value;
4784 static int require_constant_elements;
4786 /* DECL node for which an initializer is being read.
4787 0 means we are reading a constructor expression
4788 such as (struct foo) {...}. */
4789 static tree constructor_decl;
4791 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4792 static const char *constructor_asmspec;
4794 /* Nonzero if this is an initializer for a top-level decl. */
4795 static int constructor_top_level;
4797 /* Nonzero if there were any member designators in this initializer. */
4798 static int constructor_designated;
4800 /* Nesting depth of designator list. */
4801 static int designator_depth;
4803 /* Nonzero if there were diagnosed errors in this designator list. */
4804 static int designator_errorneous;
4807 /* This stack has a level for each implicit or explicit level of
4808 structuring in the initializer, including the outermost one. It
4809 saves the values of most of the variables above. */
4811 struct constructor_range_stack;
4813 struct constructor_stack
4815 struct constructor_stack *next;
4816 tree type;
4817 tree fields;
4818 tree index;
4819 tree max_index;
4820 tree unfilled_index;
4821 tree unfilled_fields;
4822 tree bit_index;
4823 tree elements;
4824 struct init_node *pending_elts;
4825 int offset;
4826 int depth;
4827 /* If nonzero, this value should replace the entire
4828 constructor at this level. */
4829 tree replacement_value;
4830 struct constructor_range_stack *range_stack;
4831 char constant;
4832 char simple;
4833 char implicit;
4834 char erroneous;
4835 char outer;
4836 char incremental;
4837 char designated;
4840 struct constructor_stack *constructor_stack;
4842 /* This stack represents designators from some range designator up to
4843 the last designator in the list. */
4845 struct constructor_range_stack
4847 struct constructor_range_stack *next, *prev;
4848 struct constructor_stack *stack;
4849 tree range_start;
4850 tree index;
4851 tree range_end;
4852 tree fields;
4855 struct constructor_range_stack *constructor_range_stack;
4857 /* This stack records separate initializers that are nested.
4858 Nested initializers can't happen in ANSI C, but GNU C allows them
4859 in cases like { ... (struct foo) { ... } ... }. */
4861 struct initializer_stack
4863 struct initializer_stack *next;
4864 tree decl;
4865 const char *asmspec;
4866 struct constructor_stack *constructor_stack;
4867 struct constructor_range_stack *constructor_range_stack;
4868 tree elements;
4869 struct spelling *spelling;
4870 struct spelling *spelling_base;
4871 int spelling_size;
4872 char top_level;
4873 char require_constant_value;
4874 char require_constant_elements;
4877 struct initializer_stack *initializer_stack;
4879 /* Prepare to parse and output the initializer for variable DECL. */
4881 void
4882 start_init (tree decl, tree asmspec_tree, int top_level)
4884 const char *locus;
4885 struct initializer_stack *p
4886 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4887 const char *asmspec = 0;
4889 if (asmspec_tree)
4890 asmspec = TREE_STRING_POINTER (asmspec_tree);
4892 p->decl = constructor_decl;
4893 p->asmspec = constructor_asmspec;
4894 p->require_constant_value = require_constant_value;
4895 p->require_constant_elements = require_constant_elements;
4896 p->constructor_stack = constructor_stack;
4897 p->constructor_range_stack = constructor_range_stack;
4898 p->elements = constructor_elements;
4899 p->spelling = spelling;
4900 p->spelling_base = spelling_base;
4901 p->spelling_size = spelling_size;
4902 p->top_level = constructor_top_level;
4903 p->next = initializer_stack;
4904 initializer_stack = p;
4906 constructor_decl = decl;
4907 constructor_asmspec = asmspec;
4908 constructor_designated = 0;
4909 constructor_top_level = top_level;
4911 if (decl != 0)
4913 require_constant_value = TREE_STATIC (decl);
4914 require_constant_elements
4915 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4916 /* For a scalar, you can always use any value to initialize,
4917 even within braces. */
4918 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4919 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4920 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4921 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4922 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4924 else
4926 require_constant_value = 0;
4927 require_constant_elements = 0;
4928 locus = "(anonymous)";
4931 constructor_stack = 0;
4932 constructor_range_stack = 0;
4934 missing_braces_mentioned = 0;
4936 spelling_base = 0;
4937 spelling_size = 0;
4938 RESTORE_SPELLING_DEPTH (0);
4940 if (locus)
4941 push_string (locus);
4944 void
4945 finish_init (void)
4947 struct initializer_stack *p = initializer_stack;
4949 /* Free the whole constructor stack of this initializer. */
4950 while (constructor_stack)
4952 struct constructor_stack *q = constructor_stack;
4953 constructor_stack = q->next;
4954 free (q);
4957 if (constructor_range_stack)
4958 abort ();
4960 /* Pop back to the data of the outer initializer (if any). */
4961 constructor_decl = p->decl;
4962 constructor_asmspec = p->asmspec;
4963 require_constant_value = p->require_constant_value;
4964 require_constant_elements = p->require_constant_elements;
4965 constructor_stack = p->constructor_stack;
4966 constructor_range_stack = p->constructor_range_stack;
4967 constructor_elements = p->elements;
4968 spelling = p->spelling;
4969 spelling_base = p->spelling_base;
4970 spelling_size = p->spelling_size;
4971 constructor_top_level = p->top_level;
4972 initializer_stack = p->next;
4973 free (p);
4976 /* Call here when we see the initializer is surrounded by braces.
4977 This is instead of a call to push_init_level;
4978 it is matched by a call to pop_init_level.
4980 TYPE is the type to initialize, for a constructor expression.
4981 For an initializer for a decl, TYPE is zero. */
4983 void
4984 really_start_incremental_init (tree type)
4986 struct constructor_stack *p
4987 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4989 if (type == 0)
4990 type = TREE_TYPE (constructor_decl);
4992 if ((*targetm.vector_opaque_p) (type))
4993 error ("opaque vector types cannot be initialized");
4995 p->type = constructor_type;
4996 p->fields = constructor_fields;
4997 p->index = constructor_index;
4998 p->max_index = constructor_max_index;
4999 p->unfilled_index = constructor_unfilled_index;
5000 p->unfilled_fields = constructor_unfilled_fields;
5001 p->bit_index = constructor_bit_index;
5002 p->elements = constructor_elements;
5003 p->constant = constructor_constant;
5004 p->simple = constructor_simple;
5005 p->erroneous = constructor_erroneous;
5006 p->pending_elts = constructor_pending_elts;
5007 p->depth = constructor_depth;
5008 p->replacement_value = 0;
5009 p->implicit = 0;
5010 p->range_stack = 0;
5011 p->outer = 0;
5012 p->incremental = constructor_incremental;
5013 p->designated = constructor_designated;
5014 p->next = 0;
5015 constructor_stack = p;
5017 constructor_constant = 1;
5018 constructor_simple = 1;
5019 constructor_depth = SPELLING_DEPTH ();
5020 constructor_elements = 0;
5021 constructor_pending_elts = 0;
5022 constructor_type = type;
5023 constructor_incremental = 1;
5024 constructor_designated = 0;
5025 designator_depth = 0;
5026 designator_errorneous = 0;
5028 if (TREE_CODE (constructor_type) == RECORD_TYPE
5029 || TREE_CODE (constructor_type) == UNION_TYPE)
5031 constructor_fields = TYPE_FIELDS (constructor_type);
5032 /* Skip any nameless bit fields at the beginning. */
5033 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5034 && DECL_NAME (constructor_fields) == 0)
5035 constructor_fields = TREE_CHAIN (constructor_fields);
5037 constructor_unfilled_fields = constructor_fields;
5038 constructor_bit_index = bitsize_zero_node;
5040 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5042 if (TYPE_DOMAIN (constructor_type))
5044 constructor_max_index
5045 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5047 /* Detect non-empty initializations of zero-length arrays. */
5048 if (constructor_max_index == NULL_TREE
5049 && TYPE_SIZE (constructor_type))
5050 constructor_max_index = build_int_2 (-1, -1);
5052 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5053 to initialize VLAs will cause a proper error; avoid tree
5054 checking errors as well by setting a safe value. */
5055 if (constructor_max_index
5056 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5057 constructor_max_index = build_int_2 (-1, -1);
5059 constructor_index
5060 = convert (bitsizetype,
5061 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5063 else
5064 constructor_index = bitsize_zero_node;
5066 constructor_unfilled_index = constructor_index;
5068 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5070 /* Vectors are like simple fixed-size arrays. */
5071 constructor_max_index =
5072 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5073 constructor_index = convert (bitsizetype, bitsize_zero_node);
5074 constructor_unfilled_index = constructor_index;
5076 else
5078 /* Handle the case of int x = {5}; */
5079 constructor_fields = constructor_type;
5080 constructor_unfilled_fields = constructor_type;
5084 /* Push down into a subobject, for initialization.
5085 If this is for an explicit set of braces, IMPLICIT is 0.
5086 If it is because the next element belongs at a lower level,
5087 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5089 void
5090 push_init_level (int implicit)
5092 struct constructor_stack *p;
5093 tree value = NULL_TREE;
5095 /* If we've exhausted any levels that didn't have braces,
5096 pop them now. */
5097 while (constructor_stack->implicit)
5099 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5100 || TREE_CODE (constructor_type) == UNION_TYPE)
5101 && constructor_fields == 0)
5102 process_init_element (pop_init_level (1));
5103 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5104 && constructor_max_index
5105 && tree_int_cst_lt (constructor_max_index, constructor_index))
5106 process_init_element (pop_init_level (1));
5107 else
5108 break;
5111 /* Unless this is an explicit brace, we need to preserve previous
5112 content if any. */
5113 if (implicit)
5115 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5116 || TREE_CODE (constructor_type) == UNION_TYPE)
5117 && constructor_fields)
5118 value = find_init_member (constructor_fields);
5119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5120 value = find_init_member (constructor_index);
5123 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5124 p->type = constructor_type;
5125 p->fields = constructor_fields;
5126 p->index = constructor_index;
5127 p->max_index = constructor_max_index;
5128 p->unfilled_index = constructor_unfilled_index;
5129 p->unfilled_fields = constructor_unfilled_fields;
5130 p->bit_index = constructor_bit_index;
5131 p->elements = constructor_elements;
5132 p->constant = constructor_constant;
5133 p->simple = constructor_simple;
5134 p->erroneous = constructor_erroneous;
5135 p->pending_elts = constructor_pending_elts;
5136 p->depth = constructor_depth;
5137 p->replacement_value = 0;
5138 p->implicit = implicit;
5139 p->outer = 0;
5140 p->incremental = constructor_incremental;
5141 p->designated = constructor_designated;
5142 p->next = constructor_stack;
5143 p->range_stack = 0;
5144 constructor_stack = p;
5146 constructor_constant = 1;
5147 constructor_simple = 1;
5148 constructor_depth = SPELLING_DEPTH ();
5149 constructor_elements = 0;
5150 constructor_incremental = 1;
5151 constructor_designated = 0;
5152 constructor_pending_elts = 0;
5153 if (!implicit)
5155 p->range_stack = constructor_range_stack;
5156 constructor_range_stack = 0;
5157 designator_depth = 0;
5158 designator_errorneous = 0;
5161 /* Don't die if an entire brace-pair level is superfluous
5162 in the containing level. */
5163 if (constructor_type == 0)
5165 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5166 || TREE_CODE (constructor_type) == UNION_TYPE)
5168 /* Don't die if there are extra init elts at the end. */
5169 if (constructor_fields == 0)
5170 constructor_type = 0;
5171 else
5173 constructor_type = TREE_TYPE (constructor_fields);
5174 push_member_name (constructor_fields);
5175 constructor_depth++;
5178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5180 constructor_type = TREE_TYPE (constructor_type);
5181 push_array_bounds (tree_low_cst (constructor_index, 0));
5182 constructor_depth++;
5185 if (constructor_type == 0)
5187 error_init ("extra brace group at end of initializer");
5188 constructor_fields = 0;
5189 constructor_unfilled_fields = 0;
5190 return;
5193 if (value && TREE_CODE (value) == CONSTRUCTOR)
5195 constructor_constant = TREE_CONSTANT (value);
5196 constructor_simple = TREE_STATIC (value);
5197 constructor_elements = CONSTRUCTOR_ELTS (value);
5198 if (constructor_elements
5199 && (TREE_CODE (constructor_type) == RECORD_TYPE
5200 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5201 set_nonincremental_init ();
5204 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5206 missing_braces_mentioned = 1;
5207 warning_init ("missing braces around initializer");
5210 if (TREE_CODE (constructor_type) == RECORD_TYPE
5211 || TREE_CODE (constructor_type) == UNION_TYPE)
5213 constructor_fields = TYPE_FIELDS (constructor_type);
5214 /* Skip any nameless bit fields at the beginning. */
5215 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5216 && DECL_NAME (constructor_fields) == 0)
5217 constructor_fields = TREE_CHAIN (constructor_fields);
5219 constructor_unfilled_fields = constructor_fields;
5220 constructor_bit_index = bitsize_zero_node;
5222 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5224 /* Vectors are like simple fixed-size arrays. */
5225 constructor_max_index =
5226 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5227 constructor_index = convert (bitsizetype, integer_zero_node);
5228 constructor_unfilled_index = constructor_index;
5230 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5232 if (TYPE_DOMAIN (constructor_type))
5234 constructor_max_index
5235 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5237 /* Detect non-empty initializations of zero-length arrays. */
5238 if (constructor_max_index == NULL_TREE
5239 && TYPE_SIZE (constructor_type))
5240 constructor_max_index = build_int_2 (-1, -1);
5242 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5243 to initialize VLAs will cause a proper error; avoid tree
5244 checking errors as well by setting a safe value. */
5245 if (constructor_max_index
5246 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5247 constructor_max_index = build_int_2 (-1, -1);
5249 constructor_index
5250 = convert (bitsizetype,
5251 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5253 else
5254 constructor_index = bitsize_zero_node;
5256 constructor_unfilled_index = constructor_index;
5257 if (value && TREE_CODE (value) == STRING_CST)
5259 /* We need to split the char/wchar array into individual
5260 characters, so that we don't have to special case it
5261 everywhere. */
5262 set_nonincremental_init_from_string (value);
5265 else
5267 warning_init ("braces around scalar initializer");
5268 constructor_fields = constructor_type;
5269 constructor_unfilled_fields = constructor_type;
5273 /* At the end of an implicit or explicit brace level,
5274 finish up that level of constructor.
5275 If we were outputting the elements as they are read, return 0
5276 from inner levels (process_init_element ignores that),
5277 but return error_mark_node from the outermost level
5278 (that's what we want to put in DECL_INITIAL).
5279 Otherwise, return a CONSTRUCTOR expression. */
5281 tree
5282 pop_init_level (int implicit)
5284 struct constructor_stack *p;
5285 tree constructor = 0;
5287 if (implicit == 0)
5289 /* When we come to an explicit close brace,
5290 pop any inner levels that didn't have explicit braces. */
5291 while (constructor_stack->implicit)
5292 process_init_element (pop_init_level (1));
5294 if (constructor_range_stack)
5295 abort ();
5298 p = constructor_stack;
5300 /* Error for initializing a flexible array member, or a zero-length
5301 array member in an inappropriate context. */
5302 if (constructor_type && constructor_fields
5303 && TREE_CODE (constructor_type) == ARRAY_TYPE
5304 && TYPE_DOMAIN (constructor_type)
5305 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5307 /* Silently discard empty initializations. The parser will
5308 already have pedwarned for empty brackets. */
5309 if (integer_zerop (constructor_unfilled_index))
5310 constructor_type = NULL_TREE;
5311 else if (! TYPE_SIZE (constructor_type))
5313 if (constructor_depth > 2)
5314 error_init ("initialization of flexible array member in a nested context");
5315 else if (pedantic)
5316 pedwarn_init ("initialization of a flexible array member");
5318 /* We have already issued an error message for the existence
5319 of a flexible array member not at the end of the structure.
5320 Discard the initializer so that we do not abort later. */
5321 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5322 constructor_type = NULL_TREE;
5324 else
5325 /* Zero-length arrays are no longer special, so we should no longer
5326 get here. */
5327 abort ();
5330 /* Warn when some struct elements are implicitly initialized to zero. */
5331 if (extra_warnings
5332 && constructor_type
5333 && TREE_CODE (constructor_type) == RECORD_TYPE
5334 && constructor_unfilled_fields)
5336 /* Do not warn for flexible array members or zero-length arrays. */
5337 while (constructor_unfilled_fields
5338 && (! DECL_SIZE (constructor_unfilled_fields)
5339 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5340 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5342 /* Do not warn if this level of the initializer uses member
5343 designators; it is likely to be deliberate. */
5344 if (constructor_unfilled_fields && !constructor_designated)
5346 push_member_name (constructor_unfilled_fields);
5347 warning_init ("missing initializer");
5348 RESTORE_SPELLING_DEPTH (constructor_depth);
5352 /* Now output all pending elements. */
5353 constructor_incremental = 1;
5354 output_pending_init_elements (1);
5356 /* Pad out the end of the structure. */
5357 if (p->replacement_value)
5358 /* If this closes a superfluous brace pair,
5359 just pass out the element between them. */
5360 constructor = p->replacement_value;
5361 else if (constructor_type == 0)
5363 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5364 && TREE_CODE (constructor_type) != UNION_TYPE
5365 && TREE_CODE (constructor_type) != ARRAY_TYPE
5366 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5368 /* A nonincremental scalar initializer--just return
5369 the element, after verifying there is just one. */
5370 if (constructor_elements == 0)
5372 if (!constructor_erroneous)
5373 error_init ("empty scalar initializer");
5374 constructor = error_mark_node;
5376 else if (TREE_CHAIN (constructor_elements) != 0)
5378 error_init ("extra elements in scalar initializer");
5379 constructor = TREE_VALUE (constructor_elements);
5381 else
5382 constructor = TREE_VALUE (constructor_elements);
5384 else
5386 if (constructor_erroneous)
5387 constructor = error_mark_node;
5388 else
5390 constructor = build_constructor (constructor_type,
5391 nreverse (constructor_elements));
5392 if (constructor_constant)
5393 TREE_CONSTANT (constructor) = 1;
5394 if (constructor_constant && constructor_simple)
5395 TREE_STATIC (constructor) = 1;
5399 constructor_type = p->type;
5400 constructor_fields = p->fields;
5401 constructor_index = p->index;
5402 constructor_max_index = p->max_index;
5403 constructor_unfilled_index = p->unfilled_index;
5404 constructor_unfilled_fields = p->unfilled_fields;
5405 constructor_bit_index = p->bit_index;
5406 constructor_elements = p->elements;
5407 constructor_constant = p->constant;
5408 constructor_simple = p->simple;
5409 constructor_erroneous = p->erroneous;
5410 constructor_incremental = p->incremental;
5411 constructor_designated = p->designated;
5412 constructor_pending_elts = p->pending_elts;
5413 constructor_depth = p->depth;
5414 if (!p->implicit)
5415 constructor_range_stack = p->range_stack;
5416 RESTORE_SPELLING_DEPTH (constructor_depth);
5418 constructor_stack = p->next;
5419 free (p);
5421 if (constructor == 0)
5423 if (constructor_stack == 0)
5424 return error_mark_node;
5425 return NULL_TREE;
5427 return constructor;
5430 /* Common handling for both array range and field name designators.
5431 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5433 static int
5434 set_designator (int array)
5436 tree subtype;
5437 enum tree_code subcode;
5439 /* Don't die if an entire brace-pair level is superfluous
5440 in the containing level. */
5441 if (constructor_type == 0)
5442 return 1;
5444 /* If there were errors in this designator list already, bail out silently. */
5445 if (designator_errorneous)
5446 return 1;
5448 if (!designator_depth)
5450 if (constructor_range_stack)
5451 abort ();
5453 /* Designator list starts at the level of closest explicit
5454 braces. */
5455 while (constructor_stack->implicit)
5456 process_init_element (pop_init_level (1));
5457 constructor_designated = 1;
5458 return 0;
5461 if (constructor_no_implicit)
5463 error_init ("initialization designators may not nest");
5464 return 1;
5467 if (TREE_CODE (constructor_type) == RECORD_TYPE
5468 || TREE_CODE (constructor_type) == UNION_TYPE)
5470 subtype = TREE_TYPE (constructor_fields);
5471 if (subtype != error_mark_node)
5472 subtype = TYPE_MAIN_VARIANT (subtype);
5474 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5476 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5478 else
5479 abort ();
5481 subcode = TREE_CODE (subtype);
5482 if (array && subcode != ARRAY_TYPE)
5484 error_init ("array index in non-array initializer");
5485 return 1;
5487 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5489 error_init ("field name not in record or union initializer");
5490 return 1;
5493 constructor_designated = 1;
5494 push_init_level (2);
5495 return 0;
5498 /* If there are range designators in designator list, push a new designator
5499 to constructor_range_stack. RANGE_END is end of such stack range or
5500 NULL_TREE if there is no range designator at this level. */
5502 static void
5503 push_range_stack (tree range_end)
5505 struct constructor_range_stack *p;
5507 p = (struct constructor_range_stack *)
5508 ggc_alloc (sizeof (struct constructor_range_stack));
5509 p->prev = constructor_range_stack;
5510 p->next = 0;
5511 p->fields = constructor_fields;
5512 p->range_start = constructor_index;
5513 p->index = constructor_index;
5514 p->stack = constructor_stack;
5515 p->range_end = range_end;
5516 if (constructor_range_stack)
5517 constructor_range_stack->next = p;
5518 constructor_range_stack = p;
5521 /* Within an array initializer, specify the next index to be initialized.
5522 FIRST is that index. If LAST is nonzero, then initialize a range
5523 of indices, running from FIRST through LAST. */
5525 void
5526 set_init_index (tree first, tree last)
5528 if (set_designator (1))
5529 return;
5531 designator_errorneous = 1;
5533 while ((TREE_CODE (first) == NOP_EXPR
5534 || TREE_CODE (first) == CONVERT_EXPR
5535 || TREE_CODE (first) == NON_LVALUE_EXPR)
5536 && (TYPE_MODE (TREE_TYPE (first))
5537 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5538 first = TREE_OPERAND (first, 0);
5540 if (last)
5541 while ((TREE_CODE (last) == NOP_EXPR
5542 || TREE_CODE (last) == CONVERT_EXPR
5543 || TREE_CODE (last) == NON_LVALUE_EXPR)
5544 && (TYPE_MODE (TREE_TYPE (last))
5545 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5546 last = TREE_OPERAND (last, 0);
5548 if (TREE_CODE (first) != INTEGER_CST)
5549 error_init ("nonconstant array index in initializer");
5550 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5551 error_init ("nonconstant array index in initializer");
5552 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5553 error_init ("array index in non-array initializer");
5554 else if (constructor_max_index
5555 && tree_int_cst_lt (constructor_max_index, first))
5556 error_init ("array index in initializer exceeds array bounds");
5557 else
5559 constructor_index = convert (bitsizetype, first);
5561 if (last)
5563 if (tree_int_cst_equal (first, last))
5564 last = 0;
5565 else if (tree_int_cst_lt (last, first))
5567 error_init ("empty index range in initializer");
5568 last = 0;
5570 else
5572 last = convert (bitsizetype, last);
5573 if (constructor_max_index != 0
5574 && tree_int_cst_lt (constructor_max_index, last))
5576 error_init ("array index range in initializer exceeds array bounds");
5577 last = 0;
5582 designator_depth++;
5583 designator_errorneous = 0;
5584 if (constructor_range_stack || last)
5585 push_range_stack (last);
5589 /* Within a struct initializer, specify the next field to be initialized. */
5591 void
5592 set_init_label (tree fieldname)
5594 tree tail;
5596 if (set_designator (0))
5597 return;
5599 designator_errorneous = 1;
5601 if (TREE_CODE (constructor_type) != RECORD_TYPE
5602 && TREE_CODE (constructor_type) != UNION_TYPE)
5604 error_init ("field name not in record or union initializer");
5605 return;
5608 for (tail = TYPE_FIELDS (constructor_type); tail;
5609 tail = TREE_CHAIN (tail))
5611 if (DECL_NAME (tail) == fieldname)
5612 break;
5615 if (tail == 0)
5616 error ("unknown field `%s' specified in initializer",
5617 IDENTIFIER_POINTER (fieldname));
5618 else
5620 constructor_fields = tail;
5621 designator_depth++;
5622 designator_errorneous = 0;
5623 if (constructor_range_stack)
5624 push_range_stack (NULL_TREE);
5628 /* Add a new initializer to the tree of pending initializers. PURPOSE
5629 identifies the initializer, either array index or field in a structure.
5630 VALUE is the value of that index or field. */
5632 static void
5633 add_pending_init (tree purpose, tree value)
5635 struct init_node *p, **q, *r;
5637 q = &constructor_pending_elts;
5638 p = 0;
5640 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5642 while (*q != 0)
5644 p = *q;
5645 if (tree_int_cst_lt (purpose, p->purpose))
5646 q = &p->left;
5647 else if (tree_int_cst_lt (p->purpose, purpose))
5648 q = &p->right;
5649 else
5651 if (TREE_SIDE_EFFECTS (p->value))
5652 warning_init ("initialized field with side-effects overwritten");
5653 p->value = value;
5654 return;
5658 else
5660 tree bitpos;
5662 bitpos = bit_position (purpose);
5663 while (*q != NULL)
5665 p = *q;
5666 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5667 q = &p->left;
5668 else if (p->purpose != purpose)
5669 q = &p->right;
5670 else
5672 if (TREE_SIDE_EFFECTS (p->value))
5673 warning_init ("initialized field with side-effects overwritten");
5674 p->value = value;
5675 return;
5680 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5681 r->purpose = purpose;
5682 r->value = value;
5684 *q = r;
5685 r->parent = p;
5686 r->left = 0;
5687 r->right = 0;
5688 r->balance = 0;
5690 while (p)
5692 struct init_node *s;
5694 if (r == p->left)
5696 if (p->balance == 0)
5697 p->balance = -1;
5698 else if (p->balance < 0)
5700 if (r->balance < 0)
5702 /* L rotation. */
5703 p->left = r->right;
5704 if (p->left)
5705 p->left->parent = p;
5706 r->right = p;
5708 p->balance = 0;
5709 r->balance = 0;
5711 s = p->parent;
5712 p->parent = r;
5713 r->parent = s;
5714 if (s)
5716 if (s->left == p)
5717 s->left = r;
5718 else
5719 s->right = r;
5721 else
5722 constructor_pending_elts = r;
5724 else
5726 /* LR rotation. */
5727 struct init_node *t = r->right;
5729 r->right = t->left;
5730 if (r->right)
5731 r->right->parent = r;
5732 t->left = r;
5734 p->left = t->right;
5735 if (p->left)
5736 p->left->parent = p;
5737 t->right = p;
5739 p->balance = t->balance < 0;
5740 r->balance = -(t->balance > 0);
5741 t->balance = 0;
5743 s = p->parent;
5744 p->parent = t;
5745 r->parent = t;
5746 t->parent = s;
5747 if (s)
5749 if (s->left == p)
5750 s->left = t;
5751 else
5752 s->right = t;
5754 else
5755 constructor_pending_elts = t;
5757 break;
5759 else
5761 /* p->balance == +1; growth of left side balances the node. */
5762 p->balance = 0;
5763 break;
5766 else /* r == p->right */
5768 if (p->balance == 0)
5769 /* Growth propagation from right side. */
5770 p->balance++;
5771 else if (p->balance > 0)
5773 if (r->balance > 0)
5775 /* R rotation. */
5776 p->right = r->left;
5777 if (p->right)
5778 p->right->parent = p;
5779 r->left = p;
5781 p->balance = 0;
5782 r->balance = 0;
5784 s = p->parent;
5785 p->parent = r;
5786 r->parent = s;
5787 if (s)
5789 if (s->left == p)
5790 s->left = r;
5791 else
5792 s->right = r;
5794 else
5795 constructor_pending_elts = r;
5797 else /* r->balance == -1 */
5799 /* RL rotation */
5800 struct init_node *t = r->left;
5802 r->left = t->right;
5803 if (r->left)
5804 r->left->parent = r;
5805 t->right = r;
5807 p->right = t->left;
5808 if (p->right)
5809 p->right->parent = p;
5810 t->left = p;
5812 r->balance = (t->balance < 0);
5813 p->balance = -(t->balance > 0);
5814 t->balance = 0;
5816 s = p->parent;
5817 p->parent = t;
5818 r->parent = t;
5819 t->parent = s;
5820 if (s)
5822 if (s->left == p)
5823 s->left = t;
5824 else
5825 s->right = t;
5827 else
5828 constructor_pending_elts = t;
5830 break;
5832 else
5834 /* p->balance == -1; growth of right side balances the node. */
5835 p->balance = 0;
5836 break;
5840 r = p;
5841 p = p->parent;
5845 /* Build AVL tree from a sorted chain. */
5847 static void
5848 set_nonincremental_init (void)
5850 tree chain;
5852 if (TREE_CODE (constructor_type) != RECORD_TYPE
5853 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5854 return;
5856 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5857 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5858 constructor_elements = 0;
5859 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5861 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5862 /* Skip any nameless bit fields at the beginning. */
5863 while (constructor_unfilled_fields != 0
5864 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5865 && DECL_NAME (constructor_unfilled_fields) == 0)
5866 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5869 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5871 if (TYPE_DOMAIN (constructor_type))
5872 constructor_unfilled_index
5873 = convert (bitsizetype,
5874 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5875 else
5876 constructor_unfilled_index = bitsize_zero_node;
5878 constructor_incremental = 0;
5881 /* Build AVL tree from a string constant. */
5883 static void
5884 set_nonincremental_init_from_string (tree str)
5886 tree value, purpose, type;
5887 HOST_WIDE_INT val[2];
5888 const char *p, *end;
5889 int byte, wchar_bytes, charwidth, bitpos;
5891 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5892 abort ();
5894 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5895 == TYPE_PRECISION (char_type_node))
5896 wchar_bytes = 1;
5897 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5898 == TYPE_PRECISION (wchar_type_node))
5899 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5900 else
5901 abort ();
5903 charwidth = TYPE_PRECISION (char_type_node);
5904 type = TREE_TYPE (constructor_type);
5905 p = TREE_STRING_POINTER (str);
5906 end = p + TREE_STRING_LENGTH (str);
5908 for (purpose = bitsize_zero_node;
5909 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5910 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5912 if (wchar_bytes == 1)
5914 val[1] = (unsigned char) *p++;
5915 val[0] = 0;
5917 else
5919 val[0] = 0;
5920 val[1] = 0;
5921 for (byte = 0; byte < wchar_bytes; byte++)
5923 if (BYTES_BIG_ENDIAN)
5924 bitpos = (wchar_bytes - byte - 1) * charwidth;
5925 else
5926 bitpos = byte * charwidth;
5927 val[bitpos < HOST_BITS_PER_WIDE_INT]
5928 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5929 << (bitpos % HOST_BITS_PER_WIDE_INT);
5933 if (!TREE_UNSIGNED (type))
5935 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5936 if (bitpos < HOST_BITS_PER_WIDE_INT)
5938 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5940 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5941 val[0] = -1;
5944 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5946 if (val[1] < 0)
5947 val[0] = -1;
5949 else if (val[0] & (((HOST_WIDE_INT) 1)
5950 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5951 val[0] |= ((HOST_WIDE_INT) -1)
5952 << (bitpos - HOST_BITS_PER_WIDE_INT);
5955 value = build_int_2 (val[1], val[0]);
5956 TREE_TYPE (value) = type;
5957 add_pending_init (purpose, value);
5960 constructor_incremental = 0;
5963 /* Return value of FIELD in pending initializer or zero if the field was
5964 not initialized yet. */
5966 static tree
5967 find_init_member (tree field)
5969 struct init_node *p;
5971 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5973 if (constructor_incremental
5974 && tree_int_cst_lt (field, constructor_unfilled_index))
5975 set_nonincremental_init ();
5977 p = constructor_pending_elts;
5978 while (p)
5980 if (tree_int_cst_lt (field, p->purpose))
5981 p = p->left;
5982 else if (tree_int_cst_lt (p->purpose, field))
5983 p = p->right;
5984 else
5985 return p->value;
5988 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5990 tree bitpos = bit_position (field);
5992 if (constructor_incremental
5993 && (!constructor_unfilled_fields
5994 || tree_int_cst_lt (bitpos,
5995 bit_position (constructor_unfilled_fields))))
5996 set_nonincremental_init ();
5998 p = constructor_pending_elts;
5999 while (p)
6001 if (field == p->purpose)
6002 return p->value;
6003 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6004 p = p->left;
6005 else
6006 p = p->right;
6009 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6011 if (constructor_elements
6012 && TREE_PURPOSE (constructor_elements) == field)
6013 return TREE_VALUE (constructor_elements);
6015 return 0;
6018 /* "Output" the next constructor element.
6019 At top level, really output it to assembler code now.
6020 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6021 TYPE is the data type that the containing data type wants here.
6022 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6024 PENDING if non-nil means output pending elements that belong
6025 right after this element. (PENDING is normally 1;
6026 it is 0 while outputting pending elements, to avoid recursion.) */
6028 static void
6029 output_init_element (tree value, tree type, tree field, int pending)
6031 if (type == error_mark_node)
6033 constructor_erroneous = 1;
6034 return;
6036 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6037 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6038 && !(TREE_CODE (value) == STRING_CST
6039 && TREE_CODE (type) == ARRAY_TYPE
6040 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6041 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6042 TYPE_MAIN_VARIANT (type))))
6043 value = default_conversion (value);
6045 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6046 && require_constant_value && !flag_isoc99 && pending)
6048 /* As an extension, allow initializing objects with static storage
6049 duration with compound literals (which are then treated just as
6050 the brace enclosed list they contain). */
6051 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6052 value = DECL_INITIAL (decl);
6055 if (value == error_mark_node)
6056 constructor_erroneous = 1;
6057 else if (!TREE_CONSTANT (value))
6058 constructor_constant = 0;
6059 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6060 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6061 || TREE_CODE (constructor_type) == UNION_TYPE)
6062 && DECL_C_BIT_FIELD (field)
6063 && TREE_CODE (value) != INTEGER_CST))
6064 constructor_simple = 0;
6066 if (require_constant_value && ! TREE_CONSTANT (value))
6068 error_init ("initializer element is not constant");
6069 value = error_mark_node;
6071 else if (require_constant_elements
6072 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6073 pedwarn ("initializer element is not computable at load time");
6075 /* If this field is empty (and not at the end of structure),
6076 don't do anything other than checking the initializer. */
6077 if (field
6078 && (TREE_TYPE (field) == error_mark_node
6079 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6080 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6081 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6082 || TREE_CHAIN (field)))))
6083 return;
6085 value = digest_init (type, value, require_constant_value);
6086 if (value == error_mark_node)
6088 constructor_erroneous = 1;
6089 return;
6092 /* If this element doesn't come next in sequence,
6093 put it on constructor_pending_elts. */
6094 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6095 && (!constructor_incremental
6096 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6098 if (constructor_incremental
6099 && tree_int_cst_lt (field, constructor_unfilled_index))
6100 set_nonincremental_init ();
6102 add_pending_init (field, value);
6103 return;
6105 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6106 && (!constructor_incremental
6107 || field != constructor_unfilled_fields))
6109 /* We do this for records but not for unions. In a union,
6110 no matter which field is specified, it can be initialized
6111 right away since it starts at the beginning of the union. */
6112 if (constructor_incremental)
6114 if (!constructor_unfilled_fields)
6115 set_nonincremental_init ();
6116 else
6118 tree bitpos, unfillpos;
6120 bitpos = bit_position (field);
6121 unfillpos = bit_position (constructor_unfilled_fields);
6123 if (tree_int_cst_lt (bitpos, unfillpos))
6124 set_nonincremental_init ();
6128 add_pending_init (field, value);
6129 return;
6131 else if (TREE_CODE (constructor_type) == UNION_TYPE
6132 && constructor_elements)
6134 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6135 warning_init ("initialized field with side-effects overwritten");
6137 /* We can have just one union field set. */
6138 constructor_elements = 0;
6141 /* Otherwise, output this element either to
6142 constructor_elements or to the assembler file. */
6144 if (field && TREE_CODE (field) == INTEGER_CST)
6145 field = copy_node (field);
6146 constructor_elements
6147 = tree_cons (field, value, constructor_elements);
6149 /* Advance the variable that indicates sequential elements output. */
6150 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6151 constructor_unfilled_index
6152 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6153 bitsize_one_node);
6154 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6156 constructor_unfilled_fields
6157 = TREE_CHAIN (constructor_unfilled_fields);
6159 /* Skip any nameless bit fields. */
6160 while (constructor_unfilled_fields != 0
6161 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6162 && DECL_NAME (constructor_unfilled_fields) == 0)
6163 constructor_unfilled_fields =
6164 TREE_CHAIN (constructor_unfilled_fields);
6166 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6167 constructor_unfilled_fields = 0;
6169 /* Now output any pending elements which have become next. */
6170 if (pending)
6171 output_pending_init_elements (0);
6174 /* Output any pending elements which have become next.
6175 As we output elements, constructor_unfilled_{fields,index}
6176 advances, which may cause other elements to become next;
6177 if so, they too are output.
6179 If ALL is 0, we return when there are
6180 no more pending elements to output now.
6182 If ALL is 1, we output space as necessary so that
6183 we can output all the pending elements. */
6185 static void
6186 output_pending_init_elements (int all)
6188 struct init_node *elt = constructor_pending_elts;
6189 tree next;
6191 retry:
6193 /* Look thru the whole pending tree.
6194 If we find an element that should be output now,
6195 output it. Otherwise, set NEXT to the element
6196 that comes first among those still pending. */
6198 next = 0;
6199 while (elt)
6201 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6203 if (tree_int_cst_equal (elt->purpose,
6204 constructor_unfilled_index))
6205 output_init_element (elt->value,
6206 TREE_TYPE (constructor_type),
6207 constructor_unfilled_index, 0);
6208 else if (tree_int_cst_lt (constructor_unfilled_index,
6209 elt->purpose))
6211 /* Advance to the next smaller node. */
6212 if (elt->left)
6213 elt = elt->left;
6214 else
6216 /* We have reached the smallest node bigger than the
6217 current unfilled index. Fill the space first. */
6218 next = elt->purpose;
6219 break;
6222 else
6224 /* Advance to the next bigger node. */
6225 if (elt->right)
6226 elt = elt->right;
6227 else
6229 /* We have reached the biggest node in a subtree. Find
6230 the parent of it, which is the next bigger node. */
6231 while (elt->parent && elt->parent->right == elt)
6232 elt = elt->parent;
6233 elt = elt->parent;
6234 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6235 elt->purpose))
6237 next = elt->purpose;
6238 break;
6243 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6244 || TREE_CODE (constructor_type) == UNION_TYPE)
6246 tree ctor_unfilled_bitpos, elt_bitpos;
6248 /* If the current record is complete we are done. */
6249 if (constructor_unfilled_fields == 0)
6250 break;
6252 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6253 elt_bitpos = bit_position (elt->purpose);
6254 /* We can't compare fields here because there might be empty
6255 fields in between. */
6256 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6258 constructor_unfilled_fields = elt->purpose;
6259 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6260 elt->purpose, 0);
6262 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6264 /* Advance to the next smaller node. */
6265 if (elt->left)
6266 elt = elt->left;
6267 else
6269 /* We have reached the smallest node bigger than the
6270 current unfilled field. Fill the space first. */
6271 next = elt->purpose;
6272 break;
6275 else
6277 /* Advance to the next bigger node. */
6278 if (elt->right)
6279 elt = elt->right;
6280 else
6282 /* We have reached the biggest node in a subtree. Find
6283 the parent of it, which is the next bigger node. */
6284 while (elt->parent && elt->parent->right == elt)
6285 elt = elt->parent;
6286 elt = elt->parent;
6287 if (elt
6288 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6289 bit_position (elt->purpose))))
6291 next = elt->purpose;
6292 break;
6299 /* Ordinarily return, but not if we want to output all
6300 and there are elements left. */
6301 if (! (all && next != 0))
6302 return;
6304 /* If it's not incremental, just skip over the gap, so that after
6305 jumping to retry we will output the next successive element. */
6306 if (TREE_CODE (constructor_type) == RECORD_TYPE
6307 || TREE_CODE (constructor_type) == UNION_TYPE)
6308 constructor_unfilled_fields = next;
6309 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6310 constructor_unfilled_index = next;
6312 /* ELT now points to the node in the pending tree with the next
6313 initializer to output. */
6314 goto retry;
6317 /* Add one non-braced element to the current constructor level.
6318 This adjusts the current position within the constructor's type.
6319 This may also start or terminate implicit levels
6320 to handle a partly-braced initializer.
6322 Once this has found the correct level for the new element,
6323 it calls output_init_element. */
6325 void
6326 process_init_element (tree value)
6328 tree orig_value = value;
6329 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6331 designator_depth = 0;
6332 designator_errorneous = 0;
6334 /* Handle superfluous braces around string cst as in
6335 char x[] = {"foo"}; */
6336 if (string_flag
6337 && constructor_type
6338 && TREE_CODE (constructor_type) == ARRAY_TYPE
6339 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6340 && integer_zerop (constructor_unfilled_index))
6342 if (constructor_stack->replacement_value)
6343 error_init ("excess elements in char array initializer");
6344 constructor_stack->replacement_value = value;
6345 return;
6348 if (constructor_stack->replacement_value != 0)
6350 error_init ("excess elements in struct initializer");
6351 return;
6354 /* Ignore elements of a brace group if it is entirely superfluous
6355 and has already been diagnosed. */
6356 if (constructor_type == 0)
6357 return;
6359 /* If we've exhausted any levels that didn't have braces,
6360 pop them now. */
6361 while (constructor_stack->implicit)
6363 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6364 || TREE_CODE (constructor_type) == UNION_TYPE)
6365 && constructor_fields == 0)
6366 process_init_element (pop_init_level (1));
6367 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6368 && (constructor_max_index == 0
6369 || tree_int_cst_lt (constructor_max_index,
6370 constructor_index)))
6371 process_init_element (pop_init_level (1));
6372 else
6373 break;
6376 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6377 if (constructor_range_stack)
6379 /* If value is a compound literal and we'll be just using its
6380 content, don't put it into a SAVE_EXPR. */
6381 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6382 || !require_constant_value
6383 || flag_isoc99)
6384 value = save_expr (value);
6387 while (1)
6389 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6391 tree fieldtype;
6392 enum tree_code fieldcode;
6394 if (constructor_fields == 0)
6396 pedwarn_init ("excess elements in struct initializer");
6397 break;
6400 fieldtype = TREE_TYPE (constructor_fields);
6401 if (fieldtype != error_mark_node)
6402 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6403 fieldcode = TREE_CODE (fieldtype);
6405 /* Error for non-static initialization of a flexible array member. */
6406 if (fieldcode == ARRAY_TYPE
6407 && !require_constant_value
6408 && TYPE_SIZE (fieldtype) == NULL_TREE
6409 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6411 error_init ("non-static initialization of a flexible array member");
6412 break;
6415 /* Accept a string constant to initialize a subarray. */
6416 if (value != 0
6417 && fieldcode == ARRAY_TYPE
6418 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6419 && string_flag)
6420 value = orig_value;
6421 /* Otherwise, if we have come to a subaggregate,
6422 and we don't have an element of its type, push into it. */
6423 else if (value != 0 && !constructor_no_implicit
6424 && value != error_mark_node
6425 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6426 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6427 || fieldcode == UNION_TYPE))
6429 push_init_level (1);
6430 continue;
6433 if (value)
6435 push_member_name (constructor_fields);
6436 output_init_element (value, fieldtype, constructor_fields, 1);
6437 RESTORE_SPELLING_DEPTH (constructor_depth);
6439 else
6440 /* Do the bookkeeping for an element that was
6441 directly output as a constructor. */
6443 /* For a record, keep track of end position of last field. */
6444 if (DECL_SIZE (constructor_fields))
6445 constructor_bit_index
6446 = size_binop (PLUS_EXPR,
6447 bit_position (constructor_fields),
6448 DECL_SIZE (constructor_fields));
6450 /* If the current field was the first one not yet written out,
6451 it isn't now, so update. */
6452 if (constructor_unfilled_fields == constructor_fields)
6454 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6455 /* Skip any nameless bit fields. */
6456 while (constructor_unfilled_fields != 0
6457 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6458 && DECL_NAME (constructor_unfilled_fields) == 0)
6459 constructor_unfilled_fields =
6460 TREE_CHAIN (constructor_unfilled_fields);
6464 constructor_fields = TREE_CHAIN (constructor_fields);
6465 /* Skip any nameless bit fields at the beginning. */
6466 while (constructor_fields != 0
6467 && DECL_C_BIT_FIELD (constructor_fields)
6468 && DECL_NAME (constructor_fields) == 0)
6469 constructor_fields = TREE_CHAIN (constructor_fields);
6471 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6473 tree fieldtype;
6474 enum tree_code fieldcode;
6476 if (constructor_fields == 0)
6478 pedwarn_init ("excess elements in union initializer");
6479 break;
6482 fieldtype = TREE_TYPE (constructor_fields);
6483 if (fieldtype != error_mark_node)
6484 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6485 fieldcode = TREE_CODE (fieldtype);
6487 /* Warn that traditional C rejects initialization of unions.
6488 We skip the warning if the value is zero. This is done
6489 under the assumption that the zero initializer in user
6490 code appears conditioned on e.g. __STDC__ to avoid
6491 "missing initializer" warnings and relies on default
6492 initialization to zero in the traditional C case.
6493 We also skip the warning if the initializer is designated,
6494 again on the assumption that this must be conditional on
6495 __STDC__ anyway (and we've already complained about the
6496 member-designator already). */
6497 if (warn_traditional && !in_system_header && !constructor_designated
6498 && !(value && (integer_zerop (value) || real_zerop (value))))
6499 warning ("traditional C rejects initialization of unions");
6501 /* Accept a string constant to initialize a subarray. */
6502 if (value != 0
6503 && fieldcode == ARRAY_TYPE
6504 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6505 && string_flag)
6506 value = orig_value;
6507 /* Otherwise, if we have come to a subaggregate,
6508 and we don't have an element of its type, push into it. */
6509 else if (value != 0 && !constructor_no_implicit
6510 && value != error_mark_node
6511 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6512 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6513 || fieldcode == UNION_TYPE))
6515 push_init_level (1);
6516 continue;
6519 if (value)
6521 push_member_name (constructor_fields);
6522 output_init_element (value, fieldtype, constructor_fields, 1);
6523 RESTORE_SPELLING_DEPTH (constructor_depth);
6525 else
6526 /* Do the bookkeeping for an element that was
6527 directly output as a constructor. */
6529 constructor_bit_index = DECL_SIZE (constructor_fields);
6530 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6533 constructor_fields = 0;
6535 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6537 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6538 enum tree_code eltcode = TREE_CODE (elttype);
6540 /* Accept a string constant to initialize a subarray. */
6541 if (value != 0
6542 && eltcode == ARRAY_TYPE
6543 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6544 && string_flag)
6545 value = orig_value;
6546 /* Otherwise, if we have come to a subaggregate,
6547 and we don't have an element of its type, push into it. */
6548 else if (value != 0 && !constructor_no_implicit
6549 && value != error_mark_node
6550 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6551 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6552 || eltcode == UNION_TYPE))
6554 push_init_level (1);
6555 continue;
6558 if (constructor_max_index != 0
6559 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6560 || integer_all_onesp (constructor_max_index)))
6562 pedwarn_init ("excess elements in array initializer");
6563 break;
6566 /* Now output the actual element. */
6567 if (value)
6569 push_array_bounds (tree_low_cst (constructor_index, 0));
6570 output_init_element (value, elttype, constructor_index, 1);
6571 RESTORE_SPELLING_DEPTH (constructor_depth);
6574 constructor_index
6575 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6577 if (! value)
6578 /* If we are doing the bookkeeping for an element that was
6579 directly output as a constructor, we must update
6580 constructor_unfilled_index. */
6581 constructor_unfilled_index = constructor_index;
6583 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6585 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6587 /* Do a basic check of initializer size. Note that vectors
6588 always have a fixed size derived from their type. */
6589 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6591 pedwarn_init ("excess elements in vector initializer");
6592 break;
6595 /* Now output the actual element. */
6596 if (value)
6597 output_init_element (value, elttype, constructor_index, 1);
6599 constructor_index
6600 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6602 if (! value)
6603 /* If we are doing the bookkeeping for an element that was
6604 directly output as a constructor, we must update
6605 constructor_unfilled_index. */
6606 constructor_unfilled_index = constructor_index;
6609 /* Handle the sole element allowed in a braced initializer
6610 for a scalar variable. */
6611 else if (constructor_fields == 0)
6613 pedwarn_init ("excess elements in scalar initializer");
6614 break;
6616 else
6618 if (value)
6619 output_init_element (value, constructor_type, NULL_TREE, 1);
6620 constructor_fields = 0;
6623 /* Handle range initializers either at this level or anywhere higher
6624 in the designator stack. */
6625 if (constructor_range_stack)
6627 struct constructor_range_stack *p, *range_stack;
6628 int finish = 0;
6630 range_stack = constructor_range_stack;
6631 constructor_range_stack = 0;
6632 while (constructor_stack != range_stack->stack)
6634 if (!constructor_stack->implicit)
6635 abort ();
6636 process_init_element (pop_init_level (1));
6638 for (p = range_stack;
6639 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6640 p = p->prev)
6642 if (!constructor_stack->implicit)
6643 abort ();
6644 process_init_element (pop_init_level (1));
6647 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6648 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6649 finish = 1;
6651 while (1)
6653 constructor_index = p->index;
6654 constructor_fields = p->fields;
6655 if (finish && p->range_end && p->index == p->range_start)
6657 finish = 0;
6658 p->prev = 0;
6660 p = p->next;
6661 if (!p)
6662 break;
6663 push_init_level (2);
6664 p->stack = constructor_stack;
6665 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6666 p->index = p->range_start;
6669 if (!finish)
6670 constructor_range_stack = range_stack;
6671 continue;
6674 break;
6677 constructor_range_stack = 0;
6680 /* Build a simple asm-statement, from one string literal. */
6681 tree
6682 simple_asm_stmt (tree expr)
6684 STRIP_NOPS (expr);
6686 if (TREE_CODE (expr) == ADDR_EXPR)
6687 expr = TREE_OPERAND (expr, 0);
6689 if (TREE_CODE (expr) == STRING_CST)
6691 tree stmt;
6693 /* Simple asm statements are treated as volatile. */
6694 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6695 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6696 ASM_INPUT_P (stmt) = 1;
6697 return stmt;
6700 error ("argument of `asm' is not a constant string");
6701 return NULL_TREE;
6704 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6705 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6707 tree
6708 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6709 tree clobbers)
6711 tree tail;
6713 if (TREE_CODE (string) != STRING_CST)
6715 error ("asm template is not a string constant");
6716 return NULL_TREE;
6719 if (cv_qualifier != NULL_TREE
6720 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6722 warning ("%s qualifier ignored on asm",
6723 IDENTIFIER_POINTER (cv_qualifier));
6724 cv_qualifier = NULL_TREE;
6727 /* We can remove output conversions that change the type,
6728 but not the mode. */
6729 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6731 tree output = TREE_VALUE (tail);
6733 STRIP_NOPS (output);
6734 TREE_VALUE (tail) = output;
6736 /* Allow conversions as LHS here. build_modify_expr as called below
6737 will do the right thing with them. */
6738 while (TREE_CODE (output) == NOP_EXPR
6739 || TREE_CODE (output) == CONVERT_EXPR
6740 || TREE_CODE (output) == FLOAT_EXPR
6741 || TREE_CODE (output) == FIX_TRUNC_EXPR
6742 || TREE_CODE (output) == FIX_FLOOR_EXPR
6743 || TREE_CODE (output) == FIX_ROUND_EXPR
6744 || TREE_CODE (output) == FIX_CEIL_EXPR)
6745 output = TREE_OPERAND (output, 0);
6747 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6750 /* Remove output conversions that change the type but not the mode. */
6751 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6753 tree output = TREE_VALUE (tail);
6754 STRIP_NOPS (output);
6755 TREE_VALUE (tail) = output;
6758 /* Perform default conversions on array and function inputs.
6759 Don't do this for other types as it would screw up operands
6760 expected to be in memory. */
6761 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6762 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6764 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6765 outputs, inputs, clobbers));
6768 /* Expand an ASM statement with operands, handling output operands
6769 that are not variables or INDIRECT_REFS by transforming such
6770 cases into cases that expand_asm_operands can handle.
6772 Arguments are same as for expand_asm_operands. */
6774 void
6775 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6776 tree clobbers, int vol, const char *filename,
6777 int line)
6779 int noutputs = list_length (outputs);
6780 int i;
6781 /* o[I] is the place that output number I should be written. */
6782 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6783 tree tail;
6785 /* Record the contents of OUTPUTS before it is modified. */
6786 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6788 o[i] = TREE_VALUE (tail);
6789 if (o[i] == error_mark_node)
6790 return;
6793 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6794 OUTPUTS some trees for where the values were actually stored. */
6795 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6797 /* Copy all the intermediate outputs into the specified outputs. */
6798 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6800 if (o[i] != TREE_VALUE (tail))
6802 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6803 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6804 free_temp_slots ();
6806 /* Restore the original value so that it's correct the next
6807 time we expand this function. */
6808 TREE_VALUE (tail) = o[i];
6810 /* Detect modification of read-only values.
6811 (Otherwise done by build_modify_expr.) */
6812 else
6814 tree type = TREE_TYPE (o[i]);
6815 if (TREE_READONLY (o[i])
6816 || TYPE_READONLY (type)
6817 || ((TREE_CODE (type) == RECORD_TYPE
6818 || TREE_CODE (type) == UNION_TYPE)
6819 && C_TYPE_FIELDS_READONLY (type)))
6820 readonly_warning (o[i], "modification by `asm'");
6824 /* Those MODIFY_EXPRs could do autoincrements. */
6825 emit_queue ();
6828 /* Expand a C `return' statement.
6829 RETVAL is the expression for what to return,
6830 or a null pointer for `return;' with no value. */
6832 tree
6833 c_expand_return (tree retval)
6835 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6837 if (TREE_THIS_VOLATILE (current_function_decl))
6838 warning ("function declared `noreturn' has a `return' statement");
6840 if (!retval)
6842 current_function_returns_null = 1;
6843 if ((warn_return_type || flag_isoc99)
6844 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6845 pedwarn_c99 ("`return' with no value, in function returning non-void");
6847 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6849 current_function_returns_null = 1;
6850 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6851 pedwarn ("`return' with a value, in function returning void");
6853 else
6855 tree t = convert_for_assignment (valtype, retval, _("return"),
6856 NULL_TREE, NULL_TREE, 0);
6857 tree res = DECL_RESULT (current_function_decl);
6858 tree inner;
6860 current_function_returns_value = 1;
6861 if (t == error_mark_node)
6862 return NULL_TREE;
6864 inner = t = convert (TREE_TYPE (res), t);
6866 /* Strip any conversions, additions, and subtractions, and see if
6867 we are returning the address of a local variable. Warn if so. */
6868 while (1)
6870 switch (TREE_CODE (inner))
6872 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6873 case PLUS_EXPR:
6874 inner = TREE_OPERAND (inner, 0);
6875 continue;
6877 case MINUS_EXPR:
6878 /* If the second operand of the MINUS_EXPR has a pointer
6879 type (or is converted from it), this may be valid, so
6880 don't give a warning. */
6882 tree op1 = TREE_OPERAND (inner, 1);
6884 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6885 && (TREE_CODE (op1) == NOP_EXPR
6886 || TREE_CODE (op1) == NON_LVALUE_EXPR
6887 || TREE_CODE (op1) == CONVERT_EXPR))
6888 op1 = TREE_OPERAND (op1, 0);
6890 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6891 break;
6893 inner = TREE_OPERAND (inner, 0);
6894 continue;
6897 case ADDR_EXPR:
6898 inner = TREE_OPERAND (inner, 0);
6900 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6901 inner = TREE_OPERAND (inner, 0);
6903 if (TREE_CODE (inner) == VAR_DECL
6904 && ! DECL_EXTERNAL (inner)
6905 && ! TREE_STATIC (inner)
6906 && DECL_CONTEXT (inner) == current_function_decl)
6907 warning ("function returns address of local variable");
6908 break;
6910 default:
6911 break;
6914 break;
6917 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6920 return add_stmt (build_return_stmt (retval));
6923 struct c_switch {
6924 /* The SWITCH_STMT being built. */
6925 tree switch_stmt;
6926 /* A splay-tree mapping the low element of a case range to the high
6927 element, or NULL_TREE if there is no high element. Used to
6928 determine whether or not a new case label duplicates an old case
6929 label. We need a tree, rather than simply a hash table, because
6930 of the GNU case range extension. */
6931 splay_tree cases;
6932 /* The next node on the stack. */
6933 struct c_switch *next;
6936 /* A stack of the currently active switch statements. The innermost
6937 switch statement is on the top of the stack. There is no need to
6938 mark the stack for garbage collection because it is only active
6939 during the processing of the body of a function, and we never
6940 collect at that point. */
6942 static struct c_switch *switch_stack;
6944 /* Start a C switch statement, testing expression EXP. Return the new
6945 SWITCH_STMT. */
6947 tree
6948 c_start_case (tree exp)
6950 enum tree_code code;
6951 tree type, orig_type = error_mark_node;
6952 struct c_switch *cs;
6954 if (exp != error_mark_node)
6956 code = TREE_CODE (TREE_TYPE (exp));
6957 orig_type = TREE_TYPE (exp);
6959 if (! INTEGRAL_TYPE_P (orig_type)
6960 && code != ERROR_MARK)
6962 error ("switch quantity not an integer");
6963 exp = integer_zero_node;
6965 else
6967 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6969 if (warn_traditional && !in_system_header
6970 && (type == long_integer_type_node
6971 || type == long_unsigned_type_node))
6972 warning ("`long' switch expression not converted to `int' in ISO C");
6974 exp = default_conversion (exp);
6975 type = TREE_TYPE (exp);
6979 /* Add this new SWITCH_STMT to the stack. */
6980 cs = (struct c_switch *) xmalloc (sizeof (*cs));
6981 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6982 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6983 cs->next = switch_stack;
6984 switch_stack = cs;
6986 return add_stmt (switch_stack->switch_stmt);
6989 /* Process a case label. */
6991 tree
6992 do_case (tree low_value, tree high_value)
6994 tree label = NULL_TREE;
6996 if (switch_stack)
6998 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7000 label = c_add_case_label (switch_stack->cases,
7001 SWITCH_COND (switch_stack->switch_stmt),
7002 low_value, high_value);
7003 if (label == error_mark_node)
7004 label = NULL_TREE;
7005 else if (switch_was_empty_p)
7007 /* Attach the first case label to the SWITCH_BODY. */
7008 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7009 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7012 else if (low_value)
7013 error ("case label not within a switch statement");
7014 else
7015 error ("`default' label not within a switch statement");
7017 return label;
7020 /* Finish the switch statement. */
7022 void
7023 c_finish_case (void)
7025 struct c_switch *cs = switch_stack;
7027 /* Rechain the next statements to the SWITCH_STMT. */
7028 last_tree = cs->switch_stmt;
7030 /* Pop the stack. */
7031 switch_stack = switch_stack->next;
7032 splay_tree_delete (cs->cases);
7033 free (cs);