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