]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
Daily bump.
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
06ceef4e 2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
2f89bbc1 3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
400fbf9f 4
1322177d 5This file is part of GCC.
400fbf9f 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
400fbf9f 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
400fbf9f
JW
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
400fbf9f
JW
21
22
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.
27
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). */
31
32#include "config.h"
670ee920 33#include "system.h"
4977bab6
ZW
34#include "coretypes.h"
35#include "tm.h"
742b62e7 36#include "rtl.h"
400fbf9f 37#include "tree.h"
e57e265b 38#include "langhooks.h"
400fbf9f 39#include "c-tree.h"
6baf1cc8 40#include "tm_p.h"
400fbf9f 41#include "flags.h"
e14417fa 42#include "output.h"
234042f4 43#include "expr.h"
5f6da302 44#include "toplev.h"
ab87f8c8 45#include "intl.h"
4dd7201e 46#include "ggc.h"
672a6f42 47#include "target.h"
400fbf9f 48
b71c7f8a 49/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 50 message within this initializer. */
b71c7f8a 51static int missing_braces_mentioned;
103b7b17 52
f55ade6e 53static tree qualify_type (tree, tree);
d1bd0ded 54static int tagged_types_tu_compatible_p (tree, tree, int);
f55ade6e 55static int comp_target_types (tree, tree, int);
d1bd0ded
GK
56static int function_types_compatible_p (tree, tree, int);
57static int type_lists_compatible_p (tree, tree, int);
f55ade6e
AJ
58static tree decl_constant_value_for_broken_optimization (tree);
59static tree default_function_array_conversion (tree);
60static tree lookup_field (tree, tree);
f55ade6e
AJ
61static tree convert_arguments (tree, tree, tree, tree);
62static tree pointer_diff (tree, tree);
f55ade6e
AJ
63static tree internal_build_compound_expr (tree, int);
64static tree convert_for_assignment (tree, tree, const char *, tree, tree,
65 int);
66static void warn_for_assignment (const char *, const char *, tree, int);
67static tree valid_compound_expr_initializer (tree, tree);
68static void push_string (const char *);
69static void push_member_name (tree);
70static void push_array_bounds (int);
71static int spelling_length (void);
72static char *print_spelling (char *);
73static void warning_init (const char *);
74static tree digest_init (tree, tree, int);
75static void output_init_element (tree, tree, tree, int);
76static void output_pending_init_elements (int);
77static int set_designator (int);
78static void push_range_stack (tree);
79static void add_pending_init (tree, tree);
80static void set_nonincremental_init (void);
81static void set_nonincremental_init_from_string (tree);
82static tree find_init_member (tree);
400fbf9f
JW
83\f
84/* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
86
87tree
2f6e4e97 88require_complete_type (tree value)
400fbf9f
JW
89{
90 tree type = TREE_TYPE (value);
91
c3d5c3fa 92 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
93 return error_mark_node;
94
400fbf9f 95 /* First, detect a valid value with a complete type. */
d0f062fb 96 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
97 return value;
98
7a228918 99 c_incomplete_type_error (value, type);
400fbf9f
JW
100 return error_mark_node;
101}
102
103/* Print an error message for invalid use of an incomplete type.
104 VALUE is the expression that was used (or 0 if that isn't known)
105 and TYPE is the type that was invalid. */
106
107void
2f6e4e97 108c_incomplete_type_error (tree value, tree type)
400fbf9f 109{
5d5993dd 110 const char *type_code_string;
400fbf9f
JW
111
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
114 return;
115
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
120 else
121 {
122 retry:
123 /* We must print an error message. Be clever about what it says. */
124
125 switch (TREE_CODE (type))
126 {
127 case RECORD_TYPE:
ab87f8c8 128 type_code_string = "struct";
400fbf9f
JW
129 break;
130
131 case UNION_TYPE:
ab87f8c8 132 type_code_string = "union";
400fbf9f
JW
133 break;
134
135 case ENUMERAL_TYPE:
ab87f8c8 136 type_code_string = "enum";
400fbf9f
JW
137 break;
138
139 case VOID_TYPE:
140 error ("invalid use of void expression");
141 return;
142
143 case ARRAY_TYPE:
144 if (TYPE_DOMAIN (type))
145 {
fba78abb
RH
146 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
147 {
148 error ("invalid use of flexible array member");
149 return;
150 }
400fbf9f
JW
151 type = TREE_TYPE (type);
152 goto retry;
153 }
154 error ("invalid use of array with unspecified bounds");
155 return;
156
157 default:
158 abort ();
159 }
160
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ab87f8c8
JL
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
400fbf9f
JW
164 else
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
168 }
169}
170
ab393bf1
NB
171/* Given a type, apply default promotions wrt unnamed function
172 arguments and return the new type. */
173
174tree
2f6e4e97 175c_type_promotes_to (tree type)
ab393bf1
NB
176{
177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
178 return double_type_node;
179
180 if (c_promoting_integer_type_p (type))
181 {
182 /* Preserve unsignedness if not really getting any wider. */
8df83eae 183 if (TYPE_UNSIGNED (type)
ab393bf1
NB
184 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
185 return unsigned_type_node;
186 return integer_type_node;
187 }
188
189 return type;
190}
191
400fbf9f
JW
192/* Return a variant of TYPE which has all the type qualifiers of LIKE
193 as well as those of TYPE. */
194
195static tree
2f6e4e97 196qualify_type (tree type, tree like)
400fbf9f 197{
2f6e4e97 198 return c_build_qualified_type (type,
afbadaa7 199 TYPE_QUALS (type) | TYPE_QUALS (like));
400fbf9f
JW
200}
201\f
202/* Return the common type of two types.
203 We assume that comptypes has already been done and returned 1;
6cb72a7d
RS
204 if that isn't so, this may crash. In particular, we assume that qualifiers
205 match.
400fbf9f
JW
206
207 This is the type for the result of most arithmetic operations
6cb72a7d 208 if the operands have the given two types. */
400fbf9f
JW
209
210tree
2f6e4e97 211common_type (tree t1, tree t2)
400fbf9f 212{
b3694847
SS
213 enum tree_code code1;
214 enum tree_code code2;
4b027d16 215 tree attributes;
400fbf9f
JW
216
217 /* Save time if the two types are the same. */
218
219 if (t1 == t2) return t1;
220
221 /* If one type is nonsense, use the other. */
222 if (t1 == error_mark_node)
223 return t2;
224 if (t2 == error_mark_node)
225 return t1;
226
d9525bec 227 /* Merge the attributes. */
5fd9b178 228 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 229
400fbf9f
JW
230 /* Treat an enum type as the unsigned integer type of the same width. */
231
232 if (TREE_CODE (t1) == ENUMERAL_TYPE)
b0c48229 233 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
400fbf9f 234 if (TREE_CODE (t2) == ENUMERAL_TYPE)
b0c48229 235 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
400fbf9f
JW
236
237 code1 = TREE_CODE (t1);
238 code2 = TREE_CODE (t2);
239
75326e8c
RK
240 /* If one type is complex, form the common type of the non-complex
241 components, then make that complex. Use T1 or T2 if it is the
242 required type. */
b6a10c9f
RS
243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
244 {
75326e8c
RK
245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
247 tree subtype = common_type (subtype1, subtype2);
248
249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
4b027d16 250 return build_type_attribute_variant (t1, attributes);
75326e8c 251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
4b027d16 252 return build_type_attribute_variant (t2, attributes);
b6a10c9f 253 else
4b027d16
RK
254 return build_type_attribute_variant (build_complex_type (subtype),
255 attributes);
b6a10c9f
RS
256 }
257
400fbf9f
JW
258 switch (code1)
259 {
260 case INTEGER_TYPE:
261 case REAL_TYPE:
262 /* If only one is real, use it as the result. */
263
264 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
4b027d16 265 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
266
267 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
4b027d16 268 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
269
270 /* Both real or both integers; use the one with greater precision. */
271
272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
4b027d16 273 return build_type_attribute_variant (t1, attributes);
400fbf9f 274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
4b027d16 275 return build_type_attribute_variant (t2, attributes);
400fbf9f 276
c7e1876b
JM
277 /* Same precision. Prefer long longs to longs to ints when the
278 same precision, following the C99 rules on integer type rank
279 (which are equivalent to the C90 rules for C90 types). */
280
281 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
283 return build_type_attribute_variant (long_long_unsigned_type_node,
284 attributes);
285
286 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
288 {
289 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
290 t1 = long_long_unsigned_type_node;
291 else
292 t1 = long_long_integer_type_node;
293 return build_type_attribute_variant (t1, attributes);
294 }
400fbf9f 295
36618528
RS
296 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
297 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
4b027d16
RK
298 return build_type_attribute_variant (long_unsigned_type_node,
299 attributes);
400fbf9f 300
36618528
RS
301 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
302 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
400fbf9f
JW
303 {
304 /* But preserve unsignedness from the other type,
305 since long cannot hold all the values of an unsigned int. */
8df83eae 306 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
4b027d16
RK
307 t1 = long_unsigned_type_node;
308 else
309 t1 = long_integer_type_node;
310 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
311 }
312
e9a25f70
JL
313 /* Likewise, prefer long double to double even if same size. */
314 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
315 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
316 return build_type_attribute_variant (long_double_type_node,
317 attributes);
318
400fbf9f
JW
319 /* Otherwise prefer the unsigned one. */
320
8df83eae 321 if (TYPE_UNSIGNED (t1))
4b027d16
RK
322 return build_type_attribute_variant (t1, attributes);
323 else
324 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
325
326 case POINTER_TYPE:
400fbf9f
JW
327 /* For two pointers, do this recursively on the target type,
328 and combine the qualifiers of the two types' targets. */
8706edbc
RS
329 /* This code was turned off; I don't know why.
330 But ANSI C specifies doing this with the qualifiers.
331 So I turned it on again. */
400fbf9f 332 {
3932261a
MM
333 tree pointed_to_1 = TREE_TYPE (t1);
334 tree pointed_to_2 = TREE_TYPE (t2);
335 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
336 TYPE_MAIN_VARIANT (pointed_to_2));
2f6e4e97
AJ
337 t1 = build_pointer_type (c_build_qualified_type
338 (target,
339 TYPE_QUALS (pointed_to_1) |
3932261a 340 TYPE_QUALS (pointed_to_2)));
4b027d16 341 return build_type_attribute_variant (t1, attributes);
400fbf9f 342 }
400fbf9f
JW
343
344 case ARRAY_TYPE:
345 {
346 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
347 /* Save space: see if the result is identical to one of the args. */
348 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
4b027d16 349 return build_type_attribute_variant (t1, attributes);
400fbf9f 350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
4b027d16 351 return build_type_attribute_variant (t2, attributes);
400fbf9f 352 /* Merge the element types, and have a size if either arg has one. */
4b027d16
RK
353 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
354 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
355 }
356
357 case FUNCTION_TYPE:
358 /* Function types: prefer the one that specified arg types.
359 If both do, merge the arg types. Also merge the return types. */
360 {
361 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
362 tree p1 = TYPE_ARG_TYPES (t1);
363 tree p2 = TYPE_ARG_TYPES (t2);
364 int len;
365 tree newargs, n;
366 int i;
367
368 /* Save space: see if the result is identical to one of the args. */
369 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
4b027d16 370 return build_type_attribute_variant (t1, attributes);
400fbf9f 371 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
4b027d16 372 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
373
374 /* Simple way if one arg fails to specify argument types. */
375 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16
RK
376 {
377 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
378 return build_type_attribute_variant (t1, attributes);
379 }
400fbf9f 380 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
381 {
382 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
383 return build_type_attribute_variant (t1, attributes);
384 }
400fbf9f
JW
385
386 /* If both args specify argument types, we must merge the two
387 lists, argument by argument. */
f75fbaf7
ZW
388 /* Tell global_bindings_p to return false so that variable_size
389 doesn't abort on VLAs in parameter types. */
390 c_override_global_bindings_to_false = true;
2f4e8f2b 391
400fbf9f
JW
392 len = list_length (p1);
393 newargs = 0;
394
395 for (i = 0; i < len; i++)
8d9bfdc5 396 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
397
398 n = newargs;
399
400 for (; p1;
401 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
402 {
403 /* A null type means arg type is not specified.
404 Take whatever the other function type has. */
405 if (TREE_VALUE (p1) == 0)
406 {
407 TREE_VALUE (n) = TREE_VALUE (p2);
408 goto parm_done;
409 }
410 if (TREE_VALUE (p2) == 0)
411 {
412 TREE_VALUE (n) = TREE_VALUE (p1);
413 goto parm_done;
414 }
2f6e4e97 415
400fbf9f
JW
416 /* Given wait (union {union wait *u; int *i} *)
417 and wait (union wait *),
418 prefer union wait * as type of parm. */
419 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
420 && TREE_VALUE (p1) != TREE_VALUE (p2))
421 {
422 tree memb;
423 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
424 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
425 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
426 COMPARE_STRICT))
400fbf9f
JW
427 {
428 TREE_VALUE (n) = TREE_VALUE (p2);
429 if (pedantic)
89abf8d1 430 pedwarn ("function types not truly compatible in ISO C");
400fbf9f
JW
431 goto parm_done;
432 }
433 }
434 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
435 && TREE_VALUE (p2) != TREE_VALUE (p1))
436 {
437 tree memb;
438 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
439 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
440 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
441 COMPARE_STRICT))
400fbf9f
JW
442 {
443 TREE_VALUE (n) = TREE_VALUE (p1);
444 if (pedantic)
89abf8d1 445 pedwarn ("function types not truly compatible in ISO C");
400fbf9f
JW
446 goto parm_done;
447 }
448 }
449 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
450 parm_done: ;
451 }
452
f75fbaf7 453 c_override_global_bindings_to_false = false;
4b027d16 454 t1 = build_function_type (valtype, newargs);
0f41302f 455 /* ... falls through ... */
400fbf9f
JW
456 }
457
458 default:
4b027d16 459 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
460 }
461
462}
463\f
464/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
465 or various other operations. Return 2 if they are compatible
466 but a warning may be needed if you use them together. */
467
468int
d1bd0ded 469comptypes (tree type1, tree type2, int flags)
400fbf9f 470{
b3694847
SS
471 tree t1 = type1;
472 tree t2 = type2;
4b027d16 473 int attrval, val;
400fbf9f
JW
474
475 /* Suppress errors caused by previously reported errors. */
476
8d47dfc5
RH
477 if (t1 == t2 || !t1 || !t2
478 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
479 return 1;
480
21318741
RK
481 /* If either type is the internal version of sizetype, return the
482 language version. */
483 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
eb1a2c88
DN
484 && TYPE_ORIG_SIZE_TYPE (t1))
485 t1 = TYPE_ORIG_SIZE_TYPE (t1);
21318741
RK
486
487 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
eb1a2c88
DN
488 && TYPE_ORIG_SIZE_TYPE (t2))
489 t2 = TYPE_ORIG_SIZE_TYPE (t2);
490
21318741 491
bca63328
JM
492 /* Enumerated types are compatible with integer types, but this is
493 not transitive: two enumerated types in the same translation unit
494 are compatible with each other only if they are the same type. */
400fbf9f 495
bca63328 496 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
8df83eae 497 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
bca63328 498 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
8df83eae 499 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
400fbf9f
JW
500
501 if (t1 == t2)
502 return 1;
503
504 /* Different classes of types can't be compatible. */
505
506 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
507
508 /* Qualifiers must match. */
509
3932261a 510 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
511 return 0;
512
08632da2
RS
513 /* Allow for two different type nodes which have essentially the same
514 definition. Note that we already checked for equality of the type
38e01259 515 qualifiers (just above). */
400fbf9f
JW
516
517 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
518 return 1;
519
4b027d16 520 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
5fd9b178 521 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
4b027d16
RK
522 return 0;
523
524 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
525 val = 0;
526
400fbf9f
JW
527 switch (TREE_CODE (t1))
528 {
529 case POINTER_TYPE:
264fa2db
ZL
530 /* We must give ObjC the first crack at comparing pointers, since
531 protocol qualifiers may be involved. */
532 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
533 break;
4b027d16 534 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
264fa2db 535 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
4b027d16 536 break;
400fbf9f
JW
537
538 case FUNCTION_TYPE:
d1bd0ded 539 val = function_types_compatible_p (t1, t2, flags);
4b027d16 540 break;
400fbf9f
JW
541
542 case ARRAY_TYPE:
543 {
400fbf9f
JW
544 tree d1 = TYPE_DOMAIN (t1);
545 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
546 bool d1_variable, d2_variable;
547 bool d1_zero, d2_zero;
4b027d16 548 val = 1;
400fbf9f
JW
549
550 /* Target types must match incl. qualifiers. */
551 if (TREE_TYPE (t1) != TREE_TYPE (t2)
d1bd0ded
GK
552 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
553 flags)))
400fbf9f
JW
554 return 0;
555
556 /* Sizes must match unless one is missing or variable. */
3f85558f 557 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 558 break;
400fbf9f 559
3f85558f
RH
560 d1_zero = ! TYPE_MAX_VALUE (d1);
561 d2_zero = ! TYPE_MAX_VALUE (d2);
562
563 d1_variable = (! d1_zero
564 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
565 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
566 d2_variable = (! d2_zero
567 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
568 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
569
570 if (d1_variable || d2_variable)
571 break;
572 if (d1_zero && d2_zero)
573 break;
574 if (d1_zero || d2_zero
575 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
05bccae2
RK
576 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
577 val = 0;
578
4b027d16 579 break;
400fbf9f
JW
580 }
581
582 case RECORD_TYPE:
264fa2db
ZL
583 /* We are dealing with two distinct structs. In assorted Objective-C
584 corner cases, however, these can still be deemed equivalent. */
37fa72e9 585 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
4b027d16 586 val = 1;
d1bd0ded
GK
587
588 case ENUMERAL_TYPE:
589 case UNION_TYPE:
766beae1 590 if (val != 1 && !same_translation_unit_p (t1, t2))
d1bd0ded 591 val = tagged_types_tu_compatible_p (t1, t2, flags);
4b027d16 592 break;
e9a25f70 593
62e1dfcf 594 case VECTOR_TYPE:
cc27e657
PB
595 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
596 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), 0);
62e1dfcf
NC
597 break;
598
e9a25f70
JL
599 default:
600 break;
400fbf9f 601 }
4b027d16 602 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
603}
604
605/* Return 1 if TTL and TTR are pointers to types that are equivalent,
1074d9d4
NP
606 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
607 to 1 or 0 depending if the check of the pointer types is meant to
608 be reflexive or not (typically, assignments are not reflexive,
609 while comparisons are reflexive).
610*/
400fbf9f
JW
611
612static int
2f6e4e97 613comp_target_types (tree ttl, tree ttr, int reflexive)
400fbf9f 614{
392202b0 615 int val;
8b40563c 616
b50d021d 617 /* Give objc_comptypes a crack at letting these types through. */
1074d9d4 618 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
392202b0 619 return val;
8b40563c 620
392202b0 621 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
d1bd0ded 622 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
8b40563c 623
400fbf9f
JW
624 if (val == 2 && pedantic)
625 pedwarn ("types are not quite compatible");
626 return val;
627}
628\f
629/* Subroutines of `comptypes'. */
630
f75fbaf7
ZW
631/* Determine whether two trees derive from the same translation unit.
632 If the CONTEXT chain ends in a null, that tree's context is still
633 being parsed, so if two trees have context chains ending in null,
766beae1 634 they're in the same translation unit. */
f75fbaf7 635int
766beae1
ZW
636same_translation_unit_p (tree t1, tree t2)
637{
638 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
639 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
640 {
641 case 'd': t1 = DECL_CONTEXT (t1); break;
642 case 't': t1 = TYPE_CONTEXT (t1); break;
643 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
644 default: abort ();
645 }
646
647 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
648 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
649 {
63185fab 650 case 'd': t2 = DECL_CONTEXT (t2); break;
766beae1
ZW
651 case 't': t2 = TYPE_CONTEXT (t2); break;
652 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
653 default: abort ();
654 }
655
656 return t1 == t2;
657}
658
d1bd0ded
GK
659/* The C standard says that two structures in different translation
660 units are compatible with each other only if the types of their
661 fields are compatible (among other things). So, consider two copies
662 of this structure: */
663
664struct tagged_tu_seen {
665 const struct tagged_tu_seen * next;
666 tree t1;
667 tree t2;
668};
669
670/* Can they be compatible with each other? We choose to break the
671 recursion by allowing those types to be compatible. */
672
673static const struct tagged_tu_seen * tagged_tu_seen_base;
674
675/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
676 compatible. If the two types are not the same (which has been
677 checked earlier), this can only happen when multiple translation
678 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
679 rules. */
680
681static int
682tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
683{
684 tree s1, s2;
685 bool needs_warning = false;
686
687 /* We have to verify that the tags of the types are the same. This
688 is harder than it looks because this may be a typedef, so we have
689 to go look at the original type. It may even be a typedef of a
4ed43216 690 typedef... */
dea984dc
ILT
691 while (TYPE_NAME (t1)
692 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
693 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
694 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
695
dea984dc
ILT
696 while (TYPE_NAME (t2)
697 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
698 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
699 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
700
701 /* C90 didn't have the requirement that the two tags be the same. */
702 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
703 return 0;
704
705 /* C90 didn't say what happened if one or both of the types were
706 incomplete; we choose to follow C99 rules here, which is that they
707 are compatible. */
708 if (TYPE_SIZE (t1) == NULL
709 || TYPE_SIZE (t2) == NULL)
710 return 1;
711
712 {
713 const struct tagged_tu_seen * tts_i;
714 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
715 if (tts_i->t1 == t1 && tts_i->t2 == t2)
716 return 1;
717 }
718
719 switch (TREE_CODE (t1))
720 {
721 case ENUMERAL_TYPE:
722 {
dedbabed 723
71cc389b 724 /* Speed up the case where the type values are in the same order. */
dedbabed
AP
725 tree tv1 = TYPE_VALUES (t1);
726 tree tv2 = TYPE_VALUES (t2);
727
728 if (tv1 == tv2)
729 return 1;
730
f38f747d 731 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
dedbabed 732 {
8cd6bdd1 733 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
dedbabed
AP
734 break;
735 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
736 return 0;
737 }
738
739 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
740 return 1;
741 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
742 return 0;
743
d1bd0ded
GK
744 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
745 return 0;
746
747 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
748 {
749 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
750 if (s2 == NULL
751 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
752 return 0;
753 }
754 return 1;
755 }
756
757 case UNION_TYPE:
758 {
759 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
760 return 0;
761
762 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
763 {
764 bool ok = false;
765 struct tagged_tu_seen tts;
766
767 tts.next = tagged_tu_seen_base;
768 tts.t1 = t1;
769 tts.t2 = t2;
770 tagged_tu_seen_base = &tts;
771
772 if (DECL_NAME (s1) != NULL)
773 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
774 if (DECL_NAME (s1) == DECL_NAME (s2))
775 {
776 int result;
777 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
778 if (result == 0)
779 break;
780 if (result == 2)
781 needs_warning = true;
782
783 if (TREE_CODE (s1) == FIELD_DECL
784 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
785 DECL_FIELD_BIT_OFFSET (s2)) != 1)
786 break;
787
788 ok = true;
789 break;
790 }
791 tagged_tu_seen_base = tts.next;
792 if (! ok)
793 return 0;
794 }
795 return needs_warning ? 2 : 1;
796 }
797
798 case RECORD_TYPE:
799 {
800 struct tagged_tu_seen tts;
801
802 tts.next = tagged_tu_seen_base;
803 tts.t1 = t1;
804 tts.t2 = t2;
805 tagged_tu_seen_base = &tts;
806
807 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
808 s1 && s2;
809 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
810 {
811 int result;
812 if (TREE_CODE (s1) != TREE_CODE (s2)
813 || DECL_NAME (s1) != DECL_NAME (s2))
814 break;
815 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
816 if (result == 0)
817 break;
818 if (result == 2)
819 needs_warning = true;
820
821 if (TREE_CODE (s1) == FIELD_DECL
822 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
823 DECL_FIELD_BIT_OFFSET (s2)) != 1)
824 break;
825 }
826 tagged_tu_seen_base = tts.next;
827 if (s1 && s2)
828 return 0;
829 return needs_warning ? 2 : 1;
830 }
831
832 default:
833 abort ();
834 }
835}
836
400fbf9f
JW
837/* Return 1 if two function types F1 and F2 are compatible.
838 If either type specifies no argument types,
839 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 840 Otherwise, if one type specifies only the number of arguments,
400fbf9f
JW
841 the other must specify that number of self-promoting arg types.
842 Otherwise, the argument types must match. */
843
844static int
d1bd0ded 845function_types_compatible_p (tree f1, tree f2, int flags)
400fbf9f
JW
846{
847 tree args1, args2;
848 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
849 int val = 1;
850 int val1;
a6fdc086
GK
851 tree ret1, ret2;
852
853 ret1 = TREE_TYPE (f1);
854 ret2 = TREE_TYPE (f2);
855
856 /* 'volatile' qualifiers on a function's return type mean the function
857 is noreturn. */
858 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
859 pedwarn ("function return types not compatible due to `volatile'");
860 if (TYPE_VOLATILE (ret1))
861 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
862 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
863 if (TYPE_VOLATILE (ret2))
864 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
865 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
d1bd0ded 866 val = comptypes (ret1, ret2, flags);
a6fdc086 867 if (val == 0)
400fbf9f
JW
868 return 0;
869
870 args1 = TYPE_ARG_TYPES (f1);
871 args2 = TYPE_ARG_TYPES (f2);
872
873 /* An unspecified parmlist matches any specified parmlist
874 whose argument types don't need default promotions. */
875
876 if (args1 == 0)
877 {
878 if (!self_promoting_args_p (args2))
879 return 0;
880 /* If one of these types comes from a non-prototype fn definition,
881 compare that with the other type's arglist.
882 If they don't match, ask for a warning (but no error). */
883 if (TYPE_ACTUAL_ARG_TYPES (f1)
d1bd0ded
GK
884 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
885 flags))
400fbf9f
JW
886 val = 2;
887 return val;
888 }
889 if (args2 == 0)
890 {
891 if (!self_promoting_args_p (args1))
892 return 0;
893 if (TYPE_ACTUAL_ARG_TYPES (f2)
d1bd0ded
GK
894 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
895 flags))
400fbf9f
JW
896 val = 2;
897 return val;
898 }
899
900 /* Both types have argument lists: compare them and propagate results. */
d1bd0ded 901 val1 = type_lists_compatible_p (args1, args2, flags);
400fbf9f
JW
902 return val1 != 1 ? val1 : val;
903}
904
905/* Check two lists of types for compatibility,
906 returning 0 for incompatible, 1 for compatible,
907 or 2 for compatible with warning. */
908
909static int
d1bd0ded 910type_lists_compatible_p (tree args1, tree args2, int flags)
400fbf9f
JW
911{
912 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
913 int val = 1;
9d5f3e49 914 int newval = 0;
400fbf9f
JW
915
916 while (1)
917 {
918 if (args1 == 0 && args2 == 0)
919 return val;
920 /* If one list is shorter than the other,
921 they fail to match. */
922 if (args1 == 0 || args2 == 0)
923 return 0;
924 /* A null pointer instead of a type
925 means there is supposed to be an argument
926 but nothing is specified about what type it has.
927 So match anything that self-promotes. */
928 if (TREE_VALUE (args1) == 0)
929 {
ab393bf1 930 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
400fbf9f
JW
931 return 0;
932 }
933 else if (TREE_VALUE (args2) == 0)
934 {
ab393bf1 935 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
400fbf9f
JW
936 return 0;
937 }
8f5b6d29
SB
938 /* If one of the lists has an error marker, ignore this arg. */
939 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
940 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
941 ;
2f6e4e97 942 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
d1bd0ded
GK
943 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
944 flags)))
400fbf9f
JW
945 {
946 /* Allow wait (union {union wait *u; int *i} *)
947 and wait (union wait *) to be compatible. */
948 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
ea3373cd
RK
949 && (TYPE_NAME (TREE_VALUE (args1)) == 0
950 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
400fbf9f
JW
951 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
952 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
953 TYPE_SIZE (TREE_VALUE (args2))))
954 {
955 tree memb;
956 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
957 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
958 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
959 flags))
400fbf9f
JW
960 break;
961 if (memb == 0)
962 return 0;
963 }
964 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
ea3373cd
RK
965 && (TYPE_NAME (TREE_VALUE (args2)) == 0
966 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
400fbf9f
JW
967 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
968 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
969 TYPE_SIZE (TREE_VALUE (args1))))
970 {
971 tree memb;
972 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
973 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
974 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
975 flags))
400fbf9f
JW
976 break;
977 if (memb == 0)
978 return 0;
979 }
980 else
981 return 0;
982 }
983
984 /* comptypes said ok, but record if it said to warn. */
985 if (newval > val)
986 val = newval;
987
988 args1 = TREE_CHAIN (args1);
989 args2 = TREE_CHAIN (args2);
990 }
991}
400fbf9f 992\f
400fbf9f
JW
993/* Compute the size to increment a pointer by. */
994
995tree
2f6e4e97 996c_size_in_bytes (tree type)
400fbf9f
JW
997{
998 enum tree_code code = TREE_CODE (type);
999
fed3cef0
RK
1000 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1001 return size_one_node;
1002
d0f062fb 1003 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1004 {
1005 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1006 return size_one_node;
400fbf9f
JW
1007 }
1008
1009 /* Convert in case a char is more than one unit. */
fed3cef0
RK
1010 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1011 size_int (TYPE_PRECISION (char_type_node)
1012 / BITS_PER_UNIT));
400fbf9f 1013}
400fbf9f 1014\f
400fbf9f
JW
1015/* Return either DECL or its known constant value (if it has one). */
1016
56cb9733 1017tree
2f6e4e97 1018decl_constant_value (tree decl)
400fbf9f 1019{
a7c1916a 1020 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1021 in a place where a variable is invalid. Note that DECL_INITIAL
1022 isn't valid for a PARM_DECL. */
a7c1916a 1023 current_function_decl != 0
4f976745 1024 && TREE_CODE (decl) != PARM_DECL
400fbf9f 1025 && ! TREE_THIS_VOLATILE (decl)
83bab8db 1026 && TREE_READONLY (decl)
400fbf9f
JW
1027 && DECL_INITIAL (decl) != 0
1028 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1029 /* This is invalid if initial value is not constant.
1030 If it has either a function call, a memory reference,
1031 or a variable, then re-evaluating it could give different results. */
1032 && TREE_CONSTANT (DECL_INITIAL (decl))
1033 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1034 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1035 return DECL_INITIAL (decl);
1036 return decl;
1037}
1038
2f74f7e9
JM
1039/* Return either DECL or its known constant value (if it has one), but
1040 return DECL if pedantic or DECL has mode BLKmode. This is for
1041 bug-compatibility with the old behavior of decl_constant_value
1042 (before GCC 3.0); every use of this function is a bug and it should
1043 be removed before GCC 3.1. It is not appropriate to use pedantic
1044 in a way that affects optimization, and BLKmode is probably not the
1045 right test for avoiding misoptimizations either. */
1046
1047static tree
2f6e4e97 1048decl_constant_value_for_broken_optimization (tree decl)
2f74f7e9
JM
1049{
1050 if (pedantic || DECL_MODE (decl) == BLKmode)
1051 return decl;
1052 else
1053 return decl_constant_value (decl);
1054}
1055
207bf485
JM
1056
1057/* Perform the default conversion of arrays and functions to pointers.
1058 Return the result of converting EXP. For any other expression, just
1059 return EXP. */
1060
1061static tree
2f6e4e97 1062default_function_array_conversion (tree exp)
207bf485
JM
1063{
1064 tree orig_exp;
1065 tree type = TREE_TYPE (exp);
1066 enum tree_code code = TREE_CODE (type);
1067 int not_lvalue = 0;
1068
1069 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
2f6e4e97 1070 an lvalue.
207bf485
JM
1071
1072 Do not use STRIP_NOPS here! It will remove conversions from pointer
1073 to integer and cause infinite recursion. */
1074 orig_exp = exp;
1075 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1076 || (TREE_CODE (exp) == NOP_EXPR
1077 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1078 {
1079 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1080 not_lvalue = 1;
1081 exp = TREE_OPERAND (exp, 0);
1082 }
1083
1084 /* Preserve the original expression code. */
1085 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1086 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1087
1088 if (code == FUNCTION_TYPE)
1089 {
1090 return build_unary_op (ADDR_EXPR, exp, 0);
1091 }
1092 if (code == ARRAY_TYPE)
1093 {
1094 tree adr;
1095 tree restype = TREE_TYPE (type);
1096 tree ptrtype;
1097 int constp = 0;
1098 int volatilep = 0;
1099 int lvalue_array_p;
1100
1101 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1102 {
1103 constp = TREE_READONLY (exp);
1104 volatilep = TREE_THIS_VOLATILE (exp);
1105 }
1106
1107 if (TYPE_QUALS (type) || constp || volatilep)
2f6e4e97 1108 restype
207bf485 1109 = c_build_qualified_type (restype,
2f6e4e97 1110 TYPE_QUALS (type)
207bf485
JM
1111 | (constp * TYPE_QUAL_CONST)
1112 | (volatilep * TYPE_QUAL_VOLATILE));
1113
1114 if (TREE_CODE (exp) == INDIRECT_REF)
1115 return convert (TYPE_POINTER_TO (restype),
1116 TREE_OPERAND (exp, 0));
1117
1118 if (TREE_CODE (exp) == COMPOUND_EXPR)
1119 {
1120 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1121 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1122 TREE_OPERAND (exp, 0), op1);
1123 }
1124
1125 lvalue_array_p = !not_lvalue && lvalue_p (exp);
db3acfa5 1126 if (!flag_isoc99 && !lvalue_array_p)
207bf485
JM
1127 {
1128 /* Before C99, non-lvalue arrays do not decay to pointers.
1129 Normally, using such an array would be invalid; but it can
1130 be used correctly inside sizeof or as a statement expression.
1131 Thus, do not give an error here; an error will result later. */
1132 return exp;
1133 }
1134
1135 ptrtype = build_pointer_type (restype);
1136
1137 if (TREE_CODE (exp) == VAR_DECL)
1138 {
1139 /* ??? This is not really quite correct
1140 in that the type of the operand of ADDR_EXPR
1141 is not the target type of the type of the ADDR_EXPR itself.
1142 Question is, can this lossage be avoided? */
1143 adr = build1 (ADDR_EXPR, ptrtype, exp);
dffd7eb6 1144 if (!c_mark_addressable (exp))
207bf485
JM
1145 return error_mark_node;
1146 TREE_CONSTANT (adr) = staticp (exp);
1147 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1148 return adr;
1149 }
1150 /* This way is better for a COMPONENT_REF since it can
1151 simplify the offset for a component. */
1152 adr = build_unary_op (ADDR_EXPR, exp, 1);
1153 return convert (ptrtype, adr);
1154 }
1155 return exp;
1156}
1157
400fbf9f
JW
1158/* Perform default promotions for C data used in expressions.
1159 Arrays and functions are converted to pointers;
1160 enumeral types or short or char, to int.
1161 In addition, manifest constants symbols are replaced by their values. */
1162
1163tree
2f6e4e97 1164default_conversion (tree exp)
400fbf9f 1165{
157689c6 1166 tree orig_exp;
b3694847
SS
1167 tree type = TREE_TYPE (exp);
1168 enum tree_code code = TREE_CODE (type);
400fbf9f 1169
207bf485
JM
1170 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1171 return default_function_array_conversion (exp);
1172
400fbf9f
JW
1173 /* Constants can be used directly unless they're not loadable. */
1174 if (TREE_CODE (exp) == CONST_DECL)
1175 exp = DECL_INITIAL (exp);
d4424a75
RK
1176
1177 /* Replace a nonvolatile const static variable with its value unless
1178 it is an array, in which case we must be sure that taking the
1179 address of the array produces consistent results. */
1180 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
400fbf9f 1181 {
2f74f7e9 1182 exp = decl_constant_value_for_broken_optimization (exp);
400fbf9f
JW
1183 type = TREE_TYPE (exp);
1184 }
1185
a7d53fce 1186 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
2f6e4e97 1187 an lvalue.
05bccae2
RK
1188
1189 Do not use STRIP_NOPS here! It will remove conversions from pointer
a7d53fce 1190 to integer and cause infinite recursion. */
157689c6 1191 orig_exp = exp;
a7d53fce
RS
1192 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1193 || (TREE_CODE (exp) == NOP_EXPR
1194 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1195 exp = TREE_OPERAND (exp, 0);
400fbf9f 1196
157689c6
NB
1197 /* Preserve the original expression code. */
1198 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1199 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1200
400fbf9f
JW
1201 /* Normally convert enums to int,
1202 but convert wide enums to something wider. */
1203 if (code == ENUMERAL_TYPE)
1204 {
b0c48229
NB
1205 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1206 TYPE_PRECISION (integer_type_node)),
1207 ((TYPE_PRECISION (type)
1208 >= TYPE_PRECISION (integer_type_node))
8df83eae 1209 && TYPE_UNSIGNED (type)));
05bccae2 1210
400fbf9f
JW
1211 return convert (type, exp);
1212 }
1213
9753f113 1214 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1215 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1216 /* If it's thinner than an int, promote it like a
d72040f5 1217 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1218 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1219 TYPE_PRECISION (integer_type_node)))
f458d1d5 1220 return convert (integer_type_node, exp);
9753f113 1221
d72040f5 1222 if (c_promoting_integer_type_p (type))
400fbf9f 1223 {
f458d1d5 1224 /* Preserve unsignedness if not really getting any wider. */
8df83eae 1225 if (TYPE_UNSIGNED (type)
f458d1d5 1226 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1227 return convert (unsigned_type_node, exp);
05bccae2 1228
400fbf9f
JW
1229 return convert (integer_type_node, exp);
1230 }
05bccae2 1231
400fbf9f
JW
1232 if (code == VOID_TYPE)
1233 {
1234 error ("void value not ignored as it ought to be");
1235 return error_mark_node;
1236 }
400fbf9f
JW
1237 return exp;
1238}
1239\f
e9b2c823
NB
1240/* Look up COMPONENT in a structure or union DECL.
1241
1242 If the component name is not found, returns NULL_TREE. Otherwise,
1243 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1244 stepping down the chain to the component, which is in the last
1245 TREE_VALUE of the list. Normally the list is of length one, but if
1246 the component is embedded within (nested) anonymous structures or
1247 unions, the list steps down the chain to the component. */
2f6e4e97 1248
2f2d13da 1249static tree
2f6e4e97 1250lookup_field (tree decl, tree component)
2f2d13da 1251{
e9b2c823 1252 tree type = TREE_TYPE (decl);
2f2d13da
DE
1253 tree field;
1254
1255 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1256 to the field elements. Use a binary search on this array to quickly
1257 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1258 will always be set for structures which have many elements. */
1259
1260 if (TYPE_LANG_SPECIFIC (type))
1261 {
1262 int bot, top, half;
d07605f5 1263 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
1264
1265 field = TYPE_FIELDS (type);
1266 bot = 0;
d07605f5 1267 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
1268 while (top - bot > 1)
1269 {
2f2d13da
DE
1270 half = (top - bot + 1) >> 1;
1271 field = field_array[bot+half];
1272
1273 if (DECL_NAME (field) == NULL_TREE)
1274 {
1275 /* Step through all anon unions in linear fashion. */
1276 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1277 {
2f2d13da 1278 field = field_array[bot++];
a68b98cf
RK
1279 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1280 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 1281 {
e9b2c823
NB
1282 tree anon = lookup_field (field, component);
1283
1284 if (anon)
1285 return tree_cons (NULL_TREE, field, anon);
2f6e4e97 1286 }
2f2d13da
DE
1287 }
1288
1289 /* Entire record is only anon unions. */
1290 if (bot > top)
1291 return NULL_TREE;
1292
1293 /* Restart the binary search, with new lower bound. */
1294 continue;
1295 }
1296
e8b87aac 1297 if (DECL_NAME (field) == component)
2f2d13da 1298 break;
e8b87aac 1299 if (DECL_NAME (field) < component)
2f2d13da
DE
1300 bot += half;
1301 else
1302 top = bot + half;
1303 }
1304
1305 if (DECL_NAME (field_array[bot]) == component)
1306 field = field_array[bot];
1307 else if (DECL_NAME (field) != component)
e9b2c823 1308 return NULL_TREE;
2f2d13da
DE
1309 }
1310 else
1311 {
1312 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1313 {
e9b2c823
NB
1314 if (DECL_NAME (field) == NULL_TREE
1315 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1316 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 1317 {
e9b2c823 1318 tree anon = lookup_field (field, component);
a68b98cf 1319
e9b2c823
NB
1320 if (anon)
1321 return tree_cons (NULL_TREE, field, anon);
2f2d13da
DE
1322 }
1323
1324 if (DECL_NAME (field) == component)
1325 break;
1326 }
e9b2c823
NB
1327
1328 if (field == NULL_TREE)
1329 return NULL_TREE;
2f2d13da
DE
1330 }
1331
e9b2c823 1332 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
1333}
1334
400fbf9f
JW
1335/* Make an expression to refer to the COMPONENT field of
1336 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1337
1338tree
2f6e4e97 1339build_component_ref (tree datum, tree component)
400fbf9f 1340{
b3694847
SS
1341 tree type = TREE_TYPE (datum);
1342 enum tree_code code = TREE_CODE (type);
1343 tree field = NULL;
1344 tree ref;
400fbf9f 1345
207bf485 1346 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
53cd18ec 1347 Ensure that the arguments are not lvalues; otherwise,
207bf485
JM
1348 if the component is an array, it would wrongly decay to a pointer in
1349 C89 mode.
1350 We cannot do this with a COND_EXPR, because in a conditional expression
1351 the default promotions are applied to both sides, and this would yield
1352 the wrong type of the result; for example, if the components have
1353 type "char". */
400fbf9f
JW
1354 switch (TREE_CODE (datum))
1355 {
1356 case COMPOUND_EXPR:
1357 {
1358 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
400fbf9f 1359 return build (COMPOUND_EXPR, TREE_TYPE (value),
53cd18ec 1360 TREE_OPERAND (datum, 0), non_lvalue (value));
400fbf9f 1361 }
e9a25f70
JL
1362 default:
1363 break;
400fbf9f
JW
1364 }
1365
1366 /* See if there is a field or component with name COMPONENT. */
1367
1368 if (code == RECORD_TYPE || code == UNION_TYPE)
1369 {
d0f062fb 1370 if (!COMPLETE_TYPE_P (type))
400fbf9f 1371 {
7a228918 1372 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1373 return error_mark_node;
1374 }
1375
e9b2c823 1376 field = lookup_field (datum, component);
400fbf9f
JW
1377
1378 if (!field)
1379 {
913d0833
KG
1380 error ("%s has no member named `%s'",
1381 code == RECORD_TYPE ? "structure" : "union",
400fbf9f
JW
1382 IDENTIFIER_POINTER (component));
1383 return error_mark_node;
1384 }
400fbf9f 1385
e9b2c823
NB
1386 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1387 This might be better solved in future the way the C++ front
1388 end does it - by giving the anonymous entities each a
1389 separate name and type, and then have build_component_ref
1390 recursively call itself. We can't do that here. */
46ea50cb 1391 do
19d76e60 1392 {
e9b2c823
NB
1393 tree subdatum = TREE_VALUE (field);
1394
1395 if (TREE_TYPE (subdatum) == error_mark_node)
1396 return error_mark_node;
1397
1398 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1399 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
19d76e60 1400 TREE_READONLY (ref) = 1;
e9b2c823 1401 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
19d76e60 1402 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
1403
1404 if (TREE_DEPRECATED (subdatum))
1405 warn_deprecated_use (subdatum);
1406
19d76e60 1407 datum = ref;
46ea50cb
RS
1408
1409 field = TREE_CHAIN (field);
19d76e60 1410 }
46ea50cb 1411 while (field);
19d76e60 1412
400fbf9f
JW
1413 return ref;
1414 }
1415 else if (code != ERROR_MARK)
1416 error ("request for member `%s' in something not a structure or union",
1417 IDENTIFIER_POINTER (component));
1418
1419 return error_mark_node;
1420}
1421\f
1422/* Given an expression PTR for a pointer, return an expression
1423 for the value pointed to.
1424 ERRORSTRING is the name of the operator to appear in error messages. */
1425
1426tree
2f6e4e97 1427build_indirect_ref (tree ptr, const char *errorstring)
400fbf9f 1428{
b3694847
SS
1429 tree pointer = default_conversion (ptr);
1430 tree type = TREE_TYPE (pointer);
400fbf9f
JW
1431
1432 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b
RS
1433 {
1434 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
1435 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1436 == TREE_TYPE (type)))
1437 return TREE_OPERAND (pointer, 0);
1438 else
1439 {
1440 tree t = TREE_TYPE (type);
b3694847 1441 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
400fbf9f 1442
baae9b65 1443 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b
RS
1444 {
1445 error ("dereferencing pointer to incomplete type");
1446 return error_mark_node;
1447 }
baae9b65 1448 if (VOID_TYPE_P (t) && skip_evaluation == 0)
870cc33b
RS
1449 warning ("dereferencing `void *' pointer");
1450
1451 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1452 so that we get the proper error message if the result is used
1453 to assign to. Also, &* is supposed to be a no-op.
1454 And ANSI C seems to specify that the type of the result
1455 should be the const type. */
1456 /* A de-reference of a pointer to const is not a const. It is valid
1457 to change it via some other pointer. */
1458 TREE_READONLY (ref) = TYPE_READONLY (t);
1459 TREE_SIDE_EFFECTS (ref)
271bd540 1460 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 1461 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
1462 return ref;
1463 }
1464 }
400fbf9f
JW
1465 else if (TREE_CODE (pointer) != ERROR_MARK)
1466 error ("invalid type argument of `%s'", errorstring);
1467 return error_mark_node;
1468}
1469
1470/* This handles expressions of the form "a[i]", which denotes
1471 an array reference.
1472
1473 This is logically equivalent in C to *(a+i), but we may do it differently.
1474 If A is a variable or a member, we generate a primitive ARRAY_REF.
1475 This avoids forcing the array out of registers, and can work on
1476 arrays that are not lvalues (for example, members of structures returned
1477 by functions). */
1478
1479tree
2f6e4e97 1480build_array_ref (tree array, tree index)
400fbf9f
JW
1481{
1482 if (index == 0)
1483 {
1484 error ("subscript missing in array reference");
1485 return error_mark_node;
1486 }
1487
1488 if (TREE_TYPE (array) == error_mark_node
1489 || TREE_TYPE (index) == error_mark_node)
1490 return error_mark_node;
1491
1492 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1493 && TREE_CODE (array) != INDIRECT_REF)
1494 {
1495 tree rval, type;
1496
400fbf9f
JW
1497 /* Subscripting with type char is likely to lose
1498 on a machine where chars are signed.
1499 So warn on any machine, but optionally.
1500 Don't warn for unsigned char since that type is safe.
1501 Don't warn for signed char because anyone who uses that
1502 must have done so deliberately. */
1503 if (warn_char_subscripts
1504 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1505 warning ("array subscript has type `char'");
1506
0e51ef9b
RS
1507 /* Apply default promotions *after* noticing character types. */
1508 index = default_conversion (index);
1509
fdeefd49
RS
1510 /* Require integer *after* promotion, for sake of enums. */
1511 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1512 {
1513 error ("array subscript is not an integer");
1514 return error_mark_node;
1515 }
1516
400fbf9f
JW
1517 /* An array that is indexed by a non-constant
1518 cannot be stored in a register; we must be able to do
1519 address arithmetic on its address.
1520 Likewise an array of elements of variable size. */
1521 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 1522 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
1523 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1524 {
dffd7eb6 1525 if (!c_mark_addressable (array))
400fbf9f
JW
1526 return error_mark_node;
1527 }
e6d52559
JW
1528 /* An array that is indexed by a constant value which is not within
1529 the array bounds cannot be stored in a register either; because we
1530 would get a crash in store_bit_field/extract_bit_field when trying
1531 to access a non-existent part of the register. */
1532 if (TREE_CODE (index) == INTEGER_CST
eb34af89
RK
1533 && TYPE_DOMAIN (TREE_TYPE (array))
1534 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 1535 {
dffd7eb6 1536 if (!c_mark_addressable (array))
e6d52559
JW
1537 return error_mark_node;
1538 }
400fbf9f 1539
400fbf9f
JW
1540 if (pedantic)
1541 {
1542 tree foo = array;
1543 while (TREE_CODE (foo) == COMPONENT_REF)
1544 foo = TREE_OPERAND (foo, 0);
5baeaac0 1545 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
05273f08
GK
1546 pedwarn ("ISO C forbids subscripting `register' array");
1547 else if (! flag_isoc99 && ! lvalue_p (foo))
56508306 1548 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
1549 }
1550
1551 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1552 rval = build (ARRAY_REF, type, array, index);
1553 /* Array ref is const/volatile if the array elements are
1554 or if the array is. */
1555 TREE_READONLY (rval)
1556 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1557 | TREE_READONLY (array));
1558 TREE_SIDE_EFFECTS (rval)
1559 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1560 | TREE_SIDE_EFFECTS (array));
1561 TREE_THIS_VOLATILE (rval)
1562 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1563 /* This was added by rms on 16 Nov 91.
2f6e4e97 1564 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
1565 in an inline function.
1566 Hope it doesn't break something else. */
1567 | TREE_THIS_VOLATILE (array));
1568 return require_complete_type (fold (rval));
1569 }
1570
1571 {
1572 tree ar = default_conversion (array);
1573 tree ind = default_conversion (index);
1574
aed11452
RK
1575 /* Do the same warning check as above, but only on the part that's
1576 syntactically the index and only if it is also semantically
1577 the index. */
1578 if (warn_char_subscripts
1579 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1580 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1581 warning ("subscript has type `char'");
1582
400fbf9f
JW
1583 /* Put the integer in IND to simplify error checking. */
1584 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1585 {
1586 tree temp = ar;
1587 ar = ind;
1588 ind = temp;
1589 }
1590
1591 if (ar == error_mark_node)
1592 return ar;
1593
004252d7
RK
1594 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1595 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
400fbf9f
JW
1596 {
1597 error ("subscripted value is neither array nor pointer");
1598 return error_mark_node;
1599 }
1600 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1601 {
1602 error ("array subscript is not an integer");
1603 return error_mark_node;
1604 }
1605
1606 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1607 "array indexing");
1608 }
1609}
1610\f
7e585d16
ZW
1611/* Build an external reference to identifier ID. FUN indicates
1612 whether this will be used for a function call. */
1613tree
2f6e4e97 1614build_external_ref (tree id, int fun)
7e585d16
ZW
1615{
1616 tree ref;
1617 tree decl = lookup_name (id);
1618 tree objc_ivar = lookup_objc_ivar (id);
1619
339a28b9 1620 if (decl && decl != error_mark_node)
7e585d16
ZW
1621 {
1622 /* Properly declared variable or function reference. */
1623 if (!objc_ivar)
1624 ref = decl;
4b1e44be 1625 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
7e585d16
ZW
1626 {
1627 warning ("local declaration of `%s' hides instance variable",
1628 IDENTIFIER_POINTER (id));
1629 ref = decl;
1630 }
1631 else
1632 ref = objc_ivar;
1633 }
339a28b9
ZW
1634 else if (objc_ivar)
1635 ref = objc_ivar;
1636 else if (fun)
1637 /* Implicit function declaration. */
1638 ref = implicitly_declare (id);
1639 else if (decl == error_mark_node)
1640 /* Don't complain about something that's already been
1641 complained about. */
1642 return error_mark_node;
1643 else
1644 {
1645 undeclared_variable (id);
1646 return error_mark_node;
1647 }
7e585d16
ZW
1648
1649 if (TREE_TYPE (ref) == error_mark_node)
1650 return error_mark_node;
1651
339a28b9
ZW
1652 if (TREE_DEPRECATED (ref))
1653 warn_deprecated_use (ref);
1654
25587e40
AO
1655 if (!skip_evaluation)
1656 assemble_external (ref);
7e585d16
ZW
1657 TREE_USED (ref) = 1;
1658
1659 if (TREE_CODE (ref) == CONST_DECL)
1660 {
1661 ref = DECL_INITIAL (ref);
1662 TREE_CONSTANT (ref) = 1;
1663 }
6a29edea 1664 else if (current_function_decl != 0
4b1e44be 1665 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
1666 && (TREE_CODE (ref) == VAR_DECL
1667 || TREE_CODE (ref) == PARM_DECL
1668 || TREE_CODE (ref) == FUNCTION_DECL))
1669 {
1670 tree context = decl_function_context (ref);
2f6e4e97 1671
6a29edea
EB
1672 if (context != 0 && context != current_function_decl)
1673 DECL_NONLOCAL (ref) = 1;
1674 }
7e585d16
ZW
1675
1676 return ref;
1677}
1678
400fbf9f
JW
1679/* Build a function call to function FUNCTION with parameters PARAMS.
1680 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1681 TREE_VALUE of each node is a parameter-expression.
1682 FUNCTION's data type may be a function type or a pointer-to-function. */
1683
1684tree
2f6e4e97 1685build_function_call (tree function, tree params)
400fbf9f 1686{
b3694847
SS
1687 tree fntype, fundecl = 0;
1688 tree coerced_params;
4977bab6 1689 tree name = NULL_TREE, result;
c96f4f73 1690 tree tem;
400fbf9f 1691
fc76e425 1692 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 1693 STRIP_TYPE_NOPS (function);
400fbf9f
JW
1694
1695 /* Convert anything with function type to a pointer-to-function. */
1696 if (TREE_CODE (function) == FUNCTION_DECL)
1697 {
1698 name = DECL_NAME (function);
19d76e60 1699
400fbf9f
JW
1700 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1701 (because calling an inline function does not mean the function
1702 needs to be separately compiled). */
1703 fntype = build_type_variant (TREE_TYPE (function),
1704 TREE_READONLY (function),
1705 TREE_THIS_VOLATILE (function));
9b7267b8 1706 fundecl = function;
400fbf9f
JW
1707 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1708 }
1709 else
1710 function = default_conversion (function);
1711
1712 fntype = TREE_TYPE (function);
1713
1714 if (TREE_CODE (fntype) == ERROR_MARK)
1715 return error_mark_node;
1716
1717 if (!(TREE_CODE (fntype) == POINTER_TYPE
1718 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1719 {
1720 error ("called object is not a function");
1721 return error_mark_node;
1722 }
1723
5ce89b2e
JM
1724 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1725 current_function_returns_abnormally = 1;
1726
400fbf9f
JW
1727 /* fntype now gets the type of function pointed to. */
1728 fntype = TREE_TYPE (fntype);
1729
c96f4f73
EB
1730 /* Check that the function is called through a compatible prototype.
1731 If it is not, replace the call by a trap, wrapped up in a compound
1732 expression if necessary. This has the nice side-effect to prevent
1733 the tree-inliner from generating invalid assignment trees which may
1734 blow up in the RTL expander later.
1735
1736 ??? This doesn't work for Objective-C because objc_comptypes
1737 refuses to compare function prototypes, yet the compiler appears
1738 to build calls that are flagged as invalid by C's comptypes. */
1739 if (! c_dialect_objc ()
1740 && TREE_CODE (function) == NOP_EXPR
1741 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1742 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1743 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1744 {
1745 tree return_type = TREE_TYPE (fntype);
1746 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1747 NULL_TREE);
1748
1749 /* This situation leads to run-time undefined behavior. We can't,
1750 therefore, simply error unless we can prove that all possible
1751 executions of the program must execute the code. */
1752 warning ("function called through a non-compatible type");
1753
bba745c1
EB
1754 /* We can, however, treat "undefined" any way we please.
1755 Call abort to encourage the user to fix the program. */
1756 inform ("if this code is reached, the program will abort");
1757
c96f4f73
EB
1758 if (VOID_TYPE_P (return_type))
1759 return trap;
1760 else
1761 {
1762 tree rhs;
1763
1764 if (AGGREGATE_TYPE_P (return_type))
1765 rhs = build_compound_literal (return_type,
1766 build_constructor (return_type,
1767 NULL_TREE));
1768 else
1769 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1770
1771 return build (COMPOUND_EXPR, return_type, trap, rhs);
1772 }
1773 }
1774
400fbf9f
JW
1775 /* Convert the parameters to the types declared in the
1776 function prototype, or apply default promotions. */
1777
1778 coerced_params
9b7267b8 1779 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
400fbf9f 1780
b34c7881 1781 /* Check that the arguments to the function are valid. */
400fbf9f 1782
b34c7881 1783 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
400fbf9f
JW
1784
1785 /* Recognize certain built-in functions so we can make tree-codes
1786 other than CALL_EXPR. We do this when it enables fold-const.c
1787 to do something useful. */
1788
1789 if (TREE_CODE (function) == ADDR_EXPR
1790 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1eb8759b
RH
1791 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1792 {
1793 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1794 params, coerced_params);
1795 if (result)
1796 return result;
1797 }
400fbf9f 1798
1eb8759b
RH
1799 result = build (CALL_EXPR, TREE_TYPE (fntype),
1800 function, coerced_params, NULL_TREE);
1eb8759b 1801 TREE_SIDE_EFFECTS (result) = 1;
b0b3afb2
BS
1802 result = fold (result);
1803
71653180 1804 if (VOID_TYPE_P (TREE_TYPE (result)))
1eb8759b
RH
1805 return result;
1806 return require_complete_type (result);
400fbf9f
JW
1807}
1808\f
1809/* Convert the argument expressions in the list VALUES
1810 to the types in the list TYPELIST. The result is a list of converted
1811 argument expressions.
1812
1813 If TYPELIST is exhausted, or when an element has NULL as its type,
1814 perform the default conversions.
1815
1816 PARMLIST is the chain of parm decls for the function being called.
1817 It may be 0, if that info is not available.
1818 It is used only for generating error messages.
1819
1820 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1821
1822 This is also where warnings about wrong number of args are generated.
1823
1824 Both VALUES and the returned value are chains of TREE_LIST nodes
1825 with the elements of the list in the TREE_VALUE slots of those nodes. */
1826
1827static tree
2f6e4e97 1828convert_arguments (tree typelist, tree values, tree name, tree fundecl)
400fbf9f 1829{
b3694847
SS
1830 tree typetail, valtail;
1831 tree result = NULL;
400fbf9f
JW
1832 int parmnum;
1833
1834 /* Scan the given expressions and types, producing individual
1835 converted arguments and pushing them on RESULT in reverse order. */
1836
1837 for (valtail = values, typetail = typelist, parmnum = 0;
1838 valtail;
1839 valtail = TREE_CHAIN (valtail), parmnum++)
1840 {
b3694847
SS
1841 tree type = typetail ? TREE_VALUE (typetail) : 0;
1842 tree val = TREE_VALUE (valtail);
400fbf9f
JW
1843
1844 if (type == void_type_node)
1845 {
1846 if (name)
1847 error ("too many arguments to function `%s'",
1848 IDENTIFIER_POINTER (name));
1849 else
1850 error ("too many arguments to function");
1851 break;
1852 }
1853
1854 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
1855 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1856 to convert automatically to a pointer. */
400fbf9f
JW
1857 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1858 val = TREE_OPERAND (val, 0);
1859
207bf485 1860 val = default_function_array_conversion (val);
400fbf9f
JW
1861
1862 val = require_complete_type (val);
1863
1864 if (type != 0)
1865 {
1866 /* Formal parm type is specified by a function prototype. */
1867 tree parmval;
1868
d0f062fb 1869 if (!COMPLETE_TYPE_P (type))
400fbf9f
JW
1870 {
1871 error ("type of formal parameter %d is incomplete", parmnum + 1);
1872 parmval = val;
1873 }
1874 else
1875 {
d45cf215
RS
1876 /* Optionally warn about conversions that
1877 differ from the default conversions. */
03829ad2 1878 if (warn_conversion || warn_traditional)
400fbf9f
JW
1879 {
1880 int formal_prec = TYPE_PRECISION (type);
400fbf9f 1881
aae43c5f 1882 if (INTEGRAL_TYPE_P (type)
400fbf9f 1883 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
754a4d82 1884 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
03829ad2
KG
1885 if (INTEGRAL_TYPE_P (type)
1886 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1887 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1888 else if (TREE_CODE (type) == COMPLEX_TYPE
1889 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1890 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f 1891 else if (TREE_CODE (type) == REAL_TYPE
aae43c5f 1892 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
754a4d82 1893 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
03829ad2
KG
1894 else if (TREE_CODE (type) == COMPLEX_TYPE
1895 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1896 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1897 else if (TREE_CODE (type) == REAL_TYPE
1898 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1899 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1900 /* ??? At some point, messages should be written about
1901 conversions between complex types, but that's too messy
1902 to do now. */
d45cf215
RS
1903 else if (TREE_CODE (type) == REAL_TYPE
1904 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1905 {
1906 /* Warn if any argument is passed as `float',
047de90b 1907 since without a prototype it would be `double'. */
d45cf215 1908 if (formal_prec == TYPE_PRECISION (float_type_node))
754a4d82 1909 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
d45cf215 1910 }
3ed56f8a
KG
1911 /* Detect integer changing in width or signedness.
1912 These warnings are only activated with
1913 -Wconversion, not with -Wtraditional. */
1914 else if (warn_conversion && INTEGRAL_TYPE_P (type)
aae43c5f 1915 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
400fbf9f 1916 {
d45cf215
RS
1917 tree would_have_been = default_conversion (val);
1918 tree type1 = TREE_TYPE (would_have_been);
1919
754a4d82 1920 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a
NB
1921 && (TYPE_MAIN_VARIANT (type)
1922 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
754a4d82
RS
1923 /* No warning if function asks for enum
1924 and the actual arg is that enum type. */
1925 ;
1926 else if (formal_prec != TYPE_PRECISION (type1))
1927 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
8df83eae 1928 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 1929 ;
800cd3b9
RS
1930 /* Don't complain if the formal parameter type
1931 is an enum, because we can't tell now whether
1932 the value was an enum--even the same enum. */
1933 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1934 ;
400fbf9f
JW
1935 else if (TREE_CODE (val) == INTEGER_CST
1936 && int_fits_type_p (val, type))
1937 /* Change in signedness doesn't matter
1938 if a constant value is unaffected. */
1939 ;
4bbbc5d9
RS
1940 /* Likewise for a constant in a NOP_EXPR. */
1941 else if (TREE_CODE (val) == NOP_EXPR
1942 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1943 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1944 ;
ce9895ae
RS
1945 /* If the value is extended from a narrower
1946 unsigned type, it doesn't matter whether we
1947 pass it as signed or unsigned; the value
1948 certainly is the same either way. */
1949 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
8df83eae 1950 && TYPE_UNSIGNED (TREE_TYPE (val)))
ce9895ae 1951 ;
8df83eae 1952 else if (TYPE_UNSIGNED (type))
3ed56f8a
KG
1953 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1954 else
1955 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f
JW
1956 }
1957 }
1958
2f6e4e97 1959 parmval = convert_for_assignment (type, val,
0f41302f 1960 (char *) 0, /* arg passing */
9b7267b8 1961 fundecl, name, parmnum + 1);
2f6e4e97 1962
61f71b34 1963 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 1964 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
1965 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1966 parmval = default_conversion (parmval);
400fbf9f 1967 }
8d9bfdc5 1968 result = tree_cons (NULL_TREE, parmval, result);
400fbf9f
JW
1969 }
1970 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1971 && (TYPE_PRECISION (TREE_TYPE (val))
1972 < TYPE_PRECISION (double_type_node)))
1973 /* Convert `float' to `double'. */
1974 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1975 else
1976 /* Convert `short' and `char' to full-size `int'. */
1977 result = tree_cons (NULL_TREE, default_conversion (val), result);
1978
1979 if (typetail)
1980 typetail = TREE_CHAIN (typetail);
1981 }
1982
1983 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1984 {
1985 if (name)
1986 error ("too few arguments to function `%s'",
1987 IDENTIFIER_POINTER (name));
1988 else
1989 error ("too few arguments to function");
1990 }
1991
1992 return nreverse (result);
1993}
1994\f
1995/* This is the entry point used by the parser
1996 for binary operators in the input.
1997 In addition to constructing the expression,
1998 we check for operands that were written with other binary operators
1999 in a way that is likely to confuse the user. */
edc7c4ec 2000
400fbf9f 2001tree
2f6e4e97 2002parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
400fbf9f
JW
2003{
2004 tree result = build_binary_op (code, arg1, arg2, 1);
2005
2006 char class;
2007 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2008 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2009 enum tree_code code1 = ERROR_MARK;
2010 enum tree_code code2 = ERROR_MARK;
2011
58bf601b
HPN
2012 if (TREE_CODE (result) == ERROR_MARK)
2013 return error_mark_node;
2014
686deecb 2015 if (IS_EXPR_CODE_CLASS (class1))
400fbf9f 2016 code1 = C_EXP_ORIGINAL_CODE (arg1);
686deecb 2017 if (IS_EXPR_CODE_CLASS (class2))
400fbf9f
JW
2018 code2 = C_EXP_ORIGINAL_CODE (arg2);
2019
2020 /* Check for cases such as x+y<<z which users are likely
2021 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2022 is cleared to prevent these warnings. */
2023 if (warn_parentheses)
2024 {
2025 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2026 {
2027 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2028 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2029 warning ("suggest parentheses around + or - inside shift");
2030 }
2031
2032 if (code == TRUTH_ORIF_EXPR)
2033 {
2034 if (code1 == TRUTH_ANDIF_EXPR
2035 || code2 == TRUTH_ANDIF_EXPR)
2036 warning ("suggest parentheses around && within ||");
2037 }
2038
2039 if (code == BIT_IOR_EXPR)
2040 {
2041 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2042 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2043 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2044 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2045 warning ("suggest parentheses around arithmetic in operand of |");
7e9d002a
RK
2046 /* Check cases like x|y==z */
2047 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2048 warning ("suggest parentheses around comparison in operand of |");
400fbf9f
JW
2049 }
2050
2051 if (code == BIT_XOR_EXPR)
2052 {
2053 if (code1 == BIT_AND_EXPR
2054 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2055 || code2 == BIT_AND_EXPR
2056 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2057 warning ("suggest parentheses around arithmetic in operand of ^");
7e9d002a
RK
2058 /* Check cases like x^y==z */
2059 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2060 warning ("suggest parentheses around comparison in operand of ^");
400fbf9f
JW
2061 }
2062
2063 if (code == BIT_AND_EXPR)
2064 {
2065 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2066 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2067 warning ("suggest parentheses around + or - in operand of &");
7e9d002a
RK
2068 /* Check cases like x&y==z */
2069 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2070 warning ("suggest parentheses around comparison in operand of &");
400fbf9f
JW
2071 }
2072 }
2073
001af587 2074 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
edc7c4ec 2075 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
001af587
RS
2076 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2077 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2078
e58cd767
RS
2079 unsigned_conversion_warning (result, arg1);
2080 unsigned_conversion_warning (result, arg2);
2081 overflow_warning (result);
2082
edc7c4ec
RS
2083 class = TREE_CODE_CLASS (TREE_CODE (result));
2084
400fbf9f
JW
2085 /* Record the code that was specified in the source,
2086 for the sake of warnings about confusing nesting. */
686deecb 2087 if (IS_EXPR_CODE_CLASS (class))
400fbf9f
JW
2088 C_SET_EXP_ORIGINAL_CODE (result, code);
2089 else
2090 {
2091 int flag = TREE_CONSTANT (result);
d11fdb45
RS
2092 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2093 so that convert_for_assignment wouldn't strip it.
2094 That way, we got warnings for things like p = (1 - 1).
2095 But it turns out we should not get those warnings. */
2096 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
400fbf9f
JW
2097 C_SET_EXP_ORIGINAL_CODE (result, code);
2098 TREE_CONSTANT (result) = flag;
2099 }
2100
2101 return result;
2102}
3e4093b6 2103\f
400fbf9f 2104
3e4093b6 2105/* Return true if `t' is known to be non-negative. */
400fbf9f 2106
3e4093b6
RS
2107int
2108c_tree_expr_nonnegative_p (tree t)
400fbf9f 2109{
3e4093b6
RS
2110 if (TREE_CODE (t) == STMT_EXPR)
2111 {
2112 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
400fbf9f 2113
3e4093b6
RS
2114 /* Find the last statement in the chain, ignoring the final
2115 * scope statement */
2116 while (TREE_CHAIN (t) != NULL_TREE
2117 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2118 t = TREE_CHAIN (t);
2119 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2120 }
2121 return tree_expr_nonnegative_p (t);
2122}
400fbf9f 2123
3e4093b6
RS
2124/* Return a tree for the difference of pointers OP0 and OP1.
2125 The resulting tree has type int. */
293c9fdd 2126
3e4093b6
RS
2127static tree
2128pointer_diff (tree op0, tree op1)
2129{
2130 tree result, folded;
2131 tree restype = ptrdiff_type_node;
400fbf9f 2132
3e4093b6
RS
2133 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2134 tree con0, con1, lit0, lit1;
2135 tree orig_op1 = op1;
400fbf9f 2136
3e4093b6
RS
2137 if (pedantic || warn_pointer_arith)
2138 {
2139 if (TREE_CODE (target_type) == VOID_TYPE)
2140 pedwarn ("pointer of type `void *' used in subtraction");
2141 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2142 pedwarn ("pointer to a function used in subtraction");
2143 }
400fbf9f 2144
3e4093b6
RS
2145 /* If the conversion to ptrdiff_type does anything like widening or
2146 converting a partial to an integral mode, we get a convert_expression
2147 that is in the way to do any simplifications.
2148 (fold-const.c doesn't know that the extra bits won't be needed.
2149 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2150 different mode in place.)
2151 So first try to find a common term here 'by hand'; we want to cover
2152 at least the cases that occur in legal static initializers. */
2153 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2154 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
400fbf9f 2155
3e4093b6
RS
2156 if (TREE_CODE (con0) == PLUS_EXPR)
2157 {
2158 lit0 = TREE_OPERAND (con0, 1);
2159 con0 = TREE_OPERAND (con0, 0);
2160 }
2161 else
2162 lit0 = integer_zero_node;
400fbf9f 2163
3e4093b6 2164 if (TREE_CODE (con1) == PLUS_EXPR)
400fbf9f 2165 {
3e4093b6
RS
2166 lit1 = TREE_OPERAND (con1, 1);
2167 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
2168 }
2169 else
3e4093b6
RS
2170 lit1 = integer_zero_node;
2171
2172 if (operand_equal_p (con0, con1, 0))
400fbf9f 2173 {
3e4093b6
RS
2174 op0 = lit0;
2175 op1 = lit1;
400fbf9f
JW
2176 }
2177
400fbf9f 2178
3e4093b6
RS
2179 /* First do the subtraction as integers;
2180 then drop through to build the divide operator.
2181 Do not do default conversions on the minus operator
2182 in case restype is a short type. */
400fbf9f 2183
3e4093b6
RS
2184 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2185 convert (restype, op1), 0);
2186 /* This generates an error if op1 is pointer to incomplete type. */
2187 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2188 error ("arithmetic on pointer to an incomplete type");
400fbf9f 2189
3e4093b6
RS
2190 /* This generates an error if op0 is pointer to incomplete type. */
2191 op1 = c_size_in_bytes (target_type);
400fbf9f 2192
3e4093b6 2193 /* Divide by the size, in easiest possible way. */
400fbf9f 2194
3e4093b6 2195 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
400fbf9f 2196
3e4093b6
RS
2197 folded = fold (result);
2198 if (folded == result)
2199 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2200 return folded;
2201}
2202\f
2203/* Construct and perhaps optimize a tree representation
2204 for a unary operation. CODE, a tree_code, specifies the operation
2205 and XARG is the operand.
2206 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2207 the default promotions (such as from short to int).
2208 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2209 allows non-lvalues; this is only used to handle conversion of non-lvalue
2210 arrays to pointers in C99. */
400fbf9f 2211
3e4093b6
RS
2212tree
2213build_unary_op (enum tree_code code, tree xarg, int flag)
2214{
2215 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2216 tree arg = xarg;
2217 tree argtype = 0;
2218 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2219 tree val;
2220 int noconvert = flag;
400fbf9f 2221
3e4093b6
RS
2222 if (typecode == ERROR_MARK)
2223 return error_mark_node;
2224 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2225 typecode = INTEGER_TYPE;
6c36d76b 2226
3e4093b6
RS
2227 switch (code)
2228 {
2229 case CONVERT_EXPR:
2230 /* This is used for unary plus, because a CONVERT_EXPR
2231 is enough to prevent anybody from looking inside for
2232 associativity, but won't generate any code. */
2233 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2234 || typecode == COMPLEX_TYPE))
400fbf9f 2235 {
3e4093b6
RS
2236 error ("wrong type argument to unary plus");
2237 return error_mark_node;
400fbf9f 2238 }
3e4093b6
RS
2239 else if (!noconvert)
2240 arg = default_conversion (arg);
2241 arg = non_lvalue (arg);
400fbf9f
JW
2242 break;
2243
3e4093b6
RS
2244 case NEGATE_EXPR:
2245 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2246 || typecode == COMPLEX_TYPE
2247 || typecode == VECTOR_TYPE))
2248 {
2249 error ("wrong type argument to unary minus");
2250 return error_mark_node;
2251 }
2252 else if (!noconvert)
2253 arg = default_conversion (arg);
400fbf9f
JW
2254 break;
2255
3e4093b6
RS
2256 case BIT_NOT_EXPR:
2257 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
03d5b1f5 2258 {
3e4093b6
RS
2259 if (!noconvert)
2260 arg = default_conversion (arg);
03d5b1f5 2261 }
3e4093b6 2262 else if (typecode == COMPLEX_TYPE)
400fbf9f 2263 {
3e4093b6
RS
2264 code = CONJ_EXPR;
2265 if (pedantic)
2266 pedwarn ("ISO C does not support `~' for complex conjugation");
2267 if (!noconvert)
2268 arg = default_conversion (arg);
2269 }
2270 else
2271 {
2272 error ("wrong type argument to bit-complement");
2273 return error_mark_node;
400fbf9f
JW
2274 }
2275 break;
2276
3e4093b6 2277 case ABS_EXPR:
11017cc7 2278 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 2279 {
3e4093b6
RS
2280 error ("wrong type argument to abs");
2281 return error_mark_node;
400fbf9f 2282 }
3e4093b6
RS
2283 else if (!noconvert)
2284 arg = default_conversion (arg);
400fbf9f
JW
2285 break;
2286
3e4093b6
RS
2287 case CONJ_EXPR:
2288 /* Conjugating a real value is a no-op, but allow it anyway. */
2289 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2290 || typecode == COMPLEX_TYPE))
400fbf9f 2291 {
3e4093b6
RS
2292 error ("wrong type argument to conjugation");
2293 return error_mark_node;
400fbf9f 2294 }
3e4093b6
RS
2295 else if (!noconvert)
2296 arg = default_conversion (arg);
400fbf9f
JW
2297 break;
2298
3e4093b6
RS
2299 case TRUTH_NOT_EXPR:
2300 if (typecode != INTEGER_TYPE
2301 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2302 && typecode != COMPLEX_TYPE
2303 /* These will convert to a pointer. */
2304 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
400fbf9f 2305 {
3e4093b6
RS
2306 error ("wrong type argument to unary exclamation mark");
2307 return error_mark_node;
400fbf9f 2308 }
ae2bcd98 2309 arg = lang_hooks.truthvalue_conversion (arg);
3e4093b6
RS
2310 return invert_truthvalue (arg);
2311
2312 case NOP_EXPR:
400fbf9f
JW
2313 break;
2314
3e4093b6
RS
2315 case REALPART_EXPR:
2316 if (TREE_CODE (arg) == COMPLEX_CST)
2317 return TREE_REALPART (arg);
2318 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2319 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2320 else
2321 return arg;
605a99f6 2322
3e4093b6
RS
2323 case IMAGPART_EXPR:
2324 if (TREE_CODE (arg) == COMPLEX_CST)
2325 return TREE_IMAGPART (arg);
2326 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2327 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2328 else
2329 return convert (TREE_TYPE (arg), integer_zero_node);
2330
2331 case PREINCREMENT_EXPR:
2332 case POSTINCREMENT_EXPR:
2333 case PREDECREMENT_EXPR:
2334 case POSTDECREMENT_EXPR:
3e4093b6
RS
2335
2336 /* Increment or decrement the real part of the value,
2337 and don't change the imaginary part. */
2338 if (typecode == COMPLEX_TYPE)
400fbf9f 2339 {
3e4093b6
RS
2340 tree real, imag;
2341
2342 if (pedantic)
2343 pedwarn ("ISO C does not support `++' and `--' on complex types");
2344
2345 arg = stabilize_reference (arg);
2346 real = build_unary_op (REALPART_EXPR, arg, 1);
2347 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2348 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2349 build_unary_op (code, real, 1), imag);
400fbf9f 2350 }
3e4093b6
RS
2351
2352 /* Report invalid types. */
2353
2354 if (typecode != POINTER_TYPE
2355 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
400fbf9f 2356 {
3e4093b6
RS
2357 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2358 error ("wrong type argument to increment");
2359 else
2360 error ("wrong type argument to decrement");
2361
2362 return error_mark_node;
400fbf9f 2363 }
400fbf9f 2364
3e4093b6
RS
2365 {
2366 tree inc;
2367 tree result_type = TREE_TYPE (arg);
400fbf9f 2368
3e4093b6
RS
2369 arg = get_unwidened (arg, 0);
2370 argtype = TREE_TYPE (arg);
2371
2372 /* Compute the increment. */
2373
2374 if (typecode == POINTER_TYPE)
2375 {
2376 /* If pointer target is an undefined struct,
2377 we just cannot know how to do the arithmetic. */
2378 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2379 {
2380 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2381 error ("increment of pointer to unknown structure");
2382 else
2383 error ("decrement of pointer to unknown structure");
2384 }
2385 else if ((pedantic || warn_pointer_arith)
2386 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2387 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2388 {
2389 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2390 pedwarn ("wrong type argument to increment");
2391 else
2392 pedwarn ("wrong type argument to decrement");
2393 }
2394
2395 inc = c_size_in_bytes (TREE_TYPE (result_type));
2396 }
2397 else
2398 inc = integer_one_node;
2399
2400 inc = convert (argtype, inc);
2401
3e4093b6
RS
2402 /* Complain about anything else that is not a true lvalue. */
2403 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2404 || code == POSTINCREMENT_EXPR)
2405 ? "invalid lvalue in increment"
2406 : "invalid lvalue in decrement")))
2407 return error_mark_node;
2408
2409 /* Report a read-only lvalue. */
2410 if (TREE_READONLY (arg))
c5b6f18e
MM
2411 readonly_error (arg,
2412 ((code == PREINCREMENT_EXPR
2413 || code == POSTINCREMENT_EXPR)
2414 ? "increment" : "decrement"));
3e4093b6
RS
2415
2416 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2417 val = boolean_increment (code, arg);
2418 else
2419 val = build (code, TREE_TYPE (arg), arg, inc);
2420 TREE_SIDE_EFFECTS (val) = 1;
2421 val = convert (result_type, val);
2422 if (TREE_CODE (val) != code)
2423 TREE_NO_UNUSED_WARNING (val) = 1;
2424 return val;
2425 }
2426
2427 case ADDR_EXPR:
2428 /* Note that this operation never does default_conversion. */
2429
2430 /* Let &* cancel out to simplify resulting code. */
2431 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 2432 {
3e4093b6
RS
2433 /* Don't let this be an lvalue. */
2434 if (lvalue_p (TREE_OPERAND (arg, 0)))
2435 return non_lvalue (TREE_OPERAND (arg, 0));
2436 return TREE_OPERAND (arg, 0);
400fbf9f 2437 }
1eb8759b 2438
3e4093b6
RS
2439 /* For &x[y], return x+y */
2440 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 2441 {
3e4093b6
RS
2442 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2443 return error_mark_node;
2444 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2445 TREE_OPERAND (arg, 1), 1);
1eb8759b 2446 }
1eb8759b 2447
3e4093b6
RS
2448 /* Anything not already handled and not a true memory reference
2449 or a non-lvalue array is an error. */
2450 else if (typecode != FUNCTION_TYPE && !flag
2451 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2452 return error_mark_node;
b6a10c9f 2453
3e4093b6
RS
2454 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2455 argtype = TREE_TYPE (arg);
400fbf9f 2456
3e4093b6
RS
2457 /* If the lvalue is const or volatile, merge that into the type
2458 to which the address will point. Note that you can't get a
2459 restricted pointer by taking the address of something, so we
2460 only have to deal with `const' and `volatile' here. */
2461 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2462 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2463 argtype = c_build_type_variant (argtype,
2464 TREE_READONLY (arg),
2465 TREE_THIS_VOLATILE (arg));
400fbf9f 2466
3e4093b6 2467 argtype = build_pointer_type (argtype);
400fbf9f 2468
3e4093b6
RS
2469 if (!c_mark_addressable (arg))
2470 return error_mark_node;
400fbf9f 2471
3e4093b6
RS
2472 {
2473 tree addr;
400fbf9f 2474
3e4093b6
RS
2475 if (TREE_CODE (arg) == COMPONENT_REF)
2476 {
2477 tree field = TREE_OPERAND (arg, 1);
400fbf9f 2478
3e4093b6 2479 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
400fbf9f 2480
3e4093b6
RS
2481 if (DECL_C_BIT_FIELD (field))
2482 {
2483 error ("attempt to take address of bit-field structure member `%s'",
2484 IDENTIFIER_POINTER (DECL_NAME (field)));
2485 return error_mark_node;
2486 }
400fbf9f 2487
3e4093b6
RS
2488 addr = fold (build (PLUS_EXPR, argtype,
2489 convert (argtype, addr),
2490 convert (argtype, byte_position (field))));
2491 }
2492 else
2493 addr = build1 (code, argtype, arg);
400fbf9f 2494
3e4093b6
RS
2495 /* Address of a static or external variable or
2496 file-scope function counts as a constant. */
2497 if (staticp (arg)
2498 && ! (TREE_CODE (arg) == FUNCTION_DECL
4b1e44be 2499 && !DECL_FILE_SCOPE_P (arg)))
3e4093b6
RS
2500 TREE_CONSTANT (addr) = 1;
2501 return addr;
2502 }
400fbf9f 2503
3e4093b6
RS
2504 default:
2505 break;
2506 }
400fbf9f 2507
3e4093b6
RS
2508 if (argtype == 0)
2509 argtype = TREE_TYPE (arg);
2510 return fold (build1 (code, argtype, arg));
2511}
400fbf9f 2512
3e4093b6
RS
2513/* Return nonzero if REF is an lvalue valid for this language.
2514 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 2515 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 2516
3e4093b6
RS
2517int
2518lvalue_p (tree ref)
2519{
2520 enum tree_code code = TREE_CODE (ref);
400fbf9f 2521
3e4093b6
RS
2522 switch (code)
2523 {
2524 case REALPART_EXPR:
2525 case IMAGPART_EXPR:
2526 case COMPONENT_REF:
2527 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 2528
3e4093b6
RS
2529 case COMPOUND_LITERAL_EXPR:
2530 case STRING_CST:
2531 return 1;
400fbf9f 2532
3e4093b6
RS
2533 case INDIRECT_REF:
2534 case ARRAY_REF:
2535 case VAR_DECL:
2536 case PARM_DECL:
2537 case RESULT_DECL:
2538 case ERROR_MARK:
2539 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2540 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 2541
3e4093b6
RS
2542 case BIND_EXPR:
2543 case RTL_EXPR:
2544 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 2545
3e4093b6
RS
2546 default:
2547 return 0;
2548 }
2549}
400fbf9f 2550
3e4093b6
RS
2551/* Return nonzero if REF is an lvalue valid for this language;
2552 otherwise, print an error message and return zero. */
64c01f80 2553
3e4093b6
RS
2554int
2555lvalue_or_else (tree ref, const char *msgid)
2556{
2557 int win = lvalue_p (ref);
912b4fc3 2558
3e4093b6
RS
2559 if (! win)
2560 error ("%s", msgid);
293c9fdd 2561
3e4093b6
RS
2562 return win;
2563}
665f2503 2564
400fbf9f 2565\f
3e4093b6 2566/* Warn about storing in something that is `const'. */
54c93c30 2567
3e4093b6 2568void
c5b6f18e 2569readonly_error (tree arg, const char *msgid)
54c93c30 2570{
3e4093b6 2571 if (TREE_CODE (arg) == COMPONENT_REF)
54c93c30 2572 {
3e4093b6 2573 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c5b6f18e 2574 readonly_error (TREE_OPERAND (arg, 0), msgid);
3e4093b6 2575 else
c5b6f18e
MM
2576 error ("%s of read-only member `%s'", _(msgid),
2577 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
54c93c30 2578 }
3e4093b6 2579 else if (TREE_CODE (arg) == VAR_DECL)
c5b6f18e
MM
2580 error ("%s of read-only variable `%s'", _(msgid),
2581 IDENTIFIER_POINTER (DECL_NAME (arg)));
3e4093b6 2582 else
c5b6f18e 2583 error ("%s of read-only location", _(msgid));
54c93c30 2584}
3e4093b6
RS
2585\f
2586/* Mark EXP saying that we need to be able to take the
2587 address of it; it should not be allocated in a register.
2588 Returns true if successful. */
54c93c30 2589
3e4093b6
RS
2590bool
2591c_mark_addressable (tree exp)
400fbf9f 2592{
3e4093b6 2593 tree x = exp;
95602da1 2594
3e4093b6
RS
2595 while (1)
2596 switch (TREE_CODE (x))
2597 {
2598 case COMPONENT_REF:
2599 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2600 {
2601 error ("cannot take address of bit-field `%s'",
2602 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2603 return false;
2604 }
95602da1 2605
3e4093b6 2606 /* ... fall through ... */
95602da1 2607
3e4093b6
RS
2608 case ADDR_EXPR:
2609 case ARRAY_REF:
2610 case REALPART_EXPR:
2611 case IMAGPART_EXPR:
2612 x = TREE_OPERAND (x, 0);
2613 break;
95602da1 2614
3e4093b6
RS
2615 case COMPOUND_LITERAL_EXPR:
2616 case CONSTRUCTOR:
2617 TREE_ADDRESSABLE (x) = 1;
2618 return true;
95602da1 2619
3e4093b6
RS
2620 case VAR_DECL:
2621 case CONST_DECL:
2622 case PARM_DECL:
2623 case RESULT_DECL:
5baeaac0 2624 if (C_DECL_REGISTER (x)
3e4093b6
RS
2625 && DECL_NONLOCAL (x))
2626 {
e697b20f 2627 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6
RS
2628 {
2629 error ("global register variable `%s' used in nested function",
2630 IDENTIFIER_POINTER (DECL_NAME (x)));
2631 return false;
2632 }
2633 pedwarn ("register variable `%s' used in nested function",
2634 IDENTIFIER_POINTER (DECL_NAME (x)));
2635 }
5baeaac0 2636 else if (C_DECL_REGISTER (x))
3e4093b6 2637 {
e697b20f 2638 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6
RS
2639 {
2640 error ("address of global register variable `%s' requested",
2641 IDENTIFIER_POINTER (DECL_NAME (x)));
2642 return false;
2643 }
400fbf9f 2644
3e4093b6
RS
2645 pedwarn ("address of register variable `%s' requested",
2646 IDENTIFIER_POINTER (DECL_NAME (x)));
2647 }
2648 put_var_into_stack (x, /*rescan=*/true);
400fbf9f 2649
3e4093b6
RS
2650 /* drops in */
2651 case FUNCTION_DECL:
2652 TREE_ADDRESSABLE (x) = 1;
2653 /* drops out */
2654 default:
2655 return true;
2656 }
2657}
2658\f
2659/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
400fbf9f
JW
2660
2661tree
3e4093b6 2662build_conditional_expr (tree ifexp, tree op1, tree op2)
400fbf9f 2663{
3e4093b6
RS
2664 tree type1;
2665 tree type2;
2666 enum tree_code code1;
2667 enum tree_code code2;
2668 tree result_type = NULL;
2669 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f 2670
ae2bcd98 2671 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
3e4093b6
RS
2672
2673 /* Promote both alternatives. */
2674
2675 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2676 op1 = default_conversion (op1);
2677 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2678 op2 = default_conversion (op2);
2679
2680 if (TREE_CODE (ifexp) == ERROR_MARK
2681 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2682 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 2683 return error_mark_node;
400fbf9f 2684
3e4093b6
RS
2685 type1 = TREE_TYPE (op1);
2686 code1 = TREE_CODE (type1);
2687 type2 = TREE_TYPE (op2);
2688 code2 = TREE_CODE (type2);
2689
b1adf557
JM
2690 /* C90 does not permit non-lvalue arrays in conditional expressions.
2691 In C99 they will be pointers by now. */
2692 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2693 {
2694 error ("non-lvalue array in conditional expression");
2695 return error_mark_node;
2696 }
2697
3e4093b6
RS
2698 /* Quickly detect the usual case where op1 and op2 have the same type
2699 after promotion. */
2700 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 2701 {
3e4093b6
RS
2702 if (type1 == type2)
2703 result_type = type1;
2704 else
2705 result_type = TYPE_MAIN_VARIANT (type1);
2706 }
2707 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2708 || code1 == COMPLEX_TYPE)
2709 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2710 || code2 == COMPLEX_TYPE))
2711 {
2712 result_type = common_type (type1, type2);
400fbf9f 2713
3e4093b6
RS
2714 /* If -Wsign-compare, warn here if type1 and type2 have
2715 different signedness. We'll promote the signed to unsigned
2716 and later code won't know it used to be different.
2717 Do this check on the original types, so that explicit casts
2718 will be considered, but default promotions won't. */
2719 if (warn_sign_compare && !skip_evaluation)
ab87f8c8 2720 {
8df83eae
RK
2721 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2722 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 2723
3e4093b6
RS
2724 if (unsigned_op1 ^ unsigned_op2)
2725 {
2726 /* Do not warn if the result type is signed, since the
2727 signed type will only be chosen if it can represent
2728 all the values of the unsigned type. */
8df83eae 2729 if (! TYPE_UNSIGNED (result_type))
3e4093b6
RS
2730 /* OK */;
2731 /* Do not warn if the signed quantity is an unsuffixed
2732 integer literal (or some static constant expression
2733 involving such literals) and it is non-negative. */
2734 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2735 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2736 /* OK */;
2737 else
2738 warning ("signed and unsigned type in conditional expression");
2739 }
2740 }
2741 }
2742 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2743 {
2744 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2745 pedwarn ("ISO C forbids conditional expr with only one void side");
2746 result_type = void_type_node;
2747 }
2748 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2749 {
2750 if (comp_target_types (type1, type2, 1))
2751 result_type = common_type (type1, type2);
2752 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2753 && TREE_CODE (orig_op1) != NOP_EXPR)
2754 result_type = qualify_type (type2, type1);
2755 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2756 && TREE_CODE (orig_op2) != NOP_EXPR)
2757 result_type = qualify_type (type1, type2);
2758 else if (VOID_TYPE_P (TREE_TYPE (type1)))
34a80643 2759 {
3e4093b6
RS
2760 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2761 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2762 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2763 TREE_TYPE (type2)));
34a80643 2764 }
3e4093b6 2765 else if (VOID_TYPE_P (TREE_TYPE (type2)))
1c2a9b35 2766 {
3e4093b6
RS
2767 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2768 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2769 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2770 TREE_TYPE (type1)));
1c2a9b35 2771 }
34a80643 2772 else
ab87f8c8 2773 {
3e4093b6
RS
2774 pedwarn ("pointer type mismatch in conditional expression");
2775 result_type = build_pointer_type (void_type_node);
ab87f8c8 2776 }
3e4093b6
RS
2777 }
2778 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2779 {
2780 if (! integer_zerop (op2))
2781 pedwarn ("pointer/integer type mismatch in conditional expression");
2782 else
ab87f8c8 2783 {
3e4093b6 2784 op2 = null_pointer_node;
ab87f8c8 2785 }
3e4093b6
RS
2786 result_type = type1;
2787 }
2788 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2789 {
2790 if (!integer_zerop (op1))
2791 pedwarn ("pointer/integer type mismatch in conditional expression");
2792 else
ab87f8c8 2793 {
3e4093b6 2794 op1 = null_pointer_node;
ab87f8c8 2795 }
3e4093b6
RS
2796 result_type = type2;
2797 }
1c2a9b35 2798
3e4093b6
RS
2799 if (!result_type)
2800 {
2801 if (flag_cond_mismatch)
2802 result_type = void_type_node;
2803 else
400fbf9f 2804 {
3e4093b6 2805 error ("type mismatch in conditional expression");
ab87f8c8 2806 return error_mark_node;
400fbf9f 2807 }
3e4093b6 2808 }
400fbf9f 2809
3e4093b6
RS
2810 /* Merge const and volatile flags of the incoming types. */
2811 result_type
2812 = build_type_variant (result_type,
2813 TREE_READONLY (op1) || TREE_READONLY (op2),
2814 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
b6a10c9f 2815
3e4093b6
RS
2816 if (result_type != TREE_TYPE (op1))
2817 op1 = convert_and_check (result_type, op1);
2818 if (result_type != TREE_TYPE (op2))
2819 op2 = convert_and_check (result_type, op2);
b6a10c9f 2820
3e4093b6 2821 if (TREE_CODE (ifexp) == INTEGER_CST)
53cd18ec 2822 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2f6e4e97 2823
3e4093b6
RS
2824 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2825}
2826\f
2827/* Given a list of expressions, return a compound expression
2828 that performs them all and returns the value of the last of them. */
400fbf9f 2829
3e4093b6
RS
2830tree
2831build_compound_expr (tree list)
2832{
2833 return internal_build_compound_expr (list, TRUE);
2834}
400fbf9f 2835
3e4093b6
RS
2836static tree
2837internal_build_compound_expr (tree list, int first_p)
2838{
2839 tree rest;
400fbf9f 2840
3e4093b6
RS
2841 if (TREE_CHAIN (list) == 0)
2842 {
2843 /* Convert arrays and functions to pointers when there
2844 really is a comma operator. */
2845 if (!first_p)
2846 TREE_VALUE (list)
2847 = default_function_array_conversion (TREE_VALUE (list));
400fbf9f 2848
3e4093b6
RS
2849 /* Don't let (0, 0) be null pointer constant. */
2850 if (!first_p && integer_zerop (TREE_VALUE (list)))
2851 return non_lvalue (TREE_VALUE (list));
2852 return TREE_VALUE (list);
2853 }
400fbf9f 2854
3e4093b6 2855 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
400fbf9f 2856
3e4093b6
RS
2857 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2858 {
2859 /* The left-hand operand of a comma expression is like an expression
2860 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2861 any side-effects, unless it was explicitly cast to (void). */
2862 if (warn_unused_value
2863 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2864 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2865 warning ("left-hand operand of comma expression has no effect");
3e4093b6 2866 }
400fbf9f 2867
3e4093b6
RS
2868 /* With -Wunused, we should also warn if the left-hand operand does have
2869 side-effects, but computes a value which is not used. For example, in
2870 `foo() + bar(), baz()' the result of the `+' operator is not used,
2871 so we should issue a warning. */
2872 else if (warn_unused_value)
2873 warn_if_unused_value (TREE_VALUE (list));
400fbf9f 2874
3e4093b6
RS
2875 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2876}
400fbf9f 2877
3e4093b6 2878/* Build an expression representing a cast to type TYPE of expression EXPR. */
400fbf9f 2879
3e4093b6
RS
2880tree
2881build_c_cast (tree type, tree expr)
2882{
2883 tree value = expr;
400fbf9f 2884
3e4093b6
RS
2885 if (type == error_mark_node || expr == error_mark_node)
2886 return error_mark_node;
400fbf9f 2887
3e4093b6
RS
2888 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2889 only in <protocol> qualifications. But when constructing cast expressions,
2890 the protocols do matter and must be kept around. */
264fa2db 2891 if (!c_dialect_objc () || !objc_is_object_ptr (type))
3e4093b6 2892 type = TYPE_MAIN_VARIANT (type);
400fbf9f 2893
3e4093b6
RS
2894 if (TREE_CODE (type) == ARRAY_TYPE)
2895 {
2896 error ("cast specifies array type");
2897 return error_mark_node;
2898 }
400fbf9f 2899
3e4093b6
RS
2900 if (TREE_CODE (type) == FUNCTION_TYPE)
2901 {
2902 error ("cast specifies function type");
2903 return error_mark_node;
2904 }
400fbf9f 2905
3e4093b6
RS
2906 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2907 {
2908 if (pedantic)
400fbf9f 2909 {
3e4093b6
RS
2910 if (TREE_CODE (type) == RECORD_TYPE
2911 || TREE_CODE (type) == UNION_TYPE)
2912 pedwarn ("ISO C forbids casting nonscalar to the same type");
400fbf9f 2913 }
3e4093b6
RS
2914 }
2915 else if (TREE_CODE (type) == UNION_TYPE)
2916 {
2917 tree field;
2918 value = default_function_array_conversion (value);
400fbf9f 2919
3e4093b6
RS
2920 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2921 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2922 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2923 break;
2924
2925 if (field)
400fbf9f 2926 {
3e4093b6
RS
2927 tree t;
2928
2929 if (pedantic)
2930 pedwarn ("ISO C forbids casts to union type");
2931 t = digest_init (type,
2932 build_constructor (type,
2933 build_tree_list (field, value)),
2934 0);
2935 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2936 return t;
400fbf9f 2937 }
3e4093b6
RS
2938 error ("cast to union type from type not present in union");
2939 return error_mark_node;
2940 }
2941 else
2942 {
2943 tree otype, ovalue;
400fbf9f 2944
3e4093b6
RS
2945 /* If casting to void, avoid the error that would come
2946 from default_conversion in the case of a non-lvalue array. */
2947 if (type == void_type_node)
2948 return build1 (CONVERT_EXPR, type, value);
400fbf9f 2949
3e4093b6
RS
2950 /* Convert functions and arrays to pointers,
2951 but don't convert any other types. */
2952 value = default_function_array_conversion (value);
2953 otype = TREE_TYPE (value);
400fbf9f 2954
3e4093b6 2955 /* Optionally warn about potentially worrisome casts. */
770ae6cc 2956
3e4093b6
RS
2957 if (warn_cast_qual
2958 && TREE_CODE (type) == POINTER_TYPE
2959 && TREE_CODE (otype) == POINTER_TYPE)
2960 {
2961 tree in_type = type;
2962 tree in_otype = otype;
2963 int added = 0;
2964 int discarded = 0;
400fbf9f 2965
3e4093b6
RS
2966 /* Check that the qualifiers on IN_TYPE are a superset of
2967 the qualifiers of IN_OTYPE. The outermost level of
2968 POINTER_TYPE nodes is uninteresting and we stop as soon
2969 as we hit a non-POINTER_TYPE node on either type. */
2970 do
2971 {
2972 in_otype = TREE_TYPE (in_otype);
2973 in_type = TREE_TYPE (in_type);
400fbf9f 2974
3e4093b6
RS
2975 /* GNU C allows cv-qualified function types. 'const'
2976 means the function is very pure, 'volatile' means it
2977 can't return. We need to warn when such qualifiers
2978 are added, not when they're taken away. */
2979 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2980 && TREE_CODE (in_type) == FUNCTION_TYPE)
2981 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2982 else
2983 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2984 }
2985 while (TREE_CODE (in_type) == POINTER_TYPE
2986 && TREE_CODE (in_otype) == POINTER_TYPE);
400fbf9f 2987
3e4093b6
RS
2988 if (added)
2989 warning ("cast adds new qualifiers to function type");
400fbf9f 2990
3e4093b6
RS
2991 if (discarded)
2992 /* There are qualifiers present in IN_OTYPE that are not
2993 present in IN_TYPE. */
2994 warning ("cast discards qualifiers from pointer target type");
2995 }
400fbf9f 2996
3e4093b6
RS
2997 /* Warn about possible alignment problems. */
2998 if (STRICT_ALIGNMENT && warn_cast_align
2999 && TREE_CODE (type) == POINTER_TYPE
3000 && TREE_CODE (otype) == POINTER_TYPE
3001 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3002 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3003 /* Don't warn about opaque types, where the actual alignment
3004 restriction is unknown. */
3005 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3006 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3007 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3008 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3009 warning ("cast increases required alignment of target type");
e9a25f70 3010
3e4093b6
RS
3011 if (TREE_CODE (type) == INTEGER_TYPE
3012 && TREE_CODE (otype) == POINTER_TYPE
3013 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3014 && !TREE_CONSTANT (value))
3015 warning ("cast from pointer to integer of different size");
400fbf9f 3016
3e4093b6
RS
3017 if (warn_bad_function_cast
3018 && TREE_CODE (value) == CALL_EXPR
3019 && TREE_CODE (type) != TREE_CODE (otype))
3020 warning ("cast does not match function type");
400fbf9f 3021
3e4093b6
RS
3022 if (TREE_CODE (type) == POINTER_TYPE
3023 && TREE_CODE (otype) == INTEGER_TYPE
3024 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3025 /* Don't warn about converting any constant. */
3026 && !TREE_CONSTANT (value))
3027 warning ("cast to pointer from integer of different size");
400fbf9f 3028
3e4093b6
RS
3029 if (TREE_CODE (type) == POINTER_TYPE
3030 && TREE_CODE (otype) == POINTER_TYPE
3031 && TREE_CODE (expr) == ADDR_EXPR
3032 && DECL_P (TREE_OPERAND (expr, 0))
3033 && flag_strict_aliasing && warn_strict_aliasing
3034 && !VOID_TYPE_P (TREE_TYPE (type)))
3035 {
3036 /* Casting the address of a decl to non void pointer. Warn
3037 if the cast breaks type based aliasing. */
3038 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3039 warning ("type-punning to incomplete type might break strict-aliasing rules");
5399d643
JW
3040 else
3041 {
3042 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3043 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3044
3045 if (!alias_sets_conflict_p (set1, set2))
3046 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3047 else if (warn_strict_aliasing > 1
3048 && !alias_sets_might_conflict_p (set1, set2))
3049 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3050 }
3e4093b6 3051 }
400fbf9f 3052
3897f229
JM
3053 /* If pedantic, warn for conversions between function and object
3054 pointer types, except for converting a null pointer constant
3055 to function pointer type. */
3056 if (pedantic
3057 && TREE_CODE (type) == POINTER_TYPE
3058 && TREE_CODE (otype) == POINTER_TYPE
3059 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3060 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3061 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3062
3063 if (pedantic
3064 && TREE_CODE (type) == POINTER_TYPE
3065 && TREE_CODE (otype) == POINTER_TYPE
3066 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3067 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3068 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3069 && TREE_CODE (expr) != NOP_EXPR))
3070 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3071
3e4093b6
RS
3072 ovalue = value;
3073 /* Replace a nonvolatile const static variable with its value. */
3074 if (optimize && TREE_CODE (value) == VAR_DECL)
3075 value = decl_constant_value (value);
3076 value = convert (type, value);
400fbf9f 3077
3e4093b6
RS
3078 /* Ignore any integer overflow caused by the cast. */
3079 if (TREE_CODE (value) == INTEGER_CST)
3080 {
3081 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
22421b79
RK
3082
3083 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3084 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3e4093b6
RS
3085 }
3086 }
400fbf9f 3087
53cd18ec
JM
3088 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3089 if (TREE_CODE (value) == INTEGER_CST
3e4093b6
RS
3090 && TREE_CODE (expr) == INTEGER_CST
3091 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3092 value = non_lvalue (value);
cff9c407 3093
53cd18ec
JM
3094 /* Don't let a cast be an lvalue. */
3095 if (value == expr)
3e4093b6 3096 value = non_lvalue (value);
e9a25f70 3097
3e4093b6 3098 return value;
400fbf9f
JW
3099}
3100
3e4093b6
RS
3101/* Interpret a cast of expression EXPR to type TYPE. */
3102tree
3103c_cast_expr (tree type, tree expr)
400fbf9f 3104{
3e4093b6 3105 int saved_wsp = warn_strict_prototypes;
c5c76735 3106
3e4093b6
RS
3107 /* This avoids warnings about unprototyped casts on
3108 integers. E.g. "#define SIG_DFL (void(*)())0". */
3109 if (TREE_CODE (expr) == INTEGER_CST)
3110 warn_strict_prototypes = 0;
3111 type = groktypename (type);
3112 warn_strict_prototypes = saved_wsp;
c5c76735 3113
3e4093b6 3114 return build_c_cast (type, expr);
400fbf9f
JW
3115}
3116
3e4093b6
RS
3117\f
3118/* Build an assignment expression of lvalue LHS from value RHS.
3119 MODIFYCODE is the code for a binary operator that we use
3120 to combine the old value of LHS with RHS to get the new value.
3121 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
2f6e4e97 3122
3e4093b6
RS
3123tree
3124build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
400fbf9f 3125{
3e4093b6
RS
3126 tree result;
3127 tree newrhs;
3128 tree lhstype = TREE_TYPE (lhs);
3129 tree olhstype = lhstype;
e9a25f70 3130
3e4093b6
RS
3131 /* Types that aren't fully specified cannot be used in assignments. */
3132 lhs = require_complete_type (lhs);
e9a25f70 3133
3e4093b6
RS
3134 /* Avoid duplicate error messages from operands that had errors. */
3135 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3136 return error_mark_node;
400fbf9f 3137
3e4093b6
RS
3138 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3139 /* Do not use STRIP_NOPS here. We do not want an enumerator
3140 whose value is 0 to count as a null pointer constant. */
3141 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3142 rhs = TREE_OPERAND (rhs, 0);
e9a25f70 3143
3e4093b6 3144 newrhs = rhs;
400fbf9f 3145
3e4093b6
RS
3146 /* If a binary op has been requested, combine the old LHS value with the RHS
3147 producing the value we should actually store into the LHS. */
3148
3149 if (modifycode != NOP_EXPR)
400fbf9f 3150 {
3e4093b6
RS
3151 lhs = stabilize_reference (lhs);
3152 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
400fbf9f 3153 }
400fbf9f 3154
3e4093b6
RS
3155 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3156 return error_mark_node;
400fbf9f 3157
3e4093b6 3158 /* Warn about storing in something that is `const'. */
bbbd6700 3159
3e4093b6
RS
3160 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3161 || ((TREE_CODE (lhstype) == RECORD_TYPE
3162 || TREE_CODE (lhstype) == UNION_TYPE)
3163 && C_TYPE_FIELDS_READONLY (lhstype)))
c5b6f18e 3164 readonly_error (lhs, "assignment");
bbbd6700 3165
3e4093b6
RS
3166 /* If storing into a structure or union member,
3167 it has probably been given type `int'.
3168 Compute the type that would go with
3169 the actual amount of storage the member occupies. */
bbbd6700 3170
3e4093b6
RS
3171 if (TREE_CODE (lhs) == COMPONENT_REF
3172 && (TREE_CODE (lhstype) == INTEGER_TYPE
3173 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3174 || TREE_CODE (lhstype) == REAL_TYPE
3175 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3176 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 3177
3e4093b6
RS
3178 /* If storing in a field that is in actuality a short or narrower than one,
3179 we must store in the field in its actual type. */
3180
3181 if (lhstype != TREE_TYPE (lhs))
3182 {
3183 lhs = copy_node (lhs);
3184 TREE_TYPE (lhs) = lhstype;
400fbf9f 3185 }
400fbf9f 3186
3e4093b6 3187 /* Convert new value to destination type. */
400fbf9f 3188
3e4093b6
RS
3189 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3190 NULL_TREE, NULL_TREE, 0);
3191 if (TREE_CODE (newrhs) == ERROR_MARK)
3192 return error_mark_node;
400fbf9f 3193
3e4093b6 3194 /* Scan operands */
400fbf9f 3195
3e4093b6
RS
3196 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3197 TREE_SIDE_EFFECTS (result) = 1;
400fbf9f 3198
3e4093b6
RS
3199 /* If we got the LHS in a different type for storing in,
3200 convert the result back to the nominal type of LHS
3201 so that the value we return always has the same type
3202 as the LHS argument. */
e855c5ce 3203
3e4093b6
RS
3204 if (olhstype == TREE_TYPE (result))
3205 return result;
3206 return convert_for_assignment (olhstype, result, _("assignment"),
3207 NULL_TREE, NULL_TREE, 0);
3208}
3209\f
3210/* Convert value RHS to type TYPE as preparation for an assignment
3211 to an lvalue of type TYPE.
3212 The real work of conversion is done by `convert'.
3213 The purpose of this function is to generate error messages
3214 for assignments that are not allowed in C.
3215 ERRTYPE is a string to use in error messages:
3216 "assignment", "return", etc. If it is null, this is parameter passing
3217 for a function call (and different error messages are output).
2f6e4e97 3218
3e4093b6
RS
3219 FUNNAME is the name of the function being called,
3220 as an IDENTIFIER_NODE, or null.
3221 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 3222
3e4093b6
RS
3223static tree
3224convert_for_assignment (tree type, tree rhs, const char *errtype,
3225 tree fundecl, tree funname, int parmnum)
3226{
3227 enum tree_code codel = TREE_CODE (type);
3228 tree rhstype;
3229 enum tree_code coder;
cb3ca04e 3230
3e4093b6
RS
3231 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3232 /* Do not use STRIP_NOPS here. We do not want an enumerator
3233 whose value is 0 to count as a null pointer constant. */
3234 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3235 rhs = TREE_OPERAND (rhs, 0);
3236
3237 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3238 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3239 rhs = default_conversion (rhs);
3240 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3241 rhs = decl_constant_value_for_broken_optimization (rhs);
3242
3243 rhstype = TREE_TYPE (rhs);
3244 coder = TREE_CODE (rhstype);
3245
3246 if (coder == ERROR_MARK)
3247 return error_mark_node;
3248
3249 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
400fbf9f 3250 {
3e4093b6
RS
3251 overflow_warning (rhs);
3252 /* Check for Objective-C protocols. This will automatically
3253 issue a warning if there are protocol violations. No need to
3254 use the return value. */
3255 if (c_dialect_objc ())
3256 objc_comptypes (type, rhstype, 0);
3257 return rhs;
400fbf9f 3258 }
3e4093b6
RS
3259
3260 if (coder == VOID_TYPE)
400fbf9f 3261 {
3e4093b6
RS
3262 error ("void value not ignored as it ought to be");
3263 return error_mark_node;
400fbf9f 3264 }
3e4093b6
RS
3265 /* A type converts to a reference to it.
3266 This code doesn't fully support references, it's just for the
3267 special case of va_start and va_copy. */
3268 if (codel == REFERENCE_TYPE
3269 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
400fbf9f 3270 {
3e4093b6 3271 if (!lvalue_p (rhs))
400fbf9f 3272 {
3e4093b6
RS
3273 error ("cannot pass rvalue to reference parameter");
3274 return error_mark_node;
400fbf9f 3275 }
3e4093b6
RS
3276 if (!c_mark_addressable (rhs))
3277 return error_mark_node;
3278 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3279
3280 /* We already know that these two types are compatible, but they
3281 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3282 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3283 likely to be va_list, a typedef to __builtin_va_list, which
3284 is different enough that it will cause problems later. */
3285 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3286 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3287
3288 rhs = build1 (NOP_EXPR, type, rhs);
3289 return rhs;
400fbf9f 3290 }
3e4093b6 3291 /* Some types can interconvert without explicit casts. */
4a5eab38 3292 else if (codel == VECTOR_TYPE
cc27e657 3293 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3e4093b6
RS
3294 return convert (type, rhs);
3295 /* Arithmetic types all interconvert, and enum is treated like int. */
3296 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3297 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3298 || codel == BOOLEAN_TYPE)
3299 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3300 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3301 || coder == BOOLEAN_TYPE))
3302 return convert_and_check (type, rhs);
400fbf9f 3303
3e4093b6
RS
3304 /* Conversion to a transparent union from its member types.
3305 This applies only to function arguments. */
3306 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
400fbf9f 3307 {
3e4093b6
RS
3308 tree memb_types;
3309 tree marginal_memb_type = 0;
3310
3311 for (memb_types = TYPE_FIELDS (type); memb_types;
3312 memb_types = TREE_CHAIN (memb_types))
400fbf9f 3313 {
3e4093b6 3314 tree memb_type = TREE_TYPE (memb_types);
400fbf9f 3315
3e4093b6
RS
3316 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3317 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3318 break;
e58cd767 3319
3e4093b6
RS
3320 if (TREE_CODE (memb_type) != POINTER_TYPE)
3321 continue;
2f6e4e97 3322
3e4093b6
RS
3323 if (coder == POINTER_TYPE)
3324 {
3325 tree ttl = TREE_TYPE (memb_type);
3326 tree ttr = TREE_TYPE (rhstype);
400fbf9f 3327
3e4093b6
RS
3328 /* Any non-function converts to a [const][volatile] void *
3329 and vice versa; otherwise, targets must be the same.
3330 Meanwhile, the lhs target must have all the qualifiers of
3331 the rhs. */
3332 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3333 || comp_target_types (memb_type, rhstype, 0))
3334 {
3335 /* If this type won't generate any warnings, use it. */
3336 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3337 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3338 && TREE_CODE (ttl) == FUNCTION_TYPE)
3339 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3340 == TYPE_QUALS (ttr))
3341 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3342 == TYPE_QUALS (ttl))))
3343 break;
400fbf9f 3344
3e4093b6
RS
3345 /* Keep looking for a better type, but remember this one. */
3346 if (! marginal_memb_type)
3347 marginal_memb_type = memb_type;
3348 }
3349 }
82bde854 3350
3e4093b6
RS
3351 /* Can convert integer zero to any pointer type. */
3352 if (integer_zerop (rhs)
3353 || (TREE_CODE (rhs) == NOP_EXPR
3354 && integer_zerop (TREE_OPERAND (rhs, 0))))
3355 {
3356 rhs = null_pointer_node;
3357 break;
3358 }
3359 }
400fbf9f 3360
3e4093b6
RS
3361 if (memb_types || marginal_memb_type)
3362 {
3363 if (! memb_types)
3364 {
3365 /* We have only a marginally acceptable member type;
3366 it needs a warning. */
3367 tree ttl = TREE_TYPE (marginal_memb_type);
3368 tree ttr = TREE_TYPE (rhstype);
714a0864 3369
3e4093b6
RS
3370 /* Const and volatile mean something different for function
3371 types, so the usual warnings are not appropriate. */
3372 if (TREE_CODE (ttr) == FUNCTION_TYPE
3373 && TREE_CODE (ttl) == FUNCTION_TYPE)
3374 {
3375 /* Because const and volatile on functions are
3376 restrictions that say the function will not do
3377 certain things, it is okay to use a const or volatile
3378 function where an ordinary one is wanted, but not
3379 vice-versa. */
3380 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3381 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3382 errtype, funname, parmnum);
3383 }
3384 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3385 warn_for_assignment ("%s discards qualifiers from pointer target type",
3386 errtype, funname,
3387 parmnum);
3388 }
400fbf9f 3389
3e4093b6
RS
3390 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3391 pedwarn ("ISO C prohibits argument conversion to union type");
0e7c47fa 3392
3e4093b6
RS
3393 return build1 (NOP_EXPR, type, rhs);
3394 }
0e7c47fa
RK
3395 }
3396
3e4093b6
RS
3397 /* Conversions among pointers */
3398 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3399 && (coder == codel))
400fbf9f 3400 {
3e4093b6
RS
3401 tree ttl = TREE_TYPE (type);
3402 tree ttr = TREE_TYPE (rhstype);
3403 bool is_opaque_pointer;
264fa2db 3404 int target_cmp = 0; /* Cache comp_target_types () result. */
400fbf9f 3405
3e4093b6 3406 /* Opaque pointers are treated like void pointers. */
5fd9b178
KH
3407 is_opaque_pointer = (targetm.vector_opaque_p (type)
3408 || targetm.vector_opaque_p (rhstype))
3e4093b6
RS
3409 && TREE_CODE (ttl) == VECTOR_TYPE
3410 && TREE_CODE (ttr) == VECTOR_TYPE;
400fbf9f 3411
3e4093b6
RS
3412 /* Any non-function converts to a [const][volatile] void *
3413 and vice versa; otherwise, targets must be the same.
3414 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3415 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 3416 || (target_cmp = comp_target_types (type, rhstype, 0))
3e4093b6
RS
3417 || is_opaque_pointer
3418 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3419 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3420 {
3421 if (pedantic
3422 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3423 ||
3424 (VOID_TYPE_P (ttr)
3425 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3426 which are not ANSI null ptr constants. */
3427 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3428 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3429 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3430 errtype, funname, parmnum);
3431 /* Const and volatile mean something different for function types,
3432 so the usual warnings are not appropriate. */
3433 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3434 && TREE_CODE (ttl) != FUNCTION_TYPE)
3435 {
3436 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3437 warn_for_assignment ("%s discards qualifiers from pointer target type",
3438 errtype, funname, parmnum);
3439 /* If this is not a case of ignoring a mismatch in signedness,
3440 no warning. */
3441 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 3442 || target_cmp)
3e4093b6
RS
3443 ;
3444 /* If there is a mismatch, do warn. */
3445 else if (pedantic)
3446 warn_for_assignment ("pointer targets in %s differ in signedness",
3447 errtype, funname, parmnum);
3448 }
3449 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3450 && TREE_CODE (ttr) == FUNCTION_TYPE)
3451 {
3452 /* Because const and volatile on functions are restrictions
3453 that say the function will not do certain things,
3454 it is okay to use a const or volatile function
3455 where an ordinary one is wanted, but not vice-versa. */
3456 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3457 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3458 errtype, funname, parmnum);
3459 }
3460 }
3461 else
3462 warn_for_assignment ("%s from incompatible pointer type",
3463 errtype, funname, parmnum);
3464 return convert (type, rhs);
3465 }
b494fd98
EB
3466 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3467 {
3468 error ("invalid use of non-lvalue array");
3469 return error_mark_node;
3470 }
3e4093b6 3471 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 3472 {
3e4093b6
RS
3473 /* An explicit constant 0 can convert to a pointer,
3474 or one that results from arithmetic, even including
3475 a cast to integer type. */
3476 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3477 &&
3478 ! (TREE_CODE (rhs) == NOP_EXPR
3479 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3480 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3481 && integer_zerop (TREE_OPERAND (rhs, 0))))
3e4093b6
RS
3482 warn_for_assignment ("%s makes pointer from integer without a cast",
3483 errtype, funname, parmnum);
b3006337
EB
3484
3485 return convert (type, rhs);
400fbf9f 3486 }
3e4093b6 3487 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 3488 {
3e4093b6
RS
3489 warn_for_assignment ("%s makes integer from pointer without a cast",
3490 errtype, funname, parmnum);
3491 return convert (type, rhs);
3492 }
3493 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3494 return convert (type, rhs);
400fbf9f 3495
3e4093b6
RS
3496 if (!errtype)
3497 {
3498 if (funname)
400fbf9f 3499 {
3e4093b6 3500 tree selector = objc_message_selector ();
805f961c 3501
3e4093b6
RS
3502 if (selector && parmnum > 2)
3503 error ("incompatible type for argument %d of `%s'",
3504 parmnum - 2, IDENTIFIER_POINTER (selector));
3505 else
3506 error ("incompatible type for argument %d of `%s'",
3507 parmnum, IDENTIFIER_POINTER (funname));
400fbf9f 3508 }
3e4093b6
RS
3509 else
3510 error ("incompatible type for argument %d of indirect function call",
3511 parmnum);
400fbf9f
JW
3512 }
3513 else
3e4093b6 3514 error ("incompatible types in %s", errtype);
53b01f59 3515
3e4093b6
RS
3516 return error_mark_node;
3517}
53b01f59 3518
d5123bae
MS
3519/* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3520 is used for error and waring reporting and indicates which argument
3521 is being processed. */
400fbf9f 3522
3e4093b6 3523tree
d5123bae 3524c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3e4093b6
RS
3525{
3526 tree ret, type;
400fbf9f 3527
3e4093b6
RS
3528 /* If FN was prototyped, the value has been converted already
3529 in convert_arguments. */
3530 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3531 return value;
f5963e61 3532
3e4093b6
RS
3533 type = TREE_TYPE (parm);
3534 ret = convert_for_assignment (type, value,
3535 (char *) 0 /* arg passing */, fn,
d5123bae 3536 DECL_NAME (fn), argnum);
61f71b34 3537 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3e4093b6
RS
3538 && INTEGRAL_TYPE_P (type)
3539 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3540 ret = default_conversion (ret);
3541 return ret;
3542}
af702de8 3543
3e4093b6
RS
3544/* Print a warning using MSGID.
3545 It gets OPNAME as its one parameter.
3546 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3547 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3548 FUNCTION and ARGNUM are handled specially if we are building an
3549 Objective-C selector. */
cd6311ef 3550
3e4093b6
RS
3551static void
3552warn_for_assignment (const char *msgid, const char *opname, tree function,
3553 int argnum)
3554{
3555 if (opname == 0)
3556 {
3557 tree selector = objc_message_selector ();
3558 char * new_opname;
af702de8 3559
3e4093b6
RS
3560 if (selector && argnum > 2)
3561 {
3562 function = selector;
3563 argnum -= 2;
400fbf9f 3564 }
3e4093b6 3565 if (argnum == 0)
73a73768 3566 {
3e4093b6
RS
3567 if (function)
3568 {
3569 /* Function name is known; supply it. */
3570 const char *const argstring = _("passing arg of `%s'");
703ad42b
KG
3571 new_opname = alloca (IDENTIFIER_LENGTH (function)
3572 + strlen (argstring) + 1 + 1);
3e4093b6
RS
3573 sprintf (new_opname, argstring,
3574 IDENTIFIER_POINTER (function));
3575 }
3576 else
3577 {
3578 /* Function name unknown (call through ptr). */
3579 const char *const argnofun = _("passing arg of pointer to function");
703ad42b 3580 new_opname = alloca (strlen (argnofun) + 1 + 1);
3e4093b6
RS
3581 sprintf (new_opname, argnofun);
3582 }
73a73768 3583 }
3e4093b6 3584 else if (function)
10d5caec 3585 {
3e4093b6
RS
3586 /* Function name is known; supply it. */
3587 const char *const argstring = _("passing arg %d of `%s'");
703ad42b
KG
3588 new_opname = alloca (IDENTIFIER_LENGTH (function)
3589 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3e4093b6
RS
3590 sprintf (new_opname, argstring, argnum,
3591 IDENTIFIER_POINTER (function));
3592 }
3593 else
3594 {
3595 /* Function name unknown (call through ptr); just give arg number. */
3596 const char *const argnofun = _("passing arg %d of pointer to function");
703ad42b 3597 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3e4093b6 3598 sprintf (new_opname, argnofun, argnum);
10d5caec 3599 }
3e4093b6 3600 opname = new_opname;
400fbf9f 3601 }
3e4093b6 3602 pedwarn (msgid, opname);
400fbf9f 3603}
3e4093b6
RS
3604\f
3605/* If VALUE is a compound expr all of whose expressions are constant, then
3606 return its value. Otherwise, return error_mark_node.
15b732b2 3607
3e4093b6
RS
3608 This is for handling COMPOUND_EXPRs as initializer elements
3609 which is allowed with a warning when -pedantic is specified. */
15b732b2 3610
3e4093b6
RS
3611static tree
3612valid_compound_expr_initializer (tree value, tree endtype)
3613{
3614 if (TREE_CODE (value) == COMPOUND_EXPR)
3615 {
3616 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3617 == error_mark_node)
3618 return error_mark_node;
3619 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3620 endtype);
3621 }
3622 else if (! TREE_CONSTANT (value)
3623 && ! initializer_constant_valid_p (value, endtype))
3624 return error_mark_node;
3625 else
3626 return value;
15b732b2 3627}
400fbf9f 3628\f
3e4093b6
RS
3629/* Perform appropriate conversions on the initial value of a variable,
3630 store it in the declaration DECL,
3631 and print any error messages that are appropriate.
3632 If the init is invalid, store an ERROR_MARK. */
400fbf9f 3633
3e4093b6
RS
3634void
3635store_init_value (tree decl, tree init)
400fbf9f 3636{
3e4093b6 3637 tree value, type;
400fbf9f 3638
3e4093b6 3639 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 3640
3e4093b6
RS
3641 type = TREE_TYPE (decl);
3642 if (TREE_CODE (type) == ERROR_MARK)
3643 return;
400fbf9f 3644
3e4093b6 3645 /* Digest the specified initializer into an expression. */
400fbf9f 3646
3e4093b6 3647 value = digest_init (type, init, TREE_STATIC (decl));
400fbf9f 3648
3e4093b6 3649 /* Store the expression if valid; else report error. */
400fbf9f 3650
3e4093b6
RS
3651 if (warn_traditional && !in_system_header
3652 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3653 warning ("traditional C rejects automatic aggregate initialization");
2f6e4e97 3654
3e4093b6 3655 DECL_INITIAL (decl) = value;
400fbf9f 3656
3e4093b6
RS
3657 /* ANSI wants warnings about out-of-range constant initializers. */
3658 STRIP_TYPE_NOPS (value);
3659 constant_expression_warning (value);
400fbf9f 3660
3e4093b6
RS
3661 /* Check if we need to set array size from compound literal size. */
3662 if (TREE_CODE (type) == ARRAY_TYPE
3663 && TYPE_DOMAIN (type) == 0
3664 && value != error_mark_node)
400fbf9f 3665 {
3e4093b6
RS
3666 tree inside_init = init;
3667
3668 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3669 inside_init = TREE_OPERAND (init, 0);
3670 inside_init = fold (inside_init);
3671
3672 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3673 {
3674 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3675
3676 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3677 {
3678 /* For int foo[] = (int [3]){1}; we need to set array size
3679 now since later on array initializer will be just the
3680 brace enclosed list of the compound literal. */
3681 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3682 layout_type (type);
3683 layout_decl (decl, 0);
3684 }
3685 }
400fbf9f 3686 }
3e4093b6
RS
3687}
3688\f
3689/* Methods for storing and printing names for error messages. */
400fbf9f 3690
3e4093b6
RS
3691/* Implement a spelling stack that allows components of a name to be pushed
3692 and popped. Each element on the stack is this structure. */
400fbf9f 3693
3e4093b6
RS
3694struct spelling
3695{
3696 int kind;
3697 union
400fbf9f 3698 {
3e4093b6
RS
3699 int i;
3700 const char *s;
3701 } u;
3702};
2f6e4e97 3703
3e4093b6
RS
3704#define SPELLING_STRING 1
3705#define SPELLING_MEMBER 2
3706#define SPELLING_BOUNDS 3
400fbf9f 3707
3e4093b6
RS
3708static struct spelling *spelling; /* Next stack element (unused). */
3709static struct spelling *spelling_base; /* Spelling stack base. */
3710static int spelling_size; /* Size of the spelling stack. */
400fbf9f 3711
3e4093b6
RS
3712/* Macros to save and restore the spelling stack around push_... functions.
3713 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 3714
3e4093b6
RS
3715#define SPELLING_DEPTH() (spelling - spelling_base)
3716#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 3717
3e4093b6
RS
3718/* Push an element on the spelling stack with type KIND and assign VALUE
3719 to MEMBER. */
400fbf9f 3720
3e4093b6
RS
3721#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3722{ \
3723 int depth = SPELLING_DEPTH (); \
3724 \
3725 if (depth >= spelling_size) \
3726 { \
3727 spelling_size += 10; \
3728 if (spelling_base == 0) \
703ad42b 3729 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3e4093b6 3730 else \
703ad42b
KG
3731 spelling_base = xrealloc (spelling_base, \
3732 spelling_size * sizeof (struct spelling)); \
3e4093b6
RS
3733 RESTORE_SPELLING_DEPTH (depth); \
3734 } \
3735 \
3736 spelling->kind = (KIND); \
3737 spelling->MEMBER = (VALUE); \
3738 spelling++; \
3739}
400fbf9f 3740
3e4093b6 3741/* Push STRING on the stack. Printed literally. */
400fbf9f 3742
3e4093b6
RS
3743static void
3744push_string (const char *string)
3745{
3746 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3747}
400fbf9f 3748
3e4093b6 3749/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 3750
3e4093b6
RS
3751static void
3752push_member_name (tree decl)
3753{
3754 const char *const string
3755 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3756 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3757}
400fbf9f 3758
3e4093b6 3759/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 3760
3e4093b6
RS
3761static void
3762push_array_bounds (int bounds)
3763{
3764 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3765}
bb58bec5 3766
3e4093b6 3767/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 3768
3e4093b6
RS
3769static int
3770spelling_length (void)
3771{
3772 int size = 0;
3773 struct spelling *p;
400fbf9f 3774
3e4093b6
RS
3775 for (p = spelling_base; p < spelling; p++)
3776 {
3777 if (p->kind == SPELLING_BOUNDS)
3778 size += 25;
3779 else
3780 size += strlen (p->u.s) + 1;
3781 }
3782
3783 return size;
400fbf9f 3784}
400fbf9f 3785
3e4093b6 3786/* Print the spelling to BUFFER and return it. */
400fbf9f 3787
3e4093b6
RS
3788static char *
3789print_spelling (char *buffer)
400fbf9f 3790{
3e4093b6
RS
3791 char *d = buffer;
3792 struct spelling *p;
400fbf9f 3793
3e4093b6
RS
3794 for (p = spelling_base; p < spelling; p++)
3795 if (p->kind == SPELLING_BOUNDS)
3796 {
3797 sprintf (d, "[%d]", p->u.i);
3798 d += strlen (d);
3799 }
3800 else
3801 {
3802 const char *s;
3803 if (p->kind == SPELLING_MEMBER)
3804 *d++ = '.';
3805 for (s = p->u.s; (*d = *s++); d++)
3806 ;
3807 }
3808 *d++ = '\0';
3809 return buffer;
3810}
400fbf9f 3811
3e4093b6
RS
3812/* Issue an error message for a bad initializer component.
3813 MSGID identifies the message.
3814 The component name is taken from the spelling stack. */
400fbf9f 3815
3e4093b6
RS
3816void
3817error_init (const char *msgid)
3818{
3819 char *ofwhat;
400fbf9f 3820
3e4093b6 3821 error ("%s", _(msgid));
703ad42b 3822 ofwhat = print_spelling (alloca (spelling_length () + 1));
3e4093b6
RS
3823 if (*ofwhat)
3824 error ("(near initialization for `%s')", ofwhat);
3825}
400fbf9f 3826
3e4093b6
RS
3827/* Issue a pedantic warning for a bad initializer component.
3828 MSGID identifies the message.
3829 The component name is taken from the spelling stack. */
400fbf9f 3830
3e4093b6
RS
3831void
3832pedwarn_init (const char *msgid)
3833{
3834 char *ofwhat;
9f720c3e 3835
3e4093b6 3836 pedwarn ("%s", _(msgid));
703ad42b 3837 ofwhat = print_spelling (alloca (spelling_length () + 1));
3e4093b6
RS
3838 if (*ofwhat)
3839 pedwarn ("(near initialization for `%s')", ofwhat);
3840}
9f720c3e 3841
3e4093b6
RS
3842/* Issue a warning for a bad initializer component.
3843 MSGID identifies the message.
3844 The component name is taken from the spelling stack. */
61179109 3845
3e4093b6
RS
3846static void
3847warning_init (const char *msgid)
3848{
3849 char *ofwhat;
7e842ef8 3850
3e4093b6 3851 warning ("%s", _(msgid));
703ad42b 3852 ofwhat = print_spelling (alloca (spelling_length () + 1));
3e4093b6
RS
3853 if (*ofwhat)
3854 warning ("(near initialization for `%s')", ofwhat);
3855}
3856\f
3857/* Digest the parser output INIT as an initializer for type TYPE.
3858 Return a C expression of type TYPE to represent the initial value.
7e842ef8 3859
3e4093b6
RS
3860 REQUIRE_CONSTANT requests an error if non-constant initializers or
3861 elements are seen. */
7e842ef8 3862
3e4093b6
RS
3863static tree
3864digest_init (tree type, tree init, int require_constant)
3865{
3866 enum tree_code code = TREE_CODE (type);
3867 tree inside_init = init;
7e842ef8 3868
3e4093b6
RS
3869 if (type == error_mark_node
3870 || init == error_mark_node
3871 || TREE_TYPE (init) == error_mark_node)
3872 return error_mark_node;
7e842ef8 3873
3e4093b6
RS
3874 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3875 /* Do not use STRIP_NOPS here. We do not want an enumerator
3876 whose value is 0 to count as a null pointer constant. */
3877 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3878 inside_init = TREE_OPERAND (init, 0);
7e842ef8 3879
3e4093b6 3880 inside_init = fold (inside_init);
7e842ef8 3881
3e4093b6
RS
3882 /* Initialization of an array of chars from a string constant
3883 optionally enclosed in braces. */
7e842ef8 3884
3e4093b6
RS
3885 if (code == ARRAY_TYPE)
3886 {
3887 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3888 if ((typ1 == char_type_node
3889 || typ1 == signed_char_type_node
3890 || typ1 == unsigned_char_type_node
3891 || typ1 == unsigned_wchar_type_node
3892 || typ1 == signed_wchar_type_node)
3893 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
7e842ef8 3894 {
3e4093b6
RS
3895 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3896 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3897 return inside_init;
7e842ef8 3898
3e4093b6
RS
3899 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3900 != char_type_node)
3901 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3902 {
3903 error_init ("char-array initialized from wide string");
3904 return error_mark_node;
3905 }
3906 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3907 == char_type_node)
3908 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3909 {
3910 error_init ("int-array initialized from non-wide string");
3911 return error_mark_node;
7e842ef8 3912 }
2f6e4e97 3913
3e4093b6
RS
3914 TREE_TYPE (inside_init) = type;
3915 if (TYPE_DOMAIN (type) != 0
3916 && TYPE_SIZE (type) != 0
3917 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3918 /* Subtract 1 (or sizeof (wchar_t))
3919 because it's ok to ignore the terminating null char
3920 that is counted in the length of the constant. */
3921 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3922 TREE_STRING_LENGTH (inside_init)
3923 - ((TYPE_PRECISION (typ1)
3924 != TYPE_PRECISION (char_type_node))
3925 ? (TYPE_PRECISION (wchar_type_node)
3926 / BITS_PER_UNIT)
3927 : 1)))
3928 pedwarn_init ("initializer-string for array of chars is too long");
7e842ef8 3929
3e4093b6 3930 return inside_init;
7e842ef8
PE
3931 }
3932 }
3933
3e4093b6
RS
3934 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3935 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3936 below and handle as a constructor. */
3937 if (code == VECTOR_TYPE
cc27e657 3938 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
3e4093b6
RS
3939 && TREE_CONSTANT (inside_init))
3940 {
3941 if (TREE_CODE (inside_init) == VECTOR_CST
cc27e657 3942 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3e4093b6
RS
3943 TYPE_MAIN_VARIANT (type),
3944 COMPARE_STRICT))
3945 return inside_init;
3946 else
3947 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3948 }
6035d635 3949
3e4093b6
RS
3950 /* Any type can be initialized
3951 from an expression of the same type, optionally with braces. */
400fbf9f 3952
3e4093b6
RS
3953 if (inside_init && TREE_TYPE (inside_init) != 0
3954 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3955 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3956 || (code == ARRAY_TYPE
3957 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3958 || (code == VECTOR_TYPE
3959 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3960 || (code == POINTER_TYPE
3897f229 3961 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 3962 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3897f229
JM
3963 TREE_TYPE (type), COMPARE_STRICT))
3964 || (code == POINTER_TYPE
3965 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3966 && comptypes (TREE_TYPE (inside_init),
3e4093b6
RS
3967 TREE_TYPE (type), COMPARE_STRICT))))
3968 {
3969 if (code == POINTER_TYPE)
b494fd98
EB
3970 {
3971 inside_init = default_function_array_conversion (inside_init);
3972
3973 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3974 {
3975 error_init ("invalid use of non-lvalue array");
3976 return error_mark_node;
3977 }
3978 }
3979
bae39a73
NS
3980 if (code == VECTOR_TYPE)
3981 /* Although the types are compatible, we may require a
3982 conversion. */
3983 inside_init = convert (type, inside_init);
3e4093b6
RS
3984
3985 if (require_constant && !flag_isoc99
3986 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 3987 {
3e4093b6
RS
3988 /* As an extension, allow initializing objects with static storage
3989 duration with compound literals (which are then treated just as
3990 the brace enclosed list they contain). */
3991 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3992 inside_init = DECL_INITIAL (decl);
400fbf9f 3993 }
3e4093b6
RS
3994
3995 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3996 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 3997 {
3e4093b6
RS
3998 error_init ("array initialized from non-constant array expression");
3999 return error_mark_node;
400fbf9f 4000 }
400fbf9f 4001
3e4093b6
RS
4002 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4003 inside_init = decl_constant_value_for_broken_optimization (inside_init);
2f6e4e97 4004
3e4093b6
RS
4005 /* Compound expressions can only occur here if -pedantic or
4006 -pedantic-errors is specified. In the later case, we always want
4007 an error. In the former case, we simply want a warning. */
4008 if (require_constant && pedantic
4009 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4010 {
4011 inside_init
4012 = valid_compound_expr_initializer (inside_init,
4013 TREE_TYPE (inside_init));
4014 if (inside_init == error_mark_node)
4015 error_init ("initializer element is not constant");
2f6e4e97 4016 else
3e4093b6
RS
4017 pedwarn_init ("initializer element is not constant");
4018 if (flag_pedantic_errors)
4019 inside_init = error_mark_node;
4020 }
4021 else if (require_constant
4022 && (!TREE_CONSTANT (inside_init)
4023 /* This test catches things like `7 / 0' which
4024 result in an expression for which TREE_CONSTANT
4025 is true, but which is not actually something
4026 that is a legal constant. We really should not
4027 be using this function, because it is a part of
4028 the back-end. Instead, the expression should
4029 already have been turned into ERROR_MARK_NODE. */
4030 || !initializer_constant_valid_p (inside_init,
4031 TREE_TYPE (inside_init))))
4032 {
4033 error_init ("initializer element is not constant");
4034 inside_init = error_mark_node;
8b40563c 4035 }
f735a153 4036
3e4093b6
RS
4037 return inside_init;
4038 }
f735a153 4039
3e4093b6 4040 /* Handle scalar types, including conversions. */
400fbf9f 4041
3e4093b6 4042 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
cc27e657
PB
4043 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4044 || code == VECTOR_TYPE)
400fbf9f 4045 {
3e4093b6
RS
4046 /* Note that convert_for_assignment calls default_conversion
4047 for arrays and functions. We must not call it in the
4048 case where inside_init is a null pointer constant. */
4049 inside_init
4050 = convert_for_assignment (type, init, _("initialization"),
4051 NULL_TREE, NULL_TREE, 0);
2f6e4e97 4052
3e4093b6 4053 if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f 4054 {
3e4093b6
RS
4055 error_init ("initializer element is not constant");
4056 inside_init = error_mark_node;
400fbf9f 4057 }
3e4093b6
RS
4058 else if (require_constant
4059 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f 4060 {
3e4093b6
RS
4061 error_init ("initializer element is not computable at load time");
4062 inside_init = error_mark_node;
400fbf9f 4063 }
3e4093b6
RS
4064
4065 return inside_init;
400fbf9f 4066 }
d9fc6069 4067
3e4093b6 4068 /* Come here only for records and arrays. */
d9fc6069 4069
3e4093b6 4070 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 4071 {
3e4093b6
RS
4072 error_init ("variable-sized object may not be initialized");
4073 return error_mark_node;
d9fc6069 4074 }
3e4093b6
RS
4075
4076 error_init ("invalid initializer");
4077 return error_mark_node;
d9fc6069 4078}
400fbf9f 4079\f
3e4093b6 4080/* Handle initializers that use braces. */
400fbf9f 4081
3e4093b6
RS
4082/* Type of object we are accumulating a constructor for.
4083 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4084static tree constructor_type;
400fbf9f 4085
3e4093b6
RS
4086/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4087 left to fill. */
4088static tree constructor_fields;
400fbf9f 4089
3e4093b6
RS
4090/* For an ARRAY_TYPE, this is the specified index
4091 at which to store the next element we get. */
4092static tree constructor_index;
400fbf9f 4093
3e4093b6
RS
4094/* For an ARRAY_TYPE, this is the maximum index. */
4095static tree constructor_max_index;
400fbf9f 4096
3e4093b6
RS
4097/* For a RECORD_TYPE, this is the first field not yet written out. */
4098static tree constructor_unfilled_fields;
400fbf9f 4099
3e4093b6
RS
4100/* For an ARRAY_TYPE, this is the index of the first element
4101 not yet written out. */
4102static tree constructor_unfilled_index;
895ea614 4103
3e4093b6
RS
4104/* In a RECORD_TYPE, the byte index of the next consecutive field.
4105 This is so we can generate gaps between fields, when appropriate. */
4106static tree constructor_bit_index;
10d5caec 4107
3e4093b6
RS
4108/* If we are saving up the elements rather than allocating them,
4109 this is the list of elements so far (in reverse order,
4110 most recent first). */
4111static tree constructor_elements;
ad47f1e5 4112
3e4093b6
RS
4113/* 1 if constructor should be incrementally stored into a constructor chain,
4114 0 if all the elements should be kept in AVL tree. */
4115static int constructor_incremental;
ad47f1e5 4116
3e4093b6
RS
4117/* 1 if so far this constructor's elements are all compile-time constants. */
4118static int constructor_constant;
ad47f1e5 4119
3e4093b6
RS
4120/* 1 if so far this constructor's elements are all valid address constants. */
4121static int constructor_simple;
ad47f1e5 4122
3e4093b6
RS
4123/* 1 if this constructor is erroneous so far. */
4124static int constructor_erroneous;
d45cf215 4125
3e4093b6
RS
4126/* Structure for managing pending initializer elements, organized as an
4127 AVL tree. */
d45cf215 4128
3e4093b6 4129struct init_node
d45cf215 4130{
3e4093b6
RS
4131 struct init_node *left, *right;
4132 struct init_node *parent;
4133 int balance;
4134 tree purpose;
4135 tree value;
d45cf215
RS
4136};
4137
3e4093b6
RS
4138/* Tree of pending elements at this constructor level.
4139 These are elements encountered out of order
4140 which belong at places we haven't reached yet in actually
4141 writing the output.
4142 Will never hold tree nodes across GC runs. */
4143static struct init_node *constructor_pending_elts;
d45cf215 4144
3e4093b6
RS
4145/* The SPELLING_DEPTH of this constructor. */
4146static int constructor_depth;
d45cf215 4147
3e4093b6
RS
4148/* 0 if implicitly pushing constructor levels is allowed. */
4149int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
d45cf215 4150
3e4093b6
RS
4151static int require_constant_value;
4152static int require_constant_elements;
d45cf215 4153
3e4093b6
RS
4154/* DECL node for which an initializer is being read.
4155 0 means we are reading a constructor expression
4156 such as (struct foo) {...}. */
4157static tree constructor_decl;
d45cf215 4158
839ee4bc
RO
4159/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4160static const char *constructor_asmspec;
4161
3e4093b6
RS
4162/* Nonzero if this is an initializer for a top-level decl. */
4163static int constructor_top_level;
d45cf215 4164
3e4093b6
RS
4165/* Nonzero if there were any member designators in this initializer. */
4166static int constructor_designated;
d45cf215 4167
3e4093b6
RS
4168/* Nesting depth of designator list. */
4169static int designator_depth;
d45cf215 4170
3e4093b6
RS
4171/* Nonzero if there were diagnosed errors in this designator list. */
4172static int designator_errorneous;
d45cf215 4173
3e4093b6
RS
4174\f
4175/* This stack has a level for each implicit or explicit level of
4176 structuring in the initializer, including the outermost one. It
4177 saves the values of most of the variables above. */
d45cf215 4178
3e4093b6
RS
4179struct constructor_range_stack;
4180
4181struct constructor_stack
d45cf215 4182{
3e4093b6
RS
4183 struct constructor_stack *next;
4184 tree type;
4185 tree fields;
4186 tree index;
4187 tree max_index;
4188 tree unfilled_index;
4189 tree unfilled_fields;
4190 tree bit_index;
4191 tree elements;
4192 struct init_node *pending_elts;
4193 int offset;
4194 int depth;
4195 /* If nonzero, this value should replace the entire
4196 constructor at this level. */
4197 tree replacement_value;
4198 struct constructor_range_stack *range_stack;
4199 char constant;
4200 char simple;
4201 char implicit;
4202 char erroneous;
4203 char outer;
4204 char incremental;
4205 char designated;
4206};
d45cf215 4207
3e4093b6 4208struct constructor_stack *constructor_stack;
d45cf215 4209
3e4093b6
RS
4210/* This stack represents designators from some range designator up to
4211 the last designator in the list. */
d45cf215 4212
3e4093b6
RS
4213struct constructor_range_stack
4214{
4215 struct constructor_range_stack *next, *prev;
4216 struct constructor_stack *stack;
4217 tree range_start;
4218 tree index;
4219 tree range_end;
4220 tree fields;
4221};
d45cf215 4222
3e4093b6 4223struct constructor_range_stack *constructor_range_stack;
d45cf215 4224
3e4093b6
RS
4225/* This stack records separate initializers that are nested.
4226 Nested initializers can't happen in ANSI C, but GNU C allows them
4227 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 4228
3e4093b6 4229struct initializer_stack
d45cf215 4230{
3e4093b6
RS
4231 struct initializer_stack *next;
4232 tree decl;
839ee4bc 4233 const char *asmspec;
3e4093b6
RS
4234 struct constructor_stack *constructor_stack;
4235 struct constructor_range_stack *constructor_range_stack;
4236 tree elements;
4237 struct spelling *spelling;
4238 struct spelling *spelling_base;
4239 int spelling_size;
4240 char top_level;
4241 char require_constant_value;
4242 char require_constant_elements;
4243};
d45cf215 4244
3e4093b6
RS
4245struct initializer_stack *initializer_stack;
4246\f
4247/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
4248
4249void
839ee4bc 4250start_init (tree decl, tree asmspec_tree, int top_level)
400fbf9f 4251{
3e4093b6 4252 const char *locus;
703ad42b 4253 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
839ee4bc
RO
4254 const char *asmspec = 0;
4255
4256 if (asmspec_tree)
4257 asmspec = TREE_STRING_POINTER (asmspec_tree);
400fbf9f 4258
3e4093b6 4259 p->decl = constructor_decl;
839ee4bc 4260 p->asmspec = constructor_asmspec;
3e4093b6
RS
4261 p->require_constant_value = require_constant_value;
4262 p->require_constant_elements = require_constant_elements;
4263 p->constructor_stack = constructor_stack;
4264 p->constructor_range_stack = constructor_range_stack;
4265 p->elements = constructor_elements;
4266 p->spelling = spelling;
4267 p->spelling_base = spelling_base;
4268 p->spelling_size = spelling_size;
4269 p->top_level = constructor_top_level;
4270 p->next = initializer_stack;
4271 initializer_stack = p;
400fbf9f 4272
3e4093b6 4273 constructor_decl = decl;
839ee4bc 4274 constructor_asmspec = asmspec;
3e4093b6
RS
4275 constructor_designated = 0;
4276 constructor_top_level = top_level;
400fbf9f 4277
3e4093b6
RS
4278 if (decl != 0)
4279 {
4280 require_constant_value = TREE_STATIC (decl);
4281 require_constant_elements
4282 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4283 /* For a scalar, you can always use any value to initialize,
4284 even within braces. */
4285 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4286 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4287 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4288 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4289 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4290 }
4291 else
4292 {
4293 require_constant_value = 0;
4294 require_constant_elements = 0;
4295 locus = "(anonymous)";
4296 }
b71c7f8a 4297
3e4093b6
RS
4298 constructor_stack = 0;
4299 constructor_range_stack = 0;
b71c7f8a 4300
3e4093b6
RS
4301 missing_braces_mentioned = 0;
4302
4303 spelling_base = 0;
4304 spelling_size = 0;
4305 RESTORE_SPELLING_DEPTH (0);
4306
4307 if (locus)
4308 push_string (locus);
4309}
4310
4311void
4312finish_init (void)
b71c7f8a 4313{
3e4093b6 4314 struct initializer_stack *p = initializer_stack;
b71c7f8a 4315
3e4093b6
RS
4316 /* Free the whole constructor stack of this initializer. */
4317 while (constructor_stack)
4318 {
4319 struct constructor_stack *q = constructor_stack;
4320 constructor_stack = q->next;
4321 free (q);
4322 }
4323
4324 if (constructor_range_stack)
4325 abort ();
4326
4327 /* Pop back to the data of the outer initializer (if any). */
36579663
AP
4328 free (spelling_base);
4329
3e4093b6 4330 constructor_decl = p->decl;
839ee4bc 4331 constructor_asmspec = p->asmspec;
3e4093b6
RS
4332 require_constant_value = p->require_constant_value;
4333 require_constant_elements = p->require_constant_elements;
4334 constructor_stack = p->constructor_stack;
4335 constructor_range_stack = p->constructor_range_stack;
4336 constructor_elements = p->elements;
4337 spelling = p->spelling;
4338 spelling_base = p->spelling_base;
4339 spelling_size = p->spelling_size;
4340 constructor_top_level = p->top_level;
4341 initializer_stack = p->next;
4342 free (p);
b71c7f8a 4343}
400fbf9f 4344\f
3e4093b6
RS
4345/* Call here when we see the initializer is surrounded by braces.
4346 This is instead of a call to push_init_level;
4347 it is matched by a call to pop_init_level.
400fbf9f 4348
3e4093b6
RS
4349 TYPE is the type to initialize, for a constructor expression.
4350 For an initializer for a decl, TYPE is zero. */
400fbf9f 4351
3e4093b6
RS
4352void
4353really_start_incremental_init (tree type)
400fbf9f 4354{
703ad42b 4355 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
400fbf9f 4356
3e4093b6
RS
4357 if (type == 0)
4358 type = TREE_TYPE (constructor_decl);
400fbf9f 4359
5fd9b178 4360 if (targetm.vector_opaque_p (type))
3e4093b6 4361 error ("opaque vector types cannot be initialized");
400fbf9f 4362
3e4093b6
RS
4363 p->type = constructor_type;
4364 p->fields = constructor_fields;
4365 p->index = constructor_index;
4366 p->max_index = constructor_max_index;
4367 p->unfilled_index = constructor_unfilled_index;
4368 p->unfilled_fields = constructor_unfilled_fields;
4369 p->bit_index = constructor_bit_index;
4370 p->elements = constructor_elements;
4371 p->constant = constructor_constant;
4372 p->simple = constructor_simple;
4373 p->erroneous = constructor_erroneous;
4374 p->pending_elts = constructor_pending_elts;
4375 p->depth = constructor_depth;
4376 p->replacement_value = 0;
4377 p->implicit = 0;
4378 p->range_stack = 0;
4379 p->outer = 0;
4380 p->incremental = constructor_incremental;
4381 p->designated = constructor_designated;
4382 p->next = 0;
4383 constructor_stack = p;
b13aca19 4384
3e4093b6
RS
4385 constructor_constant = 1;
4386 constructor_simple = 1;
4387 constructor_depth = SPELLING_DEPTH ();
4388 constructor_elements = 0;
4389 constructor_pending_elts = 0;
4390 constructor_type = type;
4391 constructor_incremental = 1;
4392 constructor_designated = 0;
4393 designator_depth = 0;
4394 designator_errorneous = 0;
400fbf9f 4395
3e4093b6
RS
4396 if (TREE_CODE (constructor_type) == RECORD_TYPE
4397 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 4398 {
3e4093b6
RS
4399 constructor_fields = TYPE_FIELDS (constructor_type);
4400 /* Skip any nameless bit fields at the beginning. */
4401 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4402 && DECL_NAME (constructor_fields) == 0)
4403 constructor_fields = TREE_CHAIN (constructor_fields);
05bccae2 4404
3e4093b6
RS
4405 constructor_unfilled_fields = constructor_fields;
4406 constructor_bit_index = bitsize_zero_node;
400fbf9f 4407 }
3e4093b6
RS
4408 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4409 {
4410 if (TYPE_DOMAIN (constructor_type))
4411 {
4412 constructor_max_index
4413 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 4414
3e4093b6
RS
4415 /* Detect non-empty initializations of zero-length arrays. */
4416 if (constructor_max_index == NULL_TREE
4417 && TYPE_SIZE (constructor_type))
4418 constructor_max_index = build_int_2 (-1, -1);
400fbf9f 4419
3e4093b6
RS
4420 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4421 to initialize VLAs will cause a proper error; avoid tree
4422 checking errors as well by setting a safe value. */
4423 if (constructor_max_index
4424 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4425 constructor_max_index = build_int_2 (-1, -1);
59c83dbf 4426
3e4093b6
RS
4427 constructor_index
4428 = convert (bitsizetype,
4429 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 4430 }
3e4093b6
RS
4431 else
4432 constructor_index = bitsize_zero_node;
59c83dbf 4433
3e4093b6
RS
4434 constructor_unfilled_index = constructor_index;
4435 }
4436 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4437 {
4438 /* Vectors are like simple fixed-size arrays. */
4439 constructor_max_index =
4440 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4441 constructor_index = convert (bitsizetype, bitsize_zero_node);
4442 constructor_unfilled_index = constructor_index;
4443 }
4444 else
4445 {
4446 /* Handle the case of int x = {5}; */
4447 constructor_fields = constructor_type;
4448 constructor_unfilled_fields = constructor_type;
4449 }
4450}
4451\f
4452/* Push down into a subobject, for initialization.
4453 If this is for an explicit set of braces, IMPLICIT is 0.
4454 If it is because the next element belongs at a lower level,
4455 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 4456
3e4093b6
RS
4457void
4458push_init_level (int implicit)
4459{
4460 struct constructor_stack *p;
4461 tree value = NULL_TREE;
400fbf9f 4462
3e4093b6
RS
4463 /* If we've exhausted any levels that didn't have braces,
4464 pop them now. */
4465 while (constructor_stack->implicit)
4466 {
4467 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4468 || TREE_CODE (constructor_type) == UNION_TYPE)
4469 && constructor_fields == 0)
4470 process_init_element (pop_init_level (1));
4471 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4472 && constructor_max_index
4473 && tree_int_cst_lt (constructor_max_index, constructor_index))
4474 process_init_element (pop_init_level (1));
4475 else
4476 break;
4477 }
400fbf9f 4478
3e4093b6
RS
4479 /* Unless this is an explicit brace, we need to preserve previous
4480 content if any. */
4481 if (implicit)
4482 {
4483 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4484 || TREE_CODE (constructor_type) == UNION_TYPE)
4485 && constructor_fields)
4486 value = find_init_member (constructor_fields);
4487 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4488 value = find_init_member (constructor_index);
400fbf9f
JW
4489 }
4490
703ad42b 4491 p = xmalloc (sizeof (struct constructor_stack));
3e4093b6
RS
4492 p->type = constructor_type;
4493 p->fields = constructor_fields;
4494 p->index = constructor_index;
4495 p->max_index = constructor_max_index;
4496 p->unfilled_index = constructor_unfilled_index;
4497 p->unfilled_fields = constructor_unfilled_fields;
4498 p->bit_index = constructor_bit_index;
4499 p->elements = constructor_elements;
4500 p->constant = constructor_constant;
4501 p->simple = constructor_simple;
4502 p->erroneous = constructor_erroneous;
4503 p->pending_elts = constructor_pending_elts;
4504 p->depth = constructor_depth;
4505 p->replacement_value = 0;
4506 p->implicit = implicit;
4507 p->outer = 0;
4508 p->incremental = constructor_incremental;
4509 p->designated = constructor_designated;
4510 p->next = constructor_stack;
4511 p->range_stack = 0;
4512 constructor_stack = p;
400fbf9f 4513
3e4093b6
RS
4514 constructor_constant = 1;
4515 constructor_simple = 1;
4516 constructor_depth = SPELLING_DEPTH ();
4517 constructor_elements = 0;
4518 constructor_incremental = 1;
4519 constructor_designated = 0;
4520 constructor_pending_elts = 0;
4521 if (!implicit)
400fbf9f 4522 {
3e4093b6
RS
4523 p->range_stack = constructor_range_stack;
4524 constructor_range_stack = 0;
4525 designator_depth = 0;
4526 designator_errorneous = 0;
4527 }
400fbf9f 4528
3e4093b6
RS
4529 /* Don't die if an entire brace-pair level is superfluous
4530 in the containing level. */
4531 if (constructor_type == 0)
4532 ;
4533 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4534 || TREE_CODE (constructor_type) == UNION_TYPE)
4535 {
4536 /* Don't die if there are extra init elts at the end. */
4537 if (constructor_fields == 0)
4538 constructor_type = 0;
4539 else
400fbf9f 4540 {
3e4093b6
RS
4541 constructor_type = TREE_TYPE (constructor_fields);
4542 push_member_name (constructor_fields);
4543 constructor_depth++;
400fbf9f 4544 }
3e4093b6
RS
4545 }
4546 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4547 {
4548 constructor_type = TREE_TYPE (constructor_type);
4549 push_array_bounds (tree_low_cst (constructor_index, 0));
4550 constructor_depth++;
400fbf9f
JW
4551 }
4552
3e4093b6 4553 if (constructor_type == 0)
400fbf9f 4554 {
3e4093b6
RS
4555 error_init ("extra brace group at end of initializer");
4556 constructor_fields = 0;
4557 constructor_unfilled_fields = 0;
4558 return;
400fbf9f
JW
4559 }
4560
3e4093b6
RS
4561 if (value && TREE_CODE (value) == CONSTRUCTOR)
4562 {
4563 constructor_constant = TREE_CONSTANT (value);
4564 constructor_simple = TREE_STATIC (value);
4565 constructor_elements = CONSTRUCTOR_ELTS (value);
4566 if (constructor_elements
4567 && (TREE_CODE (constructor_type) == RECORD_TYPE
4568 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4569 set_nonincremental_init ();
4570 }
400fbf9f 4571
3e4093b6
RS
4572 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4573 {
4574 missing_braces_mentioned = 1;
4575 warning_init ("missing braces around initializer");
4576 }
400fbf9f 4577
3e4093b6
RS
4578 if (TREE_CODE (constructor_type) == RECORD_TYPE
4579 || TREE_CODE (constructor_type) == UNION_TYPE)
4580 {
4581 constructor_fields = TYPE_FIELDS (constructor_type);
4582 /* Skip any nameless bit fields at the beginning. */
4583 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4584 && DECL_NAME (constructor_fields) == 0)
4585 constructor_fields = TREE_CHAIN (constructor_fields);
103b7b17 4586
3e4093b6
RS
4587 constructor_unfilled_fields = constructor_fields;
4588 constructor_bit_index = bitsize_zero_node;
4589 }
4590 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4591 {
4592 /* Vectors are like simple fixed-size arrays. */
4593 constructor_max_index =
4594 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4595 constructor_index = convert (bitsizetype, integer_zero_node);
4596 constructor_unfilled_index = constructor_index;
4597 }
4598 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4599 {
4600 if (TYPE_DOMAIN (constructor_type))
4601 {
4602 constructor_max_index
4603 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 4604
3e4093b6
RS
4605 /* Detect non-empty initializations of zero-length arrays. */
4606 if (constructor_max_index == NULL_TREE
4607 && TYPE_SIZE (constructor_type))
4608 constructor_max_index = build_int_2 (-1, -1);
de520661 4609
3e4093b6
RS
4610 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4611 to initialize VLAs will cause a proper error; avoid tree
4612 checking errors as well by setting a safe value. */
4613 if (constructor_max_index
4614 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4615 constructor_max_index = build_int_2 (-1, -1);
b62acd60 4616
3e4093b6
RS
4617 constructor_index
4618 = convert (bitsizetype,
4619 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4620 }
4621 else
4622 constructor_index = bitsize_zero_node;
de520661 4623
3e4093b6
RS
4624 constructor_unfilled_index = constructor_index;
4625 if (value && TREE_CODE (value) == STRING_CST)
4626 {
4627 /* We need to split the char/wchar array into individual
4628 characters, so that we don't have to special case it
4629 everywhere. */
4630 set_nonincremental_init_from_string (value);
4631 }
4632 }
4633 else
4634 {
4635 warning_init ("braces around scalar initializer");
4636 constructor_fields = constructor_type;
4637 constructor_unfilled_fields = constructor_type;
4638 }
4639}
8b6a5902 4640
3e4093b6
RS
4641/* At the end of an implicit or explicit brace level,
4642 finish up that level of constructor.
4643 If we were outputting the elements as they are read, return 0
4644 from inner levels (process_init_element ignores that),
4645 but return error_mark_node from the outermost level
4646 (that's what we want to put in DECL_INITIAL).
4647 Otherwise, return a CONSTRUCTOR expression. */
de520661 4648
3e4093b6
RS
4649tree
4650pop_init_level (int implicit)
4651{
4652 struct constructor_stack *p;
4653 tree constructor = 0;
de520661 4654
3e4093b6
RS
4655 if (implicit == 0)
4656 {
4657 /* When we come to an explicit close brace,
4658 pop any inner levels that didn't have explicit braces. */
4659 while (constructor_stack->implicit)
4660 process_init_element (pop_init_level (1));
de520661 4661
3e4093b6
RS
4662 if (constructor_range_stack)
4663 abort ();
4664 }
e5e809f4 4665
0066ef9c
RH
4666 /* Now output all pending elements. */
4667 constructor_incremental = 1;
4668 output_pending_init_elements (1);
4669
3e4093b6 4670 p = constructor_stack;
e5e809f4 4671
3e4093b6
RS
4672 /* Error for initializing a flexible array member, or a zero-length
4673 array member in an inappropriate context. */
4674 if (constructor_type && constructor_fields
4675 && TREE_CODE (constructor_type) == ARRAY_TYPE
4676 && TYPE_DOMAIN (constructor_type)
4677 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4678 {
4679 /* Silently discard empty initializations. The parser will
4680 already have pedwarned for empty brackets. */
4681 if (integer_zerop (constructor_unfilled_index))
4682 constructor_type = NULL_TREE;
4683 else if (! TYPE_SIZE (constructor_type))
4684 {
4685 if (constructor_depth > 2)
4686 error_init ("initialization of flexible array member in a nested context");
4687 else if (pedantic)
4688 pedwarn_init ("initialization of a flexible array member");
de520661 4689
3e4093b6
RS
4690 /* We have already issued an error message for the existence
4691 of a flexible array member not at the end of the structure.
4692 Discard the initializer so that we do not abort later. */
4693 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4694 constructor_type = NULL_TREE;
4695 }
4696 else
4697 /* Zero-length arrays are no longer special, so we should no longer
4698 get here. */
4699 abort ();
4700 }
de520661 4701
3e4093b6
RS
4702 /* Warn when some struct elements are implicitly initialized to zero. */
4703 if (extra_warnings
4704 && constructor_type
4705 && TREE_CODE (constructor_type) == RECORD_TYPE
4706 && constructor_unfilled_fields)
4707 {
4708 /* Do not warn for flexible array members or zero-length arrays. */
4709 while (constructor_unfilled_fields
4710 && (! DECL_SIZE (constructor_unfilled_fields)
4711 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4712 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
cc77d4d5 4713
3e4093b6
RS
4714 /* Do not warn if this level of the initializer uses member
4715 designators; it is likely to be deliberate. */
4716 if (constructor_unfilled_fields && !constructor_designated)
4717 {
4718 push_member_name (constructor_unfilled_fields);
4719 warning_init ("missing initializer");
4720 RESTORE_SPELLING_DEPTH (constructor_depth);
4721 }
4722 }
de520661 4723
3e4093b6
RS
4724 /* Pad out the end of the structure. */
4725 if (p->replacement_value)
4726 /* If this closes a superfluous brace pair,
4727 just pass out the element between them. */
4728 constructor = p->replacement_value;
4729 else if (constructor_type == 0)
4730 ;
4731 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4732 && TREE_CODE (constructor_type) != UNION_TYPE
4733 && TREE_CODE (constructor_type) != ARRAY_TYPE
4734 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4735 {
4736 /* A nonincremental scalar initializer--just return
4737 the element, after verifying there is just one. */
4738 if (constructor_elements == 0)
4739 {
4740 if (!constructor_erroneous)
4741 error_init ("empty scalar initializer");
4742 constructor = error_mark_node;
4743 }
4744 else if (TREE_CHAIN (constructor_elements) != 0)
4745 {
4746 error_init ("extra elements in scalar initializer");
4747 constructor = TREE_VALUE (constructor_elements);
4748 }
4749 else
4750 constructor = TREE_VALUE (constructor_elements);
4751 }
4752 else
4753 {
4754 if (constructor_erroneous)
4755 constructor = error_mark_node;
4756 else
4757 {
4758 constructor = build_constructor (constructor_type,
4759 nreverse (constructor_elements));
4760 if (constructor_constant)
4761 TREE_CONSTANT (constructor) = 1;
4762 if (constructor_constant && constructor_simple)
4763 TREE_STATIC (constructor) = 1;
4764 }
4765 }
de520661 4766
3e4093b6
RS
4767 constructor_type = p->type;
4768 constructor_fields = p->fields;
4769 constructor_index = p->index;
4770 constructor_max_index = p->max_index;
4771 constructor_unfilled_index = p->unfilled_index;
4772 constructor_unfilled_fields = p->unfilled_fields;
4773 constructor_bit_index = p->bit_index;
4774 constructor_elements = p->elements;
4775 constructor_constant = p->constant;
4776 constructor_simple = p->simple;
4777 constructor_erroneous = p->erroneous;
4778 constructor_incremental = p->incremental;
4779 constructor_designated = p->designated;
4780 constructor_pending_elts = p->pending_elts;
4781 constructor_depth = p->depth;
4782 if (!p->implicit)
4783 constructor_range_stack = p->range_stack;
4784 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 4785
3e4093b6
RS
4786 constructor_stack = p->next;
4787 free (p);
b621a4dd 4788
3e4093b6
RS
4789 if (constructor == 0)
4790 {
4791 if (constructor_stack == 0)
4792 return error_mark_node;
4793 return NULL_TREE;
4794 }
4795 return constructor;
4796}
8b6a5902 4797
3e4093b6
RS
4798/* Common handling for both array range and field name designators.
4799 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 4800
3e4093b6
RS
4801static int
4802set_designator (int array)
de520661 4803{
3e4093b6
RS
4804 tree subtype;
4805 enum tree_code subcode;
de520661 4806
3e4093b6
RS
4807 /* Don't die if an entire brace-pair level is superfluous
4808 in the containing level. */
4809 if (constructor_type == 0)
4810 return 1;
de520661 4811
3e4093b6
RS
4812 /* If there were errors in this designator list already, bail out silently. */
4813 if (designator_errorneous)
4814 return 1;
e28cae4f 4815
3e4093b6
RS
4816 if (!designator_depth)
4817 {
4818 if (constructor_range_stack)
4819 abort ();
de520661 4820
3e4093b6
RS
4821 /* Designator list starts at the level of closest explicit
4822 braces. */
4823 while (constructor_stack->implicit)
4824 process_init_element (pop_init_level (1));
4825 constructor_designated = 1;
4826 return 0;
4827 }
de520661 4828
3e4093b6
RS
4829 if (constructor_no_implicit)
4830 {
4831 error_init ("initialization designators may not nest");
4832 return 1;
4833 }
de520661 4834
3e4093b6
RS
4835 if (TREE_CODE (constructor_type) == RECORD_TYPE
4836 || TREE_CODE (constructor_type) == UNION_TYPE)
3c3fa147 4837 {
3e4093b6
RS
4838 subtype = TREE_TYPE (constructor_fields);
4839 if (subtype != error_mark_node)
4840 subtype = TYPE_MAIN_VARIANT (subtype);
3c3fa147 4841 }
3e4093b6 4842 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 4843 {
3e4093b6 4844 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
de520661 4845 }
3e4093b6
RS
4846 else
4847 abort ();
400fbf9f 4848
3e4093b6
RS
4849 subcode = TREE_CODE (subtype);
4850 if (array && subcode != ARRAY_TYPE)
4851 {
4852 error_init ("array index in non-array initializer");
4853 return 1;
4854 }
4855 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4856 {
4857 error_init ("field name not in record or union initializer");
4858 return 1;
4859 }
d45cf215 4860
3e4093b6
RS
4861 constructor_designated = 1;
4862 push_init_level (2);
4863 return 0;
de520661 4864}
400fbf9f 4865
3e4093b6
RS
4866/* If there are range designators in designator list, push a new designator
4867 to constructor_range_stack. RANGE_END is end of such stack range or
4868 NULL_TREE if there is no range designator at this level. */
400fbf9f 4869
3e4093b6
RS
4870static void
4871push_range_stack (tree range_end)
4872{
4873 struct constructor_range_stack *p;
400fbf9f 4874
703ad42b 4875 p = ggc_alloc (sizeof (struct constructor_range_stack));
3e4093b6
RS
4876 p->prev = constructor_range_stack;
4877 p->next = 0;
4878 p->fields = constructor_fields;
4879 p->range_start = constructor_index;
4880 p->index = constructor_index;
4881 p->stack = constructor_stack;
4882 p->range_end = range_end;
8b6a5902 4883 if (constructor_range_stack)
3e4093b6
RS
4884 constructor_range_stack->next = p;
4885 constructor_range_stack = p;
de520661 4886}
400fbf9f 4887
3e4093b6
RS
4888/* Within an array initializer, specify the next index to be initialized.
4889 FIRST is that index. If LAST is nonzero, then initialize a range
4890 of indices, running from FIRST through LAST. */
5a7ec9d9 4891
de520661 4892void
3e4093b6 4893set_init_index (tree first, tree last)
de520661 4894{
3e4093b6
RS
4895 if (set_designator (1))
4896 return;
de520661 4897
3e4093b6 4898 designator_errorneous = 1;
de520661 4899
3e4093b6
RS
4900 while ((TREE_CODE (first) == NOP_EXPR
4901 || TREE_CODE (first) == CONVERT_EXPR
4902 || TREE_CODE (first) == NON_LVALUE_EXPR)
4903 && (TYPE_MODE (TREE_TYPE (first))
4904 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4905 first = TREE_OPERAND (first, 0);
c8e4f0e9 4906
3e4093b6
RS
4907 if (last)
4908 while ((TREE_CODE (last) == NOP_EXPR
4909 || TREE_CODE (last) == CONVERT_EXPR
4910 || TREE_CODE (last) == NON_LVALUE_EXPR)
4911 && (TYPE_MODE (TREE_TYPE (last))
4912 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4913 last = TREE_OPERAND (last, 0);
de520661 4914
3e4093b6
RS
4915 if (TREE_CODE (first) != INTEGER_CST)
4916 error_init ("nonconstant array index in initializer");
4917 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4918 error_init ("nonconstant array index in initializer");
4919 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4920 error_init ("array index in non-array initializer");
622adc7e
MK
4921 else if (tree_int_cst_sgn (first) == -1)
4922 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
4923 else if (constructor_max_index
4924 && tree_int_cst_lt (constructor_max_index, first))
4925 error_init ("array index in initializer exceeds array bounds");
4926 else
de520661 4927 {
3e4093b6 4928 constructor_index = convert (bitsizetype, first);
665f2503 4929
3e4093b6 4930 if (last)
2bede729 4931 {
3e4093b6
RS
4932 if (tree_int_cst_equal (first, last))
4933 last = 0;
4934 else if (tree_int_cst_lt (last, first))
4935 {
4936 error_init ("empty index range in initializer");
4937 last = 0;
4938 }
4939 else
4940 {
4941 last = convert (bitsizetype, last);
4942 if (constructor_max_index != 0
4943 && tree_int_cst_lt (constructor_max_index, last))
4944 {
4945 error_init ("array index range in initializer exceeds array bounds");
4946 last = 0;
4947 }
4948 }
2bede729 4949 }
fed3cef0 4950
3e4093b6
RS
4951 designator_depth++;
4952 designator_errorneous = 0;
4953 if (constructor_range_stack || last)
4954 push_range_stack (last);
de520661 4955 }
de520661 4956}
3e4093b6
RS
4957
4958/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 4959
de520661 4960void
3e4093b6 4961set_init_label (tree fieldname)
de520661 4962{
3e4093b6 4963 tree tail;
94ba5069 4964
3e4093b6
RS
4965 if (set_designator (0))
4966 return;
4967
4968 designator_errorneous = 1;
4969
4970 if (TREE_CODE (constructor_type) != RECORD_TYPE
4971 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 4972 {
3e4093b6
RS
4973 error_init ("field name not in record or union initializer");
4974 return;
94ba5069
RS
4975 }
4976
3e4093b6
RS
4977 for (tail = TYPE_FIELDS (constructor_type); tail;
4978 tail = TREE_CHAIN (tail))
8b6a5902 4979 {
3e4093b6
RS
4980 if (DECL_NAME (tail) == fieldname)
4981 break;
8b6a5902
JJ
4982 }
4983
3e4093b6
RS
4984 if (tail == 0)
4985 error ("unknown field `%s' specified in initializer",
4986 IDENTIFIER_POINTER (fieldname));
4987 else
8b6a5902 4988 {
3e4093b6
RS
4989 constructor_fields = tail;
4990 designator_depth++;
8b6a5902 4991 designator_errorneous = 0;
3e4093b6
RS
4992 if (constructor_range_stack)
4993 push_range_stack (NULL_TREE);
8b6a5902 4994 }
3e4093b6
RS
4995}
4996\f
4997/* Add a new initializer to the tree of pending initializers. PURPOSE
4998 identifies the initializer, either array index or field in a structure.
4999 VALUE is the value of that index or field. */
de520661 5000
3e4093b6
RS
5001static void
5002add_pending_init (tree purpose, tree value)
5003{
5004 struct init_node *p, **q, *r;
5005
5006 q = &constructor_pending_elts;
5007 p = 0;
5008
5009 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 5010 {
3e4093b6 5011 while (*q != 0)
91fa3c30 5012 {
3e4093b6
RS
5013 p = *q;
5014 if (tree_int_cst_lt (purpose, p->purpose))
5015 q = &p->left;
5016 else if (tree_int_cst_lt (p->purpose, purpose))
5017 q = &p->right;
5018 else
5019 {
5020 if (TREE_SIDE_EFFECTS (p->value))
5021 warning_init ("initialized field with side-effects overwritten");
5022 p->value = value;
5023 return;
5024 }
91fa3c30 5025 }
de520661 5026 }
3e4093b6 5027 else
de520661 5028 {
3e4093b6 5029 tree bitpos;
400fbf9f 5030
3e4093b6
RS
5031 bitpos = bit_position (purpose);
5032 while (*q != NULL)
5033 {
5034 p = *q;
5035 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5036 q = &p->left;
5037 else if (p->purpose != purpose)
5038 q = &p->right;
5039 else
5040 {
5041 if (TREE_SIDE_EFFECTS (p->value))
5042 warning_init ("initialized field with side-effects overwritten");
5043 p->value = value;
5044 return;
5045 }
5046 }
91fa3c30 5047 }
b71c7f8a 5048
703ad42b 5049 r = ggc_alloc (sizeof (struct init_node));
3e4093b6
RS
5050 r->purpose = purpose;
5051 r->value = value;
8b6a5902 5052
3e4093b6
RS
5053 *q = r;
5054 r->parent = p;
5055 r->left = 0;
5056 r->right = 0;
5057 r->balance = 0;
b71c7f8a 5058
3e4093b6 5059 while (p)
de520661 5060 {
3e4093b6 5061 struct init_node *s;
665f2503 5062
3e4093b6 5063 if (r == p->left)
2bede729 5064 {
3e4093b6
RS
5065 if (p->balance == 0)
5066 p->balance = -1;
5067 else if (p->balance < 0)
5068 {
5069 if (r->balance < 0)
5070 {
5071 /* L rotation. */
5072 p->left = r->right;
5073 if (p->left)
5074 p->left->parent = p;
5075 r->right = p;
e7b6a0ee 5076
3e4093b6
RS
5077 p->balance = 0;
5078 r->balance = 0;
39bc99c2 5079
3e4093b6
RS
5080 s = p->parent;
5081 p->parent = r;
5082 r->parent = s;
5083 if (s)
5084 {
5085 if (s->left == p)
5086 s->left = r;
5087 else
5088 s->right = r;
5089 }
5090 else
5091 constructor_pending_elts = r;
5092 }
5093 else
5094 {
5095 /* LR rotation. */
5096 struct init_node *t = r->right;
e7b6a0ee 5097
3e4093b6
RS
5098 r->right = t->left;
5099 if (r->right)
5100 r->right->parent = r;
5101 t->left = r;
5102
5103 p->left = t->right;
5104 if (p->left)
5105 p->left->parent = p;
5106 t->right = p;
5107
5108 p->balance = t->balance < 0;
5109 r->balance = -(t->balance > 0);
5110 t->balance = 0;
5111
5112 s = p->parent;
5113 p->parent = t;
5114 r->parent = t;
5115 t->parent = s;
5116 if (s)
5117 {
5118 if (s->left == p)
5119 s->left = t;
5120 else
5121 s->right = t;
5122 }
5123 else
5124 constructor_pending_elts = t;
5125 }
5126 break;
5127 }
5128 else
5129 {
5130 /* p->balance == +1; growth of left side balances the node. */
5131 p->balance = 0;
5132 break;
5133 }
2bede729 5134 }
3e4093b6
RS
5135 else /* r == p->right */
5136 {
5137 if (p->balance == 0)
5138 /* Growth propagation from right side. */
5139 p->balance++;
5140 else if (p->balance > 0)
5141 {
5142 if (r->balance > 0)
5143 {
5144 /* R rotation. */
5145 p->right = r->left;
5146 if (p->right)
5147 p->right->parent = p;
5148 r->left = p;
5149
5150 p->balance = 0;
5151 r->balance = 0;
5152
5153 s = p->parent;
5154 p->parent = r;
5155 r->parent = s;
5156 if (s)
5157 {
5158 if (s->left == p)
5159 s->left = r;
5160 else
5161 s->right = r;
5162 }
5163 else
5164 constructor_pending_elts = r;
5165 }
5166 else /* r->balance == -1 */
5167 {
5168 /* RL rotation */
5169 struct init_node *t = r->left;
5170
5171 r->left = t->right;
5172 if (r->left)
5173 r->left->parent = r;
5174 t->right = r;
5175
5176 p->right = t->left;
5177 if (p->right)
5178 p->right->parent = p;
5179 t->left = p;
5180
5181 r->balance = (t->balance < 0);
5182 p->balance = -(t->balance > 0);
5183 t->balance = 0;
5184
5185 s = p->parent;
5186 p->parent = t;
5187 r->parent = t;
5188 t->parent = s;
5189 if (s)
5190 {
5191 if (s->left == p)
5192 s->left = t;
5193 else
5194 s->right = t;
5195 }
5196 else
5197 constructor_pending_elts = t;
5198 }
5199 break;
5200 }
5201 else
5202 {
5203 /* p->balance == -1; growth of right side balances the node. */
5204 p->balance = 0;
5205 break;
5206 }
5207 }
5208
5209 r = p;
5210 p = p->parent;
5211 }
5212}
5213
5214/* Build AVL tree from a sorted chain. */
5215
5216static void
5217set_nonincremental_init (void)
5218{
5219 tree chain;
5220
5221 if (TREE_CODE (constructor_type) != RECORD_TYPE
5222 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5223 return;
5224
5225 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5226 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5227 constructor_elements = 0;
5228 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5229 {
5230 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5231 /* Skip any nameless bit fields at the beginning. */
5232 while (constructor_unfilled_fields != 0
5233 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5234 && DECL_NAME (constructor_unfilled_fields) == 0)
5235 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 5236
de520661 5237 }
3e4093b6 5238 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 5239 {
3e4093b6
RS
5240 if (TYPE_DOMAIN (constructor_type))
5241 constructor_unfilled_index
5242 = convert (bitsizetype,
5243 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5244 else
5245 constructor_unfilled_index = bitsize_zero_node;
de520661 5246 }
3e4093b6 5247 constructor_incremental = 0;
de520661 5248}
400fbf9f 5249
3e4093b6 5250/* Build AVL tree from a string constant. */
de520661 5251
3e4093b6
RS
5252static void
5253set_nonincremental_init_from_string (tree str)
de520661 5254{
3e4093b6
RS
5255 tree value, purpose, type;
5256 HOST_WIDE_INT val[2];
5257 const char *p, *end;
5258 int byte, wchar_bytes, charwidth, bitpos;
de520661 5259
3e4093b6
RS
5260 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5261 abort ();
940ff66d 5262
3e4093b6
RS
5263 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5264 == TYPE_PRECISION (char_type_node))
5265 wchar_bytes = 1;
5266 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5267 == TYPE_PRECISION (wchar_type_node))
5268 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5269 else
5270 abort ();
400fbf9f 5271
3e4093b6
RS
5272 charwidth = TYPE_PRECISION (char_type_node);
5273 type = TREE_TYPE (constructor_type);
5274 p = TREE_STRING_POINTER (str);
5275 end = p + TREE_STRING_LENGTH (str);
91fa3c30 5276
3e4093b6
RS
5277 for (purpose = bitsize_zero_node;
5278 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5279 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 5280 {
3e4093b6 5281 if (wchar_bytes == 1)
ffc5c6a9 5282 {
3e4093b6
RS
5283 val[1] = (unsigned char) *p++;
5284 val[0] = 0;
ffc5c6a9
RH
5285 }
5286 else
3e4093b6
RS
5287 {
5288 val[0] = 0;
5289 val[1] = 0;
5290 for (byte = 0; byte < wchar_bytes; byte++)
5291 {
5292 if (BYTES_BIG_ENDIAN)
5293 bitpos = (wchar_bytes - byte - 1) * charwidth;
5294 else
5295 bitpos = byte * charwidth;
5296 val[bitpos < HOST_BITS_PER_WIDE_INT]
5297 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5298 << (bitpos % HOST_BITS_PER_WIDE_INT);
5299 }
5300 }
584ef5fe 5301
8df83eae 5302 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
5303 {
5304 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5305 if (bitpos < HOST_BITS_PER_WIDE_INT)
5306 {
5307 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5308 {
5309 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5310 val[0] = -1;
5311 }
5312 }
5313 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5314 {
5315 if (val[1] < 0)
5316 val[0] = -1;
5317 }
5318 else if (val[0] & (((HOST_WIDE_INT) 1)
5319 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5320 val[0] |= ((HOST_WIDE_INT) -1)
5321 << (bitpos - HOST_BITS_PER_WIDE_INT);
5322 }
ffc5c6a9 5323
3e4093b6
RS
5324 value = build_int_2 (val[1], val[0]);
5325 TREE_TYPE (value) = type;
5326 add_pending_init (purpose, value);
9dfcc8db
BH
5327 }
5328
3e4093b6
RS
5329 constructor_incremental = 0;
5330}
de520661 5331
3e4093b6
RS
5332/* Return value of FIELD in pending initializer or zero if the field was
5333 not initialized yet. */
5334
5335static tree
5336find_init_member (tree field)
5337{
5338 struct init_node *p;
5339
5340 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 5341 {
3e4093b6
RS
5342 if (constructor_incremental
5343 && tree_int_cst_lt (field, constructor_unfilled_index))
5344 set_nonincremental_init ();
5345
5346 p = constructor_pending_elts;
5347 while (p)
19d76e60 5348 {
3e4093b6
RS
5349 if (tree_int_cst_lt (field, p->purpose))
5350 p = p->left;
5351 else if (tree_int_cst_lt (p->purpose, field))
5352 p = p->right;
5353 else
5354 return p->value;
19d76e60 5355 }
19d76e60 5356 }
3e4093b6 5357 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 5358 {
3e4093b6 5359 tree bitpos = bit_position (field);
de520661 5360
3e4093b6
RS
5361 if (constructor_incremental
5362 && (!constructor_unfilled_fields
5363 || tree_int_cst_lt (bitpos,
5364 bit_position (constructor_unfilled_fields))))
5365 set_nonincremental_init ();
de520661 5366
3e4093b6
RS
5367 p = constructor_pending_elts;
5368 while (p)
5369 {
5370 if (field == p->purpose)
5371 return p->value;
5372 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5373 p = p->left;
5374 else
5375 p = p->right;
5376 }
5377 }
5378 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 5379 {
3e4093b6
RS
5380 if (constructor_elements
5381 && TREE_PURPOSE (constructor_elements) == field)
5382 return TREE_VALUE (constructor_elements);
de520661 5383 }
3e4093b6 5384 return 0;
de520661
RS
5385}
5386
3e4093b6
RS
5387/* "Output" the next constructor element.
5388 At top level, really output it to assembler code now.
5389 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5390 TYPE is the data type that the containing data type wants here.
5391 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8b6a5902 5392
3e4093b6
RS
5393 PENDING if non-nil means output pending elements that belong
5394 right after this element. (PENDING is normally 1;
5395 it is 0 while outputting pending elements, to avoid recursion.) */
8b6a5902 5396
3e4093b6
RS
5397static void
5398output_init_element (tree value, tree type, tree field, int pending)
5399{
5400 if (type == error_mark_node)
8b6a5902 5401 {
3e4093b6
RS
5402 constructor_erroneous = 1;
5403 return;
8b6a5902 5404 }
3e4093b6
RS
5405 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5406 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5407 && !(TREE_CODE (value) == STRING_CST
5408 && TREE_CODE (type) == ARRAY_TYPE
5409 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5410 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5411 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5412 value = default_conversion (value);
8b6a5902 5413
3e4093b6
RS
5414 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5415 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 5416 {
3e4093b6
RS
5417 /* As an extension, allow initializing objects with static storage
5418 duration with compound literals (which are then treated just as
5419 the brace enclosed list they contain). */
5420 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5421 value = DECL_INITIAL (decl);
8b6a5902
JJ
5422 }
5423
3e4093b6
RS
5424 if (value == error_mark_node)
5425 constructor_erroneous = 1;
5426 else if (!TREE_CONSTANT (value))
5427 constructor_constant = 0;
5428 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5429 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5430 || TREE_CODE (constructor_type) == UNION_TYPE)
5431 && DECL_C_BIT_FIELD (field)
5432 && TREE_CODE (value) != INTEGER_CST))
5433 constructor_simple = 0;
5434
5435 if (require_constant_value && ! TREE_CONSTANT (value))
8b6a5902 5436 {
3e4093b6
RS
5437 error_init ("initializer element is not constant");
5438 value = error_mark_node;
8b6a5902 5439 }
3e4093b6
RS
5440 else if (require_constant_elements
5441 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5442 pedwarn ("initializer element is not computable at load time");
5443
5444 /* If this field is empty (and not at the end of structure),
5445 don't do anything other than checking the initializer. */
5446 if (field
5447 && (TREE_TYPE (field) == error_mark_node
5448 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5449 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5450 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5451 || TREE_CHAIN (field)))))
5452 return;
5453
5454 value = digest_init (type, value, require_constant_value);
5455 if (value == error_mark_node)
8b6a5902 5456 {
3e4093b6
RS
5457 constructor_erroneous = 1;
5458 return;
8b6a5902 5459 }
8b6a5902 5460
3e4093b6
RS
5461 /* If this element doesn't come next in sequence,
5462 put it on constructor_pending_elts. */
5463 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5464 && (!constructor_incremental
5465 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 5466 {
3e4093b6
RS
5467 if (constructor_incremental
5468 && tree_int_cst_lt (field, constructor_unfilled_index))
5469 set_nonincremental_init ();
5470
5471 add_pending_init (field, value);
5472 return;
8b6a5902 5473 }
3e4093b6
RS
5474 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5475 && (!constructor_incremental
5476 || field != constructor_unfilled_fields))
8b6a5902 5477 {
3e4093b6
RS
5478 /* We do this for records but not for unions. In a union,
5479 no matter which field is specified, it can be initialized
5480 right away since it starts at the beginning of the union. */
5481 if (constructor_incremental)
5482 {
5483 if (!constructor_unfilled_fields)
5484 set_nonincremental_init ();
5485 else
5486 {
5487 tree bitpos, unfillpos;
5488
5489 bitpos = bit_position (field);
5490 unfillpos = bit_position (constructor_unfilled_fields);
5491
5492 if (tree_int_cst_lt (bitpos, unfillpos))
5493 set_nonincremental_init ();
5494 }
5495 }
5496
5497 add_pending_init (field, value);
5498 return;
8b6a5902 5499 }
3e4093b6
RS
5500 else if (TREE_CODE (constructor_type) == UNION_TYPE
5501 && constructor_elements)
5502 {
5503 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5504 warning_init ("initialized field with side-effects overwritten");
8b6a5902 5505
3e4093b6
RS
5506 /* We can have just one union field set. */
5507 constructor_elements = 0;
5508 }
8b6a5902 5509
3e4093b6
RS
5510 /* Otherwise, output this element either to
5511 constructor_elements or to the assembler file. */
8b6a5902 5512
3e4093b6
RS
5513 if (field && TREE_CODE (field) == INTEGER_CST)
5514 field = copy_node (field);
5515 constructor_elements
5516 = tree_cons (field, value, constructor_elements);
8b6a5902 5517
3e4093b6
RS
5518 /* Advance the variable that indicates sequential elements output. */
5519 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5520 constructor_unfilled_index
5521 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5522 bitsize_one_node);
5523 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5524 {
5525 constructor_unfilled_fields
5526 = TREE_CHAIN (constructor_unfilled_fields);
8b6a5902 5527
3e4093b6
RS
5528 /* Skip any nameless bit fields. */
5529 while (constructor_unfilled_fields != 0
5530 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5531 && DECL_NAME (constructor_unfilled_fields) == 0)
5532 constructor_unfilled_fields =
5533 TREE_CHAIN (constructor_unfilled_fields);
5534 }
5535 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5536 constructor_unfilled_fields = 0;
de520661 5537
3e4093b6
RS
5538 /* Now output any pending elements which have become next. */
5539 if (pending)
5540 output_pending_init_elements (0);
5541}
8b6a5902 5542
3e4093b6
RS
5543/* Output any pending elements which have become next.
5544 As we output elements, constructor_unfilled_{fields,index}
5545 advances, which may cause other elements to become next;
5546 if so, they too are output.
8b6a5902 5547
3e4093b6
RS
5548 If ALL is 0, we return when there are
5549 no more pending elements to output now.
665f2503 5550
3e4093b6
RS
5551 If ALL is 1, we output space as necessary so that
5552 we can output all the pending elements. */
19d76e60 5553
3e4093b6
RS
5554static void
5555output_pending_init_elements (int all)
5556{
5557 struct init_node *elt = constructor_pending_elts;
5558 tree next;
de520661 5559
3e4093b6
RS
5560 retry:
5561
ba228239 5562 /* Look through the whole pending tree.
3e4093b6
RS
5563 If we find an element that should be output now,
5564 output it. Otherwise, set NEXT to the element
5565 that comes first among those still pending. */
5566
5567 next = 0;
5568 while (elt)
5569 {
5570 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 5571 {
3e4093b6
RS
5572 if (tree_int_cst_equal (elt->purpose,
5573 constructor_unfilled_index))
5574 output_init_element (elt->value,
5575 TREE_TYPE (constructor_type),
5576 constructor_unfilled_index, 0);
5577 else if (tree_int_cst_lt (constructor_unfilled_index,
5578 elt->purpose))
8b6a5902 5579 {
3e4093b6
RS
5580 /* Advance to the next smaller node. */
5581 if (elt->left)
5582 elt = elt->left;
5583 else
5584 {
5585 /* We have reached the smallest node bigger than the
5586 current unfilled index. Fill the space first. */
5587 next = elt->purpose;
5588 break;
5589 }
8b6a5902 5590 }
ce662d4c
JJ
5591 else
5592 {
3e4093b6
RS
5593 /* Advance to the next bigger node. */
5594 if (elt->right)
5595 elt = elt->right;
5596 else
ce662d4c 5597 {
3e4093b6
RS
5598 /* We have reached the biggest node in a subtree. Find
5599 the parent of it, which is the next bigger node. */
5600 while (elt->parent && elt->parent->right == elt)
5601 elt = elt->parent;
5602 elt = elt->parent;
5603 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5604 elt->purpose))
5605 {
5606 next = elt->purpose;
5607 break;
5608 }
ce662d4c
JJ
5609 }
5610 }
8b6a5902 5611 }
3e4093b6
RS
5612 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5613 || TREE_CODE (constructor_type) == UNION_TYPE)
5614 {
5615 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 5616
3e4093b6
RS
5617 /* If the current record is complete we are done. */
5618 if (constructor_unfilled_fields == 0)
5619 break;
de520661 5620
3e4093b6
RS
5621 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5622 elt_bitpos = bit_position (elt->purpose);
5623 /* We can't compare fields here because there might be empty
5624 fields in between. */
5625 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5626 {
5627 constructor_unfilled_fields = elt->purpose;
5628 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5629 elt->purpose, 0);
5630 }
5631 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5632 {
5633 /* Advance to the next smaller node. */
5634 if (elt->left)
5635 elt = elt->left;
5636 else
5637 {
5638 /* We have reached the smallest node bigger than the
5639 current unfilled field. Fill the space first. */
5640 next = elt->purpose;
5641 break;
5642 }
5643 }
5644 else
5645 {
5646 /* Advance to the next bigger node. */
5647 if (elt->right)
5648 elt = elt->right;
5649 else
5650 {
5651 /* We have reached the biggest node in a subtree. Find
5652 the parent of it, which is the next bigger node. */
5653 while (elt->parent && elt->parent->right == elt)
5654 elt = elt->parent;
5655 elt = elt->parent;
5656 if (elt
5657 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5658 bit_position (elt->purpose))))
5659 {
5660 next = elt->purpose;
5661 break;
5662 }
5663 }
5664 }
5665 }
5666 }
de520661 5667
3e4093b6
RS
5668 /* Ordinarily return, but not if we want to output all
5669 and there are elements left. */
5670 if (! (all && next != 0))
e5cfb88f
RK
5671 return;
5672
3e4093b6
RS
5673 /* If it's not incremental, just skip over the gap, so that after
5674 jumping to retry we will output the next successive element. */
5675 if (TREE_CODE (constructor_type) == RECORD_TYPE
5676 || TREE_CODE (constructor_type) == UNION_TYPE)
5677 constructor_unfilled_fields = next;
5678 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5679 constructor_unfilled_index = next;
de520661 5680
3e4093b6
RS
5681 /* ELT now points to the node in the pending tree with the next
5682 initializer to output. */
5683 goto retry;
de520661
RS
5684}
5685\f
3e4093b6
RS
5686/* Add one non-braced element to the current constructor level.
5687 This adjusts the current position within the constructor's type.
5688 This may also start or terminate implicit levels
5689 to handle a partly-braced initializer.
e5e809f4 5690
3e4093b6
RS
5691 Once this has found the correct level for the new element,
5692 it calls output_init_element. */
5693
5694void
5695process_init_element (tree value)
e5e809f4 5696{
3e4093b6
RS
5697 tree orig_value = value;
5698 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
e5e809f4 5699
3e4093b6
RS
5700 designator_depth = 0;
5701 designator_errorneous = 0;
e5e809f4 5702
3e4093b6
RS
5703 /* Handle superfluous braces around string cst as in
5704 char x[] = {"foo"}; */
5705 if (string_flag
5706 && constructor_type
5707 && TREE_CODE (constructor_type) == ARRAY_TYPE
5708 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5709 && integer_zerop (constructor_unfilled_index))
e5e809f4 5710 {
3e4093b6
RS
5711 if (constructor_stack->replacement_value)
5712 error_init ("excess elements in char array initializer");
5713 constructor_stack->replacement_value = value;
5714 return;
e5e809f4 5715 }
8b6a5902 5716
3e4093b6
RS
5717 if (constructor_stack->replacement_value != 0)
5718 {
5719 error_init ("excess elements in struct initializer");
5720 return;
e5e809f4
JL
5721 }
5722
3e4093b6
RS
5723 /* Ignore elements of a brace group if it is entirely superfluous
5724 and has already been diagnosed. */
5725 if (constructor_type == 0)
5726 return;
e5e809f4 5727
3e4093b6
RS
5728 /* If we've exhausted any levels that didn't have braces,
5729 pop them now. */
5730 while (constructor_stack->implicit)
5731 {
5732 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5733 || TREE_CODE (constructor_type) == UNION_TYPE)
5734 && constructor_fields == 0)
5735 process_init_element (pop_init_level (1));
5736 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5737 && (constructor_max_index == 0
5738 || tree_int_cst_lt (constructor_max_index,
5739 constructor_index)))
5740 process_init_element (pop_init_level (1));
5741 else
5742 break;
5743 }
e5e809f4 5744
3e4093b6
RS
5745 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5746 if (constructor_range_stack)
e5e809f4 5747 {
3e4093b6
RS
5748 /* If value is a compound literal and we'll be just using its
5749 content, don't put it into a SAVE_EXPR. */
5750 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5751 || !require_constant_value
5752 || flag_isoc99)
5753 value = save_expr (value);
5754 }
e5e809f4 5755
3e4093b6
RS
5756 while (1)
5757 {
5758 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 5759 {
3e4093b6
RS
5760 tree fieldtype;
5761 enum tree_code fieldcode;
e5e809f4 5762
3e4093b6
RS
5763 if (constructor_fields == 0)
5764 {
5765 pedwarn_init ("excess elements in struct initializer");
5766 break;
5767 }
e5e809f4 5768
3e4093b6
RS
5769 fieldtype = TREE_TYPE (constructor_fields);
5770 if (fieldtype != error_mark_node)
5771 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5772 fieldcode = TREE_CODE (fieldtype);
e5e809f4 5773
3e4093b6
RS
5774 /* Error for non-static initialization of a flexible array member. */
5775 if (fieldcode == ARRAY_TYPE
5776 && !require_constant_value
5777 && TYPE_SIZE (fieldtype) == NULL_TREE
5778 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5779 {
5780 error_init ("non-static initialization of a flexible array member");
5781 break;
5782 }
e5e809f4 5783
3e4093b6
RS
5784 /* Accept a string constant to initialize a subarray. */
5785 if (value != 0
5786 && fieldcode == ARRAY_TYPE
5787 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5788 && string_flag)
5789 value = orig_value;
5790 /* Otherwise, if we have come to a subaggregate,
5791 and we don't have an element of its type, push into it. */
5792 else if (value != 0 && !constructor_no_implicit
5793 && value != error_mark_node
5794 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5795 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5796 || fieldcode == UNION_TYPE))
5797 {
5798 push_init_level (1);
5799 continue;
5800 }
e5e809f4 5801
3e4093b6
RS
5802 if (value)
5803 {
5804 push_member_name (constructor_fields);
5805 output_init_element (value, fieldtype, constructor_fields, 1);
5806 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
5807 }
5808 else
3e4093b6
RS
5809 /* Do the bookkeeping for an element that was
5810 directly output as a constructor. */
e5e809f4 5811 {
3e4093b6
RS
5812 /* For a record, keep track of end position of last field. */
5813 if (DECL_SIZE (constructor_fields))
5814 constructor_bit_index
5815 = size_binop (PLUS_EXPR,
5816 bit_position (constructor_fields),
5817 DECL_SIZE (constructor_fields));
5818
5819 /* If the current field was the first one not yet written out,
5820 it isn't now, so update. */
5821 if (constructor_unfilled_fields == constructor_fields)
5822 {
5823 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5824 /* Skip any nameless bit fields. */
5825 while (constructor_unfilled_fields != 0
5826 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5827 && DECL_NAME (constructor_unfilled_fields) == 0)
5828 constructor_unfilled_fields =
5829 TREE_CHAIN (constructor_unfilled_fields);
5830 }
e5e809f4 5831 }
3e4093b6
RS
5832
5833 constructor_fields = TREE_CHAIN (constructor_fields);
5834 /* Skip any nameless bit fields at the beginning. */
5835 while (constructor_fields != 0
5836 && DECL_C_BIT_FIELD (constructor_fields)
5837 && DECL_NAME (constructor_fields) == 0)
5838 constructor_fields = TREE_CHAIN (constructor_fields);
e5e809f4 5839 }
3e4093b6 5840 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 5841 {
3e4093b6
RS
5842 tree fieldtype;
5843 enum tree_code fieldcode;
e5e809f4 5844
3e4093b6
RS
5845 if (constructor_fields == 0)
5846 {
5847 pedwarn_init ("excess elements in union initializer");
5848 break;
5849 }
e5e809f4 5850
3e4093b6
RS
5851 fieldtype = TREE_TYPE (constructor_fields);
5852 if (fieldtype != error_mark_node)
5853 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5854 fieldcode = TREE_CODE (fieldtype);
e5e809f4 5855
3e4093b6
RS
5856 /* Warn that traditional C rejects initialization of unions.
5857 We skip the warning if the value is zero. This is done
5858 under the assumption that the zero initializer in user
5859 code appears conditioned on e.g. __STDC__ to avoid
5860 "missing initializer" warnings and relies on default
5861 initialization to zero in the traditional C case.
5862 We also skip the warning if the initializer is designated,
5863 again on the assumption that this must be conditional on
5864 __STDC__ anyway (and we've already complained about the
5865 member-designator already). */
5866 if (warn_traditional && !in_system_header && !constructor_designated
5867 && !(value && (integer_zerop (value) || real_zerop (value))))
5868 warning ("traditional C rejects initialization of unions");
e5e809f4 5869
3e4093b6
RS
5870 /* Accept a string constant to initialize a subarray. */
5871 if (value != 0
5872 && fieldcode == ARRAY_TYPE
5873 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5874 && string_flag)
5875 value = orig_value;
5876 /* Otherwise, if we have come to a subaggregate,
5877 and we don't have an element of its type, push into it. */
5878 else if (value != 0 && !constructor_no_implicit
5879 && value != error_mark_node
5880 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5881 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5882 || fieldcode == UNION_TYPE))
5883 {
5884 push_init_level (1);
5885 continue;
5886 }
e5e809f4 5887
3e4093b6
RS
5888 if (value)
5889 {
5890 push_member_name (constructor_fields);
5891 output_init_element (value, fieldtype, constructor_fields, 1);
5892 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
5893 }
5894 else
3e4093b6
RS
5895 /* Do the bookkeeping for an element that was
5896 directly output as a constructor. */
e5e809f4 5897 {
3e4093b6
RS
5898 constructor_bit_index = DECL_SIZE (constructor_fields);
5899 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
e5e809f4 5900 }
e5e809f4 5901
3e4093b6
RS
5902 constructor_fields = 0;
5903 }
5904 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5905 {
5906 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5907 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 5908
3e4093b6
RS
5909 /* Accept a string constant to initialize a subarray. */
5910 if (value != 0
5911 && eltcode == ARRAY_TYPE
5912 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5913 && string_flag)
5914 value = orig_value;
5915 /* Otherwise, if we have come to a subaggregate,
5916 and we don't have an element of its type, push into it. */
5917 else if (value != 0 && !constructor_no_implicit
5918 && value != error_mark_node
5919 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5920 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5921 || eltcode == UNION_TYPE))
5922 {
5923 push_init_level (1);
5924 continue;
5925 }
8b6a5902 5926
3e4093b6
RS
5927 if (constructor_max_index != 0
5928 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5929 || integer_all_onesp (constructor_max_index)))
5930 {
5931 pedwarn_init ("excess elements in array initializer");
5932 break;
5933 }
8b6a5902 5934
3e4093b6
RS
5935 /* Now output the actual element. */
5936 if (value)
5937 {
5938 push_array_bounds (tree_low_cst (constructor_index, 0));
5939 output_init_element (value, elttype, constructor_index, 1);
5940 RESTORE_SPELLING_DEPTH (constructor_depth);
5941 }
2f6e4e97 5942
3e4093b6
RS
5943 constructor_index
5944 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 5945
3e4093b6
RS
5946 if (! value)
5947 /* If we are doing the bookkeeping for an element that was
5948 directly output as a constructor, we must update
5949 constructor_unfilled_index. */
5950 constructor_unfilled_index = constructor_index;
5951 }
5952 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5953 {
5954 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 5955
3e4093b6
RS
5956 /* Do a basic check of initializer size. Note that vectors
5957 always have a fixed size derived from their type. */
5958 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5959 {
5960 pedwarn_init ("excess elements in vector initializer");
5961 break;
5962 }
8b6a5902 5963
3e4093b6
RS
5964 /* Now output the actual element. */
5965 if (value)
5966 output_init_element (value, elttype, constructor_index, 1);
8b6a5902 5967
3e4093b6
RS
5968 constructor_index
5969 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 5970
3e4093b6
RS
5971 if (! value)
5972 /* If we are doing the bookkeeping for an element that was
5973 directly output as a constructor, we must update
5974 constructor_unfilled_index. */
5975 constructor_unfilled_index = constructor_index;
5976 }
8b6a5902 5977
3e4093b6
RS
5978 /* Handle the sole element allowed in a braced initializer
5979 for a scalar variable. */
5980 else if (constructor_fields == 0)
8b6a5902 5981 {
3e4093b6
RS
5982 pedwarn_init ("excess elements in scalar initializer");
5983 break;
8b6a5902
JJ
5984 }
5985 else
5986 {
3e4093b6
RS
5987 if (value)
5988 output_init_element (value, constructor_type, NULL_TREE, 1);
5989 constructor_fields = 0;
8b6a5902
JJ
5990 }
5991
3e4093b6
RS
5992 /* Handle range initializers either at this level or anywhere higher
5993 in the designator stack. */
5994 if (constructor_range_stack)
8b6a5902 5995 {
3e4093b6
RS
5996 struct constructor_range_stack *p, *range_stack;
5997 int finish = 0;
5998
5999 range_stack = constructor_range_stack;
6000 constructor_range_stack = 0;
6001 while (constructor_stack != range_stack->stack)
8b6a5902 6002 {
3e4093b6
RS
6003 if (!constructor_stack->implicit)
6004 abort ();
6005 process_init_element (pop_init_level (1));
8b6a5902 6006 }
3e4093b6
RS
6007 for (p = range_stack;
6008 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6009 p = p->prev)
8b6a5902 6010 {
3e4093b6
RS
6011 if (!constructor_stack->implicit)
6012 abort ();
6013 process_init_element (pop_init_level (1));
8b6a5902 6014 }
3e4093b6
RS
6015
6016 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6017 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6018 finish = 1;
6019
6020 while (1)
6021 {
6022 constructor_index = p->index;
6023 constructor_fields = p->fields;
6024 if (finish && p->range_end && p->index == p->range_start)
6025 {
6026 finish = 0;
6027 p->prev = 0;
6028 }
6029 p = p->next;
6030 if (!p)
6031 break;
6032 push_init_level (2);
6033 p->stack = constructor_stack;
6034 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6035 p->index = p->range_start;
6036 }
6037
6038 if (!finish)
6039 constructor_range_stack = range_stack;
6040 continue;
8b6a5902
JJ
6041 }
6042
3e4093b6 6043 break;
8b6a5902
JJ
6044 }
6045
3e4093b6
RS
6046 constructor_range_stack = 0;
6047}
6048\f
9f0e2d86
ZW
6049/* Build a complete asm-statement, whose components are a CV_QUALIFIER
6050 (guaranteed to be 'volatile' or null) and ARGS (represented using
6051 an ASM_STMT node). */
3e4093b6 6052tree
9f0e2d86 6053build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 6054{
9f0e2d86
ZW
6055 if (!TREE_OPERAND (args, 0))
6056 TREE_OPERAND (args, 0) = cv_qualifier;
6057 return add_stmt (args);
8b6a5902
JJ
6058}
6059
9f0e2d86
ZW
6060/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6061 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6062 SIMPLE indicates whether there was anything at all after the
6063 string in the asm expression -- asm("blah") and asm("blah" : )
6064 are subtly different. We use a ASM_STMT node to represent this. */
3e4093b6 6065tree
9f0e2d86
ZW
6066build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6067 bool simple)
e5e809f4 6068{
3e4093b6 6069 tree tail;
9f0e2d86 6070 tree args;
3e4093b6
RS
6071
6072 /* We can remove output conversions that change the type,
6073 but not the mode. */
6074 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
e5e809f4 6075 {
3e4093b6 6076 tree output = TREE_VALUE (tail);
8b6a5902 6077
3e4093b6
RS
6078 STRIP_NOPS (output);
6079 TREE_VALUE (tail) = output;
8b6a5902 6080
3e4093b6
RS
6081 /* Allow conversions as LHS here. build_modify_expr as called below
6082 will do the right thing with them. */
6083 while (TREE_CODE (output) == NOP_EXPR
6084 || TREE_CODE (output) == CONVERT_EXPR
6085 || TREE_CODE (output) == FLOAT_EXPR
6086 || TREE_CODE (output) == FIX_TRUNC_EXPR
6087 || TREE_CODE (output) == FIX_FLOOR_EXPR
6088 || TREE_CODE (output) == FIX_ROUND_EXPR
6089 || TREE_CODE (output) == FIX_CEIL_EXPR)
6090 output = TREE_OPERAND (output, 0);
6091
6092 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
e5e809f4 6093 }
3e4093b6
RS
6094
6095 /* Remove output conversions that change the type but not the mode. */
6096 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
8b6a5902 6097 {
3e4093b6
RS
6098 tree output = TREE_VALUE (tail);
6099 STRIP_NOPS (output);
6100 TREE_VALUE (tail) = output;
8b6a5902 6101 }
3e4093b6
RS
6102
6103 /* Perform default conversions on array and function inputs.
6104 Don't do this for other types as it would screw up operands
6105 expected to be in memory. */
6106 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6107 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6108
9f0e2d86
ZW
6109 args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6110
6111 /* Simple asm statements are treated as volatile. */
6112 if (simple)
6113 {
6114 TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6115 ASM_INPUT_P (args) = 1;
6116 }
6117 return args;
e5e809f4
JL
6118}
6119
3e4093b6
RS
6120/* Expand an ASM statement with operands, handling output operands
6121 that are not variables or INDIRECT_REFS by transforming such
6122 cases into cases that expand_asm_operands can handle.
de520661 6123
3e4093b6 6124 Arguments are same as for expand_asm_operands. */
de520661 6125
3e4093b6
RS
6126void
6127c_expand_asm_operands (tree string, tree outputs, tree inputs,
177560b2 6128 tree clobbers, int vol, location_t locus)
de520661 6129{
3e4093b6
RS
6130 int noutputs = list_length (outputs);
6131 int i;
6132 /* o[I] is the place that output number I should be written. */
703ad42b 6133 tree *o = alloca (noutputs * sizeof (tree));
3e4093b6 6134 tree tail;
d3ab9753 6135
3e4093b6
RS
6136 /* Record the contents of OUTPUTS before it is modified. */
6137 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
e6ecc89b 6138 {
3e4093b6
RS
6139 o[i] = TREE_VALUE (tail);
6140 if (o[i] == error_mark_node)
6141 return;
e6ecc89b
JJ
6142 }
6143
3e4093b6
RS
6144 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6145 OUTPUTS some trees for where the values were actually stored. */
177560b2 6146 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
d3ab9753 6147
3e4093b6
RS
6148 /* Copy all the intermediate outputs into the specified outputs. */
6149 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
de520661 6150 {
3e4093b6
RS
6151 if (o[i] != TREE_VALUE (tail))
6152 {
6153 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6154 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6155 free_temp_slots ();
6156
6157 /* Restore the original value so that it's correct the next
6158 time we expand this function. */
6159 TREE_VALUE (tail) = o[i];
6160 }
6161 /* Detect modification of read-only values.
6162 (Otherwise done by build_modify_expr.) */
6163 else
6164 {
6165 tree type = TREE_TYPE (o[i]);
6166 if (TREE_READONLY (o[i])
6167 || TYPE_READONLY (type)
6168 || ((TREE_CODE (type) == RECORD_TYPE
6169 || TREE_CODE (type) == UNION_TYPE)
6170 && C_TYPE_FIELDS_READONLY (type)))
c5b6f18e 6171 readonly_error (o[i], "modification by `asm'");
3e4093b6 6172 }
de520661 6173 }
8b6a5902 6174
3e4093b6
RS
6175 /* Those MODIFY_EXPRs could do autoincrements. */
6176 emit_queue ();
6177}
6178\f
6179/* Expand a C `return' statement.
6180 RETVAL is the expression for what to return,
6181 or a null pointer for `return;' with no value. */
de520661 6182
3e4093b6
RS
6183tree
6184c_expand_return (tree retval)
6185{
6186 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6187
6188 if (TREE_THIS_VOLATILE (current_function_decl))
6189 warning ("function declared `noreturn' has a `return' statement");
6190
6191 if (!retval)
de520661 6192 {
3e4093b6
RS
6193 current_function_returns_null = 1;
6194 if ((warn_return_type || flag_isoc99)
6195 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6196 pedwarn_c99 ("`return' with no value, in function returning non-void");
400fbf9f 6197 }
3e4093b6 6198 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 6199 {
3e4093b6
RS
6200 current_function_returns_null = 1;
6201 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6202 pedwarn ("`return' with a value, in function returning void");
de520661 6203 }
3e4093b6 6204 else
de520661 6205 {
3e4093b6
RS
6206 tree t = convert_for_assignment (valtype, retval, _("return"),
6207 NULL_TREE, NULL_TREE, 0);
6208 tree res = DECL_RESULT (current_function_decl);
6209 tree inner;
6210
6211 current_function_returns_value = 1;
6212 if (t == error_mark_node)
6213 return NULL_TREE;
6214
6215 inner = t = convert (TREE_TYPE (res), t);
6216
6217 /* Strip any conversions, additions, and subtractions, and see if
6218 we are returning the address of a local variable. Warn if so. */
6219 while (1)
8b6a5902 6220 {
3e4093b6 6221 switch (TREE_CODE (inner))
8b6a5902 6222 {
3e4093b6
RS
6223 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6224 case PLUS_EXPR:
6225 inner = TREE_OPERAND (inner, 0);
6226 continue;
6227
6228 case MINUS_EXPR:
6229 /* If the second operand of the MINUS_EXPR has a pointer
6230 type (or is converted from it), this may be valid, so
6231 don't give a warning. */
6232 {
6233 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 6234
3e4093b6
RS
6235 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6236 && (TREE_CODE (op1) == NOP_EXPR
6237 || TREE_CODE (op1) == NON_LVALUE_EXPR
6238 || TREE_CODE (op1) == CONVERT_EXPR))
6239 op1 = TREE_OPERAND (op1, 0);
8b6a5902 6240
3e4093b6
RS
6241 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6242 break;
8b6a5902 6243
3e4093b6
RS
6244 inner = TREE_OPERAND (inner, 0);
6245 continue;
6246 }
400fbf9f 6247
3e4093b6
RS
6248 case ADDR_EXPR:
6249 inner = TREE_OPERAND (inner, 0);
c2f4acb7 6250
3e4093b6
RS
6251 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6252 inner = TREE_OPERAND (inner, 0);
8b6a5902 6253
a2f1f4c3 6254 if (DECL_P (inner)
3e4093b6
RS
6255 && ! DECL_EXTERNAL (inner)
6256 && ! TREE_STATIC (inner)
6257 && DECL_CONTEXT (inner) == current_function_decl)
6258 warning ("function returns address of local variable");
6259 break;
8b6a5902 6260
3e4093b6
RS
6261 default:
6262 break;
6263 }
de520661 6264
3e4093b6
RS
6265 break;
6266 }
6267
6268 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
de520661 6269 }
8b6a5902 6270
3e4093b6 6271 return add_stmt (build_return_stmt (retval));
de520661 6272}
3e4093b6
RS
6273\f
6274struct c_switch {
6275 /* The SWITCH_STMT being built. */
6276 tree switch_stmt;
6277 /* A splay-tree mapping the low element of a case range to the high
6278 element, or NULL_TREE if there is no high element. Used to
6279 determine whether or not a new case label duplicates an old case
6280 label. We need a tree, rather than simply a hash table, because
6281 of the GNU case range extension. */
6282 splay_tree cases;
6283 /* The next node on the stack. */
6284 struct c_switch *next;
6285};
400fbf9f 6286
3e4093b6
RS
6287/* A stack of the currently active switch statements. The innermost
6288 switch statement is on the top of the stack. There is no need to
6289 mark the stack for garbage collection because it is only active
6290 during the processing of the body of a function, and we never
6291 collect at that point. */
de520661 6292
3e4093b6 6293static struct c_switch *switch_stack;
de520661 6294
3e4093b6
RS
6295/* Start a C switch statement, testing expression EXP. Return the new
6296 SWITCH_STMT. */
de520661 6297
3e4093b6
RS
6298tree
6299c_start_case (tree exp)
de520661 6300{
3e4093b6
RS
6301 enum tree_code code;
6302 tree type, orig_type = error_mark_node;
6303 struct c_switch *cs;
2f6e4e97 6304
3e4093b6 6305 if (exp != error_mark_node)
de520661 6306 {
3e4093b6
RS
6307 code = TREE_CODE (TREE_TYPE (exp));
6308 orig_type = TREE_TYPE (exp);
6309
6310 if (! INTEGRAL_TYPE_P (orig_type)
6311 && code != ERROR_MARK)
de520661 6312 {
3e4093b6
RS
6313 error ("switch quantity not an integer");
6314 exp = integer_zero_node;
de520661 6315 }
3e4093b6 6316 else
de520661 6317 {
3e4093b6 6318 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
8b6a5902 6319
3e4093b6
RS
6320 if (warn_traditional && !in_system_header
6321 && (type == long_integer_type_node
6322 || type == long_unsigned_type_node))
6323 warning ("`long' switch expression not converted to `int' in ISO C");
8b6a5902 6324
3e4093b6
RS
6325 exp = default_conversion (exp);
6326 type = TREE_TYPE (exp);
6327 }
6328 }
6329
6330 /* Add this new SWITCH_STMT to the stack. */
703ad42b 6331 cs = xmalloc (sizeof (*cs));
3e4093b6
RS
6332 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6333 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6334 cs->next = switch_stack;
6335 switch_stack = cs;
6336
6337 return add_stmt (switch_stack->switch_stmt);
6338}
6339
6340/* Process a case label. */
6341
6342tree
6343do_case (tree low_value, tree high_value)
6344{
6345 tree label = NULL_TREE;
6346
6347 if (switch_stack)
6348 {
6349 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6350
6351 label = c_add_case_label (switch_stack->cases,
6352 SWITCH_COND (switch_stack->switch_stmt),
6353 low_value, high_value);
6354 if (label == error_mark_node)
6355 label = NULL_TREE;
6356 else if (switch_was_empty_p)
6357 {
6358 /* Attach the first case label to the SWITCH_BODY. */
6359 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6360 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
400fbf9f 6361 }
de520661 6362 }
3e4093b6
RS
6363 else if (low_value)
6364 error ("case label not within a switch statement");
6365 else
6366 error ("`default' label not within a switch statement");
de520661 6367
3e4093b6
RS
6368 return label;
6369}
de520661 6370
3e4093b6 6371/* Finish the switch statement. */
de520661 6372
3e4093b6
RS
6373void
6374c_finish_case (void)
6375{
6376 struct c_switch *cs = switch_stack;
6377
6378 /* Rechain the next statements to the SWITCH_STMT. */
6379 last_tree = cs->switch_stmt;
6380
6381 /* Pop the stack. */
6382 switch_stack = switch_stack->next;
6383 splay_tree_delete (cs->cases);
6384 free (cs);
de520661 6385}
de520661 6386
3e4093b6
RS
6387/* Build a binary-operation expression without default conversions.
6388 CODE is the kind of expression to build.
6389 This function differs from `build' in several ways:
6390 the data type of the result is computed and recorded in it,
6391 warnings are generated if arg data types are invalid,
6392 special handling for addition and subtraction of pointers is known,
6393 and some optimization is done (operations on narrow ints
6394 are done in the narrower type when that gives the same result).
6395 Constant folding is also done before the result is returned.
de520661 6396
3e4093b6
RS
6397 Note that the operands will never have enumeral types, or function
6398 or array types, because either they will have the default conversions
6399 performed or they have both just been converted to some other type in which
6400 the arithmetic is to be done. */
6401
6402tree
6403build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6404 int convert_p)
de520661 6405{
3e4093b6
RS
6406 tree type0, type1;
6407 enum tree_code code0, code1;
6408 tree op0, op1;
b62acd60 6409
3e4093b6
RS
6410 /* Expression code to give to the expression when it is built.
6411 Normally this is CODE, which is what the caller asked for,
6412 but in some special cases we change it. */
6413 enum tree_code resultcode = code;
8b6a5902 6414
3e4093b6
RS
6415 /* Data type in which the computation is to be performed.
6416 In the simplest cases this is the common type of the arguments. */
6417 tree result_type = NULL;
6418
6419 /* Nonzero means operands have already been type-converted
6420 in whatever way is necessary.
6421 Zero means they need to be converted to RESULT_TYPE. */
6422 int converted = 0;
6423
6424 /* Nonzero means create the expression with this type, rather than
6425 RESULT_TYPE. */
6426 tree build_type = 0;
6427
6428 /* Nonzero means after finally constructing the expression
6429 convert it to this type. */
6430 tree final_type = 0;
6431
6432 /* Nonzero if this is an operation like MIN or MAX which can
6433 safely be computed in short if both args are promoted shorts.
6434 Also implies COMMON.
6435 -1 indicates a bitwise operation; this makes a difference
6436 in the exact conditions for when it is safe to do the operation
6437 in a narrower mode. */
6438 int shorten = 0;
6439
6440 /* Nonzero if this is a comparison operation;
6441 if both args are promoted shorts, compare the original shorts.
6442 Also implies COMMON. */
6443 int short_compare = 0;
6444
6445 /* Nonzero if this is a right-shift operation, which can be computed on the
6446 original short and then promoted if the operand is a promoted short. */
6447 int short_shift = 0;
6448
6449 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6450 int common = 0;
6451
6452 if (convert_p)
790e9490 6453 {
3e4093b6
RS
6454 op0 = default_conversion (orig_op0);
6455 op1 = default_conversion (orig_op1);
790e9490 6456 }
3e4093b6 6457 else
790e9490 6458 {
3e4093b6
RS
6459 op0 = orig_op0;
6460 op1 = orig_op1;
790e9490
RS
6461 }
6462
3e4093b6
RS
6463 type0 = TREE_TYPE (op0);
6464 type1 = TREE_TYPE (op1);
91fa3c30 6465
3e4093b6
RS
6466 /* The expression codes of the data types of the arguments tell us
6467 whether the arguments are integers, floating, pointers, etc. */
6468 code0 = TREE_CODE (type0);
6469 code1 = TREE_CODE (type1);
6470
6471 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6472 STRIP_TYPE_NOPS (op0);
6473 STRIP_TYPE_NOPS (op1);
6474
6475 /* If an error was already reported for one of the arguments,
6476 avoid reporting another error. */
6477
6478 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6479 return error_mark_node;
6480
6481 switch (code)
de520661 6482 {
3e4093b6
RS
6483 case PLUS_EXPR:
6484 /* Handle the pointer + int case. */
6485 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6486 return pointer_int_sum (PLUS_EXPR, op0, op1);
6487 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6488 return pointer_int_sum (PLUS_EXPR, op1, op0);
fe67cf58 6489 else
3e4093b6
RS
6490 common = 1;
6491 break;
400fbf9f 6492
3e4093b6
RS
6493 case MINUS_EXPR:
6494 /* Subtraction of two similar pointers.
6495 We must subtract them as integers, then divide by object size. */
6496 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6497 && comp_target_types (type0, type1, 1))
6498 return pointer_diff (op0, op1);
6499 /* Handle pointer minus int. Just like pointer plus int. */
6500 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6501 return pointer_int_sum (MINUS_EXPR, op0, op1);
6502 else
6503 common = 1;
6504 break;
8b6a5902 6505
3e4093b6
RS
6506 case MULT_EXPR:
6507 common = 1;
6508 break;
6509
6510 case TRUNC_DIV_EXPR:
6511 case CEIL_DIV_EXPR:
6512 case FLOOR_DIV_EXPR:
6513 case ROUND_DIV_EXPR:
6514 case EXACT_DIV_EXPR:
6515 /* Floating point division by zero is a legitimate way to obtain
6516 infinities and NaNs. */
6517 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6518 warning ("division by zero");
6519
6520 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6521 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6522 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6523 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 6524 {
3e4093b6
RS
6525 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6526 resultcode = RDIV_EXPR;
6527 else
6528 /* Although it would be tempting to shorten always here, that
6529 loses on some targets, since the modulo instruction is
6530 undefined if the quotient can't be represented in the
6531 computation mode. We shorten only if unsigned or if
6532 dividing by something we know != -1. */
8df83eae 6533 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6
RS
6534 || (TREE_CODE (op1) == INTEGER_CST
6535 && ! integer_all_onesp (op1)));
6536 common = 1;
6537 }
6538 break;
de520661 6539
3e4093b6 6540 case BIT_AND_EXPR:
3e4093b6
RS
6541 case BIT_IOR_EXPR:
6542 case BIT_XOR_EXPR:
6543 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6544 shorten = -1;
6545 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6546 common = 1;
6547 break;
6548
6549 case TRUNC_MOD_EXPR:
6550 case FLOOR_MOD_EXPR:
6551 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6552 warning ("division by zero");
de520661 6553
3e4093b6
RS
6554 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6555 {
6556 /* Although it would be tempting to shorten always here, that loses
6557 on some targets, since the modulo instruction is undefined if the
6558 quotient can't be represented in the computation mode. We shorten
6559 only if unsigned or if dividing by something we know != -1. */
8df83eae 6560 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6
RS
6561 || (TREE_CODE (op1) == INTEGER_CST
6562 && ! integer_all_onesp (op1)));
6563 common = 1;
6564 }
6565 break;
de520661 6566
3e4093b6
RS
6567 case TRUTH_ANDIF_EXPR:
6568 case TRUTH_ORIF_EXPR:
6569 case TRUTH_AND_EXPR:
6570 case TRUTH_OR_EXPR:
6571 case TRUTH_XOR_EXPR:
6572 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6573 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6574 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6575 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6576 {
6577 /* Result of these operations is always an int,
6578 but that does not mean the operands should be
6579 converted to ints! */
6580 result_type = integer_type_node;
ae2bcd98
RS
6581 op0 = lang_hooks.truthvalue_conversion (op0);
6582 op1 = lang_hooks.truthvalue_conversion (op1);
3e4093b6
RS
6583 converted = 1;
6584 }
6585 break;
eba80994 6586
3e4093b6
RS
6587 /* Shift operations: result has same type as first operand;
6588 always convert second operand to int.
6589 Also set SHORT_SHIFT if shifting rightward. */
de520661 6590
3e4093b6
RS
6591 case RSHIFT_EXPR:
6592 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6593 {
6594 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
b62acd60 6595 {
3e4093b6
RS
6596 if (tree_int_cst_sgn (op1) < 0)
6597 warning ("right shift count is negative");
6598 else
bbb818c6 6599 {
3e4093b6
RS
6600 if (! integer_zerop (op1))
6601 short_shift = 1;
6602
6603 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6604 warning ("right shift count >= width of type");
bbb818c6 6605 }
b62acd60 6606 }
de520661 6607
3e4093b6
RS
6608 /* Use the type of the value to be shifted. */
6609 result_type = type0;
6610 /* Convert the shift-count to an integer, regardless of size
6611 of value being shifted. */
6612 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6613 op1 = convert (integer_type_node, op1);
6614 /* Avoid converting op1 to result_type later. */
6615 converted = 1;
400fbf9f 6616 }
3e4093b6 6617 break;
253b6b82 6618
3e4093b6
RS
6619 case LSHIFT_EXPR:
6620 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6621 {
6622 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
de520661 6623 {
3e4093b6
RS
6624 if (tree_int_cst_sgn (op1) < 0)
6625 warning ("left shift count is negative");
de520661 6626
3e4093b6
RS
6627 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6628 warning ("left shift count >= width of type");
94ba5069 6629 }
de520661 6630
3e4093b6
RS
6631 /* Use the type of the value to be shifted. */
6632 result_type = type0;
6633 /* Convert the shift-count to an integer, regardless of size
6634 of value being shifted. */
6635 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6636 op1 = convert (integer_type_node, op1);
6637 /* Avoid converting op1 to result_type later. */
6638 converted = 1;
400fbf9f 6639 }
3e4093b6 6640 break;
de520661 6641
3e4093b6
RS
6642 case RROTATE_EXPR:
6643 case LROTATE_EXPR:
6644 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6645 {
6646 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
de520661 6647 {
3e4093b6
RS
6648 if (tree_int_cst_sgn (op1) < 0)
6649 warning ("shift count is negative");
6650 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6651 warning ("shift count >= width of type");
de520661
RS
6652 }
6653
3e4093b6
RS
6654 /* Use the type of the value to be shifted. */
6655 result_type = type0;
6656 /* Convert the shift-count to an integer, regardless of size
6657 of value being shifted. */
6658 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6659 op1 = convert (integer_type_node, op1);
6660 /* Avoid converting op1 to result_type later. */
6661 converted = 1;
6662 }
6663 break;
400fbf9f 6664
3e4093b6
RS
6665 case EQ_EXPR:
6666 case NE_EXPR:
6667 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6668 warning ("comparing floating point with == or != is unsafe");
6669 /* Result of comparison is always int,
6670 but don't convert the args to int! */
6671 build_type = integer_type_node;
6672 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
eabe2b29 6673 || code0 == COMPLEX_TYPE)
3e4093b6 6674 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
eabe2b29 6675 || code1 == COMPLEX_TYPE))
3e4093b6
RS
6676 short_compare = 1;
6677 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6678 {
6679 tree tt0 = TREE_TYPE (type0);
6680 tree tt1 = TREE_TYPE (type1);
6681 /* Anything compares with void *. void * compares with anything.
6682 Otherwise, the targets must be compatible
6683 and both must be object or both incomplete. */
6684 if (comp_target_types (type0, type1, 1))
6685 result_type = common_type (type0, type1);
6686 else if (VOID_TYPE_P (tt0))
ee2990e7 6687 {
3e4093b6
RS
6688 /* op0 != orig_op0 detects the case of something
6689 whose value is 0 but which isn't a valid null ptr const. */
6690 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6691 && TREE_CODE (tt1) == FUNCTION_TYPE)
6692 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
ee2990e7 6693 }
3e4093b6 6694 else if (VOID_TYPE_P (tt1))
e6834654 6695 {
3e4093b6
RS
6696 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6697 && TREE_CODE (tt0) == FUNCTION_TYPE)
6698 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
e6834654 6699 }
3e4093b6
RS
6700 else
6701 pedwarn ("comparison of distinct pointer types lacks a cast");
e6834654 6702
3e4093b6
RS
6703 if (result_type == NULL_TREE)
6704 result_type = ptr_type_node;
e6834654 6705 }
3e4093b6
RS
6706 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6707 && integer_zerop (op1))
6708 result_type = type0;
6709 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6710 && integer_zerop (op0))
6711 result_type = type1;
6712 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 6713 {
3e4093b6
RS
6714 result_type = type0;
6715 pedwarn ("comparison between pointer and integer");
de520661 6716 }
3e4093b6 6717 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 6718 {
3e4093b6
RS
6719 result_type = type1;
6720 pedwarn ("comparison between pointer and integer");
8b6a5902 6721 }
3e4093b6 6722 break;
8b6a5902 6723
3e4093b6
RS
6724 case MAX_EXPR:
6725 case MIN_EXPR:
6726 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6727 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6728 shorten = 1;
6729 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8b6a5902 6730 {
3e4093b6 6731 if (comp_target_types (type0, type1, 1))
8b6a5902 6732 {
3e4093b6
RS
6733 result_type = common_type (type0, type1);
6734 if (pedantic
6735 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6736 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8b6a5902 6737 }
3e4093b6 6738 else
8b6a5902 6739 {
3e4093b6
RS
6740 result_type = ptr_type_node;
6741 pedwarn ("comparison of distinct pointer types lacks a cast");
8b6a5902 6742 }
8b6a5902 6743 }
de520661 6744 break;
400fbf9f 6745
3e4093b6
RS
6746 case LE_EXPR:
6747 case GE_EXPR:
6748 case LT_EXPR:
6749 case GT_EXPR:
6750 build_type = integer_type_node;
6751 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6752 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6753 short_compare = 1;
6754 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6755 {
6756 if (comp_target_types (type0, type1, 1))
6757 {
6758 result_type = common_type (type0, type1);
6759 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6760 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6761 pedwarn ("comparison of complete and incomplete pointers");
6762 else if (pedantic
6763 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6764 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6765 }
6766 else
6767 {
6768 result_type = ptr_type_node;
6769 pedwarn ("comparison of distinct pointer types lacks a cast");
6770 }
6771 }
6772 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6773 && integer_zerop (op1))
6774 {
6775 result_type = type0;
6776 if (pedantic || extra_warnings)
6777 pedwarn ("ordered comparison of pointer with integer zero");
6778 }
6779 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6780 && integer_zerop (op0))
6781 {
6782 result_type = type1;
6783 if (pedantic)
6784 pedwarn ("ordered comparison of pointer with integer zero");
6785 }
6786 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6787 {
6788 result_type = type0;
6789 pedwarn ("comparison between pointer and integer");
6790 }
6791 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6792 {
6793 result_type = type1;
6794 pedwarn ("comparison between pointer and integer");
6795 }
6796 break;
64094f6a 6797
3e4093b6
RS
6798 case UNORDERED_EXPR:
6799 case ORDERED_EXPR:
6800 case UNLT_EXPR:
6801 case UNLE_EXPR:
6802 case UNGT_EXPR:
6803 case UNGE_EXPR:
6804 case UNEQ_EXPR:
6805 build_type = integer_type_node;
6806 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6807 {
6808 error ("unordered comparison on non-floating point argument");
6809 return error_mark_node;
6810 }
6811 common = 1;
6812 break;
64094f6a 6813
3e4093b6
RS
6814 default:
6815 break;
c9fe6f9f 6816 }
8f17b5c5 6817
e57e265b
PB
6818 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6819 return error_mark_node;
6820
3e4093b6
RS
6821 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6822 || code0 == VECTOR_TYPE)
6823 &&
6824 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6825 || code1 == VECTOR_TYPE))
400fbf9f 6826 {
3e4093b6 6827 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
39b726dd 6828
3e4093b6
RS
6829 if (shorten || common || short_compare)
6830 result_type = common_type (type0, type1);
400fbf9f 6831
3e4093b6
RS
6832 /* For certain operations (which identify themselves by shorten != 0)
6833 if both args were extended from the same smaller type,
6834 do the arithmetic in that type and then extend.
400fbf9f 6835
3e4093b6
RS
6836 shorten !=0 and !=1 indicates a bitwise operation.
6837 For them, this optimization is safe only if
6838 both args are zero-extended or both are sign-extended.
6839 Otherwise, we might change the result.
6840 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6841 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 6842
3e4093b6
RS
6843 if (shorten && none_complex)
6844 {
6845 int unsigned0, unsigned1;
6846 tree arg0 = get_narrower (op0, &unsigned0);
6847 tree arg1 = get_narrower (op1, &unsigned1);
6848 /* UNS is 1 if the operation to be done is an unsigned one. */
8df83eae 6849 int uns = TYPE_UNSIGNED (result_type);
3e4093b6 6850 tree type;
400fbf9f 6851
3e4093b6 6852 final_type = result_type;
70768eda 6853
3e4093b6
RS
6854 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6855 but it *requires* conversion to FINAL_TYPE. */
70768eda 6856
3e4093b6
RS
6857 if ((TYPE_PRECISION (TREE_TYPE (op0))
6858 == TYPE_PRECISION (TREE_TYPE (arg0)))
6859 && TREE_TYPE (op0) != final_type)
8df83eae 6860 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3e4093b6
RS
6861 if ((TYPE_PRECISION (TREE_TYPE (op1))
6862 == TYPE_PRECISION (TREE_TYPE (arg1)))
6863 && TREE_TYPE (op1) != final_type)
8df83eae 6864 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
88a3dbc1 6865
3e4093b6 6866 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
abe80e6d 6867
3e4093b6
RS
6868 /* For bitwise operations, signedness of nominal type
6869 does not matter. Consider only how operands were extended. */
6870 if (shorten == -1)
6871 uns = unsigned0;
abe80e6d 6872
3e4093b6
RS
6873 /* Note that in all three cases below we refrain from optimizing
6874 an unsigned operation on sign-extended args.
6875 That would not be valid. */
abe80e6d 6876
3e4093b6
RS
6877 /* Both args variable: if both extended in same way
6878 from same width, do it in that width.
6879 Do it unsigned if args were zero-extended. */
6880 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6881 < TYPE_PRECISION (result_type))
6882 && (TYPE_PRECISION (TREE_TYPE (arg1))
6883 == TYPE_PRECISION (TREE_TYPE (arg0)))
6884 && unsigned0 == unsigned1
6885 && (unsigned0 || !uns))
6886 result_type
6887 = c_common_signed_or_unsigned_type
6888 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6889 else if (TREE_CODE (arg0) == INTEGER_CST
6890 && (unsigned1 || !uns)
6891 && (TYPE_PRECISION (TREE_TYPE (arg1))
6892 < TYPE_PRECISION (result_type))
6893 && (type
6894 = c_common_signed_or_unsigned_type (unsigned1,
6895 TREE_TYPE (arg1)),
6896 int_fits_type_p (arg0, type)))
6897 result_type = type;
6898 else if (TREE_CODE (arg1) == INTEGER_CST
6899 && (unsigned0 || !uns)
6900 && (TYPE_PRECISION (TREE_TYPE (arg0))
6901 < TYPE_PRECISION (result_type))
6902 && (type
6903 = c_common_signed_or_unsigned_type (unsigned0,
6904 TREE_TYPE (arg0)),
6905 int_fits_type_p (arg1, type)))
6906 result_type = type;
6907 }
88a3dbc1 6908
3e4093b6 6909 /* Shifts can be shortened if shifting right. */
2f6e4e97 6910
3e4093b6
RS
6911 if (short_shift)
6912 {
6913 int unsigned_arg;
6914 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 6915
3e4093b6 6916 final_type = result_type;
abe80e6d 6917
3e4093b6 6918 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 6919 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 6920
3e4093b6
RS
6921 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6922 /* We can shorten only if the shift count is less than the
6923 number of bits in the smaller type size. */
6924 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6925 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 6926 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
6927 {
6928 /* Do an unsigned shift if the operand was zero-extended. */
6929 result_type
6930 = c_common_signed_or_unsigned_type (unsigned_arg,
6931 TREE_TYPE (arg0));
6932 /* Convert value-to-be-shifted to that type. */
6933 if (TREE_TYPE (op0) != result_type)
6934 op0 = convert (result_type, op0);
6935 converted = 1;
abe80e6d 6936 }
88a3dbc1
RK
6937 }
6938
3e4093b6
RS
6939 /* Comparison operations are shortened too but differently.
6940 They identify themselves by setting short_compare = 1. */
56cb9733 6941
3e4093b6
RS
6942 if (short_compare)
6943 {
6944 /* Don't write &op0, etc., because that would prevent op0
6945 from being kept in a register.
6946 Instead, make copies of the our local variables and
6947 pass the copies by reference, then copy them back afterward. */
6948 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6949 enum tree_code xresultcode = resultcode;
6950 tree val
6951 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 6952
3e4093b6
RS
6953 if (val != 0)
6954 return val;
8f17b5c5 6955
3e4093b6
RS
6956 op0 = xop0, op1 = xop1;
6957 converted = 1;
6958 resultcode = xresultcode;
8f17b5c5 6959
3e4093b6
RS
6960 if (warn_sign_compare && skip_evaluation == 0)
6961 {
8df83eae
RK
6962 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
6963 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3e4093b6
RS
6964 int unsignedp0, unsignedp1;
6965 tree primop0 = get_narrower (op0, &unsignedp0);
6966 tree primop1 = get_narrower (op1, &unsignedp1);
400fbf9f 6967
3e4093b6
RS
6968 xop0 = orig_op0;
6969 xop1 = orig_op1;
6970 STRIP_TYPE_NOPS (xop0);
6971 STRIP_TYPE_NOPS (xop1);
e89a9554 6972
3e4093b6
RS
6973 /* Give warnings for comparisons between signed and unsigned
6974 quantities that may fail.
e89a9554 6975
3e4093b6
RS
6976 Do the checking based on the original operand trees, so that
6977 casts will be considered, but default promotions won't be.
400fbf9f 6978
3e4093b6
RS
6979 Do not warn if the comparison is being done in a signed type,
6980 since the signed type will only be chosen if it can represent
6981 all the values of the unsigned type. */
8df83eae 6982 if (! TYPE_UNSIGNED (result_type))
3e4093b6
RS
6983 /* OK */;
6984 /* Do not warn if both operands are the same signedness. */
6985 else if (op0_signed == op1_signed)
6986 /* OK */;
6987 else
6988 {
6989 tree sop, uop;
8f17b5c5 6990
3e4093b6
RS
6991 if (op0_signed)
6992 sop = xop0, uop = xop1;
6993 else
6994 sop = xop1, uop = xop0;
8f17b5c5 6995
3e4093b6
RS
6996 /* Do not warn if the signed quantity is an
6997 unsuffixed integer literal (or some static
6998 constant expression involving such literals or a
6999 conditional expression involving such literals)
7000 and it is non-negative. */
7001 if (c_tree_expr_nonnegative_p (sop))
7002 /* OK */;
7003 /* Do not warn if the comparison is an equality operation,
7004 the unsigned quantity is an integral constant, and it
7005 would fit in the result if the result were signed. */
7006 else if (TREE_CODE (uop) == INTEGER_CST
7007 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7008 && int_fits_type_p
7009 (uop, c_common_signed_type (result_type)))
7010 /* OK */;
7011 /* Do not warn if the unsigned quantity is an enumeration
7012 constant and its maximum value would fit in the result
7013 if the result were signed. */
7014 else if (TREE_CODE (uop) == INTEGER_CST
7015 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7016 && int_fits_type_p
7017 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7018 c_common_signed_type (result_type)))
7019 /* OK */;
7020 else
7021 warning ("comparison between signed and unsigned");
7022 }
8f17b5c5 7023
3e4093b6
RS
7024 /* Warn if two unsigned values are being compared in a size
7025 larger than their original size, and one (and only one) is the
7026 result of a `~' operator. This comparison will always fail.
8f17b5c5 7027
3e4093b6
RS
7028 Also warn if one operand is a constant, and the constant
7029 does not have all bits set that are set in the ~ operand
7030 when it is extended. */
8f17b5c5 7031
3e4093b6
RS
7032 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7033 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7034 {
7035 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7036 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7037 &unsignedp0);
7038 else
7039 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7040 &unsignedp1);
64094f6a 7041
3e4093b6
RS
7042 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7043 {
7044 tree primop;
7045 HOST_WIDE_INT constant, mask;
7046 int unsignedp, bits;
2ad1815d 7047
3e4093b6
RS
7048 if (host_integerp (primop0, 0))
7049 {
7050 primop = primop1;
7051 unsignedp = unsignedp1;
7052 constant = tree_low_cst (primop0, 0);
7053 }
7054 else
7055 {
7056 primop = primop0;
7057 unsignedp = unsignedp0;
7058 constant = tree_low_cst (primop1, 0);
7059 }
7060
7061 bits = TYPE_PRECISION (TREE_TYPE (primop));
7062 if (bits < TYPE_PRECISION (result_type)
7063 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7064 {
7065 mask = (~ (HOST_WIDE_INT) 0) << bits;
7066 if ((mask & constant) != mask)
7067 warning ("comparison of promoted ~unsigned with constant");
7068 }
7069 }
7070 else if (unsignedp0 && unsignedp1
7071 && (TYPE_PRECISION (TREE_TYPE (primop0))
7072 < TYPE_PRECISION (result_type))
7073 && (TYPE_PRECISION (TREE_TYPE (primop1))
7074 < TYPE_PRECISION (result_type)))
7075 warning ("comparison of promoted ~unsigned with unsigned");
7076 }
7077 }
2ad1815d 7078 }
64094f6a 7079 }
64094f6a 7080
3e4093b6
RS
7081 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7082 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7083 Then the expression will be built.
7084 It will be given type FINAL_TYPE if that is nonzero;
7085 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 7086
3e4093b6
RS
7087 if (!result_type)
7088 {
7089 binary_op_error (code);
7090 return error_mark_node;
7091 }
400fbf9f 7092
3e4093b6
RS
7093 if (! converted)
7094 {
7095 if (TREE_TYPE (op0) != result_type)
7096 op0 = convert (result_type, op0);
7097 if (TREE_TYPE (op1) != result_type)
7098 op1 = convert (result_type, op1);
7099 }
400fbf9f 7100
3e4093b6
RS
7101 if (build_type == NULL_TREE)
7102 build_type = result_type;
400fbf9f 7103
3e4093b6
RS
7104 {
7105 tree result = build (resultcode, build_type, op0, op1);
7106 tree folded;
7107
7108 /* Treat expressions in initializers specially as they can't trap. */
7109 folded = initializer_stack ? fold_initializer (result)
7110 : fold (result);
7111 if (folded == result)
7112 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7113 if (final_type != 0)
7114 return convert (final_type, folded);
7115 return folded;
7116 }
400fbf9f 7117}
3e4093b6 7118