| /* Build expressions with type checking for C++ compiler. |
| Copyright (C) 1987-2022 Free Software Foundation, Inc. |
| Hacked by Michael Tiemann (tiemann@cygnus.com) |
| |
| This file is part of GCC. |
| |
| GCC is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation; either version 3, or (at your option) |
| any later version. |
| |
| GCC is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with GCC; see the file COPYING3. If not see |
| <http://www.gnu.org/licenses/>. */ |
| |
| |
| /* This file is part of the C++ front end. |
| It contains routines to build C++ expressions given their operands, |
| including computing the types of the result, C and C++ specific error |
| checks, and some optimization. */ |
| |
| #include "config.h" |
| #include "system.h" |
| #include "coretypes.h" |
| #include "target.h" |
| #include "cp-tree.h" |
| #include "stor-layout.h" |
| #include "varasm.h" |
| #include "intl.h" |
| #include "convert.h" |
| #include "c-family/c-objc.h" |
| #include "c-family/c-ubsan.h" |
| #include "gcc-rich-location.h" |
| #include "stringpool.h" |
| #include "attribs.h" |
| #include "asan.h" |
| #include "gimplify.h" |
| |
| static tree cp_build_addr_expr_strict (tree, tsubst_flags_t); |
| static tree cp_build_function_call (tree, tree, tsubst_flags_t); |
| static tree pfn_from_ptrmemfunc (tree); |
| static tree delta_from_ptrmemfunc (tree); |
| static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int, |
| tsubst_flags_t, int); |
| static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree, |
| tsubst_flags_t); |
| static tree rationalize_conditional_expr (enum tree_code, tree, |
| tsubst_flags_t); |
| static bool comp_ptr_ttypes_real (tree, tree, int); |
| static bool comp_except_types (tree, tree, bool); |
| static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool); |
| static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *); |
| static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t); |
| static void casts_away_constness_r (tree *, tree *, tsubst_flags_t); |
| static bool casts_away_constness (tree, tree, tsubst_flags_t); |
| static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION); |
| static void error_args_num (location_t, tree, bool); |
| static int convert_arguments (tree, vec<tree, va_gc> **, tree, int, |
| tsubst_flags_t); |
| static bool is_std_move_p (tree); |
| static bool is_std_forward_p (tree); |
| |
| /* Do `exp = require_complete_type (exp);' to make sure exp |
| does not have an incomplete type. (That includes void types.) |
| Returns error_mark_node if the VALUE does not have |
| complete type when this function returns. */ |
| |
| tree |
| require_complete_type_sfinae (tree value, tsubst_flags_t complain) |
| { |
| tree type; |
| |
| if (processing_template_decl || value == error_mark_node) |
| return value; |
| |
| if (TREE_CODE (value) == OVERLOAD) |
| type = unknown_type_node; |
| else |
| type = TREE_TYPE (value); |
| |
| if (type == error_mark_node) |
| return error_mark_node; |
| |
| /* First, detect a valid value with a complete type. */ |
| if (COMPLETE_TYPE_P (type)) |
| return value; |
| |
| if (complete_type_or_maybe_complain (type, value, complain)) |
| return value; |
| else |
| return error_mark_node; |
| } |
| |
| tree |
| require_complete_type (tree value) |
| { |
| return require_complete_type_sfinae (value, tf_warning_or_error); |
| } |
| |
| /* Try to complete TYPE, if it is incomplete. For example, if TYPE is |
| a template instantiation, do the instantiation. Returns TYPE, |
| whether or not it could be completed, unless something goes |
| horribly wrong, in which case the error_mark_node is returned. */ |
| |
| tree |
| complete_type (tree type) |
| { |
| if (type == NULL_TREE) |
| /* Rather than crash, we return something sure to cause an error |
| at some point. */ |
| return error_mark_node; |
| |
| if (type == error_mark_node || COMPLETE_TYPE_P (type)) |
| ; |
| else if (TREE_CODE (type) == ARRAY_TYPE) |
| { |
| tree t = complete_type (TREE_TYPE (type)); |
| unsigned int needs_constructing, has_nontrivial_dtor; |
| if (COMPLETE_TYPE_P (t) && !dependent_type_p (type)) |
| layout_type (type); |
| needs_constructing |
| = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t)); |
| has_nontrivial_dtor |
| = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t)); |
| for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
| { |
| TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing; |
| TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor; |
| } |
| } |
| else if (CLASS_TYPE_P (type)) |
| { |
| if (modules_p ()) |
| /* TYPE could be a class member we've not loaded the definition of. */ |
| lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type))); |
| |
| if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
| instantiate_class_template (TYPE_MAIN_VARIANT (type)); |
| } |
| |
| return type; |
| } |
| |
| /* Like complete_type, but issue an error if the TYPE cannot be completed. |
| VALUE is used for informative diagnostics. |
| Returns NULL_TREE if the type cannot be made complete. */ |
| |
| tree |
| complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain) |
| { |
| type = complete_type (type); |
| if (type == error_mark_node) |
| /* We already issued an error. */ |
| return NULL_TREE; |
| else if (!COMPLETE_TYPE_P (type)) |
| { |
| if (complain & tf_error) |
| cxx_incomplete_type_diagnostic (value, type, DK_ERROR); |
| note_failed_type_completion_for_satisfaction (type); |
| return NULL_TREE; |
| } |
| else |
| return type; |
| } |
| |
| tree |
| complete_type_or_else (tree type, tree value) |
| { |
| return complete_type_or_maybe_complain (type, value, tf_warning_or_error); |
| } |
| |
| |
| /* Return the common type of two parameter lists. |
| We assume that comptypes has already been done and returned 1; |
| if that isn't so, this may crash. |
| |
| As an optimization, free the space we allocate if the parameter |
| lists are already common. */ |
| |
| static tree |
| commonparms (tree p1, tree p2) |
| { |
| tree oldargs = p1, newargs, n; |
| int i, len; |
| int any_change = 0; |
| |
| len = list_length (p1); |
| newargs = tree_last (p1); |
| |
| if (newargs == void_list_node) |
| i = 1; |
| else |
| { |
| i = 0; |
| newargs = 0; |
| } |
| |
| for (; i < len; i++) |
| newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
| |
| n = newargs; |
| |
| for (i = 0; p1; |
| p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++) |
| { |
| if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2)) |
| { |
| TREE_PURPOSE (n) = TREE_PURPOSE (p1); |
| any_change = 1; |
| } |
| else if (! TREE_PURPOSE (p1)) |
| { |
| if (TREE_PURPOSE (p2)) |
| { |
| TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
| any_change = 1; |
| } |
| } |
| else |
| { |
| if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1) |
| any_change = 1; |
| TREE_PURPOSE (n) = TREE_PURPOSE (p2); |
| } |
| if (TREE_VALUE (p1) != TREE_VALUE (p2)) |
| { |
| any_change = 1; |
| TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2)); |
| } |
| else |
| TREE_VALUE (n) = TREE_VALUE (p1); |
| } |
| if (! any_change) |
| return oldargs; |
| |
| return newargs; |
| } |
| |
| /* Given a type, perhaps copied for a typedef, |
| find the "original" version of it. */ |
| static tree |
| original_type (tree t) |
| { |
| int quals = cp_type_quals (t); |
| while (t != error_mark_node |
| && TYPE_NAME (t) != NULL_TREE) |
| { |
| tree x = TYPE_NAME (t); |
| if (TREE_CODE (x) != TYPE_DECL) |
| break; |
| x = DECL_ORIGINAL_TYPE (x); |
| if (x == NULL_TREE) |
| break; |
| t = x; |
| } |
| return cp_build_qualified_type (t, quals); |
| } |
| |
| /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE |
| and return a variant of TYPE with the merged attributes. */ |
| |
| static tree |
| merge_type_attributes_from (tree type, tree other_type) |
| { |
| tree attrs = targetm.merge_type_attributes (type, other_type); |
| attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type)); |
| return cp_build_type_attribute_variant (type, attrs); |
| } |
| |
| /* Return the common type for two arithmetic types T1 and T2 under the |
| usual arithmetic conversions. The default conversions have already |
| been applied, and enumerated types converted to their compatible |
| integer types. */ |
| |
| static tree |
| cp_common_type (tree t1, tree t2) |
| { |
| enum tree_code code1 = TREE_CODE (t1); |
| enum tree_code code2 = TREE_CODE (t2); |
| tree attributes; |
| int i; |
| |
| |
| /* In what follows, we slightly generalize the rules given in [expr] so |
| as to deal with `long long' and `complex'. First, merge the |
| attributes. */ |
| attributes = (*targetm.merge_type_attributes) (t1, t2); |
| |
| if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2)) |
| { |
| if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
| return build_type_attribute_variant (t1, attributes); |
| else |
| return NULL_TREE; |
| } |
| |
| /* FIXME: Attributes. */ |
| gcc_assert (ARITHMETIC_TYPE_P (t1) |
| || VECTOR_TYPE_P (t1) |
| || UNSCOPED_ENUM_P (t1)); |
| gcc_assert (ARITHMETIC_TYPE_P (t2) |
| || VECTOR_TYPE_P (t2) |
| || UNSCOPED_ENUM_P (t2)); |
| |
| /* If one type is complex, form the common type of the non-complex |
| components, then make that complex. Use T1 or T2 if it is the |
| required type. */ |
| if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) |
| { |
| tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; |
| tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; |
| tree subtype |
| = type_after_usual_arithmetic_conversions (subtype1, subtype2); |
| |
| if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) |
| return build_type_attribute_variant (t1, attributes); |
| else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) |
| return build_type_attribute_variant (t2, attributes); |
| else |
| return build_type_attribute_variant (build_complex_type (subtype), |
| attributes); |
| } |
| |
| if (code1 == VECTOR_TYPE) |
| { |
| /* When we get here we should have two vectors of the same size. |
| Just prefer the unsigned one if present. */ |
| if (TYPE_UNSIGNED (t1)) |
| return merge_type_attributes_from (t1, t2); |
| else |
| return merge_type_attributes_from (t2, t1); |
| } |
| |
| /* If only one is real, use it as the result. */ |
| if (code1 == REAL_TYPE && code2 != REAL_TYPE) |
| return build_type_attribute_variant (t1, attributes); |
| if (code2 == REAL_TYPE && code1 != REAL_TYPE) |
| return build_type_attribute_variant (t2, attributes); |
| |
| /* Both real or both integers; use the one with greater precision. */ |
| if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) |
| return build_type_attribute_variant (t1, attributes); |
| else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) |
| return build_type_attribute_variant (t2, attributes); |
| |
| /* The types are the same; no need to do anything fancy. */ |
| if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
| return build_type_attribute_variant (t1, attributes); |
| |
| if (code1 != REAL_TYPE) |
| { |
| /* If one is unsigned long long, then convert the other to unsigned |
| long long. */ |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node)) |
| return build_type_attribute_variant (long_long_unsigned_type_node, |
| attributes); |
| /* If one is a long long, and the other is an unsigned long, and |
| long long can represent all the values of an unsigned long, then |
| convert to a long long. Otherwise, convert to an unsigned long |
| long. Otherwise, if either operand is long long, convert the |
| other to long long. |
| |
| Since we're here, we know the TYPE_PRECISION is the same; |
| therefore converting to long long cannot represent all the values |
| of an unsigned long, so we choose unsigned long long in that |
| case. */ |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node)) |
| { |
| tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
| ? long_long_unsigned_type_node |
| : long_long_integer_type_node); |
| return build_type_attribute_variant (t, attributes); |
| } |
| |
| /* Go through the same procedure, but for longs. */ |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node)) |
| return build_type_attribute_variant (long_unsigned_type_node, |
| attributes); |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node)) |
| { |
| tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
| ? long_unsigned_type_node : long_integer_type_node); |
| return build_type_attribute_variant (t, attributes); |
| } |
| |
| /* For __intN types, either the type is __int128 (and is lower |
| priority than the types checked above, but higher than other |
| 128-bit types) or it's known to not be the same size as other |
| types (enforced in toplev.cc). Prefer the unsigned type. */ |
| for (i = 0; i < NUM_INT_N_ENTS; i ++) |
| { |
| if (int_n_enabled_p [i] |
| && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type) |
| || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type))) |
| { |
| tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) |
| ? int_n_trees[i].unsigned_type |
| : int_n_trees[i].signed_type); |
| return build_type_attribute_variant (t, attributes); |
| } |
| } |
| |
| /* Otherwise prefer the unsigned one. */ |
| if (TYPE_UNSIGNED (t1)) |
| return build_type_attribute_variant (t1, attributes); |
| else |
| return build_type_attribute_variant (t2, attributes); |
| } |
| else |
| { |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node)) |
| return build_type_attribute_variant (long_double_type_node, |
| attributes); |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node)) |
| return build_type_attribute_variant (double_type_node, |
| attributes); |
| if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node) |
| || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node)) |
| return build_type_attribute_variant (float_type_node, |
| attributes); |
| |
| /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of |
| the standard C++ floating-point types. Logic earlier in this |
| function has already eliminated the possibility that |
| TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no |
| compelling reason to choose one or the other. */ |
| return build_type_attribute_variant (t1, attributes); |
| } |
| } |
| |
| /* T1 and T2 are arithmetic or enumeration types. Return the type |
| that will result from the "usual arithmetic conversions" on T1 and |
| T2 as described in [expr]. */ |
| |
| tree |
| type_after_usual_arithmetic_conversions (tree t1, tree t2) |
| { |
| gcc_assert (ARITHMETIC_TYPE_P (t1) |
| || VECTOR_TYPE_P (t1) |
| || UNSCOPED_ENUM_P (t1)); |
| gcc_assert (ARITHMETIC_TYPE_P (t2) |
| || VECTOR_TYPE_P (t2) |
| || UNSCOPED_ENUM_P (t2)); |
| |
| /* Perform the integral promotions. We do not promote real types here. */ |
| if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1) |
| && INTEGRAL_OR_ENUMERATION_TYPE_P (t2)) |
| { |
| t1 = type_promotes_to (t1); |
| t2 = type_promotes_to (t2); |
| } |
| |
| return cp_common_type (t1, t2); |
| } |
| |
| static void |
| composite_pointer_error (const op_location_t &location, |
| diagnostic_t kind, tree t1, tree t2, |
| composite_pointer_operation operation) |
| { |
| switch (operation) |
| { |
| case CPO_COMPARISON: |
| emit_diagnostic (kind, location, 0, |
| "comparison between " |
| "distinct pointer types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| case CPO_CONVERSION: |
| emit_diagnostic (kind, location, 0, |
| "conversion between " |
| "distinct pointer types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| case CPO_CONDITIONAL_EXPR: |
| emit_diagnostic (kind, location, 0, |
| "conditional expression between " |
| "distinct pointer types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| default: |
| gcc_unreachable (); |
| } |
| } |
| |
| /* Subroutine of composite_pointer_type to implement the recursive |
| case. See that function for documentation of the parameters. And ADD_CONST |
| is used to track adding "const" where needed. */ |
| |
| static tree |
| composite_pointer_type_r (const op_location_t &location, |
| tree t1, tree t2, bool *add_const, |
| composite_pointer_operation operation, |
| tsubst_flags_t complain) |
| { |
| tree pointee1; |
| tree pointee2; |
| tree result_type; |
| tree attributes; |
| |
| /* Determine the types pointed to by T1 and T2. */ |
| if (TYPE_PTR_P (t1)) |
| { |
| pointee1 = TREE_TYPE (t1); |
| pointee2 = TREE_TYPE (t2); |
| } |
| else |
| { |
| pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1); |
| pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2); |
| } |
| |
| /* [expr.type] |
| |
| If T1 and T2 are similar types, the result is the cv-combined type of |
| T1 and T2. */ |
| if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2)) |
| result_type = pointee1; |
| else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2)) |
| || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2))) |
| { |
| result_type = composite_pointer_type_r (location, pointee1, pointee2, |
| add_const, operation, complain); |
| if (result_type == error_mark_node) |
| return error_mark_node; |
| } |
| else |
| { |
| if (complain & tf_error) |
| composite_pointer_error (location, DK_PERMERROR, |
| t1, t2, operation); |
| else |
| return error_mark_node; |
| result_type = void_type_node; |
| } |
| const int q1 = cp_type_quals (pointee1); |
| const int q2 = cp_type_quals (pointee2); |
| const int quals = q1 | q2; |
| result_type = cp_build_qualified_type (result_type, |
| (quals | (*add_const |
| ? TYPE_QUAL_CONST |
| : TYPE_UNQUALIFIED))); |
| /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for |
| the TLQ). The reason is that both T1 and T2 can then be converted to the |
| cv-combined type of T1 and T2. */ |
| if (quals != q1 || quals != q2) |
| *add_const = true; |
| /* If the original types were pointers to members, so is the |
| result. */ |
| if (TYPE_PTRMEM_P (t1)) |
| { |
| if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
| TYPE_PTRMEM_CLASS_TYPE (t2))) |
| { |
| if (complain & tf_error) |
| composite_pointer_error (location, DK_PERMERROR, |
| t1, t2, operation); |
| else |
| return error_mark_node; |
| } |
| result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
| result_type); |
| } |
| else |
| result_type = build_pointer_type (result_type); |
| |
| /* Merge the attributes. */ |
| attributes = (*targetm.merge_type_attributes) (t1, t2); |
| return build_type_attribute_variant (result_type, attributes); |
| } |
| |
| /* Return the composite pointer type (see [expr.type]) for T1 and T2. |
| ARG1 and ARG2 are the values with those types. The OPERATION is to |
| describe the operation between the pointer types, |
| in case an error occurs. |
| |
| This routine also implements the computation of a common type for |
| pointers-to-members as per [expr.eq]. */ |
| |
| tree |
| composite_pointer_type (const op_location_t &location, |
| tree t1, tree t2, tree arg1, tree arg2, |
| composite_pointer_operation operation, |
| tsubst_flags_t complain) |
| { |
| tree class1; |
| tree class2; |
| |
| /* [expr.type] |
| |
| If one operand is a null pointer constant, the composite pointer |
| type is the type of the other operand. */ |
| if (null_ptr_cst_p (arg1)) |
| return t2; |
| if (null_ptr_cst_p (arg2)) |
| return t1; |
| |
| /* We have: |
| |
| [expr.type] |
| |
| If one of the operands has type "pointer to cv1 void", then |
| the other has type "pointer to cv2 T", and the composite pointer |
| type is "pointer to cv12 void", where cv12 is the union of cv1 |
| and cv2. |
| |
| If either type is a pointer to void, make sure it is T1. */ |
| if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2))) |
| std::swap (t1, t2); |
| |
| /* Now, if T1 is a pointer to void, merge the qualifiers. */ |
| if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1))) |
| { |
| tree attributes; |
| tree result_type; |
| |
| if (TYPE_PTRFN_P (t2)) |
| { |
| if (complain & tf_error) |
| { |
| switch (operation) |
| { |
| case CPO_COMPARISON: |
| pedwarn (location, OPT_Wpedantic, |
| "ISO C++ forbids comparison between pointer " |
| "of type %<void *%> and pointer-to-function"); |
| break; |
| case CPO_CONVERSION: |
| pedwarn (location, OPT_Wpedantic, |
| "ISO C++ forbids conversion between pointer " |
| "of type %<void *%> and pointer-to-function"); |
| break; |
| case CPO_CONDITIONAL_EXPR: |
| pedwarn (location, OPT_Wpedantic, |
| "ISO C++ forbids conditional expression between " |
| "pointer of type %<void *%> and " |
| "pointer-to-function"); |
| break; |
| default: |
| gcc_unreachable (); |
| } |
| } |
| else |
| return error_mark_node; |
| } |
| result_type |
| = cp_build_qualified_type (void_type_node, |
| (cp_type_quals (TREE_TYPE (t1)) |
| | cp_type_quals (TREE_TYPE (t2)))); |
| result_type = build_pointer_type (result_type); |
| /* Merge the attributes. */ |
| attributes = (*targetm.merge_type_attributes) (t1, t2); |
| return build_type_attribute_variant (result_type, attributes); |
| } |
| |
| if (c_dialect_objc () && TYPE_PTR_P (t1) |
| && TYPE_PTR_P (t2)) |
| { |
| if (objc_have_common_type (t1, t2, -3, NULL_TREE)) |
| return objc_common_type (t1, t2); |
| } |
| |
| /* if T1 or T2 is "pointer to noexcept function" and the other type is |
| "pointer to function", where the function types are otherwise the same, |
| "pointer to function" */ |
| if (fnptr_conv_p (t1, t2)) |
| return t1; |
| if (fnptr_conv_p (t2, t1)) |
| return t2; |
| |
| /* [expr.eq] permits the application of a pointer conversion to |
| bring the pointers to a common type. */ |
| if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2) |
| && CLASS_TYPE_P (TREE_TYPE (t1)) |
| && CLASS_TYPE_P (TREE_TYPE (t2)) |
| && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1), |
| TREE_TYPE (t2))) |
| { |
| class1 = TREE_TYPE (t1); |
| class2 = TREE_TYPE (t2); |
| |
| if (DERIVED_FROM_P (class1, class2)) |
| t2 = (build_pointer_type |
| (cp_build_qualified_type (class1, cp_type_quals (class2)))); |
| else if (DERIVED_FROM_P (class2, class1)) |
| t1 = (build_pointer_type |
| (cp_build_qualified_type (class2, cp_type_quals (class1)))); |
| else |
| { |
| if (complain & tf_error) |
| composite_pointer_error (location, DK_ERROR, t1, t2, operation); |
| return error_mark_node; |
| } |
| } |
| /* [expr.eq] permits the application of a pointer-to-member |
| conversion to change the class type of one of the types. */ |
| else if (TYPE_PTRMEM_P (t1) |
| && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1), |
| TYPE_PTRMEM_CLASS_TYPE (t2))) |
| { |
| class1 = TYPE_PTRMEM_CLASS_TYPE (t1); |
| class2 = TYPE_PTRMEM_CLASS_TYPE (t2); |
| |
| if (DERIVED_FROM_P (class1, class2)) |
| t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1)); |
| else if (DERIVED_FROM_P (class2, class1)) |
| t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
| else |
| { |
| if (complain & tf_error) |
| switch (operation) |
| { |
| case CPO_COMPARISON: |
| error_at (location, "comparison between distinct " |
| "pointer-to-member types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| case CPO_CONVERSION: |
| error_at (location, "conversion between distinct " |
| "pointer-to-member types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| case CPO_CONDITIONAL_EXPR: |
| error_at (location, "conditional expression between distinct " |
| "pointer-to-member types %qT and %qT lacks a cast", |
| t1, t2); |
| break; |
| default: |
| gcc_unreachable (); |
| } |
| return error_mark_node; |
| } |
| } |
| |
| bool add_const = false; |
| return composite_pointer_type_r (location, t1, t2, &add_const, operation, |
| complain); |
| } |
| |
| /* Return the merged type of two types. |
| We assume that comptypes has already been done and returned 1; |
| if that isn't so, this may crash. |
| |
| This just combines attributes and default arguments; any other |
| differences would cause the two types to compare unalike. */ |
| |
| tree |
| merge_types (tree t1, tree t2) |
| { |
| enum tree_code code1; |
| enum tree_code code2; |
| tree attributes; |
| |
| /* Save time if the two types are the same. */ |
| if (t1 == t2) |
| return t1; |
| if (original_type (t1) == original_type (t2)) |
| return t1; |
| |
| /* If one type is nonsense, use the other. */ |
| if (t1 == error_mark_node) |
| return t2; |
| if (t2 == error_mark_node) |
| return t1; |
| |
| /* Handle merging an auto redeclaration with a previous deduced |
| return type. */ |
| if (is_auto (t1)) |
| return t2; |
| |
| /* Merge the attributes. */ |
| attributes = (*targetm.merge_type_attributes) (t1, t2); |
| |
| if (TYPE_PTRMEMFUNC_P (t1)) |
| t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
| if (TYPE_PTRMEMFUNC_P (t2)) |
| t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
| |
| code1 = TREE_CODE (t1); |
| code2 = TREE_CODE (t2); |
| if (code1 != code2) |
| { |
| gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE); |
| if (code1 == TYPENAME_TYPE) |
| { |
| t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
| code1 = TREE_CODE (t1); |
| } |
| else |
| { |
| t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
| code2 = TREE_CODE (t2); |
| } |
| } |
| |
| switch (code1) |
| { |
| case POINTER_TYPE: |
| case REFERENCE_TYPE: |
| /* For two pointers, do this recursively on the target type. */ |
| { |
| tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
| int quals = cp_type_quals (t1); |
| |
| if (code1 == POINTER_TYPE) |
| { |
| t1 = build_pointer_type (target); |
| if (TREE_CODE (target) == METHOD_TYPE) |
| t1 = build_ptrmemfunc_type (t1); |
| } |
| else |
| t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1)); |
| t1 = build_type_attribute_variant (t1, attributes); |
| t1 = cp_build_qualified_type (t1, quals); |
| |
| return t1; |
| } |
| |
| case OFFSET_TYPE: |
| { |
| int quals; |
| tree pointee; |
| quals = cp_type_quals (t1); |
| pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1), |
| TYPE_PTRMEM_POINTED_TO_TYPE (t2)); |
| t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1), |
| pointee); |
| t1 = cp_build_qualified_type (t1, quals); |
| break; |
| } |
| |
| case ARRAY_TYPE: |
| { |
| tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
| /* Save space: see if the result is identical to one of the args. */ |
| if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) |
| return build_type_attribute_variant (t1, attributes); |
| if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) |
| return build_type_attribute_variant (t2, attributes); |
| /* Merge the element types, and have a size if either arg has one. */ |
| t1 = build_cplus_array_type |
| (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); |
| break; |
| } |
| |
| case FUNCTION_TYPE: |
| /* Function types: prefer the one that specified arg types. |
| If both do, merge the arg types. Also merge the return types. */ |
| { |
| tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2)); |
| tree p1 = TYPE_ARG_TYPES (t1); |
| tree p2 = TYPE_ARG_TYPES (t2); |
| tree parms; |
| |
| /* Save space: see if the result is identical to one of the args. */ |
| if (valtype == TREE_TYPE (t1) && ! p2) |
| return cp_build_type_attribute_variant (t1, attributes); |
| if (valtype == TREE_TYPE (t2) && ! p1) |
| return cp_build_type_attribute_variant (t2, attributes); |
| |
| /* Simple way if one arg fails to specify argument types. */ |
| if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node) |
| parms = p2; |
| else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node) |
| parms = p1; |
| else |
| parms = commonparms (p1, p2); |
| |
| cp_cv_quals quals = type_memfn_quals (t1); |
| cp_ref_qualifier rqual = type_memfn_rqual (t1); |
| gcc_assert (quals == type_memfn_quals (t2)); |
| gcc_assert (rqual == type_memfn_rqual (t2)); |
| |
| tree rval = build_function_type (valtype, parms); |
| rval = apply_memfn_quals (rval, quals); |
| tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
| TYPE_RAISES_EXCEPTIONS (t2)); |
| bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
| t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p); |
| break; |
| } |
| |
| case METHOD_TYPE: |
| { |
| /* Get this value the long way, since TYPE_METHOD_BASETYPE |
| is just the main variant of this. */ |
| tree basetype = class_of_this_parm (t2); |
| tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1), |
| TYPE_RAISES_EXCEPTIONS (t2)); |
| cp_ref_qualifier rqual = type_memfn_rqual (t1); |
| tree t3; |
| bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1); |
| |
| /* If this was a member function type, get back to the |
| original type of type member function (i.e., without |
| the class instance variable up front. */ |
| t1 = build_function_type (TREE_TYPE (t1), |
| TREE_CHAIN (TYPE_ARG_TYPES (t1))); |
| t2 = build_function_type (TREE_TYPE (t2), |
| TREE_CHAIN (TYPE_ARG_TYPES (t2))); |
| t3 = merge_types (t1, t2); |
| t3 = build_method_type_directly (basetype, TREE_TYPE (t3), |
| TYPE_ARG_TYPES (t3)); |
| t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p); |
| break; |
| } |
| |
| case TYPENAME_TYPE: |
| /* There is no need to merge attributes into a TYPENAME_TYPE. |
| When the type is instantiated it will have whatever |
| attributes result from the instantiation. */ |
| return t1; |
| |
| default:; |
| if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) |
| return t1; |
| else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) |
| return t2; |
| break; |
| } |
| |
| return cp_build_type_attribute_variant (t1, attributes); |
| } |
| |
| /* Return the ARRAY_TYPE type without its domain. */ |
| |
| tree |
| strip_array_domain (tree type) |
| { |
| tree t2; |
| gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
| if (TYPE_DOMAIN (type) == NULL_TREE) |
| return type; |
| t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
| return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type)); |
| } |
| |
| /* Wrapper around cp_common_type that is used by c-common.cc and other |
| front end optimizations that remove promotions. |
| |
| Return the common type for two arithmetic types T1 and T2 under the |
| usual arithmetic conversions. The default conversions have already |
| been applied, and enumerated types converted to their compatible |
| integer types. */ |
| |
| tree |
| common_type (tree t1, tree t2) |
| { |
| /* If one type is nonsense, use the other */ |
| if (t1 == error_mark_node) |
| return t2; |
| if (t2 == error_mark_node) |
| return t1; |
| |
| return cp_common_type (t1, t2); |
| } |
| |
| /* Return the common type of two pointer types T1 and T2. This is the |
| type for the result of most arithmetic operations if the operands |
| have the given two types. |
| |
| We assume that comp_target_types has already been done and returned |
| nonzero; if that isn't so, this may crash. */ |
| |
| tree |
| common_pointer_type (tree t1, tree t2) |
| { |
| gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2)) |
| || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2)) |
| || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2))); |
| |
| return composite_pointer_type (input_location, t1, t2, |
| error_mark_node, error_mark_node, |
| CPO_CONVERSION, tf_warning_or_error); |
| } |
| |
| /* Compare two exception specifier types for exactness or subsetness, if |
| allowed. Returns false for mismatch, true for match (same, or |
| derived and !exact). |
| |
| [except.spec] "If a class X ... objects of class X or any class publicly |
| and unambiguously derived from X. Similarly, if a pointer type Y * ... |
| exceptions of type Y * or that are pointers to any type publicly and |
| unambiguously derived from Y. Otherwise a function only allows exceptions |
| that have the same type ..." |
| This does not mention cv qualifiers and is different to what throw |
| [except.throw] and catch [except.catch] will do. They will ignore the |
| top level cv qualifiers, and allow qualifiers in the pointer to class |
| example. |
| |
| We implement the letter of the standard. */ |
| |
| static bool |
| comp_except_types (tree a, tree b, bool exact) |
| { |
| if (same_type_p (a, b)) |
| return true; |
| else if (!exact) |
| { |
| if (cp_type_quals (a) || cp_type_quals (b)) |
| return false; |
| |
| if (TYPE_PTR_P (a) && TYPE_PTR_P (b)) |
| { |
| a = TREE_TYPE (a); |
| b = TREE_TYPE (b); |
| if (cp_type_quals (a) || cp_type_quals (b)) |
| return false; |
| } |
| |
| if (TREE_CODE (a) != RECORD_TYPE |
| || TREE_CODE (b) != RECORD_TYPE) |
| return false; |
| |
| if (publicly_uniquely_derived_p (a, b)) |
| return true; |
| } |
| return false; |
| } |
| |
| /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers. |
| If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5). |
| If EXACT is ce_type, the C++17 type compatibility rules apply. |
| If EXACT is ce_normal, the compatibility rules in 15.4/3 apply. |
| If EXACT is ce_exact, the specs must be exactly the same. Exception lists |
| are unordered, but we've already filtered out duplicates. Most lists will |
| be in order, we should try to make use of that. */ |
| |
| bool |
| comp_except_specs (const_tree t1, const_tree t2, int exact) |
| { |
| const_tree probe; |
| const_tree base; |
| int length = 0; |
| |
| if (t1 == t2) |
| return true; |
| |
| /* First handle noexcept. */ |
| if (exact < ce_exact) |
| { |
| if (exact == ce_type |
| && (canonical_eh_spec (CONST_CAST_TREE (t1)) |
| == canonical_eh_spec (CONST_CAST_TREE (t2)))) |
| return true; |
| |
| /* noexcept(false) is compatible with no exception-specification, |
| and less strict than any spec. */ |
| if (t1 == noexcept_false_spec) |
| return t2 == NULL_TREE || exact == ce_derived; |
| /* Even a derived noexcept(false) is compatible with no |
| exception-specification. */ |
| if (t2 == noexcept_false_spec) |
| return t1 == NULL_TREE; |
| |
| /* Otherwise, if we aren't looking for an exact match, noexcept is |
| equivalent to throw(). */ |
| if (t1 == noexcept_true_spec) |
| t1 = empty_except_spec; |
| if (t2 == noexcept_true_spec) |
| t2 = empty_except_spec; |
| } |
| |
| /* If any noexcept is left, it is only comparable to itself; |
| either we're looking for an exact match or we're redeclaring a |
| template with dependent noexcept. */ |
| if ((t1 && TREE_PURPOSE (t1)) |
| || (t2 && TREE_PURPOSE (t2))) |
| return (t1 && t2 |
| && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))); |
| |
| if (t1 == NULL_TREE) /* T1 is ... */ |
| return t2 == NULL_TREE || exact == ce_derived; |
| if (!TREE_VALUE (t1)) /* t1 is EMPTY */ |
| return t2 != NULL_TREE && !TREE_VALUE (t2); |
| if (t2 == NULL_TREE) /* T2 is ... */ |
| return false; |
| if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */ |
| return exact == ce_derived; |
| |
| /* Neither set is ... or EMPTY, make sure each part of T2 is in T1. |
| Count how many we find, to determine exactness. For exact matching and |
| ordered T1, T2, this is an O(n) operation, otherwise its worst case is |
| O(nm). */ |
| for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2)) |
| { |
| for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe)) |
| { |
| tree a = TREE_VALUE (probe); |
| tree b = TREE_VALUE (t2); |
| |
| if (comp_except_types (a, b, exact)) |
| { |
| if (probe == base && exact > ce_derived) |
| base = TREE_CHAIN (probe); |
| length++; |
| break; |
| } |
| } |
| if (probe == NULL_TREE) |
| return false; |
| } |
| return exact == ce_derived || base == NULL_TREE || length == list_length (t1); |
| } |
| |
| /* Compare the array types T1 and T2. CB says how we should behave when |
| comparing array bounds: bounds_none doesn't allow dimensionless arrays, |
| bounds_either says than any array can be [], bounds_first means that |
| onlt T1 can be an array with unknown bounds. STRICT is true if |
| qualifiers must match when comparing the types of the array elements. */ |
| |
| static bool |
| comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb, |
| bool strict) |
| { |
| tree d1; |
| tree d2; |
| tree max1, max2; |
| |
| if (t1 == t2) |
| return true; |
| |
| /* The type of the array elements must be the same. */ |
| if (strict |
| ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) |
| : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| |
| d1 = TYPE_DOMAIN (t1); |
| d2 = TYPE_DOMAIN (t2); |
| |
| if (d1 == d2) |
| return true; |
| |
| /* If one of the arrays is dimensionless, and the other has a |
| dimension, they are of different types. However, it is valid to |
| write: |
| |
| extern int a[]; |
| int a[3]; |
| |
| by [basic.link]: |
| |
| declarations for an array object can specify |
| array types that differ by the presence or absence of a major |
| array bound (_dcl.array_). */ |
| if (!d1 && d2) |
| return cb >= bounds_either; |
| else if (d1 && !d2) |
| return cb == bounds_either; |
| |
| /* Check that the dimensions are the same. */ |
| |
| if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))) |
| return false; |
| max1 = TYPE_MAX_VALUE (d1); |
| max2 = TYPE_MAX_VALUE (d2); |
| |
| if (!cp_tree_equal (max1, max2)) |
| return false; |
| |
| return true; |
| } |
| |
| /* Compare the relative position of T1 and T2 into their respective |
| template parameter list. |
| T1 and T2 must be template parameter types. |
| Return TRUE if T1 and T2 have the same position, FALSE otherwise. */ |
| |
| static bool |
| comp_template_parms_position (tree t1, tree t2) |
| { |
| tree index1, index2; |
| gcc_assert (t1 && t2 |
| && TREE_CODE (t1) == TREE_CODE (t2) |
| && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM |
| || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM |
| || TREE_CODE (t1) == TEMPLATE_TYPE_PARM)); |
| |
| index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1)); |
| index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2)); |
| |
| /* Then compare their relative position. */ |
| if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2) |
| || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2) |
| || (TEMPLATE_PARM_PARAMETER_PACK (index1) |
| != TEMPLATE_PARM_PARAMETER_PACK (index2))) |
| return false; |
| |
| /* In C++14 we can end up comparing 'auto' to a normal template |
| parameter. Don't confuse them. */ |
| if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2))) |
| return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2); |
| |
| return true; |
| } |
| |
| /* Heuristic check if two parameter types can be considered ABI-equivalent. */ |
| |
| static bool |
| cxx_safe_arg_type_equiv_p (tree t1, tree t2) |
| { |
| t1 = TYPE_MAIN_VARIANT (t1); |
| t2 = TYPE_MAIN_VARIANT (t2); |
| |
| if (TYPE_PTR_P (t1) |
| && TYPE_PTR_P (t2)) |
| return true; |
| |
| /* The signedness of the parameter matters only when an integral |
| type smaller than int is promoted to int, otherwise only the |
| precision of the parameter matters. |
| This check should make sure that the callee does not see |
| undefined values in argument registers. */ |
| if (INTEGRAL_TYPE_P (t1) |
| && INTEGRAL_TYPE_P (t2) |
| && TYPE_PRECISION (t1) == TYPE_PRECISION (t2) |
| && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2) |
| || !targetm.calls.promote_prototypes (NULL_TREE) |
| || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node))) |
| return true; |
| |
| return same_type_p (t1, t2); |
| } |
| |
| /* Check if a type cast between two function types can be considered safe. */ |
| |
| static bool |
| cxx_safe_function_type_cast_p (tree t1, tree t2) |
| { |
| if (TREE_TYPE (t1) == void_type_node && |
| TYPE_ARG_TYPES (t1) == void_list_node) |
| return true; |
| |
| if (TREE_TYPE (t2) == void_type_node && |
| TYPE_ARG_TYPES (t2) == void_list_node) |
| return true; |
| |
| if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| |
| for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2); |
| t1 && t2; |
| t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
| if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
| return false; |
| |
| return true; |
| } |
| |
| /* Subroutine in comptypes. */ |
| |
| static bool |
| structural_comptypes (tree t1, tree t2, int strict) |
| { |
| /* Both should be types that are not obviously the same. */ |
| gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2)); |
| |
| /* Suppress typename resolution under spec_hasher::equal in place of calling |
| push_to_top_level there. */ |
| if (!comparing_specializations) |
| { |
| /* TYPENAME_TYPEs should be resolved if the qualifying scope is the |
| current instantiation. */ |
| if (TREE_CODE (t1) == TYPENAME_TYPE) |
| t1 = resolve_typename_type (t1, /*only_current_p=*/true); |
| |
| if (TREE_CODE (t2) == TYPENAME_TYPE) |
| t2 = resolve_typename_type (t2, /*only_current_p=*/true); |
| } |
| |
| if (TYPE_PTRMEMFUNC_P (t1)) |
| t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1); |
| if (TYPE_PTRMEMFUNC_P (t2)) |
| t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2); |
| |
| /* Different classes of types can't be compatible. */ |
| if (TREE_CODE (t1) != TREE_CODE (t2)) |
| return false; |
| |
| /* Qualifiers must match. For array types, we will check when we |
| recur on the array element types. */ |
| if (TREE_CODE (t1) != ARRAY_TYPE |
| && cp_type_quals (t1) != cp_type_quals (t2)) |
| return false; |
| if (TREE_CODE (t1) == FUNCTION_TYPE |
| && type_memfn_quals (t1) != type_memfn_quals (t2)) |
| return false; |
| /* Need to check this before TYPE_MAIN_VARIANT. |
| FIXME function qualifiers should really change the main variant. */ |
| if (FUNC_OR_METHOD_TYPE_P (t1)) |
| { |
| if (type_memfn_rqual (t1) != type_memfn_rqual (t2)) |
| return false; |
| if (flag_noexcept_type |
| && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1), |
| TYPE_RAISES_EXCEPTIONS (t2), |
| ce_type)) |
| return false; |
| } |
| |
| /* Allow for two different type nodes which have essentially the same |
| definition. Note that we already checked for equality of the type |
| qualifiers (just above). */ |
| if (TREE_CODE (t1) != ARRAY_TYPE |
| && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) |
| goto check_alias; |
| |
| /* Compare the types. Return false on known not-same. Break on not |
| known. Never return true from this switch -- you'll break |
| specialization comparison. */ |
| switch (TREE_CODE (t1)) |
| { |
| case VOID_TYPE: |
| case BOOLEAN_TYPE: |
| /* All void and bool types are the same. */ |
| break; |
| |
| case OPAQUE_TYPE: |
| case INTEGER_TYPE: |
| case FIXED_POINT_TYPE: |
| case REAL_TYPE: |
| /* With these nodes, we can't determine type equivalence by |
| looking at what is stored in the nodes themselves, because |
| two nodes might have different TYPE_MAIN_VARIANTs but still |
| represent the same type. For example, wchar_t and int could |
| have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE, |
| TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs |
| and are distinct types. On the other hand, int and the |
| following typedef |
| |
| typedef int INT __attribute((may_alias)); |
| |
| have identical properties, different TYPE_MAIN_VARIANTs, but |
| represent the same type. The canonical type system keeps |
| track of equivalence in this case, so we fall back on it. */ |
| if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
| return false; |
| |
| /* We don't need or want the attribute comparison. */ |
| goto check_alias; |
| |
| case TEMPLATE_TEMPLATE_PARM: |
| case BOUND_TEMPLATE_TEMPLATE_PARM: |
| if (!comp_template_parms_position (t1, t2)) |
| return false; |
| if (!comp_template_parms |
| (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)), |
| DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2)))) |
| return false; |
| if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM) |
| break; |
| /* Don't check inheritance. */ |
| strict = COMPARE_STRICT; |
| /* Fall through. */ |
| |
| case RECORD_TYPE: |
| case UNION_TYPE: |
| if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2) |
| && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2) |
| || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2))) |
| break; |
| |
| if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2)) |
| break; |
| else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1)) |
| break; |
| |
| return false; |
| |
| case OFFSET_TYPE: |
| if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2), |
| strict & ~COMPARE_REDECLARATION)) |
| return false; |
| if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| break; |
| |
| case REFERENCE_TYPE: |
| if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2)) |
| return false; |
| /* fall through to checks for pointer types */ |
| gcc_fallthrough (); |
| |
| case POINTER_TYPE: |
| if (TYPE_MODE (t1) != TYPE_MODE (t2) |
| || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| break; |
| |
| case METHOD_TYPE: |
| case FUNCTION_TYPE: |
| /* Exception specs and memfn_rquals were checked above. */ |
| if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))) |
| return false; |
| break; |
| |
| case ARRAY_TYPE: |
| /* Target types must match incl. qualifiers. */ |
| if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION) |
| ? bounds_either : bounds_none), |
| /*strict=*/true)) |
| return false; |
| break; |
| |
| case TEMPLATE_TYPE_PARM: |
| /* If T1 and T2 don't have the same relative position in their |
| template parameters set, they can't be equal. */ |
| if (!comp_template_parms_position (t1, t2)) |
| return false; |
| /* If T1 and T2 don't represent the same class template deduction, |
| they aren't equal. */ |
| if (CLASS_PLACEHOLDER_TEMPLATE (t1) |
| != CLASS_PLACEHOLDER_TEMPLATE (t2)) |
| return false; |
| /* Constrained 'auto's are distinct from parms that don't have the same |
| constraints. */ |
| if (!equivalent_placeholder_constraints (t1, t2)) |
| return false; |
| break; |
| |
| case TYPENAME_TYPE: |
| if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1), |
| TYPENAME_TYPE_FULLNAME (t2))) |
| return false; |
| /* Qualifiers don't matter on scopes. */ |
| if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1), |
| TYPE_CONTEXT (t2))) |
| return false; |
| break; |
| |
| case UNBOUND_CLASS_TEMPLATE: |
| if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2))) |
| return false; |
| if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))) |
| return false; |
| break; |
| |
| case COMPLEX_TYPE: |
| if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| break; |
| |
| case VECTOR_TYPE: |
| if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2) |
| || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2)) |
| || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
| return false; |
| break; |
| |
| case TYPE_PACK_EXPANSION: |
| return (same_type_p (PACK_EXPANSION_PATTERN (t1), |
| PACK_EXPANSION_PATTERN (t2)) |
| && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), |
| PACK_EXPANSION_EXTRA_ARGS (t2))); |
| |
| case DECLTYPE_TYPE: |
| if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1) |
| != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)) |
| return false; |
| if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2)) |
| return false; |
| if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2)) |
| return false; |
| if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2))) |
| return false; |
| break; |
| |
| case UNDERLYING_TYPE: |
| if (!same_type_p (UNDERLYING_TYPE_TYPE (t1), UNDERLYING_TYPE_TYPE (t2))) |
| return false; |
| break; |
| |
| case TYPEOF_TYPE: |
| if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2))) |
| return false; |
| break; |
| |
| default: |
| return false; |
| } |
| |
| /* If we get here, we know that from a target independent POV the |
| types are the same. Make sure the target attributes are also |
| the same. */ |
| if (!comp_type_attributes (t1, t2)) |
| return false; |
| |
| check_alias: |
| if (comparing_dependent_aliases) |
| { |
| /* Don't treat an alias template specialization with dependent |
| arguments as equivalent to its underlying type when used as a |
| template argument; we need them to be distinct so that we |
| substitute into the specialization arguments at instantiation |
| time. And aliases can't be equivalent without being ==, so |
| we don't need to look any deeper. */ |
| tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent); |
| tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent); |
| if ((dep1 || dep2) && dep1 != dep2) |
| return false; |
| } |
| |
| return true; |
| } |
| |
| /* Return true if T1 and T2 are related as allowed by STRICT. STRICT |
| is a bitwise-or of the COMPARE_* flags. */ |
| |
| bool |
| comptypes (tree t1, tree t2, int strict) |
| { |
| gcc_checking_assert (t1 && t2); |
| |
| /* TYPE_ARGUMENT_PACKS are not really types. */ |
| gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK |
| && TREE_CODE (t2) != TYPE_ARGUMENT_PACK); |
| |
| if (t1 == t2) |
| return true; |
| |
| /* Suppress errors caused by previously reported errors. */ |
| if (t1 == error_mark_node || t2 == error_mark_node) |
| return false; |
| |
| if (strict == COMPARE_STRICT) |
| { |
| if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2)) |
| /* At least one of the types requires structural equality, so |
| perform a deep check. */ |
| return structural_comptypes (t1, t2, strict); |
| |
| if (flag_checking && param_use_canonical_types) |
| { |
| bool result = structural_comptypes (t1, t2, strict); |
| |
| if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2)) |
| /* The two types are structurally equivalent, but their |
| canonical types were different. This is a failure of the |
| canonical type propagation code.*/ |
| internal_error |
| ("canonical types differ for identical types %qT and %qT", |
| t1, t2); |
| else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)) |
| /* Two types are structurally different, but the canonical |
| types are the same. This means we were over-eager in |
| assigning canonical types. */ |
| internal_error |
| ("same canonical type node for different types %qT and %qT", |
| t1, t2); |
| |
| return result; |
| } |
| if (!flag_checking && param_use_canonical_types) |
| return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2); |
| else |
| return structural_comptypes (t1, t2, strict); |
| } |
| else if (strict == COMPARE_STRUCTURAL) |
| return structural_comptypes (t1, t2, COMPARE_STRICT); |
| else |
| return structural_comptypes (t1, t2, strict); |
| } |
| |
| /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring |
| top-level qualifiers. */ |
| |
| bool |
| same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2) |
| { |
| if (type1 == error_mark_node || type2 == error_mark_node) |
| return false; |
| if (type1 == type2) |
| return true; |
| |
| type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
| type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); |
| return same_type_p (type1, type2); |
| } |
| |
| /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */ |
| |
| bool |
| similar_type_p (tree type1, tree type2) |
| { |
| if (type1 == error_mark_node || type2 == error_mark_node) |
| return false; |
| |
| /* Informally, two types are similar if, ignoring top-level cv-qualification: |
| * they are the same type; or |
| * they are both pointers, and the pointed-to types are similar; or |
| * they are both pointers to member of the same class, and the types of |
| the pointed-to members are similar; or |
| * they are both arrays of the same size or both arrays of unknown bound, |
| and the array element types are similar. */ |
| |
| if (same_type_ignoring_top_level_qualifiers_p (type1, type2)) |
| return true; |
| |
| if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) |
| || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) |
| || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE)) |
| return comp_ptr_ttypes_const (type1, type2, bounds_either); |
| |
| return false; |
| } |
| |
| /* Helper function for layout_compatible_type_p and |
| is_corresponding_member_aggr. Advance to next members (NULL if |
| no further ones) and return true if those members are still part of |
| the common initial sequence. */ |
| |
| bool |
| next_common_initial_seqence (tree &memb1, tree &memb2) |
| { |
| while (memb1) |
| { |
| if (TREE_CODE (memb1) != FIELD_DECL |
| || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1))) |
| { |
| memb1 = DECL_CHAIN (memb1); |
| continue; |
| } |
| if (DECL_FIELD_IS_BASE (memb1)) |
| { |
| memb1 = TYPE_FIELDS (TREE_TYPE (memb1)); |
| continue; |
| } |
| break; |
| } |
| while (memb2) |
| { |
| if (TREE_CODE (memb2) != FIELD_DECL |
| || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2))) |
| { |
| memb2 = DECL_CHAIN (memb2); |
| continue; |
| } |
| if (DECL_FIELD_IS_BASE (memb2)) |
| { |
| memb2 = TYPE_FIELDS (TREE_TYPE (memb2)); |
| continue; |
| } |
| break; |
| } |
| if (memb1 == NULL_TREE && memb2 == NULL_TREE) |
| return true; |
| if (memb1 == NULL_TREE || memb2 == NULL_TREE) |
| return false; |
| if (DECL_BIT_FIELD_TYPE (memb1)) |
| { |
| if (!DECL_BIT_FIELD_TYPE (memb2)) |
| return false; |
| if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1), |
| DECL_BIT_FIELD_TYPE (memb2))) |
| return false; |
| if (TYPE_PRECISION (TREE_TYPE (memb1)) |
| != TYPE_PRECISION (TREE_TYPE (memb2))) |
| return false; |
| } |
| else if (DECL_BIT_FIELD_TYPE (memb2)) |
| return false; |
| else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2))) |
| return false; |
| if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1))) |
| != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2))) |
| return false; |
| if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2))) |
| return false; |
| return true; |
| } |
| |
| /* Return true if TYPE1 and TYPE2 are layout-compatible types. */ |
| |
| bool |
| layout_compatible_type_p (tree type1, tree type2) |
| { |
| if (type1 == error_mark_node || type2 == error_mark_node) |
| return false; |
| if (type1 == type2) |
| return true; |
| if (TREE_CODE (type1) != TREE_CODE (type2)) |
| return false; |
| |
| type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED); |
| type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED); |
| |
| if (TREE_CODE (type1) == ENUMERAL_TYPE) |
| return (TYPE_ALIGN (type1) == TYPE_ALIGN (type2) |
| && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) |
| && same_type_p (finish_underlying_type (type1), |
| finish_underlying_type (type2))); |
| |
| if (CLASS_TYPE_P (type1) |
| && std_layout_type_p (type1) |
| && std_layout_type_p (type2) |
| && TYPE_ALIGN (type1) == TYPE_ALIGN (type2) |
| && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))) |
| { |
| tree field1 = TYPE_FIELDS (type1); |
| tree field2 = TYPE_FIELDS (type2); |
| if (TREE_CODE (type1) == RECORD_TYPE) |
| { |
| while (1) |
| { |
| if (!next_common_initial_seqence (field1, field2)) |
| return false; |
| if (field1 == NULL_TREE) |
| return true; |
| field1 = DECL_CHAIN (field1); |
| field2 = DECL_CHAIN (field2); |
| } |
| } |
| /* Otherwise both types must be union types. |
| The standard says: |
| "Two standard-layout unions are layout-compatible if they have |
| the same number of non-static data members and corresponding |
| non-static data members (in any order) have layout-compatible |
| types." |
| but the code anticipates that bitfield vs. non-bitfield, |
| different bitfield widths or presence/absence of |
| [[no_unique_address]] should be checked as well. */ |
| auto_vec<tree, 16> vec; |
| unsigned int count = 0; |
| for (; field1; field1 = DECL_CHAIN (field1)) |
| if (TREE_CODE (field1) == FIELD_DECL) |
| count++; |
| for (; field2; field2 = DECL_CHAIN (field2)) |
| if (TREE_CODE (field2) == FIELD_DECL) |
| vec.safe_push (field2); |
| /* Discussions on core lean towards treating multiple union fields |
| of the same type as the same field, so this might need changing |
| in the future. */ |
| if (count != vec.length ()) |
| return false; |
| for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1)) |
| { |
| if (TREE_CODE (field1) != FIELD_DECL) |
| continue; |
| unsigned int j; |
| tree t1 = DECL_BIT_FIELD_TYPE (field1); |
| if (t1 == NULL_TREE) |
| t1 = TREE_TYPE (field1); |
| FOR_EACH_VEC_ELT (vec, j, field2) |
| { |
| tree t2 = DECL_BIT_FIELD_TYPE (field2); |
| if (t2 == NULL_TREE) |
| t2 = TREE_TYPE (field2); |
| if (DECL_BIT_FIELD_TYPE (field1)) |
| { |
| if (!DECL_BIT_FIELD_TYPE (field2)) |
| continue; |
| if (TYPE_PRECISION (TREE_TYPE (field1)) |
| != TYPE_PRECISION (TREE_TYPE (field2))) |
| continue; |
| } |
| else if (DECL_BIT_FIELD_TYPE (field2)) |
| continue; |
| if (!layout_compatible_type_p (t1, t2)) |
| continue; |
| if ((!lookup_attribute ("no_unique_address", |
| DECL_ATTRIBUTES (field1))) |
| != !lookup_attribute ("no_unique_address", |
| DECL_ATTRIBUTES (field2))) |
| continue; |
| break; |
| } |
| if (j == vec.length ()) |
| return false; |
| vec.unordered_remove (j); |
| } |
| return true; |
| } |
| |
| return same_type_p (type1, type2); |
| } |
| |
| /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */ |
| |
| bool |
| at_least_as_qualified_p (const_tree type1, const_tree type2) |
| { |
| int q1 = cp_type_quals (type1); |
| int q2 = cp_type_quals (type2); |
| |
| /* All qualifiers for TYPE2 must also appear in TYPE1. */ |
| return (q1 & q2) == q2; |
| } |
| |
| /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is |
| more cv-qualified that TYPE1, and 0 otherwise. */ |
| |
| int |
| comp_cv_qualification (int q1, int q2) |
| { |
| if (q1 == q2) |
| return 0; |
| |
| if ((q1 & q2) == q2) |
| return 1; |
| else if ((q1 & q2) == q1) |
| return -1; |
| |
| return 0; |
| } |
| |
| int |
| comp_cv_qualification (const_tree type1, const_tree type2) |
| { |
| int q1 = cp_type_quals (type1); |
| int q2 = cp_type_quals (type2); |
| return comp_cv_qualification (q1, q2); |
| } |
| |
| /* Returns 1 if the cv-qualification signature of TYPE1 is a proper |
| subset of the cv-qualification signature of TYPE2, and the types |
| are similar. Returns -1 if the other way 'round, and 0 otherwise. */ |
| |
| int |
| comp_cv_qual_signature (tree type1, tree type2) |
| { |
| if (comp_ptr_ttypes_real (type2, type1, -1)) |
| return 1; |
| else if (comp_ptr_ttypes_real (type1, type2, -1)) |
| return -1; |
| else |
| return 0; |
| } |
| |
| /* Subroutines of `comptypes'. */ |
| |
| /* Return true if two parameter type lists PARMS1 and PARMS2 are |
| equivalent in the sense that functions with those parameter types |
| can have equivalent types. The two lists must be equivalent, |
| element by element. */ |
| |
| bool |
| compparms (const_tree parms1, const_tree parms2) |
| { |
| const_tree t1, t2; |
| |
| /* An unspecified parmlist matches any specified parmlist |
| whose argument types don't need default promotions. */ |
| |
| for (t1 = parms1, t2 = parms2; |
| t1 || t2; |
| t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
| { |
| /* If one parmlist is shorter than the other, |
| they fail to match. */ |
| if (!t1 || !t2) |
| return false; |
| if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
| return false; |
| } |
| return true; |
| } |
| |
| |
| /* Process a sizeof or alignof expression where the operand is a type. |
| STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment) |
| or GNU (preferred alignment) semantics; it is ignored if OP is |
| SIZEOF_EXPR. */ |
| |
| tree |
| cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op, |
| bool std_alignof, bool complain) |
| { |
| gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
| if (type == error_mark_node) |
| return error_mark_node; |
| |
| type = non_reference (type); |
| if (TREE_CODE (type) == METHOD_TYPE) |
| { |
| if (complain) |
| { |
| pedwarn (loc, OPT_Wpointer_arith, |
| "invalid application of %qs to a member function", |
| OVL_OP_INFO (false, op)->name); |
| return size_one_node; |
| } |
| else |
| return error_mark_node; |
| } |
| else if (VOID_TYPE_P (type) && std_alignof) |
| { |
| if (complain) |
| error_at (loc, "invalid application of %qs to a void type", |
| OVL_OP_INFO (false, op)->name); |
| return error_mark_node; |
| } |
| |
| bool dependent_p = dependent_type_p (type); |
| if (!dependent_p) |
| complete_type (type); |
| if (dependent_p |
| /* VLA types will have a non-constant size. In the body of an |
| uninstantiated template, we don't need to try to compute the |
| value, because the sizeof expression is not an integral |
| constant expression in that case. And, if we do try to |
| compute the value, we'll likely end up with SAVE_EXPRs, which |
| the template substitution machinery does not expect to see. */ |
| || (processing_template_decl |
| && COMPLETE_TYPE_P (type) |
| && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)) |
| { |
| tree value = build_min (op, size_type_node, type); |
| TREE_READONLY (value) = 1; |
| if (op == ALIGNOF_EXPR && std_alignof) |
| ALIGNOF_EXPR_STD_P (value) = true; |
| SET_EXPR_LOCATION (value, loc); |
| return value; |
| } |
| |
| return c_sizeof_or_alignof_type (loc, complete_type (type), |
| op == SIZEOF_EXPR, std_alignof, |
| complain); |
| } |
| |
| /* Return the size of the type, without producing any warnings for |
| types whose size cannot be taken. This routine should be used only |
| in some other routine that has already produced a diagnostic about |
| using the size of such a type. */ |
| tree |
| cxx_sizeof_nowarn (tree type) |
| { |
| if (TREE_CODE (type) == FUNCTION_TYPE |
| || VOID_TYPE_P (type) |
| || TREE_CODE (type) == ERROR_MARK) |
| return size_one_node; |
| else if (!COMPLETE_TYPE_P (type)) |
| return size_zero_node; |
| else |
| return cxx_sizeof_or_alignof_type (input_location, type, |
| SIZEOF_EXPR, false, false); |
| } |
| |
| /* Process a sizeof expression where the operand is an expression. */ |
| |
| static tree |
| cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain) |
| { |
| if (e == error_mark_node) |
| return error_mark_node; |
| |
| if (instantiation_dependent_uneval_expression_p (e)) |
| { |
| e = build_min (SIZEOF_EXPR, size_type_node, e); |
| TREE_SIDE_EFFECTS (e) = 0; |
| TREE_READONLY (e) = 1; |
| SET_EXPR_LOCATION (e, loc); |
| |
| return e; |
| } |
| |
| location_t e_loc = cp_expr_loc_or_loc (e, loc); |
| STRIP_ANY_LOCATION_WRAPPER (e); |
| |
| /* To get the size of a static data member declared as an array of |
| unknown bound, we need to instantiate it. */ |
| if (VAR_P (e) |
| && VAR_HAD_UNKNOWN_BOUND (e) |
| && DECL_TEMPLATE_INSTANTIATION (e)) |
| instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false); |
| |
| if (TREE_CODE (e) == PARM_DECL |
| && DECL_ARRAY_PARAMETER_P (e) |
| && (complain & tf_warning)) |
| { |
| auto_diagnostic_group d; |
| if (warning_at (e_loc, OPT_Wsizeof_array_argument, |
| "%<sizeof%> on array function parameter %qE " |
| "will return size of %qT", e, TREE_TYPE (e))) |
| inform (DECL_SOURCE_LOCATION (e), "declared here"); |
| } |
| |
| e = mark_type_use (e); |
| |
| if (bitfield_p (e)) |
| { |
| if (complain & tf_error) |
| error_at (e_loc, |
| "invalid application of %<sizeof%> to a bit-field"); |
| else |
| return error_mark_node; |
| e = char_type_node; |
| } |
| else if (is_overloaded_fn (e)) |
| { |
| if (complain & tf_error) |
| permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to " |
| "an expression of function type"); |
| else |
| return error_mark_node; |
| e = char_type_node; |
| } |
| else if (type_unknown_p (e)) |
| { |
| if (complain & tf_error) |
| cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e)); |
| else |
| return error_mark_node; |
| e = char_type_node; |
| } |
| else |
| e = TREE_TYPE (e); |
| |
| return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false, |
| complain & tf_error); |
| } |
| |
| /* Implement the __alignof keyword: Return the minimum required |
| alignment of E, measured in bytes. For VAR_DECL's and |
| FIELD_DECL's return DECL_ALIGN (which can be set from an |
| "aligned" __attribute__ specification). STD_ALIGNOF acts |
| like in cxx_sizeof_or_alignof_type. */ |
| |
| static tree |
| cxx_alignof_expr (location_t loc, tree e, bool std_alignof, |
| tsubst_flags_t complain) |
| { |
| tree t; |
| |
| if (e == error_mark_node) |
| return error_mark_node; |
| |
| if (processing_template_decl) |
| { |
| e = build_min (ALIGNOF_EXPR, size_type_node, e); |
| TREE_SIDE_EFFECTS (e) = 0; |
| TREE_READONLY (e) = 1; |
| SET_EXPR_LOCATION (e, loc); |
| ALIGNOF_EXPR_STD_P (e) = std_alignof; |
| |
| return e; |
| } |
| |
| location_t e_loc = cp_expr_loc_or_loc (e, loc); |
| STRIP_ANY_LOCATION_WRAPPER (e); |
| |
| e = mark_type_use (e); |
| |
| if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e), |
| !(complain & tf_error))) |
| { |
| if (!(complain & tf_error)) |
| return error_mark_node; |
| t = size_one_node; |
| } |
| else if (VAR_P (e)) |
| t = size_int (DECL_ALIGN_UNIT (e)); |
| else if (bitfield_p (e)) |
| { |
| if (complain & tf_error) |
| error_at (e_loc, |
| "invalid application of %<__alignof%> to a bit-field"); |
| else |
| return error_mark_node; |
| t = size_one_node; |
| } |
| else if (TREE_CODE (e) == COMPONENT_REF |
| && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL) |
| t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1))); |
| else if (is_overloaded_fn (e)) |
| { |
| if (complain & tf_error) |
| permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to " |
| "an expression of function type"); |
| else |
| return error_mark_node; |
| if (TREE_CODE (e) == FUNCTION_DECL) |
| t = size_int (DECL_ALIGN_UNIT (e)); |
| else |
| t = size_one_node; |
| } |
| else if (type_unknown_p (e)) |
| { |
| if (complain & tf_error) |
| cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e)); |
| else |
| return error_mark_node; |
| t = size_one_node; |
| } |
| else |
| return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e), |
| ALIGNOF_EXPR, std_alignof, |
| complain & tf_error); |
| |
| return fold_convert_loc (loc, size_type_node, t); |
| } |
| |
| /* Process a sizeof or alignof expression E with code OP where the operand |
| is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */ |
| |
| tree |
| cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op, |
| bool std_alignof, bool complain) |
| { |
| gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR); |
| if (op == SIZEOF_EXPR) |
| return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none); |
| else |
| return cxx_alignof_expr (loc, e, std_alignof, |
| complain? tf_warning_or_error : tf_none); |
| } |
| |
| /* Build a representation of an expression 'alignas(E).' Return the |
| folded integer value of E if it is an integral constant expression |
| that resolves to a valid alignment. If E depends on a template |
| parameter, return a syntactic representation tree of kind |
| ALIGNOF_EXPR. Otherwise, return an error_mark_node if the |
| expression is ill formed, or NULL_TREE if E is NULL_TREE. */ |
| |
| tree |
| cxx_alignas_expr (tree e) |
| { |
| if (e == NULL_TREE || e == error_mark_node |
| || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e))) |
| return e; |
| |
| if (TYPE_P (e)) |
| /* [dcl.align]/3: |
| |
| When the alignment-specifier is of the form |
| alignas(type-id), it shall have the same effect as |
| alignas(alignof(type-id)). */ |
| |
| return cxx_sizeof_or_alignof_type (input_location, |
| e, ALIGNOF_EXPR, |
| /*std_alignof=*/true, |
| /*complain=*/true); |
| |
| /* If we reach this point, it means the alignas expression if of |
| the form "alignas(assignment-expression)", so we should follow |
| what is stated by [dcl.align]/2. */ |
| |
| if (value_dependent_expression_p (e)) |
| /* Leave value-dependent expression alone for now. */ |
| return e; |
| |
| e = instantiate_non_dependent_expr (e); |
| e = mark_rvalue_use (e); |
| |
| /* [dcl.align]/2 says: |
| |
| the assignment-expression shall be an integral constant |
| expression. */ |
| |
| if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e))) |
| { |
| error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e)); |
| return error_mark_node; |
| } |
| |
| return cxx_constant_value (e); |
| } |
| |
| |
| /* EXPR is being used in a context that is not a function call. |
| Enforce: |
| |
| [expr.ref] |
| |
| The expression can be used only as the left-hand operand of a |
| member function call. |
| |
| [expr.mptr.operator] |
| |
| If the result of .* or ->* is a function, then that result can be |
| used only as the operand for the function call operator (). |
| |
| by issuing an error message if appropriate. Returns true iff EXPR |
| violates these rules. */ |
| |
| bool |
| invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain) |
| { |
| if (expr == NULL_TREE) |
| return false; |
| /* Don't enforce this in MS mode. */ |
| if (flag_ms_extensions) |
| return false; |
| if (is_overloaded_fn (expr) && !really_overloaded_fn (expr)) |
| expr = get_first_fn (expr); |
| if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr)) |
| { |
| if (complain & tf_error) |
| { |
| if (DECL_P (expr)) |
| { |
| error_at (loc, "invalid use of non-static member function %qD", |
| expr); |
| inform (DECL_SOURCE_LOCATION (expr), "declared here"); |
| } |
| else |
| error_at (loc, "invalid use of non-static member function of " |
| "type %qT", TREE_TYPE (expr)); |
| } |
| return true; |
| } |
| return false; |
| } |
| |
| /* If EXP is a reference to a bit-field, and the type of EXP does not |
| match the declared type of the bit-field, return the declared type |
| of the bit-field. Otherwise, return NULL_TREE. */ |
| |
| tree |
| is_bitfield_expr_with_lowered_type (const_tree exp) |
| { |
| switch (TREE_CODE (exp)) |
| { |
| case COND_EXPR: |
| if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1) |
| ? TREE_OPERAND (exp, 1) |
| : TREE_OPERAND (exp, 0))) |
| return NULL_TREE; |
| return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2)); |
| |
| case COMPOUND_EXPR: |
| return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)); |
| |
| case MODIFY_EXPR: |
| case SAVE_EXPR: |
| case UNARY_PLUS_EXPR: |
| case PREDECREMENT_EXPR: |
| case PREINCREMENT_EXPR: |
| case POSTDECREMENT_EXPR: |
| case POSTINCREMENT_EXPR: |
| case NEGATE_EXPR: |
| case NON_LVALUE_EXPR: |
| case BIT_NOT_EXPR: |
| return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
| |
| case COMPONENT_REF: |
| { |
| tree field; |
| |
| field = TREE_OPERAND (exp, 1); |
| if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field)) |
| return NULL_TREE; |
| if (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field))) |
| return NULL_TREE; |
| return DECL_BIT_FIELD_TYPE (field); |
| } |
| |
| case VAR_DECL: |
| if (DECL_HAS_VALUE_EXPR_P (exp)) |
| return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR |
| (CONST_CAST_TREE (exp))); |
| return NULL_TREE; |
| |
| case VIEW_CONVERT_EXPR: |
| if (location_wrapper_p (exp)) |
| return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0)); |
| else |
| return NULL_TREE; |
| |
| default: |
| return NULL_TREE; |
| } |
| } |
| |
| /* Like is_bitfield_with_lowered_type, except that if EXP is not a |
| bitfield with a lowered type, the type of EXP is returned, rather |
| than NULL_TREE. */ |
| |
| tree |
| unlowered_expr_type (const_tree exp) |
| { |
| tree type; |
| tree etype = TREE_TYPE (exp); |
| |
| type = is_bitfield_expr_with_lowered_type (exp); |
| if (type) |
| type = cp_build_qualified_type (type, cp_type_quals (etype)); |
| else |
| type = etype; |
| |
| return type; |
| } |
| |
| /* Perform the conversions in [expr] that apply when an lvalue appears |
| in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and |
| function-to-pointer conversions. In addition, bitfield references are |
| converted to their declared types. Note that this function does not perform |
| the lvalue-to-rvalue conversion for class types. If you need that conversion |
| for class types, then you probably need to use force_rvalue. |
| |
| Although the returned value is being used as an rvalue, this |
| function does not wrap the returned expression in a |
| NON_LVALUE_EXPR; the caller is expected to be mindful of the fact |
| that the return value is no longer an lvalue. */ |
| |
| tree |
| decay_conversion (tree exp, |
| tsubst_flags_t complain, |
| bool reject_builtin /* = true */) |
| { |
| tree type; |
| enum tree_code code; |
| location_t loc = cp_expr_loc_or_input_loc (exp); |
| |
| type = TREE_TYPE (exp); |
| if (type == error_mark_node) |
| return error_mark_node; |
| |
| exp = resolve_nondeduced_context_or_error (exp, complain); |
| |
| code = TREE_CODE (type); |
| |
| if (error_operand_p (exp)) |
| return error_mark_node; |
| |
| if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) |
| { |
| mark_rvalue_use (exp, loc, reject_builtin); |
| return nullptr_node; |
| } |
| |
| /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. |
| Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ |
| if (code == VOID_TYPE) |
| { |
| if (complain & tf_error) |
| error_at (loc, "void value not ignored as it ought to be"); |
| return error_mark_node; |
| } |
| if (invalid_nonstatic_memfn_p (loc, exp, complain)) |
| return error_mark_node; |
| if (code == FUNCTION_TYPE || is_overloaded_fn (exp)) |
| { |
| exp = mark_lvalue_use (exp); |
| if (reject_builtin && reject_gcc_builtin (exp, loc)) |
| return error_mark_node; |
| return cp_build_addr_expr (exp, complain); |
| } |
| if (code == ARRAY_TYPE) |
| { |
| tree adr; |
| tree ptrtype; |
| |
| exp = mark_lvalue_use (exp); |
| |
| if (INDIRECT_REF_P (exp)) |
| return build_nop (build_pointer_type (TREE_TYPE (type)), |
| TREE_OPERAND (exp, 0)); |
| |
| if (TREE_CODE (exp) == COMPOUND_EXPR) |
| { |
| tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain); |
| if (op1 == error_mark_node) |
| return error_mark_node; |
| return build2 (COMPOUND_EXPR, TREE_TYPE (op1), |
| TREE_OPERAND (exp, 0), op1); |
| } |
| |
| if (!obvalue_p (exp) |
| && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) |
| { |
| if (complain & tf_error) |
| error_at (loc, "invalid use of non-lvalue array"); |
| return error_mark_node; |
| } |
| |
| /* Don't let an array compound literal decay to a pointer. It can |
| still be used to initialize an array or bind to a reference. */ |
| if (TREE_CODE (exp) == TARGET_EXPR) |
| { |
| if (complain & tf_error) |
| error_at (loc, "taking address of temporary array"); |
| return error_mark_node; |
| } |
| |
| ptrtype = build_pointer_type (TREE_TYPE (type)); |
| |
| if (VAR_P (exp)) |
| { |
| if (!cxx_mark_addressable (exp)) |
| return error_mark_node; |
| adr = build_nop (ptrtype, build_address (exp)); |
| return adr; |
| } |
| /* This way is better for a COMPONENT_REF since it can |
| simplify the offset for a component. */ |
| adr = cp_build_addr_expr (exp, complain); |
| return cp_convert (ptrtype, adr, complain); |
| } |
| |
| /* Otherwise, it's the lvalue-to-rvalue conversion. */ |
| exp = mark_rvalue_use (exp, loc, reject_builtin); |
| |
| /* If a bitfield is used in a context where integral promotion |
| applies, then the caller is expected to have used |
| default_conversion. That function promotes bitfields correctly |
| before calling this function. At this point, if we have a |
| bitfield referenced, we may assume that is not subject to |
| promotion, and that, therefore, the type of the resulting rvalue |
| is the declared type of the bitfield. */ |
| exp = convert_bitfield_to_declared_type (exp); |
| |
| /* We do not call rvalue() here because we do not want to wrap EXP |
| in a NON_LVALUE_EXPR. */ |
| |
| /* [basic.lval] |
| |
| Non-class rvalues always have cv-unqualified types. */ |
| type = TREE_TYPE (exp); |
| if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) |
| exp = build_nop (cv_unqualified (type), exp); |
| |
| if (!complete_type_or_maybe_complain (type, exp, complain)) |
| return error_mark_node; |
| |
| return exp; |
| } |
| |
| /* Perform preparatory conversions, as part of the "usual arithmetic |
| conversions". In particular, as per [expr]: |
| |
| Whenever an lvalue expression appears as an operand of an |
| operator that expects the rvalue for that operand, the |
| lvalue-to-rvalue, array-to-pointer, or function-to-pointer |
| standard conversions are applied to convert the expression to an |
| rvalue. |
| |
| In addition, we perform integral promotions here, as those are |
| applied to both operands to a binary operator before determining |
| what additional conversions should apply. */ |
| |
| static tree |
| cp_default_conversion (tree exp, tsubst_flags_t complain) |
| { |
| /* Check for target-specific promotions. */ |
| tree promoted_type = targetm.promoted_type (TREE_TYPE (exp)); |
| if (promoted_type) |
| exp = cp_convert (promoted_type, exp, complain); |
| /* Perform the integral promotions first so that bitfield |
| expressions (which may promote to "int", even if the bitfield is |
| declared "unsigned") are promoted correctly. */ |
| else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp))) |
| exp = cp_perform_integral_promotions (exp, complain); |
| /* Perform the other conversions. */ |
| exp = decay_conversion (exp, complain); |
| |
| return exp; |
| } |
| |
| /* C version. */ |
| |
| tree |
| default_conversion (tree exp) |
| { |
| return cp_default_conversion (exp, tf_warning_or_error); |
| } |
| |
| /* EXPR is an expression with an integral or enumeration type. |
| Perform the integral promotions in [conv.prom], and return the |
| converted value. */ |
| |
| tree |
| cp_perform_integral_promotions (tree expr, tsubst_flags_t complain) |
| { |
| tree type; |
| tree promoted_type; |
| |
| expr = mark_rvalue_use (expr); |
| if (error_operand_p (expr)) |
| return error_mark_node; |
| |
| type = TREE_TYPE (expr); |
| |
| /* [conv.prom] |
| |
| A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue |
| of type int if int can represent all the values of the bit-field; |
| otherwise, it can be converted to unsigned int if unsigned int can |
| represent all the values of the bit-field. If the bit-field is larger yet, |
| no integral promotion applies to it. If the bit-field has an enumerated |
| type, it is treated as any other value of that type for promotion |
| purposes. */ |
| tree bitfield_type = is_bitfield_expr_with_lowered_type (expr); |
| if (bitfield_type |
| && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE |
| || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node))) |
| type = bitfield_type; |
| |
| gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
| /* Scoped enums don't promote. */ |
| if (SCOPED_ENUM_P (type)) |
| return expr; |
| promoted_type = type_promotes_to (type); |
| if (type != promoted_type) |
| expr = cp_convert (promoted_type, expr, complain); |
| else if (bitfield_type && bitfield_type != type) |
| /* Prevent decay_conversion from converting to bitfield_type. */ |
| expr = build_nop (type, expr); |
| return expr; |
| } |
| |
| /* C version. */ |
| |
| tree |
| perform_integral_promotions (tree expr) |
| { |
| return cp_perform_integral_promotions (expr, tf_warning_or_error); |
| } |
| |
| /* Returns nonzero iff exp is a STRING_CST or the result of applying |
| decay_conversion to one. */ |
| |
| int |
| string_conv_p (const_tree totype, const_tree exp, int warn) |
| { |
| tree t; |
| |
| if (!TYPE_PTR_P (totype)) |
| return 0; |
| |
| t = TREE_TYPE (totype); |
| if (!same_type_p (t, char_type_node) |
| && !same_type_p (t, char8_type_node) |
| && !same_type_p (t, char16_type_node) |
| && !same_type_p (t, char32_type_node) |
| && !same_type_p (t, wchar_type_node)) |
| return 0; |
| |
| location_t loc = EXPR_LOC_OR_LOC (exp, input_location); |
| |
| STRIP_ANY_LOCATION_WRAPPER (exp); |
| |
| if (TREE_CODE (exp) == STRING_CST) |
| { |
| /* Make sure that we don't try to convert between char and wide chars. */ |
| if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t)) |
| return 0; |
| } |
| else |
| { |
| /* Is this a string constant which has decayed to 'const char *'? */ |
| t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST)); |
| if (!same_type_p (TREE_TYPE (exp), t)) |
| return 0; |
| STRIP_NOPS (exp); |
| if (TREE_CODE (exp) != ADDR_EXPR |
| || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST) |
| return 0; |
| } |
| if (warn) |
| { |
| if (cxx_dialect >= cxx11) |
| pedwarn (loc, OPT_Wwrite_strings, |
| "ISO C++ forbids converting a string constant to %qT", |
| totype); |
| else |
| warning_at (loc, OPT_Wwrite_strings, |
| "deprecated conversion from string constant to %qT", |
| totype); |
| } |
| |
| return 1; |
| } |
| |
| /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we |
| can, for example, use as an lvalue. This code used to be in |
| unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c' |
| expressions, where we're dealing with aggregates. But now it's again only |
| called from unary_complex_lvalue. The case (in particular) that led to |
| this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd |
| get it there. */ |
| |
| static tree |
| rationalize_conditional_expr (enum tree_code code, tree t, |
| tsubst_flags_t complain) |
| { |
| location_t loc = cp_expr_loc_or_input_loc (t); |
| |
| /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that |
| the first operand is always the one to be used if both operands |
| are equal, so we know what conditional expression this used to be. */ |
| if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR) |
| { |
| tree op0 = TREE_OPERAND (t, 0); |
| tree op1 = TREE_OPERAND (t, 1); |
| |
| /* The following code is incorrect if either operand side-effects. */ |
| gcc_assert (!TREE_SIDE_EFFECTS (op0) |
| && !TREE_SIDE_EFFECTS (op1)); |
| return |
| build_conditional_expr (loc, |
| build_x_binary_op (loc, |
| (TREE_CODE (t) == MIN_EXPR |
| ? LE_EXPR : GE_EXPR), |
| op0, TREE_CODE (op0), |
| op1, TREE_CODE (op1), |
| NULL_TREE, |
| /*overload=*/NULL, |
| complain), |
| cp_build_unary_op (code, op0, false, complain), |
| cp_build_unary_op (code, op1, false, complain), |
| complain); |
| } |
| |
| tree op1 = TREE_OPERAND (t, 1); |
| if (TREE_CODE (op1) != THROW_EXPR) |
| op1 = cp_build_unary_op (code, op1, false, complain); |
| tree op2 = TREE_OPERAND (t, 2); |
| if (TREE_CODE (op2) != THROW_EXPR) |
| op2 = cp_build_unary_op (code, op2, false, complain); |
| |
| return |
| build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain); |
| } |
| |
| /* Given the TYPE of an anonymous union field inside T, return the |
| FIELD_DECL for the field. If not found return NULL_TREE. Because |
| anonymous unions can nest, we must also search all anonymous unions |
| that are directly reachable. */ |
| |
| tree |
| lookup_anon_field (tree, tree type) |
| { |
| tree field; |
| |
| type = TYPE_MAIN_VARIANT (type); |
| field = ANON_AGGR_TYPE_FIELD (type); |
| gcc_assert (field); |
| return field; |
| } |
| |
| /* Build an expression representing OBJECT.MEMBER. OBJECT is an |
| expression; MEMBER is a DECL or baselink. If ACCESS_PATH is |
| non-NULL, it indicates the path to the base used to name MEMBER. |
| If PRESERVE_REFERENCE is true, the expression returned will have |
| REFERENCE_TYPE if the MEMBER does. Otherwise, the expression |
| returned will have the type referred to by the reference. |
| |
| This function does not perform access control; that is either done |
| earlier by the parser when the name of MEMBER is resolved to MEMBER |
| itself, or later when overload resolution selects one of the |
| functions indicated by MEMBER. */ |
| |
| tree |
| build_class_member_access_expr (cp_expr object, tree member, |
| tree access_path, bool preserve_reference, |
| tsubst_flags_t complain) |
| { |
| tree object_type; |
| tree member_scope; |
| tree result = NULL_TREE; |
| tree using_decl = NULL_TREE; |
| |
| if (error_operand_p (object) || error_operand_p (member)) |
| return error_mark_node; |
| |
| gcc_assert (DECL_P (member) || BASELINK_P (member)); |
| |
| /* [expr.ref] |
| |
| The type of the first expression shall be "class object" (of a |
| complete type). */ |
| object_type = TREE_TYPE (object); |
| if (!currently_open_class (object_type) |
| && !complete_type_or_maybe_complain (object_type, object, complain)) |
| return error_mark_node; |
| if (!CLASS_TYPE_P (object_type)) |
| { |
| if (complain & tf_error) |
| { |
| if (INDIRECT_TYPE_P (object_type) |
| && CLASS_TYPE_P (TREE_TYPE (object_type))) |
| error ("request for member %qD in %qE, which is of pointer " |
| "type %qT (maybe you meant to use %<->%> ?)", |
| member, object.get_value (), object_type); |
| else |
| error ("request for member %qD in %qE, which is of non-class " |
| "type %qT", member, object.get_value (), object_type); |
| } |
| return error_mark_node; |
| } |
| |
| /* The standard does not seem to actually say that MEMBER must be a |
| member of OBJECT_TYPE. However, that is clearly what is |
| intended. */ |
| if (DECL_P (member)) |
| { |
| member_scope = DECL_CLASS_CONTEXT (member); |
| if (!mark_used (member, complain) && !(complain & tf_error)) |
| return error_mark_node; |
| |
| if (TREE_UNAVAILABLE (member)) |
| error_unavailable_use (member, NULL_TREE); |
| else if (TREE_DEPRECATED (member)) |
| warn_deprecated_use (member, NULL_TREE); |
| } |
| else |
| member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member)); |
| /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will |
| presently be the anonymous union. Go outwards until we find a |
| type related to OBJECT_TYPE. */ |
| while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope)) |
| && !same_type_ignoring_top_level_qualifiers_p (member_scope, |
| object_type)) |
| member_scope = TYPE_CONTEXT (member_scope); |
| if (!member_scope || !DERIVED_FROM_P (member_scope, object_type)) |
| { |
| if (complain & tf_error) |
| { |
| if (TREE_CODE (member) == FIELD_DECL) |
| error ("invalid use of non-static data member %qE", member); |
| else |
| error ("%qD is not a member of %qT", member, object_type); |
| } |
| return error_mark_node; |
| } |
| |
| /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into |
| `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue |
| in the front end; only _DECLs and _REFs are lvalues in the back end. */ |
| if (tree temp = unary_complex_lvalue (ADDR_EXPR, object)) |
| { |
| temp = cp_build_fold_indirect_ref (temp); |
| if (!lvalue_p (object) && lvalue_p (temp)) |
| /* Preserve rvalueness. */ |
| temp = move (temp); |
| object = temp; |
| } |
| |
| /* In [expr.ref], there is an explicit list of the valid choices for |
| MEMBER. We check for each of those cases here. */ |
| if (VAR_P (member)) |
| { |
| /* A static data member. */ |
| result = member; |
| mark_exp_read (object); |
| |
| if (tree wrap = maybe_get_tls_wrapper_call (result)) |
| /* Replace an evaluated use of the thread_local variable with |
| a call to its wrapper. */ |
| result = wrap; |
| |
| /* If OBJECT has side-effects, they are supposed to occur. */ |
| if (TREE_SIDE_EFFECTS (object)) |
| result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result); |
| } |
| else if (TREE_CODE (member) == FIELD_DECL) |
| { |
| /* A non-static data member. */ |
| bool null_object_p; |
| int type_quals; |
| tree member_type; |
| |
| if (INDIRECT_REF_P (object)) |
| null_object_p = |
| integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0))); |
| else |
| null_object_p = false; |
| |
| /* Convert OBJECT to the type of MEMBER. */ |
| if (!same_type_p (TYPE_MAIN_VARIANT (object_type), |
| TYPE_MAIN_VARIANT (member_scope))) |
| { |
| tree binfo; |
| base_kind kind; |
| |
| /* We didn't complain above about a currently open class, but now we |
| must: we don't know how to refer to a base member before layout is |
| complete. But still don't complain in a template. */ |
| if (!cp_unevaluated_operand |
| && !dependent_type_p (object_type) |
| && !complete_type_or_maybe_complain (object_type, object, |
| complain)) |
| return error_mark_node; |
| |
| binfo = lookup_base (access_path ? access_path : object_type, |
| member_scope, ba_unique, &kind, complain); |
| if (binfo == error_mark_node) |
| return error_mark_node; |
| |
| /* It is invalid to try to get to a virtual base of a |
| NULL object. The most common cause is invalid use of |
| offsetof macro. */ |
| if (null_object_p && kind == bk_via_virtual) |
| { |
| if (complain & tf_error) |
| { |
| error ("invalid access to non-static data member %qD in " |
| "virtual base of NULL object", member); |
| } |
| return error_mark_node; |
| } |
| |
| /* Convert to the base. */ |
| object = build_base_path (PLUS_EXPR, object, binfo, |
| /*nonnull=*/1, complain); |
| /* If we found the base successfully then we should be able |
| to convert to it successfully. */ |
| gcc_assert (object != error_mark_node); |
| } |
| |
| /* If MEMBER is from an anonymous aggregate, we have converted |
| OBJECT so that it refers to the class containing the |
| anonymous union. Generate a reference to the anonymous union |
| itself, and recur to find MEMBER. */ |
| if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)) |
| /* When this code is called from build_field_call, the |
| object already has the type of the anonymous union. |
| That is because the COMPONENT_REF was already |
| constructed, and was then disassembled before calling |
| build_field_call. After the function-call code is |
| cleaned up, this waste can be eliminated. */ |
| && (!same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (object), DECL_CONTEXT (member)))) |
| { |
| tree anonymous_union; |
| |
| anonymous_union = lookup_anon_field (TREE_TYPE (object), |
| DECL_CONTEXT (member)); |
| object = build_class_member_access_expr (object, |
| anonymous_union, |
| /*access_path=*/NULL_TREE, |
| preserve_reference, |
| complain); |
| } |
| |
| /* Compute the type of the field, as described in [expr.ref]. */ |
| type_quals = TYPE_UNQUALIFIED; |
| member_type = TREE_TYPE (member); |
| if (!TYPE_REF_P (member_type)) |
| { |
| type_quals = (cp_type_quals (member_type) |
| | cp_type_quals (object_type)); |
| |
| /* A field is const (volatile) if the enclosing object, or the |
| field itself, is const (volatile). But, a mutable field is |
| not const, even within a const object. */ |
| if (DECL_MUTABLE_P (member)) |
| type_quals &= ~TYPE_QUAL_CONST; |
| member_type = cp_build_qualified_type (member_type, type_quals); |
| } |
| |
| result = build3_loc (input_location, COMPONENT_REF, member_type, |
| object, member, NULL_TREE); |
| |
| /* Mark the expression const or volatile, as appropriate. Even |
| though we've dealt with the type above, we still have to mark the |
| expression itself. */ |
| if (type_quals & TYPE_QUAL_CONST) |
| TREE_READONLY (result) = 1; |
| if (type_quals & TYPE_QUAL_VOLATILE) |
| TREE_THIS_VOLATILE (result) = 1; |
| } |
| else if (BASELINK_P (member)) |
| { |
| /* The member is a (possibly overloaded) member function. */ |
| tree functions; |
| tree type; |
| |
| /* If the MEMBER is exactly one static member function, then we |
| know the type of the expression. Otherwise, we must wait |
| until overload resolution has been performed. */ |
| functions = BASELINK_FUNCTIONS (member); |
| if (TREE_CODE (functions) == FUNCTION_DECL |
| && DECL_STATIC_FUNCTION_P (functions)) |
| type = TREE_TYPE (functions); |
| else |
| type = unknown_type_node; |
| /* Note that we do not convert OBJECT to the BASELINK_BINFO |
| base. That will happen when the function is called. */ |
| result = build3_loc (input_location, COMPONENT_REF, type, object, member, |
| NULL_TREE); |
| } |
| else if (TREE_CODE (member) == CONST_DECL) |
| { |
| /* The member is an enumerator. */ |
| result = member; |
| /* If OBJECT has side-effects, they are supposed to occur. */ |
| if (TREE_SIDE_EFFECTS (object)) |
| result = build2 (COMPOUND_EXPR, TREE_TYPE (result), |
| object, result); |
| } |
| else if ((using_decl = strip_using_decl (member)) != member) |
| result = build_class_member_access_expr (object, |
| using_decl, |
| access_path, preserve_reference, |
| complain); |
| else |
| { |
| if (complain & tf_error) |
| error ("invalid use of %qD", member); |
| return error_mark_node; |
| } |
| |
| if (!preserve_reference) |
| /* [expr.ref] |
| |
| If E2 is declared to have type "reference to T", then ... the |
| type of E1.E2 is T. */ |
| result = convert_from_reference (result); |
| |
| return result; |
| } |
| |
| /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if |
| SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */ |
| |
| tree |
| lookup_destructor (tree object, tree scope, tree dtor_name, |
| tsubst_flags_t complain) |
| { |
| tree object_type = TREE_TYPE (object); |
| tree dtor_type = TREE_OPERAND (dtor_name, 0); |
| tree expr; |
| |
| /* We've already complained about this destructor. */ |
| if (dtor_type == error_mark_node) |
| return error_mark_node; |
| |
| if (scope && !check_dtor_name (scope, dtor_type)) |
| { |
| if (complain & tf_error) |
| error ("qualified type %qT does not match destructor name ~%qT", |
| scope, dtor_type); |
| return error_mark_node; |
| } |
| if (is_auto (dtor_type)) |
| dtor_type = object_type; |
| else if (identifier_p (dtor_type)) |
| { |
| /* In a template, names we can't find a match for are still accepted |
| destructor names, and we check them here. */ |
| if (check_dtor_name (object_type, dtor_type)) |
| dtor_type = object_type; |
| else |
| { |
| if (complain & tf_error) |
| error ("object type %qT does not match destructor name ~%qT", |
| object_type, dtor_type); |
| return error_mark_node; |
| } |
| |
| } |
| else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type))) |
| { |
| if (complain & tf_error) |
| error ("the type being destroyed is %qT, but the destructor " |
| "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type); |
| return error_mark_node; |
| } |
| expr = lookup_member (dtor_type, complete_dtor_identifier, |
| /*protect=*/1, /*want_type=*/false, |
| tf_warning_or_error); |
| if (!expr) |
| { |
| if (complain & tf_error) |
| cxx_incomplete_type_error (dtor_name, dtor_type); |
| return error_mark_node; |
| } |
| expr = (adjust_result_of_qualified_name_lookup |
| (expr, dtor_type, object_type)); |
| if (scope == NULL_TREE) |
| /* We need to call adjust_result_of_qualified_name_lookup in case the |
| destructor names a base class, but we unset BASELINK_QUALIFIED_P so |
| that we still get virtual function binding. */ |
| BASELINK_QUALIFIED_P (expr) = false; |
| return expr; |
| } |
| |
| /* An expression of the form "A::template B" has been resolved to |
| DECL. Issue a diagnostic if B is not a template or template |
| specialization. */ |
| |
| void |
| check_template_keyword (tree decl) |
| { |
| /* The standard says: |
| |
| [temp.names] |
| |
| If a name prefixed by the keyword template is not a member |
| template, the program is ill-formed. |
| |
| DR 228 removed the restriction that the template be a member |
| template. |
| |
| DR 96, if accepted would add the further restriction that explicit |
| template arguments must be provided if the template keyword is |
| used, but, as of 2005-10-16, that DR is still in "drafting". If |
| this DR is accepted, then the semantic checks here can be |
| simplified, as the entity named must in fact be a template |
| specialization, rather than, as at present, a set of overloaded |
| functions containing at least one template function. */ |
| if (TREE_CODE (decl) != TEMPLATE_DECL |
| && TREE_CODE (decl) != TEMPLATE_ID_EXPR) |
| { |
| if (VAR_P (decl)) |
| { |
| if (DECL_USE_TEMPLATE (decl) |
| && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) |
| ; |
| else |
| permerror (input_location, "%qD is not a template", decl); |
| } |
| else if (!is_overloaded_fn (decl)) |
| permerror (input_location, "%qD is not a template", decl); |
| else |
| { |
| bool found = false; |
| |
| for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl)); |
| !found && iter; ++iter) |
| { |
| tree fn = *iter; |
| if (TREE_CODE (fn) == TEMPLATE_DECL |
| || TREE_CODE (fn) == TEMPLATE_ID_EXPR |
| || (TREE_CODE (fn) == FUNCTION_DECL |
| && DECL_USE_TEMPLATE (fn) |
| && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))) |
| found = true; |
| } |
| if (!found) |
| permerror (input_location, "%qD is not a template", decl); |
| } |
| } |
| } |
| |
| /* Record that an access failure occurred on BASETYPE_PATH attempting |
| to access DECL, where DIAG_DECL should be used for diagnostics. */ |
| |
| void |
| access_failure_info::record_access_failure (tree basetype_path, |
| tree decl, tree diag_decl) |
| { |
| m_was_inaccessible = true; |
| m_basetype_path = basetype_path; |
| m_decl = decl; |
| m_diag_decl = diag_decl; |
| } |
| |
| /* If an access failure was recorded, then attempt to locate an |
| accessor function for the pertinent field. |
| Otherwise, return NULL_TREE. */ |
| |
| tree |
| access_failure_info::get_any_accessor (bool const_p) const |
| { |
| if (!was_inaccessible_p ()) |
| return NULL_TREE; |
| |
| tree accessor |
| = locate_field_accessor (m_basetype_path, m_diag_decl, const_p); |
| if (!accessor) |
| return NULL_TREE; |
| |
| /* The accessor must itself be accessible for it to be a reasonable |
| suggestion. */ |
| if (!accessible_p (m_basetype_path, accessor, true)) |
| return NULL_TREE; |
| |
| return accessor; |
| } |
| |
| /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by |
| replacing the primary location in RICHLOC with "accessor()". */ |
| |
| void |
| access_failure_info::add_fixit_hint (rich_location *richloc, |
| tree accessor_decl) |
| { |
| pretty_printer pp; |
| pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl))); |
| pp_string (&pp, "()"); |
| richloc->add_fixit_replace (pp_formatted_text (&pp)); |
| } |
| |
| /* If an access failure was recorded, then attempt to locate an |
| accessor function for the pertinent field, and if one is |
| available, add a note and fix-it hint suggesting using it. */ |
| |
| void |
| access_failure_info::maybe_suggest_accessor (bool const_p) const |
| { |
| tree accessor = get_any_accessor (const_p); |
| if (accessor == NULL_TREE) |
| return; |
| rich_location richloc (line_table, input_location); |
| add_fixit_hint (&richloc, accessor); |
| inform (&richloc, "field %q#D can be accessed via %q#D", |
| m_diag_decl, accessor); |
| } |
| |
| /* Subroutine of finish_class_member_access_expr. |
| Issue an error about NAME not being a member of ACCESS_PATH (or |
| OBJECT_TYPE), potentially providing a fix-it hint for misspelled |
| names. */ |
| |
| static void |
| complain_about_unrecognized_member (tree access_path, tree name, |
| tree object_type) |
| { |
| /* Attempt to provide a hint about misspelled names. */ |
| tree guessed_id = lookup_member_fuzzy (access_path, name, |
| /*want_type=*/false); |
| if (guessed_id == NULL_TREE) |
| { |
| /* No hint. */ |
| error ("%q#T has no member named %qE", |
| TREE_CODE (access_path) == TREE_BINFO |
| ? TREE_TYPE (access_path) : object_type, name); |
| return; |
| } |
| |
| location_t bogus_component_loc = input_location; |
| gcc_rich_location rich_loc (bogus_component_loc); |
| |
| /* Check that the guessed name is accessible along access_path. */ |
| access_failure_info afi; |
| lookup_member (access_path, guessed_id, /*protect=*/1, |
| /*want_type=*/false, /*complain=*/false, |
| &afi); |
| if (afi.was_inaccessible_p ()) |
| { |
| tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type)); |
| if (accessor) |
| { |
| /* The guessed name isn't directly accessible, but can be accessed |
| via an accessor member function. */ |
| afi.add_fixit_hint (&rich_loc, accessor); |
| error_at (&rich_loc, |
| "%q#T has no member named %qE;" |
| " did you mean %q#D? (accessible via %q#D)", |
| TREE_CODE (access_path) == TREE_BINFO |
| ? TREE_TYPE (access_path) : object_type, |
| name, afi.get_diag_decl (), accessor); |
| } |
| else |
| { |
| /* The guessed name isn't directly accessible, and no accessor |
| member function could be found. */ |
| error_at (&rich_loc, |
| "%q#T has no member named %qE;" |
| " did you mean %q#D? (not accessible from this context)", |
| TREE_CODE (access_path) == TREE_BINFO |
| ? TREE_TYPE (access_path) : object_type, |
| name, afi.get_diag_decl ()); |
| complain_about_access (afi.get_decl (), afi.get_diag_decl (), |
| afi.get_diag_decl (), false, ak_none); |
| } |
| } |
| else |
| { |
| /* The guessed name is directly accessible; suggest it. */ |
| rich_loc.add_fixit_misspelled_id (bogus_component_loc, |
| guessed_id); |
| error_at (&rich_loc, |
| "%q#T has no member named %qE;" |
| " did you mean %qE?", |
| TREE_CODE (access_path) == TREE_BINFO |
| ? TREE_TYPE (access_path) : object_type, |
| name, guessed_id); |
| } |
| } |
| |
| /* This function is called by the parser to process a class member |
| access expression of the form OBJECT.NAME. NAME is a node used by |
| the parser to represent a name; it is not yet a DECL. It may, |
| however, be a BASELINK where the BASELINK_FUNCTIONS is a |
| TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and |
| there is no reason to do the lookup twice, so the parser keeps the |
| BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to |
| be a template via the use of the "A::template B" syntax. */ |
| |
| tree |
| finish_class_member_access_expr (cp_expr object, tree name, bool template_p, |
| tsubst_flags_t complain) |
| { |
| tree expr; |
| tree object_type; |
| tree member; |
| tree access_path = NULL_TREE; |
| tree orig_object |