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