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