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