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