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