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