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