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.
6 This file is part of GCC.
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
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
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
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. */
31 #include "coretypes.h"
35 #include "langhooks.h"
45 #include "tree-iterator.h"
46 #include "tree-gimple.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
59 /* The level of nesting inside "__alignof__". */
62 /* The level of nesting inside "sizeof". */
65 /* The level of nesting inside "typeof". */
68 struct c_label_context_se *label_context_stack_se;
69 struct c_label_context_vm *label_context_stack_vm;
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (tree, tree);
81 static int comp_target_types (tree, tree);
82 static int function_types_compatible_p (tree, tree);
83 static int type_lists_compatible_p (tree, tree);
84 static tree decl_constant_value_for_broken_optimization (tree);
85 static tree lookup_field (tree, tree);
86 static tree convert_arguments (tree, tree, tree, tree);
87 static tree pointer_diff (tree, tree);
88 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
90 static tree valid_compound_expr_initializer (tree, tree);
91 static void push_string (const char *);
92 static void push_member_name (tree);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
109 static int comptypes_internal (tree, tree);
111 /* Return true if EXP is a null pointer constant, false otherwise. */
114 null_pointer_constant_p (tree expr)
116 /* This should really operate on c_expr structures, but they aren't
117 yet available everywhere required. */
118 tree type = TREE_TYPE (expr);
119 return (TREE_CODE (expr) == INTEGER_CST
120 && !TREE_CONSTANT_OVERFLOW (expr)
121 && integer_zerop (expr)
122 && (INTEGRAL_TYPE_P (type)
123 || (TREE_CODE (type) == POINTER_TYPE
124 && VOID_TYPE_P (TREE_TYPE (type))
125 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
127 \f/* This is a cache to hold if two types are compatible or not. */
129 struct tagged_tu_seen_cache {
130 const struct tagged_tu_seen_cache * next;
133 /* The return value of tagged_types_tu_compatible_p if we had seen
134 these two types already. */
138 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
139 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
141 /* Do `exp = require_complete_type (exp);' to make sure exp
142 does not have an incomplete type. (That includes void types.) */
145 require_complete_type (tree value)
147 tree type = TREE_TYPE (value);
149 if (value == error_mark_node || type == error_mark_node)
150 return error_mark_node;
152 /* First, detect a valid value with a complete type. */
153 if (COMPLETE_TYPE_P (type))
156 c_incomplete_type_error (value, type);
157 return error_mark_node;
160 /* Print an error message for invalid use of an incomplete type.
161 VALUE is the expression that was used (or 0 if that isn't known)
162 and TYPE is the type that was invalid. */
165 c_incomplete_type_error (tree value, tree type)
167 const char *type_code_string;
169 /* Avoid duplicate error message. */
170 if (TREE_CODE (type) == ERROR_MARK)
173 if (value != 0 && (TREE_CODE (value) == VAR_DECL
174 || TREE_CODE (value) == PARM_DECL))
175 error ("%qD has an incomplete type", value);
179 /* We must print an error message. Be clever about what it says. */
181 switch (TREE_CODE (type))
184 type_code_string = "struct";
188 type_code_string = "union";
192 type_code_string = "enum";
196 error ("invalid use of void expression");
200 if (TYPE_DOMAIN (type))
202 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
204 error ("invalid use of flexible array member");
207 type = TREE_TYPE (type);
210 error ("invalid use of array with unspecified bounds");
217 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
218 error ("invalid use of undefined type %<%s %E%>",
219 type_code_string, TYPE_NAME (type));
221 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
222 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
226 /* Given a type, apply default promotions wrt unnamed function
227 arguments and return the new type. */
230 c_type_promotes_to (tree type)
232 if (TYPE_MAIN_VARIANT (type) == float_type_node)
233 return double_type_node;
235 if (c_promoting_integer_type_p (type))
237 /* Preserve unsignedness if not really getting any wider. */
238 if (TYPE_UNSIGNED (type)
239 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
240 return unsigned_type_node;
241 return integer_type_node;
247 /* Return a variant of TYPE which has all the type qualifiers of LIKE
248 as well as those of TYPE. */
251 qualify_type (tree type, tree like)
253 return c_build_qualified_type (type,
254 TYPE_QUALS (type) | TYPE_QUALS (like));
257 /* Return true iff the given tree T is a variable length array. */
260 c_vla_type_p (tree t)
262 if (TREE_CODE (t) == ARRAY_TYPE
263 && C_TYPE_VARIABLE_SIZE (t))
268 /* Return the composite type of two compatible types.
270 We assume that comptypes has already been done and returned
271 nonzero; if that isn't so, this may crash. In particular, we
272 assume that qualifiers match. */
275 composite_type (tree t1, tree t2)
277 enum tree_code code1;
278 enum tree_code code2;
281 /* Save time if the two types are the same. */
283 if (t1 == t2) return t1;
285 /* If one type is nonsense, use the other. */
286 if (t1 == error_mark_node)
288 if (t2 == error_mark_node)
291 code1 = TREE_CODE (t1);
292 code2 = TREE_CODE (t2);
294 /* Merge the attributes. */
295 attributes = targetm.merge_type_attributes (t1, t2);
297 /* If one is an enumerated type and the other is the compatible
298 integer type, the composite type might be either of the two
299 (DR#013 question 3). For consistency, use the enumerated type as
300 the composite type. */
302 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
304 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
307 gcc_assert (code1 == code2);
312 /* For two pointers, do this recursively on the target type. */
314 tree pointed_to_1 = TREE_TYPE (t1);
315 tree pointed_to_2 = TREE_TYPE (t2);
316 tree target = composite_type (pointed_to_1, pointed_to_2);
317 t1 = build_pointer_type (target);
318 t1 = build_type_attribute_variant (t1, attributes);
319 return qualify_type (t1, t2);
324 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
327 tree d1 = TYPE_DOMAIN (t1);
328 tree d2 = TYPE_DOMAIN (t2);
329 bool d1_variable, d2_variable;
330 bool d1_zero, d2_zero;
332 /* We should not have any type quals on arrays at all. */
333 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
335 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
336 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
338 d1_variable = (!d1_zero
339 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
340 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
341 d2_variable = (!d2_zero
342 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
343 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
344 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
345 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
347 /* Save space: see if the result is identical to one of the args. */
348 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
349 && (d2_variable || d2_zero || !d1_variable))
350 return build_type_attribute_variant (t1, attributes);
351 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
352 && (d1_variable || d1_zero || !d2_variable))
353 return build_type_attribute_variant (t2, attributes);
355 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
356 return build_type_attribute_variant (t1, attributes);
357 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
358 return build_type_attribute_variant (t2, attributes);
360 /* Merge the element types, and have a size if either arg has
361 one. We may have qualifiers on the element types. To set
362 up TYPE_MAIN_VARIANT correctly, we need to form the
363 composite of the unqualified types and add the qualifiers
365 quals = TYPE_QUALS (strip_array_types (elt));
366 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
367 t1 = build_array_type (unqual_elt,
368 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
374 t1 = c_build_qualified_type (t1, quals);
375 return build_type_attribute_variant (t1, attributes);
379 /* Function types: prefer the one that specified arg types.
380 If both do, merge the arg types. Also merge the return types. */
382 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
383 tree p1 = TYPE_ARG_TYPES (t1);
384 tree p2 = TYPE_ARG_TYPES (t2);
389 /* Save space: see if the result is identical to one of the args. */
390 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
391 return build_type_attribute_variant (t1, attributes);
392 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
393 return build_type_attribute_variant (t2, attributes);
395 /* Simple way if one arg fails to specify argument types. */
396 if (TYPE_ARG_TYPES (t1) == 0)
398 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
402 if (TYPE_ARG_TYPES (t2) == 0)
404 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
409 /* If both args specify argument types, we must merge the two
410 lists, argument by argument. */
411 /* Tell global_bindings_p to return false so that variable_size
412 doesn't die on VLAs in parameter types. */
413 c_override_global_bindings_to_false = true;
415 len = list_length (p1);
418 for (i = 0; i < len; i++)
419 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
424 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
426 /* A null type means arg type is not specified.
427 Take whatever the other function type has. */
428 if (TREE_VALUE (p1) == 0)
430 TREE_VALUE (n) = TREE_VALUE (p2);
433 if (TREE_VALUE (p2) == 0)
435 TREE_VALUE (n) = TREE_VALUE (p1);
439 /* Given wait (union {union wait *u; int *i} *)
440 and wait (union wait *),
441 prefer union wait * as type of parm. */
442 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
443 && TREE_VALUE (p1) != TREE_VALUE (p2))
446 tree mv2 = TREE_VALUE (p2);
447 if (mv2 && mv2 != error_mark_node
448 && TREE_CODE (mv2) != ARRAY_TYPE)
449 mv2 = TYPE_MAIN_VARIANT (mv2);
450 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
451 memb; memb = TREE_CHAIN (memb))
453 tree mv3 = TREE_TYPE (memb);
454 if (mv3 && mv3 != error_mark_node
455 && TREE_CODE (mv3) != ARRAY_TYPE)
456 mv3 = TYPE_MAIN_VARIANT (mv3);
457 if (comptypes (mv3, mv2))
459 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
462 pedwarn ("function types not truly compatible in ISO C");
467 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
468 && TREE_VALUE (p2) != TREE_VALUE (p1))
471 tree mv1 = TREE_VALUE (p1);
472 if (mv1 && mv1 != error_mark_node
473 && TREE_CODE (mv1) != ARRAY_TYPE)
474 mv1 = TYPE_MAIN_VARIANT (mv1);
475 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
476 memb; memb = TREE_CHAIN (memb))
478 tree mv3 = TREE_TYPE (memb);
479 if (mv3 && mv3 != error_mark_node
480 && TREE_CODE (mv3) != ARRAY_TYPE)
481 mv3 = TYPE_MAIN_VARIANT (mv3);
482 if (comptypes (mv3, mv1))
484 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
487 pedwarn ("function types not truly compatible in ISO C");
492 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
496 c_override_global_bindings_to_false = false;
497 t1 = build_function_type (valtype, newargs);
498 t1 = qualify_type (t1, t2);
499 /* ... falls through ... */
503 return build_type_attribute_variant (t1, attributes);
508 /* Return the type of a conditional expression between pointers to
509 possibly differently qualified versions of compatible types.
511 We assume that comp_target_types has already been done and returned
512 nonzero; if that isn't so, this may crash. */
515 common_pointer_type (tree t1, tree t2)
518 tree pointed_to_1, mv1;
519 tree pointed_to_2, mv2;
522 /* Save time if the two types are the same. */
524 if (t1 == t2) return t1;
526 /* If one type is nonsense, use the other. */
527 if (t1 == error_mark_node)
529 if (t2 == error_mark_node)
532 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
533 && TREE_CODE (t2) == POINTER_TYPE);
535 /* Merge the attributes. */
536 attributes = targetm.merge_type_attributes (t1, t2);
538 /* Find the composite type of the target types, and combine the
539 qualifiers of the two types' targets. Do not lose qualifiers on
540 array element types by taking the TYPE_MAIN_VARIANT. */
541 mv1 = pointed_to_1 = TREE_TYPE (t1);
542 mv2 = pointed_to_2 = TREE_TYPE (t2);
543 if (TREE_CODE (mv1) != ARRAY_TYPE)
544 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
545 if (TREE_CODE (mv2) != ARRAY_TYPE)
546 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
547 target = composite_type (mv1, mv2);
548 t1 = build_pointer_type (c_build_qualified_type
550 TYPE_QUALS (pointed_to_1) |
551 TYPE_QUALS (pointed_to_2)));
552 return build_type_attribute_variant (t1, attributes);
555 /* Return the common type for two arithmetic types under the usual
556 arithmetic conversions. The default conversions have already been
557 applied, and enumerated types converted to their compatible integer
558 types. The resulting type is unqualified and has no attributes.
560 This is the type for the result of most arithmetic operations
561 if the operands have the given two types. */
564 c_common_type (tree t1, tree t2)
566 enum tree_code code1;
567 enum tree_code code2;
569 /* If one type is nonsense, use the other. */
570 if (t1 == error_mark_node)
572 if (t2 == error_mark_node)
575 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
576 t1 = TYPE_MAIN_VARIANT (t1);
578 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
579 t2 = TYPE_MAIN_VARIANT (t2);
581 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
582 t1 = build_type_attribute_variant (t1, NULL_TREE);
584 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
585 t2 = build_type_attribute_variant (t2, NULL_TREE);
587 /* Save time if the two types are the same. */
589 if (t1 == t2) return t1;
591 code1 = TREE_CODE (t1);
592 code2 = TREE_CODE (t2);
594 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
595 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
596 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
597 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
599 /* When one operand is a decimal float type, the other operand cannot be
600 a generic float type or a complex type. We also disallow vector types
602 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
603 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
605 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
607 error ("can%'t mix operands of decimal float and vector types");
608 return error_mark_node;
610 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
612 error ("can%'t mix operands of decimal float and complex types");
613 return error_mark_node;
615 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
617 error ("can%'t mix operands of decimal float and other float types");
618 return error_mark_node;
622 /* If one type is a vector type, return that type. (How the usual
623 arithmetic conversions apply to the vector types extension is not
624 precisely specified.) */
625 if (code1 == VECTOR_TYPE)
628 if (code2 == VECTOR_TYPE)
631 /* If one type is complex, form the common type of the non-complex
632 components, then make that complex. Use T1 or T2 if it is the
634 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
636 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
637 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
638 tree subtype = c_common_type (subtype1, subtype2);
640 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
642 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
645 return build_complex_type (subtype);
648 /* If only one is real, use it as the result. */
650 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
653 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
656 /* If both are real and either are decimal floating point types, use
657 the decimal floating point type with the greater precision. */
659 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
661 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
662 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
663 return dfloat128_type_node;
664 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
665 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
666 return dfloat64_type_node;
667 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
668 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
669 return dfloat32_type_node;
672 /* Both real or both integers; use the one with greater precision. */
674 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
676 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
679 /* Same precision. Prefer long longs to longs to ints when the
680 same precision, following the C99 rules on integer type rank
681 (which are equivalent to the C90 rules for C90 types). */
683 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
684 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
685 return long_long_unsigned_type_node;
687 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
688 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
690 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
691 return long_long_unsigned_type_node;
693 return long_long_integer_type_node;
696 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
697 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
698 return long_unsigned_type_node;
700 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
701 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
703 /* But preserve unsignedness from the other type,
704 since long cannot hold all the values of an unsigned int. */
705 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
706 return long_unsigned_type_node;
708 return long_integer_type_node;
711 /* Likewise, prefer long double to double even if same size. */
712 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
713 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
714 return long_double_type_node;
716 /* Otherwise prefer the unsigned one. */
718 if (TYPE_UNSIGNED (t1))
724 /* Wrapper around c_common_type that is used by c-common.c and other
725 front end optimizations that remove promotions. ENUMERAL_TYPEs
726 are allowed here and are converted to their compatible integer types.
727 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
728 preferably a non-Boolean type as the common type. */
730 common_type (tree t1, tree t2)
732 if (TREE_CODE (t1) == ENUMERAL_TYPE)
733 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
734 if (TREE_CODE (t2) == ENUMERAL_TYPE)
735 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
737 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
738 if (TREE_CODE (t1) == BOOLEAN_TYPE
739 && TREE_CODE (t2) == BOOLEAN_TYPE)
740 return boolean_type_node;
742 /* If either type is BOOLEAN_TYPE, then return the other. */
743 if (TREE_CODE (t1) == BOOLEAN_TYPE)
745 if (TREE_CODE (t2) == BOOLEAN_TYPE)
748 return c_common_type (t1, t2);
751 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
752 or various other operations. Return 2 if they are compatible
753 but a warning may be needed if you use them together. */
756 comptypes (tree type1, tree type2)
758 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
761 val = comptypes_internal (type1, type2);
762 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
767 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
768 or various other operations. Return 2 if they are compatible
769 but a warning may be needed if you use them together. This
770 differs from comptypes, in that we don't free the seen types. */
773 comptypes_internal (tree type1, tree type2)
779 /* Suppress errors caused by previously reported errors. */
781 if (t1 == t2 || !t1 || !t2
782 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
785 /* If either type is the internal version of sizetype, return the
787 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
788 && TYPE_ORIG_SIZE_TYPE (t1))
789 t1 = TYPE_ORIG_SIZE_TYPE (t1);
791 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
792 && TYPE_ORIG_SIZE_TYPE (t2))
793 t2 = TYPE_ORIG_SIZE_TYPE (t2);
796 /* Enumerated types are compatible with integer types, but this is
797 not transitive: two enumerated types in the same translation unit
798 are compatible with each other only if they are the same type. */
800 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
801 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
802 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
803 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
808 /* Different classes of types can't be compatible. */
810 if (TREE_CODE (t1) != TREE_CODE (t2))
813 /* Qualifiers must match. C99 6.7.3p9 */
815 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
818 /* Allow for two different type nodes which have essentially the same
819 definition. Note that we already checked for equality of the type
820 qualifiers (just above). */
822 if (TREE_CODE (t1) != ARRAY_TYPE
823 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
826 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
827 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
830 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
833 switch (TREE_CODE (t1))
836 /* Do not remove mode or aliasing information. */
837 if (TYPE_MODE (t1) != TYPE_MODE (t2)
838 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
840 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
841 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
845 val = function_types_compatible_p (t1, t2);
850 tree d1 = TYPE_DOMAIN (t1);
851 tree d2 = TYPE_DOMAIN (t2);
852 bool d1_variable, d2_variable;
853 bool d1_zero, d2_zero;
856 /* Target types must match incl. qualifiers. */
857 if (TREE_TYPE (t1) != TREE_TYPE (t2)
858 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
861 /* Sizes must match unless one is missing or variable. */
862 if (d1 == 0 || d2 == 0 || d1 == d2)
865 d1_zero = !TYPE_MAX_VALUE (d1);
866 d2_zero = !TYPE_MAX_VALUE (d2);
868 d1_variable = (!d1_zero
869 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
870 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
871 d2_variable = (!d2_zero
872 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
873 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
874 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
875 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
877 if (d1_variable || d2_variable)
879 if (d1_zero && d2_zero)
881 if (d1_zero || d2_zero
882 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
883 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
892 if (val != 1 && !same_translation_unit_p (t1, t2))
895 return tagged_types_tu_compatible_p (t1, t2);
896 val = tagged_types_tu_compatible_p (t1, t2);
901 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
902 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
908 return attrval == 2 && val == 1 ? 2 : val;
911 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
912 ignoring their qualifiers. */
915 comp_target_types (tree ttl, tree ttr)
920 /* Do not lose qualifiers on element types of array types that are
921 pointer targets by taking their TYPE_MAIN_VARIANT. */
922 mvl = TREE_TYPE (ttl);
923 mvr = TREE_TYPE (ttr);
924 if (TREE_CODE (mvl) != ARRAY_TYPE)
925 mvl = TYPE_MAIN_VARIANT (mvl);
926 if (TREE_CODE (mvr) != ARRAY_TYPE)
927 mvr = TYPE_MAIN_VARIANT (mvr);
928 val = comptypes (mvl, mvr);
930 if (val == 2 && pedantic)
931 pedwarn ("types are not quite compatible");
935 /* Subroutines of `comptypes'. */
937 /* Determine whether two trees derive from the same translation unit.
938 If the CONTEXT chain ends in a null, that tree's context is still
939 being parsed, so if two trees have context chains ending in null,
940 they're in the same translation unit. */
942 same_translation_unit_p (tree t1, tree t2)
944 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
945 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
947 case tcc_declaration:
948 t1 = DECL_CONTEXT (t1); break;
950 t1 = TYPE_CONTEXT (t1); break;
951 case tcc_exceptional:
952 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
953 default: gcc_unreachable ();
956 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
957 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
959 case tcc_declaration:
960 t2 = DECL_CONTEXT (t2); break;
962 t2 = TYPE_CONTEXT (t2); break;
963 case tcc_exceptional:
964 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
965 default: gcc_unreachable ();
971 /* Allocate the seen two types, assuming that they are compatible. */
973 static struct tagged_tu_seen_cache *
974 alloc_tagged_tu_seen_cache (tree t1, tree t2)
976 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
977 tu->next = tagged_tu_seen_base;
981 tagged_tu_seen_base = tu;
983 /* The C standard says that two structures in different translation
984 units are compatible with each other only if the types of their
985 fields are compatible (among other things). We assume that they
986 are compatible until proven otherwise when building the cache.
987 An example where this can occur is:
992 If we are comparing this against a similar struct in another TU,
993 and did not assume they were compatible, we end up with an infinite
999 /* Free the seen types until we get to TU_TIL. */
1002 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1004 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1005 while (tu != tu_til)
1007 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
1011 tagged_tu_seen_base = tu_til;
1014 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1015 compatible. If the two types are not the same (which has been
1016 checked earlier), this can only happen when multiple translation
1017 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1021 tagged_types_tu_compatible_p (tree t1, tree t2)
1024 bool needs_warning = false;
1026 /* We have to verify that the tags of the types are the same. This
1027 is harder than it looks because this may be a typedef, so we have
1028 to go look at the original type. It may even be a typedef of a
1030 In the case of compiler-created builtin structs the TYPE_DECL
1031 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1032 while (TYPE_NAME (t1)
1033 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1034 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1035 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1037 while (TYPE_NAME (t2)
1038 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1039 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1040 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1042 /* C90 didn't have the requirement that the two tags be the same. */
1043 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1046 /* C90 didn't say what happened if one or both of the types were
1047 incomplete; we choose to follow C99 rules here, which is that they
1049 if (TYPE_SIZE (t1) == NULL
1050 || TYPE_SIZE (t2) == NULL)
1054 const struct tagged_tu_seen_cache * tts_i;
1055 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1056 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1060 switch (TREE_CODE (t1))
1064 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1065 /* Speed up the case where the type values are in the same order. */
1066 tree tv1 = TYPE_VALUES (t1);
1067 tree tv2 = TYPE_VALUES (t2);
1074 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1076 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1078 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1085 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1089 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1095 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1101 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1103 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1105 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1116 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1117 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1123 /* Speed up the common case where the fields are in the same order. */
1124 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1125 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1130 if (DECL_NAME (s1) == NULL
1131 || DECL_NAME (s1) != DECL_NAME (s2))
1133 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1140 needs_warning = true;
1142 if (TREE_CODE (s1) == FIELD_DECL
1143 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1144 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1152 tu->val = needs_warning ? 2 : 1;
1156 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1160 if (DECL_NAME (s1) != NULL)
1161 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1162 if (DECL_NAME (s1) == DECL_NAME (s2))
1165 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1172 needs_warning = true;
1174 if (TREE_CODE (s1) == FIELD_DECL
1175 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1176 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1188 tu->val = needs_warning ? 2 : 10;
1194 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1196 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1198 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1201 if (TREE_CODE (s1) != TREE_CODE (s2)
1202 || DECL_NAME (s1) != DECL_NAME (s2))
1204 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1208 needs_warning = true;
1210 if (TREE_CODE (s1) == FIELD_DECL
1211 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1212 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1218 tu->val = needs_warning ? 2 : 1;
1227 /* Return 1 if two function types F1 and F2 are compatible.
1228 If either type specifies no argument types,
1229 the other must specify a fixed number of self-promoting arg types.
1230 Otherwise, if one type specifies only the number of arguments,
1231 the other must specify that number of self-promoting arg types.
1232 Otherwise, the argument types must match. */
1235 function_types_compatible_p (tree f1, tree f2)
1238 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1243 ret1 = TREE_TYPE (f1);
1244 ret2 = TREE_TYPE (f2);
1246 /* 'volatile' qualifiers on a function's return type used to mean
1247 the function is noreturn. */
1248 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1249 pedwarn ("function return types not compatible due to %<volatile%>");
1250 if (TYPE_VOLATILE (ret1))
1251 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1252 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1253 if (TYPE_VOLATILE (ret2))
1254 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1255 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1256 val = comptypes_internal (ret1, ret2);
1260 args1 = TYPE_ARG_TYPES (f1);
1261 args2 = TYPE_ARG_TYPES (f2);
1263 /* An unspecified parmlist matches any specified parmlist
1264 whose argument types don't need default promotions. */
1268 if (!self_promoting_args_p (args2))
1270 /* If one of these types comes from a non-prototype fn definition,
1271 compare that with the other type's arglist.
1272 If they don't match, ask for a warning (but no error). */
1273 if (TYPE_ACTUAL_ARG_TYPES (f1)
1274 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1280 if (!self_promoting_args_p (args1))
1282 if (TYPE_ACTUAL_ARG_TYPES (f2)
1283 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1288 /* Both types have argument lists: compare them and propagate results. */
1289 val1 = type_lists_compatible_p (args1, args2);
1290 return val1 != 1 ? val1 : val;
1293 /* Check two lists of types for compatibility,
1294 returning 0 for incompatible, 1 for compatible,
1295 or 2 for compatible with warning. */
1298 type_lists_compatible_p (tree args1, tree args2)
1300 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1306 tree a1, mv1, a2, mv2;
1307 if (args1 == 0 && args2 == 0)
1309 /* If one list is shorter than the other,
1310 they fail to match. */
1311 if (args1 == 0 || args2 == 0)
1313 mv1 = a1 = TREE_VALUE (args1);
1314 mv2 = a2 = TREE_VALUE (args2);
1315 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1316 mv1 = TYPE_MAIN_VARIANT (mv1);
1317 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1318 mv2 = TYPE_MAIN_VARIANT (mv2);
1319 /* A null pointer instead of a type
1320 means there is supposed to be an argument
1321 but nothing is specified about what type it has.
1322 So match anything that self-promotes. */
1325 if (c_type_promotes_to (a2) != a2)
1330 if (c_type_promotes_to (a1) != a1)
1333 /* If one of the lists has an error marker, ignore this arg. */
1334 else if (TREE_CODE (a1) == ERROR_MARK
1335 || TREE_CODE (a2) == ERROR_MARK)
1337 else if (!(newval = comptypes_internal (mv1, mv2)))
1339 /* Allow wait (union {union wait *u; int *i} *)
1340 and wait (union wait *) to be compatible. */
1341 if (TREE_CODE (a1) == UNION_TYPE
1342 && (TYPE_NAME (a1) == 0
1343 || TYPE_TRANSPARENT_UNION (a1))
1344 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1345 && tree_int_cst_equal (TYPE_SIZE (a1),
1349 for (memb = TYPE_FIELDS (a1);
1350 memb; memb = TREE_CHAIN (memb))
1352 tree mv3 = TREE_TYPE (memb);
1353 if (mv3 && mv3 != error_mark_node
1354 && TREE_CODE (mv3) != ARRAY_TYPE)
1355 mv3 = TYPE_MAIN_VARIANT (mv3);
1356 if (comptypes_internal (mv3, mv2))
1362 else if (TREE_CODE (a2) == UNION_TYPE
1363 && (TYPE_NAME (a2) == 0
1364 || TYPE_TRANSPARENT_UNION (a2))
1365 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1366 && tree_int_cst_equal (TYPE_SIZE (a2),
1370 for (memb = TYPE_FIELDS (a2);
1371 memb; memb = TREE_CHAIN (memb))
1373 tree mv3 = TREE_TYPE (memb);
1374 if (mv3 && mv3 != error_mark_node
1375 && TREE_CODE (mv3) != ARRAY_TYPE)
1376 mv3 = TYPE_MAIN_VARIANT (mv3);
1377 if (comptypes_internal (mv3, mv1))
1387 /* comptypes said ok, but record if it said to warn. */
1391 args1 = TREE_CHAIN (args1);
1392 args2 = TREE_CHAIN (args2);
1396 /* Compute the size to increment a pointer by. */
1399 c_size_in_bytes (tree type)
1401 enum tree_code code = TREE_CODE (type);
1403 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1404 return size_one_node;
1406 if (!COMPLETE_OR_VOID_TYPE_P (type))
1408 error ("arithmetic on pointer to an incomplete type");
1409 return size_one_node;
1412 /* Convert in case a char is more than one unit. */
1413 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1414 size_int (TYPE_PRECISION (char_type_node)
1418 /* Return either DECL or its known constant value (if it has one). */
1421 decl_constant_value (tree decl)
1423 if (/* Don't change a variable array bound or initial value to a constant
1424 in a place where a variable is invalid. Note that DECL_INITIAL
1425 isn't valid for a PARM_DECL. */
1426 current_function_decl != 0
1427 && TREE_CODE (decl) != PARM_DECL
1428 && !TREE_THIS_VOLATILE (decl)
1429 && TREE_READONLY (decl)
1430 && DECL_INITIAL (decl) != 0
1431 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1432 /* This is invalid if initial value is not constant.
1433 If it has either a function call, a memory reference,
1434 or a variable, then re-evaluating it could give different results. */
1435 && TREE_CONSTANT (DECL_INITIAL (decl))
1436 /* Check for cases where this is sub-optimal, even though valid. */
1437 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1438 return DECL_INITIAL (decl);
1442 /* Return either DECL or its known constant value (if it has one), but
1443 return DECL if pedantic or DECL has mode BLKmode. This is for
1444 bug-compatibility with the old behavior of decl_constant_value
1445 (before GCC 3.0); every use of this function is a bug and it should
1446 be removed before GCC 3.1. It is not appropriate to use pedantic
1447 in a way that affects optimization, and BLKmode is probably not the
1448 right test for avoiding misoptimizations either. */
1451 decl_constant_value_for_broken_optimization (tree decl)
1455 if (pedantic || DECL_MODE (decl) == BLKmode)
1458 ret = decl_constant_value (decl);
1459 /* Avoid unwanted tree sharing between the initializer and current
1460 function's body where the tree can be modified e.g. by the
1462 if (ret != decl && TREE_STATIC (decl))
1463 ret = unshare_expr (ret);
1467 /* Convert the array expression EXP to a pointer. */
1469 array_to_pointer_conversion (tree exp)
1471 tree orig_exp = exp;
1472 tree type = TREE_TYPE (exp);
1474 tree restype = TREE_TYPE (type);
1477 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1479 STRIP_TYPE_NOPS (exp);
1481 if (TREE_NO_WARNING (orig_exp))
1482 TREE_NO_WARNING (exp) = 1;
1484 ptrtype = build_pointer_type (restype);
1486 if (TREE_CODE (exp) == INDIRECT_REF)
1487 return convert (ptrtype, TREE_OPERAND (exp, 0));
1489 if (TREE_CODE (exp) == VAR_DECL)
1491 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1492 ADDR_EXPR because it's the best way of representing what
1493 happens in C when we take the address of an array and place
1494 it in a pointer to the element type. */
1495 adr = build1 (ADDR_EXPR, ptrtype, exp);
1496 if (!c_mark_addressable (exp))
1497 return error_mark_node;
1498 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1502 /* This way is better for a COMPONENT_REF since it can
1503 simplify the offset for a component. */
1504 adr = build_unary_op (ADDR_EXPR, exp, 1);
1505 return convert (ptrtype, adr);
1508 /* Convert the function expression EXP to a pointer. */
1510 function_to_pointer_conversion (tree exp)
1512 tree orig_exp = exp;
1514 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1516 STRIP_TYPE_NOPS (exp);
1518 if (TREE_NO_WARNING (orig_exp))
1519 TREE_NO_WARNING (exp) = 1;
1521 return build_unary_op (ADDR_EXPR, exp, 0);
1524 /* Perform the default conversion of arrays and functions to pointers.
1525 Return the result of converting EXP. For any other expression, just
1526 return EXP after removing NOPs. */
1529 default_function_array_conversion (struct c_expr exp)
1531 tree orig_exp = exp.value;
1532 tree type = TREE_TYPE (exp.value);
1533 enum tree_code code = TREE_CODE (type);
1539 bool not_lvalue = false;
1540 bool lvalue_array_p;
1542 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1543 || TREE_CODE (exp.value) == NOP_EXPR
1544 || TREE_CODE (exp.value) == CONVERT_EXPR)
1545 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1547 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1549 exp.value = TREE_OPERAND (exp.value, 0);
1552 if (TREE_NO_WARNING (orig_exp))
1553 TREE_NO_WARNING (exp.value) = 1;
1555 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1556 if (!flag_isoc99 && !lvalue_array_p)
1558 /* Before C99, non-lvalue arrays do not decay to pointers.
1559 Normally, using such an array would be invalid; but it can
1560 be used correctly inside sizeof or as a statement expression.
1561 Thus, do not give an error here; an error will result later. */
1565 exp.value = array_to_pointer_conversion (exp.value);
1569 exp.value = function_to_pointer_conversion (exp.value);
1572 STRIP_TYPE_NOPS (exp.value);
1573 if (TREE_NO_WARNING (orig_exp))
1574 TREE_NO_WARNING (exp.value) = 1;
1582 /* EXP is an expression of integer type. Apply the integer promotions
1583 to it and return the promoted value. */
1586 perform_integral_promotions (tree exp)
1588 tree type = TREE_TYPE (exp);
1589 enum tree_code code = TREE_CODE (type);
1591 gcc_assert (INTEGRAL_TYPE_P (type));
1593 /* Normally convert enums to int,
1594 but convert wide enums to something wider. */
1595 if (code == ENUMERAL_TYPE)
1597 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1598 TYPE_PRECISION (integer_type_node)),
1599 ((TYPE_PRECISION (type)
1600 >= TYPE_PRECISION (integer_type_node))
1601 && TYPE_UNSIGNED (type)));
1603 return convert (type, exp);
1606 /* ??? This should no longer be needed now bit-fields have their
1608 if (TREE_CODE (exp) == COMPONENT_REF
1609 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1610 /* If it's thinner than an int, promote it like a
1611 c_promoting_integer_type_p, otherwise leave it alone. */
1612 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1613 TYPE_PRECISION (integer_type_node)))
1614 return convert (integer_type_node, exp);
1616 if (c_promoting_integer_type_p (type))
1618 /* Preserve unsignedness if not really getting any wider. */
1619 if (TYPE_UNSIGNED (type)
1620 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1621 return convert (unsigned_type_node, exp);
1623 return convert (integer_type_node, exp);
1630 /* Perform default promotions for C data used in expressions.
1631 Enumeral types or short or char are converted to int.
1632 In addition, manifest constants symbols are replaced by their values. */
1635 default_conversion (tree exp)
1638 tree type = TREE_TYPE (exp);
1639 enum tree_code code = TREE_CODE (type);
1641 /* Functions and arrays have been converted during parsing. */
1642 gcc_assert (code != FUNCTION_TYPE);
1643 if (code == ARRAY_TYPE)
1646 /* Constants can be used directly unless they're not loadable. */
1647 if (TREE_CODE (exp) == CONST_DECL)
1648 exp = DECL_INITIAL (exp);
1650 /* Replace a nonvolatile const static variable with its value unless
1651 it is an array, in which case we must be sure that taking the
1652 address of the array produces consistent results. */
1653 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1655 exp = decl_constant_value_for_broken_optimization (exp);
1656 type = TREE_TYPE (exp);
1659 /* Strip no-op conversions. */
1661 STRIP_TYPE_NOPS (exp);
1663 if (TREE_NO_WARNING (orig_exp))
1664 TREE_NO_WARNING (exp) = 1;
1666 if (INTEGRAL_TYPE_P (type))
1667 return perform_integral_promotions (exp);
1669 if (code == VOID_TYPE)
1671 error ("void value not ignored as it ought to be");
1672 return error_mark_node;
1677 /* Look up COMPONENT in a structure or union DECL.
1679 If the component name is not found, returns NULL_TREE. Otherwise,
1680 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1681 stepping down the chain to the component, which is in the last
1682 TREE_VALUE of the list. Normally the list is of length one, but if
1683 the component is embedded within (nested) anonymous structures or
1684 unions, the list steps down the chain to the component. */
1687 lookup_field (tree decl, tree component)
1689 tree type = TREE_TYPE (decl);
1692 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1693 to the field elements. Use a binary search on this array to quickly
1694 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1695 will always be set for structures which have many elements. */
1697 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1700 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1702 field = TYPE_FIELDS (type);
1704 top = TYPE_LANG_SPECIFIC (type)->s->len;
1705 while (top - bot > 1)
1707 half = (top - bot + 1) >> 1;
1708 field = field_array[bot+half];
1710 if (DECL_NAME (field) == NULL_TREE)
1712 /* Step through all anon unions in linear fashion. */
1713 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1715 field = field_array[bot++];
1716 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1717 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1719 tree anon = lookup_field (field, component);
1722 return tree_cons (NULL_TREE, field, anon);
1726 /* Entire record is only anon unions. */
1730 /* Restart the binary search, with new lower bound. */
1734 if (DECL_NAME (field) == component)
1736 if (DECL_NAME (field) < component)
1742 if (DECL_NAME (field_array[bot]) == component)
1743 field = field_array[bot];
1744 else if (DECL_NAME (field) != component)
1749 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1751 if (DECL_NAME (field) == NULL_TREE
1752 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1753 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1755 tree anon = lookup_field (field, component);
1758 return tree_cons (NULL_TREE, field, anon);
1761 if (DECL_NAME (field) == component)
1765 if (field == NULL_TREE)
1769 return tree_cons (NULL_TREE, field, NULL_TREE);
1772 /* Make an expression to refer to the COMPONENT field of
1773 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1776 build_component_ref (tree datum, tree component)
1778 tree type = TREE_TYPE (datum);
1779 enum tree_code code = TREE_CODE (type);
1783 if (!objc_is_public (datum, component))
1784 return error_mark_node;
1786 /* See if there is a field or component with name COMPONENT. */
1788 if (code == RECORD_TYPE || code == UNION_TYPE)
1790 if (!COMPLETE_TYPE_P (type))
1792 c_incomplete_type_error (NULL_TREE, type);
1793 return error_mark_node;
1796 field = lookup_field (datum, component);
1800 error ("%qT has no member named %qE", type, component);
1801 return error_mark_node;
1804 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1805 This might be better solved in future the way the C++ front
1806 end does it - by giving the anonymous entities each a
1807 separate name and type, and then have build_component_ref
1808 recursively call itself. We can't do that here. */
1811 tree subdatum = TREE_VALUE (field);
1813 if (TREE_TYPE (subdatum) == error_mark_node)
1814 return error_mark_node;
1816 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1818 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1819 TREE_READONLY (ref) = 1;
1820 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1821 TREE_THIS_VOLATILE (ref) = 1;
1823 if (TREE_DEPRECATED (subdatum))
1824 warn_deprecated_use (subdatum);
1828 field = TREE_CHAIN (field);
1834 else if (code != ERROR_MARK)
1835 error ("request for member %qE in something not a structure or union",
1838 return error_mark_node;
1841 /* Given an expression PTR for a pointer, return an expression
1842 for the value pointed to.
1843 ERRORSTRING is the name of the operator to appear in error messages. */
1846 build_indirect_ref (tree ptr, const char *errorstring)
1848 tree pointer = default_conversion (ptr);
1849 tree type = TREE_TYPE (pointer);
1851 if (TREE_CODE (type) == POINTER_TYPE)
1853 if (TREE_CODE (pointer) == ADDR_EXPR
1854 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1855 == TREE_TYPE (type)))
1856 return TREE_OPERAND (pointer, 0);
1859 tree t = TREE_TYPE (type);
1862 ref = build1 (INDIRECT_REF, t, pointer);
1864 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1866 error ("dereferencing pointer to incomplete type");
1867 return error_mark_node;
1869 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1870 warning (0, "dereferencing %<void *%> pointer");
1872 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1873 so that we get the proper error message if the result is used
1874 to assign to. Also, &* is supposed to be a no-op.
1875 And ANSI C seems to specify that the type of the result
1876 should be the const type. */
1877 /* A de-reference of a pointer to const is not a const. It is valid
1878 to change it via some other pointer. */
1879 TREE_READONLY (ref) = TYPE_READONLY (t);
1880 TREE_SIDE_EFFECTS (ref)
1881 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1882 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1886 else if (TREE_CODE (pointer) != ERROR_MARK)
1887 error ("invalid type argument of %qs", errorstring);
1888 return error_mark_node;
1891 /* This handles expressions of the form "a[i]", which denotes
1894 This is logically equivalent in C to *(a+i), but we may do it differently.
1895 If A is a variable or a member, we generate a primitive ARRAY_REF.
1896 This avoids forcing the array out of registers, and can work on
1897 arrays that are not lvalues (for example, members of structures returned
1901 build_array_ref (tree array, tree index)
1903 bool swapped = false;
1904 if (TREE_TYPE (array) == error_mark_node
1905 || TREE_TYPE (index) == error_mark_node)
1906 return error_mark_node;
1908 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1909 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1912 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1913 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1915 error ("subscripted value is neither array nor pointer");
1916 return error_mark_node;
1924 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1926 error ("array subscript is not an integer");
1927 return error_mark_node;
1930 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1932 error ("subscripted value is pointer to function");
1933 return error_mark_node;
1936 /* ??? Existing practice has been to warn only when the char
1937 index is syntactically the index, not for char[array]. */
1939 warn_array_subscript_with_type_char (index);
1941 /* Apply default promotions *after* noticing character types. */
1942 index = default_conversion (index);
1944 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1946 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1950 /* An array that is indexed by a non-constant
1951 cannot be stored in a register; we must be able to do
1952 address arithmetic on its address.
1953 Likewise an array of elements of variable size. */
1954 if (TREE_CODE (index) != INTEGER_CST
1955 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1956 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1958 if (!c_mark_addressable (array))
1959 return error_mark_node;
1961 /* An array that is indexed by a constant value which is not within
1962 the array bounds cannot be stored in a register either; because we
1963 would get a crash in store_bit_field/extract_bit_field when trying
1964 to access a non-existent part of the register. */
1965 if (TREE_CODE (index) == INTEGER_CST
1966 && TYPE_DOMAIN (TREE_TYPE (array))
1967 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1969 if (!c_mark_addressable (array))
1970 return error_mark_node;
1976 while (TREE_CODE (foo) == COMPONENT_REF)
1977 foo = TREE_OPERAND (foo, 0);
1978 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1979 pedwarn ("ISO C forbids subscripting %<register%> array");
1980 else if (!flag_isoc99 && !lvalue_p (foo))
1981 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1984 type = TREE_TYPE (TREE_TYPE (array));
1985 if (TREE_CODE (type) != ARRAY_TYPE)
1986 type = TYPE_MAIN_VARIANT (type);
1987 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1988 /* Array ref is const/volatile if the array elements are
1989 or if the array is. */
1990 TREE_READONLY (rval)
1991 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1992 | TREE_READONLY (array));
1993 TREE_SIDE_EFFECTS (rval)
1994 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1995 | TREE_SIDE_EFFECTS (array));
1996 TREE_THIS_VOLATILE (rval)
1997 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1998 /* This was added by rms on 16 Nov 91.
1999 It fixes vol struct foo *a; a->elts[1]
2000 in an inline function.
2001 Hope it doesn't break something else. */
2002 | TREE_THIS_VOLATILE (array));
2003 return require_complete_type (fold (rval));
2007 tree ar = default_conversion (array);
2009 if (ar == error_mark_node)
2012 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2013 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2015 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
2020 /* Build an external reference to identifier ID. FUN indicates
2021 whether this will be used for a function call. LOC is the source
2022 location of the identifier. */
2024 build_external_ref (tree id, int fun, location_t loc)
2027 tree decl = lookup_name (id);
2029 /* In Objective-C, an instance variable (ivar) may be preferred to
2030 whatever lookup_name() found. */
2031 decl = objc_lookup_ivar (decl, id);
2033 if (decl && decl != error_mark_node)
2036 /* Implicit function declaration. */
2037 ref = implicitly_declare (id);
2038 else if (decl == error_mark_node)
2039 /* Don't complain about something that's already been
2040 complained about. */
2041 return error_mark_node;
2044 undeclared_variable (id, loc);
2045 return error_mark_node;
2048 if (TREE_TYPE (ref) == error_mark_node)
2049 return error_mark_node;
2051 if (TREE_DEPRECATED (ref))
2052 warn_deprecated_use (ref);
2054 if (!skip_evaluation)
2055 assemble_external (ref);
2056 TREE_USED (ref) = 1;
2058 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2060 if (!in_sizeof && !in_typeof)
2061 C_DECL_USED (ref) = 1;
2062 else if (DECL_INITIAL (ref) == 0
2063 && DECL_EXTERNAL (ref)
2064 && !TREE_PUBLIC (ref))
2065 record_maybe_used_decl (ref);
2068 if (TREE_CODE (ref) == CONST_DECL)
2070 ref = DECL_INITIAL (ref);
2071 TREE_CONSTANT (ref) = 1;
2072 TREE_INVARIANT (ref) = 1;
2074 else if (current_function_decl != 0
2075 && !DECL_FILE_SCOPE_P (current_function_decl)
2076 && (TREE_CODE (ref) == VAR_DECL
2077 || TREE_CODE (ref) == PARM_DECL
2078 || TREE_CODE (ref) == FUNCTION_DECL))
2080 tree context = decl_function_context (ref);
2082 if (context != 0 && context != current_function_decl)
2083 DECL_NONLOCAL (ref) = 1;
2089 /* Record details of decls possibly used inside sizeof or typeof. */
2090 struct maybe_used_decl
2094 /* The level seen at (in_sizeof + in_typeof). */
2096 /* The next one at this level or above, or NULL. */
2097 struct maybe_used_decl *next;
2100 static struct maybe_used_decl *maybe_used_decls;
2102 /* Record that DECL, an undefined static function reference seen
2103 inside sizeof or typeof, might be used if the operand of sizeof is
2104 a VLA type or the operand of typeof is a variably modified
2108 record_maybe_used_decl (tree decl)
2110 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2112 t->level = in_sizeof + in_typeof;
2113 t->next = maybe_used_decls;
2114 maybe_used_decls = t;
2117 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2118 USED is false, just discard them. If it is true, mark them used
2119 (if no longer inside sizeof or typeof) or move them to the next
2120 level up (if still inside sizeof or typeof). */
2123 pop_maybe_used (bool used)
2125 struct maybe_used_decl *p = maybe_used_decls;
2126 int cur_level = in_sizeof + in_typeof;
2127 while (p && p->level > cur_level)
2132 C_DECL_USED (p->decl) = 1;
2134 p->level = cur_level;
2138 if (!used || cur_level == 0)
2139 maybe_used_decls = p;
2142 /* Return the result of sizeof applied to EXPR. */
2145 c_expr_sizeof_expr (struct c_expr expr)
2148 if (expr.value == error_mark_node)
2150 ret.value = error_mark_node;
2151 ret.original_code = ERROR_MARK;
2152 pop_maybe_used (false);
2156 ret.value = c_sizeof (TREE_TYPE (expr.value));
2157 ret.original_code = ERROR_MARK;
2158 if (c_vla_type_p (TREE_TYPE (expr.value)))
2160 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2161 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2163 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2168 /* Return the result of sizeof applied to T, a structure for the type
2169 name passed to sizeof (rather than the type itself). */
2172 c_expr_sizeof_type (struct c_type_name *t)
2176 type = groktypename (t);
2177 ret.value = c_sizeof (type);
2178 ret.original_code = ERROR_MARK;
2179 pop_maybe_used (type != error_mark_node
2180 ? C_TYPE_VARIABLE_SIZE (type) : false);
2184 /* Build a function call to function FUNCTION with parameters PARAMS.
2185 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2186 TREE_VALUE of each node is a parameter-expression.
2187 FUNCTION's data type may be a function type or a pointer-to-function. */
2190 build_function_call (tree function, tree params)
2192 tree fntype, fundecl = 0;
2193 tree coerced_params;
2194 tree name = NULL_TREE, result;
2197 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2198 STRIP_TYPE_NOPS (function);
2200 /* Convert anything with function type to a pointer-to-function. */
2201 if (TREE_CODE (function) == FUNCTION_DECL)
2203 /* Implement type-directed function overloading for builtins.
2204 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2205 handle all the type checking. The result is a complete expression
2206 that implements this function call. */
2207 tem = resolve_overloaded_builtin (function, params);
2211 name = DECL_NAME (function);
2214 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2215 function = function_to_pointer_conversion (function);
2217 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2218 expressions, like those used for ObjC messenger dispatches. */
2219 function = objc_rewrite_function_call (function, params);
2221 fntype = TREE_TYPE (function);
2223 if (TREE_CODE (fntype) == ERROR_MARK)
2224 return error_mark_node;
2226 if (!(TREE_CODE (fntype) == POINTER_TYPE
2227 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2229 error ("called object %qE is not a function", function);
2230 return error_mark_node;
2233 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2234 current_function_returns_abnormally = 1;
2236 /* fntype now gets the type of function pointed to. */
2237 fntype = TREE_TYPE (fntype);
2239 /* Check that the function is called through a compatible prototype.
2240 If it is not, replace the call by a trap, wrapped up in a compound
2241 expression if necessary. This has the nice side-effect to prevent
2242 the tree-inliner from generating invalid assignment trees which may
2243 blow up in the RTL expander later. */
2244 if ((TREE_CODE (function) == NOP_EXPR
2245 || TREE_CODE (function) == CONVERT_EXPR)
2246 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2247 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2248 && !comptypes (fntype, TREE_TYPE (tem)))
2250 tree return_type = TREE_TYPE (fntype);
2251 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2254 /* This situation leads to run-time undefined behavior. We can't,
2255 therefore, simply error unless we can prove that all possible
2256 executions of the program must execute the code. */
2257 warning (0, "function called through a non-compatible type");
2259 /* We can, however, treat "undefined" any way we please.
2260 Call abort to encourage the user to fix the program. */
2261 inform ("if this code is reached, the program will abort");
2263 if (VOID_TYPE_P (return_type))
2269 if (AGGREGATE_TYPE_P (return_type))
2270 rhs = build_compound_literal (return_type,
2271 build_constructor (return_type, 0));
2273 rhs = fold_convert (return_type, integer_zero_node);
2275 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2279 /* Convert the parameters to the types declared in the
2280 function prototype, or apply default promotions. */
2283 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2285 if (coerced_params == error_mark_node)
2286 return error_mark_node;
2288 /* Check that the arguments to the function are valid. */
2290 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2291 TYPE_ARG_TYPES (fntype));
2293 if (require_constant_value)
2295 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2296 function, coerced_params, NULL_TREE);
2298 if (TREE_CONSTANT (result)
2299 && (name == NULL_TREE
2300 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2301 pedwarn_init ("initializer element is not constant");
2304 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2305 function, coerced_params, NULL_TREE);
2307 if (VOID_TYPE_P (TREE_TYPE (result)))
2309 return require_complete_type (result);
2312 /* Convert the argument expressions in the list VALUES
2313 to the types in the list TYPELIST. The result is a list of converted
2314 argument expressions, unless there are too few arguments in which
2315 case it is error_mark_node.
2317 If TYPELIST is exhausted, or when an element has NULL as its type,
2318 perform the default conversions.
2320 PARMLIST is the chain of parm decls for the function being called.
2321 It may be 0, if that info is not available.
2322 It is used only for generating error messages.
2324 FUNCTION is a tree for the called function. It is used only for
2325 error messages, where it is formatted with %qE.
2327 This is also where warnings about wrong number of args are generated.
2329 Both VALUES and the returned value are chains of TREE_LIST nodes
2330 with the elements of the list in the TREE_VALUE slots of those nodes. */
2333 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2335 tree typetail, valtail;
2340 /* Change pointer to function to the function itself for
2342 if (TREE_CODE (function) == ADDR_EXPR
2343 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2344 function = TREE_OPERAND (function, 0);
2346 /* Handle an ObjC selector specially for diagnostics. */
2347 selector = objc_message_selector ();
2349 /* Scan the given expressions and types, producing individual
2350 converted arguments and pushing them on RESULT in reverse order. */
2352 for (valtail = values, typetail = typelist, parmnum = 0;
2354 valtail = TREE_CHAIN (valtail), parmnum++)
2356 tree type = typetail ? TREE_VALUE (typetail) : 0;
2357 tree val = TREE_VALUE (valtail);
2358 tree rname = function;
2359 int argnum = parmnum + 1;
2360 const char *invalid_func_diag;
2362 if (type == void_type_node)
2364 error ("too many arguments to function %qE", function);
2368 if (selector && argnum > 2)
2374 STRIP_TYPE_NOPS (val);
2376 val = require_complete_type (val);
2380 /* Formal parm type is specified by a function prototype. */
2383 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2385 error ("type of formal parameter %d is incomplete", parmnum + 1);
2390 /* Optionally warn about conversions that
2391 differ from the default conversions. */
2392 if (warn_conversion || warn_traditional)
2394 unsigned int formal_prec = TYPE_PRECISION (type);
2396 if (INTEGRAL_TYPE_P (type)
2397 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2398 warning (0, "passing argument %d of %qE as integer "
2399 "rather than floating due to prototype",
2401 if (INTEGRAL_TYPE_P (type)
2402 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2403 warning (0, "passing argument %d of %qE as integer "
2404 "rather than complex due to prototype",
2406 else if (TREE_CODE (type) == COMPLEX_TYPE
2407 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2408 warning (0, "passing argument %d of %qE as complex "
2409 "rather than floating due to prototype",
2411 else if (TREE_CODE (type) == REAL_TYPE
2412 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2413 warning (0, "passing argument %d of %qE as floating "
2414 "rather than integer due to prototype",
2416 else if (TREE_CODE (type) == COMPLEX_TYPE
2417 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2418 warning (0, "passing argument %d of %qE as complex "
2419 "rather than integer due to prototype",
2421 else if (TREE_CODE (type) == REAL_TYPE
2422 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2423 warning (0, "passing argument %d of %qE as floating "
2424 "rather than complex due to prototype",
2426 /* ??? At some point, messages should be written about
2427 conversions between complex types, but that's too messy
2429 else if (TREE_CODE (type) == REAL_TYPE
2430 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2432 /* Warn if any argument is passed as `float',
2433 since without a prototype it would be `double'. */
2434 if (formal_prec == TYPE_PRECISION (float_type_node)
2435 && type != dfloat32_type_node)
2436 warning (0, "passing argument %d of %qE as %<float%> "
2437 "rather than %<double%> due to prototype",
2440 /* Warn if mismatch between argument and prototype
2441 for decimal float types. Warn of conversions with
2442 binary float types and of precision narrowing due to
2444 else if (type != TREE_TYPE (val)
2445 && (type == dfloat32_type_node
2446 || type == dfloat64_type_node
2447 || type == dfloat128_type_node
2448 || TREE_TYPE (val) == dfloat32_type_node
2449 || TREE_TYPE (val) == dfloat64_type_node
2450 || TREE_TYPE (val) == dfloat128_type_node)
2452 <= TYPE_PRECISION (TREE_TYPE (val))
2453 || (type == dfloat128_type_node
2455 != dfloat64_type_node
2457 != dfloat32_type_node)))
2458 || (type == dfloat64_type_node
2460 != dfloat32_type_node))))
2461 warning (0, "passing argument %d of %qE as %qT "
2462 "rather than %qT due to prototype",
2463 argnum, rname, type, TREE_TYPE (val));
2466 /* Detect integer changing in width or signedness.
2467 These warnings are only activated with
2468 -Wconversion, not with -Wtraditional. */
2469 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2470 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2472 tree would_have_been = default_conversion (val);
2473 tree type1 = TREE_TYPE (would_have_been);
2475 if (TREE_CODE (type) == ENUMERAL_TYPE
2476 && (TYPE_MAIN_VARIANT (type)
2477 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2478 /* No warning if function asks for enum
2479 and the actual arg is that enum type. */
2481 else if (formal_prec != TYPE_PRECISION (type1))
2482 warning (OPT_Wconversion, "passing argument %d of %qE "
2483 "with different width due to prototype",
2485 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2487 /* Don't complain if the formal parameter type
2488 is an enum, because we can't tell now whether
2489 the value was an enum--even the same enum. */
2490 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2492 else if (TREE_CODE (val) == INTEGER_CST
2493 && int_fits_type_p (val, type))
2494 /* Change in signedness doesn't matter
2495 if a constant value is unaffected. */
2497 /* If the value is extended from a narrower
2498 unsigned type, it doesn't matter whether we
2499 pass it as signed or unsigned; the value
2500 certainly is the same either way. */
2501 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2502 && TYPE_UNSIGNED (TREE_TYPE (val)))
2504 else if (TYPE_UNSIGNED (type))
2505 warning (OPT_Wconversion, "passing argument %d of %qE "
2506 "as unsigned due to prototype",
2509 warning (OPT_Wconversion, "passing argument %d of %qE "
2510 "as signed due to prototype", argnum, rname);
2514 parmval = convert_for_assignment (type, val, ic_argpass,
2518 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2519 && INTEGRAL_TYPE_P (type)
2520 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2521 parmval = default_conversion (parmval);
2523 result = tree_cons (NULL_TREE, parmval, result);
2525 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2526 && (TYPE_PRECISION (TREE_TYPE (val))
2527 < TYPE_PRECISION (double_type_node))
2528 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2529 /* Convert `float' to `double'. */
2530 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2531 else if ((invalid_func_diag =
2532 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2534 error (invalid_func_diag);
2535 return error_mark_node;
2538 /* Convert `short' and `char' to full-size `int'. */
2539 result = tree_cons (NULL_TREE, default_conversion (val), result);
2542 typetail = TREE_CHAIN (typetail);
2545 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2547 error ("too few arguments to function %qE", function);
2548 return error_mark_node;
2551 return nreverse (result);
2554 /* This is the entry point used by the parser to build unary operators
2555 in the input. CODE, a tree_code, specifies the unary operator, and
2556 ARG is the operand. For unary plus, the C parser currently uses
2557 CONVERT_EXPR for code. */
2560 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2562 struct c_expr result;
2564 result.original_code = ERROR_MARK;
2565 result.value = build_unary_op (code, arg.value, 0);
2566 overflow_warning (result.value);
2570 /* This is the entry point used by the parser to build binary operators
2571 in the input. CODE, a tree_code, specifies the binary operator, and
2572 ARG1 and ARG2 are the operands. In addition to constructing the
2573 expression, we check for operands that were written with other binary
2574 operators in a way that is likely to confuse the user. */
2577 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2580 struct c_expr result;
2582 enum tree_code code1 = arg1.original_code;
2583 enum tree_code code2 = arg2.original_code;
2585 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2586 result.original_code = code;
2588 if (TREE_CODE (result.value) == ERROR_MARK)
2591 /* Check for cases such as x+y<<z which users are likely
2593 if (warn_parentheses)
2595 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2597 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2598 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2599 warning (OPT_Wparentheses,
2600 "suggest parentheses around + or - inside shift");
2603 if (code == TRUTH_ORIF_EXPR)
2605 if (code1 == TRUTH_ANDIF_EXPR
2606 || code2 == TRUTH_ANDIF_EXPR)
2607 warning (OPT_Wparentheses,
2608 "suggest parentheses around && within ||");
2611 if (code == BIT_IOR_EXPR)
2613 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2614 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2615 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2616 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2617 warning (OPT_Wparentheses,
2618 "suggest parentheses around arithmetic in operand of |");
2619 /* Check cases like x|y==z */
2620 if (TREE_CODE_CLASS (code1) == tcc_comparison
2621 || TREE_CODE_CLASS (code2) == tcc_comparison)
2622 warning (OPT_Wparentheses,
2623 "suggest parentheses around comparison in operand of |");
2626 if (code == BIT_XOR_EXPR)
2628 if (code1 == BIT_AND_EXPR
2629 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2630 || code2 == BIT_AND_EXPR
2631 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2632 warning (OPT_Wparentheses,
2633 "suggest parentheses around arithmetic in operand of ^");
2634 /* Check cases like x^y==z */
2635 if (TREE_CODE_CLASS (code1) == tcc_comparison
2636 || TREE_CODE_CLASS (code2) == tcc_comparison)
2637 warning (OPT_Wparentheses,
2638 "suggest parentheses around comparison in operand of ^");
2641 if (code == BIT_AND_EXPR)
2643 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2644 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2645 warning (OPT_Wparentheses,
2646 "suggest parentheses around + or - in operand of &");
2647 /* Check cases like x&y==z */
2648 if (TREE_CODE_CLASS (code1) == tcc_comparison
2649 || TREE_CODE_CLASS (code2) == tcc_comparison)
2650 warning (OPT_Wparentheses,
2651 "suggest parentheses around comparison in operand of &");
2653 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2654 if (TREE_CODE_CLASS (code) == tcc_comparison
2655 && (TREE_CODE_CLASS (code1) == tcc_comparison
2656 || TREE_CODE_CLASS (code2) == tcc_comparison))
2657 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2658 "have their mathematical meaning");
2662 /* Warn about comparisons against string literals, with the exception
2663 of testing for equality or inequality of a string literal with NULL. */
2664 if (code == EQ_EXPR || code == NE_EXPR)
2666 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2667 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2668 warning (OPT_Wstring_literal_comparison,
2669 "comparison with string literal");
2671 else if (TREE_CODE_CLASS (code) == tcc_comparison
2672 && (code1 == STRING_CST || code2 == STRING_CST))
2673 warning (OPT_Wstring_literal_comparison,
2674 "comparison with string literal");
2676 overflow_warning (result.value);
2681 /* Return a tree for the difference of pointers OP0 and OP1.
2682 The resulting tree has type int. */
2685 pointer_diff (tree op0, tree op1)
2687 tree restype = ptrdiff_type_node;
2689 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2690 tree con0, con1, lit0, lit1;
2691 tree orig_op1 = op1;
2693 if (pedantic || warn_pointer_arith)
2695 if (TREE_CODE (target_type) == VOID_TYPE)
2696 pedwarn ("pointer of type %<void *%> used in subtraction");
2697 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2698 pedwarn ("pointer to a function used in subtraction");
2701 /* If the conversion to ptrdiff_type does anything like widening or
2702 converting a partial to an integral mode, we get a convert_expression
2703 that is in the way to do any simplifications.
2704 (fold-const.c doesn't know that the extra bits won't be needed.
2705 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2706 different mode in place.)
2707 So first try to find a common term here 'by hand'; we want to cover
2708 at least the cases that occur in legal static initializers. */
2709 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2710 && (TYPE_PRECISION (TREE_TYPE (op0))
2711 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2712 con0 = TREE_OPERAND (op0, 0);
2715 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2716 && (TYPE_PRECISION (TREE_TYPE (op1))
2717 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2718 con1 = TREE_OPERAND (op1, 0);
2722 if (TREE_CODE (con0) == PLUS_EXPR)
2724 lit0 = TREE_OPERAND (con0, 1);
2725 con0 = TREE_OPERAND (con0, 0);
2728 lit0 = integer_zero_node;
2730 if (TREE_CODE (con1) == PLUS_EXPR)
2732 lit1 = TREE_OPERAND (con1, 1);
2733 con1 = TREE_OPERAND (con1, 0);
2736 lit1 = integer_zero_node;
2738 if (operand_equal_p (con0, con1, 0))
2745 /* First do the subtraction as integers;
2746 then drop through to build the divide operator.
2747 Do not do default conversions on the minus operator
2748 in case restype is a short type. */
2750 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2751 convert (restype, op1), 0);
2752 /* This generates an error if op1 is pointer to incomplete type. */
2753 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2754 error ("arithmetic on pointer to an incomplete type");
2756 /* This generates an error if op0 is pointer to incomplete type. */
2757 op1 = c_size_in_bytes (target_type);
2759 /* Divide by the size, in easiest possible way. */
2760 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2763 /* Construct and perhaps optimize a tree representation
2764 for a unary operation. CODE, a tree_code, specifies the operation
2765 and XARG is the operand.
2766 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2767 the default promotions (such as from short to int).
2768 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2769 allows non-lvalues; this is only used to handle conversion of non-lvalue
2770 arrays to pointers in C99. */
2773 build_unary_op (enum tree_code code, tree xarg, int flag)
2775 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2778 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2780 int noconvert = flag;
2781 const char *invalid_op_diag;
2783 if (typecode == ERROR_MARK)
2784 return error_mark_node;
2785 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2786 typecode = INTEGER_TYPE;
2788 if ((invalid_op_diag
2789 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2791 error (invalid_op_diag);
2792 return error_mark_node;
2798 /* This is used for unary plus, because a CONVERT_EXPR
2799 is enough to prevent anybody from looking inside for
2800 associativity, but won't generate any code. */
2801 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2802 || typecode == COMPLEX_TYPE
2803 || typecode == VECTOR_TYPE))
2805 error ("wrong type argument to unary plus");
2806 return error_mark_node;
2808 else if (!noconvert)
2809 arg = default_conversion (arg);
2810 arg = non_lvalue (arg);
2814 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2815 || typecode == COMPLEX_TYPE
2816 || typecode == VECTOR_TYPE))
2818 error ("wrong type argument to unary minus");
2819 return error_mark_node;
2821 else if (!noconvert)
2822 arg = default_conversion (arg);
2826 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2829 arg = default_conversion (arg);
2831 else if (typecode == COMPLEX_TYPE)
2835 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2837 arg = default_conversion (arg);
2841 error ("wrong type argument to bit-complement");
2842 return error_mark_node;
2847 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2849 error ("wrong type argument to abs");
2850 return error_mark_node;
2852 else if (!noconvert)
2853 arg = default_conversion (arg);
2857 /* Conjugating a real value is a no-op, but allow it anyway. */
2858 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2859 || typecode == COMPLEX_TYPE))
2861 error ("wrong type argument to conjugation");
2862 return error_mark_node;
2864 else if (!noconvert)
2865 arg = default_conversion (arg);
2868 case TRUTH_NOT_EXPR:
2869 if (typecode != INTEGER_TYPE
2870 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2871 && typecode != COMPLEX_TYPE)
2873 error ("wrong type argument to unary exclamation mark");
2874 return error_mark_node;
2876 arg = c_objc_common_truthvalue_conversion (arg);
2877 return invert_truthvalue (arg);
2880 if (TREE_CODE (arg) == COMPLEX_CST)
2881 return TREE_REALPART (arg);
2882 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2883 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2888 if (TREE_CODE (arg) == COMPLEX_CST)
2889 return TREE_IMAGPART (arg);
2890 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2891 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2893 return convert (TREE_TYPE (arg), integer_zero_node);
2895 case PREINCREMENT_EXPR:
2896 case POSTINCREMENT_EXPR:
2897 case PREDECREMENT_EXPR:
2898 case POSTDECREMENT_EXPR:
2900 /* Increment or decrement the real part of the value,
2901 and don't change the imaginary part. */
2902 if (typecode == COMPLEX_TYPE)
2907 pedwarn ("ISO C does not support %<++%> and %<--%>"
2908 " on complex types");
2910 arg = stabilize_reference (arg);
2911 real = build_unary_op (REALPART_EXPR, arg, 1);
2912 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2913 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2914 build_unary_op (code, real, 1), imag);
2917 /* Report invalid types. */
2919 if (typecode != POINTER_TYPE
2920 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2922 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2923 error ("wrong type argument to increment");
2925 error ("wrong type argument to decrement");
2927 return error_mark_node;
2932 tree result_type = TREE_TYPE (arg);
2934 arg = get_unwidened (arg, 0);
2935 argtype = TREE_TYPE (arg);
2937 /* Compute the increment. */
2939 if (typecode == POINTER_TYPE)
2941 /* If pointer target is an undefined struct,
2942 we just cannot know how to do the arithmetic. */
2943 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2945 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2946 error ("increment of pointer to unknown structure");
2948 error ("decrement of pointer to unknown structure");
2950 else if ((pedantic || warn_pointer_arith)
2951 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2952 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2954 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2955 pedwarn ("wrong type argument to increment");
2957 pedwarn ("wrong type argument to decrement");
2960 inc = c_size_in_bytes (TREE_TYPE (result_type));
2963 inc = integer_one_node;
2965 inc = convert (argtype, inc);
2967 /* Complain about anything else that is not a true lvalue. */
2968 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2969 || code == POSTINCREMENT_EXPR)
2972 return error_mark_node;
2974 /* Report a read-only lvalue. */
2975 if (TREE_READONLY (arg))
2977 readonly_error (arg,
2978 ((code == PREINCREMENT_EXPR
2979 || code == POSTINCREMENT_EXPR)
2980 ? lv_increment : lv_decrement));
2981 return error_mark_node;
2984 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2985 val = boolean_increment (code, arg);
2987 val = build2 (code, TREE_TYPE (arg), arg, inc);
2988 TREE_SIDE_EFFECTS (val) = 1;
2989 val = convert (result_type, val);
2990 if (TREE_CODE (val) != code)
2991 TREE_NO_WARNING (val) = 1;
2996 /* Note that this operation never does default_conversion. */
2998 /* Let &* cancel out to simplify resulting code. */
2999 if (TREE_CODE (arg) == INDIRECT_REF)
3001 /* Don't let this be an lvalue. */
3002 if (lvalue_p (TREE_OPERAND (arg, 0)))
3003 return non_lvalue (TREE_OPERAND (arg, 0));
3004 return TREE_OPERAND (arg, 0);
3007 /* For &x[y], return x+y */
3008 if (TREE_CODE (arg) == ARRAY_REF)
3010 tree op0 = TREE_OPERAND (arg, 0);
3011 if (!c_mark_addressable (op0))
3012 return error_mark_node;
3013 return build_binary_op (PLUS_EXPR,
3014 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3015 ? array_to_pointer_conversion (op0)
3017 TREE_OPERAND (arg, 1), 1);
3020 /* Anything not already handled and not a true memory reference
3021 or a non-lvalue array is an error. */
3022 else if (typecode != FUNCTION_TYPE && !flag
3023 && !lvalue_or_else (arg, lv_addressof))
3024 return error_mark_node;
3026 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3027 argtype = TREE_TYPE (arg);
3029 /* If the lvalue is const or volatile, merge that into the type
3030 to which the address will point. Note that you can't get a
3031 restricted pointer by taking the address of something, so we
3032 only have to deal with `const' and `volatile' here. */
3033 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3034 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3035 argtype = c_build_type_variant (argtype,
3036 TREE_READONLY (arg),
3037 TREE_THIS_VOLATILE (arg));
3039 if (!c_mark_addressable (arg))
3040 return error_mark_node;
3042 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3043 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3045 argtype = build_pointer_type (argtype);
3047 /* ??? Cope with user tricks that amount to offsetof. Delete this
3048 when we have proper support for integer constant expressions. */
3049 val = get_base_address (arg);
3050 if (val && TREE_CODE (val) == INDIRECT_REF
3051 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3053 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
3055 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3056 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3059 val = build1 (ADDR_EXPR, argtype, arg);
3068 argtype = TREE_TYPE (arg);
3069 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3070 : fold_build1 (code, argtype, arg);
3073 /* Return nonzero if REF is an lvalue valid for this language.
3074 Lvalues can be assigned, unless their type has TYPE_READONLY.
3075 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3080 enum tree_code code = TREE_CODE (ref);
3087 return lvalue_p (TREE_OPERAND (ref, 0));
3089 case COMPOUND_LITERAL_EXPR:
3099 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3100 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3103 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3110 /* Give an error for storing in something that is 'const'. */
3113 readonly_error (tree arg, enum lvalue_use use)
3115 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3117 /* Using this macro rather than (for example) arrays of messages
3118 ensures that all the format strings are checked at compile
3120 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3121 : (use == lv_increment ? (I) \
3122 : (use == lv_decrement ? (D) : (AS))))
3123 if (TREE_CODE (arg) == COMPONENT_REF)
3125 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3126 readonly_error (TREE_OPERAND (arg, 0), use);
3128 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3129 G_("increment of read-only member %qD"),
3130 G_("decrement of read-only member %qD"),
3131 G_("read-only member %qD used as %<asm%> output")),
3132 TREE_OPERAND (arg, 1));
3134 else if (TREE_CODE (arg) == VAR_DECL)
3135 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3136 G_("increment of read-only variable %qD"),
3137 G_("decrement of read-only variable %qD"),
3138 G_("read-only variable %qD used as %<asm%> output")),
3141 error (READONLY_MSG (G_("assignment of read-only location"),
3142 G_("increment of read-only location"),
3143 G_("decrement of read-only location"),
3144 G_("read-only location used as %<asm%> output")));
3148 /* Return nonzero if REF is an lvalue valid for this language;
3149 otherwise, print an error message and return zero. USE says
3150 how the lvalue is being used and so selects the error message. */
3153 lvalue_or_else (tree ref, enum lvalue_use use)
3155 int win = lvalue_p (ref);
3163 /* Mark EXP saying that we need to be able to take the
3164 address of it; it should not be allocated in a register.
3165 Returns true if successful. */
3168 c_mark_addressable (tree exp)
3173 switch (TREE_CODE (x))
3176 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3179 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3183 /* ... fall through ... */
3189 x = TREE_OPERAND (x, 0);
3192 case COMPOUND_LITERAL_EXPR:
3194 TREE_ADDRESSABLE (x) = 1;
3201 if (C_DECL_REGISTER (x)
3202 && DECL_NONLOCAL (x))
3204 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3207 ("global register variable %qD used in nested function", x);
3210 pedwarn ("register variable %qD used in nested function", x);
3212 else if (C_DECL_REGISTER (x))
3214 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3215 error ("address of global register variable %qD requested", x);
3217 error ("address of register variable %qD requested", x);
3223 TREE_ADDRESSABLE (x) = 1;
3230 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3233 build_conditional_expr (tree ifexp, tree op1, tree op2)
3237 enum tree_code code1;
3238 enum tree_code code2;
3239 tree result_type = NULL;
3240 tree orig_op1 = op1, orig_op2 = op2;
3242 /* Promote both alternatives. */
3244 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3245 op1 = default_conversion (op1);
3246 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3247 op2 = default_conversion (op2);
3249 if (TREE_CODE (ifexp) == ERROR_MARK
3250 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3251 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3252 return error_mark_node;
3254 type1 = TREE_TYPE (op1);
3255 code1 = TREE_CODE (type1);
3256 type2 = TREE_TYPE (op2);
3257 code2 = TREE_CODE (type2);
3259 /* C90 does not permit non-lvalue arrays in conditional expressions.
3260 In C99 they will be pointers by now. */
3261 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3263 error ("non-lvalue array in conditional expression");
3264 return error_mark_node;
3267 /* Quickly detect the usual case where op1 and op2 have the same type
3269 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3272 result_type = type1;
3274 result_type = TYPE_MAIN_VARIANT (type1);
3276 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3277 || code1 == COMPLEX_TYPE)
3278 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3279 || code2 == COMPLEX_TYPE))
3281 result_type = c_common_type (type1, type2);
3283 /* If -Wsign-compare, warn here if type1 and type2 have
3284 different signedness. We'll promote the signed to unsigned
3285 and later code won't know it used to be different.
3286 Do this check on the original types, so that explicit casts
3287 will be considered, but default promotions won't. */
3288 if (warn_sign_compare && !skip_evaluation)
3290 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3291 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3293 if (unsigned_op1 ^ unsigned_op2)
3295 /* Do not warn if the result type is signed, since the
3296 signed type will only be chosen if it can represent
3297 all the values of the unsigned type. */
3298 if (!TYPE_UNSIGNED (result_type))
3300 /* Do not warn if the signed quantity is an unsuffixed
3301 integer literal (or some static constant expression
3302 involving such literals) and it is non-negative. */
3303 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3304 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3307 warning (0, "signed and unsigned type in conditional expression");
3311 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3313 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3314 pedwarn ("ISO C forbids conditional expr with only one void side");
3315 result_type = void_type_node;
3317 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3319 if (comp_target_types (type1, type2))
3320 result_type = common_pointer_type (type1, type2);
3321 else if (null_pointer_constant_p (orig_op1))
3322 result_type = qualify_type (type2, type1);
3323 else if (null_pointer_constant_p (orig_op2))
3324 result_type = qualify_type (type1, type2);
3325 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3327 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3328 pedwarn ("ISO C forbids conditional expr between "
3329 "%<void *%> and function pointer");
3330 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3331 TREE_TYPE (type2)));
3333 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3335 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3336 pedwarn ("ISO C forbids conditional expr between "
3337 "%<void *%> and function pointer");
3338 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3339 TREE_TYPE (type1)));
3343 pedwarn ("pointer type mismatch in conditional expression");
3344 result_type = build_pointer_type (void_type_node);
3347 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3349 if (!null_pointer_constant_p (orig_op2))
3350 pedwarn ("pointer/integer type mismatch in conditional expression");
3353 op2 = null_pointer_node;
3355 result_type = type1;
3357 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3359 if (!null_pointer_constant_p (orig_op1))
3360 pedwarn ("pointer/integer type mismatch in conditional expression");
3363 op1 = null_pointer_node;
3365 result_type = type2;
3370 if (flag_cond_mismatch)
3371 result_type = void_type_node;
3374 error ("type mismatch in conditional expression");
3375 return error_mark_node;
3379 /* Merge const and volatile flags of the incoming types. */
3381 = build_type_variant (result_type,
3382 TREE_READONLY (op1) || TREE_READONLY (op2),
3383 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3385 if (result_type != TREE_TYPE (op1))
3386 op1 = convert_and_check (result_type, op1);
3387 if (result_type != TREE_TYPE (op2))
3388 op2 = convert_and_check (result_type, op2);
3390 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3393 /* Return a compound expression that performs two expressions and
3394 returns the value of the second of them. */
3397 build_compound_expr (tree expr1, tree expr2)
3399 if (!TREE_SIDE_EFFECTS (expr1))
3401 /* The left-hand operand of a comma expression is like an expression
3402 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3403 any side-effects, unless it was explicitly cast to (void). */
3404 if (warn_unused_value)
3406 if (VOID_TYPE_P (TREE_TYPE (expr1))
3407 && (TREE_CODE (expr1) == NOP_EXPR
3408 || TREE_CODE (expr1) == CONVERT_EXPR))
3410 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3411 && TREE_CODE (expr1) == COMPOUND_EXPR
3412 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3413 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3414 ; /* (void) a, (void) b, c */
3416 warning (0, "left-hand operand of comma expression has no effect");
3420 /* With -Wunused, we should also warn if the left-hand operand does have
3421 side-effects, but computes a value which is not used. For example, in
3422 `foo() + bar(), baz()' the result of the `+' operator is not used,
3423 so we should issue a warning. */
3424 else if (warn_unused_value)
3425 warn_if_unused_value (expr1, input_location);
3427 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3430 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3433 build_c_cast (tree type, tree expr)
3437 if (type == error_mark_node || expr == error_mark_node)
3438 return error_mark_node;
3440 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3441 only in <protocol> qualifications. But when constructing&n