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