| 1 | /* Handle parameterized types (templates) for GNU C++. |
| 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 3 | 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012 |
| 4 | Free Software Foundation, Inc. |
| 5 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
| 6 | Rewritten by Jason Merrill (jason@cygnus.com). |
| 7 | |
| 8 | This file is part of GCC. |
| 9 | |
| 10 | GCC is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 3, or (at your option) |
| 13 | any later version. |
| 14 | |
| 15 | GCC is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with GCC; see the file COPYING3. If not see |
| 22 | <http://www.gnu.org/licenses/>. */ |
| 23 | |
| 24 | /* Known bugs or deficiencies include: |
| 25 | |
| 26 | all methods must be provided in header files; can't use a source |
| 27 | file that contains only the method templates and "just win". */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "system.h" |
| 31 | #include "coretypes.h" |
| 32 | #include "tm.h" |
| 33 | #include "tree.h" |
| 34 | #include "intl.h" |
| 35 | #include "pointer-set.h" |
| 36 | #include "flags.h" |
| 37 | #include "cp-tree.h" |
| 38 | #include "c-family/c-common.h" |
| 39 | #include "c-family/c-objc.h" |
| 40 | #include "cp-objcp-common.h" |
| 41 | #include "tree-inline.h" |
| 42 | #include "decl.h" |
| 43 | #include "output.h" |
| 44 | #include "toplev.h" |
| 45 | #include "timevar.h" |
| 46 | #include "tree-iterator.h" |
| 47 | #include "vecprim.h" |
| 48 | |
| 49 | /* The type of functions taking a tree, and some additional data, and |
| 50 | returning an int. */ |
| 51 | typedef int (*tree_fn_t) (tree, void*); |
| 52 | |
| 53 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
| 54 | instantiations have been deferred, either because their definitions |
| 55 | were not yet available, or because we were putting off doing the work. */ |
| 56 | struct GTY ((chain_next ("%h.next"))) pending_template { |
| 57 | struct pending_template *next; |
| 58 | struct tinst_level *tinst; |
| 59 | }; |
| 60 | |
| 61 | static GTY(()) struct pending_template *pending_templates; |
| 62 | static GTY(()) struct pending_template *last_pending_template; |
| 63 | |
| 64 | int processing_template_parmlist; |
| 65 | static int template_header_count; |
| 66 | |
| 67 | static GTY(()) tree saved_trees; |
| 68 | static VEC(int,heap) *inline_parm_levels; |
| 69 | |
| 70 | static GTY(()) struct tinst_level *current_tinst_level; |
| 71 | |
| 72 | static GTY(()) tree saved_access_scope; |
| 73 | |
| 74 | /* Live only within one (recursive) call to tsubst_expr. We use |
| 75 | this to pass the statement expression node from the STMT_EXPR |
| 76 | to the EXPR_STMT that is its result. */ |
| 77 | static tree cur_stmt_expr; |
| 78 | |
| 79 | /* A map from local variable declarations in the body of the template |
| 80 | presently being instantiated to the corresponding instantiated |
| 81 | local variables. */ |
| 82 | static htab_t local_specializations; |
| 83 | |
| 84 | typedef struct GTY(()) spec_entry |
| 85 | { |
| 86 | tree tmpl; |
| 87 | tree args; |
| 88 | tree spec; |
| 89 | } spec_entry; |
| 90 | |
| 91 | static GTY ((param_is (spec_entry))) |
| 92 | htab_t decl_specializations; |
| 93 | |
| 94 | static GTY ((param_is (spec_entry))) |
| 95 | htab_t type_specializations; |
| 96 | |
| 97 | /* Contains canonical template parameter types. The vector is indexed by |
| 98 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
| 99 | TREE_LIST, whose TREE_VALUEs contain the canonical template |
| 100 | parameters of various types and levels. */ |
| 101 | static GTY(()) VEC(tree,gc) *canonical_template_parms; |
| 102 | |
| 103 | #define UNIFY_ALLOW_NONE 0 |
| 104 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 |
| 105 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 |
| 106 | #define UNIFY_ALLOW_DERIVED 4 |
| 107 | #define UNIFY_ALLOW_INTEGER 8 |
| 108 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
| 109 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
| 110 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 |
| 111 | |
| 112 | enum template_base_result { |
| 113 | tbr_incomplete_type, |
| 114 | tbr_ambiguous_baseclass, |
| 115 | tbr_success |
| 116 | }; |
| 117 | |
| 118 | static void push_access_scope (tree); |
| 119 | static void pop_access_scope (tree); |
| 120 | static void push_deduction_access_scope (tree); |
| 121 | static void pop_deduction_access_scope (tree); |
| 122 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
| 123 | unification_kind_t, int, |
| 124 | bool); |
| 125 | static int try_one_overload (tree, tree, tree, tree, tree, |
| 126 | unification_kind_t, int, bool, bool); |
| 127 | static int unify (tree, tree, tree, tree, int, bool); |
| 128 | static void add_pending_template (tree); |
| 129 | static tree reopen_tinst_level (struct tinst_level *); |
| 130 | static tree tsubst_initializer_list (tree, tree); |
| 131 | static tree get_class_bindings (tree, tree, tree); |
| 132 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
| 133 | bool, bool); |
| 134 | static void tsubst_enum (tree, tree, tree); |
| 135 | static tree add_to_template_args (tree, tree); |
| 136 | static tree add_outermost_template_args (tree, tree); |
| 137 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); |
| 138 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*, |
| 139 | tree); |
| 140 | static int type_unification_real (tree, tree, tree, const tree *, |
| 141 | unsigned int, int, unification_kind_t, int, |
| 142 | bool); |
| 143 | static void note_template_header (int); |
| 144 | static tree convert_nontype_argument_function (tree, tree); |
| 145 | static tree convert_nontype_argument (tree, tree, tsubst_flags_t); |
| 146 | static tree convert_template_argument (tree, tree, tree, |
| 147 | tsubst_flags_t, int, tree); |
| 148 | static int for_each_template_parm (tree, tree_fn_t, void*, |
| 149 | struct pointer_set_t*, bool); |
| 150 | static tree expand_template_argument_pack (tree); |
| 151 | static tree build_template_parm_index (int, int, int, tree, tree); |
| 152 | static bool inline_needs_template_parms (tree); |
| 153 | static void push_inline_template_parms_recursive (tree, int); |
| 154 | static tree retrieve_local_specialization (tree); |
| 155 | static void register_local_specialization (tree, tree); |
| 156 | static hashval_t hash_specialization (const void *p); |
| 157 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
| 158 | static int mark_template_parm (tree, void *); |
| 159 | static int template_parm_this_level_p (tree, void *); |
| 160 | static tree tsubst_friend_function (tree, tree); |
| 161 | static tree tsubst_friend_class (tree, tree); |
| 162 | static int can_complete_type_without_circularity (tree); |
| 163 | static tree get_bindings (tree, tree, tree, bool); |
| 164 | static int template_decl_level (tree); |
| 165 | static int check_cv_quals_for_unify (int, tree, tree); |
| 166 | static void template_parm_level_and_index (tree, int*, int*); |
| 167 | static int unify_pack_expansion (tree, tree, tree, |
| 168 | tree, unification_kind_t, bool, bool); |
| 169 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
| 170 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); |
| 171 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
| 172 | static void regenerate_decl_from_template (tree, tree); |
| 173 | static tree most_specialized_class (tree, tree, tsubst_flags_t); |
| 174 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
| 175 | static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); |
| 176 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); |
| 177 | static bool check_specialization_scope (void); |
| 178 | static tree process_partial_specialization (tree); |
| 179 | static void set_current_access_from_decl (tree); |
| 180 | static enum template_base_result get_template_base (tree, tree, tree, tree, |
| 181 | bool , tree *); |
| 182 | static tree try_class_unification (tree, tree, tree, tree, bool); |
| 183 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, |
| 184 | tree, tree); |
| 185 | static bool template_template_parm_bindings_ok_p (tree, tree); |
| 186 | static int template_args_equal (tree, tree); |
| 187 | static void tsubst_default_arguments (tree); |
| 188 | static tree for_each_template_parm_r (tree *, int *, void *); |
| 189 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); |
| 190 | static void copy_default_args_to_explicit_spec (tree); |
| 191 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); |
| 192 | static int eq_local_specializations (const void *, const void *); |
| 193 | static bool dependent_template_arg_p (tree); |
| 194 | static bool any_template_arguments_need_structural_equality_p (tree); |
| 195 | static bool dependent_type_p_r (tree); |
| 196 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
| 197 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
| 198 | static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree); |
| 199 | static tree tsubst_decl (tree, tree, tsubst_flags_t); |
| 200 | static void perform_typedefs_access_check (tree tmpl, tree targs); |
| 201 | static void append_type_to_template_for_access_check_1 (tree, tree, tree, |
| 202 | location_t); |
| 203 | static tree listify (tree); |
| 204 | static tree listify_autos (tree, tree); |
| 205 | static tree template_parm_to_arg (tree t); |
| 206 | static bool arg_from_parm_pack_p (tree, tree); |
| 207 | static tree current_template_args (void); |
| 208 | static tree tsubst_template_parm (tree, tree, tsubst_flags_t); |
| 209 | |
| 210 | /* Make the current scope suitable for access checking when we are |
| 211 | processing T. T can be FUNCTION_DECL for instantiated function |
| 212 | template, or VAR_DECL for static member variable (need by |
| 213 | instantiate_decl). */ |
| 214 | |
| 215 | static void |
| 216 | push_access_scope (tree t) |
| 217 | { |
| 218 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
| 219 | || TREE_CODE (t) == VAR_DECL); |
| 220 | |
| 221 | if (DECL_FRIEND_CONTEXT (t)) |
| 222 | push_nested_class (DECL_FRIEND_CONTEXT (t)); |
| 223 | else if (DECL_CLASS_SCOPE_P (t)) |
| 224 | push_nested_class (DECL_CONTEXT (t)); |
| 225 | else |
| 226 | push_to_top_level (); |
| 227 | |
| 228 | if (TREE_CODE (t) == FUNCTION_DECL) |
| 229 | { |
| 230 | saved_access_scope = tree_cons |
| 231 | (NULL_TREE, current_function_decl, saved_access_scope); |
| 232 | current_function_decl = t; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* Restore the scope set up by push_access_scope. T is the node we |
| 237 | are processing. */ |
| 238 | |
| 239 | static void |
| 240 | pop_access_scope (tree t) |
| 241 | { |
| 242 | if (TREE_CODE (t) == FUNCTION_DECL) |
| 243 | { |
| 244 | current_function_decl = TREE_VALUE (saved_access_scope); |
| 245 | saved_access_scope = TREE_CHAIN (saved_access_scope); |
| 246 | } |
| 247 | |
| 248 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
| 249 | pop_nested_class (); |
| 250 | else |
| 251 | pop_from_top_level (); |
| 252 | } |
| 253 | |
| 254 | /* Do any processing required when DECL (a member template |
| 255 | declaration) is finished. Returns the TEMPLATE_DECL corresponding |
| 256 | to DECL, unless it is a specialization, in which case the DECL |
| 257 | itself is returned. */ |
| 258 | |
| 259 | tree |
| 260 | finish_member_template_decl (tree decl) |
| 261 | { |
| 262 | if (decl == error_mark_node) |
| 263 | return error_mark_node; |
| 264 | |
| 265 | gcc_assert (DECL_P (decl)); |
| 266 | |
| 267 | if (TREE_CODE (decl) == TYPE_DECL) |
| 268 | { |
| 269 | tree type; |
| 270 | |
| 271 | type = TREE_TYPE (decl); |
| 272 | if (type == error_mark_node) |
| 273 | return error_mark_node; |
| 274 | if (MAYBE_CLASS_TYPE_P (type) |
| 275 | && CLASSTYPE_TEMPLATE_INFO (type) |
| 276 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
| 277 | { |
| 278 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
| 279 | check_member_template (tmpl); |
| 280 | return tmpl; |
| 281 | } |
| 282 | return NULL_TREE; |
| 283 | } |
| 284 | else if (TREE_CODE (decl) == FIELD_DECL) |
| 285 | error ("data member %qD cannot be a member template", decl); |
| 286 | else if (DECL_TEMPLATE_INFO (decl)) |
| 287 | { |
| 288 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
| 289 | { |
| 290 | check_member_template (DECL_TI_TEMPLATE (decl)); |
| 291 | return DECL_TI_TEMPLATE (decl); |
| 292 | } |
| 293 | else |
| 294 | return decl; |
| 295 | } |
| 296 | else |
| 297 | error ("invalid member template declaration %qD", decl); |
| 298 | |
| 299 | return error_mark_node; |
| 300 | } |
| 301 | |
| 302 | /* Create a template info node. */ |
| 303 | |
| 304 | tree |
| 305 | build_template_info (tree template_decl, tree template_args) |
| 306 | { |
| 307 | tree result = make_node (TEMPLATE_INFO); |
| 308 | TI_TEMPLATE (result) = template_decl; |
| 309 | TI_ARGS (result) = template_args; |
| 310 | return result; |
| 311 | } |
| 312 | |
| 313 | /* Return the template info node corresponding to T, whatever T is. */ |
| 314 | |
| 315 | tree |
| 316 | get_template_info (const_tree t) |
| 317 | { |
| 318 | tree tinfo = NULL_TREE; |
| 319 | |
| 320 | if (!t || t == error_mark_node) |
| 321 | return NULL; |
| 322 | |
| 323 | if (DECL_P (t) && DECL_LANG_SPECIFIC (t)) |
| 324 | tinfo = DECL_TEMPLATE_INFO (t); |
| 325 | |
| 326 | if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t)) |
| 327 | t = TREE_TYPE (t); |
| 328 | |
| 329 | if (TAGGED_TYPE_P (t)) |
| 330 | tinfo = TYPE_TEMPLATE_INFO (t); |
| 331 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 332 | tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t); |
| 333 | |
| 334 | return tinfo; |
| 335 | } |
| 336 | |
| 337 | /* Returns the template nesting level of the indicated class TYPE. |
| 338 | |
| 339 | For example, in: |
| 340 | template <class T> |
| 341 | struct A |
| 342 | { |
| 343 | template <class U> |
| 344 | struct B {}; |
| 345 | }; |
| 346 | |
| 347 | A<T>::B<U> has depth two, while A<T> has depth one. |
| 348 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
| 349 | they are instantiations, not specializations. |
| 350 | |
| 351 | This function is guaranteed to return 0 if passed NULL_TREE so |
| 352 | that, for example, `template_class_depth (current_class_type)' is |
| 353 | always safe. */ |
| 354 | |
| 355 | int |
| 356 | template_class_depth (tree type) |
| 357 | { |
| 358 | int depth; |
| 359 | |
| 360 | for (depth = 0; |
| 361 | type && TREE_CODE (type) != NAMESPACE_DECL; |
| 362 | type = (TREE_CODE (type) == FUNCTION_DECL) |
| 363 | ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type)) |
| 364 | { |
| 365 | tree tinfo = get_template_info (type); |
| 366 | |
| 367 | if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) |
| 368 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) |
| 369 | ++depth; |
| 370 | } |
| 371 | |
| 372 | return depth; |
| 373 | } |
| 374 | |
| 375 | /* Subroutine of maybe_begin_member_template_processing. |
| 376 | Returns true if processing DECL needs us to push template parms. */ |
| 377 | |
| 378 | static bool |
| 379 | inline_needs_template_parms (tree decl) |
| 380 | { |
| 381 | if (! DECL_TEMPLATE_INFO (decl)) |
| 382 | return false; |
| 383 | |
| 384 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
| 385 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
| 386 | } |
| 387 | |
| 388 | /* Subroutine of maybe_begin_member_template_processing. |
| 389 | Push the template parms in PARMS, starting from LEVELS steps into the |
| 390 | chain, and ending at the beginning, since template parms are listed |
| 391 | innermost first. */ |
| 392 | |
| 393 | static void |
| 394 | push_inline_template_parms_recursive (tree parmlist, int levels) |
| 395 | { |
| 396 | tree parms = TREE_VALUE (parmlist); |
| 397 | int i; |
| 398 | |
| 399 | if (levels > 1) |
| 400 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); |
| 401 | |
| 402 | ++processing_template_decl; |
| 403 | current_template_parms |
| 404 | = tree_cons (size_int (processing_template_decl), |
| 405 | parms, current_template_parms); |
| 406 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
| 407 | |
| 408 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
| 409 | NULL); |
| 410 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
| 411 | { |
| 412 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
| 413 | |
| 414 | if (parm == error_mark_node) |
| 415 | continue; |
| 416 | |
| 417 | gcc_assert (DECL_P (parm)); |
| 418 | |
| 419 | switch (TREE_CODE (parm)) |
| 420 | { |
| 421 | case TYPE_DECL: |
| 422 | case TEMPLATE_DECL: |
| 423 | pushdecl (parm); |
| 424 | break; |
| 425 | |
| 426 | case PARM_DECL: |
| 427 | { |
| 428 | /* Make a CONST_DECL as is done in process_template_parm. |
| 429 | It is ugly that we recreate this here; the original |
| 430 | version built in process_template_parm is no longer |
| 431 | available. */ |
| 432 | tree decl = build_decl (DECL_SOURCE_LOCATION (parm), |
| 433 | CONST_DECL, DECL_NAME (parm), |
| 434 | TREE_TYPE (parm)); |
| 435 | DECL_ARTIFICIAL (decl) = 1; |
| 436 | TREE_CONSTANT (decl) = 1; |
| 437 | TREE_READONLY (decl) = 1; |
| 438 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
| 439 | SET_DECL_TEMPLATE_PARM_P (decl); |
| 440 | pushdecl (decl); |
| 441 | } |
| 442 | break; |
| 443 | |
| 444 | default: |
| 445 | gcc_unreachable (); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /* Restore the template parameter context for a member template or |
| 451 | a friend template defined in a class definition. */ |
| 452 | |
| 453 | void |
| 454 | maybe_begin_member_template_processing (tree decl) |
| 455 | { |
| 456 | tree parms; |
| 457 | int levels = 0; |
| 458 | |
| 459 | if (inline_needs_template_parms (decl)) |
| 460 | { |
| 461 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); |
| 462 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; |
| 463 | |
| 464 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
| 465 | { |
| 466 | --levels; |
| 467 | parms = TREE_CHAIN (parms); |
| 468 | } |
| 469 | |
| 470 | push_inline_template_parms_recursive (parms, levels); |
| 471 | } |
| 472 | |
| 473 | /* Remember how many levels of template parameters we pushed so that |
| 474 | we can pop them later. */ |
| 475 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
| 476 | } |
| 477 | |
| 478 | /* Undo the effects of maybe_begin_member_template_processing. */ |
| 479 | |
| 480 | void |
| 481 | maybe_end_member_template_processing (void) |
| 482 | { |
| 483 | int i; |
| 484 | int last; |
| 485 | |
| 486 | if (VEC_length (int, inline_parm_levels) == 0) |
| 487 | return; |
| 488 | |
| 489 | last = VEC_pop (int, inline_parm_levels); |
| 490 | for (i = 0; i < last; ++i) |
| 491 | { |
| 492 | --processing_template_decl; |
| 493 | current_template_parms = TREE_CHAIN (current_template_parms); |
| 494 | poplevel (0, 0, 0); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* Return a new template argument vector which contains all of ARGS, |
| 499 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
| 500 | |
| 501 | static tree |
| 502 | add_to_template_args (tree args, tree extra_args) |
| 503 | { |
| 504 | tree new_args; |
| 505 | int extra_depth; |
| 506 | int i; |
| 507 | int j; |
| 508 | |
| 509 | if (args == NULL_TREE || extra_args == error_mark_node) |
| 510 | return extra_args; |
| 511 | |
| 512 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
| 513 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
| 514 | |
| 515 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
| 516 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); |
| 517 | |
| 518 | for (j = 1; j <= extra_depth; ++j, ++i) |
| 519 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); |
| 520 | |
| 521 | return new_args; |
| 522 | } |
| 523 | |
| 524 | /* Like add_to_template_args, but only the outermost ARGS are added to |
| 525 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH |
| 526 | (EXTRA_ARGS) levels are added. This function is used to combine |
| 527 | the template arguments from a partial instantiation with the |
| 528 | template arguments used to attain the full instantiation from the |
| 529 | partial instantiation. */ |
| 530 | |
| 531 | static tree |
| 532 | add_outermost_template_args (tree args, tree extra_args) |
| 533 | { |
| 534 | tree new_args; |
| 535 | |
| 536 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
| 537 | something very fishy is going on. */ |
| 538 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
| 539 | |
| 540 | /* If *all* the new arguments will be the EXTRA_ARGS, just return |
| 541 | them. */ |
| 542 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) |
| 543 | return extra_args; |
| 544 | |
| 545 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
| 546 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); |
| 547 | |
| 548 | new_args = add_to_template_args (args, extra_args); |
| 549 | |
| 550 | /* Now, we restore ARGS to its full dimensions. */ |
| 551 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); |
| 552 | |
| 553 | return new_args; |
| 554 | } |
| 555 | |
| 556 | /* Return the N levels of innermost template arguments from the ARGS. */ |
| 557 | |
| 558 | tree |
| 559 | get_innermost_template_args (tree args, int n) |
| 560 | { |
| 561 | tree new_args; |
| 562 | int extra_levels; |
| 563 | int i; |
| 564 | |
| 565 | gcc_assert (n >= 0); |
| 566 | |
| 567 | /* If N is 1, just return the innermost set of template arguments. */ |
| 568 | if (n == 1) |
| 569 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
| 570 | |
| 571 | /* If we're not removing anything, just return the arguments we were |
| 572 | given. */ |
| 573 | extra_levels = TMPL_ARGS_DEPTH (args) - n; |
| 574 | gcc_assert (extra_levels >= 0); |
| 575 | if (extra_levels == 0) |
| 576 | return args; |
| 577 | |
| 578 | /* Make a new set of arguments, not containing the outer arguments. */ |
| 579 | new_args = make_tree_vec (n); |
| 580 | for (i = 1; i <= n; ++i) |
| 581 | SET_TMPL_ARGS_LEVEL (new_args, i, |
| 582 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
| 583 | |
| 584 | return new_args; |
| 585 | } |
| 586 | |
| 587 | /* The inverse of get_innermost_template_args: Return all but the innermost |
| 588 | EXTRA_LEVELS levels of template arguments from the ARGS. */ |
| 589 | |
| 590 | static tree |
| 591 | strip_innermost_template_args (tree args, int extra_levels) |
| 592 | { |
| 593 | tree new_args; |
| 594 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; |
| 595 | int i; |
| 596 | |
| 597 | gcc_assert (n >= 0); |
| 598 | |
| 599 | /* If N is 1, just return the outermost set of template arguments. */ |
| 600 | if (n == 1) |
| 601 | return TMPL_ARGS_LEVEL (args, 1); |
| 602 | |
| 603 | /* If we're not removing anything, just return the arguments we were |
| 604 | given. */ |
| 605 | gcc_assert (extra_levels >= 0); |
| 606 | if (extra_levels == 0) |
| 607 | return args; |
| 608 | |
| 609 | /* Make a new set of arguments, not containing the inner arguments. */ |
| 610 | new_args = make_tree_vec (n); |
| 611 | for (i = 1; i <= n; ++i) |
| 612 | SET_TMPL_ARGS_LEVEL (new_args, i, |
| 613 | TMPL_ARGS_LEVEL (args, i)); |
| 614 | |
| 615 | return new_args; |
| 616 | } |
| 617 | |
| 618 | /* We've got a template header coming up; push to a new level for storing |
| 619 | the parms. */ |
| 620 | |
| 621 | void |
| 622 | begin_template_parm_list (void) |
| 623 | { |
| 624 | /* We use a non-tag-transparent scope here, which causes pushtag to |
| 625 | put tags in this scope, rather than in the enclosing class or |
| 626 | namespace scope. This is the right thing, since we want |
| 627 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a |
| 628 | global template class, push_template_decl handles putting the |
| 629 | TEMPLATE_DECL into top-level scope. For a nested template class, |
| 630 | e.g.: |
| 631 | |
| 632 | template <class T> struct S1 { |
| 633 | template <class T> struct S2 {}; |
| 634 | }; |
| 635 | |
| 636 | pushtag contains special code to call pushdecl_with_scope on the |
| 637 | TEMPLATE_DECL for S2. */ |
| 638 | begin_scope (sk_template_parms, NULL); |
| 639 | ++processing_template_decl; |
| 640 | ++processing_template_parmlist; |
| 641 | note_template_header (0); |
| 642 | } |
| 643 | |
| 644 | /* This routine is called when a specialization is declared. If it is |
| 645 | invalid to declare a specialization here, an error is reported and |
| 646 | false is returned, otherwise this routine will return true. */ |
| 647 | |
| 648 | static bool |
| 649 | check_specialization_scope (void) |
| 650 | { |
| 651 | tree scope = current_scope (); |
| 652 | |
| 653 | /* [temp.expl.spec] |
| 654 | |
| 655 | An explicit specialization shall be declared in the namespace of |
| 656 | which the template is a member, or, for member templates, in the |
| 657 | namespace of which the enclosing class or enclosing class |
| 658 | template is a member. An explicit specialization of a member |
| 659 | function, member class or static data member of a class template |
| 660 | shall be declared in the namespace of which the class template |
| 661 | is a member. */ |
| 662 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) |
| 663 | { |
| 664 | error ("explicit specialization in non-namespace scope %qD", scope); |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | /* [temp.expl.spec] |
| 669 | |
| 670 | In an explicit specialization declaration for a member of a class |
| 671 | template or a member template that appears in namespace scope, |
| 672 | the member template and some of its enclosing class templates may |
| 673 | remain unspecialized, except that the declaration shall not |
| 674 | explicitly specialize a class member template if its enclosing |
| 675 | class templates are not explicitly specialized as well. */ |
| 676 | if (current_template_parms) |
| 677 | { |
| 678 | error ("enclosing class templates are not explicitly specialized"); |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | /* We've just seen template <>. */ |
| 686 | |
| 687 | bool |
| 688 | begin_specialization (void) |
| 689 | { |
| 690 | begin_scope (sk_template_spec, NULL); |
| 691 | note_template_header (1); |
| 692 | return check_specialization_scope (); |
| 693 | } |
| 694 | |
| 695 | /* Called at then end of processing a declaration preceded by |
| 696 | template<>. */ |
| 697 | |
| 698 | void |
| 699 | end_specialization (void) |
| 700 | { |
| 701 | finish_scope (); |
| 702 | reset_specialization (); |
| 703 | } |
| 704 | |
| 705 | /* Any template <>'s that we have seen thus far are not referring to a |
| 706 | function specialization. */ |
| 707 | |
| 708 | void |
| 709 | reset_specialization (void) |
| 710 | { |
| 711 | processing_specialization = 0; |
| 712 | template_header_count = 0; |
| 713 | } |
| 714 | |
| 715 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
| 716 | it was of the form template <>. */ |
| 717 | |
| 718 | static void |
| 719 | note_template_header (int specialization) |
| 720 | { |
| 721 | processing_specialization = specialization; |
| 722 | template_header_count++; |
| 723 | } |
| 724 | |
| 725 | /* We're beginning an explicit instantiation. */ |
| 726 | |
| 727 | void |
| 728 | begin_explicit_instantiation (void) |
| 729 | { |
| 730 | gcc_assert (!processing_explicit_instantiation); |
| 731 | processing_explicit_instantiation = true; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | void |
| 736 | end_explicit_instantiation (void) |
| 737 | { |
| 738 | gcc_assert (processing_explicit_instantiation); |
| 739 | processing_explicit_instantiation = false; |
| 740 | } |
| 741 | |
| 742 | /* An explicit specialization or partial specialization TMPL is being |
| 743 | declared. Check that the namespace in which the specialization is |
| 744 | occurring is permissible. Returns false iff it is invalid to |
| 745 | specialize TMPL in the current namespace. */ |
| 746 | |
| 747 | static bool |
| 748 | check_specialization_namespace (tree tmpl) |
| 749 | { |
| 750 | tree tpl_ns = decl_namespace_context (tmpl); |
| 751 | |
| 752 | /* [tmpl.expl.spec] |
| 753 | |
| 754 | An explicit specialization shall be declared in the namespace of |
| 755 | which the template is a member, or, for member templates, in the |
| 756 | namespace of which the enclosing class or enclosing class |
| 757 | template is a member. An explicit specialization of a member |
| 758 | function, member class or static data member of a class template |
| 759 | shall be declared in the namespace of which the class template is |
| 760 | a member. */ |
| 761 | if (current_scope() != DECL_CONTEXT (tmpl) |
| 762 | && !at_namespace_scope_p ()) |
| 763 | { |
| 764 | error ("specialization of %qD must appear at namespace scope", tmpl); |
| 765 | return false; |
| 766 | } |
| 767 | if (is_associated_namespace (current_namespace, tpl_ns)) |
| 768 | /* Same or super-using namespace. */ |
| 769 | return true; |
| 770 | else |
| 771 | { |
| 772 | permerror (input_location, "specialization of %qD in different namespace", tmpl); |
| 773 | permerror (input_location, " from definition of %q+#D", tmpl); |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* SPEC is an explicit instantiation. Check that it is valid to |
| 779 | perform this explicit instantiation in the current namespace. */ |
| 780 | |
| 781 | static void |
| 782 | check_explicit_instantiation_namespace (tree spec) |
| 783 | { |
| 784 | tree ns; |
| 785 | |
| 786 | /* DR 275: An explicit instantiation shall appear in an enclosing |
| 787 | namespace of its template. */ |
| 788 | ns = decl_namespace_context (spec); |
| 789 | if (!is_ancestor (current_namespace, ns)) |
| 790 | permerror (input_location, "explicit instantiation of %qD in namespace %qD " |
| 791 | "(which does not enclose namespace %qD)", |
| 792 | spec, current_namespace, ns); |
| 793 | } |
| 794 | |
| 795 | /* The TYPE is being declared. If it is a template type, that means it |
| 796 | is a partial specialization. Do appropriate error-checking. */ |
| 797 | |
| 798 | tree |
| 799 | maybe_process_partial_specialization (tree type) |
| 800 | { |
| 801 | tree context; |
| 802 | |
| 803 | if (type == error_mark_node) |
| 804 | return error_mark_node; |
| 805 | |
| 806 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 807 | { |
| 808 | error ("name of class shadows template template parameter %qD", |
| 809 | TYPE_NAME (type)); |
| 810 | return error_mark_node; |
| 811 | } |
| 812 | |
| 813 | context = TYPE_CONTEXT (type); |
| 814 | |
| 815 | if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
| 816 | /* Consider non-class instantiations of alias templates as |
| 817 | well. */ |
| 818 | || (TYPE_P (type) |
| 819 | && TYPE_TEMPLATE_INFO (type) |
| 820 | && DECL_LANG_SPECIFIC (TYPE_NAME (type)) |
| 821 | && DECL_USE_TEMPLATE (TYPE_NAME (type)))) |
| 822 | { |
| 823 | /* This is for ordinary explicit specialization and partial |
| 824 | specialization of a template class such as: |
| 825 | |
| 826 | template <> class C<int>; |
| 827 | |
| 828 | or: |
| 829 | |
| 830 | template <class T> class C<T*>; |
| 831 | |
| 832 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ |
| 833 | |
| 834 | if (CLASS_TYPE_P (type) |
| 835 | && CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
| 836 | && !COMPLETE_TYPE_P (type)) |
| 837 | { |
| 838 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
| 839 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
| 840 | if (processing_template_decl) |
| 841 | { |
| 842 | if (push_template_decl (TYPE_MAIN_DECL (type)) |
| 843 | == error_mark_node) |
| 844 | return error_mark_node; |
| 845 | } |
| 846 | } |
| 847 | else if (CLASS_TYPE_P (type) |
| 848 | && CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
| 849 | error ("specialization of %qT after instantiation", type); |
| 850 | |
| 851 | if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type))) |
| 852 | { |
| 853 | error ("partial specialization of alias template %qD", |
| 854 | TYPE_TI_TEMPLATE (type)); |
| 855 | return error_mark_node; |
| 856 | } |
| 857 | } |
| 858 | else if (CLASS_TYPE_P (type) |
| 859 | && !CLASSTYPE_USE_TEMPLATE (type) |
| 860 | && CLASSTYPE_TEMPLATE_INFO (type) |
| 861 | && context && CLASS_TYPE_P (context) |
| 862 | && CLASSTYPE_TEMPLATE_INFO (context)) |
| 863 | { |
| 864 | /* This is for an explicit specialization of member class |
| 865 | template according to [temp.expl.spec/18]: |
| 866 | |
| 867 | template <> template <class U> class C<int>::D; |
| 868 | |
| 869 | The context `C<int>' must be an implicit instantiation. |
| 870 | Otherwise this is just a member class template declared |
| 871 | earlier like: |
| 872 | |
| 873 | template <> class C<int> { template <class U> class D; }; |
| 874 | template <> template <class U> class C<int>::D; |
| 875 | |
| 876 | In the first case, `C<int>::D' is a specialization of `C<T>::D' |
| 877 | while in the second case, `C<int>::D' is a primary template |
| 878 | and `C<T>::D' may not exist. */ |
| 879 | |
| 880 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) |
| 881 | && !COMPLETE_TYPE_P (type)) |
| 882 | { |
| 883 | tree t; |
| 884 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
| 885 | |
| 886 | if (current_namespace |
| 887 | != decl_namespace_context (tmpl)) |
| 888 | { |
| 889 | permerror (input_location, "specializing %q#T in different namespace", type); |
| 890 | permerror (input_location, " from definition of %q+#D", tmpl); |
| 891 | } |
| 892 | |
| 893 | /* Check for invalid specialization after instantiation: |
| 894 | |
| 895 | template <> template <> class C<int>::D<int>; |
| 896 | template <> template <class U> class C<int>::D; */ |
| 897 | |
| 898 | for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
| 899 | t; t = TREE_CHAIN (t)) |
| 900 | { |
| 901 | tree inst = TREE_VALUE (t); |
| 902 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)) |
| 903 | { |
| 904 | /* We already have a full specialization of this partial |
| 905 | instantiation. Reassign it to the new member |
| 906 | specialization template. */ |
| 907 | spec_entry elt; |
| 908 | spec_entry *entry; |
| 909 | void **slot; |
| 910 | |
| 911 | elt.tmpl = most_general_template (tmpl); |
| 912 | elt.args = CLASSTYPE_TI_ARGS (inst); |
| 913 | elt.spec = inst; |
| 914 | |
| 915 | htab_remove_elt (type_specializations, &elt); |
| 916 | |
| 917 | elt.tmpl = tmpl; |
| 918 | elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); |
| 919 | |
| 920 | slot = htab_find_slot (type_specializations, &elt, INSERT); |
| 921 | entry = ggc_alloc_spec_entry (); |
| 922 | *entry = elt; |
| 923 | *slot = entry; |
| 924 | } |
| 925 | else if (COMPLETE_OR_OPEN_TYPE_P (inst)) |
| 926 | /* But if we've had an implicit instantiation, that's a |
| 927 | problem ([temp.expl.spec]/6). */ |
| 928 | error ("specialization %qT after instantiation %qT", |
| 929 | type, inst); |
| 930 | } |
| 931 | |
| 932 | /* Mark TYPE as a specialization. And as a result, we only |
| 933 | have one level of template argument for the innermost |
| 934 | class template. */ |
| 935 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
| 936 | CLASSTYPE_TI_ARGS (type) |
| 937 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
| 938 | } |
| 939 | } |
| 940 | else if (processing_specialization) |
| 941 | { |
| 942 | /* Someday C++0x may allow for enum template specialization. */ |
| 943 | if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE |
| 944 | && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context)) |
| 945 | pedwarn (input_location, OPT_pedantic, "template specialization " |
| 946 | "of %qD not allowed by ISO C++", type); |
| 947 | else |
| 948 | { |
| 949 | error ("explicit specialization of non-template %qT", type); |
| 950 | return error_mark_node; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | return type; |
| 955 | } |
| 956 | |
| 957 | /* Returns nonzero if we can optimize the retrieval of specializations |
| 958 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
| 959 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ |
| 960 | |
| 961 | static inline bool |
| 962 | optimize_specialization_lookup_p (tree tmpl) |
| 963 | { |
| 964 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) |
| 965 | && DECL_CLASS_SCOPE_P (tmpl) |
| 966 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template |
| 967 | parameter. */ |
| 968 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) |
| 969 | /* The optimized lookup depends on the fact that the |
| 970 | template arguments for the member function template apply |
| 971 | purely to the containing class, which is not true if the |
| 972 | containing class is an explicit or partial |
| 973 | specialization. */ |
| 974 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) |
| 975 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
| 976 | && !DECL_CONV_FN_P (tmpl) |
| 977 | /* It is possible to have a template that is not a member |
| 978 | template and is not a member of a template class: |
| 979 | |
| 980 | template <typename T> |
| 981 | struct S { friend A::f(); }; |
| 982 | |
| 983 | Here, the friend function is a template, but the context does |
| 984 | not have template information. The optimized lookup relies |
| 985 | on having ARGS be the template arguments for both the class |
| 986 | and the function template. */ |
| 987 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); |
| 988 | } |
| 989 | |
| 990 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
| 991 | specialization is either an instantiation or an explicit |
| 992 | specialization) of TMPL for the given template ARGS. If there is |
| 993 | no such specialization, return NULL_TREE. The ARGS are a vector of |
| 994 | arguments, or a vector of vectors of arguments, in the case of |
| 995 | templates with more than one level of parameters. |
| 996 | |
| 997 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, |
| 998 | then we search for a partial specialization matching ARGS. This |
| 999 | parameter is ignored if TMPL is not a class template. */ |
| 1000 | |
| 1001 | static tree |
| 1002 | retrieve_specialization (tree tmpl, tree args, hashval_t hash) |
| 1003 | { |
| 1004 | if (args == error_mark_node) |
| 1005 | return NULL_TREE; |
| 1006 | |
| 1007 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
| 1008 | |
| 1009 | /* There should be as many levels of arguments as there are |
| 1010 | levels of parameters. */ |
| 1011 | gcc_assert (TMPL_ARGS_DEPTH (args) |
| 1012 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
| 1013 | |
| 1014 | if (optimize_specialization_lookup_p (tmpl)) |
| 1015 | { |
| 1016 | tree class_template; |
| 1017 | tree class_specialization; |
| 1018 | VEC(tree,gc) *methods; |
| 1019 | tree fns; |
| 1020 | int idx; |
| 1021 | |
| 1022 | /* The template arguments actually apply to the containing |
| 1023 | class. Find the class specialization with those |
| 1024 | arguments. */ |
| 1025 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); |
| 1026 | class_specialization |
| 1027 | = retrieve_specialization (class_template, args, 0); |
| 1028 | if (!class_specialization) |
| 1029 | return NULL_TREE; |
| 1030 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC |
| 1031 | for the specialization. */ |
| 1032 | idx = class_method_index_for_fn (class_specialization, tmpl); |
| 1033 | if (idx == -1) |
| 1034 | return NULL_TREE; |
| 1035 | /* Iterate through the methods with the indicated name, looking |
| 1036 | for the one that has an instance of TMPL. */ |
| 1037 | methods = CLASSTYPE_METHOD_VEC (class_specialization); |
| 1038 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) |
| 1039 | { |
| 1040 | tree fn = OVL_CURRENT (fns); |
| 1041 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl |
| 1042 | /* using-declarations can add base methods to the method vec, |
| 1043 | and we don't want those here. */ |
| 1044 | && DECL_CONTEXT (fn) == class_specialization) |
| 1045 | return fn; |
| 1046 | } |
| 1047 | return NULL_TREE; |
| 1048 | } |
| 1049 | else |
| 1050 | { |
| 1051 | spec_entry *found; |
| 1052 | spec_entry elt; |
| 1053 | htab_t specializations; |
| 1054 | |
| 1055 | elt.tmpl = tmpl; |
| 1056 | elt.args = args; |
| 1057 | elt.spec = NULL_TREE; |
| 1058 | |
| 1059 | if (DECL_CLASS_TEMPLATE_P (tmpl)) |
| 1060 | specializations = type_specializations; |
| 1061 | else |
| 1062 | specializations = decl_specializations; |
| 1063 | |
| 1064 | if (hash == 0) |
| 1065 | hash = hash_specialization (&elt); |
| 1066 | found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash); |
| 1067 | if (found) |
| 1068 | return found->spec; |
| 1069 | } |
| 1070 | |
| 1071 | return NULL_TREE; |
| 1072 | } |
| 1073 | |
| 1074 | /* Like retrieve_specialization, but for local declarations. */ |
| 1075 | |
| 1076 | static tree |
| 1077 | retrieve_local_specialization (tree tmpl) |
| 1078 | { |
| 1079 | tree spec; |
| 1080 | |
| 1081 | if (local_specializations == NULL) |
| 1082 | return NULL_TREE; |
| 1083 | |
| 1084 | spec = (tree) htab_find_with_hash (local_specializations, tmpl, |
| 1085 | htab_hash_pointer (tmpl)); |
| 1086 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
| 1087 | } |
| 1088 | |
| 1089 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
| 1090 | |
| 1091 | int |
| 1092 | is_specialization_of (tree decl, tree tmpl) |
| 1093 | { |
| 1094 | tree t; |
| 1095 | |
| 1096 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 1097 | { |
| 1098 | for (t = decl; |
| 1099 | t != NULL_TREE; |
| 1100 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) |
| 1101 | if (t == tmpl) |
| 1102 | return 1; |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
| 1107 | |
| 1108 | for (t = TREE_TYPE (decl); |
| 1109 | t != NULL_TREE; |
| 1110 | t = CLASSTYPE_USE_TEMPLATE (t) |
| 1111 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) |
| 1112 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
| 1113 | return 1; |
| 1114 | } |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | /* Returns nonzero iff DECL is a specialization of friend declaration |
| 1120 | FRIEND_DECL according to [temp.friend]. */ |
| 1121 | |
| 1122 | bool |
| 1123 | is_specialization_of_friend (tree decl, tree friend_decl) |
| 1124 | { |
| 1125 | bool need_template = true; |
| 1126 | int template_depth; |
| 1127 | |
| 1128 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
| 1129 | || TREE_CODE (decl) == TYPE_DECL); |
| 1130 | |
| 1131 | /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function |
| 1132 | of a template class, we want to check if DECL is a specialization |
| 1133 | if this. */ |
| 1134 | if (TREE_CODE (friend_decl) == FUNCTION_DECL |
| 1135 | && DECL_TEMPLATE_INFO (friend_decl) |
| 1136 | && !DECL_USE_TEMPLATE (friend_decl)) |
| 1137 | { |
| 1138 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
| 1139 | friend_decl = DECL_TI_TEMPLATE (friend_decl); |
| 1140 | need_template = false; |
| 1141 | } |
| 1142 | else if (TREE_CODE (friend_decl) == TEMPLATE_DECL |
| 1143 | && !PRIMARY_TEMPLATE_P (friend_decl)) |
| 1144 | need_template = false; |
| 1145 | |
| 1146 | /* There is nothing to do if this is not a template friend. */ |
| 1147 | if (TREE_CODE (friend_decl) != TEMPLATE_DECL) |
| 1148 | return false; |
| 1149 | |
| 1150 | if (is_specialization_of (decl, friend_decl)) |
| 1151 | return true; |
| 1152 | |
| 1153 | /* [temp.friend/6] |
| 1154 | A member of a class template may be declared to be a friend of a |
| 1155 | non-template class. In this case, the corresponding member of |
| 1156 | every specialization of the class template is a friend of the |
| 1157 | class granting friendship. |
| 1158 | |
| 1159 | For example, given a template friend declaration |
| 1160 | |
| 1161 | template <class T> friend void A<T>::f(); |
| 1162 | |
| 1163 | the member function below is considered a friend |
| 1164 | |
| 1165 | template <> struct A<int> { |
| 1166 | void f(); |
| 1167 | }; |
| 1168 | |
| 1169 | For this type of template friend, TEMPLATE_DEPTH below will be |
| 1170 | nonzero. To determine if DECL is a friend of FRIEND, we first |
| 1171 | check if the enclosing class is a specialization of another. */ |
| 1172 | |
| 1173 | template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl)); |
| 1174 | if (template_depth |
| 1175 | && DECL_CLASS_SCOPE_P (decl) |
| 1176 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
| 1177 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) |
| 1178 | { |
| 1179 | /* Next, we check the members themselves. In order to handle |
| 1180 | a few tricky cases, such as when FRIEND_DECL's are |
| 1181 | |
| 1182 | template <class T> friend void A<T>::g(T t); |
| 1183 | template <class T> template <T t> friend void A<T>::h(); |
| 1184 | |
| 1185 | and DECL's are |
| 1186 | |
| 1187 | void A<int>::g(int); |
| 1188 | template <int> void A<int>::h(); |
| 1189 | |
| 1190 | we need to figure out ARGS, the template arguments from |
| 1191 | the context of DECL. This is required for template substitution |
| 1192 | of `T' in the function parameter of `g' and template parameter |
| 1193 | of `h' in the above examples. Here ARGS corresponds to `int'. */ |
| 1194 | |
| 1195 | tree context = DECL_CONTEXT (decl); |
| 1196 | tree args = NULL_TREE; |
| 1197 | int current_depth = 0; |
| 1198 | |
| 1199 | while (current_depth < template_depth) |
| 1200 | { |
| 1201 | if (CLASSTYPE_TEMPLATE_INFO (context)) |
| 1202 | { |
| 1203 | if (current_depth == 0) |
| 1204 | args = TYPE_TI_ARGS (context); |
| 1205 | else |
| 1206 | args = add_to_template_args (TYPE_TI_ARGS (context), args); |
| 1207 | current_depth++; |
| 1208 | } |
| 1209 | context = TYPE_CONTEXT (context); |
| 1210 | } |
| 1211 | |
| 1212 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 1213 | { |
| 1214 | bool is_template; |
| 1215 | tree friend_type; |
| 1216 | tree decl_type; |
| 1217 | tree friend_args_type; |
| 1218 | tree decl_args_type; |
| 1219 | |
| 1220 | /* Make sure that both DECL and FRIEND_DECL are templates or |
| 1221 | non-templates. */ |
| 1222 | is_template = DECL_TEMPLATE_INFO (decl) |
| 1223 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); |
| 1224 | if (need_template ^ is_template) |
| 1225 | return false; |
| 1226 | else if (is_template) |
| 1227 | { |
| 1228 | /* If both are templates, check template parameter list. */ |
| 1229 | tree friend_parms |
| 1230 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
| 1231 | args, tf_none); |
| 1232 | if (!comp_template_parms |
| 1233 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), |
| 1234 | friend_parms)) |
| 1235 | return false; |
| 1236 | |
| 1237 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); |
| 1238 | } |
| 1239 | else |
| 1240 | decl_type = TREE_TYPE (decl); |
| 1241 | |
| 1242 | friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, |
| 1243 | tf_none, NULL_TREE); |
| 1244 | if (friend_type == error_mark_node) |
| 1245 | return false; |
| 1246 | |
| 1247 | /* Check if return types match. */ |
| 1248 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) |
| 1249 | return false; |
| 1250 | |
| 1251 | /* Check if function parameter types match, ignoring the |
| 1252 | `this' parameter. */ |
| 1253 | friend_args_type = TYPE_ARG_TYPES (friend_type); |
| 1254 | decl_args_type = TYPE_ARG_TYPES (decl_type); |
| 1255 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl)) |
| 1256 | friend_args_type = TREE_CHAIN (friend_args_type); |
| 1257 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
| 1258 | decl_args_type = TREE_CHAIN (decl_args_type); |
| 1259 | |
| 1260 | return compparms (decl_args_type, friend_args_type); |
| 1261 | } |
| 1262 | else |
| 1263 | { |
| 1264 | /* DECL is a TYPE_DECL */ |
| 1265 | bool is_template; |
| 1266 | tree decl_type = TREE_TYPE (decl); |
| 1267 | |
| 1268 | /* Make sure that both DECL and FRIEND_DECL are templates or |
| 1269 | non-templates. */ |
| 1270 | is_template |
| 1271 | = CLASSTYPE_TEMPLATE_INFO (decl_type) |
| 1272 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); |
| 1273 | |
| 1274 | if (need_template ^ is_template) |
| 1275 | return false; |
| 1276 | else if (is_template) |
| 1277 | { |
| 1278 | tree friend_parms; |
| 1279 | /* If both are templates, check the name of the two |
| 1280 | TEMPLATE_DECL's first because is_friend didn't. */ |
| 1281 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) |
| 1282 | != DECL_NAME (friend_decl)) |
| 1283 | return false; |
| 1284 | |
| 1285 | /* Now check template parameter list. */ |
| 1286 | friend_parms |
| 1287 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
| 1288 | args, tf_none); |
| 1289 | return comp_template_parms |
| 1290 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), |
| 1291 | friend_parms); |
| 1292 | } |
| 1293 | else |
| 1294 | return (DECL_NAME (decl) |
| 1295 | == DECL_NAME (friend_decl)); |
| 1296 | } |
| 1297 | } |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
| 1301 | /* Register the specialization SPEC as a specialization of TMPL with |
| 1302 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
| 1303 | is actually just a friend declaration. Returns SPEC, or an |
| 1304 | equivalent prior declaration, if available. */ |
| 1305 | |
| 1306 | static tree |
| 1307 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend, |
| 1308 | hashval_t hash) |
| 1309 | { |
| 1310 | tree fn; |
| 1311 | void **slot = NULL; |
| 1312 | spec_entry elt; |
| 1313 | |
| 1314 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)); |
| 1315 | |
| 1316 | if (TREE_CODE (spec) == FUNCTION_DECL |
| 1317 | && uses_template_parms (DECL_TI_ARGS (spec))) |
| 1318 | /* This is the FUNCTION_DECL for a partial instantiation. Don't |
| 1319 | register it; we want the corresponding TEMPLATE_DECL instead. |
| 1320 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than |
| 1321 | the more obvious `uses_template_parms (spec)' to avoid problems |
| 1322 | with default function arguments. In particular, given |
| 1323 | something like this: |
| 1324 | |
| 1325 | template <class T> void f(T t1, T t = T()) |
| 1326 | |
| 1327 | the default argument expression is not substituted for in an |
| 1328 | instantiation unless and until it is actually needed. */ |
| 1329 | return spec; |
| 1330 | |
| 1331 | if (optimize_specialization_lookup_p (tmpl)) |
| 1332 | /* We don't put these specializations in the hash table, but we might |
| 1333 | want to give an error about a mismatch. */ |
| 1334 | fn = retrieve_specialization (tmpl, args, 0); |
| 1335 | else |
| 1336 | { |
| 1337 | elt.tmpl = tmpl; |
| 1338 | elt.args = args; |
| 1339 | elt.spec = spec; |
| 1340 | |
| 1341 | if (hash == 0) |
| 1342 | hash = hash_specialization (&elt); |
| 1343 | |
| 1344 | slot = |
| 1345 | htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT); |
| 1346 | if (*slot) |
| 1347 | fn = ((spec_entry *) *slot)->spec; |
| 1348 | else |
| 1349 | fn = NULL_TREE; |
| 1350 | } |
| 1351 | |
| 1352 | /* We can sometimes try to re-register a specialization that we've |
| 1353 | already got. In particular, regenerate_decl_from_template calls |
| 1354 | duplicate_decls which will update the specialization list. But, |
| 1355 | we'll still get called again here anyhow. It's more convenient |
| 1356 | to simply allow this than to try to prevent it. */ |
| 1357 | if (fn == spec) |
| 1358 | return spec; |
| 1359 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) |
| 1360 | { |
| 1361 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
| 1362 | { |
| 1363 | if (DECL_ODR_USED (fn) |
| 1364 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
| 1365 | { |
| 1366 | error ("specialization of %qD after instantiation", |
| 1367 | fn); |
| 1368 | return error_mark_node; |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | tree clone; |
| 1373 | /* This situation should occur only if the first |
| 1374 | specialization is an implicit instantiation, the |
| 1375 | second is an explicit specialization, and the |
| 1376 | implicit instantiation has not yet been used. That |
| 1377 | situation can occur if we have implicitly |
| 1378 | instantiated a member function and then specialized |
| 1379 | it later. |
| 1380 | |
| 1381 | We can also wind up here if a friend declaration that |
| 1382 | looked like an instantiation turns out to be a |
| 1383 | specialization: |
| 1384 | |
| 1385 | template <class T> void foo(T); |
| 1386 | class S { friend void foo<>(int) }; |
| 1387 | template <> void foo(int); |
| 1388 | |
| 1389 | We transform the existing DECL in place so that any |
| 1390 | pointers to it become pointers to the updated |
| 1391 | declaration. |
| 1392 | |
| 1393 | If there was a definition for the template, but not |
| 1394 | for the specialization, we want this to look as if |
| 1395 | there were no definition, and vice versa. */ |
| 1396 | DECL_INITIAL (fn) = NULL_TREE; |
| 1397 | duplicate_decls (spec, fn, is_friend); |
| 1398 | /* The call to duplicate_decls will have applied |
| 1399 | [temp.expl.spec]: |
| 1400 | |
| 1401 | An explicit specialization of a function template |
| 1402 | is inline only if it is explicitly declared to be, |
| 1403 | and independently of whether its function template |
| 1404 | is. |
| 1405 | |
| 1406 | to the primary function; now copy the inline bits to |
| 1407 | the various clones. */ |
| 1408 | FOR_EACH_CLONE (clone, fn) |
| 1409 | { |
| 1410 | DECL_DECLARED_INLINE_P (clone) |
| 1411 | = DECL_DECLARED_INLINE_P (fn); |
| 1412 | DECL_SOURCE_LOCATION (clone) |
| 1413 | = DECL_SOURCE_LOCATION (fn); |
| 1414 | } |
| 1415 | check_specialization_namespace (fn); |
| 1416 | |
| 1417 | return fn; |
| 1418 | } |
| 1419 | } |
| 1420 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
| 1421 | { |
| 1422 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
| 1423 | /* Dup decl failed, but this is a new definition. Set the |
| 1424 | line number so any errors match this new |
| 1425 | definition. */ |
| 1426 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); |
| 1427 | |
| 1428 | return fn; |
| 1429 | } |
| 1430 | } |
| 1431 | else if (fn) |
| 1432 | return duplicate_decls (spec, fn, is_friend); |
| 1433 | |
| 1434 | /* A specialization must be declared in the same namespace as the |
| 1435 | template it is specializing. */ |
| 1436 | if (DECL_TEMPLATE_SPECIALIZATION (spec) |
| 1437 | && !check_specialization_namespace (tmpl)) |
| 1438 | DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); |
| 1439 | |
| 1440 | if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */) |
| 1441 | { |
| 1442 | spec_entry *entry = ggc_alloc_spec_entry (); |
| 1443 | gcc_assert (tmpl && args && spec); |
| 1444 | *entry = elt; |
| 1445 | *slot = entry; |
| 1446 | if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) |
| 1447 | && PRIMARY_TEMPLATE_P (tmpl) |
| 1448 | && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) |
| 1449 | /* TMPL is a forward declaration of a template function; keep a list |
| 1450 | of all specializations in case we need to reassign them to a friend |
| 1451 | template later in tsubst_friend_function. */ |
| 1452 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) |
| 1453 | = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); |
| 1454 | } |
| 1455 | |
| 1456 | return spec; |
| 1457 | } |
| 1458 | |
| 1459 | /* Returns true iff two spec_entry nodes are equivalent. Only compares the |
| 1460 | TMPL and ARGS members, ignores SPEC. */ |
| 1461 | |
| 1462 | int comparing_specializations; |
| 1463 | |
| 1464 | static int |
| 1465 | eq_specializations (const void *p1, const void *p2) |
| 1466 | { |
| 1467 | const spec_entry *e1 = (const spec_entry *)p1; |
| 1468 | const spec_entry *e2 = (const spec_entry *)p2; |
| 1469 | int equal; |
| 1470 | |
| 1471 | ++comparing_specializations; |
| 1472 | equal = (e1->tmpl == e2->tmpl |
| 1473 | && comp_template_args (e1->args, e2->args)); |
| 1474 | --comparing_specializations; |
| 1475 | |
| 1476 | return equal; |
| 1477 | } |
| 1478 | |
| 1479 | /* Returns a hash for a template TMPL and template arguments ARGS. */ |
| 1480 | |
| 1481 | static hashval_t |
| 1482 | hash_tmpl_and_args (tree tmpl, tree args) |
| 1483 | { |
| 1484 | hashval_t val = DECL_UID (tmpl); |
| 1485 | return iterative_hash_template_arg (args, val); |
| 1486 | } |
| 1487 | |
| 1488 | /* Returns a hash for a spec_entry node based on the TMPL and ARGS members, |
| 1489 | ignoring SPEC. */ |
| 1490 | |
| 1491 | static hashval_t |
| 1492 | hash_specialization (const void *p) |
| 1493 | { |
| 1494 | const spec_entry *e = (const spec_entry *)p; |
| 1495 | return hash_tmpl_and_args (e->tmpl, e->args); |
| 1496 | } |
| 1497 | |
| 1498 | /* Recursively calculate a hash value for a template argument ARG, for use |
| 1499 | in the hash tables of template specializations. */ |
| 1500 | |
| 1501 | hashval_t |
| 1502 | iterative_hash_template_arg (tree arg, hashval_t val) |
| 1503 | { |
| 1504 | unsigned HOST_WIDE_INT i; |
| 1505 | enum tree_code code; |
| 1506 | char tclass; |
| 1507 | |
| 1508 | if (arg == NULL_TREE) |
| 1509 | return iterative_hash_object (arg, val); |
| 1510 | |
| 1511 | if (!TYPE_P (arg)) |
| 1512 | STRIP_NOPS (arg); |
| 1513 | |
| 1514 | if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
| 1515 | /* We can get one of these when re-hashing a previous entry in the middle |
| 1516 | of substituting into a pack expansion. Just look through it. */ |
| 1517 | arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg); |
| 1518 | |
| 1519 | code = TREE_CODE (arg); |
| 1520 | tclass = TREE_CODE_CLASS (code); |
| 1521 | |
| 1522 | val = iterative_hash_object (code, val); |
| 1523 | |
| 1524 | switch (code) |
| 1525 | { |
| 1526 | case ERROR_MARK: |
| 1527 | return val; |
| 1528 | |
| 1529 | case IDENTIFIER_NODE: |
| 1530 | return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val); |
| 1531 | |
| 1532 | case TREE_VEC: |
| 1533 | { |
| 1534 | int i, len = TREE_VEC_LENGTH (arg); |
| 1535 | for (i = 0; i < len; ++i) |
| 1536 | val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val); |
| 1537 | return val; |
| 1538 | } |
| 1539 | |
| 1540 | case TYPE_PACK_EXPANSION: |
| 1541 | case EXPR_PACK_EXPANSION: |
| 1542 | val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val); |
| 1543 | return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val); |
| 1544 | |
| 1545 | case TYPE_ARGUMENT_PACK: |
| 1546 | case NONTYPE_ARGUMENT_PACK: |
| 1547 | return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val); |
| 1548 | |
| 1549 | case TREE_LIST: |
| 1550 | for (; arg; arg = TREE_CHAIN (arg)) |
| 1551 | val = iterative_hash_template_arg (TREE_VALUE (arg), val); |
| 1552 | return val; |
| 1553 | |
| 1554 | case OVERLOAD: |
| 1555 | for (; arg; arg = OVL_NEXT (arg)) |
| 1556 | val = iterative_hash_template_arg (OVL_CURRENT (arg), val); |
| 1557 | return val; |
| 1558 | |
| 1559 | case CONSTRUCTOR: |
| 1560 | { |
| 1561 | tree field, value; |
| 1562 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value) |
| 1563 | { |
| 1564 | val = iterative_hash_template_arg (field, val); |
| 1565 | val = iterative_hash_template_arg (value, val); |
| 1566 | } |
| 1567 | return val; |
| 1568 | } |
| 1569 | |
| 1570 | case PARM_DECL: |
| 1571 | if (!DECL_ARTIFICIAL (arg)) |
| 1572 | { |
| 1573 | val = iterative_hash_object (DECL_PARM_INDEX (arg), val); |
| 1574 | val = iterative_hash_object (DECL_PARM_LEVEL (arg), val); |
| 1575 | } |
| 1576 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
| 1577 | |
| 1578 | case TARGET_EXPR: |
| 1579 | return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val); |
| 1580 | |
| 1581 | case PTRMEM_CST: |
| 1582 | val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val); |
| 1583 | return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val); |
| 1584 | |
| 1585 | case TEMPLATE_PARM_INDEX: |
| 1586 | val = iterative_hash_template_arg |
| 1587 | (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val); |
| 1588 | val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val); |
| 1589 | return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val); |
| 1590 | |
| 1591 | case TRAIT_EXPR: |
| 1592 | val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val); |
| 1593 | val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val); |
| 1594 | return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val); |
| 1595 | |
| 1596 | case BASELINK: |
| 1597 | val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)), |
| 1598 | val); |
| 1599 | return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)), |
| 1600 | val); |
| 1601 | |
| 1602 | case MODOP_EXPR: |
| 1603 | val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val); |
| 1604 | code = TREE_CODE (TREE_OPERAND (arg, 1)); |
| 1605 | val = iterative_hash_object (code, val); |
| 1606 | return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val); |
| 1607 | |
| 1608 | case LAMBDA_EXPR: |
| 1609 | /* A lambda can't appear in a template arg, but don't crash on |
| 1610 | erroneous input. */ |
| 1611 | gcc_assert (seen_error ()); |
| 1612 | return val; |
| 1613 | |
| 1614 | case CAST_EXPR: |
| 1615 | case IMPLICIT_CONV_EXPR: |
| 1616 | case STATIC_CAST_EXPR: |
| 1617 | case REINTERPRET_CAST_EXPR: |
| 1618 | case CONST_CAST_EXPR: |
| 1619 | case DYNAMIC_CAST_EXPR: |
| 1620 | case NEW_EXPR: |
| 1621 | val = iterative_hash_template_arg (TREE_TYPE (arg), val); |
| 1622 | /* Now hash operands as usual. */ |
| 1623 | break; |
| 1624 | |
| 1625 | default: |
| 1626 | break; |
| 1627 | } |
| 1628 | |
| 1629 | switch (tclass) |
| 1630 | { |
| 1631 | case tcc_type: |
| 1632 | if (TYPE_CANONICAL (arg)) |
| 1633 | return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)), |
| 1634 | val); |
| 1635 | else if (TREE_CODE (arg) == DECLTYPE_TYPE) |
| 1636 | return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val); |
| 1637 | /* Otherwise just compare the types during lookup. */ |
| 1638 | return val; |
| 1639 | |
| 1640 | case tcc_declaration: |
| 1641 | case tcc_constant: |
| 1642 | return iterative_hash_expr (arg, val); |
| 1643 | |
| 1644 | default: |
| 1645 | gcc_assert (IS_EXPR_CODE_CLASS (tclass)); |
| 1646 | { |
| 1647 | unsigned n = cp_tree_operand_length (arg); |
| 1648 | for (i = 0; i < n; ++i) |
| 1649 | val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); |
| 1650 | return val; |
| 1651 | } |
| 1652 | } |
| 1653 | gcc_unreachable (); |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
| 1657 | /* Unregister the specialization SPEC as a specialization of TMPL. |
| 1658 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
| 1659 | if the SPEC was listed as a specialization of TMPL. |
| 1660 | |
| 1661 | Note that SPEC has been ggc_freed, so we can't look inside it. */ |
| 1662 | |
| 1663 | bool |
| 1664 | reregister_specialization (tree spec, tree tinfo, tree new_spec) |
| 1665 | { |
| 1666 | spec_entry *entry; |
| 1667 | spec_entry elt; |
| 1668 | |
| 1669 | elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); |
| 1670 | elt.args = TI_ARGS (tinfo); |
| 1671 | elt.spec = NULL_TREE; |
| 1672 | |
| 1673 | entry = (spec_entry *) htab_find (decl_specializations, &elt); |
| 1674 | if (entry != NULL) |
| 1675 | { |
| 1676 | gcc_assert (entry->spec == spec || entry->spec == new_spec); |
| 1677 | gcc_assert (new_spec != NULL_TREE); |
| 1678 | entry->spec = new_spec; |
| 1679 | return 1; |
| 1680 | } |
| 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | /* Compare an entry in the local specializations hash table P1 (which |
| 1686 | is really a pointer to a TREE_LIST) with P2 (which is really a |
| 1687 | DECL). */ |
| 1688 | |
| 1689 | static int |
| 1690 | eq_local_specializations (const void *p1, const void *p2) |
| 1691 | { |
| 1692 | return TREE_VALUE ((const_tree) p1) == (const_tree) p2; |
| 1693 | } |
| 1694 | |
| 1695 | /* Hash P1, an entry in the local specializations table. */ |
| 1696 | |
| 1697 | static hashval_t |
| 1698 | hash_local_specialization (const void* p1) |
| 1699 | { |
| 1700 | return htab_hash_pointer (TREE_VALUE ((const_tree) p1)); |
| 1701 | } |
| 1702 | |
| 1703 | /* Like register_specialization, but for local declarations. We are |
| 1704 | registering SPEC, an instantiation of TMPL. */ |
| 1705 | |
| 1706 | static void |
| 1707 | register_local_specialization (tree spec, tree tmpl) |
| 1708 | { |
| 1709 | void **slot; |
| 1710 | |
| 1711 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
| 1712 | htab_hash_pointer (tmpl), INSERT); |
| 1713 | *slot = build_tree_list (spec, tmpl); |
| 1714 | } |
| 1715 | |
| 1716 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
| 1717 | specialized class. */ |
| 1718 | |
| 1719 | bool |
| 1720 | explicit_class_specialization_p (tree type) |
| 1721 | { |
| 1722 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
| 1723 | return false; |
| 1724 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); |
| 1725 | } |
| 1726 | |
| 1727 | /* Print the list of functions at FNS, going through all the overloads |
| 1728 | for each element of the list. Alternatively, FNS can not be a |
| 1729 | TREE_LIST, in which case it will be printed together with all the |
| 1730 | overloads. |
| 1731 | |
| 1732 | MORE and *STR should respectively be FALSE and NULL when the function |
| 1733 | is called from the outside. They are used internally on recursive |
| 1734 | calls. print_candidates manages the two parameters and leaves NULL |
| 1735 | in *STR when it ends. */ |
| 1736 | |
| 1737 | static void |
| 1738 | print_candidates_1 (tree fns, bool more, const char **str) |
| 1739 | { |
| 1740 | tree fn, fn2; |
| 1741 | char *spaces = NULL; |
| 1742 | |
| 1743 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
| 1744 | if (TREE_CODE (fn) == TREE_LIST) |
| 1745 | { |
| 1746 | for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2)) |
| 1747 | print_candidates_1 (TREE_VALUE (fn2), |
| 1748 | TREE_CHAIN (fn2) || more, str); |
| 1749 | } |
| 1750 | else |
| 1751 | { |
| 1752 | if (!*str) |
| 1753 | { |
| 1754 | /* Pick the prefix string. */ |
| 1755 | if (!more && !OVL_NEXT (fns)) |
| 1756 | { |
| 1757 | error ("candidate is: %+#D", OVL_CURRENT (fn)); |
| 1758 | continue; |
| 1759 | } |
| 1760 | |
| 1761 | *str = _("candidates are:"); |
| 1762 | spaces = get_spaces (*str); |
| 1763 | } |
| 1764 | error ("%s %+#D", *str, OVL_CURRENT (fn)); |
| 1765 | *str = spaces ? spaces : *str; |
| 1766 | } |
| 1767 | |
| 1768 | if (!more) |
| 1769 | { |
| 1770 | free (spaces); |
| 1771 | *str = NULL; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | /* Print the list of candidate FNS in an error message. FNS can also |
| 1776 | be a TREE_LIST of non-functions in the case of an ambiguous lookup. */ |
| 1777 | |
| 1778 | void |
| 1779 | print_candidates (tree fns) |
| 1780 | { |
| 1781 | const char *str = NULL; |
| 1782 | print_candidates_1 (fns, false, &str); |
| 1783 | gcc_assert (str == NULL); |
| 1784 | } |
| 1785 | |
| 1786 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
| 1787 | which can be specialized to match the indicated DECL with the |
| 1788 | explicit template args given in TEMPLATE_ID. The DECL may be |
| 1789 | NULL_TREE if none is available. In that case, the functions in |
| 1790 | TEMPLATE_ID are non-members. |
| 1791 | |
| 1792 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
| 1793 | specialization of a member template. |
| 1794 | |
| 1795 | The TEMPLATE_COUNT is the number of references to qualifying |
| 1796 | template classes that appeared in the name of the function. See |
| 1797 | check_explicit_specialization for a more accurate description. |
| 1798 | |
| 1799 | TSK indicates what kind of template declaration (if any) is being |
| 1800 | declared. TSK_TEMPLATE indicates that the declaration given by |
| 1801 | DECL, though a FUNCTION_DECL, has template parameters, and is |
| 1802 | therefore a template function. |
| 1803 | |
| 1804 | The template args (those explicitly specified and those deduced) |
| 1805 | are output in a newly created vector *TARGS_OUT. |
| 1806 | |
| 1807 | If it is impossible to determine the result, an error message is |
| 1808 | issued. The error_mark_node is returned to indicate failure. */ |
| 1809 | |
| 1810 | static tree |
| 1811 | determine_specialization (tree template_id, |
| 1812 | tree decl, |
| 1813 | tree* targs_out, |
| 1814 | int need_member_template, |
| 1815 | int template_count, |
| 1816 | tmpl_spec_kind tsk) |
| 1817 | { |
| 1818 | tree fns; |
| 1819 | tree targs; |
| 1820 | tree explicit_targs; |
| 1821 | tree candidates = NULL_TREE; |
| 1822 | /* A TREE_LIST of templates of which DECL may be a specialization. |
| 1823 | The TREE_VALUE of each node is a TEMPLATE_DECL. The |
| 1824 | corresponding TREE_PURPOSE is the set of template arguments that, |
| 1825 | when used to instantiate the template, would produce a function |
| 1826 | with the signature of DECL. */ |
| 1827 | tree templates = NULL_TREE; |
| 1828 | int header_count; |
| 1829 | cp_binding_level *b; |
| 1830 | |
| 1831 | *targs_out = NULL_TREE; |
| 1832 | |
| 1833 | if (template_id == error_mark_node || decl == error_mark_node) |
| 1834 | return error_mark_node; |
| 1835 | |
| 1836 | fns = TREE_OPERAND (template_id, 0); |
| 1837 | explicit_targs = TREE_OPERAND (template_id, 1); |
| 1838 | |
| 1839 | if (fns == error_mark_node) |
| 1840 | return error_mark_node; |
| 1841 | |
| 1842 | /* Check for baselinks. */ |
| 1843 | if (BASELINK_P (fns)) |
| 1844 | fns = BASELINK_FUNCTIONS (fns); |
| 1845 | |
| 1846 | if (!is_overloaded_fn (fns)) |
| 1847 | { |
| 1848 | error ("%qD is not a function template", fns); |
| 1849 | return error_mark_node; |
| 1850 | } |
| 1851 | |
| 1852 | /* Count the number of template headers specified for this |
| 1853 | specialization. */ |
| 1854 | header_count = 0; |
| 1855 | for (b = current_binding_level; |
| 1856 | b->kind == sk_template_parms; |
| 1857 | b = b->level_chain) |
| 1858 | ++header_count; |
| 1859 | |
| 1860 | for (; fns; fns = OVL_NEXT (fns)) |
| 1861 | { |
| 1862 | tree fn = OVL_CURRENT (fns); |
| 1863 | |
| 1864 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
| 1865 | { |
| 1866 | tree decl_arg_types; |
| 1867 | tree fn_arg_types; |
| 1868 | tree insttype; |
| 1869 | |
| 1870 | /* In case of explicit specialization, we need to check if |
| 1871 | the number of template headers appearing in the specialization |
| 1872 | is correct. This is usually done in check_explicit_specialization, |
| 1873 | but the check done there cannot be exhaustive when specializing |
| 1874 | member functions. Consider the following code: |
| 1875 | |
| 1876 | template <> void A<int>::f(int); |
| 1877 | template <> template <> void A<int>::f(int); |
| 1878 | |
| 1879 | Assuming that A<int> is not itself an explicit specialization |
| 1880 | already, the first line specializes "f" which is a non-template |
| 1881 | member function, whilst the second line specializes "f" which |
| 1882 | is a template member function. So both lines are syntactically |
| 1883 | correct, and check_explicit_specialization does not reject |
| 1884 | them. |
| 1885 | |
| 1886 | Here, we can do better, as we are matching the specialization |
| 1887 | against the declarations. We count the number of template |
| 1888 | headers, and we check if they match TEMPLATE_COUNT + 1 |
| 1889 | (TEMPLATE_COUNT is the number of qualifying template classes, |
| 1890 | plus there must be another header for the member template |
| 1891 | itself). |
| 1892 | |
| 1893 | Notice that if header_count is zero, this is not a |
| 1894 | specialization but rather a template instantiation, so there |
| 1895 | is no check we can perform here. */ |
| 1896 | if (header_count && header_count != template_count + 1) |
| 1897 | continue; |
| 1898 | |
| 1899 | /* Check that the number of template arguments at the |
| 1900 | innermost level for DECL is the same as for FN. */ |
| 1901 | if (current_binding_level->kind == sk_template_parms |
| 1902 | && !current_binding_level->explicit_spec_p |
| 1903 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) |
| 1904 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
| 1905 | (current_template_parms)))) |
| 1906 | continue; |
| 1907 | |
| 1908 | /* DECL might be a specialization of FN. */ |
| 1909 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
| 1910 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 1911 | |
| 1912 | /* For a non-static member function, we need to make sure |
| 1913 | that the const qualification is the same. Since |
| 1914 | get_bindings does not try to merge the "this" parameter, |
| 1915 | we must do the comparison explicitly. */ |
| 1916 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) |
| 1917 | && !same_type_p (TREE_VALUE (fn_arg_types), |
| 1918 | TREE_VALUE (decl_arg_types))) |
| 1919 | continue; |
| 1920 | |
| 1921 | /* Skip the "this" parameter and, for constructors of |
| 1922 | classes with virtual bases, the VTT parameter. A |
| 1923 | full specialization of a constructor will have a VTT |
| 1924 | parameter, but a template never will. */ |
| 1925 | decl_arg_types |
| 1926 | = skip_artificial_parms_for (decl, decl_arg_types); |
| 1927 | fn_arg_types |
| 1928 | = skip_artificial_parms_for (fn, fn_arg_types); |
| 1929 | |
| 1930 | /* Check that the number of function parameters matches. |
| 1931 | For example, |
| 1932 | template <class T> void f(int i = 0); |
| 1933 | template <> void f<int>(); |
| 1934 | The specialization f<int> is invalid but is not caught |
| 1935 | by get_bindings below. */ |
| 1936 | if (cxx_dialect < cxx11 |
| 1937 | && list_length (fn_arg_types) != list_length (decl_arg_types)) |
| 1938 | continue; |
| 1939 | |
| 1940 | /* Function templates cannot be specializations; there are |
| 1941 | no partial specializations of functions. Therefore, if |
| 1942 | the type of DECL does not match FN, there is no |
| 1943 | match. */ |
| 1944 | if (tsk == tsk_template) |
| 1945 | { |
| 1946 | if (compparms (fn_arg_types, decl_arg_types)) |
| 1947 | candidates = tree_cons (NULL_TREE, fn, candidates); |
| 1948 | continue; |
| 1949 | } |
| 1950 | |
| 1951 | /* See whether this function might be a specialization of this |
| 1952 | template. */ |
| 1953 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
| 1954 | |
| 1955 | if (!targs) |
| 1956 | /* We cannot deduce template arguments that when used to |
| 1957 | specialize TMPL will produce DECL. */ |
| 1958 | continue; |
| 1959 | |
| 1960 | if (cxx_dialect >= cxx11) |
| 1961 | { |
| 1962 | /* Make sure that the deduced arguments actually work. */ |
| 1963 | insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE); |
| 1964 | if (insttype == error_mark_node) |
| 1965 | continue; |
| 1966 | fn_arg_types |
| 1967 | = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype)); |
| 1968 | if (!compparms (fn_arg_types, decl_arg_types)) |
| 1969 | continue; |
| 1970 | } |
| 1971 | |
| 1972 | /* Save this template, and the arguments deduced. */ |
| 1973 | templates = tree_cons (targs, fn, templates); |
| 1974 | } |
| 1975 | else if (need_member_template) |
| 1976 | /* FN is an ordinary member function, and we need a |
| 1977 | specialization of a member template. */ |
| 1978 | ; |
| 1979 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
| 1980 | /* We can get IDENTIFIER_NODEs here in certain erroneous |
| 1981 | cases. */ |
| 1982 | ; |
| 1983 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
| 1984 | /* This is just an ordinary non-member function. Nothing can |
| 1985 | be a specialization of that. */ |
| 1986 | ; |
| 1987 | else if (DECL_ARTIFICIAL (fn)) |
| 1988 | /* Cannot specialize functions that are created implicitly. */ |
| 1989 | ; |
| 1990 | else |
| 1991 | { |
| 1992 | tree decl_arg_types; |
| 1993 | |
| 1994 | /* This is an ordinary member function. However, since |
| 1995 | we're here, we can assume it's enclosing class is a |
| 1996 | template class. For example, |
| 1997 | |
| 1998 | template <typename T> struct S { void f(); }; |
| 1999 | template <> void S<int>::f() {} |
| 2000 | |
| 2001 | Here, S<int>::f is a non-template, but S<int> is a |
| 2002 | template class. If FN has the same type as DECL, we |
| 2003 | might be in business. */ |
| 2004 | |
| 2005 | if (!DECL_TEMPLATE_INFO (fn)) |
| 2006 | /* Its enclosing class is an explicit specialization |
| 2007 | of a template class. This is not a candidate. */ |
| 2008 | continue; |
| 2009 | |
| 2010 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
| 2011 | TREE_TYPE (TREE_TYPE (fn)))) |
| 2012 | /* The return types differ. */ |
| 2013 | continue; |
| 2014 | |
| 2015 | /* Adjust the type of DECL in case FN is a static member. */ |
| 2016 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
| 2017 | if (DECL_STATIC_FUNCTION_P (fn) |
| 2018 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
| 2019 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
| 2020 | |
| 2021 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
| 2022 | decl_arg_types)) |
| 2023 | /* They match! */ |
| 2024 | candidates = tree_cons (NULL_TREE, fn, candidates); |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | if (templates && TREE_CHAIN (templates)) |
| 2029 | { |
| 2030 | /* We have: |
| 2031 | |
| 2032 | [temp.expl.spec] |
| 2033 | |
| 2034 | It is possible for a specialization with a given function |
| 2035 | signature to be instantiated from more than one function |
| 2036 | template. In such cases, explicit specification of the |
| 2037 | template arguments must be used to uniquely identify the |
| 2038 | function template specialization being specialized. |
| 2039 | |
| 2040 | Note that here, there's no suggestion that we're supposed to |
| 2041 | determine which of the candidate templates is most |
| 2042 | specialized. However, we, also have: |
| 2043 | |
| 2044 | [temp.func.order] |
| 2045 | |
| 2046 | Partial ordering of overloaded function template |
| 2047 | declarations is used in the following contexts to select |
| 2048 | the function template to which a function template |
| 2049 | specialization refers: |
| 2050 | |
| 2051 | -- when an explicit specialization refers to a function |
| 2052 | template. |
| 2053 | |
| 2054 | So, we do use the partial ordering rules, at least for now. |
| 2055 | This extension can only serve to make invalid programs valid, |
| 2056 | so it's safe. And, there is strong anecdotal evidence that |
| 2057 | the committee intended the partial ordering rules to apply; |
| 2058 | the EDG front end has that behavior, and John Spicer claims |
| 2059 | that the committee simply forgot to delete the wording in |
| 2060 | [temp.expl.spec]. */ |
| 2061 | tree tmpl = most_specialized_instantiation (templates); |
| 2062 | if (tmpl != error_mark_node) |
| 2063 | { |
| 2064 | templates = tmpl; |
| 2065 | TREE_CHAIN (templates) = NULL_TREE; |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | if (templates == NULL_TREE && candidates == NULL_TREE) |
| 2070 | { |
| 2071 | error ("template-id %qD for %q+D does not match any template " |
| 2072 | "declaration", template_id, decl); |
| 2073 | if (header_count && header_count != template_count + 1) |
| 2074 | inform (input_location, "saw %d %<template<>%>, need %d for " |
| 2075 | "specializing a member function template", |
| 2076 | header_count, template_count + 1); |
| 2077 | return error_mark_node; |
| 2078 | } |
| 2079 | else if ((templates && TREE_CHAIN (templates)) |
| 2080 | || (candidates && TREE_CHAIN (candidates)) |
| 2081 | || (templates && candidates)) |
| 2082 | { |
| 2083 | error ("ambiguous template specialization %qD for %q+D", |
| 2084 | template_id, decl); |
| 2085 | candidates = chainon (candidates, templates); |
| 2086 | print_candidates (candidates); |
| 2087 | return error_mark_node; |
| 2088 | } |
| 2089 | |
| 2090 | /* We have one, and exactly one, match. */ |
| 2091 | if (candidates) |
| 2092 | { |
| 2093 | tree fn = TREE_VALUE (candidates); |
| 2094 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
| 2095 | /* DECL is a re-declaration or partial instantiation of a template |
| 2096 | function. */ |
| 2097 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
| 2098 | return fn; |
| 2099 | /* It was a specialization of an ordinary member function in a |
| 2100 | template class. */ |
| 2101 | return DECL_TI_TEMPLATE (fn); |
| 2102 | } |
| 2103 | |
| 2104 | /* It was a specialization of a template. */ |
| 2105 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
| 2106 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
| 2107 | { |
| 2108 | *targs_out = copy_node (targs); |
| 2109 | SET_TMPL_ARGS_LEVEL (*targs_out, |
| 2110 | TMPL_ARGS_DEPTH (*targs_out), |
| 2111 | TREE_PURPOSE (templates)); |
| 2112 | } |
| 2113 | else |
| 2114 | *targs_out = TREE_PURPOSE (templates); |
| 2115 | return TREE_VALUE (templates); |
| 2116 | } |
| 2117 | |
| 2118 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, |
| 2119 | but with the default argument values filled in from those in the |
| 2120 | TMPL_TYPES. */ |
| 2121 | |
| 2122 | static tree |
| 2123 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
| 2124 | tree tmpl_types) |
| 2125 | { |
| 2126 | tree new_spec_types; |
| 2127 | |
| 2128 | if (!spec_types) |
| 2129 | return NULL_TREE; |
| 2130 | |
| 2131 | if (spec_types == void_list_node) |
| 2132 | return void_list_node; |
| 2133 | |
| 2134 | /* Substitute into the rest of the list. */ |
| 2135 | new_spec_types = |
| 2136 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), |
| 2137 | TREE_CHAIN (tmpl_types)); |
| 2138 | |
| 2139 | /* Add the default argument for this parameter. */ |
| 2140 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), |
| 2141 | TREE_VALUE (spec_types), |
| 2142 | new_spec_types); |
| 2143 | } |
| 2144 | |
| 2145 | /* DECL is an explicit specialization. Replicate default arguments |
| 2146 | from the template it specializes. (That way, code like: |
| 2147 | |
| 2148 | template <class T> void f(T = 3); |
| 2149 | template <> void f(double); |
| 2150 | void g () { f (); } |
| 2151 | |
| 2152 | works, as required.) An alternative approach would be to look up |
| 2153 | the correct default arguments at the call-site, but this approach |
| 2154 | is consistent with how implicit instantiations are handled. */ |
| 2155 | |
| 2156 | static void |
| 2157 | copy_default_args_to_explicit_spec (tree decl) |
| 2158 | { |
| 2159 | tree tmpl; |
| 2160 | tree spec_types; |
| 2161 | tree tmpl_types; |
| 2162 | tree new_spec_types; |
| 2163 | tree old_type; |
| 2164 | tree new_type; |
| 2165 | tree t; |
| 2166 | tree object_type = NULL_TREE; |
| 2167 | tree in_charge = NULL_TREE; |
| 2168 | tree vtt = NULL_TREE; |
| 2169 | |
| 2170 | /* See if there's anything we need to do. */ |
| 2171 | tmpl = DECL_TI_TEMPLATE (decl); |
| 2172 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); |
| 2173 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) |
| 2174 | if (TREE_PURPOSE (t)) |
| 2175 | break; |
| 2176 | if (!t) |
| 2177 | return; |
| 2178 | |
| 2179 | old_type = TREE_TYPE (decl); |
| 2180 | spec_types = TYPE_ARG_TYPES (old_type); |
| 2181 | |
| 2182 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
| 2183 | { |
| 2184 | /* Remove the this pointer, but remember the object's type for |
| 2185 | CV quals. */ |
| 2186 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
| 2187 | spec_types = TREE_CHAIN (spec_types); |
| 2188 | tmpl_types = TREE_CHAIN (tmpl_types); |
| 2189 | |
| 2190 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
| 2191 | { |
| 2192 | /* DECL may contain more parameters than TMPL due to the extra |
| 2193 | in-charge parameter in constructors and destructors. */ |
| 2194 | in_charge = spec_types; |
| 2195 | spec_types = TREE_CHAIN (spec_types); |
| 2196 | } |
| 2197 | if (DECL_HAS_VTT_PARM_P (decl)) |
| 2198 | { |
| 2199 | vtt = spec_types; |
| 2200 | spec_types = TREE_CHAIN (spec_types); |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | /* Compute the merged default arguments. */ |
| 2205 | new_spec_types = |
| 2206 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
| 2207 | |
| 2208 | /* Compute the new FUNCTION_TYPE. */ |
| 2209 | if (object_type) |
| 2210 | { |
| 2211 | if (vtt) |
| 2212 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
| 2213 | TREE_VALUE (vtt), |
| 2214 | new_spec_types); |
| 2215 | |
| 2216 | if (in_charge) |
| 2217 | /* Put the in-charge parameter back. */ |
| 2218 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), |
| 2219 | TREE_VALUE (in_charge), |
| 2220 | new_spec_types); |
| 2221 | |
| 2222 | new_type = build_method_type_directly (object_type, |
| 2223 | TREE_TYPE (old_type), |
| 2224 | new_spec_types); |
| 2225 | } |
| 2226 | else |
| 2227 | new_type = build_function_type (TREE_TYPE (old_type), |
| 2228 | new_spec_types); |
| 2229 | new_type = cp_build_type_attribute_variant (new_type, |
| 2230 | TYPE_ATTRIBUTES (old_type)); |
| 2231 | new_type = build_exception_variant (new_type, |
| 2232 | TYPE_RAISES_EXCEPTIONS (old_type)); |
| 2233 | TREE_TYPE (decl) = new_type; |
| 2234 | } |
| 2235 | |
| 2236 | /* Check to see if the function just declared, as indicated in |
| 2237 | DECLARATOR, and in DECL, is a specialization of a function |
| 2238 | template. We may also discover that the declaration is an explicit |
| 2239 | instantiation at this point. |
| 2240 | |
| 2241 | Returns DECL, or an equivalent declaration that should be used |
| 2242 | instead if all goes well. Issues an error message if something is |
| 2243 | amiss. Returns error_mark_node if the error is not easily |
| 2244 | recoverable. |
| 2245 | |
| 2246 | FLAGS is a bitmask consisting of the following flags: |
| 2247 | |
| 2248 | 2: The function has a definition. |
| 2249 | 4: The function is a friend. |
| 2250 | |
| 2251 | The TEMPLATE_COUNT is the number of references to qualifying |
| 2252 | template classes that appeared in the name of the function. For |
| 2253 | example, in |
| 2254 | |
| 2255 | template <class T> struct S { void f(); }; |
| 2256 | void S<int>::f(); |
| 2257 | |
| 2258 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
| 2259 | classes are not counted in the TEMPLATE_COUNT, so that in |
| 2260 | |
| 2261 | template <class T> struct S {}; |
| 2262 | template <> struct S<int> { void f(); } |
| 2263 | template <> void S<int>::f(); |
| 2264 | |
| 2265 | the TEMPLATE_COUNT would be 0. (Note that this declaration is |
| 2266 | invalid; there should be no template <>.) |
| 2267 | |
| 2268 | If the function is a specialization, it is marked as such via |
| 2269 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO |
| 2270 | is set up correctly, and it is added to the list of specializations |
| 2271 | for that template. */ |
| 2272 | |
| 2273 | tree |
| 2274 | check_explicit_specialization (tree declarator, |
| 2275 | tree decl, |
| 2276 | int template_count, |
| 2277 | int flags) |
| 2278 | { |
| 2279 | int have_def = flags & 2; |
| 2280 | int is_friend = flags & 4; |
| 2281 | int specialization = 0; |
| 2282 | int explicit_instantiation = 0; |
| 2283 | int member_specialization = 0; |
| 2284 | tree ctype = DECL_CLASS_CONTEXT (decl); |
| 2285 | tree dname = DECL_NAME (decl); |
| 2286 | tmpl_spec_kind tsk; |
| 2287 | |
| 2288 | if (is_friend) |
| 2289 | { |
| 2290 | if (!processing_specialization) |
| 2291 | tsk = tsk_none; |
| 2292 | else |
| 2293 | tsk = tsk_excessive_parms; |
| 2294 | } |
| 2295 | else |
| 2296 | tsk = current_tmpl_spec_kind (template_count); |
| 2297 | |
| 2298 | switch (tsk) |
| 2299 | { |
| 2300 | case tsk_none: |
| 2301 | if (processing_specialization) |
| 2302 | { |
| 2303 | specialization = 1; |
| 2304 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
| 2305 | } |
| 2306 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
| 2307 | { |
| 2308 | if (is_friend) |
| 2309 | /* This could be something like: |
| 2310 | |
| 2311 | template <class T> void f(T); |
| 2312 | class S { friend void f<>(int); } */ |
| 2313 | specialization = 1; |
| 2314 | else |
| 2315 | { |
| 2316 | /* This case handles bogus declarations like template <> |
| 2317 | template <class T> void f<int>(); */ |
| 2318 | |
| 2319 | error ("template-id %qD in declaration of primary template", |
| 2320 | declarator); |
| 2321 | return decl; |
| 2322 | } |
| 2323 | } |
| 2324 | break; |
| 2325 | |
| 2326 | case tsk_invalid_member_spec: |
| 2327 | /* The error has already been reported in |
| 2328 | check_specialization_scope. */ |
| 2329 | return error_mark_node; |
| 2330 | |
| 2331 | case tsk_invalid_expl_inst: |
| 2332 | error ("template parameter list used in explicit instantiation"); |
| 2333 | |
| 2334 | /* Fall through. */ |
| 2335 | |
| 2336 | case tsk_expl_inst: |
| 2337 | if (have_def) |
| 2338 | error ("definition provided for explicit instantiation"); |
| 2339 | |
| 2340 | explicit_instantiation = 1; |
| 2341 | break; |
| 2342 | |
| 2343 | case tsk_excessive_parms: |
| 2344 | case tsk_insufficient_parms: |
| 2345 | if (tsk == tsk_excessive_parms) |
| 2346 | error ("too many template parameter lists in declaration of %qD", |
| 2347 | decl); |
| 2348 | else if (template_header_count) |
| 2349 | error("too few template parameter lists in declaration of %qD", decl); |
| 2350 | else |
| 2351 | error("explicit specialization of %qD must be introduced by " |
| 2352 | "%<template <>%>", decl); |
| 2353 | |
| 2354 | /* Fall through. */ |
| 2355 | case tsk_expl_spec: |
| 2356 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
| 2357 | if (ctype) |
| 2358 | member_specialization = 1; |
| 2359 | else |
| 2360 | specialization = 1; |
| 2361 | break; |
| 2362 | |
| 2363 | case tsk_template: |
| 2364 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
| 2365 | { |
| 2366 | /* This case handles bogus declarations like template <> |
| 2367 | template <class T> void f<int>(); */ |
| 2368 | |
| 2369 | if (uses_template_parms (declarator)) |
| 2370 | error ("function template partial specialization %qD " |
| 2371 | "is not allowed", declarator); |
| 2372 | else |
| 2373 | error ("template-id %qD in declaration of primary template", |
| 2374 | declarator); |
| 2375 | return decl; |
| 2376 | } |
| 2377 | |
| 2378 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) |
| 2379 | /* This is a specialization of a member template, without |
| 2380 | specialization the containing class. Something like: |
| 2381 | |
| 2382 | template <class T> struct S { |
| 2383 | template <class U> void f (U); |
| 2384 | }; |
| 2385 | template <> template <class U> void S<int>::f(U) {} |
| 2386 | |
| 2387 | That's a specialization -- but of the entire template. */ |
| 2388 | specialization = 1; |
| 2389 | break; |
| 2390 | |
| 2391 | default: |
| 2392 | gcc_unreachable (); |
| 2393 | } |
| 2394 | |
| 2395 | if (specialization || member_specialization) |
| 2396 | { |
| 2397 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
| 2398 | for (; t; t = TREE_CHAIN (t)) |
| 2399 | if (TREE_PURPOSE (t)) |
| 2400 | { |
| 2401 | permerror (input_location, |
| 2402 | "default argument specified in explicit specialization"); |
| 2403 | break; |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | if (specialization || member_specialization || explicit_instantiation) |
| 2408 | { |
| 2409 | tree tmpl = NULL_TREE; |
| 2410 | tree targs = NULL_TREE; |
| 2411 | |
| 2412 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ |
| 2413 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
| 2414 | { |
| 2415 | tree fns; |
| 2416 | |
| 2417 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
| 2418 | if (ctype) |
| 2419 | fns = dname; |
| 2420 | else |
| 2421 | { |
| 2422 | /* If there is no class context, the explicit instantiation |
| 2423 | must be at namespace scope. */ |
| 2424 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
| 2425 | |
| 2426 | /* Find the namespace binding, using the declaration |
| 2427 | context. */ |
| 2428 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
| 2429 | false, true); |
| 2430 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
| 2431 | { |
| 2432 | error ("%qD is not a template function", dname); |
| 2433 | fns = error_mark_node; |
| 2434 | } |
| 2435 | else |
| 2436 | { |
| 2437 | tree fn = OVL_CURRENT (fns); |
| 2438 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
| 2439 | CP_DECL_CONTEXT (fn))) |
| 2440 | error ("%qD is not declared in %qD", |
| 2441 | decl, current_namespace); |
| 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | declarator = lookup_template_function (fns, NULL_TREE); |
| 2446 | } |
| 2447 | |
| 2448 | if (declarator == error_mark_node) |
| 2449 | return error_mark_node; |
| 2450 | |
| 2451 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
| 2452 | { |
| 2453 | if (!explicit_instantiation) |
| 2454 | /* A specialization in class scope. This is invalid, |
| 2455 | but the error will already have been flagged by |
| 2456 | check_specialization_scope. */ |
| 2457 | return error_mark_node; |
| 2458 | else |
| 2459 | { |
| 2460 | /* It's not valid to write an explicit instantiation in |
| 2461 | class scope, e.g.: |
| 2462 | |
| 2463 | class C { template void f(); } |
| 2464 | |
| 2465 | This case is caught by the parser. However, on |
| 2466 | something like: |
| 2467 | |
| 2468 | template class C { void f(); }; |
| 2469 | |
| 2470 | (which is invalid) we can get here. The error will be |
| 2471 | issued later. */ |
| 2472 | ; |
| 2473 | } |
| 2474 | |
| 2475 | return decl; |
| 2476 | } |
| 2477 | else if (ctype != NULL_TREE |
| 2478 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
| 2479 | IDENTIFIER_NODE)) |
| 2480 | { |
| 2481 | /* Find the list of functions in ctype that have the same |
| 2482 | name as the declared function. */ |
| 2483 | tree name = TREE_OPERAND (declarator, 0); |
| 2484 | tree fns = NULL_TREE; |
| 2485 | int idx; |
| 2486 | |
| 2487 | if (constructor_name_p (name, ctype)) |
| 2488 | { |
| 2489 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
| 2490 | |
| 2491 | if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
| 2492 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
| 2493 | { |
| 2494 | /* From [temp.expl.spec]: |
| 2495 | |
| 2496 | If such an explicit specialization for the member |
| 2497 | of a class template names an implicitly-declared |
| 2498 | special member function (clause _special_), the |
| 2499 | program is ill-formed. |
| 2500 | |
| 2501 | Similar language is found in [temp.explicit]. */ |
| 2502 | error ("specialization of implicitly-declared special member function"); |
| 2503 | return error_mark_node; |
| 2504 | } |
| 2505 | |
| 2506 | name = is_constructor ? ctor_identifier : dtor_identifier; |
| 2507 | } |
| 2508 | |
| 2509 | if (!DECL_CONV_FN_P (decl)) |
| 2510 | { |
| 2511 | idx = lookup_fnfields_1 (ctype, name); |
| 2512 | if (idx >= 0) |
| 2513 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
| 2514 | } |
| 2515 | else |
| 2516 | { |
| 2517 | VEC(tree,gc) *methods; |
| 2518 | tree ovl; |
| 2519 | |
| 2520 | /* For a type-conversion operator, we cannot do a |
| 2521 | name-based lookup. We might be looking for `operator |
| 2522 | int' which will be a specialization of `operator T'. |
| 2523 | So, we find *all* the conversion operators, and then |
| 2524 | select from them. */ |
| 2525 | fns = NULL_TREE; |
| 2526 | |
| 2527 | methods = CLASSTYPE_METHOD_VEC (ctype); |
| 2528 | if (methods) |
| 2529 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
| 2530 | VEC_iterate (tree, methods, idx, ovl); |
| 2531 | ++idx) |
| 2532 | { |
| 2533 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
| 2534 | /* There are no more conversion functions. */ |
| 2535 | break; |
| 2536 | |
| 2537 | /* Glue all these conversion functions together |
| 2538 | with those we already have. */ |
| 2539 | for (; ovl; ovl = OVL_NEXT (ovl)) |
| 2540 | fns = ovl_cons (OVL_CURRENT (ovl), fns); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | if (fns == NULL_TREE) |
| 2545 | { |
| 2546 | error ("no member function %qD declared in %qT", name, ctype); |
| 2547 | return error_mark_node; |
| 2548 | } |
| 2549 | else |
| 2550 | TREE_OPERAND (declarator, 0) = fns; |
| 2551 | } |
| 2552 | |
| 2553 | /* Figure out what exactly is being specialized at this point. |
| 2554 | Note that for an explicit instantiation, even one for a |
| 2555 | member function, we cannot tell apriori whether the |
| 2556 | instantiation is for a member template, or just a member |
| 2557 | function of a template class. Even if a member template is |
| 2558 | being instantiated, the member template arguments may be |
| 2559 | elided if they can be deduced from the rest of the |
| 2560 | declaration. */ |
| 2561 | tmpl = determine_specialization (declarator, decl, |
| 2562 | &targs, |
| 2563 | member_specialization, |
| 2564 | template_count, |
| 2565 | tsk); |
| 2566 | |
| 2567 | if (!tmpl || tmpl == error_mark_node) |
| 2568 | /* We couldn't figure out what this declaration was |
| 2569 | specializing. */ |
| 2570 | return error_mark_node; |
| 2571 | else |
| 2572 | { |
| 2573 | tree gen_tmpl = most_general_template (tmpl); |
| 2574 | |
| 2575 | if (explicit_instantiation) |
| 2576 | { |
| 2577 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
| 2578 | is done by do_decl_instantiation later. */ |
| 2579 | |
| 2580 | int arg_depth = TMPL_ARGS_DEPTH (targs); |
| 2581 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
| 2582 | |
| 2583 | if (arg_depth > parm_depth) |
| 2584 | { |
| 2585 | /* If TMPL is not the most general template (for |
| 2586 | example, if TMPL is a friend template that is |
| 2587 | injected into namespace scope), then there will |
| 2588 | be too many levels of TARGS. Remove some of them |
| 2589 | here. */ |
| 2590 | int i; |
| 2591 | tree new_targs; |
| 2592 | |
| 2593 | new_targs = make_tree_vec (parm_depth); |
| 2594 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
| 2595 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) |
| 2596 | = TREE_VEC_ELT (targs, i); |
| 2597 | targs = new_targs; |
| 2598 | } |
| 2599 | |
| 2600 | return instantiate_template (tmpl, targs, tf_error); |
| 2601 | } |
| 2602 | |
| 2603 | /* If we thought that the DECL was a member function, but it |
| 2604 | turns out to be specializing a static member function, |
| 2605 | make DECL a static member function as well. */ |
| 2606 | if (DECL_STATIC_FUNCTION_P (tmpl) |
| 2607 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
| 2608 | revert_static_member_fn (decl); |
| 2609 | |
| 2610 | /* If this is a specialization of a member template of a |
| 2611 | template class, we want to return the TEMPLATE_DECL, not |
| 2612 | the specialization of it. */ |
| 2613 | if (tsk == tsk_template) |
| 2614 | { |
| 2615 | tree result = DECL_TEMPLATE_RESULT (tmpl); |
| 2616 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
| 2617 | DECL_INITIAL (result) = NULL_TREE; |
| 2618 | if (have_def) |
| 2619 | { |
| 2620 | tree parm; |
| 2621 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
| 2622 | DECL_SOURCE_LOCATION (result) |
| 2623 | = DECL_SOURCE_LOCATION (decl); |
| 2624 | /* We want to use the argument list specified in the |
| 2625 | definition, not in the original declaration. */ |
| 2626 | DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); |
| 2627 | for (parm = DECL_ARGUMENTS (result); parm; |
| 2628 | parm = DECL_CHAIN (parm)) |
| 2629 | DECL_CONTEXT (parm) = result; |
| 2630 | } |
| 2631 | return register_specialization (tmpl, gen_tmpl, targs, |
| 2632 | is_friend, 0); |
| 2633 | } |
| 2634 | |
| 2635 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
| 2636 | DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs); |
| 2637 | |
| 2638 | /* Inherit default function arguments from the template |
| 2639 | DECL is specializing. */ |
| 2640 | copy_default_args_to_explicit_spec (decl); |
| 2641 | |
| 2642 | /* This specialization has the same protection as the |
| 2643 | template it specializes. */ |
| 2644 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); |
| 2645 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); |
| 2646 | |
| 2647 | /* 7.1.1-1 [dcl.stc] |
| 2648 | |
| 2649 | A storage-class-specifier shall not be specified in an |
| 2650 | explicit specialization... |
| 2651 | |
| 2652 | The parser rejects these, so unless action is taken here, |
| 2653 | explicit function specializations will always appear with |
| 2654 | global linkage. |
| 2655 | |
| 2656 | The action recommended by the C++ CWG in response to C++ |
| 2657 | defect report 605 is to make the storage class and linkage |
| 2658 | of the explicit specialization match the templated function: |
| 2659 | |
| 2660 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 |
| 2661 | */ |
| 2662 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) |
| 2663 | { |
| 2664 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); |
| 2665 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); |
| 2666 | |
| 2667 | /* This specialization has the same linkage and visibility as |
| 2668 | the function template it specializes. */ |
| 2669 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); |
| 2670 | if (! TREE_PUBLIC (decl)) |
| 2671 | { |
| 2672 | DECL_INTERFACE_KNOWN (decl) = 1; |
| 2673 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
| 2674 | } |
| 2675 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
| 2676 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) |
| 2677 | { |
| 2678 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
| 2679 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | /* If DECL is a friend declaration, declared using an |
| 2684 | unqualified name, the namespace associated with DECL may |
| 2685 | have been set incorrectly. For example, in: |
| 2686 | |
| 2687 | template <typename T> void f(T); |
| 2688 | namespace N { |
| 2689 | struct S { friend void f<int>(int); } |
| 2690 | } |
| 2691 | |
| 2692 | we will have set the DECL_CONTEXT for the friend |
| 2693 | declaration to N, rather than to the global namespace. */ |
| 2694 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
| 2695 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); |
| 2696 | |
| 2697 | if (is_friend && !have_def) |
| 2698 | /* This is not really a declaration of a specialization. |
| 2699 | It's just the name of an instantiation. But, it's not |
| 2700 | a request for an instantiation, either. */ |
| 2701 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
| 2702 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
| 2703 | /* This is indeed a specialization. In case of constructors |
| 2704 | and destructors, we need in-charge and not-in-charge |
| 2705 | versions in V3 ABI. */ |
| 2706 | clone_function_decl (decl, /*update_method_vec_p=*/0); |
| 2707 | |
| 2708 | /* Register this specialization so that we can find it |
| 2709 | again. */ |
| 2710 | decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0); |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | return decl; |
| 2715 | } |
| 2716 | |
| 2717 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
| 2718 | parameters. These are represented in the same format used for |
| 2719 | DECL_TEMPLATE_PARMS. */ |
| 2720 | |
| 2721 | int |
| 2722 | comp_template_parms (const_tree parms1, const_tree parms2) |
| 2723 | { |
| 2724 | const_tree p1; |
| 2725 | const_tree p2; |
| 2726 | |
| 2727 | if (parms1 == parms2) |
| 2728 | return 1; |
| 2729 | |
| 2730 | for (p1 = parms1, p2 = parms2; |
| 2731 | p1 != NULL_TREE && p2 != NULL_TREE; |
| 2732 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) |
| 2733 | { |
| 2734 | tree t1 = TREE_VALUE (p1); |
| 2735 | tree t2 = TREE_VALUE (p2); |
| 2736 | int i; |
| 2737 | |
| 2738 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
| 2739 | gcc_assert (TREE_CODE (t2) == TREE_VEC); |
| 2740 | |
| 2741 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) |
| 2742 | return 0; |
| 2743 | |
| 2744 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
| 2745 | { |
| 2746 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
| 2747 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); |
| 2748 | |
| 2749 | /* If either of the template parameters are invalid, assume |
| 2750 | they match for the sake of error recovery. */ |
| 2751 | if (parm1 == error_mark_node || parm2 == error_mark_node) |
| 2752 | return 1; |
| 2753 | |
| 2754 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) |
| 2755 | return 0; |
| 2756 | |
| 2757 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM |
| 2758 | && (TEMPLATE_TYPE_PARAMETER_PACK (parm1) |
| 2759 | == TEMPLATE_TYPE_PARAMETER_PACK (parm2))) |
| 2760 | continue; |
| 2761 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
| 2762 | return 0; |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) |
| 2767 | /* One set of parameters has more parameters lists than the |
| 2768 | other. */ |
| 2769 | return 0; |
| 2770 | |
| 2771 | return 1; |
| 2772 | } |
| 2773 | |
| 2774 | /* Determine whether PARM is a parameter pack. */ |
| 2775 | |
| 2776 | bool |
| 2777 | template_parameter_pack_p (const_tree parm) |
| 2778 | { |
| 2779 | /* Determine if we have a non-type template parameter pack. */ |
| 2780 | if (TREE_CODE (parm) == PARM_DECL) |
| 2781 | return (DECL_TEMPLATE_PARM_P (parm) |
| 2782 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); |
| 2783 | if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX) |
| 2784 | return TEMPLATE_PARM_PARAMETER_PACK (parm); |
| 2785 | |
| 2786 | /* If this is a list of template parameters, we could get a |
| 2787 | TYPE_DECL or a TEMPLATE_DECL. */ |
| 2788 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) |
| 2789 | parm = TREE_TYPE (parm); |
| 2790 | |
| 2791 | /* Otherwise it must be a type template parameter. */ |
| 2792 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
| 2793 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
| 2794 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); |
| 2795 | } |
| 2796 | |
| 2797 | /* Determine if T is a function parameter pack. */ |
| 2798 | |
| 2799 | bool |
| 2800 | function_parameter_pack_p (const_tree t) |
| 2801 | { |
| 2802 | if (t && TREE_CODE (t) == PARM_DECL) |
| 2803 | return FUNCTION_PARAMETER_PACK_P (t); |
| 2804 | return false; |
| 2805 | } |
| 2806 | |
| 2807 | /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST. |
| 2808 | PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */ |
| 2809 | |
| 2810 | tree |
| 2811 | get_function_template_decl (const_tree primary_func_tmpl_inst) |
| 2812 | { |
| 2813 | if (! primary_func_tmpl_inst |
| 2814 | || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL |
| 2815 | || ! primary_template_instantiation_p (primary_func_tmpl_inst)) |
| 2816 | return NULL; |
| 2817 | |
| 2818 | return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst)); |
| 2819 | } |
| 2820 | |
| 2821 | /* Return true iff the function parameter PARAM_DECL was expanded |
| 2822 | from the function parameter pack PACK. */ |
| 2823 | |
| 2824 | bool |
| 2825 | function_parameter_expanded_from_pack_p (tree param_decl, tree pack) |
| 2826 | { |
| 2827 | if (DECL_ARTIFICIAL (param_decl) |
| 2828 | || !function_parameter_pack_p (pack)) |
| 2829 | return false; |
| 2830 | |
| 2831 | /* The parameter pack and its pack arguments have the same |
| 2832 | DECL_PARM_INDEX. */ |
| 2833 | return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl); |
| 2834 | } |
| 2835 | |
| 2836 | /* Determine whether ARGS describes a variadic template args list, |
| 2837 | i.e., one that is terminated by a template argument pack. */ |
| 2838 | |
| 2839 | static bool |
| 2840 | template_args_variadic_p (tree args) |
| 2841 | { |
| 2842 | int nargs; |
| 2843 | tree last_parm; |
| 2844 | |
| 2845 | if (args == NULL_TREE) |
| 2846 | return false; |
| 2847 | |
| 2848 | args = INNERMOST_TEMPLATE_ARGS (args); |
| 2849 | nargs = TREE_VEC_LENGTH (args); |
| 2850 | |
| 2851 | if (nargs == 0) |
| 2852 | return false; |
| 2853 | |
| 2854 | last_parm = TREE_VEC_ELT (args, nargs - 1); |
| 2855 | |
| 2856 | return ARGUMENT_PACK_P (last_parm); |
| 2857 | } |
| 2858 | |
| 2859 | /* Generate a new name for the parameter pack name NAME (an |
| 2860 | IDENTIFIER_NODE) that incorporates its */ |
| 2861 | |
| 2862 | static tree |
| 2863 | make_ith_pack_parameter_name (tree name, int i) |
| 2864 | { |
| 2865 | /* Munge the name to include the parameter index. */ |
| 2866 | #define NUMBUF_LEN 128 |
| 2867 | char numbuf[NUMBUF_LEN]; |
| 2868 | char* newname; |
| 2869 | int newname_len; |
| 2870 | |
| 2871 | if (name == NULL_TREE) |
| 2872 | return name; |
| 2873 | snprintf (numbuf, NUMBUF_LEN, "%i", i); |
| 2874 | newname_len = IDENTIFIER_LENGTH (name) |
| 2875 | + strlen (numbuf) + 2; |
| 2876 | newname = (char*)alloca (newname_len); |
| 2877 | snprintf (newname, newname_len, |
| 2878 | "%s#%i", IDENTIFIER_POINTER (name), i); |
| 2879 | return get_identifier (newname); |
| 2880 | } |
| 2881 | |
| 2882 | /* Return true if T is a primary function, class or alias template |
| 2883 | instantiation. */ |
| 2884 | |
| 2885 | bool |
| 2886 | primary_template_instantiation_p (const_tree t) |
| 2887 | { |
| 2888 | if (!t) |
| 2889 | return false; |
| 2890 | |
| 2891 | if (TREE_CODE (t) == FUNCTION_DECL) |
| 2892 | return DECL_LANG_SPECIFIC (t) |
| 2893 | && DECL_TEMPLATE_INSTANTIATION (t) |
| 2894 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)); |
| 2895 | else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t))) |
| 2896 | return CLASSTYPE_TEMPLATE_INSTANTIATION (t) |
| 2897 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)); |
| 2898 | else if (TYPE_P (t) |
| 2899 | && TYPE_TEMPLATE_INFO (t) |
| 2900 | && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t)) |
| 2901 | && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t))) |
| 2902 | return true; |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | /* Return true if PARM is a template template parameter. */ |
| 2907 | |
| 2908 | bool |
| 2909 | template_template_parameter_p (const_tree parm) |
| 2910 | { |
| 2911 | return DECL_TEMPLATE_TEMPLATE_PARM_P (parm); |
| 2912 | } |
| 2913 | |
| 2914 | /* Return the template parameters of T if T is a |
| 2915 | primary template instantiation, NULL otherwise. */ |
| 2916 | |
| 2917 | tree |
| 2918 | get_primary_template_innermost_parameters (const_tree t) |
| 2919 | { |
| 2920 | tree parms = NULL, template_info = NULL; |
| 2921 | |
| 2922 | if ((template_info = get_template_info (t)) |
| 2923 | && primary_template_instantiation_p (t)) |
| 2924 | parms = INNERMOST_TEMPLATE_PARMS |
| 2925 | (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info))); |
| 2926 | |
| 2927 | return parms; |
| 2928 | } |
| 2929 | |
| 2930 | /* Return the template parameters of the LEVELth level from the full list |
| 2931 | of template parameters PARMS. */ |
| 2932 | |
| 2933 | tree |
| 2934 | get_template_parms_at_level (tree parms, int level) |
| 2935 | { |
| 2936 | tree p; |
| 2937 | if (!parms |
| 2938 | || TREE_CODE (parms) != TREE_LIST |
| 2939 | || level > TMPL_PARMS_DEPTH (parms)) |
| 2940 | return NULL_TREE; |
| 2941 | |
| 2942 | for (p = parms; p; p = TREE_CHAIN (p)) |
| 2943 | if (TMPL_PARMS_DEPTH (p) == level) |
| 2944 | return p; |
| 2945 | |
| 2946 | return NULL_TREE; |
| 2947 | } |
| 2948 | |
| 2949 | /* Returns the template arguments of T if T is a template instantiation, |
| 2950 | NULL otherwise. */ |
| 2951 | |
| 2952 | tree |
| 2953 | get_template_innermost_arguments (const_tree t) |
| 2954 | { |
| 2955 | tree args = NULL, template_info = NULL; |
| 2956 | |
| 2957 | if ((template_info = get_template_info (t)) |
| 2958 | && TI_ARGS (template_info)) |
| 2959 | args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info)); |
| 2960 | |
| 2961 | return args; |
| 2962 | } |
| 2963 | |
| 2964 | /* Return the argument pack elements of T if T is a template argument pack, |
| 2965 | NULL otherwise. */ |
| 2966 | |
| 2967 | tree |
| 2968 | get_template_argument_pack_elems (const_tree t) |
| 2969 | { |
| 2970 | if (TREE_CODE (t) != TYPE_ARGUMENT_PACK |
| 2971 | && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK) |
| 2972 | return NULL; |
| 2973 | |
| 2974 | return ARGUMENT_PACK_ARGS (t); |
| 2975 | } |
| 2976 | |
| 2977 | /* Structure used to track the progress of find_parameter_packs_r. */ |
| 2978 | struct find_parameter_pack_data |
| 2979 | { |
| 2980 | /* TREE_LIST that will contain all of the parameter packs found by |
| 2981 | the traversal. */ |
| 2982 | tree* parameter_packs; |
| 2983 | |
| 2984 | /* Set of AST nodes that have been visited by the traversal. */ |
| 2985 | struct pointer_set_t *visited; |
| 2986 | }; |
| 2987 | |
| 2988 | /* Identifies all of the argument packs that occur in a template |
| 2989 | argument and appends them to the TREE_LIST inside DATA, which is a |
| 2990 | find_parameter_pack_data structure. This is a subroutine of |
| 2991 | make_pack_expansion and uses_parameter_packs. */ |
| 2992 | static tree |
| 2993 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) |
| 2994 | { |
| 2995 | tree t = *tp; |
| 2996 | struct find_parameter_pack_data* ppd = |
| 2997 | (struct find_parameter_pack_data*)data; |
| 2998 | bool parameter_pack_p = false; |
| 2999 | |
| 3000 | /* Handle type aliases/typedefs. */ |
| 3001 | if (TYPE_P (t) |
| 3002 | && TYPE_NAME (t) |
| 3003 | && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL |
| 3004 | && TYPE_DECL_ALIAS_P (TYPE_NAME (t))) |
| 3005 | { |
| 3006 | if (TYPE_TEMPLATE_INFO (t)) |
| 3007 | cp_walk_tree (&TYPE_TI_ARGS (t), |
| 3008 | &find_parameter_packs_r, |
| 3009 | ppd, ppd->visited); |
| 3010 | *walk_subtrees = 0; |
| 3011 | return NULL_TREE; |
| 3012 | } |
| 3013 | |
| 3014 | /* Identify whether this is a parameter pack or not. */ |
| 3015 | switch (TREE_CODE (t)) |
| 3016 | { |
| 3017 | case TEMPLATE_PARM_INDEX: |
| 3018 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) |
| 3019 | parameter_pack_p = true; |
| 3020 | break; |
| 3021 | |
| 3022 | case TEMPLATE_TYPE_PARM: |
| 3023 | t = TYPE_MAIN_VARIANT (t); |
| 3024 | case TEMPLATE_TEMPLATE_PARM: |
| 3025 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) |
| 3026 | parameter_pack_p = true; |
| 3027 | break; |
| 3028 | |
| 3029 | case PARM_DECL: |
| 3030 | if (FUNCTION_PARAMETER_PACK_P (t)) |
| 3031 | { |
| 3032 | /* We don't want to walk into the type of a PARM_DECL, |
| 3033 | because we don't want to see the type parameter pack. */ |
| 3034 | *walk_subtrees = 0; |
| 3035 | parameter_pack_p = true; |
| 3036 | } |
| 3037 | break; |
| 3038 | |
| 3039 | case BASES: |
| 3040 | parameter_pack_p = true; |
| 3041 | break; |
| 3042 | default: |
| 3043 | /* Not a parameter pack. */ |
| 3044 | break; |
| 3045 | } |
| 3046 | |
| 3047 | if (parameter_pack_p) |
| 3048 | { |
| 3049 | /* Add this parameter pack to the list. */ |
| 3050 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); |
| 3051 | } |
| 3052 | |
| 3053 | if (TYPE_P (t)) |
| 3054 | cp_walk_tree (&TYPE_CONTEXT (t), |
| 3055 | &find_parameter_packs_r, ppd, ppd->visited); |
| 3056 | |
| 3057 | /* This switch statement will return immediately if we don't find a |
| 3058 | parameter pack. */ |
| 3059 | switch (TREE_CODE (t)) |
| 3060 | { |
| 3061 | case TEMPLATE_PARM_INDEX: |
| 3062 | return NULL_TREE; |
| 3063 | |
| 3064 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 3065 | /* Check the template itself. */ |
| 3066 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), |
| 3067 | &find_parameter_packs_r, ppd, ppd->visited); |
| 3068 | /* Check the template arguments. */ |
| 3069 | cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd, |
| 3070 | ppd->visited); |
| 3071 | *walk_subtrees = 0; |
| 3072 | return NULL_TREE; |
| 3073 | |
| 3074 | case TEMPLATE_TYPE_PARM: |
| 3075 | case TEMPLATE_TEMPLATE_PARM: |
| 3076 | return NULL_TREE; |
| 3077 | |
| 3078 | case PARM_DECL: |
| 3079 | return NULL_TREE; |
| 3080 | |
| 3081 | case RECORD_TYPE: |
| 3082 | if (TYPE_PTRMEMFUNC_P (t)) |
| 3083 | return NULL_TREE; |
| 3084 | /* Fall through. */ |
| 3085 | |
| 3086 | case UNION_TYPE: |
| 3087 | case ENUMERAL_TYPE: |
| 3088 | if (TYPE_TEMPLATE_INFO (t)) |
| 3089 | cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)), |
| 3090 | &find_parameter_packs_r, ppd, ppd->visited); |
| 3091 | |
| 3092 | *walk_subtrees = 0; |
| 3093 | return NULL_TREE; |
| 3094 | |
| 3095 | case CONSTRUCTOR: |
| 3096 | case TEMPLATE_DECL: |
| 3097 | cp_walk_tree (&TREE_TYPE (t), |
| 3098 | &find_parameter_packs_r, ppd, ppd->visited); |
| 3099 | return NULL_TREE; |
| 3100 | |
| 3101 | case TYPENAME_TYPE: |
| 3102 | cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r, |
| 3103 | ppd, ppd->visited); |
| 3104 | *walk_subtrees = 0; |
| 3105 | return NULL_TREE; |
| 3106 | |
| 3107 | case TYPE_PACK_EXPANSION: |
| 3108 | case EXPR_PACK_EXPANSION: |
| 3109 | *walk_subtrees = 0; |
| 3110 | return NULL_TREE; |
| 3111 | |
| 3112 | case INTEGER_TYPE: |
| 3113 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
| 3114 | ppd, ppd->visited); |
| 3115 | *walk_subtrees = 0; |
| 3116 | return NULL_TREE; |
| 3117 | |
| 3118 | case IDENTIFIER_NODE: |
| 3119 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
| 3120 | ppd->visited); |
| 3121 | *walk_subtrees = 0; |
| 3122 | return NULL_TREE; |
| 3123 | |
| 3124 | default: |
| 3125 | return NULL_TREE; |
| 3126 | } |
| 3127 | |
| 3128 | return NULL_TREE; |
| 3129 | } |
| 3130 | |
| 3131 | /* Determines if the expression or type T uses any parameter packs. */ |
| 3132 | bool |
| 3133 | uses_parameter_packs (tree t) |
| 3134 | { |
| 3135 | tree parameter_packs = NULL_TREE; |
| 3136 | struct find_parameter_pack_data ppd; |
| 3137 | ppd.parameter_packs = ¶meter_packs; |
| 3138 | ppd.visited = pointer_set_create (); |
| 3139 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
| 3140 | pointer_set_destroy (ppd.visited); |
| 3141 | return parameter_packs != NULL_TREE; |
| 3142 | } |
| 3143 | |
| 3144 | /* Turn ARG, which may be an expression, type, or a TREE_LIST |
| 3145 | representation a base-class initializer into a parameter pack |
| 3146 | expansion. If all goes well, the resulting node will be an |
| 3147 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, |
| 3148 | respectively. */ |
| 3149 | tree |
| 3150 | make_pack_expansion (tree arg) |
| 3151 | { |
| 3152 | tree result; |
| 3153 | tree parameter_packs = NULL_TREE; |
| 3154 | bool for_types = false; |
| 3155 | struct find_parameter_pack_data ppd; |
| 3156 | |
| 3157 | if (!arg || arg == error_mark_node) |
| 3158 | return arg; |
| 3159 | |
| 3160 | if (TREE_CODE (arg) == TREE_LIST) |
| 3161 | { |
| 3162 | /* The only time we will see a TREE_LIST here is for a base |
| 3163 | class initializer. In this case, the TREE_PURPOSE will be a |
| 3164 | _TYPE node (representing the base class expansion we're |
| 3165 | initializing) and the TREE_VALUE will be a TREE_LIST |
| 3166 | containing the initialization arguments. |
| 3167 | |
| 3168 | The resulting expansion looks somewhat different from most |
| 3169 | expansions. Rather than returning just one _EXPANSION, we |
| 3170 | return a TREE_LIST whose TREE_PURPOSE is a |
| 3171 | TYPE_PACK_EXPANSION containing the bases that will be |
| 3172 | initialized. The TREE_VALUE will be identical to the |
| 3173 | original TREE_VALUE, which is a list of arguments that will |
| 3174 | be passed to each base. We do not introduce any new pack |
| 3175 | expansion nodes into the TREE_VALUE (although it is possible |
| 3176 | that some already exist), because the TREE_PURPOSE and |
| 3177 | TREE_VALUE all need to be expanded together with the same |
| 3178 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the |
| 3179 | resulting TREE_PURPOSE will mention the parameter packs in |
| 3180 | both the bases and the arguments to the bases. */ |
| 3181 | tree purpose; |
| 3182 | tree value; |
| 3183 | tree parameter_packs = NULL_TREE; |
| 3184 | |
| 3185 | /* Determine which parameter packs will be used by the base |
| 3186 | class expansion. */ |
| 3187 | ppd.visited = pointer_set_create (); |
| 3188 | ppd.parameter_packs = ¶meter_packs; |
| 3189 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
| 3190 | &ppd, ppd.visited); |
| 3191 | |
| 3192 | if (parameter_packs == NULL_TREE) |
| 3193 | { |
| 3194 | error ("base initializer expansion %<%T%> contains no parameter packs", arg); |
| 3195 | pointer_set_destroy (ppd.visited); |
| 3196 | return error_mark_node; |
| 3197 | } |
| 3198 | |
| 3199 | if (TREE_VALUE (arg) != void_type_node) |
| 3200 | { |
| 3201 | /* Collect the sets of parameter packs used in each of the |
| 3202 | initialization arguments. */ |
| 3203 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) |
| 3204 | { |
| 3205 | /* Determine which parameter packs will be expanded in this |
| 3206 | argument. */ |
| 3207 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
| 3208 | &ppd, ppd.visited); |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | pointer_set_destroy (ppd.visited); |
| 3213 | |
| 3214 | /* Create the pack expansion type for the base type. */ |
| 3215 | purpose = cxx_make_type (TYPE_PACK_EXPANSION); |
| 3216 | SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg)); |
| 3217 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; |
| 3218 | |
| 3219 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; |
| 3220 | they will rarely be compared to anything. */ |
| 3221 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); |
| 3222 | |
| 3223 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); |
| 3224 | } |
| 3225 | |
| 3226 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) |
| 3227 | for_types = true; |
| 3228 | |
| 3229 | /* Build the PACK_EXPANSION_* node. */ |
| 3230 | result = for_types |
| 3231 | ? cxx_make_type (TYPE_PACK_EXPANSION) |
| 3232 | : make_node (EXPR_PACK_EXPANSION); |
| 3233 | SET_PACK_EXPANSION_PATTERN (result, arg); |
| 3234 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) |
| 3235 | { |
| 3236 | /* Propagate type and const-expression information. */ |
| 3237 | TREE_TYPE (result) = TREE_TYPE (arg); |
| 3238 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); |
| 3239 | } |
| 3240 | else |
| 3241 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; |
| 3242 | they will rarely be compared to anything. */ |
| 3243 | SET_TYPE_STRUCTURAL_EQUALITY (result); |
| 3244 | |
| 3245 | /* Determine which parameter packs will be expanded. */ |
| 3246 | ppd.parameter_packs = ¶meter_packs; |
| 3247 | ppd.visited = pointer_set_create (); |
| 3248 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
| 3249 | pointer_set_destroy (ppd.visited); |
| 3250 | |
| 3251 | /* Make sure we found some parameter packs. */ |
| 3252 | if (parameter_packs == NULL_TREE) |
| 3253 | { |
| 3254 | if (TYPE_P (arg)) |
| 3255 | error ("expansion pattern %<%T%> contains no argument packs", arg); |
| 3256 | else |
| 3257 | error ("expansion pattern %<%E%> contains no argument packs", arg); |
| 3258 | return error_mark_node; |
| 3259 | } |
| 3260 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; |
| 3261 | |
| 3262 | PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p (); |
| 3263 | |
| 3264 | return result; |
| 3265 | } |
| 3266 | |
| 3267 | /* Checks T for any "bare" parameter packs, which have not yet been |
| 3268 | expanded, and issues an error if any are found. This operation can |
| 3269 | only be done on full expressions or types (e.g., an expression |
| 3270 | statement, "if" condition, etc.), because we could have expressions like: |
| 3271 | |
| 3272 | foo(f(g(h(args)))...) |
| 3273 | |
| 3274 | where "args" is a parameter pack. check_for_bare_parameter_packs |
| 3275 | should not be called for the subexpressions args, h(args), |
| 3276 | g(h(args)), or f(g(h(args))), because we would produce erroneous |
| 3277 | error messages. |
| 3278 | |
| 3279 | Returns TRUE and emits an error if there were bare parameter packs, |
| 3280 | returns FALSE otherwise. */ |
| 3281 | bool |
| 3282 | check_for_bare_parameter_packs (tree t) |
| 3283 | { |
| 3284 | tree parameter_packs = NULL_TREE; |
| 3285 | struct find_parameter_pack_data ppd; |
| 3286 | |
| 3287 | if (!processing_template_decl || !t || t == error_mark_node) |
| 3288 | return false; |
| 3289 | |
| 3290 | if (TREE_CODE (t) == TYPE_DECL) |
| 3291 | t = TREE_TYPE (t); |
| 3292 | |
| 3293 | ppd.parameter_packs = ¶meter_packs; |
| 3294 | ppd.visited = pointer_set_create (); |
| 3295 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
| 3296 | pointer_set_destroy (ppd.visited); |
| 3297 | |
| 3298 | if (parameter_packs) |
| 3299 | { |
| 3300 | error ("parameter packs not expanded with %<...%>:"); |
| 3301 | while (parameter_packs) |
| 3302 | { |
| 3303 | tree pack = TREE_VALUE (parameter_packs); |
| 3304 | tree name = NULL_TREE; |
| 3305 | |
| 3306 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM |
| 3307 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) |
| 3308 | name = TYPE_NAME (pack); |
| 3309 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) |
| 3310 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); |
| 3311 | else |
| 3312 | name = DECL_NAME (pack); |
| 3313 | |
| 3314 | if (name) |
| 3315 | inform (input_location, " %qD", name); |
| 3316 | else |
| 3317 | inform (input_location, " <anonymous>"); |
| 3318 | |
| 3319 | parameter_packs = TREE_CHAIN (parameter_packs); |
| 3320 | } |
| 3321 | |
| 3322 | return true; |
| 3323 | } |
| 3324 | |
| 3325 | return false; |
| 3326 | } |
| 3327 | |
| 3328 | /* Expand any parameter packs that occur in the template arguments in |
| 3329 | ARGS. */ |
| 3330 | tree |
| 3331 | expand_template_argument_pack (tree args) |
| 3332 | { |
| 3333 | tree result_args = NULL_TREE; |
| 3334 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; |
| 3335 | int num_result_args = -1; |
| 3336 | int non_default_args_count = -1; |
| 3337 | |
| 3338 | /* First, determine if we need to expand anything, and the number of |
| 3339 | slots we'll need. */ |
| 3340 | for (in_arg = 0; in_arg < nargs; ++in_arg) |
| 3341 | { |
| 3342 | tree arg = TREE_VEC_ELT (args, in_arg); |
| 3343 | if (arg == NULL_TREE) |
| 3344 | return args; |
| 3345 | if (ARGUMENT_PACK_P (arg)) |
| 3346 | { |
| 3347 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); |
| 3348 | if (num_result_args < 0) |
| 3349 | num_result_args = in_arg + num_packed; |
| 3350 | else |
| 3351 | num_result_args += num_packed; |
| 3352 | } |
| 3353 | else |
| 3354 | { |
| 3355 | if (num_result_args >= 0) |
| 3356 | num_result_args++; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | /* If no expansion is necessary, we're done. */ |
| 3361 | if (num_result_args < 0) |
| 3362 | return args; |
| 3363 | |
| 3364 | /* Expand arguments. */ |
| 3365 | result_args = make_tree_vec (num_result_args); |
| 3366 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args)) |
| 3367 | non_default_args_count = |
| 3368 | GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args); |
| 3369 | for (in_arg = 0; in_arg < nargs; ++in_arg) |
| 3370 | { |
| 3371 | tree arg = TREE_VEC_ELT (args, in_arg); |
| 3372 | if (ARGUMENT_PACK_P (arg)) |
| 3373 | { |
| 3374 | tree packed = ARGUMENT_PACK_ARGS (arg); |
| 3375 | int i, num_packed = TREE_VEC_LENGTH (packed); |
| 3376 | for (i = 0; i < num_packed; ++i, ++out_arg) |
| 3377 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); |
| 3378 | if (non_default_args_count > 0) |
| 3379 | non_default_args_count += num_packed; |
| 3380 | } |
| 3381 | else |
| 3382 | { |
| 3383 | TREE_VEC_ELT (result_args, out_arg) = arg; |
| 3384 | ++out_arg; |
| 3385 | } |
| 3386 | } |
| 3387 | if (non_default_args_count >= 0) |
| 3388 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count); |
| 3389 | return result_args; |
| 3390 | } |
| 3391 | |
| 3392 | /* Checks if DECL shadows a template parameter. |
| 3393 | |
| 3394 | [temp.local]: A template-parameter shall not be redeclared within its |
| 3395 | scope (including nested scopes). |
| 3396 | |
| 3397 | Emits an error and returns TRUE if the DECL shadows a parameter, |
| 3398 | returns FALSE otherwise. */ |
| 3399 | |
| 3400 | bool |
| 3401 | check_template_shadow (tree decl) |
| 3402 | { |
| 3403 | tree olddecl; |
| 3404 | |
| 3405 | /* If we're not in a template, we can't possibly shadow a template |
| 3406 | parameter. */ |
| 3407 | if (!current_template_parms) |
| 3408 | return true; |
| 3409 | |
| 3410 | /* Figure out what we're shadowing. */ |
| 3411 | if (TREE_CODE (decl) == OVERLOAD) |
| 3412 | decl = OVL_CURRENT (decl); |
| 3413 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
| 3414 | |
| 3415 | /* If there's no previous binding for this name, we're not shadowing |
| 3416 | anything, let alone a template parameter. */ |
| 3417 | if (!olddecl) |
| 3418 | return true; |
| 3419 | |
| 3420 | /* If we're not shadowing a template parameter, we're done. Note |
| 3421 | that OLDDECL might be an OVERLOAD (or perhaps even an |
| 3422 | ERROR_MARK), so we can't just blithely assume it to be a _DECL |
| 3423 | node. */ |
| 3424 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
| 3425 | return true; |
| 3426 | |
| 3427 | /* We check for decl != olddecl to avoid bogus errors for using a |
| 3428 | name inside a class. We check TPFI to avoid duplicate errors for |
| 3429 | inline member templates. */ |
| 3430 | if (decl == olddecl |
| 3431 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
| 3432 | return true; |
| 3433 | |
| 3434 | error ("declaration of %q+#D", decl); |
| 3435 | error (" shadows template parm %q+#D", olddecl); |
| 3436 | return false; |
| 3437 | } |
| 3438 | |
| 3439 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
| 3440 | ORIG_LEVEL, DECL, and TYPE. */ |
| 3441 | |
| 3442 | static tree |
| 3443 | build_template_parm_index (int index, |
| 3444 | int level, |
| 3445 | int orig_level, |
| 3446 | tree decl, |
| 3447 | tree type) |
| 3448 | { |
| 3449 | tree t = make_node (TEMPLATE_PARM_INDEX); |
| 3450 | TEMPLATE_PARM_IDX (t) = index; |
| 3451 | TEMPLATE_PARM_LEVEL (t) = level; |
| 3452 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; |
| 3453 | TEMPLATE_PARM_DECL (t) = decl; |
| 3454 | TREE_TYPE (t) = type; |
| 3455 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
| 3456 | TREE_READONLY (t) = TREE_READONLY (decl); |
| 3457 | |
| 3458 | return t; |
| 3459 | } |
| 3460 | |
| 3461 | /* Find the canonical type parameter for the given template type |
| 3462 | parameter. Returns the canonical type parameter, which may be TYPE |
| 3463 | if no such parameter existed. */ |
| 3464 | |
| 3465 | static tree |
| 3466 | canonical_type_parameter (tree type) |
| 3467 | { |
| 3468 | tree list; |
| 3469 | int idx = TEMPLATE_TYPE_IDX (type); |
| 3470 | if (!canonical_template_parms) |
| 3471 | canonical_template_parms = VEC_alloc (tree, gc, idx+1); |
| 3472 | |
| 3473 | while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx) |
| 3474 | VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE); |
| 3475 | |
| 3476 | list = VEC_index (tree, canonical_template_parms, idx); |
| 3477 | while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL)) |
| 3478 | list = TREE_CHAIN (list); |
| 3479 | |
| 3480 | if (list) |
| 3481 | return TREE_VALUE (list); |
| 3482 | else |
| 3483 | { |
| 3484 | VEC_replace(tree, canonical_template_parms, idx, |
| 3485 | tree_cons (NULL_TREE, type, |
| 3486 | VEC_index (tree, canonical_template_parms, idx))); |
| 3487 | return type; |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
| 3492 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
| 3493 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
| 3494 | new one is created. */ |
| 3495 | |
| 3496 | static tree |
| 3497 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
| 3498 | tsubst_flags_t complain) |
| 3499 | { |
| 3500 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE |
| 3501 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) |
| 3502 | != TEMPLATE_PARM_LEVEL (index) - levels) |
| 3503 | || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))) |
| 3504 | { |
| 3505 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
| 3506 | tree decl, t; |
| 3507 | |
| 3508 | decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), |
| 3509 | TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
| 3510 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); |
| 3511 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
| 3512 | DECL_ARTIFICIAL (decl) = 1; |
| 3513 | SET_DECL_TEMPLATE_PARM_P (decl); |
| 3514 | |
| 3515 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
| 3516 | TEMPLATE_PARM_LEVEL (index) - levels, |
| 3517 | TEMPLATE_PARM_ORIG_LEVEL (index), |
| 3518 | decl, type); |
| 3519 | TEMPLATE_PARM_DESCENDANTS (index) = t; |
| 3520 | TEMPLATE_PARM_PARAMETER_PACK (t) |
| 3521 | = TEMPLATE_PARM_PARAMETER_PACK (index); |
| 3522 | |
| 3523 | /* Template template parameters need this. */ |
| 3524 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
| 3525 | DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms |
| 3526 | (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)), |
| 3527 | args, complain); |
| 3528 | } |
| 3529 | |
| 3530 | return TEMPLATE_PARM_DESCENDANTS (index); |
| 3531 | } |
| 3532 | |
| 3533 | /* Process information from new template parameter PARM and append it |
| 3534 | to the LIST being built. This new parameter is a non-type |
| 3535 | parameter iff IS_NON_TYPE is true. This new parameter is a |
| 3536 | parameter pack iff IS_PARAMETER_PACK is true. The location of PARM |
| 3537 | is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template |
| 3538 | parameter list PARM belongs to. This is used used to create a |
| 3539 | proper canonical type for the type of PARM that is to be created, |
| 3540 | iff PARM is a type. If the size is not known, this parameter shall |
| 3541 | be set to 0. */ |
| 3542 | |
| 3543 | tree |
| 3544 | process_template_parm (tree list, location_t parm_loc, tree parm, |
| 3545 | bool is_non_type, bool is_parameter_pack) |
| 3546 | { |
| 3547 | tree decl = 0; |
| 3548 | tree defval; |
| 3549 | tree err_parm_list; |
| 3550 | int idx = 0; |
| 3551 | |
| 3552 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
| 3553 | defval = TREE_PURPOSE (parm); |
| 3554 | |
| 3555 | if (list) |
| 3556 | { |
| 3557 | tree p = tree_last (list); |
| 3558 | |
| 3559 | if (p && TREE_VALUE (p) != error_mark_node) |
| 3560 | { |
| 3561 | p = TREE_VALUE (p); |
| 3562 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) |
| 3563 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); |
| 3564 | else |
| 3565 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
| 3566 | } |
| 3567 | |
| 3568 | ++idx; |
| 3569 | } |
| 3570 | else |
| 3571 | idx = 0; |
| 3572 | |
| 3573 | if (is_non_type) |
| 3574 | { |
| 3575 | parm = TREE_VALUE (parm); |
| 3576 | |
| 3577 | SET_DECL_TEMPLATE_PARM_P (parm); |
| 3578 | |
| 3579 | if (TREE_TYPE (parm) == error_mark_node) |
| 3580 | { |
| 3581 | err_parm_list = build_tree_list (defval, parm); |
| 3582 | TREE_VALUE (err_parm_list) = error_mark_node; |
| 3583 | return chainon (list, err_parm_list); |
| 3584 | } |
| 3585 | else |
| 3586 | { |
| 3587 | /* [temp.param] |
| 3588 | |
| 3589 | The top-level cv-qualifiers on the template-parameter are |
| 3590 | ignored when determining its type. */ |
| 3591 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); |
| 3592 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) |
| 3593 | { |
| 3594 | err_parm_list = build_tree_list (defval, parm); |
| 3595 | TREE_VALUE (err_parm_list) = error_mark_node; |
| 3596 | return chainon (list, err_parm_list); |
| 3597 | } |
| 3598 | |
| 3599 | if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack) |
| 3600 | { |
| 3601 | /* This template parameter is not a parameter pack, but it |
| 3602 | should be. Complain about "bare" parameter packs. */ |
| 3603 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
| 3604 | |
| 3605 | /* Recover by calling this a parameter pack. */ |
| 3606 | is_parameter_pack = true; |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | /* A template parameter is not modifiable. */ |
| 3611 | TREE_CONSTANT (parm) = 1; |
| 3612 | TREE_READONLY (parm) = 1; |
| 3613 | decl = build_decl (parm_loc, |
| 3614 | CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
| 3615 | TREE_CONSTANT (decl) = 1; |
| 3616 | TREE_READONLY (decl) = 1; |
| 3617 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
| 3618 | = build_template_parm_index (idx, processing_template_decl, |
| 3619 | processing_template_decl, |
| 3620 | decl, TREE_TYPE (parm)); |
| 3621 | |
| 3622 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) |
| 3623 | = is_parameter_pack; |
| 3624 | } |
| 3625 | else |
| 3626 | { |
| 3627 | tree t; |
| 3628 | parm = TREE_VALUE (TREE_VALUE (parm)); |
| 3629 | |
| 3630 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
| 3631 | { |
| 3632 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
| 3633 | /* This is for distinguishing between real templates and template |
| 3634 | template parameters */ |
| 3635 | TREE_TYPE (parm) = t; |
| 3636 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; |
| 3637 | decl = parm; |
| 3638 | } |
| 3639 | else |
| 3640 | { |
| 3641 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
| 3642 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
| 3643 | decl = build_decl (parm_loc, |
| 3644 | TYPE_DECL, parm, t); |
| 3645 | } |
| 3646 | |
| 3647 | TYPE_NAME (t) = decl; |
| 3648 | TYPE_STUB_DECL (t) = decl; |
| 3649 | parm = decl; |
| 3650 | TEMPLATE_TYPE_PARM_INDEX (t) |
| 3651 | = build_template_parm_index (idx, processing_template_decl, |
| 3652 | processing_template_decl, |
| 3653 | decl, TREE_TYPE (parm)); |
| 3654 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
| 3655 | TYPE_CANONICAL (t) = canonical_type_parameter (t); |
| 3656 | } |
| 3657 | DECL_ARTIFICIAL (decl) = 1; |
| 3658 | SET_DECL_TEMPLATE_PARM_P (decl); |
| 3659 | pushdecl (decl); |
| 3660 | parm = build_tree_list (defval, parm); |
| 3661 | return chainon (list, parm); |
| 3662 | } |
| 3663 | |
| 3664 | /* The end of a template parameter list has been reached. Process the |
| 3665 | tree list into a parameter vector, converting each parameter into a more |
| 3666 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others |
| 3667 | as PARM_DECLs. */ |
| 3668 | |
| 3669 | tree |
| 3670 | end_template_parm_list (tree parms) |
| 3671 | { |
| 3672 | int nparms; |
| 3673 | tree parm, next; |
| 3674 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
| 3675 | |
| 3676 | current_template_parms |
| 3677 | = tree_cons (size_int (processing_template_decl), |
| 3678 | saved_parmlist, current_template_parms); |
| 3679 | |
| 3680 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
| 3681 | { |
| 3682 | next = TREE_CHAIN (parm); |
| 3683 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
| 3684 | TREE_CHAIN (parm) = NULL_TREE; |
| 3685 | } |
| 3686 | |
| 3687 | --processing_template_parmlist; |
| 3688 | |
| 3689 | return saved_parmlist; |
| 3690 | } |
| 3691 | |
| 3692 | /* end_template_decl is called after a template declaration is seen. */ |
| 3693 | |
| 3694 | void |
| 3695 | end_template_decl (void) |
| 3696 | { |
| 3697 | reset_specialization (); |
| 3698 | |
| 3699 | if (! processing_template_decl) |
| 3700 | return; |
| 3701 | |
| 3702 | /* This matches the pushlevel in begin_template_parm_list. */ |
| 3703 | finish_scope (); |
| 3704 | |
| 3705 | --processing_template_decl; |
| 3706 | current_template_parms = TREE_CHAIN (current_template_parms); |
| 3707 | } |
| 3708 | |
| 3709 | /* Takes a TREE_LIST representing a template parameter and convert it |
| 3710 | into an argument suitable to be passed to the type substitution |
| 3711 | functions. Note that If the TREE_LIST contains an error_mark |
| 3712 | node, the returned argument is error_mark_node. */ |
| 3713 | |
| 3714 | static tree |
| 3715 | template_parm_to_arg (tree t) |
| 3716 | { |
| 3717 | |
| 3718 | if (t == NULL_TREE |
| 3719 | || TREE_CODE (t) != TREE_LIST) |
| 3720 | return t; |
| 3721 | |
| 3722 | if (error_operand_p (TREE_VALUE (t))) |
| 3723 | return error_mark_node; |
| 3724 | |
| 3725 | t = TREE_VALUE (t); |
| 3726 | |
| 3727 | if (TREE_CODE (t) == TYPE_DECL |
| 3728 | || TREE_CODE (t) == TEMPLATE_DECL) |
| 3729 | { |
| 3730 | t = TREE_TYPE (t); |
| 3731 | |
| 3732 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) |
| 3733 | { |
| 3734 | /* Turn this argument into a TYPE_ARGUMENT_PACK |
| 3735 | with a single element, which expands T. */ |
| 3736 | tree vec = make_tree_vec (1); |
| 3737 | #ifdef ENABLE_CHECKING |
| 3738 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT |
| 3739 | (vec, TREE_VEC_LENGTH (vec)); |
| 3740 | #endif |
| 3741 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); |
| 3742 | |
| 3743 | t = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 3744 | SET_ARGUMENT_PACK_ARGS (t, vec); |
| 3745 | } |
| 3746 | } |
| 3747 | else |
| 3748 | { |
| 3749 | t = DECL_INITIAL (t); |
| 3750 | |
| 3751 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) |
| 3752 | { |
| 3753 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK |
| 3754 | with a single element, which expands T. */ |
| 3755 | tree vec = make_tree_vec (1); |
| 3756 | tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t)); |
| 3757 | #ifdef ENABLE_CHECKING |
| 3758 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT |
| 3759 | (vec, TREE_VEC_LENGTH (vec)); |
| 3760 | #endif |
| 3761 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); |
| 3762 | |
| 3763 | t = make_node (NONTYPE_ARGUMENT_PACK); |
| 3764 | SET_ARGUMENT_PACK_ARGS (t, vec); |
| 3765 | TREE_TYPE (t) = type; |
| 3766 | } |
| 3767 | } |
| 3768 | return t; |
| 3769 | } |
| 3770 | |
| 3771 | /* This function returns TRUE if PARM_PACK is a template parameter |
| 3772 | pack and if ARG_PACK is what template_parm_to_arg returned when |
| 3773 | passed PARM_PACK. */ |
| 3774 | |
| 3775 | static bool |
| 3776 | arg_from_parm_pack_p (tree arg_pack, tree parm_pack) |
| 3777 | { |
| 3778 | /* For clarity in the comments below let's use the representation |
| 3779 | argument_pack<elements>' to denote an argument pack and its |
| 3780 | elements. |
| 3781 | |
| 3782 | In the 'if' block below, we want to detect cases where |
| 3783 | ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to |
| 3784 | check if ARG_PACK is an argument pack which sole element is |
| 3785 | the expansion of PARM_PACK. That argument pack is typically |
| 3786 | created by template_parm_to_arg when passed a parameter |
| 3787 | pack. */ |
| 3788 | |
| 3789 | if (arg_pack |
| 3790 | && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 |
| 3791 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0))) |
| 3792 | { |
| 3793 | tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0); |
| 3794 | tree pattern = PACK_EXPANSION_PATTERN (expansion); |
| 3795 | if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack)) |
| 3796 | || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern))) |
| 3797 | /* The argument pack that the parameter maps to is just an |
| 3798 | expansion of the parameter itself, such as one would |
| 3799 | find in the implicit typedef of a class inside the |
| 3800 | class itself. Consider this parameter "unsubstituted", |
| 3801 | so that we will maintain the outer pack expansion. */ |
| 3802 | return true; |
| 3803 | } |
| 3804 | return false; |
| 3805 | } |
| 3806 | |
| 3807 | /* Within the declaration of a template, return all levels of template |
| 3808 | parameters that apply. The template parameters are represented as |
| 3809 | a TREE_VEC, in the form documented in cp-tree.h for template |
| 3810 | arguments. */ |
| 3811 | |
| 3812 | static tree |
| 3813 | current_template_args (void) |
| 3814 | { |
| 3815 | tree header; |
| 3816 | tree args = NULL_TREE; |
| 3817 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
| 3818 | int l = length; |
| 3819 | |
| 3820 | /* If there is only one level of template parameters, we do not |
| 3821 | create a TREE_VEC of TREE_VECs. Instead, we return a single |
| 3822 | TREE_VEC containing the arguments. */ |
| 3823 | if (length > 1) |
| 3824 | args = make_tree_vec (length); |
| 3825 | |
| 3826 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) |
| 3827 | { |
| 3828 | tree a = copy_node (TREE_VALUE (header)); |
| 3829 | int i; |
| 3830 | |
| 3831 | TREE_TYPE (a) = NULL_TREE; |
| 3832 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
| 3833 | TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i)); |
| 3834 | |
| 3835 | #ifdef ENABLE_CHECKING |
| 3836 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a)); |
| 3837 | #endif |
| 3838 | |
| 3839 | if (length > 1) |
| 3840 | TREE_VEC_ELT (args, --l) = a; |
| 3841 | else |
| 3842 | args = a; |
| 3843 | } |
| 3844 | |
| 3845 | if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE) |
| 3846 | /* This can happen for template parms of a template template |
| 3847 | parameter, e.g: |
| 3848 | |
| 3849 | template<template<class T, class U> class TT> struct S; |
| 3850 | |
| 3851 | Consider the level of the parms of TT; T and U both have |
| 3852 | level 2; TT has no template parm of level 1. So in this case |
| 3853 | the first element of full_template_args is NULL_TREE. If we |
| 3854 | leave it like this TMPL_ARG_DEPTH on args returns 1 instead |
| 3855 | of 2. This will make tsubst wrongly consider that T and U |
| 3856 | have level 1. Instead, let's create a dummy vector as the |
| 3857 | first element of full_template_args so that TMPL_ARG_DEPTH |
| 3858 | returns the correct depth for args. */ |
| 3859 | TREE_VEC_ELT (args, 0) = make_tree_vec (1); |
| 3860 | return args; |
| 3861 | } |
| 3862 | |
| 3863 | /* Update the declared TYPE by doing any lookups which were thought to be |
| 3864 | dependent, but are not now that we know the SCOPE of the declarator. */ |
| 3865 | |
| 3866 | tree |
| 3867 | maybe_update_decl_type (tree orig_type, tree scope) |
| 3868 | { |
| 3869 | tree type = orig_type; |
| 3870 | |
| 3871 | if (type == NULL_TREE) |
| 3872 | return type; |
| 3873 | |
| 3874 | if (TREE_CODE (orig_type) == TYPE_DECL) |
| 3875 | type = TREE_TYPE (type); |
| 3876 | |
| 3877 | if (scope && TYPE_P (scope) && dependent_type_p (scope) |
| 3878 | && dependent_type_p (type) |
| 3879 | /* Don't bother building up the args in this case. */ |
| 3880 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM) |
| 3881 | { |
| 3882 | /* tsubst in the args corresponding to the template parameters, |
| 3883 | including auto if present. Most things will be unchanged, but |
| 3884 | make_typename_type and tsubst_qualified_id will resolve |
| 3885 | TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */ |
| 3886 | tree args = current_template_args (); |
| 3887 | tree auto_node = type_uses_auto (type); |
| 3888 | tree pushed; |
| 3889 | if (auto_node) |
| 3890 | { |
| 3891 | tree auto_vec = make_tree_vec (1); |
| 3892 | TREE_VEC_ELT (auto_vec, 0) = auto_node; |
| 3893 | args = add_to_template_args (args, auto_vec); |
| 3894 | } |
| 3895 | pushed = push_scope (scope); |
| 3896 | type = tsubst (type, args, tf_warning_or_error, NULL_TREE); |
| 3897 | if (pushed) |
| 3898 | pop_scope (scope); |
| 3899 | } |
| 3900 | |
| 3901 | if (type == error_mark_node) |
| 3902 | return orig_type; |
| 3903 | |
| 3904 | if (TREE_CODE (orig_type) == TYPE_DECL) |
| 3905 | { |
| 3906 | if (same_type_p (type, TREE_TYPE (orig_type))) |
| 3907 | type = orig_type; |
| 3908 | else |
| 3909 | type = TYPE_NAME (type); |
| 3910 | } |
| 3911 | return type; |
| 3912 | } |
| 3913 | |
| 3914 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
| 3915 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
| 3916 | a member template. Used by push_template_decl below. */ |
| 3917 | |
| 3918 | static tree |
| 3919 | build_template_decl (tree decl, tree parms, bool member_template_p) |
| 3920 | { |
| 3921 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); |
| 3922 | DECL_TEMPLATE_PARMS (tmpl) = parms; |
| 3923 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); |
| 3924 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
| 3925 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
| 3926 | |
| 3927 | return tmpl; |
| 3928 | } |
| 3929 | |
| 3930 | struct template_parm_data |
| 3931 | { |
| 3932 | /* The level of the template parameters we are currently |
| 3933 | processing. */ |
| 3934 | int level; |
| 3935 | |
| 3936 | /* The index of the specialization argument we are currently |
| 3937 | processing. */ |
| 3938 | int current_arg; |
| 3939 | |
| 3940 | /* An array whose size is the number of template parameters. The |
| 3941 | elements are nonzero if the parameter has been used in any one |
| 3942 | of the arguments processed so far. */ |
| 3943 | int* parms; |
| 3944 | |
| 3945 | /* An array whose size is the number of template arguments. The |
| 3946 | elements are nonzero if the argument makes use of template |
| 3947 | parameters of this level. */ |
| 3948 | int* arg_uses_template_parms; |
| 3949 | }; |
| 3950 | |
| 3951 | /* Subroutine of push_template_decl used to see if each template |
| 3952 | parameter in a partial specialization is used in the explicit |
| 3953 | argument list. If T is of the LEVEL given in DATA (which is |
| 3954 | treated as a template_parm_data*), then DATA->PARMS is marked |
| 3955 | appropriately. */ |
| 3956 | |
| 3957 | static int |
| 3958 | mark_template_parm (tree t, void* data) |
| 3959 | { |
| 3960 | int level; |
| 3961 | int idx; |
| 3962 | struct template_parm_data* tpd = (struct template_parm_data*) data; |
| 3963 | |
| 3964 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) |
| 3965 | { |
| 3966 | level = TEMPLATE_PARM_LEVEL (t); |
| 3967 | idx = TEMPLATE_PARM_IDX (t); |
| 3968 | } |
| 3969 | else |
| 3970 | { |
| 3971 | level = TEMPLATE_TYPE_LEVEL (t); |
| 3972 | idx = TEMPLATE_TYPE_IDX (t); |
| 3973 | } |
| 3974 | |
| 3975 | if (level == tpd->level) |
| 3976 | { |
| 3977 | tpd->parms[idx] = 1; |
| 3978 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; |
| 3979 | } |
| 3980 | |
| 3981 | /* Return zero so that for_each_template_parm will continue the |
| 3982 | traversal of the tree; we want to mark *every* template parm. */ |
| 3983 | return 0; |
| 3984 | } |
| 3985 | |
| 3986 | /* Process the partial specialization DECL. */ |
| 3987 | |
| 3988 | static tree |
| 3989 | process_partial_specialization (tree decl) |
| 3990 | { |
| 3991 | tree type = TREE_TYPE (decl); |
| 3992 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); |
| 3993 | tree specargs = CLASSTYPE_TI_ARGS (type); |
| 3994 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
| 3995 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); |
| 3996 | tree inner_parms; |
| 3997 | tree inst; |
| 3998 | int nargs = TREE_VEC_LENGTH (inner_args); |
| 3999 | int ntparms; |
| 4000 | int i; |
| 4001 | bool did_error_intro = false; |
| 4002 | struct template_parm_data tpd; |
| 4003 | struct template_parm_data tpd2; |
| 4004 | |
| 4005 | gcc_assert (current_template_parms); |
| 4006 | |
| 4007 | inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
| 4008 | ntparms = TREE_VEC_LENGTH (inner_parms); |
| 4009 | |
| 4010 | /* We check that each of the template parameters given in the |
| 4011 | partial specialization is used in the argument list to the |
| 4012 | specialization. For example: |
| 4013 | |
| 4014 | template <class T> struct S; |
| 4015 | template <class T> struct S<T*>; |
| 4016 | |
| 4017 | The second declaration is OK because `T*' uses the template |
| 4018 | parameter T, whereas |
| 4019 | |
| 4020 | template <class T> struct S<int>; |
| 4021 | |
| 4022 | is no good. Even trickier is: |
| 4023 | |
| 4024 | template <class T> |
| 4025 | struct S1 |
| 4026 | { |
| 4027 | template <class U> |
| 4028 | struct S2; |
| 4029 | template <class U> |
| 4030 | struct S2<T>; |
| 4031 | }; |
| 4032 | |
| 4033 | The S2<T> declaration is actually invalid; it is a |
| 4034 | full-specialization. Of course, |
| 4035 | |
| 4036 | template <class U> |
| 4037 | struct S2<T (*)(U)>; |
| 4038 | |
| 4039 | or some such would have been OK. */ |
| 4040 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); |
| 4041 | tpd.parms = XALLOCAVEC (int, ntparms); |
| 4042 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
| 4043 | |
| 4044 | tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
| 4045 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
| 4046 | for (i = 0; i < nargs; ++i) |
| 4047 | { |
| 4048 | tpd.current_arg = i; |
| 4049 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), |
| 4050 | &mark_template_parm, |
| 4051 | &tpd, |
| 4052 | NULL, |
| 4053 | /*include_nondeduced_p=*/false); |
| 4054 | } |
| 4055 | for (i = 0; i < ntparms; ++i) |
| 4056 | if (tpd.parms[i] == 0) |
| 4057 | { |
| 4058 | /* One of the template parms was not used in the |
| 4059 | specialization. */ |
| 4060 | if (!did_error_intro) |
| 4061 | { |
| 4062 | error ("template parameters not used in partial specialization:"); |
| 4063 | did_error_intro = true; |
| 4064 | } |
| 4065 | |
| 4066 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
| 4067 | } |
| 4068 | |
| 4069 | if (did_error_intro) |
| 4070 | return error_mark_node; |
| 4071 | |
| 4072 | /* [temp.class.spec] |
| 4073 | |
| 4074 | The argument list of the specialization shall not be identical to |
| 4075 | the implicit argument list of the primary template. */ |
| 4076 | if (comp_template_args |
| 4077 | (inner_args, |
| 4078 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
| 4079 | (maintmpl))))) |
| 4080 | error ("partial specialization %qT does not specialize any template arguments", type); |
| 4081 | |
| 4082 | /* [temp.class.spec] |
| 4083 | |
| 4084 | A partially specialized non-type argument expression shall not |
| 4085 | involve template parameters of the partial specialization except |
| 4086 | when the argument expression is a simple identifier. |
| 4087 | |
| 4088 | The type of a template parameter corresponding to a specialized |
| 4089 | non-type argument shall not be dependent on a parameter of the |
| 4090 | specialization. |
| 4091 | |
| 4092 | Also, we verify that pack expansions only occur at the |
| 4093 | end of the argument list. */ |
| 4094 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
| 4095 | tpd2.parms = 0; |
| 4096 | for (i = 0; i < nargs; ++i) |
| 4097 | { |
| 4098 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
| 4099 | tree arg = TREE_VEC_ELT (inner_args, i); |
| 4100 | tree packed_args = NULL_TREE; |
| 4101 | int j, len = 1; |
| 4102 | |
| 4103 | if (ARGUMENT_PACK_P (arg)) |
| 4104 | { |
| 4105 | /* Extract the arguments from the argument pack. We'll be |
| 4106 | iterating over these in the following loop. */ |
| 4107 | packed_args = ARGUMENT_PACK_ARGS (arg); |
| 4108 | len = TREE_VEC_LENGTH (packed_args); |
| 4109 | } |
| 4110 | |
| 4111 | for (j = 0; j < len; j++) |
| 4112 | { |
| 4113 | if (packed_args) |
| 4114 | /* Get the Jth argument in the parameter pack. */ |
| 4115 | arg = TREE_VEC_ELT (packed_args, j); |
| 4116 | |
| 4117 | if (PACK_EXPANSION_P (arg)) |
| 4118 | { |
| 4119 | /* Pack expansions must come at the end of the |
| 4120 | argument list. */ |
| 4121 | if ((packed_args && j < len - 1) |
| 4122 | || (!packed_args && i < nargs - 1)) |
| 4123 | { |
| 4124 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
| 4125 | error ("parameter pack argument %qE must be at the " |
| 4126 | "end of the template argument list", arg); |
| 4127 | else |
| 4128 | error ("parameter pack argument %qT must be at the " |
| 4129 | "end of the template argument list", arg); |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
| 4134 | /* We only care about the pattern. */ |
| 4135 | arg = PACK_EXPANSION_PATTERN (arg); |
| 4136 | |
| 4137 | if (/* These first two lines are the `non-type' bit. */ |
| 4138 | !TYPE_P (arg) |
| 4139 | && TREE_CODE (arg) != TEMPLATE_DECL |
| 4140 | /* This next line is the `argument expression is not just a |
| 4141 | simple identifier' condition and also the `specialized |
| 4142 | non-type argument' bit. */ |
| 4143 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) |
| 4144 | { |
| 4145 | if ((!packed_args && tpd.arg_uses_template_parms[i]) |
| 4146 | || (packed_args && uses_template_parms (arg))) |
| 4147 | error ("template argument %qE involves template parameter(s)", |
| 4148 | arg); |
| 4149 | else |
| 4150 | { |
| 4151 | /* Look at the corresponding template parameter, |
| 4152 | marking which template parameters its type depends |
| 4153 | upon. */ |
| 4154 | tree type = TREE_TYPE (parm); |
| 4155 | |
| 4156 | if (!tpd2.parms) |
| 4157 | { |
| 4158 | /* We haven't yet initialized TPD2. Do so now. */ |
| 4159 | tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
| 4160 | /* The number of parameters here is the number in the |
| 4161 | main template, which, as checked in the assertion |
| 4162 | above, is NARGS. */ |
| 4163 | tpd2.parms = XALLOCAVEC (int, nargs); |
| 4164 | tpd2.level = |
| 4165 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); |
| 4166 | } |
| 4167 | |
| 4168 | /* Mark the template parameters. But this time, we're |
| 4169 | looking for the template parameters of the main |
| 4170 | template, not in the specialization. */ |
| 4171 | tpd2.current_arg = i; |
| 4172 | tpd2.arg_uses_template_parms[i] = 0; |
| 4173 | memset (tpd2.parms, 0, sizeof (int) * nargs); |
| 4174 | for_each_template_parm (type, |
| 4175 | &mark_template_parm, |
| 4176 | &tpd2, |
| 4177 | NULL, |
| 4178 | /*include_nondeduced_p=*/false); |
| 4179 | |
| 4180 | if (tpd2.arg_uses_template_parms [i]) |
| 4181 | { |
| 4182 | /* The type depended on some template parameters. |
| 4183 | If they are fully specialized in the |
| 4184 | specialization, that's OK. */ |
| 4185 | int j; |
| 4186 | int count = 0; |
| 4187 | for (j = 0; j < nargs; ++j) |
| 4188 | if (tpd2.parms[j] != 0 |
| 4189 | && tpd.arg_uses_template_parms [j]) |
| 4190 | ++count; |
| 4191 | if (count != 0) |
| 4192 | error_n (input_location, count, |
| 4193 | "type %qT of template argument %qE depends " |
| 4194 | "on a template parameter", |
| 4195 | "type %qT of template argument %qE depends " |
| 4196 | "on template parameters", |
| 4197 | type, |
| 4198 | arg); |
| 4199 | } |
| 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | /* We should only get here once. */ |
| 4206 | gcc_assert (!COMPLETE_TYPE_P (type)); |
| 4207 | |
| 4208 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
| 4209 | = tree_cons (specargs, inner_parms, |
| 4210 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
| 4211 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
| 4212 | |
| 4213 | for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst; |
| 4214 | inst = TREE_CHAIN (inst)) |
| 4215 | { |
| 4216 | tree inst_type = TREE_VALUE (inst); |
| 4217 | if (COMPLETE_TYPE_P (inst_type) |
| 4218 | && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type)) |
| 4219 | { |
| 4220 | tree spec = most_specialized_class (inst_type, maintmpl, tf_none); |
| 4221 | if (spec && TREE_TYPE (spec) == type) |
| 4222 | permerror (input_location, |
| 4223 | "partial specialization of %qT after instantiation " |
| 4224 | "of %qT", type, inst_type); |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | return decl; |
| 4229 | } |
| 4230 | |
| 4231 | /* Check that a template declaration's use of default arguments and |
| 4232 | parameter packs is not invalid. Here, PARMS are the template |
| 4233 | parameters. IS_PRIMARY is nonzero if DECL is the thing declared by |
| 4234 | a primary template. IS_PARTIAL is nonzero if DECL is a partial |
| 4235 | specialization. |
| 4236 | |
| 4237 | |
| 4238 | IS_FRIEND_DECL is nonzero if DECL is a friend function template |
| 4239 | declaration (but not a definition); 1 indicates a declaration, 2 |
| 4240 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are |
| 4241 | emitted for extraneous default arguments. |
| 4242 | |
| 4243 | Returns TRUE if there were no errors found, FALSE otherwise. */ |
| 4244 | |
| 4245 | bool |
| 4246 | check_default_tmpl_args (tree decl, tree parms, int is_primary, |
| 4247 | int is_partial, int is_friend_decl) |
| 4248 | { |
| 4249 | const char *msg; |
| 4250 | int last_level_to_check; |
| 4251 | tree parm_level; |
| 4252 | bool no_errors = true; |
| 4253 | |
| 4254 | /* [temp.param] |
| 4255 | |
| 4256 | A default template-argument shall not be specified in a |
| 4257 | function template declaration or a function template definition, nor |
| 4258 | in the template-parameter-list of the definition of a member of a |
| 4259 | class template. */ |
| 4260 | |
| 4261 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
| 4262 | /* You can't have a function template declaration in a local |
| 4263 | scope, nor you can you define a member of a class template in a |
| 4264 | local scope. */ |
| 4265 | return true; |
| 4266 | |
| 4267 | if (current_class_type |
| 4268 | && !TYPE_BEING_DEFINED (current_class_type) |
| 4269 | && DECL_LANG_SPECIFIC (decl) |
| 4270 | && DECL_DECLARES_FUNCTION_P (decl) |
| 4271 | /* If this is either a friend defined in the scope of the class |
| 4272 | or a member function. */ |
| 4273 | && (DECL_FUNCTION_MEMBER_P (decl) |
| 4274 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) |
| 4275 | : DECL_FRIEND_CONTEXT (decl) |
| 4276 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) |
| 4277 | : false) |
| 4278 | /* And, if it was a member function, it really was defined in |
| 4279 | the scope of the class. */ |
| 4280 | && (!DECL_FUNCTION_MEMBER_P (decl) |
| 4281 | || DECL_INITIALIZED_IN_CLASS_P (decl))) |
| 4282 | /* We already checked these parameters when the template was |
| 4283 | declared, so there's no need to do it again now. This function |
| 4284 | was defined in class scope, but we're processing it's body now |
| 4285 | that the class is complete. */ |
| 4286 | return true; |
| 4287 | |
| 4288 | /* Core issue 226 (C++0x only): the following only applies to class |
| 4289 | templates. */ |
| 4290 | if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL) |
| 4291 | { |
| 4292 | /* [temp.param] |
| 4293 | |
| 4294 | If a template-parameter has a default template-argument, all |
| 4295 | subsequent template-parameters shall have a default |
| 4296 | template-argument supplied. */ |
| 4297 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) |
| 4298 | { |
| 4299 | tree inner_parms = TREE_VALUE (parm_level); |
| 4300 | int ntparms = TREE_VEC_LENGTH (inner_parms); |
| 4301 | int seen_def_arg_p = 0; |
| 4302 | int i; |
| 4303 | |
| 4304 | for (i = 0; i < ntparms; ++i) |
| 4305 | { |
| 4306 | tree parm = TREE_VEC_ELT (inner_parms, i); |
| 4307 | |
| 4308 | if (parm == error_mark_node) |
| 4309 | continue; |
| 4310 | |
| 4311 | if (TREE_PURPOSE (parm)) |
| 4312 | seen_def_arg_p = 1; |
| 4313 | else if (seen_def_arg_p |
| 4314 | && !template_parameter_pack_p (TREE_VALUE (parm))) |
| 4315 | { |
| 4316 | error ("no default argument for %qD", TREE_VALUE (parm)); |
| 4317 | /* For better subsequent error-recovery, we indicate that |
| 4318 | there should have been a default argument. */ |
| 4319 | TREE_PURPOSE (parm) = error_mark_node; |
| 4320 | no_errors = false; |
| 4321 | } |
| 4322 | else if (is_primary |
| 4323 | && !is_partial |
| 4324 | && !is_friend_decl |
| 4325 | /* Don't complain about an enclosing partial |
| 4326 | specialization. */ |
| 4327 | && parm_level == parms |
| 4328 | && TREE_CODE (decl) == TYPE_DECL |
| 4329 | && i < ntparms - 1 |
| 4330 | && template_parameter_pack_p (TREE_VALUE (parm))) |
| 4331 | { |
| 4332 | /* A primary class template can only have one |
| 4333 | parameter pack, at the end of the template |
| 4334 | parameter list. */ |
| 4335 | |
| 4336 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) |
| 4337 | error ("parameter pack %qE must be at the end of the" |
| 4338 | " template parameter list", TREE_VALUE (parm)); |
| 4339 | else |
| 4340 | error ("parameter pack %qT must be at the end of the" |
| 4341 | " template parameter list", |
| 4342 | TREE_TYPE (TREE_VALUE (parm))); |
| 4343 | |
| 4344 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) |
| 4345 | = error_mark_node; |
| 4346 | no_errors = false; |
| 4347 | } |
| 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
| 4353 | || is_partial |
| 4354 | || !is_primary |
| 4355 | || is_friend_decl) |
| 4356 | /* For an ordinary class template, default template arguments are |
| 4357 | allowed at the innermost level, e.g.: |
| 4358 | template <class T = int> |
| 4359 | struct S {}; |
| 4360 | but, in a partial specialization, they're not allowed even |
| 4361 | there, as we have in [temp.class.spec]: |
| 4362 | |
| 4363 | The template parameter list of a specialization shall not |
| 4364 | contain default template argument values. |
| 4365 | |
| 4366 | So, for a partial specialization, or for a function template |
| 4367 | (in C++98/C++03), we look at all of them. */ |
| 4368 | ; |
| 4369 | else |
| 4370 | /* But, for a primary class template that is not a partial |
| 4371 | specialization we look at all template parameters except the |
| 4372 | innermost ones. */ |
| 4373 | parms = TREE_CHAIN (parms); |
| 4374 | |
| 4375 | /* Figure out what error message to issue. */ |
| 4376 | if (is_friend_decl == 2) |
| 4377 | msg = G_("default template arguments may not be used in function template " |
| 4378 | "friend re-declaration"); |
| 4379 | else if (is_friend_decl) |
| 4380 | msg = G_("default template arguments may not be used in function template " |
| 4381 | "friend declarations"); |
| 4382 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
| 4383 | msg = G_("default template arguments may not be used in function templates " |
| 4384 | "without -std=c++11 or -std=gnu++11"); |
| 4385 | else if (is_partial) |
| 4386 | msg = G_("default template arguments may not be used in " |
| 4387 | "partial specializations"); |
| 4388 | else |
| 4389 | msg = G_("default argument for template parameter for class enclosing %qD"); |
| 4390 | |
| 4391 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) |
| 4392 | /* If we're inside a class definition, there's no need to |
| 4393 | examine the parameters to the class itself. On the one |
| 4394 | hand, they will be checked when the class is defined, and, |
| 4395 | on the other, default arguments are valid in things like: |
| 4396 | template <class T = double> |
| 4397 | struct S { template <class U> void f(U); }; |
| 4398 | Here the default argument for `S' has no bearing on the |
| 4399 | declaration of `f'. */ |
| 4400 | last_level_to_check = template_class_depth (current_class_type) + 1; |
| 4401 | else |
| 4402 | /* Check everything. */ |
| 4403 | last_level_to_check = 0; |
| 4404 | |
| 4405 | for (parm_level = parms; |
| 4406 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; |
| 4407 | parm_level = TREE_CHAIN (parm_level)) |
| 4408 | { |
| 4409 | tree inner_parms = TREE_VALUE (parm_level); |
| 4410 | int i; |
| 4411 | int ntparms; |
| 4412 | |
| 4413 | ntparms = TREE_VEC_LENGTH (inner_parms); |
| 4414 | for (i = 0; i < ntparms; ++i) |
| 4415 | { |
| 4416 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) |
| 4417 | continue; |
| 4418 | |
| 4419 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
| 4420 | { |
| 4421 | if (msg) |
| 4422 | { |
| 4423 | no_errors = false; |
| 4424 | if (is_friend_decl == 2) |
| 4425 | return no_errors; |
| 4426 | |
| 4427 | error (msg, decl); |
| 4428 | msg = 0; |
| 4429 | } |
| 4430 | |
| 4431 | /* Clear out the default argument so that we are not |
| 4432 | confused later. */ |
| 4433 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | /* At this point, if we're still interested in issuing messages, |
| 4438 | they must apply to classes surrounding the object declared. */ |
| 4439 | if (msg) |
| 4440 | msg = G_("default argument for template parameter for class " |
| 4441 | "enclosing %qD"); |
| 4442 | } |
| 4443 | |
| 4444 | return no_errors; |
| 4445 | } |
| 4446 | |
| 4447 | /* Worker for push_template_decl_real, called via |
| 4448 | for_each_template_parm. DATA is really an int, indicating the |
| 4449 | level of the parameters we are interested in. If T is a template |
| 4450 | parameter of that level, return nonzero. */ |
| 4451 | |
| 4452 | static int |
| 4453 | template_parm_this_level_p (tree t, void* data) |
| 4454 | { |
| 4455 | int this_level = *(int *)data; |
| 4456 | int level; |
| 4457 | |
| 4458 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) |
| 4459 | level = TEMPLATE_PARM_LEVEL (t); |
| 4460 | else |
| 4461 | level = TEMPLATE_TYPE_LEVEL (t); |
| 4462 | return level == this_level; |
| 4463 | } |
| 4464 | |
| 4465 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
| 4466 | parameters given by current_template_args, or reuses a |
| 4467 | previously existing one, if appropriate. Returns the DECL, or an |
| 4468 | equivalent one, if it is replaced via a call to duplicate_decls. |
| 4469 | |
| 4470 | If IS_FRIEND is true, DECL is a friend declaration. */ |
| 4471 | |
| 4472 | tree |
| 4473 | push_template_decl_real (tree decl, bool is_friend) |
| 4474 | { |
| 4475 | tree tmpl; |
| 4476 | tree args; |
| 4477 | tree info; |
| 4478 | tree ctx; |
| 4479 | int primary; |
| 4480 | int is_partial; |
| 4481 | int new_template_p = 0; |
| 4482 | /* True if the template is a member template, in the sense of |
| 4483 | [temp.mem]. */ |
| 4484 | bool member_template_p = false; |
| 4485 | |
| 4486 | if (decl == error_mark_node || !current_template_parms) |
| 4487 | return error_mark_node; |
| 4488 | |
| 4489 | /* See if this is a partial specialization. */ |
| 4490 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
| 4491 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
| 4492 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
| 4493 | |
| 4494 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
| 4495 | is_friend = true; |
| 4496 | |
| 4497 | if (is_friend) |
| 4498 | /* For a friend, we want the context of the friend function, not |
| 4499 | the type of which it is a friend. */ |
| 4500 | ctx = CP_DECL_CONTEXT (decl); |
| 4501 | else if (CP_DECL_CONTEXT (decl) |
| 4502 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) |
| 4503 | /* In the case of a virtual function, we want the class in which |
| 4504 | it is defined. */ |
| 4505 | ctx = CP_DECL_CONTEXT (decl); |
| 4506 | else |
| 4507 | /* Otherwise, if we're currently defining some class, the DECL |
| 4508 | is assumed to be a member of the class. */ |
| 4509 | ctx = current_scope (); |
| 4510 | |
| 4511 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
| 4512 | ctx = NULL_TREE; |
| 4513 | |
| 4514 | if (!DECL_CONTEXT (decl)) |
| 4515 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
| 4516 | |
| 4517 | /* See if this is a primary template. */ |
| 4518 | if (is_friend && ctx) |
| 4519 | /* A friend template that specifies a class context, i.e. |
| 4520 | template <typename T> friend void A<T>::f(); |
| 4521 | is not primary. */ |
| 4522 | primary = 0; |
| 4523 | else |
| 4524 | primary = template_parm_scope_p (); |
| 4525 | |
| 4526 | if (primary) |
| 4527 | { |
| 4528 | if (DECL_CLASS_SCOPE_P (decl)) |
| 4529 | member_template_p = true; |
| 4530 | if (TREE_CODE (decl) == TYPE_DECL |
| 4531 | && ANON_AGGRNAME_P (DECL_NAME (decl))) |
| 4532 | { |
| 4533 | error ("template class without a name"); |
| 4534 | return error_mark_node; |
| 4535 | } |
| 4536 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
| 4537 | { |
| 4538 | if (DECL_DESTRUCTOR_P (decl)) |
| 4539 | { |
| 4540 | /* [temp.mem] |
| 4541 | |
| 4542 | A destructor shall not be a member template. */ |
| 4543 | error ("destructor %qD declared as member template", decl); |
| 4544 | return error_mark_node; |
| 4545 | } |
| 4546 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) |
| 4547 | && (!prototype_p (TREE_TYPE (decl)) |
| 4548 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node |
| 4549 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) |
| 4550 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) |
| 4551 | == void_list_node))) |
| 4552 | { |
| 4553 | /* [basic.stc.dynamic.allocation] |
| 4554 | |
| 4555 | An allocation function can be a function |
| 4556 | template. ... Template allocation functions shall |
| 4557 | have two or more parameters. */ |
| 4558 | error ("invalid template declaration of %qD", decl); |
| 4559 | return error_mark_node; |
| 4560 | } |
| 4561 | } |
| 4562 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
| 4563 | && CLASS_TYPE_P (TREE_TYPE (decl))) |
| 4564 | /* OK */; |
| 4565 | else if (TREE_CODE (decl) == TYPE_DECL |
| 4566 | && TYPE_DECL_ALIAS_P (decl)) |
| 4567 | /* alias-declaration */ |
| 4568 | gcc_assert (!DECL_ARTIFICIAL (decl)); |
| 4569 | else |
| 4570 | { |
| 4571 | error ("template declaration of %q#D", decl); |
| 4572 | return error_mark_node; |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | /* Check to see that the rules regarding the use of default |
| 4577 | arguments are not being violated. */ |
| 4578 | check_default_tmpl_args (decl, current_template_parms, |
| 4579 | primary, is_partial, /*is_friend_decl=*/0); |
| 4580 | |
| 4581 | /* Ensure that there are no parameter packs in the type of this |
| 4582 | declaration that have not been expanded. */ |
| 4583 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 4584 | { |
| 4585 | /* Check each of the arguments individually to see if there are |
| 4586 | any bare parameter packs. */ |
| 4587 | tree type = TREE_TYPE (decl); |
| 4588 | tree arg = DECL_ARGUMENTS (decl); |
| 4589 | tree argtype = TYPE_ARG_TYPES (type); |
| 4590 | |
| 4591 | while (arg && argtype) |
| 4592 | { |
| 4593 | if (!FUNCTION_PARAMETER_PACK_P (arg) |
| 4594 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
| 4595 | { |
| 4596 | /* This is a PARM_DECL that contains unexpanded parameter |
| 4597 | packs. We have already complained about this in the |
| 4598 | check_for_bare_parameter_packs call, so just replace |
| 4599 | these types with ERROR_MARK_NODE. */ |
| 4600 | TREE_TYPE (arg) = error_mark_node; |
| 4601 | TREE_VALUE (argtype) = error_mark_node; |
| 4602 | } |
| 4603 | |
| 4604 | arg = DECL_CHAIN (arg); |
| 4605 | argtype = TREE_CHAIN (argtype); |
| 4606 | } |
| 4607 | |
| 4608 | /* Check for bare parameter packs in the return type and the |
| 4609 | exception specifiers. */ |
| 4610 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
| 4611 | /* Errors were already issued, set return type to int |
| 4612 | as the frontend doesn't expect error_mark_node as |
| 4613 | the return type. */ |
| 4614 | TREE_TYPE (type) = integer_type_node; |
| 4615 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
| 4616 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; |
| 4617 | } |
| 4618 | else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL |
| 4619 | && TYPE_DECL_ALIAS_P (decl)) |
| 4620 | ? DECL_ORIGINAL_TYPE (decl) |
| 4621 | : TREE_TYPE (decl))) |
| 4622 | { |
| 4623 | TREE_TYPE (decl) = error_mark_node; |
| 4624 | return error_mark_node; |
| 4625 | } |
| 4626 | |
| 4627 | if (is_partial) |
| 4628 | return process_partial_specialization (decl); |
| 4629 | |
| 4630 | args = current_template_args (); |
| 4631 | |
| 4632 | if (!ctx |
| 4633 | || TREE_CODE (ctx) == FUNCTION_DECL |
| 4634 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
| 4635 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
| 4636 | { |
| 4637 | if (DECL_LANG_SPECIFIC (decl) |
| 4638 | && DECL_TEMPLATE_INFO (decl) |
| 4639 | && DECL_TI_TEMPLATE (decl)) |
| 4640 | tmpl = DECL_TI_TEMPLATE (decl); |
| 4641 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
| 4642 | be DECL_LANG_SPECIFIC. The information equivalent to |
| 4643 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ |
| 4644 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
| 4645 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
| 4646 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) |
| 4647 | { |
| 4648 | /* Since a template declaration already existed for this |
| 4649 | class-type, we must be redeclaring it here. Make sure |
| 4650 | that the redeclaration is valid. */ |
| 4651 | redeclare_class_template (TREE_TYPE (decl), |
| 4652 | current_template_parms); |
| 4653 | /* We don't need to create a new TEMPLATE_DECL; just use the |
| 4654 | one we already had. */ |
| 4655 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); |
| 4656 | } |
| 4657 | else |
| 4658 | { |
| 4659 | tmpl = build_template_decl (decl, current_template_parms, |
| 4660 | member_template_p); |
| 4661 | new_template_p = 1; |
| 4662 | |
| 4663 | if (DECL_LANG_SPECIFIC (decl) |
| 4664 | && DECL_TEMPLATE_SPECIALIZATION (decl)) |
| 4665 | { |
| 4666 | /* A specialization of a member template of a template |
| 4667 | class. */ |
| 4668 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
| 4669 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); |
| 4670 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; |
| 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | else |
| 4675 | { |
| 4676 | tree a, t, current, parms; |
| 4677 | int i; |
| 4678 | tree tinfo = get_template_info (decl); |
| 4679 | |
| 4680 | if (!tinfo) |
| 4681 | { |
| 4682 | error ("template definition of non-template %q#D", decl); |
| 4683 | return error_mark_node; |
| 4684 | } |
| 4685 | |
| 4686 | tmpl = TI_TEMPLATE (tinfo); |
| 4687 | |
| 4688 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
| 4689 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
| 4690 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
| 4691 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
| 4692 | { |
| 4693 | tree new_tmpl; |
| 4694 | |
| 4695 | /* The declaration is a specialization of a member |
| 4696 | template, declared outside the class. Therefore, the |
| 4697 | innermost template arguments will be NULL, so we |
| 4698 | replace them with the arguments determined by the |
| 4699 | earlier call to check_explicit_specialization. */ |
| 4700 | args = DECL_TI_ARGS (decl); |
| 4701 | |
| 4702 | new_tmpl |
| 4703 | = build_template_decl (decl, current_template_parms, |
| 4704 | member_template_p); |
| 4705 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
| 4706 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); |
| 4707 | DECL_TI_TEMPLATE (decl) = new_tmpl; |
| 4708 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); |
| 4709 | DECL_TEMPLATE_INFO (new_tmpl) |
| 4710 | = build_template_info (tmpl, args); |
| 4711 | |
| 4712 | register_specialization (new_tmpl, |
| 4713 | most_general_template (tmpl), |
| 4714 | args, |
| 4715 | is_friend, 0); |
| 4716 | return decl; |
| 4717 | } |
| 4718 | |
| 4719 | /* Make sure the template headers we got make sense. */ |
| 4720 | |
| 4721 | parms = DECL_TEMPLATE_PARMS (tmpl); |
| 4722 | i = TMPL_PARMS_DEPTH (parms); |
| 4723 | if (TMPL_ARGS_DEPTH (args) != i) |
| 4724 | { |
| 4725 | error ("expected %d levels of template parms for %q#D, got %d", |
| 4726 | i, decl, TMPL_ARGS_DEPTH (args)); |
| 4727 | } |
| 4728 | else |
| 4729 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) |
| 4730 | { |
| 4731 | a = TMPL_ARGS_LEVEL (args, i); |
| 4732 | t = INNERMOST_TEMPLATE_PARMS (parms); |
| 4733 | |
| 4734 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) |
| 4735 | { |
| 4736 | if (current == decl) |
| 4737 | error ("got %d template parameters for %q#D", |
| 4738 | TREE_VEC_LENGTH (a), decl); |
| 4739 | else |
| 4740 | error ("got %d template parameters for %q#T", |
| 4741 | TREE_VEC_LENGTH (a), current); |
| 4742 | error (" but %d required", TREE_VEC_LENGTH (t)); |
| 4743 | /* Avoid crash in import_export_decl. */ |
| 4744 | DECL_INTERFACE_KNOWN (decl) = 1; |
| 4745 | return error_mark_node; |
| 4746 | } |
| 4747 | |
| 4748 | if (current == decl) |
| 4749 | current = ctx; |
| 4750 | else if (current == NULL_TREE) |
| 4751 | /* Can happen in erroneous input. */ |
| 4752 | break; |
| 4753 | else |
| 4754 | current = (TYPE_P (current) |
| 4755 | ? TYPE_CONTEXT (current) |
| 4756 | : DECL_CONTEXT (current)); |
| 4757 | } |
| 4758 | |
| 4759 | /* Check that the parms are used in the appropriate qualifying scopes |
| 4760 | in the declarator. */ |
| 4761 | if (!comp_template_args |
| 4762 | (TI_ARGS (tinfo), |
| 4763 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) |
| 4764 | { |
| 4765 | error ("\ |
| 4766 | template arguments to %qD do not match original template %qD", |
| 4767 | decl, DECL_TEMPLATE_RESULT (tmpl)); |
| 4768 | if (!uses_template_parms (TI_ARGS (tinfo))) |
| 4769 | inform (input_location, "use template<> for an explicit specialization"); |
| 4770 | /* Avoid crash in import_export_decl. */ |
| 4771 | DECL_INTERFACE_KNOWN (decl) = 1; |
| 4772 | return error_mark_node; |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
| 4777 | TREE_TYPE (tmpl) = TREE_TYPE (decl); |
| 4778 | |
| 4779 | /* Push template declarations for global functions and types. Note |
| 4780 | that we do not try to push a global template friend declared in a |
| 4781 | template class; such a thing may well depend on the template |
| 4782 | parameters of the class. */ |
| 4783 | if (new_template_p && !ctx |
| 4784 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
| 4785 | { |
| 4786 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
| 4787 | if (tmpl == error_mark_node) |
| 4788 | return error_mark_node; |
| 4789 | |
| 4790 | /* Hide template friend classes that haven't been declared yet. */ |
| 4791 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) |
| 4792 | { |
| 4793 | DECL_ANTICIPATED (tmpl) = 1; |
| 4794 | DECL_FRIEND_P (tmpl) = 1; |
| 4795 | } |
| 4796 | } |
| 4797 | |
| 4798 | if (primary) |
| 4799 | { |
| 4800 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
| 4801 | int i; |
| 4802 | |
| 4803 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
| 4804 | if (DECL_CONV_FN_P (tmpl)) |
| 4805 | { |
| 4806 | int depth = TMPL_PARMS_DEPTH (parms); |
| 4807 | |
| 4808 | /* It is a conversion operator. See if the type converted to |
| 4809 | depends on innermost template operands. */ |
| 4810 | |
| 4811 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
| 4812 | depth)) |
| 4813 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
| 4814 | } |
| 4815 | |
| 4816 | /* Give template template parms a DECL_CONTEXT of the template |
| 4817 | for which they are a parameter. */ |
| 4818 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
| 4819 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) |
| 4820 | { |
| 4821 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
| 4822 | if (TREE_CODE (parm) == TEMPLATE_DECL) |
| 4823 | DECL_CONTEXT (parm) = tmpl; |
| 4824 | } |
| 4825 | } |
| 4826 | |
| 4827 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
| 4828 | back to its most general template. If TMPL is a specialization, |
| 4829 | ARGS may only have the innermost set of arguments. Add the missing |
| 4830 | argument levels if necessary. */ |
| 4831 | if (DECL_TEMPLATE_INFO (tmpl)) |
| 4832 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); |
| 4833 | |
| 4834 | info = build_template_info (tmpl, args); |
| 4835 | |
| 4836 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
| 4837 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
| 4838 | else |
| 4839 | { |
| 4840 | if (primary && !DECL_LANG_SPECIFIC (decl)) |
| 4841 | retrofit_lang_decl (decl); |
| 4842 | if (DECL_LANG_SPECIFIC (decl)) |
| 4843 | DECL_TEMPLATE_INFO (decl) = info; |
| 4844 | } |
| 4845 | |
| 4846 | return DECL_TEMPLATE_RESULT (tmpl); |
| 4847 | } |
| 4848 | |
| 4849 | tree |
| 4850 | push_template_decl (tree decl) |
| 4851 | { |
| 4852 | return push_template_decl_real (decl, false); |
| 4853 | } |
| 4854 | |
| 4855 | /* Called when a class template TYPE is redeclared with the indicated |
| 4856 | template PARMS, e.g.: |
| 4857 | |
| 4858 | template <class T> struct S; |
| 4859 | template <class T> struct S {}; */ |
| 4860 | |
| 4861 | bool |
| 4862 | redeclare_class_template (tree type, tree parms) |
| 4863 | { |
| 4864 | tree tmpl; |
| 4865 | tree tmpl_parms; |
| 4866 | int i; |
| 4867 | |
| 4868 | if (!TYPE_TEMPLATE_INFO (type)) |
| 4869 | { |
| 4870 | error ("%qT is not a template type", type); |
| 4871 | return false; |
| 4872 | } |
| 4873 | |
| 4874 | tmpl = TYPE_TI_TEMPLATE (type); |
| 4875 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
| 4876 | /* The type is nested in some template class. Nothing to worry |
| 4877 | about here; there are no new template parameters for the nested |
| 4878 | type. */ |
| 4879 | return true; |
| 4880 | |
| 4881 | if (!parms) |
| 4882 | { |
| 4883 | error ("template specifiers not specified in declaration of %qD", |
| 4884 | tmpl); |
| 4885 | return false; |
| 4886 | } |
| 4887 | |
| 4888 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
| 4889 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); |
| 4890 | |
| 4891 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
| 4892 | { |
| 4893 | error_n (input_location, TREE_VEC_LENGTH (parms), |
| 4894 | "redeclared with %d template parameter", |
| 4895 | "redeclared with %d template parameters", |
| 4896 | TREE_VEC_LENGTH (parms)); |
| 4897 | inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms), |
| 4898 | "previous declaration %q+D used %d template parameter", |
| 4899 | "previous declaration %q+D used %d template parameters", |
| 4900 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); |
| 4901 | return false; |
| 4902 | } |
| 4903 | |
| 4904 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) |
| 4905 | { |
| 4906 | tree tmpl_parm; |
| 4907 | tree parm; |
| 4908 | tree tmpl_default; |
| 4909 | tree parm_default; |
| 4910 | |
| 4911 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node |
| 4912 | || TREE_VEC_ELT (parms, i) == error_mark_node) |
| 4913 | continue; |
| 4914 | |
| 4915 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); |
| 4916 | if (tmpl_parm == error_mark_node) |
| 4917 | return false; |
| 4918 | |
| 4919 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
| 4920 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); |
| 4921 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); |
| 4922 | |
| 4923 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
| 4924 | TEMPLATE_DECL. */ |
| 4925 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
| 4926 | || (TREE_CODE (tmpl_parm) != TYPE_DECL |
| 4927 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) |
| 4928 | || (TREE_CODE (tmpl_parm) != PARM_DECL |
| 4929 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) |
| 4930 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) |
| 4931 | || (TREE_CODE (tmpl_parm) == PARM_DECL |
| 4932 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) |
| 4933 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))) |
| 4934 | { |
| 4935 | error ("template parameter %q+#D", tmpl_parm); |
| 4936 | error ("redeclared here as %q#D", parm); |
| 4937 | return false; |
| 4938 | } |
| 4939 | |
| 4940 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) |
| 4941 | { |
| 4942 | /* We have in [temp.param]: |
| 4943 | |
| 4944 | A template-parameter may not be given default arguments |
| 4945 | by two different declarations in the same scope. */ |
| 4946 | error_at (input_location, "redefinition of default argument for %q#D", parm); |
| 4947 | inform (DECL_SOURCE_LOCATION (tmpl_parm), |
| 4948 | "original definition appeared here"); |
| 4949 | return false; |
| 4950 | } |
| 4951 | |
| 4952 | if (parm_default != NULL_TREE) |
| 4953 | /* Update the previous template parameters (which are the ones |
| 4954 | that will really count) with the new default value. */ |
| 4955 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; |
| 4956 | else if (tmpl_default != NULL_TREE) |
| 4957 | /* Update the new parameters, too; they'll be used as the |
| 4958 | parameters for any members. */ |
| 4959 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; |
| 4960 | } |
| 4961 | |
| 4962 | return true; |
| 4963 | } |
| 4964 | |
| 4965 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
| 4966 | (possibly simplified) expression. */ |
| 4967 | |
| 4968 | static tree |
| 4969 | fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain) |
| 4970 | { |
| 4971 | if (expr == NULL_TREE) |
| 4972 | return NULL_TREE; |
| 4973 | |
| 4974 | /* If we're in a template, but EXPR isn't value dependent, simplify |
| 4975 | it. We're supposed to treat: |
| 4976 | |
| 4977 | template <typename T> void f(T[1 + 1]); |
| 4978 | template <typename T> void f(T[2]); |
| 4979 | |
| 4980 | as two declarations of the same function, for example. */ |
| 4981 | if (processing_template_decl |
| 4982 | && !type_dependent_expression_p (expr) |
| 4983 | && potential_constant_expression (expr) |
| 4984 | && !value_dependent_expression_p (expr)) |
| 4985 | { |
| 4986 | HOST_WIDE_INT saved_processing_template_decl; |
| 4987 | |
| 4988 | saved_processing_template_decl = processing_template_decl; |
| 4989 | processing_template_decl = 0; |
| 4990 | expr = tsubst_copy_and_build (expr, |
| 4991 | /*args=*/NULL_TREE, |
| 4992 | complain, |
| 4993 | /*in_decl=*/NULL_TREE, |
| 4994 | /*function_p=*/false, |
| 4995 | /*integral_constant_expression_p=*/true); |
| 4996 | processing_template_decl = saved_processing_template_decl; |
| 4997 | } |
| 4998 | return expr; |
| 4999 | } |
| 5000 | |
| 5001 | tree |
| 5002 | fold_non_dependent_expr (tree expr) |
| 5003 | { |
| 5004 | return fold_non_dependent_expr_sfinae (expr, tf_error); |
| 5005 | } |
| 5006 | |
| 5007 | /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias |
| 5008 | template declaration, or a TYPE_DECL for an alias declaration. */ |
| 5009 | |
| 5010 | bool |
| 5011 | alias_type_or_template_p (tree t) |
| 5012 | { |
| 5013 | if (t == NULL_TREE) |
| 5014 | return false; |
| 5015 | return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t)) |
| 5016 | || (TYPE_P (t) |
| 5017 | && TYPE_NAME (t) |
| 5018 | && TYPE_DECL_ALIAS_P (TYPE_NAME (t))) |
| 5019 | || DECL_ALIAS_TEMPLATE_P (t)); |
| 5020 | } |
| 5021 | |
| 5022 | /* Return TRUE iff is a specialization of an alias template. */ |
| 5023 | |
| 5024 | bool |
| 5025 | alias_template_specialization_p (tree t) |
| 5026 | { |
| 5027 | if (t == NULL_TREE) |
| 5028 | return false; |
| 5029 | return (primary_template_instantiation_p (t) |
| 5030 | && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t))); |
| 5031 | } |
| 5032 | |
| 5033 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which |
| 5034 | must be a function or a pointer-to-function type, as specified |
| 5035 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, |
| 5036 | and check that the resulting function has external linkage. */ |
| 5037 | |
| 5038 | static tree |
| 5039 | convert_nontype_argument_function (tree type, tree expr) |
| 5040 | { |
| 5041 | tree fns = expr; |
| 5042 | tree fn, fn_no_ptr; |
| 5043 | linkage_kind linkage; |
| 5044 | |
| 5045 | fn = instantiate_type (type, fns, tf_none); |
| 5046 | if (fn == error_mark_node) |
| 5047 | return error_mark_node; |
| 5048 | |
| 5049 | fn_no_ptr = fn; |
| 5050 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) |
| 5051 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); |
| 5052 | if (BASELINK_P (fn_no_ptr)) |
| 5053 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); |
| 5054 | |
| 5055 | /* [temp.arg.nontype]/1 |
| 5056 | |
| 5057 | A template-argument for a non-type, non-template template-parameter |
| 5058 | shall be one of: |
| 5059 | [...] |
| 5060 | -- the address of an object or function with external [C++11: or |
| 5061 | internal] linkage. */ |
| 5062 | linkage = decl_linkage (fn_no_ptr); |
| 5063 | if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external) |
| 5064 | { |
| 5065 | if (cxx_dialect >= cxx0x) |
| 5066 | error ("%qE is not a valid template argument for type %qT " |
| 5067 | "because %qD has no linkage", |
| 5068 | expr, type, fn_no_ptr); |
| 5069 | else |
| 5070 | error ("%qE is not a valid template argument for type %qT " |
| 5071 | "because %qD does not have external linkage", |
| 5072 | expr, type, fn_no_ptr); |
| 5073 | return NULL_TREE; |
| 5074 | } |
| 5075 | |
| 5076 | return fn; |
| 5077 | } |
| 5078 | |
| 5079 | /* Subroutine of convert_nontype_argument. |
| 5080 | Check if EXPR of type TYPE is a valid pointer-to-member constant. |
| 5081 | Emit an error otherwise. */ |
| 5082 | |
| 5083 | static bool |
| 5084 | check_valid_ptrmem_cst_expr (tree type, tree expr, |
| 5085 | tsubst_flags_t complain) |
| 5086 | { |
| 5087 | STRIP_NOPS (expr); |
| 5088 | if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST)) |
| 5089 | return true; |
| 5090 | if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr)) |
| 5091 | return true; |
| 5092 | if (complain & tf_error) |
| 5093 | { |
| 5094 | error ("%qE is not a valid template argument for type %qT", |
| 5095 | expr, type); |
| 5096 | error ("it must be a pointer-to-member of the form %<&X::Y%>"); |
| 5097 | } |
| 5098 | return false; |
| 5099 | } |
| 5100 | |
| 5101 | /* Returns TRUE iff the address of OP is value-dependent. |
| 5102 | |
| 5103 | 14.6.2.4 [temp.dep.temp]: |
| 5104 | A non-integral non-type template-argument is dependent if its type is |
| 5105 | dependent or it has either of the following forms |
| 5106 | qualified-id |
| 5107 | & qualified-id |
| 5108 | and contains a nested-name-specifier which specifies a class-name that |
| 5109 | names a dependent type. |
| 5110 | |
| 5111 | We generalize this to just say that the address of a member of a |
| 5112 | dependent class is value-dependent; the above doesn't cover the |
| 5113 | address of a static data member named with an unqualified-id. */ |
| 5114 | |
| 5115 | static bool |
| 5116 | has_value_dependent_address (tree op) |
| 5117 | { |
| 5118 | /* We could use get_inner_reference here, but there's no need; |
| 5119 | this is only relevant for template non-type arguments, which |
| 5120 | can only be expressed as &id-expression. */ |
| 5121 | if (DECL_P (op)) |
| 5122 | { |
| 5123 | tree ctx = CP_DECL_CONTEXT (op); |
| 5124 | if (TYPE_P (ctx) && dependent_type_p (ctx)) |
| 5125 | return true; |
| 5126 | } |
| 5127 | |
| 5128 | return false; |
| 5129 | } |
| 5130 | |
| 5131 | /* The next set of functions are used for providing helpful explanatory |
| 5132 | diagnostics for failed overload resolution. Their messages should be |
| 5133 | indented by two spaces for consistency with the messages in |
| 5134 | call.c */ |
| 5135 | |
| 5136 | static int |
| 5137 | unify_success (bool explain_p ATTRIBUTE_UNUSED) |
| 5138 | { |
| 5139 | return 0; |
| 5140 | } |
| 5141 | |
| 5142 | static int |
| 5143 | unify_parameter_deduction_failure (bool explain_p, tree parm) |
| 5144 | { |
| 5145 | if (explain_p) |
| 5146 | inform (input_location, |
| 5147 | " couldn't deduce template parameter %qD", parm); |
| 5148 | return 1; |
| 5149 | } |
| 5150 | |
| 5151 | static int |
| 5152 | unify_invalid (bool explain_p ATTRIBUTE_UNUSED) |
| 5153 | { |
| 5154 | return 1; |
| 5155 | } |
| 5156 | |
| 5157 | static int |
| 5158 | unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg) |
| 5159 | { |
| 5160 | if (explain_p) |
| 5161 | inform (input_location, |
| 5162 | " types %qT and %qT have incompatible cv-qualifiers", |
| 5163 | parm, arg); |
| 5164 | return 1; |
| 5165 | } |
| 5166 | |
| 5167 | static int |
| 5168 | unify_type_mismatch (bool explain_p, tree parm, tree arg) |
| 5169 | { |
| 5170 | if (explain_p) |
| 5171 | inform (input_location, " mismatched types %qT and %qT", parm, arg); |
| 5172 | return 1; |
| 5173 | } |
| 5174 | |
| 5175 | static int |
| 5176 | unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg) |
| 5177 | { |
| 5178 | if (explain_p) |
| 5179 | inform (input_location, |
| 5180 | " template parameter %qD is not a parameter pack, but " |
| 5181 | "argument %qD is", |
| 5182 | parm, arg); |
| 5183 | return 1; |
| 5184 | } |
| 5185 | |
| 5186 | static int |
| 5187 | unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg) |
| 5188 | { |
| 5189 | if (explain_p) |
| 5190 | inform (input_location, |
| 5191 | " template argument %qE does not match " |
| 5192 | "pointer-to-member constant %qE", |
| 5193 | arg, parm); |
| 5194 | return 1; |
| 5195 | } |
| 5196 | |
| 5197 | static int |
| 5198 | unify_expression_unequal (bool explain_p, tree parm, tree arg) |
| 5199 | { |
| 5200 | if (explain_p) |
| 5201 | inform (input_location, " %qE is not equivalent to %qE", parm, arg); |
| 5202 | return 1; |
| 5203 | } |
| 5204 | |
| 5205 | static int |
| 5206 | unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg) |
| 5207 | { |
| 5208 | if (explain_p) |
| 5209 | inform (input_location, |
| 5210 | " inconsistent parameter pack deduction with %qT and %qT", |
| 5211 | old_arg, new_arg); |
| 5212 | return 1; |
| 5213 | } |
| 5214 | |
| 5215 | static int |
| 5216 | unify_inconsistency (bool explain_p, tree parm, tree first, tree second) |
| 5217 | { |
| 5218 | if (explain_p) |
| 5219 | { |
| 5220 | if (TYPE_P (parm)) |
| 5221 | inform (input_location, |
| 5222 | " deduced conflicting types for parameter %qT (%qT and %qT)", |
| 5223 | parm, first, second); |
| 5224 | else |
| 5225 | inform (input_location, |
| 5226 | " deduced conflicting values for non-type parameter " |
| 5227 | "%qE (%qE and %qE)", parm, first, second); |
| 5228 | } |
| 5229 | return 1; |
| 5230 | } |
| 5231 | |
| 5232 | static int |
| 5233 | unify_vla_arg (bool explain_p, tree arg) |
| 5234 | { |
| 5235 | if (explain_p) |
| 5236 | inform (input_location, |
| 5237 | " variable-sized array type %qT is not " |
| 5238 | "a valid template argument", |
| 5239 | arg); |
| 5240 | return 1; |
| 5241 | } |
| 5242 | |
| 5243 | static int |
| 5244 | unify_method_type_error (bool explain_p, tree arg) |
| 5245 | { |
| 5246 | if (explain_p) |
| 5247 | inform (input_location, |
| 5248 | " member function type %qT is not a valid template argument", |
| 5249 | arg); |
| 5250 | return 1; |
| 5251 | } |
| 5252 | |
| 5253 | static int |
| 5254 | unify_arity (bool explain_p, int have, int wanted) |
| 5255 | { |
| 5256 | if (explain_p) |
| 5257 | inform_n (input_location, wanted, |
| 5258 | " candidate expects %d argument, %d provided", |
| 5259 | " candidate expects %d arguments, %d provided", |
| 5260 | wanted, have); |
| 5261 | return 1; |
| 5262 | } |
| 5263 | |
| 5264 | static int |
| 5265 | unify_too_many_arguments (bool explain_p, int have, int wanted) |
| 5266 | { |
| 5267 | return unify_arity (explain_p, have, wanted); |
| 5268 | } |
| 5269 | |
| 5270 | static int |
| 5271 | unify_too_few_arguments (bool explain_p, int have, int wanted) |
| 5272 | { |
| 5273 | return unify_arity (explain_p, have, wanted); |
| 5274 | } |
| 5275 | |
| 5276 | static int |
| 5277 | unify_arg_conversion (bool explain_p, tree to_type, |
| 5278 | tree from_type, tree arg) |
| 5279 | { |
| 5280 | if (explain_p) |
| 5281 | inform (input_location, " cannot convert %qE (type %qT) to type %qT", |
| 5282 | arg, from_type, to_type); |
| 5283 | return 1; |
| 5284 | } |
| 5285 | |
| 5286 | static int |
| 5287 | unify_no_common_base (bool explain_p, enum template_base_result r, |
| 5288 | tree parm, tree arg) |
| 5289 | { |
| 5290 | if (explain_p) |
| 5291 | switch (r) |
| 5292 | { |
| 5293 | case tbr_ambiguous_baseclass: |
| 5294 | inform (input_location, " %qT is an ambiguous base class of %qT", |
| 5295 | arg, parm); |
| 5296 | break; |
| 5297 | default: |
| 5298 | inform (input_location, " %qT is not derived from %qT", arg, parm); |
| 5299 | break; |
| 5300 | } |
| 5301 | return 1; |
| 5302 | } |
| 5303 | |
| 5304 | static int |
| 5305 | unify_inconsistent_template_template_parameters (bool explain_p) |
| 5306 | { |
| 5307 | if (explain_p) |
| 5308 | inform (input_location, |
| 5309 | " template parameters of a template template argument are " |
| 5310 | "inconsistent with other deduced template arguments"); |
| 5311 | return 1; |
| 5312 | } |
| 5313 | |
| 5314 | static int |
| 5315 | unify_template_deduction_failure (bool explain_p, tree parm, tree arg) |
| 5316 | { |
| 5317 | if (explain_p) |
| 5318 | inform (input_location, |
| 5319 | " can't deduce a template for %qT from non-template type %qT", |
| 5320 | parm, arg); |
| 5321 | return 1; |
| 5322 | } |
| 5323 | |
| 5324 | static int |
| 5325 | unify_template_argument_mismatch (bool explain_p, tree parm, tree arg) |
| 5326 | { |
| 5327 | if (explain_p) |
| 5328 | inform (input_location, |
| 5329 | " template argument %qE does not match %qD", arg, parm); |
| 5330 | return 1; |
| 5331 | } |
| 5332 | |
| 5333 | static int |
| 5334 | unify_overload_resolution_failure (bool explain_p, tree arg) |
| 5335 | { |
| 5336 | if (explain_p) |
| 5337 | inform (input_location, |
| 5338 | " could not resolve address from overloaded function %qE", |
| 5339 | arg); |
| 5340 | return 1; |
| 5341 | } |
| 5342 | |
| 5343 | /* Attempt to convert the non-type template parameter EXPR to the |
| 5344 | indicated TYPE. If the conversion is successful, return the |
| 5345 | converted value. If the conversion is unsuccessful, return |
| 5346 | NULL_TREE if we issued an error message, or error_mark_node if we |
| 5347 | did not. We issue error messages for out-and-out bad template |
| 5348 | parameters, but not simply because the conversion failed, since we |
| 5349 | might be just trying to do argument deduction. Both TYPE and EXPR |
| 5350 | must be non-dependent. |
| 5351 | |
| 5352 | The conversion follows the special rules described in |
| 5353 | [temp.arg.nontype], and it is much more strict than an implicit |
| 5354 | conversion. |
| 5355 | |
| 5356 | This function is called twice for each template argument (see |
| 5357 | lookup_template_class for a more accurate description of this |
| 5358 | problem). This means that we need to handle expressions which |
| 5359 | are not valid in a C++ source, but can be created from the |
| 5360 | first call (for instance, casts to perform conversions). These |
| 5361 | hacks can go away after we fix the double coercion problem. */ |
| 5362 | |
| 5363 | static tree |
| 5364 | convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) |
| 5365 | { |
| 5366 | tree expr_type; |
| 5367 | |
| 5368 | /* Detect immediately string literals as invalid non-type argument. |
| 5369 | This special-case is not needed for correctness (we would easily |
| 5370 | catch this later), but only to provide better diagnostic for this |
| 5371 | common user mistake. As suggested by DR 100, we do not mention |
| 5372 | linkage issues in the diagnostic as this is not the point. */ |
| 5373 | /* FIXME we're making this OK. */ |
| 5374 | if (TREE_CODE (expr) == STRING_CST) |
| 5375 | { |
| 5376 | if (complain & tf_error) |
| 5377 | error ("%qE is not a valid template argument for type %qT " |
| 5378 | "because string literals can never be used in this context", |
| 5379 | expr, type); |
| 5380 | return NULL_TREE; |
| 5381 | } |
| 5382 | |
| 5383 | /* Add the ADDR_EXPR now for the benefit of |
| 5384 | value_dependent_expression_p. */ |
| 5385 | if (TYPE_PTROBV_P (type) |
| 5386 | && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE) |
| 5387 | expr = decay_conversion (expr); |
| 5388 | |
| 5389 | /* If we are in a template, EXPR may be non-dependent, but still |
| 5390 | have a syntactic, rather than semantic, form. For example, EXPR |
| 5391 | might be a SCOPE_REF, rather than the VAR_DECL to which the |
| 5392 | SCOPE_REF refers. Preserving the qualifying scope is necessary |
| 5393 | so that access checking can be performed when the template is |
| 5394 | instantiated -- but here we need the resolved form so that we can |
| 5395 | convert the argument. */ |
| 5396 | if (TYPE_REF_OBJ_P (type) |
| 5397 | && has_value_dependent_address (expr)) |
| 5398 | /* If we want the address and it's value-dependent, don't fold. */; |
| 5399 | else if (!type_unknown_p (expr)) |
| 5400 | expr = fold_non_dependent_expr_sfinae (expr, complain); |
| 5401 | if (error_operand_p (expr)) |
| 5402 | return error_mark_node; |
| 5403 | expr_type = TREE_TYPE (expr); |
| 5404 | if (TREE_CODE (type) == REFERENCE_TYPE) |
| 5405 | expr = mark_lvalue_use (expr); |
| 5406 | else |
| 5407 | expr = mark_rvalue_use (expr); |
| 5408 | |
| 5409 | /* 14.3.2/5: The null pointer{,-to-member} conversion is applied |
| 5410 | to a non-type argument of "nullptr". */ |
| 5411 | if (expr == nullptr_node |
| 5412 | && (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))) |
| 5413 | expr = convert (type, expr); |
| 5414 | |
| 5415 | /* In C++11, integral or enumeration non-type template arguments can be |
| 5416 | arbitrary constant expressions. Pointer and pointer to |
| 5417 | member arguments can be general constant expressions that evaluate |
| 5418 | to a null value, but otherwise still need to be of a specific form. */ |
| 5419 | if (cxx_dialect >= cxx0x) |
| 5420 | { |
| 5421 | if (TREE_CODE (expr) == PTRMEM_CST) |
| 5422 | /* A PTRMEM_CST is already constant, and a valid template |
| 5423 | argument for a parameter of pointer to member type, we just want |
| 5424 | to leave it in that form rather than lower it to a |
| 5425 | CONSTRUCTOR. */; |
| 5426 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
| 5427 | expr = maybe_constant_value (expr); |
| 5428 | else if (TYPE_PTR_P (type) |
| 5429 | || TYPE_PTR_TO_MEMBER_P (type)) |
| 5430 | { |
| 5431 | tree folded = maybe_constant_value (expr); |
| 5432 | if (TYPE_PTR_P (type) ? integer_zerop (folded) |
| 5433 | : null_member_pointer_value_p (folded)) |
| 5434 | expr = folded; |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | /* HACK: Due to double coercion, we can get a |
| 5439 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, |
| 5440 | which is the tree that we built on the first call (see |
| 5441 | below when coercing to reference to object or to reference to |
| 5442 | function). We just strip everything and get to the arg. |
| 5443 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C |
| 5444 | for examples. */ |
| 5445 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
| 5446 | { |
| 5447 | tree probe_type, probe = expr; |
| 5448 | if (REFERENCE_REF_P (probe)) |
| 5449 | probe = TREE_OPERAND (probe, 0); |
| 5450 | probe_type = TREE_TYPE (probe); |
| 5451 | if (TREE_CODE (probe) == NOP_EXPR) |
| 5452 | { |
| 5453 | /* ??? Maybe we could use convert_from_reference here, but we |
| 5454 | would need to relax its constraints because the NOP_EXPR |
| 5455 | could actually change the type to something more cv-qualified, |
| 5456 | and this is not folded by convert_from_reference. */ |
| 5457 | tree addr = TREE_OPERAND (probe, 0); |
| 5458 | gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE); |
| 5459 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); |
| 5460 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); |
| 5461 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| 5462 | (TREE_TYPE (probe_type), |
| 5463 | TREE_TYPE (TREE_TYPE (addr)))); |
| 5464 | |
| 5465 | expr = TREE_OPERAND (addr, 0); |
| 5466 | expr_type = TREE_TYPE (expr); |
| 5467 | } |
| 5468 | } |
| 5469 | |
| 5470 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
| 5471 | parameter is a pointer to object, through decay and |
| 5472 | qualification conversion. Let's strip everything. */ |
| 5473 | else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type)) |
| 5474 | { |
| 5475 | STRIP_NOPS (expr); |
| 5476 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); |
| 5477 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); |
| 5478 | /* Skip the ADDR_EXPR only if it is part of the decay for |
| 5479 | an array. Otherwise, it is part of the original argument |
| 5480 | in the source code. */ |
| 5481 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) |
| 5482 | expr = TREE_OPERAND (expr, 0); |
| 5483 | expr_type = TREE_TYPE (expr); |
| 5484 | } |
| 5485 | |
| 5486 | /* [temp.arg.nontype]/5, bullet 1 |
| 5487 | |
| 5488 | For a non-type template-parameter of integral or enumeration type, |
| 5489 | integral promotions (_conv.prom_) and integral conversions |
| 5490 | (_conv.integral_) are applied. */ |
| 5491 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
| 5492 | { |
| 5493 | tree t = build_integral_nontype_arg_conv (type, expr, complain); |
| 5494 | t = maybe_constant_value (t); |
| 5495 | if (t != error_mark_node) |
| 5496 | expr = t; |
| 5497 | |
| 5498 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) |
| 5499 | return error_mark_node; |
| 5500 | |
| 5501 | /* Notice that there are constant expressions like '4 % 0' which |
| 5502 | do not fold into integer constants. */ |
| 5503 | if (TREE_CODE (expr) != INTEGER_CST) |
| 5504 | { |
| 5505 | if (complain & tf_error) |
| 5506 | { |
| 5507 | int errs = errorcount, warns = warningcount; |
| 5508 | if (processing_template_decl |
| 5509 | && !require_potential_constant_expression (expr)) |
| 5510 | return NULL_TREE; |
| 5511 | expr = cxx_constant_value (expr); |
| 5512 | if (errorcount > errs || warningcount > warns) |
| 5513 | inform (EXPR_LOC_OR_HERE (expr), |
| 5514 | "in template argument for type %qT ", type); |
| 5515 | if (expr == error_mark_node) |
| 5516 | return NULL_TREE; |
| 5517 | /* else cxx_constant_value complained but gave us |
| 5518 | a real constant, so go ahead. */ |
| 5519 | gcc_assert (TREE_CODE (expr) == INTEGER_CST); |
| 5520 | } |
| 5521 | else |
| 5522 | return NULL_TREE; |
| 5523 | } |
| 5524 | |
| 5525 | /* Avoid typedef problems. */ |
| 5526 | if (TREE_TYPE (expr) != type) |
| 5527 | expr = fold_convert (type, expr); |
| 5528 | } |
| 5529 | /* [temp.arg.nontype]/5, bullet 2 |
| 5530 | |
| 5531 | For a non-type template-parameter of type pointer to object, |
| 5532 | qualification conversions (_conv.qual_) and the array-to-pointer |
| 5533 | conversion (_conv.array_) are applied. */ |
| 5534 | else if (TYPE_PTROBV_P (type)) |
| 5535 | { |
| 5536 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): |
| 5537 | |
| 5538 | A template-argument for a non-type, non-template template-parameter |
| 5539 | shall be one of: [...] |
| 5540 | |
| 5541 | -- the name of a non-type template-parameter; |
| 5542 | -- the address of an object or function with external linkage, [...] |
| 5543 | expressed as "& id-expression" where the & is optional if the name |
| 5544 | refers to a function or array, or if the corresponding |
| 5545 | template-parameter is a reference. |
| 5546 | |
| 5547 | Here, we do not care about functions, as they are invalid anyway |
| 5548 | for a parameter of type pointer-to-object. */ |
| 5549 | |
| 5550 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) |
| 5551 | /* Non-type template parameters are OK. */ |
| 5552 | ; |
| 5553 | else if (cxx_dialect >= cxx0x && integer_zerop (expr)) |
| 5554 | /* Null pointer values are OK in C++11. */; |
| 5555 | else if (TREE_CODE (expr) != ADDR_EXPR |
| 5556 | && TREE_CODE (expr_type) != ARRAY_TYPE) |
| 5557 | { |
| 5558 | if (TREE_CODE (expr) == VAR_DECL) |
| 5559 | { |
| 5560 | error ("%qD is not a valid template argument " |
| 5561 | "because %qD is a variable, not the address of " |
| 5562 | "a variable", |
| 5563 | expr, expr); |
| 5564 | return NULL_TREE; |
| 5565 | } |
| 5566 | /* Other values, like integer constants, might be valid |
| 5567 | non-type arguments of some other type. */ |
| 5568 | return error_mark_node; |
| 5569 | } |
| 5570 | else |
| 5571 | { |
| 5572 | tree decl; |
| 5573 | |
| 5574 | decl = ((TREE_CODE (expr) == ADDR_EXPR) |
| 5575 | ? TREE_OPERAND (expr, 0) : expr); |
| 5576 | if (TREE_CODE (decl) != VAR_DECL) |
| 5577 | { |
| 5578 | error ("%qE is not a valid template argument of type %qT " |
| 5579 | "because %qE is not a variable", |
| 5580 | expr, type, decl); |
| 5581 | return NULL_TREE; |
| 5582 | } |
| 5583 | else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl)) |
| 5584 | { |
| 5585 | error ("%qE is not a valid template argument of type %qT " |
| 5586 | "because %qD does not have external linkage", |
| 5587 | expr, type, decl); |
| 5588 | return NULL_TREE; |
| 5589 | } |
| 5590 | else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none) |
| 5591 | { |
| 5592 | error ("%qE is not a valid template argument of type %qT " |
| 5593 | "because %qD has no linkage", |
| 5594 | expr, type, decl); |
| 5595 | return NULL_TREE; |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | expr = decay_conversion (expr); |
| 5600 | if (expr == error_mark_node) |
| 5601 | return error_mark_node; |
| 5602 | |
| 5603 | expr = perform_qualification_conversions (type, expr); |
| 5604 | if (expr == error_mark_node) |
| 5605 | return error_mark_node; |
| 5606 | } |
| 5607 | /* [temp.arg.nontype]/5, bullet 3 |
| 5608 | |
| 5609 | For a non-type template-parameter of type reference to object, no |
| 5610 | conversions apply. The type referred to by the reference may be more |
| 5611 | cv-qualified than the (otherwise identical) type of the |
| 5612 | template-argument. The template-parameter is bound directly to the |
| 5613 | template-argument, which must be an lvalue. */ |
| 5614 | else if (TYPE_REF_OBJ_P (type)) |
| 5615 | { |
| 5616 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), |
| 5617 | expr_type)) |
| 5618 | return error_mark_node; |
| 5619 | |
| 5620 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
| 5621 | { |
| 5622 | error ("%qE is not a valid template argument for type %qT " |
| 5623 | "because of conflicts in cv-qualification", expr, type); |
| 5624 | return NULL_TREE; |
| 5625 | } |
| 5626 | |
| 5627 | if (!real_lvalue_p (expr)) |
| 5628 | { |
| 5629 | error ("%qE is not a valid template argument for type %qT " |
| 5630 | "because it is not an lvalue", expr, type); |
| 5631 | return NULL_TREE; |
| 5632 | } |
| 5633 | |
| 5634 | /* [temp.arg.nontype]/1 |
| 5635 | |
| 5636 | A template-argument for a non-type, non-template template-parameter |
| 5637 | shall be one of: [...] |
| 5638 | |
| 5639 | -- the address of an object or function with external linkage. */ |
| 5640 | if (TREE_CODE (expr) == INDIRECT_REF |
| 5641 | && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0)))) |
| 5642 | { |
| 5643 | expr = TREE_OPERAND (expr, 0); |
| 5644 | if (DECL_P (expr)) |
| 5645 | { |
| 5646 | error ("%q#D is not a valid template argument for type %qT " |
| 5647 | "because a reference variable does not have a constant " |
| 5648 | "address", expr, type); |
| 5649 | return NULL_TREE; |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | if (!DECL_P (expr)) |
| 5654 | { |
| 5655 | error ("%qE is not a valid template argument for type %qT " |
| 5656 | "because it is not an object with external linkage", |
| 5657 | expr, type); |
| 5658 | return NULL_TREE; |
| 5659 | } |
| 5660 | |
| 5661 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
| 5662 | { |
| 5663 | error ("%qE is not a valid template argument for type %qT " |
| 5664 | "because object %qD has not external linkage", |
| 5665 | expr, type, expr); |
| 5666 | return NULL_TREE; |
| 5667 | } |
| 5668 | |
| 5669 | expr = build_nop (type, build_address (expr)); |
| 5670 | } |
| 5671 | /* [temp.arg.nontype]/5, bullet 4 |
| 5672 | |
| 5673 | For a non-type template-parameter of type pointer to function, only |
| 5674 | the function-to-pointer conversion (_conv.func_) is applied. If the |
| 5675 | template-argument represents a set of overloaded functions (or a |
| 5676 | pointer to such), the matching function is selected from the set |
| 5677 | (_over.over_). */ |
| 5678 | else if (TYPE_PTRFN_P (type)) |
| 5679 | { |
| 5680 | /* If the argument is a template-id, we might not have enough |
| 5681 | context information to decay the pointer. */ |
| 5682 | if (!type_unknown_p (expr_type)) |
| 5683 | { |
| 5684 | expr = decay_conversion (expr); |
| 5685 | if (expr == error_mark_node) |
| 5686 | return error_mark_node; |
| 5687 | } |
| 5688 | |
| 5689 | if (cxx_dialect >= cxx0x && integer_zerop (expr)) |
| 5690 | /* Null pointer values are OK in C++11. */ |
| 5691 | return perform_qualification_conversions (type, expr); |
| 5692 | |
| 5693 | expr = convert_nontype_argument_function (type, expr); |
| 5694 | if (!expr || expr == error_mark_node) |
| 5695 | return expr; |
| 5696 | |
| 5697 | if (TREE_CODE (expr) != ADDR_EXPR) |
| 5698 | { |
| 5699 | error ("%qE is not a valid template argument for type %qT", expr, type); |
| 5700 | error ("it must be the address of a function with external linkage"); |
| 5701 | return NULL_TREE; |
| 5702 | } |
| 5703 | } |
| 5704 | /* [temp.arg.nontype]/5, bullet 5 |
| 5705 | |
| 5706 | For a non-type template-parameter of type reference to function, no |
| 5707 | conversions apply. If the template-argument represents a set of |
| 5708 | overloaded functions, the matching function is selected from the set |
| 5709 | (_over.over_). */ |
| 5710 | else if (TYPE_REFFN_P (type)) |
| 5711 | { |
| 5712 | if (TREE_CODE (expr) == ADDR_EXPR) |
| 5713 | { |
| 5714 | error ("%qE is not a valid template argument for type %qT " |
| 5715 | "because it is a pointer", expr, type); |
| 5716 | inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0)); |
| 5717 | return NULL_TREE; |
| 5718 | } |
| 5719 | |
| 5720 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
| 5721 | if (!expr || expr == error_mark_node) |
| 5722 | return expr; |
| 5723 | |
| 5724 | expr = build_nop (type, build_address (expr)); |
| 5725 | } |
| 5726 | /* [temp.arg.nontype]/5, bullet 6 |
| 5727 | |
| 5728 | For a non-type template-parameter of type pointer to member function, |
| 5729 | no conversions apply. If the template-argument represents a set of |
| 5730 | overloaded member functions, the matching member function is selected |
| 5731 | from the set (_over.over_). */ |
| 5732 | else if (TYPE_PTRMEMFUNC_P (type)) |
| 5733 | { |
| 5734 | expr = instantiate_type (type, expr, tf_none); |
| 5735 | if (expr == error_mark_node) |
| 5736 | return error_mark_node; |
| 5737 | |
| 5738 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
| 5739 | expression must be a pointer-to-member constant. */ |
| 5740 | if (!check_valid_ptrmem_cst_expr (type, expr, complain)) |
| 5741 | return error_mark_node; |
| 5742 | |
| 5743 | /* There is no way to disable standard conversions in |
| 5744 | resolve_address_of_overloaded_function (called by |
| 5745 | instantiate_type). It is possible that the call succeeded by |
| 5746 | converting &B::I to &D::I (where B is a base of D), so we need |
| 5747 | to reject this conversion here. |
| 5748 | |
| 5749 | Actually, even if there was a way to disable standard conversions, |
| 5750 | it would still be better to reject them here so that we can |
| 5751 | provide a superior diagnostic. */ |
| 5752 | if (!same_type_p (TREE_TYPE (expr), type)) |
| 5753 | { |
| 5754 | error ("%qE is not a valid template argument for type %qT " |
| 5755 | "because it is of type %qT", expr, type, |
| 5756 | TREE_TYPE (expr)); |
| 5757 | /* If we are just one standard conversion off, explain. */ |
| 5758 | if (can_convert (type, TREE_TYPE (expr))) |
| 5759 | inform (input_location, |
| 5760 | "standard conversions are not allowed in this context"); |
| 5761 | return NULL_TREE; |
| 5762 | } |
| 5763 | } |
| 5764 | /* [temp.arg.nontype]/5, bullet 7 |
| 5765 | |
| 5766 | For a non-type template-parameter of type pointer to data member, |
| 5767 | qualification conversions (_conv.qual_) are applied. */ |
| 5768 | else if (TYPE_PTRMEM_P (type)) |
| 5769 | { |
| 5770 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
| 5771 | expression must be a pointer-to-member constant. */ |
| 5772 | if (!check_valid_ptrmem_cst_expr (type, expr, complain)) |
| 5773 | return error_mark_node; |
| 5774 | |
| 5775 | expr = perform_qualification_conversions (type, expr); |
| 5776 | if (expr == error_mark_node) |
| 5777 | return expr; |
| 5778 | } |
| 5779 | else if (NULLPTR_TYPE_P (type)) |
| 5780 | { |
| 5781 | if (expr != nullptr_node) |
| 5782 | { |
| 5783 | error ("%qE is not a valid template argument for type %qT " |
| 5784 | "because it is of type %qT", expr, type, TREE_TYPE (expr)); |
| 5785 | return NULL_TREE; |
| 5786 | } |
| 5787 | return expr; |
| 5788 | } |
| 5789 | /* A template non-type parameter must be one of the above. */ |
| 5790 | else |
| 5791 | gcc_unreachable (); |
| 5792 | |
| 5793 | /* Sanity check: did we actually convert the argument to the |
| 5794 | right type? */ |
| 5795 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| 5796 | (type, TREE_TYPE (expr))); |
| 5797 | return expr; |
| 5798 | } |
| 5799 | |
| 5800 | /* Subroutine of coerce_template_template_parms, which returns 1 if |
| 5801 | PARM_PARM and ARG_PARM match using the rule for the template |
| 5802 | parameters of template template parameters. Both PARM and ARG are |
| 5803 | template parameters; the rest of the arguments are the same as for |
| 5804 | coerce_template_template_parms. |
| 5805 | */ |
| 5806 | static int |
| 5807 | coerce_template_template_parm (tree parm, |
| 5808 | tree arg, |
| 5809 | tsubst_flags_t complain, |
| 5810 | tree in_decl, |
| 5811 | tree outer_args) |
| 5812 | { |
| 5813 | if (arg == NULL_TREE || arg == error_mark_node |
| 5814 | || parm == NULL_TREE || parm == error_mark_node) |
| 5815 | return 0; |
| 5816 | |
| 5817 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
| 5818 | return 0; |
| 5819 | |
| 5820 | switch (TREE_CODE (parm)) |
| 5821 | { |
| 5822 | case TEMPLATE_DECL: |
| 5823 | /* We encounter instantiations of templates like |
| 5824 | template <template <template <class> class> class TT> |
| 5825 | class C; */ |
| 5826 | { |
| 5827 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
| 5828 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
| 5829 | |
| 5830 | if (!coerce_template_template_parms |
| 5831 | (parmparm, argparm, complain, in_decl, outer_args)) |
| 5832 | return 0; |
| 5833 | } |
| 5834 | /* Fall through. */ |
| 5835 | |
| 5836 | case TYPE_DECL: |
| 5837 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) |
| 5838 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) |
| 5839 | /* Argument is a parameter pack but parameter is not. */ |
| 5840 | return 0; |
| 5841 | break; |
| 5842 | |
| 5843 | case PARM_DECL: |
| 5844 | /* The tsubst call is used to handle cases such as |
| 5845 | |
| 5846 | template <int> class C {}; |
| 5847 | template <class T, template <T> class TT> class D {}; |
| 5848 | D<int, C> d; |
| 5849 | |
| 5850 | i.e. the parameter list of TT depends on earlier parameters. */ |
| 5851 | if (!uses_template_parms (TREE_TYPE (arg)) |
| 5852 | && !same_type_p |
| 5853 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), |
| 5854 | TREE_TYPE (arg))) |
| 5855 | return 0; |
| 5856 | |
| 5857 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) |
| 5858 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) |
| 5859 | /* Argument is a parameter pack but parameter is not. */ |
| 5860 | return 0; |
| 5861 | |
| 5862 | break; |
| 5863 | |
| 5864 | default: |
| 5865 | gcc_unreachable (); |
| 5866 | } |
| 5867 | |
| 5868 | return 1; |
| 5869 | } |
| 5870 | |
| 5871 | |
| 5872 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
| 5873 | template template parameters. Both PARM_PARMS and ARG_PARMS are |
| 5874 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL |
| 5875 | or PARM_DECL. |
| 5876 | |
| 5877 | Consider the example: |
| 5878 | template <class T> class A; |
| 5879 | template<template <class U> class TT> class B; |
| 5880 | |
| 5881 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
| 5882 | the parameters to A, and OUTER_ARGS contains A. */ |
| 5883 | |
| 5884 | static int |
| 5885 | coerce_template_template_parms (tree parm_parms, |
| 5886 | tree arg_parms, |
| 5887 | tsubst_flags_t complain, |
| 5888 | tree in_decl, |
| 5889 | tree outer_args) |
| 5890 | { |
| 5891 | int nparms, nargs, i; |
| 5892 | tree parm, arg; |
| 5893 | int variadic_p = 0; |
| 5894 | |
| 5895 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
| 5896 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); |
| 5897 | |
| 5898 | nparms = TREE_VEC_LENGTH (parm_parms); |
| 5899 | nargs = TREE_VEC_LENGTH (arg_parms); |
| 5900 | |
| 5901 | /* Determine whether we have a parameter pack at the end of the |
| 5902 | template template parameter's template parameter list. */ |
| 5903 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) |
| 5904 | { |
| 5905 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); |
| 5906 | |
| 5907 | if (parm == error_mark_node) |
| 5908 | return 0; |
| 5909 | |
| 5910 | switch (TREE_CODE (parm)) |
| 5911 | { |
| 5912 | case TEMPLATE_DECL: |
| 5913 | case TYPE_DECL: |
| 5914 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) |
| 5915 | variadic_p = 1; |
| 5916 | break; |
| 5917 | |
| 5918 | case PARM_DECL: |
| 5919 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) |
| 5920 | variadic_p = 1; |
| 5921 | break; |
| 5922 | |
| 5923 | default: |
| 5924 | gcc_unreachable (); |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | if (nargs != nparms |
| 5929 | && !(variadic_p && nargs >= nparms - 1)) |
| 5930 | return 0; |
| 5931 | |
| 5932 | /* Check all of the template parameters except the parameter pack at |
| 5933 | the end (if any). */ |
| 5934 | for (i = 0; i < nparms - variadic_p; ++i) |
| 5935 | { |
| 5936 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
| 5937 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) |
| 5938 | continue; |
| 5939 | |
| 5940 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
| 5941 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); |
| 5942 | |
| 5943 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
| 5944 | outer_args)) |
| 5945 | return 0; |
| 5946 | |
| 5947 | } |
| 5948 | |
| 5949 | if (variadic_p) |
| 5950 | { |
| 5951 | /* Check each of the template parameters in the template |
| 5952 | argument against the template parameter pack at the end of |
| 5953 | the template template parameter. */ |
| 5954 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) |
| 5955 | return 0; |
| 5956 | |
| 5957 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
| 5958 | |
| 5959 | for (; i < nargs; ++i) |
| 5960 | { |
| 5961 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) |
| 5962 | continue; |
| 5963 | |
| 5964 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); |
| 5965 | |
| 5966 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
| 5967 | outer_args)) |
| 5968 | return 0; |
| 5969 | } |
| 5970 | } |
| 5971 | |
| 5972 | return 1; |
| 5973 | } |
| 5974 | |
| 5975 | /* Verifies that the deduced template arguments (in TARGS) for the |
| 5976 | template template parameters (in TPARMS) represent valid bindings, |
| 5977 | by comparing the template parameter list of each template argument |
| 5978 | to the template parameter list of its corresponding template |
| 5979 | template parameter, in accordance with DR150. This |
| 5980 | routine can only be called after all template arguments have been |
| 5981 | deduced. It will return TRUE if all of the template template |
| 5982 | parameter bindings are okay, FALSE otherwise. */ |
| 5983 | bool |
| 5984 | template_template_parm_bindings_ok_p (tree tparms, tree targs) |
| 5985 | { |
| 5986 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
| 5987 | bool ret = true; |
| 5988 | |
| 5989 | /* We're dealing with template parms in this process. */ |
| 5990 | ++processing_template_decl; |
| 5991 | |
| 5992 | targs = INNERMOST_TEMPLATE_ARGS (targs); |
| 5993 | |
| 5994 | for (i = 0; i < ntparms; ++i) |
| 5995 | { |
| 5996 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
| 5997 | tree targ = TREE_VEC_ELT (targs, i); |
| 5998 | |
| 5999 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) |
| 6000 | { |
| 6001 | tree packed_args = NULL_TREE; |
| 6002 | int idx, len = 1; |
| 6003 | |
| 6004 | if (ARGUMENT_PACK_P (targ)) |
| 6005 | { |
| 6006 | /* Look inside the argument pack. */ |
| 6007 | packed_args = ARGUMENT_PACK_ARGS (targ); |
| 6008 | len = TREE_VEC_LENGTH (packed_args); |
| 6009 | } |
| 6010 | |
| 6011 | for (idx = 0; idx < len; ++idx) |
| 6012 | { |
| 6013 | tree targ_parms = NULL_TREE; |
| 6014 | |
| 6015 | if (packed_args) |
| 6016 | /* Extract the next argument from the argument |
| 6017 | pack. */ |
| 6018 | targ = TREE_VEC_ELT (packed_args, idx); |
| 6019 | |
| 6020 | if (PACK_EXPANSION_P (targ)) |
| 6021 | /* Look at the pattern of the pack expansion. */ |
| 6022 | targ = PACK_EXPANSION_PATTERN (targ); |
| 6023 | |
| 6024 | /* Extract the template parameters from the template |
| 6025 | argument. */ |
| 6026 | if (TREE_CODE (targ) == TEMPLATE_DECL) |
| 6027 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ); |
| 6028 | else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) |
| 6029 | targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ)); |
| 6030 | |
| 6031 | /* Verify that we can coerce the template template |
| 6032 | parameters from the template argument to the template |
| 6033 | parameter. This requires an exact match. */ |
| 6034 | if (targ_parms |
| 6035 | && !coerce_template_template_parms |
| 6036 | (DECL_INNERMOST_TEMPLATE_PARMS (tparm), |
| 6037 | targ_parms, |
| 6038 | tf_none, |
| 6039 | tparm, |
| 6040 | targs)) |
| 6041 | { |
| 6042 | ret = false; |
| 6043 | goto out; |
| 6044 | } |
| 6045 | } |
| 6046 | } |
| 6047 | } |
| 6048 | |
| 6049 | out: |
| 6050 | |
| 6051 | --processing_template_decl; |
| 6052 | return ret; |
| 6053 | } |
| 6054 | |
| 6055 | /* Since type attributes aren't mangled, we need to strip them from |
| 6056 | template type arguments. */ |
| 6057 | |
| 6058 | static tree |
| 6059 | canonicalize_type_argument (tree arg, tsubst_flags_t complain) |
| 6060 | { |
| 6061 | tree mv; |
| 6062 | if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg)) |
| 6063 | return arg; |
| 6064 | mv = TYPE_MAIN_VARIANT (arg); |
| 6065 | arg = strip_typedefs (arg); |
| 6066 | if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv) |
| 6067 | || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv)) |
| 6068 | { |
| 6069 | if (complain & tf_warning) |
| 6070 | warning (0, "ignoring attributes on template argument %qT", arg); |
| 6071 | arg = build_aligned_type (arg, TYPE_ALIGN (mv)); |
| 6072 | arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv)); |
| 6073 | } |
| 6074 | return arg; |
| 6075 | } |
| 6076 | |
| 6077 | /* Convert the indicated template ARG as necessary to match the |
| 6078 | indicated template PARM. Returns the converted ARG, or |
| 6079 | error_mark_node if the conversion was unsuccessful. Error and |
| 6080 | warning messages are issued under control of COMPLAIN. This |
| 6081 | conversion is for the Ith parameter in the parameter list. ARGS is |
| 6082 | the full set of template arguments deduced so far. */ |
| 6083 | |
| 6084 | static tree |
| 6085 | convert_template_argument (tree parm, |
| 6086 | tree arg, |
| 6087 | tree args, |
| 6088 | tsubst_flags_t complain, |
| 6089 | int i, |
| 6090 | tree in_decl) |
| 6091 | { |
| 6092 | tree orig_arg; |
| 6093 | tree val; |
| 6094 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
| 6095 | |
| 6096 | if (TREE_CODE (arg) == TREE_LIST |
| 6097 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
| 6098 | { |
| 6099 | /* The template argument was the name of some |
| 6100 | member function. That's usually |
| 6101 | invalid, but static members are OK. In any |
| 6102 | case, grab the underlying fields/functions |
| 6103 | and issue an error later if required. */ |
| 6104 | orig_arg = TREE_VALUE (arg); |
| 6105 | TREE_TYPE (arg) = unknown_type_node; |
| 6106 | } |
| 6107 | |
| 6108 | orig_arg = arg; |
| 6109 | |
| 6110 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
| 6111 | requires_type = (TREE_CODE (parm) == TYPE_DECL |
| 6112 | || requires_tmpl_type); |
| 6113 | |
| 6114 | /* When determining whether an argument pack expansion is a template, |
| 6115 | look at the pattern. */ |
| 6116 | if (TREE_CODE (arg) == TYPE_PACK_EXPANSION) |
| 6117 | arg = PACK_EXPANSION_PATTERN (arg); |
| 6118 | |
| 6119 | /* Deal with an injected-class-name used as a template template arg. */ |
| 6120 | if (requires_tmpl_type && CLASS_TYPE_P (arg)) |
| 6121 | { |
| 6122 | tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg)); |
| 6123 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 6124 | { |
| 6125 | if (cxx_dialect >= cxx0x) |
| 6126 | /* OK under DR 1004. */; |
| 6127 | else if (complain & tf_warning_or_error) |
| 6128 | pedwarn (input_location, OPT_pedantic, "injected-class-name %qD" |
| 6129 | " used as template template argument", TYPE_NAME (arg)); |
| 6130 | else if (flag_pedantic_errors) |
| 6131 | t = arg; |
| 6132 | |
| 6133 | arg = t; |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | is_tmpl_type = |
| 6138 | ((TREE_CODE (arg) == TEMPLATE_DECL |
| 6139 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) |
| 6140 | || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK) |
| 6141 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
| 6142 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); |
| 6143 | |
| 6144 | if (is_tmpl_type |
| 6145 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
| 6146 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) |
| 6147 | arg = TYPE_STUB_DECL (arg); |
| 6148 | |
| 6149 | is_type = TYPE_P (arg) || is_tmpl_type; |
| 6150 | |
| 6151 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF |
| 6152 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) |
| 6153 | { |
| 6154 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR) |
| 6155 | { |
| 6156 | if (complain & tf_error) |
| 6157 | error ("invalid use of destructor %qE as a type", orig_arg); |
| 6158 | return error_mark_node; |
| 6159 | } |
| 6160 | |
| 6161 | permerror (input_location, |
| 6162 | "to refer to a type member of a template parameter, " |
| 6163 | "use %<typename %E%>", orig_arg); |
| 6164 | |
| 6165 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
| 6166 | TREE_OPERAND (arg, 1), |
| 6167 | typename_type, |
| 6168 | complain & tf_error); |
| 6169 | arg = orig_arg; |
| 6170 | is_type = 1; |
| 6171 | } |
| 6172 | if (is_type != requires_type) |
| 6173 | { |
| 6174 | if (in_decl) |
| 6175 | { |
| 6176 | if (complain & tf_error) |
| 6177 | { |
| 6178 | error ("type/value mismatch at argument %d in template " |
| 6179 | "parameter list for %qD", |
| 6180 | i + 1, in_decl); |
| 6181 | if (is_type) |
| 6182 | error (" expected a constant of type %qT, got %qT", |
| 6183 | TREE_TYPE (parm), |
| 6184 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
| 6185 | else if (requires_tmpl_type) |
| 6186 | error (" expected a class template, got %qE", orig_arg); |
| 6187 | else |
| 6188 | error (" expected a type, got %qE", orig_arg); |
| 6189 | } |
| 6190 | } |
| 6191 | return error_mark_node; |
| 6192 | } |
| 6193 | if (is_tmpl_type ^ requires_tmpl_type) |
| 6194 | { |
| 6195 | if (in_decl && (complain & tf_error)) |
| 6196 | { |
| 6197 | error ("type/value mismatch at argument %d in template " |
| 6198 | "parameter list for %qD", |
| 6199 | i + 1, in_decl); |
| 6200 | if (is_tmpl_type) |
| 6201 | error (" expected a type, got %qT", DECL_NAME (arg)); |
| 6202 | else |
| 6203 | error (" expected a class template, got %qT", orig_arg); |
| 6204 | } |
| 6205 | return error_mark_node; |
| 6206 | } |
| 6207 | |
| 6208 | if (is_type) |
| 6209 | { |
| 6210 | if (requires_tmpl_type) |
| 6211 | { |
| 6212 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
| 6213 | val = orig_arg; |
| 6214 | else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
| 6215 | /* The number of argument required is not known yet. |
| 6216 | Just accept it for now. */ |
| 6217 | val = TREE_TYPE (arg); |
| 6218 | else |
| 6219 | { |
| 6220 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
| 6221 | tree argparm; |
| 6222 | |
| 6223 | argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
| 6224 | |
| 6225 | if (coerce_template_template_parms (parmparm, argparm, |
| 6226 | complain, in_decl, |
| 6227 | args)) |
| 6228 | { |
| 6229 | val = arg; |
| 6230 | |
| 6231 | /* TEMPLATE_TEMPLATE_PARM node is preferred over |
| 6232 | TEMPLATE_DECL. */ |
| 6233 | if (val != error_mark_node) |
| 6234 | { |
| 6235 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) |
| 6236 | val = TREE_TYPE (val); |
| 6237 | if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION) |
| 6238 | val = make_pack_expansion (val); |
| 6239 | } |
| 6240 | } |
| 6241 | else |
| 6242 | { |
| 6243 | if (in_decl && (complain & tf_error)) |
| 6244 | { |
| 6245 | error ("type/value mismatch at argument %d in " |
| 6246 | "template parameter list for %qD", |
| 6247 | i + 1, in_decl); |
| 6248 | error (" expected a template of type %qD, got %qT", |
| 6249 | parm, orig_arg); |
| 6250 | } |
| 6251 | |
| 6252 | val = error_mark_node; |
| 6253 | } |
| 6254 | } |
| 6255 | } |
| 6256 | else |
| 6257 | val = orig_arg; |
| 6258 | /* We only form one instance of each template specialization. |
| 6259 | Therefore, if we use a non-canonical variant (i.e., a |
| 6260 | typedef), any future messages referring to the type will use |
| 6261 | the typedef, which is confusing if those future uses do not |
| 6262 | themselves also use the typedef. */ |
| 6263 | if (TYPE_P (val)) |
| 6264 | val = canonicalize_type_argument (val, complain); |
| 6265 | } |
| 6266 | else |
| 6267 | { |
| 6268 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
| 6269 | |
| 6270 | if (invalid_nontype_parm_type_p (t, complain)) |
| 6271 | return error_mark_node; |
| 6272 | |
| 6273 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
| 6274 | { |
| 6275 | if (same_type_p (t, TREE_TYPE (orig_arg))) |
| 6276 | val = orig_arg; |
| 6277 | else |
| 6278 | { |
| 6279 | /* Not sure if this is reachable, but it doesn't hurt |
| 6280 | to be robust. */ |
| 6281 | error ("type mismatch in nontype parameter pack"); |
| 6282 | val = error_mark_node; |
| 6283 | } |
| 6284 | } |
| 6285 | else if (!uses_template_parms (orig_arg) && !uses_template_parms (t)) |
| 6286 | /* We used to call digest_init here. However, digest_init |
| 6287 | will report errors, which we don't want when complain |
| 6288 | is zero. More importantly, digest_init will try too |
| 6289 | hard to convert things: for example, `0' should not be |
| 6290 | converted to pointer type at this point according to |
| 6291 | the standard. Accepting this is not merely an |
| 6292 | extension, since deciding whether or not these |
| 6293 | conversions can occur is part of determining which |
| 6294 | function template to call, or whether a given explicit |
| 6295 | argument specification is valid. */ |
| 6296 | val = convert_nontype_argument (t, orig_arg, complain); |
| 6297 | else |
| 6298 | val = strip_typedefs_expr (orig_arg); |
| 6299 | |
| 6300 | if (val == NULL_TREE) |
| 6301 | val = error_mark_node; |
| 6302 | else if (val == error_mark_node && (complain & tf_error)) |
| 6303 | error ("could not convert template argument %qE to %qT", orig_arg, t); |
| 6304 | |
| 6305 | if (TREE_CODE (val) == SCOPE_REF) |
| 6306 | { |
| 6307 | /* Strip typedefs from the SCOPE_REF. */ |
| 6308 | tree type = canonicalize_type_argument (TREE_TYPE (val), complain); |
| 6309 | tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0), |
| 6310 | complain); |
| 6311 | val = build_qualified_name (type, scope, TREE_OPERAND (val, 1), |
| 6312 | QUALIFIED_NAME_IS_TEMPLATE (val)); |
| 6313 | } |
| 6314 | } |
| 6315 | |
| 6316 | return val; |
| 6317 | } |
| 6318 | |
| 6319 | /* Coerces the remaining template arguments in INNER_ARGS (from |
| 6320 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. |
| 6321 | Returns the coerced argument pack. PARM_IDX is the position of this |
| 6322 | parameter in the template parameter list. ARGS is the original |
| 6323 | template argument list. */ |
| 6324 | static tree |
| 6325 | coerce_template_parameter_pack (tree parms, |
| 6326 | int parm_idx, |
| 6327 | tree args, |
| 6328 | tree inner_args, |
| 6329 | int arg_idx, |
| 6330 | tree new_args, |
| 6331 | int* lost, |
| 6332 | tree in_decl, |
| 6333 | tsubst_flags_t complain) |
| 6334 | { |
| 6335 | tree parm = TREE_VEC_ELT (parms, parm_idx); |
| 6336 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
| 6337 | tree packed_args; |
| 6338 | tree argument_pack; |
| 6339 | tree packed_types = NULL_TREE; |
| 6340 | |
| 6341 | if (arg_idx > nargs) |
| 6342 | arg_idx = nargs; |
| 6343 | |
| 6344 | packed_args = make_tree_vec (nargs - arg_idx); |
| 6345 | |
| 6346 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL |
| 6347 | && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm)))) |
| 6348 | { |
| 6349 | /* When the template parameter is a non-type template |
| 6350 | parameter pack whose type uses parameter packs, we need |
| 6351 | to look at each of the template arguments |
| 6352 | separately. Build a vector of the types for these |
| 6353 | non-type template parameters in PACKED_TYPES. */ |
| 6354 | tree expansion |
| 6355 | = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm))); |
| 6356 | packed_types = tsubst_pack_expansion (expansion, args, |
| 6357 | complain, in_decl); |
| 6358 | |
| 6359 | if (packed_types == error_mark_node) |
| 6360 | return error_mark_node; |
| 6361 | |
| 6362 | /* Check that we have the right number of arguments. */ |
| 6363 | if (arg_idx < nargs |
| 6364 | && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)) |
| 6365 | && nargs - arg_idx != TREE_VEC_LENGTH (packed_types)) |
| 6366 | { |
| 6367 | int needed_parms |
| 6368 | = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types); |
| 6369 | error ("wrong number of template arguments (%d, should be %d)", |
| 6370 | nargs, needed_parms); |
| 6371 | return error_mark_node; |
| 6372 | } |
| 6373 | |
| 6374 | /* If we aren't able to check the actual arguments now |
| 6375 | (because they haven't been expanded yet), we can at least |
| 6376 | verify that all of the types used for the non-type |
| 6377 | template parameter pack are, in fact, valid for non-type |
| 6378 | template parameters. */ |
| 6379 | if (arg_idx < nargs |
| 6380 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) |
| 6381 | { |
| 6382 | int j, len = TREE_VEC_LENGTH (packed_types); |
| 6383 | for (j = 0; j < len; ++j) |
| 6384 | { |
| 6385 | tree t = TREE_VEC_ELT (packed_types, j); |
| 6386 | if (invalid_nontype_parm_type_p (t, complain)) |
| 6387 | return error_mark_node; |
| 6388 | } |
| 6389 | } |
| 6390 | } |
| 6391 | |
| 6392 | /* Convert the remaining arguments, which will be a part of the |
| 6393 | parameter pack "parm". */ |
| 6394 | for (; arg_idx < nargs; ++arg_idx) |
| 6395 | { |
| 6396 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); |
| 6397 | tree actual_parm = TREE_VALUE (parm); |
| 6398 | |
| 6399 | if (packed_types && !PACK_EXPANSION_P (arg)) |
| 6400 | { |
| 6401 | /* When we have a vector of types (corresponding to the |
| 6402 | non-type template parameter pack that uses parameter |
| 6403 | packs in its type, as mention above), and the |
| 6404 | argument is not an expansion (which expands to a |
| 6405 | currently unknown number of arguments), clone the |
| 6406 | parm and give it the next type in PACKED_TYPES. */ |
| 6407 | actual_parm = copy_node (actual_parm); |
| 6408 | TREE_TYPE (actual_parm) = |
| 6409 | TREE_VEC_ELT (packed_types, arg_idx - parm_idx); |
| 6410 | } |
| 6411 | |
| 6412 | if (arg != error_mark_node) |
| 6413 | arg = convert_template_argument (actual_parm, |
| 6414 | arg, new_args, complain, parm_idx, |
| 6415 | in_decl); |
| 6416 | if (arg == error_mark_node) |
| 6417 | (*lost)++; |
| 6418 | TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg; |
| 6419 | } |
| 6420 | |
| 6421 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL |
| 6422 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) |
| 6423 | argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 6424 | else |
| 6425 | { |
| 6426 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); |
| 6427 | TREE_TYPE (argument_pack) |
| 6428 | = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl); |
| 6429 | TREE_CONSTANT (argument_pack) = 1; |
| 6430 | } |
| 6431 | |
| 6432 | SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args); |
| 6433 | #ifdef ENABLE_CHECKING |
| 6434 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args, |
| 6435 | TREE_VEC_LENGTH (packed_args)); |
| 6436 | #endif |
| 6437 | return argument_pack; |
| 6438 | } |
| 6439 | |
| 6440 | /* Returns true if the template argument vector ARGS contains |
| 6441 | any pack expansions, false otherwise. */ |
| 6442 | |
| 6443 | static bool |
| 6444 | any_pack_expanson_args_p (tree args) |
| 6445 | { |
| 6446 | int i; |
| 6447 | if (args) |
| 6448 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) |
| 6449 | if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i))) |
| 6450 | return true; |
| 6451 | return false; |
| 6452 | } |
| 6453 | |
| 6454 | /* Convert all template arguments to their appropriate types, and |
| 6455 | return a vector containing the innermost resulting template |
| 6456 | arguments. If any error occurs, return error_mark_node. Error and |
| 6457 | warning messages are issued under control of COMPLAIN. |
| 6458 | |
| 6459 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
| 6460 | for arguments not specified in ARGS. Otherwise, if |
| 6461 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
| 6462 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but |
| 6463 | USE_DEFAULT_ARGS is false, then all arguments must be specified in |
| 6464 | ARGS. */ |
| 6465 | |
| 6466 | static tree |
| 6467 | coerce_template_parms (tree parms, |
| 6468 | tree args, |
| 6469 | tree in_decl, |
| 6470 | tsubst_flags_t complain, |
| 6471 | bool require_all_args, |
| 6472 | bool use_default_args) |
| 6473 | { |
| 6474 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
| 6475 | tree inner_args; |
| 6476 | tree new_args; |
| 6477 | tree new_inner_args; |
| 6478 | int saved_unevaluated_operand; |
| 6479 | int saved_inhibit_evaluation_warnings; |
| 6480 | |
| 6481 | /* When used as a boolean value, indicates whether this is a |
| 6482 | variadic template parameter list. Since it's an int, we can also |
| 6483 | subtract it from nparms to get the number of non-variadic |
| 6484 | parameters. */ |
| 6485 | int variadic_p = 0; |
| 6486 | int post_variadic_parms = 0; |
| 6487 | |
| 6488 | if (args == error_mark_node) |
| 6489 | return error_mark_node; |
| 6490 | |
| 6491 | nparms = TREE_VEC_LENGTH (parms); |
| 6492 | |
| 6493 | /* Determine if there are any parameter packs. */ |
| 6494 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) |
| 6495 | { |
| 6496 | tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx)); |
| 6497 | if (variadic_p) |
| 6498 | ++post_variadic_parms; |
| 6499 | if (template_parameter_pack_p (tparm)) |
| 6500 | ++variadic_p; |
| 6501 | } |
| 6502 | |
| 6503 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
| 6504 | /* If there are no parameters that follow a parameter pack, we need to |
| 6505 | expand any argument packs so that we can deduce a parameter pack from |
| 6506 | some non-packed args followed by an argument pack, as in variadic85.C. |
| 6507 | If there are such parameters, we need to leave argument packs intact |
| 6508 | so the arguments are assigned properly. This can happen when dealing |
| 6509 | with a nested class inside a partial specialization of a class |
| 6510 | template, as in variadic92.C, or when deducing a template parameter pack |
| 6511 | from a sub-declarator, as in variadic114.C. */ |
| 6512 | if (!post_variadic_parms) |
| 6513 | inner_args = expand_template_argument_pack (inner_args); |
| 6514 | |
| 6515 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
| 6516 | if ((nargs > nparms && !variadic_p) |
| 6517 | || (nargs < nparms - variadic_p |
| 6518 | && require_all_args |
| 6519 | && !any_pack_expanson_args_p (inner_args) |
| 6520 | && (!use_default_args |
| 6521 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
| 6522 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) |
| 6523 | { |
| 6524 | if (complain & tf_error) |
| 6525 | { |
| 6526 | if (variadic_p) |
| 6527 | { |
| 6528 | nparms -= variadic_p; |
| 6529 | error ("wrong number of template arguments " |
| 6530 | "(%d, should be %d or more)", nargs, nparms); |
| 6531 | } |
| 6532 | else |
| 6533 | error ("wrong number of template arguments " |
| 6534 | "(%d, should be %d)", nargs, nparms); |
| 6535 | |
| 6536 | if (in_decl) |
| 6537 | error ("provided for %q+D", in_decl); |
| 6538 | } |
| 6539 | |
| 6540 | return error_mark_node; |
| 6541 | } |
| 6542 | |
| 6543 | /* We need to evaluate the template arguments, even though this |
| 6544 | template-id may be nested within a "sizeof". */ |
| 6545 | saved_unevaluated_operand = cp_unevaluated_operand; |
| 6546 | cp_unevaluated_operand = 0; |
| 6547 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; |
| 6548 | c_inhibit_evaluation_warnings = 0; |
| 6549 | new_inner_args = make_tree_vec (nparms); |
| 6550 | new_args = add_outermost_template_args (args, new_inner_args); |
| 6551 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
| 6552 | { |
| 6553 | tree arg; |
| 6554 | tree parm; |
| 6555 | |
| 6556 | /* Get the Ith template parameter. */ |
| 6557 | parm = TREE_VEC_ELT (parms, parm_idx); |
| 6558 | |
| 6559 | if (parm == error_mark_node) |
| 6560 | { |
| 6561 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
| 6562 | continue; |
| 6563 | } |
| 6564 | |
| 6565 | /* Calculate the next argument. */ |
| 6566 | if (arg_idx < nargs) |
| 6567 | arg = TREE_VEC_ELT (inner_args, arg_idx); |
| 6568 | else |
| 6569 | arg = NULL_TREE; |
| 6570 | |
| 6571 | if (template_parameter_pack_p (TREE_VALUE (parm)) |
| 6572 | && !(arg && ARGUMENT_PACK_P (arg))) |
| 6573 | { |
| 6574 | /* All remaining arguments will be placed in the |
| 6575 | template parameter pack PARM. */ |
| 6576 | arg = coerce_template_parameter_pack (parms, parm_idx, args, |
| 6577 | inner_args, arg_idx, |
| 6578 | new_args, &lost, |
| 6579 | in_decl, complain); |
| 6580 | |
| 6581 | /* Store this argument. */ |
| 6582 | if (arg == error_mark_node) |
| 6583 | lost++; |
| 6584 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; |
| 6585 | |
| 6586 | /* We are done with all of the arguments. */ |
| 6587 | arg_idx = nargs; |
| 6588 | |
| 6589 | continue; |
| 6590 | } |
| 6591 | else if (arg) |
| 6592 | { |
| 6593 | if (PACK_EXPANSION_P (arg)) |
| 6594 | { |
| 6595 | /* We don't know how many args we have yet, just |
| 6596 | use the unconverted ones for now. */ |
| 6597 | new_inner_args = inner_args; |
| 6598 | break; |
| 6599 | } |
| 6600 | } |
| 6601 | else if (require_all_args) |
| 6602 | { |
| 6603 | /* There must be a default arg in this case. */ |
| 6604 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
| 6605 | complain, in_decl); |
| 6606 | /* The position of the first default template argument, |
| 6607 | is also the number of non-defaulted arguments in NEW_INNER_ARGS. |
| 6608 | Record that. */ |
| 6609 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) |
| 6610 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx); |
| 6611 | } |
| 6612 | else |
| 6613 | break; |
| 6614 | |
| 6615 | if (arg == error_mark_node) |
| 6616 | { |
| 6617 | if (complain & tf_error) |
| 6618 | error ("template argument %d is invalid", arg_idx + 1); |
| 6619 | } |
| 6620 | else if (!arg) |
| 6621 | /* This only occurs if there was an error in the template |
| 6622 | parameter list itself (which we would already have |
| 6623 | reported) that we are trying to recover from, e.g., a class |
| 6624 | template with a parameter list such as |
| 6625 | template<typename..., typename>. */ |
| 6626 | ++lost; |
| 6627 | else |
| 6628 | arg = convert_template_argument (TREE_VALUE (parm), |
| 6629 | arg, new_args, complain, |
| 6630 | parm_idx, in_decl); |
| 6631 | |
| 6632 | if (arg == error_mark_node) |
| 6633 | lost++; |
| 6634 | TREE_VEC_ELT (new_inner_args, arg_idx) = arg; |
| 6635 | } |
| 6636 | cp_unevaluated_operand = saved_unevaluated_operand; |
| 6637 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; |
| 6638 | |
| 6639 | if (lost) |
| 6640 | return error_mark_node; |
| 6641 | |
| 6642 | #ifdef ENABLE_CHECKING |
| 6643 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) |
| 6644 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, |
| 6645 | TREE_VEC_LENGTH (new_inner_args)); |
| 6646 | #endif |
| 6647 | |
| 6648 | return new_inner_args; |
| 6649 | } |
| 6650 | |
| 6651 | /* Returns 1 if template args OT and NT are equivalent. */ |
| 6652 | |
| 6653 | static int |
| 6654 | template_args_equal (tree ot, tree nt) |
| 6655 | { |
| 6656 | if (nt == ot) |
| 6657 | return 1; |
| 6658 | if (nt == NULL_TREE || ot == NULL_TREE) |
| 6659 | return false; |
| 6660 | |
| 6661 | if (TREE_CODE (nt) == TREE_VEC) |
| 6662 | /* For member templates */ |
| 6663 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
| 6664 | else if (PACK_EXPANSION_P (ot)) |
| 6665 | return (PACK_EXPANSION_P (nt) |
| 6666 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), |
| 6667 | PACK_EXPANSION_PATTERN (nt)) |
| 6668 | && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot), |
| 6669 | PACK_EXPANSION_EXTRA_ARGS (nt))); |
| 6670 | else if (ARGUMENT_PACK_P (ot)) |
| 6671 | { |
| 6672 | int i, len; |
| 6673 | tree opack, npack; |
| 6674 | |
| 6675 | if (!ARGUMENT_PACK_P (nt)) |
| 6676 | return 0; |
| 6677 | |
| 6678 | opack = ARGUMENT_PACK_ARGS (ot); |
| 6679 | npack = ARGUMENT_PACK_ARGS (nt); |
| 6680 | len = TREE_VEC_LENGTH (opack); |
| 6681 | if (TREE_VEC_LENGTH (npack) != len) |
| 6682 | return 0; |
| 6683 | for (i = 0; i < len; ++i) |
| 6684 | if (!template_args_equal (TREE_VEC_ELT (opack, i), |
| 6685 | TREE_VEC_ELT (npack, i))) |
| 6686 | return 0; |
| 6687 | return 1; |
| 6688 | } |
| 6689 | else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT) |
| 6690 | { |
| 6691 | /* We get here probably because we are in the middle of substituting |
| 6692 | into the pattern of a pack expansion. In that case the |
| 6693 | ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are |
| 6694 | interested in. So we want to use the initial pack argument for |
| 6695 | the comparison. */ |
| 6696 | ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot); |
| 6697 | if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT) |
| 6698 | nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt); |
| 6699 | return template_args_equal (ot, nt); |
| 6700 | } |
| 6701 | else if (TYPE_P (nt)) |
| 6702 | return TYPE_P (ot) && same_type_p (ot, nt); |
| 6703 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
| 6704 | return 0; |
| 6705 | else |
| 6706 | return cp_tree_equal (ot, nt); |
| 6707 | } |
| 6708 | |
| 6709 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of |
| 6710 | template arguments. Returns 0 otherwise, and updates OLDARG_PTR and |
| 6711 | NEWARG_PTR with the offending arguments if they are non-NULL. */ |
| 6712 | |
| 6713 | static int |
| 6714 | comp_template_args_with_info (tree oldargs, tree newargs, |
| 6715 | tree *oldarg_ptr, tree *newarg_ptr) |
| 6716 | { |
| 6717 | int i; |
| 6718 | |
| 6719 | if (oldargs == newargs) |
| 6720 | return 1; |
| 6721 | |
| 6722 | if (!oldargs || !newargs) |
| 6723 | return 0; |
| 6724 | |
| 6725 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
| 6726 | return 0; |
| 6727 | |
| 6728 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
| 6729 | { |
| 6730 | tree nt = TREE_VEC_ELT (newargs, i); |
| 6731 | tree ot = TREE_VEC_ELT (oldargs, i); |
| 6732 | |
| 6733 | if (! template_args_equal (ot, nt)) |
| 6734 | { |
| 6735 | if (oldarg_ptr != NULL) |
| 6736 | *oldarg_ptr = ot; |
| 6737 | if (newarg_ptr != NULL) |
| 6738 | *newarg_ptr = nt; |
| 6739 | return 0; |
| 6740 | } |
| 6741 | } |
| 6742 | return 1; |
| 6743 | } |
| 6744 | |
| 6745 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets |
| 6746 | of template arguments. Returns 0 otherwise. */ |
| 6747 | |
| 6748 | int |
| 6749 | comp_template_args (tree oldargs, tree newargs) |
| 6750 | { |
| 6751 | return comp_template_args_with_info (oldargs, newargs, NULL, NULL); |
| 6752 | } |
| 6753 | |
| 6754 | static void |
| 6755 | add_pending_template (tree d) |
| 6756 | { |
| 6757 | tree ti = (TYPE_P (d) |
| 6758 | ? CLASSTYPE_TEMPLATE_INFO (d) |
| 6759 | : DECL_TEMPLATE_INFO (d)); |
| 6760 | struct pending_template *pt; |
| 6761 | int level; |
| 6762 | |
| 6763 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
| 6764 | return; |
| 6765 | |
| 6766 | /* We are called both from instantiate_decl, where we've already had a |
| 6767 | tinst_level pushed, and instantiate_template, where we haven't. |
| 6768 | Compensate. */ |
| 6769 | level = !current_tinst_level || current_tinst_level->decl != d; |
| 6770 | |
| 6771 | if (level) |
| 6772 | push_tinst_level (d); |
| 6773 | |
| 6774 | pt = ggc_alloc_pending_template (); |
| 6775 | pt->next = NULL; |
| 6776 | pt->tinst = current_tinst_level; |
| 6777 | if (last_pending_template) |
| 6778 | last_pending_template->next = pt; |
| 6779 | else |
| 6780 | pending_templates = pt; |
| 6781 | |
| 6782 | last_pending_template = pt; |
| 6783 | |
| 6784 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
| 6785 | |
| 6786 | if (level) |
| 6787 | pop_tinst_level (); |
| 6788 | } |
| 6789 | |
| 6790 | |
| 6791 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
| 6792 | ARGLIST. Valid choices for FNS are given in the cp-tree.def |
| 6793 | documentation for TEMPLATE_ID_EXPR. */ |
| 6794 | |
| 6795 | tree |
| 6796 | lookup_template_function (tree fns, tree arglist) |
| 6797 | { |
| 6798 | tree type; |
| 6799 | |
| 6800 | if (fns == error_mark_node || arglist == error_mark_node) |
| 6801 | return error_mark_node; |
| 6802 | |
| 6803 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
| 6804 | |
| 6805 | if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE) |
| 6806 | { |
| 6807 | error ("%q#D is not a function template", fns); |
| 6808 | return error_mark_node; |
| 6809 | } |
| 6810 | |
| 6811 | if (BASELINK_P (fns)) |
| 6812 | { |
| 6813 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
| 6814 | unknown_type_node, |
| 6815 | BASELINK_FUNCTIONS (fns), |
| 6816 | arglist); |
| 6817 | return fns; |
| 6818 | } |
| 6819 | |
| 6820 | type = TREE_TYPE (fns); |
| 6821 | if (TREE_CODE (fns) == OVERLOAD || !type) |
| 6822 | type = unknown_type_node; |
| 6823 | |
| 6824 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
| 6825 | } |
| 6826 | |
| 6827 | /* Within the scope of a template class S<T>, the name S gets bound |
| 6828 | (in build_self_reference) to a TYPE_DECL for the class, not a |
| 6829 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, |
| 6830 | or one of its enclosing classes, and that type is a template, |
| 6831 | return the associated TEMPLATE_DECL. Otherwise, the original |
| 6832 | DECL is returned. |
| 6833 | |
| 6834 | Also handle the case when DECL is a TREE_LIST of ambiguous |
| 6835 | injected-class-names from different bases. */ |
| 6836 | |
| 6837 | tree |
| 6838 | maybe_get_template_decl_from_type_decl (tree decl) |
| 6839 | { |
| 6840 | if (decl == NULL_TREE) |
| 6841 | return decl; |
| 6842 | |
| 6843 | /* DR 176: A lookup that finds an injected-class-name (10.2 |
| 6844 | [class.member.lookup]) can result in an ambiguity in certain cases |
| 6845 | (for example, if it is found in more than one base class). If all of |
| 6846 | the injected-class-names that are found refer to specializations of |
| 6847 | the same class template, and if the name is followed by a |
| 6848 | template-argument-list, the reference refers to the class template |
| 6849 | itself and not a specialization thereof, and is not ambiguous. */ |
| 6850 | if (TREE_CODE (decl) == TREE_LIST) |
| 6851 | { |
| 6852 | tree t, tmpl = NULL_TREE; |
| 6853 | for (t = decl; t; t = TREE_CHAIN (t)) |
| 6854 | { |
| 6855 | tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t)); |
| 6856 | if (!tmpl) |
| 6857 | tmpl = elt; |
| 6858 | else if (tmpl != elt) |
| 6859 | break; |
| 6860 | } |
| 6861 | if (tmpl && t == NULL_TREE) |
| 6862 | return tmpl; |
| 6863 | else |
| 6864 | return decl; |
| 6865 | } |
| 6866 | |
| 6867 | return (decl != NULL_TREE |
| 6868 | && DECL_SELF_REFERENCE_P (decl) |
| 6869 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
| 6870 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
| 6871 | } |
| 6872 | |
| 6873 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
| 6874 | parameters, find the desired type. |
| 6875 | |
| 6876 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. |
| 6877 | |
| 6878 | IN_DECL, if non-NULL, is the template declaration we are trying to |
| 6879 | instantiate. |
| 6880 | |
| 6881 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
| 6882 | the class we are looking up. |
| 6883 | |
| 6884 | Issue error and warning messages under control of COMPLAIN. |
| 6885 | |
| 6886 | If the template class is really a local class in a template |
| 6887 | function, then the FUNCTION_CONTEXT is the function in which it is |
| 6888 | being instantiated. |
| 6889 | |
| 6890 | ??? Note that this function is currently called *twice* for each |
| 6891 | template-id: the first time from the parser, while creating the |
| 6892 | incomplete type (finish_template_type), and the second type during the |
| 6893 | real instantiation (instantiate_template_class). This is surely something |
| 6894 | that we want to avoid. It also causes some problems with argument |
| 6895 | coercion (see convert_nontype_argument for more information on this). */ |
| 6896 | |
| 6897 | static tree |
| 6898 | lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context, |
| 6899 | int entering_scope, tsubst_flags_t complain) |
| 6900 | { |
| 6901 | tree templ = NULL_TREE, parmlist; |
| 6902 | tree t; |
| 6903 | void **slot; |
| 6904 | spec_entry *entry; |
| 6905 | spec_entry elt; |
| 6906 | hashval_t hash; |
| 6907 | |
| 6908 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
| 6909 | { |
| 6910 | tree value = innermost_non_namespace_value (d1); |
| 6911 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) |
| 6912 | templ = value; |
| 6913 | else |
| 6914 | { |
| 6915 | if (context) |
| 6916 | push_decl_namespace (context); |
| 6917 | templ = lookup_name (d1); |
| 6918 | templ = maybe_get_template_decl_from_type_decl (templ); |
| 6919 | if (context) |
| 6920 | pop_decl_namespace (); |
| 6921 | } |
| 6922 | if (templ) |
| 6923 | context = DECL_CONTEXT (templ); |
| 6924 | } |
| 6925 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
| 6926 | { |
| 6927 | tree type = TREE_TYPE (d1); |
| 6928 | |
| 6929 | /* If we are declaring a constructor, say A<T>::A<T>, we will get |
| 6930 | an implicit typename for the second A. Deal with it. */ |
| 6931 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
| 6932 | type = TREE_TYPE (type); |
| 6933 | |
| 6934 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
| 6935 | { |
| 6936 | templ = CLASSTYPE_TI_TEMPLATE (type); |
| 6937 | d1 = DECL_NAME (templ); |
| 6938 | } |
| 6939 | } |
| 6940 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
| 6941 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
| 6942 | { |
| 6943 | templ = TYPE_TI_TEMPLATE (d1); |
| 6944 | d1 = DECL_NAME (templ); |
| 6945 | } |
| 6946 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
| 6947 | && DECL_TEMPLATE_RESULT (d1) |
| 6948 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
| 6949 | { |
| 6950 | templ = d1; |
| 6951 | d1 = DECL_NAME (templ); |
| 6952 | context = DECL_CONTEXT (templ); |
| 6953 | } |
| 6954 | |
| 6955 | /* Issue an error message if we didn't find a template. */ |
| 6956 | if (! templ) |
| 6957 | { |
| 6958 | if (complain & tf_error) |
| 6959 | error ("%qT is not a template", d1); |
| 6960 | return error_mark_node; |
| 6961 | } |
| 6962 | |
| 6963 | if (TREE_CODE (templ) != TEMPLATE_DECL |
| 6964 | /* Make sure it's a user visible template, if it was named by |
| 6965 | the user. */ |
| 6966 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) |
| 6967 | && !PRIMARY_TEMPLATE_P (templ))) |
| 6968 | { |
| 6969 | if (complain & tf_error) |
| 6970 | { |
| 6971 | error ("non-template type %qT used as a template", d1); |
| 6972 | if (in_decl) |
| 6973 | error ("for template declaration %q+D", in_decl); |
| 6974 | } |
| 6975 | return error_mark_node; |
| 6976 | } |
| 6977 | |
| 6978 | complain &= ~tf_user; |
| 6979 | |
| 6980 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) |
| 6981 | { |
| 6982 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store |
| 6983 | template arguments */ |
| 6984 | |
| 6985 | tree parm; |
| 6986 | tree arglist2; |
| 6987 | tree outer; |
| 6988 | |
| 6989 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); |
| 6990 | |
| 6991 | /* Consider an example where a template template parameter declared as |
| 6992 | |
| 6993 | template <class T, class U = std::allocator<T> > class TT |
| 6994 | |
| 6995 | The template parameter level of T and U are one level larger than |
| 6996 | of TT. To proper process the default argument of U, say when an |
| 6997 | instantiation `TT<int>' is seen, we need to build the full |
| 6998 | arguments containing {int} as the innermost level. Outer levels, |
| 6999 | available when not appearing as default template argument, can be |
| 7000 | obtained from the arguments of the enclosing template. |
| 7001 | |
| 7002 | Suppose that TT is later substituted with std::vector. The above |
| 7003 | instantiation is `TT<int, std::allocator<T> >' with TT at |
| 7004 | level 1, and T at level 2, while the template arguments at level 1 |
| 7005 | becomes {std::vector} and the inner level 2 is {int}. */ |
| 7006 | |
| 7007 | outer = DECL_CONTEXT (templ); |
| 7008 | if (outer) |
| 7009 | outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer))); |
| 7010 | else if (current_template_parms) |
| 7011 | /* This is an argument of the current template, so we haven't set |
| 7012 | DECL_CONTEXT yet. */ |
| 7013 | outer = current_template_args (); |
| 7014 | |
| 7015 | if (outer) |
| 7016 | arglist = add_to_template_args (outer, arglist); |
| 7017 | |
| 7018 | arglist2 = coerce_template_parms (parmlist, arglist, templ, |
| 7019 | complain, |
| 7020 | /*require_all_args=*/true, |
| 7021 | /*use_default_args=*/true); |
| 7022 | if (arglist2 == error_mark_node |
| 7023 | || (!uses_template_parms (arglist2) |
| 7024 | && check_instantiated_args (templ, arglist2, complain))) |
| 7025 | return error_mark_node; |
| 7026 | |
| 7027 | parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); |
| 7028 | return parm; |
| 7029 | } |
| 7030 | else |
| 7031 | { |
| 7032 | tree template_type = TREE_TYPE (templ); |
| 7033 | tree gen_tmpl; |
| 7034 | tree type_decl; |
| 7035 | tree found = NULL_TREE; |
| 7036 | int arg_depth; |
| 7037 | int parm_depth; |
| 7038 | int is_dependent_type; |
| 7039 | int use_partial_inst_tmpl = false; |
| 7040 | |
| 7041 | if (template_type == error_mark_node) |
| 7042 | /* An error occured while building the template TEMPL, and a |
| 7043 | diagnostic has most certainly been emitted for that |
| 7044 | already. Let's propagate that error. */ |
| 7045 | return error_mark_node; |
| 7046 | |
| 7047 | gen_tmpl = most_general_template (templ); |
| 7048 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); |
| 7049 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
| 7050 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
| 7051 | |
| 7052 | if (arg_depth == 1 && parm_depth > 1) |
| 7053 | { |
| 7054 | /* We've been given an incomplete set of template arguments. |
| 7055 | For example, given: |
| 7056 | |
| 7057 | template <class T> struct S1 { |
| 7058 | template <class U> struct S2 {}; |
| 7059 | template <class U> struct S2<U*> {}; |
| 7060 | }; |
| 7061 | |
| 7062 | we will be called with an ARGLIST of `U*', but the |
| 7063 | TEMPLATE will be `template <class T> template |
| 7064 | <class U> struct S1<T>::S2'. We must fill in the missing |
| 7065 | arguments. */ |
| 7066 | arglist |
| 7067 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)), |
| 7068 | arglist); |
| 7069 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
| 7070 | } |
| 7071 | |
| 7072 | /* Now we should have enough arguments. */ |
| 7073 | gcc_assert (parm_depth == arg_depth); |
| 7074 | |
| 7075 | /* From here on, we're only interested in the most general |
| 7076 | template. */ |
| 7077 | |
| 7078 | /* Calculate the BOUND_ARGS. These will be the args that are |
| 7079 | actually tsubst'd into the definition to create the |
| 7080 | instantiation. */ |
| 7081 | if (parm_depth > 1) |
| 7082 | { |
| 7083 | /* We have multiple levels of arguments to coerce, at once. */ |
| 7084 | int i; |
| 7085 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
| 7086 | |
| 7087 | tree bound_args = make_tree_vec (parm_depth); |
| 7088 | |
| 7089 | for (i = saved_depth, |
| 7090 | t = DECL_TEMPLATE_PARMS (gen_tmpl); |
| 7091 | i > 0 && t != NULL_TREE; |
| 7092 | --i, t = TREE_CHAIN (t)) |
| 7093 | { |
| 7094 | tree a; |
| 7095 | if (i == saved_depth) |
| 7096 | a = coerce_template_parms (TREE_VALUE (t), |
| 7097 | arglist, gen_tmpl, |
| 7098 | complain, |
| 7099 | /*require_all_args=*/true, |
| 7100 | /*use_default_args=*/true); |
| 7101 | else |
| 7102 | /* Outer levels should have already been coerced. */ |
| 7103 | a = TMPL_ARGS_LEVEL (arglist, i); |
| 7104 | |
| 7105 | /* Don't process further if one of the levels fails. */ |
| 7106 | if (a == error_mark_node) |
| 7107 | { |
| 7108 | /* Restore the ARGLIST to its full size. */ |
| 7109 | TREE_VEC_LENGTH (arglist) = saved_depth; |
| 7110 | return error_mark_node; |
| 7111 | } |
| 7112 | |
| 7113 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
| 7114 | |
| 7115 | /* We temporarily reduce the length of the ARGLIST so |
| 7116 | that coerce_template_parms will see only the arguments |
| 7117 | corresponding to the template parameters it is |
| 7118 | examining. */ |
| 7119 | TREE_VEC_LENGTH (arglist)--; |
| 7120 | } |
| 7121 | |
| 7122 | /* Restore the ARGLIST to its full size. */ |
| 7123 | TREE_VEC_LENGTH (arglist) = saved_depth; |
| 7124 | |
| 7125 | arglist = bound_args; |
| 7126 | } |
| 7127 | else |
| 7128 | arglist |
| 7129 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), |
| 7130 | INNERMOST_TEMPLATE_ARGS (arglist), |
| 7131 | gen_tmpl, |
| 7132 | complain, |
| 7133 | /*require_all_args=*/true, |
| 7134 | /*use_default_args=*/true); |
| 7135 | |
| 7136 | if (arglist == error_mark_node) |
| 7137 | /* We were unable to bind the arguments. */ |
| 7138 | return error_mark_node; |
| 7139 | |
| 7140 | /* In the scope of a template class, explicit references to the |
| 7141 | template class refer to the type of the template, not any |
| 7142 | instantiation of it. For example, in: |
| 7143 | |
| 7144 | template <class T> class C { void f(C<T>); } |
| 7145 | |
| 7146 | the `C<T>' is just the same as `C'. Outside of the |
| 7147 | class, however, such a reference is an instantiation. */ |
| 7148 | if ((entering_scope |
| 7149 | || !PRIMARY_TEMPLATE_P (gen_tmpl) |
| 7150 | || currently_open_class (template_type)) |
| 7151 | /* comp_template_args is expensive, check it last. */ |
| 7152 | && comp_template_args (TYPE_TI_ARGS (template_type), |
| 7153 | arglist)) |
| 7154 | return template_type; |
| 7155 | |
| 7156 | /* If we already have this specialization, return it. */ |
| 7157 | elt.tmpl = gen_tmpl; |
| 7158 | elt.args = arglist; |
| 7159 | hash = hash_specialization (&elt); |
| 7160 | entry = (spec_entry *) htab_find_with_hash (type_specializations, |
| 7161 | &elt, hash); |
| 7162 | |
| 7163 | if (entry) |
| 7164 | return entry->spec; |
| 7165 | |
| 7166 | is_dependent_type = uses_template_parms (arglist); |
| 7167 | |
| 7168 | /* If the deduced arguments are invalid, then the binding |
| 7169 | failed. */ |
| 7170 | if (!is_dependent_type |
| 7171 | && check_instantiated_args (gen_tmpl, |
| 7172 | INNERMOST_TEMPLATE_ARGS (arglist), |
| 7173 | complain)) |
| 7174 | return error_mark_node; |
| 7175 | |
| 7176 | if (!is_dependent_type |
| 7177 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
| 7178 | && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl)) |
| 7179 | && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL) |
| 7180 | { |
| 7181 | found = xref_tag_from_type (TREE_TYPE (gen_tmpl), |
| 7182 | DECL_NAME (gen_tmpl), |
| 7183 | /*tag_scope=*/ts_global); |
| 7184 | return found; |
| 7185 | } |
| 7186 | |
| 7187 | context = tsubst (DECL_CONTEXT (gen_tmpl), arglist, |
| 7188 | complain, in_decl); |
| 7189 | if (context == error_mark_node) |
| 7190 | return error_mark_node; |
| 7191 | |
| 7192 | if (!context) |
| 7193 | context = global_namespace; |
| 7194 | |
| 7195 | /* Create the type. */ |
| 7196 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
| 7197 | { |
| 7198 | if (!is_dependent_type) |
| 7199 | { |
| 7200 | set_current_access_from_decl (TYPE_NAME (template_type)); |
| 7201 | t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE, |
| 7202 | tsubst (ENUM_UNDERLYING_TYPE (template_type), |
| 7203 | arglist, complain, in_decl), |
| 7204 | SCOPED_ENUM_P (template_type), NULL); |
| 7205 | } |
| 7206 | else |
| 7207 | { |
| 7208 | /* We don't want to call start_enum for this type, since |
| 7209 | the values for the enumeration constants may involve |
| 7210 | template parameters. And, no one should be interested |
| 7211 | in the enumeration constants for such a type. */ |
| 7212 | t = cxx_make_type (ENUMERAL_TYPE); |
| 7213 | SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); |
| 7214 | } |
| 7215 | SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type)); |
| 7216 | ENUM_FIXED_UNDERLYING_TYPE_P (t) |
| 7217 | = ENUM_FIXED_UNDERLYING_TYPE_P (template_type); |
| 7218 | } |
| 7219 | else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)) |
| 7220 | { |
| 7221 | /* The user referred to a specialization of an alias |
| 7222 | template represented by GEN_TMPL. |
| 7223 | |
| 7224 | [temp.alias]/2 says: |
| 7225 | |
| 7226 | When a template-id refers to the specialization of an |
| 7227 | alias template, it is equivalent to the associated |
| 7228 | type obtained by substitution of its |
| 7229 | template-arguments for the template-parameters in the |
| 7230 | type-id of the alias template. */ |
| 7231 | |
| 7232 | t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl); |
| 7233 | /* Note that the call above (by indirectly calling |
| 7234 | register_specialization in tsubst_decl) registers the |
| 7235 | TYPE_DECL representing the specialization of the alias |
| 7236 | template. So next time someone substitutes ARGLIST for |
| 7237 | the template parms into the alias template (GEN_TMPL), |
| 7238 | she'll get that TYPE_DECL back. */ |
| 7239 | |
| 7240 | if (t == error_mark_node) |
| 7241 | return t; |
| 7242 | } |
| 7243 | else if (CLASS_TYPE_P (template_type)) |
| 7244 | { |
| 7245 | t = make_class_type (TREE_CODE (template_type)); |
| 7246 | CLASSTYPE_DECLARED_CLASS (t) |
| 7247 | = CLASSTYPE_DECLARED_CLASS (template_type); |
| 7248 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
| 7249 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
| 7250 | |
| 7251 | /* A local class. Make sure the decl gets registered properly. */ |
| 7252 | if (context == current_function_decl) |
| 7253 | pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_global); |
| 7254 | |
| 7255 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) |
| 7256 | /* This instantiation is another name for the primary |
| 7257 | template type. Set the TYPE_CANONICAL field |
| 7258 | appropriately. */ |
| 7259 | TYPE_CANONICAL (t) = template_type; |
| 7260 | else if (any_template_arguments_need_structural_equality_p (arglist)) |
| 7261 | /* Some of the template arguments require structural |
| 7262 | equality testing, so this template class requires |
| 7263 | structural equality testing. */ |
| 7264 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
| 7265 | } |
| 7266 | else |
| 7267 | gcc_unreachable (); |
| 7268 | |
| 7269 | /* If we called start_enum or pushtag above, this information |
| 7270 | will already be set up. */ |
| 7271 | if (!TYPE_NAME (t)) |
| 7272 | { |
| 7273 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
| 7274 | |
| 7275 | type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t); |
| 7276 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
| 7277 | DECL_SOURCE_LOCATION (type_decl) |
| 7278 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
| 7279 | } |
| 7280 | else |
| 7281 | type_decl = TYPE_NAME (t); |
| 7282 | |
| 7283 | if (CLASS_TYPE_P (template_type)) |
| 7284 | { |
| 7285 | TREE_PRIVATE (type_decl) |
| 7286 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); |
| 7287 | TREE_PROTECTED (type_decl) |
| 7288 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); |
| 7289 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
| 7290 | { |
| 7291 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; |
| 7292 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); |
| 7293 | } |
| 7294 | } |
| 7295 | |
| 7296 | /* Let's consider the explicit specialization of a member |
| 7297 | of a class template specialization that is implicitely instantiated, |
| 7298 | e.g.: |
| 7299 | template<class T> |
| 7300 | struct S |
| 7301 | { |
| 7302 | template<class U> struct M {}; //#0 |
| 7303 | }; |
| 7304 | |
| 7305 | template<> |
| 7306 | template<> |
| 7307 | struct S<int>::M<char> //#1 |
| 7308 | { |
| 7309 | int i; |
| 7310 | }; |
| 7311 | [temp.expl.spec]/4 says this is valid. |
| 7312 | |
| 7313 | In this case, when we write: |
| 7314 | S<int>::M<char> m; |
| 7315 | |
| 7316 | M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from |
| 7317 | the one of #0. |
| 7318 | |
| 7319 | When we encounter #1, we want to store the partial instantiation |
| 7320 | of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE. |
| 7321 | |
| 7322 | For all cases other than this "explicit specialization of member of a |
| 7323 | class template", we just want to store the most general template into |
| 7324 | the CLASSTYPE_TI_TEMPLATE of M. |
| 7325 | |
| 7326 | This case of "explicit specialization of member of a class template" |
| 7327 | only happens when: |
| 7328 | 1/ the enclosing class is an instantiation of, and therefore not |
| 7329 | the same as, the context of the most general template, and |
| 7330 | 2/ we aren't looking at the partial instantiation itself, i.e. |
| 7331 | the innermost arguments are not the same as the innermost parms of |
| 7332 | the most general template. |
| 7333 | |
| 7334 | So it's only when 1/ and 2/ happens that we want to use the partial |
| 7335 | instantiation of the member template in lieu of its most general |
| 7336 | template. */ |
| 7337 | |
| 7338 | if (PRIMARY_TEMPLATE_P (gen_tmpl) |
| 7339 | && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist) |
| 7340 | /* the enclosing class must be an instantiation... */ |
| 7341 | && CLASS_TYPE_P (context) |
| 7342 | && !same_type_p (context, DECL_CONTEXT (gen_tmpl))) |
| 7343 | { |
| 7344 | tree partial_inst_args; |
| 7345 | TREE_VEC_LENGTH (arglist)--; |
| 7346 | ++processing_template_decl; |
| 7347 | partial_inst_args = |
| 7348 | tsubst (INNERMOST_TEMPLATE_ARGS |
| 7349 | (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))), |
| 7350 | arglist, complain, NULL_TREE); |
| 7351 | --processing_template_decl; |
| 7352 | TREE_VEC_LENGTH (arglist)++; |
| 7353 | use_partial_inst_tmpl = |
| 7354 | /*...and we must not be looking at the partial instantiation |
| 7355 | itself. */ |
| 7356 | !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist), |
| 7357 | partial_inst_args); |
| 7358 | } |
| 7359 | |
| 7360 | if (!use_partial_inst_tmpl) |
| 7361 | /* This case is easy; there are no member templates involved. */ |
| 7362 | found = gen_tmpl; |
| 7363 | else |
| 7364 | { |
| 7365 | /* This is a full instantiation of a member template. Find |
| 7366 | the partial instantiation of which this is an instance. */ |
| 7367 | |
| 7368 | /* Temporarily reduce by one the number of levels in the ARGLIST |
| 7369 | so as to avoid comparing the last set of arguments. */ |
| 7370 | TREE_VEC_LENGTH (arglist)--; |
| 7371 | found = tsubst (gen_tmpl, arglist, complain, NULL_TREE); |
| 7372 | TREE_VEC_LENGTH (arglist)++; |
| 7373 | /* FOUND is either a proper class type, or an alias |
| 7374 | template specialization. In the later case, it's a |
| 7375 | TYPE_DECL, resulting from the substituting of arguments |
| 7376 | for parameters in the TYPE_DECL of the alias template |
| 7377 | done earlier. So be careful while getting the template |
| 7378 | of FOUND. */ |
| 7379 | found = TREE_CODE (found) == TYPE_DECL |
| 7380 | ? TYPE_TI_TEMPLATE (TREE_TYPE (found)) |
| 7381 | : CLASSTYPE_TI_TEMPLATE (found); |
| 7382 | } |
| 7383 | |
| 7384 | SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist)); |
| 7385 | |
| 7386 | elt.spec = t; |
| 7387 | slot = htab_find_slot_with_hash (type_specializations, |
| 7388 | &elt, hash, INSERT); |
| 7389 | entry = ggc_alloc_spec_entry (); |
| 7390 | *entry = elt; |
| 7391 | *slot = entry; |
| 7392 | |
| 7393 | /* Note this use of the partial instantiation so we can check it |
| 7394 | later in maybe_process_partial_specialization. */ |
| 7395 | DECL_TEMPLATE_INSTANTIATIONS (templ) |
| 7396 | = tree_cons (arglist, t, |
| 7397 | DECL_TEMPLATE_INSTANTIATIONS (templ)); |
| 7398 | |
| 7399 | if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type) |
| 7400 | /* Now that the type has been registered on the instantiations |
| 7401 | list, we set up the enumerators. Because the enumeration |
| 7402 | constants may involve the enumeration type itself, we make |
| 7403 | sure to register the type first, and then create the |
| 7404 | constants. That way, doing tsubst_expr for the enumeration |
| 7405 | constants won't result in recursive calls here; we'll find |
| 7406 | the instantiation and exit above. */ |
| 7407 | tsubst_enum (template_type, t, arglist); |
| 7408 | |
| 7409 | if (CLASS_TYPE_P (template_type) && is_dependent_type) |
| 7410 | /* If the type makes use of template parameters, the |
| 7411 | code that generates debugging information will crash. */ |
| 7412 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; |
| 7413 | |
| 7414 | /* Possibly limit visibility based on template args. */ |
| 7415 | TREE_PUBLIC (type_decl) = 1; |
| 7416 | determine_visibility (type_decl); |
| 7417 | |
| 7418 | return t; |
| 7419 | } |
| 7420 | } |
| 7421 | |
| 7422 | /* Wrapper for lookup_template_class_1. */ |
| 7423 | |
| 7424 | tree |
| 7425 | lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, |
| 7426 | int entering_scope, tsubst_flags_t complain) |
| 7427 | { |
| 7428 | tree ret; |
| 7429 | timevar_push (TV_TEMPLATE_INST); |
| 7430 | ret = lookup_template_class_1 (d1, arglist, in_decl, context, |
| 7431 | entering_scope, complain); |
| 7432 | timevar_pop (TV_TEMPLATE_INST); |
| 7433 | return ret; |
| 7434 | } |
| 7435 | \f |
| 7436 | struct pair_fn_data |
| 7437 | { |
| 7438 | tree_fn_t fn; |
| 7439 | void *data; |
| 7440 | /* True when we should also visit template parameters that occur in |
| 7441 | non-deduced contexts. */ |
| 7442 | bool include_nondeduced_p; |
| 7443 | struct pointer_set_t *visited; |
| 7444 | }; |
| 7445 | |
| 7446 | /* Called from for_each_template_parm via walk_tree. */ |
| 7447 | |
| 7448 | static tree |
| 7449 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
| 7450 | { |
| 7451 | tree t = *tp; |
| 7452 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; |
| 7453 | tree_fn_t fn = pfd->fn; |
| 7454 | void *data = pfd->data; |
| 7455 | |
| 7456 | if (TYPE_P (t) |
| 7457 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE) |
| 7458 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited, |
| 7459 | pfd->include_nondeduced_p)) |
| 7460 | return error_mark_node; |
| 7461 | |
| 7462 | switch (TREE_CODE (t)) |
| 7463 | { |
| 7464 | case RECORD_TYPE: |
| 7465 | if (TYPE_PTRMEMFUNC_P (t)) |
| 7466 | break; |
| 7467 | /* Fall through. */ |
| 7468 | |
| 7469 | case UNION_TYPE: |
| 7470 | case ENUMERAL_TYPE: |
| 7471 | if (!TYPE_TEMPLATE_INFO (t)) |
| 7472 | *walk_subtrees = 0; |
| 7473 | else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)), |
| 7474 | fn, data, pfd->visited, |
| 7475 | pfd->include_nondeduced_p)) |
| 7476 | return error_mark_node; |
| 7477 | break; |
| 7478 | |
| 7479 | case INTEGER_TYPE: |
| 7480 | if (for_each_template_parm (TYPE_MIN_VALUE (t), |
| 7481 | fn, data, pfd->visited, |
| 7482 | pfd->include_nondeduced_p) |
| 7483 | || for_each_template_parm (TYPE_MAX_VALUE (t), |
| 7484 | fn, data, pfd->visited, |
| 7485 | pfd->include_nondeduced_p)) |
| 7486 | return error_mark_node; |
| 7487 | break; |
| 7488 | |
| 7489 | case METHOD_TYPE: |
| 7490 | /* Since we're not going to walk subtrees, we have to do this |
| 7491 | explicitly here. */ |
| 7492 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
| 7493 | pfd->visited, pfd->include_nondeduced_p)) |
| 7494 | return error_mark_node; |
| 7495 | /* Fall through. */ |
| 7496 | |
| 7497 | case FUNCTION_TYPE: |
| 7498 | /* Check the return type. */ |
| 7499 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
| 7500 | pfd->include_nondeduced_p)) |
| 7501 | return error_mark_node; |
| 7502 | |
| 7503 | /* Check the parameter types. Since default arguments are not |
| 7504 | instantiated until they are needed, the TYPE_ARG_TYPES may |
| 7505 | contain expressions that involve template parameters. But, |
| 7506 | no-one should be looking at them yet. And, once they're |
| 7507 | instantiated, they don't contain template parameters, so |
| 7508 | there's no point in looking at them then, either. */ |
| 7509 | { |
| 7510 | tree parm; |
| 7511 | |
| 7512 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) |
| 7513 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
| 7514 | pfd->visited, pfd->include_nondeduced_p)) |
| 7515 | return error_mark_node; |
| 7516 | |
| 7517 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
| 7518 | want walk_tree walking into them itself. */ |
| 7519 | *walk_subtrees = 0; |
| 7520 | } |
| 7521 | break; |
| 7522 | |
| 7523 | case TYPEOF_TYPE: |
| 7524 | case UNDERLYING_TYPE: |
| 7525 | if (pfd->include_nondeduced_p |
| 7526 | && for_each_template_parm (TYPE_FIELDS (t), fn, data, |
| 7527 | pfd->visited, |
| 7528 | pfd->include_nondeduced_p)) |
| 7529 | return error_mark_node; |
| 7530 | break; |
| 7531 | |
| 7532 | case FUNCTION_DECL: |
| 7533 | case VAR_DECL: |
| 7534 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
| 7535 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
| 7536 | pfd->visited, pfd->include_nondeduced_p)) |
| 7537 | return error_mark_node; |
| 7538 | /* Fall through. */ |
| 7539 | |
| 7540 | case PARM_DECL: |
| 7541 | case CONST_DECL: |
| 7542 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) |
| 7543 | && for_each_template_parm (DECL_INITIAL (t), fn, data, |
| 7544 | pfd->visited, pfd->include_nondeduced_p)) |
| 7545 | return error_mark_node; |
| 7546 | if (DECL_CONTEXT (t) |
| 7547 | && pfd->include_nondeduced_p |
| 7548 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
| 7549 | pfd->visited, pfd->include_nondeduced_p)) |
| 7550 | return error_mark_node; |
| 7551 | break; |
| 7552 | |
| 7553 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 7554 | /* Record template parameters such as `T' inside `TT<T>'. */ |
| 7555 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited, |
| 7556 | pfd->include_nondeduced_p)) |
| 7557 | return error_mark_node; |
| 7558 | /* Fall through. */ |
| 7559 | |
| 7560 | case TEMPLATE_TEMPLATE_PARM: |
| 7561 | case TEMPLATE_TYPE_PARM: |
| 7562 | case TEMPLATE_PARM_INDEX: |
| 7563 | if (fn && (*fn)(t, data)) |
| 7564 | return error_mark_node; |
| 7565 | else if (!fn) |
| 7566 | return error_mark_node; |
| 7567 | break; |
| 7568 | |
| 7569 | case TEMPLATE_DECL: |
| 7570 | /* A template template parameter is encountered. */ |
| 7571 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
| 7572 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited, |
| 7573 | pfd->include_nondeduced_p)) |
| 7574 | return error_mark_node; |
| 7575 | |
| 7576 | /* Already substituted template template parameter */ |
| 7577 | *walk_subtrees = 0; |
| 7578 | break; |
| 7579 | |
| 7580 | case TYPENAME_TYPE: |
| 7581 | if (!fn |
| 7582 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
| 7583 | data, pfd->visited, |
| 7584 | pfd->include_nondeduced_p)) |
| 7585 | return error_mark_node; |
| 7586 | break; |
| 7587 | |
| 7588 | case CONSTRUCTOR: |
| 7589 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) |
| 7590 | && pfd->include_nondeduced_p |
| 7591 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
| 7592 | (TREE_TYPE (t)), fn, data, |
| 7593 | pfd->visited, pfd->include_nondeduced_p)) |
| 7594 | return error_mark_node; |
| 7595 | break; |
| 7596 | |
| 7597 | case INDIRECT_REF: |
| 7598 | case COMPONENT_REF: |
| 7599 | /* If there's no type, then this thing must be some expression |
| 7600 | involving template parameters. */ |
| 7601 | if (!fn && !TREE_TYPE (t)) |
| 7602 | return error_mark_node; |
| 7603 | break; |
| 7604 | |
| 7605 | case MODOP_EXPR: |
| 7606 | case CAST_EXPR: |
| 7607 | case IMPLICIT_CONV_EXPR: |
| 7608 | case REINTERPRET_CAST_EXPR: |
| 7609 | case CONST_CAST_EXPR: |
| 7610 | case STATIC_CAST_EXPR: |
| 7611 | case DYNAMIC_CAST_EXPR: |
| 7612 | case ARROW_EXPR: |
| 7613 | case DOTSTAR_EXPR: |
| 7614 | case TYPEID_EXPR: |
| 7615 | case PSEUDO_DTOR_EXPR: |
| 7616 | if (!fn) |
| 7617 | return error_mark_node; |
| 7618 | break; |
| 7619 | |
| 7620 | default: |
| 7621 | break; |
| 7622 | } |
| 7623 | |
| 7624 | /* We didn't find any template parameters we liked. */ |
| 7625 | return NULL_TREE; |
| 7626 | } |
| 7627 | |
| 7628 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
| 7629 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, |
| 7630 | call FN with the parameter and the DATA. |
| 7631 | If FN returns nonzero, the iteration is terminated, and |
| 7632 | for_each_template_parm returns 1. Otherwise, the iteration |
| 7633 | continues. If FN never returns a nonzero value, the value |
| 7634 | returned by for_each_template_parm is 0. If FN is NULL, it is |
| 7635 | considered to be the function which always returns 1. |
| 7636 | |
| 7637 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template |
| 7638 | parameters that occur in non-deduced contexts. When false, only |
| 7639 | visits those template parameters that can be deduced. */ |
| 7640 | |
| 7641 | static int |
| 7642 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
| 7643 | struct pointer_set_t *visited, |
| 7644 | bool include_nondeduced_p) |
| 7645 | { |
| 7646 | struct pair_fn_data pfd; |
| 7647 | int result; |
| 7648 | |
| 7649 | /* Set up. */ |
| 7650 | pfd.fn = fn; |
| 7651 | pfd.data = data; |
| 7652 | pfd.include_nondeduced_p = include_nondeduced_p; |
| 7653 | |
| 7654 | /* Walk the tree. (Conceptually, we would like to walk without |
| 7655 | duplicates, but for_each_template_parm_r recursively calls |
| 7656 | for_each_template_parm, so we would need to reorganize a fair |
| 7657 | bit to use walk_tree_without_duplicates, so we keep our own |
| 7658 | visited list.) */ |
| 7659 | if (visited) |
| 7660 | pfd.visited = visited; |
| 7661 | else |
| 7662 | pfd.visited = pointer_set_create (); |
| 7663 | result = cp_walk_tree (&t, |
| 7664 | for_each_template_parm_r, |
| 7665 | &pfd, |
| 7666 | pfd.visited) != NULL_TREE; |
| 7667 | |
| 7668 | /* Clean up. */ |
| 7669 | if (!visited) |
| 7670 | { |
| 7671 | pointer_set_destroy (pfd.visited); |
| 7672 | pfd.visited = 0; |
| 7673 | } |
| 7674 | |
| 7675 | return result; |
| 7676 | } |
| 7677 | |
| 7678 | /* Returns true if T depends on any template parameter. */ |
| 7679 | |
| 7680 | int |
| 7681 | uses_template_parms (tree t) |
| 7682 | { |
| 7683 | bool dependent_p; |
| 7684 | int saved_processing_template_decl; |
| 7685 | |
| 7686 | saved_processing_template_decl = processing_template_decl; |
| 7687 | if (!saved_processing_template_decl) |
| 7688 | processing_template_decl = 1; |
| 7689 | if (TYPE_P (t)) |
| 7690 | dependent_p = dependent_type_p (t); |
| 7691 | else if (TREE_CODE (t) == TREE_VEC) |
| 7692 | dependent_p = any_dependent_template_arguments_p (t); |
| 7693 | else if (TREE_CODE (t) == TREE_LIST) |
| 7694 | dependent_p = (uses_template_parms (TREE_VALUE (t)) |
| 7695 | || uses_template_parms (TREE_CHAIN (t))); |
| 7696 | else if (TREE_CODE (t) == TYPE_DECL) |
| 7697 | dependent_p = dependent_type_p (TREE_TYPE (t)); |
| 7698 | else if (DECL_P (t) |
| 7699 | || EXPR_P (t) |
| 7700 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
| 7701 | || TREE_CODE (t) == OVERLOAD |
| 7702 | || BASELINK_P (t) |
| 7703 | || TREE_CODE (t) == IDENTIFIER_NODE |
| 7704 | || TREE_CODE (t) == TRAIT_EXPR |
| 7705 | || TREE_CODE (t) == CONSTRUCTOR |
| 7706 | || CONSTANT_CLASS_P (t)) |
| 7707 | dependent_p = (type_dependent_expression_p (t) |
| 7708 | || value_dependent_expression_p (t)); |
| 7709 | else |
| 7710 | { |
| 7711 | gcc_assert (t == error_mark_node); |
| 7712 | dependent_p = false; |
| 7713 | } |
| 7714 | |
| 7715 | processing_template_decl = saved_processing_template_decl; |
| 7716 | |
| 7717 | return dependent_p; |
| 7718 | } |
| 7719 | |
| 7720 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
| 7721 | |
| 7722 | int |
| 7723 | uses_template_parms_level (tree t, int level) |
| 7724 | { |
| 7725 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, |
| 7726 | /*include_nondeduced_p=*/true); |
| 7727 | } |
| 7728 | |
| 7729 | /* Returns TRUE iff INST is an instantiation we don't need to do in an |
| 7730 | ill-formed translation unit, i.e. a variable or function that isn't |
| 7731 | usable in a constant expression. */ |
| 7732 | |
| 7733 | static inline bool |
| 7734 | neglectable_inst_p (tree d) |
| 7735 | { |
| 7736 | return (DECL_P (d) |
| 7737 | && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d) |
| 7738 | : decl_maybe_constant_var_p (d))); |
| 7739 | } |
| 7740 | |
| 7741 | /* Returns TRUE iff we should refuse to instantiate DECL because it's |
| 7742 | neglectable and instantiated from within an erroneous instantiation. */ |
| 7743 | |
| 7744 | static bool |
| 7745 | limit_bad_template_recursion (tree decl) |
| 7746 | { |
| 7747 | struct tinst_level *lev = current_tinst_level; |
| 7748 | int errs = errorcount + sorrycount; |
| 7749 | if (lev == NULL || errs == 0 || !neglectable_inst_p (decl)) |
| 7750 | return false; |
| 7751 | |
| 7752 | for (; lev; lev = lev->next) |
| 7753 | if (neglectable_inst_p (lev->decl)) |
| 7754 | break; |
| 7755 | |
| 7756 | return (lev && errs > lev->errors); |
| 7757 | } |
| 7758 | |
| 7759 | static int tinst_depth; |
| 7760 | extern int max_tinst_depth; |
| 7761 | #ifdef GATHER_STATISTICS |
| 7762 | int depth_reached; |
| 7763 | #endif |
| 7764 | static GTY(()) struct tinst_level *last_error_tinst_level; |
| 7765 | |
| 7766 | /* We're starting to instantiate D; record the template instantiation context |
| 7767 | for diagnostics and to restore it later. */ |
| 7768 | |
| 7769 | int |
| 7770 | push_tinst_level (tree d) |
| 7771 | { |
| 7772 | struct tinst_level *new_level; |
| 7773 | |
| 7774 | if (tinst_depth >= max_tinst_depth) |
| 7775 | { |
| 7776 | last_error_tinst_level = current_tinst_level; |
| 7777 | if (TREE_CODE (d) == TREE_LIST) |
| 7778 | error ("template instantiation depth exceeds maximum of %d (use " |
| 7779 | "-ftemplate-depth= to increase the maximum) substituting %qS", |
| 7780 | max_tinst_depth, d); |
| 7781 | else |
| 7782 | error ("template instantiation depth exceeds maximum of %d (use " |
| 7783 | "-ftemplate-depth= to increase the maximum) instantiating %qD", |
| 7784 | max_tinst_depth, d); |
| 7785 | |
| 7786 | print_instantiation_context (); |
| 7787 | |
| 7788 | return 0; |
| 7789 | } |
| 7790 | |
| 7791 | /* If the current instantiation caused problems, don't let it instantiate |
| 7792 | anything else. Do allow deduction substitution and decls usable in |
| 7793 | constant expressions. */ |
| 7794 | if (limit_bad_template_recursion (d)) |
| 7795 | return 0; |
| 7796 | |
| 7797 | new_level = ggc_alloc_tinst_level (); |
| 7798 | new_level->decl = d; |
| 7799 | new_level->locus = input_location; |
| 7800 | new_level->errors = errorcount+sorrycount; |
| 7801 | new_level->in_system_header_p = in_system_header; |
| 7802 | new_level->next = current_tinst_level; |
| 7803 | current_tinst_level = new_level; |
| 7804 | |
| 7805 | ++tinst_depth; |
| 7806 | #ifdef GATHER_STATISTICS |
| 7807 | if (tinst_depth > depth_reached) |
| 7808 | depth_reached = tinst_depth; |
| 7809 | #endif |
| 7810 | |
| 7811 | return 1; |
| 7812 | } |
| 7813 | |
| 7814 | /* We're done instantiating this template; return to the instantiation |
| 7815 | context. */ |
| 7816 | |
| 7817 | void |
| 7818 | pop_tinst_level (void) |
| 7819 | { |
| 7820 | /* Restore the filename and line number stashed away when we started |
| 7821 | this instantiation. */ |
| 7822 | input_location = current_tinst_level->locus; |
| 7823 | current_tinst_level = current_tinst_level->next; |
| 7824 | --tinst_depth; |
| 7825 | } |
| 7826 | |
| 7827 | /* We're instantiating a deferred template; restore the template |
| 7828 | instantiation context in which the instantiation was requested, which |
| 7829 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
| 7830 | |
| 7831 | static tree |
| 7832 | reopen_tinst_level (struct tinst_level *level) |
| 7833 | { |
| 7834 | struct tinst_level *t; |
| 7835 | |
| 7836 | tinst_depth = 0; |
| 7837 | for (t = level; t; t = t->next) |
| 7838 | ++tinst_depth; |
| 7839 | |
| 7840 | current_tinst_level = level; |
| 7841 | pop_tinst_level (); |
| 7842 | if (current_tinst_level) |
| 7843 | current_tinst_level->errors = errorcount+sorrycount; |
| 7844 | return level->decl; |
| 7845 | } |
| 7846 | |
| 7847 | /* Returns the TINST_LEVEL which gives the original instantiation |
| 7848 | context. */ |
| 7849 | |
| 7850 | struct tinst_level * |
| 7851 | outermost_tinst_level (void) |
| 7852 | { |
| 7853 | struct tinst_level *level = current_tinst_level; |
| 7854 | if (level) |
| 7855 | while (level->next) |
| 7856 | level = level->next; |
| 7857 | return level; |
| 7858 | } |
| 7859 | |
| 7860 | /* Returns TRUE if PARM is a parameter of the template TEMPL. */ |
| 7861 | |
| 7862 | bool |
| 7863 | parameter_of_template_p (tree parm, tree templ) |
| 7864 | { |
| 7865 | tree parms; |
| 7866 | int i; |
| 7867 | |
| 7868 | if (!parm || !templ) |
| 7869 | return false; |
| 7870 | |
| 7871 | gcc_assert (DECL_TEMPLATE_PARM_P (parm)); |
| 7872 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); |
| 7873 | |
| 7874 | parms = DECL_TEMPLATE_PARMS (templ); |
| 7875 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
| 7876 | |
| 7877 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
| 7878 | { |
| 7879 | tree p = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
| 7880 | if (p == error_mark_node) |
| 7881 | continue; |
| 7882 | |
| 7883 | if (parm == p |
| 7884 | || (DECL_INITIAL (parm) |
| 7885 | && DECL_INITIAL (parm) == DECL_INITIAL (p))) |
| 7886 | return true; |
| 7887 | } |
| 7888 | |
| 7889 | return false; |
| 7890 | } |
| 7891 | |
| 7892 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
| 7893 | vector of template arguments, as for tsubst. |
| 7894 | |
| 7895 | Returns an appropriate tsubst'd friend declaration. */ |
| 7896 | |
| 7897 | static tree |
| 7898 | tsubst_friend_function (tree decl, tree args) |
| 7899 | { |
| 7900 | tree new_friend; |
| 7901 | |
| 7902 | if (TREE_CODE (decl) == FUNCTION_DECL |
| 7903 | && DECL_TEMPLATE_INSTANTIATION (decl) |
| 7904 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
| 7905 | /* This was a friend declared with an explicit template |
| 7906 | argument list, e.g.: |
| 7907 | |
| 7908 | friend void f<>(T); |
| 7909 | |
| 7910 | to indicate that f was a template instantiation, not a new |
| 7911 | function declaration. Now, we have to figure out what |
| 7912 | instantiation of what template. */ |
| 7913 | { |
| 7914 | tree template_id, arglist, fns; |
| 7915 | tree new_args; |
| 7916 | tree tmpl; |
| 7917 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
| 7918 | |
| 7919 | /* Friend functions are looked up in the containing namespace scope. |
| 7920 | We must enter that scope, to avoid finding member functions of the |
| 7921 | current class with same name. */ |
| 7922 | push_nested_namespace (ns); |
| 7923 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, |
| 7924 | tf_warning_or_error, NULL_TREE, |
| 7925 | /*integral_constant_expression_p=*/false); |
| 7926 | pop_nested_namespace (ns); |
| 7927 | arglist = tsubst (DECL_TI_ARGS (decl), args, |
| 7928 | tf_warning_or_error, NULL_TREE); |
| 7929 | template_id = lookup_template_function (fns, arglist); |
| 7930 | |
| 7931 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
| 7932 | tmpl = determine_specialization (template_id, new_friend, |
| 7933 | &new_args, |
| 7934 | /*need_member_template=*/0, |
| 7935 | TREE_VEC_LENGTH (args), |
| 7936 | tsk_none); |
| 7937 | return instantiate_template (tmpl, new_args, tf_error); |
| 7938 | } |
| 7939 | |
| 7940 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
| 7941 | |
| 7942 | /* The NEW_FRIEND will look like an instantiation, to the |
| 7943 | compiler, but is not an instantiation from the point of view of |
| 7944 | the language. For example, we might have had: |
| 7945 | |
| 7946 | template <class T> struct S { |
| 7947 | template <class U> friend void f(T, U); |
| 7948 | }; |
| 7949 | |
| 7950 | Then, in S<int>, template <class U> void f(int, U) is not an |
| 7951 | instantiation of anything. */ |
| 7952 | if (new_friend == error_mark_node) |
| 7953 | return error_mark_node; |
| 7954 | |
| 7955 | DECL_USE_TEMPLATE (new_friend) = 0; |
| 7956 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
| 7957 | { |
| 7958 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; |
| 7959 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) |
| 7960 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); |
| 7961 | } |
| 7962 | |
| 7963 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
| 7964 | is not a template instantiation and should not be mangled like |
| 7965 | one. Therefore, we forget the mangling here; we'll recompute it |
| 7966 | later if we need it. */ |
| 7967 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
| 7968 | { |
| 7969 | SET_DECL_RTL (new_friend, NULL); |
| 7970 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
| 7971 | } |
| 7972 | |
| 7973 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
| 7974 | { |
| 7975 | tree old_decl; |
| 7976 | tree new_friend_template_info; |
| 7977 | tree new_friend_result_template_info; |
| 7978 | tree ns; |
| 7979 | int new_friend_is_defn; |
| 7980 | |
| 7981 | /* We must save some information from NEW_FRIEND before calling |
| 7982 | duplicate decls since that function will free NEW_FRIEND if |
| 7983 | possible. */ |
| 7984 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); |
| 7985 | new_friend_is_defn = |
| 7986 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
| 7987 | (template_for_substitution (new_friend))) |
| 7988 | != NULL_TREE); |
| 7989 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
| 7990 | { |
| 7991 | /* This declaration is a `primary' template. */ |
| 7992 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; |
| 7993 | |
| 7994 | new_friend_result_template_info |
| 7995 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
| 7996 | } |
| 7997 | else |
| 7998 | new_friend_result_template_info = NULL_TREE; |
| 7999 | |
| 8000 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
| 8001 | if (new_friend_is_defn) |
| 8002 | DECL_INITIAL (new_friend) = error_mark_node; |
| 8003 | |
| 8004 | /* Inside pushdecl_namespace_level, we will push into the |
| 8005 | current namespace. However, the friend function should go |
| 8006 | into the namespace of the template. */ |
| 8007 | ns = decl_namespace_context (new_friend); |
| 8008 | push_nested_namespace (ns); |
| 8009 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
| 8010 | pop_nested_namespace (ns); |
| 8011 | |
| 8012 | if (old_decl == error_mark_node) |
| 8013 | return error_mark_node; |
| 8014 | |
| 8015 | if (old_decl != new_friend) |
| 8016 | { |
| 8017 | /* This new friend declaration matched an existing |
| 8018 | declaration. For example, given: |
| 8019 | |
| 8020 | template <class T> void f(T); |
| 8021 | template <class U> class C { |
| 8022 | template <class T> friend void f(T) {} |
| 8023 | }; |
| 8024 | |
| 8025 | the friend declaration actually provides the definition |
| 8026 | of `f', once C has been instantiated for some type. So, |
| 8027 | old_decl will be the out-of-class template declaration, |
| 8028 | while new_friend is the in-class definition. |
| 8029 | |
| 8030 | But, if `f' was called before this point, the |
| 8031 | instantiation of `f' will have DECL_TI_ARGS corresponding |
| 8032 | to `T' but not to `U', references to which might appear |
| 8033 | in the definition of `f'. Previously, the most general |
| 8034 | template for an instantiation of `f' was the out-of-class |
| 8035 | version; now it is the in-class version. Therefore, we |
| 8036 | run through all specialization of `f', adding to their |
| 8037 | DECL_TI_ARGS appropriately. In particular, they need a |
| 8038 | new set of outer arguments, corresponding to the |
| 8039 | arguments for this class instantiation. |
| 8040 | |
| 8041 | The same situation can arise with something like this: |
| 8042 | |
| 8043 | friend void f(int); |
| 8044 | template <class T> class C { |
| 8045 | friend void f(T) {} |
| 8046 | }; |
| 8047 | |
| 8048 | when `C<int>' is instantiated. Now, `f(int)' is defined |
| 8049 | in the class. */ |
| 8050 | |
| 8051 | if (!new_friend_is_defn) |
| 8052 | /* On the other hand, if the in-class declaration does |
| 8053 | *not* provide a definition, then we don't want to alter |
| 8054 | existing definitions. We can just leave everything |
| 8055 | alone. */ |
| 8056 | ; |
| 8057 | else |
| 8058 | { |
| 8059 | tree new_template = TI_TEMPLATE (new_friend_template_info); |
| 8060 | tree new_args = TI_ARGS (new_friend_template_info); |
| 8061 | |
| 8062 | /* Overwrite whatever template info was there before, if |
| 8063 | any, with the new template information pertaining to |
| 8064 | the declaration. */ |
| 8065 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; |
| 8066 | |
| 8067 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) |
| 8068 | { |
| 8069 | /* We should have called reregister_specialization in |
| 8070 | duplicate_decls. */ |
| 8071 | gcc_assert (retrieve_specialization (new_template, |
| 8072 | new_args, 0) |
| 8073 | == old_decl); |
| 8074 | |
| 8075 | /* Instantiate it if the global has already been used. */ |
| 8076 | if (DECL_ODR_USED (old_decl)) |
| 8077 | instantiate_decl (old_decl, /*defer_ok=*/true, |
| 8078 | /*expl_inst_class_mem_p=*/false); |
| 8079 | } |
| 8080 | else |
| 8081 | { |
| 8082 | tree t; |
| 8083 | |
| 8084 | /* Indicate that the old function template is a partial |
| 8085 | instantiation. */ |
| 8086 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
| 8087 | = new_friend_result_template_info; |
| 8088 | |
| 8089 | gcc_assert (new_template |
| 8090 | == most_general_template (new_template)); |
| 8091 | gcc_assert (new_template != old_decl); |
| 8092 | |
| 8093 | /* Reassign any specializations already in the hash table |
| 8094 | to the new more general template, and add the |
| 8095 | additional template args. */ |
| 8096 | for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl); |
| 8097 | t != NULL_TREE; |
| 8098 | t = TREE_CHAIN (t)) |
| 8099 | { |
| 8100 | tree spec = TREE_VALUE (t); |
| 8101 | spec_entry elt; |
| 8102 | |
| 8103 | elt.tmpl = old_decl; |
| 8104 | elt.args = DECL_TI_ARGS (spec); |
| 8105 | elt.spec = NULL_TREE; |
| 8106 | |
| 8107 | htab_remove_elt (decl_specializations, &elt); |
| 8108 | |
| 8109 | DECL_TI_ARGS (spec) |
| 8110 | = add_outermost_template_args (new_args, |
| 8111 | DECL_TI_ARGS (spec)); |
| 8112 | |
| 8113 | register_specialization |
| 8114 | (spec, new_template, DECL_TI_ARGS (spec), true, 0); |
| 8115 | |
| 8116 | } |
| 8117 | DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE; |
| 8118 | } |
| 8119 | } |
| 8120 | |
| 8121 | /* The information from NEW_FRIEND has been merged into OLD_DECL |
| 8122 | by duplicate_decls. */ |
| 8123 | new_friend = old_decl; |
| 8124 | } |
| 8125 | } |
| 8126 | else |
| 8127 | { |
| 8128 | tree context = DECL_CONTEXT (new_friend); |
| 8129 | bool dependent_p; |
| 8130 | |
| 8131 | /* In the code |
| 8132 | template <class T> class C { |
| 8133 | template <class U> friend void C1<U>::f (); // case 1 |
| 8134 | friend void C2<T>::f (); // case 2 |
| 8135 | }; |
| 8136 | we only need to make sure CONTEXT is a complete type for |
| 8137 | case 2. To distinguish between the two cases, we note that |
| 8138 | CONTEXT of case 1 remains dependent type after tsubst while |
| 8139 | this isn't true for case 2. */ |
| 8140 | ++processing_template_decl; |
| 8141 | dependent_p = dependent_type_p (context); |
| 8142 | --processing_template_decl; |
| 8143 | |
| 8144 | if (!dependent_p |
| 8145 | && !complete_type_or_else (context, NULL_TREE)) |
| 8146 | return error_mark_node; |
| 8147 | |
| 8148 | if (COMPLETE_TYPE_P (context)) |
| 8149 | { |
| 8150 | /* Check to see that the declaration is really present, and, |
| 8151 | possibly obtain an improved declaration. */ |
| 8152 | tree fn = check_classfn (context, |
| 8153 | new_friend, NULL_TREE); |
| 8154 | |
| 8155 | if (fn) |
| 8156 | new_friend = fn; |
| 8157 | } |
| 8158 | } |
| 8159 | |
| 8160 | return new_friend; |
| 8161 | } |
| 8162 | |
| 8163 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
| 8164 | template arguments, as for tsubst. |
| 8165 | |
| 8166 | Returns an appropriate tsubst'd friend type or error_mark_node on |
| 8167 | failure. */ |
| 8168 | |
| 8169 | static tree |
| 8170 | tsubst_friend_class (tree friend_tmpl, tree args) |
| 8171 | { |
| 8172 | tree friend_type; |
| 8173 | tree tmpl; |
| 8174 | tree context; |
| 8175 | |
| 8176 | context = CP_DECL_CONTEXT (friend_tmpl); |
| 8177 | |
| 8178 | if (context != global_namespace) |
| 8179 | { |
| 8180 | if (TREE_CODE (context) == NAMESPACE_DECL) |
| 8181 | push_nested_namespace (context); |
| 8182 | else |
| 8183 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
| 8184 | } |
| 8185 | |
| 8186 | /* Look for a class template declaration. We look for hidden names |
| 8187 | because two friend declarations of the same template are the |
| 8188 | same. For example, in: |
| 8189 | |
| 8190 | struct A { |
| 8191 | template <typename> friend class F; |
| 8192 | }; |
| 8193 | template <typename> struct B { |
| 8194 | template <typename> friend class F; |
| 8195 | }; |
| 8196 | |
| 8197 | both F templates are the same. */ |
| 8198 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, |
| 8199 | /*block_p=*/true, 0, |
| 8200 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); |
| 8201 | |
| 8202 | /* But, if we don't find one, it might be because we're in a |
| 8203 | situation like this: |
| 8204 | |
| 8205 | template <class T> |
| 8206 | struct S { |
| 8207 | template <class U> |
| 8208 | friend struct S; |
| 8209 | }; |
| 8210 | |
| 8211 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL |
| 8212 | for `S<int>', not the TEMPLATE_DECL. */ |
| 8213 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
| 8214 | { |
| 8215 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
| 8216 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
| 8217 | } |
| 8218 | |
| 8219 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
| 8220 | { |
| 8221 | /* The friend template has already been declared. Just |
| 8222 | check to see that the declarations match, and install any new |
| 8223 | default parameters. We must tsubst the default parameters, |
| 8224 | of course. We only need the innermost template parameters |
| 8225 | because that is all that redeclare_class_template will look |
| 8226 | at. */ |
| 8227 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
| 8228 | > TMPL_ARGS_DEPTH (args)) |
| 8229 | { |
| 8230 | tree parms; |
| 8231 | location_t saved_input_location; |
| 8232 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
| 8233 | args, tf_warning_or_error); |
| 8234 | |
| 8235 | saved_input_location = input_location; |
| 8236 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); |
| 8237 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
| 8238 | input_location = saved_input_location; |
| 8239 | |
| 8240 | } |
| 8241 | |
| 8242 | friend_type = TREE_TYPE (tmpl); |
| 8243 | } |
| 8244 | else |
| 8245 | { |
| 8246 | /* The friend template has not already been declared. In this |
| 8247 | case, the instantiation of the template class will cause the |
| 8248 | injection of this template into the global scope. */ |
| 8249 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
| 8250 | if (tmpl == error_mark_node) |
| 8251 | return error_mark_node; |
| 8252 | |
| 8253 | /* The new TMPL is not an instantiation of anything, so we |
| 8254 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
| 8255 | the new type because that is supposed to be the corresponding |
| 8256 | template decl, i.e., TMPL. */ |
| 8257 | DECL_USE_TEMPLATE (tmpl) = 0; |
| 8258 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; |
| 8259 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; |
| 8260 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
| 8261 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); |
| 8262 | |
| 8263 | /* Inject this template into the global scope. */ |
| 8264 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
| 8265 | } |
| 8266 | |
| 8267 | if (context != global_namespace) |
| 8268 | { |
| 8269 | if (TREE_CODE (context) == NAMESPACE_DECL) |
| 8270 | pop_nested_namespace (context); |
| 8271 | else |
| 8272 | pop_nested_class (); |
| 8273 | } |
| 8274 | |
| 8275 | return friend_type; |
| 8276 | } |
| 8277 | |
| 8278 | /* Returns zero if TYPE cannot be completed later due to circularity. |
| 8279 | Otherwise returns one. */ |
| 8280 | |
| 8281 | static int |
| 8282 | can_complete_type_without_circularity (tree type) |
| 8283 | { |
| 8284 | if (type == NULL_TREE || type == error_mark_node) |
| 8285 | return 0; |
| 8286 | else if (COMPLETE_TYPE_P (type)) |
| 8287 | return 1; |
| 8288 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) |
| 8289 | return can_complete_type_without_circularity (TREE_TYPE (type)); |
| 8290 | else if (CLASS_TYPE_P (type) |
| 8291 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) |
| 8292 | return 0; |
| 8293 | else |
| 8294 | return 1; |
| 8295 | } |
| 8296 | |
| 8297 | /* Apply any attributes which had to be deferred until instantiation |
| 8298 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; |
| 8299 | ARGS, COMPLAIN, IN_DECL are as tsubst. */ |
| 8300 | |
| 8301 | static void |
| 8302 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, |
| 8303 | tree args, tsubst_flags_t complain, tree in_decl) |
| 8304 | { |
| 8305 | tree last_dep = NULL_TREE; |
| 8306 | tree t; |
| 8307 | tree *p; |
| 8308 | |
| 8309 | for (t = attributes; t; t = TREE_CHAIN (t)) |
| 8310 | if (ATTR_IS_DEPENDENT (t)) |
| 8311 | { |
| 8312 | last_dep = t; |
| 8313 | attributes = copy_list (attributes); |
| 8314 | break; |
| 8315 | } |
| 8316 | |
| 8317 | if (DECL_P (*decl_p)) |
| 8318 | { |
| 8319 | if (TREE_TYPE (*decl_p) == error_mark_node) |
| 8320 | return; |
| 8321 | p = &DECL_ATTRIBUTES (*decl_p); |
| 8322 | } |
| 8323 | else |
| 8324 | p = &TYPE_ATTRIBUTES (*decl_p); |
| 8325 | |
| 8326 | if (last_dep) |
| 8327 | { |
| 8328 | tree late_attrs = NULL_TREE; |
| 8329 | tree *q = &late_attrs; |
| 8330 | |
| 8331 | for (*p = attributes; *p; ) |
| 8332 | { |
| 8333 | t = *p; |
| 8334 | if (ATTR_IS_DEPENDENT (t)) |
| 8335 | { |
| 8336 | *p = TREE_CHAIN (t); |
| 8337 | TREE_CHAIN (t) = NULL_TREE; |
| 8338 | /* If the first attribute argument is an identifier, don't |
| 8339 | pass it through tsubst. Attributes like mode, format, |
| 8340 | cleanup and several target specific attributes expect it |
| 8341 | unmodified. */ |
| 8342 | if (TREE_VALUE (t) |
| 8343 | && TREE_CODE (TREE_VALUE (t)) == TREE_LIST |
| 8344 | && TREE_VALUE (TREE_VALUE (t)) |
| 8345 | && (TREE_CODE (TREE_VALUE (TREE_VALUE (t))) |
| 8346 | == IDENTIFIER_NODE)) |
| 8347 | { |
| 8348 | tree chain |
| 8349 | = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain, |
| 8350 | in_decl, |
| 8351 | /*integral_constant_expression_p=*/false); |
| 8352 | if (chain != TREE_CHAIN (TREE_VALUE (t))) |
| 8353 | TREE_VALUE (t) |
| 8354 | = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)), |
| 8355 | chain); |
| 8356 | } |
| 8357 | else |
| 8358 | TREE_VALUE (t) |
| 8359 | = tsubst_expr (TREE_VALUE (t), args, complain, in_decl, |
| 8360 | /*integral_constant_expression_p=*/false); |
| 8361 | *q = t; |
| 8362 | q = &TREE_CHAIN (t); |
| 8363 | } |
| 8364 | else |
| 8365 | p = &TREE_CHAIN (t); |
| 8366 | } |
| 8367 | |
| 8368 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
| 8369 | } |
| 8370 | } |
| 8371 | |
| 8372 | /* Perform (or defer) access check for typedefs that were referenced |
| 8373 | from within the template TMPL code. |
| 8374 | This is a subroutine of instantiate_template and instantiate_class_template. |
| 8375 | TMPL is the template to consider and TARGS is the list of arguments of |
| 8376 | that template. */ |
| 8377 | |
| 8378 | static void |
| 8379 | perform_typedefs_access_check (tree tmpl, tree targs) |
| 8380 | { |
| 8381 | location_t saved_location; |
| 8382 | int i; |
| 8383 | qualified_typedef_usage_t *iter; |
| 8384 | |
| 8385 | if (!tmpl |
| 8386 | || (!CLASS_TYPE_P (tmpl) |
| 8387 | && TREE_CODE (tmpl) != FUNCTION_DECL)) |
| 8388 | return; |
| 8389 | |
| 8390 | saved_location = input_location; |
| 8391 | FOR_EACH_VEC_ELT (qualified_typedef_usage_t, |
| 8392 | get_types_needing_access_check (tmpl), |
| 8393 | i, iter) |
| 8394 | { |
| 8395 | tree type_decl = iter->typedef_decl; |
| 8396 | tree type_scope = iter->context; |
| 8397 | |
| 8398 | if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope)) |
| 8399 | continue; |
| 8400 | |
| 8401 | if (uses_template_parms (type_decl)) |
| 8402 | type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE); |
| 8403 | if (uses_template_parms (type_scope)) |
| 8404 | type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); |
| 8405 | |
| 8406 | /* Make access check error messages point to the location |
| 8407 | of the use of the typedef. */ |
| 8408 | input_location = iter->locus; |
| 8409 | perform_or_defer_access_check (TYPE_BINFO (type_scope), |
| 8410 | type_decl, type_decl); |
| 8411 | } |
| 8412 | input_location = saved_location; |
| 8413 | } |
| 8414 | |
| 8415 | static tree |
| 8416 | instantiate_class_template_1 (tree type) |
| 8417 | { |
| 8418 | tree templ, args, pattern, t, member; |
| 8419 | tree typedecl; |
| 8420 | tree pbinfo; |
| 8421 | tree base_list; |
| 8422 | unsigned int saved_maximum_field_alignment; |
| 8423 | tree fn_context; |
| 8424 | |
| 8425 | if (type == error_mark_node) |
| 8426 | return error_mark_node; |
| 8427 | |
| 8428 | if (COMPLETE_OR_OPEN_TYPE_P (type) |
| 8429 | || uses_template_parms (type)) |
| 8430 | return type; |
| 8431 | |
| 8432 | /* Figure out which template is being instantiated. */ |
| 8433 | templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
| 8434 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); |
| 8435 | |
| 8436 | /* Determine what specialization of the original template to |
| 8437 | instantiate. */ |
| 8438 | t = most_specialized_class (type, templ, tf_warning_or_error); |
| 8439 | if (t == error_mark_node) |
| 8440 | { |
| 8441 | TYPE_BEING_DEFINED (type) = 1; |
| 8442 | return error_mark_node; |
| 8443 | } |
| 8444 | else if (t) |
| 8445 | { |
| 8446 | /* This TYPE is actually an instantiation of a partial |
| 8447 | specialization. We replace the innermost set of ARGS with |
| 8448 | the arguments appropriate for substitution. For example, |
| 8449 | given: |
| 8450 | |
| 8451 | template <class T> struct S {}; |
| 8452 | template <class T> struct S<T*> {}; |
| 8453 | |
| 8454 | and supposing that we are instantiating S<int*>, ARGS will |
| 8455 | presently be {int*} -- but we need {int}. */ |
| 8456 | pattern = TREE_TYPE (t); |
| 8457 | args = TREE_PURPOSE (t); |
| 8458 | } |
| 8459 | else |
| 8460 | { |
| 8461 | pattern = TREE_TYPE (templ); |
| 8462 | args = CLASSTYPE_TI_ARGS (type); |
| 8463 | } |
| 8464 | |
| 8465 | /* If the template we're instantiating is incomplete, then clearly |
| 8466 | there's nothing we can do. */ |
| 8467 | if (!COMPLETE_TYPE_P (pattern)) |
| 8468 | return type; |
| 8469 | |
| 8470 | /* If we've recursively instantiated too many templates, stop. */ |
| 8471 | if (! push_tinst_level (type)) |
| 8472 | return type; |
| 8473 | |
| 8474 | /* Now we're really doing the instantiation. Mark the type as in |
| 8475 | the process of being defined. */ |
| 8476 | TYPE_BEING_DEFINED (type) = 1; |
| 8477 | |
| 8478 | /* We may be in the middle of deferred access check. Disable |
| 8479 | it now. */ |
| 8480 | push_deferring_access_checks (dk_no_deferred); |
| 8481 | |
| 8482 | fn_context = decl_function_context (TYPE_MAIN_DECL (type)); |
| 8483 | if (!fn_context) |
| 8484 | push_to_top_level (); |
| 8485 | /* Use #pragma pack from the template context. */ |
| 8486 | saved_maximum_field_alignment = maximum_field_alignment; |
| 8487 | maximum_field_alignment = TYPE_PRECISION (pattern); |
| 8488 | |
| 8489 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
| 8490 | |
| 8491 | /* Set the input location to the most specialized template definition. |
| 8492 | This is needed if tsubsting causes an error. */ |
| 8493 | typedecl = TYPE_MAIN_DECL (pattern); |
| 8494 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) = |
| 8495 | DECL_SOURCE_LOCATION (typedecl); |
| 8496 | |
| 8497 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
| 8498 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); |
| 8499 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
| 8500 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
| 8501 | if (ANON_AGGR_TYPE_P (pattern)) |
| 8502 | SET_ANON_AGGR_TYPE_P (type); |
| 8503 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
| 8504 | { |
| 8505 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; |
| 8506 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); |
| 8507 | /* Adjust visibility for template arguments. */ |
| 8508 | determine_visibility (TYPE_MAIN_DECL (type)); |
| 8509 | } |
| 8510 | CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern); |
| 8511 | |
| 8512 | pbinfo = TYPE_BINFO (pattern); |
| 8513 | |
| 8514 | /* We should never instantiate a nested class before its enclosing |
| 8515 | class; we need to look up the nested class by name before we can |
| 8516 | instantiate it, and that lookup should instantiate the enclosing |
| 8517 | class. */ |
| 8518 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) |
| 8519 | || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); |
| 8520 | |
| 8521 | base_list = NULL_TREE; |
| 8522 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
| 8523 | { |
| 8524 | tree pbase_binfo; |
| 8525 | tree pushed_scope; |
| 8526 | int i; |
| 8527 | |
| 8528 | /* We must enter the scope containing the type, as that is where |
| 8529 | the accessibility of types named in dependent bases are |
| 8530 | looked up from. */ |
| 8531 | pushed_scope = push_scope (CP_TYPE_CONTEXT (type)); |
| 8532 | |
| 8533 | /* Substitute into each of the bases to determine the actual |
| 8534 | basetypes. */ |
| 8535 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
| 8536 | { |
| 8537 | tree base; |
| 8538 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
| 8539 | tree expanded_bases = NULL_TREE; |
| 8540 | int idx, len = 1; |
| 8541 | |
| 8542 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) |
| 8543 | { |
| 8544 | expanded_bases = |
| 8545 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), |
| 8546 | args, tf_error, NULL_TREE); |
| 8547 | if (expanded_bases == error_mark_node) |
| 8548 | continue; |
| 8549 | |
| 8550 | len = TREE_VEC_LENGTH (expanded_bases); |
| 8551 | } |
| 8552 | |
| 8553 | for (idx = 0; idx < len; idx++) |
| 8554 | { |
| 8555 | if (expanded_bases) |
| 8556 | /* Extract the already-expanded base class. */ |
| 8557 | base = TREE_VEC_ELT (expanded_bases, idx); |
| 8558 | else |
| 8559 | /* Substitute to figure out the base class. */ |
| 8560 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, |
| 8561 | NULL_TREE); |
| 8562 | |
| 8563 | if (base == error_mark_node) |
| 8564 | continue; |
| 8565 | |
| 8566 | base_list = tree_cons (access, base, base_list); |
| 8567 | if (BINFO_VIRTUAL_P (pbase_binfo)) |
| 8568 | TREE_TYPE (base_list) = integer_type_node; |
| 8569 | } |
| 8570 | } |
| 8571 | |
| 8572 | /* The list is now in reverse order; correct that. */ |
| 8573 | base_list = nreverse (base_list); |
| 8574 | |
| 8575 | if (pushed_scope) |
| 8576 | pop_scope (pushed_scope); |
| 8577 | } |
| 8578 | /* Now call xref_basetypes to set up all the base-class |
| 8579 | information. */ |
| 8580 | xref_basetypes (type, base_list); |
| 8581 | |
| 8582 | apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), |
| 8583 | (int) ATTR_FLAG_TYPE_IN_PLACE, |
| 8584 | args, tf_error, NULL_TREE); |
| 8585 | fixup_attribute_variants (type); |
| 8586 | |
| 8587 | /* Now that our base classes are set up, enter the scope of the |
| 8588 | class, so that name lookups into base classes, etc. will work |
| 8589 | correctly. This is precisely analogous to what we do in |
| 8590 | begin_class_definition when defining an ordinary non-template |
| 8591 | class, except we also need to push the enclosing classes. */ |
| 8592 | push_nested_class (type); |
| 8593 | |
| 8594 | /* Now members are processed in the order of declaration. */ |
| 8595 | for (member = CLASSTYPE_DECL_LIST (pattern); |
| 8596 | member; member = TREE_CHAIN (member)) |
| 8597 | { |
| 8598 | tree t = TREE_VALUE (member); |
| 8599 | |
| 8600 | if (TREE_PURPOSE (member)) |
| 8601 | { |
| 8602 | if (TYPE_P (t)) |
| 8603 | { |
| 8604 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
| 8605 | |
| 8606 | tree newtag; |
| 8607 | bool class_template_p; |
| 8608 | |
| 8609 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
| 8610 | && TYPE_LANG_SPECIFIC (t) |
| 8611 | && CLASSTYPE_IS_TEMPLATE (t)); |
| 8612 | /* If the member is a class template, then -- even after |
| 8613 | substitution -- there may be dependent types in the |
| 8614 | template argument list for the class. We increment |
| 8615 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as |
| 8616 | that function will assume that no types are dependent |
| 8617 | when outside of a template. */ |
| 8618 | if (class_template_p) |
| 8619 | ++processing_template_decl; |
| 8620 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
| 8621 | if (class_template_p) |
| 8622 | --processing_template_decl; |
| 8623 | if (newtag == error_mark_node) |
| 8624 | continue; |
| 8625 | |
| 8626 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
| 8627 | { |
| 8628 | tree name = TYPE_IDENTIFIER (t); |
| 8629 | |
| 8630 | if (class_template_p) |
| 8631 | /* Unfortunately, lookup_template_class sets |
| 8632 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial |
| 8633 | instantiation (i.e., for the type of a member |
| 8634 | template class nested within a template class.) |
| 8635 | This behavior is required for |
| 8636 | maybe_process_partial_specialization to work |
| 8637 | correctly, but is not accurate in this case; |
| 8638 | the TAG is not an instantiation of anything. |
| 8639 | (The corresponding TEMPLATE_DECL is an |
| 8640 | instantiation, but the TYPE is not.) */ |
| 8641 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
| 8642 | |
| 8643 | /* Now, we call pushtag to put this NEWTAG into the scope of |
| 8644 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid |
| 8645 | pushtag calling push_template_decl. We don't have to do |
| 8646 | this for enums because it will already have been done in |
| 8647 | tsubst_enum. */ |
| 8648 | if (name) |
| 8649 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); |
| 8650 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
| 8651 | } |
| 8652 | } |
| 8653 | else if (TREE_CODE (t) == FUNCTION_DECL |
| 8654 | || DECL_FUNCTION_TEMPLATE_P (t)) |
| 8655 | { |
| 8656 | /* Build new TYPE_METHODS. */ |
| 8657 | tree r; |
| 8658 | |
| 8659 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8660 | ++processing_template_decl; |
| 8661 | r = tsubst (t, args, tf_error, NULL_TREE); |
| 8662 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8663 | --processing_template_decl; |
| 8664 | set_current_access_from_decl (r); |
| 8665 | finish_member_declaration (r); |
| 8666 | /* Instantiate members marked with attribute used. */ |
| 8667 | if (r != error_mark_node && DECL_PRESERVE_P (r)) |
| 8668 | mark_used (r); |
| 8669 | } |
| 8670 | else |
| 8671 | { |
| 8672 | /* Build new TYPE_FIELDS. */ |
| 8673 | if (TREE_CODE (t) == STATIC_ASSERT) |
| 8674 | { |
| 8675 | tree condition = |
| 8676 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, |
| 8677 | tf_warning_or_error, NULL_TREE, |
| 8678 | /*integral_constant_expression_p=*/true); |
| 8679 | finish_static_assert (condition, |
| 8680 | STATIC_ASSERT_MESSAGE (t), |
| 8681 | STATIC_ASSERT_SOURCE_LOCATION (t), |
| 8682 | /*member_p=*/true); |
| 8683 | } |
| 8684 | else if (TREE_CODE (t) != CONST_DECL) |
| 8685 | { |
| 8686 | tree r; |
| 8687 | |
| 8688 | /* The file and line for this declaration, to |
| 8689 | assist in error message reporting. Since we |
| 8690 | called push_tinst_level above, we don't need to |
| 8691 | restore these. */ |
| 8692 | input_location = DECL_SOURCE_LOCATION (t); |
| 8693 | |
| 8694 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8695 | ++processing_template_decl; |
| 8696 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
| 8697 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8698 | --processing_template_decl; |
| 8699 | if (TREE_CODE (r) == VAR_DECL) |
| 8700 | { |
| 8701 | /* In [temp.inst]: |
| 8702 | |
| 8703 | [t]he initialization (and any associated |
| 8704 | side-effects) of a static data member does |
| 8705 | not occur unless the static data member is |
| 8706 | itself used in a way that requires the |
| 8707 | definition of the static data member to |
| 8708 | exist. |
| 8709 | |
| 8710 | Therefore, we do not substitute into the |
| 8711 | initialized for the static data member here. */ |
| 8712 | finish_static_data_member_decl |
| 8713 | (r, |
| 8714 | /*init=*/NULL_TREE, |
| 8715 | /*init_const_expr_p=*/false, |
| 8716 | /*asmspec_tree=*/NULL_TREE, |
| 8717 | /*flags=*/0); |
| 8718 | /* Instantiate members marked with attribute used. */ |
| 8719 | if (r != error_mark_node && DECL_PRESERVE_P (r)) |
| 8720 | mark_used (r); |
| 8721 | } |
| 8722 | else if (TREE_CODE (r) == FIELD_DECL) |
| 8723 | { |
| 8724 | /* Determine whether R has a valid type and can be |
| 8725 | completed later. If R is invalid, then it is |
| 8726 | replaced by error_mark_node so that it will not be |
| 8727 | added to TYPE_FIELDS. */ |
| 8728 | tree rtype = TREE_TYPE (r); |
| 8729 | if (can_complete_type_without_circularity (rtype)) |
| 8730 | complete_type (rtype); |
| 8731 | |
| 8732 | if (!COMPLETE_TYPE_P (rtype)) |
| 8733 | { |
| 8734 | cxx_incomplete_type_error (r, rtype); |
| 8735 | r = error_mark_node; |
| 8736 | } |
| 8737 | } |
| 8738 | |
| 8739 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
| 8740 | such a thing will already have been added to the field |
| 8741 | list by tsubst_enum in finish_member_declaration in the |
| 8742 | CLASSTYPE_NESTED_UTDS case above. */ |
| 8743 | if (!(TREE_CODE (r) == TYPE_DECL |
| 8744 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE |
| 8745 | && DECL_ARTIFICIAL (r))) |
| 8746 | { |
| 8747 | set_current_access_from_decl (r); |
| 8748 | finish_member_declaration (r); |
| 8749 | } |
| 8750 | } |
| 8751 | } |
| 8752 | } |
| 8753 | else |
| 8754 | { |
| 8755 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) |
| 8756 | { |
| 8757 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ |
| 8758 | |
| 8759 | tree friend_type = t; |
| 8760 | bool adjust_processing_template_decl = false; |
| 8761 | |
| 8762 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
| 8763 | { |
| 8764 | /* template <class T> friend class C; */ |
| 8765 | friend_type = tsubst_friend_class (friend_type, args); |
| 8766 | adjust_processing_template_decl = true; |
| 8767 | } |
| 8768 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) |
| 8769 | { |
| 8770 | /* template <class T> friend class C::D; */ |
| 8771 | friend_type = tsubst (friend_type, args, |
| 8772 | tf_warning_or_error, NULL_TREE); |
| 8773 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
| 8774 | friend_type = TREE_TYPE (friend_type); |
| 8775 | adjust_processing_template_decl = true; |
| 8776 | } |
| 8777 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE |
| 8778 | || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) |
| 8779 | { |
| 8780 | /* This could be either |
| 8781 | |
| 8782 | friend class T::C; |
| 8783 | |
| 8784 | when dependent_type_p is false or |
| 8785 | |
| 8786 | template <class U> friend class T::C; |
| 8787 | |
| 8788 | otherwise. */ |
| 8789 | friend_type = tsubst (friend_type, args, |
| 8790 | tf_warning_or_error, NULL_TREE); |
| 8791 | /* Bump processing_template_decl for correct |
| 8792 | dependent_type_p calculation. */ |
| 8793 | ++processing_template_decl; |
| 8794 | if (dependent_type_p (friend_type)) |
| 8795 | adjust_processing_template_decl = true; |
| 8796 | --processing_template_decl; |
| 8797 | } |
| 8798 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
| 8799 | && hidden_name_p (TYPE_NAME (friend_type))) |
| 8800 | { |
| 8801 | /* friend class C; |
| 8802 | |
| 8803 | where C hasn't been declared yet. Let's lookup name |
| 8804 | from namespace scope directly, bypassing any name that |
| 8805 | come from dependent base class. */ |
| 8806 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
| 8807 | |
| 8808 | /* The call to xref_tag_from_type does injection for friend |
| 8809 | classes. */ |
| 8810 | push_nested_namespace (ns); |
| 8811 | friend_type = |
| 8812 | xref_tag_from_type (friend_type, NULL_TREE, |
| 8813 | /*tag_scope=*/ts_current); |
| 8814 | pop_nested_namespace (ns); |
| 8815 | } |
| 8816 | else if (uses_template_parms (friend_type)) |
| 8817 | /* friend class C<T>; */ |
| 8818 | friend_type = tsubst (friend_type, args, |
| 8819 | tf_warning_or_error, NULL_TREE); |
| 8820 | /* Otherwise it's |
| 8821 | |
| 8822 | friend class C; |
| 8823 | |
| 8824 | where C is already declared or |
| 8825 | |
| 8826 | friend class C<int>; |
| 8827 | |
| 8828 | We don't have to do anything in these cases. */ |
| 8829 | |
| 8830 | if (adjust_processing_template_decl) |
| 8831 | /* Trick make_friend_class into realizing that the friend |
| 8832 | we're adding is a template, not an ordinary class. It's |
| 8833 | important that we use make_friend_class since it will |
| 8834 | perform some error-checking and output cross-reference |
| 8835 | information. */ |
| 8836 | ++processing_template_decl; |
| 8837 | |
| 8838 | if (friend_type != error_mark_node) |
| 8839 | make_friend_class (type, friend_type, /*complain=*/false); |
| 8840 | |
| 8841 | if (adjust_processing_template_decl) |
| 8842 | --processing_template_decl; |
| 8843 | } |
| 8844 | else |
| 8845 | { |
| 8846 | /* Build new DECL_FRIENDLIST. */ |
| 8847 | tree r; |
| 8848 | |
| 8849 | /* The file and line for this declaration, to |
| 8850 | assist in error message reporting. Since we |
| 8851 | called push_tinst_level above, we don't need to |
| 8852 | restore these. */ |
| 8853 | input_location = DECL_SOURCE_LOCATION (t); |
| 8854 | |
| 8855 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8856 | { |
| 8857 | ++processing_template_decl; |
| 8858 | push_deferring_access_checks (dk_no_check); |
| 8859 | } |
| 8860 | |
| 8861 | r = tsubst_friend_function (t, args); |
| 8862 | add_friend (type, r, /*complain=*/false); |
| 8863 | if (TREE_CODE (t) == TEMPLATE_DECL) |
| 8864 | { |
| 8865 | pop_deferring_access_checks (); |
| 8866 | --processing_template_decl; |
| 8867 | } |
| 8868 | } |
| 8869 | } |
| 8870 | } |
| 8871 | |
| 8872 | if (CLASSTYPE_LAMBDA_EXPR (type)) |
| 8873 | { |
| 8874 | tree decl = lambda_function (type); |
| 8875 | if (decl) |
| 8876 | { |
| 8877 | tree lambda = CLASSTYPE_LAMBDA_EXPR (type); |
| 8878 | if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda)) |
| 8879 | { |
| 8880 | apply_lambda_return_type (lambda, void_type_node); |
| 8881 | LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE; |
| 8882 | } |
| 8883 | |
| 8884 | instantiate_decl (decl, false, false); |
| 8885 | maybe_add_lambda_conv_op (type); |
| 8886 | } |
| 8887 | else |
| 8888 | gcc_assert (errorcount); |
| 8889 | } |
| 8890 | |
| 8891 | /* Set the file and line number information to whatever is given for |
| 8892 | the class itself. This puts error messages involving generated |
| 8893 | implicit functions at a predictable point, and the same point |
| 8894 | that would be used for non-template classes. */ |
| 8895 | input_location = DECL_SOURCE_LOCATION (typedecl); |
| 8896 | |
| 8897 | unreverse_member_declarations (type); |
| 8898 | finish_struct_1 (type); |
| 8899 | TYPE_BEING_DEFINED (type) = 0; |
| 8900 | |
| 8901 | /* We don't instantiate default arguments for member functions. 14.7.1: |
| 8902 | |
| 8903 | The implicit instantiation of a class template specialization causes |
| 8904 | the implicit instantiation of the declarations, but not of the |
| 8905 | definitions or default arguments, of the class member functions, |
| 8906 | member classes, static data members and member templates.... */ |
| 8907 | |
| 8908 | /* Some typedefs referenced from within the template code need to be access |
| 8909 | checked at template instantiation time, i.e now. These types were |
| 8910 | added to the template at parsing time. Let's get those and perform |
| 8911 | the access checks then. */ |
| 8912 | perform_typedefs_access_check (pattern, args); |
| 8913 | perform_deferred_access_checks (); |
| 8914 | pop_nested_class (); |
| 8915 | maximum_field_alignment = saved_maximum_field_alignment; |
| 8916 | if (!fn_context) |
| 8917 | pop_from_top_level (); |
| 8918 | pop_deferring_access_checks (); |
| 8919 | pop_tinst_level (); |
| 8920 | |
| 8921 | /* The vtable for a template class can be emitted in any translation |
| 8922 | unit in which the class is instantiated. When there is no key |
| 8923 | method, however, finish_struct_1 will already have added TYPE to |
| 8924 | the keyed_classes list. */ |
| 8925 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) |
| 8926 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
| 8927 | |
| 8928 | return type; |
| 8929 | } |
| 8930 | |
| 8931 | /* Wrapper for instantiate_class_template_1. */ |
| 8932 | |
| 8933 | tree |
| 8934 | instantiate_class_template (tree type) |
| 8935 | { |
| 8936 | tree ret; |
| 8937 | timevar_push (TV_TEMPLATE_INST); |
| 8938 | ret = instantiate_class_template_1 (type); |
| 8939 | timevar_pop (TV_TEMPLATE_INST); |
| 8940 | return ret; |
| 8941 | } |
| 8942 | |
| 8943 | static tree |
| 8944 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
| 8945 | { |
| 8946 | tree r; |
| 8947 | |
| 8948 | if (!t) |
| 8949 | r = t; |
| 8950 | else if (TYPE_P (t)) |
| 8951 | r = tsubst (t, args, complain, in_decl); |
| 8952 | else |
| 8953 | { |
| 8954 | if (!(complain & tf_warning)) |
| 8955 | ++c_inhibit_evaluation_warnings; |
| 8956 | r = tsubst_expr (t, args, complain, in_decl, |
| 8957 | /*integral_constant_expression_p=*/true); |
| 8958 | if (!(complain & tf_warning)) |
| 8959 | --c_inhibit_evaluation_warnings; |
| 8960 | /* Preserve the raw-reference nature of T. */ |
| 8961 | if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE |
| 8962 | && REFERENCE_REF_P (r)) |
| 8963 | r = TREE_OPERAND (r, 0); |
| 8964 | } |
| 8965 | return r; |
| 8966 | } |
| 8967 | |
| 8968 | /* Given a function parameter pack TMPL_PARM and some function parameters |
| 8969 | instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them |
| 8970 | and set *SPEC_P to point at the next point in the list. */ |
| 8971 | |
| 8972 | static tree |
| 8973 | extract_fnparm_pack (tree tmpl_parm, tree *spec_p) |
| 8974 | { |
| 8975 | /* Collect all of the extra "packed" parameters into an |
| 8976 | argument pack. */ |
| 8977 | tree parmvec; |
| 8978 | tree parmtypevec; |
| 8979 | tree argpack = make_node (NONTYPE_ARGUMENT_PACK); |
| 8980 | tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 8981 | tree spec_parm = *spec_p; |
| 8982 | int i, len; |
| 8983 | |
| 8984 | for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm)) |
| 8985 | if (tmpl_parm |
| 8986 | && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm)) |
| 8987 | break; |
| 8988 | |
| 8989 | /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */ |
| 8990 | parmvec = make_tree_vec (len); |
| 8991 | parmtypevec = make_tree_vec (len); |
| 8992 | spec_parm = *spec_p; |
| 8993 | for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm)) |
| 8994 | { |
| 8995 | TREE_VEC_ELT (parmvec, i) = spec_parm; |
| 8996 | TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm); |
| 8997 | } |
| 8998 | |
| 8999 | /* Build the argument packs. */ |
| 9000 | SET_ARGUMENT_PACK_ARGS (argpack, parmvec); |
| 9001 | SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec); |
| 9002 | TREE_TYPE (argpack) = argtypepack; |
| 9003 | *spec_p = spec_parm; |
| 9004 | |
| 9005 | return argpack; |
| 9006 | } |
| 9007 | |
| 9008 | /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a |
| 9009 | NONTYPE_ARGUMENT_PACK. */ |
| 9010 | |
| 9011 | static tree |
| 9012 | make_fnparm_pack (tree spec_parm) |
| 9013 | { |
| 9014 | return extract_fnparm_pack (NULL_TREE, &spec_parm); |
| 9015 | } |
| 9016 | |
| 9017 | /* Substitute ARGS into T, which is an pack expansion |
| 9018 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a |
| 9019 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node |
| 9020 | (if only a partial substitution could be performed) or |
| 9021 | ERROR_MARK_NODE if there was an error. */ |
| 9022 | tree |
| 9023 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, |
| 9024 | tree in_decl) |
| 9025 | { |
| 9026 | tree pattern; |
| 9027 | tree pack, packs = NULL_TREE; |
| 9028 | bool unsubstituted_packs = false; |
| 9029 | bool real_packs = false; |
| 9030 | int missing_level = 0; |
| 9031 | int i, len = -1; |
| 9032 | tree result; |
| 9033 | htab_t saved_local_specializations = NULL; |
| 9034 | bool need_local_specializations = false; |
| 9035 | int levels; |
| 9036 | |
| 9037 | gcc_assert (PACK_EXPANSION_P (t)); |
| 9038 | pattern = PACK_EXPANSION_PATTERN (t); |
| 9039 | |
| 9040 | /* Add in any args remembered from an earlier partial instantiation. */ |
| 9041 | args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args); |
| 9042 | |
| 9043 | levels = TMPL_ARGS_DEPTH (args); |
| 9044 | |
| 9045 | /* Determine the argument packs that will instantiate the parameter |
| 9046 | packs used in the expansion expression. While we're at it, |
| 9047 | compute the number of arguments to be expanded and make sure it |
| 9048 | is consistent. */ |
| 9049 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; |
| 9050 | pack = TREE_CHAIN (pack)) |
| 9051 | { |
| 9052 | tree parm_pack = TREE_VALUE (pack); |
| 9053 | tree arg_pack = NULL_TREE; |
| 9054 | tree orig_arg = NULL_TREE; |
| 9055 | int level = 0; |
| 9056 | |
| 9057 | if (TREE_CODE (parm_pack) == BASES) |
| 9058 | { |
| 9059 | if (BASES_DIRECT (parm_pack)) |
| 9060 | return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack), |
| 9061 | args, complain, in_decl, false)); |
| 9062 | else |
| 9063 | return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack), |
| 9064 | args, complain, in_decl, false)); |
| 9065 | } |
| 9066 | if (TREE_CODE (parm_pack) == PARM_DECL) |
| 9067 | { |
| 9068 | if (PACK_EXPANSION_LOCAL_P (t)) |
| 9069 | arg_pack = retrieve_local_specialization (parm_pack); |
| 9070 | else |
| 9071 | { |
| 9072 | /* We can't rely on local_specializations for a parameter |
| 9073 | name used later in a function declaration (such as in a |
| 9074 | late-specified return type). Even if it exists, it might |
| 9075 | have the wrong value for a recursive call. Just make a |
| 9076 | dummy decl, since it's only used for its type. */ |
| 9077 | /* Copy before tsubsting so that we don't recurse into any |
| 9078 | later PARM_DECLs. */ |
| 9079 | arg_pack = tsubst_decl (copy_node (parm_pack), args, complain); |
| 9080 | if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack)) |
| 9081 | /* Partial instantiation of the parm_pack, we can't build |
| 9082 | up an argument pack yet. */ |
| 9083 | arg_pack = NULL_TREE; |
| 9084 | else |
| 9085 | arg_pack = make_fnparm_pack (arg_pack); |
| 9086 | need_local_specializations = true; |
| 9087 | } |
| 9088 | } |
| 9089 | else |
| 9090 | { |
| 9091 | int idx; |
| 9092 | template_parm_level_and_index (parm_pack, &level, &idx); |
| 9093 | |
| 9094 | if (level <= levels) |
| 9095 | arg_pack = TMPL_ARG (args, level, idx); |
| 9096 | } |
| 9097 | |
| 9098 | orig_arg = arg_pack; |
| 9099 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) |
| 9100 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); |
| 9101 | |
| 9102 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
| 9103 | /* This can only happen if we forget to expand an argument |
| 9104 | pack somewhere else. Just return an error, silently. */ |
| 9105 | { |
| 9106 | result = make_tree_vec (1); |
| 9107 | TREE_VEC_ELT (result, 0) = error_mark_node; |
| 9108 | return result; |
| 9109 | } |
| 9110 | |
| 9111 | if (arg_from_parm_pack_p (arg_pack, parm_pack)) |
| 9112 | /* The argument pack that the parameter maps to is just an |
| 9113 | expansion of the parameter itself, such as one would find |
| 9114 | in the implicit typedef of a class inside the class itself. |
| 9115 | Consider this parameter "unsubstituted", so that we will |
| 9116 | maintain the outer pack expansion. */ |
| 9117 | arg_pack = NULL_TREE; |
| 9118 | |
| 9119 | if (arg_pack) |
| 9120 | { |
| 9121 | int my_len = |
| 9122 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); |
| 9123 | |
| 9124 | /* Don't bother trying to do a partial substitution with |
| 9125 | incomplete packs; we'll try again after deduction. */ |
| 9126 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) |
| 9127 | return t; |
| 9128 | |
| 9129 | if (len < 0) |
| 9130 | len = my_len; |
| 9131 | else if (len != my_len) |
| 9132 | { |
| 9133 | if (!(complain & tf_error)) |
| 9134 | /* Fail quietly. */; |
| 9135 | else if (TREE_CODE (t) == TYPE_PACK_EXPANSION) |
| 9136 | error ("mismatched argument pack lengths while expanding " |
| 9137 | "%<%T%>", |
| 9138 | pattern); |
| 9139 | else |
| 9140 | error ("mismatched argument pack lengths while expanding " |
| 9141 | "%<%E%>", |
| 9142 | pattern); |
| 9143 | return error_mark_node; |
| 9144 | } |
| 9145 | |
| 9146 | if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1 |
| 9147 | && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), |
| 9148 | 0))) |
| 9149 | /* This isn't a real argument pack yet. */; |
| 9150 | else |
| 9151 | real_packs = true; |
| 9152 | |
| 9153 | /* Keep track of the parameter packs and their corresponding |
| 9154 | argument packs. */ |
| 9155 | packs = tree_cons (parm_pack, arg_pack, packs); |
| 9156 | TREE_TYPE (packs) = orig_arg; |
| 9157 | } |
| 9158 | else |
| 9159 | { |
| 9160 | /* We can't substitute for this parameter pack. We use a flag as |
| 9161 | well as the missing_level counter because function parameter |
| 9162 | packs don't have a level. */ |
| 9163 | unsubstituted_packs = true; |
| 9164 | if (!missing_level || missing_level > level) |
| 9165 | missing_level = level; |
| 9166 | } |
| 9167 | } |
| 9168 | |
| 9169 | /* We cannot expand this expansion expression, because we don't have |
| 9170 | all of the argument packs we need. */ |
| 9171 | if (unsubstituted_packs) |
| 9172 | { |
| 9173 | if (real_packs) |
| 9174 | { |
| 9175 | /* We got some full packs, but we can't substitute them in until we |
| 9176 | have values for all the packs. So remember these until then. */ |
| 9177 | tree save_args; |
| 9178 | |
| 9179 | t = make_pack_expansion (pattern); |
| 9180 | |
| 9181 | /* The call to add_to_template_args above assumes no overlap |
| 9182 | between saved args and new args, so prune away any fake |
| 9183 | args, i.e. those that satisfied arg_from_parm_pack_p above. */ |
| 9184 | if (missing_level && levels >= missing_level) |
| 9185 | { |
| 9186 | gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args) |
| 9187 | && missing_level > 1); |
| 9188 | TREE_VEC_LENGTH (args) = missing_level - 1; |
| 9189 | save_args = copy_node (args); |
| 9190 | TREE_VEC_LENGTH (args) = levels; |
| 9191 | } |
| 9192 | else |
| 9193 | save_args = args; |
| 9194 | |
| 9195 | PACK_EXPANSION_EXTRA_ARGS (t) = save_args; |
| 9196 | } |
| 9197 | else |
| 9198 | { |
| 9199 | /* There were no real arguments, we're just replacing a parameter |
| 9200 | pack with another version of itself. Substitute into the |
| 9201 | pattern and return a PACK_EXPANSION_*. The caller will need to |
| 9202 | deal with that. */ |
| 9203 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) |
| 9204 | t = tsubst_expr (pattern, args, complain, in_decl, |
| 9205 | /*integral_constant_expression_p=*/false); |
| 9206 | else |
| 9207 | t = tsubst (pattern, args, complain, in_decl); |
| 9208 | t = make_pack_expansion (t); |
| 9209 | } |
| 9210 | return t; |
| 9211 | } |
| 9212 | |
| 9213 | /* We could not find any argument packs that work. */ |
| 9214 | if (len < 0) |
| 9215 | return error_mark_node; |
| 9216 | |
| 9217 | if (need_local_specializations) |
| 9218 | { |
| 9219 | /* We're in a late-specified return type, so create our own local |
| 9220 | specializations table; the current table is either NULL or (in the |
| 9221 | case of recursive unification) might have bindings that we don't |
| 9222 | want to use or alter. */ |
| 9223 | saved_local_specializations = local_specializations; |
| 9224 | local_specializations = htab_create (37, |
| 9225 | hash_local_specialization, |
| 9226 | eq_local_specializations, |
| 9227 | NULL); |
| 9228 | } |
| 9229 | |
| 9230 | /* For each argument in each argument pack, substitute into the |
| 9231 | pattern. */ |
| 9232 | result = make_tree_vec (len); |
| 9233 | for (i = 0; i < len; ++i) |
| 9234 | { |
| 9235 | /* For parameter pack, change the substitution of the parameter |
| 9236 | pack to the ith argument in its argument pack, then expand |
| 9237 | the pattern. */ |
| 9238 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
| 9239 | { |
| 9240 | tree parm = TREE_PURPOSE (pack); |
| 9241 | tree arg; |
| 9242 | |
| 9243 | /* Select the Ith argument from the pack. */ |
| 9244 | if (TREE_CODE (parm) == PARM_DECL) |
| 9245 | { |
| 9246 | if (i == 0) |
| 9247 | { |
| 9248 | arg = make_node (ARGUMENT_PACK_SELECT); |
| 9249 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); |
| 9250 | mark_used (parm); |
| 9251 | register_local_specialization (arg, parm); |
| 9252 | } |
| 9253 | else |
| 9254 | arg = retrieve_local_specialization (parm); |
| 9255 | } |
| 9256 | else |
| 9257 | { |
| 9258 | int idx, level; |
| 9259 | template_parm_level_and_index (parm, &level, &idx); |
| 9260 | |
| 9261 | if (i == 0) |
| 9262 | { |
| 9263 | arg = make_node (ARGUMENT_PACK_SELECT); |
| 9264 | ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack); |
| 9265 | /* Update the corresponding argument. */ |
| 9266 | TMPL_ARG (args, level, idx) = arg; |
| 9267 | } |
| 9268 | else |
| 9269 | /* Re-use the ARGUMENT_PACK_SELECT. */ |
| 9270 | arg = TMPL_ARG (args, level, idx); |
| 9271 | } |
| 9272 | ARGUMENT_PACK_SELECT_INDEX (arg) = i; |
| 9273 | } |
| 9274 | |
| 9275 | /* Substitute into the PATTERN with the altered arguments. */ |
| 9276 | if (!TYPE_P (pattern)) |
| 9277 | TREE_VEC_ELT (result, i) = |
| 9278 | tsubst_expr (pattern, args, complain, in_decl, |
| 9279 | /*integral_constant_expression_p=*/false); |
| 9280 | else |
| 9281 | TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl); |
| 9282 | |
| 9283 | if (TREE_VEC_ELT (result, i) == error_mark_node) |
| 9284 | { |
| 9285 | result = error_mark_node; |
| 9286 | break; |
| 9287 | } |
| 9288 | } |
| 9289 | |
| 9290 | /* Update ARGS to restore the substitution from parameter packs to |
| 9291 | their argument packs. */ |
| 9292 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
| 9293 | { |
| 9294 | tree parm = TREE_PURPOSE (pack); |
| 9295 | |
| 9296 | if (TREE_CODE (parm) == PARM_DECL) |
| 9297 | register_local_specialization (TREE_TYPE (pack), parm); |
| 9298 | else |
| 9299 | { |
| 9300 | int idx, level; |
| 9301 | template_parm_level_and_index (parm, &level, &idx); |
| 9302 | |
| 9303 | /* Update the corresponding argument. */ |
| 9304 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
| 9305 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = |
| 9306 | TREE_TYPE (pack); |
| 9307 | else |
| 9308 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); |
| 9309 | } |
| 9310 | } |
| 9311 | |
| 9312 | if (need_local_specializations) |
| 9313 | { |
| 9314 | htab_delete (local_specializations); |
| 9315 | local_specializations = saved_local_specializations; |
| 9316 | } |
| 9317 | |
| 9318 | return result; |
| 9319 | } |
| 9320 | |
| 9321 | /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template |
| 9322 | TMPL. We do this using DECL_PARM_INDEX, which should work even with |
| 9323 | parameter packs; all parms generated from a function parameter pack will |
| 9324 | have the same DECL_PARM_INDEX. */ |
| 9325 | |
| 9326 | tree |
| 9327 | get_pattern_parm (tree parm, tree tmpl) |
| 9328 | { |
| 9329 | tree pattern = DECL_TEMPLATE_RESULT (tmpl); |
| 9330 | tree patparm; |
| 9331 | |
| 9332 | if (DECL_ARTIFICIAL (parm)) |
| 9333 | { |
| 9334 | for (patparm = DECL_ARGUMENTS (pattern); |
| 9335 | patparm; patparm = DECL_CHAIN (patparm)) |
| 9336 | if (DECL_ARTIFICIAL (patparm) |
| 9337 | && DECL_NAME (parm) == DECL_NAME (patparm)) |
| 9338 | break; |
| 9339 | } |
| 9340 | else |
| 9341 | { |
| 9342 | patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl)); |
| 9343 | patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm); |
| 9344 | gcc_assert (DECL_PARM_INDEX (patparm) |
| 9345 | == DECL_PARM_INDEX (parm)); |
| 9346 | } |
| 9347 | |
| 9348 | return patparm; |
| 9349 | } |
| 9350 | |
| 9351 | /* Substitute ARGS into the vector or list of template arguments T. */ |
| 9352 | |
| 9353 | static tree |
| 9354 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
| 9355 | { |
| 9356 | tree orig_t = t; |
| 9357 | int len, need_new = 0, i, expanded_len_adjust = 0, out; |
| 9358 | tree *elts; |
| 9359 | |
| 9360 | if (t == error_mark_node) |
| 9361 | return error_mark_node; |
| 9362 | |
| 9363 | len = TREE_VEC_LENGTH (t); |
| 9364 | elts = XALLOCAVEC (tree, len); |
| 9365 | |
| 9366 | for (i = 0; i < len; i++) |
| 9367 | { |
| 9368 | tree orig_arg = TREE_VEC_ELT (t, i); |
| 9369 | tree new_arg; |
| 9370 | |
| 9371 | if (TREE_CODE (orig_arg) == TREE_VEC) |
| 9372 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); |
| 9373 | else if (PACK_EXPANSION_P (orig_arg)) |
| 9374 | { |
| 9375 | /* Substitute into an expansion expression. */ |
| 9376 | new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); |
| 9377 | |
| 9378 | if (TREE_CODE (new_arg) == TREE_VEC) |
| 9379 | /* Add to the expanded length adjustment the number of |
| 9380 | expanded arguments. We subtract one from this |
| 9381 | measurement, because the argument pack expression |
| 9382 | itself is already counted as 1 in |
| 9383 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if |
| 9384 | the argument pack is empty. */ |
| 9385 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; |
| 9386 | } |
| 9387 | else if (ARGUMENT_PACK_P (orig_arg)) |
| 9388 | { |
| 9389 | /* Substitute into each of the arguments. */ |
| 9390 | new_arg = TYPE_P (orig_arg) |
| 9391 | ? cxx_make_type (TREE_CODE (orig_arg)) |
| 9392 | : make_node (TREE_CODE (orig_arg)); |
| 9393 | |
| 9394 | SET_ARGUMENT_PACK_ARGS ( |
| 9395 | new_arg, |
| 9396 | tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), |
| 9397 | args, complain, in_decl)); |
| 9398 | |
| 9399 | if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node) |
| 9400 | new_arg = error_mark_node; |
| 9401 | |
| 9402 | if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) { |
| 9403 | TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args, |
| 9404 | complain, in_decl); |
| 9405 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); |
| 9406 | |
| 9407 | if (TREE_TYPE (new_arg) == error_mark_node) |
| 9408 | new_arg = error_mark_node; |
| 9409 | } |
| 9410 | } |
| 9411 | else |
| 9412 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
| 9413 | |
| 9414 | if (new_arg == error_mark_node) |
| 9415 | return error_mark_node; |
| 9416 | |
| 9417 | elts[i] = new_arg; |
| 9418 | if (new_arg != orig_arg) |
| 9419 | need_new = 1; |
| 9420 | } |
| 9421 | |
| 9422 | if (!need_new) |
| 9423 | return t; |
| 9424 | |
| 9425 | /* Make space for the expanded arguments coming from template |
| 9426 | argument packs. */ |
| 9427 | t = make_tree_vec (len + expanded_len_adjust); |
| 9428 | /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the |
| 9429 | arguments for a member template. |
| 9430 | In that case each TREE_VEC in ORIG_T represents a level of template |
| 9431 | arguments, and ORIG_T won't carry any non defaulted argument count. |
| 9432 | It will rather be the nested TREE_VECs that will carry one. |
| 9433 | In other words, ORIG_T carries a non defaulted argument count only |
| 9434 | if it doesn't contain any nested TREE_VEC. */ |
| 9435 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t)) |
| 9436 | { |
| 9437 | int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t); |
| 9438 | count += expanded_len_adjust; |
| 9439 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count); |
| 9440 | } |
| 9441 | for (i = 0, out = 0; i < len; i++) |
| 9442 | { |
| 9443 | if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i)) |
| 9444 | || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i))) |
| 9445 | && TREE_CODE (elts[i]) == TREE_VEC) |
| 9446 | { |
| 9447 | int idx; |
| 9448 | |
| 9449 | /* Now expand the template argument pack "in place". */ |
| 9450 | for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) |
| 9451 | TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx); |
| 9452 | } |
| 9453 | else |
| 9454 | { |
| 9455 | TREE_VEC_ELT (t, out) = elts[i]; |
| 9456 | out++; |
| 9457 | } |
| 9458 | } |
| 9459 | |
| 9460 | return t; |
| 9461 | } |
| 9462 | |
| 9463 | /* Return the result of substituting ARGS into the template parameters |
| 9464 | given by PARMS. If there are m levels of ARGS and m + n levels of |
| 9465 | PARMS, then the result will contain n levels of PARMS. For |
| 9466 | example, if PARMS is `template <class T> template <class U> |
| 9467 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the |
| 9468 | result will be `template <int*, double, class V>'. */ |
| 9469 | |
| 9470 | static tree |
| 9471 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
| 9472 | { |
| 9473 | tree r = NULL_TREE; |
| 9474 | tree* new_parms; |
| 9475 | |
| 9476 | /* When substituting into a template, we must set |
| 9477 | PROCESSING_TEMPLATE_DECL as the template parameters may be |
| 9478 | dependent if they are based on one-another, and the dependency |
| 9479 | predicates are short-circuit outside of templates. */ |
| 9480 | ++processing_template_decl; |
| 9481 | |
| 9482 | for (new_parms = &r; |
| 9483 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); |
| 9484 | new_parms = &(TREE_CHAIN (*new_parms)), |
| 9485 | parms = TREE_CHAIN (parms)) |
| 9486 | { |
| 9487 | tree new_vec = |
| 9488 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
| 9489 | int i; |
| 9490 | |
| 9491 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
| 9492 | { |
| 9493 | tree tuple; |
| 9494 | |
| 9495 | if (parms == error_mark_node) |
| 9496 | continue; |
| 9497 | |
| 9498 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); |
| 9499 | |
| 9500 | if (tuple == error_mark_node) |
| 9501 | continue; |
| 9502 | |
| 9503 | TREE_VEC_ELT (new_vec, i) = |
| 9504 | tsubst_template_parm (tuple, args, complain); |
| 9505 | } |
| 9506 | |
| 9507 | *new_parms = |
| 9508 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) |
| 9509 | - TMPL_ARGS_DEPTH (args)), |
| 9510 | new_vec, NULL_TREE); |
| 9511 | } |
| 9512 | |
| 9513 | --processing_template_decl; |
| 9514 | |
| 9515 | return r; |
| 9516 | } |
| 9517 | |
| 9518 | /* Return the result of substituting ARGS into one template parameter |
| 9519 | given by T. T Must be a TREE_LIST which TREE_VALUE is the template |
| 9520 | parameter and which TREE_PURPOSE is the default argument of the |
| 9521 | template parameter. */ |
| 9522 | |
| 9523 | static tree |
| 9524 | tsubst_template_parm (tree t, tree args, tsubst_flags_t complain) |
| 9525 | { |
| 9526 | tree default_value, parm_decl; |
| 9527 | |
| 9528 | if (args == NULL_TREE |
| 9529 | || t == NULL_TREE |
| 9530 | || t == error_mark_node) |
| 9531 | return t; |
| 9532 | |
| 9533 | gcc_assert (TREE_CODE (t) == TREE_LIST); |
| 9534 | |
| 9535 | default_value = TREE_PURPOSE (t); |
| 9536 | parm_decl = TREE_VALUE (t); |
| 9537 | |
| 9538 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); |
| 9539 | if (TREE_CODE (parm_decl) == PARM_DECL |
| 9540 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) |
| 9541 | parm_decl = error_mark_node; |
| 9542 | default_value = tsubst_template_arg (default_value, args, |
| 9543 | complain, NULL_TREE); |
| 9544 | |
| 9545 | return build_tree_list (default_value, parm_decl); |
| 9546 | } |
| 9547 | |
| 9548 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
| 9549 | type T. If T is not an aggregate or enumeration type, it is |
| 9550 | handled as if by tsubst. IN_DECL is as for tsubst. If |
| 9551 | ENTERING_SCOPE is nonzero, T is the context for a template which |
| 9552 | we are presently tsubst'ing. Return the substituted value. */ |
| 9553 | |
| 9554 | static tree |
| 9555 | tsubst_aggr_type (tree t, |
| 9556 | tree args, |
| 9557 | tsubst_flags_t complain, |
| 9558 | tree in_decl, |
| 9559 | int entering_scope) |
| 9560 | { |
| 9561 | if (t == NULL_TREE) |
| 9562 | return NULL_TREE; |
| 9563 | |
| 9564 | switch (TREE_CODE (t)) |
| 9565 | { |
| 9566 | case RECORD_TYPE: |
| 9567 | if (TYPE_PTRMEMFUNC_P (t)) |
| 9568 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
| 9569 | |
| 9570 | /* Else fall through. */ |
| 9571 | case ENUMERAL_TYPE: |
| 9572 | case UNION_TYPE: |
| 9573 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
| 9574 | { |
| 9575 | tree argvec; |
| 9576 | tree context; |
| 9577 | tree r; |
| 9578 | int saved_unevaluated_operand; |
| 9579 | int saved_inhibit_evaluation_warnings; |
| 9580 | |
| 9581 | /* In "sizeof(X<I>)" we need to evaluate "I". */ |
| 9582 | saved_unevaluated_operand = cp_unevaluated_operand; |
| 9583 | cp_unevaluated_operand = 0; |
| 9584 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; |
| 9585 | c_inhibit_evaluation_warnings = 0; |
| 9586 | |
| 9587 | /* First, determine the context for the type we are looking |
| 9588 | up. */ |
| 9589 | context = TYPE_CONTEXT (t); |
| 9590 | if (context && TYPE_P (context)) |
| 9591 | { |
| 9592 | context = tsubst_aggr_type (context, args, complain, |
| 9593 | in_decl, /*entering_scope=*/1); |
| 9594 | /* If context is a nested class inside a class template, |
| 9595 | it may still need to be instantiated (c++/33959). */ |
| 9596 | context = complete_type (context); |
| 9597 | } |
| 9598 | |
| 9599 | /* Then, figure out what arguments are appropriate for the |
| 9600 | type we are trying to find. For example, given: |
| 9601 | |
| 9602 | template <class T> struct S; |
| 9603 | template <class T, class U> void f(T, U) { S<U> su; } |
| 9604 | |
| 9605 | and supposing that we are instantiating f<int, double>, |
| 9606 | then our ARGS will be {int, double}, but, when looking up |
| 9607 | S we only want {double}. */ |
| 9608 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
| 9609 | complain, in_decl); |
| 9610 | if (argvec == error_mark_node) |
| 9611 | r = error_mark_node; |
| 9612 | else |
| 9613 | { |
| 9614 | r = lookup_template_class (t, argvec, in_decl, context, |
| 9615 | entering_scope, complain); |
| 9616 | r = cp_build_qualified_type_real (r, cp_type_quals (t), complain); |
| 9617 | } |
| 9618 | |
| 9619 | cp_unevaluated_operand = saved_unevaluated_operand; |
| 9620 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; |
| 9621 | |
| 9622 | return r; |
| 9623 | } |
| 9624 | else |
| 9625 | /* This is not a template type, so there's nothing to do. */ |
| 9626 | return t; |
| 9627 | |
| 9628 | default: |
| 9629 | return tsubst (t, args, complain, in_decl); |
| 9630 | } |
| 9631 | } |
| 9632 | |
| 9633 | /* Substitute into the default argument ARG (a default argument for |
| 9634 | FN), which has the indicated TYPE. */ |
| 9635 | |
| 9636 | tree |
| 9637 | tsubst_default_argument (tree fn, tree type, tree arg) |
| 9638 | { |
| 9639 | tree saved_class_ptr = NULL_TREE; |
| 9640 | tree saved_class_ref = NULL_TREE; |
| 9641 | |
| 9642 | /* This can happen in invalid code. */ |
| 9643 | if (TREE_CODE (arg) == DEFAULT_ARG) |
| 9644 | return arg; |
| 9645 | |
| 9646 | /* This default argument came from a template. Instantiate the |
| 9647 | default argument here, not in tsubst. In the case of |
| 9648 | something like: |
| 9649 | |
| 9650 | template <class T> |
| 9651 | struct S { |
| 9652 | static T t(); |
| 9653 | void f(T = t()); |
| 9654 | }; |
| 9655 | |
| 9656 | we must be careful to do name lookup in the scope of S<T>, |
| 9657 | rather than in the current class. */ |
| 9658 | push_access_scope (fn); |
| 9659 | /* The "this" pointer is not valid in a default argument. */ |
| 9660 | if (cfun) |
| 9661 | { |
| 9662 | saved_class_ptr = current_class_ptr; |
| 9663 | cp_function_chain->x_current_class_ptr = NULL_TREE; |
| 9664 | saved_class_ref = current_class_ref; |
| 9665 | cp_function_chain->x_current_class_ref = NULL_TREE; |
| 9666 | } |
| 9667 | |
| 9668 | push_deferring_access_checks(dk_no_deferred); |
| 9669 | /* The default argument expression may cause implicitly defined |
| 9670 | member functions to be synthesized, which will result in garbage |
| 9671 | collection. We must treat this situation as if we were within |
| 9672 | the body of function so as to avoid collecting live data on the |
| 9673 | stack. */ |
| 9674 | ++function_depth; |
| 9675 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
| 9676 | tf_warning_or_error, NULL_TREE, |
| 9677 | /*integral_constant_expression_p=*/false); |
| 9678 | --function_depth; |
| 9679 | pop_deferring_access_checks(); |
| 9680 | |
| 9681 | /* Restore the "this" pointer. */ |
| 9682 | if (cfun) |
| 9683 | { |
| 9684 | cp_function_chain->x_current_class_ptr = saved_class_ptr; |
| 9685 | cp_function_chain->x_current_class_ref = saved_class_ref; |
| 9686 | } |
| 9687 | |
| 9688 | /* Make sure the default argument is reasonable. */ |
| 9689 | arg = check_default_argument (type, arg); |
| 9690 | |
| 9691 | pop_access_scope (fn); |
| 9692 | |
| 9693 | return arg; |
| 9694 | } |
| 9695 | |
| 9696 | /* Substitute into all the default arguments for FN. */ |
| 9697 | |
| 9698 | static void |
| 9699 | tsubst_default_arguments (tree fn) |
| 9700 | { |
| 9701 | tree arg; |
| 9702 | tree tmpl_args; |
| 9703 | |
| 9704 | tmpl_args = DECL_TI_ARGS (fn); |
| 9705 | |
| 9706 | /* If this function is not yet instantiated, we certainly don't need |
| 9707 | its default arguments. */ |
| 9708 | if (uses_template_parms (tmpl_args)) |
| 9709 | return; |
| 9710 | /* Don't do this again for clones. */ |
| 9711 | if (DECL_CLONED_FUNCTION_P (fn)) |
| 9712 | return; |
| 9713 | |
| 9714 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 9715 | arg; |
| 9716 | arg = TREE_CHAIN (arg)) |
| 9717 | if (TREE_PURPOSE (arg)) |
| 9718 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
| 9719 | TREE_VALUE (arg), |
| 9720 | TREE_PURPOSE (arg)); |
| 9721 | } |
| 9722 | |
| 9723 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
| 9724 | result of the substitution. Issue error and warning messages under |
| 9725 | control of COMPLAIN. */ |
| 9726 | |
| 9727 | static tree |
| 9728 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
| 9729 | { |
| 9730 | #define RETURN(EXP) do { r = (EXP); goto out; } while(0) |
| 9731 | location_t saved_loc; |
| 9732 | tree r = NULL_TREE; |
| 9733 | tree in_decl = t; |
| 9734 | hashval_t hash = 0; |
| 9735 | |
| 9736 | /* Set the filename and linenumber to improve error-reporting. */ |
| 9737 | saved_loc = input_location; |
| 9738 | input_location = DECL_SOURCE_LOCATION (t); |
| 9739 | |
| 9740 | switch (TREE_CODE (t)) |
| 9741 | { |
| 9742 | case TEMPLATE_DECL: |
| 9743 | { |
| 9744 | /* We can get here when processing a member function template, |
| 9745 | member class template, or template template parameter. */ |
| 9746 | tree decl = DECL_TEMPLATE_RESULT (t); |
| 9747 | tree spec; |
| 9748 | tree tmpl_args; |
| 9749 | tree full_args; |
| 9750 | |
| 9751 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
| 9752 | { |
| 9753 | /* Template template parameter is treated here. */ |
| 9754 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 9755 | if (new_type == error_mark_node) |
| 9756 | RETURN (error_mark_node); |
| 9757 | |
| 9758 | r = copy_decl (t); |
| 9759 | DECL_CHAIN (r) = NULL_TREE; |
| 9760 | TREE_TYPE (r) = new_type; |
| 9761 | DECL_TEMPLATE_RESULT (r) |
| 9762 | = build_decl (DECL_SOURCE_LOCATION (decl), |
| 9763 | TYPE_DECL, DECL_NAME (decl), new_type); |
| 9764 | DECL_TEMPLATE_PARMS (r) |
| 9765 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
| 9766 | complain); |
| 9767 | TYPE_NAME (new_type) = r; |
| 9768 | break; |
| 9769 | } |
| 9770 | |
| 9771 | /* We might already have an instance of this template. |
| 9772 | The ARGS are for the surrounding class type, so the |
| 9773 | full args contain the tsubst'd args for the context, |
| 9774 | plus the innermost args from the template decl. */ |
| 9775 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
| 9776 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
| 9777 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); |
| 9778 | /* Because this is a template, the arguments will still be |
| 9779 | dependent, even after substitution. If |
| 9780 | PROCESSING_TEMPLATE_DECL is not set, the dependency |
| 9781 | predicates will short-circuit. */ |
| 9782 | ++processing_template_decl; |
| 9783 | full_args = tsubst_template_args (tmpl_args, args, |
| 9784 | complain, in_decl); |
| 9785 | --processing_template_decl; |
| 9786 | if (full_args == error_mark_node) |
| 9787 | RETURN (error_mark_node); |
| 9788 | |
| 9789 | /* If this is a default template template argument, |
| 9790 | tsubst might not have changed anything. */ |
| 9791 | if (full_args == tmpl_args) |
| 9792 | RETURN (t); |
| 9793 | |
| 9794 | hash = hash_tmpl_and_args (t, full_args); |
| 9795 | spec = retrieve_specialization (t, full_args, hash); |
| 9796 | if (spec != NULL_TREE) |
| 9797 | { |
| 9798 | r = spec; |
| 9799 | break; |
| 9800 | } |
| 9801 | |
| 9802 | /* Make a new template decl. It will be similar to the |
| 9803 | original, but will record the current template arguments. |
| 9804 | We also create a new function declaration, which is just |
| 9805 | like the old one, but points to this new template, rather |
| 9806 | than the old one. */ |
| 9807 | r = copy_decl (t); |
| 9808 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
| 9809 | DECL_CHAIN (r) = NULL_TREE; |
| 9810 | |
| 9811 | DECL_TEMPLATE_INFO (r) = build_template_info (t, args); |
| 9812 | |
| 9813 | if (TREE_CODE (decl) == TYPE_DECL |
| 9814 | && !TYPE_DECL_ALIAS_P (decl)) |
| 9815 | { |
| 9816 | tree new_type; |
| 9817 | ++processing_template_decl; |
| 9818 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 9819 | --processing_template_decl; |
| 9820 | if (new_type == error_mark_node) |
| 9821 | RETURN (error_mark_node); |
| 9822 | |
| 9823 | TREE_TYPE (r) = new_type; |
| 9824 | CLASSTYPE_TI_TEMPLATE (new_type) = r; |
| 9825 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
| 9826 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
| 9827 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
| 9828 | } |
| 9829 | else |
| 9830 | { |
| 9831 | tree new_decl; |
| 9832 | ++processing_template_decl; |
| 9833 | new_decl = tsubst (decl, args, complain, in_decl); |
| 9834 | --processing_template_decl; |
| 9835 | if (new_decl == error_mark_node) |
| 9836 | RETURN (error_mark_node); |
| 9837 | |
| 9838 | DECL_TEMPLATE_RESULT (r) = new_decl; |
| 9839 | DECL_TI_TEMPLATE (new_decl) = r; |
| 9840 | TREE_TYPE (r) = TREE_TYPE (new_decl); |
| 9841 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); |
| 9842 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
| 9843 | } |
| 9844 | |
| 9845 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
| 9846 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; |
| 9847 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; |
| 9848 | |
| 9849 | /* The template parameters for this new template are all the |
| 9850 | template parameters for the old template, except the |
| 9851 | outermost level of parameters. */ |
| 9852 | DECL_TEMPLATE_PARMS (r) |
| 9853 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
| 9854 | complain); |
| 9855 | |
| 9856 | if (PRIMARY_TEMPLATE_P (t)) |
| 9857 | DECL_PRIMARY_TEMPLATE (r) = r; |
| 9858 | |
| 9859 | if (TREE_CODE (decl) != TYPE_DECL) |
| 9860 | /* Record this non-type partial instantiation. */ |
| 9861 | register_specialization (r, t, |
| 9862 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
| 9863 | false, hash); |
| 9864 | } |
| 9865 | break; |
| 9866 | |
| 9867 | case FUNCTION_DECL: |
| 9868 | { |
| 9869 | tree ctx; |
| 9870 | tree argvec = NULL_TREE; |
| 9871 | tree *friends; |
| 9872 | tree gen_tmpl; |
| 9873 | tree type; |
| 9874 | int member; |
| 9875 | int args_depth; |
| 9876 | int parms_depth; |
| 9877 | |
| 9878 | /* Nobody should be tsubst'ing into non-template functions. */ |
| 9879 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
| 9880 | |
| 9881 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) |
| 9882 | { |
| 9883 | tree spec; |
| 9884 | bool dependent_p; |
| 9885 | |
| 9886 | /* If T is not dependent, just return it. We have to |
| 9887 | increment PROCESSING_TEMPLATE_DECL because |
| 9888 | value_dependent_expression_p assumes that nothing is |
| 9889 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ |
| 9890 | ++processing_template_decl; |
| 9891 | dependent_p = value_dependent_expression_p (t); |
| 9892 | --processing_template_decl; |
| 9893 | if (!dependent_p) |
| 9894 | RETURN (t); |
| 9895 | |
| 9896 | /* Calculate the most general template of which R is a |
| 9897 | specialization, and the complete set of arguments used to |
| 9898 | specialize R. */ |
| 9899 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); |
| 9900 | argvec = tsubst_template_args (DECL_TI_ARGS |
| 9901 | (DECL_TEMPLATE_RESULT |
| 9902 | (DECL_TI_TEMPLATE (t))), |
| 9903 | args, complain, in_decl); |
| 9904 | if (argvec == error_mark_node) |
| 9905 | RETURN (error_mark_node); |
| 9906 | |
| 9907 | /* Check to see if we already have this specialization. */ |
| 9908 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
| 9909 | spec = retrieve_specialization (gen_tmpl, argvec, hash); |
| 9910 | |
| 9911 | if (spec) |
| 9912 | { |
| 9913 | r = spec; |
| 9914 | break; |
| 9915 | } |
| 9916 | |
| 9917 | /* We can see more levels of arguments than parameters if |
| 9918 | there was a specialization of a member template, like |
| 9919 | this: |
| 9920 | |
| 9921 | template <class T> struct S { template <class U> void f(); } |
| 9922 | template <> template <class U> void S<int>::f(U); |
| 9923 | |
| 9924 | Here, we'll be substituting into the specialization, |
| 9925 | because that's where we can find the code we actually |
| 9926 | want to generate, but we'll have enough arguments for |
| 9927 | the most general template. |
| 9928 | |
| 9929 | We also deal with the peculiar case: |
| 9930 | |
| 9931 | template <class T> struct S { |
| 9932 | template <class U> friend void f(); |
| 9933 | }; |
| 9934 | template <class U> void f() {} |
| 9935 | template S<int>; |
| 9936 | template void f<double>(); |
| 9937 | |
| 9938 | Here, the ARGS for the instantiation of will be {int, |
| 9939 | double}. But, we only need as many ARGS as there are |
| 9940 | levels of template parameters in CODE_PATTERN. We are |
| 9941 | careful not to get fooled into reducing the ARGS in |
| 9942 | situations like: |
| 9943 | |
| 9944 | template <class T> struct S { template <class U> void f(U); } |
| 9945 | template <class T> template <> void S<T>::f(int) {} |
| 9946 | |
| 9947 | which we can spot because the pattern will be a |
| 9948 | specialization in this case. */ |
| 9949 | args_depth = TMPL_ARGS_DEPTH (args); |
| 9950 | parms_depth = |
| 9951 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); |
| 9952 | if (args_depth > parms_depth |
| 9953 | && !DECL_TEMPLATE_SPECIALIZATION (t)) |
| 9954 | args = get_innermost_template_args (args, parms_depth); |
| 9955 | } |
| 9956 | else |
| 9957 | { |
| 9958 | /* This special case arises when we have something like this: |
| 9959 | |
| 9960 | template <class T> struct S { |
| 9961 | friend void f<int>(int, double); |
| 9962 | }; |
| 9963 | |
| 9964 | Here, the DECL_TI_TEMPLATE for the friend declaration |
| 9965 | will be an IDENTIFIER_NODE. We are being called from |
| 9966 | tsubst_friend_function, and we want only to create a |
| 9967 | new decl (R) with appropriate types so that we can call |
| 9968 | determine_specialization. */ |
| 9969 | gen_tmpl = NULL_TREE; |
| 9970 | } |
| 9971 | |
| 9972 | if (DECL_CLASS_SCOPE_P (t)) |
| 9973 | { |
| 9974 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
| 9975 | member = 2; |
| 9976 | else |
| 9977 | member = 1; |
| 9978 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
| 9979 | complain, t, /*entering_scope=*/1); |
| 9980 | } |
| 9981 | else |
| 9982 | { |
| 9983 | member = 0; |
| 9984 | ctx = DECL_CONTEXT (t); |
| 9985 | } |
| 9986 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 9987 | if (type == error_mark_node) |
| 9988 | RETURN (error_mark_node); |
| 9989 | |
| 9990 | /* We do NOT check for matching decls pushed separately at this |
| 9991 | point, as they may not represent instantiations of this |
| 9992 | template, and in any case are considered separate under the |
| 9993 | discrete model. */ |
| 9994 | r = copy_decl (t); |
| 9995 | DECL_USE_TEMPLATE (r) = 0; |
| 9996 | TREE_TYPE (r) = type; |
| 9997 | /* Clear out the mangled name and RTL for the instantiation. */ |
| 9998 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); |
| 9999 | SET_DECL_RTL (r, NULL); |
| 10000 | /* Leave DECL_INITIAL set on deleted instantiations. */ |
| 10001 | if (!DECL_DELETED_FN (r)) |
| 10002 | DECL_INITIAL (r) = NULL_TREE; |
| 10003 | DECL_CONTEXT (r) = ctx; |
| 10004 | |
| 10005 | if (member && DECL_CONV_FN_P (r)) |
| 10006 | /* Type-conversion operator. Reconstruct the name, in |
| 10007 | case it's the name of one of the template's parameters. */ |
| 10008 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); |
| 10009 | |
| 10010 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
| 10011 | complain, t); |
| 10012 | DECL_RESULT (r) = NULL_TREE; |
| 10013 | |
| 10014 | TREE_STATIC (r) = 0; |
| 10015 | TREE_PUBLIC (r) = TREE_PUBLIC (t); |
| 10016 | DECL_EXTERNAL (r) = 1; |
| 10017 | /* If this is an instantiation of a function with internal |
| 10018 | linkage, we already know what object file linkage will be |
| 10019 | assigned to the instantiation. */ |
| 10020 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); |
| 10021 | DECL_DEFER_OUTPUT (r) = 0; |
| 10022 | DECL_CHAIN (r) = NULL_TREE; |
| 10023 | DECL_PENDING_INLINE_INFO (r) = 0; |
| 10024 | DECL_PENDING_INLINE_P (r) = 0; |
| 10025 | DECL_SAVED_TREE (r) = NULL_TREE; |
| 10026 | DECL_STRUCT_FUNCTION (r) = NULL; |
| 10027 | TREE_USED (r) = 0; |
| 10028 | /* We'll re-clone as appropriate in instantiate_template. */ |
| 10029 | DECL_CLONED_FUNCTION (r) = NULL_TREE; |
| 10030 | |
| 10031 | /* If we aren't complaining now, return on error before we register |
| 10032 | the specialization so that we'll complain eventually. */ |
| 10033 | if ((complain & tf_error) == 0 |
| 10034 | && IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
| 10035 | && !grok_op_properties (r, /*complain=*/false)) |
| 10036 | RETURN (error_mark_node); |
| 10037 | |
| 10038 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
| 10039 | this in the special friend case mentioned above where |
| 10040 | GEN_TMPL is NULL. */ |
| 10041 | if (gen_tmpl) |
| 10042 | { |
| 10043 | DECL_TEMPLATE_INFO (r) |
| 10044 | = build_template_info (gen_tmpl, argvec); |
| 10045 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
| 10046 | register_specialization (r, gen_tmpl, argvec, false, hash); |
| 10047 | |
| 10048 | /* We're not supposed to instantiate default arguments |
| 10049 | until they are called, for a template. But, for a |
| 10050 | declaration like: |
| 10051 | |
| 10052 | template <class T> void f () |
| 10053 | { extern void g(int i = T()); } |
| 10054 | |
| 10055 | we should do the substitution when the template is |
| 10056 | instantiated. We handle the member function case in |
| 10057 | instantiate_class_template since the default arguments |
| 10058 | might refer to other members of the class. */ |
| 10059 | if (!member |
| 10060 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
| 10061 | && !uses_template_parms (argvec)) |
| 10062 | tsubst_default_arguments (r); |
| 10063 | } |
| 10064 | else |
| 10065 | DECL_TEMPLATE_INFO (r) = NULL_TREE; |
| 10066 | |
| 10067 | /* Copy the list of befriending classes. */ |
| 10068 | for (friends = &DECL_BEFRIENDING_CLASSES (r); |
| 10069 | *friends; |
| 10070 | friends = &TREE_CHAIN (*friends)) |
| 10071 | { |
| 10072 | *friends = copy_node (*friends); |
| 10073 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), |
| 10074 | args, complain, |
| 10075 | in_decl); |
| 10076 | } |
| 10077 | |
| 10078 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
| 10079 | { |
| 10080 | maybe_retrofit_in_chrg (r); |
| 10081 | if (DECL_CONSTRUCTOR_P (r)) |
| 10082 | grok_ctor_properties (ctx, r); |
| 10083 | /* If this is an instantiation of a member template, clone it. |
| 10084 | If it isn't, that'll be handled by |
| 10085 | clone_constructors_and_destructors. */ |
| 10086 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
| 10087 | clone_function_decl (r, /*update_method_vec_p=*/0); |
| 10088 | } |
| 10089 | else if ((complain & tf_error) != 0 |
| 10090 | && IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
| 10091 | && !grok_op_properties (r, /*complain=*/true)) |
| 10092 | RETURN (error_mark_node); |
| 10093 | |
| 10094 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) |
| 10095 | SET_DECL_FRIEND_CONTEXT (r, |
| 10096 | tsubst (DECL_FRIEND_CONTEXT (t), |
| 10097 | args, complain, in_decl)); |
| 10098 | |
| 10099 | /* Possibly limit visibility based on template args. */ |
| 10100 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; |
| 10101 | if (DECL_VISIBILITY_SPECIFIED (t)) |
| 10102 | { |
| 10103 | DECL_VISIBILITY_SPECIFIED (r) = 0; |
| 10104 | DECL_ATTRIBUTES (r) |
| 10105 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); |
| 10106 | } |
| 10107 | determine_visibility (r); |
| 10108 | if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r) |
| 10109 | && !processing_template_decl) |
| 10110 | defaulted_late_check (r); |
| 10111 | |
| 10112 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
| 10113 | args, complain, in_decl); |
| 10114 | } |
| 10115 | break; |
| 10116 | |
| 10117 | case PARM_DECL: |
| 10118 | { |
| 10119 | tree type = NULL_TREE; |
| 10120 | int i, len = 1; |
| 10121 | tree expanded_types = NULL_TREE; |
| 10122 | tree prev_r = NULL_TREE; |
| 10123 | tree first_r = NULL_TREE; |
| 10124 | |
| 10125 | if (FUNCTION_PARAMETER_PACK_P (t)) |
| 10126 | { |
| 10127 | /* If there is a local specialization that isn't a |
| 10128 | parameter pack, it means that we're doing a "simple" |
| 10129 | substitution from inside tsubst_pack_expansion. Just |
| 10130 | return the local specialization (which will be a single |
| 10131 | parm). */ |
| 10132 | tree spec = retrieve_local_specialization (t); |
| 10133 | if (spec |
| 10134 | && TREE_CODE (spec) == PARM_DECL |
| 10135 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) |
| 10136 | RETURN (spec); |
| 10137 | |
| 10138 | /* Expand the TYPE_PACK_EXPANSION that provides the types for |
| 10139 | the parameters in this function parameter pack. */ |
| 10140 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, |
| 10141 | complain, in_decl); |
| 10142 | if (TREE_CODE (expanded_types) == TREE_VEC) |
| 10143 | { |
| 10144 | len = TREE_VEC_LENGTH (expanded_types); |
| 10145 | |
| 10146 | /* Zero-length parameter packs are boring. Just substitute |
| 10147 | into the chain. */ |
| 10148 | if (len == 0) |
| 10149 | RETURN (tsubst (TREE_CHAIN (t), args, complain, |
| 10150 | TREE_CHAIN (t))); |
| 10151 | } |
| 10152 | else |
| 10153 | { |
| 10154 | /* All we did was update the type. Make a note of that. */ |
| 10155 | type = expanded_types; |
| 10156 | expanded_types = NULL_TREE; |
| 10157 | } |
| 10158 | } |
| 10159 | |
| 10160 | /* Loop through all of the parameter's we'll build. When T is |
| 10161 | a function parameter pack, LEN is the number of expanded |
| 10162 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ |
| 10163 | r = NULL_TREE; |
| 10164 | for (i = 0; i < len; ++i) |
| 10165 | { |
| 10166 | prev_r = r; |
| 10167 | r = copy_node (t); |
| 10168 | if (DECL_TEMPLATE_PARM_P (t)) |
| 10169 | SET_DECL_TEMPLATE_PARM_P (r); |
| 10170 | |
| 10171 | if (expanded_types) |
| 10172 | /* We're on the Ith parameter of the function parameter |
| 10173 | pack. */ |
| 10174 | { |
| 10175 | /* An argument of a function parameter pack is not a parameter |
| 10176 | pack. */ |
| 10177 | FUNCTION_PARAMETER_PACK_P (r) = false; |
| 10178 | |
| 10179 | /* Get the Ith type. */ |
| 10180 | type = TREE_VEC_ELT (expanded_types, i); |
| 10181 | |
| 10182 | /* Rename the parameter to include the index. */ |
| 10183 | DECL_NAME (r) |
| 10184 | = make_ith_pack_parameter_name (DECL_NAME (r), i); |
| 10185 | } |
| 10186 | else if (!type) |
| 10187 | /* We're dealing with a normal parameter. */ |
| 10188 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 10189 | |
| 10190 | type = type_decays_to (type); |
| 10191 | TREE_TYPE (r) = type; |
| 10192 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
| 10193 | |
| 10194 | if (DECL_INITIAL (r)) |
| 10195 | { |
| 10196 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
| 10197 | DECL_INITIAL (r) = TREE_TYPE (r); |
| 10198 | else |
| 10199 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
| 10200 | complain, in_decl); |
| 10201 | } |
| 10202 | |
| 10203 | DECL_CONTEXT (r) = NULL_TREE; |
| 10204 | |
| 10205 | if (!DECL_TEMPLATE_PARM_P (r)) |
| 10206 | DECL_ARG_TYPE (r) = type_passed_as (type); |
| 10207 | |
| 10208 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
| 10209 | args, complain, in_decl); |
| 10210 | |
| 10211 | /* Keep track of the first new parameter we |
| 10212 | generate. That's what will be returned to the |
| 10213 | caller. */ |
| 10214 | if (!first_r) |
| 10215 | first_r = r; |
| 10216 | |
| 10217 | /* Build a proper chain of parameters when substituting |
| 10218 | into a function parameter pack. */ |
| 10219 | if (prev_r) |
| 10220 | DECL_CHAIN (prev_r) = r; |
| 10221 | } |
| 10222 | |
| 10223 | if (DECL_CHAIN (t)) |
| 10224 | DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args, |
| 10225 | complain, DECL_CHAIN (t)); |
| 10226 | |
| 10227 | /* FIRST_R contains the start of the chain we've built. */ |
| 10228 | r = first_r; |
| 10229 | } |
| 10230 | break; |
| 10231 | |
| 10232 | case FIELD_DECL: |
| 10233 | { |
| 10234 | tree type; |
| 10235 | |
| 10236 | r = copy_decl (t); |
| 10237 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 10238 | if (type == error_mark_node) |
| 10239 | RETURN (error_mark_node); |
| 10240 | TREE_TYPE (r) = type; |
| 10241 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
| 10242 | |
| 10243 | if (DECL_C_BIT_FIELD (r)) |
| 10244 | /* For bit-fields, DECL_INITIAL gives the number of bits. For |
| 10245 | non-bit-fields DECL_INITIAL is a non-static data member |
| 10246 | initializer, which gets deferred instantiation. */ |
| 10247 | DECL_INITIAL (r) |
| 10248 | = tsubst_expr (DECL_INITIAL (t), args, |
| 10249 | complain, in_decl, |
| 10250 | /*integral_constant_expression_p=*/true); |
| 10251 | else if (DECL_INITIAL (t)) |
| 10252 | { |
| 10253 | /* Set up DECL_TEMPLATE_INFO so that we can get at the |
| 10254 | NSDMI in perform_member_init. Still set DECL_INITIAL |
| 10255 | so that we know there is one. */ |
| 10256 | DECL_INITIAL (r) = void_zero_node; |
| 10257 | gcc_assert (DECL_LANG_SPECIFIC (r) == NULL); |
| 10258 | retrofit_lang_decl (r); |
| 10259 | DECL_TEMPLATE_INFO (r) = build_template_info (t, args); |
| 10260 | } |
| 10261 | /* We don't have to set DECL_CONTEXT here; it is set by |
| 10262 | finish_member_declaration. */ |
| 10263 | DECL_CHAIN (r) = NULL_TREE; |
| 10264 | if (VOID_TYPE_P (type)) |
| 10265 | error ("instantiation of %q+D as type %qT", r, type); |
| 10266 | |
| 10267 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, |
| 10268 | args, complain, in_decl); |
| 10269 | } |
| 10270 | break; |
| 10271 | |
| 10272 | case USING_DECL: |
| 10273 | /* We reach here only for member using decls. We also need to check |
| 10274 | uses_template_parms because DECL_DEPENDENT_P is not set for a |
| 10275 | using-declaration that designates a member of the current |
| 10276 | instantiation (c++/53549). */ |
| 10277 | if (DECL_DEPENDENT_P (t) |
| 10278 | || uses_template_parms (USING_DECL_SCOPE (t))) |
| 10279 | { |
| 10280 | r = do_class_using_decl |
| 10281 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), |
| 10282 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); |
| 10283 | if (!r) |
| 10284 | r = error_mark_node; |
| 10285 | else |
| 10286 | { |
| 10287 | TREE_PROTECTED (r) = TREE_PROTECTED (t); |
| 10288 | TREE_PRIVATE (r) = TREE_PRIVATE (t); |
| 10289 | } |
| 10290 | } |
| 10291 | else |
| 10292 | { |
| 10293 | r = copy_node (t); |
| 10294 | DECL_CHAIN (r) = NULL_TREE; |
| 10295 | } |
| 10296 | break; |
| 10297 | |
| 10298 | case TYPE_DECL: |
| 10299 | case VAR_DECL: |
| 10300 | { |
| 10301 | tree argvec = NULL_TREE; |
| 10302 | tree gen_tmpl = NULL_TREE; |
| 10303 | tree spec; |
| 10304 | tree tmpl = NULL_TREE; |
| 10305 | tree ctx; |
| 10306 | tree type = NULL_TREE; |
| 10307 | bool local_p; |
| 10308 | |
| 10309 | if (TREE_CODE (t) == TYPE_DECL |
| 10310 | && t == TYPE_MAIN_DECL (TREE_TYPE (t))) |
| 10311 | { |
| 10312 | /* If this is the canonical decl, we don't have to |
| 10313 | mess with instantiations, and often we can't (for |
| 10314 | typename, template type parms and such). Note that |
| 10315 | TYPE_NAME is not correct for the above test if |
| 10316 | we've copied the type for a typedef. */ |
| 10317 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 10318 | if (type == error_mark_node) |
| 10319 | RETURN (error_mark_node); |
| 10320 | r = TYPE_NAME (type); |
| 10321 | break; |
| 10322 | } |
| 10323 | |
| 10324 | /* Check to see if we already have the specialization we |
| 10325 | need. */ |
| 10326 | spec = NULL_TREE; |
| 10327 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) |
| 10328 | { |
| 10329 | /* T is a static data member or namespace-scope entity. |
| 10330 | We have to substitute into namespace-scope variables |
| 10331 | (even though such entities are never templates) because |
| 10332 | of cases like: |
| 10333 | |
| 10334 | template <class T> void f() { extern T t; } |
| 10335 | |
| 10336 | where the entity referenced is not known until |
| 10337 | instantiation time. */ |
| 10338 | local_p = false; |
| 10339 | ctx = DECL_CONTEXT (t); |
| 10340 | if (DECL_CLASS_SCOPE_P (t)) |
| 10341 | { |
| 10342 | ctx = tsubst_aggr_type (ctx, args, |
| 10343 | complain, |
| 10344 | in_decl, /*entering_scope=*/1); |
| 10345 | /* If CTX is unchanged, then T is in fact the |
| 10346 | specialization we want. That situation occurs when |
| 10347 | referencing a static data member within in its own |
| 10348 | class. We can use pointer equality, rather than |
| 10349 | same_type_p, because DECL_CONTEXT is always |
| 10350 | canonical... */ |
| 10351 | if (ctx == DECL_CONTEXT (t) |
| 10352 | && (TREE_CODE (t) != TYPE_DECL |
| 10353 | /* ... unless T is a member template; in which |
| 10354 | case our caller can be willing to create a |
| 10355 | specialization of that template represented |
| 10356 | by T. */ |
| 10357 | || !(DECL_TI_TEMPLATE (t) |
| 10358 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))) |
| 10359 | spec = t; |
| 10360 | } |
| 10361 | |
| 10362 | if (!spec) |
| 10363 | { |
| 10364 | tmpl = DECL_TI_TEMPLATE (t); |
| 10365 | gen_tmpl = most_general_template (tmpl); |
| 10366 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); |
| 10367 | if (argvec == error_mark_node) |
| 10368 | RETURN (error_mark_node); |
| 10369 | hash = hash_tmpl_and_args (gen_tmpl, argvec); |
| 10370 | spec = retrieve_specialization (gen_tmpl, argvec, hash); |
| 10371 | } |
| 10372 | } |
| 10373 | else |
| 10374 | { |
| 10375 | /* A local variable. */ |
| 10376 | local_p = true; |
| 10377 | /* Subsequent calls to pushdecl will fill this in. */ |
| 10378 | ctx = NULL_TREE; |
| 10379 | spec = retrieve_local_specialization (t); |
| 10380 | } |
| 10381 | /* If we already have the specialization we need, there is |
| 10382 | nothing more to do. */ |
| 10383 | if (spec) |
| 10384 | { |
| 10385 | r = spec; |
| 10386 | break; |
| 10387 | } |
| 10388 | |
| 10389 | if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t)) |
| 10390 | { |
| 10391 | /* Just use name lookup to find a member alias for an anonymous |
| 10392 | union, but then add it to the hash table. */ |
| 10393 | r = lookup_name (DECL_NAME (t)); |
| 10394 | gcc_assert (DECL_ANON_UNION_VAR_P (r)); |
| 10395 | register_local_specialization (r, t); |
| 10396 | break; |
| 10397 | } |
| 10398 | |
| 10399 | /* Create a new node for the specialization we need. */ |
| 10400 | r = copy_decl (t); |
| 10401 | if (type == NULL_TREE) |
| 10402 | { |
| 10403 | if (is_typedef_decl (t)) |
| 10404 | type = DECL_ORIGINAL_TYPE (t); |
| 10405 | else |
| 10406 | type = TREE_TYPE (t); |
| 10407 | if (TREE_CODE (t) == VAR_DECL |
| 10408 | && VAR_HAD_UNKNOWN_BOUND (t) |
| 10409 | && type != error_mark_node) |
| 10410 | type = strip_array_domain (type); |
| 10411 | type = tsubst (type, args, complain, in_decl); |
| 10412 | } |
| 10413 | if (TREE_CODE (r) == VAR_DECL) |
| 10414 | { |
| 10415 | /* Even if the original location is out of scope, the |
| 10416 | newly substituted one is not. */ |
| 10417 | DECL_DEAD_FOR_LOCAL (r) = 0; |
| 10418 | DECL_INITIALIZED_P (r) = 0; |
| 10419 | DECL_TEMPLATE_INSTANTIATED (r) = 0; |
| 10420 | if (type == error_mark_node) |
| 10421 | RETURN (error_mark_node); |
| 10422 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 10423 | { |
| 10424 | /* It may seem that this case cannot occur, since: |
| 10425 | |
| 10426 | typedef void f(); |
| 10427 | void g() { f x; } |
| 10428 | |
| 10429 | declares a function, not a variable. However: |
| 10430 | |
| 10431 | typedef void f(); |
| 10432 | template <typename T> void g() { T t; } |
| 10433 | template void g<f>(); |
| 10434 | |
| 10435 | is an attempt to declare a variable with function |
| 10436 | type. */ |
| 10437 | error ("variable %qD has function type", |
| 10438 | /* R is not yet sufficiently initialized, so we |
| 10439 | just use its name. */ |
| 10440 | DECL_NAME (r)); |
| 10441 | RETURN (error_mark_node); |
| 10442 | } |
| 10443 | type = complete_type (type); |
| 10444 | /* Wait until cp_finish_decl to set this again, to handle |
| 10445 | circular dependency (template/instantiate6.C). */ |
| 10446 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0; |
| 10447 | type = check_var_type (DECL_NAME (r), type); |
| 10448 | |
| 10449 | if (DECL_HAS_VALUE_EXPR_P (t)) |
| 10450 | { |
| 10451 | tree ve = DECL_VALUE_EXPR (t); |
| 10452 | ve = tsubst_expr (ve, args, complain, in_decl, |
| 10453 | /*constant_expression_p=*/false); |
| 10454 | if (REFERENCE_REF_P (ve)) |
| 10455 | { |
| 10456 | gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); |
| 10457 | ve = TREE_OPERAND (ve, 0); |
| 10458 | } |
| 10459 | SET_DECL_VALUE_EXPR (r, ve); |
| 10460 | } |
| 10461 | } |
| 10462 | else if (DECL_SELF_REFERENCE_P (t)) |
| 10463 | SET_DECL_SELF_REFERENCE_P (r); |
| 10464 | TREE_TYPE (r) = type; |
| 10465 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
| 10466 | DECL_CONTEXT (r) = ctx; |
| 10467 | /* Clear out the mangled name and RTL for the instantiation. */ |
| 10468 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); |
| 10469 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
| 10470 | SET_DECL_RTL (r, NULL); |
| 10471 | /* The initializer must not be expanded until it is required; |
| 10472 | see [temp.inst]. */ |
| 10473 | DECL_INITIAL (r) = NULL_TREE; |
| 10474 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
| 10475 | SET_DECL_RTL (r, NULL); |
| 10476 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
| 10477 | if (TREE_CODE (r) == VAR_DECL) |
| 10478 | { |
| 10479 | /* Possibly limit visibility based on template args. */ |
| 10480 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; |
| 10481 | if (DECL_VISIBILITY_SPECIFIED (t)) |
| 10482 | { |
| 10483 | DECL_VISIBILITY_SPECIFIED (r) = 0; |
| 10484 | DECL_ATTRIBUTES (r) |
| 10485 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); |
| 10486 | } |
| 10487 | determine_visibility (r); |
| 10488 | } |
| 10489 | |
| 10490 | if (!local_p) |
| 10491 | { |
| 10492 | /* A static data member declaration is always marked |
| 10493 | external when it is declared in-class, even if an |
| 10494 | initializer is present. We mimic the non-template |
| 10495 | processing here. */ |
| 10496 | DECL_EXTERNAL (r) = 1; |
| 10497 | |
| 10498 | register_specialization (r, gen_tmpl, argvec, false, hash); |
| 10499 | DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec); |
| 10500 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
| 10501 | } |
| 10502 | else if (cp_unevaluated_operand) |
| 10503 | { |
| 10504 | /* We're substituting this var in a decltype outside of its |
| 10505 | scope, such as for a lambda return type. Don't add it to |
| 10506 | local_specializations, do perform auto deduction. */ |
| 10507 | tree auto_node = type_uses_auto (type); |
| 10508 | if (auto_node) |
| 10509 | { |
| 10510 | tree init |
| 10511 | = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl, |
| 10512 | /*constant_expression_p=*/false); |
| 10513 | init = resolve_nondeduced_context (init); |
| 10514 | TREE_TYPE (r) = type |
| 10515 | = do_auto_deduction (type, init, auto_node); |
| 10516 | } |
| 10517 | } |
| 10518 | else |
| 10519 | register_local_specialization (r, t); |
| 10520 | |
| 10521 | DECL_CHAIN (r) = NULL_TREE; |
| 10522 | |
| 10523 | apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), |
| 10524 | /*flags=*/0, |
| 10525 | args, complain, in_decl); |
| 10526 | |
| 10527 | /* Preserve a typedef that names a type. */ |
| 10528 | if (is_typedef_decl (r)) |
| 10529 | { |
| 10530 | DECL_ORIGINAL_TYPE (r) = NULL_TREE; |
| 10531 | set_underlying_type (r); |
| 10532 | } |
| 10533 | |
| 10534 | layout_decl (r, 0); |
| 10535 | } |
| 10536 | break; |
| 10537 | |
| 10538 | default: |
| 10539 | gcc_unreachable (); |
| 10540 | } |
| 10541 | #undef RETURN |
| 10542 | |
| 10543 | out: |
| 10544 | /* Restore the file and line information. */ |
| 10545 | input_location = saved_loc; |
| 10546 | |
| 10547 | return r; |
| 10548 | } |
| 10549 | |
| 10550 | /* Substitute into the ARG_TYPES of a function type. |
| 10551 | If END is a TREE_CHAIN, leave it and any following types |
| 10552 | un-substituted. */ |
| 10553 | |
| 10554 | static tree |
| 10555 | tsubst_arg_types (tree arg_types, |
| 10556 | tree args, |
| 10557 | tree end, |
| 10558 | tsubst_flags_t complain, |
| 10559 | tree in_decl) |
| 10560 | { |
| 10561 | tree remaining_arg_types; |
| 10562 | tree type = NULL_TREE; |
| 10563 | int i = 1; |
| 10564 | tree expanded_args = NULL_TREE; |
| 10565 | tree default_arg; |
| 10566 | |
| 10567 | if (!arg_types || arg_types == void_list_node || arg_types == end) |
| 10568 | return arg_types; |
| 10569 | |
| 10570 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
| 10571 | args, end, complain, in_decl); |
| 10572 | if (remaining_arg_types == error_mark_node) |
| 10573 | return error_mark_node; |
| 10574 | |
| 10575 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
| 10576 | { |
| 10577 | /* For a pack expansion, perform substitution on the |
| 10578 | entire expression. Later on, we'll handle the arguments |
| 10579 | one-by-one. */ |
| 10580 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), |
| 10581 | args, complain, in_decl); |
| 10582 | |
| 10583 | if (TREE_CODE (expanded_args) == TREE_VEC) |
| 10584 | /* So that we'll spin through the parameters, one by one. */ |
| 10585 | i = TREE_VEC_LENGTH (expanded_args); |
| 10586 | else |
| 10587 | { |
| 10588 | /* We only partially substituted into the parameter |
| 10589 | pack. Our type is TYPE_PACK_EXPANSION. */ |
| 10590 | type = expanded_args; |
| 10591 | expanded_args = NULL_TREE; |
| 10592 | } |
| 10593 | } |
| 10594 | |
| 10595 | while (i > 0) { |
| 10596 | --i; |
| 10597 | |
| 10598 | if (expanded_args) |
| 10599 | type = TREE_VEC_ELT (expanded_args, i); |
| 10600 | else if (!type) |
| 10601 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); |
| 10602 | |
| 10603 | if (type == error_mark_node) |
| 10604 | return error_mark_node; |
| 10605 | if (VOID_TYPE_P (type)) |
| 10606 | { |
| 10607 | if (complain & tf_error) |
| 10608 | { |
| 10609 | error ("invalid parameter type %qT", type); |
| 10610 | if (in_decl) |
| 10611 | error ("in declaration %q+D", in_decl); |
| 10612 | } |
| 10613 | return error_mark_node; |
| 10614 | } |
| 10615 | |
| 10616 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
| 10617 | top-level qualifiers as required. */ |
| 10618 | type = cv_unqualified (type_decays_to (type)); |
| 10619 | |
| 10620 | /* We do not substitute into default arguments here. The standard |
| 10621 | mandates that they be instantiated only when needed, which is |
| 10622 | done in build_over_call. */ |
| 10623 | default_arg = TREE_PURPOSE (arg_types); |
| 10624 | |
| 10625 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) |
| 10626 | { |
| 10627 | /* We've instantiated a template before its default arguments |
| 10628 | have been parsed. This can happen for a nested template |
| 10629 | class, and is not an error unless we require the default |
| 10630 | argument in a call of this function. */ |
| 10631 | remaining_arg_types = |
| 10632 | tree_cons (default_arg, type, remaining_arg_types); |
| 10633 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), |
| 10634 | remaining_arg_types); |
| 10635 | } |
| 10636 | else |
| 10637 | remaining_arg_types = |
| 10638 | hash_tree_cons (default_arg, type, remaining_arg_types); |
| 10639 | } |
| 10640 | |
| 10641 | return remaining_arg_types; |
| 10642 | } |
| 10643 | |
| 10644 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
| 10645 | *not* handle the exception-specification for FNTYPE, because the |
| 10646 | initial substitution of explicitly provided template parameters |
| 10647 | during argument deduction forbids substitution into the |
| 10648 | exception-specification: |
| 10649 | |
| 10650 | [temp.deduct] |
| 10651 | |
| 10652 | All references in the function type of the function template to the |
| 10653 | corresponding template parameters are replaced by the specified tem- |
| 10654 | plate argument values. If a substitution in a template parameter or |
| 10655 | in the function type of the function template results in an invalid |
| 10656 | type, type deduction fails. [Note: The equivalent substitution in |
| 10657 | exception specifications is done only when the function is instanti- |
| 10658 | ated, at which point a program is ill-formed if the substitution |
| 10659 | results in an invalid type.] */ |
| 10660 | |
| 10661 | static tree |
| 10662 | tsubst_function_type (tree t, |
| 10663 | tree args, |
| 10664 | tsubst_flags_t complain, |
| 10665 | tree in_decl) |
| 10666 | { |
| 10667 | tree return_type; |
| 10668 | tree arg_types; |
| 10669 | tree fntype; |
| 10670 | |
| 10671 | /* The TYPE_CONTEXT is not used for function/method types. */ |
| 10672 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
| 10673 | |
| 10674 | /* Substitute the return type. */ |
| 10675 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 10676 | if (return_type == error_mark_node) |
| 10677 | return error_mark_node; |
| 10678 | /* The standard does not presently indicate that creation of a |
| 10679 | function type with an invalid return type is a deduction failure. |
| 10680 | However, that is clearly analogous to creating an array of "void" |
| 10681 | or a reference to a reference. This is core issue #486. */ |
| 10682 | if (TREE_CODE (return_type) == ARRAY_TYPE |
| 10683 | || TREE_CODE (return_type) == FUNCTION_TYPE) |
| 10684 | { |
| 10685 | if (complain & tf_error) |
| 10686 | { |
| 10687 | if (TREE_CODE (return_type) == ARRAY_TYPE) |
| 10688 | error ("function returning an array"); |
| 10689 | else |
| 10690 | error ("function returning a function"); |
| 10691 | } |
| 10692 | return error_mark_node; |
| 10693 | } |
| 10694 | |
| 10695 | /* Substitute the argument types. */ |
| 10696 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE, |
| 10697 | complain, in_decl); |
| 10698 | if (arg_types == error_mark_node) |
| 10699 | return error_mark_node; |
| 10700 | |
| 10701 | /* Construct a new type node and return it. */ |
| 10702 | if (TREE_CODE (t) == FUNCTION_TYPE) |
| 10703 | { |
| 10704 | fntype = build_function_type (return_type, arg_types); |
| 10705 | fntype = apply_memfn_quals (fntype, type_memfn_quals (t)); |
| 10706 | } |
| 10707 | else |
| 10708 | { |
| 10709 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); |
| 10710 | if (! MAYBE_CLASS_TYPE_P (r)) |
| 10711 | { |
| 10712 | /* [temp.deduct] |
| 10713 | |
| 10714 | Type deduction may fail for any of the following |
| 10715 | reasons: |
| 10716 | |
| 10717 | -- Attempting to create "pointer to member of T" when T |
| 10718 | is not a class type. */ |
| 10719 | if (complain & tf_error) |
| 10720 | error ("creating pointer to member function of non-class type %qT", |
| 10721 | r); |
| 10722 | return error_mark_node; |
| 10723 | } |
| 10724 | |
| 10725 | fntype = build_method_type_directly (r, return_type, |
| 10726 | TREE_CHAIN (arg_types)); |
| 10727 | } |
| 10728 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
| 10729 | |
| 10730 | return fntype; |
| 10731 | } |
| 10732 | |
| 10733 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
| 10734 | ARGS into that specification, and return the substituted |
| 10735 | specification. If there is no specification, return NULL_TREE. */ |
| 10736 | |
| 10737 | static tree |
| 10738 | tsubst_exception_specification (tree fntype, |
| 10739 | tree args, |
| 10740 | tsubst_flags_t complain, |
| 10741 | tree in_decl, |
| 10742 | bool defer_ok) |
| 10743 | { |
| 10744 | tree specs; |
| 10745 | tree new_specs; |
| 10746 | |
| 10747 | specs = TYPE_RAISES_EXCEPTIONS (fntype); |
| 10748 | new_specs = NULL_TREE; |
| 10749 | if (specs && TREE_PURPOSE (specs)) |
| 10750 | { |
| 10751 | /* A noexcept-specifier. */ |
| 10752 | tree expr = TREE_PURPOSE (specs); |
| 10753 | if (TREE_CODE (expr) == INTEGER_CST) |
| 10754 | new_specs = expr; |
| 10755 | else if (defer_ok) |
| 10756 | { |
| 10757 | /* Defer instantiation of noexcept-specifiers to avoid |
| 10758 | excessive instantiations (c++/49107). */ |
| 10759 | new_specs = make_node (DEFERRED_NOEXCEPT); |
| 10760 | if (DEFERRED_NOEXCEPT_SPEC_P (specs)) |
| 10761 | { |
| 10762 | /* We already partially instantiated this member template, |
| 10763 | so combine the new args with the old. */ |
| 10764 | DEFERRED_NOEXCEPT_PATTERN (new_specs) |
| 10765 | = DEFERRED_NOEXCEPT_PATTERN (expr); |
| 10766 | DEFERRED_NOEXCEPT_ARGS (new_specs) |
| 10767 | = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args); |
| 10768 | } |
| 10769 | else |
| 10770 | { |
| 10771 | DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr; |
| 10772 | DEFERRED_NOEXCEPT_ARGS (new_specs) = args; |
| 10773 | } |
| 10774 | } |
| 10775 | else |
| 10776 | new_specs = tsubst_copy_and_build |
| 10777 | (expr, args, complain, in_decl, /*function_p=*/false, |
| 10778 | /*integral_constant_expression_p=*/true); |
| 10779 | new_specs = build_noexcept_spec (new_specs, complain); |
| 10780 | } |
| 10781 | else if (specs) |
| 10782 | { |
| 10783 | if (! TREE_VALUE (specs)) |
| 10784 | new_specs = specs; |
| 10785 | else |
| 10786 | while (specs) |
| 10787 | { |
| 10788 | tree spec; |
| 10789 | int i, len = 1; |
| 10790 | tree expanded_specs = NULL_TREE; |
| 10791 | |
| 10792 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) |
| 10793 | { |
| 10794 | /* Expand the pack expansion type. */ |
| 10795 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), |
| 10796 | args, complain, |
| 10797 | in_decl); |
| 10798 | |
| 10799 | if (expanded_specs == error_mark_node) |
| 10800 | return error_mark_node; |
| 10801 | else if (TREE_CODE (expanded_specs) == TREE_VEC) |
| 10802 | len = TREE_VEC_LENGTH (expanded_specs); |
| 10803 | else |
| 10804 | { |
| 10805 | /* We're substituting into a member template, so |
| 10806 | we got a TYPE_PACK_EXPANSION back. Add that |
| 10807 | expansion and move on. */ |
| 10808 | gcc_assert (TREE_CODE (expanded_specs) |
| 10809 | == TYPE_PACK_EXPANSION); |
| 10810 | new_specs = add_exception_specifier (new_specs, |
| 10811 | expanded_specs, |
| 10812 | complain); |
| 10813 | specs = TREE_CHAIN (specs); |
| 10814 | continue; |
| 10815 | } |
| 10816 | } |
| 10817 | |
| 10818 | for (i = 0; i < len; ++i) |
| 10819 | { |
| 10820 | if (expanded_specs) |
| 10821 | spec = TREE_VEC_ELT (expanded_specs, i); |
| 10822 | else |
| 10823 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); |
| 10824 | if (spec == error_mark_node) |
| 10825 | return spec; |
| 10826 | new_specs = add_exception_specifier (new_specs, spec, |
| 10827 | complain); |
| 10828 | } |
| 10829 | |
| 10830 | specs = TREE_CHAIN (specs); |
| 10831 | } |
| 10832 | } |
| 10833 | return new_specs; |
| 10834 | } |
| 10835 | |
| 10836 | /* Take the tree structure T and replace template parameters used |
| 10837 | therein with the argument vector ARGS. IN_DECL is an associated |
| 10838 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. |
| 10839 | Issue error and warning messages under control of COMPLAIN. Note |
| 10840 | that we must be relatively non-tolerant of extensions here, in |
| 10841 | order to preserve conformance; if we allow substitutions that |
| 10842 | should not be allowed, we may allow argument deductions that should |
| 10843 | not succeed, and therefore report ambiguous overload situations |
| 10844 | where there are none. In theory, we could allow the substitution, |
| 10845 | but indicate that it should have failed, and allow our caller to |
| 10846 | make sure that the right thing happens, but we don't try to do this |
| 10847 | yet. |
| 10848 | |
| 10849 | This function is used for dealing with types, decls and the like; |
| 10850 | for expressions, use tsubst_expr or tsubst_copy. */ |
| 10851 | |
| 10852 | tree |
| 10853 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
| 10854 | { |
| 10855 | enum tree_code code; |
| 10856 | tree type, r = NULL_TREE; |
| 10857 | |
| 10858 | if (t == NULL_TREE || t == error_mark_node |
| 10859 | || t == integer_type_node |
| 10860 | || t == void_type_node |
| 10861 | || t == char_type_node |
| 10862 | || t == unknown_type_node |
| 10863 | || TREE_CODE (t) == NAMESPACE_DECL |
| 10864 | || TREE_CODE (t) == TRANSLATION_UNIT_DECL) |
| 10865 | return t; |
| 10866 | |
| 10867 | if (DECL_P (t)) |
| 10868 | return tsubst_decl (t, args, complain); |
| 10869 | |
| 10870 | if (args == NULL_TREE) |
| 10871 | return t; |
| 10872 | |
| 10873 | code = TREE_CODE (t); |
| 10874 | |
| 10875 | if (code == IDENTIFIER_NODE) |
| 10876 | type = IDENTIFIER_TYPE_VALUE (t); |
| 10877 | else |
| 10878 | type = TREE_TYPE (t); |
| 10879 | |
| 10880 | gcc_assert (type != unknown_type_node); |
| 10881 | |
| 10882 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
| 10883 | such as attribute aligned. */ |
| 10884 | if (TYPE_P (t) |
| 10885 | && typedef_variant_p (t)) |
| 10886 | { |
| 10887 | tree decl = TYPE_NAME (t); |
| 10888 | |
| 10889 | if (TYPE_DECL_ALIAS_P (decl) |
| 10890 | && DECL_LANG_SPECIFIC (decl) |
| 10891 | && DECL_TEMPLATE_INFO (decl) |
| 10892 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) |
| 10893 | { |
| 10894 | /* DECL represents an alias template and we want to |
| 10895 | instantiate it. Let's substitute our arguments for the |
| 10896 | template parameters into the declaration and get the |
| 10897 | resulting type. */ |
| 10898 | r = tsubst (decl, args, complain, decl); |
| 10899 | } |
| 10900 | else if (DECL_CLASS_SCOPE_P (decl) |
| 10901 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
| 10902 | && uses_template_parms (DECL_CONTEXT (decl))) |
| 10903 | { |
| 10904 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); |
| 10905 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); |
| 10906 | r = retrieve_specialization (tmpl, gen_args, 0); |
| 10907 | } |
| 10908 | else if (DECL_FUNCTION_SCOPE_P (decl) |
| 10909 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
| 10910 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) |
| 10911 | r = retrieve_local_specialization (decl); |
| 10912 | else |
| 10913 | /* The typedef is from a non-template context. */ |
| 10914 | return t; |
| 10915 | |
| 10916 | if (r) |
| 10917 | { |
| 10918 | r = TREE_TYPE (r); |
| 10919 | r = cp_build_qualified_type_real |
| 10920 | (r, cp_type_quals (t) | cp_type_quals (r), |
| 10921 | complain | tf_ignore_bad_quals); |
| 10922 | return r; |
| 10923 | } |
| 10924 | else |
| 10925 | { |
| 10926 | /* We don't have an instantiation yet, so drop the typedef. */ |
| 10927 | int quals = cp_type_quals (t); |
| 10928 | t = DECL_ORIGINAL_TYPE (decl); |
| 10929 | t = cp_build_qualified_type_real (t, quals, |
| 10930 | complain | tf_ignore_bad_quals); |
| 10931 | } |
| 10932 | } |
| 10933 | |
| 10934 | if (type |
| 10935 | && code != TYPENAME_TYPE |
| 10936 | && code != TEMPLATE_TYPE_PARM |
| 10937 | && code != IDENTIFIER_NODE |
| 10938 | && code != FUNCTION_TYPE |
| 10939 | && code != METHOD_TYPE) |
| 10940 | type = tsubst (type, args, complain, in_decl); |
| 10941 | if (type == error_mark_node) |
| 10942 | return error_mark_node; |
| 10943 | |
| 10944 | switch (code) |
| 10945 | { |
| 10946 | case RECORD_TYPE: |
| 10947 | case UNION_TYPE: |
| 10948 | case ENUMERAL_TYPE: |
| 10949 | return tsubst_aggr_type (t, args, complain, in_decl, |
| 10950 | /*entering_scope=*/0); |
| 10951 | |
| 10952 | case ERROR_MARK: |
| 10953 | case IDENTIFIER_NODE: |
| 10954 | case VOID_TYPE: |
| 10955 | case REAL_TYPE: |
| 10956 | case COMPLEX_TYPE: |
| 10957 | case VECTOR_TYPE: |
| 10958 | case BOOLEAN_TYPE: |
| 10959 | case NULLPTR_TYPE: |
| 10960 | case LANG_TYPE: |
| 10961 | return t; |
| 10962 | |
| 10963 | case INTEGER_TYPE: |
| 10964 | if (t == integer_type_node) |
| 10965 | return t; |
| 10966 | |
| 10967 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST |
| 10968 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) |
| 10969 | return t; |
| 10970 | |
| 10971 | { |
| 10972 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
| 10973 | |
| 10974 | max = tsubst_expr (omax, args, complain, in_decl, |
| 10975 | /*integral_constant_expression_p=*/false); |
| 10976 | |
| 10977 | /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if |
| 10978 | needed. */ |
| 10979 | if (TREE_CODE (max) == NOP_EXPR |
| 10980 | && TREE_SIDE_EFFECTS (omax) |
| 10981 | && !TREE_TYPE (max)) |
| 10982 | TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); |
| 10983 | |
| 10984 | /* If we're in a partial instantiation, preserve the magic NOP_EXPR |
| 10985 | with TREE_SIDE_EFFECTS that indicates this is not an integral |
| 10986 | constant expression. */ |
| 10987 | if (processing_template_decl |
| 10988 | && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) |
| 10989 | { |
| 10990 | gcc_assert (TREE_CODE (max) == NOP_EXPR); |
| 10991 | TREE_SIDE_EFFECTS (max) = 1; |
| 10992 | } |
| 10993 | |
| 10994 | return compute_array_index_type (NULL_TREE, max, complain); |
| 10995 | } |
| 10996 | |
| 10997 | case TEMPLATE_TYPE_PARM: |
| 10998 | case TEMPLATE_TEMPLATE_PARM: |
| 10999 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 11000 | case TEMPLATE_PARM_INDEX: |
| 11001 | { |
| 11002 | int idx; |
| 11003 | int level; |
| 11004 | int levels; |
| 11005 | tree arg = NULL_TREE; |
| 11006 | |
| 11007 | r = NULL_TREE; |
| 11008 | |
| 11009 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
| 11010 | template_parm_level_and_index (t, &level, &idx); |
| 11011 | |
| 11012 | levels = TMPL_ARGS_DEPTH (args); |
| 11013 | if (level <= levels) |
| 11014 | { |
| 11015 | arg = TMPL_ARG (args, level, idx); |
| 11016 | |
| 11017 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
| 11018 | /* See through ARGUMENT_PACK_SELECT arguments. */ |
| 11019 | arg = ARGUMENT_PACK_SELECT_ARG (arg); |
| 11020 | } |
| 11021 | |
| 11022 | if (arg == error_mark_node) |
| 11023 | return error_mark_node; |
| 11024 | else if (arg != NULL_TREE) |
| 11025 | { |
| 11026 | if (ARGUMENT_PACK_P (arg)) |
| 11027 | /* If ARG is an argument pack, we don't actually want to |
| 11028 | perform a substitution here, because substitutions |
| 11029 | for argument packs are only done |
| 11030 | element-by-element. We can get to this point when |
| 11031 | substituting the type of a non-type template |
| 11032 | parameter pack, when that type actually contains |
| 11033 | template parameter packs from an outer template, e.g., |
| 11034 | |
| 11035 | template<typename... Types> struct A { |
| 11036 | template<Types... Values> struct B { }; |
| 11037 | }; */ |
| 11038 | return t; |
| 11039 | |
| 11040 | if (code == TEMPLATE_TYPE_PARM) |
| 11041 | { |
| 11042 | int quals; |
| 11043 | gcc_assert (TYPE_P (arg)); |
| 11044 | |
| 11045 | quals = cp_type_quals (arg) | cp_type_quals (t); |
| 11046 | |
| 11047 | return cp_build_qualified_type_real |
| 11048 | (arg, quals, complain | tf_ignore_bad_quals); |
| 11049 | } |
| 11050 | else if (code == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 11051 | { |
| 11052 | /* We are processing a type constructed from a |
| 11053 | template template parameter. */ |
| 11054 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
| 11055 | args, complain, in_decl); |
| 11056 | if (argvec == error_mark_node) |
| 11057 | return error_mark_node; |
| 11058 | |
| 11059 | gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
| 11060 | || TREE_CODE (arg) == TEMPLATE_DECL |
| 11061 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); |
| 11062 | |
| 11063 | if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) |
| 11064 | /* Consider this code: |
| 11065 | |
| 11066 | template <template <class> class Template> |
| 11067 | struct Internal { |
| 11068 | template <class Arg> using Bind = Template<Arg>; |
| 11069 | }; |
| 11070 | |
| 11071 | template <template <class> class Template, class Arg> |
| 11072 | using Instantiate = Template<Arg>; //#0 |
| 11073 | |
| 11074 | template <template <class> class Template, |
| 11075 | class Argument> |
| 11076 | using Bind = |
| 11077 | Instantiate<Internal<Template>::template Bind, |
| 11078 | Argument>; //#1 |
| 11079 | |
| 11080 | When #1 is parsed, the |
| 11081 | BOUND_TEMPLATE_TEMPLATE_PARM representing the |
| 11082 | parameter `Template' in #0 matches the |
| 11083 | UNBOUND_CLASS_TEMPLATE representing the argument |
| 11084 | `Internal<Template>::template Bind'; We then want |
| 11085 | to assemble the type `Bind<Argument>' that can't |
| 11086 | be fully created right now, because |
| 11087 | `Internal<Template>' not being complete, the Bind |
| 11088 | template cannot be looked up in that context. So |
| 11089 | we need to "store" `Bind<Argument>' for later |
| 11090 | when the context of Bind becomes complete. Let's |
| 11091 | store that in a TYPENAME_TYPE. */ |
| 11092 | return make_typename_type (TYPE_CONTEXT (arg), |
| 11093 | build_nt (TEMPLATE_ID_EXPR, |
| 11094 | TYPE_IDENTIFIER (arg), |
| 11095 | argvec), |
| 11096 | typename_type, |
| 11097 | complain); |
| 11098 | |
| 11099 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
| 11100 | are resolving nested-types in the signature of a |
| 11101 | member function templates. Otherwise ARG is a |
| 11102 | TEMPLATE_DECL and is the real template to be |
| 11103 | instantiated. */ |
| 11104 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
| 11105 | arg = TYPE_NAME (arg); |
| 11106 | |
| 11107 | r = lookup_template_class (arg, |
| 11108 | argvec, in_decl, |
| 11109 | DECL_CONTEXT (arg), |
| 11110 | /*entering_scope=*/0, |
| 11111 | complain); |
| 11112 | return cp_build_qualified_type_real |
| 11113 | (r, cp_type_quals (t), complain); |
| 11114 | } |
| 11115 | else |
| 11116 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ |
| 11117 | return convert_from_reference (unshare_expr (arg)); |
| 11118 | } |
| 11119 | |
| 11120 | if (level == 1) |
| 11121 | /* This can happen during the attempted tsubst'ing in |
| 11122 | unify. This means that we don't yet have any information |
| 11123 | about the template parameter in question. */ |
| 11124 | return t; |
| 11125 | |
| 11126 | /* If we get here, we must have been looking at a parm for a |
| 11127 | more deeply nested template. Make a new version of this |
| 11128 | template parameter, but with a lower level. */ |
| 11129 | switch (code) |
| 11130 | { |
| 11131 | case TEMPLATE_TYPE_PARM: |
| 11132 | case TEMPLATE_TEMPLATE_PARM: |
| 11133 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 11134 | if (cp_type_quals (t)) |
| 11135 | { |
| 11136 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
| 11137 | r = cp_build_qualified_type_real |
| 11138 | (r, cp_type_quals (t), |
| 11139 | complain | (code == TEMPLATE_TYPE_PARM |
| 11140 | ? tf_ignore_bad_quals : 0)); |
| 11141 | } |
| 11142 | else |
| 11143 | { |
| 11144 | r = copy_type (t); |
| 11145 | TEMPLATE_TYPE_PARM_INDEX (r) |
| 11146 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), |
| 11147 | r, levels, args, complain); |
| 11148 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
| 11149 | TYPE_MAIN_VARIANT (r) = r; |
| 11150 | TYPE_POINTER_TO (r) = NULL_TREE; |
| 11151 | TYPE_REFERENCE_TO (r) = NULL_TREE; |
| 11152 | |
| 11153 | if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) |
| 11154 | /* We have reduced the level of the template |
| 11155 | template parameter, but not the levels of its |
| 11156 | template parameters, so canonical_type_parameter |
| 11157 | will not be able to find the canonical template |
| 11158 | template parameter for this level. Thus, we |
| 11159 | require structural equality checking to compare |
| 11160 | TEMPLATE_TEMPLATE_PARMs. */ |
| 11161 | SET_TYPE_STRUCTURAL_EQUALITY (r); |
| 11162 | else if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
| 11163 | SET_TYPE_STRUCTURAL_EQUALITY (r); |
| 11164 | else |
| 11165 | TYPE_CANONICAL (r) = canonical_type_parameter (r); |
| 11166 | |
| 11167 | if (code == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 11168 | { |
| 11169 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
| 11170 | complain, in_decl); |
| 11171 | if (argvec == error_mark_node) |
| 11172 | return error_mark_node; |
| 11173 | |
| 11174 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
| 11175 | = build_template_info (TYPE_TI_TEMPLATE (t), argvec); |
| 11176 | } |
| 11177 | } |
| 11178 | break; |
| 11179 | |
| 11180 | case TEMPLATE_PARM_INDEX: |
| 11181 | r = reduce_template_parm_level (t, type, levels, args, complain); |
| 11182 | break; |
| 11183 | |
| 11184 | default: |
| 11185 | gcc_unreachable (); |
| 11186 | } |
| 11187 | |
| 11188 | return r; |
| 11189 | } |
| 11190 | |
| 11191 | case TREE_LIST: |
| 11192 | { |
| 11193 | tree purpose, value, chain; |
| 11194 | |
| 11195 | if (t == void_list_node) |
| 11196 | return t; |
| 11197 | |
| 11198 | purpose = TREE_PURPOSE (t); |
| 11199 | if (purpose) |
| 11200 | { |
| 11201 | purpose = tsubst (purpose, args, complain, in_decl); |
| 11202 | if (purpose == error_mark_node) |
| 11203 | return error_mark_node; |
| 11204 | } |
| 11205 | value = TREE_VALUE (t); |
| 11206 | if (value) |
| 11207 | { |
| 11208 | value = tsubst (value, args, complain, in_decl); |
| 11209 | if (value == error_mark_node) |
| 11210 | return error_mark_node; |
| 11211 | } |
| 11212 | chain = TREE_CHAIN (t); |
| 11213 | if (chain && chain != void_type_node) |
| 11214 | { |
| 11215 | chain = tsubst (chain, args, complain, in_decl); |
| 11216 | if (chain == error_mark_node) |
| 11217 | return error_mark_node; |
| 11218 | } |
| 11219 | if (purpose == TREE_PURPOSE (t) |
| 11220 | && value == TREE_VALUE (t) |
| 11221 | && chain == TREE_CHAIN (t)) |
| 11222 | return t; |
| 11223 | return hash_tree_cons (purpose, value, chain); |
| 11224 | } |
| 11225 | |
| 11226 | case TREE_BINFO: |
| 11227 | /* We should never be tsubsting a binfo. */ |
| 11228 | gcc_unreachable (); |
| 11229 | |
| 11230 | case TREE_VEC: |
| 11231 | /* A vector of template arguments. */ |
| 11232 | gcc_assert (!type); |
| 11233 | return tsubst_template_args (t, args, complain, in_decl); |
| 11234 | |
| 11235 | case POINTER_TYPE: |
| 11236 | case REFERENCE_TYPE: |
| 11237 | { |
| 11238 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
| 11239 | return t; |
| 11240 | |
| 11241 | /* [temp.deduct] |
| 11242 | |
| 11243 | Type deduction may fail for any of the following |
| 11244 | reasons: |
| 11245 | |
| 11246 | -- Attempting to create a pointer to reference type. |
| 11247 | -- Attempting to create a reference to a reference type or |
| 11248 | a reference to void. |
| 11249 | |
| 11250 | Core issue 106 says that creating a reference to a reference |
| 11251 | during instantiation is no longer a cause for failure. We |
| 11252 | only enforce this check in strict C++98 mode. */ |
| 11253 | if ((TREE_CODE (type) == REFERENCE_TYPE |
| 11254 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
| 11255 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) |
| 11256 | { |
| 11257 | static location_t last_loc; |
| 11258 | |
| 11259 | /* We keep track of the last time we issued this error |
| 11260 | message to avoid spewing a ton of messages during a |
| 11261 | single bad template instantiation. */ |
| 11262 | if (complain & tf_error |
| 11263 | && last_loc != input_location) |
| 11264 | { |
| 11265 | if (TREE_CODE (type) == VOID_TYPE) |
| 11266 | error ("forming reference to void"); |
| 11267 | else if (code == POINTER_TYPE) |
| 11268 | error ("forming pointer to reference type %qT", type); |
| 11269 | else |
| 11270 | error ("forming reference to reference type %qT", type); |
| 11271 | last_loc = input_location; |
| 11272 | } |
| 11273 | |
| 11274 | return error_mark_node; |
| 11275 | } |
| 11276 | else if (code == POINTER_TYPE) |
| 11277 | { |
| 11278 | r = build_pointer_type (type); |
| 11279 | if (TREE_CODE (type) == METHOD_TYPE) |
| 11280 | r = build_ptrmemfunc_type (r); |
| 11281 | } |
| 11282 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
| 11283 | /* In C++0x, during template argument substitution, when there is an |
| 11284 | attempt to create a reference to a reference type, reference |
| 11285 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: |
| 11286 | |
| 11287 | "If a template-argument for a template-parameter T names a type |
| 11288 | that is a reference to a type A, an attempt to create the type |
| 11289 | 'lvalue reference to cv T' creates the type 'lvalue reference to |
| 11290 | A,' while an attempt to create the type type rvalue reference to |
| 11291 | cv T' creates the type T" |
| 11292 | */ |
| 11293 | r = cp_build_reference_type |
| 11294 | (TREE_TYPE (type), |
| 11295 | TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); |
| 11296 | else |
| 11297 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
| 11298 | r = cp_build_qualified_type_real (r, cp_type_quals (t), complain); |
| 11299 | |
| 11300 | if (r != error_mark_node) |
| 11301 | /* Will this ever be needed for TYPE_..._TO values? */ |
| 11302 | layout_type (r); |
| 11303 | |
| 11304 | return r; |
| 11305 | } |
| 11306 | case OFFSET_TYPE: |
| 11307 | { |
| 11308 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
| 11309 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
| 11310 | { |
| 11311 | /* [temp.deduct] |
| 11312 | |
| 11313 | Type deduction may fail for any of the following |
| 11314 | reasons: |
| 11315 | |
| 11316 | -- Attempting to create "pointer to member of T" when T |
| 11317 | is not a class type. */ |
| 11318 | if (complain & tf_error) |
| 11319 | error ("creating pointer to member of non-class type %qT", r); |
| 11320 | return error_mark_node; |
| 11321 | } |
| 11322 | if (TREE_CODE (type) == REFERENCE_TYPE) |
| 11323 | { |
| 11324 | if (complain & tf_error) |
| 11325 | error ("creating pointer to member reference type %qT", type); |
| 11326 | return error_mark_node; |
| 11327 | } |
| 11328 | if (TREE_CODE (type) == VOID_TYPE) |
| 11329 | { |
| 11330 | if (complain & tf_error) |
| 11331 | error ("creating pointer to member of type void"); |
| 11332 | return error_mark_node; |
| 11333 | } |
| 11334 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
| 11335 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 11336 | { |
| 11337 | /* The type of the implicit object parameter gets its |
| 11338 | cv-qualifiers from the FUNCTION_TYPE. */ |
| 11339 | tree memptr; |
| 11340 | tree method_type = build_memfn_type (type, r, type_memfn_quals (type)); |
| 11341 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
| 11342 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), |
| 11343 | complain); |
| 11344 | } |
| 11345 | else |
| 11346 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
| 11347 | cp_type_quals (t), |
| 11348 | complain); |
| 11349 | } |
| 11350 | case FUNCTION_TYPE: |
| 11351 | case METHOD_TYPE: |
| 11352 | { |
| 11353 | tree fntype; |
| 11354 | tree specs; |
| 11355 | fntype = tsubst_function_type (t, args, complain, in_decl); |
| 11356 | if (fntype == error_mark_node) |
| 11357 | return error_mark_node; |
| 11358 | |
| 11359 | /* Substitute the exception specification. */ |
| 11360 | specs = tsubst_exception_specification (t, args, complain, |
| 11361 | in_decl, /*defer_ok*/true); |
| 11362 | if (specs == error_mark_node) |
| 11363 | return error_mark_node; |
| 11364 | if (specs) |
| 11365 | fntype = build_exception_variant (fntype, specs); |
| 11366 | return fntype; |
| 11367 | } |
| 11368 | case ARRAY_TYPE: |
| 11369 | { |
| 11370 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
| 11371 | if (domain == error_mark_node) |
| 11372 | return error_mark_node; |
| 11373 | |
| 11374 | /* As an optimization, we avoid regenerating the array type if |
| 11375 | it will obviously be the same as T. */ |
| 11376 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
| 11377 | return t; |
| 11378 | |
| 11379 | /* These checks should match the ones in grokdeclarator. |
| 11380 | |
| 11381 | [temp.deduct] |
| 11382 | |
| 11383 | The deduction may fail for any of the following reasons: |
| 11384 | |
| 11385 | -- Attempting to create an array with an element type that |
| 11386 | is void, a function type, or a reference type, or [DR337] |
| 11387 | an abstract class type. */ |
| 11388 | if (TREE_CODE (type) == VOID_TYPE |
| 11389 | || TREE_CODE (type) == FUNCTION_TYPE |
| 11390 | || TREE_CODE (type) == REFERENCE_TYPE) |
| 11391 | { |
| 11392 | if (complain & tf_error) |
| 11393 | error ("creating array of %qT", type); |
| 11394 | return error_mark_node; |
| 11395 | } |
| 11396 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
| 11397 | { |
| 11398 | if (complain & tf_error) |
| 11399 | error ("creating array of %qT, which is an abstract class type", |
| 11400 | type); |
| 11401 | return error_mark_node; |
| 11402 | } |
| 11403 | |
| 11404 | r = build_cplus_array_type (type, domain); |
| 11405 | |
| 11406 | if (TYPE_USER_ALIGN (t)) |
| 11407 | { |
| 11408 | TYPE_ALIGN (r) = TYPE_ALIGN (t); |
| 11409 | TYPE_USER_ALIGN (r) = 1; |
| 11410 | } |
| 11411 | |
| 11412 | return r; |
| 11413 | } |
| 11414 | |
| 11415 | case TYPENAME_TYPE: |
| 11416 | { |
| 11417 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
| 11418 | in_decl, /*entering_scope=*/1); |
| 11419 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, |
| 11420 | complain, in_decl); |
| 11421 | |
| 11422 | if (ctx == error_mark_node || f == error_mark_node) |
| 11423 | return error_mark_node; |
| 11424 | |
| 11425 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
| 11426 | { |
| 11427 | if (complain & tf_error) |
| 11428 | error ("%qT is not a class, struct, or union type", ctx); |
| 11429 | return error_mark_node; |
| 11430 | } |
| 11431 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) |
| 11432 | { |
| 11433 | /* Normally, make_typename_type does not require that the CTX |
| 11434 | have complete type in order to allow things like: |
| 11435 | |
| 11436 | template <class T> struct S { typename S<T>::X Y; }; |
| 11437 | |
| 11438 | But, such constructs have already been resolved by this |
| 11439 | point, so here CTX really should have complete type, unless |
| 11440 | it's a partial instantiation. */ |
| 11441 | ctx = complete_type (ctx); |
| 11442 | if (!COMPLETE_TYPE_P (ctx)) |
| 11443 | { |
| 11444 | if (complain & tf_error) |
| 11445 | cxx_incomplete_type_error (NULL_TREE, ctx); |
| 11446 | return error_mark_node; |
| 11447 | } |
| 11448 | } |
| 11449 | |
| 11450 | f = make_typename_type (ctx, f, typename_type, |
| 11451 | (complain & tf_error) | tf_keep_type_decl); |
| 11452 | if (f == error_mark_node) |
| 11453 | return f; |
| 11454 | if (TREE_CODE (f) == TYPE_DECL) |
| 11455 | { |
| 11456 | complain |= tf_ignore_bad_quals; |
| 11457 | f = TREE_TYPE (f); |
| 11458 | } |
| 11459 | |
| 11460 | if (TREE_CODE (f) != TYPENAME_TYPE) |
| 11461 | { |
| 11462 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) |
| 11463 | { |
| 11464 | if (complain & tf_error) |
| 11465 | error ("%qT resolves to %qT, which is not an enumeration type", |
| 11466 | t, f); |
| 11467 | else |
| 11468 | return error_mark_node; |
| 11469 | } |
| 11470 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) |
| 11471 | { |
| 11472 | if (complain & tf_error) |
| 11473 | error ("%qT resolves to %qT, which is is not a class type", |
| 11474 | t, f); |
| 11475 | else |
| 11476 | return error_mark_node; |
| 11477 | } |
| 11478 | } |
| 11479 | |
| 11480 | return cp_build_qualified_type_real |
| 11481 | (f, cp_type_quals (f) | cp_type_quals (t), complain); |
| 11482 | } |
| 11483 | |
| 11484 | case UNBOUND_CLASS_TEMPLATE: |
| 11485 | { |
| 11486 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
| 11487 | in_decl, /*entering_scope=*/1); |
| 11488 | tree name = TYPE_IDENTIFIER (t); |
| 11489 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
| 11490 | |
| 11491 | if (ctx == error_mark_node || name == error_mark_node) |
| 11492 | return error_mark_node; |
| 11493 | |
| 11494 | if (parm_list) |
| 11495 | parm_list = tsubst_template_parms (parm_list, args, complain); |
| 11496 | return make_unbound_class_template (ctx, name, parm_list, complain); |
| 11497 | } |
| 11498 | |
| 11499 | case TYPEOF_TYPE: |
| 11500 | { |
| 11501 | tree type; |
| 11502 | |
| 11503 | ++cp_unevaluated_operand; |
| 11504 | ++c_inhibit_evaluation_warnings; |
| 11505 | |
| 11506 | type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, |
| 11507 | complain, in_decl, |
| 11508 | /*integral_constant_expression_p=*/false); |
| 11509 | |
| 11510 | --cp_unevaluated_operand; |
| 11511 | --c_inhibit_evaluation_warnings; |
| 11512 | |
| 11513 | type = finish_typeof (type); |
| 11514 | return cp_build_qualified_type_real (type, |
| 11515 | cp_type_quals (t) |
| 11516 | | cp_type_quals (type), |
| 11517 | complain); |
| 11518 | } |
| 11519 | |
| 11520 | case DECLTYPE_TYPE: |
| 11521 | { |
| 11522 | tree type; |
| 11523 | |
| 11524 | ++cp_unevaluated_operand; |
| 11525 | ++c_inhibit_evaluation_warnings; |
| 11526 | |
| 11527 | type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, |
| 11528 | complain, in_decl, |
| 11529 | /*integral_constant_expression_p=*/false); |
| 11530 | |
| 11531 | --cp_unevaluated_operand; |
| 11532 | --c_inhibit_evaluation_warnings; |
| 11533 | |
| 11534 | if (DECLTYPE_FOR_LAMBDA_CAPTURE (t)) |
| 11535 | type = lambda_capture_field_type (type); |
| 11536 | else if (DECLTYPE_FOR_LAMBDA_PROXY (t)) |
| 11537 | type = lambda_proxy_type (type); |
| 11538 | else |
| 11539 | type = finish_decltype_type |
| 11540 | (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain); |
| 11541 | return cp_build_qualified_type_real (type, |
| 11542 | cp_type_quals (t) |
| 11543 | | cp_type_quals (type), |
| 11544 | complain); |
| 11545 | } |
| 11546 | |
| 11547 | case UNDERLYING_TYPE: |
| 11548 | { |
| 11549 | tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args, |
| 11550 | complain, in_decl); |
| 11551 | return finish_underlying_type (type); |
| 11552 | } |
| 11553 | |
| 11554 | case TYPE_ARGUMENT_PACK: |
| 11555 | case NONTYPE_ARGUMENT_PACK: |
| 11556 | { |
| 11557 | tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code); |
| 11558 | tree packed_out = |
| 11559 | tsubst_template_args (ARGUMENT_PACK_ARGS (t), |
| 11560 | args, |
| 11561 | complain, |
| 11562 | in_decl); |
| 11563 | SET_ARGUMENT_PACK_ARGS (r, packed_out); |
| 11564 | |
| 11565 | /* For template nontype argument packs, also substitute into |
| 11566 | the type. */ |
| 11567 | if (code == NONTYPE_ARGUMENT_PACK) |
| 11568 | TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 11569 | |
| 11570 | return r; |
| 11571 | } |
| 11572 | break; |
| 11573 | |
| 11574 | case INTEGER_CST: |
| 11575 | case REAL_CST: |
| 11576 | case STRING_CST: |
| 11577 | case PLUS_EXPR: |
| 11578 | case MINUS_EXPR: |
| 11579 | case NEGATE_EXPR: |
| 11580 | case NOP_EXPR: |
| 11581 | case INDIRECT_REF: |
| 11582 | case ADDR_EXPR: |
| 11583 | case CALL_EXPR: |
| 11584 | case ARRAY_REF: |
| 11585 | case SCOPE_REF: |
| 11586 | /* We should use one of the expression tsubsts for these codes. */ |
| 11587 | gcc_unreachable (); |
| 11588 | |
| 11589 | default: |
| 11590 | sorry ("use of %qs in template", tree_code_name [(int) code]); |
| 11591 | return error_mark_node; |
| 11592 | } |
| 11593 | } |
| 11594 | |
| 11595 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
| 11596 | type of the expression on the left-hand side of the "." or "->" |
| 11597 | operator. */ |
| 11598 | |
| 11599 | static tree |
| 11600 | tsubst_baselink (tree baselink, tree object_type, |
| 11601 | tree args, tsubst_flags_t complain, tree in_decl) |
| 11602 | { |
| 11603 | tree name; |
| 11604 | tree qualifying_scope; |
| 11605 | tree fns; |
| 11606 | tree optype; |
| 11607 | tree template_args = 0; |
| 11608 | bool template_id_p = false; |
| 11609 | bool qualified = BASELINK_QUALIFIED_P (baselink); |
| 11610 | |
| 11611 | /* A baselink indicates a function from a base class. Both the |
| 11612 | BASELINK_ACCESS_BINFO and the base class referenced may |
| 11613 | indicate bases of the template class, rather than the |
| 11614 | instantiated class. In addition, lookups that were not |
| 11615 | ambiguous before may be ambiguous now. Therefore, we perform |
| 11616 | the lookup again. */ |
| 11617 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
| 11618 | qualifying_scope = tsubst (qualifying_scope, args, |
| 11619 | complain, in_decl); |
| 11620 | fns = BASELINK_FUNCTIONS (baselink); |
| 11621 | optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl); |
| 11622 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
| 11623 | { |
| 11624 | template_id_p = true; |
| 11625 | template_args = TREE_OPERAND (fns, 1); |
| 11626 | fns = TREE_OPERAND (fns, 0); |
| 11627 | if (template_args) |
| 11628 | template_args = tsubst_template_args (template_args, args, |
| 11629 | complain, in_decl); |
| 11630 | } |
| 11631 | name = DECL_NAME (get_first_fn (fns)); |
| 11632 | if (IDENTIFIER_TYPENAME_P (name)) |
| 11633 | name = mangle_conv_op_name_for_type (optype); |
| 11634 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); |
| 11635 | if (!baselink) |
| 11636 | return error_mark_node; |
| 11637 | |
| 11638 | /* If lookup found a single function, mark it as used at this |
| 11639 | point. (If it lookup found multiple functions the one selected |
| 11640 | later by overload resolution will be marked as used at that |
| 11641 | point.) */ |
| 11642 | if (BASELINK_P (baselink)) |
| 11643 | fns = BASELINK_FUNCTIONS (baselink); |
| 11644 | if (!template_id_p && !really_overloaded_fn (fns)) |
| 11645 | mark_used (OVL_CURRENT (fns)); |
| 11646 | |
| 11647 | /* Add back the template arguments, if present. */ |
| 11648 | if (BASELINK_P (baselink) && template_id_p) |
| 11649 | BASELINK_FUNCTIONS (baselink) |
| 11650 | = build_nt (TEMPLATE_ID_EXPR, |
| 11651 | BASELINK_FUNCTIONS (baselink), |
| 11652 | template_args); |
| 11653 | /* Update the conversion operator type. */ |
| 11654 | BASELINK_OPTYPE (baselink) = optype; |
| 11655 | |
| 11656 | if (!object_type) |
| 11657 | object_type = current_class_type; |
| 11658 | |
| 11659 | if (qualified) |
| 11660 | baselink = adjust_result_of_qualified_name_lookup (baselink, |
| 11661 | qualifying_scope, |
| 11662 | object_type); |
| 11663 | return baselink; |
| 11664 | } |
| 11665 | |
| 11666 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is |
| 11667 | true if the qualified-id will be a postfix-expression in-and-of |
| 11668 | itself; false if more of the postfix-expression follows the |
| 11669 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand |
| 11670 | of "&". */ |
| 11671 | |
| 11672 | static tree |
| 11673 | tsubst_qualified_id (tree qualified_id, tree args, |
| 11674 | tsubst_flags_t complain, tree in_decl, |
| 11675 | bool done, bool address_p) |
| 11676 | { |
| 11677 | tree expr; |
| 11678 | tree scope; |
| 11679 | tree name; |
| 11680 | bool is_template; |
| 11681 | tree template_args; |
| 11682 | |
| 11683 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
| 11684 | |
| 11685 | /* Figure out what name to look up. */ |
| 11686 | name = TREE_OPERAND (qualified_id, 1); |
| 11687 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
| 11688 | { |
| 11689 | is_template = true; |
| 11690 | template_args = TREE_OPERAND (name, 1); |
| 11691 | if (template_args) |
| 11692 | template_args = tsubst_template_args (template_args, args, |
| 11693 | complain, in_decl); |
| 11694 | name = TREE_OPERAND (name, 0); |
| 11695 | } |
| 11696 | else |
| 11697 | { |
| 11698 | is_template = false; |
| 11699 | template_args = NULL_TREE; |
| 11700 | } |
| 11701 | |
| 11702 | /* Substitute into the qualifying scope. When there are no ARGS, we |
| 11703 | are just trying to simplify a non-dependent expression. In that |
| 11704 | case the qualifying scope may be dependent, and, in any case, |
| 11705 | substituting will not help. */ |
| 11706 | scope = TREE_OPERAND (qualified_id, 0); |
| 11707 | if (args) |
| 11708 | { |
| 11709 | scope = tsubst (scope, args, complain, in_decl); |
| 11710 | expr = tsubst_copy (name, args, complain, in_decl); |
| 11711 | } |
| 11712 | else |
| 11713 | expr = name; |
| 11714 | |
| 11715 | if (dependent_scope_p (scope)) |
| 11716 | { |
| 11717 | if (is_template) |
| 11718 | expr = build_min_nt (TEMPLATE_ID_EXPR, expr, template_args); |
| 11719 | return build_qualified_name (NULL_TREE, scope, expr, |
| 11720 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
| 11721 | } |
| 11722 | |
| 11723 | if (!BASELINK_P (name) && !DECL_P (expr)) |
| 11724 | { |
| 11725 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
| 11726 | { |
| 11727 | /* A BIT_NOT_EXPR is used to represent a destructor. */ |
| 11728 | if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) |
| 11729 | { |
| 11730 | error ("qualifying type %qT does not match destructor name ~%qT", |
| 11731 | scope, TREE_OPERAND (expr, 0)); |
| 11732 | expr = error_mark_node; |
| 11733 | } |
| 11734 | else |
| 11735 | expr = lookup_qualified_name (scope, complete_dtor_identifier, |
| 11736 | /*is_type_p=*/0, false); |
| 11737 | } |
| 11738 | else |
| 11739 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); |
| 11740 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
| 11741 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) |
| 11742 | { |
| 11743 | if (complain & tf_error) |
| 11744 | { |
| 11745 | error ("dependent-name %qE is parsed as a non-type, but " |
| 11746 | "instantiation yields a type", qualified_id); |
| 11747 | inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); |
| 11748 | } |
| 11749 | return error_mark_node; |
| 11750 | } |
| 11751 | } |
| 11752 | |
| 11753 | if (DECL_P (expr)) |
| 11754 | { |
| 11755 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, |
| 11756 | scope); |
| 11757 | /* Remember that there was a reference to this entity. */ |
| 11758 | mark_used (expr); |
| 11759 | } |
| 11760 | |
| 11761 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) |
| 11762 | { |
| 11763 | if (complain & tf_error) |
| 11764 | qualified_name_lookup_error (scope, |
| 11765 | TREE_OPERAND (qualified_id, 1), |
| 11766 | expr, input_location); |
| 11767 | return error_mark_node; |
| 11768 | } |
| 11769 | |
| 11770 | if (is_template) |
| 11771 | expr = lookup_template_function (expr, template_args); |
| 11772 | |
| 11773 | if (expr == error_mark_node && complain & tf_error) |
| 11774 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
| 11775 | expr, input_location); |
| 11776 | else if (TYPE_P (scope)) |
| 11777 | { |
| 11778 | expr = (adjust_result_of_qualified_name_lookup |
| 11779 | (expr, scope, current_class_type)); |
| 11780 | expr = (finish_qualified_id_expr |
| 11781 | (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id), |
| 11782 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), |
| 11783 | /*template_arg_p=*/false)); |
| 11784 | } |
| 11785 | |
| 11786 | /* Expressions do not generally have reference type. */ |
| 11787 | if (TREE_CODE (expr) != SCOPE_REF |
| 11788 | /* However, if we're about to form a pointer-to-member, we just |
| 11789 | want the referenced member referenced. */ |
| 11790 | && TREE_CODE (expr) != OFFSET_REF) |
| 11791 | expr = convert_from_reference (expr); |
| 11792 | |
| 11793 | return expr; |
| 11794 | } |
| 11795 | |
| 11796 | /* Like tsubst, but deals with expressions. This function just replaces |
| 11797 | template parms; to finish processing the resultant expression, use |
| 11798 | tsubst_copy_and_build or tsubst_expr. */ |
| 11799 | |
| 11800 | static tree |
| 11801 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
| 11802 | { |
| 11803 | enum tree_code code; |
| 11804 | tree r; |
| 11805 | |
| 11806 | if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE) |
| 11807 | return t; |
| 11808 | |
| 11809 | code = TREE_CODE (t); |
| 11810 | |
| 11811 | switch (code) |
| 11812 | { |
| 11813 | case PARM_DECL: |
| 11814 | r = retrieve_local_specialization (t); |
| 11815 | |
| 11816 | if (r == NULL) |
| 11817 | { |
| 11818 | tree c; |
| 11819 | |
| 11820 | /* We get here for a use of 'this' in an NSDMI. */ |
| 11821 | if (DECL_NAME (t) == this_identifier |
| 11822 | && at_function_scope_p () |
| 11823 | && DECL_CONSTRUCTOR_P (current_function_decl)) |
| 11824 | return current_class_ptr; |
| 11825 | |
| 11826 | /* This can happen for a parameter name used later in a function |
| 11827 | declaration (such as in a late-specified return type). Just |
| 11828 | make a dummy decl, since it's only used for its type. */ |
| 11829 | gcc_assert (cp_unevaluated_operand != 0); |
| 11830 | /* We copy T because want to tsubst the PARM_DECL only, |
| 11831 | not the following PARM_DECLs that are chained to T. */ |
| 11832 | c = copy_node (t); |
| 11833 | r = tsubst_decl (c, args, complain); |
| 11834 | /* Give it the template pattern as its context; its true context |
| 11835 | hasn't been instantiated yet and this is good enough for |
| 11836 | mangling. */ |
| 11837 | DECL_CONTEXT (r) = DECL_CONTEXT (t); |
| 11838 | } |
| 11839 | |
| 11840 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
| 11841 | r = ARGUMENT_PACK_SELECT_ARG (r); |
| 11842 | mark_used (r); |
| 11843 | return r; |
| 11844 | |
| 11845 | case CONST_DECL: |
| 11846 | { |
| 11847 | tree enum_type; |
| 11848 | tree v; |
| 11849 | |
| 11850 | if (DECL_TEMPLATE_PARM_P (t)) |
| 11851 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); |
| 11852 | /* There is no need to substitute into namespace-scope |
| 11853 | enumerators. */ |
| 11854 | if (DECL_NAMESPACE_SCOPE_P (t)) |
| 11855 | return t; |
| 11856 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
| 11857 | if (args == NULL_TREE) |
| 11858 | return integral_constant_value (t); |
| 11859 | |
| 11860 | /* Unfortunately, we cannot just call lookup_name here. |
| 11861 | Consider: |
| 11862 | |
| 11863 | template <int I> int f() { |
| 11864 | enum E { a = I }; |
| 11865 | struct S { void g() { E e = a; } }; |
| 11866 | }; |
| 11867 | |
| 11868 | When we instantiate f<7>::S::g(), say, lookup_name is not |
| 11869 | clever enough to find f<7>::a. */ |
| 11870 | enum_type |
| 11871 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
| 11872 | /*entering_scope=*/0); |
| 11873 | |
| 11874 | for (v = TYPE_VALUES (enum_type); |
| 11875 | v != NULL_TREE; |
| 11876 | v = TREE_CHAIN (v)) |
| 11877 | if (TREE_PURPOSE (v) == DECL_NAME (t)) |
| 11878 | return TREE_VALUE (v); |
| 11879 | |
| 11880 | /* We didn't find the name. That should never happen; if |
| 11881 | name-lookup found it during preliminary parsing, we |
| 11882 | should find it again here during instantiation. */ |
| 11883 | gcc_unreachable (); |
| 11884 | } |
| 11885 | return t; |
| 11886 | |
| 11887 | case FIELD_DECL: |
| 11888 | if (DECL_CONTEXT (t)) |
| 11889 | { |
| 11890 | tree ctx; |
| 11891 | |
| 11892 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
| 11893 | /*entering_scope=*/1); |
| 11894 | if (ctx != DECL_CONTEXT (t)) |
| 11895 | { |
| 11896 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); |
| 11897 | if (!r) |
| 11898 | { |
| 11899 | if (complain & tf_error) |
| 11900 | error ("using invalid field %qD", t); |
| 11901 | return error_mark_node; |
| 11902 | } |
| 11903 | return r; |
| 11904 | } |
| 11905 | } |
| 11906 | |
| 11907 | return t; |
| 11908 | |
| 11909 | case VAR_DECL: |
| 11910 | case FUNCTION_DECL: |
| 11911 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
| 11912 | || local_variable_p (t)) |
| 11913 | t = tsubst (t, args, complain, in_decl); |
| 11914 | mark_used (t); |
| 11915 | return t; |
| 11916 | |
| 11917 | case NAMESPACE_DECL: |
| 11918 | return t; |
| 11919 | |
| 11920 | case OVERLOAD: |
| 11921 | /* An OVERLOAD will always be a non-dependent overload set; an |
| 11922 | overload set from function scope will just be represented with an |
| 11923 | IDENTIFIER_NODE, and from class scope with a BASELINK. */ |
| 11924 | gcc_assert (!uses_template_parms (t)); |
| 11925 | return t; |
| 11926 | |
| 11927 | case BASELINK: |
| 11928 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
| 11929 | |
| 11930 | case TEMPLATE_DECL: |
| 11931 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
| 11932 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
| 11933 | args, complain, in_decl); |
| 11934 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
| 11935 | return tsubst (t, args, complain, in_decl); |
| 11936 | else if (DECL_CLASS_SCOPE_P (t) |
| 11937 | && uses_template_parms (DECL_CONTEXT (t))) |
| 11938 | { |
| 11939 | /* Template template argument like the following example need |
| 11940 | special treatment: |
| 11941 | |
| 11942 | template <template <class> class TT> struct C {}; |
| 11943 | template <class T> struct D { |
| 11944 | template <class U> struct E {}; |
| 11945 | C<E> c; // #1 |
| 11946 | }; |
| 11947 | D<int> d; // #2 |
| 11948 | |
| 11949 | We are processing the template argument `E' in #1 for |
| 11950 | the template instantiation #2. Originally, `E' is a |
| 11951 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we |
| 11952 | have to substitute this with one having context `D<int>'. */ |
| 11953 | |
| 11954 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); |
| 11955 | return lookup_field (context, DECL_NAME(t), 0, false); |
| 11956 | } |
| 11957 | else |
| 11958 | /* Ordinary template template argument. */ |
| 11959 | return t; |
| 11960 | |
| 11961 | case CAST_EXPR: |
| 11962 | case REINTERPRET_CAST_EXPR: |
| 11963 | case CONST_CAST_EXPR: |
| 11964 | case STATIC_CAST_EXPR: |
| 11965 | case DYNAMIC_CAST_EXPR: |
| 11966 | case IMPLICIT_CONV_EXPR: |
| 11967 | case CONVERT_EXPR: |
| 11968 | case NOP_EXPR: |
| 11969 | return build1 |
| 11970 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
| 11971 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
| 11972 | |
| 11973 | case SIZEOF_EXPR: |
| 11974 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
| 11975 | { |
| 11976 | |
| 11977 | tree expanded; |
| 11978 | int len = 0; |
| 11979 | |
| 11980 | ++cp_unevaluated_operand; |
| 11981 | ++c_inhibit_evaluation_warnings; |
| 11982 | /* We only want to compute the number of arguments. */ |
| 11983 | expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args, |
| 11984 | complain, in_decl); |
| 11985 | --cp_unevaluated_operand; |
| 11986 | --c_inhibit_evaluation_warnings; |
| 11987 | |
| 11988 | if (TREE_CODE (expanded) == TREE_VEC) |
| 11989 | len = TREE_VEC_LENGTH (expanded); |
| 11990 | |
| 11991 | if (expanded == error_mark_node) |
| 11992 | return error_mark_node; |
| 11993 | else if (PACK_EXPANSION_P (expanded) |
| 11994 | || (TREE_CODE (expanded) == TREE_VEC |
| 11995 | && len > 0 |
| 11996 | && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1)))) |
| 11997 | { |
| 11998 | if (TREE_CODE (expanded) == TREE_VEC) |
| 11999 | expanded = TREE_VEC_ELT (expanded, len - 1); |
| 12000 | |
| 12001 | if (TYPE_P (expanded)) |
| 12002 | return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, |
| 12003 | complain & tf_error); |
| 12004 | else |
| 12005 | return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR, |
| 12006 | complain & tf_error); |
| 12007 | } |
| 12008 | else |
| 12009 | return build_int_cst (size_type_node, len); |
| 12010 | } |
| 12011 | /* Fall through */ |
| 12012 | |
| 12013 | case INDIRECT_REF: |
| 12014 | case NEGATE_EXPR: |
| 12015 | case TRUTH_NOT_EXPR: |
| 12016 | case BIT_NOT_EXPR: |
| 12017 | case ADDR_EXPR: |
| 12018 | case UNARY_PLUS_EXPR: /* Unary + */ |
| 12019 | case ALIGNOF_EXPR: |
| 12020 | case AT_ENCODE_EXPR: |
| 12021 | case ARROW_EXPR: |
| 12022 | case THROW_EXPR: |
| 12023 | case TYPEID_EXPR: |
| 12024 | case REALPART_EXPR: |
| 12025 | case IMAGPART_EXPR: |
| 12026 | return build1 |
| 12027 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
| 12028 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
| 12029 | |
| 12030 | case COMPONENT_REF: |
| 12031 | { |
| 12032 | tree object; |
| 12033 | tree name; |
| 12034 | |
| 12035 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
| 12036 | name = TREE_OPERAND (t, 1); |
| 12037 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
| 12038 | { |
| 12039 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
| 12040 | complain, in_decl); |
| 12041 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
| 12042 | } |
| 12043 | else if (TREE_CODE (name) == SCOPE_REF |
| 12044 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) |
| 12045 | { |
| 12046 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
| 12047 | complain, in_decl); |
| 12048 | name = TREE_OPERAND (name, 1); |
| 12049 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
| 12050 | complain, in_decl); |
| 12051 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
| 12052 | name = build_qualified_name (/*type=*/NULL_TREE, |
| 12053 | base, name, |
| 12054 | /*template_p=*/false); |
| 12055 | } |
| 12056 | else if (BASELINK_P (name)) |
| 12057 | name = tsubst_baselink (name, |
| 12058 | non_reference (TREE_TYPE (object)), |
| 12059 | args, complain, |
| 12060 | in_decl); |
| 12061 | else |
| 12062 | name = tsubst_copy (name, args, complain, in_decl); |
| 12063 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
| 12064 | } |
| 12065 | |
| 12066 | case PLUS_EXPR: |
| 12067 | case MINUS_EXPR: |
| 12068 | case MULT_EXPR: |
| 12069 | case TRUNC_DIV_EXPR: |
| 12070 | case CEIL_DIV_EXPR: |
| 12071 | case FLOOR_DIV_EXPR: |
| 12072 | case ROUND_DIV_EXPR: |
| 12073 | case EXACT_DIV_EXPR: |
| 12074 | case BIT_AND_EXPR: |
| 12075 | case BIT_IOR_EXPR: |
| 12076 | case BIT_XOR_EXPR: |
| 12077 | case TRUNC_MOD_EXPR: |
| 12078 | case FLOOR_MOD_EXPR: |
| 12079 | case TRUTH_ANDIF_EXPR: |
| 12080 | case TRUTH_ORIF_EXPR: |
| 12081 | case TRUTH_AND_EXPR: |
| 12082 | case TRUTH_OR_EXPR: |
| 12083 | case RSHIFT_EXPR: |
| 12084 | case LSHIFT_EXPR: |
| 12085 | case RROTATE_EXPR: |
| 12086 | case LROTATE_EXPR: |
| 12087 | case EQ_EXPR: |
| 12088 | case NE_EXPR: |
| 12089 | case MAX_EXPR: |
| 12090 | case MIN_EXPR: |
| 12091 | case LE_EXPR: |
| 12092 | case GE_EXPR: |
| 12093 | case LT_EXPR: |
| 12094 | case GT_EXPR: |
| 12095 | case COMPOUND_EXPR: |
| 12096 | case DOTSTAR_EXPR: |
| 12097 | case MEMBER_REF: |
| 12098 | case PREDECREMENT_EXPR: |
| 12099 | case PREINCREMENT_EXPR: |
| 12100 | case POSTDECREMENT_EXPR: |
| 12101 | case POSTINCREMENT_EXPR: |
| 12102 | return build_nt |
| 12103 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12104 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); |
| 12105 | |
| 12106 | case SCOPE_REF: |
| 12107 | return build_qualified_name (/*type=*/NULL_TREE, |
| 12108 | tsubst_copy (TREE_OPERAND (t, 0), |
| 12109 | args, complain, in_decl), |
| 12110 | tsubst_copy (TREE_OPERAND (t, 1), |
| 12111 | args, complain, in_decl), |
| 12112 | QUALIFIED_NAME_IS_TEMPLATE (t)); |
| 12113 | |
| 12114 | case ARRAY_REF: |
| 12115 | return build_nt |
| 12116 | (ARRAY_REF, |
| 12117 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12118 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), |
| 12119 | NULL_TREE, NULL_TREE); |
| 12120 | |
| 12121 | case CALL_EXPR: |
| 12122 | { |
| 12123 | int n = VL_EXP_OPERAND_LENGTH (t); |
| 12124 | tree result = build_vl_exp (CALL_EXPR, n); |
| 12125 | int i; |
| 12126 | for (i = 0; i < n; i++) |
| 12127 | TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args, |
| 12128 | complain, in_decl); |
| 12129 | return result; |
| 12130 | } |
| 12131 | |
| 12132 | case COND_EXPR: |
| 12133 | case MODOP_EXPR: |
| 12134 | case PSEUDO_DTOR_EXPR: |
| 12135 | { |
| 12136 | r = build_nt |
| 12137 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12138 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), |
| 12139 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); |
| 12140 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
| 12141 | return r; |
| 12142 | } |
| 12143 | |
| 12144 | case NEW_EXPR: |
| 12145 | { |
| 12146 | r = build_nt |
| 12147 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12148 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), |
| 12149 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); |
| 12150 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
| 12151 | return r; |
| 12152 | } |
| 12153 | |
| 12154 | case DELETE_EXPR: |
| 12155 | { |
| 12156 | r = build_nt |
| 12157 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12158 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); |
| 12159 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
| 12160 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); |
| 12161 | return r; |
| 12162 | } |
| 12163 | |
| 12164 | case TEMPLATE_ID_EXPR: |
| 12165 | { |
| 12166 | /* Substituted template arguments */ |
| 12167 | tree fn = TREE_OPERAND (t, 0); |
| 12168 | tree targs = TREE_OPERAND (t, 1); |
| 12169 | |
| 12170 | fn = tsubst_copy (fn, args, complain, in_decl); |
| 12171 | if (targs) |
| 12172 | targs = tsubst_template_args (targs, args, complain, in_decl); |
| 12173 | |
| 12174 | return lookup_template_function (fn, targs); |
| 12175 | } |
| 12176 | |
| 12177 | case TREE_LIST: |
| 12178 | { |
| 12179 | tree purpose, value, chain; |
| 12180 | |
| 12181 | if (t == void_list_node) |
| 12182 | return t; |
| 12183 | |
| 12184 | purpose = TREE_PURPOSE (t); |
| 12185 | if (purpose) |
| 12186 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
| 12187 | value = TREE_VALUE (t); |
| 12188 | if (value) |
| 12189 | value = tsubst_copy (value, args, complain, in_decl); |
| 12190 | chain = TREE_CHAIN (t); |
| 12191 | if (chain && chain != void_type_node) |
| 12192 | chain = tsubst_copy (chain, args, complain, in_decl); |
| 12193 | if (purpose == TREE_PURPOSE (t) |
| 12194 | && value == TREE_VALUE (t) |
| 12195 | && chain == TREE_CHAIN (t)) |
| 12196 | return t; |
| 12197 | return tree_cons (purpose, value, chain); |
| 12198 | } |
| 12199 | |
| 12200 | case RECORD_TYPE: |
| 12201 | case UNION_TYPE: |
| 12202 | case ENUMERAL_TYPE: |
| 12203 | case INTEGER_TYPE: |
| 12204 | case TEMPLATE_TYPE_PARM: |
| 12205 | case TEMPLATE_TEMPLATE_PARM: |
| 12206 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 12207 | case TEMPLATE_PARM_INDEX: |
| 12208 | case POINTER_TYPE: |
| 12209 | case REFERENCE_TYPE: |
| 12210 | case OFFSET_TYPE: |
| 12211 | case FUNCTION_TYPE: |
| 12212 | case METHOD_TYPE: |
| 12213 | case ARRAY_TYPE: |
| 12214 | case TYPENAME_TYPE: |
| 12215 | case UNBOUND_CLASS_TEMPLATE: |
| 12216 | case TYPEOF_TYPE: |
| 12217 | case DECLTYPE_TYPE: |
| 12218 | case TYPE_DECL: |
| 12219 | return tsubst (t, args, complain, in_decl); |
| 12220 | |
| 12221 | case USING_DECL: |
| 12222 | t = DECL_NAME (t); |
| 12223 | /* Fall through. */ |
| 12224 | case IDENTIFIER_NODE: |
| 12225 | if (IDENTIFIER_TYPENAME_P (t)) |
| 12226 | { |
| 12227 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 12228 | return mangle_conv_op_name_for_type (new_type); |
| 12229 | } |
| 12230 | else |
| 12231 | return t; |
| 12232 | |
| 12233 | case CONSTRUCTOR: |
| 12234 | /* This is handled by tsubst_copy_and_build. */ |
| 12235 | gcc_unreachable (); |
| 12236 | |
| 12237 | case VA_ARG_EXPR: |
| 12238 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
| 12239 | in_decl), |
| 12240 | tsubst (TREE_TYPE (t), args, complain, in_decl)); |
| 12241 | |
| 12242 | case CLEANUP_POINT_EXPR: |
| 12243 | /* We shouldn't have built any of these during initial template |
| 12244 | generation. Instead, they should be built during instantiation |
| 12245 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ |
| 12246 | gcc_unreachable (); |
| 12247 | |
| 12248 | case OFFSET_REF: |
| 12249 | r = build2 |
| 12250 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
| 12251 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
| 12252 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); |
| 12253 | PTRMEM_OK_P (r) = PTRMEM_OK_P (t); |
| 12254 | mark_used (TREE_OPERAND (r, 1)); |
| 12255 | return r; |
| 12256 | |
| 12257 | case EXPR_PACK_EXPANSION: |
| 12258 | error ("invalid use of pack expansion expression"); |
| 12259 | return error_mark_node; |
| 12260 | |
| 12261 | case NONTYPE_ARGUMENT_PACK: |
| 12262 | error ("use %<...%> to expand argument pack"); |
| 12263 | return error_mark_node; |
| 12264 | |
| 12265 | case INTEGER_CST: |
| 12266 | case REAL_CST: |
| 12267 | case STRING_CST: |
| 12268 | case COMPLEX_CST: |
| 12269 | { |
| 12270 | /* Instantiate any typedefs in the type. */ |
| 12271 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 12272 | r = fold_convert (type, t); |
| 12273 | gcc_assert (TREE_CODE (r) == code); |
| 12274 | return r; |
| 12275 | } |
| 12276 | |
| 12277 | case PTRMEM_CST: |
| 12278 | /* These can sometimes show up in a partial instantiation, but never |
| 12279 | involve template parms. */ |
| 12280 | gcc_assert (!uses_template_parms (t)); |
| 12281 | return t; |
| 12282 | |
| 12283 | default: |
| 12284 | /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */ |
| 12285 | gcc_checking_assert (false); |
| 12286 | return t; |
| 12287 | } |
| 12288 | } |
| 12289 | |
| 12290 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
| 12291 | |
| 12292 | static tree |
| 12293 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, |
| 12294 | tree in_decl) |
| 12295 | { |
| 12296 | tree new_clauses = NULL, nc, oc; |
| 12297 | |
| 12298 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) |
| 12299 | { |
| 12300 | nc = copy_node (oc); |
| 12301 | OMP_CLAUSE_CHAIN (nc) = new_clauses; |
| 12302 | new_clauses = nc; |
| 12303 | |
| 12304 | switch (OMP_CLAUSE_CODE (nc)) |
| 12305 | { |
| 12306 | case OMP_CLAUSE_LASTPRIVATE: |
| 12307 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) |
| 12308 | { |
| 12309 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); |
| 12310 | tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain, |
| 12311 | in_decl, /*integral_constant_expression_p=*/false); |
| 12312 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) |
| 12313 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); |
| 12314 | } |
| 12315 | /* FALLTHRU */ |
| 12316 | case OMP_CLAUSE_PRIVATE: |
| 12317 | case OMP_CLAUSE_SHARED: |
| 12318 | case OMP_CLAUSE_FIRSTPRIVATE: |
| 12319 | case OMP_CLAUSE_REDUCTION: |
| 12320 | case OMP_CLAUSE_COPYIN: |
| 12321 | case OMP_CLAUSE_COPYPRIVATE: |
| 12322 | case OMP_CLAUSE_IF: |
| 12323 | case OMP_CLAUSE_NUM_THREADS: |
| 12324 | case OMP_CLAUSE_SCHEDULE: |
| 12325 | case OMP_CLAUSE_COLLAPSE: |
| 12326 | case OMP_CLAUSE_FINAL: |
| 12327 | OMP_CLAUSE_OPERAND (nc, 0) |
| 12328 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
| 12329 | in_decl, /*integral_constant_expression_p=*/false); |
| 12330 | break; |
| 12331 | case OMP_CLAUSE_NOWAIT: |
| 12332 | case OMP_CLAUSE_ORDERED: |
| 12333 | case OMP_CLAUSE_DEFAULT: |
| 12334 | case OMP_CLAUSE_UNTIED: |
| 12335 | case OMP_CLAUSE_MERGEABLE: |
| 12336 | break; |
| 12337 | default: |
| 12338 | gcc_unreachable (); |
| 12339 | } |
| 12340 | } |
| 12341 | |
| 12342 | return finish_omp_clauses (nreverse (new_clauses)); |
| 12343 | } |
| 12344 | |
| 12345 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
| 12346 | |
| 12347 | static tree |
| 12348 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, |
| 12349 | tree in_decl) |
| 12350 | { |
| 12351 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) |
| 12352 | |
| 12353 | tree purpose, value, chain; |
| 12354 | |
| 12355 | if (t == NULL) |
| 12356 | return t; |
| 12357 | |
| 12358 | if (TREE_CODE (t) != TREE_LIST) |
| 12359 | return tsubst_copy_and_build (t, args, complain, in_decl, |
| 12360 | /*function_p=*/false, |
| 12361 | /*integral_constant_expression_p=*/false); |
| 12362 | |
| 12363 | if (t == void_list_node) |
| 12364 | return t; |
| 12365 | |
| 12366 | purpose = TREE_PURPOSE (t); |
| 12367 | if (purpose) |
| 12368 | purpose = RECUR (purpose); |
| 12369 | value = TREE_VALUE (t); |
| 12370 | if (value) |
| 12371 | { |
| 12372 | if (TREE_CODE (value) != LABEL_DECL) |
| 12373 | value = RECUR (value); |
| 12374 | else |
| 12375 | { |
| 12376 | value = lookup_label (DECL_NAME (value)); |
| 12377 | gcc_assert (TREE_CODE (value) == LABEL_DECL); |
| 12378 | TREE_USED (value) = 1; |
| 12379 | } |
| 12380 | } |
| 12381 | chain = TREE_CHAIN (t); |
| 12382 | if (chain && chain != void_type_node) |
| 12383 | chain = RECUR (chain); |
| 12384 | return tree_cons (purpose, value, chain); |
| 12385 | #undef RECUR |
| 12386 | } |
| 12387 | |
| 12388 | /* Substitute one OMP_FOR iterator. */ |
| 12389 | |
| 12390 | static void |
| 12391 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv, |
| 12392 | tree condv, tree incrv, tree *clauses, |
| 12393 | tree args, tsubst_flags_t complain, tree in_decl, |
| 12394 | bool integral_constant_expression_p) |
| 12395 | { |
| 12396 | #define RECUR(NODE) \ |
| 12397 | tsubst_expr ((NODE), args, complain, in_decl, \ |
| 12398 | integral_constant_expression_p) |
| 12399 | tree decl, init, cond, incr, auto_node; |
| 12400 | |
| 12401 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); |
| 12402 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); |
| 12403 | decl = RECUR (TREE_OPERAND (init, 0)); |
| 12404 | init = TREE_OPERAND (init, 1); |
| 12405 | auto_node = type_uses_auto (TREE_TYPE (decl)); |
| 12406 | if (auto_node && init) |
| 12407 | { |
| 12408 | tree init_expr = init; |
| 12409 | if (TREE_CODE (init_expr) == DECL_EXPR) |
| 12410 | init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr)); |
| 12411 | init_expr = RECUR (init_expr); |
| 12412 | TREE_TYPE (decl) |
| 12413 | = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node); |
| 12414 | } |
| 12415 | gcc_assert (!type_dependent_expression_p (decl)); |
| 12416 | |
| 12417 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) |
| 12418 | { |
| 12419 | cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i)); |
| 12420 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); |
| 12421 | if (TREE_CODE (incr) == MODIFY_EXPR) |
| 12422 | incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR, |
| 12423 | RECUR (TREE_OPERAND (incr, 1)), |
| 12424 | complain); |
| 12425 | else |
| 12426 | incr = RECUR (incr); |
| 12427 | TREE_VEC_ELT (declv, i) = decl; |
| 12428 | TREE_VEC_ELT (initv, i) = init; |
| 12429 | TREE_VEC_ELT (condv, i) = cond; |
| 12430 | TREE_VEC_ELT (incrv, i) = incr; |
| 12431 | return; |
| 12432 | } |
| 12433 | |
| 12434 | if (init && TREE_CODE (init) != DECL_EXPR) |
| 12435 | { |
| 12436 | tree c; |
| 12437 | for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c)) |
| 12438 | { |
| 12439 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE |
| 12440 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) |
| 12441 | && OMP_CLAUSE_DECL (c) == decl) |
| 12442 | break; |
| 12443 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE |
| 12444 | && OMP_CLAUSE_DECL (c) == decl) |
| 12445 | error ("iteration variable %qD should not be firstprivate", decl); |
| 12446 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION |
| 12447 | && OMP_CLAUSE_DECL (c) == decl) |
| 12448 | error ("iteration variable %qD should not be reduction", decl); |
| 12449 | } |
| 12450 | if (c == NULL) |
| 12451 | { |
| 12452 | c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE); |
| 12453 | OMP_CLAUSE_DECL (c) = decl; |
| 12454 | c = finish_omp_clauses (c); |
| 12455 | if (c) |
| 12456 | { |
| 12457 | OMP_CLAUSE_CHAIN (c) = *clauses; |
| 12458 | *clauses = c; |
| 12459 | } |
| 12460 | } |
| 12461 | } |
| 12462 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); |
| 12463 | if (COMPARISON_CLASS_P (cond)) |
| 12464 | cond = build2 (TREE_CODE (cond), boolean_type_node, |
| 12465 | RECUR (TREE_OPERAND (cond, 0)), |
| 12466 | RECUR (TREE_OPERAND (cond, 1))); |
| 12467 | else |
| 12468 | cond = RECUR (cond); |
| 12469 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); |
| 12470 | switch (TREE_CODE (incr)) |
| 12471 | { |
| 12472 | case PREINCREMENT_EXPR: |
| 12473 | case PREDECREMENT_EXPR: |
| 12474 | case POSTINCREMENT_EXPR: |
| 12475 | case POSTDECREMENT_EXPR: |
| 12476 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), |
| 12477 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); |
| 12478 | break; |
| 12479 | case MODIFY_EXPR: |
| 12480 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR |
| 12481 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) |
| 12482 | { |
| 12483 | tree rhs = TREE_OPERAND (incr, 1); |
| 12484 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), |
| 12485 | RECUR (TREE_OPERAND (incr, 0)), |
| 12486 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), |
| 12487 | RECUR (TREE_OPERAND (rhs, 0)), |
| 12488 | RECUR (TREE_OPERAND (rhs, 1)))); |
| 12489 | } |
| 12490 | else |
| 12491 | incr = RECUR (incr); |
| 12492 | break; |
| 12493 | case MODOP_EXPR: |
| 12494 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR |
| 12495 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) |
| 12496 | { |
| 12497 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); |
| 12498 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, |
| 12499 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), |
| 12500 | TREE_TYPE (decl), lhs, |
| 12501 | RECUR (TREE_OPERAND (incr, 2)))); |
| 12502 | } |
| 12503 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR |
| 12504 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR |
| 12505 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) |
| 12506 | { |
| 12507 | tree rhs = TREE_OPERAND (incr, 2); |
| 12508 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), |
| 12509 | RECUR (TREE_OPERAND (incr, 0)), |
| 12510 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), |
| 12511 | RECUR (TREE_OPERAND (rhs, 0)), |
| 12512 | RECUR (TREE_OPERAND (rhs, 1)))); |
| 12513 | } |
| 12514 | else |
| 12515 | incr = RECUR (incr); |
| 12516 | break; |
| 12517 | default: |
| 12518 | incr = RECUR (incr); |
| 12519 | break; |
| 12520 | } |
| 12521 | |
| 12522 | TREE_VEC_ELT (declv, i) = decl; |
| 12523 | TREE_VEC_ELT (initv, i) = init; |
| 12524 | TREE_VEC_ELT (condv, i) = cond; |
| 12525 | TREE_VEC_ELT (incrv, i) = incr; |
| 12526 | #undef RECUR |
| 12527 | } |
| 12528 | |
| 12529 | /* Like tsubst_copy for expressions, etc. but also does semantic |
| 12530 | processing. */ |
| 12531 | |
| 12532 | static tree |
| 12533 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
| 12534 | bool integral_constant_expression_p) |
| 12535 | { |
| 12536 | #define RECUR(NODE) \ |
| 12537 | tsubst_expr ((NODE), args, complain, in_decl, \ |
| 12538 | integral_constant_expression_p) |
| 12539 | |
| 12540 | tree stmt, tmp; |
| 12541 | |
| 12542 | if (t == NULL_TREE || t == error_mark_node) |
| 12543 | return t; |
| 12544 | |
| 12545 | if (EXPR_HAS_LOCATION (t)) |
| 12546 | input_location = EXPR_LOCATION (t); |
| 12547 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
| 12548 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); |
| 12549 | |
| 12550 | switch (TREE_CODE (t)) |
| 12551 | { |
| 12552 | case STATEMENT_LIST: |
| 12553 | { |
| 12554 | tree_stmt_iterator i; |
| 12555 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) |
| 12556 | RECUR (tsi_stmt (i)); |
| 12557 | break; |
| 12558 | } |
| 12559 | |
| 12560 | case CTOR_INITIALIZER: |
| 12561 | finish_mem_initializers (tsubst_initializer_list |
| 12562 | (TREE_OPERAND (t, 0), args)); |
| 12563 | break; |
| 12564 | |
| 12565 | case RETURN_EXPR: |
| 12566 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
| 12567 | break; |
| 12568 | |
| 12569 | case EXPR_STMT: |
| 12570 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
| 12571 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
| 12572 | finish_stmt_expr_expr (tmp, cur_stmt_expr); |
| 12573 | else |
| 12574 | finish_expr_stmt (tmp); |
| 12575 | break; |
| 12576 | |
| 12577 | case USING_STMT: |
| 12578 | do_using_directive (USING_STMT_NAMESPACE (t)); |
| 12579 | break; |
| 12580 | |
| 12581 | case DECL_EXPR: |
| 12582 | { |
| 12583 | tree decl, pattern_decl; |
| 12584 | tree init; |
| 12585 | |
| 12586 | pattern_decl = decl = DECL_EXPR_DECL (t); |
| 12587 | if (TREE_CODE (decl) == LABEL_DECL) |
| 12588 | finish_label_decl (DECL_NAME (decl)); |
| 12589 | else if (TREE_CODE (decl) == USING_DECL) |
| 12590 | { |
| 12591 | tree scope = USING_DECL_SCOPE (decl); |
| 12592 | tree name = DECL_NAME (decl); |
| 12593 | tree decl; |
| 12594 | |
| 12595 | scope = tsubst (scope, args, complain, in_decl); |
| 12596 | decl = lookup_qualified_name (scope, name, |
| 12597 | /*is_type_p=*/false, |
| 12598 | /*complain=*/false); |
| 12599 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
| 12600 | qualified_name_lookup_error (scope, name, decl, input_location); |
| 12601 | else |
| 12602 | do_local_using_decl (decl, scope, name); |
| 12603 | } |
| 12604 | else |
| 12605 | { |
| 12606 | init = DECL_INITIAL (decl); |
| 12607 | decl = tsubst (decl, args, complain, in_decl); |
| 12608 | if (decl != error_mark_node) |
| 12609 | { |
| 12610 | /* By marking the declaration as instantiated, we avoid |
| 12611 | trying to instantiate it. Since instantiate_decl can't |
| 12612 | handle local variables, and since we've already done |
| 12613 | all that needs to be done, that's the right thing to |
| 12614 | do. */ |
| 12615 | if (TREE_CODE (decl) == VAR_DECL) |
| 12616 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; |
| 12617 | if (TREE_CODE (decl) == VAR_DECL |
| 12618 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) |
| 12619 | /* Anonymous aggregates are a special case. */ |
| 12620 | finish_anon_union (decl); |
| 12621 | else if (is_capture_proxy (DECL_EXPR_DECL (t))) |
| 12622 | { |
| 12623 | DECL_CONTEXT (decl) = current_function_decl; |
| 12624 | if (DECL_NAME (decl) == this_identifier) |
| 12625 | { |
| 12626 | tree lam = DECL_CONTEXT (current_function_decl); |
| 12627 | lam = CLASSTYPE_LAMBDA_EXPR (lam); |
| 12628 | LAMBDA_EXPR_THIS_CAPTURE (lam) = decl; |
| 12629 | } |
| 12630 | insert_capture_proxy (decl); |
| 12631 | } |
| 12632 | else |
| 12633 | { |
| 12634 | int const_init = false; |
| 12635 | maybe_push_decl (decl); |
| 12636 | if (TREE_CODE (decl) == VAR_DECL |
| 12637 | && DECL_PRETTY_FUNCTION_P (decl)) |
| 12638 | { |
| 12639 | /* For __PRETTY_FUNCTION__ we have to adjust the |
| 12640 | initializer. */ |
| 12641 | const char *const name |
| 12642 | = cxx_printable_name (current_function_decl, 2); |
| 12643 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
| 12644 | } |
| 12645 | else |
| 12646 | { |
| 12647 | tree t = RECUR (init); |
| 12648 | |
| 12649 | if (init && !t) |
| 12650 | { |
| 12651 | /* If we had an initializer but it |
| 12652 | instantiated to nothing, |
| 12653 | value-initialize the object. This will |
| 12654 | only occur when the initializer was a |
| 12655 | pack expansion where the parameter packs |
| 12656 | used in that expansion were of length |
| 12657 | zero. */ |
| 12658 | init = build_value_init (TREE_TYPE (decl), |
| 12659 | complain); |
| 12660 | if (TREE_CODE (init) == AGGR_INIT_EXPR) |
| 12661 | init = get_target_expr_sfinae (init, complain); |
| 12662 | } |
| 12663 | else |
| 12664 | init = t; |
| 12665 | } |
| 12666 | |
| 12667 | if (TREE_CODE (decl) == VAR_DECL) |
| 12668 | const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P |
| 12669 | (pattern_decl)); |
| 12670 | cp_finish_decl (decl, init, const_init, NULL_TREE, 0); |
| 12671 | } |
| 12672 | } |
| 12673 | } |
| 12674 | |
| 12675 | /* A DECL_EXPR can also be used as an expression, in the condition |
| 12676 | clause of an if/for/while construct. */ |
| 12677 | return decl; |
| 12678 | } |
| 12679 | |
| 12680 | case FOR_STMT: |
| 12681 | stmt = begin_for_stmt (NULL_TREE, NULL_TREE); |
| 12682 | RECUR (FOR_INIT_STMT (t)); |
| 12683 | finish_for_init_stmt (stmt); |
| 12684 | tmp = RECUR (FOR_COND (t)); |
| 12685 | finish_for_cond (tmp, stmt); |
| 12686 | tmp = RECUR (FOR_EXPR (t)); |
| 12687 | finish_for_expr (tmp, stmt); |
| 12688 | RECUR (FOR_BODY (t)); |
| 12689 | finish_for_stmt (stmt); |
| 12690 | break; |
| 12691 | |
| 12692 | case RANGE_FOR_STMT: |
| 12693 | { |
| 12694 | tree decl, expr; |
| 12695 | stmt = begin_for_stmt (NULL_TREE, NULL_TREE); |
| 12696 | decl = RANGE_FOR_DECL (t); |
| 12697 | decl = tsubst (decl, args, complain, in_decl); |
| 12698 | maybe_push_decl (decl); |
| 12699 | expr = RECUR (RANGE_FOR_EXPR (t)); |
| 12700 | stmt = cp_convert_range_for (stmt, decl, expr); |
| 12701 | RECUR (RANGE_FOR_BODY (t)); |
| 12702 | finish_for_stmt (stmt); |
| 12703 | } |
| 12704 | break; |
| 12705 | |
| 12706 | case WHILE_STMT: |
| 12707 | stmt = begin_while_stmt (); |
| 12708 | tmp = RECUR (WHILE_COND (t)); |
| 12709 | finish_while_stmt_cond (tmp, stmt); |
| 12710 | RECUR (WHILE_BODY (t)); |
| 12711 | finish_while_stmt (stmt); |
| 12712 | break; |
| 12713 | |
| 12714 | case DO_STMT: |
| 12715 | stmt = begin_do_stmt (); |
| 12716 | RECUR (DO_BODY (t)); |
| 12717 | finish_do_body (stmt); |
| 12718 | tmp = RECUR (DO_COND (t)); |
| 12719 | finish_do_stmt (tmp, stmt); |
| 12720 | break; |
| 12721 | |
| 12722 | case IF_STMT: |
| 12723 | stmt = begin_if_stmt (); |
| 12724 | tmp = RECUR (IF_COND (t)); |
| 12725 | finish_if_stmt_cond (tmp, stmt); |
| 12726 | RECUR (THEN_CLAUSE (t)); |
| 12727 | finish_then_clause (stmt); |
| 12728 | |
| 12729 | if (ELSE_CLAUSE (t)) |
| 12730 | { |
| 12731 | begin_else_clause (stmt); |
| 12732 | RECUR (ELSE_CLAUSE (t)); |
| 12733 | finish_else_clause (stmt); |
| 12734 | } |
| 12735 | |
| 12736 | finish_if_stmt (stmt); |
| 12737 | break; |
| 12738 | |
| 12739 | case BIND_EXPR: |
| 12740 | if (BIND_EXPR_BODY_BLOCK (t)) |
| 12741 | stmt = begin_function_body (); |
| 12742 | else |
| 12743 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) |
| 12744 | ? BCS_TRY_BLOCK : 0); |
| 12745 | |
| 12746 | RECUR (BIND_EXPR_BODY (t)); |
| 12747 | |
| 12748 | if (BIND_EXPR_BODY_BLOCK (t)) |
| 12749 | finish_function_body (stmt); |
| 12750 | else |
| 12751 | finish_compound_stmt (stmt); |
| 12752 | break; |
| 12753 | |
| 12754 | case BREAK_STMT: |
| 12755 | finish_break_stmt (); |
| 12756 | break; |
| 12757 | |
| 12758 | case CONTINUE_STMT: |
| 12759 | finish_continue_stmt (); |
| 12760 | break; |
| 12761 | |
| 12762 | case SWITCH_STMT: |
| 12763 | stmt = begin_switch_stmt (); |
| 12764 | tmp = RECUR (SWITCH_STMT_COND (t)); |
| 12765 | finish_switch_cond (tmp, stmt); |
| 12766 | RECUR (SWITCH_STMT_BODY (t)); |
| 12767 | finish_switch_stmt (stmt); |
| 12768 | break; |
| 12769 | |
| 12770 | case CASE_LABEL_EXPR: |
| 12771 | finish_case_label (EXPR_LOCATION (t), |
| 12772 | RECUR (CASE_LOW (t)), |
| 12773 | RECUR (CASE_HIGH (t))); |
| 12774 | break; |
| 12775 | |
| 12776 | case LABEL_EXPR: |
| 12777 | { |
| 12778 | tree decl = LABEL_EXPR_LABEL (t); |
| 12779 | tree label; |
| 12780 | |
| 12781 | label = finish_label_stmt (DECL_NAME (decl)); |
| 12782 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) |
| 12783 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); |
| 12784 | } |
| 12785 | break; |
| 12786 | |
| 12787 | case GOTO_EXPR: |
| 12788 | tmp = GOTO_DESTINATION (t); |
| 12789 | if (TREE_CODE (tmp) != LABEL_DECL) |
| 12790 | /* Computed goto's must be tsubst'd into. On the other hand, |
| 12791 | non-computed gotos must not be; the identifier in question |
| 12792 | will have no binding. */ |
| 12793 | tmp = RECUR (tmp); |
| 12794 | else |
| 12795 | tmp = DECL_NAME (tmp); |
| 12796 | finish_goto_stmt (tmp); |
| 12797 | break; |
| 12798 | |
| 12799 | case ASM_EXPR: |
| 12800 | tmp = finish_asm_stmt |
| 12801 | (ASM_VOLATILE_P (t), |
| 12802 | RECUR (ASM_STRING (t)), |
| 12803 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
| 12804 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), |
| 12805 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl), |
| 12806 | tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl)); |
| 12807 | { |
| 12808 | tree asm_expr = tmp; |
| 12809 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) |
| 12810 | asm_expr = TREE_OPERAND (asm_expr, 0); |
| 12811 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); |
| 12812 | } |
| 12813 | break; |
| 12814 | |
| 12815 | case TRY_BLOCK: |
| 12816 | if (CLEANUP_P (t)) |
| 12817 | { |
| 12818 | stmt = begin_try_block (); |
| 12819 | RECUR (TRY_STMTS (t)); |
| 12820 | finish_cleanup_try_block (stmt); |
| 12821 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
| 12822 | } |
| 12823 | else |
| 12824 | { |
| 12825 | tree compound_stmt = NULL_TREE; |
| 12826 | |
| 12827 | if (FN_TRY_BLOCK_P (t)) |
| 12828 | stmt = begin_function_try_block (&compound_stmt); |
| 12829 | else |
| 12830 | stmt = begin_try_block (); |
| 12831 | |
| 12832 | RECUR (TRY_STMTS (t)); |
| 12833 | |
| 12834 | if (FN_TRY_BLOCK_P (t)) |
| 12835 | finish_function_try_block (stmt); |
| 12836 | else |
| 12837 | finish_try_block (stmt); |
| 12838 | |
| 12839 | RECUR (TRY_HANDLERS (t)); |
| 12840 | if (FN_TRY_BLOCK_P (t)) |
| 12841 | finish_function_handler_sequence (stmt, compound_stmt); |
| 12842 | else |
| 12843 | finish_handler_sequence (stmt); |
| 12844 | } |
| 12845 | break; |
| 12846 | |
| 12847 | case HANDLER: |
| 12848 | { |
| 12849 | tree decl = HANDLER_PARMS (t); |
| 12850 | |
| 12851 | if (decl) |
| 12852 | { |
| 12853 | decl = tsubst (decl, args, complain, in_decl); |
| 12854 | /* Prevent instantiate_decl from trying to instantiate |
| 12855 | this variable. We've already done all that needs to be |
| 12856 | done. */ |
| 12857 | if (decl != error_mark_node) |
| 12858 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; |
| 12859 | } |
| 12860 | stmt = begin_handler (); |
| 12861 | finish_handler_parms (decl, stmt); |
| 12862 | RECUR (HANDLER_BODY (t)); |
| 12863 | finish_handler (stmt); |
| 12864 | } |
| 12865 | break; |
| 12866 | |
| 12867 | case TAG_DEFN: |
| 12868 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
| 12869 | break; |
| 12870 | |
| 12871 | case STATIC_ASSERT: |
| 12872 | { |
| 12873 | tree condition = |
| 12874 | tsubst_expr (STATIC_ASSERT_CONDITION (t), |
| 12875 | args, |
| 12876 | complain, in_decl, |
| 12877 | /*integral_constant_expression_p=*/true); |
| 12878 | finish_static_assert (condition, |
| 12879 | STATIC_ASSERT_MESSAGE (t), |
| 12880 | STATIC_ASSERT_SOURCE_LOCATION (t), |
| 12881 | /*member_p=*/false); |
| 12882 | } |
| 12883 | break; |
| 12884 | |
| 12885 | case OMP_PARALLEL: |
| 12886 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), |
| 12887 | args, complain, in_decl); |
| 12888 | stmt = begin_omp_parallel (); |
| 12889 | RECUR (OMP_PARALLEL_BODY (t)); |
| 12890 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
| 12891 | = OMP_PARALLEL_COMBINED (t); |
| 12892 | break; |
| 12893 | |
| 12894 | case OMP_TASK: |
| 12895 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), |
| 12896 | args, complain, in_decl); |
| 12897 | stmt = begin_omp_task (); |
| 12898 | RECUR (OMP_TASK_BODY (t)); |
| 12899 | finish_omp_task (tmp, stmt); |
| 12900 | break; |
| 12901 | |
| 12902 | case OMP_FOR: |
| 12903 | { |
| 12904 | tree clauses, body, pre_body; |
| 12905 | tree declv, initv, condv, incrv; |
| 12906 | int i; |
| 12907 | |
| 12908 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), |
| 12909 | args, complain, in_decl); |
| 12910 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
| 12911 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
| 12912 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
| 12913 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
| 12914 | |
| 12915 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) |
| 12916 | tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv, |
| 12917 | &clauses, args, complain, in_decl, |
| 12918 | integral_constant_expression_p); |
| 12919 | |
| 12920 | stmt = begin_omp_structured_block (); |
| 12921 | |
| 12922 | for (i = 0; i < TREE_VEC_LENGTH (initv); i++) |
| 12923 | if (TREE_VEC_ELT (initv, i) == NULL |
| 12924 | || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR) |
| 12925 | TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i)); |
| 12926 | else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i)))) |
| 12927 | { |
| 12928 | tree init = RECUR (TREE_VEC_ELT (initv, i)); |
| 12929 | gcc_assert (init == TREE_VEC_ELT (declv, i)); |
| 12930 | TREE_VEC_ELT (initv, i) = NULL_TREE; |
| 12931 | } |
| 12932 | else |
| 12933 | { |
| 12934 | tree decl_expr = TREE_VEC_ELT (initv, i); |
| 12935 | tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); |
| 12936 | gcc_assert (init != NULL); |
| 12937 | TREE_VEC_ELT (initv, i) = RECUR (init); |
| 12938 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL; |
| 12939 | RECUR (decl_expr); |
| 12940 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init; |
| 12941 | } |
| 12942 | |
| 12943 | pre_body = push_stmt_list (); |
| 12944 | RECUR (OMP_FOR_PRE_BODY (t)); |
| 12945 | pre_body = pop_stmt_list (pre_body); |
| 12946 | |
| 12947 | body = push_stmt_list (); |
| 12948 | RECUR (OMP_FOR_BODY (t)); |
| 12949 | body = pop_stmt_list (body); |
| 12950 | |
| 12951 | t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv, |
| 12952 | body, pre_body, clauses); |
| 12953 | |
| 12954 | add_stmt (finish_omp_structured_block (stmt)); |
| 12955 | } |
| 12956 | break; |
| 12957 | |
| 12958 | case OMP_SECTIONS: |
| 12959 | case OMP_SINGLE: |
| 12960 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); |
| 12961 | stmt = push_stmt_list (); |
| 12962 | RECUR (OMP_BODY (t)); |
| 12963 | stmt = pop_stmt_list (stmt); |
| 12964 | |
| 12965 | t = copy_node (t); |
| 12966 | OMP_BODY (t) = stmt; |
| 12967 | OMP_CLAUSES (t) = tmp; |
| 12968 | add_stmt (t); |
| 12969 | break; |
| 12970 | |
| 12971 | case OMP_SECTION: |
| 12972 | case OMP_CRITICAL: |
| 12973 | case OMP_MASTER: |
| 12974 | case OMP_ORDERED: |
| 12975 | stmt = push_stmt_list (); |
| 12976 | RECUR (OMP_BODY (t)); |
| 12977 | stmt = pop_stmt_list (stmt); |
| 12978 | |
| 12979 | t = copy_node (t); |
| 12980 | OMP_BODY (t) = stmt; |
| 12981 | add_stmt (t); |
| 12982 | break; |
| 12983 | |
| 12984 | case OMP_ATOMIC: |
| 12985 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
| 12986 | if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR) |
| 12987 | { |
| 12988 | tree op1 = TREE_OPERAND (t, 1); |
| 12989 | tree rhs1 = NULL_TREE; |
| 12990 | tree lhs, rhs; |
| 12991 | if (TREE_CODE (op1) == COMPOUND_EXPR) |
| 12992 | { |
| 12993 | rhs1 = RECUR (TREE_OPERAND (op1, 0)); |
| 12994 | op1 = TREE_OPERAND (op1, 1); |
| 12995 | } |
| 12996 | lhs = RECUR (TREE_OPERAND (op1, 0)); |
| 12997 | rhs = RECUR (TREE_OPERAND (op1, 1)); |
| 12998 | finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs, |
| 12999 | NULL_TREE, NULL_TREE, rhs1); |
| 13000 | } |
| 13001 | else |
| 13002 | { |
| 13003 | tree op1 = TREE_OPERAND (t, 1); |
| 13004 | tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE; |
| 13005 | tree rhs1 = NULL_TREE; |
| 13006 | enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1)); |
| 13007 | enum tree_code opcode = NOP_EXPR; |
| 13008 | if (code == OMP_ATOMIC_READ) |
| 13009 | { |
| 13010 | v = RECUR (TREE_OPERAND (op1, 0)); |
| 13011 | lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); |
| 13012 | } |
| 13013 | else if (code == OMP_ATOMIC_CAPTURE_OLD |
| 13014 | || code == OMP_ATOMIC_CAPTURE_NEW) |
| 13015 | { |
| 13016 | tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1); |
| 13017 | v = RECUR (TREE_OPERAND (op1, 0)); |
| 13018 | lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); |
| 13019 | if (TREE_CODE (op11) == COMPOUND_EXPR) |
| 13020 | { |
| 13021 | rhs1 = RECUR (TREE_OPERAND (op11, 0)); |
| 13022 | op11 = TREE_OPERAND (op11, 1); |
| 13023 | } |
| 13024 | lhs = RECUR (TREE_OPERAND (op11, 0)); |
| 13025 | rhs = RECUR (TREE_OPERAND (op11, 1)); |
| 13026 | opcode = TREE_CODE (op11); |
| 13027 | } |
| 13028 | else |
| 13029 | { |
| 13030 | code = OMP_ATOMIC; |
| 13031 | lhs = RECUR (TREE_OPERAND (op1, 0)); |
| 13032 | rhs = RECUR (TREE_OPERAND (op1, 1)); |
| 13033 | } |
| 13034 | finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1); |
| 13035 | } |
| 13036 | break; |
| 13037 | |
| 13038 | case TRANSACTION_EXPR: |
| 13039 | { |
| 13040 | int flags = 0; |
| 13041 | flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0); |
| 13042 | flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0); |
| 13043 | |
| 13044 | if (TRANSACTION_EXPR_IS_STMT (t)) |
| 13045 | { |
| 13046 | tree body = TRANSACTION_EXPR_BODY (t); |
| 13047 | tree noex = NULL_TREE; |
| 13048 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) |
| 13049 | { |
| 13050 | noex = MUST_NOT_THROW_COND (body); |
| 13051 | if (noex == NULL_TREE) |
| 13052 | noex = boolean_true_node; |
| 13053 | body = TREE_OPERAND (body, 0); |
| 13054 | } |
| 13055 | stmt = begin_transaction_stmt (input_location, NULL, flags); |
| 13056 | RECUR (body); |
| 13057 | finish_transaction_stmt (stmt, NULL, flags, RECUR (noex)); |
| 13058 | } |
| 13059 | else |
| 13060 | { |
| 13061 | stmt = build_transaction_expr (EXPR_LOCATION (t), |
| 13062 | RECUR (TRANSACTION_EXPR_BODY (t)), |
| 13063 | flags, NULL_TREE); |
| 13064 | return stmt; |
| 13065 | } |
| 13066 | } |
| 13067 | break; |
| 13068 | |
| 13069 | case MUST_NOT_THROW_EXPR: |
| 13070 | return build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)), |
| 13071 | RECUR (MUST_NOT_THROW_COND (t))); |
| 13072 | |
| 13073 | case EXPR_PACK_EXPANSION: |
| 13074 | error ("invalid use of pack expansion expression"); |
| 13075 | return error_mark_node; |
| 13076 | |
| 13077 | case NONTYPE_ARGUMENT_PACK: |
| 13078 | error ("use %<...%> to expand argument pack"); |
| 13079 | return error_mark_node; |
| 13080 | |
| 13081 | default: |
| 13082 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
| 13083 | |
| 13084 | return tsubst_copy_and_build (t, args, complain, in_decl, |
| 13085 | /*function_p=*/false, |
| 13086 | integral_constant_expression_p); |
| 13087 | } |
| 13088 | |
| 13089 | return NULL_TREE; |
| 13090 | #undef RECUR |
| 13091 | } |
| 13092 | |
| 13093 | /* T is a postfix-expression that is not being used in a function |
| 13094 | call. Return the substituted version of T. */ |
| 13095 | |
| 13096 | static tree |
| 13097 | tsubst_non_call_postfix_expression (tree t, tree args, |
| 13098 | tsubst_flags_t complain, |
| 13099 | tree in_decl) |
| 13100 | { |
| 13101 | if (TREE_CODE (t) == SCOPE_REF) |
| 13102 | t = tsubst_qualified_id (t, args, complain, in_decl, |
| 13103 | /*done=*/false, /*address_p=*/false); |
| 13104 | else |
| 13105 | t = tsubst_copy_and_build (t, args, complain, in_decl, |
| 13106 | /*function_p=*/false, |
| 13107 | /*integral_constant_expression_p=*/false); |
| 13108 | |
| 13109 | return t; |
| 13110 | } |
| 13111 | |
| 13112 | /* Like tsubst but deals with expressions and performs semantic |
| 13113 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
| 13114 | |
| 13115 | tree |
| 13116 | tsubst_copy_and_build (tree t, |
| 13117 | tree args, |
| 13118 | tsubst_flags_t complain, |
| 13119 | tree in_decl, |
| 13120 | bool function_p, |
| 13121 | bool integral_constant_expression_p) |
| 13122 | { |
| 13123 | #define RECUR(NODE) \ |
| 13124 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ |
| 13125 | /*function_p=*/false, \ |
| 13126 | integral_constant_expression_p) |
| 13127 | |
| 13128 | tree op1; |
| 13129 | |
| 13130 | if (t == NULL_TREE || t == error_mark_node) |
| 13131 | return t; |
| 13132 | |
| 13133 | switch (TREE_CODE (t)) |
| 13134 | { |
| 13135 | case USING_DECL: |
| 13136 | t = DECL_NAME (t); |
| 13137 | /* Fall through. */ |
| 13138 | case IDENTIFIER_NODE: |
| 13139 | { |
| 13140 | tree decl; |
| 13141 | cp_id_kind idk; |
| 13142 | bool non_integral_constant_expression_p; |
| 13143 | const char *error_msg; |
| 13144 | |
| 13145 | if (IDENTIFIER_TYPENAME_P (t)) |
| 13146 | { |
| 13147 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 13148 | t = mangle_conv_op_name_for_type (new_type); |
| 13149 | } |
| 13150 | |
| 13151 | /* Look up the name. */ |
| 13152 | decl = lookup_name (t); |
| 13153 | |
| 13154 | /* By convention, expressions use ERROR_MARK_NODE to indicate |
| 13155 | failure, not NULL_TREE. */ |
| 13156 | if (decl == NULL_TREE) |
| 13157 | decl = error_mark_node; |
| 13158 | |
| 13159 | decl = finish_id_expression (t, decl, NULL_TREE, |
| 13160 | &idk, |
| 13161 | integral_constant_expression_p, |
| 13162 | /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x), |
| 13163 | &non_integral_constant_expression_p, |
| 13164 | /*template_p=*/false, |
| 13165 | /*done=*/true, |
| 13166 | /*address_p=*/false, |
| 13167 | /*template_arg_p=*/false, |
| 13168 | &error_msg, |
| 13169 | input_location); |
| 13170 | if (error_msg) |
| 13171 | error (error_msg); |
| 13172 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) |
| 13173 | { |
| 13174 | if (complain & tf_error) |
| 13175 | unqualified_name_lookup_error (decl); |
| 13176 | decl = error_mark_node; |
| 13177 | } |
| 13178 | return decl; |
| 13179 | } |
| 13180 | |
| 13181 | case TEMPLATE_ID_EXPR: |
| 13182 | { |
| 13183 | tree object; |
| 13184 | tree templ = RECUR (TREE_OPERAND (t, 0)); |
| 13185 | tree targs = TREE_OPERAND (t, 1); |
| 13186 | |
| 13187 | if (targs) |
| 13188 | targs = tsubst_template_args (targs, args, complain, in_decl); |
| 13189 | |
| 13190 | if (TREE_CODE (templ) == COMPONENT_REF) |
| 13191 | { |
| 13192 | object = TREE_OPERAND (templ, 0); |
| 13193 | templ = TREE_OPERAND (templ, 1); |
| 13194 | } |
| 13195 | else |
| 13196 | object = NULL_TREE; |
| 13197 | templ = lookup_template_function (templ, targs); |
| 13198 | |
| 13199 | if (object) |
| 13200 | return build3 (COMPONENT_REF, TREE_TYPE (templ), |
| 13201 | object, templ, NULL_TREE); |
| 13202 | else |
| 13203 | return baselink_for_fns (templ); |
| 13204 | } |
| 13205 | |
| 13206 | case INDIRECT_REF: |
| 13207 | { |
| 13208 | tree r = RECUR (TREE_OPERAND (t, 0)); |
| 13209 | |
| 13210 | if (REFERENCE_REF_P (t)) |
| 13211 | { |
| 13212 | /* A type conversion to reference type will be enclosed in |
| 13213 | such an indirect ref, but the substitution of the cast |
| 13214 | will have also added such an indirect ref. */ |
| 13215 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) |
| 13216 | r = convert_from_reference (r); |
| 13217 | } |
| 13218 | else |
| 13219 | r = build_x_indirect_ref (r, RO_UNARY_STAR, complain); |
| 13220 | return r; |
| 13221 | } |
| 13222 | |
| 13223 | case NOP_EXPR: |
| 13224 | return build_nop |
| 13225 | (tsubst (TREE_TYPE (t), args, complain, in_decl), |
| 13226 | RECUR (TREE_OPERAND (t, 0))); |
| 13227 | |
| 13228 | case IMPLICIT_CONV_EXPR: |
| 13229 | { |
| 13230 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 13231 | tree expr = RECUR (TREE_OPERAND (t, 0)); |
| 13232 | int flags = LOOKUP_IMPLICIT; |
| 13233 | if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t)) |
| 13234 | flags = LOOKUP_NORMAL; |
| 13235 | return perform_implicit_conversion_flags (type, expr, complain, |
| 13236 | flags); |
| 13237 | } |
| 13238 | |
| 13239 | case CONVERT_EXPR: |
| 13240 | return build1 |
| 13241 | (CONVERT_EXPR, |
| 13242 | tsubst (TREE_TYPE (t), args, complain, in_decl), |
| 13243 | RECUR (TREE_OPERAND (t, 0))); |
| 13244 | |
| 13245 | case CAST_EXPR: |
| 13246 | case REINTERPRET_CAST_EXPR: |
| 13247 | case CONST_CAST_EXPR: |
| 13248 | case DYNAMIC_CAST_EXPR: |
| 13249 | case STATIC_CAST_EXPR: |
| 13250 | { |
| 13251 | tree type; |
| 13252 | tree op; |
| 13253 | |
| 13254 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 13255 | if (integral_constant_expression_p |
| 13256 | && !cast_valid_in_integral_constant_expression_p (type)) |
| 13257 | { |
| 13258 | if (complain & tf_error) |
| 13259 | error ("a cast to a type other than an integral or " |
| 13260 | "enumeration type cannot appear in a constant-expression"); |
| 13261 | return error_mark_node; |
| 13262 | } |
| 13263 | |
| 13264 | op = RECUR (TREE_OPERAND (t, 0)); |
| 13265 | |
| 13266 | switch (TREE_CODE (t)) |
| 13267 | { |
| 13268 | case CAST_EXPR: |
| 13269 | return build_functional_cast (type, op, complain); |
| 13270 | case REINTERPRET_CAST_EXPR: |
| 13271 | return build_reinterpret_cast (type, op, complain); |
| 13272 | case CONST_CAST_EXPR: |
| 13273 | return build_const_cast (type, op, complain); |
| 13274 | case DYNAMIC_CAST_EXPR: |
| 13275 | return build_dynamic_cast (type, op, complain); |
| 13276 | case STATIC_CAST_EXPR: |
| 13277 | return build_static_cast (type, op, complain); |
| 13278 | default: |
| 13279 | gcc_unreachable (); |
| 13280 | } |
| 13281 | } |
| 13282 | |
| 13283 | case POSTDECREMENT_EXPR: |
| 13284 | case POSTINCREMENT_EXPR: |
| 13285 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
| 13286 | args, complain, in_decl); |
| 13287 | return build_x_unary_op (TREE_CODE (t), op1, complain); |
| 13288 | |
| 13289 | case PREDECREMENT_EXPR: |
| 13290 | case PREINCREMENT_EXPR: |
| 13291 | case NEGATE_EXPR: |
| 13292 | case BIT_NOT_EXPR: |
| 13293 | case ABS_EXPR: |
| 13294 | case TRUTH_NOT_EXPR: |
| 13295 | case UNARY_PLUS_EXPR: /* Unary + */ |
| 13296 | case REALPART_EXPR: |
| 13297 | case IMAGPART_EXPR: |
| 13298 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), |
| 13299 | complain); |
| 13300 | |
| 13301 | case FIX_TRUNC_EXPR: |
| 13302 | return cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)), |
| 13303 | 0, complain); |
| 13304 | |
| 13305 | case ADDR_EXPR: |
| 13306 | op1 = TREE_OPERAND (t, 0); |
| 13307 | if (TREE_CODE (op1) == LABEL_DECL) |
| 13308 | return finish_label_address_expr (DECL_NAME (op1), |
| 13309 | EXPR_LOCATION (op1)); |
| 13310 | if (TREE_CODE (op1) == SCOPE_REF) |
| 13311 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
| 13312 | /*done=*/true, /*address_p=*/true); |
| 13313 | else |
| 13314 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
| 13315 | in_decl); |
| 13316 | return build_x_unary_op (ADDR_EXPR, op1, complain); |
| 13317 | |
| 13318 | case PLUS_EXPR: |
| 13319 | case MINUS_EXPR: |
| 13320 | case MULT_EXPR: |
| 13321 | case TRUNC_DIV_EXPR: |
| 13322 | case CEIL_DIV_EXPR: |
| 13323 | case FLOOR_DIV_EXPR: |
| 13324 | case ROUND_DIV_EXPR: |
| 13325 | case EXACT_DIV_EXPR: |
| 13326 | case BIT_AND_EXPR: |
| 13327 | case BIT_IOR_EXPR: |
| 13328 | case BIT_XOR_EXPR: |
| 13329 | case TRUNC_MOD_EXPR: |
| 13330 | case FLOOR_MOD_EXPR: |
| 13331 | case TRUTH_ANDIF_EXPR: |
| 13332 | case TRUTH_ORIF_EXPR: |
| 13333 | case TRUTH_AND_EXPR: |
| 13334 | case TRUTH_OR_EXPR: |
| 13335 | case RSHIFT_EXPR: |
| 13336 | case LSHIFT_EXPR: |
| 13337 | case RROTATE_EXPR: |
| 13338 | case LROTATE_EXPR: |
| 13339 | case EQ_EXPR: |
| 13340 | case NE_EXPR: |
| 13341 | case MAX_EXPR: |
| 13342 | case MIN_EXPR: |
| 13343 | case LE_EXPR: |
| 13344 | case GE_EXPR: |
| 13345 | case LT_EXPR: |
| 13346 | case GT_EXPR: |
| 13347 | case MEMBER_REF: |
| 13348 | case DOTSTAR_EXPR: |
| 13349 | { |
| 13350 | tree r = build_x_binary_op |
| 13351 | (TREE_CODE (t), |
| 13352 | RECUR (TREE_OPERAND (t, 0)), |
| 13353 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
| 13354 | ? ERROR_MARK |
| 13355 | : TREE_CODE (TREE_OPERAND (t, 0))), |
| 13356 | RECUR (TREE_OPERAND (t, 1)), |
| 13357 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
| 13358 | ? ERROR_MARK |
| 13359 | : TREE_CODE (TREE_OPERAND (t, 1))), |
| 13360 | /*overload=*/NULL, |
| 13361 | complain); |
| 13362 | if (EXPR_P (r) && TREE_NO_WARNING (t)) |
| 13363 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
| 13364 | return r; |
| 13365 | } |
| 13366 | |
| 13367 | case SCOPE_REF: |
| 13368 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
| 13369 | /*address_p=*/false); |
| 13370 | case ARRAY_REF: |
| 13371 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
| 13372 | args, complain, in_decl); |
| 13373 | return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain); |
| 13374 | |
| 13375 | case SIZEOF_EXPR: |
| 13376 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) |
| 13377 | return tsubst_copy (t, args, complain, in_decl); |
| 13378 | /* Fall through */ |
| 13379 | |
| 13380 | case ALIGNOF_EXPR: |
| 13381 | op1 = TREE_OPERAND (t, 0); |
| 13382 | if (!args) |
| 13383 | { |
| 13384 | /* When there are no ARGS, we are trying to evaluate a |
| 13385 | non-dependent expression from the parser. Trying to do |
| 13386 | the substitutions may not work. */ |
| 13387 | if (!TYPE_P (op1)) |
| 13388 | op1 = TREE_TYPE (op1); |
| 13389 | } |
| 13390 | else |
| 13391 | { |
| 13392 | ++cp_unevaluated_operand; |
| 13393 | ++c_inhibit_evaluation_warnings; |
| 13394 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
| 13395 | /*function_p=*/false, |
| 13396 | /*integral_constant_expression_p=*/false); |
| 13397 | --cp_unevaluated_operand; |
| 13398 | --c_inhibit_evaluation_warnings; |
| 13399 | } |
| 13400 | if (TYPE_P (op1)) |
| 13401 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), |
| 13402 | complain & tf_error); |
| 13403 | else |
| 13404 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t), |
| 13405 | complain & tf_error); |
| 13406 | |
| 13407 | case AT_ENCODE_EXPR: |
| 13408 | { |
| 13409 | op1 = TREE_OPERAND (t, 0); |
| 13410 | ++cp_unevaluated_operand; |
| 13411 | ++c_inhibit_evaluation_warnings; |
| 13412 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
| 13413 | /*function_p=*/false, |
| 13414 | /*integral_constant_expression_p=*/false); |
| 13415 | --cp_unevaluated_operand; |
| 13416 | --c_inhibit_evaluation_warnings; |
| 13417 | return objc_build_encode_expr (op1); |
| 13418 | } |
| 13419 | |
| 13420 | case NOEXCEPT_EXPR: |
| 13421 | op1 = TREE_OPERAND (t, 0); |
| 13422 | ++cp_unevaluated_operand; |
| 13423 | ++c_inhibit_evaluation_warnings; |
| 13424 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
| 13425 | /*function_p=*/false, |
| 13426 | /*integral_constant_expression_p=*/false); |
| 13427 | --cp_unevaluated_operand; |
| 13428 | --c_inhibit_evaluation_warnings; |
| 13429 | return finish_noexcept_expr (op1, complain); |
| 13430 | |
| 13431 | case MODOP_EXPR: |
| 13432 | { |
| 13433 | tree r = build_x_modify_expr |
| 13434 | (RECUR (TREE_OPERAND (t, 0)), |
| 13435 | TREE_CODE (TREE_OPERAND (t, 1)), |
| 13436 | RECUR (TREE_OPERAND (t, 2)), |
| 13437 | complain); |
| 13438 | /* TREE_NO_WARNING must be set if either the expression was |
| 13439 | parenthesized or it uses an operator such as >>= rather |
| 13440 | than plain assignment. In the former case, it was already |
| 13441 | set and must be copied. In the latter case, |
| 13442 | build_x_modify_expr sets it and it must not be reset |
| 13443 | here. */ |
| 13444 | if (TREE_NO_WARNING (t)) |
| 13445 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
| 13446 | return r; |
| 13447 | } |
| 13448 | |
| 13449 | case ARROW_EXPR: |
| 13450 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
| 13451 | args, complain, in_decl); |
| 13452 | /* Remember that there was a reference to this entity. */ |
| 13453 | if (DECL_P (op1)) |
| 13454 | mark_used (op1); |
| 13455 | return build_x_arrow (op1); |
| 13456 | |
| 13457 | case NEW_EXPR: |
| 13458 | { |
| 13459 | tree placement = RECUR (TREE_OPERAND (t, 0)); |
| 13460 | tree init = RECUR (TREE_OPERAND (t, 3)); |
| 13461 | VEC(tree,gc) *placement_vec; |
| 13462 | VEC(tree,gc) *init_vec; |
| 13463 | tree ret; |
| 13464 | |
| 13465 | if (placement == NULL_TREE) |
| 13466 | placement_vec = NULL; |
| 13467 | else |
| 13468 | { |
| 13469 | placement_vec = make_tree_vector (); |
| 13470 | for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) |
| 13471 | VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement)); |
| 13472 | } |
| 13473 | |
| 13474 | /* If there was an initializer in the original tree, but it |
| 13475 | instantiated to an empty list, then we should pass a |
| 13476 | non-NULL empty vector to tell build_new that it was an |
| 13477 | empty initializer() rather than no initializer. This can |
| 13478 | only happen when the initializer is a pack expansion whose |
| 13479 | parameter packs are of length zero. */ |
| 13480 | if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) |
| 13481 | init_vec = NULL; |
| 13482 | else |
| 13483 | { |
| 13484 | init_vec = make_tree_vector (); |
| 13485 | if (init == void_zero_node) |
| 13486 | gcc_assert (init_vec != NULL); |
| 13487 | else |
| 13488 | { |
| 13489 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) |
| 13490 | VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init)); |
| 13491 | } |
| 13492 | } |
| 13493 | |
| 13494 | ret = build_new (&placement_vec, |
| 13495 | tsubst (TREE_OPERAND (t, 1), args, complain, in_decl), |
| 13496 | RECUR (TREE_OPERAND (t, 2)), |
| 13497 | &init_vec, |
| 13498 | NEW_EXPR_USE_GLOBAL (t), |
| 13499 | complain); |
| 13500 | |
| 13501 | if (placement_vec != NULL) |
| 13502 | release_tree_vector (placement_vec); |
| 13503 | if (init_vec != NULL) |
| 13504 | release_tree_vector (init_vec); |
| 13505 | |
| 13506 | return ret; |
| 13507 | } |
| 13508 | |
| 13509 | case DELETE_EXPR: |
| 13510 | return delete_sanity |
| 13511 | (RECUR (TREE_OPERAND (t, 0)), |
| 13512 | RECUR (TREE_OPERAND (t, 1)), |
| 13513 | DELETE_EXPR_USE_VEC (t), |
| 13514 | DELETE_EXPR_USE_GLOBAL (t), |
| 13515 | complain); |
| 13516 | |
| 13517 | case COMPOUND_EXPR: |
| 13518 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
| 13519 | RECUR (TREE_OPERAND (t, 1)), |
| 13520 | complain); |
| 13521 | |
| 13522 | case CALL_EXPR: |
| 13523 | { |
| 13524 | tree function; |
| 13525 | VEC(tree,gc) *call_args; |
| 13526 | unsigned int nargs, i; |
| 13527 | bool qualified_p; |
| 13528 | bool koenig_p; |
| 13529 | tree ret; |
| 13530 | |
| 13531 | function = CALL_EXPR_FN (t); |
| 13532 | /* When we parsed the expression, we determined whether or |
| 13533 | not Koenig lookup should be performed. */ |
| 13534 | koenig_p = KOENIG_LOOKUP_P (t); |
| 13535 | if (TREE_CODE (function) == SCOPE_REF) |
| 13536 | { |
| 13537 | qualified_p = true; |
| 13538 | function = tsubst_qualified_id (function, args, complain, in_decl, |
| 13539 | /*done=*/false, |
| 13540 | /*address_p=*/false); |
| 13541 | } |
| 13542 | else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE) |
| 13543 | { |
| 13544 | /* Do nothing; calling tsubst_copy_and_build on an identifier |
| 13545 | would incorrectly perform unqualified lookup again. |
| 13546 | |
| 13547 | Note that we can also have an IDENTIFIER_NODE if the earlier |
| 13548 | unqualified lookup found a member function; in that case |
| 13549 | koenig_p will be false and we do want to do the lookup |
| 13550 | again to find the instantiated member function. |
| 13551 | |
| 13552 | FIXME but doing that causes c++/15272, so we need to stop |
| 13553 | using IDENTIFIER_NODE in that situation. */ |
| 13554 | qualified_p = false; |
| 13555 | } |
| 13556 | else |
| 13557 | { |
| 13558 | if (TREE_CODE (function) == COMPONENT_REF) |
| 13559 | { |
| 13560 | tree op = TREE_OPERAND (function, 1); |
| 13561 | |
| 13562 | qualified_p = (TREE_CODE (op) == SCOPE_REF |
| 13563 | || (BASELINK_P (op) |
| 13564 | && BASELINK_QUALIFIED_P (op))); |
| 13565 | } |
| 13566 | else |
| 13567 | qualified_p = false; |
| 13568 | |
| 13569 | function = tsubst_copy_and_build (function, args, complain, |
| 13570 | in_decl, |
| 13571 | !qualified_p, |
| 13572 | integral_constant_expression_p); |
| 13573 | |
| 13574 | if (BASELINK_P (function)) |
| 13575 | qualified_p = true; |
| 13576 | } |
| 13577 | |
| 13578 | nargs = call_expr_nargs (t); |
| 13579 | call_args = make_tree_vector (); |
| 13580 | for (i = 0; i < nargs; ++i) |
| 13581 | { |
| 13582 | tree arg = CALL_EXPR_ARG (t, i); |
| 13583 | |
| 13584 | if (!PACK_EXPANSION_P (arg)) |
| 13585 | VEC_safe_push (tree, gc, call_args, |
| 13586 | RECUR (CALL_EXPR_ARG (t, i))); |
| 13587 | else |
| 13588 | { |
| 13589 | /* Expand the pack expansion and push each entry onto |
| 13590 | CALL_ARGS. */ |
| 13591 | arg = tsubst_pack_expansion (arg, args, complain, in_decl); |
| 13592 | if (TREE_CODE (arg) == TREE_VEC) |
| 13593 | { |
| 13594 | unsigned int len, j; |
| 13595 | |
| 13596 | len = TREE_VEC_LENGTH (arg); |
| 13597 | for (j = 0; j < len; ++j) |
| 13598 | { |
| 13599 | tree value = TREE_VEC_ELT (arg, j); |
| 13600 | if (value != NULL_TREE) |
| 13601 | value = convert_from_reference (value); |
| 13602 | VEC_safe_push (tree, gc, call_args, value); |
| 13603 | } |
| 13604 | } |
| 13605 | else |
| 13606 | { |
| 13607 | /* A partial substitution. Add one entry. */ |
| 13608 | VEC_safe_push (tree, gc, call_args, arg); |
| 13609 | } |
| 13610 | } |
| 13611 | } |
| 13612 | |
| 13613 | /* We do not perform argument-dependent lookup if normal |
| 13614 | lookup finds a non-function, in accordance with the |
| 13615 | expected resolution of DR 218. */ |
| 13616 | if (koenig_p |
| 13617 | && ((is_overloaded_fn (function) |
| 13618 | /* If lookup found a member function, the Koenig lookup is |
| 13619 | not appropriate, even if an unqualified-name was used |
| 13620 | to denote the function. */ |
| 13621 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) |
| 13622 | || TREE_CODE (function) == IDENTIFIER_NODE) |
| 13623 | /* Only do this when substitution turns a dependent call |
| 13624 | into a non-dependent call. */ |
| 13625 | && type_dependent_expression_p_push (t) |
| 13626 | && !any_type_dependent_arguments_p (call_args)) |
| 13627 | function = perform_koenig_lookup (function, call_args, false, |
| 13628 | tf_none); |
| 13629 | |
| 13630 | if (TREE_CODE (function) == IDENTIFIER_NODE |
| 13631 | && !any_type_dependent_arguments_p (call_args)) |
| 13632 | { |
| 13633 | if (koenig_p && (complain & tf_warning_or_error)) |
| 13634 | { |
| 13635 | /* For backwards compatibility and good diagnostics, try |
| 13636 | the unqualified lookup again if we aren't in SFINAE |
| 13637 | context. */ |
| 13638 | tree unq = (tsubst_copy_and_build |
| 13639 | (function, args, complain, in_decl, true, |
| 13640 | integral_constant_expression_p)); |
| 13641 | if (unq == error_mark_node) |
| 13642 | return error_mark_node; |
| 13643 | |
| 13644 | if (unq != function) |
| 13645 | { |
| 13646 | tree fn = unq; |
| 13647 | if (TREE_CODE (fn) == INDIRECT_REF) |
| 13648 | fn = TREE_OPERAND (fn, 0); |
| 13649 | if (TREE_CODE (fn) == COMPONENT_REF) |
| 13650 | fn = TREE_OPERAND (fn, 1); |
| 13651 | if (is_overloaded_fn (fn)) |
| 13652 | fn = get_first_fn (fn); |
| 13653 | permerror (EXPR_LOC_OR_HERE (t), |
| 13654 | "%qD was not declared in this scope, " |
| 13655 | "and no declarations were found by " |
| 13656 | "argument-dependent lookup at the point " |
| 13657 | "of instantiation", function); |
| 13658 | if (!DECL_P (fn)) |
| 13659 | /* Can't say anything more. */; |
| 13660 | else if (DECL_CLASS_SCOPE_P (fn)) |
| 13661 | { |
| 13662 | inform (EXPR_LOC_OR_HERE (t), |
| 13663 | "declarations in dependent base %qT are " |
| 13664 | "not found by unqualified lookup", |
| 13665 | DECL_CLASS_CONTEXT (fn)); |
| 13666 | if (current_class_ptr) |
| 13667 | inform (EXPR_LOC_OR_HERE (t), |
| 13668 | "use %<this->%D%> instead", function); |
| 13669 | else |
| 13670 | inform (EXPR_LOC_OR_HERE (t), |
| 13671 | "use %<%T::%D%> instead", |
| 13672 | current_class_name, function); |
| 13673 | } |
| 13674 | else |
| 13675 | inform (0, "%q+D declared here, later in the " |
| 13676 | "translation unit", fn); |
| 13677 | function = unq; |
| 13678 | } |
| 13679 | } |
| 13680 | if (TREE_CODE (function) == IDENTIFIER_NODE) |
| 13681 | { |
| 13682 | unqualified_name_lookup_error (function); |
| 13683 | release_tree_vector (call_args); |
| 13684 | return error_mark_node; |
| 13685 | } |
| 13686 | } |
| 13687 | |
| 13688 | /* Remember that there was a reference to this entity. */ |
| 13689 | if (DECL_P (function)) |
| 13690 | mark_used (function); |
| 13691 | |
| 13692 | if (TREE_CODE (function) == OFFSET_REF) |
| 13693 | ret = build_offset_ref_call_from_tree (function, &call_args); |
| 13694 | else if (TREE_CODE (function) == COMPONENT_REF) |
| 13695 | { |
| 13696 | tree instance = TREE_OPERAND (function, 0); |
| 13697 | tree fn = TREE_OPERAND (function, 1); |
| 13698 | |
| 13699 | if (processing_template_decl |
| 13700 | && (type_dependent_expression_p (instance) |
| 13701 | || (!BASELINK_P (fn) |
| 13702 | && TREE_CODE (fn) != FIELD_DECL) |
| 13703 | || type_dependent_expression_p (fn) |
| 13704 | || any_type_dependent_arguments_p (call_args))) |
| 13705 | ret = build_nt_call_vec (function, call_args); |
| 13706 | else if (!BASELINK_P (fn)) |
| 13707 | ret = finish_call_expr (function, &call_args, |
| 13708 | /*disallow_virtual=*/false, |
| 13709 | /*koenig_p=*/false, |
| 13710 | complain); |
| 13711 | else |
| 13712 | ret = (build_new_method_call |
| 13713 | (instance, fn, |
| 13714 | &call_args, NULL_TREE, |
| 13715 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
| 13716 | /*fn_p=*/NULL, |
| 13717 | complain)); |
| 13718 | } |
| 13719 | else |
| 13720 | ret = finish_call_expr (function, &call_args, |
| 13721 | /*disallow_virtual=*/qualified_p, |
| 13722 | koenig_p, |
| 13723 | complain); |
| 13724 | |
| 13725 | release_tree_vector (call_args); |
| 13726 | |
| 13727 | return ret; |
| 13728 | } |
| 13729 | |
| 13730 | case COND_EXPR: |
| 13731 | return build_x_conditional_expr |
| 13732 | (RECUR (TREE_OPERAND (t, 0)), |
| 13733 | RECUR (TREE_OPERAND (t, 1)), |
| 13734 | RECUR (TREE_OPERAND (t, 2)), |
| 13735 | complain); |
| 13736 | |
| 13737 | case PSEUDO_DTOR_EXPR: |
| 13738 | return finish_pseudo_destructor_expr |
| 13739 | (RECUR (TREE_OPERAND (t, 0)), |
| 13740 | RECUR (TREE_OPERAND (t, 1)), |
| 13741 | tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)); |
| 13742 | |
| 13743 | case TREE_LIST: |
| 13744 | { |
| 13745 | tree purpose, value, chain; |
| 13746 | |
| 13747 | if (t == void_list_node) |
| 13748 | return t; |
| 13749 | |
| 13750 | if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t))) |
| 13751 | || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))) |
| 13752 | { |
| 13753 | /* We have pack expansions, so expand those and |
| 13754 | create a new list out of it. */ |
| 13755 | tree purposevec = NULL_TREE; |
| 13756 | tree valuevec = NULL_TREE; |
| 13757 | tree chain; |
| 13758 | int i, len = -1; |
| 13759 | |
| 13760 | /* Expand the argument expressions. */ |
| 13761 | if (TREE_PURPOSE (t)) |
| 13762 | purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args, |
| 13763 | complain, in_decl); |
| 13764 | if (TREE_VALUE (t)) |
| 13765 | valuevec = tsubst_pack_expansion (TREE_VALUE (t), args, |
| 13766 | complain, in_decl); |
| 13767 | |
| 13768 | /* Build the rest of the list. */ |
| 13769 | chain = TREE_CHAIN (t); |
| 13770 | if (chain && chain != void_type_node) |
| 13771 | chain = RECUR (chain); |
| 13772 | |
| 13773 | /* Determine the number of arguments. */ |
| 13774 | if (purposevec && TREE_CODE (purposevec) == TREE_VEC) |
| 13775 | { |
| 13776 | len = TREE_VEC_LENGTH (purposevec); |
| 13777 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); |
| 13778 | } |
| 13779 | else if (TREE_CODE (valuevec) == TREE_VEC) |
| 13780 | len = TREE_VEC_LENGTH (valuevec); |
| 13781 | else |
| 13782 | { |
| 13783 | /* Since we only performed a partial substitution into |
| 13784 | the argument pack, we only return a single list |
| 13785 | node. */ |
| 13786 | if (purposevec == TREE_PURPOSE (t) |
| 13787 | && valuevec == TREE_VALUE (t) |
| 13788 | && chain == TREE_CHAIN (t)) |
| 13789 | return t; |
| 13790 | |
| 13791 | return tree_cons (purposevec, valuevec, chain); |
| 13792 | } |
| 13793 | |
| 13794 | /* Convert the argument vectors into a TREE_LIST */ |
| 13795 | i = len; |
| 13796 | while (i > 0) |
| 13797 | { |
| 13798 | /* Grab the Ith values. */ |
| 13799 | i--; |
| 13800 | purpose = purposevec ? TREE_VEC_ELT (purposevec, i) |
| 13801 | : NULL_TREE; |
| 13802 | value |
| 13803 | = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i)) |
| 13804 | : NULL_TREE; |
| 13805 | |
| 13806 | /* Build the list (backwards). */ |
| 13807 | chain = tree_cons (purpose, value, chain); |
| 13808 | } |
| 13809 | |
| 13810 | return chain; |
| 13811 | } |
| 13812 | |
| 13813 | purpose = TREE_PURPOSE (t); |
| 13814 | if (purpose) |
| 13815 | purpose = RECUR (purpose); |
| 13816 | value = TREE_VALUE (t); |
| 13817 | if (value) |
| 13818 | value = RECUR (value); |
| 13819 | chain = TREE_CHAIN (t); |
| 13820 | if (chain && chain != void_type_node) |
| 13821 | chain = RECUR (chain); |
| 13822 | if (purpose == TREE_PURPOSE (t) |
| 13823 | && value == TREE_VALUE (t) |
| 13824 | && chain == TREE_CHAIN (t)) |
| 13825 | return t; |
| 13826 | return tree_cons (purpose, value, chain); |
| 13827 | } |
| 13828 | |
| 13829 | case COMPONENT_REF: |
| 13830 | { |
| 13831 | tree object; |
| 13832 | tree object_type; |
| 13833 | tree member; |
| 13834 | |
| 13835 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
| 13836 | args, complain, in_decl); |
| 13837 | /* Remember that there was a reference to this entity. */ |
| 13838 | if (DECL_P (object)) |
| 13839 | mark_used (object); |
| 13840 | object_type = TREE_TYPE (object); |
| 13841 | |
| 13842 | member = TREE_OPERAND (t, 1); |
| 13843 | if (BASELINK_P (member)) |
| 13844 | member = tsubst_baselink (member, |
| 13845 | non_reference (TREE_TYPE (object)), |
| 13846 | args, complain, in_decl); |
| 13847 | else |
| 13848 | member = tsubst_copy (member, args, complain, in_decl); |
| 13849 | if (member == error_mark_node) |
| 13850 | return error_mark_node; |
| 13851 | |
| 13852 | if (type_dependent_expression_p (object)) |
| 13853 | /* We can't do much here. */; |
| 13854 | else if (!CLASS_TYPE_P (object_type)) |
| 13855 | { |
| 13856 | if (SCALAR_TYPE_P (object_type)) |
| 13857 | { |
| 13858 | tree s = NULL_TREE; |
| 13859 | tree dtor = member; |
| 13860 | |
| 13861 | if (TREE_CODE (dtor) == SCOPE_REF) |
| 13862 | { |
| 13863 | s = TREE_OPERAND (dtor, 0); |
| 13864 | dtor = TREE_OPERAND (dtor, 1); |
| 13865 | } |
| 13866 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) |
| 13867 | { |
| 13868 | dtor = TREE_OPERAND (dtor, 0); |
| 13869 | if (TYPE_P (dtor)) |
| 13870 | return finish_pseudo_destructor_expr (object, s, dtor); |
| 13871 | } |
| 13872 | } |
| 13873 | } |
| 13874 | else if (TREE_CODE (member) == SCOPE_REF |
| 13875 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) |
| 13876 | { |
| 13877 | /* Lookup the template functions now that we know what the |
| 13878 | scope is. */ |
| 13879 | tree scope = TREE_OPERAND (member, 0); |
| 13880 | tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); |
| 13881 | tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); |
| 13882 | member = lookup_qualified_name (scope, tmpl, |
| 13883 | /*is_type_p=*/false, |
| 13884 | /*complain=*/false); |
| 13885 | if (BASELINK_P (member)) |
| 13886 | { |
| 13887 | BASELINK_FUNCTIONS (member) |
| 13888 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
| 13889 | args); |
| 13890 | member = (adjust_result_of_qualified_name_lookup |
| 13891 | (member, BINFO_TYPE (BASELINK_BINFO (member)), |
| 13892 | object_type)); |
| 13893 | } |
| 13894 | else |
| 13895 | { |
| 13896 | qualified_name_lookup_error (scope, tmpl, member, |
| 13897 | input_location); |
| 13898 | return error_mark_node; |
| 13899 | } |
| 13900 | } |
| 13901 | else if (TREE_CODE (member) == SCOPE_REF |
| 13902 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) |
| 13903 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) |
| 13904 | { |
| 13905 | if (complain & tf_error) |
| 13906 | { |
| 13907 | if (TYPE_P (TREE_OPERAND (member, 0))) |
| 13908 | error ("%qT is not a class or namespace", |
| 13909 | TREE_OPERAND (member, 0)); |
| 13910 | else |
| 13911 | error ("%qD is not a class or namespace", |
| 13912 | TREE_OPERAND (member, 0)); |
| 13913 | } |
| 13914 | return error_mark_node; |
| 13915 | } |
| 13916 | else if (TREE_CODE (member) == FIELD_DECL) |
| 13917 | return finish_non_static_data_member (member, object, NULL_TREE); |
| 13918 | |
| 13919 | return finish_class_member_access_expr (object, member, |
| 13920 | /*template_p=*/false, |
| 13921 | complain); |
| 13922 | } |
| 13923 | |
| 13924 | case THROW_EXPR: |
| 13925 | return build_throw |
| 13926 | (RECUR (TREE_OPERAND (t, 0))); |
| 13927 | |
| 13928 | case CONSTRUCTOR: |
| 13929 | { |
| 13930 | VEC(constructor_elt,gc) *n; |
| 13931 | constructor_elt *ce; |
| 13932 | unsigned HOST_WIDE_INT idx; |
| 13933 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
| 13934 | bool process_index_p; |
| 13935 | int newlen; |
| 13936 | bool need_copy_p = false; |
| 13937 | tree r; |
| 13938 | |
| 13939 | if (type == error_mark_node) |
| 13940 | return error_mark_node; |
| 13941 | |
| 13942 | /* digest_init will do the wrong thing if we let it. */ |
| 13943 | if (type && TYPE_PTRMEMFUNC_P (type)) |
| 13944 | return t; |
| 13945 | |
| 13946 | /* We do not want to process the index of aggregate |
| 13947 | initializers as they are identifier nodes which will be |
| 13948 | looked up by digest_init. */ |
| 13949 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
| 13950 | |
| 13951 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
| 13952 | newlen = VEC_length (constructor_elt, n); |
| 13953 | FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce) |
| 13954 | { |
| 13955 | if (ce->index && process_index_p) |
| 13956 | ce->index = RECUR (ce->index); |
| 13957 | |
| 13958 | if (PACK_EXPANSION_P (ce->value)) |
| 13959 | { |
| 13960 | /* Substitute into the pack expansion. */ |
| 13961 | ce->value = tsubst_pack_expansion (ce->value, args, complain, |
| 13962 | in_decl); |
| 13963 | |
| 13964 | if (ce->value == error_mark_node |
| 13965 | || PACK_EXPANSION_P (ce->value)) |
| 13966 | ; |
| 13967 | else if (TREE_VEC_LENGTH (ce->value) == 1) |
| 13968 | /* Just move the argument into place. */ |
| 13969 | ce->value = TREE_VEC_ELT (ce->value, 0); |
| 13970 | else |
| 13971 | { |
| 13972 | /* Update the length of the final CONSTRUCTOR |
| 13973 | arguments vector, and note that we will need to |
| 13974 | copy.*/ |
| 13975 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; |
| 13976 | need_copy_p = true; |
| 13977 | } |
| 13978 | } |
| 13979 | else |
| 13980 | ce->value = RECUR (ce->value); |
| 13981 | } |
| 13982 | |
| 13983 | if (need_copy_p) |
| 13984 | { |
| 13985 | VEC(constructor_elt,gc) *old_n = n; |
| 13986 | |
| 13987 | n = VEC_alloc (constructor_elt, gc, newlen); |
| 13988 | FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce) |
| 13989 | { |
| 13990 | if (TREE_CODE (ce->value) == TREE_VEC) |
| 13991 | { |
| 13992 | int i, len = TREE_VEC_LENGTH (ce->value); |
| 13993 | for (i = 0; i < len; ++i) |
| 13994 | CONSTRUCTOR_APPEND_ELT (n, 0, |
| 13995 | TREE_VEC_ELT (ce->value, i)); |
| 13996 | } |
| 13997 | else |
| 13998 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); |
| 13999 | } |
| 14000 | } |
| 14001 | |
| 14002 | r = build_constructor (init_list_type_node, n); |
| 14003 | CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); |
| 14004 | |
| 14005 | if (TREE_HAS_CONSTRUCTOR (t)) |
| 14006 | return finish_compound_literal (type, r, complain); |
| 14007 | |
| 14008 | TREE_TYPE (r) = type; |
| 14009 | return r; |
| 14010 | } |
| 14011 | |
| 14012 | case TYPEID_EXPR: |
| 14013 | { |
| 14014 | tree operand_0 = TREE_OPERAND (t, 0); |
| 14015 | if (TYPE_P (operand_0)) |
| 14016 | { |
| 14017 | operand_0 = tsubst (operand_0, args, complain, in_decl); |
| 14018 | return get_typeid (operand_0); |
| 14019 | } |
| 14020 | else |
| 14021 | { |
| 14022 | operand_0 = RECUR (operand_0); |
| 14023 | return build_typeid (operand_0); |
| 14024 | } |
| 14025 | } |
| 14026 | |
| 14027 | case VAR_DECL: |
| 14028 | if (!args) |
| 14029 | return t; |
| 14030 | /* Fall through */ |
| 14031 | |
| 14032 | case PARM_DECL: |
| 14033 | { |
| 14034 | tree r = tsubst_copy (t, args, complain, in_decl); |
| 14035 | |
| 14036 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
| 14037 | /* If the original type was a reference, we'll be wrapped in |
| 14038 | the appropriate INDIRECT_REF. */ |
| 14039 | r = convert_from_reference (r); |
| 14040 | return r; |
| 14041 | } |
| 14042 | |
| 14043 | case VA_ARG_EXPR: |
| 14044 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
| 14045 | tsubst (TREE_TYPE (t), args, complain, in_decl)); |
| 14046 | |
| 14047 | case OFFSETOF_EXPR: |
| 14048 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
| 14049 | |
| 14050 | case TRAIT_EXPR: |
| 14051 | { |
| 14052 | tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args, |
| 14053 | complain, in_decl); |
| 14054 | |
| 14055 | tree type2 = TRAIT_EXPR_TYPE2 (t); |
| 14056 | if (type2) |
| 14057 | type2 = tsubst_copy (type2, args, complain, in_decl); |
| 14058 | |
| 14059 | return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2); |
| 14060 | } |
| 14061 | |
| 14062 | case STMT_EXPR: |
| 14063 | { |
| 14064 | tree old_stmt_expr = cur_stmt_expr; |
| 14065 | tree stmt_expr = begin_stmt_expr (); |
| 14066 | |
| 14067 | cur_stmt_expr = stmt_expr; |
| 14068 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
| 14069 | integral_constant_expression_p); |
| 14070 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
| 14071 | cur_stmt_expr = old_stmt_expr; |
| 14072 | |
| 14073 | /* If the resulting list of expression statement is empty, |
| 14074 | fold it further into void_zero_node. */ |
| 14075 | if (empty_expr_stmt_p (stmt_expr)) |
| 14076 | stmt_expr = void_zero_node; |
| 14077 | |
| 14078 | return stmt_expr; |
| 14079 | } |
| 14080 | |
| 14081 | case CONST_DECL: |
| 14082 | t = tsubst_copy (t, args, complain, in_decl); |
| 14083 | /* As in finish_id_expression, we resolve enumeration constants |
| 14084 | to their underlying values. */ |
| 14085 | if (TREE_CODE (t) == CONST_DECL && !processing_template_decl) |
| 14086 | { |
| 14087 | used_types_insert (TREE_TYPE (t)); |
| 14088 | return DECL_INITIAL (t); |
| 14089 | } |
| 14090 | return t; |
| 14091 | |
| 14092 | case LAMBDA_EXPR: |
| 14093 | { |
| 14094 | tree r = build_lambda_expr (); |
| 14095 | |
| 14096 | tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE); |
| 14097 | LAMBDA_EXPR_CLOSURE (r) = type; |
| 14098 | CLASSTYPE_LAMBDA_EXPR (type) = r; |
| 14099 | |
| 14100 | LAMBDA_EXPR_LOCATION (r) |
| 14101 | = LAMBDA_EXPR_LOCATION (t); |
| 14102 | LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) |
| 14103 | = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t); |
| 14104 | LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t); |
| 14105 | LAMBDA_EXPR_DISCRIMINATOR (r) |
| 14106 | = (LAMBDA_EXPR_DISCRIMINATOR (t)); |
| 14107 | LAMBDA_EXPR_EXTRA_SCOPE (r) |
| 14108 | = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t)); |
| 14109 | if (LAMBDA_EXPR_RETURN_TYPE (t) == dependent_lambda_return_type_node) |
| 14110 | { |
| 14111 | LAMBDA_EXPR_RETURN_TYPE (r) = dependent_lambda_return_type_node; |
| 14112 | LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (r) = true; |
| 14113 | } |
| 14114 | else |
| 14115 | LAMBDA_EXPR_RETURN_TYPE (r) |
| 14116 | = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl); |
| 14117 | |
| 14118 | gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE |
| 14119 | && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL); |
| 14120 | |
| 14121 | /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ |
| 14122 | determine_visibility (TYPE_NAME (type)); |
| 14123 | /* Now that we know visibility, instantiate the type so we have a |
| 14124 | declaration of the op() for later calls to lambda_function. */ |
| 14125 | complete_type (type); |
| 14126 | |
| 14127 | /* The capture list refers to closure members, so this needs to |
| 14128 | wait until after we finish instantiating the type. Also keep |
| 14129 | any captures that may have been added during instantiation. */ |
| 14130 | LAMBDA_EXPR_CAPTURE_LIST (r) |
| 14131 | = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)), |
| 14132 | LAMBDA_EXPR_CAPTURE_LIST (r)); |
| 14133 | LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE; |
| 14134 | |
| 14135 | return build_lambda_object (r); |
| 14136 | } |
| 14137 | |
| 14138 | case TARGET_EXPR: |
| 14139 | /* We can get here for a constant initializer of non-dependent type. |
| 14140 | FIXME stop folding in cp_parser_initializer_clause. */ |
| 14141 | { |
| 14142 | tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t))); |
| 14143 | return r; |
| 14144 | } |
| 14145 | |
| 14146 | case TRANSACTION_EXPR: |
| 14147 | return tsubst_expr(t, args, complain, in_decl, |
| 14148 | integral_constant_expression_p); |
| 14149 | |
| 14150 | default: |
| 14151 | /* Handle Objective-C++ constructs, if appropriate. */ |
| 14152 | { |
| 14153 | tree subst |
| 14154 | = objcp_tsubst_copy_and_build (t, args, complain, |
| 14155 | in_decl, /*function_p=*/false); |
| 14156 | if (subst) |
| 14157 | return subst; |
| 14158 | } |
| 14159 | return tsubst_copy (t, args, complain, in_decl); |
| 14160 | } |
| 14161 | |
| 14162 | #undef RECUR |
| 14163 | } |
| 14164 | |
| 14165 | /* Verify that the instantiated ARGS are valid. For type arguments, |
| 14166 | make sure that the type's linkage is ok. For non-type arguments, |
| 14167 | make sure they are constants if they are integral or enumerations. |
| 14168 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
| 14169 | |
| 14170 | static bool |
| 14171 | check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) |
| 14172 | { |
| 14173 | if (ARGUMENT_PACK_P (t)) |
| 14174 | { |
| 14175 | tree vec = ARGUMENT_PACK_ARGS (t); |
| 14176 | int len = TREE_VEC_LENGTH (vec); |
| 14177 | bool result = false; |
| 14178 | int i; |
| 14179 | |
| 14180 | for (i = 0; i < len; ++i) |
| 14181 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) |
| 14182 | result = true; |
| 14183 | return result; |
| 14184 | } |
| 14185 | else if (TYPE_P (t)) |
| 14186 | { |
| 14187 | /* [basic.link]: A name with no linkage (notably, the name |
| 14188 | of a class or enumeration declared in a local scope) |
| 14189 | shall not be used to declare an entity with linkage. |
| 14190 | This implies that names with no linkage cannot be used as |
| 14191 | template arguments |
| 14192 | |
| 14193 | DR 757 relaxes this restriction for C++0x. */ |
| 14194 | tree nt = (cxx_dialect > cxx98 ? NULL_TREE |
| 14195 | : no_linkage_check (t, /*relaxed_p=*/false)); |
| 14196 | |
| 14197 | if (nt) |
| 14198 | { |
| 14199 | /* DR 488 makes use of a type with no linkage cause |
| 14200 | type deduction to fail. */ |
| 14201 | if (complain & tf_error) |
| 14202 | { |
| 14203 | if (TYPE_ANONYMOUS_P (nt)) |
| 14204 | error ("%qT is/uses anonymous type", t); |
| 14205 | else |
| 14206 | error ("template argument for %qD uses local type %qT", |
| 14207 | tmpl, t); |
| 14208 | } |
| 14209 | return true; |
| 14210 | } |
| 14211 | /* In order to avoid all sorts of complications, we do not |
| 14212 | allow variably-modified types as template arguments. */ |
| 14213 | else if (variably_modified_type_p (t, NULL_TREE)) |
| 14214 | { |
| 14215 | if (complain & tf_error) |
| 14216 | error ("%qT is a variably modified type", t); |
| 14217 | return true; |
| 14218 | } |
| 14219 | } |
| 14220 | /* A non-type argument of integral or enumerated type must be a |
| 14221 | constant. */ |
| 14222 | else if (TREE_TYPE (t) |
| 14223 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) |
| 14224 | && !TREE_CONSTANT (t)) |
| 14225 | { |
| 14226 | if (complain & tf_error) |
| 14227 | error ("integral expression %qE is not constant", t); |
| 14228 | return true; |
| 14229 | } |
| 14230 | return false; |
| 14231 | } |
| 14232 | |
| 14233 | static bool |
| 14234 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
| 14235 | { |
| 14236 | int ix, len = DECL_NTPARMS (tmpl); |
| 14237 | bool result = false; |
| 14238 | |
| 14239 | for (ix = 0; ix != len; ix++) |
| 14240 | { |
| 14241 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) |
| 14242 | result = true; |
| 14243 | } |
| 14244 | if (result && (complain & tf_error)) |
| 14245 | error (" trying to instantiate %qD", tmpl); |
| 14246 | return result; |
| 14247 | } |
| 14248 | |
| 14249 | /* In C++0x, it's possible to have a function template whose type depends |
| 14250 | on itself recursively. This is most obvious with decltype, but can also |
| 14251 | occur with enumeration scope (c++/48969). So we need to catch infinite |
| 14252 | recursion and reject the substitution at deduction time; this function |
| 14253 | will return error_mark_node for any repeated substitution. |
| 14254 | |
| 14255 | This also catches excessive recursion such as when f<N> depends on |
| 14256 | f<N-1> across all integers, and returns error_mark_node for all the |
| 14257 | substitutions back up to the initial one. |
| 14258 | |
| 14259 | This is, of course, not reentrant. */ |
| 14260 | |
| 14261 | static tree |
| 14262 | deduction_tsubst_fntype (tree fn, tree targs, tsubst_flags_t complain) |
| 14263 | { |
| 14264 | static bool excessive_deduction_depth; |
| 14265 | static int deduction_depth; |
| 14266 | struct pending_template *old_last_pend = last_pending_template; |
| 14267 | struct tinst_level *old_error_tinst = last_error_tinst_level; |
| 14268 | |
| 14269 | tree fntype = TREE_TYPE (fn); |
| 14270 | tree tinst; |
| 14271 | tree r; |
| 14272 | |
| 14273 | if (excessive_deduction_depth) |
| 14274 | return error_mark_node; |
| 14275 | |
| 14276 | tinst = build_tree_list (fn, targs); |
| 14277 | if (!push_tinst_level (tinst)) |
| 14278 | { |
| 14279 | excessive_deduction_depth = true; |
| 14280 | ggc_free (tinst); |
| 14281 | return error_mark_node; |
| 14282 | } |
| 14283 | |
| 14284 | input_location = DECL_SOURCE_LOCATION (fn); |
| 14285 | ++deduction_depth; |
| 14286 | push_deduction_access_scope (fn); |
| 14287 | r = tsubst (fntype, targs, complain, NULL_TREE); |
| 14288 | pop_deduction_access_scope (fn); |
| 14289 | --deduction_depth; |
| 14290 | |
| 14291 | if (excessive_deduction_depth) |
| 14292 | { |
| 14293 | r = error_mark_node; |
| 14294 | if (deduction_depth == 0) |
| 14295 | /* Reset once we're all the way out. */ |
| 14296 | excessive_deduction_depth = false; |
| 14297 | } |
| 14298 | |
| 14299 | pop_tinst_level (); |
| 14300 | /* We can't free this if a pending_template entry or last_error_tinst_level |
| 14301 | is pointing at it. */ |
| 14302 | if (last_pending_template == old_last_pend |
| 14303 | && last_error_tinst_level == old_error_tinst) |
| 14304 | ggc_free (tinst); |
| 14305 | return r; |
| 14306 | } |
| 14307 | |
| 14308 | /* Instantiate the indicated variable or function template TMPL with |
| 14309 | the template arguments in TARG_PTR. */ |
| 14310 | |
| 14311 | static tree |
| 14312 | instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain) |
| 14313 | { |
| 14314 | tree targ_ptr = orig_args; |
| 14315 | tree fndecl; |
| 14316 | tree gen_tmpl; |
| 14317 | tree spec; |
| 14318 | |
| 14319 | if (tmpl == error_mark_node) |
| 14320 | return error_mark_node; |
| 14321 | |
| 14322 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
| 14323 | |
| 14324 | /* If this function is a clone, handle it specially. */ |
| 14325 | if (DECL_CLONED_FUNCTION_P (tmpl)) |
| 14326 | { |
| 14327 | tree spec; |
| 14328 | tree clone; |
| 14329 | |
| 14330 | /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have |
| 14331 | DECL_CLONED_FUNCTION. */ |
| 14332 | spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl), |
| 14333 | targ_ptr, complain); |
| 14334 | if (spec == error_mark_node) |
| 14335 | return error_mark_node; |
| 14336 | |
| 14337 | /* Look for the clone. */ |
| 14338 | FOR_EACH_CLONE (clone, spec) |
| 14339 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
| 14340 | return clone; |
| 14341 | /* We should always have found the clone by now. */ |
| 14342 | gcc_unreachable (); |
| 14343 | return NULL_TREE; |
| 14344 | } |
| 14345 | |
| 14346 | /* Check to see if we already have this specialization. */ |
| 14347 | gen_tmpl = most_general_template (tmpl); |
| 14348 | if (tmpl != gen_tmpl) |
| 14349 | /* The TMPL is a partial instantiation. To get a full set of |
| 14350 | arguments we must add the arguments used to perform the |
| 14351 | partial instantiation. */ |
| 14352 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), |
| 14353 | targ_ptr); |
| 14354 | |
| 14355 | /* It would be nice to avoid hashing here and then again in tsubst_decl, |
| 14356 | but it doesn't seem to be on the hot path. */ |
| 14357 | spec = retrieve_specialization (gen_tmpl, targ_ptr, 0); |
| 14358 | |
| 14359 | gcc_assert (tmpl == gen_tmpl |
| 14360 | || ((fndecl = retrieve_specialization (tmpl, orig_args, 0)) |
| 14361 | == spec) |
| 14362 | || fndecl == NULL_TREE); |
| 14363 | |
| 14364 | if (spec != NULL_TREE) |
| 14365 | return spec; |
| 14366 | |
| 14367 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
| 14368 | complain)) |
| 14369 | return error_mark_node; |
| 14370 | |
| 14371 | /* We are building a FUNCTION_DECL, during which the access of its |
| 14372 | parameters and return types have to be checked. However this |
| 14373 | FUNCTION_DECL which is the desired context for access checking |
| 14374 | is not built yet. We solve this chicken-and-egg problem by |
| 14375 | deferring all checks until we have the FUNCTION_DECL. */ |
| 14376 | push_deferring_access_checks (dk_deferred); |
| 14377 | |
| 14378 | /* Instantiation of the function happens in the context of the function |
| 14379 | template, not the context of the overload resolution we're doing. */ |
| 14380 | push_to_top_level (); |
| 14381 | if (DECL_CLASS_SCOPE_P (gen_tmpl)) |
| 14382 | { |
| 14383 | tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr, |
| 14384 | complain, gen_tmpl); |
| 14385 | push_nested_class (ctx); |
| 14386 | } |
| 14387 | /* Substitute template parameters to obtain the specialization. */ |
| 14388 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
| 14389 | targ_ptr, complain, gen_tmpl); |
| 14390 | if (DECL_CLASS_SCOPE_P (gen_tmpl)) |
| 14391 | pop_nested_class (); |
| 14392 | pop_from_top_level (); |
| 14393 | |
| 14394 | if (fndecl == error_mark_node) |
| 14395 | return error_mark_node; |
| 14396 | |
| 14397 | /* Now we know the specialization, compute access previously |
| 14398 | deferred. */ |
| 14399 | push_access_scope (fndecl); |
| 14400 | |
| 14401 | /* Some typedefs referenced from within the template code need to be access |
| 14402 | checked at template instantiation time, i.e now. These types were |
| 14403 | added to the template at parsing time. Let's get those and perfom |
| 14404 | the acces checks then. */ |
| 14405 | perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr); |
| 14406 | perform_deferred_access_checks (); |
| 14407 | pop_access_scope (fndecl); |
| 14408 | pop_deferring_access_checks (); |
| 14409 | |
| 14410 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
| 14411 | template, not the most general template. */ |
| 14412 | DECL_TI_TEMPLATE (fndecl) = tmpl; |
| 14413 | |
| 14414 | /* If we've just instantiated the main entry point for a function, |
| 14415 | instantiate all the alternate entry points as well. We do this |
| 14416 | by cloning the instantiation of the main entry point, not by |
| 14417 | instantiating the template clones. */ |
| 14418 | if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl))) |
| 14419 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); |
| 14420 | |
| 14421 | return fndecl; |
| 14422 | } |
| 14423 | |
| 14424 | /* Wrapper for instantiate_template_1. */ |
| 14425 | |
| 14426 | tree |
| 14427 | instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) |
| 14428 | { |
| 14429 | tree ret; |
| 14430 | timevar_push (TV_TEMPLATE_INST); |
| 14431 | ret = instantiate_template_1 (tmpl, orig_args, complain); |
| 14432 | timevar_pop (TV_TEMPLATE_INST); |
| 14433 | return ret; |
| 14434 | } |
| 14435 | |
| 14436 | /* We're going to do deduction substitution on the type of TMPL, a function |
| 14437 | template. In C++11 mode, push into that access scope. In C++03 mode, |
| 14438 | disable access checking. */ |
| 14439 | |
| 14440 | static void |
| 14441 | push_deduction_access_scope (tree tmpl) |
| 14442 | { |
| 14443 | if (cxx_dialect >= cxx0x) |
| 14444 | { |
| 14445 | int ptd = processing_template_decl; |
| 14446 | push_access_scope (DECL_TEMPLATE_RESULT (tmpl)); |
| 14447 | /* Preserve processing_template_decl across push_to_top_level. */ |
| 14448 | if (ptd && !processing_template_decl) |
| 14449 | ++processing_template_decl; |
| 14450 | } |
| 14451 | else |
| 14452 | push_deferring_access_checks (dk_no_check); |
| 14453 | } |
| 14454 | |
| 14455 | /* And pop back out. */ |
| 14456 | |
| 14457 | static void |
| 14458 | pop_deduction_access_scope (tree tmpl) |
| 14459 | { |
| 14460 | if (cxx_dialect >= cxx0x) |
| 14461 | pop_access_scope (DECL_TEMPLATE_RESULT (tmpl)); |
| 14462 | else |
| 14463 | pop_deferring_access_checks (); |
| 14464 | } |
| 14465 | |
| 14466 | /* PARM is a template parameter pack for FN. Returns true iff |
| 14467 | PARM is used in a deducible way in the argument list of FN. */ |
| 14468 | |
| 14469 | static bool |
| 14470 | pack_deducible_p (tree parm, tree fn) |
| 14471 | { |
| 14472 | tree t = FUNCTION_FIRST_USER_PARMTYPE (fn); |
| 14473 | for (; t; t = TREE_CHAIN (t)) |
| 14474 | { |
| 14475 | tree type = TREE_VALUE (t); |
| 14476 | tree packs; |
| 14477 | if (!PACK_EXPANSION_P (type)) |
| 14478 | continue; |
| 14479 | for (packs = PACK_EXPANSION_PARAMETER_PACKS (type); |
| 14480 | packs; packs = TREE_CHAIN (packs)) |
| 14481 | if (TREE_VALUE (packs) == parm) |
| 14482 | { |
| 14483 | /* The template parameter pack is used in a function parameter |
| 14484 | pack. If this is the end of the parameter list, the |
| 14485 | template parameter pack is deducible. */ |
| 14486 | if (TREE_CHAIN (t) == void_list_node) |
| 14487 | return true; |
| 14488 | else |
| 14489 | /* Otherwise, not. Well, it could be deduced from |
| 14490 | a non-pack parameter, but doing so would end up with |
| 14491 | a deduction mismatch, so don't bother. */ |
| 14492 | return false; |
| 14493 | } |
| 14494 | } |
| 14495 | /* The template parameter pack isn't used in any function parameter |
| 14496 | packs, but it might be used deeper, e.g. tuple<Args...>. */ |
| 14497 | return true; |
| 14498 | } |
| 14499 | |
| 14500 | /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with |
| 14501 | NARGS elements of the arguments that are being used when calling |
| 14502 | it. TARGS is a vector into which the deduced template arguments |
| 14503 | are placed. |
| 14504 | |
| 14505 | Return zero for success, 2 for an incomplete match that doesn't resolve |
| 14506 | all the types, and 1 for complete failure. An error message will be |
| 14507 | printed only for an incomplete match. |
| 14508 | |
| 14509 | If FN is a conversion operator, or we are trying to produce a specific |
| 14510 | specialization, RETURN_TYPE is the return type desired. |
| 14511 | |
| 14512 | The EXPLICIT_TARGS are explicit template arguments provided via a |
| 14513 | template-id. |
| 14514 | |
| 14515 | The parameter STRICT is one of: |
| 14516 | |
| 14517 | DEDUCE_CALL: |
| 14518 | We are deducing arguments for a function call, as in |
| 14519 | [temp.deduct.call]. |
| 14520 | |
| 14521 | DEDUCE_CONV: |
| 14522 | We are deducing arguments for a conversion function, as in |
| 14523 | [temp.deduct.conv]. |
| 14524 | |
| 14525 | DEDUCE_EXACT: |
| 14526 | We are deducing arguments when doing an explicit instantiation |
| 14527 | as in [temp.explicit], when determining an explicit specialization |
| 14528 | as in [temp.expl.spec], or when taking the address of a function |
| 14529 | template, as in [temp.deduct.funcaddr]. */ |
| 14530 | |
| 14531 | int |
| 14532 | fn_type_unification (tree fn, |
| 14533 | tree explicit_targs, |
| 14534 | tree targs, |
| 14535 | const tree *args, |
| 14536 | unsigned int nargs, |
| 14537 | tree return_type, |
| 14538 | unification_kind_t strict, |
| 14539 | int flags, |
| 14540 | bool explain_p) |
| 14541 | { |
| 14542 | tree parms; |
| 14543 | tree fntype; |
| 14544 | int result; |
| 14545 | |
| 14546 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
| 14547 | |
| 14548 | fntype = TREE_TYPE (fn); |
| 14549 | if (explicit_targs) |
| 14550 | { |
| 14551 | /* [temp.deduct] |
| 14552 | |
| 14553 | The specified template arguments must match the template |
| 14554 | parameters in kind (i.e., type, nontype, template), and there |
| 14555 | must not be more arguments than there are parameters; |
| 14556 | otherwise type deduction fails. |
| 14557 | |
| 14558 | Nontype arguments must match the types of the corresponding |
| 14559 | nontype template parameters, or must be convertible to the |
| 14560 | types of the corresponding nontype parameters as specified in |
| 14561 | _temp.arg.nontype_, otherwise type deduction fails. |
| 14562 | |
| 14563 | All references in the function type of the function template |
| 14564 | to the corresponding template parameters are replaced by the |
| 14565 | specified template argument values. If a substitution in a |
| 14566 | template parameter or in the function type of the function |
| 14567 | template results in an invalid type, type deduction fails. */ |
| 14568 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
| 14569 | int i, len = TREE_VEC_LENGTH (tparms); |
| 14570 | tree converted_args; |
| 14571 | bool incomplete = false; |
| 14572 | |
| 14573 | if (explicit_targs == error_mark_node) |
| 14574 | return unify_invalid (explain_p); |
| 14575 | |
| 14576 | converted_args |
| 14577 | = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, |
| 14578 | (explain_p |
| 14579 | ? tf_warning_or_error |
| 14580 | : tf_none), |
| 14581 | /*require_all_args=*/false, |
| 14582 | /*use_default_args=*/false)); |
| 14583 | if (converted_args == error_mark_node) |
| 14584 | return 1; |
| 14585 | |
| 14586 | /* Substitute the explicit args into the function type. This is |
| 14587 | necessary so that, for instance, explicitly declared function |
| 14588 | arguments can match null pointed constants. If we were given |
| 14589 | an incomplete set of explicit args, we must not do semantic |
| 14590 | processing during substitution as we could create partial |
| 14591 | instantiations. */ |
| 14592 | for (i = 0; i < len; i++) |
| 14593 | { |
| 14594 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
| 14595 | bool parameter_pack = false; |
| 14596 | tree targ = TREE_VEC_ELT (converted_args, i); |
| 14597 | |
| 14598 | /* Dig out the actual parm. */ |
| 14599 | if (TREE_CODE (parm) == TYPE_DECL |
| 14600 | || TREE_CODE (parm) == TEMPLATE_DECL) |
| 14601 | { |
| 14602 | parm = TREE_TYPE (parm); |
| 14603 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); |
| 14604 | } |
| 14605 | else if (TREE_CODE (parm) == PARM_DECL) |
| 14606 | { |
| 14607 | parm = DECL_INITIAL (parm); |
| 14608 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); |
| 14609 | } |
| 14610 | |
| 14611 | if (!parameter_pack && targ == NULL_TREE) |
| 14612 | /* No explicit argument for this template parameter. */ |
| 14613 | incomplete = true; |
| 14614 | |
| 14615 | if (parameter_pack && pack_deducible_p (parm, fn)) |
| 14616 | { |
| 14617 | /* Mark the argument pack as "incomplete". We could |
| 14618 | still deduce more arguments during unification. |
| 14619 | We remove this mark in type_unification_real. */ |
| 14620 | if (targ) |
| 14621 | { |
| 14622 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; |
| 14623 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) |
| 14624 | = ARGUMENT_PACK_ARGS (targ); |
| 14625 | } |
| 14626 | |
| 14627 | /* We have some incomplete argument packs. */ |
| 14628 | incomplete = true; |
| 14629 | } |
| 14630 | } |
| 14631 | |
| 14632 | processing_template_decl += incomplete; |
| 14633 | fntype = deduction_tsubst_fntype (fn, converted_args, |
| 14634 | (explain_p |
| 14635 | ? tf_warning_or_error |
| 14636 | : tf_none)); |
| 14637 | processing_template_decl -= incomplete; |
| 14638 | |
| 14639 | if (fntype == error_mark_node) |
| 14640 | return 1; |
| 14641 | |
| 14642 | /* Place the explicitly specified arguments in TARGS. */ |
| 14643 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
| 14644 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
| 14645 | } |
| 14646 | |
| 14647 | /* Never do unification on the 'this' parameter. */ |
| 14648 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
| 14649 | |
| 14650 | if (return_type) |
| 14651 | { |
| 14652 | tree *new_args; |
| 14653 | |
| 14654 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
| 14655 | new_args = XALLOCAVEC (tree, nargs + 1); |
| 14656 | new_args[0] = return_type; |
| 14657 | memcpy (new_args + 1, args, nargs * sizeof (tree)); |
| 14658 | args = new_args; |
| 14659 | ++nargs; |
| 14660 | } |
| 14661 | |
| 14662 | /* We allow incomplete unification without an error message here |
| 14663 | because the standard doesn't seem to explicitly prohibit it. Our |
| 14664 | callers must be ready to deal with unification failures in any |
| 14665 | event. */ |
| 14666 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
| 14667 | targs, parms, args, nargs, /*subr=*/0, |
| 14668 | strict, flags, explain_p); |
| 14669 | |
| 14670 | /* Now that we have bindings for all of the template arguments, |
| 14671 | ensure that the arguments deduced for the template template |
| 14672 | parameters have compatible template parameter lists. We cannot |
| 14673 | check this property before we have deduced all template |
| 14674 | arguments, because the template parameter types of a template |
| 14675 | template parameter might depend on prior template parameters |
| 14676 | deduced after the template template parameter. The following |
| 14677 | ill-formed example illustrates this issue: |
| 14678 | |
| 14679 | template<typename T, template<T> class C> void f(C<5>, T); |
| 14680 | |
| 14681 | template<int N> struct X {}; |
| 14682 | |
| 14683 | void g() { |
| 14684 | f(X<5>(), 5l); // error: template argument deduction fails |
| 14685 | } |
| 14686 | |
| 14687 | The template parameter list of 'C' depends on the template type |
| 14688 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to |
| 14689 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the |
| 14690 | time that we deduce 'C'. */ |
| 14691 | if (result == 0 |
| 14692 | && !template_template_parm_bindings_ok_p |
| 14693 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) |
| 14694 | return unify_inconsistent_template_template_parameters (explain_p); |
| 14695 | |
| 14696 | if (result == 0) |
| 14697 | /* All is well so far. Now, check: |
| 14698 | |
| 14699 | [temp.deduct] |
| 14700 | |
| 14701 | When all template arguments have been deduced, all uses of |
| 14702 | template parameters in nondeduced contexts are replaced with |
| 14703 | the corresponding deduced argument values. If the |
| 14704 | substitution results in an invalid type, as described above, |
| 14705 | type deduction fails. */ |
| 14706 | { |
| 14707 | tree substed = deduction_tsubst_fntype (fn, targs, |
| 14708 | (explain_p |
| 14709 | ? tf_warning_or_error |
| 14710 | : tf_none)); |
| 14711 | if (substed == error_mark_node) |
| 14712 | return 1; |
| 14713 | |
| 14714 | /* If we're looking for an exact match, check that what we got |
| 14715 | is indeed an exact match. It might not be if some template |
| 14716 | parameters are used in non-deduced contexts. But don't check |
| 14717 | for an exact match if we have dependent template arguments; |
| 14718 | in that case we're doing partial ordering, and we already know |
| 14719 | that we have two candidates that will provide the actual type. */ |
| 14720 | if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs)) |
| 14721 | { |
| 14722 | unsigned int i; |
| 14723 | |
| 14724 | tree sarg |
| 14725 | = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed)); |
| 14726 | if (return_type) |
| 14727 | sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); |
| 14728 | for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) |
| 14729 | if (!same_type_p (args[i], TREE_VALUE (sarg))) |
| 14730 | return unify_type_mismatch (explain_p, args[i], |
| 14731 | TREE_VALUE (sarg)); |
| 14732 | } |
| 14733 | } |
| 14734 | |
| 14735 | return result; |
| 14736 | } |
| 14737 | |
| 14738 | /* Adjust types before performing type deduction, as described in |
| 14739 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two |
| 14740 | sections are symmetric. PARM is the type of a function parameter |
| 14741 | or the return type of the conversion function. ARG is the type of |
| 14742 | the argument passed to the call, or the type of the value |
| 14743 | initialized with the result of the conversion function. |
| 14744 | ARG_EXPR is the original argument expression, which may be null. */ |
| 14745 | |
| 14746 | static int |
| 14747 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
| 14748 | tree* parm, |
| 14749 | tree* arg, |
| 14750 | tree arg_expr) |
| 14751 | { |
| 14752 | int result = 0; |
| 14753 | |
| 14754 | switch (strict) |
| 14755 | { |
| 14756 | case DEDUCE_CALL: |
| 14757 | break; |
| 14758 | |
| 14759 | case DEDUCE_CONV: |
| 14760 | { |
| 14761 | /* Swap PARM and ARG throughout the remainder of this |
| 14762 | function; the handling is precisely symmetric since PARM |
| 14763 | will initialize ARG rather than vice versa. */ |
| 14764 | tree* temp = parm; |
| 14765 | parm = arg; |
| 14766 | arg = temp; |
| 14767 | break; |
| 14768 | } |
| 14769 | |
| 14770 | case DEDUCE_EXACT: |
| 14771 | /* Core issue #873: Do the DR606 thing (see below) for these cases, |
| 14772 | too, but here handle it by stripping the reference from PARM |
| 14773 | rather than by adding it to ARG. */ |
| 14774 | if (TREE_CODE (*parm) == REFERENCE_TYPE |
| 14775 | && TYPE_REF_IS_RVALUE (*parm) |
| 14776 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM |
| 14777 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED |
| 14778 | && TREE_CODE (*arg) == REFERENCE_TYPE |
| 14779 | && !TYPE_REF_IS_RVALUE (*arg)) |
| 14780 | *parm = TREE_TYPE (*parm); |
| 14781 | /* Nothing else to do in this case. */ |
| 14782 | return 0; |
| 14783 | |
| 14784 | default: |
| 14785 | gcc_unreachable (); |
| 14786 | } |
| 14787 | |
| 14788 | if (TREE_CODE (*parm) != REFERENCE_TYPE) |
| 14789 | { |
| 14790 | /* [temp.deduct.call] |
| 14791 | |
| 14792 | If P is not a reference type: |
| 14793 | |
| 14794 | --If A is an array type, the pointer type produced by the |
| 14795 | array-to-pointer standard conversion (_conv.array_) is |
| 14796 | used in place of A for type deduction; otherwise, |
| 14797 | |
| 14798 | --If A is a function type, the pointer type produced by |
| 14799 | the function-to-pointer standard conversion |
| 14800 | (_conv.func_) is used in place of A for type deduction; |
| 14801 | otherwise, |
| 14802 | |
| 14803 | --If A is a cv-qualified type, the top level |
| 14804 | cv-qualifiers of A's type are ignored for type |
| 14805 | deduction. */ |
| 14806 | if (TREE_CODE (*arg) == ARRAY_TYPE) |
| 14807 | *arg = build_pointer_type (TREE_TYPE (*arg)); |
| 14808 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
| 14809 | *arg = build_pointer_type (*arg); |
| 14810 | else |
| 14811 | *arg = TYPE_MAIN_VARIANT (*arg); |
| 14812 | } |
| 14813 | |
| 14814 | /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is |
| 14815 | of the form T&&, where T is a template parameter, and the argument |
| 14816 | is an lvalue, T is deduced as A& */ |
| 14817 | if (TREE_CODE (*parm) == REFERENCE_TYPE |
| 14818 | && TYPE_REF_IS_RVALUE (*parm) |
| 14819 | && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM |
| 14820 | && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED |
| 14821 | && (arg_expr ? real_lvalue_p (arg_expr) |
| 14822 | /* try_one_overload doesn't provide an arg_expr, but |
| 14823 | functions are always lvalues. */ |
| 14824 | : TREE_CODE (*arg) == FUNCTION_TYPE)) |
| 14825 | *arg = build_reference_type (*arg); |
| 14826 | |
| 14827 | /* [temp.deduct.call] |
| 14828 | |
| 14829 | If P is a cv-qualified type, the top level cv-qualifiers |
| 14830 | of P's type are ignored for type deduction. If P is a |
| 14831 | reference type, the type referred to by P is used for |
| 14832 | type deduction. */ |
| 14833 | *parm = TYPE_MAIN_VARIANT (*parm); |
| 14834 | if (TREE_CODE (*parm) == REFERENCE_TYPE) |
| 14835 | { |
| 14836 | *parm = TREE_TYPE (*parm); |
| 14837 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
| 14838 | } |
| 14839 | |
| 14840 | /* DR 322. For conversion deduction, remove a reference type on parm |
| 14841 | too (which has been swapped into ARG). */ |
| 14842 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) |
| 14843 | *arg = TREE_TYPE (*arg); |
| 14844 | |
| 14845 | return result; |
| 14846 | } |
| 14847 | |
| 14848 | /* Subroutine of unify_one_argument. PARM is a function parameter of a |
| 14849 | template which does contain any deducible template parameters; check if |
| 14850 | ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in |
| 14851 | unify_one_argument. */ |
| 14852 | |
| 14853 | static int |
| 14854 | check_non_deducible_conversion (tree parm, tree arg, int strict, |
| 14855 | int flags, bool explain_p) |
| 14856 | { |
| 14857 | tree type; |
| 14858 | |
| 14859 | if (!TYPE_P (arg)) |
| 14860 | type = TREE_TYPE (arg); |
| 14861 | else |
| 14862 | type = arg; |
| 14863 | |
| 14864 | if (same_type_p (parm, type)) |
| 14865 | return unify_success (explain_p); |
| 14866 | |
| 14867 | if (strict == DEDUCE_CONV) |
| 14868 | { |
| 14869 | if (can_convert_arg (type, parm, NULL_TREE, flags)) |
| 14870 | return unify_success (explain_p); |
| 14871 | } |
| 14872 | else if (strict != DEDUCE_EXACT) |
| 14873 | { |
| 14874 | if (can_convert_arg (parm, type, |
| 14875 | TYPE_P (arg) ? NULL_TREE : arg, |
| 14876 | flags)) |
| 14877 | return unify_success (explain_p); |
| 14878 | } |
| 14879 | |
| 14880 | if (strict == DEDUCE_EXACT) |
| 14881 | return unify_type_mismatch (explain_p, parm, arg); |
| 14882 | else |
| 14883 | return unify_arg_conversion (explain_p, parm, type, arg); |
| 14884 | } |
| 14885 | |
| 14886 | /* Subroutine of type_unification_real and unify_pack_expansion to |
| 14887 | handle unification of a single P/A pair. Parameters are as |
| 14888 | for those functions. */ |
| 14889 | |
| 14890 | static int |
| 14891 | unify_one_argument (tree tparms, tree targs, tree parm, tree arg, |
| 14892 | int subr, unification_kind_t strict, int flags, |
| 14893 | bool explain_p) |
| 14894 | { |
| 14895 | tree arg_expr = NULL_TREE; |
| 14896 | int arg_strict; |
| 14897 | |
| 14898 | if (arg == error_mark_node || parm == error_mark_node) |
| 14899 | return unify_invalid (explain_p); |
| 14900 | if (arg == unknown_type_node) |
| 14901 | /* We can't deduce anything from this, but we might get all the |
| 14902 | template args from other function args. */ |
| 14903 | return unify_success (explain_p); |
| 14904 | |
| 14905 | /* FIXME uses_deducible_template_parms */ |
| 14906 | if (TYPE_P (parm) && !uses_template_parms (parm)) |
| 14907 | return check_non_deducible_conversion (parm, arg, strict, flags, |
| 14908 | explain_p); |
| 14909 | |
| 14910 | switch (strict) |
| 14911 | { |
| 14912 | case DEDUCE_CALL: |
| 14913 | arg_strict = (UNIFY_ALLOW_OUTER_LEVEL |
| 14914 | | UNIFY_ALLOW_MORE_CV_QUAL |
| 14915 | | UNIFY_ALLOW_DERIVED); |
| 14916 | break; |
| 14917 | |
| 14918 | case DEDUCE_CONV: |
| 14919 | arg_strict = UNIFY_ALLOW_LESS_CV_QUAL; |
| 14920 | break; |
| 14921 | |
| 14922 | case DEDUCE_EXACT: |
| 14923 | arg_strict = UNIFY_ALLOW_NONE; |
| 14924 | break; |
| 14925 | |
| 14926 | default: |
| 14927 | gcc_unreachable (); |
| 14928 | } |
| 14929 | |
| 14930 | /* We only do these transformations if this is the top-level |
| 14931 | parameter_type_list in a call or declaration matching; in other |
| 14932 | situations (nested function declarators, template argument lists) we |
| 14933 | won't be comparing a type to an expression, and we don't do any type |
| 14934 | adjustments. */ |
| 14935 | if (!subr) |
| 14936 | { |
| 14937 | if (!TYPE_P (arg)) |
| 14938 | { |
| 14939 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
| 14940 | if (type_unknown_p (arg)) |
| 14941 | { |
| 14942 | /* [temp.deduct.type] A template-argument can be |
| 14943 | deduced from a pointer to function or pointer |
| 14944 | to member function argument if the set of |
| 14945 | overloaded functions does not contain function |
| 14946 | templates and at most one of a set of |
| 14947 | overloaded functions provides a unique |
| 14948 | match. */ |
| 14949 | |
| 14950 | if (resolve_overloaded_unification |
| 14951 | (tparms, targs, parm, arg, strict, |
| 14952 | arg_strict, explain_p)) |
| 14953 | return unify_success (explain_p); |
| 14954 | return unify_overload_resolution_failure (explain_p, arg); |
| 14955 | } |
| 14956 | |
| 14957 | arg_expr = arg; |
| 14958 | arg = unlowered_expr_type (arg); |
| 14959 | if (arg == error_mark_node) |
| 14960 | return unify_invalid (explain_p); |
| 14961 | } |
| 14962 | |
| 14963 | arg_strict |= |
| 14964 | maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr); |
| 14965 | } |
| 14966 | else |
| 14967 | gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL) |
| 14968 | == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)); |
| 14969 | |
| 14970 | /* For deduction from an init-list we need the actual list. */ |
| 14971 | if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr)) |
| 14972 | arg = arg_expr; |
| 14973 | return unify (tparms, targs, parm, arg, arg_strict, explain_p); |
| 14974 | } |
| 14975 | |
| 14976 | /* Most parms like fn_type_unification. |
| 14977 | |
| 14978 | If SUBR is 1, we're being called recursively (to unify the |
| 14979 | arguments of a function or method parameter of a function |
| 14980 | template). */ |
| 14981 | |
| 14982 | static int |
| 14983 | type_unification_real (tree tparms, |
| 14984 | tree targs, |
| 14985 | tree xparms, |
| 14986 | const tree *xargs, |
| 14987 | unsigned int xnargs, |
| 14988 | int subr, |
| 14989 | unification_kind_t strict, |
| 14990 | int flags, |
| 14991 | bool explain_p) |
| 14992 | { |
| 14993 | tree parm, arg; |
| 14994 | int i; |
| 14995 | int ntparms = TREE_VEC_LENGTH (tparms); |
| 14996 | int saw_undeduced = 0; |
| 14997 | tree parms; |
| 14998 | const tree *args; |
| 14999 | unsigned int nargs; |
| 15000 | unsigned int ia; |
| 15001 | |
| 15002 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
| 15003 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); |
| 15004 | gcc_assert (ntparms > 0); |
| 15005 | |
| 15006 | /* Reset the number of non-defaulted template arguments contained |
| 15007 | in TARGS. */ |
| 15008 | NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE; |
| 15009 | |
| 15010 | again: |
| 15011 | parms = xparms; |
| 15012 | args = xargs; |
| 15013 | nargs = xnargs; |
| 15014 | |
| 15015 | ia = 0; |
| 15016 | while (parms && parms != void_list_node |
| 15017 | && ia < nargs) |
| 15018 | { |
| 15019 | parm = TREE_VALUE (parms); |
| 15020 | |
| 15021 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION |
| 15022 | && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node)) |
| 15023 | /* For a function parameter pack that occurs at the end of the |
| 15024 | parameter-declaration-list, the type A of each remaining |
| 15025 | argument of the call is compared with the type P of the |
| 15026 | declarator-id of the function parameter pack. */ |
| 15027 | break; |
| 15028 | |
| 15029 | parms = TREE_CHAIN (parms); |
| 15030 | |
| 15031 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION) |
| 15032 | /* For a function parameter pack that does not occur at the |
| 15033 | end of the parameter-declaration-list, the type of the |
| 15034 | parameter pack is a non-deduced context. */ |
| 15035 | continue; |
| 15036 | |
| 15037 | arg = args[ia]; |
| 15038 | ++ia; |
| 15039 | |
| 15040 | if (unify_one_argument (tparms, targs, parm, arg, subr, strict, |
| 15041 | flags, explain_p)) |
| 15042 | return 1; |
| 15043 | } |
| 15044 | |
| 15045 | if (parms |
| 15046 | && parms != void_list_node |
| 15047 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
| 15048 | { |
| 15049 | /* Unify the remaining arguments with the pack expansion type. */ |
| 15050 | tree argvec; |
| 15051 | tree parmvec = make_tree_vec (1); |
| 15052 | |
| 15053 | /* Allocate a TREE_VEC and copy in all of the arguments */ |
| 15054 | argvec = make_tree_vec (nargs - ia); |
| 15055 | for (i = 0; ia < nargs; ++ia, ++i) |
| 15056 | TREE_VEC_ELT (argvec, i) = args[ia]; |
| 15057 | |
| 15058 | /* Copy the parameter into parmvec. */ |
| 15059 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); |
| 15060 | if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict, |
| 15061 | /*subr=*/subr, explain_p)) |
| 15062 | return 1; |
| 15063 | |
| 15064 | /* Advance to the end of the list of parameters. */ |
| 15065 | parms = TREE_CHAIN (parms); |
| 15066 | } |
| 15067 | |
| 15068 | /* Fail if we've reached the end of the parm list, and more args |
| 15069 | are present, and the parm list isn't variadic. */ |
| 15070 | if (ia < nargs && parms == void_list_node) |
| 15071 | return unify_too_many_arguments (explain_p, nargs, ia); |
| 15072 | /* Fail if parms are left and they don't have default values. */ |
| 15073 | if (parms && parms != void_list_node |
| 15074 | && TREE_PURPOSE (parms) == NULL_TREE) |
| 15075 | { |
| 15076 | unsigned int count = nargs; |
| 15077 | tree p = parms; |
| 15078 | while (p && p != void_list_node) |
| 15079 | { |
| 15080 | count++; |
| 15081 | p = TREE_CHAIN (p); |
| 15082 | } |
| 15083 | return unify_too_few_arguments (explain_p, ia, count); |
| 15084 | } |
| 15085 | |
| 15086 | if (!subr) |
| 15087 | { |
| 15088 | tsubst_flags_t complain = (explain_p |
| 15089 | ? tf_warning_or_error |
| 15090 | : tf_none); |
| 15091 | |
| 15092 | for (i = 0; i < ntparms; i++) |
| 15093 | { |
| 15094 | tree targ = TREE_VEC_ELT (targs, i); |
| 15095 | tree tparm = TREE_VEC_ELT (tparms, i); |
| 15096 | |
| 15097 | /* Clear the "incomplete" flags on all argument packs now so that |
| 15098 | substituting them into later default arguments works. */ |
| 15099 | if (targ && ARGUMENT_PACK_P (targ)) |
| 15100 | { |
| 15101 | ARGUMENT_PACK_INCOMPLETE_P (targ) = 0; |
| 15102 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE; |
| 15103 | } |
| 15104 | |
| 15105 | if (targ || tparm == error_mark_node) |
| 15106 | continue; |
| 15107 | tparm = TREE_VALUE (tparm); |
| 15108 | |
| 15109 | /* If this is an undeduced nontype parameter that depends on |
| 15110 | a type parameter, try another pass; its type may have been |
| 15111 | deduced from a later argument than the one from which |
| 15112 | this parameter can be deduced. */ |
| 15113 | if (TREE_CODE (tparm) == PARM_DECL |
| 15114 | && uses_template_parms (TREE_TYPE (tparm)) |
| 15115 | && !saw_undeduced++) |
| 15116 | goto again; |
| 15117 | |
| 15118 | /* Core issue #226 (C++0x) [temp.deduct]: |
| 15119 | |
| 15120 | If a template argument has not been deduced, its |
| 15121 | default template argument, if any, is used. |
| 15122 | |
| 15123 | When we are in C++98 mode, TREE_PURPOSE will either |
| 15124 | be NULL_TREE or ERROR_MARK_NODE, so we do not need |
| 15125 | to explicitly check cxx_dialect here. */ |
| 15126 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
| 15127 | { |
| 15128 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
| 15129 | tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i)); |
| 15130 | location_t save_loc = input_location; |
| 15131 | if (DECL_P (parm)) |
| 15132 | input_location = DECL_SOURCE_LOCATION (parm); |
| 15133 | arg = tsubst_template_arg (arg, targs, complain, NULL_TREE); |
| 15134 | arg = convert_template_argument (parm, arg, targs, complain, |
| 15135 | i, NULL_TREE); |
| 15136 | input_location = save_loc; |
| 15137 | if (arg == error_mark_node) |
| 15138 | return 1; |
| 15139 | else |
| 15140 | { |
| 15141 | TREE_VEC_ELT (targs, i) = arg; |
| 15142 | /* The position of the first default template argument, |
| 15143 | is also the number of non-defaulted arguments in TARGS. |
| 15144 | Record that. */ |
| 15145 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) |
| 15146 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i); |
| 15147 | continue; |
| 15148 | } |
| 15149 | } |
| 15150 | |
| 15151 | /* If the type parameter is a parameter pack, then it will |
| 15152 | be deduced to an empty parameter pack. */ |
| 15153 | if (template_parameter_pack_p (tparm)) |
| 15154 | { |
| 15155 | tree arg; |
| 15156 | |
| 15157 | if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX) |
| 15158 | { |
| 15159 | arg = make_node (NONTYPE_ARGUMENT_PACK); |
| 15160 | TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm)); |
| 15161 | TREE_CONSTANT (arg) = 1; |
| 15162 | } |
| 15163 | else |
| 15164 | arg = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 15165 | |
| 15166 | SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0)); |
| 15167 | |
| 15168 | TREE_VEC_ELT (targs, i) = arg; |
| 15169 | continue; |
| 15170 | } |
| 15171 | |
| 15172 | return unify_parameter_deduction_failure (explain_p, tparm); |
| 15173 | } |
| 15174 | } |
| 15175 | #ifdef ENABLE_CHECKING |
| 15176 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) |
| 15177 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs)); |
| 15178 | #endif |
| 15179 | |
| 15180 | return unify_success (explain_p); |
| 15181 | } |
| 15182 | |
| 15183 | /* Subroutine of type_unification_real. Args are like the variables |
| 15184 | at the call site. ARG is an overloaded function (or template-id); |
| 15185 | we try deducing template args from each of the overloads, and if |
| 15186 | only one succeeds, we go with that. Modifies TARGS and returns |
| 15187 | true on success. */ |
| 15188 | |
| 15189 | static bool |
| 15190 | resolve_overloaded_unification (tree tparms, |
| 15191 | tree targs, |
| 15192 | tree parm, |
| 15193 | tree arg, |
| 15194 | unification_kind_t strict, |
| 15195 | int sub_strict, |
| 15196 | bool explain_p) |
| 15197 | { |
| 15198 | tree tempargs = copy_node (targs); |
| 15199 | int good = 0; |
| 15200 | tree goodfn = NULL_TREE; |
| 15201 | bool addr_p; |
| 15202 | |
| 15203 | if (TREE_CODE (arg) == ADDR_EXPR) |
| 15204 | { |
| 15205 | arg = TREE_OPERAND (arg, 0); |
| 15206 | addr_p = true; |
| 15207 | } |
| 15208 | else |
| 15209 | addr_p = false; |
| 15210 | |
| 15211 | if (TREE_CODE (arg) == COMPONENT_REF) |
| 15212 | /* Handle `&x' where `x' is some static or non-static member |
| 15213 | function name. */ |
| 15214 | arg = TREE_OPERAND (arg, 1); |
| 15215 | |
| 15216 | if (TREE_CODE (arg) == OFFSET_REF) |
| 15217 | arg = TREE_OPERAND (arg, 1); |
| 15218 | |
| 15219 | /* Strip baselink information. */ |
| 15220 | if (BASELINK_P (arg)) |
| 15221 | arg = BASELINK_FUNCTIONS (arg); |
| 15222 | |
| 15223 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
| 15224 | { |
| 15225 | /* If we got some explicit template args, we need to plug them into |
| 15226 | the affected templates before we try to unify, in case the |
| 15227 | explicit args will completely resolve the templates in question. */ |
| 15228 | |
| 15229 | int ok = 0; |
| 15230 | tree expl_subargs = TREE_OPERAND (arg, 1); |
| 15231 | arg = TREE_OPERAND (arg, 0); |
| 15232 | |
| 15233 | for (; arg; arg = OVL_NEXT (arg)) |
| 15234 | { |
| 15235 | tree fn = OVL_CURRENT (arg); |
| 15236 | tree subargs, elem; |
| 15237 | |
| 15238 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
| 15239 | continue; |
| 15240 | |
| 15241 | ++processing_template_decl; |
| 15242 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
| 15243 | expl_subargs, /*check_ret=*/false); |
| 15244 | if (subargs && !any_dependent_template_arguments_p (subargs)) |
| 15245 | { |
| 15246 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
| 15247 | if (try_one_overload (tparms, targs, tempargs, parm, |
| 15248 | elem, strict, sub_strict, addr_p, explain_p) |
| 15249 | && (!goodfn || !same_type_p (goodfn, elem))) |
| 15250 | { |
| 15251 | goodfn = elem; |
| 15252 | ++good; |
| 15253 | } |
| 15254 | } |
| 15255 | else if (subargs) |
| 15256 | ++ok; |
| 15257 | --processing_template_decl; |
| 15258 | } |
| 15259 | /* If no templates (or more than one) are fully resolved by the |
| 15260 | explicit arguments, this template-id is a non-deduced context; it |
| 15261 | could still be OK if we deduce all template arguments for the |
| 15262 | enclosing call through other arguments. */ |
| 15263 | if (good != 1) |
| 15264 | good = ok; |
| 15265 | } |
| 15266 | else if (TREE_CODE (arg) != OVERLOAD |
| 15267 | && TREE_CODE (arg) != FUNCTION_DECL) |
| 15268 | /* If ARG is, for example, "(0, &f)" then its type will be unknown |
| 15269 | -- but the deduction does not succeed because the expression is |
| 15270 | not just the function on its own. */ |
| 15271 | return false; |
| 15272 | else |
| 15273 | for (; arg; arg = OVL_NEXT (arg)) |
| 15274 | if (try_one_overload (tparms, targs, tempargs, parm, |
| 15275 | TREE_TYPE (OVL_CURRENT (arg)), |
| 15276 | strict, sub_strict, addr_p, explain_p) |
| 15277 | && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg)))) |
| 15278 | { |
| 15279 | goodfn = OVL_CURRENT (arg); |
| 15280 | ++good; |
| 15281 | } |
| 15282 | |
| 15283 | /* [temp.deduct.type] A template-argument can be deduced from a pointer |
| 15284 | to function or pointer to member function argument if the set of |
| 15285 | overloaded functions does not contain function templates and at most |
| 15286 | one of a set of overloaded functions provides a unique match. |
| 15287 | |
| 15288 | So if we found multiple possibilities, we return success but don't |
| 15289 | deduce anything. */ |
| 15290 | |
| 15291 | if (good == 1) |
| 15292 | { |
| 15293 | int i = TREE_VEC_LENGTH (targs); |
| 15294 | for (; i--; ) |
| 15295 | if (TREE_VEC_ELT (tempargs, i)) |
| 15296 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); |
| 15297 | } |
| 15298 | if (good) |
| 15299 | return true; |
| 15300 | |
| 15301 | return false; |
| 15302 | } |
| 15303 | |
| 15304 | /* Core DR 115: In contexts where deduction is done and fails, or in |
| 15305 | contexts where deduction is not done, if a template argument list is |
| 15306 | specified and it, along with any default template arguments, identifies |
| 15307 | a single function template specialization, then the template-id is an |
| 15308 | lvalue for the function template specialization. */ |
| 15309 | |
| 15310 | tree |
| 15311 | resolve_nondeduced_context (tree orig_expr) |
| 15312 | { |
| 15313 | tree expr, offset, baselink; |
| 15314 | bool addr; |
| 15315 | |
| 15316 | if (!type_unknown_p (orig_expr)) |
| 15317 | return orig_expr; |
| 15318 | |
| 15319 | expr = orig_expr; |
| 15320 | addr = false; |
| 15321 | offset = NULL_TREE; |
| 15322 | baselink = NULL_TREE; |
| 15323 | |
| 15324 | if (TREE_CODE (expr) == ADDR_EXPR) |
| 15325 | { |
| 15326 | expr = TREE_OPERAND (expr, 0); |
| 15327 | addr = true; |
| 15328 | } |
| 15329 | if (TREE_CODE (expr) == OFFSET_REF) |
| 15330 | { |
| 15331 | offset = expr; |
| 15332 | expr = TREE_OPERAND (expr, 1); |
| 15333 | } |
| 15334 | if (BASELINK_P (expr)) |
| 15335 | { |
| 15336 | baselink = expr; |
| 15337 | expr = BASELINK_FUNCTIONS (expr); |
| 15338 | } |
| 15339 | |
| 15340 | if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) |
| 15341 | { |
| 15342 | int good = 0; |
| 15343 | tree goodfn = NULL_TREE; |
| 15344 | |
| 15345 | /* If we got some explicit template args, we need to plug them into |
| 15346 | the affected templates before we try to unify, in case the |
| 15347 | explicit args will completely resolve the templates in question. */ |
| 15348 | |
| 15349 | tree expl_subargs = TREE_OPERAND (expr, 1); |
| 15350 | tree arg = TREE_OPERAND (expr, 0); |
| 15351 | tree badfn = NULL_TREE; |
| 15352 | tree badargs = NULL_TREE; |
| 15353 | |
| 15354 | for (; arg; arg = OVL_NEXT (arg)) |
| 15355 | { |
| 15356 | tree fn = OVL_CURRENT (arg); |
| 15357 | tree subargs, elem; |
| 15358 | |
| 15359 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
| 15360 | continue; |
| 15361 | |
| 15362 | ++processing_template_decl; |
| 15363 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
| 15364 | expl_subargs, /*check_ret=*/false); |
| 15365 | if (subargs && !any_dependent_template_arguments_p (subargs)) |
| 15366 | { |
| 15367 | elem = instantiate_template (fn, subargs, tf_none); |
| 15368 | if (elem == error_mark_node) |
| 15369 | { |
| 15370 | badfn = fn; |
| 15371 | badargs = subargs; |
| 15372 | } |
| 15373 | else if (elem && (!goodfn || !decls_match (goodfn, elem))) |
| 15374 | { |
| 15375 | goodfn = elem; |
| 15376 | ++good; |
| 15377 | } |
| 15378 | } |
| 15379 | --processing_template_decl; |
| 15380 | } |
| 15381 | if (good == 1) |
| 15382 | { |
| 15383 | mark_used (goodfn); |
| 15384 | expr = goodfn; |
| 15385 | if (baselink) |
| 15386 | expr = build_baselink (BASELINK_BINFO (baselink), |
| 15387 | BASELINK_ACCESS_BINFO (baselink), |
| 15388 | expr, BASELINK_OPTYPE (baselink)); |
| 15389 | if (offset) |
| 15390 | { |
| 15391 | tree base |
| 15392 | = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0))); |
| 15393 | expr = build_offset_ref (base, expr, addr); |
| 15394 | } |
| 15395 | if (addr) |
| 15396 | expr = cp_build_addr_expr (expr, tf_warning_or_error); |
| 15397 | return expr; |
| 15398 | } |
| 15399 | else if (good == 0 && badargs) |
| 15400 | /* There were no good options and at least one bad one, so let the |
| 15401 | user know what the problem is. */ |
| 15402 | instantiate_template (badfn, badargs, tf_warning_or_error); |
| 15403 | } |
| 15404 | return orig_expr; |
| 15405 | } |
| 15406 | |
| 15407 | /* Subroutine of resolve_overloaded_unification; does deduction for a single |
| 15408 | overload. Fills TARGS with any deduced arguments, or error_mark_node if |
| 15409 | different overloads deduce different arguments for a given parm. |
| 15410 | ADDR_P is true if the expression for which deduction is being |
| 15411 | performed was of the form "& fn" rather than simply "fn". |
| 15412 | |
| 15413 | Returns 1 on success. */ |
| 15414 | |
| 15415 | static int |
| 15416 | try_one_overload (tree tparms, |
| 15417 | tree orig_targs, |
| 15418 | tree targs, |
| 15419 | tree parm, |
| 15420 | tree arg, |
| 15421 | unification_kind_t strict, |
| 15422 | int sub_strict, |
| 15423 | bool addr_p, |
| 15424 | bool explain_p) |
| 15425 | { |
| 15426 | int nargs; |
| 15427 | tree tempargs; |
| 15428 | int i; |
| 15429 | |
| 15430 | /* [temp.deduct.type] A template-argument can be deduced from a pointer |
| 15431 | to function or pointer to member function argument if the set of |
| 15432 | overloaded functions does not contain function templates and at most |
| 15433 | one of a set of overloaded functions provides a unique match. |
| 15434 | |
| 15435 | So if this is a template, just return success. */ |
| 15436 | |
| 15437 | if (uses_template_parms (arg)) |
| 15438 | return 1; |
| 15439 | |
| 15440 | if (TREE_CODE (arg) == METHOD_TYPE) |
| 15441 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); |
| 15442 | else if (addr_p) |
| 15443 | arg = build_pointer_type (arg); |
| 15444 | |
| 15445 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL); |
| 15446 | |
| 15447 | /* We don't copy orig_targs for this because if we have already deduced |
| 15448 | some template args from previous args, unify would complain when we |
| 15449 | try to deduce a template parameter for the same argument, even though |
| 15450 | there isn't really a conflict. */ |
| 15451 | nargs = TREE_VEC_LENGTH (targs); |
| 15452 | tempargs = make_tree_vec (nargs); |
| 15453 | |
| 15454 | if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p)) |
| 15455 | return 0; |
| 15456 | |
| 15457 | /* First make sure we didn't deduce anything that conflicts with |
| 15458 | explicitly specified args. */ |
| 15459 | for (i = nargs; i--; ) |
| 15460 | { |
| 15461 | tree elt = TREE_VEC_ELT (tempargs, i); |
| 15462 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
| 15463 | |
| 15464 | if (!elt) |
| 15465 | /*NOP*/; |
| 15466 | else if (uses_template_parms (elt)) |
| 15467 | /* Since we're unifying against ourselves, we will fill in |
| 15468 | template args used in the function parm list with our own |
| 15469 | template parms. Discard them. */ |
| 15470 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; |
| 15471 | else if (oldelt && !template_args_equal (oldelt, elt)) |
| 15472 | return 0; |
| 15473 | } |
| 15474 | |
| 15475 | for (i = nargs; i--; ) |
| 15476 | { |
| 15477 | tree elt = TREE_VEC_ELT (tempargs, i); |
| 15478 | |
| 15479 | if (elt) |
| 15480 | TREE_VEC_ELT (targs, i) = elt; |
| 15481 | } |
| 15482 | |
| 15483 | return 1; |
| 15484 | } |
| 15485 | |
| 15486 | /* PARM is a template class (perhaps with unbound template |
| 15487 | parameters). ARG is a fully instantiated type. If ARG can be |
| 15488 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and |
| 15489 | TARGS are as for unify. */ |
| 15490 | |
| 15491 | static tree |
| 15492 | try_class_unification (tree tparms, tree targs, tree parm, tree arg, |
| 15493 | bool explain_p) |
| 15494 | { |
| 15495 | tree copy_of_targs; |
| 15496 | |
| 15497 | if (!CLASSTYPE_TEMPLATE_INFO (arg) |
| 15498 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
| 15499 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
| 15500 | return NULL_TREE; |
| 15501 | |
| 15502 | /* We need to make a new template argument vector for the call to |
| 15503 | unify. If we used TARGS, we'd clutter it up with the result of |
| 15504 | the attempted unification, even if this class didn't work out. |
| 15505 | We also don't want to commit ourselves to all the unifications |
| 15506 | we've already done, since unification is supposed to be done on |
| 15507 | an argument-by-argument basis. In other words, consider the |
| 15508 | following pathological case: |
| 15509 | |
| 15510 | template <int I, int J, int K> |
| 15511 | struct S {}; |
| 15512 | |
| 15513 | template <int I, int J> |
| 15514 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; |
| 15515 | |
| 15516 | template <int I, int J, int K> |
| 15517 | void f(S<I, J, K>, S<I, I, I>); |
| 15518 | |
| 15519 | void g() { |
| 15520 | S<0, 0, 0> s0; |
| 15521 | S<0, 1, 2> s2; |
| 15522 | |
| 15523 | f(s0, s2); |
| 15524 | } |
| 15525 | |
| 15526 | Now, by the time we consider the unification involving `s2', we |
| 15527 | already know that we must have `f<0, 0, 0>'. But, even though |
| 15528 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
| 15529 | because there are two ways to unify base classes of S<0, 1, 2> |
| 15530 | with S<I, I, I>. If we kept the already deduced knowledge, we |
| 15531 | would reject the possibility I=1. */ |
| 15532 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
| 15533 | |
| 15534 | /* If unification failed, we're done. */ |
| 15535 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
| 15536 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) |
| 15537 | return NULL_TREE; |
| 15538 | |
| 15539 | return arg; |
| 15540 | } |
| 15541 | |
| 15542 | /* Given a template type PARM and a class type ARG, find the unique |
| 15543 | base type in ARG that is an instance of PARM. We do not examine |
| 15544 | ARG itself; only its base-classes. If there is not exactly one |
| 15545 | appropriate base class, return NULL_TREE. PARM may be the type of |
| 15546 | a partial specialization, as well as a plain template type. Used |
| 15547 | by unify. */ |
| 15548 | |
| 15549 | static enum template_base_result |
| 15550 | get_template_base (tree tparms, tree targs, tree parm, tree arg, |
| 15551 | bool explain_p, tree *result) |
| 15552 | { |
| 15553 | tree rval = NULL_TREE; |
| 15554 | tree binfo; |
| 15555 | |
| 15556 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
| 15557 | |
| 15558 | binfo = TYPE_BINFO (complete_type (arg)); |
| 15559 | if (!binfo) |
| 15560 | { |
| 15561 | /* The type could not be completed. */ |
| 15562 | *result = NULL_TREE; |
| 15563 | return tbr_incomplete_type; |
| 15564 | } |
| 15565 | |
| 15566 | /* Walk in inheritance graph order. The search order is not |
| 15567 | important, and this avoids multiple walks of virtual bases. */ |
| 15568 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) |
| 15569 | { |
| 15570 | tree r = try_class_unification (tparms, targs, parm, |
| 15571 | BINFO_TYPE (binfo), explain_p); |
| 15572 | |
| 15573 | if (r) |
| 15574 | { |
| 15575 | /* If there is more than one satisfactory baseclass, then: |
| 15576 | |
| 15577 | [temp.deduct.call] |
| 15578 | |
| 15579 | If they yield more than one possible deduced A, the type |
| 15580 | deduction fails. |
| 15581 | |
| 15582 | applies. */ |
| 15583 | if (rval && !same_type_p (r, rval)) |
| 15584 | { |
| 15585 | *result = NULL_TREE; |
| 15586 | return tbr_ambiguous_baseclass; |
| 15587 | } |
| 15588 | |
| 15589 | rval = r; |
| 15590 | } |
| 15591 | } |
| 15592 | |
| 15593 | *result = rval; |
| 15594 | return tbr_success; |
| 15595 | } |
| 15596 | |
| 15597 | /* Returns the level of DECL, which declares a template parameter. */ |
| 15598 | |
| 15599 | static int |
| 15600 | template_decl_level (tree decl) |
| 15601 | { |
| 15602 | switch (TREE_CODE (decl)) |
| 15603 | { |
| 15604 | case TYPE_DECL: |
| 15605 | case TEMPLATE_DECL: |
| 15606 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); |
| 15607 | |
| 15608 | case PARM_DECL: |
| 15609 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); |
| 15610 | |
| 15611 | default: |
| 15612 | gcc_unreachable (); |
| 15613 | } |
| 15614 | return 0; |
| 15615 | } |
| 15616 | |
| 15617 | /* Decide whether ARG can be unified with PARM, considering only the |
| 15618 | cv-qualifiers of each type, given STRICT as documented for unify. |
| 15619 | Returns nonzero iff the unification is OK on that basis. */ |
| 15620 | |
| 15621 | static int |
| 15622 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
| 15623 | { |
| 15624 | int arg_quals = cp_type_quals (arg); |
| 15625 | int parm_quals = cp_type_quals (parm); |
| 15626 | |
| 15627 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
| 15628 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
| 15629 | { |
| 15630 | /* Although a CVR qualifier is ignored when being applied to a |
| 15631 | substituted template parameter ([8.3.2]/1 for example), that |
| 15632 | does not allow us to unify "const T" with "int&" because both |
| 15633 | types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type]. |
| 15634 | It is ok when we're allowing additional CV qualifiers |
| 15635 | at the outer level [14.8.2.1]/3,1st bullet. */ |
| 15636 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
| 15637 | || TREE_CODE (arg) == FUNCTION_TYPE |
| 15638 | || TREE_CODE (arg) == METHOD_TYPE) |
| 15639 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) |
| 15640 | return 0; |
| 15641 | |
| 15642 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) |
| 15643 | && (parm_quals & TYPE_QUAL_RESTRICT)) |
| 15644 | return 0; |
| 15645 | } |
| 15646 | |
| 15647 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
| 15648 | && (arg_quals & parm_quals) != parm_quals) |
| 15649 | return 0; |
| 15650 | |
| 15651 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
| 15652 | && (parm_quals & arg_quals) != arg_quals) |
| 15653 | return 0; |
| 15654 | |
| 15655 | return 1; |
| 15656 | } |
| 15657 | |
| 15658 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
| 15659 | void |
| 15660 | template_parm_level_and_index (tree parm, int* level, int* index) |
| 15661 | { |
| 15662 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
| 15663 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
| 15664 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 15665 | { |
| 15666 | *index = TEMPLATE_TYPE_IDX (parm); |
| 15667 | *level = TEMPLATE_TYPE_LEVEL (parm); |
| 15668 | } |
| 15669 | else |
| 15670 | { |
| 15671 | *index = TEMPLATE_PARM_IDX (parm); |
| 15672 | *level = TEMPLATE_PARM_LEVEL (parm); |
| 15673 | } |
| 15674 | } |
| 15675 | |
| 15676 | #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \ |
| 15677 | do { \ |
| 15678 | if (unify (TP, TA, P, A, S, EP)) \ |
| 15679 | return 1; \ |
| 15680 | } while (0); |
| 15681 | |
| 15682 | /* Unifies the remaining arguments in PACKED_ARGS with the pack |
| 15683 | expansion at the end of PACKED_PARMS. Returns 0 if the type |
| 15684 | deduction succeeds, 1 otherwise. STRICT is the same as in |
| 15685 | unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function |
| 15686 | call argument list. We'll need to adjust the arguments to make them |
| 15687 | types. SUBR tells us if this is from a recursive call to |
| 15688 | type_unification_real, or for comparing two template argument |
| 15689 | lists. */ |
| 15690 | |
| 15691 | static int |
| 15692 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, |
| 15693 | tree packed_args, unification_kind_t strict, |
| 15694 | bool subr, bool explain_p) |
| 15695 | { |
| 15696 | tree parm |
| 15697 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); |
| 15698 | tree pattern = PACK_EXPANSION_PATTERN (parm); |
| 15699 | tree pack, packs = NULL_TREE; |
| 15700 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; |
| 15701 | int len = TREE_VEC_LENGTH (packed_args); |
| 15702 | |
| 15703 | /* Determine the parameter packs we will be deducing from the |
| 15704 | pattern, and record their current deductions. */ |
| 15705 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); |
| 15706 | pack; pack = TREE_CHAIN (pack)) |
| 15707 | { |
| 15708 | tree parm_pack = TREE_VALUE (pack); |
| 15709 | int idx, level; |
| 15710 | |
| 15711 | /* Determine the index and level of this parameter pack. */ |
| 15712 | template_parm_level_and_index (parm_pack, &level, &idx); |
| 15713 | |
| 15714 | /* Keep track of the parameter packs and their corresponding |
| 15715 | argument packs. */ |
| 15716 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); |
| 15717 | TREE_TYPE (packs) = make_tree_vec (len - start); |
| 15718 | } |
| 15719 | |
| 15720 | /* Loop through all of the arguments that have not yet been |
| 15721 | unified and unify each with the pattern. */ |
| 15722 | for (i = start; i < len; i++) |
| 15723 | { |
| 15724 | tree parm; |
| 15725 | bool any_explicit = false; |
| 15726 | tree arg = TREE_VEC_ELT (packed_args, i); |
| 15727 | |
| 15728 | /* For each parameter pack, set its TMPL_ARG to either NULL_TREE |
| 15729 | or the element of its argument pack at the current index if |
| 15730 | this argument was explicitly specified. */ |
| 15731 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
| 15732 | { |
| 15733 | int idx, level; |
| 15734 | tree arg, pargs; |
| 15735 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); |
| 15736 | |
| 15737 | arg = NULL_TREE; |
| 15738 | if (TREE_VALUE (pack) |
| 15739 | && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack))) |
| 15740 | && (i - start < TREE_VEC_LENGTH (pargs))) |
| 15741 | { |
| 15742 | any_explicit = true; |
| 15743 | arg = TREE_VEC_ELT (pargs, i - start); |
| 15744 | } |
| 15745 | TMPL_ARG (targs, level, idx) = arg; |
| 15746 | } |
| 15747 | |
| 15748 | /* If we had explicit template arguments, substitute them into the |
| 15749 | pattern before deduction. */ |
| 15750 | if (any_explicit) |
| 15751 | { |
| 15752 | /* Some arguments might still be unspecified or dependent. */ |
| 15753 | bool dependent; |
| 15754 | ++processing_template_decl; |
| 15755 | dependent = any_dependent_template_arguments_p (targs); |
| 15756 | if (!dependent) |
| 15757 | --processing_template_decl; |
| 15758 | parm = tsubst (pattern, targs, |
| 15759 | explain_p ? tf_warning_or_error : tf_none, |
| 15760 | NULL_TREE); |
| 15761 | if (dependent) |
| 15762 | --processing_template_decl; |
| 15763 | if (parm == error_mark_node) |
| 15764 | return 1; |
| 15765 | } |
| 15766 | else |
| 15767 | parm = pattern; |
| 15768 | |
| 15769 | /* Unify the pattern with the current argument. */ |
| 15770 | if (unify_one_argument (tparms, targs, parm, arg, subr, strict, |
| 15771 | LOOKUP_IMPLICIT, explain_p)) |
| 15772 | return 1; |
| 15773 | |
| 15774 | /* For each parameter pack, collect the deduced value. */ |
| 15775 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
| 15776 | { |
| 15777 | int idx, level; |
| 15778 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); |
| 15779 | |
| 15780 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = |
| 15781 | TMPL_ARG (targs, level, idx); |
| 15782 | } |
| 15783 | } |
| 15784 | |
| 15785 | /* Verify that the results of unification with the parameter packs |
| 15786 | produce results consistent with what we've seen before, and make |
| 15787 | the deduced argument packs available. */ |
| 15788 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
| 15789 | { |
| 15790 | tree old_pack = TREE_VALUE (pack); |
| 15791 | tree new_args = TREE_TYPE (pack); |
| 15792 | int i, len = TREE_VEC_LENGTH (new_args); |
| 15793 | int idx, level; |
| 15794 | bool nondeduced_p = false; |
| 15795 | |
| 15796 | /* By default keep the original deduced argument pack. |
| 15797 | If necessary, more specific code is going to update the |
| 15798 | resulting deduced argument later down in this function. */ |
| 15799 | template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); |
| 15800 | TMPL_ARG (targs, level, idx) = old_pack; |
| 15801 | |
| 15802 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't |
| 15803 | actually deduce anything. */ |
| 15804 | for (i = 0; i < len && !nondeduced_p; ++i) |
| 15805 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) |
| 15806 | nondeduced_p = true; |
| 15807 | if (nondeduced_p) |
| 15808 | continue; |
| 15809 | |
| 15810 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) |
| 15811 | { |
| 15812 | /* If we had fewer function args than explicit template args, |
| 15813 | just use the explicits. */ |
| 15814 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); |
| 15815 | int explicit_len = TREE_VEC_LENGTH (explicit_args); |
| 15816 | if (len < explicit_len) |
| 15817 | new_args = explicit_args; |
| 15818 | } |
| 15819 | |
| 15820 | if (!old_pack) |
| 15821 | { |
| 15822 | tree result; |
| 15823 | /* Build the deduced *_ARGUMENT_PACK. */ |
| 15824 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) |
| 15825 | { |
| 15826 | result = make_node (NONTYPE_ARGUMENT_PACK); |
| 15827 | TREE_TYPE (result) = |
| 15828 | TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack))); |
| 15829 | TREE_CONSTANT (result) = 1; |
| 15830 | } |
| 15831 | else |
| 15832 | result = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 15833 | |
| 15834 | SET_ARGUMENT_PACK_ARGS (result, new_args); |
| 15835 | |
| 15836 | /* Note the deduced argument packs for this parameter |
| 15837 | pack. */ |
| 15838 | TMPL_ARG (targs, level, idx) = result; |
| 15839 | } |
| 15840 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) |
| 15841 | && (ARGUMENT_PACK_ARGS (old_pack) |
| 15842 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) |
| 15843 | { |
| 15844 | /* We only had the explicitly-provided arguments before, but |
| 15845 | now we have a complete set of arguments. */ |
| 15846 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); |
| 15847 | |
| 15848 | SET_ARGUMENT_PACK_ARGS (old_pack, new_args); |
| 15849 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; |
| 15850 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; |
| 15851 | } |
| 15852 | else |
| 15853 | { |
| 15854 | tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE; |
| 15855 | tree old_args = ARGUMENT_PACK_ARGS (old_pack); |
| 15856 | |
| 15857 | if (!comp_template_args_with_info (old_args, new_args, |
| 15858 | &bad_old_arg, &bad_new_arg)) |
| 15859 | /* Inconsistent unification of this parameter pack. */ |
| 15860 | return unify_parameter_pack_inconsistent (explain_p, |
| 15861 | bad_old_arg, |
| 15862 | bad_new_arg); |
| 15863 | } |
| 15864 | } |
| 15865 | |
| 15866 | return unify_success (explain_p); |
| 15867 | } |
| 15868 | |
| 15869 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
| 15870 | set of template parameters to a template. TARGS is the bindings |
| 15871 | for those template parameters, as determined thus far; TARGS may |
| 15872 | include template arguments for outer levels of template parameters |
| 15873 | as well. PARM is a parameter to a template function, or a |
| 15874 | subcomponent of that parameter; ARG is the corresponding argument. |
| 15875 | This function attempts to match PARM with ARG in a manner |
| 15876 | consistent with the existing assignments in TARGS. If more values |
| 15877 | are deduced, then TARGS is updated. |
| 15878 | |
| 15879 | Returns 0 if the type deduction succeeds, 1 otherwise. The |
| 15880 | parameter STRICT is a bitwise or of the following flags: |
| 15881 | |
| 15882 | UNIFY_ALLOW_NONE: |
| 15883 | Require an exact match between PARM and ARG. |
| 15884 | UNIFY_ALLOW_MORE_CV_QUAL: |
| 15885 | Allow the deduced ARG to be more cv-qualified (by qualification |
| 15886 | conversion) than ARG. |
| 15887 | UNIFY_ALLOW_LESS_CV_QUAL: |
| 15888 | Allow the deduced ARG to be less cv-qualified than ARG. |
| 15889 | UNIFY_ALLOW_DERIVED: |
| 15890 | Allow the deduced ARG to be a template base class of ARG, |
| 15891 | or a pointer to a template base class of the type pointed to by |
| 15892 | ARG. |
| 15893 | UNIFY_ALLOW_INTEGER: |
| 15894 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX |
| 15895 | case for more information. |
| 15896 | UNIFY_ALLOW_OUTER_LEVEL: |
| 15897 | This is the outermost level of a deduction. Used to determine validity |
| 15898 | of qualification conversions. A valid qualification conversion must |
| 15899 | have const qualified pointers leading up to the inner type which |
| 15900 | requires additional CV quals, except at the outer level, where const |
| 15901 | is not required [conv.qual]. It would be normal to set this flag in |
| 15902 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
| 15903 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: |
| 15904 | This is the outermost level of a deduction, and PARM can be more CV |
| 15905 | qualified at this point. |
| 15906 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: |
| 15907 | This is the outermost level of a deduction, and PARM can be less CV |
| 15908 | qualified at this point. */ |
| 15909 | |
| 15910 | static int |
| 15911 | unify (tree tparms, tree targs, tree parm, tree arg, int strict, |
| 15912 | bool explain_p) |
| 15913 | { |
| 15914 | int idx; |
| 15915 | tree targ; |
| 15916 | tree tparm; |
| 15917 | int strict_in = strict; |
| 15918 | |
| 15919 | /* I don't think this will do the right thing with respect to types. |
| 15920 | But the only case I've seen it in so far has been array bounds, where |
| 15921 | signedness is the only information lost, and I think that will be |
| 15922 | okay. */ |
| 15923 | while (TREE_CODE (parm) == NOP_EXPR) |
| 15924 | parm = TREE_OPERAND (parm, 0); |
| 15925 | |
| 15926 | if (arg == error_mark_node) |
| 15927 | return unify_invalid (explain_p); |
| 15928 | if (arg == unknown_type_node |
| 15929 | || arg == init_list_type_node) |
| 15930 | /* We can't deduce anything from this, but we might get all the |
| 15931 | template args from other function args. */ |
| 15932 | return unify_success (explain_p); |
| 15933 | |
| 15934 | /* If PARM uses template parameters, then we can't bail out here, |
| 15935 | even if ARG == PARM, since we won't record unifications for the |
| 15936 | template parameters. We might need them if we're trying to |
| 15937 | figure out which of two things is more specialized. */ |
| 15938 | if (arg == parm && !uses_template_parms (parm)) |
| 15939 | return unify_success (explain_p); |
| 15940 | |
| 15941 | /* Handle init lists early, so the rest of the function can assume |
| 15942 | we're dealing with a type. */ |
| 15943 | if (BRACE_ENCLOSED_INITIALIZER_P (arg)) |
| 15944 | { |
| 15945 | tree elt, elttype; |
| 15946 | unsigned i; |
| 15947 | tree orig_parm = parm; |
| 15948 | |
| 15949 | /* Replace T with std::initializer_list<T> for deduction. */ |
| 15950 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
| 15951 | && flag_deduce_init_list) |
| 15952 | parm = listify (parm); |
| 15953 | |
| 15954 | if (!is_std_init_list (parm)) |
| 15955 | /* We can only deduce from an initializer list argument if the |
| 15956 | parameter is std::initializer_list; otherwise this is a |
| 15957 | non-deduced context. */ |
| 15958 | return unify_success (explain_p); |
| 15959 | |
| 15960 | elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); |
| 15961 | |
| 15962 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt) |
| 15963 | { |
| 15964 | int elt_strict = strict; |
| 15965 | |
| 15966 | if (elt == error_mark_node) |
| 15967 | return unify_invalid (explain_p); |
| 15968 | |
| 15969 | if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) |
| 15970 | { |
| 15971 | tree type = TREE_TYPE (elt); |
| 15972 | /* It should only be possible to get here for a call. */ |
| 15973 | gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); |
| 15974 | elt_strict |= maybe_adjust_types_for_deduction |
| 15975 | (DEDUCE_CALL, &elttype, &type, elt); |
| 15976 | elt = type; |
| 15977 | } |
| 15978 | |
| 15979 | RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict, |
| 15980 | explain_p); |
| 15981 | } |
| 15982 | |
| 15983 | /* If the std::initializer_list<T> deduction worked, replace the |
| 15984 | deduced A with std::initializer_list<A>. */ |
| 15985 | if (orig_parm != parm) |
| 15986 | { |
| 15987 | idx = TEMPLATE_TYPE_IDX (orig_parm); |
| 15988 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
| 15989 | targ = listify (targ); |
| 15990 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ; |
| 15991 | } |
| 15992 | return unify_success (explain_p); |
| 15993 | } |
| 15994 | |
| 15995 | /* Immediately reject some pairs that won't unify because of |
| 15996 | cv-qualification mismatches. */ |
| 15997 | if (TREE_CODE (arg) == TREE_CODE (parm) |
| 15998 | && TYPE_P (arg) |
| 15999 | /* It is the elements of the array which hold the cv quals of an array |
| 16000 | type, and the elements might be template type parms. We'll check |
| 16001 | when we recurse. */ |
| 16002 | && TREE_CODE (arg) != ARRAY_TYPE |
| 16003 | /* We check the cv-qualifiers when unifying with template type |
| 16004 | parameters below. We want to allow ARG `const T' to unify with |
| 16005 | PARM `T' for example, when computing which of two templates |
| 16006 | is more specialized, for example. */ |
| 16007 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM |
| 16008 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
| 16009 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
| 16010 | |
| 16011 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
| 16012 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
| 16013 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
| 16014 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; |
| 16015 | strict &= ~UNIFY_ALLOW_DERIVED; |
| 16016 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
| 16017 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; |
| 16018 | |
| 16019 | switch (TREE_CODE (parm)) |
| 16020 | { |
| 16021 | case TYPENAME_TYPE: |
| 16022 | case SCOPE_REF: |
| 16023 | case UNBOUND_CLASS_TEMPLATE: |
| 16024 | /* In a type which contains a nested-name-specifier, template |
| 16025 | argument values cannot be deduced for template parameters used |
| 16026 | within the nested-name-specifier. */ |
| 16027 | return unify_success (explain_p); |
| 16028 | |
| 16029 | case TEMPLATE_TYPE_PARM: |
| 16030 | case TEMPLATE_TEMPLATE_PARM: |
| 16031 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
| 16032 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
| 16033 | if (tparm == error_mark_node) |
| 16034 | return unify_invalid (explain_p); |
| 16035 | |
| 16036 | if (TEMPLATE_TYPE_LEVEL (parm) |
| 16037 | != template_decl_level (tparm)) |
| 16038 | /* The PARM is not one we're trying to unify. Just check |
| 16039 | to see if it matches ARG. */ |
| 16040 | { |
| 16041 | if (TREE_CODE (arg) == TREE_CODE (parm) |
| 16042 | && same_type_p (parm, arg)) |
| 16043 | return unify_success (explain_p); |
| 16044 | else |
| 16045 | return unify_type_mismatch (explain_p, parm, arg); |
| 16046 | } |
| 16047 | idx = TEMPLATE_TYPE_IDX (parm); |
| 16048 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
| 16049 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
| 16050 | if (tparm == error_mark_node) |
| 16051 | return unify_invalid (explain_p); |
| 16052 | |
| 16053 | /* Check for mixed types and values. */ |
| 16054 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
| 16055 | && TREE_CODE (tparm) != TYPE_DECL) |
| 16056 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
| 16057 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
| 16058 | gcc_unreachable (); |
| 16059 | |
| 16060 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 16061 | { |
| 16062 | /* ARG must be constructed from a template class or a template |
| 16063 | template parameter. */ |
| 16064 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM |
| 16065 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
| 16066 | return unify_template_deduction_failure (explain_p, parm, arg); |
| 16067 | |
| 16068 | { |
| 16069 | tree parmvec = TYPE_TI_ARGS (parm); |
| 16070 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
| 16071 | tree full_argvec = add_to_template_args (targs, argvec); |
| 16072 | tree parm_parms |
| 16073 | = DECL_INNERMOST_TEMPLATE_PARMS |
| 16074 | (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm)); |
| 16075 | int i, len; |
| 16076 | int parm_variadic_p = 0; |
| 16077 | |
| 16078 | /* The resolution to DR150 makes clear that default |
| 16079 | arguments for an N-argument may not be used to bind T |
| 16080 | to a template template parameter with fewer than N |
| 16081 | parameters. It is not safe to permit the binding of |
| 16082 | default arguments as an extension, as that may change |
| 16083 | the meaning of a conforming program. Consider: |
| 16084 | |
| 16085 | struct Dense { static const unsigned int dim = 1; }; |
| 16086 | |
| 16087 | template <template <typename> class View, |
| 16088 | typename Block> |
| 16089 | void operator+(float, View<Block> const&); |
| 16090 | |
| 16091 | template <typename Block, |
| 16092 | unsigned int Dim = Block::dim> |
| 16093 | struct Lvalue_proxy { operator float() const; }; |
| 16094 | |
| 16095 | void |
| 16096 | test_1d (void) { |
| 16097 | Lvalue_proxy<Dense> p; |
| 16098 | float b; |
| 16099 | b + p; |
| 16100 | } |
| 16101 | |
| 16102 | Here, if Lvalue_proxy is permitted to bind to View, then |
| 16103 | the global operator+ will be used; if they are not, the |
| 16104 | Lvalue_proxy will be converted to float. */ |
| 16105 | if (coerce_template_parms (parm_parms, |
| 16106 | full_argvec, |
| 16107 | TYPE_TI_TEMPLATE (parm), |
| 16108 | (explain_p |
| 16109 | ? tf_warning_or_error |
| 16110 | : tf_none), |
| 16111 | /*require_all_args=*/true, |
| 16112 | /*use_default_args=*/false) |
| 16113 | == error_mark_node) |
| 16114 | return 1; |
| 16115 | |
| 16116 | /* Deduce arguments T, i from TT<T> or TT<i>. |
| 16117 | We check each element of PARMVEC and ARGVEC individually |
| 16118 | rather than the whole TREE_VEC since they can have |
| 16119 | different number of elements. */ |
| 16120 | |
| 16121 | parmvec = expand_template_argument_pack (parmvec); |
| 16122 | argvec = expand_template_argument_pack (argvec); |
| 16123 | |
| 16124 | len = TREE_VEC_LENGTH (parmvec); |
| 16125 | |
| 16126 | /* Check if the parameters end in a pack, making them |
| 16127 | variadic. */ |
| 16128 | if (len > 0 |
| 16129 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) |
| 16130 | parm_variadic_p = 1; |
| 16131 | |
| 16132 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) |
| 16133 | return unify_too_few_arguments (explain_p, |
| 16134 | TREE_VEC_LENGTH (argvec), len); |
| 16135 | |
| 16136 | for (i = 0; i < len - parm_variadic_p; ++i) |
| 16137 | { |
| 16138 | RECUR_AND_CHECK_FAILURE (tparms, targs, |
| 16139 | TREE_VEC_ELT (parmvec, i), |
| 16140 | TREE_VEC_ELT (argvec, i), |
| 16141 | UNIFY_ALLOW_NONE, explain_p); |
| 16142 | } |
| 16143 | |
| 16144 | if (parm_variadic_p |
| 16145 | && unify_pack_expansion (tparms, targs, |
| 16146 | parmvec, argvec, |
| 16147 | DEDUCE_EXACT, |
| 16148 | /*subr=*/true, explain_p)) |
| 16149 | return 1; |
| 16150 | } |
| 16151 | arg = TYPE_TI_TEMPLATE (arg); |
| 16152 | |
| 16153 | /* Fall through to deduce template name. */ |
| 16154 | } |
| 16155 | |
| 16156 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
| 16157 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 16158 | { |
| 16159 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ |
| 16160 | |
| 16161 | /* Simple cases: Value already set, does match or doesn't. */ |
| 16162 | if (targ != NULL_TREE && template_args_equal (targ, arg)) |
| 16163 | return unify_success (explain_p); |
| 16164 | else if (targ) |
| 16165 | return unify_inconsistency (explain_p, parm, targ, arg); |
| 16166 | } |
| 16167 | else |
| 16168 | { |
| 16169 | /* If PARM is `const T' and ARG is only `int', we don't have |
| 16170 | a match unless we are allowing additional qualification. |
| 16171 | If ARG is `const int' and PARM is just `T' that's OK; |
| 16172 | that binds `const int' to `T'. */ |
| 16173 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
| 16174 | arg, parm)) |
| 16175 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
| 16176 | |
| 16177 | /* Consider the case where ARG is `const volatile int' and |
| 16178 | PARM is `const T'. Then, T should be `volatile int'. */ |
| 16179 | arg = cp_build_qualified_type_real |
| 16180 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); |
| 16181 | if (arg == error_mark_node) |
| 16182 | return unify_invalid (explain_p); |
| 16183 | |
| 16184 | /* Simple cases: Value already set, does match or doesn't. */ |
| 16185 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
| 16186 | return unify_success (explain_p); |
| 16187 | else if (targ) |
| 16188 | return unify_inconsistency (explain_p, parm, targ, arg); |
| 16189 | |
| 16190 | /* Make sure that ARG is not a variable-sized array. (Note |
| 16191 | that were talking about variable-sized arrays (like |
| 16192 | `int[n]'), rather than arrays of unknown size (like |
| 16193 | `int[]').) We'll get very confused by such a type since |
| 16194 | the bound of the array is not constant, and therefore |
| 16195 | not mangleable. Besides, such types are not allowed in |
| 16196 | ISO C++, so we can do as we please here. We do allow |
| 16197 | them for 'auto' deduction, since that isn't ABI-exposed. */ |
| 16198 | if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE)) |
| 16199 | return unify_vla_arg (explain_p, arg); |
| 16200 | |
| 16201 | /* Strip typedefs as in convert_template_argument. */ |
| 16202 | arg = canonicalize_type_argument (arg, tf_none); |
| 16203 | } |
| 16204 | |
| 16205 | /* If ARG is a parameter pack or an expansion, we cannot unify |
| 16206 | against it unless PARM is also a parameter pack. */ |
| 16207 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) |
| 16208 | && !template_parameter_pack_p (parm)) |
| 16209 | return unify_parameter_pack_mismatch (explain_p, parm, arg); |
| 16210 | |
| 16211 | /* If the argument deduction results is a METHOD_TYPE, |
| 16212 | then there is a problem. |
| 16213 | METHOD_TYPE doesn't map to any real C++ type the result of |
| 16214 | the deduction can not be of that type. */ |
| 16215 | if (TREE_CODE (arg) == METHOD_TYPE) |
| 16216 | return unify_method_type_error (explain_p, arg); |
| 16217 | |
| 16218 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
| 16219 | return unify_success (explain_p); |
| 16220 | |
| 16221 | case TEMPLATE_PARM_INDEX: |
| 16222 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
| 16223 | if (tparm == error_mark_node) |
| 16224 | return unify_invalid (explain_p); |
| 16225 | |
| 16226 | if (TEMPLATE_PARM_LEVEL (parm) |
| 16227 | != template_decl_level (tparm)) |
| 16228 | { |
| 16229 | /* The PARM is not one we're trying to unify. Just check |
| 16230 | to see if it matches ARG. */ |
| 16231 | int result = !(TREE_CODE (arg) == TREE_CODE (parm) |
| 16232 | && cp_tree_equal (parm, arg)); |
| 16233 | if (result) |
| 16234 | unify_expression_unequal (explain_p, parm, arg); |
| 16235 | return result; |
| 16236 | } |
| 16237 | |
| 16238 | idx = TEMPLATE_PARM_IDX (parm); |
| 16239 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
| 16240 | |
| 16241 | if (targ) |
| 16242 | { |
| 16243 | int x = !cp_tree_equal (targ, arg); |
| 16244 | if (x) |
| 16245 | unify_inconsistency (explain_p, parm, targ, arg); |
| 16246 | return x; |
| 16247 | } |
| 16248 | |
| 16249 | /* [temp.deduct.type] If, in the declaration of a function template |
| 16250 | with a non-type template-parameter, the non-type |
| 16251 | template-parameter is used in an expression in the function |
| 16252 | parameter-list and, if the corresponding template-argument is |
| 16253 | deduced, the template-argument type shall match the type of the |
| 16254 | template-parameter exactly, except that a template-argument |
| 16255 | deduced from an array bound may be of any integral type. |
| 16256 | The non-type parameter might use already deduced type parameters. */ |
| 16257 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
| 16258 | if (!TREE_TYPE (arg)) |
| 16259 | /* Template-parameter dependent expression. Just accept it for now. |
| 16260 | It will later be processed in convert_template_argument. */ |
| 16261 | ; |
| 16262 | else if (same_type_p (TREE_TYPE (arg), tparm)) |
| 16263 | /* OK */; |
| 16264 | else if ((strict & UNIFY_ALLOW_INTEGER) |
| 16265 | && (TREE_CODE (tparm) == INTEGER_TYPE |
| 16266 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) |
| 16267 | /* Convert the ARG to the type of PARM; the deduced non-type |
| 16268 | template argument must exactly match the types of the |
| 16269 | corresponding parameter. */ |
| 16270 | arg = fold (build_nop (tparm, arg)); |
| 16271 | else if (uses_template_parms (tparm)) |
| 16272 | /* We haven't deduced the type of this parameter yet. Try again |
| 16273 | later. */ |
| 16274 | return unify_success (explain_p); |
| 16275 | else |
| 16276 | return unify_type_mismatch (explain_p, tparm, arg); |
| 16277 | |
| 16278 | /* If ARG is a parameter pack or an expansion, we cannot unify |
| 16279 | against it unless PARM is also a parameter pack. */ |
| 16280 | if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) |
| 16281 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) |
| 16282 | return unify_parameter_pack_mismatch (explain_p, parm, arg); |
| 16283 | |
| 16284 | arg = strip_typedefs_expr (arg); |
| 16285 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
| 16286 | return unify_success (explain_p); |
| 16287 | |
| 16288 | case PTRMEM_CST: |
| 16289 | { |
| 16290 | /* A pointer-to-member constant can be unified only with |
| 16291 | another constant. */ |
| 16292 | if (TREE_CODE (arg) != PTRMEM_CST) |
| 16293 | return unify_ptrmem_cst_mismatch (explain_p, parm, arg); |
| 16294 | |
| 16295 | /* Just unify the class member. It would be useless (and possibly |
| 16296 | wrong, depending on the strict flags) to unify also |
| 16297 | PTRMEM_CST_CLASS, because we want to be sure that both parm and |
| 16298 | arg refer to the same variable, even if through different |
| 16299 | classes. For instance: |
| 16300 | |
| 16301 | struct A { int x; }; |
| 16302 | struct B : A { }; |
| 16303 | |
| 16304 | Unification of &A::x and &B::x must succeed. */ |
| 16305 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
| 16306 | PTRMEM_CST_MEMBER (arg), strict, explain_p); |
| 16307 | } |
| 16308 | |
| 16309 | case POINTER_TYPE: |
| 16310 | { |
| 16311 | if (TREE_CODE (arg) != POINTER_TYPE) |
| 16312 | return unify_type_mismatch (explain_p, parm, arg); |
| 16313 | |
| 16314 | /* [temp.deduct.call] |
| 16315 | |
| 16316 | A can be another pointer or pointer to member type that can |
| 16317 | be converted to the deduced A via a qualification |
| 16318 | conversion (_conv.qual_). |
| 16319 | |
| 16320 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. |
| 16321 | This will allow for additional cv-qualification of the |
| 16322 | pointed-to types if appropriate. */ |
| 16323 | |
| 16324 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
| 16325 | /* The derived-to-base conversion only persists through one |
| 16326 | level of pointers. */ |
| 16327 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
| 16328 | |
| 16329 | return unify (tparms, targs, TREE_TYPE (parm), |
| 16330 | TREE_TYPE (arg), strict, explain_p); |
| 16331 | } |
| 16332 | |
| 16333 | case REFERENCE_TYPE: |
| 16334 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
| 16335 | return unify_type_mismatch (explain_p, parm, arg); |
| 16336 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
| 16337 | strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); |
| 16338 | |
| 16339 | case ARRAY_TYPE: |
| 16340 | if (TREE_CODE (arg) != ARRAY_TYPE) |
| 16341 | return unify_type_mismatch (explain_p, parm, arg); |
| 16342 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
| 16343 | != (TYPE_DOMAIN (arg) == NULL_TREE)) |
| 16344 | return unify_type_mismatch (explain_p, parm, arg); |
| 16345 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
| 16346 | { |
| 16347 | tree parm_max; |
| 16348 | tree arg_max; |
| 16349 | bool parm_cst; |
| 16350 | bool arg_cst; |
| 16351 | |
| 16352 | /* Our representation of array types uses "N - 1" as the |
| 16353 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is |
| 16354 | not an integer constant. We cannot unify arbitrarily |
| 16355 | complex expressions, so we eliminate the MINUS_EXPRs |
| 16356 | here. */ |
| 16357 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); |
| 16358 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; |
| 16359 | if (!parm_cst) |
| 16360 | { |
| 16361 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
| 16362 | parm_max = TREE_OPERAND (parm_max, 0); |
| 16363 | } |
| 16364 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
| 16365 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; |
| 16366 | if (!arg_cst) |
| 16367 | { |
| 16368 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are |
| 16369 | trying to unify the type of a variable with the type |
| 16370 | of a template parameter. For example: |
| 16371 | |
| 16372 | template <unsigned int N> |
| 16373 | void f (char (&) [N]); |
| 16374 | int g(); |
| 16375 | void h(int i) { |
| 16376 | char a[g(i)]; |
| 16377 | f(a); |
| 16378 | } |
| 16379 | |
| 16380 | Here, the type of the ARG will be "int [g(i)]", and |
| 16381 | may be a SAVE_EXPR, etc. */ |
| 16382 | if (TREE_CODE (arg_max) != MINUS_EXPR) |
| 16383 | return unify_vla_arg (explain_p, arg); |
| 16384 | arg_max = TREE_OPERAND (arg_max, 0); |
| 16385 | } |
| 16386 | |
| 16387 | /* If only one of the bounds used a MINUS_EXPR, compensate |
| 16388 | by adding one to the other bound. */ |
| 16389 | if (parm_cst && !arg_cst) |
| 16390 | parm_max = fold_build2_loc (input_location, PLUS_EXPR, |
| 16391 | integer_type_node, |
| 16392 | parm_max, |
| 16393 | integer_one_node); |
| 16394 | else if (arg_cst && !parm_cst) |
| 16395 | arg_max = fold_build2_loc (input_location, PLUS_EXPR, |
| 16396 | integer_type_node, |
| 16397 | arg_max, |
| 16398 | integer_one_node); |
| 16399 | |
| 16400 | RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max, |
| 16401 | UNIFY_ALLOW_INTEGER, explain_p); |
| 16402 | } |
| 16403 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
| 16404 | strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); |
| 16405 | |
| 16406 | case REAL_TYPE: |
| 16407 | case COMPLEX_TYPE: |
| 16408 | case VECTOR_TYPE: |
| 16409 | case INTEGER_TYPE: |
| 16410 | case BOOLEAN_TYPE: |
| 16411 | case ENUMERAL_TYPE: |
| 16412 | case VOID_TYPE: |
| 16413 | case NULLPTR_TYPE: |
| 16414 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
| 16415 | return unify_type_mismatch (explain_p, parm, arg); |
| 16416 | |
| 16417 | /* We have already checked cv-qualification at the top of the |
| 16418 | function. */ |
| 16419 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
| 16420 | return unify_type_mismatch (explain_p, parm, arg); |
| 16421 | |
| 16422 | /* As far as unification is concerned, this wins. Later checks |
| 16423 | will invalidate it if necessary. */ |
| 16424 | return unify_success (explain_p); |
| 16425 | |
| 16426 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ |
| 16427 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
| 16428 | case INTEGER_CST: |
| 16429 | while (TREE_CODE (arg) == NOP_EXPR) |
| 16430 | arg = TREE_OPERAND (arg, 0); |
| 16431 | |
| 16432 | if (TREE_CODE (arg) != INTEGER_CST) |
| 16433 | return unify_template_argument_mismatch (explain_p, parm, arg); |
| 16434 | return (tree_int_cst_equal (parm, arg) |
| 16435 | ? unify_success (explain_p) |
| 16436 | : unify_template_argument_mismatch (explain_p, parm, arg)); |
| 16437 | |
| 16438 | case TREE_VEC: |
| 16439 | { |
| 16440 | int i, len, argslen; |
| 16441 | int parm_variadic_p = 0; |
| 16442 | |
| 16443 | if (TREE_CODE (arg) != TREE_VEC) |
| 16444 | return unify_template_argument_mismatch (explain_p, parm, arg); |
| 16445 | |
| 16446 | len = TREE_VEC_LENGTH (parm); |
| 16447 | argslen = TREE_VEC_LENGTH (arg); |
| 16448 | |
| 16449 | /* Check for pack expansions in the parameters. */ |
| 16450 | for (i = 0; i < len; ++i) |
| 16451 | { |
| 16452 | if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i))) |
| 16453 | { |
| 16454 | if (i == len - 1) |
| 16455 | /* We can unify against something with a trailing |
| 16456 | parameter pack. */ |
| 16457 | parm_variadic_p = 1; |
| 16458 | else |
| 16459 | /* [temp.deduct.type]/9: If the template argument list of |
| 16460 | P contains a pack expansion that is not the last |
| 16461 | template argument, the entire template argument list |
| 16462 | is a non-deduced context. */ |
| 16463 | return unify_success (explain_p); |
| 16464 | } |
| 16465 | } |
| 16466 | |
| 16467 | /* If we don't have enough arguments to satisfy the parameters |
| 16468 | (not counting the pack expression at the end), or we have |
| 16469 | too many arguments for a parameter list that doesn't end in |
| 16470 | a pack expression, we can't unify. */ |
| 16471 | if (parm_variadic_p |
| 16472 | ? argslen < len - parm_variadic_p |
| 16473 | : argslen != len) |
| 16474 | return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len); |
| 16475 | |
| 16476 | /* Unify all of the parameters that precede the (optional) |
| 16477 | pack expression. */ |
| 16478 | for (i = 0; i < len - parm_variadic_p; ++i) |
| 16479 | { |
| 16480 | RECUR_AND_CHECK_FAILURE (tparms, targs, |
| 16481 | TREE_VEC_ELT (parm, i), |
| 16482 | TREE_VEC_ELT (arg, i), |
| 16483 | UNIFY_ALLOW_NONE, explain_p); |
| 16484 | } |
| 16485 | if (parm_variadic_p) |
| 16486 | return unify_pack_expansion (tparms, targs, parm, arg, |
| 16487 | DEDUCE_EXACT, |
| 16488 | /*subr=*/true, explain_p); |
| 16489 | return unify_success (explain_p); |
| 16490 | } |
| 16491 | |
| 16492 | case RECORD_TYPE: |
| 16493 | case UNION_TYPE: |
| 16494 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
| 16495 | return unify_type_mismatch (explain_p, parm, arg); |
| 16496 | |
| 16497 | if (TYPE_PTRMEMFUNC_P (parm)) |
| 16498 | { |
| 16499 | if (!TYPE_PTRMEMFUNC_P (arg)) |
| 16500 | return unify_type_mismatch (explain_p, parm, arg); |
| 16501 | |
| 16502 | return unify (tparms, targs, |
| 16503 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
| 16504 | TYPE_PTRMEMFUNC_FN_TYPE (arg), |
| 16505 | strict, explain_p); |
| 16506 | } |
| 16507 | |
| 16508 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
| 16509 | { |
| 16510 | tree t = NULL_TREE; |
| 16511 | |
| 16512 | if (strict_in & UNIFY_ALLOW_DERIVED) |
| 16513 | { |
| 16514 | /* First, we try to unify the PARM and ARG directly. */ |
| 16515 | t = try_class_unification (tparms, targs, |
| 16516 | parm, arg, explain_p); |
| 16517 | |
| 16518 | if (!t) |
| 16519 | { |
| 16520 | /* Fallback to the special case allowed in |
| 16521 | [temp.deduct.call]: |
| 16522 | |
| 16523 | If P is a class, and P has the form |
| 16524 | template-id, then A can be a derived class of |
| 16525 | the deduced A. Likewise, if P is a pointer to |
| 16526 | a class of the form template-id, A can be a |
| 16527 | pointer to a derived class pointed to by the |
| 16528 | deduced A. */ |
| 16529 | enum template_base_result r; |
| 16530 | r = get_template_base (tparms, targs, parm, arg, |
| 16531 | explain_p, &t); |
| 16532 | |
| 16533 | if (!t) |
| 16534 | return unify_no_common_base (explain_p, r, parm, arg); |
| 16535 | } |
| 16536 | } |
| 16537 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
| 16538 | && (CLASSTYPE_TI_TEMPLATE (parm) |
| 16539 | == CLASSTYPE_TI_TEMPLATE (arg))) |
| 16540 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
| 16541 | Then, we should unify `int' and `U'. */ |
| 16542 | t = arg; |
| 16543 | else |
| 16544 | /* There's no chance of unification succeeding. */ |
| 16545 | return unify_type_mismatch (explain_p, parm, arg); |
| 16546 | |
| 16547 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
| 16548 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p); |
| 16549 | } |
| 16550 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
| 16551 | return unify_type_mismatch (explain_p, parm, arg); |
| 16552 | return unify_success (explain_p); |
| 16553 | |
| 16554 | case METHOD_TYPE: |
| 16555 | case FUNCTION_TYPE: |
| 16556 | { |
| 16557 | unsigned int nargs; |
| 16558 | tree *args; |
| 16559 | tree a; |
| 16560 | unsigned int i; |
| 16561 | |
| 16562 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
| 16563 | return unify_type_mismatch (explain_p, parm, arg); |
| 16564 | |
| 16565 | /* CV qualifications for methods can never be deduced, they must |
| 16566 | match exactly. We need to check them explicitly here, |
| 16567 | because type_unification_real treats them as any other |
| 16568 | cv-qualified parameter. */ |
| 16569 | if (TREE_CODE (parm) == METHOD_TYPE |
| 16570 | && (!check_cv_quals_for_unify |
| 16571 | (UNIFY_ALLOW_NONE, |
| 16572 | class_of_this_parm (arg), |
| 16573 | class_of_this_parm (parm)))) |
| 16574 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
| 16575 | |
| 16576 | RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), |
| 16577 | TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p); |
| 16578 | |
| 16579 | nargs = list_length (TYPE_ARG_TYPES (arg)); |
| 16580 | args = XALLOCAVEC (tree, nargs); |
| 16581 | for (a = TYPE_ARG_TYPES (arg), i = 0; |
| 16582 | a != NULL_TREE && a != void_list_node; |
| 16583 | a = TREE_CHAIN (a), ++i) |
| 16584 | args[i] = TREE_VALUE (a); |
| 16585 | nargs = i; |
| 16586 | |
| 16587 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
| 16588 | args, nargs, 1, DEDUCE_EXACT, |
| 16589 | LOOKUP_NORMAL, explain_p); |
| 16590 | } |
| 16591 | |
| 16592 | case OFFSET_TYPE: |
| 16593 | /* Unify a pointer to member with a pointer to member function, which |
| 16594 | deduces the type of the member as a function type. */ |
| 16595 | if (TYPE_PTRMEMFUNC_P (arg)) |
| 16596 | { |
| 16597 | tree method_type; |
| 16598 | tree fntype; |
| 16599 | |
| 16600 | /* Check top-level cv qualifiers */ |
| 16601 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) |
| 16602 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
| 16603 | |
| 16604 | RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
| 16605 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), |
| 16606 | UNIFY_ALLOW_NONE, explain_p); |
| 16607 | |
| 16608 | /* Determine the type of the function we are unifying against. */ |
| 16609 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); |
| 16610 | fntype = |
| 16611 | build_function_type (TREE_TYPE (method_type), |
| 16612 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); |
| 16613 | |
| 16614 | /* Extract the cv-qualifiers of the member function from the |
| 16615 | implicit object parameter and place them on the function |
| 16616 | type to be restored later. */ |
| 16617 | fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type)); |
| 16618 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p); |
| 16619 | } |
| 16620 | |
| 16621 | if (TREE_CODE (arg) != OFFSET_TYPE) |
| 16622 | return unify_type_mismatch (explain_p, parm, arg); |
| 16623 | RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
| 16624 | TYPE_OFFSET_BASETYPE (arg), |
| 16625 | UNIFY_ALLOW_NONE, explain_p); |
| 16626 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
| 16627 | strict, explain_p); |
| 16628 | |
| 16629 | case CONST_DECL: |
| 16630 | if (DECL_TEMPLATE_PARM_P (parm)) |
| 16631 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p); |
| 16632 | if (arg != integral_constant_value (parm)) |
| 16633 | return unify_template_argument_mismatch (explain_p, parm, arg); |
| 16634 | return unify_success (explain_p); |
| 16635 | |
| 16636 | case FIELD_DECL: |
| 16637 | case TEMPLATE_DECL: |
| 16638 | /* Matched cases are handled by the ARG == PARM test above. */ |
| 16639 | return unify_template_argument_mismatch (explain_p, parm, arg); |
| 16640 | |
| 16641 | case VAR_DECL: |
| 16642 | /* A non-type template parameter that is a variable should be a |
| 16643 | an integral constant, in which case, it whould have been |
| 16644 | folded into its (constant) value. So we should not be getting |
| 16645 | a variable here. */ |
| 16646 | gcc_unreachable (); |
| 16647 | |
| 16648 | case TYPE_ARGUMENT_PACK: |
| 16649 | case NONTYPE_ARGUMENT_PACK: |
| 16650 | return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm), |
| 16651 | ARGUMENT_PACK_ARGS (arg), strict, explain_p); |
| 16652 | |
| 16653 | case TYPEOF_TYPE: |
| 16654 | case DECLTYPE_TYPE: |
| 16655 | case UNDERLYING_TYPE: |
| 16656 | /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE, |
| 16657 | or UNDERLYING_TYPE nodes. */ |
| 16658 | return unify_success (explain_p); |
| 16659 | |
| 16660 | case ERROR_MARK: |
| 16661 | /* Unification fails if we hit an error node. */ |
| 16662 | return unify_invalid (explain_p); |
| 16663 | |
| 16664 | default: |
| 16665 | /* An unresolved overload is a nondeduced context. */ |
| 16666 | if (is_overloaded_fn (parm) || type_unknown_p (parm)) |
| 16667 | return unify_success (explain_p); |
| 16668 | gcc_assert (EXPR_P (parm)); |
| 16669 | |
| 16670 | /* We must be looking at an expression. This can happen with |
| 16671 | something like: |
| 16672 | |
| 16673 | template <int I> |
| 16674 | void foo(S<I>, S<I + 2>); |
| 16675 | |
| 16676 | This is a "nondeduced context": |
| 16677 | |
| 16678 | [deduct.type] |
| 16679 | |
| 16680 | The nondeduced contexts are: |
| 16681 | |
| 16682 | --A type that is a template-id in which one or more of |
| 16683 | the template-arguments is an expression that references |
| 16684 | a template-parameter. |
| 16685 | |
| 16686 | In these cases, we assume deduction succeeded, but don't |
| 16687 | actually infer any unifications. */ |
| 16688 | |
| 16689 | if (!uses_template_parms (parm) |
| 16690 | && !template_args_equal (parm, arg)) |
| 16691 | return unify_expression_unequal (explain_p, parm, arg); |
| 16692 | else |
| 16693 | return unify_success (explain_p); |
| 16694 | } |
| 16695 | } |
| 16696 | #undef RECUR_AND_CHECK_FAILURE |
| 16697 | \f |
| 16698 | /* Note that DECL can be defined in this translation unit, if |
| 16699 | required. */ |
| 16700 | |
| 16701 | static void |
| 16702 | mark_definable (tree decl) |
| 16703 | { |
| 16704 | tree clone; |
| 16705 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
| 16706 | FOR_EACH_CLONE (clone, decl) |
| 16707 | DECL_NOT_REALLY_EXTERN (clone) = 1; |
| 16708 | } |
| 16709 | |
| 16710 | /* Called if RESULT is explicitly instantiated, or is a member of an |
| 16711 | explicitly instantiated class. */ |
| 16712 | |
| 16713 | void |
| 16714 | mark_decl_instantiated (tree result, int extern_p) |
| 16715 | { |
| 16716 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
| 16717 | |
| 16718 | /* If this entity has already been written out, it's too late to |
| 16719 | make any modifications. */ |
| 16720 | if (TREE_ASM_WRITTEN (result)) |
| 16721 | return; |
| 16722 | |
| 16723 | if (TREE_CODE (result) != FUNCTION_DECL) |
| 16724 | /* The TREE_PUBLIC flag for function declarations will have been |
| 16725 | set correctly by tsubst. */ |
| 16726 | TREE_PUBLIC (result) = 1; |
| 16727 | |
| 16728 | /* This might have been set by an earlier implicit instantiation. */ |
| 16729 | DECL_COMDAT (result) = 0; |
| 16730 | |
| 16731 | if (extern_p) |
| 16732 | DECL_NOT_REALLY_EXTERN (result) = 0; |
| 16733 | else |
| 16734 | { |
| 16735 | mark_definable (result); |
| 16736 | /* Always make artificials weak. */ |
| 16737 | if (DECL_ARTIFICIAL (result) && flag_weak) |
| 16738 | comdat_linkage (result); |
| 16739 | /* For WIN32 we also want to put explicit instantiations in |
| 16740 | linkonce sections. */ |
| 16741 | else if (TREE_PUBLIC (result)) |
| 16742 | maybe_make_one_only (result); |
| 16743 | } |
| 16744 | |
| 16745 | /* If EXTERN_P, then this function will not be emitted -- unless |
| 16746 | followed by an explicit instantiation, at which point its linkage |
| 16747 | will be adjusted. If !EXTERN_P, then this function will be |
| 16748 | emitted here. In neither circumstance do we want |
| 16749 | import_export_decl to adjust the linkage. */ |
| 16750 | DECL_INTERFACE_KNOWN (result) = 1; |
| 16751 | } |
| 16752 | |
| 16753 | /* Subroutine of more_specialized_fn: check whether TARGS is missing any |
| 16754 | important template arguments. If any are missing, we check whether |
| 16755 | they're important by using error_mark_node for substituting into any |
| 16756 | args that were used for partial ordering (the ones between ARGS and END) |
| 16757 | and seeing if it bubbles up. */ |
| 16758 | |
| 16759 | static bool |
| 16760 | check_undeduced_parms (tree targs, tree args, tree end) |
| 16761 | { |
| 16762 | bool found = false; |
| 16763 | int i; |
| 16764 | for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i) |
| 16765 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
| 16766 | { |
| 16767 | found = true; |
| 16768 | TREE_VEC_ELT (targs, i) = error_mark_node; |
| 16769 | } |
| 16770 | if (found) |
| 16771 | { |
| 16772 | tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE); |
| 16773 | if (substed == error_mark_node) |
| 16774 | return true; |
| 16775 | } |
| 16776 | return false; |
| 16777 | } |
| 16778 | |
| 16779 | /* Given two function templates PAT1 and PAT2, return: |
| 16780 | |
| 16781 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. |
| 16782 | -1 if PAT2 is more specialized than PAT1. |
| 16783 | 0 if neither is more specialized. |
| 16784 | |
| 16785 | LEN indicates the number of parameters we should consider |
| 16786 | (defaulted parameters should not be considered). |
| 16787 | |
| 16788 | The 1998 std underspecified function template partial ordering, and |
| 16789 | DR214 addresses the issue. We take pairs of arguments, one from |
| 16790 | each of the templates, and deduce them against each other. One of |
| 16791 | the templates will be more specialized if all the *other* |
| 16792 | template's arguments deduce against its arguments and at least one |
| 16793 | of its arguments *does* *not* deduce against the other template's |
| 16794 | corresponding argument. Deduction is done as for class templates. |
| 16795 | The arguments used in deduction have reference and top level cv |
| 16796 | qualifiers removed. Iff both arguments were originally reference |
| 16797 | types *and* deduction succeeds in both directions, the template |
| 16798 | with the more cv-qualified argument wins for that pairing (if |
| 16799 | neither is more cv-qualified, they both are equal). Unlike regular |
| 16800 | deduction, after all the arguments have been deduced in this way, |
| 16801 | we do *not* verify the deduced template argument values can be |
| 16802 | substituted into non-deduced contexts. |
| 16803 | |
| 16804 | The logic can be a bit confusing here, because we look at deduce1 and |
| 16805 | targs1 to see if pat2 is at least as specialized, and vice versa; if we |
| 16806 | can find template arguments for pat1 to make arg1 look like arg2, that |
| 16807 | means that arg2 is at least as specialized as arg1. */ |
| 16808 | |
| 16809 | int |
| 16810 | more_specialized_fn (tree pat1, tree pat2, int len) |
| 16811 | { |
| 16812 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); |
| 16813 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); |
| 16814 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); |
| 16815 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); |
| 16816 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); |
| 16817 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); |
| 16818 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); |
| 16819 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); |
| 16820 | tree origs1, origs2; |
| 16821 | bool lose1 = false; |
| 16822 | bool lose2 = false; |
| 16823 | |
| 16824 | /* Remove the this parameter from non-static member functions. If |
| 16825 | one is a non-static member function and the other is not a static |
| 16826 | member function, remove the first parameter from that function |
| 16827 | also. This situation occurs for operator functions where we |
| 16828 | locate both a member function (with this pointer) and non-member |
| 16829 | operator (with explicit first operand). */ |
| 16830 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
| 16831 | { |
| 16832 | len--; /* LEN is the number of significant arguments for DECL1 */ |
| 16833 | args1 = TREE_CHAIN (args1); |
| 16834 | if (!DECL_STATIC_FUNCTION_P (decl2)) |
| 16835 | args2 = TREE_CHAIN (args2); |
| 16836 | } |
| 16837 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) |
| 16838 | { |
| 16839 | args2 = TREE_CHAIN (args2); |
| 16840 | if (!DECL_STATIC_FUNCTION_P (decl1)) |
| 16841 | { |
| 16842 | len--; |
| 16843 | args1 = TREE_CHAIN (args1); |
| 16844 | } |
| 16845 | } |
| 16846 | |
| 16847 | /* If only one is a conversion operator, they are unordered. */ |
| 16848 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) |
| 16849 | return 0; |
| 16850 | |
| 16851 | /* Consider the return type for a conversion function */ |
| 16852 | if (DECL_CONV_FN_P (decl1)) |
| 16853 | { |
| 16854 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
| 16855 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); |
| 16856 | len++; |
| 16857 | } |
| 16858 | |
| 16859 | processing_template_decl++; |
| 16860 | |
| 16861 | origs1 = args1; |
| 16862 | origs2 = args2; |
| 16863 | |
| 16864 | while (len-- |
| 16865 | /* Stop when an ellipsis is seen. */ |
| 16866 | && args1 != NULL_TREE && args2 != NULL_TREE) |
| 16867 | { |
| 16868 | tree arg1 = TREE_VALUE (args1); |
| 16869 | tree arg2 = TREE_VALUE (args2); |
| 16870 | int deduce1, deduce2; |
| 16871 | int quals1 = -1; |
| 16872 | int quals2 = -1; |
| 16873 | |
| 16874 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
| 16875 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
| 16876 | { |
| 16877 | /* When both arguments are pack expansions, we need only |
| 16878 | unify the patterns themselves. */ |
| 16879 | arg1 = PACK_EXPANSION_PATTERN (arg1); |
| 16880 | arg2 = PACK_EXPANSION_PATTERN (arg2); |
| 16881 | |
| 16882 | /* This is the last comparison we need to do. */ |
| 16883 | len = 0; |
| 16884 | } |
| 16885 | |
| 16886 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
| 16887 | { |
| 16888 | arg1 = TREE_TYPE (arg1); |
| 16889 | quals1 = cp_type_quals (arg1); |
| 16890 | } |
| 16891 | |
| 16892 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
| 16893 | { |
| 16894 | arg2 = TREE_TYPE (arg2); |
| 16895 | quals2 = cp_type_quals (arg2); |
| 16896 | } |
| 16897 | |
| 16898 | if ((quals1 < 0) != (quals2 < 0)) |
| 16899 | { |
| 16900 | /* Only of the args is a reference, see if we should apply |
| 16901 | array/function pointer decay to it. This is not part of |
| 16902 | DR214, but is, IMHO, consistent with the deduction rules |
| 16903 | for the function call itself, and with our earlier |
| 16904 | implementation of the underspecified partial ordering |
| 16905 | rules. (nathan). */ |
| 16906 | if (quals1 >= 0) |
| 16907 | { |
| 16908 | switch (TREE_CODE (arg1)) |
| 16909 | { |
| 16910 | case ARRAY_TYPE: |
| 16911 | arg1 = TREE_TYPE (arg1); |
| 16912 | /* FALLTHROUGH. */ |
| 16913 | case FUNCTION_TYPE: |
| 16914 | arg1 = build_pointer_type (arg1); |
| 16915 | break; |
| 16916 | |
| 16917 | default: |
| 16918 | break; |
| 16919 | } |
| 16920 | } |
| 16921 | else |
| 16922 | { |
| 16923 | switch (TREE_CODE (arg2)) |
| 16924 | { |
| 16925 | case ARRAY_TYPE: |
| 16926 | arg2 = TREE_TYPE (arg2); |
| 16927 | /* FALLTHROUGH. */ |
| 16928 | case FUNCTION_TYPE: |
| 16929 | arg2 = build_pointer_type (arg2); |
| 16930 | break; |
| 16931 | |
| 16932 | default: |
| 16933 | break; |
| 16934 | } |
| 16935 | } |
| 16936 | } |
| 16937 | |
| 16938 | arg1 = TYPE_MAIN_VARIANT (arg1); |
| 16939 | arg2 = TYPE_MAIN_VARIANT (arg2); |
| 16940 | |
| 16941 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
| 16942 | { |
| 16943 | int i, len2 = list_length (args2); |
| 16944 | tree parmvec = make_tree_vec (1); |
| 16945 | tree argvec = make_tree_vec (len2); |
| 16946 | tree ta = args2; |
| 16947 | |
| 16948 | /* Setup the parameter vector, which contains only ARG1. */ |
| 16949 | TREE_VEC_ELT (parmvec, 0) = arg1; |
| 16950 | |
| 16951 | /* Setup the argument vector, which contains the remaining |
| 16952 | arguments. */ |
| 16953 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) |
| 16954 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); |
| 16955 | |
| 16956 | deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec, |
| 16957 | argvec, DEDUCE_EXACT, |
| 16958 | /*subr=*/true, /*explain_p=*/false) |
| 16959 | == 0); |
| 16960 | |
| 16961 | /* We cannot deduce in the other direction, because ARG1 is |
| 16962 | a pack expansion but ARG2 is not. */ |
| 16963 | deduce2 = 0; |
| 16964 | } |
| 16965 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
| 16966 | { |
| 16967 | int i, len1 = list_length (args1); |
| 16968 | tree parmvec = make_tree_vec (1); |
| 16969 | tree argvec = make_tree_vec (len1); |
| 16970 | tree ta = args1; |
| 16971 | |
| 16972 | /* Setup the parameter vector, which contains only ARG1. */ |
| 16973 | TREE_VEC_ELT (parmvec, 0) = arg2; |
| 16974 | |
| 16975 | /* Setup the argument vector, which contains the remaining |
| 16976 | arguments. */ |
| 16977 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) |
| 16978 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); |
| 16979 | |
| 16980 | deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec, |
| 16981 | argvec, DEDUCE_EXACT, |
| 16982 | /*subr=*/true, /*explain_p=*/false) |
| 16983 | == 0); |
| 16984 | |
| 16985 | /* We cannot deduce in the other direction, because ARG2 is |
| 16986 | a pack expansion but ARG1 is not.*/ |
| 16987 | deduce1 = 0; |
| 16988 | } |
| 16989 | |
| 16990 | else |
| 16991 | { |
| 16992 | /* The normal case, where neither argument is a pack |
| 16993 | expansion. */ |
| 16994 | deduce1 = (unify (tparms1, targs1, arg1, arg2, |
| 16995 | UNIFY_ALLOW_NONE, /*explain_p=*/false) |
| 16996 | == 0); |
| 16997 | deduce2 = (unify (tparms2, targs2, arg2, arg1, |
| 16998 | UNIFY_ALLOW_NONE, /*explain_p=*/false) |
| 16999 | == 0); |
| 17000 | } |
| 17001 | |
| 17002 | /* If we couldn't deduce arguments for tparms1 to make arg1 match |
| 17003 | arg2, then arg2 is not as specialized as arg1. */ |
| 17004 | if (!deduce1) |
| 17005 | lose2 = true; |
| 17006 | if (!deduce2) |
| 17007 | lose1 = true; |
| 17008 | |
| 17009 | /* "If, for a given type, deduction succeeds in both directions |
| 17010 | (i.e., the types are identical after the transformations above) |
| 17011 | and if the type from the argument template is more cv-qualified |
| 17012 | than the type from the parameter template (as described above) |
| 17013 | that type is considered to be more specialized than the other. If |
| 17014 | neither type is more cv-qualified than the other then neither type |
| 17015 | is more specialized than the other." */ |
| 17016 | |
| 17017 | if (deduce1 && deduce2 |
| 17018 | && quals1 != quals2 && quals1 >= 0 && quals2 >= 0) |
| 17019 | { |
| 17020 | if ((quals1 & quals2) == quals2) |
| 17021 | lose2 = true; |
| 17022 | if ((quals1 & quals2) == quals1) |
| 17023 | lose1 = true; |
| 17024 | } |
| 17025 | |
| 17026 | if (lose1 && lose2) |
| 17027 | /* We've failed to deduce something in either direction. |
| 17028 | These must be unordered. */ |
| 17029 | break; |
| 17030 | |
| 17031 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
| 17032 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
| 17033 | /* We have already processed all of the arguments in our |
| 17034 | handing of the pack expansion type. */ |
| 17035 | len = 0; |
| 17036 | |
| 17037 | args1 = TREE_CHAIN (args1); |
| 17038 | args2 = TREE_CHAIN (args2); |
| 17039 | } |
| 17040 | |
| 17041 | /* "In most cases, all template parameters must have values in order for |
| 17042 | deduction to succeed, but for partial ordering purposes a template |
| 17043 | parameter may remain without a value provided it is not used in the |
| 17044 | types being used for partial ordering." |
| 17045 | |
| 17046 | Thus, if we are missing any of the targs1 we need to substitute into |
| 17047 | origs1, then pat2 is not as specialized as pat1. This can happen when |
| 17048 | there is a nondeduced context. */ |
| 17049 | if (!lose2 && check_undeduced_parms (targs1, origs1, args1)) |
| 17050 | lose2 = true; |
| 17051 | if (!lose1 && check_undeduced_parms (targs2, origs2, args2)) |
| 17052 | lose1 = true; |
| 17053 | |
| 17054 | processing_template_decl--; |
| 17055 | |
| 17056 | /* All things being equal, if the next argument is a pack expansion |
| 17057 | for one function but not for the other, prefer the |
| 17058 | non-variadic function. FIXME this is bogus; see c++/41958. */ |
| 17059 | if (lose1 == lose2 |
| 17060 | && args1 && TREE_VALUE (args1) |
| 17061 | && args2 && TREE_VALUE (args2)) |
| 17062 | { |
| 17063 | lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION; |
| 17064 | lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION; |
| 17065 | } |
| 17066 | |
| 17067 | if (lose1 == lose2) |
| 17068 | return 0; |
| 17069 | else if (!lose1) |
| 17070 | return 1; |
| 17071 | else |
| 17072 | return -1; |
| 17073 | } |
| 17074 | |
| 17075 | /* Determine which of two partial specializations is more specialized. |
| 17076 | |
| 17077 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
| 17078 | to the first partial specialization. The TREE_VALUE is the |
| 17079 | innermost set of template parameters for the partial |
| 17080 | specialization. PAT2 is similar, but for the second template. |
| 17081 | |
| 17082 | Return 1 if the first partial specialization is more specialized; |
| 17083 | -1 if the second is more specialized; 0 if neither is more |
| 17084 | specialized. |
| 17085 | |
| 17086 | See [temp.class.order] for information about determining which of |
| 17087 | two templates is more specialized. */ |
| 17088 | |
| 17089 | static int |
| 17090 | more_specialized_class (tree pat1, tree pat2) |
| 17091 | { |
| 17092 | tree targs; |
| 17093 | tree tmpl1, tmpl2; |
| 17094 | int winner = 0; |
| 17095 | bool any_deductions = false; |
| 17096 | |
| 17097 | tmpl1 = TREE_TYPE (pat1); |
| 17098 | tmpl2 = TREE_TYPE (pat2); |
| 17099 | |
| 17100 | /* Just like what happens for functions, if we are ordering between |
| 17101 | different class template specializations, we may encounter dependent |
| 17102 | types in the arguments, and we need our dependency check functions |
| 17103 | to behave correctly. */ |
| 17104 | ++processing_template_decl; |
| 17105 | targs = get_class_bindings (TREE_VALUE (pat1), |
| 17106 | CLASSTYPE_TI_ARGS (tmpl1), |
| 17107 | CLASSTYPE_TI_ARGS (tmpl2)); |
| 17108 | if (targs) |
| 17109 | { |
| 17110 | --winner; |
| 17111 | any_deductions = true; |
| 17112 | } |
| 17113 | |
| 17114 | targs = get_class_bindings (TREE_VALUE (pat2), |
| 17115 | CLASSTYPE_TI_ARGS (tmpl2), |
| 17116 | CLASSTYPE_TI_ARGS (tmpl1)); |
| 17117 | if (targs) |
| 17118 | { |
| 17119 | ++winner; |
| 17120 | any_deductions = true; |
| 17121 | } |
| 17122 | --processing_template_decl; |
| 17123 | |
| 17124 | /* In the case of a tie where at least one of the class templates |
| 17125 | has a parameter pack at the end, the template with the most |
| 17126 | non-packed parameters wins. */ |
| 17127 | if (winner == 0 |
| 17128 | && any_deductions |
| 17129 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) |
| 17130 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) |
| 17131 | { |
| 17132 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); |
| 17133 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); |
| 17134 | int len1 = TREE_VEC_LENGTH (args1); |
| 17135 | int len2 = TREE_VEC_LENGTH (args2); |
| 17136 | |
| 17137 | /* We don't count the pack expansion at the end. */ |
| 17138 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) |
| 17139 | --len1; |
| 17140 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) |
| 17141 | --len2; |
| 17142 | |
| 17143 | if (len1 > len2) |
| 17144 | return 1; |
| 17145 | else if (len1 < len2) |
| 17146 | return -1; |
| 17147 | } |
| 17148 | |
| 17149 | return winner; |
| 17150 | } |
| 17151 | |
| 17152 | /* Return the template arguments that will produce the function signature |
| 17153 | DECL from the function template FN, with the explicit template |
| 17154 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
| 17155 | also match. Return NULL_TREE if no satisfactory arguments could be |
| 17156 | found. */ |
| 17157 | |
| 17158 | static tree |
| 17159 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
| 17160 | { |
| 17161 | int ntparms = DECL_NTPARMS (fn); |
| 17162 | tree targs = make_tree_vec (ntparms); |
| 17163 | tree decl_type; |
| 17164 | tree decl_arg_types; |
| 17165 | tree *args; |
| 17166 | unsigned int nargs, ix; |
| 17167 | tree arg; |
| 17168 | |
| 17169 | /* Substitute the explicit template arguments into the type of DECL. |
| 17170 | The call to fn_type_unification will handle substitution into the |
| 17171 | FN. */ |
| 17172 | decl_type = TREE_TYPE (decl); |
| 17173 | if (explicit_args && uses_template_parms (decl_type)) |
| 17174 | { |
| 17175 | tree tmpl; |
| 17176 | tree converted_args; |
| 17177 | |
| 17178 | if (DECL_TEMPLATE_INFO (decl)) |
| 17179 | tmpl = DECL_TI_TEMPLATE (decl); |
| 17180 | else |
| 17181 | /* We can get here for some invalid specializations. */ |
| 17182 | return NULL_TREE; |
| 17183 | |
| 17184 | converted_args |
| 17185 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
| 17186 | explicit_args, NULL_TREE, |
| 17187 | tf_none, |
| 17188 | /*require_all_args=*/false, |
| 17189 | /*use_default_args=*/false); |
| 17190 | if (converted_args == error_mark_node) |
| 17191 | return NULL_TREE; |
| 17192 | |
| 17193 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); |
| 17194 | if (decl_type == error_mark_node) |
| 17195 | return NULL_TREE; |
| 17196 | } |
| 17197 | |
| 17198 | /* Never do unification on the 'this' parameter. */ |
| 17199 | decl_arg_types = skip_artificial_parms_for (decl, |
| 17200 | TYPE_ARG_TYPES (decl_type)); |
| 17201 | |
| 17202 | nargs = list_length (decl_arg_types); |
| 17203 | args = XALLOCAVEC (tree, nargs); |
| 17204 | for (arg = decl_arg_types, ix = 0; |
| 17205 | arg != NULL_TREE && arg != void_list_node; |
| 17206 | arg = TREE_CHAIN (arg), ++ix) |
| 17207 | args[ix] = TREE_VALUE (arg); |
| 17208 | |
| 17209 | if (fn_type_unification (fn, explicit_args, targs, |
| 17210 | args, ix, |
| 17211 | (check_rettype || DECL_CONV_FN_P (fn) |
| 17212 | ? TREE_TYPE (decl_type) : NULL_TREE), |
| 17213 | DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)) |
| 17214 | return NULL_TREE; |
| 17215 | |
| 17216 | return targs; |
| 17217 | } |
| 17218 | |
| 17219 | /* Return the innermost template arguments that, when applied to a |
| 17220 | template specialization whose innermost template parameters are |
| 17221 | TPARMS, and whose specialization arguments are SPEC_ARGS, yield the |
| 17222 | ARGS. |
| 17223 | |
| 17224 | For example, suppose we have: |
| 17225 | |
| 17226 | template <class T, class U> struct S {}; |
| 17227 | template <class T> struct S<T*, int> {}; |
| 17228 | |
| 17229 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be |
| 17230 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
| 17231 | int}. The resulting vector will be {double}, indicating that `T' |
| 17232 | is bound to `double'. */ |
| 17233 | |
| 17234 | static tree |
| 17235 | get_class_bindings (tree tparms, tree spec_args, tree args) |
| 17236 | { |
| 17237 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
| 17238 | tree deduced_args; |
| 17239 | tree innermost_deduced_args; |
| 17240 | |
| 17241 | innermost_deduced_args = make_tree_vec (ntparms); |
| 17242 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
| 17243 | { |
| 17244 | deduced_args = copy_node (args); |
| 17245 | SET_TMPL_ARGS_LEVEL (deduced_args, |
| 17246 | TMPL_ARGS_DEPTH (deduced_args), |
| 17247 | innermost_deduced_args); |
| 17248 | } |
| 17249 | else |
| 17250 | deduced_args = innermost_deduced_args; |
| 17251 | |
| 17252 | if (unify (tparms, deduced_args, |
| 17253 | INNERMOST_TEMPLATE_ARGS (spec_args), |
| 17254 | INNERMOST_TEMPLATE_ARGS (args), |
| 17255 | UNIFY_ALLOW_NONE, /*explain_p=*/false)) |
| 17256 | return NULL_TREE; |
| 17257 | |
| 17258 | for (i = 0; i < ntparms; ++i) |
| 17259 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
| 17260 | return NULL_TREE; |
| 17261 | |
| 17262 | /* Verify that nondeduced template arguments agree with the type |
| 17263 | obtained from argument deduction. |
| 17264 | |
| 17265 | For example: |
| 17266 | |
| 17267 | struct A { typedef int X; }; |
| 17268 | template <class T, class U> struct C {}; |
| 17269 | template <class T> struct C<T, typename T::X> {}; |
| 17270 | |
| 17271 | Then with the instantiation `C<A, int>', we can deduce that |
| 17272 | `T' is `A' but unify () does not check whether `typename T::X' |
| 17273 | is `int'. */ |
| 17274 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); |
| 17275 | if (spec_args == error_mark_node |
| 17276 | /* We only need to check the innermost arguments; the other |
| 17277 | arguments will always agree. */ |
| 17278 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), |
| 17279 | INNERMOST_TEMPLATE_ARGS (args))) |
| 17280 | return NULL_TREE; |
| 17281 | |
| 17282 | /* Now that we have bindings for all of the template arguments, |
| 17283 | ensure that the arguments deduced for the template template |
| 17284 | parameters have compatible template parameter lists. See the use |
| 17285 | of template_template_parm_bindings_ok_p in fn_type_unification |
| 17286 | for more information. */ |
| 17287 | if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) |
| 17288 | return NULL_TREE; |
| 17289 | |
| 17290 | return deduced_args; |
| 17291 | } |
| 17292 | |
| 17293 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
| 17294 | Return the TREE_LIST node with the most specialized template, if |
| 17295 | any. If there is no most specialized template, the error_mark_node |
| 17296 | is returned. |
| 17297 | |
| 17298 | Note that this function does not look at, or modify, the |
| 17299 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node |
| 17300 | returned is one of the elements of INSTANTIATIONS, callers may |
| 17301 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, |
| 17302 | and retrieve it from the value returned. */ |
| 17303 | |
| 17304 | tree |
| 17305 | most_specialized_instantiation (tree templates) |
| 17306 | { |
| 17307 | tree fn, champ; |
| 17308 | |
| 17309 | ++processing_template_decl; |
| 17310 | |
| 17311 | champ = templates; |
| 17312 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) |
| 17313 | { |
| 17314 | int fate = 0; |
| 17315 | |
| 17316 | if (get_bindings (TREE_VALUE (champ), |
| 17317 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), |
| 17318 | NULL_TREE, /*check_ret=*/true)) |
| 17319 | fate--; |
| 17320 | |
| 17321 | if (get_bindings (TREE_VALUE (fn), |
| 17322 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), |
| 17323 | NULL_TREE, /*check_ret=*/true)) |
| 17324 | fate++; |
| 17325 | |
| 17326 | if (fate == -1) |
| 17327 | champ = fn; |
| 17328 | else if (!fate) |
| 17329 | { |
| 17330 | /* Equally specialized, move to next function. If there |
| 17331 | is no next function, nothing's most specialized. */ |
| 17332 | fn = TREE_CHAIN (fn); |
| 17333 | champ = fn; |
| 17334 | if (!fn) |
| 17335 | break; |
| 17336 | } |
| 17337 | } |
| 17338 | |
| 17339 | if (champ) |
| 17340 | /* Now verify that champ is better than everything earlier in the |
| 17341 | instantiation list. */ |
| 17342 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
| 17343 | if (get_bindings (TREE_VALUE (champ), |
| 17344 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), |
| 17345 | NULL_TREE, /*check_ret=*/true) |
| 17346 | || !get_bindings (TREE_VALUE (fn), |
| 17347 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), |
| 17348 | NULL_TREE, /*check_ret=*/true)) |
| 17349 | { |
| 17350 | champ = NULL_TREE; |
| 17351 | break; |
| 17352 | } |
| 17353 | |
| 17354 | processing_template_decl--; |
| 17355 | |
| 17356 | if (!champ) |
| 17357 | return error_mark_node; |
| 17358 | |
| 17359 | return champ; |
| 17360 | } |
| 17361 | |
| 17362 | /* If DECL is a specialization of some template, return the most |
| 17363 | general such template. Otherwise, returns NULL_TREE. |
| 17364 | |
| 17365 | For example, given: |
| 17366 | |
| 17367 | template <class T> struct S { template <class U> void f(U); }; |
| 17368 | |
| 17369 | if TMPL is `template <class U> void S<int>::f(U)' this will return |
| 17370 | the full template. This function will not trace past partial |
| 17371 | specializations, however. For example, given in addition: |
| 17372 | |
| 17373 | template <class T> struct S<T*> { template <class U> void f(U); }; |
| 17374 | |
| 17375 | if TMPL is `template <class U> void S<int*>::f(U)' this will return |
| 17376 | `template <class T> template <class U> S<T*>::f(U)'. */ |
| 17377 | |
| 17378 | tree |
| 17379 | most_general_template (tree decl) |
| 17380 | { |
| 17381 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
| 17382 | an immediate specialization. */ |
| 17383 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 17384 | { |
| 17385 | if (DECL_TEMPLATE_INFO (decl)) { |
| 17386 | decl = DECL_TI_TEMPLATE (decl); |
| 17387 | |
| 17388 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a |
| 17389 | template friend. */ |
| 17390 | if (TREE_CODE (decl) != TEMPLATE_DECL) |
| 17391 | return NULL_TREE; |
| 17392 | } else |
| 17393 | return NULL_TREE; |
| 17394 | } |
| 17395 | |
| 17396 | /* Look for more and more general templates. */ |
| 17397 | while (DECL_TEMPLATE_INFO (decl)) |
| 17398 | { |
| 17399 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
| 17400 | (See cp-tree.h for details.) */ |
| 17401 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
| 17402 | break; |
| 17403 | |
| 17404 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
| 17405 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
| 17406 | break; |
| 17407 | |
| 17408 | /* Stop if we run into an explicitly specialized class template. */ |
| 17409 | if (!DECL_NAMESPACE_SCOPE_P (decl) |
| 17410 | && DECL_CONTEXT (decl) |
| 17411 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) |
| 17412 | break; |
| 17413 | |
| 17414 | decl = DECL_TI_TEMPLATE (decl); |
| 17415 | } |
| 17416 | |
| 17417 | return decl; |
| 17418 | } |
| 17419 | |
| 17420 | /* Return the most specialized of the class template partial |
| 17421 | specializations of TMPL which can produce TYPE, a specialization of |
| 17422 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is |
| 17423 | a _TYPE node corresponding to the partial specialization, while the |
| 17424 | TREE_PURPOSE is the set of template arguments that must be |
| 17425 | substituted into the TREE_TYPE in order to generate TYPE. |
| 17426 | |
| 17427 | If the choice of partial specialization is ambiguous, a diagnostic |
| 17428 | is issued, and the error_mark_node is returned. If there are no |
| 17429 | partial specializations of TMPL matching TYPE, then NULL_TREE is |
| 17430 | returned. */ |
| 17431 | |
| 17432 | static tree |
| 17433 | most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain) |
| 17434 | { |
| 17435 | tree list = NULL_TREE; |
| 17436 | tree t; |
| 17437 | tree champ; |
| 17438 | int fate; |
| 17439 | bool ambiguous_p; |
| 17440 | tree args; |
| 17441 | tree outer_args = NULL_TREE; |
| 17442 | |
| 17443 | tmpl = most_general_template (tmpl); |
| 17444 | args = CLASSTYPE_TI_ARGS (type); |
| 17445 | |
| 17446 | /* For determining which partial specialization to use, only the |
| 17447 | innermost args are interesting. */ |
| 17448 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
| 17449 | { |
| 17450 | outer_args = strip_innermost_template_args (args, 1); |
| 17451 | args = INNERMOST_TEMPLATE_ARGS (args); |
| 17452 | } |
| 17453 | |
| 17454 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
| 17455 | { |
| 17456 | tree partial_spec_args; |
| 17457 | tree spec_args; |
| 17458 | tree parms = TREE_VALUE (t); |
| 17459 | |
| 17460 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); |
| 17461 | |
| 17462 | ++processing_template_decl; |
| 17463 | |
| 17464 | if (outer_args) |
| 17465 | { |
| 17466 | int i; |
| 17467 | |
| 17468 | /* Discard the outer levels of args, and then substitute in the |
| 17469 | template args from the enclosing class. */ |
| 17470 | partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args); |
| 17471 | partial_spec_args = tsubst_template_args |
| 17472 | (partial_spec_args, outer_args, tf_none, NULL_TREE); |
| 17473 | |
| 17474 | /* PARMS already refers to just the innermost parms, but the |
| 17475 | template parms in partial_spec_args had their levels lowered |
| 17476 | by tsubst, so we need to do the same for the parm list. We |
| 17477 | can't just tsubst the TREE_VEC itself, as tsubst wants to |
| 17478 | treat a TREE_VEC as an argument vector. */ |
| 17479 | parms = copy_node (parms); |
| 17480 | for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) |
| 17481 | TREE_VEC_ELT (parms, i) = |
| 17482 | tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE); |
| 17483 | |
| 17484 | } |
| 17485 | |
| 17486 | partial_spec_args = |
| 17487 | coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
| 17488 | add_to_template_args (outer_args, |
| 17489 | partial_spec_args), |
| 17490 | tmpl, tf_none, |
| 17491 | /*require_all_args=*/true, |
| 17492 | /*use_default_args=*/true); |
| 17493 | |
| 17494 | --processing_template_decl; |
| 17495 | |
| 17496 | if (partial_spec_args == error_mark_node) |
| 17497 | return error_mark_node; |
| 17498 | |
| 17499 | spec_args = get_class_bindings (parms, |
| 17500 | partial_spec_args, |
| 17501 | args); |
| 17502 | if (spec_args) |
| 17503 | { |
| 17504 | if (outer_args) |
| 17505 | spec_args = add_to_template_args (outer_args, spec_args); |
| 17506 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
| 17507 | TREE_TYPE (list) = TREE_TYPE (t); |
| 17508 | } |
| 17509 | } |
| 17510 | |
| 17511 | if (! list) |
| 17512 | return NULL_TREE; |
| 17513 | |
| 17514 | ambiguous_p = false; |
| 17515 | t = list; |
| 17516 | champ = t; |
| 17517 | t = TREE_CHAIN (t); |
| 17518 | for (; t; t = TREE_CHAIN (t)) |
| 17519 | { |
| 17520 | fate = more_specialized_class (champ, t); |
| 17521 | if (fate == 1) |
| 17522 | ; |
| 17523 | else |
| 17524 | { |
| 17525 | if (fate == 0) |
| 17526 | { |
| 17527 | t = TREE_CHAIN (t); |
| 17528 | if (! t) |
| 17529 | { |
| 17530 | ambiguous_p = true; |
| 17531 | break; |
| 17532 | } |
| 17533 | } |
| 17534 | champ = t; |
| 17535 | } |
| 17536 | } |
| 17537 | |
| 17538 | if (!ambiguous_p) |
| 17539 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) |
| 17540 | { |
| 17541 | fate = more_specialized_class (champ, t); |
| 17542 | if (fate != 1) |
| 17543 | { |
| 17544 | ambiguous_p = true; |
| 17545 | break; |
| 17546 | } |
| 17547 | } |
| 17548 | |
| 17549 | if (ambiguous_p) |
| 17550 | { |
| 17551 | const char *str; |
| 17552 | char *spaces = NULL; |
| 17553 | if (!(complain & tf_error)) |
| 17554 | return error_mark_node; |
| 17555 | error ("ambiguous class template instantiation for %q#T", type); |
| 17556 | str = ngettext ("candidate is:", "candidates are:", list_length (list)); |
| 17557 | for (t = list; t; t = TREE_CHAIN (t)) |
| 17558 | { |
| 17559 | error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t)); |
| 17560 | spaces = spaces ? spaces : get_spaces (str); |
| 17561 | } |
| 17562 | free (spaces); |
| 17563 | return error_mark_node; |
| 17564 | } |
| 17565 | |
| 17566 | return champ; |
| 17567 | } |
| 17568 | |
| 17569 | /* Explicitly instantiate DECL. */ |
| 17570 | |
| 17571 | void |
| 17572 | do_decl_instantiation (tree decl, tree storage) |
| 17573 | { |
| 17574 | tree result = NULL_TREE; |
| 17575 | int extern_p = 0; |
| 17576 | |
| 17577 | if (!decl || decl == error_mark_node) |
| 17578 | /* An error occurred, for which grokdeclarator has already issued |
| 17579 | an appropriate message. */ |
| 17580 | return; |
| 17581 | else if (! DECL_LANG_SPECIFIC (decl)) |
| 17582 | { |
| 17583 | error ("explicit instantiation of non-template %q#D", decl); |
| 17584 | return; |
| 17585 | } |
| 17586 | else if (TREE_CODE (decl) == VAR_DECL) |
| 17587 | { |
| 17588 | /* There is an asymmetry here in the way VAR_DECLs and |
| 17589 | FUNCTION_DECLs are handled by grokdeclarator. In the case of |
| 17590 | the latter, the DECL we get back will be marked as a |
| 17591 | template instantiation, and the appropriate |
| 17592 | DECL_TEMPLATE_INFO will be set up. This does not happen for |
| 17593 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator |
| 17594 | should handle VAR_DECLs as it currently handles |
| 17595 | FUNCTION_DECLs. */ |
| 17596 | if (!DECL_CLASS_SCOPE_P (decl)) |
| 17597 | { |
| 17598 | error ("%qD is not a static data member of a class template", decl); |
| 17599 | return; |
| 17600 | } |
| 17601 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
| 17602 | if (!result || TREE_CODE (result) != VAR_DECL) |
| 17603 | { |
| 17604 | error ("no matching template for %qD found", decl); |
| 17605 | return; |
| 17606 | } |
| 17607 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
| 17608 | { |
| 17609 | error ("type %qT for explicit instantiation %qD does not match " |
| 17610 | "declared type %qT", TREE_TYPE (result), decl, |
| 17611 | TREE_TYPE (decl)); |
| 17612 | return; |
| 17613 | } |
| 17614 | } |
| 17615 | else if (TREE_CODE (decl) != FUNCTION_DECL) |
| 17616 | { |
| 17617 | error ("explicit instantiation of %q#D", decl); |
| 17618 | return; |
| 17619 | } |
| 17620 | else |
| 17621 | result = decl; |
| 17622 | |
| 17623 | /* Check for various error cases. Note that if the explicit |
| 17624 | instantiation is valid the RESULT will currently be marked as an |
| 17625 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
| 17626 | until we get here. */ |
| 17627 | |
| 17628 | if (DECL_TEMPLATE_SPECIALIZATION (result)) |
| 17629 | { |
| 17630 | /* DR 259 [temp.spec]. |
| 17631 | |
| 17632 | Both an explicit instantiation and a declaration of an explicit |
| 17633 | specialization shall not appear in a program unless the explicit |
| 17634 | instantiation follows a declaration of the explicit specialization. |
| 17635 | |
| 17636 | For a given set of template parameters, if an explicit |
| 17637 | instantiation of a template appears after a declaration of an |
| 17638 | explicit specialization for that template, the explicit |
| 17639 | instantiation has no effect. */ |
| 17640 | return; |
| 17641 | } |
| 17642 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
| 17643 | { |
| 17644 | /* [temp.spec] |
| 17645 | |
| 17646 | No program shall explicitly instantiate any template more |
| 17647 | than once. |
| 17648 | |
| 17649 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
| 17650 | the first instantiation was `extern' and the second is not, |
| 17651 | and EXTERN_P for the opposite case. */ |
| 17652 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) |
| 17653 | permerror (input_location, "duplicate explicit instantiation of %q#D", result); |
| 17654 | /* If an "extern" explicit instantiation follows an ordinary |
| 17655 | explicit instantiation, the template is instantiated. */ |
| 17656 | if (extern_p) |
| 17657 | return; |
| 17658 | } |
| 17659 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) |
| 17660 | { |
| 17661 | error ("no matching template for %qD found", result); |
| 17662 | return; |
| 17663 | } |
| 17664 | else if (!DECL_TEMPLATE_INFO (result)) |
| 17665 | { |
| 17666 | permerror (input_location, "explicit instantiation of non-template %q#D", result); |
| 17667 | return; |
| 17668 | } |
| 17669 | |
| 17670 | if (storage == NULL_TREE) |
| 17671 | ; |
| 17672 | else if (storage == ridpointers[(int) RID_EXTERN]) |
| 17673 | { |
| 17674 | if (!in_system_header && (cxx_dialect == cxx98)) |
| 17675 | pedwarn (input_location, OPT_pedantic, |
| 17676 | "ISO C++ 1998 forbids the use of %<extern%> on explicit " |
| 17677 | "instantiations"); |
| 17678 | extern_p = 1; |
| 17679 | } |
| 17680 | else |
| 17681 | error ("storage class %qD applied to template instantiation", storage); |
| 17682 | |
| 17683 | check_explicit_instantiation_namespace (result); |
| 17684 | mark_decl_instantiated (result, extern_p); |
| 17685 | if (! extern_p) |
| 17686 | instantiate_decl (result, /*defer_ok=*/1, |
| 17687 | /*expl_inst_class_mem_p=*/false); |
| 17688 | } |
| 17689 | |
| 17690 | static void |
| 17691 | mark_class_instantiated (tree t, int extern_p) |
| 17692 | { |
| 17693 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); |
| 17694 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
| 17695 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; |
| 17696 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
| 17697 | if (! extern_p) |
| 17698 | { |
| 17699 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; |
| 17700 | rest_of_type_compilation (t, 1); |
| 17701 | } |
| 17702 | } |
| 17703 | |
| 17704 | /* Called from do_type_instantiation through binding_table_foreach to |
| 17705 | do recursive instantiation for the type bound in ENTRY. */ |
| 17706 | static void |
| 17707 | bt_instantiate_type_proc (binding_entry entry, void *data) |
| 17708 | { |
| 17709 | tree storage = *(tree *) data; |
| 17710 | |
| 17711 | if (MAYBE_CLASS_TYPE_P (entry->type) |
| 17712 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) |
| 17713 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); |
| 17714 | } |
| 17715 | |
| 17716 | /* Called from do_type_instantiation to instantiate a member |
| 17717 | (a member function or a static member variable) of an |
| 17718 | explicitly instantiated class template. */ |
| 17719 | static void |
| 17720 | instantiate_class_member (tree decl, int extern_p) |
| 17721 | { |
| 17722 | mark_decl_instantiated (decl, extern_p); |
| 17723 | if (! extern_p) |
| 17724 | instantiate_decl (decl, /*defer_ok=*/1, |
| 17725 | /*expl_inst_class_mem_p=*/true); |
| 17726 | } |
| 17727 | |
| 17728 | /* Perform an explicit instantiation of template class T. STORAGE, if |
| 17729 | non-null, is the RID for extern, inline or static. COMPLAIN is |
| 17730 | nonzero if this is called from the parser, zero if called recursively, |
| 17731 | since the standard is unclear (as detailed below). */ |
| 17732 | |
| 17733 | void |
| 17734 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
| 17735 | { |
| 17736 | int extern_p = 0; |
| 17737 | int nomem_p = 0; |
| 17738 | int static_p = 0; |
| 17739 | int previous_instantiation_extern_p = 0; |
| 17740 | |
| 17741 | if (TREE_CODE (t) == TYPE_DECL) |
| 17742 | t = TREE_TYPE (t); |
| 17743 | |
| 17744 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
| 17745 | { |
| 17746 | tree tmpl = |
| 17747 | (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL; |
| 17748 | if (tmpl) |
| 17749 | error ("explicit instantiation of non-class template %qD", tmpl); |
| 17750 | else |
| 17751 | error ("explicit instantiation of non-template type %qT", t); |
| 17752 | return; |
| 17753 | } |
| 17754 | |
| 17755 | complete_type (t); |
| 17756 | |
| 17757 | if (!COMPLETE_TYPE_P (t)) |
| 17758 | { |
| 17759 | if (complain & tf_error) |
| 17760 | error ("explicit instantiation of %q#T before definition of template", |
| 17761 | t); |
| 17762 | return; |
| 17763 | } |
| 17764 | |
| 17765 | if (storage != NULL_TREE) |
| 17766 | { |
| 17767 | if (!in_system_header) |
| 17768 | { |
| 17769 | if (storage == ridpointers[(int) RID_EXTERN]) |
| 17770 | { |
| 17771 | if (cxx_dialect == cxx98) |
| 17772 | pedwarn (input_location, OPT_pedantic, |
| 17773 | "ISO C++ 1998 forbids the use of %<extern%> on " |
| 17774 | "explicit instantiations"); |
| 17775 | } |
| 17776 | else |
| 17777 | pedwarn (input_location, OPT_pedantic, |
| 17778 | "ISO C++ forbids the use of %qE" |
| 17779 | " on explicit instantiations", storage); |
| 17780 | } |
| 17781 | |
| 17782 | if (storage == ridpointers[(int) RID_INLINE]) |
| 17783 | nomem_p = 1; |
| 17784 | else if (storage == ridpointers[(int) RID_EXTERN]) |
| 17785 | extern_p = 1; |
| 17786 | else if (storage == ridpointers[(int) RID_STATIC]) |
| 17787 | static_p = 1; |
| 17788 | else |
| 17789 | { |
| 17790 | error ("storage class %qD applied to template instantiation", |
| 17791 | storage); |
| 17792 | extern_p = 0; |
| 17793 | } |
| 17794 | } |
| 17795 | |
| 17796 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
| 17797 | { |
| 17798 | /* DR 259 [temp.spec]. |
| 17799 | |
| 17800 | Both an explicit instantiation and a declaration of an explicit |
| 17801 | specialization shall not appear in a program unless the explicit |
| 17802 | instantiation follows a declaration of the explicit specialization. |
| 17803 | |
| 17804 | For a given set of template parameters, if an explicit |
| 17805 | instantiation of a template appears after a declaration of an |
| 17806 | explicit specialization for that template, the explicit |
| 17807 | instantiation has no effect. */ |
| 17808 | return; |
| 17809 | } |
| 17810 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) |
| 17811 | { |
| 17812 | /* [temp.spec] |
| 17813 | |
| 17814 | No program shall explicitly instantiate any template more |
| 17815 | than once. |
| 17816 | |
| 17817 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
| 17818 | instantiation was `extern'. If EXTERN_P then the second is. |
| 17819 | These cases are OK. */ |
| 17820 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
| 17821 | |
| 17822 | if (!previous_instantiation_extern_p && !extern_p |
| 17823 | && (complain & tf_error)) |
| 17824 | permerror (input_location, "duplicate explicit instantiation of %q#T", t); |
| 17825 | |
| 17826 | /* If we've already instantiated the template, just return now. */ |
| 17827 | if (!CLASSTYPE_INTERFACE_ONLY (t)) |
| 17828 | return; |
| 17829 | } |
| 17830 | |
| 17831 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
| 17832 | mark_class_instantiated (t, extern_p); |
| 17833 | |
| 17834 | if (nomem_p) |
| 17835 | return; |
| 17836 | |
| 17837 | { |
| 17838 | tree tmp; |
| 17839 | |
| 17840 | /* In contrast to implicit instantiation, where only the |
| 17841 | declarations, and not the definitions, of members are |
| 17842 | instantiated, we have here: |
| 17843 | |
| 17844 | [temp.explicit] |
| 17845 | |
| 17846 | The explicit instantiation of a class template specialization |
| 17847 | implies the instantiation of all of its members not |
| 17848 | previously explicitly specialized in the translation unit |
| 17849 | containing the explicit instantiation. |
| 17850 | |
| 17851 | Of course, we can't instantiate member template classes, since |
| 17852 | we don't have any arguments for them. Note that the standard |
| 17853 | is unclear on whether the instantiation of the members are |
| 17854 | *explicit* instantiations or not. However, the most natural |
| 17855 | interpretation is that it should be an explicit instantiation. */ |
| 17856 | |
| 17857 | if (! static_p) |
| 17858 | for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp)) |
| 17859 | if (TREE_CODE (tmp) == FUNCTION_DECL |
| 17860 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
| 17861 | instantiate_class_member (tmp, extern_p); |
| 17862 | |
| 17863 | for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp)) |
| 17864 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) |
| 17865 | instantiate_class_member (tmp, extern_p); |
| 17866 | |
| 17867 | if (CLASSTYPE_NESTED_UTDS (t)) |
| 17868 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), |
| 17869 | bt_instantiate_type_proc, &storage); |
| 17870 | } |
| 17871 | } |
| 17872 | |
| 17873 | /* Given a function DECL, which is a specialization of TMPL, modify |
| 17874 | DECL to be a re-instantiation of TMPL with the same template |
| 17875 | arguments. TMPL should be the template into which tsubst'ing |
| 17876 | should occur for DECL, not the most general template. |
| 17877 | |
| 17878 | One reason for doing this is a scenario like this: |
| 17879 | |
| 17880 | template <class T> |
| 17881 | void f(const T&, int i); |
| 17882 | |
| 17883 | void g() { f(3, 7); } |
| 17884 | |
| 17885 | template <class T> |
| 17886 | void f(const T& t, const int i) { } |
| 17887 | |
| 17888 | Note that when the template is first instantiated, with |
| 17889 | instantiate_template, the resulting DECL will have no name for the |
| 17890 | first parameter, and the wrong type for the second. So, when we go |
| 17891 | to instantiate the DECL, we regenerate it. */ |
| 17892 | |
| 17893 | static void |
| 17894 | regenerate_decl_from_template (tree decl, tree tmpl) |
| 17895 | { |
| 17896 | /* The arguments used to instantiate DECL, from the most general |
| 17897 | template. */ |
| 17898 | tree args; |
| 17899 | tree code_pattern; |
| 17900 | |
| 17901 | args = DECL_TI_ARGS (decl); |
| 17902 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); |
| 17903 | |
| 17904 | /* Make sure that we can see identifiers, and compute access |
| 17905 | correctly. */ |
| 17906 | push_access_scope (decl); |
| 17907 | |
| 17908 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 17909 | { |
| 17910 | tree decl_parm; |
| 17911 | tree pattern_parm; |
| 17912 | tree specs; |
| 17913 | int args_depth; |
| 17914 | int parms_depth; |
| 17915 | |
| 17916 | args_depth = TMPL_ARGS_DEPTH (args); |
| 17917 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
| 17918 | if (args_depth > parms_depth) |
| 17919 | args = get_innermost_template_args (args, parms_depth); |
| 17920 | |
| 17921 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), |
| 17922 | args, tf_error, NULL_TREE, |
| 17923 | /*defer_ok*/false); |
| 17924 | if (specs && specs != error_mark_node) |
| 17925 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), |
| 17926 | specs); |
| 17927 | |
| 17928 | /* Merge parameter declarations. */ |
| 17929 | decl_parm = skip_artificial_parms_for (decl, |
| 17930 | DECL_ARGUMENTS (decl)); |
| 17931 | pattern_parm |
| 17932 | = skip_artificial_parms_for (code_pattern, |
| 17933 | DECL_ARGUMENTS (code_pattern)); |
| 17934 | while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
| 17935 | { |
| 17936 | tree parm_type; |
| 17937 | tree attributes; |
| 17938 | |
| 17939 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
| 17940 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); |
| 17941 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, |
| 17942 | NULL_TREE); |
| 17943 | parm_type = type_decays_to (parm_type); |
| 17944 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
| 17945 | TREE_TYPE (decl_parm) = parm_type; |
| 17946 | attributes = DECL_ATTRIBUTES (pattern_parm); |
| 17947 | if (DECL_ATTRIBUTES (decl_parm) != attributes) |
| 17948 | { |
| 17949 | DECL_ATTRIBUTES (decl_parm) = attributes; |
| 17950 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); |
| 17951 | } |
| 17952 | decl_parm = DECL_CHAIN (decl_parm); |
| 17953 | pattern_parm = DECL_CHAIN (pattern_parm); |
| 17954 | } |
| 17955 | /* Merge any parameters that match with the function parameter |
| 17956 | pack. */ |
| 17957 | if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm)) |
| 17958 | { |
| 17959 | int i, len; |
| 17960 | tree expanded_types; |
| 17961 | /* Expand the TYPE_PACK_EXPANSION that provides the types for |
| 17962 | the parameters in this function parameter pack. */ |
| 17963 | expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm), |
| 17964 | args, tf_error, NULL_TREE); |
| 17965 | len = TREE_VEC_LENGTH (expanded_types); |
| 17966 | for (i = 0; i < len; i++) |
| 17967 | { |
| 17968 | tree parm_type; |
| 17969 | tree attributes; |
| 17970 | |
| 17971 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) |
| 17972 | /* Rename the parameter to include the index. */ |
| 17973 | DECL_NAME (decl_parm) = |
| 17974 | make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i); |
| 17975 | parm_type = TREE_VEC_ELT (expanded_types, i); |
| 17976 | parm_type = type_decays_to (parm_type); |
| 17977 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
| 17978 | TREE_TYPE (decl_parm) = parm_type; |
| 17979 | attributes = DECL_ATTRIBUTES (pattern_parm); |
| 17980 | if (DECL_ATTRIBUTES (decl_parm) != attributes) |
| 17981 | { |
| 17982 | DECL_ATTRIBUTES (decl_parm) = attributes; |
| 17983 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); |
| 17984 | } |
| 17985 | decl_parm = DECL_CHAIN (decl_parm); |
| 17986 | } |
| 17987 | } |
| 17988 | /* Merge additional specifiers from the CODE_PATTERN. */ |
| 17989 | if (DECL_DECLARED_INLINE_P (code_pattern) |
| 17990 | && !DECL_DECLARED_INLINE_P (decl)) |
| 17991 | DECL_DECLARED_INLINE_P (decl) = 1; |
| 17992 | } |
| 17993 | else if (TREE_CODE (decl) == VAR_DECL) |
| 17994 | { |
| 17995 | DECL_INITIAL (decl) = |
| 17996 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
| 17997 | tf_error, DECL_TI_TEMPLATE (decl), |
| 17998 | /*integral_constant_expression_p=*/false); |
| 17999 | if (VAR_HAD_UNKNOWN_BOUND (decl)) |
| 18000 | TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args, |
| 18001 | tf_error, DECL_TI_TEMPLATE (decl)); |
| 18002 | } |
| 18003 | else |
| 18004 | gcc_unreachable (); |
| 18005 | |
| 18006 | pop_access_scope (decl); |
| 18007 | } |
| 18008 | |
| 18009 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
| 18010 | substituted to get DECL. */ |
| 18011 | |
| 18012 | tree |
| 18013 | template_for_substitution (tree decl) |
| 18014 | { |
| 18015 | tree tmpl = DECL_TI_TEMPLATE (decl); |
| 18016 | |
| 18017 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern |
| 18018 | for the instantiation. This is not always the most general |
| 18019 | template. Consider, for example: |
| 18020 | |
| 18021 | template <class T> |
| 18022 | struct S { template <class U> void f(); |
| 18023 | template <> void f<int>(); }; |
| 18024 | |
| 18025 | and an instantiation of S<double>::f<int>. We want TD to be the |
| 18026 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ |
| 18027 | while (/* An instantiation cannot have a definition, so we need a |
| 18028 | more general template. */ |
| 18029 | DECL_TEMPLATE_INSTANTIATION (tmpl) |
| 18030 | /* We must also deal with friend templates. Given: |
| 18031 | |
| 18032 | template <class T> struct S { |
| 18033 | template <class U> friend void f() {}; |
| 18034 | }; |
| 18035 | |
| 18036 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, |
| 18037 | so far as the language is concerned, but that's still |
| 18038 | where we get the pattern for the instantiation from. On |
| 18039 | other hand, if the definition comes outside the class, say: |
| 18040 | |
| 18041 | template <class T> struct S { |
| 18042 | template <class U> friend void f(); |
| 18043 | }; |
| 18044 | template <class U> friend void f() {} |
| 18045 | |
| 18046 | we don't need to look any further. That's what the check for |
| 18047 | DECL_INITIAL is for. */ |
| 18048 | || (TREE_CODE (decl) == FUNCTION_DECL |
| 18049 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) |
| 18050 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) |
| 18051 | { |
| 18052 | /* The present template, TD, should not be a definition. If it |
| 18053 | were a definition, we should be using it! Note that we |
| 18054 | cannot restructure the loop to just keep going until we find |
| 18055 | a template with a definition, since that might go too far if |
| 18056 | a specialization was declared, but not defined. */ |
| 18057 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
| 18058 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); |
| 18059 | |
| 18060 | /* Fetch the more general template. */ |
| 18061 | tmpl = DECL_TI_TEMPLATE (tmpl); |
| 18062 | } |
| 18063 | |
| 18064 | return tmpl; |
| 18065 | } |
| 18066 | |
| 18067 | /* Returns true if we need to instantiate this template instance even if we |
| 18068 | know we aren't going to emit it.. */ |
| 18069 | |
| 18070 | bool |
| 18071 | always_instantiate_p (tree decl) |
| 18072 | { |
| 18073 | /* We always instantiate inline functions so that we can inline them. An |
| 18074 | explicit instantiation declaration prohibits implicit instantiation of |
| 18075 | non-inline functions. With high levels of optimization, we would |
| 18076 | normally inline non-inline functions -- but we're not allowed to do |
| 18077 | that for "extern template" functions. Therefore, we check |
| 18078 | DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */ |
| 18079 | return ((TREE_CODE (decl) == FUNCTION_DECL |
| 18080 | && DECL_DECLARED_INLINE_P (decl)) |
| 18081 | /* And we need to instantiate static data members so that |
| 18082 | their initializers are available in integral constant |
| 18083 | expressions. */ |
| 18084 | || (TREE_CODE (decl) == VAR_DECL |
| 18085 | && decl_maybe_constant_var_p (decl))); |
| 18086 | } |
| 18087 | |
| 18088 | /* If FN has a noexcept-specifier that hasn't been instantiated yet, |
| 18089 | instantiate it now, modifying TREE_TYPE (fn). */ |
| 18090 | |
| 18091 | void |
| 18092 | maybe_instantiate_noexcept (tree fn) |
| 18093 | { |
| 18094 | tree fntype, spec, noex, clone; |
| 18095 | |
| 18096 | /* Don't instantiate a noexcept-specification from template context. */ |
| 18097 | if (processing_template_decl) |
| 18098 | return; |
| 18099 | |
| 18100 | if (DECL_CLONED_FUNCTION_P (fn)) |
| 18101 | fn = DECL_CLONED_FUNCTION (fn); |
| 18102 | fntype = TREE_TYPE (fn); |
| 18103 | spec = TYPE_RAISES_EXCEPTIONS (fntype); |
| 18104 | |
| 18105 | if (!DEFERRED_NOEXCEPT_SPEC_P (spec)) |
| 18106 | return; |
| 18107 | |
| 18108 | noex = TREE_PURPOSE (spec); |
| 18109 | |
| 18110 | if (TREE_CODE (noex) == DEFERRED_NOEXCEPT) |
| 18111 | { |
| 18112 | if (push_tinst_level (fn)) |
| 18113 | { |
| 18114 | push_access_scope (fn); |
| 18115 | push_deferring_access_checks (dk_no_deferred); |
| 18116 | input_location = DECL_SOURCE_LOCATION (fn); |
| 18117 | noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex), |
| 18118 | DEFERRED_NOEXCEPT_ARGS (noex), |
| 18119 | tf_warning_or_error, fn, |
| 18120 | /*function_p=*/false, |
| 18121 | /*integral_constant_expression_p=*/true); |
| 18122 | pop_deferring_access_checks (); |
| 18123 | pop_access_scope (fn); |
| 18124 | pop_tinst_level (); |
| 18125 | spec = build_noexcept_spec (noex, tf_warning_or_error); |
| 18126 | if (spec == error_mark_node) |
| 18127 | spec = noexcept_false_spec; |
| 18128 | } |
| 18129 | else |
| 18130 | spec = noexcept_false_spec; |
| 18131 | } |
| 18132 | else |
| 18133 | { |
| 18134 | /* This is an implicitly declared function, so NOEX is a list of |
| 18135 | other functions to evaluate and merge. */ |
| 18136 | tree elt; |
| 18137 | spec = noexcept_true_spec; |
| 18138 | for (elt = noex; elt; elt = OVL_NEXT (elt)) |
| 18139 | { |
| 18140 | tree fn = OVL_CURRENT (elt); |
| 18141 | tree subspec; |
| 18142 | maybe_instantiate_noexcept (fn); |
| 18143 | subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); |
| 18144 | spec = merge_exception_specifiers (spec, subspec, NULL_TREE); |
| 18145 | } |
| 18146 | } |
| 18147 | |
| 18148 | TREE_TYPE (fn) = build_exception_variant (fntype, spec); |
| 18149 | |
| 18150 | FOR_EACH_CLONE (clone, fn) |
| 18151 | { |
| 18152 | if (TREE_TYPE (clone) == fntype) |
| 18153 | TREE_TYPE (clone) = TREE_TYPE (fn); |
| 18154 | else |
| 18155 | TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec); |
| 18156 | } |
| 18157 | } |
| 18158 | |
| 18159 | /* Produce the definition of D, a _DECL generated from a template. If |
| 18160 | DEFER_OK is nonzero, then we don't have to actually do the |
| 18161 | instantiation now; we just have to do it sometime. Normally it is |
| 18162 | an error if this is an explicit instantiation but D is undefined. |
| 18163 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
| 18164 | explicitly instantiated class template. */ |
| 18165 | |
| 18166 | tree |
| 18167 | instantiate_decl (tree d, int defer_ok, |
| 18168 | bool expl_inst_class_mem_p) |
| 18169 | { |
| 18170 | tree tmpl = DECL_TI_TEMPLATE (d); |
| 18171 | tree gen_args; |
| 18172 | tree args; |
| 18173 | tree td; |
| 18174 | tree code_pattern; |
| 18175 | tree spec; |
| 18176 | tree gen_tmpl; |
| 18177 | bool pattern_defined; |
| 18178 | location_t saved_loc = input_location; |
| 18179 | bool external_p; |
| 18180 | tree fn_context; |
| 18181 | bool nested; |
| 18182 | |
| 18183 | /* This function should only be used to instantiate templates for |
| 18184 | functions and static member variables. */ |
| 18185 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
| 18186 | || TREE_CODE (d) == VAR_DECL); |
| 18187 | |
| 18188 | /* Variables are never deferred; if instantiation is required, they |
| 18189 | are instantiated right away. That allows for better code in the |
| 18190 | case that an expression refers to the value of the variable -- |
| 18191 | if the variable has a constant value the referring expression can |
| 18192 | take advantage of that fact. */ |
| 18193 | if (TREE_CODE (d) == VAR_DECL |
| 18194 | || decl_function_context (d) |
| 18195 | || DECL_DECLARED_CONSTEXPR_P (d)) |
| 18196 | defer_ok = 0; |
| 18197 | |
| 18198 | /* Don't instantiate cloned functions. Instead, instantiate the |
| 18199 | functions they cloned. */ |
| 18200 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) |
| 18201 | d = DECL_CLONED_FUNCTION (d); |
| 18202 | |
| 18203 | if (DECL_TEMPLATE_INSTANTIATED (d) |
| 18204 | || (TREE_CODE (d) == FUNCTION_DECL |
| 18205 | && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d)) |
| 18206 | || DECL_TEMPLATE_SPECIALIZATION (d)) |
| 18207 | /* D has already been instantiated or explicitly specialized, so |
| 18208 | there's nothing for us to do here. |
| 18209 | |
| 18210 | It might seem reasonable to check whether or not D is an explicit |
| 18211 | instantiation, and, if so, stop here. But when an explicit |
| 18212 | instantiation is deferred until the end of the compilation, |
| 18213 | DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do |
| 18214 | the instantiation. */ |
| 18215 | return d; |
| 18216 | |
| 18217 | /* Check to see whether we know that this template will be |
| 18218 | instantiated in some other file, as with "extern template" |
| 18219 | extension. */ |
| 18220 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); |
| 18221 | |
| 18222 | /* In general, we do not instantiate such templates. */ |
| 18223 | if (external_p && !always_instantiate_p (d)) |
| 18224 | return d; |
| 18225 | |
| 18226 | gen_tmpl = most_general_template (tmpl); |
| 18227 | gen_args = DECL_TI_ARGS (d); |
| 18228 | |
| 18229 | if (tmpl != gen_tmpl) |
| 18230 | /* We should already have the extra args. */ |
| 18231 | gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) |
| 18232 | == TMPL_ARGS_DEPTH (gen_args)); |
| 18233 | /* And what's in the hash table should match D. */ |
| 18234 | gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d |
| 18235 | || spec == NULL_TREE); |
| 18236 | |
| 18237 | /* This needs to happen before any tsubsting. */ |
| 18238 | if (! push_tinst_level (d)) |
| 18239 | return d; |
| 18240 | |
| 18241 | timevar_push (TV_TEMPLATE_INST); |
| 18242 | |
| 18243 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
| 18244 | for the instantiation. */ |
| 18245 | td = template_for_substitution (d); |
| 18246 | code_pattern = DECL_TEMPLATE_RESULT (td); |
| 18247 | |
| 18248 | /* We should never be trying to instantiate a member of a class |
| 18249 | template or partial specialization. */ |
| 18250 | gcc_assert (d != code_pattern); |
| 18251 | |
| 18252 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
| 18253 | || DECL_TEMPLATE_SPECIALIZATION (td)) |
| 18254 | /* In the case of a friend template whose definition is provided |
| 18255 | outside the class, we may have too many arguments. Drop the |
| 18256 | ones we don't need. The same is true for specializations. */ |
| 18257 | args = get_innermost_template_args |
| 18258 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); |
| 18259 | else |
| 18260 | args = gen_args; |
| 18261 | |
| 18262 | if (TREE_CODE (d) == FUNCTION_DECL) |
| 18263 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE |
| 18264 | || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)); |
| 18265 | else |
| 18266 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
| 18267 | |
| 18268 | /* We may be in the middle of deferred access check. Disable it now. */ |
| 18269 | push_deferring_access_checks (dk_no_deferred); |
| 18270 | |
| 18271 | /* Unless an explicit instantiation directive has already determined |
| 18272 | the linkage of D, remember that a definition is available for |
| 18273 | this entity. */ |
| 18274 | if (pattern_defined |
| 18275 | && !DECL_INTERFACE_KNOWN (d) |
| 18276 | && !DECL_NOT_REALLY_EXTERN (d)) |
| 18277 | mark_definable (d); |
| 18278 | |
| 18279 | DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern); |
| 18280 | DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern); |
| 18281 | input_location = DECL_SOURCE_LOCATION (d); |
| 18282 | |
| 18283 | /* If D is a member of an explicitly instantiated class template, |
| 18284 | and no definition is available, treat it like an implicit |
| 18285 | instantiation. */ |
| 18286 | if (!pattern_defined && expl_inst_class_mem_p |
| 18287 | && DECL_EXPLICIT_INSTANTIATION (d)) |
| 18288 | { |
| 18289 | /* Leave linkage flags alone on instantiations with anonymous |
| 18290 | visibility. */ |
| 18291 | if (TREE_PUBLIC (d)) |
| 18292 | { |
| 18293 | DECL_NOT_REALLY_EXTERN (d) = 0; |
| 18294 | DECL_INTERFACE_KNOWN (d) = 0; |
| 18295 | } |
| 18296 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
| 18297 | } |
| 18298 | |
| 18299 | if (TREE_CODE (d) == FUNCTION_DECL) |
| 18300 | maybe_instantiate_noexcept (d); |
| 18301 | |
| 18302 | /* Recheck the substitutions to obtain any warning messages |
| 18303 | about ignoring cv qualifiers. Don't do this for artificial decls, |
| 18304 | as it breaks the context-sensitive substitution for lambda op(). */ |
| 18305 | if (!defer_ok && !DECL_ARTIFICIAL (d)) |
| 18306 | { |
| 18307 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); |
| 18308 | tree type = TREE_TYPE (gen); |
| 18309 | |
| 18310 | /* Make sure that we can see identifiers, and compute access |
| 18311 | correctly. D is already the target FUNCTION_DECL with the |
| 18312 | right context. */ |
| 18313 | push_access_scope (d); |
| 18314 | |
| 18315 | if (TREE_CODE (gen) == FUNCTION_DECL) |
| 18316 | { |
| 18317 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
| 18318 | tsubst_exception_specification (type, gen_args, tf_warning_or_error, |
| 18319 | d, /*defer_ok*/true); |
| 18320 | /* Don't simply tsubst the function type, as that will give |
| 18321 | duplicate warnings about poor parameter qualifications. |
| 18322 | The function arguments are the same as the decl_arguments |
| 18323 | without the top level cv qualifiers. */ |
| 18324 | type = TREE_TYPE (type); |
| 18325 | } |
| 18326 | tsubst (type, gen_args, tf_warning_or_error, d); |
| 18327 | |
| 18328 | pop_access_scope (d); |
| 18329 | } |
| 18330 | |
| 18331 | /* Defer all other templates, unless we have been explicitly |
| 18332 | forbidden from doing so. */ |
| 18333 | if (/* If there is no definition, we cannot instantiate the |
| 18334 | template. */ |
| 18335 | ! pattern_defined |
| 18336 | /* If it's OK to postpone instantiation, do so. */ |
| 18337 | || defer_ok |
| 18338 | /* If this is a static data member that will be defined |
| 18339 | elsewhere, we don't want to instantiate the entire data |
| 18340 | member, but we do want to instantiate the initializer so that |
| 18341 | we can substitute that elsewhere. */ |
| 18342 | || (external_p && TREE_CODE (d) == VAR_DECL)) |
| 18343 | { |
| 18344 | /* The definition of the static data member is now required so |
| 18345 | we must substitute the initializer. */ |
| 18346 | if (TREE_CODE (d) == VAR_DECL |
| 18347 | && !DECL_INITIAL (d) |
| 18348 | && DECL_INITIAL (code_pattern)) |
| 18349 | { |
| 18350 | tree ns; |
| 18351 | tree init; |
| 18352 | bool const_init = false; |
| 18353 | |
| 18354 | ns = decl_namespace_context (d); |
| 18355 | push_nested_namespace (ns); |
| 18356 | push_nested_class (DECL_CONTEXT (d)); |
| 18357 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
| 18358 | args, |
| 18359 | tf_warning_or_error, NULL_TREE, |
| 18360 | /*integral_constant_expression_p=*/false); |
| 18361 | /* Make sure the initializer is still constant, in case of |
| 18362 | circular dependency (template/instantiate6.C). */ |
| 18363 | const_init |
| 18364 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); |
| 18365 | cp_finish_decl (d, init, /*init_const_expr_p=*/const_init, |
| 18366 | /*asmspec_tree=*/NULL_TREE, |
| 18367 | LOOKUP_ONLYCONVERTING); |
| 18368 | pop_nested_class (); |
| 18369 | pop_nested_namespace (ns); |
| 18370 | } |
| 18371 | |
| 18372 | /* We restore the source position here because it's used by |
| 18373 | add_pending_template. */ |
| 18374 | input_location = saved_loc; |
| 18375 | |
| 18376 | if (at_eof && !pattern_defined |
| 18377 | && DECL_EXPLICIT_INSTANTIATION (d) |
| 18378 | && DECL_NOT_REALLY_EXTERN (d)) |
| 18379 | /* [temp.explicit] |
| 18380 | |
| 18381 | The definition of a non-exported function template, a |
| 18382 | non-exported member function template, or a non-exported |
| 18383 | member function or static data member of a class template |
| 18384 | shall be present in every translation unit in which it is |
| 18385 | explicitly instantiated. */ |
| 18386 | permerror (input_location, "explicit instantiation of %qD " |
| 18387 | "but no definition available", d); |
| 18388 | |
| 18389 | /* If we're in unevaluated context, we just wanted to get the |
| 18390 | constant value; this isn't an odr use, so don't queue |
| 18391 | a full instantiation. */ |
| 18392 | if (cp_unevaluated_operand != 0) |
| 18393 | goto out; |
| 18394 | /* ??? Historically, we have instantiated inline functions, even |
| 18395 | when marked as "extern template". */ |
| 18396 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) |
| 18397 | add_pending_template (d); |
| 18398 | goto out; |
| 18399 | } |
| 18400 | /* Tell the repository that D is available in this translation unit |
| 18401 | -- and see if it is supposed to be instantiated here. */ |
| 18402 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) |
| 18403 | { |
| 18404 | /* In a PCH file, despite the fact that the repository hasn't |
| 18405 | requested instantiation in the PCH it is still possible that |
| 18406 | an instantiation will be required in a file that includes the |
| 18407 | PCH. */ |
| 18408 | if (pch_file) |
| 18409 | add_pending_template (d); |
| 18410 | /* Instantiate inline functions so that the inliner can do its |
| 18411 | job, even though we'll not be emitting a copy of this |
| 18412 | function. */ |
| 18413 | if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d))) |
| 18414 | goto out; |
| 18415 | } |
| 18416 | |
| 18417 | fn_context = decl_function_context (d); |
| 18418 | nested = (current_function_decl != NULL_TREE); |
| 18419 | if (!fn_context) |
| 18420 | push_to_top_level (); |
| 18421 | else if (nested) |
| 18422 | push_function_context (); |
| 18423 | |
| 18424 | /* Mark D as instantiated so that recursive calls to |
| 18425 | instantiate_decl do not try to instantiate it again. */ |
| 18426 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
| 18427 | |
| 18428 | /* Regenerate the declaration in case the template has been modified |
| 18429 | by a subsequent redeclaration. */ |
| 18430 | regenerate_decl_from_template (d, td); |
| 18431 | |
| 18432 | /* We already set the file and line above. Reset them now in case |
| 18433 | they changed as a result of calling regenerate_decl_from_template. */ |
| 18434 | input_location = DECL_SOURCE_LOCATION (d); |
| 18435 | |
| 18436 | if (TREE_CODE (d) == VAR_DECL) |
| 18437 | { |
| 18438 | tree init; |
| 18439 | bool const_init = false; |
| 18440 | |
| 18441 | /* Clear out DECL_RTL; whatever was there before may not be right |
| 18442 | since we've reset the type of the declaration. */ |
| 18443 | SET_DECL_RTL (d, NULL); |
| 18444 | DECL_IN_AGGR_P (d) = 0; |
| 18445 | |
| 18446 | /* The initializer is placed in DECL_INITIAL by |
| 18447 | regenerate_decl_from_template so we don't need to |
| 18448 | push/pop_access_scope again here. Pull it out so that |
| 18449 | cp_finish_decl can process it. */ |
| 18450 | init = DECL_INITIAL (d); |
| 18451 | DECL_INITIAL (d) = NULL_TREE; |
| 18452 | DECL_INITIALIZED_P (d) = 0; |
| 18453 | |
| 18454 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
| 18455 | initializer. That function will defer actual emission until |
| 18456 | we have a chance to determine linkage. */ |
| 18457 | DECL_EXTERNAL (d) = 0; |
| 18458 | |
| 18459 | /* Enter the scope of D so that access-checking works correctly. */ |
| 18460 | push_nested_class (DECL_CONTEXT (d)); |
| 18461 | const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); |
| 18462 | cp_finish_decl (d, init, const_init, NULL_TREE, 0); |
| 18463 | pop_nested_class (); |
| 18464 | } |
| 18465 | else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern)) |
| 18466 | synthesize_method (d); |
| 18467 | else if (TREE_CODE (d) == FUNCTION_DECL) |
| 18468 | { |
| 18469 | htab_t saved_local_specializations; |
| 18470 | tree subst_decl; |
| 18471 | tree tmpl_parm; |
| 18472 | tree spec_parm; |
| 18473 | |
| 18474 | /* Save away the current list, in case we are instantiating one |
| 18475 | template from within the body of another. */ |
| 18476 | saved_local_specializations = local_specializations; |
| 18477 | |
| 18478 | /* Set up the list of local specializations. */ |
| 18479 | local_specializations = htab_create (37, |
| 18480 | hash_local_specialization, |
| 18481 | eq_local_specializations, |
| 18482 | NULL); |
| 18483 | |
| 18484 | /* Set up context. */ |
| 18485 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
| 18486 | |
| 18487 | /* Create substitution entries for the parameters. */ |
| 18488 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); |
| 18489 | tmpl_parm = DECL_ARGUMENTS (subst_decl); |
| 18490 | spec_parm = DECL_ARGUMENTS (d); |
| 18491 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) |
| 18492 | { |
| 18493 | register_local_specialization (spec_parm, tmpl_parm); |
| 18494 | spec_parm = skip_artificial_parms_for (d, spec_parm); |
| 18495 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); |
| 18496 | } |
| 18497 | for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm)) |
| 18498 | { |
| 18499 | if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm)) |
| 18500 | { |
| 18501 | register_local_specialization (spec_parm, tmpl_parm); |
| 18502 | spec_parm = DECL_CHAIN (spec_parm); |
| 18503 | } |
| 18504 | else |
| 18505 | { |
| 18506 | /* Register the (value) argument pack as a specialization of |
| 18507 | TMPL_PARM, then move on. */ |
| 18508 | tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm); |
| 18509 | register_local_specialization (argpack, tmpl_parm); |
| 18510 | } |
| 18511 | } |
| 18512 | gcc_assert (!spec_parm); |
| 18513 | |
| 18514 | /* Substitute into the body of the function. */ |
| 18515 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, |
| 18516 | tf_warning_or_error, tmpl, |
| 18517 | /*integral_constant_expression_p=*/false); |
| 18518 | |
| 18519 | /* Set the current input_location to the end of the function |
| 18520 | so that finish_function knows where we are. */ |
| 18521 | input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; |
| 18522 | |
| 18523 | /* We don't need the local specializations any more. */ |
| 18524 | htab_delete (local_specializations); |
| 18525 | local_specializations = saved_local_specializations; |
| 18526 | |
| 18527 | /* Finish the function. */ |
| 18528 | d = finish_function (0); |
| 18529 | expand_or_defer_fn (d); |
| 18530 | } |
| 18531 | |
| 18532 | /* We're not deferring instantiation any more. */ |
| 18533 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; |
| 18534 | |
| 18535 | if (!fn_context) |
| 18536 | pop_from_top_level (); |
| 18537 | else if (nested) |
| 18538 | pop_function_context (); |
| 18539 | |
| 18540 | out: |
| 18541 | input_location = saved_loc; |
| 18542 | pop_deferring_access_checks (); |
| 18543 | pop_tinst_level (); |
| 18544 | |
| 18545 | timevar_pop (TV_TEMPLATE_INST); |
| 18546 | |
| 18547 | return d; |
| 18548 | } |
| 18549 | |
| 18550 | /* Run through the list of templates that we wish we could |
| 18551 | instantiate, and instantiate any we can. RETRIES is the |
| 18552 | number of times we retry pending template instantiation. */ |
| 18553 | |
| 18554 | void |
| 18555 | instantiate_pending_templates (int retries) |
| 18556 | { |
| 18557 | int reconsider; |
| 18558 | location_t saved_loc = input_location; |
| 18559 | |
| 18560 | /* Instantiating templates may trigger vtable generation. This in turn |
| 18561 | may require further template instantiations. We place a limit here |
| 18562 | to avoid infinite loop. */ |
| 18563 | if (pending_templates && retries >= max_tinst_depth) |
| 18564 | { |
| 18565 | tree decl = pending_templates->tinst->decl; |
| 18566 | |
| 18567 | error ("template instantiation depth exceeds maximum of %d" |
| 18568 | " instantiating %q+D, possibly from virtual table generation" |
| 18569 | " (use -ftemplate-depth= to increase the maximum)", |
| 18570 | max_tinst_depth, decl); |
| 18571 | if (TREE_CODE (decl) == FUNCTION_DECL) |
| 18572 | /* Pretend that we defined it. */ |
| 18573 | DECL_INITIAL (decl) = error_mark_node; |
| 18574 | return; |
| 18575 | } |
| 18576 | |
| 18577 | do |
| 18578 | { |
| 18579 | struct pending_template **t = &pending_templates; |
| 18580 | struct pending_template *last = NULL; |
| 18581 | reconsider = 0; |
| 18582 | while (*t) |
| 18583 | { |
| 18584 | tree instantiation = reopen_tinst_level ((*t)->tinst); |
| 18585 | bool complete = false; |
| 18586 | |
| 18587 | if (TYPE_P (instantiation)) |
| 18588 | { |
| 18589 | tree fn; |
| 18590 | |
| 18591 | if (!COMPLETE_TYPE_P (instantiation)) |
| 18592 | { |
| 18593 | instantiate_class_template (instantiation); |
| 18594 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) |
| 18595 | for (fn = TYPE_METHODS (instantiation); |
| 18596 | fn; |
| 18597 | fn = TREE_CHAIN (fn)) |
| 18598 | if (! DECL_ARTIFICIAL (fn)) |
| 18599 | instantiate_decl (fn, |
| 18600 | /*defer_ok=*/0, |
| 18601 | /*expl_inst_class_mem_p=*/false); |
| 18602 | if (COMPLETE_TYPE_P (instantiation)) |
| 18603 | reconsider = 1; |
| 18604 | } |
| 18605 | |
| 18606 | complete = COMPLETE_TYPE_P (instantiation); |
| 18607 | } |
| 18608 | else |
| 18609 | { |
| 18610 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
| 18611 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
| 18612 | { |
| 18613 | instantiation |
| 18614 | = instantiate_decl (instantiation, |
| 18615 | /*defer_ok=*/0, |
| 18616 | /*expl_inst_class_mem_p=*/false); |
| 18617 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
| 18618 | reconsider = 1; |
| 18619 | } |
| 18620 | |
| 18621 | complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
| 18622 | || DECL_TEMPLATE_INSTANTIATED (instantiation)); |
| 18623 | } |
| 18624 | |
| 18625 | if (complete) |
| 18626 | /* If INSTANTIATION has been instantiated, then we don't |
| 18627 | need to consider it again in the future. */ |
| 18628 | *t = (*t)->next; |
| 18629 | else |
| 18630 | { |
| 18631 | last = *t; |
| 18632 | t = &(*t)->next; |
| 18633 | } |
| 18634 | tinst_depth = 0; |
| 18635 | current_tinst_level = NULL; |
| 18636 | } |
| 18637 | last_pending_template = last; |
| 18638 | } |
| 18639 | while (reconsider); |
| 18640 | |
| 18641 | input_location = saved_loc; |
| 18642 | } |
| 18643 | |
| 18644 | /* Substitute ARGVEC into T, which is a list of initializers for |
| 18645 | either base class or a non-static data member. The TREE_PURPOSEs |
| 18646 | are DECLs, and the TREE_VALUEs are the initializer values. Used by |
| 18647 | instantiate_decl. */ |
| 18648 | |
| 18649 | static tree |
| 18650 | tsubst_initializer_list (tree t, tree argvec) |
| 18651 | { |
| 18652 | tree inits = NULL_TREE; |
| 18653 | |
| 18654 | for (; t; t = TREE_CHAIN (t)) |
| 18655 | { |
| 18656 | tree decl; |
| 18657 | tree init; |
| 18658 | tree expanded_bases = NULL_TREE; |
| 18659 | tree expanded_arguments = NULL_TREE; |
| 18660 | int i, len = 1; |
| 18661 | |
| 18662 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
| 18663 | { |
| 18664 | tree expr; |
| 18665 | tree arg; |
| 18666 | |
| 18667 | /* Expand the base class expansion type into separate base |
| 18668 | classes. */ |
| 18669 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, |
| 18670 | tf_warning_or_error, |
| 18671 | NULL_TREE); |
| 18672 | if (expanded_bases == error_mark_node) |
| 18673 | continue; |
| 18674 | |
| 18675 | /* We'll be building separate TREE_LISTs of arguments for |
| 18676 | each base. */ |
| 18677 | len = TREE_VEC_LENGTH (expanded_bases); |
| 18678 | expanded_arguments = make_tree_vec (len); |
| 18679 | for (i = 0; i < len; i++) |
| 18680 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; |
| 18681 | |
| 18682 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to |
| 18683 | expand each argument in the TREE_VALUE of t. */ |
| 18684 | expr = make_node (EXPR_PACK_EXPANSION); |
| 18685 | PACK_EXPANSION_LOCAL_P (expr) = true; |
| 18686 | PACK_EXPANSION_PARAMETER_PACKS (expr) = |
| 18687 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); |
| 18688 | |
| 18689 | if (TREE_VALUE (t) == void_type_node) |
| 18690 | /* VOID_TYPE_NODE is used to indicate |
| 18691 | value-initialization. */ |
| 18692 | { |
| 18693 | for (i = 0; i < len; i++) |
| 18694 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; |
| 18695 | } |
| 18696 | else |
| 18697 | { |
| 18698 | /* Substitute parameter packs into each argument in the |
| 18699 | TREE_LIST. */ |
| 18700 | in_base_initializer = 1; |
| 18701 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) |
| 18702 | { |
| 18703 | tree expanded_exprs; |
| 18704 | |
| 18705 | /* Expand the argument. */ |
| 18706 | SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg)); |
| 18707 | expanded_exprs |
| 18708 | = tsubst_pack_expansion (expr, argvec, |
| 18709 | tf_warning_or_error, |
| 18710 | NULL_TREE); |
| 18711 | if (expanded_exprs == error_mark_node) |
| 18712 | continue; |
| 18713 | |
| 18714 | /* Prepend each of the expanded expressions to the |
| 18715 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ |
| 18716 | for (i = 0; i < len; i++) |
| 18717 | { |
| 18718 | TREE_VEC_ELT (expanded_arguments, i) = |
| 18719 | tree_cons (NULL_TREE, |
| 18720 | TREE_VEC_ELT (expanded_exprs, i), |
| 18721 | TREE_VEC_ELT (expanded_arguments, i)); |
| 18722 | } |
| 18723 | } |
| 18724 | in_base_initializer = 0; |
| 18725 | |
| 18726 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
| 18727 | since we built them backwards. */ |
| 18728 | for (i = 0; i < len; i++) |
| 18729 | { |
| 18730 | TREE_VEC_ELT (expanded_arguments, i) = |
| 18731 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); |
| 18732 | } |
| 18733 | } |
| 18734 | } |
| 18735 | |
| 18736 | for (i = 0; i < len; ++i) |
| 18737 | { |
| 18738 | if (expanded_bases) |
| 18739 | { |
| 18740 | decl = TREE_VEC_ELT (expanded_bases, i); |
| 18741 | decl = expand_member_init (decl); |
| 18742 | init = TREE_VEC_ELT (expanded_arguments, i); |
| 18743 | } |
| 18744 | else |
| 18745 | { |
| 18746 | tree tmp; |
| 18747 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, |
| 18748 | tf_warning_or_error, NULL_TREE); |
| 18749 | |
| 18750 | decl = expand_member_init (decl); |
| 18751 | if (decl && !DECL_P (decl)) |
| 18752 | in_base_initializer = 1; |
| 18753 | |
| 18754 | init = TREE_VALUE (t); |
| 18755 | tmp = init; |
| 18756 | if (init != void_type_node) |
| 18757 | init = tsubst_expr (init, argvec, |
| 18758 | tf_warning_or_error, NULL_TREE, |
| 18759 | /*integral_constant_expression_p=*/false); |
| 18760 | if (init == NULL_TREE && tmp != NULL_TREE) |
| 18761 | /* If we had an initializer but it instantiated to nothing, |
| 18762 | value-initialize the object. This will only occur when |
| 18763 | the initializer was a pack expansion where the parameter |
| 18764 | packs used in that expansion were of length zero. */ |
| 18765 | init = void_type_node; |
| 18766 | in_base_initializer = 0; |
| 18767 | } |
| 18768 | |
| 18769 | if (decl) |
| 18770 | { |
| 18771 | init = build_tree_list (decl, init); |
| 18772 | TREE_CHAIN (init) = inits; |
| 18773 | inits = init; |
| 18774 | } |
| 18775 | } |
| 18776 | } |
| 18777 | return inits; |
| 18778 | } |
| 18779 | |
| 18780 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
| 18781 | |
| 18782 | static void |
| 18783 | set_current_access_from_decl (tree decl) |
| 18784 | { |
| 18785 | if (TREE_PRIVATE (decl)) |
| 18786 | current_access_specifier = access_private_node; |
| 18787 | else if (TREE_PROTECTED (decl)) |
| 18788 | current_access_specifier = access_protected_node; |
| 18789 | else |
| 18790 | current_access_specifier = access_public_node; |
| 18791 | } |
| 18792 | |
| 18793 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
| 18794 | is the instantiation (which should have been created with |
| 18795 | start_enum) and ARGS are the template arguments to use. */ |
| 18796 | |
| 18797 | static void |
| 18798 | tsubst_enum (tree tag, tree newtag, tree args) |
| 18799 | { |
| 18800 | tree e; |
| 18801 | |
| 18802 | if (SCOPED_ENUM_P (newtag)) |
| 18803 | begin_scope (sk_scoped_enum, newtag); |
| 18804 | |
| 18805 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) |
| 18806 | { |
| 18807 | tree value; |
| 18808 | tree decl; |
| 18809 | |
| 18810 | decl = TREE_VALUE (e); |
| 18811 | /* Note that in a template enum, the TREE_VALUE is the |
| 18812 | CONST_DECL, not the corresponding INTEGER_CST. */ |
| 18813 | value = tsubst_expr (DECL_INITIAL (decl), |
| 18814 | args, tf_warning_or_error, NULL_TREE, |
| 18815 | /*integral_constant_expression_p=*/true); |
| 18816 | |
| 18817 | /* Give this enumeration constant the correct access. */ |
| 18818 | set_current_access_from_decl (decl); |
| 18819 | |
| 18820 | /* Actually build the enumerator itself. */ |
| 18821 | build_enumerator |
| 18822 | (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl)); |
| 18823 | } |
| 18824 | |
| 18825 | if (SCOPED_ENUM_P (newtag)) |
| 18826 | finish_scope (); |
| 18827 | |
| 18828 | finish_enum_value_list (newtag); |
| 18829 | finish_enum (newtag); |
| 18830 | |
| 18831 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
| 18832 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); |
| 18833 | } |
| 18834 | |
| 18835 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
| 18836 | its type -- but without substituting the innermost set of template |
| 18837 | arguments. So, innermost set of template parameters will appear in |
| 18838 | the type. */ |
| 18839 | |
| 18840 | tree |
| 18841 | get_mostly_instantiated_function_type (tree decl) |
| 18842 | { |
| 18843 | tree fn_type; |
| 18844 | tree tmpl; |
| 18845 | tree targs; |
| 18846 | tree tparms; |
| 18847 | int parm_depth; |
| 18848 | |
| 18849 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); |
| 18850 | targs = DECL_TI_ARGS (decl); |
| 18851 | tparms = DECL_TEMPLATE_PARMS (tmpl); |
| 18852 | parm_depth = TMPL_PARMS_DEPTH (tparms); |
| 18853 | |
| 18854 | /* There should be as many levels of arguments as there are levels |
| 18855 | of parameters. */ |
| 18856 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
| 18857 | |
| 18858 | fn_type = TREE_TYPE (tmpl); |
| 18859 | |
| 18860 | if (parm_depth == 1) |
| 18861 | /* No substitution is necessary. */ |
| 18862 | ; |
| 18863 | else |
| 18864 | { |
| 18865 | int i; |
| 18866 | tree partial_args; |
| 18867 | |
| 18868 | /* Replace the innermost level of the TARGS with NULL_TREEs to |
| 18869 | let tsubst know not to substitute for those parameters. */ |
| 18870 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
| 18871 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) |
| 18872 | SET_TMPL_ARGS_LEVEL (partial_args, i, |
| 18873 | TMPL_ARGS_LEVEL (targs, i)); |
| 18874 | SET_TMPL_ARGS_LEVEL (partial_args, |
| 18875 | TMPL_ARGS_DEPTH (targs), |
| 18876 | make_tree_vec (DECL_NTPARMS (tmpl))); |
| 18877 | |
| 18878 | /* Make sure that we can see identifiers, and compute access |
| 18879 | correctly. */ |
| 18880 | push_access_scope (decl); |
| 18881 | |
| 18882 | ++processing_template_decl; |
| 18883 | /* Now, do the (partial) substitution to figure out the |
| 18884 | appropriate function type. */ |
| 18885 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
| 18886 | --processing_template_decl; |
| 18887 | |
| 18888 | /* Substitute into the template parameters to obtain the real |
| 18889 | innermost set of parameters. This step is important if the |
| 18890 | innermost set of template parameters contains value |
| 18891 | parameters whose types depend on outer template parameters. */ |
| 18892 | TREE_VEC_LENGTH (partial_args)--; |
| 18893 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
| 18894 | |
| 18895 | pop_access_scope (decl); |
| 18896 | } |
| 18897 | |
| 18898 | return fn_type; |
| 18899 | } |
| 18900 | |
| 18901 | /* Return truthvalue if we're processing a template different from |
| 18902 | the last one involved in diagnostics. */ |
| 18903 | int |
| 18904 | problematic_instantiation_changed (void) |
| 18905 | { |
| 18906 | return current_tinst_level != last_error_tinst_level; |
| 18907 | } |
| 18908 | |
| 18909 | /* Remember current template involved in diagnostics. */ |
| 18910 | void |
| 18911 | record_last_problematic_instantiation (void) |
| 18912 | { |
| 18913 | last_error_tinst_level = current_tinst_level; |
| 18914 | } |
| 18915 | |
| 18916 | struct tinst_level * |
| 18917 | current_instantiation (void) |
| 18918 | { |
| 18919 | return current_tinst_level; |
| 18920 | } |
| 18921 | |
| 18922 | /* [temp.param] Check that template non-type parm TYPE is of an allowable |
| 18923 | type. Return zero for ok, nonzero for disallowed. Issue error and |
| 18924 | warning messages under control of COMPLAIN. */ |
| 18925 | |
| 18926 | static int |
| 18927 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
| 18928 | { |
| 18929 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
| 18930 | return 0; |
| 18931 | else if (POINTER_TYPE_P (type)) |
| 18932 | return 0; |
| 18933 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
| 18934 | return 0; |
| 18935 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
| 18936 | return 0; |
| 18937 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
| 18938 | return 0; |
| 18939 | else if (TREE_CODE (type) == DECLTYPE_TYPE) |
| 18940 | return 0; |
| 18941 | else if (TREE_CODE (type) == NULLPTR_TYPE) |
| 18942 | return 0; |
| 18943 | |
| 18944 | if (complain & tf_error) |
| 18945 | { |
| 18946 | if (type == error_mark_node) |
| 18947 | inform (input_location, "invalid template non-type parameter"); |
| 18948 | else |
| 18949 | error ("%q#T is not a valid type for a template non-type parameter", |
| 18950 | type); |
| 18951 | } |
| 18952 | return 1; |
| 18953 | } |
| 18954 | |
| 18955 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
| 18956 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ |
| 18957 | |
| 18958 | static bool |
| 18959 | dependent_type_p_r (tree type) |
| 18960 | { |
| 18961 | tree scope; |
| 18962 | |
| 18963 | /* [temp.dep.type] |
| 18964 | |
| 18965 | A type is dependent if it is: |
| 18966 | |
| 18967 | -- a template parameter. Template template parameters are types |
| 18968 | for us (since TYPE_P holds true for them) so we handle |
| 18969 | them here. */ |
| 18970 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
| 18971 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
| 18972 | return true; |
| 18973 | /* -- a qualified-id with a nested-name-specifier which contains a |
| 18974 | class-name that names a dependent type or whose unqualified-id |
| 18975 | names a dependent type. */ |
| 18976 | if (TREE_CODE (type) == TYPENAME_TYPE) |
| 18977 | return true; |
| 18978 | /* -- a cv-qualified type where the cv-unqualified type is |
| 18979 | dependent. */ |
| 18980 | type = TYPE_MAIN_VARIANT (type); |
| 18981 | /* -- a compound type constructed from any dependent type. */ |
| 18982 | if (TYPE_PTR_TO_MEMBER_P (type)) |
| 18983 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
| 18984 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
| 18985 | (type))); |
| 18986 | else if (TREE_CODE (type) == POINTER_TYPE |
| 18987 | || TREE_CODE (type) == REFERENCE_TYPE) |
| 18988 | return dependent_type_p (TREE_TYPE (type)); |
| 18989 | else if (TREE_CODE (type) == FUNCTION_TYPE |
| 18990 | || TREE_CODE (type) == METHOD_TYPE) |
| 18991 | { |
| 18992 | tree arg_type; |
| 18993 | |
| 18994 | if (dependent_type_p (TREE_TYPE (type))) |
| 18995 | return true; |
| 18996 | for (arg_type = TYPE_ARG_TYPES (type); |
| 18997 | arg_type; |
| 18998 | arg_type = TREE_CHAIN (arg_type)) |
| 18999 | if (dependent_type_p (TREE_VALUE (arg_type))) |
| 19000 | return true; |
| 19001 | return false; |
| 19002 | } |
| 19003 | /* -- an array type constructed from any dependent type or whose |
| 19004 | size is specified by a constant expression that is |
| 19005 | value-dependent. |
| 19006 | |
| 19007 | We checked for type- and value-dependence of the bounds in |
| 19008 | compute_array_index_type, so TYPE_DEPENDENT_P is already set. */ |
| 19009 | if (TREE_CODE (type) == ARRAY_TYPE) |
| 19010 | { |
| 19011 | if (TYPE_DOMAIN (type) |
| 19012 | && dependent_type_p (TYPE_DOMAIN (type))) |
| 19013 | return true; |
| 19014 | return dependent_type_p (TREE_TYPE (type)); |
| 19015 | } |
| 19016 | |
| 19017 | /* -- a template-id in which either the template name is a template |
| 19018 | parameter ... */ |
| 19019 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) |
| 19020 | return true; |
| 19021 | /* ... or any of the template arguments is a dependent type or |
| 19022 | an expression that is type-dependent or value-dependent. */ |
| 19023 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
| 19024 | && (any_dependent_template_arguments_p |
| 19025 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
| 19026 | return true; |
| 19027 | |
| 19028 | /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are |
| 19029 | dependent; if the argument of the `typeof' expression is not |
| 19030 | type-dependent, then it should already been have resolved. */ |
| 19031 | if (TREE_CODE (type) == TYPEOF_TYPE |
| 19032 | || TREE_CODE (type) == DECLTYPE_TYPE |
| 19033 | || TREE_CODE (type) == UNDERLYING_TYPE) |
| 19034 | return true; |
| 19035 | |
| 19036 | /* A template argument pack is dependent if any of its packed |
| 19037 | arguments are. */ |
| 19038 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) |
| 19039 | { |
| 19040 | tree args = ARGUMENT_PACK_ARGS (type); |
| 19041 | int i, len = TREE_VEC_LENGTH (args); |
| 19042 | for (i = 0; i < len; ++i) |
| 19043 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) |
| 19044 | return true; |
| 19045 | } |
| 19046 | |
| 19047 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must |
| 19048 | be template parameters. */ |
| 19049 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION) |
| 19050 | return true; |
| 19051 | |
| 19052 | /* The standard does not specifically mention types that are local |
| 19053 | to template functions or local classes, but they should be |
| 19054 | considered dependent too. For example: |
| 19055 | |
| 19056 | template <int I> void f() { |
| 19057 | enum E { a = I }; |
| 19058 | S<sizeof (E)> s; |
| 19059 | } |
| 19060 | |
| 19061 | The size of `E' cannot be known until the value of `I' has been |
| 19062 | determined. Therefore, `E' must be considered dependent. */ |
| 19063 | scope = TYPE_CONTEXT (type); |
| 19064 | if (scope && TYPE_P (scope)) |
| 19065 | return dependent_type_p (scope); |
| 19066 | /* Don't use type_dependent_expression_p here, as it can lead |
| 19067 | to infinite recursion trying to determine whether a lambda |
| 19068 | nested in a lambda is dependent (c++/47687). */ |
| 19069 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL |
| 19070 | && DECL_LANG_SPECIFIC (scope) |
| 19071 | && DECL_TEMPLATE_INFO (scope) |
| 19072 | && (any_dependent_template_arguments_p |
| 19073 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope))))) |
| 19074 | return true; |
| 19075 | |
| 19076 | /* Other types are non-dependent. */ |
| 19077 | return false; |
| 19078 | } |
| 19079 | |
| 19080 | /* Returns TRUE if TYPE is dependent, in the sense of |
| 19081 | [temp.dep.type]. Note that a NULL type is considered dependent. */ |
| 19082 | |
| 19083 | bool |
| 19084 | dependent_type_p (tree type) |
| 19085 | { |
| 19086 | /* If there are no template parameters in scope, then there can't be |
| 19087 | any dependent types. */ |
| 19088 | if (!processing_template_decl) |
| 19089 | { |
| 19090 | /* If we are not processing a template, then nobody should be |
| 19091 | providing us with a dependent type. */ |
| 19092 | gcc_assert (type); |
| 19093 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)); |
| 19094 | return false; |
| 19095 | } |
| 19096 | |
| 19097 | /* If the type is NULL, we have not computed a type for the entity |
| 19098 | in question; in that case, the type is dependent. */ |
| 19099 | if (!type) |
| 19100 | return true; |
| 19101 | |
| 19102 | /* Erroneous types can be considered non-dependent. */ |
| 19103 | if (type == error_mark_node) |
| 19104 | return false; |
| 19105 | |
| 19106 | /* If we have not already computed the appropriate value for TYPE, |
| 19107 | do so now. */ |
| 19108 | if (!TYPE_DEPENDENT_P_VALID (type)) |
| 19109 | { |
| 19110 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); |
| 19111 | TYPE_DEPENDENT_P_VALID (type) = 1; |
| 19112 | } |
| 19113 | |
| 19114 | return TYPE_DEPENDENT_P (type); |
| 19115 | } |
| 19116 | |
| 19117 | /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any |
| 19118 | lookup. In other words, a dependent type that is not the current |
| 19119 | instantiation. */ |
| 19120 | |
| 19121 | bool |
| 19122 | dependent_scope_p (tree scope) |
| 19123 | { |
| 19124 | return (scope && TYPE_P (scope) && dependent_type_p (scope) |
| 19125 | && !currently_open_class (scope)); |
| 19126 | } |
| 19127 | |
| 19128 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
| 19129 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
| 19130 | expression. */ |
| 19131 | |
| 19132 | /* Note that this predicate is not appropriate for general expressions; |
| 19133 | only constant expressions (that satisfy potential_constant_expression) |
| 19134 | can be tested for value dependence. |
| 19135 | |
| 19136 | We should really also have a predicate for "instantiation-dependent". |
| 19137 | |
| 19138 | fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent. |
| 19139 | (what about instantiation-dependent constant-expressions?) |
| 19140 | is_late_template_attribute: defer if instantiation-dependent. |
| 19141 | compute_array_index_type: proceed if constant and not t- or v-dependent |
| 19142 | if instantiation-dependent, need to remember full expression |
| 19143 | uses_template_parms: FIXME - need to audit callers |
| 19144 | tsubst_decl [function_decl]: Why is this using value_dependent_expression_p? |
| 19145 | dependent_type_p [array_type]: dependent if index type is dependent |
| 19146 | (or non-constant?) |
| 19147 | static_assert - instantiation-dependent */ |
| 19148 | |
| 19149 | bool |
| 19150 | value_dependent_expression_p (tree expression) |
| 19151 | { |
| 19152 | if (!processing_template_decl) |
| 19153 | return false; |
| 19154 | |
| 19155 | /* A name declared with a dependent type. */ |
| 19156 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
| 19157 | return true; |
| 19158 | |
| 19159 | switch (TREE_CODE (expression)) |
| 19160 | { |
| 19161 | case IDENTIFIER_NODE: |
| 19162 | /* A name that has not been looked up -- must be dependent. */ |
| 19163 | return true; |
| 19164 | |
| 19165 | case TEMPLATE_PARM_INDEX: |
| 19166 | /* A non-type template parm. */ |
| 19167 | return true; |
| 19168 | |
| 19169 | case CONST_DECL: |
| 19170 | /* A non-type template parm. */ |
| 19171 | if (DECL_TEMPLATE_PARM_P (expression)) |
| 19172 | return true; |
| 19173 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
| 19174 | |
| 19175 | case VAR_DECL: |
| 19176 | /* A constant with literal type and is initialized |
| 19177 | with an expression that is value-dependent. |
| 19178 | |
| 19179 | Note that a non-dependent parenthesized initializer will have |
| 19180 | already been replaced with its constant value, so if we see |
| 19181 | a TREE_LIST it must be dependent. */ |
| 19182 | if (DECL_INITIAL (expression) |
| 19183 | && decl_constant_var_p (expression) |
| 19184 | && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST |
| 19185 | || value_dependent_expression_p (DECL_INITIAL (expression)))) |
| 19186 | return true; |
| 19187 | return false; |
| 19188 | |
| 19189 | case DYNAMIC_CAST_EXPR: |
| 19190 | case STATIC_CAST_EXPR: |
| 19191 | case CONST_CAST_EXPR: |
| 19192 | case REINTERPRET_CAST_EXPR: |
| 19193 | case CAST_EXPR: |
| 19194 | /* These expressions are value-dependent if the type to which |
| 19195 | the cast occurs is dependent or the expression being casted |
| 19196 | is value-dependent. */ |
| 19197 | { |
| 19198 | tree type = TREE_TYPE (expression); |
| 19199 | |
| 19200 | if (dependent_type_p (type)) |
| 19201 | return true; |
| 19202 | |
| 19203 | /* A functional cast has a list of operands. */ |
| 19204 | expression = TREE_OPERAND (expression, 0); |
| 19205 | if (!expression) |
| 19206 | { |
| 19207 | /* If there are no operands, it must be an expression such |
| 19208 | as "int()". This should not happen for aggregate types |
| 19209 | because it would form non-constant expressions. */ |
| 19210 | gcc_assert (cxx_dialect >= cxx0x |
| 19211 | || INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
| 19212 | |
| 19213 | return false; |
| 19214 | } |
| 19215 | |
| 19216 | if (TREE_CODE (expression) == TREE_LIST) |
| 19217 | return any_value_dependent_elements_p (expression); |
| 19218 | |
| 19219 | return value_dependent_expression_p (expression); |
| 19220 | } |
| 19221 | |
| 19222 | case SIZEOF_EXPR: |
| 19223 | case ALIGNOF_EXPR: |
| 19224 | case TYPEID_EXPR: |
| 19225 | /* A `sizeof' expression is value-dependent if the operand is |
| 19226 | type-dependent or is a pack expansion. */ |
| 19227 | expression = TREE_OPERAND (expression, 0); |
| 19228 | if (PACK_EXPANSION_P (expression)) |
| 19229 | return true; |
| 19230 | else if (TYPE_P (expression)) |
| 19231 | return dependent_type_p (expression); |
| 19232 | return type_dependent_expression_p (expression); |
| 19233 | |
| 19234 | case AT_ENCODE_EXPR: |
| 19235 | /* An 'encode' expression is value-dependent if the operand is |
| 19236 | type-dependent. */ |
| 19237 | expression = TREE_OPERAND (expression, 0); |
| 19238 | return dependent_type_p (expression); |
| 19239 | |
| 19240 | case NOEXCEPT_EXPR: |
| 19241 | expression = TREE_OPERAND (expression, 0); |
| 19242 | return type_dependent_expression_p (expression); |
| 19243 | |
| 19244 | case SCOPE_REF: |
| 19245 | { |
| 19246 | tree name = TREE_OPERAND (expression, 1); |
| 19247 | return value_dependent_expression_p (name); |
| 19248 | } |
| 19249 | |
| 19250 | case COMPONENT_REF: |
| 19251 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) |
| 19252 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); |
| 19253 | |
| 19254 | case NONTYPE_ARGUMENT_PACK: |
| 19255 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument |
| 19256 | is value-dependent. */ |
| 19257 | { |
| 19258 | tree values = ARGUMENT_PACK_ARGS (expression); |
| 19259 | int i, len = TREE_VEC_LENGTH (values); |
| 19260 | |
| 19261 | for (i = 0; i < len; ++i) |
| 19262 | if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) |
| 19263 | return true; |
| 19264 | |
| 19265 | return false; |
| 19266 | } |
| 19267 | |
| 19268 | case TRAIT_EXPR: |
| 19269 | { |
| 19270 | tree type2 = TRAIT_EXPR_TYPE2 (expression); |
| 19271 | return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)) |
| 19272 | || (type2 ? dependent_type_p (type2) : false)); |
| 19273 | } |
| 19274 | |
| 19275 | case MODOP_EXPR: |
| 19276 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) |
| 19277 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); |
| 19278 | |
| 19279 | case ARRAY_REF: |
| 19280 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) |
| 19281 | || (value_dependent_expression_p (TREE_OPERAND (expression, 1)))); |
| 19282 | |
| 19283 | case ADDR_EXPR: |
| 19284 | { |
| 19285 | tree op = TREE_OPERAND (expression, 0); |
| 19286 | return (value_dependent_expression_p (op) |
| 19287 | || has_value_dependent_address (op)); |
| 19288 | } |
| 19289 | |
| 19290 | case CALL_EXPR: |
| 19291 | { |
| 19292 | tree fn = get_callee_fndecl (expression); |
| 19293 | int i, nargs; |
| 19294 | if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression))) |
| 19295 | return true; |
| 19296 | nargs = call_expr_nargs (expression); |
| 19297 | for (i = 0; i < nargs; ++i) |
| 19298 | { |
| 19299 | tree op = CALL_EXPR_ARG (expression, i); |
| 19300 | /* In a call to a constexpr member function, look through the |
| 19301 | implicit ADDR_EXPR on the object argument so that it doesn't |
| 19302 | cause the call to be considered value-dependent. We also |
| 19303 | look through it in potential_constant_expression. */ |
| 19304 | if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn) |
| 19305 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) |
| 19306 | && TREE_CODE (op) == ADDR_EXPR) |
| 19307 | op = TREE_OPERAND (op, 0); |
| 19308 | if (value_dependent_expression_p (op)) |
| 19309 | return true; |
| 19310 | } |
| 19311 | return false; |
| 19312 | } |
| 19313 | |
| 19314 | case TEMPLATE_ID_EXPR: |
| 19315 | /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be |
| 19316 | type-dependent. */ |
| 19317 | return type_dependent_expression_p (expression); |
| 19318 | |
| 19319 | case CONSTRUCTOR: |
| 19320 | { |
| 19321 | unsigned ix; |
| 19322 | tree val; |
| 19323 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val) |
| 19324 | if (value_dependent_expression_p (val)) |
| 19325 | return true; |
| 19326 | return false; |
| 19327 | } |
| 19328 | |
| 19329 | case STMT_EXPR: |
| 19330 | /* Treat a GNU statement expression as dependent to avoid crashing |
| 19331 | under fold_non_dependent_expr; it can't be constant. */ |
| 19332 | return true; |
| 19333 | |
| 19334 | default: |
| 19335 | /* A constant expression is value-dependent if any subexpression is |
| 19336 | value-dependent. */ |
| 19337 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
| 19338 | { |
| 19339 | case tcc_reference: |
| 19340 | case tcc_unary: |
| 19341 | case tcc_comparison: |
| 19342 | case tcc_binary: |
| 19343 | case tcc_expression: |
| 19344 | case tcc_vl_exp: |
| 19345 | { |
| 19346 | int i, len = cp_tree_operand_length (expression); |
| 19347 | |
| 19348 | for (i = 0; i < len; i++) |
| 19349 | { |
| 19350 | tree t = TREE_OPERAND (expression, i); |
| 19351 | |
| 19352 | /* In some cases, some of the operands may be missing.l |
| 19353 | (For example, in the case of PREDECREMENT_EXPR, the |
| 19354 | amount to increment by may be missing.) That doesn't |
| 19355 | make the expression dependent. */ |
| 19356 | if (t && value_dependent_expression_p (t)) |
| 19357 | return true; |
| 19358 | } |
| 19359 | } |
| 19360 | break; |
| 19361 | default: |
| 19362 | break; |
| 19363 | } |
| 19364 | break; |
| 19365 | } |
| 19366 | |
| 19367 | /* The expression is not value-dependent. */ |
| 19368 | return false; |
| 19369 | } |
| 19370 | |
| 19371 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of |
| 19372 | [temp.dep.expr]. Note that an expression with no type is |
| 19373 | considered dependent. Other parts of the compiler arrange for an |
| 19374 | expression with type-dependent subexpressions to have no type, so |
| 19375 | this function doesn't have to be fully recursive. */ |
| 19376 | |
| 19377 | bool |
| 19378 | type_dependent_expression_p (tree expression) |
| 19379 | { |
| 19380 | if (!processing_template_decl) |
| 19381 | return false; |
| 19382 | |
| 19383 | if (expression == error_mark_node) |
| 19384 | return false; |
| 19385 | |
| 19386 | /* An unresolved name is always dependent. */ |
| 19387 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
| 19388 | || TREE_CODE (expression) == USING_DECL) |
| 19389 | return true; |
| 19390 | |
| 19391 | /* Some expression forms are never type-dependent. */ |
| 19392 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR |
| 19393 | || TREE_CODE (expression) == SIZEOF_EXPR |
| 19394 | || TREE_CODE (expression) == ALIGNOF_EXPR |
| 19395 | || TREE_CODE (expression) == AT_ENCODE_EXPR |
| 19396 | || TREE_CODE (expression) == NOEXCEPT_EXPR |
| 19397 | || TREE_CODE (expression) == TRAIT_EXPR |
| 19398 | || TREE_CODE (expression) == TYPEID_EXPR |
| 19399 | || TREE_CODE (expression) == DELETE_EXPR |
| 19400 | || TREE_CODE (expression) == VEC_DELETE_EXPR |
| 19401 | || TREE_CODE (expression) == THROW_EXPR) |
| 19402 | return false; |
| 19403 | |
| 19404 | /* The types of these expressions depends only on the type to which |
| 19405 | the cast occurs. */ |
| 19406 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR |
| 19407 | || TREE_CODE (expression) == STATIC_CAST_EXPR |
| 19408 | || TREE_CODE (expression) == CONST_CAST_EXPR |
| 19409 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR |
| 19410 | || TREE_CODE (expression) == IMPLICIT_CONV_EXPR |
| 19411 | || TREE_CODE (expression) == CAST_EXPR) |
| 19412 | return dependent_type_p (TREE_TYPE (expression)); |
| 19413 | |
| 19414 | /* The types of these expressions depends only on the type created |
| 19415 | by the expression. */ |
| 19416 | if (TREE_CODE (expression) == NEW_EXPR |
| 19417 | || TREE_CODE (expression) == VEC_NEW_EXPR) |
| 19418 | { |
| 19419 | /* For NEW_EXPR tree nodes created inside a template, either |
| 19420 | the object type itself or a TREE_LIST may appear as the |
| 19421 | operand 1. */ |
| 19422 | tree type = TREE_OPERAND (expression, 1); |
| 19423 | if (TREE_CODE (type) == TREE_LIST) |
| 19424 | /* This is an array type. We need to check array dimensions |
| 19425 | as well. */ |
| 19426 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) |
| 19427 | || value_dependent_expression_p |
| 19428 | (TREE_OPERAND (TREE_VALUE (type), 1)); |
| 19429 | else |
| 19430 | return dependent_type_p (type); |
| 19431 | } |
| 19432 | |
| 19433 | if (TREE_CODE (expression) == SCOPE_REF) |
| 19434 | { |
| 19435 | tree scope = TREE_OPERAND (expression, 0); |
| 19436 | tree name = TREE_OPERAND (expression, 1); |
| 19437 | |
| 19438 | /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it |
| 19439 | contains an identifier associated by name lookup with one or more |
| 19440 | declarations declared with a dependent type, or...a |
| 19441 | nested-name-specifier or qualified-id that names a member of an |
| 19442 | unknown specialization. */ |
| 19443 | return (type_dependent_expression_p (name) |
| 19444 | || dependent_scope_p (scope)); |
| 19445 | } |
| 19446 | |
| 19447 | if (TREE_CODE (expression) == FUNCTION_DECL |
| 19448 | && DECL_LANG_SPECIFIC (expression) |
| 19449 | && DECL_TEMPLATE_INFO (expression) |
| 19450 | && (any_dependent_template_arguments_p |
| 19451 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) |
| 19452 | return true; |
| 19453 | |
| 19454 | if (TREE_CODE (expression) == TEMPLATE_DECL |
| 19455 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) |
| 19456 | return false; |
| 19457 | |
| 19458 | if (TREE_CODE (expression) == STMT_EXPR) |
| 19459 | expression = stmt_expr_value_expr (expression); |
| 19460 | |
| 19461 | if (BRACE_ENCLOSED_INITIALIZER_P (expression)) |
| 19462 | { |
| 19463 | tree elt; |
| 19464 | unsigned i; |
| 19465 | |
| 19466 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt) |
| 19467 | { |
| 19468 | if (type_dependent_expression_p (elt)) |
| 19469 | return true; |
| 19470 | } |
| 19471 | return false; |
| 19472 | } |
| 19473 | |
| 19474 | /* A static data member of the current instantiation with incomplete |
| 19475 | array type is type-dependent, as the definition and specializations |
| 19476 | can have different bounds. */ |
| 19477 | if (TREE_CODE (expression) == VAR_DECL |
| 19478 | && DECL_CLASS_SCOPE_P (expression) |
| 19479 | && dependent_type_p (DECL_CONTEXT (expression)) |
| 19480 | && VAR_HAD_UNKNOWN_BOUND (expression)) |
| 19481 | return true; |
| 19482 | |
| 19483 | if (TREE_TYPE (expression) == unknown_type_node) |
| 19484 | { |
| 19485 | if (TREE_CODE (expression) == ADDR_EXPR) |
| 19486 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); |
| 19487 | if (TREE_CODE (expression) == COMPONENT_REF |
| 19488 | || TREE_CODE (expression) == OFFSET_REF) |
| 19489 | { |
| 19490 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) |
| 19491 | return true; |
| 19492 | expression = TREE_OPERAND (expression, 1); |
| 19493 | if (TREE_CODE (expression) == IDENTIFIER_NODE) |
| 19494 | return false; |
| 19495 | } |
| 19496 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
| 19497 | if (TREE_CODE (expression) == SCOPE_REF) |
| 19498 | return false; |
| 19499 | |
| 19500 | if (BASELINK_P (expression)) |
| 19501 | expression = BASELINK_FUNCTIONS (expression); |
| 19502 | |
| 19503 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
| 19504 | { |
| 19505 | if (any_dependent_template_arguments_p |
| 19506 | (TREE_OPERAND (expression, 1))) |
| 19507 | return true; |
| 19508 | expression = TREE_OPERAND (expression, 0); |
| 19509 | } |
| 19510 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
| 19511 | || TREE_CODE (expression) == FUNCTION_DECL); |
| 19512 | |
| 19513 | while (expression) |
| 19514 | { |
| 19515 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
| 19516 | return true; |
| 19517 | expression = OVL_NEXT (expression); |
| 19518 | } |
| 19519 | return false; |
| 19520 | } |
| 19521 | |
| 19522 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
| 19523 | |
| 19524 | return (dependent_type_p (TREE_TYPE (expression))); |
| 19525 | } |
| 19526 | |
| 19527 | /* Like type_dependent_expression_p, but it also works while not processing |
| 19528 | a template definition, i.e. during substitution or mangling. */ |
| 19529 | |
| 19530 | bool |
| 19531 | type_dependent_expression_p_push (tree expr) |
| 19532 | { |
| 19533 | bool b; |
| 19534 | ++processing_template_decl; |
| 19535 | b = type_dependent_expression_p (expr); |
| 19536 | --processing_template_decl; |
| 19537 | return b; |
| 19538 | } |
| 19539 | |
| 19540 | /* Returns TRUE if ARGS contains a type-dependent expression. */ |
| 19541 | |
| 19542 | bool |
| 19543 | any_type_dependent_arguments_p (const VEC(tree,gc) *args) |
| 19544 | { |
| 19545 | unsigned int i; |
| 19546 | tree arg; |
| 19547 | |
| 19548 | FOR_EACH_VEC_ELT (tree, args, i, arg) |
| 19549 | { |
| 19550 | if (type_dependent_expression_p (arg)) |
| 19551 | return true; |
| 19552 | } |
| 19553 | return false; |
| 19554 | } |
| 19555 | |
| 19556 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
| 19557 | expressions) contains any type-dependent expressions. */ |
| 19558 | |
| 19559 | bool |
| 19560 | any_type_dependent_elements_p (const_tree list) |
| 19561 | { |
| 19562 | for (; list; list = TREE_CHAIN (list)) |
| 19563 | if (type_dependent_expression_p (TREE_VALUE (list))) |
| 19564 | return true; |
| 19565 | |
| 19566 | return false; |
| 19567 | } |
| 19568 | |
| 19569 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
| 19570 | expressions) contains any value-dependent expressions. */ |
| 19571 | |
| 19572 | bool |
| 19573 | any_value_dependent_elements_p (const_tree list) |
| 19574 | { |
| 19575 | for (; list; list = TREE_CHAIN (list)) |
| 19576 | if (value_dependent_expression_p (TREE_VALUE (list))) |
| 19577 | return true; |
| 19578 | |
| 19579 | return false; |
| 19580 | } |
| 19581 | |
| 19582 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
| 19583 | |
| 19584 | bool |
| 19585 | dependent_template_arg_p (tree arg) |
| 19586 | { |
| 19587 | if (!processing_template_decl) |
| 19588 | return false; |
| 19589 | |
| 19590 | /* Assume a template argument that was wrongly written by the user |
| 19591 | is dependent. This is consistent with what |
| 19592 | any_dependent_template_arguments_p [that calls this function] |
| 19593 | does. */ |
| 19594 | if (!arg || arg == error_mark_node) |
| 19595 | return true; |
| 19596 | |
| 19597 | if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
| 19598 | arg = ARGUMENT_PACK_SELECT_ARG (arg); |
| 19599 | |
| 19600 | if (TREE_CODE (arg) == TEMPLATE_DECL |
| 19601 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
| 19602 | return dependent_template_p (arg); |
| 19603 | else if (ARGUMENT_PACK_P (arg)) |
| 19604 | { |
| 19605 | tree args = ARGUMENT_PACK_ARGS (arg); |
| 19606 | int i, len = TREE_VEC_LENGTH (args); |
| 19607 | for (i = 0; i < len; ++i) |
| 19608 | { |
| 19609 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) |
| 19610 | return true; |
| 19611 | } |
| 19612 | |
| 19613 | return false; |
| 19614 | } |
| 19615 | else if (TYPE_P (arg)) |
| 19616 | return dependent_type_p (arg); |
| 19617 | else |
| 19618 | return (type_dependent_expression_p (arg) |
| 19619 | || value_dependent_expression_p (arg)); |
| 19620 | } |
| 19621 | |
| 19622 | /* Returns true if ARGS (a collection of template arguments) contains |
| 19623 | any types that require structural equality testing. */ |
| 19624 | |
| 19625 | bool |
| 19626 | any_template_arguments_need_structural_equality_p (tree args) |
| 19627 | { |
| 19628 | int i; |
| 19629 | int j; |
| 19630 | |
| 19631 | if (!args) |
| 19632 | return false; |
| 19633 | if (args == error_mark_node) |
| 19634 | return true; |
| 19635 | |
| 19636 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
| 19637 | { |
| 19638 | tree level = TMPL_ARGS_LEVEL (args, i + 1); |
| 19639 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
| 19640 | { |
| 19641 | tree arg = TREE_VEC_ELT (level, j); |
| 19642 | tree packed_args = NULL_TREE; |
| 19643 | int k, len = 1; |
| 19644 | |
| 19645 | if (ARGUMENT_PACK_P (arg)) |
| 19646 | { |
| 19647 | /* Look inside the argument pack. */ |
| 19648 | packed_args = ARGUMENT_PACK_ARGS (arg); |
| 19649 | len = TREE_VEC_LENGTH (packed_args); |
| 19650 | } |
| 19651 | |
| 19652 | for (k = 0; k < len; ++k) |
| 19653 | { |
| 19654 | if (packed_args) |
| 19655 | arg = TREE_VEC_ELT (packed_args, k); |
| 19656 | |
| 19657 | if (error_operand_p (arg)) |
| 19658 | return true; |
| 19659 | else if (TREE_CODE (arg) == TEMPLATE_DECL |
| 19660 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
| 19661 | continue; |
| 19662 | else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg)) |
| 19663 | return true; |
| 19664 | else if (!TYPE_P (arg) && TREE_TYPE (arg) |
| 19665 | && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg))) |
| 19666 | return true; |
| 19667 | } |
| 19668 | } |
| 19669 | } |
| 19670 | |
| 19671 | return false; |
| 19672 | } |
| 19673 | |
| 19674 | /* Returns true if ARGS (a collection of template arguments) contains |
| 19675 | any dependent arguments. */ |
| 19676 | |
| 19677 | bool |
| 19678 | any_dependent_template_arguments_p (const_tree args) |
| 19679 | { |
| 19680 | int i; |
| 19681 | int j; |
| 19682 | |
| 19683 | if (!args) |
| 19684 | return false; |
| 19685 | if (args == error_mark_node) |
| 19686 | return true; |
| 19687 | |
| 19688 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
| 19689 | { |
| 19690 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
| 19691 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
| 19692 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) |
| 19693 | return true; |
| 19694 | } |
| 19695 | |
| 19696 | return false; |
| 19697 | } |
| 19698 | |
| 19699 | /* Returns TRUE if the template TMPL is dependent. */ |
| 19700 | |
| 19701 | bool |
| 19702 | dependent_template_p (tree tmpl) |
| 19703 | { |
| 19704 | if (TREE_CODE (tmpl) == OVERLOAD) |
| 19705 | { |
| 19706 | while (tmpl) |
| 19707 | { |
| 19708 | if (dependent_template_p (OVL_CURRENT (tmpl))) |
| 19709 | return true; |
| 19710 | tmpl = OVL_NEXT (tmpl); |
| 19711 | } |
| 19712 | return false; |
| 19713 | } |
| 19714 | |
| 19715 | /* Template template parameters are dependent. */ |
| 19716 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) |
| 19717 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) |
| 19718 | return true; |
| 19719 | /* So are names that have not been looked up. */ |
| 19720 | if (TREE_CODE (tmpl) == SCOPE_REF |
| 19721 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) |
| 19722 | return true; |
| 19723 | /* So are member templates of dependent classes. */ |
| 19724 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) |
| 19725 | return dependent_type_p (DECL_CONTEXT (tmpl)); |
| 19726 | return false; |
| 19727 | } |
| 19728 | |
| 19729 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
| 19730 | |
| 19731 | bool |
| 19732 | dependent_template_id_p (tree tmpl, tree args) |
| 19733 | { |
| 19734 | return (dependent_template_p (tmpl) |
| 19735 | || any_dependent_template_arguments_p (args)); |
| 19736 | } |
| 19737 | |
| 19738 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
| 19739 | is dependent. */ |
| 19740 | |
| 19741 | bool |
| 19742 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) |
| 19743 | { |
| 19744 | int i; |
| 19745 | |
| 19746 | if (!processing_template_decl) |
| 19747 | return false; |
| 19748 | |
| 19749 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) |
| 19750 | { |
| 19751 | tree decl = TREE_VEC_ELT (declv, i); |
| 19752 | tree init = TREE_VEC_ELT (initv, i); |
| 19753 | tree cond = TREE_VEC_ELT (condv, i); |
| 19754 | tree incr = TREE_VEC_ELT (incrv, i); |
| 19755 | |
| 19756 | if (type_dependent_expression_p (decl)) |
| 19757 | return true; |
| 19758 | |
| 19759 | if (init && type_dependent_expression_p (init)) |
| 19760 | return true; |
| 19761 | |
| 19762 | if (type_dependent_expression_p (cond)) |
| 19763 | return true; |
| 19764 | |
| 19765 | if (COMPARISON_CLASS_P (cond) |
| 19766 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) |
| 19767 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) |
| 19768 | return true; |
| 19769 | |
| 19770 | if (TREE_CODE (incr) == MODOP_EXPR) |
| 19771 | { |
| 19772 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) |
| 19773 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) |
| 19774 | return true; |
| 19775 | } |
| 19776 | else if (type_dependent_expression_p (incr)) |
| 19777 | return true; |
| 19778 | else if (TREE_CODE (incr) == MODIFY_EXPR) |
| 19779 | { |
| 19780 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) |
| 19781 | return true; |
| 19782 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) |
| 19783 | { |
| 19784 | tree t = TREE_OPERAND (incr, 1); |
| 19785 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) |
| 19786 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) |
| 19787 | return true; |
| 19788 | } |
| 19789 | } |
| 19790 | } |
| 19791 | |
| 19792 | return false; |
| 19793 | } |
| 19794 | |
| 19795 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
| 19796 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
| 19797 | no such TYPE can be found. Note that this function peers inside |
| 19798 | uninstantiated templates and therefore should be used only in |
| 19799 | extremely limited situations. ONLY_CURRENT_P restricts this |
| 19800 | peering to the currently open classes hierarchy (which is required |
| 19801 | when comparing types). */ |
| 19802 | |
| 19803 | tree |
| 19804 | resolve_typename_type (tree type, bool only_current_p) |
| 19805 | { |
| 19806 | tree scope; |
| 19807 | tree name; |
| 19808 | tree decl; |
| 19809 | int quals; |
| 19810 | tree pushed_scope; |
| 19811 | tree result; |
| 19812 | |
| 19813 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
| 19814 | |
| 19815 | scope = TYPE_CONTEXT (type); |
| 19816 | /* Usually the non-qualified identifier of a TYPENAME_TYPE is |
| 19817 | TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of |
| 19818 | a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing |
| 19819 | the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified |
| 19820 | identifier of the TYPENAME_TYPE anymore. |
| 19821 | So by getting the TYPE_IDENTIFIER of the _main declaration_ of the |
| 19822 | TYPENAME_TYPE instead, we avoid messing up with a possible |
| 19823 | typedef variant case. */ |
| 19824 | name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); |
| 19825 | |
| 19826 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve |
| 19827 | it first before we can figure out what NAME refers to. */ |
| 19828 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
| 19829 | { |
| 19830 | if (TYPENAME_IS_RESOLVING_P (scope)) |
| 19831 | /* Given a class template A with a dependent base with nested type C, |
| 19832 | typedef typename A::C::C C will land us here, as trying to resolve |
| 19833 | the initial A::C leads to the local C typedef, which leads back to |
| 19834 | A::C::C. So we break the recursion now. */ |
| 19835 | return type; |
| 19836 | else |
| 19837 | scope = resolve_typename_type (scope, only_current_p); |
| 19838 | } |
| 19839 | /* If we don't know what SCOPE refers to, then we cannot resolve the |
| 19840 | TYPENAME_TYPE. */ |
| 19841 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
| 19842 | return type; |
| 19843 | /* If the SCOPE is a template type parameter, we have no way of |
| 19844 | resolving the name. */ |
| 19845 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) |
| 19846 | return type; |
| 19847 | /* If the SCOPE is not the current instantiation, there's no reason |
| 19848 | to look inside it. */ |
| 19849 | if (only_current_p && !currently_open_class (scope)) |
| 19850 | return type; |
| 19851 | /* If this is a typedef, we don't want to look inside (c++/11987). */ |
| 19852 | if (typedef_variant_p (type)) |
| 19853 | return type; |
| 19854 | /* If SCOPE isn't the template itself, it will not have a valid |
| 19855 | TYPE_FIELDS list. */ |
| 19856 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) |
| 19857 | /* scope is either the template itself or a compatible instantiation |
| 19858 | like X<T>, so look up the name in the original template. */ |
| 19859 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
| 19860 | else |
| 19861 | /* scope is a partial instantiation, so we can't do the lookup or we |
| 19862 | will lose the template arguments. */ |
| 19863 | return type; |
| 19864 | /* Enter the SCOPE so that name lookup will be resolved as if we |
| 19865 | were in the class definition. In particular, SCOPE will no |
| 19866 | longer be considered a dependent type. */ |
| 19867 | pushed_scope = push_scope (scope); |
| 19868 | /* Look up the declaration. */ |
| 19869 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true, |
| 19870 | tf_warning_or_error); |
| 19871 | |
| 19872 | result = NULL_TREE; |
| 19873 | |
| 19874 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
| 19875 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ |
| 19876 | if (!decl) |
| 19877 | /*nop*/; |
| 19878 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE |
| 19879 | && TREE_CODE (decl) == TYPE_DECL) |
| 19880 | { |
| 19881 | result = TREE_TYPE (decl); |
| 19882 | if (result == error_mark_node) |
| 19883 | result = NULL_TREE; |
| 19884 | } |
| 19885 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR |
| 19886 | && DECL_CLASS_TEMPLATE_P (decl)) |
| 19887 | { |
| 19888 | tree tmpl; |
| 19889 | tree args; |
| 19890 | /* Obtain the template and the arguments. */ |
| 19891 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); |
| 19892 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); |
| 19893 | /* Instantiate the template. */ |
| 19894 | result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, |
| 19895 | /*entering_scope=*/0, |
| 19896 | tf_error | tf_user); |
| 19897 | if (result == error_mark_node) |
| 19898 | result = NULL_TREE; |
| 19899 | } |
| 19900 | |
| 19901 | /* Leave the SCOPE. */ |
| 19902 | if (pushed_scope) |
| 19903 | pop_scope (pushed_scope); |
| 19904 | |
| 19905 | /* If we failed to resolve it, return the original typename. */ |
| 19906 | if (!result) |
| 19907 | return type; |
| 19908 | |
| 19909 | /* If lookup found a typename type, resolve that too. */ |
| 19910 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) |
| 19911 | { |
| 19912 | /* Ill-formed programs can cause infinite recursion here, so we |
| 19913 | must catch that. */ |
| 19914 | TYPENAME_IS_RESOLVING_P (type) = 1; |
| 19915 | result = resolve_typename_type (result, only_current_p); |
| 19916 | TYPENAME_IS_RESOLVING_P (type) = 0; |
| 19917 | } |
| 19918 | |
| 19919 | /* Qualify the resulting type. */ |
| 19920 | quals = cp_type_quals (type); |
| 19921 | if (quals) |
| 19922 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); |
| 19923 | |
| 19924 | return result; |
| 19925 | } |
| 19926 | |
| 19927 | /* EXPR is an expression which is not type-dependent. Return a proxy |
| 19928 | for EXPR that can be used to compute the types of larger |
| 19929 | expressions containing EXPR. */ |
| 19930 | |
| 19931 | tree |
| 19932 | build_non_dependent_expr (tree expr) |
| 19933 | { |
| 19934 | tree inner_expr; |
| 19935 | |
| 19936 | #ifdef ENABLE_CHECKING |
| 19937 | /* Try to get a constant value for all non-type-dependent expressions in |
| 19938 | order to expose bugs in *_dependent_expression_p and constexpr. */ |
| 19939 | if (cxx_dialect >= cxx0x) |
| 19940 | maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none)); |
| 19941 | #endif |
| 19942 | |
| 19943 | /* Preserve OVERLOADs; the functions must be available to resolve |
| 19944 | types. */ |
| 19945 | inner_expr = expr; |
| 19946 | if (TREE_CODE (inner_expr) == STMT_EXPR) |
| 19947 | inner_expr = stmt_expr_value_expr (inner_expr); |
| 19948 | if (TREE_CODE (inner_expr) == ADDR_EXPR) |
| 19949 | inner_expr = TREE_OPERAND (inner_expr, 0); |
| 19950 | if (TREE_CODE (inner_expr) == COMPONENT_REF) |
| 19951 | inner_expr = TREE_OPERAND (inner_expr, 1); |
| 19952 | if (is_overloaded_fn (inner_expr) |
| 19953 | || TREE_CODE (inner_expr) == OFFSET_REF) |
| 19954 | return expr; |
| 19955 | /* There is no need to return a proxy for a variable. */ |
| 19956 | if (TREE_CODE (expr) == VAR_DECL) |
| 19957 | return expr; |
| 19958 | /* Preserve string constants; conversions from string constants to |
| 19959 | "char *" are allowed, even though normally a "const char *" |
| 19960 | cannot be used to initialize a "char *". */ |
| 19961 | if (TREE_CODE (expr) == STRING_CST) |
| 19962 | return expr; |
| 19963 | /* Preserve arithmetic constants, as an optimization -- there is no |
| 19964 | reason to create a new node. */ |
| 19965 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) |
| 19966 | return expr; |
| 19967 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
| 19968 | There is at least one place where we want to know that a |
| 19969 | particular expression is a throw-expression: when checking a ?: |
| 19970 | expression, there are special rules if the second or third |
| 19971 | argument is a throw-expression. */ |
| 19972 | if (TREE_CODE (expr) == THROW_EXPR) |
| 19973 | return expr; |
| 19974 | |
| 19975 | /* Don't wrap an initializer list, we need to be able to look inside. */ |
| 19976 | if (BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 19977 | return expr; |
| 19978 | |
| 19979 | if (TREE_CODE (expr) == COND_EXPR) |
| 19980 | return build3 (COND_EXPR, |
| 19981 | TREE_TYPE (expr), |
| 19982 | TREE_OPERAND (expr, 0), |
| 19983 | (TREE_OPERAND (expr, 1) |
| 19984 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
| 19985 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), |
| 19986 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); |
| 19987 | if (TREE_CODE (expr) == COMPOUND_EXPR |
| 19988 | && !COMPOUND_EXPR_OVERLOADED (expr)) |
| 19989 | return build2 (COMPOUND_EXPR, |
| 19990 | TREE_TYPE (expr), |
| 19991 | TREE_OPERAND (expr, 0), |
| 19992 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); |
| 19993 | |
| 19994 | /* If the type is unknown, it can't really be non-dependent */ |
| 19995 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); |
| 19996 | |
| 19997 | /* Otherwise, build a NON_DEPENDENT_EXPR. */ |
| 19998 | return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr); |
| 19999 | } |
| 20000 | |
| 20001 | /* ARGS is a vector of expressions as arguments to a function call. |
| 20002 | Replace the arguments with equivalent non-dependent expressions. |
| 20003 | This modifies ARGS in place. */ |
| 20004 | |
| 20005 | void |
| 20006 | make_args_non_dependent (VEC(tree,gc) *args) |
| 20007 | { |
| 20008 | unsigned int ix; |
| 20009 | tree arg; |
| 20010 | |
| 20011 | FOR_EACH_VEC_ELT (tree, args, ix, arg) |
| 20012 | { |
| 20013 | tree newarg = build_non_dependent_expr (arg); |
| 20014 | if (newarg != arg) |
| 20015 | VEC_replace (tree, args, ix, newarg); |
| 20016 | } |
| 20017 | } |
| 20018 | |
| 20019 | /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM |
| 20020 | with a level one deeper than the actual template parms. */ |
| 20021 | |
| 20022 | tree |
| 20023 | make_auto (void) |
| 20024 | { |
| 20025 | tree au = cxx_make_type (TEMPLATE_TYPE_PARM); |
| 20026 | TYPE_NAME (au) = build_decl (BUILTINS_LOCATION, |
| 20027 | TYPE_DECL, get_identifier ("auto"), au); |
| 20028 | TYPE_STUB_DECL (au) = TYPE_NAME (au); |
| 20029 | TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index |
| 20030 | (0, processing_template_decl + 1, processing_template_decl + 1, |
| 20031 | TYPE_NAME (au), NULL_TREE); |
| 20032 | TYPE_CANONICAL (au) = canonical_type_parameter (au); |
| 20033 | DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; |
| 20034 | SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); |
| 20035 | |
| 20036 | return au; |
| 20037 | } |
| 20038 | |
| 20039 | /* Given type ARG, return std::initializer_list<ARG>. */ |
| 20040 | |
| 20041 | static tree |
| 20042 | listify (tree arg) |
| 20043 | { |
| 20044 | tree std_init_list = namespace_binding |
| 20045 | (get_identifier ("initializer_list"), std_node); |
| 20046 | tree argvec; |
| 20047 | if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list)) |
| 20048 | { |
| 20049 | error ("deducing from brace-enclosed initializer list requires " |
| 20050 | "#include <initializer_list>"); |
| 20051 | return error_mark_node; |
| 20052 | } |
| 20053 | argvec = make_tree_vec (1); |
| 20054 | TREE_VEC_ELT (argvec, 0) = arg; |
| 20055 | return lookup_template_class (std_init_list, argvec, NULL_TREE, |
| 20056 | NULL_TREE, 0, tf_warning_or_error); |
| 20057 | } |
| 20058 | |
| 20059 | /* Replace auto in TYPE with std::initializer_list<auto>. */ |
| 20060 | |
| 20061 | static tree |
| 20062 | listify_autos (tree type, tree auto_node) |
| 20063 | { |
| 20064 | tree init_auto = listify (auto_node); |
| 20065 | tree argvec = make_tree_vec (1); |
| 20066 | TREE_VEC_ELT (argvec, 0) = init_auto; |
| 20067 | if (processing_template_decl) |
| 20068 | argvec = add_to_template_args (current_template_args (), argvec); |
| 20069 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); |
| 20070 | } |
| 20071 | |
| 20072 | /* walk_tree helper for do_auto_deduction. */ |
| 20073 | |
| 20074 | static tree |
| 20075 | contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 20076 | void *type) |
| 20077 | { |
| 20078 | /* Is this a variable with the type we're looking for? */ |
| 20079 | if (DECL_P (*tp) |
| 20080 | && TREE_TYPE (*tp) == type) |
| 20081 | return *tp; |
| 20082 | else |
| 20083 | return NULL_TREE; |
| 20084 | } |
| 20085 | |
| 20086 | /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced |
| 20087 | from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */ |
| 20088 | |
| 20089 | tree |
| 20090 | do_auto_deduction (tree type, tree init, tree auto_node) |
| 20091 | { |
| 20092 | tree parms, tparms, targs; |
| 20093 | tree args[1]; |
| 20094 | tree decl; |
| 20095 | int val; |
| 20096 | |
| 20097 | if (type_dependent_expression_p (init)) |
| 20098 | /* Defining a subset of type-dependent expressions that we can deduce |
| 20099 | from ahead of time isn't worth the trouble. */ |
| 20100 | return type; |
| 20101 | |
| 20102 | /* The name of the object being declared shall not appear in the |
| 20103 | initializer expression. */ |
| 20104 | decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type); |
| 20105 | if (decl) |
| 20106 | { |
| 20107 | error ("variable %q#D with %<auto%> type used in its own " |
| 20108 | "initializer", decl); |
| 20109 | return error_mark_node; |
| 20110 | } |
| 20111 | |
| 20112 | /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto |
| 20113 | with either a new invented type template parameter U or, if the |
| 20114 | initializer is a braced-init-list (8.5.4), with |
| 20115 | std::initializer_list<U>. */ |
| 20116 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
| 20117 | type = listify_autos (type, auto_node); |
| 20118 | |
| 20119 | init = resolve_nondeduced_context (init); |
| 20120 | |
| 20121 | parms = build_tree_list (NULL_TREE, type); |
| 20122 | args[0] = init; |
| 20123 | tparms = make_tree_vec (1); |
| 20124 | targs = make_tree_vec (1); |
| 20125 | TREE_VEC_ELT (tparms, 0) |
| 20126 | = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); |
| 20127 | val = type_unification_real (tparms, targs, parms, args, 1, 0, |
| 20128 | DEDUCE_CALL, LOOKUP_NORMAL, |
| 20129 | /*explain_p=*/false); |
| 20130 | if (val > 0) |
| 20131 | { |
| 20132 | if (processing_template_decl) |
| 20133 | /* Try again at instantiation time. */ |
| 20134 | return type; |
| 20135 | if (type && type != error_mark_node) |
| 20136 | /* If type is error_mark_node a diagnostic must have been |
| 20137 | emitted by now. Also, having a mention to '<type error>' |
| 20138 | in the diagnostic is not really useful to the user. */ |
| 20139 | error ("unable to deduce %qT from %qE", type, init); |
| 20140 | return error_mark_node; |
| 20141 | } |
| 20142 | |
| 20143 | /* If the list of declarators contains more than one declarator, the type |
| 20144 | of each declared variable is determined as described above. If the |
| 20145 | type deduced for the template parameter U is not the same in each |
| 20146 | deduction, the program is ill-formed. */ |
| 20147 | if (TREE_TYPE (auto_node) |
| 20148 | && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0))) |
| 20149 | { |
| 20150 | error ("inconsistent deduction for %qT: %qT and then %qT", |
| 20151 | auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)); |
| 20152 | return error_mark_node; |
| 20153 | } |
| 20154 | TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0); |
| 20155 | |
| 20156 | if (processing_template_decl) |
| 20157 | targs = add_to_template_args (current_template_args (), targs); |
| 20158 | return tsubst (type, targs, tf_warning_or_error, NULL_TREE); |
| 20159 | } |
| 20160 | |
| 20161 | /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the |
| 20162 | result. */ |
| 20163 | |
| 20164 | tree |
| 20165 | splice_late_return_type (tree type, tree late_return_type) |
| 20166 | { |
| 20167 | tree argvec; |
| 20168 | |
| 20169 | if (late_return_type == NULL_TREE) |
| 20170 | return type; |
| 20171 | argvec = make_tree_vec (1); |
| 20172 | TREE_VEC_ELT (argvec, 0) = late_return_type; |
| 20173 | if (processing_template_parmlist) |
| 20174 | /* For a late-specified return type in a template type-parameter, we |
| 20175 | need to add a dummy argument level for its parmlist. */ |
| 20176 | argvec = add_to_template_args |
| 20177 | (make_tree_vec (processing_template_parmlist), argvec); |
| 20178 | if (current_template_parms) |
| 20179 | argvec = add_to_template_args (current_template_args (), argvec); |
| 20180 | return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); |
| 20181 | } |
| 20182 | |
| 20183 | /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */ |
| 20184 | |
| 20185 | bool |
| 20186 | is_auto (const_tree type) |
| 20187 | { |
| 20188 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
| 20189 | && TYPE_IDENTIFIER (type) == get_identifier ("auto")) |
| 20190 | return true; |
| 20191 | else |
| 20192 | return false; |
| 20193 | } |
| 20194 | |
| 20195 | /* Returns true iff TYPE contains a use of 'auto'. Since auto can only |
| 20196 | appear as a type-specifier for the declaration in question, we don't |
| 20197 | have to look through the whole type. */ |
| 20198 | |
| 20199 | tree |
| 20200 | type_uses_auto (tree type) |
| 20201 | { |
| 20202 | enum tree_code code; |
| 20203 | if (is_auto (type)) |
| 20204 | return type; |
| 20205 | |
| 20206 | code = TREE_CODE (type); |
| 20207 | |
| 20208 | if (code == POINTER_TYPE || code == REFERENCE_TYPE |
| 20209 | || code == OFFSET_TYPE || code == FUNCTION_TYPE |
| 20210 | || code == METHOD_TYPE || code == ARRAY_TYPE) |
| 20211 | return type_uses_auto (TREE_TYPE (type)); |
| 20212 | |
| 20213 | if (TYPE_PTRMEMFUNC_P (type)) |
| 20214 | return type_uses_auto (TREE_TYPE (TREE_TYPE |
| 20215 | (TYPE_PTRMEMFUNC_FN_TYPE (type)))); |
| 20216 | |
| 20217 | return NULL_TREE; |
| 20218 | } |
| 20219 | |
| 20220 | /* For a given template T, return the vector of typedefs referenced |
| 20221 | in T for which access check is needed at T instantiation time. |
| 20222 | T is either a FUNCTION_DECL or a RECORD_TYPE. |
| 20223 | Those typedefs were added to T by the function |
| 20224 | append_type_to_template_for_access_check. */ |
| 20225 | |
| 20226 | VEC(qualified_typedef_usage_t,gc)* |
| 20227 | get_types_needing_access_check (tree t) |
| 20228 | { |
| 20229 | tree ti; |
| 20230 | VEC(qualified_typedef_usage_t,gc) *result = NULL; |
| 20231 | |
| 20232 | if (!t || t == error_mark_node) |
| 20233 | return NULL; |
| 20234 | |
| 20235 | if (!(ti = get_template_info (t))) |
| 20236 | return NULL; |
| 20237 | |
| 20238 | if (CLASS_TYPE_P (t) |
| 20239 | || TREE_CODE (t) == FUNCTION_DECL) |
| 20240 | { |
| 20241 | if (!TI_TEMPLATE (ti)) |
| 20242 | return NULL; |
| 20243 | |
| 20244 | result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti); |
| 20245 | } |
| 20246 | |
| 20247 | return result; |
| 20248 | } |
| 20249 | |
| 20250 | /* Append the typedef TYPE_DECL used in template T to a list of typedefs |
| 20251 | tied to T. That list of typedefs will be access checked at |
| 20252 | T instantiation time. |
| 20253 | T is either a FUNCTION_DECL or a RECORD_TYPE. |
| 20254 | TYPE_DECL is a TYPE_DECL node representing a typedef. |
| 20255 | SCOPE is the scope through which TYPE_DECL is accessed. |
| 20256 | LOCATION is the location of the usage point of TYPE_DECL. |
| 20257 | |
| 20258 | This function is a subroutine of |
| 20259 | append_type_to_template_for_access_check. */ |
| 20260 | |
| 20261 | static void |
| 20262 | append_type_to_template_for_access_check_1 (tree t, |
| 20263 | tree type_decl, |
| 20264 | tree scope, |
| 20265 | location_t location) |
| 20266 | { |
| 20267 | qualified_typedef_usage_t typedef_usage; |
| 20268 | tree ti; |
| 20269 | |
| 20270 | if (!t || t == error_mark_node) |
| 20271 | return; |
| 20272 | |
| 20273 | gcc_assert ((TREE_CODE (t) == FUNCTION_DECL |
| 20274 | || CLASS_TYPE_P (t)) |
| 20275 | && type_decl |
| 20276 | && TREE_CODE (type_decl) == TYPE_DECL |
| 20277 | && scope); |
| 20278 | |
| 20279 | if (!(ti = get_template_info (t))) |
| 20280 | return; |
| 20281 | |
| 20282 | gcc_assert (TI_TEMPLATE (ti)); |
| 20283 | |
| 20284 | typedef_usage.typedef_decl = type_decl; |
| 20285 | typedef_usage.context = scope; |
| 20286 | typedef_usage.locus = location; |
| 20287 | |
| 20288 | VEC_safe_push (qualified_typedef_usage_t, gc, |
| 20289 | TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), |
| 20290 | &typedef_usage); |
| 20291 | } |
| 20292 | |
| 20293 | /* Append TYPE_DECL to the template TEMPL. |
| 20294 | TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL. |
| 20295 | At TEMPL instanciation time, TYPE_DECL will be checked to see |
| 20296 | if it can be accessed through SCOPE. |
| 20297 | LOCATION is the location of the usage point of TYPE_DECL. |
| 20298 | |
| 20299 | e.g. consider the following code snippet: |
| 20300 | |
| 20301 | class C |
| 20302 | { |
| 20303 | typedef int myint; |
| 20304 | }; |
| 20305 | |
| 20306 | template<class U> struct S |
| 20307 | { |
| 20308 | C::myint mi; // <-- usage point of the typedef C::myint |
| 20309 | }; |
| 20310 | |
| 20311 | S<char> s; |
| 20312 | |
| 20313 | At S<char> instantiation time, we need to check the access of C::myint |
| 20314 | In other words, we need to check the access of the myint typedef through |
| 20315 | the C scope. For that purpose, this function will add the myint typedef |
| 20316 | and the scope C through which its being accessed to a list of typedefs |
| 20317 | tied to the template S. That list will be walked at template instantiation |
| 20318 | time and access check performed on each typedefs it contains. |
| 20319 | Note that this particular code snippet should yield an error because |
| 20320 | myint is private to C. */ |
| 20321 | |
| 20322 | void |
| 20323 | append_type_to_template_for_access_check (tree templ, |
| 20324 | tree type_decl, |
| 20325 | tree scope, |
| 20326 | location_t location) |
| 20327 | { |
| 20328 | qualified_typedef_usage_t *iter; |
| 20329 | int i; |
| 20330 | |
| 20331 | gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL)); |
| 20332 | |
| 20333 | /* Make sure we don't append the type to the template twice. */ |
| 20334 | FOR_EACH_VEC_ELT (qualified_typedef_usage_t, |
| 20335 | get_types_needing_access_check (templ), |
| 20336 | i, iter) |
| 20337 | if (iter->typedef_decl == type_decl && scope == iter->context) |
| 20338 | return; |
| 20339 | |
| 20340 | append_type_to_template_for_access_check_1 (templ, type_decl, |
| 20341 | scope, location); |
| 20342 | } |
| 20343 | |
| 20344 | /* Set up the hash tables for template instantiations. */ |
| 20345 | |
| 20346 | void |
| 20347 | init_template_processing (void) |
| 20348 | { |
| 20349 | decl_specializations = htab_create_ggc (37, |
| 20350 | hash_specialization, |
| 20351 | eq_specializations, |
| 20352 | ggc_free); |
| 20353 | type_specializations = htab_create_ggc (37, |
| 20354 | hash_specialization, |
| 20355 | eq_specializations, |
| 20356 | ggc_free); |
| 20357 | } |
| 20358 | |
| 20359 | /* Print stats about the template hash tables for -fstats. */ |
| 20360 | |
| 20361 | void |
| 20362 | print_template_statistics (void) |
| 20363 | { |
| 20364 | fprintf (stderr, "decl_specializations: size %ld, %ld elements, " |
| 20365 | "%f collisions\n", (long) htab_size (decl_specializations), |
| 20366 | (long) htab_elements (decl_specializations), |
| 20367 | htab_collisions (decl_specializations)); |
| 20368 | fprintf (stderr, "type_specializations: size %ld, %ld elements, " |
| 20369 | "%f collisions\n", (long) htab_size (type_specializations), |
| 20370 | (long) htab_elements (type_specializations), |
| 20371 | htab_collisions (type_specializations)); |
| 20372 | } |
| 20373 | |
| 20374 | #include "gt-cp-pt.h" |