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