X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=blobdiff_plain;f=gcc%2Fcp%2Fpt.c;h=e53e90ffbedeb98f1732394c9cbe7fa743fc12a1;hp=dbff91e617dae39ad91fb36c2f8dd349b1c9aeab;hb=2cbaacd915abd833c03e181f78b86dce4e761718;hpb=890ca6c44a3393ec4e11f55959f28d90a4b48bce diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index dbff91e617d..e53e90ffbed 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -109,13 +109,22 @@ static GTY(()) VEC(tree,gc) *canonical_template_parms; #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 +enum template_base_result { + tbr_incomplete_type, + tbr_ambiguous_baseclass, + tbr_success +}; + static void push_access_scope (tree); static void pop_access_scope (tree); +static void push_deduction_access_scope (tree); +static void pop_deduction_access_scope (tree); static bool resolve_overloaded_unification (tree, tree, tree, tree, - unification_kind_t, int); + unification_kind_t, int, + bool); static int try_one_overload (tree, tree, tree, tree, tree, - unification_kind_t, int, bool); -static int unify (tree, tree, tree, tree, int); + unification_kind_t, int, bool, bool); +static int unify (tree, tree, tree, tree, int, bool); static void add_pending_template (tree); static tree reopen_tinst_level (struct tinst_level *); static tree tsubst_initializer_list (tree, tree); @@ -129,7 +138,8 @@ static bool check_instantiated_args (tree, tree, tsubst_flags_t); static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, tree); static int type_unification_real (tree, tree, tree, const tree *, - unsigned int, int, unification_kind_t, int); + unsigned int, int, unification_kind_t, int, + bool); static void note_template_header (int); static tree convert_nontype_argument_function (tree, tree); static tree convert_nontype_argument (tree, tree, tsubst_flags_t); @@ -154,7 +164,8 @@ static tree get_bindings (tree, tree, tree, bool); static int template_decl_level (tree); static int check_cv_quals_for_unify (int, tree, tree); static void template_parm_level_and_index (tree, int*, int*); -static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool); +static int unify_pack_expansion (tree, tree, tree, + tree, unification_kind_t, bool, bool); static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); static tree tsubst_template_parms (tree, tree, tsubst_flags_t); @@ -166,8 +177,9 @@ static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); static bool check_specialization_scope (void); static tree process_partial_specialization (tree); static void set_current_access_from_decl (tree); -static tree get_template_base (tree, tree, tree, tree); -static tree try_class_unification (tree, tree, tree, tree); +static enum template_base_result get_template_base (tree, tree, tree, tree, + bool , tree *); +static tree try_class_unification (tree, tree, tree, tree, bool); static int coerce_template_template_parms (tree, tree, tsubst_flags_t, tree, tree); static bool template_template_parm_bindings_ok_p (tree, tree); @@ -191,6 +203,7 @@ static void append_type_to_template_for_access_check_1 (tree, tree, tree, static tree listify (tree); static tree listify_autos (tree, tree); static tree template_parm_to_arg (tree t); +static bool arg_from_parm_pack_p (tree, tree); static tree current_template_args (void); static tree fixup_template_type_parm_type (tree, int); static tree fixup_template_parm_index (tree, tree, int); @@ -801,7 +814,13 @@ maybe_process_partial_specialization (tree type) context = TYPE_CONTEXT (type); - if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) + if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) + /* Consider non-class instantiations of alias templates as + well. */ + || (TYPE_P (type) + && TYPE_TEMPLATE_INFO (type) + && DECL_LANG_SPECIFIC (TYPE_NAME (type)) + && DECL_USE_TEMPLATE (TYPE_NAME (type)))) { /* This is for ordinary explicit specialization and partial specialization of a template class such as: @@ -814,7 +833,8 @@ maybe_process_partial_specialization (tree type) Make sure that `C' and `C' are implicit instantiations. */ - if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) + if (CLASS_TYPE_P (type) + && CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) { check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); @@ -826,8 +846,16 @@ maybe_process_partial_specialization (tree type) return error_mark_node; } } - else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) + else if (CLASS_TYPE_P (type) + && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) error ("specialization of %qT after instantiation", type); + + if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type))) + { + error ("partial specialization of alias template %qD", + TYPE_TI_TEMPLATE (type)); + return error_mark_node; + } } else if (CLASS_TYPE_P (type) && !CLASSTYPE_USE_TEMPLATE (type) @@ -879,7 +907,8 @@ maybe_process_partial_specialization (tree type) instantiation. Reassign it to the new member specialization template. */ spec_entry elt; - spec_entry **slot; + spec_entry *entry; + void **slot; elt.tmpl = most_general_template (tmpl); elt.args = CLASSTYPE_TI_ARGS (inst); @@ -890,10 +919,10 @@ maybe_process_partial_specialization (tree type) elt.tmpl = tmpl; elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); - slot = (spec_entry **) - htab_find_slot (type_specializations, &elt, INSERT); - *slot = ggc_alloc_spec_entry (); - **slot = elt; + slot = htab_find_slot (type_specializations, &elt, INSERT); + entry = ggc_alloc_spec_entry (); + *entry = elt; + *slot = entry; } else if (COMPLETE_OR_OPEN_TYPE_P (inst)) /* But if we've had an implicit instantiation, that's a @@ -1281,7 +1310,7 @@ register_specialization (tree spec, tree tmpl, tree args, bool is_friend, hashval_t hash) { tree fn; - spec_entry **slot = NULL; + void **slot = NULL; spec_entry elt; gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)); @@ -1314,10 +1343,10 @@ register_specialization (tree spec, tree tmpl, tree args, bool is_friend, if (hash == 0) hash = hash_specialization (&elt); - slot = (spec_entry **) + slot = htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT); if (*slot) - fn = (*slot)->spec; + fn = ((spec_entry *) *slot)->spec; else fn = NULL_TREE; } @@ -1410,11 +1439,12 @@ register_specialization (tree spec, tree tmpl, tree args, bool is_friend, && !check_specialization_namespace (tmpl)) DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); - if (!optimize_specialization_lookup_p (tmpl)) + if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */) { + spec_entry *entry = ggc_alloc_spec_entry (); gcc_assert (tmpl && args && spec); - *slot = ggc_alloc_spec_entry (); - **slot = elt; + *entry = elt; + *slot = entry; if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) && PRIMARY_TEMPLATE_P (tmpl) && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) @@ -1576,6 +1606,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) return val; case CAST_EXPR: + case IMPLICIT_CONV_EXPR: case STATIC_CAST_EXPR: case REINTERPRET_CAST_EXPR: case CONST_CAST_EXPR: @@ -1607,7 +1638,7 @@ iterative_hash_template_arg (tree arg, hashval_t val) default: gcc_assert (IS_EXPR_CODE_CLASS (tclass)); { - unsigned n = TREE_OPERAND_LENGTH (arg); + unsigned n = cp_tree_operand_length (arg); for (i = 0; i < n; ++i) val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); return val; @@ -1626,19 +1657,19 @@ iterative_hash_template_arg (tree arg, hashval_t val) bool reregister_specialization (tree spec, tree tinfo, tree new_spec) { - spec_entry **slot; + spec_entry *entry; spec_entry elt; elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); elt.args = TI_ARGS (tinfo); elt.spec = NULL_TREE; - slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT); - if (*slot) + entry = (spec_entry *) htab_find (decl_specializations, &elt); + if (entry != NULL) { - gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec); + gcc_assert (entry->spec == spec || entry->spec == new_spec); gcc_assert (new_spec != NULL_TREE); - (*slot)->spec = new_spec; + entry->spec = new_spec; return 1; } @@ -1789,7 +1820,7 @@ determine_specialization (tree template_id, with the signature of DECL. */ tree templates = NULL_TREE; int header_count; - struct cp_binding_level *b; + cp_binding_level *b; *targs_out = NULL_TREE; @@ -2729,12 +2760,15 @@ template_parameter_pack_p (const_tree parm) if (TREE_CODE (parm) == PARM_DECL) return (DECL_TEMPLATE_PARM_P (parm) && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); + if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX) + return TEMPLATE_PARM_PARAMETER_PACK (parm); /* If this is a list of template parameters, we could get a TYPE_DECL or a TEMPLATE_DECL. */ if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) parm = TREE_TYPE (parm); + /* Otherwise it must be a type template parameter. */ return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) && TEMPLATE_TYPE_PARAMETER_PACK (parm)); @@ -2823,8 +2857,8 @@ make_ith_pack_parameter_name (tree name, int i) return get_identifier (newname); } -/* Return true if T is a primary function - or class template instantiation. */ +/* Return true if T is a primary function, class or alias template + instantiation. */ bool primary_template_instantiation_p (const_tree t) @@ -2839,6 +2873,11 @@ primary_template_instantiation_p (const_tree t) else if (CLASS_TYPE_P (t)) return CLASSTYPE_TEMPLATE_INSTANTIATION (t) && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)); + else if (TYPE_P (t) + && TYPE_TEMPLATE_INFO (t) + && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t)) + && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t))) + return true; return false; } @@ -2945,6 +2984,7 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) break; case TEMPLATE_TYPE_PARM: + t = TYPE_MAIN_VARIANT (t); case TEMPLATE_TEMPLATE_PARM: if (TEMPLATE_TYPE_PARAMETER_PACK (t)) parameter_pack_p = true; @@ -2960,6 +3000,9 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) } break; + case BASES: + parameter_pack_p = true; + break; default: /* Not a parameter pack. */ break; @@ -3013,6 +3056,7 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) *walk_subtrees = 0; return NULL_TREE; + case CONSTRUCTOR: case TEMPLATE_DECL: cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, ppd->visited); @@ -3992,6 +4036,63 @@ template_parm_to_arg (tree t) return t; } +/* This function returns TRUE if PARM_PACK is a template parameter + pack and if ARG_PACK is what template_parm_to_arg returned when + passed PARM_PACK. */ + +static bool +arg_from_parm_pack_p (tree arg_pack, tree parm_pack) +{ + /* For clarity in the comments below let's use the representation + argument_pack' to denote an argument pack and its + elements. + + In the 'if' block below, we want to detect cases where + ARG_PACK is argument_pack. I.e, we want to + check if ARG_PACK is an argument pack which sole element is + the expansion of PARM_PACK. That argument pack is typically + created by template_parm_to_arg when passed a parameter + pack. */ + + if (arg_pack + && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 + && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) + { + tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); + tree pattern = PACK_EXPANSION_PATTERN (expansion); + /* So we have an argument_pack. We want to test if P + is actually PARM_PACK. We will not use cp_tree_equal to + test P and PARM_PACK because during type fixup (by + fixup_template_parm) P can be a pre-fixup version of a + type and PARM_PACK be its post-fixup version. + cp_tree_equal would consider them as different even + though we would want to consider them compatible for our + precise purpose here. + + Thus we are going to consider that P and PARM_PACK are + compatible if they have the same DECL. */ + if ((/* If ARG_PACK is a type parameter pack named by the + same DECL as parm_pack ... */ + (TYPE_P (pattern) + && TYPE_P (parm_pack) + && TYPE_NAME (pattern) == TYPE_NAME (parm_pack)) + /* ... or if PARM_PACK is a non-type parameter named by the + same DECL as ARG_PACK. Note that PARM_PACK being a + non-type parameter means it's either a PARM_DECL or a + TEMPLATE_PARM_INDEX. */ + || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX + && ((TREE_CODE (parm_pack) == PARM_DECL + && (TEMPLATE_PARM_DECL (pattern) + == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack)))) + || (TREE_CODE (parm_pack) == TEMPLATE_PARM_INDEX + && (TEMPLATE_PARM_DECL (pattern) + == TEMPLATE_PARM_DECL (parm_pack)))))) + && template_parameter_pack_p (pattern)) + return true; + } + return false; +} + /* Within the declaration of a template, return all levels of template parameters that apply. The template parameters are represented as a TREE_VEC, in the form documented in cp-tree.h for template @@ -4109,6 +4210,7 @@ build_template_decl (tree decl, tree parms, bool member_template_p) tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); DECL_TEMPLATE_PARMS (tmpl) = parms; DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); + DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; return tmpl; @@ -4568,7 +4670,7 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary, "friend declarations"); else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) msg = G_("default template arguments may not be used in function templates " - "without -std=c++0x or -std=gnu++0x"); + "without -std=c++11 or -std=gnu++11"); else if (is_partial) msg = G_("default template arguments may not be used in " "partial specializations"); @@ -4749,6 +4851,10 @@ push_template_decl_real (tree decl, bool is_friend) else if (DECL_IMPLICIT_TYPEDEF_P (decl) && CLASS_TYPE_P (TREE_TYPE (decl))) /* OK */; + else if (TREE_CODE (decl) == TYPE_DECL + && TYPE_DECL_ALIAS_P (decl)) + /* alias-declaration */ + gcc_assert (!DECL_ARTIFICIAL (decl)); else { error ("template declaration of %q#D", decl); @@ -5013,8 +5119,13 @@ template arguments to %qD do not match original template %qD", if (DECL_IMPLICIT_TYPEDEF_P (decl)) SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); - else if (DECL_LANG_SPECIFIC (decl)) - DECL_TEMPLATE_INFO (decl) = info; + else + { + if (primary && !DECL_LANG_SPECIFIC (decl)) + retrofit_lang_decl (decl); + if (DECL_LANG_SPECIFIC (decl)) + DECL_TEMPLATE_INFO (decl) = info; + } return DECL_TEMPLATE_RESULT (tmpl); } @@ -5177,6 +5288,32 @@ fold_non_dependent_expr (tree expr) return fold_non_dependent_expr_sfinae (expr, tf_error); } +/* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias + template declaration, or a TYPE_DECL for an alias declaration. */ + +bool +alias_type_or_template_p (tree t) +{ + if (t == NULL_TREE) + return false; + return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t)) + || (TYPE_P (t) + && TYPE_NAME (t) + && TYPE_DECL_ALIAS_P (TYPE_NAME (t))) + || DECL_ALIAS_TEMPLATE_P (t)); +} + +/* Return TRUE iff is a specialization of an alias template. */ + +bool +alias_template_specialization_p (tree t) +{ + if (t == NULL_TREE) + return false; + return (primary_template_instantiation_p (t) + && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t))); +} + /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which must be a function or a pointer-to-function type, as specified in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, @@ -5195,7 +5332,7 @@ convert_nontype_argument_function (tree type, tree expr) fn_no_ptr = fn; if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); - if (TREE_CODE (fn_no_ptr) == BASELINK) + if (BASELINK_P (fn_no_ptr)) fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); /* [temp.arg.nontype]/1 @@ -5226,6 +5363,8 @@ check_valid_ptrmem_cst_expr (tree type, tree expr, STRIP_NOPS (expr); if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST)) return true; + if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr)) + return true; if (complain & tf_error) { error ("%qE is not a valid template argument for type %qT", @@ -5265,6 +5404,211 @@ has_value_dependent_address (tree op) return false; } +/* The next set of functions are used for providing helpful explanatory + diagnostics for failed overload resolution. Their messages should be + indented by two spaces for consistency with the messages in + call.c */ + +static int +unify_success (bool explain_p ATTRIBUTE_UNUSED) +{ + return 0; +} + +static int +unify_parameter_deduction_failure (bool explain_p, tree parm) +{ + if (explain_p) + inform (input_location, + " couldn't deduce template parameter %qD", parm); + return 1; +} + +static int +unify_invalid (bool explain_p ATTRIBUTE_UNUSED) +{ + return 1; +} + +static int +unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, + " types %qT and %qT have incompatible cv-qualifiers", + parm, arg); + return 1; +} + +static int +unify_type_mismatch (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, " mismatched types %qT and %qT", parm, arg); + return 1; +} + +static int +unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, + " template parameter %qD is not a parameter pack, but " + "argument %qD is", + parm, arg); + return 1; +} + +static int +unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, + " template argument %qE does not match " + "pointer-to-member constant %qE", + arg, parm); + return 1; +} + +static int +unify_expression_unequal (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, " %qE is not equivalent to %qE", parm, arg); + return 1; +} + +static int +unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg) +{ + if (explain_p) + inform (input_location, + " inconsistent parameter pack deduction with %qT and %qT", + old_arg, new_arg); + return 1; +} + +static int +unify_inconsistency (bool explain_p, tree parm, tree first, tree second) +{ + if (explain_p) + inform (input_location, + " deduced conflicting types for parameter %qT (%qT and %qT)", + parm, first, second); + return 1; +} + +static int +unify_vla_arg (bool explain_p, tree arg) +{ + if (explain_p) + inform (input_location, + " variable-sized array type %qT is not " + "a valid template argument", + arg); + return 1; +} + +static int +unify_method_type_error (bool explain_p, tree arg) +{ + if (explain_p) + inform (input_location, + " member function type %qT is not a valid template argument", + arg); + return 1; +} + +static int +unify_arity (bool explain_p, int have, int wanted) +{ + if (explain_p) + inform_n (input_location, wanted, + " candidate expects %d argument, %d provided", + " candidate expects %d arguments, %d provided", + wanted, have); + return 1; +} + +static int +unify_too_many_arguments (bool explain_p, int have, int wanted) +{ + return unify_arity (explain_p, have, wanted); +} + +static int +unify_too_few_arguments (bool explain_p, int have, int wanted) +{ + return unify_arity (explain_p, have, wanted); +} + +static int +unify_arg_conversion (bool explain_p, tree to_type, + tree from_type, tree arg) +{ + if (explain_p) + inform (input_location, " cannot convert %qE (type %qT) to type %qT", + arg, from_type, to_type); + return 1; +} + +static int +unify_no_common_base (bool explain_p, enum template_base_result r, + tree parm, tree arg) +{ + if (explain_p) + switch (r) + { + case tbr_ambiguous_baseclass: + inform (input_location, " %qT is an ambiguous base class of %qT", + arg, parm); + break; + default: + inform (input_location, " %qT is not derived from %qT", arg, parm); + break; + } + return 1; +} + +static int +unify_inconsistent_template_template_parameters (bool explain_p) +{ + if (explain_p) + inform (input_location, + " template parameters of a template template argument are " + "inconsistent with other deduced template arguments"); + return 1; +} + +static int +unify_template_deduction_failure (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, + " can't deduce a template for %qT from non-template type %qT", + parm, arg); + return 1; +} + +static int +unify_template_argument_mismatch (bool explain_p, tree parm, tree arg) +{ + if (explain_p) + inform (input_location, + " template argument %qE does not match %qD", arg, parm); + return 1; +} + +static int +unify_overload_resolution_failure (bool explain_p, tree arg) +{ + if (explain_p) + inform (input_location, + " could not resolve address from overloaded function %qE", + arg); + return 1; +} + /* Attempt to convert the non-type template parameter EXPR to the indicated TYPE. If the conversion is successful, return the converted value. If the conversion is unsuccessful, return @@ -5331,6 +5675,17 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) else expr = mark_rvalue_use (expr); + /* 14.3.2/5: The null pointer{,-to-member} conversion is applied + to a non-type argument of "nullptr". */ + if (expr == nullptr_node + && (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))) + expr = convert (type, expr); + + /* In C++11, non-type template arguments can be arbitrary constant + expressions. But don't fold a PTRMEM_CST to a CONSTRUCTOR yet. */ + if (cxx_dialect >= cxx0x && TREE_CODE (expr) != PTRMEM_CST) + expr = maybe_constant_value (expr); + /* HACK: Due to double coercion, we can get a NOP_EXPR(ADDR_EXPR (arg)) here, which is the tree that we built on the first call (see @@ -5338,41 +5693,45 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) function). We just strip everything and get to the arg. See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C for examples. */ - if (TREE_CODE (expr) == NOP_EXPR) + if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) { - if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) + tree probe_type, probe = expr; + if (REFERENCE_REF_P (probe)) + probe = TREE_OPERAND (probe, 0); + probe_type = TREE_TYPE (probe); + if (TREE_CODE (probe) == NOP_EXPR) { /* ??? Maybe we could use convert_from_reference here, but we would need to relax its constraints because the NOP_EXPR could actually change the type to something more cv-qualified, and this is not folded by convert_from_reference. */ - tree addr = TREE_OPERAND (expr, 0); - gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); + tree addr = TREE_OPERAND (probe, 0); + gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE); gcc_assert (TREE_CODE (addr) == ADDR_EXPR); gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); gcc_assert (same_type_ignoring_top_level_qualifiers_p - (TREE_TYPE (expr_type), + (TREE_TYPE (probe_type), TREE_TYPE (TREE_TYPE (addr)))); expr = TREE_OPERAND (addr, 0); expr_type = TREE_TYPE (expr); } + } - /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the - parameter is a pointer to object, through decay and - qualification conversion. Let's strip everything. */ - else if (TYPE_PTROBV_P (type)) - { - STRIP_NOPS (expr); - gcc_assert (TREE_CODE (expr) == ADDR_EXPR); - gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); - /* Skip the ADDR_EXPR only if it is part of the decay for - an array. Otherwise, it is part of the original argument - in the source code. */ - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) - expr = TREE_OPERAND (expr, 0); - expr_type = TREE_TYPE (expr); - } + /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the + parameter is a pointer to object, through decay and + qualification conversion. Let's strip everything. */ + else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type)) + { + STRIP_NOPS (expr); + gcc_assert (TREE_CODE (expr) == ADDR_EXPR); + gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); + /* Skip the ADDR_EXPR only if it is part of the decay for + an array. Otherwise, it is part of the original argument + in the source code. */ + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) + expr = TREE_OPERAND (expr, 0); + expr_type = TREE_TYPE (expr); } /* [temp.arg.nontype]/5, bullet 1 @@ -5435,6 +5794,8 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) /* Non-type template parameters are OK. */ ; + else if (cxx_dialect >= cxx0x && integer_zerop (expr)) + /* Null pointer values are OK in C++11. */; else if (TREE_CODE (expr) != ADDR_EXPR && TREE_CODE (expr_type) != ARRAY_TYPE) { @@ -5562,6 +5923,10 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) return error_mark_node; } + if (cxx_dialect >= cxx0x && integer_zerop (expr)) + /* Null pointer values are OK in C++11. */ + return perform_qualification_conversions (type, expr); + expr = convert_nontype_argument_function (type, expr); if (!expr || expr == error_mark_node) return expr; @@ -5648,6 +6013,16 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) if (expr == error_mark_node) return expr; } + else if (NULLPTR_TYPE_P (type)) + { + if (expr != nullptr_node) + { + error ("%qE is not a valid template argument for type %qT " + "because it is of type %qT", expr, type, TREE_TYPE (expr)); + return NULL_TREE; + } + return expr; + } /* A template non-type parameter must be one of the above. */ else gcc_unreachable (); @@ -5914,6 +6289,28 @@ template_template_parm_bindings_ok_p (tree tparms, tree targs) return ret; } +/* Since type attributes aren't mangled, we need to strip them from + template type arguments. */ + +static tree +canonicalize_type_argument (tree arg, tsubst_flags_t complain) +{ + tree mv; + if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg)) + return arg; + mv = TYPE_MAIN_VARIANT (arg); + arg = strip_typedefs (arg); + if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv) + || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv)) + { + if (complain & tf_warning) + warning (0, "ignoring attributes on template argument %qT", arg); + arg = build_aligned_type (arg, TYPE_ALIGN (mv)); + arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv)); + } + return arg; +} + /* Convert the indicated template ARG as necessary to match the indicated template PARM. Returns the converted ARG, or error_mark_node if the conversion was unsuccessful. Error and @@ -5962,7 +6359,9 @@ convert_template_argument (tree parm, tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg)); if (TREE_CODE (t) == TEMPLATE_DECL) { - if (complain & tf_warning_or_error) + if (cxx_dialect >= cxx0x) + /* OK under DR 1004. */; + else if (complain & tf_warning_or_error) pedwarn (input_location, OPT_pedantic, "injected-class-name %qD" " used as template template argument", TYPE_NAME (arg)); else if (flag_pedantic_errors) @@ -6088,7 +6487,7 @@ convert_template_argument (tree parm, the typedef, which is confusing if those future uses do not themselves also use the typedef. */ if (TYPE_P (val)) - val = strip_typedefs (val); + val = canonicalize_type_argument (val, complain); } else { @@ -6132,8 +6531,9 @@ convert_template_argument (tree parm, if (TREE_CODE (val) == SCOPE_REF) { /* Strip typedefs from the SCOPE_REF. */ - tree type = strip_typedefs (TREE_TYPE (val)); - tree scope = strip_typedefs (TREE_OPERAND (val, 0)); + tree type = canonicalize_type_argument (TREE_TYPE (val), complain); + tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0), + complain); val = build_qualified_name (type, scope, TREE_OPERAND (val, 1), QUALIFIED_NAME_IS_TEMPLATE (val)); } @@ -6295,6 +6695,7 @@ coerce_template_parms (tree parms, subtract it from nparms to get the number of non-variadic parameters. */ int variadic_p = 0; + int post_variadic_parms = 0; if (args == error_mark_node) return error_mark_node; @@ -6305,19 +6706,22 @@ coerce_template_parms (tree parms, for (parm_idx = 0; parm_idx < nparms; ++parm_idx) { tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); + if (variadic_p) + ++post_variadic_parms; if (template_parameter_pack_p (tparm)) ++variadic_p; } inner_args = INNERMOST_TEMPLATE_ARGS (args); - /* If there are 0 or 1 parameter packs, we need to expand any argument - packs so that we can deduce a parameter pack from some non-packed args - followed by an argument pack, as in variadic85.C. If there are more - than that, we need to leave argument packs intact so the arguments are - assigned to the right parameter packs. This should only happen when - dealing with a nested class inside a partial specialization of a class - template, as in variadic92.C. */ - if (variadic_p <= 1) + /* If there are no parameters that follow a parameter pack, we need to + expand any argument packs so that we can deduce a parameter pack from + some non-packed args followed by an argument pack, as in variadic85.C. + If there are such parameters, we need to leave argument packs intact + so the arguments are assigned properly. This can happen when dealing + with a nested class inside a partial specialization of a class + template, as in variadic92.C, or when deducing a template parameter pack + from a sub-declarator, as in variadic114.C. */ + if (!post_variadic_parms) inner_args = expand_template_argument_pack (inner_args); nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; @@ -6332,7 +6736,7 @@ coerce_template_parms (tree parms, { if (variadic_p) { - --nparms; + nparms -= variadic_p; error ("wrong number of template arguments " "(%d, should be %d or more)", nargs, nparms); } @@ -6399,22 +6803,10 @@ coerce_template_parms (tree parms, { if (PACK_EXPANSION_P (arg)) { - if (complain & tf_error) - { - /* FIXME this restriction was removed by N2555; see - bug 35722. */ - /* If ARG is a pack expansion, but PARM is not a - template parameter pack (if it were, we would have - handled it above), we're trying to expand into a - fixed-length argument list. */ - if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) - sorry ("cannot expand %<%E%> into a fixed-length " - "argument list", arg); - else - sorry ("cannot expand %<%T%> into a fixed-length " - "argument list", arg); - } - ++lost; + /* We don't know how many args we have yet, just + use the unconverted ones for now. */ + new_inner_args = args; + break; } } else if (require_all_args) @@ -6474,6 +6866,8 @@ template_args_equal (tree ot, tree nt) { if (nt == ot) return 1; + if (nt == NULL_TREE || ot == NULL_TREE) + return false; if (TREE_CODE (nt) == TREE_VEC) /* For member templates */ @@ -6521,11 +6915,13 @@ template_args_equal (tree ot, tree nt) return cp_tree_equal (ot, nt); } -/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets - of template arguments. Returns 0 otherwise. */ +/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of + template arguments. Returns 0 otherwise, and updates OLDARG_PTR and + NEWARG_PTR with the offending arguments if they are non-NULL. */ -int -comp_template_args (tree oldargs, tree newargs) +static int +comp_template_args_with_info (tree oldargs, tree newargs, + tree *oldarg_ptr, tree *newarg_ptr) { int i; @@ -6538,11 +6934,26 @@ comp_template_args (tree oldargs, tree newargs) tree ot = TREE_VEC_ELT (oldargs, i); if (! template_args_equal (ot, nt)) - return 0; + { + if (oldarg_ptr != NULL) + *oldarg_ptr = ot; + if (newarg_ptr != NULL) + *newarg_ptr = nt; + return 0; + } } return 1; } +/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets + of template arguments. Returns 0 otherwise. */ + +int +comp_template_args (tree oldargs, tree newargs) +{ + return comp_template_args_with_info (oldargs, newargs, NULL, NULL); +} + static void add_pending_template (tree d) { @@ -6593,8 +7004,12 @@ lookup_template_function (tree fns, tree arglist) return error_mark_node; gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); - gcc_assert (fns && (is_overloaded_fn (fns) - || TREE_CODE (fns) == IDENTIFIER_NODE)); + + if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE) + { + error ("%q#D is not a function template", fns); + return error_mark_node; + } if (BASELINK_P (fns)) { @@ -6688,7 +7103,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, { tree templ = NULL_TREE, parmlist; tree t; - spec_entry **slot; + void **slot; spec_entry *entry; spec_entry elt; hashval_t hash; @@ -6995,7 +7410,31 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, ENUM_FIXED_UNDERLYING_TYPE_P (t) = ENUM_FIXED_UNDERLYING_TYPE_P (template_type); } - else + else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)) + { + /* The user referred to a specialization of an alias + template represented by GEN_TMPL. + + [temp.alias]/2 says: + + When a template-id refers to the specialization of an + alias template, it is equivalent to the associated + type obtained by substitution of its + template-arguments for the template-parameters in the + type-id of the alias template. */ + + t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl); + /* Note that the call above (by indirectly calling + register_specialization in tsubst_decl) registers the + TYPE_DECL representing the specialization of the alias + template. So next time someone substitutes ARGLIST for + the template parms into the alias template (GEN_TMPL), + she'll get that TYPE_DECL back. */ + + if (t == error_mark_node) + return t; + } + else if (CLASS_TYPE_P (template_type)) { t = make_class_type (TREE_CODE (template_type)); CLASSTYPE_DECLARED_CLASS (t) @@ -7018,6 +7457,8 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, structural equality testing. */ SET_TYPE_STRUCTURAL_EQUALITY (t); } + else + gcc_unreachable (); /* If we called start_enum or pushtag above, this information will already be set up. */ @@ -7033,14 +7474,17 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, else type_decl = TYPE_NAME (t); - TREE_PRIVATE (type_decl) - = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); - TREE_PROTECTED (type_decl) - = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); - if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) + if (CLASS_TYPE_P (template_type)) { - DECL_VISIBILITY_SPECIFIED (type_decl) = 1; - DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); + TREE_PRIVATE (type_decl) + = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); + TREE_PROTECTED (type_decl) + = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); + if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) + { + DECL_VISIBILITY_SPECIFIED (type_decl) = 1; + DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); + } } /* Let's consider the explicit specialization of a member @@ -7096,7 +7540,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, ++processing_template_decl; partial_inst_args = tsubst (INNERMOST_TEMPLATE_ARGS - (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))), + (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))), arglist, complain, NULL_TREE); --processing_template_decl; TREE_VEC_LENGTH (arglist)++; @@ -7120,16 +7564,25 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, TREE_VEC_LENGTH (arglist)--; found = tsubst (gen_tmpl, arglist, complain, NULL_TREE); TREE_VEC_LENGTH (arglist)++; - found = CLASSTYPE_TI_TEMPLATE (found); + /* FOUND is either a proper class type, or an alias + template specialization. In the later case, it's a + TYPE_DECL, resulting from the substituting of arguments + for parameters in the TYPE_DECL of the alias template + done earlier. So be careful while getting the template + of FOUND. */ + found = TREE_CODE (found) == TYPE_DECL + ? TYPE_TI_TEMPLATE (TREE_TYPE (found)) + : CLASSTYPE_TI_TEMPLATE (found); } SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist)); elt.spec = t; - slot = (spec_entry **) htab_find_slot_with_hash (type_specializations, - &elt, hash, INSERT); - *slot = ggc_alloc_spec_entry (); - **slot = elt; + slot = htab_find_slot_with_hash (type_specializations, + &elt, hash, INSERT); + entry = ggc_alloc_spec_entry (); + *entry = elt; + *slot = entry; /* Note this use of the partial instantiation so we can check it later in maybe_process_partial_specialization. */ @@ -7147,7 +7600,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, the instantiation and exit above. */ tsubst_enum (template_type, t, arglist); - if (is_dependent_type) + if (CLASS_TYPE_P (template_type) && is_dependent_type) /* If the type makes use of template parameters, the code that generates debugging information will crash. */ DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; @@ -7345,6 +7798,7 @@ for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) case MODOP_EXPR: case CAST_EXPR: + case IMPLICIT_CONV_EXPR: case REINTERPRET_CAST_EXPR: case CONST_CAST_EXPR: case STATIC_CAST_EXPR: @@ -7439,7 +7893,7 @@ uses_template_parms (tree t) || EXPR_P (t) || TREE_CODE (t) == TEMPLATE_PARM_INDEX || TREE_CODE (t) == OVERLOAD - || TREE_CODE (t) == BASELINK + || BASELINK_P (t) || TREE_CODE (t) == IDENTIFIER_NODE || TREE_CODE (t) == TRAIT_EXPR || TREE_CODE (t) == CONSTRUCTOR @@ -7466,13 +7920,42 @@ uses_template_parms_level (tree t, int level) /*include_nondeduced_p=*/true); } +/* Returns TRUE iff INST is an instantiation we don't need to do in an + ill-formed translation unit, i.e. a variable or function that isn't + usable in a constant expression. */ + +static inline bool +neglectable_inst_p (tree d) +{ + return (DECL_P (d) + && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d) + : decl_maybe_constant_var_p (d))); +} + +/* Returns TRUE iff we should refuse to instantiate DECL because it's + neglectable and instantiated from within an erroneous instantiation. */ + +static bool +limit_bad_template_recursion (tree decl) +{ + struct tinst_level *lev = current_tinst_level; + int errs = errorcount + sorrycount; + if (lev == NULL || errs == 0 || !neglectable_inst_p (decl)) + return false; + + for (; lev; lev = lev->next) + if (neglectable_inst_p (lev->decl)) + break; + + return (lev && errs > lev->errors); +} + static int tinst_depth; extern int max_tinst_depth; #ifdef GATHER_STATISTICS int depth_reached; #endif -static int tinst_level_tick; -static int last_template_error_tick; +static GTY(()) struct tinst_level *last_error_tinst_level; /* We're starting to instantiate D; record the template instantiation context for diagnostics and to restore it later. */ @@ -7484,25 +7967,31 @@ push_tinst_level (tree d) if (tinst_depth >= max_tinst_depth) { - /* If the instantiation in question still has unbound template parms, - we don't really care if we can't instantiate it, so just return. - This happens with base instantiation for implicit `typename'. */ - if (uses_template_parms (d)) - return 0; - - last_template_error_tick = tinst_level_tick; - error ("template instantiation depth exceeds maximum of %d (use " - "-ftemplate-depth= to increase the maximum) instantiating %qD", - max_tinst_depth, d); + last_error_tinst_level = current_tinst_level; + if (TREE_CODE (d) == TREE_LIST) + error ("template instantiation depth exceeds maximum of %d (use " + "-ftemplate-depth= to increase the maximum) substituting %qS", + max_tinst_depth, d); + else + error ("template instantiation depth exceeds maximum of %d (use " + "-ftemplate-depth= to increase the maximum) instantiating %qD", + max_tinst_depth, d); print_instantiation_context (); return 0; } + /* If the current instantiation caused problems, don't let it instantiate + anything else. Do allow deduction substitution and decls usable in + constant expressions. */ + if (limit_bad_template_recursion (d)) + return 0; + new_level = ggc_alloc_tinst_level (); new_level->decl = d; new_level->locus = input_location; + new_level->errors = errorcount+sorrycount; new_level->in_system_header_p = in_system_header; new_level->next = current_tinst_level; current_tinst_level = new_level; @@ -7513,7 +8002,6 @@ push_tinst_level (tree d) depth_reached = tinst_depth; #endif - ++tinst_level_tick; return 1; } @@ -7528,7 +8016,6 @@ pop_tinst_level (void) input_location = current_tinst_level->locus; current_tinst_level = current_tinst_level->next; --tinst_depth; - ++tinst_level_tick; } /* We're instantiating a deferred template; restore the template @@ -7546,6 +8033,8 @@ reopen_tinst_level (struct tinst_level *level) current_tinst_level = level; pop_tinst_level (); + if (current_tinst_level) + current_tinst_level->errors = errorcount+sorrycount; return level->decl; } @@ -7580,8 +8069,13 @@ parameter_of_template_p (tree parm, tree templ) parms = INNERMOST_TEMPLATE_PARMS (parms); for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) - if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i))) - return true; + { + tree p = TREE_VALUE (TREE_VEC_ELT (parms, i)); + if (parm == p + || (DECL_INITIAL (parm) + && DECL_INITIAL (parm) == DECL_INITIAL (p))) + return true; + } return false; } @@ -8188,16 +8682,6 @@ instantiate_class_template_1 (tree type) input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) = DECL_SOURCE_LOCATION (typedecl); - TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern); - TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); - TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); - TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); - TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern); - TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern); - TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern); - TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern); - TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); - TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); TYPE_PACKED (type) = TYPE_PACKED (pattern); TYPE_ALIGN (type) = TYPE_ALIGN (pattern); TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); @@ -8208,6 +8692,8 @@ instantiate_class_template_1 (tree type) { CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); + /* Adjust visibility for template arguments. */ + determine_visibility (TYPE_MAIN_DECL (type)); } CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern); @@ -8365,6 +8851,9 @@ instantiate_class_template_1 (tree type) --processing_template_decl; set_current_access_from_decl (r); finish_member_declaration (r); + /* Instantiate members marked with attribute used. */ + if (r != error_mark_node && DECL_PRESERVE_P (r)) + mark_used (r); } else { @@ -8414,8 +8903,9 @@ instantiate_class_template_1 (tree type) /*init_const_expr_p=*/false, /*asmspec_tree=*/NULL_TREE, /*flags=*/0); - if (DECL_INITIALIZED_IN_CLASS_P (r)) - check_static_variable_definition (r, TREE_TYPE (r)); + /* Instantiate members marked with attribute used. */ + if (r != error_mark_node && DECL_PRESERVE_P (r)) + mark_used (r); } else if (TREE_CODE (r) == FIELD_DECL) { @@ -8472,7 +8962,8 @@ instantiate_class_template_1 (tree type) friend_type = TREE_TYPE (friend_type); adjust_processing_template_decl = true; } - else if (TREE_CODE (friend_type) == TYPENAME_TYPE) + else if (TREE_CODE (friend_type) == TYPENAME_TYPE + || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) { /* This could be either @@ -8566,6 +9057,18 @@ instantiate_class_template_1 (tree type) } } + if (CLASSTYPE_LAMBDA_EXPR (type)) + { + tree lambda = CLASSTYPE_LAMBDA_EXPR (type); + if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda)) + { + apply_lambda_return_type (lambda, void_type_node); + LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE; + } + instantiate_decl (lambda_function (type), false, false); + maybe_add_lambda_conv_op (type); + } + /* Set the file and line number information to whatever is given for the class itself. This puts error messages involving generated implicit functions at a predictable point, and the same point @@ -8634,15 +9137,20 @@ tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) /*integral_constant_expression_p=*/true); if (!(complain & tf_warning)) --c_inhibit_evaluation_warnings; + /* Preserve the raw-reference nature of T. */ + if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE + && REFERENCE_REF_P (r)) + r = TREE_OPERAND (r, 0); } return r; } -/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a - NONTYPE_ARGUMENT_PACK. */ +/* Given a function parameter pack TMPL_PARM and some function parameters + instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them + and set *SPEC_P to point at the next point in the list. */ static tree -make_fnparm_pack (tree spec_parm) +extract_fnparm_pack (tree tmpl_parm, tree *spec_p) { /* Collect all of the extra "packed" parameters into an argument pack. */ @@ -8650,11 +9158,18 @@ make_fnparm_pack (tree spec_parm) tree parmtypevec; tree argpack = make_node (NONTYPE_ARGUMENT_PACK); tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK); - int i, len = list_length (spec_parm); + tree spec_parm = *spec_p; + int i, len; + + for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm)) + if (tmpl_parm + && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm)) + break; /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ parmvec = make_tree_vec (len); parmtypevec = make_tree_vec (len); + spec_parm = *spec_p; for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm)) { TREE_VEC_ELT (parmvec, i) = spec_parm; @@ -8665,9 +9180,19 @@ make_fnparm_pack (tree spec_parm) SET_ARGUMENT_PACK_ARGS (argpack, parmvec); SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); TREE_TYPE (argpack) = argtypepack; + *spec_p = spec_parm; return argpack; -} +} + +/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a + NONTYPE_ARGUMENT_PACK. */ + +static tree +make_fnparm_pack (tree spec_parm) +{ + return extract_fnparm_pack (NULL_TREE, &spec_parm); +} /* Substitute ARGS into T, which is an pack expansion (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a @@ -8679,10 +9204,10 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, tree in_decl) { tree pattern; - tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE; + tree pack, packs = NULL_TREE; + bool unsubstituted_packs = false; int i, len = -1; tree result; - int incomplete = 0; htab_t saved_local_specializations = NULL; gcc_assert (PACK_EXPANSION_P (t)); @@ -8699,6 +9224,15 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, tree arg_pack = NULL_TREE; tree orig_arg = NULL_TREE; + if (TREE_CODE (parm_pack) == BASES) + { + if (BASES_DIRECT (parm_pack)) + return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack), + args, complain, in_decl, false)); + else + return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack), + args, complain, in_decl, false)); + } if (TREE_CODE (parm_pack) == PARM_DECL) { if (!cp_unevaluated_operand) @@ -8711,7 +9245,12 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, have the wrong value for a recursive call. Just make a dummy decl, since it's only used for its type. */ arg_pack = tsubst_decl (parm_pack, args, complain); - arg_pack = make_fnparm_pack (arg_pack); + if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack)) + /* Partial instantiation of the parm_pack, we can't build + up an argument pack yet. */ + arg_pack = NULL_TREE; + else + arg_pack = make_fnparm_pack (arg_pack); } } else @@ -8737,74 +9276,28 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, return result; } - /* For clarity in the comments below let's use the - representation 'argument_pack' to denote an - argument pack and its elements. - - In the 'if' block below, we want to detect cases where - ARG_PACK is argument_pack. I.e, we want to - check if ARG_PACK is an argument pack which sole element is - the expansion of PARM_PACK. That argument pack is typically - created by template_parm_to_arg when passed a parameter - pack. */ - if (arg_pack - && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 - && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) - { - tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); - tree pattern = PACK_EXPANSION_PATTERN (expansion); - /* So we have an argument_pack. We want to test if P - is actually PARM_PACK. We will not use cp_tree_equal to - test P and PARM_PACK because during type fixup (by - fixup_template_parm) P can be a pre-fixup version of a - type and PARM_PACK be its post-fixup version. - cp_tree_equal would consider them as different even - though we would want to consider them compatible for our - precise purpose here. - - Thus we are going to consider that P and PARM_PACK are - compatible if they have the same DECL. */ - if ((/* If ARG_PACK is a type parameter pack named by the - same DECL as parm_pack ... */ - (TYPE_P (pattern) - && TYPE_P (parm_pack) - && TYPE_NAME (pattern) == TYPE_NAME (parm_pack)) - /* ... or if ARG_PACK is a non-type parameter - named by the same DECL as parm_pack ... */ - || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX - && TREE_CODE (parm_pack) == PARM_DECL - && TEMPLATE_PARM_DECL (pattern) - == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack)))) - && template_parameter_pack_p (pattern)) - /* ... then the argument pack that the parameter maps to - is just an expansion of the parameter itself, such as - one would find in the implicit typedef of a class - inside the class itself. Consider this parameter - "unsubstituted", so that we will maintain the outer - pack expansion. */ - arg_pack = NULL_TREE; - } + if (arg_from_parm_pack_p (arg_pack, parm_pack)) + /* The argument pack that the parameter maps to is just an + expansion of the parameter itself, such as one would find + in the implicit typedef of a class inside the class itself. + Consider this parameter "unsubstituted", so that we will + maintain the outer pack expansion. */ + arg_pack = NULL_TREE; if (arg_pack) { int my_len = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); - /* It's all-or-nothing with incomplete argument packs. */ - if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) - return error_mark_node; - + /* Don't bother trying to do a partial substitution with + incomplete packs; we'll try again after deduction. */ if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) - incomplete = 1; + return t; if (len < 0) len = my_len; else if (len != my_len) { - if (incomplete) - /* We got explicit args for some packs but not others; - do nothing now and try again after deduction. */ - return t; if (TREE_CODE (t) == TYPE_PACK_EXPANSION) error ("mismatched argument pack lengths while expanding " "%<%T%>", @@ -8822,10 +9315,11 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, TREE_TYPE (packs) = orig_arg; } else - /* We can't substitute for this parameter pack. */ - unsubstituted_packs = tree_cons (TREE_PURPOSE (pack), - TREE_VALUE (pack), - unsubstituted_packs); + { + /* We can't substitute for this parameter pack. */ + unsubstituted_packs = true; + break; + } } /* We cannot expand this expansion expression, because we don't have @@ -8862,8 +9356,8 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, /* For each argument in each argument pack, substitute into the pattern. */ - result = make_tree_vec (len + incomplete); - for (i = 0; i < len + incomplete; ++i) + result = make_tree_vec (len); + for (i = 0; i < len; ++i) { /* For parameter pack, change the substitution of the parameter pack to the ith argument in its argument pack, then expand @@ -8871,33 +9365,38 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, for (pack = packs; pack; pack = TREE_CHAIN (pack)) { tree parm = TREE_PURPOSE (pack); + tree arg; + /* Select the Ith argument from the pack. */ if (TREE_CODE (parm) == PARM_DECL) { - /* Select the Ith argument from the pack. */ - tree arg = make_node (ARGUMENT_PACK_SELECT); - ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); - ARGUMENT_PACK_SELECT_INDEX (arg) = i; - mark_used (parm); - register_local_specialization (arg, parm); + if (i == 0) + { + arg = make_node (ARGUMENT_PACK_SELECT); + ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); + mark_used (parm); + register_local_specialization (arg, parm); + } + else + arg = retrieve_local_specialization (parm); } else { - tree value = parm; int idx, level; template_parm_level_and_index (parm, &level, &idx); - - if (i < len) + + if (i == 0) { - /* Select the Ith argument from the pack. */ - value = make_node (ARGUMENT_PACK_SELECT); - ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack); - ARGUMENT_PACK_SELECT_INDEX (value) = i; + arg = make_node (ARGUMENT_PACK_SELECT); + ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); + /* Update the corresponding argument. */ + TMPL_ARG (args, level, idx) = arg; } - - /* Update the corresponding argument. */ - TMPL_ARG (args, level, idx) = value; + else + /* Re-use the ARGUMENT_PACK_SELECT. */ + arg = TMPL_ARG (args, level, idx); } + ARGUMENT_PACK_SELECT_INDEX (arg) = i; } /* Substitute into the PATTERN with the altered arguments. */ @@ -8908,13 +9407,6 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, else TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); - if (i == len) - /* When we have incomplete argument packs, the last "expanded" - result is itself a pack expansion, which allows us - to deduce more arguments. */ - TREE_VEC_ELT (result, i) = - make_pack_expansion (TREE_VEC_ELT (result, i)); - if (TREE_VEC_ELT (result, i) == error_mark_node) { result = error_mark_node; @@ -9222,14 +9714,13 @@ tsubst_aggr_type (tree t, /* First, determine the context for the type we are looking up. */ context = TYPE_CONTEXT (t); - if (context) + if (context && TYPE_P (context)) { context = tsubst_aggr_type (context, args, complain, in_decl, /*entering_scope=*/1); /* If context is a nested class inside a class template, it may still need to be instantiated (c++/33959). */ - if (TYPE_P (context)) - context = complete_type (context); + context = complete_type (context); } /* Then, figure out what arguments are appropriate for the @@ -9446,7 +9937,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) DECL_TEMPLATE_INFO (r) = build_template_info (t, args); - if (TREE_CODE (decl) == TYPE_DECL) + if (TREE_CODE (decl) == TYPE_DECL + && !TYPE_DECL_ALIAS_P (decl)) { tree new_type; ++processing_template_decl; @@ -9536,6 +10028,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (t))), args, complain, in_decl); + if (argvec == error_mark_node) + RETURN (error_mark_node); /* Check to see if we already have this specialization. */ hash = hash_tmpl_and_args (gen_tmpl, argvec); @@ -9801,14 +10295,14 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) if (DECL_TEMPLATE_PARM_P (t)) SET_DECL_TEMPLATE_PARM_P (r); - /* An argument of a function parameter pack is not a parameter - pack. */ - FUNCTION_PARAMETER_PACK_P (r) = false; - if (expanded_types) /* We're on the Ith parameter of the function parameter pack. */ { + /* An argument of a function parameter pack is not a parameter + pack. */ + FUNCTION_PARAMETER_PACK_P (r) = false; + /* Get the Ith type. */ type = TREE_VEC_ELT (expanded_types, i); @@ -9874,11 +10368,24 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) TREE_TYPE (r) = type; cp_apply_type_quals_to_decl (cp_type_quals (type), r); - /* DECL_INITIAL gives the number of bits in a bit-field. */ - DECL_INITIAL (r) - = tsubst_expr (DECL_INITIAL (t), args, - complain, in_decl, - /*integral_constant_expression_p=*/true); + if (DECL_C_BIT_FIELD (r)) + /* For bit-fields, DECL_INITIAL gives the number of bits. For + non-bit-fields DECL_INITIAL is a non-static data member + initializer, which gets deferred instantiation. */ + DECL_INITIAL (r) + = tsubst_expr (DECL_INITIAL (t), args, + complain, in_decl, + /*integral_constant_expression_p=*/true); + else if (DECL_INITIAL (t)) + { + /* Set up DECL_TEMPLATE_INFO so that we can get at the + NSDMI in perform_member_init. Still set DECL_INITIAL + so that we know there is one. */ + DECL_INITIAL (r) = void_zero_node; + gcc_assert (DECL_LANG_SPECIFIC (r) == NULL); + retrofit_lang_decl (r); + DECL_TEMPLATE_INFO (r) = build_template_info (t, args); + } /* We don't have to set DECL_CONTEXT here; it is set by finish_member_declaration. */ DECL_CHAIN (r) = NULL_TREE; @@ -9964,8 +10471,15 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) referencing a static data member within in its own class. We can use pointer equality, rather than same_type_p, because DECL_CONTEXT is always - canonical. */ - if (ctx == DECL_CONTEXT (t)) + canonical... */ + if (ctx == DECL_CONTEXT (t) + && (TREE_CODE (t) != TYPE_DECL + /* ... unless T is a member template; in which + case our caller can be willing to create a + specialization of that template represented + by T. */ + || !(DECL_TI_TEMPLATE (t) + && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))) spec = t; } @@ -10047,6 +10561,11 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) tree ve = DECL_VALUE_EXPR (t); ve = tsubst_expr (ve, args, complain, in_decl, /*constant_expression_p=*/false); + if (REFERENCE_REF_P (ve)) + { + gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); + ve = TREE_OPERAND (ve, 0); + } SET_DECL_VALUE_EXPR (r, ve); } } @@ -10096,18 +10615,14 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain) scope, such as for a lambda return type. Don't add it to local_specializations, do perform auto deduction. */ tree auto_node = type_uses_auto (type); - tree init - = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl, - /*constant_expression_p=*/false); - - if (auto_node && init) + if (auto_node) { + tree init + = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl, + /*constant_expression_p=*/false); init = resolve_nondeduced_context (init); - if (describable_type (init)) - { - type = do_auto_deduction (type, init, auto_node); - TREE_TYPE (r) = type; - } + TREE_TYPE (r) = type + = do_auto_deduction (type, init, auto_node); } } else @@ -10207,7 +10722,7 @@ tsubst_arg_types (tree arg_types, /* Do array-to-pointer, function-to-pointer conversion, and ignore top-level qualifiers as required. */ - type = TYPE_MAIN_VARIANT (type_decays_to (type)); + type = cv_unqualified (type_decays_to (type)); /* We do not substitute into default arguments here. The standard mandates that they be instantiated only when needed, which is @@ -10330,7 +10845,8 @@ static tree tsubst_exception_specification (tree fntype, tree args, tsubst_flags_t complain, - tree in_decl) + tree in_decl, + bool defer_ok) { tree specs; tree new_specs; @@ -10340,9 +10856,33 @@ tsubst_exception_specification (tree fntype, if (specs && TREE_PURPOSE (specs)) { /* A noexcept-specifier. */ - new_specs = tsubst_copy_and_build - (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false, - /*integral_constant_expression_p=*/true); + tree expr = TREE_PURPOSE (specs); + if (expr == boolean_true_node || expr == boolean_false_node) + new_specs = expr; + else if (defer_ok) + { + /* Defer instantiation of noexcept-specifiers to avoid + excessive instantiations (c++/49107). */ + new_specs = make_node (DEFERRED_NOEXCEPT); + if (DEFERRED_NOEXCEPT_SPEC_P (specs)) + { + /* We already partially instantiated this member template, + so combine the new args with the old. */ + DEFERRED_NOEXCEPT_PATTERN (new_specs) + = DEFERRED_NOEXCEPT_PATTERN (expr); + DEFERRED_NOEXCEPT_ARGS (new_specs) + = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args); + } + else + { + DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr; + DEFERRED_NOEXCEPT_ARGS (new_specs) = args; + } + } + else + new_specs = tsubst_copy_and_build + (expr, args, complain, in_decl, /*function_p=*/false, + /*integral_constant_expression_p=*/true); new_specs = build_noexcept_spec (new_specs, complain); } else if (specs) @@ -10420,7 +10960,7 @@ tree tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) { enum tree_code code; - tree type, r; + tree type, r = NULL_TREE; if (t == NULL_TREE || t == error_mark_node || t == integer_type_node @@ -10452,10 +10992,21 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) && typedef_variant_p (t)) { tree decl = TYPE_NAME (t); - - if (DECL_CLASS_SCOPE_P (decl) - && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) - && uses_template_parms (DECL_CONTEXT (decl))) + + if (TYPE_DECL_ALIAS_P (decl) + && DECL_LANG_SPECIFIC (decl) + && DECL_TEMPLATE_INFO (decl) + && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) + { + /* DECL represents an alias template and we want to + instantiate it. Let's substitute our arguments for the + template parameters into the declaration and get the + resulting type. */ + r = tsubst (decl, args, complain, decl); + } + else if (DECL_CLASS_SCOPE_P (decl) + && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) + && uses_template_parms (DECL_CONTEXT (decl))) { tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); @@ -10605,6 +11156,46 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) if (argvec == error_mark_node) return error_mark_node; + gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM + || TREE_CODE (arg) == TEMPLATE_DECL + || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); + + if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) + /* Consider this code: + + template