1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23
24 /* This file is part of the C front end.
25 It contains routines to build C expressions given their operands,
26 including computing the types of the result, C-specific error checks,
27 and some optimization. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "langhooks.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 #include "tree-iterator.h"
46 #include "tree-gimple.h"
47 #include "tree-flow.h"
48
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
51 enum impl_conv {
52 ic_argpass,
53 ic_argpass_nonproto,
54 ic_assign,
55 ic_init,
56 ic_return
57 };
58
59 /* The level of nesting inside "__alignof__". */
60 int in_alignof;
61
62 /* The level of nesting inside "sizeof". */
63 int in_sizeof;
64
65 /* The level of nesting inside "typeof". */
66 int in_typeof;
67
68 struct c_label_context_se *label_context_stack_se;
69 struct c_label_context_vm *label_context_stack_vm;
70
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
74
75 static int require_constant_value;
76 static int require_constant_elements;
77
78 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cm) */
79 static bool types_are_block_compatible (tree lhptee, tree rhptee);
80 static tree build_block_call (tree, tree, tree);
81 /* APPLE LOCAL end radar 5732232 - blocks (C++ cm) */
82 static bool null_pointer_constant_p (tree);
83 static tree qualify_type (tree, tree);
84 static int tagged_types_tu_compatible_p (tree, tree);
85 static int comp_target_types (tree, tree);
86 static int function_types_compatible_p (tree, tree);
87 static int type_lists_compatible_p (tree, tree);
88 static tree decl_constant_value_for_broken_optimization (tree);
89 static tree lookup_field (tree, tree);
90 static tree convert_arguments (tree, tree, tree, tree);
91 static tree pointer_diff (tree, tree);
92 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
93 int);
94 static tree valid_compound_expr_initializer (tree, tree);
95 static void push_string (const char *);
96 static void push_member_name (tree);
97 static int spelling_length (void);
98 static char *print_spelling (char *);
99 static void warning_init (const char *);
100 static tree digest_init (tree, tree, bool, int);
101 static void output_init_element (tree, bool, tree, tree, int);
102 static void output_pending_init_elements (int);
103 static int set_designator (int);
104 static void push_range_stack (tree);
105 static void add_pending_init (tree, tree);
106 static void set_nonincremental_init (void);
107 static void set_nonincremental_init_from_string (tree);
108 static tree find_init_member (tree);
109 static void readonly_error (tree, enum lvalue_use);
110 static int lvalue_or_else (tree, enum lvalue_use);
111 static int lvalue_p (tree);
112 static void record_maybe_used_decl (tree);
113 static int comptypes_internal (tree, tree);
114
115 /* Return true if EXP is a null pointer constant, false otherwise. */
116
117 static bool
null_pointer_constant_p(tree expr)118 null_pointer_constant_p (tree expr)
119 {
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type = TREE_TYPE (expr);
123 return (TREE_CODE (expr) == INTEGER_CST
124 && !TREE_CONSTANT_OVERFLOW (expr)
125 && integer_zerop (expr)
126 && (INTEGRAL_TYPE_P (type)
127 || (TREE_CODE (type) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type))
129 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 }
131 /* This is a cache to hold if two types are compatible or not. */
132
133 struct tagged_tu_seen_cache {
134 const struct tagged_tu_seen_cache * next;
135 tree t1;
136 tree t2;
137 /* The return value of tagged_types_tu_compatible_p if we had seen
138 these two types already. */
139 int val;
140 };
141
142 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
143 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
144
145 /* Do `exp = require_complete_type (exp);' to make sure exp
146 does not have an incomplete type. (That includes void types.) */
147
148 tree
require_complete_type(tree value)149 require_complete_type (tree value)
150 {
151 tree type = TREE_TYPE (value);
152
153 if (value == error_mark_node || type == error_mark_node)
154 return error_mark_node;
155
156 /* First, detect a valid value with a complete type. */
157 if (COMPLETE_TYPE_P (type))
158 return value;
159
160 c_incomplete_type_error (value, type);
161 return error_mark_node;
162 }
163
164 /* Print an error message for invalid use of an incomplete type.
165 VALUE is the expression that was used (or 0 if that isn't known)
166 and TYPE is the type that was invalid. */
167
168 void
c_incomplete_type_error(tree value,tree type)169 c_incomplete_type_error (tree value, tree type)
170 {
171 const char *type_code_string;
172
173 /* Avoid duplicate error message. */
174 if (TREE_CODE (type) == ERROR_MARK)
175 return;
176
177 if (value != 0 && (TREE_CODE (value) == VAR_DECL
178 || TREE_CODE (value) == PARM_DECL))
179 error ("%qD has an incomplete type", value);
180 else
181 {
182 retry:
183 /* We must print an error message. Be clever about what it says. */
184
185 switch (TREE_CODE (type))
186 {
187 case RECORD_TYPE:
188 type_code_string = "struct";
189 break;
190
191 case UNION_TYPE:
192 type_code_string = "union";
193 break;
194
195 case ENUMERAL_TYPE:
196 type_code_string = "enum";
197 break;
198
199 case VOID_TYPE:
200 error ("invalid use of void expression");
201 return;
202
203 case ARRAY_TYPE:
204 if (TYPE_DOMAIN (type))
205 {
206 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
207 {
208 error ("invalid use of flexible array member");
209 return;
210 }
211 type = TREE_TYPE (type);
212 goto retry;
213 }
214 error ("invalid use of array with unspecified bounds");
215 return;
216
217 default:
218 gcc_unreachable ();
219 }
220
221 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
222 error ("invalid use of undefined type %<%s %E%>",
223 type_code_string, TYPE_NAME (type));
224 else
225 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
226 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
227 }
228 }
229
230 /* Given a type, apply default promotions wrt unnamed function
231 arguments and return the new type. */
232
233 tree
c_type_promotes_to(tree type)234 c_type_promotes_to (tree type)
235 {
236 if (TYPE_MAIN_VARIANT (type) == float_type_node)
237 return double_type_node;
238
239 if (c_promoting_integer_type_p (type))
240 {
241 /* Preserve unsignedness if not really getting any wider. */
242 if (TYPE_UNSIGNED (type)
243 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
244 return unsigned_type_node;
245 return integer_type_node;
246 }
247
248 return type;
249 }
250
251 /* Return a variant of TYPE which has all the type qualifiers of LIKE
252 as well as those of TYPE. */
253
254 static tree
qualify_type(tree type,tree like)255 qualify_type (tree type, tree like)
256 {
257 return c_build_qualified_type (type,
258 TYPE_QUALS (type) | TYPE_QUALS (like));
259 }
260
261 /* Return true iff the given tree T is a variable length array. */
262
263 bool
c_vla_type_p(tree t)264 c_vla_type_p (tree t)
265 {
266 if (TREE_CODE (t) == ARRAY_TYPE
267 && C_TYPE_VARIABLE_SIZE (t))
268 return true;
269 return false;
270 }
271
272 /* Return the composite type of two compatible types.
273
274 We assume that comptypes has already been done and returned
275 nonzero; if that isn't so, this may crash. In particular, we
276 assume that qualifiers match. */
277
278 tree
composite_type(tree t1,tree t2)279 composite_type (tree t1, tree t2)
280 {
281 enum tree_code code1;
282 enum tree_code code2;
283 tree attributes;
284
285 /* Save time if the two types are the same. */
286
287 if (t1 == t2) return t1;
288
289 /* If one type is nonsense, use the other. */
290 if (t1 == error_mark_node)
291 return t2;
292 if (t2 == error_mark_node)
293 return t1;
294
295 code1 = TREE_CODE (t1);
296 code2 = TREE_CODE (t2);
297
298 /* Merge the attributes. */
299 attributes = targetm.merge_type_attributes (t1, t2);
300
301 /* If one is an enumerated type and the other is the compatible
302 integer type, the composite type might be either of the two
303 (DR#013 question 3). For consistency, use the enumerated type as
304 the composite type. */
305
306 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
307 return t1;
308 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
309 return t2;
310
311 gcc_assert (code1 == code2);
312
313 switch (code1)
314 {
315 case POINTER_TYPE:
316 /* For two pointers, do this recursively on the target type. */
317 {
318 tree pointed_to_1 = TREE_TYPE (t1);
319 tree pointed_to_2 = TREE_TYPE (t2);
320 tree target = composite_type (pointed_to_1, pointed_to_2);
321 t1 = build_pointer_type (target);
322 t1 = build_type_attribute_variant (t1, attributes);
323 return qualify_type (t1, t2);
324 }
325
326 case ARRAY_TYPE:
327 {
328 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
329 int quals;
330 tree unqual_elt;
331 tree d1 = TYPE_DOMAIN (t1);
332 tree d2 = TYPE_DOMAIN (t2);
333 bool d1_variable, d2_variable;
334 bool d1_zero, d2_zero;
335
336 /* We should not have any type quals on arrays at all. */
337 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
338
339 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
340 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
341
342 d1_variable = (!d1_zero
343 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
344 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
345 d2_variable = (!d2_zero
346 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
347 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
348 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
349 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
350
351 /* Save space: see if the result is identical to one of the args. */
352 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
353 && (d2_variable || d2_zero || !d1_variable))
354 return build_type_attribute_variant (t1, attributes);
355 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
356 && (d1_variable || d1_zero || !d2_variable))
357 return build_type_attribute_variant (t2, attributes);
358
359 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
360 return build_type_attribute_variant (t1, attributes);
361 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
362 return build_type_attribute_variant (t2, attributes);
363
364 /* Merge the element types, and have a size if either arg has
365 one. We may have qualifiers on the element types. To set
366 up TYPE_MAIN_VARIANT correctly, we need to form the
367 composite of the unqualified types and add the qualifiers
368 back at the end. */
369 quals = TYPE_QUALS (strip_array_types (elt));
370 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
371 t1 = build_array_type (unqual_elt,
372 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
373 && (d2_variable
374 || d2_zero
375 || !d1_variable))
376 ? t1
377 : t2));
378 t1 = c_build_qualified_type (t1, quals);
379 return build_type_attribute_variant (t1, attributes);
380 }
381
382 case ENUMERAL_TYPE:
383 case RECORD_TYPE:
384 case UNION_TYPE:
385 if (attributes != NULL)
386 {
387 /* Try harder not to create a new aggregate type. */
388 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
389 return t1;
390 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
391 return t2;
392 }
393 return build_type_attribute_variant (t1, attributes);
394
395 case FUNCTION_TYPE:
396 /* Function types: prefer the one that specified arg types.
397 If both do, merge the arg types. Also merge the return types. */
398 {
399 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400 tree p1 = TYPE_ARG_TYPES (t1);
401 tree p2 = TYPE_ARG_TYPES (t2);
402 int len;
403 tree newargs, n;
404 int i;
405
406 /* Save space: see if the result is identical to one of the args. */
407 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
408 return build_type_attribute_variant (t1, attributes);
409 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
410 return build_type_attribute_variant (t2, attributes);
411
412 /* Simple way if one arg fails to specify argument types. */
413 if (TYPE_ARG_TYPES (t1) == 0)
414 {
415 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
416 t1 = build_type_attribute_variant (t1, attributes);
417 return qualify_type (t1, t2);
418 }
419 if (TYPE_ARG_TYPES (t2) == 0)
420 {
421 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
422 t1 = build_type_attribute_variant (t1, attributes);
423 return qualify_type (t1, t2);
424 }
425
426 /* If both args specify argument types, we must merge the two
427 lists, argument by argument. */
428 /* Tell global_bindings_p to return false so that variable_size
429 doesn't die on VLAs in parameter types. */
430 c_override_global_bindings_to_false = true;
431
432 len = list_length (p1);
433 newargs = 0;
434
435 for (i = 0; i < len; i++)
436 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
437
438 n = newargs;
439
440 for (; p1;
441 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
442 {
443 /* A null type means arg type is not specified.
444 Take whatever the other function type has. */
445 if (TREE_VALUE (p1) == 0)
446 {
447 TREE_VALUE (n) = TREE_VALUE (p2);
448 goto parm_done;
449 }
450 if (TREE_VALUE (p2) == 0)
451 {
452 TREE_VALUE (n) = TREE_VALUE (p1);
453 goto parm_done;
454 }
455
456 /* Given wait (union {union wait *u; int *i} *)
457 and wait (union wait *),
458 prefer union wait * as type of parm. */
459 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
460 && TREE_VALUE (p1) != TREE_VALUE (p2))
461 {
462 tree memb;
463 tree mv2 = TREE_VALUE (p2);
464 if (mv2 && mv2 != error_mark_node
465 && TREE_CODE (mv2) != ARRAY_TYPE)
466 mv2 = TYPE_MAIN_VARIANT (mv2);
467 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
468 memb; memb = TREE_CHAIN (memb))
469 {
470 tree mv3 = TREE_TYPE (memb);
471 if (mv3 && mv3 != error_mark_node
472 && TREE_CODE (mv3) != ARRAY_TYPE)
473 mv3 = TYPE_MAIN_VARIANT (mv3);
474 if (comptypes (mv3, mv2))
475 {
476 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
477 TREE_VALUE (p2));
478 if (pedantic)
479 pedwarn ("function types not truly compatible in ISO C");
480 goto parm_done;
481 }
482 }
483 }
484 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
485 && TREE_VALUE (p2) != TREE_VALUE (p1))
486 {
487 tree memb;
488 tree mv1 = TREE_VALUE (p1);
489 if (mv1 && mv1 != error_mark_node
490 && TREE_CODE (mv1) != ARRAY_TYPE)
491 mv1 = TYPE_MAIN_VARIANT (mv1);
492 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
493 memb; memb = TREE_CHAIN (memb))
494 {
495 tree mv3 = TREE_TYPE (memb);
496 if (mv3 && mv3 != error_mark_node
497 && TREE_CODE (mv3) != ARRAY_TYPE)
498 mv3 = TYPE_MAIN_VARIANT (mv3);
499 if (comptypes (mv3, mv1))
500 {
501 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
502 TREE_VALUE (p1));
503 if (pedantic)
504 pedwarn ("function types not truly compatible in ISO C");
505 goto parm_done;
506 }
507 }
508 }
509 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
510 parm_done: ;
511 }
512
513 c_override_global_bindings_to_false = false;
514 t1 = build_function_type (valtype, newargs);
515 t1 = qualify_type (t1, t2);
516 /* ... falls through ... */
517 }
518
519 default:
520 return build_type_attribute_variant (t1, attributes);
521 }
522
523 }
524
525 /* Return the type of a conditional expression between pointers to
526 possibly differently qualified versions of compatible types.
527
528 We assume that comp_target_types has already been done and returned
529 nonzero; if that isn't so, this may crash. */
530
531 static tree
common_pointer_type(tree t1,tree t2)532 common_pointer_type (tree t1, tree t2)
533 {
534 tree attributes;
535 tree pointed_to_1, mv1;
536 tree pointed_to_2, mv2;
537 tree target;
538
539 /* Save time if the two types are the same. */
540
541 if (t1 == t2) return t1;
542
543 /* If one type is nonsense, use the other. */
544 if (t1 == error_mark_node)
545 return t2;
546 if (t2 == error_mark_node)
547 return t1;
548
549 /* APPLE LOCAL begin blocks 6065211 */
550 gcc_assert ((TREE_CODE (t1) == POINTER_TYPE
551 && TREE_CODE (t2) == POINTER_TYPE)
552 || (TREE_CODE (t1) == BLOCK_POINTER_TYPE
553 && TREE_CODE (t2) == BLOCK_POINTER_TYPE));
554 /* APPLE LOCAL end blocks 6065211 */
555
556 /* Merge the attributes. */
557 attributes = targetm.merge_type_attributes (t1, t2);
558
559 /* Find the composite type of the target types, and combine the
560 qualifiers of the two types' targets. Do not lose qualifiers on
561 array element types by taking the TYPE_MAIN_VARIANT. */
562 mv1 = pointed_to_1 = TREE_TYPE (t1);
563 mv2 = pointed_to_2 = TREE_TYPE (t2);
564 if (TREE_CODE (mv1) != ARRAY_TYPE)
565 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
566 if (TREE_CODE (mv2) != ARRAY_TYPE)
567 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
568 target = composite_type (mv1, mv2);
569 /* APPLE LOCAL begin blocks 6065211 */
570 t1 = c_build_qualified_type (target,
571 TYPE_QUALS (pointed_to_1) |
572 TYPE_QUALS (pointed_to_2));
573 if (TREE_CODE (t2) == BLOCK_POINTER_TYPE)
574 t1 = build_block_pointer_type (t1);
575 else
576 t1 = build_pointer_type (t1);
577 /* APPLE LOCAL end blocks 6065211 */
578 return build_type_attribute_variant (t1, attributes);
579 }
580
581 /* Return the common type for two arithmetic types under the usual
582 arithmetic conversions. The default conversions have already been
583 applied, and enumerated types converted to their compatible integer
584 types. The resulting type is unqualified and has no attributes.
585
586 This is the type for the result of most arithmetic operations
587 if the operands have the given two types. */
588
589 static tree
c_common_type(tree t1,tree t2)590 c_common_type (tree t1, tree t2)
591 {
592 enum tree_code code1;
593 enum tree_code code2;
594
595 /* If one type is nonsense, use the other. */
596 if (t1 == error_mark_node)
597 return t2;
598 if (t2 == error_mark_node)
599 return t1;
600
601 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
602 t1 = TYPE_MAIN_VARIANT (t1);
603
604 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
605 t2 = TYPE_MAIN_VARIANT (t2);
606
607 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
608 t1 = build_type_attribute_variant (t1, NULL_TREE);
609
610 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
611 t2 = build_type_attribute_variant (t2, NULL_TREE);
612
613 /* Save time if the two types are the same. */
614
615 if (t1 == t2) return t1;
616
617 code1 = TREE_CODE (t1);
618 code2 = TREE_CODE (t2);
619
620 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
621 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
622 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
623 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
624
625 /* When one operand is a decimal float type, the other operand cannot be
626 a generic float type or a complex type. We also disallow vector types
627 here. */
628 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
629 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
630 {
631 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
632 {
633 error ("can%'t mix operands of decimal float and vector types");
634 return error_mark_node;
635 }
636 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
637 {
638 error ("can%'t mix operands of decimal float and complex types");
639 return error_mark_node;
640 }
641 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
642 {
643 error ("can%'t mix operands of decimal float and other float types");
644 return error_mark_node;
645 }
646 }
647
648 /* If one type is a vector type, return that type. (How the usual
649 arithmetic conversions apply to the vector types extension is not
650 precisely specified.) */
651 if (code1 == VECTOR_TYPE)
652 return t1;
653
654 if (code2 == VECTOR_TYPE)
655 return t2;
656
657 /* If one type is complex, form the common type of the non-complex
658 components, then make that complex. Use T1 or T2 if it is the
659 required type. */
660 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
661 {
662 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
663 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
664 tree subtype = c_common_type (subtype1, subtype2);
665
666 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
667 return t1;
668 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
669 return t2;
670 else
671 return build_complex_type (subtype);
672 }
673
674 /* If only one is real, use it as the result. */
675
676 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
677 return t1;
678
679 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
680 return t2;
681
682 /* If both are real and either are decimal floating point types, use
683 the decimal floating point type with the greater precision. */
684
685 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
686 {
687 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
688 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
689 return dfloat128_type_node;
690 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
691 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
692 return dfloat64_type_node;
693 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
694 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
695 return dfloat32_type_node;
696 }
697
698 /* Both real or both integers; use the one with greater precision. */
699
700 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
701 return t1;
702 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
703 return t2;
704
705 /* Same precision. Prefer long longs to longs to ints when the
706 same precision, following the C99 rules on integer type rank
707 (which are equivalent to the C90 rules for C90 types). */
708
709 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
710 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
711 return long_long_unsigned_type_node;
712
713 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
714 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
715 {
716 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
717 return long_long_unsigned_type_node;
718 else
719 return long_long_integer_type_node;
720 }
721
722 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
723 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
724 return long_unsigned_type_node;
725
726 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
727 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
728 {
729 /* But preserve unsignedness from the other type,
730 since long cannot hold all the values of an unsigned int. */
731 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
732 return long_unsigned_type_node;
733 else
734 return long_integer_type_node;
735 }
736
737 /* Likewise, prefer long double to double even if same size. */
738 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
739 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
740 return long_double_type_node;
741
742 /* Otherwise prefer the unsigned one. */
743
744 if (TYPE_UNSIGNED (t1))
745 return t1;
746 else
747 return t2;
748 }
749
750 /* Wrapper around c_common_type that is used by c-common.c and other
751 front end optimizations that remove promotions. ENUMERAL_TYPEs
752 are allowed here and are converted to their compatible integer types.
753 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
754 preferably a non-Boolean type as the common type. */
755 tree
common_type(tree t1,tree t2)756 common_type (tree t1, tree t2)
757 {
758 if (TREE_CODE (t1) == ENUMERAL_TYPE)
759 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
760 if (TREE_CODE (t2) == ENUMERAL_TYPE)
761 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
762
763 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
764 if (TREE_CODE (t1) == BOOLEAN_TYPE
765 && TREE_CODE (t2) == BOOLEAN_TYPE)
766 return boolean_type_node;
767
768 /* If either type is BOOLEAN_TYPE, then return the other. */
769 if (TREE_CODE (t1) == BOOLEAN_TYPE)
770 return t2;
771 if (TREE_CODE (t2) == BOOLEAN_TYPE)
772 return t1;
773
774 return c_common_type (t1, t2);
775 }
776
777 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
778 or various other operations. Return 2 if they are compatible
779 but a warning may be needed if you use them together. */
780
781 int
comptypes(tree type1,tree type2)782 comptypes (tree type1, tree type2)
783 {
784 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
785 int val;
786
787 val = comptypes_internal (type1, type2);
788 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
789
790 return val;
791 }
792
793 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
794 or various other operations. Return 2 if they are compatible
795 but a warning may be needed if you use them together. This
796 differs from comptypes, in that we don't free the seen types. */
797
798 static int
comptypes_internal(tree type1,tree type2)799 comptypes_internal (tree type1, tree type2)
800 {
801 tree t1 = type1;
802 tree t2 = type2;
803 int attrval, val;
804
805 /* Suppress errors caused by previously reported errors. */
806
807 if (t1 == t2 || !t1 || !t2
808 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
809 return 1;
810
811 /* If either type is the internal version of sizetype, return the
812 language version. */
813 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
814 && TYPE_ORIG_SIZE_TYPE (t1))
815 t1 = TYPE_ORIG_SIZE_TYPE (t1);
816
817 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
818 && TYPE_ORIG_SIZE_TYPE (t2))
819 t2 = TYPE_ORIG_SIZE_TYPE (t2);
820
821
822 /* Enumerated types are compatible with integer types, but this is
823 not transitive: two enumerated types in the same translation unit
824 are compatible with each other only if they are the same type. */
825
826 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
827 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
828 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
829 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
830
831 if (t1 == t2)
832 return 1;
833
834 /* Different classes of types can't be compatible. */
835
836 if (TREE_CODE (t1) != TREE_CODE (t2))
837 return 0;
838
839 /* Qualifiers must match. C99 6.7.3p9 */
840
841 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
842 return 0;
843
844 /* Allow for two different type nodes which have essentially the same
845 definition. Note that we already checked for equality of the type
846 qualifiers (just above). */
847
848 if (TREE_CODE (t1) != ARRAY_TYPE
849 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
850 return 1;
851
852 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
853 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
854 return 0;
855
856 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
857 val = 0;
858
859 switch (TREE_CODE (t1))
860 {
861 /* APPLE LOCAL begin radar 5795493 */
862 case BLOCK_POINTER_TYPE:
863 val = (TREE_CODE (t2) == BLOCK_POINTER_TYPE) &&
864 types_are_block_compatible (TREE_TYPE (t1), TREE_TYPE (t2));
865 break;
866
867 /* APPLE LOCAL end radar 5795493 */
868 case POINTER_TYPE:
869 /* Do not remove mode or aliasing information. */
870 if (TYPE_MODE (t1) != TYPE_MODE (t2)
871 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
872 break;
873 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
874 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
875 break;
876
877 case FUNCTION_TYPE:
878 val = function_types_compatible_p (t1, t2);
879 break;
880
881 case ARRAY_TYPE:
882 {
883 tree d1 = TYPE_DOMAIN (t1);
884 tree d2 = TYPE_DOMAIN (t2);
885 bool d1_variable, d2_variable;
886 bool d1_zero, d2_zero;
887 val = 1;
888
889 /* Target types must match incl. qualifiers. */
890 if (TREE_TYPE (t1) != TREE_TYPE (t2)
891 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
892 return 0;
893
894 /* Sizes must match unless one is missing or variable. */
895 if (d1 == 0 || d2 == 0 || d1 == d2)
896 break;
897
898 d1_zero = !TYPE_MAX_VALUE (d1);
899 d2_zero = !TYPE_MAX_VALUE (d2);
900
901 d1_variable = (!d1_zero
902 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
903 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
904 d2_variable = (!d2_zero
905 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
906 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
907 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
908 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
909
910 if (d1_variable || d2_variable)
911 break;
912 if (d1_zero && d2_zero)
913 break;
914 if (d1_zero || d2_zero
915 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
916 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
917 val = 0;
918
919 break;
920 }
921
922 case ENUMERAL_TYPE:
923 case RECORD_TYPE:
924 case UNION_TYPE:
925 if (val != 1 && !same_translation_unit_p (t1, t2))
926 {
927 tree a1 = TYPE_ATTRIBUTES (t1);
928 tree a2 = TYPE_ATTRIBUTES (t2);
929
930 if (! attribute_list_contained (a1, a2)
931 && ! attribute_list_contained (a2, a1))
932 break;
933
934 if (attrval != 2)
935 return tagged_types_tu_compatible_p (t1, t2);
936 val = tagged_types_tu_compatible_p (t1, t2);
937 }
938 break;
939
940 case VECTOR_TYPE:
941 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
942 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
943 break;
944
945 default:
946 break;
947 }
948 return attrval == 2 && val == 1 ? 2 : val;
949 }
950
951 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
952 ignoring their qualifiers. */
953
954 static int
comp_target_types(tree ttl,tree ttr)955 comp_target_types (tree ttl, tree ttr)
956 {
957 int val;
958 tree mvl, mvr;
959
960 /* APPLE LOCAL begin blocks 6065211 */
961 if (TREE_CODE (ttl) == BLOCK_POINTER_TYPE
962 && TREE_CODE (ttr) == BLOCK_POINTER_TYPE)
963 return types_are_block_compatible (TREE_TYPE (ttl),
964 TREE_TYPE (ttr));
965 if (TREE_CODE (ttl) != TREE_CODE (ttr))
966 return 0;
967 /* APPLE LOCAL end blocks 6065211 */
968
969 /* Do not lose qualifiers on element types of array types that are
970 pointer targets by taking their TYPE_MAIN_VARIANT. */
971 mvl = TREE_TYPE (ttl);
972 mvr = TREE_TYPE (ttr);
973 if (TREE_CODE (mvl) != ARRAY_TYPE)
974 mvl = TYPE_MAIN_VARIANT (mvl);
975 if (TREE_CODE (mvr) != ARRAY_TYPE)
976 mvr = TYPE_MAIN_VARIANT (mvr);
977 val = comptypes (mvl, mvr);
978
979 if (val == 2 && pedantic)
980 pedwarn ("types are not quite compatible");
981 return val;
982 }
983
984 /* Subroutines of `comptypes'. */
985
986 /* Determine whether two trees derive from the same translation unit.
987 If the CONTEXT chain ends in a null, that tree's context is still
988 being parsed, so if two trees have context chains ending in null,
989 they're in the same translation unit. */
990 int
same_translation_unit_p(tree t1,tree t2)991 same_translation_unit_p (tree t1, tree t2)
992 {
993 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
994 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
995 {
996 case tcc_declaration:
997 t1 = DECL_CONTEXT (t1); break;
998 case tcc_type:
999 t1 = TYPE_CONTEXT (t1); break;
1000 case tcc_exceptional:
1001 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1002 default: gcc_unreachable ();
1003 }
1004
1005 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1006 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1007 {
1008 case tcc_declaration:
1009 t2 = DECL_CONTEXT (t2); break;
1010 case tcc_type:
1011 t2 = TYPE_CONTEXT (t2); break;
1012 case tcc_exceptional:
1013 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1014 default: gcc_unreachable ();
1015 }
1016
1017 return t1 == t2;
1018 }
1019
1020 /* Allocate the seen two types, assuming that they are compatible. */
1021
1022 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(tree t1,tree t2)1023 alloc_tagged_tu_seen_cache (tree t1, tree t2)
1024 {
1025 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1026 tu->next = tagged_tu_seen_base;
1027 tu->t1 = t1;
1028 tu->t2 = t2;
1029
1030 tagged_tu_seen_base = tu;
1031
1032 /* The C standard says that two structures in different translation
1033 units are compatible with each other only if the types of their
1034 fields are compatible (among other things). We assume that they
1035 are compatible until proven otherwise when building the cache.
1036 An example where this can occur is:
1037 struct a
1038 {
1039 struct a *next;
1040 };
1041 If we are comparing this against a similar struct in another TU,
1042 and did not assume they were compatible, we end up with an infinite
1043 loop. */
1044 tu->val = 1;
1045 return tu;
1046 }
1047
1048 /* Free the seen types until we get to TU_TIL. */
1049
1050 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1051 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1052 {
1053 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1054 while (tu != tu_til)
1055 {
1056 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
1057 tu = tu1->next;
1058 free (tu1);
1059 }
1060 tagged_tu_seen_base = tu_til;
1061 }
1062
1063 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1064 compatible. If the two types are not the same (which has been
1065 checked earlier), this can only happen when multiple translation
1066 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1067 rules. */
1068
1069 static int
tagged_types_tu_compatible_p(tree t1,tree t2)1070 tagged_types_tu_compatible_p (tree t1, tree t2)
1071 {
1072 tree s1, s2;
1073 bool needs_warning = false;
1074
1075 /* We have to verify that the tags of the types are the same. This
1076 is harder than it looks because this may be a typedef, so we have
1077 to go look at the original type. It may even be a typedef of a
1078 typedef...
1079 In the case of compiler-created builtin structs the TYPE_DECL
1080 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1081 while (TYPE_NAME (t1)
1082 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1083 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1084 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1085
1086 while (TYPE_NAME (t2)
1087 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1088 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1089 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1090
1091 /* C90 didn't have the requirement that the two tags be the same. */
1092 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1093 return 0;
1094
1095 /* C90 didn't say what happened if one or both of the types were
1096 incomplete; we choose to follow C99 rules here, which is that they
1097 are compatible. */
1098 if (TYPE_SIZE (t1) == NULL
1099 || TYPE_SIZE (t2) == NULL)
1100 return 1;
1101
1102 {
1103 const struct tagged_tu_seen_cache * tts_i;
1104 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1105 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1106 return tts_i->val;
1107 }
1108
1109 switch (TREE_CODE (t1))
1110 {
1111 case ENUMERAL_TYPE:
1112 {
1113 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1114 /* Speed up the case where the type values are in the same order. */
1115 tree tv1 = TYPE_VALUES (t1);
1116 tree tv2 = TYPE_VALUES (t2);
1117
1118 if (tv1 == tv2)
1119 {
1120 return 1;
1121 }
1122
1123 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1124 {
1125 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1126 break;
1127 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1128 {
1129 tu->val = 0;
1130 return 0;
1131 }
1132 }
1133
1134 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1135 {
1136 return 1;
1137 }
1138 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1139 {
1140 tu->val = 0;
1141 return 0;
1142 }
1143
1144 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1145 {
1146 tu->val = 0;
1147 return 0;
1148 }
1149
1150 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1151 {
1152 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1153 if (s2 == NULL
1154 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1155 {
1156 tu->val = 0;
1157 return 0;
1158 }
1159 }
1160 return 1;
1161 }
1162
1163 case UNION_TYPE:
1164 {
1165 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1166 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1167 {
1168 tu->val = 0;
1169 return 0;
1170 }
1171
1172 /* Speed up the common case where the fields are in the same order. */
1173 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1174 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1175 {
1176 int result;
1177
1178
1179 if (DECL_NAME (s1) == NULL
1180 || DECL_NAME (s1) != DECL_NAME (s2))
1181 break;
1182 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1183 if (result == 0)
1184 {
1185 tu->val = 0;
1186 return 0;
1187 }
1188 if (result == 2)
1189 needs_warning = true;
1190
1191 if (TREE_CODE (s1) == FIELD_DECL
1192 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1193 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1194 {
1195 tu->val = 0;
1196 return 0;
1197 }
1198 }
1199 if (!s1 && !s2)
1200 {
1201 tu->val = needs_warning ? 2 : 1;
1202 return tu->val;
1203 }
1204
1205 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1206 {
1207 bool ok = false;
1208
1209 if (DECL_NAME (s1) != NULL)
1210 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1211 if (DECL_NAME (s1) == DECL_NAME (s2))
1212 {
1213 int result;
1214 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1215 if (result == 0)
1216 {
1217 tu->val = 0;
1218 return 0;
1219 }
1220 if (result == 2)
1221 needs_warning = true;
1222
1223 if (TREE_CODE (s1) == FIELD_DECL
1224 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1225 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1226 break;
1227
1228 ok = true;
1229 break;
1230 }
1231 if (!ok)
1232 {
1233 tu->val = 0;
1234 return 0;
1235 }
1236 }
1237 tu->val = needs_warning ? 2 : 10;
1238 return tu->val;
1239 }
1240
1241 case RECORD_TYPE:
1242 {
1243 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1244
1245 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1246 s1 && s2;
1247 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1248 {
1249 int result;
1250 if (TREE_CODE (s1) != TREE_CODE (s2)
1251 || DECL_NAME (s1) != DECL_NAME (s2))
1252 break;
1253 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1254 if (result == 0)
1255 break;
1256 if (result == 2)
1257 needs_warning = true;
1258
1259 if (TREE_CODE (s1) == FIELD_DECL
1260 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1261 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1262 break;
1263 }
1264 if (s1 && s2)
1265 tu->val = 0;
1266 else
1267 tu->val = needs_warning ? 2 : 1;
1268 return tu->val;
1269 }
1270
1271 default:
1272 gcc_unreachable ();
1273 }
1274 }
1275
1276 /* Return 1 if two function types F1 and F2 are compatible.
1277 If either type specifies no argument types,
1278 the other must specify a fixed number of self-promoting arg types.
1279 Otherwise, if one type specifies only the number of arguments,
1280 the other must specify that number of self-promoting arg types.
1281 Otherwise, the argument types must match. */
1282
1283 static int
function_types_compatible_p(tree f1,tree f2)1284 function_types_compatible_p (tree f1, tree f2)
1285 {
1286 tree args1, args2;
1287 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1288 int val = 1;
1289 int val1;
1290 tree ret1, ret2;
1291
1292 ret1 = TREE_TYPE (f1);
1293 ret2 = TREE_TYPE (f2);
1294
1295 /* 'volatile' qualifiers on a function's return type used to mean
1296 the function is noreturn. */
1297 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1298 pedwarn ("function return types not compatible due to %<volatile%>");
1299 if (TYPE_VOLATILE (ret1))
1300 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1301 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1302 if (TYPE_VOLATILE (ret2))
1303 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1304 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1305 val = comptypes_internal (ret1, ret2);
1306 if (val == 0)
1307 return 0;
1308
1309 args1 = TYPE_ARG_TYPES (f1);
1310 args2 = TYPE_ARG_TYPES (f2);
1311
1312 /* An unspecified parmlist matches any specified parmlist
1313 whose argument types don't need default promotions. */
1314
1315 if (args1 == 0)
1316 {
1317 if (!self_promoting_args_p (args2))
1318 return 0;
1319 /* If one of these types comes from a non-prototype fn definition,
1320 compare that with the other type's arglist.
1321 If they don't match, ask for a warning (but no error). */
1322 if (TYPE_ACTUAL_ARG_TYPES (f1)
1323 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1324 val = 2;
1325 return val;
1326 }
1327 if (args2 == 0)
1328 {
1329 if (!self_promoting_args_p (args1))
1330 return 0;
1331 if (TYPE_ACTUAL_ARG_TYPES (f2)
1332 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1333 val = 2;
1334 return val;
1335 }
1336
1337 /* Both types have argument lists: compare them and propagate results. */
1338 val1 = type_lists_compatible_p (args1, args2);
1339 return val1 != 1 ? val1 : val;
1340 }
1341
1342 /* Check two lists of types for compatibility,
1343 returning 0 for incompatible, 1 for compatible,
1344 or 2 for compatible with warning. */
1345
1346 static int
type_lists_compatible_p(tree args1,tree args2)1347 type_lists_compatible_p (tree args1, tree args2)
1348 {
1349 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1350 int val = 1;
1351 int newval = 0;
1352
1353 while (1)
1354 {
1355 tree a1, mv1, a2, mv2;
1356 if (args1 == 0 && args2 == 0)
1357 return val;
1358 /* If one list is shorter than the other,
1359 they fail to match. */
1360 if (args1 == 0 || args2 == 0)
1361 return 0;
1362 mv1 = a1 = TREE_VALUE (args1);
1363 mv2 = a2 = TREE_VALUE (args2);
1364 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1365 mv1 = TYPE_MAIN_VARIANT (mv1);
1366 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1367 mv2 = TYPE_MAIN_VARIANT (mv2);
1368 /* A null pointer instead of a type
1369 means there is supposed to be an argument
1370 but nothing is specified about what type it has.
1371 So match anything that self-promotes. */
1372 if (a1 == 0)
1373 {
1374 if (c_type_promotes_to (a2) != a2)
1375 return 0;
1376 }
1377 else if (a2 == 0)
1378 {
1379 if (c_type_promotes_to (a1) != a1)
1380 return 0;
1381 }
1382 /* If one of the lists has an error marker, ignore this arg. */
1383 else if (TREE_CODE (a1) == ERROR_MARK
1384 || TREE_CODE (a2) == ERROR_MARK)
1385 ;
1386 else if (!(newval = comptypes_internal (mv1, mv2)))
1387 {
1388 /* Allow wait (union {union wait *u; int *i} *)
1389 and wait (union wait *) to be compatible. */
1390 if (TREE_CODE (a1) == UNION_TYPE
1391 && (TYPE_NAME (a1) == 0
1392 || TYPE_TRANSPARENT_UNION (a1))
1393 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1394 && tree_int_cst_equal (TYPE_SIZE (a1),
1395 TYPE_SIZE (a2)))
1396 {
1397 tree memb;
1398 for (memb = TYPE_FIELDS (a1);
1399 memb; memb = TREE_CHAIN (memb))
1400 {
1401 tree mv3 = TREE_TYPE (memb);
1402 if (mv3 && mv3 != error_mark_node
1403 && TREE_CODE (mv3) != ARRAY_TYPE)
1404 mv3 = TYPE_MAIN_VARIANT (mv3);
1405 if (comptypes_internal (mv3, mv2))
1406 break;
1407 }
1408 if (memb == 0)
1409 return 0;
1410 }
1411 else if (TREE_CODE (a2) == UNION_TYPE
1412 && (TYPE_NAME (a2) == 0
1413 || TYPE_TRANSPARENT_UNION (a2))
1414 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1415 && tree_int_cst_equal (TYPE_SIZE (a2),
1416 TYPE_SIZE (a1)))
1417 {
1418 tree memb;
1419 for (memb = TYPE_FIELDS (a2);
1420 memb; memb = TREE_CHAIN (memb))
1421 {
1422 tree mv3 = TREE_TYPE (memb);
1423 if (mv3 && mv3 != error_mark_node
1424 && TREE_CODE (mv3) != ARRAY_TYPE)
1425 mv3 = TYPE_MAIN_VARIANT (mv3);
1426 if (comptypes_internal (mv3, mv1))
1427 break;
1428 }
1429 if (memb == 0)
1430 return 0;
1431 }
1432 else
1433 return 0;
1434 }
1435
1436 /* comptypes said ok, but record if it said to warn. */
1437 if (newval > val)
1438 val = newval;
1439
1440 args1 = TREE_CHAIN (args1);
1441 args2 = TREE_CHAIN (args2);
1442 }
1443 }
1444
1445 /* Compute the size to increment a pointer by. */
1446
1447 static tree
c_size_in_bytes(tree type)1448 c_size_in_bytes (tree type)
1449 {
1450 enum tree_code code = TREE_CODE (type);
1451
1452 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1453 return size_one_node;
1454
1455 if (!COMPLETE_OR_VOID_TYPE_P (type))
1456 {
1457 error ("arithmetic on pointer to an incomplete type");
1458 return size_one_node;
1459 }
1460
1461 /* Convert in case a char is more than one unit. */
1462 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1463 size_int (TYPE_PRECISION (char_type_node)
1464 / BITS_PER_UNIT));
1465 }
1466
1467 /* Return either DECL or its known constant value (if it has one). */
1468
1469 tree
decl_constant_value(tree decl)1470 decl_constant_value (tree decl)
1471 {
1472 if (/* Don't change a variable array bound or initial value to a constant
1473 in a place where a variable is invalid. Note that DECL_INITIAL
1474 isn't valid for a PARM_DECL. */
1475 current_function_decl != 0
1476 && TREE_CODE (decl) != PARM_DECL
1477 && !TREE_THIS_VOLATILE (decl)
1478 && TREE_READONLY (decl)
1479 && DECL_INITIAL (decl) != 0
1480 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1481 /* This is invalid if initial value is not constant.
1482 If it has either a function call, a memory reference,
1483 or a variable, then re-evaluating it could give different results. */
1484 && TREE_CONSTANT (DECL_INITIAL (decl))
1485 /* Check for cases where this is sub-optimal, even though valid. */
1486 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1487 return DECL_INITIAL (decl);
1488 return decl;
1489 }
1490
1491 /* Return either DECL or its known constant value (if it has one), but
1492 return DECL if pedantic or DECL has mode BLKmode. This is for
1493 bug-compatibility with the old behavior of decl_constant_value
1494 (before GCC 3.0); every use of this function is a bug and it should
1495 be removed before GCC 3.1. It is not appropriate to use pedantic
1496 in a way that affects optimization, and BLKmode is probably not the
1497 right test for avoiding misoptimizations either. */
1498
1499 static tree
decl_constant_value_for_broken_optimization(tree decl)1500 decl_constant_value_for_broken_optimization (tree decl)
1501 {
1502 tree ret;
1503
1504 if (pedantic || DECL_MODE (decl) == BLKmode)
1505 return decl;
1506
1507 ret = decl_constant_value (decl);
1508 /* Avoid unwanted tree sharing between the initializer and current
1509 function's body where the tree can be modified e.g. by the
1510 gimplifier. */
1511 if (ret != decl && TREE_STATIC (decl))
1512 ret = unshare_expr (ret);
1513 return ret;
1514 }
1515
1516 /* Convert the array expression EXP to a pointer. */
1517 /* APPLE LOCAL radar 6212722 */
1518 tree
array_to_pointer_conversion(tree exp)1519 array_to_pointer_conversion (tree exp)
1520 {
1521 tree orig_exp = exp;
1522 tree type = TREE_TYPE (exp);
1523 tree adr;
1524 tree restype = TREE_TYPE (type);
1525 tree ptrtype;
1526
1527 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1528
1529 STRIP_TYPE_NOPS (exp);
1530
1531 if (TREE_NO_WARNING (orig_exp))
1532 TREE_NO_WARNING (exp) = 1;
1533
1534 ptrtype = build_pointer_type (restype);
1535
1536 if (TREE_CODE (exp) == INDIRECT_REF)
1537 return convert (ptrtype, TREE_OPERAND (exp, 0));
1538
1539 if (TREE_CODE (exp) == VAR_DECL)
1540 {
1541 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1542 ADDR_EXPR because it's the best way of representing what
1543 happens in C when we take the address of an array and place
1544 it in a pointer to the element type. */
1545 adr = build1 (ADDR_EXPR, ptrtype, exp);
1546 if (!c_mark_addressable (exp))
1547 return error_mark_node;
1548 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1549 return adr;
1550 }
1551
1552 /* This way is better for a COMPONENT_REF since it can
1553 simplify the offset for a component. */
1554 adr = build_unary_op (ADDR_EXPR, exp, 1);
1555 return convert (ptrtype, adr);
1556 }
1557
1558 /* Convert the function expression EXP to a pointer. */
1559 /* APPLE LOCAL radar 6212722 */
1560 tree
function_to_pointer_conversion(tree exp)1561 function_to_pointer_conversion (tree exp)
1562 {
1563 tree orig_exp = exp;
1564
1565 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1566
1567 STRIP_TYPE_NOPS (exp);
1568
1569 if (TREE_NO_WARNING (orig_exp))
1570 TREE_NO_WARNING (exp) = 1;
1571
1572 return build_unary_op (ADDR_EXPR, exp, 0);
1573 }
1574
1575 /* Perform the default conversion of arrays and functions to pointers.
1576 Return the result of converting EXP. For any other expression, just
1577 return EXP after removing NOPs. */
1578
1579 struct c_expr
default_function_array_conversion(struct c_expr exp)1580 default_function_array_conversion (struct c_expr exp)
1581 {
1582 tree orig_exp = exp.value;
1583 tree type = TREE_TYPE (exp.value);
1584 enum tree_code code = TREE_CODE (type);
1585
1586 switch (code)
1587 {
1588 case ARRAY_TYPE:
1589 {
1590 bool not_lvalue = false;
1591 bool lvalue_array_p;
1592
1593 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1594 || TREE_CODE (exp.value) == NOP_EXPR
1595 || TREE_CODE (exp.value) == CONVERT_EXPR)
1596 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1597 {
1598 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1599 not_lvalue = true;
1600 exp.value = TREE_OPERAND (exp.value, 0);
1601 }
1602
1603 if (TREE_NO_WARNING (orig_exp))
1604 TREE_NO_WARNING (exp.value) = 1;
1605
1606 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1607 if (!flag_isoc99 && !lvalue_array_p)
1608 {
1609 /* Before C99, non-lvalue arrays do not decay to pointers.
1610 Normally, using such an array would be invalid; but it can
1611 be used correctly inside sizeof or as a statement expression.
1612 Thus, do not give an error here; an error will result later. */
1613 return exp;
1614 }
1615
1616 exp.value = array_to_pointer_conversion (exp.value);
1617 }
1618 break;
1619 case FUNCTION_TYPE:
1620 exp.value = function_to_pointer_conversion (exp.value);
1621 break;
1622 default:
1623 STRIP_TYPE_NOPS (exp.value);
1624 if (TREE_NO_WARNING (orig_exp))
1625 TREE_NO_WARNING (exp.value) = 1;
1626 break;
1627 }
1628
1629 return exp;
1630 }
1631
1632
1633 /* EXP is an expression of integer type. Apply the integer promotions
1634 to it and return the promoted value. */
1635
1636 tree
perform_integral_promotions(tree exp)1637 perform_integral_promotions (tree exp)
1638 {
1639 tree type = TREE_TYPE (exp);
1640 enum tree_code code = TREE_CODE (type);
1641
1642 gcc_assert (INTEGRAL_TYPE_P (type));
1643
1644 /* Normally convert enums to int,
1645 but convert wide enums to something wider. */
1646 if (code == ENUMERAL_TYPE)
1647 {
1648 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1649 TYPE_PRECISION (integer_type_node)),
1650 ((TYPE_PRECISION (type)
1651 >= TYPE_PRECISION (integer_type_node))
1652 && TYPE_UNSIGNED (type)));
1653
1654 return convert (type, exp);
1655 }
1656
1657 /* ??? This should no longer be needed now bit-fields have their
1658 proper types. */
1659 if (TREE_CODE (exp) == COMPONENT_REF
1660 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1661 /* If it's thinner than an int, promote it like a
1662 c_promoting_integer_type_p, otherwise leave it alone. */
1663 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1664 TYPE_PRECISION (integer_type_node)))
1665 return convert (integer_type_node, exp);
1666
1667 if (c_promoting_integer_type_p (type))
1668 {
1669 /* Preserve unsignedness if not really getting any wider. */
1670 if (TYPE_UNSIGNED (type)
1671 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1672 return convert (unsigned_type_node, exp);
1673
1674 return convert (integer_type_node, exp);
1675 }
1676
1677 return exp;
1678 }
1679
1680
1681 /* Perform default promotions for C data used in expressions.
1682 Enumeral types or short or char are converted to int.
1683 In addition, manifest constants symbols are replaced by their values. */
1684
1685 tree
default_conversion(tree exp)1686 default_conversion (tree exp)
1687 {
1688 tree orig_exp;
1689 tree type = TREE_TYPE (exp);
1690 enum tree_code code = TREE_CODE (type);
1691
1692 /* Functions and arrays have been converted during parsing. */
1693 gcc_assert (code != FUNCTION_TYPE);
1694 if (code == ARRAY_TYPE)
1695 return exp;
1696
1697 /* Constants can be used directly unless they're not loadable. */
1698 if (TREE_CODE (exp) == CONST_DECL)
1699 exp = DECL_INITIAL (exp);
1700
1701 /* Replace a nonvolatile const static variable with its value unless
1702 it is an array, in which case we must be sure that taking the
1703 address of the array produces consistent results. */
1704 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1705 {
1706 exp = decl_constant_value_for_broken_optimization (exp);
1707 type = TREE_TYPE (exp);
1708 }
1709
1710 /* Strip no-op conversions. */
1711 orig_exp = exp;
1712 STRIP_TYPE_NOPS (exp);
1713
1714 if (TREE_NO_WARNING (orig_exp))
1715 TREE_NO_WARNING (exp) = 1;
1716
1717 if (INTEGRAL_TYPE_P (type))
1718 return perform_integral_promotions (exp);
1719
1720 if (code == VOID_TYPE)
1721 {
1722 error ("void value not ignored as it ought to be");
1723 return error_mark_node;
1724 }
1725 return exp;
1726 }
1727
1728 /* Look up COMPONENT in a structure or union DECL.
1729
1730 If the component name is not found, returns NULL_TREE. Otherwise,
1731 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1732 stepping down the chain to the component, which is in the last
1733 TREE_VALUE of the list. Normally the list is of length one, but if
1734 the component is embedded within (nested) anonymous structures or
1735 unions, the list steps down the chain to the component. */
1736
1737 static tree
lookup_field(tree decl,tree component)1738 lookup_field (tree decl, tree component)
1739 {
1740 tree type = TREE_TYPE (decl);
1741 tree field;
1742
1743 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1744 to the field elements. Use a binary search on this array to quickly
1745 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1746 will always be set for structures which have many elements. */
1747
1748 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1749 {
1750 int bot, top, half;
1751 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1752
1753 field = TYPE_FIELDS (type);
1754 bot = 0;
1755 top = TYPE_LANG_SPECIFIC (type)->s->len;
1756 while (top - bot > 1)
1757 {
1758 half = (top - bot + 1) >> 1;
1759 field = field_array[bot+half];
1760
1761 if (DECL_NAME (field) == NULL_TREE)
1762 {
1763 /* Step through all anon unions in linear fashion. */
1764 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1765 {
1766 field = field_array[bot++];
1767 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1768 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1769 {
1770 tree anon = lookup_field (field, component);
1771
1772 if (anon)
1773 return tree_cons (NULL_TREE, field, anon);
1774 }
1775 }
1776
1777 /* Entire record is only anon unions. */
1778 if (bot > top)
1779 return NULL_TREE;
1780
1781 /* Restart the binary search, with new lower bound. */
1782 continue;
1783 }
1784
1785 if (DECL_NAME (field) == component)
1786 break;
1787 if (DECL_NAME (field) < component)
1788 bot += half;
1789 else
1790 top = bot + half;
1791 }
1792
1793 if (DECL_NAME (field_array[bot]) == component)
1794 field = field_array[bot];
1795 else if (DECL_NAME (field) != component)
1796 return NULL_TREE;
1797 }
1798 else
1799 {
1800 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1801 {
1802 if (DECL_NAME (field) == NULL_TREE
1803 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1804 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1805 {
1806 tree anon = lookup_field (field, component);
1807
1808 if (anon)
1809 return tree_cons (NULL_TREE, field, anon);
1810 }
1811
1812 if (DECL_NAME (field) == component)
1813 break;
1814 }
1815
1816 if (field == NULL_TREE)
1817 return NULL_TREE;
1818 }
1819
1820 return tree_cons (NULL_TREE, field, NULL_TREE);
1821 }
1822
1823 /* Make an expression to refer to the COMPONENT field of
1824 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1825
1826 tree
build_component_ref(tree datum,tree component)1827 build_component_ref (tree datum, tree component)
1828 {
1829 tree type = TREE_TYPE (datum);
1830 enum tree_code code = TREE_CODE (type);
1831 tree field = NULL;
1832 tree ref;
1833
1834 if (!objc_is_public (datum, component))
1835 return error_mark_node;
1836
1837 /* APPLE LOCAL begin C* property (Radar 4436866) */
1838 /* APPLE LOCAL radar 5285911 */
1839 if ((ref = objc_build_property_reference_expr (datum, component)))
1840 return ref;
1841 /* APPLE LOCAL end C* property (Radar 4436866) */
1842
1843 /* See if there is a field or component with name COMPONENT. */
1844
1845 if (code == RECORD_TYPE || code == UNION_TYPE)
1846 {
1847 if (!COMPLETE_TYPE_P (type))
1848 {
1849 c_incomplete_type_error (NULL_TREE, type);
1850 return error_mark_node;
1851 }
1852
1853 field = lookup_field (datum, component);
1854
1855 if (!field)
1856 {
1857 error ("%qT has no member named %qE", type, component);
1858 return error_mark_node;
1859 }
1860
1861 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1862 This might be better solved in future the way the C++ front
1863 end does it - by giving the anonymous entities each a
1864 separate name and type, and then have build_component_ref
1865 recursively call itself. We can't do that here. */
1866 do
1867 {
1868 tree subdatum = TREE_VALUE (field);
1869 int quals;
1870 tree subtype;
1871
1872 if (TREE_TYPE (subdatum) == error_mark_node)
1873 return error_mark_node;
1874
1875 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1876 quals |= TYPE_QUALS (TREE_TYPE (datum));
1877 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
1878
1879 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
1880 NULL_TREE);
1881 /* APPLE LOCAL radar 4697411 */
1882 objc_volatilize_component_ref (ref, TREE_TYPE (subdatum));
1883 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1884 TREE_READONLY (ref) = 1;
1885 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1886 TREE_THIS_VOLATILE (ref) = 1;
1887
1888 if (TREE_DEPRECATED (subdatum))
1889 warn_deprecated_use (subdatum);
1890
1891 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
1892 if (TREE_UNAVAILABLE (subdatum))
1893 error_unavailable_use (subdatum);
1894 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
1895
1896 datum = ref;
1897
1898 field = TREE_CHAIN (field);
1899 }
1900 while (field);
1901
1902 return ref;
1903 }
1904 else if (code != ERROR_MARK)
1905 error ("request for member %qE in something not a structure or union",
1906 component);
1907
1908 return error_mark_node;
1909 }
1910
1911 /* Given an expression PTR for a pointer, return an expression
1912 for the value pointed to.
1913 ERRORSTRING is the name of the operator to appear in error messages. */
1914
1915 tree
build_indirect_ref(tree ptr,const char * errorstring)1916 build_indirect_ref (tree ptr, const char *errorstring)
1917 {
1918 tree pointer = default_conversion (ptr);
1919 tree type = TREE_TYPE (pointer);
1920
1921 if (TREE_CODE (type) == POINTER_TYPE)
1922 {
1923 if (TREE_CODE (pointer) == CONVERT_EXPR
1924 || TREE_CODE (pointer) == NOP_EXPR
1925 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
1926 {
1927 /* If a warning is issued, mark it to avoid duplicates from
1928 the backend. This only needs to be done at
1929 warn_strict_aliasing > 2. */
1930 if (warn_strict_aliasing > 2)
1931 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
1932 type, TREE_OPERAND (pointer, 0)))
1933 TREE_NO_WARNING (pointer) = 1;
1934 }
1935
1936 if (TREE_CODE (pointer) == ADDR_EXPR
1937 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1938 == TREE_TYPE (type)))
1939 return TREE_OPERAND (pointer, 0);
1940 else
1941 {
1942 tree t = TREE_TYPE (type);
1943 tree ref;
1944
1945 ref = build1 (INDIRECT_REF, t, pointer);
1946
1947 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1948 {
1949 error ("dereferencing pointer to incomplete type");
1950 return error_mark_node;
1951 }
1952 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1953 warning (0, "dereferencing %<void *%> pointer");
1954
1955 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1956 so that we get the proper error message if the result is used
1957 to assign to. Also, &* is supposed to be a no-op.
1958 And ANSI C seems to specify that the type of the result
1959 should be the const type. */
1960 /* A de-reference of a pointer to const is not a const. It is valid
1961 to change it via some other pointer. */
1962 TREE_READONLY (ref) = TYPE_READONLY (t);
1963 TREE_SIDE_EFFECTS (ref)
1964 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1965 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1966 return ref;
1967 }
1968 }
1969 else if (TREE_CODE (pointer) != ERROR_MARK)
1970 error ("invalid type argument of %qs (have %qT)", errorstring, type);
1971 return error_mark_node;
1972 }
1973
1974 /* This handles expressions of the form "a[i]", which denotes
1975 an array reference.
1976
1977 This is logically equivalent in C to *(a+i), but we may do it differently.
1978 If A is a variable or a member, we generate a primitive ARRAY_REF.
1979 This avoids forcing the array out of registers, and can work on
1980 arrays that are not lvalues (for example, members of structures returned
1981 by functions). */
1982
1983 tree
build_array_ref(tree array,tree index)1984 build_array_ref (tree array, tree index)
1985 {
1986 bool swapped = false;
1987 if (TREE_TYPE (array) == error_mark_node
1988 || TREE_TYPE (index) == error_mark_node)
1989 return error_mark_node;
1990
1991 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1992 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1993 {
1994 tree temp;
1995 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1996 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1997 {
1998 error ("subscripted value is neither array nor pointer");
1999 return error_mark_node;
2000 }
2001 temp = array;
2002 array = index;
2003 index = temp;
2004 swapped = true;
2005 }
2006
2007 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2008 {
2009 error ("array subscript is not an integer");
2010 return error_mark_node;
2011 }
2012
2013 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2014 {
2015 error ("subscripted value is pointer to function");
2016 return error_mark_node;
2017 }
2018
2019 /* ??? Existing practice has been to warn only when the char
2020 index is syntactically the index, not for char[array]. */
2021 if (!swapped)
2022 warn_array_subscript_with_type_char (index);
2023
2024 /* Apply default promotions *after* noticing character types. */
2025 index = default_conversion (index);
2026
2027 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2028
2029 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2030 {
2031 tree rval, type;
2032
2033 /* An array that is indexed by a non-constant
2034 cannot be stored in a register; we must be able to do
2035 address arithmetic on its address.
2036 Likewise an array of elements of variable size. */
2037 if (TREE_CODE (index) != INTEGER_CST
2038 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2039 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2040 {
2041 if (!c_mark_addressable (array))
2042 return error_mark_node;
2043 }
2044 /* An array that is indexed by a constant value which is not within
2045 the array bounds cannot be stored in a register either; because we
2046 would get a crash in store_bit_field/extract_bit_field when trying
2047 to access a non-existent part of the register. */
2048 if (TREE_CODE (index) == INTEGER_CST
2049 && TYPE_DOMAIN (TREE_TYPE (array))
2050 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2051 {
2052 if (!c_mark_addressable (array))
2053 return error_mark_node;
2054 }
2055
2056 if (pedantic)
2057 {
2058 tree foo = array;
2059 while (TREE_CODE (foo) == COMPONENT_REF)
2060 foo = TREE_OPERAND (foo, 0);
2061 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2062 pedwarn ("ISO C forbids subscripting %<register%> array");
2063 else if (!flag_isoc99 && !lvalue_p (foo))
2064 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
2065 }
2066
2067 type = TREE_TYPE (TREE_TYPE (array));
2068 if (TREE_CODE (type) != ARRAY_TYPE)
2069 type = TYPE_MAIN_VARIANT (type);
2070 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2071 /* Array ref is const/volatile if the array elements are
2072 or if the array is. */
2073 TREE_READONLY (rval)
2074 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2075 | TREE_READONLY (array));
2076 TREE_SIDE_EFFECTS (rval)
2077 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2078 | TREE_SIDE_EFFECTS (array));
2079 TREE_THIS_VOLATILE (rval)
2080 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2081 /* This was added by rms on 16 Nov 91.
2082 It fixes vol struct foo *a; a->elts[1]
2083 in an inline function.
2084 Hope it doesn't break something else. */
2085 | TREE_THIS_VOLATILE (array));
2086 return require_complete_type (fold (rval));
2087 }
2088 else
2089 {
2090 tree ar = default_conversion (array);
2091
2092 if (ar == error_mark_node)
2093 return ar;
2094
2095 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2096 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2097
2098 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2099 "array indexing");
2100 }
2101 }
2102
2103 /* Build an external reference to identifier ID. FUN indicates
2104 whether this will be used for a function call. LOC is the source
2105 location of the identifier. */
2106 tree
build_external_ref(tree id,int fun,location_t loc)2107 build_external_ref (tree id, int fun, location_t loc)
2108 {
2109 tree ref;
2110 tree decl = lookup_name (id);
2111
2112 /* In Objective-C, an instance variable (ivar) may be preferred to
2113 whatever lookup_name() found. */
2114 decl = objc_lookup_ivar (decl, id);
2115 /* APPLE LOCAL begin radar 5732232 - blocks (C++ ci) */
2116 if (decl && decl != error_mark_node)
2117 {
2118 if (cur_block
2119 && (TREE_CODE (decl) == VAR_DECL
2120 || TREE_CODE (decl) == PARM_DECL)
2121 && !lookup_name_in_block (id, &decl))
2122 {
2123 /* APPLE LOCAL begin radar 5803005 (C++ ci) */
2124 bool gdecl;
2125 /* We are referencing a variable inside a block whose declaration
2126 is outside. */
2127 gcc_assert (decl &&
2128 (TREE_CODE (decl) == VAR_DECL
2129 || TREE_CODE (decl) == PARM_DECL));
2130 gdecl = (TREE_CODE (decl) == VAR_DECL &&
2131 /* APPLE LOCAL radar 6177162 */
2132 (DECL_EXTERNAL (decl) || TREE_STATIC (decl)));
2133 /* Treat all 'global' variables as 'byref' by default. */
2134 /* APPLE LOCAL begin radar 6014138 (C++ ci) */
2135 if (gdecl || (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl)))
2136 /* APPLE LOCAL end radar 6014138 (C++ ci) */
2137 {
2138 /* APPLE LOCAL begin radar 5803600 (C++ ci) */
2139 /* byref globals are directly accessed. */
2140 /* APPLE LOCAL begin radar 7760213 */
2141 if (!gdecl) {
2142 if (HasByrefArray(TREE_TYPE (decl)))
2143 error ("cannot access __block variable of array type inside block");
2144 /* build a decl for the byref variable. */
2145 decl = build_block_byref_decl (id, decl, decl);
2146 }
2147 /* APPLE LOCAL end radar 7760213 */
2148 else
2149 add_block_global_byref_list (decl);
2150 }
2151 else
2152 {
2153 /* 'byref' globals are never copied-in. So, do not add
2154 them to the copied-in list. */
2155 if (!in_block_global_byref_list (decl)) {
2156 /* APPLE LOCAL begin radar 7721728 */
2157 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
2158 error ("cannot access copied-in variable of array type inside block");
2159 /* APPLE LOCAL end radar 7721728 */
2160 /* build a new decl node. set its type to 'const' type
2161 of the old decl. */
2162 decl = build_block_ref_decl (id, decl);
2163 }
2164 /* APPLE LOCAL end radar 5803600 (C++ ci) */
2165 /* APPLE LOCAL end radar 5803005 (C++ ci) */
2166 }
2167 }
2168 ref = decl;
2169 }
2170 /* APPLE LOCAL end radar 5732232 - blocks (C++ ci) */
2171 else if (fun)
2172 /* Implicit function declaration. */
2173 ref = implicitly_declare (id);
2174 else if (decl == error_mark_node)
2175 /* Don't complain about something that's already been
2176 complained about. */
2177 return error_mark_node;
2178 else
2179 {
2180 undeclared_variable (id, loc);
2181 return error_mark_node;
2182 }
2183
2184 if (TREE_TYPE (ref) == error_mark_node)
2185 return error_mark_node;
2186
2187 if (TREE_DEPRECATED (ref))
2188 warn_deprecated_use (ref);
2189
2190 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
2191 if (TREE_UNAVAILABLE (ref))
2192 error_unavailable_use (ref);
2193 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
2194
2195 if (!skip_evaluation)
2196 assemble_external (ref);
2197 TREE_USED (ref) = 1;
2198
2199 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2200 {
2201 if (!in_sizeof && !in_typeof)
2202 C_DECL_USED (ref) = 1;
2203 else if (DECL_INITIAL (ref) == 0
2204 && DECL_EXTERNAL (ref)
2205 && !TREE_PUBLIC (ref))
2206 record_maybe_used_decl (ref);
2207 }
2208
2209 if (TREE_CODE (ref) == CONST_DECL)
2210 {
2211 used_types_insert (TREE_TYPE (ref));
2212 ref = DECL_INITIAL (ref);
2213 TREE_CONSTANT (ref) = 1;
2214 TREE_INVARIANT (ref) = 1;
2215 }
2216 else if (current_function_decl != 0
2217 && !DECL_FILE_SCOPE_P (current_function_decl)
2218 && (TREE_CODE (ref) == VAR_DECL
2219 || TREE_CODE (ref) == PARM_DECL
2220 || TREE_CODE (ref) == FUNCTION_DECL))
2221 {
2222 tree context = decl_function_context (ref);
2223
2224 if (context != 0 && context != current_function_decl)
2225 DECL_NONLOCAL (ref) = 1;
2226 }
2227 /* C99 6.7.4p3: An inline definition of a function with external
2228 linkage ... shall not contain a reference to an identifier with
2229 internal linkage. */
2230 else if (current_function_decl != 0
2231 && DECL_DECLARED_INLINE_P (current_function_decl)
2232 && DECL_EXTERNAL (current_function_decl)
2233 && VAR_OR_FUNCTION_DECL_P (ref)
2234 && DECL_FILE_SCOPE_P (ref)
2235 && pedantic
2236 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2237 && ! TREE_PUBLIC (ref))
2238 pedwarn ("%H%qD is static but used in inline function %qD "
2239 "which is not static", &loc, ref, current_function_decl);
2240
2241 return ref;
2242 }
2243
2244 /* Record details of decls possibly used inside sizeof or typeof. */
2245 struct maybe_used_decl
2246 {
2247 /* The decl. */
2248 tree decl;
2249 /* The level seen at (in_sizeof + in_typeof). */
2250 int level;
2251 /* The next one at this level or above, or NULL. */
2252 struct maybe_used_decl *next;
2253 };
2254
2255 static struct maybe_used_decl *maybe_used_decls;
2256
2257 /* Record that DECL, an undefined static function reference seen
2258 inside sizeof or typeof, might be used if the operand of sizeof is
2259 a VLA type or the operand of typeof is a variably modified
2260 type. */
2261
2262 static void
record_maybe_used_decl(tree decl)2263 record_maybe_used_decl (tree decl)
2264 {
2265 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2266 t->decl = decl;
2267 t->level = in_sizeof + in_typeof;
2268 t->next = maybe_used_decls;
2269 maybe_used_decls = t;
2270 }
2271
2272 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2273 USED is false, just discard them. If it is true, mark them used
2274 (if no longer inside sizeof or typeof) or move them to the next
2275 level up (if still inside sizeof or typeof). */
2276
2277 void
pop_maybe_used(bool used)2278 pop_maybe_used (bool used)
2279 {
2280 struct maybe_used_decl *p = maybe_used_decls;
2281 int cur_level = in_sizeof + in_typeof;
2282 while (p && p->level > cur_level)
2283 {
2284 if (used)
2285 {
2286 if (cur_level == 0)
2287 C_DECL_USED (p->decl) = 1;
2288 else
2289 p->level = cur_level;
2290 }
2291 p = p->next;
2292 }
2293 if (!used || cur_level == 0)
2294 maybe_used_decls = p;
2295 }
2296
2297 /* Return the result of sizeof applied to EXPR. */
2298
2299 struct c_expr
c_expr_sizeof_expr(struct c_expr expr)2300 c_expr_sizeof_expr (struct c_expr expr)
2301 {
2302 struct c_expr ret;
2303 if (expr.value == error_mark_node)
2304 {
2305 ret.value = error_mark_node;
2306 ret.original_code = ERROR_MARK;
2307 pop_maybe_used (false);
2308 }
2309 else
2310 {
2311 ret.value = c_sizeof (TREE_TYPE (expr.value));
2312 ret.original_code = ERROR_MARK;
2313 if (c_vla_type_p (TREE_TYPE (expr.value)))
2314 {
2315 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2316 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2317 }
2318 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2319 }
2320 return ret;
2321 }
2322
2323 /* Return the result of sizeof applied to T, a structure for the type
2324 name passed to sizeof (rather than the type itself). */
2325
2326 struct c_expr
c_expr_sizeof_type(struct c_type_name * t)2327 c_expr_sizeof_type (struct c_type_name *t)
2328 {
2329 tree type;
2330 struct c_expr ret;
2331 type = groktypename (t);
2332 ret.value = c_sizeof (type);
2333 ret.original_code = ERROR_MARK;
2334 pop_maybe_used (type != error_mark_node
2335 ? C_TYPE_VARIABLE_SIZE (type) : false);
2336 return ret;
2337 }
2338
2339 /* Build a function call to function FUNCTION with parameters PARAMS.
2340 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2341 TREE_VALUE of each node is a parameter-expression.
2342 FUNCTION's data type may be a function type or a pointer-to-function. */
2343
2344 tree
build_function_call(tree function,tree params)2345 build_function_call (tree function, tree params)
2346 {
2347 tree fntype, fundecl = 0;
2348 tree coerced_params;
2349 tree name = NULL_TREE, result;
2350 tree tem;
2351
2352 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2353 STRIP_TYPE_NOPS (function);
2354
2355 /* Convert anything with function type to a pointer-to-function. */
2356 if (TREE_CODE (function) == FUNCTION_DECL)
2357 {
2358 /* Implement type-directed function overloading for builtins.
2359 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2360 handle all the type checking. The result is a complete expression
2361 that implements this function call. */
2362 tem = resolve_overloaded_builtin (function, params);
2363 if (tem)
2364 return tem;
2365
2366 name = DECL_NAME (function);
2367 fundecl = function;
2368 }
2369 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2370 function = function_to_pointer_conversion (function);
2371
2372 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2373 expressions, like those used for ObjC messenger dispatches. */
2374 function = objc_rewrite_function_call (function, params);
2375
2376 fntype = TREE_TYPE (function);
2377
2378 if (TREE_CODE (fntype) == ERROR_MARK)
2379 return error_mark_node;
2380 /* APPLE LOCAL begin radar 5732232 - blocks */
2381 if (!((TREE_CODE (fntype) == POINTER_TYPE
2382 || TREE_CODE (fntype) == BLOCK_POINTER_TYPE)
2383 /* APPLE LOCAL end radar 5732232 - blocks */
2384 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2385 {
2386 error ("called object %qE is not a function", function);
2387 return error_mark_node;
2388 }
2389
2390 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2391 current_function_returns_abnormally = 1;
2392
2393 /* fntype now gets the type of function pointed to. */
2394 fntype = TREE_TYPE (fntype);
2395
2396 /* Check that the function is called through a compatible prototype.
2397 If it is not, replace the call by a trap, wrapped up in a compound
2398 expression if necessary. This has the nice side-effect to prevent
2399 the tree-inliner from generating invalid assignment trees which may
2400 blow up in the RTL expander later. */
2401 if ((TREE_CODE (function) == NOP_EXPR
2402 || TREE_CODE (function) == CONVERT_EXPR)
2403 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2404 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2405 && !comptypes (fntype, TREE_TYPE (tem)))
2406 {
2407 tree return_type = TREE_TYPE (fntype);
2408 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2409 NULL_TREE);
2410
2411 /* This situation leads to run-time undefined behavior. We can't,
2412 therefore, simply error unless we can prove that all possible
2413 executions of the program must execute the code. */
2414 warning (0, "function called through a non-compatible type");
2415
2416 /* We can, however, treat "undefined" any way we please.
2417 Call abort to encourage the user to fix the program. */
2418 inform ("if this code is reached, the program will abort");
2419
2420 if (VOID_TYPE_P (return_type))
2421 return trap;
2422 else
2423 {
2424 tree rhs;
2425
2426 if (AGGREGATE_TYPE_P (return_type))
2427 rhs = build_compound_literal (return_type,
2428 build_constructor (return_type, 0));
2429 else
2430 rhs = fold_convert (return_type, integer_zero_node);
2431
2432 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2433 }
2434 }
2435
2436 /* Convert the parameters to the types declared in the
2437 function prototype, or apply default promotions. */
2438
2439 coerced_params
2440 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2441
2442 if (coerced_params == error_mark_node)
2443 return error_mark_node;
2444
2445 /* Check that the arguments to the function are valid. */
2446
2447 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2448 TYPE_ARG_TYPES (fntype));
2449
2450 /* APPLE LOCAL begin radar 5732232 - blocks */
2451 if (TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE)
2452 result = build_block_call (fntype, function, coerced_params);
2453 else
2454 /* APPLE LOCAL end radar 5732232 - blocks */
2455 if (require_constant_value)
2456 {
2457 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2458 function, coerced_params, NULL_TREE);
2459
2460 if (TREE_CONSTANT (result)
2461 && (name == NULL_TREE
2462 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2463 pedwarn_init ("initializer element is not constant");
2464 }
2465 else
2466 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2467 function, coerced_params, NULL_TREE);
2468
2469 if (VOID_TYPE_P (TREE_TYPE (result)))
2470 return result;
2471 return require_complete_type (result);
2472 }
2473
2474 /* Convert the argument expressions in the list VALUES
2475 to the types in the list TYPELIST. The result is a list of converted
2476 argument expressions, unless there are too few arguments in which
2477 case it is error_mark_node.
2478
2479 If TYPELIST is exhausted, or when an element has NULL as its type,
2480 perform the default conversions.
2481
2482 PARMLIST is the chain of parm decls for the function being called.
2483 It may be 0, if that info is not available.
2484 It is used only for generating error messages.
2485
2486 FUNCTION is a tree for the called function. It is used only for
2487 error messages, where it is formatted with %qE.
2488
2489 This is also where warnings about wrong number of args are generated.
2490
2491 Both VALUES and the returned value are chains of TREE_LIST nodes
2492 with the elements of the list in the TREE_VALUE slots of those nodes. */
2493
2494 static tree
convert_arguments(tree typelist,tree values,tree function,tree fundecl)2495 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2496 {
2497 tree typetail, valtail;
2498 tree result = NULL;
2499 int parmnum;
2500 tree selector;
2501
2502 /* Change pointer to function to the function itself for
2503 diagnostics. */
2504 if (TREE_CODE (function) == ADDR_EXPR
2505 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2506 function = TREE_OPERAND (function, 0);
2507
2508 /* Handle an ObjC selector specially for diagnostics. */
2509 selector = objc_message_selector ();
2510
2511 /* Scan the given expressions and types, producing individual
2512 converted arguments and pushing them on RESULT in reverse order. */
2513
2514 for (valtail = values, typetail = typelist, parmnum = 0;
2515 valtail;
2516 valtail = TREE_CHAIN (valtail), parmnum++)
2517 {
2518 tree type = typetail ? TREE_VALUE (typetail) : 0;
2519 tree val = TREE_VALUE (valtail);
2520 tree rname = function;
2521 int argnum = parmnum + 1;
2522 const char *invalid_func_diag;
2523
2524 if (type == void_type_node)
2525 {
2526 /* APPLE LOCAL begin radar 5732232 - blocks */
2527 if (TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE)
2528 {
2529 error ("too many arguments to block call");
2530 break;
2531 }
2532 /* APPLE LOCAL end radar 5732232 - blocks */
2533 /* APPLE LOCAL begin radar 4491608 */
2534 error ("too many arguments to function %qE", selector ? selector
2535 : function);
2536 /* APPLE LOCAL end radar 4491608 */
2537 break;
2538 }
2539
2540 if (selector && argnum > 2)
2541 {
2542 rname = selector;
2543 argnum -= 2;
2544 }
2545
2546 STRIP_TYPE_NOPS (val);
2547
2548 val = require_complete_type (val);
2549
2550 if (type != 0)
2551 {
2552 /* Formal parm type is specified by a function prototype. */
2553 tree parmval;
2554
2555 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2556 {
2557 error ("type of formal parameter %d is incomplete", parmnum + 1);
2558 parmval = val;
2559 }
2560 else
2561 {
2562 /* Optionally warn about conversions that
2563 differ from the default conversions. */
2564 if (warn_conversion || warn_traditional)
2565 {
2566 unsigned int formal_prec = TYPE_PRECISION (type);
2567
2568 if (INTEGRAL_TYPE_P (type)
2569 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2570 warning (0, "passing argument %d of %qE as integer "
2571 "rather than floating due to prototype",
2572 argnum, rname);
2573 if (INTEGRAL_TYPE_P (type)
2574 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2575 warning (0, "passing argument %d of %qE as integer "
2576 "rather than complex due to prototype",
2577 argnum, rname);
2578 else if (TREE_CODE (type) == COMPLEX_TYPE
2579 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2580 warning (0, "passing argument %d of %qE as complex "
2581 "rather than floating due to prototype",
2582 argnum, rname);
2583 else if (TREE_CODE (type) == REAL_TYPE
2584 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2585 warning (0, "passing argument %d of %qE as floating "
2586 "rather than integer due to prototype",
2587 argnum, rname);
2588 else if (TREE_CODE (type) == COMPLEX_TYPE
2589 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2590 warning (0, "passing argument %d of %qE as complex "
2591 "rather than integer due to prototype",
2592 argnum, rname);
2593 else if (TREE_CODE (type) == REAL_TYPE
2594 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2595 warning (0, "passing argument %d of %qE as floating "
2596 "rather than complex due to prototype",
2597 argnum, rname);
2598 /* ??? At some point, messages should be written about
2599 conversions between complex types, but that's too messy
2600 to do now. */
2601 else if (TREE_CODE (type) == REAL_TYPE
2602 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2603 {
2604 /* Warn if any argument is passed as `float',
2605 since without a prototype it would be `double'. */
2606 if (formal_prec == TYPE_PRECISION (float_type_node)
2607 && type != dfloat32_type_node)
2608 warning (0, "passing argument %d of %qE as %<float%> "
2609 "rather than %<double%> due to prototype",
2610 argnum, rname);
2611
2612 /* Warn if mismatch between argument and prototype
2613 for decimal float types. Warn of conversions with
2614 binary float types and of precision narrowing due to
2615 prototype. */
2616 else if (type != TREE_TYPE (val)
2617 && (type == dfloat32_type_node
2618 || type == dfloat64_type_node
2619 || type == dfloat128_type_node
2620 || TREE_TYPE (val) == dfloat32_type_node
2621 || TREE_TYPE (val) == dfloat64_type_node
2622 || TREE_TYPE (val) == dfloat128_type_node)
2623 && (formal_prec
2624 <= TYPE_PRECISION (TREE_TYPE (val))
2625 || (type == dfloat128_type_node
2626 && (TREE_TYPE (val)
2627 != dfloat64_type_node
2628 && (TREE_TYPE (val)
2629 != dfloat32_type_node)))
2630 || (type == dfloat64_type_node
2631 && (TREE_TYPE (val)
2632 != dfloat32_type_node))))
2633 warning (0, "passing argument %d of %qE as %qT "
2634 "rather than %qT due to prototype",
2635 argnum, rname, type, TREE_TYPE (val));
2636
2637 }
2638 /* Detect integer changing in width or signedness.
2639 These warnings are only activated with
2640 -Wconversion, not with -Wtraditional. */
2641 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2642 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2643 {
2644 tree would_have_been = default_conversion (val);
2645 tree type1 = TREE_TYPE (would_have_been);
2646
2647 if (TREE_CODE (type) == ENUMERAL_TYPE
2648 && (TYPE_MAIN_VARIANT (type)
2649 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2650 /* No warning if function asks for enum
2651 and the actual arg is that enum type. */
2652 ;
2653 else if (formal_prec != TYPE_PRECISION (type1))
2654 warning (OPT_Wconversion, "passing argument %d of %qE "
2655 "with different width due to prototype",
2656 argnum, rname);
2657 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2658 ;
2659 /* Don't complain if the formal parameter type
2660 is an enum, because we can't tell now whether
2661 the value was an enum--even the same enum. */
2662 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2663 ;
2664 else if (TREE_CODE (val) == INTEGER_CST
2665 && int_fits_type_p (val, type))
2666 /* Change in signedness doesn't matter
2667 if a constant value is unaffected. */
2668 ;
2669 /* If the value is extended from a narrower
2670 unsigned type, it doesn't matter whether we
2671 pass it as signed or unsigned; the value
2672 certainly is the same either way. */
2673 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2674 && TYPE_UNSIGNED (TREE_TYPE (val)))
2675 ;
2676 else if (TYPE_UNSIGNED (type))
2677 warning (OPT_Wconversion, "passing argument %d of %qE "
2678 "as unsigned due to prototype",
2679 argnum, rname);
2680 else
2681 warning (OPT_Wconversion, "passing argument %d of %qE "
2682 "as signed due to prototype", argnum, rname);
2683 }
2684 }
2685
2686 parmval = convert_for_assignment (type, val, ic_argpass,
2687 fundecl, function,
2688 parmnum + 1);
2689
2690 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2691 && INTEGRAL_TYPE_P (type)
2692 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2693 parmval = default_conversion (parmval);
2694 }
2695 result = tree_cons (NULL_TREE, parmval, result);
2696 }
2697 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2698 && (TYPE_PRECISION (TREE_TYPE (val))
2699 < TYPE_PRECISION (double_type_node))
2700 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2701 /* Convert `float' to `double'. */
2702 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2703 else if ((invalid_func_diag =
2704 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2705 {
2706 error (invalid_func_diag, "");
2707 return error_mark_node;
2708 }
2709 else
2710 /* Convert `short' and `char' to full-size `int'. */
2711 result = tree_cons (NULL_TREE, default_conversion (val), result);
2712
2713 if (typetail)
2714 typetail = TREE_CHAIN (typetail);
2715 }
2716
2717 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2718 {
2719 /* APPLE LOCAL begin radar 5732232 - blocks */
2720 if (TREE_CODE (TREE_TYPE (function)) == BLOCK_POINTER_TYPE)
2721 error ("too few arguments to block %qE", function);
2722 else
2723 error ("too few arguments to function %qE", function);
2724 /* APPLE LOCAL end radar 5732232 - blocks */
2725 return error_mark_node;
2726 }
2727
2728 return nreverse (result);
2729 }
2730
2731 /* This is the entry point used by the parser to build unary operators
2732 in the input. CODE, a tree_code, specifies the unary operator, and
2733 ARG is the operand. For unary plus, the C parser currently uses
2734 CONVERT_EXPR for code. */
2735
2736 struct c_expr
parser_build_unary_op(enum tree_code code,struct c_expr arg)2737 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2738 {
2739 struct c_expr result;
2740
2741 result.original_code = ERROR_MARK;
2742 result.value = build_unary_op (code, arg.value, 0);
2743
2744 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2745 overflow_warning (result.value);
2746
2747 return result;
2748 }
2749
2750 /* This is the entry point used by the parser to build binary operators
2751 in the input. CODE, a tree_code, specifies the binary operator, and
2752 ARG1 and ARG2 are the operands. In addition to constructing the
2753 expression, we check for operands that were written with other binary
2754 operators in a way that is likely to confuse the user. */
2755
2756 struct c_expr
parser_build_binary_op(enum tree_code code,struct c_expr arg1,struct c_expr arg2)2757 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2758 struct c_expr arg2)
2759 {
2760 struct c_expr result;
2761
2762 enum tree_code code1 = arg1.original_code;
2763 enum tree_code code2 = arg2.original_code;
2764
2765 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2766 result.original_code = code;
2767
2768 if (TREE_CODE (result.value) == ERROR_MARK)
2769 return result;
2770
2771 /* Check for cases such as x+y<<z which users are likely
2772 to misinterpret. */
2773 if (warn_parentheses)
2774 warn_about_parentheses (code, code1, code2);
2775
2776 /* Warn about comparisons against string literals, with the exception
2777 of testing for equality or inequality of a string literal with NULL. */
2778 if (code == EQ_EXPR || code == NE_EXPR)
2779 {
2780 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2781 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2782 warning (OPT_Waddress,
2783 "comparison with string literal results in unspecified behaviour");
2784 }
2785 else if (TREE_CODE_CLASS (code) == tcc_comparison
2786 && (code1 == STRING_CST || code2 == STRING_CST))
2787 warning (OPT_Waddress,
2788 "comparison with string literal results in unspecified behaviour");
2789
2790 if (TREE_OVERFLOW_P (result.value)
2791 && !TREE_OVERFLOW_P (arg1.value)
2792 && !TREE_OVERFLOW_P (arg2.value))
2793 overflow_warning (result.value);
2794
2795 return result;
2796 }
2797
2798 /* Return a tree for the difference of pointers OP0 and OP1.
2799 The resulting tree has type int. */
2800
2801 static tree
pointer_diff(tree op0,tree op1)2802 pointer_diff (tree op0, tree op1)
2803 {
2804 tree restype = ptrdiff_type_node;
2805
2806 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2807 tree con0, con1, lit0, lit1;
2808 tree orig_op1 = op1;
2809
2810 if (pedantic || warn_pointer_arith)
2811 {
2812 if (TREE_CODE (target_type) == VOID_TYPE)
2813 pedwarn ("pointer of type %<void *%> used in subtraction");
2814 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2815 pedwarn ("pointer to a function used in subtraction");
2816 }
2817
2818 /* If the conversion to ptrdiff_type does anything like widening or
2819 converting a partial to an integral mode, we get a convert_expression
2820 that is in the way to do any simplifications.
2821 (fold-const.c doesn't know that the extra bits won't be needed.
2822 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2823 different mode in place.)
2824 So first try to find a common term here 'by hand'; we want to cover
2825 at least the cases that occur in legal static initializers. */
2826 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2827 && (TYPE_PRECISION (TREE_TYPE (op0))
2828 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2829 con0 = TREE_OPERAND (op0, 0);
2830 else
2831 con0 = op0;
2832 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2833 && (TYPE_PRECISION (TREE_TYPE (op1))
2834 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2835 con1 = TREE_OPERAND (op1, 0);
2836 else
2837 con1 = op1;
2838
2839 if (TREE_CODE (con0) == PLUS_EXPR)
2840 {
2841 lit0 = TREE_OPERAND (con0, 1);
2842 con0 = TREE_OPERAND (con0, 0);
2843 }
2844 else
2845 lit0 = integer_zero_node;
2846
2847 if (TREE_CODE (con1) == PLUS_EXPR)
2848 {
2849 lit1 = TREE_OPERAND (con1, 1);
2850 con1 = TREE_OPERAND (con1, 0);
2851 }
2852 else
2853 lit1 = integer_zero_node;
2854
2855 if (operand_equal_p (con0, con1, 0))
2856 {
2857 op0 = lit0;
2858 op1 = lit1;
2859 }
2860
2861
2862 /* First do the subtraction as integers;
2863 then drop through to build the divide operator.
2864 Do not do default conversions on the minus operator
2865 in case restype is a short type. */
2866
2867 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2868 convert (restype, op1), 0);
2869 /* This generates an error if op1 is pointer to incomplete type. */
2870 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2871 error ("arithmetic on pointer to an incomplete type");
2872
2873 /* This generates an error if op0 is pointer to incomplete type. */
2874 op1 = c_size_in_bytes (target_type);
2875
2876 /* Divide by the size, in easiest possible way. */
2877 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2878 }
2879
2880 /* Construct and perhaps optimize a tree representation
2881 for a unary operation. CODE, a tree_code, specifies the operation
2882 and XARG is the operand.
2883 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2884 the default promotions (such as from short to int).
2885 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2886 allows non-lvalues; this is only used to handle conversion of non-lvalue
2887 arrays to pointers in C99. */
2888
2889 tree
build_unary_op(enum tree_code code,tree xarg,int flag)2890 build_unary_op (enum tree_code code, tree xarg, int flag)
2891 {
2892 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2893 tree arg = xarg;
2894 tree argtype = 0;
2895 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2896 tree val;
2897 int noconvert = flag;
2898 const char *invalid_op_diag;
2899
2900 if (typecode == ERROR_MARK)
2901 return error_mark_node;
2902 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2903 typecode = INTEGER_TYPE;
2904
2905 if ((invalid_op_diag
2906 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2907 {
2908 error (invalid_op_diag, "");
2909 return error_mark_node;
2910 }
2911
2912 switch (code)
2913 {
2914 case CONVERT_EXPR:
2915 /* This is used for unary plus, because a CONVERT_EXPR
2916 is enough to prevent anybody from looking inside for
2917 associativity, but won't generate any code. */
2918 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2919 || typecode == COMPLEX_TYPE
2920 || typecode == VECTOR_TYPE))
2921 {
2922 error ("wrong type argument to unary plus");
2923 return error_mark_node;
2924 }
2925 else if (!noconvert)
2926 arg = default_conversion (arg);
2927 arg = non_lvalue (arg);
2928 break;
2929
2930 case NEGATE_EXPR:
2931 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2932 || typecode == COMPLEX_TYPE
2933 || typecode == VECTOR_TYPE))
2934 {
2935 error ("wrong type argument to unary minus");
2936 return error_mark_node;
2937 }
2938 else if (!noconvert)
2939 arg = default_conversion (arg);
2940 break;
2941
2942 case BIT_NOT_EXPR:
2943 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2944 {
2945 if (!noconvert)
2946 arg = default_conversion (arg);
2947 }
2948 else if (typecode == COMPLEX_TYPE)
2949 {
2950 code = CONJ_EXPR;
2951 if (pedantic)
2952 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2953 if (!noconvert)
2954 arg = default_conversion (arg);
2955 }
2956 else
2957 {
2958 error ("wrong type argument to bit-complement");
2959 return error_mark_node;
2960 }
2961 break;
2962
2963 case ABS_EXPR:
2964 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2965 {
2966 error ("wrong type argument to abs");
2967 return error_mark_node;
2968 }
2969 else if (!noconvert)
2970 arg = default_conversion (arg);
2971 break;
2972
2973 case CONJ_EXPR:
2974 /* Conjugating a real value is a no-op, but allow it anyway. */
2975 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2976 || typecode == COMPLEX_TYPE))
2977 {
2978 error ("wrong type argument to conjugation");
2979 return error_mark_node;
2980 }
2981 else if (!noconvert)
2982 arg = default_conversion (arg);
2983 break;
2984
2985 case TRUTH_NOT_EXPR:
2986 if (typecode != INTEGER_TYPE
2987 /* APPLE LOCAL radar 5732232 - blocks */
2988 && typecode != BLOCK_POINTER_TYPE
2989 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2990 && typecode != COMPLEX_TYPE)
2991 {
2992 error ("wrong type argument to unary exclamation mark");
2993 return error_mark_node;
2994 }
2995 arg = c_objc_common_truthvalue_conversion (arg);
2996 return invert_truthvalue (arg);
2997
2998 case REALPART_EXPR:
2999 if (TREE_CODE (arg) == COMPLEX_CST)
3000 return TREE_REALPART (arg);
3001 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3002 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3003 else
3004 return arg;
3005
3006 case IMAGPART_EXPR:
3007 if (TREE_CODE (arg) == COMPLEX_CST)
3008 return TREE_IMAGPART (arg);
3009 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3010 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3011 else
3012 return convert (TREE_TYPE (arg), integer_zero_node);
3013
3014 case PREINCREMENT_EXPR:
3015 case POSTINCREMENT_EXPR:
3016 case PREDECREMENT_EXPR:
3017 case POSTDECREMENT_EXPR:
3018
3019 /* Increment or decrement the real part of the value,
3020 and don't change the imaginary part. */
3021 if (typecode == COMPLEX_TYPE)
3022 {
3023 tree real, imag;
3024
3025 if (pedantic)
3026 pedwarn ("ISO C does not support %<++%> and %<--%>"
3027 " on complex types");
3028
3029 arg = stabilize_reference (arg);
3030 real = build_unary_op (REALPART_EXPR, arg, 1);
3031 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3032 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3033 build_unary_op (code, real, 1), imag);
3034 }
3035
3036 /* Report invalid types. */
3037
3038 if (typecode != POINTER_TYPE
3039 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3040 {
3041 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3042 error ("wrong type argument to increment");
3043 else
3044 error ("wrong type argument to decrement");
3045
3046 return error_mark_node;
3047 }
3048
3049 {
3050 tree inc;
3051 tree result_type = TREE_TYPE (arg);
3052
3053 arg = get_unwidened (arg, 0);
3054 argtype = TREE_TYPE (arg);
3055
3056 /* Compute the increment. */
3057
3058 if (typecode == POINTER_TYPE)
3059 {
3060 /* If pointer target is an undefined struct,
3061 we just cannot know how to do the arithmetic. */
3062 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
3063 {
3064 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3065 error ("increment of pointer to unknown structure");
3066 else
3067 error ("decrement of pointer to unknown structure");
3068 }
3069 else if ((pedantic || warn_pointer_arith)
3070 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3071 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3072 {
3073 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3074 pedwarn ("wrong type argument to increment");
3075 else
3076 pedwarn ("wrong type argument to decrement");
3077 }
3078
3079 inc = c_size_in_bytes (TREE_TYPE (result_type));
3080 }
3081 else
3082 inc = integer_one_node;
3083
3084 inc = convert (argtype, inc);
3085
3086 /* Complain about anything else that is not a true lvalue. */
3087 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3088 || code == POSTINCREMENT_EXPR)
3089 ? lv_increment
3090 : lv_decrement)))
3091 return error_mark_node;
3092
3093 /* Report a read-only lvalue. */
3094 if (TREE_READONLY (arg))
3095 {
3096 readonly_error (arg,
3097 ((code == PREINCREMENT_EXPR
3098 || code == POSTINCREMENT_EXPR)
3099 ? lv_increment : lv_decrement));
3100 return error_mark_node;
3101 }
3102
3103 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3104 val = boolean_increment (code, arg);
3105 else
3106 val = build2 (code, TREE_TYPE (arg), arg, inc);
3107 TREE_SIDE_EFFECTS (val) = 1;
3108 val = convert (result_type, val);
3109 if (TREE_CODE (val) != code)
3110 TREE_NO_WARNING (val) = 1;
3111 return val;
3112 }
3113
3114 case ADDR_EXPR:
3115 /* Note that this operation never does default_conversion. */
3116
3117 /* Let &* cancel out to simplify resulting code. */
3118 if (TREE_CODE (arg) == INDIRECT_REF)
3119 {
3120 /* Don't let this be an lvalue. */
3121 if (lvalue_p (TREE_OPERAND (arg, 0)))
3122 return non_lvalue (TREE_OPERAND (arg, 0));
3123 return TREE_OPERAND (arg, 0);
3124 }
3125
3126 /* For &x[y], return x+y */
3127 if (TREE_CODE (arg) == ARRAY_REF)
3128 {
3129 tree op0 = TREE_OPERAND (arg, 0);
3130 if (!c_mark_addressable (op0))
3131 return error_mark_node;
3132 return build_binary_op (PLUS_EXPR,
3133 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3134 ? array_to_pointer_conversion (op0)
3135 : op0),
3136 TREE_OPERAND (arg, 1), 1);
3137 }
3138
3139 /* Anything not already handled and not a true memory reference
3140 or a non-lvalue array is an error. */
3141 else if (typecode != FUNCTION_TYPE && !flag
3142 && !lvalue_or_else (arg, lv_addressof))
3143 return error_mark_node;
3144
3145 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3146 argtype = TREE_TYPE (arg);
3147
3148 /* If the lvalue is const or volatile, merge that into the type
3149 to which the address will point. Note that you can't get a
3150 restricted pointer by taking the address of something, so we
3151 only have to deal with `const' and `volatile' here. */
3152 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3153 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3154 argtype = c_build_type_variant (argtype,
3155 TREE_READONLY (arg),
3156 TREE_THIS_VOLATILE (arg));
3157
3158 if (!c_mark_addressable (arg))
3159 return error_mark_node;
3160
3161 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3162 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3163
3164 argtype = build_pointer_type (argtype);
3165
3166 /* ??? Cope with user tricks that amount to offsetof. Delete this
3167 when we have proper support for integer constant expressions. */
3168 val = get_base_address (arg);
3169 if (val && TREE_CODE (val) == INDIRECT_REF
3170 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3171 {
3172 tree op0 = fold_convert (argtype, fold_offsetof (arg, val)), op1;
3173
3174 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3175 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3176 }
3177
3178 val = build1 (ADDR_EXPR, argtype, arg);
3179
3180 return val;
3181
3182 default:
3183 gcc_unreachable ();
3184 }
3185
3186 if (argtype == 0)
3187 argtype = TREE_TYPE (arg);
3188 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3189 : fold_build1 (code, argtype, arg);
3190 }
3191
3192 /* Return nonzero if REF is an lvalue valid for this language.
3193 Lvalues can be assigned, unless their type has TYPE_READONLY.
3194 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3195
3196 static int
lvalue_p(tree ref)3197 lvalue_p (tree ref)
3198 {
3199 enum tree_code code = TREE_CODE (ref);
3200
3201 switch (code)
3202 {
3203 case REALPART_EXPR:
3204 case IMAGPART_EXPR:
3205 case COMPONENT_REF:
3206 return lvalue_p (TREE_OPERAND (ref, 0));
3207
3208 case COMPOUND_LITERAL_EXPR:
3209 case STRING_CST:
3210 return 1;
3211
3212 case INDIRECT_REF:
3213 case ARRAY_REF:
3214 case VAR_DECL:
3215 case PARM_DECL:
3216 case RESULT_DECL:
3217 case ERROR_MARK:
3218 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3219 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3220
3221 case BIND_EXPR:
3222 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3223
3224 default:
3225 return 0;
3226 }
3227 }
3228
3229 /* Give an error for storing in something that is 'const'. */
3230
3231 static void
readonly_error(tree arg,enum lvalue_use use)3232 readonly_error (tree arg, enum lvalue_use use)
3233 {
3234 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3235 || use == lv_asm);
3236 /* Using this macro rather than (for example) arrays of messages
3237 ensures that all the format strings are checked at compile
3238 time. */
3239 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3240 : (use == lv_increment ? (I) \
3241 : (use == lv_decrement ? (D) : (AS))))
3242 if (TREE_CODE (arg) == COMPONENT_REF)
3243 {
3244 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3245 readonly_error (TREE_OPERAND (arg, 0), use);
3246 else
3247 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3248 G_("increment of read-only member %qD"),
3249 G_("decrement of read-only member %qD"),
3250 G_("read-only member %qD used as %<asm%> output")),
3251 TREE_OPERAND (arg, 1));
3252 }
3253 else if (TREE_CODE (arg) == VAR_DECL)
3254 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3255 G_("increment of read-only variable %qD"),
3256 G_("decrement of read-only variable %qD"),
3257 G_("read-only variable %qD used as %<asm%> output")),
3258 arg);
3259 else
3260 error (READONLY_MSG (G_("assignment of read-only location"),
3261 G_("increment of read-only location"),
3262 G_("decrement of read-only location"),
3263 G_("read-only location used as %<asm%> output")));
3264 }
3265
3266
3267 /* Return nonzero if REF is an lvalue valid for this language;
3268 otherwise, print an error message and return zero. USE says
3269 how the lvalue is being used and so selects the error message. */
3270
3271 static int
lvalue_or_else(tree ref,enum lvalue_use use)3272 lvalue_or_else (tree ref, enum lvalue_use use)
3273 {
3274 int win = lvalue_p (ref);
3275
3276 if (!win)
3277 lvalue_error (use);
3278
3279 return win;
3280 }
3281
3282 /* Mark EXP saying that we need to be able to take the
3283 address of it; it should not be allocated in a register.
3284 Returns true if successful. */
3285
3286 bool
c_mark_addressable(tree exp)3287 c_mark_addressable (tree exp)
3288 {
3289 tree x = exp;
3290
3291 while (1)
3292 switch (TREE_CODE (x))
3293 {
3294 case COMPONENT_REF:
3295 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3296 {
3297 error
3298 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3299 return false;
3300 }
3301
3302 /* ... fall through ... */
3303
3304 case ADDR_EXPR:
3305 case ARRAY_REF:
3306 case REALPART_EXPR:
3307 case IMAGPART_EXPR:
3308 x = TREE_OPERAND (x, 0);
3309 break;
3310
3311 case COMPOUND_LITERAL_EXPR:
3312 case CONSTRUCTOR:
3313 TREE_ADDRESSABLE (x) = 1;
3314 return true;
3315
3316 case VAR_DECL:
3317 case CONST_DECL:
3318 case PARM_DECL:
3319 case RESULT_DECL:
3320 if (C_DECL_REGISTER (x)
3321 && DECL_NONLOCAL (x))
3322 {
3323 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3324 {
3325 error
3326 ("global register variable %qD used in nested function", x);
3327 return false;
3328 }
3329 pedwarn ("register variable %qD used in nested function", x);
3330 }
3331 else if (C_DECL_REGISTER (x))
3332 {
3333 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3334 error ("address of global register variable %qD requested", x);
3335 else
3336 error ("address of register variable %qD requested", x);
3337 return false;
3338 }
3339
3340 /* drops in */
3341 case FUNCTION_DECL:
3342 TREE_ADDRESSABLE (x) = 1;
3343 /* drops out */
3344 default:
3345 return true;
3346 }
3347 }
3348
3349 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3350
3351 tree
build_conditional_expr(tree ifexp,tree op1,tree op2)3352 build_conditional_expr (tree ifexp, tree op1, tree op2)
3353 {
3354 tree type1;
3355 tree type2;
3356 enum tree_code code1;
3357 enum tree_code code2;
3358 tree result_type = NULL;
3359 tree orig_op1 = op1, orig_op2 = op2;
3360
3361 /* Promote both alternatives. */
3362
3363 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3364 op1 = default_conversion (op1);
3365 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3366 op2 = default_conversion (op2);
3367
3368 if (TREE_CODE (ifexp) == ERROR_MARK
3369 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3370 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3371 return error_mark_node;
3372
3373 type1 = TREE_TYPE (op1);
3374 code1 = TREE_CODE (type1);
3375 type2 = TREE_TYPE (op2);
3376 code2 = TREE_CODE (type2);
3377
3378 /* C90 does not permit non-lvalue arrays in conditional expressions.
3379 In C99 they will be pointers by now. */
3380 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3381 {
3382 error ("non-lvalue array in conditional expression");
3383 return error_mark_node;
3384 }
3385
3386 /* Quickly detect the usual case where op1 and op2 have the same type
3387 after promotion. */
3388 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3389 {
3390 if (type1 == type2)
3391 result_type = type1;
3392 else
3393 result_type = TYPE_MAIN_VARIANT (type1);
3394 }
3395 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3396 || code1 == COMPLEX_TYPE)
3397 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3398 || code2 == COMPLEX_TYPE))
3399 {
3400 result_type = c_common_type (type1, type2);
3401
3402 /* If -Wsign-compare, warn here if type1 and type2 have
3403 different signedness. We'll promote the signed to unsigned
3404 and later code won't know it used to be different.
3405 Do this check on the original types, so that explicit casts
3406 will be considered, but default promotions won't. */
3407 if (warn_sign_compare && !skip_evaluation)
3408 {
3409 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3410 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3411
3412 if (unsigned_op1 ^ unsigned_op2)
3413 {
3414 bool ovf;
3415
3416 /* Do not warn if the result type is signed, since the
3417 signed type will only be chosen if it can represent
3418 all the values of the unsigned type. */
3419 if (!TYPE_UNSIGNED (result_type))
3420 /* OK */;
3421 /* Do not warn if the signed quantity is an unsuffixed
3422 integer literal (or some static constant expression
3423 involving such literals) and it is non-negative. */
3424 else if ((unsigned_op2
3425 && tree_expr_nonnegative_warnv_p (op1, &ovf))
3426 || (unsigned_op1
3427 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
3428 /* OK */;
3429 else
3430 warning (0, "signed and unsigned type in conditional expression");
3431 }
3432 }
3433 }
3434 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3435 {
3436 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3437 pedwarn ("ISO C forbids conditional expr with only one void side");
3438 result_type = void_type_node;
3439 }
3440 /* APPLE LOCAL begin blocks 6065211 */
3441 else if ((code1 == POINTER_TYPE
3442 || code1 == BLOCK_POINTER_TYPE)
3443 && (code2 == POINTER_TYPE
3444 || code2 == BLOCK_POINTER_TYPE))
3445 /* APPLE LOCAL end blocks 6065211 */
3446 {
3447 if (comp_target_types (type1, type2))
3448 result_type = common_pointer_type (type1, type2);
3449 else if (null_pointer_constant_p (orig_op1))
3450 result_type = qualify_type (type2, type1);
3451 else if (null_pointer_constant_p (orig_op2))
3452 result_type = qualify_type (type1, type2);
3453 /* APPLE LOCAL begin blocks 6065211 */
3454 else if (code2 == BLOCK_POINTER_TYPE
3455 && VOID_TYPE_P (TREE_TYPE (type1)))
3456 result_type = type2;
3457 else if (code1 == BLOCK_POINTER_TYPE
3458 && VOID_TYPE_P (TREE_TYPE (type2)))
3459 result_type = type1;
3460 /* APPLE LOCAL end blocks 6065211 */
3461 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3462 {
3463 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3464 pedwarn ("ISO C forbids conditional expr between "
3465 "%<void *%> and function pointer");
3466 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3467 TREE_TYPE (type2)));
3468 }
3469 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3470 {
3471 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3472 pedwarn ("ISO C forbids conditional expr between "
3473 "%<void *%> and function pointer");
3474 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3475 TREE_TYPE (type1)));
3476 }
3477 else
3478 {
3479 pedwarn ("pointer type mismatch in conditional expression");
3480 result_type = build_pointer_type (void_type_node);
3481 }
3482 }
3483 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3484 {
3485 if (!null_pointer_constant_p (orig_op2))
3486 pedwarn ("pointer/integer type mismatch in conditional expression");
3487 else
3488 {
3489 op2 = null_pointer_node;
3490 }
3491 result_type = type1;
3492 }
3493 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3494 {
3495 if (!null_pointer_constant_p (orig_op1))
3496 pedwarn ("pointer/integer type mismatch in conditional expression");
3497 else
3498 {
3499 op1 = null_pointer_node;
3500 }
3501 result_type = type2;
3502 }
3503 /* APPLE LOCAL begin radar 5732232 - blocks (C++ co) */
3504 /* APPLE LOCAL radar 5957801 */
3505 else if (code1 == BLOCK_POINTER_TYPE && code2 == INTEGER_TYPE)
3506 {
3507 if (!null_pointer_constant_p (orig_op2))
3508 error ("block pointer/integer type mismatch in conditional expression");
3509 else
3510 {
3511 op2 = convert (type1, null_pointer_node);
3512 }
3513 result_type = type1;
3514 }
3515 /* APPLE LOCAL radar 5957801 */
3516 else if (code2 == BLOCK_POINTER_TYPE && code1 == INTEGER_TYPE)
3517 {
3518 if (!null_pointer_constant_p (orig_op1))
3519 error ("block pointer/integer type mismatch in conditional expression");
3520 else
3521 {
3522 op1 = convert (type2, null_pointer_node);
3523 }
3524 result_type = type2;
3525 }
3526
3527 /* APPLE LOCAL end radar 5732232 - blocks (C++ co) */
3528 if (!result_type)
3529 {
3530 if (flag_cond_mismatch)
3531 result_type = void_type_node;
3532 else
3533 {
3534 error ("type mismatch in conditional expression");
3535 return error_mark_node;
3536 }
3537 }
3538
3539 /* Merge const and volatile flags of the incoming types. */
3540 result_type
3541 = build_type_variant (result_type,
3542 TREE_READONLY (op1) || TREE_READONLY (op2),
3543 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3544
3545 if (result_type != TREE_TYPE (op1))
3546 op1 = convert_and_check (result_type, op1);
3547 if (result_type != TREE_TYPE (op2))
3548 op2 = convert_and_check (result_type, op2);
3549
3550 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3551 }
3552
3553 /* Return a compound expression that performs two expressions and
3554 returns the value of the second of them. */
3555
3556 tree
build_compound_expr(tree expr1,tree expr2)3557 build_compound_expr (tree expr1, tree expr2)
3558 {
3559 if (!TREE_SIDE_EFFECTS (expr1))
3560 {
3561 /* The left-hand operand of a comma expression is like an expression
3562 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3563 any side-effects, unless it was explicitly cast to (void). */
3564 if (warn_unused_value)
3565 {
3566 if (VOID_TYPE_P (TREE_TYPE (expr1))
3567 && (TREE_CODE (expr1) == NOP_EXPR
3568 || TREE_CODE (expr1) == CONVERT_EXPR))
3569 ; /* (void) a, b */
3570 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3571 && TREE_CODE (expr1) == COMPOUND_EXPR
3572 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3573 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3574 ; /* (void) a, (void) b, c */
3575 else
3576 warning (0, "left-hand operand of comma expression has no effect");
3577 }
3578 }
3579
3580 /* With -Wunused, we should also warn if the left-hand operand does have
3581 side-effects, but computes a value which is not used. For example, in
3582 `foo() + bar(), baz()' the result of the `+' operator is not used,
3583 so we should issue a warning. */
3584 else if (warn_unused_value)
3585 warn_if_unused_value (expr1, input_location);
3586
3587 if (expr2 == error_mark_node)
3588 return error_mark_node;
3589
3590 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3591 }
3592
3593 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3594
3595 tree
build_c_cast(tree type,tree expr)3596 build_c_cast (tree type, tree expr)
3597 {
3598 tree value = expr;
3599
3600 if (type == error_mark_node || expr == error_mark_node)
3601 return error_mark_node;
3602
3603 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3604 only in <protocol> qualifications. But when constructing cast expressions,
3605 the protocols do matter and must be kept around. */
3606 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3607 return build1 (NOP_EXPR, type, expr);
3608
3609 type = TYPE_MAIN_VARIANT (type);
3610
3611 if (TREE_CODE (type) == ARRAY_TYPE)
3612 {
3613 error ("cast specifies array type");
3614 return error_mark_node;
3615 }
3616
3617 if (TREE_CODE (type) == FUNCTION_TYPE)
3618 {
3619 error ("cast specifies function type");
3620 return error_mark_node;
3621 }
3622
3623 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3624 {
3625 if (pedantic)
3626 {
3627 if (TREE_CODE (type) == RECORD_TYPE
3628 || TREE_CODE (type) == UNION_TYPE)
3629 pedwarn ("ISO C forbids casting nonscalar to the same type");
3630 }
3631 }
3632 else if (TREE_CODE (type) == UNION_TYPE)
3633 {
3634 tree field;
3635
3636 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3637 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3638 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3639 break;
3640
3641 if (field)
3642 {
3643 tree t;
3644
3645 if (pedantic)
3646 pedwarn ("ISO C forbids casts to union type");
3647 t = digest_init (type,
3648 build_constructor_single (type, field, value),
3649 true, 0);
3650 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3651 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3652 return t;
3653 }
3654 error ("cast to union type from type not present in union");
3655 return error_mark_node;
3656 }
3657 else
3658 {
3659 tree otype, ovalue;
3660
3661 if (type == void_type_node)
3662 return build1 (CONVERT_EXPR, type, value);
3663
3664 otype = TREE_TYPE (value);
3665
3666 /* APPLE LOCAL begin radar 5732232 - blocks */
3667 if (TREE_CODE (otype) == BLOCK_POINTER_TYPE &&
3668 TREE_CODE (type) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
3669 return build1 (NOP_EXPR, type, value);
3670 /* APPLE LOCAL end radar 5732232 - blocks */
3671
3672 /* Optionally warn about potentially worrisome casts. */
3673
3674 if (warn_cast_qual
3675 && TREE_CODE (type) == POINTER_TYPE
3676 && TREE_CODE (otype) == POINTER_TYPE)
3677 {
3678 tree in_type = type;
3679 tree in_otype = otype;
3680 int added = 0;
3681 int discarded = 0;
3682
3683 /* Check that the qualifiers on IN_TYPE are a superset of
3684 the qualifiers of IN_OTYPE. The outermost level of
3685 POINTER_TYPE nodes is uninteresting and we stop as soon
3686 as we hit a non-POINTER_TYPE node on either type. */
3687 do
3688 {
3689 in_otype = TREE_TYPE (in_otype);
3690 in_type = TREE_TYPE (in_type);
3691
3692 /* GNU C allows cv-qualified function types. 'const'
3693 means the function is very pure, 'volatile' means it
3694 can't return. We need to warn when such qualifiers
3695 are added, not when they're taken away. */
3696 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3697 && TREE_CODE (in_type) == FUNCTION_TYPE)
3698 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3699 else
3700 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3701 }
3702 while (TREE_CODE (in_type) == POINTER_TYPE
3703 && TREE_CODE (in_otype) == POINTER_TYPE);
3704
3705 if (added)
3706 warning (0, "cast adds new qualifiers to function type");
3707
3708 if (discarded)
3709 /* There are qualifiers present in IN_OTYPE that are not
3710 present in IN_TYPE. */
3711 warning (0, "cast discards qualifiers from pointer target type");
3712 }
3713
3714 /* Warn about possible alignment problems. */
3715 if (STRICT_ALIGNMENT
3716 && TREE_CODE (type) == POINTER_TYPE
3717 && TREE_CODE (otype) == POINTER_TYPE
3718 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3719 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3720 /* Don't warn about opaque types, where the actual alignment
3721 restriction is unknown. */
3722 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3723 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3724 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3725 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3726 warning (OPT_Wcast_align,
3727 "cast increases required alignment of target type");
3728
3729 if (TREE_CODE (type) == INTEGER_TYPE
3730 && TREE_CODE (otype) == POINTER_TYPE
3731 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3732 /* Unlike conversion of integers to pointers, where the
3733 warning is disabled for converting constants because
3734 of cases such as SIG_*, warn about converting constant
3735 pointers to integers. In some cases it may cause unwanted
3736 sign extension, and a warning is appropriate. */
3737 warning (OPT_Wpointer_to_int_cast,
3738 "cast from pointer to integer of different size");
3739
3740 if (TREE_CODE (value) == CALL_EXPR
3741 && TREE_CODE (type) != TREE_CODE (otype))
3742 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3743 "to non-matching type %qT", otype, type);
3744
3745 if (TREE_CODE (type) == POINTER_TYPE
3746 && TREE_CODE (otype) == INTEGER_TYPE
3747 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3748 /* Don't warn about converting any constant. */
3749 && !TREE_CONSTANT (value))
3750 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3751 "of different size");
3752
3753 if (warn_strict_aliasing <= 2)
3754 strict_aliasing_warning (otype, type, expr);
3755
3756 /* If pedantic, warn for conversions between function and object
3757 pointer types, except for converting a null pointer constant
3758 to function pointer type. */
3759 if (pedantic
3760 && TREE_CODE (type) == POINTER_TYPE
3761 && TREE_CODE (otype) == POINTER_TYPE
3762 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3763 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3764 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3765
3766 if (pedantic
3767 && TREE_CODE (type) == POINTER_TYPE
3768 && TREE_CODE (otype) == POINTER_TYPE
3769 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3770 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3771 && !null_pointer_constant_p (value))
3772 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3773
3774 ovalue = value;
3775 /* APPLE LOCAL begin don't sign-extend pointers cast to integers */
3776 if (TREE_CODE (type) == INTEGER_TYPE
3777 && TREE_CODE (otype) == POINTER_TYPE
3778 && TYPE_PRECISION (type) > TYPE_PRECISION (otype)
3779 && TYPE_UNSIGNED (type))
3780 value = convert (c_common_type_for_size (POINTER_SIZE, 1), value);
3781 /* APPLE LOCAL end don't sign-extend pointers cast to integers */
3782 value = convert (type, value);
3783
3784 /* Ignore any integer overflow caused by the cast. */
3785 if (TREE_CODE (value) == INTEGER_CST)
3786 {
3787 if (CONSTANT_CLASS_P (ovalue)
3788 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3789 {
3790 /* Avoid clobbering a shared constant. */
3791 value = copy_node (value);
3792 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3793 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3794 }
3795 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3796 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3797 value = build_int_cst_wide (TREE_TYPE (value),
3798 TREE_INT_CST_LOW (value),
3799 TREE_INT_CST_HIGH (value));
3800 }
3801 }
3802
3803 /* Don't let a cast be an lvalue. */
3804 if (value == expr)
3805 value = non_lvalue (value);
3806
3807 return value;
3808 }
3809
3810 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cm) */
3811 static bool
functiontypes_are_block_compatible(tree f1,tree f2)3812 functiontypes_are_block_compatible (tree f1, tree f2)
3813 {
3814 tree arg1, arg2;
3815 if (!types_are_block_compatible (TREE_TYPE (f1), TREE_TYPE (f2)))
3816 return false;
3817 arg1 = TYPE_ARG_TYPES (f1);
3818 arg2 = TYPE_ARG_TYPES (f2);
3819 /* APPLE LOCAL radar 6246965, 6196572 */
3820 return (!arg1) || (type_lists_compatible_p (arg1, arg2) == 1);
3821 }
3822
3823 static bool
types_are_block_compatible(tree lhptee,tree rhptee)3824 types_are_block_compatible (tree lhptee, tree rhptee)
3825 {
3826 if (TYPE_MAIN_VARIANT (lhptee) == TYPE_MAIN_VARIANT (rhptee))
3827 return true;
3828 if (TREE_CODE (lhptee) == FUNCTION_TYPE && TREE_CODE (rhptee) == FUNCTION_TYPE)
3829 return functiontypes_are_block_compatible (lhptee, rhptee);
3830 /* APPLE LOCAL begin radar 5882266 (C++ cm) */
3831 if (TREE_CODE (lhptee) == POINTER_TYPE && TREE_CODE (rhptee) == POINTER_TYPE)
3832 return types_are_block_compatible (TREE_TYPE (lhptee), TREE_TYPE (rhptee));
3833 /* APPLE LOCAL end radar 5882266 (C++ cm) */
3834 /* APPLE LOCAL begin radar 5988995 (C++ cm) */
3835 if (TREE_CODE (lhptee) == BLOCK_POINTER_TYPE
3836 && TREE_CODE (rhptee) == BLOCK_POINTER_TYPE)
3837 return types_are_block_compatible (TREE_TYPE (lhptee), TREE_TYPE (rhptee));
3838 /* APPLE LOCAL end radar 5988995 (C++ cm) */
3839 return false;
3840 }
3841
3842 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
3843 /**
3844 build_block_call - Routine to build a block call; as in:
3845 ((double(*)(void *, int))(BLOCK_PTR_EXP->__FuncPtr))(I, 42);
3846 FNTYPE is the original function type derived from the syntax.
3847 BLOCK_PTR_EXP is the block pointer variable.
3848 PARAMS is the parameter list.
3849 */
3850 static tree
build_block_call(tree fntype,tree block_ptr_exp,tree params)3851 build_block_call (tree fntype, tree block_ptr_exp, tree params)
3852 {
3853 tree function_ptr_exp;
3854 tree typelist;
3855 bool block_ptr_exp_side_effect = TREE_SIDE_EFFECTS (block_ptr_exp);
3856
3857 /* First convert BLOCK_PTR_EXP to 'void *'. */
3858 block_ptr_exp = convert (ptr_type_node, block_ptr_exp);
3859 gcc_assert (generic_block_literal_struct_type);
3860 block_ptr_exp = convert (build_pointer_type (generic_block_literal_struct_type),
3861 block_ptr_exp);
3862 if (block_ptr_exp_side_effect)
3863 block_ptr_exp = save_expr (block_ptr_exp);
3864
3865 /* BLOCK_PTR_VAR->__FuncPtr */
3866 function_ptr_exp = build_component_ref (build_indirect_ref (block_ptr_exp, "->"),
3867 get_identifier ("__FuncPtr"));
3868 gcc_assert (function_ptr_exp);
3869
3870 /* Build: result_type(*)(void *, function-arg-type-list) */
3871 typelist = TYPE_ARG_TYPES (fntype);
3872 typelist = tree_cons (NULL_TREE, ptr_type_node, typelist);
3873 fntype = build_function_type (TREE_TYPE (fntype), typelist);
3874 function_ptr_exp = convert (build_pointer_type (fntype), function_ptr_exp);
3875 params = tree_cons (NULL_TREE, block_ptr_exp, params);
3876 return fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
3877 function_ptr_exp, params, NULL_TREE);
3878 }
3879 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
3880 /* APPLE LOCAL end radar 5732232 - blocks (C++ cm) */
3881
3882 /* Interpret a cast of expression EXPR to type TYPE. */
3883 tree
c_cast_expr(struct c_type_name * type_name,tree expr)3884 c_cast_expr (struct c_type_name *type_name, tree expr)
3885 {
3886 tree type;
3887 int saved_wsp = warn_strict_prototypes;
3888
3889 /* This avoids warnings about unprototyped casts on
3890 integers. E.g. "#define SIG_DFL (void(*)())0". */
3891 if (TREE_CODE (expr) == INTEGER_CST)
3892 warn_strict_prototypes = 0;
3893 type = groktypename (type_name);
3894 warn_strict_prototypes = saved_wsp;
3895
3896 return build_c_cast (type, expr);
3897 }
3898
3899 /* Build an assignment expression of lvalue LHS from value RHS.
3900 MODIFYCODE is the code for a binary operator that we use
3901 to combine the old value of LHS with RHS to get the new value.
3902 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3903
3904 tree
build_modify_expr(tree lhs,enum tree_code modifycode,tree rhs)3905 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3906 {
3907 tree result;
3908 tree newrhs;
3909 tree lhstype = TREE_TYPE (lhs);
3910 tree olhstype = lhstype;
3911 /* APPLE LOCAL __block assign sequence point 6639533 */
3912 bool insert_sequence_point = false;
3913
3914 /* APPLE LOCAL begin radar 4426814 */
3915 if (c_dialect_objc () && flag_objc_gc)
3916 {
3917 /* APPLE LOCAL radar 5276085 */
3918 objc_weak_reference_expr (&lhs);
3919 lhstype = TREE_TYPE (lhs);
3920 olhstype = lhstype;
3921 }
3922 /* APPLE LOCAL end radar 4426814 */
3923
3924 /* Types that aren't fully specified cannot be used in assignments. */
3925 lhs = require_complete_type (lhs);
3926
3927 /* Avoid duplicate error messages from operands that had errors. */
3928 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3929 return error_mark_node;
3930
3931 /* APPLE LOCAL radar 5285911 */
3932 if (!lvalue_or_else (lhs, lv_assign))
3933 return error_mark_node;
3934
3935 STRIP_TYPE_NOPS (rhs);
3936
3937 /* APPLE LOCAL begin __block assign sequence point 6639533 */
3938 /* For byref = x;, we have to transform this into {( typeof(x) x' =
3939 x; byref = x`; )} to ensure there is a sequence point before the
3940 evaluation of the byref, inorder to ensure that the access
3941 expression for byref doesn't start running before x is evaluated,
3942 as it will access the __forwarding pointer and that must be done
3943 after x is evaluated. */
3944 /* First we check to see if lhs is a byref... byrefs look like:
3945 __Block_byref_X.__forwarding->x */
3946 if (TREE_CODE (lhs) == COMPONENT_REF)
3947 {
3948 tree inner = TREE_OPERAND (lhs, 0);
3949 /* now check for -> */
3950 if (TREE_CODE (inner) == INDIRECT_REF)
3951 {
3952 inner = TREE_OPERAND (inner, 0);
3953 if (TREE_CODE (inner) == COMPONENT_REF)
3954 {
3955 inner = TREE_OPERAND (inner, 0);
3956 if (TREE_CODE (inner) == VAR_DECL
3957 && COPYABLE_BYREF_LOCAL_VAR (inner))
3958 {
3959 tree old_rhs = rhs;
3960 /* then we save the rhs. */
3961 rhs = save_expr (rhs);
3962 if (rhs != old_rhs)
3963 /* And arrange for the sequence point to be inserted. */
3964 insert_sequence_point = true;
3965 }
3966 }
3967 }
3968 }
3969 /* APPLE LOCAL end __block assign sequence point 6639533 */
3970
3971 newrhs = rhs;
3972
3973 /* If a binary op has been requested, combine the old LHS value with the RHS
3974 producing the value we should actually store into the LHS. */
3975
3976 if (modifycode != NOP_EXPR)
3977 {
3978 lhs = stabilize_reference (lhs);
3979 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3980 }
3981
3982 /* APPLE LOCAL begin C* property (Radar 4436866) */
3983 if (c_dialect_objc ())
3984 {
3985 result = objc_build_setter_call (lhs, newrhs);
3986 if (result)
3987 /* APPLE LOCAL begin __block assign sequence point 6639533 */
3988 {
3989 if (insert_sequence_point)
3990 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), build1 (NOP_EXPR, void_type_node, rhs), result);
3991 return result;
3992 }
3993 /* APPLE LOCAL end __block assign sequence point 6639533 */
3994 }
3995 /* APPLE LOCAL end C* property (Radar 4436866) */
3996
3997 /* Give an error for storing in something that is 'const'. */
3998
3999 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4000 || ((TREE_CODE (lhstype) == RECORD_TYPE
4001 || TREE_CODE (lhstype) == UNION_TYPE)
4002 && C_TYPE_FIELDS_READONLY (lhstype)))
4003 {
4004 readonly_error (lhs, lv_assign);
4005 return error_mark_node;
4006 }
4007
4008 /* If storing into a structure or union member,
4009 it has probably been given type `int'.
4010 Compute the type that would go with
4011 the actual amount of storage the member occupies. */
4012
4013 if (TREE_CODE (lhs) == COMPONENT_REF
4014 && (TREE_CODE (lhstype) == INTEGER_TYPE
4015 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4016 || TREE_CODE (lhstype) == REAL_TYPE
4017 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4018 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4019
4020 /* If storing in a field that is in actuality a short or narrower than one,
4021 we must store in the field in its actual type. */
4022
4023 if (lhstype != TREE_TYPE (lhs))
4024 {
4025 lhs = copy_node (lhs);
4026 TREE_TYPE (lhs) = lhstype;
4027 }
4028
4029 /* Convert new value to destination type. */
4030
4031 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
4032 NULL_TREE, NULL_TREE, 0);
4033 if (TREE_CODE (newrhs) == ERROR_MARK)
4034 return error_mark_node;
4035
4036 /* Emit ObjC write barrier, if necessary. */
4037 if (c_dialect_objc () && flag_objc_gc)
4038 {
4039 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4040 if (result)
4041 /* APPLE LOCAL begin __block assign sequence point 6639533 */
4042 {
4043 if (insert_sequence_point)
4044 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), build1 (NOP_EXPR, void_type_node, rhs), result);
4045 return result;
4046 }
4047 /* APPLE LOCAL end __block assign sequence point 6639533 */
4048 }
4049
4050 /* Scan operands. */
4051
4052 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4053 TREE_SIDE_EFFECTS (result) = 1;
4054
4055 /* APPLE LOCAL begin __block assign sequence point 6639533 */
4056 if (insert_sequence_point)
4057 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), build1 (NOP_EXPR, void_type_node, rhs), result);
4058 /* APPLE LOCAL end __block assign sequence point 6639533 */
4059
4060 /* If we got the LHS in a different type for storing in,
4061 convert the result back to the nominal type of LHS
4062 so that the value we return always has the same type
4063 as the LHS argument. */
4064
4065 if (olhstype == TREE_TYPE (result))
4066 return result;
4067 return convert_for_assignment (olhstype, result, ic_assign,
4068 NULL_TREE, NULL_TREE, 0);
4069 }
4070
4071 /* Convert value RHS to type TYPE as preparation for an assignment
4072 to an lvalue of type TYPE.
4073 The real work of conversion is done by `convert'.
4074 The purpose of this function is to generate error messages
4075 for assignments that are not allowed in C.
4076 ERRTYPE says whether it is argument passing, assignment,
4077 initialization or return.
4078
4079 FUNCTION is a tree for the function being called.
4080 PARMNUM is the number of the argument, for printing in error messages. */
4081
4082 static tree
convert_for_assignment(tree type,tree rhs,enum impl_conv errtype,tree fundecl,tree function,int parmnum)4083 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
4084 tree fundecl, tree function, int parmnum)
4085 {
4086 enum tree_code codel = TREE_CODE (type);
4087 tree rhstype;
4088 enum tree_code coder;
4089 tree rname = NULL_TREE;
4090 bool objc_ok = false;
4091
4092 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
4093 {
4094 tree selector;
4095 /* Change pointer to function to the function itself for
4096 diagnostics. */
4097 if (TREE_CODE (function) == ADDR_EXPR
4098 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4099 function = TREE_OPERAND (function, 0);
4100
4101 /* Handle an ObjC selector specially for diagnostics. */
4102 selector = objc_message_selector ();
4103 rname = function;
4104 if (selector && parmnum > 2)
4105 {
4106 rname = selector;
4107 parmnum -= 2;
4108 }
4109 }
4110
4111 /* This macro is used to emit diagnostics to ensure that all format
4112 strings are complete sentences, visible to gettext and checked at
4113 compile time. */
4114 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
4115 do { \
4116 switch (errtype) \
4117 { \
4118 case ic_argpass: \
4119 pedwarn (AR, parmnum, rname); \
4120 break; \
4121 case ic_argpass_nonproto: \
4122 warning (0, AR, parmnum, rname); \
4123 break; \
4124 case ic_assign: \
4125 pedwarn (AS); \
4126 break; \
4127 case ic_init: \
4128 pedwarn (IN); \
4129 break; \
4130 case ic_return: \
4131 pedwarn (RE); \
4132 break; \
4133 default: \
4134 gcc_unreachable (); \
4135 } \
4136 } while (0)
4137
4138 STRIP_TYPE_NOPS (rhs);
4139
4140 if (optimize && TREE_CODE (rhs) == VAR_DECL
4141 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
4142 rhs = decl_constant_value_for_broken_optimization (rhs);
4143
4144 rhstype = TREE_TYPE (rhs);
4145 coder = TREE_CODE (rhstype);
4146
4147 if (coder == ERROR_MARK)
4148 return error_mark_node;
4149
4150 if (c_dialect_objc ())
4151 {
4152 int parmno;
4153
4154 switch (errtype)
4155 {
4156 case ic_return:
4157 parmno = 0;
4158 break;
4159
4160 case ic_assign:
4161 parmno = -1;
4162 break;
4163
4164 case ic_init:
4165 parmno = -2;
4166 break;
4167
4168 default:
4169 parmno = parmnum;
4170 break;
4171 }
4172
4173 /* APPLE LOCAL radar 6231433 */
4174 objc_ok = objc_compare_types (type, rhstype, parmno, rname, "comparison");
4175 }
4176
4177 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4178 return rhs;
4179
4180 if (coder == VOID_TYPE)
4181 {
4182 /* Except for passing an argument to an unprototyped function,
4183 this is a constraint violation. When passing an argument to
4184 an unprototyped function, it is compile-time undefined;
4185 making it a constraint in that case was rejected in
4186 DR#252. */
4187 error ("void value not ignored as it ought to be");
4188 return error_mark_node;
4189 }
4190 /* A type converts to a reference to it.
4191 This code doesn't fully support references, it's just for the
4192 special case of va_start and va_copy. */
4193 if (codel == REFERENCE_TYPE
4194 /* APPLE LOCAL begin radar 4502186 */
4195 && comptypes (objc_non_volatilized_type (TREE_TYPE (type)),
4196 objc_non_volatilized_type (TREE_TYPE (rhs))) == 1)
4197 /* APPLE LOCAL end radar 4502186 */
4198 {
4199 if (!lvalue_p (rhs))
4200 {
4201 error ("cannot pass rvalue to reference parameter");
4202 return error_mark_node;
4203 }
4204 if (!c_mark_addressable (rhs))
4205 return error_mark_node;
4206 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4207
4208 /* We already know that these two types are compatible, but they
4209 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4210 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4211 likely to be va_list, a typedef to __builtin_va_list, which
4212 is different enough that it will cause problems later. */
4213 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4214 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4215
4216 rhs = build1 (NOP_EXPR, type, rhs);
4217 return rhs;
4218 }
4219 /* Some types can interconvert without explicit casts. */
4220 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4221 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4222 return convert (type, rhs);
4223 /* Arithmetic types all interconvert, and enum is treated like int. */
4224 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4225 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4226 || codel == BOOLEAN_TYPE)
4227 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4228 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4229 || coder == BOOLEAN_TYPE))
4230 return convert_and_check (type, rhs);
4231
4232 /* Aggregates in different TUs might need conversion. */
4233 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
4234 && codel == coder
4235 && comptypes (type, rhstype))
4236 return convert_and_check (type, rhs);
4237
4238 /* Conversion to a transparent union from its member types.
4239 This applies only to function arguments. */
4240 if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
4241 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
4242 {
4243 tree memb, marginal_memb = NULL_TREE;
4244
4245 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
4246 {
4247 tree memb_type = TREE_TYPE (memb);
4248
4249 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4250 TYPE_MAIN_VARIANT (rhstype)))
4251 break;
4252
4253 if (TREE_CODE (memb_type) != POINTER_TYPE)
4254 continue;
4255
4256 if (coder == POINTER_TYPE)
4257 {
4258 tree ttl = TREE_TYPE (memb_type);
4259 tree ttr = TREE_TYPE (rhstype);
4260
4261 /* Any non-function converts to a [const][volatile] void *
4262 and vice versa; otherwise, targets must be the same.
4263 Meanwhile, the lhs target must have all the qualifiers of
4264 the rhs. */
4265 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4266 || comp_target_types (memb_type, rhstype))
4267 {
4268 /* If this type won't generate any warnings, use it. */
4269 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4270 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4271 && TREE_CODE (ttl) == FUNCTION_TYPE)
4272 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4273 == TYPE_QUALS (ttr))
4274 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4275 == TYPE_QUALS (ttl))))
4276 break;
4277
4278 /* Keep looking for a better type, but remember this one. */
4279 if (!marginal_memb)
4280 marginal_memb = memb;
4281 }
4282 }
4283
4284 /* Can convert integer zero to any pointer type. */
4285 if (null_pointer_constant_p (rhs))
4286 {
4287 rhs = null_pointer_node;
4288 break;
4289 }
4290 }
4291
4292 if (memb || marginal_memb)
4293 {
4294 if (!memb)
4295 {
4296 /* We have only a marginally acceptable member type;
4297 it needs a warning. */
4298 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4299 tree ttr = TREE_TYPE (rhstype);
4300
4301 /* Const and volatile mean something different for function
4302 types, so the usual warnings are not appropriate. */
4303 if (TREE_CODE (ttr) == FUNCTION_TYPE
4304 && TREE_CODE (ttl) == FUNCTION_TYPE)
4305 {
4306 /* Because const and volatile on functions are
4307 restrictions that say the function will not do
4308 certain things, it is okay to use a const or volatile
4309 function where an ordinary one is wanted, but not
4310 vice-versa. */
4311 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4312 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
4313 "makes qualified function "
4314 "pointer from unqualified"),
4315 G_("assignment makes qualified "
4316 "function pointer from "
4317 "unqualified"),
4318 G_("initialization makes qualified "
4319 "function pointer from "
4320 "unqualified"),
4321 G_("return makes qualified function "
4322 "pointer from unqualified"));
4323 }
4324 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4325 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4326 "qualifiers from pointer target type"),
4327 G_("assignment discards qualifiers "
4328 "from pointer target type"),
4329 G_("initialization discards qualifiers "
4330 "from pointer target type"),
4331 G_("return discards qualifiers from "
4332 "pointer target type"));
4333
4334 memb = marginal_memb;
4335 }
4336
4337 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4338 pedwarn ("ISO C prohibits argument conversion to union type");
4339
4340 return build_constructor_single (type, memb, rhs);
4341 }
4342 }
4343
4344 /* Conversions among pointers */
4345 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4346 && (coder == codel))
4347 {
4348 /* APPLE LOCAL begin radar 4193359 */
4349 /* Types differing only by the presence of the 'volatile'
4350 qualifier are acceptable if the 'volatile' has been added
4351 in by the Objective-C EH machinery. */
4352 tree ttl = objc_non_volatilized_type (TREE_TYPE (type));
4353 tree ttr = objc_non_volatilized_type (TREE_TYPE (rhstype));
4354 /* APPLE LOCAL end radar 4193359 */
4355 tree mvl = ttl;
4356 tree mvr = ttr;
4357 bool is_opaque_pointer;
4358 int target_cmp = 0; /* Cache comp_target_types () result. */
4359
4360 if (TREE_CODE (mvl) != ARRAY_TYPE)
4361 mvl = TYPE_MAIN_VARIANT (mvl);
4362 if (TREE_CODE (mvr) != ARRAY_TYPE)
4363 mvr = TYPE_MAIN_VARIANT (mvr);
4364 /* Opaque pointers are treated like void pointers. */
4365 is_opaque_pointer = (targetm.vector_opaque_p (type)
4366 || targetm.vector_opaque_p (rhstype))
4367 && TREE_CODE (ttl) == VECTOR_TYPE
4368 && TREE_CODE (ttr) == VECTOR_TYPE;
4369
4370 /* C++ does not allow the implicit conversion void* -> T*. However,
4371 for the purpose of reducing the number of false positives, we
4372 tolerate the special case of
4373
4374 int *p = NULL;
4375
4376 where NULL is typically defined in C to be '(void *) 0'. */
4377 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4378 warning (OPT_Wc___compat, "request for implicit conversion from "
4379 "%qT to %qT not permitted in C++", rhstype, type);
4380
4381 /* Check if the right-hand side has a format attribute but the
4382 left-hand side doesn't. */
4383 if (warn_missing_format_attribute
4384 && check_missing_format_attribute (type, rhstype))
4385 {
4386 switch (errtype)
4387 {
4388 case ic_argpass:
4389 case ic_argpass_nonproto:
4390 warning (OPT_Wmissing_format_attribute,
4391 "argument %d of %qE might be "
4392 "a candidate for a format attribute",
4393 parmnum, rname);
4394 break;
4395 case ic_assign:
4396 warning (OPT_Wmissing_format_attribute,
4397 "assignment left-hand side might be "
4398 "a candidate for a format attribute");
4399 break;
4400 case ic_init:
4401 warning (OPT_Wmissing_format_attribute,
4402 "initialization left-hand side might be "
4403 "a candidate for a format attribute");
4404 break;
4405 case ic_return:
4406 warning (OPT_Wmissing_format_attribute,
4407 "return type might be "
4408 "a candidate for a format attribute");
4409 break;
4410 default:
4411 gcc_unreachable ();
4412 }
4413 }
4414
4415 /* Any non-function converts to a [const][volatile] void *
4416 and vice versa; otherwise, targets must be the same.
4417 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4418 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4419 || (target_cmp = comp_target_types (type, rhstype))
4420 || is_opaque_pointer
4421 || (c_common_unsigned_type (mvl)
4422 == c_common_unsigned_type (mvr)))
4423 {
4424 if (pedantic
4425 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4426 ||
4427 (VOID_TYPE_P (ttr)
4428 && !null_pointer_constant_p (rhs)
4429 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4430 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4431 "%qE between function pointer "
4432 "and %<void *%>"),
4433 G_("ISO C forbids assignment between "
4434 "function pointer and %<void *%>"),
4435 G_("ISO C forbids initialization between "
4436 "function pointer and %<void *%>"),
4437 G_("ISO C forbids return between function "
4438 "pointer and %<void *%>"));
4439 /* Const and volatile mean something different for function types,
4440 so the usual warnings are not appropriate. */
4441 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4442 && TREE_CODE (ttl) != FUNCTION_TYPE)
4443 {
4444 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4445 {
4446 /* APPLE LOCAL begin radar 4193359 */
4447 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4448 "qualifiers from pointer target type"),
4449 G_("assignment discards qualifiers "
4450 "from pointer target type"),
4451 G_("initialization discards qualifiers "
4452 "from pointer target type"),
4453 G_("return discards qualifiers from "
4454 "pointer target type"));
4455 }
4456 /* If this is not a case of ignoring a mismatch in signedness,
4457 no warning. */
4458 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4459 || target_cmp)
4460 ;
4461 /* If there is a mismatch, do warn. */
4462 else if (warn_pointer_sign)
4463 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4464 "%d of %qE differ in signedness"),
4465 G_("pointer targets in assignment "
4466 "differ in signedness"),
4467 G_("pointer targets in initialization "
4468 "differ in signedness"),
4469 G_("pointer targets in return differ "
4470 "in signedness"));
4471 }
4472 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4473 && TREE_CODE (ttr) == FUNCTION_TYPE)
4474 {
4475 /* Because const and volatile on functions are restrictions
4476 that say the function will not do certain things,
4477 it is okay to use a const or volatile function
4478 where an ordinary one is wanted, but not vice-versa. */
4479 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4480 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4481 "qualified function pointer "
4482 "from unqualified"),
4483 G_("assignment makes qualified function "
4484 "pointer from unqualified"),
4485 G_("initialization makes qualified "
4486 "function pointer from unqualified"),
4487 G_("return makes qualified function "
4488 "pointer from unqualified"));
4489 }
4490 }
4491 else
4492 /* Avoid warning about the volatile ObjC EH puts on decls. */
4493 if (!objc_ok)
4494 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4495 "incompatible pointer type"),
4496 G_("assignment from incompatible pointer type"),
4497 G_("initialization from incompatible "
4498 "pointer type"),
4499 G_("return from incompatible pointer type"));
4500
4501 return convert (type, rhs);
4502 }
4503 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4504 {
4505 /* ??? This should not be an error when inlining calls to
4506 unprototyped functions. */
4507 error ("invalid use of non-lvalue array");
4508 return error_mark_node;
4509 }
4510 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4511 {
4512 /* An explicit constant 0 can convert to a pointer,
4513 or one that results from arithmetic, even including
4514 a cast to integer type. */
4515 if (!null_pointer_constant_p (rhs))
4516 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4517 "pointer from integer without a cast"),
4518 G_("assignment makes pointer from integer "
4519 "without a cast"),
4520 G_("initialization makes pointer from "
4521 "integer without a cast"),
4522 G_("return makes pointer from integer "
4523 "without a cast"));
4524
4525 return convert (type, rhs);
4526 }
4527 /* APPLE LOCAL begin radar 5732232 - blocks */
4528 else if (codel == BLOCK_POINTER_TYPE && coder == INTEGER_TYPE)
4529 {
4530 if (!null_pointer_constant_p (rhs))
4531 {
4532 error("invalid conversion %s integer 'int', expected block pointer",
4533 errtype == ic_assign ? "assigning" : "initializing");
4534 return error_mark_node;
4535 }
4536 return build_int_cst (type, 0);
4537 }
4538 else if (codel == BLOCK_POINTER_TYPE && coder == codel)
4539 {
4540 tree lhptee = TREE_TYPE (type);
4541 tree rhptee = TREE_TYPE(rhstype);
4542 if (lhptee == rhptee)
4543 return rhs;
4544 if (!types_are_block_compatible (lhptee, rhptee))
4545 {
4546 error ("incompatible block pointer types %s %qT, expected %qT",
4547 errtype == ic_assign ? "assigning" : "initializing",
4548 rhstype, type);
4549 return error_mark_node;
4550 }
4551 return rhs;
4552 }
4553 /* APPLE LOCAL begin radar 5831855 */
4554 /* APPLE LOCAL radar 5878380 */
4555 else if (codel == BLOCK_POINTER_TYPE && POINTER_TYPE_P (rhstype) &&
4556 (VOID_TYPE_P (TREE_TYPE (rhstype)) || objc_is_id (rhstype)))
4557 return convert (type, rhs);
4558 /* APPLE LOCAL radar 5878380 */
4559 else if (coder == BLOCK_POINTER_TYPE && POINTER_TYPE_P (type) &&
4560 (VOID_TYPE_P (TREE_TYPE (type)) || objc_is_id (type)))
4561 /* APPLE LOCAL end radar 5831855 */
4562 return convert (type, rhs);
4563 /* APPLE LOCAL end radar 5732232 - blocks */
4564 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4565 {
4566 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4567 "from pointer without a cast"),
4568 G_("assignment makes integer from pointer "
4569 "without a cast"),
4570 G_("initialization makes integer from pointer "
4571 "without a cast"),
4572 G_("return makes integer from pointer "
4573 "without a cast"));
4574 return convert (type, rhs);
4575 }
4576 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4577 return convert (type, rhs);
4578
4579 switch (errtype)
4580 {
4581 case ic_argpass:
4582 case ic_argpass_nonproto:
4583 /* ??? This should not be an error when inlining calls to
4584 unprototyped functions. */
4585 error ("convert_for_assignment: incompatible type for argument %d of %qE", parmnum, rname);
4586 break;
4587 case ic_assign:
4588 error ("incompatible types in assignment");
4589 break;
4590 case ic_init:
4591 error ("incompatible types in initialization");
4592 break;
4593 case ic_return:
4594 error ("incompatible types in return");
4595 break;
4596 default:
4597 gcc_unreachable ();
4598 }
4599
4600 return error_mark_node;
4601 }
4602
4603 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4604 is used for error and warning reporting and indicates which argument
4605 is being processed. */
4606
4607 tree
c_convert_parm_for_inlining(tree parm,tree value,tree fn,int argnum)4608 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4609 {
4610 tree ret, type;
4611
4612 /* If FN was prototyped at the call site, the value has been converted
4613 already in convert_arguments.
4614 However, we might see a prototype now that was not in place when
4615 the function call was seen, so check that the VALUE actually matches
4616 PARM before taking an early exit. */
4617 if (!value
4618 || (TYPE_ARG_TYPES (TREE_TYPE (fn))
4619 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
4620 == TYPE_MAIN_VARIANT (TREE_TYPE (value)))))
4621 return value;
4622
4623 type = TREE_TYPE (parm);
4624 ret = convert_for_assignment (type, value,
4625 ic_argpass_nonproto, fn,
4626 fn, argnum);
4627 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4628 && INTEGRAL_TYPE_P (type)
4629 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4630 ret = default_conversion (ret);
4631 return ret;
4632 }
4633
4634 /* If VALUE is a compound expr all of whose expressions are constant, then
4635 return its value. Otherwise, return error_mark_node.
4636
4637 This is for handling COMPOUND_EXPRs as initializer elements
4638 which is allowed with a warning when -pedantic is specified. */
4639
4640 static tree
valid_compound_expr_initializer(tree value,tree endtype)4641 valid_compound_expr_initializer (tree value, tree endtype)
4642 {
4643 if (TREE_CODE (value) == COMPOUND_EXPR)
4644 {
4645 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4646 == error_mark_node)
4647 return error_mark_node;
4648 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4649 endtype);
4650 }
4651 else if (!initializer_constant_valid_p (value, endtype))
4652 return error_mark_node;
4653 else
4654 return value;
4655 }
4656
4657 /* Perform appropriate conversions on the initial value of a variable,
4658 store it in the declaration DECL,
4659 and print any error messages that are appropriate.
4660 If the init is invalid, store an ERROR_MARK. */
4661
4662 void
store_init_value(tree decl,tree init)4663 store_init_value (tree decl, tree init)
4664 {
4665 tree value, type;
4666
4667 /* If variable's type was invalidly declared, just ignore it. */
4668
4669 type = TREE_TYPE (decl);
4670 if (TREE_CODE (type) == ERROR_MARK)
4671 return;
4672
4673 /* Digest the specified initializer into an expression. */
4674
4675 value = digest_init (type, init, true, TREE_STATIC (decl));
4676
4677 /* Store the expression if valid; else report error. */
4678
4679 if (!in_system_header
4680 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4681 warning (OPT_Wtraditional, "traditional C rejects automatic "
4682 "aggregate initialization");
4683
4684 DECL_INITIAL (decl) = value;
4685
4686 /* ANSI wants warnings about out-of-range constant initializers. */
4687 STRIP_TYPE_NOPS (value);
4688 constant_expression_warning (value);
4689
4690 /* Check if we need to set array size from compound literal size. */
4691 if (TREE_CODE (type) == ARRAY_TYPE
4692 && TYPE_DOMAIN (type) == 0
4693 && value != error_mark_node)
4694 {
4695 tree inside_init = init;
4696
4697 STRIP_TYPE_NOPS (inside_init);
4698 inside_init = fold (inside_init);
4699
4700 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4701 {
4702 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4703
4704 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
4705 {
4706 /* For int foo[] = (int [3]){1}; we need to set array size
4707 now since later on array initializer will be just the
4708 brace enclosed list of the compound literal. */
4709 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4710 TREE_TYPE (decl) = type;
4711 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
4712 layout_type (type);
4713 layout_decl (cldecl, 0);
4714 }
4715 }
4716 }
4717 }
4718
4719 /* Methods for storing and printing names for error messages. */
4720
4721 /* Implement a spelling stack that allows components of a name to be pushed
4722 and popped. Each element on the stack is this structure. */
4723
4724 struct spelling
4725 {
4726 int kind;
4727 union
4728 {
4729 unsigned HOST_WIDE_INT i;
4730 const char *s;
4731 } u;
4732 };
4733
4734 #define SPELLING_STRING 1
4735 #define SPELLING_MEMBER 2
4736 #define SPELLING_BOUNDS 3
4737
4738 static struct spelling *spelling; /* Next stack element (unused). */
4739 static struct spelling *spelling_base; /* Spelling stack base. */
4740 static int spelling_size; /* Size of the spelling stack. */
4741
4742 /* Macros to save and restore the spelling stack around push_... functions.
4743 Alternative to SAVE_SPELLING_STACK. */
4744
4745 #define SPELLING_DEPTH() (spelling - spelling_base)
4746 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4747
4748 /* Push an element on the spelling stack with type KIND and assign VALUE
4749 to MEMBER. */
4750
4751 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4752 { \
4753 int depth = SPELLING_DEPTH (); \
4754 \
4755 if (depth >= spelling_size) \
4756 { \
4757 spelling_size += 10; \
4758 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4759 spelling_size); \
4760 RESTORE_SPELLING_DEPTH (depth); \
4761 } \
4762 \
4763 spelling->kind = (KIND); \
4764 spelling->MEMBER = (VALUE); \
4765 spelling++; \
4766 }
4767
4768 /* Push STRING on the stack. Printed literally. */
4769
4770 static void
push_string(const char * string)4771 push_string (const char *string)
4772 {
4773 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4774 }
4775
4776 /* Push a member name on the stack. Printed as '.' STRING. */
4777
4778 static void
push_member_name(tree decl)4779 push_member_name (tree decl)
4780 {
4781 const char *const string
4782 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4783 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4784 }
4785
4786 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4787
4788 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)4789 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4790 {
4791 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4792 }
4793
4794 /* Compute the maximum size in bytes of the printed spelling. */
4795
4796 static int
spelling_length(void)4797 spelling_length (void)
4798 {
4799 int size = 0;
4800 struct spelling *p;
4801
4802 for (p = spelling_base; p < spelling; p++)
4803 {
4804 if (p->kind == SPELLING_BOUNDS)
4805 size += 25;
4806 else
4807 size += strlen (p->u.s) + 1;
4808 }
4809
4810 return size;
4811 }
4812
4813 /* Print the spelling to BUFFER and return it. */
4814
4815 static char *
print_spelling(char * buffer)4816 print_spelling (char *buffer)
4817 {
4818 char *d = buffer;
4819 struct spelling *p;
4820
4821 for (p = spelling_base; p < spelling; p++)
4822 if (p->kind == SPELLING_BOUNDS)
4823 {
4824 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4825 d += strlen (d);
4826 }
4827 else
4828 {
4829 const char *s;
4830 if (p->kind == SPELLING_MEMBER)
4831 *d++ = '.';
4832 for (s = p->u.s; (*d = *s++); d++)
4833 ;
4834 }
4835 *d++ = '\0';
4836 return buffer;
4837 }
4838
4839 /* Issue an error message for a bad initializer component.
4840 MSGID identifies the message.
4841 The component name is taken from the spelling stack. */
4842
4843 void
error_init(const char * msgid)4844 error_init (const char *msgid)
4845 {
4846 char *ofwhat;
4847
4848 error ("%s", _(msgid));
4849 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4850 if (*ofwhat)
4851 error ("(near initialization for %qs)", ofwhat);
4852 }
4853
4854 /* Issue a pedantic warning for a bad initializer component.
4855 MSGID identifies the message.
4856 The component name is taken from the spelling stack. */
4857
4858 void
pedwarn_init(const char * msgid)4859 pedwarn_init (const char *msgid)
4860 {
4861 char *ofwhat;
4862
4863 pedwarn ("%s", _(msgid));
4864 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4865 if (*ofwhat)
4866 pedwarn ("(near initialization for %qs)", ofwhat);
4867 }
4868
4869 /* Issue a warning for a bad initializer component.
4870 MSGID identifies the message.
4871 The component name is taken from the spelling stack. */
4872
4873 static void
warning_init(const char * msgid)4874 warning_init (const char *msgid)
4875 {
4876 char *ofwhat;
4877
4878 warning (0, "%s", _(msgid));
4879 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4880 if (*ofwhat)
4881 warning (0, "(near initialization for %qs)", ofwhat);
4882 }
4883
4884 /* If TYPE is an array type and EXPR is a parenthesized string
4885 constant, warn if pedantic that EXPR is being used to initialize an
4886 object of type TYPE. */
4887
4888 void
maybe_warn_string_init(tree type,struct c_expr expr)4889 maybe_warn_string_init (tree type, struct c_expr expr)
4890 {
4891 if (pedantic
4892 && TREE_CODE (type) == ARRAY_TYPE
4893 && TREE_CODE (expr.value) == STRING_CST
4894 && expr.original_code != STRING_CST)
4895 pedwarn_init ("array initialized from parenthesized string constant");
4896 }
4897
4898 /* APPLE LOCAL begin radar 5932809 - copyable byref blocks */
do_digest_init(tree type,tree init)4899 tree do_digest_init (tree type, tree init)
4900 {
4901 return digest_init (type, init, true, false);
4902 }
4903 /* APPLE LOCAL end radar 5932809 - copyable byref blocks */
4904
4905 /* Digest the parser output INIT as an initializer for type TYPE.
4906 Return a C expression of type TYPE to represent the initial value.
4907
4908 If INIT is a string constant, STRICT_STRING is true if it is
4909 unparenthesized or we should not warn here for it being parenthesized.
4910 For other types of INIT, STRICT_STRING is not used.
4911
4912 REQUIRE_CONSTANT requests an error if non-constant initializers or
4913 elements are seen. */
4914
4915 static tree
digest_init(tree type,tree init,bool strict_string,int require_constant)4916 digest_init (tree type, tree init, bool strict_string, int require_constant)
4917 {
4918 enum tree_code code = TREE_CODE (type);
4919 tree inside_init = init;
4920
4921 if (type == error_mark_node
4922 || !init
4923 || init == error_mark_node
4924 || TREE_TYPE (init) == error_mark_node)
4925 return error_mark_node;
4926
4927 STRIP_TYPE_NOPS (inside_init);
4928
4929 inside_init = fold (inside_init);
4930
4931 /* Initialization of an array of chars from a string constant
4932 optionally enclosed in braces. */
4933
4934 if (code == ARRAY_TYPE && inside_init
4935 && TREE_CODE (inside_init) == STRING_CST)
4936 {
4937 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4938 /* Note that an array could be both an array of character type
4939 and an array of wchar_t if wchar_t is signed char or unsigned
4940 char. */
4941 bool char_array = (typ1 == char_type_node
4942 || typ1 == signed_char_type_node
4943 || typ1 == unsigned_char_type_node);
4944 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4945 if (char_array || wchar_array)
4946 {
4947 struct c_expr expr;
4948 bool char_string;
4949 expr.value = inside_init;
4950 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4951 maybe_warn_string_init (type, expr);
4952
4953 char_string
4954 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4955 == char_type_node);
4956
4957 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4958 TYPE_MAIN_VARIANT (type)))
4959 return inside_init;
4960
4961 if (!wchar_array && !char_string)
4962 {
4963 error_init ("char-array initialized from wide string");
4964 return error_mark_node;
4965 }
4966 if (char_string && !char_array)
4967 {
4968 error_init ("wchar_t-array initialized from non-wide string");
4969 return error_mark_node;
4970 }
4971
4972 TREE_TYPE (inside_init) = type;
4973 if (TYPE_DOMAIN (type) != 0
4974 && TYPE_SIZE (type) != 0
4975 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4976 /* Subtract 1 (or sizeof (wchar_t))
4977 because it's ok to ignore the terminating null char
4978 that is counted in the length of the constant. */
4979 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4980 TREE_STRING_LENGTH (inside_init)
4981 - ((TYPE_PRECISION (typ1)
4982 != TYPE_PRECISION (char_type_node))
4983 ? (TYPE_PRECISION (wchar_type_node)
4984 / BITS_PER_UNIT)
4985 : 1)))
4986 pedwarn_init ("initializer-string for array of chars is too long");
4987
4988 return inside_init;
4989 }
4990 else if (INTEGRAL_TYPE_P (typ1))
4991 {
4992 error_init ("array of inappropriate type initialized "
4993 "from string constant");
4994 return error_mark_node;
4995 }
4996 }
4997
4998 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4999 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5000 below and handle as a constructor. */
5001 if (code == VECTOR_TYPE
5002 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5003 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5004 && TREE_CONSTANT (inside_init))
5005 {
5006 if (TREE_CODE (inside_init) == VECTOR_CST
5007 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5008 TYPE_MAIN_VARIANT (type)))
5009 return inside_init;
5010
5011 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5012 {
5013 unsigned HOST_WIDE_INT ix;
5014 tree value;
5015 bool constant_p = true;
5016
5017 /* Iterate through elements and check if all constructor
5018 elements are *_CSTs. */
5019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5020 if (!CONSTANT_CLASS_P (value))
5021 {
5022 constant_p = false;
5023 break;
5024 }
5025
5026 if (constant_p)
5027 return build_vector_from_ctor (type,
5028 CONSTRUCTOR_ELTS (inside_init));
5029 }
5030 }
5031
5032 /* Any type can be initialized
5033 from an expression of the same type, optionally with braces. */
5034
5035 if (inside_init && TREE_TYPE (inside_init) != 0
5036 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5037 TYPE_MAIN_VARIANT (type))
5038 || (code == ARRAY_TYPE
5039 && comptypes (TREE_TYPE (inside_init), type))
5040 || (code == VECTOR_TYPE
5041 && comptypes (TREE_TYPE (inside_init), type))
5042 || (code == POINTER_TYPE
5043 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5044 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5045 TREE_TYPE (type)))))
5046 {
5047 if (code == POINTER_TYPE)
5048 {
5049 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5050 {
5051 if (TREE_CODE (inside_init) == STRING_CST
5052 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5053 inside_init = array_to_pointer_conversion (inside_init);
5054 else
5055 {
5056 error_init ("invalid use of non-lvalue array");
5057 return error_mark_node;
5058 }
5059 }
5060 }
5061
5062 if (code == VECTOR_TYPE)
5063 /* Although the types are compatible, we may require a
5064 conversion. */
5065 inside_init = convert (type, inside_init);
5066
5067 if (require_constant
5068 && (code == VECTOR_TYPE || !flag_isoc99)
5069 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5070 {
5071 /* As an extension, allow initializing objects with static storage
5072 duration with compound literals (which are then treated just as
5073 the brace enclosed list they contain). Also allow this for
5074 vectors, as we can only assign them with compound literals. */
5075 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5076 inside_init = DECL_INITIAL (decl);
5077 }
5078
5079 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5080 && TREE_CODE (inside_init) != CONSTRUCTOR)
5081 {
5082 error_init ("array initialized from non-constant array expression");
5083 return error_mark_node;
5084 }
5085
5086 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
5087 inside_init = decl_constant_value_for_broken_optimization (inside_init);
5088
5089 /* Compound expressions can only occur here if -pedantic or
5090 -pedantic-errors is specified. In the later case, we always want
5091 an error. In the former case, we simply want a warning. */
5092 if (require_constant && pedantic
5093 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5094 {
5095 inside_init
5096 = valid_compound_expr_initializer (inside_init,
5097 TREE_TYPE (inside_init));
5098 if (inside_init == error_mark_node)
5099 error_init ("initializer element is not constant");
5100 else
5101 pedwarn_init ("initializer element is not constant");
5102 if (flag_pedantic_errors)
5103 inside_init = error_mark_node;
5104 }
5105 else if (require_constant
5106 && !initializer_constant_valid_p (inside_init,
5107 TREE_TYPE (inside_init)))
5108 {
5109 error_init ("initializer element is not constant");
5110 inside_init = error_mark_node;
5111 }
5112
5113 /* Added to enable additional -Wmissing-format-attribute warnings. */
5114 /* APPLE LOCAL begin radar 5822844 */
5115 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE ||
5116 TREE_CODE (TREE_TYPE (inside_init)) == BLOCK_POINTER_TYPE)
5117 /* APPLE LOCAL end radar 5822844 */
5118 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
5119 NULL_TREE, 0);
5120 return inside_init;
5121 }
5122
5123 /* Handle scalar types, including conversions. */
5124
5125 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
5126 /* APPLE LOCAL radar 5732232 - blocks */
5127 || code == BLOCK_POINTER_TYPE
5128 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
5129 || code == VECTOR_TYPE)
5130 {
5131 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5132 && (TREE_CODE (init) == STRING_CST
5133 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5134 init = array_to_pointer_conversion (init);
5135 inside_init
5136 = convert_for_assignment (type, init, ic_init,
5137 NULL_TREE, NULL_TREE, 0);
5138
5139 /* Check to see if we have already given an error message. */
5140 if (inside_init == error_mark_node)
5141 ;
5142 else if (require_constant && !TREE_CONSTANT (inside_init))
5143 {
5144 error_init ("initializer element is not constant");
5145 inside_init = error_mark_node;
5146 }
5147 else if (require_constant
5148 && !initializer_constant_valid_p (inside_init,
5149 TREE_TYPE (inside_init)))
5150 {
5151 error_init ("initializer element is not computable at load time");
5152 inside_init = error_mark_node;
5153 }
5154
5155 return inside_init;
5156 }
5157
5158 /* Come here only for records and arrays. */
5159
5160 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5161 {
5162 error_init ("variable-sized object may not be initialized");
5163 return error_mark_node;
5164 }
5165
5166 error_init ("invalid initializer");
5167 return error_mark_node;
5168 }
5169
5170 /* Handle initializers that use braces. */
5171
5172 /* Type of object we are accumulating a constructor for.
5173 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5174 static tree constructor_type;
5175
5176 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5177 left to fill. */
5178 static tree constructor_fields;
5179
5180 /* For an ARRAY_TYPE, this is the specified index
5181 at which to store the next element we get. */
5182 static tree constructor_index;
5183
5184 /* For an ARRAY_TYPE, this is the maximum index. */
5185 static tree constructor_max_index;
5186
5187 /* For a RECORD_TYPE, this is the first field not yet written out. */
5188 static tree constructor_unfilled_fields;
5189
5190 /* For an ARRAY_TYPE, this is the index of the first element
5191 not yet written out. */
5192 static tree constructor_unfilled_index;
5193
5194 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5195 This is so we can generate gaps between fields, when appropriate. */
5196 static tree constructor_bit_index;
5197
5198 /* If we are saving up the elements rather than allocating them,
5199 this is the list of elements so far (in reverse order,
5200 most recent first). */
5201 static VEC(constructor_elt,gc) *constructor_elements;
5202
5203 /* 1 if constructor should be incrementally stored into a constructor chain,
5204 0 if all the elements should be kept in AVL tree. */
5205 static int constructor_incremental;
5206
5207 /* 1 if so far this constructor's elements are all compile-time constants. */
5208 static int constructor_constant;
5209
5210 /* 1 if so far this constructor's elements are all valid address constants. */
5211 static int constructor_simple;
5212
5213 /* 1 if this constructor is erroneous so far. */
5214 static int constructor_erroneous;
5215
5216 /* Structure for managing pending initializer elements, organized as an
5217 AVL tree. */
5218
5219 struct init_node
5220 {
5221 struct init_node *left, *right;
5222 struct init_node *parent;
5223 int balance;
5224 tree purpose;
5225 tree value;
5226 };
5227
5228 /* Tree of pending elements at this constructor level.
5229 These are elements encountered out of order
5230 which belong at places we haven't reached yet in actually
5231 writing the output.
5232 Will never hold tree nodes across GC runs. */
5233 static struct init_node *constructor_pending_elts;
5234
5235 /* The SPELLING_DEPTH of this constructor. */
5236 static int constructor_depth;
5237
5238 /* DECL node for which an initializer is being read.
5239 0 means we are reading a constructor expression
5240 such as (struct foo) {...}. */
5241 static tree constructor_decl;
5242
5243 /* Nonzero if this is an initializer for a top-level decl. */
5244 static int constructor_top_level;
5245
5246 /* Nonzero if there were any member designators in this initializer. */
5247 static int constructor_designated;
5248
5249 /* Nesting depth of designator list. */
5250 static int designator_depth;
5251
5252 /* Nonzero if there were diagnosed errors in this designator list. */
5253 static int designator_erroneous;
5254
5255
5256 /* This stack has a level for each implicit or explicit level of
5257 structuring in the initializer, including the outermost one. It
5258 saves the values of most of the variables above. */
5259
5260 struct constructor_range_stack;
5261
5262 struct constructor_stack
5263 {
5264 struct constructor_stack *next;
5265 tree type;
5266 tree fields;
5267 tree index;
5268 tree max_index;
5269 tree unfilled_index;
5270 tree unfilled_fields;
5271 tree bit_index;
5272 VEC(constructor_elt,gc) *elements;
5273 struct init_node *pending_elts;
5274 int offset;
5275 int depth;
5276 /* If value nonzero, this value should replace the entire
5277 constructor at this level. */
5278 struct c_expr replacement_value;
5279 struct constructor_range_stack *range_stack;
5280 char constant;
5281 char simple;
5282 char implicit;
5283 char erroneous;
5284 char outer;
5285 char incremental;
5286 char designated;
5287 };
5288
5289 static struct constructor_stack *constructor_stack;
5290
5291 /* This stack represents designators from some range designator up to
5292 the last designator in the list. */
5293
5294 struct constructor_range_stack
5295 {
5296 struct constructor_range_stack *next, *prev;
5297 struct constructor_stack *stack;
5298 tree range_start;
5299 tree index;
5300 tree range_end;
5301 tree fields;
5302 };
5303
5304 static struct constructor_range_stack *constructor_range_stack;
5305
5306 /* This stack records separate initializers that are nested.
5307 Nested initializers can't happen in ANSI C, but GNU C allows them
5308 in cases like { ... (struct foo) { ... } ... }. */
5309
5310 struct initializer_stack
5311 {
5312 struct initializer_stack *next;
5313 tree decl;
5314 struct constructor_stack *constructor_stack;
5315 struct constructor_range_stack *constructor_range_stack;
5316 VEC(constructor_elt,gc) *elements;
5317 struct spelling *spelling;
5318 struct spelling *spelling_base;
5319 int spelling_size;
5320 char top_level;
5321 char require_constant_value;
5322 char require_constant_elements;
5323 };
5324
5325 static struct initializer_stack *initializer_stack;
5326
5327 /* Prepare to parse and output the initializer for variable DECL. */
5328
5329 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level)5330 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5331 {
5332 const char *locus;
5333 struct initializer_stack *p = XNEW (struct initializer_stack);
5334
5335 p->decl = constructor_decl;
5336 p->require_constant_value = require_constant_value;
5337 p->require_constant_elements = require_constant_elements;
5338 p->constructor_stack = constructor_stack;
5339 p->constructor_range_stack = constructor_range_stack;
5340 p->elements = constructor_elements;
5341 p->spelling = spelling;
5342 p->spelling_base = spelling_base;
5343 p->spelling_size = spelling_size;
5344 p->top_level = constructor_top_level;
5345 p->next = initializer_stack;
5346 initializer_stack = p;
5347
5348 constructor_decl = decl;
5349 constructor_designated = 0;
5350 constructor_top_level = top_level;
5351
5352 if (decl != 0 && decl != error_mark_node)
5353 {
5354 require_constant_value = TREE_STATIC (decl);
5355 require_constant_elements
5356 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5357 /* For a scalar, you can always use any value to initialize,
5358 even within braces. */
5359 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5360 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5361 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5362 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5363 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5364 }
5365 else
5366 {
5367 require_constant_value = 0;
5368 require_constant_elements = 0;
5369 locus = "(anonymous)";
5370 }
5371
5372 constructor_stack = 0;
5373 constructor_range_stack = 0;
5374
5375 missing_braces_mentioned = 0;
5376
5377 spelling_base = 0;
5378 spelling_size = 0;
5379 RESTORE_SPELLING_DEPTH (0);
5380
5381 if (locus)
5382 push_string (locus);
5383 }
5384
5385 void
finish_init(void)5386 finish_init (void)
5387 {
5388 struct initializer_stack *p = initializer_stack;
5389
5390 /* Free the whole constructor stack of this initializer. */
5391 while (constructor_stack)
5392 {
5393 struct constructor_stack *q = constructor_stack;
5394 constructor_stack = q->next;
5395 free (q);
5396 }
5397
5398 gcc_assert (!constructor_range_stack);
5399
5400 /* Pop back to the data of the outer initializer (if any). */
5401 free (spelling_base);
5402
5403 constructor_decl = p->decl;
5404 require_constant_value = p->require_constant_value;
5405 require_constant_elements = p->require_constant_elements;
5406 constructor_stack = p->constructor_stack;
5407 constructor_range_stack = p->constructor_range_stack;
5408 constructor_elements = p->elements;
5409 spelling = p->spelling;
5410 spelling_base = p->spelling_base;
5411 spelling_size = p->spelling_size;
5412 constructor_top_level = p->top_level;
5413 initializer_stack = p->next;
5414 free (p);
5415 }
5416
5417 /* Call here when we see the initializer is surrounded by braces.
5418 This is instead of a call to push_init_level;
5419 it is matched by a call to pop_init_level.
5420
5421 TYPE is the type to initialize, for a constructor expression.
5422 For an initializer for a decl, TYPE is zero. */
5423
5424 void
really_start_incremental_init(tree type)5425 really_start_incremental_init (tree type)
5426 {
5427 struct constructor_stack *p = XNEW (struct constructor_stack);
5428
5429 if (type == 0)
5430 type = TREE_TYPE (constructor_decl);
5431
5432 if (targetm.vector_opaque_p (type))
5433 error ("opaque vector types cannot be initialized");
5434
5435 p->type = constructor_type;
5436 p->fields = constructor_fields;
5437 p->index = constructor_index;
5438 p->max_index = constructor_max_index;
5439 p->unfilled_index = constructor_unfilled_index;
5440 p->unfilled_fields = constructor_unfilled_fields;
5441 p->bit_index = constructor_bit_index;
5442 p->elements = constructor_elements;
5443 p->constant = constructor_constant;
5444 p->simple = constructor_simple;
5445 p->erroneous = constructor_erroneous;
5446 p->pending_elts = constructor_pending_elts;
5447 p->depth = constructor_depth;
5448 p->replacement_value.value = 0;
5449 p->replacement_value.original_code = ERROR_MARK;
5450 p->implicit = 0;
5451 p->range_stack = 0;
5452 p->outer = 0;
5453 p->incremental = constructor_incremental;
5454 p->designated = constructor_designated;
5455 p->next = 0;
5456 constructor_stack = p;
5457
5458 constructor_constant = 1;
5459 constructor_simple = 1;
5460 constructor_depth = SPELLING_DEPTH ();
5461 constructor_elements = 0;
5462 constructor_pending_elts = 0;
5463 constructor_type = type;
5464 constructor_incremental = 1;
5465 constructor_designated = 0;
5466 designator_depth = 0;
5467 designator_erroneous = 0;
5468
5469 if (TREE_CODE (constructor_type) == RECORD_TYPE
5470 || TREE_CODE (constructor_type) == UNION_TYPE)
5471 {
5472 constructor_fields = TYPE_FIELDS (constructor_type);
5473 /* Skip any nameless bit fields at the beginning. */
5474 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5475 && DECL_NAME (constructor_fields) == 0)
5476 constructor_fields = TREE_CHAIN (constructor_fields);
5477
5478 constructor_unfilled_fields = constructor_fields;
5479 constructor_bit_index = bitsize_zero_node;
5480 }
5481 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5482 {
5483 if (TYPE_DOMAIN (constructor_type))
5484 {
5485 constructor_max_index
5486 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5487
5488 /* Detect non-empty initializations of zero-length arrays. */
5489 if (constructor_max_index == NULL_TREE
5490 && TYPE_SIZE (constructor_type))
5491 constructor_max_index = build_int_cst (NULL_TREE, -1);
5492
5493 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5494 to initialize VLAs will cause a proper error; avoid tree
5495 checking errors as well by setting a safe value. */
5496 if (constructor_max_index
5497 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5498 constructor_max_index = build_int_cst (NULL_TREE, -1);
5499
5500 constructor_index
5501 = convert (bitsizetype,
5502 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5503 }
5504 else
5505 {
5506 constructor_index = bitsize_zero_node;
5507 constructor_max_index = NULL_TREE;
5508 }
5509
5510 constructor_unfilled_index = constructor_index;
5511 }
5512 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5513 {
5514 /* Vectors are like simple fixed-size arrays. */
5515 constructor_max_index =
5516 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5517 constructor_index = bitsize_zero_node;
5518 constructor_unfilled_index = constructor_index;
5519 }
5520 else
5521 {
5522 /* Handle the case of int x = {5}; */
5523 constructor_fields = constructor_type;
5524 constructor_unfilled_fields = constructor_type;
5525 }
5526 }
5527
5528 /* Push down into a subobject, for initialization.
5529 If this is for an explicit set of braces, IMPLICIT is 0.
5530 If it is because the next element belongs at a lower level,
5531 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5532
5533 void
push_init_level(int implicit)5534 push_init_level (int implicit)
5535 {
5536 struct constructor_stack *p;
5537 tree value = NULL_TREE;
5538
5539 /* If we've exhausted any levels that didn't have braces,
5540 pop them now. If implicit == 1, this will have been done in
5541 process_init_element; do not repeat it here because in the case
5542 of excess initializers for an empty aggregate this leads to an
5543 infinite cycle of popping a level and immediately recreating
5544 it. */
5545 if (implicit != 1)
5546 {
5547 while (constructor_stack->implicit)
5548 {
5549 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5550 || TREE_CODE (constructor_type) == UNION_TYPE)
5551 && constructor_fields == 0)
5552 process_init_element (pop_init_level (1));
5553 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5554 && constructor_max_index
5555 && tree_int_cst_lt (constructor_max_index,
5556 constructor_index))
5557 process_init_element (pop_init_level (1));
5558 else
5559 break;
5560 }
5561 }
5562
5563 /* Unless this is an explicit brace, we need to preserve previous
5564 content if any. */
5565 if (implicit)
5566 {
5567 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5568 || TREE_CODE (constructor_type) == UNION_TYPE)
5569 && constructor_fields)
5570 value = find_init_member (constructor_fields);
5571 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5572 value = find_init_member (constructor_index);
5573 }
5574
5575 p = XNEW (struct constructor_stack);
5576 p->type = constructor_type;
5577 p->fields = constructor_fields;
5578 p->index = constructor_index;
5579 p->max_index = constructor_max_index;
5580 p->unfilled_index = constructor_unfilled_index;
5581 p->unfilled_fields = constructor_unfilled_fields;
5582 p->bit_index = constructor_bit_index;
5583 p->elements = constructor_elements;
5584 p->constant = constructor_constant;
5585 p->simple = constructor_simple;
5586 p->erroneous = constructor_erroneous;
5587 p->pending_elts = constructor_pending_elts;
5588 p->depth = constructor_depth;
5589 p->replacement_value.value = 0;
5590 p->replacement_value.original_code = ERROR_MARK;
5591 p->implicit = implicit;
5592 p->outer = 0;
5593 p->incremental = constructor_incremental;
5594 p->designated = constructor_designated;
5595 p->next = constructor_stack;
5596 p->range_stack = 0;
5597 constructor_stack = p;
5598
5599 constructor_constant = 1;
5600 constructor_simple = 1;
5601 constructor_depth = SPELLING_DEPTH ();
5602 constructor_elements = 0;
5603 constructor_incremental = 1;
5604 constructor_designated = 0;
5605 constructor_pending_elts = 0;
5606 if (!implicit)
5607 {
5608 p->range_stack = constructor_range_stack;
5609 constructor_range_stack = 0;
5610 designator_depth = 0;
5611 designator_erroneous = 0;
5612 }
5613
5614 /* Don't die if an entire brace-pair level is superfluous
5615 in the containing level. */
5616 if (constructor_type == 0)
5617 ;
5618 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5619 || TREE_CODE (constructor_type) == UNION_TYPE)
5620 {
5621 /* Don't die if there are extra init elts at the end. */
5622 if (constructor_fields == 0)
5623 constructor_type = 0;
5624 else
5625 {
5626 constructor_type = TREE_TYPE (constructor_fields);
5627 push_member_name (constructor_fields);
5628 constructor_depth++;
5629 }
5630 }
5631 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5632 {
5633 constructor_type = TREE_TYPE (constructor_type);
5634 push_array_bounds (tree_low_cst (constructor_index, 1));
5635 constructor_depth++;
5636 }
5637
5638 if (constructor_type == 0)
5639 {
5640 error_init ("extra brace group at end of initializer");
5641 constructor_fields = 0;
5642 constructor_unfilled_fields = 0;
5643 return;
5644 }
5645
5646 if (value && TREE_CODE (value) == CONSTRUCTOR)
5647 {
5648 constructor_constant = TREE_CONSTANT (value);
5649 constructor_simple = TREE_STATIC (value);
5650 constructor_elements = CONSTRUCTOR_ELTS (value);
5651 if (!VEC_empty (constructor_elt, constructor_elements)
5652 && (TREE_CODE (constructor_type) == RECORD_TYPE
5653 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5654 set_nonincremental_init ();
5655 }
5656
5657 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5658 {
5659 missing_braces_mentioned = 1;
5660 warning_init ("missing braces around initializer");
5661 }
5662
5663 if (TREE_CODE (constructor_type) == RECORD_TYPE
5664 || TREE_CODE (constructor_type) == UNION_TYPE)
5665 {
5666 constructor_fields = TYPE_FIELDS (constructor_type);
5667 /* Skip any nameless bit fields at the beginning. */
5668 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5669 && DECL_NAME (constructor_fields) == 0)
5670 constructor_fields = TREE_CHAIN (constructor_fields);
5671
5672 constructor_unfilled_fields = constructor_fields;
5673 constructor_bit_index = bitsize_zero_node;
5674 }
5675 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5676 {
5677 /* Vectors are like simple fixed-size arrays. */
5678 constructor_max_index =
5679 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5680 constructor_index = convert (bitsizetype, integer_zero_node);
5681 constructor_unfilled_index = constructor_index;
5682 }
5683 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5684 {
5685 if (TYPE_DOMAIN (constructor_type))
5686 {
5687 constructor_max_index
5688 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5689
5690 /* Detect non-empty initializations of zero-length arrays. */
5691 if (constructor_max_index == NULL_TREE
5692 && TYPE_SIZE (constructor_type))
5693 constructor_max_index = build_int_cst (NULL_TREE, -1);
5694
5695 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5696 to initialize VLAs will cause a proper error; avoid tree
5697 checking errors as well by setting a safe value. */
5698 if (constructor_max_index
5699 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5700 constructor_max_index = build_int_cst (NULL_TREE, -1);
5701
5702 constructor_index
5703 = convert (bitsizetype,
5704 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5705 }
5706 else
5707 constructor_index = bitsize_zero_node;
5708
5709 constructor_unfilled_index = constructor_index;
5710 if (value && TREE_CODE (value) == STRING_CST)
5711 {
5712 /* We need to split the char/wchar array into individual
5713 characters, so that we don't have to special case it
5714 everywhere. */
5715 set_nonincremental_init_from_string (value);
5716 }
5717 }
5718 else
5719 {
5720 if (constructor_type != error_mark_node)
5721 warning_init ("braces around scalar initializer");
5722 constructor_fields = constructor_type;
5723 constructor_unfilled_fields = constructor_type;
5724 }
5725 }
5726
5727 /* At the end of an implicit or explicit brace level,
5728 finish up that level of constructor. If a single expression
5729 with redundant braces initialized that level, return the
5730 c_expr structure for that expression. Otherwise, the original_code
5731 element is set to ERROR_MARK.
5732 If we were outputting the elements as they are read, return 0 as the value
5733 from inner levels (process_init_element ignores that),
5734 but return error_mark_node as the value from the outermost level
5735 (that's what we want to put in DECL_INITIAL).
5736 Otherwise, return a CONSTRUCTOR expression as the value. */
5737
5738 struct c_expr
pop_init_level(int implicit)5739 pop_init_level (int implicit)
5740 {
5741 struct constructor_stack *p;
5742 struct c_expr ret;
5743 ret.value = 0;
5744 ret.original_code = ERROR_MARK;
5745
5746 if (implicit == 0)
5747 {
5748 /* When we come to an explicit close brace,
5749 pop any inner levels that didn't have explicit braces. */
5750 while (constructor_stack->implicit)
5751 process_init_element (pop_init_level (1));
5752
5753 gcc_assert (!constructor_range_stack);
5754 }
5755
5756 /* Now output all pending elements. */
5757 constructor_incremental = 1;
5758 output_pending_init_elements (1);
5759
5760 p = constructor_stack;
5761
5762 /* Error for initializing a flexible array member, or a zero-length
5763 array member in an inappropriate context. */
5764 if (constructor_type && constructor_fields
5765 && TREE_CODE (constructor_type) == ARRAY_TYPE
5766 && TYPE_DOMAIN (constructor_type)
5767 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5768 {
5769 /* Silently discard empty initializations. The parser will
5770 already have pedwarned for empty brackets. */
5771 if (integer_zerop (constructor_unfilled_index))
5772 constructor_type = NULL_TREE;
5773 else
5774 {
5775 gcc_assert (!TYPE_SIZE (constructor_type));
5776
5777 if (constructor_depth > 2)
5778 error_init ("initialization of flexible array member in a nested context");
5779 else if (pedantic)
5780 pedwarn_init ("initialization of a flexible array member");
5781
5782 /* We have already issued an error message for the existence
5783 of a flexible array member not at the end of the structure.
5784 Discard the initializer so that we do not die later. */
5785 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5786 constructor_type = NULL_TREE;
5787 }
5788 }
5789
5790 /* Warn when some struct elements are implicitly initialized to zero. */
5791 if (warn_missing_field_initializers
5792 && constructor_type
5793 && TREE_CODE (constructor_type) == RECORD_TYPE
5794 && constructor_unfilled_fields)
5795 {
5796 /* Do not warn for flexible array members or zero-length arrays. */
5797 while (constructor_unfilled_fields
5798 && (!DECL_SIZE (constructor_unfilled_fields)
5799 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5800 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5801
5802 /* Do not warn if this level of the initializer uses member
5803 designators; it is likely to be deliberate. */
5804 if (constructor_unfilled_fields && !constructor_designated)
5805 {
5806 push_member_name (constructor_unfilled_fields);
5807 warning_init ("missing initializer");
5808 RESTORE_SPELLING_DEPTH (constructor_depth);
5809 }
5810 }
5811
5812 /* Pad out the end of the structure. */
5813 if (p->replacement_value.value)
5814 /* If this closes a superfluous brace pair,
5815 just pass out the element between them. */
5816 ret = p->replacement_value;
5817 else if (constructor_type == 0)
5818 ;
5819 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5820 && TREE_CODE (constructor_type) != UNION_TYPE
5821 && TREE_CODE (constructor_type) != ARRAY_TYPE
5822 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5823 {
5824 /* A nonincremental scalar initializer--just return
5825 the element, after verifying there is just one. */
5826 if (VEC_empty (constructor_elt,constructor_elements))
5827 {
5828 if (!constructor_erroneous)
5829 error_init ("empty scalar initializer");
5830 ret.value = error_mark_node;
5831 }
5832 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5833 {
5834 error_init ("extra elements in scalar initializer");
5835 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5836 }
5837 else
5838 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5839 }
5840 else
5841 {
5842 if (constructor_erroneous)
5843 ret.value = error_mark_node;
5844 /* APPLE LOCAL begin radar 4188876 */
5845 else if (!constructor_constant
5846 && TREE_CODE (constructor_type) == VECTOR_TYPE && constructor_decl
5847 && (TREE_CODE (TREE_TYPE (constructor_decl)) == RECORD_TYPE
5848 || TREE_CODE (TREE_TYPE (constructor_decl)) == UNION_TYPE))
5849 {
5850 error ("Initializer is a non-const vector type");
5851 ret.value = error_mark_node;
5852 }
5853 /* APPLE LOCAL end radar 4188876 */
5854 else
5855 {
5856 ret.value = build_constructor (constructor_type,
5857 constructor_elements);
5858 if (constructor_constant)
5859 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5860 if (constructor_constant && constructor_simple)
5861 TREE_STATIC (ret.value) = 1;
5862 }
5863 }
5864
5865 constructor_type = p->type;
5866 constructor_fields = p->fields;
5867 constructor_index = p->index;
5868 constructor_max_index = p->max_index;
5869 constructor_unfilled_index = p->unfilled_index;
5870 constructor_unfilled_fields = p->unfilled_fields;
5871 constructor_bit_index = p->bit_index;
5872 constructor_elements = p->elements;
5873 constructor_constant = p->constant;
5874 constructor_simple = p->simple;
5875 constructor_erroneous = p->erroneous;
5876 constructor_incremental = p->incremental;
5877 constructor_designated = p->designated;
5878 constructor_pending_elts = p->pending_elts;
5879 constructor_depth = p->depth;
5880 if (!p->implicit)
5881 constructor_range_stack = p->range_stack;
5882 RESTORE_SPELLING_DEPTH (constructor_depth);
5883
5884 constructor_stack = p->next;
5885 free (p);
5886
5887 if (ret.value == 0 && constructor_stack == 0)
5888 ret.value = error_mark_node;
5889 return ret;
5890 }
5891
5892 /* Common handling for both array range and field name designators.
5893 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5894
5895 static int
set_designator(int array)5896 set_designator (int array)
5897 {
5898 tree subtype;
5899 enum tree_code subcode;
5900
5901 /* Don't die if an entire brace-pair level is superfluous
5902 in the containing level. */
5903 if (constructor_type == 0)
5904 return 1;
5905
5906 /* If there were errors in this designator list already, bail out
5907 silently. */
5908 if (designator_erroneous)
5909 return 1;
5910
5911 if (!designator_depth)
5912 {
5913 gcc_assert (!constructor_range_stack);
5914
5915 /* Designator list starts at the level of closest explicit
5916 braces. */
5917 while (constructor_stack->implicit)
5918 process_init_element (pop_init_level (1));
5919 constructor_designated = 1;
5920 return 0;
5921 }
5922
5923 switch (TREE_CODE (constructor_type))
5924 {
5925 case RECORD_TYPE:
5926 case UNION_TYPE:
5927 subtype = TREE_TYPE (constructor_fields);
5928 if (subtype != error_mark_node)
5929 subtype = TYPE_MAIN_VARIANT (subtype);
5930 break;
5931 case ARRAY_TYPE:
5932 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5933 break;
5934 default:
5935 gcc_unreachable ();
5936 }
5937
5938 subcode = TREE_CODE (subtype);
5939 if (array && subcode != ARRAY_TYPE)
5940 {
5941 error_init ("array index in non-array initializer");
5942 return 1;
5943 }
5944 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5945 {
5946 error_init ("field name not in record or union initializer");
5947 return 1;
5948 }
5949
5950 constructor_designated = 1;
5951 push_init_level (2);
5952 return 0;
5953 }
5954
5955 /* If there are range designators in designator list, push a new designator
5956 to constructor_range_stack. RANGE_END is end of such stack range or
5957 NULL_TREE if there is no range designator at this level. */
5958
5959 static void
push_range_stack(tree range_end)5960 push_range_stack (tree range_end)
5961 {
5962 struct constructor_range_stack *p;
5963
5964 p = GGC_NEW (struct constructor_range_stack);
5965 p->prev = constructor_range_stack;
5966 p->next = 0;
5967 p->fields = constructor_fields;
5968 p->range_start = constructor_index;
5969 p->index = constructor_index;
5970 p->stack = constructor_stack;
5971 p->range_end = range_end;
5972 if (constructor_range_stack)
5973 constructor_range_stack->next = p;
5974 constructor_range_stack = p;
5975 }
5976
5977 /* Within an array initializer, specify the next index to be initialized.
5978 FIRST is that index. If LAST is nonzero, then initialize a range
5979 of indices, running from FIRST through LAST. */
5980
5981 void
set_init_index(tree first,tree last)5982 set_init_index (tree first, tree last)
5983 {
5984 if (set_designator (1))
5985 return;
5986
5987 designator_erroneous = 1;
5988
5989 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5990 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5991 {
5992 error_init ("array index in initializer not of integer type");
5993 return;
5994 }
5995
5996 if (TREE_CODE (first) != INTEGER_CST)
5997 error_init ("nonconstant array index in initializer");
5998 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5999 error_init ("nonconstant array index in initializer");
6000 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6001 error_init ("array index in non-array initializer");
6002 else if (tree_int_cst_sgn (first) == -1)
6003 error_init ("array index in initializer exceeds array bounds");
6004 else if (constructor_max_index
6005 && tree_int_cst_lt (constructor_max_index, first))
6006 error_init ("array index in initializer exceeds array bounds");
6007 else
6008 {
6009 constructor_index = convert (bitsizetype, first);
6010
6011 if (last)
6012 {
6013 if (tree_int_cst_equal (first, last))
6014 last = 0;
6015 else if (tree_int_cst_lt (last, first))
6016 {
6017 error_init ("empty index range in initializer");
6018 last = 0;
6019 }
6020 else
6021 {
6022 last = convert (bitsizetype, last);
6023 if (constructor_max_index != 0
6024 && tree_int_cst_lt (constructor_max_index, last))
6025 {
6026 error_init ("array index range in initializer exceeds array bounds");
6027 last = 0;
6028 }
6029 }
6030 }
6031
6032 designator_depth++;
6033 designator_erroneous = 0;
6034 if (constructor_range_stack || last)
6035 push_range_stack (last);
6036 }
6037 }
6038
6039 /* Within a struct initializer, specify the next field to be initialized. */
6040
6041 void
set_init_label(tree fieldname)6042 set_init_label (tree fieldname)
6043 {
6044 tree tail;
6045
6046 if (set_designator (0))
6047 return;
6048
6049 designator_erroneous = 1;
6050
6051 if (TREE_CODE (constructor_type) != RECORD_TYPE
6052 && TREE_CODE (constructor_type) != UNION_TYPE)
6053 {
6054 error_init ("field name not in record or union initializer");
6055 return;
6056 }
6057
6058 for (tail = TYPE_FIELDS (constructor_type); tail;
6059 tail = TREE_CHAIN (tail))
6060 {
6061 if (DECL_NAME (tail) == fieldname)
6062 break;
6063 }
6064
6065 if (tail == 0)
6066 error ("unknown field %qE specified in initializer", fieldname);
6067 else
6068 {
6069 constructor_fields = tail;
6070 designator_depth++;
6071 designator_erroneous = 0;
6072 if (constructor_range_stack)
6073 push_range_stack (NULL_TREE);
6074 }
6075 }
6076
6077 /* Add a new initializer to the tree of pending initializers. PURPOSE
6078 identifies the initializer, either array index or field in a structure.
6079 VALUE is the value of that index or field. */
6080
6081 static void
add_pending_init(tree purpose,tree value)6082 add_pending_init (tree purpose, tree value)
6083 {
6084 struct init_node *p, **q, *r;
6085
6086 q = &constructor_pending_elts;
6087 p = 0;
6088
6089 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6090 {
6091 while (*q != 0)
6092 {
6093 p = *q;
6094 if (tree_int_cst_lt (purpose, p->purpose))
6095 q = &p->left;
6096 else if (tree_int_cst_lt (p->purpose, purpose))
6097 q = &p->right;
6098 else
6099 {
6100 if (TREE_SIDE_EFFECTS (p->value))
6101 warning_init ("initialized field with side-effects overwritten");
6102 else if (warn_override_init)
6103 warning_init ("initialized field overwritten");
6104 p->value = value;
6105 return;
6106 }
6107 }
6108 }
6109 else
6110 {
6111 tree bitpos;
6112
6113 bitpos = bit_position (purpose);
6114 while (*q != NULL)
6115 {
6116 p = *q;
6117 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6118 q = &p->left;
6119 else if (p->purpose != purpose)
6120 q = &p->right;
6121 else
6122 {
6123 if (TREE_SIDE_EFFECTS (p->value))
6124 warning_init ("initialized field with side-effects overwritten");
6125 else if (warn_override_init)
6126 warning_init ("initialized field overwritten");
6127 p->value = value;
6128 return;
6129 }
6130 }
6131 }
6132
6133 r = GGC_NEW (struct init_node);
6134 r->purpose = purpose;
6135 r->value = value;
6136
6137 *q = r;
6138 r->parent = p;
6139 r->left = 0;
6140 r->right = 0;
6141 r->balance = 0;
6142
6143 while (p)
6144 {
6145 struct init_node *s;
6146
6147 if (r == p->left)
6148 {
6149 if (p->balance == 0)
6150 p->balance = -1;
6151 else if (p->balance < 0)
6152 {
6153 if (r->balance < 0)
6154 {
6155 /* L rotation. */
6156 p->left = r->right;
6157 if (p->left)
6158 p->left->parent = p;
6159 r->right = p;
6160
6161 p->balance = 0;
6162 r->balance = 0;
6163
6164 s = p->parent;
6165 p->parent = r;
6166 r->parent = s;
6167 if (s)
6168 {
6169 if (s->left == p)
6170 s->left = r;
6171 else
6172 s->right = r;
6173 }
6174 else
6175 constructor_pending_elts = r;
6176 }
6177 else
6178 {
6179 /* LR rotation. */
6180 struct init_node *t = r->right;
6181
6182 r->right = t->left;
6183 if (r->right)
6184 r->right->parent = r;
6185 t->left = r;
6186
6187 p->left = t->right;
6188 if (p->left)
6189 p->left->parent = p;
6190 t->right = p;
6191
6192 p->balance = t->balance < 0;
6193 r->balance = -(t->balance > 0);
6194 t->balance = 0;
6195
6196 s = p->parent;
6197 p->parent = t;
6198 r->parent = t;
6199 t->parent = s;
6200 if (s)
6201 {
6202 if (s->left == p)
6203 s->left = t;
6204 else
6205 s->right = t;
6206 }
6207 else
6208 constructor_pending_elts = t;
6209 }
6210 break;
6211 }
6212 else
6213 {
6214 /* p->balance == +1; growth of left side balances the node. */
6215 p->balance = 0;
6216 break;
6217 }
6218 }
6219 else /* r == p->right */
6220 {
6221 if (p->balance == 0)
6222 /* Growth propagation from right side. */
6223 p->balance++;
6224 else if (p->balance > 0)
6225 {
6226 if (r->balance > 0)
6227 {
6228 /* R rotation. */
6229 p->right = r->left;
6230 if (p->right)
6231 p->right->parent = p;
6232 r->left = p;
6233
6234 p->balance = 0;
6235 r->balance = 0;
6236
6237 s = p->parent;
6238 p->parent = r;
6239 r->parent = s;
6240 if (s)
6241 {
6242 if (s->left == p)
6243 s->left = r;
6244 else
6245 s->right = r;
6246 }
6247 else
6248 constructor_pending_elts = r;
6249 }
6250 else /* r->balance == -1 */
6251 {
6252 /* RL rotation */
6253 struct init_node *t = r->left;
6254
6255 r->left = t->right;
6256 if (r->left)
6257 r->left->parent = r;
6258 t->right = r;
6259
6260 p->right = t->left;
6261 if (p->right)
6262 p->right->parent = p;
6263 t->left = p;
6264
6265 r->balance = (t->balance < 0);
6266 p->balance = -(t->balance > 0);
6267 t->balance = 0;
6268
6269 s = p->parent;
6270 p->parent = t;
6271 r->parent = t;
6272 t->parent = s;
6273 if (s)
6274 {
6275 if (s->left == p)
6276 s->left = t;
6277 else
6278 s->right = t;
6279 }
6280 else
6281 constructor_pending_elts = t;
6282 }
6283 break;
6284 }
6285 else
6286 {
6287 /* p->balance == -1; growth of right side balances the node. */
6288 p->balance = 0;
6289 break;
6290 }
6291 }
6292
6293 r = p;
6294 p = p->parent;
6295 }
6296 }
6297
6298 /* Build AVL tree from a sorted chain. */
6299
6300 static void
set_nonincremental_init(void)6301 set_nonincremental_init (void)
6302 {
6303 unsigned HOST_WIDE_INT ix;
6304 tree index, value;
6305
6306 if (TREE_CODE (constructor_type) != RECORD_TYPE
6307 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6308 return;
6309
6310 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6311 add_pending_init (index, value);
6312 constructor_elements = 0;
6313 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6314 {
6315 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6316 /* Skip any nameless bit fields at the beginning. */
6317 while (constructor_unfilled_fields != 0
6318 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6319 && DECL_NAME (constructor_unfilled_fields) == 0)
6320 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6321
6322 }
6323 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6324 {
6325 if (TYPE_DOMAIN (constructor_type))
6326 constructor_unfilled_index
6327 = convert (bitsizetype,
6328 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6329 else
6330 constructor_unfilled_index = bitsize_zero_node;
6331 }
6332 constructor_incremental = 0;
6333 }
6334
6335 /* Build AVL tree from a string constant. */
6336
6337 static void
set_nonincremental_init_from_string(tree str)6338 set_nonincremental_init_from_string (tree str)
6339 {
6340 tree value, purpose, type;
6341 HOST_WIDE_INT val[2];
6342 const char *p, *end;
6343 int byte, wchar_bytes, charwidth, bitpos;
6344
6345 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6346
6347 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6348 == TYPE_PRECISION (char_type_node))
6349 wchar_bytes = 1;
6350 else
6351 {
6352 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6353 == TYPE_PRECISION (wchar_type_node));
6354 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6355 }
6356 charwidth = TYPE_PRECISION (char_type_node);
6357 type = TREE_TYPE (constructor_type);
6358 p = TREE_STRING_POINTER (str);
6359 end = p + TREE_STRING_LENGTH (str);
6360
6361 for (purpose = bitsize_zero_node;
6362 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6363 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6364 {
6365 if (wchar_bytes == 1)
6366 {
6367 val[1] = (unsigned char) *p++;
6368 val[0] = 0;
6369 }
6370 else
6371 {
6372 val[0] = 0;
6373 val[1] = 0;
6374 for (byte = 0; byte < wchar_bytes; byte++)
6375 {
6376 if (BYTES_BIG_ENDIAN)
6377 bitpos = (wchar_bytes - byte - 1) * charwidth;
6378 else
6379 bitpos = byte * charwidth;
6380 val[bitpos < HOST_BITS_PER_WIDE_INT]
6381 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6382 << (bitpos % HOST_BITS_PER_WIDE_INT);
6383 }
6384 }
6385
6386 if (!TYPE_UNSIGNED (type))
6387 {
6388 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6389 if (bitpos < HOST_BITS_PER_WIDE_INT)
6390 {
6391 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6392 {
6393 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6394 val[0] = -1;
6395 }
6396 }
6397 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6398 {
6399 if (val[1] < 0)
6400 val[0] = -1;
6401 }
6402 else if (val[0] & (((HOST_WIDE_INT) 1)
6403 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6404 val[0] |= ((HOST_WIDE_INT) -1)
6405 << (bitpos - HOST_BITS_PER_WIDE_INT);
6406 }
6407
6408 value = build_int_cst_wide (type, val[1], val[0]);
6409 add_pending_init (purpose, value);
6410 }
6411
6412 constructor_incremental = 0;
6413 }
6414
6415 /* Return value of FIELD in pending initializer or zero if the field was
6416 not initialized yet. */
6417
6418 static tree
find_init_member(tree field)6419 find_init_member (tree field)
6420 {
6421 struct init_node *p;
6422
6423 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6424 {
6425 if (constructor_incremental
6426 && tree_int_cst_lt (field, constructor_unfilled_index))
6427 set_nonincremental_init ();
6428
6429 p = constructor_pending_elts;
6430 while (p)
6431 {
6432 if (tree_int_cst_lt (field, p->purpose))
6433 p = p->left;
6434 else if (tree_int_cst_lt (p->purpose, field))
6435 p = p->right;
6436 else
6437 return p->value;
6438 }
6439 }
6440 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6441 {
6442 tree bitpos = bit_position (field);
6443
6444 if (constructor_incremental
6445 && (!constructor_unfilled_fields
6446 || tree_int_cst_lt (bitpos,
6447 bit_position (constructor_unfilled_fields))))
6448 set_nonincremental_init ();
6449
6450 p = constructor_pending_elts;
6451 while (p)
6452 {
6453 if (field == p->purpose)
6454 return p->value;
6455 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6456 p = p->left;
6457 else
6458 p = p->right;
6459 }
6460 }
6461 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6462 {
6463 if (!VEC_empty (constructor_elt, constructor_elements)
6464 && (VEC_last (constructor_elt, constructor_elements)->index
6465 == field))
6466 return VEC_last (constructor_elt, constructor_elements)->value;
6467 }
6468 return 0;
6469 }
6470
6471 /* "Output" the next constructor element.
6472 At top level, really output it to assembler code now.
6473 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6474 TYPE is the data type that the containing data type wants here.
6475 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6476 If VALUE is a string constant, STRICT_STRING is true if it is
6477 unparenthesized or we should not warn here for it being parenthesized.
6478 For other types of VALUE, STRICT_STRING is not used.
6479
6480 PENDING if non-nil means output pending elements that belong
6481 right after this element. (PENDING is normally 1;
6482 it is 0 while outputting pending elements, to avoid recursion.) */
6483
6484 static void
output_init_element(tree value,bool strict_string,tree type,tree field,int pending)6485 output_init_element (tree value, bool strict_string, tree type, tree field,
6486 int pending)
6487 {
6488 constructor_elt *celt;
6489
6490 if (type == error_mark_node || value == error_mark_node)
6491 {
6492 constructor_erroneous = 1;
6493 return;
6494 }
6495 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6496 && (TREE_CODE (value) == STRING_CST
6497 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6498 && !(TREE_CODE (value) == STRING_CST
6499 && TREE_CODE (type) == ARRAY_TYPE
6500 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6501 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6502 TYPE_MAIN_VARIANT (type)))
6503 value = array_to_pointer_conversion (value);
6504
6505 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6506 && require_constant_value && !flag_isoc99 && pending)
6507 {
6508 /* As an extension, allow initializing objects with static storage
6509 duration with compound literals (which are then treated just as
6510 the brace enclosed list they contain). */
6511 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6512 value = DECL_INITIAL (decl);
6513 }
6514
6515 if (value == error_mark_node)
6516 constructor_erroneous = 1;
6517 else if (!TREE_CONSTANT (value))
6518 constructor_constant = 0;
6519 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6520 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6521 || TREE_CODE (constructor_type) == UNION_TYPE)
6522 && DECL_C_BIT_FIELD (field)
6523 && TREE_CODE (value) != INTEGER_CST))
6524 constructor_simple = 0;
6525
6526 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6527 {
6528 if (require_constant_value)
6529 {
6530 error_init ("initializer element is not constant");
6531 value = error_mark_node;
6532 }
6533 else if (require_constant_elements)
6534 pedwarn ("initializer element is not computable at load time");
6535 }
6536
6537 /* If this field is empty (and not at the end of structure),
6538 don't do anything other than checking the initializer. */
6539 if (field
6540 && (TREE_TYPE (field) == error_mark_node
6541 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6542 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6543 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6544 || TREE_CHAIN (field)))))
6545 return;
6546
6547 value = digest_init (type, value, strict_string, require_constant_value);
6548 if (value == error_mark_node)
6549 {
6550 constructor_erroneous = 1;
6551 return;
6552 }
6553
6554 /* If this element doesn't come next in sequence,
6555 put it on constructor_pending_elts. */
6556 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6557 && (!constructor_incremental
6558 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6559 {
6560 if (constructor_incremental
6561 && tree_int_cst_lt (field, constructor_unfilled_index))
6562 set_nonincremental_init ();
6563
6564 add_pending_init (field, value);
6565 return;
6566 }
6567 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6568 && (!constructor_incremental
6569 || field != constructor_unfilled_fields))
6570 {
6571 /* We do this for records but not for unions. In a union,
6572 no matter which field is specified, it can be initialized
6573 right away since it starts at the beginning of the union. */
6574 if (constructor_incremental)
6575 {
6576 if (!constructor_unfilled_fields)
6577 set_nonincremental_init ();
6578 else
6579 {
6580 tree bitpos, unfillpos;
6581
6582 bitpos = bit_position (field);
6583 unfillpos = bit_position (constructor_unfilled_fields);
6584
6585 if (tree_int_cst_lt (bitpos, unfillpos))
6586 set_nonincremental_init ();
6587 }
6588 }
6589
6590 add_pending_init (field, value);
6591 return;
6592 }
6593 else if (TREE_CODE (constructor_type) == UNION_TYPE
6594 && !VEC_empty (constructor_elt, constructor_elements))
6595 {
6596 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6597 constructor_elements)->value))
6598 warning_init ("initialized field with side-effects overwritten");
6599 else if (warn_override_init)
6600 warning_init ("initialized field overwritten");
6601
6602 /* We can have just one union field set. */
6603 constructor_elements = 0;
6604 }
6605
6606 /* Otherwise, output this element either to
6607 constructor_elements or to the assembler file. */
6608
6609 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6610 celt->index = field;
6611 celt->value = value;
6612
6613 /* Advance the variable that indicates sequential elements output. */
6614 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6615 constructor_unfilled_index
6616 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6617 bitsize_one_node);
6618 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6619 {
6620 constructor_unfilled_fields
6621 = TREE_CHAIN (constructor_unfilled_fields);
6622
6623 /* Skip any nameless bit fields. */
6624 while (constructor_unfilled_fields != 0
6625 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6626 && DECL_NAME (constructor_unfilled_fields) == 0)
6627 constructor_unfilled_fields =
6628 TREE_CHAIN (constructor_unfilled_fields);
6629 }
6630 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6631 constructor_unfilled_fields = 0;
6632
6633 /* Now output any pending elements which have become next. */
6634 if (pending)
6635 output_pending_init_elements (0);
6636 }
6637
6638 /* Output any pending elements which have become next.
6639 As we output elements, constructor_unfilled_{fields,index}
6640 advances, which may cause other elements to become next;
6641 if so, they too are output.
6642
6643 If ALL is 0, we return when there are
6644 no more pending elements to output now.
6645
6646 If ALL is 1, we output space as necessary so that
6647 we can output all the pending elements. */
6648
6649 static void
output_pending_init_elements(int all)6650 output_pending_init_elements (int all)
6651 {
6652 struct init_node *elt = constructor_pending_elts;
6653 tree next;
6654
6655 retry:
6656
6657 /* Look through the whole pending tree.
6658 If we find an element that should be output now,
6659 output it. Otherwise, set NEXT to the element
6660 that comes first among those still pending. */
6661
6662 next = 0;
6663 while (elt)
6664 {
6665 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6666 {
6667 if (tree_int_cst_equal (elt->purpose,
6668 constructor_unfilled_index))
6669 output_init_element (elt->value, true,
6670 TREE_TYPE (constructor_type),
6671 constructor_unfilled_index, 0);
6672 else if (tree_int_cst_lt (constructor_unfilled_index,
6673 elt->purpose))
6674 {
6675 /* Advance to the next smaller node. */
6676 if (elt->left)
6677 elt = elt->left;
6678 else
6679 {
6680 /* We have reached the smallest node bigger than the
6681 current unfilled index. Fill the space first. */
6682 next = elt->purpose;
6683 break;
6684 }
6685 }
6686 else
6687 {
6688 /* Advance to the next bigger node. */
6689 if (elt->right)
6690 elt = elt->right;
6691 else
6692 {
6693 /* We have reached the biggest node in a subtree. Find
6694 the parent of it, which is the next bigger node. */
6695 while (elt->parent && elt->parent->right == elt)
6696 elt = elt->parent;
6697 elt = elt->parent;
6698 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6699 elt->purpose))
6700 {
6701 next = elt->purpose;
6702 break;
6703 }
6704 }
6705 }
6706 }
6707 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6708 || TREE_CODE (constructor_type) == UNION_TYPE)
6709 {
6710 tree ctor_unfilled_bitpos, elt_bitpos;
6711
6712 /* If the current record is complete we are done. */
6713 if (constructor_unfilled_fields == 0)
6714 break;
6715
6716 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6717 elt_bitpos = bit_position (elt->purpose);
6718 /* We can't compare fields here because there might be empty
6719 fields in between. */
6720 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6721 {
6722 constructor_unfilled_fields = elt->purpose;
6723 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6724 elt->purpose, 0);
6725 }
6726 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6727 {
6728 /* Advance to the next smaller node. */
6729 if (elt->left)
6730 elt = elt->left;
6731 else
6732 {
6733 /* We have reached the smallest node bigger than the
6734 current unfilled field. Fill the space first. */
6735 next = elt->purpose;
6736 break;
6737 }
6738 }
6739 else
6740 {
6741 /* Advance to the next bigger node. */
6742 if (elt->right)
6743 elt = elt->right;
6744 else
6745 {
6746 /* We have reached the biggest node in a subtree. Find
6747 the parent of it, which is the next bigger node. */
6748 while (elt->parent && elt->parent->right == elt)
6749 elt = elt->parent;
6750 elt = elt->parent;
6751 if (elt
6752 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6753 bit_position (elt->purpose))))
6754 {
6755 next = elt->purpose;
6756 break;
6757 }
6758 }
6759 }
6760 }
6761 }
6762
6763 /* Ordinarily return, but not if we want to output all
6764 and there are elements left. */
6765 if (!(all && next != 0))
6766 return;
6767
6768 /* If it's not incremental, just skip over the gap, so that after
6769 jumping to retry we will output the next successive element. */
6770 if (TREE_CODE (constructor_type) == RECORD_TYPE
6771 || TREE_CODE (constructor_type) == UNION_TYPE)
6772 constructor_unfilled_fields = next;
6773 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6774 constructor_unfilled_index = next;
6775
6776 /* ELT now points to the node in the pending tree with the next
6777 initializer to output. */
6778 goto retry;
6779 }
6780
6781 /* Add one non-braced element to the current constructor level.
6782 This adjusts the current position within the constructor's type.
6783 This may also start or terminate implicit levels
6784 to handle a partly-braced initializer.
6785
6786 Once this has found the correct level for the new element,
6787 it calls output_init_element. */
6788
6789 void
process_init_element(struct c_expr value)6790 process_init_element (struct c_expr value)
6791 {
6792 tree orig_value = value.value;
6793 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6794 bool strict_string = value.original_code == STRING_CST;
6795
6796 designator_depth = 0;
6797 designator_erroneous = 0;
6798
6799 /* Handle superfluous braces around string cst as in
6800 char x[] = {"foo"}; */
6801 if (string_flag
6802 && constructor_type
6803 && TREE_CODE (constructor_type) == ARRAY_TYPE
6804 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6805 && integer_zerop (constructor_unfilled_index))
6806 {
6807 if (constructor_stack->replacement_value.value)
6808 error_init ("excess elements in char array initializer");
6809 constructor_stack->replacement_value = value;
6810 return;
6811 }
6812
6813 if (constructor_stack->replacement_value.value != 0)
6814 {
6815 error_init ("excess elements in struct initializer");
6816 return;
6817 }
6818
6819 /* Ignore elements of a brace group if it is entirely superfluous
6820 and has already been diagnosed. */
6821 if (constructor_type == 0)
6822 return;
6823
6824 /* If we've exhausted any levels that didn't have braces,
6825 pop them now. */
6826 while (constructor_stack->implicit)
6827 {
6828 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6829 || TREE_CODE (constructor_type) == UNION_TYPE)
6830 && constructor_fields == 0)
6831 process_init_element (pop_init_level (1));
6832 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6833 && (constructor_max_index == 0
6834 || tree_int_cst_lt (constructor_max_index,
6835 constructor_index)))
6836 process_init_element (pop_init_level (1));
6837 else
6838 break;
6839 }
6840
6841 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6842 if (constructor_range_stack)
6843 {
6844 /* If value is a compound literal and we'll be just using its
6845 content, don't put it into a SAVE_EXPR. */
6846 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6847 || !require_constant_value
6848 || flag_isoc99)
6849 value.value = save_expr (value.value);
6850 }
6851
6852 while (1)
6853 {
6854 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6855 {
6856 tree fieldtype;
6857 enum tree_code fieldcode;
6858
6859 if (constructor_fields == 0)
6860 {
6861 pedwarn_init ("excess elements in struct initializer");
6862 break;
6863 }
6864
6865 fieldtype = TREE_TYPE (constructor_fields);
6866 if (fieldtype != error_mark_node)
6867 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6868 fieldcode = TREE_CODE (fieldtype);
6869
6870 /* Error for non-static initialization of a flexible array member. */
6871 if (fieldcode == ARRAY_TYPE
6872 && !require_constant_value
6873 && TYPE_SIZE (fieldtype) == NULL_TREE
6874 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6875 {
6876 error_init ("non-static initialization of a flexible array member");
6877 break;
6878 }
6879
6880 /* Accept a string constant to initialize a subarray. */
6881 if (value.value != 0
6882 && fieldcode == ARRAY_TYPE
6883 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6884 && string_flag)
6885 value.value = orig_value;
6886 /* Otherwise, if we have come to a subaggregate,
6887 and we don't have an element of its type, push into it. */
6888 else if (value.value != 0
6889 && value.value != error_mark_node
6890 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6891 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6892 || fieldcode == UNION_TYPE))
6893 {
6894 push_init_level (1);
6895 continue;
6896 }
6897
6898 if (value.value)
6899 {
6900 push_member_name (constructor_fields);
6901 output_init_element (value.value, strict_string,
6902 fieldtype, constructor_fields, 1);
6903 RESTORE_SPELLING_DEPTH (constructor_depth);
6904 }
6905 else
6906 /* Do the bookkeeping for an element that was
6907 directly output as a constructor. */
6908 {
6909 /* For a record, keep track of end position of last field. */
6910 if (DECL_SIZE (constructor_fields))
6911 constructor_bit_index
6912 = size_binop (PLUS_EXPR,
6913 bit_position (constructor_fields),
6914 DECL_SIZE (constructor_fields));
6915
6916 /* If the current field was the first one not yet written out,
6917 it isn't now, so update. */
6918 if (constructor_unfilled_fields == constructor_fields)
6919 {
6920 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6921 /* Skip any nameless bit fields. */
6922 while (constructor_unfilled_fields != 0
6923 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6924 && DECL_NAME (constructor_unfilled_fields) == 0)
6925 constructor_unfilled_fields =
6926 TREE_CHAIN (constructor_unfilled_fields);
6927 }
6928 }
6929
6930 constructor_fields = TREE_CHAIN (constructor_fields);
6931 /* Skip any nameless bit fields at the beginning. */
6932 while (constructor_fields != 0
6933 && DECL_C_BIT_FIELD (constructor_fields)
6934 && DECL_NAME (constructor_fields) == 0)
6935 constructor_fields = TREE_CHAIN (constructor_fields);
6936 }
6937 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6938 {
6939 tree fieldtype;
6940 enum tree_code fieldcode;
6941
6942 if (constructor_fields == 0)
6943 {
6944 pedwarn_init ("excess elements in union initializer");
6945 break;
6946 }
6947
6948 fieldtype = TREE_TYPE (constructor_fields);
6949 if (fieldtype != error_mark_node)
6950 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6951 fieldcode = TREE_CODE (fieldtype);
6952
6953 /* Warn that traditional C rejects initialization of unions.
6954 We skip the warning if the value is zero. This is done
6955 under the assumption that the zero initializer in user
6956 code appears conditioned on e.g. __STDC__ to avoid
6957 "missing initializer" warnings and relies on default
6958 initialization to zero in the traditional C case.
6959 We also skip the warning if the initializer is designated,
6960 again on the assumption that this must be conditional on
6961 __STDC__ anyway (and we've already complained about the
6962 member-designator already). */
6963 if (!in_system_header && !constructor_designated
6964 && !(value.value && (integer_zerop (value.value)
6965 || real_zerop (value.value))))
6966 warning (OPT_Wtraditional, "traditional C rejects initialization "
6967 "of unions");
6968
6969 /* Accept a string constant to initialize a subarray. */
6970 if (value.value != 0
6971 && fieldcode == ARRAY_TYPE
6972 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6973 && string_flag)
6974 value.value = orig_value;
6975 /* Otherwise, if we have come to a subaggregate,
6976 and we don't have an element of its type, push into it. */
6977 else if (value.value != 0
6978 && value.value != error_mark_node
6979 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6980 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6981 || fieldcode == UNION_TYPE))
6982 {
6983 push_init_level (1);
6984 continue;
6985 }
6986
6987 if (value.value)
6988 {
6989 push_member_name (constructor_fields);
6990 output_init_element (value.value, strict_string,
6991 fieldtype, constructor_fields, 1);
6992 RESTORE_SPELLING_DEPTH (constructor_depth);
6993 }
6994 else
6995 /* Do the bookkeeping for an element that was
6996 directly output as a constructor. */
6997 {
6998 constructor_bit_index = DECL_SIZE (constructor_fields);
6999 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7000 }
7001
7002 constructor_fields = 0;
7003 }
7004 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7005 {
7006 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7007 enum tree_code eltcode = TREE_CODE (elttype);
7008
7009 /* Accept a string constant to initialize a subarray. */
7010 if (value.value != 0
7011 && eltcode == ARRAY_TYPE
7012 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7013 && string_flag)
7014 value.value = orig_value;
7015 /* Otherwise, if we have come to a subaggregate,
7016 and we don't have an element of its type, push into it. */
7017 else if (value.value != 0
7018 && value.value != error_mark_node
7019 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7020 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7021 || eltcode == UNION_TYPE))
7022 {
7023 push_init_level (1);
7024 continue;
7025 }
7026
7027 if (constructor_max_index != 0
7028 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7029 || integer_all_onesp (constructor_max_index)))
7030 {
7031 pedwarn_init ("excess elements in array initializer");
7032 break;
7033 }
7034
7035 /* Now output the actual element. */
7036 if (value.value)
7037 {
7038 push_array_bounds (tree_low_cst (constructor_index, 1));
7039 output_init_element (value.value, strict_string,
7040 elttype, constructor_index, 1);
7041 RESTORE_SPELLING_DEPTH (constructor_depth);
7042 }
7043
7044 constructor_index
7045 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7046
7047 if (!value.value)
7048 /* If we are doing the bookkeeping for an element that was
7049 directly output as a constructor, we must update
7050 constructor_unfilled_index. */
7051 constructor_unfilled_index = constructor_index;
7052 }
7053 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7054 {
7055 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7056
7057 /* Do a basic check of initializer size. Note that vectors
7058 always have a fixed size derived from their type. */
7059 if (tree_int_cst_lt (constructor_max_index, constructor_index))
7060 {
7061 pedwarn_init ("excess elements in vector initializer");
7062 break;
7063 }
7064
7065 /* Now output the actual element. */
7066 if (value.value)
7067 output_init_element (value.value, strict_string,
7068 elttype, constructor_index, 1);
7069
7070 constructor_index
7071 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
7072
7073 if (!value.value)
7074 /* If we are doing the bookkeeping for an element that was
7075 directly output as a constructor, we must update
7076 constructor_unfilled_index. */
7077 constructor_unfilled_index = constructor_index;
7078 }
7079
7080 /* Handle the sole element allowed in a braced initializer
7081 for a scalar variable. */
7082 else if (constructor_type != error_mark_node
7083 && constructor_fields == 0)
7084 {
7085 pedwarn_init ("excess elements in scalar initializer");
7086 break;
7087 }
7088 else
7089 {
7090 if (value.value)
7091 output_init_element (value.value, strict_string,
7092 constructor_type, NULL_TREE, 1);
7093 constructor_fields = 0;
7094 }
7095
7096 /* Handle range initializers either at this level or anywhere higher
7097 in the designator stack. */
7098 if (constructor_range_stack)
7099 {
7100 struct constructor_range_stack *p, *range_stack;
7101 int finish = 0;
7102
7103 range_stack = constructor_range_stack;
7104 constructor_range_stack = 0;
7105 while (constructor_stack != range_stack->stack)
7106 {
7107 gcc_assert (constructor_stack->implicit);
7108 process_init_element (pop_init_level (1));
7109 }
7110 for (p = range_stack;
7111 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
7112 p = p->prev)
7113 {
7114 gcc_assert (constructor_stack->implicit);
7115 process_init_element (pop_init_level (1));
7116 }
7117
7118 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
7119 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
7120 finish = 1;
7121
7122 while (1)
7123 {
7124 constructor_index = p->index;
7125 constructor_fields = p->fields;
7126 if (finish && p->range_end && p->index == p->range_start)
7127 {
7128 finish = 0;
7129 p->prev = 0;
7130 }
7131 p = p->next;
7132 if (!p)
7133 break;
7134 push_init_level (2);
7135 p->stack = constructor_stack;
7136 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
7137 p->index = p->range_start;
7138 }
7139
7140 if (!finish)
7141 constructor_range_stack = range_stack;
7142 continue;
7143 }
7144
7145 break;
7146 }
7147
7148 constructor_range_stack = 0;
7149 }
7150
7151 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
7152 (guaranteed to be 'volatile' or null) and ARGS (represented using
7153 an ASM_EXPR node). */
7154 tree
build_asm_stmt(tree cv_qualifier,tree args)7155 build_asm_stmt (tree cv_qualifier, tree args)
7156 {
7157 if (!ASM_VOLATILE_P (args) && cv_qualifier)
7158 ASM_VOLATILE_P (args) = 1;
7159 return add_stmt (args);
7160 }
7161
7162 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
7163 some INPUTS, and some CLOBBERS. The latter three may be NULL.
7164 SIMPLE indicates whether there was anything at all after the
7165 string in the asm expression -- asm("blah") and asm("blah" : )
7166 are subtly different. We use a ASM_EXPR node to represent this. */
7167 tree
build_asm_expr(tree string,tree outputs,tree inputs,tree clobbers,bool simple)7168 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
7169 bool simple)
7170 {
7171 tree tail;
7172 tree args;
7173 int i;
7174 const char *constraint;
7175 const char **oconstraints;
7176 bool allows_mem, allows_reg, is_inout;
7177 int ninputs, noutputs;
7178
7179 ninputs = list_length (inputs);
7180 noutputs = list_length (outputs);
7181 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
7182
7183 string = resolve_asm_operand_names (string, outputs, inputs);
7184
7185 /* Remove output conversions that change the type but not the mode. */
7186 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
7187 {
7188 tree output = TREE_VALUE (tail);
7189
7190 /* ??? Really, this should not be here. Users should be using a
7191 proper lvalue, dammit. But there's a long history of using casts
7192 in the output operands. In cases like longlong.h, this becomes a
7193 primitive form of typechecking -- if the cast can be removed, then
7194 the output operand had a type of the proper width; otherwise we'll
7195 get an error. Gross, but ... */
7196 STRIP_NOPS (output);
7197
7198 if (!lvalue_or_else (output, lv_asm))
7199 output = error_mark_node;
7200
7201 if (output != error_mark_node
7202 && (TREE_READONLY (output)
7203 || TYPE_READONLY (TREE_TYPE (output))
7204 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
7205 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
7206 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
7207 readonly_error (output, lv_asm);
7208
7209 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7210 oconstraints[i] = constraint;
7211
7212 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7213 &allows_mem, &allows_reg, &is_inout))
7214 {
7215 /* If the operand is going to end up in memory,
7216 mark it addressable. */
7217 if (!allows_reg && !c_mark_addressable (output))
7218 output = error_mark_node;
7219 }
7220 else
7221 output = error_mark_node;
7222
7223 TREE_VALUE (tail) = output;
7224 }
7225
7226 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
7227 {
7228 tree input;
7229
7230 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7231 input = TREE_VALUE (tail);
7232
7233 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
7234 oconstraints, &allows_mem, &allows_reg))
7235 {
7236 /* If the operand is going to end up in memory,
7237 mark it addressable. */
7238 if (!allows_reg && allows_mem)
7239 {
7240 /* Strip the nops as we allow this case. FIXME, this really
7241 should be rejected or made deprecated. */
7242 STRIP_NOPS (input);
7243 if (!c_mark_addressable (input))
7244 input = error_mark_node;
7245 }
7246 }
7247 else
7248 input = error_mark_node;
7249
7250 TREE_VALUE (tail) = input;
7251 }
7252
7253 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
7254
7255 /* asm statements without outputs, including simple ones, are treated
7256 as volatile. */
7257 ASM_INPUT_P (args) = simple;
7258 ASM_VOLATILE_P (args) = (noutputs == 0);
7259
7260 return args;
7261 }
7262
7263 /* Generate a goto statement to LABEL. */
7264
7265 tree
c_finish_goto_label(tree label)7266 c_finish_goto_label (tree label)
7267 {
7268 tree decl = lookup_label (label);
7269 if (!decl)
7270 return NULL_TREE;
7271
7272 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
7273 {
7274 error ("jump into statement expression");
7275 return NULL_TREE;
7276 }
7277
7278 if (C_DECL_UNJUMPABLE_VM (decl))
7279 {
7280 error ("jump into scope of identifier with variably modified type");
7281 return NULL_TREE;
7282 }
7283
7284 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
7285 {
7286 /* No jump from outside this statement expression context, so
7287 record that there is a jump from within this context. */
7288 struct c_label_list *nlist;
7289 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7290 nlist->next = label_context_stack_se->labels_used;
7291 nlist->label = decl;
7292 label_context_stack_se->labels_used = nlist;
7293 }
7294
7295 if (!C_DECL_UNDEFINABLE_VM (decl))
7296 {
7297 /* No jump from outside this context context of identifiers with
7298 variably modified type, so record that there is a jump from
7299 within this context. */
7300 struct c_label_list *nlist;
7301 nlist = XOBNEW (&parser_obstack, struct c_label_list);
7302 nlist->next = label_context_stack_vm->labels_used;
7303 nlist->label = decl;
7304 label_context_stack_vm->labels_used = nlist;
7305 }
7306
7307 TREE_USED (decl) = 1;
7308 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7309 }
7310
7311 /* Generate a computed goto statement to EXPR. */
7312
7313 tree
c_finish_goto_ptr(tree expr)7314 c_finish_goto_ptr (tree expr)
7315 {
7316 if (pedantic)
7317 pedwarn ("ISO C forbids %<goto *expr;%>");
7318 expr = convert (ptr_type_node, expr);
7319 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7320 }
7321
7322 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cm) */
7323 /** c_finish_block_return_stmt - Utilty routine to figure out block's return
7324 type.
7325 */
7326 static tree
c_finish_block_return_stmt(tree retval)7327 c_finish_block_return_stmt (tree retval)
7328 {
7329 tree valtype;
7330 /* If this is the first return we've seen in the block, infer the type of
7331 the block from it. */
7332 if (cur_block->return_type == NULL_TREE)
7333 {
7334 tree restype;
7335 if (retval)
7336 {
7337 restype = TYPE_MAIN_VARIANT (TREE_TYPE (retval));
7338 TREE_TYPE (current_function_decl)
7339 = build_function_type (restype,
7340 TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)));
7341 TREE_TYPE (DECL_RESULT (current_function_decl)) = restype;
7342 relayout_decl (DECL_RESULT (current_function_decl));
7343 }
7344 else
7345 restype = void_type_node;
7346
7347 cur_block->return_type = restype;
7348 }
7349
7350 /* Verify that this result type matches the previous one. We are
7351 pickier with blocks than for normal functions because this is a
7352 new feature and we set the rules. */
7353 if (TREE_CODE (cur_block->return_type) == VOID_TYPE)
7354 {
7355 if (retval)
7356 {
7357 error ("void block should not return a value");
7358 retval = NULL_TREE;
7359 }
7360 return retval;
7361 }
7362
7363 if (!retval)
7364 {
7365 error ("non-void block should return a value");
7366 return error_mark_node;
7367 }
7368
7369 /* We have a non-void block with an expression, continue checking. */
7370 valtype = TREE_TYPE (retval);
7371
7372 /* For now, restrict multiple return statements in a block to have
7373 strict compatible types only. */
7374 if (!types_are_block_compatible (cur_block->return_type, valtype))
7375 error ("incompatible type returning %qT, expected %qT",
7376 valtype, cur_block->return_type);
7377 return retval;
7378 }
7379 /* APPLE LOCAL end radar 5732232 - blocks (C++ cm) */
7380
7381 /* Generate a C `return' statement. RETVAL is the expression for what
7382 to return, or a null pointer for `return;' with no value. */
7383
7384 tree
c_finish_return(tree retval)7385 c_finish_return (tree retval)
7386 {
7387 /* APPLE LOCAL begin radar 5732232 - blocks */
7388 tree valtype, ret_stmt;
7389 bool no_warning = false;
7390
7391 /* APPLE LOCAL radar 5822844 - radar 6185344 */
7392 if (cur_block && !cur_block->block_has_return_type)
7393 {
7394 retval = c_finish_block_return_stmt (retval);
7395 if (retval == error_mark_node)
7396 return NULL_TREE;
7397 }
7398
7399 valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7400 /* APPLE LOCAL end radar 5732232 - blocks */
7401
7402 if (TREE_THIS_VOLATILE (current_function_decl))
7403 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7404
7405 if (!retval)
7406 {
7407 current_function_returns_null = 1;
7408 if ((warn_return_type || flag_isoc99)
7409 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7410 {
7411 pedwarn_c99 ("%<return%> with no value, in "
7412 "function returning non-void");
7413 no_warning = true;
7414 }
7415 }
7416 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7417 {
7418 current_function_returns_null = 1;
7419 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7420 pedwarn ("%<return%> with a value, in function returning void");
7421 }
7422 else
7423 {
7424 tree t = convert_for_assignment (valtype, retval, ic_return,
7425 NULL_TREE, NULL_TREE, 0);
7426 tree res = DECL_RESULT (current_function_decl);
7427 tree inner;
7428
7429 current_function_returns_value = 1;
7430 if (t == error_mark_node)
7431 return NULL_TREE;
7432
7433 inner = t = convert (TREE_TYPE (res), t);
7434
7435 /* Strip any conversions, additions, and subtractions, and see if
7436 we are returning the address of a local variable. Warn if so. */
7437 while (1)
7438 {
7439 switch (TREE_CODE (inner))
7440 {
7441 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7442 case PLUS_EXPR:
7443 inner = TREE_OPERAND (inner, 0);
7444 continue;
7445
7446 case MINUS_EXPR:
7447 /* If the second operand of the MINUS_EXPR has a pointer
7448 type (or is converted from it), this may be valid, so
7449 don't give a warning. */
7450 {
7451 tree op1 = TREE_OPERAND (inner, 1);
7452
7453 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7454 && (TREE_CODE (op1) == NOP_EXPR
7455 || TREE_CODE (op1) == NON_LVALUE_EXPR
7456 || TREE_CODE (op1) == CONVERT_EXPR))
7457 op1 = TREE_OPERAND (op1, 0);
7458
7459 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7460 break;
7461
7462 inner = TREE_OPERAND (inner, 0);
7463 continue;
7464 }
7465
7466 case ADDR_EXPR:
7467 inner = TREE_OPERAND (inner, 0);
7468
7469 while (REFERENCE_CLASS_P (inner)
7470 && TREE_CODE (inner) != INDIRECT_REF)
7471 inner = TREE_OPERAND (inner, 0);
7472
7473 if (DECL_P (inner)
7474 && !DECL_EXTERNAL (inner)
7475 && !TREE_STATIC (inner)
7476 && DECL_CONTEXT (inner) == current_function_decl)
7477 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cn) */
7478 {
7479 if (TREE_CODE (valtype) == BLOCK_POINTER_TYPE)
7480 /* APPLE LOCAL radar 6048570 */
7481 error ("returning block that lives on the local stack");
7482 else
7483 warning (0, "function returns address of local variable");
7484 }
7485 /* APPLE LOCAL end radar 5732232 - blocks (C++ cn) */
7486 break;
7487
7488 default:
7489 break;
7490 }
7491
7492 break;
7493 }
7494
7495 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7496 }
7497
7498 ret_stmt = build_stmt (RETURN_EXPR, retval);
7499 TREE_NO_WARNING (ret_stmt) |= no_warning;
7500 return add_stmt (ret_stmt);
7501 }
7502
7503 struct c_switch {
7504 /* The SWITCH_EXPR being built. */
7505 tree switch_expr;
7506
7507 /* The original type of the testing expression, i.e. before the
7508 default conversion is applied. */
7509 tree orig_type;
7510
7511 /* A splay-tree mapping the low element of a case range to the high
7512 element, or NULL_TREE if there is no high element. Used to
7513 determine whether or not a new case label duplicates an old case
7514 label. We need a tree, rather than simply a hash table, because
7515 of the GNU case range extension. */
7516 splay_tree cases;
7517
7518 /* Number of nested statement expressions within this switch
7519 statement; if nonzero, case and default labels may not
7520 appear. */
7521 unsigned int blocked_stmt_expr;
7522
7523 /* Scope of outermost declarations of identifiers with variably
7524 modified type within this switch statement; if nonzero, case and
7525 default labels may not appear. */
7526 unsigned int blocked_vm;
7527
7528 /* The next node on the stack. */
7529 struct c_switch *next;
7530 };
7531
7532 /* A stack of the currently active switch statements. The innermost
7533 switch statement is on the top of the stack. There is no need to
7534 mark the stack for garbage collection because it is only active
7535 during the processing of the body of a function, and we never
7536 collect at that point. */
7537
7538 struct c_switch *c_switch_stack;
7539
7540 /* Start a C switch statement, testing expression EXP. Return the new
7541 SWITCH_EXPR. */
7542
7543 tree
c_start_case(tree exp)7544 c_start_case (tree exp)
7545 {
7546 tree orig_type = error_mark_node;
7547 struct c_switch *cs;
7548
7549 if (exp != error_mark_node)
7550 {
7551 orig_type = TREE_TYPE (exp);
7552
7553 if (!INTEGRAL_TYPE_P (orig_type))
7554 {
7555 if (orig_type != error_mark_node)
7556 {
7557 error ("switch quantity not an integer");
7558 orig_type = error_mark_node;
7559 }
7560 exp = integer_zero_node;
7561 }
7562 else
7563 {
7564 tree type = TYPE_MAIN_VARIANT (orig_type);
7565
7566 if (!in_system_header
7567 && (type == long_integer_type_node
7568 || type == long_unsigned_type_node))
7569 warning (OPT_Wtraditional, "%<long%> switch expression not "
7570 "converted to %<int%> in ISO C");
7571
7572 exp = default_conversion (exp);
7573 }
7574 }
7575
7576 /* Add this new SWITCH_EXPR to the stack. */
7577 cs = XNEW (struct c_switch);
7578 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7579 cs->orig_type = orig_type;
7580 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7581 cs->blocked_stmt_expr = 0;
7582 cs->blocked_vm = 0;
7583 cs->next = c_switch_stack;
7584 c_switch_stack = cs;
7585
7586 return add_stmt (cs->switch_expr);
7587 }
7588
7589 /* Process a case label. */
7590
7591 tree
do_case(tree low_value,tree high_value)7592 do_case (tree low_value, tree high_value)
7593 {
7594 tree label = NULL_TREE;
7595
7596 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7597 && !c_switch_stack->blocked_vm)
7598 {
7599 label = c_add_case_label (c_switch_stack->cases,
7600 SWITCH_COND (c_switch_stack->switch_expr),
7601 c_switch_stack->orig_type,
7602 low_value, high_value);
7603 if (label == error_mark_node)
7604 label = NULL_TREE;
7605 }
7606 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7607 {
7608 if (low_value)
7609 error ("case label in statement expression not containing "
7610 "enclosing switch statement");
7611 else
7612 error ("%<default%> label in statement expression not containing "
7613 "enclosing switch statement");
7614 }
7615 else if (c_switch_stack && c_switch_stack->blocked_vm)
7616 {
7617 if (low_value)
7618 error ("case label in scope of identifier with variably modified "
7619 "type not containing enclosing switch statement");
7620 else
7621 error ("%<default%> label in scope of identifier with variably "
7622 "modified type not containing enclosing switch statement");
7623 }
7624 else if (low_value)
7625 error ("case label not within a switch statement");
7626 else
7627 error ("%<default%> label not within a switch statement");
7628
7629 return label;
7630 }
7631
7632 /* Finish the switch statement. */
7633
7634 void
c_finish_case(tree body)7635 c_finish_case (tree body)
7636 {
7637 struct c_switch *cs = c_switch_stack;
7638 location_t switch_location;
7639
7640 SWITCH_BODY (cs->switch_expr) = body;
7641
7642 /* We must not be within a statement expression nested in the switch
7643 at this point; we might, however, be within the scope of an
7644 identifier with variably modified type nested in the switch. */
7645 gcc_assert (!cs->blocked_stmt_expr);
7646
7647 /* Emit warnings as needed. */
7648 if (EXPR_HAS_LOCATION (cs->switch_expr))
7649 switch_location = EXPR_LOCATION (cs->switch_expr);
7650 else
7651 switch_location = input_location;
7652 c_do_switch_warnings (cs->cases, switch_location,
7653 TREE_TYPE (cs->switch_expr),
7654 SWITCH_COND (cs->switch_expr));
7655
7656 /* Pop the stack. */
7657 c_switch_stack = cs->next;
7658 splay_tree_delete (cs->cases);
7659 XDELETE (cs);
7660 }
7661
7662 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7663 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7664 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7665 statement, and was not surrounded with parenthesis. */
7666
7667 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block,bool nested_if)7668 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7669 tree else_block, bool nested_if)
7670 {
7671 tree stmt;
7672
7673 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7674 if (warn_parentheses && nested_if && else_block == NULL)
7675 {
7676 tree inner_if = then_block;
7677
7678 /* We know from the grammar productions that there is an IF nested
7679 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7680 it might not be exactly THEN_BLOCK, but should be the last
7681 non-container statement within. */
7682 while (1)
7683 switch (TREE_CODE (inner_if))
7684 {
7685 case COND_EXPR:
7686 goto found;
7687 case BIND_EXPR:
7688 inner_if = BIND_EXPR_BODY (inner_if);
7689 break;
7690 case STATEMENT_LIST:
7691 inner_if = expr_last (then_block);
7692 break;
7693 case TRY_FINALLY_EXPR:
7694 case TRY_CATCH_EXPR:
7695 inner_if = TREE_OPERAND (inner_if, 0);
7696 break;
7697 default:
7698 gcc_unreachable ();
7699 }
7700 found:
7701
7702 if (COND_EXPR_ELSE (inner_if))
7703 warning (OPT_Wparentheses,
7704 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7705 &if_locus);
7706 }
7707
7708 empty_body_warning (then_block, else_block);
7709
7710 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7711 SET_EXPR_LOCATION (stmt, if_locus);
7712 add_stmt (stmt);
7713 }
7714
7715 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7716 /* Emit a general-purpose loop construct. START_LOCUS is the location
7717 of the beginning of the loop. COND is the loop condition.
7718 COND_IS_FIRST is false for DO loops. INCR is the FOR increment
7719 expression. BODY is the statement controlled by the loop. BLAB is
7720 the break label. CLAB is the continue label. ATTRS is the
7721 attributes associated with the loop, which at present are
7722 associated with the topmost label. Everything is allowed to be
7723 NULL. */
7724
7725 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7726 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,tree attrs,bool cond_is_first)7727 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7728 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7729 tree blab, tree clab, tree attrs, bool cond_is_first)
7730 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7731 {
7732 tree entry = NULL, exit = NULL, t;
7733
7734 /* If the condition is zero don't generate a loop construct. */
7735 if (cond && integer_zerop (cond))
7736 {
7737 if (cond_is_first)
7738 {
7739 t = build_and_jump (&blab);
7740 SET_EXPR_LOCATION (t, start_locus);
7741 add_stmt (t);
7742 }
7743 }
7744 else
7745 {
7746 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7747
7748 /* If we have an exit condition, then we build an IF with gotos either
7749 out of the loop, or to the top of it. If there's no exit condition,
7750 then we just build a jump back to the top. */
7751 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7752
7753 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7754 /* Add the attributes to the 'top' label. */
7755 decl_attributes (&LABEL_EXPR_LABEL (top), attrs, 0);
7756
7757 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7758 if (cond && !integer_nonzerop (cond))
7759 {
7760 /* Canonicalize the loop condition to the end. This means
7761 generating a branch to the loop condition. Reuse the
7762 continue label, if possible. */
7763 if (cond_is_first)
7764 {
7765 if (incr || !clab)
7766 {
7767 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7768 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7769 }
7770 else
7771 t = build1 (GOTO_EXPR, void_type_node, clab);
7772 SET_EXPR_LOCATION (t, start_locus);
7773 add_stmt (t);
7774 }
7775
7776 t = build_and_jump (&blab);
7777 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7778 if (cond_is_first)
7779 SET_EXPR_LOCATION (exit, start_locus);
7780 else
7781 SET_EXPR_LOCATION (exit, input_location);
7782 }
7783
7784 add_stmt (top);
7785 }
7786
7787 if (body)
7788 add_stmt (body);
7789 if (clab)
7790 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7791 if (incr)
7792 add_stmt (incr);
7793 if (entry)
7794 add_stmt (entry);
7795 if (exit)
7796 add_stmt (exit);
7797 if (blab)
7798 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7799 }
7800
7801 tree
c_finish_bc_stmt(tree * label_p,bool is_break)7802 c_finish_bc_stmt (tree *label_p, bool is_break)
7803 {
7804 bool skip;
7805 tree label = *label_p;
7806
7807 /* In switch statements break is sometimes stylistically used after
7808 a return statement. This can lead to spurious warnings about
7809 control reaching the end of a non-void function when it is
7810 inlined. Note that we are calling block_may_fallthru with
7811 language specific tree nodes; this works because
7812 block_may_fallthru returns true when given something it does not
7813 understand. */
7814 skip = !block_may_fallthru (cur_stmt_list);
7815
7816 if (!label)
7817 {
7818 if (!skip)
7819 *label_p = label = create_artificial_label ();
7820 }
7821 else if (TREE_CODE (label) == LABEL_DECL)
7822 ;
7823 else switch (TREE_INT_CST_LOW (label))
7824 {
7825 case 0:
7826 if (is_break)
7827 error ("break statement not within loop or switch");
7828 else
7829 error ("continue statement not within a loop");
7830 return NULL_TREE;
7831
7832 case 1:
7833 gcc_assert (is_break);
7834 error ("break statement used with OpenMP for loop");
7835 return NULL_TREE;
7836
7837 default:
7838 gcc_unreachable ();
7839 }
7840
7841 if (skip)
7842 return NULL_TREE;
7843
7844 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7845 }
7846
7847 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7848
7849 static void
emit_side_effect_warnings(tree expr)7850 emit_side_effect_warnings (tree expr)
7851 {
7852 if (expr == error_mark_node)
7853 ;
7854 else if (!TREE_SIDE_EFFECTS (expr))
7855 {
7856 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7857 warning (0, "%Hstatement with no effect",
7858 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7859 }
7860 else if (warn_unused_value)
7861 warn_if_unused_value (expr, input_location);
7862 }
7863
7864 /* Process an expression as if it were a complete statement. Emit
7865 diagnostics, but do not call ADD_STMT. */
7866
7867 tree
c_process_expr_stmt(tree expr)7868 c_process_expr_stmt (tree expr)
7869 {
7870 if (!expr)
7871 return NULL_TREE;
7872
7873 if (warn_sequence_point)
7874 verify_sequence_points (expr);
7875
7876 if (TREE_TYPE (expr) != error_mark_node
7877 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7878 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7879 error ("expression statement has incomplete type");
7880
7881 /* If we're not processing a statement expression, warn about unused values.
7882 Warnings for statement expressions will be emitted later, once we figure
7883 out which is the result. */
7884 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7885 && (extra_warnings || warn_unused_value))
7886 emit_side_effect_warnings (expr);
7887
7888 /* If the expression is not of a type to which we cannot assign a line
7889 number, wrap the thing in a no-op NOP_EXPR. */
7890 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7891 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7892
7893 if (EXPR_P (expr))
7894 SET_EXPR_LOCATION (expr, input_location);
7895
7896 return expr;
7897 }
7898
7899 /* Emit an expression as a statement. */
7900
7901 tree
c_finish_expr_stmt(tree expr)7902 c_finish_expr_stmt (tree expr)
7903 {
7904 if (expr)
7905 return add_stmt (c_process_expr_stmt (expr));
7906 else
7907 return NULL;
7908 }
7909
7910 /* Do the opposite and emit a statement as an expression. To begin,
7911 create a new binding level and return it. */
7912
7913 tree
c_begin_stmt_expr(void)7914 c_begin_stmt_expr (void)
7915 {
7916 tree ret;
7917 struct c_label_context_se *nstack;
7918 struct c_label_list *glist;
7919
7920 /* We must force a BLOCK for this level so that, if it is not expanded
7921 later, there is a way to turn off the entire subtree of blocks that
7922 are contained in it. */
7923 keep_next_level ();
7924 ret = c_begin_compound_stmt (true);
7925 if (c_switch_stack)
7926 {
7927 c_switch_stack->blocked_stmt_expr++;
7928 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7929 }
7930 for (glist = label_context_stack_se->labels_used;
7931 glist != NULL;
7932 glist = glist->next)
7933 {
7934 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7935 }
7936 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7937 nstack->labels_def = NULL;
7938 nstack->labels_used = NULL;
7939 nstack->next = label_context_stack_se;
7940 label_context_stack_se = nstack;
7941
7942 /* Mark the current statement list as belonging to a statement list. */
7943 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7944
7945 return ret;
7946 }
7947
7948 tree
c_finish_stmt_expr(tree body)7949 c_finish_stmt_expr (tree body)
7950 {
7951 tree last, type, tmp, val;
7952 tree *last_p;
7953 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7954
7955 body = c_end_compound_stmt (body, true);
7956 if (c_switch_stack)
7957 {
7958 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7959 c_switch_stack->blocked_stmt_expr--;
7960 }
7961 /* It is no longer possible to jump to labels defined within this
7962 statement expression. */
7963 for (dlist = label_context_stack_se->labels_def;
7964 dlist != NULL;
7965 dlist = dlist->next)
7966 {
7967 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7968 }
7969 /* It is again possible to define labels with a goto just outside
7970 this statement expression. */
7971 for (glist = label_context_stack_se->next->labels_used;
7972 glist != NULL;
7973 glist = glist->next)
7974 {
7975 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7976 glist_prev = glist;
7977 }
7978 if (glist_prev != NULL)
7979 glist_prev->next = label_context_stack_se->labels_used;
7980 else
7981 label_context_stack_se->next->labels_used
7982 = label_context_stack_se->labels_used;
7983 label_context_stack_se = label_context_stack_se->next;
7984
7985 /* Locate the last statement in BODY. See c_end_compound_stmt
7986 about always returning a BIND_EXPR. */
7987 last_p = &BIND_EXPR_BODY (body);
7988 last = BIND_EXPR_BODY (body);
7989
7990 continue_searching:
7991 if (TREE_CODE (last) == STATEMENT_LIST)
7992 {
7993 tree_stmt_iterator i;
7994
7995 /* This can happen with degenerate cases like ({ }). No value. */
7996 if (!TREE_SIDE_EFFECTS (last))
7997 return body;
7998
7999 /* If we're supposed to generate side effects warnings, process
8000 all of the statements except the last. */
8001 if (extra_warnings || warn_unused_value)
8002 {
8003 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8004 emit_side_effect_warnings (tsi_stmt (i));
8005 }
8006 else
8007 i = tsi_last (last);
8008 last_p = tsi_stmt_ptr (i);
8009 last = *last_p;
8010 }
8011
8012 /* If the end of the list is exception related, then the list was split
8013 by a call to push_cleanup. Continue searching. */
8014 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8015 || TREE_CODE (last) == TRY_CATCH_EXPR)
8016 {
8017 last_p = &TREE_OPERAND (last, 0);
8018 last = *last_p;
8019 goto continue_searching;
8020 }
8021
8022 /* In the case that the BIND_EXPR is not necessary, return the
8023 expression out from inside it. */
8024 if (last == error_mark_node
8025 || (last == BIND_EXPR_BODY (body)
8026 && BIND_EXPR_VARS (body) == NULL))
8027 {
8028 /* Do not warn if the return value of a statement expression is
8029 unused. */
8030 if (EXPR_P (last))
8031 TREE_NO_WARNING (last) = 1;
8032 return last;
8033 }
8034
8035 /* Extract the type of said expression. */
8036 type = TREE_TYPE (last);
8037
8038 /* If we're not returning a value at all, then the BIND_EXPR that
8039 we already have is a fine expression to return. */
8040 if (!type || VOID_TYPE_P (type))
8041 return body;
8042
8043 /* Now that we've located the expression containing the value, it seems
8044 silly to make voidify_wrapper_expr repeat the process. Create a
8045 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8046 tmp = create_tmp_var_raw (type, NULL);
8047
8048 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8049 tree_expr_nonnegative_p giving up immediately. */
8050 val = last;
8051 if (TREE_CODE (val) == NOP_EXPR
8052 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8053 val = TREE_OPERAND (val, 0);
8054
8055 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8056 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
8057
8058 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8059 }
8060
8061 /* Begin the scope of an identifier of variably modified type, scope
8062 number SCOPE. Jumping from outside this scope to inside it is not
8063 permitted. */
8064
8065 void
c_begin_vm_scope(unsigned int scope)8066 c_begin_vm_scope (unsigned int scope)
8067 {
8068 struct c_label_context_vm *nstack;
8069 struct c_label_list *glist;
8070
8071 gcc_assert (scope > 0);
8072
8073 /* At file_scope, we don't have to do any processing. */
8074 if (label_context_stack_vm == NULL)
8075 return;
8076
8077 if (c_switch_stack && !c_switch_stack->blocked_vm)
8078 c_switch_stack->blocked_vm = scope;
8079 for (glist = label_context_stack_vm->labels_used;
8080 glist != NULL;
8081 glist = glist->next)
8082 {
8083 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
8084 }
8085 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
8086 nstack->labels_def = NULL;
8087 nstack->labels_used = NULL;
8088 nstack->scope = scope;
8089 nstack->next = label_context_stack_vm;
8090 label_context_stack_vm = nstack;
8091 }
8092
8093 /* End a scope which may contain identifiers of variably modified
8094 type, scope number SCOPE. */
8095
8096 void
c_end_vm_scope(unsigned int scope)8097 c_end_vm_scope (unsigned int scope)
8098 {
8099 if (label_context_stack_vm == NULL)
8100 return;
8101 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
8102 c_switch_stack->blocked_vm = 0;
8103 /* We may have a number of nested scopes of identifiers with
8104 variably modified type, all at this depth. Pop each in turn. */
8105 while (label_context_stack_vm->scope == scope)
8106 {
8107 struct c_label_list *dlist, *glist, *glist_prev = NULL;
8108
8109 /* It is no longer possible to jump to labels defined within this
8110 scope. */
8111 for (dlist = label_context_stack_vm->labels_def;
8112 dlist != NULL;
8113 dlist = dlist->next)
8114 {
8115 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
8116 }
8117 /* It is again possible to define labels with a goto just outside
8118 this scope. */
8119 for (glist = label_context_stack_vm->next->labels_used;
8120 glist != NULL;
8121 glist = glist->next)
8122 {
8123 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
8124 glist_prev = glist;
8125 }
8126 if (glist_prev != NULL)
8127 glist_prev->next = label_context_stack_vm->labels_used;
8128 else
8129 label_context_stack_vm->next->labels_used
8130 = label_context_stack_vm->labels_used;
8131 label_context_stack_vm = label_context_stack_vm->next;
8132 }
8133 }
8134
8135 /* Begin and end compound statements. This is as simple as pushing
8136 and popping new statement lists from the tree. */
8137
8138 tree
c_begin_compound_stmt(bool do_scope)8139 c_begin_compound_stmt (bool do_scope)
8140 {
8141 tree stmt = push_stmt_list ();
8142 if (do_scope)
8143 push_scope ();
8144 return stmt;
8145 }
8146
8147 tree
c_end_compound_stmt(tree stmt,bool do_scope)8148 c_end_compound_stmt (tree stmt, bool do_scope)
8149 {
8150 tree block = NULL;
8151
8152 if (do_scope)
8153 {
8154 if (c_dialect_objc ())
8155 objc_clear_super_receiver ();
8156 block = pop_scope ();
8157 }
8158
8159 stmt = pop_stmt_list (stmt);
8160 stmt = c_build_bind_expr (block, stmt);
8161
8162 /* If this compound statement is nested immediately inside a statement
8163 expression, then force a BIND_EXPR to be created. Otherwise we'll
8164 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8165 STATEMENT_LISTs merge, and thus we can lose track of what statement
8166 was really last. */
8167 if (cur_stmt_list
8168 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8169 && TREE_CODE (stmt) != BIND_EXPR)
8170 {
8171 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8172 TREE_SIDE_EFFECTS (stmt) = 1;
8173 }
8174
8175 return stmt;
8176 }
8177
8178 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8179 when the current scope is exited. EH_ONLY is true when this is not
8180 meant to apply to normal control flow transfer. */
8181
8182 void
push_cleanup(tree ARG_UNUSED (decl),tree cleanup,bool eh_only)8183 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
8184 {
8185 enum tree_code code;
8186 tree stmt, list;
8187 bool stmt_expr;
8188
8189 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8190 stmt = build_stmt (code, NULL, cleanup);
8191 add_stmt (stmt);
8192 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8193 list = push_stmt_list ();
8194 TREE_OPERAND (stmt, 0) = list;
8195 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8196 }
8197
8198 /* Build a binary-operation expression without default conversions.
8199 CODE is the kind of expression to build.
8200 This function differs from `build' in several ways:
8201 the data type of the result is computed and recorded in it,
8202 warnings are generated if arg data types are invalid,
8203 special handling for addition and subtraction of pointers is known,
8204 and some optimization is done (operations on narrow ints
8205 are done in the narrower type when that gives the same result).
8206 Constant folding is also done before the result is returned.
8207
8208 Note that the operands will never have enumeral types, or function
8209 or array types, because either they will have the default conversions
8210 performed or they have both just been converted to some other type in which
8211 the arithmetic is to be done. */
8212
8213 tree
build_binary_op(enum tree_code code,tree orig_op0,tree orig_op1,int convert_p)8214 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
8215 int convert_p)
8216 {
8217 tree type0, type1;
8218 enum tree_code code0, code1;
8219 tree op0, op1;
8220 const char *invalid_op_diag;
8221
8222 /* Expression code to give to the expression when it is built.
8223 Normally this is CODE, which is what the caller asked for,
8224 but in some special cases we change it. */
8225 enum tree_code resultcode = code;
8226
8227 /* Data type in which the computation is to be performed.
8228 In the simplest cases this is the common type of the arguments. */
8229 tree result_type = NULL;
8230
8231 /* Nonzero means operands have already been type-converted
8232 in whatever way is necessary.
8233 Zero means they need to be converted to RESULT_TYPE. */
8234 int converted = 0;
8235
8236 /* Nonzero means create the expression with this type, rather than
8237 RESULT_TYPE. */
8238 tree build_type = 0;
8239
8240 /* Nonzero means after finally constructing the expression
8241 convert it to this type. */
8242 tree final_type = 0;
8243
8244 /* Nonzero if this is an operation like MIN or MAX which can
8245 safely be computed in short if both args are promoted shorts.
8246 Also implies COMMON.
8247 -1 indicates a bitwise operation; this makes a difference
8248 in the exact conditions for when it is safe to do the operation
8249 in a narrower mode. */
8250 int shorten = 0;
8251
8252 /* Nonzero if this is a comparison operation;
8253 if both args are promoted shorts, compare the original shorts.
8254 Also implies COMMON. */
8255 int short_compare = 0;
8256
8257 /* Nonzero if this is a right-shift operation, which can be computed on the
8258 original short and then promoted if the operand is a promoted short. */
8259 int short_shift = 0;
8260
8261 /* Nonzero means set RESULT_TYPE to the common type of the args. */
8262 int common = 0;
8263
8264 /* True means types are compatible as far as ObjC is concerned. */
8265 bool objc_ok;
8266
8267 if (convert_p)
8268 {
8269 op0 = default_conversion (orig_op0);
8270 op1 = default_conversion (orig_op1);
8271 }
8272 else
8273 {
8274 op0 = orig_op0;
8275 op1 = orig_op1;
8276 }
8277
8278 type0 = TREE_TYPE (op0);
8279 type1 = TREE_TYPE (op1);
8280
8281 /* The expression codes of the data types of the arguments tell us
8282 whether the arguments are integers, floating, pointers, etc. */
8283 code0 = TREE_CODE (type0);
8284 code1 = TREE_CODE (type1);
8285
8286 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
8287 STRIP_TYPE_NOPS (op0);
8288 STRIP_TYPE_NOPS (op1);
8289
8290 /* If an error was already reported for one of the arguments,
8291 avoid reporting another error. */
8292
8293 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8294 return error_mark_node;
8295
8296 if ((invalid_op_diag
8297 = targetm.invalid_binary_op (code, type0, type1)))
8298 {
8299 error (invalid_op_diag, "");
8300 return error_mark_node;
8301 }
8302
8303 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE, "comparison");
8304
8305 switch (code)
8306 {
8307 case PLUS_EXPR:
8308 /* Handle the pointer + int case. */
8309 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8310 return pointer_int_sum (PLUS_EXPR, op0, op1);
8311 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
8312 return pointer_int_sum (PLUS_EXPR, op1, op0);
8313 else
8314 common = 1;
8315 break;
8316
8317 case MINUS_EXPR:
8318 /* Subtraction of two similar pointers.
8319 We must subtract them as integers, then divide by object size. */
8320 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
8321 && comp_target_types (type0, type1))
8322 return pointer_diff (op0, op1);
8323 /* Handle pointer minus int. Just like pointer plus int. */
8324 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8325 return pointer_int_sum (MINUS_EXPR, op0, op1);
8326 else
8327 common = 1;
8328 break;
8329
8330 case MULT_EXPR:
8331 common = 1;
8332 break;
8333
8334 case TRUNC_DIV_EXPR:
8335 case CEIL_DIV_EXPR:
8336 case FLOOR_DIV_EXPR:
8337 case ROUND_DIV_EXPR:
8338 case EXACT_DIV_EXPR:
8339 /* Floating point division by zero is a legitimate way to obtain
8340 infinities and NaNs. */
8341 if (skip_evaluation == 0 && integer_zerop (op1))
8342 warning (OPT_Wdiv_by_zero, "division by zero");
8343
8344 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8345 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8346 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8347 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8348 {
8349 enum tree_code tcode0 = code0, tcode1 = code1;
8350
8351 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
8352 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
8353 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
8354 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
8355
8356 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
8357 resultcode = RDIV_EXPR;
8358 else
8359 /* Although it would be tempting to shorten always here, that
8360 loses on some targets, since the modulo instruction is
8361 undefined if the quotient can't be represented in the
8362 computation mode. We shorten only if unsigned or if
8363 dividing by something we know != -1. */
8364 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8365 || (TREE_CODE (op1) == INTEGER_CST
8366 && !integer_all_onesp (op1)));
8367 common = 1;
8368 }
8369 break;
8370
8371 case BIT_AND_EXPR:
8372 case BIT_IOR_EXPR:
8373 case BIT_XOR_EXPR:
8374 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8375 shorten = -1;
8376 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
8377 common = 1;
8378 break;
8379
8380 case TRUNC_MOD_EXPR:
8381 case FLOOR_MOD_EXPR:
8382 if (skip_evaluation == 0 && integer_zerop (op1))
8383 warning (OPT_Wdiv_by_zero, "division by zero");
8384
8385 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8386 {
8387 /* Although it would be tempting to shorten always here, that loses
8388 on some targets, since the modulo instruction is undefined if the
8389 quotient can't be represented in the computation mode. We shorten
8390 only if unsigned or if dividing by something we know != -1. */
8391 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8392 || (TREE_CODE (op1) == INTEGER_CST
8393 && !integer_all_onesp (op1)));
8394 common = 1;
8395 }
8396 break;
8397
8398 case TRUTH_ANDIF_EXPR:
8399 case TRUTH_ORIF_EXPR:
8400 case TRUTH_AND_EXPR:
8401 case TRUTH_OR_EXPR:
8402 case TRUTH_XOR_EXPR:
8403 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8404 /* APPLE LOCAL radar 5928316 */
8405 || code0 == BLOCK_POINTER_TYPE
8406 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
8407 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8408 /* APPLE LOCAL radar 5928316 */
8409 || code1 == BLOCK_POINTER_TYPE
8410 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
8411 {
8412 /* Result of these operations is always an int,
8413 but that does not mean the operands should be
8414 converted to ints! */
8415 result_type = integer_type_node;
8416 op0 = c_common_truthvalue_conversion (op0);
8417 op1 = c_common_truthvalue_conversion (op1);
8418 converted = 1;
8419 }
8420 break;
8421
8422 /* Shift operations: result has same type as first operand;
8423 always convert second operand to int.
8424 Also set SHORT_SHIFT if shifting rightward. */
8425
8426 case RSHIFT_EXPR:
8427 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8428 {
8429 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8430 {
8431 if (tree_int_cst_sgn (op1) < 0)
8432 warning (0, "right shift count is negative");
8433 else
8434 {
8435 if (!integer_zerop (op1))
8436 short_shift = 1;
8437
8438 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8439 warning (0, "right shift count >= width of type");
8440 }
8441 }
8442
8443 /* Use the type of the value to be shifted. */
8444 result_type = type0;
8445 /* Convert the shift-count to an integer, regardless of size
8446 of value being shifted. */
8447 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8448 op1 = convert (integer_type_node, op1);
8449 /* Avoid converting op1 to result_type later. */
8450 converted = 1;
8451 }
8452 break;
8453
8454 case LSHIFT_EXPR:
8455 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8456 {
8457 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8458 {
8459 if (tree_int_cst_sgn (op1) < 0)
8460 warning (0, "left shift count is negative");
8461
8462 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8463 warning (0, "left shift count >= width of type");
8464 }
8465
8466 /* Use the type of the value to be shifted. */
8467 result_type = type0;
8468 /* Convert the shift-count to an integer, regardless of size
8469 of value being shifted. */
8470 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8471 op1 = convert (integer_type_node, op1);
8472 /* Avoid converting op1 to result_type later. */
8473 converted = 1;
8474 }
8475 break;
8476
8477 case EQ_EXPR:
8478 case NE_EXPR:
8479 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8480 warning (OPT_Wfloat_equal,
8481 "comparing floating point with == or != is unsafe");
8482 /* Result of comparison is always int,
8483 but don't convert the args to int! */
8484 build_type = integer_type_node;
8485 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8486 || code0 == COMPLEX_TYPE)
8487 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8488 || code1 == COMPLEX_TYPE))
8489 short_compare = 1;
8490 /* APPLE LOCAL begin blocks 6065211 */
8491 else if ((code0 == POINTER_TYPE || code0 == BLOCK_POINTER_TYPE)
8492 && (code1 == POINTER_TYPE || code1 == BLOCK_POINTER_TYPE))
8493 /* APPLE LOCAL end blocks 6065211 */
8494 {
8495 tree tt0 = TREE_TYPE (type0);
8496 tree tt1 = TREE_TYPE (type1);
8497 /* Anything compares with void *. void * compares with anything.
8498 Otherwise, the targets must be compatible
8499 and both must be object or both incomplete. */
8500 if (comp_target_types (type0, type1))
8501 result_type = common_pointer_type (type0, type1);
8502 /* APPLE LOCAL begin blocks 6065211 */
8503 else if (code1 == BLOCK_POINTER_TYPE && VOID_TYPE_P (tt0))
8504 ;
8505 else if (code0 == BLOCK_POINTER_TYPE && VOID_TYPE_P (tt1))
8506 ;
8507 /* APPLE LOCAL end blocks 6065211 */
8508 else if (VOID_TYPE_P (tt0))
8509 {
8510 /* op0 != orig_op0 detects the case of something
8511 whose value is 0 but which isn't a valid null ptr const. */
8512 if (pedantic && !null_pointer_constant_p (orig_op0)
8513 && TREE_CODE (tt1) == FUNCTION_TYPE)
8514 pedwarn ("ISO C forbids comparison of %<void *%>"
8515 " with function pointer");
8516 }
8517 else if (VOID_TYPE_P (tt1))
8518 {
8519 if (pedantic && !null_pointer_constant_p (orig_op1)
8520 && TREE_CODE (tt0) == FUNCTION_TYPE)
8521 pedwarn ("ISO C forbids comparison of %<void *%>"
8522 " with function pointer");
8523 }
8524 else
8525 /* Avoid warning about the volatile ObjC EH puts on decls. */
8526 if (!objc_ok)
8527 /* APPLE LOCAL begin blocks 6065211 */
8528 {
8529 if (code0 == BLOCK_POINTER_TYPE || code1 == BLOCK_POINTER_TYPE)
8530 pedwarn ("comparison of distinct block types lacks a cast");
8531 else
8532 pedwarn ("comparison of distinct pointer types lacks a cast");
8533 }
8534 /* APPLE LOCAL end blocks 6065211 */
8535
8536 if (result_type == NULL_TREE)
8537 result_type = ptr_type_node;
8538 }
8539 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8540 {
8541 if (TREE_CODE (op0) == ADDR_EXPR
8542 && DECL_P (TREE_OPERAND (op0, 0))
8543 && (TREE_CODE (TREE_OPERAND (op0, 0)) == PARM_DECL
8544 || TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
8545 || !DECL_WEAK (TREE_OPERAND (op0, 0))))
8546 warning (OPT_Waddress, "the address of %qD will never be NULL",
8547 TREE_OPERAND (op0, 0));
8548 result_type = type0;
8549 }
8550 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8551 {
8552 if (TREE_CODE (op1) == ADDR_EXPR
8553 && DECL_P (TREE_OPERAND (op1, 0))
8554 && (TREE_CODE (TREE_OPERAND (op1, 0)) == PARM_DECL
8555 || TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL
8556 || !DECL_WEAK (TREE_OPERAND (op1, 0))))
8557 warning (OPT_Waddress, "the address of %qD will never be NULL",
8558 TREE_OPERAND (op1, 0));
8559 result_type = type1;
8560 }
8561 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8562 {
8563 result_type = type0;
8564 pedwarn ("comparison between pointer and integer");
8565 }
8566 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8567 {
8568 result_type = type1;
8569 pedwarn ("comparison between pointer and integer");
8570 }
8571 /* APPLE LOCAL begin radar 5732232 - blocks (C++ cl) */
8572 else if (code0 == BLOCK_POINTER_TYPE && null_pointer_constant_p (orig_op1))
8573 result_type = type0;
8574 else if (code1 == BLOCK_POINTER_TYPE && null_pointer_constant_p (orig_op0))
8575 result_type = type1;
8576 /* APPLE LOCAL end radar 5732232 - blocks (C++ cl) */
8577 break;
8578
8579 case LE_EXPR:
8580 case GE_EXPR:
8581 case LT_EXPR:
8582 case GT_EXPR:
8583 build_type = integer_type_node;
8584 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
8585 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
8586 short_compare = 1;
8587 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8588 {
8589 if (comp_target_types (type0, type1))
8590 {
8591 result_type = common_pointer_type (type0, type1);
8592 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8593 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8594 pedwarn ("comparison of complete and incomplete pointers");
8595 else if (pedantic
8596 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8597 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8598 }
8599 else
8600 {
8601 result_type = ptr_type_node;
8602 pedwarn ("comparison of distinct pointer types lacks a cast");
8603 }
8604 }
8605 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
8606 {
8607 result_type = type0;
8608 if (pedantic || extra_warnings)
8609 pedwarn ("ordered comparison of pointer with integer zero");
8610 }
8611 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
8612 {
8613 result_type = type1;
8614 if (pedantic)
8615 pedwarn ("ordered comparison of pointer with integer zero");
8616 }
8617 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8618 {
8619 result_type = type0;
8620 pedwarn ("comparison between pointer and integer");
8621 }
8622 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8623 {
8624 result_type = type1;
8625 pedwarn ("comparison between pointer and integer");
8626 }
8627 break;
8628
8629 default:
8630 gcc_unreachable ();
8631 }
8632
8633 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8634 return error_mark_node;
8635
8636 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8637 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8638 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8639 TREE_TYPE (type1))))
8640 {
8641 binary_op_error (code, type0, type1);
8642 return error_mark_node;
8643 }
8644
8645 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8646 || code0 == VECTOR_TYPE)
8647 &&
8648 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8649 || code1 == VECTOR_TYPE))
8650 {
8651 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8652
8653 if (shorten || common || short_compare)
8654 result_type = c_common_type (type0, type1);
8655
8656 /* For certain operations (which identify themselves by shorten != 0)
8657 if both args were extended from the same smaller type,
8658 do the arithmetic in that type and then extend.
8659
8660 shorten !=0 and !=1 indicates a bitwise operation.
8661 For them, this optimization is safe only if
8662 both args are zero-extended or both are sign-extended.
8663 Otherwise, we might change the result.
8664 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8665 but calculated in (unsigned short) it would be (unsigned short)-1. */
8666
8667 if (shorten && none_complex)
8668 {
8669 int unsigned0, unsigned1;
8670 tree arg0, arg1;
8671 int uns;
8672 tree type;
8673
8674 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
8675 excessive narrowing when we call get_narrower below. For
8676 example, suppose that OP0 is of unsigned int extended
8677 from signed char and that RESULT_TYPE is long long int.
8678 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
8679 like
8680
8681 (long long int) (unsigned int) signed_char
8682
8683 which get_narrower would narrow down to
8684
8685 (unsigned int) signed char
8686
8687 If we do not cast OP0 first, get_narrower would return
8688 signed_char, which is inconsistent with the case of the
8689 explicit cast. */
8690 op0 = convert (result_type, op0);
8691 op1 = convert (result_type, op1);
8692
8693 arg0 = get_narrower (op0, &unsigned0);
8694 arg1 = get_narrower (op1, &unsigned1);
8695
8696 /* UNS is 1 if the operation to be done is an unsigned one. */
8697 uns = TYPE_UNSIGNED (result_type);
8698
8699 final_type = result_type;
8700
8701 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8702 but it *requires* conversion to FINAL_TYPE. */
8703
8704 if ((TYPE_PRECISION (TREE_TYPE (op0))
8705 == TYPE_PRECISION (TREE_TYPE (arg0)))
8706 && TREE_TYPE (op0) != final_type)
8707 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8708 if ((TYPE_PRECISION (TREE_TYPE (op1))
8709 == TYPE_PRECISION (TREE_TYPE (arg1)))
8710 && TREE_TYPE (op1) != final_type)
8711 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8712
8713 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8714
8715 /* For bitwise operations, signedness of nominal type
8716 does not matter. Consider only how operands were extended. */
8717 if (shorten == -1)
8718 uns = unsigned0;
8719
8720 /* Note that in all three cases below we refrain from optimizing
8721 an unsigned operation on sign-extended args.
8722 That would not be valid. */
8723
8724 /* Both args variable: if both extended in same way
8725 from same width, do it in that width.
8726 Do it unsigned if args were zero-extended. */
8727 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8728 < TYPE_PRECISION (result_type))
8729 && (TYPE_PRECISION (TREE_TYPE (arg1))
8730 == TYPE_PRECISION (TREE_TYPE (arg0)))
8731 && unsigned0 == unsigned1
8732 && (unsigned0 || !uns))
8733 result_type
8734 = c_common_signed_or_unsigned_type
8735 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8736 else if (TREE_CODE (arg0) == INTEGER_CST
8737 && (unsigned1 || !uns)
8738 && (TYPE_PRECISION (TREE_TYPE (arg1))
8739 < TYPE_PRECISION (result_type))
8740 && (type
8741 = c_common_signed_or_unsigned_type (unsigned1,
8742 TREE_TYPE (arg1)),
8743 int_fits_type_p (arg0, type)))
8744 result_type = type;
8745 else if (TREE_CODE (arg1) == INTEGER_CST
8746 && (unsigned0 || !uns)
8747 && (TYPE_PRECISION (TREE_TYPE (arg0))
8748 < TYPE_PRECISION (result_type))
8749 && (type
8750 = c_common_signed_or_unsigned_type (unsigned0,
8751 TREE_TYPE (arg0)),
8752 int_fits_type_p (arg1, type)))
8753 result_type = type;
8754 }
8755
8756 /* Shifts can be shortened if shifting right. */
8757
8758 if (short_shift)
8759 {
8760 int unsigned_arg;
8761 tree arg0 = get_narrower (op0, &unsigned_arg);
8762
8763 final_type = result_type;
8764
8765 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8766 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8767
8768 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8769 /* We can shorten only if the shift count is less than the
8770 number of bits in the smaller type size. */
8771 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8772 /* We cannot drop an unsigned shift after sign-extension. */
8773 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8774 {
8775 /* Do an unsigned shift if the operand was zero-extended. */
8776 result_type
8777 = c_common_signed_or_unsigned_type (unsigned_arg,
8778 TREE_TYPE (arg0));
8779 /* Convert value-to-be-shifted to that type. */
8780 if (TREE_TYPE (op0) != result_type)
8781 op0 = convert (result_type, op0);
8782 converted = 1;
8783 }
8784 }
8785
8786 /* Comparison operations are shortened too but differently.
8787 They identify themselves by setting short_compare = 1. */
8788
8789 if (short_compare)
8790 {
8791 /* Don't write &op0, etc., because that would prevent op0
8792 from being kept in a register.
8793 Instead, make copies of the our local variables and
8794 pass the copies by reference, then copy them back afterward. */
8795 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8796 enum tree_code xresultcode = resultcode;
8797 tree val
8798 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8799
8800 if (val != 0)
8801 return val;
8802
8803 op0 = xop0, op1 = xop1;
8804 converted = 1;
8805 resultcode = xresultcode;
8806
8807 if (warn_sign_compare && skip_evaluation == 0)
8808 {
8809 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8810 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8811 int unsignedp0, unsignedp1;
8812 tree primop0 = get_narrower (op0, &unsignedp0);
8813 tree primop1 = get_narrower (op1, &unsignedp1);
8814
8815 xop0 = orig_op0;
8816 xop1 = orig_op1;
8817 STRIP_TYPE_NOPS (xop0);
8818 STRIP_TYPE_NOPS (xop1);
8819
8820 /* Give warnings for comparisons between signed and unsigned
8821 quantities that may fail.
8822
8823 Do the checking based on the original operand trees, so that
8824 casts will be considered, but default promotions won't be.
8825
8826 Do not warn if the comparison is being done in a signed type,
8827 since the signed type will only be chosen if it can represent
8828 all the values of the unsigned type. */
8829 if (!TYPE_UNSIGNED (result_type))
8830 /* OK */;
8831 /* Do not warn if both operands are the same signedness. */
8832 else if (op0_signed == op1_signed)
8833 /* OK */;
8834 else
8835 {
8836 tree sop, uop;
8837 bool ovf;
8838
8839 if (op0_signed)
8840 sop = xop0, uop = xop1;
8841 else
8842 sop = xop1, uop = xop0;
8843
8844 /* Do not warn if the signed quantity is an
8845 unsuffixed integer literal (or some static
8846 constant expression involving such literals or a
8847 conditional expression involving such literals)
8848 and it is non-negative. */
8849 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
8850 /* OK */;
8851 /* Do not warn if the comparison is an equality operation,
8852 the unsigned quantity is an integral constant, and it
8853 would fit in the result if the result were signed. */
8854 else if (TREE_CODE (uop) == INTEGER_CST
8855 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8856 && int_fits_type_p
8857 (uop, c_common_signed_type (result_type)))
8858 /* OK */;
8859 /* Do not warn if the unsigned quantity is an enumeration
8860 constant and its maximum value would fit in the result
8861 if the result were signed. */
8862 else if (TREE_CODE (uop) == INTEGER_CST
8863 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8864 && int_fits_type_p
8865 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8866 c_common_signed_type (result_type)))
8867 /* OK */;
8868 else
8869 warning (0, "comparison between signed and unsigned");
8870 }
8871
8872 /* Warn if two unsigned values are being compared in a size
8873 larger than their original size, and one (and only one) is the
8874 result of a `~' operator. This comparison will always fail.
8875
8876 Also warn if one operand is a constant, and the constant
8877 does not have all bits set that are set in the ~ operand
8878 when it is extended. */
8879
8880 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8881 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8882 {
8883 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8884 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8885 &unsignedp0);
8886 else
8887 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8888 &unsignedp1);
8889
8890 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8891 {
8892 tree primop;
8893 HOST_WIDE_INT constant, mask;
8894 int unsignedp, bits;
8895
8896 if (host_integerp (primop0, 0))
8897 {
8898 primop = primop1;
8899 unsignedp = unsignedp1;
8900 constant = tree_low_cst (primop0, 0);
8901 }
8902 else
8903 {
8904 primop = primop0;
8905 unsignedp = unsignedp0;
8906 constant = tree_low_cst (primop1, 0);
8907 }
8908
8909 bits = TYPE_PRECISION (TREE_TYPE (primop));
8910 if (bits < TYPE_PRECISION (result_type)
8911 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8912 {
8913 mask = (~(HOST_WIDE_INT) 0) << bits;
8914 if ((mask & constant) != mask)
8915 warning (0, "comparison of promoted ~unsigned with constant");
8916 }
8917 }
8918 else if (unsignedp0 && unsignedp1
8919 && (TYPE_PRECISION (TREE_TYPE (primop0))
8920 < TYPE_PRECISION (result_type))
8921 && (TYPE_PRECISION (TREE_TYPE (primop1))
8922 < TYPE_PRECISION (result_type)))
8923 warning (0, "comparison of promoted ~unsigned with unsigned");
8924 }
8925 }
8926 }
8927 }
8928
8929 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8930 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8931 Then the expression will be built.
8932 It will be given type FINAL_TYPE if that is nonzero;
8933 otherwise, it will be given type RESULT_TYPE. */
8934
8935 if (!result_type)
8936 {
8937 binary_op_error (code, TREE_TYPE (op0), TREE_TYPE (op1));
8938 return error_mark_node;
8939 }
8940
8941 if (!converted)
8942 {
8943 if (TREE_TYPE (op0) != result_type)
8944 op0 = convert_and_check (result_type, op0);
8945 if (TREE_TYPE (op1) != result_type)
8946 op1 = convert_and_check (result_type, op1);
8947
8948 /* This can happen if one operand has a vector type, and the other
8949 has a different type. */
8950 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8951 return error_mark_node;
8952 }
8953
8954 if (build_type == NULL_TREE)
8955 build_type = result_type;
8956
8957 {
8958 /* Treat expressions in initializers specially as they can't trap. */
8959 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8960 build_type,
8961 op0, op1)
8962 : fold_build2 (resultcode, build_type,
8963 op0, op1);
8964
8965 if (final_type != 0)
8966 result = convert (final_type, result);
8967 return result;
8968 }
8969 }
8970
8971
8972 /* Convert EXPR to be a truth-value, validating its type for this
8973 purpose. */
8974
8975 tree
c_objc_common_truthvalue_conversion(tree expr)8976 c_objc_common_truthvalue_conversion (tree expr)
8977 {
8978 switch (TREE_CODE (TREE_TYPE (expr)))
8979 {
8980 case ARRAY_TYPE:
8981 error ("used array that cannot be converted to pointer where scalar is required");
8982 return error_mark_node;
8983
8984 case RECORD_TYPE:
8985 error ("used struct type value where scalar is required");
8986 return error_mark_node;
8987
8988 case UNION_TYPE:
8989 error ("used union type value where scalar is required");
8990 return error_mark_node;
8991
8992 case FUNCTION_TYPE:
8993 gcc_unreachable ();
8994
8995 default:
8996 break;
8997 }
8998
8999 /* ??? Should we also give an error for void and vectors rather than
9000 leaving those to give errors later? */
9001 return c_common_truthvalue_conversion (expr);
9002 }
9003
9004
9005 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9006 required. */
9007
9008 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * ti ATTRIBUTE_UNUSED,bool * se)9009 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
9010 bool *ti ATTRIBUTE_UNUSED, bool *se)
9011 {
9012 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9013 {
9014 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9015 /* Executing a compound literal inside a function reinitializes
9016 it. */
9017 if (!TREE_STATIC (decl))
9018 *se = true;
9019 return decl;
9020 }
9021 else
9022 return expr;
9023 }
9024
9025 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9026
9027 tree
c_begin_omp_parallel(void)9028 c_begin_omp_parallel (void)
9029 {
9030 tree block;
9031
9032 keep_next_level ();
9033 block = c_begin_compound_stmt (true);
9034
9035 return block;
9036 }
9037
9038 tree
c_finish_omp_parallel(tree clauses,tree block)9039 c_finish_omp_parallel (tree clauses, tree block)
9040 {
9041 tree stmt;
9042
9043 block = c_end_compound_stmt (block, true);
9044
9045 stmt = make_node (OMP_PARALLEL);
9046 TREE_TYPE (stmt) = void_type_node;
9047 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9048 OMP_PARALLEL_BODY (stmt) = block;
9049
9050 return add_stmt (stmt);
9051 }
9052
9053 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9054 Remove any elements from the list that are invalid. */
9055
9056 tree
c_finish_omp_clauses(tree clauses)9057 c_finish_omp_clauses (tree clauses)
9058 {
9059 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9060 tree c, t, *pc = &clauses;
9061 const char *name;
9062
9063 bitmap_obstack_initialize (NULL);
9064 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9065 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9066 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9067
9068 for (pc = &clauses, c = clauses; c ; c = *pc)
9069 {
9070 bool remove = false;
9071 bool need_complete = false;
9072 bool need_implicitly_determined = false;
9073
9074 switch (OMP_CLAUSE_CODE (c))
9075 {
9076 case OMP_CLAUSE_SHARED:
9077 name = "shared";
9078 need_implicitly_determined = true;
9079 goto check_dup_generic;
9080
9081 case OMP_CLAUSE_PRIVATE:
9082 name = "private";
9083 need_complete = true;
9084 need_implicitly_determined = true;
9085 goto check_dup_generic;
9086
9087 case OMP_CLAUSE_REDUCTION:
9088 name = "reduction";
9089 need_implicitly_determined = true;
9090 t = OMP_CLAUSE_DECL (c);
9091 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
9092 || POINTER_TYPE_P (TREE_TYPE (t)))
9093 {
9094 error ("%qE has invalid type for %<reduction%>", t);
9095 remove = true;
9096 }
9097 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
9098 {
9099 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
9100 const char *r_name = NULL;
9101
9102 switch (r_code)
9103 {
9104 case PLUS_EXPR:
9105 case MULT_EXPR:
9106 case MINUS_EXPR:
9107 break;
9108 case BIT_AND_EXPR:
9109 r_name = "&";
9110 break;
9111 case BIT_XOR_EXPR:
9112 r_name = "^";
9113 break;
9114 case BIT_IOR_EXPR:
9115 r_name = "|";
9116 break;
9117 case TRUTH_ANDIF_EXPR:
9118 r_name = "&&";
9119 break;
9120 case TRUTH_ORIF_EXPR:
9121 r_name = "||";
9122 break;
9123 default:
9124 gcc_unreachable ();
9125 }
9126 if (r_name)
9127 {
9128 error ("%qE has invalid type for %<reduction(%s)%>",
9129 t, r_name);
9130 remove = true;
9131 }
9132 }
9133 goto check_dup_generic;
9134
9135 case OMP_CLAUSE_COPYPRIVATE:
9136 name = "copyprivate";
9137 goto check_dup_generic;
9138
9139 case OMP_CLAUSE_COPYIN:
9140 name = "copyin";
9141 t = OMP_CLAUSE_DECL (c);
9142 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
9143 {
9144 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
9145 remove = true;
9146 }
9147 goto check_dup_generic;
9148
9149 check_dup_generic:
9150 t = OMP_CLAUSE_DECL (c);
9151 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9152 {
9153 error ("%qE is not a variable in clause %qs", t, name);
9154 remove = true;
9155 }
9156 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9157 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9158 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9159 {
9160 error ("%qE appears more than once in data clauses", t);
9161 remove = true;
9162 }
9163 else
9164 bitmap_set_bit (&generic_head, DECL_UID (t));
9165 break;
9166
9167 case OMP_CLAUSE_FIRSTPRIVATE:
9168 name = "firstprivate";
9169 t = OMP_CLAUSE_DECL (c);
9170 need_complete = true;
9171 need_implicitly_determined = true;
9172 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9173 {
9174 error ("%qE is not a variable in clause %<firstprivate%>", t);
9175 remove = true;
9176 }
9177 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9178 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9179 {
9180 error ("%qE appears more than once in data clauses", t);
9181 remove = true;
9182 }
9183 else
9184 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
9185 break;
9186
9187 case OMP_CLAUSE_LASTPRIVATE:
9188 name = "lastprivate";
9189 t = OMP_CLAUSE_DECL (c);
9190 need_complete = true;
9191 need_implicitly_determined = true;
9192 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9193 {
9194 error ("%qE is not a variable in clause %<lastprivate%>", t);
9195 remove = true;
9196 }
9197 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
9198 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
9199 {
9200 error ("%qE appears more than once in data clauses", t);
9201 remove = true;
9202 }
9203 else
9204 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
9205 break;
9206
9207 case OMP_CLAUSE_IF:
9208 case OMP_CLAUSE_NUM_THREADS:
9209 case OMP_CLAUSE_SCHEDULE:
9210 case OMP_CLAUSE_NOWAIT:
9211 case OMP_CLAUSE_ORDERED:
9212 case OMP_CLAUSE_DEFAULT:
9213 pc = &OMP_CLAUSE_CHAIN (c);
9214 continue;
9215
9216 default:
9217 gcc_unreachable ();
9218 }
9219
9220 if (!remove)
9221 {
9222 t = OMP_CLAUSE_DECL (c);
9223
9224 if (need_complete)
9225 {
9226 t = require_complete_type (t);
9227 if (t == error_mark_node)
9228 remove = true;
9229 }
9230
9231 if (need_implicitly_determined)
9232 {
9233 const char *share_name = NULL;
9234
9235 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
9236 share_name = "threadprivate";
9237 else switch (c_omp_predetermined_sharing (t))
9238 {
9239 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9240 break;
9241 case OMP_CLAUSE_DEFAULT_SHARED:
9242 share_name = "shared";
9243 break;
9244 case OMP_CLAUSE_DEFAULT_PRIVATE:
9245 share_name = "private";
9246 break;
9247 default:
9248 gcc_unreachable ();
9249 }
9250 if (share_name)
9251 {
9252 error ("%qE is predetermined %qs for %qs",
9253 t, share_name, name);
9254 remove = true;
9255 }
9256 }
9257 }
9258
9259 if (remove)
9260 *pc = OMP_CLAUSE_CHAIN (c);
9261 else
9262 pc = &OMP_CLAUSE_CHAIN (c);
9263 }
9264
9265 bitmap_obstack_release (NULL);
9266 return clauses;
9267 }
9268