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