| /* Perform -*- C++ -*- constant expression evaluation, including calls to |
| constexpr functions. These routines are used both during actual parsing |
| and during the instantiation of template functions. |
| |
| Copyright (C) 1998-2019 Free Software Foundation, Inc. |
| |
| 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/>. */ |
| |
| #include "config.h" |
| #include "system.h" |
| #include "coretypes.h" |
| #include "cp-tree.h" |
| #include "varasm.h" |
| #include "c-family/c-objc.h" |
| #include "tree-iterator.h" |
| #include "gimplify.h" |
| #include "builtins.h" |
| #include "tree-inline.h" |
| #include "ubsan.h" |
| #include "gimple-fold.h" |
| #include "timevar.h" |
| #include "fold-const-call.h" |
| |
| static bool verify_constant (tree, bool, bool *, bool *); |
| #define VERIFY_CONSTANT(X) \ |
| do { \ |
| if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
| return t; \ |
| } while (0) |
| |
| static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
| bool insert = false); |
| |
| /* Returns true iff FUN is an instantiation of a constexpr function |
| template or a defaulted constexpr function. */ |
| |
| bool |
| is_instantiation_of_constexpr (tree fun) |
| { |
| return ((DECL_TEMPLOID_INSTANTIATION (fun) |
| && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) |
| || (DECL_DEFAULTED_FN (fun) |
| && DECL_DECLARED_CONSTEXPR_P (fun))); |
| } |
| |
| /* Return true if T is a literal type. */ |
| |
| bool |
| literal_type_p (tree t) |
| { |
| if (SCALAR_TYPE_P (t) |
| || VECTOR_TYPE_P (t) |
| || TYPE_REF_P (t) |
| || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
| return true; |
| if (CLASS_TYPE_P (t)) |
| { |
| t = complete_type (t); |
| gcc_assert (COMPLETE_TYPE_P (t) || errorcount); |
| return CLASSTYPE_LITERAL_P (t); |
| } |
| if (TREE_CODE (t) == ARRAY_TYPE) |
| return literal_type_p (strip_array_types (t)); |
| return false; |
| } |
| |
| /* If DECL is a variable declared `constexpr', require its type |
| be literal. Return error_mark_node if we give an error, the |
| DECL otherwise. */ |
| |
| tree |
| ensure_literal_type_for_constexpr_object (tree decl) |
| { |
| tree type = TREE_TYPE (decl); |
| if (VAR_P (decl) |
| && (DECL_DECLARED_CONSTEXPR_P (decl) |
| || var_in_constexpr_fn (decl)) |
| && !processing_template_decl) |
| { |
| tree stype = strip_array_types (type); |
| if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) |
| /* Don't complain here, we'll complain about incompleteness |
| when we try to initialize the variable. */; |
| else if (type_uses_auto (type)) |
| /* We don't know the actual type yet. */; |
| else if (!literal_type_p (type)) |
| { |
| if (DECL_DECLARED_CONSTEXPR_P (decl)) |
| { |
| auto_diagnostic_group d; |
| error_at (DECL_SOURCE_LOCATION (decl), |
| "the type %qT of %<constexpr%> variable %qD " |
| "is not literal", type, decl); |
| explain_non_literal_class (type); |
| decl = error_mark_node; |
| } |
| else |
| { |
| if (!is_instantiation_of_constexpr (current_function_decl)) |
| { |
| auto_diagnostic_group d; |
| error_at (DECL_SOURCE_LOCATION (decl), |
| "variable %qD of non-literal type %qT in " |
| "%<constexpr%> function", decl, type); |
| explain_non_literal_class (type); |
| decl = error_mark_node; |
| } |
| cp_function_chain->invalid_constexpr = true; |
| } |
| } |
| else if (DECL_DECLARED_CONSTEXPR_P (decl) |
| && variably_modified_type_p (type, NULL_TREE)) |
| { |
| error_at (DECL_SOURCE_LOCATION (decl), |
| "%<constexpr%> variable %qD has variably-modified " |
| "type %qT", decl, type); |
| decl = error_mark_node; |
| } |
| } |
| return decl; |
| } |
| |
| /* Representation of entries in the constexpr function definition table. */ |
| |
| struct GTY((for_user)) constexpr_fundef { |
| tree decl; |
| tree body; |
| tree parms; |
| tree result; |
| }; |
| |
| struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
| { |
| static hashval_t hash (constexpr_fundef *); |
| static bool equal (constexpr_fundef *, constexpr_fundef *); |
| }; |
| |
| /* This table holds all constexpr function definitions seen in |
| the current translation unit. */ |
| |
| static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; |
| |
| /* Utility function used for managing the constexpr function table. |
| Return true if the entries pointed to by P and Q are for the |
| same constexpr function. */ |
| |
| inline bool |
| constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs) |
| { |
| return lhs->decl == rhs->decl; |
| } |
| |
| /* Utility function used for managing the constexpr function table. |
| Return a hash value for the entry pointed to by Q. */ |
| |
| inline hashval_t |
| constexpr_fundef_hasher::hash (constexpr_fundef *fundef) |
| { |
| return DECL_UID (fundef->decl); |
| } |
| |
| /* Return a previously saved definition of function FUN. */ |
| |
| static constexpr_fundef * |
| retrieve_constexpr_fundef (tree fun) |
| { |
| if (constexpr_fundef_table == NULL) |
| return NULL; |
| |
| constexpr_fundef fundef = { fun, NULL, NULL, NULL }; |
| return constexpr_fundef_table->find (&fundef); |
| } |
| |
| /* Check whether the parameter and return types of FUN are valid for a |
| constexpr function, and complain if COMPLAIN. */ |
| |
| bool |
| is_valid_constexpr_fn (tree fun, bool complain) |
| { |
| bool ret = true; |
| |
| if (DECL_INHERITED_CTOR (fun) |
| && TREE_CODE (fun) == TEMPLATE_DECL) |
| { |
| ret = false; |
| if (complain) |
| error ("inherited constructor %qD is not %<constexpr%>", |
| DECL_INHERITED_CTOR (fun)); |
| } |
| else |
| { |
| for (tree parm = FUNCTION_FIRST_USER_PARM (fun); |
| parm != NULL_TREE; parm = TREE_CHAIN (parm)) |
| if (!literal_type_p (TREE_TYPE (parm))) |
| { |
| ret = false; |
| if (complain) |
| { |
| auto_diagnostic_group d; |
| error ("invalid type for parameter %d of %<constexpr%> " |
| "function %q+#D", DECL_PARM_INDEX (parm), fun); |
| explain_non_literal_class (TREE_TYPE (parm)); |
| } |
| } |
| } |
| |
| if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
| { |
| ret = false; |
| if (complain) |
| inform (DECL_SOURCE_LOCATION (fun), |
| "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
| } |
| else if (!DECL_CONSTRUCTOR_P (fun)) |
| { |
| tree rettype = TREE_TYPE (TREE_TYPE (fun)); |
| if (!literal_type_p (rettype)) |
| { |
| ret = false; |
| if (complain) |
| { |
| auto_diagnostic_group d; |
| error ("invalid return type %qT of %<constexpr%> function %q+D", |
| rettype, fun); |
| explain_non_literal_class (rettype); |
| } |
| } |
| |
| /* C++14 DR 1684 removed this restriction. */ |
| if (cxx_dialect < cxx14 |
| && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) |
| && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
| { |
| ret = false; |
| if (complain) |
| { |
| auto_diagnostic_group d; |
| if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, |
| "enclosing class of %<constexpr%> non-static" |
| " member function %q+#D is not a literal type", |
| fun)) |
| explain_non_literal_class (DECL_CONTEXT (fun)); |
| } |
| } |
| } |
| else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) |
| { |
| ret = false; |
| if (complain) |
| error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); |
| } |
| |
| return ret; |
| } |
| |
| /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF |
| for a member of an anonymous aggregate, INIT is the initializer for that |
| member, and VEC_OUTER is the vector of constructor elements for the class |
| whose constructor we are processing. Add the initializer to the vector |
| and return true to indicate success. */ |
| |
| static bool |
| build_anon_member_initialization (tree member, tree init, |
| vec<constructor_elt, va_gc> **vec_outer) |
| { |
| /* MEMBER presents the relevant fields from the inside out, but we need |
| to build up the initializer from the outside in so that we can reuse |
| previously built CONSTRUCTORs if this is, say, the second field in an |
| anonymous struct. So we use a vec as a stack. */ |
| auto_vec<tree, 2> fields; |
| do |
| { |
| fields.safe_push (TREE_OPERAND (member, 1)); |
| member = TREE_OPERAND (member, 0); |
| } |
| while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) |
| && TREE_CODE (member) == COMPONENT_REF); |
| |
| /* VEC has the constructor elements vector for the context of FIELD. |
| If FIELD is an anonymous aggregate, we will push inside it. */ |
| vec<constructor_elt, va_gc> **vec = vec_outer; |
| tree field; |
| while (field = fields.pop(), |
| ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| { |
| tree ctor; |
| /* If there is already an outer constructor entry for the anonymous |
| aggregate FIELD, use it; otherwise, insert one. */ |
| if (vec_safe_is_empty (*vec) |
| || (*vec)->last().index != field) |
| { |
| ctor = build_constructor (TREE_TYPE (field), NULL); |
| CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); |
| } |
| else |
| ctor = (*vec)->last().value; |
| vec = &CONSTRUCTOR_ELTS (ctor); |
| } |
| |
| /* Now we're at the innermost field, the one that isn't an anonymous |
| aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ |
| gcc_assert (fields.is_empty()); |
| CONSTRUCTOR_APPEND_ELT (*vec, field, init); |
| |
| return true; |
| } |
| |
| /* Subroutine of build_constexpr_constructor_member_initializers. |
| The expression tree T represents a data member initialization |
| in a (constexpr) constructor definition. Build a pairing of |
| the data member with its initializer, and prepend that pair |
| to the existing initialization pair INITS. */ |
| |
| static bool |
| build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) |
| { |
| tree member, init; |
| if (TREE_CODE (t) == CLEANUP_POINT_EXPR) |
| t = TREE_OPERAND (t, 0); |
| if (TREE_CODE (t) == EXPR_STMT) |
| t = TREE_OPERAND (t, 0); |
| if (t == error_mark_node) |
| return false; |
| if (TREE_CODE (t) == STATEMENT_LIST) |
| { |
| tree_stmt_iterator i; |
| for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) |
| { |
| if (! build_data_member_initialization (tsi_stmt (i), vec)) |
| return false; |
| } |
| return true; |
| } |
| if (TREE_CODE (t) == CLEANUP_STMT) |
| { |
| /* We can't see a CLEANUP_STMT in a constructor for a literal class, |
| but we can in a constexpr constructor for a non-literal class. Just |
| ignore it; either all the initialization will be constant, in which |
| case the cleanup can't run, or it can't be constexpr. |
| Still recurse into CLEANUP_BODY. */ |
| return build_data_member_initialization (CLEANUP_BODY (t), vec); |
| } |
| if (TREE_CODE (t) == CONVERT_EXPR) |
| t = TREE_OPERAND (t, 0); |
| if (TREE_CODE (t) == INIT_EXPR |
| /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
| use what this function builds for cx_check_missing_mem_inits, and |
| assignment in the ctor body doesn't count. */ |
| || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) |
| { |
| member = TREE_OPERAND (t, 0); |
| init = break_out_target_exprs (TREE_OPERAND (t, 1)); |
| } |
| else if (TREE_CODE (t) == CALL_EXPR) |
| { |
| tree fn = get_callee_fndecl (t); |
| if (!fn || !DECL_CONSTRUCTOR_P (fn)) |
| /* We're only interested in calls to subobject constructors. */ |
| return true; |
| member = CALL_EXPR_ARG (t, 0); |
| /* We don't use build_cplus_new here because it complains about |
| abstract bases. Leaving the call unwrapped means that it has the |
| wrong type, but cxx_eval_constant_expression doesn't care. */ |
| init = break_out_target_exprs (t); |
| } |
| else if (TREE_CODE (t) == BIND_EXPR) |
| return build_data_member_initialization (BIND_EXPR_BODY (t), vec); |
| else |
| /* Don't add anything else to the CONSTRUCTOR. */ |
| return true; |
| if (INDIRECT_REF_P (member)) |
| member = TREE_OPERAND (member, 0); |
| if (TREE_CODE (member) == NOP_EXPR) |
| { |
| tree op = member; |
| STRIP_NOPS (op); |
| if (TREE_CODE (op) == ADDR_EXPR) |
| { |
| gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (TREE_TYPE (op)), |
| TREE_TYPE (TREE_TYPE (member)))); |
| /* Initializing a cv-qualified member; we need to look through |
| the const_cast. */ |
| member = op; |
| } |
| else if (op == current_class_ptr |
| && (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (TREE_TYPE (member)), |
| current_class_type))) |
| /* Delegating constructor. */ |
| member = op; |
| else |
| { |
| /* This is an initializer for an empty base; keep it for now so |
| we can check it in cxx_eval_bare_aggregate. */ |
| gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); |
| } |
| } |
| if (TREE_CODE (member) == ADDR_EXPR) |
| member = TREE_OPERAND (member, 0); |
| if (TREE_CODE (member) == COMPONENT_REF) |
| { |
| tree aggr = TREE_OPERAND (member, 0); |
| if (TREE_CODE (aggr) == VAR_DECL) |
| /* Initializing a local variable, don't add anything. */ |
| return true; |
| if (TREE_CODE (aggr) != COMPONENT_REF) |
| /* Normal member initialization. */ |
| member = TREE_OPERAND (member, 1); |
| else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) |
| /* Initializing a member of an anonymous union. */ |
| return build_anon_member_initialization (member, init, vec); |
| else |
| /* We're initializing a vtable pointer in a base. Leave it as |
| COMPONENT_REF so we remember the path to get to the vfield. */ |
| gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); |
| } |
| |
| /* Value-initialization can produce multiple initializers for the |
| same field; use the last one. */ |
| if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) |
| (*vec)->last().value = init; |
| else |
| CONSTRUCTOR_APPEND_ELT (*vec, member, init); |
| return true; |
| } |
| |
| /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. |
| In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a |
| BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ |
| |
| static bool |
| check_constexpr_bind_expr_vars (tree t) |
| { |
| gcc_assert (TREE_CODE (t) == BIND_EXPR); |
| |
| for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
| if (TREE_CODE (var) == TYPE_DECL |
| && DECL_IMPLICIT_TYPEDEF_P (var) |
| && !LAMBDA_TYPE_P (TREE_TYPE (var))) |
| return false; |
| return true; |
| } |
| |
| /* Subroutine of check_constexpr_ctor_body. */ |
| |
| static bool |
| check_constexpr_ctor_body_1 (tree last, tree list) |
| { |
| switch (TREE_CODE (list)) |
| { |
| case DECL_EXPR: |
| if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
| || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) |
| return true; |
| return false; |
| |
| case CLEANUP_POINT_EXPR: |
| return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), |
| /*complain=*/false); |
| |
| case BIND_EXPR: |
| if (!check_constexpr_bind_expr_vars (list) |
| || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), |
| /*complain=*/false)) |
| return false; |
| return true; |
| |
| case USING_STMT: |
| case STATIC_ASSERT: |
| case DEBUG_BEGIN_STMT: |
| return true; |
| |
| default: |
| return false; |
| } |
| } |
| |
| /* Make sure that there are no statements after LAST in the constructor |
| body represented by LIST. */ |
| |
| bool |
| check_constexpr_ctor_body (tree last, tree list, bool complain) |
| { |
| /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
| if (cxx_dialect >= cxx14) |
| return true; |
| |
| bool ok = true; |
| if (TREE_CODE (list) == STATEMENT_LIST) |
| { |
| tree_stmt_iterator i = tsi_last (list); |
| for (; !tsi_end_p (i); tsi_prev (&i)) |
| { |
| tree t = tsi_stmt (i); |
| if (t == last) |
| break; |
| if (!check_constexpr_ctor_body_1 (last, t)) |
| { |
| ok = false; |
| break; |
| } |
| } |
| } |
| else if (list != last |
| && !check_constexpr_ctor_body_1 (last, list)) |
| ok = false; |
| if (!ok) |
| { |
| if (complain) |
| error ("%<constexpr%> constructor does not have empty body"); |
| DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
| } |
| return ok; |
| } |
| |
| /* V is a vector of constructor elements built up for the base and member |
| initializers of a constructor for TYPE. They need to be in increasing |
| offset order, which they might not be yet if TYPE has a primary base |
| which is not first in the base-clause or a vptr and at least one base |
| all of which are non-primary. */ |
| |
| static vec<constructor_elt, va_gc> * |
| sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) |
| { |
| tree pri = CLASSTYPE_PRIMARY_BINFO (type); |
| tree field_type; |
| unsigned i; |
| constructor_elt *ce; |
| |
| if (pri) |
| field_type = BINFO_TYPE (pri); |
| else if (TYPE_CONTAINS_VPTR_P (type)) |
| field_type = vtbl_ptr_type_node; |
| else |
| return v; |
| |
| /* Find the element for the primary base or vptr and move it to the |
| beginning of the vec. */ |
| for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| if (TREE_TYPE (ce->index) == field_type) |
| break; |
| |
| if (i > 0 && i < vec_safe_length (v)) |
| { |
| vec<constructor_elt, va_gc> &vref = *v; |
| constructor_elt elt = vref[i]; |
| for (; i > 0; --i) |
| vref[i] = vref[i-1]; |
| vref[0] = elt; |
| } |
| |
| return v; |
| } |
| |
| /* Build compile-time evalable representations of member-initializer list |
| for a constexpr constructor. */ |
| |
| static tree |
| build_constexpr_constructor_member_initializers (tree type, tree body) |
| { |
| vec<constructor_elt, va_gc> *vec = NULL; |
| bool ok = true; |
| while (true) |
| switch (TREE_CODE (body)) |
| { |
| case MUST_NOT_THROW_EXPR: |
| case EH_SPEC_BLOCK: |
| body = TREE_OPERAND (body, 0); |
| break; |
| |
| case STATEMENT_LIST: |
| for (tree_stmt_iterator i = tsi_start (body); |
| !tsi_end_p (i); tsi_next (&i)) |
| { |
| body = tsi_stmt (i); |
| if (TREE_CODE (body) == BIND_EXPR) |
| break; |
| } |
| break; |
| |
| case BIND_EXPR: |
| body = BIND_EXPR_BODY (body); |
| goto found; |
| |
| default: |
| gcc_unreachable (); |
| } |
| found: |
| if (TREE_CODE (body) == TRY_BLOCK) |
| { |
| body = TREE_OPERAND (body, 0); |
| if (TREE_CODE (body) == BIND_EXPR) |
| body = BIND_EXPR_BODY (body); |
| } |
| if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
| { |
| body = TREE_OPERAND (body, 0); |
| if (TREE_CODE (body) == EXPR_STMT) |
| body = TREE_OPERAND (body, 0); |
| if (TREE_CODE (body) == INIT_EXPR |
| && (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (TREE_OPERAND (body, 0)), |
| current_class_type))) |
| { |
| /* Trivial copy. */ |
| return TREE_OPERAND (body, 1); |
| } |
| ok = build_data_member_initialization (body, &vec); |
| } |
| else if (TREE_CODE (body) == STATEMENT_LIST) |
| { |
| tree_stmt_iterator i; |
| for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) |
| { |
| ok = build_data_member_initialization (tsi_stmt (i), &vec); |
| if (!ok) |
| break; |
| } |
| } |
| else if (EXPR_P (body)) |
| ok = build_data_member_initialization (body, &vec); |
| else |
| gcc_assert (errorcount > 0); |
| if (ok) |
| { |
| if (vec_safe_length (vec) > 0) |
| { |
| /* In a delegating constructor, return the target. */ |
| constructor_elt *ce = &(*vec)[0]; |
| if (ce->index == current_class_ptr) |
| { |
| body = ce->value; |
| vec_free (vec); |
| return body; |
| } |
| } |
| vec = sort_constexpr_mem_initializers (type, vec); |
| return build_constructor (type, vec); |
| } |
| else |
| return error_mark_node; |
| } |
| |
| /* We have an expression tree T that represents a call, either CALL_EXPR |
| or AGGR_INIT_EXPR. If the call is lexically to a named function, |
| retrun the _DECL for that function. */ |
| |
| static tree |
| get_function_named_in_call (tree t) |
| { |
| tree fun = cp_get_callee (t); |
| if (fun && TREE_CODE (fun) == ADDR_EXPR |
| && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) |
| fun = TREE_OPERAND (fun, 0); |
| return fun; |
| } |
| |
| /* Subroutine of register_constexpr_fundef. BODY is the body of a function |
| declared to be constexpr, or a sub-statement thereof. Returns the |
| return value if suitable, error_mark_node for a statement not allowed in |
| a constexpr function, or NULL_TREE if no return value was found. */ |
| |
| tree |
| constexpr_fn_retval (tree body) |
| { |
| switch (TREE_CODE (body)) |
| { |
| case STATEMENT_LIST: |
| { |
| tree_stmt_iterator i; |
| tree expr = NULL_TREE; |
| for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) |
| { |
| tree s = constexpr_fn_retval (tsi_stmt (i)); |
| if (s == error_mark_node) |
| return error_mark_node; |
| else if (s == NULL_TREE) |
| /* Keep iterating. */; |
| else if (expr) |
| /* Multiple return statements. */ |
| return error_mark_node; |
| else |
| expr = s; |
| } |
| return expr; |
| } |
| |
| case RETURN_EXPR: |
| return break_out_target_exprs (TREE_OPERAND (body, 0)); |
| |
| case DECL_EXPR: |
| { |
| tree decl = DECL_EXPR_DECL (body); |
| if (TREE_CODE (decl) == USING_DECL |
| /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ |
| || DECL_ARTIFICIAL (decl)) |
| return NULL_TREE; |
| return error_mark_node; |
| } |
| |
| case CLEANUP_POINT_EXPR: |
| return constexpr_fn_retval (TREE_OPERAND (body, 0)); |
| |
| case BIND_EXPR: |
| if (!check_constexpr_bind_expr_vars (body)) |
| return error_mark_node; |
| return constexpr_fn_retval (BIND_EXPR_BODY (body)); |
| |
| case USING_STMT: |
| case DEBUG_BEGIN_STMT: |
| return NULL_TREE; |
| |
| case CALL_EXPR: |
| { |
| tree fun = get_function_named_in_call (body); |
| if (fun != NULL_TREE |
| && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
| return NULL_TREE; |
| } |
| /* Fallthru. */ |
| |
| default: |
| return error_mark_node; |
| } |
| } |
| |
| /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
| FUN; do the necessary transformations to turn it into a single expression |
| that we can store in the hash table. */ |
| |
| static tree |
| massage_constexpr_body (tree fun, tree body) |
| { |
| if (DECL_CONSTRUCTOR_P (fun)) |
| body = build_constexpr_constructor_member_initializers |
| (DECL_CONTEXT (fun), body); |
| else if (cxx_dialect < cxx14) |
| { |
| if (TREE_CODE (body) == EH_SPEC_BLOCK) |
| body = EH_SPEC_STMTS (body); |
| if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) |
| body = TREE_OPERAND (body, 0); |
| body = constexpr_fn_retval (body); |
| } |
| return body; |
| } |
| |
| /* CTYPE is a type constructed from BODY. Return true if some |
| bases/fields are uninitialized, and complain if COMPLAIN. */ |
| |
| static bool |
| cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
| { |
| unsigned nelts = 0; |
| |
| if (body) |
| { |
| if (TREE_CODE (body) != CONSTRUCTOR) |
| return false; |
| nelts = CONSTRUCTOR_NELTS (body); |
| } |
| tree field = TYPE_FIELDS (ctype); |
| |
| if (TREE_CODE (ctype) == UNION_TYPE) |
| { |
| if (nelts == 0 && next_initializable_field (field)) |
| { |
| if (complain) |
| error ("%<constexpr%> constructor for union %qT must " |
| "initialize exactly one non-static data member", ctype); |
| return true; |
| } |
| return false; |
| } |
| |
| /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
| need an explicit initialization. */ |
| bool bad = false; |
| for (unsigned i = 0; i <= nelts; ++i) |
| { |
| tree index = NULL_TREE; |
| if (i < nelts) |
| { |
| index = CONSTRUCTOR_ELT (body, i)->index; |
| /* Skip base and vtable inits. */ |
| if (TREE_CODE (index) != FIELD_DECL |
| || DECL_ARTIFICIAL (index)) |
| continue; |
| } |
| |
| for (; field != index; field = DECL_CHAIN (field)) |
| { |
| tree ftype; |
| if (TREE_CODE (field) != FIELD_DECL) |
| continue; |
| if (DECL_UNNAMED_BIT_FIELD (field)) |
| continue; |
| if (DECL_ARTIFICIAL (field)) |
| continue; |
| if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| { |
| /* Recurse to check the anonummous aggregate member. */ |
| bad |= cx_check_missing_mem_inits |
| (TREE_TYPE (field), NULL_TREE, complain); |
| if (bad && !complain) |
| return true; |
| continue; |
| } |
| if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field))) |
| /* An empty field doesn't need an initializer. */ |
| continue; |
| ftype = strip_array_types (TREE_TYPE (field)); |
| if (type_has_constexpr_default_constructor (ftype)) |
| { |
| /* It's OK to skip a member with a trivial constexpr ctor. |
| A constexpr ctor that isn't trivial should have been |
| added in by now. */ |
| gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) |
| || errorcount != 0); |
| continue; |
| } |
| if (!complain) |
| return true; |
| auto_diagnostic_group d; |
| error ("member %qD must be initialized by mem-initializer " |
| "in %<constexpr%> constructor", field); |
| inform (DECL_SOURCE_LOCATION (field), "declared here"); |
| bad = true; |
| } |
| if (field == NULL_TREE) |
| break; |
| |
| if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) |
| { |
| /* Check the anonymous aggregate initializer is valid. */ |
| bad |= cx_check_missing_mem_inits |
| (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); |
| if (bad && !complain) |
| return true; |
| } |
| field = DECL_CHAIN (field); |
| } |
| |
| return bad; |
| } |
| |
| /* We are processing the definition of the constexpr function FUN. |
| Check that its BODY fulfills the propriate requirements and |
| enter it in the constexpr function definition table. |
| For constructor BODY is actually the TREE_LIST of the |
| member-initializer list. */ |
| |
| tree |
| register_constexpr_fundef (tree fun, tree body) |
| { |
| constexpr_fundef entry; |
| constexpr_fundef **slot; |
| |
| if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) |
| return NULL; |
| |
| tree massaged = massage_constexpr_body (fun, body); |
| if (massaged == NULL_TREE || massaged == error_mark_node) |
| { |
| if (!DECL_CONSTRUCTOR_P (fun)) |
| error ("body of %<constexpr%> function %qD not a return-statement", |
| fun); |
| return NULL; |
| } |
| |
| if (!potential_rvalue_constant_expression (massaged)) |
| { |
| if (!DECL_GENERATED_P (fun)) |
| require_potential_rvalue_constant_expression (massaged); |
| return NULL; |
| } |
| |
| if (DECL_CONSTRUCTOR_P (fun) && !DECL_DEFAULTED_FN (fun) |
| && cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
| massaged, !DECL_GENERATED_P (fun))) |
| return NULL; |
| |
| /* Create the constexpr function table if necessary. */ |
| if (constexpr_fundef_table == NULL) |
| constexpr_fundef_table |
| = hash_table<constexpr_fundef_hasher>::create_ggc (101); |
| |
| entry.decl = fun; |
| tree saved_fn = current_function_decl; |
| bool clear_ctx = false; |
| current_function_decl = fun; |
| if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
| { |
| clear_ctx = true; |
| DECL_CONTEXT (DECL_RESULT (fun)) = fun; |
| } |
| entry.body = copy_fn (fun, entry.parms, entry.result); |
| current_function_decl = saved_fn; |
| slot = constexpr_fundef_table->find_slot (&entry, INSERT); |
| if (clear_ctx) |
| DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE; |
| |
| gcc_assert (*slot == NULL); |
| *slot = ggc_alloc<constexpr_fundef> (); |
| **slot = entry; |
| |
| return fun; |
| } |
| |
| /* FUN is a non-constexpr function called in a context that requires a |
| constant expression. If it comes from a constexpr template, explain why |
| the instantiation isn't constexpr. */ |
| |
| void |
| explain_invalid_constexpr_fn (tree fun) |
| { |
| static hash_set<tree> *diagnosed; |
| tree body; |
| location_t save_loc; |
| /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
| if (!DECL_DEFAULTED_FN (fun) |
| && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
| && !is_instantiation_of_constexpr (fun)) |
| return; |
| if (diagnosed == NULL) |
| diagnosed = new hash_set<tree>; |
| if (diagnosed->add (fun)) |
| /* Already explained. */ |
| return; |
| |
| save_loc = input_location; |
| if (!lambda_static_thunk_p (fun)) |
| { |
| /* Diagnostics should completely ignore the static thunk, so leave |
| input_location set to our caller's location. */ |
| input_location = DECL_SOURCE_LOCATION (fun); |
| inform (input_location, |
| "%qD is not usable as a %<constexpr%> function because:", fun); |
| } |
| /* First check the declaration. */ |
| if (is_valid_constexpr_fn (fun, true)) |
| { |
| /* Then if it's OK, the body. */ |
| if (!DECL_DECLARED_CONSTEXPR_P (fun) |
| && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))) |
| explain_implicit_non_constexpr (fun); |
| else |
| { |
| body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
| require_potential_rvalue_constant_expression (body); |
| if (DECL_CONSTRUCTOR_P (fun)) |
| cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
| } |
| } |
| input_location = save_loc; |
| } |
| |
| /* Objects of this type represent calls to constexpr functions |
| along with the bindings of parameters to their arguments, for |
| the purpose of compile time evaluation. */ |
| |
| struct GTY((for_user)) constexpr_call { |
| /* Description of the constexpr function definition. */ |
| constexpr_fundef *fundef; |
| /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE |
| is a parameter _DECL and the TREE_VALUE is the value of the parameter. |
| Note: This arrangement is made to accommodate the use of |
| iterative_hash_template_arg (see pt.c). If you change this |
| representation, also change the hash calculation in |
| cxx_eval_call_expression. */ |
| tree bindings; |
| /* Result of the call. |
| NULL means the call is being evaluated. |
| error_mark_node means that the evaluation was erroneous; |
| otherwise, the actuall value of the call. */ |
| tree result; |
| /* The hash of this call; we remember it here to avoid having to |
| recalculate it when expanding the hash table. */ |
| hashval_t hash; |
| /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ |
| bool manifestly_const_eval; |
| }; |
| |
| struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
| { |
| static hashval_t hash (constexpr_call *); |
| static bool equal (constexpr_call *, constexpr_call *); |
| }; |
| |
| enum constexpr_switch_state { |
| /* Used when processing a switch for the first time by cxx_eval_switch_expr |
| and default: label for that switch has not been seen yet. */ |
| css_default_not_seen, |
| /* Used when processing a switch for the first time by cxx_eval_switch_expr |
| and default: label for that switch has been seen already. */ |
| css_default_seen, |
| /* Used when processing a switch for the second time by |
| cxx_eval_switch_expr, where default: label should match. */ |
| css_default_processing |
| }; |
| |
| /* The constexpr expansion context. CALL is the current function |
| expansion, CTOR is the current aggregate initializer, OBJECT is the |
| object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES |
| is a map of values of variables initialized within the expression. */ |
| |
| struct constexpr_ctx { |
| /* The innermost call we're evaluating. */ |
| constexpr_call *call; |
| /* Values for any temporaries or local variables within the |
| constant-expression. */ |
| hash_map<tree,tree> *values; |
| /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we |
| aren't inside a loop. */ |
| vec<tree> *save_exprs; |
| /* The CONSTRUCTOR we're currently building up for an aggregate |
| initializer. */ |
| tree ctor; |
| /* The object we're building the CONSTRUCTOR for. */ |
| tree object; |
| /* If inside SWITCH_EXPR. */ |
| constexpr_switch_state *css_state; |
| /* Number of cxx_eval_constant_expression calls (except skipped ones, |
| on simple constants or location wrappers) encountered during current |
| cxx_eval_outermost_constant_expr call. */ |
| HOST_WIDE_INT *constexpr_ops_count; |
| |
| /* Whether we should error on a non-constant expression or fail quietly. */ |
| bool quiet; |
| /* Whether we are strictly conforming to constant expression rules or |
| trying harder to get a constant value. */ |
| bool strict; |
| /* Whether __builtin_is_constant_evaluated () should be true. */ |
| bool manifestly_const_eval; |
| }; |
| |
| /* A table of all constexpr calls that have been evaluated by the |
| compiler in this translation unit. */ |
| |
| static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
| |
| static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
| bool, bool *, bool *, tree * = NULL); |
| |
| /* Compute a hash value for a constexpr call representation. */ |
| |
| inline hashval_t |
| constexpr_call_hasher::hash (constexpr_call *info) |
| { |
| return info->hash; |
| } |
| |
| /* Return true if the objects pointed to by P and Q represent calls |
| to the same constexpr function with the same arguments. |
| Otherwise, return false. */ |
| |
| bool |
| constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) |
| { |
| tree lhs_bindings; |
| tree rhs_bindings; |
| if (lhs == rhs) |
| return true; |
| if (lhs->hash != rhs->hash) |
| return false; |
| if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
| return false; |
| if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
| return false; |
| lhs_bindings = lhs->bindings; |
| rhs_bindings = rhs->bindings; |
| while (lhs_bindings != NULL && rhs_bindings != NULL) |
| { |
| tree lhs_arg = TREE_VALUE (lhs_bindings); |
| tree rhs_arg = TREE_VALUE (rhs_bindings); |
| gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (lhs_arg), TREE_TYPE (rhs_arg))); |
| if (!cp_tree_equal (lhs_arg, rhs_arg)) |
| return false; |
| lhs_bindings = TREE_CHAIN (lhs_bindings); |
| rhs_bindings = TREE_CHAIN (rhs_bindings); |
| } |
| return lhs_bindings == rhs_bindings; |
| } |
| |
| /* Initialize the constexpr call table, if needed. */ |
| |
| static void |
| maybe_initialize_constexpr_call_table (void) |
| { |
| if (constexpr_call_table == NULL) |
| constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); |
| } |
| |
| /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
| a function happens to get called recursively, we unshare the callee |
| function's body and evaluate this unshared copy instead of evaluating the |
| original body. |
| |
| FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function |
| copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map |
| that's keyed off of the original FUNCTION_DECL and whose value is a |
| TREE_LIST of this function's unused copies awaiting reuse. |
| |
| This is not GC-deletable to avoid GC affecting UID generation. */ |
| |
| static GTY(()) hash_map<tree, tree> *fundef_copies_table; |
| |
| /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */ |
| |
| static void |
| maybe_initialize_fundef_copies_table () |
| { |
| if (fundef_copies_table == NULL) |
| fundef_copies_table = hash_map<tree,tree>::create_ggc (101); |
| } |
| |
| /* Reuse a copy or create a new unshared copy of the function FUN. |
| Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
| is parms, TYPE is result. */ |
| |
| static tree |
| get_fundef_copy (constexpr_fundef *fundef) |
| { |
| maybe_initialize_fundef_copies_table (); |
| |
| tree copy; |
| bool existed; |
| tree *slot = &fundef_copies_table->get_or_insert (fundef->decl, &existed); |
| |
| if (!existed) |
| { |
| /* There is no cached function available, or in use. We can use |
| the function directly. That the slot is now created records |
| that this function is now in use. */ |
| copy = build_tree_list (fundef->body, fundef->parms); |
| TREE_TYPE (copy) = fundef->result; |
| } |
| else if (*slot == NULL_TREE) |
| { |
| /* We've already used the function itself, so make a copy. */ |
| copy = build_tree_list (NULL, NULL); |
| tree saved_body = DECL_SAVED_TREE (fundef->decl); |
| tree saved_parms = DECL_ARGUMENTS (fundef->decl); |
| tree saved_result = DECL_RESULT (fundef->decl); |
| tree saved_fn = current_function_decl; |
| DECL_SAVED_TREE (fundef->decl) = fundef->body; |
| DECL_ARGUMENTS (fundef->decl) = fundef->parms; |
| DECL_RESULT (fundef->decl) = fundef->result; |
| current_function_decl = fundef->decl; |
| TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), |
| TREE_TYPE (copy)); |
| current_function_decl = saved_fn; |
| DECL_RESULT (fundef->decl) = saved_result; |
| DECL_ARGUMENTS (fundef->decl) = saved_parms; |
| DECL_SAVED_TREE (fundef->decl) = saved_body; |
| } |
| else |
| { |
| /* We have a cached function available. */ |
| copy = *slot; |
| *slot = TREE_CHAIN (copy); |
| } |
| |
| return copy; |
| } |
| |
| /* Save the copy COPY of function FUN for later reuse by |
| get_fundef_copy(). By construction, there will always be an entry |
| to find. */ |
| |
| static void |
| save_fundef_copy (tree fun, tree copy) |
| { |
| tree *slot = fundef_copies_table->get (fun); |
| TREE_CHAIN (copy) = *slot; |
| *slot = copy; |
| } |
| |
| /* We have an expression tree T that represents a call, either CALL_EXPR |
| or AGGR_INIT_EXPR. Return the Nth argument. */ |
| |
| static inline tree |
| get_nth_callarg (tree t, int n) |
| { |
| switch (TREE_CODE (t)) |
| { |
| case CALL_EXPR: |
| return CALL_EXPR_ARG (t, n); |
| |
| case AGGR_INIT_EXPR: |
| return AGGR_INIT_EXPR_ARG (t, n); |
| |
| default: |
| gcc_unreachable (); |
| return NULL; |
| } |
| } |
| |
| /* Attempt to evaluate T which represents a call to a builtin function. |
| We assume here that all builtin functions evaluate to scalar types |
| represented by _CST nodes. */ |
| |
| static tree |
| cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| const int nargs = call_expr_nargs (t); |
| tree *args = (tree *) alloca (nargs * sizeof (tree)); |
| tree new_call; |
| int i; |
| |
| /* Don't fold __builtin_constant_p within a constexpr function. */ |
| bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
| |
| /* If we aren't requiring a constant expression, defer __builtin_constant_p |
| in a constexpr function until we have values for the parameters. */ |
| if (bi_const_p |
| && !ctx->manifestly_const_eval |
| && current_function_decl |
| && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) |
| { |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* For __builtin_is_constant_evaluated, defer it if not |
| ctx->manifestly_const_eval, otherwise fold it to true. */ |
| if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
| BUILT_IN_FRONTEND)) |
| { |
| if (!ctx->manifestly_const_eval) |
| { |
| *non_constant_p = true; |
| return t; |
| } |
| return boolean_true_node; |
| } |
| |
| /* Be permissive for arguments to built-ins; __builtin_constant_p should |
| return constant false for a non-constant argument. */ |
| constexpr_ctx new_ctx = *ctx; |
| new_ctx.quiet = true; |
| for (i = 0; i < nargs; ++i) |
| { |
| args[i] = CALL_EXPR_ARG (t, i); |
| /* If builtin_valid_in_constant_expr_p is true, |
| potential_constant_expression_1 has not recursed into the arguments |
| of the builtin, verify it here. */ |
| if (!builtin_valid_in_constant_expr_p (fun) |
| || potential_constant_expression (args[i])) |
| { |
| bool dummy1 = false, dummy2 = false; |
| args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false, |
| &dummy1, &dummy2); |
| } |
| |
| if (bi_const_p) |
| /* For __builtin_constant_p, fold all expressions with constant values |
| even if they aren't C++ constant-expressions. */ |
| args[i] = cp_fold_rvalue (args[i]); |
| } |
| |
| bool save_ffbcp = force_folding_builtin_constant_p; |
| force_folding_builtin_constant_p = true; |
| tree save_cur_fn = current_function_decl; |
| /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ |
| if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) |
| && ctx->call |
| && ctx->call->fundef) |
| current_function_decl = ctx->call->fundef->decl; |
| new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), |
| CALL_EXPR_FN (t), nargs, args); |
| current_function_decl = save_cur_fn; |
| force_folding_builtin_constant_p = save_ffbcp; |
| if (new_call == NULL) |
| { |
| if (!*non_constant_p && !ctx->quiet) |
| { |
| /* Do not allow__builtin_unreachable in constexpr function. |
| The __builtin_unreachable call with BUILTINS_LOCATION |
| comes from cp_maybe_instrument_return. */ |
| if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) |
| && EXPR_LOCATION (t) == BUILTINS_LOCATION) |
| error ("%<constexpr%> call flows off the end of the function"); |
| else |
| { |
| new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), |
| CALL_EXPR_FN (t), nargs, args); |
| error ("%q+E is not a constant expression", new_call); |
| } |
| } |
| *non_constant_p = true; |
| return t; |
| } |
| |
| if (!potential_constant_expression (new_call)) |
| { |
| if (!*non_constant_p && !ctx->quiet) |
| error ("%q+E is not a constant expression", new_call); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
| non_constant_p, overflow_p); |
| } |
| |
| /* TEMP is the constant value of a temporary object of type TYPE. Adjust |
| the type of the value to match. */ |
| |
| static tree |
| adjust_temp_type (tree type, tree temp) |
| { |
| if (same_type_p (TREE_TYPE (temp), type)) |
| return temp; |
| /* Avoid wrapping an aggregate value in a NOP_EXPR. */ |
| if (TREE_CODE (temp) == CONSTRUCTOR) |
| { |
| /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ |
| tree t = copy_node (temp); |
| TREE_TYPE (t) = type; |
| return t; |
| } |
| if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
| return build0 (EMPTY_CLASS_EXPR, type); |
| gcc_assert (scalarish_type_p (type)); |
| /* Now we know we're dealing with a scalar, and a prvalue of non-class |
| type is cv-unqualified. */ |
| return cp_fold_convert (cv_unqualified (type), temp); |
| } |
| |
| /* Callback for walk_tree used by unshare_constructor. */ |
| |
| static tree |
| find_constructor (tree *tp, int *walk_subtrees, void *) |
| { |
| if (TYPE_P (*tp)) |
| *walk_subtrees = 0; |
| if (TREE_CODE (*tp) == CONSTRUCTOR) |
| return *tp; |
| return NULL_TREE; |
| } |
| |
| /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a |
| subexpression, return an unshared copy of T. Otherwise return T. */ |
| |
| tree |
| unshare_constructor (tree t) |
| { |
| tree ctor = walk_tree (&t, find_constructor, NULL, NULL); |
| if (ctor != NULL_TREE) |
| return unshare_expr (t); |
| return t; |
| } |
| |
| /* Subroutine of cxx_eval_call_expression. |
| We are processing a call expression (either CALL_EXPR or |
| AGGR_INIT_EXPR) in the context of CTX. Evaluate |
| all arguments and bind their values to correspondings |
| parameters, making up the NEW_CALL context. */ |
| |
| static void |
| cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, |
| constexpr_call *new_call, |
| bool *non_constant_p, bool *overflow_p, |
| bool *non_constant_args) |
| { |
| const int nargs = call_expr_nargs (t); |
| tree fun = new_call->fundef->decl; |
| tree parms = new_call->fundef->parms; |
| int i; |
| tree *p = &new_call->bindings; |
| for (i = 0; i < nargs; ++i) |
| { |
| tree x, arg; |
| tree type = parms ? TREE_TYPE (parms) : void_type_node; |
| x = get_nth_callarg (t, i); |
| /* For member function, the first argument is a pointer to the implied |
| object. For a constructor, it might still be a dummy object, in |
| which case we get the real argument from ctx. */ |
| if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
| && is_dummy_object (x)) |
| { |
| x = ctx->object; |
| x = build_address (x); |
| } |
| if (TREE_ADDRESSABLE (type)) |
| /* Undo convert_for_arg_passing work here. */ |
| x = convert_from_reference (x); |
| arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, |
| non_constant_p, overflow_p); |
| /* Don't VERIFY_CONSTANT here. */ |
| if (*non_constant_p && ctx->quiet) |
| return; |
| /* Just discard ellipsis args after checking their constantitude. */ |
| if (!parms) |
| continue; |
| |
| if (!*non_constant_p) |
| { |
| /* Don't share a CONSTRUCTOR that might be changed. */ |
| arg = unshare_constructor (arg); |
| /* Make sure the binding has the same type as the parm. But |
| only for constant args. */ |
| if (!TYPE_REF_P (type)) |
| arg = adjust_temp_type (type, arg); |
| if (!TREE_CONSTANT (arg)) |
| *non_constant_args = true; |
| |
| /* For virtual calls, adjust the this argument, so that it is |
| the object on which the method is called, rather than |
| one of its bases. */ |
| if (i == 0 && DECL_VIRTUAL_P (fun)) |
| { |
| tree addr = arg; |
| STRIP_NOPS (addr); |
| if (TREE_CODE (addr) == ADDR_EXPR) |
| { |
| tree obj = TREE_OPERAND (addr, 0); |
| while (TREE_CODE (obj) == COMPONENT_REF |
| && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) |
| && !same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (obj), DECL_CONTEXT (fun))) |
| obj = TREE_OPERAND (obj, 0); |
| if (obj != TREE_OPERAND (addr, 0)) |
| arg = build_fold_addr_expr_with_type (obj, |
| TREE_TYPE (arg)); |
| } |
| } |
| |
| *p = build_tree_list (parms, arg); |
| p = &TREE_CHAIN (*p); |
| } |
| parms = TREE_CHAIN (parms); |
| } |
| } |
| |
| /* Variables and functions to manage constexpr call expansion context. |
| These do not need to be marked for PCH or GC. */ |
| |
| /* FIXME remember and print actual constant arguments. */ |
| static vec<tree> call_stack; |
| static int call_stack_tick; |
| static int last_cx_error_tick; |
| |
| static bool |
| push_cx_call_context (tree call) |
| { |
| ++call_stack_tick; |
| if (!EXPR_HAS_LOCATION (call)) |
| SET_EXPR_LOCATION (call, input_location); |
| call_stack.safe_push (call); |
| if (call_stack.length () > (unsigned) max_constexpr_depth) |
| return false; |
| return true; |
| } |
| |
| static void |
| pop_cx_call_context (void) |
| { |
| ++call_stack_tick; |
| call_stack.pop (); |
| } |
| |
| vec<tree> |
| cx_error_context (void) |
| { |
| vec<tree> r = vNULL; |
| if (call_stack_tick != last_cx_error_tick |
| && !call_stack.is_empty ()) |
| r = call_stack; |
| last_cx_error_tick = call_stack_tick; |
| return r; |
| } |
| |
| /* Evaluate a call T to a GCC internal function when possible and return |
| the evaluated result or, under the control of CTX, give an error, set |
| NON_CONSTANT_P, and return the unevaluated call T otherwise. */ |
| |
| static tree |
| cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| enum tree_code opcode = ERROR_MARK; |
| |
| switch (CALL_EXPR_IFN (t)) |
| { |
| case IFN_UBSAN_NULL: |
| case IFN_UBSAN_BOUNDS: |
| case IFN_UBSAN_VPTR: |
| case IFN_FALLTHROUGH: |
| return void_node; |
| |
| case IFN_ADD_OVERFLOW: |
| opcode = PLUS_EXPR; |
| break; |
| case IFN_SUB_OVERFLOW: |
| opcode = MINUS_EXPR; |
| break; |
| case IFN_MUL_OVERFLOW: |
| opcode = MULT_EXPR; |
| break; |
| |
| case IFN_LAUNDER: |
| return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
| false, non_constant_p, overflow_p); |
| |
| case IFN_VEC_CONVERT: |
| { |
| tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
| false, non_constant_p, |
| overflow_p); |
| if (TREE_CODE (arg) == VECTOR_CST) |
| if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
| return r; |
| } |
| /* FALLTHRU */ |
| |
| default: |
| if (!ctx->quiet) |
| error_at (cp_expr_loc_or_loc (t, input_location), |
| "call to internal function %qE", t); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* Evaluate constant arguments using OPCODE and return a complex |
| number containing the result and the overflow bit. */ |
| tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, |
| non_constant_p, overflow_p); |
| tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, |
| non_constant_p, overflow_p); |
| |
| if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) |
| { |
| location_t loc = cp_expr_loc_or_loc (t, input_location); |
| tree type = TREE_TYPE (TREE_TYPE (t)); |
| tree result = fold_binary_loc (loc, opcode, type, |
| fold_convert_loc (loc, type, arg0), |
| fold_convert_loc (loc, type, arg1)); |
| tree ovf |
| = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); |
| /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ |
| if (TREE_OVERFLOW (result)) |
| TREE_OVERFLOW (result) = 0; |
| |
| return build_complex (TREE_TYPE (t), result, ovf); |
| } |
| |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
| |
| static void |
| clear_no_implicit_zero (tree ctor) |
| { |
| if (CONSTRUCTOR_NO_CLEARING (ctor)) |
| { |
| CONSTRUCTOR_NO_CLEARING (ctor) = false; |
| tree elt; unsigned HOST_WIDE_INT idx; |
| FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt) |
| if (TREE_CODE (elt) == CONSTRUCTOR) |
| clear_no_implicit_zero (elt); |
| } |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Evaluate the call expression tree T in the context of OLD_CALL expression |
| evaluation. */ |
| |
| static tree |
| cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| location_t loc = cp_expr_loc_or_loc (t, input_location); |
| tree fun = get_function_named_in_call (t); |
| constexpr_call new_call |
| = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
| bool depth_ok; |
| |
| if (fun == NULL_TREE) |
| return cxx_eval_internal_function (ctx, t, lval, |
| non_constant_p, overflow_p); |
| |
| if (TREE_CODE (fun) != FUNCTION_DECL) |
| { |
| /* Might be a constexpr function pointer. */ |
| fun = cxx_eval_constant_expression (ctx, fun, |
| /*lval*/false, non_constant_p, |
| overflow_p); |
| STRIP_NOPS (fun); |
| if (TREE_CODE (fun) == ADDR_EXPR) |
| fun = TREE_OPERAND (fun, 0); |
| /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
| indirection, the called expression is a pointer into the |
| virtual table which should contain FDESC_EXPR. Extract the |
| FUNCTION_DECL from there. */ |
| else if (TARGET_VTABLE_USES_DESCRIPTORS |
| && TREE_CODE (fun) == POINTER_PLUS_EXPR |
| && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR |
| && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) |
| { |
| tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); |
| if (VAR_P (d) |
| && DECL_VTABLE_OR_VTT_P (d) |
| && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE |
| && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type |
| && DECL_INITIAL (d) |
| && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) |
| { |
| tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), |
| TYPE_SIZE_UNIT (vtable_entry_type)); |
| HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); |
| if (idx >= 0) |
| { |
| tree fdesc |
| = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; |
| if (TREE_CODE (fdesc) == FDESC_EXPR |
| && integer_zerop (TREE_OPERAND (fdesc, 1))) |
| fun = TREE_OPERAND (fdesc, 0); |
| } |
| } |
| } |
| } |
| if (TREE_CODE (fun) != FUNCTION_DECL) |
| { |
| if (!ctx->quiet && !*non_constant_p) |
| error_at (loc, "expression %qE does not designate a %<constexpr%> " |
| "function", fun); |
| *non_constant_p = true; |
| return t; |
| } |
| if (DECL_CLONED_FUNCTION_P (fun)) |
| fun = DECL_CLONED_FUNCTION (fun); |
| |
| if (is_ubsan_builtin_p (fun)) |
| return void_node; |
| |
| if (fndecl_built_in_p (fun)) |
| return cxx_eval_builtin_function_call (ctx, t, fun, |
| lval, non_constant_p, overflow_p); |
| if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
| { |
| if (!ctx->quiet) |
| { |
| if (!lambda_static_thunk_p (fun)) |
| error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
| explain_invalid_constexpr_fn (fun); |
| } |
| *non_constant_p = true; |
| return t; |
| } |
| |
| constexpr_ctx new_ctx = *ctx; |
| if (DECL_CONSTRUCTOR_P (fun) && !ctx->object |
| && TREE_CODE (t) == AGGR_INIT_EXPR) |
| { |
| /* We want to have an initialization target for an AGGR_INIT_EXPR. |
| If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ |
| new_ctx.object = AGGR_INIT_EXPR_SLOT (t); |
| tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); |
| CONSTRUCTOR_NO_CLEARING (ctor) = true; |
| ctx->values->put (new_ctx.object, ctor); |
| ctx = &new_ctx; |
| } |
| |
| /* Shortcut trivial constructor/op=. */ |
| if (trivial_fn_p (fun)) |
| { |
| tree init = NULL_TREE; |
| if (call_expr_nargs (t) == 2) |
| init = convert_from_reference (get_nth_callarg (t, 1)); |
| else if (TREE_CODE (t) == AGGR_INIT_EXPR |
| && AGGR_INIT_ZERO_FIRST (t)) |
| init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
| if (init) |
| { |
| tree op = get_nth_callarg (t, 0); |
| if (is_dummy_object (op)) |
| op = ctx->object; |
| else |
| op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); |
| tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); |
| new_ctx.call = &new_call; |
| return cxx_eval_constant_expression (&new_ctx, set, lval, |
| non_constant_p, overflow_p); |
| } |
| } |
| |
| /* We can't defer instantiating the function any longer. */ |
| if (!DECL_INITIAL (fun) |
| && DECL_TEMPLOID_INSTANTIATION (fun)) |
| { |
| location_t save_loc = input_location; |
| input_location = loc; |
| ++function_depth; |
| instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
| --function_depth; |
| input_location = save_loc; |
| } |
| |
| /* If in direct recursive call, optimize definition search. */ |
| if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
| new_call.fundef = ctx->call->fundef; |
| else |
| { |
| new_call.fundef = retrieve_constexpr_fundef (fun); |
| if (new_call.fundef == NULL || new_call.fundef->body == NULL |
| || fun == current_function_decl) |
| { |
| if (!ctx->quiet) |
| { |
| /* We need to check for current_function_decl here in case we're |
| being called during cp_fold_function, because at that point |
| DECL_INITIAL is set properly and we have a fundef but we |
| haven't lowered invisirefs yet (c++/70344). */ |
| if (DECL_INITIAL (fun) == error_mark_node |
| || fun == current_function_decl) |
| error_at (loc, "%qD called in a constant expression before its " |
| "definition is complete", fun); |
| else if (DECL_INITIAL (fun)) |
| { |
| /* The definition of fun was somehow unsuitable. But pretend |
| that lambda static thunks don't exist. */ |
| if (!lambda_static_thunk_p (fun)) |
| error_at (loc, "%qD called in a constant expression", fun); |
| explain_invalid_constexpr_fn (fun); |
| } |
| else |
| error_at (loc, "%qD used before its definition", fun); |
| } |
| *non_constant_p = true; |
| return t; |
| } |
| } |
| |
| bool non_constant_args = false; |
| cxx_bind_parameters_in_call (ctx, t, &new_call, |
| non_constant_p, overflow_p, &non_constant_args); |
| if (*non_constant_p) |
| return t; |
| |
| depth_ok = push_cx_call_context (t); |
| |
| tree result = NULL_TREE; |
| |
| constexpr_call *entry = NULL; |
| if (depth_ok && !non_constant_args && ctx->strict) |
| { |
| new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
| new_call.hash |
| = iterative_hash_template_arg (new_call.bindings, new_call.hash); |
| new_call.hash |
| = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
| |
| /* If we have seen this call before, we are done. */ |
| maybe_initialize_constexpr_call_table (); |
| constexpr_call **slot |
| = constexpr_call_table->find_slot (&new_call, INSERT); |
| entry = *slot; |
| if (entry == NULL) |
| { |
| /* We need to keep a pointer to the entry, not just the slot, as the |
| slot can move in the call to cxx_eval_builtin_function_call. */ |
| *slot = entry = ggc_alloc<constexpr_call> (); |
| *entry = new_call; |
| } |
| /* Calls that are in progress have their result set to NULL, |
| so that we can detect circular dependencies. */ |
| else if (entry->result == NULL) |
| { |
| if (!ctx->quiet) |
| error ("call has circular dependency"); |
| *non_constant_p = true; |
| entry->result = result = error_mark_node; |
| } |
| else |
| result = entry->result; |
| } |
| |
| if (!depth_ok) |
| { |
| if (!ctx->quiet) |
| error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
| "%<-fconstexpr-depth=%> to increase the maximum)", |
| max_constexpr_depth); |
| *non_constant_p = true; |
| result = error_mark_node; |
| } |
| else |
| { |
| if (result && result != error_mark_node) |
| /* OK */; |
| else if (!DECL_SAVED_TREE (fun)) |
| { |
| /* When at_eof >= 2, cgraph has started throwing away |
| DECL_SAVED_TREE, so fail quietly. FIXME we get here because of |
| late code generation for VEC_INIT_EXPR, which needs to be |
| completely reconsidered. */ |
| gcc_assert (at_eof >= 2 && ctx->quiet); |
| *non_constant_p = true; |
| } |
| else |
| { |
| tree body, parms, res; |
| |
| /* Reuse or create a new unshared copy of this function's body. */ |
| tree copy = get_fundef_copy (new_call.fundef); |
| body = TREE_PURPOSE (copy); |
| parms = TREE_VALUE (copy); |
| res = TREE_TYPE (copy); |
| |
| /* Associate the bindings with the remapped parms. */ |
| tree bound = new_call.bindings; |
| tree remapped = parms; |
| while (bound) |
| { |
| tree oparm = TREE_PURPOSE (bound); |
| tree arg = TREE_VALUE (bound); |
| gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm)); |
| /* Don't share a CONSTRUCTOR that might be changed. */ |
| arg = unshare_constructor (arg); |
| ctx->values->put (remapped, arg); |
| bound = TREE_CHAIN (bound); |
| remapped = DECL_CHAIN (remapped); |
| } |
| /* Add the RESULT_DECL to the values map, too. */ |
| tree slot = NULL_TREE; |
| if (DECL_BY_REFERENCE (res)) |
| { |
| slot = AGGR_INIT_EXPR_SLOT (t); |
| tree addr = build_address (slot); |
| addr = build_nop (TREE_TYPE (res), addr); |
| ctx->values->put (res, addr); |
| ctx->values->put (slot, NULL_TREE); |
| } |
| else |
| ctx->values->put (res, NULL_TREE); |
| |
| /* Track the callee's evaluated SAVE_EXPRs so that we can forget |
| their values after the call. */ |
| constexpr_ctx ctx_with_save_exprs = *ctx; |
| auto_vec<tree, 10> save_exprs; |
| ctx_with_save_exprs.save_exprs = &save_exprs; |
| ctx_with_save_exprs.call = &new_call; |
| |
| tree jump_target = NULL_TREE; |
| cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
| lval, non_constant_p, overflow_p, |
| &jump_target); |
| |
| if (DECL_CONSTRUCTOR_P (fun)) |
| /* This can be null for a subobject constructor call, in |
| which case what we care about is the initialization |
| side-effects rather than the value. We could get at the |
| value by evaluating *this, but we don't bother; there's |
| no need to put such a call in the hash table. */ |
| result = lval ? ctx->object : ctx->ctor; |
| else if (VOID_TYPE_P (TREE_TYPE (res))) |
| result = void_node; |
| else |
| { |
| result = *ctx->values->get (slot ? slot : res); |
| if (result == NULL_TREE && !*non_constant_p) |
| { |
| if (!ctx->quiet) |
| error ("%<constexpr%> call flows off the end " |
| "of the function"); |
| *non_constant_p = true; |
| } |
| } |
| |
| /* Forget the saved values of the callee's SAVE_EXPRs. */ |
| unsigned int i; |
| tree save_expr; |
| FOR_EACH_VEC_ELT (save_exprs, i, save_expr) |
| ctx_with_save_exprs.values->remove (save_expr); |
| |
| /* Remove the parms/result from the values map. Is it worth |
| bothering to do this when the map itself is only live for |
| one constexpr evaluation? If so, maybe also clear out |
| other vars from call, maybe in BIND_EXPR handling? */ |
| ctx->values->remove (res); |
| if (slot) |
| ctx->values->remove (slot); |
| for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
| ctx->values->remove (parm); |
| |
| /* Make the unshared function copy we used available for re-use. */ |
| save_fundef_copy (fun, copy); |
| } |
| |
| if (result == error_mark_node) |
| *non_constant_p = true; |
| if (*non_constant_p || *overflow_p) |
| result = error_mark_node; |
| else if (!result) |
| result = void_node; |
| if (entry) |
| entry->result = result; |
| } |
| |
| /* The result of a constexpr function must be completely initialized. */ |
| if (TREE_CODE (result) == CONSTRUCTOR) |
| clear_no_implicit_zero (result); |
| |
| pop_cx_call_context (); |
| return unshare_constructor (result); |
| } |
| |
| /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ |
| |
| bool |
| reduced_constant_expression_p (tree t) |
| { |
| if (t == NULL_TREE) |
| return false; |
| |
| switch (TREE_CODE (t)) |
| { |
| case PTRMEM_CST: |
| /* Even if we can't lower this yet, it's constant. */ |
| return true; |
| |
| case CONSTRUCTOR: |
| /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ |
| tree idx, val, field; unsigned HOST_WIDE_INT i; |
| if (CONSTRUCTOR_NO_CLEARING (t)) |
| { |
| if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) |
| /* An initialized vector would have a VECTOR_CST. */ |
| return false; |
| else |
| field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); |
| } |
| else |
| field = NULL_TREE; |
| FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val) |
| { |
| /* If VAL is null, we're in the middle of initializing this |
| element. */ |
| if (!reduced_constant_expression_p (val)) |
| return false; |
| if (field) |
| { |
| if (idx != field) |
| return false; |
| field = next_initializable_field (DECL_CHAIN (field)); |
| } |
| } |
| if (field) |
| return false; |
| else if (CONSTRUCTOR_NO_CLEARING (t)) |
| /* All the fields are initialized. */ |
| CONSTRUCTOR_NO_CLEARING (t) = false; |
| return true; |
| |
| default: |
| /* FIXME are we calling this too much? */ |
| return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; |
| } |
| } |
| |
| /* Some expressions may have constant operands but are not constant |
| themselves, such as 1/0. Call this function to check for that |
| condition. |
| |
| We only call this in places that require an arithmetic constant, not in |
| places where we might have a non-constant expression that can be a |
| component of a constant expression, such as the address of a constexpr |
| variable that might be dereferenced later. */ |
| |
| static bool |
| verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, |
| bool *overflow_p) |
| { |
| if (!*non_constant_p && !reduced_constant_expression_p (t)) |
| { |
| if (!allow_non_constant) |
| error ("%q+E is not a constant expression", t); |
| *non_constant_p = true; |
| } |
| if (TREE_OVERFLOW_P (t)) |
| { |
| if (!allow_non_constant) |
| { |
| permerror (input_location, "overflow in constant expression"); |
| /* If we're being permissive (and are in an enforcing |
| context), ignore the overflow. */ |
| if (flag_permissive) |
| return *non_constant_p; |
| } |
| *overflow_p = true; |
| } |
| return *non_constant_p; |
| } |
| |
| /* Check whether the shift operation with code CODE and type TYPE on LHS |
| and RHS is undefined. If it is, give an error with an explanation, |
| and return true; return false otherwise. */ |
| |
| static bool |
| cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, |
| enum tree_code code, tree type, tree lhs, tree rhs) |
| { |
| if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) |
| || TREE_CODE (lhs) != INTEGER_CST |
| || TREE_CODE (rhs) != INTEGER_CST) |
| return false; |
| |
| tree lhstype = TREE_TYPE (lhs); |
| unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); |
| |
| /* [expr.shift] The behavior is undefined if the right operand |
| is negative, or greater than or equal to the length in bits |
| of the promoted left operand. */ |
| if (tree_int_cst_sgn (rhs) == -1) |
| { |
| if (!ctx->quiet) |
| permerror (loc, "right operand of shift expression %q+E is negative", |
| build2_loc (loc, code, type, lhs, rhs)); |
| return (!flag_permissive || ctx->quiet); |
| } |
| if (compare_tree_int (rhs, uprec) >= 0) |
| { |
| if (!ctx->quiet) |
| permerror (loc, "right operand of shift expression %q+E is >= than " |
| "the precision of the left operand", |
| build2_loc (loc, code, type, lhs, rhs)); |
| return (!flag_permissive || ctx->quiet); |
| } |
| |
| /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] |
| if E1 has a signed type and non-negative value, and E1x2^E2 is |
| representable in the corresponding unsigned type of the result type, |
| then that value, converted to the result type, is the resulting value; |
| otherwise, the behavior is undefined. |
| For C++2a: |
| The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
| 2^N, where N is the range exponent of the type of the result. */ |
| if (code == LSHIFT_EXPR |
| && !TYPE_OVERFLOW_WRAPS (lhstype) |
| && cxx_dialect >= cxx11 |
| && cxx_dialect < cxx2a) |
| { |
| if (tree_int_cst_sgn (lhs) == -1) |
| { |
| if (!ctx->quiet) |
| permerror (loc, |
| "left operand of shift expression %q+E is negative", |
| build2_loc (loc, code, type, lhs, rhs)); |
| return (!flag_permissive || ctx->quiet); |
| } |
| /* For signed x << y the following: |
| (unsigned) x >> ((prec (lhs) - 1) - y) |
| if > 1, is undefined. The right-hand side of this formula |
| is the highest bit of the LHS that can be set (starting from 0), |
| so that the shift doesn't overflow. We then right-shift the LHS |
| to see whether any other bit is set making the original shift |
| undefined -- the result is not representable in the corresponding |
| unsigned type. */ |
| tree t = build_int_cst (unsigned_type_node, uprec - 1); |
| t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); |
| tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); |
| t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); |
| if (tree_int_cst_lt (integer_one_node, t)) |
| { |
| if (!ctx->quiet) |
| permerror (loc, "shift expression %q+E overflows", |
| build2_loc (loc, code, type, lhs, rhs)); |
| return (!flag_permissive || ctx->quiet); |
| } |
| } |
| return false; |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to reduce the unary expression tree T to a compile time value. |
| If successful, return the value. Otherwise issue a diagnostic |
| and return error_mark_node. */ |
| |
| static tree |
| cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
| bool /*lval*/, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree r; |
| tree orig_arg = TREE_OPERAND (t, 0); |
| tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (arg); |
| location_t loc = EXPR_LOCATION (t); |
| enum tree_code code = TREE_CODE (t); |
| tree type = TREE_TYPE (t); |
| r = fold_unary_loc (loc, code, type, arg); |
| if (r == NULL_TREE) |
| { |
| if (arg == orig_arg) |
| r = t; |
| else |
| r = build1_loc (loc, code, type, arg); |
| } |
| VERIFY_CONSTANT (r); |
| return r; |
| } |
| |
| /* Helper function for cxx_eval_binary_expression. Try to optimize |
| original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the |
| generic folding should be used. */ |
| |
| static tree |
| cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, |
| tree lhs, tree rhs, bool *non_constant_p, |
| bool *overflow_p) |
| { |
| STRIP_NOPS (lhs); |
| if (TREE_CODE (lhs) != ADDR_EXPR) |
| return NULL_TREE; |
| |
| lhs = TREE_OPERAND (lhs, 0); |
| |
| /* &A[i] p+ j => &A[i + j] */ |
| if (TREE_CODE (lhs) == ARRAY_REF |
| && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST |
| && TREE_CODE (rhs) == INTEGER_CST |
| && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) |
| && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) |
| { |
| tree orig_type = TREE_TYPE (t); |
| location_t loc = EXPR_LOCATION (t); |
| tree type = TREE_TYPE (lhs); |
| |
| t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); |
| tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); |
| nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, |
| overflow_p); |
| if (*non_constant_p) |
| return NULL_TREE; |
| /* Don't fold an out-of-bound access. */ |
| if (!tree_int_cst_le (t, nelts)) |
| return NULL_TREE; |
| rhs = cp_fold_convert (ssizetype, rhs); |
| /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. |
| constexpr int A[1]; ... (char *)&A[0] + 1 */ |
| if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, |
| rhs, TYPE_SIZE_UNIT (type)))) |
| return NULL_TREE; |
| /* Make sure to treat the second operand of POINTER_PLUS_EXPR |
| as signed. */ |
| rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, |
| TYPE_SIZE_UNIT (type)); |
| t = size_binop_loc (loc, PLUS_EXPR, rhs, t); |
| t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), |
| t, NULL_TREE, NULL_TREE); |
| t = cp_build_addr_expr (t, tf_warning_or_error); |
| t = cp_fold_convert (orig_type, t); |
| return cxx_eval_constant_expression (ctx, t, /*lval*/false, |
| non_constant_p, overflow_p); |
| } |
| |
| return NULL_TREE; |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Like cxx_eval_unary_expression, except for binary expressions. */ |
| |
| static tree |
| cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
| bool /*lval*/, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree r = NULL_TREE; |
| tree orig_lhs = TREE_OPERAND (t, 0); |
| tree orig_rhs = TREE_OPERAND (t, 1); |
| tree lhs, rhs; |
| lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
| non_constant_p, overflow_p); |
| /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
| subtraction. */ |
| if (*non_constant_p) |
| return t; |
| rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
| non_constant_p, overflow_p); |
| if (*non_constant_p) |
| return t; |
| |
| location_t loc = EXPR_LOCATION (t); |
| enum tree_code code = TREE_CODE (t); |
| tree type = TREE_TYPE (t); |
| |
| if (code == EQ_EXPR || code == NE_EXPR) |
| { |
| bool is_code_eq = (code == EQ_EXPR); |
| |
| if (TREE_CODE (lhs) == PTRMEM_CST |
| && TREE_CODE (rhs) == PTRMEM_CST) |
| { |
| tree lmem = PTRMEM_CST_MEMBER (lhs); |
| tree rmem = PTRMEM_CST_MEMBER (rhs); |
| bool eq; |
| if (TREE_CODE (lmem) == TREE_CODE (rmem) |
| && TREE_CODE (lmem) == FIELD_DECL |
| && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE |
| && same_type_p (DECL_CONTEXT (lmem), |
| DECL_CONTEXT (rmem))) |
| /* If both refer to (possibly different) members of the same union |
| (12.3), they compare equal. */ |
| eq = true; |
| else |
| eq = cp_tree_equal (lhs, rhs); |
| r = constant_boolean_node (eq == is_code_eq, type); |
| } |
| else if ((TREE_CODE (lhs) == PTRMEM_CST |
| || TREE_CODE (rhs) == PTRMEM_CST) |
| && (null_member_pointer_value_p (lhs) |
| || null_member_pointer_value_p (rhs))) |
| r = constant_boolean_node (!is_code_eq, type); |
| else if (TREE_CODE (lhs) == PTRMEM_CST) |
| lhs = cplus_expand_constant (lhs); |
| else if (TREE_CODE (rhs) == PTRMEM_CST) |
| rhs = cplus_expand_constant (rhs); |
| } |
| if (code == POINTER_PLUS_EXPR && !*non_constant_p |
| && integer_zerop (lhs) && !integer_zerop (rhs)) |
| { |
| if (!ctx->quiet) |
| error ("arithmetic involving a null pointer in %qE", lhs); |
| *non_constant_p = true; |
| return t; |
| } |
| else if (code == POINTER_PLUS_EXPR) |
| r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, |
| overflow_p); |
| |
| if (r == NULL_TREE) |
| r = fold_binary_loc (loc, code, type, lhs, rhs); |
| |
| if (r == NULL_TREE) |
| { |
| if (lhs == orig_lhs && rhs == orig_rhs) |
| r = t; |
| else |
| r = build2_loc (loc, code, type, lhs, rhs); |
| } |
| else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
| *non_constant_p = true; |
| /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
| a local array in a constexpr function. */ |
| bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
| if (!ptr) |
| VERIFY_CONSTANT (r); |
| return r; |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to evaluate condition expressions. Dead branches are not |
| looked into. */ |
| |
| static tree |
| cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p, |
| tree *jump_target) |
| { |
| tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| /*lval*/false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (val); |
| /* Don't VERIFY_CONSTANT the other operands. */ |
| if (integer_zerop (val)) |
| val = TREE_OPERAND (t, 2); |
| else |
| val = TREE_OPERAND (t, 1); |
| if (TREE_CODE (t) == IF_STMT && !val) |
| val = void_node; |
| return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
| overflow_p, jump_target); |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to evaluate vector condition expressions. Unlike |
| cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal |
| ternary arithmetics operation, where all 3 arguments have to be |
| evaluated as constants and then folding computes the result from |
| them. */ |
| |
| static tree |
| cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| /*lval*/false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (arg1); |
| tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
| /*lval*/false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (arg2); |
| tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), |
| /*lval*/false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (arg3); |
| location_t loc = EXPR_LOCATION (t); |
| tree type = TREE_TYPE (t); |
| tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); |
| if (r == NULL_TREE) |
| { |
| if (arg1 == TREE_OPERAND (t, 0) |
| && arg2 == TREE_OPERAND (t, 1) |
| && arg3 == TREE_OPERAND (t, 2)) |
| r = t; |
| else |
| r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); |
| } |
| VERIFY_CONSTANT (r); |
| return r; |
| } |
| |
| /* Returns less than, equal to, or greater than zero if KEY is found to be |
| less than, to match, or to be greater than the constructor_elt's INDEX. */ |
| |
| static int |
| array_index_cmp (tree key, tree index) |
| { |
| gcc_assert (TREE_CODE (key) == INTEGER_CST); |
| |
| switch (TREE_CODE (index)) |
| { |
| case INTEGER_CST: |
| return tree_int_cst_compare (key, index); |
| case RANGE_EXPR: |
| { |
| tree lo = TREE_OPERAND (index, 0); |
| tree hi = TREE_OPERAND (index, 1); |
| if (tree_int_cst_lt (key, lo)) |
| return -1; |
| else if (tree_int_cst_lt (hi, key)) |
| return 1; |
| else |
| return 0; |
| } |
| default: |
| gcc_unreachable (); |
| } |
| } |
| |
| /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 |
| if none. If INSERT is true, insert a matching element rather than fail. */ |
| |
| static HOST_WIDE_INT |
| find_array_ctor_elt (tree ary, tree dindex, bool insert) |
| { |
| if (tree_int_cst_sgn (dindex) < 0) |
| return -1; |
| |
| unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); |
| vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); |
| unsigned HOST_WIDE_INT len = vec_safe_length (elts); |
| |
| unsigned HOST_WIDE_INT end = len; |
| unsigned HOST_WIDE_INT begin = 0; |
| |
| /* If the last element of the CONSTRUCTOR has its own index, we can assume |
| that the same is true of the other elements and index directly. */ |
| if (end > 0) |
| { |
| tree cindex = (*elts)[end - 1].index; |
| if (TREE_CODE (cindex) == INTEGER_CST |
| && compare_tree_int (cindex, end - 1) == 0) |
| { |
| if (i < end) |
| return i; |
| else |
| begin = end; |
| } |
| } |
| |
| /* Otherwise, find a matching index by means of a binary search. */ |
| while (begin != end) |
| { |
| unsigned HOST_WIDE_INT middle = (begin + end) / 2; |
| constructor_elt &elt = (*elts)[middle]; |
| tree idx = elt.index; |
| |
| int cmp = array_index_cmp (dindex, idx); |
| if (cmp < 0) |
| end = middle; |
| else if (cmp > 0) |
| begin = middle + 1; |
| else |
| { |
| if (insert && TREE_CODE (idx) == RANGE_EXPR) |
| { |
| /* We need to split the range. */ |
| constructor_elt e; |
| tree lo = TREE_OPERAND (idx, 0); |
| tree hi = TREE_OPERAND (idx, 1); |
| tree value = elt.value; |
| dindex = fold_convert (sizetype, dindex); |
| if (tree_int_cst_lt (lo, dindex)) |
| { |
| /* There are still some lower elts; shorten the range. */ |
| tree new_hi = int_const_binop (MINUS_EXPR, dindex, |
| size_one_node); |
| if (tree_int_cst_equal (lo, new_hi)) |
| /* Only one element left, no longer a range. */ |
| elt.index = lo; |
| else |
| TREE_OPERAND (idx, 1) = new_hi; |
| /* Append the element we want to insert. */ |
| ++middle; |
| e.index = dindex; |
| e.value = unshare_constructor (value); |
| vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
| } |
| else |
| /* No lower elts, the range elt is now ours. */ |
| elt.index = dindex; |
| |
| if (tree_int_cst_lt (dindex, hi)) |
| { |
| /* There are still some higher elts; append a range. */ |
| tree new_lo = int_const_binop (PLUS_EXPR, dindex, |
| size_one_node); |
| if (tree_int_cst_equal (new_lo, hi)) |
| e.index = hi; |
| else |
| e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); |
| e.value = unshare_constructor (value); |
| vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); |
| } |
| } |
| return middle; |
| } |
| } |
| |
| if (insert) |
| { |
| constructor_elt e = { dindex, NULL_TREE }; |
| vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); |
| return end; |
| } |
| |
| return -1; |
| } |
| |
| /* Under the control of CTX, issue a detailed diagnostic for |
| an out-of-bounds subscript INDEX into the expression ARRAY. */ |
| |
| static void |
| diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index) |
| { |
| if (!ctx->quiet) |
| { |
| tree arraytype = TREE_TYPE (array); |
| |
| /* Convert the unsigned array subscript to a signed integer to avoid |
| printing huge numbers for small negative values. */ |
| tree sidx = fold_convert (ssizetype, index); |
| if (DECL_P (array)) |
| { |
| if (TYPE_DOMAIN (arraytype)) |
| error ("array subscript value %qE is outside the bounds " |
| "of array %qD of type %qT", sidx, array, arraytype); |
| else |
| error ("non-zero array subscript %qE is used with array %qD of " |
| "type %qT with unknown bounds", sidx, array, arraytype); |
| inform (DECL_SOURCE_LOCATION (array), "declared here"); |
| } |
| else if (TYPE_DOMAIN (arraytype)) |
| error ("array subscript value %qE is outside the bounds " |
| "of array type %qT", sidx, arraytype); |
| else |
| error ("non-zero array subscript %qE is used with array of type %qT " |
| "with unknown bounds", sidx, arraytype); |
| } |
| } |
| |
| /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
| a VECTOR_TYPE). */ |
| |
| static tree |
| get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree nelts; |
| if (TREE_CODE (type) == ARRAY_TYPE) |
| { |
| if (TYPE_DOMAIN (type)) |
| nelts = array_type_nelts_top (type); |
| else |
| nelts = size_zero_node; |
| } |
| else if (VECTOR_TYPE_P (type)) |
| nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); |
| else |
| gcc_unreachable (); |
| |
| /* For VLAs, the number of elements won't be an integer constant. */ |
| nelts = cxx_eval_constant_expression (ctx, nelts, false, |
| non_constant_p, overflow_p); |
| return nelts; |
| } |
| |
| /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
| STRING_CST STRING. */ |
| |
| static tree |
| extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) |
| { |
| tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); |
| tree r; |
| |
| if (chars_per_elt == 1) |
| r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); |
| else |
| { |
| const unsigned char *ptr |
| = ((const unsigned char *)TREE_STRING_POINTER (string) |
| + index * chars_per_elt); |
| r = native_interpret_expr (type, ptr, chars_per_elt); |
| } |
| return r; |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to reduce a reference to an array slot. */ |
| |
| static tree |
| cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree oldary = TREE_OPERAND (t, 0); |
| tree ary = cxx_eval_constant_expression (ctx, oldary, |
| lval, |
| non_constant_p, overflow_p); |
| tree index, oldidx; |
| HOST_WIDE_INT i = 0; |
| tree elem_type = NULL_TREE; |
| unsigned len = 0, elem_nchars = 1; |
| if (*non_constant_p) |
| return t; |
| oldidx = TREE_OPERAND (t, 1); |
| index = cxx_eval_constant_expression (ctx, oldidx, |
| false, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (index); |
| if (!lval) |
| { |
| elem_type = TREE_TYPE (TREE_TYPE (ary)); |
| if (TREE_CODE (ary) == VIEW_CONVERT_EXPR |
| && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
| && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) |
| ary = TREE_OPERAND (ary, 0); |
| if (TREE_CODE (ary) == CONSTRUCTOR) |
| len = CONSTRUCTOR_NELTS (ary); |
| else if (TREE_CODE (ary) == STRING_CST) |
| { |
| elem_nchars = (TYPE_PRECISION (elem_type) |
| / TYPE_PRECISION (char_type_node)); |
| len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; |
| } |
| else if (TREE_CODE (ary) == VECTOR_CST) |
| /* We don't create variable-length VECTOR_CSTs. */ |
| len = VECTOR_CST_NELTS (ary).to_constant (); |
| else |
| { |
| /* We can't do anything with other tree codes, so use |
| VERIFY_CONSTANT to complain and fail. */ |
| VERIFY_CONSTANT (ary); |
| gcc_unreachable (); |
| } |
| |
| if (!tree_fits_shwi_p (index) |
| || (i = tree_to_shwi (index)) < 0) |
| { |
| diag_array_subscript (ctx, ary, index); |
| *non_constant_p = true; |
| return t; |
| } |
| } |
| |
| tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, |
| overflow_p); |
| VERIFY_CONSTANT (nelts); |
| if ((lval |
| ? !tree_int_cst_le (index, nelts) |
| : !tree_int_cst_lt (index, nelts)) |
| || tree_int_cst_sgn (index) < 0) |
| { |
| diag_array_subscript (ctx, ary, index); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| if (lval && ary == oldary && index == oldidx) |
| return t; |
| else if (lval) |
| return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); |
| |
| bool found; |
| if (TREE_CODE (ary) == CONSTRUCTOR) |
| { |
| HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); |
| found = (ix >= 0); |
| if (found) |
| i = ix; |
| } |
| else |
| found = (i < len); |
| |
| if (found) |
| { |
| tree r; |
| if (TREE_CODE (ary) == CONSTRUCTOR) |
| r = (*CONSTRUCTOR_ELTS (ary))[i].value; |
| else if (TREE_CODE (ary) == VECTOR_CST) |
| r = VECTOR_CST_ELT (ary, i); |
| else |
| r = extract_string_elt (ary, elem_nchars, i); |
| |
| if (r) |
| /* Don't VERIFY_CONSTANT here. */ |
| return r; |
| |
| /* Otherwise the element doesn't have a value yet. */ |
| } |
| |
| /* Not found. */ |
| |
| if (TREE_CODE (ary) == CONSTRUCTOR |
| && CONSTRUCTOR_NO_CLEARING (ary)) |
| { |
| /* 'ary' is part of the aggregate initializer we're currently |
| building; if there's no initializer for this element yet, |
| that's an error. */ |
| if (!ctx->quiet) |
| error ("accessing uninitialized array element"); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* If it's within the array bounds but doesn't have an explicit |
| initializer, it's initialized from {}. But use build_value_init |
| directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ |
| tree val; |
| if (CP_AGGREGATE_TYPE_P (elem_type)) |
| { |
| tree empty_ctor = build_constructor (init_list_type_node, NULL); |
| val = digest_init (elem_type, empty_ctor, tf_warning_or_error); |
| } |
| else |
| val = build_value_init (elem_type, tf_warning_or_error); |
| return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
| overflow_p); |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to reduce a field access of a value of class type. */ |
| |
| static tree |
| cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| unsigned HOST_WIDE_INT i; |
| tree field; |
| tree value; |
| tree part = TREE_OPERAND (t, 1); |
| tree orig_whole = TREE_OPERAND (t, 0); |
| tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
| lval, |
| non_constant_p, overflow_p); |
| if (INDIRECT_REF_P (whole) |
| && integer_zerop (TREE_OPERAND (whole, 0))) |
| { |
| if (!ctx->quiet) |
| error ("dereferencing a null pointer in %qE", orig_whole); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| if (TREE_CODE (whole) == PTRMEM_CST) |
| whole = cplus_expand_constant (whole); |
| if (whole == orig_whole) |
| return t; |
| if (lval) |
| return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
| whole, part, NULL_TREE); |
| /* Don't VERIFY_CONSTANT here; we only want to check that we got a |
| CONSTRUCTOR. */ |
| if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) |
| { |
| if (!ctx->quiet) |
| error ("%qE is not a constant expression", orig_whole); |
| *non_constant_p = true; |
| } |
| if (DECL_MUTABLE_P (part)) |
| { |
| if (!ctx->quiet) |
| error ("mutable %qD is not usable in a constant expression", part); |
| *non_constant_p = true; |
| } |
| if (*non_constant_p) |
| return t; |
| bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
| FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
| { |
| /* Use name match for PMF fields, as a variant will have a |
| different FIELD_DECL with a different type. */ |
| if (pmf ? DECL_NAME (field) == DECL_NAME (part) |
| : field == part) |
| { |
| if (value) |
| { |
| STRIP_ANY_LOCATION_WRAPPER (value); |
| return value; |
| } |
| else |
| /* We're in the middle of initializing it. */ |
| break; |
| } |
| } |
| if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE |
| && CONSTRUCTOR_NELTS (whole) > 0) |
| { |
| /* DR 1188 says we don't have to deal with this. */ |
| if (!ctx->quiet) |
| error ("accessing %qD member instead of initialized %qD member in " |
| "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
| classes never get represented; throw together a value now. */ |
| if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
| return build_constructor (TREE_TYPE (t), NULL); |
| |
| gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
| |
| if (CONSTRUCTOR_NO_CLEARING (whole)) |
| { |
| /* 'whole' is part of the aggregate initializer we're currently |
| building; if there's no initializer for this member yet, that's an |
| error. */ |
| if (!ctx->quiet) |
| error ("accessing uninitialized member %qD", part); |
| *non_constant_p = true; |
| return t; |
| } |
| |
| /* If there's no explicit init for this field, it's value-initialized. */ |
| value = build_value_init (TREE_TYPE (t), tf_warning_or_error); |
| return cxx_eval_constant_expression (ctx, value, |
| lval, |
| non_constant_p, overflow_p); |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Attempt to reduce a field access of a value of class type that is |
| expressed as a BIT_FIELD_REF. */ |
| |
| static tree |
| cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree orig_whole = TREE_OPERAND (t, 0); |
| tree retval, fldval, utype, mask; |
| bool fld_seen = false; |
| HOST_WIDE_INT istart, isize; |
| tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
| lval, |
| non_constant_p, overflow_p); |
| tree start, field, value; |
| unsigned HOST_WIDE_INT i; |
| |
| if (whole == orig_whole) |
| return t; |
| /* Don't VERIFY_CONSTANT here; we only want to check that we got a |
| CONSTRUCTOR. */ |
| if (!*non_constant_p |
| && TREE_CODE (whole) != VECTOR_CST |
| && TREE_CODE (whole) != CONSTRUCTOR) |
| { |
| if (!ctx->quiet) |
| error ("%qE is not a constant expression", orig_whole); |
| *non_constant_p = true; |
| } |
| if (*non_constant_p) |
| return t; |
| |
| if (TREE_CODE (whole) == VECTOR_CST) |
| return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, |
| TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); |
| |
| start = TREE_OPERAND (t, 2); |
| istart = tree_to_shwi (start); |
| isize = tree_to_shwi (TREE_OPERAND (t, 1)); |
| utype = TREE_TYPE (t); |
| if (!TYPE_UNSIGNED (utype)) |
| utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); |
| retval = build_int_cst (utype, 0); |
| FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
| { |
| tree bitpos = bit_position (field); |
| STRIP_ANY_LOCATION_WRAPPER (value); |
| if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
| return value; |
| if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE |
| && TREE_CODE (value) == INTEGER_CST |
| && tree_fits_shwi_p (bitpos) |
| && tree_fits_shwi_p (DECL_SIZE (field))) |
| { |
| HOST_WIDE_INT bit = tree_to_shwi (bitpos); |
| HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); |
| HOST_WIDE_INT shift; |
| if (bit >= istart && bit + sz <= istart + isize) |
| { |
| fldval = fold_convert (utype, value); |
| mask = build_int_cst_type (utype, -1); |
| mask = fold_build2 (LSHIFT_EXPR, utype, mask, |
| size_int (TYPE_PRECISION (utype) - sz)); |
| mask = fold_build2 (RSHIFT_EXPR, utype, mask, |
| size_int (TYPE_PRECISION (utype) - sz)); |
| fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); |
| shift = bit - istart; |
| if (BYTES_BIG_ENDIAN) |
| shift = TYPE_PRECISION (utype) - shift - sz; |
| fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, |
| size_int (shift)); |
| retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); |
| fld_seen = true; |
| } |
| } |
| } |
| if (fld_seen) |
| return fold_convert (TREE_TYPE (t), retval); |
| gcc_unreachable (); |
| return error_mark_node; |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| Evaluate a short-circuited logical expression T in the context |
| of a given constexpr CALL. BAILOUT_VALUE is the value for |
| early return. CONTINUE_VALUE is used here purely for |
| sanity check purposes. */ |
| |
| static tree |
| cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
| tree bailout_value, tree continue_value, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| tree r; |
| tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| lval, |
| non_constant_p, overflow_p); |
| VERIFY_CONSTANT (lhs); |
| if (tree_int_cst_equal (lhs, bailout_value)) |
| return lhs; |
| gcc_assert (tree_int_cst_equal (lhs, continue_value)); |
| r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
| lval, non_constant_p, |
| overflow_p); |
| VERIFY_CONSTANT (r); |
| return r; |
| } |
| |
| /* REF is a COMPONENT_REF designating a particular field. V is a vector of |
| CONSTRUCTOR elements to initialize (part of) an object containing that |
| field. Return a pointer to the constructor_elt corresponding to the |
| initialization of the field. */ |
| |
| static constructor_elt * |
| base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) |
| { |
| tree aggr = TREE_OPERAND (ref, 0); |
| tree field = TREE_OPERAND (ref, 1); |
| HOST_WIDE_INT i; |
| constructor_elt *ce; |
| |
| gcc_assert (TREE_CODE (ref) == COMPONENT_REF); |
| |
| if (TREE_CODE (aggr) == COMPONENT_REF) |
| { |
| constructor_elt *base_ce |
| = base_field_constructor_elt (v, aggr); |
| v = CONSTRUCTOR_ELTS (base_ce->value); |
| } |
| |
| for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| if (ce->index == field) |
| return ce; |
| |
| gcc_unreachable (); |
| return NULL; |
| } |
| |
| /* Some of the expressions fed to the constexpr mechanism are calls to |
| constructors, which have type void. In that case, return the type being |
| initialized by the constructor. */ |
| |
| static tree |
| initialized_type (tree t) |
| { |
| if (TYPE_P (t)) |
| return t; |
| tree type = TREE_TYPE (t); |
| if (TREE_CODE (t) == CALL_EXPR) |
| { |
| /* A constructor call has void type, so we need to look deeper. */ |
| tree fn = get_function_named_in_call (t); |
| if (fn && TREE_CODE (fn) == FUNCTION_DECL |
| && DECL_CXX_CONSTRUCTOR_P (fn)) |
| type = DECL_CONTEXT (fn); |
| } |
| else if (TREE_CODE (t) == COMPOUND_EXPR) |
| return initialized_type (TREE_OPERAND (t, 1)); |
| else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
| type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); |
| return cv_unqualified (type); |
| } |
| |
| /* We're about to initialize element INDEX of an array or class from VALUE. |
| Set up NEW_CTX appropriately by adjusting .object to refer to the |
| subobject and creating a new CONSTRUCTOR if the element is itself |
| a class or array. */ |
| |
| static void |
| init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, |
| tree index, tree &value) |
| { |
| new_ctx = *ctx; |
| |
| if (index && TREE_CODE (index) != INTEGER_CST |
| && TREE_CODE (index) != FIELD_DECL) |
| /* This won't have an element in the new CONSTRUCTOR. */ |
| return; |
| |
| tree type = initialized_type (value); |
| if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) |
| /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ |
| return; |
| |
| /* The sub-aggregate initializer might contain a placeholder; |
| update object to refer to the subobject and ctor to refer to |
| the (newly created) sub-initializer. */ |
| if (ctx->object) |
| new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); |
| tree elt = build_constructor (type, NULL); |
| CONSTRUCTOR_NO_CLEARING (elt) = true; |
| new_ctx.ctor = elt; |
| |
| if (TREE_CODE (value) == TARGET_EXPR) |
| /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ |
| value = TARGET_EXPR_INITIAL (value); |
| } |
| |
| /* We're about to process an initializer for a class or array TYPE. Make |
| sure that CTX is set up appropriately. */ |
| |
| static void |
| verify_ctor_sanity (const constexpr_ctx *ctx, tree type) |
| { |
| /* We don't bother building a ctor for an empty base subobject. */ |
| if (is_empty_class (type)) |
| return; |
| |
| /* We're in the middle of an initializer that might involve placeholders; |
| our caller should have created a CONSTRUCTOR for us to put the |
| initializer into. We will either return that constructor or T. */ |
| gcc_assert (ctx->ctor); |
| gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| (type, TREE_TYPE (ctx->ctor))); |
| /* We used to check that ctx->ctor was empty, but that isn't the case when |
| the object is zero-initialized before calling the constructor. */ |
| if (ctx->object) |
| { |
| tree otype = TREE_TYPE (ctx->object); |
| gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) |
| /* Handle flexible array members. */ |
| || (TREE_CODE (otype) == ARRAY_TYPE |
| && TYPE_DOMAIN (otype) == NULL_TREE |
| && TREE_CODE (type) == ARRAY_TYPE |
| && (same_type_ignoring_top_level_qualifiers_p |
| (TREE_TYPE (type), TREE_TYPE (otype))))); |
| } |
| gcc_assert (!ctx->object || !DECL_P (ctx->object) |
| || *(ctx->values->get (ctx->object)) == ctx->ctor); |
| } |
| |
| /* Subroutine of cxx_eval_constant_expression. |
| The expression tree T denotes a C-style array or a C-style |
| aggregate. Reduce it to a constant expression. */ |
| |
| static tree |
| cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
| bool lval, |
| bool *non_constant_p, bool *overflow_p) |
| { |
| vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
| bool changed = false; |
| gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); |
| tree type = TREE_TYPE (t); |
| |
| constexpr_ctx new_ctx; |
| if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
| { |
| /* We don't really need the ctx->ctor business for a PMF or |
| vector, but it's simpler to use the same code. */ |
| new_ctx = *ctx; |
| new_ctx.ctor = build_constructor (type, NULL); |
| new_ctx.object = NULL_TREE; |
| ctx = &new_ctx; |
| }; |
| verify_ctor_sanity (ctx, type); |
| vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
| vec_alloc (*p, vec_safe_length (v)); |
| |
| unsigned i; |
| tree index, value; |
| bool constant_p = true; |
| bool side_effects_p = false; |
| FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
| { |
| tree orig_value = value; |
| init_subob_ctx (ctx, new_ctx, index, value); |
| if (new_ctx.ctor != ctx->ctor) |
| /* If we built a new CONSTRUCTOR, attach it now so that other |
| initializers can refer to it. */ |
| CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor); |
| tree elt = cxx_eval_constant_expression (&new_ctx, value, |
| lval, |
| non_constant_p, overflow_p); |
| /* Don't VERIFY_CONSTANT here. */ |
| if (ctx->quiet && *non_constant_p) |
| break; |
| if (elt != orig_value) |
| changed = true; |
| |
| if (!TREE_CONSTANT (elt)) |
| constant_p = false; |
| if (TREE_SIDE_EFFECTS (elt)) |
| side_effects_p = true; |
| if (index && TREE_CODE (index) == COMPONENT_REF) |
| { |
| /* This is an initialization of a vfield inside a base |
| subaggregate that we already initialized; push this |
| initialization into the previous initialization. */ |
| constructor_elt *inner = base_field_constructor_elt (*p, index); |
| inner->value = elt; |
| changed = true; |
| } |
| else if (index |
| && (TREE_CODE (index) == NOP_EXPR |
| || TREE_CODE (index) == POINTER_PLUS_EXPR)) |
| { |
| /* This is an initializer for an empty base; now that we've |
| checked that it's constant, we can ignore it. */ |
| gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
| changed = true; |
| } |
| else |
| { |
| if (new_ctx.ctor != ctx->ctor) |
| { |
| /* We appended this element above; update the value. */ |
| gcc_assert ((*p)->last().index == index); |
| (*p)->last().value = elt; |
| } |
| <
|