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