]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
Fix doloop pattern in spu.md
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
0953878d 2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
8bcd6380 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
068e6bb3 4 Free Software Foundation, Inc.
400fbf9f 5
1322177d 6This file is part of GCC.
400fbf9f 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
400fbf9f 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
400fbf9f
JW
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
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,
5088b058 26 and some optimization. */
400fbf9f
JW
27
28#include "config.h"
670ee920 29#include "system.h"
4977bab6
ZW
30#include "coretypes.h"
31#include "tm.h"
742b62e7 32#include "rtl.h"
400fbf9f 33#include "tree.h"
e57e265b 34#include "langhooks.h"
400fbf9f 35#include "c-tree.h"
6baf1cc8 36#include "tm_p.h"
400fbf9f 37#include "flags.h"
e14417fa 38#include "output.h"
234042f4 39#include "expr.h"
5f6da302 40#include "toplev.h"
ab87f8c8 41#include "intl.h"
4dd7201e 42#include "ggc.h"
672a6f42 43#include "target.h"
325c3691 44#include "tree-iterator.h"
3a5b9284 45#include "tree-gimple.h"
089efaa4 46#include "tree-flow.h"
325c3691 47
2ac2f164
JM
48/* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50enum impl_conv {
51 ic_argpass,
6dcc04b0 52 ic_argpass_nonproto,
2ac2f164
JM
53 ic_assign,
54 ic_init,
55 ic_return
56};
57
bc4b653b
JM
58/* The level of nesting inside "__alignof__". */
59int in_alignof;
60
61/* The level of nesting inside "sizeof". */
62int in_sizeof;
63
64/* The level of nesting inside "typeof". */
65int in_typeof;
400fbf9f 66
187230a7
JM
67struct c_label_context_se *label_context_stack_se;
68struct c_label_context_vm *label_context_stack_vm;
16ef3acc 69
b71c7f8a 70/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 71 message within this initializer. */
b71c7f8a 72static int missing_braces_mentioned;
103b7b17 73
bf730f15
RS
74static int require_constant_value;
75static int require_constant_elements;
76
58f9752a 77static bool null_pointer_constant_p (const_tree);
f55ade6e 78static tree qualify_type (tree, tree);
58f9752a 79static int tagged_types_tu_compatible_p (const_tree, const_tree);
58393038 80static int comp_target_types (tree, tree);
58f9752a
KG
81static int function_types_compatible_p (const_tree, const_tree);
82static int type_lists_compatible_p (const_tree, const_tree);
f55ade6e 83static tree decl_constant_value_for_broken_optimization (tree);
f55ade6e 84static tree lookup_field (tree, tree);
94a0dd7b 85static int convert_arguments (int, tree *, tree, tree, tree, tree);
f55ade6e 86static tree pointer_diff (tree, tree);
2ac2f164 87static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
f55ade6e 88 int);
f55ade6e
AJ
89static tree valid_compound_expr_initializer (tree, tree);
90static void push_string (const char *);
91static void push_member_name (tree);
f55ade6e
AJ
92static int spelling_length (void);
93static char *print_spelling (char *);
94static void warning_init (const char *);
916c5919
JM
95static tree digest_init (tree, tree, bool, int);
96static void output_init_element (tree, bool, tree, tree, int);
f55ade6e
AJ
97static void output_pending_init_elements (int);
98static int set_designator (int);
99static void push_range_stack (tree);
100static void add_pending_init (tree, tree);
101static void set_nonincremental_init (void);
102static void set_nonincremental_init_from_string (tree);
103static tree find_init_member (tree);
9bf24266 104static void readonly_error (tree, enum lvalue_use);
58f9752a
KG
105static int lvalue_or_else (const_tree, enum lvalue_use);
106static int lvalue_p (const_tree);
4e2fb7de 107static void record_maybe_used_decl (tree);
58f9752a 108static int comptypes_internal (const_tree, const_tree);
6aa3c60d
JM
109\f
110/* Return true if EXP is a null pointer constant, false otherwise. */
111
112static bool
58f9752a 113null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
114{
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 119 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
125}
f13c9b2c
AP
126\f/* This is a cache to hold if two types are compatible or not. */
127
128struct tagged_tu_seen_cache {
129 const struct tagged_tu_seen_cache * next;
58f9752a
KG
130 const_tree t1;
131 const_tree t2;
f13c9b2c
AP
132 /* The return value of tagged_types_tu_compatible_p if we had seen
133 these two types already. */
134 int val;
135};
136
137static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
138static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
139
400fbf9f
JW
140/* Do `exp = require_complete_type (exp);' to make sure exp
141 does not have an incomplete type. (That includes void types.) */
142
143tree
2f6e4e97 144require_complete_type (tree value)
400fbf9f
JW
145{
146 tree type = TREE_TYPE (value);
147
c3d5c3fa 148 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
149 return error_mark_node;
150
400fbf9f 151 /* First, detect a valid value with a complete type. */
d0f062fb 152 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
153 return value;
154
7a228918 155 c_incomplete_type_error (value, type);
400fbf9f
JW
156 return error_mark_node;
157}
158
159/* Print an error message for invalid use of an incomplete type.
160 VALUE is the expression that was used (or 0 if that isn't known)
161 and TYPE is the type that was invalid. */
162
163void
ac7d7749 164c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 165{
5d5993dd 166 const char *type_code_string;
400fbf9f
JW
167
168 /* Avoid duplicate error message. */
169 if (TREE_CODE (type) == ERROR_MARK)
170 return;
171
172 if (value != 0 && (TREE_CODE (value) == VAR_DECL
173 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 174 error ("%qD has an incomplete type", value);
400fbf9f
JW
175 else
176 {
177 retry:
178 /* We must print an error message. Be clever about what it says. */
179
180 switch (TREE_CODE (type))
181 {
182 case RECORD_TYPE:
ab87f8c8 183 type_code_string = "struct";
400fbf9f
JW
184 break;
185
186 case UNION_TYPE:
ab87f8c8 187 type_code_string = "union";
400fbf9f
JW
188 break;
189
190 case ENUMERAL_TYPE:
ab87f8c8 191 type_code_string = "enum";
400fbf9f
JW
192 break;
193
194 case VOID_TYPE:
195 error ("invalid use of void expression");
196 return;
197
198 case ARRAY_TYPE:
199 if (TYPE_DOMAIN (type))
200 {
fba78abb
RH
201 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
202 {
203 error ("invalid use of flexible array member");
204 return;
205 }
400fbf9f
JW
206 type = TREE_TYPE (type);
207 goto retry;
208 }
209 error ("invalid use of array with unspecified bounds");
210 return;
211
212 default:
366de0ce 213 gcc_unreachable ();
400fbf9f
JW
214 }
215
216 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
217 error ("invalid use of undefined type %<%s %E%>",
218 type_code_string, TYPE_NAME (type));
400fbf9f
JW
219 else
220 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 221 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
222 }
223}
224
ab393bf1
NB
225/* Given a type, apply default promotions wrt unnamed function
226 arguments and return the new type. */
227
228tree
2f6e4e97 229c_type_promotes_to (tree type)
ab393bf1
NB
230{
231 if (TYPE_MAIN_VARIANT (type) == float_type_node)
232 return double_type_node;
233
234 if (c_promoting_integer_type_p (type))
235 {
236 /* Preserve unsignedness if not really getting any wider. */
8df83eae 237 if (TYPE_UNSIGNED (type)
c22cacf3
MS
238 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
239 return unsigned_type_node;
ab393bf1
NB
240 return integer_type_node;
241 }
242
243 return type;
244}
245
400fbf9f
JW
246/* Return a variant of TYPE which has all the type qualifiers of LIKE
247 as well as those of TYPE. */
248
249static tree
2f6e4e97 250qualify_type (tree type, tree like)
400fbf9f 251{
2f6e4e97 252 return c_build_qualified_type (type,
afbadaa7 253 TYPE_QUALS (type) | TYPE_QUALS (like));
400fbf9f 254}
52ffd86e
MS
255
256/* Return true iff the given tree T is a variable length array. */
257
258bool
ac7d7749 259c_vla_type_p (const_tree t)
52ffd86e
MS
260{
261 if (TREE_CODE (t) == ARRAY_TYPE
262 && C_TYPE_VARIABLE_SIZE (t))
263 return true;
264 return false;
265}
400fbf9f 266\f
10bc1b1b 267/* Return the composite type of two compatible types.
5305f6d7 268
10bc1b1b
JM
269 We assume that comptypes has already been done and returned
270 nonzero; if that isn't so, this may crash. In particular, we
271 assume that qualifiers match. */
400fbf9f
JW
272
273tree
10bc1b1b 274composite_type (tree t1, tree t2)
400fbf9f 275{
b3694847
SS
276 enum tree_code code1;
277 enum tree_code code2;
4b027d16 278 tree attributes;
400fbf9f
JW
279
280 /* Save time if the two types are the same. */
281
282 if (t1 == t2) return t1;
283
284 /* If one type is nonsense, use the other. */
285 if (t1 == error_mark_node)
286 return t2;
287 if (t2 == error_mark_node)
288 return t1;
289
10bc1b1b
JM
290 code1 = TREE_CODE (t1);
291 code2 = TREE_CODE (t2);
292
d9525bec 293 /* Merge the attributes. */
5fd9b178 294 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 295
10bc1b1b
JM
296 /* If one is an enumerated type and the other is the compatible
297 integer type, the composite type might be either of the two
298 (DR#013 question 3). For consistency, use the enumerated type as
299 the composite type. */
400fbf9f 300
10bc1b1b
JM
301 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
302 return t1;
303 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
304 return t2;
75326e8c 305
366de0ce 306 gcc_assert (code1 == code2);
b6a10c9f 307
400fbf9f
JW
308 switch (code1)
309 {
400fbf9f 310 case POINTER_TYPE:
10bc1b1b 311 /* For two pointers, do this recursively on the target type. */
400fbf9f 312 {
3932261a
MM
313 tree pointed_to_1 = TREE_TYPE (t1);
314 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b
JM
315 tree target = composite_type (pointed_to_1, pointed_to_2);
316 t1 = build_pointer_type (target);
fe7080d2
AP
317 t1 = build_type_attribute_variant (t1, attributes);
318 return qualify_type (t1, t2);
400fbf9f 319 }
400fbf9f
JW
320
321 case ARRAY_TYPE:
322 {
10bc1b1b 323 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
324 int quals;
325 tree unqual_elt;
ca8bdb78
JM
326 tree d1 = TYPE_DOMAIN (t1);
327 tree d2 = TYPE_DOMAIN (t2);
328 bool d1_variable, d2_variable;
329 bool d1_zero, d2_zero;
46df2823 330
de46b2fe 331 /* We should not have any type quals on arrays at all. */
366de0ce 332 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
c22cacf3 333
ca8bdb78
JM
334 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
335 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
336
337 d1_variable = (!d1_zero
338 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
339 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
340 d2_variable = (!d2_zero
341 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
342 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
343 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
344 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 345
400fbf9f 346 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
347 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
348 && (d2_variable || d2_zero || !d1_variable))
4b027d16 349 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
351 && (d1_variable || d1_zero || !d2_variable))
4b027d16 352 return build_type_attribute_variant (t2, attributes);
c22cacf3 353
de46b2fe
AP
354 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
355 return build_type_attribute_variant (t1, attributes);
356 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
357 return build_type_attribute_variant (t2, attributes);
c22cacf3 358
46df2823
JM
359 /* Merge the element types, and have a size if either arg has
360 one. We may have qualifiers on the element types. To set
361 up TYPE_MAIN_VARIANT correctly, we need to form the
362 composite of the unqualified types and add the qualifiers
363 back at the end. */
364 quals = TYPE_QUALS (strip_array_types (elt));
365 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
366 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
367 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
368 && (d2_variable
369 || d2_zero
370 || !d1_variable))
371 ? t1
372 : t2));
46df2823 373 t1 = c_build_qualified_type (t1, quals);
de46b2fe 374 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
375 }
376
fcb99e7b
JJ
377 case ENUMERAL_TYPE:
378 case RECORD_TYPE:
379 case UNION_TYPE:
380 if (attributes != NULL)
381 {
382 /* Try harder not to create a new aggregate type. */
383 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
384 return t1;
385 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
386 return t2;
387 }
388 return build_type_attribute_variant (t1, attributes);
389
400fbf9f
JW
390 case FUNCTION_TYPE:
391 /* Function types: prefer the one that specified arg types.
392 If both do, merge the arg types. Also merge the return types. */
393 {
10bc1b1b 394 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
395 tree p1 = TYPE_ARG_TYPES (t1);
396 tree p2 = TYPE_ARG_TYPES (t2);
397 int len;
398 tree newargs, n;
399 int i;
400
401 /* Save space: see if the result is identical to one of the args. */
3f75a254 402 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 403 return build_type_attribute_variant (t1, attributes);
3f75a254 404 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 405 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
406
407 /* Simple way if one arg fails to specify argument types. */
408 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 409 {
fe7080d2
AP
410 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
4b027d16 413 }
400fbf9f 414 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
415 {
416 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
417 t1 = build_type_attribute_variant (t1, attributes);
418 return qualify_type (t1, t2);
4b027d16 419 }
400fbf9f
JW
420
421 /* If both args specify argument types, we must merge the two
422 lists, argument by argument. */
f75fbaf7 423 /* Tell global_bindings_p to return false so that variable_size
535a42b1 424 doesn't die on VLAs in parameter types. */
f75fbaf7 425 c_override_global_bindings_to_false = true;
2f4e8f2b 426
400fbf9f
JW
427 len = list_length (p1);
428 newargs = 0;
429
430 for (i = 0; i < len; i++)
8d9bfdc5 431 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
432
433 n = newargs;
434
435 for (; p1;
436 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
437 {
438 /* A null type means arg type is not specified.
439 Take whatever the other function type has. */
440 if (TREE_VALUE (p1) == 0)
441 {
442 TREE_VALUE (n) = TREE_VALUE (p2);
443 goto parm_done;
444 }
445 if (TREE_VALUE (p2) == 0)
446 {
447 TREE_VALUE (n) = TREE_VALUE (p1);
448 goto parm_done;
449 }
2f6e4e97 450
400fbf9f
JW
451 /* Given wait (union {union wait *u; int *i} *)
452 and wait (union wait *),
453 prefer union wait * as type of parm. */
454 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
455 && TREE_VALUE (p1) != TREE_VALUE (p2))
456 {
457 tree memb;
58cb41e6
JJ
458 tree mv2 = TREE_VALUE (p2);
459 if (mv2 && mv2 != error_mark_node
460 && TREE_CODE (mv2) != ARRAY_TYPE)
461 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
462 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
463 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
464 {
465 tree mv3 = TREE_TYPE (memb);
466 if (mv3 && mv3 != error_mark_node
467 && TREE_CODE (mv3) != ARRAY_TYPE)
468 mv3 = TYPE_MAIN_VARIANT (mv3);
469 if (comptypes (mv3, mv2))
470 {
471 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
472 TREE_VALUE (p2));
473 if (pedantic)
474 pedwarn ("function types not truly compatible in ISO C");
475 goto parm_done;
476 }
477 }
400fbf9f
JW
478 }
479 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
480 && TREE_VALUE (p2) != TREE_VALUE (p1))
481 {
482 tree memb;
58cb41e6
JJ
483 tree mv1 = TREE_VALUE (p1);
484 if (mv1 && mv1 != error_mark_node
485 && TREE_CODE (mv1) != ARRAY_TYPE)
486 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f
JW
487 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
488 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
489 {
490 tree mv3 = TREE_TYPE (memb);
491 if (mv3 && mv3 != error_mark_node
492 && TREE_CODE (mv3) != ARRAY_TYPE)
493 mv3 = TYPE_MAIN_VARIANT (mv3);
494 if (comptypes (mv3, mv1))
495 {
496 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
497 TREE_VALUE (p1));
498 if (pedantic)
499 pedwarn ("function types not truly compatible in ISO C");
500 goto parm_done;
501 }
502 }
400fbf9f 503 }
10bc1b1b 504 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
505 parm_done: ;
506 }
507
f75fbaf7 508 c_override_global_bindings_to_false = false;
4b027d16 509 t1 = build_function_type (valtype, newargs);
fe7080d2 510 t1 = qualify_type (t1, t2);
0f41302f 511 /* ... falls through ... */
400fbf9f
JW
512 }
513
514 default:
4b027d16 515 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
516 }
517
518}
10bc1b1b
JM
519
520/* Return the type of a conditional expression between pointers to
521 possibly differently qualified versions of compatible types.
522
523 We assume that comp_target_types has already been done and returned
524 nonzero; if that isn't so, this may crash. */
525
526static tree
527common_pointer_type (tree t1, tree t2)
528{
529 tree attributes;
46df2823
JM
530 tree pointed_to_1, mv1;
531 tree pointed_to_2, mv2;
10bc1b1b
JM
532 tree target;
533
534 /* Save time if the two types are the same. */
535
536 if (t1 == t2) return t1;
537
538 /* If one type is nonsense, use the other. */
539 if (t1 == error_mark_node)
540 return t2;
541 if (t2 == error_mark_node)
542 return t1;
543
366de0ce 544 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 545 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
546
547 /* Merge the attributes. */
548 attributes = targetm.merge_type_attributes (t1, t2);
549
550 /* Find the composite type of the target types, and combine the
46df2823
JM
551 qualifiers of the two types' targets. Do not lose qualifiers on
552 array element types by taking the TYPE_MAIN_VARIANT. */
553 mv1 = pointed_to_1 = TREE_TYPE (t1);
554 mv2 = pointed_to_2 = TREE_TYPE (t2);
555 if (TREE_CODE (mv1) != ARRAY_TYPE)
556 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
557 if (TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
559 target = composite_type (mv1, mv2);
10bc1b1b
JM
560 t1 = build_pointer_type (c_build_qualified_type
561 (target,
562 TYPE_QUALS (pointed_to_1) |
563 TYPE_QUALS (pointed_to_2)));
564 return build_type_attribute_variant (t1, attributes);
565}
566
567/* Return the common type for two arithmetic types under the usual
568 arithmetic conversions. The default conversions have already been
569 applied, and enumerated types converted to their compatible integer
570 types. The resulting type is unqualified and has no attributes.
571
572 This is the type for the result of most arithmetic operations
573 if the operands have the given two types. */
574
ccf7f880
JJ
575static tree
576c_common_type (tree t1, tree t2)
10bc1b1b
JM
577{
578 enum tree_code code1;
579 enum tree_code code2;
580
581 /* If one type is nonsense, use the other. */
582 if (t1 == error_mark_node)
583 return t2;
584 if (t2 == error_mark_node)
585 return t1;
586
587 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
588 t1 = TYPE_MAIN_VARIANT (t1);
589
590 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
591 t2 = TYPE_MAIN_VARIANT (t2);
592
593 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
594 t1 = build_type_attribute_variant (t1, NULL_TREE);
595
596 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
597 t2 = build_type_attribute_variant (t2, NULL_TREE);
598
599 /* Save time if the two types are the same. */
600
601 if (t1 == t2) return t1;
602
603 code1 = TREE_CODE (t1);
604 code2 = TREE_CODE (t2);
605
366de0ce 606 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
607 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
608 || code1 == INTEGER_TYPE);
366de0ce 609 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
610 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
611 || code2 == INTEGER_TYPE);
10bc1b1b 612
5fc89bfd
JJ
613 /* When one operand is a decimal float type, the other operand cannot be
614 a generic float type or a complex type. We also disallow vector types
615 here. */
616 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
617 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
618 {
619 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
620 {
621 error ("can%'t mix operands of decimal float and vector types");
622 return error_mark_node;
623 }
624 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
625 {
626 error ("can%'t mix operands of decimal float and complex types");
627 return error_mark_node;
628 }
629 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
630 {
631 error ("can%'t mix operands of decimal float and other float types");
632 return error_mark_node;
633 }
634 }
635
10bc1b1b
JM
636 /* If one type is a vector type, return that type. (How the usual
637 arithmetic conversions apply to the vector types extension is not
638 precisely specified.) */
639 if (code1 == VECTOR_TYPE)
640 return t1;
641
642 if (code2 == VECTOR_TYPE)
643 return t2;
644
645 /* If one type is complex, form the common type of the non-complex
646 components, then make that complex. Use T1 or T2 if it is the
647 required type. */
648 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
649 {
650 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
651 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 652 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
653
654 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
655 return t1;
656 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
657 return t2;
658 else
659 return build_complex_type (subtype);
660 }
661
662 /* If only one is real, use it as the result. */
663
664 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
665 return t1;
666
667 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
668 return t2;
669
9a8ce21f
JG
670 /* If both are real and either are decimal floating point types, use
671 the decimal floating point type with the greater precision. */
672
673 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
674 {
675 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
676 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
677 return dfloat128_type_node;
678 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
679 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
680 return dfloat64_type_node;
681 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
682 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
683 return dfloat32_type_node;
684 }
685
ab22c1fa
CF
686 /* Deal with fixed-point types. */
687 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
688 {
689 unsigned int unsignedp = 0, satp = 0;
690 enum machine_mode m1, m2;
691 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
692
693 m1 = TYPE_MODE (t1);
694 m2 = TYPE_MODE (t2);
695
696 /* If one input type is saturating, the result type is saturating. */
697 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
698 satp = 1;
699
700 /* If both fixed-point types are unsigned, the result type is unsigned.
701 When mixing fixed-point and integer types, follow the sign of the
702 fixed-point type.
703 Otherwise, the result type is signed. */
704 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
705 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
706 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
707 && TYPE_UNSIGNED (t1))
708 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
709 && TYPE_UNSIGNED (t2)))
710 unsignedp = 1;
711
712 /* The result type is signed. */
713 if (unsignedp == 0)
714 {
715 /* If the input type is unsigned, we need to convert to the
716 signed type. */
717 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
718 {
719 unsigned char mclass = 0;
720 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
721 mclass = MODE_FRACT;
722 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
723 mclass = MODE_ACCUM;
724 else
725 gcc_unreachable ();
726 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
727 }
728 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
729 {
730 unsigned char mclass = 0;
731 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
732 mclass = MODE_FRACT;
733 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
734 mclass = MODE_ACCUM;
735 else
736 gcc_unreachable ();
737 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
738 }
739 }
740
741 if (code1 == FIXED_POINT_TYPE)
742 {
743 fbit1 = GET_MODE_FBIT (m1);
744 ibit1 = GET_MODE_IBIT (m1);
745 }
746 else
747 {
748 fbit1 = 0;
749 /* Signed integers need to subtract one sign bit. */
750 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
751 }
752
753 if (code2 == FIXED_POINT_TYPE)
754 {
755 fbit2 = GET_MODE_FBIT (m2);
756 ibit2 = GET_MODE_IBIT (m2);
757 }
758 else
759 {
760 fbit2 = 0;
761 /* Signed integers need to subtract one sign bit. */
762 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
763 }
764
765 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
766 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
767 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
768 satp);
769 }
770
10bc1b1b
JM
771 /* Both real or both integers; use the one with greater precision. */
772
773 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
774 return t1;
775 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
776 return t2;
777
778 /* Same precision. Prefer long longs to longs to ints when the
779 same precision, following the C99 rules on integer type rank
780 (which are equivalent to the C90 rules for C90 types). */
781
782 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
783 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
784 return long_long_unsigned_type_node;
785
786 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
787 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
788 {
789 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
790 return long_long_unsigned_type_node;
791 else
c22cacf3 792 return long_long_integer_type_node;
10bc1b1b
JM
793 }
794
795 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
796 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
797 return long_unsigned_type_node;
798
799 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
800 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
801 {
802 /* But preserve unsignedness from the other type,
803 since long cannot hold all the values of an unsigned int. */
804 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
805 return long_unsigned_type_node;
806 else
807 return long_integer_type_node;
808 }
809
810 /* Likewise, prefer long double to double even if same size. */
811 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
812 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
813 return long_double_type_node;
814
815 /* Otherwise prefer the unsigned one. */
816
817 if (TYPE_UNSIGNED (t1))
818 return t1;
819 else
820 return t2;
821}
400fbf9f 822\f
5922c215
JM
823/* Wrapper around c_common_type that is used by c-common.c and other
824 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
825 are allowed here and are converted to their compatible integer types.
826 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
827 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
828tree
829common_type (tree t1, tree t2)
830{
831 if (TREE_CODE (t1) == ENUMERAL_TYPE)
832 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
833 if (TREE_CODE (t2) == ENUMERAL_TYPE)
834 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
835
836 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
837 if (TREE_CODE (t1) == BOOLEAN_TYPE
838 && TREE_CODE (t2) == BOOLEAN_TYPE)
839 return boolean_type_node;
840
841 /* If either type is BOOLEAN_TYPE, then return the other. */
842 if (TREE_CODE (t1) == BOOLEAN_TYPE)
843 return t2;
844 if (TREE_CODE (t2) == BOOLEAN_TYPE)
845 return t1;
846
ccf7f880
JJ
847 return c_common_type (t1, t2);
848}
f13c9b2c 849
400fbf9f
JW
850/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
851 or various other operations. Return 2 if they are compatible
852 but a warning may be needed if you use them together. */
853
854int
132da1a5 855comptypes (tree type1, tree type2)
f13c9b2c
AP
856{
857 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
858 int val;
859
860 val = comptypes_internal (type1, type2);
861 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 862
f13c9b2c 863 return val;
c22cacf3
MS
864}
865\f
f13c9b2c
AP
866/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
867 or various other operations. Return 2 if they are compatible
868 but a warning may be needed if you use them together. This
869 differs from comptypes, in that we don't free the seen types. */
870
871static int
58f9752a 872comptypes_internal (const_tree type1, const_tree type2)
400fbf9f 873{
58f9752a
KG
874 const_tree t1 = type1;
875 const_tree t2 = type2;
4b027d16 876 int attrval, val;
400fbf9f
JW
877
878 /* Suppress errors caused by previously reported errors. */
879
8d47dfc5
RH
880 if (t1 == t2 || !t1 || !t2
881 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
882 return 1;
883
21318741
RK
884 /* If either type is the internal version of sizetype, return the
885 language version. */
886 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
eb1a2c88
DN
887 && TYPE_ORIG_SIZE_TYPE (t1))
888 t1 = TYPE_ORIG_SIZE_TYPE (t1);
21318741
RK
889
890 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
eb1a2c88
DN
891 && TYPE_ORIG_SIZE_TYPE (t2))
892 t2 = TYPE_ORIG_SIZE_TYPE (t2);
893
21318741 894
bca63328
JM
895 /* Enumerated types are compatible with integer types, but this is
896 not transitive: two enumerated types in the same translation unit
897 are compatible with each other only if they are the same type. */
400fbf9f 898
bca63328 899 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
8df83eae 900 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
bca63328 901 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
8df83eae 902 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
400fbf9f
JW
903
904 if (t1 == t2)
905 return 1;
906
907 /* Different classes of types can't be compatible. */
908
3aeb3655
EC
909 if (TREE_CODE (t1) != TREE_CODE (t2))
910 return 0;
400fbf9f 911
118a3a8b 912 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 913
3932261a 914 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
915 return 0;
916
08632da2
RS
917 /* Allow for two different type nodes which have essentially the same
918 definition. Note that we already checked for equality of the type
38e01259 919 qualifiers (just above). */
400fbf9f 920
46df2823
JM
921 if (TREE_CODE (t1) != ARRAY_TYPE
922 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
923 return 1;
924
4b027d16 925 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
3f75a254 926 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
4b027d16
RK
927 return 0;
928
929 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
930 val = 0;
931
400fbf9f
JW
932 switch (TREE_CODE (t1))
933 {
934 case POINTER_TYPE:
106f5de5
UW
935 /* Do not remove mode or aliasing information. */
936 if (TYPE_MODE (t1) != TYPE_MODE (t2)
937 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
938 break;
4b027d16 939 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
f13c9b2c 940 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
4b027d16 941 break;
400fbf9f
JW
942
943 case FUNCTION_TYPE:
132da1a5 944 val = function_types_compatible_p (t1, t2);
4b027d16 945 break;
400fbf9f
JW
946
947 case ARRAY_TYPE:
948 {
400fbf9f
JW
949 tree d1 = TYPE_DOMAIN (t1);
950 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
951 bool d1_variable, d2_variable;
952 bool d1_zero, d2_zero;
4b027d16 953 val = 1;
400fbf9f
JW
954
955 /* Target types must match incl. qualifiers. */
956 if (TREE_TYPE (t1) != TREE_TYPE (t2)
f13c9b2c 957 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
400fbf9f
JW
958 return 0;
959
960 /* Sizes must match unless one is missing or variable. */
3f85558f 961 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 962 break;
400fbf9f 963
3f75a254
JM
964 d1_zero = !TYPE_MAX_VALUE (d1);
965 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 966
3f75a254 967 d1_variable = (!d1_zero
3f85558f
RH
968 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
969 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 970 d2_variable = (!d2_zero
3f85558f
RH
971 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
972 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
973 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
974 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f
RH
975
976 if (d1_variable || d2_variable)
977 break;
978 if (d1_zero && d2_zero)
979 break;
980 if (d1_zero || d2_zero
3f75a254
JM
981 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
982 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
983 val = 0;
984
c22cacf3 985 break;
400fbf9f
JW
986 }
987
d1bd0ded 988 case ENUMERAL_TYPE:
58393038 989 case RECORD_TYPE:
d1bd0ded 990 case UNION_TYPE:
766beae1 991 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 992 {
fcb99e7b
JJ
993 tree a1 = TYPE_ATTRIBUTES (t1);
994 tree a2 = TYPE_ATTRIBUTES (t2);
995
996 if (! attribute_list_contained (a1, a2)
997 && ! attribute_list_contained (a2, a1))
998 break;
999
f13c9b2c
AP
1000 if (attrval != 2)
1001 return tagged_types_tu_compatible_p (t1, t2);
1002 val = tagged_types_tu_compatible_p (t1, t2);
1003 }
4b027d16 1004 break;
e9a25f70 1005
62e1dfcf 1006 case VECTOR_TYPE:
cc27e657 1007 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
f13c9b2c 1008 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
62e1dfcf
NC
1009 break;
1010
e9a25f70
JL
1011 default:
1012 break;
400fbf9f 1013 }
4b027d16 1014 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1015}
1016
1017/* Return 1 if TTL and TTR are pointers to types that are equivalent,
58393038 1018 ignoring their qualifiers. */
400fbf9f
JW
1019
1020static int
58393038 1021comp_target_types (tree ttl, tree ttr)
400fbf9f 1022{
392202b0 1023 int val;
46df2823 1024 tree mvl, mvr;
8b40563c 1025
46df2823
JM
1026 /* Do not lose qualifiers on element types of array types that are
1027 pointer targets by taking their TYPE_MAIN_VARIANT. */
1028 mvl = TREE_TYPE (ttl);
1029 mvr = TREE_TYPE (ttr);
1030 if (TREE_CODE (mvl) != ARRAY_TYPE)
1031 mvl = TYPE_MAIN_VARIANT (mvl);
1032 if (TREE_CODE (mvr) != ARRAY_TYPE)
1033 mvr = TYPE_MAIN_VARIANT (mvr);
1034 val = comptypes (mvl, mvr);
8b40563c 1035
400fbf9f
JW
1036 if (val == 2 && pedantic)
1037 pedwarn ("types are not quite compatible");
1038 return val;
1039}
1040\f
1041/* Subroutines of `comptypes'. */
1042
f75fbaf7
ZW
1043/* Determine whether two trees derive from the same translation unit.
1044 If the CONTEXT chain ends in a null, that tree's context is still
1045 being parsed, so if two trees have context chains ending in null,
766beae1 1046 they're in the same translation unit. */
f75fbaf7 1047int
58f9752a 1048same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1049{
1050 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1051 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1052 {
6615c446
JO
1053 case tcc_declaration:
1054 t1 = DECL_CONTEXT (t1); break;
1055 case tcc_type:
1056 t1 = TYPE_CONTEXT (t1); break;
1057 case tcc_exceptional:
1058 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1059 default: gcc_unreachable ();
766beae1
ZW
1060 }
1061
1062 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1063 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1064 {
6615c446
JO
1065 case tcc_declaration:
1066 t2 = DECL_CONTEXT (t2); break;
1067 case tcc_type:
1068 t2 = TYPE_CONTEXT (t2); break;
1069 case tcc_exceptional:
1070 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1071 default: gcc_unreachable ();
766beae1
ZW
1072 }
1073
1074 return t1 == t2;
1075}
1076
f13c9b2c 1077/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1078
f13c9b2c 1079static struct tagged_tu_seen_cache *
58f9752a 1080alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1081{
cceb1885 1082 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1083 tu->next = tagged_tu_seen_base;
1084 tu->t1 = t1;
1085 tu->t2 = t2;
c22cacf3 1086
f13c9b2c 1087 tagged_tu_seen_base = tu;
c22cacf3 1088
f13c9b2c
AP
1089 /* The C standard says that two structures in different translation
1090 units are compatible with each other only if the types of their
1091 fields are compatible (among other things). We assume that they
1092 are compatible until proven otherwise when building the cache.
1093 An example where this can occur is:
1094 struct a
1095 {
1096 struct a *next;
1097 };
1098 If we are comparing this against a similar struct in another TU,
c83eecad 1099 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1100 loop. */
1101 tu->val = 1;
1102 return tu;
1103}
d1bd0ded 1104
f13c9b2c 1105/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1106
f13c9b2c
AP
1107static void
1108free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1109{
1110 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1111 while (tu != tu_til)
1112 {
741ac903
KG
1113 const struct tagged_tu_seen_cache *const tu1
1114 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1115 tu = tu1->next;
b1d5455a 1116 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1117 }
1118 tagged_tu_seen_base = tu_til;
1119}
d1bd0ded
GK
1120
1121/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1122 compatible. If the two types are not the same (which has been
1123 checked earlier), this can only happen when multiple translation
1124 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1125 rules. */
1126
1127static int
58f9752a 1128tagged_types_tu_compatible_p (const_tree t1, const_tree t2)
d1bd0ded
GK
1129{
1130 tree s1, s2;
1131 bool needs_warning = false;
3aeb3655 1132
d1bd0ded
GK
1133 /* We have to verify that the tags of the types are the same. This
1134 is harder than it looks because this may be a typedef, so we have
1135 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1136 typedef...
1137 In the case of compiler-created builtin structs the TYPE_DECL
1138 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1139 while (TYPE_NAME (t1)
1140 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1141 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1142 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1143
dea984dc
ILT
1144 while (TYPE_NAME (t2)
1145 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1146 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1147 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1148
1149 /* C90 didn't have the requirement that the two tags be the same. */
1150 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1151 return 0;
3aeb3655 1152
d1bd0ded
GK
1153 /* C90 didn't say what happened if one or both of the types were
1154 incomplete; we choose to follow C99 rules here, which is that they
1155 are compatible. */
1156 if (TYPE_SIZE (t1) == NULL
1157 || TYPE_SIZE (t2) == NULL)
1158 return 1;
3aeb3655 1159
d1bd0ded 1160 {
f13c9b2c 1161 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1162 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1163 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1164 return tts_i->val;
d1bd0ded 1165 }
3aeb3655 1166
d1bd0ded
GK
1167 switch (TREE_CODE (t1))
1168 {
1169 case ENUMERAL_TYPE:
1170 {
f13c9b2c 1171 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1172 /* Speed up the case where the type values are in the same order. */
1173 tree tv1 = TYPE_VALUES (t1);
1174 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1175
c22cacf3 1176 if (tv1 == tv2)
f13c9b2c
AP
1177 {
1178 return 1;
1179 }
3aeb3655 1180
c22cacf3
MS
1181 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1182 {
1183 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1184 break;
1185 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1186 {
c22cacf3 1187 tu->val = 0;
f13c9b2c
AP
1188 return 0;
1189 }
c22cacf3 1190 }
3aeb3655 1191
c22cacf3 1192 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1193 {
1194 return 1;
1195 }
c22cacf3 1196 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1197 {
1198 tu->val = 0;
1199 return 0;
1200 }
3aeb3655 1201
d1bd0ded 1202 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1203 {
1204 tu->val = 0;
1205 return 0;
1206 }
3aeb3655 1207
d1bd0ded
GK
1208 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1209 {
1210 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1211 if (s2 == NULL
1212 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1213 {
1214 tu->val = 0;
1215 return 0;
1216 }
d1bd0ded
GK
1217 }
1218 return 1;
1219 }
1220
1221 case UNION_TYPE:
1222 {
f13c9b2c 1223 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1224 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1225 {
1226 tu->val = 0;
1227 return 0;
1228 }
c22cacf3 1229
f13c9b2c
AP
1230 /* Speed up the common case where the fields are in the same order. */
1231 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1232 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1233 {
1234 int result;
c22cacf3
MS
1235
1236
f13c9b2c 1237 if (DECL_NAME (s1) == NULL
c22cacf3 1238 || DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c
AP
1239 break;
1240 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1241 if (result == 0)
1242 {
1243 tu->val = 0;
1244 return 0;
1245 }
1246 if (result == 2)
1247 needs_warning = true;
1248
1249 if (TREE_CODE (s1) == FIELD_DECL
1250 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1251 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1252 {
1253 tu->val = 0;
1254 return 0;
1255 }
1256 }
1257 if (!s1 && !s2)
1258 {
1259 tu->val = needs_warning ? 2 : 1;
1260 return tu->val;
1261 }
d1bd0ded
GK
1262
1263 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1264 {
1265 bool ok = false;
3aeb3655 1266
d1bd0ded 1267 if (DECL_NAME (s1) != NULL)
398ce3dd 1268 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
d1bd0ded
GK
1269 if (DECL_NAME (s1) == DECL_NAME (s2))
1270 {
1271 int result;
f13c9b2c 1272 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
d1bd0ded 1273 if (result == 0)
f13c9b2c
AP
1274 {
1275 tu->val = 0;
1276 return 0;
1277 }
d1bd0ded
GK
1278 if (result == 2)
1279 needs_warning = true;
3aeb3655 1280
d1bd0ded
GK
1281 if (TREE_CODE (s1) == FIELD_DECL
1282 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1283 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1284 break;
1285
1286 ok = true;
1287 break;
1288 }
3f75a254 1289 if (!ok)
f13c9b2c
AP
1290 {
1291 tu->val = 0;
1292 return 0;
1293 }
d1bd0ded 1294 }
f13c9b2c
AP
1295 tu->val = needs_warning ? 2 : 10;
1296 return tu->val;
d1bd0ded
GK
1297 }
1298
1299 case RECORD_TYPE:
1300 {
c22cacf3 1301 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1302
1303 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded
GK
1304 s1 && s2;
1305 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1306 {
1307 int result;
1308 if (TREE_CODE (s1) != TREE_CODE (s2)
1309 || DECL_NAME (s1) != DECL_NAME (s2))
1310 break;
f13c9b2c 1311 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
d1bd0ded
GK
1312 if (result == 0)
1313 break;
1314 if (result == 2)
1315 needs_warning = true;
3aeb3655 1316
d1bd0ded
GK
1317 if (TREE_CODE (s1) == FIELD_DECL
1318 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1319 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1320 break;
1321 }
d1bd0ded 1322 if (s1 && s2)
f13c9b2c
AP
1323 tu->val = 0;
1324 else
1325 tu->val = needs_warning ? 2 : 1;
1326 return tu->val;
d1bd0ded
GK
1327 }
1328
1329 default:
366de0ce 1330 gcc_unreachable ();
d1bd0ded
GK
1331 }
1332}
1333
400fbf9f
JW
1334/* Return 1 if two function types F1 and F2 are compatible.
1335 If either type specifies no argument types,
1336 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1337 Otherwise, if one type specifies only the number of arguments,
400fbf9f
JW
1338 the other must specify that number of self-promoting arg types.
1339 Otherwise, the argument types must match. */
1340
1341static int
58f9752a 1342function_types_compatible_p (const_tree f1, const_tree f2)
400fbf9f
JW
1343{
1344 tree args1, args2;
1345 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1346 int val = 1;
1347 int val1;
a6fdc086
GK
1348 tree ret1, ret2;
1349
1350 ret1 = TREE_TYPE (f1);
1351 ret2 = TREE_TYPE (f2);
1352
e508a019
JM
1353 /* 'volatile' qualifiers on a function's return type used to mean
1354 the function is noreturn. */
1355 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
bda67431 1356 pedwarn ("function return types not compatible due to %<volatile%>");
a6fdc086
GK
1357 if (TYPE_VOLATILE (ret1))
1358 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1359 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1360 if (TYPE_VOLATILE (ret2))
1361 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1362 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
f13c9b2c 1363 val = comptypes_internal (ret1, ret2);
a6fdc086 1364 if (val == 0)
400fbf9f
JW
1365 return 0;
1366
1367 args1 = TYPE_ARG_TYPES (f1);
1368 args2 = TYPE_ARG_TYPES (f2);
1369
1370 /* An unspecified parmlist matches any specified parmlist
1371 whose argument types don't need default promotions. */
1372
1373 if (args1 == 0)
1374 {
1375 if (!self_promoting_args_p (args2))
1376 return 0;
1377 /* If one of these types comes from a non-prototype fn definition,
1378 compare that with the other type's arglist.
3176a0c2 1379 If they don't match, ask for a warning (but no error). */
400fbf9f 1380 if (TYPE_ACTUAL_ARG_TYPES (f1)
132da1a5 1381 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
400fbf9f
JW
1382 val = 2;
1383 return val;
1384 }
1385 if (args2 == 0)
1386 {
1387 if (!self_promoting_args_p (args1))
1388 return 0;
1389 if (TYPE_ACTUAL_ARG_TYPES (f2)
132da1a5 1390 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
400fbf9f
JW
1391 val = 2;
1392 return val;
1393 }
1394
1395 /* Both types have argument lists: compare them and propagate results. */
132da1a5 1396 val1 = type_lists_compatible_p (args1, args2);
400fbf9f
JW
1397 return val1 != 1 ? val1 : val;
1398}
1399
1400/* Check two lists of types for compatibility,
1401 returning 0 for incompatible, 1 for compatible,
1402 or 2 for compatible with warning. */
1403
1404static int
58f9752a 1405type_lists_compatible_p (const_tree args1, const_tree args2)
400fbf9f
JW
1406{
1407 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1408 int val = 1;
9d5f3e49 1409 int newval = 0;
400fbf9f
JW
1410
1411 while (1)
1412 {
46df2823 1413 tree a1, mv1, a2, mv2;
400fbf9f
JW
1414 if (args1 == 0 && args2 == 0)
1415 return val;
1416 /* If one list is shorter than the other,
1417 they fail to match. */
1418 if (args1 == 0 || args2 == 0)
1419 return 0;
46df2823
JM
1420 mv1 = a1 = TREE_VALUE (args1);
1421 mv2 = a2 = TREE_VALUE (args2);
1422 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1423 mv1 = TYPE_MAIN_VARIANT (mv1);
1424 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1425 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
1426 /* A null pointer instead of a type
1427 means there is supposed to be an argument
1428 but nothing is specified about what type it has.
1429 So match anything that self-promotes. */
46df2823 1430 if (a1 == 0)
400fbf9f 1431 {
46df2823 1432 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1433 return 0;
1434 }
46df2823 1435 else if (a2 == 0)
400fbf9f 1436 {
46df2823 1437 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1438 return 0;
1439 }
8f5b6d29 1440 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1441 else if (TREE_CODE (a1) == ERROR_MARK
1442 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1443 ;
f13c9b2c 1444 else if (!(newval = comptypes_internal (mv1, mv2)))
400fbf9f
JW
1445 {
1446 /* Allow wait (union {union wait *u; int *i} *)
1447 and wait (union wait *) to be compatible. */
46df2823
JM
1448 if (TREE_CODE (a1) == UNION_TYPE
1449 && (TYPE_NAME (a1) == 0
1450 || TYPE_TRANSPARENT_UNION (a1))
1451 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1452 && tree_int_cst_equal (TYPE_SIZE (a1),
1453 TYPE_SIZE (a2)))
400fbf9f
JW
1454 {
1455 tree memb;
46df2823 1456 for (memb = TYPE_FIELDS (a1);
400fbf9f 1457 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
1458 {
1459 tree mv3 = TREE_TYPE (memb);
1460 if (mv3 && mv3 != error_mark_node
1461 && TREE_CODE (mv3) != ARRAY_TYPE)
1462 mv3 = TYPE_MAIN_VARIANT (mv3);
f13c9b2c 1463 if (comptypes_internal (mv3, mv2))
58cb41e6
JJ
1464 break;
1465 }
400fbf9f
JW
1466 if (memb == 0)
1467 return 0;
1468 }
46df2823
JM
1469 else if (TREE_CODE (a2) == UNION_TYPE
1470 && (TYPE_NAME (a2) == 0
1471 || TYPE_TRANSPARENT_UNION (a2))
1472 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1473 && tree_int_cst_equal (TYPE_SIZE (a2),
1474 TYPE_SIZE (a1)))
400fbf9f
JW
1475 {
1476 tree memb;
46df2823 1477 for (memb = TYPE_FIELDS (a2);
400fbf9f 1478 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
1479 {
1480 tree mv3 = TREE_TYPE (memb);
1481 if (mv3 && mv3 != error_mark_node
1482 && TREE_CODE (mv3) != ARRAY_TYPE)
1483 mv3 = TYPE_MAIN_VARIANT (mv3);
f13c9b2c 1484 if (comptypes_internal (mv3, mv1))
58cb41e6
JJ
1485 break;
1486 }
400fbf9f
JW
1487 if (memb == 0)
1488 return 0;
1489 }
1490 else
1491 return 0;
1492 }
1493
1494 /* comptypes said ok, but record if it said to warn. */
1495 if (newval > val)
1496 val = newval;
1497
1498 args1 = TREE_CHAIN (args1);
1499 args2 = TREE_CHAIN (args2);
1500 }
1501}
400fbf9f 1502\f
400fbf9f
JW
1503/* Compute the size to increment a pointer by. */
1504
4e2fb7de 1505static tree
58f9752a 1506c_size_in_bytes (const_tree type)
400fbf9f
JW
1507{
1508 enum tree_code code = TREE_CODE (type);
1509
fed3cef0
RK
1510 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1511 return size_one_node;
1512
d0f062fb 1513 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1514 {
1515 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1516 return size_one_node;
400fbf9f
JW
1517 }
1518
1519 /* Convert in case a char is more than one unit. */
fed3cef0
RK
1520 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1521 size_int (TYPE_PRECISION (char_type_node)
1522 / BITS_PER_UNIT));
400fbf9f 1523}
400fbf9f 1524\f
400fbf9f
JW
1525/* Return either DECL or its known constant value (if it has one). */
1526
56cb9733 1527tree
2f6e4e97 1528decl_constant_value (tree decl)
400fbf9f 1529{
a7c1916a 1530 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1531 in a place where a variable is invalid. Note that DECL_INITIAL
1532 isn't valid for a PARM_DECL. */
a7c1916a 1533 current_function_decl != 0
4f976745 1534 && TREE_CODE (decl) != PARM_DECL
3f75a254 1535 && !TREE_THIS_VOLATILE (decl)
83bab8db 1536 && TREE_READONLY (decl)
400fbf9f
JW
1537 && DECL_INITIAL (decl) != 0
1538 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1539 /* This is invalid if initial value is not constant.
1540 If it has either a function call, a memory reference,
1541 or a variable, then re-evaluating it could give different results. */
1542 && TREE_CONSTANT (DECL_INITIAL (decl))
1543 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1544 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1545 return DECL_INITIAL (decl);
1546 return decl;
1547}
1548
2f74f7e9
JM
1549/* Return either DECL or its known constant value (if it has one), but
1550 return DECL if pedantic or DECL has mode BLKmode. This is for
1551 bug-compatibility with the old behavior of decl_constant_value
1552 (before GCC 3.0); every use of this function is a bug and it should
1553 be removed before GCC 3.1. It is not appropriate to use pedantic
1554 in a way that affects optimization, and BLKmode is probably not the
1555 right test for avoiding misoptimizations either. */
1556
1557static tree
2f6e4e97 1558decl_constant_value_for_broken_optimization (tree decl)
2f74f7e9 1559{
c477e13b
JJ
1560 tree ret;
1561
2f74f7e9
JM
1562 if (pedantic || DECL_MODE (decl) == BLKmode)
1563 return decl;
c477e13b
JJ
1564
1565 ret = decl_constant_value (decl);
1566 /* Avoid unwanted tree sharing between the initializer and current
1567 function's body where the tree can be modified e.g. by the
1568 gimplifier. */
1569 if (ret != decl && TREE_STATIC (decl))
1570 ret = unshare_expr (ret);
1571 return ret;
2f74f7e9
JM
1572}
1573
f2a71bbc
JM
1574/* Convert the array expression EXP to a pointer. */
1575static tree
1576array_to_pointer_conversion (tree exp)
207bf485 1577{
f2a71bbc 1578 tree orig_exp = exp;
207bf485 1579 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1580 tree adr;
1581 tree restype = TREE_TYPE (type);
1582 tree ptrtype;
207bf485 1583
f2a71bbc 1584 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1585
f2a71bbc 1586 STRIP_TYPE_NOPS (exp);
207bf485 1587
487a92fe
JM
1588 if (TREE_NO_WARNING (orig_exp))
1589 TREE_NO_WARNING (exp) = 1;
207bf485 1590
f2a71bbc
JM
1591 ptrtype = build_pointer_type (restype);
1592
1593 if (TREE_CODE (exp) == INDIRECT_REF)
1594 return convert (ptrtype, TREE_OPERAND (exp, 0));
1595
1596 if (TREE_CODE (exp) == VAR_DECL)
207bf485 1597 {
f2a71bbc
JM
1598 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1599 ADDR_EXPR because it's the best way of representing what
1600 happens in C when we take the address of an array and place
1601 it in a pointer to the element type. */
1602 adr = build1 (ADDR_EXPR, ptrtype, exp);
1603 if (!c_mark_addressable (exp))
1604 return error_mark_node;
1605 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1606 return adr;
207bf485 1607 }
207bf485 1608
f2a71bbc
JM
1609 /* This way is better for a COMPONENT_REF since it can
1610 simplify the offset for a component. */
1611 adr = build_unary_op (ADDR_EXPR, exp, 1);
1612 return convert (ptrtype, adr);
1613}
207bf485 1614
f2a71bbc
JM
1615/* Convert the function expression EXP to a pointer. */
1616static tree
1617function_to_pointer_conversion (tree exp)
1618{
1619 tree orig_exp = exp;
207bf485 1620
f2a71bbc 1621 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1622
f2a71bbc 1623 STRIP_TYPE_NOPS (exp);
207bf485 1624
f2a71bbc
JM
1625 if (TREE_NO_WARNING (orig_exp))
1626 TREE_NO_WARNING (exp) = 1;
207bf485 1627
f2a71bbc
JM
1628 return build_unary_op (ADDR_EXPR, exp, 0);
1629}
207bf485 1630
f2a71bbc
JM
1631/* Perform the default conversion of arrays and functions to pointers.
1632 Return the result of converting EXP. For any other expression, just
1633 return EXP after removing NOPs. */
1634
1635struct c_expr
1636default_function_array_conversion (struct c_expr exp)
1637{
1638 tree orig_exp = exp.value;
1639 tree type = TREE_TYPE (exp.value);
1640 enum tree_code code = TREE_CODE (type);
1641
1642 switch (code)
1643 {
1644 case ARRAY_TYPE:
1645 {
1646 bool not_lvalue = false;
1647 bool lvalue_array_p;
1648
1649 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
718699e9
JM
1650 || TREE_CODE (exp.value) == NOP_EXPR
1651 || TREE_CODE (exp.value) == CONVERT_EXPR)
f2a71bbc
JM
1652 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1653 {
1654 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1655 not_lvalue = true;
1656 exp.value = TREE_OPERAND (exp.value, 0);
1657 }
1658
1659 if (TREE_NO_WARNING (orig_exp))
1660 TREE_NO_WARNING (exp.value) = 1;
1661
1662 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1663 if (!flag_isoc99 && !lvalue_array_p)
1664 {
1665 /* Before C99, non-lvalue arrays do not decay to pointers.
1666 Normally, using such an array would be invalid; but it can
1667 be used correctly inside sizeof or as a statement expression.
1668 Thus, do not give an error here; an error will result later. */
1669 return exp;
1670 }
1671
1672 exp.value = array_to_pointer_conversion (exp.value);
1673 }
1674 break;
1675 case FUNCTION_TYPE:
1676 exp.value = function_to_pointer_conversion (exp.value);
1677 break;
1678 default:
1679 STRIP_TYPE_NOPS (exp.value);
1680 if (TREE_NO_WARNING (orig_exp))
1681 TREE_NO_WARNING (exp.value) = 1;
1682 break;
207bf485 1683 }
f2a71bbc 1684
207bf485
JM
1685 return exp;
1686}
1687
522ddfa2
JM
1688
1689/* EXP is an expression of integer type. Apply the integer promotions
1690 to it and return the promoted value. */
400fbf9f
JW
1691
1692tree
522ddfa2 1693perform_integral_promotions (tree exp)
400fbf9f 1694{
b3694847
SS
1695 tree type = TREE_TYPE (exp);
1696 enum tree_code code = TREE_CODE (type);
400fbf9f 1697
522ddfa2 1698 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 1699
400fbf9f
JW
1700 /* Normally convert enums to int,
1701 but convert wide enums to something wider. */
1702 if (code == ENUMERAL_TYPE)
1703 {
b0c48229
NB
1704 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1705 TYPE_PRECISION (integer_type_node)),
1706 ((TYPE_PRECISION (type)
1707 >= TYPE_PRECISION (integer_type_node))
8df83eae 1708 && TYPE_UNSIGNED (type)));
05bccae2 1709
400fbf9f
JW
1710 return convert (type, exp);
1711 }
1712
522ddfa2
JM
1713 /* ??? This should no longer be needed now bit-fields have their
1714 proper types. */
9753f113 1715 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1716 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1717 /* If it's thinner than an int, promote it like a
d72040f5 1718 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1719 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1720 TYPE_PRECISION (integer_type_node)))
f458d1d5 1721 return convert (integer_type_node, exp);
9753f113 1722
d72040f5 1723 if (c_promoting_integer_type_p (type))
400fbf9f 1724 {
f458d1d5 1725 /* Preserve unsignedness if not really getting any wider. */
8df83eae 1726 if (TYPE_UNSIGNED (type)
f458d1d5 1727 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1728 return convert (unsigned_type_node, exp);
05bccae2 1729
400fbf9f
JW
1730 return convert (integer_type_node, exp);
1731 }
05bccae2 1732
522ddfa2
JM
1733 return exp;
1734}
1735
1736
1737/* Perform default promotions for C data used in expressions.
46bdb9cf 1738 Enumeral types or short or char are converted to int.
522ddfa2
JM
1739 In addition, manifest constants symbols are replaced by their values. */
1740
1741tree
1742default_conversion (tree exp)
1743{
1744 tree orig_exp;
1745 tree type = TREE_TYPE (exp);
1746 enum tree_code code = TREE_CODE (type);
1747
46bdb9cf
JM
1748 /* Functions and arrays have been converted during parsing. */
1749 gcc_assert (code != FUNCTION_TYPE);
1750 if (code == ARRAY_TYPE)
1751 return exp;
522ddfa2
JM
1752
1753 /* Constants can be used directly unless they're not loadable. */
1754 if (TREE_CODE (exp) == CONST_DECL)
1755 exp = DECL_INITIAL (exp);
1756
1757 /* Replace a nonvolatile const static variable with its value unless
1758 it is an array, in which case we must be sure that taking the
1759 address of the array produces consistent results. */
1760 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1761 {
1762 exp = decl_constant_value_for_broken_optimization (exp);
1763 type = TREE_TYPE (exp);
1764 }
1765
1766 /* Strip no-op conversions. */
1767 orig_exp = exp;
1768 STRIP_TYPE_NOPS (exp);
1769
1770 if (TREE_NO_WARNING (orig_exp))
1771 TREE_NO_WARNING (exp) = 1;
1772
1773 if (INTEGRAL_TYPE_P (type))
1774 return perform_integral_promotions (exp);
1775
400fbf9f
JW
1776 if (code == VOID_TYPE)
1777 {
1778 error ("void value not ignored as it ought to be");
1779 return error_mark_node;
1780 }
400fbf9f
JW
1781 return exp;
1782}
1783\f
e9b2c823
NB
1784/* Look up COMPONENT in a structure or union DECL.
1785
1786 If the component name is not found, returns NULL_TREE. Otherwise,
1787 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1788 stepping down the chain to the component, which is in the last
1789 TREE_VALUE of the list. Normally the list is of length one, but if
1790 the component is embedded within (nested) anonymous structures or
1791 unions, the list steps down the chain to the component. */
2f6e4e97 1792
2f2d13da 1793static tree
2f6e4e97 1794lookup_field (tree decl, tree component)
2f2d13da 1795{
e9b2c823 1796 tree type = TREE_TYPE (decl);
2f2d13da
DE
1797 tree field;
1798
1799 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1800 to the field elements. Use a binary search on this array to quickly
1801 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1802 will always be set for structures which have many elements. */
1803
22a0b85f 1804 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
1805 {
1806 int bot, top, half;
d07605f5 1807 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
1808
1809 field = TYPE_FIELDS (type);
1810 bot = 0;
d07605f5 1811 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
1812 while (top - bot > 1)
1813 {
2f2d13da
DE
1814 half = (top - bot + 1) >> 1;
1815 field = field_array[bot+half];
1816
1817 if (DECL_NAME (field) == NULL_TREE)
1818 {
1819 /* Step through all anon unions in linear fashion. */
1820 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1821 {
2f2d13da 1822 field = field_array[bot++];
a68b98cf
RK
1823 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1824 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 1825 {
e9b2c823
NB
1826 tree anon = lookup_field (field, component);
1827
1828 if (anon)
1829 return tree_cons (NULL_TREE, field, anon);
2f6e4e97 1830 }
2f2d13da
DE
1831 }
1832
1833 /* Entire record is only anon unions. */
1834 if (bot > top)
1835 return NULL_TREE;
1836
1837 /* Restart the binary search, with new lower bound. */
1838 continue;
1839 }
1840
e8b87aac 1841 if (DECL_NAME (field) == component)
2f2d13da 1842 break;
e8b87aac 1843 if (DECL_NAME (field) < component)
2f2d13da
DE
1844 bot += half;
1845 else
1846 top = bot + half;
1847 }
1848
1849 if (DECL_NAME (field_array[bot]) == component)
1850 field = field_array[bot];
1851 else if (DECL_NAME (field) != component)
e9b2c823 1852 return NULL_TREE;
2f2d13da
DE
1853 }
1854 else
1855 {
1856 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1857 {
e9b2c823
NB
1858 if (DECL_NAME (field) == NULL_TREE
1859 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1860 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 1861 {
e9b2c823 1862 tree anon = lookup_field (field, component);
a68b98cf 1863
e9b2c823
NB
1864 if (anon)
1865 return tree_cons (NULL_TREE, field, anon);
2f2d13da
DE
1866 }
1867
1868 if (DECL_NAME (field) == component)
1869 break;
1870 }
e9b2c823
NB
1871
1872 if (field == NULL_TREE)
1873 return NULL_TREE;
2f2d13da
DE
1874 }
1875
e9b2c823 1876 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
1877}
1878
400fbf9f
JW
1879/* Make an expression to refer to the COMPONENT field of
1880 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1881
1882tree
2f6e4e97 1883build_component_ref (tree datum, tree component)
400fbf9f 1884{
b3694847
SS
1885 tree type = TREE_TYPE (datum);
1886 enum tree_code code = TREE_CODE (type);
1887 tree field = NULL;
1888 tree ref;
400fbf9f 1889
7a3ea201
RH
1890 if (!objc_is_public (datum, component))
1891 return error_mark_node;
1892
400fbf9f
JW
1893 /* See if there is a field or component with name COMPONENT. */
1894
1895 if (code == RECORD_TYPE || code == UNION_TYPE)
1896 {
d0f062fb 1897 if (!COMPLETE_TYPE_P (type))
400fbf9f 1898 {
7a228918 1899 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1900 return error_mark_node;
1901 }
1902
e9b2c823 1903 field = lookup_field (datum, component);
400fbf9f
JW
1904
1905 if (!field)
1906 {
c51a1ba9 1907 error ("%qT has no member named %qE", type, component);
400fbf9f
JW
1908 return error_mark_node;
1909 }
400fbf9f 1910
e9b2c823
NB
1911 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1912 This might be better solved in future the way the C++ front
1913 end does it - by giving the anonymous entities each a
1914 separate name and type, and then have build_component_ref
1915 recursively call itself. We can't do that here. */
46ea50cb 1916 do
19d76e60 1917 {
e9b2c823 1918 tree subdatum = TREE_VALUE (field);
efed193e
JM
1919 int quals;
1920 tree subtype;
e9b2c823
NB
1921
1922 if (TREE_TYPE (subdatum) == error_mark_node)
1923 return error_mark_node;
1924
efed193e
JM
1925 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1926 quals |= TYPE_QUALS (TREE_TYPE (datum));
1927 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1928
1929 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 1930 NULL_TREE);
e9b2c823 1931 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
19d76e60 1932 TREE_READONLY (ref) = 1;
e9b2c823 1933 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
19d76e60 1934 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
1935
1936 if (TREE_DEPRECATED (subdatum))
1937 warn_deprecated_use (subdatum);
1938
19d76e60 1939 datum = ref;
46ea50cb
RS
1940
1941 field = TREE_CHAIN (field);
19d76e60 1942 }
46ea50cb 1943 while (field);
19d76e60 1944
400fbf9f
JW
1945 return ref;
1946 }
1947 else if (code != ERROR_MARK)
c51a1ba9
JM
1948 error ("request for member %qE in something not a structure or union",
1949 component);
400fbf9f
JW
1950
1951 return error_mark_node;
1952}
1953\f
1954/* Given an expression PTR for a pointer, return an expression
1955 for the value pointed to.
1956 ERRORSTRING is the name of the operator to appear in error messages. */
1957
1958tree
2f6e4e97 1959build_indirect_ref (tree ptr, const char *errorstring)
400fbf9f 1960{
b3694847
SS
1961 tree pointer = default_conversion (ptr);
1962 tree type = TREE_TYPE (pointer);
400fbf9f
JW
1963
1964 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 1965 {
79bedddc
SR
1966 if (TREE_CODE (pointer) == CONVERT_EXPR
1967 || TREE_CODE (pointer) == NOP_EXPR
1968 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
1969 {
1970 /* If a warning is issued, mark it to avoid duplicates from
1971 the backend. This only needs to be done at
1972 warn_strict_aliasing > 2. */
1973 if (warn_strict_aliasing > 2)
1974 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
1975 type, TREE_OPERAND (pointer, 0)))
1976 TREE_NO_WARNING (pointer) = 1;
1977 }
1978
870cc33b 1979 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
1980 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1981 == TREE_TYPE (type)))
1982 return TREE_OPERAND (pointer, 0);
1983 else
1984 {
1985 tree t = TREE_TYPE (type);
46df2823
JM
1986 tree ref;
1987
984dfd8c 1988 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 1989
baae9b65 1990 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b
RS
1991 {
1992 error ("dereferencing pointer to incomplete type");
1993 return error_mark_node;
1994 }
baae9b65 1995 if (VOID_TYPE_P (t) && skip_evaluation == 0)
d4ee4d25 1996 warning (0, "dereferencing %<void *%> pointer");
870cc33b
RS
1997
1998 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1999 so that we get the proper error message if the result is used
2000 to assign to. Also, &* is supposed to be a no-op.
2001 And ANSI C seems to specify that the type of the result
2002 should be the const type. */
2003 /* A de-reference of a pointer to const is not a const. It is valid
2004 to change it via some other pointer. */
2005 TREE_READONLY (ref) = TYPE_READONLY (t);
2006 TREE_SIDE_EFFECTS (ref)
271bd540 2007 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2008 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
2009 return ref;
2010 }
2011 }
400fbf9f 2012 else if (TREE_CODE (pointer) != ERROR_MARK)
2c751309 2013 error ("invalid type argument of %qs (have %qT)", errorstring, type);
400fbf9f
JW
2014 return error_mark_node;
2015}
2016
2017/* This handles expressions of the form "a[i]", which denotes
2018 an array reference.
2019
2020 This is logically equivalent in C to *(a+i), but we may do it differently.
2021 If A is a variable or a member, we generate a primitive ARRAY_REF.
2022 This avoids forcing the array out of registers, and can work on
2023 arrays that are not lvalues (for example, members of structures returned
2024 by functions). */
2025
2026tree
2f6e4e97 2027build_array_ref (tree array, tree index)
400fbf9f 2028{
a4ab7973 2029 bool swapped = false;
400fbf9f
JW
2030 if (TREE_TYPE (array) == error_mark_node
2031 || TREE_TYPE (index) == error_mark_node)
2032 return error_mark_node;
2033
a4ab7973
JM
2034 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2035 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
400fbf9f 2036 {
a4ab7973
JM
2037 tree temp;
2038 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2039 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2040 {
a4ab7973 2041 error ("subscripted value is neither array nor pointer");
fdeefd49
RS
2042 return error_mark_node;
2043 }
a4ab7973
JM
2044 temp = array;
2045 array = index;
2046 index = temp;
2047 swapped = true;
2048 }
2049
2050 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2051 {
2052 error ("array subscript is not an integer");
2053 return error_mark_node;
2054 }
2055
2056 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2057 {
2058 error ("subscripted value is pointer to function");
2059 return error_mark_node;
2060 }
2061
ff6b6641
GDR
2062 /* ??? Existing practice has been to warn only when the char
2063 index is syntactically the index, not for char[array]. */
2064 if (!swapped)
2065 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2066
2067 /* Apply default promotions *after* noticing character types. */
2068 index = default_conversion (index);
2069
2070 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2071
2072 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2073 {
2074 tree rval, type;
fdeefd49 2075
400fbf9f
JW
2076 /* An array that is indexed by a non-constant
2077 cannot be stored in a register; we must be able to do
2078 address arithmetic on its address.
2079 Likewise an array of elements of variable size. */
2080 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2081 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2082 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2083 {
dffd7eb6 2084 if (!c_mark_addressable (array))
400fbf9f
JW
2085 return error_mark_node;
2086 }
e6d52559
JW
2087 /* An array that is indexed by a constant value which is not within
2088 the array bounds cannot be stored in a register either; because we
2089 would get a crash in store_bit_field/extract_bit_field when trying
2090 to access a non-existent part of the register. */
2091 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2092 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2093 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2094 {
dffd7eb6 2095 if (!c_mark_addressable (array))
e6d52559
JW
2096 return error_mark_node;
2097 }
400fbf9f 2098
400fbf9f
JW
2099 if (pedantic)
2100 {
2101 tree foo = array;
2102 while (TREE_CODE (foo) == COMPONENT_REF)
2103 foo = TREE_OPERAND (foo, 0);
5baeaac0 2104 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
bda67431 2105 pedwarn ("ISO C forbids subscripting %<register%> array");
3f75a254 2106 else if (!flag_isoc99 && !lvalue_p (foo))
56508306 2107 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2108 }
2109
46df2823
JM
2110 type = TREE_TYPE (TREE_TYPE (array));
2111 if (TREE_CODE (type) != ARRAY_TYPE)
2112 type = TYPE_MAIN_VARIANT (type);
53fb4de3 2113 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2114 /* Array ref is const/volatile if the array elements are
c22cacf3 2115 or if the array is. */
400fbf9f
JW
2116 TREE_READONLY (rval)
2117 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2118 | TREE_READONLY (array));
2119 TREE_SIDE_EFFECTS (rval)
2120 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2121 | TREE_SIDE_EFFECTS (array));
2122 TREE_THIS_VOLATILE (rval)
2123 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2124 /* This was added by rms on 16 Nov 91.
2f6e4e97 2125 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2126 in an inline function.
2127 Hope it doesn't break something else. */
2128 | TREE_THIS_VOLATILE (array));
2129 return require_complete_type (fold (rval));
2130 }
a4ab7973
JM
2131 else
2132 {
2133 tree ar = default_conversion (array);
400fbf9f 2134
a4ab7973
JM
2135 if (ar == error_mark_node)
2136 return ar;
400fbf9f 2137
a4ab7973
JM
2138 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2139 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2140
a4ab7973
JM
2141 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2142 "array indexing");
2143 }
400fbf9f
JW
2144}
2145\f
7e585d16 2146/* Build an external reference to identifier ID. FUN indicates
766beb40
JM
2147 whether this will be used for a function call. LOC is the source
2148 location of the identifier. */
7e585d16 2149tree
766beb40 2150build_external_ref (tree id, int fun, location_t loc)
7e585d16
ZW
2151{
2152 tree ref;
2153 tree decl = lookup_name (id);
16b34ad6
ZL
2154
2155 /* In Objective-C, an instance variable (ivar) may be preferred to
2156 whatever lookup_name() found. */
2157 decl = objc_lookup_ivar (decl, id);
7e585d16 2158
339a28b9 2159 if (decl && decl != error_mark_node)
16b34ad6 2160 ref = decl;
339a28b9
ZW
2161 else if (fun)
2162 /* Implicit function declaration. */
2163 ref = implicitly_declare (id);
2164 else if (decl == error_mark_node)
2165 /* Don't complain about something that's already been
2166 complained about. */
2167 return error_mark_node;
2168 else
2169 {
766beb40 2170 undeclared_variable (id, loc);
339a28b9
ZW
2171 return error_mark_node;
2172 }
7e585d16
ZW
2173
2174 if (TREE_TYPE (ref) == error_mark_node)
2175 return error_mark_node;
2176
339a28b9
ZW
2177 if (TREE_DEPRECATED (ref))
2178 warn_deprecated_use (ref);
2179
ad960f56
MLI
2180 /* Recursive call does not count as usage. */
2181 if (ref != current_function_decl)
2182 {
2183 if (!skip_evaluation)
2184 assemble_external (ref);
2185 TREE_USED (ref) = 1;
2186 }
7e585d16 2187
bc4b653b
JM
2188 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2189 {
2190 if (!in_sizeof && !in_typeof)
2191 C_DECL_USED (ref) = 1;
2192 else if (DECL_INITIAL (ref) == 0
2193 && DECL_EXTERNAL (ref)
2194 && !TREE_PUBLIC (ref))
2195 record_maybe_used_decl (ref);
2196 }
2197
7e585d16
ZW
2198 if (TREE_CODE (ref) == CONST_DECL)
2199 {
6193b8b7 2200 used_types_insert (TREE_TYPE (ref));
7e585d16
ZW
2201 ref = DECL_INITIAL (ref);
2202 TREE_CONSTANT (ref) = 1;
6de9cd9a 2203 TREE_INVARIANT (ref) = 1;
7e585d16 2204 }
6a29edea 2205 else if (current_function_decl != 0
4b1e44be 2206 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2207 && (TREE_CODE (ref) == VAR_DECL
2208 || TREE_CODE (ref) == PARM_DECL
2209 || TREE_CODE (ref) == FUNCTION_DECL))
2210 {
2211 tree context = decl_function_context (ref);
2f6e4e97 2212
6a29edea
EB
2213 if (context != 0 && context != current_function_decl)
2214 DECL_NONLOCAL (ref) = 1;
2215 }
71113fcd
GK
2216 /* C99 6.7.4p3: An inline definition of a function with external
2217 linkage ... shall not contain a reference to an identifier with
2218 internal linkage. */
2219 else if (current_function_decl != 0
2220 && DECL_DECLARED_INLINE_P (current_function_decl)
2221 && DECL_EXTERNAL (current_function_decl)
2222 && VAR_OR_FUNCTION_DECL_P (ref)
2223 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2224 && ! TREE_PUBLIC (ref))
2225 pedwarn ("%H%qD is static but used in inline function %qD "
2226 "which is not static", &loc, ref, current_function_decl);
7e585d16
ZW
2227
2228 return ref;
2229}
2230
bc4b653b
JM
2231/* Record details of decls possibly used inside sizeof or typeof. */
2232struct maybe_used_decl
2233{
2234 /* The decl. */
2235 tree decl;
2236 /* The level seen at (in_sizeof + in_typeof). */
2237 int level;
2238 /* The next one at this level or above, or NULL. */
2239 struct maybe_used_decl *next;
2240};
2241
2242static struct maybe_used_decl *maybe_used_decls;
2243
2244/* Record that DECL, an undefined static function reference seen
2245 inside sizeof or typeof, might be used if the operand of sizeof is
2246 a VLA type or the operand of typeof is a variably modified
2247 type. */
2248
4e2fb7de 2249static void
bc4b653b
JM
2250record_maybe_used_decl (tree decl)
2251{
2252 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2253 t->decl = decl;
2254 t->level = in_sizeof + in_typeof;
2255 t->next = maybe_used_decls;
2256 maybe_used_decls = t;
2257}
2258
2259/* Pop the stack of decls possibly used inside sizeof or typeof. If
2260 USED is false, just discard them. If it is true, mark them used
2261 (if no longer inside sizeof or typeof) or move them to the next
2262 level up (if still inside sizeof or typeof). */
2263
2264void
2265pop_maybe_used (bool used)
2266{
2267 struct maybe_used_decl *p = maybe_used_decls;
2268 int cur_level = in_sizeof + in_typeof;
2269 while (p && p->level > cur_level)
2270 {
2271 if (used)
2272 {
2273 if (cur_level == 0)
2274 C_DECL_USED (p->decl) = 1;
2275 else
2276 p->level = cur_level;
2277 }
2278 p = p->next;
2279 }
2280 if (!used || cur_level == 0)
2281 maybe_used_decls = p;
2282}
2283
2284/* Return the result of sizeof applied to EXPR. */
2285
2286struct c_expr
2287c_expr_sizeof_expr (struct c_expr expr)
2288{
2289 struct c_expr ret;
ad97f4be
JM
2290 if (expr.value == error_mark_node)
2291 {
2292 ret.value = error_mark_node;
2293 ret.original_code = ERROR_MARK;
2294 pop_maybe_used (false);
2295 }
2296 else
2297 {
2298 ret.value = c_sizeof (TREE_TYPE (expr.value));
2299 ret.original_code = ERROR_MARK;
52ffd86e
MS
2300 if (c_vla_type_p (TREE_TYPE (expr.value)))
2301 {
2302 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2303 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2304 }
ad97f4be
JM
2305 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2306 }
bc4b653b
JM
2307 return ret;
2308}
2309
2310/* Return the result of sizeof applied to T, a structure for the type
2311 name passed to sizeof (rather than the type itself). */
2312
2313struct c_expr
f8893e47 2314c_expr_sizeof_type (struct c_type_name *t)
bc4b653b
JM
2315{
2316 tree type;
2317 struct c_expr ret;
2318 type = groktypename (t);
2319 ret.value = c_sizeof (type);
2320 ret.original_code = ERROR_MARK;
16464cc1
VR
2321 pop_maybe_used (type != error_mark_node
2322 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2323 return ret;
2324}
2325
400fbf9f
JW
2326/* Build a function call to function FUNCTION with parameters PARAMS.
2327 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2328 TREE_VALUE of each node is a parameter-expression.
2329 FUNCTION's data type may be a function type or a pointer-to-function. */
2330
2331tree
2f6e4e97 2332build_function_call (tree function, tree params)
400fbf9f 2333{
b3694847 2334 tree fntype, fundecl = 0;
4977bab6 2335 tree name = NULL_TREE, result;
c96f4f73 2336 tree tem;
94a0dd7b
SL
2337 int nargs;
2338 tree *argarray;
2339
400fbf9f 2340
fc76e425 2341 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2342 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2343
2344 /* Convert anything with function type to a pointer-to-function. */
2345 if (TREE_CODE (function) == FUNCTION_DECL)
2346 {
58646b77
PB
2347 /* Implement type-directed function overloading for builtins.
2348 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2349 handle all the type checking. The result is a complete expression
2350 that implements this function call. */
2351 tem = resolve_overloaded_builtin (function, params);
2352 if (tem)
2353 return tem;
48ae6c13 2354
400fbf9f 2355 name = DECL_NAME (function);
a5eadacc 2356 fundecl = function;
400fbf9f 2357 }
f2a71bbc
JM
2358 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2359 function = function_to_pointer_conversion (function);
400fbf9f 2360
6e955430
ZL
2361 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2362 expressions, like those used for ObjC messenger dispatches. */
2363 function = objc_rewrite_function_call (function, params);
2364
400fbf9f
JW
2365 fntype = TREE_TYPE (function);
2366
2367 if (TREE_CODE (fntype) == ERROR_MARK)
2368 return error_mark_node;
2369
2370 if (!(TREE_CODE (fntype) == POINTER_TYPE
2371 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2372 {
f0c721ad 2373 error ("called object %qE is not a function", function);
400fbf9f
JW
2374 return error_mark_node;
2375 }
2376
5ce89b2e
JM
2377 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2378 current_function_returns_abnormally = 1;
2379
400fbf9f
JW
2380 /* fntype now gets the type of function pointed to. */
2381 fntype = TREE_TYPE (fntype);
2382
c96f4f73
EB
2383 /* Check that the function is called through a compatible prototype.
2384 If it is not, replace the call by a trap, wrapped up in a compound
2385 expression if necessary. This has the nice side-effect to prevent
2386 the tree-inliner from generating invalid assignment trees which may
58393038 2387 blow up in the RTL expander later. */
718699e9
JM
2388 if ((TREE_CODE (function) == NOP_EXPR
2389 || TREE_CODE (function) == CONVERT_EXPR)
c96f4f73
EB
2390 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2391 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2392 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2393 {
2394 tree return_type = TREE_TYPE (fntype);
2395 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2396 NULL_TREE);
2397
2398 /* This situation leads to run-time undefined behavior. We can't,
2399 therefore, simply error unless we can prove that all possible
2400 executions of the program must execute the code. */
d4ee4d25 2401 warning (0, "function called through a non-compatible type");
c96f4f73 2402
bba745c1
EB
2403 /* We can, however, treat "undefined" any way we please.
2404 Call abort to encourage the user to fix the program. */
2405 inform ("if this code is reached, the program will abort");
2406
c96f4f73
EB
2407 if (VOID_TYPE_P (return_type))
2408 return trap;
2409 else
2410 {
2411 tree rhs;
2412
2413 if (AGGREGATE_TYPE_P (return_type))
2414 rhs = build_compound_literal (return_type,
4038c495 2415 build_constructor (return_type, 0));
c96f4f73 2416 else
4c7a6c1b 2417 rhs = fold_convert (return_type, integer_zero_node);
c96f4f73 2418
53fb4de3 2419 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
c96f4f73
EB
2420 }
2421 }
2422
400fbf9f
JW
2423 /* Convert the parameters to the types declared in the
2424 function prototype, or apply default promotions. */
2425
94a0dd7b
SL
2426 nargs = list_length (params);
2427 argarray = (tree *) alloca (nargs * sizeof (tree));
2428 nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype),
2429 params, function, fundecl);
2430 if (nargs < 0)
3789b316
JM
2431 return error_mark_node;
2432
b34c7881 2433 /* Check that the arguments to the function are valid. */
400fbf9f 2434
94a0dd7b 2435 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
10a22b11 2436 TYPE_ARG_TYPES (fntype));
400fbf9f 2437
bf730f15
RS
2438 if (require_constant_value)
2439 {
94a0dd7b
SL
2440 result = fold_build_call_array_initializer (TREE_TYPE (fntype),
2441 function, nargs, argarray);
bf730f15
RS
2442 if (TREE_CONSTANT (result)
2443 && (name == NULL_TREE
2444 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2445 pedwarn_init ("initializer element is not constant");
2446 }
2447 else
94a0dd7b
SL
2448 result = fold_build_call_array (TREE_TYPE (fntype),
2449 function, nargs, argarray);
b0b3afb2 2450
71653180 2451 if (VOID_TYPE_P (TREE_TYPE (result)))
1eb8759b
RH
2452 return result;
2453 return require_complete_type (result);
400fbf9f
JW
2454}
2455\f
2456/* Convert the argument expressions in the list VALUES
94a0dd7b
SL
2457 to the types in the list TYPELIST. The resulting arguments are
2458 stored in the array ARGARRAY which has size NARGS.
400fbf9f
JW
2459
2460 If TYPELIST is exhausted, or when an element has NULL as its type,
2461 perform the default conversions.
2462
2463 PARMLIST is the chain of parm decls for the function being called.
2464 It may be 0, if that info is not available.
2465 It is used only for generating error messages.
2466
03dafa61
JM
2467 FUNCTION is a tree for the called function. It is used only for
2468 error messages, where it is formatted with %qE.
400fbf9f
JW
2469
2470 This is also where warnings about wrong number of args are generated.
2471
94a0dd7b
SL
2472 VALUES is a chain of TREE_LIST nodes with the elements of the list
2473 in the TREE_VALUE slots of those nodes.
400fbf9f 2474
94a0dd7b
SL
2475 Returns the actual number of arguments processed (which may be less
2476 than NARGS in some error situations), or -1 on failure. */
2477
2478static int
2479convert_arguments (int nargs, tree *argarray,
2480 tree typelist, tree values, tree function, tree fundecl)
400fbf9f 2481{
b3694847 2482 tree typetail, valtail;
400fbf9f 2483 int parmnum;
b5d32c25
KG
2484 const bool type_generic = fundecl
2485 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
03dafa61 2486 tree selector;
03dafa61 2487
2ac2f164
JM
2488 /* Change pointer to function to the function itself for
2489 diagnostics. */
03dafa61
JM
2490 if (TREE_CODE (function) == ADDR_EXPR
2491 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 2492 function = TREE_OPERAND (function, 0);
03dafa61
JM
2493
2494 /* Handle an ObjC selector specially for diagnostics. */
2495 selector = objc_message_selector ();
400fbf9f
JW
2496
2497 /* Scan the given expressions and types, producing individual
94a0dd7b 2498 converted arguments and storing them in ARGARRAY. */
400fbf9f
JW
2499
2500 for (valtail = values, typetail = typelist, parmnum = 0;
2501 valtail;
2502 valtail = TREE_CHAIN (valtail), parmnum++)
2503 {
b3694847
SS
2504 tree type = typetail ? TREE_VALUE (typetail) : 0;
2505 tree val = TREE_VALUE (valtail);
03dafa61
JM
2506 tree rname = function;
2507 int argnum = parmnum + 1;
4d3e6fae 2508 const char *invalid_func_diag;
400fbf9f
JW
2509
2510 if (type == void_type_node)
2511 {
03dafa61 2512 error ("too many arguments to function %qE", function);
94a0dd7b 2513 return parmnum;
400fbf9f
JW
2514 }
2515
03dafa61
JM
2516 if (selector && argnum > 2)
2517 {
2518 rname = selector;
2519 argnum -= 2;
2520 }
2521
ed248cf7 2522 STRIP_TYPE_NOPS (val);
400fbf9f 2523
400fbf9f
JW
2524 val = require_complete_type (val);
2525
2526 if (type != 0)
2527 {
2528 /* Formal parm type is specified by a function prototype. */
2529 tree parmval;
2530
20913689 2531 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
2532 {
2533 error ("type of formal parameter %d is incomplete", parmnum + 1);
2534 parmval = val;
2535 }
2536 else
2537 {
d45cf215
RS
2538 /* Optionally warn about conversions that
2539 differ from the default conversions. */
05170031 2540 if (warn_traditional_conversion || warn_traditional)
400fbf9f 2541 {
e3a64162 2542 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 2543
aae43c5f 2544 if (INTEGRAL_TYPE_P (type)
400fbf9f 2545 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
d4ee4d25 2546 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
2547 "rather than floating due to prototype",
2548 argnum, rname);
03829ad2
KG
2549 if (INTEGRAL_TYPE_P (type)
2550 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
d4ee4d25 2551 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
2552 "rather than complex due to prototype",
2553 argnum, rname);
aae43c5f
RK
2554 else if (TREE_CODE (type) == COMPLEX_TYPE
2555 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
d4ee4d25 2556 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
2557 "rather than floating due to prototype",
2558 argnum, rname);
400fbf9f 2559 else if (TREE_CODE (type) == REAL_TYPE
aae43c5f 2560 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
d4ee4d25 2561 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
2562 "rather than integer due to prototype",
2563 argnum, rname);
03829ad2
KG
2564 else if (TREE_CODE (type) == COMPLEX_TYPE
2565 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
d4ee4d25 2566 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
2567 "rather than integer due to prototype",
2568 argnum, rname);
aae43c5f
RK
2569 else if (TREE_CODE (type) == REAL_TYPE
2570 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
d4ee4d25 2571 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
2572 "rather than complex due to prototype",
2573 argnum, rname);
aae43c5f
RK
2574 /* ??? At some point, messages should be written about
2575 conversions between complex types, but that's too messy
2576 to do now. */
d45cf215
RS
2577 else if (TREE_CODE (type) == REAL_TYPE
2578 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2579 {
2580 /* Warn if any argument is passed as `float',
047de90b 2581 since without a prototype it would be `double'. */
9a8ce21f
JG
2582 if (formal_prec == TYPE_PRECISION (float_type_node)
2583 && type != dfloat32_type_node)
d4ee4d25 2584 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
2585 "rather than %<double%> due to prototype",
2586 argnum, rname);
9a8ce21f
JG
2587
2588 /* Warn if mismatch between argument and prototype
2589 for decimal float types. Warn of conversions with
2590 binary float types and of precision narrowing due to
2591 prototype. */
2592 else if (type != TREE_TYPE (val)
2593 && (type == dfloat32_type_node
2594 || type == dfloat64_type_node
c22cacf3 2595 || type == dfloat128_type_node
9a8ce21f
JG
2596 || TREE_TYPE (val) == dfloat32_type_node
2597 || TREE_TYPE (val) == dfloat64_type_node
2598 || TREE_TYPE (val) == dfloat128_type_node)
c22cacf3 2599 && (formal_prec
9a8ce21f
JG
2600 <= TYPE_PRECISION (TREE_TYPE (val))
2601 || (type == dfloat128_type_node
2602 && (TREE_TYPE (val)
c22cacf3
MS
2603 != dfloat64_type_node
2604 && (TREE_TYPE (val)
9a8ce21f
JG
2605 != dfloat32_type_node)))
2606 || (type == dfloat64_type_node
2607 && (TREE_TYPE (val)
2608 != dfloat32_type_node))))
2609 warning (0, "passing argument %d of %qE as %qT "
2610 "rather than %qT due to prototype",
2611 argnum, rname, type, TREE_TYPE (val));
2612
d45cf215 2613 }
3ed56f8a
KG
2614 /* Detect integer changing in width or signedness.
2615 These warnings are only activated with
05170031
MLI
2616 -Wtraditional-conversion, not with -Wtraditional. */
2617 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
aae43c5f 2618 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
400fbf9f 2619 {
d45cf215
RS
2620 tree would_have_been = default_conversion (val);
2621 tree type1 = TREE_TYPE (would_have_been);
2622
754a4d82 2623 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a
NB
2624 && (TYPE_MAIN_VARIANT (type)
2625 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
754a4d82
RS
2626 /* No warning if function asks for enum
2627 and the actual arg is that enum type. */
2628 ;
2629 else if (formal_prec != TYPE_PRECISION (type1))
05170031 2630 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
3176a0c2
DD
2631 "with different width due to prototype",
2632 argnum, rname);
8df83eae 2633 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 2634 ;
800cd3b9
RS
2635 /* Don't complain if the formal parameter type
2636 is an enum, because we can't tell now whether
2637 the value was an enum--even the same enum. */
2638 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2639 ;
400fbf9f
JW
2640 else if (TREE_CODE (val) == INTEGER_CST
2641 && int_fits_type_p (val, type))
2642 /* Change in signedness doesn't matter
2643 if a constant value is unaffected. */
2644 ;
ce9895ae
RS
2645 /* If the value is extended from a narrower
2646 unsigned type, it doesn't matter whether we
2647 pass it as signed or unsigned; the value
2648 certainly is the same either way. */
2649 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
8df83eae 2650 && TYPE_UNSIGNED (TREE_TYPE (val)))
ce9895ae 2651 ;
8df83eae 2652 else if (TYPE_UNSIGNED (type))
05170031 2653 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
3176a0c2
DD
2654 "as unsigned due to prototype",
2655 argnum, rname);
3ed56f8a 2656 else
05170031 2657 warning (OPT_Wtraditional_conversion, "passing argument %d of %qE "
3176a0c2 2658 "as signed due to prototype", argnum, rname);
400fbf9f
JW
2659 }
2660 }
2661
2ac2f164
JM
2662 parmval = convert_for_assignment (type, val, ic_argpass,
2663 fundecl, function,
2664 parmnum + 1);
2f6e4e97 2665
61f71b34 2666 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 2667 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
2668 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2669 parmval = default_conversion (parmval);
400fbf9f 2670 }
94a0dd7b 2671 argarray[parmnum] = parmval;
400fbf9f
JW
2672 }
2673 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
c22cacf3
MS
2674 && (TYPE_PRECISION (TREE_TYPE (val))
2675 < TYPE_PRECISION (double_type_node))
9a8ce21f 2676 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
b5d32c25
KG
2677 {
2678 if (type_generic)
2679 argarray[parmnum] = val;
2680 else
2681 /* Convert `float' to `double'. */
2682 argarray[parmnum] = convert (double_type_node, val);
2683 }
c22cacf3
MS
2684 else if ((invalid_func_diag =
2685 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
2686 {
2687 error (invalid_func_diag);
94a0dd7b 2688 return -1;
4d3e6fae 2689 }
400fbf9f
JW
2690 else
2691 /* Convert `short' and `char' to full-size `int'. */
94a0dd7b 2692 argarray[parmnum] = default_conversion (val);
400fbf9f
JW
2693
2694 if (typetail)
2695 typetail = TREE_CHAIN (typetail);
2696 }
2697
94a0dd7b
SL
2698 gcc_assert (parmnum == nargs);
2699
400fbf9f 2700 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316
JM
2701 {
2702 error ("too few arguments to function %qE", function);
94a0dd7b 2703 return -1;
3789b316 2704 }
400fbf9f 2705
94a0dd7b 2706 return parmnum;
400fbf9f
JW
2707}
2708\f
43f6dfd3
RS
2709/* This is the entry point used by the parser to build unary operators
2710 in the input. CODE, a tree_code, specifies the unary operator, and
2711 ARG is the operand. For unary plus, the C parser currently uses
2712 CONVERT_EXPR for code. */
2713
2714struct c_expr
2715parser_build_unary_op (enum tree_code code, struct c_expr arg)
2716{
2717 struct c_expr result;
2718
2719 result.original_code = ERROR_MARK;
2720 result.value = build_unary_op (code, arg.value, 0);
59c0753d
MLI
2721
2722 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2723 overflow_warning (result.value);
2724
43f6dfd3
RS
2725 return result;
2726}
2727
2728/* This is the entry point used by the parser to build binary operators
2729 in the input. CODE, a tree_code, specifies the binary operator, and
2730 ARG1 and ARG2 are the operands. In addition to constructing the
2731 expression, we check for operands that were written with other binary
2732 operators in a way that is likely to confuse the user. */
edc7c4ec 2733
487a92fe
JM
2734struct c_expr
2735parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2736 struct c_expr arg2)
400fbf9f 2737{
487a92fe 2738 struct c_expr result;
400fbf9f 2739
487a92fe
JM
2740 enum tree_code code1 = arg1.original_code;
2741 enum tree_code code2 = arg2.original_code;
400fbf9f 2742
487a92fe
JM
2743 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2744 result.original_code = code;
58bf601b 2745
487a92fe
JM
2746 if (TREE_CODE (result.value) == ERROR_MARK)
2747 return result;
400fbf9f
JW
2748
2749 /* Check for cases such as x+y<<z which users are likely
487a92fe 2750 to misinterpret. */
400fbf9f 2751 if (warn_parentheses)
2a67bec2 2752 warn_about_parentheses (code, code1, code2);
001af587 2753
63a08740
DM
2754 if (code1 != tcc_comparison)
2755 warn_logical_operator (code, arg1.value, arg2.value);
2756
e994a705
RS
2757 /* Warn about comparisons against string literals, with the exception
2758 of testing for equality or inequality of a string literal with NULL. */
2759 if (code == EQ_EXPR || code == NE_EXPR)
2760 {
2761 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2762 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
4dad0aca 2763 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
e994a705
RS
2764 }
2765 else if (TREE_CODE_CLASS (code) == tcc_comparison
2766 && (code1 == STRING_CST || code2 == STRING_CST))
4dad0aca 2767 warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
e994a705 2768
59c0753d
MLI
2769 if (TREE_OVERFLOW_P (result.value)
2770 && !TREE_OVERFLOW_P (arg1.value)
2771 && !TREE_OVERFLOW_P (arg2.value))
2772 overflow_warning (result.value);
400fbf9f
JW
2773
2774 return result;
2775}
3e4093b6 2776\f
3e4093b6
RS
2777/* Return a tree for the difference of pointers OP0 and OP1.
2778 The resulting tree has type int. */
293c9fdd 2779
3e4093b6
RS
2780static tree
2781pointer_diff (tree op0, tree op1)
2782{
3e4093b6 2783 tree restype = ptrdiff_type_node;
400fbf9f 2784
3e4093b6
RS
2785 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2786 tree con0, con1, lit0, lit1;
2787 tree orig_op1 = op1;
400fbf9f 2788
3e4093b6
RS
2789 if (pedantic || warn_pointer_arith)
2790 {
2791 if (TREE_CODE (target_type) == VOID_TYPE)
bda67431 2792 pedwarn ("pointer of type %<void *%> used in subtraction");
3e4093b6
RS
2793 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2794 pedwarn ("pointer to a function used in subtraction");
2795 }
400fbf9f 2796
3e4093b6
RS
2797 /* If the conversion to ptrdiff_type does anything like widening or
2798 converting a partial to an integral mode, we get a convert_expression
2799 that is in the way to do any simplifications.
2800 (fold-const.c doesn't know that the extra bits won't be needed.
2801 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2802 different mode in place.)
2803 So first try to find a common term here 'by hand'; we want to cover
2804 at least the cases that occur in legal static initializers. */
1344f9a3
JM
2805 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2806 && (TYPE_PRECISION (TREE_TYPE (op0))
2807 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2808 con0 = TREE_OPERAND (op0, 0);
2809 else
2810 con0 = op0;
2811 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2812 && (TYPE_PRECISION (TREE_TYPE (op1))
2813 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2814 con1 = TREE_OPERAND (op1, 0);
2815 else
2816 con1 = op1;
400fbf9f 2817
3e4093b6
RS
2818 if (TREE_CODE (con0) == PLUS_EXPR)
2819 {
2820 lit0 = TREE_OPERAND (con0, 1);
2821 con0 = TREE_OPERAND (con0, 0);
2822 }
2823 else
2824 lit0 = integer_zero_node;
400fbf9f 2825
3e4093b6 2826 if (TREE_CODE (con1) == PLUS_EXPR)
400fbf9f 2827 {
3e4093b6
RS
2828 lit1 = TREE_OPERAND (con1, 1);
2829 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
2830 }
2831 else
3e4093b6
RS
2832 lit1 = integer_zero_node;
2833
2834 if (operand_equal_p (con0, con1, 0))
400fbf9f 2835 {
3e4093b6
RS
2836 op0 = lit0;
2837 op1 = lit1;
400fbf9f
JW
2838 }
2839
400fbf9f 2840
3e4093b6
RS
2841 /* First do the subtraction as integers;
2842 then drop through to build the divide operator.
2843 Do not do default conversions on the minus operator
2844 in case restype is a short type. */
400fbf9f 2845
3e4093b6
RS
2846 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2847 convert (restype, op1), 0);
2848 /* This generates an error if op1 is pointer to incomplete type. */
2849 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2850 error ("arithmetic on pointer to an incomplete type");
400fbf9f 2851
3e4093b6
RS
2852 /* This generates an error if op0 is pointer to incomplete type. */
2853 op1 = c_size_in_bytes (target_type);
400fbf9f 2854
3e4093b6 2855 /* Divide by the size, in easiest possible way. */
987b67bc 2856 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3e4093b6
RS
2857}
2858\f
2859/* Construct and perhaps optimize a tree representation
2860 for a unary operation. CODE, a tree_code, specifies the operation
2861 and XARG is the operand.
2862 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2863 the default promotions (such as from short to int).
2864 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2865 allows non-lvalues; this is only used to handle conversion of non-lvalue
2866 arrays to pointers in C99. */
400fbf9f 2867
3e4093b6
RS
2868tree
2869build_unary_op (enum tree_code code, tree xarg, int flag)
2870{
2871 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2872 tree arg = xarg;
2873 tree argtype = 0;
2874 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2875 tree val;
2876 int noconvert = flag;
4de67c26 2877 const char *invalid_op_diag;
400fbf9f 2878
3e4093b6
RS
2879 if (typecode == ERROR_MARK)
2880 return error_mark_node;
2881 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2882 typecode = INTEGER_TYPE;
6c36d76b 2883
4de67c26
JM
2884 if ((invalid_op_diag
2885 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2886 {
2887 error (invalid_op_diag);
2888 return error_mark_node;
2889 }
2890
3e4093b6
RS
2891 switch (code)
2892 {
2893 case CONVERT_EXPR:
2894 /* This is used for unary plus, because a CONVERT_EXPR
2895 is enough to prevent anybody from looking inside for
2896 associativity, but won't generate any code. */
2897 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 2898 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 2899 || typecode == VECTOR_TYPE))
400fbf9f 2900 {
3e4093b6
RS
2901 error ("wrong type argument to unary plus");
2902 return error_mark_node;
400fbf9f 2903 }
3e4093b6
RS
2904 else if (!noconvert)
2905 arg = default_conversion (arg);
2906 arg = non_lvalue (arg);
400fbf9f
JW
2907 break;
2908
3e4093b6
RS
2909 case NEGATE_EXPR:
2910 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 2911 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
2912 || typecode == VECTOR_TYPE))
2913 {
2914 error ("wrong type argument to unary minus");
2915 return error_mark_node;
2916 }
2917 else if (!noconvert)
2918 arg = default_conversion (arg);
400fbf9f
JW
2919 break;
2920
3e4093b6 2921 case BIT_NOT_EXPR:
462643f0
AP
2922 /* ~ works on integer types and non float vectors. */
2923 if (typecode == INTEGER_TYPE
2924 || (typecode == VECTOR_TYPE
2925 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 2926 {
3e4093b6
RS
2927 if (!noconvert)
2928 arg = default_conversion (arg);
03d5b1f5 2929 }
3e4093b6 2930 else if (typecode == COMPLEX_TYPE)
400fbf9f 2931 {
3e4093b6
RS
2932 code = CONJ_EXPR;
2933 if (pedantic)
bda67431 2934 pedwarn ("ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
2935 if (!noconvert)
2936 arg = default_conversion (arg);
2937 }
2938 else
2939 {
2940 error ("wrong type argument to bit-complement");
2941 return error_mark_node;
400fbf9f
JW
2942 }
2943 break;
2944
3e4093b6 2945 case ABS_EXPR:
11017cc7 2946 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 2947 {
3e4093b6
RS
2948 error ("wrong type argument to abs");
2949 return error_mark_node;
400fbf9f 2950 }
3e4093b6
RS
2951 else if (!noconvert)
2952 arg = default_conversion (arg);
400fbf9f
JW
2953 break;
2954
3e4093b6
RS
2955 case CONJ_EXPR:
2956 /* Conjugating a real value is a no-op, but allow it anyway. */
2957 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2958 || typecode == COMPLEX_TYPE))
400fbf9f 2959 {
3e4093b6
RS
2960 error ("wrong type argument to conjugation");
2961 return error_mark_node;
400fbf9f 2962 }
3e4093b6
RS
2963 else if (!noconvert)
2964 arg = default_conversion (arg);
400fbf9f
JW
2965 break;
2966
3e4093b6 2967 case TRUTH_NOT_EXPR:
ab22c1fa 2968 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 2969 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 2970 && typecode != COMPLEX_TYPE)
400fbf9f 2971 {
3e4093b6
RS
2972 error ("wrong type argument to unary exclamation mark");
2973 return error_mark_node;
400fbf9f 2974 }
85498824 2975 arg = c_objc_common_truthvalue_conversion (arg);
3e4093b6
RS
2976 return invert_truthvalue (arg);
2977
3e4093b6
RS
2978 case REALPART_EXPR:
2979 if (TREE_CODE (arg) == COMPLEX_CST)
2980 return TREE_REALPART (arg);
2981 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
987b67bc 2982 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3e4093b6
RS
2983 else
2984 return arg;
605a99f6 2985
3e4093b6
RS
2986 case IMAGPART_EXPR:
2987 if (TREE_CODE (arg) == COMPLEX_CST)
2988 return TREE_IMAGPART (arg);
2989 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
987b67bc 2990 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3e4093b6
RS
2991 else
2992 return convert (TREE_TYPE (arg), integer_zero_node);
2993
2994 case PREINCREMENT_EXPR:
2995 case POSTINCREMENT_EXPR:
2996 case PREDECREMENT_EXPR:
2997 case POSTDECREMENT_EXPR:
3e4093b6
RS
2998
2999 /* Increment or decrement the real part of the value,
3000 and don't change the imaginary part. */
3001 if (typecode == COMPLEX_TYPE)
400fbf9f 3002 {
3e4093b6
RS
3003 tree real, imag;
3004
3005 if (pedantic)
bda67431
JM
3006 pedwarn ("ISO C does not support %<++%> and %<--%>"
3007 " on complex types");
3e4093b6
RS
3008
3009 arg = stabilize_reference (arg);
3010 real = build_unary_op (REALPART_EXPR, arg, 1);
3011 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
a87db577
AP
3012 real = build_unary_op (code, real, 1);
3013 if (real == error_mark_node || imag == error_mark_node)
3014 return error_mark_node;
53fb4de3 3015 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
a87db577 3016 real, imag);
400fbf9f 3017 }
3e4093b6
RS
3018
3019 /* Report invalid types. */
3020
ab22c1fa 3021 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3022 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
400fbf9f 3023 {
3e4093b6 3024 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c22cacf3
MS
3025 error ("wrong type argument to increment");
3026 else
3027 error ("wrong type argument to decrement");
3e4093b6
RS
3028
3029 return error_mark_node;
400fbf9f 3030 }
400fbf9f 3031
3e4093b6
RS
3032 {
3033 tree inc;
3034 tree result_type = TREE_TYPE (arg);
400fbf9f 3035
3e4093b6
RS
3036 arg = get_unwidened (arg, 0);
3037 argtype = TREE_TYPE (arg);
3038
3039 /* Compute the increment. */
3040
3041 if (typecode == POINTER_TYPE)
3042 {
3043 /* If pointer target is an undefined struct,
3044 we just cannot know how to do the arithmetic. */
3045 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
3046 {
3047 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3048 error ("increment of pointer to unknown structure");
3049 else
3050 error ("decrement of pointer to unknown structure");
3051 }
3052 else if ((pedantic || warn_pointer_arith)
3053 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3054 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
c22cacf3 3055 {
3e4093b6
RS
3056 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3057 pedwarn ("wrong type argument to increment");
3058 else
3059 pedwarn ("wrong type argument to decrement");
3060 }
3061
3062 inc = c_size_in_bytes (TREE_TYPE (result_type));
5be014d5 3063 inc = fold_convert (sizetype, inc);
3e4093b6 3064 }
ab22c1fa
CF
3065 else if (FRACT_MODE_P (TYPE_MODE (result_type)))
3066 {
3067 /* For signed fract types, we invert ++ to -- or
3068 -- to ++, and change inc from 1 to -1, because
3069 it is not possible to represent 1 in signed fract constants.
3070 For unsigned fract types, the result always overflows and
3071 we get an undefined (original) or the maximum value. */
3072 if (code == PREINCREMENT_EXPR)
3073 code = PREDECREMENT_EXPR;
3074 else if (code == PREDECREMENT_EXPR)
3075 code = PREINCREMENT_EXPR;
3076 else if (code == POSTINCREMENT_EXPR)
3077 code = POSTDECREMENT_EXPR;
3078 else /* code == POSTDECREMENT_EXPR */
3079 code = POSTINCREMENT_EXPR;
3080
3081 inc = integer_minus_one_node;
3082 inc = convert (argtype, inc);
3083 }
3e4093b6 3084 else
5be014d5
AP
3085 {
3086 inc = integer_one_node;
3087 inc = convert (argtype, inc);
3088 }
3e4093b6 3089
3e4093b6
RS
3090 /* Complain about anything else that is not a true lvalue. */
3091 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3092 || code == POSTINCREMENT_EXPR)
9bf24266
JM
3093 ? lv_increment
3094 : lv_decrement)))
3e4093b6
RS
3095 return error_mark_node;
3096
3097 /* Report a read-only lvalue. */
3098 if (TREE_READONLY (arg))
953ff289
DN
3099 {
3100 readonly_error (arg,
3101 ((code == PREINCREMENT_EXPR
3102 || code == POSTINCREMENT_EXPR)
3103 ? lv_increment : lv_decrement));
3104 return error_mark_node;
3105 }
3e4093b6
RS
3106
3107 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3108 val = boolean_increment (code, arg);
3109 else
53fb4de3 3110 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6
RS
3111 TREE_SIDE_EFFECTS (val) = 1;
3112 val = convert (result_type, val);
3113 if (TREE_CODE (val) != code)
6de9cd9a 3114 TREE_NO_WARNING (val) = 1;
3e4093b6
RS
3115 return val;
3116 }
3117
3118 case ADDR_EXPR:
3119 /* Note that this operation never does default_conversion. */
3120
3121 /* Let &* cancel out to simplify resulting code. */
3122 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 3123 {
3e4093b6
RS
3124 /* Don't let this be an lvalue. */
3125 if (lvalue_p (TREE_OPERAND (arg, 0)))
3126 return non_lvalue (TREE_OPERAND (arg, 0));
3127 return TREE_OPERAND (arg, 0);
400fbf9f 3128 }
1eb8759b 3129
7c672dfc 3130 /* For &x[y], return x+y */
3e4093b6 3131 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 3132 {
f2a71bbc
JM
3133 tree op0 = TREE_OPERAND (arg, 0);
3134 if (!c_mark_addressable (op0))
3e4093b6 3135 return error_mark_node;
46bdb9cf 3136 return build_binary_op (PLUS_EXPR,
f2a71bbc
JM
3137 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3138 ? array_to_pointer_conversion (op0)
3139 : op0),
7c672dfc 3140 TREE_OPERAND (arg, 1), 1);
1eb8759b 3141 }
1eb8759b 3142
3e4093b6
RS
3143 /* Anything not already handled and not a true memory reference
3144 or a non-lvalue array is an error. */
3145 else if (typecode != FUNCTION_TYPE && !flag
9bf24266 3146 && !lvalue_or_else (arg, lv_addressof))
3e4093b6 3147 return error_mark_node;
b6a10c9f 3148
3e4093b6
RS
3149 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3150 argtype = TREE_TYPE (arg);
400fbf9f 3151
3e4093b6 3152 /* If the lvalue is const or volatile, merge that into the type
c22cacf3 3153 to which the address will point. Note that you can't get a
3e4093b6
RS
3154 restricted pointer by taking the address of something, so we
3155 only have to deal with `const' and `volatile' here. */
6615c446 3156 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3e4093b6
RS
3157 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3158 argtype = c_build_type_variant (argtype,
3159 TREE_READONLY (arg),
3160 TREE_THIS_VOLATILE (arg));
400fbf9f 3161
3e4093b6
RS
3162 if (!c_mark_addressable (arg))
3163 return error_mark_node;
400fbf9f 3164
abb54d14
JM
3165 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3166 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 3167
5cc200fc 3168 argtype = build_pointer_type (argtype);
5e55f99d
RH
3169
3170 /* ??? Cope with user tricks that amount to offsetof. Delete this
3171 when we have proper support for integer constant expressions. */
3172 val = get_base_address (arg);
3173 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
3174 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3175 {
5be014d5 3176 tree op0 = fold_convert (sizetype, fold_offsetof (arg, val)), op1;
3aa2ddb8
JJ
3177
3178 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
5be014d5 3179 return fold_build2 (POINTER_PLUS_EXPR, argtype, op1, op0);
3aa2ddb8 3180 }
5e55f99d 3181
5cc200fc 3182 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 3183
5cc200fc 3184 return val;
400fbf9f 3185
3e4093b6 3186 default:
1344f9a3 3187 gcc_unreachable ();
3e4093b6 3188 }
400fbf9f 3189
3e4093b6
RS
3190 if (argtype == 0)
3191 argtype = TREE_TYPE (arg);
00d1b1d6 3192 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
c22cacf3 3193 : fold_build1 (code, argtype, arg);
3e4093b6 3194}
400fbf9f 3195
3e4093b6
RS
3196/* Return nonzero if REF is an lvalue valid for this language.
3197 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 3198 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 3199
37dc0d8d 3200static int
58f9752a 3201lvalue_p (const_tree ref)
3e4093b6 3202{
58f9752a 3203 const enum tree_code code = TREE_CODE (ref);
400fbf9f 3204
3e4093b6
RS
3205 switch (code)
3206 {
3207 case REALPART_EXPR:
3208 case IMAGPART_EXPR:
3209 case COMPONENT_REF:
3210 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 3211
3e4093b6
RS
3212 case COMPOUND_LITERAL_EXPR:
3213 case STRING_CST:
3214 return 1;
400fbf9f 3215
3e4093b6
RS
3216 case INDIRECT_REF:
3217 case ARRAY_REF:
3218 case VAR_DECL:
3219 case PARM_DECL:
3220 case RESULT_DECL:
3221 case ERROR_MARK:
3222 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3223 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 3224
3e4093b6 3225 case BIND_EXPR:
3e4093b6 3226 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 3227
3e4093b6
RS
3228 default:
3229 return 0;
3230 }
3231}
400fbf9f 3232\f
9bf24266 3233/* Give an error for storing in something that is 'const'. */
54c93c30 3234
9bf24266
JM
3235static void
3236readonly_error (tree arg, enum lvalue_use use)
54c93c30 3237{
5544530a
PB
3238 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3239 || use == lv_asm);
9bf24266
JM
3240 /* Using this macro rather than (for example) arrays of messages
3241 ensures that all the format strings are checked at compile
3242 time. */
5544530a 3243#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
c22cacf3 3244 : (use == lv_increment ? (I) \
5544530a 3245 : (use == lv_decrement ? (D) : (AS))))
3e4093b6 3246 if (TREE_CODE (arg) == COMPONENT_REF)
54c93c30 3247 {
3e4093b6 3248 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9bf24266 3249 readonly_error (TREE_OPERAND (arg, 0), use);
3e4093b6 3250 else
4b794eaf
JJ
3251 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3252 G_("increment of read-only member %qD"),
5544530a
PB
3253 G_("decrement of read-only member %qD"),
3254 G_("read-only member %qD used as %<asm%> output")),
c51a1ba9 3255 TREE_OPERAND (arg, 1));
54c93c30 3256 }
3e4093b6 3257 else if (TREE_CODE (arg) == VAR_DECL)
4b794eaf
JJ
3258 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3259 G_("increment of read-only variable %qD"),
5544530a
PB
3260 G_("decrement of read-only variable %qD"),
3261 G_("read-only variable %qD used as %<asm%> output")),
c51a1ba9 3262 arg);
3e4093b6 3263 else
d7705934
DB
3264 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3265 G_("increment of read-only location %qE"),
3266 G_("decrement of read-only location %qE"),
3267 G_("read-only location %qE used as %<asm%> output")),
3268 arg);
54c93c30 3269}
37dc0d8d
JM
3270
3271
3272/* Return nonzero if REF is an lvalue valid for this language;
3273 otherwise, print an error message and return zero. USE says
3274 how the lvalue is being used and so selects the error message. */
3275
3276static int
58f9752a 3277lvalue_or_else (const_tree ref, enum lvalue_use use)
37dc0d8d
JM
3278{
3279 int win = lvalue_p (ref);
3280
3281 if (!win)
3282 lvalue_error (use);
3283
3284 return win;
3285}
3e4093b6
RS
3286\f
3287/* Mark EXP saying that we need to be able to take the
3288 address of it; it should not be allocated in a register.
3289 Returns true if successful. */
54c93c30 3290
3e4093b6
RS
3291bool
3292c_mark_addressable (tree exp)
400fbf9f 3293{
3e4093b6 3294 tree x = exp;
95602da1 3295
3e4093b6
RS
3296 while (1)
3297 switch (TREE_CODE (x))
3298 {
3299 case COMPONENT_REF:
3300 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3301 {
0039fa55
AN
3302 error
3303 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
3304 return false;
3305 }
95602da1 3306
3e4093b6 3307 /* ... fall through ... */
95602da1 3308
3e4093b6
RS
3309 case ADDR_EXPR:
3310 case ARRAY_REF:
3311 case REALPART_EXPR:
3312 case IMAGPART_EXPR:
3313 x = TREE_OPERAND (x, 0);
3314 break;
95602da1 3315
3e4093b6
RS
3316 case COMPOUND_LITERAL_EXPR:
3317 case CONSTRUCTOR:
3318 TREE_ADDRESSABLE (x) = 1;
3319 return true;
95602da1 3320
3e4093b6
RS
3321 case VAR_DECL:
3322 case CONST_DECL:
3323 case PARM_DECL:
3324 case RESULT_DECL:
5baeaac0 3325 if (C_DECL_REGISTER (x)
3e4093b6
RS
3326 && DECL_NONLOCAL (x))
3327 {
e697b20f 3328 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 3329 {
0039fa55
AN
3330 error
3331 ("global register variable %qD used in nested function", x);
3e4093b6
RS
3332 return false;
3333 }
0039fa55 3334 pedwarn ("register variable %qD used in nested function", x);
3e4093b6 3335 }
5baeaac0 3336 else if (C_DECL_REGISTER (x))
3e4093b6 3337 {
e697b20f 3338 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
3339 error ("address of global register variable %qD requested", x);
3340 else
3341 error ("address of register variable %qD requested", x);
3342 return false;
3e4093b6 3343 }
400fbf9f 3344
3e4093b6
RS
3345 /* drops in */
3346 case FUNCTION_DECL:
3347 TREE_ADDRESSABLE (x) = 1;
3348 /* drops out */
3349 default:
3350 return true;
3351 }
3352}
3353\f
3354/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
400fbf9f
JW
3355
3356tree
3e4093b6 3357build_conditional_expr (tree ifexp, tree op1, tree op2)
400fbf9f 3358{
3e4093b6
RS
3359 tree type1;
3360 tree type2;
3361 enum tree_code code1;
3362 enum tree_code code2;
3363 tree result_type = NULL;
3364 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f 3365
3e4093b6
RS
3366 /* Promote both alternatives. */
3367
3368 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3369 op1 = default_conversion (op1);
3370 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3371 op2 = default_conversion (op2);
3372
3373 if (TREE_CODE (ifexp) == ERROR_MARK
3374 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3375 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 3376 return error_mark_node;
400fbf9f 3377
3e4093b6
RS
3378 type1 = TREE_TYPE (op1);
3379 code1 = TREE_CODE (type1);
3380 type2 = TREE_TYPE (op2);
3381 code2 = TREE_CODE (type2);
3382
b1adf557
JM
3383 /* C90 does not permit non-lvalue arrays in conditional expressions.
3384 In C99 they will be pointers by now. */
3385 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3386 {
3387 error ("non-lvalue array in conditional expression");
3388 return error_mark_node;
3389 }
3390
3e4093b6
RS
3391 /* Quickly detect the usual case where op1 and op2 have the same type
3392 after promotion. */
3393 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 3394 {
3e4093b6
RS
3395 if (type1 == type2)
3396 result_type = type1;
3397 else
3398 result_type = TYPE_MAIN_VARIANT (type1);
3399 }
3400 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
3401 || code1 == COMPLEX_TYPE)
3402 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3403 || code2 == COMPLEX_TYPE))
3e4093b6 3404 {
ccf7f880 3405 result_type = c_common_type (type1, type2);
400fbf9f 3406
3e4093b6
RS
3407 /* If -Wsign-compare, warn here if type1 and type2 have
3408 different signedness. We'll promote the signed to unsigned
3409 and later code won't know it used to be different.
3410 Do this check on the original types, so that explicit casts
3411 will be considered, but default promotions won't. */
3412 if (warn_sign_compare && !skip_evaluation)
ab87f8c8 3413 {
8df83eae
RK
3414 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3415 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 3416
3e4093b6
RS
3417 if (unsigned_op1 ^ unsigned_op2)
3418 {
6ac01510
ILT
3419 bool ovf;
3420
3e4093b6
RS
3421 /* Do not warn if the result type is signed, since the
3422 signed type will only be chosen if it can represent
3423 all the values of the unsigned type. */
3f75a254 3424 if (!TYPE_UNSIGNED (result_type))
3e4093b6
RS
3425 /* OK */;
3426 /* Do not warn if the signed quantity is an unsuffixed
3427 integer literal (or some static constant expression
3428 involving such literals) and it is non-negative. */
6ac01510
ILT
3429 else if ((unsigned_op2
3430 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3431 || (unsigned_op1
3432 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3e4093b6
RS
3433 /* OK */;
3434 else
d4ee4d25 3435 warning (0, "signed and unsigned type in conditional expression");
3e4093b6
RS
3436 }
3437 }
3438 }
3439 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3440 {
3441 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3442 pedwarn ("ISO C forbids conditional expr with only one void side");
3443 result_type = void_type_node;
3444 }
3445 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3446 {
58393038 3447 if (comp_target_types (type1, type2))
10bc1b1b 3448 result_type = common_pointer_type (type1, type2);
6aa3c60d 3449 else if (null_pointer_constant_p (orig_op1))
3e4093b6 3450 result_type = qualify_type (type2, type1);
6aa3c60d 3451 else if (null_pointer_constant_p (orig_op2))
3e4093b6
RS
3452 result_type = qualify_type (type1, type2);
3453 else if (VOID_TYPE_P (TREE_TYPE (type1)))
34a80643 3454 {
3e4093b6 3455 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
bda67431
JM
3456 pedwarn ("ISO C forbids conditional expr between "
3457 "%<void *%> and function pointer");
3e4093b6
RS
3458 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3459 TREE_TYPE (type2)));
34a80643 3460 }
3e4093b6 3461 else if (VOID_TYPE_P (TREE_TYPE (type2)))
1c2a9b35 3462 {
3e4093b6 3463 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
bda67431
JM
3464 pedwarn ("ISO C forbids conditional expr between "
3465 "%<void *%> and function pointer");
3e4093b6
RS
3466 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3467 TREE_TYPE (type1)));
1c2a9b35 3468 }
34a80643 3469 else
ab87f8c8 3470 {
3e4093b6
RS
3471 pedwarn ("pointer type mismatch in conditional expression");
3472 result_type = build_pointer_type (void_type_node);
ab87f8c8 3473 }
3e4093b6
RS
3474 }
3475 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3476 {
6aa3c60d 3477 if (!null_pointer_constant_p (orig_op2))
3e4093b6
RS
3478 pedwarn ("pointer/integer type mismatch in conditional expression");
3479 else
ab87f8c8 3480 {
3e4093b6 3481 op2 = null_pointer_node;
ab87f8c8 3482 }
3e4093b6
RS
3483 result_type = type1;
3484 }
3485 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3486 {
6aa3c60d 3487 if (!null_pointer_constant_p (orig_op1))
3e4093b6
RS
3488 pedwarn ("pointer/integer type mismatch in conditional expression");
3489 else
ab87f8c8 3490 {
3e4093b6 3491 op1 = null_pointer_node;
ab87f8c8 3492 }
3e4093b6
RS
3493 result_type = type2;
3494 }
1c2a9b35 3495
3e4093b6
RS
3496 if (!result_type)
3497 {
3498 if (flag_cond_mismatch)
3499 result_type = void_type_node;
3500 else
400fbf9f 3501 {
3e4093b6 3502 error ("type mismatch in conditional expression");
ab87f8c8 3503 return error_mark_node;
400fbf9f 3504 }
3e4093b6 3505 }
400fbf9f 3506
3e4093b6
RS
3507 /* Merge const and volatile flags of the incoming types. */
3508 result_type
3509 = build_type_variant (result_type,
3510 TREE_READONLY (op1) || TREE_READONLY (op2),
3511 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
b6a10c9f 3512
3e4093b6
RS
3513 if (result_type != TREE_TYPE (op1))
3514 op1 = convert_and_check (result_type, op1);
3515 if (result_type != TREE_TYPE (op2))
3516 op2 = convert_and_check (result_type, op2);
b6a10c9f 3517
8c900457 3518 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3e4093b6
RS
3519}
3520\f
487a92fe
JM
3521/* Return a compound expression that performs two expressions and
3522 returns the value of the second of them. */
400fbf9f 3523
3e4093b6 3524tree
487a92fe 3525build_compound_expr (tree expr1, tree expr2)
3e4093b6 3526{
3f75a254 3527 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
3528 {
3529 /* The left-hand operand of a comma expression is like an expression
c22cacf3 3530 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3e4093b6 3531 any side-effects, unless it was explicitly cast to (void). */
e14a6540 3532 if (warn_unused_value)
47aecf47 3533 {
e14a6540 3534 if (VOID_TYPE_P (TREE_TYPE (expr1))
4c7a6c1b
JM
3535 && (TREE_CODE (expr1) == NOP_EXPR
3536 || TREE_CODE (expr1) == CONVERT_EXPR))
47aecf47 3537 ; /* (void) a, b */
e14a6540
JM
3538 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3539 && TREE_CODE (expr1) == COMPOUND_EXPR
4c7a6c1b
JM
3540 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3541 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
47aecf47
JM
3542 ; /* (void) a, (void) b, c */
3543 else
253a697e
DM
3544 warning (OPT_Wunused_value,
3545 "left-hand operand of comma expression has no effect");
47aecf47 3546 }
3e4093b6 3547 }
400fbf9f 3548
3e4093b6
RS
3549 /* With -Wunused, we should also warn if the left-hand operand does have
3550 side-effects, but computes a value which is not used. For example, in
3551 `foo() + bar(), baz()' the result of the `+' operator is not used,
3552 so we should issue a warning. */
3553 else if (warn_unused_value)
487a92fe 3554 warn_if_unused_value (expr1, input_location);
400fbf9f 3555
e63d6886
AP
3556 if (expr2 == error_mark_node)
3557 return error_mark_node;
3558
53fb4de3 3559 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3e4093b6 3560}
400fbf9f 3561
3e4093b6 3562/* Build an expression representing a cast to type TYPE of expression EXPR. */
400fbf9f 3563
3e4093b6
RS
3564tree
3565build_c_cast (tree type, tree expr)
3566{
3567 tree value = expr;
400fbf9f 3568
3e4093b6
RS
3569 if (type == error_mark_node || expr == error_mark_node)
3570 return error_mark_node;
400fbf9f 3571
3e4093b6
RS
3572 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3573 only in <protocol> qualifications. But when constructing cast expressions,
3574 the protocols do matter and must be kept around. */
700686fa
ZL
3575 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3576 return build1 (NOP_EXPR, type, expr);
3577
3578 type = TYPE_MAIN_VARIANT (type);
400fbf9f 3579
3e4093b6
RS
3580 if (TREE_CODE (type) == ARRAY_TYPE)
3581 {
3582 error ("cast specifies array type");
3583 return error_mark_node;
3584 }
400fbf9f 3585
3e4093b6
RS
3586 if (TREE_CODE (type) == FUNCTION_TYPE)
3587 {
3588 error ("cast specifies function type");
3589 return error_mark_node;
3590 }
400fbf9f 3591
3e4093b6
RS
3592 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3593 {
3594 if (pedantic)
400fbf9f 3595 {
3e4093b6
RS
3596 if (TREE_CODE (type) == RECORD_TYPE
3597 || TREE_CODE (type) == UNION_TYPE)
3598 pedwarn ("ISO C forbids casting nonscalar to the same type");
400fbf9f 3599 }
3e4093b6
RS
3600 }
3601 else if (TREE_CODE (type) == UNION_TYPE)
3602 {
3603 tree field;
400fbf9f 3604
3e4093b6
RS
3605 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3606 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
132da1a5 3607 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
3608 break;
3609
3610 if (field)
400fbf9f 3611 {
3e4093b6
RS
3612 tree t;
3613
3614 if (pedantic)
3615 pedwarn ("ISO C forbids casts to union type");
3616 t = digest_init (type,
4038c495 3617 build_constructor_single (type, field, value),
916c5919 3618 true, 0);
3e4093b6 3619 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6de9cd9a 3620 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3e4093b6 3621 return t;
400fbf9f 3622 }
3e4093b6
RS
3623 error ("cast to union type from type not present in union");
3624 return error_mark_node;
3625 }
3626 else
3627 {
3628 tree otype, ovalue;
400fbf9f 3629
3e4093b6
RS
3630 if (type == void_type_node)
3631 return build1 (CONVERT_EXPR, type, value);
400fbf9f 3632
3e4093b6 3633 otype = TREE_TYPE (value);
400fbf9f 3634
3e4093b6 3635 /* Optionally warn about potentially worrisome casts. */
770ae6cc 3636
3e4093b6
RS
3637 if (warn_cast_qual
3638 && TREE_CODE (type) == POINTER_TYPE
3639 && TREE_CODE (otype) == POINTER_TYPE)
3640 {
3641 tree in_type = type;
3642 tree in_otype = otype;
3643 int added = 0;
3644 int discarded = 0;
400fbf9f 3645
3e4093b6
RS
3646 /* Check that the qualifiers on IN_TYPE are a superset of
3647 the qualifiers of IN_OTYPE. The outermost level of
3648 POINTER_TYPE nodes is uninteresting and we stop as soon
3649 as we hit a non-POINTER_TYPE node on either type. */
3650 do
3651 {
3652 in_otype = TREE_TYPE (in_otype);
3653 in_type = TREE_TYPE (in_type);
400fbf9f 3654
3e4093b6
RS
3655 /* GNU C allows cv-qualified function types. 'const'
3656 means the function is very pure, 'volatile' means it
3657 can't return. We need to warn when such qualifiers
3658 are added, not when they're taken away. */
3659 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3660 && TREE_CODE (in_type) == FUNCTION_TYPE)
3661 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3662 else
3663 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3664 }
3665 while (TREE_CODE (in_type) == POINTER_TYPE
3666 && TREE_CODE (in_otype) == POINTER_TYPE);
400fbf9f 3667
3e4093b6 3668 if (added)
ea7c218e 3669 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
400fbf9f 3670
3e4093b6
RS
3671 if (discarded)
3672 /* There are qualifiers present in IN_OTYPE that are not
3673 present in IN_TYPE. */
ea7c218e 3674 warning (OPT_Wcast_qual, "cast discards qualifiers from pointer target type");
3e4093b6 3675 }
400fbf9f 3676
3e4093b6 3677 /* Warn about possible alignment problems. */
3176a0c2 3678 if (STRICT_ALIGNMENT
3e4093b6
RS
3679 && TREE_CODE (type) == POINTER_TYPE
3680 && TREE_CODE (otype) == POINTER_TYPE
3681 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3682 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3683 /* Don't warn about opaque types, where the actual alignment
3684 restriction is unknown. */
3685 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3686 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3687 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3688 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3176a0c2
DD
3689 warning (OPT_Wcast_align,
3690 "cast increases required alignment of target type");
e9a25f70 3691
3176a0c2 3692 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 3693 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 3694 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
3695 /* Unlike conversion of integers to pointers, where the
3696 warning is disabled for converting constants because
3697 of cases such as SIG_*, warn about converting constant
3698 pointers to integers. In some cases it may cause unwanted
8d1c7aef 3699 sign extension, and a warning is appropriate. */
3176a0c2
DD
3700 warning (OPT_Wpointer_to_int_cast,
3701 "cast from pointer to integer of different size");
400fbf9f 3702
3176a0c2 3703 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 3704 && TREE_CODE (type) != TREE_CODE (otype))
3176a0c2
DD
3705 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3706 "to non-matching type %qT", otype, type);
400fbf9f 3707
3176a0c2 3708 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
3709 && TREE_CODE (otype) == INTEGER_TYPE
3710 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3711 /* Don't warn about converting any constant. */
3712 && !TREE_CONSTANT (value))
3176a0c2
DD
3713 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3714 "of different size");
400fbf9f 3715
79bedddc
SR
3716 if (warn_strict_aliasing <= 2)
3717 strict_aliasing_warning (otype, type, expr);
400fbf9f 3718
3897f229
JM
3719 /* If pedantic, warn for conversions between function and object
3720 pointer types, except for converting a null pointer constant
3721 to function pointer type. */
3722 if (pedantic
3723 && TREE_CODE (type) == POINTER_TYPE
3724 && TREE_CODE (otype) == POINTER_TYPE
3725 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3726 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3727 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3728
3729 if (pedantic
3730 && TREE_CODE (type) == POINTER_TYPE
3731 && TREE_CODE (otype) == POINTER_TYPE
3732 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3733 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 3734 && !null_pointer_constant_p (value))
3897f229
JM
3735 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3736
3e4093b6 3737 ovalue = value;
3e4093b6 3738 value = convert (type, value);
400fbf9f 3739
3e4093b6
RS
3740 /* Ignore any integer overflow caused by the cast. */
3741 if (TREE_CODE (value) == INTEGER_CST)
3742 {
8bcd6380 3743 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 3744 {
8bcd6380
RS
3745 if (!TREE_OVERFLOW (value))
3746 {
3747 /* Avoid clobbering a shared constant. */
3748 value = copy_node (value);
3749 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3750 }
6414bad6 3751 }
8bcd6380 3752 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
3753 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3754 value = build_int_cst_wide (TREE_TYPE (value),
3755 TREE_INT_CST_LOW (value),
3756 TREE_INT_CST_HIGH (value));
3e4093b6
RS
3757 }
3758 }
400fbf9f 3759
53cd18ec
JM
3760 /* Don't let a cast be an lvalue. */
3761 if (value == expr)
3e4093b6 3762 value = non_lvalue (value);
e9a25f70 3763
3e4093b6 3764 return value;
400fbf9f
JW
3765}
3766
3e4093b6
RS
3767/* Interpret a cast of expression EXPR to type TYPE. */
3768tree
f8893e47 3769c_cast_expr (struct c_type_name *type_name, tree expr)
400fbf9f 3770{
f8893e47 3771 tree type;
3e4093b6 3772 int saved_wsp = warn_strict_prototypes;
c5c76735 3773
3e4093b6
RS
3774 /* This avoids warnings about unprototyped casts on
3775 integers. E.g. "#define SIG_DFL (void(*)())0". */
3776 if (TREE_CODE (expr) == INTEGER_CST)
3777 warn_strict_prototypes = 0;
f8893e47 3778 type = groktypename (type_name);
3e4093b6 3779 warn_strict_prototypes = saved_wsp;
c5c76735 3780
3e4093b6 3781 return build_c_cast (type, expr);
400fbf9f 3782}
3e4093b6
RS
3783\f
3784/* Build an assignment expression of lvalue LHS from value RHS.
3785 MODIFYCODE is the code for a binary operator that we use
3786 to combine the old value of LHS with RHS to get the new value.
3787 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
2f6e4e97 3788
3e4093b6
RS
3789tree
3790build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
400fbf9f 3791{
3e4093b6
RS
3792 tree result;
3793 tree newrhs;
3794 tree lhstype = TREE_TYPE (lhs);
3795 tree olhstype = lhstype;
e9a25f70 3796
3e4093b6
RS
3797 /* Types that aren't fully specified cannot be used in assignments. */
3798 lhs = require_complete_type (lhs);
e9a25f70 3799
3e4093b6
RS
3800 /* Avoid duplicate error messages from operands that had errors. */
3801 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3802 return error_mark_node;
400fbf9f 3803
c0bcacec
VR
3804 if (!lvalue_or_else (lhs, lv_assign))
3805 return error_mark_node;
3806
ed248cf7 3807 STRIP_TYPE_NOPS (rhs);
e9a25f70 3808
3e4093b6 3809 newrhs = rhs;
400fbf9f 3810
3e4093b6
RS
3811 /* If a binary op has been requested, combine the old LHS value with the RHS
3812 producing the value we should actually store into the LHS. */
3813
3814 if (modifycode != NOP_EXPR)
400fbf9f 3815 {
3e4093b6
RS
3816 lhs = stabilize_reference (lhs);
3817 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
400fbf9f 3818 }
400fbf9f 3819
9bf24266 3820 /* Give an error for storing in something that is 'const'. */
bbbd6700 3821
3e4093b6
RS
3822 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3823 || ((TREE_CODE (lhstype) == RECORD_TYPE
3824 || TREE_CODE (lhstype) == UNION_TYPE)
3825 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289
DN
3826 {
3827 readonly_error (lhs, lv_assign);
3828 return error_mark_node;
3829 }
bbbd6700 3830
3e4093b6
RS
3831 /* If storing into a structure or union member,
3832 it has probably been given type `int'.
3833 Compute the type that would go with
3834 the actual amount of storage the member occupies. */
bbbd6700 3835
3e4093b6
RS
3836 if (TREE_CODE (lhs) == COMPONENT_REF
3837 && (TREE_CODE (lhstype) == INTEGER_TYPE
3838 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3839 || TREE_CODE (lhstype) == REAL_TYPE
3840 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3841 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 3842
3e4093b6
RS
3843 /* If storing in a field that is in actuality a short or narrower than one,
3844 we must store in the field in its actual type. */
3845
3846 if (lhstype != TREE_TYPE (lhs))
3847 {
3848 lhs = copy_node (lhs);
3849 TREE_TYPE (lhs) = lhstype;
400fbf9f 3850 }
400fbf9f 3851
3e4093b6 3852 /* Convert new value to destination type. */
400fbf9f 3853
2ac2f164 3854 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3e4093b6
RS
3855 NULL_TREE, NULL_TREE, 0);
3856 if (TREE_CODE (newrhs) == ERROR_MARK)
3857 return error_mark_node;
400fbf9f 3858
6e955430
ZL
3859 /* Emit ObjC write barrier, if necessary. */
3860 if (c_dialect_objc () && flag_objc_gc)
3861 {
3862 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3863 if (result)
3864 return result;
3865 }
3866
ea4b7848 3867 /* Scan operands. */
400fbf9f 3868
53fb4de3 3869 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3e4093b6 3870 TREE_SIDE_EFFECTS (result) = 1;
400fbf9f 3871
3e4093b6
RS
3872 /* If we got the LHS in a different type for storing in,
3873 convert the result back to the nominal type of LHS
3874 so that the value we return always has the same type
3875 as the LHS argument. */
e855c5ce 3876
3e4093b6
RS
3877 if (olhstype == TREE_TYPE (result))
3878 return result;
2ac2f164 3879 return convert_for_assignment (olhstype, result, ic_assign,
3e4093b6
RS
3880 NULL_TREE, NULL_TREE, 0);
3881}
3882\f
3883/* Convert value RHS to type TYPE as preparation for an assignment
3884 to an lvalue of type TYPE.
3885 The real work of conversion is done by `convert'.
3886 The purpose of this function is to generate error messages
3887 for assignments that are not allowed in C.
2ac2f164
JM
3888 ERRTYPE says whether it is argument passing, assignment,
3889 initialization or return.
2f6e4e97 3890
2ac2f164 3891 FUNCTION is a tree for the function being called.
3e4093b6 3892 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 3893
3e4093b6 3894static tree
2ac2f164
JM
3895convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3896 tree fundecl, tree function, int parmnum)
3e4093b6
RS
3897{
3898 enum tree_code codel = TREE_CODE (type);
3899 tree rhstype;
3900 enum tree_code coder;
2ac2f164 3901 tree rname = NULL_TREE;
58393038 3902 bool objc_ok = false;
2ac2f164 3903
6dcc04b0 3904 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
2ac2f164
JM
3905 {
3906 tree selector;
3907 /* Change pointer to function to the function itself for
3908 diagnostics. */
3909 if (TREE_CODE (function) == ADDR_EXPR
3910 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3911 function = TREE_OPERAND (function, 0);
3912
3913 /* Handle an ObjC selector specially for diagnostics. */
3914 selector = objc_message_selector ();
3915 rname = function;
3916 if (selector && parmnum > 2)
3917 {
3918 rname = selector;
3919 parmnum -= 2;
3920 }
3921 }
3922
3923 /* This macro is used to emit diagnostics to ensure that all format
3924 strings are complete sentences, visible to gettext and checked at
3925 compile time. */
3926#define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3927 do { \
3928 switch (errtype) \
3929 { \
3930 case ic_argpass: \
3931 pedwarn (AR, parmnum, rname); \
3932 break; \
6dcc04b0 3933 case ic_argpass_nonproto: \
d4ee4d25 3934 warning (0, AR, parmnum, rname); \
6dcc04b0 3935 break; \
2ac2f164
JM
3936 case ic_assign: \
3937 pedwarn (AS); \
3938 break; \
3939 case ic_init: \
3940 pedwarn (IN); \
3941 break; \
3942 case ic_return: \
3943 pedwarn (RE); \
3944 break; \
3945 default: \
3946 gcc_unreachable (); \
3947 } \
3948 } while (0)
cb3ca04e 3949
ed248cf7 3950 STRIP_TYPE_NOPS (rhs);
3e4093b6 3951
46bdb9cf
JM
3952 if (optimize && TREE_CODE (rhs) == VAR_DECL
3953 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3e4093b6
RS
3954 rhs = decl_constant_value_for_broken_optimization (rhs);
3955
3956 rhstype = TREE_TYPE (rhs);
3957 coder = TREE_CODE (rhstype);
3958
3959 if (coder == ERROR_MARK)
3960 return error_mark_node;
3961
58393038
ZL
3962 if (c_dialect_objc ())
3963 {
3964 int parmno;
3965
3966 switch (errtype)
3967 {
3968 case ic_return:
3969 parmno = 0;
3970 break;
3971
3972 case ic_assign:
3973 parmno = -1;
3974 break;
3975
3976 case ic_init:
3977 parmno = -2;
3978 break;
3979
3980 default:
3981 parmno = parmnum;
3982 break;
3983 }
3984
3985 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3986 }
3987
3e4093b6 3988 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 3989 return rhs;
3e4093b6
RS
3990
3991 if (coder == VOID_TYPE)
400fbf9f 3992 {
6dcc04b0
JM
3993 /* Except for passing an argument to an unprototyped function,
3994 this is a constraint violation. When passing an argument to
3995 an unprototyped function, it is compile-time undefined;
3996 making it a constraint in that case was rejected in
3997 DR#252. */
3e4093b6
RS
3998 error ("void value not ignored as it ought to be");
3999 return error_mark_node;
400fbf9f 4000 }
3e4093b6
RS
4001 /* A type converts to a reference to it.
4002 This code doesn't fully support references, it's just for the
4003 special case of va_start and va_copy. */
4004 if (codel == REFERENCE_TYPE
132da1a5 4005 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
400fbf9f 4006 {
3e4093b6 4007 if (!lvalue_p (rhs))
400fbf9f 4008 {
3e4093b6
RS
4009 error ("cannot pass rvalue to reference parameter");
4010 return error_mark_node;
400fbf9f 4011 }
3e4093b6
RS
4012 if (!c_mark_addressable (rhs))
4013 return error_mark_node;
4014 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4015
4016 /* We already know that these two types are compatible, but they
4017 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4018 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4019 likely to be va_list, a typedef to __builtin_va_list, which
4020 is different enough that it will cause problems later. */
4021 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4022 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4023
4024 rhs = build1 (NOP_EXPR, type, rhs);
4025 return rhs;
400fbf9f 4026 }
3e4093b6 4027 /* Some types can interconvert without explicit casts. */
3274deff 4028 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 4029 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
4030 return convert (type, rhs);
4031 /* Arithmetic types all interconvert, and enum is treated like int. */
4032 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 4033 || codel == FIXED_POINT_TYPE
3e4093b6
RS
4034 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4035 || codel == BOOLEAN_TYPE)
4036 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 4037 || coder == FIXED_POINT_TYPE
3e4093b6
RS
4038 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4039 || coder == BOOLEAN_TYPE))
4040 return convert_and_check (type, rhs);
400fbf9f 4041
79077aea
JJ
4042 /* Aggregates in different TUs might need conversion. */
4043 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4044 && codel == coder
4045 && comptypes (type, rhstype))
4046 return convert_and_check (type, rhs);
4047
3e4093b6
RS
4048 /* Conversion to a transparent union from its member types.
4049 This applies only to function arguments. */
79077aea
JJ
4050 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4051 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
400fbf9f 4052 {
0257e383 4053 tree memb, marginal_memb = NULL_TREE;
3e4093b6 4054
0257e383 4055 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
400fbf9f 4056 {
0257e383 4057 tree memb_type = TREE_TYPE (memb);
400fbf9f 4058
3e4093b6 4059 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 4060 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 4061 break;
e58cd767 4062
3e4093b6
RS
4063 if (TREE_CODE (memb_type) != POINTER_TYPE)
4064 continue;
2f6e4e97 4065
3e4093b6
RS
4066 if (coder == POINTER_TYPE)
4067 {
4068 tree ttl = TREE_TYPE (memb_type);
4069 tree ttr = TREE_TYPE (rhstype);
400fbf9f 4070
3e4093b6
RS
4071 /* Any non-function converts to a [const][volatile] void *
4072 and vice versa; otherwise, targets must be the same.
4073 Meanwhile, the lhs target must have all the qualifiers of
4074 the rhs. */
4075 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
58393038 4076 || comp_target_types (memb_type, rhstype))
3e4093b6
RS
4077 {
4078 /* If this type won't generate any warnings, use it. */
4079 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4080 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4081 && TREE_CODE (ttl) == FUNCTION_TYPE)
4082 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4083 == TYPE_QUALS (ttr))
4084 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4085 == TYPE_QUALS (ttl))))
4086 break;
400fbf9f 4087
3e4093b6 4088 /* Keep looking for a better type, but remember this one. */
0257e383
RH
4089 if (!marginal_memb)
4090 marginal_memb = memb;
3e4093b6
RS
4091 }
4092 }
82bde854 4093
3e4093b6 4094 /* Can convert integer zero to any pointer type. */
6aa3c60d 4095 if (null_pointer_constant_p (rhs))
3e4093b6
RS
4096 {
4097 rhs = null_pointer_node;
4098 break;
4099 }
4100 }
400fbf9f 4101
0257e383 4102 if (memb || marginal_memb)
3e4093b6 4103 {
0257e383 4104 if (!memb)
3e4093b6
RS
4105 {
4106 /* We have only a marginally acceptable member type;
4107 it needs a warning. */
0257e383 4108 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 4109 tree ttr = TREE_TYPE (rhstype);
714a0864 4110
3e4093b6
RS
4111 /* Const and volatile mean something different for function
4112 types, so the usual warnings are not appropriate. */
4113 if (TREE_CODE (ttr) == FUNCTION_TYPE
4114 && TREE_CODE (ttl) == FUNCTION_TYPE)
4115 {
4116 /* Because const and volatile on functions are
4117 restrictions that say the function will not do
4118 certain things, it is okay to use a const or volatile
4119 function where an ordinary one is wanted, but not
4120 vice-versa. */
4121 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4b794eaf 4122 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
2ac2f164
JM
4123 "makes qualified function "
4124 "pointer from unqualified"),
4b794eaf 4125 G_("assignment makes qualified "
2ac2f164
JM
4126 "function pointer from "
4127 "unqualified"),
4b794eaf 4128 G_("initialization makes qualified "
2ac2f164
JM
4129 "function pointer from "
4130 "unqualified"),
4b794eaf 4131 G_("return makes qualified function "
2ac2f164 4132 "pointer from unqualified"));
3e4093b6
RS
4133 }
4134 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4b794eaf 4135 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
2ac2f164 4136 "qualifiers from pointer target type"),
4b794eaf 4137 G_("assignment discards qualifiers "
2ac2f164 4138 "from pointer target type"),
4b794eaf 4139 G_("initialization discards qualifiers "
2ac2f164 4140 "from pointer target type"),
4b794eaf 4141 G_("return discards qualifiers from "
2ac2f164 4142 "pointer target type"));
0257e383
RH
4143
4144 memb = marginal_memb;
3e4093b6 4145 }
400fbf9f 4146
13b22d3a 4147 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3e4093b6 4148 pedwarn ("ISO C prohibits argument conversion to union type");
0e7c47fa 4149
ff7637ef 4150 rhs = fold_convert (TREE_TYPE (memb), rhs);
0257e383 4151 return build_constructor_single (type, memb, rhs);
3e4093b6 4152 }
0e7c47fa
RK
4153 }
4154
3e4093b6
RS
4155 /* Conversions among pointers */
4156 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4157 && (coder == codel))
400fbf9f 4158 {
3e4093b6
RS
4159 tree ttl = TREE_TYPE (type);
4160 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
4161 tree mvl = ttl;
4162 tree mvr = ttr;
3e4093b6 4163 bool is_opaque_pointer;
264fa2db 4164 int target_cmp = 0; /* Cache comp_target_types () result. */
400fbf9f 4165
46df2823
JM
4166 if (TREE_CODE (mvl) != ARRAY_TYPE)
4167 mvl = TYPE_MAIN_VARIANT (mvl);
4168 if (TREE_CODE (mvr) != ARRAY_TYPE)
4169 mvr = TYPE_MAIN_VARIANT (mvr);
3e4093b6 4170 /* Opaque pointers are treated like void pointers. */
5fd9b178 4171 is_opaque_pointer = (targetm.vector_opaque_p (type)
c22cacf3
MS
4172 || targetm.vector_opaque_p (rhstype))
4173 && TREE_CODE (ttl) == VECTOR_TYPE
4174 && TREE_CODE (ttr) == VECTOR_TYPE;
4175
b7e20b53 4176 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
4177 for the purpose of reducing the number of false positives, we
4178 tolerate the special case of
b7e20b53 4179
c22cacf3 4180 int *p = NULL;
b7e20b53 4181
c22cacf3 4182 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 4183 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c22cacf3
MS
4184 warning (OPT_Wc___compat, "request for implicit conversion from "
4185 "%qT to %qT not permitted in C++", rhstype, type);
400fbf9f 4186
7876a414
KG
4187 /* Check if the right-hand side has a format attribute but the
4188 left-hand side doesn't. */
104f8784
KG
4189 if (warn_missing_format_attribute
4190 && check_missing_format_attribute (type, rhstype))
c22cacf3 4191 {
104f8784
KG
4192 switch (errtype)
4193 {
4194 case ic_argpass:
4195 case ic_argpass_nonproto:
4196 warning (OPT_Wmissing_format_attribute,
4197 "argument %d of %qE might be "
4198 "a candidate for a format attribute",
4199 parmnum, rname);
4200 break;
4201 case ic_assign:
4202 warning (OPT_Wmissing_format_attribute,
4203 "assignment left-hand side might be "
4204 "a candidate for a format attribute");
4205 break;
4206 case ic_init:
4207 warning (OPT_Wmissing_format_attribute,
4208 "initialization left-hand side might be "
4209 "a candidate for a format attribute");
4210 break;
4211 case ic_return:
4212 warning (OPT_Wmissing_format_attribute,
4213 "return type might be "
4214 "a candidate for a format attribute");
4215 break;
4216 default:
4217 gcc_unreachable ();
4218 }
7876a414 4219 }
c22cacf3 4220
3e4093b6
RS
4221 /* Any non-function converts to a [const][volatile] void *
4222 and vice versa; otherwise, targets must be the same.
4223 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4224 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
58393038 4225 || (target_cmp = comp_target_types (type, rhstype))
3e4093b6 4226 || is_opaque_pointer
12753674
RE
4227 || (c_common_unsigned_type (mvl)
4228 == c_common_unsigned_type (mvr)))
3e4093b6
RS
4229 {
4230 if (pedantic
4231 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4232 ||
4233 (VOID_TYPE_P (ttr)
6aa3c60d 4234 && !null_pointer_constant_p (rhs)
3e4093b6 4235 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4b794eaf 4236 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
2ac2f164
JM
4237 "%qE between function pointer "
4238 "and %<void *%>"),
4b794eaf 4239 G_("ISO C forbids assignment between "
2ac2f164 4240 "function pointer and %<void *%>"),
4b794eaf 4241 G_("ISO C forbids initialization between "
2ac2f164 4242 "function pointer and %<void *%>"),
4b794eaf 4243 G_("ISO C forbids return between function "
2ac2f164 4244 "pointer and %<void *%>"));
3e4093b6
RS
4245 /* Const and volatile mean something different for function types,
4246 so the usual warnings are not appropriate. */
4247 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4248 && TREE_CODE (ttl) != FUNCTION_TYPE)
4249 {
4250 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
58393038
ZL
4251 {
4252 /* Types differing only by the presence of the 'volatile'
4253 qualifier are acceptable if the 'volatile' has been added
4254 in by the Objective-C EH machinery. */
4255 if (!objc_type_quals_match (ttl, ttr))
4b794eaf 4256 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
58393038 4257 "qualifiers from pointer target type"),
4b794eaf 4258 G_("assignment discards qualifiers "
58393038 4259 "from pointer target type"),
4b794eaf 4260 G_("initialization discards qualifiers "
58393038 4261 "from pointer target type"),
4b794eaf 4262 G_("return discards qualifiers from "
58393038
ZL
4263 "pointer target type"));
4264 }
3e4093b6
RS
4265 /* If this is not a case of ignoring a mismatch in signedness,
4266 no warning. */
4267 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 4268 || target_cmp)
3e4093b6
RS
4269 ;
4270 /* If there is a mismatch, do warn. */
f2fd3821 4271 else if (warn_pointer_sign)
4b794eaf 4272 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
2ac2f164 4273 "%d of %qE differ in signedness"),
4b794eaf 4274 G_("pointer targets in assignment "
2ac2f164 4275 "differ in signedness"),
4b794eaf 4276 G_("pointer targets in initialization "
2ac2f164 4277 "differ in signedness"),
4b794eaf 4278 G_("pointer targets in return differ "
2ac2f164 4279 "in signedness"));
3e4093b6
RS
4280 }
4281 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4282 && TREE_CODE (ttr) == FUNCTION_TYPE)
4283 {
4284 /* Because const and volatile on functions are restrictions
4285 that say the function will not do certain things,
4286 it is okay to use a const or volatile function
4287 where an ordinary one is wanted, but not vice-versa. */
4288 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4b794eaf 4289 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
2ac2f164
JM
4290 "qualified function pointer "
4291 "from unqualified"),
4b794eaf 4292 G_("assignment makes qualified function "
2ac2f164 4293 "pointer from unqualified"),
4b794eaf 4294 G_("initialization makes qualified "
2ac2f164 4295 "function pointer from unqualified"),
4b794eaf 4296 G_("return makes qualified function "
2ac2f164 4297 "pointer from unqualified"));
3e4093b6
RS
4298 }
4299 }
4300 else
58393038
ZL
4301 /* Avoid warning about the volatile ObjC EH puts on decls. */
4302 if (!objc_ok)
4b794eaf 4303 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
58393038 4304 "incompatible pointer type"),
4b794eaf
JJ
4305 G_("assignment from incompatible pointer type"),
4306 G_("initialization from incompatible "
58393038 4307 "pointer type"),
4b794eaf 4308 G_("return from incompatible pointer type"));
58393038 4309
3e4093b6
RS
4310 return convert (type, rhs);
4311 }
b494fd98
EB
4312 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4313 {
6dcc04b0
JM
4314 /* ??? This should not be an error when inlining calls to
4315 unprototyped functions. */
b494fd98
EB
4316 error ("invalid use of non-lvalue array");
4317 return error_mark_node;
4318 }
3e4093b6 4319 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 4320 {
3e4093b6
RS
4321 /* An explicit constant 0 can convert to a pointer,
4322 or one that results from arithmetic, even including
4323 a cast to integer type. */
6aa3c60d 4324 if (!null_pointer_constant_p (rhs))
4b794eaf 4325 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
2ac2f164 4326 "pointer from integer without a cast"),
4b794eaf 4327 G_("assignment makes pointer from integer "
2ac2f164 4328 "without a cast"),
4b794eaf 4329 G_("initialization makes pointer from "
2ac2f164 4330 "integer without a cast"),
4b794eaf 4331 G_("return makes pointer from integer "
2ac2f164 4332 "without a cast"));
b3006337
EB
4333
4334 return convert (type, rhs);
400fbf9f 4335 }
3e4093b6 4336 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 4337 {
4b794eaf 4338 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
2ac2f164 4339 "from pointer without a cast"),
4b794eaf 4340 G_("assignment makes integer from pointer "
2ac2f164 4341 "without a cast"),
4b794eaf 4342 G_("initialization makes integer from pointer "
2ac2f164 4343 "without a cast"),
4b794eaf 4344 G_("return makes integer from pointer "
2ac2f164 4345 "without a cast"));
3e4093b6
RS
4346 return convert (type, rhs);
4347 }
4348 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4349 return convert (type, rhs);
400fbf9f 4350
2ac2f164 4351 switch (errtype)
3e4093b6 4352 {
2ac2f164 4353 case ic_argpass:
6dcc04b0
JM
4354 case ic_argpass_nonproto:
4355 /* ??? This should not be an error when inlining calls to
4356 unprototyped functions. */
2ac2f164
JM
4357 error ("incompatible type for argument %d of %qE", parmnum, rname);
4358 break;
4359 case ic_assign:
4360 error ("incompatible types in assignment");
4361 break;
4362 case ic_init:
4363 error ("incompatible types in initialization");
4364 break;
4365 case ic_return:
4366 error ("incompatible types in return");
4367 break;
4368 default:
4369 gcc_unreachable ();
400fbf9f 4370 }
53b01f59 4371
3e4093b6
RS
4372 return error_mark_node;
4373}
3e4093b6
RS
4374\f
4375/* If VALUE is a compound expr all of whose expressions are constant, then
4376 return its value. Otherwise, return error_mark_node.
15b732b2 4377
3e4093b6
RS
4378 This is for handling COMPOUND_EXPRs as initializer elements
4379 which is allowed with a warning when -pedantic is specified. */
15b732b2 4380
3e4093b6
RS
4381static tree
4382valid_compound_expr_initializer (tree value, tree endtype)
4383{
4384 if (TREE_CODE (value) == COMPOUND_EXPR)
4385 {
4386 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4387 == error_mark_node)
4388 return error_mark_node;
4389 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4390 endtype);
4391 }
116df786 4392 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
4393 return error_mark_node;
4394 else
4395 return value;
15b732b2 4396}
400fbf9f 4397\f
3e4093b6
RS
4398/* Perform appropriate conversions on the initial value of a variable,
4399 store it in the declaration DECL,
4400 and print any error messages that are appropriate.
4401 If the init is invalid, store an ERROR_MARK. */
400fbf9f 4402
3e4093b6
RS
4403void
4404store_init_value (tree decl, tree init)
400fbf9f 4405{
3e4093b6 4406 tree value, type;
400fbf9f 4407
3e4093b6 4408 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 4409
3e4093b6
RS
4410 type = TREE_TYPE (decl);
4411 if (TREE_CODE (type) == ERROR_MARK)
4412 return;
400fbf9f 4413
3e4093b6 4414 /* Digest the specified initializer into an expression. */
400fbf9f 4415
916c5919 4416 value = digest_init (type, init, true, TREE_STATIC (decl));
400fbf9f 4417
3e4093b6 4418 /* Store the expression if valid; else report error. */
400fbf9f 4419
3176a0c2 4420 if (!in_system_header
3f75a254 4421 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
4422 warning (OPT_Wtraditional, "traditional C rejects automatic "
4423 "aggregate initialization");
2f6e4e97 4424
3e4093b6 4425 DECL_INITIAL (decl) = value;
400fbf9f 4426
3e4093b6
RS
4427 /* ANSI wants warnings about out-of-range constant initializers. */
4428 STRIP_TYPE_NOPS (value);
c2658540
MLI
4429 if (TREE_STATIC (decl))
4430 constant_expression_warning (value);
400fbf9f 4431
3e4093b6
RS
4432 /* Check if we need to set array size from compound literal size. */
4433 if (TREE_CODE (type) == ARRAY_TYPE
4434 && TYPE_DOMAIN (type) == 0
4435 && value != error_mark_node)
400fbf9f 4436 {
3e4093b6
RS
4437 tree inside_init = init;
4438
ed248cf7 4439 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
4440 inside_init = fold (inside_init);
4441
4442 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4443 {
8d9f82d5 4444 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 4445
8d9f82d5 4446 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
4447 {
4448 /* For int foo[] = (int [3]){1}; we need to set array size
4449 now since later on array initializer will be just the
4450 brace enclosed list of the compound literal. */
8d9f82d5
AO
4451 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4452 TREE_TYPE (decl) = type;
4453 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 4454 layout_type (type);
8d9f82d5 4455 layout_decl (cldecl, 0);
3e4093b6
RS
4456 }
4457 }
400fbf9f 4458 }
3e4093b6
RS
4459}
4460\f
4461/* Methods for storing and printing names for error messages. */
400fbf9f 4462
3e4093b6
RS
4463/* Implement a spelling stack that allows components of a name to be pushed
4464 and popped. Each element on the stack is this structure. */
400fbf9f 4465
3e4093b6
RS
4466struct spelling
4467{
4468 int kind;
4469 union
400fbf9f 4470 {
a0f0ab9f 4471 unsigned HOST_WIDE_INT i;
3e4093b6
RS
4472 const char *s;
4473 } u;
4474};
2f6e4e97 4475
3e4093b6
RS
4476#define SPELLING_STRING 1
4477#define SPELLING_MEMBER 2
4478#define SPELLING_BOUNDS 3
400fbf9f 4479
3e4093b6
RS
4480static struct spelling *spelling; /* Next stack element (unused). */
4481static struct spelling *spelling_base; /* Spelling stack base. */
4482static int spelling_size; /* Size of the spelling stack. */
400fbf9f 4483
3e4093b6
RS
4484/* Macros to save and restore the spelling stack around push_... functions.
4485 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 4486
3e4093b6
RS
4487#define SPELLING_DEPTH() (spelling - spelling_base)
4488#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 4489
3e4093b6
RS
4490/* Push an element on the spelling stack with type KIND and assign VALUE
4491 to MEMBER. */
400fbf9f 4492
3e4093b6
RS
4493#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4494{ \
4495 int depth = SPELLING_DEPTH (); \
4496 \
4497 if (depth >= spelling_size) \
4498 { \
4499 spelling_size += 10; \
cca8ead2
BI
4500 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4501 spelling_size); \
3e4093b6
RS
4502 RESTORE_SPELLING_DEPTH (depth); \
4503 } \
4504 \
4505 spelling->kind = (KIND); \
4506 spelling->MEMBER = (VALUE); \
4507 spelling++; \
4508}
400fbf9f 4509
3e4093b6 4510/* Push STRING on the stack. Printed literally. */
400fbf9f 4511
3e4093b6
RS
4512static void
4513push_string (const char *string)
4514{
4515 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4516}
400fbf9f 4517
3e4093b6 4518/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 4519
3e4093b6
RS
4520static void
4521push_member_name (tree decl)
4522{
4523 const char *const string
4524 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4525 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4526}
400fbf9f 4527
3e4093b6 4528/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 4529
3e4093b6 4530static void
a0f0ab9f 4531push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
4532{
4533 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4534}
bb58bec5 4535
3e4093b6 4536/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 4537
3e4093b6
RS
4538static int
4539spelling_length (void)
4540{
4541 int size = 0;
4542 struct spelling *p;
400fbf9f 4543
3e4093b6
RS
4544 for (p = spelling_base; p < spelling; p++)
4545 {
4546 if (p->kind == SPELLING_BOUNDS)
4547 size += 25;
4548 else
4549 size += strlen (p->u.s) + 1;
4550 }
4551
4552 return size;
400fbf9f 4553}
400fbf9f 4554
3e4093b6 4555/* Print the spelling to BUFFER and return it. */
400fbf9f 4556
3e4093b6
RS
4557static char *
4558print_spelling (char *buffer)
400fbf9f 4559{
3e4093b6
RS
4560 char *d = buffer;
4561 struct spelling *p;
400fbf9f 4562
3e4093b6
RS
4563 for (p = spelling_base; p < spelling; p++)
4564 if (p->kind == SPELLING_BOUNDS)
4565 {
a0f0ab9f 4566 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
4567 d += strlen (d);
4568 }
4569 else
4570 {
4571 const char *s;
4572 if (p->kind == SPELLING_MEMBER)
4573 *d++ = '.';
4574 for (s = p->u.s; (*d = *s++); d++)
4575 ;
4576 }
4577 *d++ = '\0';
4578 return buffer;
4579}
400fbf9f 4580
3e4093b6
RS
4581/* Issue an error message for a bad initializer component.
4582 MSGID identifies the message.
4583 The component name is taken from the spelling stack. */
400fbf9f 4584
3e4093b6
RS
4585void
4586error_init (const char *msgid)
4587{
4588 char *ofwhat;
400fbf9f 4589
3e4093b6 4590 error ("%s", _(msgid));
28dab132 4591 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4592 if (*ofwhat)
bda67431 4593 error ("(near initialization for %qs)", ofwhat);
3e4093b6 4594}
400fbf9f 4595
3e4093b6
RS
4596/* Issue a pedantic warning for a bad initializer component.
4597 MSGID identifies the message.
4598 The component name is taken from the spelling stack. */
400fbf9f 4599
3e4093b6
RS
4600void
4601pedwarn_init (const char *msgid)
4602{
4603 char *ofwhat;
9f720c3e 4604
3e4093b6 4605 pedwarn ("%s", _(msgid));
28dab132 4606 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4607 if (*ofwhat)
bda67431 4608 pedwarn ("(near initialization for %qs)", ofwhat);
3e4093b6 4609}
9f720c3e 4610
3e4093b6
RS
4611/* Issue a warning for a bad initializer component.
4612 MSGID identifies the message.
4613 The component name is taken from the spelling stack. */
61179109 4614
3e4093b6
RS
4615static void
4616warning_init (const char *msgid)
4617{
4618 char *ofwhat;
7e842ef8 4619
d4ee4d25 4620 warning (0, "%s", _(msgid));
28dab132 4621 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4622 if (*ofwhat)
d4ee4d25 4623 warning (0, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
4624}
4625\f
916c5919
JM
4626/* If TYPE is an array type and EXPR is a parenthesized string
4627 constant, warn if pedantic that EXPR is being used to initialize an
4628 object of type TYPE. */
4629
4630void
4631maybe_warn_string_init (tree type, struct c_expr expr)
4632{
4633 if (pedantic
4634 && TREE_CODE (type) == ARRAY_TYPE
4635 && TREE_CODE (expr.value) == STRING_CST
4636 && expr.original_code != STRING_CST)
4637 pedwarn_init ("array initialized from parenthesized string constant");
4638}
4639
3e4093b6
RS
4640/* Digest the parser output INIT as an initializer for type TYPE.
4641 Return a C expression of type TYPE to represent the initial value.
7e842ef8 4642
916c5919
JM
4643 If INIT is a string constant, STRICT_STRING is true if it is
4644 unparenthesized or we should not warn here for it being parenthesized.
4645 For other types of INIT, STRICT_STRING is not used.
4646
3e4093b6
RS
4647 REQUIRE_CONSTANT requests an error if non-constant initializers or
4648 elements are seen. */
7e842ef8 4649
3e4093b6 4650static tree
916c5919 4651digest_init (tree type, tree init, bool strict_string, int require_constant)
3e4093b6
RS
4652{
4653 enum tree_code code = TREE_CODE (type);
4654 tree inside_init = init;
7e842ef8 4655
3e4093b6 4656 if (type == error_mark_node
f01da1a5 4657 || !init
3e4093b6
RS
4658 || init == error_mark_node
4659 || TREE_TYPE (init) == error_mark_node)
4660 return error_mark_node;
7e842ef8 4661
ed248cf7 4662 STRIP_TYPE_NOPS (inside_init);
7e842ef8 4663
3e4093b6 4664 inside_init = fold (inside_init);
7e842ef8 4665
3e4093b6
RS
4666 /* Initialization of an array of chars from a string constant
4667 optionally enclosed in braces. */
7e842ef8 4668
197463ae
JM
4669 if (code == ARRAY_TYPE && inside_init
4670 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6
RS
4671 {
4672 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
197463ae
JM
4673 /* Note that an array could be both an array of character type
4674 and an array of wchar_t if wchar_t is signed char or unsigned
4675 char. */
4676 bool char_array = (typ1 == char_type_node
4677 || typ1 == signed_char_type_node
4678 || typ1 == unsigned_char_type_node);
4679 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4680 if (char_array || wchar_array)
7e842ef8 4681 {
916c5919 4682 struct c_expr expr;
197463ae 4683 bool char_string;
916c5919
JM
4684 expr.value = inside_init;
4685 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4686 maybe_warn_string_init (type, expr);
4687
197463ae
JM
4688 char_string
4689 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4690 == char_type_node);
4691
3e4093b6 4692 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 4693 TYPE_MAIN_VARIANT (type)))
3e4093b6 4694 return inside_init;
7e842ef8 4695
197463ae 4696 if (!wchar_array && !char_string)
3e4093b6
RS
4697 {
4698 error_init ("char-array initialized from wide string");
4699 return error_mark_node;
4700 }
197463ae 4701 if (char_string && !char_array)
3e4093b6 4702 {
197463ae 4703 error_init ("wchar_t-array initialized from non-wide string");
3e4093b6 4704 return error_mark_node;
7e842ef8 4705 }
2f6e4e97 4706
3e4093b6
RS
4707 TREE_TYPE (inside_init) = type;
4708 if (TYPE_DOMAIN (type) != 0
4709 && TYPE_SIZE (type) != 0
4710 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4711 /* Subtract 1 (or sizeof (wchar_t))
4712 because it's ok to ignore the terminating null char
4713 that is counted in the length of the constant. */
4714 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4715 TREE_STRING_LENGTH (inside_init)
4716 - ((TYPE_PRECISION (typ1)
4717 != TYPE_PRECISION (char_type_node))
4718 ? (TYPE_PRECISION (wchar_type_node)
4719 / BITS_PER_UNIT)
4720 : 1)))
4721 pedwarn_init ("initializer-string for array of chars is too long");
7e842ef8 4722
3e4093b6 4723 return inside_init;
7e842ef8 4724 }
197463ae
JM
4725 else if (INTEGRAL_TYPE_P (typ1))
4726 {
4727 error_init ("array of inappropriate type initialized "
4728 "from string constant");
4729 return error_mark_node;
4730 }
7e842ef8
PE
4731 }
4732
3e4093b6
RS
4733 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4734 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4735 below and handle as a constructor. */
e89be13b
JJ
4736 if (code == VECTOR_TYPE
4737 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 4738 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
4739 && TREE_CONSTANT (inside_init))
4740 {
4741 if (TREE_CODE (inside_init) == VECTOR_CST
4742 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4743 TYPE_MAIN_VARIANT (type)))
4744 return inside_init;
4745
4746 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4747 {
4038c495
GB
4748 unsigned HOST_WIDE_INT ix;
4749 tree value;
4750 bool constant_p = true;
e89be13b
JJ
4751
4752 /* Iterate through elements and check if all constructor
4753 elements are *_CSTs. */
4038c495
GB
4754 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4755 if (!CONSTANT_CLASS_P (value))
4756 {
4757 constant_p = false;
4758 break;
4759 }
e89be13b 4760
4038c495
GB
4761 if (constant_p)
4762 return build_vector_from_ctor (type,
4763 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
4764 }
4765 }
6035d635 4766
3e4093b6
RS
4767 /* Any type can be initialized
4768 from an expression of the same type, optionally with braces. */
400fbf9f 4769
3e4093b6
RS
4770 if (inside_init && TREE_TYPE (inside_init) != 0
4771 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 4772 TYPE_MAIN_VARIANT (type))
3e4093b6 4773 || (code == ARRAY_TYPE
132da1a5 4774 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 4775 || (code == VECTOR_TYPE
132da1a5 4776 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 4777 || (code == POINTER_TYPE
3897f229 4778 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 4779 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 4780 TREE_TYPE (type)))))
3e4093b6
RS
4781 {
4782 if (code == POINTER_TYPE)
b494fd98 4783 {
b494fd98
EB
4784 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4785 {
f2a71bbc
JM
4786 if (TREE_CODE (inside_init) == STRING_CST
4787 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4788 inside_init = array_to_pointer_conversion (inside_init);
4789 else
4790 {
4791 error_init ("invalid use of non-lvalue array");
4792 return error_mark_node;
4793 }
b494fd98 4794 }
f2a71bbc 4795 }
b494fd98 4796
bae39a73
NS
4797 if (code == VECTOR_TYPE)
4798 /* Although the types are compatible, we may require a
4799 conversion. */
4800 inside_init = convert (type, inside_init);
3e4093b6 4801
ca58211b
PB
4802 if (require_constant
4803 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 4804 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 4805 {
3e4093b6
RS
4806 /* As an extension, allow initializing objects with static storage
4807 duration with compound literals (which are then treated just as
ca58211b
PB
4808 the brace enclosed list they contain). Also allow this for
4809 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
4810 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4811 inside_init = DECL_INITIAL (decl);
400fbf9f 4812 }
3e4093b6
RS
4813
4814 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4815 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 4816 {
3e4093b6
RS
4817 error_init ("array initialized from non-constant array expression");
4818 return error_mark_node;
400fbf9f 4819 }
400fbf9f 4820
3e4093b6
RS
4821 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4822 inside_init = decl_constant_value_for_broken_optimization (inside_init);
2f6e4e97 4823
3e4093b6
RS
4824 /* Compound expressions can only occur here if -pedantic or
4825 -pedantic-errors is specified. In the later case, we always want
4826 an error. In the former case, we simply want a warning. */
4827 if (require_constant && pedantic
4828 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4829 {
4830 inside_init
4831 = valid_compound_expr_initializer (inside_init,
4832 TREE_TYPE (inside_init));
4833 if (inside_init == error_mark_node)
4834 error_init ("initializer element is not constant");
2f6e4e97 4835 else
3e4093b6
RS
4836 pedwarn_init ("initializer element is not constant");
4837 if (flag_pedantic_errors)
4838 inside_init = error_mark_node;
4839 }
4840 else if (require_constant
116df786
RH
4841 && !initializer_constant_valid_p (inside_init,
4842 TREE_TYPE (inside_init)))
3e4093b6
RS
4843 {
4844 error_init ("initializer element is not constant");
4845 inside_init = error_mark_node;
8b40563c 4846 }
f735a153 4847
8fcef540
KG
4848 /* Added to enable additional -Wmissing-format-attribute warnings. */
4849 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4850 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4851 NULL_TREE, 0);
3e4093b6
RS
4852 return inside_init;
4853 }
f735a153 4854
3e4093b6 4855 /* Handle scalar types, including conversions. */
400fbf9f 4856
ab22c1fa
CF
4857 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
4858 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
4859 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 4860 {
f2a71bbc
JM
4861 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4862 && (TREE_CODE (init) == STRING_CST
4863 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4864 init = array_to_pointer_conversion (init);
3e4093b6 4865 inside_init
2ac2f164 4866 = convert_for_assignment (type, init, ic_init,
3e4093b6 4867 NULL_TREE, NULL_TREE, 0);
2f6e4e97 4868
3274deff
JW
4869 /* Check to see if we have already given an error message. */
4870 if (inside_init == error_mark_node)
4871 ;
3f75a254 4872 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 4873 {
3e4093b6
RS
4874 error_init ("initializer element is not constant");
4875 inside_init = error_mark_node;
400fbf9f 4876 }
3e4093b6 4877 else if (require_constant
116df786
RH
4878 && !initializer_constant_valid_p (inside_init,
4879 TREE_TYPE (inside_init)))
400fbf9f 4880 {
3e4093b6
RS
4881 error_init ("initializer element is not computable at load time");
4882 inside_init = error_mark_node;
400fbf9f 4883 }
3e4093b6
RS
4884
4885 return inside_init;
400fbf9f 4886 }
d9fc6069 4887
3e4093b6 4888 /* Come here only for records and arrays. */
d9fc6069 4889
3e4093b6 4890 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 4891 {
3e4093b6
RS
4892 error_init ("variable-sized object may not be initialized");
4893 return error_mark_node;
d9fc6069 4894 }
3e4093b6
RS
4895
4896 error_init ("invalid initializer");
4897 return error_mark_node;
d9fc6069 4898}
400fbf9f 4899\f
3e4093b6 4900/* Handle initializers that use braces. */
400fbf9f 4901
3e4093b6
RS
4902/* Type of object we are accumulating a constructor for.
4903 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4904static tree constructor_type;
400fbf9f 4905
3e4093b6
RS
4906/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4907 left to fill. */
4908static tree constructor_fields;
400fbf9f 4909
3e4093b6
RS
4910/* For an ARRAY_TYPE, this is the specified index
4911 at which to store the next element we get. */
4912static tree constructor_index;
400fbf9f 4913
3e4093b6
RS
4914/* For an ARRAY_TYPE, this is the maximum index. */
4915static tree constructor_max_index;
400fbf9f 4916
3e4093b6
RS
4917/* For a RECORD_TYPE, this is the first field not yet written out. */
4918static tree constructor_unfilled_fields;
400fbf9f 4919
3e4093b6
RS
4920/* For an ARRAY_TYPE, this is the index of the first element
4921 not yet written out. */
4922static tree constructor_unfilled_index;
895ea614 4923
3e4093b6
RS
4924/* In a RECORD_TYPE, the byte index of the next consecutive field.
4925 This is so we can generate gaps between fields, when appropriate. */
4926static tree constructor_bit_index;
10d5caec 4927
3e4093b6
RS
4928/* If we are saving up the elements rather than allocating them,
4929 this is the list of elements so far (in reverse order,
4930 most recent first). */
4038c495 4931static VEC(constructor_elt,gc) *constructor_elements;
ad47f1e5 4932
3e4093b6
RS
4933/* 1 if constructor should be incrementally stored into a constructor chain,
4934 0 if all the elements should be kept in AVL tree. */
4935static int constructor_incremental;
ad47f1e5 4936
3e4093b6
RS
4937/* 1 if so far this constructor's elements are all compile-time constants. */
4938static int constructor_constant;
ad47f1e5 4939
3e4093b6
RS
4940/* 1 if so far this constructor's elements are all valid address constants. */
4941static int constructor_simple;
ad47f1e5 4942
3e4093b6
RS
4943/* 1 if this constructor is erroneous so far. */
4944static int constructor_erroneous;
d45cf215 4945
3e4093b6
RS
4946/* Structure for managing pending initializer elements, organized as an
4947 AVL tree. */
d45cf215 4948
3e4093b6 4949struct init_node
d45cf215 4950{
3e4093b6
RS
4951 struct init_node *left, *right;
4952 struct init_node *parent;
4953 int balance;
4954 tree purpose;
4955 tree value;
d45cf215
RS
4956};
4957
3e4093b6
RS
4958/* Tree of pending elements at this constructor level.
4959 These are elements encountered out of order
4960 which belong at places we haven't reached yet in actually
4961 writing the output.
4962 Will never hold tree nodes across GC runs. */
4963static struct init_node *constructor_pending_elts;
d45cf215 4964
3e4093b6
RS
4965/* The SPELLING_DEPTH of this constructor. */
4966static int constructor_depth;
d45cf215 4967
3e4093b6
RS
4968/* DECL node for which an initializer is being read.
4969 0 means we are reading a constructor expression
4970 such as (struct foo) {...}. */
4971static tree constructor_decl;
d45cf215 4972
3e4093b6
RS
4973/* Nonzero if this is an initializer for a top-level decl. */
4974static int constructor_top_level;
d45cf215 4975
3e4093b6
RS
4976/* Nonzero if there were any member designators in this initializer. */
4977static int constructor_designated;
d45cf215 4978
3e4093b6
RS
4979/* Nesting depth of designator list. */
4980static int designator_depth;
d45cf215 4981
3e4093b6 4982/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 4983static int designator_erroneous;
d45cf215 4984
3e4093b6
RS
4985\f
4986/* This stack has a level for each implicit or explicit level of
4987 structuring in the initializer, including the outermost one. It
4988 saves the values of most of the variables above. */
d45cf215 4989
3e4093b6
RS
4990struct constructor_range_stack;
4991
4992struct constructor_stack
d45cf215 4993{
3e4093b6
RS
4994 struct constructor_stack *next;
4995 tree type;
4996 tree fields;
4997 tree index;
4998 tree max_index;
4999 tree unfilled_index;
5000 tree unfilled_fields;
5001 tree bit_index;
4038c495 5002 VEC(constructor_elt,gc) *elements;
3e4093b6
RS
5003 struct init_node *pending_elts;
5004 int offset;
5005 int depth;
916c5919 5006 /* If value nonzero, this value should replace the entire
3e4093b6 5007 constructor at this level. */
916c5919 5008 struct c_expr replacement_value;
3e4093b6
RS
5009 struct constructor_range_stack *range_stack;
5010 char constant;
5011 char simple;
5012 char implicit;
5013 char erroneous;
5014 char outer;
5015 char incremental;
5016 char designated;
5017};
d45cf215 5018
802415d1 5019static struct constructor_stack *constructor_stack;
d45cf215 5020
3e4093b6
RS
5021/* This stack represents designators from some range designator up to
5022 the last designator in the list. */
d45cf215 5023
3e4093b6
RS
5024struct constructor_range_stack
5025{
5026 struct constructor_range_stack *next, *prev;
5027 struct constructor_stack *stack;
5028 tree range_start;
5029 tree index;
5030 tree range_end;
5031 tree fields;
5032};
d45cf215 5033
802415d1 5034static struct constructor_range_stack *constructor_range_stack;
d45cf215 5035
3e4093b6
RS
5036/* This stack records separate initializers that are nested.
5037 Nested initializers can't happen in ANSI C, but GNU C allows them
5038 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 5039
3e4093b6 5040struct initializer_stack
d45cf215 5041{
3e4093b6
RS
5042 struct initializer_stack *next;
5043 tree decl;
3e4093b6
RS
5044 struct constructor_stack *constructor_stack;
5045 struct constructor_range_stack *constructor_range_stack;
4038c495 5046 VEC(constructor_elt,gc) *elements;
3e4093b6
RS
5047 struct spelling *spelling;
5048 struct spelling *spelling_base;
5049 int spelling_size;
5050 char top_level;
5051 char require_constant_value;
5052 char require_constant_elements;
5053};
d45cf215 5054
802415d1 5055static struct initializer_stack *initializer_stack;
3e4093b6
RS
5056\f
5057/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
5058
5059void
a396f8ae 5060start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 5061{
3e4093b6 5062 const char *locus;
cceb1885 5063 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 5064
3e4093b6 5065 p->decl = constructor_decl;
3e4093b6
RS
5066 p->require_constant_value = require_constant_value;
5067 p->require_constant_elements = require_constant_elements;
5068 p->constructor_stack = constructor_stack;
5069 p->constructor_range_stack = constructor_range_stack;
5070 p->elements = constructor_elements;
5071 p->spelling = spelling;
5072 p->spelling_base = spelling_base;
5073 p->spelling_size = spelling_size;
5074 p->top_level = constructor_top_level;
5075 p->next = initializer_stack;
5076 initializer_stack = p;
400fbf9f 5077
3e4093b6 5078 constructor_decl = decl;
3e4093b6
RS
5079 constructor_designated = 0;
5080 constructor_top_level = top_level;
400fbf9f 5081
6f17bbcf 5082 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
5083 {
5084 require_constant_value = TREE_STATIC (decl);
5085 require_constant_elements
5086 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5087 /* For a scalar, you can always use any value to initialize,
5088 even within braces. */
5089 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5090 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5091 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5092 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5093 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5094 }
5095 else
5096 {
5097 require_constant_value = 0;
5098 require_constant_elements = 0;
5099 locus = "(anonymous)";
5100 }
b71c7f8a 5101
3e4093b6
RS
5102 constructor_stack = 0;
5103 constructor_range_stack = 0;
b71c7f8a 5104
3e4093b6
RS
5105 missing_braces_mentioned = 0;
5106
5107 spelling_base = 0;
5108 spelling_size = 0;
5109 RESTORE_SPELLING_DEPTH (0);
5110
5111 if (locus)
5112 push_string (locus);
5113}
5114
5115void
5116finish_init (void)
b71c7f8a 5117{
3e4093b6 5118 struct initializer_stack *p = initializer_stack;
b71c7f8a 5119
3e4093b6
RS
5120 /* Free the whole constructor stack of this initializer. */
5121 while (constructor_stack)
5122 {
5123 struct constructor_stack *q = constructor_stack;
5124 constructor_stack = q->next;
5125 free (q);
5126 }
5127
366de0ce 5128 gcc_assert (!constructor_range_stack);
3e4093b6
RS
5129
5130 /* Pop back to the data of the outer initializer (if any). */
36579663 5131 free (spelling_base);
3aeb3655 5132
3e4093b6 5133 constructor_decl = p->decl;
3e4093b6
RS
5134 require_constant_value = p->require_constant_value;
5135 require_constant_elements = p->require_constant_elements;
5136 constructor_stack = p->constructor_stack;
5137 constructor_range_stack = p->constructor_range_stack;
5138 constructor_elements = p->elements;
5139 spelling = p->spelling;
5140 spelling_base = p->spelling_base;
5141 spelling_size = p->spelling_size;
5142 constructor_top_level = p->top_level;
5143 initializer_stack = p->next;
5144 free (p);
b71c7f8a 5145}
400fbf9f 5146\f
3e4093b6
RS
5147/* Call here when we see the initializer is surrounded by braces.
5148 This is instead of a call to push_init_level;
5149 it is matched by a call to pop_init_level.
400fbf9f 5150
3e4093b6
RS
5151 TYPE is the type to initialize, for a constructor expression.
5152 For an initializer for a decl, TYPE is zero. */
400fbf9f 5153
3e4093b6
RS
5154void
5155really_start_incremental_init (tree type)
400fbf9f 5156{
5d038c4c 5157 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 5158
3e4093b6
RS
5159 if (type == 0)
5160 type = TREE_TYPE (constructor_decl);
400fbf9f 5161
5fd9b178 5162 if (targetm.vector_opaque_p (type))
3e4093b6 5163 error ("opaque vector types cannot be initialized");
400fbf9f 5164
3e4093b6
RS
5165 p->type = constructor_type;
5166 p->fields = constructor_fields;
5167 p->index = constructor_index;
5168 p->max_index = constructor_max_index;
5169 p->unfilled_index = constructor_unfilled_index;
5170 p->unfilled_fields = constructor_unfilled_fields;
5171 p->bit_index = constructor_bit_index;
5172 p->elements = constructor_elements;
5173 p->constant = constructor_constant;
5174 p->simple = constructor_simple;
5175 p->erroneous = constructor_erroneous;
5176 p->pending_elts = constructor_pending_elts;
5177 p->depth = constructor_depth;
916c5919
JM
5178 p->replacement_value.value = 0;
5179 p->replacement_value.original_code = ERROR_MARK;
3e4093b6
RS
5180 p->implicit = 0;
5181 p->range_stack = 0;
5182 p->outer = 0;
5183 p->incremental = constructor_incremental;
5184 p->designated = constructor_designated;
5185 p->next = 0;
5186 constructor_stack = p;
b13aca19 5187
3e4093b6
RS
5188 constructor_constant = 1;
5189 constructor_simple = 1;
5190 constructor_depth = SPELLING_DEPTH ();
5191 constructor_elements = 0;
5192 constructor_pending_elts = 0;
5193 constructor_type = type;
5194 constructor_incremental = 1;
5195 constructor_designated = 0;
5196 designator_depth = 0;
b06df647 5197 designator_erroneous = 0;
400fbf9f 5198
3e4093b6
RS
5199 if (TREE_CODE (constructor_type) == RECORD_TYPE
5200 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 5201 {
3e4093b6
RS
5202 constructor_fields = TYPE_FIELDS (constructor_type);
5203 /* Skip any nameless bit fields at the beginning. */
5204 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5205 && DECL_NAME (constructor_fields) == 0)
5206 constructor_fields = TREE_CHAIN (constructor_fields);
05bccae2 5207
3e4093b6
RS
5208 constructor_unfilled_fields = constructor_fields;
5209 constructor_bit_index = bitsize_zero_node;
400fbf9f 5210 }
3e4093b6
RS
5211 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5212 {
5213 if (TYPE_DOMAIN (constructor_type))
5214 {
5215 constructor_max_index
5216 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 5217
3e4093b6
RS
5218 /* Detect non-empty initializations of zero-length arrays. */
5219 if (constructor_max_index == NULL_TREE
5220 && TYPE_SIZE (constructor_type))
7d60be94 5221 constructor_max_index = build_int_cst (NULL_TREE, -1);
400fbf9f 5222
3e4093b6
RS
5223 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5224 to initialize VLAs will cause a proper error; avoid tree
5225 checking errors as well by setting a safe value. */
5226 if (constructor_max_index
5227 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7d60be94 5228 constructor_max_index = build_int_cst (NULL_TREE, -1);
59c83dbf 5229
3e4093b6
RS
5230 constructor_index
5231 = convert (bitsizetype,
5232 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 5233 }
3e4093b6 5234 else
493179da
JM
5235 {
5236 constructor_index = bitsize_zero_node;
5237 constructor_max_index = NULL_TREE;
5238 }
59c83dbf 5239
3e4093b6
RS
5240 constructor_unfilled_index = constructor_index;
5241 }
5242 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5243 {
5244 /* Vectors are like simple fixed-size arrays. */
5245 constructor_max_index =
7d60be94 5246 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 5247 constructor_index = bitsize_zero_node;
3e4093b6
RS
5248 constructor_unfilled_index = constructor_index;
5249 }
5250 else
5251 {
5252 /* Handle the case of int x = {5}; */
5253 constructor_fields = constructor_type;
5254 constructor_unfilled_fields = constructor_type;
5255 }
5256}
5257\f
5258/* Push down into a subobject, for initialization.
5259 If this is for an explicit set of braces, IMPLICIT is 0.
5260 If it is because the next element belongs at a lower level,
5261 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 5262
3e4093b6
RS
5263void
5264push_init_level (int implicit)
5265{
5266 struct constructor_stack *p;
5267 tree value = NULL_TREE;
400fbf9f 5268
3e4093b6 5269 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
5270 pop them now. If implicit == 1, this will have been done in
5271 process_init_element; do not repeat it here because in the case
5272 of excess initializers for an empty aggregate this leads to an
5273 infinite cycle of popping a level and immediately recreating
5274 it. */
5275 if (implicit != 1)
3e4093b6 5276 {
472d98b4
JM
5277 while (constructor_stack->implicit)
5278 {
5279 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5280 || TREE_CODE (constructor_type) == UNION_TYPE)
5281 && constructor_fields == 0)
5282 process_init_element (pop_init_level (1));
5283 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5284 && constructor_max_index
5285 && tree_int_cst_lt (constructor_max_index,
5286 constructor_index))
5287 process_init_element (pop_init_level (1));
5288 else
5289 break;
5290 }
3e4093b6 5291 }
400fbf9f 5292
3e4093b6
RS
5293 /* Unless this is an explicit brace, we need to preserve previous
5294 content if any. */
5295 if (implicit)
5296 {
5297 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5298 || TREE_CODE (constructor_type) == UNION_TYPE)
5299 && constructor_fields)
5300 value = find_init_member (constructor_fields);
5301 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5302 value = find_init_member (constructor_index);
400fbf9f
JW
5303 }
5304
5d038c4c 5305 p = XNEW (struct constructor_stack);
3e4093b6
RS
5306 p->type = constructor_type;
5307 p->fields = constructor_fields;
5308 p->index = constructor_index;
5309 p->max_index = constructor_max_index;
5310 p->unfilled_index = constructor_unfilled_index;
5311 p->unfilled_fields = constructor_unfilled_fields;
5312 p->bit_index = constructor_bit_index;
5313 p->elements = constructor_elements;
5314 p->constant = constructor_constant;
5315 p->simple = constructor_simple;
5316 p->erroneous = constructor_erroneous;
5317 p->pending_elts = constructor_pending_elts;
5318 p->depth = constructor_depth;
916c5919
JM
5319 p->replacement_value.value = 0;
5320 p->replacement_value.original_code = ERROR_MARK;
3e4093b6
RS
5321 p->implicit = implicit;
5322 p->outer = 0;
5323 p->incremental = constructor_incremental;
5324 p->designated = constructor_designated;
5325 p->next = constructor_stack;
5326 p->range_stack = 0;
5327 constructor_stack = p;
400fbf9f 5328
3e4093b6
RS
5329 constructor_constant = 1;
5330 constructor_simple = 1;
5331 constructor_depth = SPELLING_DEPTH ();
5332 constructor_elements = 0;
5333 constructor_incremental = 1;
5334 constructor_designated = 0;
5335 constructor_pending_elts = 0;
5336 if (!implicit)
400fbf9f 5337 {
3e4093b6
RS
5338 p->range_stack = constructor_range_stack;
5339 constructor_range_stack = 0;
5340 designator_depth = 0;
b06df647 5341 designator_erroneous = 0;
3e4093b6 5342 }
400fbf9f 5343
3e4093b6
RS
5344 /* Don't die if an entire brace-pair level is superfluous
5345 in the containing level. */
5346 if (constructor_type == 0)
5347 ;
5348 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5349 || TREE_CODE (constructor_type) == UNION_TYPE)
5350 {
5351 /* Don't die if there are extra init elts at the end. */
5352 if (constructor_fields == 0)
5353 constructor_type = 0;
5354 else
400fbf9f 5355 {
3e4093b6
RS
5356 constructor_type = TREE_TYPE (constructor_fields);
5357 push_member_name (constructor_fields);
5358 constructor_depth++;
400fbf9f 5359 }
3e4093b6
RS
5360 }
5361 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5362 {
5363 constructor_type = TREE_TYPE (constructor_type);
a0f0ab9f 5364 push_array_bounds (tree_low_cst (constructor_index, 1));
3e4093b6 5365 constructor_depth++;
400fbf9f
JW
5366 }
5367
3e4093b6 5368 if (constructor_type == 0)
400fbf9f 5369 {
3e4093b6
RS
5370 error_init ("extra brace group at end of initializer");
5371 constructor_fields = 0;
5372 constructor_unfilled_fields = 0;
5373 return;
400fbf9f
JW
5374 }
5375
3e4093b6
RS
5376 if (value && TREE_CODE (value) == CONSTRUCTOR)
5377 {
5378 constructor_constant = TREE_CONSTANT (value);
5379 constructor_simple = TREE_STATIC (value);
5380 constructor_elements = CONSTRUCTOR_ELTS (value);
4038c495 5381 if (!VEC_empty (constructor_elt, constructor_elements)
3e4093b6
RS
5382 && (TREE_CODE (constructor_type) == RECORD_TYPE
5383 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5384 set_nonincremental_init ();
5385 }
400fbf9f 5386
3e4093b6
RS
5387 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5388 {
5389 missing_braces_mentioned = 1;
5390 warning_init ("missing braces around initializer");
5391 }
400fbf9f 5392
3e4093b6
RS
5393 if (TREE_CODE (constructor_type) == RECORD_TYPE
5394 || TREE_CODE (constructor_type) == UNION_TYPE)
5395 {
5396 constructor_fields = TYPE_FIELDS (constructor_type);
5397 /* Skip any nameless bit fields at the beginning. */
5398 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5399 && DECL_NAME (constructor_fields) == 0)
5400 constructor_fields = TREE_CHAIN (constructor_fields);
103b7b17 5401
3e4093b6
RS
5402 constructor_unfilled_fields = constructor_fields;
5403 constructor_bit_index = bitsize_zero_node;
5404 }
5405 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5406 {
5407 /* Vectors are like simple fixed-size arrays. */
5408 constructor_max_index =
7d60be94 5409 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
3e4093b6
RS
5410 constructor_index = convert (bitsizetype, integer_zero_node);
5411 constructor_unfilled_index = constructor_index;
5412 }
5413 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5414 {
5415 if (TYPE_DOMAIN (constructor_type))
5416 {
5417 constructor_max_index
5418 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 5419
3e4093b6
RS
5420 /* Detect non-empty initializations of zero-length arrays. */
5421 if (constructor_max_index == NULL_TREE
5422 && TYPE_SIZE (constructor_type))
7d60be94 5423 constructor_max_index = build_int_cst (NULL_TREE, -1);
de520661 5424
3e4093b6
RS
5425 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5426 to initialize VLAs will cause a proper error; avoid tree
5427 checking errors as well by setting a safe value. */
5428 if (constructor_max_index
5429 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7d60be94 5430 constructor_max_index = build_int_cst (NULL_TREE, -1);
b62acd60 5431
3e4093b6
RS
5432 constructor_index
5433 = convert (bitsizetype,
5434 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5435 }
5436 else
5437 constructor_index = bitsize_zero_node;
de520661 5438
3e4093b6
RS
5439 constructor_unfilled_index = constructor_index;
5440 if (value && TREE_CODE (value) == STRING_CST)
5441 {
5442 /* We need to split the char/wchar array into individual
5443 characters, so that we don't have to special case it
5444 everywhere. */
5445 set_nonincremental_init_from_string (value);
5446 }
5447 }
5448 else
5449 {
b4519d39
SB
5450 if (constructor_type != error_mark_node)
5451 warning_init ("braces around scalar initializer");
3e4093b6
RS
5452 constructor_fields = constructor_type;
5453 constructor_unfilled_fields = constructor_type;
5454 }
5455}
8b6a5902 5456
3e4093b6 5457/* At the end of an implicit or explicit brace level,
916c5919
JM
5458 finish up that level of constructor. If a single expression
5459 with redundant braces initialized that level, return the
5460 c_expr structure for that expression. Otherwise, the original_code
5461 element is set to ERROR_MARK.
5462 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 5463 from inner levels (process_init_element ignores that),
916c5919 5464 but return error_mark_node as the value from the outermost level
3e4093b6 5465 (that's what we want to put in DECL_INITIAL).
916c5919 5466 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 5467
916c5919 5468struct c_expr
3e4093b6
RS
5469pop_init_level (int implicit)
5470{
5471 struct constructor_stack *p;
916c5919
JM
5472 struct c_expr ret;
5473 ret.value = 0;
5474 ret.original_code = ERROR_MARK;
de520661 5475
3e4093b6
RS
5476 if (implicit == 0)
5477 {
5478 /* When we come to an explicit close brace,
5479 pop any inner levels that didn't have explicit braces. */
5480 while (constructor_stack->implicit)
5481 process_init_element (pop_init_level (1));
de520661 5482
366de0ce 5483 gcc_assert (!constructor_range_stack);
3e4093b6 5484 }
e5e809f4 5485
0066ef9c
RH
5486 /* Now output all pending elements. */
5487 constructor_incremental = 1;
5488 output_pending_init_elements (1);
5489
3e4093b6 5490 p = constructor_stack;
e5e809f4 5491
3e4093b6
RS
5492 /* Error for initializing a flexible array member, or a zero-length
5493 array member in an inappropriate context. */
5494 if (constructor_type && constructor_fields
5495 && TREE_CODE (constructor_type) == ARRAY_TYPE
5496 && TYPE_DOMAIN (constructor_type)
3f75a254 5497 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
5498 {
5499 /* Silently discard empty initializations. The parser will
5500 already have pedwarned for empty brackets. */
5501 if (integer_zerop (constructor_unfilled_index))
5502 constructor_type = NULL_TREE;
366de0ce 5503 else
3e4093b6 5504 {
366de0ce 5505 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 5506
3e4093b6
RS
5507 if (constructor_depth > 2)
5508 error_init ("initialization of flexible array member in a nested context");
5509 else if (pedantic)
5510 pedwarn_init ("initialization of a flexible array member");
de520661 5511
3e4093b6
RS
5512 /* We have already issued an error message for the existence
5513 of a flexible array member not at the end of the structure.
535a42b1 5514 Discard the initializer so that we do not die later. */
3e4093b6
RS
5515 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5516 constructor_type = NULL_TREE;
5517 }
3e4093b6 5518 }
de520661 5519
3e4093b6 5520 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 5521 if (warn_missing_field_initializers
3e4093b6
RS
5522 && constructor_type
5523 && TREE_CODE (constructor_type) == RECORD_TYPE
5524 && constructor_unfilled_fields)
5525 {
5526 /* Do not warn for flexible array members or zero-length arrays. */
5527 while (constructor_unfilled_fields
3f75a254 5528 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6
RS
5529 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5530 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
cc77d4d5 5531
3e4093b6
RS
5532 /* Do not warn if this level of the initializer uses member
5533 designators; it is likely to be deliberate. */
5534 if (constructor_unfilled_fields && !constructor_designated)
5535 {
5536 push_member_name (constructor_unfilled_fields);
5537 warning_init ("missing initializer");
5538 RESTORE_SPELLING_DEPTH (constructor_depth);
5539 }
5540 }
de520661 5541
3e4093b6 5542 /* Pad out the end of the structure. */
916c5919 5543 if (p->replacement_value.value)
3e4093b6
RS
5544 /* If this closes a superfluous brace pair,
5545 just pass out the element between them. */
916c5919 5546 ret = p->replacement_value;
3e4093b6
RS
5547 else if (constructor_type == 0)
5548 ;
5549 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5550 && TREE_CODE (constructor_type) != UNION_TYPE
5551 && TREE_CODE (constructor_type) != ARRAY_TYPE
5552 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5553 {
5554 /* A nonincremental scalar initializer--just return
5555 the element, after verifying there is just one. */
4038c495 5556 if (VEC_empty (constructor_elt,constructor_elements))
3e4093b6
RS
5557 {
5558 if (!constructor_erroneous)
5559 error_init ("empty scalar initializer");
916c5919 5560 ret.value = error_mark_node;
3e4093b6 5561 }
4038c495 5562 else if (VEC_length (constructor_elt,constructor_elements) != 1)
3e4093b6
RS
5563 {
5564 error_init ("extra elements in scalar initializer");
4038c495 5565 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
3e4093b6
RS
5566 }
5567 else
4038c495 5568 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
3e4093b6
RS
5569 }
5570 else
5571 {
5572 if (constructor_erroneous)
916c5919 5573 ret.value = error_mark_node;
3e4093b6
RS
5574 else
5575 {
916c5919 5576 ret.value = build_constructor (constructor_type,
4038c495 5577 constructor_elements);
3e4093b6 5578 if (constructor_constant)
916c5919 5579 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
3e4093b6 5580 if (constructor_constant && constructor_simple)
916c5919 5581 TREE_STATIC (ret.value) = 1;
3e4093b6
RS
5582 }
5583 }
de520661 5584
3e4093b6
RS
5585 constructor_type = p->type;
5586 constructor_fields = p->fields;
5587 constructor_index = p->index;
5588 constructor_max_index = p->max_index;
5589 constructor_unfilled_index = p->unfilled_index;
5590 constructor_unfilled_fields = p->unfilled_fields;
5591 constructor_bit_index = p->bit_index;
5592 constructor_elements = p->elements;
5593 constructor_constant = p->constant;
5594 constructor_simple = p->simple;
5595 constructor_erroneous = p->erroneous;
5596 constructor_incremental = p->incremental;
5597 constructor_designated = p->designated;
5598 constructor_pending_elts = p->pending_elts;
5599 constructor_depth = p->depth;
5600 if (!p->implicit)
5601 constructor_range_stack = p->range_stack;
5602 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 5603
3e4093b6
RS
5604 constructor_stack = p->next;
5605 free (p);
b621a4dd 5606
5d5e98dc
VR
5607 if (ret.value == 0 && constructor_stack == 0)
5608 ret.value = error_mark_node;
916c5919 5609 return ret;
3e4093b6 5610}
8b6a5902 5611
3e4093b6
RS
5612/* Common handling for both array range and field name designators.
5613 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 5614
3e4093b6
RS
5615static int
5616set_designator (int array)
de520661 5617{
3e4093b6
RS
5618 tree subtype;
5619 enum tree_code subcode;
de520661 5620
3e4093b6
RS
5621 /* Don't die if an entire brace-pair level is superfluous
5622 in the containing level. */
5623 if (constructor_type == 0)
5624 return 1;
de520661 5625
366de0ce
NS
5626 /* If there were errors in this designator list already, bail out
5627 silently. */
b06df647 5628 if (designator_erroneous)
3e4093b6 5629 return 1;
e28cae4f 5630
3e4093b6
RS
5631 if (!designator_depth)
5632 {
366de0ce 5633 gcc_assert (!constructor_range_stack);
de520661 5634
3e4093b6
RS
5635 /* Designator list starts at the level of closest explicit
5636 braces. */
5637 while (constructor_stack->implicit)
5638 process_init_element (pop_init_level (1));
5639 constructor_designated = 1;
5640 return 0;
5641 }
de520661 5642
366de0ce 5643 switch (TREE_CODE (constructor_type))
3c3fa147 5644 {
366de0ce
NS
5645 case RECORD_TYPE:
5646 case UNION_TYPE:
3e4093b6
RS
5647 subtype = TREE_TYPE (constructor_fields);
5648 if (subtype != error_mark_node)
5649 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
5650 break;
5651 case ARRAY_TYPE:
3e4093b6 5652 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
5653 break;
5654 default:
5655 gcc_unreachable ();
de520661 5656 }
400fbf9f 5657
3e4093b6
RS
5658 subcode = TREE_CODE (subtype);
5659 if (array && subcode != ARRAY_TYPE)
5660 {
5661 error_init ("array index in non-array initializer");
5662 return 1;
5663 }
5664 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5665 {
5666 error_init ("field name not in record or union initializer");
5667 return 1;
5668 }
d45cf215 5669
3e4093b6
RS
5670 constructor_designated = 1;
5671 push_init_level (2);
5672 return 0;
de520661 5673}
400fbf9f 5674
3e4093b6
RS
5675/* If there are range designators in designator list, push a new designator
5676 to constructor_range_stack. RANGE_END is end of such stack range or
5677 NULL_TREE if there is no range designator at this level. */
400fbf9f 5678
3e4093b6
RS
5679static void
5680push_range_stack (tree range_end)
5681{
5682 struct constructor_range_stack *p;
400fbf9f 5683
5d038c4c 5684 p = GGC_NEW (struct constructor_range_stack);
3e4093b6
RS
5685 p->prev = constructor_range_stack;
5686 p->next = 0;
5687 p->fields = constructor_fields;
5688 p->range_start = constructor_index;
5689 p->index = constructor_index;
5690 p->stack = constructor_stack;
5691 p->range_end = range_end;
8b6a5902 5692 if (constructor_range_stack)
3e4093b6
RS
5693 constructor_range_stack->next = p;
5694 constructor_range_stack = p;
de520661 5695}
400fbf9f 5696
3e4093b6
RS
5697/* Within an array initializer, specify the next index to be initialized.
5698 FIRST is that index. If LAST is nonzero, then initialize a range
5699 of indices, running from FIRST through LAST. */
5a7ec9d9 5700
de520661 5701void
3e4093b6 5702set_init_index (tree first, tree last)
de520661 5703{
3e4093b6
RS
5704 if (set_designator (1))
5705 return;
de520661 5706
b06df647 5707 designator_erroneous = 1;
de520661 5708
3ea8cd06
JM
5709 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5710 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5711 {
5712 error_init ("array index in initializer not of integer type");
5713 return;
5714 }
5715
3e4093b6
RS
5716 if (TREE_CODE (first) != INTEGER_CST)
5717 error_init ("nonconstant array index in initializer");
5718 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5719 error_init ("nonconstant array index in initializer");
5720 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5721 error_init ("array index in non-array initializer");
622adc7e
MK
5722 else if (tree_int_cst_sgn (first) == -1)
5723 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
5724 else if (constructor_max_index
5725 && tree_int_cst_lt (constructor_max_index, first))
5726 error_init ("array index in initializer exceeds array bounds");
5727 else
de520661 5728 {
3e4093b6 5729 constructor_index = convert (bitsizetype, first);
665f2503 5730
3e4093b6 5731 if (last)
2bede729 5732 {
3e4093b6
RS
5733 if (tree_int_cst_equal (first, last))
5734 last = 0;
5735 else if (tree_int_cst_lt (last, first))
5736 {
5737 error_init ("empty index range in initializer");
5738 last = 0;
5739 }
5740 else
5741 {
5742 last = convert (bitsizetype, last);
5743 if (constructor_max_index != 0
5744 && tree_int_cst_lt (constructor_max_index, last))
5745 {
5746 error_init ("array index range in initializer exceeds array bounds");
5747 last = 0;
5748 }
5749 }
2bede729 5750 }
fed3cef0 5751
3e4093b6 5752 designator_depth++;
b06df647 5753 designator_erroneous = 0;
3e4093b6
RS
5754 if (constructor_range_stack || last)
5755 push_range_stack (last);
de520661 5756 }
de520661 5757}
3e4093b6
RS
5758
5759/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 5760
de520661 5761void
3e4093b6 5762set_init_label (tree fieldname)
de520661 5763{
3e4093b6 5764 tree tail;
94ba5069 5765
3e4093b6
RS
5766 if (set_designator (0))
5767 return;
5768
b06df647 5769 designator_erroneous = 1;
3e4093b6
RS
5770
5771 if (TREE_CODE (constructor_type) != RECORD_TYPE
5772 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 5773 {
3e4093b6
RS
5774 error_init ("field name not in record or union initializer");
5775 return;
94ba5069
RS
5776 }
5777
3e4093b6
RS
5778 for (tail = TYPE_FIELDS (constructor_type); tail;
5779 tail = TREE_CHAIN (tail))
8b6a5902 5780 {
3e4093b6
RS
5781 if (DECL_NAME (tail) == fieldname)
5782 break;
8b6a5902
JJ
5783 }
5784
3e4093b6 5785 if (tail == 0)
c51a1ba9 5786 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 5787 else
8b6a5902 5788 {
3e4093b6
RS
5789 constructor_fields = tail;
5790 designator_depth++;
b06df647 5791 designator_erroneous = 0;
3e4093b6
RS
5792 if (constructor_range_stack)
5793 push_range_stack (NULL_TREE);
8b6a5902 5794 }
3e4093b6
RS
5795}
5796\f
5797/* Add a new initializer to the tree of pending initializers. PURPOSE
5798 identifies the initializer, either array index or field in a structure.
5799 VALUE is the value of that index or field. */
de520661 5800
3e4093b6
RS
5801static void
5802add_pending_init (tree purpose, tree value)
5803{
5804 struct init_node *p, **q, *r;
5805
5806 q = &constructor_pending_elts;
5807 p = 0;
5808
5809 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 5810 {
3e4093b6 5811 while (*q != 0)
91fa3c30 5812 {
3e4093b6
RS
5813 p = *q;
5814 if (tree_int_cst_lt (purpose, p->purpose))
5815 q = &p->left;
5816 else if (tree_int_cst_lt (p->purpose, purpose))
5817 q = &p->right;
5818 else
5819 {
5820 if (TREE_SIDE_EFFECTS (p->value))
5821 warning_init ("initialized field with side-effects overwritten");
7ed322d7
JM
5822 else if (warn_override_init)
5823 warning_init ("initialized field overwritten");
3e4093b6
RS
5824 p->value = value;
5825 return;
5826 }
91fa3c30 5827 }
de520661 5828 }
3e4093b6 5829 else
de520661 5830 {
3e4093b6 5831 tree bitpos;
400fbf9f 5832
3e4093b6
RS
5833 bitpos = bit_position (purpose);
5834 while (*q != NULL)
5835 {
5836 p = *q;
5837 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5838 q = &p->left;
5839 else if (p->purpose != purpose)
5840 q = &p->right;
5841 else
5842 {
5843 if (TREE_SIDE_EFFECTS (p->value))
5844 warning_init ("initialized field with side-effects overwritten");
7ed322d7
JM
5845 else if (warn_override_init)
5846 warning_init ("initialized field overwritten");
3e4093b6
RS
5847 p->value = value;
5848 return;
5849 }
5850 }
91fa3c30 5851 }
b71c7f8a 5852
5d038c4c 5853 r = GGC_NEW (struct init_node);
3e4093b6
RS
5854 r->purpose = purpose;
5855 r->value = value;
8b6a5902 5856
3e4093b6
RS
5857 *q = r;
5858 r->parent = p;
5859 r->left = 0;
5860 r->right = 0;
5861 r->balance = 0;
b71c7f8a 5862
3e4093b6 5863 while (p)
de520661 5864 {
3e4093b6 5865 struct init_node *s;
665f2503 5866
3e4093b6 5867 if (r == p->left)
2bede729 5868 {
3e4093b6
RS
5869 if (p->balance == 0)
5870 p->balance = -1;
5871 else if (p->balance < 0)
5872 {
5873 if (r->balance < 0)
5874 {
5875 /* L rotation. */
5876 p->left = r->right;
5877 if (p->left)
5878 p->left->parent = p;
5879 r->right = p;
e7b6a0ee 5880
3e4093b6
RS
5881 p->balance = 0;
5882 r->balance = 0;
39bc99c2 5883
3e4093b6
RS
5884 s = p->parent;
5885 p->parent = r;
5886 r->parent = s;
5887 if (s)
5888 {
5889 if (s->left == p)
5890 s->left = r;
5891 else
5892 s->right = r;
5893 }
5894 else
5895 constructor_pending_elts = r;
5896 }
5897 else
5898 {
5899 /* LR rotation. */
5900 struct init_node *t = r->right;
e7b6a0ee 5901
3e4093b6
RS
5902 r->right = t->left;
5903 if (r->right)
5904 r->right->parent = r;
5905 t->left = r;
5906
5907 p->left = t->right;
5908 if (p->left)
5909 p->left->parent = p;
5910 t->right = p;
5911
5912 p->balance = t->balance < 0;
5913 r->balance = -(t->balance > 0);
5914 t->balance = 0;
5915
5916 s = p->parent;
5917 p->parent = t;
5918 r->parent = t;
5919 t->parent = s;
5920 if (s)
5921 {
5922 if (s->left == p)
5923 s->left = t;
5924 else
5925 s->right = t;
5926 }
5927 else
5928 constructor_pending_elts = t;
5929 }
5930 break;
5931 }
5932 else
5933 {
5934 /* p->balance == +1; growth of left side balances the node. */
5935 p->balance = 0;
5936 break;
5937 }
2bede729 5938 }
3e4093b6
RS
5939 else /* r == p->right */
5940 {
5941 if (p->balance == 0)
5942 /* Growth propagation from right side. */
5943 p->balance++;
5944 else if (p->balance > 0)
5945 {
5946 if (r->balance > 0)
5947 {
5948 /* R rotation. */
5949 p->right = r->left;
5950 if (p->right)
5951 p->right->parent = p;
5952 r->left = p;
5953
5954 p->balance = 0;
5955 r->balance = 0;
5956
5957 s = p->parent;
5958 p->parent = r;
5959 r->parent = s;
5960 if (s)
5961 {
5962 if (s->left == p)
5963 s->left = r;
5964 else
5965 s->right = r;
5966 }
5967 else
5968 constructor_pending_elts = r;
5969 }
5970 else /* r->balance == -1 */
5971 {
5972 /* RL rotation */
5973 struct init_node *t = r->left;
5974
5975 r->left = t->right;
5976 if (r->left)
5977 r->left->parent = r;
5978 t->right = r;
5979
5980 p->right = t->left;
5981 if (p->right)
5982 p->right->parent = p;
5983 t->left = p;
5984
5985 r->balance = (t->balance < 0);
5986 p->balance = -(t->balance > 0);
5987 t->balance = 0;
5988
5989 s = p->parent;
5990 p->parent = t;
5991 r->parent = t;
5992 t->parent = s;
5993 if (s)
5994 {
5995 if (s->left == p)
5996 s->left = t;
5997 else
5998 s->right = t;
5999 }
6000 else
6001 constructor_pending_elts = t;
6002 }
6003 break;
6004 }
6005 else
6006 {
6007 /* p->balance == -1; growth of right side balances the node. */
6008 p->balance = 0;
6009 break;
6010 }
6011 }
6012
6013 r = p;
6014 p = p->parent;
6015 }
6016}
6017
6018/* Build AVL tree from a sorted chain. */
6019
6020static void
6021set_nonincremental_init (void)
6022{
4038c495
GB
6023 unsigned HOST_WIDE_INT ix;
6024 tree index, value;
3e4093b6
RS
6025
6026 if (TREE_CODE (constructor_type) != RECORD_TYPE
6027 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6028 return;
6029
4038c495
GB
6030 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6031 add_pending_init (index, value);
3e4093b6
RS
6032 constructor_elements = 0;
6033 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6034 {
6035 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6036 /* Skip any nameless bit fields at the beginning. */
6037 while (constructor_unfilled_fields != 0
6038 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6039 && DECL_NAME (constructor_unfilled_fields) == 0)
6040 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 6041
de520661 6042 }
3e4093b6 6043 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 6044 {
3e4093b6
RS
6045 if (TYPE_DOMAIN (constructor_type))
6046 constructor_unfilled_index
6047 = convert (bitsizetype,
6048 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6049 else
6050 constructor_unfilled_index = bitsize_zero_node;
de520661 6051 }
3e4093b6 6052 constructor_incremental = 0;
de520661 6053}
400fbf9f 6054
3e4093b6 6055/* Build AVL tree from a string constant. */
de520661 6056
3e4093b6
RS
6057static void
6058set_nonincremental_init_from_string (tree str)
de520661 6059{
3e4093b6
RS
6060 tree value, purpose, type;
6061 HOST_WIDE_INT val[2];
6062 const char *p, *end;
6063 int byte, wchar_bytes, charwidth, bitpos;
de520661 6064
366de0ce 6065 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 6066
3e4093b6
RS
6067 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6068 == TYPE_PRECISION (char_type_node))
6069 wchar_bytes = 1;
3e4093b6 6070 else
366de0ce
NS
6071 {
6072 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6073 == TYPE_PRECISION (wchar_type_node));
6074 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6075 }
3e4093b6
RS
6076 charwidth = TYPE_PRECISION (char_type_node);
6077 type = TREE_TYPE (constructor_type);
6078 p = TREE_STRING_POINTER (str);
6079 end = p + TREE_STRING_LENGTH (str);
91fa3c30 6080
3e4093b6
RS
6081 for (purpose = bitsize_zero_node;
6082 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6083 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 6084 {
3e4093b6 6085 if (wchar_bytes == 1)
ffc5c6a9 6086 {
3e4093b6
RS
6087 val[1] = (unsigned char) *p++;
6088 val[0] = 0;
ffc5c6a9
RH
6089 }
6090 else
3e4093b6
RS
6091 {
6092 val[0] = 0;
6093 val[1] = 0;
6094 for (byte = 0; byte < wchar_bytes; byte++)
6095 {
6096 if (BYTES_BIG_ENDIAN)
6097 bitpos = (wchar_bytes - byte - 1) * charwidth;
6098 else
6099 bitpos = byte * charwidth;
6100 val[bitpos < HOST_BITS_PER_WIDE_INT]
6101 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6102 << (bitpos % HOST_BITS_PER_WIDE_INT);
6103 }
6104 }
584ef5fe 6105
8df83eae 6106 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
6107 {
6108 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6109 if (bitpos < HOST_BITS_PER_WIDE_INT)
6110 {
6111 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6112 {
6113 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6114 val[0] = -1;
6115 }
6116 }
6117 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6118 {
6119 if (val[1] < 0)
c22cacf3 6120 val[0] = -1;
3e4093b6
RS
6121 }
6122 else if (val[0] & (((HOST_WIDE_INT) 1)
6123 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6124 val[0] |= ((HOST_WIDE_INT) -1)
6125 << (bitpos - HOST_BITS_PER_WIDE_INT);
6126 }
ffc5c6a9 6127
7d60be94 6128 value = build_int_cst_wide (type, val[1], val[0]);
3e4093b6 6129 add_pending_init (purpose, value);
9dfcc8db
BH
6130 }
6131
3e4093b6
RS
6132 constructor_incremental = 0;
6133}
de520661 6134
3e4093b6
RS
6135/* Return value of FIELD in pending initializer or zero if the field was
6136 not initialized yet. */
6137
6138static tree
6139find_init_member (tree field)
6140{
6141 struct init_node *p;
6142
6143 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 6144 {
3e4093b6
RS
6145 if (constructor_incremental
6146 && tree_int_cst_lt (field, constructor_unfilled_index))
6147 set_nonincremental_init ();
6148
6149 p = constructor_pending_elts;
6150 while (p)
19d76e60 6151 {
3e4093b6
RS
6152 if (tree_int_cst_lt (field, p->purpose))
6153 p = p->left;
6154 else if (tree_int_cst_lt (p->purpose, field))
6155 p = p->right;
6156 else
6157 return p->value;
19d76e60 6158 }
19d76e60 6159 }
3e4093b6 6160 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 6161 {
3e4093b6 6162 tree bitpos = bit_position (field);
de520661 6163
3e4093b6
RS
6164 if (constructor_incremental
6165 && (!constructor_unfilled_fields
6166 || tree_int_cst_lt (bitpos,
6167 bit_position (constructor_unfilled_fields))))
6168 set_nonincremental_init ();
de520661 6169
3e4093b6
RS
6170 p = constructor_pending_elts;
6171 while (p)
6172 {
6173 if (field == p->purpose)
6174 return p->value;
6175 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6176 p = p->left;
6177 else
6178 p = p->right;
6179 }
6180 }
6181 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 6182 {
4038c495
GB
6183 if (!VEC_empty (constructor_elt, constructor_elements)
6184 && (VEC_last (constructor_elt, constructor_elements)->index
6185 == field))
6186 return VEC_last (constructor_elt, constructor_elements)->value;
de520661 6187 }
3e4093b6 6188 return 0;
de520661
RS
6189}
6190
3e4093b6
RS
6191/* "Output" the next constructor element.
6192 At top level, really output it to assembler code now.
6193 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6194 TYPE is the data type that the containing data type wants here.
6195 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
6196 If VALUE is a string constant, STRICT_STRING is true if it is
6197 unparenthesized or we should not warn here for it being parenthesized.
6198 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 6199
3e4093b6
RS
6200 PENDING if non-nil means output pending elements that belong
6201 right after this element. (PENDING is normally 1;
6202 it is 0 while outputting pending elements, to avoid recursion.) */
8b6a5902 6203
3e4093b6 6204static void
916c5919
JM
6205output_init_element (tree value, bool strict_string, tree type, tree field,
6206 int pending)
3e4093b6 6207{
4038c495
GB
6208 constructor_elt *celt;
6209
0a880880 6210 if (type == error_mark_node || value == error_mark_node)
8b6a5902 6211 {
3e4093b6
RS
6212 constructor_erroneous = 1;
6213 return;
8b6a5902 6214 }
46bdb9cf
JM
6215 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6216 && (TREE_CODE (value) == STRING_CST
6217 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6218 && !(TREE_CODE (value) == STRING_CST
6219 && TREE_CODE (type) == ARRAY_TYPE
6220 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6221 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6222 TYPE_MAIN_VARIANT (type)))
f2a71bbc 6223 value = array_to_pointer_conversion (value);
8b6a5902 6224
3e4093b6
RS
6225 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6226 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 6227 {
3e4093b6
RS
6228 /* As an extension, allow initializing objects with static storage
6229 duration with compound literals (which are then treated just as
6230 the brace enclosed list they contain). */
6231 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6232 value = DECL_INITIAL (decl);
8b6a5902
JJ
6233 }
6234
3e4093b6
RS
6235 if (value == error_mark_node)
6236 constructor_erroneous = 1;
6237 else if (!TREE_CONSTANT (value))
6238 constructor_constant = 0;
116df786 6239 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
6240 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6241 || TREE_CODE (constructor_type) == UNION_TYPE)
6242 && DECL_C_BIT_FIELD (field)
6243 && TREE_CODE (value) != INTEGER_CST))
6244 constructor_simple = 0;
6245
116df786 6246 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 6247 {
116df786
RH
6248 if (require_constant_value)
6249 {
6250 error_init ("initializer element is not constant");
6251 value = error_mark_node;
6252 }
6253 else if (require_constant_elements)
6254 pedwarn ("initializer element is not computable at load time");
8b6a5902 6255 }
3e4093b6
RS
6256
6257 /* If this field is empty (and not at the end of structure),
6258 don't do anything other than checking the initializer. */
6259 if (field
6260 && (TREE_TYPE (field) == error_mark_node
6261 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6262 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6263 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6264 || TREE_CHAIN (field)))))
6265 return;
6266
916c5919 6267 value = digest_init (type, value, strict_string, require_constant_value);
3e4093b6 6268 if (value == error_mark_node)
8b6a5902 6269 {
3e4093b6
RS
6270 constructor_erroneous = 1;
6271 return;
8b6a5902 6272 }
8b6a5902 6273
3e4093b6
RS
6274 /* If this element doesn't come next in sequence,
6275 put it on constructor_pending_elts. */
6276 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6277 && (!constructor_incremental
6278 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 6279 {
3e4093b6
RS
6280 if (constructor_incremental
6281 && tree_int_cst_lt (field, constructor_unfilled_index))
6282 set_nonincremental_init ();
6283
6284 add_pending_init (field, value);
6285 return;
8b6a5902 6286 }
3e4093b6
RS
6287 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6288 && (!constructor_incremental
6289 || field != constructor_unfilled_fields))
8b6a5902 6290 {
3e4093b6
RS
6291 /* We do this for records but not for unions. In a union,
6292 no matter which field is specified, it can be initialized
6293 right away since it starts at the beginning of the union. */
6294 if (constructor_incremental)
6295 {
6296 if (!constructor_unfilled_fields)
6297 set_nonincremental_init ();
6298 else
6299 {
6300 tree bitpos, unfillpos;
6301
6302 bitpos = bit_position (field);
6303 unfillpos = bit_position (constructor_unfilled_fields);
6304
6305 if (tree_int_cst_lt (bitpos, unfillpos))
6306 set_nonincremental_init ();
6307 }
6308 }
6309
6310 add_pending_init (field, value);
6311 return;
8b6a5902 6312 }
3e4093b6 6313 else if (TREE_CODE (constructor_type) == UNION_TYPE
4038c495 6314 && !VEC_empty (constructor_elt, constructor_elements))
3e4093b6 6315 {
4038c495
GB
6316 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6317 constructor_elements)->value))
3e4093b6 6318 warning_init ("initialized field with side-effects overwritten");
7ed322d7
JM
6319 else if (warn_override_init)
6320 warning_init ("initialized field overwritten");
8b6a5902 6321
3e4093b6
RS
6322 /* We can have just one union field set. */
6323 constructor_elements = 0;
6324 }
8b6a5902 6325
3e4093b6
RS
6326 /* Otherwise, output this element either to
6327 constructor_elements or to the assembler file. */
8b6a5902 6328
4038c495
GB
6329 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6330 celt->index = field;
6331 celt->value = value;
8b6a5902 6332
3e4093b6
RS
6333 /* Advance the variable that indicates sequential elements output. */
6334 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6335 constructor_unfilled_index
6336 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6337 bitsize_one_node);
6338 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6339 {
6340 constructor_unfilled_fields
6341 = TREE_CHAIN (constructor_unfilled_fields);
8b6a5902 6342
3e4093b6
RS
6343 /* Skip any nameless bit fields. */
6344 while (constructor_unfilled_fields != 0
6345 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6346 && DECL_NAME (constructor_unfilled_fields) == 0)
6347 constructor_unfilled_fields =
6348 TREE_CHAIN (constructor_unfilled_fields);
6349 }
6350 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6351 constructor_unfilled_fields = 0;
de520661 6352
3e4093b6
RS
6353 /* Now output any pending elements which have become next. */
6354 if (pending)
6355 output_pending_init_elements (0);
6356}
8b6a5902 6357
3e4093b6
RS
6358/* Output any pending elements which have become next.
6359 As we output elements, constructor_unfilled_{fields,index}
6360 advances, which may cause other elements to become next;
6361 if so, they too are output.
8b6a5902 6362
3e4093b6
RS
6363 If ALL is 0, we return when there are
6364 no more pending elements to output now.
665f2503 6365
3e4093b6
RS
6366 If ALL is 1, we output space as necessary so that
6367 we can output all the pending elements. */
19d76e60 6368
3e4093b6
RS
6369static void
6370output_pending_init_elements (int all)
6371{
6372 struct init_node *elt = constructor_pending_elts;
6373 tree next;
de520661 6374
3e4093b6
RS
6375 retry:
6376
ba228239 6377 /* Look through the whole pending tree.
3e4093b6
RS
6378 If we find an element that should be output now,
6379 output it. Otherwise, set NEXT to the element
6380 that comes first among those still pending. */
6381
6382 next = 0;
6383 while (elt)
6384 {
6385 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 6386 {
3e4093b6
RS
6387 if (tree_int_cst_equal (elt->purpose,
6388 constructor_unfilled_index))
916c5919 6389 output_init_element (elt->value, true,
3e4093b6
RS
6390 TREE_TYPE (constructor_type),
6391 constructor_unfilled_index, 0);
6392 else if (tree_int_cst_lt (constructor_unfilled_index,
6393 elt->purpose))
8b6a5902 6394 {
3e4093b6
RS
6395 /* Advance to the next smaller node. */
6396 if (elt->left)
6397 elt = elt->left;
6398 else
6399 {
6400 /* We have reached the smallest node bigger than the
6401 current unfilled index. Fill the space first. */
6402 next = elt->purpose;
6403 break;
6404 }
8b6a5902 6405 }
ce662d4c
JJ
6406 else
6407 {
3e4093b6
RS
6408 /* Advance to the next bigger node. */
6409 if (elt->right)
6410 elt = elt->right;
6411 else
ce662d4c 6412 {
3e4093b6
RS
6413 /* We have reached the biggest node in a subtree. Find
6414 the parent of it, which is the next bigger node. */
6415 while (elt->parent && elt->parent->right == elt)
6416 elt = elt->parent;
6417 elt = elt->parent;
6418 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6419 elt->purpose))
6420 {
6421 next = elt->purpose;
6422 break;
6423 }
ce662d4c
JJ
6424 }
6425 }
8b6a5902 6426 }
3e4093b6
RS
6427 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6428 || TREE_CODE (constructor_type) == UNION_TYPE)
6429 {
6430 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 6431
3e4093b6
RS
6432 /* If the current record is complete we are done. */
6433 if (constructor_unfilled_fields == 0)
6434 break;
de520661 6435
3e4093b6
RS
6436 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6437 elt_bitpos = bit_position (elt->purpose);
6438 /* We can't compare fields here because there might be empty
6439 fields in between. */
6440 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6441 {
6442 constructor_unfilled_fields = elt->purpose;
916c5919 6443 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
3e4093b6
RS
6444 elt->purpose, 0);
6445 }
6446 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6447 {
6448 /* Advance to the next smaller node. */
6449 if (elt->left)
6450 elt = elt->left;
6451 else
6452 {
6453 /* We have reached the smallest node bigger than the
6454 current unfilled field. Fill the space first. */
6455 next = elt->purpose;
6456 break;
6457 }
6458 }
6459 else
6460 {
6461 /* Advance to the next bigger node. */
6462 if (elt->right)
6463 elt = elt->right;
6464 else
6465 {
6466 /* We have reached the biggest node in a subtree. Find
6467 the parent of it, which is the next bigger node. */
6468 while (elt->parent && elt->parent->right == elt)
6469 elt = elt->parent;
6470 elt = elt->parent;
6471 if (elt
6472 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6473 bit_position (elt->purpose))))
6474 {
6475 next = elt->purpose;
6476 break;
6477 }
6478 }
6479 }
6480 }
6481 }
de520661 6482
3e4093b6
RS
6483 /* Ordinarily return, but not if we want to output all
6484 and there are elements left. */
3f75a254 6485 if (!(all && next != 0))
e5cfb88f
RK
6486 return;
6487
3e4093b6
RS
6488 /* If it's not incremental, just skip over the gap, so that after
6489 jumping to retry we will output the next successive element. */
6490 if (TREE_CODE (constructor_type) == RECORD_TYPE
6491 || TREE_CODE (constructor_type) == UNION_TYPE)
6492 constructor_unfilled_fields = next;
6493 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6494 constructor_unfilled_index = next;
de520661 6495
3e4093b6
RS
6496 /* ELT now points to the node in the pending tree with the next
6497 initializer to output. */
6498 goto retry;
de520661
RS
6499}
6500\f
3e4093b6
RS
6501/* Add one non-braced element to the current constructor level.
6502 This adjusts the current position within the constructor's type.
6503 This may also start or terminate implicit levels
6504 to handle a partly-braced initializer.
e5e809f4 6505
3e4093b6
RS
6506 Once this has found the correct level for the new element,
6507 it calls output_init_element. */
6508
6509void
916c5919 6510process_init_element (struct c_expr value)
e5e809f4 6511{
916c5919
JM
6512 tree orig_value = value.value;
6513 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6514 bool strict_string = value.original_code == STRING_CST;
e5e809f4 6515
3e4093b6 6516 designator_depth = 0;
b06df647 6517 designator_erroneous = 0;
e5e809f4 6518
3e4093b6
RS
6519 /* Handle superfluous braces around string cst as in
6520 char x[] = {"foo"}; */
6521 if (string_flag
6522 && constructor_type
6523 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 6524 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 6525 && integer_zerop (constructor_unfilled_index))
e5e809f4 6526 {
916c5919 6527 if (constructor_stack->replacement_value.value)
c22cacf3 6528 error_init ("excess elements in char array initializer");
3e4093b6
RS
6529 constructor_stack->replacement_value = value;
6530 return;
e5e809f4 6531 }
8b6a5902 6532
916c5919 6533 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
6534 {
6535 error_init ("excess elements in struct initializer");
6536 return;
e5e809f4
JL
6537 }
6538
3e4093b6
RS
6539 /* Ignore elements of a brace group if it is entirely superfluous
6540 and has already been diagnosed. */
6541 if (constructor_type == 0)
6542 return;
e5e809f4 6543
3e4093b6
RS
6544 /* If we've exhausted any levels that didn't have braces,
6545 pop them now. */
6546 while (constructor_stack->implicit)
6547 {
6548 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6549 || TREE_CODE (constructor_type) == UNION_TYPE)
6550 && constructor_fields == 0)
6551 process_init_element (pop_init_level (1));
6552 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6553 && (constructor_max_index == 0
6554 || tree_int_cst_lt (constructor_max_index,
6555 constructor_index)))
6556 process_init_element (pop_init_level (1));
6557 else
6558 break;
6559 }
e5e809f4 6560
3e4093b6
RS
6561 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6562 if (constructor_range_stack)
e5e809f4 6563 {
3e4093b6
RS
6564 /* If value is a compound literal and we'll be just using its
6565 content, don't put it into a SAVE_EXPR. */
916c5919 6566 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
6567 || !require_constant_value
6568 || flag_isoc99)
916c5919 6569 value.value = save_expr (value.value);
3e4093b6 6570 }
e5e809f4 6571
3e4093b6
RS
6572 while (1)
6573 {
6574 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 6575 {
3e4093b6
RS
6576 tree fieldtype;
6577 enum tree_code fieldcode;
e5e809f4 6578
3e4093b6
RS
6579 if (constructor_fields == 0)
6580 {
6581 pedwarn_init ("excess elements in struct initializer");
6582 break;
6583 }
e5e809f4 6584
3e4093b6
RS
6585 fieldtype = TREE_TYPE (constructor_fields);
6586 if (fieldtype != error_mark_node)
6587 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6588 fieldcode = TREE_CODE (fieldtype);
e5e809f4 6589
3e4093b6
RS
6590 /* Error for non-static initialization of a flexible array member. */
6591 if (fieldcode == ARRAY_TYPE
6592 && !require_constant_value
6593 && TYPE_SIZE (fieldtype) == NULL_TREE
6594 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6595 {
6596 error_init ("non-static initialization of a flexible array member");
6597 break;
6598 }
e5e809f4 6599
3e4093b6 6600 /* Accept a string constant to initialize a subarray. */
916c5919 6601 if (value.value != 0
3e4093b6 6602 && fieldcode == ARRAY_TYPE
197463ae 6603 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 6604 && string_flag)
916c5919 6605 value.value = orig_value;
3e4093b6
RS
6606 /* Otherwise, if we have come to a subaggregate,
6607 and we don't have an element of its type, push into it. */
0953878d 6608 else if (value.value != 0
916c5919
JM
6609 && value.value != error_mark_node
6610 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6
RS
6611 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6612 || fieldcode == UNION_TYPE))
6613 {
6614 push_init_level (1);
6615 continue;
6616 }
e5e809f4 6617
916c5919 6618 if (value.value)
3e4093b6
RS
6619 {
6620 push_member_name (constructor_fields);
916c5919
JM
6621 output_init_element (value.value, strict_string,
6622 fieldtype, constructor_fields, 1);
3e4093b6 6623 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
6624 }
6625 else
3e4093b6
RS
6626 /* Do the bookkeeping for an element that was
6627 directly output as a constructor. */
e5e809f4 6628 {
3e4093b6
RS
6629 /* For a record, keep track of end position of last field. */
6630 if (DECL_SIZE (constructor_fields))
c22cacf3 6631 constructor_bit_index
3e4093b6 6632 = size_binop (PLUS_EXPR,
c22cacf3
MS
6633 bit_position (constructor_fields),
6634 DECL_SIZE (constructor_fields));
3e4093b6
RS
6635
6636 /* If the current field was the first one not yet written out,
6637 it isn't now, so update. */
6638 if (constructor_unfilled_fields == constructor_fields)
6639 {
6640 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6641 /* Skip any nameless bit fields. */
6642 while (constructor_unfilled_fields != 0
6643 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6644 && DECL_NAME (constructor_unfilled_fields) == 0)
6645 constructor_unfilled_fields =
6646 TREE_CHAIN (constructor_unfilled_fields);
6647 }
e5e809f4 6648 }
3e4093b6
RS
6649
6650 constructor_fields = TREE_CHAIN (constructor_fields);
6651 /* Skip any nameless bit fields at the beginning. */
6652 while (constructor_fields != 0
6653 && DECL_C_BIT_FIELD (constructor_fields)
6654 && DECL_NAME (constructor_fields) == 0)
6655 constructor_fields = TREE_CHAIN (constructor_fields);
e5e809f4 6656 }
3e4093b6 6657 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 6658 {
3e4093b6
RS
6659 tree fieldtype;
6660 enum tree_code fieldcode;
e5e809f4 6661
3e4093b6
RS
6662 if (constructor_fields == 0)
6663 {
6664 pedwarn_init ("excess elements in union initializer");
6665 break;
6666 }
e5e809f4 6667
3e4093b6
RS
6668 fieldtype = TREE_TYPE (constructor_fields);
6669 if (fieldtype != error_mark_node)
6670 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6671 fieldcode = TREE_CODE (fieldtype);
e5e809f4 6672
3e4093b6
RS
6673 /* Warn that traditional C rejects initialization of unions.
6674 We skip the warning if the value is zero. This is done
6675 under the assumption that the zero initializer in user
6676 code appears conditioned on e.g. __STDC__ to avoid
6677 "missing initializer" warnings and relies on default
6678 initialization to zero in the traditional C case.
6679 We also skip the warning if the initializer is designated,
6680 again on the assumption that this must be conditional on
6681 __STDC__ anyway (and we've already complained about the
6682 member-designator already). */
3176a0c2 6683 if (!in_system_header && !constructor_designated
916c5919
JM
6684 && !(value.value && (integer_zerop (value.value)
6685 || real_zerop (value.value))))
3176a0c2
DD
6686 warning (OPT_Wtraditional, "traditional C rejects initialization "
6687 "of unions");
e5e809f4 6688
3e4093b6 6689 /* Accept a string constant to initialize a subarray. */
916c5919 6690 if (value.value != 0
3e4093b6 6691 && fieldcode == ARRAY_TYPE
197463ae 6692 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 6693 && string_flag)
916c5919 6694 value.value = orig_value;
3e4093b6
RS
6695 /* Otherwise, if we have come to a subaggregate,
6696 and we don't have an element of its type, push into it. */
0953878d 6697 else if (value.value != 0
916c5919
JM
6698 && value.value != error_mark_node
6699 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6
RS
6700 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6701 || fieldcode == UNION_TYPE))
6702 {
6703 push_init_level (1);
6704 continue;
6705 }
e5e809f4 6706
916c5919 6707 if (value.value)
3e4093b6
RS
6708 {
6709 push_member_name (constructor_fields);
916c5919
JM
6710 output_init_element (value.value, strict_string,
6711 fieldtype, constructor_fields, 1);
3e4093b6 6712 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
6713 }
6714 else
3e4093b6
RS
6715 /* Do the bookkeeping for an element that was
6716 directly output as a constructor. */
e5e809f4 6717 {
3e4093b6
RS
6718 constructor_bit_index = DECL_SIZE (constructor_fields);
6719 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
e5e809f4 6720 }
e5e809f4 6721
3e4093b6
RS
6722 constructor_fields = 0;
6723 }
6724 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6725 {
6726 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6727 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 6728
3e4093b6 6729 /* Accept a string constant to initialize a subarray. */
916c5919 6730 if (value.value != 0
3e4093b6 6731 && eltcode == ARRAY_TYPE
197463ae 6732 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 6733 && string_flag)
916c5919 6734 value.value = orig_value;
3e4093b6
RS
6735 /* Otherwise, if we have come to a subaggregate,
6736 and we don't have an element of its type, push into it. */
0953878d 6737 else if (value.value != 0
916c5919
JM
6738 && value.value != error_mark_node
6739 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6
RS
6740 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6741 || eltcode == UNION_TYPE))
6742 {
6743 push_init_level (1);
6744 continue;
6745 }
8b6a5902 6746
3e4093b6
RS
6747 if (constructor_max_index != 0
6748 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6749 || integer_all_onesp (constructor_max_index)))
6750 {
6751 pedwarn_init ("excess elements in array initializer");
6752 break;
6753 }
8b6a5902 6754
3e4093b6 6755 /* Now output the actual element. */
916c5919 6756 if (value.value)
3e4093b6 6757 {
a0f0ab9f 6758 push_array_bounds (tree_low_cst (constructor_index, 1));
916c5919
JM
6759 output_init_element (value.value, strict_string,
6760 elttype, constructor_index, 1);
3e4093b6
RS
6761 RESTORE_SPELLING_DEPTH (constructor_depth);
6762 }
2f6e4e97 6763
3e4093b6
RS
6764 constructor_index
6765 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 6766
916c5919 6767 if (!value.value)
3e4093b6
RS
6768 /* If we are doing the bookkeeping for an element that was
6769 directly output as a constructor, we must update
6770 constructor_unfilled_index. */
6771 constructor_unfilled_index = constructor_index;
6772 }
6773 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6774 {
6775 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 6776
c22cacf3
MS
6777 /* Do a basic check of initializer size. Note that vectors
6778 always have a fixed size derived from their type. */
3e4093b6
RS
6779 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6780 {
6781 pedwarn_init ("excess elements in vector initializer");
6782 break;
6783 }
8b6a5902 6784
3e4093b6 6785 /* Now output the actual element. */
916c5919
JM
6786 if (value.value)
6787 output_init_element (value.value, strict_string,
6788 elttype, constructor_index, 1);
8b6a5902 6789
3e4093b6
RS
6790 constructor_index
6791 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 6792
916c5919 6793 if (!value.value)
3e4093b6
RS
6794 /* If we are doing the bookkeeping for an element that was
6795 directly output as a constructor, we must update
6796 constructor_unfilled_index. */
6797 constructor_unfilled_index = constructor_index;
6798 }
8b6a5902 6799
3e4093b6
RS
6800 /* Handle the sole element allowed in a braced initializer
6801 for a scalar variable. */
b4519d39
SB
6802 else if (constructor_type != error_mark_node
6803 && constructor_fields == 0)
8b6a5902 6804 {
3e4093b6
RS
6805 pedwarn_init ("excess elements in scalar initializer");
6806 break;
8b6a5902
JJ
6807 }
6808 else
6809 {
916c5919
JM
6810 if (value.value)
6811 output_init_element (value.value, strict_string,
6812 constructor_type, NULL_TREE, 1);
3e4093b6 6813 constructor_fields = 0;
8b6a5902
JJ
6814 }
6815
3e4093b6
RS
6816 /* Handle range initializers either at this level or anywhere higher
6817 in the designator stack. */
6818 if (constructor_range_stack)
8b6a5902 6819 {
3e4093b6
RS
6820 struct constructor_range_stack *p, *range_stack;
6821 int finish = 0;
6822
6823 range_stack = constructor_range_stack;
6824 constructor_range_stack = 0;
6825 while (constructor_stack != range_stack->stack)
8b6a5902 6826 {
366de0ce 6827 gcc_assert (constructor_stack->implicit);
3e4093b6 6828 process_init_element (pop_init_level (1));
8b6a5902 6829 }
3e4093b6
RS
6830 for (p = range_stack;
6831 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6832 p = p->prev)
8b6a5902 6833 {
366de0ce 6834 gcc_assert (constructor_stack->implicit);
3e4093b6 6835 process_init_element (pop_init_level (1));
8b6a5902 6836 }
3e4093b6
RS
6837
6838 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6839 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6840 finish = 1;
6841
6842 while (1)
6843 {
6844 constructor_index = p->index;
6845 constructor_fields = p->fields;
6846 if (finish && p->range_end && p->index == p->range_start)
6847 {
6848 finish = 0;
6849 p->prev = 0;
6850 }
6851 p = p->next;
6852 if (!p)
6853 break;
6854 push_init_level (2);
6855 p->stack = constructor_stack;
6856 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6857 p->index = p->range_start;
6858 }
6859
6860 if (!finish)
6861 constructor_range_stack = range_stack;
6862 continue;
8b6a5902
JJ
6863 }
6864
3e4093b6 6865 break;
8b6a5902
JJ
6866 }
6867
3e4093b6
RS
6868 constructor_range_stack = 0;
6869}
6870\f
9f0e2d86
ZW
6871/* Build a complete asm-statement, whose components are a CV_QUALIFIER
6872 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 6873 an ASM_EXPR node). */
3e4093b6 6874tree
9f0e2d86 6875build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 6876{
6de9cd9a
DN
6877 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6878 ASM_VOLATILE_P (args) = 1;
9f0e2d86 6879 return add_stmt (args);
8b6a5902
JJ
6880}
6881
9f0e2d86
ZW
6882/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6883 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6884 SIMPLE indicates whether there was anything at all after the
6885 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 6886 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 6887tree
9f0e2d86
ZW
6888build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6889 bool simple)
e5e809f4 6890{
3e4093b6 6891 tree tail;
9f0e2d86 6892 tree args;
6de9cd9a
DN
6893 int i;
6894 const char *constraint;
74f0c611 6895 const char **oconstraints;
6de9cd9a 6896 bool allows_mem, allows_reg, is_inout;
74f0c611 6897 int ninputs, noutputs;
6de9cd9a
DN
6898
6899 ninputs = list_length (inputs);
6900 noutputs = list_length (outputs);
74f0c611
RH
6901 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6902
6903 string = resolve_asm_operand_names (string, outputs, inputs);
3e4093b6 6904
6de9cd9a
DN
6905 /* Remove output conversions that change the type but not the mode. */
6906 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 6907 {
3e4093b6 6908 tree output = TREE_VALUE (tail);
74f0c611
RH
6909
6910 /* ??? Really, this should not be here. Users should be using a
6911 proper lvalue, dammit. But there's a long history of using casts
6912 in the output operands. In cases like longlong.h, this becomes a
6913 primitive form of typechecking -- if the cast can be removed, then
6914 the output operand had a type of the proper width; otherwise we'll
6915 get an error. Gross, but ... */
3e4093b6 6916 STRIP_NOPS (output);
74f0c611
RH
6917
6918 if (!lvalue_or_else (output, lv_asm))
6919 output = error_mark_node;
8b6a5902 6920
5544530a
PB
6921 if (output != error_mark_node
6922 && (TREE_READONLY (output)
6923 || TYPE_READONLY (TREE_TYPE (output))
6924 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6925 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6926 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6927 readonly_error (output, lv_asm);
6928
6de9cd9a 6929 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
6930 oconstraints[i] = constraint;
6931
6932 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6933 &allows_mem, &allows_reg, &is_inout))
6934 {
6935 /* If the operand is going to end up in memory,
6936 mark it addressable. */
6937 if (!allows_reg && !c_mark_addressable (output))
6938 output = error_mark_node;
6939 }
6940 else
c22cacf3 6941 output = error_mark_node;
3e4093b6 6942
74f0c611 6943 TREE_VALUE (tail) = output;
8b6a5902 6944 }
3e4093b6 6945
74f0c611
RH
6946 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6947 {
6948 tree input;
6949
6950 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6951 input = TREE_VALUE (tail);
6952
74f0c611
RH
6953 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6954 oconstraints, &allows_mem, &allows_reg))
6955 {
6956 /* If the operand is going to end up in memory,
6957 mark it addressable. */
b4c33883
AP
6958 if (!allows_reg && allows_mem)
6959 {
6960 /* Strip the nops as we allow this case. FIXME, this really
6961 should be rejected or made deprecated. */
6962 STRIP_NOPS (input);
6963 if (!c_mark_addressable (input))
6964 input = error_mark_node;
6965 }
74f0c611
RH
6966 }
6967 else
6968 input = error_mark_node;
6969
6970 TREE_VALUE (tail) = input;
6971 }
3e4093b6 6972
e130a54b 6973 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
9f0e2d86 6974
5544530a
PB
6975 /* asm statements without outputs, including simple ones, are treated
6976 as volatile. */
6977 ASM_INPUT_P (args) = simple;
6978 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 6979
9f0e2d86 6980 return args;
e5e809f4 6981}
3e4093b6 6982\f
506e2710
RH
6983/* Generate a goto statement to LABEL. */
6984
6985tree
6986c_finish_goto_label (tree label)
6987{
6988 tree decl = lookup_label (label);
6989 if (!decl)
6990 return NULL_TREE;
6991
16ef3acc
JM
6992 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6993 {
6994 error ("jump into statement expression");
6995 return NULL_TREE;
6996 }
6997
187230a7
JM
6998 if (C_DECL_UNJUMPABLE_VM (decl))
6999 {
7000 error ("jump into scope of identifier with variably modified type");
7001 return NULL_TREE;
7002 }
7003
16ef3acc
JM
7004 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7005 {
7006 /* No jump from outside this statement expression context, so
7007 record that there is a jump from within this context. */
7008 struct c_label_list *nlist;
7009 nlist = XOBNEW (&parser_obstack, struct c_label_list);
187230a7
JM
7010 nlist->next = label_context_stack_se->labels_used;
7011 nlist->label = decl;
7012 label_context_stack_se->labels_used = nlist;
7013 }
7014
7015 if (!C_DECL_UNDEFINABLE_VM (decl))
7016 {
7017 /* No jump from outside this context context of identifiers with
7018 variably modified type, so record that there is a jump from
7019 within this context. */
7020 struct c_label_list *nlist;
7021 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7022 nlist->next = label_context_stack_vm->labels_used;
16ef3acc 7023 nlist->label = decl;
187230a7 7024 label_context_stack_vm->labels_used = nlist;
16ef3acc
JM
7025 }
7026
506e2710 7027 TREE_USED (decl) = 1;
53fb4de3 7028 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
506e2710
RH
7029}
7030
7031/* Generate a computed goto statement to EXPR. */
7032
7033tree
7034c_finish_goto_ptr (tree expr)
7035{
7036 if (pedantic)
bda67431 7037 pedwarn ("ISO C forbids %<goto *expr;%>");
506e2710 7038 expr = convert (ptr_type_node, expr);
53fb4de3 7039 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
506e2710
RH
7040}
7041
5088b058
RH
7042/* Generate a C `return' statement. RETVAL is the expression for what
7043 to return, or a null pointer for `return;' with no value. */
de520661 7044
506e2710 7045tree
5088b058 7046c_finish_return (tree retval)
3e4093b6 7047{
0c9b182b
JJ
7048 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7049 bool no_warning = false;
3e4093b6
RS
7050
7051 if (TREE_THIS_VOLATILE (current_function_decl))
d4ee4d25 7052 warning (0, "function declared %<noreturn%> has a %<return%> statement");
3e4093b6
RS
7053
7054 if (!retval)
de520661 7055 {
3e4093b6
RS
7056 current_function_returns_null = 1;
7057 if ((warn_return_type || flag_isoc99)
7058 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b
JJ
7059 {
7060 pedwarn_c99 ("%<return%> with no value, in "
7061 "function returning non-void");
7062 no_warning = true;
7063 }
400fbf9f 7064 }
3e4093b6 7065 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 7066 {
3e4093b6 7067 current_function_returns_null = 1;
2397c575 7068 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
bda67431 7069 pedwarn ("%<return%> with a value, in function returning void");
2397c575
MLI
7070 else if (pedantic)
7071 pedwarn ("ISO C forbids %<return%> with expression, in function returning void");
de520661 7072 }
3e4093b6 7073 else
de520661 7074 {
2ac2f164 7075 tree t = convert_for_assignment (valtype, retval, ic_return,
3e4093b6
RS
7076 NULL_TREE, NULL_TREE, 0);
7077 tree res = DECL_RESULT (current_function_decl);
7078 tree inner;
7079
7080 current_function_returns_value = 1;
7081 if (t == error_mark_node)
506e2710 7082 return NULL_TREE;
3e4093b6
RS
7083
7084 inner = t = convert (TREE_TYPE (res), t);
7085
7086 /* Strip any conversions, additions, and subtractions, and see if
7087 we are returning the address of a local variable. Warn if so. */
7088 while (1)
8b6a5902 7089 {
3e4093b6 7090 switch (TREE_CODE (inner))
8b6a5902 7091 {
3e4093b6
RS
7092 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7093 case PLUS_EXPR:
7094 inner = TREE_OPERAND (inner, 0);
7095 continue;
7096
7097 case MINUS_EXPR:
7098 /* If the second operand of the MINUS_EXPR has a pointer
7099 type (or is converted from it), this may be valid, so
7100 don't give a warning. */
7101 {
7102 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 7103
3f75a254 7104 while (!POINTER_TYPE_P (TREE_TYPE (op1))
3e4093b6
RS
7105 && (TREE_CODE (op1) == NOP_EXPR
7106 || TREE_CODE (op1) == NON_LVALUE_EXPR
7107 || TREE_CODE (op1) == CONVERT_EXPR))
7108 op1 = TREE_OPERAND (op1, 0);
8b6a5902 7109
3e4093b6
RS
7110 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7111 break;
8b6a5902 7112
3e4093b6
RS
7113 inner = TREE_OPERAND (inner, 0);
7114 continue;
7115 }
400fbf9f 7116
3e4093b6
RS
7117 case ADDR_EXPR:
7118 inner = TREE_OPERAND (inner, 0);
c2f4acb7 7119
6615c446 7120 while (REFERENCE_CLASS_P (inner)
c22cacf3 7121 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 7122 inner = TREE_OPERAND (inner, 0);
8b6a5902 7123
a2f1f4c3 7124 if (DECL_P (inner)
3f75a254
JM
7125 && !DECL_EXTERNAL (inner)
7126 && !TREE_STATIC (inner)
3e4093b6 7127 && DECL_CONTEXT (inner) == current_function_decl)
d4ee4d25 7128 warning (0, "function returns address of local variable");
3e4093b6 7129 break;
8b6a5902 7130
3e4093b6
RS
7131 default:
7132 break;
7133 }
de520661 7134
3e4093b6
RS
7135 break;
7136 }
7137
53fb4de3 7138 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
de520661 7139 }
8b6a5902 7140
0c9b182b
JJ
7141 ret_stmt = build_stmt (RETURN_EXPR, retval);
7142 TREE_NO_WARNING (ret_stmt) |= no_warning;
7143 return add_stmt (ret_stmt);
de520661 7144}
3e4093b6
RS
7145\f
7146struct c_switch {
604f5adf
ILT
7147 /* The SWITCH_EXPR being built. */
7148 tree switch_expr;
a6c0a76c 7149
89dbed81 7150 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
7151 default conversion is applied. */
7152 tree orig_type;
7153
3e4093b6
RS
7154 /* A splay-tree mapping the low element of a case range to the high
7155 element, or NULL_TREE if there is no high element. Used to
7156 determine whether or not a new case label duplicates an old case
7157 label. We need a tree, rather than simply a hash table, because
7158 of the GNU case range extension. */
7159 splay_tree cases;
a6c0a76c 7160
16ef3acc
JM
7161 /* Number of nested statement expressions within this switch
7162 statement; if nonzero, case and default labels may not
7163 appear. */
7164 unsigned int blocked_stmt_expr;
7165
187230a7
JM
7166 /* Scope of outermost declarations of identifiers with variably
7167 modified type within this switch statement; if nonzero, case and
7168 default labels may not appear. */
7169 unsigned int blocked_vm;
7170
3e4093b6
RS
7171 /* The next node on the stack. */
7172 struct c_switch *next;
7173};
400fbf9f 7174
3e4093b6
RS
7175/* A stack of the currently active switch statements. The innermost
7176 switch statement is on the top of the stack. There is no need to
7177 mark the stack for garbage collection because it is only active
7178 during the processing of the body of a function, and we never
7179 collect at that point. */
de520661 7180
506e2710 7181struct c_switch *c_switch_stack;
de520661 7182
3e4093b6 7183/* Start a C switch statement, testing expression EXP. Return the new
604f5adf 7184 SWITCH_EXPR. */
de520661 7185
3e4093b6
RS
7186tree
7187c_start_case (tree exp)
de520661 7188{
c58e8676 7189 tree orig_type = error_mark_node;
3e4093b6 7190 struct c_switch *cs;
2f6e4e97 7191
3e4093b6 7192 if (exp != error_mark_node)
de520661 7193 {
3e4093b6
RS
7194 orig_type = TREE_TYPE (exp);
7195
c58e8676 7196 if (!INTEGRAL_TYPE_P (orig_type))
de520661 7197 {
c58e8676
VR
7198 if (orig_type != error_mark_node)
7199 {
7200 error ("switch quantity not an integer");
7201 orig_type = error_mark_node;
7202 }
3e4093b6 7203 exp = integer_zero_node;
de520661 7204 }
3e4093b6 7205 else
de520661 7206 {
c58e8676 7207 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 7208
3176a0c2 7209 if (!in_system_header
3e4093b6
RS
7210 && (type == long_integer_type_node
7211 || type == long_unsigned_type_node))
3176a0c2
DD
7212 warning (OPT_Wtraditional, "%<long%> switch expression not "
7213 "converted to %<int%> in ISO C");
8b6a5902 7214
3e4093b6 7215 exp = default_conversion (exp);
3e4093b6
RS
7216 }
7217 }
7218
604f5adf 7219 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 7220 cs = XNEW (struct c_switch);
604f5adf 7221 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
a6c0a76c 7222 cs->orig_type = orig_type;
3e4093b6 7223 cs->cases = splay_tree_new (case_compare, NULL, NULL);
16ef3acc 7224 cs->blocked_stmt_expr = 0;
187230a7 7225 cs->blocked_vm = 0;
506e2710
RH
7226 cs->next = c_switch_stack;
7227 c_switch_stack = cs;
3e4093b6 7228
604f5adf 7229 return add_stmt (cs->switch_expr);
3e4093b6
RS
7230}
7231
7232/* Process a case label. */
7233
7234tree
7235do_case (tree low_value, tree high_value)
7236{
7237 tree label = NULL_TREE;
7238
187230a7
JM
7239 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7240 && !c_switch_stack->blocked_vm)
3e4093b6 7241 {
506e2710 7242 label = c_add_case_label (c_switch_stack->cases,
604f5adf 7243 SWITCH_COND (c_switch_stack->switch_expr),
a6c0a76c 7244 c_switch_stack->orig_type,
3e4093b6
RS
7245 low_value, high_value);
7246 if (label == error_mark_node)
7247 label = NULL_TREE;
de520661 7248 }
16ef3acc
JM
7249 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7250 {
7251 if (low_value)
7252 error ("case label in statement expression not containing "
7253 "enclosing switch statement");
7254 else
7255 error ("%<default%> label in statement expression not containing "
7256 "enclosing switch statement");
7257 }
187230a7
JM
7258 else if (c_switch_stack && c_switch_stack->blocked_vm)
7259 {
7260 if (low_value)
7261 error ("case label in scope of identifier with variably modified "
7262 "type not containing enclosing switch statement");
7263 else
7264 error ("%<default%> label in scope of identifier with variably "
7265 "modified type not containing enclosing switch statement");
7266 }
3e4093b6
RS
7267 else if (low_value)
7268 error ("case label not within a switch statement");
7269 else
bda67431 7270 error ("%<default%> label not within a switch statement");
de520661 7271
3e4093b6
RS
7272 return label;
7273}
de520661 7274
3e4093b6 7275/* Finish the switch statement. */
de520661 7276
3e4093b6 7277void
325c3691 7278c_finish_case (tree body)
3e4093b6 7279{
506e2710 7280 struct c_switch *cs = c_switch_stack;
fbc315db 7281 location_t switch_location;
3e4093b6 7282
604f5adf 7283 SWITCH_BODY (cs->switch_expr) = body;
325c3691 7284
187230a7
JM
7285 /* We must not be within a statement expression nested in the switch
7286 at this point; we might, however, be within the scope of an
7287 identifier with variably modified type nested in the switch. */
16ef3acc
JM
7288 gcc_assert (!cs->blocked_stmt_expr);
7289
6de9cd9a 7290 /* Emit warnings as needed. */
fbc315db
ILT
7291 if (EXPR_HAS_LOCATION (cs->switch_expr))
7292 switch_location = EXPR_LOCATION (cs->switch_expr);
7293 else
7294 switch_location = input_location;
7295 c_do_switch_warnings (cs->cases, switch_location,
7296 TREE_TYPE (cs->switch_expr),
7297 SWITCH_COND (cs->switch_expr));
6de9cd9a 7298
3e4093b6 7299 /* Pop the stack. */
506e2710 7300 c_switch_stack = cs->next;
3e4093b6 7301 splay_tree_delete (cs->cases);
5d038c4c 7302 XDELETE (cs);
de520661 7303}
325c3691 7304\f
506e2710
RH
7305/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7306 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7307 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7308 statement, and was not surrounded with parenthesis. */
325c3691 7309
9e51cf9d 7310void
506e2710
RH
7311c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7312 tree else_block, bool nested_if)
325c3691 7313{
506e2710 7314 tree stmt;
325c3691 7315
506e2710
RH
7316 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7317 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 7318 {
506e2710 7319 tree inner_if = then_block;
16865eaa 7320
61ada8ae 7321 /* We know from the grammar productions that there is an IF nested
506e2710
RH
7322 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7323 it might not be exactly THEN_BLOCK, but should be the last
7324 non-container statement within. */
7325 while (1)
7326 switch (TREE_CODE (inner_if))
7327 {
7328 case COND_EXPR:
7329 goto found;
7330 case BIND_EXPR:
7331 inner_if = BIND_EXPR_BODY (inner_if);
7332 break;
7333 case STATEMENT_LIST:
7334 inner_if = expr_last (then_block);
7335 break;
7336 case TRY_FINALLY_EXPR:
7337 case TRY_CATCH_EXPR:
7338 inner_if = TREE_OPERAND (inner_if, 0);
7339 break;
7340 default:
366de0ce 7341 gcc_unreachable ();
506e2710
RH
7342 }
7343 found:
16865eaa 7344
506e2710 7345 if (COND_EXPR_ELSE (inner_if))
c303630a
DD
7346 warning (OPT_Wparentheses,
7347 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
506e2710
RH
7348 &if_locus);
7349 }
16865eaa 7350
62e00e94 7351 empty_if_body_warning (then_block, else_block);
325c3691 7352
2214de30 7353 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 7354 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 7355 add_stmt (stmt);
325c3691
RH
7356}
7357
506e2710
RH
7358/* Emit a general-purpose loop construct. START_LOCUS is the location of
7359 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7360 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 7361 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 7362 the continue label. Everything is allowed to be NULL. */
325c3691
RH
7363
7364void
506e2710
RH
7365c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7366 tree blab, tree clab, bool cond_is_first)
325c3691 7367{
506e2710
RH
7368 tree entry = NULL, exit = NULL, t;
7369
28af952a
RS
7370 /* If the condition is zero don't generate a loop construct. */
7371 if (cond && integer_zerop (cond))
7372 {
7373 if (cond_is_first)
7374 {
7375 t = build_and_jump (&blab);
7376 SET_EXPR_LOCATION (t, start_locus);
7377 add_stmt (t);
7378 }
7379 }
7380 else
506e2710
RH
7381 {
7382 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 7383
506e2710 7384 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
7385 out of the loop, or to the top of it. If there's no exit condition,
7386 then we just build a jump back to the top. */
506e2710 7387 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 7388
28af952a 7389 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
7390 {
7391 /* Canonicalize the loop condition to the end. This means
7392 generating a branch to the loop condition. Reuse the
7393 continue label, if possible. */
7394 if (cond_is_first)
7395 {
7396 if (incr || !clab)
7397 {
7398 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7399 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7400 }
7401 else
7402 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 7403 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
7404 add_stmt (t);
7405 }
7406
506e2710 7407 t = build_and_jump (&blab);
c22cacf3 7408 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
506e2710 7409 if (cond_is_first)
c22cacf3 7410 SET_EXPR_LOCATION (exit, start_locus);
506e2710 7411 else
c22cacf3
MS
7412 SET_EXPR_LOCATION (exit, input_location);
7413 }
7414
506e2710
RH
7415 add_stmt (top);
7416 }
c22cacf3 7417
506e2710
RH
7418 if (body)
7419 add_stmt (body);
7420 if (clab)
7421 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7422 if (incr)
7423 add_stmt (incr);
7424 if (entry)
7425 add_stmt (entry);
7426 if (exit)
7427 add_stmt (exit);
7428 if (blab)
7429 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 7430}
325c3691
RH
7431
7432tree
506e2710 7433c_finish_bc_stmt (tree *label_p, bool is_break)
325c3691 7434{
089efaa4 7435 bool skip;
506e2710 7436 tree label = *label_p;
325c3691 7437
089efaa4
ILT
7438 /* In switch statements break is sometimes stylistically used after
7439 a return statement. This can lead to spurious warnings about
7440 control reaching the end of a non-void function when it is
7441 inlined. Note that we are calling block_may_fallthru with
7442 language specific tree nodes; this works because
7443 block_may_fallthru returns true when given something it does not
7444 understand. */
7445 skip = !block_may_fallthru (cur_stmt_list);
7446
506e2710 7447 if (!label)
089efaa4
ILT
7448 {
7449 if (!skip)
7450 *label_p = label = create_artificial_label ();
7451 }
953ff289
DN
7452 else if (TREE_CODE (label) == LABEL_DECL)
7453 ;
7454 else switch (TREE_INT_CST_LOW (label))
506e2710 7455 {
953ff289 7456 case 0:
506e2710
RH
7457 if (is_break)
7458 error ("break statement not within loop or switch");
7459 else
c22cacf3 7460 error ("continue statement not within a loop");
506e2710 7461 return NULL_TREE;
953ff289
DN
7462
7463 case 1:
7464 gcc_assert (is_break);
7465 error ("break statement used with OpenMP for loop");
7466 return NULL_TREE;
7467
7468 default:
7469 gcc_unreachable ();
506e2710 7470 }
325c3691 7471
089efaa4
ILT
7472 if (skip)
7473 return NULL_TREE;
7474
53fb4de3 7475 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
7476}
7477
506e2710 7478/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
7479
7480static void
7481emit_side_effect_warnings (tree expr)
7482{
e6b5a630
RH
7483 if (expr == error_mark_node)
7484 ;
7485 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
7486 {
7487 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
27f33b15 7488 warning (OPT_Wunused_value, "%Hstatement with no effect",
607bdeaa 7489 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
3a5b9284 7490 }
27f33b15 7491 else
3a5b9284
RH
7492 warn_if_unused_value (expr, input_location);
7493}
7494
506e2710
RH
7495/* Process an expression as if it were a complete statement. Emit
7496 diagnostics, but do not call ADD_STMT. */
3a5b9284 7497
506e2710
RH
7498tree
7499c_process_expr_stmt (tree expr)
3a5b9284
RH
7500{
7501 if (!expr)
506e2710 7502 return NULL_TREE;
3a5b9284 7503
3a5b9284
RH
7504 if (warn_sequence_point)
7505 verify_sequence_points (expr);
7506
7507 if (TREE_TYPE (expr) != error_mark_node
7508 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7509 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7510 error ("expression statement has incomplete type");
7511
7512 /* If we're not processing a statement expression, warn about unused values.
7513 Warnings for statement expressions will be emitted later, once we figure
7514 out which is the result. */
7515 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 7516 && warn_unused_value)
3a5b9284
RH
7517 emit_side_effect_warnings (expr);
7518
7519 /* If the expression is not of a type to which we cannot assign a line
7520 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 7521 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
3a5b9284
RH
7522 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7523
07beea0d 7524 if (CAN_HAVE_LOCATION_P (expr))
a281759f 7525 SET_EXPR_LOCATION (expr, input_location);
506e2710
RH
7526
7527 return expr;
7528}
7529
7530/* Emit an expression as a statement. */
7531
7532tree
7533c_finish_expr_stmt (tree expr)
7534{
7535 if (expr)
7536 return add_stmt (c_process_expr_stmt (expr));
7537 else
7538 return NULL;
3a5b9284
RH
7539}
7540
7541/* Do the opposite and emit a statement as an expression. To begin,
7542 create a new binding level and return it. */
325c3691
RH
7543
7544tree
7545c_begin_stmt_expr (void)
7546{
7547 tree ret;
187230a7 7548 struct c_label_context_se *nstack;
16ef3acc 7549 struct c_label_list *glist;
325c3691
RH
7550
7551 /* We must force a BLOCK for this level so that, if it is not expanded
7552 later, there is a way to turn off the entire subtree of blocks that
7553 are contained in it. */
7554 keep_next_level ();
7555 ret = c_begin_compound_stmt (true);
16ef3acc
JM
7556 if (c_switch_stack)
7557 {
7558 c_switch_stack->blocked_stmt_expr++;
7559 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7560 }
187230a7 7561 for (glist = label_context_stack_se->labels_used;
16ef3acc
JM
7562 glist != NULL;
7563 glist = glist->next)
7564 {
7565 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7566 }
187230a7 7567 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
16ef3acc
JM
7568 nstack->labels_def = NULL;
7569 nstack->labels_used = NULL;
187230a7
JM
7570 nstack->next = label_context_stack_se;
7571 label_context_stack_se = nstack;
325c3691
RH
7572
7573 /* Mark the current statement list as belonging to a statement list. */
7574 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7575
7576 return ret;
7577}
7578
7579tree
7580c_finish_stmt_expr (tree body)
7581{
3a5b9284 7582 tree last, type, tmp, val;
325c3691 7583 tree *last_p;
16ef3acc 7584 struct c_label_list *dlist, *glist, *glist_prev = NULL;
325c3691
RH
7585
7586 body = c_end_compound_stmt (body, true);
16ef3acc
JM
7587 if (c_switch_stack)
7588 {
7589 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7590 c_switch_stack->blocked_stmt_expr--;
7591 }
7592 /* It is no longer possible to jump to labels defined within this
7593 statement expression. */
187230a7 7594 for (dlist = label_context_stack_se->labels_def;
16ef3acc
JM
7595 dlist != NULL;
7596 dlist = dlist->next)
7597 {
7598 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7599 }
7600 /* It is again possible to define labels with a goto just outside
7601 this statement expression. */
187230a7 7602 for (glist = label_context_stack_se->next->labels_used;
16ef3acc
JM
7603 glist != NULL;
7604 glist = glist->next)
7605 {
7606 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7607 glist_prev = glist;
7608 }
7609 if (glist_prev != NULL)
187230a7 7610 glist_prev->next = label_context_stack_se->labels_used;
16ef3acc 7611 else
187230a7
JM
7612 label_context_stack_se->next->labels_used
7613 = label_context_stack_se->labels_used;
7614 label_context_stack_se = label_context_stack_se->next;
325c3691 7615
3a5b9284
RH
7616 /* Locate the last statement in BODY. See c_end_compound_stmt
7617 about always returning a BIND_EXPR. */
7618 last_p = &BIND_EXPR_BODY (body);
7619 last = BIND_EXPR_BODY (body);
7620
7621 continue_searching:
325c3691
RH
7622 if (TREE_CODE (last) == STATEMENT_LIST)
7623 {
3a5b9284
RH
7624 tree_stmt_iterator i;
7625
7626 /* This can happen with degenerate cases like ({ }). No value. */
7627 if (!TREE_SIDE_EFFECTS (last))
7628 return body;
7629
7630 /* If we're supposed to generate side effects warnings, process
7631 all of the statements except the last. */
27f33b15 7632 if (warn_unused_value)
325c3691 7633 {
3a5b9284
RH
7634 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7635 emit_side_effect_warnings (tsi_stmt (i));
325c3691
RH
7636 }
7637 else
3a5b9284
RH
7638 i = tsi_last (last);
7639 last_p = tsi_stmt_ptr (i);
7640 last = *last_p;
325c3691
RH
7641 }
7642
3a5b9284
RH
7643 /* If the end of the list is exception related, then the list was split
7644 by a call to push_cleanup. Continue searching. */
7645 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7646 || TREE_CODE (last) == TRY_CATCH_EXPR)
7647 {
7648 last_p = &TREE_OPERAND (last, 0);
7649 last = *last_p;
7650 goto continue_searching;
7651 }
7652
7653 /* In the case that the BIND_EXPR is not necessary, return the
7654 expression out from inside it. */
e6b5a630
RH
7655 if (last == error_mark_node
7656 || (last == BIND_EXPR_BODY (body)
7657 && BIND_EXPR_VARS (body) == NULL))
591baeb0
JM
7658 {
7659 /* Do not warn if the return value of a statement expression is
7660 unused. */
07beea0d 7661 if (CAN_HAVE_LOCATION_P (last))
591baeb0
JM
7662 TREE_NO_WARNING (last) = 1;
7663 return last;
7664 }
325c3691
RH
7665
7666 /* Extract the type of said expression. */
7667 type = TREE_TYPE (last);
325c3691 7668
3a5b9284
RH
7669 /* If we're not returning a value at all, then the BIND_EXPR that
7670 we already have is a fine expression to return. */
7671 if (!type || VOID_TYPE_P (type))
7672 return body;
7673
7674 /* Now that we've located the expression containing the value, it seems
7675 silly to make voidify_wrapper_expr repeat the process. Create a
7676 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7677 tmp = create_tmp_var_raw (type, NULL);
7678
7679 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7680 tree_expr_nonnegative_p giving up immediately. */
7681 val = last;
7682 if (TREE_CODE (val) == NOP_EXPR
7683 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7684 val = TREE_OPERAND (val, 0);
7685
53fb4de3 7686 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
3a5b9284
RH
7687 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7688
53fb4de3 7689 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
325c3691 7690}
187230a7
JM
7691
7692/* Begin the scope of an identifier of variably modified type, scope
7693 number SCOPE. Jumping from outside this scope to inside it is not
7694 permitted. */
7695
7696void
7697c_begin_vm_scope (unsigned int scope)
7698{
7699 struct c_label_context_vm *nstack;
7700 struct c_label_list *glist;
7701
7702 gcc_assert (scope > 0);
71d0c100
MS
7703
7704 /* At file_scope, we don't have to do any processing. */
7705 if (label_context_stack_vm == NULL)
7706 return;
7707
187230a7
JM
7708 if (c_switch_stack && !c_switch_stack->blocked_vm)
7709 c_switch_stack->blocked_vm = scope;
7710 for (glist = label_context_stack_vm->labels_used;
7711 glist != NULL;
7712 glist = glist->next)
7713 {
7714 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7715 }
7716 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7717 nstack->labels_def = NULL;
7718 nstack->labels_used = NULL;
7719 nstack->scope = scope;
7720 nstack->next = label_context_stack_vm;
7721 label_context_stack_vm = nstack;
7722}
7723
7724/* End a scope which may contain identifiers of variably modified
7725 type, scope number SCOPE. */
7726
7727void
7728c_end_vm_scope (unsigned int scope)
7729{
7730 if (label_context_stack_vm == NULL)
7731 return;
7732 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7733 c_switch_stack->blocked_vm = 0;
7734 /* We may have a number of nested scopes of identifiers with
7735 variably modified type, all at this depth. Pop each in turn. */
7736 while (label_context_stack_vm->scope == scope)
7737 {
7738 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7739
7740 /* It is no longer possible to jump to labels defined within this
7741 scope. */
7742 for (dlist = label_context_stack_vm->labels_def;
7743 dlist != NULL;
7744 dlist = dlist->next)
7745 {
7746 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7747 }
7748 /* It is again possible to define labels with a goto just outside
7749 this scope. */
7750 for (glist = label_context_stack_vm->next->labels_used;
7751 glist != NULL;
7752 glist = glist->next)
7753 {
7754 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7755 glist_prev = glist;
7756 }
7757 if (glist_prev != NULL)
7758 glist_prev->next = label_context_stack_vm->labels_used;
7759 else
7760 label_context_stack_vm->next->labels_used
7761 = label_context_stack_vm->labels_used;
7762 label_context_stack_vm = label_context_stack_vm->next;
7763 }
7764}
325c3691
RH
7765\f
7766/* Begin and end compound statements. This is as simple as pushing
7767 and popping new statement lists from the tree. */
7768
7769tree
7770c_begin_compound_stmt (bool do_scope)
7771{
7772 tree stmt = push_stmt_list ();
7773 if (do_scope)
4dfa0342 7774 push_scope ();
325c3691
RH
7775 return stmt;
7776}
7777
7778tree
7779c_end_compound_stmt (tree stmt, bool do_scope)
7780{
7781 tree block = NULL;
7782
7783 if (do_scope)
7784 {
7785 if (c_dialect_objc ())
7786 objc_clear_super_receiver ();
7787 block = pop_scope ();
7788 }
7789
7790 stmt = pop_stmt_list (stmt);
7791 stmt = c_build_bind_expr (block, stmt);
7792
7793 /* If this compound statement is nested immediately inside a statement
7794 expression, then force a BIND_EXPR to be created. Otherwise we'll
7795 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7796 STATEMENT_LISTs merge, and thus we can lose track of what statement
7797 was really last. */
7798 if (cur_stmt_list
7799 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7800 && TREE_CODE (stmt) != BIND_EXPR)
7801 {
53fb4de3 7802 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691
RH
7803 TREE_SIDE_EFFECTS (stmt) = 1;
7804 }
7805
7806 return stmt;
7807}
5a508662
RH
7808
7809/* Queue a cleanup. CLEANUP is an expression/statement to be executed
7810 when the current scope is exited. EH_ONLY is true when this is not
7811 meant to apply to normal control flow transfer. */
7812
7813void
e18476eb 7814push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
5a508662 7815{
3a5b9284
RH
7816 enum tree_code code;
7817 tree stmt, list;
7818 bool stmt_expr;
7819
7820 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7821 stmt = build_stmt (code, NULL, cleanup);
5a508662 7822 add_stmt (stmt);
3a5b9284
RH
7823 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7824 list = push_stmt_list ();
7825 TREE_OPERAND (stmt, 0) = list;
7826 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 7827}
325c3691 7828\f
3e4093b6
RS
7829/* Build a binary-operation expression without default conversions.
7830 CODE is the kind of expression to build.
7831 This function differs from `build' in several ways:
7832 the data type of the result is computed and recorded in it,
7833 warnings are generated if arg data types are invalid,
7834 special handling for addition and subtraction of pointers is known,
7835 and some optimization is done (operations on narrow ints
7836 are done in the narrower type when that gives the same result).
7837 Constant folding is also done before the result is returned.
de520661 7838
3e4093b6
RS
7839 Note that the operands will never have enumeral types, or function
7840 or array types, because either they will have the default conversions
7841 performed or they have both just been converted to some other type in which
7842 the arithmetic is to be done. */
7843
7844tree
7845build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7846 int convert_p)
de520661 7847{
3e4093b6
RS
7848 tree type0, type1;
7849 enum tree_code code0, code1;
7850 tree op0, op1;
4de67c26 7851 const char *invalid_op_diag;
b62acd60 7852
3e4093b6
RS
7853 /* Expression code to give to the expression when it is built.
7854 Normally this is CODE, which is what the caller asked for,
7855 but in some special cases we change it. */
7856 enum tree_code resultcode = code;
8b6a5902 7857
3e4093b6
RS
7858 /* Data type in which the computation is to be performed.
7859 In the simplest cases this is the common type of the arguments. */
7860 tree result_type = NULL;
7861
7862 /* Nonzero means operands have already been type-converted
7863 in whatever way is necessary.
7864 Zero means they need to be converted to RESULT_TYPE. */
7865 int converted = 0;
7866
7867 /* Nonzero means create the expression with this type, rather than
7868 RESULT_TYPE. */
7869 tree build_type = 0;
7870
7871 /* Nonzero means after finally constructing the expression
7872 convert it to this type. */
7873 tree final_type = 0;
7874
7875 /* Nonzero if this is an operation like MIN or MAX which can
7876 safely be computed in short if both args are promoted shorts.
7877 Also implies COMMON.
7878 -1 indicates a bitwise operation; this makes a difference
7879 in the exact conditions for when it is safe to do the operation
7880 in a narrower mode. */
7881 int shorten = 0;
7882
7883 /* Nonzero if this is a comparison operation;
7884 if both args are promoted shorts, compare the original shorts.
7885 Also implies COMMON. */
7886 int short_compare = 0;
7887
7888 /* Nonzero if this is a right-shift operation, which can be computed on the
7889 original short and then promoted if the operand is a promoted short. */
7890 int short_shift = 0;
7891
7892 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7893 int common = 0;
7894
58393038
ZL
7895 /* True means types are compatible as far as ObjC is concerned. */
7896 bool objc_ok;
7897
3e4093b6 7898 if (convert_p)
790e9490 7899 {
3e4093b6
RS
7900 op0 = default_conversion (orig_op0);
7901 op1 = default_conversion (orig_op1);
790e9490 7902 }
3e4093b6 7903 else
790e9490 7904 {
3e4093b6
RS
7905 op0 = orig_op0;
7906 op1 = orig_op1;
790e9490
RS
7907 }
7908
3e4093b6
RS
7909 type0 = TREE_TYPE (op0);
7910 type1 = TREE_TYPE (op1);
91fa3c30 7911
3e4093b6
RS
7912 /* The expression codes of the data types of the arguments tell us
7913 whether the arguments are integers, floating, pointers, etc. */
7914 code0 = TREE_CODE (type0);
7915 code1 = TREE_CODE (type1);
7916
7917 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7918 STRIP_TYPE_NOPS (op0);
7919 STRIP_TYPE_NOPS (op1);
7920
7921 /* If an error was already reported for one of the arguments,
7922 avoid reporting another error. */
7923
7924 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7925 return error_mark_node;
7926
4de67c26
JM
7927 if ((invalid_op_diag
7928 = targetm.invalid_binary_op (code, type0, type1)))
7929 {
7930 error (invalid_op_diag);
7931 return error_mark_node;
7932 }
7933
58393038
ZL
7934 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7935
3e4093b6 7936 switch (code)
de520661 7937 {
3e4093b6
RS
7938 case PLUS_EXPR:
7939 /* Handle the pointer + int case. */
7940 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7941 return pointer_int_sum (PLUS_EXPR, op0, op1);
7942 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7943 return pointer_int_sum (PLUS_EXPR, op1, op0);
fe67cf58 7944 else
3e4093b6
RS
7945 common = 1;
7946 break;
400fbf9f 7947
3e4093b6
RS
7948 case MINUS_EXPR:
7949 /* Subtraction of two similar pointers.
7950 We must subtract them as integers, then divide by object size. */
7951 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
58393038 7952 && comp_target_types (type0, type1))
3e4093b6
RS
7953 return pointer_diff (op0, op1);
7954 /* Handle pointer minus int. Just like pointer plus int. */
7955 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7956 return pointer_int_sum (MINUS_EXPR, op0, op1);
7957 else
7958 common = 1;
7959 break;
8b6a5902 7960
3e4093b6
RS
7961 case MULT_EXPR:
7962 common = 1;
7963 break;
7964
7965 case TRUNC_DIV_EXPR:
7966 case CEIL_DIV_EXPR:
7967 case FLOOR_DIV_EXPR:
7968 case ROUND_DIV_EXPR:
7969 case EXACT_DIV_EXPR:
2e9cb75e 7970 warn_for_div_by_zero (op1);
3e4093b6
RS
7971
7972 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 7973 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
7974 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7975 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 7976 || code1 == FIXED_POINT_TYPE
3e4093b6 7977 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 7978 {
5bed876a
AH
7979 enum tree_code tcode0 = code0, tcode1 = code1;
7980
3a021db2 7981 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 7982 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 7983 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 7984 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 7985
ab22c1fa
CF
7986 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
7987 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
7988 resultcode = RDIV_EXPR;
7989 else
7990 /* Although it would be tempting to shorten always here, that
7991 loses on some targets, since the modulo instruction is
7992 undefined if the quotient can't be represented in the
7993 computation mode. We shorten only if unsigned or if
7994 dividing by something we know != -1. */
8df83eae 7995 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 7996 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 7997 && !integer_all_onesp (op1)));
3e4093b6
RS
7998 common = 1;
7999 }
8000 break;
de520661 8001
3e4093b6 8002 case BIT_AND_EXPR:
3e4093b6
RS
8003 case BIT_IOR_EXPR:
8004 case BIT_XOR_EXPR:
8005 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8006 shorten = -1;
9ef0c8d9
AP
8007 /* Allow vector types which are not floating point types. */
8008 else if (code0 == VECTOR_TYPE
8009 && code1 == VECTOR_TYPE
8010 && !VECTOR_FLOAT_TYPE_P (type0)
8011 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
8012 common = 1;
8013 break;
8014
8015 case TRUNC_MOD_EXPR:
8016 case FLOOR_MOD_EXPR:
2e9cb75e 8017 warn_for_div_by_zero (op1);
de520661 8018
3e4093b6
RS
8019 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8020 {
8021 /* Although it would be tempting to shorten always here, that loses
8022 on some targets, since the modulo instruction is undefined if the
8023 quotient can't be represented in the computation mode. We shorten
8024 only if unsigned or if dividing by something we know != -1. */
8df83eae 8025 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 8026 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 8027 && !integer_all_onesp (op1)));
3e4093b6
RS
8028 common = 1;
8029 }
8030 break;
de520661 8031
3e4093b6
RS
8032 case TRUTH_ANDIF_EXPR:
8033 case TRUTH_ORIF_EXPR:
8034 case TRUTH_AND_EXPR:
8035 case TRUTH_OR_EXPR:
8036 case TRUTH_XOR_EXPR:
8037 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
8038 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8039 || code0 == FIXED_POINT_TYPE)
3e4093b6 8040 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
8041 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8042 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
8043 {
8044 /* Result of these operations is always an int,
8045 but that does not mean the operands should be
8046 converted to ints! */
8047 result_type = integer_type_node;
85498824
JM
8048 op0 = c_common_truthvalue_conversion (op0);
8049 op1 = c_common_truthvalue_conversion (op1);
3e4093b6
RS
8050 converted = 1;
8051 }
8052 break;
eba80994 8053
3e4093b6
RS
8054 /* Shift operations: result has same type as first operand;
8055 always convert second operand to int.
8056 Also set SHORT_SHIFT if shifting rightward. */
de520661 8057
3e4093b6 8058 case RSHIFT_EXPR:
ab22c1fa
CF
8059 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8060 && code1 == INTEGER_TYPE)
3e4093b6
RS
8061 {
8062 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
b62acd60 8063 {
3e4093b6 8064 if (tree_int_cst_sgn (op1) < 0)
d4ee4d25 8065 warning (0, "right shift count is negative");
3e4093b6 8066 else
bbb818c6 8067 {
3f75a254 8068 if (!integer_zerop (op1))
3e4093b6
RS
8069 short_shift = 1;
8070
8071 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
d4ee4d25 8072 warning (0, "right shift count >= width of type");
bbb818c6 8073 }
b62acd60 8074 }
de520661 8075
3e4093b6
RS
8076 /* Use the type of the value to be shifted. */
8077 result_type = type0;
8078 /* Convert the shift-count to an integer, regardless of size
8079 of value being shifted. */
8080 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8081 op1 = convert (integer_type_node, op1);
8082 /* Avoid converting op1 to result_type later. */
8083 converted = 1;
400fbf9f 8084 }
3e4093b6 8085 break;
253b6b82 8086
3e4093b6 8087 case LSHIFT_EXPR:
ab22c1fa
CF
8088 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
8089 && code1 == INTEGER_TYPE)
3e4093b6
RS
8090 {
8091 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
de520661 8092 {
3e4093b6 8093 if (tree_int_cst_sgn (op1) < 0)
d4ee4d25 8094 warning (0, "left shift count is negative");
de520661 8095
3e4093b6 8096 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
d4ee4d25 8097 warning (0, "left shift count >= width of type");
94ba5069 8098 }
de520661 8099
3e4093b6
RS
8100 /* Use the type of the value to be shifted. */
8101 result_type = type0;
8102 /* Convert the shift-count to an integer, regardless of size
8103 of value being shifted. */
8104 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8105 op1 = convert (integer_type_node, op1);
8106 /* Avoid converting op1 to result_type later. */
8107 converted = 1;
400fbf9f 8108 }
3e4093b6 8109 break;
de520661 8110
3e4093b6
RS
8111 case EQ_EXPR:
8112 case NE_EXPR:
44c21c7f
DD
8113 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8114 warning (OPT_Wfloat_equal,
8115 "comparing floating point with == or != is unsafe");
3e4093b6
RS
8116 /* Result of comparison is always int,
8117 but don't convert the args to int! */
8118 build_type = integer_type_node;
8119 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 8120 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 8121 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 8122 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6
RS
8123 short_compare = 1;
8124 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8125 {
8126 tree tt0 = TREE_TYPE (type0);
8127 tree tt1 = TREE_TYPE (type1);
8128 /* Anything compares with void *. void * compares with anything.
8129 Otherwise, the targets must be compatible
8130 and both must be object or both incomplete. */
58393038 8131 if (comp_target_types (type0, type1))
10bc1b1b 8132 result_type = common_pointer_type (type0, type1);
3e4093b6 8133 else if (VOID_TYPE_P (tt0))
ee2990e7 8134 {
3e4093b6
RS
8135 /* op0 != orig_op0 detects the case of something
8136 whose value is 0 but which isn't a valid null ptr const. */
6aa3c60d 8137 if (pedantic && !null_pointer_constant_p (orig_op0)
3e4093b6 8138 && TREE_CODE (tt1) == FUNCTION_TYPE)
bda67431
JM
8139 pedwarn ("ISO C forbids comparison of %<void *%>"
8140 " with function pointer");
ee2990e7 8141 }
3e4093b6 8142 else if (VOID_TYPE_P (tt1))
e6834654 8143 {
6aa3c60d 8144 if (pedantic && !null_pointer_constant_p (orig_op1)
3e4093b6 8145 && TREE_CODE (tt0) == FUNCTION_TYPE)
bda67431
JM
8146 pedwarn ("ISO C forbids comparison of %<void *%>"
8147 " with function pointer");
e6834654 8148 }
3e4093b6 8149 else
58393038
ZL
8150 /* Avoid warning about the volatile ObjC EH puts on decls. */
8151 if (!objc_ok)
8152 pedwarn ("comparison of distinct pointer types lacks a cast");
e6834654 8153
3e4093b6
RS
8154 if (result_type == NULL_TREE)
8155 result_type = ptr_type_node;
e6834654 8156 }
6aa3c60d 8157 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
690a704a
BE
8158 {
8159 if (TREE_CODE (op0) == ADDR_EXPR
b3c6d2ea 8160 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
c116cd05 8161 warning (OPT_Waddress, "the address of %qD will never be NULL",
690a704a
BE
8162 TREE_OPERAND (op0, 0));
8163 result_type = type0;
8164 }
6aa3c60d 8165 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
690a704a 8166 {
c22cacf3 8167 if (TREE_CODE (op1) == ADDR_EXPR
b3c6d2ea 8168 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
c116cd05 8169 warning (OPT_Waddress, "the address of %qD will never be NULL",
690a704a
BE
8170 TREE_OPERAND (op1, 0));
8171 result_type = type1;
8172 }
3e4093b6 8173 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 8174 {
3e4093b6
RS
8175 result_type = type0;
8176 pedwarn ("comparison between pointer and integer");
de520661 8177 }
3e4093b6 8178 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 8179 {
3e4093b6
RS
8180 result_type = type1;
8181 pedwarn ("comparison between pointer and integer");
8b6a5902 8182 }
3e4093b6 8183 break;
8b6a5902 8184
3e4093b6
RS
8185 case LE_EXPR:
8186 case GE_EXPR:
8187 case LT_EXPR:
8188 case GT_EXPR:
8189 build_type = integer_type_node;
ab22c1fa
CF
8190 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8191 || code0 == FIXED_POINT_TYPE)
8192 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8193 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
8194 short_compare = 1;
8195 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8196 {
58393038 8197 if (comp_target_types (type0, type1))
3e4093b6 8198 {
10bc1b1b 8199 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
8200 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8201 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8202 pedwarn ("comparison of complete and incomplete pointers");
8203 else if (pedantic
8204 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8205 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8206 }
8207 else
8208 {
8209 result_type = ptr_type_node;
8210 pedwarn ("comparison of distinct pointer types lacks a cast");
8211 }
8212 }
6aa3c60d 8213 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
8214 {
8215 result_type = type0;
8216 if (pedantic || extra_warnings)
8217 pedwarn ("ordered comparison of pointer with integer zero");
8218 }
6aa3c60d 8219 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
8220 {
8221 result_type = type1;
8222 if (pedantic)
8223 pedwarn ("ordered comparison of pointer with integer zero");
8224 }
8225 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8226 {
8227 result_type = type0;
8228 pedwarn ("comparison between pointer and integer");
8229 }
8230 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8231 {
8232 result_type = type1;
8233 pedwarn ("comparison between pointer and integer");
8234 }
8235 break;
64094f6a 8236
3e4093b6 8237 default:
37b2f290 8238 gcc_unreachable ();
c9fe6f9f 8239 }
8f17b5c5 8240
e57e265b
PB
8241 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8242 return error_mark_node;
8243
5bed876a
AH
8244 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8245 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8246 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8247 TREE_TYPE (type1))))
8248 {
2c751309 8249 binary_op_error (code, type0, type1);
5bed876a
AH
8250 return error_mark_node;
8251 }
8252
3e4093b6 8253 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 8254 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
8255 &&
8256 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 8257 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 8258 {
3e4093b6 8259 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
39b726dd 8260
3e4093b6 8261 if (shorten || common || short_compare)
3bf6bfcc
JJ
8262 {
8263 result_type = c_common_type (type0, type1);
8264 if (result_type == error_mark_node)
8265 return error_mark_node;
8266 }
400fbf9f 8267
3e4093b6
RS
8268 /* For certain operations (which identify themselves by shorten != 0)
8269 if both args were extended from the same smaller type,
8270 do the arithmetic in that type and then extend.
400fbf9f 8271
3e4093b6
RS
8272 shorten !=0 and !=1 indicates a bitwise operation.
8273 For them, this optimization is safe only if
8274 both args are zero-extended or both are sign-extended.
8275 Otherwise, we might change the result.
8276 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8277 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 8278
3e4093b6
RS
8279 if (shorten && none_complex)
8280 {
8281 int unsigned0, unsigned1;
e6620e86
KH
8282 tree arg0, arg1;
8283 int uns;
3e4093b6 8284 tree type;
400fbf9f 8285
e6620e86
KH
8286 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8287 excessive narrowing when we call get_narrower below. For
8288 example, suppose that OP0 is of unsigned int extended
8289 from signed char and that RESULT_TYPE is long long int.
8290 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8291 like
8292
8293 (long long int) (unsigned int) signed_char
8294
8295 which get_narrower would narrow down to
8296
8297 (unsigned int) signed char
8298
8299 If we do not cast OP0 first, get_narrower would return
8300 signed_char, which is inconsistent with the case of the
8301 explicit cast. */
8302 op0 = convert (result_type, op0);
8303 op1 = convert (result_type, op1);
8304
8305 arg0 = get_narrower (op0, &unsigned0);
8306 arg1 = get_narrower (op1, &unsigned1);
8307
8308 /* UNS is 1 if the operation to be done is an unsigned one. */
8309 uns = TYPE_UNSIGNED (result_type);
8310
3e4093b6 8311 final_type = result_type;
70768eda 8312
3e4093b6
RS
8313 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8314 but it *requires* conversion to FINAL_TYPE. */
70768eda 8315
3e4093b6
RS
8316 if ((TYPE_PRECISION (TREE_TYPE (op0))
8317 == TYPE_PRECISION (TREE_TYPE (arg0)))
8318 && TREE_TYPE (op0) != final_type)
8df83eae 8319 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3e4093b6
RS
8320 if ((TYPE_PRECISION (TREE_TYPE (op1))
8321 == TYPE_PRECISION (TREE_TYPE (arg1)))
8322 && TREE_TYPE (op1) != final_type)
8df83eae 8323 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
88a3dbc1 8324
3e4093b6 8325 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
abe80e6d 8326
3e4093b6
RS
8327 /* For bitwise operations, signedness of nominal type
8328 does not matter. Consider only how operands were extended. */
8329 if (shorten == -1)
8330 uns = unsigned0;
abe80e6d 8331
3e4093b6
RS
8332 /* Note that in all three cases below we refrain from optimizing
8333 an unsigned operation on sign-extended args.
8334 That would not be valid. */
abe80e6d 8335
3e4093b6
RS
8336 /* Both args variable: if both extended in same way
8337 from same width, do it in that width.
8338 Do it unsigned if args were zero-extended. */
8339 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8340 < TYPE_PRECISION (result_type))
8341 && (TYPE_PRECISION (TREE_TYPE (arg1))
8342 == TYPE_PRECISION (TREE_TYPE (arg0)))
8343 && unsigned0 == unsigned1
8344 && (unsigned0 || !uns))
8345 result_type
8346 = c_common_signed_or_unsigned_type
5922c215 8347 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3e4093b6
RS
8348 else if (TREE_CODE (arg0) == INTEGER_CST
8349 && (unsigned1 || !uns)
8350 && (TYPE_PRECISION (TREE_TYPE (arg1))
8351 < TYPE_PRECISION (result_type))
8352 && (type
8353 = c_common_signed_or_unsigned_type (unsigned1,
eafaa675
AP
8354 TREE_TYPE (arg1)))
8355 && !POINTER_TYPE_P (type)
8356 && int_fits_type_p (arg0, type))
3e4093b6
RS
8357 result_type = type;
8358 else if (TREE_CODE (arg1) == INTEGER_CST
8359 && (unsigned0 || !uns)
8360 && (TYPE_PRECISION (TREE_TYPE (arg0))
8361 < TYPE_PRECISION (result_type))
8362 && (type
8363 = c_common_signed_or_unsigned_type (unsigned0,
eafaa675
AP
8364 TREE_TYPE (arg0)))
8365 && !POINTER_TYPE_P (type)
8366 && int_fits_type_p (arg1, type))
3e4093b6
RS
8367 result_type = type;
8368 }
88a3dbc1 8369
3e4093b6 8370 /* Shifts can be shortened if shifting right. */
2f6e4e97 8371
3e4093b6
RS
8372 if (short_shift)
8373 {
8374 int unsigned_arg;
8375 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 8376
3e4093b6 8377 final_type = result_type;
abe80e6d 8378
3e4093b6 8379 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 8380 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 8381
3e4093b6
RS
8382 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8383 /* We can shorten only if the shift count is less than the
8384 number of bits in the smaller type size. */
8385 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8386 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 8387 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
8388 {
8389 /* Do an unsigned shift if the operand was zero-extended. */
8390 result_type
8391 = c_common_signed_or_unsigned_type (unsigned_arg,
8392 TREE_TYPE (arg0));
8393 /* Convert value-to-be-shifted to that type. */
8394 if (TREE_TYPE (op0) != result_type)
8395 op0 = convert (result_type, op0);
8396 converted = 1;
abe80e6d 8397 }
88a3dbc1
RK
8398 }
8399
3e4093b6
RS
8400 /* Comparison operations are shortened too but differently.
8401 They identify themselves by setting short_compare = 1. */
56cb9733 8402
3e4093b6
RS
8403 if (short_compare)
8404 {
8405 /* Don't write &op0, etc., because that would prevent op0
8406 from being kept in a register.
8407 Instead, make copies of the our local variables and
8408 pass the copies by reference, then copy them back afterward. */
8409 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8410 enum tree_code xresultcode = resultcode;
8411 tree val
8412 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 8413
3e4093b6
RS
8414 if (val != 0)
8415 return val;
8f17b5c5 8416
3e4093b6
RS
8417 op0 = xop0, op1 = xop1;
8418 converted = 1;
8419 resultcode = xresultcode;
8f17b5c5 8420
3e4093b6
RS
8421 if (warn_sign_compare && skip_evaluation == 0)
8422 {
3f75a254
JM
8423 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8424 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3e4093b6
RS
8425 int unsignedp0, unsignedp1;
8426 tree primop0 = get_narrower (op0, &unsignedp0);
8427 tree primop1 = get_narrower (op1, &unsignedp1);
400fbf9f 8428
3e4093b6
RS
8429 xop0 = orig_op0;
8430 xop1 = orig_op1;
8431 STRIP_TYPE_NOPS (xop0);
8432 STRIP_TYPE_NOPS (xop1);
e89a9554 8433
3e4093b6
RS
8434 /* Give warnings for comparisons between signed and unsigned
8435 quantities that may fail.
e89a9554 8436
3e4093b6
RS
8437 Do the checking based on the original operand trees, so that
8438 casts will be considered, but default promotions won't be.
400fbf9f 8439
3e4093b6
RS
8440 Do not warn if the comparison is being done in a signed type,
8441 since the signed type will only be chosen if it can represent
8442 all the values of the unsigned type. */
3f75a254 8443 if (!TYPE_UNSIGNED (result_type))
3e4093b6 8444 /* OK */;
c22cacf3
MS
8445 /* Do not warn if both operands are the same signedness. */
8446 else if (op0_signed == op1_signed)
8447 /* OK */;
3e4093b6
RS
8448 else
8449 {
8450 tree sop, uop;
6ac01510 8451 bool ovf;
8f17b5c5 8452
3e4093b6
RS
8453 if (op0_signed)
8454 sop = xop0, uop = xop1;
8455 else
8456 sop = xop1, uop = xop0;
8f17b5c5 8457
3e4093b6
RS
8458 /* Do not warn if the signed quantity is an
8459 unsuffixed integer literal (or some static
8460 constant expression involving such literals or a
8461 conditional expression involving such literals)
8462 and it is non-negative. */
6ac01510 8463 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
3e4093b6
RS
8464 /* OK */;
8465 /* Do not warn if the comparison is an equality operation,
8466 the unsigned quantity is an integral constant, and it
8467 would fit in the result if the result were signed. */
8468 else if (TREE_CODE (uop) == INTEGER_CST
8469 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8470 && int_fits_type_p
8471 (uop, c_common_signed_type (result_type)))
8472 /* OK */;
8473 /* Do not warn if the unsigned quantity is an enumeration
8474 constant and its maximum value would fit in the result
8475 if the result were signed. */
8476 else if (TREE_CODE (uop) == INTEGER_CST
8477 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8478 && int_fits_type_p
3f75a254 8479 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
3e4093b6
RS
8480 c_common_signed_type (result_type)))
8481 /* OK */;
8482 else
d4ee4d25 8483 warning (0, "comparison between signed and unsigned");
3e4093b6 8484 }
8f17b5c5 8485
3e4093b6
RS
8486 /* Warn if two unsigned values are being compared in a size
8487 larger than their original size, and one (and only one) is the
8488 result of a `~' operator. This comparison will always fail.
8f17b5c5 8489
3e4093b6
RS
8490 Also warn if one operand is a constant, and the constant
8491 does not have all bits set that are set in the ~ operand
8492 when it is extended. */
8f17b5c5 8493
3e4093b6
RS
8494 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8495 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8496 {
8497 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8498 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8499 &unsignedp0);
8500 else
8501 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8502 &unsignedp1);
64094f6a 8503
3e4093b6
RS
8504 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8505 {
8506 tree primop;
8507 HOST_WIDE_INT constant, mask;
8508 int unsignedp, bits;
2ad1815d 8509
3e4093b6
RS
8510 if (host_integerp (primop0, 0))
8511 {
8512 primop = primop1;
8513 unsignedp = unsignedp1;
8514 constant = tree_low_cst (primop0, 0);
8515 }
8516 else
8517 {
8518 primop = primop0;
8519 unsignedp = unsignedp0;
8520 constant = tree_low_cst (primop1, 0);
8521 }
8522
8523 bits = TYPE_PRECISION (TREE_TYPE (primop));
8524 if (bits < TYPE_PRECISION (result_type)
8525 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8526 {
3f75a254 8527 mask = (~(HOST_WIDE_INT) 0) << bits;
3e4093b6 8528 if ((mask & constant) != mask)
d4ee4d25 8529 warning (0, "comparison of promoted ~unsigned with constant");
3e4093b6
RS
8530 }
8531 }
8532 else if (unsignedp0 && unsignedp1
8533 && (TYPE_PRECISION (TREE_TYPE (primop0))
8534 < TYPE_PRECISION (result_type))
8535 && (TYPE_PRECISION (TREE_TYPE (primop1))
8536 < TYPE_PRECISION (result_type)))
d4ee4d25 8537 warning (0, "comparison of promoted ~unsigned with unsigned");
3e4093b6
RS
8538 }
8539 }
2ad1815d 8540 }
64094f6a 8541 }
64094f6a 8542
3e4093b6
RS
8543 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8544 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8545 Then the expression will be built.
8546 It will be given type FINAL_TYPE if that is nonzero;
8547 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 8548
3e4093b6
RS
8549 if (!result_type)
8550 {
2c751309 8551 binary_op_error (code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
8552 return error_mark_node;
8553 }
400fbf9f 8554
3f75a254 8555 if (!converted)
3e4093b6
RS
8556 {
8557 if (TREE_TYPE (op0) != result_type)
0f57299d 8558 op0 = convert_and_check (result_type, op0);
3e4093b6 8559 if (TREE_TYPE (op1) != result_type)
0f57299d 8560 op1 = convert_and_check (result_type, op1);
d97c6333
JW
8561
8562 /* This can happen if one operand has a vector type, and the other
8563 has a different type. */
8564 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8565 return error_mark_node;
3e4093b6 8566 }
400fbf9f 8567
3e4093b6
RS
8568 if (build_type == NULL_TREE)
8569 build_type = result_type;
400fbf9f 8570
3e4093b6 8571 {
3e4093b6 8572 /* Treat expressions in initializers specially as they can't trap. */
00d1b1d6
JM
8573 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8574 build_type,
8575 op0, op1)
8576 : fold_build2 (resultcode, build_type,
8577 op0, op1);
6de9cd9a 8578
3e4093b6 8579 if (final_type != 0)
6de9cd9a
DN
8580 result = convert (final_type, result);
8581 return result;
3e4093b6 8582 }
400fbf9f 8583}
85498824
JM
8584
8585
8586/* Convert EXPR to be a truth-value, validating its type for this
46bdb9cf 8587 purpose. */
85498824
JM
8588
8589tree
8590c_objc_common_truthvalue_conversion (tree expr)
8591{
85498824
JM
8592 switch (TREE_CODE (TREE_TYPE (expr)))
8593 {
8594 case ARRAY_TYPE:
8595 error ("used array that cannot be converted to pointer where scalar is required");
8596 return error_mark_node;
8597
8598 case RECORD_TYPE:
8599 error ("used struct type value where scalar is required");
8600 return error_mark_node;
8601
8602 case UNION_TYPE:
8603 error ("used union type value where scalar is required");
8604 return error_mark_node;
8605
46bdb9cf
JM
8606 case FUNCTION_TYPE:
8607 gcc_unreachable ();
8608
85498824
JM
8609 default:
8610 break;
8611 }
8612
8613 /* ??? Should we also give an error for void and vectors rather than
8614 leaving those to give errors later? */
8615 return c_common_truthvalue_conversion (expr);
8616}
73f397d4
JM
8617\f
8618
8619/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8620 required. */
8621
8622tree
8623c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8624 bool *ti ATTRIBUTE_UNUSED, bool *se)
8625{
8626 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8627 {
8628 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8629 /* Executing a compound literal inside a function reinitializes
8630 it. */
8631 if (!TREE_STATIC (decl))
8632 *se = true;
8633 return decl;
8634 }
8635 else
8636 return expr;
8637}
953ff289 8638\f
c0220ea4 8639/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
8640
8641tree
8642c_begin_omp_parallel (void)
8643{
8644 tree block;
8645
8646 keep_next_level ();
8647 block = c_begin_compound_stmt (true);
8648
8649 return block;
8650}
8651
8652tree
8653c_finish_omp_parallel (tree clauses, tree block)
8654{
8655 tree stmt;
8656
8657 block = c_end_compound_stmt (block, true);
8658
8659 stmt = make_node (OMP_PARALLEL);
8660 TREE_TYPE (stmt) = void_type_node;
8661 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8662 OMP_PARALLEL_BODY (stmt) = block;
8663
8664 return add_stmt (stmt);
8665}
8666
8667/* For all elements of CLAUSES, validate them vs OpenMP constraints.
8668 Remove any elements from the list that are invalid. */
8669
8670tree
8671c_finish_omp_clauses (tree clauses)
8672{
8673 bitmap_head generic_head, firstprivate_head, lastprivate_head;
8674 tree c, t, *pc = &clauses;
8675 const char *name;
8676
8677 bitmap_obstack_initialize (NULL);
8678 bitmap_initialize (&generic_head, &bitmap_default_obstack);
8679 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
8680 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
8681
8682 for (pc = &clauses, c = clauses; c ; c = *pc)
8683 {
8684 bool remove = false;
8685 bool need_complete = false;
8686 bool need_implicitly_determined = false;
8687
aaf46ef9 8688 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
8689 {
8690 case OMP_CLAUSE_SHARED:
8691 name = "shared";
8692 need_implicitly_determined = true;
8693 goto check_dup_generic;
8694
8695 case OMP_CLAUSE_PRIVATE:
8696 name = "private";
8697 need_complete = true;
8698 need_implicitly_determined = true;
8699 goto check_dup_generic;
8700
8701 case OMP_CLAUSE_REDUCTION:
8702 name = "reduction";
8703 need_implicitly_determined = true;
8704 t = OMP_CLAUSE_DECL (c);
8705 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
8706 || POINTER_TYPE_P (TREE_TYPE (t)))
8707 {
8708 error ("%qE has invalid type for %<reduction%>", t);
8709 remove = true;
8710 }
8711 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
8712 {
8713 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
8714 const char *r_name = NULL;
8715
8716 switch (r_code)
8717 {
8718 case PLUS_EXPR:
8719 case MULT_EXPR:
8720 case MINUS_EXPR:
8721 break;
8722 case BIT_AND_EXPR:
8723 r_name = "&";
8724 break;
8725 case BIT_XOR_EXPR:
8726 r_name = "^";
8727 break;
8728 case BIT_IOR_EXPR:
8729 r_name = "|";
8730 break;
8731 case TRUTH_ANDIF_EXPR:
8732 r_name = "&&";
8733 break;
8734 case TRUTH_ORIF_EXPR:
8735 r_name = "||";
8736 break;
8737 default:
8738 gcc_unreachable ();
8739 }
8740 if (r_name)
8741 {
8742 error ("%qE has invalid type for %<reduction(%s)%>",
8743 t, r_name);
8744 remove = true;
8745 }
8746 }
8747 goto check_dup_generic;
8748
8749 case OMP_CLAUSE_COPYPRIVATE:
8750 name = "copyprivate";
8751 goto check_dup_generic;
8752
8753 case OMP_CLAUSE_COPYIN:
8754 name = "copyin";
8755 t = OMP_CLAUSE_DECL (c);
8756 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
8757 {
8758 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
8759 remove = true;
8760 }
8761 goto check_dup_generic;
8762
8763 check_dup_generic:
8764 t = OMP_CLAUSE_DECL (c);
8765 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8766 {
8767 error ("%qE is not a variable in clause %qs", t, name);
8768 remove = true;
8769 }
8770 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8771 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8772 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8773 {
8774 error ("%qE appears more than once in data clauses", t);
8775 remove = true;
8776 }
8777 else
8778 bitmap_set_bit (&generic_head, DECL_UID (t));
8779 break;
8780
8781 case OMP_CLAUSE_FIRSTPRIVATE:
8782 name = "firstprivate";
8783 t = OMP_CLAUSE_DECL (c);
8784 need_complete = true;
8785 need_implicitly_determined = true;
8786 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8787 {
8788 error ("%qE is not a variable in clause %<firstprivate%>", t);
8789 remove = true;
8790 }
8791 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8792 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8793 {
8794 error ("%qE appears more than once in data clauses", t);
8795 remove = true;
8796 }
8797 else
8798 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8799 break;
8800
8801 case OMP_CLAUSE_LASTPRIVATE:
8802 name = "lastprivate";
8803 t = OMP_CLAUSE_DECL (c);
8804 need_complete = true;
8805 need_implicitly_determined = true;
8806 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8807 {
8808 error ("%qE is not a variable in clause %<lastprivate%>", t);
8809 remove = true;
8810 }
8811 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8812 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8813 {
8814 error ("%qE appears more than once in data clauses", t);
8815 remove = true;
8816 }
8817 else
8818 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8819 break;
8820
8821 case OMP_CLAUSE_IF:
8822 case OMP_CLAUSE_NUM_THREADS:
8823 case OMP_CLAUSE_SCHEDULE:
8824 case OMP_CLAUSE_NOWAIT:
8825 case OMP_CLAUSE_ORDERED:
8826 case OMP_CLAUSE_DEFAULT:
8827 pc = &OMP_CLAUSE_CHAIN (c);
8828 continue;
8829
8830 default:
8831 gcc_unreachable ();
8832 }
8833
8834 if (!remove)
8835 {
8836 t = OMP_CLAUSE_DECL (c);
8837
8838 if (need_complete)
8839 {
8840 t = require_complete_type (t);
8841 if (t == error_mark_node)
8842 remove = true;
8843 }
8844
8845 if (need_implicitly_determined)
8846 {
8847 const char *share_name = NULL;
8848
8849 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
8850 share_name = "threadprivate";
8851 else switch (c_omp_predetermined_sharing (t))
8852 {
8853 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8854 break;
8855 case OMP_CLAUSE_DEFAULT_SHARED:
8856 share_name = "shared";
8857 break;
8858 case OMP_CLAUSE_DEFAULT_PRIVATE:
8859 share_name = "private";
8860 break;
8861 default:
8862 gcc_unreachable ();
8863 }
8864 if (share_name)
8865 {
8866 error ("%qE is predetermined %qs for %qs",
8867 t, share_name, name);
8868 remove = true;
8869 }
8870 }
8871 }
8872
8873 if (remove)
8874 *pc = OMP_CLAUSE_CHAIN (c);
8875 else
8876 pc = &OMP_CLAUSE_CHAIN (c);
8877 }
8878
8879 bitmap_obstack_release (NULL);
8880 return clauses;
8881}