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