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, 2007, 2008, 2009
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
34 #include "langhooks.h"
45 #include "tree-iterator.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
58 /* Whether we are building a boolean conversion inside
59 convert_for_assignment, or some other late binary operation. If
60 build_binary_op is called (from code shared with C++) in this case,
61 then the operands have already been folded and the result will not
62 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
63 bool in_late_binary_op;
65 /* The level of nesting inside "__alignof__". */
68 /* The level of nesting inside "sizeof". */
71 /* The level of nesting inside "typeof". */
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned;
78 static int require_constant_value;
79 static int require_constant_elements;
81 static bool null_pointer_constant_p (const_tree);
82 static tree qualify_type (tree, tree);
83 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *);
84 static int comp_target_types (location_t, tree, tree);
85 static int function_types_compatible_p (const_tree, const_tree, bool *);
86 static int type_lists_compatible_p (const_tree, const_tree, bool *);
87 static tree lookup_field (tree, tree);
88 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
90 static tree pointer_diff (location_t, tree, tree);
91 static tree convert_for_assignment (location_t, tree, tree, tree,
92 enum impl_conv, bool, tree, tree, int);
93 static tree valid_compound_expr_initializer (tree, tree);
94 static void push_string (const char *);
95 static void push_member_name (tree);
96 static int spelling_length (void);
97 static char *print_spelling (char *);
98 static void warning_init (int, const char *);
99 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
100 static void output_init_element (tree, tree, bool, tree, tree, int, bool);
101 static void output_pending_init_elements (int);
102 static int set_designator (int);
103 static void push_range_stack (tree);
104 static void add_pending_init (tree, tree, tree, bool);
105 static void set_nonincremental_init (void);
106 static void set_nonincremental_init_from_string (tree);
107 static tree find_init_member (tree);
108 static void readonly_error (tree, enum lvalue_use);
109 static void readonly_warning (tree, enum lvalue_use);
110 static int lvalue_or_else (const_tree, enum lvalue_use);
111 static void record_maybe_used_decl (tree);
112 static int comptypes_internal (const_tree, const_tree, bool *);
114 /* Return true if EXP is a null pointer constant, false otherwise. */
117 null_pointer_constant_p (const_tree expr)
119 /* This should really operate on c_expr structures, but they aren't
120 yet available everywhere required. */
121 tree type = TREE_TYPE (expr);
122 return (TREE_CODE (expr) == INTEGER_CST
123 && !TREE_OVERFLOW (expr)
124 && integer_zerop (expr)
125 && (INTEGRAL_TYPE_P (type)
126 || (TREE_CODE (type) == POINTER_TYPE
127 && VOID_TYPE_P (TREE_TYPE (type))
128 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
131 /* EXPR may appear in an unevaluated part of an integer constant
132 expression, but not in an evaluated part. Wrap it in a
133 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
134 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
137 note_integer_operands (tree expr)
140 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
142 ret = copy_node (expr);
143 TREE_OVERFLOW (ret) = 1;
147 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
148 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
153 /* Having checked whether EXPR may appear in an unevaluated part of an
154 integer constant expression and found that it may, remove any
155 C_MAYBE_CONST_EXPR noting this fact and return the resulting
159 remove_c_maybe_const_expr (tree expr)
161 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
162 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 \f/* This is a cache to hold if two types are compatible or not. */
169 struct tagged_tu_seen_cache {
170 const struct tagged_tu_seen_cache * next;
173 /* The return value of tagged_types_tu_compatible_p if we had seen
174 these two types already. */
178 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
179 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
181 /* Do `exp = require_complete_type (exp);' to make sure exp
182 does not have an incomplete type. (That includes void types.) */
185 require_complete_type (tree value)
187 tree type = TREE_TYPE (value);
189 if (value == error_mark_node || type == error_mark_node)
190 return error_mark_node;
192 /* First, detect a valid value with a complete type. */
193 if (COMPLETE_TYPE_P (type))
196 c_incomplete_type_error (value, type);
197 return error_mark_node;
200 /* Print an error message for invalid use of an incomplete type.
201 VALUE is the expression that was used (or 0 if that isn't known)
202 and TYPE is the type that was invalid. */
205 c_incomplete_type_error (const_tree value, const_tree type)
207 const char *type_code_string;
209 /* Avoid duplicate error message. */
210 if (TREE_CODE (type) == ERROR_MARK)
213 if (value != 0 && (TREE_CODE (value) == VAR_DECL
214 || TREE_CODE (value) == PARM_DECL))
215 error ("%qD has an incomplete type", value);
219 /* We must print an error message. Be clever about what it says. */
221 switch (TREE_CODE (type))
224 type_code_string = "struct";
228 type_code_string = "union";
232 type_code_string = "enum";
236 error ("invalid use of void expression");
240 if (TYPE_DOMAIN (type))
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 error ("invalid use of flexible array member");
247 type = TREE_TYPE (type);
250 error ("invalid use of array with unspecified bounds");
257 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
258 error ("invalid use of undefined type %<%s %E%>",
259 type_code_string, TYPE_NAME (type));
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
270 c_type_promotes_to (tree type)
272 if (TYPE_MAIN_VARIANT (type) == float_type_node)
273 return double_type_node;
275 if (c_promoting_integer_type_p (type))
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type)
279 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
280 return unsigned_type_node;
281 return integer_type_node;
287 /* Return a variant of TYPE which has all the type qualifiers of LIKE
288 as well as those of TYPE. */
291 qualify_type (tree type, tree like)
293 return c_build_qualified_type (type,
294 TYPE_QUALS (type) | TYPE_QUALS (like));
297 /* Return true iff the given tree T is a variable length array. */
300 c_vla_type_p (const_tree t)
302 if (TREE_CODE (t) == ARRAY_TYPE
303 && C_TYPE_VARIABLE_SIZE (t))
308 /* Return the composite type of two compatible types.
310 We assume that comptypes has already been done and returned
311 nonzero; if that isn't so, this may crash. In particular, we
312 assume that qualifiers match. */
315 composite_type (tree t1, tree t2)
317 enum tree_code code1;
318 enum tree_code code2;
321 /* Save time if the two types are the same. */
323 if (t1 == t2) return t1;
325 /* If one type is nonsense, use the other. */
326 if (t1 == error_mark_node)
328 if (t2 == error_mark_node)
331 code1 = TREE_CODE (t1);
332 code2 = TREE_CODE (t2);
334 /* Merge the attributes. */
335 attributes = targetm.merge_type_attributes (t1, t2);
337 /* If one is an enumerated type and the other is the compatible
338 integer type, the composite type might be either of the two
339 (DR#013 question 3). For consistency, use the enumerated type as
340 the composite type. */
342 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
344 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
347 gcc_assert (code1 == code2);
352 /* For two pointers, do this recursively on the target type. */
354 tree pointed_to_1 = TREE_TYPE (t1);
355 tree pointed_to_2 = TREE_TYPE (t2);
356 tree target = composite_type (pointed_to_1, pointed_to_2);
357 t1 = build_pointer_type (target);
358 t1 = build_type_attribute_variant (t1, attributes);
359 return qualify_type (t1, t2);
364 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
367 tree d1 = TYPE_DOMAIN (t1);
368 tree d2 = TYPE_DOMAIN (t2);
369 bool d1_variable, d2_variable;
370 bool d1_zero, d2_zero;
371 bool t1_complete, t2_complete;
373 /* We should not have any type quals on arrays at all. */
374 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
376 t1_complete = COMPLETE_TYPE_P (t1);
377 t2_complete = COMPLETE_TYPE_P (t2);
379 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
380 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
382 d1_variable = (!d1_zero
383 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
384 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
385 d2_variable = (!d2_zero
386 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
387 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
388 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
389 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
391 /* Save space: see if the result is identical to one of the args. */
392 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
393 && (d2_variable || d2_zero || !d1_variable))
394 return build_type_attribute_variant (t1, attributes);
395 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
396 && (d1_variable || d1_zero || !d2_variable))
397 return build_type_attribute_variant (t2, attributes);
399 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
400 return build_type_attribute_variant (t1, attributes);
401 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
402 return build_type_attribute_variant (t2, attributes);
404 /* Merge the element types, and have a size if either arg has
405 one. We may have qualifiers on the element types. To set
406 up TYPE_MAIN_VARIANT correctly, we need to form the
407 composite of the unqualified types and add the qualifiers
409 quals = TYPE_QUALS (strip_array_types (elt));
410 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
411 t1 = build_array_type (unqual_elt,
412 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
418 /* Ensure a composite type involving a zero-length array type
419 is a zero-length type not an incomplete type. */
420 if (d1_zero && d2_zero
421 && (t1_complete || t2_complete)
422 && !COMPLETE_TYPE_P (t1))
424 TYPE_SIZE (t1) = bitsize_zero_node;
425 TYPE_SIZE_UNIT (t1) = size_zero_node;
427 t1 = c_build_qualified_type (t1, quals);
428 return build_type_attribute_variant (t1, attributes);
434 if (attributes != NULL)
436 /* Try harder not to create a new aggregate type. */
437 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
439 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
442 return build_type_attribute_variant (t1, attributes);
445 /* Function types: prefer the one that specified arg types.
446 If both do, merge the arg types. Also merge the return types. */
448 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
449 tree p1 = TYPE_ARG_TYPES (t1);
450 tree p2 = TYPE_ARG_TYPES (t2);
455 /* Save space: see if the result is identical to one of the args. */
456 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
457 return build_type_attribute_variant (t1, attributes);
458 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
459 return build_type_attribute_variant (t2, attributes);
461 /* Simple way if one arg fails to specify argument types. */
462 if (TYPE_ARG_TYPES (t1) == 0)
464 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
465 t1 = build_type_attribute_variant (t1, attributes);
466 return qualify_type (t1, t2);
468 if (TYPE_ARG_TYPES (t2) == 0)
470 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
471 t1 = build_type_attribute_variant (t1, attributes);
472 return qualify_type (t1, t2);
475 /* If both args specify argument types, we must merge the two
476 lists, argument by argument. */
477 /* Tell global_bindings_p to return false so that variable_size
478 doesn't die on VLAs in parameter types. */
479 c_override_global_bindings_to_false = true;
481 len = list_length (p1);
484 for (i = 0; i < len; i++)
485 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
490 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
492 /* A null type means arg type is not specified.
493 Take whatever the other function type has. */
494 if (TREE_VALUE (p1) == 0)
496 TREE_VALUE (n) = TREE_VALUE (p2);
499 if (TREE_VALUE (p2) == 0)
501 TREE_VALUE (n) = TREE_VALUE (p1);
505 /* Given wait (union {union wait *u; int *i} *)
506 and wait (union wait *),
507 prefer union wait * as type of parm. */
508 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
509 && TREE_VALUE (p1) != TREE_VALUE (p2))
512 tree mv2 = TREE_VALUE (p2);
513 if (mv2 && mv2 != error_mark_node
514 && TREE_CODE (mv2) != ARRAY_TYPE)
515 mv2 = TYPE_MAIN_VARIANT (mv2);
516 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
517 memb; memb = TREE_CHAIN (memb))
519 tree mv3 = TREE_TYPE (memb);
520 if (mv3 && mv3 != error_mark_node
521 && TREE_CODE (mv3) != ARRAY_TYPE)
522 mv3 = TYPE_MAIN_VARIANT (mv3);
523 if (comptypes (mv3, mv2))
525 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
527 pedwarn (input_location, OPT_pedantic,
528 "function types not truly compatible in ISO C");
533 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
534 && TREE_VALUE (p2) != TREE_VALUE (p1))
537 tree mv1 = TREE_VALUE (p1);
538 if (mv1 && mv1 != error_mark_node
539 && TREE_CODE (mv1) != ARRAY_TYPE)
540 mv1 = TYPE_MAIN_VARIANT (mv1);
541 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
542 memb; memb = TREE_CHAIN (memb))
544 tree mv3 = TREE_TYPE (memb);
545 if (mv3 && mv3 != error_mark_node
546 && TREE_CODE (mv3) != ARRAY_TYPE)
547 mv3 = TYPE_MAIN_VARIANT (mv3);
548 if (comptypes (mv3, mv1))
550 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
552 pedwarn (input_location, OPT_pedantic,
553 "function types not truly compatible in ISO C");
558 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
562 c_override_global_bindings_to_false = false;
563 t1 = build_function_type (valtype, newargs);
564 t1 = qualify_type (t1, t2);
565 /* ... falls through ... */
569 return build_type_attribute_variant (t1, attributes);
574 /* Return the type of a conditional expression between pointers to
575 possibly differently qualified versions of compatible types.
577 We assume that comp_target_types has already been done and returned
578 nonzero; if that isn't so, this may crash. */
581 common_pointer_type (tree t1, tree t2)
584 tree pointed_to_1, mv1;
585 tree pointed_to_2, mv2;
587 unsigned target_quals;
589 /* Save time if the two types are the same. */
591 if (t1 == t2) return t1;
593 /* If one type is nonsense, use the other. */
594 if (t1 == error_mark_node)
596 if (t2 == error_mark_node)
599 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
600 && TREE_CODE (t2) == POINTER_TYPE);
602 /* Merge the attributes. */
603 attributes = targetm.merge_type_attributes (t1, t2);
605 /* Find the composite type of the target types, and combine the
606 qualifiers of the two types' targets. Do not lose qualifiers on
607 array element types by taking the TYPE_MAIN_VARIANT. */
608 mv1 = pointed_to_1 = TREE_TYPE (t1);
609 mv2 = pointed_to_2 = TREE_TYPE (t2);
610 if (TREE_CODE (mv1) != ARRAY_TYPE)
611 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
612 if (TREE_CODE (mv2) != ARRAY_TYPE)
613 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
614 target = composite_type (mv1, mv2);
616 /* For function types do not merge const qualifiers, but drop them
617 if used inconsistently. The middle-end uses these to mark const
618 and noreturn functions. */
619 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
620 target_quals = TYPE_QUALS (pointed_to_1) & TYPE_QUALS (pointed_to_2);
622 target_quals = TYPE_QUALS (pointed_to_1) | TYPE_QUALS (pointed_to_2);
623 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
624 return build_type_attribute_variant (t1, attributes);
627 /* Return the common type for two arithmetic types under the usual
628 arithmetic conversions. The default conversions have already been
629 applied, and enumerated types converted to their compatible integer
630 types. The resulting type is unqualified and has no attributes.
632 This is the type for the result of most arithmetic operations
633 if the operands have the given two types. */
636 c_common_type (tree t1, tree t2)
638 enum tree_code code1;
639 enum tree_code code2;
641 /* If one type is nonsense, use the other. */
642 if (t1 == error_mark_node)
644 if (t2 == error_mark_node)
647 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
648 t1 = TYPE_MAIN_VARIANT (t1);
650 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
651 t2 = TYPE_MAIN_VARIANT (t2);
653 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
654 t1 = build_type_attribute_variant (t1, NULL_TREE);
656 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
657 t2 = build_type_attribute_variant (t2, NULL_TREE);
659 /* Save time if the two types are the same. */
661 if (t1 == t2) return t1;
663 code1 = TREE_CODE (t1);
664 code2 = TREE_CODE (t2);
666 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
667 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
668 || code1 == INTEGER_TYPE);
669 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
670 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
671 || code2 == INTEGER_TYPE);
673 /* When one operand is a decimal float type, the other operand cannot be
674 a generic float type or a complex type. We also disallow vector types
676 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
677 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
679 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
681 error ("can%'t mix operands of decimal float and vector types");
682 return error_mark_node;
684 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
686 error ("can%'t mix operands of decimal float and complex types");
687 return error_mark_node;
689 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
691 error ("can%'t mix operands of decimal float and other float types");
692 return error_mark_node;
696 /* If one type is a vector type, return that type. (How the usual
697 arithmetic conversions apply to the vector types extension is not
698 precisely specified.) */
699 if (code1 == VECTOR_TYPE)
702 if (code2 == VECTOR_TYPE)
705 /* If one type is complex, form the common type of the non-complex
706 components, then make that complex. Use T1 or T2 if it is the
708 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
710 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
711 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
712 tree subtype = c_common_type (subtype1, subtype2);
714 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
716 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
719 return build_complex_type (subtype);
722 /* If only one is real, use it as the result. */
724 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
727 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
730 /* If both are real and either are decimal floating point types, use
731 the decimal floating point type with the greater precision. */
733 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
735 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
736 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
737 return dfloat128_type_node;
738 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
739 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
740 return dfloat64_type_node;
741 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
742 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
743 return dfloat32_type_node;
746 /* Deal with fixed-point types. */
747 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
749 unsigned int unsignedp = 0, satp = 0;
750 enum machine_mode m1, m2;
751 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
756 /* If one input type is saturating, the result type is saturating. */
757 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
760 /* If both fixed-point types are unsigned, the result type is unsigned.
761 When mixing fixed-point and integer types, follow the sign of the
763 Otherwise, the result type is signed. */
764 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
765 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
766 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
767 && TYPE_UNSIGNED (t1))
768 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
769 && TYPE_UNSIGNED (t2)))
772 /* The result type is signed. */
775 /* If the input type is unsigned, we need to convert to the
777 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
779 enum mode_class mclass = (enum mode_class) 0;
780 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
782 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
786 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
788 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
790 enum mode_class mclass = (enum mode_class) 0;
791 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
793 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
797 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
801 if (code1 == FIXED_POINT_TYPE)
803 fbit1 = GET_MODE_FBIT (m1);
804 ibit1 = GET_MODE_IBIT (m1);
809 /* Signed integers need to subtract one sign bit. */
810 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
813 if (code2 == FIXED_POINT_TYPE)
815 fbit2 = GET_MODE_FBIT (m2);
816 ibit2 = GET_MODE_IBIT (m2);
821 /* Signed integers need to subtract one sign bit. */
822 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
825 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
826 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
827 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
831 /* Both real or both integers; use the one with greater precision. */
833 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
835 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
838 /* Same precision. Prefer long longs to longs to ints when the
839 same precision, following the C99 rules on integer type rank
840 (which are equivalent to the C90 rules for C90 types). */
842 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
843 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
844 return long_long_unsigned_type_node;
846 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
847 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
849 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
850 return long_long_unsigned_type_node;
852 return long_long_integer_type_node;
855 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
856 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
857 return long_unsigned_type_node;
859 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
860 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
862 /* But preserve unsignedness from the other type,
863 since long cannot hold all the values of an unsigned int. */
864 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
865 return long_unsigned_type_node;
867 return long_integer_type_node;
870 /* Likewise, prefer long double to double even if same size. */
871 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
872 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
873 return long_double_type_node;
875 /* Otherwise prefer the unsigned one. */
877 if (TYPE_UNSIGNED (t1))
883 /* Wrapper around c_common_type that is used by c-common.c and other
884 front end optimizations that remove promotions. ENUMERAL_TYPEs
885 are allowed here and are converted to their compatible integer types.
886 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
887 preferably a non-Boolean type as the common type. */
889 common_type (tree t1, tree t2)
891 if (TREE_CODE (t1) == ENUMERAL_TYPE)
892 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
893 if (TREE_CODE (t2) == ENUMERAL_TYPE)
894 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
896 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
897 if (TREE_CODE (t1) == BOOLEAN_TYPE
898 && TREE_CODE (t2) == BOOLEAN_TYPE)
899 return boolean_type_node;
901 /* If either type is BOOLEAN_TYPE, then return the other. */
902 if (TREE_CODE (t1) == BOOLEAN_TYPE)
904 if (TREE_CODE (t2) == BOOLEAN_TYPE)
907 return c_common_type (t1, t2);
910 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
911 or various other operations. Return 2 if they are compatible
912 but a warning may be needed if you use them together. */
915 comptypes (tree type1, tree type2)
917 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
920 val = comptypes_internal (type1, type2, NULL);
921 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
926 /* Like comptypes, but if it returns non-zero because enum and int are
927 compatible, it sets *ENUM_AND_INT_P to true. */
930 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
932 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
935 val = comptypes_internal (type1, type2, enum_and_int_p);
936 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
941 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
942 or various other operations. Return 2 if they are compatible
943 but a warning may be needed if you use them together. If
944 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
945 compatible integer type, then this sets *ENUM_AND_INT_P to true;
946 *ENUM_AND_INT_P is never set to false. This differs from
947 comptypes, in that we don't free the seen types. */
950 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p)
952 const_tree t1 = type1;
953 const_tree t2 = type2;
956 /* Suppress errors caused by previously reported errors. */
958 if (t1 == t2 || !t1 || !t2
959 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
962 /* If either type is the internal version of sizetype, return the
964 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
965 && TYPE_ORIG_SIZE_TYPE (t1))
966 t1 = TYPE_ORIG_SIZE_TYPE (t1);
968 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
969 && TYPE_ORIG_SIZE_TYPE (t2))
970 t2 = TYPE_ORIG_SIZE_TYPE (t2);
973 /* Enumerated types are compatible with integer types, but this is
974 not transitive: two enumerated types in the same translation unit
975 are compatible with each other only if they are the same type. */
977 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
979 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
980 if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE)
981 *enum_and_int_p = true;
983 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
986 if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE)
987 *enum_and_int_p = true;
993 /* Different classes of types can't be compatible. */
995 if (TREE_CODE (t1) != TREE_CODE (t2))
998 /* Qualifiers must match. C99 6.7.3p9 */
1000 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1003 /* Allow for two different type nodes which have essentially the same
1004 definition. Note that we already checked for equality of the type
1005 qualifiers (just above). */
1007 if (TREE_CODE (t1) != ARRAY_TYPE
1008 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1011 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1012 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1015 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1018 switch (TREE_CODE (t1))
1021 /* Do not remove mode or aliasing information. */
1022 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1023 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1025 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1026 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1031 val = function_types_compatible_p (t1, t2, enum_and_int_p);
1036 tree d1 = TYPE_DOMAIN (t1);
1037 tree d2 = TYPE_DOMAIN (t2);
1038 bool d1_variable, d2_variable;
1039 bool d1_zero, d2_zero;
1042 /* Target types must match incl. qualifiers. */
1043 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1044 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1048 /* Sizes must match unless one is missing or variable. */
1049 if (d1 == 0 || d2 == 0 || d1 == d2)
1052 d1_zero = !TYPE_MAX_VALUE (d1);
1053 d2_zero = !TYPE_MAX_VALUE (d2);
1055 d1_variable = (!d1_zero
1056 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1057 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1058 d2_variable = (!d2_zero
1059 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1060 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1061 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1062 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1064 if (d1_variable || d2_variable)
1066 if (d1_zero && d2_zero)
1068 if (d1_zero || d2_zero
1069 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1070 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1079 if (val != 1 && !same_translation_unit_p (t1, t2))
1081 tree a1 = TYPE_ATTRIBUTES (t1);
1082 tree a2 = TYPE_ATTRIBUTES (t2);
1084 if (! attribute_list_contained (a1, a2)
1085 && ! attribute_list_contained (a2, a1))
1089 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1090 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1095 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1096 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1103 return attrval == 2 && val == 1 ? 2 : val;
1106 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
1107 ignoring their qualifiers. */
1110 comp_target_types (location_t location, tree ttl, tree ttr)
1114 bool enum_and_int_p;
1116 /* Do not lose qualifiers on element types of array types that are
1117 pointer targets by taking their TYPE_MAIN_VARIANT. */
1118 mvl = TREE_TYPE (ttl);
1119 mvr = TREE_TYPE (ttr);
1120 if (TREE_CODE (mvl) != ARRAY_TYPE)
1121 mvl = TYPE_MAIN_VARIANT (mvl);
1122 if (TREE_CODE (mvr) != ARRAY_TYPE)
1123 mvr = TYPE_MAIN_VARIANT (mvr);
1124 enum_and_int_p = false;
1125 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1128 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1130 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1131 warning_at (location, OPT_Wc___compat,
1132 "pointer target types incompatible in C++");
1137 /* Subroutines of `comptypes'. */
1139 /* Determine whether two trees derive from the same translation unit.
1140 If the CONTEXT chain ends in a null, that tree's context is still
1141 being parsed, so if two trees have context chains ending in null,
1142 they're in the same translation unit. */
1144 same_translation_unit_p (const_tree t1, const_tree t2)
1146 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1147 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1149 case tcc_declaration:
1150 t1 = DECL_CONTEXT (t1); break;
1152 t1 = TYPE_CONTEXT (t1); break;
1153 case tcc_exceptional:
1154 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1155 default: gcc_unreachable ();
1158 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1159 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1161 case tcc_declaration:
1162 t2 = DECL_CONTEXT (t2); break;
1164 t2 = TYPE_CONTEXT (t2); break;
1165 case tcc_exceptional:
1166 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1167 default: gcc_unreachable ();
1173 /* Allocate the seen two types, assuming that they are compatible. */
1175 static struct tagged_tu_seen_cache *
1176 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1178 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1179 tu->next = tagged_tu_seen_base;
1183 tagged_tu_seen_base = tu;
1185 /* The C standard says that two structures in different translation
1186 units are compatible with each other only if the types of their
1187 fields are compatible (among other things). We assume that they
1188 are compatible until proven otherwise when building the cache.
1189 An example where this can occur is:
1194 If we are comparing this against a similar struct in another TU,
1195 and did not assume they were compatible, we end up with an infinite
1201 /* Free the seen types until we get to TU_TIL. */
1204 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1206 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1207 while (tu != tu_til)
1209 const struct tagged_tu_seen_cache *const tu1
1210 = (const struct tagged_tu_seen_cache *) tu;
1212 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1214 tagged_tu_seen_base = tu_til;
1217 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1218 compatible. If the two types are not the same (which has been
1219 checked earlier), this can only happen when multiple translation
1220 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1221 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1224 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1225 bool *enum_and_int_p)
1228 bool needs_warning = false;
1230 /* We have to verify that the tags of the types are the same. This
1231 is harder than it looks because this may be a typedef, so we have
1232 to go look at the original type. It may even be a typedef of a
1234 In the case of compiler-created builtin structs the TYPE_DECL
1235 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1236 while (TYPE_NAME (t1)
1237 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1238 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1239 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1241 while (TYPE_NAME (t2)
1242 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1243 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1244 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1246 /* C90 didn't have the requirement that the two tags be the same. */
1247 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1250 /* C90 didn't say what happened if one or both of the types were
1251 incomplete; we choose to follow C99 rules here, which is that they
1253 if (TYPE_SIZE (t1) == NULL
1254 || TYPE_SIZE (t2) == NULL)
1258 const struct tagged_tu_seen_cache * tts_i;
1259 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1260 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1264 switch (TREE_CODE (t1))
1268 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1269 /* Speed up the case where the type values are in the same order. */
1270 tree tv1 = TYPE_VALUES (t1);
1271 tree tv2 = TYPE_VALUES (t2);
1278 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1280 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1282 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1289 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1293 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1299 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1305 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1307 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1309 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1320 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1321 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1327 /* Speed up the common case where the fields are in the same order. */
1328 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1329 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1333 if (DECL_NAME (s1) != DECL_NAME (s2))
1335 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1338 if (result != 1 && !DECL_NAME (s1))
1346 needs_warning = true;
1348 if (TREE_CODE (s1) == FIELD_DECL
1349 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1350 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1358 tu->val = needs_warning ? 2 : 1;
1362 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1366 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1367 if (DECL_NAME (s1) == DECL_NAME (s2))
1371 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1374 if (result != 1 && !DECL_NAME (s1))
1382 needs_warning = true;
1384 if (TREE_CODE (s1) == FIELD_DECL
1385 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1386 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1398 tu->val = needs_warning ? 2 : 10;
1404 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1406 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1408 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1411 if (TREE_CODE (s1) != TREE_CODE (s2)
1412 || DECL_NAME (s1) != DECL_NAME (s2))
1414 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1419 needs_warning = true;
1421 if (TREE_CODE (s1) == FIELD_DECL
1422 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1423 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1429 tu->val = needs_warning ? 2 : 1;
1438 /* Return 1 if two function types F1 and F2 are compatible.
1439 If either type specifies no argument types,
1440 the other must specify a fixed number of self-promoting arg types.
1441 Otherwise, if one type specifies only the number of arguments,
1442 the other must specify that number of self-promoting arg types.
1443 Otherwise, the argument types must match.
1444 ENUM_AND_INT_P is as in comptypes_internal. */
1447 function_types_compatible_p (const_tree f1, const_tree f2,
1448 bool *enum_and_int_p)
1451 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1456 ret1 = TREE_TYPE (f1);
1457 ret2 = TREE_TYPE (f2);
1459 /* 'volatile' qualifiers on a function's return type used to mean
1460 the function is noreturn. */
1461 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1462 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1463 if (TYPE_VOLATILE (ret1))
1464 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1465 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1466 if (TYPE_VOLATILE (ret2))
1467 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1468 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1469 val = comptypes_internal (ret1, ret2, enum_and_int_p);
1473 args1 = TYPE_ARG_TYPES (f1);
1474 args2 = TYPE_ARG_TYPES (f2);
1476 /* An unspecified parmlist matches any specified parmlist
1477 whose argument types don't need default promotions. */
1481 if (!self_promoting_args_p (args2))
1483 /* If one of these types comes from a non-prototype fn definition,
1484 compare that with the other type's arglist.
1485 If they don't match, ask for a warning (but no error). */
1486 if (TYPE_ACTUAL_ARG_TYPES (f1)
1487 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1494 if (!self_promoting_args_p (args1))
1496 if (TYPE_ACTUAL_ARG_TYPES (f2)
1497 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1503 /* Both types have argument lists: compare them and propagate results. */
1504 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p);
1505 return val1 != 1 ? val1 : val;
1508 /* Check two lists of types for compatibility, returning 0 for
1509 incompatible, 1 for compatible, or 2 for compatible with
1510 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1513 type_lists_compatible_p (const_tree args1, const_tree args2,
1514 bool *enum_and_int_p)
1516 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1522 tree a1, mv1, a2, mv2;
1523 if (args1 == 0 && args2 == 0)
1525 /* If one list is shorter than the other,
1526 they fail to match. */
1527 if (args1 == 0 || args2 == 0)
1529 mv1 = a1 = TREE_VALUE (args1);
1530 mv2 = a2 = TREE_VALUE (args2);
1531 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1532 mv1 = TYPE_MAIN_VARIANT (mv1);
1533 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1534 mv2 = TYPE_MAIN_VARIANT (mv2);
1535 /* A null pointer instead of a type
1536 means there is supposed to be an argument
1537 but nothing is specified about what type it has.
1538 So match anything that self-promotes. */
1541 if (c_type_promotes_to (a2) != a2)
1546 if (c_type_promotes_to (a1) != a1)
1549 /* If one of the lists has an error marker, ignore this arg. */
1550 else if (TREE_CODE (a1) == ERROR_MARK
1551 || TREE_CODE (a2) == ERROR_MARK)
1553 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p)))
1555 /* Allow wait (union {union wait *u; int *i} *)
1556 and wait (union wait *) to be compatible. */
1557 if (TREE_CODE (a1) == UNION_TYPE
1558 && (TYPE_NAME (a1) == 0
1559 || TYPE_TRANSPARENT_UNION (a1))
1560 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1561 && tree_int_cst_equal (TYPE_SIZE (a1),
1565 for (memb = TYPE_FIELDS (a1);
1566 memb; memb = TREE_CHAIN (memb))
1568 tree mv3 = TREE_TYPE (memb);
1569 if (mv3 && mv3 != error_mark_node
1570 && TREE_CODE (mv3) != ARRAY_TYPE)
1571 mv3 = TYPE_MAIN_VARIANT (mv3);
1572 if (comptypes_internal (mv3, mv2, enum_and_int_p))
1578 else if (TREE_CODE (a2) == UNION_TYPE
1579 && (TYPE_NAME (a2) == 0
1580 || TYPE_TRANSPARENT_UNION (a2))
1581 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1582 && tree_int_cst_equal (TYPE_SIZE (a2),
1586 for (memb = TYPE_FIELDS (a2);
1587 memb; memb = TREE_CHAIN (memb))
1589 tree mv3 = TREE_TYPE (memb);
1590 if (mv3 && mv3 != error_mark_node
1591 && TREE_CODE (mv3) != ARRAY_TYPE)
1592 mv3 = TYPE_MAIN_VARIANT (mv3);
1593 if (comptypes_internal (mv3, mv1, enum_and_int_p))
1603 /* comptypes said ok, but record if it said to warn. */
1607 args1 = TREE_CHAIN (args1);
1608 args2 = TREE_CHAIN (args2);
1612 /* Compute the size to increment a pointer by. */
1615 c_size_in_bytes (const_tree type)
1617 enum tree_code code = TREE_CODE (type);
1619 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1620 return size_one_node;
1622 if (!COMPLETE_OR_VOID_TYPE_P (type))
1624 error ("arithmetic on pointer to an incomplete type");
1625 return size_one_node;
1628 /* Convert in case a char is more than one unit. */
1629 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1630 size_int (TYPE_PRECISION (char_type_node)
1634 /* Return either DECL or its known constant value (if it has one). */
1637 decl_constant_value (tree decl)
1639 if (/* Don't change a variable array bound or initial value to a constant
1640 in a place where a variable is invalid. Note that DECL_INITIAL
1641 isn't valid for a PARM_DECL. */
1642 current_function_decl != 0
1643 && TREE_CODE (decl) != PARM_DECL
1644 && !TREE_THIS_VOLATILE (decl)
1645 && TREE_READONLY (decl)
1646 && DECL_INITIAL (decl) != 0
1647 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1648 /* This is invalid if initial value is not constant.
1649 If it has either a function call, a memory reference,
1650 or a variable, then re-evaluating it could give different results. */
1651 && TREE_CONSTANT (DECL_INITIAL (decl))
1652 /* Check for cases where this is sub-optimal, even though valid. */
1653 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1654 return DECL_INITIAL (decl);
1658 /* Convert the array expression EXP to a pointer. */
1660 array_to_pointer_conversion (location_t loc, tree exp)
1662 tree orig_exp = exp;
1663 tree type = TREE_TYPE (exp);
1665 tree restype = TREE_TYPE (type);
1668 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1670 STRIP_TYPE_NOPS (exp);
1672 if (TREE_NO_WARNING (orig_exp))
1673 TREE_NO_WARNING (exp) = 1;
1675 ptrtype = build_pointer_type (restype);
1677 if (TREE_CODE (exp) == INDIRECT_REF)
1678 return convert (ptrtype, TREE_OPERAND (exp, 0));
1680 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1681 return convert (ptrtype, adr);
1684 /* Convert the function expression EXP to a pointer. */
1686 function_to_pointer_conversion (location_t loc, tree exp)
1688 tree orig_exp = exp;
1690 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1692 STRIP_TYPE_NOPS (exp);
1694 if (TREE_NO_WARNING (orig_exp))
1695 TREE_NO_WARNING (exp) = 1;
1697 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1700 /* Perform the default conversion of arrays and functions to pointers.
1701 Return the result of converting EXP. For any other expression, just
1704 LOC is the location of the expression. */
1707 default_function_array_conversion (location_t loc, struct c_expr exp)
1709 tree orig_exp = exp.value;
1710 tree type = TREE_TYPE (exp.value);
1711 enum tree_code code = TREE_CODE (type);
1717 bool not_lvalue = false;
1718 bool lvalue_array_p;
1720 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1721 || CONVERT_EXPR_P (exp.value))
1722 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1724 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1726 exp.value = TREE_OPERAND (exp.value, 0);
1729 if (TREE_NO_WARNING (orig_exp))
1730 TREE_NO_WARNING (exp.value) = 1;
1732 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1733 if (!flag_isoc99 && !lvalue_array_p)
1735 /* Before C99, non-lvalue arrays do not decay to pointers.
1736 Normally, using such an array would be invalid; but it can
1737 be used correctly inside sizeof or as a statement expression.
1738 Thus, do not give an error here; an error will result later. */
1742 exp.value = array_to_pointer_conversion (loc, exp.value);
1746 exp.value = function_to_pointer_conversion (loc, exp.value);
1756 /* EXP is an expression of integer type. Apply the integer promotions
1757 to it and return the promoted value. */
1760 perform_integral_promotions (tree exp)
1762 tree type = TREE_TYPE (exp);
1763 enum tree_code code = TREE_CODE (type);
1765 gcc_assert (INTEGRAL_TYPE_P (type));
1767 /* Normally convert enums to int,
1768 but convert wide enums to something wider. */
1769 if (code == ENUMERAL_TYPE)
1771 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1772 TYPE_PRECISION (integer_type_node)),
1773 ((TYPE_PRECISION (type)
1774 >= TYPE_PRECISION (integer_type_node))
1775 && TYPE_UNSIGNED (type)));
1777 return convert (type, exp);
1780 /* ??? This should no longer be needed now bit-fields have their
1782 if (TREE_CODE (exp) == COMPONENT_REF
1783 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1784 /* If it's thinner than an int, promote it like a
1785 c_promoting_integer_type_p, otherwise leave it alone. */
1786 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1787 TYPE_PRECISION (integer_type_node)))
1788 return convert (integer_type_node, exp);
1790 if (c_promoting_integer_type_p (type))
1792 /* Preserve unsignedness if not really getting any wider. */
1793 if (TYPE_UNSIGNED (type)
1794 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1795 return convert (unsigned_type_node, exp);
1797 return convert (integer_type_node, exp);
1804 /* Perform default promotions for C data used in expressions.
1805 Enumeral types or short or char are converted to int.
1806 In addition, manifest constants symbols are replaced by their values. */
1809 default_conversion (tree exp)
1812 tree type = TREE_TYPE (exp);
1813 enum tree_code code = TREE_CODE (type);
1816 /* Functions and arrays have been converted during parsing. */
1817 gcc_assert (code != FUNCTION_TYPE);
1818 if (code == ARRAY_TYPE)
1821 /* Constants can be used directly unless they're not loadable. */
1822 if (TREE_CODE (exp) == CONST_DECL)
1823 exp = DECL_INITIAL (exp);
1825 /* Strip no-op conversions. */
1827 STRIP_TYPE_NOPS (exp);
1829 if (TREE_NO_WARNING (orig_exp))
1830 TREE_NO_WARNING (exp) = 1;
1832 if (code == VOID_TYPE)
1834 error ("void value not ignored as it ought to be");
1835 return error_mark_node;
1838 exp = require_complete_type (exp);
1839 if (exp == error_mark_node)
1840 return error_mark_node;
1842 promoted_type = targetm.promoted_type (type);
1844 return convert (promoted_type, exp);
1846 if (INTEGRAL_TYPE_P (type))
1847 return perform_integral_promotions (exp);
1852 /* Look up COMPONENT in a structure or union DECL.
1854 If the component name is not found, returns NULL_TREE. Otherwise,
1855 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1856 stepping down the chain to the component, which is in the last
1857 TREE_VALUE of the list. Normally the list is of length one, but if
1858 the component is embedded within (nested) anonymous structures or
1859 unions, the list steps down the chain to the component. */
1862 lookup_field (tree decl, tree component)
1864 tree type = TREE_TYPE (decl);
1867 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1868 to the field elements. Use a binary search on this array to quickly
1869 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1870 will always be set for structures which have many elements. */
1872 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1875 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1877 field = TYPE_FIELDS (type);
1879 top = TYPE_LANG_SPECIFIC (type)->s->len;
1880 while (top - bot > 1)
1882 half = (top - bot + 1) >> 1;
1883 field = field_array[bot+half];
1885 if (DECL_NAME (field) == NULL_TREE)
1887 /* Step through all anon unions in linear fashion. */
1888 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1890 field = field_array[bot++];
1891 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1892 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1894 tree anon = lookup_field (field, component);
1897 return tree_cons (NULL_TREE, field, anon);
1901 /* Entire record is only anon unions. */
1905 /* Restart the binary search, with new lower bound. */
1909 if (DECL_NAME (field) == component)
1911 if (DECL_NAME (field) < component)
1917 if (DECL_NAME (field_array[bot]) == component)
1918 field = field_array[bot];
1919 else if (DECL_NAME (field) != component)
1924 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1926 if (DECL_NAME (field) == NULL_TREE
1927 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1928 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1930 tree anon = lookup_field (field, component);
1933 return tree_cons (NULL_TREE, field, anon);
1936 if (DECL_NAME (field) == component)
1940 if (field == NULL_TREE)
1944 return tree_cons (NULL_TREE, field, NULL_TREE);
1947 /* Make an expression to refer to the COMPONENT field of structure or
1948 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
1949 location of the COMPONENT_REF. */
1952 build_component_ref (location_t loc, tree datum, tree component)
1954 tree type = TREE_TYPE (datum);
1955 enum tree_code code = TREE_CODE (type);
1958 bool datum_lvalue = lvalue_p (datum);
1960 if (!objc_is_public (datum, component))
1961 return error_mark_node;
1963 /* See if there is a field or component with name COMPONENT. */
1965 if (code == RECORD_TYPE || code == UNION_TYPE)
1967 if (!COMPLETE_TYPE_P (type))
1969 c_incomplete_type_error (NULL_TREE, type);
1970 return error_mark_node;
1973 field = lookup_field (datum, component);
1977 error_at (loc, "%qT has no member named %qE", type, component);
1978 return error_mark_node;
1981 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1982 This might be better solved in future the way the C++ front
1983 end does it - by giving the anonymous entities each a
1984 separate name and type, and then have build_component_ref
1985 recursively call itself. We can't do that here. */
1988 tree subdatum = TREE_VALUE (field);
1991 bool use_datum_quals;
1993 if (TREE_TYPE (subdatum) == error_mark_node)
1994 return error_mark_node;
1996 /* If this is an rvalue, it does not have qualifiers in C
1997 standard terms and we must avoid propagating such
1998 qualifiers down to a non-lvalue array that is then
1999 converted to a pointer. */
2000 use_datum_quals = (datum_lvalue
2001 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2003 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2004 if (use_datum_quals)
2005 quals |= TYPE_QUALS (TREE_TYPE (datum));
2006 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2008 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2010 SET_EXPR_LOCATION (ref, loc);
2011 if (TREE_READONLY (subdatum)
2012 || (use_datum_quals && TREE_READONLY (datum)))
2013 TREE_READONLY (ref) = 1;
2014 if (TREE_THIS_VOLATILE (subdatum)
2015 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2016 TREE_THIS_VOLATILE (ref) = 1;
2018 if (TREE_DEPRECATED (subdatum))
2019 warn_deprecated_use (subdatum, NULL_TREE);
2023 field = TREE_CHAIN (field);
2029 else if (code != ERROR_MARK)
2031 "request for member %qE in something not a structure or union",
2034 return error_mark_node;
2037 /* Given an expression PTR for a pointer, return an expression
2038 for the value pointed to.
2039 ERRORSTRING is the name of the operator to appear in error messages.
2041 LOC is the location to use for the generated tree. */
2044 build_indirect_ref (location_t loc, tree ptr, const char *errorstring)
2046 tree pointer = default_conversion (ptr);
2047 tree type = TREE_TYPE (pointer);
2050 if (TREE_CODE (type) == POINTER_TYPE)
2052 if (CONVERT_EXPR_P (pointer)
2053 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2055 /* If a warning is issued, mark it to avoid duplicates from
2056 the backend. This only needs to be done at
2057 warn_strict_aliasing > 2. */
2058 if (warn_strict_aliasing > 2)
2059 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2060 type, TREE_OPERAND (pointer, 0)))
2061 TREE_NO_WARNING (pointer) = 1;
2064 if (TREE_CODE (pointer) == ADDR_EXPR
2065 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2066 == TREE_TYPE (type)))
2068 ref = TREE_OPERAND (pointer, 0);
2069 protected_set_expr_location (ref, loc);
2074 tree t = TREE_TYPE (type);
2076 ref = build1 (INDIRECT_REF, t, pointer);
2078 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2080 error_at (loc, "dereferencing pointer to incomplete type");
2081 return error_mark_node;
2083 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2084 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2086 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2087 so that we get the proper error message if the result is used
2088 to assign to. Also, &* is supposed to be a no-op.
2089 And ANSI C seems to specify that the type of the result
2090 should be the const type. */
2091 /* A de-reference of a pointer to const is not a const. It is valid
2092 to change it via some other pointer. */
2093 TREE_READONLY (ref) = TYPE_READONLY (t);
2094 TREE_SIDE_EFFECTS (ref)
2095 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2096 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2097 protected_set_expr_location (ref, loc);
2101 else if (TREE_CODE (pointer) != ERROR_MARK)
2103 "invalid type argument of %qs (have %qT)", errorstring, type);
2104 return error_mark_node;
2107 /* This handles expressions of the form "a[i]", which denotes
2110 This is logically equivalent in C to *(a+i), but we may do it differently.
2111 If A is a variable or a member, we generate a primitive ARRAY_REF.
2112 This avoids forcing the array out of registers, and can work on
2113 arrays that are not lvalues (for example, members of structures returned
2116 LOC is the location to use for the returned expression. */
2119 build_array_ref (location_t loc, tree array, tree index)
2122 bool swapped = false;
2123 if (TREE_TYPE (array) == error_mark_node
2124 || TREE_TYPE (index) == error_mark_node)
2125 return error_mark_node;
2127 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2128 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2131 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2132 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2134 error_at (loc, "subscripted value is neither array nor pointer");
2135 return error_mark_node;
2143 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2145 error_at (loc, "array subscript is not an integer");
2146 return error_mark_node;
2149 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2151 error_at (loc, "subscripted value is pointer to function");
2152 return error_mark_node;
2155 /* ??? Existing practice has been to warn only when the char
2156 index is syntactically the index, not for char[array]. */
2158 warn_array_subscript_with_type_char (index);
2160 /* Apply default promotions *after* noticing character types. */
2161 index = default_conversion (index);
2163 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2165 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2169 /* An array that is indexed by a non-constant
2170 cannot be stored in a register; we must be able to do
2171 address arithmetic on its address.
2172 Likewise an array of elements of variable size. */
2173 if (TREE_CODE (index) != INTEGER_CST
2174 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2175 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2177 if (!c_mark_addressable (array))
2178 return error_mark_node;
2180 /* An array that is indexed by a constant value which is not within
2181 the array bounds cannot be stored in a register either; because we
2182 would get a crash in store_bit_field/extract_bit_field when trying
2183 to access a non-existent part of the register. */
2184 if (TREE_CODE (index) == INTEGER_CST
2185 && TYPE_DOMAIN (TREE_TYPE (array))
2186 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2188 if (!c_mark_addressable (array))
2189 return error_mark_node;
2195 while (TREE_CODE (foo) == COMPONENT_REF)
2196 foo = TREE_OPERAND (foo, 0);
2197 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2198 pedwarn (loc, OPT_pedantic,
2199 "ISO C forbids subscripting %<register%> array");
2200 else if (!flag_isoc99 && !lvalue_p (foo))
2201 pedwarn (loc, OPT_pedantic,
2202 "ISO C90 forbids subscripting non-lvalue array");
2205 type = TREE_TYPE (TREE_TYPE (array));
2206 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2207 /* Array ref is const/volatile if the array elements are
2208 or if the array is. */
2209 TREE_READONLY (rval)
2210 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2211 | TREE_READONLY (array));
2212 TREE_SIDE_EFFECTS (rval)
2213 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2214 | TREE_SIDE_EFFECTS (array));
2215 TREE_THIS_VOLATILE (rval)
2216 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2217 /* This was added by rms on 16 Nov 91.
2218 It fixes vol struct foo *a; a->elts[1]
2219 in an inline function.
2220 Hope it doesn't break something else. */
2221 | TREE_THIS_VOLATILE (array));
2222 ret = require_complete_type (rval);
2223 protected_set_expr_location (ret, loc);
2228 tree ar = default_conversion (array);
2230 if (ar == error_mark_node)
2233 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2234 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2236 return build_indirect_ref
2237 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2242 /* Build an external reference to identifier ID. FUN indicates
2243 whether this will be used for a function call. LOC is the source
2244 location of the identifier. This sets *TYPE to the type of the
2245 identifier, which is not the same as the type of the returned value
2246 for CONST_DECLs defined as enum constants. If the type of the
2247 identifier is not available, *TYPE is set to NULL. */
2249 build_external_ref (location_t loc, tree id, int fun, tree *type)
2252 tree decl = lookup_name (id);
2254 /* In Objective-C, an instance variable (ivar) may be preferred to
2255 whatever lookup_name() found. */
2256 decl = objc_lookup_ivar (decl, id);
2259 if (decl && decl != error_mark_node)
2262 *type = TREE_TYPE (ref);
2265 /* Implicit function declaration. */
2266 ref = implicitly_declare (loc, id);
2267 else if (decl == error_mark_node)
2268 /* Don't complain about something that's already been
2269 complained about. */
2270 return error_mark_node;
2273 undeclared_variable (loc, id);
2274 return error_mark_node;
2277 if (TREE_TYPE (ref) == error_mark_node)
2278 return error_mark_node;
2280 if (TREE_DEPRECATED (ref))
2281 warn_deprecated_use (ref, NULL_TREE);
2283 /* Recursive call does not count as usage. */
2284 if (ref != current_function_decl)
2286 TREE_USED (ref) = 1;
2289 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2291 if (!in_sizeof && !in_typeof)
2292 C_DECL_USED (ref) = 1;
2293 else if (DECL_INITIAL (ref) == 0
2294 && DECL_EXTERNAL (ref)
2295 && !TREE_PUBLIC (ref))
2296 record_maybe_used_decl (ref);
2299 if (TREE_CODE (ref) == CONST_DECL)
2301 used_types_insert (TREE_TYPE (ref));
2304 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2305 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2307 warning_at (loc, OPT_Wc___compat,
2308 ("enum constant defined in struct or union "
2309 "is not visible in C++"));
2310 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2313 ref = DECL_INITIAL (ref);
2314 TREE_CONSTANT (ref) = 1;
2316 else if (current_function_decl != 0
2317 && !DECL_FILE_SCOPE_P (current_function_decl)
2318 && (TREE_CODE (ref) == VAR_DECL
2319 || TREE_CODE (ref) == PARM_DECL
2320 || TREE_CODE (ref) == FUNCTION_DECL))
2322 tree context = decl_function_context (ref);
2324 if (context != 0 && context != current_function_decl)
2325 DECL_NONLOCAL (ref) = 1;
2327 /* C99 6.7.4p3: An inline definition of a function with external
2328 linkage ... shall not contain a reference to an identifier with
2329 internal linkage. */
2330 else if (current_function_decl != 0
2331 && DECL_DECLARED_INLINE_P (current_function_decl)
2332 && DECL_EXTERNAL (current_function_decl)
2333 && VAR_OR_FUNCTION_DECL_P (ref)
2334 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2335 && ! TREE_PUBLIC (ref)
2336 && DECL_CONTEXT (ref) != current_function_decl)
2337 record_inline_static (loc, current_function_decl, ref,
2343 /* Record details of decls possibly used inside sizeof or typeof. */
2344 struct maybe_used_decl
2348 /* The level seen at (in_sizeof + in_typeof). */
2350 /* The next one at this level or above, or NULL. */
2351 struct maybe_used_decl *next;
2354 static struct maybe_used_decl *maybe_used_decls;
2356 /* Record that DECL, an undefined static function reference seen
2357 inside sizeof or typeof, might be used if the operand of sizeof is
2358 a VLA type or the operand of typeof is a variably modified
2362 record_maybe_used_decl (tree decl)
2364 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2366 t->level = in_sizeof + in_typeof;
2367 t->next = maybe_used_decls;
2368 maybe_used_decls = t;
2371 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2372 USED is false, just discard them. If it is true, mark them used
2373 (if no longer inside sizeof or typeof) or move them to the next
2374 level up (if still inside sizeof or typeof). */
2377 pop_maybe_used (bool used)
2379 struct maybe_used_decl *p = maybe_used_decls;
2380 int cur_level = in_sizeof + in_typeof;
2381 while (p && p->level > cur_level)
2386 C_DECL_USED (p->decl) = 1;
2388 p->level = cur_level;
2392 if (!used || cur_level == 0)
2393 maybe_used_decls = p;
2396 /* Return the result of sizeof applied to EXPR. */
2399 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2402 if (expr.value == error_mark_node)
2404 ret.value = error_mark_node;
2405 ret.original_code = ERROR_MARK;
2406 ret.original_type = NULL;
2407 pop_maybe_used (false);
2411 bool expr_const_operands = true;
2412 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2413 &expr_const_operands);
2414 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2415 ret.original_code = ERROR_MARK;
2416 ret.original_type = NULL;
2417 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2419 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2420 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2421 folded_expr, ret.value);
2422 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2423 SET_EXPR_LOCATION (ret.value, loc);
2425 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2430 /* Return the result of sizeof applied to T, a structure for the type
2431 name passed to sizeof (rather than the type itself). LOC is the
2432 location of the original expression. */
2435 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2439 tree type_expr = NULL_TREE;
2440 bool type_expr_const = true;
2441 type = groktypename (t, &type_expr, &type_expr_const);
2442 ret.value = c_sizeof (loc, type);
2443 ret.original_code = ERROR_MARK;
2444 ret.original_type = NULL;
2445 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2446 && c_vla_type_p (type))
2448 /* If the type is a [*] array, it is a VLA but is represented as
2449 having a size of zero. In such a case we must ensure that
2450 the result of sizeof does not get folded to a constant by
2451 c_fully_fold, because if the size is evaluated the result is
2452 not constant and so constraints on zero or negative size
2453 arrays must not be applied when this sizeof call is inside
2454 another array declarator. */
2456 type_expr = integer_zero_node;
2457 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2458 type_expr, ret.value);
2459 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2461 pop_maybe_used (type != error_mark_node
2462 ? C_TYPE_VARIABLE_SIZE (type) : false);
2466 /* Build a function call to function FUNCTION with parameters PARAMS.
2467 The function call is at LOC.
2468 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2469 TREE_VALUE of each node is a parameter-expression.
2470 FUNCTION's data type may be a function type or a pointer-to-function. */
2473 build_function_call (location_t loc, tree function, tree params)
2478 vec = VEC_alloc (tree, gc, list_length (params));
2479 for (; params; params = TREE_CHAIN (params))
2480 VEC_quick_push (tree, vec, TREE_VALUE (params));
2481 ret = build_function_call_vec (loc, function, vec, NULL);
2482 VEC_free (tree, gc, vec);
2486 /* Build a function call to function FUNCTION with parameters PARAMS.
2487 ORIGTYPES, if not NULL, is a vector of types; each element is
2488 either NULL or the original type of the corresponding element in
2489 PARAMS. The original type may differ from TREE_TYPE of the
2490 parameter for enums. FUNCTION's data type may be a function type
2491 or pointer-to-function. This function changes the elements of
2495 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2496 VEC(tree,gc) *origtypes)
2498 tree fntype, fundecl = 0;
2499 tree name = NULL_TREE, result;
2505 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2506 STRIP_TYPE_NOPS (function);
2508 /* Convert anything with function type to a pointer-to-function. */
2509 if (TREE_CODE (function) == FUNCTION_DECL)
2511 /* Implement type-directed function overloading for builtins.
2512 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2513 handle all the type checking. The result is a complete expression
2514 that implements this function call. */
2515 tem = resolve_overloaded_builtin (loc, function, params);
2519 name = DECL_NAME (function);
2522 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2523 function = function_to_pointer_conversion (loc, function);
2525 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2526 expressions, like those used for ObjC messenger dispatches. */
2527 if (!VEC_empty (tree, params))
2528 function = objc_rewrite_function_call (function,
2529 VEC_index (tree, params, 0));
2531 function = c_fully_fold (function, false, NULL);
2533 fntype = TREE_TYPE (function);
2535 if (TREE_CODE (fntype) == ERROR_MARK)
2536 return error_mark_node;
2538 if (!(TREE_CODE (fntype) == POINTER_TYPE
2539 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2541 error_at (loc, "called object %qE is not a function", function);
2542 return error_mark_node;
2545 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2546 current_function_returns_abnormally = 1;
2548 /* fntype now gets the type of function pointed to. */
2549 fntype = TREE_TYPE (fntype);
2551 /* Convert the parameters to the types declared in the
2552 function prototype, or apply default promotions. */
2554 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2557 return error_mark_node;
2559 /* Check that the function is called through a compatible prototype.
2560 If it is not, replace the call by a trap, wrapped up in a compound
2561 expression if necessary. This has the nice side-effect to prevent
2562 the tree-inliner from generating invalid assignment trees which may
2563 blow up in the RTL expander later. */
2564 if (CONVERT_EXPR_P (function)
2565 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2566 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2567 && !comptypes (fntype, TREE_TYPE (tem)))
2569 tree return_type = TREE_TYPE (fntype);
2570 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2574 /* This situation leads to run-time undefined behavior. We can't,
2575 therefore, simply error unless we can prove that all possible
2576 executions of the program must execute the code. */
2577 if (warning_at (loc, 0, "function called through a non-compatible type"))
2578 /* We can, however, treat "undefined" any way we please.
2579 Call abort to encourage the user to fix the program. */
2580 inform (loc, "if this code is reached, the program will abort");
2581 /* Before the abort, allow the function arguments to exit or
2583 for (i = 0; i < nargs; i++)
2584 trap = build2 (COMPOUND_EXPR, void_type_node,
2585 VEC_index (tree, params, i), trap);
2587 if (VOID_TYPE_P (return_type))
2589 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2591 "function with qualified void return type called");
2598 if (AGGREGATE_TYPE_P (return_type))
2599 rhs = build_compound_literal (loc, return_type,
2600 build_constructor (return_type, 0),
2603 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2605 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2610 argarray = VEC_address (tree, params);
2612 /* Check that arguments to builtin functions match the expectations. */
2614 && DECL_BUILT_IN (fundecl)
2615 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2616 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2617 return error_mark_node;
2619 /* Check that the arguments to the function are valid. */
2620 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2621 TYPE_ARG_TYPES (fntype));
2623 if (name != NULL_TREE
2624 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2626 if (require_constant_value)
2628 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2629 function, nargs, argarray);
2631 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2632 function, nargs, argarray);
2633 if (TREE_CODE (result) == NOP_EXPR
2634 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2635 STRIP_TYPE_NOPS (result);
2638 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2639 function, nargs, argarray);
2641 if (VOID_TYPE_P (TREE_TYPE (result)))
2643 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2645 "function with qualified void return type called");
2648 return require_complete_type (result);
2651 /* Convert the argument expressions in the vector VALUES
2652 to the types in the list TYPELIST.
2654 If TYPELIST is exhausted, or when an element has NULL as its type,
2655 perform the default conversions.
2657 ORIGTYPES is the original types of the expressions in VALUES. This
2658 holds the type of enum values which have been converted to integral
2659 types. It may be NULL.
2661 FUNCTION is a tree for the called function. It is used only for
2662 error messages, where it is formatted with %qE.
2664 This is also where warnings about wrong number of args are generated.
2666 Returns the actual number of arguments processed (which may be less
2667 than the length of VALUES in some error situations), or -1 on
2671 convert_arguments (tree typelist, VEC(tree,gc) *values,
2672 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2675 unsigned int parmnum;
2676 const bool type_generic = fundecl
2677 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2678 bool type_generic_remove_excess_precision = false;
2681 /* Change pointer to function to the function itself for
2683 if (TREE_CODE (function) == ADDR_EXPR
2684 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2685 function = TREE_OPERAND (function, 0);
2687 /* Handle an ObjC selector specially for diagnostics. */
2688 selector = objc_message_selector ();
2690 /* For type-generic built-in functions, determine whether excess
2691 precision should be removed (classification) or not
2694 && DECL_BUILT_IN (fundecl)
2695 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2697 switch (DECL_FUNCTION_CODE (fundecl))
2699 case BUILT_IN_ISFINITE:
2700 case BUILT_IN_ISINF:
2701 case BUILT_IN_ISINF_SIGN:
2702 case BUILT_IN_ISNAN:
2703 case BUILT_IN_ISNORMAL:
2704 case BUILT_IN_FPCLASSIFY:
2705 type_generic_remove_excess_precision = true;
2709 type_generic_remove_excess_precision = false;
2714 /* Scan the given expressions and types, producing individual
2715 converted arguments. */
2717 for (typetail = typelist, parmnum = 0;
2718 VEC_iterate (tree, values, parmnum, val);
2721 tree type = typetail ? TREE_VALUE (typetail) : 0;
2722 tree valtype = TREE_TYPE (val);
2723 tree rname = function;
2724 int argnum = parmnum + 1;
2725 const char *invalid_func_diag;
2726 bool excess_precision = false;
2730 if (type == void_type_node)
2732 error ("too many arguments to function %qE", function);
2736 if (selector && argnum > 2)
2742 npc = null_pointer_constant_p (val);
2744 /* If there is excess precision and a prototype, convert once to
2745 the required type rather than converting via the semantic
2746 type. Likewise without a prototype a float value represented
2747 as long double should be converted once to double. But for
2748 type-generic classification functions excess precision must
2750 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2751 && (type || !type_generic || !type_generic_remove_excess_precision))
2753 val = TREE_OPERAND (val, 0);
2754 excess_precision = true;
2756 val = c_fully_fold (val, false, NULL);
2757 STRIP_TYPE_NOPS (val);
2759 val = require_complete_type (val);
2763 /* Formal parm type is specified by a function prototype. */
2765 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2767 error ("type of formal parameter %d is incomplete", parmnum + 1);
2774 /* Optionally warn about conversions that
2775 differ from the default conversions. */
2776 if (warn_traditional_conversion || warn_traditional)
2778 unsigned int formal_prec = TYPE_PRECISION (type);
2780 if (INTEGRAL_TYPE_P (type)
2781 && TREE_CODE (valtype) == REAL_TYPE)
2782 warning (0, "passing argument %d of %qE as integer "
2783 "rather than floating due to prototype",
2785 if (INTEGRAL_TYPE_P (type)
2786 && TREE_CODE (valtype) == COMPLEX_TYPE)
2787 warning (0, "passing argument %d of %qE as integer "
2788 "rather than complex due to prototype",
2790 else if (TREE_CODE (type) == COMPLEX_TYPE
2791 && TREE_CODE (valtype) == REAL_TYPE)
2792 warning (0, "passing argument %d of %qE as complex "
2793 "rather than floating due to prototype",
2795 else if (TREE_CODE (type) == REAL_TYPE
2796 && INTEGRAL_TYPE_P (valtype))
2797 warning (0, "passing argument %d of %qE as floating "
2798 "rather than integer due to prototype",
2800 else if (TREE_CODE (type) == COMPLEX_TYPE
2801 && INTEGRAL_TYPE_P (valtype))
2802 warning (0, "passing argument %d of %qE as complex "
2803 "rather than integer due to prototype",
2805 else if (TREE_CODE (type) == REAL_TYPE
2806 && TREE_CODE (valtype) == COMPLEX_TYPE)
2807 warning (0, "passing argument %d of %qE as floating "
2808 "rather than complex due to prototype",
2810 /* ??? At some point, messages should be written about
2811 conversions between complex types, but that's too messy
2813 else if (TREE_CODE (type) == REAL_TYPE
2814 && TREE_CODE (valtype) == REAL_TYPE)
2816 /* Warn if any argument is passed as `float',
2817 since without a prototype it would be `double'. */
2818 if (formal_prec == TYPE_PRECISION (float_type_node)
2819 && type != dfloat32_type_node)
2820 warning (0, "passing argument %d of %qE as %<float%> "
2821 "rather than %<double%> due to prototype",
2824 /* Warn if mismatch between argument and prototype
2825 for decimal float types. Warn of conversions with
2826 binary float types and of precision narrowing due to
2828 else if (type != valtype
2829 && (type == dfloat32_type_node
2830 || type == dfloat64_type_node
2831 || type == dfloat128_type_node
2832 || valtype == dfloat32_type_node
2833 || valtype == dfloat64_type_node
2834 || valtype == dfloat128_type_node)
2836 <= TYPE_PRECISION (valtype)
2837 || (type == dfloat128_type_node
2839 != dfloat64_type_node
2841 != dfloat32_type_node)))
2842 || (type == dfloat64_type_node
2844 != dfloat32_type_node))))
2845 warning (0, "passing argument %d of %qE as %qT "
2846 "rather than %qT due to prototype",
2847 argnum, rname, type, valtype);
2850 /* Detect integer changing in width or signedness.
2851 These warnings are only activated with
2852 -Wtraditional-conversion, not with -Wtraditional. */
2853 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2854 && INTEGRAL_TYPE_P (valtype))
2856 tree would_have_been = default_conversion (val);
2857 tree type1 = TREE_TYPE (would_have_been);
2859 if (TREE_CODE (type) == ENUMERAL_TYPE
2860 && (TYPE_MAIN_VARIANT (type)
2861 == TYPE_MAIN_VARIANT (valtype)))
2862 /* No warning if function asks for enum
2863 and the actual arg is that enum type. */
2865 else if (formal_prec != TYPE_PRECISION (type1))
2866 warning (OPT_Wtraditional_conversion,
2867 "passing argument %d of %qE "
2868 "with different width due to prototype",
2870 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2872 /* Don't complain if the formal parameter type
2873 is an enum, because we can't tell now whether
2874 the value was an enum--even the same enum. */
2875 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2877 else if (TREE_CODE (val) == INTEGER_CST
2878 && int_fits_type_p (val, type))
2879 /* Change in signedness doesn't matter
2880 if a constant value is unaffected. */
2882 /* If the value is extended from a narrower
2883 unsigned type, it doesn't matter whether we
2884 pass it as signed or unsigned; the value
2885 certainly is the same either way. */
2886 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2887 && TYPE_UNSIGNED (valtype))
2889 else if (TYPE_UNSIGNED (type))
2890 warning (OPT_Wtraditional_conversion,
2891 "passing argument %d of %qE "
2892 "as unsigned due to prototype",
2895 warning (OPT_Wtraditional_conversion,
2896 "passing argument %d of %qE "
2897 "as signed due to prototype", argnum, rname);
2901 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2902 sake of better warnings from convert_and_check. */
2903 if (excess_precision)
2904 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2905 origtype = (origtypes == NULL
2907 : VEC_index (tree, origtypes, parmnum));
2908 parmval = convert_for_assignment (input_location, type, val,
2909 origtype, ic_argpass, npc,
2913 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2914 && INTEGRAL_TYPE_P (type)
2915 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2916 parmval = default_conversion (parmval);
2919 else if (TREE_CODE (valtype) == REAL_TYPE
2920 && (TYPE_PRECISION (valtype)
2921 < TYPE_PRECISION (double_type_node))
2922 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
2927 /* Convert `float' to `double'. */
2928 parmval = convert (double_type_node, val);
2930 else if (excess_precision && !type_generic)
2931 /* A "double" argument with excess precision being passed
2932 without a prototype or in variable arguments. */
2933 parmval = convert (valtype, val);
2934 else if ((invalid_func_diag =
2935 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2937 error (invalid_func_diag);
2941 /* Convert `short' and `char' to full-size `int'. */
2942 parmval = default_conversion (val);
2944 VEC_replace (tree, values, parmnum, parmval);
2947 typetail = TREE_CHAIN (typetail);
2950 gcc_assert (parmnum == VEC_length (tree, values));
2952 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2954 error ("too few arguments to function %qE", function);
2961 /* This is the entry point used by the parser to build unary operators
2962 in the input. CODE, a tree_code, specifies the unary operator, and
2963 ARG is the operand. For unary plus, the C parser currently uses
2964 CONVERT_EXPR for code.
2966 LOC is the location to use for the tree generated.
2970 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
2972 struct c_expr result;
2974 result.value = build_unary_op (loc, code, arg.value, 0);
2975 result.original_code = code;
2976 result.original_type = NULL;
2978 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
2979 overflow_warning (loc, result.value);
2984 /* This is the entry point used by the parser to build binary operators
2985 in the input. CODE, a tree_code, specifies the binary operator, and
2986 ARG1 and ARG2 are the operands. In addition to constructing the
2987 expression, we check for operands that were written with other binary
2988 operators in a way that is likely to confuse the user.
2990 LOCATION is the location of the binary operator. */
2993 parser_build_binary_op (location_t location, enum tree_code code,
2994 struct c_expr arg1, struct c_expr arg2)
2996 struct c_expr result;
2998 enum tree_code code1 = arg1.original_code;
2999 enum tree_code code2 = arg2.original_code;
3000 tree type1 = (arg1.original_type
3001 ? arg1.original_type
3002 : TREE_TYPE (arg1.value));
3003 tree type2 = (arg2.original_type
3004 ? arg2.original_type
3005 : TREE_TYPE (arg2.value));
3007 result.value = build_binary_op (location, code,
3008 arg1.value, arg2.value, 1);
3009 result.original_code = code;
3010 result.original_type = NULL;
3012 if (TREE_CODE (result.value) == ERROR_MARK)
3015 if (location != UNKNOWN_LOCATION)
3016 protected_set_expr_location (result.value, location);
3018 /* Check for cases such as x+y<<z which users are likely
3020 if (warn_parentheses)
3021 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3023 if (warn_logical_op)
3024 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3025 code1, arg1.value, code2, arg2.value);
3027 /* Warn about comparisons against string literals, with the exception
3028 of testing for equality or inequality of a string literal with NULL. */
3029 if (code == EQ_EXPR || code == NE_EXPR)
3031 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3032 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3033 warning_at (location, OPT_Waddress,
3034 "comparison with string literal results in unspecified behavior");
3036 else if (TREE_CODE_CLASS (code) == tcc_comparison
3037 && (code1 == STRING_CST || code2 == STRING_CST))
3038 warning_at (location, OPT_Waddress,
3039 "comparison with string literal results in unspecified behavior");
3041 if (TREE_OVERFLOW_P (result.value)
3042 && !TREE_OVERFLOW_P (arg1.value)
3043 && !TREE_OVERFLOW_P (arg2.value))
3044 overflow_warning (location, result.value);
3046 /* Warn about comparisons of different enum types. */
3047 if (warn_enum_compare
3048 && TREE_CODE_CLASS (code) == tcc_comparison
3049 && TREE_CODE (type1) == ENUMERAL_TYPE
3050 && TREE_CODE (type2) == ENUMERAL_TYPE
3051 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3052 warning_at (location, OPT_Wenum_compare,
3053 "comparison between %qT and %qT",
3059 /* Return a tree for the difference of pointers OP0 and OP1.
3060 The resulting tree has type int. */
3063 pointer_diff (location_t loc, tree op0, tree op1)
3065 tree restype = ptrdiff_type_node;
3067 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3068 tree con0, con1, lit0, lit1;
3069 tree orig_op1 = op1;
3071 if (TREE_CODE (target_type) == VOID_TYPE)
3072 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3073 "pointer of type %<void *%> used in subtraction");
3074 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3075 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3076 "pointer to a function used in subtraction");
3078 /* If the conversion to ptrdiff_type does anything like widening or
3079 converting a partial to an integral mode, we get a convert_expression
3080 that is in the way to do any simplifications.
3081 (fold-const.c doesn't know that the extra bits won't be needed.
3082 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3083 different mode in place.)
3084 So first try to find a common term here 'by hand'; we want to cover
3085 at least the cases that occur in legal static initializers. */
3086 if (CONVERT_EXPR_P (op0)
3087 && (TYPE_PRECISION (TREE_TYPE (op0))
3088 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3089 con0 = TREE_OPERAND (op0, 0);
3092 if (CONVERT_EXPR_P (op1)
3093 && (TYPE_PRECISION (TREE_TYPE (op1))
3094 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3095 con1 = TREE_OPERAND (op1, 0);
3099 if (TREE_CODE (con0) == PLUS_EXPR)
3101 lit0 = TREE_OPERAND (con0, 1);
3102 con0 = TREE_OPERAND (con0, 0);
3105 lit0 = integer_zero_node;
3107 if (TREE_CODE (con1) == PLUS_EXPR)
3109 lit1 = TREE_OPERAND (con1, 1);
3110 con1 = TREE_OPERAND (con1, 0);
3113 lit1 = integer_zero_node;
3115 if (operand_equal_p (con0, con1, 0))
3122 /* First do the subtraction as integers;
3123 then drop through to build the divide operator.
3124 Do not do default conversions on the minus operator
3125 in case restype is a short type. */
3127 op0 = build_binary_op (loc,
3128 MINUS_EXPR, convert (restype, op0),
3129 convert (restype, op1), 0);
3130 /* This generates an error if op1 is pointer to incomplete type. */
3131 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3132 error_at (loc, "arithmetic on pointer to an incomplete type");
3134 /* This generates an error if op0 is pointer to incomplete type. */
3135 op1 = c_size_in_bytes (target_type);
3137 /* Divide by the size, in easiest possible way. */
3138 return fold_build2_loc (loc, EXACT_DIV_EXPR, restype,
3139 op0, convert (restype, op1));
3142 /* Construct and perhaps optimize a tree representation
3143 for a unary operation. CODE, a tree_code, specifies the operation
3144 and XARG is the operand.
3145 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3146 the default promotions (such as from short to int).
3147 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3148 allows non-lvalues; this is only used to handle conversion of non-lvalue
3149 arrays to pointers in C99.
3151 LOCATION is the location of the operator. */
3154 build_unary_op (location_t location,
3155 enum tree_code code, tree xarg, int flag)
3157 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3160 enum tree_code typecode;
3162 tree ret = error_mark_node;
3163 tree eptype = NULL_TREE;
3164 int noconvert = flag;
3165 const char *invalid_op_diag;
3168 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3170 arg = remove_c_maybe_const_expr (arg);
3172 if (code != ADDR_EXPR)
3173 arg = require_complete_type (arg);
3175 typecode = TREE_CODE (TREE_TYPE (arg));
3176 if (typecode == ERROR_MARK)
3177 return error_mark_node;
3178 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3179 typecode = INTEGER_TYPE;
3181 if ((invalid_op_diag
3182 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3184 error_at (location, invalid_op_diag);
3185 return error_mark_node;
3188 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3190 eptype = TREE_TYPE (arg);
3191 arg = TREE_OPERAND (arg, 0);
3197 /* This is used for unary plus, because a CONVERT_EXPR
3198 is enough to prevent anybody from looking inside for
3199 associativity, but won't generate any code. */
3200 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3201 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3202 || typecode == VECTOR_TYPE))
3204 error_at (location, "wrong type argument to unary plus");
3205 return error_mark_node;
3207 else if (!noconvert)
3208 arg = default_conversion (arg);
3209 arg = non_lvalue_loc (location, arg);
3213 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3214 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3215 || typecode == VECTOR_TYPE))
3217 error_at (location, "wrong type argument to unary minus");
3218 return error_mark_node;
3220 else if (!noconvert)
3221 arg = default_conversion (arg);
3225 /* ~ works on integer types and non float vectors. */
3226 if (typecode == INTEGER_TYPE
3227 || (typecode == VECTOR_TYPE
3228 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3231 arg = default_conversion (arg);
3233 else if (typecode == COMPLEX_TYPE)
3236 pedwarn (location, OPT_pedantic,
3237 "ISO C does not support %<~%> for complex conjugation");
3239 arg = default_conversion (arg);
3243 error_at (location, "wrong type argument to bit-complement");
3244 return error_mark_node;
3249 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3251 error_at (location, "wrong type argument to abs");
3252 return error_mark_node;
3254 else if (!noconvert)
3255 arg = default_conversion (arg);
3259 /* Conjugating a real value is a no-op, but allow it anyway. */
3260 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3261 || typecode == COMPLEX_TYPE))
3263 error_at (location, "wrong type argument to conjugation");
3264 return error_mark_node;
3266 else if (!noconvert)
3267 arg = default_conversion (arg);
3270 case TRUTH_NOT_EXPR:
3271 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3272 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3273 && typecode != COMPLEX_TYPE)
3276 "wrong type argument to unary exclamation mark");
3277 return error_mark_node;
3279 arg = c_objc_common_truthvalue_conversion (location, arg);
3280 ret = invert_truthvalue_loc (location, arg);
3281 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3282 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3283 location = EXPR_LOCATION (ret);
3284 goto return_build_unary_op;
3287 if (TREE_CODE (arg) == COMPLEX_CST)
3288 ret = TREE_REALPART (arg);
3289 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3290 ret = fold_build1_loc (location,
3291 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3294 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3295 eptype = TREE_TYPE (eptype);
3296 goto return_build_unary_op;
3299 if (TREE_CODE (arg) == COMPLEX_CST)
3300 ret = TREE_IMAGPART (arg);
3301 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3302 ret = fold_build1_loc (location,
3303 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3305 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3306 integer_zero_node, arg);
3307 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3308 eptype = TREE_TYPE (eptype);
3309 goto return_build_unary_op;
3311 case PREINCREMENT_EXPR:
3312 case POSTINCREMENT_EXPR:
3313 case PREDECREMENT_EXPR:
3314 case POSTDECREMENT_EXPR:
3316 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3318 tree inner = build_unary_op (location, code,
3319 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3320 if (inner == error_mark_node)
3321 return error_mark_node;
3322 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3323 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3324 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3325 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3326 goto return_build_unary_op;
3329 /* Complain about anything that is not a true lvalue. */
3330 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3331 || code == POSTINCREMENT_EXPR)
3334 return error_mark_node;
3336 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3338 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3339 warning_at (location, OPT_Wc___compat,
3340 "increment of enumeration value is invalid in C++");
3342 warning_at (location, OPT_Wc___compat,
3343 "decrement of enumeration value is invalid in C++");
3346 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3347 arg = c_fully_fold (arg, false, NULL);
3349 /* Increment or decrement the real part of the value,
3350 and don't change the imaginary part. */
3351 if (typecode == COMPLEX_TYPE)
3355 pedwarn (location, OPT_pedantic,
3356 "ISO C does not support %<++%> and %<--%> on complex types");
3358 arg = stabilize_reference (arg);
3359 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3360 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3361 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3362 if (real == error_mark_node || imag == error_mark_node)
3363 return error_mark_node;
3364 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3366 goto return_build_unary_op;
3369 /* Report invalid types. */
3371 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3372 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3374 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3375 error_at (location, "wrong type argument to increment");
3377 error_at (location, "wrong type argument to decrement");
3379 return error_mark_node;
3385 argtype = TREE_TYPE (arg);
3387 /* Compute the increment. */
3389 if (typecode == POINTER_TYPE)
3391 /* If pointer target is an undefined struct,
3392 we just cannot know how to do the arithmetic. */
3393 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3395 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3397 "increment of pointer to unknown structure");
3400 "decrement of pointer to unknown structure");
3402 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3403 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3405 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3406 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3407 "wrong type argument to increment");
3409 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3410 "wrong type argument to decrement");
3413 inc = c_size_in_bytes (TREE_TYPE (argtype));
3414 inc = fold_convert_loc (location, sizetype, inc);
3416 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3418 /* For signed fract types, we invert ++ to -- or
3419 -- to ++, and change inc from 1 to -1, because
3420 it is not possible to represent 1 in signed fract constants.
3421 For unsigned fract types, the result always overflows and
3422 we get an undefined (original) or the maximum value. */