pt.c revision 122180
1/* Handle parameterized types (templates) for GNU C++. 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3 2001, 2002 Free Software Foundation, Inc. 4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. 5 Rewritten by Jason Merrill (jason@cygnus.com). 6 7This file is part of GNU CC. 8 9GNU CC is free software; you can redistribute it and/or modify 10it under the terms of the GNU General Public License as published by 11the Free Software Foundation; either version 2, or (at your option) 12any later version. 13 14GNU CC is distributed in the hope that it will be useful, 15but WITHOUT ANY WARRANTY; without even the implied warranty of 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17GNU General Public License for more details. 18 19You should have received a copy of the GNU General Public License 20along with GNU CC; see the file COPYING. If not, write to 21the Free Software Foundation, 59 Temple Place - Suite 330, 22Boston, MA 02111-1307, USA. */ 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 "obstack.h" 32#include "tree.h" 33#include "flags.h" 34#include "cp-tree.h" 35#include "tree-inline.h" 36#include "decl.h" 37#include "parse.h" 38#include "lex.h" 39#include "output.h" 40#include "except.h" 41#include "toplev.h" 42#include "rtl.h" 43#include "ggc.h" 44#include "timevar.h" 45 46/* The type of functions taking a tree, and some additional data, and 47 returning an int. */ 48typedef int (*tree_fn_t) PARAMS ((tree, void*)); 49 50/* The PENDING_TEMPLATES is a TREE_LIST of templates whose 51 instantiations have been deferred, either because their definitions 52 were not yet available, or because we were putting off doing the 53 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where 54 the instantiate request occurred; the TREE_VALUE is either a DECL 55 (for a function or static data member), or a TYPE (for a class) 56 indicating what we are hoping to instantiate. */ 57static GTY(()) tree pending_templates; 58static tree last_pending_template; 59 60int processing_template_parmlist; 61static int template_header_count; 62 63static GTY(()) tree saved_trees; 64static GTY(()) varray_type inline_parm_levels; 65static size_t inline_parm_levels_used; 66 67static GTY(()) tree current_tinst_level; 68 69static GTY(()) tree saved_access_scope; 70 71/* A map from local variable declarations in the body of the template 72 presently being instantiated to the corresponding instantiated 73 local variables. */ 74static htab_t local_specializations; 75 76#define UNIFY_ALLOW_NONE 0 77#define UNIFY_ALLOW_MORE_CV_QUAL 1 78#define UNIFY_ALLOW_LESS_CV_QUAL 2 79#define UNIFY_ALLOW_DERIVED 4 80#define UNIFY_ALLOW_INTEGER 8 81#define UNIFY_ALLOW_OUTER_LEVEL 16 82#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 83#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 84#define UNIFY_ALLOW_MAX_CORRECTION 128 85 86#define GTB_VIA_VIRTUAL 1 /* The base class we are examining is 87 virtual, or a base class of a virtual 88 base. */ 89#define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current 90 type with the desired type. */ 91 92static void push_access_scope_real PARAMS ((tree, tree, tree)); 93static void push_access_scope PARAMS ((tree)); 94static void pop_access_scope PARAMS ((tree)); 95static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree, 96 unification_kind_t, int)); 97static int try_one_overload PARAMS ((tree, tree, tree, tree, tree, 98 unification_kind_t, int, bool)); 99static int unify PARAMS ((tree, tree, tree, tree, int)); 100static void add_pending_template PARAMS ((tree)); 101static void reopen_tinst_level PARAMS ((tree)); 102static tree classtype_mangled_name PARAMS ((tree)); 103static char *mangle_class_name_for_template PARAMS ((const char *, 104 tree, tree)); 105static tree tsubst_initializer_list PARAMS ((tree, tree)); 106static int list_eq PARAMS ((tree, tree)); 107static tree get_class_bindings PARAMS ((tree, tree, tree)); 108static tree coerce_template_parms PARAMS ((tree, tree, tree, 109 tsubst_flags_t, int)); 110static void tsubst_enum PARAMS ((tree, tree, tree)); 111static tree add_to_template_args PARAMS ((tree, tree)); 112static tree add_outermost_template_args PARAMS ((tree, tree)); 113static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*, 114 tree*)); 115static int type_unification_real PARAMS ((tree, tree, tree, tree, 116 int, unification_kind_t, int, int)); 117static void note_template_header PARAMS ((int)); 118static tree maybe_fold_nontype_arg PARAMS ((tree)); 119static tree convert_nontype_argument PARAMS ((tree, tree)); 120static tree convert_template_argument PARAMS ((tree, tree, tree, 121 tsubst_flags_t, int, tree)); 122static tree get_bindings_overload PARAMS ((tree, tree, tree)); 123static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t)); 124static tree build_template_parm_index PARAMS ((int, int, int, tree, tree)); 125static int inline_needs_template_parms PARAMS ((tree)); 126static void push_inline_template_parms_recursive PARAMS ((tree, int)); 127static tree retrieve_specialization PARAMS ((tree, tree)); 128static tree retrieve_local_specialization PARAMS ((tree)); 129static tree register_specialization PARAMS ((tree, tree, tree)); 130static void register_local_specialization PARAMS ((tree, tree)); 131static int reregister_specialization PARAMS ((tree, tree, tree)); 132static tree reduce_template_parm_level PARAMS ((tree, tree, int)); 133static tree build_template_decl PARAMS ((tree, tree)); 134static int mark_template_parm PARAMS ((tree, void *)); 135static int template_parm_this_level_p PARAMS ((tree, void *)); 136static tree tsubst_friend_function PARAMS ((tree, tree)); 137static tree tsubst_friend_class PARAMS ((tree, tree)); 138static int can_complete_type_without_circularity PARAMS ((tree)); 139static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int)); 140static int template_decl_level PARAMS ((tree)); 141static tree maybe_get_template_decl_from_type_decl PARAMS ((tree)); 142static int check_cv_quals_for_unify PARAMS ((int, tree, tree)); 143static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t)); 144static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t)); 145static void regenerate_decl_from_template PARAMS ((tree, tree)); 146static tree most_specialized PARAMS ((tree, tree, tree)); 147static tree most_specialized_class PARAMS ((tree, tree)); 148static int template_class_depth_real PARAMS ((tree, int)); 149static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int)); 150static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t)); 151static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree)); 152static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree)); 153static void check_specialization_scope PARAMS ((void)); 154static tree process_partial_specialization PARAMS ((tree)); 155static void set_current_access_from_decl PARAMS ((tree)); 156static void check_default_tmpl_args PARAMS ((tree, tree, int, int)); 157static tree tsubst_call_declarator_parms PARAMS ((tree, tree, 158 tsubst_flags_t, tree)); 159static tree get_template_base_recursive PARAMS ((tree, tree, 160 tree, tree, tree, int)); 161static tree get_template_base PARAMS ((tree, tree, tree, tree)); 162static int verify_class_unification PARAMS ((tree, tree, tree)); 163static tree try_class_unification PARAMS ((tree, tree, tree, tree)); 164static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t, 165 tree, tree)); 166static tree determine_specialization PARAMS ((tree, tree, tree *, int)); 167static int template_args_equal PARAMS ((tree, tree)); 168static void tsubst_default_arguments PARAMS ((tree)); 169static tree for_each_template_parm_r PARAMS ((tree *, int *, void *)); 170static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree)); 171static void copy_default_args_to_explicit_spec PARAMS ((tree)); 172static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t)); 173 174/* Make the current scope suitable for access checking when we are 175 processing T. T can be FUNCTION_DECL for instantiated function 176 template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for 177 static member variable (need by instantiate_decl). ARGS is the 178 template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE, 179 this is used instead of the context of T. */ 180 181void 182push_access_scope_real (t, args, context) 183 tree t, args, context; 184{ 185 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 186 { 187 /* When we are processing specialization `foo<Outer>' for code like 188 189 template <class U> typename U::Inner foo (); 190 class Outer { 191 struct Inner {}; 192 friend Outer::Inner foo<Outer> (); 193 }; 194 195 `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of 196 its specialization. We can get the FUNCTION_DECL with the right 197 information because this specialization has already been 198 registered by the friend declaration above. */ 199 200 if (DECL_FUNCTION_TEMPLATE_P (t) && args) 201 { 202 tree full_args = tsubst_template_arg_vector 203 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none); 204 tree spec = NULL_TREE; 205 if (full_args != error_mark_node) 206 spec = retrieve_specialization (t, full_args); 207 if (spec) 208 t = spec; 209 } 210 } 211 212 if (!context) 213 context = DECL_CONTEXT (t); 214 if (context && TYPE_P (context)) 215 push_nested_class (context, 2); 216 else 217 push_to_top_level (); 218 219 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 220 { 221 saved_access_scope = tree_cons 222 (NULL_TREE, current_function_decl, saved_access_scope); 223 current_function_decl = t; 224 } 225} 226 227/* Like push_access_scope_real, but always uses DECL_CONTEXT. */ 228 229void 230push_access_scope (t) 231 tree t; 232{ 233 push_access_scope_real (t, NULL_TREE, NULL_TREE); 234} 235 236/* Restore the scope set up by push_access_scope. T is the node we 237 are processing. */ 238 239void 240pop_access_scope (t) 241 tree t; 242{ 243 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 244 { 245 current_function_decl = TREE_VALUE (saved_access_scope); 246 saved_access_scope = TREE_CHAIN (saved_access_scope); 247 } 248 249 if (DECL_CLASS_SCOPE_P (t)) 250 pop_nested_class (); 251 else 252 pop_from_top_level (); 253} 254 255/* Do any processing required when DECL (a member template declaration 256 using TEMPLATE_PARAMETERS as its innermost parameter list) is 257 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless 258 it is a specialization, in which case the DECL itself is returned. */ 259 260tree 261finish_member_template_decl (decl) 262 tree decl; 263{ 264 if (decl == NULL_TREE || decl == void_type_node) 265 return NULL_TREE; 266 else if (decl == error_mark_node) 267 /* By returning NULL_TREE, the parser will just ignore this 268 declaration. We have already issued the error. */ 269 return NULL_TREE; 270 else if (TREE_CODE (decl) == TREE_LIST) 271 { 272 /* Assume that the class is the only declspec. */ 273 decl = TREE_VALUE (decl); 274 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) 275 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) 276 { 277 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); 278 check_member_template (tmpl); 279 return tmpl; 280 } 281 return NULL_TREE; 282 } 283 else if (TREE_CODE (decl) == FIELD_DECL) 284 error ("data member `%D' cannot be a member template", decl); 285 else if (DECL_TEMPLATE_INFO (decl)) 286 { 287 if (!DECL_TEMPLATE_SPECIALIZATION (decl)) 288 { 289 check_member_template (DECL_TI_TEMPLATE (decl)); 290 return DECL_TI_TEMPLATE (decl); 291 } 292 else 293 return decl; 294 } 295 else 296 error ("invalid member template declaration `%D'", decl); 297 298 return error_mark_node; 299} 300 301/* Returns the template nesting level of the indicated class TYPE. 302 303 For example, in: 304 template <class T> 305 struct A 306 { 307 template <class U> 308 struct B {}; 309 }; 310 311 A<T>::B<U> has depth two, while A<T> has depth one. 312 Both A<T>::B<int> and A<int>::B<U> have depth one, if 313 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not 314 specializations. 315 316 This function is guaranteed to return 0 if passed NULL_TREE so 317 that, for example, `template_class_depth (current_class_type)' is 318 always safe. */ 319 320static int 321template_class_depth_real (type, count_specializations) 322 tree type; 323 int count_specializations; 324{ 325 int depth; 326 327 for (depth = 0; 328 type && TREE_CODE (type) != NAMESPACE_DECL; 329 type = (TREE_CODE (type) == FUNCTION_DECL) 330 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) 331 { 332 if (TREE_CODE (type) != FUNCTION_DECL) 333 { 334 if (CLASSTYPE_TEMPLATE_INFO (type) 335 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) 336 && ((count_specializations 337 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 338 || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) 339 ++depth; 340 } 341 else 342 { 343 if (DECL_TEMPLATE_INFO (type) 344 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) 345 && ((count_specializations 346 && DECL_TEMPLATE_SPECIALIZATION (type)) 347 || uses_template_parms (DECL_TI_ARGS (type)))) 348 ++depth; 349 } 350 } 351 352 return depth; 353} 354 355/* Returns the template nesting level of the indicated class TYPE. 356 Like template_class_depth_real, but instantiations do not count in 357 the depth. */ 358 359int 360template_class_depth (type) 361 tree type; 362{ 363 return template_class_depth_real (type, /*count_specializations=*/0); 364} 365 366/* Returns 1 if processing DECL as part of do_pending_inlines 367 needs us to push template parms. */ 368 369static int 370inline_needs_template_parms (decl) 371 tree decl; 372{ 373 if (! DECL_TEMPLATE_INFO (decl)) 374 return 0; 375 376 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) 377 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); 378} 379 380/* Subroutine of maybe_begin_member_template_processing. 381 Push the template parms in PARMS, starting from LEVELS steps into the 382 chain, and ending at the beginning, since template parms are listed 383 innermost first. */ 384 385static void 386push_inline_template_parms_recursive (parmlist, levels) 387 tree parmlist; 388 int levels; 389{ 390 tree parms = TREE_VALUE (parmlist); 391 int i; 392 393 if (levels > 1) 394 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); 395 396 ++processing_template_decl; 397 current_template_parms 398 = tree_cons (size_int (processing_template_decl), 399 parms, current_template_parms); 400 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; 401 402 pushlevel (0); 403 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 404 { 405 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 406 my_friendly_assert (DECL_P (parm), 0); 407 408 switch (TREE_CODE (parm)) 409 { 410 case TYPE_DECL: 411 case TEMPLATE_DECL: 412 pushdecl (parm); 413 break; 414 415 case PARM_DECL: 416 { 417 /* Make a CONST_DECL as is done in process_template_parm. 418 It is ugly that we recreate this here; the original 419 version built in process_template_parm is no longer 420 available. */ 421 tree decl = build_decl (CONST_DECL, DECL_NAME (parm), 422 TREE_TYPE (parm)); 423 DECL_ARTIFICIAL (decl) = 1; 424 DECL_INITIAL (decl) = DECL_INITIAL (parm); 425 SET_DECL_TEMPLATE_PARM_P (decl); 426 pushdecl (decl); 427 } 428 break; 429 430 default: 431 abort (); 432 } 433 } 434} 435 436/* Restore the template parameter context for a member template or 437 a friend template defined in a class definition. */ 438 439void 440maybe_begin_member_template_processing (decl) 441 tree decl; 442{ 443 tree parms; 444 int levels = 0; 445 446 if (inline_needs_template_parms (decl)) 447 { 448 parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); 449 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; 450 451 if (DECL_TEMPLATE_SPECIALIZATION (decl)) 452 { 453 --levels; 454 parms = TREE_CHAIN (parms); 455 } 456 457 push_inline_template_parms_recursive (parms, levels); 458 } 459 460 /* Remember how many levels of template parameters we pushed so that 461 we can pop them later. */ 462 if (!inline_parm_levels) 463 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); 464 if (inline_parm_levels_used == inline_parm_levels->num_elements) 465 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); 466 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; 467 ++inline_parm_levels_used; 468} 469 470/* Undo the effects of begin_member_template_processing. */ 471 472void 473maybe_end_member_template_processing () 474{ 475 int i; 476 477 if (!inline_parm_levels_used) 478 return; 479 480 --inline_parm_levels_used; 481 for (i = 0; 482 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); 483 ++i) 484 { 485 --processing_template_decl; 486 current_template_parms = TREE_CHAIN (current_template_parms); 487 poplevel (0, 0, 0); 488 } 489} 490 491/* Returns nonzero iff T is a member template function. We must be 492 careful as in 493 494 template <class T> class C { void f(); } 495 496 Here, f is a template function, and a member, but not a member 497 template. This function does not concern itself with the origin of 498 T, only its present state. So if we have 499 500 template <class T> class C { template <class U> void f(U); } 501 502 then neither C<int>::f<char> nor C<T>::f<double> is considered 503 to be a member template. But, `template <class U> void 504 C<int>::f(U)' is considered a member template. */ 505 506int 507is_member_template (t) 508 tree t; 509{ 510 if (!DECL_FUNCTION_TEMPLATE_P (t)) 511 /* Anything that isn't a function or a template function is 512 certainly not a member template. */ 513 return 0; 514 515 /* A local class can't have member templates. */ 516 if (decl_function_context (t)) 517 return 0; 518 519 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) 520 /* If there are more levels of template parameters than 521 there are template classes surrounding the declaration, 522 then we have a member template. */ 523 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 524 template_class_depth (DECL_CONTEXT (t)))); 525} 526 527#if 0 /* UNUSED */ 528/* Returns nonzero iff T is a member template class. See 529 is_member_template for a description of what precisely constitutes 530 a member template. */ 531 532int 533is_member_template_class (t) 534 tree t; 535{ 536 if (!DECL_CLASS_TEMPLATE_P (t)) 537 /* Anything that isn't a class template, is certainly not a member 538 template. */ 539 return 0; 540 541 if (!DECL_CLASS_SCOPE_P (t)) 542 /* Anything whose context isn't a class type is surely not a 543 member template. */ 544 return 0; 545 546 /* If there are more levels of template parameters than there are 547 template classes surrounding the declaration, then we have a 548 member template. */ 549 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 550 template_class_depth (DECL_CONTEXT (t))); 551} 552#endif 553 554/* Return a new template argument vector which contains all of ARGS, 555 but has as its innermost set of arguments the EXTRA_ARGS. */ 556 557static tree 558add_to_template_args (args, extra_args) 559 tree args; 560 tree extra_args; 561{ 562 tree new_args; 563 int extra_depth; 564 int i; 565 int j; 566 567 extra_depth = TMPL_ARGS_DEPTH (extra_args); 568 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); 569 570 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) 571 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); 572 573 for (j = 1; j <= extra_depth; ++j, ++i) 574 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); 575 576 return new_args; 577} 578 579/* Like add_to_template_args, but only the outermost ARGS are added to 580 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH 581 (EXTRA_ARGS) levels are added. This function is used to combine 582 the template arguments from a partial instantiation with the 583 template arguments used to attain the full instantiation from the 584 partial instantiation. */ 585 586static tree 587add_outermost_template_args (args, extra_args) 588 tree args; 589 tree extra_args; 590{ 591 tree new_args; 592 593 /* If there are more levels of EXTRA_ARGS than there are ARGS, 594 something very fishy is going on. */ 595 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), 596 0); 597 598 /* If *all* the new arguments will be the EXTRA_ARGS, just return 599 them. */ 600 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) 601 return extra_args; 602 603 /* For the moment, we make ARGS look like it contains fewer levels. */ 604 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); 605 606 new_args = add_to_template_args (args, extra_args); 607 608 /* Now, we restore ARGS to its full dimensions. */ 609 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); 610 611 return new_args; 612} 613 614/* Return the N levels of innermost template arguments from the ARGS. */ 615 616tree 617get_innermost_template_args (args, n) 618 tree args; 619 int n; 620{ 621 tree new_args; 622 int extra_levels; 623 int i; 624 625 my_friendly_assert (n >= 0, 20000603); 626 627 /* If N is 1, just return the innermost set of template arguments. */ 628 if (n == 1) 629 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); 630 631 /* If we're not removing anything, just return the arguments we were 632 given. */ 633 extra_levels = TMPL_ARGS_DEPTH (args) - n; 634 my_friendly_assert (extra_levels >= 0, 20000603); 635 if (extra_levels == 0) 636 return args; 637 638 /* Make a new set of arguments, not containing the outer arguments. */ 639 new_args = make_tree_vec (n); 640 for (i = 1; i <= n; ++i) 641 SET_TMPL_ARGS_LEVEL (new_args, i, 642 TMPL_ARGS_LEVEL (args, i + extra_levels)); 643 644 return new_args; 645} 646 647/* We've got a template header coming up; push to a new level for storing 648 the parms. */ 649 650void 651begin_template_parm_list () 652{ 653 /* We use a non-tag-transparent scope here, which causes pushtag to 654 put tags in this scope, rather than in the enclosing class or 655 namespace scope. This is the right thing, since we want 656 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a 657 global template class, push_template_decl handles putting the 658 TEMPLATE_DECL into top-level scope. For a nested template class, 659 e.g.: 660 661 template <class T> struct S1 { 662 template <class T> struct S2 {}; 663 }; 664 665 pushtag contains special code to call pushdecl_with_scope on the 666 TEMPLATE_DECL for S2. */ 667 begin_scope (sk_template_parms); 668 ++processing_template_decl; 669 ++processing_template_parmlist; 670 note_template_header (0); 671} 672 673/* This routine is called when a specialization is declared. If it is 674 invalid to declare a specialization here, an error is reported. */ 675 676static void 677check_specialization_scope () 678{ 679 tree scope = current_scope (); 680 681 /* [temp.expl.spec] 682 683 An explicit specialization shall be declared in the namespace of 684 which the template is a member, or, for member templates, in the 685 namespace of which the enclosing class or enclosing class 686 template is a member. An explicit specialization of a member 687 function, member class or static data member of a class template 688 shall be declared in the namespace of which the class template 689 is a member. */ 690 if (scope && TREE_CODE (scope) != NAMESPACE_DECL) 691 error ("explicit specialization in non-namespace scope `%D'", 692 scope); 693 694 /* [temp.expl.spec] 695 696 In an explicit specialization declaration for a member of a class 697 template or a member template that appears in namespace scope, 698 the member template and some of its enclosing class templates may 699 remain unspecialized, except that the declaration shall not 700 explicitly specialize a class member template if its enclosing 701 class templates are not explicitly specialized as well. */ 702 if (current_template_parms) 703 error ("enclosing class templates are not explicitly specialized"); 704} 705 706/* We've just seen template <>. */ 707 708void 709begin_specialization () 710{ 711 begin_scope (sk_template_spec); 712 note_template_header (1); 713 check_specialization_scope (); 714} 715 716/* Called at then end of processing a declaration preceded by 717 template<>. */ 718 719void 720end_specialization () 721{ 722 finish_scope (); 723 reset_specialization (); 724} 725 726/* Any template <>'s that we have seen thus far are not referring to a 727 function specialization. */ 728 729void 730reset_specialization () 731{ 732 processing_specialization = 0; 733 template_header_count = 0; 734} 735 736/* We've just seen a template header. If SPECIALIZATION is nonzero, 737 it was of the form template <>. */ 738 739static void 740note_template_header (specialization) 741 int specialization; 742{ 743 processing_specialization = specialization; 744 template_header_count++; 745} 746 747/* We're beginning an explicit instantiation. */ 748 749void 750begin_explicit_instantiation () 751{ 752 ++processing_explicit_instantiation; 753} 754 755 756void 757end_explicit_instantiation () 758{ 759 my_friendly_assert(processing_explicit_instantiation > 0, 0); 760 --processing_explicit_instantiation; 761} 762 763/* The TYPE is being declared. If it is a template type, that means it 764 is a partial specialization. Do appropriate error-checking. */ 765 766void 767maybe_process_partial_specialization (type) 768 tree type; 769{ 770 /* TYPE maybe an ERROR_MARK_NODE. */ 771 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE; 772 773 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) 774 { 775 /* This is for ordinary explicit specialization and partial 776 specialization of a template class such as: 777 778 template <> class C<int>; 779 780 or: 781 782 template <class T> class C<T*>; 783 784 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ 785 786 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) 787 && !COMPLETE_TYPE_P (type)) 788 { 789 if (current_namespace 790 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) 791 { 792 pedwarn ("specializing `%#T' in different namespace", type); 793 cp_pedwarn_at (" from definition of `%#D'", 794 CLASSTYPE_TI_TEMPLATE (type)); 795 } 796 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 797 if (processing_template_decl) 798 push_template_decl (TYPE_MAIN_DECL (type)); 799 } 800 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) 801 error ("specialization of `%T' after instantiation", type); 802 } 803 else if (CLASS_TYPE_P (type) 804 && !CLASSTYPE_USE_TEMPLATE (type) 805 && CLASSTYPE_TEMPLATE_INFO (type) 806 && context && CLASS_TYPE_P (context) 807 && CLASSTYPE_TEMPLATE_INFO (context)) 808 { 809 /* This is for an explicit specialization of member class 810 template according to [temp.expl.spec/18]: 811 812 template <> template <class U> class C<int>::D; 813 814 The context `C<int>' must be an implicit instantiation. 815 Otherwise this is just a member class template declared 816 earlier like: 817 818 template <> class C<int> { template <class U> class D; }; 819 template <> template <class U> class C<int>::D; 820 821 In the first case, `C<int>::D' is a specialization of `C<T>::D' 822 while in the second case, `C<int>::D' is a primary template 823 and `C<T>::D' may not exist. */ 824 825 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) 826 && !COMPLETE_TYPE_P (type)) 827 { 828 tree t; 829 830 if (current_namespace 831 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) 832 { 833 pedwarn ("specializing `%#T' in different namespace", type); 834 cp_pedwarn_at (" from definition of `%#D'", 835 CLASSTYPE_TI_TEMPLATE (type)); 836 } 837 838 /* Check for invalid specialization after instantiation: 839 840 template <> template <> class C<int>::D<int>; 841 template <> template <class U> class C<int>::D; */ 842 843 for (t = DECL_TEMPLATE_INSTANTIATIONS 844 (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); 845 t; t = TREE_CHAIN (t)) 846 if (TREE_VALUE (t) != type 847 && TYPE_CONTEXT (TREE_VALUE (t)) == context) 848 error ("specialization `%T' after instantiation `%T'", 849 type, TREE_VALUE (t)); 850 851 /* Mark TYPE as a specialization. And as a result, we only 852 have one level of template argument for the innermost 853 class template. */ 854 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 855 CLASSTYPE_TI_ARGS (type) 856 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 857 } 858 } 859 else if (processing_specialization) 860 error ("explicit specialization of non-template `%T'", type); 861} 862 863/* Retrieve the specialization (in the sense of [temp.spec] - a 864 specialization is either an instantiation or an explicit 865 specialization) of TMPL for the given template ARGS. If there is 866 no such specialization, return NULL_TREE. The ARGS are a vector of 867 arguments, or a vector of vectors of arguments, in the case of 868 templates with more than one level of parameters. */ 869 870static tree 871retrieve_specialization (tmpl, args) 872 tree tmpl; 873 tree args; 874{ 875 tree s; 876 877 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); 878 879 /* There should be as many levels of arguments as there are 880 levels of parameters. */ 881 my_friendly_assert (TMPL_ARGS_DEPTH (args) 882 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 883 0); 884 885 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 886 s != NULL_TREE; 887 s = TREE_CHAIN (s)) 888 if (comp_template_args (TREE_PURPOSE (s), args)) 889 return TREE_VALUE (s); 890 891 return NULL_TREE; 892} 893 894/* Like retrieve_specialization, but for local declarations. */ 895 896static tree 897retrieve_local_specialization (tmpl) 898 tree tmpl; 899{ 900 return (tree) htab_find (local_specializations, tmpl); 901} 902 903/* Returns nonzero iff DECL is a specialization of TMPL. */ 904 905int 906is_specialization_of (decl, tmpl) 907 tree decl; 908 tree tmpl; 909{ 910 tree t; 911 912 if (TREE_CODE (decl) == FUNCTION_DECL) 913 { 914 for (t = decl; 915 t != NULL_TREE; 916 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) 917 if (t == tmpl) 918 return 1; 919 } 920 else 921 { 922 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); 923 924 for (t = TREE_TYPE (decl); 925 t != NULL_TREE; 926 t = CLASSTYPE_USE_TEMPLATE (t) 927 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) 928 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) 929 return 1; 930 } 931 932 return 0; 933} 934 935/* Register the specialization SPEC as a specialization of TMPL with 936 the indicated ARGS. Returns SPEC, or an equivalent prior 937 declaration, if available. */ 938 939static tree 940register_specialization (spec, tmpl, args) 941 tree spec; 942 tree tmpl; 943 tree args; 944{ 945 tree s; 946 947 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); 948 949 if (TREE_CODE (spec) == FUNCTION_DECL 950 && uses_template_parms (DECL_TI_ARGS (spec))) 951 /* This is the FUNCTION_DECL for a partial instantiation. Don't 952 register it; we want the corresponding TEMPLATE_DECL instead. 953 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than 954 the more obvious `uses_template_parms (spec)' to avoid problems 955 with default function arguments. In particular, given 956 something like this: 957 958 template <class T> void f(T t1, T t = T()) 959 960 the default argument expression is not substituted for in an 961 instantiation unless and until it is actually needed. */ 962 return spec; 963 964 /* There should be as many levels of arguments as there are 965 levels of parameters. */ 966 my_friendly_assert (TMPL_ARGS_DEPTH (args) 967 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 968 0); 969 970 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 971 s != NULL_TREE; 972 s = TREE_CHAIN (s)) 973 { 974 tree fn = TREE_VALUE (s); 975 976 /* We can sometimes try to re-register a specialization that we've 977 already got. In particular, regenerate_decl_from_template 978 calls duplicate_decls which will update the specialization 979 list. But, we'll still get called again here anyhow. It's 980 more convenient to simply allow this than to try to prevent it. */ 981 if (fn == spec) 982 return spec; 983 else if (comp_template_args (TREE_PURPOSE (s), args)) 984 { 985 if (DECL_TEMPLATE_SPECIALIZATION (spec)) 986 { 987 if (DECL_TEMPLATE_INSTANTIATION (fn)) 988 { 989 if (TREE_USED (fn) 990 || DECL_EXPLICIT_INSTANTIATION (fn)) 991 { 992 error ("specialization of %D after instantiation", 993 fn); 994 return spec; 995 } 996 else 997 { 998 /* This situation should occur only if the first 999 specialization is an implicit instantiation, 1000 the second is an explicit specialization, and 1001 the implicit instantiation has not yet been 1002 used. That situation can occur if we have 1003 implicitly instantiated a member function and 1004 then specialized it later. 1005 1006 We can also wind up here if a friend 1007 declaration that looked like an instantiation 1008 turns out to be a specialization: 1009 1010 template <class T> void foo(T); 1011 class S { friend void foo<>(int) }; 1012 template <> void foo(int); 1013 1014 We transform the existing DECL in place so that 1015 any pointers to it become pointers to the 1016 updated declaration. 1017 1018 If there was a definition for the template, but 1019 not for the specialization, we want this to 1020 look as if there is no definition, and vice 1021 versa. */ 1022 DECL_INITIAL (fn) = NULL_TREE; 1023 duplicate_decls (spec, fn); 1024 1025 return fn; 1026 } 1027 } 1028 else if (DECL_TEMPLATE_SPECIALIZATION (fn)) 1029 { 1030 duplicate_decls (spec, fn); 1031 return fn; 1032 } 1033 } 1034 } 1035 } 1036 1037 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) 1038 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); 1039 1040 return spec; 1041} 1042 1043/* Unregister the specialization SPEC as a specialization of TMPL. 1044 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true 1045 if the SPEC was listed as a specialization of TMPL. */ 1046 1047static int 1048reregister_specialization (tree spec, tree tmpl, tree new_spec) 1049{ 1050 tree* s; 1051 1052 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 1053 *s != NULL_TREE; 1054 s = &TREE_CHAIN (*s)) 1055 if (TREE_VALUE (*s) == spec) 1056 { 1057 if (!new_spec) 1058 *s = TREE_CHAIN (*s); 1059 else 1060 TREE_VALUE (*s) = new_spec; 1061 return 1; 1062 } 1063 1064 return 0; 1065} 1066 1067/* Like register_specialization, but for local declarations. We are 1068 registering SPEC, an instantiation of TMPL. */ 1069 1070static void 1071register_local_specialization (spec, tmpl) 1072 tree spec; 1073 tree tmpl; 1074{ 1075 void **slot; 1076 1077 slot = htab_find_slot (local_specializations, tmpl, INSERT); 1078 *slot = spec; 1079} 1080 1081/* Print the list of candidate FNS in an error message. */ 1082 1083void 1084print_candidates (fns) 1085 tree fns; 1086{ 1087 tree fn; 1088 1089 const char *str = "candidates are:"; 1090 1091 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) 1092 { 1093 tree f; 1094 1095 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) 1096 cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); 1097 str = " "; 1098 } 1099} 1100 1101/* Returns the template (one of the functions given by TEMPLATE_ID) 1102 which can be specialized to match the indicated DECL with the 1103 explicit template args given in TEMPLATE_ID. The DECL may be 1104 NULL_TREE if none is available. In that case, the functions in 1105 TEMPLATE_ID are non-members. 1106 1107 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a 1108 specialization of a member template. 1109 1110 The template args (those explicitly specified and those deduced) 1111 are output in a newly created vector *TARGS_OUT. 1112 1113 If it is impossible to determine the result, an error message is 1114 issued. The error_mark_node is returned to indicate failure. */ 1115 1116static tree 1117determine_specialization (template_id, decl, targs_out, 1118 need_member_template) 1119 tree template_id; 1120 tree decl; 1121 tree* targs_out; 1122 int need_member_template; 1123{ 1124 tree fns; 1125 tree targs; 1126 tree explicit_targs; 1127 tree candidates = NULL_TREE; 1128 tree templates = NULL_TREE; 1129 1130 *targs_out = NULL_TREE; 1131 1132 if (template_id == error_mark_node) 1133 return error_mark_node; 1134 1135 fns = TREE_OPERAND (template_id, 0); 1136 explicit_targs = TREE_OPERAND (template_id, 1); 1137 1138 if (fns == error_mark_node) 1139 return error_mark_node; 1140 1141 /* Check for baselinks. */ 1142 if (BASELINK_P (fns)) 1143 fns = BASELINK_FUNCTIONS (fns); 1144 1145 if (!is_overloaded_fn (fns)) 1146 { 1147 error ("`%D' is not a function template", fns); 1148 return error_mark_node; 1149 } 1150 1151 for (; fns; fns = OVL_NEXT (fns)) 1152 { 1153 tree fn = OVL_CURRENT (fns); 1154 1155 if (TREE_CODE (fn) == TEMPLATE_DECL) 1156 { 1157 tree decl_arg_types; 1158 1159 /* DECL might be a specialization of FN. */ 1160 1161 /* Adjust the type of DECL in case FN is a static member. */ 1162 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 1163 if (DECL_STATIC_FUNCTION_P (fn) 1164 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1165 decl_arg_types = TREE_CHAIN (decl_arg_types); 1166 1167 /* Check that the number of function parameters matches. 1168 For example, 1169 template <class T> void f(int i = 0); 1170 template <> void f<int>(); 1171 The specialization f<int> is invalid but is not caught 1172 by get_bindings below. */ 1173 1174 if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn))) 1175 != list_length (decl_arg_types)) 1176 continue; 1177 1178 /* See whether this function might be a specialization of this 1179 template. */ 1180 targs = get_bindings (fn, decl, explicit_targs); 1181 1182 if (!targs) 1183 /* We cannot deduce template arguments that when used to 1184 specialize TMPL will produce DECL. */ 1185 continue; 1186 1187 /* Save this template, and the arguments deduced. */ 1188 templates = tree_cons (targs, fn, templates); 1189 } 1190 else if (need_member_template) 1191 /* FN is an ordinary member function, and we need a 1192 specialization of a member template. */ 1193 ; 1194 else if (TREE_CODE (fn) != FUNCTION_DECL) 1195 /* We can get IDENTIFIER_NODEs here in certain erroneous 1196 cases. */ 1197 ; 1198 else if (!DECL_FUNCTION_MEMBER_P (fn)) 1199 /* This is just an ordinary non-member function. Nothing can 1200 be a specialization of that. */ 1201 ; 1202 else if (DECL_ARTIFICIAL (fn)) 1203 /* Cannot specialize functions that are created implicitly. */ 1204 ; 1205 else 1206 { 1207 tree decl_arg_types; 1208 1209 /* This is an ordinary member function. However, since 1210 we're here, we can assume it's enclosing class is a 1211 template class. For example, 1212 1213 template <typename T> struct S { void f(); }; 1214 template <> void S<int>::f() {} 1215 1216 Here, S<int>::f is a non-template, but S<int> is a 1217 template class. If FN has the same type as DECL, we 1218 might be in business. */ 1219 1220 if (!DECL_TEMPLATE_INFO (fn)) 1221 /* Its enclosing class is an explicit specialization 1222 of a template class. This is not a candidate. */ 1223 continue; 1224 1225 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 1226 TREE_TYPE (TREE_TYPE (fn)))) 1227 /* The return types differ. */ 1228 continue; 1229 1230 /* Adjust the type of DECL in case FN is a static member. */ 1231 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 1232 if (DECL_STATIC_FUNCTION_P (fn) 1233 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1234 decl_arg_types = TREE_CHAIN (decl_arg_types); 1235 1236 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 1237 decl_arg_types)) 1238 /* They match! */ 1239 candidates = tree_cons (NULL_TREE, fn, candidates); 1240 } 1241 } 1242 1243 if (templates && TREE_CHAIN (templates)) 1244 { 1245 /* We have: 1246 1247 [temp.expl.spec] 1248 1249 It is possible for a specialization with a given function 1250 signature to be instantiated from more than one function 1251 template. In such cases, explicit specification of the 1252 template arguments must be used to uniquely identify the 1253 function template specialization being specialized. 1254 1255 Note that here, there's no suggestion that we're supposed to 1256 determine which of the candidate templates is most 1257 specialized. However, we, also have: 1258 1259 [temp.func.order] 1260 1261 Partial ordering of overloaded function template 1262 declarations is used in the following contexts to select 1263 the function template to which a function template 1264 specialization refers: 1265 1266 -- when an explicit specialization refers to a function 1267 template. 1268 1269 So, we do use the partial ordering rules, at least for now. 1270 This extension can only serve to make invalid programs valid, 1271 so it's safe. And, there is strong anecdotal evidence that 1272 the committee intended the partial ordering rules to apply; 1273 the EDG front-end has that behavior, and John Spicer claims 1274 that the committee simply forgot to delete the wording in 1275 [temp.expl.spec]. */ 1276 tree tmpl = most_specialized (templates, decl, explicit_targs); 1277 if (tmpl && tmpl != error_mark_node) 1278 { 1279 targs = get_bindings (tmpl, decl, explicit_targs); 1280 templates = tree_cons (targs, tmpl, NULL_TREE); 1281 } 1282 } 1283 1284 if (templates == NULL_TREE && candidates == NULL_TREE) 1285 { 1286 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", 1287 template_id, decl); 1288 return error_mark_node; 1289 } 1290 else if ((templates && TREE_CHAIN (templates)) 1291 || (candidates && TREE_CHAIN (candidates)) 1292 || (templates && candidates)) 1293 { 1294 cp_error_at ("ambiguous template specialization `%D' for `%+D'", 1295 template_id, decl); 1296 chainon (candidates, templates); 1297 print_candidates (candidates); 1298 return error_mark_node; 1299 } 1300 1301 /* We have one, and exactly one, match. */ 1302 if (candidates) 1303 { 1304 /* It was a specialization of an ordinary member function in a 1305 template class. */ 1306 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); 1307 return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); 1308 } 1309 1310 /* It was a specialization of a template. */ 1311 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); 1312 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) 1313 { 1314 *targs_out = copy_node (targs); 1315 SET_TMPL_ARGS_LEVEL (*targs_out, 1316 TMPL_ARGS_DEPTH (*targs_out), 1317 TREE_PURPOSE (templates)); 1318 } 1319 else 1320 *targs_out = TREE_PURPOSE (templates); 1321 return TREE_VALUE (templates); 1322} 1323 1324/* Returns a chain of parameter types, exactly like the SPEC_TYPES, 1325 but with the default argument values filled in from those in the 1326 TMPL_TYPES. */ 1327 1328static tree 1329copy_default_args_to_explicit_spec_1 (spec_types, 1330 tmpl_types) 1331 tree spec_types; 1332 tree tmpl_types; 1333{ 1334 tree new_spec_types; 1335 1336 if (!spec_types) 1337 return NULL_TREE; 1338 1339 if (spec_types == void_list_node) 1340 return void_list_node; 1341 1342 /* Substitute into the rest of the list. */ 1343 new_spec_types = 1344 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), 1345 TREE_CHAIN (tmpl_types)); 1346 1347 /* Add the default argument for this parameter. */ 1348 return hash_tree_cons (TREE_PURPOSE (tmpl_types), 1349 TREE_VALUE (spec_types), 1350 new_spec_types); 1351} 1352 1353/* DECL is an explicit specialization. Replicate default arguments 1354 from the template it specializes. (That way, code like: 1355 1356 template <class T> void f(T = 3); 1357 template <> void f(double); 1358 void g () { f (); } 1359 1360 works, as required.) An alternative approach would be to look up 1361 the correct default arguments at the call-site, but this approach 1362 is consistent with how implicit instantiations are handled. */ 1363 1364static void 1365copy_default_args_to_explicit_spec (decl) 1366 tree decl; 1367{ 1368 tree tmpl; 1369 tree spec_types; 1370 tree tmpl_types; 1371 tree new_spec_types; 1372 tree old_type; 1373 tree new_type; 1374 tree t; 1375 tree object_type = NULL_TREE; 1376 tree in_charge = NULL_TREE; 1377 tree vtt = NULL_TREE; 1378 1379 /* See if there's anything we need to do. */ 1380 tmpl = DECL_TI_TEMPLATE (decl); 1381 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); 1382 for (t = tmpl_types; t; t = TREE_CHAIN (t)) 1383 if (TREE_PURPOSE (t)) 1384 break; 1385 if (!t) 1386 return; 1387 1388 old_type = TREE_TYPE (decl); 1389 spec_types = TYPE_ARG_TYPES (old_type); 1390 1391 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1392 { 1393 /* Remove the this pointer, but remember the object's type for 1394 CV quals. */ 1395 object_type = TREE_TYPE (TREE_VALUE (spec_types)); 1396 spec_types = TREE_CHAIN (spec_types); 1397 tmpl_types = TREE_CHAIN (tmpl_types); 1398 1399 if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 1400 { 1401 /* DECL may contain more parameters than TMPL due to the extra 1402 in-charge parameter in constructors and destructors. */ 1403 in_charge = spec_types; 1404 spec_types = TREE_CHAIN (spec_types); 1405 } 1406 if (DECL_HAS_VTT_PARM_P (decl)) 1407 { 1408 vtt = spec_types; 1409 spec_types = TREE_CHAIN (spec_types); 1410 } 1411 } 1412 1413 /* Compute the merged default arguments. */ 1414 new_spec_types = 1415 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); 1416 1417 /* Compute the new FUNCTION_TYPE. */ 1418 if (object_type) 1419 { 1420 if (vtt) 1421 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), 1422 TREE_VALUE (vtt), 1423 new_spec_types); 1424 1425 if (in_charge) 1426 /* Put the in-charge parameter back. */ 1427 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), 1428 TREE_VALUE (in_charge), 1429 new_spec_types); 1430 1431 new_type = build_cplus_method_type (object_type, 1432 TREE_TYPE (old_type), 1433 new_spec_types); 1434 } 1435 else 1436 new_type = build_function_type (TREE_TYPE (old_type), 1437 new_spec_types); 1438 new_type = build_type_attribute_variant (new_type, 1439 TYPE_ATTRIBUTES (old_type)); 1440 new_type = build_exception_variant (new_type, 1441 TYPE_RAISES_EXCEPTIONS (old_type)); 1442 TREE_TYPE (decl) = new_type; 1443} 1444 1445/* Check to see if the function just declared, as indicated in 1446 DECLARATOR, and in DECL, is a specialization of a function 1447 template. We may also discover that the declaration is an explicit 1448 instantiation at this point. 1449 1450 Returns DECL, or an equivalent declaration that should be used 1451 instead if all goes well. Issues an error message if something is 1452 amiss. Returns error_mark_node if the error is not easily 1453 recoverable. 1454 1455 FLAGS is a bitmask consisting of the following flags: 1456 1457 2: The function has a definition. 1458 4: The function is a friend. 1459 1460 The TEMPLATE_COUNT is the number of references to qualifying 1461 template classes that appeared in the name of the function. For 1462 example, in 1463 1464 template <class T> struct S { void f(); }; 1465 void S<int>::f(); 1466 1467 the TEMPLATE_COUNT would be 1. However, explicitly specialized 1468 classes are not counted in the TEMPLATE_COUNT, so that in 1469 1470 template <class T> struct S {}; 1471 template <> struct S<int> { void f(); } 1472 template <> void S<int>::f(); 1473 1474 the TEMPLATE_COUNT would be 0. (Note that this declaration is 1475 invalid; there should be no template <>.) 1476 1477 If the function is a specialization, it is marked as such via 1478 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO 1479 is set up correctly, and it is added to the list of specializations 1480 for that template. */ 1481 1482tree 1483check_explicit_specialization (declarator, decl, template_count, flags) 1484 tree declarator; 1485 tree decl; 1486 int template_count; 1487 int flags; 1488{ 1489 int have_def = flags & 2; 1490 int is_friend = flags & 4; 1491 int specialization = 0; 1492 int explicit_instantiation = 0; 1493 int member_specialization = 0; 1494 tree ctype = DECL_CLASS_CONTEXT (decl); 1495 tree dname = DECL_NAME (decl); 1496 tmpl_spec_kind tsk; 1497 1498 tsk = current_tmpl_spec_kind (template_count); 1499 1500 switch (tsk) 1501 { 1502 case tsk_none: 1503 if (processing_specialization) 1504 { 1505 specialization = 1; 1506 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 1507 } 1508 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 1509 { 1510 if (is_friend) 1511 /* This could be something like: 1512 1513 template <class T> void f(T); 1514 class S { friend void f<>(int); } */ 1515 specialization = 1; 1516 else 1517 { 1518 /* This case handles bogus declarations like template <> 1519 template <class T> void f<int>(); */ 1520 1521 error ("template-id `%D' in declaration of primary template", 1522 declarator); 1523 return decl; 1524 } 1525 } 1526 break; 1527 1528 case tsk_invalid_member_spec: 1529 /* The error has already been reported in 1530 check_specialization_scope. */ 1531 return error_mark_node; 1532 1533 case tsk_invalid_expl_inst: 1534 error ("template parameter list used in explicit instantiation"); 1535 1536 /* Fall through. */ 1537 1538 case tsk_expl_inst: 1539 if (have_def) 1540 error ("definition provided for explicit instantiation"); 1541 1542 explicit_instantiation = 1; 1543 break; 1544 1545 case tsk_excessive_parms: 1546 error ("too many template parameter lists in declaration of `%D'", 1547 decl); 1548 return error_mark_node; 1549 1550 /* Fall through. */ 1551 case tsk_expl_spec: 1552 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 1553 if (ctype) 1554 member_specialization = 1; 1555 else 1556 specialization = 1; 1557 break; 1558 1559 case tsk_insufficient_parms: 1560 if (template_header_count) 1561 { 1562 error("too few template parameter lists in declaration of `%D'", 1563 decl); 1564 return decl; 1565 } 1566 else if (ctype != NULL_TREE 1567 && !TYPE_BEING_DEFINED (ctype) 1568 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) 1569 && !is_friend) 1570 { 1571 /* For backwards compatibility, we accept: 1572 1573 template <class T> struct S { void f(); }; 1574 void S<int>::f() {} // Missing template <> 1575 1576 That used to be valid C++. */ 1577 if (pedantic) 1578 pedwarn 1579 ("explicit specialization not preceded by `template <>'"); 1580 specialization = 1; 1581 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 1582 } 1583 break; 1584 1585 case tsk_template: 1586 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 1587 { 1588 /* This case handles bogus declarations like template <> 1589 template <class T> void f<int>(); */ 1590 1591 if (uses_template_parms (declarator)) 1592 error ("partial specialization `%D' of function template", 1593 declarator); 1594 else 1595 error ("template-id `%D' in declaration of primary template", 1596 declarator); 1597 return decl; 1598 } 1599 1600 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) 1601 /* This is a specialization of a member template, without 1602 specialization the containing class. Something like: 1603 1604 template <class T> struct S { 1605 template <class U> void f (U); 1606 }; 1607 template <> template <class U> void S<int>::f(U) {} 1608 1609 That's a specialization -- but of the entire template. */ 1610 specialization = 1; 1611 break; 1612 1613 default: 1614 abort (); 1615 } 1616 1617 if (specialization || member_specialization) 1618 { 1619 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); 1620 for (; t; t = TREE_CHAIN (t)) 1621 if (TREE_PURPOSE (t)) 1622 { 1623 pedwarn 1624 ("default argument specified in explicit specialization"); 1625 break; 1626 } 1627 if (current_lang_name == lang_name_c) 1628 error ("template specialization with C linkage"); 1629 } 1630 1631 if (specialization || member_specialization || explicit_instantiation) 1632 { 1633 tree tmpl = NULL_TREE; 1634 tree targs = NULL_TREE; 1635 1636 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ 1637 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) 1638 { 1639 tree fns; 1640 1641 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 1642 0); 1643 if (!ctype) 1644 fns = IDENTIFIER_NAMESPACE_VALUE (dname); 1645 else 1646 fns = dname; 1647 1648 declarator = 1649 lookup_template_function (fns, NULL_TREE); 1650 } 1651 1652 if (declarator == error_mark_node) 1653 return error_mark_node; 1654 1655 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) 1656 { 1657 if (!explicit_instantiation) 1658 /* A specialization in class scope. This is invalid, 1659 but the error will already have been flagged by 1660 check_specialization_scope. */ 1661 return error_mark_node; 1662 else 1663 { 1664 /* It's not valid to write an explicit instantiation in 1665 class scope, e.g.: 1666 1667 class C { template void f(); } 1668 1669 This case is caught by the parser. However, on 1670 something like: 1671 1672 template class C { void f(); }; 1673 1674 (which is invalid) we can get here. The error will be 1675 issued later. */ 1676 ; 1677 } 1678 1679 return decl; 1680 } 1681 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) 1682 { 1683 /* A friend declaration. We can't do much, because we don't 1684 know what this resolves to, yet. */ 1685 my_friendly_assert (is_friend != 0, 0); 1686 my_friendly_assert (!explicit_instantiation, 0); 1687 SET_DECL_IMPLICIT_INSTANTIATION (decl); 1688 return decl; 1689 } 1690 else if (ctype != NULL_TREE 1691 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == 1692 IDENTIFIER_NODE)) 1693 { 1694 /* Find the list of functions in ctype that have the same 1695 name as the declared function. */ 1696 tree name = TREE_OPERAND (declarator, 0); 1697 tree fns = NULL_TREE; 1698 int idx; 1699 1700 if (constructor_name_p (name, ctype)) 1701 { 1702 int is_constructor = DECL_CONSTRUCTOR_P (decl); 1703 1704 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) 1705 : !TYPE_HAS_DESTRUCTOR (ctype)) 1706 { 1707 /* From [temp.expl.spec]: 1708 1709 If such an explicit specialization for the member 1710 of a class template names an implicitly-declared 1711 special member function (clause _special_), the 1712 program is ill-formed. 1713 1714 Similar language is found in [temp.explicit]. */ 1715 error ("specialization of implicitly-declared special member function"); 1716 return error_mark_node; 1717 } 1718 1719 name = is_constructor ? ctor_identifier : dtor_identifier; 1720 } 1721 1722 if (!DECL_CONV_FN_P (decl)) 1723 { 1724 idx = lookup_fnfields_1 (ctype, name); 1725 if (idx >= 0) 1726 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); 1727 } 1728 else 1729 { 1730 tree methods; 1731 1732 /* For a type-conversion operator, we cannot do a 1733 name-based lookup. We might be looking for `operator 1734 int' which will be a specialization of `operator T'. 1735 So, we find *all* the conversion operators, and then 1736 select from them. */ 1737 fns = NULL_TREE; 1738 1739 methods = CLASSTYPE_METHOD_VEC (ctype); 1740 if (methods) 1741 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; 1742 idx < TREE_VEC_LENGTH (methods); ++idx) 1743 { 1744 tree ovl = TREE_VEC_ELT (methods, idx); 1745 1746 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) 1747 /* There are no more conversion functions. */ 1748 break; 1749 1750 /* Glue all these conversion functions together 1751 with those we already have. */ 1752 for (; ovl; ovl = OVL_NEXT (ovl)) 1753 fns = ovl_cons (OVL_CURRENT (ovl), fns); 1754 } 1755 } 1756 1757 if (fns == NULL_TREE) 1758 { 1759 error ("no member function `%D' declared in `%T'", 1760 name, ctype); 1761 return error_mark_node; 1762 } 1763 else 1764 TREE_OPERAND (declarator, 0) = fns; 1765 } 1766 1767 /* Figure out what exactly is being specialized at this point. 1768 Note that for an explicit instantiation, even one for a 1769 member function, we cannot tell apriori whether the 1770 instantiation is for a member template, or just a member 1771 function of a template class. Even if a member template is 1772 being instantiated, the member template arguments may be 1773 elided if they can be deduced from the rest of the 1774 declaration. */ 1775 tmpl = determine_specialization (declarator, decl, 1776 &targs, 1777 member_specialization); 1778 1779 if (!tmpl || tmpl == error_mark_node) 1780 /* We couldn't figure out what this declaration was 1781 specializing. */ 1782 return error_mark_node; 1783 else 1784 { 1785 tree gen_tmpl = most_general_template (tmpl); 1786 1787 if (explicit_instantiation) 1788 { 1789 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that 1790 is done by do_decl_instantiation later. */ 1791 1792 int arg_depth = TMPL_ARGS_DEPTH (targs); 1793 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 1794 1795 if (arg_depth > parm_depth) 1796 { 1797 /* If TMPL is not the most general template (for 1798 example, if TMPL is a friend template that is 1799 injected into namespace scope), then there will 1800 be too many levels of TARGS. Remove some of them 1801 here. */ 1802 int i; 1803 tree new_targs; 1804 1805 new_targs = make_tree_vec (parm_depth); 1806 for (i = arg_depth - parm_depth; i < arg_depth; ++i) 1807 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) 1808 = TREE_VEC_ELT (targs, i); 1809 targs = new_targs; 1810 } 1811 1812 return instantiate_template (tmpl, targs); 1813 } 1814 1815 /* If we thought that the DECL was a member function, but it 1816 turns out to be specializing a static member function, 1817 make DECL a static member function as well. We also have 1818 to adjust last_function_parms to avoid confusing 1819 start_function later. */ 1820 if (DECL_STATIC_FUNCTION_P (tmpl) 1821 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1822 { 1823 revert_static_member_fn (decl); 1824 last_function_parms = TREE_CHAIN (last_function_parms); 1825 } 1826 1827 /* If this is a specialization of a member template of a 1828 template class. In we want to return the TEMPLATE_DECL, 1829 not the specialization of it. */ 1830 if (tsk == tsk_template) 1831 { 1832 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 1833 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; 1834 return tmpl; 1835 } 1836 1837 /* Set up the DECL_TEMPLATE_INFO for DECL. */ 1838 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); 1839 1840 /* Inherit default function arguments from the template 1841 DECL is specializing. */ 1842 copy_default_args_to_explicit_spec (decl); 1843 1844 /* This specialization has the same protection as the 1845 template it specializes. */ 1846 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); 1847 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); 1848 1849 if (is_friend && !have_def) 1850 /* This is not really a declaration of a specialization. 1851 It's just the name of an instantiation. But, it's not 1852 a request for an instantiation, either. */ 1853 SET_DECL_IMPLICIT_INSTANTIATION (decl); 1854 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) 1855 /* This is indeed a specialization. In case of constructors 1856 and destructors, we need in-charge and not-in-charge 1857 versions in V3 ABI. */ 1858 clone_function_decl (decl, /*update_method_vec_p=*/0); 1859 1860 /* Register this specialization so that we can find it 1861 again. */ 1862 decl = register_specialization (decl, gen_tmpl, targs); 1863 } 1864 } 1865 1866 return decl; 1867} 1868 1869/* TYPE is being declared. Verify that the use of template headers 1870 and such is reasonable. Issue error messages if not. */ 1871 1872void 1873maybe_check_template_type (type) 1874 tree type; 1875{ 1876 if (template_header_count) 1877 { 1878 /* We are in the scope of some `template <...>' header. */ 1879 1880 int context_depth 1881 = template_class_depth_real (TYPE_CONTEXT (type), 1882 /*count_specializations=*/1); 1883 1884 if (template_header_count <= context_depth) 1885 /* This is OK; the template headers are for the context. We 1886 are actually too lenient here; like 1887 check_explicit_specialization we should consider the number 1888 of template types included in the actual declaration. For 1889 example, 1890 1891 template <class T> struct S { 1892 template <class U> template <class V> 1893 struct I {}; 1894 }; 1895 1896 is invalid, but: 1897 1898 template <class T> struct S { 1899 template <class U> struct I; 1900 }; 1901 1902 template <class T> template <class U. 1903 struct S<T>::I {}; 1904 1905 is not. */ 1906 ; 1907 else if (template_header_count > context_depth + 1) 1908 /* There are two many template parameter lists. */ 1909 error ("too many template parameter lists in declaration of `%T'", type); 1910 } 1911} 1912 1913/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template 1914 parameters. These are represented in the same format used for 1915 DECL_TEMPLATE_PARMS. */ 1916 1917int comp_template_parms (parms1, parms2) 1918 tree parms1; 1919 tree parms2; 1920{ 1921 tree p1; 1922 tree p2; 1923 1924 if (parms1 == parms2) 1925 return 1; 1926 1927 for (p1 = parms1, p2 = parms2; 1928 p1 != NULL_TREE && p2 != NULL_TREE; 1929 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) 1930 { 1931 tree t1 = TREE_VALUE (p1); 1932 tree t2 = TREE_VALUE (p2); 1933 int i; 1934 1935 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); 1936 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); 1937 1938 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) 1939 return 0; 1940 1941 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) 1942 { 1943 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); 1944 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); 1945 1946 if (TREE_CODE (parm1) != TREE_CODE (parm2)) 1947 return 0; 1948 1949 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) 1950 continue; 1951 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) 1952 return 0; 1953 } 1954 } 1955 1956 if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) 1957 /* One set of parameters has more parameters lists than the 1958 other. */ 1959 return 0; 1960 1961 return 1; 1962} 1963 1964/* Complain if DECL shadows a template parameter. 1965 1966 [temp.local]: A template-parameter shall not be redeclared within its 1967 scope (including nested scopes). */ 1968 1969void 1970check_template_shadow (decl) 1971 tree decl; 1972{ 1973 tree olddecl; 1974 1975 /* If we're not in a template, we can't possibly shadow a template 1976 parameter. */ 1977 if (!current_template_parms) 1978 return; 1979 1980 /* Figure out what we're shadowing. */ 1981 if (TREE_CODE (decl) == OVERLOAD) 1982 decl = OVL_CURRENT (decl); 1983 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); 1984 1985 /* If there's no previous binding for this name, we're not shadowing 1986 anything, let alone a template parameter. */ 1987 if (!olddecl) 1988 return; 1989 1990 /* If we're not shadowing a template parameter, we're done. Note 1991 that OLDDECL might be an OVERLOAD (or perhaps even an 1992 ERROR_MARK), so we can't just blithely assume it to be a _DECL 1993 node. */ 1994 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) 1995 return; 1996 1997 /* We check for decl != olddecl to avoid bogus errors for using a 1998 name inside a class. We check TPFI to avoid duplicate errors for 1999 inline member templates. */ 2000 if (decl == olddecl 2001 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) 2002 return; 2003 2004 cp_error_at ("declaration of `%#D'", decl); 2005 cp_error_at (" shadows template parm `%#D'", olddecl); 2006} 2007 2008/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, 2009 ORIG_LEVEL, DECL, and TYPE. */ 2010 2011static tree 2012build_template_parm_index (index, level, orig_level, decl, type) 2013 int index; 2014 int level; 2015 int orig_level; 2016 tree decl; 2017 tree type; 2018{ 2019 tree t = make_node (TEMPLATE_PARM_INDEX); 2020 TEMPLATE_PARM_IDX (t) = index; 2021 TEMPLATE_PARM_LEVEL (t) = level; 2022 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; 2023 TEMPLATE_PARM_DECL (t) = decl; 2024 TREE_TYPE (t) = type; 2025 2026 return t; 2027} 2028 2029/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose 2030 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a 2031 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a 2032 new one is created. */ 2033 2034static tree 2035reduce_template_parm_level (index, type, levels) 2036 tree index; 2037 tree type; 2038 int levels; 2039{ 2040 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE 2041 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) 2042 != TEMPLATE_PARM_LEVEL (index) - levels)) 2043 { 2044 tree decl 2045 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), 2046 DECL_NAME (TEMPLATE_PARM_DECL (index)), 2047 type); 2048 tree t 2049 = build_template_parm_index (TEMPLATE_PARM_IDX (index), 2050 TEMPLATE_PARM_LEVEL (index) - levels, 2051 TEMPLATE_PARM_ORIG_LEVEL (index), 2052 decl, type); 2053 TEMPLATE_PARM_DESCENDANTS (index) = t; 2054 2055 DECL_ARTIFICIAL (decl) = 1; 2056 SET_DECL_TEMPLATE_PARM_P (decl); 2057 2058 /* Template template parameters need this. */ 2059 DECL_TEMPLATE_PARMS (decl) 2060 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); 2061 } 2062 2063 return TEMPLATE_PARM_DESCENDANTS (index); 2064} 2065 2066/* Process information from new template parameter NEXT and append it to the 2067 LIST being built. */ 2068 2069tree 2070process_template_parm (list, next) 2071 tree list, next; 2072{ 2073 tree parm; 2074 tree decl = 0; 2075 tree defval; 2076 int is_type, idx; 2077 2078 parm = next; 2079 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); 2080 defval = TREE_PURPOSE (parm); 2081 parm = TREE_VALUE (parm); 2082 is_type = TREE_PURPOSE (parm) == class_type_node; 2083 2084 if (list) 2085 { 2086 tree p = TREE_VALUE (tree_last (list)); 2087 2088 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) 2089 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); 2090 else 2091 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); 2092 ++idx; 2093 } 2094 else 2095 idx = 0; 2096 2097 if (!is_type) 2098 { 2099 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); 2100 /* is a const-param */ 2101 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), 2102 PARM, 0, NULL); 2103 SET_DECL_TEMPLATE_PARM_P (parm); 2104 2105 /* [temp.param] 2106 2107 The top-level cv-qualifiers on the template-parameter are 2108 ignored when determining its type. */ 2109 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); 2110 2111 /* A template parameter is not modifiable. */ 2112 TREE_READONLY (parm) = 1; 2113 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) 2114 TREE_TYPE (parm) = void_type_node; 2115 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); 2116 DECL_INITIAL (parm) = DECL_INITIAL (decl) 2117 = build_template_parm_index (idx, processing_template_decl, 2118 processing_template_decl, 2119 decl, TREE_TYPE (parm)); 2120 } 2121 else 2122 { 2123 tree t; 2124 parm = TREE_VALUE (parm); 2125 2126 if (parm && TREE_CODE (parm) == TEMPLATE_DECL) 2127 { 2128 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); 2129 /* This is for distinguishing between real templates and template 2130 template parameters */ 2131 TREE_TYPE (parm) = t; 2132 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; 2133 decl = parm; 2134 } 2135 else 2136 { 2137 t = make_aggr_type (TEMPLATE_TYPE_PARM); 2138 /* parm is either IDENTIFIER_NODE or NULL_TREE */ 2139 decl = build_decl (TYPE_DECL, parm, t); 2140 } 2141 2142 TYPE_NAME (t) = decl; 2143 TYPE_STUB_DECL (t) = decl; 2144 parm = decl; 2145 TEMPLATE_TYPE_PARM_INDEX (t) 2146 = build_template_parm_index (idx, processing_template_decl, 2147 processing_template_decl, 2148 decl, TREE_TYPE (parm)); 2149 } 2150 DECL_ARTIFICIAL (decl) = 1; 2151 SET_DECL_TEMPLATE_PARM_P (decl); 2152 pushdecl (decl); 2153 parm = build_tree_list (defval, parm); 2154 return chainon (list, parm); 2155} 2156 2157/* The end of a template parameter list has been reached. Process the 2158 tree list into a parameter vector, converting each parameter into a more 2159 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others 2160 as PARM_DECLs. */ 2161 2162tree 2163end_template_parm_list (parms) 2164 tree parms; 2165{ 2166 int nparms; 2167 tree parm, next; 2168 tree saved_parmlist = make_tree_vec (list_length (parms)); 2169 2170 current_template_parms 2171 = tree_cons (size_int (processing_template_decl), 2172 saved_parmlist, current_template_parms); 2173 2174 for (parm = parms, nparms = 0; parm; parm = next, nparms++) 2175 { 2176 next = TREE_CHAIN (parm); 2177 TREE_VEC_ELT (saved_parmlist, nparms) = parm; 2178 TREE_CHAIN (parm) = NULL_TREE; 2179 } 2180 2181 --processing_template_parmlist; 2182 2183 return saved_parmlist; 2184} 2185 2186/* end_template_decl is called after a template declaration is seen. */ 2187 2188void 2189end_template_decl () 2190{ 2191 reset_specialization (); 2192 2193 if (! processing_template_decl) 2194 return; 2195 2196 /* This matches the pushlevel in begin_template_parm_list. */ 2197 finish_scope (); 2198 2199 --processing_template_decl; 2200 current_template_parms = TREE_CHAIN (current_template_parms); 2201} 2202 2203/* Given a template argument vector containing the template PARMS. 2204 The innermost PARMS are given first. */ 2205 2206tree 2207current_template_args () 2208{ 2209 tree header; 2210 tree args = NULL_TREE; 2211 int length = TMPL_PARMS_DEPTH (current_template_parms); 2212 int l = length; 2213 2214 /* If there is only one level of template parameters, we do not 2215 create a TREE_VEC of TREE_VECs. Instead, we return a single 2216 TREE_VEC containing the arguments. */ 2217 if (length > 1) 2218 args = make_tree_vec (length); 2219 2220 for (header = current_template_parms; header; header = TREE_CHAIN (header)) 2221 { 2222 tree a = copy_node (TREE_VALUE (header)); 2223 int i; 2224 2225 TREE_TYPE (a) = NULL_TREE; 2226 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) 2227 { 2228 tree t = TREE_VEC_ELT (a, i); 2229 2230 /* T will be a list if we are called from within a 2231 begin/end_template_parm_list pair, but a vector directly 2232 if within a begin/end_member_template_processing pair. */ 2233 if (TREE_CODE (t) == TREE_LIST) 2234 { 2235 t = TREE_VALUE (t); 2236 2237 if (TREE_CODE (t) == TYPE_DECL 2238 || TREE_CODE (t) == TEMPLATE_DECL) 2239 t = TREE_TYPE (t); 2240 else 2241 t = DECL_INITIAL (t); 2242 TREE_VEC_ELT (a, i) = t; 2243 } 2244 } 2245 2246 if (length > 1) 2247 TREE_VEC_ELT (args, --l) = a; 2248 else 2249 args = a; 2250 } 2251 2252 return args; 2253} 2254 2255/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated 2256 template PARMS. Used by push_template_decl below. */ 2257 2258static tree 2259build_template_decl (decl, parms) 2260 tree decl; 2261 tree parms; 2262{ 2263 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); 2264 DECL_TEMPLATE_PARMS (tmpl) = parms; 2265 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); 2266 if (DECL_LANG_SPECIFIC (decl)) 2267 { 2268 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); 2269 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); 2270 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); 2271 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); 2272 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); 2273 if (DECL_OVERLOADED_OPERATOR_P (decl)) 2274 SET_OVERLOADED_OPERATOR_CODE (tmpl, 2275 DECL_OVERLOADED_OPERATOR_P (decl)); 2276 } 2277 2278 return tmpl; 2279} 2280 2281struct template_parm_data 2282{ 2283 /* The level of the template parameters we are currently 2284 processing. */ 2285 int level; 2286 2287 /* The index of the specialization argument we are currently 2288 processing. */ 2289 int current_arg; 2290 2291 /* An array whose size is the number of template parameters. The 2292 elements are nonzero if the parameter has been used in any one 2293 of the arguments processed so far. */ 2294 int* parms; 2295 2296 /* An array whose size is the number of template arguments. The 2297 elements are nonzero if the argument makes use of template 2298 parameters of this level. */ 2299 int* arg_uses_template_parms; 2300}; 2301 2302/* Subroutine of push_template_decl used to see if each template 2303 parameter in a partial specialization is used in the explicit 2304 argument list. If T is of the LEVEL given in DATA (which is 2305 treated as a template_parm_data*), then DATA->PARMS is marked 2306 appropriately. */ 2307 2308static int 2309mark_template_parm (t, data) 2310 tree t; 2311 void* data; 2312{ 2313 int level; 2314 int idx; 2315 struct template_parm_data* tpd = (struct template_parm_data*) data; 2316 2317 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 2318 { 2319 level = TEMPLATE_PARM_LEVEL (t); 2320 idx = TEMPLATE_PARM_IDX (t); 2321 } 2322 else 2323 { 2324 level = TEMPLATE_TYPE_LEVEL (t); 2325 idx = TEMPLATE_TYPE_IDX (t); 2326 } 2327 2328 if (level == tpd->level) 2329 { 2330 tpd->parms[idx] = 1; 2331 tpd->arg_uses_template_parms[tpd->current_arg] = 1; 2332 } 2333 2334 /* Return zero so that for_each_template_parm will continue the 2335 traversal of the tree; we want to mark *every* template parm. */ 2336 return 0; 2337} 2338 2339/* Process the partial specialization DECL. */ 2340 2341static tree 2342process_partial_specialization (decl) 2343 tree decl; 2344{ 2345 tree type = TREE_TYPE (decl); 2346 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); 2347 tree specargs = CLASSTYPE_TI_ARGS (type); 2348 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); 2349 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); 2350 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); 2351 int nargs = TREE_VEC_LENGTH (inner_args); 2352 int ntparms = TREE_VEC_LENGTH (inner_parms); 2353 int i; 2354 int did_error_intro = 0; 2355 struct template_parm_data tpd; 2356 struct template_parm_data tpd2; 2357 2358 /* We check that each of the template parameters given in the 2359 partial specialization is used in the argument list to the 2360 specialization. For example: 2361 2362 template <class T> struct S; 2363 template <class T> struct S<T*>; 2364 2365 The second declaration is OK because `T*' uses the template 2366 parameter T, whereas 2367 2368 template <class T> struct S<int>; 2369 2370 is no good. Even trickier is: 2371 2372 template <class T> 2373 struct S1 2374 { 2375 template <class U> 2376 struct S2; 2377 template <class U> 2378 struct S2<T>; 2379 }; 2380 2381 The S2<T> declaration is actually invalid; it is a 2382 full-specialization. Of course, 2383 2384 template <class U> 2385 struct S2<T (*)(U)>; 2386 2387 or some such would have been OK. */ 2388 tpd.level = TMPL_PARMS_DEPTH (current_template_parms); 2389 tpd.parms = alloca (sizeof (int) * ntparms); 2390 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms); 2391 2392 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); 2393 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); 2394 for (i = 0; i < nargs; ++i) 2395 { 2396 tpd.current_arg = i; 2397 for_each_template_parm (TREE_VEC_ELT (inner_args, i), 2398 &mark_template_parm, 2399 &tpd, 2400 NULL); 2401 } 2402 for (i = 0; i < ntparms; ++i) 2403 if (tpd.parms[i] == 0) 2404 { 2405 /* One of the template parms was not used in the 2406 specialization. */ 2407 if (!did_error_intro) 2408 { 2409 error ("template parameters not used in partial specialization:"); 2410 did_error_intro = 1; 2411 } 2412 2413 error (" `%D'", 2414 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); 2415 } 2416 2417 /* [temp.class.spec] 2418 2419 The argument list of the specialization shall not be identical to 2420 the implicit argument list of the primary template. */ 2421 if (comp_template_args 2422 (inner_args, 2423 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE 2424 (maintmpl))))) 2425 error ("partial specialization `%T' does not specialize any template arguments", type); 2426 2427 /* [temp.class.spec] 2428 2429 A partially specialized non-type argument expression shall not 2430 involve template parameters of the partial specialization except 2431 when the argument expression is a simple identifier. 2432 2433 The type of a template parameter corresponding to a specialized 2434 non-type argument shall not be dependent on a parameter of the 2435 specialization. */ 2436 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); 2437 tpd2.parms = 0; 2438 for (i = 0; i < nargs; ++i) 2439 { 2440 tree arg = TREE_VEC_ELT (inner_args, i); 2441 if (/* These first two lines are the `non-type' bit. */ 2442 !TYPE_P (arg) 2443 && TREE_CODE (arg) != TEMPLATE_DECL 2444 /* This next line is the `argument expression is not just a 2445 simple identifier' condition and also the `specialized 2446 non-type argument' bit. */ 2447 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) 2448 { 2449 if (tpd.arg_uses_template_parms[i]) 2450 error ("template argument `%E' involves template parameter(s)", arg); 2451 else 2452 { 2453 /* Look at the corresponding template parameter, 2454 marking which template parameters its type depends 2455 upon. */ 2456 tree type = 2457 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, 2458 i))); 2459 2460 if (!tpd2.parms) 2461 { 2462 /* We haven't yet initialized TPD2. Do so now. */ 2463 tpd2.arg_uses_template_parms 2464 = (int*) alloca (sizeof (int) * nargs); 2465 /* The number of parameters here is the number in the 2466 main template, which, as checked in the assertion 2467 above, is NARGS. */ 2468 tpd2.parms = (int*) alloca (sizeof (int) * nargs); 2469 tpd2.level = 2470 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); 2471 } 2472 2473 /* Mark the template parameters. But this time, we're 2474 looking for the template parameters of the main 2475 template, not in the specialization. */ 2476 tpd2.current_arg = i; 2477 tpd2.arg_uses_template_parms[i] = 0; 2478 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs); 2479 for_each_template_parm (type, 2480 &mark_template_parm, 2481 &tpd2, 2482 NULL); 2483 2484 if (tpd2.arg_uses_template_parms [i]) 2485 { 2486 /* The type depended on some template parameters. 2487 If they are fully specialized in the 2488 specialization, that's OK. */ 2489 int j; 2490 for (j = 0; j < nargs; ++j) 2491 if (tpd2.parms[j] != 0 2492 && tpd.arg_uses_template_parms [j]) 2493 { 2494 error ("type `%T' of template argument `%E' depends on template parameter(s)", 2495 type, 2496 arg); 2497 break; 2498 } 2499 } 2500 } 2501 } 2502 } 2503 2504 if (retrieve_specialization (maintmpl, specargs)) 2505 /* We've already got this specialization. */ 2506 return decl; 2507 2508 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) 2509 = tree_cons (inner_args, inner_parms, 2510 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); 2511 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; 2512 return decl; 2513} 2514 2515/* Check that a template declaration's use of default arguments is not 2516 invalid. Here, PARMS are the template parameters. IS_PRIMARY is 2517 nonzero if DECL is the thing declared by a primary template. 2518 IS_PARTIAL is nonzero if DECL is a partial specialization. */ 2519 2520static void 2521check_default_tmpl_args (decl, parms, is_primary, is_partial) 2522 tree decl; 2523 tree parms; 2524 int is_primary; 2525 int is_partial; 2526{ 2527 const char *msg; 2528 int last_level_to_check; 2529 tree parm_level; 2530 2531 /* [temp.param] 2532 2533 A default template-argument shall not be specified in a 2534 function template declaration or a function template definition, nor 2535 in the template-parameter-list of the definition of a member of a 2536 class template. */ 2537 2538 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) 2539 /* You can't have a function template declaration in a local 2540 scope, nor you can you define a member of a class template in a 2541 local scope. */ 2542 return; 2543 2544 if (current_class_type 2545 && !TYPE_BEING_DEFINED (current_class_type) 2546 && DECL_LANG_SPECIFIC (decl) 2547 /* If this is either a friend defined in the scope of the class 2548 or a member function. */ 2549 && ((DECL_CONTEXT (decl) 2550 && same_type_p (DECL_CONTEXT (decl), current_class_type)) 2551 || (DECL_FRIEND_CONTEXT (decl) 2552 && same_type_p (DECL_FRIEND_CONTEXT (decl), 2553 current_class_type))) 2554 /* And, if it was a member function, it really was defined in 2555 the scope of the class. */ 2556 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl))) 2557 /* We already checked these parameters when the template was 2558 declared, so there's no need to do it again now. This function 2559 was defined in class scope, but we're processing it's body now 2560 that the class is complete. */ 2561 return; 2562 2563 /* [temp.param] 2564 2565 If a template-parameter has a default template-argument, all 2566 subsequent template-parameters shall have a default 2567 template-argument supplied. */ 2568 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) 2569 { 2570 tree inner_parms = TREE_VALUE (parm_level); 2571 int ntparms = TREE_VEC_LENGTH (inner_parms); 2572 int seen_def_arg_p = 0; 2573 int i; 2574 2575 for (i = 0; i < ntparms; ++i) 2576 { 2577 tree parm = TREE_VEC_ELT (inner_parms, i); 2578 if (TREE_PURPOSE (parm)) 2579 seen_def_arg_p = 1; 2580 else if (seen_def_arg_p) 2581 { 2582 error ("no default argument for `%D'", TREE_VALUE (parm)); 2583 /* For better subsequent error-recovery, we indicate that 2584 there should have been a default argument. */ 2585 TREE_PURPOSE (parm) = error_mark_node; 2586 } 2587 } 2588 } 2589 2590 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) 2591 /* For an ordinary class template, default template arguments are 2592 allowed at the innermost level, e.g.: 2593 template <class T = int> 2594 struct S {}; 2595 but, in a partial specialization, they're not allowed even 2596 there, as we have in [temp.class.spec]: 2597 2598 The template parameter list of a specialization shall not 2599 contain default template argument values. 2600 2601 So, for a partial specialization, or for a function template, 2602 we look at all of them. */ 2603 ; 2604 else 2605 /* But, for a primary class template that is not a partial 2606 specialization we look at all template parameters except the 2607 innermost ones. */ 2608 parms = TREE_CHAIN (parms); 2609 2610 /* Figure out what error message to issue. */ 2611 if (TREE_CODE (decl) == FUNCTION_DECL) 2612 msg = "default template arguments may not be used in function templates"; 2613 else if (is_partial) 2614 msg = "default template arguments may not be used in partial specializations"; 2615 else 2616 msg = "default argument for template parameter for class enclosing `%D'"; 2617 2618 if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) 2619 /* If we're inside a class definition, there's no need to 2620 examine the parameters to the class itself. On the one 2621 hand, they will be checked when the class is defined, and, 2622 on the other, default arguments are valid in things like: 2623 template <class T = double> 2624 struct S { template <class U> void f(U); }; 2625 Here the default argument for `S' has no bearing on the 2626 declaration of `f'. */ 2627 last_level_to_check = template_class_depth (current_class_type) + 1; 2628 else 2629 /* Check everything. */ 2630 last_level_to_check = 0; 2631 2632 for (parm_level = parms; 2633 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; 2634 parm_level = TREE_CHAIN (parm_level)) 2635 { 2636 tree inner_parms = TREE_VALUE (parm_level); 2637 int i; 2638 int ntparms; 2639 2640 ntparms = TREE_VEC_LENGTH (inner_parms); 2641 for (i = 0; i < ntparms; ++i) 2642 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) 2643 { 2644 if (msg) 2645 { 2646 error (msg, decl); 2647 msg = 0; 2648 } 2649 2650 /* Clear out the default argument so that we are not 2651 confused later. */ 2652 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; 2653 } 2654 2655 /* At this point, if we're still interested in issuing messages, 2656 they must apply to classes surrounding the object declared. */ 2657 if (msg) 2658 msg = "default argument for template parameter for class enclosing `%D'"; 2659 } 2660} 2661 2662/* Worker for push_template_decl_real, called via 2663 for_each_template_parm. DATA is really an int, indicating the 2664 level of the parameters we are interested in. If T is a template 2665 parameter of that level, return nonzero. */ 2666 2667static int 2668template_parm_this_level_p (t, data) 2669 tree t; 2670 void *data; 2671{ 2672 int this_level = *(int *)data; 2673 int level; 2674 2675 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 2676 level = TEMPLATE_PARM_LEVEL (t); 2677 else 2678 level = TEMPLATE_TYPE_LEVEL (t); 2679 return level == this_level; 2680} 2681 2682/* Creates a TEMPLATE_DECL for the indicated DECL using the template 2683 parameters given by current_template_args, or reuses a 2684 previously existing one, if appropriate. Returns the DECL, or an 2685 equivalent one, if it is replaced via a call to duplicate_decls. 2686 2687 If IS_FRIEND is nonzero, DECL is a friend declaration. */ 2688 2689tree 2690push_template_decl_real (decl, is_friend) 2691 tree decl; 2692 int is_friend; 2693{ 2694 tree tmpl; 2695 tree args; 2696 tree info; 2697 tree ctx; 2698 int primary; 2699 int is_partial; 2700 int new_template_p = 0; 2701 2702 /* See if this is a partial specialization. */ 2703 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) 2704 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE 2705 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); 2706 2707 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); 2708 2709 if (is_friend) 2710 /* For a friend, we want the context of the friend function, not 2711 the type of which it is a friend. */ 2712 ctx = DECL_CONTEXT (decl); 2713 else if (CP_DECL_CONTEXT (decl) 2714 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) 2715 /* In the case of a virtual function, we want the class in which 2716 it is defined. */ 2717 ctx = CP_DECL_CONTEXT (decl); 2718 else 2719 /* Otherwise, if we're currently defining some class, the DECL 2720 is assumed to be a member of the class. */ 2721 ctx = current_scope (); 2722 2723 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) 2724 ctx = NULL_TREE; 2725 2726 if (!DECL_CONTEXT (decl)) 2727 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 2728 2729 /* See if this is a primary template. */ 2730 primary = template_parm_scope_p (); 2731 2732 if (primary) 2733 { 2734 if (current_lang_name == lang_name_c) 2735 error ("template with C linkage"); 2736 else if (TREE_CODE (decl) == TYPE_DECL 2737 && ANON_AGGRNAME_P (DECL_NAME (decl))) 2738 error ("template class without a name"); 2739 else if ((DECL_IMPLICIT_TYPEDEF_P (decl) 2740 && CLASS_TYPE_P (TREE_TYPE (decl))) 2741 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) 2742 || TREE_CODE (decl) == FUNCTION_DECL) 2743 /* OK */; 2744 else 2745 error ("template declaration of `%#D'", decl); 2746 } 2747 2748 /* Check to see that the rules regarding the use of default 2749 arguments are not being violated. */ 2750 check_default_tmpl_args (decl, current_template_parms, 2751 primary, is_partial); 2752 2753 if (is_partial) 2754 return process_partial_specialization (decl); 2755 2756 args = current_template_args (); 2757 2758 if (!ctx 2759 || TREE_CODE (ctx) == FUNCTION_DECL 2760 || (TREE_CODE (ctx) != TEMPLATE_TYPE_PARM 2761 && TREE_CODE (ctx) != BOUND_TEMPLATE_TEMPLATE_PARM 2762 && TYPE_BEING_DEFINED (ctx)) 2763 || (is_friend && !DECL_TEMPLATE_INFO (decl))) 2764 { 2765 if (DECL_LANG_SPECIFIC (decl) 2766 && DECL_TEMPLATE_INFO (decl) 2767 && DECL_TI_TEMPLATE (decl)) 2768 tmpl = DECL_TI_TEMPLATE (decl); 2769 /* If DECL is a TYPE_DECL for a class-template, then there won't 2770 be DECL_LANG_SPECIFIC. The information equivalent to 2771 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ 2772 else if (DECL_IMPLICIT_TYPEDEF_P (decl) 2773 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 2774 && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) 2775 { 2776 /* Since a template declaration already existed for this 2777 class-type, we must be redeclaring it here. Make sure 2778 that the redeclaration is valid. */ 2779 redeclare_class_template (TREE_TYPE (decl), 2780 current_template_parms); 2781 /* We don't need to create a new TEMPLATE_DECL; just use the 2782 one we already had. */ 2783 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); 2784 } 2785 else 2786 { 2787 tmpl = build_template_decl (decl, current_template_parms); 2788 new_template_p = 1; 2789 2790 if (DECL_LANG_SPECIFIC (decl) 2791 && DECL_TEMPLATE_SPECIALIZATION (decl)) 2792 { 2793 /* A specialization of a member template of a template 2794 class. */ 2795 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 2796 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); 2797 DECL_TEMPLATE_INFO (decl) = NULL_TREE; 2798 } 2799 } 2800 } 2801 else 2802 { 2803 tree a, t, current, parms; 2804 int i; 2805 2806 if (TREE_CODE (decl) == TYPE_DECL) 2807 { 2808 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) 2809 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) 2810 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 2811 && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) 2812 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); 2813 else 2814 { 2815 error ("`%D' does not declare a template type", decl); 2816 return decl; 2817 } 2818 } 2819 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) 2820 { 2821 error ("template definition of non-template `%#D'", decl); 2822 return decl; 2823 } 2824 else 2825 tmpl = DECL_TI_TEMPLATE (decl); 2826 2827 if (is_member_template (tmpl) 2828 && DECL_FUNCTION_TEMPLATE_P (tmpl) 2829 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 2830 && DECL_TEMPLATE_SPECIALIZATION (decl)) 2831 { 2832 tree new_tmpl; 2833 2834 /* The declaration is a specialization of a member 2835 template, declared outside the class. Therefore, the 2836 innermost template arguments will be NULL, so we 2837 replace them with the arguments determined by the 2838 earlier call to check_explicit_specialization. */ 2839 args = DECL_TI_ARGS (decl); 2840 2841 new_tmpl 2842 = build_template_decl (decl, current_template_parms); 2843 DECL_TEMPLATE_RESULT (new_tmpl) = decl; 2844 TREE_TYPE (new_tmpl) = TREE_TYPE (decl); 2845 DECL_TI_TEMPLATE (decl) = new_tmpl; 2846 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); 2847 DECL_TEMPLATE_INFO (new_tmpl) 2848 = tree_cons (tmpl, args, NULL_TREE); 2849 2850 register_specialization (new_tmpl, 2851 most_general_template (tmpl), 2852 args); 2853 return decl; 2854 } 2855 2856 /* Make sure the template headers we got make sense. */ 2857 2858 parms = DECL_TEMPLATE_PARMS (tmpl); 2859 i = TMPL_PARMS_DEPTH (parms); 2860 if (TMPL_ARGS_DEPTH (args) != i) 2861 { 2862 error ("expected %d levels of template parms for `%#D', got %d", 2863 i, decl, TMPL_ARGS_DEPTH (args)); 2864 } 2865 else 2866 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) 2867 { 2868 a = TMPL_ARGS_LEVEL (args, i); 2869 t = INNERMOST_TEMPLATE_PARMS (parms); 2870 2871 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) 2872 { 2873 if (current == decl) 2874 error ("got %d template parameters for `%#D'", 2875 TREE_VEC_LENGTH (a), decl); 2876 else 2877 error ("got %d template parameters for `%#T'", 2878 TREE_VEC_LENGTH (a), current); 2879 error (" but %d required", TREE_VEC_LENGTH (t)); 2880 } 2881 2882 /* Perhaps we should also check that the parms are used in the 2883 appropriate qualifying scopes in the declarator? */ 2884 2885 if (current == decl) 2886 current = ctx; 2887 else 2888 current = TYPE_CONTEXT (current); 2889 } 2890 } 2891 2892 DECL_TEMPLATE_RESULT (tmpl) = decl; 2893 TREE_TYPE (tmpl) = TREE_TYPE (decl); 2894 2895 /* Push template declarations for global functions and types. Note 2896 that we do not try to push a global template friend declared in a 2897 template class; such a thing may well depend on the template 2898 parameters of the class. */ 2899 if (new_template_p && !ctx 2900 && !(is_friend && template_class_depth (current_class_type) > 0)) 2901 tmpl = pushdecl_namespace_level (tmpl); 2902 2903 if (primary) 2904 { 2905 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; 2906 if (DECL_CONV_FN_P (tmpl)) 2907 { 2908 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 2909 2910 /* It is a conversion operator. See if the type converted to 2911 depends on innermost template operands. */ 2912 2913 if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)), 2914 template_parm_this_level_p, 2915 &depth, 2916 NULL)) 2917 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; 2918 } 2919 } 2920 2921 info = tree_cons (tmpl, args, NULL_TREE); 2922 2923 if (DECL_IMPLICIT_TYPEDEF_P (decl)) 2924 { 2925 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); 2926 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) 2927 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE 2928 /* Don't change the name if we've already set it up. */ 2929 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) 2930 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); 2931 } 2932 else if (DECL_LANG_SPECIFIC (decl)) 2933 DECL_TEMPLATE_INFO (decl) = info; 2934 2935 return DECL_TEMPLATE_RESULT (tmpl); 2936} 2937 2938tree 2939push_template_decl (decl) 2940 tree decl; 2941{ 2942 return push_template_decl_real (decl, 0); 2943} 2944 2945/* Called when a class template TYPE is redeclared with the indicated 2946 template PARMS, e.g.: 2947 2948 template <class T> struct S; 2949 template <class T> struct S {}; */ 2950 2951void 2952redeclare_class_template (type, parms) 2953 tree type; 2954 tree parms; 2955{ 2956 tree tmpl; 2957 tree tmpl_parms; 2958 int i; 2959 2960 if (!TYPE_TEMPLATE_INFO (type)) 2961 { 2962 error ("`%T' is not a template type", type); 2963 return; 2964 } 2965 2966 tmpl = TYPE_TI_TEMPLATE (type); 2967 if (!PRIMARY_TEMPLATE_P (tmpl)) 2968 /* The type is nested in some template class. Nothing to worry 2969 about here; there are no new template parameters for the nested 2970 type. */ 2971 return; 2972 2973 parms = INNERMOST_TEMPLATE_PARMS (parms); 2974 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); 2975 2976 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) 2977 { 2978 cp_error_at ("previous declaration `%D'", tmpl); 2979 error ("used %d template parameter%s instead of %d", 2980 TREE_VEC_LENGTH (tmpl_parms), 2981 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", 2982 TREE_VEC_LENGTH (parms)); 2983 return; 2984 } 2985 2986 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) 2987 { 2988 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); 2989 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 2990 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); 2991 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); 2992 2993 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) 2994 { 2995 cp_error_at ("template parameter `%#D'", tmpl_parm); 2996 error ("redeclared here as `%#D'", parm); 2997 return; 2998 } 2999 3000 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) 3001 { 3002 /* We have in [temp.param]: 3003 3004 A template-parameter may not be given default arguments 3005 by two different declarations in the same scope. */ 3006 error ("redefinition of default argument for `%#D'", parm); 3007 cp_error_at (" original definition appeared here", tmpl_parm); 3008 return; 3009 } 3010 3011 if (parm_default != NULL_TREE) 3012 /* Update the previous template parameters (which are the ones 3013 that will really count) with the new default value. */ 3014 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; 3015 else if (tmpl_default != NULL_TREE) 3016 /* Update the new parameters, too; they'll be used as the 3017 parameters for any members. */ 3018 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; 3019 } 3020} 3021 3022/* Attempt to convert the non-type template parameter EXPR to the 3023 indicated TYPE. If the conversion is successful, return the 3024 converted value. If the conversion is unsuccessful, return 3025 NULL_TREE if we issued an error message, or error_mark_node if we 3026 did not. We issue error messages for out-and-out bad template 3027 parameters, but not simply because the conversion failed, since we 3028 might be just trying to do argument deduction. By the time this 3029 function is called, neither TYPE nor EXPR may make use of template 3030 parameters. */ 3031 3032static tree 3033convert_nontype_argument (type, expr) 3034 tree type; 3035 tree expr; 3036{ 3037 tree expr_type = TREE_TYPE (expr); 3038 3039 /* A template-argument for a non-type, non-template 3040 template-parameter shall be one of: 3041 3042 --an integral constant-expression of integral or enumeration 3043 type; or 3044 3045 --the name of a non-type template-parameter; or 3046 3047 --the name of an object or function with external linkage, 3048 including function templates and function template-ids but 3049 excluding non-static class members, expressed as id-expression; 3050 or 3051 3052 --the address of an object or function with external linkage, 3053 including function templates and function template-ids but 3054 excluding non-static class members, expressed as & id-expression 3055 where the & is optional if the name refers to a function or 3056 array; or 3057 3058 --a pointer to member expressed as described in _expr.unary.op_. */ 3059 3060 /* An integral constant-expression can include const variables or 3061 enumerators. Simplify things by folding them to their values, 3062 unless we're about to bind the declaration to a reference 3063 parameter. */ 3064 if (INTEGRAL_TYPE_P (expr_type) 3065 && TREE_CODE (type) != REFERENCE_TYPE) 3066 expr = decl_constant_value (expr); 3067 3068 if (is_overloaded_fn (expr)) 3069 /* OK for now. We'll check that it has external linkage later. 3070 Check this first since if expr_type is the unknown_type_node 3071 we would otherwise complain below. */ 3072 ; 3073 else if (TYPE_PTRMEM_P (expr_type) 3074 || TYPE_PTRMEMFUNC_P (expr_type)) 3075 { 3076 if (TREE_CODE (expr) != PTRMEM_CST) 3077 goto bad_argument; 3078 } 3079 else if (TYPE_PTR_P (expr_type) 3080 || TYPE_PTRMEM_P (expr_type) 3081 || TREE_CODE (expr_type) == ARRAY_TYPE 3082 || TREE_CODE (type) == REFERENCE_TYPE 3083 /* If expr is the address of an overloaded function, we 3084 will get the unknown_type_node at this point. */ 3085 || expr_type == unknown_type_node) 3086 { 3087 tree referent; 3088 tree e = expr; 3089 STRIP_NOPS (e); 3090 3091 if (TREE_CODE (expr_type) == ARRAY_TYPE 3092 || (TREE_CODE (type) == REFERENCE_TYPE 3093 && TREE_CODE (e) != ADDR_EXPR)) 3094 referent = e; 3095 else 3096 { 3097 if (TREE_CODE (e) != ADDR_EXPR) 3098 { 3099 bad_argument: 3100 error ("`%E' is not a valid template argument", expr); 3101 if (TYPE_PTR_P (expr_type)) 3102 { 3103 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) 3104 error ("it must be the address of a function with external linkage"); 3105 else 3106 error ("it must be the address of an object with external linkage"); 3107 } 3108 else if (TYPE_PTRMEM_P (expr_type) 3109 || TYPE_PTRMEMFUNC_P (expr_type)) 3110 error ("it must be a pointer-to-member of the form `&X::Y'"); 3111 3112 return NULL_TREE; 3113 } 3114 3115 referent = TREE_OPERAND (e, 0); 3116 STRIP_NOPS (referent); 3117 } 3118 3119 if (TREE_CODE (referent) == STRING_CST) 3120 { 3121 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", 3122 referent); 3123 return NULL_TREE; 3124 } 3125 3126 if (is_overloaded_fn (referent)) 3127 /* We'll check that it has external linkage later. */ 3128 ; 3129 else if (TREE_CODE (referent) != VAR_DECL) 3130 goto bad_argument; 3131 else if (!DECL_EXTERNAL_LINKAGE_P (referent)) 3132 { 3133 error ("address of non-extern `%E' cannot be used as template argument", referent); 3134 return error_mark_node; 3135 } 3136 } 3137 else if (INTEGRAL_TYPE_P (expr_type) 3138 || TYPE_PTRMEM_P (expr_type) 3139 || TYPE_PTRMEMFUNC_P (expr_type)) 3140 { 3141 if (! TREE_CONSTANT (expr)) 3142 { 3143 non_constant: 3144 error ("non-constant `%E' cannot be used as template argument", 3145 expr); 3146 return NULL_TREE; 3147 } 3148 } 3149 else 3150 { 3151 error ("object `%E' cannot be used as template argument", expr); 3152 return NULL_TREE; 3153 } 3154 3155 switch (TREE_CODE (type)) 3156 { 3157 case INTEGER_TYPE: 3158 case BOOLEAN_TYPE: 3159 case ENUMERAL_TYPE: 3160 /* For a non-type template-parameter of integral or enumeration 3161 type, integral promotions (_conv.prom_) and integral 3162 conversions (_conv.integral_) are applied. */ 3163 if (!INTEGRAL_TYPE_P (expr_type)) 3164 return error_mark_node; 3165 3166 /* It's safe to call digest_init in this case; we know we're 3167 just converting one integral constant expression to another. */ 3168 expr = digest_init (type, expr, (tree*) 0); 3169 3170 if (TREE_CODE (expr) != INTEGER_CST) 3171 /* Curiously, some TREE_CONSTANT integral expressions do not 3172 simplify to integer constants. For example, `3 % 0', 3173 remains a TRUNC_MOD_EXPR. */ 3174 goto non_constant; 3175 3176 return expr; 3177 3178 case POINTER_TYPE: 3179 { 3180 tree type_pointed_to = TREE_TYPE (type); 3181 3182 if (TYPE_PTRMEM_P (type)) 3183 { 3184 tree e; 3185 3186 /* For a non-type template-parameter of type pointer to data 3187 member, qualification conversions (_conv.qual_) are 3188 applied. */ 3189 e = perform_qualification_conversions (type, expr); 3190 if (TREE_CODE (e) == NOP_EXPR) 3191 /* The call to perform_qualification_conversions will 3192 insert a NOP_EXPR over EXPR to do express conversion, 3193 if necessary. But, that will confuse us if we use 3194 this (converted) template parameter to instantiate 3195 another template; then the thing will not look like a 3196 valid template argument. So, just make a new 3197 constant, of the appropriate type. */ 3198 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); 3199 return e; 3200 } 3201 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) 3202 { 3203 /* For a non-type template-parameter of type pointer to 3204 function, only the function-to-pointer conversion 3205 (_conv.func_) is applied. If the template-argument 3206 represents a set of overloaded functions (or a pointer to 3207 such), the matching function is selected from the set 3208 (_over.over_). */ 3209 tree fns; 3210 tree fn; 3211 3212 if (TREE_CODE (expr) == ADDR_EXPR) 3213 fns = TREE_OPERAND (expr, 0); 3214 else 3215 fns = expr; 3216 3217 fn = instantiate_type (type_pointed_to, fns, tf_none); 3218 3219 if (fn == error_mark_node) 3220 return error_mark_node; 3221 3222 if (!DECL_EXTERNAL_LINKAGE_P (fn)) 3223 { 3224 if (really_overloaded_fn (fns)) 3225 return error_mark_node; 3226 else 3227 goto bad_argument; 3228 } 3229 3230 expr = build_unary_op (ADDR_EXPR, fn, 0); 3231 3232 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 3233 0); 3234 return expr; 3235 } 3236 else 3237 { 3238 /* For a non-type template-parameter of type pointer to 3239 object, qualification conversions (_conv.qual_) and the 3240 array-to-pointer conversion (_conv.array_) are applied. 3241 [Note: In particular, neither the null pointer conversion 3242 (_conv.ptr_) nor the derived-to-base conversion 3243 (_conv.ptr_) are applied. Although 0 is a valid 3244 template-argument for a non-type template-parameter of 3245 integral type, it is not a valid template-argument for a 3246 non-type template-parameter of pointer type.] 3247 3248 The call to decay_conversion performs the 3249 array-to-pointer conversion, if appropriate. */ 3250 expr = decay_conversion (expr); 3251 3252 if (expr == error_mark_node) 3253 return error_mark_node; 3254 else 3255 return perform_qualification_conversions (type, expr); 3256 } 3257 } 3258 break; 3259 3260 case REFERENCE_TYPE: 3261 { 3262 tree type_referred_to = TREE_TYPE (type); 3263 3264 /* If this expression already has reference type, get the 3265 underling object. */ 3266 if (TREE_CODE (expr_type) == REFERENCE_TYPE) 3267 { 3268 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604); 3269 expr = TREE_OPERAND (expr, 0); 3270 expr_type = TREE_TYPE (expr); 3271 } 3272 3273 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) 3274 { 3275 /* For a non-type template-parameter of type reference to 3276 function, no conversions apply. If the 3277 template-argument represents a set of overloaded 3278 functions, the matching function is selected from the 3279 set (_over.over_). */ 3280 tree fn; 3281 3282 fn = instantiate_type (type_referred_to, expr, tf_none); 3283 3284 if (fn == error_mark_node) 3285 return error_mark_node; 3286 3287 if (!DECL_EXTERNAL_LINKAGE_P (fn)) 3288 { 3289 if (really_overloaded_fn (expr)) 3290 /* Don't issue an error here; we might get a different 3291 function if the overloading had worked out 3292 differently. */ 3293 return error_mark_node; 3294 else 3295 goto bad_argument; 3296 } 3297 3298 my_friendly_assert (same_type_p (type_referred_to, 3299 TREE_TYPE (fn)), 3300 0); 3301 3302 expr = fn; 3303 } 3304 else 3305 { 3306 /* For a non-type template-parameter of type reference to 3307 object, no conversions apply. The type referred to by the 3308 reference may be more cv-qualified than the (otherwise 3309 identical) type of the template-argument. The 3310 template-parameter is bound directly to the 3311 template-argument, which must be an lvalue. */ 3312 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type), 3313 TYPE_MAIN_VARIANT (type_referred_to)) 3314 || !at_least_as_qualified_p (type_referred_to, 3315 expr_type) 3316 || !real_lvalue_p (expr)) 3317 return error_mark_node; 3318 } 3319 3320 cxx_mark_addressable (expr); 3321 return build1 (ADDR_EXPR, type, expr); 3322 } 3323 break; 3324 3325 case RECORD_TYPE: 3326 { 3327 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112); 3328 3329 /* For a non-type template-parameter of type pointer to member 3330 function, no conversions apply. If the template-argument 3331 represents a set of overloaded member functions, the 3332 matching member function is selected from the set 3333 (_over.over_). */ 3334 3335 if (!TYPE_PTRMEMFUNC_P (expr_type) && 3336 expr_type != unknown_type_node) 3337 return error_mark_node; 3338 3339 if (TREE_CODE (expr) == PTRMEM_CST) 3340 { 3341 /* A ptr-to-member constant. */ 3342 if (!same_type_p (type, expr_type)) 3343 return error_mark_node; 3344 else 3345 return expr; 3346 } 3347 3348 if (TREE_CODE (expr) != ADDR_EXPR) 3349 return error_mark_node; 3350 3351 expr = instantiate_type (type, expr, tf_none); 3352 3353 if (expr == error_mark_node) 3354 return error_mark_node; 3355 3356 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 3357 0); 3358 return expr; 3359 } 3360 break; 3361 3362 default: 3363 /* All non-type parameters must have one of these types. */ 3364 abort (); 3365 break; 3366 } 3367 3368 return error_mark_node; 3369} 3370 3371/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for 3372 template template parameters. Both PARM_PARMS and ARG_PARMS are 3373 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL 3374 or PARM_DECL. 3375 3376 ARG_PARMS may contain more parameters than PARM_PARMS. If this is 3377 the case, then extra parameters must have default arguments. 3378 3379 Consider the example: 3380 template <class T, class Allocator = allocator> class vector; 3381 template<template <class U> class TT> class C; 3382 3383 C<vector> is a valid instantiation. PARM_PARMS for the above code 3384 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for 3385 T and Allocator) and OUTER_ARGS contains the argument that is used to 3386 substitute the TT parameter. */ 3387 3388static int 3389coerce_template_template_parms (parm_parms, arg_parms, complain, 3390 in_decl, outer_args) 3391 tree parm_parms, arg_parms; 3392 tsubst_flags_t complain; 3393 tree in_decl, outer_args; 3394{ 3395 int nparms, nargs, i; 3396 tree parm, arg; 3397 3398 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); 3399 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); 3400 3401 nparms = TREE_VEC_LENGTH (parm_parms); 3402 nargs = TREE_VEC_LENGTH (arg_parms); 3403 3404 /* The rule here is opposite of coerce_template_parms. */ 3405 if (nargs < nparms 3406 || (nargs > nparms 3407 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) 3408 return 0; 3409 3410 for (i = 0; i < nparms; ++i) 3411 { 3412 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); 3413 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); 3414 3415 if (arg == NULL_TREE || arg == error_mark_node 3416 || parm == NULL_TREE || parm == error_mark_node) 3417 return 0; 3418 3419 if (TREE_CODE (arg) != TREE_CODE (parm)) 3420 return 0; 3421 3422 switch (TREE_CODE (parm)) 3423 { 3424 case TYPE_DECL: 3425 break; 3426 3427 case TEMPLATE_DECL: 3428 /* We encounter instantiations of templates like 3429 template <template <template <class> class> class TT> 3430 class C; */ 3431 { 3432 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); 3433 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); 3434 3435 if (!coerce_template_template_parms (parmparm, argparm, 3436 complain, in_decl, 3437 outer_args)) 3438 return 0; 3439 } 3440 break; 3441 3442 case PARM_DECL: 3443 /* The tsubst call is used to handle cases such as 3444 template <class T, template <T> class TT> class D; 3445 i.e. the parameter list of TT depends on earlier parameters. */ 3446 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, 3447 complain, in_decl), 3448 TREE_TYPE (arg))) 3449 return 0; 3450 break; 3451 3452 default: 3453 abort (); 3454 } 3455 } 3456 return 1; 3457} 3458 3459/* Convert the indicated template ARG as necessary to match the 3460 indicated template PARM. Returns the converted ARG, or 3461 error_mark_node if the conversion was unsuccessful. Error and 3462 warning messages are issued under control of COMPLAIN. This 3463 conversion is for the Ith parameter in the parameter list. ARGS is 3464 the full set of template arguments deduced so far. */ 3465 3466static tree 3467convert_template_argument (parm, arg, args, complain, i, in_decl) 3468 tree parm; 3469 tree arg; 3470 tree args; 3471 tsubst_flags_t complain; 3472 int i; 3473 tree in_decl; 3474{ 3475 tree val; 3476 tree inner_args; 3477 int is_type, requires_type, is_tmpl_type, requires_tmpl_type; 3478 3479 inner_args = INNERMOST_TEMPLATE_ARGS (args); 3480 3481 if (TREE_CODE (arg) == TREE_LIST 3482 && TREE_TYPE (arg) != NULL_TREE 3483 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) 3484 { 3485 /* The template argument was the name of some 3486 member function. That's usually 3487 invalid, but static members are OK. In any 3488 case, grab the underlying fields/functions 3489 and issue an error later if required. */ 3490 arg = TREE_VALUE (arg); 3491 TREE_TYPE (arg) = unknown_type_node; 3492 } 3493 3494 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; 3495 requires_type = (TREE_CODE (parm) == TYPE_DECL 3496 || requires_tmpl_type); 3497 3498 if (TREE_CODE (arg) != RECORD_TYPE) 3499 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL 3500 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) 3501 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 3502 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); 3503 else if (CLASSTYPE_IS_TEMPLATE (arg) 3504 && is_base_of_enclosing_class (arg, current_class_type)) 3505 /* This is a template name used within the scope of the 3506 template. It could be the template, or it could be the 3507 instantiation. Choose whichever makes sense. */ 3508 is_tmpl_type = requires_tmpl_type; 3509 else 3510 /* It is a non-template class, or a specialization of a template 3511 class, or a non-template member of a template class. */ 3512 is_tmpl_type = 0; 3513 3514 if (is_tmpl_type 3515 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 3516 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) 3517 arg = TYPE_STUB_DECL (arg); 3518 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) 3519 arg = CLASSTYPE_TI_TEMPLATE (arg); 3520 3521 is_type = TYPE_P (arg) || is_tmpl_type; 3522 3523 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF 3524 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) 3525 { 3526 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg); 3527 3528 arg = make_typename_type (TREE_OPERAND (arg, 0), 3529 TREE_OPERAND (arg, 1), 3530 complain & tf_error); 3531 is_type = 1; 3532 } 3533 if (is_type != requires_type) 3534 { 3535 if (in_decl) 3536 { 3537 if (complain & tf_error) 3538 { 3539 error ("type/value mismatch at argument %d in template parameter list for `%D'", 3540 i + 1, in_decl); 3541 if (is_type) 3542 error (" expected a constant of type `%T', got `%T'", 3543 TREE_TYPE (parm), 3544 (is_tmpl_type ? DECL_NAME (arg) : arg)); 3545 else 3546 error (" expected a type, got `%E'", arg); 3547 } 3548 } 3549 return error_mark_node; 3550 } 3551 if (is_tmpl_type ^ requires_tmpl_type) 3552 { 3553 if (in_decl && (complain & tf_error)) 3554 { 3555 error ("type/value mismatch at argument %d in template parameter list for `%D'", 3556 i + 1, in_decl); 3557 if (is_tmpl_type) 3558 error (" expected a type, got `%T'", DECL_NAME (arg)); 3559 else 3560 error (" expected a class template, got `%T'", arg); 3561 } 3562 return error_mark_node; 3563 } 3564 3565 if (is_type) 3566 { 3567 if (requires_tmpl_type) 3568 { 3569 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) 3570 /* The number of argument required is not known yet. 3571 Just accept it for now. */ 3572 val = TREE_TYPE (arg); 3573 else 3574 { 3575 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); 3576 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); 3577 3578 if (coerce_template_template_parms (parmparm, argparm, 3579 complain, in_decl, 3580 inner_args)) 3581 { 3582 val = arg; 3583 3584 /* TEMPLATE_TEMPLATE_PARM node is preferred over 3585 TEMPLATE_DECL. */ 3586 if (val != error_mark_node 3587 && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) 3588 val = TREE_TYPE (val); 3589 } 3590 else 3591 { 3592 if (in_decl && (complain & tf_error)) 3593 { 3594 error ("type/value mismatch at argument %d in template parameter list for `%D'", 3595 i + 1, in_decl); 3596 error (" expected a template of type `%D', got `%D'", parm, arg); 3597 } 3598 3599 val = error_mark_node; 3600 } 3601 } 3602 } 3603 else 3604 { 3605 val = groktypename (arg); 3606 if (! processing_template_decl) 3607 { 3608 /* [basic.link]: A name with no linkage (notably, the 3609 name of a class or enumeration declared in a local 3610 scope) shall not be used to declare an entity with 3611 linkage. This implies that names with no linkage 3612 cannot be used as template arguments. */ 3613 tree t = no_linkage_check (val); 3614 if (t) 3615 { 3616 if (TYPE_ANONYMOUS_P (t)) 3617 pedwarn 3618 ("template-argument `%T' uses anonymous type", val); 3619 else 3620 error 3621 ("template-argument `%T' uses local type `%T'", 3622 val, t); 3623 return error_mark_node; 3624 } 3625 3626 /* In order to avoid all sorts of complications, we do 3627 not allow variably-modified types as template 3628 arguments. */ 3629 if (variably_modified_type_p (val)) 3630 { 3631 error ("template-argument `%T' is a variably modified type", 3632 val); 3633 return error_mark_node; 3634 } 3635 } 3636 } 3637 } 3638 else 3639 { 3640 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); 3641 3642 if (invalid_nontype_parm_type_p (t, complain)) 3643 return error_mark_node; 3644 3645 if (processing_template_decl) 3646 arg = maybe_fold_nontype_arg (arg); 3647 3648 if (!uses_template_parms (arg) && !uses_template_parms (t)) 3649 /* We used to call digest_init here. However, digest_init 3650 will report errors, which we don't want when complain 3651 is zero. More importantly, digest_init will try too 3652 hard to convert things: for example, `0' should not be 3653 converted to pointer type at this point according to 3654 the standard. Accepting this is not merely an 3655 extension, since deciding whether or not these 3656 conversions can occur is part of determining which 3657 function template to call, or whether a given explicit 3658 argument specification is valid. */ 3659 val = convert_nontype_argument (t, arg); 3660 else 3661 val = arg; 3662 3663 if (val == NULL_TREE) 3664 val = error_mark_node; 3665 else if (val == error_mark_node && (complain & tf_error)) 3666 error ("could not convert template argument `%E' to `%T'", 3667 arg, t); 3668 } 3669 3670 return val; 3671} 3672 3673/* Convert all template arguments to their appropriate types, and 3674 return a vector containing the innermost resulting template 3675 arguments. If any error occurs, return error_mark_node. Error and 3676 warning messages are issued under control of COMPLAIN. Some error 3677 messages are issued even if COMPLAIN is zero; for instance, if a 3678 template argument is composed from a local class. 3679 3680 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be 3681 provided in ARGLIST, or else trailing parameters must have default 3682 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument 3683 deduction for any unspecified trailing arguments. */ 3684 3685static tree 3686coerce_template_parms (parms, args, in_decl, 3687 complain, 3688 require_all_arguments) 3689 tree parms, args; 3690 tree in_decl; 3691 tsubst_flags_t complain; 3692 int require_all_arguments; 3693{ 3694 int nparms, nargs, i, lost = 0; 3695 tree inner_args; 3696 tree new_args; 3697 tree new_inner_args; 3698 3699 inner_args = INNERMOST_TEMPLATE_ARGS (args); 3700 nargs = NUM_TMPL_ARGS (inner_args); 3701 nparms = TREE_VEC_LENGTH (parms); 3702 3703 if (nargs > nparms 3704 || (nargs < nparms 3705 && require_all_arguments 3706 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) 3707 { 3708 if (complain & tf_error) 3709 { 3710 error ("wrong number of template arguments (%d, should be %d)", 3711 nargs, nparms); 3712 3713 if (in_decl) 3714 cp_error_at ("provided for `%D'", in_decl); 3715 } 3716 3717 return error_mark_node; 3718 } 3719 3720 new_inner_args = make_tree_vec (nparms); 3721 new_args = add_outermost_template_args (args, new_inner_args); 3722 for (i = 0; i < nparms; i++) 3723 { 3724 tree arg; 3725 tree parm; 3726 3727 /* Get the Ith template parameter. */ 3728 parm = TREE_VEC_ELT (parms, i); 3729 3730 /* Calculate the Ith argument. */ 3731 if (inner_args && TREE_CODE (inner_args) == TREE_LIST) 3732 { 3733 arg = TREE_VALUE (inner_args); 3734 inner_args = TREE_CHAIN (inner_args); 3735 } 3736 else if (i < nargs) 3737 arg = TREE_VEC_ELT (inner_args, i); 3738 /* If no template argument was supplied, look for a default 3739 value. */ 3740 else if (TREE_PURPOSE (parm) == NULL_TREE) 3741 { 3742 /* There was no default value. */ 3743 my_friendly_assert (!require_all_arguments, 0); 3744 break; 3745 } 3746 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) 3747 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); 3748 else 3749 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, 3750 in_decl); 3751 3752 /* Now, convert the Ith argument, as necessary. */ 3753 if (arg == NULL_TREE) 3754 /* We're out of arguments. */ 3755 { 3756 my_friendly_assert (!require_all_arguments, 0); 3757 break; 3758 } 3759 else if (arg == error_mark_node) 3760 { 3761 error ("template argument %d is invalid", i + 1); 3762 arg = error_mark_node; 3763 } 3764 else 3765 arg = convert_template_argument (TREE_VALUE (parm), 3766 arg, new_args, complain, i, 3767 in_decl); 3768 3769 if (arg == error_mark_node) 3770 lost++; 3771 TREE_VEC_ELT (new_inner_args, i) = arg; 3772 } 3773 3774 if (lost) 3775 return error_mark_node; 3776 3777 return new_inner_args; 3778} 3779 3780/* Returns 1 if template args OT and NT are equivalent. */ 3781 3782static int 3783template_args_equal (ot, nt) 3784 tree ot, nt; 3785{ 3786 if (nt == ot) 3787 return 1; 3788 3789 if (TREE_CODE (nt) == TREE_VEC) 3790 /* For member templates */ 3791 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); 3792 else if (TYPE_P (nt)) 3793 return TYPE_P (ot) && same_type_p (ot, nt); 3794 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) 3795 return 0; 3796 else 3797 return (cp_tree_equal (ot, nt) > 0); 3798} 3799 3800/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets 3801 of template arguments. Returns 0 otherwise. */ 3802 3803int 3804comp_template_args (oldargs, newargs) 3805 tree oldargs, newargs; 3806{ 3807 int i; 3808 3809 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) 3810 return 0; 3811 3812 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) 3813 { 3814 tree nt = TREE_VEC_ELT (newargs, i); 3815 tree ot = TREE_VEC_ELT (oldargs, i); 3816 3817 if (! template_args_equal (ot, nt)) 3818 return 0; 3819 } 3820 return 1; 3821} 3822 3823/* Given class template name and parameter list, produce a user-friendly name 3824 for the instantiation. */ 3825 3826static char * 3827mangle_class_name_for_template (name, parms, arglist) 3828 const char *name; 3829 tree parms, arglist; 3830{ 3831 static struct obstack scratch_obstack; 3832 static char *scratch_firstobj; 3833 int i, nparms; 3834 3835 if (!scratch_firstobj) 3836 gcc_obstack_init (&scratch_obstack); 3837 else 3838 obstack_free (&scratch_obstack, scratch_firstobj); 3839 scratch_firstobj = obstack_alloc (&scratch_obstack, 1); 3840 3841#define ccat(C) obstack_1grow (&scratch_obstack, (C)); 3842#define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) 3843 3844 cat (name); 3845 ccat ('<'); 3846 nparms = TREE_VEC_LENGTH (parms); 3847 arglist = INNERMOST_TEMPLATE_ARGS (arglist); 3848 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); 3849 for (i = 0; i < nparms; i++) 3850 { 3851 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 3852 tree arg = TREE_VEC_ELT (arglist, i); 3853 3854 if (i) 3855 ccat (','); 3856 3857 if (TREE_CODE (parm) == TYPE_DECL) 3858 { 3859 cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); 3860 continue; 3861 } 3862 else if (TREE_CODE (parm) == TEMPLATE_DECL) 3863 { 3864 if (TREE_CODE (arg) == TEMPLATE_DECL) 3865 { 3866 /* Already substituted with real template. Just output 3867 the template name here */ 3868 tree context = DECL_CONTEXT (arg); 3869 if (context) 3870 { 3871 /* The template may be defined in a namespace, or 3872 may be a member template. */ 3873 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL 3874 || CLASS_TYPE_P (context), 3875 980422); 3876 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER)); 3877 cat("::"); 3878 } 3879 cat (IDENTIFIER_POINTER (DECL_NAME (arg))); 3880 } 3881 else 3882 /* Output the parameter declaration */ 3883 cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); 3884 continue; 3885 } 3886 else 3887 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); 3888 3889 if (TREE_CODE (arg) == TREE_LIST) 3890 { 3891 /* New list cell was built because old chain link was in 3892 use. */ 3893 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); 3894 arg = TREE_VALUE (arg); 3895 } 3896 /* No need to check arglist against parmlist here; we did that 3897 in coerce_template_parms, called from lookup_template_class. */ 3898 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); 3899 } 3900 { 3901 char *bufp = obstack_next_free (&scratch_obstack); 3902 int offset = 0; 3903 while (bufp[offset - 1] == ' ') 3904 offset--; 3905 obstack_blank_fast (&scratch_obstack, offset); 3906 3907 /* B<C<char> >, not B<C<char>> */ 3908 if (bufp[offset - 1] == '>') 3909 ccat (' '); 3910 } 3911 ccat ('>'); 3912 ccat ('\0'); 3913 return (char *) obstack_base (&scratch_obstack); 3914} 3915 3916static tree 3917classtype_mangled_name (t) 3918 tree t; 3919{ 3920 if (CLASSTYPE_TEMPLATE_INFO (t) 3921 /* Specializations have already had their names set up in 3922 lookup_template_class. */ 3923 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) 3924 { 3925 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); 3926 3927 /* For non-primary templates, the template parameters are 3928 implicit from their surrounding context. */ 3929 if (PRIMARY_TEMPLATE_P (tmpl)) 3930 { 3931 tree name = DECL_NAME (tmpl); 3932 char *mangled_name = mangle_class_name_for_template 3933 (IDENTIFIER_POINTER (name), 3934 DECL_INNERMOST_TEMPLATE_PARMS (tmpl), 3935 CLASSTYPE_TI_ARGS (t)); 3936 tree id = get_identifier (mangled_name); 3937 IDENTIFIER_TEMPLATE (id) = name; 3938 return id; 3939 } 3940 } 3941 3942 return TYPE_IDENTIFIER (t); 3943} 3944 3945static void 3946add_pending_template (d) 3947 tree d; 3948{ 3949 tree ti = (TYPE_P (d) 3950 ? CLASSTYPE_TEMPLATE_INFO (d) 3951 : DECL_TEMPLATE_INFO (d)); 3952 tree pt; 3953 int level; 3954 3955 if (TI_PENDING_TEMPLATE_FLAG (ti)) 3956 return; 3957 3958 /* We are called both from instantiate_decl, where we've already had a 3959 tinst_level pushed, and instantiate_template, where we haven't. 3960 Compensate. */ 3961 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); 3962 3963 if (level) 3964 push_tinst_level (d); 3965 3966 pt = tree_cons (current_tinst_level, d, NULL_TREE); 3967 if (last_pending_template) 3968 TREE_CHAIN (last_pending_template) = pt; 3969 else 3970 pending_templates = pt; 3971 3972 last_pending_template = pt; 3973 3974 TI_PENDING_TEMPLATE_FLAG (ti) = 1; 3975 3976 if (level) 3977 pop_tinst_level (); 3978} 3979 3980 3981/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and 3982 ARGLIST. Valid choices for FNS are given in the cp-tree.def 3983 documentation for TEMPLATE_ID_EXPR. */ 3984 3985tree 3986lookup_template_function (fns, arglist) 3987 tree fns, arglist; 3988{ 3989 tree type; 3990 3991 if (fns == error_mark_node || arglist == error_mark_node) 3992 return error_mark_node; 3993 3994 if (fns == NULL_TREE 3995 || TREE_CODE (fns) == FUNCTION_DECL) 3996 { 3997 error ("non-template used as template"); 3998 return error_mark_node; 3999 } 4000 4001 my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL 4002 || TREE_CODE (fns) == OVERLOAD 4003 || BASELINK_P (fns) 4004 || TREE_CODE (fns) == IDENTIFIER_NODE 4005 || TREE_CODE (fns) == LOOKUP_EXPR, 4006 20020730); 4007 4008 if (BASELINK_P (fns)) 4009 { 4010 BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR, 4011 unknown_type_node, 4012 BASELINK_FUNCTIONS (fns), 4013 arglist); 4014 return fns; 4015 } 4016 4017 type = TREE_TYPE (fns); 4018 if (TREE_CODE (fns) == OVERLOAD || !type) 4019 type = unknown_type_node; 4020 4021 return build (TEMPLATE_ID_EXPR, type, fns, arglist); 4022} 4023 4024/* Within the scope of a template class S<T>, the name S gets bound 4025 (in build_self_reference) to a TYPE_DECL for the class, not a 4026 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, 4027 or one of its enclosing classes, and that type is a template, 4028 return the associated TEMPLATE_DECL. Otherwise, the original 4029 DECL is returned. */ 4030 4031static tree 4032maybe_get_template_decl_from_type_decl (decl) 4033 tree decl; 4034{ 4035 return (decl != NULL_TREE 4036 && TREE_CODE (decl) == TYPE_DECL 4037 && DECL_ARTIFICIAL (decl) 4038 && CLASS_TYPE_P (TREE_TYPE (decl)) 4039 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 4040 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; 4041} 4042 4043/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of 4044 parameters, find the desired type. 4045 4046 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. 4047 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will 4048 be a TREE_LIST if called directly from the parser, and a TREE_VEC 4049 otherwise.) 4050 4051 IN_DECL, if non-NULL, is the template declaration we are trying to 4052 instantiate. 4053 4054 If ENTERING_SCOPE is nonzero, we are about to enter the scope of 4055 the class we are looking up. 4056 4057 Issue error and warning messages under control of COMPLAIN. 4058 4059 If the template class is really a local class in a template 4060 function, then the FUNCTION_CONTEXT is the function in which it is 4061 being instantiated. */ 4062 4063tree 4064lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain) 4065 tree d1, arglist; 4066 tree in_decl; 4067 tree context; 4068 int entering_scope; 4069 tsubst_flags_t complain; 4070{ 4071 tree template = NULL_TREE, parmlist; 4072 tree t; 4073 4074 timevar_push (TV_NAME_LOOKUP); 4075 if (TREE_CODE (d1) == IDENTIFIER_NODE) 4076 { 4077 if (IDENTIFIER_VALUE (d1) 4078 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) 4079 template = IDENTIFIER_VALUE (d1); 4080 else 4081 { 4082 if (context) 4083 push_decl_namespace (context); 4084 template = lookup_name (d1, /*prefer_type=*/0); 4085 template = maybe_get_template_decl_from_type_decl (template); 4086 if (context) 4087 pop_decl_namespace (); 4088 } 4089 if (template) 4090 context = DECL_CONTEXT (template); 4091 if (template 4092 && TREE_CODE (template) == TYPE_DECL 4093 && IS_AGGR_TYPE (TREE_TYPE (template)) 4094 && TREE_CODE (TREE_TYPE (template)) != TEMPLATE_TYPE_PARM) 4095 { 4096 d1 = template; 4097 goto type_decl; 4098 } 4099 } 4100 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) 4101 { 4102 tree type; 4103 type_decl: 4104 type = TREE_TYPE (d1); 4105 4106 /* If we are declaring a constructor, say A<T>::A<T>, we will get 4107 an implicit typename for the second A. Deal with it. */ 4108 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) 4109 type = TREE_TYPE (type); 4110 4111 if (CLASSTYPE_TEMPLATE_INFO (type)) 4112 { 4113 template = CLASSTYPE_TI_TEMPLATE (type); 4114 d1 = DECL_NAME (template); 4115 } 4116 } 4117 else if (TREE_CODE (d1) == ENUMERAL_TYPE 4118 || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) 4119 { 4120 template = TYPE_TI_TEMPLATE (d1); 4121 d1 = DECL_NAME (template); 4122 } 4123 else if (TREE_CODE (d1) == TEMPLATE_DECL 4124 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) 4125 { 4126 template = d1; 4127 d1 = DECL_NAME (template); 4128 context = DECL_CONTEXT (template); 4129 } 4130 4131 /* With something like `template <class T> class X class X { ... };' 4132 we could end up with D1 having nothing but an IDENTIFIER_VALUE. 4133 We don't want to do that, but we have to deal with the situation, 4134 so let's give them some syntax errors to chew on instead of a 4135 crash. Alternatively D1 might not be a template type at all. */ 4136 if (! template) 4137 { 4138 if (complain & tf_error) 4139 error ("`%T' is not a template", d1); 4140 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 4141 } 4142 4143 if (TREE_CODE (template) != TEMPLATE_DECL 4144 /* If we're called from the parser, make sure it's a user visible 4145 template. */ 4146 || ((!arglist || TREE_CODE (arglist) == TREE_LIST) 4147 && !DECL_TEMPLATE_PARM_P (template) 4148 && !PRIMARY_TEMPLATE_P (template))) 4149 { 4150 if (complain & tf_error) 4151 { 4152 error ("non-template type `%T' used as a template", d1); 4153 if (in_decl) 4154 cp_error_at ("for template declaration `%D'", in_decl); 4155 } 4156 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 4157 } 4158 4159 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) 4160 { 4161 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store 4162 template arguments */ 4163 4164 tree parm; 4165 tree arglist2; 4166 4167 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); 4168 4169 /* Consider an example where a template template parameter declared as 4170 4171 template <class T, class U = std::allocator<T> > class TT 4172 4173 The template parameter level of T and U are one level larger than 4174 of TT. To proper process the default argument of U, say when an 4175 instantiation `TT<int>' is seen, we need to build the full 4176 arguments containing {int} as the innermost level. Outer levels, 4177 available when not appearing as default template argument, can be 4178 obtained from `current_template_args ()'. 4179 4180 Suppose that TT is later substituted with std::vector. The above 4181 instantiation is `TT<int, std::allocator<T> >' with TT at 4182 level 1, and T at level 2, while the template arguments at level 1 4183 becomes {std::vector} and the inner level 2 is {int}. */ 4184 4185 if (current_template_parms) 4186 arglist = add_to_template_args (current_template_args (), arglist); 4187 4188 arglist2 = coerce_template_parms (parmlist, arglist, template, 4189 complain, /*require_all_args=*/1); 4190 if (arglist2 == error_mark_node) 4191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 4192 4193 parm = bind_template_template_parm (TREE_TYPE (template), arglist2); 4194 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); 4195 } 4196 else 4197 { 4198 tree template_type = TREE_TYPE (template); 4199 tree gen_tmpl; 4200 tree type_decl; 4201 tree found = NULL_TREE; 4202 tree *tp; 4203 int arg_depth; 4204 int parm_depth; 4205 int is_partial_instantiation; 4206 4207 gen_tmpl = most_general_template (template); 4208 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); 4209 parm_depth = TMPL_PARMS_DEPTH (parmlist); 4210 arg_depth = TMPL_ARGS_DEPTH (arglist); 4211 4212 if (arg_depth == 1 && parm_depth > 1) 4213 { 4214 /* We've been given an incomplete set of template arguments. 4215 For example, given: 4216 4217 template <class T> struct S1 { 4218 template <class U> struct S2 {}; 4219 template <class U> struct S2<U*> {}; 4220 }; 4221 4222 we will be called with an ARGLIST of `U*', but the 4223 TEMPLATE will be `template <class T> template 4224 <class U> struct S1<T>::S2'. We must fill in the missing 4225 arguments. */ 4226 arglist 4227 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), 4228 arglist); 4229 arg_depth = TMPL_ARGS_DEPTH (arglist); 4230 } 4231 4232 /* Now we should have enough arguments. */ 4233 my_friendly_assert (parm_depth == arg_depth, 0); 4234 4235 /* From here on, we're only interested in the most general 4236 template. */ 4237 template = gen_tmpl; 4238 4239 /* Calculate the BOUND_ARGS. These will be the args that are 4240 actually tsubst'd into the definition to create the 4241 instantiation. */ 4242 if (parm_depth > 1) 4243 { 4244 /* We have multiple levels of arguments to coerce, at once. */ 4245 int i; 4246 int saved_depth = TMPL_ARGS_DEPTH (arglist); 4247 4248 tree bound_args = make_tree_vec (parm_depth); 4249 4250 for (i = saved_depth, 4251 t = DECL_TEMPLATE_PARMS (template); 4252 i > 0 && t != NULL_TREE; 4253 --i, t = TREE_CHAIN (t)) 4254 { 4255 tree a = coerce_template_parms (TREE_VALUE (t), 4256 arglist, template, 4257 complain, /*require_all_args=*/1); 4258 SET_TMPL_ARGS_LEVEL (bound_args, i, a); 4259 4260 /* We temporarily reduce the length of the ARGLIST so 4261 that coerce_template_parms will see only the arguments 4262 corresponding to the template parameters it is 4263 examining. */ 4264 TREE_VEC_LENGTH (arglist)--; 4265 } 4266 4267 /* Restore the ARGLIST to its full size. */ 4268 TREE_VEC_LENGTH (arglist) = saved_depth; 4269 4270 arglist = bound_args; 4271 } 4272 else 4273 arglist 4274 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), 4275 INNERMOST_TEMPLATE_ARGS (arglist), 4276 template, 4277 complain, /*require_all_args=*/1); 4278 4279 if (arglist == error_mark_node) 4280 /* We were unable to bind the arguments. */ 4281 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 4282 4283 /* In the scope of a template class, explicit references to the 4284 template class refer to the type of the template, not any 4285 instantiation of it. For example, in: 4286 4287 template <class T> class C { void f(C<T>); } 4288 4289 the `C<T>' is just the same as `C'. Outside of the 4290 class, however, such a reference is an instantiation. */ 4291 if (comp_template_args (TYPE_TI_ARGS (template_type), 4292 arglist)) 4293 { 4294 found = template_type; 4295 4296 if (!entering_scope && PRIMARY_TEMPLATE_P (template)) 4297 { 4298 tree ctx; 4299 4300 /* Note that we use DECL_CONTEXT, rather than 4301 CP_DECL_CONTEXT, so that the termination test is 4302 always just `ctx'. We're not interested in namespace 4303 scopes. */ 4304 for (ctx = current_class_type; 4305 ctx; 4306 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) 4307 if (same_type_p (ctx, template_type)) 4308 break; 4309 4310 if (!ctx) 4311 /* We're not in the scope of the class, so the 4312 TEMPLATE_TYPE is not the type we want after 4313 all. */ 4314 found = NULL_TREE; 4315 } 4316 } 4317 if (found) 4318 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); 4319 4320 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template); 4321 *tp; 4322 tp = &TREE_CHAIN (*tp)) 4323 if (comp_template_args (TREE_PURPOSE (*tp), arglist)) 4324 { 4325 found = *tp; 4326 4327 /* Use the move-to-front heuristic to speed up future 4328 searches. */ 4329 *tp = TREE_CHAIN (*tp); 4330 TREE_CHAIN (found) 4331 = DECL_TEMPLATE_INSTANTIATIONS (template); 4332 DECL_TEMPLATE_INSTANTIATIONS (template) = found; 4333 4334 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found)); 4335 } 4336 4337 /* This type is a "partial instantiation" if any of the template 4338 arguments still involve template parameters. Note that we set 4339 IS_PARTIAL_INSTANTIATION for partial specializations as 4340 well. */ 4341 is_partial_instantiation = uses_template_parms (arglist); 4342 4343 if (!is_partial_instantiation 4344 && !PRIMARY_TEMPLATE_P (template) 4345 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) 4346 { 4347 found = xref_tag_from_type (TREE_TYPE (template), 4348 DECL_NAME (template), 4349 /*globalize=*/1); 4350 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); 4351 } 4352 4353 context = tsubst (DECL_CONTEXT (template), arglist, 4354 complain, in_decl); 4355 if (!context) 4356 context = global_namespace; 4357 4358 /* Create the type. */ 4359 if (TREE_CODE (template_type) == ENUMERAL_TYPE) 4360 { 4361 if (!is_partial_instantiation) 4362 { 4363 set_current_access_from_decl (TYPE_NAME (template_type)); 4364 t = start_enum (TYPE_IDENTIFIER (template_type)); 4365 } 4366 else 4367 /* We don't want to call start_enum for this type, since 4368 the values for the enumeration constants may involve 4369 template parameters. And, no one should be interested 4370 in the enumeration constants for such a type. */ 4371 t = make_node (ENUMERAL_TYPE); 4372 } 4373 else 4374 { 4375 t = make_aggr_type (TREE_CODE (template_type)); 4376 CLASSTYPE_DECLARED_CLASS (t) 4377 = CLASSTYPE_DECLARED_CLASS (template_type); 4378 CLASSTYPE_GOT_SEMICOLON (t) = 1; 4379 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); 4380 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); 4381 4382 /* A local class. Make sure the decl gets registered properly. */ 4383 if (context == current_function_decl) 4384 pushtag (DECL_NAME (template), t, 0); 4385 } 4386 4387 /* If we called start_enum or pushtag above, this information 4388 will already be set up. */ 4389 if (!TYPE_NAME (t)) 4390 { 4391 TYPE_CONTEXT (t) = FROB_CONTEXT (context); 4392 4393 type_decl = create_implicit_typedef (DECL_NAME (template), t); 4394 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); 4395 TYPE_STUB_DECL (t) = type_decl; 4396 DECL_SOURCE_LOCATION (type_decl) 4397 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); 4398 } 4399 else 4400 type_decl = TYPE_NAME (t); 4401 4402 TREE_PRIVATE (type_decl) 4403 = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); 4404 TREE_PROTECTED (type_decl) 4405 = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); 4406 4407 /* Set up the template information. We have to figure out which 4408 template is the immediate parent if this is a full 4409 instantiation. */ 4410 if (parm_depth == 1 || is_partial_instantiation 4411 || !PRIMARY_TEMPLATE_P (template)) 4412 /* This case is easy; there are no member templates involved. */ 4413 found = template; 4414 else 4415 { 4416 /* This is a full instantiation of a member template. Look 4417 for a partial instantiation of which this is an instance. */ 4418 4419 for (found = DECL_TEMPLATE_INSTANTIATIONS (template); 4420 found; found = TREE_CHAIN (found)) 4421 { 4422 int success; 4423 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); 4424 4425 /* We only want partial instantiations, here, not 4426 specializations or full instantiations. */ 4427 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) 4428 || !uses_template_parms (TREE_VALUE (found))) 4429 continue; 4430 4431 /* Temporarily reduce by one the number of levels in the 4432 ARGLIST and in FOUND so as to avoid comparing the 4433 last set of arguments. */ 4434 TREE_VEC_LENGTH (arglist)--; 4435 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; 4436 4437 /* See if the arguments match. If they do, then TMPL is 4438 the partial instantiation we want. */ 4439 success = comp_template_args (TREE_PURPOSE (found), arglist); 4440 4441 /* Restore the argument vectors to their full size. */ 4442 TREE_VEC_LENGTH (arglist)++; 4443 TREE_VEC_LENGTH (TREE_PURPOSE (found))++; 4444 4445 if (success) 4446 { 4447 found = tmpl; 4448 break; 4449 } 4450 } 4451 4452 if (!found) 4453 { 4454 /* There was no partial instantiation. This happens 4455 where C<T> is a member template of A<T> and it's used 4456 in something like 4457 4458 template <typename T> struct B { A<T>::C<int> m; }; 4459 B<float>; 4460 4461 Create the partial instantiation. 4462 */ 4463 TREE_VEC_LENGTH (arglist)--; 4464 found = tsubst (template, arglist, complain, NULL_TREE); 4465 TREE_VEC_LENGTH (arglist)++; 4466 } 4467 } 4468 4469 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); 4470 DECL_TEMPLATE_INSTANTIATIONS (template) 4471 = tree_cons (arglist, t, 4472 DECL_TEMPLATE_INSTANTIATIONS (template)); 4473 4474 if (TREE_CODE (t) == ENUMERAL_TYPE 4475 && !is_partial_instantiation) 4476 /* Now that the type has been registered on the instantiations 4477 list, we set up the enumerators. Because the enumeration 4478 constants may involve the enumeration type itself, we make 4479 sure to register the type first, and then create the 4480 constants. That way, doing tsubst_expr for the enumeration 4481 constants won't result in recursive calls here; we'll find 4482 the instantiation and exit above. */ 4483 tsubst_enum (template_type, t, arglist); 4484 4485 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO 4486 is set up. */ 4487 if (TREE_CODE (t) != ENUMERAL_TYPE) 4488 DECL_NAME (type_decl) = classtype_mangled_name (t); 4489 if (!is_partial_instantiation) 4490 { 4491 /* For backwards compatibility; code that uses 4492 -fexternal-templates expects looking up a template to 4493 instantiate it. I think DDD still relies on this. 4494 (jason 8/20/1998) */ 4495 if (TREE_CODE (t) != ENUMERAL_TYPE 4496 && flag_external_templates 4497 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) 4498 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) 4499 add_pending_template (t); 4500 } 4501 else 4502 /* If the type makes use of template parameters, the 4503 code that generates debugging information will crash. */ 4504 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; 4505 4506 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 4507 } 4508 timevar_pop (TV_NAME_LOOKUP); 4509} 4510 4511struct pair_fn_data 4512{ 4513 tree_fn_t fn; 4514 void *data; 4515 htab_t visited; 4516}; 4517 4518/* Called from for_each_template_parm via walk_tree. */ 4519 4520static tree 4521for_each_template_parm_r (tp, walk_subtrees, d) 4522 tree *tp; 4523 int *walk_subtrees; 4524 void *d; 4525{ 4526 tree t = *tp; 4527 struct pair_fn_data *pfd = (struct pair_fn_data *) d; 4528 tree_fn_t fn = pfd->fn; 4529 void *data = pfd->data; 4530 void **slot; 4531 4532 /* If we have already visited this tree, there's no need to walk 4533 subtrees. Otherwise, add it to the visited table. */ 4534 slot = htab_find_slot (pfd->visited, *tp, INSERT); 4535 if (*slot) 4536 { 4537 *walk_subtrees = 0; 4538 return NULL_TREE; 4539 } 4540 *slot = *tp; 4541 4542 if (TYPE_P (t) 4543 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) 4544 return error_mark_node; 4545 4546 switch (TREE_CODE (t)) 4547 { 4548 case RECORD_TYPE: 4549 if (TYPE_PTRMEMFUNC_P (t)) 4550 break; 4551 /* Fall through. */ 4552 4553 case UNION_TYPE: 4554 case ENUMERAL_TYPE: 4555 if (!TYPE_TEMPLATE_INFO (t)) 4556 *walk_subtrees = 0; 4557 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), 4558 fn, data, pfd->visited)) 4559 return error_mark_node; 4560 break; 4561 4562 case METHOD_TYPE: 4563 /* Since we're not going to walk subtrees, we have to do this 4564 explicitly here. */ 4565 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, 4566 pfd->visited)) 4567 return error_mark_node; 4568 /* Fall through. */ 4569 4570 case FUNCTION_TYPE: 4571 /* Check the return type. */ 4572 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) 4573 return error_mark_node; 4574 4575 /* Check the parameter types. Since default arguments are not 4576 instantiated until they are needed, the TYPE_ARG_TYPES may 4577 contain expressions that involve template parameters. But, 4578 no-one should be looking at them yet. And, once they're 4579 instantiated, they don't contain template parameters, so 4580 there's no point in looking at them then, either. */ 4581 { 4582 tree parm; 4583 4584 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) 4585 if (for_each_template_parm (TREE_VALUE (parm), fn, data, 4586 pfd->visited)) 4587 return error_mark_node; 4588 4589 /* Since we've already handled the TYPE_ARG_TYPES, we don't 4590 want walk_tree walking into them itself. */ 4591 *walk_subtrees = 0; 4592 } 4593 break; 4594 4595 case FUNCTION_DECL: 4596 case VAR_DECL: 4597 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) 4598 && for_each_template_parm (DECL_TI_ARGS (t), fn, data, 4599 pfd->visited)) 4600 return error_mark_node; 4601 /* Fall through. */ 4602 4603 case CONST_DECL: 4604 case PARM_DECL: 4605 if (DECL_CONTEXT (t) 4606 && for_each_template_parm (DECL_CONTEXT (t), fn, data, 4607 pfd->visited)) 4608 return error_mark_node; 4609 break; 4610 4611 case BOUND_TEMPLATE_TEMPLATE_PARM: 4612 /* Record template parameters such as `T' inside `TT<T>'. */ 4613 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) 4614 return error_mark_node; 4615 /* Fall through. */ 4616 4617 case TEMPLATE_TEMPLATE_PARM: 4618 case TEMPLATE_TYPE_PARM: 4619 case TEMPLATE_PARM_INDEX: 4620 if (fn && (*fn)(t, data)) 4621 return error_mark_node; 4622 else if (!fn) 4623 return error_mark_node; 4624 break; 4625 4626 case TEMPLATE_DECL: 4627 /* A template template parameter is encountered */ 4628 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) 4629 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) 4630 return error_mark_node; 4631 4632 /* Already substituted template template parameter */ 4633 *walk_subtrees = 0; 4634 break; 4635 4636 case TYPENAME_TYPE: 4637 if (!fn 4638 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, 4639 data, pfd->visited)) 4640 return error_mark_node; 4641 break; 4642 4643 case CONSTRUCTOR: 4644 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) 4645 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE 4646 (TREE_TYPE (t)), fn, data, 4647 pfd->visited)) 4648 return error_mark_node; 4649 break; 4650 4651 case INDIRECT_REF: 4652 case COMPONENT_REF: 4653 /* If there's no type, then this thing must be some expression 4654 involving template parameters. */ 4655 if (!fn && !TREE_TYPE (t)) 4656 return error_mark_node; 4657 break; 4658 4659 case MODOP_EXPR: 4660 case CAST_EXPR: 4661 case REINTERPRET_CAST_EXPR: 4662 case CONST_CAST_EXPR: 4663 case STATIC_CAST_EXPR: 4664 case DYNAMIC_CAST_EXPR: 4665 case ARROW_EXPR: 4666 case DOTSTAR_EXPR: 4667 case TYPEID_EXPR: 4668 case LOOKUP_EXPR: 4669 case PSEUDO_DTOR_EXPR: 4670 if (!fn) 4671 return error_mark_node; 4672 break; 4673 4674 case BASELINK: 4675 /* If we do not handle this case specially, we end up walking 4676 the BINFO hierarchy, which is circular, and therefore 4677 confuses walk_tree. */ 4678 *walk_subtrees = 0; 4679 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, 4680 pfd->visited)) 4681 return error_mark_node; 4682 break; 4683 4684 default: 4685 break; 4686 } 4687 4688 /* We didn't find any template parameters we liked. */ 4689 return NULL_TREE; 4690} 4691 4692/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, 4693 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, 4694 call FN with the parameter and the DATA. 4695 If FN returns nonzero, the iteration is terminated, and 4696 for_each_template_parm returns 1. Otherwise, the iteration 4697 continues. If FN never returns a nonzero value, the value 4698 returned by for_each_template_parm is 0. If FN is NULL, it is 4699 considered to be the function which always returns 1. */ 4700 4701static int 4702for_each_template_parm (t, fn, data, visited) 4703 tree t; 4704 tree_fn_t fn; 4705 void* data; 4706 htab_t visited; 4707{ 4708 struct pair_fn_data pfd; 4709 int result; 4710 4711 /* Set up. */ 4712 pfd.fn = fn; 4713 pfd.data = data; 4714 4715 /* Walk the tree. (Conceptually, we would like to walk without 4716 duplicates, but for_each_template_parm_r recursively calls 4717 for_each_template_parm, so we would need to reorganize a fair 4718 bit to use walk_tree_without_duplicates, so we keep our own 4719 visited list.) */ 4720 if (visited) 4721 pfd.visited = visited; 4722 else 4723 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, 4724 NULL); 4725 result = walk_tree (&t, 4726 for_each_template_parm_r, 4727 &pfd, 4728 NULL) != NULL_TREE; 4729 4730 /* Clean up. */ 4731 if (!visited) 4732 htab_delete (pfd.visited); 4733 4734 return result; 4735} 4736 4737int 4738uses_template_parms (t) 4739 tree t; 4740{ 4741 return for_each_template_parm (t, 0, 0, NULL); 4742} 4743 4744static int tinst_depth; 4745extern int max_tinst_depth; 4746#ifdef GATHER_STATISTICS 4747int depth_reached; 4748#endif 4749static int tinst_level_tick; 4750static int last_template_error_tick; 4751 4752/* We're starting to instantiate D; record the template instantiation context 4753 for diagnostics and to restore it later. */ 4754 4755int 4756push_tinst_level (d) 4757 tree d; 4758{ 4759 tree new; 4760 4761 if (tinst_depth >= max_tinst_depth) 4762 { 4763 /* If the instantiation in question still has unbound template parms, 4764 we don't really care if we can't instantiate it, so just return. 4765 This happens with base instantiation for implicit `typename'. */ 4766 if (uses_template_parms (d)) 4767 return 0; 4768 4769 last_template_error_tick = tinst_level_tick; 4770 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'", 4771 max_tinst_depth, d); 4772 4773 print_instantiation_context (); 4774 4775 return 0; 4776 } 4777 4778 new = build_expr_wfl (d, input_filename, lineno, 0); 4779 TREE_CHAIN (new) = current_tinst_level; 4780 current_tinst_level = new; 4781 4782 ++tinst_depth; 4783#ifdef GATHER_STATISTICS 4784 if (tinst_depth > depth_reached) 4785 depth_reached = tinst_depth; 4786#endif 4787 4788 ++tinst_level_tick; 4789 return 1; 4790} 4791 4792/* We're done instantiating this template; return to the instantiation 4793 context. */ 4794 4795void 4796pop_tinst_level () 4797{ 4798 tree old = current_tinst_level; 4799 4800 /* Restore the filename and line number stashed away when we started 4801 this instantiation. */ 4802 lineno = TINST_LINE (old); 4803 input_filename = TINST_FILE (old); 4804 extract_interface_info (); 4805 4806 current_tinst_level = TREE_CHAIN (old); 4807 --tinst_depth; 4808 ++tinst_level_tick; 4809} 4810 4811/* We're instantiating a deferred template; restore the template 4812 instantiation context in which the instantiation was requested, which 4813 is one step out from LEVEL. */ 4814 4815static void 4816reopen_tinst_level (level) 4817 tree level; 4818{ 4819 tree t; 4820 4821 tinst_depth = 0; 4822 for (t = level; t; t = TREE_CHAIN (t)) 4823 ++tinst_depth; 4824 4825 current_tinst_level = level; 4826 pop_tinst_level (); 4827} 4828 4829/* Return the outermost template instantiation context, for use with 4830 -falt-external-templates. */ 4831 4832tree 4833tinst_for_decl () 4834{ 4835 tree p = current_tinst_level; 4836 4837 if (p) 4838 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p)) 4839 ; 4840 return p; 4841} 4842 4843/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the 4844 vector of template arguments, as for tsubst. 4845 4846 Returns an appropriate tsubst'd friend declaration. */ 4847 4848static tree 4849tsubst_friend_function (decl, args) 4850 tree decl; 4851 tree args; 4852{ 4853 tree new_friend; 4854 int line = lineno; 4855 const char *file = input_filename; 4856 4857 lineno = DECL_SOURCE_LINE (decl); 4858 input_filename = DECL_SOURCE_FILE (decl); 4859 4860 if (TREE_CODE (decl) == FUNCTION_DECL 4861 && DECL_TEMPLATE_INSTANTIATION (decl) 4862 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 4863 /* This was a friend declared with an explicit template 4864 argument list, e.g.: 4865 4866 friend void f<>(T); 4867 4868 to indicate that f was a template instantiation, not a new 4869 function declaration. Now, we have to figure out what 4870 instantiation of what template. */ 4871 { 4872 tree template_id, arglist, fns; 4873 tree new_args; 4874 tree tmpl; 4875 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); 4876 4877 /* Friend functions are looked up in the containing namespace scope. 4878 We must enter that scope, to avoid finding member functions of the 4879 current cless with same name. */ 4880 push_nested_namespace (ns); 4881 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, 4882 tf_error | tf_warning, NULL_TREE); 4883 pop_nested_namespace (ns); 4884 arglist = tsubst (DECL_TI_ARGS (decl), args, 4885 tf_error | tf_warning, NULL_TREE); 4886 template_id = lookup_template_function (fns, arglist); 4887 4888 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); 4889 tmpl = determine_specialization (template_id, new_friend, 4890 &new_args, 4891 /*need_member_template=*/0); 4892 new_friend = instantiate_template (tmpl, new_args); 4893 goto done; 4894 } 4895 4896 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); 4897 4898 /* The NEW_FRIEND will look like an instantiation, to the 4899 compiler, but is not an instantiation from the point of view of 4900 the language. For example, we might have had: 4901 4902 template <class T> struct S { 4903 template <class U> friend void f(T, U); 4904 }; 4905 4906 Then, in S<int>, template <class U> void f(int, U) is not an 4907 instantiation of anything. */ 4908 DECL_USE_TEMPLATE (new_friend) = 0; 4909 if (TREE_CODE (decl) == TEMPLATE_DECL) 4910 { 4911 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; 4912 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) 4913 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); 4914 } 4915 4916 /* The mangled name for the NEW_FRIEND is incorrect. The function 4917 is not a template instantiation and should not be mangled like 4918 one. Therefore, we forget the mangling here; we'll recompute it 4919 later if we need it. */ 4920 if (TREE_CODE (new_friend) != TEMPLATE_DECL) 4921 { 4922 SET_DECL_RTL (new_friend, NULL_RTX); 4923 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); 4924 } 4925 4926 if (DECL_NAMESPACE_SCOPE_P (new_friend)) 4927 { 4928 tree old_decl; 4929 tree new_friend_template_info; 4930 tree new_friend_result_template_info; 4931 tree ns; 4932 int new_friend_is_defn; 4933 4934 /* We must save some information from NEW_FRIEND before calling 4935 duplicate decls since that function will free NEW_FRIEND if 4936 possible. */ 4937 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); 4938 if (TREE_CODE (new_friend) == TEMPLATE_DECL) 4939 { 4940 /* This declaration is a `primary' template. */ 4941 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; 4942 4943 new_friend_is_defn 4944 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE; 4945 new_friend_result_template_info 4946 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); 4947 } 4948 else 4949 { 4950 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; 4951 new_friend_result_template_info = NULL_TREE; 4952 } 4953 4954 /* Inside pushdecl_namespace_level, we will push into the 4955 current namespace. However, the friend function should go 4956 into the namespace of the template. */ 4957 ns = decl_namespace_context (new_friend); 4958 push_nested_namespace (ns); 4959 old_decl = pushdecl_namespace_level (new_friend); 4960 pop_nested_namespace (ns); 4961 4962 if (old_decl != new_friend) 4963 { 4964 /* This new friend declaration matched an existing 4965 declaration. For example, given: 4966 4967 template <class T> void f(T); 4968 template <class U> class C { 4969 template <class T> friend void f(T) {} 4970 }; 4971 4972 the friend declaration actually provides the definition 4973 of `f', once C has been instantiated for some type. So, 4974 old_decl will be the out-of-class template declaration, 4975 while new_friend is the in-class definition. 4976 4977 But, if `f' was called before this point, the 4978 instantiation of `f' will have DECL_TI_ARGS corresponding 4979 to `T' but not to `U', references to which might appear 4980 in the definition of `f'. Previously, the most general 4981 template for an instantiation of `f' was the out-of-class 4982 version; now it is the in-class version. Therefore, we 4983 run through all specialization of `f', adding to their 4984 DECL_TI_ARGS appropriately. In particular, they need a 4985 new set of outer arguments, corresponding to the 4986 arguments for this class instantiation. 4987 4988 The same situation can arise with something like this: 4989 4990 friend void f(int); 4991 template <class T> class C { 4992 friend void f(T) {} 4993 }; 4994 4995 when `C<int>' is instantiated. Now, `f(int)' is defined 4996 in the class. */ 4997 4998 if (!new_friend_is_defn) 4999 /* On the other hand, if the in-class declaration does 5000 *not* provide a definition, then we don't want to alter 5001 existing definitions. We can just leave everything 5002 alone. */ 5003 ; 5004 else 5005 { 5006 /* Overwrite whatever template info was there before, if 5007 any, with the new template information pertaining to 5008 the declaration. */ 5009 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; 5010 5011 if (TREE_CODE (old_decl) != TEMPLATE_DECL) 5012 reregister_specialization (new_friend, 5013 most_general_template (old_decl), 5014 old_decl); 5015 else 5016 { 5017 tree t; 5018 tree new_friend_args; 5019 5020 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) 5021 = new_friend_result_template_info; 5022 5023 new_friend_args = TI_ARGS (new_friend_template_info); 5024 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); 5025 t != NULL_TREE; 5026 t = TREE_CHAIN (t)) 5027 { 5028 tree spec = TREE_VALUE (t); 5029 5030 DECL_TI_ARGS (spec) 5031 = add_outermost_template_args (new_friend_args, 5032 DECL_TI_ARGS (spec)); 5033 } 5034 5035 /* Now, since specializations are always supposed to 5036 hang off of the most general template, we must move 5037 them. */ 5038 t = most_general_template (old_decl); 5039 if (t != old_decl) 5040 { 5041 DECL_TEMPLATE_SPECIALIZATIONS (t) 5042 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), 5043 DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); 5044 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; 5045 } 5046 } 5047 } 5048 5049 /* The information from NEW_FRIEND has been merged into OLD_DECL 5050 by duplicate_decls. */ 5051 new_friend = old_decl; 5052 } 5053 } 5054 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend))) 5055 { 5056 /* Check to see that the declaration is really present, and, 5057 possibly obtain an improved declaration. */ 5058 tree fn = check_classfn (DECL_CONTEXT (new_friend), 5059 new_friend); 5060 5061 if (fn) 5062 new_friend = fn; 5063 } 5064 5065 done: 5066 lineno = line; 5067 input_filename = file; 5068 return new_friend; 5069} 5070 5071/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of 5072 template arguments, as for tsubst. 5073 5074 Returns an appropriate tsubst'd friend type or error_mark_node on 5075 failure. */ 5076 5077static tree 5078tsubst_friend_class (friend_tmpl, args) 5079 tree friend_tmpl; 5080 tree args; 5081{ 5082 tree friend_type; 5083 tree tmpl; 5084 tree context; 5085 5086 context = DECL_CONTEXT (friend_tmpl); 5087 5088 if (context) 5089 { 5090 if (TREE_CODE (context) == NAMESPACE_DECL) 5091 push_nested_namespace (context); 5092 else 5093 push_nested_class (tsubst (context, args, tf_none, NULL_TREE), 2); 5094 } 5095 5096 /* First, we look for a class template. */ 5097 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); 5098 5099 /* But, if we don't find one, it might be because we're in a 5100 situation like this: 5101 5102 template <class T> 5103 struct S { 5104 template <class U> 5105 friend struct S; 5106 }; 5107 5108 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL 5109 for `S<int>', not the TEMPLATE_DECL. */ 5110 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 5111 { 5112 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); 5113 tmpl = maybe_get_template_decl_from_type_decl (tmpl); 5114 } 5115 5116 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) 5117 { 5118 /* The friend template has already been declared. Just 5119 check to see that the declarations match, and install any new 5120 default parameters. We must tsubst the default parameters, 5121 of course. We only need the innermost template parameters 5122 because that is all that redeclare_class_template will look 5123 at. */ 5124 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) 5125 > TMPL_ARGS_DEPTH (args)) 5126 { 5127 tree parms; 5128 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), 5129 args, tf_error | tf_warning); 5130 redeclare_class_template (TREE_TYPE (tmpl), parms); 5131 } 5132 5133 friend_type = TREE_TYPE (tmpl); 5134 } 5135 else 5136 { 5137 /* The friend template has not already been declared. In this 5138 case, the instantiation of the template class will cause the 5139 injection of this template into the global scope. */ 5140 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE); 5141 5142 /* The new TMPL is not an instantiation of anything, so we 5143 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for 5144 the new type because that is supposed to be the corresponding 5145 template decl, i.e., TMPL. */ 5146 DECL_USE_TEMPLATE (tmpl) = 0; 5147 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; 5148 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; 5149 5150 /* Inject this template into the global scope. */ 5151 friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); 5152 } 5153 5154 if (context) 5155 { 5156 if (TREE_CODE (context) == NAMESPACE_DECL) 5157 pop_nested_namespace (context); 5158 else 5159 pop_nested_class (); 5160 } 5161 5162 return friend_type; 5163} 5164 5165/* Returns zero if TYPE cannot be completed later due to circularity. 5166 Otherwise returns one. */ 5167 5168static int 5169can_complete_type_without_circularity (type) 5170 tree type; 5171{ 5172 if (type == NULL_TREE || type == error_mark_node) 5173 return 0; 5174 else if (COMPLETE_TYPE_P (type)) 5175 return 1; 5176 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) 5177 return can_complete_type_without_circularity (TREE_TYPE (type)); 5178 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) 5179 return 0; 5180 else 5181 return 1; 5182} 5183 5184tree 5185instantiate_class_template (type) 5186 tree type; 5187{ 5188 tree template, args, pattern, t, member; 5189 tree typedecl; 5190 5191 if (type == error_mark_node) 5192 return error_mark_node; 5193 5194 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type)) 5195 return type; 5196 5197 /* Figure out which template is being instantiated. */ 5198 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); 5199 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); 5200 5201 /* Figure out which arguments are being used to do the 5202 instantiation. */ 5203 args = CLASSTYPE_TI_ARGS (type); 5204 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); 5205 5206 if (pedantic && PARTIAL_INSTANTIATION_P (type)) 5207 /* If this is a partial instantiation, then we can't instantiate 5208 the type; there's no telling whether or not one of the 5209 template parameters might eventually be instantiated to some 5210 value that results in a specialization being used. For 5211 example, consider: 5212 5213 template <class T> 5214 struct S {}; 5215 5216 template <class U> 5217 void f(S<U>); 5218 5219 template <> 5220 struct S<int> {}; 5221 5222 Now, the `S<U>' in `f<int>' is the specialization, not an 5223 instantiation of the original template. */ 5224 return type; 5225 5226 /* Determine what specialization of the original template to 5227 instantiate. */ 5228 if (PARTIAL_INSTANTIATION_P (type)) 5229 /* There's no telling which specialization is appropriate at this 5230 point. Since all peeking at the innards of this partial 5231 instantiation are extensions (like the "implicit typename" 5232 extension, which allows users to omit the keyword `typename' on 5233 names that are declared as types in template base classes), we 5234 are free to do what we please. 5235 5236 Trying to figure out which partial instantiation to use can 5237 cause a crash. (Some of the template arguments don't even have 5238 types.) So, we just use the most general version. */ 5239 t = NULL_TREE; 5240 else 5241 { 5242 t = most_specialized_class (template, args); 5243 5244 if (t == error_mark_node) 5245 { 5246 const char *str = "candidates are:"; 5247 error ("ambiguous class template instantiation for `%#T'", type); 5248 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; 5249 t = TREE_CHAIN (t)) 5250 { 5251 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), 5252 args)) 5253 { 5254 cp_error_at ("%s %+#T", str, TREE_TYPE (t)); 5255 str = " "; 5256 } 5257 } 5258 TYPE_BEING_DEFINED (type) = 1; 5259 return error_mark_node; 5260 } 5261 } 5262 5263 if (t) 5264 pattern = TREE_TYPE (t); 5265 else 5266 pattern = TREE_TYPE (template); 5267 5268 /* If the template we're instantiating is incomplete, then clearly 5269 there's nothing we can do. */ 5270 if (!COMPLETE_TYPE_P (pattern)) 5271 return type; 5272 5273 /* If this is a partial instantiation, don't tsubst anything. We will 5274 only use this type for implicit typename, so the actual contents don't 5275 matter. All that matters is whether a particular name is a type. */ 5276 if (PARTIAL_INSTANTIATION_P (type)) 5277 { 5278 /* The fields set here must be kept in sync with those cleared 5279 in begin_class_definition. */ 5280 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); 5281 TYPE_FIELDS (type) = TYPE_FIELDS (pattern); 5282 TYPE_METHODS (type) = TYPE_METHODS (pattern); 5283 CLASSTYPE_DECL_LIST (type) = CLASSTYPE_DECL_LIST (pattern); 5284 CLASSTYPE_NESTED_UDTS (type) = CLASSTYPE_NESTED_UDTS (pattern); 5285 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern); 5286 5287 /* Pretend that the type is complete, so that we will look 5288 inside it during name lookup and such. */ 5289 TYPE_SIZE (type) = bitsize_zero_node; 5290 return type; 5291 } 5292 5293 /* If we've recursively instantiated too many templates, stop. */ 5294 if (! push_tinst_level (type)) 5295 return type; 5296 5297 /* Now we're really doing the instantiation. Mark the type as in 5298 the process of being defined. */ 5299 TYPE_BEING_DEFINED (type) = 1; 5300 5301 maybe_push_to_top_level (uses_template_parms (type)); 5302 5303 if (t) 5304 { 5305 /* This TYPE is actually an instantiation of a partial 5306 specialization. We replace the innermost set of ARGS with 5307 the arguments appropriate for substitution. For example, 5308 given: 5309 5310 template <class T> struct S {}; 5311 template <class T> struct S<T*> {}; 5312 5313 and supposing that we are instantiating S<int*>, ARGS will 5314 present be {int*} but we need {int}. */ 5315 tree inner_args 5316 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), 5317 args); 5318 5319 /* If there were multiple levels in ARGS, replacing the 5320 innermost level would alter CLASSTYPE_TI_ARGS, which we don't 5321 want, so we make a copy first. */ 5322 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) 5323 { 5324 args = copy_node (args); 5325 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); 5326 } 5327 else 5328 args = inner_args; 5329 } 5330 5331 if (flag_external_templates) 5332 { 5333 if (flag_alt_external_templates) 5334 { 5335 CLASSTYPE_INTERFACE_ONLY (type) = interface_only; 5336 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); 5337 } 5338 else 5339 { 5340 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); 5341 SET_CLASSTYPE_INTERFACE_UNKNOWN_X 5342 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); 5343 } 5344 } 5345 else 5346 { 5347 SET_CLASSTYPE_INTERFACE_UNKNOWN (type); 5348 } 5349 5350 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); 5351 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); 5352 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); 5353 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); 5354 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); 5355 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); 5356 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); 5357 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); 5358 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); 5359 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); 5360 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); 5361 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); 5362 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); 5363 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); 5364 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); 5365 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type) 5366 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern); 5367 TYPE_USES_MULTIPLE_INHERITANCE (type) 5368 = TYPE_USES_MULTIPLE_INHERITANCE (pattern); 5369 TYPE_USES_VIRTUAL_BASECLASSES (type) 5370 = TYPE_USES_VIRTUAL_BASECLASSES (pattern); 5371 TYPE_PACKED (type) = TYPE_PACKED (pattern); 5372 TYPE_ALIGN (type) = TYPE_ALIGN (pattern); 5373 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); 5374 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ 5375 if (ANON_AGGR_TYPE_P (pattern)) 5376 SET_ANON_AGGR_TYPE_P (type); 5377 5378 if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))) 5379 /* First instantiate our enclosing class. */ 5380 complete_type (TYPE_CONTEXT (type)); 5381 5382 if (TYPE_BINFO_BASETYPES (pattern)) 5383 { 5384 tree base_list = NULL_TREE; 5385 tree pbases = TYPE_BINFO_BASETYPES (pattern); 5386 int i; 5387 5388 /* Substitute into each of the bases to determine the actual 5389 basetypes. */ 5390 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) 5391 { 5392 tree base; 5393 tree access; 5394 tree pbase; 5395 5396 pbase = TREE_VEC_ELT (pbases, i); 5397 5398 /* Substitute to figure out the base class. */ 5399 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE); 5400 if (base == error_mark_node) 5401 continue; 5402 5403 /* Calculate the correct access node. */ 5404 if (TREE_VIA_VIRTUAL (pbase)) 5405 { 5406 if (TREE_VIA_PUBLIC (pbase)) 5407 access = access_public_virtual_node; 5408 else if (TREE_VIA_PROTECTED (pbase)) 5409 access = access_protected_virtual_node; 5410 else 5411 access = access_private_virtual_node; 5412 } 5413 else 5414 { 5415 if (TREE_VIA_PUBLIC (pbase)) 5416 access = access_public_node; 5417 else if (TREE_VIA_PROTECTED (pbase)) 5418 access = access_protected_node; 5419 else 5420 access = access_private_node; 5421 } 5422 5423 base_list = tree_cons (access, base, base_list); 5424 } 5425 5426 /* The list is now in reverse order; correct that. */ 5427 base_list = nreverse (base_list); 5428 5429 /* Now call xref_basetypes to set up all the base-class 5430 information. */ 5431 xref_basetypes (type, base_list); 5432 } 5433 5434 /* Now that our base classes are set up, enter the scope of the 5435 class, so that name lookups into base classes, etc. will work 5436 correctly. This is precisely analogous to what we do in 5437 begin_class_definition when defining an ordinary non-template 5438 class. */ 5439 pushclass (type, 1); 5440 5441 /* Now members are processed in the order of declaration. */ 5442 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member)) 5443 { 5444 tree t = TREE_VALUE (member); 5445 5446 if (TREE_PURPOSE (member)) 5447 { 5448 if (TYPE_P (t)) 5449 { 5450 /* Build new CLASSTYPE_NESTED_UDTS. */ 5451 5452 tree tag = t; 5453 tree name = TYPE_IDENTIFIER (tag); 5454 tree newtag; 5455 5456 newtag = tsubst (tag, args, tf_error, NULL_TREE); 5457 my_friendly_assert (newtag != error_mark_node, 20010206); 5458 if (TREE_CODE (newtag) != ENUMERAL_TYPE) 5459 { 5460 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) 5461 /* Unfortunately, lookup_template_class sets 5462 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial 5463 instantiation (i.e., for the type of a member template 5464 class nested within a template class.) This behavior is 5465 required for maybe_process_partial_specialization to work 5466 correctly, but is not accurate in this case; the TAG is not 5467 an instantiation of anything. (The corresponding 5468 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ 5469 CLASSTYPE_USE_TEMPLATE (newtag) = 0; 5470 5471 /* Now, we call pushtag to put this NEWTAG into the scope of 5472 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid 5473 pushtag calling push_template_decl. We don't have to do 5474 this for enums because it will already have been done in 5475 tsubst_enum. */ 5476 if (name) 5477 SET_IDENTIFIER_TYPE_VALUE (name, newtag); 5478 pushtag (name, newtag, /*globalize=*/0); 5479 } 5480 } 5481 else if (TREE_CODE (t) == FUNCTION_DECL 5482 || DECL_FUNCTION_TEMPLATE_P (t)) 5483 { 5484 /* Build new TYPE_METHODS. */ 5485 5486 tree r = tsubst (t, args, tf_error, NULL_TREE); 5487 set_current_access_from_decl (r); 5488 grok_special_member_properties (r); 5489 finish_member_declaration (r); 5490 } 5491 else 5492 { 5493 /* Build new TYPE_FIELDS. */ 5494 5495 if (TREE_CODE (t) != CONST_DECL) 5496 { 5497 tree r; 5498 5499 /* The the file and line for this declaration, to assist 5500 in error message reporting. Since we called 5501 push_tinst_level above, we don't need to restore these. */ 5502 lineno = DECL_SOURCE_LINE (t); 5503 input_filename = DECL_SOURCE_FILE (t); 5504 5505 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE); 5506 if (TREE_CODE (r) == VAR_DECL) 5507 { 5508 tree init; 5509 5510 if (DECL_INITIALIZED_IN_CLASS_P (r)) 5511 init = tsubst_expr (DECL_INITIAL (t), args, 5512 tf_error | tf_warning, NULL_TREE); 5513 else 5514 init = NULL_TREE; 5515 5516 finish_static_data_member_decl (r, init, 5517 /*asmspec_tree=*/NULL_TREE, 5518 /*flags=*/0); 5519 5520 if (DECL_INITIALIZED_IN_CLASS_P (r)) 5521 check_static_variable_definition (r, TREE_TYPE (r)); 5522 } 5523 else if (TREE_CODE (r) == FIELD_DECL) 5524 { 5525 /* Determine whether R has a valid type and can be 5526 completed later. If R is invalid, then it is 5527 replaced by error_mark_node so that it will not be 5528 added to TYPE_FIELDS. */ 5529 tree rtype = TREE_TYPE (r); 5530 if (can_complete_type_without_circularity (rtype)) 5531 complete_type (rtype); 5532 5533 if (!COMPLETE_TYPE_P (rtype)) 5534 { 5535 cxx_incomplete_type_error (r, rtype); 5536 r = error_mark_node; 5537 } 5538 } 5539 5540 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, 5541 such a thing will already have been added to the field 5542 list by tsubst_enum in finish_member_declaration in the 5543 CLASSTYPE_NESTED_UDTS case above. */ 5544 if (!(TREE_CODE (r) == TYPE_DECL 5545 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE 5546 && DECL_ARTIFICIAL (r))) 5547 { 5548 set_current_access_from_decl (r); 5549 finish_member_declaration (r); 5550 } 5551 } 5552 } 5553 } 5554 else 5555 { 5556 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) 5557 { 5558 /* Build new CLASSTYPE_FRIEND_CLASSES. */ 5559 5560 tree friend_type = t; 5561 tree new_friend_type; 5562 5563 if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5564 new_friend_type = tsubst_friend_class (friend_type, args); 5565 else if (uses_template_parms (friend_type)) 5566 new_friend_type = tsubst (friend_type, args, 5567 tf_error | tf_warning, NULL_TREE); 5568 else 5569 { 5570 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); 5571 5572 /* The call to xref_tag_from_type does injection for friend 5573 classes. */ 5574 push_nested_namespace (ns); 5575 new_friend_type = 5576 xref_tag_from_type (friend_type, NULL_TREE, 1); 5577 pop_nested_namespace (ns); 5578 } 5579 5580 if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5581 /* Trick make_friend_class into realizing that the friend 5582 we're adding is a template, not an ordinary class. It's 5583 important that we use make_friend_class since it will 5584 perform some error-checking and output cross-reference 5585 information. */ 5586 ++processing_template_decl; 5587 5588 if (new_friend_type != error_mark_node) 5589 make_friend_class (type, new_friend_type); 5590 5591 if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5592 --processing_template_decl; 5593 } 5594 else 5595 /* Build new DECL_FRIENDLIST. */ 5596 add_friend (type, tsubst_friend_function (t, args)); 5597 } 5598 } 5599 5600 /* Set the file and line number information to whatever is given for 5601 the class itself. This puts error messages involving generated 5602 implicit functions at a predictable point, and the same point 5603 that would be used for non-template classes. */ 5604 typedecl = TYPE_MAIN_DECL (type); 5605 lineno = DECL_SOURCE_LINE (typedecl); 5606 input_filename = DECL_SOURCE_FILE (typedecl); 5607 5608 unreverse_member_declarations (type); 5609 finish_struct_1 (type); 5610 CLASSTYPE_GOT_SEMICOLON (type) = 1; 5611 5612 /* Clear this now so repo_template_used is happy. */ 5613 TYPE_BEING_DEFINED (type) = 0; 5614 repo_template_used (type); 5615 5616 /* Now that the class is complete, instantiate default arguments for 5617 any member functions. We don't do this earlier because the 5618 default arguments may reference members of the class. */ 5619 if (!PRIMARY_TEMPLATE_P (template)) 5620 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) 5621 if (TREE_CODE (t) == FUNCTION_DECL 5622 /* Implicitly generated member functions will not have template 5623 information; they are not instantiations, but instead are 5624 created "fresh" for each instantiation. */ 5625 && DECL_TEMPLATE_INFO (t)) 5626 tsubst_default_arguments (t); 5627 5628 popclass (); 5629 pop_from_top_level (); 5630 pop_tinst_level (); 5631 5632 if (TYPE_CONTAINS_VPTR_P (type)) 5633 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); 5634 5635 return type; 5636} 5637 5638static int 5639list_eq (t1, t2) 5640 tree t1, t2; 5641{ 5642 if (t1 == NULL_TREE) 5643 return t2 == NULL_TREE; 5644 if (t2 == NULL_TREE) 5645 return 0; 5646 /* Don't care if one declares its arg const and the other doesn't -- the 5647 main variant of the arg type is all that matters. */ 5648 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) 5649 != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) 5650 return 0; 5651 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); 5652} 5653 5654/* If arg is a non-type template parameter that does not depend on template 5655 arguments, fold it like we weren't in the body of a template. */ 5656 5657static tree 5658maybe_fold_nontype_arg (arg) 5659 tree arg; 5660{ 5661 if (arg && !TYPE_P (arg) && !uses_template_parms (arg)) 5662 { 5663 /* Sometimes, one of the args was an expression involving a 5664 template constant parameter, like N - 1. Now that we've 5665 tsubst'd, we might have something like 2 - 1. This will 5666 confuse lookup_template_class, so we do constant folding 5667 here. We have to unset processing_template_decl, to 5668 fool build_expr_from_tree() into building an actual 5669 tree. */ 5670 5671 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already 5672 as simple as it's going to get, and trying to reprocess 5673 the trees will break. */ 5674 if (!TREE_TYPE (arg)) 5675 { 5676 int saved_processing_template_decl = processing_template_decl; 5677 processing_template_decl = 0; 5678 arg = build_expr_from_tree (arg); 5679 processing_template_decl = saved_processing_template_decl; 5680 } 5681 5682 arg = fold (arg); 5683 } 5684 return arg; 5685} 5686 5687/* Substitute ARGS into the vector of template arguments T. */ 5688 5689static tree 5690tsubst_template_arg_vector (t, args, complain) 5691 tree t; 5692 tree args; 5693 tsubst_flags_t complain; 5694{ 5695 int len = TREE_VEC_LENGTH (t), need_new = 0, i; 5696 tree *elts = (tree *) alloca (len * sizeof (tree)); 5697 5698 memset ((char *) elts, 0, len * sizeof (tree)); 5699 5700 for (i = 0; i < len; i++) 5701 { 5702 if (TREE_VEC_ELT (t, i) != NULL_TREE 5703 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) 5704 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), 5705 args, complain); 5706 else 5707 elts[i] = maybe_fold_nontype_arg 5708 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, 5709 NULL_TREE)); 5710 5711 if (elts[i] == error_mark_node) 5712 return error_mark_node; 5713 5714 if (elts[i] != TREE_VEC_ELT (t, i)) 5715 need_new = 1; 5716 } 5717 5718 if (!need_new) 5719 return t; 5720 5721 t = make_tree_vec (len); 5722 for (i = 0; i < len; i++) 5723 TREE_VEC_ELT (t, i) = elts[i]; 5724 5725 return t; 5726} 5727 5728/* Return the result of substituting ARGS into the template parameters 5729 given by PARMS. If there are m levels of ARGS and m + n levels of 5730 PARMS, then the result will contain n levels of PARMS. For 5731 example, if PARMS is `template <class T> template <class U> 5732 template <T*, U, class V>' and ARGS is {{int}, {double}} then the 5733 result will be `template <int*, double, class V>'. */ 5734 5735static tree 5736tsubst_template_parms (parms, args, complain) 5737 tree parms; 5738 tree args; 5739 tsubst_flags_t complain; 5740{ 5741 tree r = NULL_TREE; 5742 tree* new_parms; 5743 5744 for (new_parms = &r; 5745 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); 5746 new_parms = &(TREE_CHAIN (*new_parms)), 5747 parms = TREE_CHAIN (parms)) 5748 { 5749 tree new_vec = 5750 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); 5751 int i; 5752 5753 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) 5754 { 5755 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); 5756 tree default_value = TREE_PURPOSE (tuple); 5757 tree parm_decl = TREE_VALUE (tuple); 5758 5759 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); 5760 default_value = tsubst_expr (default_value, args, 5761 complain, NULL_TREE); 5762 tuple = build_tree_list (maybe_fold_nontype_arg (default_value), 5763 parm_decl); 5764 TREE_VEC_ELT (new_vec, i) = tuple; 5765 } 5766 5767 *new_parms = 5768 tree_cons (size_int (TMPL_PARMS_DEPTH (parms) 5769 - TMPL_ARGS_DEPTH (args)), 5770 new_vec, NULL_TREE); 5771 } 5772 5773 return r; 5774} 5775 5776/* Substitute the ARGS into the indicated aggregate (or enumeration) 5777 type T. If T is not an aggregate or enumeration type, it is 5778 handled as if by tsubst. IN_DECL is as for tsubst. If 5779 ENTERING_SCOPE is nonzero, T is the context for a template which 5780 we are presently tsubst'ing. Return the substituted value. */ 5781 5782static tree 5783tsubst_aggr_type (t, args, complain, in_decl, entering_scope) 5784 tree t; 5785 tree args; 5786 tsubst_flags_t complain; 5787 tree in_decl; 5788 int entering_scope; 5789{ 5790 if (t == NULL_TREE) 5791 return NULL_TREE; 5792 5793 switch (TREE_CODE (t)) 5794 { 5795 case RECORD_TYPE: 5796 if (TYPE_PTRMEMFUNC_P (t)) 5797 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); 5798 5799 /* else fall through */ 5800 case ENUMERAL_TYPE: 5801 case UNION_TYPE: 5802 if (TYPE_TEMPLATE_INFO (t)) 5803 { 5804 tree argvec; 5805 tree context; 5806 tree r; 5807 5808 /* First, determine the context for the type we are looking 5809 up. */ 5810 if (TYPE_CONTEXT (t) != NULL_TREE) 5811 context = tsubst_aggr_type (TYPE_CONTEXT (t), args, 5812 complain, 5813 in_decl, /*entering_scope=*/1); 5814 else 5815 context = NULL_TREE; 5816 5817 /* Then, figure out what arguments are appropriate for the 5818 type we are trying to find. For example, given: 5819 5820 template <class T> struct S; 5821 template <class T, class U> void f(T, U) { S<U> su; } 5822 5823 and supposing that we are instantiating f<int, double>, 5824 then our ARGS will be {int, double}, but, when looking up 5825 S we only want {double}. */ 5826 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, 5827 complain); 5828 if (argvec == error_mark_node) 5829 return error_mark_node; 5830 5831 r = lookup_template_class (t, argvec, in_decl, context, 5832 entering_scope, complain); 5833 5834 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); 5835 } 5836 else 5837 /* This is not a template type, so there's nothing to do. */ 5838 return t; 5839 5840 default: 5841 return tsubst (t, args, complain, in_decl); 5842 } 5843} 5844 5845/* Substitute into the default argument ARG (a default argument for 5846 FN), which has the indicated TYPE. */ 5847 5848tree 5849tsubst_default_argument (fn, type, arg) 5850 tree fn; 5851 tree type; 5852 tree arg; 5853{ 5854 /* This default argument came from a template. Instantiate the 5855 default argument here, not in tsubst. In the case of 5856 something like: 5857 5858 template <class T> 5859 struct S { 5860 static T t(); 5861 void f(T = t()); 5862 }; 5863 5864 we must be careful to do name lookup in the scope of S<T>, 5865 rather than in the current class. 5866 5867 ??? current_class_type affects a lot more than name lookup. This is 5868 very fragile. Fortunately, it will go away when we do 2-phase name 5869 binding properly. */ 5870 5871 /* FN is already the desired FUNCTION_DECL. */ 5872 push_access_scope (fn); 5873 5874 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), 5875 tf_error | tf_warning, NULL_TREE); 5876 5877 pop_access_scope (fn); 5878 5879 /* Make sure the default argument is reasonable. */ 5880 arg = check_default_argument (type, arg); 5881 5882 return arg; 5883} 5884 5885/* Substitute into all the default arguments for FN. */ 5886 5887static void 5888tsubst_default_arguments (fn) 5889 tree fn; 5890{ 5891 tree arg; 5892 tree tmpl_args; 5893 5894 tmpl_args = DECL_TI_ARGS (fn); 5895 5896 /* If this function is not yet instantiated, we certainly don't need 5897 its default arguments. */ 5898 if (uses_template_parms (tmpl_args)) 5899 return; 5900 5901 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); 5902 arg; 5903 arg = TREE_CHAIN (arg)) 5904 if (TREE_PURPOSE (arg)) 5905 TREE_PURPOSE (arg) = tsubst_default_argument (fn, 5906 TREE_VALUE (arg), 5907 TREE_PURPOSE (arg)); 5908} 5909 5910/* Substitute the ARGS into the T, which is a _DECL. TYPE is the 5911 (already computed) substitution of ARGS into TREE_TYPE (T), if 5912 appropriate. Return the result of the substitution. Issue error 5913 and warning messages under control of COMPLAIN. */ 5914 5915static tree 5916tsubst_decl (t, args, type, complain) 5917 tree t; 5918 tree args; 5919 tree type; 5920 tsubst_flags_t complain; 5921{ 5922 int saved_lineno; 5923 const char *saved_filename; 5924 tree r = NULL_TREE; 5925 tree in_decl = t; 5926 5927 /* Set the filename and linenumber to improve error-reporting. */ 5928 saved_lineno = lineno; 5929 saved_filename = input_filename; 5930 lineno = DECL_SOURCE_LINE (t); 5931 input_filename = DECL_SOURCE_FILE (t); 5932 5933 switch (TREE_CODE (t)) 5934 { 5935 case TEMPLATE_DECL: 5936 { 5937 /* We can get here when processing a member template function 5938 of a template class. */ 5939 tree decl = DECL_TEMPLATE_RESULT (t); 5940 tree spec; 5941 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); 5942 5943 if (!is_template_template_parm) 5944 { 5945 /* We might already have an instance of this template. 5946 The ARGS are for the surrounding class type, so the 5947 full args contain the tsubst'd args for the context, 5948 plus the innermost args from the template decl. */ 5949 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) 5950 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) 5951 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); 5952 tree full_args; 5953 5954 full_args = tsubst_template_arg_vector (tmpl_args, args, 5955 complain); 5956 5957 /* tsubst_template_arg_vector doesn't copy the vector if 5958 nothing changed. But, *something* should have 5959 changed. */ 5960 my_friendly_assert (full_args != tmpl_args, 0); 5961 5962 spec = retrieve_specialization (t, full_args); 5963 if (spec != NULL_TREE) 5964 { 5965 r = spec; 5966 break; 5967 } 5968 } 5969 5970 /* Make a new template decl. It will be similar to the 5971 original, but will record the current template arguments. 5972 We also create a new function declaration, which is just 5973 like the old one, but points to this new template, rather 5974 than the old one. */ 5975 r = copy_decl (t); 5976 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); 5977 TREE_CHAIN (r) = NULL_TREE; 5978 5979 if (is_template_template_parm) 5980 { 5981 tree new_decl = tsubst (decl, args, complain, in_decl); 5982 DECL_TEMPLATE_RESULT (r) = new_decl; 5983 TREE_TYPE (r) = TREE_TYPE (new_decl); 5984 break; 5985 } 5986 5987 DECL_CONTEXT (r) 5988 = tsubst_aggr_type (DECL_CONTEXT (t), args, 5989 complain, in_decl, 5990 /*entering_scope=*/1); 5991 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); 5992 5993 if (TREE_CODE (decl) == TYPE_DECL) 5994 { 5995 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 5996 TREE_TYPE (r) = new_type; 5997 CLASSTYPE_TI_TEMPLATE (new_type) = r; 5998 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); 5999 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); 6000 } 6001 else 6002 { 6003 tree new_decl = tsubst (decl, args, complain, in_decl); 6004 6005 DECL_TEMPLATE_RESULT (r) = new_decl; 6006 DECL_TI_TEMPLATE (new_decl) = r; 6007 TREE_TYPE (r) = TREE_TYPE (new_decl); 6008 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); 6009 } 6010 6011 SET_DECL_IMPLICIT_INSTANTIATION (r); 6012 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; 6013 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; 6014 6015 /* The template parameters for this new template are all the 6016 template parameters for the old template, except the 6017 outermost level of parameters. */ 6018 DECL_TEMPLATE_PARMS (r) 6019 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, 6020 complain); 6021 6022 if (PRIMARY_TEMPLATE_P (t)) 6023 DECL_PRIMARY_TEMPLATE (r) = r; 6024 6025 /* We don't partially instantiate partial specializations. */ 6026 if (TREE_CODE (decl) == TYPE_DECL) 6027 break; 6028 6029 /* Record this partial instantiation. */ 6030 register_specialization (r, t, 6031 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); 6032 6033 } 6034 break; 6035 6036 case FUNCTION_DECL: 6037 { 6038 tree ctx; 6039 tree argvec = NULL_TREE; 6040 tree *friends; 6041 tree gen_tmpl; 6042 int member; 6043 int args_depth; 6044 int parms_depth; 6045 6046 /* Nobody should be tsubst'ing into non-template functions. */ 6047 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); 6048 6049 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) 6050 { 6051 tree spec; 6052 6053 /* If T is not dependent, just return it. */ 6054 if (!uses_template_parms (DECL_TI_ARGS (t))) 6055 return t; 6056 6057 /* Calculate the most general template of which R is a 6058 specialization, and the complete set of arguments used to 6059 specialize R. */ 6060 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); 6061 argvec 6062 = tsubst_template_arg_vector (DECL_TI_ARGS 6063 (DECL_TEMPLATE_RESULT (gen_tmpl)), 6064 args, complain); 6065 6066 /* Check to see if we already have this specialization. */ 6067 spec = retrieve_specialization (gen_tmpl, argvec); 6068 6069 if (spec) 6070 { 6071 r = spec; 6072 break; 6073 } 6074 6075 /* We can see more levels of arguments than parameters if 6076 there was a specialization of a member template, like 6077 this: 6078 6079 template <class T> struct S { template <class U> void f(); } 6080 template <> template <class U> void S<int>::f(U); 6081 6082 Here, we'll be substituting into the specialization, 6083 because that's where we can find the code we actually 6084 want to generate, but we'll have enough arguments for 6085 the most general template. 6086 6087 We also deal with the peculiar case: 6088 6089 template <class T> struct S { 6090 template <class U> friend void f(); 6091 }; 6092 template <class U> void f() {} 6093 template S<int>; 6094 template void f<double>(); 6095 6096 Here, the ARGS for the instantiation of will be {int, 6097 double}. But, we only need as many ARGS as there are 6098 levels of template parameters in CODE_PATTERN. We are 6099 careful not to get fooled into reducing the ARGS in 6100 situations like: 6101 6102 template <class T> struct S { template <class U> void f(U); } 6103 template <class T> template <> void S<T>::f(int) {} 6104 6105 which we can spot because the pattern will be a 6106 specialization in this case. */ 6107 args_depth = TMPL_ARGS_DEPTH (args); 6108 parms_depth = 6109 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); 6110 if (args_depth > parms_depth 6111 && !DECL_TEMPLATE_SPECIALIZATION (t)) 6112 args = get_innermost_template_args (args, parms_depth); 6113 } 6114 else 6115 { 6116 /* This special case arises when we have something like this: 6117 6118 template <class T> struct S { 6119 friend void f<int>(int, double); 6120 }; 6121 6122 Here, the DECL_TI_TEMPLATE for the friend declaration 6123 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are 6124 being called from tsubst_friend_function, and we want 6125 only to create a new decl (R) with appropriate types so 6126 that we can call determine_specialization. */ 6127 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) 6128 == LOOKUP_EXPR) 6129 || (TREE_CODE (DECL_TI_TEMPLATE (t)) 6130 == IDENTIFIER_NODE), 0); 6131 gen_tmpl = NULL_TREE; 6132 } 6133 6134 if (DECL_CLASS_SCOPE_P (t)) 6135 { 6136 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) 6137 member = 2; 6138 else 6139 member = 1; 6140 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 6141 complain, t, 6142 /*entering_scope=*/1); 6143 } 6144 else 6145 { 6146 member = 0; 6147 ctx = DECL_CONTEXT (t); 6148 } 6149 type = tsubst (type, args, complain, in_decl); 6150 if (type == error_mark_node) 6151 return error_mark_node; 6152 6153 /* We do NOT check for matching decls pushed separately at this 6154 point, as they may not represent instantiations of this 6155 template, and in any case are considered separate under the 6156 discrete model. */ 6157 r = copy_decl (t); 6158 DECL_USE_TEMPLATE (r) = 0; 6159 TREE_TYPE (r) = type; 6160 /* Clear out the mangled name and RTL for the instantiation. */ 6161 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 6162 SET_DECL_RTL (r, NULL_RTX); 6163 6164 DECL_CONTEXT (r) = ctx; 6165 6166 if (member && DECL_CONV_FN_P (r)) 6167 /* Type-conversion operator. Reconstruct the name, in 6168 case it's the name of one of the template's parameters. */ 6169 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); 6170 6171 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, 6172 complain, t); 6173 DECL_RESULT (r) = NULL_TREE; 6174 6175 TREE_STATIC (r) = 0; 6176 TREE_PUBLIC (r) = TREE_PUBLIC (t); 6177 DECL_EXTERNAL (r) = 1; 6178 DECL_INTERFACE_KNOWN (r) = 0; 6179 DECL_DEFER_OUTPUT (r) = 0; 6180 TREE_CHAIN (r) = NULL_TREE; 6181 DECL_PENDING_INLINE_INFO (r) = 0; 6182 DECL_PENDING_INLINE_P (r) = 0; 6183 DECL_SAVED_TREE (r) = NULL_TREE; 6184 TREE_USED (r) = 0; 6185 if (DECL_CLONED_FUNCTION (r)) 6186 { 6187 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), 6188 args, complain, t); 6189 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); 6190 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; 6191 } 6192 6193 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do 6194 this in the special friend case mentioned above where 6195 GEN_TMPL is NULL. */ 6196 if (gen_tmpl) 6197 { 6198 DECL_TEMPLATE_INFO (r) 6199 = tree_cons (gen_tmpl, argvec, NULL_TREE); 6200 SET_DECL_IMPLICIT_INSTANTIATION (r); 6201 register_specialization (r, gen_tmpl, argvec); 6202 6203 /* We're not supposed to instantiate default arguments 6204 until they are called, for a template. But, for a 6205 declaration like: 6206 6207 template <class T> void f () 6208 { extern void g(int i = T()); } 6209 6210 we should do the substitution when the template is 6211 instantiated. We handle the member function case in 6212 instantiate_class_template since the default arguments 6213 might refer to other members of the class. */ 6214 if (!member 6215 && !PRIMARY_TEMPLATE_P (gen_tmpl) 6216 && !uses_template_parms (argvec)) 6217 tsubst_default_arguments (r); 6218 } 6219 6220 /* Copy the list of befriending classes. */ 6221 for (friends = &DECL_BEFRIENDING_CLASSES (r); 6222 *friends; 6223 friends = &TREE_CHAIN (*friends)) 6224 { 6225 *friends = copy_node (*friends); 6226 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), 6227 args, complain, 6228 in_decl); 6229 } 6230 6231 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) 6232 { 6233 maybe_retrofit_in_chrg (r); 6234 if (DECL_CONSTRUCTOR_P (r)) 6235 grok_ctor_properties (ctx, r); 6236 /* If this is an instantiation of a member template, clone it. 6237 If it isn't, that'll be handled by 6238 clone_constructors_and_destructors. */ 6239 if (PRIMARY_TEMPLATE_P (gen_tmpl)) 6240 clone_function_decl (r, /*update_method_vec_p=*/0); 6241 } 6242 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) 6243 grok_op_properties (r, DECL_FRIEND_P (r)); 6244 } 6245 break; 6246 6247 case PARM_DECL: 6248 { 6249 r = copy_node (t); 6250 if (DECL_TEMPLATE_PARM_P (t)) 6251 SET_DECL_TEMPLATE_PARM_P (r); 6252 6253 TREE_TYPE (r) = type; 6254 c_apply_type_quals_to_decl (cp_type_quals (type), r); 6255 6256 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) 6257 DECL_INITIAL (r) = TREE_TYPE (r); 6258 else 6259 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, 6260 complain, in_decl); 6261 6262 DECL_CONTEXT (r) = NULL_TREE; 6263 6264 if (!DECL_TEMPLATE_PARM_P (r)) 6265 DECL_ARG_TYPE (r) = type_passed_as (type); 6266 if (TREE_CHAIN (t)) 6267 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, 6268 complain, TREE_CHAIN (t)); 6269 } 6270 break; 6271 6272 case FIELD_DECL: 6273 { 6274 r = copy_decl (t); 6275 TREE_TYPE (r) = type; 6276 c_apply_type_quals_to_decl (cp_type_quals (type), r); 6277 6278 /* We don't have to set DECL_CONTEXT here; it is set by 6279 finish_member_declaration. */ 6280 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, 6281 complain, in_decl); 6282 TREE_CHAIN (r) = NULL_TREE; 6283 if (VOID_TYPE_P (type)) 6284 cp_error_at ("instantiation of `%D' as type `%T'", r, type); 6285 } 6286 break; 6287 6288 case USING_DECL: 6289 { 6290 r = copy_node (t); 6291 DECL_INITIAL (r) 6292 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); 6293 TREE_CHAIN (r) = NULL_TREE; 6294 } 6295 break; 6296 6297 case TYPE_DECL: 6298 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM 6299 || t == TYPE_MAIN_DECL (TREE_TYPE (t))) 6300 { 6301 /* If this is the canonical decl, we don't have to mess with 6302 instantiations, and often we can't (for typename, template 6303 type parms and such). Note that TYPE_NAME is not correct for 6304 the above test if we've copied the type for a typedef. */ 6305 r = TYPE_NAME (type); 6306 break; 6307 } 6308 6309 /* Fall through. */ 6310 6311 case VAR_DECL: 6312 { 6313 tree argvec = NULL_TREE; 6314 tree gen_tmpl = NULL_TREE; 6315 tree spec; 6316 tree tmpl = NULL_TREE; 6317 tree ctx; 6318 int local_p; 6319 6320 /* Assume this is a non-local variable. */ 6321 local_p = 0; 6322 6323 if (TYPE_P (CP_DECL_CONTEXT (t))) 6324 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 6325 complain, 6326 in_decl, /*entering_scope=*/1); 6327 else if (DECL_NAMESPACE_SCOPE_P (t)) 6328 ctx = DECL_CONTEXT (t); 6329 else 6330 { 6331 /* Subsequent calls to pushdecl will fill this in. */ 6332 ctx = NULL_TREE; 6333 local_p = 1; 6334 } 6335 6336 /* Check to see if we already have this specialization. */ 6337 if (!local_p) 6338 { 6339 tmpl = DECL_TI_TEMPLATE (t); 6340 gen_tmpl = most_general_template (tmpl); 6341 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); 6342 spec = retrieve_specialization (gen_tmpl, argvec); 6343 } 6344 else 6345 spec = retrieve_local_specialization (t); 6346 6347 if (spec) 6348 { 6349 r = spec; 6350 break; 6351 } 6352 6353 r = copy_decl (t); 6354 if (TREE_CODE (r) == VAR_DECL) 6355 type = complete_type (type); 6356 else if (DECL_SELF_REFERENCE_P (t)) 6357 SET_DECL_SELF_REFERENCE_P (r); 6358 TREE_TYPE (r) = type; 6359 c_apply_type_quals_to_decl (cp_type_quals (type), r); 6360 DECL_CONTEXT (r) = ctx; 6361 /* Clear out the mangled name and RTL for the instantiation. */ 6362 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 6363 SET_DECL_RTL (r, NULL_RTX); 6364 6365 /* Don't try to expand the initializer until someone tries to use 6366 this variable; otherwise we run into circular dependencies. */ 6367 DECL_INITIAL (r) = NULL_TREE; 6368 SET_DECL_RTL (r, NULL_RTX); 6369 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; 6370 6371 /* Even if the original location is out of scope, the newly 6372 substituted one is not. */ 6373 if (TREE_CODE (r) == VAR_DECL) 6374 { 6375 DECL_DEAD_FOR_LOCAL (r) = 0; 6376 DECL_INITIALIZED_P (r) = 0; 6377 } 6378 6379 if (!local_p) 6380 { 6381 /* A static data member declaration is always marked 6382 external when it is declared in-class, even if an 6383 initializer is present. We mimic the non-template 6384 processing here. */ 6385 DECL_EXTERNAL (r) = 1; 6386 6387 register_specialization (r, gen_tmpl, argvec); 6388 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); 6389 SET_DECL_IMPLICIT_INSTANTIATION (r); 6390 } 6391 else 6392 register_local_specialization (r, t); 6393 6394 TREE_CHAIN (r) = NULL_TREE; 6395 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type)) 6396 cp_error_at ("instantiation of `%D' as type `%T'", r, type); 6397 /* Compute the size, alignment, etc. of R. */ 6398 layout_decl (r, 0); 6399 } 6400 break; 6401 6402 default: 6403 abort (); 6404 } 6405 6406 /* Restore the file and line information. */ 6407 lineno = saved_lineno; 6408 input_filename = saved_filename; 6409 6410 return r; 6411} 6412 6413/* Substitue into the ARG_TYPES of a function type. */ 6414 6415static tree 6416tsubst_arg_types (arg_types, args, complain, in_decl) 6417 tree arg_types; 6418 tree args; 6419 tsubst_flags_t complain; 6420 tree in_decl; 6421{ 6422 tree remaining_arg_types; 6423 tree type; 6424 6425 if (!arg_types || arg_types == void_list_node) 6426 return arg_types; 6427 6428 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), 6429 args, complain, in_decl); 6430 if (remaining_arg_types == error_mark_node) 6431 return error_mark_node; 6432 6433 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); 6434 if (type == error_mark_node) 6435 return error_mark_node; 6436 if (VOID_TYPE_P (type)) 6437 { 6438 if (complain & tf_error) 6439 { 6440 error ("invalid parameter type `%T'", type); 6441 if (in_decl) 6442 cp_error_at ("in declaration `%D'", in_decl); 6443 } 6444 return error_mark_node; 6445 } 6446 6447 /* Do array-to-pointer, function-to-pointer conversion, and ignore 6448 top-level qualifiers as required. */ 6449 type = TYPE_MAIN_VARIANT (type_decays_to (type)); 6450 6451 /* Note that we do not substitute into default arguments here. The 6452 standard mandates that they be instantiated only when needed, 6453 which is done in build_over_call. */ 6454 return hash_tree_cons (TREE_PURPOSE (arg_types), type, 6455 remaining_arg_types); 6456 6457} 6458 6459/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does 6460 *not* handle the exception-specification for FNTYPE, because the 6461 initial substitution of explicitly provided template parameters 6462 during argument deduction forbids substitution into the 6463 exception-specification: 6464 6465 [temp.deduct] 6466 6467 All references in the function type of the function template to the 6468 corresponding template parameters are replaced by the specified tem- 6469 plate argument values. If a substitution in a template parameter or 6470 in the function type of the function template results in an invalid 6471 type, type deduction fails. [Note: The equivalent substitution in 6472 exception specifications is done only when the function is instanti- 6473 ated, at which point a program is ill-formed if the substitution 6474 results in an invalid type.] */ 6475 6476static tree 6477tsubst_function_type (t, args, complain, in_decl) 6478 tree t; 6479 tree args; 6480 tsubst_flags_t complain; 6481 tree in_decl; 6482{ 6483 tree return_type; 6484 tree arg_types; 6485 tree fntype; 6486 6487 /* The TYPE_CONTEXT is not used for function/method types. */ 6488 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); 6489 6490 /* Substitute the return type. */ 6491 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 6492 if (return_type == error_mark_node) 6493 return error_mark_node; 6494 6495 /* Substitue the argument types. */ 6496 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, 6497 complain, in_decl); 6498 if (arg_types == error_mark_node) 6499 return error_mark_node; 6500 6501 /* Construct a new type node and return it. */ 6502 if (TREE_CODE (t) == FUNCTION_TYPE) 6503 fntype = build_function_type (return_type, arg_types); 6504 else 6505 { 6506 tree r = TREE_TYPE (TREE_VALUE (arg_types)); 6507 if (! IS_AGGR_TYPE (r)) 6508 { 6509 /* [temp.deduct] 6510 6511 Type deduction may fail for any of the following 6512 reasons: 6513 6514 -- Attempting to create "pointer to member of T" when T 6515 is not a class type. */ 6516 if (complain & tf_error) 6517 error ("creating pointer to member function of non-class type `%T'", 6518 r); 6519 return error_mark_node; 6520 } 6521 6522 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN 6523 (arg_types)); 6524 } 6525 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); 6526 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); 6527 6528 return fntype; 6529} 6530 6531/* Substitute into the PARMS of a call-declarator. */ 6532 6533static tree 6534tsubst_call_declarator_parms (parms, args, complain, in_decl) 6535 tree parms; 6536 tree args; 6537 tsubst_flags_t complain; 6538 tree in_decl; 6539{ 6540 tree new_parms; 6541 tree type; 6542 tree defarg; 6543 6544 if (!parms || parms == void_list_node) 6545 return parms; 6546 6547 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), 6548 args, complain, in_decl); 6549 6550 /* Figure out the type of this parameter. */ 6551 type = tsubst (TREE_VALUE (parms), args, complain, in_decl); 6552 6553 /* Figure out the default argument as well. Note that we use 6554 tsubst_expr since the default argument is really an expression. */ 6555 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); 6556 6557 /* Chain this parameter on to the front of those we have already 6558 processed. We don't use hash_tree_cons because that function 6559 doesn't check TREE_PARMLIST. */ 6560 new_parms = tree_cons (defarg, type, new_parms); 6561 6562 /* And note that these are parameters. */ 6563 TREE_PARMLIST (new_parms) = 1; 6564 6565 return new_parms; 6566} 6567 6568/* Take the tree structure T and replace template parameters used 6569 therein with the argument vector ARGS. IN_DECL is an associated 6570 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. 6571 Issue error and warning messages under control of COMPLAIN. Note 6572 that we must be relatively non-tolerant of extensions here, in 6573 order to preserve conformance; if we allow substitutions that 6574 should not be allowed, we may allow argument deductions that should 6575 not succeed, and therefore report ambiguous overload situations 6576 where there are none. In theory, we could allow the substitution, 6577 but indicate that it should have failed, and allow our caller to 6578 make sure that the right thing happens, but we don't try to do this 6579 yet. 6580 6581 This function is used for dealing with types, decls and the like; 6582 for expressions, use tsubst_expr or tsubst_copy. */ 6583 6584tree 6585tsubst (t, args, complain, in_decl) 6586 tree t, args; 6587 tsubst_flags_t complain; 6588 tree in_decl; 6589{ 6590 tree type, r; 6591 6592 if (t == NULL_TREE || t == error_mark_node 6593 || t == integer_type_node 6594 || t == void_type_node 6595 || t == char_type_node 6596 || TREE_CODE (t) == NAMESPACE_DECL) 6597 return t; 6598 6599 if (TREE_CODE (t) == IDENTIFIER_NODE) 6600 type = IDENTIFIER_TYPE_VALUE (t); 6601 else 6602 type = TREE_TYPE (t); 6603 if (type == unknown_type_node) 6604 abort (); 6605 6606 if (type && TREE_CODE (t) != FUNCTION_DECL 6607 && TREE_CODE (t) != TYPENAME_TYPE 6608 && TREE_CODE (t) != TEMPLATE_DECL 6609 && TREE_CODE (t) != IDENTIFIER_NODE 6610 && TREE_CODE (t) != FUNCTION_TYPE 6611 && TREE_CODE (t) != METHOD_TYPE) 6612 type = tsubst (type, args, complain, in_decl); 6613 if (type == error_mark_node) 6614 return error_mark_node; 6615 6616 if (DECL_P (t)) 6617 return tsubst_decl (t, args, type, complain); 6618 6619 switch (TREE_CODE (t)) 6620 { 6621 case RECORD_TYPE: 6622 case UNION_TYPE: 6623 case ENUMERAL_TYPE: 6624 return tsubst_aggr_type (t, args, complain, in_decl, 6625 /*entering_scope=*/0); 6626 6627 case ERROR_MARK: 6628 case IDENTIFIER_NODE: 6629 case VOID_TYPE: 6630 case REAL_TYPE: 6631 case COMPLEX_TYPE: 6632 case VECTOR_TYPE: 6633 case BOOLEAN_TYPE: 6634 case INTEGER_CST: 6635 case REAL_CST: 6636 case STRING_CST: 6637 return t; 6638 6639 case INTEGER_TYPE: 6640 if (t == integer_type_node) 6641 return t; 6642 6643 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST 6644 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) 6645 return t; 6646 6647 { 6648 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); 6649 6650 max = tsubst_expr (omax, args, complain, in_decl); 6651 if (max == error_mark_node) 6652 return error_mark_node; 6653 6654 /* See if we can reduce this expression to something simpler. */ 6655 max = maybe_fold_nontype_arg (max); 6656 if (!processing_template_decl) 6657 max = decl_constant_value (max); 6658 6659 if (processing_template_decl 6660 /* When providing explicit arguments to a template 6661 function, but leaving some arguments for subsequent 6662 deduction, MAX may be template-dependent even if we're 6663 not PROCESSING_TEMPLATE_DECL. We still need to check for 6664 template parms, though; MAX won't be an INTEGER_CST for 6665 dynamic arrays, either. */ 6666 || (TREE_CODE (max) != INTEGER_CST 6667 && uses_template_parms (max))) 6668 { 6669 tree itype = make_node (INTEGER_TYPE); 6670 TYPE_MIN_VALUE (itype) = size_zero_node; 6671 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, 6672 integer_one_node); 6673 return itype; 6674 } 6675 6676 if (integer_zerop (omax)) 6677 { 6678 /* Still allow an explicit array of size zero. */ 6679 if (pedantic) 6680 pedwarn ("creating array with size zero"); 6681 } 6682 else if (integer_zerop (max) 6683 || (TREE_CODE (max) == INTEGER_CST 6684 && INT_CST_LT (max, integer_zero_node))) 6685 { 6686 /* [temp.deduct] 6687 6688 Type deduction may fail for any of the following 6689 reasons: 6690 6691 Attempting to create an array with a size that is 6692 zero or negative. */ 6693 if (complain & tf_error) 6694 error ("creating array with size zero (`%E')", max); 6695 6696 return error_mark_node; 6697 } 6698 6699 return compute_array_index_type (NULL_TREE, max); 6700 } 6701 6702 case TEMPLATE_TYPE_PARM: 6703 case TEMPLATE_TEMPLATE_PARM: 6704 case BOUND_TEMPLATE_TEMPLATE_PARM: 6705 case TEMPLATE_PARM_INDEX: 6706 { 6707 int idx; 6708 int level; 6709 int levels; 6710 6711 r = NULL_TREE; 6712 6713 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM 6714 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM 6715 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 6716 { 6717 idx = TEMPLATE_TYPE_IDX (t); 6718 level = TEMPLATE_TYPE_LEVEL (t); 6719 } 6720 else 6721 { 6722 idx = TEMPLATE_PARM_IDX (t); 6723 level = TEMPLATE_PARM_LEVEL (t); 6724 } 6725 6726 if (TREE_VEC_LENGTH (args) > 0) 6727 { 6728 tree arg = NULL_TREE; 6729 6730 levels = TMPL_ARGS_DEPTH (args); 6731 if (level <= levels) 6732 arg = TMPL_ARG (args, level, idx); 6733 6734 if (arg == error_mark_node) 6735 return error_mark_node; 6736 else if (arg != NULL_TREE) 6737 { 6738 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) 6739 { 6740 my_friendly_assert (TYPE_P (arg), 0); 6741 return cp_build_qualified_type_real 6742 (arg, cp_type_quals (arg) | cp_type_quals (t), 6743 complain | tf_ignore_bad_quals); 6744 } 6745 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 6746 { 6747 /* We are processing a type constructed from 6748 a template template parameter */ 6749 tree argvec = tsubst (TYPE_TI_ARGS (t), 6750 args, complain, in_decl); 6751 if (argvec == error_mark_node) 6752 return error_mark_node; 6753 6754 /* We can get a TEMPLATE_TEMPLATE_PARM here when 6755 we are resolving nested-types in the signature of 6756 a member function templates. 6757 Otherwise ARG is a TEMPLATE_DECL and is the real 6758 template to be instantiated. */ 6759 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) 6760 arg = TYPE_NAME (arg); 6761 6762 r = lookup_template_class (arg, 6763 argvec, in_decl, 6764 DECL_CONTEXT (arg), 6765 /*entering_scope=*/0, 6766 complain); 6767 return cp_build_qualified_type_real 6768 (r, TYPE_QUALS (t), complain); 6769 } 6770 else 6771 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ 6772 return arg; 6773 } 6774 } 6775 else 6776 abort (); 6777 6778 if (level == 1) 6779 /* This can happen during the attempted tsubst'ing in 6780 unify. This means that we don't yet have any information 6781 about the template parameter in question. */ 6782 return t; 6783 6784 /* If we get here, we must have been looking at a parm for a 6785 more deeply nested template. Make a new version of this 6786 template parameter, but with a lower level. */ 6787 switch (TREE_CODE (t)) 6788 { 6789 case TEMPLATE_TYPE_PARM: 6790 case TEMPLATE_TEMPLATE_PARM: 6791 case BOUND_TEMPLATE_TEMPLATE_PARM: 6792 if (cp_type_quals (t)) 6793 { 6794 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); 6795 r = cp_build_qualified_type_real 6796 (r, cp_type_quals (t), 6797 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM 6798 ? tf_ignore_bad_quals : 0)); 6799 } 6800 else 6801 { 6802 r = copy_type (t); 6803 TEMPLATE_TYPE_PARM_INDEX (r) 6804 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), 6805 r, levels); 6806 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); 6807 TYPE_MAIN_VARIANT (r) = r; 6808 TYPE_POINTER_TO (r) = NULL_TREE; 6809 TYPE_REFERENCE_TO (r) = NULL_TREE; 6810 6811 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 6812 { 6813 tree argvec = tsubst (TYPE_TI_ARGS (t), args, 6814 complain, in_decl); 6815 if (argvec == error_mark_node) 6816 return error_mark_node; 6817 6818 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) 6819 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); 6820 } 6821 } 6822 break; 6823 6824 case TEMPLATE_PARM_INDEX: 6825 r = reduce_template_parm_level (t, type, levels); 6826 break; 6827 6828 default: 6829 abort (); 6830 } 6831 6832 return r; 6833 } 6834 6835 case TREE_LIST: 6836 { 6837 tree purpose, value, chain, result; 6838 6839 if (t == void_list_node) 6840 return t; 6841 6842 purpose = TREE_PURPOSE (t); 6843 if (purpose) 6844 { 6845 purpose = tsubst (purpose, args, complain, in_decl); 6846 if (purpose == error_mark_node) 6847 return error_mark_node; 6848 } 6849 value = TREE_VALUE (t); 6850 if (value) 6851 { 6852 value = tsubst (value, args, complain, in_decl); 6853 if (value == error_mark_node) 6854 return error_mark_node; 6855 } 6856 chain = TREE_CHAIN (t); 6857 if (chain && chain != void_type_node) 6858 { 6859 chain = tsubst (chain, args, complain, in_decl); 6860 if (chain == error_mark_node) 6861 return error_mark_node; 6862 } 6863 if (purpose == TREE_PURPOSE (t) 6864 && value == TREE_VALUE (t) 6865 && chain == TREE_CHAIN (t)) 6866 return t; 6867 if (TREE_PARMLIST (t)) 6868 { 6869 result = tree_cons (purpose, value, chain); 6870 TREE_PARMLIST (result) = 1; 6871 } 6872 else 6873 result = hash_tree_cons (purpose, value, chain); 6874 return result; 6875 } 6876 case TREE_VEC: 6877 if (type != NULL_TREE) 6878 { 6879 /* A binfo node. We always need to make a copy, of the node 6880 itself and of its BINFO_BASETYPES. */ 6881 6882 t = copy_node (t); 6883 6884 /* Make sure type isn't a typedef copy. */ 6885 type = BINFO_TYPE (TYPE_BINFO (type)); 6886 6887 TREE_TYPE (t) = complete_type (type); 6888 if (IS_AGGR_TYPE (type)) 6889 { 6890 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); 6891 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); 6892 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) 6893 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); 6894 } 6895 return t; 6896 } 6897 6898 /* Otherwise, a vector of template arguments. */ 6899 return tsubst_template_arg_vector (t, args, complain); 6900 6901 case POINTER_TYPE: 6902 case REFERENCE_TYPE: 6903 { 6904 enum tree_code code; 6905 6906 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) 6907 return t; 6908 6909 code = TREE_CODE (t); 6910 6911 6912 /* [temp.deduct] 6913 6914 Type deduction may fail for any of the following 6915 reasons: 6916 6917 -- Attempting to create a pointer to reference type. 6918 -- Attempting to create a reference to a reference type or 6919 a reference to void. */ 6920 if (TREE_CODE (type) == REFERENCE_TYPE 6921 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) 6922 { 6923 static int last_line = 0; 6924 static const char* last_file = 0; 6925 6926 /* We keep track of the last time we issued this error 6927 message to avoid spewing a ton of messages during a 6928 single bad template instantiation. */ 6929 if (complain & tf_error 6930 && (last_line != lineno || last_file != input_filename)) 6931 { 6932 if (TREE_CODE (type) == VOID_TYPE) 6933 error ("forming reference to void"); 6934 else 6935 error ("forming %s to reference type `%T'", 6936 (code == POINTER_TYPE) ? "pointer" : "reference", 6937 type); 6938 last_line = lineno; 6939 last_file = input_filename; 6940 } 6941 6942 return error_mark_node; 6943 } 6944 else if (code == POINTER_TYPE) 6945 { 6946 r = build_pointer_type (type); 6947 if (TREE_CODE (type) == METHOD_TYPE) 6948 r = build_ptrmemfunc_type (r); 6949 } 6950 else 6951 r = build_reference_type (type); 6952 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); 6953 6954 if (r != error_mark_node) 6955 /* Will this ever be needed for TYPE_..._TO values? */ 6956 layout_type (r); 6957 6958 return r; 6959 } 6960 case OFFSET_TYPE: 6961 { 6962 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); 6963 if (r == error_mark_node || !IS_AGGR_TYPE (r)) 6964 { 6965 /* [temp.deduct] 6966 6967 Type deduction may fail for any of the following 6968 reasons: 6969 6970 -- Attempting to create "pointer to member of T" when T 6971 is not a class type. */ 6972 if (complain & tf_error) 6973 error ("creating pointer to member of non-class type `%T'", r); 6974 return error_mark_node; 6975 } 6976 if (TREE_CODE (type) == REFERENCE_TYPE) 6977 { 6978 if (complain & tf_error) 6979 error ("creating pointer to member reference type `%T'", type); 6980 6981 return error_mark_node; 6982 } 6983 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231); 6984 if (TREE_CODE (type) == FUNCTION_TYPE) 6985 /* This is really a method type. The cv qualifiers of the 6986 this pointer should _not_ be determined by the cv 6987 qualifiers of the class type. They should be held 6988 somewhere in the FUNCTION_TYPE, but we don't do that at 6989 the moment. Consider 6990 typedef void (Func) () const; 6991 6992 template <typename T1> void Foo (Func T1::*); 6993 6994 */ 6995 return build_cplus_method_type (TYPE_MAIN_VARIANT (r), 6996 TREE_TYPE (type), 6997 TYPE_ARG_TYPES (type)); 6998 else 6999 return build_offset_type (r, type); 7000 } 7001 case FUNCTION_TYPE: 7002 case METHOD_TYPE: 7003 { 7004 tree fntype; 7005 tree raises; 7006 7007 fntype = tsubst_function_type (t, args, complain, in_decl); 7008 if (fntype == error_mark_node) 7009 return error_mark_node; 7010 7011 /* Substitue the exception specification. */ 7012 raises = TYPE_RAISES_EXCEPTIONS (t); 7013 if (raises) 7014 { 7015 tree list = NULL_TREE; 7016 7017 if (! TREE_VALUE (raises)) 7018 list = raises; 7019 else 7020 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) 7021 { 7022 tree spec = TREE_VALUE (raises); 7023 7024 spec = tsubst (spec, args, complain, in_decl); 7025 if (spec == error_mark_node) 7026 return spec; 7027 list = add_exception_specifier (list, spec, complain); 7028 } 7029 fntype = build_exception_variant (fntype, list); 7030 } 7031 return fntype; 7032 } 7033 case ARRAY_TYPE: 7034 { 7035 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); 7036 if (domain == error_mark_node) 7037 return error_mark_node; 7038 7039 /* As an optimization, we avoid regenerating the array type if 7040 it will obviously be the same as T. */ 7041 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) 7042 return t; 7043 7044 /* These checks should match the ones in grokdeclarator. 7045 7046 [temp.deduct] 7047 7048 The deduction may fail for any of the following reasons: 7049 7050 -- Attempting to create an array with an element type that 7051 is void, a function type, or a reference type. */ 7052 if (TREE_CODE (type) == VOID_TYPE 7053 || TREE_CODE (type) == FUNCTION_TYPE 7054 || TREE_CODE (type) == REFERENCE_TYPE) 7055 { 7056 if (complain & tf_error) 7057 error ("creating array of `%T'", type); 7058 return error_mark_node; 7059 } 7060 7061 r = build_cplus_array_type (type, domain); 7062 return r; 7063 } 7064 7065 case PLUS_EXPR: 7066 case MINUS_EXPR: 7067 { 7068 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7069 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); 7070 7071 if (e1 == error_mark_node || e2 == error_mark_node) 7072 return error_mark_node; 7073 7074 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); 7075 } 7076 7077 case NEGATE_EXPR: 7078 case NOP_EXPR: 7079 { 7080 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7081 if (e == error_mark_node) 7082 return error_mark_node; 7083 7084 return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); 7085 } 7086 7087 case TYPENAME_TYPE: 7088 { 7089 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, 7090 in_decl, /*entering_scope=*/1); 7091 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, 7092 complain, in_decl); 7093 7094 if (ctx == error_mark_node || f == error_mark_node) 7095 return error_mark_node; 7096 7097 if (!IS_AGGR_TYPE (ctx)) 7098 { 7099 if (complain & tf_error) 7100 error ("`%T' is not a class, struct, or union type", 7101 ctx); 7102 return error_mark_node; 7103 } 7104 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) 7105 { 7106 /* Normally, make_typename_type does not require that the CTX 7107 have complete type in order to allow things like: 7108 7109 template <class T> struct S { typename S<T>::X Y; }; 7110 7111 But, such constructs have already been resolved by this 7112 point, so here CTX really should have complete type, unless 7113 it's a partial instantiation. */ 7114 ctx = complete_type (ctx); 7115 if (!COMPLETE_TYPE_P (ctx)) 7116 { 7117 if (complain & tf_error) 7118 cxx_incomplete_type_error (NULL_TREE, ctx); 7119 return error_mark_node; 7120 } 7121 } 7122 7123 f = make_typename_type (ctx, f, 7124 (complain & tf_error) | tf_keep_type_decl); 7125 if (f == error_mark_node) 7126 return f; 7127 if (TREE_CODE (f) == TYPE_DECL) 7128 { 7129 complain |= tf_ignore_bad_quals; 7130 f = TREE_TYPE (f); 7131 } 7132 7133 return cp_build_qualified_type_real 7134 (f, cp_type_quals (f) | cp_type_quals (t), complain); 7135 } 7136 7137 case UNBOUND_CLASS_TEMPLATE: 7138 { 7139 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, 7140 in_decl, /*entering_scope=*/1); 7141 tree name = TYPE_IDENTIFIER (t); 7142 7143 if (ctx == error_mark_node || name == error_mark_node) 7144 return error_mark_node; 7145 7146 return make_unbound_class_template (ctx, name, complain); 7147 } 7148 7149 case INDIRECT_REF: 7150 { 7151 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7152 if (e == error_mark_node) 7153 return error_mark_node; 7154 return make_pointer_declarator (type, e); 7155 } 7156 7157 case ADDR_EXPR: 7158 { 7159 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7160 if (e == error_mark_node) 7161 return error_mark_node; 7162 return make_reference_declarator (type, e); 7163 } 7164 7165 case ARRAY_REF: 7166 { 7167 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7168 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); 7169 if (e1 == error_mark_node || e2 == error_mark_node) 7170 return error_mark_node; 7171 7172 return build_nt (ARRAY_REF, e1, e2, tsubst_expr); 7173 } 7174 7175 case CALL_EXPR: 7176 { 7177 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7178 tree e2 = (tsubst_call_declarator_parms 7179 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); 7180 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, 7181 complain, in_decl); 7182 7183 if (e1 == error_mark_node || e2 == error_mark_node 7184 || e3 == error_mark_node) 7185 return error_mark_node; 7186 7187 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); 7188 } 7189 7190 case SCOPE_REF: 7191 { 7192 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 7193 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); 7194 if (e1 == error_mark_node || e2 == error_mark_node) 7195 return error_mark_node; 7196 7197 return build_nt (TREE_CODE (t), e1, e2); 7198 } 7199 7200 case TYPEOF_TYPE: 7201 { 7202 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl); 7203 if (e1 == error_mark_node) 7204 return error_mark_node; 7205 7206 return cp_build_qualified_type_real (TREE_TYPE (e1), 7207 cp_type_quals (t) 7208 | cp_type_quals (TREE_TYPE (e1)), 7209 complain); 7210 } 7211 7212 default: 7213 sorry ("use of `%s' in template", 7214 tree_code_name [(int) TREE_CODE (t)]); 7215 return error_mark_node; 7216 } 7217} 7218 7219/* Like tsubst, but deals with expressions. This function just replaces 7220 template parms; to finish processing the resultant expression, use 7221 tsubst_expr. */ 7222 7223tree 7224tsubst_copy (t, args, complain, in_decl) 7225 tree t, args; 7226 tsubst_flags_t complain; 7227 tree in_decl; 7228{ 7229 enum tree_code code; 7230 tree r; 7231 7232 if (t == NULL_TREE || t == error_mark_node) 7233 return t; 7234 7235 code = TREE_CODE (t); 7236 7237 switch (code) 7238 { 7239 case PARM_DECL: 7240 return do_identifier (DECL_NAME (t), 0, NULL_TREE); 7241 7242 case CONST_DECL: 7243 { 7244 tree enum_type; 7245 tree v; 7246 7247 if (!DECL_CONTEXT (t)) 7248 /* This is a global enumeration constant. */ 7249 return t; 7250 7251 /* Unfortunately, we cannot just call lookup_name here. 7252 Consider: 7253 7254 template <int I> int f() { 7255 enum E { a = I }; 7256 struct S { void g() { E e = a; } }; 7257 }; 7258 7259 When we instantiate f<7>::S::g(), say, lookup_name is not 7260 clever enough to find f<7>::a. */ 7261 enum_type 7262 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, 7263 /*entering_scope=*/0); 7264 7265 for (v = TYPE_VALUES (enum_type); 7266 v != NULL_TREE; 7267 v = TREE_CHAIN (v)) 7268 if (TREE_PURPOSE (v) == DECL_NAME (t)) 7269 return TREE_VALUE (v); 7270 7271 /* We didn't find the name. That should never happen; if 7272 name-lookup found it during preliminary parsing, we 7273 should find it again here during instantiation. */ 7274 abort (); 7275 } 7276 return t; 7277 7278 case FIELD_DECL: 7279 if (DECL_CONTEXT (t)) 7280 { 7281 tree ctx; 7282 7283 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, 7284 /*entering_scope=*/1); 7285 if (ctx != DECL_CONTEXT (t)) 7286 return lookup_field (ctx, DECL_NAME (t), 0, 0); 7287 } 7288 return t; 7289 7290 case VAR_DECL: 7291 case FUNCTION_DECL: 7292 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) 7293 t = tsubst (t, args, complain, in_decl); 7294 mark_used (t); 7295 return t; 7296 7297 case TEMPLATE_DECL: 7298 if (is_member_template (t)) 7299 return tsubst (t, args, complain, in_decl); 7300 else 7301 return t; 7302 7303 case LOOKUP_EXPR: 7304 { 7305 /* We must tsubst into a LOOKUP_EXPR in case the names to 7306 which it refers is a conversion operator; in that case the 7307 name will change. We avoid making unnecessary copies, 7308 however. */ 7309 7310 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); 7311 7312 if (id != TREE_OPERAND (t, 0)) 7313 { 7314 r = build_nt (LOOKUP_EXPR, id); 7315 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); 7316 t = r; 7317 } 7318 7319 return t; 7320 } 7321 7322 case CAST_EXPR: 7323 case REINTERPRET_CAST_EXPR: 7324 case CONST_CAST_EXPR: 7325 case STATIC_CAST_EXPR: 7326 case DYNAMIC_CAST_EXPR: 7327 case NOP_EXPR: 7328 return build1 7329 (code, tsubst (TREE_TYPE (t), args, complain, in_decl), 7330 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); 7331 7332 case INDIRECT_REF: 7333 case NEGATE_EXPR: 7334 case TRUTH_NOT_EXPR: 7335 case BIT_NOT_EXPR: 7336 case ADDR_EXPR: 7337 case CONVERT_EXPR: /* Unary + */ 7338 case SIZEOF_EXPR: 7339 case ALIGNOF_EXPR: 7340 case ARROW_EXPR: 7341 case THROW_EXPR: 7342 case TYPEID_EXPR: 7343 case REALPART_EXPR: 7344 case IMAGPART_EXPR: 7345 return build1 7346 (code, tsubst (TREE_TYPE (t), args, complain, in_decl), 7347 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); 7348 7349 case PLUS_EXPR: 7350 case MINUS_EXPR: 7351 case MULT_EXPR: 7352 case TRUNC_DIV_EXPR: 7353 case CEIL_DIV_EXPR: 7354 case FLOOR_DIV_EXPR: 7355 case ROUND_DIV_EXPR: 7356 case EXACT_DIV_EXPR: 7357 case BIT_AND_EXPR: 7358 case BIT_ANDTC_EXPR: 7359 case BIT_IOR_EXPR: 7360 case BIT_XOR_EXPR: 7361 case TRUNC_MOD_EXPR: 7362 case FLOOR_MOD_EXPR: 7363 case TRUTH_ANDIF_EXPR: 7364 case TRUTH_ORIF_EXPR: 7365 case TRUTH_AND_EXPR: 7366 case TRUTH_OR_EXPR: 7367 case RSHIFT_EXPR: 7368 case LSHIFT_EXPR: 7369 case RROTATE_EXPR: 7370 case LROTATE_EXPR: 7371 case EQ_EXPR: 7372 case NE_EXPR: 7373 case MAX_EXPR: 7374 case MIN_EXPR: 7375 case LE_EXPR: 7376 case GE_EXPR: 7377 case LT_EXPR: 7378 case GT_EXPR: 7379 case COMPONENT_REF: 7380 case ARRAY_REF: 7381 case COMPOUND_EXPR: 7382 case SCOPE_REF: 7383 case DOTSTAR_EXPR: 7384 case MEMBER_REF: 7385 case PREDECREMENT_EXPR: 7386 case PREINCREMENT_EXPR: 7387 case POSTDECREMENT_EXPR: 7388 case POSTINCREMENT_EXPR: 7389 return build_nt 7390 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 7391 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); 7392 7393 case CALL_EXPR: 7394 { 7395 tree fn = TREE_OPERAND (t, 0); 7396 if (is_overloaded_fn (fn)) 7397 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); 7398 else 7399 /* Sometimes FN is a LOOKUP_EXPR. */ 7400 fn = tsubst_copy (fn, args, complain, in_decl); 7401 return build_nt 7402 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, 7403 in_decl), 7404 NULL_TREE); 7405 } 7406 7407 case METHOD_CALL_EXPR: 7408 { 7409 tree name = TREE_OPERAND (t, 0); 7410 if (TREE_CODE (name) == BIT_NOT_EXPR) 7411 { 7412 name = tsubst_copy (TREE_OPERAND (name, 0), args, 7413 complain, in_decl); 7414 name = build1 (BIT_NOT_EXPR, NULL_TREE, name); 7415 } 7416 else if (TREE_CODE (name) == SCOPE_REF 7417 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) 7418 { 7419 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, 7420 complain, in_decl); 7421 name = TREE_OPERAND (TREE_OPERAND (name, 1), 0); 7422 if (TREE_CODE (name) == TYPE_DECL) 7423 name = TREE_TYPE (name); 7424 name = tsubst_copy (name, args, complain, in_decl); 7425 name = build1 (BIT_NOT_EXPR, NULL_TREE, name); 7426 name = build_nt (SCOPE_REF, base, name); 7427 } 7428 else 7429 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); 7430 return build_nt 7431 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, 7432 complain, in_decl), 7433 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), 7434 NULL_TREE); 7435 } 7436 7437 case STMT_EXPR: 7438 /* This processing should really occur in tsubst_expr, However, 7439 tsubst_expr does not recurse into expressions, since it 7440 assumes that there aren't any statements inside them. 7441 Instead, it simply calls build_expr_from_tree. So, we need 7442 to expand the STMT_EXPR here. */ 7443 if (!processing_template_decl) 7444 { 7445 tree stmt_expr = begin_stmt_expr (); 7446 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl); 7447 return finish_stmt_expr (stmt_expr); 7448 } 7449 7450 return t; 7451 7452 case COND_EXPR: 7453 case MODOP_EXPR: 7454 case PSEUDO_DTOR_EXPR: 7455 { 7456 r = build_nt 7457 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 7458 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), 7459 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); 7460 return r; 7461 } 7462 7463 case NEW_EXPR: 7464 { 7465 r = build_nt 7466 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 7467 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), 7468 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); 7469 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); 7470 return r; 7471 } 7472 7473 case DELETE_EXPR: 7474 { 7475 r = build_nt 7476 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 7477 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); 7478 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); 7479 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); 7480 return r; 7481 } 7482 7483 case TEMPLATE_ID_EXPR: 7484 { 7485 /* Substituted template arguments */ 7486 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, 7487 in_decl); 7488 7489 if (targs && TREE_CODE (targs) == TREE_LIST) 7490 { 7491 tree chain; 7492 for (chain = targs; chain; chain = TREE_CHAIN (chain)) 7493 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); 7494 } 7495 else if (targs) 7496 { 7497 int i; 7498 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) 7499 TREE_VEC_ELT (targs, i) 7500 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); 7501 } 7502 7503 return lookup_template_function 7504 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); 7505 } 7506 7507 case TREE_LIST: 7508 { 7509 tree purpose, value, chain; 7510 7511 if (t == void_list_node) 7512 return t; 7513 7514 purpose = TREE_PURPOSE (t); 7515 if (purpose) 7516 purpose = tsubst_copy (purpose, args, complain, in_decl); 7517 value = TREE_VALUE (t); 7518 if (value) 7519 value = tsubst_copy (value, args, complain, in_decl); 7520 chain = TREE_CHAIN (t); 7521 if (chain && chain != void_type_node) 7522 chain = tsubst_copy (chain, args, complain, in_decl); 7523 if (purpose == TREE_PURPOSE (t) 7524 && value == TREE_VALUE (t) 7525 && chain == TREE_CHAIN (t)) 7526 return t; 7527 return tree_cons (purpose, value, chain); 7528 } 7529 7530 case RECORD_TYPE: 7531 case UNION_TYPE: 7532 case ENUMERAL_TYPE: 7533 case INTEGER_TYPE: 7534 case TEMPLATE_TYPE_PARM: 7535 case TEMPLATE_TEMPLATE_PARM: 7536 case BOUND_TEMPLATE_TEMPLATE_PARM: 7537 case TEMPLATE_PARM_INDEX: 7538 case POINTER_TYPE: 7539 case REFERENCE_TYPE: 7540 case OFFSET_TYPE: 7541 case FUNCTION_TYPE: 7542 case METHOD_TYPE: 7543 case ARRAY_TYPE: 7544 case TYPENAME_TYPE: 7545 case UNBOUND_CLASS_TEMPLATE: 7546 case TYPEOF_TYPE: 7547 case TYPE_DECL: 7548 return tsubst (t, args, complain, in_decl); 7549 7550 case IDENTIFIER_NODE: 7551 if (IDENTIFIER_TYPENAME_P (t)) 7552 { 7553 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 7554 return mangle_conv_op_name_for_type (new_type); 7555 } 7556 else 7557 return t; 7558 7559 case CONSTRUCTOR: 7560 { 7561 r = build 7562 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), 7563 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, 7564 complain, in_decl)); 7565 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); 7566 return r; 7567 } 7568 7569 case VA_ARG_EXPR: 7570 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, 7571 in_decl), 7572 tsubst (TREE_TYPE (t), args, complain, in_decl)); 7573 7574 default: 7575 return t; 7576 } 7577} 7578 7579/* Like tsubst_copy, but also does semantic processing. */ 7580 7581tree 7582tsubst_expr (t, args, complain, in_decl) 7583 tree t, args; 7584 tsubst_flags_t complain; 7585 tree in_decl; 7586{ 7587 tree stmt, tmp; 7588 7589 if (t == NULL_TREE || t == error_mark_node) 7590 return t; 7591 7592 if (processing_template_decl) 7593 return tsubst_copy (t, args, complain, in_decl); 7594 7595 if (!statement_code_p (TREE_CODE (t))) 7596 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); 7597 7598 switch (TREE_CODE (t)) 7599 { 7600 case RETURN_INIT: 7601 prep_stmt (t); 7602 finish_named_return_value 7603 (TREE_OPERAND (t, 0), 7604 tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl)); 7605 break; 7606 7607 case CTOR_INITIALIZER: 7608 prep_stmt (t); 7609 finish_mem_initializers (tsubst_initializer_list 7610 (TREE_OPERAND (t, 0), args)); 7611 break; 7612 7613 case RETURN_STMT: 7614 prep_stmt (t); 7615 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t), 7616 args, complain, in_decl)); 7617 break; 7618 7619 case EXPR_STMT: 7620 prep_stmt (t); 7621 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), 7622 args, complain, in_decl)); 7623 break; 7624 7625 case USING_STMT: 7626 prep_stmt (t); 7627 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t), 7628 args, complain, in_decl)); 7629 break; 7630 7631 case DECL_STMT: 7632 { 7633 tree decl; 7634 tree init; 7635 7636 prep_stmt (t); 7637 decl = DECL_STMT_DECL (t); 7638 if (TREE_CODE (decl) == LABEL_DECL) 7639 finish_label_decl (DECL_NAME (decl)); 7640 else if (TREE_CODE (decl) == USING_DECL) 7641 { 7642 tree scope = DECL_INITIAL (decl); 7643 tree name = DECL_NAME (decl); 7644 7645 scope = tsubst_expr (scope, args, complain, in_decl); 7646 do_local_using_decl (build_nt (SCOPE_REF, scope, name)); 7647 } 7648 else 7649 { 7650 init = DECL_INITIAL (decl); 7651 decl = tsubst (decl, args, complain, in_decl); 7652 if (decl != error_mark_node) 7653 { 7654 if (init) 7655 DECL_INITIAL (decl) = error_mark_node; 7656 /* By marking the declaration as instantiated, we avoid 7657 trying to instantiate it. Since instantiate_decl can't 7658 handle local variables, and since we've already done 7659 all that needs to be done, that's the right thing to 7660 do. */ 7661 if (TREE_CODE (decl) == VAR_DECL) 7662 DECL_TEMPLATE_INSTANTIATED (decl) = 1; 7663 if (TREE_CODE (decl) == VAR_DECL 7664 && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 7665 /* Anonymous aggregates are a special case. */ 7666 finish_anon_union (decl); 7667 else 7668 { 7669 maybe_push_decl (decl); 7670 if (DECL_PRETTY_FUNCTION_P (decl)) 7671 { 7672 /* For __PRETTY_FUNCTION__ we have to adjust the 7673 initializer. */ 7674 const char *const name 7675 = cxx_printable_name (current_function_decl, 2); 7676 init = cp_fname_init (name); 7677 TREE_TYPE (decl) = TREE_TYPE (init); 7678 } 7679 else 7680 init = tsubst_expr (init, args, complain, in_decl); 7681 cp_finish_decl (decl, init, NULL_TREE, 0); 7682 } 7683 } 7684 } 7685 7686 /* A DECL_STMT can also be used as an expression, in the condition 7687 clause of an if/for/while construct. If we aren't followed by 7688 another statement, return our decl. */ 7689 if (TREE_CHAIN (t) == NULL_TREE) 7690 return decl; 7691 } 7692 break; 7693 7694 case FOR_STMT: 7695 { 7696 prep_stmt (t); 7697 7698 stmt = begin_for_stmt (); 7699 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl); 7700 finish_for_init_stmt (stmt); 7701 finish_for_cond (tsubst_expr (FOR_COND (t), 7702 args, complain, in_decl), 7703 stmt); 7704 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); 7705 finish_for_expr (tmp, stmt); 7706 tsubst_expr (FOR_BODY (t), args, complain, in_decl); 7707 finish_for_stmt (stmt); 7708 } 7709 break; 7710 7711 case WHILE_STMT: 7712 { 7713 prep_stmt (t); 7714 stmt = begin_while_stmt (); 7715 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), 7716 args, complain, in_decl), 7717 stmt); 7718 tsubst_expr (WHILE_BODY (t), args, complain, in_decl); 7719 finish_while_stmt (stmt); 7720 } 7721 break; 7722 7723 case DO_STMT: 7724 { 7725 prep_stmt (t); 7726 stmt = begin_do_stmt (); 7727 tsubst_expr (DO_BODY (t), args, complain, in_decl); 7728 finish_do_body (stmt); 7729 finish_do_stmt (tsubst_expr (DO_COND (t), 7730 args, complain, in_decl), 7731 stmt); 7732 } 7733 break; 7734 7735 case IF_STMT: 7736 { 7737 prep_stmt (t); 7738 stmt = begin_if_stmt (); 7739 finish_if_stmt_cond (tsubst_expr (IF_COND (t), 7740 args, complain, in_decl), 7741 stmt); 7742 7743 if (tmp = THEN_CLAUSE (t), tmp) 7744 { 7745 tsubst_expr (tmp, args, complain, in_decl); 7746 finish_then_clause (stmt); 7747 } 7748 7749 if (tmp = ELSE_CLAUSE (t), tmp) 7750 { 7751 begin_else_clause (); 7752 tsubst_expr (tmp, args, complain, in_decl); 7753 finish_else_clause (stmt); 7754 } 7755 7756 finish_if_stmt (); 7757 } 7758 break; 7759 7760 case COMPOUND_STMT: 7761 { 7762 prep_stmt (t); 7763 if (COMPOUND_STMT_BODY_BLOCK (t)) 7764 stmt = begin_function_body (); 7765 else 7766 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); 7767 7768 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl); 7769 7770 if (COMPOUND_STMT_BODY_BLOCK (t)) 7771 finish_function_body (stmt); 7772 else 7773 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); 7774 } 7775 break; 7776 7777 case BREAK_STMT: 7778 prep_stmt (t); 7779 finish_break_stmt (); 7780 break; 7781 7782 case CONTINUE_STMT: 7783 prep_stmt (t); 7784 finish_continue_stmt (); 7785 break; 7786 7787 case SWITCH_STMT: 7788 { 7789 tree val; 7790 7791 prep_stmt (t); 7792 stmt = begin_switch_stmt (); 7793 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); 7794 finish_switch_cond (val, stmt); 7795 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); 7796 finish_switch_stmt (stmt); 7797 } 7798 break; 7799 7800 case CASE_LABEL: 7801 prep_stmt (t); 7802 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), 7803 tsubst_expr (CASE_HIGH (t), args, complain, 7804 in_decl)); 7805 break; 7806 7807 case LABEL_STMT: 7808 lineno = STMT_LINENO (t); 7809 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); 7810 break; 7811 7812 case FILE_STMT: 7813 input_filename = FILE_STMT_FILENAME (t); 7814 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t))); 7815 break; 7816 7817 case GOTO_STMT: 7818 prep_stmt (t); 7819 tmp = GOTO_DESTINATION (t); 7820 if (TREE_CODE (tmp) != LABEL_DECL) 7821 /* Computed goto's must be tsubst'd into. On the other hand, 7822 non-computed gotos must not be; the identifier in question 7823 will have no binding. */ 7824 tmp = tsubst_expr (tmp, args, complain, in_decl); 7825 else 7826 tmp = DECL_NAME (tmp); 7827 finish_goto_stmt (tmp); 7828 break; 7829 7830 case ASM_STMT: 7831 prep_stmt (t); 7832 tmp = finish_asm_stmt 7833 (ASM_CV_QUAL (t), 7834 tsubst_expr (ASM_STRING (t), args, complain, in_decl), 7835 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), 7836 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), 7837 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl)); 7838 ASM_INPUT_P (tmp) = ASM_INPUT_P (t); 7839 break; 7840 7841 case TRY_BLOCK: 7842 prep_stmt (t); 7843 if (CLEANUP_P (t)) 7844 { 7845 stmt = begin_try_block (); 7846 tsubst_expr (TRY_STMTS (t), args, complain, in_decl); 7847 finish_cleanup_try_block (stmt); 7848 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, 7849 complain, in_decl), 7850 stmt); 7851 } 7852 else 7853 { 7854 if (FN_TRY_BLOCK_P (t)) 7855 stmt = begin_function_try_block (); 7856 else 7857 stmt = begin_try_block (); 7858 7859 tsubst_expr (TRY_STMTS (t), args, complain, in_decl); 7860 7861 if (FN_TRY_BLOCK_P (t)) 7862 finish_function_try_block (stmt); 7863 else 7864 finish_try_block (stmt); 7865 7866 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl); 7867 if (FN_TRY_BLOCK_P (t)) 7868 finish_function_handler_sequence (stmt); 7869 else 7870 finish_handler_sequence (stmt); 7871 } 7872 break; 7873 7874 case HANDLER: 7875 { 7876 tree decl; 7877 7878 prep_stmt (t); 7879 stmt = begin_handler (); 7880 if (HANDLER_PARMS (t)) 7881 { 7882 decl = DECL_STMT_DECL (HANDLER_PARMS (t)); 7883 decl = tsubst (decl, args, complain, in_decl); 7884 /* Prevent instantiate_decl from trying to instantiate 7885 this variable. We've already done all that needs to be 7886 done. */ 7887 DECL_TEMPLATE_INSTANTIATED (decl) = 1; 7888 } 7889 else 7890 decl = NULL_TREE; 7891 finish_handler_parms (decl, stmt); 7892 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); 7893 finish_handler (stmt); 7894 } 7895 break; 7896 7897 case TAG_DEFN: 7898 prep_stmt (t); 7899 tsubst (TREE_TYPE (t), args, complain, NULL_TREE); 7900 break; 7901 7902 default: 7903 abort (); 7904 } 7905 7906 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); 7907} 7908 7909/* Instantiate the indicated variable or function template TMPL with 7910 the template arguments in TARG_PTR. */ 7911 7912tree 7913instantiate_template (tmpl, targ_ptr) 7914 tree tmpl, targ_ptr; 7915{ 7916 tree fndecl; 7917 tree gen_tmpl; 7918 tree spec; 7919 int i, len; 7920 tree inner_args; 7921 7922 if (tmpl == error_mark_node) 7923 return error_mark_node; 7924 7925 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); 7926 7927 /* If this function is a clone, handle it specially. */ 7928 if (DECL_CLONED_FUNCTION_P (tmpl)) 7929 { 7930 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr); 7931 tree clone; 7932 7933 /* Look for the clone. */ 7934 for (clone = TREE_CHAIN (spec); 7935 clone && DECL_CLONED_FUNCTION_P (clone); 7936 clone = TREE_CHAIN (clone)) 7937 if (DECL_NAME (clone) == DECL_NAME (tmpl)) 7938 return clone; 7939 /* We should always have found the clone by now. */ 7940 abort (); 7941 return NULL_TREE; 7942 } 7943 7944 /* Check to see if we already have this specialization. */ 7945 spec = retrieve_specialization (tmpl, targ_ptr); 7946 if (spec != NULL_TREE) 7947 return spec; 7948 7949 gen_tmpl = most_general_template (tmpl); 7950 if (tmpl != gen_tmpl) 7951 { 7952 /* The TMPL is a partial instantiation. To get a full set of 7953 arguments we must add the arguments used to perform the 7954 partial instantiation. */ 7955 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), 7956 targ_ptr); 7957 7958 /* Check to see if we already have this specialization. */ 7959 spec = retrieve_specialization (gen_tmpl, targ_ptr); 7960 if (spec != NULL_TREE) 7961 return spec; 7962 } 7963 7964 len = DECL_NTPARMS (gen_tmpl); 7965 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr); 7966 i = len; 7967 while (i--) 7968 { 7969 tree t = TREE_VEC_ELT (inner_args, i); 7970 if (TYPE_P (t)) 7971 { 7972 tree nt = target_type (t); 7973 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) 7974 { 7975 error ("type `%T' composed from a local class is not a valid template-argument", t); 7976 error (" trying to instantiate `%D'", gen_tmpl); 7977 return error_mark_node; 7978 } 7979 } 7980 } 7981 7982 /* Make sure that we can see identifiers, and compute access 7983 correctly. The desired FUNCTION_DECL for FNDECL may or may not be 7984 created earlier. Let push_access_scope_real figure that out. */ 7985 push_access_scope_real 7986 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr, 7987 tf_error, gen_tmpl)); 7988 7989 /* substitute template parameters */ 7990 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), 7991 targ_ptr, tf_error, gen_tmpl); 7992 7993 pop_access_scope (gen_tmpl); 7994 7995 /* The DECL_TI_TEMPLATE should always be the immediate parent 7996 template, not the most general template. */ 7997 DECL_TI_TEMPLATE (fndecl) = tmpl; 7998 7999 if (flag_external_templates) 8000 add_pending_template (fndecl); 8001 8002 /* If we've just instantiated the main entry point for a function, 8003 instantiate all the alternate entry points as well. We do this 8004 by cloning the instantiation of the main entry point, not by 8005 instantiating the template clones. */ 8006 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) 8007 clone_function_decl (fndecl, /*update_method_vec_p=*/0); 8008 8009 return fndecl; 8010} 8011 8012/* The FN is a TEMPLATE_DECL for a function. The ARGS are the 8013 arguments that are being used when calling it. TARGS is a vector 8014 into which the deduced template arguments are placed. 8015 8016 Return zero for success, 2 for an incomplete match that doesn't resolve 8017 all the types, and 1 for complete failure. An error message will be 8018 printed only for an incomplete match. 8019 8020 If FN is a conversion operator, or we are trying to produce a specific 8021 specialization, RETURN_TYPE is the return type desired. 8022 8023 The EXPLICIT_TARGS are explicit template arguments provided via a 8024 template-id. 8025 8026 The parameter STRICT is one of: 8027 8028 DEDUCE_CALL: 8029 We are deducing arguments for a function call, as in 8030 [temp.deduct.call]. 8031 8032 DEDUCE_CONV: 8033 We are deducing arguments for a conversion function, as in 8034 [temp.deduct.conv]. 8035 8036 DEDUCE_EXACT: 8037 We are deducing arguments when doing an explicit instantiation 8038 as in [temp.explicit], when determining an explicit specialization 8039 as in [temp.expl.spec], or when taking the address of a function 8040 template, as in [temp.deduct.funcaddr]. 8041 8042 DEDUCE_ORDER: 8043 We are deducing arguments when calculating the partial 8044 ordering between specializations of function or class 8045 templates, as in [temp.func.order] and [temp.class.order]. 8046 8047 LEN is the number of parms to consider before returning success, or -1 8048 for all. This is used in partial ordering to avoid comparing parms for 8049 which no actual argument was passed, since they are not considered in 8050 overload resolution (and are explicitly excluded from consideration in 8051 partial ordering in [temp.func.order]/6). */ 8052 8053int 8054fn_type_unification (fn, explicit_targs, targs, args, return_type, 8055 strict, len) 8056 tree fn, explicit_targs, targs, args, return_type; 8057 unification_kind_t strict; 8058 int len; 8059{ 8060 tree parms; 8061 tree fntype; 8062 int result; 8063 8064 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); 8065 8066 fntype = TREE_TYPE (fn); 8067 if (explicit_targs) 8068 { 8069 /* [temp.deduct] 8070 8071 The specified template arguments must match the template 8072 parameters in kind (i.e., type, nontype, template), and there 8073 must not be more arguments than there are parameters; 8074 otherwise type deduction fails. 8075 8076 Nontype arguments must match the types of the corresponding 8077 nontype template parameters, or must be convertible to the 8078 types of the corresponding nontype parameters as specified in 8079 _temp.arg.nontype_, otherwise type deduction fails. 8080 8081 All references in the function type of the function template 8082 to the corresponding template parameters are replaced by the 8083 specified template argument values. If a substitution in a 8084 template parameter or in the function type of the function 8085 template results in an invalid type, type deduction fails. */ 8086 int i; 8087 tree converted_args; 8088 8089 converted_args 8090 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), 8091 explicit_targs, NULL_TREE, tf_none, 8092 /*require_all_arguments=*/0)); 8093 if (converted_args == error_mark_node) 8094 return 1; 8095 8096 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); 8097 if (fntype == error_mark_node) 8098 return 1; 8099 8100 /* Place the explicitly specified arguments in TARGS. */ 8101 for (i = 0; i < TREE_VEC_LENGTH (targs); i++) 8102 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); 8103 } 8104 8105 parms = TYPE_ARG_TYPES (fntype); 8106 /* Never do unification on the 'this' parameter. */ 8107 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 8108 parms = TREE_CHAIN (parms); 8109 8110 if (return_type) 8111 { 8112 /* We've been given a return type to match, prepend it. */ 8113 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); 8114 args = tree_cons (NULL_TREE, return_type, args); 8115 if (len >= 0) 8116 ++len; 8117 } 8118 8119 /* We allow incomplete unification without an error message here 8120 because the standard doesn't seem to explicitly prohibit it. Our 8121 callers must be ready to deal with unification failures in any 8122 event. */ 8123 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), 8124 targs, parms, args, /*subr=*/0, 8125 strict, /*allow_incomplete*/1, len); 8126 8127 if (result == 0) 8128 /* All is well so far. Now, check: 8129 8130 [temp.deduct] 8131 8132 When all template arguments have been deduced, all uses of 8133 template parameters in nondeduced contexts are replaced with 8134 the corresponding deduced argument values. If the 8135 substitution results in an invalid type, as described above, 8136 type deduction fails. */ 8137 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) 8138 == error_mark_node) 8139 return 1; 8140 8141 return result; 8142} 8143 8144/* Adjust types before performing type deduction, as described in 8145 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two 8146 sections are symmetric. PARM is the type of a function parameter 8147 or the return type of the conversion function. ARG is the type of 8148 the argument passed to the call, or the type of the value 8149 initialized with the result of the conversion function. */ 8150 8151static int 8152maybe_adjust_types_for_deduction (strict, parm, arg) 8153 unification_kind_t strict; 8154 tree* parm; 8155 tree* arg; 8156{ 8157 int result = 0; 8158 8159 switch (strict) 8160 { 8161 case DEDUCE_CALL: 8162 break; 8163 8164 case DEDUCE_CONV: 8165 { 8166 /* Swap PARM and ARG throughout the remainder of this 8167 function; the handling is precisely symmetric since PARM 8168 will initialize ARG rather than vice versa. */ 8169 tree* temp = parm; 8170 parm = arg; 8171 arg = temp; 8172 break; 8173 } 8174 8175 case DEDUCE_EXACT: 8176 /* There is nothing to do in this case. */ 8177 return 0; 8178 8179 case DEDUCE_ORDER: 8180 /* DR 214. [temp.func.order] is underspecified, and leads to no 8181 ordering between things like `T *' and `T const &' for `U *'. 8182 The former has T=U and the latter T=U*. The former looks more 8183 specialized and John Spicer considers it well-formed (the EDG 8184 compiler accepts it). 8185 8186 John also confirms that deduction should proceed as in a function 8187 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL. 8188 However, in ordering, ARG can have REFERENCE_TYPE, but no argument 8189 to an actual call can have such a type. 8190 8191 If both ARG and PARM are REFERENCE_TYPE, we change neither. 8192 If only ARG is a REFERENCE_TYPE, we look through that and then 8193 proceed as with DEDUCE_CALL (which could further convert it). */ 8194 if (TREE_CODE (*arg) == REFERENCE_TYPE) 8195 { 8196 if (TREE_CODE (*parm) == REFERENCE_TYPE) 8197 return 0; 8198 *arg = TREE_TYPE (*arg); 8199 } 8200 break; 8201 default: 8202 abort (); 8203 } 8204 8205 if (TREE_CODE (*parm) != REFERENCE_TYPE) 8206 { 8207 /* [temp.deduct.call] 8208 8209 If P is not a reference type: 8210 8211 --If A is an array type, the pointer type produced by the 8212 array-to-pointer standard conversion (_conv.array_) is 8213 used in place of A for type deduction; otherwise, 8214 8215 --If A is a function type, the pointer type produced by 8216 the function-to-pointer standard conversion 8217 (_conv.func_) is used in place of A for type deduction; 8218 otherwise, 8219 8220 --If A is a cv-qualified type, the top level 8221 cv-qualifiers of A's type are ignored for type 8222 deduction. */ 8223 if (TREE_CODE (*arg) == ARRAY_TYPE) 8224 *arg = build_pointer_type (TREE_TYPE (*arg)); 8225 else if (TREE_CODE (*arg) == FUNCTION_TYPE) 8226 *arg = build_pointer_type (*arg); 8227 else 8228 *arg = TYPE_MAIN_VARIANT (*arg); 8229 } 8230 8231 /* [temp.deduct.call] 8232 8233 If P is a cv-qualified type, the top level cv-qualifiers 8234 of P's type are ignored for type deduction. If P is a 8235 reference type, the type referred to by P is used for 8236 type deduction. */ 8237 *parm = TYPE_MAIN_VARIANT (*parm); 8238 if (TREE_CODE (*parm) == REFERENCE_TYPE) 8239 { 8240 *parm = TREE_TYPE (*parm); 8241 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 8242 } 8243 8244 /* DR 322. For conversion deduction, remove a reference type on parm 8245 too (which has been swapped into ARG). */ 8246 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) 8247 *arg = TREE_TYPE (*arg); 8248 8249 return result; 8250} 8251 8252/* Most parms like fn_type_unification. 8253 8254 If SUBR is 1, we're being called recursively (to unify the 8255 arguments of a function or method parameter of a function 8256 template). */ 8257 8258static int 8259type_unification_real (tparms, targs, xparms, xargs, subr, 8260 strict, allow_incomplete, xlen) 8261 tree tparms, targs, xparms, xargs; 8262 int subr; 8263 unification_kind_t strict; 8264 int allow_incomplete, xlen; 8265{ 8266 tree parm, arg; 8267 int i; 8268 int ntparms = TREE_VEC_LENGTH (tparms); 8269 int sub_strict; 8270 int saw_undeduced = 0; 8271 tree parms, args; 8272 int len; 8273 8274 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); 8275 my_friendly_assert (xparms == NULL_TREE 8276 || TREE_CODE (xparms) == TREE_LIST, 290); 8277 /* ARGS could be NULL. */ 8278 if (xargs) 8279 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291); 8280 my_friendly_assert (ntparms > 0, 292); 8281 8282 switch (strict) 8283 { 8284 case DEDUCE_CALL: 8285 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL 8286 | UNIFY_ALLOW_DERIVED); 8287 break; 8288 8289 case DEDUCE_CONV: 8290 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; 8291 break; 8292 8293 case DEDUCE_EXACT: 8294 sub_strict = UNIFY_ALLOW_NONE; 8295 break; 8296 8297 case DEDUCE_ORDER: 8298 sub_strict = UNIFY_ALLOW_NONE; 8299 break; 8300 8301 default: 8302 abort (); 8303 } 8304 8305 if (xlen == 0) 8306 return 0; 8307 8308 again: 8309 parms = xparms; 8310 args = xargs; 8311 len = xlen; 8312 8313 while (parms 8314 && parms != void_list_node 8315 && args 8316 && args != void_list_node) 8317 { 8318 parm = TREE_VALUE (parms); 8319 parms = TREE_CHAIN (parms); 8320 arg = TREE_VALUE (args); 8321 args = TREE_CHAIN (args); 8322 8323 if (arg == error_mark_node) 8324 return 1; 8325 if (arg == unknown_type_node) 8326 /* We can't deduce anything from this, but we might get all the 8327 template args from other function args. */ 8328 continue; 8329 8330 /* Conversions will be performed on a function argument that 8331 corresponds with a function parameter that contains only 8332 non-deducible template parameters and explicitly specified 8333 template parameters. */ 8334 if (! uses_template_parms (parm)) 8335 { 8336 tree type; 8337 8338 if (!TYPE_P (arg)) 8339 type = TREE_TYPE (arg); 8340 else 8341 { 8342 type = arg; 8343 arg = NULL_TREE; 8344 } 8345 8346 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER) 8347 { 8348 if (same_type_p (parm, type)) 8349 continue; 8350 } 8351 else 8352 /* It might work; we shouldn't check now, because we might 8353 get into infinite recursion. Overload resolution will 8354 handle it. */ 8355 continue; 8356 8357 return 1; 8358 } 8359 8360 if (!TYPE_P (arg)) 8361 { 8362 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); 8363 if (type_unknown_p (arg)) 8364 { 8365 /* [temp.deduct.type] A template-argument can be deduced from 8366 a pointer to function or pointer to member function 8367 argument if the set of overloaded functions does not 8368 contain function templates and at most one of a set of 8369 overloaded functions provides a unique match. */ 8370 8371 if (resolve_overloaded_unification 8372 (tparms, targs, parm, arg, strict, sub_strict) 8373 != 0) 8374 return 1; 8375 continue; 8376 } 8377 arg = TREE_TYPE (arg); 8378 } 8379 8380 { 8381 int arg_strict = sub_strict; 8382 8383 if (!subr) 8384 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); 8385 8386 if (unify (tparms, targs, parm, arg, arg_strict)) 8387 return 1; 8388 } 8389 8390 /* Are we done with the interesting parms? */ 8391 if (--len == 0) 8392 goto done; 8393 } 8394 /* Fail if we've reached the end of the parm list, and more args 8395 are present, and the parm list isn't variadic. */ 8396 if (args && args != void_list_node && parms == void_list_node) 8397 return 1; 8398 /* Fail if parms are left and they don't have default values. */ 8399 if (parms 8400 && parms != void_list_node 8401 && TREE_PURPOSE (parms) == NULL_TREE) 8402 return 1; 8403 8404 done: 8405 if (!subr) 8406 for (i = 0; i < ntparms; i++) 8407 if (TREE_VEC_ELT (targs, i) == NULL_TREE) 8408 { 8409 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); 8410 8411 /* If this is an undeduced nontype parameter that depends on 8412 a type parameter, try another pass; its type may have been 8413 deduced from a later argument than the one from which 8414 this parameter can be deduced. */ 8415 if (TREE_CODE (tparm) == PARM_DECL 8416 && uses_template_parms (TREE_TYPE (tparm)) 8417 && !saw_undeduced++) 8418 goto again; 8419 8420 if (!allow_incomplete) 8421 error ("incomplete type unification"); 8422 return 2; 8423 } 8424 return 0; 8425} 8426 8427/* Subroutine of type_unification_real. Args are like the variables at the 8428 call site. ARG is an overloaded function (or template-id); we try 8429 deducing template args from each of the overloads, and if only one 8430 succeeds, we go with that. Modifies TARGS and returns 0 on success. */ 8431 8432static int 8433resolve_overloaded_unification (tparms, targs, parm, arg, strict, 8434 sub_strict) 8435 tree tparms, targs, parm, arg; 8436 unification_kind_t strict; 8437 int sub_strict; 8438{ 8439 tree tempargs = copy_node (targs); 8440 int good = 0; 8441 bool addr_p; 8442 8443 if (TREE_CODE (arg) == ADDR_EXPR) 8444 { 8445 arg = TREE_OPERAND (arg, 0); 8446 addr_p = true; 8447 } 8448 else 8449 addr_p = false; 8450 8451 if (TREE_CODE (arg) == COMPONENT_REF) 8452 /* Handle `&x' where `x' is some static or non-static member 8453 function name. */ 8454 arg = TREE_OPERAND (arg, 1); 8455 8456 if (TREE_CODE (arg) == OFFSET_REF) 8457 arg = TREE_OPERAND (arg, 1); 8458 8459 /* Strip baselink information. */ 8460 if (BASELINK_P (arg)) 8461 arg = BASELINK_FUNCTIONS (arg); 8462 8463 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) 8464 { 8465 /* If we got some explicit template args, we need to plug them into 8466 the affected templates before we try to unify, in case the 8467 explicit args will completely resolve the templates in question. */ 8468 8469 tree expl_subargs = TREE_OPERAND (arg, 1); 8470 arg = TREE_OPERAND (arg, 0); 8471 8472 for (; arg; arg = OVL_NEXT (arg)) 8473 { 8474 tree fn = OVL_CURRENT (arg); 8475 tree subargs, elem; 8476 8477 if (TREE_CODE (fn) != TEMPLATE_DECL) 8478 continue; 8479 8480 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn), 8481 expl_subargs); 8482 if (subargs) 8483 { 8484 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); 8485 good += try_one_overload (tparms, targs, tempargs, parm, 8486 elem, strict, sub_strict, addr_p); 8487 } 8488 } 8489 } 8490 else if (TREE_CODE (arg) == OVERLOAD 8491 || TREE_CODE (arg) == FUNCTION_DECL) 8492 { 8493 for (; arg; arg = OVL_NEXT (arg)) 8494 good += try_one_overload (tparms, targs, tempargs, parm, 8495 TREE_TYPE (OVL_CURRENT (arg)), 8496 strict, sub_strict, addr_p); 8497 } 8498 else 8499 abort (); 8500 8501 /* [temp.deduct.type] A template-argument can be deduced from a pointer 8502 to function or pointer to member function argument if the set of 8503 overloaded functions does not contain function templates and at most 8504 one of a set of overloaded functions provides a unique match. 8505 8506 So if we found multiple possibilities, we return success but don't 8507 deduce anything. */ 8508 8509 if (good == 1) 8510 { 8511 int i = TREE_VEC_LENGTH (targs); 8512 for (; i--; ) 8513 if (TREE_VEC_ELT (tempargs, i)) 8514 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); 8515 } 8516 if (good) 8517 return 0; 8518 8519 return 1; 8520} 8521 8522/* Subroutine of resolve_overloaded_unification; does deduction for a single 8523 overload. Fills TARGS with any deduced arguments, or error_mark_node if 8524 different overloads deduce different arguments for a given parm. 8525 ADDR_P is true if the expression for which deduction is being 8526 performed was of the form "& fn" rather than simply "fn". 8527 8528 Returns 1 on success. */ 8529 8530static int 8531try_one_overload (tree tparms, 8532 tree orig_targs, 8533 tree targs, 8534 tree parm, 8535 tree arg, 8536 unification_kind_t strict, 8537 int sub_strict, 8538 bool addr_p) 8539{ 8540 int nargs; 8541 tree tempargs; 8542 int i; 8543 8544 /* [temp.deduct.type] A template-argument can be deduced from a pointer 8545 to function or pointer to member function argument if the set of 8546 overloaded functions does not contain function templates and at most 8547 one of a set of overloaded functions provides a unique match. 8548 8549 So if this is a template, just return success. */ 8550 8551 if (uses_template_parms (arg)) 8552 return 1; 8553 8554 if (TREE_CODE (arg) == METHOD_TYPE) 8555 arg = build_ptrmemfunc_type (build_pointer_type (arg)); 8556 else if (addr_p) 8557 arg = build_pointer_type (arg); 8558 8559 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); 8560 8561 /* We don't copy orig_targs for this because if we have already deduced 8562 some template args from previous args, unify would complain when we 8563 try to deduce a template parameter for the same argument, even though 8564 there isn't really a conflict. */ 8565 nargs = TREE_VEC_LENGTH (targs); 8566 tempargs = make_tree_vec (nargs); 8567 8568 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) 8569 return 0; 8570 8571 /* First make sure we didn't deduce anything that conflicts with 8572 explicitly specified args. */ 8573 for (i = nargs; i--; ) 8574 { 8575 tree elt = TREE_VEC_ELT (tempargs, i); 8576 tree oldelt = TREE_VEC_ELT (orig_targs, i); 8577 8578 if (elt == NULL_TREE) 8579 continue; 8580 else if (uses_template_parms (elt)) 8581 { 8582 /* Since we're unifying against ourselves, we will fill in template 8583 args used in the function parm list with our own template parms. 8584 Discard them. */ 8585 TREE_VEC_ELT (tempargs, i) = NULL_TREE; 8586 continue; 8587 } 8588 else if (oldelt && ! template_args_equal (oldelt, elt)) 8589 return 0; 8590 } 8591 8592 for (i = nargs; i--; ) 8593 { 8594 tree elt = TREE_VEC_ELT (tempargs, i); 8595 8596 if (elt) 8597 TREE_VEC_ELT (targs, i) = elt; 8598 } 8599 8600 return 1; 8601} 8602 8603/* Verify that nondeduce template argument agrees with the type 8604 obtained from argument deduction. Return nonzero if the 8605 verification fails. 8606 8607 For example: 8608 8609 struct A { typedef int X; }; 8610 template <class T, class U> struct C {}; 8611 template <class T> struct C<T, typename T::X> {}; 8612 8613 Then with the instantiation `C<A, int>', we can deduce that 8614 `T' is `A' but unify () does not check whether `typename T::X' 8615 is `int'. This function ensure that they agree. 8616 8617 TARGS, PARMS are the same as the arguments of unify. 8618 ARGS contains template arguments from all levels. */ 8619 8620static int 8621verify_class_unification (targs, parms, args) 8622 tree targs, parms, args; 8623{ 8624 parms = tsubst (parms, add_outermost_template_args (args, targs), 8625 tf_none, NULL_TREE); 8626 if (parms == error_mark_node) 8627 return 1; 8628 8629 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args)); 8630} 8631 8632/* PARM is a template class (perhaps with unbound template 8633 parameters). ARG is a fully instantiated type. If ARG can be 8634 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and 8635 TARGS are as for unify. */ 8636 8637static tree 8638try_class_unification (tparms, targs, parm, arg) 8639 tree tparms; 8640 tree targs; 8641 tree parm; 8642 tree arg; 8643{ 8644 tree copy_of_targs; 8645 8646 if (!CLASSTYPE_TEMPLATE_INFO (arg) 8647 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) 8648 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) 8649 return NULL_TREE; 8650 8651 /* We need to make a new template argument vector for the call to 8652 unify. If we used TARGS, we'd clutter it up with the result of 8653 the attempted unification, even if this class didn't work out. 8654 We also don't want to commit ourselves to all the unifications 8655 we've already done, since unification is supposed to be done on 8656 an argument-by-argument basis. In other words, consider the 8657 following pathological case: 8658 8659 template <int I, int J, int K> 8660 struct S {}; 8661 8662 template <int I, int J> 8663 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; 8664 8665 template <int I, int J, int K> 8666 void f(S<I, J, K>, S<I, I, I>); 8667 8668 void g() { 8669 S<0, 0, 0> s0; 8670 S<0, 1, 2> s2; 8671 8672 f(s0, s2); 8673 } 8674 8675 Now, by the time we consider the unification involving `s2', we 8676 already know that we must have `f<0, 0, 0>'. But, even though 8677 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid 8678 because there are two ways to unify base classes of S<0, 1, 2> 8679 with S<I, I, I>. If we kept the already deduced knowledge, we 8680 would reject the possibility I=1. */ 8681 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); 8682 8683 /* If unification failed, we're done. */ 8684 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), 8685 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) 8686 return NULL_TREE; 8687 8688 return arg; 8689} 8690 8691/* Subroutine of get_template_base. RVAL, if non-NULL, is a base we 8692 have already discovered to be satisfactory. ARG_BINFO is the binfo 8693 for the base class of ARG that we are currently examining. */ 8694 8695static tree 8696get_template_base_recursive (tparms, targs, parm, 8697 arg_binfo, rval, flags) 8698 tree tparms; 8699 tree targs; 8700 tree arg_binfo; 8701 tree rval; 8702 tree parm; 8703 int flags; 8704{ 8705 tree binfos; 8706 int i, n_baselinks; 8707 tree arg = BINFO_TYPE (arg_binfo); 8708 8709 if (!(flags & GTB_IGNORE_TYPE)) 8710 { 8711 tree r = try_class_unification (tparms, targs, 8712 parm, arg); 8713 8714 /* If there is more than one satisfactory baseclass, then: 8715 8716 [temp.deduct.call] 8717 8718 If they yield more than one possible deduced A, the type 8719 deduction fails. 8720 8721 applies. */ 8722 if (r && rval && !same_type_p (r, rval)) 8723 return error_mark_node; 8724 else if (r) 8725 rval = r; 8726 } 8727 8728 binfos = BINFO_BASETYPES (arg_binfo); 8729 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; 8730 8731 /* Process base types. */ 8732 for (i = 0; i < n_baselinks; i++) 8733 { 8734 tree base_binfo = TREE_VEC_ELT (binfos, i); 8735 int this_virtual; 8736 8737 /* Skip this base, if we've already seen it. */ 8738 if (BINFO_MARKED (base_binfo)) 8739 continue; 8740 8741 this_virtual = 8742 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); 8743 8744 /* When searching for a non-virtual, we cannot mark virtually 8745 found binfos. */ 8746 if (! this_virtual) 8747 SET_BINFO_MARKED (base_binfo); 8748 8749 rval = get_template_base_recursive (tparms, targs, 8750 parm, 8751 base_binfo, 8752 rval, 8753 GTB_VIA_VIRTUAL * this_virtual); 8754 8755 /* If we discovered more than one matching base class, we can 8756 stop now. */ 8757 if (rval == error_mark_node) 8758 return error_mark_node; 8759 } 8760 8761 return rval; 8762} 8763 8764/* Given a template type PARM and a class type ARG, find the unique 8765 base type in ARG that is an instance of PARM. We do not examine 8766 ARG itself; only its base-classes. If there is no appropriate base 8767 class, return NULL_TREE. If there is more than one, return 8768 error_mark_node. PARM may be the type of a partial specialization, 8769 as well as a plain template type. Used by unify. */ 8770 8771static tree 8772get_template_base (tparms, targs, parm, arg) 8773 tree tparms; 8774 tree targs; 8775 tree parm; 8776 tree arg; 8777{ 8778 tree rval; 8779 tree arg_binfo; 8780 8781 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); 8782 8783 arg_binfo = TYPE_BINFO (complete_type (arg)); 8784 rval = get_template_base_recursive (tparms, targs, 8785 parm, arg_binfo, 8786 NULL_TREE, 8787 GTB_IGNORE_TYPE); 8788 8789 /* Since get_template_base_recursive marks the bases classes, we 8790 must unmark them here. */ 8791 dfs_walk (arg_binfo, dfs_unmark, markedp, 0); 8792 8793 return rval; 8794} 8795 8796/* Returns the level of DECL, which declares a template parameter. */ 8797 8798static int 8799template_decl_level (decl) 8800 tree decl; 8801{ 8802 switch (TREE_CODE (decl)) 8803 { 8804 case TYPE_DECL: 8805 case TEMPLATE_DECL: 8806 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); 8807 8808 case PARM_DECL: 8809 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); 8810 8811 default: 8812 abort (); 8813 return 0; 8814 } 8815} 8816 8817/* Decide whether ARG can be unified with PARM, considering only the 8818 cv-qualifiers of each type, given STRICT as documented for unify. 8819 Returns nonzero iff the unification is OK on that basis.*/ 8820 8821static int 8822check_cv_quals_for_unify (strict, arg, parm) 8823 int strict; 8824 tree arg; 8825 tree parm; 8826{ 8827 int arg_quals = cp_type_quals (arg); 8828 int parm_quals = cp_type_quals (parm); 8829 8830 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM) 8831 { 8832 /* If the cvr quals of parm will not unify with ARG, they'll be 8833 ignored in instantiation, so we have to do the same here. */ 8834 if (TREE_CODE (arg) == REFERENCE_TYPE) 8835 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 8836 if (!POINTER_TYPE_P (arg) && 8837 TREE_CODE (arg) != TEMPLATE_TYPE_PARM) 8838 parm_quals &= ~TYPE_QUAL_RESTRICT; 8839 } 8840 8841 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) 8842 && (arg_quals & parm_quals) != parm_quals) 8843 return 0; 8844 8845 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) 8846 && (parm_quals & arg_quals) != arg_quals) 8847 return 0; 8848 8849 return 1; 8850} 8851 8852/* Takes parameters as for type_unification. Returns 0 if the 8853 type deduction succeeds, 1 otherwise. The parameter STRICT is a 8854 bitwise or of the following flags: 8855 8856 UNIFY_ALLOW_NONE: 8857 Require an exact match between PARM and ARG. 8858 UNIFY_ALLOW_MORE_CV_QUAL: 8859 Allow the deduced ARG to be more cv-qualified (by qualification 8860 conversion) than ARG. 8861 UNIFY_ALLOW_LESS_CV_QUAL: 8862 Allow the deduced ARG to be less cv-qualified than ARG. 8863 UNIFY_ALLOW_DERIVED: 8864 Allow the deduced ARG to be a template base class of ARG, 8865 or a pointer to a template base class of the type pointed to by 8866 ARG. 8867 UNIFY_ALLOW_INTEGER: 8868 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX 8869 case for more information. 8870 UNIFY_ALLOW_OUTER_LEVEL: 8871 This is the outermost level of a deduction. Used to determine validity 8872 of qualification conversions. A valid qualification conversion must 8873 have const qualified pointers leading up to the inner type which 8874 requires additional CV quals, except at the outer level, where const 8875 is not required [conv.qual]. It would be normal to set this flag in 8876 addition to setting UNIFY_ALLOW_MORE_CV_QUAL. 8877 UNIFY_ALLOW_OUTER_MORE_CV_QUAL: 8878 This is the outermost level of a deduction, and PARM can be more CV 8879 qualified at this point. 8880 UNIFY_ALLOW_OUTER_LESS_CV_QUAL: 8881 This is the outermost level of a deduction, and PARM can be less CV 8882 qualified at this point. 8883 UNIFY_ALLOW_MAX_CORRECTION: 8884 This is an INTEGER_TYPE's maximum value. Used if the range may 8885 have been derived from a size specification, such as an array size. 8886 If the size was given by a nontype template parameter N, the maximum 8887 value will have the form N-1. The flag says that we can (and indeed 8888 must) unify N with (ARG + 1), an exception to the normal rules on 8889 folding PARM. */ 8890 8891static int 8892unify (tparms, targs, parm, arg, strict) 8893 tree tparms, targs, parm, arg; 8894 int strict; 8895{ 8896 int idx; 8897 tree targ; 8898 tree tparm; 8899 int strict_in = strict; 8900 8901 /* I don't think this will do the right thing with respect to types. 8902 But the only case I've seen it in so far has been array bounds, where 8903 signedness is the only information lost, and I think that will be 8904 okay. */ 8905 while (TREE_CODE (parm) == NOP_EXPR) 8906 parm = TREE_OPERAND (parm, 0); 8907 8908 if (arg == error_mark_node) 8909 return 1; 8910 if (arg == unknown_type_node) 8911 /* We can't deduce anything from this, but we might get all the 8912 template args from other function args. */ 8913 return 0; 8914 8915 /* If PARM uses template parameters, then we can't bail out here, 8916 even if ARG == PARM, since we won't record unifications for the 8917 template parameters. We might need them if we're trying to 8918 figure out which of two things is more specialized. */ 8919 if (arg == parm && !uses_template_parms (parm)) 8920 return 0; 8921 8922 /* Immediately reject some pairs that won't unify because of 8923 cv-qualification mismatches. */ 8924 if (TREE_CODE (arg) == TREE_CODE (parm) 8925 && TYPE_P (arg) 8926 /* It is the elements of the array which hold the cv quals of an array 8927 type, and the elements might be template type parms. We'll check 8928 when we recurse. */ 8929 && TREE_CODE (arg) != ARRAY_TYPE 8930 /* We check the cv-qualifiers when unifying with template type 8931 parameters below. We want to allow ARG `const T' to unify with 8932 PARM `T' for example, when computing which of two templates 8933 is more specialized, for example. */ 8934 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM 8935 && !check_cv_quals_for_unify (strict_in, arg, parm)) 8936 return 1; 8937 8938 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) 8939 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) 8940 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; 8941 strict &= ~UNIFY_ALLOW_OUTER_LEVEL; 8942 strict &= ~UNIFY_ALLOW_DERIVED; 8943 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 8944 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; 8945 strict &= ~UNIFY_ALLOW_MAX_CORRECTION; 8946 8947 switch (TREE_CODE (parm)) 8948 { 8949 case TYPENAME_TYPE: 8950 case SCOPE_REF: 8951 case UNBOUND_CLASS_TEMPLATE: 8952 /* In a type which contains a nested-name-specifier, template 8953 argument values cannot be deduced for template parameters used 8954 within the nested-name-specifier. */ 8955 return 0; 8956 8957 case TEMPLATE_TYPE_PARM: 8958 case TEMPLATE_TEMPLATE_PARM: 8959 case BOUND_TEMPLATE_TEMPLATE_PARM: 8960 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 8961 8962 if (TEMPLATE_TYPE_LEVEL (parm) 8963 != template_decl_level (tparm)) 8964 /* The PARM is not one we're trying to unify. Just check 8965 to see if it matches ARG. */ 8966 return (TREE_CODE (arg) == TREE_CODE (parm) 8967 && same_type_p (parm, arg)) ? 0 : 1; 8968 idx = TEMPLATE_TYPE_IDX (parm); 8969 targ = TREE_VEC_ELT (targs, idx); 8970 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); 8971 8972 /* Check for mixed types and values. */ 8973 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM 8974 && TREE_CODE (tparm) != TYPE_DECL) 8975 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 8976 && TREE_CODE (tparm) != TEMPLATE_DECL)) 8977 return 1; 8978 8979 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 8980 { 8981 /* ARG must be constructed from a template class or a template 8982 template parameter. */ 8983 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM 8984 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))) 8985 return 1; 8986 8987 { 8988 tree parmtmpl = TYPE_TI_TEMPLATE (parm); 8989 tree parmvec = TYPE_TI_ARGS (parm); 8990 tree argvec = TYPE_TI_ARGS (arg); 8991 tree argtmplvec 8992 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); 8993 int i; 8994 8995 /* The parameter and argument roles have to be switched here 8996 in order to handle default arguments properly. For example, 8997 template<template <class> class TT> void f(TT<int>) 8998 should be able to accept vector<int> which comes from 8999 template <class T, class Allocator = allocator> 9000 class vector. */ 9001 9002 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) 9003 == error_mark_node) 9004 return 1; 9005 9006 /* Deduce arguments T, i from TT<T> or TT<i>. 9007 We check each element of PARMVEC and ARGVEC individually 9008 rather than the whole TREE_VEC since they can have 9009 different number of elements. */ 9010 9011 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) 9012 { 9013 tree t = TREE_VEC_ELT (parmvec, i); 9014 9015 if (unify (tparms, targs, t, 9016 TREE_VEC_ELT (argvec, i), 9017 UNIFY_ALLOW_NONE)) 9018 return 1; 9019 } 9020 } 9021 arg = TYPE_TI_TEMPLATE (arg); 9022 9023 /* Fall through to deduce template name. */ 9024 } 9025 9026 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 9027 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 9028 { 9029 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ 9030 9031 /* Simple cases: Value already set, does match or doesn't. */ 9032 if (targ != NULL_TREE && template_args_equal (targ, arg)) 9033 return 0; 9034 else if (targ) 9035 return 1; 9036 } 9037 else 9038 { 9039 /* If ARG is an offset type, we're trying to unify '*T' with 9040 'U C::*', which is ill-formed. See the comment in the 9041 POINTER_TYPE case about this ugliness. */ 9042 if (TREE_CODE (arg) == OFFSET_TYPE) 9043 return 1; 9044 9045 /* If PARM is `const T' and ARG is only `int', we don't have 9046 a match unless we are allowing additional qualification. 9047 If ARG is `const int' and PARM is just `T' that's OK; 9048 that binds `const int' to `T'. */ 9049 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, 9050 arg, parm)) 9051 return 1; 9052 9053 /* Consider the case where ARG is `const volatile int' and 9054 PARM is `const T'. Then, T should be `volatile int'. */ 9055 arg = cp_build_qualified_type_real 9056 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); 9057 if (arg == error_mark_node) 9058 return 1; 9059 9060 /* Simple cases: Value already set, does match or doesn't. */ 9061 if (targ != NULL_TREE && same_type_p (targ, arg)) 9062 return 0; 9063 else if (targ) 9064 return 1; 9065 9066 /* Make sure that ARG is not a variable-sized array. (Note 9067 that were talking about variable-sized arrays (like 9068 `int[n]'), rather than arrays of unknown size (like 9069 `int[]').) We'll get very confused by such a type since 9070 the bound of the array will not be computable in an 9071 instantiation. Besides, such types are not allowed in 9072 ISO C++, so we can do as we please here. */ 9073 if (variably_modified_type_p (arg)) 9074 return 1; 9075 } 9076 9077 TREE_VEC_ELT (targs, idx) = arg; 9078 return 0; 9079 9080 case TEMPLATE_PARM_INDEX: 9081 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 9082 9083 if (TEMPLATE_PARM_LEVEL (parm) 9084 != template_decl_level (tparm)) 9085 /* The PARM is not one we're trying to unify. Just check 9086 to see if it matches ARG. */ 9087 return (TREE_CODE (arg) == TREE_CODE (parm) 9088 && cp_tree_equal (parm, arg) > 0) ? 0 : 1; 9089 9090 idx = TEMPLATE_PARM_IDX (parm); 9091 targ = TREE_VEC_ELT (targs, idx); 9092 9093 if (targ) 9094 { 9095 int i = (cp_tree_equal (targ, arg) > 0); 9096 if (i == 1) 9097 return 0; 9098 else if (i == 0) 9099 return 1; 9100 else 9101 abort (); 9102 } 9103 9104 /* [temp.deduct.type] If, in the declaration of a function template 9105 with a non-type template-parameter, the non-type 9106 template-parameter is used in an expression in the function 9107 parameter-list and, if the corresponding template-argument is 9108 deduced, the template-argument type shall match the type of the 9109 template-parameter exactly, except that a template-argument 9110 deduced from an array bound may be of any integral type. 9111 The non-type parameter might use already deduced type parameters. */ 9112 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); 9113 if (!TREE_TYPE (arg)) 9114 /* Template-parameter dependent expression. Just accept it for now. 9115 It will later be processed in convert_template_argument. */ 9116 ; 9117 else if (same_type_p (TREE_TYPE (arg), tparm)) 9118 /* OK */; 9119 else if ((strict & UNIFY_ALLOW_INTEGER) 9120 && (TREE_CODE (tparm) == INTEGER_TYPE 9121 || TREE_CODE (tparm) == BOOLEAN_TYPE)) 9122 /* OK */; 9123 else if (uses_template_parms (tparm)) 9124 /* We haven't deduced the type of this parameter yet. Try again 9125 later. */ 9126 return 0; 9127 else 9128 return 1; 9129 9130 TREE_VEC_ELT (targs, idx) = arg; 9131 return 0; 9132 9133 case POINTER_TYPE: 9134 { 9135 if (TREE_CODE (arg) != POINTER_TYPE) 9136 return 1; 9137 9138 /* [temp.deduct.call] 9139 9140 A can be another pointer or pointer to member type that can 9141 be converted to the deduced A via a qualification 9142 conversion (_conv.qual_). 9143 9144 We pass down STRICT here rather than UNIFY_ALLOW_NONE. 9145 This will allow for additional cv-qualification of the 9146 pointed-to types if appropriate. */ 9147 9148 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) 9149 /* The derived-to-base conversion only persists through one 9150 level of pointers. */ 9151 strict |= (strict_in & UNIFY_ALLOW_DERIVED); 9152 9153 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE 9154 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) 9155 { 9156 /* Avoid getting confused about cv-quals; don't recurse here. 9157 Pointers to members should really be just OFFSET_TYPE, not 9158 this two-level nonsense... */ 9159 9160 parm = TREE_TYPE (parm); 9161 arg = TREE_TYPE (arg); 9162 goto offset; 9163 } 9164 9165 return unify (tparms, targs, TREE_TYPE (parm), 9166 TREE_TYPE (arg), strict); 9167 } 9168 9169 case REFERENCE_TYPE: 9170 if (TREE_CODE (arg) != REFERENCE_TYPE) 9171 return 1; 9172 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 9173 strict & UNIFY_ALLOW_MORE_CV_QUAL); 9174 9175 case ARRAY_TYPE: 9176 if (TREE_CODE (arg) != ARRAY_TYPE) 9177 return 1; 9178 if ((TYPE_DOMAIN (parm) == NULL_TREE) 9179 != (TYPE_DOMAIN (arg) == NULL_TREE)) 9180 return 1; 9181 if (TYPE_DOMAIN (parm) != NULL_TREE 9182 && unify (tparms, targs, TYPE_DOMAIN (parm), 9183 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) 9184 return 1; 9185 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 9186 strict & UNIFY_ALLOW_MORE_CV_QUAL); 9187 9188 case REAL_TYPE: 9189 case COMPLEX_TYPE: 9190 case VECTOR_TYPE: 9191 case INTEGER_TYPE: 9192 case BOOLEAN_TYPE: 9193 case VOID_TYPE: 9194 if (TREE_CODE (arg) != TREE_CODE (parm)) 9195 return 1; 9196 9197 if (TREE_CODE (parm) == INTEGER_TYPE 9198 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) 9199 { 9200 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) 9201 && unify (tparms, targs, TYPE_MIN_VALUE (parm), 9202 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) 9203 return 1; 9204 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) 9205 && unify (tparms, targs, TYPE_MAX_VALUE (parm), 9206 TYPE_MAX_VALUE (arg), 9207 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION)) 9208 return 1; 9209 } 9210 /* We have already checked cv-qualification at the top of the 9211 function. */ 9212 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) 9213 return 1; 9214 9215 /* As far as unification is concerned, this wins. Later checks 9216 will invalidate it if necessary. */ 9217 return 0; 9218 9219 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ 9220 /* Type INTEGER_CST can come from ordinary constant template args. */ 9221 case INTEGER_CST: 9222 while (TREE_CODE (arg) == NOP_EXPR) 9223 arg = TREE_OPERAND (arg, 0); 9224 9225 if (TREE_CODE (arg) != INTEGER_CST) 9226 return 1; 9227 return !tree_int_cst_equal (parm, arg); 9228 9229 case TREE_VEC: 9230 { 9231 int i; 9232 if (TREE_CODE (arg) != TREE_VEC) 9233 return 1; 9234 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) 9235 return 1; 9236 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) 9237 if (unify (tparms, targs, 9238 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), 9239 UNIFY_ALLOW_NONE)) 9240 return 1; 9241 return 0; 9242 } 9243 9244 case RECORD_TYPE: 9245 case UNION_TYPE: 9246 if (TREE_CODE (arg) != TREE_CODE (parm)) 9247 return 1; 9248 9249 if (TYPE_PTRMEMFUNC_P (parm)) 9250 { 9251 if (!TYPE_PTRMEMFUNC_P (arg)) 9252 return 1; 9253 9254 return unify (tparms, targs, 9255 TYPE_PTRMEMFUNC_FN_TYPE (parm), 9256 TYPE_PTRMEMFUNC_FN_TYPE (arg), 9257 strict); 9258 } 9259 9260 if (CLASSTYPE_TEMPLATE_INFO (parm)) 9261 { 9262 tree t = NULL_TREE; 9263 9264 if (strict_in & UNIFY_ALLOW_DERIVED) 9265 { 9266 /* First, we try to unify the PARM and ARG directly. */ 9267 t = try_class_unification (tparms, targs, 9268 parm, arg); 9269 9270 if (!t) 9271 { 9272 /* Fallback to the special case allowed in 9273 [temp.deduct.call]: 9274 9275 If P is a class, and P has the form 9276 template-id, then A can be a derived class of 9277 the deduced A. Likewise, if P is a pointer to 9278 a class of the form template-id, A can be a 9279 pointer to a derived class pointed to by the 9280 deduced A. */ 9281 t = get_template_base (tparms, targs, 9282 parm, arg); 9283 9284 if (! t || t == error_mark_node) 9285 return 1; 9286 } 9287 } 9288 else if (CLASSTYPE_TEMPLATE_INFO (arg) 9289 && (CLASSTYPE_TI_TEMPLATE (parm) 9290 == CLASSTYPE_TI_TEMPLATE (arg))) 9291 /* Perhaps PARM is something like S<U> and ARG is S<int>. 9292 Then, we should unify `int' and `U'. */ 9293 t = arg; 9294 else 9295 /* There's no chance of unification succeeding. */ 9296 return 1; 9297 9298 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), 9299 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); 9300 } 9301 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) 9302 return 1; 9303 return 0; 9304 9305 case METHOD_TYPE: 9306 case FUNCTION_TYPE: 9307 if (TREE_CODE (arg) != TREE_CODE (parm)) 9308 return 1; 9309 9310 if (unify (tparms, targs, TREE_TYPE (parm), 9311 TREE_TYPE (arg), UNIFY_ALLOW_NONE)) 9312 return 1; 9313 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), 9314 TYPE_ARG_TYPES (arg), 1, 9315 DEDUCE_EXACT, 0, -1); 9316 9317 case OFFSET_TYPE: 9318 offset: 9319 if (TREE_CODE (arg) != OFFSET_TYPE) 9320 return 1; 9321 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), 9322 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) 9323 return 1; 9324 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 9325 strict); 9326 9327 case CONST_DECL: 9328 if (arg != decl_constant_value (parm)) 9329 return 1; 9330 return 0; 9331 9332 case TEMPLATE_DECL: 9333 /* Matched cases are handled by the ARG == PARM test above. */ 9334 return 1; 9335 9336 case MINUS_EXPR: 9337 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node) 9338 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION)) 9339 { 9340 /* We handle this case specially, since it comes up with 9341 arrays. In particular, something like: 9342 9343 template <int N> void f(int (&x)[N]); 9344 9345 Here, we are trying to unify the range type, which 9346 looks like [0 ... (N - 1)]. */ 9347 tree t, t1, t2; 9348 t1 = TREE_OPERAND (parm, 0); 9349 t2 = TREE_OPERAND (parm, 1); 9350 9351 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2)); 9352 9353 return unify (tparms, targs, t1, t, strict); 9354 } 9355 /* else fall through */ 9356 9357 default: 9358 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) 9359 { 9360 9361 /* We're looking at an expression. This can happen with 9362 something like: 9363 9364 template <int I> 9365 void foo(S<I>, S<I + 2>); 9366 9367 This is a "nondeduced context": 9368 9369 [deduct.type] 9370 9371 The nondeduced contexts are: 9372 9373 --A type that is a template-id in which one or more of 9374 the template-arguments is an expression that references 9375 a template-parameter. 9376 9377 In these cases, we assume deduction succeeded, but don't 9378 actually infer any unifications. */ 9379 9380 if (!uses_template_parms (parm) 9381 && !template_args_equal (parm, arg)) 9382 return 1; 9383 else 9384 return 0; 9385 } 9386 else 9387 sorry ("use of `%s' in template type unification", 9388 tree_code_name [(int) TREE_CODE (parm)]); 9389 9390 return 1; 9391 } 9392} 9393 9394/* Called if RESULT is explicitly instantiated, or is a member of an 9395 explicitly instantiated class, or if using -frepo and the 9396 instantiation of RESULT has been assigned to this file. */ 9397 9398void 9399mark_decl_instantiated (result, extern_p) 9400 tree result; 9401 int extern_p; 9402{ 9403 /* We used to set this unconditionally; we moved that to 9404 do_decl_instantiation so it wouldn't get set on members of 9405 explicit class template instantiations. But we still need to set 9406 it here for the 'extern template' case in order to suppress 9407 implicit instantiations. */ 9408 if (extern_p) 9409 SET_DECL_EXPLICIT_INSTANTIATION (result); 9410 9411 /* If this entity has already been written out, it's too late to 9412 make any modifications. */ 9413 if (TREE_ASM_WRITTEN (result)) 9414 return; 9415 9416 if (TREE_CODE (result) != FUNCTION_DECL) 9417 /* The TREE_PUBLIC flag for function declarations will have been 9418 set correctly by tsubst. */ 9419 TREE_PUBLIC (result) = 1; 9420 9421 /* This might have been set by an earlier implicit instantiation. */ 9422 DECL_COMDAT (result) = 0; 9423 9424 if (! extern_p) 9425 { 9426 DECL_INTERFACE_KNOWN (result) = 1; 9427 DECL_NOT_REALLY_EXTERN (result) = 1; 9428 9429 /* Always make artificials weak. */ 9430 if (DECL_ARTIFICIAL (result) && flag_weak) 9431 comdat_linkage (result); 9432 /* For WIN32 we also want to put explicit instantiations in 9433 linkonce sections. */ 9434 else if (TREE_PUBLIC (result)) 9435 maybe_make_one_only (result); 9436 } 9437 9438 if (TREE_CODE (result) == FUNCTION_DECL) 9439 defer_fn (result); 9440} 9441 9442/* Given two function templates PAT1 and PAT2, return: 9443 9444 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER. 9445 9446 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. 9447 -1 if PAT2 is more specialized than PAT1. 9448 0 if neither is more specialized. 9449 9450 LEN is passed through to fn_type_unification. */ 9451 9452int 9453more_specialized (pat1, pat2, deduce, len) 9454 tree pat1, pat2; 9455 int deduce; 9456 int len; 9457{ 9458 tree targs; 9459 int winner = 0; 9460 9461 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2), 9462 NULL_TREE, 0, deduce, len); 9463 if (targs) 9464 --winner; 9465 9466 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1), 9467 NULL_TREE, 0, deduce, len); 9468 if (targs) 9469 ++winner; 9470 9471 return winner; 9472} 9473 9474/* Given two class template specialization list nodes PAT1 and PAT2, return: 9475 9476 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. 9477 -1 if PAT2 is more specialized than PAT1. 9478 0 if neither is more specialized. 9479 9480 FULL_ARGS is the full set of template arguments that triggers this 9481 partial ordering. */ 9482 9483int 9484more_specialized_class (pat1, pat2, full_args) 9485 tree pat1, pat2, full_args; 9486{ 9487 tree targs; 9488 int winner = 0; 9489 9490 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), 9491 add_outermost_template_args (full_args, TREE_PURPOSE (pat2))); 9492 if (targs) 9493 --winner; 9494 9495 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), 9496 add_outermost_template_args (full_args, TREE_PURPOSE (pat1))); 9497 if (targs) 9498 ++winner; 9499 9500 return winner; 9501} 9502 9503/* Return the template arguments that will produce the function signature 9504 DECL from the function template FN, with the explicit template 9505 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must 9506 also match. Return NULL_TREE if no satisfactory arguments could be 9507 found. DEDUCE and LEN are passed through to fn_type_unification. */ 9508 9509static tree 9510get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len) 9511 tree fn, decl, explicit_args; 9512 int check_rettype, deduce, len; 9513{ 9514 int ntparms = DECL_NTPARMS (fn); 9515 tree targs = make_tree_vec (ntparms); 9516 tree decl_type; 9517 tree decl_arg_types; 9518 int i; 9519 9520 /* Substitute the explicit template arguments into the type of DECL. 9521 The call to fn_type_unification will handle substitution into the 9522 FN. */ 9523 decl_type = TREE_TYPE (decl); 9524 if (explicit_args && uses_template_parms (decl_type)) 9525 { 9526 tree tmpl; 9527 tree converted_args; 9528 9529 if (DECL_TEMPLATE_INFO (decl)) 9530 tmpl = DECL_TI_TEMPLATE (decl); 9531 else 9532 /* We can get here for some invalid specializations. */ 9533 return NULL_TREE; 9534 9535 converted_args 9536 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), 9537 explicit_args, NULL_TREE, 9538 tf_none, /*require_all_arguments=*/0)); 9539 if (converted_args == error_mark_node) 9540 return NULL_TREE; 9541 9542 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); 9543 if (decl_type == error_mark_node) 9544 return NULL_TREE; 9545 } 9546 9547 decl_arg_types = TYPE_ARG_TYPES (decl_type); 9548 /* Never do unification on the 'this' parameter. */ 9549 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 9550 decl_arg_types = TREE_CHAIN (decl_arg_types); 9551 9552 i = fn_type_unification (fn, explicit_args, targs, 9553 decl_arg_types, 9554 (check_rettype || DECL_CONV_FN_P (fn) 9555 ? TREE_TYPE (decl_type) : NULL_TREE), 9556 deduce, len); 9557 9558 if (i != 0) 9559 return NULL_TREE; 9560 9561 return targs; 9562} 9563 9564/* For most uses, we want to check the return type. */ 9565 9566tree 9567get_bindings (fn, decl, explicit_args) 9568 tree fn, decl, explicit_args; 9569{ 9570 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1); 9571} 9572 9573/* But for resolve_overloaded_unification, we only care about the parameter 9574 types. */ 9575 9576static tree 9577get_bindings_overload (fn, decl, explicit_args) 9578 tree fn, decl, explicit_args; 9579{ 9580 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1); 9581} 9582 9583/* Return the innermost template arguments that, when applied to a 9584 template specialization whose innermost template parameters are 9585 TPARMS, and whose specialization arguments are PARMS, yield the 9586 ARGS. 9587 9588 For example, suppose we have: 9589 9590 template <class T, class U> struct S {}; 9591 template <class T> struct S<T*, int> {}; 9592 9593 Then, suppose we want to get `S<double*, int>'. The TPARMS will be 9594 {T}, the PARMS will be {T*, int} and the ARGS will be {double*, 9595 int}. The resulting vector will be {double}, indicating that `T' 9596 is bound to `double'. */ 9597 9598static tree 9599get_class_bindings (tparms, parms, args) 9600 tree tparms, parms, args; 9601{ 9602 int i, ntparms = TREE_VEC_LENGTH (tparms); 9603 tree vec = make_tree_vec (ntparms); 9604 9605 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args), 9606 UNIFY_ALLOW_NONE)) 9607 return NULL_TREE; 9608 9609 for (i = 0; i < ntparms; ++i) 9610 if (! TREE_VEC_ELT (vec, i)) 9611 return NULL_TREE; 9612 9613 if (verify_class_unification (vec, parms, args)) 9614 return NULL_TREE; 9615 9616 return vec; 9617} 9618 9619/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. 9620 Pick the most specialized template, and return the corresponding 9621 instantiation, or if there is no corresponding instantiation, the 9622 template itself. If there is no most specialized template, 9623 error_mark_node is returned. If there are no templates at all, 9624 NULL_TREE is returned. */ 9625 9626tree 9627most_specialized_instantiation (instantiations) 9628 tree instantiations; 9629{ 9630 tree fn, champ; 9631 int fate; 9632 9633 if (!instantiations) 9634 return NULL_TREE; 9635 9636 champ = instantiations; 9637 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) 9638 { 9639 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), 9640 DEDUCE_EXACT, -1); 9641 if (fate == 1) 9642 ; 9643 else 9644 { 9645 if (fate == 0) 9646 { 9647 fn = TREE_CHAIN (fn); 9648 if (! fn) 9649 return error_mark_node; 9650 } 9651 champ = fn; 9652 } 9653 } 9654 9655 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) 9656 { 9657 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), 9658 DEDUCE_EXACT, -1); 9659 if (fate != 1) 9660 return error_mark_node; 9661 } 9662 9663 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); 9664} 9665 9666/* Return the most specialized of the list of templates in FNS that can 9667 produce an instantiation matching DECL, given the explicit template 9668 arguments EXPLICIT_ARGS. */ 9669 9670static tree 9671most_specialized (fns, decl, explicit_args) 9672 tree fns, decl, explicit_args; 9673{ 9674 tree candidates = NULL_TREE; 9675 tree fn, args; 9676 9677 for (fn = fns; fn; fn = TREE_CHAIN (fn)) 9678 { 9679 tree candidate = TREE_VALUE (fn); 9680 9681 args = get_bindings (candidate, decl, explicit_args); 9682 if (args) 9683 candidates = tree_cons (NULL_TREE, candidate, candidates); 9684 } 9685 9686 return most_specialized_instantiation (candidates); 9687} 9688 9689/* If DECL is a specialization of some template, return the most 9690 general such template. Otherwise, returns NULL_TREE. 9691 9692 For example, given: 9693 9694 template <class T> struct S { template <class U> void f(U); }; 9695 9696 if TMPL is `template <class U> void S<int>::f(U)' this will return 9697 the full template. This function will not trace past partial 9698 specializations, however. For example, given in addition: 9699 9700 template <class T> struct S<T*> { template <class U> void f(U); }; 9701 9702 if TMPL is `template <class U> void S<int*>::f(U)' this will return 9703 `template <class T> template <class U> S<T*>::f(U)'. */ 9704 9705tree 9706most_general_template (decl) 9707 tree decl; 9708{ 9709 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is 9710 an immediate specialization. */ 9711 if (TREE_CODE (decl) == FUNCTION_DECL) 9712 { 9713 if (DECL_TEMPLATE_INFO (decl)) { 9714 decl = DECL_TI_TEMPLATE (decl); 9715 9716 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a 9717 template friend. */ 9718 if (TREE_CODE (decl) != TEMPLATE_DECL) 9719 return NULL_TREE; 9720 } else 9721 return NULL_TREE; 9722 } 9723 9724 /* Look for more and more general templates. */ 9725 while (DECL_TEMPLATE_INFO (decl)) 9726 { 9727 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE 9728 in some cases. (See cp-tree.h for details.) */ 9729 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 9730 break; 9731 9732 if (CLASS_TYPE_P (TREE_TYPE (decl)) 9733 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) 9734 break; 9735 9736 /* Stop if we run into an explicitly specialized class template. */ 9737 if (!DECL_NAMESPACE_SCOPE_P (decl) 9738 && DECL_CONTEXT (decl) 9739 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) 9740 break; 9741 9742 decl = DECL_TI_TEMPLATE (decl); 9743 } 9744 9745 return decl; 9746} 9747 9748/* Return the most specialized of the class template specializations 9749 of TMPL which can produce an instantiation matching ARGS, or 9750 error_mark_node if the choice is ambiguous. */ 9751 9752static tree 9753most_specialized_class (tmpl, args) 9754 tree tmpl; 9755 tree args; 9756{ 9757 tree list = NULL_TREE; 9758 tree t; 9759 tree champ; 9760 int fate; 9761 9762 tmpl = most_general_template (tmpl); 9763 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) 9764 { 9765 tree spec_args 9766 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); 9767 if (spec_args) 9768 { 9769 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); 9770 TREE_TYPE (list) = TREE_TYPE (t); 9771 } 9772 } 9773 9774 if (! list) 9775 return NULL_TREE; 9776 9777 t = list; 9778 champ = t; 9779 t = TREE_CHAIN (t); 9780 for (; t; t = TREE_CHAIN (t)) 9781 { 9782 fate = more_specialized_class (champ, t, args); 9783 if (fate == 1) 9784 ; 9785 else 9786 { 9787 if (fate == 0) 9788 { 9789 t = TREE_CHAIN (t); 9790 if (! t) 9791 return error_mark_node; 9792 } 9793 champ = t; 9794 } 9795 } 9796 9797 for (t = list; t && t != champ; t = TREE_CHAIN (t)) 9798 { 9799 fate = more_specialized_class (champ, t, args); 9800 if (fate != 1) 9801 return error_mark_node; 9802 } 9803 9804 return champ; 9805} 9806 9807/* Explicitly instantiate DECL. */ 9808 9809void 9810do_decl_instantiation (tree decl, tree storage) 9811{ 9812 tree result = NULL_TREE; 9813 int extern_p = 0; 9814 9815 if (!decl) 9816 /* An error occurred, for which grokdeclarator has already issued 9817 an appropriate message. */ 9818 return; 9819 else if (! DECL_LANG_SPECIFIC (decl)) 9820 { 9821 error ("explicit instantiation of non-template `%#D'", decl); 9822 return; 9823 } 9824 else if (TREE_CODE (decl) == VAR_DECL) 9825 { 9826 /* There is an asymmetry here in the way VAR_DECLs and 9827 FUNCTION_DECLs are handled by grokdeclarator. In the case of 9828 the latter, the DECL we get back will be marked as a 9829 template instantiation, and the appropriate 9830 DECL_TEMPLATE_INFO will be set up. This does not happen for 9831 VAR_DECLs so we do the lookup here. Probably, grokdeclarator 9832 should handle VAR_DECLs as it currently handles 9833 FUNCTION_DECLs. */ 9834 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); 9835 if (result && TREE_CODE (result) != VAR_DECL) 9836 { 9837 error ("no matching template for `%D' found", result); 9838 return; 9839 } 9840 } 9841 else if (TREE_CODE (decl) != FUNCTION_DECL) 9842 { 9843 error ("explicit instantiation of `%#D'", decl); 9844 return; 9845 } 9846 else 9847 result = decl; 9848 9849 /* Check for various error cases. Note that if the explicit 9850 instantiation is valid the RESULT will currently be marked as an 9851 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set 9852 until we get here. */ 9853 9854 if (DECL_TEMPLATE_SPECIALIZATION (result)) 9855 { 9856 /* DR 259 [temp.spec]. 9857 9858 Both an explicit instantiation and a declaration of an explicit 9859 specialization shall not appear in a program unless the explicit 9860 instantiation follows a declaration of the explicit specialization. 9861 9862 For a given set of template parameters, if an explicit 9863 instantiation of a template appears after a declaration of an 9864 explicit specialization for that template, the explicit 9865 instantiation has no effect. */ 9866 return; 9867 } 9868 else if (DECL_EXPLICIT_INSTANTIATION (result)) 9869 { 9870 /* [temp.spec] 9871 9872 No program shall explicitly instantiate any template more 9873 than once. 9874 9875 We check DECL_INTERFACE_KNOWN so as not to complain when the first 9876 instantiation was `extern' and the second is not, and EXTERN_P for 9877 the opposite case. If -frepo, chances are we already got marked 9878 as an explicit instantiation because of the repo file. */ 9879 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) 9880 pedwarn ("duplicate explicit instantiation of `%#D'", result); 9881 9882 /* If we've already instantiated the template, just return now. */ 9883 if (DECL_INTERFACE_KNOWN (result)) 9884 return; 9885 } 9886 else if (!DECL_IMPLICIT_INSTANTIATION (result)) 9887 { 9888 error ("no matching template for `%D' found", result); 9889 return; 9890 } 9891 else if (!DECL_TEMPLATE_INFO (result)) 9892 { 9893 pedwarn ("explicit instantiation of non-template `%#D'", result); 9894 return; 9895 } 9896 9897 if (flag_external_templates) 9898 return; 9899 9900 if (storage == NULL_TREE) 9901 ; 9902 else if (storage == ridpointers[(int) RID_EXTERN]) 9903 { 9904 if (pedantic && !in_system_header) 9905 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations"); 9906 extern_p = 1; 9907 } 9908 else 9909 error ("storage class `%D' applied to template instantiation", 9910 storage); 9911 9912 SET_DECL_EXPLICIT_INSTANTIATION (result); 9913 mark_decl_instantiated (result, extern_p); 9914 repo_template_instantiated (result, extern_p); 9915 if (! extern_p) 9916 instantiate_decl (result, /*defer_ok=*/1); 9917} 9918 9919void 9920mark_class_instantiated (t, extern_p) 9921 tree t; 9922 int extern_p; 9923{ 9924 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); 9925 SET_CLASSTYPE_INTERFACE_KNOWN (t); 9926 CLASSTYPE_INTERFACE_ONLY (t) = extern_p; 9927 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; 9928 if (! extern_p) 9929 { 9930 CLASSTYPE_DEBUG_REQUESTED (t) = 1; 9931 rest_of_type_compilation (t, 1); 9932 } 9933} 9934 9935/* Called from do_type_instantiation through binding_table_foreach to 9936 do recursive instantiation for the type bound in ENTRY. */ 9937static void 9938bt_instantiate_type_proc (binding_entry entry, void *data) 9939{ 9940 tree storage = *(tree *) data; 9941 9942 if (IS_AGGR_TYPE (entry->type) 9943 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) 9944 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); 9945} 9946 9947/* Perform an explicit instantiation of template class T. STORAGE, if 9948 non-null, is the RID for extern, inline or static. COMPLAIN is 9949 nonzero if this is called from the parser, zero if called recursively, 9950 since the standard is unclear (as detailed below). */ 9951 9952void 9953do_type_instantiation (t, storage, complain) 9954 tree t, storage; 9955 tsubst_flags_t complain; 9956{ 9957 int extern_p = 0; 9958 int nomem_p = 0; 9959 int static_p = 0; 9960 9961 if (TREE_CODE (t) == TYPE_DECL) 9962 t = TREE_TYPE (t); 9963 9964 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) 9965 { 9966 error ("explicit instantiation of non-template type `%T'", t); 9967 return; 9968 } 9969 9970 complete_type (t); 9971 9972 /* With -fexternal-templates, explicit instantiations are treated the same 9973 as implicit ones. */ 9974 if (flag_external_templates) 9975 return; 9976 9977 if (!COMPLETE_TYPE_P (t)) 9978 { 9979 if (complain & tf_error) 9980 error ("explicit instantiation of `%#T' before definition of template", 9981 t); 9982 return; 9983 } 9984 9985 if (storage != NULL_TREE) 9986 { 9987 if (pedantic && !in_system_header) 9988 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations", 9989 IDENTIFIER_POINTER (storage)); 9990 9991 if (storage == ridpointers[(int) RID_INLINE]) 9992 nomem_p = 1; 9993 else if (storage == ridpointers[(int) RID_EXTERN]) 9994 extern_p = 1; 9995 else if (storage == ridpointers[(int) RID_STATIC]) 9996 static_p = 1; 9997 else 9998 { 9999 error ("storage class `%D' applied to template instantiation", 10000 storage); 10001 extern_p = 0; 10002 } 10003 } 10004 10005 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) 10006 { 10007 /* DR 259 [temp.spec]. 10008 10009 Both an explicit instantiation and a declaration of an explicit 10010 specialization shall not appear in a program unless the explicit 10011 instantiation follows a declaration of the explicit specialization. 10012 10013 For a given set of template parameters, if an explicit 10014 instantiation of a template appears after a declaration of an 10015 explicit specialization for that template, the explicit 10016 instantiation has no effect. */ 10017 return; 10018 } 10019 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) 10020 { 10021 /* [temp.spec] 10022 10023 No program shall explicitly instantiate any template more 10024 than once. 10025 10026 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation 10027 was `extern'. If EXTERN_P then the second is. If -frepo, chances 10028 are we already got marked as an explicit instantiation because of the 10029 repo file. All these cases are OK. */ 10030 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository 10031 && (complain & tf_error)) 10032 pedwarn ("duplicate explicit instantiation of `%#T'", t); 10033 10034 /* If we've already instantiated the template, just return now. */ 10035 if (!CLASSTYPE_INTERFACE_ONLY (t)) 10036 return; 10037 } 10038 10039 mark_class_instantiated (t, extern_p); 10040 repo_template_instantiated (t, extern_p); 10041 10042 if (nomem_p) 10043 return; 10044 10045 { 10046 tree tmp; 10047 10048 /* In contrast to implicit instantiation, where only the 10049 declarations, and not the definitions, of members are 10050 instantiated, we have here: 10051 10052 [temp.explicit] 10053 10054 The explicit instantiation of a class template specialization 10055 implies the instantiation of all of its members not 10056 previously explicitly specialized in the translation unit 10057 containing the explicit instantiation. 10058 10059 Of course, we can't instantiate member template classes, since 10060 we don't have any arguments for them. Note that the standard 10061 is unclear on whether the instantiation of the members are 10062 *explicit* instantiations or not. We choose to be generous, 10063 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow 10064 the explicit instantiation of a class where some of the members 10065 have no definition in the current translation unit. */ 10066 10067 if (! static_p) 10068 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) 10069 if (TREE_CODE (tmp) == FUNCTION_DECL 10070 && DECL_TEMPLATE_INSTANTIATION (tmp)) 10071 { 10072 mark_decl_instantiated (tmp, extern_p); 10073 repo_template_instantiated (tmp, extern_p); 10074 if (! extern_p) 10075 instantiate_decl (tmp, /*defer_ok=*/1); 10076 } 10077 10078 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) 10079 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) 10080 { 10081 mark_decl_instantiated (tmp, extern_p); 10082 repo_template_instantiated (tmp, extern_p); 10083 if (! extern_p) 10084 instantiate_decl (tmp, /*defer_ok=*/1); 10085 } 10086 10087 if (CLASSTYPE_NESTED_UDTS (t)) 10088 binding_table_foreach (CLASSTYPE_NESTED_UDTS (t), 10089 bt_instantiate_type_proc, &storage); 10090 } 10091} 10092 10093/* Given a function DECL, which is a specialization of TMPL, modify 10094 DECL to be a re-instantiation of TMPL with the same template 10095 arguments. TMPL should be the template into which tsubst'ing 10096 should occur for DECL, not the most general template. 10097 10098 One reason for doing this is a scenario like this: 10099 10100 template <class T> 10101 void f(const T&, int i); 10102 10103 void g() { f(3, 7); } 10104 10105 template <class T> 10106 void f(const T& t, const int i) { } 10107 10108 Note that when the template is first instantiated, with 10109 instantiate_template, the resulting DECL will have no name for the 10110 first parameter, and the wrong type for the second. So, when we go 10111 to instantiate the DECL, we regenerate it. */ 10112 10113static void 10114regenerate_decl_from_template (decl, tmpl) 10115 tree decl; 10116 tree tmpl; 10117{ 10118 /* The most general version of TMPL. */ 10119 tree gen_tmpl; 10120 /* The arguments used to instantiate DECL, from the most general 10121 template. */ 10122 tree args; 10123 tree code_pattern; 10124 tree new_decl; 10125 int unregistered; 10126 10127 args = DECL_TI_ARGS (decl); 10128 code_pattern = DECL_TEMPLATE_RESULT (tmpl); 10129 10130 /* Unregister the specialization so that when we tsubst we will not 10131 just return DECL. We don't have to unregister DECL from TMPL 10132 because if would only be registered there if it were a partial 10133 instantiation of a specialization, which it isn't: it's a full 10134 instantiation. */ 10135 gen_tmpl = most_general_template (tmpl); 10136 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl)); 10137 unregistered = reregister_specialization (decl, gen_tmpl, NULL_TREE); 10138 10139 /* If the DECL was not unregistered then something peculiar is 10140 happening: we created a specialization but did not call 10141 register_specialization for it. */ 10142 my_friendly_assert (unregistered, 0); 10143 10144 10145 /* Do the substitution to get the new declaration. */ 10146 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE); 10147 10148 if (TREE_CODE (decl) == VAR_DECL) 10149 { 10150 /* Set up DECL_INITIAL, since tsubst doesn't. */ 10151 if (!DECL_INITIALIZED_IN_CLASS_P (decl)) 10152 DECL_INITIAL (new_decl) = 10153 tsubst_expr (DECL_INITIAL (code_pattern), args, 10154 tf_error, DECL_TI_TEMPLATE (decl)); 10155 } 10156 else if (TREE_CODE (decl) == FUNCTION_DECL) 10157 { 10158 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the 10159 new decl. */ 10160 DECL_INITIAL (new_decl) = error_mark_node; 10161 /* And don't complain about a duplicate definition. */ 10162 DECL_INITIAL (decl) = NULL_TREE; 10163 } 10164 10165 pop_access_scope (decl); 10166 10167 /* The immediate parent of the new template is still whatever it was 10168 before, even though tsubst sets DECL_TI_TEMPLATE up as the most 10169 general template. We also reset the DECL_ASSEMBLER_NAME since 10170 tsubst always calculates the name as if the function in question 10171 were really a template instance, and sometimes, with friend 10172 functions, this is not so. See tsubst_friend_function for 10173 details. */ 10174 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); 10175 COPY_DECL_ASSEMBLER_NAME (decl, new_decl); 10176 COPY_DECL_RTL (decl, new_decl); 10177 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl); 10178 10179 /* Call duplicate decls to merge the old and new declarations. */ 10180 duplicate_decls (new_decl, decl); 10181 10182 /* Now, re-register the specialization. */ 10183 register_specialization (decl, gen_tmpl, args); 10184} 10185 10186/* Produce the definition of D, a _DECL generated from a template. If 10187 DEFER_OK is nonzero, then we don't have to actually do the 10188 instantiation now; we just have to do it sometime. */ 10189 10190tree 10191instantiate_decl (d, defer_ok) 10192 tree d; 10193 int defer_ok; 10194{ 10195 tree tmpl = DECL_TI_TEMPLATE (d); 10196 tree gen_args; 10197 tree args; 10198 tree td; 10199 tree code_pattern; 10200 tree spec; 10201 tree gen_tmpl; 10202 int pattern_defined; 10203 int line = lineno; 10204 int need_push; 10205 const char *file = input_filename; 10206 10207 /* This function should only be used to instantiate templates for 10208 functions and static member variables. */ 10209 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL 10210 || TREE_CODE (d) == VAR_DECL, 0); 10211 10212 if (TREE_CODE (d) == VAR_DECL) 10213 defer_ok = 0; 10214 10215 /* Don't instantiate cloned functions. Instead, instantiate the 10216 functions they cloned. */ 10217 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) 10218 d = DECL_CLONED_FUNCTION (d); 10219 10220 if (DECL_TEMPLATE_INSTANTIATED (d)) 10221 /* D has already been instantiated. It might seem reasonable to 10222 check whether or not D is an explicit instantiation, and, if so, 10223 stop here. But when an explicit instantiation is deferred 10224 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION 10225 is set, even though we still need to do the instantiation. */ 10226 return d; 10227 10228 /* If we already have a specialization of this declaration, then 10229 there's no reason to instantiate it. Note that 10230 retrieve_specialization gives us both instantiations and 10231 specializations, so we must explicitly check 10232 DECL_TEMPLATE_SPECIALIZATION. */ 10233 gen_tmpl = most_general_template (tmpl); 10234 gen_args = DECL_TI_ARGS (d); 10235 spec = retrieve_specialization (gen_tmpl, gen_args); 10236 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) 10237 return spec; 10238 10239 /* This needs to happen before any tsubsting. */ 10240 if (! push_tinst_level (d)) 10241 return d; 10242 10243 timevar_push (TV_PARSE); 10244 10245 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern 10246 for the instantiation. This is not always the most general 10247 template. Consider, for example: 10248 10249 template <class T> 10250 struct S { template <class U> void f(); 10251 template <> void f<int>(); }; 10252 10253 and an instantiation of S<double>::f<int>. We want TD to be the 10254 specialization S<T>::f<int>, not the more general S<T>::f<U>. */ 10255 td = tmpl; 10256 while (/* An instantiation cannot have a definition, so we need a 10257 more general template. */ 10258 DECL_TEMPLATE_INSTANTIATION (td) 10259 /* We must also deal with friend templates. Given: 10260 10261 template <class T> struct S { 10262 template <class U> friend void f() {}; 10263 }; 10264 10265 S<int>::f<U> say, is not an instantiation of S<T>::f<U>, 10266 so far as the language is concerned, but that's still 10267 where we get the pattern for the instantiation from. On 10268 other hand, if the definition comes outside the class, say: 10269 10270 template <class T> struct S { 10271 template <class U> friend void f(); 10272 }; 10273 template <class U> friend void f() {} 10274 10275 we don't need to look any further. That's what the check for 10276 DECL_INITIAL is for. */ 10277 || (TREE_CODE (d) == FUNCTION_DECL 10278 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) 10279 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)))) 10280 { 10281 /* The present template, TD, should not be a definition. If it 10282 were a definition, we should be using it! Note that we 10283 cannot restructure the loop to just keep going until we find 10284 a template with a definition, since that might go too far if 10285 a specialization was declared, but not defined. */ 10286 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL 10287 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), 10288 0); 10289 10290 /* Fetch the more general template. */ 10291 td = DECL_TI_TEMPLATE (td); 10292 } 10293 10294 code_pattern = DECL_TEMPLATE_RESULT (td); 10295 10296 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) 10297 || DECL_TEMPLATE_SPECIALIZATION (td)) 10298 /* In the case of a friend template whose definition is provided 10299 outside the class, we may have too many arguments. Drop the 10300 ones we don't need. The same is true for specializations. */ 10301 args = get_innermost_template_args 10302 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); 10303 else 10304 args = gen_args; 10305 10306 if (TREE_CODE (d) == FUNCTION_DECL) 10307 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); 10308 else 10309 pattern_defined = ! DECL_IN_AGGR_P (code_pattern); 10310 10311 lineno = DECL_SOURCE_LINE (d); 10312 input_filename = DECL_SOURCE_FILE (d); 10313 10314 if (pattern_defined) 10315 { 10316 /* Let the repository code that this template definition is 10317 available. 10318 10319 The repository doesn't need to know about cloned functions 10320 because they never actually show up in the object file. It 10321 does need to know about the clones; those are the symbols 10322 that the linker will be emitting error messages about. */ 10323 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d) 10324 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d)) 10325 { 10326 tree t; 10327 10328 for (t = TREE_CHAIN (d); 10329 t && DECL_CLONED_FUNCTION_P (t); 10330 t = TREE_CHAIN (t)) 10331 repo_template_used (t); 10332 } 10333 else 10334 repo_template_used (d); 10335 10336 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) 10337 { 10338 if (flag_alt_external_templates) 10339 { 10340 if (interface_unknown) 10341 warn_if_unknown_interface (d); 10342 } 10343 else if (DECL_INTERFACE_KNOWN (code_pattern)) 10344 { 10345 DECL_INTERFACE_KNOWN (d) = 1; 10346 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); 10347 } 10348 else 10349 warn_if_unknown_interface (code_pattern); 10350 } 10351 10352 if (at_eof) 10353 import_export_decl (d); 10354 } 10355 10356 if (!defer_ok) 10357 { 10358 /* Recheck the substitutions to obtain any warning messages 10359 about ignoring cv qualifiers. */ 10360 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); 10361 tree type = TREE_TYPE (gen); 10362 10363 /* Make sure that we can see identifiers, and compute access 10364 correctly. D is already the target FUNCTION_DECL with the 10365 right context. */ 10366 push_access_scope (d); 10367 10368 if (TREE_CODE (gen) == FUNCTION_DECL) 10369 { 10370 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d); 10371 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, 10372 tf_error | tf_warning, d); 10373 /* Don't simply tsubst the function type, as that will give 10374 duplicate warnings about poor parameter qualifications. 10375 The function arguments are the same as the decl_arguments 10376 without the top level cv qualifiers. */ 10377 type = TREE_TYPE (type); 10378 } 10379 tsubst (type, gen_args, tf_error | tf_warning, d); 10380 10381 pop_access_scope (d); 10382 } 10383 10384 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d) 10385 && DECL_INITIAL (d) == NULL_TREE) 10386 /* We should have set up DECL_INITIAL in instantiate_class_template. */ 10387 abort (); 10388 /* Reject all external templates except inline functions. */ 10389 else if (DECL_INTERFACE_KNOWN (d) 10390 && ! DECL_NOT_REALLY_EXTERN (d) 10391 && ! (TREE_CODE (d) == FUNCTION_DECL 10392 && DECL_INLINE (d))) 10393 goto out; 10394 /* Defer all other templates, unless we have been explicitly 10395 forbidden from doing so. We restore the source position here 10396 because it's used by add_pending_template. */ 10397 else if (! pattern_defined || defer_ok) 10398 { 10399 lineno = line; 10400 input_filename = file; 10401 10402 if (at_eof && !pattern_defined 10403 && DECL_EXPLICIT_INSTANTIATION (d)) 10404 /* [temp.explicit] 10405 10406 The definition of a non-exported function template, a 10407 non-exported member function template, or a non-exported 10408 member function or static data member of a class template 10409 shall be present in every translation unit in which it is 10410 explicitly instantiated. */ 10411 pedwarn 10412 ("explicit instantiation of `%D' but no definition available", d); 10413 10414 add_pending_template (d); 10415 goto out; 10416 } 10417 10418 need_push = !global_bindings_p (); 10419 if (need_push) 10420 push_to_top_level (); 10421 10422 /* Regenerate the declaration in case the template has been modified 10423 by a subsequent redeclaration. */ 10424 regenerate_decl_from_template (d, td); 10425 10426 /* We already set the file and line above. Reset them now in case 10427 they changed as a result of calling regenerate_decl_from_template. */ 10428 lineno = DECL_SOURCE_LINE (d); 10429 input_filename = DECL_SOURCE_FILE (d); 10430 10431 if (TREE_CODE (d) == VAR_DECL) 10432 { 10433 /* Clear out DECL_RTL; whatever was there before may not be right 10434 since we've reset the type of the declaration. */ 10435 SET_DECL_RTL (d, NULL_RTX); 10436 10437 DECL_IN_AGGR_P (d) = 0; 10438 import_export_decl (d); 10439 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); 10440 10441 if (DECL_EXTERNAL (d)) 10442 { 10443 /* The fact that this code is executing indicates that: 10444 10445 (1) D is a template static data member, for which a 10446 definition is available. 10447 10448 (2) An implicit or explicit instantiation has occured. 10449 10450 (3) We are not going to emit a definition of the static 10451 data member at this time. 10452 10453 This situation is peculiar, but it occurs on platforms 10454 without weak symbols when performing an implicit 10455 instantiation. There, we cannot implicitly instantiate a 10456 defined static data member in more than one translation 10457 unit, so import_export_decl marks the declaration as 10458 external; we must rely on explicit instantiation. */ 10459 } 10460 else 10461 { 10462 /* Mark D as instantiated so that recursive calls to 10463 instantiate_decl do not try to instantiate it again. */ 10464 DECL_TEMPLATE_INSTANTIATED (d) = 1; 10465 cp_finish_decl (d, 10466 (!DECL_INITIALIZED_IN_CLASS_P (d) 10467 ? DECL_INITIAL (d) : NULL_TREE), 10468 NULL_TREE, 0); 10469 } 10470 } 10471 else if (TREE_CODE (d) == FUNCTION_DECL) 10472 { 10473 htab_t saved_local_specializations; 10474 10475 /* Mark D as instantiated so that recursive calls to 10476 instantiate_decl do not try to instantiate it again. */ 10477 DECL_TEMPLATE_INSTANTIATED (d) = 1; 10478 10479 /* Save away the current list, in case we are instantiating one 10480 template from within the body of another. */ 10481 saved_local_specializations = local_specializations; 10482 10483 /* Set up the list of local specializations. */ 10484 local_specializations = htab_create (37, 10485 htab_hash_pointer, 10486 htab_eq_pointer, 10487 NULL); 10488 10489 /* Set up context. */ 10490 import_export_decl (d); 10491 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED); 10492 10493 /* Substitute into the body of the function. */ 10494 tsubst_expr (DECL_SAVED_TREE (code_pattern), args, 10495 tf_error | tf_warning, tmpl); 10496 10497 /* We don't need the local specializations any more. */ 10498 htab_delete (local_specializations); 10499 local_specializations = saved_local_specializations; 10500 10501 /* Finish the function. */ 10502 d = finish_function (0); 10503 expand_body (d); 10504 } 10505 10506 /* We're not deferring instantiation any more. */ 10507 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; 10508 10509 if (need_push) 10510 pop_from_top_level (); 10511 10512out: 10513 lineno = line; 10514 input_filename = file; 10515 10516 pop_tinst_level (); 10517 10518 timevar_pop (TV_PARSE); 10519 10520 return d; 10521} 10522 10523/* Run through the list of templates that we wish we could 10524 instantiate, and instantiate any we can. */ 10525 10526int 10527instantiate_pending_templates () 10528{ 10529 tree *t; 10530 tree last = NULL_TREE; 10531 int instantiated_something = 0; 10532 int reconsider; 10533 10534 do 10535 { 10536 reconsider = 0; 10537 10538 t = &pending_templates; 10539 while (*t) 10540 { 10541 tree instantiation = TREE_VALUE (*t); 10542 10543 reopen_tinst_level (TREE_PURPOSE (*t)); 10544 10545 if (TYPE_P (instantiation)) 10546 { 10547 tree fn; 10548 10549 if (!COMPLETE_TYPE_P (instantiation)) 10550 { 10551 instantiate_class_template (instantiation); 10552 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) 10553 for (fn = TYPE_METHODS (instantiation); 10554 fn; 10555 fn = TREE_CHAIN (fn)) 10556 if (! DECL_ARTIFICIAL (fn)) 10557 instantiate_decl (fn, /*defer_ok=*/0); 10558 if (COMPLETE_TYPE_P (instantiation)) 10559 { 10560 instantiated_something = 1; 10561 reconsider = 1; 10562 } 10563 } 10564 10565 if (COMPLETE_TYPE_P (instantiation)) 10566 /* If INSTANTIATION has been instantiated, then we don't 10567 need to consider it again in the future. */ 10568 *t = TREE_CHAIN (*t); 10569 else 10570 { 10571 last = *t; 10572 t = &TREE_CHAIN (*t); 10573 } 10574 } 10575 else 10576 { 10577 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) 10578 && !DECL_TEMPLATE_INSTANTIATED (instantiation)) 10579 { 10580 instantiation = instantiate_decl (instantiation, 10581 /*defer_ok=*/0); 10582 if (DECL_TEMPLATE_INSTANTIATED (instantiation)) 10583 { 10584 instantiated_something = 1; 10585 reconsider = 1; 10586 } 10587 } 10588 10589 if (DECL_TEMPLATE_SPECIALIZATION (instantiation) 10590 || DECL_TEMPLATE_INSTANTIATED (instantiation)) 10591 /* If INSTANTIATION has been instantiated, then we don't 10592 need to consider it again in the future. */ 10593 *t = TREE_CHAIN (*t); 10594 else 10595 { 10596 last = *t; 10597 t = &TREE_CHAIN (*t); 10598 } 10599 } 10600 tinst_depth = 0; 10601 current_tinst_level = NULL_TREE; 10602 } 10603 last_pending_template = last; 10604 } 10605 while (reconsider); 10606 10607 return instantiated_something; 10608} 10609 10610/* Substitute ARGVEC into T, which is a list of initializers for 10611 either base class or a non-static data member. The TREE_PURPOSEs 10612 are DECLs, and the TREE_VALUEs are the initializer values. Used by 10613 instantiate_decl. */ 10614 10615static tree 10616tsubst_initializer_list (t, argvec) 10617 tree t, argvec; 10618{ 10619 tree inits = NULL_TREE; 10620 10621 for (; t; t = TREE_CHAIN (t)) 10622 { 10623 tree decl; 10624 tree init; 10625 tree val; 10626 10627 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning, 10628 NULL_TREE); 10629 decl = expand_member_init (decl); 10630 if (decl && !DECL_P (decl)) 10631 in_base_initializer = 1; 10632 10633 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning, 10634 NULL_TREE); 10635 if (!init) 10636 ; 10637 else if (TREE_CODE (init) == TREE_LIST) 10638 for (val = init; val; val = TREE_CHAIN (val)) 10639 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val)); 10640 else if (init != void_type_node) 10641 init = convert_from_reference (init); 10642 10643 in_base_initializer = 0; 10644 10645 if (decl) 10646 { 10647 init = build_tree_list (decl, init); 10648 TREE_CHAIN (init) = inits; 10649 inits = init; 10650 } 10651 } 10652 return inits; 10653} 10654 10655/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ 10656 10657static void 10658set_current_access_from_decl (decl) 10659 tree decl; 10660{ 10661 if (TREE_PRIVATE (decl)) 10662 current_access_specifier = access_private_node; 10663 else if (TREE_PROTECTED (decl)) 10664 current_access_specifier = access_protected_node; 10665 else 10666 current_access_specifier = access_public_node; 10667} 10668 10669/* Instantiate an enumerated type. TAG is the template type, NEWTAG 10670 is the instantiation (which should have been created with 10671 start_enum) and ARGS are the template arguments to use. */ 10672 10673static void 10674tsubst_enum (tag, newtag, args) 10675 tree tag; 10676 tree newtag; 10677 tree args; 10678{ 10679 tree e; 10680 10681 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) 10682 { 10683 tree value; 10684 10685 /* Note that in a template enum, the TREE_VALUE is the 10686 CONST_DECL, not the corresponding INTEGER_CST. */ 10687 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), 10688 args, tf_error | tf_warning, 10689 NULL_TREE); 10690 10691 /* Give this enumeration constant the correct access. */ 10692 set_current_access_from_decl (TREE_VALUE (e)); 10693 10694 /* Actually build the enumerator itself. */ 10695 build_enumerator (TREE_PURPOSE (e), value, newtag); 10696 } 10697 10698 finish_enum (newtag); 10699 DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) 10700 = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); 10701} 10702 10703/* DECL is a FUNCTION_DECL that is a template specialization. Return 10704 its type -- but without substituting the innermost set of template 10705 arguments. So, innermost set of template parameters will appear in 10706 the type. */ 10707 10708tree 10709get_mostly_instantiated_function_type (decl) 10710 tree decl; 10711{ 10712 tree fn_type; 10713 tree tmpl; 10714 tree targs; 10715 tree tparms; 10716 int parm_depth; 10717 10718 tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); 10719 targs = DECL_TI_ARGS (decl); 10720 tparms = DECL_TEMPLATE_PARMS (tmpl); 10721 parm_depth = TMPL_PARMS_DEPTH (tparms); 10722 10723 /* There should be as many levels of arguments as there are levels 10724 of parameters. */ 10725 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); 10726 10727 fn_type = TREE_TYPE (tmpl); 10728 10729 if (parm_depth == 1) 10730 /* No substitution is necessary. */ 10731 ; 10732 else 10733 { 10734 int i; 10735 tree partial_args; 10736 10737 /* Replace the innermost level of the TARGS with NULL_TREEs to 10738 let tsubst know not to substitute for those parameters. */ 10739 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); 10740 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) 10741 SET_TMPL_ARGS_LEVEL (partial_args, i, 10742 TMPL_ARGS_LEVEL (targs, i)); 10743 SET_TMPL_ARGS_LEVEL (partial_args, 10744 TMPL_ARGS_DEPTH (targs), 10745 make_tree_vec (DECL_NTPARMS (tmpl))); 10746 10747 /* Make sure that we can see identifiers, and compute access 10748 correctly. We can just use the context of DECL for the 10749 partial substitution here. It depends only on outer template 10750 parameters, regardless of whether the innermost level is 10751 specialized or not. */ 10752 push_access_scope (decl); 10753 10754 /* Now, do the (partial) substitution to figure out the 10755 appropriate function type. */ 10756 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); 10757 10758 /* Substitute into the template parameters to obtain the real 10759 innermost set of parameters. This step is important if the 10760 innermost set of template parameters contains value 10761 parameters whose types depend on outer template parameters. */ 10762 TREE_VEC_LENGTH (partial_args)--; 10763 tparms = tsubst_template_parms (tparms, partial_args, tf_error); 10764 10765 pop_access_scope (decl); 10766 } 10767 10768 return fn_type; 10769} 10770 10771/* Return truthvalue if we're processing a template different from 10772 the last one involved in diagnostics. */ 10773int 10774problematic_instantiation_changed () 10775{ 10776 return last_template_error_tick != tinst_level_tick; 10777} 10778 10779/* Remember current template involved in diagnostics. */ 10780void 10781record_last_problematic_instantiation () 10782{ 10783 last_template_error_tick = tinst_level_tick; 10784} 10785 10786tree 10787current_instantiation () 10788{ 10789 return current_tinst_level; 10790} 10791 10792/* [temp.param] Check that template non-type parm TYPE is of an allowable 10793 type. Return zero for ok, nonzero for disallowed. Issue error and 10794 warning messages under control of COMPLAIN. */ 10795 10796static int 10797invalid_nontype_parm_type_p (type, complain) 10798 tree type; 10799 tsubst_flags_t complain; 10800{ 10801 if (INTEGRAL_TYPE_P (type)) 10802 return 0; 10803 else if (POINTER_TYPE_P (type)) 10804 return 0; 10805 else if (TYPE_PTRMEM_P (type)) 10806 return 0; 10807 else if (TYPE_PTRMEMFUNC_P (type)) 10808 return 0; 10809 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 10810 return 0; 10811 else if (TREE_CODE (type) == TYPENAME_TYPE) 10812 return 0; 10813 10814 if (complain & tf_error) 10815 error ("`%#T' is not a valid type for a template constant parameter", 10816 type); 10817 return 1; 10818} 10819 10820#include "gt-cp-pt.h" 10821