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